x86, apic: Don't use logical-flat mode when CPU hotplug may exceed 8 CPUs
[linux-2.6/mini2440.git] / arch / x86 / kernel / hpet.c
blob58778736496a37170a63e56019a040267098d183
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 EVT_TO_HPET_DEV(evt) container_of(evt, struct hpet_dev, evt)
33 * HPET address is set in acpi/boot.c, when an ACPI entry exists
35 unsigned long hpet_address;
36 u8 hpet_msi_disable;
38 #ifdef CONFIG_PCI_MSI
39 static unsigned long hpet_num_timers;
40 #endif
41 static void __iomem *hpet_virt_address;
43 struct hpet_dev {
44 struct clock_event_device evt;
45 unsigned int num;
46 int cpu;
47 unsigned int irq;
48 unsigned int flags;
49 char name[10];
52 unsigned long hpet_readl(unsigned long a)
54 return readl(hpet_virt_address + a);
57 static inline void hpet_writel(unsigned long d, unsigned long a)
59 writel(d, hpet_virt_address + a);
62 #ifdef CONFIG_X86_64
63 #include <asm/pgtable.h>
64 #endif
66 static inline void hpet_set_mapping(void)
68 hpet_virt_address = ioremap_nocache(hpet_address, HPET_MMAP_SIZE);
69 #ifdef CONFIG_X86_64
70 __set_fixmap(VSYSCALL_HPET, hpet_address, PAGE_KERNEL_VSYSCALL_NOCACHE);
71 #endif
74 static inline void hpet_clear_mapping(void)
76 iounmap(hpet_virt_address);
77 hpet_virt_address = NULL;
81 * HPET command line enable / disable
83 static int boot_hpet_disable;
84 int hpet_force_user;
85 static int hpet_verbose;
87 static int __init hpet_setup(char *str)
89 if (str) {
90 if (!strncmp("disable", str, 7))
91 boot_hpet_disable = 1;
92 if (!strncmp("force", str, 5))
93 hpet_force_user = 1;
94 if (!strncmp("verbose", str, 7))
95 hpet_verbose = 1;
97 return 1;
99 __setup("hpet=", hpet_setup);
101 static int __init disable_hpet(char *str)
103 boot_hpet_disable = 1;
104 return 1;
106 __setup("nohpet", disable_hpet);
108 static inline int is_hpet_capable(void)
110 return !boot_hpet_disable && hpet_address;
114 * HPET timer interrupt enable / disable
116 static int hpet_legacy_int_enabled;
119 * is_hpet_enabled - check whether the hpet timer interrupt is enabled
121 int is_hpet_enabled(void)
123 return is_hpet_capable() && hpet_legacy_int_enabled;
125 EXPORT_SYMBOL_GPL(is_hpet_enabled);
127 static void _hpet_print_config(const char *function, int line)
129 u32 i, timers, l, h;
130 printk(KERN_INFO "hpet: %s(%d):\n", function, line);
131 l = hpet_readl(HPET_ID);
132 h = hpet_readl(HPET_PERIOD);
133 timers = ((l & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT) + 1;
134 printk(KERN_INFO "hpet: ID: 0x%x, PERIOD: 0x%x\n", l, h);
135 l = hpet_readl(HPET_CFG);
136 h = hpet_readl(HPET_STATUS);
137 printk(KERN_INFO "hpet: CFG: 0x%x, STATUS: 0x%x\n", l, h);
138 l = hpet_readl(HPET_COUNTER);
139 h = hpet_readl(HPET_COUNTER+4);
140 printk(KERN_INFO "hpet: COUNTER_l: 0x%x, COUNTER_h: 0x%x\n", l, h);
142 for (i = 0; i < timers; i++) {
143 l = hpet_readl(HPET_Tn_CFG(i));
144 h = hpet_readl(HPET_Tn_CFG(i)+4);
145 printk(KERN_INFO "hpet: T%d: CFG_l: 0x%x, CFG_h: 0x%x\n",
146 i, l, h);
147 l = hpet_readl(HPET_Tn_CMP(i));
148 h = hpet_readl(HPET_Tn_CMP(i)+4);
149 printk(KERN_INFO "hpet: T%d: CMP_l: 0x%x, CMP_h: 0x%x\n",
150 i, l, h);
151 l = hpet_readl(HPET_Tn_ROUTE(i));
152 h = hpet_readl(HPET_Tn_ROUTE(i)+4);
153 printk(KERN_INFO "hpet: T%d ROUTE_l: 0x%x, ROUTE_h: 0x%x\n",
154 i, l, h);
158 #define hpet_print_config() \
159 do { \
160 if (hpet_verbose) \
161 _hpet_print_config(__FUNCTION__, __LINE__); \
162 } while (0)
165 * When the hpet driver (/dev/hpet) is enabled, we need to reserve
166 * timer 0 and timer 1 in case of RTC emulation.
168 #ifdef CONFIG_HPET
170 static void hpet_reserve_msi_timers(struct hpet_data *hd);
172 static void hpet_reserve_platform_timers(unsigned long id)
174 struct hpet __iomem *hpet = hpet_virt_address;
175 struct hpet_timer __iomem *timer = &hpet->hpet_timers[2];
176 unsigned int nrtimers, i;
177 struct hpet_data hd;
179 nrtimers = ((id & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT) + 1;
181 memset(&hd, 0, sizeof(hd));
182 hd.hd_phys_address = hpet_address;
183 hd.hd_address = hpet;
184 hd.hd_nirqs = nrtimers;
185 hpet_reserve_timer(&hd, 0);
187 #ifdef CONFIG_HPET_EMULATE_RTC
188 hpet_reserve_timer(&hd, 1);
189 #endif
192 * NOTE that hd_irq[] reflects IOAPIC input pins (LEGACY_8254
193 * is wrong for i8259!) not the output IRQ. Many BIOS writers
194 * don't bother configuring *any* comparator interrupts.
196 hd.hd_irq[0] = HPET_LEGACY_8254;
197 hd.hd_irq[1] = HPET_LEGACY_RTC;
199 for (i = 2; i < nrtimers; timer++, i++) {
200 hd.hd_irq[i] = (readl(&timer->hpet_config) &
201 Tn_INT_ROUTE_CNF_MASK) >> Tn_INT_ROUTE_CNF_SHIFT;
204 hpet_reserve_msi_timers(&hd);
206 hpet_alloc(&hd);
209 #else
210 static void hpet_reserve_platform_timers(unsigned long id) { }
211 #endif
214 * Common hpet info
216 static unsigned long hpet_period;
218 static void hpet_legacy_set_mode(enum clock_event_mode mode,
219 struct clock_event_device *evt);
220 static int hpet_legacy_next_event(unsigned long delta,
221 struct clock_event_device *evt);
224 * The hpet clock event device
226 static struct clock_event_device hpet_clockevent = {
227 .name = "hpet",
228 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
229 .set_mode = hpet_legacy_set_mode,
230 .set_next_event = hpet_legacy_next_event,
231 .shift = 32,
232 .irq = 0,
233 .rating = 50,
236 static void hpet_stop_counter(void)
238 unsigned long cfg = hpet_readl(HPET_CFG);
239 cfg &= ~HPET_CFG_ENABLE;
240 hpet_writel(cfg, HPET_CFG);
243 static void hpet_reset_counter(void)
245 hpet_writel(0, HPET_COUNTER);
246 hpet_writel(0, HPET_COUNTER + 4);
249 static void hpet_start_counter(void)
251 unsigned long cfg = hpet_readl(HPET_CFG);
252 cfg |= HPET_CFG_ENABLE;
253 hpet_writel(cfg, HPET_CFG);
256 static void hpet_restart_counter(void)
258 hpet_stop_counter();
259 hpet_reset_counter();
260 hpet_start_counter();
263 static void hpet_resume_device(void)
265 force_hpet_resume();
268 static void hpet_resume_counter(void)
270 hpet_resume_device();
271 hpet_restart_counter();
274 static void hpet_enable_legacy_int(void)
276 unsigned long cfg = hpet_readl(HPET_CFG);
278 cfg |= HPET_CFG_LEGACY;
279 hpet_writel(cfg, HPET_CFG);
280 hpet_legacy_int_enabled = 1;
283 static void hpet_legacy_clockevent_register(void)
285 /* Start HPET legacy interrupts */
286 hpet_enable_legacy_int();
289 * The mult factor is defined as (include/linux/clockchips.h)
290 * mult/2^shift = cyc/ns (in contrast to ns/cyc in clocksource.h)
291 * hpet_period is in units of femtoseconds (per cycle), so
292 * mult/2^shift = cyc/ns = 10^6/hpet_period
293 * mult = (10^6 * 2^shift)/hpet_period
294 * mult = (FSEC_PER_NSEC << hpet_clockevent.shift)/hpet_period
296 hpet_clockevent.mult = div_sc((unsigned long) FSEC_PER_NSEC,
297 hpet_period, hpet_clockevent.shift);
298 /* Calculate the min / max delta */
299 hpet_clockevent.max_delta_ns = clockevent_delta2ns(0x7FFFFFFF,
300 &hpet_clockevent);
301 /* 5 usec minimum reprogramming delta. */
302 hpet_clockevent.min_delta_ns = 5000;
305 * Start hpet with the boot cpu mask and make it
306 * global after the IO_APIC has been initialized.
308 hpet_clockevent.cpumask = cpumask_of(smp_processor_id());
309 clockevents_register_device(&hpet_clockevent);
310 global_clock_event = &hpet_clockevent;
311 printk(KERN_DEBUG "hpet clockevent registered\n");
314 static int hpet_setup_msi_irq(unsigned int irq);
316 static void hpet_set_mode(enum clock_event_mode mode,
317 struct clock_event_device *evt, int timer)
319 unsigned long cfg, cmp, now;
320 uint64_t delta;
322 switch (mode) {
323 case CLOCK_EVT_MODE_PERIODIC:
324 hpet_stop_counter();
325 delta = ((uint64_t)(NSEC_PER_SEC/HZ)) * evt->mult;
326 delta >>= evt->shift;
327 now = hpet_readl(HPET_COUNTER);
328 cmp = now + (unsigned long) delta;
329 cfg = hpet_readl(HPET_Tn_CFG(timer));
330 /* Make sure we use edge triggered interrupts */
331 cfg &= ~HPET_TN_LEVEL;
332 cfg |= HPET_TN_ENABLE | HPET_TN_PERIODIC |
333 HPET_TN_SETVAL | HPET_TN_32BIT;
334 hpet_writel(cfg, HPET_Tn_CFG(timer));
335 hpet_writel(cmp, HPET_Tn_CMP(timer));
336 udelay(1);
338 * HPET on AMD 81xx needs a second write (with HPET_TN_SETVAL
339 * cleared) to T0_CMP to set the period. The HPET_TN_SETVAL
340 * bit is automatically cleared after the first write.
341 * (See AMD-8111 HyperTransport I/O Hub Data Sheet,
342 * Publication # 24674)
344 hpet_writel((unsigned long) delta, HPET_Tn_CMP(timer));
345 hpet_start_counter();
346 hpet_print_config();
347 break;
349 case CLOCK_EVT_MODE_ONESHOT:
350 cfg = hpet_readl(HPET_Tn_CFG(timer));
351 cfg &= ~HPET_TN_PERIODIC;
352 cfg |= HPET_TN_ENABLE | HPET_TN_32BIT;
353 hpet_writel(cfg, HPET_Tn_CFG(timer));
354 break;
356 case CLOCK_EVT_MODE_UNUSED:
357 case CLOCK_EVT_MODE_SHUTDOWN:
358 cfg = hpet_readl(HPET_Tn_CFG(timer));
359 cfg &= ~HPET_TN_ENABLE;
360 hpet_writel(cfg, HPET_Tn_CFG(timer));
361 break;
363 case CLOCK_EVT_MODE_RESUME:
364 if (timer == 0) {
365 hpet_enable_legacy_int();
366 } else {
367 struct hpet_dev *hdev = EVT_TO_HPET_DEV(evt);
368 hpet_setup_msi_irq(hdev->irq);
369 disable_irq(hdev->irq);
370 irq_set_affinity(hdev->irq, cpumask_of(hdev->cpu));
371 enable_irq(hdev->irq);
373 hpet_print_config();
374 break;
378 static int hpet_next_event(unsigned long delta,
379 struct clock_event_device *evt, int timer)
381 u32 cnt;
383 cnt = hpet_readl(HPET_COUNTER);
384 cnt += (u32) delta;
385 hpet_writel(cnt, HPET_Tn_CMP(timer));
388 * We need to read back the CMP register to make sure that
389 * what we wrote hit the chip before we compare it to the
390 * counter.
392 WARN_ON_ONCE((u32)hpet_readl(HPET_Tn_CMP(timer)) != cnt);
394 return (s32)((u32)hpet_readl(HPET_COUNTER) - cnt) >= 0 ? -ETIME : 0;
397 static void hpet_legacy_set_mode(enum clock_event_mode mode,
398 struct clock_event_device *evt)
400 hpet_set_mode(mode, evt, 0);
403 static int hpet_legacy_next_event(unsigned long delta,
404 struct clock_event_device *evt)
406 return hpet_next_event(delta, evt, 0);
410 * HPET MSI Support
412 #ifdef CONFIG_PCI_MSI
414 static DEFINE_PER_CPU(struct hpet_dev *, cpu_hpet_dev);
415 static struct hpet_dev *hpet_devs;
417 void hpet_msi_unmask(unsigned int irq)
419 struct hpet_dev *hdev = get_irq_data(irq);
420 unsigned long cfg;
422 /* unmask it */
423 cfg = hpet_readl(HPET_Tn_CFG(hdev->num));
424 cfg |= HPET_TN_FSB;
425 hpet_writel(cfg, HPET_Tn_CFG(hdev->num));
428 void hpet_msi_mask(unsigned int irq)
430 unsigned long cfg;
431 struct hpet_dev *hdev = get_irq_data(irq);
433 /* mask it */
434 cfg = hpet_readl(HPET_Tn_CFG(hdev->num));
435 cfg &= ~HPET_TN_FSB;
436 hpet_writel(cfg, HPET_Tn_CFG(hdev->num));
439 void hpet_msi_write(unsigned int irq, struct msi_msg *msg)
441 struct hpet_dev *hdev = get_irq_data(irq);
443 hpet_writel(msg->data, HPET_Tn_ROUTE(hdev->num));
444 hpet_writel(msg->address_lo, HPET_Tn_ROUTE(hdev->num) + 4);
447 void hpet_msi_read(unsigned int irq, struct msi_msg *msg)
449 struct hpet_dev *hdev = get_irq_data(irq);
451 msg->data = hpet_readl(HPET_Tn_ROUTE(hdev->num));
452 msg->address_lo = hpet_readl(HPET_Tn_ROUTE(hdev->num) + 4);
453 msg->address_hi = 0;
456 static void hpet_msi_set_mode(enum clock_event_mode mode,
457 struct clock_event_device *evt)
459 struct hpet_dev *hdev = EVT_TO_HPET_DEV(evt);
460 hpet_set_mode(mode, evt, hdev->num);
463 static int hpet_msi_next_event(unsigned long delta,
464 struct clock_event_device *evt)
466 struct hpet_dev *hdev = EVT_TO_HPET_DEV(evt);
467 return hpet_next_event(delta, evt, hdev->num);
470 static int hpet_setup_msi_irq(unsigned int irq)
472 if (arch_setup_hpet_msi(irq)) {
473 destroy_irq(irq);
474 return -EINVAL;
476 return 0;
479 static int hpet_assign_irq(struct hpet_dev *dev)
481 unsigned int irq;
483 irq = create_irq();
484 if (!irq)
485 return -EINVAL;
487 set_irq_data(irq, dev);
489 if (hpet_setup_msi_irq(irq))
490 return -EINVAL;
492 dev->irq = irq;
493 return 0;
496 static irqreturn_t hpet_interrupt_handler(int irq, void *data)
498 struct hpet_dev *dev = (struct hpet_dev *)data;
499 struct clock_event_device *hevt = &dev->evt;
501 if (!hevt->event_handler) {
502 printk(KERN_INFO "Spurious HPET timer interrupt on HPET timer %d\n",
503 dev->num);
504 return IRQ_HANDLED;
507 hevt->event_handler(hevt);
508 return IRQ_HANDLED;
511 static int hpet_setup_irq(struct hpet_dev *dev)
514 if (request_irq(dev->irq, hpet_interrupt_handler,
515 IRQF_TIMER | IRQF_DISABLED | IRQF_NOBALANCING,
516 dev->name, dev))
517 return -1;
519 disable_irq(dev->irq);
520 irq_set_affinity(dev->irq, cpumask_of(dev->cpu));
521 enable_irq(dev->irq);
523 printk(KERN_DEBUG "hpet: %s irq %d for MSI\n",
524 dev->name, dev->irq);
526 return 0;
529 /* This should be called in specific @cpu */
530 static void init_one_hpet_msi_clockevent(struct hpet_dev *hdev, int cpu)
532 struct clock_event_device *evt = &hdev->evt;
533 uint64_t hpet_freq;
535 WARN_ON(cpu != smp_processor_id());
536 if (!(hdev->flags & HPET_DEV_VALID))
537 return;
539 if (hpet_setup_msi_irq(hdev->irq))
540 return;
542 hdev->cpu = cpu;
543 per_cpu(cpu_hpet_dev, cpu) = hdev;
544 evt->name = hdev->name;
545 hpet_setup_irq(hdev);
546 evt->irq = hdev->irq;
548 evt->rating = 110;
549 evt->features = CLOCK_EVT_FEAT_ONESHOT;
550 if (hdev->flags & HPET_DEV_PERI_CAP)
551 evt->features |= CLOCK_EVT_FEAT_PERIODIC;
553 evt->set_mode = hpet_msi_set_mode;
554 evt->set_next_event = hpet_msi_next_event;
555 evt->shift = 32;
558 * The period is a femto seconds value. We need to calculate the
559 * scaled math multiplication factor for nanosecond to hpet tick
560 * conversion.
562 hpet_freq = 1000000000000000ULL;
563 do_div(hpet_freq, hpet_period);
564 evt->mult = div_sc((unsigned long) hpet_freq,
565 NSEC_PER_SEC, evt->shift);
566 /* Calculate the max delta */
567 evt->max_delta_ns = clockevent_delta2ns(0x7FFFFFFF, evt);
568 /* 5 usec minimum reprogramming delta. */
569 evt->min_delta_ns = 5000;
571 evt->cpumask = cpumask_of(hdev->cpu);
572 clockevents_register_device(evt);
575 #ifdef CONFIG_HPET
576 /* Reserve at least one timer for userspace (/dev/hpet) */
577 #define RESERVE_TIMERS 1
578 #else
579 #define RESERVE_TIMERS 0
580 #endif
582 static void hpet_msi_capability_lookup(unsigned int start_timer)
584 unsigned int id;
585 unsigned int num_timers;
586 unsigned int num_timers_used = 0;
587 int i;
589 if (hpet_msi_disable)
590 return;
592 id = hpet_readl(HPET_ID);
594 num_timers = ((id & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT);
595 num_timers++; /* Value read out starts from 0 */
596 hpet_print_config();
598 hpet_devs = kzalloc(sizeof(struct hpet_dev) * num_timers, GFP_KERNEL);
599 if (!hpet_devs)
600 return;
602 hpet_num_timers = num_timers;
604 for (i = start_timer; i < num_timers - RESERVE_TIMERS; i++) {
605 struct hpet_dev *hdev = &hpet_devs[num_timers_used];
606 unsigned long cfg = hpet_readl(HPET_Tn_CFG(i));
608 /* Only consider HPET timer with MSI support */
609 if (!(cfg & HPET_TN_FSB_CAP))
610 continue;
612 hdev->flags = 0;
613 if (cfg & HPET_TN_PERIODIC_CAP)
614 hdev->flags |= HPET_DEV_PERI_CAP;
615 hdev->num = i;
617 sprintf(hdev->name, "hpet%d", i);
618 if (hpet_assign_irq(hdev))
619 continue;
621 hdev->flags |= HPET_DEV_FSB_CAP;
622 hdev->flags |= HPET_DEV_VALID;
623 num_timers_used++;
624 if (num_timers_used == num_possible_cpus())
625 break;
628 printk(KERN_INFO "HPET: %d timers in total, %d timers will be used for per-cpu timer\n",
629 num_timers, num_timers_used);
632 #ifdef CONFIG_HPET
633 static void hpet_reserve_msi_timers(struct hpet_data *hd)
635 int i;
637 if (!hpet_devs)
638 return;
640 for (i = 0; i < hpet_num_timers; i++) {
641 struct hpet_dev *hdev = &hpet_devs[i];
643 if (!(hdev->flags & HPET_DEV_VALID))
644 continue;
646 hd->hd_irq[hdev->num] = hdev->irq;
647 hpet_reserve_timer(hd, hdev->num);
650 #endif
652 static struct hpet_dev *hpet_get_unused_timer(void)
654 int i;
656 if (!hpet_devs)
657 return NULL;
659 for (i = 0; i < hpet_num_timers; i++) {
660 struct hpet_dev *hdev = &hpet_devs[i];
662 if (!(hdev->flags & HPET_DEV_VALID))
663 continue;
664 if (test_and_set_bit(HPET_DEV_USED_BIT,
665 (unsigned long *)&hdev->flags))
666 continue;
667 return hdev;
669 return NULL;
672 struct hpet_work_struct {
673 struct delayed_work work;
674 struct completion complete;
677 static void hpet_work(struct work_struct *w)
679 struct hpet_dev *hdev;
680 int cpu = smp_processor_id();
681 struct hpet_work_struct *hpet_work;
683 hpet_work = container_of(w, struct hpet_work_struct, work.work);
685 hdev = hpet_get_unused_timer();
686 if (hdev)
687 init_one_hpet_msi_clockevent(hdev, cpu);
689 complete(&hpet_work->complete);
692 static int hpet_cpuhp_notify(struct notifier_block *n,
693 unsigned long action, void *hcpu)
695 unsigned long cpu = (unsigned long)hcpu;
696 struct hpet_work_struct work;
697 struct hpet_dev *hdev = per_cpu(cpu_hpet_dev, cpu);
699 switch (action & 0xf) {
700 case CPU_ONLINE:
701 INIT_DELAYED_WORK_ON_STACK(&work.work, hpet_work);
702 init_completion(&work.complete);
703 /* FIXME: add schedule_work_on() */
704 schedule_delayed_work_on(cpu, &work.work, 0);
705 wait_for_completion(&work.complete);
706 destroy_timer_on_stack(&work.work.timer);
707 break;
708 case CPU_DEAD:
709 if (hdev) {
710 free_irq(hdev->irq, hdev);
711 hdev->flags &= ~HPET_DEV_USED;
712 per_cpu(cpu_hpet_dev, cpu) = NULL;
714 break;
716 return NOTIFY_OK;
718 #else
720 static int hpet_setup_msi_irq(unsigned int irq)
722 return 0;
724 static void hpet_msi_capability_lookup(unsigned int start_timer)
726 return;
729 #ifdef CONFIG_HPET
730 static void hpet_reserve_msi_timers(struct hpet_data *hd)
732 return;
734 #endif
736 static int hpet_cpuhp_notify(struct notifier_block *n,
737 unsigned long action, void *hcpu)
739 return NOTIFY_OK;
742 #endif
745 * Clock source related code
747 static cycle_t read_hpet(struct clocksource *cs)
749 return (cycle_t)hpet_readl(HPET_COUNTER);
752 #ifdef CONFIG_X86_64
753 static cycle_t __vsyscall_fn vread_hpet(void)
755 return readl((const void __iomem *)fix_to_virt(VSYSCALL_HPET) + 0xf0);
757 #endif
759 static struct clocksource clocksource_hpet = {
760 .name = "hpet",
761 .rating = 250,
762 .read = read_hpet,
763 .mask = HPET_MASK,
764 .shift = HPET_SHIFT,
765 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
766 .resume = hpet_resume_counter,
767 #ifdef CONFIG_X86_64
768 .vread = vread_hpet,
769 #endif
772 static int hpet_clocksource_register(void)
774 u64 start, now;
775 cycle_t t1;
777 /* Start the counter */
778 hpet_restart_counter();
780 /* Verify whether hpet counter works */
781 t1 = hpet_readl(HPET_COUNTER);
782 rdtscll(start);
785 * We don't know the TSC frequency yet, but waiting for
786 * 200000 TSC cycles is safe:
787 * 4 GHz == 50us
788 * 1 GHz == 200us
790 do {
791 rep_nop();
792 rdtscll(now);
793 } while ((now - start) < 200000UL);
795 if (t1 == hpet_readl(HPET_COUNTER)) {
796 printk(KERN_WARNING
797 "HPET counter not counting. HPET disabled\n");
798 return -ENODEV;
802 * The definition of mult is (include/linux/clocksource.h)
803 * mult/2^shift = ns/cyc and hpet_period is in units of fsec/cyc
804 * so we first need to convert hpet_period to ns/cyc units:
805 * mult/2^shift = ns/cyc = hpet_period/10^6
806 * mult = (hpet_period * 2^shift)/10^6
807 * mult = (hpet_period << shift)/FSEC_PER_NSEC
809 clocksource_hpet.mult = div_sc(hpet_period, FSEC_PER_NSEC, HPET_SHIFT);
811 clocksource_register(&clocksource_hpet);
813 return 0;
817 * hpet_enable - Try to setup the HPET timer. Returns 1 on success.
819 int __init hpet_enable(void)
821 unsigned long id;
822 int i;
824 if (!is_hpet_capable())
825 return 0;
827 hpet_set_mapping();
830 * Read the period and check for a sane value:
832 hpet_period = hpet_readl(HPET_PERIOD);
835 * AMD SB700 based systems with spread spectrum enabled use a
836 * SMM based HPET emulation to provide proper frequency
837 * setting. The SMM code is initialized with the first HPET
838 * register access and takes some time to complete. During
839 * this time the config register reads 0xffffffff. We check
840 * for max. 1000 loops whether the config register reads a non
841 * 0xffffffff value to make sure that HPET is up and running
842 * before we go further. A counting loop is safe, as the HPET
843 * access takes thousands of CPU cycles. On non SB700 based
844 * machines this check is only done once and has no side
845 * effects.
847 for (i = 0; hpet_readl(HPET_CFG) == 0xFFFFFFFF; i++) {
848 if (i == 1000) {
849 printk(KERN_WARNING
850 "HPET config register value = 0xFFFFFFFF. "
851 "Disabling HPET\n");
852 goto out_nohpet;
856 if (hpet_period < HPET_MIN_PERIOD || hpet_period > HPET_MAX_PERIOD)
857 goto out_nohpet;
860 * Read the HPET ID register to retrieve the IRQ routing
861 * information and the number of channels
863 id = hpet_readl(HPET_ID);
864 hpet_print_config();
866 #ifdef CONFIG_HPET_EMULATE_RTC
868 * The legacy routing mode needs at least two channels, tick timer
869 * and the rtc emulation channel.
871 if (!(id & HPET_ID_NUMBER))
872 goto out_nohpet;
873 #endif
875 if (hpet_clocksource_register())
876 goto out_nohpet;
878 if (id & HPET_ID_LEGSUP) {
879 hpet_legacy_clockevent_register();
880 hpet_msi_capability_lookup(2);
881 return 1;
883 hpet_msi_capability_lookup(0);
884 return 0;
886 out_nohpet:
887 hpet_clear_mapping();
888 hpet_address = 0;
889 return 0;
893 * Needs to be late, as the reserve_timer code calls kalloc !
895 * Not a problem on i386 as hpet_enable is called from late_time_init,
896 * but on x86_64 it is necessary !
898 static __init int hpet_late_init(void)
900 int cpu;
902 if (boot_hpet_disable)
903 return -ENODEV;
905 if (!hpet_address) {
906 if (!force_hpet_address)
907 return -ENODEV;
909 hpet_address = force_hpet_address;
910 hpet_enable();
913 if (!hpet_virt_address)
914 return -ENODEV;
916 hpet_reserve_platform_timers(hpet_readl(HPET_ID));
917 hpet_print_config();
919 if (hpet_msi_disable)
920 return 0;
922 for_each_online_cpu(cpu) {
923 hpet_cpuhp_notify(NULL, CPU_ONLINE, (void *)(long)cpu);
926 /* This notifier should be called after workqueue is ready */
927 hotcpu_notifier(hpet_cpuhp_notify, -20);
929 return 0;
931 fs_initcall(hpet_late_init);
933 void hpet_disable(void)
935 if (is_hpet_capable()) {
936 unsigned long cfg = hpet_readl(HPET_CFG);
938 if (hpet_legacy_int_enabled) {
939 cfg &= ~HPET_CFG_LEGACY;
940 hpet_legacy_int_enabled = 0;
942 cfg &= ~HPET_CFG_ENABLE;
943 hpet_writel(cfg, HPET_CFG);
947 #ifdef CONFIG_HPET_EMULATE_RTC
949 /* HPET in LegacyReplacement Mode eats up RTC interrupt line. When, HPET
950 * is enabled, we support RTC interrupt functionality in software.
951 * RTC has 3 kinds of interrupts:
952 * 1) Update Interrupt - generate an interrupt, every sec, when RTC clock
953 * is updated
954 * 2) Alarm Interrupt - generate an interrupt at a specific time of day
955 * 3) Periodic Interrupt - generate periodic interrupt, with frequencies
956 * 2Hz-8192Hz (2Hz-64Hz for non-root user) (all freqs in powers of 2)
957 * (1) and (2) above are implemented using polling at a frequency of
958 * 64 Hz. The exact frequency is a tradeoff between accuracy and interrupt
959 * overhead. (DEFAULT_RTC_INT_FREQ)
960 * For (3), we use interrupts at 64Hz or user specified periodic
961 * frequency, whichever is higher.
963 #include <linux/mc146818rtc.h>
964 #include <linux/rtc.h>
965 #include <asm/rtc.h>
967 #define DEFAULT_RTC_INT_FREQ 64
968 #define DEFAULT_RTC_SHIFT 6
969 #define RTC_NUM_INTS 1
971 static unsigned long hpet_rtc_flags;
972 static int hpet_prev_update_sec;
973 static struct rtc_time hpet_alarm_time;
974 static unsigned long hpet_pie_count;
975 static u32 hpet_t1_cmp;
976 static unsigned long hpet_default_delta;
977 static unsigned long hpet_pie_delta;
978 static unsigned long hpet_pie_limit;
980 static rtc_irq_handler irq_handler;
983 * Check that the hpet counter c1 is ahead of the c2
985 static inline int hpet_cnt_ahead(u32 c1, u32 c2)
987 return (s32)(c2 - c1) < 0;
991 * Registers a IRQ handler.
993 int hpet_register_irq_handler(rtc_irq_handler handler)
995 if (!is_hpet_enabled())
996 return -ENODEV;
997 if (irq_handler)
998 return -EBUSY;
1000 irq_handler = handler;
1002 return 0;
1004 EXPORT_SYMBOL_GPL(hpet_register_irq_handler);
1007 * Deregisters the IRQ handler registered with hpet_register_irq_handler()
1008 * and does cleanup.
1010 void hpet_unregister_irq_handler(rtc_irq_handler handler)
1012 if (!is_hpet_enabled())
1013 return;
1015 irq_handler = NULL;
1016 hpet_rtc_flags = 0;
1018 EXPORT_SYMBOL_GPL(hpet_unregister_irq_handler);
1021 * Timer 1 for RTC emulation. We use one shot mode, as periodic mode
1022 * is not supported by all HPET implementations for timer 1.
1024 * hpet_rtc_timer_init() is called when the rtc is initialized.
1026 int hpet_rtc_timer_init(void)
1028 unsigned long cfg, cnt, delta, flags;
1030 if (!is_hpet_enabled())
1031 return 0;
1033 if (!hpet_default_delta) {
1034 uint64_t clc;
1036 clc = (uint64_t) hpet_clockevent.mult * NSEC_PER_SEC;
1037 clc >>= hpet_clockevent.shift + DEFAULT_RTC_SHIFT;
1038 hpet_default_delta = (unsigned long) clc;
1041 if (!(hpet_rtc_flags & RTC_PIE) || hpet_pie_limit)
1042 delta = hpet_default_delta;
1043 else
1044 delta = hpet_pie_delta;
1046 local_irq_save(flags);
1048 cnt = delta + hpet_readl(HPET_COUNTER);
1049 hpet_writel(cnt, HPET_T1_CMP);
1050 hpet_t1_cmp = cnt;
1052 cfg = hpet_readl(HPET_T1_CFG);
1053 cfg &= ~HPET_TN_PERIODIC;
1054 cfg |= HPET_TN_ENABLE | HPET_TN_32BIT;
1055 hpet_writel(cfg, HPET_T1_CFG);
1057 local_irq_restore(flags);
1059 return 1;
1061 EXPORT_SYMBOL_GPL(hpet_rtc_timer_init);
1064 * The functions below are called from rtc driver.
1065 * Return 0 if HPET is not being used.
1066 * Otherwise do the necessary changes and return 1.
1068 int hpet_mask_rtc_irq_bit(unsigned long bit_mask)
1070 if (!is_hpet_enabled())
1071 return 0;
1073 hpet_rtc_flags &= ~bit_mask;
1074 return 1;
1076 EXPORT_SYMBOL_GPL(hpet_mask_rtc_irq_bit);
1078 int hpet_set_rtc_irq_bit(unsigned long bit_mask)
1080 unsigned long oldbits = hpet_rtc_flags;
1082 if (!is_hpet_enabled())
1083 return 0;
1085 hpet_rtc_flags |= bit_mask;
1087 if ((bit_mask & RTC_UIE) && !(oldbits & RTC_UIE))
1088 hpet_prev_update_sec = -1;
1090 if (!oldbits)
1091 hpet_rtc_timer_init();
1093 return 1;
1095 EXPORT_SYMBOL_GPL(hpet_set_rtc_irq_bit);
1097 int hpet_set_alarm_time(unsigned char hrs, unsigned char min,
1098 unsigned char sec)
1100 if (!is_hpet_enabled())
1101 return 0;
1103 hpet_alarm_time.tm_hour = hrs;
1104 hpet_alarm_time.tm_min = min;
1105 hpet_alarm_time.tm_sec = sec;
1107 return 1;
1109 EXPORT_SYMBOL_GPL(hpet_set_alarm_time);
1111 int hpet_set_periodic_freq(unsigned long freq)
1113 uint64_t clc;
1115 if (!is_hpet_enabled())
1116 return 0;
1118 if (freq <= DEFAULT_RTC_INT_FREQ)
1119 hpet_pie_limit = DEFAULT_RTC_INT_FREQ / freq;
1120 else {
1121 clc = (uint64_t) hpet_clockevent.mult * NSEC_PER_SEC;
1122 do_div(clc, freq);
1123 clc >>= hpet_clockevent.shift;
1124 hpet_pie_delta = (unsigned long) clc;
1126 return 1;
1128 EXPORT_SYMBOL_GPL(hpet_set_periodic_freq);
1130 int hpet_rtc_dropped_irq(void)
1132 return is_hpet_enabled();
1134 EXPORT_SYMBOL_GPL(hpet_rtc_dropped_irq);
1136 static void hpet_rtc_timer_reinit(void)
1138 unsigned long cfg, delta;
1139 int lost_ints = -1;
1141 if (unlikely(!hpet_rtc_flags)) {
1142 cfg = hpet_readl(HPET_T1_CFG);
1143 cfg &= ~HPET_TN_ENABLE;
1144 hpet_writel(cfg, HPET_T1_CFG);
1145 return;
1148 if (!(hpet_rtc_flags & RTC_PIE) || hpet_pie_limit)
1149 delta = hpet_default_delta;
1150 else
1151 delta = hpet_pie_delta;
1154 * Increment the comparator value until we are ahead of the
1155 * current count.
1157 do {
1158 hpet_t1_cmp += delta;
1159 hpet_writel(hpet_t1_cmp, HPET_T1_CMP);
1160 lost_ints++;
1161 } while (!hpet_cnt_ahead(hpet_t1_cmp, hpet_readl(HPET_COUNTER)));
1163 if (lost_ints) {
1164 if (hpet_rtc_flags & RTC_PIE)
1165 hpet_pie_count += lost_ints;
1166 if (printk_ratelimit())
1167 printk(KERN_WARNING "hpet1: lost %d rtc interrupts\n",
1168 lost_ints);
1172 irqreturn_t hpet_rtc_interrupt(int irq, void *dev_id)
1174 struct rtc_time curr_time;
1175 unsigned long rtc_int_flag = 0;
1177 hpet_rtc_timer_reinit();
1178 memset(&curr_time, 0, sizeof(struct rtc_time));
1180 if (hpet_rtc_flags & (RTC_UIE | RTC_AIE))
1181 get_rtc_time(&curr_time);
1183 if (hpet_rtc_flags & RTC_UIE &&
1184 curr_time.tm_sec != hpet_prev_update_sec) {
1185 if (hpet_prev_update_sec >= 0)
1186 rtc_int_flag = RTC_UF;
1187 hpet_prev_update_sec = curr_time.tm_sec;
1190 if (hpet_rtc_flags & RTC_PIE &&
1191 ++hpet_pie_count >= hpet_pie_limit) {
1192 rtc_int_flag |= RTC_PF;
1193 hpet_pie_count = 0;
1196 if (hpet_rtc_flags & RTC_AIE &&
1197 (curr_time.tm_sec == hpet_alarm_time.tm_sec) &&
1198 (curr_time.tm_min == hpet_alarm_time.tm_min) &&
1199 (curr_time.tm_hour == hpet_alarm_time.tm_hour))
1200 rtc_int_flag |= RTC_AF;
1202 if (rtc_int_flag) {
1203 rtc_int_flag |= (RTC_IRQF | (RTC_NUM_INTS << 8));
1204 if (irq_handler)
1205 irq_handler(rtc_int_flag, dev_id);
1207 return IRQ_HANDLED;
1209 EXPORT_SYMBOL_GPL(hpet_rtc_interrupt);
1210 #endif