blob: ad0de0c2714ecc9a27e12a1cdeb7f0fe1043a5ae [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>
Paul Gortmaker69c60c82011-05-26 12:22:53 -04004#include <linux/export.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>
Ralf Baechle334955e2011-06-01 19:04:57 +01007#include <linux/i8253.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09008#include <linux/slab.h>
john stultz5d0cf412006-06-26 00:25:12 -07009#include <linux/hpet.h>
10#include <linux/init.h>
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -070011#include <linux/cpu.h>
Ingo Molnar4588c1f2008-09-06 14:19:17 +020012#include <linux/pm.h>
13#include <linux/io.h>
john stultz5d0cf412006-06-26 00:25:12 -070014
Thomas Gleixner28769142007-10-12 23:04:06 +020015#include <asm/fixmap.h>
Ingo Molnar4588c1f2008-09-06 14:19:17 +020016#include <asm/hpet.h>
Ralf Baechle16f871b2011-06-01 19:05:06 +010017#include <asm/time.h>
john stultz5d0cf412006-06-26 00:25:12 -070018
Ingo Molnar4588c1f2008-09-06 14:19:17 +020019#define HPET_MASK CLOCKSOURCE_MASK(32)
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
Thomas Gleixnerf1c18072010-12-13 12:43:23 +010031#define HPET_MIN_CYCLES 128
32#define HPET_MIN_PROG_DELTA (HPET_MIN_CYCLES + (HPET_MIN_CYCLES >> 1))
33
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080034/*
35 * HPET address is set in acpi/boot.c, when an ACPI entry exists
36 */
Ingo Molnar4588c1f2008-09-06 14:19:17 +020037unsigned long hpet_address;
Suresh Siddhac8bc6f32009-08-04 12:07:09 -070038u8 hpet_blockid; /* OS timer block num */
Pallipadi, Venkatesh73472a42010-01-21 11:09:52 -080039u8 hpet_msi_disable;
40
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
Ferenc Wagner3f7787b2011-11-18 15:28:22 +010055inline struct hpet_dev *EVT_TO_HPET_DEV(struct clock_event_device *evtdev)
56{
57 return container_of(evtdev, struct hpet_dev, evt);
58}
59
Jan Beulich5946fa32009-08-19 08:44:24 +010060inline unsigned int hpet_readl(unsigned int a)
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080061{
62 return readl(hpet_virt_address + a);
63}
64
Jan Beulich5946fa32009-08-19 08:44:24 +010065static inline void hpet_writel(unsigned int d, unsigned int a)
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080066{
67 writel(d, hpet_virt_address + a);
68}
69
Thomas Gleixner28769142007-10-12 23:04:06 +020070#ifdef CONFIG_X86_64
Thomas Gleixner28769142007-10-12 23:04:06 +020071#include <asm/pgtable.h>
Yinghai Lu2387ce52008-07-13 14:50:56 -070072#endif
Thomas Gleixner28769142007-10-12 23:04:06 +020073
Thomas Gleixner06a24de2007-10-12 23:04:06 +020074static inline void hpet_set_mapping(void)
75{
76 hpet_virt_address = ioremap_nocache(hpet_address, HPET_MMAP_SIZE);
Yinghai Lu2387ce52008-07-13 14:50:56 -070077#ifdef CONFIG_X86_64
Andy Lutomirskid319bb72011-06-05 13:50:21 -040078 __set_fixmap(VSYSCALL_HPET, hpet_address, PAGE_KERNEL_VVAR_NOCACHE);
Yinghai Lu2387ce52008-07-13 14:50:56 -070079#endif
Thomas Gleixner06a24de2007-10-12 23:04:06 +020080}
81
82static inline void hpet_clear_mapping(void)
83{
84 iounmap(hpet_virt_address);
85 hpet_virt_address = NULL;
86}
87
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080088/*
89 * HPET command line enable / disable
90 */
91static int boot_hpet_disable;
Thomas Gleixnerb17530b2007-10-19 20:35:02 +020092int hpet_force_user;
Andreas Herrmannb98103a2009-02-21 00:09:47 +010093static int hpet_verbose;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080094
Ingo Molnar4588c1f2008-09-06 14:19:17 +020095static int __init hpet_setup(char *str)
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080096{
97 if (str) {
98 if (!strncmp("disable", str, 7))
99 boot_hpet_disable = 1;
Thomas Gleixnerb17530b2007-10-19 20:35:02 +0200100 if (!strncmp("force", str, 5))
101 hpet_force_user = 1;
Andreas Herrmannb98103a2009-02-21 00:09:47 +0100102 if (!strncmp("verbose", str, 7))
103 hpet_verbose = 1;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800104 }
105 return 1;
106}
107__setup("hpet=", hpet_setup);
108
Thomas Gleixner28769142007-10-12 23:04:06 +0200109static int __init disable_hpet(char *str)
110{
111 boot_hpet_disable = 1;
112 return 1;
113}
114__setup("nohpet", disable_hpet);
115
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800116static inline int is_hpet_capable(void)
117{
Ingo Molnar4588c1f2008-09-06 14:19:17 +0200118 return !boot_hpet_disable && hpet_address;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800119}
120
121/*
122 * HPET timer interrupt enable / disable
123 */
124static int hpet_legacy_int_enabled;
125
126/**
127 * is_hpet_enabled - check whether the hpet timer interrupt is enabled
128 */
129int is_hpet_enabled(void)
130{
131 return is_hpet_capable() && hpet_legacy_int_enabled;
132}
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +0100133EXPORT_SYMBOL_GPL(is_hpet_enabled);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800134
Andreas Herrmannb98103a2009-02-21 00:09:47 +0100135static void _hpet_print_config(const char *function, int line)
136{
137 u32 i, timers, l, h;
138 printk(KERN_INFO "hpet: %s(%d):\n", function, line);
139 l = hpet_readl(HPET_ID);
140 h = hpet_readl(HPET_PERIOD);
141 timers = ((l & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT) + 1;
142 printk(KERN_INFO "hpet: ID: 0x%x, PERIOD: 0x%x\n", l, h);
143 l = hpet_readl(HPET_CFG);
144 h = hpet_readl(HPET_STATUS);
145 printk(KERN_INFO "hpet: CFG: 0x%x, STATUS: 0x%x\n", l, h);
146 l = hpet_readl(HPET_COUNTER);
147 h = hpet_readl(HPET_COUNTER+4);
148 printk(KERN_INFO "hpet: COUNTER_l: 0x%x, COUNTER_h: 0x%x\n", l, h);
149
150 for (i = 0; i < timers; i++) {
151 l = hpet_readl(HPET_Tn_CFG(i));
152 h = hpet_readl(HPET_Tn_CFG(i)+4);
153 printk(KERN_INFO "hpet: T%d: CFG_l: 0x%x, CFG_h: 0x%x\n",
154 i, l, h);
155 l = hpet_readl(HPET_Tn_CMP(i));
156 h = hpet_readl(HPET_Tn_CMP(i)+4);
157 printk(KERN_INFO "hpet: T%d: CMP_l: 0x%x, CMP_h: 0x%x\n",
158 i, l, h);
159 l = hpet_readl(HPET_Tn_ROUTE(i));
160 h = hpet_readl(HPET_Tn_ROUTE(i)+4);
161 printk(KERN_INFO "hpet: T%d ROUTE_l: 0x%x, ROUTE_h: 0x%x\n",
162 i, l, h);
163 }
164}
165
166#define hpet_print_config() \
167do { \
168 if (hpet_verbose) \
169 _hpet_print_config(__FUNCTION__, __LINE__); \
170} while (0)
171
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800172/*
173 * When the hpet driver (/dev/hpet) is enabled, we need to reserve
174 * timer 0 and timer 1 in case of RTC emulation.
175 */
176#ifdef CONFIG_HPET
Venki Pallipadif0ed4e62008-09-08 10:18:40 -0700177
Venki Pallipadi5f79f2f2008-09-24 10:03:17 -0700178static void hpet_reserve_msi_timers(struct hpet_data *hd);
Venki Pallipadif0ed4e62008-09-08 10:18:40 -0700179
Jan Beulich5946fa32009-08-19 08:44:24 +0100180static void hpet_reserve_platform_timers(unsigned int id)
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800181{
182 struct hpet __iomem *hpet = hpet_virt_address;
Balaji Rao37a47db82008-01-30 13:30:03 +0100183 struct hpet_timer __iomem *timer = &hpet->hpet_timers[2];
184 unsigned int nrtimers, i;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800185 struct hpet_data hd;
186
187 nrtimers = ((id & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT) + 1;
188
Ingo Molnar4588c1f2008-09-06 14:19:17 +0200189 memset(&hd, 0, sizeof(hd));
190 hd.hd_phys_address = hpet_address;
191 hd.hd_address = hpet;
192 hd.hd_nirqs = nrtimers;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800193 hpet_reserve_timer(&hd, 0);
194
195#ifdef CONFIG_HPET_EMULATE_RTC
196 hpet_reserve_timer(&hd, 1);
197#endif
Thomas Gleixner5761d642008-04-04 16:26:10 +0200198
David Brownell64a76f62008-07-29 12:47:38 -0700199 /*
200 * NOTE that hd_irq[] reflects IOAPIC input pins (LEGACY_8254
201 * is wrong for i8259!) not the output IRQ. Many BIOS writers
202 * don't bother configuring *any* comparator interrupts.
203 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800204 hd.hd_irq[0] = HPET_LEGACY_8254;
205 hd.hd_irq[1] = HPET_LEGACY_RTC;
206
Ingo Molnarfc3fbc42008-04-27 14:04:14 +0200207 for (i = 2; i < nrtimers; timer++, i++) {
Ingo Molnar4588c1f2008-09-06 14:19:17 +0200208 hd.hd_irq[i] = (readl(&timer->hpet_config) &
209 Tn_INT_ROUTE_CNF_MASK) >> Tn_INT_ROUTE_CNF_SHIFT;
Ingo Molnarfc3fbc42008-04-27 14:04:14 +0200210 }
Thomas Gleixner5761d642008-04-04 16:26:10 +0200211
Venki Pallipadif0ed4e62008-09-08 10:18:40 -0700212 hpet_reserve_msi_timers(&hd);
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700213
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800214 hpet_alloc(&hd);
Thomas Gleixner5761d642008-04-04 16:26:10 +0200215
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800216}
217#else
Jan Beulich5946fa32009-08-19 08:44:24 +0100218static void hpet_reserve_platform_timers(unsigned int id) { }
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800219#endif
220
221/*
222 * Common hpet info
223 */
Thomas Gleixnerab0e08f2011-05-18 21:33:43 +0000224static unsigned long hpet_freq;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800225
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200226static void hpet_legacy_set_mode(enum clock_event_mode mode,
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800227 struct clock_event_device *evt);
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200228static int hpet_legacy_next_event(unsigned long delta,
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800229 struct clock_event_device *evt);
230
231/*
232 * The hpet clock event device
233 */
234static struct clock_event_device hpet_clockevent = {
235 .name = "hpet",
236 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200237 .set_mode = hpet_legacy_set_mode,
238 .set_next_event = hpet_legacy_next_event,
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800239 .irq = 0,
Venki Pallipadi59c69f22007-10-12 23:04:23 +0200240 .rating = 50,
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800241};
242
Andreas Herrmann8d6f0c82009-02-21 00:10:44 +0100243static void hpet_stop_counter(void)
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800244{
245 unsigned long cfg = hpet_readl(HPET_CFG);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800246 cfg &= ~HPET_CFG_ENABLE;
247 hpet_writel(cfg, HPET_CFG);
Andreas Herrmann7a6f9cb2009-04-21 20:00:37 +0200248}
249
250static void hpet_reset_counter(void)
251{
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800252 hpet_writel(0, HPET_COUNTER);
253 hpet_writel(0, HPET_COUNTER + 4);
Andreas Herrmann8d6f0c82009-02-21 00:10:44 +0100254}
255
256static void hpet_start_counter(void)
257{
Jan Beulich5946fa32009-08-19 08:44:24 +0100258 unsigned int cfg = hpet_readl(HPET_CFG);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800259 cfg |= HPET_CFG_ENABLE;
260 hpet_writel(cfg, HPET_CFG);
261}
262
Andreas Herrmann8d6f0c82009-02-21 00:10:44 +0100263static void hpet_restart_counter(void)
264{
265 hpet_stop_counter();
Andreas Herrmann7a6f9cb2009-04-21 20:00:37 +0200266 hpet_reset_counter();
Andreas Herrmann8d6f0c82009-02-21 00:10:44 +0100267 hpet_start_counter();
268}
269
Venki Pallipadi59c69f22007-10-12 23:04:23 +0200270static void hpet_resume_device(void)
271{
Venki Pallipadibfe0c1c2007-10-12 23:04:24 +0200272 force_hpet_resume();
Venki Pallipadi59c69f22007-10-12 23:04:23 +0200273}
274
Magnus Damm17622332010-02-02 14:41:39 -0800275static void hpet_resume_counter(struct clocksource *cs)
Venki Pallipadi59c69f22007-10-12 23:04:23 +0200276{
277 hpet_resume_device();
Andreas Herrmann8d6f0c82009-02-21 00:10:44 +0100278 hpet_restart_counter();
Venki Pallipadi59c69f22007-10-12 23:04:23 +0200279}
280
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200281static void hpet_enable_legacy_int(void)
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800282{
Jan Beulich5946fa32009-08-19 08:44:24 +0100283 unsigned int cfg = hpet_readl(HPET_CFG);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800284
285 cfg |= HPET_CFG_LEGACY;
286 hpet_writel(cfg, HPET_CFG);
287 hpet_legacy_int_enabled = 1;
288}
289
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200290static void hpet_legacy_clockevent_register(void)
291{
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200292 /* Start HPET legacy interrupts */
293 hpet_enable_legacy_int();
294
295 /*
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200296 * Start hpet with the boot cpu mask and make it
297 * global after the IO_APIC has been initialized.
298 */
Rusty Russell320ab2b2008-12-13 21:20:26 +1030299 hpet_clockevent.cpumask = cpumask_of(smp_processor_id());
Thomas Gleixnerab0e08f2011-05-18 21:33:43 +0000300 clockevents_config_and_register(&hpet_clockevent, hpet_freq,
301 HPET_MIN_PROG_DELTA, 0x7FFFFFFF);
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200302 global_clock_event = &hpet_clockevent;
303 printk(KERN_DEBUG "hpet clockevent registered\n");
304}
305
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700306static int hpet_setup_msi_irq(unsigned int irq);
307
venkatesh.pallipadi@intel.comb40d5752008-09-05 18:02:16 -0700308static void hpet_set_mode(enum clock_event_mode mode,
309 struct clock_event_device *evt, int timer)
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800310{
Jan Beulich5946fa32009-08-19 08:44:24 +0100311 unsigned int cfg, cmp, now;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800312 uint64_t delta;
313
Ingo Molnar4588c1f2008-09-06 14:19:17 +0200314 switch (mode) {
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800315 case CLOCK_EVT_MODE_PERIODIC:
Andreas Herrmannc23e2532009-02-21 00:16:35 +0100316 hpet_stop_counter();
venkatesh.pallipadi@intel.comb40d5752008-09-05 18:02:16 -0700317 delta = ((uint64_t)(NSEC_PER_SEC/HZ)) * evt->mult;
318 delta >>= evt->shift;
Andreas Herrmann7a6f9cb2009-04-21 20:00:37 +0200319 now = hpet_readl(HPET_COUNTER);
Jan Beulich5946fa32009-08-19 08:44:24 +0100320 cmp = now + (unsigned int) delta;
venkatesh.pallipadi@intel.comb40d5752008-09-05 18:02:16 -0700321 cfg = hpet_readl(HPET_Tn_CFG(timer));
john stultzb13e2462009-02-12 18:48:53 -0800322 /* Make sure we use edge triggered interrupts */
323 cfg &= ~HPET_TN_LEVEL;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800324 cfg |= HPET_TN_ENABLE | HPET_TN_PERIODIC |
325 HPET_TN_SETVAL | HPET_TN_32BIT;
venkatesh.pallipadi@intel.comb40d5752008-09-05 18:02:16 -0700326 hpet_writel(cfg, HPET_Tn_CFG(timer));
Andreas Herrmann7a6f9cb2009-04-21 20:00:37 +0200327 hpet_writel(cmp, HPET_Tn_CMP(timer));
328 udelay(1);
329 /*
330 * HPET on AMD 81xx needs a second write (with HPET_TN_SETVAL
331 * cleared) to T0_CMP to set the period. The HPET_TN_SETVAL
332 * bit is automatically cleared after the first write.
333 * (See AMD-8111 HyperTransport I/O Hub Data Sheet,
334 * Publication # 24674)
335 */
Jan Beulich5946fa32009-08-19 08:44:24 +0100336 hpet_writel((unsigned int) delta, HPET_Tn_CMP(timer));
Andreas Herrmannc23e2532009-02-21 00:16:35 +0100337 hpet_start_counter();
Andreas Herrmannb98103a2009-02-21 00:09:47 +0100338 hpet_print_config();
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800339 break;
340
341 case CLOCK_EVT_MODE_ONESHOT:
venkatesh.pallipadi@intel.comb40d5752008-09-05 18:02:16 -0700342 cfg = hpet_readl(HPET_Tn_CFG(timer));
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800343 cfg &= ~HPET_TN_PERIODIC;
344 cfg |= HPET_TN_ENABLE | HPET_TN_32BIT;
venkatesh.pallipadi@intel.comb40d5752008-09-05 18:02:16 -0700345 hpet_writel(cfg, HPET_Tn_CFG(timer));
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800346 break;
347
348 case CLOCK_EVT_MODE_UNUSED:
349 case CLOCK_EVT_MODE_SHUTDOWN:
venkatesh.pallipadi@intel.comb40d5752008-09-05 18:02:16 -0700350 cfg = hpet_readl(HPET_Tn_CFG(timer));
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800351 cfg &= ~HPET_TN_ENABLE;
venkatesh.pallipadi@intel.comb40d5752008-09-05 18:02:16 -0700352 hpet_writel(cfg, HPET_Tn_CFG(timer));
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800353 break;
Thomas Gleixner18de5bc2007-07-21 04:37:34 -0700354
355 case CLOCK_EVT_MODE_RESUME:
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700356 if (timer == 0) {
357 hpet_enable_legacy_int();
358 } else {
359 struct hpet_dev *hdev = EVT_TO_HPET_DEV(evt);
360 hpet_setup_msi_irq(hdev->irq);
361 disable_irq(hdev->irq);
Rusty Russell0de26522008-12-13 21:20:26 +1030362 irq_set_affinity(hdev->irq, cpumask_of(hdev->cpu));
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700363 enable_irq(hdev->irq);
364 }
Andreas Herrmannb98103a2009-02-21 00:09:47 +0100365 hpet_print_config();
Thomas Gleixner18de5bc2007-07-21 04:37:34 -0700366 break;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800367 }
368}
369
venkatesh.pallipadi@intel.comb40d5752008-09-05 18:02:16 -0700370static int hpet_next_event(unsigned long delta,
371 struct clock_event_device *evt, int timer)
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800372{
Thomas Gleixnerf7676252008-09-06 03:03:32 +0200373 u32 cnt;
Thomas Gleixner995bd3b2010-09-15 15:11:57 +0200374 s32 res;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800375
376 cnt = hpet_readl(HPET_COUNTER);
Thomas Gleixnerf7676252008-09-06 03:03:32 +0200377 cnt += (u32) delta;
venkatesh.pallipadi@intel.comb40d5752008-09-05 18:02:16 -0700378 hpet_writel(cnt, HPET_Tn_CMP(timer));
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800379
Thomas Gleixner72d43d92008-09-06 03:06:08 +0200380 /*
Thomas Gleixner995bd3b2010-09-15 15:11:57 +0200381 * HPETs are a complete disaster. The compare register is
382 * based on a equal comparison and neither provides a less
383 * than or equal functionality (which would require to take
384 * the wraparound into account) nor a simple count down event
385 * mode. Further the write to the comparator register is
386 * delayed internally up to two HPET clock cycles in certain
Thomas Gleixnerf1c18072010-12-13 12:43:23 +0100387 * chipsets (ATI, ICH9,10). Some newer AMD chipsets have even
388 * longer delays. We worked around that by reading back the
389 * compare register, but that required another workaround for
390 * ICH9,10 chips where the first readout after write can
391 * return the old stale value. We already had a minimum
392 * programming delta of 5us enforced, but a NMI or SMI hitting
Thomas Gleixner995bd3b2010-09-15 15:11:57 +0200393 * between the counter readout and the comparator write can
394 * move us behind that point easily. Now instead of reading
395 * the compare register back several times, we make the ETIME
396 * decision based on the following: Return ETIME if the
Thomas Gleixnerf1c18072010-12-13 12:43:23 +0100397 * counter value after the write is less than HPET_MIN_CYCLES
Thomas Gleixner995bd3b2010-09-15 15:11:57 +0200398 * away from the event or if the counter is already ahead of
Thomas Gleixnerf1c18072010-12-13 12:43:23 +0100399 * the event. The minimum programming delta for the generic
400 * clockevents code is set to 1.5 * HPET_MIN_CYCLES.
Thomas Gleixner72d43d92008-09-06 03:06:08 +0200401 */
Thomas Gleixner995bd3b2010-09-15 15:11:57 +0200402 res = (s32)(cnt - hpet_readl(HPET_COUNTER));
Thomas Gleixner72d43d92008-09-06 03:06:08 +0200403
Thomas Gleixnerf1c18072010-12-13 12:43:23 +0100404 return res < HPET_MIN_CYCLES ? -ETIME : 0;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800405}
406
venkatesh.pallipadi@intel.comb40d5752008-09-05 18:02:16 -0700407static void hpet_legacy_set_mode(enum clock_event_mode mode,
408 struct clock_event_device *evt)
409{
410 hpet_set_mode(mode, evt, 0);
411}
412
413static int hpet_legacy_next_event(unsigned long delta,
414 struct clock_event_device *evt)
415{
416 return hpet_next_event(delta, evt, 0);
417}
418
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800419/*
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -0700420 * HPET MSI Support
421 */
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700422#ifdef CONFIG_PCI_MSI
Venki Pallipadi5f79f2f2008-09-24 10:03:17 -0700423
424static DEFINE_PER_CPU(struct hpet_dev *, cpu_hpet_dev);
425static struct hpet_dev *hpet_devs;
426
Thomas Gleixnerd0fbca82010-09-28 16:18:39 +0200427void hpet_msi_unmask(struct irq_data *data)
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -0700428{
Thomas Gleixnerd0fbca82010-09-28 16:18:39 +0200429 struct hpet_dev *hdev = data->handler_data;
Jan Beulich5946fa32009-08-19 08:44:24 +0100430 unsigned int cfg;
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -0700431
432 /* unmask it */
433 cfg = hpet_readl(HPET_Tn_CFG(hdev->num));
434 cfg |= HPET_TN_FSB;
435 hpet_writel(cfg, HPET_Tn_CFG(hdev->num));
436}
437
Thomas Gleixnerd0fbca82010-09-28 16:18:39 +0200438void hpet_msi_mask(struct irq_data *data)
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -0700439{
Thomas Gleixnerd0fbca82010-09-28 16:18:39 +0200440 struct hpet_dev *hdev = data->handler_data;
Jan Beulich5946fa32009-08-19 08:44:24 +0100441 unsigned int cfg;
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -0700442
443 /* mask it */
444 cfg = hpet_readl(HPET_Tn_CFG(hdev->num));
445 cfg &= ~HPET_TN_FSB;
446 hpet_writel(cfg, HPET_Tn_CFG(hdev->num));
447}
448
Thomas Gleixnerd0fbca82010-09-28 16:18:39 +0200449void hpet_msi_write(struct hpet_dev *hdev, struct msi_msg *msg)
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -0700450{
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -0700451 hpet_writel(msg->data, HPET_Tn_ROUTE(hdev->num));
452 hpet_writel(msg->address_lo, HPET_Tn_ROUTE(hdev->num) + 4);
453}
454
Thomas Gleixnerd0fbca82010-09-28 16:18:39 +0200455void hpet_msi_read(struct hpet_dev *hdev, struct msi_msg *msg)
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -0700456{
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -0700457 msg->data = hpet_readl(HPET_Tn_ROUTE(hdev->num));
458 msg->address_lo = hpet_readl(HPET_Tn_ROUTE(hdev->num) + 4);
459 msg->address_hi = 0;
460}
461
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700462static void hpet_msi_set_mode(enum clock_event_mode mode,
463 struct clock_event_device *evt)
464{
465 struct hpet_dev *hdev = EVT_TO_HPET_DEV(evt);
466 hpet_set_mode(mode, evt, hdev->num);
467}
468
469static int hpet_msi_next_event(unsigned long delta,
470 struct clock_event_device *evt)
471{
472 struct hpet_dev *hdev = EVT_TO_HPET_DEV(evt);
473 return hpet_next_event(delta, evt, hdev->num);
474}
475
476static int hpet_setup_msi_irq(unsigned int irq)
477{
Suresh Siddhac8bc6f32009-08-04 12:07:09 -0700478 if (arch_setup_hpet_msi(irq, hpet_blockid)) {
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700479 destroy_irq(irq);
480 return -EINVAL;
481 }
482 return 0;
483}
484
485static int hpet_assign_irq(struct hpet_dev *dev)
486{
487 unsigned int irq;
488
Thomas Gleixner02198962010-09-28 23:20:23 +0200489 irq = create_irq_nr(0, -1);
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700490 if (!irq)
491 return -EINVAL;
492
Thomas Gleixner2c778652011-03-12 12:20:43 +0100493 irq_set_handler_data(irq, dev);
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700494
495 if (hpet_setup_msi_irq(irq))
496 return -EINVAL;
497
498 dev->irq = irq;
499 return 0;
500}
501
502static irqreturn_t hpet_interrupt_handler(int irq, void *data)
503{
504 struct hpet_dev *dev = (struct hpet_dev *)data;
505 struct clock_event_device *hevt = &dev->evt;
506
507 if (!hevt->event_handler) {
508 printk(KERN_INFO "Spurious HPET timer interrupt on HPET timer %d\n",
509 dev->num);
510 return IRQ_HANDLED;
511 }
512
513 hevt->event_handler(hevt);
514 return IRQ_HANDLED;
515}
516
517static int hpet_setup_irq(struct hpet_dev *dev)
518{
519
520 if (request_irq(dev->irq, hpet_interrupt_handler,
Thomas Gleixner507fa3a2009-06-14 17:46:01 +0200521 IRQF_TIMER | IRQF_DISABLED | IRQF_NOBALANCING,
522 dev->name, dev))
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700523 return -1;
524
525 disable_irq(dev->irq);
Rusty Russell0de26522008-12-13 21:20:26 +1030526 irq_set_affinity(dev->irq, cpumask_of(dev->cpu));
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700527 enable_irq(dev->irq);
528
Yinghai Luc81bba42008-09-25 11:53:11 -0700529 printk(KERN_DEBUG "hpet: %s irq %d for MSI\n",
530 dev->name, dev->irq);
531
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700532 return 0;
533}
534
535/* This should be called in specific @cpu */
536static void init_one_hpet_msi_clockevent(struct hpet_dev *hdev, int cpu)
537{
538 struct clock_event_device *evt = &hdev->evt;
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700539
540 WARN_ON(cpu != smp_processor_id());
541 if (!(hdev->flags & HPET_DEV_VALID))
542 return;
543
544 if (hpet_setup_msi_irq(hdev->irq))
545 return;
546
547 hdev->cpu = cpu;
548 per_cpu(cpu_hpet_dev, cpu) = hdev;
549 evt->name = hdev->name;
550 hpet_setup_irq(hdev);
551 evt->irq = hdev->irq;
552
553 evt->rating = 110;
554 evt->features = CLOCK_EVT_FEAT_ONESHOT;
555 if (hdev->flags & HPET_DEV_PERI_CAP)
556 evt->features |= CLOCK_EVT_FEAT_PERIODIC;
557
558 evt->set_mode = hpet_msi_set_mode;
559 evt->set_next_event = hpet_msi_next_event;
Rusty Russell320ab2b2008-12-13 21:20:26 +1030560 evt->cpumask = cpumask_of(hdev->cpu);
Thomas Gleixnerab0e08f2011-05-18 21:33:43 +0000561
562 clockevents_config_and_register(evt, hpet_freq, HPET_MIN_PROG_DELTA,
563 0x7FFFFFFF);
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700564}
565
566#ifdef CONFIG_HPET
567/* Reserve at least one timer for userspace (/dev/hpet) */
568#define RESERVE_TIMERS 1
569#else
570#define RESERVE_TIMERS 0
571#endif
Venki Pallipadi5f79f2f2008-09-24 10:03:17 -0700572
573static void hpet_msi_capability_lookup(unsigned int start_timer)
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700574{
575 unsigned int id;
576 unsigned int num_timers;
577 unsigned int num_timers_used = 0;
578 int i;
579
Pallipadi, Venkatesh73472a42010-01-21 11:09:52 -0800580 if (hpet_msi_disable)
581 return;
582
Shaohua Li39fe05e2009-08-12 11:16:12 +0800583 if (boot_cpu_has(X86_FEATURE_ARAT))
584 return;
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700585 id = hpet_readl(HPET_ID);
586
587 num_timers = ((id & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT);
588 num_timers++; /* Value read out starts from 0 */
Andreas Herrmannb98103a2009-02-21 00:09:47 +0100589 hpet_print_config();
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700590
591 hpet_devs = kzalloc(sizeof(struct hpet_dev) * num_timers, GFP_KERNEL);
592 if (!hpet_devs)
593 return;
594
595 hpet_num_timers = num_timers;
596
597 for (i = start_timer; i < num_timers - RESERVE_TIMERS; i++) {
598 struct hpet_dev *hdev = &hpet_devs[num_timers_used];
Jan Beulich5946fa32009-08-19 08:44:24 +0100599 unsigned int cfg = hpet_readl(HPET_Tn_CFG(i));
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700600
601 /* Only consider HPET timer with MSI support */
602 if (!(cfg & HPET_TN_FSB_CAP))
603 continue;
604
605 hdev->flags = 0;
606 if (cfg & HPET_TN_PERIODIC_CAP)
607 hdev->flags |= HPET_DEV_PERI_CAP;
608 hdev->num = i;
609
610 sprintf(hdev->name, "hpet%d", i);
611 if (hpet_assign_irq(hdev))
612 continue;
613
614 hdev->flags |= HPET_DEV_FSB_CAP;
615 hdev->flags |= HPET_DEV_VALID;
616 num_timers_used++;
617 if (num_timers_used == num_possible_cpus())
618 break;
619 }
620
621 printk(KERN_INFO "HPET: %d timers in total, %d timers will be used for per-cpu timer\n",
622 num_timers, num_timers_used);
623}
624
Venki Pallipadi5f79f2f2008-09-24 10:03:17 -0700625#ifdef CONFIG_HPET
626static void hpet_reserve_msi_timers(struct hpet_data *hd)
627{
628 int i;
629
630 if (!hpet_devs)
631 return;
632
633 for (i = 0; i < hpet_num_timers; i++) {
634 struct hpet_dev *hdev = &hpet_devs[i];
635
636 if (!(hdev->flags & HPET_DEV_VALID))
637 continue;
638
639 hd->hd_irq[hdev->num] = hdev->irq;
640 hpet_reserve_timer(hd, hdev->num);
641 }
642}
643#endif
644
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700645static struct hpet_dev *hpet_get_unused_timer(void)
646{
647 int i;
648
649 if (!hpet_devs)
650 return NULL;
651
652 for (i = 0; i < hpet_num_timers; i++) {
653 struct hpet_dev *hdev = &hpet_devs[i];
654
655 if (!(hdev->flags & HPET_DEV_VALID))
656 continue;
657 if (test_and_set_bit(HPET_DEV_USED_BIT,
658 (unsigned long *)&hdev->flags))
659 continue;
660 return hdev;
661 }
662 return NULL;
663}
664
665struct hpet_work_struct {
666 struct delayed_work work;
667 struct completion complete;
668};
669
670static void hpet_work(struct work_struct *w)
671{
672 struct hpet_dev *hdev;
673 int cpu = smp_processor_id();
674 struct hpet_work_struct *hpet_work;
675
676 hpet_work = container_of(w, struct hpet_work_struct, work.work);
677
678 hdev = hpet_get_unused_timer();
679 if (hdev)
680 init_one_hpet_msi_clockevent(hdev, cpu);
681
682 complete(&hpet_work->complete);
683}
684
685static int hpet_cpuhp_notify(struct notifier_block *n,
686 unsigned long action, void *hcpu)
687{
688 unsigned long cpu = (unsigned long)hcpu;
689 struct hpet_work_struct work;
690 struct hpet_dev *hdev = per_cpu(cpu_hpet_dev, cpu);
691
692 switch (action & 0xf) {
693 case CPU_ONLINE:
Andrew Mortonca1cab32010-10-26 14:22:34 -0700694 INIT_DELAYED_WORK_ONSTACK(&work.work, hpet_work);
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700695 init_completion(&work.complete);
696 /* FIXME: add schedule_work_on() */
697 schedule_delayed_work_on(cpu, &work.work, 0);
698 wait_for_completion(&work.complete);
Thomas Gleixner336f6c32009-01-22 09:50:44 +0100699 destroy_timer_on_stack(&work.work.timer);
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700700 break;
701 case CPU_DEAD:
702 if (hdev) {
703 free_irq(hdev->irq, hdev);
704 hdev->flags &= ~HPET_DEV_USED;
705 per_cpu(cpu_hpet_dev, cpu) = NULL;
706 }
707 break;
708 }
709 return NOTIFY_OK;
710}
711#else
712
Steven Noonanba374c92008-09-08 16:19:09 -0700713static int hpet_setup_msi_irq(unsigned int irq)
714{
715 return 0;
716}
Venki Pallipadi5f79f2f2008-09-24 10:03:17 -0700717static void hpet_msi_capability_lookup(unsigned int start_timer)
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700718{
719 return;
720}
721
Venki Pallipadi5f79f2f2008-09-24 10:03:17 -0700722#ifdef CONFIG_HPET
723static void hpet_reserve_msi_timers(struct hpet_data *hd)
724{
725 return;
726}
727#endif
728
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700729static int hpet_cpuhp_notify(struct notifier_block *n,
730 unsigned long action, void *hcpu)
731{
732 return NOTIFY_OK;
733}
734
735#endif
736
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -0700737/*
john stultz6bb74df2007-03-05 00:30:50 -0800738 * Clock source related code
739 */
Magnus Damm8e196082009-04-21 12:24:00 -0700740static cycle_t read_hpet(struct clocksource *cs)
john stultz6bb74df2007-03-05 00:30:50 -0800741{
742 return (cycle_t)hpet_readl(HPET_COUNTER);
743}
744
745static struct clocksource clocksource_hpet = {
746 .name = "hpet",
747 .rating = 250,
748 .read = read_hpet,
749 .mask = HPET_MASK,
john stultz6bb74df2007-03-05 00:30:50 -0800750 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
Andreas Herrmann8d6f0c82009-02-21 00:10:44 +0100751 .resume = hpet_resume_counter,
Thomas Gleixner28769142007-10-12 23:04:06 +0200752#ifdef CONFIG_X86_64
Andy Lutomirski98d0ac32011-07-14 06:47:22 -0400753 .archdata = { .vclock_mode = VCLOCK_HPET },
Thomas Gleixner28769142007-10-12 23:04:06 +0200754#endif
john stultz6bb74df2007-03-05 00:30:50 -0800755};
756
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200757static int hpet_clocksource_register(void)
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800758{
Carlos R. Mafra6fd592d2008-05-05 20:11:22 -0300759 u64 start, now;
Thomas Gleixner075bcd12007-07-21 17:11:12 +0200760 cycle_t t1;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800761
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800762 /* Start the counter */
Andreas Herrmann8d6f0c82009-02-21 00:10:44 +0100763 hpet_restart_counter();
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800764
Thomas Gleixner075bcd12007-07-21 17:11:12 +0200765 /* Verify whether hpet counter works */
Magnus Damm8e196082009-04-21 12:24:00 -0700766 t1 = hpet_readl(HPET_COUNTER);
Thomas Gleixner075bcd12007-07-21 17:11:12 +0200767 rdtscll(start);
768
769 /*
770 * We don't know the TSC frequency yet, but waiting for
771 * 200000 TSC cycles is safe:
772 * 4 GHz == 50us
773 * 1 GHz == 200us
774 */
775 do {
776 rep_nop();
777 rdtscll(now);
778 } while ((now - start) < 200000UL);
779
Magnus Damm8e196082009-04-21 12:24:00 -0700780 if (t1 == hpet_readl(HPET_COUNTER)) {
Thomas Gleixner075bcd12007-07-21 17:11:12 +0200781 printk(KERN_WARNING
782 "HPET counter not counting. HPET disabled\n");
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200783 return -ENODEV;
Thomas Gleixner075bcd12007-07-21 17:11:12 +0200784 }
785
John Stultzf12a15b2010-07-13 17:56:27 -0700786 clocksource_register_hz(&clocksource_hpet, (u32)hpet_freq);
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200787 return 0;
788}
789
Pavel Machekb02a7f22008-02-05 00:48:13 +0100790/**
791 * hpet_enable - Try to setup the HPET timer. Returns 1 on success.
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200792 */
793int __init hpet_enable(void)
794{
Thomas Gleixnerab0e08f2011-05-18 21:33:43 +0000795 unsigned long hpet_period;
Jan Beulich5946fa32009-08-19 08:44:24 +0100796 unsigned int id;
Thomas Gleixnerab0e08f2011-05-18 21:33:43 +0000797 u64 freq;
Thomas Gleixnera6825f12008-08-14 12:17:06 +0200798 int i;
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200799
800 if (!is_hpet_capable())
801 return 0;
802
803 hpet_set_mapping();
804
805 /*
806 * Read the period and check for a sane value:
807 */
808 hpet_period = hpet_readl(HPET_PERIOD);
Thomas Gleixnera6825f12008-08-14 12:17:06 +0200809
810 /*
811 * AMD SB700 based systems with spread spectrum enabled use a
812 * SMM based HPET emulation to provide proper frequency
813 * setting. The SMM code is initialized with the first HPET
814 * register access and takes some time to complete. During
815 * this time the config register reads 0xffffffff. We check
816 * for max. 1000 loops whether the config register reads a non
817 * 0xffffffff value to make sure that HPET is up and running
818 * before we go further. A counting loop is safe, as the HPET
819 * access takes thousands of CPU cycles. On non SB700 based
820 * machines this check is only done once and has no side
821 * effects.
822 */
823 for (i = 0; hpet_readl(HPET_CFG) == 0xFFFFFFFF; i++) {
824 if (i == 1000) {
825 printk(KERN_WARNING
826 "HPET config register value = 0xFFFFFFFF. "
827 "Disabling HPET\n");
828 goto out_nohpet;
829 }
830 }
831
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200832 if (hpet_period < HPET_MIN_PERIOD || hpet_period > HPET_MAX_PERIOD)
833 goto out_nohpet;
834
835 /*
Thomas Gleixnerab0e08f2011-05-18 21:33:43 +0000836 * The period is a femto seconds value. Convert it to a
837 * frequency.
838 */
839 freq = FSEC_PER_SEC;
840 do_div(freq, hpet_period);
841 hpet_freq = freq;
842
843 /*
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200844 * Read the HPET ID register to retrieve the IRQ routing
845 * information and the number of channels
846 */
847 id = hpet_readl(HPET_ID);
Andreas Herrmannb98103a2009-02-21 00:09:47 +0100848 hpet_print_config();
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200849
850#ifdef CONFIG_HPET_EMULATE_RTC
851 /*
852 * The legacy routing mode needs at least two channels, tick timer
853 * and the rtc emulation channel.
854 */
855 if (!(id & HPET_ID_NUMBER))
856 goto out_nohpet;
857#endif
858
859 if (hpet_clocksource_register())
860 goto out_nohpet;
861
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800862 if (id & HPET_ID_LEGSUP) {
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200863 hpet_legacy_clockevent_register();
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800864 return 1;
865 }
866 return 0;
867
868out_nohpet:
Thomas Gleixner06a24de2007-10-12 23:04:06 +0200869 hpet_clear_mapping();
Janne Kulmalabacbe992008-12-16 13:39:57 +0200870 hpet_address = 0;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800871 return 0;
872}
873
Thomas Gleixner28769142007-10-12 23:04:06 +0200874/*
875 * Needs to be late, as the reserve_timer code calls kalloc !
876 *
877 * Not a problem on i386 as hpet_enable is called from late_time_init,
878 * but on x86_64 it is necessary !
879 */
880static __init int hpet_late_init(void)
881{
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700882 int cpu;
883
Venki Pallipadi59c69f22007-10-12 23:04:23 +0200884 if (boot_hpet_disable)
Thomas Gleixner28769142007-10-12 23:04:06 +0200885 return -ENODEV;
886
Venki Pallipadi59c69f22007-10-12 23:04:23 +0200887 if (!hpet_address) {
888 if (!force_hpet_address)
889 return -ENODEV;
890
891 hpet_address = force_hpet_address;
892 hpet_enable();
Venki Pallipadi59c69f22007-10-12 23:04:23 +0200893 }
894
Jeremy Fitzhardinge39c04b52008-12-16 12:32:23 -0800895 if (!hpet_virt_address)
896 return -ENODEV;
897
Shaohua Li39fe05e2009-08-12 11:16:12 +0800898 if (hpet_readl(HPET_ID) & HPET_ID_LEGSUP)
899 hpet_msi_capability_lookup(2);
900 else
901 hpet_msi_capability_lookup(0);
902
Thomas Gleixner28769142007-10-12 23:04:06 +0200903 hpet_reserve_platform_timers(hpet_readl(HPET_ID));
Andreas Herrmannb98103a2009-02-21 00:09:47 +0100904 hpet_print_config();
Venki Pallipadi59c69f22007-10-12 23:04:23 +0200905
Pallipadi, Venkatesh73472a42010-01-21 11:09:52 -0800906 if (hpet_msi_disable)
907 return 0;
908
Shaohua Li39fe05e2009-08-12 11:16:12 +0800909 if (boot_cpu_has(X86_FEATURE_ARAT))
910 return 0;
911
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700912 for_each_online_cpu(cpu) {
913 hpet_cpuhp_notify(NULL, CPU_ONLINE, (void *)(long)cpu);
914 }
915
916 /* This notifier should be called after workqueue is ready */
917 hotcpu_notifier(hpet_cpuhp_notify, -20);
918
Thomas Gleixner28769142007-10-12 23:04:06 +0200919 return 0;
920}
921fs_initcall(hpet_late_init);
922
OGAWA Hirofumic86c7fb2007-12-03 17:17:10 +0100923void hpet_disable(void)
924{
Stefano Stabelliniff487802010-07-21 18:32:37 +0100925 if (is_hpet_capable() && hpet_virt_address) {
Jan Beulich5946fa32009-08-19 08:44:24 +0100926 unsigned int cfg = hpet_readl(HPET_CFG);
OGAWA Hirofumic86c7fb2007-12-03 17:17:10 +0100927
928 if (hpet_legacy_int_enabled) {
929 cfg &= ~HPET_CFG_LEGACY;
930 hpet_legacy_int_enabled = 0;
931 }
932 cfg &= ~HPET_CFG_ENABLE;
933 hpet_writel(cfg, HPET_CFG);
934 }
935}
936
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800937#ifdef CONFIG_HPET_EMULATE_RTC
938
939/* HPET in LegacyReplacement Mode eats up RTC interrupt line. When, HPET
940 * is enabled, we support RTC interrupt functionality in software.
941 * RTC has 3 kinds of interrupts:
942 * 1) Update Interrupt - generate an interrupt, every sec, when RTC clock
943 * is updated
944 * 2) Alarm Interrupt - generate an interrupt at a specific time of day
945 * 3) Periodic Interrupt - generate periodic interrupt, with frequencies
946 * 2Hz-8192Hz (2Hz-64Hz for non-root user) (all freqs in powers of 2)
947 * (1) and (2) above are implemented using polling at a frequency of
948 * 64 Hz. The exact frequency is a tradeoff between accuracy and interrupt
949 * overhead. (DEFAULT_RTC_INT_FREQ)
950 * For (3), we use interrupts at 64Hz or user specified periodic
951 * frequency, whichever is higher.
952 */
953#include <linux/mc146818rtc.h>
954#include <linux/rtc.h>
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +0100955#include <asm/rtc.h>
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800956
957#define DEFAULT_RTC_INT_FREQ 64
958#define DEFAULT_RTC_SHIFT 6
959#define RTC_NUM_INTS 1
960
961static unsigned long hpet_rtc_flags;
David Brownell7e2a31d2008-07-23 21:30:47 -0700962static int hpet_prev_update_sec;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800963static struct rtc_time hpet_alarm_time;
964static unsigned long hpet_pie_count;
Pavel Emelyanovff08f762009-02-04 13:40:31 +0300965static u32 hpet_t1_cmp;
Jan Beulich5946fa32009-08-19 08:44:24 +0100966static u32 hpet_default_delta;
967static u32 hpet_pie_delta;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800968static unsigned long hpet_pie_limit;
969
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +0100970static rtc_irq_handler irq_handler;
971
972/*
Pavel Emelyanovff08f762009-02-04 13:40:31 +0300973 * Check that the hpet counter c1 is ahead of the c2
974 */
975static inline int hpet_cnt_ahead(u32 c1, u32 c2)
976{
977 return (s32)(c2 - c1) < 0;
978}
979
980/*
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +0100981 * Registers a IRQ handler.
982 */
983int hpet_register_irq_handler(rtc_irq_handler handler)
984{
985 if (!is_hpet_enabled())
986 return -ENODEV;
987 if (irq_handler)
988 return -EBUSY;
989
990 irq_handler = handler;
991
992 return 0;
993}
994EXPORT_SYMBOL_GPL(hpet_register_irq_handler);
995
996/*
997 * Deregisters the IRQ handler registered with hpet_register_irq_handler()
998 * and does cleanup.
999 */
1000void hpet_unregister_irq_handler(rtc_irq_handler handler)
1001{
1002 if (!is_hpet_enabled())
1003 return;
1004
1005 irq_handler = NULL;
1006 hpet_rtc_flags = 0;
1007}
1008EXPORT_SYMBOL_GPL(hpet_unregister_irq_handler);
1009
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001010/*
1011 * Timer 1 for RTC emulation. We use one shot mode, as periodic mode
1012 * is not supported by all HPET implementations for timer 1.
1013 *
1014 * hpet_rtc_timer_init() is called when the rtc is initialized.
1015 */
1016int hpet_rtc_timer_init(void)
1017{
Jan Beulich5946fa32009-08-19 08:44:24 +01001018 unsigned int cfg, cnt, delta;
1019 unsigned long flags;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001020
1021 if (!is_hpet_enabled())
1022 return 0;
1023
1024 if (!hpet_default_delta) {
1025 uint64_t clc;
1026
1027 clc = (uint64_t) hpet_clockevent.mult * NSEC_PER_SEC;
1028 clc >>= hpet_clockevent.shift + DEFAULT_RTC_SHIFT;
Jan Beulich5946fa32009-08-19 08:44:24 +01001029 hpet_default_delta = clc;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001030 }
1031
1032 if (!(hpet_rtc_flags & RTC_PIE) || hpet_pie_limit)
1033 delta = hpet_default_delta;
1034 else
1035 delta = hpet_pie_delta;
1036
1037 local_irq_save(flags);
1038
1039 cnt = delta + hpet_readl(HPET_COUNTER);
1040 hpet_writel(cnt, HPET_T1_CMP);
1041 hpet_t1_cmp = cnt;
1042
1043 cfg = hpet_readl(HPET_T1_CFG);
1044 cfg &= ~HPET_TN_PERIODIC;
1045 cfg |= HPET_TN_ENABLE | HPET_TN_32BIT;
1046 hpet_writel(cfg, HPET_T1_CFG);
1047
1048 local_irq_restore(flags);
1049
1050 return 1;
1051}
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +01001052EXPORT_SYMBOL_GPL(hpet_rtc_timer_init);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001053
Mark Langsdorf2ded6e62011-11-18 16:33:06 +01001054static void hpet_disable_rtc_channel(void)
1055{
1056 unsigned long cfg;
1057 cfg = hpet_readl(HPET_T1_CFG);
1058 cfg &= ~HPET_TN_ENABLE;
1059 hpet_writel(cfg, HPET_T1_CFG);
1060}
1061
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001062/*
1063 * The functions below are called from rtc driver.
1064 * Return 0 if HPET is not being used.
1065 * Otherwise do the necessary changes and return 1.
1066 */
1067int hpet_mask_rtc_irq_bit(unsigned long bit_mask)
1068{
1069 if (!is_hpet_enabled())
1070 return 0;
1071
1072 hpet_rtc_flags &= ~bit_mask;
Mark Langsdorf2ded6e62011-11-18 16:33:06 +01001073 if (unlikely(!hpet_rtc_flags))
1074 hpet_disable_rtc_channel();
1075
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001076 return 1;
1077}
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +01001078EXPORT_SYMBOL_GPL(hpet_mask_rtc_irq_bit);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001079
1080int hpet_set_rtc_irq_bit(unsigned long bit_mask)
1081{
1082 unsigned long oldbits = hpet_rtc_flags;
1083
1084 if (!is_hpet_enabled())
1085 return 0;
1086
1087 hpet_rtc_flags |= bit_mask;
1088
David Brownell7e2a31d2008-07-23 21:30:47 -07001089 if ((bit_mask & RTC_UIE) && !(oldbits & RTC_UIE))
1090 hpet_prev_update_sec = -1;
1091
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001092 if (!oldbits)
1093 hpet_rtc_timer_init();
1094
1095 return 1;
1096}
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +01001097EXPORT_SYMBOL_GPL(hpet_set_rtc_irq_bit);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001098
1099int hpet_set_alarm_time(unsigned char hrs, unsigned char min,
1100 unsigned char sec)
1101{
1102 if (!is_hpet_enabled())
1103 return 0;
1104
1105 hpet_alarm_time.tm_hour = hrs;
1106 hpet_alarm_time.tm_min = min;
1107 hpet_alarm_time.tm_sec = sec;
1108
1109 return 1;
1110}
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +01001111EXPORT_SYMBOL_GPL(hpet_set_alarm_time);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001112
1113int hpet_set_periodic_freq(unsigned long freq)
1114{
1115 uint64_t clc;
1116
1117 if (!is_hpet_enabled())
1118 return 0;
1119
1120 if (freq <= DEFAULT_RTC_INT_FREQ)
1121 hpet_pie_limit = DEFAULT_RTC_INT_FREQ / freq;
1122 else {
1123 clc = (uint64_t) hpet_clockevent.mult * NSEC_PER_SEC;
1124 do_div(clc, freq);
1125 clc >>= hpet_clockevent.shift;
Jan Beulich5946fa32009-08-19 08:44:24 +01001126 hpet_pie_delta = clc;
Alok Katariab4a5e8a2010-03-11 14:00:16 -08001127 hpet_pie_limit = 0;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001128 }
1129 return 1;
1130}
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +01001131EXPORT_SYMBOL_GPL(hpet_set_periodic_freq);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001132
1133int hpet_rtc_dropped_irq(void)
1134{
1135 return is_hpet_enabled();
1136}
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +01001137EXPORT_SYMBOL_GPL(hpet_rtc_dropped_irq);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001138
1139static void hpet_rtc_timer_reinit(void)
1140{
Mark Langsdorf2ded6e62011-11-18 16:33:06 +01001141 unsigned int delta;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001142 int lost_ints = -1;
1143
Mark Langsdorf2ded6e62011-11-18 16:33:06 +01001144 if (unlikely(!hpet_rtc_flags))
1145 hpet_disable_rtc_channel();
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001146
1147 if (!(hpet_rtc_flags & RTC_PIE) || hpet_pie_limit)
1148 delta = hpet_default_delta;
1149 else
1150 delta = hpet_pie_delta;
1151
1152 /*
1153 * Increment the comparator value until we are ahead of the
1154 * current count.
1155 */
1156 do {
1157 hpet_t1_cmp += delta;
1158 hpet_writel(hpet_t1_cmp, HPET_T1_CMP);
1159 lost_ints++;
Pavel Emelyanovff08f762009-02-04 13:40:31 +03001160 } while (!hpet_cnt_ahead(hpet_t1_cmp, hpet_readl(HPET_COUNTER)));
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001161
1162 if (lost_ints) {
1163 if (hpet_rtc_flags & RTC_PIE)
1164 hpet_pie_count += lost_ints;
1165 if (printk_ratelimit())
David Brownell7e2a31d2008-07-23 21:30:47 -07001166 printk(KERN_WARNING "hpet1: lost %d rtc interrupts\n",
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001167 lost_ints);
1168 }
1169}
1170
1171irqreturn_t hpet_rtc_interrupt(int irq, void *dev_id)
1172{
1173 struct rtc_time curr_time;
1174 unsigned long rtc_int_flag = 0;
1175
1176 hpet_rtc_timer_reinit();
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +01001177 memset(&curr_time, 0, sizeof(struct rtc_time));
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001178
1179 if (hpet_rtc_flags & (RTC_UIE | RTC_AIE))
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +01001180 get_rtc_time(&curr_time);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001181
1182 if (hpet_rtc_flags & RTC_UIE &&
1183 curr_time.tm_sec != hpet_prev_update_sec) {
David Brownell7e2a31d2008-07-23 21:30:47 -07001184 if (hpet_prev_update_sec >= 0)
1185 rtc_int_flag = RTC_UF;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001186 hpet_prev_update_sec = curr_time.tm_sec;
1187 }
1188
1189 if (hpet_rtc_flags & RTC_PIE &&
1190 ++hpet_pie_count >= hpet_pie_limit) {
1191 rtc_int_flag |= RTC_PF;
1192 hpet_pie_count = 0;
1193 }
1194
Bernhard Walle8ee291f2008-01-15 16:44:38 +01001195 if (hpet_rtc_flags & RTC_AIE &&
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001196 (curr_time.tm_sec == hpet_alarm_time.tm_sec) &&
1197 (curr_time.tm_min == hpet_alarm_time.tm_min) &&
1198 (curr_time.tm_hour == hpet_alarm_time.tm_hour))
1199 rtc_int_flag |= RTC_AF;
1200
1201 if (rtc_int_flag) {
1202 rtc_int_flag |= (RTC_IRQF | (RTC_NUM_INTS << 8));
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +01001203 if (irq_handler)
1204 irq_handler(rtc_int_flag, dev_id);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001205 }
1206 return IRQ_HANDLED;
1207}
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +01001208EXPORT_SYMBOL_GPL(hpet_rtc_interrupt);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001209#endif