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