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