x86: HPET: Chose a paranoid safe value for the ETIME check
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / x86 / kernel / hpet.c
blob987ef2902804a8e239be92ace2d9e5cda4759c6d
1 #include <linux/clocksource.h>
2 #include <linux/clockchips.h>
3 #include <linux/interrupt.h>
4 #include <linux/sysdev.h>
5 #include <linux/delay.h>
6 #include <linux/errno.h>
7 #include <linux/hpet.h>
8 #include <linux/init.h>
9 #include <linux/cpu.h>
10 #include <linux/pm.h>
11 #include <linux/io.h>
13 #include <asm/fixmap.h>
14 #include <asm/i8253.h>
15 #include <asm/hpet.h>
17 #define HPET_MASK CLOCKSOURCE_MASK(32)
18 #define HPET_SHIFT 22
20 /* FSEC = 10^-15
21 NSEC = 10^-9 */
22 #define FSEC_PER_NSEC 1000000L
24 #define HPET_DEV_USED_BIT 2
25 #define HPET_DEV_USED (1 << HPET_DEV_USED_BIT)
26 #define HPET_DEV_VALID 0x8
27 #define HPET_DEV_FSB_CAP 0x1000
28 #define HPET_DEV_PERI_CAP 0x2000
30 #define HPET_MIN_CYCLES 128
31 #define HPET_MIN_PROG_DELTA (HPET_MIN_CYCLES + (HPET_MIN_CYCLES >> 1))
33 #define EVT_TO_HPET_DEV(evt) container_of(evt, struct hpet_dev, evt)
36 * HPET address is set in acpi/boot.c, when an ACPI entry exists
38 unsigned long hpet_address;
39 u8 hpet_msi_disable;
41 #ifdef CONFIG_PCI_MSI
42 static unsigned long hpet_num_timers;
43 #endif
44 static void __iomem *hpet_virt_address;
46 struct hpet_dev {
47 struct clock_event_device evt;
48 unsigned int num;
49 int cpu;
50 unsigned int irq;
51 unsigned int flags;
52 char name[10];
55 unsigned long hpet_readl(unsigned long a)
57 return readl(hpet_virt_address + a);
60 static inline void hpet_writel(unsigned long d, unsigned long a)
62 writel(d, hpet_virt_address + a);
65 #ifdef CONFIG_X86_64
66 #include <asm/pgtable.h>
67 #endif
69 static inline void hpet_set_mapping(void)
71 hpet_virt_address = ioremap_nocache(hpet_address, HPET_MMAP_SIZE);
72 #ifdef CONFIG_X86_64
73 __set_fixmap(VSYSCALL_HPET, hpet_address, PAGE_KERNEL_VSYSCALL_NOCACHE);
74 #endif
77 static inline void hpet_clear_mapping(void)
79 iounmap(hpet_virt_address);
80 hpet_virt_address = NULL;
84 * HPET command line enable / disable
86 static int boot_hpet_disable;
87 int hpet_force_user;
88 static int hpet_verbose;
90 static int __init hpet_setup(char *str)
92 if (str) {
93 if (!strncmp("disable", str, 7))
94 boot_hpet_disable = 1;
95 if (!strncmp("force", str, 5))
96 hpet_force_user = 1;
97 if (!strncmp("verbose", str, 7))
98 hpet_verbose = 1;
100 return 1;
102 __setup("hpet=", hpet_setup);
104 static int __init disable_hpet(char *str)
106 boot_hpet_disable = 1;
107 return 1;
109 __setup("nohpet", disable_hpet);
111 static inline int is_hpet_capable(void)
113 return !boot_hpet_disable && hpet_address;
117 * HPET timer interrupt enable / disable
119 static int hpet_legacy_int_enabled;
122 * is_hpet_enabled - check whether the hpet timer interrupt is enabled
124 int is_hpet_enabled(void)
126 return is_hpet_capable() && hpet_legacy_int_enabled;
128 EXPORT_SYMBOL_GPL(is_hpet_enabled);
130 static void _hpet_print_config(const char *function, int line)
132 u32 i, timers, l, h;
133 printk(KERN_INFO "hpet: %s(%d):\n", function, line);
134 l = hpet_readl(HPET_ID);
135 h = hpet_readl(HPET_PERIOD);
136 timers = ((l & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT) + 1;
137 printk(KERN_INFO "hpet: ID: 0x%x, PERIOD: 0x%x\n", l, h);
138 l = hpet_readl(HPET_CFG);
139 h = hpet_readl(HPET_STATUS);
140 printk(KERN_INFO "hpet: CFG: 0x%x, STATUS: 0x%x\n", l, h);
141 l = hpet_readl(HPET_COUNTER);
142 h = hpet_readl(HPET_COUNTER+4);
143 printk(KERN_INFO "hpet: COUNTER_l: 0x%x, COUNTER_h: 0x%x\n", l, h);
145 for (i = 0; i < timers; i++) {
146 l = hpet_readl(HPET_Tn_CFG(i));
147 h = hpet_readl(HPET_Tn_CFG(i)+4);
148 printk(KERN_INFO "hpet: T%d: CFG_l: 0x%x, CFG_h: 0x%x\n",
149 i, l, h);
150 l = hpet_readl(HPET_Tn_CMP(i));
151 h = hpet_readl(HPET_Tn_CMP(i)+4);
152 printk(KERN_INFO "hpet: T%d: CMP_l: 0x%x, CMP_h: 0x%x\n",
153 i, l, h);
154 l = hpet_readl(HPET_Tn_ROUTE(i));
155 h = hpet_readl(HPET_Tn_ROUTE(i)+4);
156 printk(KERN_INFO "hpet: T%d ROUTE_l: 0x%x, ROUTE_h: 0x%x\n",
157 i, l, h);
161 #define hpet_print_config() \
162 do { \
163 if (hpet_verbose) \
164 _hpet_print_config(__FUNCTION__, __LINE__); \
165 } while (0)
168 * When the hpet driver (/dev/hpet) is enabled, we need to reserve
169 * timer 0 and timer 1 in case of RTC emulation.
171 #ifdef CONFIG_HPET
173 static void hpet_reserve_msi_timers(struct hpet_data *hd);
175 static void hpet_reserve_platform_timers(unsigned long id)
177 struct hpet __iomem *hpet = hpet_virt_address;
178 struct hpet_timer __iomem *timer = &hpet->hpet_timers[2];
179 unsigned int nrtimers, i;
180 struct hpet_data hd;
182 nrtimers = ((id & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT) + 1;
184 memset(&hd, 0, sizeof(hd));
185 hd.hd_phys_address = hpet_address;
186 hd.hd_address = hpet;
187 hd.hd_nirqs = nrtimers;
188 hpet_reserve_timer(&hd, 0);
190 #ifdef CONFIG_HPET_EMULATE_RTC
191 hpet_reserve_timer(&hd, 1);
192 #endif
195 * NOTE that hd_irq[] reflects IOAPIC input pins (LEGACY_8254
196 * is wrong for i8259!) not the output IRQ. Many BIOS writers
197 * don't bother configuring *any* comparator interrupts.
199 hd.hd_irq[0] = HPET_LEGACY_8254;
200 hd.hd_irq[1] = HPET_LEGACY_RTC;
202 for (i = 2; i < nrtimers; timer++, i++) {
203 hd.hd_irq[i] = (readl(&timer->hpet_config) &
204 Tn_INT_ROUTE_CNF_MASK) >> Tn_INT_ROUTE_CNF_SHIFT;
207 hpet_reserve_msi_timers(&hd);
209 hpet_alloc(&hd);
212 #else
213 static void hpet_reserve_platform_timers(unsigned long id) { }
214 #endif
217 * Common hpet info
219 static unsigned long hpet_period;
221 static void hpet_legacy_set_mode(enum clock_event_mode mode,
222 struct clock_event_device *evt);
223 static int hpet_legacy_next_event(unsigned long delta,
224 struct clock_event_device *evt);
227 * The hpet clock event device
229 static struct clock_event_device hpet_clockevent = {
230 .name = "hpet",
231 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
232 .set_mode = hpet_legacy_set_mode,
233 .set_next_event = hpet_legacy_next_event,
234 .shift = 32,
235 .irq = 0,
236 .rating = 50,
239 static void hpet_stop_counter(void)
241 unsigned long cfg = hpet_readl(HPET_CFG);
242 cfg &= ~HPET_CFG_ENABLE;
243 hpet_writel(cfg, HPET_CFG);
246 static void hpet_reset_counter(void)
248 hpet_writel(0, HPET_COUNTER);
249 hpet_writel(0, HPET_COUNTER + 4);
252 static void hpet_start_counter(void)
254 unsigned long cfg = hpet_readl(HPET_CFG);
255 cfg |= HPET_CFG_ENABLE;
256 hpet_writel(cfg, HPET_CFG);
259 static void hpet_restart_counter(void)
261 hpet_stop_counter();
262 hpet_reset_counter();
263 hpet_start_counter();
266 static void hpet_resume_device(void)
268 force_hpet_resume();
271 static void hpet_resume_counter(void)
273 hpet_resume_device();
274 hpet_restart_counter();
277 static void hpet_enable_legacy_int(void)
279 unsigned long cfg = hpet_readl(HPET_CFG);
281 cfg |= HPET_CFG_LEGACY;
282 hpet_writel(cfg, HPET_CFG);
283 hpet_legacy_int_enabled = 1;
286 static void hpet_legacy_clockevent_register(void)
288 /* Start HPET legacy interrupts */
289 hpet_enable_legacy_int();
292 * The mult factor is defined as (include/linux/clockchips.h)
293 * mult/2^shift = cyc/ns (in contrast to ns/cyc in clocksource.h)
294 * hpet_period is in units of femtoseconds (per cycle), so
295 * mult/2^shift = cyc/ns = 10^6/hpet_period
296 * mult = (10^6 * 2^shift)/hpet_period
297 * mult = (FSEC_PER_NSEC << hpet_clockevent.shift)/hpet_period
299 hpet_clockevent.mult = div_sc((unsigned long) FSEC_PER_NSEC,
300 hpet_period, hpet_clockevent.shift);
301 /* Calculate the min / max delta */
302 hpet_clockevent.max_delta_ns = clockevent_delta2ns(0x7FFFFFFF,
303 &hpet_clockevent);
304 /* Setup minimum reprogramming delta. */
305 hpet_clockevent.min_delta_ns = clockevent_delta2ns(HPET_MIN_PROG_DELTA,
306 &hpet_clockevent);
309 * Start hpet with the boot cpu mask and make it
310 * global after the IO_APIC has been initialized.
312 hpet_clockevent.cpumask = cpumask_of(smp_processor_id());
313 clockevents_register_device(&hpet_clockevent);
314 global_clock_event = &hpet_clockevent;
315 printk(KERN_DEBUG "hpet clockevent registered\n");
318 static int hpet_setup_msi_irq(unsigned int irq);
320 static void hpet_set_mode(enum clock_event_mode mode,
321 struct clock_event_device *evt, int timer)
323 unsigned long cfg, cmp, now;
324 uint64_t delta;
326 switch (mode) {
327 case CLOCK_EVT_MODE_PERIODIC:
328 hpet_stop_counter();
329 delta = ((uint64_t)(NSEC_PER_SEC/HZ)) * evt->mult;
330 delta >>= evt->shift;
331 now = hpet_readl(HPET_COUNTER);
332 cmp = now + (unsigned long) delta;
333 cfg = hpet_readl(HPET_Tn_CFG(timer));
334 /* Make sure we use edge triggered interrupts */
335 cfg &= ~HPET_TN_LEVEL;
336 cfg |= HPET_TN_ENABLE | HPET_TN_PERIODIC |
337 HPET_TN_SETVAL | HPET_TN_32BIT;
338 hpet_writel(cfg, HPET_Tn_CFG(timer));
339 hpet_writel(cmp, HPET_Tn_CMP(timer));
340 udelay(1);
342 * HPET on AMD 81xx needs a second write (with HPET_TN_SETVAL
343 * cleared) to T0_CMP to set the period. The HPET_TN_SETVAL
344 * bit is automatically cleared after the first write.
345 * (See AMD-8111 HyperTransport I/O Hub Data Sheet,
346 * Publication # 24674)
348 hpet_writel((unsigned long) delta, HPET_Tn_CMP(timer));
349 hpet_start_counter();
350 hpet_print_config();
351 break;
353 case CLOCK_EVT_MODE_ONESHOT:
354 cfg = hpet_readl(HPET_Tn_CFG(timer));
355 cfg &= ~HPET_TN_PERIODIC;
356 cfg |= HPET_TN_ENABLE | HPET_TN_32BIT;
357 hpet_writel(cfg, HPET_Tn_CFG(timer));
358 break;
360 case CLOCK_EVT_MODE_UNUSED:
361 case CLOCK_EVT_MODE_SHUTDOWN:
362 cfg = hpet_readl(HPET_Tn_CFG(timer));
363 cfg &= ~HPET_TN_ENABLE;
364 hpet_writel(cfg, HPET_Tn_CFG(timer));
365 break;
367 case CLOCK_EVT_MODE_RESUME:
368 if (timer == 0) {
369 hpet_enable_legacy_int();
370 } else {
371 struct hpet_dev *hdev = EVT_TO_HPET_DEV(evt);
372 hpet_setup_msi_irq(hdev->irq);
373 disable_irq(hdev->irq);
374 irq_set_affinity(hdev->irq, cpumask_of(hdev->cpu));
375 enable_irq(hdev->irq);
377 hpet_print_config();
378 break;
382 static int hpet_next_event(unsigned long delta,
383 struct clock_event_device *evt, int timer)
385 u32 cnt;
386 s32 res;
388 cnt = hpet_readl(HPET_COUNTER);
389 cnt += (u32) delta;
390 hpet_writel(cnt, HPET_Tn_CMP(timer));
393 * HPETs are a complete disaster. The compare register is
394 * based on a equal comparison and neither provides a less
395 * than or equal functionality (which would require to take
396 * the wraparound into account) nor a simple count down event
397 * mode. Further the write to the comparator register is
398 * delayed internally up to two HPET clock cycles in certain
399 * chipsets (ATI, ICH9,10). Some newer AMD chipsets have even
400 * longer delays. We worked around that by reading back the
401 * compare register, but that required another workaround for
402 * ICH9,10 chips where the first readout after write can
403 * return the old stale value. We already had a minimum
404 * programming delta of 5us enforced, but a NMI or SMI hitting
405 * between the counter readout and the comparator write can
406 * move us behind that point easily. Now instead of reading
407 * the compare register back several times, we make the ETIME
408 * decision based on the following: Return ETIME if the
409 * counter value after the write is less than HPET_MIN_CYCLES
410 * away from the event or if the counter is already ahead of
411 * the event. The minimum programming delta for the generic
412 * clockevents code is set to 1.5 * HPET_MIN_CYCLES.
414 res = (s32)(cnt - (u32)hpet_readl(HPET_COUNTER));
416 return res < HPET_MIN_CYCLES ? -ETIME : 0;
419 static void hpet_legacy_set_mode(enum clock_event_mode mode,
420 struct clock_event_device *evt)
422 hpet_set_mode(mode, evt, 0);
425 static int hpet_legacy_next_event(unsigned long delta,
426 struct clock_event_device *evt)
428 return hpet_next_event(delta, evt, 0);
432 * HPET MSI Support
434 #ifdef CONFIG_PCI_MSI
436 static DEFINE_PER_CPU(struct hpet_dev *, cpu_hpet_dev);
437 static struct hpet_dev *hpet_devs;
439 void hpet_msi_unmask(unsigned int irq)
441 struct hpet_dev *hdev = get_irq_data(irq);
442 unsigned long cfg;
444 /* unmask it */
445 cfg = hpet_readl(HPET_Tn_CFG(hdev->num));
446 cfg |= HPET_TN_FSB;
447 hpet_writel(cfg, HPET_Tn_CFG(hdev->num));
450 void hpet_msi_mask(unsigned int irq)
452 unsigned long cfg;
453 struct hpet_dev *hdev = get_irq_data(irq);
455 /* mask it */
456 cfg = hpet_readl(HPET_Tn_CFG(hdev->num));
457 cfg &= ~HPET_TN_FSB;
458 hpet_writel(cfg, HPET_Tn_CFG(hdev->num));
461 void hpet_msi_write(unsigned int irq, struct msi_msg *msg)
463 struct hpet_dev *hdev = get_irq_data(irq);
465 hpet_writel(msg->data, HPET_Tn_ROUTE(hdev->num));
466 hpet_writel(msg->address_lo, HPET_Tn_ROUTE(hdev->num) + 4);
469 void hpet_msi_read(unsigned int irq, struct msi_msg *msg)
471 struct hpet_dev *hdev = get_irq_data(irq);
473 msg->data = hpet_readl(HPET_Tn_ROUTE(hdev->num));
474 msg->address_lo = hpet_readl(HPET_Tn_ROUTE(hdev->num) + 4);
475 msg->address_hi = 0;
478 static void hpet_msi_set_mode(enum clock_event_mode mode,
479 struct clock_event_device *evt)
481 struct hpet_dev *hdev = EVT_TO_HPET_DEV(evt);
482 hpet_set_mode(mode, evt, hdev->num);
485 static int hpet_msi_next_event(unsigned long delta,
486 struct clock_event_device *evt)
488 struct hpet_dev *hdev = EVT_TO_HPET_DEV(evt);
489 return hpet_next_event(delta, evt, hdev->num);
492 static int hpet_setup_msi_irq(unsigned int irq)
494 if (arch_setup_hpet_msi(irq)) {
495 destroy_irq(irq);
496 return -EINVAL;
498 return 0;
501 static int hpet_assign_irq(struct hpet_dev *dev)
503 unsigned int irq;
505 irq = create_irq_nr(0, -1);
506 if (!irq)
507 return -EINVAL;
509 set_irq_data(irq, dev);
511 if (hpet_setup_msi_irq(irq))
512 return -EINVAL;
514 dev->irq = irq;
515 return 0;
518 static irqreturn_t hpet_interrupt_handler(int irq, void *data)
520 struct hpet_dev *dev = (struct hpet_dev *)data;
521 struct clock_event_device *hevt = &dev->evt;
523 if (!hevt->event_handler) {
524 printk(KERN_INFO "Spurious HPET timer interrupt on HPET timer %d\n",
525 dev->num);
526 return IRQ_HANDLED;
529 hevt->event_handler(hevt);
530 return IRQ_HANDLED;
533 static int hpet_setup_irq(struct hpet_dev *dev)
536 if (request_irq(dev->irq, hpet_interrupt_handler,
537 IRQF_TIMER | IRQF_DISABLED | IRQF_NOBALANCING,
538 dev->name, dev))
539 return -1;
541 disable_irq(dev->irq);
542 irq_set_affinity(dev->irq, cpumask_of(dev->cpu));
543 enable_irq(dev->irq);
545 printk(KERN_DEBUG "hpet: %s irq %d for MSI\n",
546 dev->name, dev->irq);
548 return 0;
551 /* This should be called in specific @cpu */
552 static void init_one_hpet_msi_clockevent(struct hpet_dev *hdev, int cpu)
554 struct clock_event_device *evt = &hdev->evt;
555 uint64_t hpet_freq;
557 WARN_ON(cpu != smp_processor_id());
558 if (!(hdev->flags & HPET_DEV_VALID))
559 return;
561 if (hpet_setup_msi_irq(hdev->irq))
562 return;
564 hdev->cpu = cpu;
565 per_cpu(cpu_hpet_dev, cpu) = hdev;
566 evt->name = hdev->name;
567 hpet_setup_irq(hdev);
568 evt->irq = hdev->irq;
570 evt->rating = 110;
571 evt->features = CLOCK_EVT_FEAT_ONESHOT;
572 if (hdev->flags & HPET_DEV_PERI_CAP)
573 evt->features |= CLOCK_EVT_FEAT_PERIODIC;
575 evt->set_mode = hpet_msi_set_mode;
576 evt->set_next_event = hpet_msi_next_event;
577 evt->shift = 32;
580 * The period is a femto seconds value. We need to calculate the
581 * scaled math multiplication factor for nanosecond to hpet tick
582 * conversion.
584 hpet_freq = 1000000000000000ULL;
585 do_div(hpet_freq, hpet_period);
586 evt->mult = div_sc((unsigned long) hpet_freq,
587 NSEC_PER_SEC, evt->shift);
588 /* Calculate the max delta */
589 evt->max_delta_ns = clockevent_delta2ns(0x7FFFFFFF, evt);
590 /* 5 usec minimum reprogramming delta. */
591 evt->min_delta_ns = 5000;
593 evt->cpumask = cpumask_of(hdev->cpu);
594 clockevents_register_device(evt);
597 #ifdef CONFIG_HPET
598 /* Reserve at least one timer for userspace (/dev/hpet) */
599 #define RESERVE_TIMERS 1
600 #else
601 #define RESERVE_TIMERS 0
602 #endif
604 static void hpet_msi_capability_lookup(unsigned int start_timer)
606 unsigned int id;
607 unsigned int num_timers;
608 unsigned int num_timers_used = 0;
609 int i;
611 if (hpet_msi_disable)
612 return;
614 if (boot_cpu_has(X86_FEATURE_ARAT))
615 return;
616 id = hpet_readl(HPET_ID);
618 num_timers = ((id & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT);
619 num_timers++; /* Value read out starts from 0 */
620 hpet_print_config();
622 hpet_devs = kzalloc(sizeof(struct hpet_dev) * num_timers, GFP_KERNEL);
623 if (!hpet_devs)
624 return;
626 hpet_num_timers = num_timers;
628 for (i = start_timer; i < num_timers - RESERVE_TIMERS; i++) {
629 struct hpet_dev *hdev = &hpet_devs[num_timers_used];
630 unsigned long cfg = hpet_readl(HPET_Tn_CFG(i));
632 /* Only consider HPET timer with MSI support */
633 if (!(cfg & HPET_TN_FSB_CAP))
634 continue;
636 hdev->flags = 0;
637 if (cfg & HPET_TN_PERIODIC_CAP)
638 hdev->flags |= HPET_DEV_PERI_CAP;
639 hdev->num = i;
641 sprintf(hdev->name, "hpet%d", i);
642 if (hpet_assign_irq(hdev))
643 continue;
645 hdev->flags |= HPET_DEV_FSB_CAP;
646 hdev->flags |= HPET_DEV_VALID;
647 num_timers_used++;
648 if (num_timers_used == num_possible_cpus())
649 break;
652 printk(KERN_INFO "HPET: %d timers in total, %d timers will be used for per-cpu timer\n",
653 num_timers, num_timers_used);
656 #ifdef CONFIG_HPET
657 static void hpet_reserve_msi_timers(struct hpet_data *hd)
659 int i;
661 if (!hpet_devs)
662 return;
664 for (i = 0; i < hpet_num_timers; i++) {
665 struct hpet_dev *hdev = &hpet_devs[i];
667 if (!(hdev->flags & HPET_DEV_VALID))
668 continue;
670 hd->hd_irq[hdev->num] = hdev->irq;
671 hpet_reserve_timer(hd, hdev->num);
674 #endif
676 static struct hpet_dev *hpet_get_unused_timer(void)
678 int i;
680 if (!hpet_devs)
681 return NULL;
683 for (i = 0; i < hpet_num_timers; i++) {
684 struct hpet_dev *hdev = &hpet_devs[i];
686 if (!(hdev->flags & HPET_DEV_VALID))
687 continue;
688 if (test_and_set_bit(HPET_DEV_USED_BIT,
689 (unsigned long *)&hdev->flags))
690 continue;
691 return hdev;
693 return NULL;
696 struct hpet_work_struct {
697 struct delayed_work work;
698 struct completion complete;
701 static void hpet_work(struct work_struct *w)
703 struct hpet_dev *hdev;
704 int cpu = smp_processor_id();
705 struct hpet_work_struct *hpet_work;
707 hpet_work = container_of(w, struct hpet_work_struct, work.work);
709 hdev = hpet_get_unused_timer();
710 if (hdev)
711 init_one_hpet_msi_clockevent(hdev, cpu);
713 complete(&hpet_work->complete);
716 static int hpet_cpuhp_notify(struct notifier_block *n,
717 unsigned long action, void *hcpu)
719 unsigned long cpu = (unsigned long)hcpu;
720 struct hpet_work_struct work;
721 struct hpet_dev *hdev = per_cpu(cpu_hpet_dev, cpu);
723 switch (action & 0xf) {
724 case CPU_ONLINE:
725 INIT_DELAYED_WORK_ON_STACK(&work.work, hpet_work);
726 init_completion(&work.complete);
727 /* FIXME: add schedule_work_on() */
728 schedule_delayed_work_on(cpu, &work.work, 0);
729 wait_for_completion(&work.complete);
730 destroy_timer_on_stack(&work.work.timer);
731 break;
732 case CPU_DEAD:
733 if (hdev) {
734 free_irq(hdev->irq, hdev);
735 hdev->flags &= ~HPET_DEV_USED;
736 per_cpu(cpu_hpet_dev, cpu) = NULL;
738 break;
740 return NOTIFY_OK;
742 #else
744 static int hpet_setup_msi_irq(unsigned int irq)
746 return 0;
748 static void hpet_msi_capability_lookup(unsigned int start_timer)
750 return;
753 #ifdef CONFIG_HPET
754 static void hpet_reserve_msi_timers(struct hpet_data *hd)
756 return;
758 #endif
760 static int hpet_cpuhp_notify(struct notifier_block *n,
761 unsigned long action, void *hcpu)
763 return NOTIFY_OK;
766 #endif
769 * Clock source related code
771 static cycle_t read_hpet(struct clocksource *cs)
773 return (cycle_t)hpet_readl(HPET_COUNTER);
776 #ifdef CONFIG_X86_64
777 static cycle_t __vsyscall_fn vread_hpet(void)
779 return readl((const void __iomem *)fix_to_virt(VSYSCALL_HPET) + 0xf0);
781 #endif
783 static struct clocksource clocksource_hpet = {
784 .name = "hpet",
785 .rating = 250,
786 .read = read_hpet,
787 .mask = HPET_MASK,
788 .shift = HPET_SHIFT,
789 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
790 .resume = hpet_resume_counter,
791 #ifdef CONFIG_X86_64
792 .vread = vread_hpet,
793 #endif
796 static int hpet_clocksource_register(void)
798 u64 start, now;
799 cycle_t t1;
801 /* Start the counter */
802 hpet_restart_counter();
804 /* Verify whether hpet counter works */
805 t1 = hpet_readl(HPET_COUNTER);
806 rdtscll(start);
809 * We don't know the TSC frequency yet, but waiting for
810 * 200000 TSC cycles is safe:
811 * 4 GHz == 50us
812 * 1 GHz == 200us
814 do {
815 rep_nop();
816 rdtscll(now);
817 } while ((now - start) < 200000UL);
819 if (t1 == hpet_readl(HPET_COUNTER)) {
820 printk(KERN_WARNING
821 "HPET counter not counting. HPET disabled\n");
822 return -ENODEV;
826 * The definition of mult is (include/linux/clocksource.h)
827 * mult/2^shift = ns/cyc and hpet_period is in units of fsec/cyc
828 * so we first need to convert hpet_period to ns/cyc units:
829 * mult/2^shift = ns/cyc = hpet_period/10^6
830 * mult = (hpet_period * 2^shift)/10^6
831 * mult = (hpet_period << shift)/FSEC_PER_NSEC
833 clocksource_hpet.mult = div_sc(hpet_period, FSEC_PER_NSEC, HPET_SHIFT);
835 clocksource_register(&clocksource_hpet);
837 return 0;
841 * hpet_enable - Try to setup the HPET timer. Returns 1 on success.
843 int __init hpet_enable(void)
845 unsigned long id;
846 int i;
848 if (!is_hpet_capable())
849 return 0;
851 hpet_set_mapping();
854 * Read the period and check for a sane value:
856 hpet_period = hpet_readl(HPET_PERIOD);
859 * AMD SB700 based systems with spread spectrum enabled use a
860 * SMM based HPET emulation to provide proper frequency
861 * setting. The SMM code is initialized with the first HPET
862 * register access and takes some time to complete. During
863 * this time the config register reads 0xffffffff. We check
864 * for max. 1000 loops whether the config register reads a non
865 * 0xffffffff value to make sure that HPET is up and running
866 * before we go further. A counting loop is safe, as the HPET
867 * access takes thousands of CPU cycles. On non SB700 based
868 * machines this check is only done once and has no side
869 * effects.
871 for (i = 0; hpet_readl(HPET_CFG) == 0xFFFFFFFF; i++) {
872 if (i == 1000) {
873 printk(KERN_WARNING
874 "HPET config register value = 0xFFFFFFFF. "
875 "Disabling HPET\n");
876 goto out_nohpet;
880 if (hpet_period < HPET_MIN_PERIOD || hpet_period > HPET_MAX_PERIOD)
881 goto out_nohpet;
884 * Read the HPET ID register to retrieve the IRQ routing
885 * information and the number of channels
887 id = hpet_readl(HPET_ID);
888 hpet_print_config();
890 #ifdef CONFIG_HPET_EMULATE_RTC
892 * The legacy routing mode needs at least two channels, tick timer
893 * and the rtc emulation channel.
895 if (!(id & HPET_ID_NUMBER))
896 goto out_nohpet;
897 #endif
899 if (hpet_clocksource_register())
900 goto out_nohpet;
902 if (id & HPET_ID_LEGSUP) {
903 hpet_legacy_clockevent_register();
904 return 1;
906 return 0;
908 out_nohpet:
909 hpet_clear_mapping();
910 hpet_address = 0;
911 return 0;
915 * Needs to be late, as the reserve_timer code calls kalloc !
917 * Not a problem on i386 as hpet_enable is called from late_time_init,
918 * but on x86_64 it is necessary !
920 static __init int hpet_late_init(void)
922 int cpu;
924 if (boot_hpet_disable)
925 return -ENODEV;
927 if (!hpet_address) {
928 if (!force_hpet_address)
929 return -ENODEV;
931 hpet_address = force_hpet_address;
932 hpet_enable();
935 if (!hpet_virt_address)
936 return -ENODEV;
938 if (hpet_readl(HPET_ID) & HPET_ID_LEGSUP)
939 hpet_msi_capability_lookup(2);
940 else
941 hpet_msi_capability_lookup(0);
943 hpet_reserve_platform_timers(hpet_readl(HPET_ID));
944 hpet_print_config();
946 if (hpet_msi_disable)
947 return 0;
949 if (boot_cpu_has(X86_FEATURE_ARAT))
950 return 0;
952 for_each_online_cpu(cpu) {
953 hpet_cpuhp_notify(NULL, CPU_ONLINE, (void *)(long)cpu);
956 /* This notifier should be called after workqueue is ready */
957 hotcpu_notifier(hpet_cpuhp_notify, -20);
959 return 0;
961 fs_initcall(hpet_late_init);
963 void hpet_disable(void)
965 if (is_hpet_capable() && hpet_virt_address) {
966 unsigned long cfg = hpet_readl(HPET_CFG);
968 if (hpet_legacy_int_enabled) {
969 cfg &= ~HPET_CFG_LEGACY;
970 hpet_legacy_int_enabled = 0;
972 cfg &= ~HPET_CFG_ENABLE;
973 hpet_writel(cfg, HPET_CFG);
977 #ifdef CONFIG_HPET_EMULATE_RTC
979 /* HPET in LegacyReplacement Mode eats up RTC interrupt line. When, HPET
980 * is enabled, we support RTC interrupt functionality in software.
981 * RTC has 3 kinds of interrupts:
982 * 1) Update Interrupt - generate an interrupt, every sec, when RTC clock
983 * is updated
984 * 2) Alarm Interrupt - generate an interrupt at a specific time of day
985 * 3) Periodic Interrupt - generate periodic interrupt, with frequencies
986 * 2Hz-8192Hz (2Hz-64Hz for non-root user) (all freqs in powers of 2)
987 * (1) and (2) above are implemented using polling at a frequency of
988 * 64 Hz. The exact frequency is a tradeoff between accuracy and interrupt
989 * overhead. (DEFAULT_RTC_INT_FREQ)
990 * For (3), we use interrupts at 64Hz or user specified periodic
991 * frequency, whichever is higher.
993 #include <linux/mc146818rtc.h>
994 #include <linux/rtc.h>
995 #include <asm/rtc.h>
997 #define DEFAULT_RTC_INT_FREQ 64
998 #define DEFAULT_RTC_SHIFT 6
999 #define RTC_NUM_INTS 1
1001 static unsigned long hpet_rtc_flags;
1002 static int hpet_prev_update_sec;
1003 static struct rtc_time hpet_alarm_time;
1004 static unsigned long hpet_pie_count;
1005 static u32 hpet_t1_cmp;
1006 static unsigned long hpet_default_delta;
1007 static unsigned long hpet_pie_delta;
1008 static unsigned long hpet_pie_limit;
1010 static rtc_irq_handler irq_handler;
1013 * Check that the hpet counter c1 is ahead of the c2
1015 static inline int hpet_cnt_ahead(u32 c1, u32 c2)
1017 return (s32)(c2 - c1) < 0;
1021 * Registers a IRQ handler.
1023 int hpet_register_irq_handler(rtc_irq_handler handler)
1025 if (!is_hpet_enabled())
1026 return -ENODEV;
1027 if (irq_handler)
1028 return -EBUSY;
1030 irq_handler = handler;
1032 return 0;
1034 EXPORT_SYMBOL_GPL(hpet_register_irq_handler);
1037 * Deregisters the IRQ handler registered with hpet_register_irq_handler()
1038 * and does cleanup.
1040 void hpet_unregister_irq_handler(rtc_irq_handler handler)
1042 if (!is_hpet_enabled())
1043 return;
1045 irq_handler = NULL;
1046 hpet_rtc_flags = 0;
1048 EXPORT_SYMBOL_GPL(hpet_unregister_irq_handler);
1051 * Timer 1 for RTC emulation. We use one shot mode, as periodic mode
1052 * is not supported by all HPET implementations for timer 1.
1054 * hpet_rtc_timer_init() is called when the rtc is initialized.
1056 int hpet_rtc_timer_init(void)
1058 unsigned long cfg, cnt, delta, flags;
1060 if (!is_hpet_enabled())
1061 return 0;
1063 if (!hpet_default_delta) {
1064 uint64_t clc;
1066 clc = (uint64_t) hpet_clockevent.mult * NSEC_PER_SEC;
1067 clc >>= hpet_clockevent.shift + DEFAULT_RTC_SHIFT;
1068 hpet_default_delta = (unsigned long) clc;
1071 if (!(hpet_rtc_flags & RTC_PIE) || hpet_pie_limit)
1072 delta = hpet_default_delta;
1073 else
1074 delta = hpet_pie_delta;
1076 local_irq_save(flags);
1078 cnt = delta + hpet_readl(HPET_COUNTER);
1079 hpet_writel(cnt, HPET_T1_CMP);
1080 hpet_t1_cmp = cnt;
1082 cfg = hpet_readl(HPET_T1_CFG);
1083 cfg &= ~HPET_TN_PERIODIC;
1084 cfg |= HPET_TN_ENABLE | HPET_TN_32BIT;
1085 hpet_writel(cfg, HPET_T1_CFG);
1087 local_irq_restore(flags);
1089 return 1;
1091 EXPORT_SYMBOL_GPL(hpet_rtc_timer_init);
1094 * The functions below are called from rtc driver.
1095 * Return 0 if HPET is not being used.
1096 * Otherwise do the necessary changes and return 1.
1098 int hpet_mask_rtc_irq_bit(unsigned long bit_mask)
1100 if (!is_hpet_enabled())
1101 return 0;
1103 hpet_rtc_flags &= ~bit_mask;
1104 return 1;
1106 EXPORT_SYMBOL_GPL(hpet_mask_rtc_irq_bit);
1108 int hpet_set_rtc_irq_bit(unsigned long bit_mask)
1110 unsigned long oldbits = hpet_rtc_flags;
1112 if (!is_hpet_enabled())
1113 return 0;
1115 hpet_rtc_flags |= bit_mask;
1117 if ((bit_mask & RTC_UIE) && !(oldbits & RTC_UIE))
1118 hpet_prev_update_sec = -1;
1120 if (!oldbits)
1121 hpet_rtc_timer_init();
1123 return 1;
1125 EXPORT_SYMBOL_GPL(hpet_set_rtc_irq_bit);
1127 int hpet_set_alarm_time(unsigned char hrs, unsigned char min,
1128 unsigned char sec)
1130 if (!is_hpet_enabled())
1131 return 0;
1133 hpet_alarm_time.tm_hour = hrs;
1134 hpet_alarm_time.tm_min = min;
1135 hpet_alarm_time.tm_sec = sec;
1137 return 1;
1139 EXPORT_SYMBOL_GPL(hpet_set_alarm_time);
1141 int hpet_set_periodic_freq(unsigned long freq)
1143 uint64_t clc;
1145 if (!is_hpet_enabled())
1146 return 0;
1148 if (freq <= DEFAULT_RTC_INT_FREQ)
1149 hpet_pie_limit = DEFAULT_RTC_INT_FREQ / freq;
1150 else {
1151 clc = (uint64_t) hpet_clockevent.mult * NSEC_PER_SEC;
1152 do_div(clc, freq);
1153 clc >>= hpet_clockevent.shift;
1154 hpet_pie_delta = (unsigned long) clc;
1156 return 1;
1158 EXPORT_SYMBOL_GPL(hpet_set_periodic_freq);
1160 int hpet_rtc_dropped_irq(void)
1162 return is_hpet_enabled();
1164 EXPORT_SYMBOL_GPL(hpet_rtc_dropped_irq);
1166 static void hpet_rtc_timer_reinit(void)
1168 unsigned long cfg, delta;
1169 int lost_ints = -1;
1171 if (unlikely(!hpet_rtc_flags)) {
1172 cfg = hpet_readl(HPET_T1_CFG);
1173 cfg &= ~HPET_TN_ENABLE;
1174 hpet_writel(cfg, HPET_T1_CFG);
1175 return;
1178 if (!(hpet_rtc_flags & RTC_PIE) || hpet_pie_limit)
1179 delta = hpet_default_delta;
1180 else
1181 delta = hpet_pie_delta;
1184 * Increment the comparator value until we are ahead of the
1185 * current count.
1187 do {
1188 hpet_t1_cmp += delta;
1189 hpet_writel(hpet_t1_cmp, HPET_T1_CMP);
1190 lost_ints++;
1191 } while (!hpet_cnt_ahead(hpet_t1_cmp, hpet_readl(HPET_COUNTER)));
1193 if (lost_ints) {
1194 if (hpet_rtc_flags & RTC_PIE)
1195 hpet_pie_count += lost_ints;
1196 if (printk_ratelimit())
1197 printk(KERN_WARNING "hpet1: lost %d rtc interrupts\n",
1198 lost_ints);
1202 irqreturn_t hpet_rtc_interrupt(int irq, void *dev_id)
1204 struct rtc_time curr_time;
1205 unsigned long rtc_int_flag = 0;
1207 hpet_rtc_timer_reinit();
1208 memset(&curr_time, 0, sizeof(struct rtc_time));
1210 if (hpet_rtc_flags & (RTC_UIE | RTC_AIE))
1211 get_rtc_time(&curr_time);
1213 if (hpet_rtc_flags & RTC_UIE &&
1214 curr_time.tm_sec != hpet_prev_update_sec) {
1215 if (hpet_prev_update_sec >= 0)
1216 rtc_int_flag = RTC_UF;
1217 hpet_prev_update_sec = curr_time.tm_sec;
1220 if (hpet_rtc_flags & RTC_PIE &&
1221 ++hpet_pie_count >= hpet_pie_limit) {
1222 rtc_int_flag |= RTC_PF;
1223 hpet_pie_count = 0;
1226 if (hpet_rtc_flags & RTC_AIE &&
1227 (curr_time.tm_sec == hpet_alarm_time.tm_sec) &&
1228 (curr_time.tm_min == hpet_alarm_time.tm_min) &&
1229 (curr_time.tm_hour == hpet_alarm_time.tm_hour))
1230 rtc_int_flag |= RTC_AF;
1232 if (rtc_int_flag) {
1233 rtc_int_flag |= (RTC_IRQF | (RTC_NUM_INTS << 8));
1234 if (irq_handler)
1235 irq_handler(rtc_int_flag, dev_id);
1237 return IRQ_HANDLED;
1239 EXPORT_SYMBOL_GPL(hpet_rtc_interrupt);
1240 #endif