Commit 4cddb886a4d0e5cc7a790151740bfb87b568c97d
Committed by
Tony Luck
1 parent
fb86611f8f
Exists in
master
and in
7 other branches
mmtimer: Push BKL down into the ioctl handler
Switches to unlocked_ioctl read to remove ioctl BKL method. Fix the unknown ioctl return. Probably a nice easy one to kill off BKL usage entirely later Signed-off-by: Alan Cox <alan@redhat.com> Acked-by: Jes Sorensen <jes@sgi.com> Signed-off-by: Tony Luck <tony.luck@intel.com>
Showing 1 changed file with 15 additions and 14 deletions Inline Diff
drivers/char/mmtimer.c
1 | /* | 1 | /* |
2 | * Timer device implementation for SGI SN platforms. | 2 | * Timer device implementation for SGI SN platforms. |
3 | * | 3 | * |
4 | * This file is subject to the terms and conditions of the GNU General Public | 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 | 5 | * License. See the file "COPYING" in the main directory of this archive |
6 | * for more details. | 6 | * for more details. |
7 | * | 7 | * |
8 | * Copyright (c) 2001-2006 Silicon Graphics, Inc. All rights reserved. | 8 | * Copyright (c) 2001-2006 Silicon Graphics, Inc. All rights reserved. |
9 | * | 9 | * |
10 | * This driver exports an API that should be supportable by any HPET or IA-PC | 10 | * This driver exports an API that should be supportable by any HPET or IA-PC |
11 | * multimedia timer. The code below is currently specific to the SGI Altix | 11 | * multimedia timer. The code below is currently specific to the SGI Altix |
12 | * SHub RTC, however. | 12 | * SHub RTC, however. |
13 | * | 13 | * |
14 | * 11/01/01 - jbarnes - initial revision | 14 | * 11/01/01 - jbarnes - initial revision |
15 | * 9/10/04 - Christoph Lameter - remove interrupt support for kernel inclusion | 15 | * 9/10/04 - Christoph Lameter - remove interrupt support for kernel inclusion |
16 | * 10/1/04 - Christoph Lameter - provide posix clock CLOCK_SGI_CYCLE | 16 | * 10/1/04 - Christoph Lameter - provide posix clock CLOCK_SGI_CYCLE |
17 | * 10/13/04 - Christoph Lameter, Dimitri Sivanich - provide timer interrupt | 17 | * 10/13/04 - Christoph Lameter, Dimitri Sivanich - provide timer interrupt |
18 | * support via the posix timer interface | 18 | * support via the posix timer interface |
19 | */ | 19 | */ |
20 | 20 | ||
21 | #include <linux/types.h> | 21 | #include <linux/types.h> |
22 | #include <linux/kernel.h> | 22 | #include <linux/kernel.h> |
23 | #include <linux/ioctl.h> | 23 | #include <linux/ioctl.h> |
24 | #include <linux/module.h> | 24 | #include <linux/module.h> |
25 | #include <linux/init.h> | 25 | #include <linux/init.h> |
26 | #include <linux/errno.h> | 26 | #include <linux/errno.h> |
27 | #include <linux/mm.h> | 27 | #include <linux/mm.h> |
28 | #include <linux/fs.h> | 28 | #include <linux/fs.h> |
29 | #include <linux/mmtimer.h> | 29 | #include <linux/mmtimer.h> |
30 | #include <linux/miscdevice.h> | 30 | #include <linux/miscdevice.h> |
31 | #include <linux/posix-timers.h> | 31 | #include <linux/posix-timers.h> |
32 | #include <linux/interrupt.h> | 32 | #include <linux/interrupt.h> |
33 | #include <linux/time.h> | 33 | #include <linux/time.h> |
34 | #include <linux/math64.h> | 34 | #include <linux/math64.h> |
35 | #include <linux/smp_lock.h> | ||
35 | 36 | ||
36 | #include <asm/uaccess.h> | 37 | #include <asm/uaccess.h> |
37 | #include <asm/sn/addrs.h> | 38 | #include <asm/sn/addrs.h> |
38 | #include <asm/sn/intr.h> | 39 | #include <asm/sn/intr.h> |
39 | #include <asm/sn/shub_mmr.h> | 40 | #include <asm/sn/shub_mmr.h> |
40 | #include <asm/sn/nodepda.h> | 41 | #include <asm/sn/nodepda.h> |
41 | #include <asm/sn/shubio.h> | 42 | #include <asm/sn/shubio.h> |
42 | 43 | ||
43 | MODULE_AUTHOR("Jesse Barnes <jbarnes@sgi.com>"); | 44 | MODULE_AUTHOR("Jesse Barnes <jbarnes@sgi.com>"); |
44 | MODULE_DESCRIPTION("SGI Altix RTC Timer"); | 45 | MODULE_DESCRIPTION("SGI Altix RTC Timer"); |
45 | MODULE_LICENSE("GPL"); | 46 | MODULE_LICENSE("GPL"); |
46 | 47 | ||
47 | /* name of the device, usually in /dev */ | 48 | /* name of the device, usually in /dev */ |
48 | #define MMTIMER_NAME "mmtimer" | 49 | #define MMTIMER_NAME "mmtimer" |
49 | #define MMTIMER_DESC "SGI Altix RTC Timer" | 50 | #define MMTIMER_DESC "SGI Altix RTC Timer" |
50 | #define MMTIMER_VERSION "2.1" | 51 | #define MMTIMER_VERSION "2.1" |
51 | 52 | ||
52 | #define RTC_BITS 55 /* 55 bits for this implementation */ | 53 | #define RTC_BITS 55 /* 55 bits for this implementation */ |
53 | 54 | ||
54 | extern unsigned long sn_rtc_cycles_per_second; | 55 | extern unsigned long sn_rtc_cycles_per_second; |
55 | 56 | ||
56 | #define RTC_COUNTER_ADDR ((long *)LOCAL_MMR_ADDR(SH_RTC)) | 57 | #define RTC_COUNTER_ADDR ((long *)LOCAL_MMR_ADDR(SH_RTC)) |
57 | 58 | ||
58 | #define rtc_time() (*RTC_COUNTER_ADDR) | 59 | #define rtc_time() (*RTC_COUNTER_ADDR) |
59 | 60 | ||
60 | static int mmtimer_ioctl(struct inode *inode, struct file *file, | 61 | static long mmtimer_ioctl(struct file *file, unsigned int cmd, |
61 | unsigned int cmd, unsigned long arg); | 62 | unsigned long arg); |
62 | static int mmtimer_mmap(struct file *file, struct vm_area_struct *vma); | 63 | static int mmtimer_mmap(struct file *file, struct vm_area_struct *vma); |
63 | 64 | ||
64 | /* | 65 | /* |
65 | * Period in femtoseconds (10^-15 s) | 66 | * Period in femtoseconds (10^-15 s) |
66 | */ | 67 | */ |
67 | static unsigned long mmtimer_femtoperiod = 0; | 68 | static unsigned long mmtimer_femtoperiod = 0; |
68 | 69 | ||
69 | static const struct file_operations mmtimer_fops = { | 70 | static const struct file_operations mmtimer_fops = { |
70 | .owner = THIS_MODULE, | 71 | .owner = THIS_MODULE, |
71 | .mmap = mmtimer_mmap, | 72 | .mmap = mmtimer_mmap, |
72 | .ioctl = mmtimer_ioctl, | 73 | .unlocked_ioctl = mmtimer_ioctl, |
73 | }; | 74 | }; |
74 | 75 | ||
75 | /* | 76 | /* |
76 | * We only have comparison registers RTC1-4 currently available per | 77 | * We only have comparison registers RTC1-4 currently available per |
77 | * node. RTC0 is used by SAL. | 78 | * node. RTC0 is used by SAL. |
78 | */ | 79 | */ |
79 | /* Check for an RTC interrupt pending */ | 80 | /* Check for an RTC interrupt pending */ |
80 | static int mmtimer_int_pending(int comparator) | 81 | static int mmtimer_int_pending(int comparator) |
81 | { | 82 | { |
82 | if (HUB_L((unsigned long *)LOCAL_MMR_ADDR(SH_EVENT_OCCURRED)) & | 83 | if (HUB_L((unsigned long *)LOCAL_MMR_ADDR(SH_EVENT_OCCURRED)) & |
83 | SH_EVENT_OCCURRED_RTC1_INT_MASK << comparator) | 84 | SH_EVENT_OCCURRED_RTC1_INT_MASK << comparator) |
84 | return 1; | 85 | return 1; |
85 | else | 86 | else |
86 | return 0; | 87 | return 0; |
87 | } | 88 | } |
88 | 89 | ||
89 | /* Clear the RTC interrupt pending bit */ | 90 | /* Clear the RTC interrupt pending bit */ |
90 | static void mmtimer_clr_int_pending(int comparator) | 91 | static void mmtimer_clr_int_pending(int comparator) |
91 | { | 92 | { |
92 | HUB_S((u64 *)LOCAL_MMR_ADDR(SH_EVENT_OCCURRED_ALIAS), | 93 | HUB_S((u64 *)LOCAL_MMR_ADDR(SH_EVENT_OCCURRED_ALIAS), |
93 | SH_EVENT_OCCURRED_RTC1_INT_MASK << comparator); | 94 | SH_EVENT_OCCURRED_RTC1_INT_MASK << comparator); |
94 | } | 95 | } |
95 | 96 | ||
96 | /* Setup timer on comparator RTC1 */ | 97 | /* Setup timer on comparator RTC1 */ |
97 | static void mmtimer_setup_int_0(int cpu, u64 expires) | 98 | static void mmtimer_setup_int_0(int cpu, u64 expires) |
98 | { | 99 | { |
99 | u64 val; | 100 | u64 val; |
100 | 101 | ||
101 | /* Disable interrupt */ | 102 | /* Disable interrupt */ |
102 | HUB_S((u64 *)LOCAL_MMR_ADDR(SH_RTC1_INT_ENABLE), 0UL); | 103 | HUB_S((u64 *)LOCAL_MMR_ADDR(SH_RTC1_INT_ENABLE), 0UL); |
103 | 104 | ||
104 | /* Initialize comparator value */ | 105 | /* Initialize comparator value */ |
105 | HUB_S((u64 *)LOCAL_MMR_ADDR(SH_INT_CMPB), -1L); | 106 | HUB_S((u64 *)LOCAL_MMR_ADDR(SH_INT_CMPB), -1L); |
106 | 107 | ||
107 | /* Clear pending bit */ | 108 | /* Clear pending bit */ |
108 | mmtimer_clr_int_pending(0); | 109 | mmtimer_clr_int_pending(0); |
109 | 110 | ||
110 | val = ((u64)SGI_MMTIMER_VECTOR << SH_RTC1_INT_CONFIG_IDX_SHFT) | | 111 | val = ((u64)SGI_MMTIMER_VECTOR << SH_RTC1_INT_CONFIG_IDX_SHFT) | |
111 | ((u64)cpu_physical_id(cpu) << | 112 | ((u64)cpu_physical_id(cpu) << |
112 | SH_RTC1_INT_CONFIG_PID_SHFT); | 113 | SH_RTC1_INT_CONFIG_PID_SHFT); |
113 | 114 | ||
114 | /* Set configuration */ | 115 | /* Set configuration */ |
115 | HUB_S((u64 *)LOCAL_MMR_ADDR(SH_RTC1_INT_CONFIG), val); | 116 | HUB_S((u64 *)LOCAL_MMR_ADDR(SH_RTC1_INT_CONFIG), val); |
116 | 117 | ||
117 | /* Enable RTC interrupts */ | 118 | /* Enable RTC interrupts */ |
118 | HUB_S((u64 *)LOCAL_MMR_ADDR(SH_RTC1_INT_ENABLE), 1UL); | 119 | HUB_S((u64 *)LOCAL_MMR_ADDR(SH_RTC1_INT_ENABLE), 1UL); |
119 | 120 | ||
120 | /* Initialize comparator value */ | 121 | /* Initialize comparator value */ |
121 | HUB_S((u64 *)LOCAL_MMR_ADDR(SH_INT_CMPB), expires); | 122 | HUB_S((u64 *)LOCAL_MMR_ADDR(SH_INT_CMPB), expires); |
122 | 123 | ||
123 | 124 | ||
124 | } | 125 | } |
125 | 126 | ||
126 | /* Setup timer on comparator RTC2 */ | 127 | /* Setup timer on comparator RTC2 */ |
127 | static void mmtimer_setup_int_1(int cpu, u64 expires) | 128 | static void mmtimer_setup_int_1(int cpu, u64 expires) |
128 | { | 129 | { |
129 | u64 val; | 130 | u64 val; |
130 | 131 | ||
131 | HUB_S((u64 *)LOCAL_MMR_ADDR(SH_RTC2_INT_ENABLE), 0UL); | 132 | HUB_S((u64 *)LOCAL_MMR_ADDR(SH_RTC2_INT_ENABLE), 0UL); |
132 | 133 | ||
133 | HUB_S((u64 *)LOCAL_MMR_ADDR(SH_INT_CMPC), -1L); | 134 | HUB_S((u64 *)LOCAL_MMR_ADDR(SH_INT_CMPC), -1L); |
134 | 135 | ||
135 | mmtimer_clr_int_pending(1); | 136 | mmtimer_clr_int_pending(1); |
136 | 137 | ||
137 | val = ((u64)SGI_MMTIMER_VECTOR << SH_RTC2_INT_CONFIG_IDX_SHFT) | | 138 | val = ((u64)SGI_MMTIMER_VECTOR << SH_RTC2_INT_CONFIG_IDX_SHFT) | |
138 | ((u64)cpu_physical_id(cpu) << | 139 | ((u64)cpu_physical_id(cpu) << |
139 | SH_RTC2_INT_CONFIG_PID_SHFT); | 140 | SH_RTC2_INT_CONFIG_PID_SHFT); |
140 | 141 | ||
141 | HUB_S((u64 *)LOCAL_MMR_ADDR(SH_RTC2_INT_CONFIG), val); | 142 | HUB_S((u64 *)LOCAL_MMR_ADDR(SH_RTC2_INT_CONFIG), val); |
142 | 143 | ||
143 | HUB_S((u64 *)LOCAL_MMR_ADDR(SH_RTC2_INT_ENABLE), 1UL); | 144 | HUB_S((u64 *)LOCAL_MMR_ADDR(SH_RTC2_INT_ENABLE), 1UL); |
144 | 145 | ||
145 | HUB_S((u64 *)LOCAL_MMR_ADDR(SH_INT_CMPC), expires); | 146 | HUB_S((u64 *)LOCAL_MMR_ADDR(SH_INT_CMPC), expires); |
146 | } | 147 | } |
147 | 148 | ||
148 | /* Setup timer on comparator RTC3 */ | 149 | /* Setup timer on comparator RTC3 */ |
149 | static void mmtimer_setup_int_2(int cpu, u64 expires) | 150 | static void mmtimer_setup_int_2(int cpu, u64 expires) |
150 | { | 151 | { |
151 | u64 val; | 152 | u64 val; |
152 | 153 | ||
153 | HUB_S((u64 *)LOCAL_MMR_ADDR(SH_RTC3_INT_ENABLE), 0UL); | 154 | HUB_S((u64 *)LOCAL_MMR_ADDR(SH_RTC3_INT_ENABLE), 0UL); |
154 | 155 | ||
155 | HUB_S((u64 *)LOCAL_MMR_ADDR(SH_INT_CMPD), -1L); | 156 | HUB_S((u64 *)LOCAL_MMR_ADDR(SH_INT_CMPD), -1L); |
156 | 157 | ||
157 | mmtimer_clr_int_pending(2); | 158 | mmtimer_clr_int_pending(2); |
158 | 159 | ||
159 | val = ((u64)SGI_MMTIMER_VECTOR << SH_RTC3_INT_CONFIG_IDX_SHFT) | | 160 | val = ((u64)SGI_MMTIMER_VECTOR << SH_RTC3_INT_CONFIG_IDX_SHFT) | |
160 | ((u64)cpu_physical_id(cpu) << | 161 | ((u64)cpu_physical_id(cpu) << |
161 | SH_RTC3_INT_CONFIG_PID_SHFT); | 162 | SH_RTC3_INT_CONFIG_PID_SHFT); |
162 | 163 | ||
163 | HUB_S((u64 *)LOCAL_MMR_ADDR(SH_RTC3_INT_CONFIG), val); | 164 | HUB_S((u64 *)LOCAL_MMR_ADDR(SH_RTC3_INT_CONFIG), val); |
164 | 165 | ||
165 | HUB_S((u64 *)LOCAL_MMR_ADDR(SH_RTC3_INT_ENABLE), 1UL); | 166 | HUB_S((u64 *)LOCAL_MMR_ADDR(SH_RTC3_INT_ENABLE), 1UL); |
166 | 167 | ||
167 | HUB_S((u64 *)LOCAL_MMR_ADDR(SH_INT_CMPD), expires); | 168 | HUB_S((u64 *)LOCAL_MMR_ADDR(SH_INT_CMPD), expires); |
168 | } | 169 | } |
169 | 170 | ||
170 | /* | 171 | /* |
171 | * This function must be called with interrupts disabled and preemption off | 172 | * This function must be called with interrupts disabled and preemption off |
172 | * in order to insure that the setup succeeds in a deterministic time frame. | 173 | * in order to insure that the setup succeeds in a deterministic time frame. |
173 | * It will check if the interrupt setup succeeded. | 174 | * It will check if the interrupt setup succeeded. |
174 | */ | 175 | */ |
175 | static int mmtimer_setup(int cpu, int comparator, unsigned long expires) | 176 | static int mmtimer_setup(int cpu, int comparator, unsigned long expires) |
176 | { | 177 | { |
177 | 178 | ||
178 | switch (comparator) { | 179 | switch (comparator) { |
179 | case 0: | 180 | case 0: |
180 | mmtimer_setup_int_0(cpu, expires); | 181 | mmtimer_setup_int_0(cpu, expires); |
181 | break; | 182 | break; |
182 | case 1: | 183 | case 1: |
183 | mmtimer_setup_int_1(cpu, expires); | 184 | mmtimer_setup_int_1(cpu, expires); |
184 | break; | 185 | break; |
185 | case 2: | 186 | case 2: |
186 | mmtimer_setup_int_2(cpu, expires); | 187 | mmtimer_setup_int_2(cpu, expires); |
187 | break; | 188 | break; |
188 | } | 189 | } |
189 | /* We might've missed our expiration time */ | 190 | /* We might've missed our expiration time */ |
190 | if (rtc_time() <= expires) | 191 | if (rtc_time() <= expires) |
191 | return 1; | 192 | return 1; |
192 | 193 | ||
193 | /* | 194 | /* |
194 | * If an interrupt is already pending then its okay | 195 | * If an interrupt is already pending then its okay |
195 | * if not then we failed | 196 | * if not then we failed |
196 | */ | 197 | */ |
197 | return mmtimer_int_pending(comparator); | 198 | return mmtimer_int_pending(comparator); |
198 | } | 199 | } |
199 | 200 | ||
200 | static int mmtimer_disable_int(long nasid, int comparator) | 201 | static int mmtimer_disable_int(long nasid, int comparator) |
201 | { | 202 | { |
202 | switch (comparator) { | 203 | switch (comparator) { |
203 | case 0: | 204 | case 0: |
204 | nasid == -1 ? HUB_S((u64 *)LOCAL_MMR_ADDR(SH_RTC1_INT_ENABLE), | 205 | nasid == -1 ? HUB_S((u64 *)LOCAL_MMR_ADDR(SH_RTC1_INT_ENABLE), |
205 | 0UL) : REMOTE_HUB_S(nasid, SH_RTC1_INT_ENABLE, 0UL); | 206 | 0UL) : REMOTE_HUB_S(nasid, SH_RTC1_INT_ENABLE, 0UL); |
206 | break; | 207 | break; |
207 | case 1: | 208 | case 1: |
208 | nasid == -1 ? HUB_S((u64 *)LOCAL_MMR_ADDR(SH_RTC2_INT_ENABLE), | 209 | nasid == -1 ? HUB_S((u64 *)LOCAL_MMR_ADDR(SH_RTC2_INT_ENABLE), |
209 | 0UL) : REMOTE_HUB_S(nasid, SH_RTC2_INT_ENABLE, 0UL); | 210 | 0UL) : REMOTE_HUB_S(nasid, SH_RTC2_INT_ENABLE, 0UL); |
210 | break; | 211 | break; |
211 | case 2: | 212 | case 2: |
212 | nasid == -1 ? HUB_S((u64 *)LOCAL_MMR_ADDR(SH_RTC3_INT_ENABLE), | 213 | nasid == -1 ? HUB_S((u64 *)LOCAL_MMR_ADDR(SH_RTC3_INT_ENABLE), |
213 | 0UL) : REMOTE_HUB_S(nasid, SH_RTC3_INT_ENABLE, 0UL); | 214 | 0UL) : REMOTE_HUB_S(nasid, SH_RTC3_INT_ENABLE, 0UL); |
214 | break; | 215 | break; |
215 | default: | 216 | default: |
216 | return -EFAULT; | 217 | return -EFAULT; |
217 | } | 218 | } |
218 | return 0; | 219 | return 0; |
219 | } | 220 | } |
220 | 221 | ||
221 | #define COMPARATOR 1 /* The comparator to use */ | 222 | #define COMPARATOR 1 /* The comparator to use */ |
222 | 223 | ||
223 | #define TIMER_OFF 0xbadcabLL /* Timer is not setup */ | 224 | #define TIMER_OFF 0xbadcabLL /* Timer is not setup */ |
224 | #define TIMER_SET 0 /* Comparator is set for this timer */ | 225 | #define TIMER_SET 0 /* Comparator is set for this timer */ |
225 | 226 | ||
226 | /* There is one of these for each timer */ | 227 | /* There is one of these for each timer */ |
227 | struct mmtimer { | 228 | struct mmtimer { |
228 | struct rb_node list; | 229 | struct rb_node list; |
229 | struct k_itimer *timer; | 230 | struct k_itimer *timer; |
230 | int cpu; | 231 | int cpu; |
231 | }; | 232 | }; |
232 | 233 | ||
233 | struct mmtimer_node { | 234 | struct mmtimer_node { |
234 | spinlock_t lock ____cacheline_aligned; | 235 | spinlock_t lock ____cacheline_aligned; |
235 | struct rb_root timer_head; | 236 | struct rb_root timer_head; |
236 | struct rb_node *next; | 237 | struct rb_node *next; |
237 | struct tasklet_struct tasklet; | 238 | struct tasklet_struct tasklet; |
238 | }; | 239 | }; |
239 | static struct mmtimer_node *timers; | 240 | static struct mmtimer_node *timers; |
240 | 241 | ||
241 | 242 | ||
242 | /* | 243 | /* |
243 | * Add a new mmtimer struct to the node's mmtimer list. | 244 | * Add a new mmtimer struct to the node's mmtimer list. |
244 | * This function assumes the struct mmtimer_node is locked. | 245 | * This function assumes the struct mmtimer_node is locked. |
245 | */ | 246 | */ |
246 | static void mmtimer_add_list(struct mmtimer *n) | 247 | static void mmtimer_add_list(struct mmtimer *n) |
247 | { | 248 | { |
248 | int nodeid = n->timer->it.mmtimer.node; | 249 | int nodeid = n->timer->it.mmtimer.node; |
249 | unsigned long expires = n->timer->it.mmtimer.expires; | 250 | unsigned long expires = n->timer->it.mmtimer.expires; |
250 | struct rb_node **link = &timers[nodeid].timer_head.rb_node; | 251 | struct rb_node **link = &timers[nodeid].timer_head.rb_node; |
251 | struct rb_node *parent = NULL; | 252 | struct rb_node *parent = NULL; |
252 | struct mmtimer *x; | 253 | struct mmtimer *x; |
253 | 254 | ||
254 | /* | 255 | /* |
255 | * Find the right place in the rbtree: | 256 | * Find the right place in the rbtree: |
256 | */ | 257 | */ |
257 | while (*link) { | 258 | while (*link) { |
258 | parent = *link; | 259 | parent = *link; |
259 | x = rb_entry(parent, struct mmtimer, list); | 260 | x = rb_entry(parent, struct mmtimer, list); |
260 | 261 | ||
261 | if (expires < x->timer->it.mmtimer.expires) | 262 | if (expires < x->timer->it.mmtimer.expires) |
262 | link = &(*link)->rb_left; | 263 | link = &(*link)->rb_left; |
263 | else | 264 | else |
264 | link = &(*link)->rb_right; | 265 | link = &(*link)->rb_right; |
265 | } | 266 | } |
266 | 267 | ||
267 | /* | 268 | /* |
268 | * Insert the timer to the rbtree and check whether it | 269 | * Insert the timer to the rbtree and check whether it |
269 | * replaces the first pending timer | 270 | * replaces the first pending timer |
270 | */ | 271 | */ |
271 | rb_link_node(&n->list, parent, link); | 272 | rb_link_node(&n->list, parent, link); |
272 | rb_insert_color(&n->list, &timers[nodeid].timer_head); | 273 | rb_insert_color(&n->list, &timers[nodeid].timer_head); |
273 | 274 | ||
274 | if (!timers[nodeid].next || expires < rb_entry(timers[nodeid].next, | 275 | if (!timers[nodeid].next || expires < rb_entry(timers[nodeid].next, |
275 | struct mmtimer, list)->timer->it.mmtimer.expires) | 276 | struct mmtimer, list)->timer->it.mmtimer.expires) |
276 | timers[nodeid].next = &n->list; | 277 | timers[nodeid].next = &n->list; |
277 | } | 278 | } |
278 | 279 | ||
279 | /* | 280 | /* |
280 | * Set the comparator for the next timer. | 281 | * Set the comparator for the next timer. |
281 | * This function assumes the struct mmtimer_node is locked. | 282 | * This function assumes the struct mmtimer_node is locked. |
282 | */ | 283 | */ |
283 | static void mmtimer_set_next_timer(int nodeid) | 284 | static void mmtimer_set_next_timer(int nodeid) |
284 | { | 285 | { |
285 | struct mmtimer_node *n = &timers[nodeid]; | 286 | struct mmtimer_node *n = &timers[nodeid]; |
286 | struct mmtimer *x; | 287 | struct mmtimer *x; |
287 | struct k_itimer *t; | 288 | struct k_itimer *t; |
288 | int o; | 289 | int o; |
289 | 290 | ||
290 | restart: | 291 | restart: |
291 | if (n->next == NULL) | 292 | if (n->next == NULL) |
292 | return; | 293 | return; |
293 | 294 | ||
294 | x = rb_entry(n->next, struct mmtimer, list); | 295 | x = rb_entry(n->next, struct mmtimer, list); |
295 | t = x->timer; | 296 | t = x->timer; |
296 | if (!t->it.mmtimer.incr) { | 297 | if (!t->it.mmtimer.incr) { |
297 | /* Not an interval timer */ | 298 | /* Not an interval timer */ |
298 | if (!mmtimer_setup(x->cpu, COMPARATOR, | 299 | if (!mmtimer_setup(x->cpu, COMPARATOR, |
299 | t->it.mmtimer.expires)) { | 300 | t->it.mmtimer.expires)) { |
300 | /* Late setup, fire now */ | 301 | /* Late setup, fire now */ |
301 | tasklet_schedule(&n->tasklet); | 302 | tasklet_schedule(&n->tasklet); |
302 | } | 303 | } |
303 | return; | 304 | return; |
304 | } | 305 | } |
305 | 306 | ||
306 | /* Interval timer */ | 307 | /* Interval timer */ |
307 | o = 0; | 308 | o = 0; |
308 | while (!mmtimer_setup(x->cpu, COMPARATOR, t->it.mmtimer.expires)) { | 309 | while (!mmtimer_setup(x->cpu, COMPARATOR, t->it.mmtimer.expires)) { |
309 | unsigned long e, e1; | 310 | unsigned long e, e1; |
310 | struct rb_node *next; | 311 | struct rb_node *next; |
311 | t->it.mmtimer.expires += t->it.mmtimer.incr << o; | 312 | t->it.mmtimer.expires += t->it.mmtimer.incr << o; |
312 | t->it_overrun += 1 << o; | 313 | t->it_overrun += 1 << o; |
313 | o++; | 314 | o++; |
314 | if (o > 20) { | 315 | if (o > 20) { |
315 | printk(KERN_ALERT "mmtimer: cannot reschedule timer\n"); | 316 | printk(KERN_ALERT "mmtimer: cannot reschedule timer\n"); |
316 | t->it.mmtimer.clock = TIMER_OFF; | 317 | t->it.mmtimer.clock = TIMER_OFF; |
317 | n->next = rb_next(&x->list); | 318 | n->next = rb_next(&x->list); |
318 | rb_erase(&x->list, &n->timer_head); | 319 | rb_erase(&x->list, &n->timer_head); |
319 | kfree(x); | 320 | kfree(x); |
320 | goto restart; | 321 | goto restart; |
321 | } | 322 | } |
322 | 323 | ||
323 | e = t->it.mmtimer.expires; | 324 | e = t->it.mmtimer.expires; |
324 | next = rb_next(&x->list); | 325 | next = rb_next(&x->list); |
325 | 326 | ||
326 | if (next == NULL) | 327 | if (next == NULL) |
327 | continue; | 328 | continue; |
328 | 329 | ||
329 | e1 = rb_entry(next, struct mmtimer, list)-> | 330 | e1 = rb_entry(next, struct mmtimer, list)-> |
330 | timer->it.mmtimer.expires; | 331 | timer->it.mmtimer.expires; |
331 | if (e > e1) { | 332 | if (e > e1) { |
332 | n->next = next; | 333 | n->next = next; |
333 | rb_erase(&x->list, &n->timer_head); | 334 | rb_erase(&x->list, &n->timer_head); |
334 | mmtimer_add_list(x); | 335 | mmtimer_add_list(x); |
335 | goto restart; | 336 | goto restart; |
336 | } | 337 | } |
337 | } | 338 | } |
338 | } | 339 | } |
339 | 340 | ||
340 | /** | 341 | /** |
341 | * mmtimer_ioctl - ioctl interface for /dev/mmtimer | 342 | * mmtimer_ioctl - ioctl interface for /dev/mmtimer |
342 | * @inode: inode of the device | ||
343 | * @file: file structure for the device | 343 | * @file: file structure for the device |
344 | * @cmd: command to execute | 344 | * @cmd: command to execute |
345 | * @arg: optional argument to command | 345 | * @arg: optional argument to command |
346 | * | 346 | * |
347 | * Executes the command specified by @cmd. Returns 0 for success, < 0 for | 347 | * Executes the command specified by @cmd. Returns 0 for success, < 0 for |
348 | * failure. | 348 | * failure. |
349 | * | 349 | * |
350 | * Valid commands: | 350 | * Valid commands: |
351 | * | 351 | * |
352 | * %MMTIMER_GETOFFSET - Should return the offset (relative to the start | 352 | * %MMTIMER_GETOFFSET - Should return the offset (relative to the start |
353 | * of the page where the registers are mapped) for the counter in question. | 353 | * of the page where the registers are mapped) for the counter in question. |
354 | * | 354 | * |
355 | * %MMTIMER_GETRES - Returns the resolution of the clock in femto (10^-15) | 355 | * %MMTIMER_GETRES - Returns the resolution of the clock in femto (10^-15) |
356 | * seconds | 356 | * seconds |
357 | * | 357 | * |
358 | * %MMTIMER_GETFREQ - Copies the frequency of the clock in Hz to the address | 358 | * %MMTIMER_GETFREQ - Copies the frequency of the clock in Hz to the address |
359 | * specified by @arg | 359 | * specified by @arg |
360 | * | 360 | * |
361 | * %MMTIMER_GETBITS - Returns the number of bits in the clock's counter | 361 | * %MMTIMER_GETBITS - Returns the number of bits in the clock's counter |
362 | * | 362 | * |
363 | * %MMTIMER_MMAPAVAIL - Returns 1 if the registers can be mmap'd into userspace | 363 | * %MMTIMER_MMAPAVAIL - Returns 1 if the registers can be mmap'd into userspace |
364 | * | 364 | * |
365 | * %MMTIMER_GETCOUNTER - Gets the current value in the counter and places it | 365 | * %MMTIMER_GETCOUNTER - Gets the current value in the counter and places it |
366 | * in the address specified by @arg. | 366 | * in the address specified by @arg. |
367 | */ | 367 | */ |
368 | static int mmtimer_ioctl(struct inode *inode, struct file *file, | 368 | static long mmtimer_ioctl(struct file *file, unsigned int cmd, |
369 | unsigned int cmd, unsigned long arg) | 369 | unsigned long arg) |
370 | { | 370 | { |
371 | int ret = 0; | 371 | int ret = 0; |
372 | 372 | ||
373 | lock_kernel(); | ||
374 | |||
373 | switch (cmd) { | 375 | switch (cmd) { |
374 | case MMTIMER_GETOFFSET: /* offset of the counter */ | 376 | case MMTIMER_GETOFFSET: /* offset of the counter */ |
375 | /* | 377 | /* |
376 | * SN RTC registers are on their own 64k page | 378 | * SN RTC registers are on their own 64k page |
377 | */ | 379 | */ |
378 | if(PAGE_SIZE <= (1 << 16)) | 380 | if(PAGE_SIZE <= (1 << 16)) |
379 | ret = (((long)RTC_COUNTER_ADDR) & (PAGE_SIZE-1)) / 8; | 381 | ret = (((long)RTC_COUNTER_ADDR) & (PAGE_SIZE-1)) / 8; |
380 | else | 382 | else |
381 | ret = -ENOSYS; | 383 | ret = -ENOSYS; |
382 | break; | 384 | break; |
383 | 385 | ||
384 | case MMTIMER_GETRES: /* resolution of the clock in 10^-15 s */ | 386 | case MMTIMER_GETRES: /* resolution of the clock in 10^-15 s */ |
385 | if(copy_to_user((unsigned long __user *)arg, | 387 | if(copy_to_user((unsigned long __user *)arg, |
386 | &mmtimer_femtoperiod, sizeof(unsigned long))) | 388 | &mmtimer_femtoperiod, sizeof(unsigned long))) |
387 | return -EFAULT; | 389 | ret = -EFAULT; |
388 | break; | 390 | break; |
389 | 391 | ||
390 | case MMTIMER_GETFREQ: /* frequency in Hz */ | 392 | case MMTIMER_GETFREQ: /* frequency in Hz */ |
391 | if(copy_to_user((unsigned long __user *)arg, | 393 | if(copy_to_user((unsigned long __user *)arg, |
392 | &sn_rtc_cycles_per_second, | 394 | &sn_rtc_cycles_per_second, |
393 | sizeof(unsigned long))) | 395 | sizeof(unsigned long))) |
394 | return -EFAULT; | 396 | ret = -EFAULT; |
395 | ret = 0; | ||
396 | break; | 397 | break; |
397 | 398 | ||
398 | case MMTIMER_GETBITS: /* number of bits in the clock */ | 399 | case MMTIMER_GETBITS: /* number of bits in the clock */ |
399 | ret = RTC_BITS; | 400 | ret = RTC_BITS; |
400 | break; | 401 | break; |
401 | 402 | ||
402 | case MMTIMER_MMAPAVAIL: /* can we mmap the clock into userspace? */ | 403 | case MMTIMER_MMAPAVAIL: /* can we mmap the clock into userspace? */ |
403 | ret = (PAGE_SIZE <= (1 << 16)) ? 1 : 0; | 404 | ret = (PAGE_SIZE <= (1 << 16)) ? 1 : 0; |
404 | break; | 405 | break; |
405 | 406 | ||
406 | case MMTIMER_GETCOUNTER: | 407 | case MMTIMER_GETCOUNTER: |
407 | if(copy_to_user((unsigned long __user *)arg, | 408 | if(copy_to_user((unsigned long __user *)arg, |
408 | RTC_COUNTER_ADDR, sizeof(unsigned long))) | 409 | RTC_COUNTER_ADDR, sizeof(unsigned long))) |
409 | return -EFAULT; | 410 | ret = -EFAULT; |
410 | break; | 411 | break; |
411 | default: | 412 | default: |
412 | ret = -ENOSYS; | 413 | ret = -ENOTTY; |
413 | break; | 414 | break; |
414 | } | 415 | } |
415 | 416 | unlock_kernel(); | |
416 | return ret; | 417 | return ret; |
417 | } | 418 | } |
418 | 419 | ||
419 | /** | 420 | /** |
420 | * mmtimer_mmap - maps the clock's registers into userspace | 421 | * mmtimer_mmap - maps the clock's registers into userspace |
421 | * @file: file structure for the device | 422 | * @file: file structure for the device |
422 | * @vma: VMA to map the registers into | 423 | * @vma: VMA to map the registers into |
423 | * | 424 | * |
424 | * Calls remap_pfn_range() to map the clock's registers into | 425 | * Calls remap_pfn_range() to map the clock's registers into |
425 | * the calling process' address space. | 426 | * the calling process' address space. |
426 | */ | 427 | */ |
427 | static int mmtimer_mmap(struct file *file, struct vm_area_struct *vma) | 428 | static int mmtimer_mmap(struct file *file, struct vm_area_struct *vma) |
428 | { | 429 | { |
429 | unsigned long mmtimer_addr; | 430 | unsigned long mmtimer_addr; |
430 | 431 | ||
431 | if (vma->vm_end - vma->vm_start != PAGE_SIZE) | 432 | if (vma->vm_end - vma->vm_start != PAGE_SIZE) |
432 | return -EINVAL; | 433 | return -EINVAL; |
433 | 434 | ||
434 | if (vma->vm_flags & VM_WRITE) | 435 | if (vma->vm_flags & VM_WRITE) |
435 | return -EPERM; | 436 | return -EPERM; |
436 | 437 | ||
437 | if (PAGE_SIZE > (1 << 16)) | 438 | if (PAGE_SIZE > (1 << 16)) |
438 | return -ENOSYS; | 439 | return -ENOSYS; |
439 | 440 | ||
440 | vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); | 441 | vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); |
441 | 442 | ||
442 | mmtimer_addr = __pa(RTC_COUNTER_ADDR); | 443 | mmtimer_addr = __pa(RTC_COUNTER_ADDR); |
443 | mmtimer_addr &= ~(PAGE_SIZE - 1); | 444 | mmtimer_addr &= ~(PAGE_SIZE - 1); |
444 | mmtimer_addr &= 0xfffffffffffffffUL; | 445 | mmtimer_addr &= 0xfffffffffffffffUL; |
445 | 446 | ||
446 | if (remap_pfn_range(vma, vma->vm_start, mmtimer_addr >> PAGE_SHIFT, | 447 | if (remap_pfn_range(vma, vma->vm_start, mmtimer_addr >> PAGE_SHIFT, |
447 | PAGE_SIZE, vma->vm_page_prot)) { | 448 | PAGE_SIZE, vma->vm_page_prot)) { |
448 | printk(KERN_ERR "remap_pfn_range failed in mmtimer.c\n"); | 449 | printk(KERN_ERR "remap_pfn_range failed in mmtimer.c\n"); |
449 | return -EAGAIN; | 450 | return -EAGAIN; |
450 | } | 451 | } |
451 | 452 | ||
452 | return 0; | 453 | return 0; |
453 | } | 454 | } |
454 | 455 | ||
455 | static struct miscdevice mmtimer_miscdev = { | 456 | static struct miscdevice mmtimer_miscdev = { |
456 | SGI_MMTIMER, | 457 | SGI_MMTIMER, |
457 | MMTIMER_NAME, | 458 | MMTIMER_NAME, |
458 | &mmtimer_fops | 459 | &mmtimer_fops |
459 | }; | 460 | }; |
460 | 461 | ||
461 | static struct timespec sgi_clock_offset; | 462 | static struct timespec sgi_clock_offset; |
462 | static int sgi_clock_period; | 463 | static int sgi_clock_period; |
463 | 464 | ||
464 | /* | 465 | /* |
465 | * Posix Timer Interface | 466 | * Posix Timer Interface |
466 | */ | 467 | */ |
467 | 468 | ||
468 | static struct timespec sgi_clock_offset; | 469 | static struct timespec sgi_clock_offset; |
469 | static int sgi_clock_period; | 470 | static int sgi_clock_period; |
470 | 471 | ||
471 | static int sgi_clock_get(clockid_t clockid, struct timespec *tp) | 472 | static int sgi_clock_get(clockid_t clockid, struct timespec *tp) |
472 | { | 473 | { |
473 | u64 nsec; | 474 | u64 nsec; |
474 | 475 | ||
475 | nsec = rtc_time() * sgi_clock_period | 476 | nsec = rtc_time() * sgi_clock_period |
476 | + sgi_clock_offset.tv_nsec; | 477 | + sgi_clock_offset.tv_nsec; |
477 | *tp = ns_to_timespec(nsec); | 478 | *tp = ns_to_timespec(nsec); |
478 | tp->tv_sec += sgi_clock_offset.tv_sec; | 479 | tp->tv_sec += sgi_clock_offset.tv_sec; |
479 | return 0; | 480 | return 0; |
480 | }; | 481 | }; |
481 | 482 | ||
482 | static int sgi_clock_set(clockid_t clockid, struct timespec *tp) | 483 | static int sgi_clock_set(clockid_t clockid, struct timespec *tp) |
483 | { | 484 | { |
484 | 485 | ||
485 | u64 nsec; | 486 | u64 nsec; |
486 | u32 rem; | 487 | u32 rem; |
487 | 488 | ||
488 | nsec = rtc_time() * sgi_clock_period; | 489 | nsec = rtc_time() * sgi_clock_period; |
489 | 490 | ||
490 | sgi_clock_offset.tv_sec = tp->tv_sec - div_u64_rem(nsec, NSEC_PER_SEC, &rem); | 491 | sgi_clock_offset.tv_sec = tp->tv_sec - div_u64_rem(nsec, NSEC_PER_SEC, &rem); |
491 | 492 | ||
492 | if (rem <= tp->tv_nsec) | 493 | if (rem <= tp->tv_nsec) |
493 | sgi_clock_offset.tv_nsec = tp->tv_sec - rem; | 494 | sgi_clock_offset.tv_nsec = tp->tv_sec - rem; |
494 | else { | 495 | else { |
495 | sgi_clock_offset.tv_nsec = tp->tv_sec + NSEC_PER_SEC - rem; | 496 | sgi_clock_offset.tv_nsec = tp->tv_sec + NSEC_PER_SEC - rem; |
496 | sgi_clock_offset.tv_sec--; | 497 | sgi_clock_offset.tv_sec--; |
497 | } | 498 | } |
498 | return 0; | 499 | return 0; |
499 | } | 500 | } |
500 | 501 | ||
501 | /** | 502 | /** |
502 | * mmtimer_interrupt - timer interrupt handler | 503 | * mmtimer_interrupt - timer interrupt handler |
503 | * @irq: irq received | 504 | * @irq: irq received |
504 | * @dev_id: device the irq came from | 505 | * @dev_id: device the irq came from |
505 | * | 506 | * |
506 | * Called when one of the comarators matches the counter, This | 507 | * Called when one of the comarators matches the counter, This |
507 | * routine will send signals to processes that have requested | 508 | * routine will send signals to processes that have requested |
508 | * them. | 509 | * them. |
509 | * | 510 | * |
510 | * This interrupt is run in an interrupt context | 511 | * This interrupt is run in an interrupt context |
511 | * by the SHUB. It is therefore safe to locally access SHub | 512 | * by the SHUB. It is therefore safe to locally access SHub |
512 | * registers. | 513 | * registers. |
513 | */ | 514 | */ |
514 | static irqreturn_t | 515 | static irqreturn_t |
515 | mmtimer_interrupt(int irq, void *dev_id) | 516 | mmtimer_interrupt(int irq, void *dev_id) |
516 | { | 517 | { |
517 | unsigned long expires = 0; | 518 | unsigned long expires = 0; |
518 | int result = IRQ_NONE; | 519 | int result = IRQ_NONE; |
519 | unsigned indx = cpu_to_node(smp_processor_id()); | 520 | unsigned indx = cpu_to_node(smp_processor_id()); |
520 | struct mmtimer *base; | 521 | struct mmtimer *base; |
521 | 522 | ||
522 | spin_lock(&timers[indx].lock); | 523 | spin_lock(&timers[indx].lock); |
523 | base = rb_entry(timers[indx].next, struct mmtimer, list); | 524 | base = rb_entry(timers[indx].next, struct mmtimer, list); |
524 | if (base == NULL) { | 525 | if (base == NULL) { |
525 | spin_unlock(&timers[indx].lock); | 526 | spin_unlock(&timers[indx].lock); |
526 | return result; | 527 | return result; |
527 | } | 528 | } |
528 | 529 | ||
529 | if (base->cpu == smp_processor_id()) { | 530 | if (base->cpu == smp_processor_id()) { |
530 | if (base->timer) | 531 | if (base->timer) |
531 | expires = base->timer->it.mmtimer.expires; | 532 | expires = base->timer->it.mmtimer.expires; |
532 | /* expires test won't work with shared irqs */ | 533 | /* expires test won't work with shared irqs */ |
533 | if ((mmtimer_int_pending(COMPARATOR) > 0) || | 534 | if ((mmtimer_int_pending(COMPARATOR) > 0) || |
534 | (expires && (expires <= rtc_time()))) { | 535 | (expires && (expires <= rtc_time()))) { |
535 | mmtimer_clr_int_pending(COMPARATOR); | 536 | mmtimer_clr_int_pending(COMPARATOR); |
536 | tasklet_schedule(&timers[indx].tasklet); | 537 | tasklet_schedule(&timers[indx].tasklet); |
537 | result = IRQ_HANDLED; | 538 | result = IRQ_HANDLED; |
538 | } | 539 | } |
539 | } | 540 | } |
540 | spin_unlock(&timers[indx].lock); | 541 | spin_unlock(&timers[indx].lock); |
541 | return result; | 542 | return result; |
542 | } | 543 | } |
543 | 544 | ||
544 | static void mmtimer_tasklet(unsigned long data) | 545 | static void mmtimer_tasklet(unsigned long data) |
545 | { | 546 | { |
546 | int nodeid = data; | 547 | int nodeid = data; |
547 | struct mmtimer_node *mn = &timers[nodeid]; | 548 | struct mmtimer_node *mn = &timers[nodeid]; |
548 | struct mmtimer *x = rb_entry(mn->next, struct mmtimer, list); | 549 | struct mmtimer *x = rb_entry(mn->next, struct mmtimer, list); |
549 | struct k_itimer *t; | 550 | struct k_itimer *t; |
550 | unsigned long flags; | 551 | unsigned long flags; |
551 | 552 | ||
552 | /* Send signal and deal with periodic signals */ | 553 | /* Send signal and deal with periodic signals */ |
553 | spin_lock_irqsave(&mn->lock, flags); | 554 | spin_lock_irqsave(&mn->lock, flags); |
554 | if (!mn->next) | 555 | if (!mn->next) |
555 | goto out; | 556 | goto out; |
556 | 557 | ||
557 | x = rb_entry(mn->next, struct mmtimer, list); | 558 | x = rb_entry(mn->next, struct mmtimer, list); |
558 | t = x->timer; | 559 | t = x->timer; |
559 | 560 | ||
560 | if (t->it.mmtimer.clock == TIMER_OFF) | 561 | if (t->it.mmtimer.clock == TIMER_OFF) |
561 | goto out; | 562 | goto out; |
562 | 563 | ||
563 | t->it_overrun = 0; | 564 | t->it_overrun = 0; |
564 | 565 | ||
565 | mn->next = rb_next(&x->list); | 566 | mn->next = rb_next(&x->list); |
566 | rb_erase(&x->list, &mn->timer_head); | 567 | rb_erase(&x->list, &mn->timer_head); |
567 | 568 | ||
568 | if (posix_timer_event(t, 0) != 0) | 569 | if (posix_timer_event(t, 0) != 0) |
569 | t->it_overrun++; | 570 | t->it_overrun++; |
570 | 571 | ||
571 | if(t->it.mmtimer.incr) { | 572 | if(t->it.mmtimer.incr) { |
572 | t->it.mmtimer.expires += t->it.mmtimer.incr; | 573 | t->it.mmtimer.expires += t->it.mmtimer.incr; |
573 | mmtimer_add_list(x); | 574 | mmtimer_add_list(x); |
574 | } else { | 575 | } else { |
575 | /* Ensure we don't false trigger in mmtimer_interrupt */ | 576 | /* Ensure we don't false trigger in mmtimer_interrupt */ |
576 | t->it.mmtimer.clock = TIMER_OFF; | 577 | t->it.mmtimer.clock = TIMER_OFF; |
577 | t->it.mmtimer.expires = 0; | 578 | t->it.mmtimer.expires = 0; |
578 | kfree(x); | 579 | kfree(x); |
579 | } | 580 | } |
580 | /* Set comparator for next timer, if there is one */ | 581 | /* Set comparator for next timer, if there is one */ |
581 | mmtimer_set_next_timer(nodeid); | 582 | mmtimer_set_next_timer(nodeid); |
582 | 583 | ||
583 | t->it_overrun_last = t->it_overrun; | 584 | t->it_overrun_last = t->it_overrun; |
584 | out: | 585 | out: |
585 | spin_unlock_irqrestore(&mn->lock, flags); | 586 | spin_unlock_irqrestore(&mn->lock, flags); |
586 | } | 587 | } |
587 | 588 | ||
588 | static int sgi_timer_create(struct k_itimer *timer) | 589 | static int sgi_timer_create(struct k_itimer *timer) |
589 | { | 590 | { |
590 | /* Insure that a newly created timer is off */ | 591 | /* Insure that a newly created timer is off */ |
591 | timer->it.mmtimer.clock = TIMER_OFF; | 592 | timer->it.mmtimer.clock = TIMER_OFF; |
592 | return 0; | 593 | return 0; |
593 | } | 594 | } |
594 | 595 | ||
595 | /* This does not really delete a timer. It just insures | 596 | /* This does not really delete a timer. It just insures |
596 | * that the timer is not active | 597 | * that the timer is not active |
597 | * | 598 | * |
598 | * Assumption: it_lock is already held with irq's disabled | 599 | * Assumption: it_lock is already held with irq's disabled |
599 | */ | 600 | */ |
600 | static int sgi_timer_del(struct k_itimer *timr) | 601 | static int sgi_timer_del(struct k_itimer *timr) |
601 | { | 602 | { |
602 | cnodeid_t nodeid = timr->it.mmtimer.node; | 603 | cnodeid_t nodeid = timr->it.mmtimer.node; |
603 | unsigned long irqflags; | 604 | unsigned long irqflags; |
604 | 605 | ||
605 | spin_lock_irqsave(&timers[nodeid].lock, irqflags); | 606 | spin_lock_irqsave(&timers[nodeid].lock, irqflags); |
606 | if (timr->it.mmtimer.clock != TIMER_OFF) { | 607 | if (timr->it.mmtimer.clock != TIMER_OFF) { |
607 | unsigned long expires = timr->it.mmtimer.expires; | 608 | unsigned long expires = timr->it.mmtimer.expires; |
608 | struct rb_node *n = timers[nodeid].timer_head.rb_node; | 609 | struct rb_node *n = timers[nodeid].timer_head.rb_node; |
609 | struct mmtimer *uninitialized_var(t); | 610 | struct mmtimer *uninitialized_var(t); |
610 | int r = 0; | 611 | int r = 0; |
611 | 612 | ||
612 | timr->it.mmtimer.clock = TIMER_OFF; | 613 | timr->it.mmtimer.clock = TIMER_OFF; |
613 | timr->it.mmtimer.expires = 0; | 614 | timr->it.mmtimer.expires = 0; |
614 | 615 | ||
615 | while (n) { | 616 | while (n) { |
616 | t = rb_entry(n, struct mmtimer, list); | 617 | t = rb_entry(n, struct mmtimer, list); |
617 | if (t->timer == timr) | 618 | if (t->timer == timr) |
618 | break; | 619 | break; |
619 | 620 | ||
620 | if (expires < t->timer->it.mmtimer.expires) | 621 | if (expires < t->timer->it.mmtimer.expires) |
621 | n = n->rb_left; | 622 | n = n->rb_left; |
622 | else | 623 | else |
623 | n = n->rb_right; | 624 | n = n->rb_right; |
624 | } | 625 | } |
625 | 626 | ||
626 | if (!n) { | 627 | if (!n) { |
627 | spin_unlock_irqrestore(&timers[nodeid].lock, irqflags); | 628 | spin_unlock_irqrestore(&timers[nodeid].lock, irqflags); |
628 | return 0; | 629 | return 0; |
629 | } | 630 | } |
630 | 631 | ||
631 | if (timers[nodeid].next == n) { | 632 | if (timers[nodeid].next == n) { |
632 | timers[nodeid].next = rb_next(n); | 633 | timers[nodeid].next = rb_next(n); |
633 | r = 1; | 634 | r = 1; |
634 | } | 635 | } |
635 | 636 | ||
636 | rb_erase(n, &timers[nodeid].timer_head); | 637 | rb_erase(n, &timers[nodeid].timer_head); |
637 | kfree(t); | 638 | kfree(t); |
638 | 639 | ||
639 | if (r) { | 640 | if (r) { |
640 | mmtimer_disable_int(cnodeid_to_nasid(nodeid), | 641 | mmtimer_disable_int(cnodeid_to_nasid(nodeid), |
641 | COMPARATOR); | 642 | COMPARATOR); |
642 | mmtimer_set_next_timer(nodeid); | 643 | mmtimer_set_next_timer(nodeid); |
643 | } | 644 | } |
644 | } | 645 | } |
645 | spin_unlock_irqrestore(&timers[nodeid].lock, irqflags); | 646 | spin_unlock_irqrestore(&timers[nodeid].lock, irqflags); |
646 | return 0; | 647 | return 0; |
647 | } | 648 | } |
648 | 649 | ||
649 | /* Assumption: it_lock is already held with irq's disabled */ | 650 | /* Assumption: it_lock is already held with irq's disabled */ |
650 | static void sgi_timer_get(struct k_itimer *timr, struct itimerspec *cur_setting) | 651 | static void sgi_timer_get(struct k_itimer *timr, struct itimerspec *cur_setting) |
651 | { | 652 | { |
652 | 653 | ||
653 | if (timr->it.mmtimer.clock == TIMER_OFF) { | 654 | if (timr->it.mmtimer.clock == TIMER_OFF) { |
654 | cur_setting->it_interval.tv_nsec = 0; | 655 | cur_setting->it_interval.tv_nsec = 0; |
655 | cur_setting->it_interval.tv_sec = 0; | 656 | cur_setting->it_interval.tv_sec = 0; |
656 | cur_setting->it_value.tv_nsec = 0; | 657 | cur_setting->it_value.tv_nsec = 0; |
657 | cur_setting->it_value.tv_sec =0; | 658 | cur_setting->it_value.tv_sec =0; |
658 | return; | 659 | return; |
659 | } | 660 | } |
660 | 661 | ||
661 | cur_setting->it_interval = ns_to_timespec(timr->it.mmtimer.incr * sgi_clock_period); | 662 | cur_setting->it_interval = ns_to_timespec(timr->it.mmtimer.incr * sgi_clock_period); |
662 | cur_setting->it_value = ns_to_timespec((timr->it.mmtimer.expires - rtc_time()) * sgi_clock_period); | 663 | cur_setting->it_value = ns_to_timespec((timr->it.mmtimer.expires - rtc_time()) * sgi_clock_period); |
663 | } | 664 | } |
664 | 665 | ||
665 | 666 | ||
666 | static int sgi_timer_set(struct k_itimer *timr, int flags, | 667 | static int sgi_timer_set(struct k_itimer *timr, int flags, |
667 | struct itimerspec * new_setting, | 668 | struct itimerspec * new_setting, |
668 | struct itimerspec * old_setting) | 669 | struct itimerspec * old_setting) |
669 | { | 670 | { |
670 | unsigned long when, period, irqflags; | 671 | unsigned long when, period, irqflags; |
671 | int err = 0; | 672 | int err = 0; |
672 | cnodeid_t nodeid; | 673 | cnodeid_t nodeid; |
673 | struct mmtimer *base; | 674 | struct mmtimer *base; |
674 | struct rb_node *n; | 675 | struct rb_node *n; |
675 | 676 | ||
676 | if (old_setting) | 677 | if (old_setting) |
677 | sgi_timer_get(timr, old_setting); | 678 | sgi_timer_get(timr, old_setting); |
678 | 679 | ||
679 | sgi_timer_del(timr); | 680 | sgi_timer_del(timr); |
680 | when = timespec_to_ns(&new_setting->it_value); | 681 | when = timespec_to_ns(&new_setting->it_value); |
681 | period = timespec_to_ns(&new_setting->it_interval); | 682 | period = timespec_to_ns(&new_setting->it_interval); |
682 | 683 | ||
683 | if (when == 0) | 684 | if (when == 0) |
684 | /* Clear timer */ | 685 | /* Clear timer */ |
685 | return 0; | 686 | return 0; |
686 | 687 | ||
687 | base = kmalloc(sizeof(struct mmtimer), GFP_KERNEL); | 688 | base = kmalloc(sizeof(struct mmtimer), GFP_KERNEL); |
688 | if (base == NULL) | 689 | if (base == NULL) |
689 | return -ENOMEM; | 690 | return -ENOMEM; |
690 | 691 | ||
691 | if (flags & TIMER_ABSTIME) { | 692 | if (flags & TIMER_ABSTIME) { |
692 | struct timespec n; | 693 | struct timespec n; |
693 | unsigned long now; | 694 | unsigned long now; |
694 | 695 | ||
695 | getnstimeofday(&n); | 696 | getnstimeofday(&n); |
696 | now = timespec_to_ns(&n); | 697 | now = timespec_to_ns(&n); |
697 | if (when > now) | 698 | if (when > now) |
698 | when -= now; | 699 | when -= now; |
699 | else | 700 | else |
700 | /* Fire the timer immediately */ | 701 | /* Fire the timer immediately */ |
701 | when = 0; | 702 | when = 0; |
702 | } | 703 | } |
703 | 704 | ||
704 | /* | 705 | /* |
705 | * Convert to sgi clock period. Need to keep rtc_time() as near as possible | 706 | * Convert to sgi clock period. Need to keep rtc_time() as near as possible |
706 | * to getnstimeofday() in order to be as faithful as possible to the time | 707 | * to getnstimeofday() in order to be as faithful as possible to the time |
707 | * specified. | 708 | * specified. |
708 | */ | 709 | */ |
709 | when = (when + sgi_clock_period - 1) / sgi_clock_period + rtc_time(); | 710 | when = (when + sgi_clock_period - 1) / sgi_clock_period + rtc_time(); |
710 | period = (period + sgi_clock_period - 1) / sgi_clock_period; | 711 | period = (period + sgi_clock_period - 1) / sgi_clock_period; |
711 | 712 | ||
712 | /* | 713 | /* |
713 | * We are allocating a local SHub comparator. If we would be moved to another | 714 | * We are allocating a local SHub comparator. If we would be moved to another |
714 | * cpu then another SHub may be local to us. Prohibit that by switching off | 715 | * cpu then another SHub may be local to us. Prohibit that by switching off |
715 | * preemption. | 716 | * preemption. |
716 | */ | 717 | */ |
717 | preempt_disable(); | 718 | preempt_disable(); |
718 | 719 | ||
719 | nodeid = cpu_to_node(smp_processor_id()); | 720 | nodeid = cpu_to_node(smp_processor_id()); |
720 | 721 | ||
721 | /* Lock the node timer structure */ | 722 | /* Lock the node timer structure */ |
722 | spin_lock_irqsave(&timers[nodeid].lock, irqflags); | 723 | spin_lock_irqsave(&timers[nodeid].lock, irqflags); |
723 | 724 | ||
724 | base->timer = timr; | 725 | base->timer = timr; |
725 | base->cpu = smp_processor_id(); | 726 | base->cpu = smp_processor_id(); |
726 | 727 | ||
727 | timr->it.mmtimer.clock = TIMER_SET; | 728 | timr->it.mmtimer.clock = TIMER_SET; |
728 | timr->it.mmtimer.node = nodeid; | 729 | timr->it.mmtimer.node = nodeid; |
729 | timr->it.mmtimer.incr = period; | 730 | timr->it.mmtimer.incr = period; |
730 | timr->it.mmtimer.expires = when; | 731 | timr->it.mmtimer.expires = when; |
731 | 732 | ||
732 | n = timers[nodeid].next; | 733 | n = timers[nodeid].next; |
733 | 734 | ||
734 | /* Add the new struct mmtimer to node's timer list */ | 735 | /* Add the new struct mmtimer to node's timer list */ |
735 | mmtimer_add_list(base); | 736 | mmtimer_add_list(base); |
736 | 737 | ||
737 | if (timers[nodeid].next == n) { | 738 | if (timers[nodeid].next == n) { |
738 | /* No need to reprogram comparator for now */ | 739 | /* No need to reprogram comparator for now */ |
739 | spin_unlock_irqrestore(&timers[nodeid].lock, irqflags); | 740 | spin_unlock_irqrestore(&timers[nodeid].lock, irqflags); |
740 | preempt_enable(); | 741 | preempt_enable(); |
741 | return err; | 742 | return err; |
742 | } | 743 | } |
743 | 744 | ||
744 | /* We need to reprogram the comparator */ | 745 | /* We need to reprogram the comparator */ |
745 | if (n) | 746 | if (n) |
746 | mmtimer_disable_int(cnodeid_to_nasid(nodeid), COMPARATOR); | 747 | mmtimer_disable_int(cnodeid_to_nasid(nodeid), COMPARATOR); |
747 | 748 | ||
748 | mmtimer_set_next_timer(nodeid); | 749 | mmtimer_set_next_timer(nodeid); |
749 | 750 | ||
750 | /* Unlock the node timer structure */ | 751 | /* Unlock the node timer structure */ |
751 | spin_unlock_irqrestore(&timers[nodeid].lock, irqflags); | 752 | spin_unlock_irqrestore(&timers[nodeid].lock, irqflags); |
752 | 753 | ||
753 | preempt_enable(); | 754 | preempt_enable(); |
754 | 755 | ||
755 | return err; | 756 | return err; |
756 | } | 757 | } |
757 | 758 | ||
758 | static struct k_clock sgi_clock = { | 759 | static struct k_clock sgi_clock = { |
759 | .res = 0, | 760 | .res = 0, |
760 | .clock_set = sgi_clock_set, | 761 | .clock_set = sgi_clock_set, |
761 | .clock_get = sgi_clock_get, | 762 | .clock_get = sgi_clock_get, |
762 | .timer_create = sgi_timer_create, | 763 | .timer_create = sgi_timer_create, |
763 | .nsleep = do_posix_clock_nonanosleep, | 764 | .nsleep = do_posix_clock_nonanosleep, |
764 | .timer_set = sgi_timer_set, | 765 | .timer_set = sgi_timer_set, |
765 | .timer_del = sgi_timer_del, | 766 | .timer_del = sgi_timer_del, |
766 | .timer_get = sgi_timer_get | 767 | .timer_get = sgi_timer_get |
767 | }; | 768 | }; |
768 | 769 | ||
769 | /** | 770 | /** |
770 | * mmtimer_init - device initialization routine | 771 | * mmtimer_init - device initialization routine |
771 | * | 772 | * |
772 | * Does initial setup for the mmtimer device. | 773 | * Does initial setup for the mmtimer device. |
773 | */ | 774 | */ |
774 | static int __init mmtimer_init(void) | 775 | static int __init mmtimer_init(void) |
775 | { | 776 | { |
776 | cnodeid_t node, maxn = -1; | 777 | cnodeid_t node, maxn = -1; |
777 | 778 | ||
778 | if (!ia64_platform_is("sn2")) | 779 | if (!ia64_platform_is("sn2")) |
779 | return 0; | 780 | return 0; |
780 | 781 | ||
781 | /* | 782 | /* |
782 | * Sanity check the cycles/sec variable | 783 | * Sanity check the cycles/sec variable |
783 | */ | 784 | */ |
784 | if (sn_rtc_cycles_per_second < 100000) { | 785 | if (sn_rtc_cycles_per_second < 100000) { |
785 | printk(KERN_ERR "%s: unable to determine clock frequency\n", | 786 | printk(KERN_ERR "%s: unable to determine clock frequency\n", |
786 | MMTIMER_NAME); | 787 | MMTIMER_NAME); |
787 | goto out1; | 788 | goto out1; |
788 | } | 789 | } |
789 | 790 | ||
790 | mmtimer_femtoperiod = ((unsigned long)1E15 + sn_rtc_cycles_per_second / | 791 | mmtimer_femtoperiod = ((unsigned long)1E15 + sn_rtc_cycles_per_second / |
791 | 2) / sn_rtc_cycles_per_second; | 792 | 2) / sn_rtc_cycles_per_second; |
792 | 793 | ||
793 | if (request_irq(SGI_MMTIMER_VECTOR, mmtimer_interrupt, IRQF_PERCPU, MMTIMER_NAME, NULL)) { | 794 | if (request_irq(SGI_MMTIMER_VECTOR, mmtimer_interrupt, IRQF_PERCPU, MMTIMER_NAME, NULL)) { |
794 | printk(KERN_WARNING "%s: unable to allocate interrupt.", | 795 | printk(KERN_WARNING "%s: unable to allocate interrupt.", |
795 | MMTIMER_NAME); | 796 | MMTIMER_NAME); |
796 | goto out1; | 797 | goto out1; |
797 | } | 798 | } |
798 | 799 | ||
799 | if (misc_register(&mmtimer_miscdev)) { | 800 | if (misc_register(&mmtimer_miscdev)) { |
800 | printk(KERN_ERR "%s: failed to register device\n", | 801 | printk(KERN_ERR "%s: failed to register device\n", |
801 | MMTIMER_NAME); | 802 | MMTIMER_NAME); |
802 | goto out2; | 803 | goto out2; |
803 | } | 804 | } |
804 | 805 | ||
805 | /* Get max numbered node, calculate slots needed */ | 806 | /* Get max numbered node, calculate slots needed */ |
806 | for_each_online_node(node) { | 807 | for_each_online_node(node) { |
807 | maxn = node; | 808 | maxn = node; |
808 | } | 809 | } |
809 | maxn++; | 810 | maxn++; |
810 | 811 | ||
811 | /* Allocate list of node ptrs to mmtimer_t's */ | 812 | /* Allocate list of node ptrs to mmtimer_t's */ |
812 | timers = kzalloc(sizeof(struct mmtimer_node)*maxn, GFP_KERNEL); | 813 | timers = kzalloc(sizeof(struct mmtimer_node)*maxn, GFP_KERNEL); |
813 | if (timers == NULL) { | 814 | if (timers == NULL) { |
814 | printk(KERN_ERR "%s: failed to allocate memory for device\n", | 815 | printk(KERN_ERR "%s: failed to allocate memory for device\n", |
815 | MMTIMER_NAME); | 816 | MMTIMER_NAME); |
816 | goto out3; | 817 | goto out3; |
817 | } | 818 | } |
818 | 819 | ||
819 | /* Initialize struct mmtimer's for each online node */ | 820 | /* Initialize struct mmtimer's for each online node */ |
820 | for_each_online_node(node) { | 821 | for_each_online_node(node) { |
821 | spin_lock_init(&timers[node].lock); | 822 | spin_lock_init(&timers[node].lock); |
822 | tasklet_init(&timers[node].tasklet, mmtimer_tasklet, | 823 | tasklet_init(&timers[node].tasklet, mmtimer_tasklet, |
823 | (unsigned long) node); | 824 | (unsigned long) node); |
824 | } | 825 | } |
825 | 826 | ||
826 | sgi_clock_period = sgi_clock.res = NSEC_PER_SEC / sn_rtc_cycles_per_second; | 827 | sgi_clock_period = sgi_clock.res = NSEC_PER_SEC / sn_rtc_cycles_per_second; |
827 | register_posix_clock(CLOCK_SGI_CYCLE, &sgi_clock); | 828 | register_posix_clock(CLOCK_SGI_CYCLE, &sgi_clock); |
828 | 829 | ||
829 | printk(KERN_INFO "%s: v%s, %ld MHz\n", MMTIMER_DESC, MMTIMER_VERSION, | 830 | printk(KERN_INFO "%s: v%s, %ld MHz\n", MMTIMER_DESC, MMTIMER_VERSION, |
830 | sn_rtc_cycles_per_second/(unsigned long)1E6); | 831 | sn_rtc_cycles_per_second/(unsigned long)1E6); |
831 | 832 | ||
832 | return 0; | 833 | return 0; |
833 | 834 | ||
834 | out3: | 835 | out3: |
835 | kfree(timers); | 836 | kfree(timers); |
836 | misc_deregister(&mmtimer_miscdev); | 837 | misc_deregister(&mmtimer_miscdev); |
837 | out2: | 838 | out2: |
838 | free_irq(SGI_MMTIMER_VECTOR, NULL); | 839 | free_irq(SGI_MMTIMER_VECTOR, NULL); |
839 | out1: | 840 | out1: |
840 | return -1; | 841 | return -1; |
841 | } | 842 | } |
842 | 843 |