]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Intel & MS High Precision Event Timer Implementation. | |
3 | * | |
4 | * Copyright (C) 2003 Intel Corporation | |
5 | * Venki Pallipadi | |
6 | * (c) Copyright 2004 Hewlett-Packard Development Company, L.P. | |
7 | * Bob Picco <robert.picco@hp.com> | |
8 | * | |
9 | * This program is free software; you can redistribute it and/or modify | |
10 | * it under the terms of the GNU General Public License version 2 as | |
11 | * published by the Free Software Foundation. | |
12 | */ | |
13 | ||
14 | #include <linux/config.h> | |
15 | #include <linux/interrupt.h> | |
16 | #include <linux/module.h> | |
17 | #include <linux/kernel.h> | |
18 | #include <linux/types.h> | |
19 | #include <linux/miscdevice.h> | |
20 | #include <linux/major.h> | |
21 | #include <linux/ioport.h> | |
22 | #include <linux/fcntl.h> | |
23 | #include <linux/init.h> | |
24 | #include <linux/poll.h> | |
25 | #include <linux/proc_fs.h> | |
26 | #include <linux/spinlock.h> | |
27 | #include <linux/sysctl.h> | |
28 | #include <linux/wait.h> | |
29 | #include <linux/bcd.h> | |
30 | #include <linux/seq_file.h> | |
31 | #include <linux/bitops.h> | |
32 | ||
33 | #include <asm/current.h> | |
34 | #include <asm/uaccess.h> | |
35 | #include <asm/system.h> | |
36 | #include <asm/io.h> | |
37 | #include <asm/irq.h> | |
38 | #include <asm/div64.h> | |
39 | ||
40 | #include <linux/acpi.h> | |
41 | #include <acpi/acpi_bus.h> | |
42 | #include <linux/hpet.h> | |
43 | ||
44 | /* | |
45 | * The High Precision Event Timer driver. | |
46 | * This driver is closely modelled after the rtc.c driver. | |
96803820 | 47 | * http://www.intel.com/hardwaredesign/hpetspec.htm |
1da177e4 LT |
48 | */ |
49 | #define HPET_USER_FREQ (64) | |
50 | #define HPET_DRIFT (500) | |
51 | ||
642d30bb | 52 | static u32 hpet_nhpet, hpet_max_freq = HPET_USER_FREQ; |
1da177e4 LT |
53 | |
54 | /* A lock for concurrent access by app and isr hpet activity. */ | |
55 | static DEFINE_SPINLOCK(hpet_lock); | |
56 | /* A lock for concurrent intermodule access to hpet and isr hpet activity. */ | |
57 | static DEFINE_SPINLOCK(hpet_task_lock); | |
58 | ||
59 | #define HPET_DEV_NAME (7) | |
60 | ||
61 | struct hpet_dev { | |
62 | struct hpets *hd_hpets; | |
63 | struct hpet __iomem *hd_hpet; | |
64 | struct hpet_timer __iomem *hd_timer; | |
65 | unsigned long hd_ireqfreq; | |
66 | unsigned long hd_irqdata; | |
67 | wait_queue_head_t hd_waitqueue; | |
68 | struct fasync_struct *hd_async_queue; | |
69 | struct hpet_task *hd_task; | |
70 | unsigned int hd_flags; | |
71 | unsigned int hd_irq; | |
72 | unsigned int hd_hdwirq; | |
73 | char hd_name[HPET_DEV_NAME]; | |
74 | }; | |
75 | ||
76 | struct hpets { | |
77 | struct hpets *hp_next; | |
78 | struct hpet __iomem *hp_hpet; | |
79 | unsigned long hp_hpet_phys; | |
80 | struct time_interpolator *hp_interpolator; | |
ba3f213f | 81 | unsigned long long hp_tick_freq; |
1da177e4 LT |
82 | unsigned long hp_delta; |
83 | unsigned int hp_ntimer; | |
84 | unsigned int hp_which; | |
85 | struct hpet_dev hp_dev[1]; | |
86 | }; | |
87 | ||
88 | static struct hpets *hpets; | |
89 | ||
90 | #define HPET_OPEN 0x0001 | |
91 | #define HPET_IE 0x0002 /* interrupt enabled */ | |
92 | #define HPET_PERIODIC 0x0004 | |
0d290861 | 93 | #define HPET_SHARED_IRQ 0x0008 |
1da177e4 LT |
94 | |
95 | #if BITS_PER_LONG == 64 | |
96 | #define write_counter(V, MC) writeq(V, MC) | |
97 | #define read_counter(MC) readq(MC) | |
98 | #else | |
99 | #define write_counter(V, MC) writel(V, MC) | |
100 | #define read_counter(MC) readl(MC) | |
101 | #endif | |
102 | ||
103 | #ifndef readq | |
887c27f3 | 104 | static inline unsigned long long readq(void __iomem *addr) |
1da177e4 LT |
105 | { |
106 | return readl(addr) | (((unsigned long long)readl(addr + 4)) << 32LL); | |
107 | } | |
108 | #endif | |
109 | ||
110 | #ifndef writeq | |
887c27f3 | 111 | static inline void writeq(unsigned long long v, void __iomem *addr) |
1da177e4 LT |
112 | { |
113 | writel(v & 0xffffffff, addr); | |
114 | writel(v >> 32, addr + 4); | |
115 | } | |
116 | #endif | |
117 | ||
118 | static irqreturn_t hpet_interrupt(int irq, void *data, struct pt_regs *regs) | |
119 | { | |
120 | struct hpet_dev *devp; | |
121 | unsigned long isr; | |
122 | ||
123 | devp = data; | |
0d290861 CL |
124 | isr = 1 << (devp - devp->hd_hpets->hp_dev); |
125 | ||
126 | if ((devp->hd_flags & HPET_SHARED_IRQ) && | |
127 | !(isr & readl(&devp->hd_hpet->hpet_isr))) | |
128 | return IRQ_NONE; | |
1da177e4 LT |
129 | |
130 | spin_lock(&hpet_lock); | |
131 | devp->hd_irqdata++; | |
132 | ||
133 | /* | |
134 | * For non-periodic timers, increment the accumulator. | |
135 | * This has the effect of treating non-periodic like periodic. | |
136 | */ | |
137 | if ((devp->hd_flags & (HPET_IE | HPET_PERIODIC)) == HPET_IE) { | |
138 | unsigned long m, t; | |
139 | ||
140 | t = devp->hd_ireqfreq; | |
141 | m = read_counter(&devp->hd_hpet->hpet_mc); | |
142 | write_counter(t + m + devp->hd_hpets->hp_delta, | |
143 | &devp->hd_timer->hpet_compare); | |
144 | } | |
145 | ||
0d290861 CL |
146 | if (devp->hd_flags & HPET_SHARED_IRQ) |
147 | writel(isr, &devp->hd_hpet->hpet_isr); | |
1da177e4 LT |
148 | spin_unlock(&hpet_lock); |
149 | ||
150 | spin_lock(&hpet_task_lock); | |
151 | if (devp->hd_task) | |
152 | devp->hd_task->ht_func(devp->hd_task->ht_data); | |
153 | spin_unlock(&hpet_task_lock); | |
154 | ||
155 | wake_up_interruptible(&devp->hd_waitqueue); | |
156 | ||
157 | kill_fasync(&devp->hd_async_queue, SIGIO, POLL_IN); | |
158 | ||
159 | return IRQ_HANDLED; | |
160 | } | |
161 | ||
162 | static int hpet_open(struct inode *inode, struct file *file) | |
163 | { | |
164 | struct hpet_dev *devp; | |
165 | struct hpets *hpetp; | |
166 | int i; | |
167 | ||
168 | if (file->f_mode & FMODE_WRITE) | |
169 | return -EINVAL; | |
170 | ||
171 | spin_lock_irq(&hpet_lock); | |
172 | ||
173 | for (devp = NULL, hpetp = hpets; hpetp && !devp; hpetp = hpetp->hp_next) | |
174 | for (i = 0; i < hpetp->hp_ntimer; i++) | |
175 | if (hpetp->hp_dev[i].hd_flags & HPET_OPEN | |
176 | || hpetp->hp_dev[i].hd_task) | |
177 | continue; | |
178 | else { | |
179 | devp = &hpetp->hp_dev[i]; | |
180 | break; | |
181 | } | |
182 | ||
183 | if (!devp) { | |
184 | spin_unlock_irq(&hpet_lock); | |
185 | return -EBUSY; | |
186 | } | |
187 | ||
188 | file->private_data = devp; | |
189 | devp->hd_irqdata = 0; | |
190 | devp->hd_flags |= HPET_OPEN; | |
191 | spin_unlock_irq(&hpet_lock); | |
192 | ||
193 | return 0; | |
194 | } | |
195 | ||
196 | static ssize_t | |
197 | hpet_read(struct file *file, char __user *buf, size_t count, loff_t * ppos) | |
198 | { | |
199 | DECLARE_WAITQUEUE(wait, current); | |
200 | unsigned long data; | |
201 | ssize_t retval; | |
202 | struct hpet_dev *devp; | |
203 | ||
204 | devp = file->private_data; | |
205 | if (!devp->hd_ireqfreq) | |
206 | return -EIO; | |
207 | ||
208 | if (count < sizeof(unsigned long)) | |
209 | return -EINVAL; | |
210 | ||
211 | add_wait_queue(&devp->hd_waitqueue, &wait); | |
212 | ||
213 | for ( ; ; ) { | |
214 | set_current_state(TASK_INTERRUPTIBLE); | |
215 | ||
216 | spin_lock_irq(&hpet_lock); | |
217 | data = devp->hd_irqdata; | |
218 | devp->hd_irqdata = 0; | |
219 | spin_unlock_irq(&hpet_lock); | |
220 | ||
221 | if (data) | |
222 | break; | |
223 | else if (file->f_flags & O_NONBLOCK) { | |
224 | retval = -EAGAIN; | |
225 | goto out; | |
226 | } else if (signal_pending(current)) { | |
227 | retval = -ERESTARTSYS; | |
228 | goto out; | |
229 | } | |
230 | schedule(); | |
231 | } | |
232 | ||
233 | retval = put_user(data, (unsigned long __user *)buf); | |
234 | if (!retval) | |
235 | retval = sizeof(unsigned long); | |
236 | out: | |
237 | __set_current_state(TASK_RUNNING); | |
238 | remove_wait_queue(&devp->hd_waitqueue, &wait); | |
239 | ||
240 | return retval; | |
241 | } | |
242 | ||
243 | static unsigned int hpet_poll(struct file *file, poll_table * wait) | |
244 | { | |
245 | unsigned long v; | |
246 | struct hpet_dev *devp; | |
247 | ||
248 | devp = file->private_data; | |
249 | ||
250 | if (!devp->hd_ireqfreq) | |
251 | return 0; | |
252 | ||
253 | poll_wait(file, &devp->hd_waitqueue, wait); | |
254 | ||
255 | spin_lock_irq(&hpet_lock); | |
256 | v = devp->hd_irqdata; | |
257 | spin_unlock_irq(&hpet_lock); | |
258 | ||
259 | if (v != 0) | |
260 | return POLLIN | POLLRDNORM; | |
261 | ||
262 | return 0; | |
263 | } | |
264 | ||
265 | static int hpet_mmap(struct file *file, struct vm_area_struct *vma) | |
266 | { | |
267 | #ifdef CONFIG_HPET_MMAP | |
268 | struct hpet_dev *devp; | |
269 | unsigned long addr; | |
270 | ||
271 | if (((vma->vm_end - vma->vm_start) != PAGE_SIZE) || vma->vm_pgoff) | |
272 | return -EINVAL; | |
273 | ||
274 | devp = file->private_data; | |
275 | addr = devp->hd_hpets->hp_hpet_phys; | |
276 | ||
277 | if (addr & (PAGE_SIZE - 1)) | |
278 | return -ENOSYS; | |
279 | ||
280 | vma->vm_flags |= VM_IO; | |
281 | vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); | |
1da177e4 LT |
282 | |
283 | if (io_remap_pfn_range(vma, vma->vm_start, addr >> PAGE_SHIFT, | |
284 | PAGE_SIZE, vma->vm_page_prot)) { | |
285 | printk(KERN_ERR "remap_pfn_range failed in hpet.c\n"); | |
286 | return -EAGAIN; | |
287 | } | |
288 | ||
289 | return 0; | |
290 | #else | |
291 | return -ENOSYS; | |
292 | #endif | |
293 | } | |
294 | ||
295 | static int hpet_fasync(int fd, struct file *file, int on) | |
296 | { | |
297 | struct hpet_dev *devp; | |
298 | ||
299 | devp = file->private_data; | |
300 | ||
301 | if (fasync_helper(fd, file, on, &devp->hd_async_queue) >= 0) | |
302 | return 0; | |
303 | else | |
304 | return -EIO; | |
305 | } | |
306 | ||
307 | static int hpet_release(struct inode *inode, struct file *file) | |
308 | { | |
309 | struct hpet_dev *devp; | |
310 | struct hpet_timer __iomem *timer; | |
311 | int irq = 0; | |
312 | ||
313 | devp = file->private_data; | |
314 | timer = devp->hd_timer; | |
315 | ||
316 | spin_lock_irq(&hpet_lock); | |
317 | ||
318 | writeq((readq(&timer->hpet_config) & ~Tn_INT_ENB_CNF_MASK), | |
319 | &timer->hpet_config); | |
320 | ||
321 | irq = devp->hd_irq; | |
322 | devp->hd_irq = 0; | |
323 | ||
324 | devp->hd_ireqfreq = 0; | |
325 | ||
326 | if (devp->hd_flags & HPET_PERIODIC | |
327 | && readq(&timer->hpet_config) & Tn_TYPE_CNF_MASK) { | |
328 | unsigned long v; | |
329 | ||
330 | v = readq(&timer->hpet_config); | |
331 | v ^= Tn_TYPE_CNF_MASK; | |
332 | writeq(v, &timer->hpet_config); | |
333 | } | |
334 | ||
335 | devp->hd_flags &= ~(HPET_OPEN | HPET_IE | HPET_PERIODIC); | |
336 | spin_unlock_irq(&hpet_lock); | |
337 | ||
338 | if (irq) | |
339 | free_irq(irq, devp); | |
340 | ||
341 | if (file->f_flags & FASYNC) | |
342 | hpet_fasync(-1, file, 0); | |
343 | ||
344 | file->private_data = NULL; | |
345 | return 0; | |
346 | } | |
347 | ||
348 | static int hpet_ioctl_common(struct hpet_dev *, int, unsigned long, int); | |
349 | ||
350 | static int | |
351 | hpet_ioctl(struct inode *inode, struct file *file, unsigned int cmd, | |
352 | unsigned long arg) | |
353 | { | |
354 | struct hpet_dev *devp; | |
355 | ||
356 | devp = file->private_data; | |
357 | return hpet_ioctl_common(devp, cmd, arg, 0); | |
358 | } | |
359 | ||
360 | static int hpet_ioctl_ieon(struct hpet_dev *devp) | |
361 | { | |
362 | struct hpet_timer __iomem *timer; | |
363 | struct hpet __iomem *hpet; | |
364 | struct hpets *hpetp; | |
365 | int irq; | |
366 | unsigned long g, v, t, m; | |
367 | unsigned long flags, isr; | |
368 | ||
369 | timer = devp->hd_timer; | |
370 | hpet = devp->hd_hpet; | |
371 | hpetp = devp->hd_hpets; | |
372 | ||
9090e6db CL |
373 | if (!devp->hd_ireqfreq) |
374 | return -EIO; | |
375 | ||
1da177e4 LT |
376 | spin_lock_irq(&hpet_lock); |
377 | ||
378 | if (devp->hd_flags & HPET_IE) { | |
379 | spin_unlock_irq(&hpet_lock); | |
380 | return -EBUSY; | |
381 | } | |
382 | ||
383 | devp->hd_flags |= HPET_IE; | |
0d290861 CL |
384 | |
385 | if (readl(&timer->hpet_config) & Tn_INT_TYPE_CNF_MASK) | |
386 | devp->hd_flags |= HPET_SHARED_IRQ; | |
1da177e4 LT |
387 | spin_unlock_irq(&hpet_lock); |
388 | ||
1da177e4 LT |
389 | irq = devp->hd_hdwirq; |
390 | ||
391 | if (irq) { | |
0d290861 | 392 | unsigned long irq_flags; |
1da177e4 | 393 | |
0d290861 CL |
394 | sprintf(devp->hd_name, "hpet%d", (int)(devp - hpetp->hp_dev)); |
395 | irq_flags = devp->hd_flags & HPET_SHARED_IRQ | |
396 | ? SA_SHIRQ : SA_INTERRUPT; | |
397 | if (request_irq(irq, hpet_interrupt, irq_flags, | |
398 | devp->hd_name, (void *)devp)) { | |
1da177e4 LT |
399 | printk(KERN_ERR "hpet: IRQ %d is not free\n", irq); |
400 | irq = 0; | |
401 | } | |
402 | } | |
403 | ||
404 | if (irq == 0) { | |
405 | spin_lock_irq(&hpet_lock); | |
406 | devp->hd_flags ^= HPET_IE; | |
407 | spin_unlock_irq(&hpet_lock); | |
408 | return -EIO; | |
409 | } | |
410 | ||
411 | devp->hd_irq = irq; | |
412 | t = devp->hd_ireqfreq; | |
413 | v = readq(&timer->hpet_config); | |
414 | g = v | Tn_INT_ENB_CNF_MASK; | |
415 | ||
416 | if (devp->hd_flags & HPET_PERIODIC) { | |
417 | write_counter(t, &timer->hpet_compare); | |
418 | g |= Tn_TYPE_CNF_MASK; | |
419 | v |= Tn_TYPE_CNF_MASK; | |
420 | writeq(v, &timer->hpet_config); | |
421 | v |= Tn_VAL_SET_CNF_MASK; | |
422 | writeq(v, &timer->hpet_config); | |
423 | local_irq_save(flags); | |
424 | m = read_counter(&hpet->hpet_mc); | |
425 | write_counter(t + m + hpetp->hp_delta, &timer->hpet_compare); | |
426 | } else { | |
427 | local_irq_save(flags); | |
428 | m = read_counter(&hpet->hpet_mc); | |
429 | write_counter(t + m + hpetp->hp_delta, &timer->hpet_compare); | |
430 | } | |
431 | ||
0d290861 | 432 | if (devp->hd_flags & HPET_SHARED_IRQ) { |
3d5640d1 | 433 | isr = 1 << (devp - devp->hd_hpets->hp_dev); |
0d290861 CL |
434 | writel(isr, &hpet->hpet_isr); |
435 | } | |
1da177e4 LT |
436 | writeq(g, &timer->hpet_config); |
437 | local_irq_restore(flags); | |
438 | ||
439 | return 0; | |
440 | } | |
441 | ||
ba3f213f CL |
442 | /* converts Hz to number of timer ticks */ |
443 | static inline unsigned long hpet_time_div(struct hpets *hpets, | |
444 | unsigned long dis) | |
1da177e4 | 445 | { |
ba3f213f | 446 | unsigned long long m; |
1da177e4 | 447 | |
ba3f213f | 448 | m = hpets->hp_tick_freq + (dis >> 1); |
1da177e4 | 449 | do_div(m, dis); |
1da177e4 LT |
450 | return (unsigned long)m; |
451 | } | |
452 | ||
453 | static int | |
454 | hpet_ioctl_common(struct hpet_dev *devp, int cmd, unsigned long arg, int kernel) | |
455 | { | |
456 | struct hpet_timer __iomem *timer; | |
457 | struct hpet __iomem *hpet; | |
458 | struct hpets *hpetp; | |
459 | int err; | |
460 | unsigned long v; | |
461 | ||
462 | switch (cmd) { | |
463 | case HPET_IE_OFF: | |
464 | case HPET_INFO: | |
465 | case HPET_EPI: | |
466 | case HPET_DPI: | |
467 | case HPET_IRQFREQ: | |
468 | timer = devp->hd_timer; | |
469 | hpet = devp->hd_hpet; | |
470 | hpetp = devp->hd_hpets; | |
471 | break; | |
472 | case HPET_IE_ON: | |
473 | return hpet_ioctl_ieon(devp); | |
474 | default: | |
475 | return -EINVAL; | |
476 | } | |
477 | ||
478 | err = 0; | |
479 | ||
480 | switch (cmd) { | |
481 | case HPET_IE_OFF: | |
482 | if ((devp->hd_flags & HPET_IE) == 0) | |
483 | break; | |
484 | v = readq(&timer->hpet_config); | |
485 | v &= ~Tn_INT_ENB_CNF_MASK; | |
486 | writeq(v, &timer->hpet_config); | |
487 | if (devp->hd_irq) { | |
488 | free_irq(devp->hd_irq, devp); | |
489 | devp->hd_irq = 0; | |
490 | } | |
491 | devp->hd_flags ^= HPET_IE; | |
492 | break; | |
493 | case HPET_INFO: | |
494 | { | |
495 | struct hpet_info info; | |
496 | ||
af95eade CL |
497 | if (devp->hd_ireqfreq) |
498 | info.hi_ireqfreq = | |
499 | hpet_time_div(hpetp, devp->hd_ireqfreq); | |
500 | else | |
501 | info.hi_ireqfreq = 0; | |
1da177e4 LT |
502 | info.hi_flags = |
503 | readq(&timer->hpet_config) & Tn_PER_INT_CAP_MASK; | |
c860ed9f CL |
504 | info.hi_hpet = hpetp->hp_which; |
505 | info.hi_timer = devp - hpetp->hp_dev; | |
8e8505be CL |
506 | if (kernel) |
507 | memcpy((void *)arg, &info, sizeof(info)); | |
508 | else | |
509 | if (copy_to_user((void __user *)arg, &info, | |
510 | sizeof(info))) | |
511 | err = -EFAULT; | |
1da177e4 LT |
512 | break; |
513 | } | |
514 | case HPET_EPI: | |
515 | v = readq(&timer->hpet_config); | |
516 | if ((v & Tn_PER_INT_CAP_MASK) == 0) { | |
517 | err = -ENXIO; | |
518 | break; | |
519 | } | |
520 | devp->hd_flags |= HPET_PERIODIC; | |
521 | break; | |
522 | case HPET_DPI: | |
523 | v = readq(&timer->hpet_config); | |
524 | if ((v & Tn_PER_INT_CAP_MASK) == 0) { | |
525 | err = -ENXIO; | |
526 | break; | |
527 | } | |
528 | if (devp->hd_flags & HPET_PERIODIC && | |
529 | readq(&timer->hpet_config) & Tn_TYPE_CNF_MASK) { | |
530 | v = readq(&timer->hpet_config); | |
531 | v ^= Tn_TYPE_CNF_MASK; | |
532 | writeq(v, &timer->hpet_config); | |
533 | } | |
534 | devp->hd_flags &= ~HPET_PERIODIC; | |
535 | break; | |
536 | case HPET_IRQFREQ: | |
537 | if (!kernel && (arg > hpet_max_freq) && | |
538 | !capable(CAP_SYS_RESOURCE)) { | |
539 | err = -EACCES; | |
540 | break; | |
541 | } | |
542 | ||
189e2dd1 | 543 | if (!arg) { |
1da177e4 LT |
544 | err = -EINVAL; |
545 | break; | |
546 | } | |
547 | ||
ba3f213f | 548 | devp->hd_ireqfreq = hpet_time_div(hpetp, arg); |
1da177e4 LT |
549 | } |
550 | ||
551 | return err; | |
552 | } | |
553 | ||
554 | static struct file_operations hpet_fops = { | |
555 | .owner = THIS_MODULE, | |
556 | .llseek = no_llseek, | |
557 | .read = hpet_read, | |
558 | .poll = hpet_poll, | |
559 | .ioctl = hpet_ioctl, | |
560 | .open = hpet_open, | |
561 | .release = hpet_release, | |
562 | .fasync = hpet_fasync, | |
563 | .mmap = hpet_mmap, | |
564 | }; | |
565 | ||
566 | EXPORT_SYMBOL(hpet_alloc); | |
567 | EXPORT_SYMBOL(hpet_register); | |
568 | EXPORT_SYMBOL(hpet_unregister); | |
569 | EXPORT_SYMBOL(hpet_control); | |
570 | ||
571 | int hpet_register(struct hpet_task *tp, int periodic) | |
572 | { | |
573 | unsigned int i; | |
574 | u64 mask; | |
575 | struct hpet_timer __iomem *timer; | |
576 | struct hpet_dev *devp; | |
577 | struct hpets *hpetp; | |
578 | ||
579 | switch (periodic) { | |
580 | case 1: | |
581 | mask = Tn_PER_INT_CAP_MASK; | |
582 | break; | |
583 | case 0: | |
584 | mask = 0; | |
585 | break; | |
586 | default: | |
587 | return -EINVAL; | |
588 | } | |
589 | ||
7522e4ec CL |
590 | tp->ht_opaque = NULL; |
591 | ||
1da177e4 LT |
592 | spin_lock_irq(&hpet_task_lock); |
593 | spin_lock(&hpet_lock); | |
594 | ||
595 | for (devp = NULL, hpetp = hpets; hpetp && !devp; hpetp = hpetp->hp_next) | |
596 | for (timer = hpetp->hp_hpet->hpet_timers, i = 0; | |
597 | i < hpetp->hp_ntimer; i++, timer++) { | |
598 | if ((readq(&timer->hpet_config) & Tn_PER_INT_CAP_MASK) | |
599 | != mask) | |
600 | continue; | |
601 | ||
602 | devp = &hpetp->hp_dev[i]; | |
603 | ||
604 | if (devp->hd_flags & HPET_OPEN || devp->hd_task) { | |
605 | devp = NULL; | |
606 | continue; | |
607 | } | |
608 | ||
609 | tp->ht_opaque = devp; | |
610 | devp->hd_task = tp; | |
611 | break; | |
612 | } | |
613 | ||
614 | spin_unlock(&hpet_lock); | |
615 | spin_unlock_irq(&hpet_task_lock); | |
616 | ||
617 | if (tp->ht_opaque) | |
618 | return 0; | |
619 | else | |
620 | return -EBUSY; | |
621 | } | |
622 | ||
623 | static inline int hpet_tpcheck(struct hpet_task *tp) | |
624 | { | |
625 | struct hpet_dev *devp; | |
626 | struct hpets *hpetp; | |
627 | ||
628 | devp = tp->ht_opaque; | |
629 | ||
630 | if (!devp) | |
631 | return -ENXIO; | |
632 | ||
633 | for (hpetp = hpets; hpetp; hpetp = hpetp->hp_next) | |
634 | if (devp >= hpetp->hp_dev | |
635 | && devp < (hpetp->hp_dev + hpetp->hp_ntimer) | |
636 | && devp->hd_hpet == hpetp->hp_hpet) | |
637 | return 0; | |
638 | ||
639 | return -ENXIO; | |
640 | } | |
641 | ||
642 | int hpet_unregister(struct hpet_task *tp) | |
643 | { | |
644 | struct hpet_dev *devp; | |
645 | struct hpet_timer __iomem *timer; | |
646 | int err; | |
647 | ||
648 | if ((err = hpet_tpcheck(tp))) | |
649 | return err; | |
650 | ||
651 | spin_lock_irq(&hpet_task_lock); | |
652 | spin_lock(&hpet_lock); | |
653 | ||
654 | devp = tp->ht_opaque; | |
655 | if (devp->hd_task != tp) { | |
656 | spin_unlock(&hpet_lock); | |
657 | spin_unlock_irq(&hpet_task_lock); | |
658 | return -ENXIO; | |
659 | } | |
660 | ||
661 | timer = devp->hd_timer; | |
662 | writeq((readq(&timer->hpet_config) & ~Tn_INT_ENB_CNF_MASK), | |
663 | &timer->hpet_config); | |
664 | devp->hd_flags &= ~(HPET_IE | HPET_PERIODIC); | |
665 | devp->hd_task = NULL; | |
666 | spin_unlock(&hpet_lock); | |
667 | spin_unlock_irq(&hpet_task_lock); | |
668 | ||
669 | return 0; | |
670 | } | |
671 | ||
672 | int hpet_control(struct hpet_task *tp, unsigned int cmd, unsigned long arg) | |
673 | { | |
674 | struct hpet_dev *devp; | |
675 | int err; | |
676 | ||
677 | if ((err = hpet_tpcheck(tp))) | |
678 | return err; | |
679 | ||
680 | spin_lock_irq(&hpet_lock); | |
681 | devp = tp->ht_opaque; | |
682 | if (devp->hd_task != tp) { | |
683 | spin_unlock_irq(&hpet_lock); | |
684 | return -ENXIO; | |
685 | } | |
686 | spin_unlock_irq(&hpet_lock); | |
687 | return hpet_ioctl_common(devp, cmd, arg, 1); | |
688 | } | |
689 | ||
690 | static ctl_table hpet_table[] = { | |
691 | { | |
692 | .ctl_name = 1, | |
693 | .procname = "max-user-freq", | |
694 | .data = &hpet_max_freq, | |
695 | .maxlen = sizeof(int), | |
696 | .mode = 0644, | |
697 | .proc_handler = &proc_dointvec, | |
698 | }, | |
699 | {.ctl_name = 0} | |
700 | }; | |
701 | ||
702 | static ctl_table hpet_root[] = { | |
703 | { | |
704 | .ctl_name = 1, | |
705 | .procname = "hpet", | |
706 | .maxlen = 0, | |
707 | .mode = 0555, | |
708 | .child = hpet_table, | |
709 | }, | |
710 | {.ctl_name = 0} | |
711 | }; | |
712 | ||
713 | static ctl_table dev_root[] = { | |
714 | { | |
715 | .ctl_name = CTL_DEV, | |
716 | .procname = "dev", | |
717 | .maxlen = 0, | |
718 | .mode = 0555, | |
719 | .child = hpet_root, | |
720 | }, | |
721 | {.ctl_name = 0} | |
722 | }; | |
723 | ||
724 | static struct ctl_table_header *sysctl_header; | |
725 | ||
726 | static void hpet_register_interpolator(struct hpets *hpetp) | |
727 | { | |
728 | #ifdef CONFIG_TIME_INTERPOLATION | |
729 | struct time_interpolator *ti; | |
730 | ||
731 | ti = kmalloc(sizeof(*ti), GFP_KERNEL); | |
732 | if (!ti) | |
733 | return; | |
734 | ||
735 | memset(ti, 0, sizeof(*ti)); | |
736 | ti->source = TIME_SOURCE_MMIO64; | |
737 | ti->shift = 10; | |
738 | ti->addr = &hpetp->hp_hpet->hpet_mc; | |
ba3f213f | 739 | ti->frequency = hpetp->hp_tick_freq; |
96803820 | 740 | ti->drift = HPET_DRIFT; |
1da177e4 LT |
741 | ti->mask = -1; |
742 | ||
743 | hpetp->hp_interpolator = ti; | |
744 | register_time_interpolator(ti); | |
745 | #endif | |
746 | } | |
747 | ||
748 | /* | |
749 | * Adjustment for when arming the timer with | |
750 | * initial conditions. That is, main counter | |
751 | * ticks expired before interrupts are enabled. | |
752 | */ | |
753 | #define TICK_CALIBRATE (1000UL) | |
754 | ||
755 | static unsigned long hpet_calibrate(struct hpets *hpetp) | |
756 | { | |
757 | struct hpet_timer __iomem *timer = NULL; | |
758 | unsigned long t, m, count, i, flags, start; | |
759 | struct hpet_dev *devp; | |
760 | int j; | |
761 | struct hpet __iomem *hpet; | |
762 | ||
763 | for (j = 0, devp = hpetp->hp_dev; j < hpetp->hp_ntimer; j++, devp++) | |
764 | if ((devp->hd_flags & HPET_OPEN) == 0) { | |
765 | timer = devp->hd_timer; | |
766 | break; | |
767 | } | |
768 | ||
769 | if (!timer) | |
770 | return 0; | |
771 | ||
3d5640d1 | 772 | hpet = hpetp->hp_hpet; |
1da177e4 LT |
773 | t = read_counter(&timer->hpet_compare); |
774 | ||
775 | i = 0; | |
ba3f213f | 776 | count = hpet_time_div(hpetp, TICK_CALIBRATE); |
1da177e4 LT |
777 | |
778 | local_irq_save(flags); | |
779 | ||
780 | start = read_counter(&hpet->hpet_mc); | |
781 | ||
782 | do { | |
783 | m = read_counter(&hpet->hpet_mc); | |
784 | write_counter(t + m + hpetp->hp_delta, &timer->hpet_compare); | |
785 | } while (i++, (m - start) < count); | |
786 | ||
787 | local_irq_restore(flags); | |
788 | ||
789 | return (m - start) / i; | |
790 | } | |
791 | ||
792 | int hpet_alloc(struct hpet_data *hdp) | |
793 | { | |
794 | u64 cap, mcfg; | |
795 | struct hpet_dev *devp; | |
796 | u32 i, ntimer; | |
797 | struct hpets *hpetp; | |
798 | size_t siz; | |
799 | struct hpet __iomem *hpet; | |
800 | static struct hpets *last = (struct hpets *)0; | |
318db8f0 | 801 | unsigned long period; |
ba3f213f | 802 | unsigned long long temp; |
1da177e4 LT |
803 | |
804 | /* | |
805 | * hpet_alloc can be called by platform dependent code. | |
806 | * if platform dependent code has allocated the hpet | |
807 | * ACPI also reports hpet, then we catch it here. | |
808 | */ | |
809 | for (hpetp = hpets; hpetp; hpetp = hpetp->hp_next) | |
810 | if (hpetp->hp_hpet == hdp->hd_address) | |
811 | return 0; | |
812 | ||
813 | siz = sizeof(struct hpets) + ((hdp->hd_nirqs - 1) * | |
814 | sizeof(struct hpet_dev)); | |
815 | ||
816 | hpetp = kmalloc(siz, GFP_KERNEL); | |
817 | ||
818 | if (!hpetp) | |
819 | return -ENOMEM; | |
820 | ||
821 | memset(hpetp, 0, siz); | |
822 | ||
823 | hpetp->hp_which = hpet_nhpet++; | |
824 | hpetp->hp_hpet = hdp->hd_address; | |
825 | hpetp->hp_hpet_phys = hdp->hd_phys_address; | |
826 | ||
827 | hpetp->hp_ntimer = hdp->hd_nirqs; | |
828 | ||
829 | for (i = 0; i < hdp->hd_nirqs; i++) | |
830 | hpetp->hp_dev[i].hd_hdwirq = hdp->hd_irq[i]; | |
831 | ||
832 | hpet = hpetp->hp_hpet; | |
833 | ||
834 | cap = readq(&hpet->hpet_cap); | |
835 | ||
836 | ntimer = ((cap & HPET_NUM_TIM_CAP_MASK) >> HPET_NUM_TIM_CAP_SHIFT) + 1; | |
837 | ||
838 | if (hpetp->hp_ntimer != ntimer) { | |
839 | printk(KERN_WARNING "hpet: number irqs doesn't agree" | |
840 | " with number of timers\n"); | |
841 | kfree(hpetp); | |
842 | return -ENODEV; | |
843 | } | |
844 | ||
845 | if (last) | |
846 | last->hp_next = hpetp; | |
847 | else | |
848 | hpets = hpetp; | |
849 | ||
850 | last = hpetp; | |
851 | ||
ba3f213f CL |
852 | period = (cap & HPET_COUNTER_CLK_PERIOD_MASK) >> |
853 | HPET_COUNTER_CLK_PERIOD_SHIFT; /* fs, 10^-15 */ | |
854 | temp = 1000000000000000uLL; /* 10^15 femtoseconds per second */ | |
855 | temp += period >> 1; /* round */ | |
856 | do_div(temp, period); | |
857 | hpetp->hp_tick_freq = temp; /* ticks per second */ | |
1da177e4 LT |
858 | |
859 | printk(KERN_INFO "hpet%d: at MMIO 0x%lx, IRQ%s", | |
860 | hpetp->hp_which, hdp->hd_phys_address, | |
861 | hpetp->hp_ntimer > 1 ? "s" : ""); | |
862 | for (i = 0; i < hpetp->hp_ntimer; i++) | |
863 | printk("%s %d", i > 0 ? "," : "", hdp->hd_irq[i]); | |
864 | printk("\n"); | |
865 | ||
318db8f0 CL |
866 | printk(KERN_INFO "hpet%u: %u %d-bit timers, %Lu Hz\n", |
867 | hpetp->hp_which, hpetp->hp_ntimer, | |
868 | cap & HPET_COUNTER_SIZE_MASK ? 64 : 32, hpetp->hp_tick_freq); | |
1da177e4 LT |
869 | |
870 | mcfg = readq(&hpet->hpet_config); | |
871 | if ((mcfg & HPET_ENABLE_CNF_MASK) == 0) { | |
872 | write_counter(0L, &hpet->hpet_mc); | |
873 | mcfg |= HPET_ENABLE_CNF_MASK; | |
874 | writeq(mcfg, &hpet->hpet_config); | |
875 | } | |
876 | ||
642d30bb | 877 | for (i = 0, devp = hpetp->hp_dev; i < hpetp->hp_ntimer; i++, devp++) { |
1da177e4 LT |
878 | struct hpet_timer __iomem *timer; |
879 | ||
880 | timer = &hpet->hpet_timers[devp - hpetp->hp_dev]; | |
1da177e4 LT |
881 | |
882 | devp->hd_hpets = hpetp; | |
883 | devp->hd_hpet = hpet; | |
884 | devp->hd_timer = timer; | |
885 | ||
886 | /* | |
887 | * If the timer was reserved by platform code, | |
888 | * then make timer unavailable for opens. | |
889 | */ | |
890 | if (hdp->hd_state & (1 << i)) { | |
891 | devp->hd_flags = HPET_OPEN; | |
892 | continue; | |
893 | } | |
894 | ||
895 | init_waitqueue_head(&devp->hd_waitqueue); | |
896 | } | |
897 | ||
898 | hpetp->hp_delta = hpet_calibrate(hpetp); | |
899 | hpet_register_interpolator(hpetp); | |
900 | ||
901 | return 0; | |
902 | } | |
903 | ||
904 | static acpi_status hpet_resources(struct acpi_resource *res, void *data) | |
905 | { | |
906 | struct hpet_data *hdp; | |
907 | acpi_status status; | |
908 | struct acpi_resource_address64 addr; | |
909 | struct hpets *hpetp; | |
910 | ||
911 | hdp = data; | |
912 | ||
913 | status = acpi_resource_to_address64(res, &addr); | |
914 | ||
915 | if (ACPI_SUCCESS(status)) { | |
916 | unsigned long size; | |
917 | ||
918 | size = addr.max_address_range - addr.min_address_range + 1; | |
919 | hdp->hd_phys_address = addr.min_address_range; | |
920 | hdp->hd_address = ioremap(addr.min_address_range, size); | |
921 | ||
922 | for (hpetp = hpets; hpetp; hpetp = hpetp->hp_next) | |
923 | if (hpetp->hp_hpet == hdp->hd_address) | |
924 | return -EBUSY; | |
925 | } else if (res->id == ACPI_RSTYPE_EXT_IRQ) { | |
926 | struct acpi_resource_ext_irq *irqp; | |
927 | int i; | |
928 | ||
929 | irqp = &res->data.extended_irq; | |
930 | ||
931 | if (irqp->number_of_interrupts > 0) { | |
932 | hdp->hd_nirqs = irqp->number_of_interrupts; | |
933 | ||
a9bd53bc KK |
934 | for (i = 0; i < hdp->hd_nirqs; i++) { |
935 | int rc = | |
1da177e4 LT |
936 | acpi_register_gsi(irqp->interrupts[i], |
937 | irqp->edge_level, | |
938 | irqp->active_high_low); | |
a9bd53bc KK |
939 | if (rc < 0) |
940 | return AE_ERROR; | |
941 | hdp->hd_irq[i] = rc; | |
942 | } | |
1da177e4 LT |
943 | } |
944 | } | |
945 | ||
946 | return AE_OK; | |
947 | } | |
948 | ||
949 | static int hpet_acpi_add(struct acpi_device *device) | |
950 | { | |
951 | acpi_status result; | |
952 | struct hpet_data data; | |
953 | ||
954 | memset(&data, 0, sizeof(data)); | |
955 | ||
956 | result = | |
957 | acpi_walk_resources(device->handle, METHOD_NAME__CRS, | |
958 | hpet_resources, &data); | |
959 | ||
960 | if (ACPI_FAILURE(result)) | |
961 | return -ENODEV; | |
962 | ||
963 | if (!data.hd_address || !data.hd_nirqs) { | |
964 | printk("%s: no address or irqs in _CRS\n", __FUNCTION__); | |
965 | return -ENODEV; | |
966 | } | |
967 | ||
968 | return hpet_alloc(&data); | |
969 | } | |
970 | ||
971 | static int hpet_acpi_remove(struct acpi_device *device, int type) | |
972 | { | |
973 | /* XXX need to unregister interpolator, dealloc mem, etc */ | |
974 | return -EINVAL; | |
975 | } | |
976 | ||
977 | static struct acpi_driver hpet_acpi_driver = { | |
978 | .name = "hpet", | |
979 | .ids = "PNP0103", | |
980 | .ops = { | |
981 | .add = hpet_acpi_add, | |
982 | .remove = hpet_acpi_remove, | |
983 | }, | |
984 | }; | |
985 | ||
986 | static struct miscdevice hpet_misc = { HPET_MINOR, "hpet", &hpet_fops }; | |
987 | ||
988 | static int __init hpet_init(void) | |
989 | { | |
990 | int result; | |
991 | ||
992 | result = misc_register(&hpet_misc); | |
993 | if (result < 0) | |
994 | return -ENODEV; | |
995 | ||
996 | sysctl_header = register_sysctl_table(dev_root, 0); | |
997 | ||
998 | result = acpi_bus_register_driver(&hpet_acpi_driver); | |
999 | if (result < 0) { | |
1000 | if (sysctl_header) | |
1001 | unregister_sysctl_table(sysctl_header); | |
1002 | misc_deregister(&hpet_misc); | |
1003 | return result; | |
1004 | } | |
1005 | ||
1006 | return 0; | |
1007 | } | |
1008 | ||
1009 | static void __exit hpet_exit(void) | |
1010 | { | |
1011 | acpi_bus_unregister_driver(&hpet_acpi_driver); | |
1012 | ||
1013 | if (sysctl_header) | |
1014 | unregister_sysctl_table(sysctl_header); | |
1015 | misc_deregister(&hpet_misc); | |
1016 | ||
1017 | return; | |
1018 | } | |
1019 | ||
1020 | module_init(hpet_init); | |
1021 | module_exit(hpet_exit); | |
1022 | MODULE_AUTHOR("Bob Picco <Robert.Picco@hp.com>"); | |
1023 | MODULE_LICENSE("GPL"); |