1 #include <linux/clocksource.h>
2 #include <linux/clockchips.h>
3 #include <linux/interrupt.h>
4 #include <linux/export.h>
5 #include <linux/delay.h>
6 #include <linux/errno.h>
7 #include <linux/i8253.h>
8 #include <linux/slab.h>
9 #include <linux/hpet.h>
10 #include <linux/init.h>
11 #include <linux/cpu.h>
15 #include <asm/fixmap.h>
19 #define HPET_MASK CLOCKSOURCE_MASK(32)
23 #define FSEC_PER_NSEC 1000000L
25 #define HPET_DEV_USED_BIT 2
26 #define HPET_DEV_USED (1 << HPET_DEV_USED_BIT)
27 #define HPET_DEV_VALID 0x8
28 #define HPET_DEV_FSB_CAP 0x1000
29 #define HPET_DEV_PERI_CAP 0x2000
31 #define HPET_MIN_CYCLES 128
32 #define HPET_MIN_PROG_DELTA (HPET_MIN_CYCLES + (HPET_MIN_CYCLES >> 1))
35 * HPET address is set in acpi/boot.c, when an ACPI entry exists
37 unsigned long hpet_address;
38 u8 hpet_blockid; /* OS timer block num */
42 static unsigned long hpet_num_timers;
44 static void __iomem *hpet_virt_address;
45 static int hpet_legacy_use_64_bits;
48 struct clock_event_device evt;
56 inline struct hpet_dev *EVT_TO_HPET_DEV(struct clock_event_device *evtdev)
58 return container_of(evtdev, struct hpet_dev, evt);
61 inline unsigned int hpet_readl(unsigned int a)
63 return readl(hpet_virt_address + a);
66 static inline void hpet_writel(unsigned int d, unsigned int a)
68 writel(d, hpet_virt_address + a);
72 #include <asm/pgtable.h>
73 static inline unsigned long hpet_read_value(unsigned long a)
75 if (hpet_legacy_use_64_bits)
76 return readq(hpet_virt_address + a);
78 return readl(hpet_virt_address + a);
81 static void hpet_write_value(unsigned long d, unsigned long a)
83 if (hpet_legacy_use_64_bits)
84 writeq(d, hpet_virt_address + a);
86 writel(d, hpet_virt_address + a);
91 static inline unsigned long hpet_read_value(unsigned long a)
93 return readl(hpet_virt_address + a);
96 static void hpet_write_value(unsigned long d, unsigned long a)
98 writel(d, hpet_virt_address + a);
102 static inline void hpet_set_mapping(void)
104 hpet_virt_address = ioremap_nocache(hpet_address, HPET_MMAP_SIZE);
106 __set_fixmap(VSYSCALL_HPET, hpet_address, PAGE_KERNEL_VVAR_NOCACHE);
110 static inline void hpet_clear_mapping(void)
112 iounmap(hpet_virt_address);
113 hpet_virt_address = NULL;
117 * HPET command line enable / disable
119 static int boot_hpet_disable;
121 static int hpet_verbose;
123 static int __init hpet_setup(char *str)
126 if (!strncmp("disable", str, 7))
127 boot_hpet_disable = 1;
128 if (!strncmp("force", str, 5))
130 if (!strncmp("verbose", str, 7))
135 __setup("hpet=", hpet_setup);
137 static int __init disable_hpet(char *str)
139 boot_hpet_disable = 1;
142 __setup("nohpet", disable_hpet);
145 static int hpet64 = 0;
146 static int __init hpet64_setup(char *str)
151 __setup("hpet64", hpet64_setup);
155 static inline int is_hpet_capable(void)
157 return !boot_hpet_disable && hpet_address;
161 * HPET timer interrupt enable / disable
163 static int hpet_legacy_int_enabled;
166 * is_hpet_enabled - check whether the hpet timer interrupt is enabled
168 int is_hpet_enabled(void)
170 return is_hpet_capable() && hpet_legacy_int_enabled;
172 EXPORT_SYMBOL_GPL(is_hpet_enabled);
174 static void _hpet_print_config(const char *function, int line)
177 printk(KERN_INFO "hpet: %s(%d):\n", function, line);
178 l = hpet_readl(HPET_ID);
179 h = hpet_readl(HPET_PERIOD);
180 timers = ((l & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT) + 1;
181 printk(KERN_INFO "hpet: ID: 0x%x, PERIOD: 0x%x\n", l, h);
182 l = hpet_readl(HPET_CFG);
183 h = hpet_readl(HPET_STATUS);
184 printk(KERN_INFO "hpet: CFG: 0x%x, STATUS: 0x%x\n", l, h);
185 l = hpet_readl(HPET_COUNTER);
186 h = hpet_readl(HPET_COUNTER+4);
187 printk(KERN_INFO "hpet: COUNTER_l: 0x%x, COUNTER_h: 0x%x\n", l, h);
189 for (i = 0; i < timers; i++) {
190 l = hpet_readl(HPET_Tn_CFG(i));
191 h = hpet_readl(HPET_Tn_CFG(i)+4);
192 printk(KERN_INFO "hpet: T%d: CFG_l: 0x%x, CFG_h: 0x%x\n",
194 l = hpet_readl(HPET_Tn_CMP(i));
195 h = hpet_readl(HPET_Tn_CMP(i)+4);
196 printk(KERN_INFO "hpet: T%d: CMP_l: 0x%x, CMP_h: 0x%x\n",
198 l = hpet_readl(HPET_Tn_ROUTE(i));
199 h = hpet_readl(HPET_Tn_ROUTE(i)+4);
200 printk(KERN_INFO "hpet: T%d ROUTE_l: 0x%x, ROUTE_h: 0x%x\n",
205 #define hpet_print_config() \
208 _hpet_print_config(__FUNCTION__, __LINE__); \
212 * When the hpet driver (/dev/hpet) is enabled, we need to reserve
213 * timer 0 and timer 1 in case of RTC emulation.
217 static void hpet_reserve_msi_timers(struct hpet_data *hd);
219 static void hpet_reserve_platform_timers(unsigned int id)
221 struct hpet __iomem *hpet = hpet_virt_address;
222 struct hpet_timer __iomem *timer = &hpet->hpet_timers[2];
223 unsigned int nrtimers, i;
226 nrtimers = ((id & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT) + 1;
228 memset(&hd, 0, sizeof(hd));
229 hd.hd_phys_address = hpet_address;
230 hd.hd_address = hpet;
231 hd.hd_nirqs = nrtimers;
232 hpet_reserve_timer(&hd, 0);
234 #ifdef CONFIG_HPET_EMULATE_RTC
235 hpet_reserve_timer(&hd, 1);
239 * NOTE that hd_irq[] reflects IOAPIC input pins (LEGACY_8254
240 * is wrong for i8259!) not the output IRQ. Many BIOS writers
241 * don't bother configuring *any* comparator interrupts.
243 hd.hd_irq[0] = HPET_LEGACY_8254;
244 hd.hd_irq[1] = HPET_LEGACY_RTC;
246 for (i = 2; i < nrtimers; timer++, i++) {
247 hd.hd_irq[i] = (readl(&timer->hpet_config) &
248 Tn_INT_ROUTE_CNF_MASK) >> Tn_INT_ROUTE_CNF_SHIFT;
251 hpet_reserve_msi_timers(&hd);
257 static void hpet_reserve_platform_timers(unsigned int id) { }
263 static unsigned long hpet_freq;
264 static int hpet_legacy_use_64_bits; /* configure T0 in 64-bit mode? */
266 static void hpet_legacy_set_mode(enum clock_event_mode mode,
267 struct clock_event_device *evt);
268 static int hpet_legacy_next_event(unsigned long delta,
269 struct clock_event_device *evt);
272 * The hpet clock event device
274 static struct clock_event_device hpet_clockevent = {
276 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
277 .set_mode = hpet_legacy_set_mode,
278 .set_next_event = hpet_legacy_next_event,
283 static void hpet_stop_counter(void)
285 unsigned long cfg = hpet_readl(HPET_CFG);
286 cfg &= ~HPET_CFG_ENABLE;
287 hpet_writel(cfg, HPET_CFG);
290 static void hpet_reset_counter(void)
292 hpet_writel(0, HPET_COUNTER);
293 hpet_writel(0, HPET_COUNTER + 4);
296 static void hpet_start_counter(void)
298 unsigned int cfg = hpet_readl(HPET_CFG);
299 cfg |= HPET_CFG_ENABLE;
300 hpet_writel(cfg, HPET_CFG);
303 static void hpet_restart_counter(void)
306 hpet_reset_counter();
307 hpet_start_counter();
310 static void hpet_resume_device(void)
315 static void hpet_resume_counter(struct clocksource *cs)
317 hpet_resume_device();
318 hpet_restart_counter();
321 static void hpet_enable_legacy_int(void)
323 unsigned int cfg = hpet_readl(HPET_CFG);
325 cfg |= HPET_CFG_LEGACY;
326 hpet_writel(cfg, HPET_CFG);
327 hpet_legacy_int_enabled = 1;
330 static int timer0_use_64_bits(void)
332 #ifndef CONFIG_X86_64
333 /* using the HPET in 64-bit mode without atomic 64-bit
334 * accesses is too inefficient
339 if (unlikely(hpet64)) {
341 id = hpet_readl(HPET_ID);
342 t0_cfg = hpet_readl(HPET_Tn_CFG(0));
344 if ((id & HPET_ID_64BIT) && (t0_cfg & HPET_TN_64BIT_CAP)) {
345 printk(KERN_DEBUG "hpet timer0 configured in 64-bit mode\n");
349 printk(KERN_DEBUG "hpet timer0 does not support 64-bit mode\n");
357 static void hpet_legacy_clockevent_register(void)
359 /* Start HPET legacy interrupts */
360 hpet_enable_legacy_int();
361 hpet_legacy_use_64_bits = timer0_use_64_bits();
364 * Start hpet with the boot cpu mask and make it
365 * global after the IO_APIC has been initialized.
367 hpet_clockevent.cpumask = cpumask_of(smp_processor_id());
368 clockevents_config_and_register(&hpet_clockevent, hpet_freq,
369 HPET_MIN_PROG_DELTA, 0x7FFFFFFF);
370 global_clock_event = &hpet_clockevent;
371 printk(KERN_DEBUG "hpet clockevent registered\n");
374 static int hpet_setup_msi_irq(unsigned int irq);
376 static void hpet_set_mode(enum clock_event_mode mode,
377 struct clock_event_device *evt, int timer)
379 unsigned int cfg, cmp, now;
383 case CLOCK_EVT_MODE_PERIODIC:
385 delta = ((uint64_t)(NSEC_PER_SEC/HZ)) * evt->mult;
386 delta >>= evt->shift;
387 now = hpet_readl(HPET_COUNTER);
388 cmp = now + (unsigned int) delta;
389 cfg = hpet_readl(HPET_Tn_CFG(timer));
390 /* Make sure we use edge triggered interrupts */
391 cfg &= ~HPET_TN_LEVEL;
392 cfg |= HPET_TN_ENABLE | HPET_TN_PERIODIC |
394 (hpet_legacy_use_64_bits ? 0 : HPET_TN_32BIT);
395 hpet_writel(cfg, HPET_Tn_CFG(timer));
396 hpet_write_value(cmp, HPET_Tn_CMP(timer));
399 * HPET on AMD 81xx needs a second write (with HPET_TN_SETVAL
400 * cleared) to T0_CMP to set the period. The HPET_TN_SETVAL
401 * bit is automatically cleared after the first write.
402 * (See AMD-8111 HyperTransport I/O Hub Data Sheet,
403 * Publication # 24674)
405 hpet_write_value((unsigned long) delta, HPET_Tn_CMP(timer));
406 hpet_start_counter();
410 case CLOCK_EVT_MODE_ONESHOT:
411 cfg = hpet_readl(HPET_Tn_CFG(timer));
412 cfg &= ~HPET_TN_PERIODIC;
413 cfg |= HPET_TN_ENABLE |
414 (hpet_legacy_use_64_bits ? 0 : HPET_TN_32BIT);
415 hpet_writel(cfg, HPET_Tn_CFG(timer));
418 case CLOCK_EVT_MODE_UNUSED:
419 case CLOCK_EVT_MODE_SHUTDOWN:
420 cfg = hpet_readl(HPET_Tn_CFG(timer));
421 cfg &= ~HPET_TN_ENABLE;
422 hpet_writel(cfg, HPET_Tn_CFG(timer));
425 case CLOCK_EVT_MODE_RESUME:
427 hpet_enable_legacy_int();
429 struct hpet_dev *hdev = EVT_TO_HPET_DEV(evt);
430 hpet_setup_msi_irq(hdev->irq);
431 disable_irq(hdev->irq);
432 irq_set_affinity(hdev->irq, cpumask_of(hdev->cpu));
433 enable_irq(hdev->irq);
440 static int hpet_next_event(unsigned long delta,
441 struct clock_event_device *evt, int timer)
446 cnt = hpet_read_value(HPET_COUNTER);
448 hpet_write_value(cnt, HPET_Tn_CMP(timer));
451 * HPETs are a complete disaster. The compare register is
452 * based on a equal comparison and neither provides a less
453 * than or equal functionality (which would require to take
454 * the wraparound into account) nor a simple count down event
455 * mode. Further the write to the comparator register is
456 * delayed internally up to two HPET clock cycles in certain
457 * chipsets (ATI, ICH9,10). Some newer AMD chipsets have even
458 * longer delays. We worked around that by reading back the
459 * compare register, but that required another workaround for
460 * ICH9,10 chips where the first readout after write can
461 * return the old stale value. We already had a minimum
462 * programming delta of 5us enforced, but a NMI or SMI hitting
463 * between the counter readout and the comparator write can
464 * move us behind that point easily. Now instead of reading
465 * the compare register back several times, we make the ETIME
466 * decision based on the following: Return ETIME if the
467 * counter value after the write is less than HPET_MIN_CYCLES
468 * away from the event or if the counter is already ahead of
469 * the event. The minimum programming delta for the generic
470 * clockevents code is set to 1.5 * HPET_MIN_CYCLES.
472 res = (s32)((u32)cnt - (u32)hpet_readl(HPET_COUNTER));
474 return res < HPET_MIN_CYCLES ? -ETIME : 0;
477 static void hpet_legacy_set_mode(enum clock_event_mode mode,
478 struct clock_event_device *evt)
480 hpet_set_mode(mode, evt, 0);
483 static int hpet_legacy_next_event(unsigned long delta,
484 struct clock_event_device *evt)
486 return hpet_next_event(delta, evt, 0);
492 #ifdef CONFIG_PCI_MSI
494 static DEFINE_PER_CPU(struct hpet_dev *, cpu_hpet_dev);
495 static struct hpet_dev *hpet_devs;
497 void hpet_msi_unmask(struct irq_data *data)
499 struct hpet_dev *hdev = data->handler_data;
503 cfg = hpet_readl(HPET_Tn_CFG(hdev->num));
505 hpet_writel(cfg, HPET_Tn_CFG(hdev->num));
508 void hpet_msi_mask(struct irq_data *data)
510 struct hpet_dev *hdev = data->handler_data;
514 cfg = hpet_readl(HPET_Tn_CFG(hdev->num));
516 hpet_writel(cfg, HPET_Tn_CFG(hdev->num));
519 void hpet_msi_write(struct hpet_dev *hdev, struct msi_msg *msg)
521 hpet_writel(msg->data, HPET_Tn_ROUTE(hdev->num));
522 hpet_writel(msg->address_lo, HPET_Tn_ROUTE(hdev->num) + 4);
525 void hpet_msi_read(struct hpet_dev *hdev, struct msi_msg *msg)
527 msg->data = hpet_readl(HPET_Tn_ROUTE(hdev->num));
528 msg->address_lo = hpet_readl(HPET_Tn_ROUTE(hdev->num) + 4);
532 static void hpet_msi_set_mode(enum clock_event_mode mode,
533 struct clock_event_device *evt)
535 struct hpet_dev *hdev = EVT_TO_HPET_DEV(evt);
536 hpet_set_mode(mode, evt, hdev->num);
539 static int hpet_msi_next_event(unsigned long delta,
540 struct clock_event_device *evt)
542 struct hpet_dev *hdev = EVT_TO_HPET_DEV(evt);
543 return hpet_next_event(delta, evt, hdev->num);
546 static int hpet_setup_msi_irq(unsigned int irq)
548 if (arch_setup_hpet_msi(irq, hpet_blockid)) {
555 static int hpet_assign_irq(struct hpet_dev *dev)
559 irq = create_irq_nr(0, -1);
563 irq_set_handler_data(irq, dev);
565 if (hpet_setup_msi_irq(irq))
572 static irqreturn_t hpet_interrupt_handler(int irq, void *data)
574 struct hpet_dev *dev = (struct hpet_dev *)data;
575 struct clock_event_device *hevt = &dev->evt;
577 if (!hevt->event_handler) {
578 printk(KERN_INFO "Spurious HPET timer interrupt on HPET timer %d\n",
583 hevt->event_handler(hevt);
587 static int hpet_setup_irq(struct hpet_dev *dev)
590 if (request_irq(dev->irq, hpet_interrupt_handler,
591 IRQF_TIMER | IRQF_DISABLED | IRQF_NOBALANCING,
595 disable_irq(dev->irq);
596 irq_set_affinity(dev->irq, cpumask_of(dev->cpu));
597 enable_irq(dev->irq);
599 printk(KERN_DEBUG "hpet: %s irq %d for MSI\n",
600 dev->name, dev->irq);
605 /* This should be called in specific @cpu */
606 static void init_one_hpet_msi_clockevent(struct hpet_dev *hdev, int cpu)
608 struct clock_event_device *evt = &hdev->evt;
610 WARN_ON(cpu != smp_processor_id());
611 if (!(hdev->flags & HPET_DEV_VALID))
614 if (hpet_setup_msi_irq(hdev->irq))
618 per_cpu(cpu_hpet_dev, cpu) = hdev;
619 evt->name = hdev->name;
620 hpet_setup_irq(hdev);
621 evt->irq = hdev->irq;
624 evt->features = CLOCK_EVT_FEAT_ONESHOT;
625 if (hdev->flags & HPET_DEV_PERI_CAP)
626 evt->features |= CLOCK_EVT_FEAT_PERIODIC;
628 evt->set_mode = hpet_msi_set_mode;
629 evt->set_next_event = hpet_msi_next_event;
630 evt->cpumask = cpumask_of(hdev->cpu);
632 clockevents_config_and_register(evt, hpet_freq, HPET_MIN_PROG_DELTA,
637 /* Reserve at least one timer for userspace (/dev/hpet) */
638 #define RESERVE_TIMERS 1
640 #define RESERVE_TIMERS 0
643 static void hpet_msi_capability_lookup(unsigned int start_timer)
646 unsigned int num_timers;
647 unsigned int num_timers_used = 0;
650 if (hpet_msi_disable)
653 if (boot_cpu_has(X86_FEATURE_ARAT))
655 id = hpet_readl(HPET_ID);
657 num_timers = ((id & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT);
658 num_timers++; /* Value read out starts from 0 */
661 hpet_devs = kzalloc(sizeof(struct hpet_dev) * num_timers, GFP_KERNEL);
665 hpet_num_timers = num_timers;
667 for (i = start_timer; i < num_timers - RESERVE_TIMERS; i++) {
668 struct hpet_dev *hdev = &hpet_devs[num_timers_used];
669 unsigned int cfg = hpet_readl(HPET_Tn_CFG(i));
671 /* Only consider HPET timer with MSI support */
672 if (!(cfg & HPET_TN_FSB_CAP))
676 if (cfg & HPET_TN_PERIODIC_CAP)
677 hdev->flags |= HPET_DEV_PERI_CAP;
680 sprintf(hdev->name, "hpet%d", i);
681 if (hpet_assign_irq(hdev))
684 hdev->flags |= HPET_DEV_FSB_CAP;
685 hdev->flags |= HPET_DEV_VALID;
687 if (num_timers_used == num_possible_cpus())
691 printk(KERN_INFO "HPET: %d timers in total, %d timers will be used for per-cpu timer\n",
692 num_timers, num_timers_used);
696 static void hpet_reserve_msi_timers(struct hpet_data *hd)
703 for (i = 0; i < hpet_num_timers; i++) {
704 struct hpet_dev *hdev = &hpet_devs[i];
706 if (!(hdev->flags & HPET_DEV_VALID))
709 hd->hd_irq[hdev->num] = hdev->irq;
710 hpet_reserve_timer(hd, hdev->num);
715 static struct hpet_dev *hpet_get_unused_timer(void)
722 for (i = 0; i < hpet_num_timers; i++) {
723 struct hpet_dev *hdev = &hpet_devs[i];
725 if (!(hdev->flags & HPET_DEV_VALID))
727 if (test_and_set_bit(HPET_DEV_USED_BIT,
728 (unsigned long *)&hdev->flags))
735 struct hpet_work_struct {
736 struct delayed_work work;
737 struct completion complete;
740 static void hpet_work(struct work_struct *w)
742 struct hpet_dev *hdev;
743 int cpu = smp_processor_id();
744 struct hpet_work_struct *hpet_work;
746 hpet_work = container_of(w, struct hpet_work_struct, work.work);
748 hdev = hpet_get_unused_timer();
750 init_one_hpet_msi_clockevent(hdev, cpu);
752 complete(&hpet_work->complete);
755 static int hpet_cpuhp_notify(struct notifier_block *n,
756 unsigned long action, void *hcpu)
758 unsigned long cpu = (unsigned long)hcpu;
759 struct hpet_work_struct work;
760 struct hpet_dev *hdev = per_cpu(cpu_hpet_dev, cpu);
762 switch (action & 0xf) {
764 INIT_DELAYED_WORK_ONSTACK(&work.work, hpet_work);
765 init_completion(&work.complete);
766 /* FIXME: add schedule_work_on() */
767 schedule_delayed_work_on(cpu, &work.work, 0);
768 wait_for_completion(&work.complete);
769 destroy_timer_on_stack(&work.work.timer);
773 free_irq(hdev->irq, hdev);
774 hdev->flags &= ~HPET_DEV_USED;
775 per_cpu(cpu_hpet_dev, cpu) = NULL;
783 static int hpet_setup_msi_irq(unsigned int irq)
787 static void hpet_msi_capability_lookup(unsigned int start_timer)
793 static void hpet_reserve_msi_timers(struct hpet_data *hd)
799 static int hpet_cpuhp_notify(struct notifier_block *n,
800 unsigned long action, void *hcpu)
808 * Clock source related code
810 static cycle_t read_hpet(struct clocksource *cs)
812 return (cycle_t)hpet_readl(HPET_COUNTER);
815 static struct clocksource clocksource_hpet = {
820 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
821 .resume = hpet_resume_counter,
823 .archdata = { .vclock_mode = VCLOCK_HPET },
827 static int hpet_clocksource_register(void)
832 /* Start the counter */
833 hpet_restart_counter();
835 /* Verify whether hpet counter works */
836 t1 = hpet_readl(HPET_COUNTER);
840 * We don't know the TSC frequency yet, but waiting for
841 * 200000 TSC cycles is safe:
848 } while ((now - start) < 200000UL);
850 if (t1 == hpet_readl(HPET_COUNTER)) {
852 "HPET counter not counting. HPET disabled\n");
856 clocksource_register_hz(&clocksource_hpet, (u32)hpet_freq);
861 * hpet_enable - Try to setup the HPET timer. Returns 1 on success.
863 int __init hpet_enable(void)
865 unsigned long hpet_period;
870 if (!is_hpet_capable())
876 * Read the period and check for a sane value:
878 hpet_period = hpet_readl(HPET_PERIOD);
881 * AMD SB700 based systems with spread spectrum enabled use a
882 * SMM based HPET emulation to provide proper frequency
883 * setting. The SMM code is initialized with the first HPET
884 * register access and takes some time to complete. During
885 * this time the config register reads 0xffffffff. We check
886 * for max. 1000 loops whether the config register reads a non
887 * 0xffffffff value to make sure that HPET is up and running
888 * before we go further. A counting loop is safe, as the HPET
889 * access takes thousands of CPU cycles. On non SB700 based
890 * machines this check is only done once and has no side
893 for (i = 0; hpet_readl(HPET_CFG) == 0xFFFFFFFF; i++) {
896 "HPET config register value = 0xFFFFFFFF. "
902 if (hpet_period < HPET_MIN_PERIOD || hpet_period > HPET_MAX_PERIOD)
906 * The period is a femto seconds value. Convert it to a
910 do_div(freq, hpet_period);
914 * Read the HPET ID register to retrieve the IRQ routing
915 * information and the number of channels
917 id = hpet_readl(HPET_ID);
920 #ifdef CONFIG_HPET_EMULATE_RTC
922 * The legacy routing mode needs at least two channels, tick timer
923 * and the rtc emulation channel.
925 if (!(id & HPET_ID_NUMBER))
929 if (hpet_clocksource_register())
932 if (id & HPET_ID_LEGSUP) {
933 hpet_legacy_clockevent_register();
939 hpet_clear_mapping();
945 * Needs to be late, as the reserve_timer code calls kalloc !
947 * Not a problem on i386 as hpet_enable is called from late_time_init,
948 * but on x86_64 it is necessary !
950 static __init int hpet_late_init(void)
954 if (boot_hpet_disable)
958 if (!force_hpet_address)
961 hpet_address = force_hpet_address;
965 if (!hpet_virt_address)
968 if (hpet_readl(HPET_ID) & HPET_ID_LEGSUP)
969 hpet_msi_capability_lookup(2);
971 hpet_msi_capability_lookup(0);
973 hpet_reserve_platform_timers(hpet_readl(HPET_ID));
976 if (hpet_msi_disable)
979 if (boot_cpu_has(X86_FEATURE_ARAT))
982 for_each_online_cpu(cpu) {
983 hpet_cpuhp_notify(NULL, CPU_ONLINE, (void *)(long)cpu);
986 /* This notifier should be called after workqueue is ready */
987 hotcpu_notifier(hpet_cpuhp_notify, -20);
991 fs_initcall(hpet_late_init);
993 void hpet_disable(void)
995 if (is_hpet_capable() && hpet_virt_address) {
996 unsigned int cfg = hpet_readl(HPET_CFG);
998 if (hpet_legacy_int_enabled) {
999 cfg &= ~HPET_CFG_LEGACY;
1000 hpet_legacy_int_enabled = 0;
1002 cfg &= ~HPET_CFG_ENABLE;
1003 hpet_writel(cfg, HPET_CFG);
1007 #ifdef CONFIG_HPET_EMULATE_RTC
1009 /* HPET in LegacyReplacement Mode eats up RTC interrupt line. When, HPET
1010 * is enabled, we support RTC interrupt functionality in software.
1011 * RTC has 3 kinds of interrupts:
1012 * 1) Update Interrupt - generate an interrupt, every sec, when RTC clock
1014 * 2) Alarm Interrupt - generate an interrupt at a specific time of day
1015 * 3) Periodic Interrupt - generate periodic interrupt, with frequencies
1016 * 2Hz-8192Hz (2Hz-64Hz for non-root user) (all freqs in powers of 2)
1017 * (1) and (2) above are implemented using polling at a frequency of
1018 * 64 Hz. The exact frequency is a tradeoff between accuracy and interrupt
1019 * overhead. (DEFAULT_RTC_INT_FREQ)
1020 * For (3), we use interrupts at 64Hz or user specified periodic
1021 * frequency, whichever is higher.
1023 #include <linux/mc146818rtc.h>
1024 #include <linux/rtc.h>
1025 #include <asm/rtc.h>
1027 #define DEFAULT_RTC_INT_FREQ 64
1028 #define DEFAULT_RTC_SHIFT 6
1029 #define RTC_NUM_INTS 1
1031 static unsigned long hpet_rtc_flags;
1032 static int hpet_prev_update_sec;
1033 static struct rtc_time hpet_alarm_time;
1034 static unsigned long hpet_pie_count;
1035 static u32 hpet_t1_cmp;
1036 static u32 hpet_default_delta;
1037 static u32 hpet_pie_delta;
1038 static unsigned long hpet_pie_limit;
1040 static rtc_irq_handler irq_handler;
1043 * Check that the hpet counter c1 is ahead of the c2
1045 static inline int hpet_cnt_ahead(u32 c1, u32 c2)
1047 return (s32)(c2 - c1) < 0;
1051 * Registers a IRQ handler.
1053 int hpet_register_irq_handler(rtc_irq_handler handler)
1055 if (!is_hpet_enabled())
1060 irq_handler = handler;
1064 EXPORT_SYMBOL_GPL(hpet_register_irq_handler);
1067 * Deregisters the IRQ handler registered with hpet_register_irq_handler()
1070 void hpet_unregister_irq_handler(rtc_irq_handler handler)
1072 if (!is_hpet_enabled())
1078 EXPORT_SYMBOL_GPL(hpet_unregister_irq_handler);
1081 * Timer 1 for RTC emulation. We use one shot mode, as periodic mode
1082 * is not supported by all HPET implementations for timer 1.
1084 * hpet_rtc_timer_init() is called when the rtc is initialized.
1086 int hpet_rtc_timer_init(void)
1088 unsigned int cfg, cnt, delta;
1089 unsigned long flags;
1091 if (!is_hpet_enabled())
1094 if (!hpet_default_delta) {
1097 clc = (uint64_t) hpet_clockevent.mult * NSEC_PER_SEC;
1098 clc >>= hpet_clockevent.shift + DEFAULT_RTC_SHIFT;
1099 hpet_default_delta = clc;
1102 if (!(hpet_rtc_flags & RTC_PIE) || hpet_pie_limit)
1103 delta = hpet_default_delta;
1105 delta = hpet_pie_delta;
1107 local_irq_save(flags);
1109 cnt = delta + hpet_readl(HPET_COUNTER);
1110 hpet_writel(cnt, HPET_T1_CMP);
1113 cfg = hpet_readl(HPET_T1_CFG);
1114 cfg &= ~HPET_TN_PERIODIC;
1115 cfg |= HPET_TN_ENABLE | HPET_TN_32BIT;
1116 hpet_writel(cfg, HPET_T1_CFG);
1118 local_irq_restore(flags);
1122 EXPORT_SYMBOL_GPL(hpet_rtc_timer_init);
1124 static void hpet_disable_rtc_channel(void)
1127 cfg = hpet_readl(HPET_T1_CFG);
1128 cfg &= ~HPET_TN_ENABLE;
1129 hpet_writel(cfg, HPET_T1_CFG);
1133 * The functions below are called from rtc driver.
1134 * Return 0 if HPET is not being used.
1135 * Otherwise do the necessary changes and return 1.
1137 int hpet_mask_rtc_irq_bit(unsigned long bit_mask)
1139 if (!is_hpet_enabled())
1142 hpet_rtc_flags &= ~bit_mask;
1143 if (unlikely(!hpet_rtc_flags))
1144 hpet_disable_rtc_channel();
1148 EXPORT_SYMBOL_GPL(hpet_mask_rtc_irq_bit);
1150 int hpet_set_rtc_irq_bit(unsigned long bit_mask)
1152 unsigned long oldbits = hpet_rtc_flags;
1154 if (!is_hpet_enabled())
1157 hpet_rtc_flags |= bit_mask;
1159 if ((bit_mask & RTC_UIE) && !(oldbits & RTC_UIE))
1160 hpet_prev_update_sec = -1;
1163 hpet_rtc_timer_init();
1167 EXPORT_SYMBOL_GPL(hpet_set_rtc_irq_bit);
1169 int hpet_set_alarm_time(unsigned char hrs, unsigned char min,
1172 if (!is_hpet_enabled())
1175 hpet_alarm_time.tm_hour = hrs;
1176 hpet_alarm_time.tm_min = min;
1177 hpet_alarm_time.tm_sec = sec;
1181 EXPORT_SYMBOL_GPL(hpet_set_alarm_time);
1183 int hpet_set_periodic_freq(unsigned long freq)
1187 if (!is_hpet_enabled())
1190 if (freq <= DEFAULT_RTC_INT_FREQ)
1191 hpet_pie_limit = DEFAULT_RTC_INT_FREQ / freq;
1193 clc = (uint64_t) hpet_clockevent.mult * NSEC_PER_SEC;
1195 clc >>= hpet_clockevent.shift;
1196 hpet_pie_delta = clc;
1201 EXPORT_SYMBOL_GPL(hpet_set_periodic_freq);
1203 int hpet_rtc_dropped_irq(void)
1205 return is_hpet_enabled();
1207 EXPORT_SYMBOL_GPL(hpet_rtc_dropped_irq);
1209 static void hpet_rtc_timer_reinit(void)
1214 if (unlikely(!hpet_rtc_flags))
1215 hpet_disable_rtc_channel();
1217 if (!(hpet_rtc_flags & RTC_PIE) || hpet_pie_limit)
1218 delta = hpet_default_delta;
1220 delta = hpet_pie_delta;
1223 * Increment the comparator value until we are ahead of the
1227 hpet_t1_cmp += delta;
1228 hpet_writel(hpet_t1_cmp, HPET_T1_CMP);
1230 } while (!hpet_cnt_ahead(hpet_t1_cmp, hpet_readl(HPET_COUNTER)));
1233 if (hpet_rtc_flags & RTC_PIE)
1234 hpet_pie_count += lost_ints;
1235 if (printk_ratelimit())
1236 printk(KERN_WARNING "hpet1: lost %d rtc interrupts\n",
1241 irqreturn_t hpet_rtc_interrupt(int irq, void *dev_id)
1243 struct rtc_time curr_time;
1244 unsigned long rtc_int_flag = 0;
1246 hpet_rtc_timer_reinit();
1247 memset(&curr_time, 0, sizeof(struct rtc_time));
1249 if (hpet_rtc_flags & (RTC_UIE | RTC_AIE))
1250 get_rtc_time(&curr_time);
1252 if (hpet_rtc_flags & RTC_UIE &&
1253 curr_time.tm_sec != hpet_prev_update_sec) {
1254 if (hpet_prev_update_sec >= 0)
1255 rtc_int_flag = RTC_UF;
1256 hpet_prev_update_sec = curr_time.tm_sec;
1259 if (hpet_rtc_flags & RTC_PIE &&
1260 ++hpet_pie_count >= hpet_pie_limit) {
1261 rtc_int_flag |= RTC_PF;
1265 if (hpet_rtc_flags & RTC_AIE &&
1266 (curr_time.tm_sec == hpet_alarm_time.tm_sec) &&
1267 (curr_time.tm_min == hpet_alarm_time.tm_min) &&
1268 (curr_time.tm_hour == hpet_alarm_time.tm_hour))
1269 rtc_int_flag |= RTC_AF;
1272 rtc_int_flag |= (RTC_IRQF | (RTC_NUM_INTS << 8));
1274 irq_handler(rtc_int_flag, dev_id);
1278 EXPORT_SYMBOL_GPL(hpet_rtc_interrupt);