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