]> git.karo-electronics.de Git - karo-tx-linux.git/blob - arch/x86/kernel/hpet.c
74f5a3ffab0c1ea5041cbf4c3dcf210856542870
[karo-tx-linux.git] / arch / x86 / kernel / hpet.c
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>
12
13 #include <asm/fixmap.h>
14 #include <asm/i8253.h>
15 #include <asm/hpet.h>
16
17 #define HPET_MASK                       CLOCKSOURCE_MASK(32)
18 #define HPET_SHIFT                      22
19
20 /* FSEC = 10^-15
21    NSEC = 10^-9 */
22 #define FSEC_PER_NSEC                   1000000L
23
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
29
30 #define EVT_TO_HPET_DEV(evt) container_of(evt, struct hpet_dev, evt)
31
32 /*
33  * HPET address is set in acpi/boot.c, when an ACPI entry exists
34  */
35 unsigned long                           hpet_address;
36 u8                                      hpet_msi_disable;
37
38 #ifdef CONFIG_PCI_MSI
39 static unsigned long                    hpet_num_timers;
40 #endif
41 static void __iomem                     *hpet_virt_address;
42
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];
50 };
51
52 unsigned long hpet_readl(unsigned long a)
53 {
54         return readl(hpet_virt_address + a);
55 }
56
57 static inline void hpet_writel(unsigned long d, unsigned long a)
58 {
59         writel(d, hpet_virt_address + a);
60 }
61
62 #ifdef CONFIG_X86_64
63 #include <asm/pgtable.h>
64 #endif
65
66 static inline void hpet_set_mapping(void)
67 {
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
72 }
73
74 static inline void hpet_clear_mapping(void)
75 {
76         iounmap(hpet_virt_address);
77         hpet_virt_address = NULL;
78 }
79
80 /*
81  * HPET command line enable / disable
82  */
83 static int boot_hpet_disable;
84 int hpet_force_user;
85 static int hpet_verbose;
86
87 static int __init hpet_setup(char *str)
88 {
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;
96         }
97         return 1;
98 }
99 __setup("hpet=", hpet_setup);
100
101 static int __init disable_hpet(char *str)
102 {
103         boot_hpet_disable = 1;
104         return 1;
105 }
106 __setup("nohpet", disable_hpet);
107
108 static inline int is_hpet_capable(void)
109 {
110         return !boot_hpet_disable && hpet_address;
111 }
112
113 /*
114  * HPET timer interrupt enable / disable
115  */
116 static int hpet_legacy_int_enabled;
117
118 /**
119  * is_hpet_enabled - check whether the hpet timer interrupt is enabled
120  */
121 int is_hpet_enabled(void)
122 {
123         return is_hpet_capable() && hpet_legacy_int_enabled;
124 }
125 EXPORT_SYMBOL_GPL(is_hpet_enabled);
126
127 static void _hpet_print_config(const char *function, int line)
128 {
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);
141
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);
155         }
156 }
157
158 #define hpet_print_config()                                     \
159 do {                                                            \
160         if (hpet_verbose)                                       \
161                 _hpet_print_config(__FUNCTION__, __LINE__);     \
162 } while (0)
163
164 /*
165  * When the hpet driver (/dev/hpet) is enabled, we need to reserve
166  * timer 0 and timer 1 in case of RTC emulation.
167  */
168 #ifdef CONFIG_HPET
169
170 static void hpet_reserve_msi_timers(struct hpet_data *hd);
171
172 static void hpet_reserve_platform_timers(unsigned long id)
173 {
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;
178
179         nrtimers = ((id & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT) + 1;
180
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);
186
187 #ifdef CONFIG_HPET_EMULATE_RTC
188         hpet_reserve_timer(&hd, 1);
189 #endif
190
191         /*
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.
195          */
196         hd.hd_irq[0] = HPET_LEGACY_8254;
197         hd.hd_irq[1] = HPET_LEGACY_RTC;
198
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;
202         }
203
204         hpet_reserve_msi_timers(&hd);
205
206         hpet_alloc(&hd);
207
208 }
209 #else
210 static void hpet_reserve_platform_timers(unsigned long id) { }
211 #endif
212
213 /*
214  * Common hpet info
215  */
216 static unsigned long hpet_period;
217
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);
222
223 /*
224  * The hpet clock event device
225  */
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,
234 };
235
236 static void hpet_stop_counter(void)
237 {
238         unsigned long cfg = hpet_readl(HPET_CFG);
239         cfg &= ~HPET_CFG_ENABLE;
240         hpet_writel(cfg, HPET_CFG);
241 }
242
243 static void hpet_reset_counter(void)
244 {
245         hpet_writel(0, HPET_COUNTER);
246         hpet_writel(0, HPET_COUNTER + 4);
247 }
248
249 static void hpet_start_counter(void)
250 {
251         unsigned long cfg = hpet_readl(HPET_CFG);
252         cfg |= HPET_CFG_ENABLE;
253         hpet_writel(cfg, HPET_CFG);
254 }
255
256 static void hpet_restart_counter(void)
257 {
258         hpet_stop_counter();
259         hpet_reset_counter();
260         hpet_start_counter();
261 }
262
263 static void hpet_resume_device(void)
264 {
265         force_hpet_resume();
266 }
267
268 static void hpet_resume_counter(void)
269 {
270         hpet_resume_device();
271         hpet_restart_counter();
272 }
273
274 static void hpet_enable_legacy_int(void)
275 {
276         unsigned long cfg = hpet_readl(HPET_CFG);
277
278         cfg |= HPET_CFG_LEGACY;
279         hpet_writel(cfg, HPET_CFG);
280         hpet_legacy_int_enabled = 1;
281 }
282
283 static void hpet_legacy_clockevent_register(void)
284 {
285         /* Start HPET legacy interrupts */
286         hpet_enable_legacy_int();
287
288         /*
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
295          */
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;
303
304         /*
305          * Start hpet with the boot cpu mask and make it
306          * global after the IO_APIC has been initialized.
307          */
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");
312 }
313
314 static int hpet_setup_msi_irq(unsigned int irq);
315
316 static void hpet_set_mode(enum clock_event_mode mode,
317                           struct clock_event_device *evt, int timer)
318 {
319         unsigned long cfg, cmp, now;
320         uint64_t delta;
321
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);
337                 /*
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)
343                  */
344                 hpet_writel((unsigned long) delta, HPET_Tn_CMP(timer));
345                 hpet_start_counter();
346                 hpet_print_config();
347                 break;
348
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;
355
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;
362
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);
372                 }
373                 hpet_print_config();
374                 break;
375         }
376 }
377
378 static int hpet_next_event(unsigned long delta,
379                            struct clock_event_device *evt, int timer)
380 {
381         u32 cnt;
382
383         cnt = hpet_readl(HPET_COUNTER);
384         cnt += (u32) delta;
385         hpet_writel(cnt, HPET_Tn_CMP(timer));
386
387         /*
388          * We need to read back the CMP register on certain HPET
389          * implementations (ATI chipsets) which seem to delay the
390          * transfer of the compare register into the internal compare
391          * logic. With small deltas this might actually be too late as
392          * the counter could already be higher than the compare value
393          * at that point and we would wait for the next hpet interrupt
394          * forever. We found out that reading the CMP register back
395          * forces the transfer so we can rely on the comparison with
396          * the counter register below. If the read back from the
397          * compare register does not match the value we programmed
398          * then we might have a real hardware problem. We can not do
399          * much about it here, but at least alert the user/admin with
400          * a prominent warning.
401          * An erratum on some chipsets (ICH9,..), results in comparator read
402          * immediately following a write returning old value. Workaround
403          * for this is to read this value second time, when first
404          * read returns old value.
405          */
406         if (unlikely((u32)hpet_readl(HPET_Tn_CMP(timer)) != cnt)) {
407                 WARN_ONCE((u32)hpet_readl(HPET_Tn_CMP(timer)) != cnt,
408                   KERN_WARNING "hpet: compare register read back failed.\n");
409         }
410
411         return (s32)((u32)hpet_readl(HPET_COUNTER) - cnt) >= 0 ? -ETIME : 0;
412 }
413
414 static void hpet_legacy_set_mode(enum clock_event_mode mode,
415                         struct clock_event_device *evt)
416 {
417         hpet_set_mode(mode, evt, 0);
418 }
419
420 static int hpet_legacy_next_event(unsigned long delta,
421                         struct clock_event_device *evt)
422 {
423         return hpet_next_event(delta, evt, 0);
424 }
425
426 /*
427  * HPET MSI Support
428  */
429 #ifdef CONFIG_PCI_MSI
430
431 static DEFINE_PER_CPU(struct hpet_dev *, cpu_hpet_dev);
432 static struct hpet_dev  *hpet_devs;
433
434 void hpet_msi_unmask(unsigned int irq)
435 {
436         struct hpet_dev *hdev = get_irq_data(irq);
437         unsigned long cfg;
438
439         /* unmask it */
440         cfg = hpet_readl(HPET_Tn_CFG(hdev->num));
441         cfg |= HPET_TN_FSB;
442         hpet_writel(cfg, HPET_Tn_CFG(hdev->num));
443 }
444
445 void hpet_msi_mask(unsigned int irq)
446 {
447         unsigned long cfg;
448         struct hpet_dev *hdev = get_irq_data(irq);
449
450         /* mask it */
451         cfg = hpet_readl(HPET_Tn_CFG(hdev->num));
452         cfg &= ~HPET_TN_FSB;
453         hpet_writel(cfg, HPET_Tn_CFG(hdev->num));
454 }
455
456 void hpet_msi_write(unsigned int irq, struct msi_msg *msg)
457 {
458         struct hpet_dev *hdev = get_irq_data(irq);
459
460         hpet_writel(msg->data, HPET_Tn_ROUTE(hdev->num));
461         hpet_writel(msg->address_lo, HPET_Tn_ROUTE(hdev->num) + 4);
462 }
463
464 void hpet_msi_read(unsigned int irq, struct msi_msg *msg)
465 {
466         struct hpet_dev *hdev = get_irq_data(irq);
467
468         msg->data = hpet_readl(HPET_Tn_ROUTE(hdev->num));
469         msg->address_lo = hpet_readl(HPET_Tn_ROUTE(hdev->num) + 4);
470         msg->address_hi = 0;
471 }
472
473 static void hpet_msi_set_mode(enum clock_event_mode mode,
474                                 struct clock_event_device *evt)
475 {
476         struct hpet_dev *hdev = EVT_TO_HPET_DEV(evt);
477         hpet_set_mode(mode, evt, hdev->num);
478 }
479
480 static int hpet_msi_next_event(unsigned long delta,
481                                 struct clock_event_device *evt)
482 {
483         struct hpet_dev *hdev = EVT_TO_HPET_DEV(evt);
484         return hpet_next_event(delta, evt, hdev->num);
485 }
486
487 static int hpet_setup_msi_irq(unsigned int irq)
488 {
489         if (arch_setup_hpet_msi(irq)) {
490                 destroy_irq(irq);
491                 return -EINVAL;
492         }
493         return 0;
494 }
495
496 static int hpet_assign_irq(struct hpet_dev *dev)
497 {
498         unsigned int irq;
499
500         irq = create_irq();
501         if (!irq)
502                 return -EINVAL;
503
504         set_irq_data(irq, dev);
505
506         if (hpet_setup_msi_irq(irq))
507                 return -EINVAL;
508
509         dev->irq = irq;
510         return 0;
511 }
512
513 static irqreturn_t hpet_interrupt_handler(int irq, void *data)
514 {
515         struct hpet_dev *dev = (struct hpet_dev *)data;
516         struct clock_event_device *hevt = &dev->evt;
517
518         if (!hevt->event_handler) {
519                 printk(KERN_INFO "Spurious HPET timer interrupt on HPET timer %d\n",
520                                 dev->num);
521                 return IRQ_HANDLED;
522         }
523
524         hevt->event_handler(hevt);
525         return IRQ_HANDLED;
526 }
527
528 static int hpet_setup_irq(struct hpet_dev *dev)
529 {
530
531         if (request_irq(dev->irq, hpet_interrupt_handler,
532                         IRQF_TIMER | IRQF_DISABLED | IRQF_NOBALANCING,
533                         dev->name, dev))
534                 return -1;
535
536         disable_irq(dev->irq);
537         irq_set_affinity(dev->irq, cpumask_of(dev->cpu));
538         enable_irq(dev->irq);
539
540         printk(KERN_DEBUG "hpet: %s irq %d for MSI\n",
541                          dev->name, dev->irq);
542
543         return 0;
544 }
545
546 /* This should be called in specific @cpu */
547 static void init_one_hpet_msi_clockevent(struct hpet_dev *hdev, int cpu)
548 {
549         struct clock_event_device *evt = &hdev->evt;
550         uint64_t hpet_freq;
551
552         WARN_ON(cpu != smp_processor_id());
553         if (!(hdev->flags & HPET_DEV_VALID))
554                 return;
555
556         if (hpet_setup_msi_irq(hdev->irq))
557                 return;
558
559         hdev->cpu = cpu;
560         per_cpu(cpu_hpet_dev, cpu) = hdev;
561         evt->name = hdev->name;
562         hpet_setup_irq(hdev);
563         evt->irq = hdev->irq;
564
565         evt->rating = 110;
566         evt->features = CLOCK_EVT_FEAT_ONESHOT;
567         if (hdev->flags & HPET_DEV_PERI_CAP)
568                 evt->features |= CLOCK_EVT_FEAT_PERIODIC;
569
570         evt->set_mode = hpet_msi_set_mode;
571         evt->set_next_event = hpet_msi_next_event;
572         evt->shift = 32;
573
574         /*
575          * The period is a femto seconds value. We need to calculate the
576          * scaled math multiplication factor for nanosecond to hpet tick
577          * conversion.
578          */
579         hpet_freq = 1000000000000000ULL;
580         do_div(hpet_freq, hpet_period);
581         evt->mult = div_sc((unsigned long) hpet_freq,
582                                       NSEC_PER_SEC, evt->shift);
583         /* Calculate the max delta */
584         evt->max_delta_ns = clockevent_delta2ns(0x7FFFFFFF, evt);
585         /* 5 usec minimum reprogramming delta. */
586         evt->min_delta_ns = 5000;
587
588         evt->cpumask = cpumask_of(hdev->cpu);
589         clockevents_register_device(evt);
590 }
591
592 #ifdef CONFIG_HPET
593 /* Reserve at least one timer for userspace (/dev/hpet) */
594 #define RESERVE_TIMERS 1
595 #else
596 #define RESERVE_TIMERS 0
597 #endif
598
599 static void hpet_msi_capability_lookup(unsigned int start_timer)
600 {
601         unsigned int id;
602         unsigned int num_timers;
603         unsigned int num_timers_used = 0;
604         int i;
605
606         if (hpet_msi_disable)
607                 return;
608
609         id = hpet_readl(HPET_ID);
610
611         num_timers = ((id & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT);
612         num_timers++; /* Value read out starts from 0 */
613         hpet_print_config();
614
615         hpet_devs = kzalloc(sizeof(struct hpet_dev) * num_timers, GFP_KERNEL);
616         if (!hpet_devs)
617                 return;
618
619         hpet_num_timers = num_timers;
620
621         for (i = start_timer; i < num_timers - RESERVE_TIMERS; i++) {
622                 struct hpet_dev *hdev = &hpet_devs[num_timers_used];
623                 unsigned long cfg = hpet_readl(HPET_Tn_CFG(i));
624
625                 /* Only consider HPET timer with MSI support */
626                 if (!(cfg & HPET_TN_FSB_CAP))
627                         continue;
628
629                 hdev->flags = 0;
630                 if (cfg & HPET_TN_PERIODIC_CAP)
631                         hdev->flags |= HPET_DEV_PERI_CAP;
632                 hdev->num = i;
633
634                 sprintf(hdev->name, "hpet%d", i);
635                 if (hpet_assign_irq(hdev))
636                         continue;
637
638                 hdev->flags |= HPET_DEV_FSB_CAP;
639                 hdev->flags |= HPET_DEV_VALID;
640                 num_timers_used++;
641                 if (num_timers_used == num_possible_cpus())
642                         break;
643         }
644
645         printk(KERN_INFO "HPET: %d timers in total, %d timers will be used for per-cpu timer\n",
646                 num_timers, num_timers_used);
647 }
648
649 #ifdef CONFIG_HPET
650 static void hpet_reserve_msi_timers(struct hpet_data *hd)
651 {
652         int i;
653
654         if (!hpet_devs)
655                 return;
656
657         for (i = 0; i < hpet_num_timers; i++) {
658                 struct hpet_dev *hdev = &hpet_devs[i];
659
660                 if (!(hdev->flags & HPET_DEV_VALID))
661                         continue;
662
663                 hd->hd_irq[hdev->num] = hdev->irq;
664                 hpet_reserve_timer(hd, hdev->num);
665         }
666 }
667 #endif
668
669 static struct hpet_dev *hpet_get_unused_timer(void)
670 {
671         int i;
672
673         if (!hpet_devs)
674                 return NULL;
675
676         for (i = 0; i < hpet_num_timers; i++) {
677                 struct hpet_dev *hdev = &hpet_devs[i];
678
679                 if (!(hdev->flags & HPET_DEV_VALID))
680                         continue;
681                 if (test_and_set_bit(HPET_DEV_USED_BIT,
682                         (unsigned long *)&hdev->flags))
683                         continue;
684                 return hdev;
685         }
686         return NULL;
687 }
688
689 struct hpet_work_struct {
690         struct delayed_work work;
691         struct completion complete;
692 };
693
694 static void hpet_work(struct work_struct *w)
695 {
696         struct hpet_dev *hdev;
697         int cpu = smp_processor_id();
698         struct hpet_work_struct *hpet_work;
699
700         hpet_work = container_of(w, struct hpet_work_struct, work.work);
701
702         hdev = hpet_get_unused_timer();
703         if (hdev)
704                 init_one_hpet_msi_clockevent(hdev, cpu);
705
706         complete(&hpet_work->complete);
707 }
708
709 static int hpet_cpuhp_notify(struct notifier_block *n,
710                 unsigned long action, void *hcpu)
711 {
712         unsigned long cpu = (unsigned long)hcpu;
713         struct hpet_work_struct work;
714         struct hpet_dev *hdev = per_cpu(cpu_hpet_dev, cpu);
715
716         switch (action & 0xf) {
717         case CPU_ONLINE:
718                 INIT_DELAYED_WORK_ON_STACK(&work.work, hpet_work);
719                 init_completion(&work.complete);
720                 /* FIXME: add schedule_work_on() */
721                 schedule_delayed_work_on(cpu, &work.work, 0);
722                 wait_for_completion(&work.complete);
723                 destroy_timer_on_stack(&work.work.timer);
724                 break;
725         case CPU_DEAD:
726                 if (hdev) {
727                         free_irq(hdev->irq, hdev);
728                         hdev->flags &= ~HPET_DEV_USED;
729                         per_cpu(cpu_hpet_dev, cpu) = NULL;
730                 }
731                 break;
732         }
733         return NOTIFY_OK;
734 }
735 #else
736
737 static int hpet_setup_msi_irq(unsigned int irq)
738 {
739         return 0;
740 }
741 static void hpet_msi_capability_lookup(unsigned int start_timer)
742 {
743         return;
744 }
745
746 #ifdef CONFIG_HPET
747 static void hpet_reserve_msi_timers(struct hpet_data *hd)
748 {
749         return;
750 }
751 #endif
752
753 static int hpet_cpuhp_notify(struct notifier_block *n,
754                 unsigned long action, void *hcpu)
755 {
756         return NOTIFY_OK;
757 }
758
759 #endif
760
761 /*
762  * Clock source related code
763  */
764 static cycle_t read_hpet(struct clocksource *cs)
765 {
766         return (cycle_t)hpet_readl(HPET_COUNTER);
767 }
768
769 #ifdef CONFIG_X86_64
770 static cycle_t __vsyscall_fn vread_hpet(void)
771 {
772         return readl((const void __iomem *)fix_to_virt(VSYSCALL_HPET) + 0xf0);
773 }
774 #endif
775
776 static struct clocksource clocksource_hpet = {
777         .name           = "hpet",
778         .rating         = 250,
779         .read           = read_hpet,
780         .mask           = HPET_MASK,
781         .shift          = HPET_SHIFT,
782         .flags          = CLOCK_SOURCE_IS_CONTINUOUS,
783         .resume         = hpet_resume_counter,
784 #ifdef CONFIG_X86_64
785         .vread          = vread_hpet,
786 #endif
787 };
788
789 static int hpet_clocksource_register(void)
790 {
791         u64 start, now;
792         cycle_t t1;
793
794         /* Start the counter */
795         hpet_restart_counter();
796
797         /* Verify whether hpet counter works */
798         t1 = hpet_readl(HPET_COUNTER);
799         rdtscll(start);
800
801         /*
802          * We don't know the TSC frequency yet, but waiting for
803          * 200000 TSC cycles is safe:
804          * 4 GHz == 50us
805          * 1 GHz == 200us
806          */
807         do {
808                 rep_nop();
809                 rdtscll(now);
810         } while ((now - start) < 200000UL);
811
812         if (t1 == hpet_readl(HPET_COUNTER)) {
813                 printk(KERN_WARNING
814                        "HPET counter not counting. HPET disabled\n");
815                 return -ENODEV;
816         }
817
818         /*
819          * The definition of mult is (include/linux/clocksource.h)
820          * mult/2^shift = ns/cyc and hpet_period is in units of fsec/cyc
821          * so we first need to convert hpet_period to ns/cyc units:
822          *  mult/2^shift = ns/cyc = hpet_period/10^6
823          *  mult = (hpet_period * 2^shift)/10^6
824          *  mult = (hpet_period << shift)/FSEC_PER_NSEC
825          */
826         clocksource_hpet.mult = div_sc(hpet_period, FSEC_PER_NSEC, HPET_SHIFT);
827
828         clocksource_register(&clocksource_hpet);
829
830         return 0;
831 }
832
833 /**
834  * hpet_enable - Try to setup the HPET timer. Returns 1 on success.
835  */
836 int __init hpet_enable(void)
837 {
838         unsigned long id;
839         int i;
840
841         if (!is_hpet_capable())
842                 return 0;
843
844         hpet_set_mapping();
845
846         /*
847          * Read the period and check for a sane value:
848          */
849         hpet_period = hpet_readl(HPET_PERIOD);
850
851         /*
852          * AMD SB700 based systems with spread spectrum enabled use a
853          * SMM based HPET emulation to provide proper frequency
854          * setting. The SMM code is initialized with the first HPET
855          * register access and takes some time to complete. During
856          * this time the config register reads 0xffffffff. We check
857          * for max. 1000 loops whether the config register reads a non
858          * 0xffffffff value to make sure that HPET is up and running
859          * before we go further. A counting loop is safe, as the HPET
860          * access takes thousands of CPU cycles. On non SB700 based
861          * machines this check is only done once and has no side
862          * effects.
863          */
864         for (i = 0; hpet_readl(HPET_CFG) == 0xFFFFFFFF; i++) {
865                 if (i == 1000) {
866                         printk(KERN_WARNING
867                                "HPET config register value = 0xFFFFFFFF. "
868                                "Disabling HPET\n");
869                         goto out_nohpet;
870                 }
871         }
872
873         if (hpet_period < HPET_MIN_PERIOD || hpet_period > HPET_MAX_PERIOD)
874                 goto out_nohpet;
875
876         /*
877          * Read the HPET ID register to retrieve the IRQ routing
878          * information and the number of channels
879          */
880         id = hpet_readl(HPET_ID);
881         hpet_print_config();
882
883 #ifdef CONFIG_HPET_EMULATE_RTC
884         /*
885          * The legacy routing mode needs at least two channels, tick timer
886          * and the rtc emulation channel.
887          */
888         if (!(id & HPET_ID_NUMBER))
889                 goto out_nohpet;
890 #endif
891
892         if (hpet_clocksource_register())
893                 goto out_nohpet;
894
895         if (id & HPET_ID_LEGSUP) {
896                 hpet_legacy_clockevent_register();
897                 hpet_msi_capability_lookup(2);
898                 return 1;
899         }
900         hpet_msi_capability_lookup(0);
901         return 0;
902
903 out_nohpet:
904         hpet_clear_mapping();
905         hpet_address = 0;
906         return 0;
907 }
908
909 /*
910  * Needs to be late, as the reserve_timer code calls kalloc !
911  *
912  * Not a problem on i386 as hpet_enable is called from late_time_init,
913  * but on x86_64 it is necessary !
914  */
915 static __init int hpet_late_init(void)
916 {
917         int cpu;
918
919         if (boot_hpet_disable)
920                 return -ENODEV;
921
922         if (!hpet_address) {
923                 if (!force_hpet_address)
924                         return -ENODEV;
925
926                 hpet_address = force_hpet_address;
927                 hpet_enable();
928         }
929
930         if (!hpet_virt_address)
931                 return -ENODEV;
932
933         hpet_reserve_platform_timers(hpet_readl(HPET_ID));
934         hpet_print_config();
935
936         if (hpet_msi_disable)
937                 return 0;
938
939         for_each_online_cpu(cpu) {
940                 hpet_cpuhp_notify(NULL, CPU_ONLINE, (void *)(long)cpu);
941         }
942
943         /* This notifier should be called after workqueue is ready */
944         hotcpu_notifier(hpet_cpuhp_notify, -20);
945
946         return 0;
947 }
948 fs_initcall(hpet_late_init);
949
950 void hpet_disable(void)
951 {
952         if (is_hpet_capable()) {
953                 unsigned long cfg = hpet_readl(HPET_CFG);
954
955                 if (hpet_legacy_int_enabled) {
956                         cfg &= ~HPET_CFG_LEGACY;
957                         hpet_legacy_int_enabled = 0;
958                 }
959                 cfg &= ~HPET_CFG_ENABLE;
960                 hpet_writel(cfg, HPET_CFG);
961         }
962 }
963
964 #ifdef CONFIG_HPET_EMULATE_RTC
965
966 /* HPET in LegacyReplacement Mode eats up RTC interrupt line. When, HPET
967  * is enabled, we support RTC interrupt functionality in software.
968  * RTC has 3 kinds of interrupts:
969  * 1) Update Interrupt - generate an interrupt, every sec, when RTC clock
970  *    is updated
971  * 2) Alarm Interrupt - generate an interrupt at a specific time of day
972  * 3) Periodic Interrupt - generate periodic interrupt, with frequencies
973  *    2Hz-8192Hz (2Hz-64Hz for non-root user) (all freqs in powers of 2)
974  * (1) and (2) above are implemented using polling at a frequency of
975  * 64 Hz. The exact frequency is a tradeoff between accuracy and interrupt
976  * overhead. (DEFAULT_RTC_INT_FREQ)
977  * For (3), we use interrupts at 64Hz or user specified periodic
978  * frequency, whichever is higher.
979  */
980 #include <linux/mc146818rtc.h>
981 #include <linux/rtc.h>
982 #include <asm/rtc.h>
983
984 #define DEFAULT_RTC_INT_FREQ    64
985 #define DEFAULT_RTC_SHIFT       6
986 #define RTC_NUM_INTS            1
987
988 static unsigned long hpet_rtc_flags;
989 static int hpet_prev_update_sec;
990 static struct rtc_time hpet_alarm_time;
991 static unsigned long hpet_pie_count;
992 static u32 hpet_t1_cmp;
993 static unsigned long hpet_default_delta;
994 static unsigned long hpet_pie_delta;
995 static unsigned long hpet_pie_limit;
996
997 static rtc_irq_handler irq_handler;
998
999 /*
1000  * Check that the hpet counter c1 is ahead of the c2
1001  */
1002 static inline int hpet_cnt_ahead(u32 c1, u32 c2)
1003 {
1004         return (s32)(c2 - c1) < 0;
1005 }
1006
1007 /*
1008  * Registers a IRQ handler.
1009  */
1010 int hpet_register_irq_handler(rtc_irq_handler handler)
1011 {
1012         if (!is_hpet_enabled())
1013                 return -ENODEV;
1014         if (irq_handler)
1015                 return -EBUSY;
1016
1017         irq_handler = handler;
1018
1019         return 0;
1020 }
1021 EXPORT_SYMBOL_GPL(hpet_register_irq_handler);
1022
1023 /*
1024  * Deregisters the IRQ handler registered with hpet_register_irq_handler()
1025  * and does cleanup.
1026  */
1027 void hpet_unregister_irq_handler(rtc_irq_handler handler)
1028 {
1029         if (!is_hpet_enabled())
1030                 return;
1031
1032         irq_handler = NULL;
1033         hpet_rtc_flags = 0;
1034 }
1035 EXPORT_SYMBOL_GPL(hpet_unregister_irq_handler);
1036
1037 /*
1038  * Timer 1 for RTC emulation. We use one shot mode, as periodic mode
1039  * is not supported by all HPET implementations for timer 1.
1040  *
1041  * hpet_rtc_timer_init() is called when the rtc is initialized.
1042  */
1043 int hpet_rtc_timer_init(void)
1044 {
1045         unsigned long cfg, cnt, delta, flags;
1046
1047         if (!is_hpet_enabled())
1048                 return 0;
1049
1050         if (!hpet_default_delta) {
1051                 uint64_t clc;
1052
1053                 clc = (uint64_t) hpet_clockevent.mult * NSEC_PER_SEC;
1054                 clc >>= hpet_clockevent.shift + DEFAULT_RTC_SHIFT;
1055                 hpet_default_delta = (unsigned long) clc;
1056         }
1057
1058         if (!(hpet_rtc_flags & RTC_PIE) || hpet_pie_limit)
1059                 delta = hpet_default_delta;
1060         else
1061                 delta = hpet_pie_delta;
1062
1063         local_irq_save(flags);
1064
1065         cnt = delta + hpet_readl(HPET_COUNTER);
1066         hpet_writel(cnt, HPET_T1_CMP);
1067         hpet_t1_cmp = cnt;
1068
1069         cfg = hpet_readl(HPET_T1_CFG);
1070         cfg &= ~HPET_TN_PERIODIC;
1071         cfg |= HPET_TN_ENABLE | HPET_TN_32BIT;
1072         hpet_writel(cfg, HPET_T1_CFG);
1073
1074         local_irq_restore(flags);
1075
1076         return 1;
1077 }
1078 EXPORT_SYMBOL_GPL(hpet_rtc_timer_init);
1079
1080 /*
1081  * The functions below are called from rtc driver.
1082  * Return 0 if HPET is not being used.
1083  * Otherwise do the necessary changes and return 1.
1084  */
1085 int hpet_mask_rtc_irq_bit(unsigned long bit_mask)
1086 {
1087         if (!is_hpet_enabled())
1088                 return 0;
1089
1090         hpet_rtc_flags &= ~bit_mask;
1091         return 1;
1092 }
1093 EXPORT_SYMBOL_GPL(hpet_mask_rtc_irq_bit);
1094
1095 int hpet_set_rtc_irq_bit(unsigned long bit_mask)
1096 {
1097         unsigned long oldbits = hpet_rtc_flags;
1098
1099         if (!is_hpet_enabled())
1100                 return 0;
1101
1102         hpet_rtc_flags |= bit_mask;
1103
1104         if ((bit_mask & RTC_UIE) && !(oldbits & RTC_UIE))
1105                 hpet_prev_update_sec = -1;
1106
1107         if (!oldbits)
1108                 hpet_rtc_timer_init();
1109
1110         return 1;
1111 }
1112 EXPORT_SYMBOL_GPL(hpet_set_rtc_irq_bit);
1113
1114 int hpet_set_alarm_time(unsigned char hrs, unsigned char min,
1115                         unsigned char sec)
1116 {
1117         if (!is_hpet_enabled())
1118                 return 0;
1119
1120         hpet_alarm_time.tm_hour = hrs;
1121         hpet_alarm_time.tm_min = min;
1122         hpet_alarm_time.tm_sec = sec;
1123
1124         return 1;
1125 }
1126 EXPORT_SYMBOL_GPL(hpet_set_alarm_time);
1127
1128 int hpet_set_periodic_freq(unsigned long freq)
1129 {
1130         uint64_t clc;
1131
1132         if (!is_hpet_enabled())
1133                 return 0;
1134
1135         if (freq <= DEFAULT_RTC_INT_FREQ)
1136                 hpet_pie_limit = DEFAULT_RTC_INT_FREQ / freq;
1137         else {
1138                 clc = (uint64_t) hpet_clockevent.mult * NSEC_PER_SEC;
1139                 do_div(clc, freq);
1140                 clc >>= hpet_clockevent.shift;
1141                 hpet_pie_delta = (unsigned long) clc;
1142         }
1143         return 1;
1144 }
1145 EXPORT_SYMBOL_GPL(hpet_set_periodic_freq);
1146
1147 int hpet_rtc_dropped_irq(void)
1148 {
1149         return is_hpet_enabled();
1150 }
1151 EXPORT_SYMBOL_GPL(hpet_rtc_dropped_irq);
1152
1153 static void hpet_rtc_timer_reinit(void)
1154 {
1155         unsigned long cfg, delta;
1156         int lost_ints = -1;
1157
1158         if (unlikely(!hpet_rtc_flags)) {
1159                 cfg = hpet_readl(HPET_T1_CFG);
1160                 cfg &= ~HPET_TN_ENABLE;
1161                 hpet_writel(cfg, HPET_T1_CFG);
1162                 return;
1163         }
1164
1165         if (!(hpet_rtc_flags & RTC_PIE) || hpet_pie_limit)
1166                 delta = hpet_default_delta;
1167         else
1168                 delta = hpet_pie_delta;
1169
1170         /*
1171          * Increment the comparator value until we are ahead of the
1172          * current count.
1173          */
1174         do {
1175                 hpet_t1_cmp += delta;
1176                 hpet_writel(hpet_t1_cmp, HPET_T1_CMP);
1177                 lost_ints++;
1178         } while (!hpet_cnt_ahead(hpet_t1_cmp, hpet_readl(HPET_COUNTER)));
1179
1180         if (lost_ints) {
1181                 if (hpet_rtc_flags & RTC_PIE)
1182                         hpet_pie_count += lost_ints;
1183                 if (printk_ratelimit())
1184                         printk(KERN_WARNING "hpet1: lost %d rtc interrupts\n",
1185                                 lost_ints);
1186         }
1187 }
1188
1189 irqreturn_t hpet_rtc_interrupt(int irq, void *dev_id)
1190 {
1191         struct rtc_time curr_time;
1192         unsigned long rtc_int_flag = 0;
1193
1194         hpet_rtc_timer_reinit();
1195         memset(&curr_time, 0, sizeof(struct rtc_time));
1196
1197         if (hpet_rtc_flags & (RTC_UIE | RTC_AIE))
1198                 get_rtc_time(&curr_time);
1199
1200         if (hpet_rtc_flags & RTC_UIE &&
1201             curr_time.tm_sec != hpet_prev_update_sec) {
1202                 if (hpet_prev_update_sec >= 0)
1203                         rtc_int_flag = RTC_UF;
1204                 hpet_prev_update_sec = curr_time.tm_sec;
1205         }
1206
1207         if (hpet_rtc_flags & RTC_PIE &&
1208             ++hpet_pie_count >= hpet_pie_limit) {
1209                 rtc_int_flag |= RTC_PF;
1210                 hpet_pie_count = 0;
1211         }
1212
1213         if (hpet_rtc_flags & RTC_AIE &&
1214             (curr_time.tm_sec == hpet_alarm_time.tm_sec) &&
1215             (curr_time.tm_min == hpet_alarm_time.tm_min) &&
1216             (curr_time.tm_hour == hpet_alarm_time.tm_hour))
1217                         rtc_int_flag |= RTC_AF;
1218
1219         if (rtc_int_flag) {
1220                 rtc_int_flag |= (RTC_IRQF | (RTC_NUM_INTS << 8));
1221                 if (irq_handler)
1222                         irq_handler(rtc_int_flag, dev_id);
1223         }
1224         return IRQ_HANDLED;
1225 }
1226 EXPORT_SYMBOL_GPL(hpet_rtc_interrupt);
1227 #endif