blob: ba6e658846035a75f17b16e5ee5cf1de82eb337f [file] [log] [blame]
john stultz5d0cf412006-06-26 00:25:12 -07001#include <linux/clocksource.h>
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08002#include <linux/clockchips.h>
Ingo Molnar4588c1f2008-09-06 14:19:17 +02003#include <linux/interrupt.h>
4#include <linux/sysdev.h>
Thomas Gleixner28769142007-10-12 23:04:06 +02005#include <linux/delay.h>
john stultz5d0cf412006-06-26 00:25:12 -07006#include <linux/errno.h>
7#include <linux/hpet.h>
8#include <linux/init.h>
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07009#include <linux/cpu.h>
Ingo Molnar4588c1f2008-09-06 14:19:17 +020010#include <linux/pm.h>
11#include <linux/io.h>
john stultz5d0cf412006-06-26 00:25:12 -070012
Thomas Gleixner28769142007-10-12 23:04:06 +020013#include <asm/fixmap.h>
Thomas Gleixner06a24de2007-10-12 23:04:06 +020014#include <asm/i8253.h>
Ingo Molnar4588c1f2008-09-06 14:19:17 +020015#include <asm/hpet.h>
john stultz5d0cf412006-06-26 00:25:12 -070016
Ingo Molnar4588c1f2008-09-06 14:19:17 +020017#define HPET_MASK CLOCKSOURCE_MASK(32)
18#define HPET_SHIFT 22
john stultz5d0cf412006-06-26 00:25:12 -070019
Pavel Machekb10db7f2008-01-30 13:30:00 +010020/* FSEC = 10^-15
21 NSEC = 10^-9 */
Ingo Molnar4588c1f2008-09-06 14:19:17 +020022#define FSEC_PER_NSEC 1000000L
john stultz5d0cf412006-06-26 00:25:12 -070023
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -070024#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
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080032/*
33 * HPET address is set in acpi/boot.c, when an ACPI entry exists
34 */
Ingo Molnar4588c1f2008-09-06 14:19:17 +020035unsigned long hpet_address;
Suresh Siddhac8bc6f32009-08-04 12:07:09 -070036u8 hpet_blockid; /* OS timer block num */
Ingo Molnare951e4a2008-11-25 08:42:01 +010037#ifdef CONFIG_PCI_MSI
Hannes Eder3b71e9e2008-11-23 20:19:33 +010038static unsigned long hpet_num_timers;
Ingo Molnare951e4a2008-11-25 08:42:01 +010039#endif
Ingo Molnar4588c1f2008-09-06 14:19:17 +020040static void __iomem *hpet_virt_address;
john stultz5d0cf412006-06-26 00:25:12 -070041
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -070042struct hpet_dev {
Ingo Molnar4588c1f2008-09-06 14:19:17 +020043 struct clock_event_device evt;
44 unsigned int num;
45 int cpu;
46 unsigned int irq;
47 unsigned int flags;
48 char name[10];
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -070049};
50
Jan Beulich5946fa32009-08-19 08:44:24 +010051inline unsigned int hpet_readl(unsigned int a)
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080052{
53 return readl(hpet_virt_address + a);
54}
55
Jan Beulich5946fa32009-08-19 08:44:24 +010056static inline void hpet_writel(unsigned int d, unsigned int a)
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080057{
58 writel(d, hpet_virt_address + a);
59}
60
Thomas Gleixner28769142007-10-12 23:04:06 +020061#ifdef CONFIG_X86_64
Thomas Gleixner28769142007-10-12 23:04:06 +020062#include <asm/pgtable.h>
Yinghai Lu2387ce52008-07-13 14:50:56 -070063#endif
Thomas Gleixner28769142007-10-12 23:04:06 +020064
Thomas Gleixner06a24de2007-10-12 23:04:06 +020065static inline void hpet_set_mapping(void)
66{
67 hpet_virt_address = ioremap_nocache(hpet_address, HPET_MMAP_SIZE);
Yinghai Lu2387ce52008-07-13 14:50:56 -070068#ifdef CONFIG_X86_64
69 __set_fixmap(VSYSCALL_HPET, hpet_address, PAGE_KERNEL_VSYSCALL_NOCACHE);
70#endif
Thomas Gleixner06a24de2007-10-12 23:04:06 +020071}
72
73static inline void hpet_clear_mapping(void)
74{
75 iounmap(hpet_virt_address);
76 hpet_virt_address = NULL;
77}
78
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080079/*
80 * HPET command line enable / disable
81 */
82static int boot_hpet_disable;
Thomas Gleixnerb17530b2007-10-19 20:35:02 +020083int hpet_force_user;
Andreas Herrmannb98103a2009-02-21 00:09:47 +010084static int hpet_verbose;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080085
Ingo Molnar4588c1f2008-09-06 14:19:17 +020086static int __init hpet_setup(char *str)
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080087{
88 if (str) {
89 if (!strncmp("disable", str, 7))
90 boot_hpet_disable = 1;
Thomas Gleixnerb17530b2007-10-19 20:35:02 +020091 if (!strncmp("force", str, 5))
92 hpet_force_user = 1;
Andreas Herrmannb98103a2009-02-21 00:09:47 +010093 if (!strncmp("verbose", str, 7))
94 hpet_verbose = 1;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080095 }
96 return 1;
97}
98__setup("hpet=", hpet_setup);
99
Thomas Gleixner28769142007-10-12 23:04:06 +0200100static int __init disable_hpet(char *str)
101{
102 boot_hpet_disable = 1;
103 return 1;
104}
105__setup("nohpet", disable_hpet);
106
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800107static inline int is_hpet_capable(void)
108{
Ingo Molnar4588c1f2008-09-06 14:19:17 +0200109 return !boot_hpet_disable && hpet_address;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800110}
111
112/*
113 * HPET timer interrupt enable / disable
114 */
115static int hpet_legacy_int_enabled;
116
117/**
118 * is_hpet_enabled - check whether the hpet timer interrupt is enabled
119 */
120int is_hpet_enabled(void)
121{
122 return is_hpet_capable() && hpet_legacy_int_enabled;
123}
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +0100124EXPORT_SYMBOL_GPL(is_hpet_enabled);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800125
Andreas Herrmannb98103a2009-02-21 00:09:47 +0100126static void _hpet_print_config(const char *function, int line)
127{
128 u32 i, timers, l, h;
129 printk(KERN_INFO "hpet: %s(%d):\n", function, line);
130 l = hpet_readl(HPET_ID);
131 h = hpet_readl(HPET_PERIOD);
132 timers = ((l & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT) + 1;
133 printk(KERN_INFO "hpet: ID: 0x%x, PERIOD: 0x%x\n", l, h);
134 l = hpet_readl(HPET_CFG);
135 h = hpet_readl(HPET_STATUS);
136 printk(KERN_INFO "hpet: CFG: 0x%x, STATUS: 0x%x\n", l, h);
137 l = hpet_readl(HPET_COUNTER);
138 h = hpet_readl(HPET_COUNTER+4);
139 printk(KERN_INFO "hpet: COUNTER_l: 0x%x, COUNTER_h: 0x%x\n", l, h);
140
141 for (i = 0; i < timers; i++) {
142 l = hpet_readl(HPET_Tn_CFG(i));
143 h = hpet_readl(HPET_Tn_CFG(i)+4);
144 printk(KERN_INFO "hpet: T%d: CFG_l: 0x%x, CFG_h: 0x%x\n",
145 i, l, h);
146 l = hpet_readl(HPET_Tn_CMP(i));
147 h = hpet_readl(HPET_Tn_CMP(i)+4);
148 printk(KERN_INFO "hpet: T%d: CMP_l: 0x%x, CMP_h: 0x%x\n",
149 i, l, h);
150 l = hpet_readl(HPET_Tn_ROUTE(i));
151 h = hpet_readl(HPET_Tn_ROUTE(i)+4);
152 printk(KERN_INFO "hpet: T%d ROUTE_l: 0x%x, ROUTE_h: 0x%x\n",
153 i, l, h);
154 }
155}
156
157#define hpet_print_config() \
158do { \
159 if (hpet_verbose) \
160 _hpet_print_config(__FUNCTION__, __LINE__); \
161} while (0)
162
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800163/*
164 * When the hpet driver (/dev/hpet) is enabled, we need to reserve
165 * timer 0 and timer 1 in case of RTC emulation.
166 */
167#ifdef CONFIG_HPET
Venki Pallipadif0ed4e62008-09-08 10:18:40 -0700168
Venki Pallipadi5f79f2f2008-09-24 10:03:17 -0700169static void hpet_reserve_msi_timers(struct hpet_data *hd);
Venki Pallipadif0ed4e62008-09-08 10:18:40 -0700170
Jan Beulich5946fa32009-08-19 08:44:24 +0100171static void hpet_reserve_platform_timers(unsigned int id)
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800172{
173 struct hpet __iomem *hpet = hpet_virt_address;
Balaji Rao37a47db82008-01-30 13:30:03 +0100174 struct hpet_timer __iomem *timer = &hpet->hpet_timers[2];
175 unsigned int nrtimers, i;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800176 struct hpet_data hd;
177
178 nrtimers = ((id & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT) + 1;
179
Ingo Molnar4588c1f2008-09-06 14:19:17 +0200180 memset(&hd, 0, sizeof(hd));
181 hd.hd_phys_address = hpet_address;
182 hd.hd_address = hpet;
183 hd.hd_nirqs = nrtimers;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800184 hpet_reserve_timer(&hd, 0);
185
186#ifdef CONFIG_HPET_EMULATE_RTC
187 hpet_reserve_timer(&hd, 1);
188#endif
Thomas Gleixner5761d642008-04-04 16:26:10 +0200189
David Brownell64a76f62008-07-29 12:47:38 -0700190 /*
191 * NOTE that hd_irq[] reflects IOAPIC input pins (LEGACY_8254
192 * is wrong for i8259!) not the output IRQ. Many BIOS writers
193 * don't bother configuring *any* comparator interrupts.
194 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800195 hd.hd_irq[0] = HPET_LEGACY_8254;
196 hd.hd_irq[1] = HPET_LEGACY_RTC;
197
Ingo Molnarfc3fbc42008-04-27 14:04:14 +0200198 for (i = 2; i < nrtimers; timer++, i++) {
Ingo Molnar4588c1f2008-09-06 14:19:17 +0200199 hd.hd_irq[i] = (readl(&timer->hpet_config) &
200 Tn_INT_ROUTE_CNF_MASK) >> Tn_INT_ROUTE_CNF_SHIFT;
Ingo Molnarfc3fbc42008-04-27 14:04:14 +0200201 }
Thomas Gleixner5761d642008-04-04 16:26:10 +0200202
Venki Pallipadif0ed4e62008-09-08 10:18:40 -0700203 hpet_reserve_msi_timers(&hd);
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700204
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800205 hpet_alloc(&hd);
Thomas Gleixner5761d642008-04-04 16:26:10 +0200206
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800207}
208#else
Jan Beulich5946fa32009-08-19 08:44:24 +0100209static void hpet_reserve_platform_timers(unsigned int id) { }
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800210#endif
211
212/*
213 * Common hpet info
214 */
215static unsigned long hpet_period;
216
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200217static void hpet_legacy_set_mode(enum clock_event_mode mode,
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800218 struct clock_event_device *evt);
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200219static int hpet_legacy_next_event(unsigned long delta,
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800220 struct clock_event_device *evt);
221
222/*
223 * The hpet clock event device
224 */
225static struct clock_event_device hpet_clockevent = {
226 .name = "hpet",
227 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200228 .set_mode = hpet_legacy_set_mode,
229 .set_next_event = hpet_legacy_next_event,
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800230 .shift = 32,
231 .irq = 0,
Venki Pallipadi59c69f22007-10-12 23:04:23 +0200232 .rating = 50,
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800233};
234
Andreas Herrmann8d6f0c82009-02-21 00:10:44 +0100235static void hpet_stop_counter(void)
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800236{
237 unsigned long cfg = hpet_readl(HPET_CFG);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800238 cfg &= ~HPET_CFG_ENABLE;
239 hpet_writel(cfg, HPET_CFG);
Andreas Herrmann7a6f9cb2009-04-21 20:00:37 +0200240}
241
242static void hpet_reset_counter(void)
243{
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800244 hpet_writel(0, HPET_COUNTER);
245 hpet_writel(0, HPET_COUNTER + 4);
Andreas Herrmann8d6f0c82009-02-21 00:10:44 +0100246}
247
248static void hpet_start_counter(void)
249{
Jan Beulich5946fa32009-08-19 08:44:24 +0100250 unsigned int cfg = hpet_readl(HPET_CFG);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800251 cfg |= HPET_CFG_ENABLE;
252 hpet_writel(cfg, HPET_CFG);
253}
254
Andreas Herrmann8d6f0c82009-02-21 00:10:44 +0100255static void hpet_restart_counter(void)
256{
257 hpet_stop_counter();
Andreas Herrmann7a6f9cb2009-04-21 20:00:37 +0200258 hpet_reset_counter();
Andreas Herrmann8d6f0c82009-02-21 00:10:44 +0100259 hpet_start_counter();
260}
261
Venki Pallipadi59c69f22007-10-12 23:04:23 +0200262static void hpet_resume_device(void)
263{
Venki Pallipadibfe0c1c2007-10-12 23:04:24 +0200264 force_hpet_resume();
Venki Pallipadi59c69f22007-10-12 23:04:23 +0200265}
266
Andreas Herrmann8d6f0c82009-02-21 00:10:44 +0100267static void hpet_resume_counter(void)
Venki Pallipadi59c69f22007-10-12 23:04:23 +0200268{
269 hpet_resume_device();
Andreas Herrmann8d6f0c82009-02-21 00:10:44 +0100270 hpet_restart_counter();
Venki Pallipadi59c69f22007-10-12 23:04:23 +0200271}
272
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200273static void hpet_enable_legacy_int(void)
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800274{
Jan Beulich5946fa32009-08-19 08:44:24 +0100275 unsigned int cfg = hpet_readl(HPET_CFG);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800276
277 cfg |= HPET_CFG_LEGACY;
278 hpet_writel(cfg, HPET_CFG);
279 hpet_legacy_int_enabled = 1;
280}
281
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200282static void hpet_legacy_clockevent_register(void)
283{
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200284 /* Start HPET legacy interrupts */
285 hpet_enable_legacy_int();
286
287 /*
Carlos R. Mafra6fd592d2008-05-05 20:11:22 -0300288 * The mult factor is defined as (include/linux/clockchips.h)
289 * mult/2^shift = cyc/ns (in contrast to ns/cyc in clocksource.h)
290 * hpet_period is in units of femtoseconds (per cycle), so
291 * mult/2^shift = cyc/ns = 10^6/hpet_period
292 * mult = (10^6 * 2^shift)/hpet_period
293 * mult = (FSEC_PER_NSEC << hpet_clockevent.shift)/hpet_period
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200294 */
Carlos R. Mafra6fd592d2008-05-05 20:11:22 -0300295 hpet_clockevent.mult = div_sc((unsigned long) FSEC_PER_NSEC,
296 hpet_period, hpet_clockevent.shift);
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200297 /* Calculate the min / max delta */
298 hpet_clockevent.max_delta_ns = clockevent_delta2ns(0x7FFFFFFF,
299 &hpet_clockevent);
Thomas Gleixner7cfb04352008-09-03 21:37:24 +0000300 /* 5 usec minimum reprogramming delta. */
301 hpet_clockevent.min_delta_ns = 5000;
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200302
303 /*
304 * Start hpet with the boot cpu mask and make it
305 * global after the IO_APIC has been initialized.
306 */
Rusty Russell320ab2b2008-12-13 21:20:26 +1030307 hpet_clockevent.cpumask = cpumask_of(smp_processor_id());
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200308 clockevents_register_device(&hpet_clockevent);
309 global_clock_event = &hpet_clockevent;
310 printk(KERN_DEBUG "hpet clockevent registered\n");
311}
312
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700313static int hpet_setup_msi_irq(unsigned int irq);
314
venkatesh.pallipadi@intel.comb40d5752008-09-05 18:02:16 -0700315static void hpet_set_mode(enum clock_event_mode mode,
316 struct clock_event_device *evt, int timer)
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800317{
Jan Beulich5946fa32009-08-19 08:44:24 +0100318 unsigned int cfg, cmp, now;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800319 uint64_t delta;
320
Ingo Molnar4588c1f2008-09-06 14:19:17 +0200321 switch (mode) {
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800322 case CLOCK_EVT_MODE_PERIODIC:
Andreas Herrmannc23e2532009-02-21 00:16:35 +0100323 hpet_stop_counter();
venkatesh.pallipadi@intel.comb40d5752008-09-05 18:02:16 -0700324 delta = ((uint64_t)(NSEC_PER_SEC/HZ)) * evt->mult;
325 delta >>= evt->shift;
Andreas Herrmann7a6f9cb2009-04-21 20:00:37 +0200326 now = hpet_readl(HPET_COUNTER);
Jan Beulich5946fa32009-08-19 08:44:24 +0100327 cmp = now + (unsigned int) delta;
venkatesh.pallipadi@intel.comb40d5752008-09-05 18:02:16 -0700328 cfg = hpet_readl(HPET_Tn_CFG(timer));
john stultzb13e2462009-02-12 18:48:53 -0800329 /* Make sure we use edge triggered interrupts */
330 cfg &= ~HPET_TN_LEVEL;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800331 cfg |= HPET_TN_ENABLE | HPET_TN_PERIODIC |
332 HPET_TN_SETVAL | HPET_TN_32BIT;
venkatesh.pallipadi@intel.comb40d5752008-09-05 18:02:16 -0700333 hpet_writel(cfg, HPET_Tn_CFG(timer));
Andreas Herrmann7a6f9cb2009-04-21 20:00:37 +0200334 hpet_writel(cmp, HPET_Tn_CMP(timer));
335 udelay(1);
336 /*
337 * HPET on AMD 81xx needs a second write (with HPET_TN_SETVAL
338 * cleared) to T0_CMP to set the period. The HPET_TN_SETVAL
339 * bit is automatically cleared after the first write.
340 * (See AMD-8111 HyperTransport I/O Hub Data Sheet,
341 * Publication # 24674)
342 */
Jan Beulich5946fa32009-08-19 08:44:24 +0100343 hpet_writel((unsigned int) delta, HPET_Tn_CMP(timer));
Andreas Herrmannc23e2532009-02-21 00:16:35 +0100344 hpet_start_counter();
Andreas Herrmannb98103a2009-02-21 00:09:47 +0100345 hpet_print_config();
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800346 break;
347
348 case CLOCK_EVT_MODE_ONESHOT:
venkatesh.pallipadi@intel.comb40d5752008-09-05 18:02:16 -0700349 cfg = hpet_readl(HPET_Tn_CFG(timer));
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800350 cfg &= ~HPET_TN_PERIODIC;
351 cfg |= HPET_TN_ENABLE | HPET_TN_32BIT;
venkatesh.pallipadi@intel.comb40d5752008-09-05 18:02:16 -0700352 hpet_writel(cfg, HPET_Tn_CFG(timer));
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800353 break;
354
355 case CLOCK_EVT_MODE_UNUSED:
356 case CLOCK_EVT_MODE_SHUTDOWN:
venkatesh.pallipadi@intel.comb40d5752008-09-05 18:02:16 -0700357 cfg = hpet_readl(HPET_Tn_CFG(timer));
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800358 cfg &= ~HPET_TN_ENABLE;
venkatesh.pallipadi@intel.comb40d5752008-09-05 18:02:16 -0700359 hpet_writel(cfg, HPET_Tn_CFG(timer));
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800360 break;
Thomas Gleixner18de5bc2007-07-21 04:37:34 -0700361
362 case CLOCK_EVT_MODE_RESUME:
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700363 if (timer == 0) {
364 hpet_enable_legacy_int();
365 } else {
366 struct hpet_dev *hdev = EVT_TO_HPET_DEV(evt);
367 hpet_setup_msi_irq(hdev->irq);
368 disable_irq(hdev->irq);
Rusty Russell0de26522008-12-13 21:20:26 +1030369 irq_set_affinity(hdev->irq, cpumask_of(hdev->cpu));
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700370 enable_irq(hdev->irq);
371 }
Andreas Herrmannb98103a2009-02-21 00:09:47 +0100372 hpet_print_config();
Thomas Gleixner18de5bc2007-07-21 04:37:34 -0700373 break;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800374 }
375}
376
venkatesh.pallipadi@intel.comb40d5752008-09-05 18:02:16 -0700377static int hpet_next_event(unsigned long delta,
378 struct clock_event_device *evt, int timer)
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800379{
Thomas Gleixnerf7676252008-09-06 03:03:32 +0200380 u32 cnt;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800381
382 cnt = hpet_readl(HPET_COUNTER);
Thomas Gleixnerf7676252008-09-06 03:03:32 +0200383 cnt += (u32) delta;
venkatesh.pallipadi@intel.comb40d5752008-09-05 18:02:16 -0700384 hpet_writel(cnt, HPET_Tn_CMP(timer));
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800385
Thomas Gleixner72d43d92008-09-06 03:06:08 +0200386 /*
Thomas Gleixner18ed61d2009-11-27 15:24:44 +0100387 * We need to read back the CMP register on certain HPET
388 * implementations (ATI chipsets) which seem to delay the
389 * transfer of the compare register into the internal compare
390 * logic. With small deltas this might actually be too late as
391 * the counter could already be higher than the compare value
392 * at that point and we would wait for the next hpet interrupt
393 * forever. We found out that reading the CMP register back
394 * forces the transfer so we can rely on the comparison with
395 * the counter register below. If the read back from the
396 * compare register does not match the value we programmed
397 * then we might have a real hardware problem. We can not do
398 * much about it here, but at least alert the user/admin with
399 * a prominent warning.
Thomas Gleixner72d43d92008-09-06 03:06:08 +0200400 */
Thomas Gleixner18ed61d2009-11-27 15:24:44 +0100401 WARN_ONCE(hpet_readl(HPET_Tn_CMP(timer)) != cnt,
402 KERN_WARNING "hpet: compare register read back failed.\n");
Thomas Gleixner72d43d92008-09-06 03:06:08 +0200403
Jan Beulich5946fa32009-08-19 08:44:24 +0100404 return (s32)(hpet_readl(HPET_COUNTER) - cnt) >= 0 ? -ETIME : 0;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800405}
406
venkatesh.pallipadi@intel.comb40d5752008-09-05 18:02:16 -0700407static void hpet_legacy_set_mode(enum clock_event_mode mode,
408 struct clock_event_device *evt)
409{
410 hpet_set_mode(mode, evt, 0);
411}
412
413static int hpet_legacy_next_event(unsigned long delta,
414 struct clock_event_device *evt)
415{
416 return hpet_next_event(delta, evt, 0);
417}
418
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800419/*
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -0700420 * HPET MSI Support
421 */
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700422#ifdef CONFIG_PCI_MSI
Venki Pallipadi5f79f2f2008-09-24 10:03:17 -0700423
424static DEFINE_PER_CPU(struct hpet_dev *, cpu_hpet_dev);
425static struct hpet_dev *hpet_devs;
426
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -0700427void hpet_msi_unmask(unsigned int irq)
428{
429 struct hpet_dev *hdev = get_irq_data(irq);
Jan Beulich5946fa32009-08-19 08:44:24 +0100430 unsigned int cfg;
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -0700431
432 /* unmask it */
433 cfg = hpet_readl(HPET_Tn_CFG(hdev->num));
434 cfg |= HPET_TN_FSB;
435 hpet_writel(cfg, HPET_Tn_CFG(hdev->num));
436}
437
438void hpet_msi_mask(unsigned int irq)
439{
Jan Beulich5946fa32009-08-19 08:44:24 +0100440 unsigned int cfg;
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -0700441 struct hpet_dev *hdev = get_irq_data(irq);
442
443 /* mask it */
444 cfg = hpet_readl(HPET_Tn_CFG(hdev->num));
445 cfg &= ~HPET_TN_FSB;
446 hpet_writel(cfg, HPET_Tn_CFG(hdev->num));
447}
448
449void hpet_msi_write(unsigned int irq, struct msi_msg *msg)
450{
451 struct hpet_dev *hdev = get_irq_data(irq);
452
453 hpet_writel(msg->data, HPET_Tn_ROUTE(hdev->num));
454 hpet_writel(msg->address_lo, HPET_Tn_ROUTE(hdev->num) + 4);
455}
456
457void hpet_msi_read(unsigned int irq, struct msi_msg *msg)
458{
459 struct hpet_dev *hdev = get_irq_data(irq);
460
461 msg->data = hpet_readl(HPET_Tn_ROUTE(hdev->num));
462 msg->address_lo = hpet_readl(HPET_Tn_ROUTE(hdev->num) + 4);
463 msg->address_hi = 0;
464}
465
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700466static void hpet_msi_set_mode(enum clock_event_mode mode,
467 struct clock_event_device *evt)
468{
469 struct hpet_dev *hdev = EVT_TO_HPET_DEV(evt);
470 hpet_set_mode(mode, evt, hdev->num);
471}
472
473static int hpet_msi_next_event(unsigned long delta,
474 struct clock_event_device *evt)
475{
476 struct hpet_dev *hdev = EVT_TO_HPET_DEV(evt);
477 return hpet_next_event(delta, evt, hdev->num);
478}
479
480static int hpet_setup_msi_irq(unsigned int irq)
481{
Suresh Siddhac8bc6f32009-08-04 12:07:09 -0700482 if (arch_setup_hpet_msi(irq, hpet_blockid)) {
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700483 destroy_irq(irq);
484 return -EINVAL;
485 }
486 return 0;
487}
488
489static int hpet_assign_irq(struct hpet_dev *dev)
490{
491 unsigned int irq;
492
493 irq = create_irq();
494 if (!irq)
495 return -EINVAL;
496
497 set_irq_data(irq, dev);
498
499 if (hpet_setup_msi_irq(irq))
500 return -EINVAL;
501
502 dev->irq = irq;
503 return 0;
504}
505
506static irqreturn_t hpet_interrupt_handler(int irq, void *data)
507{
508 struct hpet_dev *dev = (struct hpet_dev *)data;
509 struct clock_event_device *hevt = &dev->evt;
510
511 if (!hevt->event_handler) {
512 printk(KERN_INFO "Spurious HPET timer interrupt on HPET timer %d\n",
513 dev->num);
514 return IRQ_HANDLED;
515 }
516
517 hevt->event_handler(hevt);
518 return IRQ_HANDLED;
519}
520
521static int hpet_setup_irq(struct hpet_dev *dev)
522{
523
524 if (request_irq(dev->irq, hpet_interrupt_handler,
Thomas Gleixner507fa3a2009-06-14 17:46:01 +0200525 IRQF_TIMER | IRQF_DISABLED | IRQF_NOBALANCING,
526 dev->name, dev))
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700527 return -1;
528
529 disable_irq(dev->irq);
Rusty Russell0de26522008-12-13 21:20:26 +1030530 irq_set_affinity(dev->irq, cpumask_of(dev->cpu));
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700531 enable_irq(dev->irq);
532
Yinghai Luc81bba42008-09-25 11:53:11 -0700533 printk(KERN_DEBUG "hpet: %s irq %d for MSI\n",
534 dev->name, dev->irq);
535
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700536 return 0;
537}
538
539/* This should be called in specific @cpu */
540static void init_one_hpet_msi_clockevent(struct hpet_dev *hdev, int cpu)
541{
542 struct clock_event_device *evt = &hdev->evt;
543 uint64_t hpet_freq;
544
545 WARN_ON(cpu != smp_processor_id());
546 if (!(hdev->flags & HPET_DEV_VALID))
547 return;
548
549 if (hpet_setup_msi_irq(hdev->irq))
550 return;
551
552 hdev->cpu = cpu;
553 per_cpu(cpu_hpet_dev, cpu) = hdev;
554 evt->name = hdev->name;
555 hpet_setup_irq(hdev);
556 evt->irq = hdev->irq;
557
558 evt->rating = 110;
559 evt->features = CLOCK_EVT_FEAT_ONESHOT;
560 if (hdev->flags & HPET_DEV_PERI_CAP)
561 evt->features |= CLOCK_EVT_FEAT_PERIODIC;
562
563 evt->set_mode = hpet_msi_set_mode;
564 evt->set_next_event = hpet_msi_next_event;
565 evt->shift = 32;
566
567 /*
568 * The period is a femto seconds value. We need to calculate the
569 * scaled math multiplication factor for nanosecond to hpet tick
570 * conversion.
571 */
572 hpet_freq = 1000000000000000ULL;
573 do_div(hpet_freq, hpet_period);
574 evt->mult = div_sc((unsigned long) hpet_freq,
575 NSEC_PER_SEC, evt->shift);
576 /* Calculate the max delta */
577 evt->max_delta_ns = clockevent_delta2ns(0x7FFFFFFF, evt);
578 /* 5 usec minimum reprogramming delta. */
579 evt->min_delta_ns = 5000;
580
Rusty Russell320ab2b2008-12-13 21:20:26 +1030581 evt->cpumask = cpumask_of(hdev->cpu);
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700582 clockevents_register_device(evt);
583}
584
585#ifdef CONFIG_HPET
586/* Reserve at least one timer for userspace (/dev/hpet) */
587#define RESERVE_TIMERS 1
588#else
589#define RESERVE_TIMERS 0
590#endif
Venki Pallipadi5f79f2f2008-09-24 10:03:17 -0700591
592static void hpet_msi_capability_lookup(unsigned int start_timer)
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700593{
594 unsigned int id;
595 unsigned int num_timers;
596 unsigned int num_timers_used = 0;
597 int i;
598
Shaohua Li39fe05e2009-08-12 11:16:12 +0800599 if (boot_cpu_has(X86_FEATURE_ARAT))
600 return;
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700601 id = hpet_readl(HPET_ID);
602
603 num_timers = ((id & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT);
604 num_timers++; /* Value read out starts from 0 */
Andreas Herrmannb98103a2009-02-21 00:09:47 +0100605 hpet_print_config();
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700606
607 hpet_devs = kzalloc(sizeof(struct hpet_dev) * num_timers, GFP_KERNEL);
608 if (!hpet_devs)
609 return;
610
611 hpet_num_timers = num_timers;
612
613 for (i = start_timer; i < num_timers - RESERVE_TIMERS; i++) {
614 struct hpet_dev *hdev = &hpet_devs[num_timers_used];
Jan Beulich5946fa32009-08-19 08:44:24 +0100615 unsigned int cfg = hpet_readl(HPET_Tn_CFG(i));
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700616
617 /* Only consider HPET timer with MSI support */
618 if (!(cfg & HPET_TN_FSB_CAP))
619 continue;
620
621 hdev->flags = 0;
622 if (cfg & HPET_TN_PERIODIC_CAP)
623 hdev->flags |= HPET_DEV_PERI_CAP;
624 hdev->num = i;
625
626 sprintf(hdev->name, "hpet%d", i);
627 if (hpet_assign_irq(hdev))
628 continue;
629
630 hdev->flags |= HPET_DEV_FSB_CAP;
631 hdev->flags |= HPET_DEV_VALID;
632 num_timers_used++;
633 if (num_timers_used == num_possible_cpus())
634 break;
635 }
636
637 printk(KERN_INFO "HPET: %d timers in total, %d timers will be used for per-cpu timer\n",
638 num_timers, num_timers_used);
639}
640
Venki Pallipadi5f79f2f2008-09-24 10:03:17 -0700641#ifdef CONFIG_HPET
642static void hpet_reserve_msi_timers(struct hpet_data *hd)
643{
644 int i;
645
646 if (!hpet_devs)
647 return;
648
649 for (i = 0; i < hpet_num_timers; i++) {
650 struct hpet_dev *hdev = &hpet_devs[i];
651
652 if (!(hdev->flags & HPET_DEV_VALID))
653 continue;
654
655 hd->hd_irq[hdev->num] = hdev->irq;
656 hpet_reserve_timer(hd, hdev->num);
657 }
658}
659#endif
660
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700661static struct hpet_dev *hpet_get_unused_timer(void)
662{
663 int i;
664
665 if (!hpet_devs)
666 return NULL;
667
668 for (i = 0; i < hpet_num_timers; i++) {
669 struct hpet_dev *hdev = &hpet_devs[i];
670
671 if (!(hdev->flags & HPET_DEV_VALID))
672 continue;
673 if (test_and_set_bit(HPET_DEV_USED_BIT,
674 (unsigned long *)&hdev->flags))
675 continue;
676 return hdev;
677 }
678 return NULL;
679}
680
681struct hpet_work_struct {
682 struct delayed_work work;
683 struct completion complete;
684};
685
686static void hpet_work(struct work_struct *w)
687{
688 struct hpet_dev *hdev;
689 int cpu = smp_processor_id();
690 struct hpet_work_struct *hpet_work;
691
692 hpet_work = container_of(w, struct hpet_work_struct, work.work);
693
694 hdev = hpet_get_unused_timer();
695 if (hdev)
696 init_one_hpet_msi_clockevent(hdev, cpu);
697
698 complete(&hpet_work->complete);
699}
700
701static int hpet_cpuhp_notify(struct notifier_block *n,
702 unsigned long action, void *hcpu)
703{
704 unsigned long cpu = (unsigned long)hcpu;
705 struct hpet_work_struct work;
706 struct hpet_dev *hdev = per_cpu(cpu_hpet_dev, cpu);
707
708 switch (action & 0xf) {
709 case CPU_ONLINE:
Thomas Gleixner336f6c32009-01-22 09:50:44 +0100710 INIT_DELAYED_WORK_ON_STACK(&work.work, hpet_work);
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700711 init_completion(&work.complete);
712 /* FIXME: add schedule_work_on() */
713 schedule_delayed_work_on(cpu, &work.work, 0);
714 wait_for_completion(&work.complete);
Thomas Gleixner336f6c32009-01-22 09:50:44 +0100715 destroy_timer_on_stack(&work.work.timer);
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700716 break;
717 case CPU_DEAD:
718 if (hdev) {
719 free_irq(hdev->irq, hdev);
720 hdev->flags &= ~HPET_DEV_USED;
721 per_cpu(cpu_hpet_dev, cpu) = NULL;
722 }
723 break;
724 }
725 return NOTIFY_OK;
726}
727#else
728
Steven Noonanba374c92008-09-08 16:19:09 -0700729static int hpet_setup_msi_irq(unsigned int irq)
730{
731 return 0;
732}
Venki Pallipadi5f79f2f2008-09-24 10:03:17 -0700733static void hpet_msi_capability_lookup(unsigned int start_timer)
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700734{
735 return;
736}
737
Venki Pallipadi5f79f2f2008-09-24 10:03:17 -0700738#ifdef CONFIG_HPET
739static void hpet_reserve_msi_timers(struct hpet_data *hd)
740{
741 return;
742}
743#endif
744
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700745static int hpet_cpuhp_notify(struct notifier_block *n,
746 unsigned long action, void *hcpu)
747{
748 return NOTIFY_OK;
749}
750
751#endif
752
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -0700753/*
john stultz6bb74df2007-03-05 00:30:50 -0800754 * Clock source related code
755 */
Magnus Damm8e196082009-04-21 12:24:00 -0700756static cycle_t read_hpet(struct clocksource *cs)
john stultz6bb74df2007-03-05 00:30:50 -0800757{
758 return (cycle_t)hpet_readl(HPET_COUNTER);
759}
760
Thomas Gleixner28769142007-10-12 23:04:06 +0200761#ifdef CONFIG_X86_64
762static cycle_t __vsyscall_fn vread_hpet(void)
763{
764 return readl((const void __iomem *)fix_to_virt(VSYSCALL_HPET) + 0xf0);
765}
766#endif
767
john stultz6bb74df2007-03-05 00:30:50 -0800768static struct clocksource clocksource_hpet = {
769 .name = "hpet",
770 .rating = 250,
771 .read = read_hpet,
772 .mask = HPET_MASK,
773 .shift = HPET_SHIFT,
774 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
Andreas Herrmann8d6f0c82009-02-21 00:10:44 +0100775 .resume = hpet_resume_counter,
Thomas Gleixner28769142007-10-12 23:04:06 +0200776#ifdef CONFIG_X86_64
777 .vread = vread_hpet,
778#endif
john stultz6bb74df2007-03-05 00:30:50 -0800779};
780
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200781static int hpet_clocksource_register(void)
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800782{
Carlos R. Mafra6fd592d2008-05-05 20:11:22 -0300783 u64 start, now;
Thomas Gleixner075bcd12007-07-21 17:11:12 +0200784 cycle_t t1;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800785
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800786 /* Start the counter */
Andreas Herrmann8d6f0c82009-02-21 00:10:44 +0100787 hpet_restart_counter();
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800788
Thomas Gleixner075bcd12007-07-21 17:11:12 +0200789 /* Verify whether hpet counter works */
Magnus Damm8e196082009-04-21 12:24:00 -0700790 t1 = hpet_readl(HPET_COUNTER);
Thomas Gleixner075bcd12007-07-21 17:11:12 +0200791 rdtscll(start);
792
793 /*
794 * We don't know the TSC frequency yet, but waiting for
795 * 200000 TSC cycles is safe:
796 * 4 GHz == 50us
797 * 1 GHz == 200us
798 */
799 do {
800 rep_nop();
801 rdtscll(now);
802 } while ((now - start) < 200000UL);
803
Magnus Damm8e196082009-04-21 12:24:00 -0700804 if (t1 == hpet_readl(HPET_COUNTER)) {
Thomas Gleixner075bcd12007-07-21 17:11:12 +0200805 printk(KERN_WARNING
806 "HPET counter not counting. HPET disabled\n");
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200807 return -ENODEV;
Thomas Gleixner075bcd12007-07-21 17:11:12 +0200808 }
809
Carlos R. Mafra6fd592d2008-05-05 20:11:22 -0300810 /*
811 * The definition of mult is (include/linux/clocksource.h)
812 * mult/2^shift = ns/cyc and hpet_period is in units of fsec/cyc
813 * so we first need to convert hpet_period to ns/cyc units:
814 * mult/2^shift = ns/cyc = hpet_period/10^6
815 * mult = (hpet_period * 2^shift)/10^6
816 * mult = (hpet_period << shift)/FSEC_PER_NSEC
john stultz6bb74df2007-03-05 00:30:50 -0800817 */
Carlos R. Mafra6fd592d2008-05-05 20:11:22 -0300818 clocksource_hpet.mult = div_sc(hpet_period, FSEC_PER_NSEC, HPET_SHIFT);
john stultz6bb74df2007-03-05 00:30:50 -0800819
820 clocksource_register(&clocksource_hpet);
821
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200822 return 0;
823}
824
Pavel Machekb02a7f22008-02-05 00:48:13 +0100825/**
826 * hpet_enable - Try to setup the HPET timer. Returns 1 on success.
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200827 */
828int __init hpet_enable(void)
829{
Jan Beulich5946fa32009-08-19 08:44:24 +0100830 unsigned int id;
Thomas Gleixnera6825f12008-08-14 12:17:06 +0200831 int i;
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200832
833 if (!is_hpet_capable())
834 return 0;
835
836 hpet_set_mapping();
837
838 /*
839 * Read the period and check for a sane value:
840 */
841 hpet_period = hpet_readl(HPET_PERIOD);
Thomas Gleixnera6825f12008-08-14 12:17:06 +0200842
843 /*
844 * AMD SB700 based systems with spread spectrum enabled use a
845 * SMM based HPET emulation to provide proper frequency
846 * setting. The SMM code is initialized with the first HPET
847 * register access and takes some time to complete. During
848 * this time the config register reads 0xffffffff. We check
849 * for max. 1000 loops whether the config register reads a non
850 * 0xffffffff value to make sure that HPET is up and running
851 * before we go further. A counting loop is safe, as the HPET
852 * access takes thousands of CPU cycles. On non SB700 based
853 * machines this check is only done once and has no side
854 * effects.
855 */
856 for (i = 0; hpet_readl(HPET_CFG) == 0xFFFFFFFF; i++) {
857 if (i == 1000) {
858 printk(KERN_WARNING
859 "HPET config register value = 0xFFFFFFFF. "
860 "Disabling HPET\n");
861 goto out_nohpet;
862 }
863 }
864
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200865 if (hpet_period < HPET_MIN_PERIOD || hpet_period > HPET_MAX_PERIOD)
866 goto out_nohpet;
867
868 /*
869 * Read the HPET ID register to retrieve the IRQ routing
870 * information and the number of channels
871 */
872 id = hpet_readl(HPET_ID);
Andreas Herrmannb98103a2009-02-21 00:09:47 +0100873 hpet_print_config();
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200874
875#ifdef CONFIG_HPET_EMULATE_RTC
876 /*
877 * The legacy routing mode needs at least two channels, tick timer
878 * and the rtc emulation channel.
879 */
880 if (!(id & HPET_ID_NUMBER))
881 goto out_nohpet;
882#endif
883
884 if (hpet_clocksource_register())
885 goto out_nohpet;
886
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800887 if (id & HPET_ID_LEGSUP) {
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200888 hpet_legacy_clockevent_register();
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800889 return 1;
890 }
891 return 0;
892
893out_nohpet:
Thomas Gleixner06a24de2007-10-12 23:04:06 +0200894 hpet_clear_mapping();
Janne Kulmalabacbe992008-12-16 13:39:57 +0200895 hpet_address = 0;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800896 return 0;
897}
898
Thomas Gleixner28769142007-10-12 23:04:06 +0200899/*
900 * Needs to be late, as the reserve_timer code calls kalloc !
901 *
902 * Not a problem on i386 as hpet_enable is called from late_time_init,
903 * but on x86_64 it is necessary !
904 */
905static __init int hpet_late_init(void)
906{
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700907 int cpu;
908
Venki Pallipadi59c69f22007-10-12 23:04:23 +0200909 if (boot_hpet_disable)
Thomas Gleixner28769142007-10-12 23:04:06 +0200910 return -ENODEV;
911
Venki Pallipadi59c69f22007-10-12 23:04:23 +0200912 if (!hpet_address) {
913 if (!force_hpet_address)
914 return -ENODEV;
915
916 hpet_address = force_hpet_address;
917 hpet_enable();
Venki Pallipadi59c69f22007-10-12 23:04:23 +0200918 }
919
Jeremy Fitzhardinge39c04b52008-12-16 12:32:23 -0800920 if (!hpet_virt_address)
921 return -ENODEV;
922
Shaohua Li39fe05e2009-08-12 11:16:12 +0800923 if (hpet_readl(HPET_ID) & HPET_ID_LEGSUP)
924 hpet_msi_capability_lookup(2);
925 else
926 hpet_msi_capability_lookup(0);
927
Thomas Gleixner28769142007-10-12 23:04:06 +0200928 hpet_reserve_platform_timers(hpet_readl(HPET_ID));
Andreas Herrmannb98103a2009-02-21 00:09:47 +0100929 hpet_print_config();
Venki Pallipadi59c69f22007-10-12 23:04:23 +0200930
Shaohua Li39fe05e2009-08-12 11:16:12 +0800931 if (boot_cpu_has(X86_FEATURE_ARAT))
932 return 0;
933
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700934 for_each_online_cpu(cpu) {
935 hpet_cpuhp_notify(NULL, CPU_ONLINE, (void *)(long)cpu);
936 }
937
938 /* This notifier should be called after workqueue is ready */
939 hotcpu_notifier(hpet_cpuhp_notify, -20);
940
Thomas Gleixner28769142007-10-12 23:04:06 +0200941 return 0;
942}
943fs_initcall(hpet_late_init);
944
OGAWA Hirofumic86c7fb2007-12-03 17:17:10 +0100945void hpet_disable(void)
946{
947 if (is_hpet_capable()) {
Jan Beulich5946fa32009-08-19 08:44:24 +0100948 unsigned int cfg = hpet_readl(HPET_CFG);
OGAWA Hirofumic86c7fb2007-12-03 17:17:10 +0100949
950 if (hpet_legacy_int_enabled) {
951 cfg &= ~HPET_CFG_LEGACY;
952 hpet_legacy_int_enabled = 0;
953 }
954 cfg &= ~HPET_CFG_ENABLE;
955 hpet_writel(cfg, HPET_CFG);
956 }
957}
958
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800959#ifdef CONFIG_HPET_EMULATE_RTC
960
961/* HPET in LegacyReplacement Mode eats up RTC interrupt line. When, HPET
962 * is enabled, we support RTC interrupt functionality in software.
963 * RTC has 3 kinds of interrupts:
964 * 1) Update Interrupt - generate an interrupt, every sec, when RTC clock
965 * is updated
966 * 2) Alarm Interrupt - generate an interrupt at a specific time of day
967 * 3) Periodic Interrupt - generate periodic interrupt, with frequencies
968 * 2Hz-8192Hz (2Hz-64Hz for non-root user) (all freqs in powers of 2)
969 * (1) and (2) above are implemented using polling at a frequency of
970 * 64 Hz. The exact frequency is a tradeoff between accuracy and interrupt
971 * overhead. (DEFAULT_RTC_INT_FREQ)
972 * For (3), we use interrupts at 64Hz or user specified periodic
973 * frequency, whichever is higher.
974 */
975#include <linux/mc146818rtc.h>
976#include <linux/rtc.h>
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +0100977#include <asm/rtc.h>
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800978
979#define DEFAULT_RTC_INT_FREQ 64
980#define DEFAULT_RTC_SHIFT 6
981#define RTC_NUM_INTS 1
982
983static unsigned long hpet_rtc_flags;
David Brownell7e2a31d2008-07-23 21:30:47 -0700984static int hpet_prev_update_sec;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800985static struct rtc_time hpet_alarm_time;
986static unsigned long hpet_pie_count;
Pavel Emelyanovff08f762009-02-04 13:40:31 +0300987static u32 hpet_t1_cmp;
Jan Beulich5946fa32009-08-19 08:44:24 +0100988static u32 hpet_default_delta;
989static u32 hpet_pie_delta;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800990static unsigned long hpet_pie_limit;
991
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +0100992static rtc_irq_handler irq_handler;
993
994/*
Pavel Emelyanovff08f762009-02-04 13:40:31 +0300995 * Check that the hpet counter c1 is ahead of the c2
996 */
997static inline int hpet_cnt_ahead(u32 c1, u32 c2)
998{
999 return (s32)(c2 - c1) < 0;
1000}
1001
1002/*
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +01001003 * Registers a IRQ handler.
1004 */
1005int hpet_register_irq_handler(rtc_irq_handler handler)
1006{
1007 if (!is_hpet_enabled())
1008 return -ENODEV;
1009 if (irq_handler)
1010 return -EBUSY;
1011
1012 irq_handler = handler;
1013
1014 return 0;
1015}
1016EXPORT_SYMBOL_GPL(hpet_register_irq_handler);
1017
1018/*
1019 * Deregisters the IRQ handler registered with hpet_register_irq_handler()
1020 * and does cleanup.
1021 */
1022void hpet_unregister_irq_handler(rtc_irq_handler handler)
1023{
1024 if (!is_hpet_enabled())
1025 return;
1026
1027 irq_handler = NULL;
1028 hpet_rtc_flags = 0;
1029}
1030EXPORT_SYMBOL_GPL(hpet_unregister_irq_handler);
1031
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001032/*
1033 * Timer 1 for RTC emulation. We use one shot mode, as periodic mode
1034 * is not supported by all HPET implementations for timer 1.
1035 *
1036 * hpet_rtc_timer_init() is called when the rtc is initialized.
1037 */
1038int hpet_rtc_timer_init(void)
1039{
Jan Beulich5946fa32009-08-19 08:44:24 +01001040 unsigned int cfg, cnt, delta;
1041 unsigned long flags;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001042
1043 if (!is_hpet_enabled())
1044 return 0;
1045
1046 if (!hpet_default_delta) {
1047 uint64_t clc;
1048
1049 clc = (uint64_t) hpet_clockevent.mult * NSEC_PER_SEC;
1050 clc >>= hpet_clockevent.shift + DEFAULT_RTC_SHIFT;
Jan Beulich5946fa32009-08-19 08:44:24 +01001051 hpet_default_delta = clc;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001052 }
1053
1054 if (!(hpet_rtc_flags & RTC_PIE) || hpet_pie_limit)
1055 delta = hpet_default_delta;
1056 else
1057 delta = hpet_pie_delta;
1058
1059 local_irq_save(flags);
1060
1061 cnt = delta + hpet_readl(HPET_COUNTER);
1062 hpet_writel(cnt, HPET_T1_CMP);
1063 hpet_t1_cmp = cnt;
1064
1065 cfg = hpet_readl(HPET_T1_CFG);
1066 cfg &= ~HPET_TN_PERIODIC;
1067 cfg |= HPET_TN_ENABLE | HPET_TN_32BIT;
1068 hpet_writel(cfg, HPET_T1_CFG);
1069
1070 local_irq_restore(flags);
1071
1072 return 1;
1073}
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +01001074EXPORT_SYMBOL_GPL(hpet_rtc_timer_init);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001075
1076/*
1077 * The functions below are called from rtc driver.
1078 * Return 0 if HPET is not being used.
1079 * Otherwise do the necessary changes and return 1.
1080 */
1081int hpet_mask_rtc_irq_bit(unsigned long bit_mask)
1082{
1083 if (!is_hpet_enabled())
1084 return 0;
1085
1086 hpet_rtc_flags &= ~bit_mask;
1087 return 1;
1088}
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +01001089EXPORT_SYMBOL_GPL(hpet_mask_rtc_irq_bit);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001090
1091int hpet_set_rtc_irq_bit(unsigned long bit_mask)
1092{
1093 unsigned long oldbits = hpet_rtc_flags;
1094
1095 if (!is_hpet_enabled())
1096 return 0;
1097
1098 hpet_rtc_flags |= bit_mask;
1099
David Brownell7e2a31d2008-07-23 21:30:47 -07001100 if ((bit_mask & RTC_UIE) && !(oldbits & RTC_UIE))
1101 hpet_prev_update_sec = -1;
1102
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001103 if (!oldbits)
1104 hpet_rtc_timer_init();
1105
1106 return 1;
1107}
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +01001108EXPORT_SYMBOL_GPL(hpet_set_rtc_irq_bit);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001109
1110int hpet_set_alarm_time(unsigned char hrs, unsigned char min,
1111 unsigned char sec)
1112{
1113 if (!is_hpet_enabled())
1114 return 0;
1115
1116 hpet_alarm_time.tm_hour = hrs;
1117 hpet_alarm_time.tm_min = min;
1118 hpet_alarm_time.tm_sec = sec;
1119
1120 return 1;
1121}
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +01001122EXPORT_SYMBOL_GPL(hpet_set_alarm_time);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001123
1124int hpet_set_periodic_freq(unsigned long freq)
1125{
1126 uint64_t clc;
1127
1128 if (!is_hpet_enabled())
1129 return 0;
1130
1131 if (freq <= DEFAULT_RTC_INT_FREQ)
1132 hpet_pie_limit = DEFAULT_RTC_INT_FREQ / freq;
1133 else {
1134 clc = (uint64_t) hpet_clockevent.mult * NSEC_PER_SEC;
1135 do_div(clc, freq);
1136 clc >>= hpet_clockevent.shift;
Jan Beulich5946fa32009-08-19 08:44:24 +01001137 hpet_pie_delta = clc;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001138 }
1139 return 1;
1140}
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +01001141EXPORT_SYMBOL_GPL(hpet_set_periodic_freq);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001142
1143int hpet_rtc_dropped_irq(void)
1144{
1145 return is_hpet_enabled();
1146}
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +01001147EXPORT_SYMBOL_GPL(hpet_rtc_dropped_irq);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001148
1149static void hpet_rtc_timer_reinit(void)
1150{
Jan Beulich5946fa32009-08-19 08:44:24 +01001151 unsigned int cfg, delta;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001152 int lost_ints = -1;
1153
1154 if (unlikely(!hpet_rtc_flags)) {
1155 cfg = hpet_readl(HPET_T1_CFG);
1156 cfg &= ~HPET_TN_ENABLE;
1157 hpet_writel(cfg, HPET_T1_CFG);
1158 return;
1159 }
1160
1161 if (!(hpet_rtc_flags & RTC_PIE) || hpet_pie_limit)
1162 delta = hpet_default_delta;
1163 else
1164 delta = hpet_pie_delta;
1165
1166 /*
1167 * Increment the comparator value until we are ahead of the
1168 * current count.
1169 */
1170 do {
1171 hpet_t1_cmp += delta;
1172 hpet_writel(hpet_t1_cmp, HPET_T1_CMP);
1173 lost_ints++;
Pavel Emelyanovff08f762009-02-04 13:40:31 +03001174 } while (!hpet_cnt_ahead(hpet_t1_cmp, hpet_readl(HPET_COUNTER)));
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001175
1176 if (lost_ints) {
1177 if (hpet_rtc_flags & RTC_PIE)
1178 hpet_pie_count += lost_ints;
1179 if (printk_ratelimit())
David Brownell7e2a31d2008-07-23 21:30:47 -07001180 printk(KERN_WARNING "hpet1: lost %d rtc interrupts\n",
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001181 lost_ints);
1182 }
1183}
1184
1185irqreturn_t hpet_rtc_interrupt(int irq, void *dev_id)
1186{
1187 struct rtc_time curr_time;
1188 unsigned long rtc_int_flag = 0;
1189
1190 hpet_rtc_timer_reinit();
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +01001191 memset(&curr_time, 0, sizeof(struct rtc_time));
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001192
1193 if (hpet_rtc_flags & (RTC_UIE | RTC_AIE))
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +01001194 get_rtc_time(&curr_time);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001195
1196 if (hpet_rtc_flags & RTC_UIE &&
1197 curr_time.tm_sec != hpet_prev_update_sec) {
David Brownell7e2a31d2008-07-23 21:30:47 -07001198 if (hpet_prev_update_sec >= 0)
1199 rtc_int_flag = RTC_UF;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001200 hpet_prev_update_sec = curr_time.tm_sec;
1201 }
1202
1203 if (hpet_rtc_flags & RTC_PIE &&
1204 ++hpet_pie_count >= hpet_pie_limit) {
1205 rtc_int_flag |= RTC_PF;
1206 hpet_pie_count = 0;
1207 }
1208
Bernhard Walle8ee291f2008-01-15 16:44:38 +01001209 if (hpet_rtc_flags & RTC_AIE &&
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001210 (curr_time.tm_sec == hpet_alarm_time.tm_sec) &&
1211 (curr_time.tm_min == hpet_alarm_time.tm_min) &&
1212 (curr_time.tm_hour == hpet_alarm_time.tm_hour))
1213 rtc_int_flag |= RTC_AF;
1214
1215 if (rtc_int_flag) {
1216 rtc_int_flag |= (RTC_IRQF | (RTC_NUM_INTS << 8));
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +01001217 if (irq_handler)
1218 irq_handler(rtc_int_flag, dev_id);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001219 }
1220 return IRQ_HANDLED;
1221}
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +01001222EXPORT_SYMBOL_GPL(hpet_rtc_interrupt);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001223#endif