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