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