xref: /OK3568_Linux_fs/kernel/drivers/char/uv_mmtimer.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Timer device implementation for SGI UV platform.
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * This file is subject to the terms and conditions of the GNU General Public
5*4882a593Smuzhiyun  * License.  See the file "COPYING" in the main directory of this archive
6*4882a593Smuzhiyun  * for more details.
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * Copyright (c) 2009 Silicon Graphics, Inc.  All rights reserved.
9*4882a593Smuzhiyun  *
10*4882a593Smuzhiyun  */
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun #include <linux/types.h>
13*4882a593Smuzhiyun #include <linux/kernel.h>
14*4882a593Smuzhiyun #include <linux/ioctl.h>
15*4882a593Smuzhiyun #include <linux/module.h>
16*4882a593Smuzhiyun #include <linux/init.h>
17*4882a593Smuzhiyun #include <linux/errno.h>
18*4882a593Smuzhiyun #include <linux/mm.h>
19*4882a593Smuzhiyun #include <linux/fs.h>
20*4882a593Smuzhiyun #include <linux/mmtimer.h>
21*4882a593Smuzhiyun #include <linux/miscdevice.h>
22*4882a593Smuzhiyun #include <linux/posix-timers.h>
23*4882a593Smuzhiyun #include <linux/interrupt.h>
24*4882a593Smuzhiyun #include <linux/time.h>
25*4882a593Smuzhiyun #include <linux/math64.h>
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun #include <asm/genapic.h>
28*4882a593Smuzhiyun #include <asm/uv/uv_hub.h>
29*4882a593Smuzhiyun #include <asm/uv/bios.h>
30*4882a593Smuzhiyun #include <asm/uv/uv.h>
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun MODULE_AUTHOR("Dimitri Sivanich <sivanich@sgi.com>");
33*4882a593Smuzhiyun MODULE_DESCRIPTION("SGI UV Memory Mapped RTC Timer");
34*4882a593Smuzhiyun MODULE_LICENSE("GPL");
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun /* name of the device, usually in /dev */
37*4882a593Smuzhiyun #define UV_MMTIMER_NAME "mmtimer"
38*4882a593Smuzhiyun #define UV_MMTIMER_DESC "SGI UV Memory Mapped RTC Timer"
39*4882a593Smuzhiyun #define UV_MMTIMER_VERSION "1.0"
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun static long uv_mmtimer_ioctl(struct file *file, unsigned int cmd,
42*4882a593Smuzhiyun 						unsigned long arg);
43*4882a593Smuzhiyun static int uv_mmtimer_mmap(struct file *file, struct vm_area_struct *vma);
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun /*
46*4882a593Smuzhiyun  * Period in femtoseconds (10^-15 s)
47*4882a593Smuzhiyun  */
48*4882a593Smuzhiyun static unsigned long uv_mmtimer_femtoperiod;
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun static const struct file_operations uv_mmtimer_fops = {
51*4882a593Smuzhiyun 	.owner = THIS_MODULE,
52*4882a593Smuzhiyun 	.mmap =	uv_mmtimer_mmap,
53*4882a593Smuzhiyun 	.unlocked_ioctl = uv_mmtimer_ioctl,
54*4882a593Smuzhiyun 	.llseek = noop_llseek,
55*4882a593Smuzhiyun };
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun /**
58*4882a593Smuzhiyun  * uv_mmtimer_ioctl - ioctl interface for /dev/uv_mmtimer
59*4882a593Smuzhiyun  * @file: file structure for the device
60*4882a593Smuzhiyun  * @cmd: command to execute
61*4882a593Smuzhiyun  * @arg: optional argument to command
62*4882a593Smuzhiyun  *
63*4882a593Smuzhiyun  * Executes the command specified by @cmd.  Returns 0 for success, < 0 for
64*4882a593Smuzhiyun  * failure.
65*4882a593Smuzhiyun  *
66*4882a593Smuzhiyun  * Valid commands:
67*4882a593Smuzhiyun  *
68*4882a593Smuzhiyun  * %MMTIMER_GETOFFSET - Should return the offset (relative to the start
69*4882a593Smuzhiyun  * of the page where the registers are mapped) for the counter in question.
70*4882a593Smuzhiyun  *
71*4882a593Smuzhiyun  * %MMTIMER_GETRES - Returns the resolution of the clock in femto (10^-15)
72*4882a593Smuzhiyun  * seconds
73*4882a593Smuzhiyun  *
74*4882a593Smuzhiyun  * %MMTIMER_GETFREQ - Copies the frequency of the clock in Hz to the address
75*4882a593Smuzhiyun  * specified by @arg
76*4882a593Smuzhiyun  *
77*4882a593Smuzhiyun  * %MMTIMER_GETBITS - Returns the number of bits in the clock's counter
78*4882a593Smuzhiyun  *
79*4882a593Smuzhiyun  * %MMTIMER_MMAPAVAIL - Returns 1 if registers can be mmap'd into userspace
80*4882a593Smuzhiyun  *
81*4882a593Smuzhiyun  * %MMTIMER_GETCOUNTER - Gets the current value in the counter and places it
82*4882a593Smuzhiyun  * in the address specified by @arg.
83*4882a593Smuzhiyun  */
uv_mmtimer_ioctl(struct file * file,unsigned int cmd,unsigned long arg)84*4882a593Smuzhiyun static long uv_mmtimer_ioctl(struct file *file, unsigned int cmd,
85*4882a593Smuzhiyun 						unsigned long arg)
86*4882a593Smuzhiyun {
87*4882a593Smuzhiyun 	int ret = 0;
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun 	switch (cmd) {
90*4882a593Smuzhiyun 	case MMTIMER_GETOFFSET:	/* offset of the counter */
91*4882a593Smuzhiyun 		/*
92*4882a593Smuzhiyun 		 * Starting with HUB rev 2.0, the UV RTC register is
93*4882a593Smuzhiyun 		 * replicated across all cachelines of it's own page.
94*4882a593Smuzhiyun 		 * This allows faster simultaneous reads from a given socket.
95*4882a593Smuzhiyun 		 *
96*4882a593Smuzhiyun 		 * The offset returned is in 64 bit units.
97*4882a593Smuzhiyun 		 */
98*4882a593Smuzhiyun 		if (uv_get_min_hub_revision_id() == 1)
99*4882a593Smuzhiyun 			ret = 0;
100*4882a593Smuzhiyun 		else
101*4882a593Smuzhiyun 			ret = ((uv_blade_processor_id() * L1_CACHE_BYTES) %
102*4882a593Smuzhiyun 					PAGE_SIZE) / 8;
103*4882a593Smuzhiyun 		break;
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun 	case MMTIMER_GETRES: /* resolution of the clock in 10^-15 s */
106*4882a593Smuzhiyun 		if (copy_to_user((unsigned long __user *)arg,
107*4882a593Smuzhiyun 				&uv_mmtimer_femtoperiod, sizeof(unsigned long)))
108*4882a593Smuzhiyun 			ret = -EFAULT;
109*4882a593Smuzhiyun 		break;
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun 	case MMTIMER_GETFREQ: /* frequency in Hz */
112*4882a593Smuzhiyun 		if (copy_to_user((unsigned long __user *)arg,
113*4882a593Smuzhiyun 				&sn_rtc_cycles_per_second,
114*4882a593Smuzhiyun 				sizeof(unsigned long)))
115*4882a593Smuzhiyun 			ret = -EFAULT;
116*4882a593Smuzhiyun 		break;
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun 	case MMTIMER_GETBITS: /* number of bits in the clock */
119*4882a593Smuzhiyun 		ret = hweight64(UVH_RTC_REAL_TIME_CLOCK_MASK);
120*4882a593Smuzhiyun 		break;
121*4882a593Smuzhiyun 
122*4882a593Smuzhiyun 	case MMTIMER_MMAPAVAIL:
123*4882a593Smuzhiyun 		ret = 1;
124*4882a593Smuzhiyun 		break;
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun 	case MMTIMER_GETCOUNTER:
127*4882a593Smuzhiyun 		if (copy_to_user((unsigned long __user *)arg,
128*4882a593Smuzhiyun 				(unsigned long *)uv_local_mmr_address(UVH_RTC),
129*4882a593Smuzhiyun 				sizeof(unsigned long)))
130*4882a593Smuzhiyun 			ret = -EFAULT;
131*4882a593Smuzhiyun 		break;
132*4882a593Smuzhiyun 	default:
133*4882a593Smuzhiyun 		ret = -ENOTTY;
134*4882a593Smuzhiyun 		break;
135*4882a593Smuzhiyun 	}
136*4882a593Smuzhiyun 	return ret;
137*4882a593Smuzhiyun }
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun /**
140*4882a593Smuzhiyun  * uv_mmtimer_mmap - maps the clock's registers into userspace
141*4882a593Smuzhiyun  * @file: file structure for the device
142*4882a593Smuzhiyun  * @vma: VMA to map the registers into
143*4882a593Smuzhiyun  *
144*4882a593Smuzhiyun  * Calls remap_pfn_range() to map the clock's registers into
145*4882a593Smuzhiyun  * the calling process' address space.
146*4882a593Smuzhiyun  */
uv_mmtimer_mmap(struct file * file,struct vm_area_struct * vma)147*4882a593Smuzhiyun static int uv_mmtimer_mmap(struct file *file, struct vm_area_struct *vma)
148*4882a593Smuzhiyun {
149*4882a593Smuzhiyun 	unsigned long uv_mmtimer_addr;
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun 	if (vma->vm_end - vma->vm_start != PAGE_SIZE)
152*4882a593Smuzhiyun 		return -EINVAL;
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun 	if (vma->vm_flags & VM_WRITE)
155*4882a593Smuzhiyun 		return -EPERM;
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun 	if (PAGE_SIZE > (1 << 16))
158*4882a593Smuzhiyun 		return -ENOSYS;
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun 	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun 	uv_mmtimer_addr = UV_LOCAL_MMR_BASE | UVH_RTC;
163*4882a593Smuzhiyun 	uv_mmtimer_addr &= ~(PAGE_SIZE - 1);
164*4882a593Smuzhiyun 	uv_mmtimer_addr &= 0xfffffffffffffffUL;
165*4882a593Smuzhiyun 
166*4882a593Smuzhiyun 	if (remap_pfn_range(vma, vma->vm_start, uv_mmtimer_addr >> PAGE_SHIFT,
167*4882a593Smuzhiyun 					PAGE_SIZE, vma->vm_page_prot)) {
168*4882a593Smuzhiyun 		printk(KERN_ERR "remap_pfn_range failed in uv_mmtimer_mmap\n");
169*4882a593Smuzhiyun 		return -EAGAIN;
170*4882a593Smuzhiyun 	}
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun 	return 0;
173*4882a593Smuzhiyun }
174*4882a593Smuzhiyun 
175*4882a593Smuzhiyun static struct miscdevice uv_mmtimer_miscdev = {
176*4882a593Smuzhiyun 	MISC_DYNAMIC_MINOR,
177*4882a593Smuzhiyun 	UV_MMTIMER_NAME,
178*4882a593Smuzhiyun 	&uv_mmtimer_fops
179*4882a593Smuzhiyun };
180*4882a593Smuzhiyun 
181*4882a593Smuzhiyun 
182*4882a593Smuzhiyun /**
183*4882a593Smuzhiyun  * uv_mmtimer_init - device initialization routine
184*4882a593Smuzhiyun  *
185*4882a593Smuzhiyun  * Does initial setup for the uv_mmtimer device.
186*4882a593Smuzhiyun  */
uv_mmtimer_init(void)187*4882a593Smuzhiyun static int __init uv_mmtimer_init(void)
188*4882a593Smuzhiyun {
189*4882a593Smuzhiyun 	if (!is_uv_system()) {
190*4882a593Smuzhiyun 		printk(KERN_ERR "%s: Hardware unsupported\n", UV_MMTIMER_NAME);
191*4882a593Smuzhiyun 		return -1;
192*4882a593Smuzhiyun 	}
193*4882a593Smuzhiyun 
194*4882a593Smuzhiyun 	/*
195*4882a593Smuzhiyun 	 * Sanity check the cycles/sec variable
196*4882a593Smuzhiyun 	 */
197*4882a593Smuzhiyun 	if (sn_rtc_cycles_per_second < 100000) {
198*4882a593Smuzhiyun 		printk(KERN_ERR "%s: unable to determine clock frequency\n",
199*4882a593Smuzhiyun 		       UV_MMTIMER_NAME);
200*4882a593Smuzhiyun 		return -1;
201*4882a593Smuzhiyun 	}
202*4882a593Smuzhiyun 
203*4882a593Smuzhiyun 	uv_mmtimer_femtoperiod = ((unsigned long)1E15 +
204*4882a593Smuzhiyun 				sn_rtc_cycles_per_second / 2) /
205*4882a593Smuzhiyun 				sn_rtc_cycles_per_second;
206*4882a593Smuzhiyun 
207*4882a593Smuzhiyun 	if (misc_register(&uv_mmtimer_miscdev)) {
208*4882a593Smuzhiyun 		printk(KERN_ERR "%s: failed to register device\n",
209*4882a593Smuzhiyun 		       UV_MMTIMER_NAME);
210*4882a593Smuzhiyun 		return -1;
211*4882a593Smuzhiyun 	}
212*4882a593Smuzhiyun 
213*4882a593Smuzhiyun 	printk(KERN_INFO "%s: v%s, %ld MHz\n", UV_MMTIMER_DESC,
214*4882a593Smuzhiyun 		UV_MMTIMER_VERSION,
215*4882a593Smuzhiyun 		sn_rtc_cycles_per_second/(unsigned long)1E6);
216*4882a593Smuzhiyun 
217*4882a593Smuzhiyun 	return 0;
218*4882a593Smuzhiyun }
219*4882a593Smuzhiyun 
220*4882a593Smuzhiyun module_init(uv_mmtimer_init);
221