blob: efaf906daf93ff4dcca7385d26047c2db8691fbe [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09007#include <linux/slab.h>
john stultz5d0cf412006-06-26 00:25:12 -07008#include <linux/hpet.h>
9#include <linux/init.h>
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -070010#include <linux/cpu.h>
Ingo Molnar4588c1f2008-09-06 14:19:17 +020011#include <linux/pm.h>
12#include <linux/io.h>
john stultz5d0cf412006-06-26 00:25:12 -070013
Thomas Gleixner28769142007-10-12 23:04:06 +020014#include <asm/fixmap.h>
Thomas Gleixner06a24de2007-10-12 23:04:06 +020015#include <asm/i8253.h>
Ingo Molnar4588c1f2008-09-06 14:19:17 +020016#include <asm/hpet.h>
john stultz5d0cf412006-06-26 00:25:12 -070017
Ingo Molnar4588c1f2008-09-06 14:19:17 +020018#define HPET_MASK CLOCKSOURCE_MASK(32)
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 */
Pallipadi, Venkatesh73472a42010-01-21 11:09:52 -080037u8 hpet_msi_disable;
38
Ingo Molnare951e4a2008-11-25 08:42:01 +010039#ifdef CONFIG_PCI_MSI
Hannes Eder3b71e9e2008-11-23 20:19:33 +010040static unsigned long hpet_num_timers;
Ingo Molnare951e4a2008-11-25 08:42:01 +010041#endif
Ingo Molnar4588c1f2008-09-06 14:19:17 +020042static void __iomem *hpet_virt_address;
john stultz5d0cf412006-06-26 00:25:12 -070043
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -070044struct hpet_dev {
Ingo Molnar4588c1f2008-09-06 14:19:17 +020045 struct clock_event_device evt;
46 unsigned int num;
47 int cpu;
48 unsigned int irq;
49 unsigned int flags;
50 char name[10];
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -070051};
52
Jan Beulich5946fa32009-08-19 08:44:24 +010053inline unsigned int hpet_readl(unsigned int a)
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080054{
55 return readl(hpet_virt_address + a);
56}
57
Jan Beulich5946fa32009-08-19 08:44:24 +010058static inline void hpet_writel(unsigned int d, unsigned int a)
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080059{
60 writel(d, hpet_virt_address + a);
61}
62
Thomas Gleixner28769142007-10-12 23:04:06 +020063#ifdef CONFIG_X86_64
Thomas Gleixner28769142007-10-12 23:04:06 +020064#include <asm/pgtable.h>
Yinghai Lu2387ce52008-07-13 14:50:56 -070065#endif
Thomas Gleixner28769142007-10-12 23:04:06 +020066
Thomas Gleixner06a24de2007-10-12 23:04:06 +020067static inline void hpet_set_mapping(void)
68{
69 hpet_virt_address = ioremap_nocache(hpet_address, HPET_MMAP_SIZE);
Yinghai Lu2387ce52008-07-13 14:50:56 -070070#ifdef CONFIG_X86_64
71 __set_fixmap(VSYSCALL_HPET, hpet_address, PAGE_KERNEL_VSYSCALL_NOCACHE);
72#endif
Thomas Gleixner06a24de2007-10-12 23:04:06 +020073}
74
75static inline void hpet_clear_mapping(void)
76{
77 iounmap(hpet_virt_address);
78 hpet_virt_address = NULL;
79}
80
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080081/*
82 * HPET command line enable / disable
83 */
84static int boot_hpet_disable;
Thomas Gleixnerb17530b2007-10-19 20:35:02 +020085int hpet_force_user;
Andreas Herrmannb98103a2009-02-21 00:09:47 +010086static int hpet_verbose;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080087
Ingo Molnar4588c1f2008-09-06 14:19:17 +020088static int __init hpet_setup(char *str)
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080089{
90 if (str) {
91 if (!strncmp("disable", str, 7))
92 boot_hpet_disable = 1;
Thomas Gleixnerb17530b2007-10-19 20:35:02 +020093 if (!strncmp("force", str, 5))
94 hpet_force_user = 1;
Andreas Herrmannb98103a2009-02-21 00:09:47 +010095 if (!strncmp("verbose", str, 7))
96 hpet_verbose = 1;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080097 }
98 return 1;
99}
100__setup("hpet=", hpet_setup);
101
Thomas Gleixner28769142007-10-12 23:04:06 +0200102static int __init disable_hpet(char *str)
103{
104 boot_hpet_disable = 1;
105 return 1;
106}
107__setup("nohpet", disable_hpet);
108
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800109static inline int is_hpet_capable(void)
110{
Ingo Molnar4588c1f2008-09-06 14:19:17 +0200111 return !boot_hpet_disable && hpet_address;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800112}
113
114/*
115 * HPET timer interrupt enable / disable
116 */
117static int hpet_legacy_int_enabled;
118
119/**
120 * is_hpet_enabled - check whether the hpet timer interrupt is enabled
121 */
122int is_hpet_enabled(void)
123{
124 return is_hpet_capable() && hpet_legacy_int_enabled;
125}
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +0100126EXPORT_SYMBOL_GPL(is_hpet_enabled);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800127
Andreas Herrmannb98103a2009-02-21 00:09:47 +0100128static void _hpet_print_config(const char *function, int line)
129{
130 u32 i, timers, l, h;
131 printk(KERN_INFO "hpet: %s(%d):\n", function, line);
132 l = hpet_readl(HPET_ID);
133 h = hpet_readl(HPET_PERIOD);
134 timers = ((l & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT) + 1;
135 printk(KERN_INFO "hpet: ID: 0x%x, PERIOD: 0x%x\n", l, h);
136 l = hpet_readl(HPET_CFG);
137 h = hpet_readl(HPET_STATUS);
138 printk(KERN_INFO "hpet: CFG: 0x%x, STATUS: 0x%x\n", l, h);
139 l = hpet_readl(HPET_COUNTER);
140 h = hpet_readl(HPET_COUNTER+4);
141 printk(KERN_INFO "hpet: COUNTER_l: 0x%x, COUNTER_h: 0x%x\n", l, h);
142
143 for (i = 0; i < timers; i++) {
144 l = hpet_readl(HPET_Tn_CFG(i));
145 h = hpet_readl(HPET_Tn_CFG(i)+4);
146 printk(KERN_INFO "hpet: T%d: CFG_l: 0x%x, CFG_h: 0x%x\n",
147 i, l, h);
148 l = hpet_readl(HPET_Tn_CMP(i));
149 h = hpet_readl(HPET_Tn_CMP(i)+4);
150 printk(KERN_INFO "hpet: T%d: CMP_l: 0x%x, CMP_h: 0x%x\n",
151 i, l, h);
152 l = hpet_readl(HPET_Tn_ROUTE(i));
153 h = hpet_readl(HPET_Tn_ROUTE(i)+4);
154 printk(KERN_INFO "hpet: T%d ROUTE_l: 0x%x, ROUTE_h: 0x%x\n",
155 i, l, h);
156 }
157}
158
159#define hpet_print_config() \
160do { \
161 if (hpet_verbose) \
162 _hpet_print_config(__FUNCTION__, __LINE__); \
163} while (0)
164
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800165/*
166 * When the hpet driver (/dev/hpet) is enabled, we need to reserve
167 * timer 0 and timer 1 in case of RTC emulation.
168 */
169#ifdef CONFIG_HPET
Venki Pallipadif0ed4e62008-09-08 10:18:40 -0700170
Venki Pallipadi5f79f2f2008-09-24 10:03:17 -0700171static void hpet_reserve_msi_timers(struct hpet_data *hd);
Venki Pallipadif0ed4e62008-09-08 10:18:40 -0700172
Jan Beulich5946fa32009-08-19 08:44:24 +0100173static void hpet_reserve_platform_timers(unsigned int id)
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800174{
175 struct hpet __iomem *hpet = hpet_virt_address;
Balaji Rao37a47db82008-01-30 13:30:03 +0100176 struct hpet_timer __iomem *timer = &hpet->hpet_timers[2];
177 unsigned int nrtimers, i;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800178 struct hpet_data hd;
179
180 nrtimers = ((id & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT) + 1;
181
Ingo Molnar4588c1f2008-09-06 14:19:17 +0200182 memset(&hd, 0, sizeof(hd));
183 hd.hd_phys_address = hpet_address;
184 hd.hd_address = hpet;
185 hd.hd_nirqs = nrtimers;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800186 hpet_reserve_timer(&hd, 0);
187
188#ifdef CONFIG_HPET_EMULATE_RTC
189 hpet_reserve_timer(&hd, 1);
190#endif
Thomas Gleixner5761d642008-04-04 16:26:10 +0200191
David Brownell64a76f62008-07-29 12:47:38 -0700192 /*
193 * NOTE that hd_irq[] reflects IOAPIC input pins (LEGACY_8254
194 * is wrong for i8259!) not the output IRQ. Many BIOS writers
195 * don't bother configuring *any* comparator interrupts.
196 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800197 hd.hd_irq[0] = HPET_LEGACY_8254;
198 hd.hd_irq[1] = HPET_LEGACY_RTC;
199
Ingo Molnarfc3fbc42008-04-27 14:04:14 +0200200 for (i = 2; i < nrtimers; timer++, i++) {
Ingo Molnar4588c1f2008-09-06 14:19:17 +0200201 hd.hd_irq[i] = (readl(&timer->hpet_config) &
202 Tn_INT_ROUTE_CNF_MASK) >> Tn_INT_ROUTE_CNF_SHIFT;
Ingo Molnarfc3fbc42008-04-27 14:04:14 +0200203 }
Thomas Gleixner5761d642008-04-04 16:26:10 +0200204
Venki Pallipadif0ed4e62008-09-08 10:18:40 -0700205 hpet_reserve_msi_timers(&hd);
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700206
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800207 hpet_alloc(&hd);
Thomas Gleixner5761d642008-04-04 16:26:10 +0200208
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800209}
210#else
Jan Beulich5946fa32009-08-19 08:44:24 +0100211static void hpet_reserve_platform_timers(unsigned int id) { }
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800212#endif
213
214/*
215 * Common hpet info
216 */
217static unsigned long hpet_period;
218
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200219static void hpet_legacy_set_mode(enum clock_event_mode mode,
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800220 struct clock_event_device *evt);
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200221static int hpet_legacy_next_event(unsigned long delta,
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800222 struct clock_event_device *evt);
223
224/*
225 * The hpet clock event device
226 */
227static struct clock_event_device hpet_clockevent = {
228 .name = "hpet",
229 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200230 .set_mode = hpet_legacy_set_mode,
231 .set_next_event = hpet_legacy_next_event,
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800232 .shift = 32,
233 .irq = 0,
Venki Pallipadi59c69f22007-10-12 23:04:23 +0200234 .rating = 50,
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800235};
236
Andreas Herrmann8d6f0c82009-02-21 00:10:44 +0100237static void hpet_stop_counter(void)
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800238{
239 unsigned long cfg = hpet_readl(HPET_CFG);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800240 cfg &= ~HPET_CFG_ENABLE;
241 hpet_writel(cfg, HPET_CFG);
Andreas Herrmann7a6f9cb2009-04-21 20:00:37 +0200242}
243
244static void hpet_reset_counter(void)
245{
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800246 hpet_writel(0, HPET_COUNTER);
247 hpet_writel(0, HPET_COUNTER + 4);
Andreas Herrmann8d6f0c82009-02-21 00:10:44 +0100248}
249
250static void hpet_start_counter(void)
251{
Jan Beulich5946fa32009-08-19 08:44:24 +0100252 unsigned int cfg = hpet_readl(HPET_CFG);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800253 cfg |= HPET_CFG_ENABLE;
254 hpet_writel(cfg, HPET_CFG);
255}
256
Andreas Herrmann8d6f0c82009-02-21 00:10:44 +0100257static void hpet_restart_counter(void)
258{
259 hpet_stop_counter();
Andreas Herrmann7a6f9cb2009-04-21 20:00:37 +0200260 hpet_reset_counter();
Andreas Herrmann8d6f0c82009-02-21 00:10:44 +0100261 hpet_start_counter();
262}
263
Venki Pallipadi59c69f22007-10-12 23:04:23 +0200264static void hpet_resume_device(void)
265{
Venki Pallipadibfe0c1c2007-10-12 23:04:24 +0200266 force_hpet_resume();
Venki Pallipadi59c69f22007-10-12 23:04:23 +0200267}
268
Magnus Damm17622332010-02-02 14:41:39 -0800269static void hpet_resume_counter(struct clocksource *cs)
Venki Pallipadi59c69f22007-10-12 23:04:23 +0200270{
271 hpet_resume_device();
Andreas Herrmann8d6f0c82009-02-21 00:10:44 +0100272 hpet_restart_counter();
Venki Pallipadi59c69f22007-10-12 23:04:23 +0200273}
274
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200275static void hpet_enable_legacy_int(void)
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800276{
Jan Beulich5946fa32009-08-19 08:44:24 +0100277 unsigned int cfg = hpet_readl(HPET_CFG);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800278
279 cfg |= HPET_CFG_LEGACY;
280 hpet_writel(cfg, HPET_CFG);
281 hpet_legacy_int_enabled = 1;
282}
283
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200284static void hpet_legacy_clockevent_register(void)
285{
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200286 /* Start HPET legacy interrupts */
287 hpet_enable_legacy_int();
288
289 /*
Carlos R. Mafra6fd592d2008-05-05 20:11:22 -0300290 * The mult factor is defined as (include/linux/clockchips.h)
291 * mult/2^shift = cyc/ns (in contrast to ns/cyc in clocksource.h)
292 * hpet_period is in units of femtoseconds (per cycle), so
293 * mult/2^shift = cyc/ns = 10^6/hpet_period
294 * mult = (10^6 * 2^shift)/hpet_period
295 * mult = (FSEC_PER_NSEC << hpet_clockevent.shift)/hpet_period
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200296 */
Carlos R. Mafra6fd592d2008-05-05 20:11:22 -0300297 hpet_clockevent.mult = div_sc((unsigned long) FSEC_PER_NSEC,
298 hpet_period, hpet_clockevent.shift);
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200299 /* Calculate the min / max delta */
300 hpet_clockevent.max_delta_ns = clockevent_delta2ns(0x7FFFFFFF,
301 &hpet_clockevent);
Thomas Gleixner7cfb04352008-09-03 21:37:24 +0000302 /* 5 usec minimum reprogramming delta. */
303 hpet_clockevent.min_delta_ns = 5000;
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200304
305 /*
306 * Start hpet with the boot cpu mask and make it
307 * global after the IO_APIC has been initialized.
308 */
Rusty Russell320ab2b2008-12-13 21:20:26 +1030309 hpet_clockevent.cpumask = cpumask_of(smp_processor_id());
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200310 clockevents_register_device(&hpet_clockevent);
311 global_clock_event = &hpet_clockevent;
312 printk(KERN_DEBUG "hpet clockevent registered\n");
313}
314
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700315static int hpet_setup_msi_irq(unsigned int irq);
316
venkatesh.pallipadi@intel.comb40d5752008-09-05 18:02:16 -0700317static void hpet_set_mode(enum clock_event_mode mode,
318 struct clock_event_device *evt, int timer)
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800319{
Jan Beulich5946fa32009-08-19 08:44:24 +0100320 unsigned int cfg, cmp, now;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800321 uint64_t delta;
322
Ingo Molnar4588c1f2008-09-06 14:19:17 +0200323 switch (mode) {
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800324 case CLOCK_EVT_MODE_PERIODIC:
Andreas Herrmannc23e2532009-02-21 00:16:35 +0100325 hpet_stop_counter();
venkatesh.pallipadi@intel.comb40d5752008-09-05 18:02:16 -0700326 delta = ((uint64_t)(NSEC_PER_SEC/HZ)) * evt->mult;
327 delta >>= evt->shift;
Andreas Herrmann7a6f9cb2009-04-21 20:00:37 +0200328 now = hpet_readl(HPET_COUNTER);
Jan Beulich5946fa32009-08-19 08:44:24 +0100329 cmp = now + (unsigned int) delta;
venkatesh.pallipadi@intel.comb40d5752008-09-05 18:02:16 -0700330 cfg = hpet_readl(HPET_Tn_CFG(timer));
john stultzb13e2462009-02-12 18:48:53 -0800331 /* Make sure we use edge triggered interrupts */
332 cfg &= ~HPET_TN_LEVEL;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800333 cfg |= HPET_TN_ENABLE | HPET_TN_PERIODIC |
334 HPET_TN_SETVAL | HPET_TN_32BIT;
venkatesh.pallipadi@intel.comb40d5752008-09-05 18:02:16 -0700335 hpet_writel(cfg, HPET_Tn_CFG(timer));
Andreas Herrmann7a6f9cb2009-04-21 20:00:37 +0200336 hpet_writel(cmp, HPET_Tn_CMP(timer));
337 udelay(1);
338 /*
339 * HPET on AMD 81xx needs a second write (with HPET_TN_SETVAL
340 * cleared) to T0_CMP to set the period. The HPET_TN_SETVAL
341 * bit is automatically cleared after the first write.
342 * (See AMD-8111 HyperTransport I/O Hub Data Sheet,
343 * Publication # 24674)
344 */
Jan Beulich5946fa32009-08-19 08:44:24 +0100345 hpet_writel((unsigned int) delta, HPET_Tn_CMP(timer));
Andreas Herrmannc23e2532009-02-21 00:16:35 +0100346 hpet_start_counter();
Andreas Herrmannb98103a2009-02-21 00:09:47 +0100347 hpet_print_config();
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800348 break;
349
350 case CLOCK_EVT_MODE_ONESHOT:
venkatesh.pallipadi@intel.comb40d5752008-09-05 18:02:16 -0700351 cfg = hpet_readl(HPET_Tn_CFG(timer));
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800352 cfg &= ~HPET_TN_PERIODIC;
353 cfg |= HPET_TN_ENABLE | HPET_TN_32BIT;
venkatesh.pallipadi@intel.comb40d5752008-09-05 18:02:16 -0700354 hpet_writel(cfg, HPET_Tn_CFG(timer));
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800355 break;
356
357 case CLOCK_EVT_MODE_UNUSED:
358 case CLOCK_EVT_MODE_SHUTDOWN:
venkatesh.pallipadi@intel.comb40d5752008-09-05 18:02:16 -0700359 cfg = hpet_readl(HPET_Tn_CFG(timer));
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800360 cfg &= ~HPET_TN_ENABLE;
venkatesh.pallipadi@intel.comb40d5752008-09-05 18:02:16 -0700361 hpet_writel(cfg, HPET_Tn_CFG(timer));
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800362 break;
Thomas Gleixner18de5bc2007-07-21 04:37:34 -0700363
364 case CLOCK_EVT_MODE_RESUME:
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700365 if (timer == 0) {
366 hpet_enable_legacy_int();
367 } else {
368 struct hpet_dev *hdev = EVT_TO_HPET_DEV(evt);
369 hpet_setup_msi_irq(hdev->irq);
370 disable_irq(hdev->irq);
Rusty Russell0de26522008-12-13 21:20:26 +1030371 irq_set_affinity(hdev->irq, cpumask_of(hdev->cpu));
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700372 enable_irq(hdev->irq);
373 }
Andreas Herrmannb98103a2009-02-21 00:09:47 +0100374 hpet_print_config();
Thomas Gleixner18de5bc2007-07-21 04:37:34 -0700375 break;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800376 }
377}
378
venkatesh.pallipadi@intel.comb40d5752008-09-05 18:02:16 -0700379static int hpet_next_event(unsigned long delta,
380 struct clock_event_device *evt, int timer)
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800381{
Thomas Gleixnerf7676252008-09-06 03:03:32 +0200382 u32 cnt;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800383
384 cnt = hpet_readl(HPET_COUNTER);
Thomas Gleixnerf7676252008-09-06 03:03:32 +0200385 cnt += (u32) delta;
venkatesh.pallipadi@intel.comb40d5752008-09-05 18:02:16 -0700386 hpet_writel(cnt, HPET_Tn_CMP(timer));
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800387
Thomas Gleixner72d43d92008-09-06 03:06:08 +0200388 /*
Thomas Gleixner18ed61d2009-11-27 15:24:44 +0100389 * We need to read back the CMP register on certain HPET
390 * implementations (ATI chipsets) which seem to delay the
391 * transfer of the compare register into the internal compare
392 * logic. With small deltas this might actually be too late as
393 * the counter could already be higher than the compare value
394 * at that point and we would wait for the next hpet interrupt
395 * forever. We found out that reading the CMP register back
396 * forces the transfer so we can rely on the comparison with
Thomas Gleixner54ff7e52010-09-14 22:10:21 +0200397 * the counter register below. If the read back from the
398 * compare register does not match the value we programmed
399 * then we might have a real hardware problem. We can not do
400 * much about it here, but at least alert the user/admin with
401 * a prominent warning.
Thomas Gleixner30a564b2010-04-13 15:31:36 +0200402 *
Thomas Gleixner54ff7e52010-09-14 22:10:21 +0200403 * An erratum on some chipsets (ICH9,..), results in
404 * comparator read immediately following a write returning old
405 * value. Workaround for this is to read this value second
406 * time, when first read returns old value.
Thomas Gleixner30a564b2010-04-13 15:31:36 +0200407 *
Thomas Gleixner54ff7e52010-09-14 22:10:21 +0200408 * In fact the write to the comparator register is delayed up
409 * to two HPET cycles so the workaround we tried to restrict
410 * the readback to those known to be borked ATI chipsets
411 * failed miserably. So we give up on optimizations forever
412 * and penalize all HPET incarnations unconditionally.
Thomas Gleixner72d43d92008-09-06 03:06:08 +0200413 */
Thomas Gleixner54ff7e52010-09-14 22:10:21 +0200414 if (unlikely((u32)hpet_readl(HPET_Tn_CMP(timer)) != cnt)) {
415 if (hpet_readl(HPET_Tn_CMP(timer)) != cnt)
Thomas Gleixner30a564b2010-04-13 15:31:36 +0200416 printk_once(KERN_WARNING
Thomas Gleixner54ff7e52010-09-14 22:10:21 +0200417 "hpet: compare register read back failed.\n");
Pallipadi, Venkatesh8da854c2010-02-25 10:53:48 -0800418 }
Thomas Gleixner72d43d92008-09-06 03:06:08 +0200419
Jan Beulich5946fa32009-08-19 08:44:24 +0100420 return (s32)(hpet_readl(HPET_COUNTER) - cnt) >= 0 ? -ETIME : 0;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800421}
422
venkatesh.pallipadi@intel.comb40d5752008-09-05 18:02:16 -0700423static void hpet_legacy_set_mode(enum clock_event_mode mode,
424 struct clock_event_device *evt)
425{
426 hpet_set_mode(mode, evt, 0);
427}
428
429static int hpet_legacy_next_event(unsigned long delta,
430 struct clock_event_device *evt)
431{
432 return hpet_next_event(delta, evt, 0);
433}
434
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800435/*
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -0700436 * HPET MSI Support
437 */
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700438#ifdef CONFIG_PCI_MSI
Venki Pallipadi5f79f2f2008-09-24 10:03:17 -0700439
440static DEFINE_PER_CPU(struct hpet_dev *, cpu_hpet_dev);
441static struct hpet_dev *hpet_devs;
442
Thomas Gleixnerd0fbca82010-09-28 16:18:39 +0200443void hpet_msi_unmask(struct irq_data *data)
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -0700444{
Thomas Gleixnerd0fbca82010-09-28 16:18:39 +0200445 struct hpet_dev *hdev = data->handler_data;
Jan Beulich5946fa32009-08-19 08:44:24 +0100446 unsigned int cfg;
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -0700447
448 /* unmask it */
449 cfg = hpet_readl(HPET_Tn_CFG(hdev->num));
450 cfg |= HPET_TN_FSB;
451 hpet_writel(cfg, HPET_Tn_CFG(hdev->num));
452}
453
Thomas Gleixnerd0fbca82010-09-28 16:18:39 +0200454void hpet_msi_mask(struct irq_data *data)
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -0700455{
Thomas Gleixnerd0fbca82010-09-28 16:18:39 +0200456 struct hpet_dev *hdev = data->handler_data;
Jan Beulich5946fa32009-08-19 08:44:24 +0100457 unsigned int cfg;
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -0700458
459 /* mask it */
460 cfg = hpet_readl(HPET_Tn_CFG(hdev->num));
461 cfg &= ~HPET_TN_FSB;
462 hpet_writel(cfg, HPET_Tn_CFG(hdev->num));
463}
464
Thomas Gleixnerd0fbca82010-09-28 16:18:39 +0200465void hpet_msi_write(struct hpet_dev *hdev, struct msi_msg *msg)
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -0700466{
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -0700467 hpet_writel(msg->data, HPET_Tn_ROUTE(hdev->num));
468 hpet_writel(msg->address_lo, HPET_Tn_ROUTE(hdev->num) + 4);
469}
470
Thomas Gleixnerd0fbca82010-09-28 16:18:39 +0200471void hpet_msi_read(struct hpet_dev *hdev, struct msi_msg *msg)
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -0700472{
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -0700473 msg->data = hpet_readl(HPET_Tn_ROUTE(hdev->num));
474 msg->address_lo = hpet_readl(HPET_Tn_ROUTE(hdev->num) + 4);
475 msg->address_hi = 0;
476}
477
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700478static void hpet_msi_set_mode(enum clock_event_mode mode,
479 struct clock_event_device *evt)
480{
481 struct hpet_dev *hdev = EVT_TO_HPET_DEV(evt);
482 hpet_set_mode(mode, evt, hdev->num);
483}
484
485static int hpet_msi_next_event(unsigned long delta,
486 struct clock_event_device *evt)
487{
488 struct hpet_dev *hdev = EVT_TO_HPET_DEV(evt);
489 return hpet_next_event(delta, evt, hdev->num);
490}
491
492static int hpet_setup_msi_irq(unsigned int irq)
493{
Suresh Siddhac8bc6f32009-08-04 12:07:09 -0700494 if (arch_setup_hpet_msi(irq, hpet_blockid)) {
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700495 destroy_irq(irq);
496 return -EINVAL;
497 }
498 return 0;
499}
500
501static int hpet_assign_irq(struct hpet_dev *dev)
502{
503 unsigned int irq;
504
Thomas Gleixner02198962010-09-28 23:20:23 +0200505 irq = create_irq_nr(0, -1);
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700506 if (!irq)
507 return -EINVAL;
508
509 set_irq_data(irq, dev);
510
511 if (hpet_setup_msi_irq(irq))
512 return -EINVAL;
513
514 dev->irq = irq;
515 return 0;
516}
517
518static irqreturn_t hpet_interrupt_handler(int irq, void *data)
519{
520 struct hpet_dev *dev = (struct hpet_dev *)data;
521 struct clock_event_device *hevt = &dev->evt;
522
523 if (!hevt->event_handler) {
524 printk(KERN_INFO "Spurious HPET timer interrupt on HPET timer %d\n",
525 dev->num);
526 return IRQ_HANDLED;
527 }
528
529 hevt->event_handler(hevt);
530 return IRQ_HANDLED;
531}
532
533static int hpet_setup_irq(struct hpet_dev *dev)
534{
535
536 if (request_irq(dev->irq, hpet_interrupt_handler,
Thomas Gleixner507fa3a2009-06-14 17:46:01 +0200537 IRQF_TIMER | IRQF_DISABLED | IRQF_NOBALANCING,
538 dev->name, dev))
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700539 return -1;
540
541 disable_irq(dev->irq);
Rusty Russell0de26522008-12-13 21:20:26 +1030542 irq_set_affinity(dev->irq, cpumask_of(dev->cpu));
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700543 enable_irq(dev->irq);
544
Yinghai Luc81bba42008-09-25 11:53:11 -0700545 printk(KERN_DEBUG "hpet: %s irq %d for MSI\n",
546 dev->name, dev->irq);
547
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700548 return 0;
549}
550
551/* This should be called in specific @cpu */
552static void init_one_hpet_msi_clockevent(struct hpet_dev *hdev, int cpu)
553{
554 struct clock_event_device *evt = &hdev->evt;
555 uint64_t hpet_freq;
556
557 WARN_ON(cpu != smp_processor_id());
558 if (!(hdev->flags & HPET_DEV_VALID))
559 return;
560
561 if (hpet_setup_msi_irq(hdev->irq))
562 return;
563
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;
572 if (hdev->flags & HPET_DEV_PERI_CAP)
573 evt->features |= CLOCK_EVT_FEAT_PERIODIC;
574
575 evt->set_mode = hpet_msi_set_mode;
576 evt->set_next_event = hpet_msi_next_event;
577 evt->shift = 32;
578
579 /*
580 * The period is a femto seconds value. We need to calculate the
581 * scaled math multiplication factor for nanosecond to hpet tick
582 * conversion.
583 */
Chris Wilson4936a3b2010-08-09 14:20:10 -0700584 hpet_freq = FSEC_PER_SEC;
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700585 do_div(hpet_freq, hpet_period);
586 evt->mult = div_sc((unsigned long) hpet_freq,
587 NSEC_PER_SEC, evt->shift);
588 /* Calculate the max delta */
589 evt->max_delta_ns = clockevent_delta2ns(0x7FFFFFFF, evt);
590 /* 5 usec minimum reprogramming delta. */
591 evt->min_delta_ns = 5000;
592
Rusty Russell320ab2b2008-12-13 21:20:26 +1030593 evt->cpumask = cpumask_of(hdev->cpu);
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700594 clockevents_register_device(evt);
595}
596
597#ifdef CONFIG_HPET
598/* Reserve at least one timer for userspace (/dev/hpet) */
599#define RESERVE_TIMERS 1
600#else
601#define RESERVE_TIMERS 0
602#endif
Venki Pallipadi5f79f2f2008-09-24 10:03:17 -0700603
604static void hpet_msi_capability_lookup(unsigned int start_timer)
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700605{
606 unsigned int id;
607 unsigned int num_timers;
608 unsigned int num_timers_used = 0;
609 int i;
610
Pallipadi, Venkatesh73472a42010-01-21 11:09:52 -0800611 if (hpet_msi_disable)
612 return;
613
Shaohua Li39fe05e2009-08-12 11:16:12 +0800614 if (boot_cpu_has(X86_FEATURE_ARAT))
615 return;
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700616 id = hpet_readl(HPET_ID);
617
618 num_timers = ((id & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT);
619 num_timers++; /* Value read out starts from 0 */
Andreas Herrmannb98103a2009-02-21 00:09:47 +0100620 hpet_print_config();
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700621
622 hpet_devs = kzalloc(sizeof(struct hpet_dev) * num_timers, GFP_KERNEL);
623 if (!hpet_devs)
624 return;
625
626 hpet_num_timers = num_timers;
627
628 for (i = start_timer; i < num_timers - RESERVE_TIMERS; i++) {
629 struct hpet_dev *hdev = &hpet_devs[num_timers_used];
Jan Beulich5946fa32009-08-19 08:44:24 +0100630 unsigned int cfg = hpet_readl(HPET_Tn_CFG(i));
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700631
632 /* Only consider HPET timer with MSI support */
633 if (!(cfg & HPET_TN_FSB_CAP))
634 continue;
635
636 hdev->flags = 0;
637 if (cfg & HPET_TN_PERIODIC_CAP)
638 hdev->flags |= HPET_DEV_PERI_CAP;
639 hdev->num = i;
640
641 sprintf(hdev->name, "hpet%d", i);
642 if (hpet_assign_irq(hdev))
643 continue;
644
645 hdev->flags |= HPET_DEV_FSB_CAP;
646 hdev->flags |= HPET_DEV_VALID;
647 num_timers_used++;
648 if (num_timers_used == num_possible_cpus())
649 break;
650 }
651
652 printk(KERN_INFO "HPET: %d timers in total, %d timers will be used for per-cpu timer\n",
653 num_timers, num_timers_used);
654}
655
Venki Pallipadi5f79f2f2008-09-24 10:03:17 -0700656#ifdef CONFIG_HPET
657static void hpet_reserve_msi_timers(struct hpet_data *hd)
658{
659 int i;
660
661 if (!hpet_devs)
662 return;
663
664 for (i = 0; i < hpet_num_timers; i++) {
665 struct hpet_dev *hdev = &hpet_devs[i];
666
667 if (!(hdev->flags & HPET_DEV_VALID))
668 continue;
669
670 hd->hd_irq[hdev->num] = hdev->irq;
671 hpet_reserve_timer(hd, hdev->num);
672 }
673}
674#endif
675
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700676static struct hpet_dev *hpet_get_unused_timer(void)
677{
678 int i;
679
680 if (!hpet_devs)
681 return NULL;
682
683 for (i = 0; i < hpet_num_timers; i++) {
684 struct hpet_dev *hdev = &hpet_devs[i];
685
686 if (!(hdev->flags & HPET_DEV_VALID))
687 continue;
688 if (test_and_set_bit(HPET_DEV_USED_BIT,
689 (unsigned long *)&hdev->flags))
690 continue;
691 return hdev;
692 }
693 return NULL;
694}
695
696struct hpet_work_struct {
697 struct delayed_work work;
698 struct completion complete;
699};
700
701static void hpet_work(struct work_struct *w)
702{
703 struct hpet_dev *hdev;
704 int cpu = smp_processor_id();
705 struct hpet_work_struct *hpet_work;
706
707 hpet_work = container_of(w, struct hpet_work_struct, work.work);
708
709 hdev = hpet_get_unused_timer();
710 if (hdev)
711 init_one_hpet_msi_clockevent(hdev, cpu);
712
713 complete(&hpet_work->complete);
714}
715
716static int hpet_cpuhp_notify(struct notifier_block *n,
717 unsigned long action, void *hcpu)
718{
719 unsigned long cpu = (unsigned long)hcpu;
720 struct hpet_work_struct work;
721 struct hpet_dev *hdev = per_cpu(cpu_hpet_dev, cpu);
722
723 switch (action & 0xf) {
724 case CPU_ONLINE:
Thomas Gleixner336f6c32009-01-22 09:50:44 +0100725 INIT_DELAYED_WORK_ON_STACK(&work.work, hpet_work);
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700726 init_completion(&work.complete);
727 /* FIXME: add schedule_work_on() */
728 schedule_delayed_work_on(cpu, &work.work, 0);
729 wait_for_completion(&work.complete);
Thomas Gleixner336f6c32009-01-22 09:50:44 +0100730 destroy_timer_on_stack(&work.work.timer);
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700731 break;
732 case CPU_DEAD:
733 if (hdev) {
734 free_irq(hdev->irq, hdev);
735 hdev->flags &= ~HPET_DEV_USED;
736 per_cpu(cpu_hpet_dev, cpu) = NULL;
737 }
738 break;
739 }
740 return NOTIFY_OK;
741}
742#else
743
Steven Noonanba374c92008-09-08 16:19:09 -0700744static int hpet_setup_msi_irq(unsigned int irq)
745{
746 return 0;
747}
Venki Pallipadi5f79f2f2008-09-24 10:03:17 -0700748static void hpet_msi_capability_lookup(unsigned int start_timer)
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700749{
750 return;
751}
752
Venki Pallipadi5f79f2f2008-09-24 10:03:17 -0700753#ifdef CONFIG_HPET
754static void hpet_reserve_msi_timers(struct hpet_data *hd)
755{
756 return;
757}
758#endif
759
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700760static int hpet_cpuhp_notify(struct notifier_block *n,
761 unsigned long action, void *hcpu)
762{
763 return NOTIFY_OK;
764}
765
766#endif
767
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -0700768/*
john stultz6bb74df2007-03-05 00:30:50 -0800769 * Clock source related code
770 */
Magnus Damm8e196082009-04-21 12:24:00 -0700771static cycle_t read_hpet(struct clocksource *cs)
john stultz6bb74df2007-03-05 00:30:50 -0800772{
773 return (cycle_t)hpet_readl(HPET_COUNTER);
774}
775
Thomas Gleixner28769142007-10-12 23:04:06 +0200776#ifdef CONFIG_X86_64
777static cycle_t __vsyscall_fn vread_hpet(void)
778{
779 return readl((const void __iomem *)fix_to_virt(VSYSCALL_HPET) + 0xf0);
780}
781#endif
782
john stultz6bb74df2007-03-05 00:30:50 -0800783static struct clocksource clocksource_hpet = {
784 .name = "hpet",
785 .rating = 250,
786 .read = read_hpet,
787 .mask = HPET_MASK,
john stultz6bb74df2007-03-05 00:30:50 -0800788 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
Andreas Herrmann8d6f0c82009-02-21 00:10:44 +0100789 .resume = hpet_resume_counter,
Thomas Gleixner28769142007-10-12 23:04:06 +0200790#ifdef CONFIG_X86_64
791 .vread = vread_hpet,
792#endif
john stultz6bb74df2007-03-05 00:30:50 -0800793};
794
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200795static int hpet_clocksource_register(void)
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800796{
Carlos R. Mafra6fd592d2008-05-05 20:11:22 -0300797 u64 start, now;
John Stultzf12a15b2010-07-13 17:56:27 -0700798 u64 hpet_freq;
Thomas Gleixner075bcd12007-07-21 17:11:12 +0200799 cycle_t t1;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800800
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800801 /* Start the counter */
Andreas Herrmann8d6f0c82009-02-21 00:10:44 +0100802 hpet_restart_counter();
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800803
Thomas Gleixner075bcd12007-07-21 17:11:12 +0200804 /* Verify whether hpet counter works */
Magnus Damm8e196082009-04-21 12:24:00 -0700805 t1 = hpet_readl(HPET_COUNTER);
Thomas Gleixner075bcd12007-07-21 17:11:12 +0200806 rdtscll(start);
807
808 /*
809 * We don't know the TSC frequency yet, but waiting for
810 * 200000 TSC cycles is safe:
811 * 4 GHz == 50us
812 * 1 GHz == 200us
813 */
814 do {
815 rep_nop();
816 rdtscll(now);
817 } while ((now - start) < 200000UL);
818
Magnus Damm8e196082009-04-21 12:24:00 -0700819 if (t1 == hpet_readl(HPET_COUNTER)) {
Thomas Gleixner075bcd12007-07-21 17:11:12 +0200820 printk(KERN_WARNING
821 "HPET counter not counting. HPET disabled\n");
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200822 return -ENODEV;
Thomas Gleixner075bcd12007-07-21 17:11:12 +0200823 }
824
Carlos R. Mafra6fd592d2008-05-05 20:11:22 -0300825 /*
826 * The definition of mult is (include/linux/clocksource.h)
827 * mult/2^shift = ns/cyc and hpet_period is in units of fsec/cyc
828 * so we first need to convert hpet_period to ns/cyc units:
829 * mult/2^shift = ns/cyc = hpet_period/10^6
830 * mult = (hpet_period * 2^shift)/10^6
831 * mult = (hpet_period << shift)/FSEC_PER_NSEC
john stultz6bb74df2007-03-05 00:30:50 -0800832 */
john stultz6bb74df2007-03-05 00:30:50 -0800833
John Stultzf12a15b2010-07-13 17:56:27 -0700834 /* Need to convert hpet_period (fsec/cyc) to cyc/sec:
835 *
836 * cyc/sec = FSEC_PER_SEC/hpet_period(fsec/cyc)
837 * cyc/sec = (FSEC_PER_NSEC * NSEC_PER_SEC)/hpet_period
838 */
Chris Wilson4936a3b2010-08-09 14:20:10 -0700839 hpet_freq = FSEC_PER_SEC;
John Stultzf12a15b2010-07-13 17:56:27 -0700840 do_div(hpet_freq, hpet_period);
841 clocksource_register_hz(&clocksource_hpet, (u32)hpet_freq);
john stultz6bb74df2007-03-05 00:30:50 -0800842
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200843 return 0;
844}
845
Pavel Machekb02a7f22008-02-05 00:48:13 +0100846/**
847 * hpet_enable - Try to setup the HPET timer. Returns 1 on success.
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200848 */
849int __init hpet_enable(void)
850{
Jan Beulich5946fa32009-08-19 08:44:24 +0100851 unsigned int id;
Thomas Gleixnera6825f12008-08-14 12:17:06 +0200852 int i;
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200853
854 if (!is_hpet_capable())
855 return 0;
856
857 hpet_set_mapping();
858
859 /*
860 * Read the period and check for a sane value:
861 */
862 hpet_period = hpet_readl(HPET_PERIOD);
Thomas Gleixnera6825f12008-08-14 12:17:06 +0200863
864 /*
865 * AMD SB700 based systems with spread spectrum enabled use a
866 * SMM based HPET emulation to provide proper frequency
867 * setting. The SMM code is initialized with the first HPET
868 * register access and takes some time to complete. During
869 * this time the config register reads 0xffffffff. We check
870 * for max. 1000 loops whether the config register reads a non
871 * 0xffffffff value to make sure that HPET is up and running
872 * before we go further. A counting loop is safe, as the HPET
873 * access takes thousands of CPU cycles. On non SB700 based
874 * machines this check is only done once and has no side
875 * effects.
876 */
877 for (i = 0; hpet_readl(HPET_CFG) == 0xFFFFFFFF; i++) {
878 if (i == 1000) {
879 printk(KERN_WARNING
880 "HPET config register value = 0xFFFFFFFF. "
881 "Disabling HPET\n");
882 goto out_nohpet;
883 }
884 }
885
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200886 if (hpet_period < HPET_MIN_PERIOD || hpet_period > HPET_MAX_PERIOD)
887 goto out_nohpet;
888
889 /*
890 * Read the HPET ID register to retrieve the IRQ routing
891 * information and the number of channels
892 */
893 id = hpet_readl(HPET_ID);
Andreas Herrmannb98103a2009-02-21 00:09:47 +0100894 hpet_print_config();
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200895
896#ifdef CONFIG_HPET_EMULATE_RTC
897 /*
898 * The legacy routing mode needs at least two channels, tick timer
899 * and the rtc emulation channel.
900 */
901 if (!(id & HPET_ID_NUMBER))
902 goto out_nohpet;
903#endif
904
905 if (hpet_clocksource_register())
906 goto out_nohpet;
907
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800908 if (id & HPET_ID_LEGSUP) {
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200909 hpet_legacy_clockevent_register();
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800910 return 1;
911 }
912 return 0;
913
914out_nohpet:
Thomas Gleixner06a24de2007-10-12 23:04:06 +0200915 hpet_clear_mapping();
Janne Kulmalabacbe992008-12-16 13:39:57 +0200916 hpet_address = 0;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800917 return 0;
918}
919
Thomas Gleixner28769142007-10-12 23:04:06 +0200920/*
921 * Needs to be late, as the reserve_timer code calls kalloc !
922 *
923 * Not a problem on i386 as hpet_enable is called from late_time_init,
924 * but on x86_64 it is necessary !
925 */
926static __init int hpet_late_init(void)
927{
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700928 int cpu;
929
Venki Pallipadi59c69f22007-10-12 23:04:23 +0200930 if (boot_hpet_disable)
Thomas Gleixner28769142007-10-12 23:04:06 +0200931 return -ENODEV;
932
Venki Pallipadi59c69f22007-10-12 23:04:23 +0200933 if (!hpet_address) {
934 if (!force_hpet_address)
935 return -ENODEV;
936
937 hpet_address = force_hpet_address;
938 hpet_enable();
Venki Pallipadi59c69f22007-10-12 23:04:23 +0200939 }
940
Jeremy Fitzhardinge39c04b52008-12-16 12:32:23 -0800941 if (!hpet_virt_address)
942 return -ENODEV;
943
Shaohua Li39fe05e2009-08-12 11:16:12 +0800944 if (hpet_readl(HPET_ID) & HPET_ID_LEGSUP)
945 hpet_msi_capability_lookup(2);
946 else
947 hpet_msi_capability_lookup(0);
948
Thomas Gleixner28769142007-10-12 23:04:06 +0200949 hpet_reserve_platform_timers(hpet_readl(HPET_ID));
Andreas Herrmannb98103a2009-02-21 00:09:47 +0100950 hpet_print_config();
Venki Pallipadi59c69f22007-10-12 23:04:23 +0200951
Pallipadi, Venkatesh73472a42010-01-21 11:09:52 -0800952 if (hpet_msi_disable)
953 return 0;
954
Shaohua Li39fe05e2009-08-12 11:16:12 +0800955 if (boot_cpu_has(X86_FEATURE_ARAT))
956 return 0;
957
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700958 for_each_online_cpu(cpu) {
959 hpet_cpuhp_notify(NULL, CPU_ONLINE, (void *)(long)cpu);
960 }
961
962 /* This notifier should be called after workqueue is ready */
963 hotcpu_notifier(hpet_cpuhp_notify, -20);
964
Thomas Gleixner28769142007-10-12 23:04:06 +0200965 return 0;
966}
967fs_initcall(hpet_late_init);
968
OGAWA Hirofumic86c7fb2007-12-03 17:17:10 +0100969void hpet_disable(void)
970{
Stefano Stabelliniff487802010-07-21 18:32:37 +0100971 if (is_hpet_capable() && hpet_virt_address) {
Jan Beulich5946fa32009-08-19 08:44:24 +0100972 unsigned int cfg = hpet_readl(HPET_CFG);
OGAWA Hirofumic86c7fb2007-12-03 17:17:10 +0100973
974 if (hpet_legacy_int_enabled) {
975 cfg &= ~HPET_CFG_LEGACY;
976 hpet_legacy_int_enabled = 0;
977 }
978 cfg &= ~HPET_CFG_ENABLE;
979 hpet_writel(cfg, HPET_CFG);
980 }
981}
982
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800983#ifdef CONFIG_HPET_EMULATE_RTC
984
985/* HPET in LegacyReplacement Mode eats up RTC interrupt line. When, HPET
986 * is enabled, we support RTC interrupt functionality in software.
987 * RTC has 3 kinds of interrupts:
988 * 1) Update Interrupt - generate an interrupt, every sec, when RTC clock
989 * is updated
990 * 2) Alarm Interrupt - generate an interrupt at a specific time of day
991 * 3) Periodic Interrupt - generate periodic interrupt, with frequencies
992 * 2Hz-8192Hz (2Hz-64Hz for non-root user) (all freqs in powers of 2)
993 * (1) and (2) above are implemented using polling at a frequency of
994 * 64 Hz. The exact frequency is a tradeoff between accuracy and interrupt
995 * overhead. (DEFAULT_RTC_INT_FREQ)
996 * For (3), we use interrupts at 64Hz or user specified periodic
997 * frequency, whichever is higher.
998 */
999#include <linux/mc146818rtc.h>
1000#include <linux/rtc.h>
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +01001001#include <asm/rtc.h>
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001002
1003#define DEFAULT_RTC_INT_FREQ 64
1004#define DEFAULT_RTC_SHIFT 6
1005#define RTC_NUM_INTS 1
1006
1007static unsigned long hpet_rtc_flags;
David Brownell7e2a31d2008-07-23 21:30:47 -07001008static int hpet_prev_update_sec;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001009static struct rtc_time hpet_alarm_time;
1010static unsigned long hpet_pie_count;
Pavel Emelyanovff08f762009-02-04 13:40:31 +03001011static u32 hpet_t1_cmp;
Jan Beulich5946fa32009-08-19 08:44:24 +01001012static u32 hpet_default_delta;
1013static u32 hpet_pie_delta;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001014static unsigned long hpet_pie_limit;
1015
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +01001016static rtc_irq_handler irq_handler;
1017
1018/*
Pavel Emelyanovff08f762009-02-04 13:40:31 +03001019 * Check that the hpet counter c1 is ahead of the c2
1020 */
1021static inline int hpet_cnt_ahead(u32 c1, u32 c2)
1022{
1023 return (s32)(c2 - c1) < 0;
1024}
1025
1026/*
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +01001027 * Registers a IRQ handler.
1028 */
1029int hpet_register_irq_handler(rtc_irq_handler handler)
1030{
1031 if (!is_hpet_enabled())
1032 return -ENODEV;
1033 if (irq_handler)
1034 return -EBUSY;
1035
1036 irq_handler = handler;
1037
1038 return 0;
1039}
1040EXPORT_SYMBOL_GPL(hpet_register_irq_handler);
1041
1042/*
1043 * Deregisters the IRQ handler registered with hpet_register_irq_handler()
1044 * and does cleanup.
1045 */
1046void hpet_unregister_irq_handler(rtc_irq_handler handler)
1047{
1048 if (!is_hpet_enabled())
1049 return;
1050
1051 irq_handler = NULL;
1052 hpet_rtc_flags = 0;
1053}
1054EXPORT_SYMBOL_GPL(hpet_unregister_irq_handler);
1055
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001056/*
1057 * Timer 1 for RTC emulation. We use one shot mode, as periodic mode
1058 * is not supported by all HPET implementations for timer 1.
1059 *
1060 * hpet_rtc_timer_init() is called when the rtc is initialized.
1061 */
1062int hpet_rtc_timer_init(void)
1063{
Jan Beulich5946fa32009-08-19 08:44:24 +01001064 unsigned int cfg, cnt, delta;
1065 unsigned long flags;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001066
1067 if (!is_hpet_enabled())
1068 return 0;
1069
1070 if (!hpet_default_delta) {
1071 uint64_t clc;
1072
1073 clc = (uint64_t) hpet_clockevent.mult * NSEC_PER_SEC;
1074 clc >>= hpet_clockevent.shift + DEFAULT_RTC_SHIFT;
Jan Beulich5946fa32009-08-19 08:44:24 +01001075 hpet_default_delta = clc;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001076 }
1077
1078 if (!(hpet_rtc_flags & RTC_PIE) || hpet_pie_limit)
1079 delta = hpet_default_delta;
1080 else
1081 delta = hpet_pie_delta;
1082
1083 local_irq_save(flags);
1084
1085 cnt = delta + hpet_readl(HPET_COUNTER);
1086 hpet_writel(cnt, HPET_T1_CMP);
1087 hpet_t1_cmp = cnt;
1088
1089 cfg = hpet_readl(HPET_T1_CFG);
1090 cfg &= ~HPET_TN_PERIODIC;
1091 cfg |= HPET_TN_ENABLE | HPET_TN_32BIT;
1092 hpet_writel(cfg, HPET_T1_CFG);
1093
1094 local_irq_restore(flags);
1095
1096 return 1;
1097}
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +01001098EXPORT_SYMBOL_GPL(hpet_rtc_timer_init);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001099
1100/*
1101 * The functions below are called from rtc driver.
1102 * Return 0 if HPET is not being used.
1103 * Otherwise do the necessary changes and return 1.
1104 */
1105int hpet_mask_rtc_irq_bit(unsigned long bit_mask)
1106{
1107 if (!is_hpet_enabled())
1108 return 0;
1109
1110 hpet_rtc_flags &= ~bit_mask;
1111 return 1;
1112}
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +01001113EXPORT_SYMBOL_GPL(hpet_mask_rtc_irq_bit);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001114
1115int hpet_set_rtc_irq_bit(unsigned long bit_mask)
1116{
1117 unsigned long oldbits = hpet_rtc_flags;
1118
1119 if (!is_hpet_enabled())
1120 return 0;
1121
1122 hpet_rtc_flags |= bit_mask;
1123
David Brownell7e2a31d2008-07-23 21:30:47 -07001124 if ((bit_mask & RTC_UIE) && !(oldbits & RTC_UIE))
1125 hpet_prev_update_sec = -1;
1126
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001127 if (!oldbits)
1128 hpet_rtc_timer_init();
1129
1130 return 1;
1131}
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +01001132EXPORT_SYMBOL_GPL(hpet_set_rtc_irq_bit);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001133
1134int hpet_set_alarm_time(unsigned char hrs, unsigned char min,
1135 unsigned char sec)
1136{
1137 if (!is_hpet_enabled())
1138 return 0;
1139
1140 hpet_alarm_time.tm_hour = hrs;
1141 hpet_alarm_time.tm_min = min;
1142 hpet_alarm_time.tm_sec = sec;
1143
1144 return 1;
1145}
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +01001146EXPORT_SYMBOL_GPL(hpet_set_alarm_time);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001147
1148int hpet_set_periodic_freq(unsigned long freq)
1149{
1150 uint64_t clc;
1151
1152 if (!is_hpet_enabled())
1153 return 0;
1154
1155 if (freq <= DEFAULT_RTC_INT_FREQ)
1156 hpet_pie_limit = DEFAULT_RTC_INT_FREQ / freq;
1157 else {
1158 clc = (uint64_t) hpet_clockevent.mult * NSEC_PER_SEC;
1159 do_div(clc, freq);
1160 clc >>= hpet_clockevent.shift;
Jan Beulich5946fa32009-08-19 08:44:24 +01001161 hpet_pie_delta = clc;
Alok Katariab4a5e8a2010-03-11 14:00:16 -08001162 hpet_pie_limit = 0;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001163 }
1164 return 1;
1165}
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +01001166EXPORT_SYMBOL_GPL(hpet_set_periodic_freq);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001167
1168int hpet_rtc_dropped_irq(void)
1169{
1170 return is_hpet_enabled();
1171}
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +01001172EXPORT_SYMBOL_GPL(hpet_rtc_dropped_irq);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001173
1174static void hpet_rtc_timer_reinit(void)
1175{
Jan Beulich5946fa32009-08-19 08:44:24 +01001176 unsigned int cfg, delta;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001177 int lost_ints = -1;
1178
1179 if (unlikely(!hpet_rtc_flags)) {
1180 cfg = hpet_readl(HPET_T1_CFG);
1181 cfg &= ~HPET_TN_ENABLE;
1182 hpet_writel(cfg, HPET_T1_CFG);
1183 return;
1184 }
1185
1186 if (!(hpet_rtc_flags & RTC_PIE) || hpet_pie_limit)
1187 delta = hpet_default_delta;
1188 else
1189 delta = hpet_pie_delta;
1190
1191 /*
1192 * Increment the comparator value until we are ahead of the
1193 * current count.
1194 */
1195 do {
1196 hpet_t1_cmp += delta;
1197 hpet_writel(hpet_t1_cmp, HPET_T1_CMP);
1198 lost_ints++;
Pavel Emelyanovff08f762009-02-04 13:40:31 +03001199 } while (!hpet_cnt_ahead(hpet_t1_cmp, hpet_readl(HPET_COUNTER)));
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001200
1201 if (lost_ints) {
1202 if (hpet_rtc_flags & RTC_PIE)
1203 hpet_pie_count += lost_ints;
1204 if (printk_ratelimit())
David Brownell7e2a31d2008-07-23 21:30:47 -07001205 printk(KERN_WARNING "hpet1: lost %d rtc interrupts\n",
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001206 lost_ints);
1207 }
1208}
1209
1210irqreturn_t hpet_rtc_interrupt(int irq, void *dev_id)
1211{
1212 struct rtc_time curr_time;
1213 unsigned long rtc_int_flag = 0;
1214
1215 hpet_rtc_timer_reinit();
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +01001216 memset(&curr_time, 0, sizeof(struct rtc_time));
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001217
1218 if (hpet_rtc_flags & (RTC_UIE | RTC_AIE))
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +01001219 get_rtc_time(&curr_time);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001220
1221 if (hpet_rtc_flags & RTC_UIE &&
1222 curr_time.tm_sec != hpet_prev_update_sec) {
David Brownell7e2a31d2008-07-23 21:30:47 -07001223 if (hpet_prev_update_sec >= 0)
1224 rtc_int_flag = RTC_UF;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001225 hpet_prev_update_sec = curr_time.tm_sec;
1226 }
1227
1228 if (hpet_rtc_flags & RTC_PIE &&
1229 ++hpet_pie_count >= hpet_pie_limit) {
1230 rtc_int_flag |= RTC_PF;
1231 hpet_pie_count = 0;
1232 }
1233
Bernhard Walle8ee291f2008-01-15 16:44:38 +01001234 if (hpet_rtc_flags & RTC_AIE &&
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001235 (curr_time.tm_sec == hpet_alarm_time.tm_sec) &&
1236 (curr_time.tm_min == hpet_alarm_time.tm_min) &&
1237 (curr_time.tm_hour == hpet_alarm_time.tm_hour))
1238 rtc_int_flag |= RTC_AF;
1239
1240 if (rtc_int_flag) {
1241 rtc_int_flag |= (RTC_IRQF | (RTC_NUM_INTS << 8));
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +01001242 if (irq_handler)
1243 irq_handler(rtc_int_flag, dev_id);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001244 }
1245 return IRQ_HANDLED;
1246}
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +01001247EXPORT_SYMBOL_GPL(hpet_rtc_interrupt);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001248#endif