blob: ba390d731175a492686643dd873786eafda9521b [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)
19#define HPET_SHIFT 22
john stultz5d0cf412006-06-26 00:25:12 -070020
Pavel Machekb10db7f2008-01-30 13:30:00 +010021/* FSEC = 10^-15
22 NSEC = 10^-9 */
Ingo Molnar4588c1f2008-09-06 14:19:17 +020023#define FSEC_PER_NSEC 1000000L
john stultz5d0cf412006-06-26 00:25:12 -070024
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -070025#define HPET_DEV_USED_BIT 2
26#define HPET_DEV_USED (1 << HPET_DEV_USED_BIT)
27#define HPET_DEV_VALID 0x8
28#define HPET_DEV_FSB_CAP 0x1000
29#define HPET_DEV_PERI_CAP 0x2000
30
31#define EVT_TO_HPET_DEV(evt) container_of(evt, struct hpet_dev, evt)
32
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080033/*
34 * HPET address is set in acpi/boot.c, when an ACPI entry exists
35 */
Ingo Molnar4588c1f2008-09-06 14:19:17 +020036unsigned long hpet_address;
Suresh Siddhac8bc6f32009-08-04 12:07:09 -070037u8 hpet_blockid; /* OS timer block num */
Pallipadi, Venkatesh73472a42010-01-21 11:09:52 -080038u8 hpet_msi_disable;
Thomas Gleixner30a564b2010-04-13 15:31:36 +020039u8 hpet_readback_cmp;
Pallipadi, Venkatesh73472a42010-01-21 11:09:52 -080040
Ingo Molnare951e4a2008-11-25 08:42:01 +010041#ifdef CONFIG_PCI_MSI
Hannes Eder3b71e9e2008-11-23 20:19:33 +010042static unsigned long hpet_num_timers;
Ingo Molnare951e4a2008-11-25 08:42:01 +010043#endif
Ingo Molnar4588c1f2008-09-06 14:19:17 +020044static void __iomem *hpet_virt_address;
john stultz5d0cf412006-06-26 00:25:12 -070045
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -070046struct hpet_dev {
Ingo Molnar4588c1f2008-09-06 14:19:17 +020047 struct clock_event_device evt;
48 unsigned int num;
49 int cpu;
50 unsigned int irq;
51 unsigned int flags;
52 char name[10];
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -070053};
54
Jan Beulich5946fa32009-08-19 08:44:24 +010055inline unsigned int hpet_readl(unsigned int a)
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080056{
57 return readl(hpet_virt_address + a);
58}
59
Jan Beulich5946fa32009-08-19 08:44:24 +010060static inline void hpet_writel(unsigned int d, unsigned int a)
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080061{
62 writel(d, hpet_virt_address + a);
63}
64
Thomas Gleixner28769142007-10-12 23:04:06 +020065#ifdef CONFIG_X86_64
Thomas Gleixner28769142007-10-12 23:04:06 +020066#include <asm/pgtable.h>
Yinghai Lu2387ce52008-07-13 14:50:56 -070067#endif
Thomas Gleixner28769142007-10-12 23:04:06 +020068
Thomas Gleixner06a24de2007-10-12 23:04:06 +020069static inline void hpet_set_mapping(void)
70{
71 hpet_virt_address = ioremap_nocache(hpet_address, HPET_MMAP_SIZE);
Yinghai Lu2387ce52008-07-13 14:50:56 -070072#ifdef CONFIG_X86_64
73 __set_fixmap(VSYSCALL_HPET, hpet_address, PAGE_KERNEL_VSYSCALL_NOCACHE);
74#endif
Thomas Gleixner06a24de2007-10-12 23:04:06 +020075}
76
77static inline void hpet_clear_mapping(void)
78{
79 iounmap(hpet_virt_address);
80 hpet_virt_address = NULL;
81}
82
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080083/*
84 * HPET command line enable / disable
85 */
86static int boot_hpet_disable;
Thomas Gleixnerb17530b2007-10-19 20:35:02 +020087int hpet_force_user;
Andreas Herrmannb98103a2009-02-21 00:09:47 +010088static int hpet_verbose;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080089
Ingo Molnar4588c1f2008-09-06 14:19:17 +020090static int __init hpet_setup(char *str)
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080091{
92 if (str) {
93 if (!strncmp("disable", str, 7))
94 boot_hpet_disable = 1;
Thomas Gleixnerb17530b2007-10-19 20:35:02 +020095 if (!strncmp("force", str, 5))
96 hpet_force_user = 1;
Andreas Herrmannb98103a2009-02-21 00:09:47 +010097 if (!strncmp("verbose", str, 7))
98 hpet_verbose = 1;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080099 }
100 return 1;
101}
102__setup("hpet=", hpet_setup);
103
Thomas Gleixner28769142007-10-12 23:04:06 +0200104static int __init disable_hpet(char *str)
105{
106 boot_hpet_disable = 1;
107 return 1;
108}
109__setup("nohpet", disable_hpet);
110
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800111static inline int is_hpet_capable(void)
112{
Ingo Molnar4588c1f2008-09-06 14:19:17 +0200113 return !boot_hpet_disable && hpet_address;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800114}
115
116/*
117 * HPET timer interrupt enable / disable
118 */
119static int hpet_legacy_int_enabled;
120
121/**
122 * is_hpet_enabled - check whether the hpet timer interrupt is enabled
123 */
124int is_hpet_enabled(void)
125{
126 return is_hpet_capable() && hpet_legacy_int_enabled;
127}
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +0100128EXPORT_SYMBOL_GPL(is_hpet_enabled);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800129
Andreas Herrmannb98103a2009-02-21 00:09:47 +0100130static void _hpet_print_config(const char *function, int line)
131{
132 u32 i, timers, l, h;
133 printk(KERN_INFO "hpet: %s(%d):\n", function, line);
134 l = hpet_readl(HPET_ID);
135 h = hpet_readl(HPET_PERIOD);
136 timers = ((l & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT) + 1;
137 printk(KERN_INFO "hpet: ID: 0x%x, PERIOD: 0x%x\n", l, h);
138 l = hpet_readl(HPET_CFG);
139 h = hpet_readl(HPET_STATUS);
140 printk(KERN_INFO "hpet: CFG: 0x%x, STATUS: 0x%x\n", l, h);
141 l = hpet_readl(HPET_COUNTER);
142 h = hpet_readl(HPET_COUNTER+4);
143 printk(KERN_INFO "hpet: COUNTER_l: 0x%x, COUNTER_h: 0x%x\n", l, h);
144
145 for (i = 0; i < timers; i++) {
146 l = hpet_readl(HPET_Tn_CFG(i));
147 h = hpet_readl(HPET_Tn_CFG(i)+4);
148 printk(KERN_INFO "hpet: T%d: CFG_l: 0x%x, CFG_h: 0x%x\n",
149 i, l, h);
150 l = hpet_readl(HPET_Tn_CMP(i));
151 h = hpet_readl(HPET_Tn_CMP(i)+4);
152 printk(KERN_INFO "hpet: T%d: CMP_l: 0x%x, CMP_h: 0x%x\n",
153 i, l, h);
154 l = hpet_readl(HPET_Tn_ROUTE(i));
155 h = hpet_readl(HPET_Tn_ROUTE(i)+4);
156 printk(KERN_INFO "hpet: T%d ROUTE_l: 0x%x, ROUTE_h: 0x%x\n",
157 i, l, h);
158 }
159}
160
161#define hpet_print_config() \
162do { \
163 if (hpet_verbose) \
164 _hpet_print_config(__FUNCTION__, __LINE__); \
165} while (0)
166
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800167/*
168 * When the hpet driver (/dev/hpet) is enabled, we need to reserve
169 * timer 0 and timer 1 in case of RTC emulation.
170 */
171#ifdef CONFIG_HPET
Venki Pallipadif0ed4e62008-09-08 10:18:40 -0700172
Venki Pallipadi5f79f2f2008-09-24 10:03:17 -0700173static void hpet_reserve_msi_timers(struct hpet_data *hd);
Venki Pallipadif0ed4e62008-09-08 10:18:40 -0700174
Jan Beulich5946fa32009-08-19 08:44:24 +0100175static void hpet_reserve_platform_timers(unsigned int id)
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800176{
177 struct hpet __iomem *hpet = hpet_virt_address;
Balaji Rao37a47db82008-01-30 13:30:03 +0100178 struct hpet_timer __iomem *timer = &hpet->hpet_timers[2];
179 unsigned int nrtimers, i;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800180 struct hpet_data hd;
181
182 nrtimers = ((id & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT) + 1;
183
Ingo Molnar4588c1f2008-09-06 14:19:17 +0200184 memset(&hd, 0, sizeof(hd));
185 hd.hd_phys_address = hpet_address;
186 hd.hd_address = hpet;
187 hd.hd_nirqs = nrtimers;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800188 hpet_reserve_timer(&hd, 0);
189
190#ifdef CONFIG_HPET_EMULATE_RTC
191 hpet_reserve_timer(&hd, 1);
192#endif
Thomas Gleixner5761d642008-04-04 16:26:10 +0200193
David Brownell64a76f62008-07-29 12:47:38 -0700194 /*
195 * NOTE that hd_irq[] reflects IOAPIC input pins (LEGACY_8254
196 * is wrong for i8259!) not the output IRQ. Many BIOS writers
197 * don't bother configuring *any* comparator interrupts.
198 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800199 hd.hd_irq[0] = HPET_LEGACY_8254;
200 hd.hd_irq[1] = HPET_LEGACY_RTC;
201
Ingo Molnarfc3fbc42008-04-27 14:04:14 +0200202 for (i = 2; i < nrtimers; timer++, i++) {
Ingo Molnar4588c1f2008-09-06 14:19:17 +0200203 hd.hd_irq[i] = (readl(&timer->hpet_config) &
204 Tn_INT_ROUTE_CNF_MASK) >> Tn_INT_ROUTE_CNF_SHIFT;
Ingo Molnarfc3fbc42008-04-27 14:04:14 +0200205 }
Thomas Gleixner5761d642008-04-04 16:26:10 +0200206
Venki Pallipadif0ed4e62008-09-08 10:18:40 -0700207 hpet_reserve_msi_timers(&hd);
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700208
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800209 hpet_alloc(&hd);
Thomas Gleixner5761d642008-04-04 16:26:10 +0200210
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800211}
212#else
Jan Beulich5946fa32009-08-19 08:44:24 +0100213static void hpet_reserve_platform_timers(unsigned int id) { }
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800214#endif
215
216/*
217 * Common hpet info
218 */
219static unsigned long hpet_period;
220
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200221static void hpet_legacy_set_mode(enum clock_event_mode mode,
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800222 struct clock_event_device *evt);
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200223static int hpet_legacy_next_event(unsigned long delta,
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800224 struct clock_event_device *evt);
225
226/*
227 * The hpet clock event device
228 */
229static struct clock_event_device hpet_clockevent = {
230 .name = "hpet",
231 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200232 .set_mode = hpet_legacy_set_mode,
233 .set_next_event = hpet_legacy_next_event,
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800234 .shift = 32,
235 .irq = 0,
Venki Pallipadi59c69f22007-10-12 23:04:23 +0200236 .rating = 50,
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800237};
238
Andreas Herrmann8d6f0c82009-02-21 00:10:44 +0100239static void hpet_stop_counter(void)
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800240{
241 unsigned long cfg = hpet_readl(HPET_CFG);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800242 cfg &= ~HPET_CFG_ENABLE;
243 hpet_writel(cfg, HPET_CFG);
Andreas Herrmann7a6f9cb2009-04-21 20:00:37 +0200244}
245
246static void hpet_reset_counter(void)
247{
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800248 hpet_writel(0, HPET_COUNTER);
249 hpet_writel(0, HPET_COUNTER + 4);
Andreas Herrmann8d6f0c82009-02-21 00:10:44 +0100250}
251
252static void hpet_start_counter(void)
253{
Jan Beulich5946fa32009-08-19 08:44:24 +0100254 unsigned int cfg = hpet_readl(HPET_CFG);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800255 cfg |= HPET_CFG_ENABLE;
256 hpet_writel(cfg, HPET_CFG);
257}
258
Andreas Herrmann8d6f0c82009-02-21 00:10:44 +0100259static void hpet_restart_counter(void)
260{
261 hpet_stop_counter();
Andreas Herrmann7a6f9cb2009-04-21 20:00:37 +0200262 hpet_reset_counter();
Andreas Herrmann8d6f0c82009-02-21 00:10:44 +0100263 hpet_start_counter();
264}
265
Venki Pallipadi59c69f22007-10-12 23:04:23 +0200266static void hpet_resume_device(void)
267{
Venki Pallipadibfe0c1c2007-10-12 23:04:24 +0200268 force_hpet_resume();
Venki Pallipadi59c69f22007-10-12 23:04:23 +0200269}
270
Magnus Damm17622332010-02-02 14:41:39 -0800271static void hpet_resume_counter(struct clocksource *cs)
Venki Pallipadi59c69f22007-10-12 23:04:23 +0200272{
273 hpet_resume_device();
Andreas Herrmann8d6f0c82009-02-21 00:10:44 +0100274 hpet_restart_counter();
Venki Pallipadi59c69f22007-10-12 23:04:23 +0200275}
276
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200277static void hpet_enable_legacy_int(void)
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800278{
Jan Beulich5946fa32009-08-19 08:44:24 +0100279 unsigned int cfg = hpet_readl(HPET_CFG);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800280
281 cfg |= HPET_CFG_LEGACY;
282 hpet_writel(cfg, HPET_CFG);
283 hpet_legacy_int_enabled = 1;
284}
285
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200286static void hpet_legacy_clockevent_register(void)
287{
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200288 /* Start HPET legacy interrupts */
289 hpet_enable_legacy_int();
290
291 /*
Carlos R. Mafra6fd592d2008-05-05 20:11:22 -0300292 * The mult factor is defined as (include/linux/clockchips.h)
293 * mult/2^shift = cyc/ns (in contrast to ns/cyc in clocksource.h)
294 * hpet_period is in units of femtoseconds (per cycle), so
295 * mult/2^shift = cyc/ns = 10^6/hpet_period
296 * mult = (10^6 * 2^shift)/hpet_period
297 * mult = (FSEC_PER_NSEC << hpet_clockevent.shift)/hpet_period
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200298 */
Carlos R. Mafra6fd592d2008-05-05 20:11:22 -0300299 hpet_clockevent.mult = div_sc((unsigned long) FSEC_PER_NSEC,
300 hpet_period, hpet_clockevent.shift);
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200301 /* Calculate the min / max delta */
302 hpet_clockevent.max_delta_ns = clockevent_delta2ns(0x7FFFFFFF,
303 &hpet_clockevent);
Thomas Gleixner7cfb04352008-09-03 21:37:24 +0000304 /* 5 usec minimum reprogramming delta. */
305 hpet_clockevent.min_delta_ns = 5000;
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200306
307 /*
308 * Start hpet with the boot cpu mask and make it
309 * global after the IO_APIC has been initialized.
310 */
Rusty Russell320ab2b2008-12-13 21:20:26 +1030311 hpet_clockevent.cpumask = cpumask_of(smp_processor_id());
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200312 clockevents_register_device(&hpet_clockevent);
313 global_clock_event = &hpet_clockevent;
314 printk(KERN_DEBUG "hpet clockevent registered\n");
315}
316
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700317static int hpet_setup_msi_irq(unsigned int irq);
318
venkatesh.pallipadi@intel.comb40d5752008-09-05 18:02:16 -0700319static void hpet_set_mode(enum clock_event_mode mode,
320 struct clock_event_device *evt, int timer)
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800321{
Jan Beulich5946fa32009-08-19 08:44:24 +0100322 unsigned int cfg, cmp, now;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800323 uint64_t delta;
324
Ingo Molnar4588c1f2008-09-06 14:19:17 +0200325 switch (mode) {
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800326 case CLOCK_EVT_MODE_PERIODIC:
Andreas Herrmannc23e2532009-02-21 00:16:35 +0100327 hpet_stop_counter();
venkatesh.pallipadi@intel.comb40d5752008-09-05 18:02:16 -0700328 delta = ((uint64_t)(NSEC_PER_SEC/HZ)) * evt->mult;
329 delta >>= evt->shift;
Andreas Herrmann7a6f9cb2009-04-21 20:00:37 +0200330 now = hpet_readl(HPET_COUNTER);
Jan Beulich5946fa32009-08-19 08:44:24 +0100331 cmp = now + (unsigned int) delta;
venkatesh.pallipadi@intel.comb40d5752008-09-05 18:02:16 -0700332 cfg = hpet_readl(HPET_Tn_CFG(timer));
john stultzb13e2462009-02-12 18:48:53 -0800333 /* Make sure we use edge triggered interrupts */
334 cfg &= ~HPET_TN_LEVEL;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800335 cfg |= HPET_TN_ENABLE | HPET_TN_PERIODIC |
336 HPET_TN_SETVAL | HPET_TN_32BIT;
venkatesh.pallipadi@intel.comb40d5752008-09-05 18:02:16 -0700337 hpet_writel(cfg, HPET_Tn_CFG(timer));
Andreas Herrmann7a6f9cb2009-04-21 20:00:37 +0200338 hpet_writel(cmp, HPET_Tn_CMP(timer));
339 udelay(1);
340 /*
341 * HPET on AMD 81xx needs a second write (with HPET_TN_SETVAL
342 * cleared) to T0_CMP to set the period. The HPET_TN_SETVAL
343 * bit is automatically cleared after the first write.
344 * (See AMD-8111 HyperTransport I/O Hub Data Sheet,
345 * Publication # 24674)
346 */
Jan Beulich5946fa32009-08-19 08:44:24 +0100347 hpet_writel((unsigned int) delta, HPET_Tn_CMP(timer));
Andreas Herrmannc23e2532009-02-21 00:16:35 +0100348 hpet_start_counter();
Andreas Herrmannb98103a2009-02-21 00:09:47 +0100349 hpet_print_config();
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800350 break;
351
352 case CLOCK_EVT_MODE_ONESHOT:
venkatesh.pallipadi@intel.comb40d5752008-09-05 18:02:16 -0700353 cfg = hpet_readl(HPET_Tn_CFG(timer));
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800354 cfg &= ~HPET_TN_PERIODIC;
355 cfg |= HPET_TN_ENABLE | HPET_TN_32BIT;
venkatesh.pallipadi@intel.comb40d5752008-09-05 18:02:16 -0700356 hpet_writel(cfg, HPET_Tn_CFG(timer));
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800357 break;
358
359 case CLOCK_EVT_MODE_UNUSED:
360 case CLOCK_EVT_MODE_SHUTDOWN:
venkatesh.pallipadi@intel.comb40d5752008-09-05 18:02:16 -0700361 cfg = hpet_readl(HPET_Tn_CFG(timer));
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800362 cfg &= ~HPET_TN_ENABLE;
venkatesh.pallipadi@intel.comb40d5752008-09-05 18:02:16 -0700363 hpet_writel(cfg, HPET_Tn_CFG(timer));
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800364 break;
Thomas Gleixner18de5bc2007-07-21 04:37:34 -0700365
366 case CLOCK_EVT_MODE_RESUME:
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700367 if (timer == 0) {
368 hpet_enable_legacy_int();
369 } else {
370 struct hpet_dev *hdev = EVT_TO_HPET_DEV(evt);
371 hpet_setup_msi_irq(hdev->irq);
372 disable_irq(hdev->irq);
Rusty Russell0de26522008-12-13 21:20:26 +1030373 irq_set_affinity(hdev->irq, cpumask_of(hdev->cpu));
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700374 enable_irq(hdev->irq);
375 }
Andreas Herrmannb98103a2009-02-21 00:09:47 +0100376 hpet_print_config();
Thomas Gleixner18de5bc2007-07-21 04:37:34 -0700377 break;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800378 }
379}
380
venkatesh.pallipadi@intel.comb40d5752008-09-05 18:02:16 -0700381static int hpet_next_event(unsigned long delta,
382 struct clock_event_device *evt, int timer)
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800383{
Thomas Gleixnerf7676252008-09-06 03:03:32 +0200384 u32 cnt;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800385
386 cnt = hpet_readl(HPET_COUNTER);
Thomas Gleixnerf7676252008-09-06 03:03:32 +0200387 cnt += (u32) delta;
venkatesh.pallipadi@intel.comb40d5752008-09-05 18:02:16 -0700388 hpet_writel(cnt, HPET_Tn_CMP(timer));
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800389
Thomas Gleixner72d43d92008-09-06 03:06:08 +0200390 /*
Thomas Gleixner18ed61d2009-11-27 15:24:44 +0100391 * We need to read back the CMP register on certain HPET
392 * implementations (ATI chipsets) which seem to delay the
393 * transfer of the compare register into the internal compare
394 * logic. With small deltas this might actually be too late as
395 * the counter could already be higher than the compare value
396 * at that point and we would wait for the next hpet interrupt
397 * forever. We found out that reading the CMP register back
398 * forces the transfer so we can rely on the comparison with
Thomas Gleixner30a564b2010-04-13 15:31:36 +0200399 * the counter register below.
400 *
401 * That works fine on those ATI chipsets, but on newer Intel
402 * chipsets (ICH9...) this triggers due to an erratum: Reading
403 * the comparator immediately following a write is returning
404 * the old value.
405 *
406 * We restrict the read back to the affected ATI chipsets (set
407 * by quirks) and also run it with hpet=verbose for debugging
408 * purposes.
Thomas Gleixner72d43d92008-09-06 03:06:08 +0200409 */
Thomas Gleixner30a564b2010-04-13 15:31:36 +0200410 if (hpet_readback_cmp || hpet_verbose) {
411 u32 cmp = hpet_readl(HPET_Tn_CMP(timer));
412
413 if (cmp != cnt)
414 printk_once(KERN_WARNING
415 "hpet: compare register read back failed.\n");
Pallipadi, Venkatesh8da854c2010-02-25 10:53:48 -0800416 }
Thomas Gleixner72d43d92008-09-06 03:06:08 +0200417
Jan Beulich5946fa32009-08-19 08:44:24 +0100418 return (s32)(hpet_readl(HPET_COUNTER) - cnt) >= 0 ? -ETIME : 0;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800419}
420
venkatesh.pallipadi@intel.comb40d5752008-09-05 18:02:16 -0700421static void hpet_legacy_set_mode(enum clock_event_mode mode,
422 struct clock_event_device *evt)
423{
424 hpet_set_mode(mode, evt, 0);
425}
426
427static int hpet_legacy_next_event(unsigned long delta,
428 struct clock_event_device *evt)
429{
430 return hpet_next_event(delta, evt, 0);
431}
432
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800433/*
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -0700434 * HPET MSI Support
435 */
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700436#ifdef CONFIG_PCI_MSI
Venki Pallipadi5f79f2f2008-09-24 10:03:17 -0700437
438static DEFINE_PER_CPU(struct hpet_dev *, cpu_hpet_dev);
439static struct hpet_dev *hpet_devs;
440
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -0700441void hpet_msi_unmask(unsigned int irq)
442{
443 struct hpet_dev *hdev = get_irq_data(irq);
Jan Beulich5946fa32009-08-19 08:44:24 +0100444 unsigned int cfg;
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -0700445
446 /* unmask it */
447 cfg = hpet_readl(HPET_Tn_CFG(hdev->num));
448 cfg |= HPET_TN_FSB;
449 hpet_writel(cfg, HPET_Tn_CFG(hdev->num));
450}
451
452void hpet_msi_mask(unsigned int irq)
453{
Jan Beulich5946fa32009-08-19 08:44:24 +0100454 unsigned int cfg;
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -0700455 struct hpet_dev *hdev = get_irq_data(irq);
456
457 /* mask it */
458 cfg = hpet_readl(HPET_Tn_CFG(hdev->num));
459 cfg &= ~HPET_TN_FSB;
460 hpet_writel(cfg, HPET_Tn_CFG(hdev->num));
461}
462
463void hpet_msi_write(unsigned int irq, struct msi_msg *msg)
464{
465 struct hpet_dev *hdev = get_irq_data(irq);
466
467 hpet_writel(msg->data, HPET_Tn_ROUTE(hdev->num));
468 hpet_writel(msg->address_lo, HPET_Tn_ROUTE(hdev->num) + 4);
469}
470
471void hpet_msi_read(unsigned int irq, struct msi_msg *msg)
472{
473 struct hpet_dev *hdev = get_irq_data(irq);
474
475 msg->data = hpet_readl(HPET_Tn_ROUTE(hdev->num));
476 msg->address_lo = hpet_readl(HPET_Tn_ROUTE(hdev->num) + 4);
477 msg->address_hi = 0;
478}
479
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700480static void hpet_msi_set_mode(enum clock_event_mode mode,
481 struct clock_event_device *evt)
482{
483 struct hpet_dev *hdev = EVT_TO_HPET_DEV(evt);
484 hpet_set_mode(mode, evt, hdev->num);
485}
486
487static int hpet_msi_next_event(unsigned long delta,
488 struct clock_event_device *evt)
489{
490 struct hpet_dev *hdev = EVT_TO_HPET_DEV(evt);
491 return hpet_next_event(delta, evt, hdev->num);
492}
493
494static int hpet_setup_msi_irq(unsigned int irq)
495{
Suresh Siddhac8bc6f32009-08-04 12:07:09 -0700496 if (arch_setup_hpet_msi(irq, hpet_blockid)) {
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700497 destroy_irq(irq);
498 return -EINVAL;
499 }
500 return 0;
501}
502
503static int hpet_assign_irq(struct hpet_dev *dev)
504{
505 unsigned int irq;
506
507 irq = create_irq();
508 if (!irq)
509 return -EINVAL;
510
511 set_irq_data(irq, dev);
512
513 if (hpet_setup_msi_irq(irq))
514 return -EINVAL;
515
516 dev->irq = irq;
517 return 0;
518}
519
520static irqreturn_t hpet_interrupt_handler(int irq, void *data)
521{
522 struct hpet_dev *dev = (struct hpet_dev *)data;
523 struct clock_event_device *hevt = &dev->evt;
524
525 if (!hevt->event_handler) {
526 printk(KERN_INFO "Spurious HPET timer interrupt on HPET timer %d\n",
527 dev->num);
528 return IRQ_HANDLED;
529 }
530
531 hevt->event_handler(hevt);
532 return IRQ_HANDLED;
533}
534
535static int hpet_setup_irq(struct hpet_dev *dev)
536{
537
538 if (request_irq(dev->irq, hpet_interrupt_handler,
Thomas Gleixner507fa3a2009-06-14 17:46:01 +0200539 IRQF_TIMER | IRQF_DISABLED | IRQF_NOBALANCING,
540 dev->name, dev))
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700541 return -1;
542
543 disable_irq(dev->irq);
Rusty Russell0de26522008-12-13 21:20:26 +1030544 irq_set_affinity(dev->irq, cpumask_of(dev->cpu));
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700545 enable_irq(dev->irq);
546
Yinghai Luc81bba42008-09-25 11:53:11 -0700547 printk(KERN_DEBUG "hpet: %s irq %d for MSI\n",
548 dev->name, dev->irq);
549
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700550 return 0;
551}
552
553/* This should be called in specific @cpu */
554static void init_one_hpet_msi_clockevent(struct hpet_dev *hdev, int cpu)
555{
556 struct clock_event_device *evt = &hdev->evt;
557 uint64_t hpet_freq;
558
559 WARN_ON(cpu != smp_processor_id());
560 if (!(hdev->flags & HPET_DEV_VALID))
561 return;
562
563 if (hpet_setup_msi_irq(hdev->irq))
564 return;
565
566 hdev->cpu = cpu;
567 per_cpu(cpu_hpet_dev, cpu) = hdev;
568 evt->name = hdev->name;
569 hpet_setup_irq(hdev);
570 evt->irq = hdev->irq;
571
572 evt->rating = 110;
573 evt->features = CLOCK_EVT_FEAT_ONESHOT;
574 if (hdev->flags & HPET_DEV_PERI_CAP)
575 evt->features |= CLOCK_EVT_FEAT_PERIODIC;
576
577 evt->set_mode = hpet_msi_set_mode;
578 evt->set_next_event = hpet_msi_next_event;
579 evt->shift = 32;
580
581 /*
582 * The period is a femto seconds value. We need to calculate the
583 * scaled math multiplication factor for nanosecond to hpet tick
584 * conversion.
585 */
586 hpet_freq = 1000000000000000ULL;
587 do_div(hpet_freq, hpet_period);
588 evt->mult = div_sc((unsigned long) hpet_freq,
589 NSEC_PER_SEC, evt->shift);
590 /* Calculate the max delta */
591 evt->max_delta_ns = clockevent_delta2ns(0x7FFFFFFF, evt);
592 /* 5 usec minimum reprogramming delta. */
593 evt->min_delta_ns = 5000;
594
Rusty Russell320ab2b2008-12-13 21:20:26 +1030595 evt->cpumask = cpumask_of(hdev->cpu);
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700596 clockevents_register_device(evt);
597}
598
599#ifdef CONFIG_HPET
600/* Reserve at least one timer for userspace (/dev/hpet) */
601#define RESERVE_TIMERS 1
602#else
603#define RESERVE_TIMERS 0
604#endif
Venki Pallipadi5f79f2f2008-09-24 10:03:17 -0700605
606static void hpet_msi_capability_lookup(unsigned int start_timer)
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700607{
608 unsigned int id;
609 unsigned int num_timers;
610 unsigned int num_timers_used = 0;
611 int i;
612
Pallipadi, Venkatesh73472a42010-01-21 11:09:52 -0800613 if (hpet_msi_disable)
614 return;
615
Shaohua Li39fe05e2009-08-12 11:16:12 +0800616 if (boot_cpu_has(X86_FEATURE_ARAT))
617 return;
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700618 id = hpet_readl(HPET_ID);
619
620 num_timers = ((id & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT);
621 num_timers++; /* Value read out starts from 0 */
Andreas Herrmannb98103a2009-02-21 00:09:47 +0100622 hpet_print_config();
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700623
624 hpet_devs = kzalloc(sizeof(struct hpet_dev) * num_timers, GFP_KERNEL);
625 if (!hpet_devs)
626 return;
627
628 hpet_num_timers = num_timers;
629
630 for (i = start_timer; i < num_timers - RESERVE_TIMERS; i++) {
631 struct hpet_dev *hdev = &hpet_devs[num_timers_used];
Jan Beulich5946fa32009-08-19 08:44:24 +0100632 unsigned int cfg = hpet_readl(HPET_Tn_CFG(i));
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700633
634 /* Only consider HPET timer with MSI support */
635 if (!(cfg & HPET_TN_FSB_CAP))
636 continue;
637
638 hdev->flags = 0;
639 if (cfg & HPET_TN_PERIODIC_CAP)
640 hdev->flags |= HPET_DEV_PERI_CAP;
641 hdev->num = i;
642
643 sprintf(hdev->name, "hpet%d", i);
644 if (hpet_assign_irq(hdev))
645 continue;
646
647 hdev->flags |= HPET_DEV_FSB_CAP;
648 hdev->flags |= HPET_DEV_VALID;
649 num_timers_used++;
650 if (num_timers_used == num_possible_cpus())
651 break;
652 }
653
654 printk(KERN_INFO "HPET: %d timers in total, %d timers will be used for per-cpu timer\n",
655 num_timers, num_timers_used);
656}
657
Venki Pallipadi5f79f2f2008-09-24 10:03:17 -0700658#ifdef CONFIG_HPET
659static void hpet_reserve_msi_timers(struct hpet_data *hd)
660{
661 int i;
662
663 if (!hpet_devs)
664 return;
665
666 for (i = 0; i < hpet_num_timers; i++) {
667 struct hpet_dev *hdev = &hpet_devs[i];
668
669 if (!(hdev->flags & HPET_DEV_VALID))
670 continue;
671
672 hd->hd_irq[hdev->num] = hdev->irq;
673 hpet_reserve_timer(hd, hdev->num);
674 }
675}
676#endif
677
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700678static struct hpet_dev *hpet_get_unused_timer(void)
679{
680 int i;
681
682 if (!hpet_devs)
683 return NULL;
684
685 for (i = 0; i < hpet_num_timers; i++) {
686 struct hpet_dev *hdev = &hpet_devs[i];
687
688 if (!(hdev->flags & HPET_DEV_VALID))
689 continue;
690 if (test_and_set_bit(HPET_DEV_USED_BIT,
691 (unsigned long *)&hdev->flags))
692 continue;
693 return hdev;
694 }
695 return NULL;
696}
697
698struct hpet_work_struct {
699 struct delayed_work work;
700 struct completion complete;
701};
702
703static void hpet_work(struct work_struct *w)
704{
705 struct hpet_dev *hdev;
706 int cpu = smp_processor_id();
707 struct hpet_work_struct *hpet_work;
708
709 hpet_work = container_of(w, struct hpet_work_struct, work.work);
710
711 hdev = hpet_get_unused_timer();
712 if (hdev)
713 init_one_hpet_msi_clockevent(hdev, cpu);
714
715 complete(&hpet_work->complete);
716}
717
718static int hpet_cpuhp_notify(struct notifier_block *n,
719 unsigned long action, void *hcpu)
720{
721 unsigned long cpu = (unsigned long)hcpu;
722 struct hpet_work_struct work;
723 struct hpet_dev *hdev = per_cpu(cpu_hpet_dev, cpu);
724
725 switch (action & 0xf) {
726 case CPU_ONLINE:
Thomas Gleixner336f6c32009-01-22 09:50:44 +0100727 INIT_DELAYED_WORK_ON_STACK(&work.work, hpet_work);
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700728 init_completion(&work.complete);
729 /* FIXME: add schedule_work_on() */
730 schedule_delayed_work_on(cpu, &work.work, 0);
731 wait_for_completion(&work.complete);
Thomas Gleixner336f6c32009-01-22 09:50:44 +0100732 destroy_timer_on_stack(&work.work.timer);
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700733 break;
734 case CPU_DEAD:
735 if (hdev) {
736 free_irq(hdev->irq, hdev);
737 hdev->flags &= ~HPET_DEV_USED;
738 per_cpu(cpu_hpet_dev, cpu) = NULL;
739 }
740 break;
741 }
742 return NOTIFY_OK;
743}
744#else
745
Steven Noonanba374c92008-09-08 16:19:09 -0700746static int hpet_setup_msi_irq(unsigned int irq)
747{
748 return 0;
749}
Venki Pallipadi5f79f2f2008-09-24 10:03:17 -0700750static void hpet_msi_capability_lookup(unsigned int start_timer)
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700751{
752 return;
753}
754
Venki Pallipadi5f79f2f2008-09-24 10:03:17 -0700755#ifdef CONFIG_HPET
756static void hpet_reserve_msi_timers(struct hpet_data *hd)
757{
758 return;
759}
760#endif
761
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700762static int hpet_cpuhp_notify(struct notifier_block *n,
763 unsigned long action, void *hcpu)
764{
765 return NOTIFY_OK;
766}
767
768#endif
769
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -0700770/*
john stultz6bb74df2007-03-05 00:30:50 -0800771 * Clock source related code
772 */
Magnus Damm8e196082009-04-21 12:24:00 -0700773static cycle_t read_hpet(struct clocksource *cs)
john stultz6bb74df2007-03-05 00:30:50 -0800774{
775 return (cycle_t)hpet_readl(HPET_COUNTER);
776}
777
Thomas Gleixner28769142007-10-12 23:04:06 +0200778#ifdef CONFIG_X86_64
779static cycle_t __vsyscall_fn vread_hpet(void)
780{
781 return readl((const void __iomem *)fix_to_virt(VSYSCALL_HPET) + 0xf0);
782}
783#endif
784
john stultz6bb74df2007-03-05 00:30:50 -0800785static struct clocksource clocksource_hpet = {
786 .name = "hpet",
787 .rating = 250,
788 .read = read_hpet,
789 .mask = HPET_MASK,
790 .shift = HPET_SHIFT,
791 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
Andreas Herrmann8d6f0c82009-02-21 00:10:44 +0100792 .resume = hpet_resume_counter,
Thomas Gleixner28769142007-10-12 23:04:06 +0200793#ifdef CONFIG_X86_64
794 .vread = vread_hpet,
795#endif
john stultz6bb74df2007-03-05 00:30:50 -0800796};
797
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200798static int hpet_clocksource_register(void)
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800799{
Carlos R. Mafra6fd592d2008-05-05 20:11:22 -0300800 u64 start, now;
Thomas Gleixner075bcd12007-07-21 17:11:12 +0200801 cycle_t t1;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800802
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800803 /* Start the counter */
Andreas Herrmann8d6f0c82009-02-21 00:10:44 +0100804 hpet_restart_counter();
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800805
Thomas Gleixner075bcd12007-07-21 17:11:12 +0200806 /* Verify whether hpet counter works */
Magnus Damm8e196082009-04-21 12:24:00 -0700807 t1 = hpet_readl(HPET_COUNTER);
Thomas Gleixner075bcd12007-07-21 17:11:12 +0200808 rdtscll(start);
809
810 /*
811 * We don't know the TSC frequency yet, but waiting for
812 * 200000 TSC cycles is safe:
813 * 4 GHz == 50us
814 * 1 GHz == 200us
815 */
816 do {
817 rep_nop();
818 rdtscll(now);
819 } while ((now - start) < 200000UL);
820
Magnus Damm8e196082009-04-21 12:24:00 -0700821 if (t1 == hpet_readl(HPET_COUNTER)) {
Thomas Gleixner075bcd12007-07-21 17:11:12 +0200822 printk(KERN_WARNING
823 "HPET counter not counting. HPET disabled\n");
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200824 return -ENODEV;
Thomas Gleixner075bcd12007-07-21 17:11:12 +0200825 }
826
Carlos R. Mafra6fd592d2008-05-05 20:11:22 -0300827 /*
828 * The definition of mult is (include/linux/clocksource.h)
829 * mult/2^shift = ns/cyc and hpet_period is in units of fsec/cyc
830 * so we first need to convert hpet_period to ns/cyc units:
831 * mult/2^shift = ns/cyc = hpet_period/10^6
832 * mult = (hpet_period * 2^shift)/10^6
833 * mult = (hpet_period << shift)/FSEC_PER_NSEC
john stultz6bb74df2007-03-05 00:30:50 -0800834 */
Carlos R. Mafra6fd592d2008-05-05 20:11:22 -0300835 clocksource_hpet.mult = div_sc(hpet_period, FSEC_PER_NSEC, HPET_SHIFT);
john stultz6bb74df2007-03-05 00:30:50 -0800836
837 clocksource_register(&clocksource_hpet);
838
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200839 return 0;
840}
841
Pavel Machekb02a7f22008-02-05 00:48:13 +0100842/**
843 * hpet_enable - Try to setup the HPET timer. Returns 1 on success.
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200844 */
845int __init hpet_enable(void)
846{
Jan Beulich5946fa32009-08-19 08:44:24 +0100847 unsigned int id;
Thomas Gleixnera6825f12008-08-14 12:17:06 +0200848 int i;
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200849
850 if (!is_hpet_capable())
851 return 0;
852
853 hpet_set_mapping();
854
855 /*
856 * Read the period and check for a sane value:
857 */
858 hpet_period = hpet_readl(HPET_PERIOD);
Thomas Gleixnera6825f12008-08-14 12:17:06 +0200859
860 /*
861 * AMD SB700 based systems with spread spectrum enabled use a
862 * SMM based HPET emulation to provide proper frequency
863 * setting. The SMM code is initialized with the first HPET
864 * register access and takes some time to complete. During
865 * this time the config register reads 0xffffffff. We check
866 * for max. 1000 loops whether the config register reads a non
867 * 0xffffffff value to make sure that HPET is up and running
868 * before we go further. A counting loop is safe, as the HPET
869 * access takes thousands of CPU cycles. On non SB700 based
870 * machines this check is only done once and has no side
871 * effects.
872 */
873 for (i = 0; hpet_readl(HPET_CFG) == 0xFFFFFFFF; i++) {
874 if (i == 1000) {
875 printk(KERN_WARNING
876 "HPET config register value = 0xFFFFFFFF. "
877 "Disabling HPET\n");
878 goto out_nohpet;
879 }
880 }
881
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200882 if (hpet_period < HPET_MIN_PERIOD || hpet_period > HPET_MAX_PERIOD)
883 goto out_nohpet;
884
885 /*
886 * Read the HPET ID register to retrieve the IRQ routing
887 * information and the number of channels
888 */
889 id = hpet_readl(HPET_ID);
Andreas Herrmannb98103a2009-02-21 00:09:47 +0100890 hpet_print_config();
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200891
892#ifdef CONFIG_HPET_EMULATE_RTC
893 /*
894 * The legacy routing mode needs at least two channels, tick timer
895 * and the rtc emulation channel.
896 */
897 if (!(id & HPET_ID_NUMBER))
898 goto out_nohpet;
899#endif
900
901 if (hpet_clocksource_register())
902 goto out_nohpet;
903
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800904 if (id & HPET_ID_LEGSUP) {
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200905 hpet_legacy_clockevent_register();
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800906 return 1;
907 }
908 return 0;
909
910out_nohpet:
Thomas Gleixner06a24de2007-10-12 23:04:06 +0200911 hpet_clear_mapping();
Janne Kulmalabacbe992008-12-16 13:39:57 +0200912 hpet_address = 0;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800913 return 0;
914}
915
Thomas Gleixner28769142007-10-12 23:04:06 +0200916/*
917 * Needs to be late, as the reserve_timer code calls kalloc !
918 *
919 * Not a problem on i386 as hpet_enable is called from late_time_init,
920 * but on x86_64 it is necessary !
921 */
922static __init int hpet_late_init(void)
923{
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700924 int cpu;
925
Venki Pallipadi59c69f22007-10-12 23:04:23 +0200926 if (boot_hpet_disable)
Thomas Gleixner28769142007-10-12 23:04:06 +0200927 return -ENODEV;
928
Venki Pallipadi59c69f22007-10-12 23:04:23 +0200929 if (!hpet_address) {
930 if (!force_hpet_address)
931 return -ENODEV;
932
933 hpet_address = force_hpet_address;
934 hpet_enable();
Venki Pallipadi59c69f22007-10-12 23:04:23 +0200935 }
936
Jeremy Fitzhardinge39c04b52008-12-16 12:32:23 -0800937 if (!hpet_virt_address)
938 return -ENODEV;
939
Shaohua Li39fe05e2009-08-12 11:16:12 +0800940 if (hpet_readl(HPET_ID) & HPET_ID_LEGSUP)
941 hpet_msi_capability_lookup(2);
942 else
943 hpet_msi_capability_lookup(0);
944
Thomas Gleixner28769142007-10-12 23:04:06 +0200945 hpet_reserve_platform_timers(hpet_readl(HPET_ID));
Andreas Herrmannb98103a2009-02-21 00:09:47 +0100946 hpet_print_config();
Venki Pallipadi59c69f22007-10-12 23:04:23 +0200947
Pallipadi, Venkatesh73472a42010-01-21 11:09:52 -0800948 if (hpet_msi_disable)
949 return 0;
950
Shaohua Li39fe05e2009-08-12 11:16:12 +0800951 if (boot_cpu_has(X86_FEATURE_ARAT))
952 return 0;
953
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700954 for_each_online_cpu(cpu) {
955 hpet_cpuhp_notify(NULL, CPU_ONLINE, (void *)(long)cpu);
956 }
957
958 /* This notifier should be called after workqueue is ready */
959 hotcpu_notifier(hpet_cpuhp_notify, -20);
960
Thomas Gleixner28769142007-10-12 23:04:06 +0200961 return 0;
962}
963fs_initcall(hpet_late_init);
964
OGAWA Hirofumic86c7fb2007-12-03 17:17:10 +0100965void hpet_disable(void)
966{
Stefano Stabelliniff487802010-07-21 18:32:37 +0100967 if (is_hpet_capable() && hpet_virt_address) {
Jan Beulich5946fa32009-08-19 08:44:24 +0100968 unsigned int cfg = hpet_readl(HPET_CFG);
OGAWA Hirofumic86c7fb2007-12-03 17:17:10 +0100969
970 if (hpet_legacy_int_enabled) {
971 cfg &= ~HPET_CFG_LEGACY;
972 hpet_legacy_int_enabled = 0;
973 }
974 cfg &= ~HPET_CFG_ENABLE;
975 hpet_writel(cfg, HPET_CFG);
976 }
977}
978
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800979#ifdef CONFIG_HPET_EMULATE_RTC
980
981/* HPET in LegacyReplacement Mode eats up RTC interrupt line. When, HPET
982 * is enabled, we support RTC interrupt functionality in software.
983 * RTC has 3 kinds of interrupts:
984 * 1) Update Interrupt - generate an interrupt, every sec, when RTC clock
985 * is updated
986 * 2) Alarm Interrupt - generate an interrupt at a specific time of day
987 * 3) Periodic Interrupt - generate periodic interrupt, with frequencies
988 * 2Hz-8192Hz (2Hz-64Hz for non-root user) (all freqs in powers of 2)
989 * (1) and (2) above are implemented using polling at a frequency of
990 * 64 Hz. The exact frequency is a tradeoff between accuracy and interrupt
991 * overhead. (DEFAULT_RTC_INT_FREQ)
992 * For (3), we use interrupts at 64Hz or user specified periodic
993 * frequency, whichever is higher.
994 */
995#include <linux/mc146818rtc.h>
996#include <linux/rtc.h>
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +0100997#include <asm/rtc.h>
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800998
999#define DEFAULT_RTC_INT_FREQ 64
1000#define DEFAULT_RTC_SHIFT 6
1001#define RTC_NUM_INTS 1
1002
1003static unsigned long hpet_rtc_flags;
David Brownell7e2a31d2008-07-23 21:30:47 -07001004static int hpet_prev_update_sec;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001005static struct rtc_time hpet_alarm_time;
1006static unsigned long hpet_pie_count;
Pavel Emelyanovff08f762009-02-04 13:40:31 +03001007static u32 hpet_t1_cmp;
Jan Beulich5946fa32009-08-19 08:44:24 +01001008static u32 hpet_default_delta;
1009static u32 hpet_pie_delta;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001010static unsigned long hpet_pie_limit;
1011
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +01001012static rtc_irq_handler irq_handler;
1013
1014/*
Pavel Emelyanovff08f762009-02-04 13:40:31 +03001015 * Check that the hpet counter c1 is ahead of the c2
1016 */
1017static inline int hpet_cnt_ahead(u32 c1, u32 c2)
1018{
1019 return (s32)(c2 - c1) < 0;
1020}
1021
1022/*
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +01001023 * Registers a IRQ handler.
1024 */
1025int hpet_register_irq_handler(rtc_irq_handler handler)
1026{
1027 if (!is_hpet_enabled())
1028 return -ENODEV;
1029 if (irq_handler)
1030 return -EBUSY;
1031
1032 irq_handler = handler;
1033
1034 return 0;
1035}
1036EXPORT_SYMBOL_GPL(hpet_register_irq_handler);
1037
1038/*
1039 * Deregisters the IRQ handler registered with hpet_register_irq_handler()
1040 * and does cleanup.
1041 */
1042void hpet_unregister_irq_handler(rtc_irq_handler handler)
1043{
1044 if (!is_hpet_enabled())
1045 return;
1046
1047 irq_handler = NULL;
1048 hpet_rtc_flags = 0;
1049}
1050EXPORT_SYMBOL_GPL(hpet_unregister_irq_handler);
1051
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001052/*
1053 * Timer 1 for RTC emulation. We use one shot mode, as periodic mode
1054 * is not supported by all HPET implementations for timer 1.
1055 *
1056 * hpet_rtc_timer_init() is called when the rtc is initialized.
1057 */
1058int hpet_rtc_timer_init(void)
1059{
Jan Beulich5946fa32009-08-19 08:44:24 +01001060 unsigned int cfg, cnt, delta;
1061 unsigned long flags;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001062
1063 if (!is_hpet_enabled())
1064 return 0;
1065
1066 if (!hpet_default_delta) {
1067 uint64_t clc;
1068
1069 clc = (uint64_t) hpet_clockevent.mult * NSEC_PER_SEC;
1070 clc >>= hpet_clockevent.shift + DEFAULT_RTC_SHIFT;
Jan Beulich5946fa32009-08-19 08:44:24 +01001071 hpet_default_delta = clc;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001072 }
1073
1074 if (!(hpet_rtc_flags & RTC_PIE) || hpet_pie_limit)
1075 delta = hpet_default_delta;
1076 else
1077 delta = hpet_pie_delta;
1078
1079 local_irq_save(flags);
1080
1081 cnt = delta + hpet_readl(HPET_COUNTER);
1082 hpet_writel(cnt, HPET_T1_CMP);
1083 hpet_t1_cmp = cnt;
1084
1085 cfg = hpet_readl(HPET_T1_CFG);
1086 cfg &= ~HPET_TN_PERIODIC;
1087 cfg |= HPET_TN_ENABLE | HPET_TN_32BIT;
1088 hpet_writel(cfg, HPET_T1_CFG);
1089
1090 local_irq_restore(flags);
1091
1092 return 1;
1093}
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +01001094EXPORT_SYMBOL_GPL(hpet_rtc_timer_init);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001095
1096/*
1097 * The functions below are called from rtc driver.
1098 * Return 0 if HPET is not being used.
1099 * Otherwise do the necessary changes and return 1.
1100 */
1101int hpet_mask_rtc_irq_bit(unsigned long bit_mask)
1102{
1103 if (!is_hpet_enabled())
1104 return 0;
1105
1106 hpet_rtc_flags &= ~bit_mask;
1107 return 1;
1108}
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +01001109EXPORT_SYMBOL_GPL(hpet_mask_rtc_irq_bit);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001110
1111int hpet_set_rtc_irq_bit(unsigned long bit_mask)
1112{
1113 unsigned long oldbits = hpet_rtc_flags;
1114
1115 if (!is_hpet_enabled())
1116 return 0;
1117
1118 hpet_rtc_flags |= bit_mask;
1119
David Brownell7e2a31d2008-07-23 21:30:47 -07001120 if ((bit_mask & RTC_UIE) && !(oldbits & RTC_UIE))
1121 hpet_prev_update_sec = -1;
1122
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001123 if (!oldbits)
1124 hpet_rtc_timer_init();
1125
1126 return 1;
1127}
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +01001128EXPORT_SYMBOL_GPL(hpet_set_rtc_irq_bit);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001129
1130int hpet_set_alarm_time(unsigned char hrs, unsigned char min,
1131 unsigned char sec)
1132{
1133 if (!is_hpet_enabled())
1134 return 0;
1135
1136 hpet_alarm_time.tm_hour = hrs;
1137 hpet_alarm_time.tm_min = min;
1138 hpet_alarm_time.tm_sec = sec;
1139
1140 return 1;
1141}
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +01001142EXPORT_SYMBOL_GPL(hpet_set_alarm_time);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001143
1144int hpet_set_periodic_freq(unsigned long freq)
1145{
1146 uint64_t clc;
1147
1148 if (!is_hpet_enabled())
1149 return 0;
1150
1151 if (freq <= DEFAULT_RTC_INT_FREQ)
1152 hpet_pie_limit = DEFAULT_RTC_INT_FREQ / freq;
1153 else {
1154 clc = (uint64_t) hpet_clockevent.mult * NSEC_PER_SEC;
1155 do_div(clc, freq);
1156 clc >>= hpet_clockevent.shift;
Jan Beulich5946fa32009-08-19 08:44:24 +01001157 hpet_pie_delta = clc;
Alok Katariab4a5e8a2010-03-11 14:00:16 -08001158 hpet_pie_limit = 0;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001159 }
1160 return 1;
1161}
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +01001162EXPORT_SYMBOL_GPL(hpet_set_periodic_freq);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001163
1164int hpet_rtc_dropped_irq(void)
1165{
1166 return is_hpet_enabled();
1167}
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +01001168EXPORT_SYMBOL_GPL(hpet_rtc_dropped_irq);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001169
1170static void hpet_rtc_timer_reinit(void)
1171{
Jan Beulich5946fa32009-08-19 08:44:24 +01001172 unsigned int cfg, delta;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001173 int lost_ints = -1;
1174
1175 if (unlikely(!hpet_rtc_flags)) {
1176 cfg = hpet_readl(HPET_T1_CFG);
1177 cfg &= ~HPET_TN_ENABLE;
1178 hpet_writel(cfg, HPET_T1_CFG);
1179 return;
1180 }
1181
1182 if (!(hpet_rtc_flags & RTC_PIE) || hpet_pie_limit)
1183 delta = hpet_default_delta;
1184 else
1185 delta = hpet_pie_delta;
1186
1187 /*
1188 * Increment the comparator value until we are ahead of the
1189 * current count.
1190 */
1191 do {
1192 hpet_t1_cmp += delta;
1193 hpet_writel(hpet_t1_cmp, HPET_T1_CMP);
1194 lost_ints++;
Pavel Emelyanovff08f762009-02-04 13:40:31 +03001195 } while (!hpet_cnt_ahead(hpet_t1_cmp, hpet_readl(HPET_COUNTER)));
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001196
1197 if (lost_ints) {
1198 if (hpet_rtc_flags & RTC_PIE)
1199 hpet_pie_count += lost_ints;
1200 if (printk_ratelimit())
David Brownell7e2a31d2008-07-23 21:30:47 -07001201 printk(KERN_WARNING "hpet1: lost %d rtc interrupts\n",
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001202 lost_ints);
1203 }
1204}
1205
1206irqreturn_t hpet_rtc_interrupt(int irq, void *dev_id)
1207{
1208 struct rtc_time curr_time;
1209 unsigned long rtc_int_flag = 0;
1210
1211 hpet_rtc_timer_reinit();
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +01001212 memset(&curr_time, 0, sizeof(struct rtc_time));
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001213
1214 if (hpet_rtc_flags & (RTC_UIE | RTC_AIE))
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +01001215 get_rtc_time(&curr_time);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001216
1217 if (hpet_rtc_flags & RTC_UIE &&
1218 curr_time.tm_sec != hpet_prev_update_sec) {
David Brownell7e2a31d2008-07-23 21:30:47 -07001219 if (hpet_prev_update_sec >= 0)
1220 rtc_int_flag = RTC_UF;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001221 hpet_prev_update_sec = curr_time.tm_sec;
1222 }
1223
1224 if (hpet_rtc_flags & RTC_PIE &&
1225 ++hpet_pie_count >= hpet_pie_limit) {
1226 rtc_int_flag |= RTC_PF;
1227 hpet_pie_count = 0;
1228 }
1229
Bernhard Walle8ee291f2008-01-15 16:44:38 +01001230 if (hpet_rtc_flags & RTC_AIE &&
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001231 (curr_time.tm_sec == hpet_alarm_time.tm_sec) &&
1232 (curr_time.tm_min == hpet_alarm_time.tm_min) &&
1233 (curr_time.tm_hour == hpet_alarm_time.tm_hour))
1234 rtc_int_flag |= RTC_AF;
1235
1236 if (rtc_int_flag) {
1237 rtc_int_flag |= (RTC_IRQF | (RTC_NUM_INTS << 8));
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +01001238 if (irq_handler)
1239 irq_handler(rtc_int_flag, dev_id);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001240 }
1241 return IRQ_HANDLED;
1242}
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +01001243EXPORT_SYMBOL_GPL(hpet_rtc_interrupt);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001244#endif