blob: d10cc009845fa8bcbd1c4a7e7190c9c4c552dafd [file] [log] [blame]
john stultz5d0cf412006-06-26 00:25:12 -07001#include <linux/clocksource.h>
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08002#include <linux/clockchips.h>
Ingo Molnar4588c1f2008-09-06 14:19:17 +02003#include <linux/interrupt.h>
4#include <linux/sysdev.h>
Thomas Gleixner28769142007-10-12 23:04:06 +02005#include <linux/delay.h>
john stultz5d0cf412006-06-26 00:25:12 -07006#include <linux/errno.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09007#include <linux/slab.h>
john stultz5d0cf412006-06-26 00:25:12 -07008#include <linux/hpet.h>
9#include <linux/init.h>
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -070010#include <linux/cpu.h>
Ingo Molnar4588c1f2008-09-06 14:19:17 +020011#include <linux/pm.h>
12#include <linux/io.h>
john stultz5d0cf412006-06-26 00:25:12 -070013
Thomas Gleixner28769142007-10-12 23:04:06 +020014#include <asm/fixmap.h>
Thomas Gleixner06a24de2007-10-12 23:04:06 +020015#include <asm/i8253.h>
Ingo Molnar4588c1f2008-09-06 14:19:17 +020016#include <asm/hpet.h>
john stultz5d0cf412006-06-26 00:25:12 -070017
Ingo Molnar4588c1f2008-09-06 14:19:17 +020018#define HPET_MASK CLOCKSOURCE_MASK(32)
john stultz5d0cf412006-06-26 00:25:12 -070019
Pavel Machekb10db7f2008-01-30 13:30:00 +010020/* FSEC = 10^-15
21 NSEC = 10^-9 */
Ingo Molnar4588c1f2008-09-06 14:19:17 +020022#define FSEC_PER_NSEC 1000000L
john stultz5d0cf412006-06-26 00:25:12 -070023
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -070024#define HPET_DEV_USED_BIT 2
25#define HPET_DEV_USED (1 << HPET_DEV_USED_BIT)
26#define HPET_DEV_VALID 0x8
27#define HPET_DEV_FSB_CAP 0x1000
28#define HPET_DEV_PERI_CAP 0x2000
29
Thomas Gleixnerf1c18072010-12-13 12:43:23 +010030#define HPET_MIN_CYCLES 128
31#define HPET_MIN_PROG_DELTA (HPET_MIN_CYCLES + (HPET_MIN_CYCLES >> 1))
32
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -070033#define EVT_TO_HPET_DEV(evt) container_of(evt, struct hpet_dev, evt)
34
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080035/*
36 * HPET address is set in acpi/boot.c, when an ACPI entry exists
37 */
Ingo Molnar4588c1f2008-09-06 14:19:17 +020038unsigned long hpet_address;
Suresh Siddhac8bc6f32009-08-04 12:07:09 -070039u8 hpet_blockid; /* OS timer block num */
Pallipadi, Venkatesh73472a42010-01-21 11:09:52 -080040u8 hpet_msi_disable;
41
Ingo Molnare951e4a2008-11-25 08:42:01 +010042#ifdef CONFIG_PCI_MSI
Hannes Eder3b71e9e2008-11-23 20:19:33 +010043static unsigned long hpet_num_timers;
Ingo Molnare951e4a2008-11-25 08:42:01 +010044#endif
Ingo Molnar4588c1f2008-09-06 14:19:17 +020045static void __iomem *hpet_virt_address;
john stultz5d0cf412006-06-26 00:25:12 -070046
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -070047struct hpet_dev {
Ingo Molnar4588c1f2008-09-06 14:19:17 +020048 struct clock_event_device evt;
49 unsigned int num;
50 int cpu;
51 unsigned int irq;
52 unsigned int flags;
53 char name[10];
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -070054};
55
Jan Beulich5946fa32009-08-19 08:44:24 +010056inline unsigned int hpet_readl(unsigned int a)
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080057{
58 return readl(hpet_virt_address + a);
59}
60
Jan Beulich5946fa32009-08-19 08:44:24 +010061static inline void hpet_writel(unsigned int d, unsigned int a)
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080062{
63 writel(d, hpet_virt_address + a);
64}
65
Thomas Gleixner28769142007-10-12 23:04:06 +020066#ifdef CONFIG_X86_64
Thomas Gleixner28769142007-10-12 23:04:06 +020067#include <asm/pgtable.h>
Yinghai Lu2387ce52008-07-13 14:50:56 -070068#endif
Thomas Gleixner28769142007-10-12 23:04:06 +020069
Thomas Gleixner06a24de2007-10-12 23:04:06 +020070static inline void hpet_set_mapping(void)
71{
72 hpet_virt_address = ioremap_nocache(hpet_address, HPET_MMAP_SIZE);
Yinghai Lu2387ce52008-07-13 14:50:56 -070073#ifdef CONFIG_X86_64
Andy Lutomirskid319bb72011-06-05 13:50:21 -040074 __set_fixmap(VSYSCALL_HPET, hpet_address, PAGE_KERNEL_VVAR_NOCACHE);
Yinghai Lu2387ce52008-07-13 14:50:56 -070075#endif
Thomas Gleixner06a24de2007-10-12 23:04:06 +020076}
77
78static inline void hpet_clear_mapping(void)
79{
80 iounmap(hpet_virt_address);
81 hpet_virt_address = NULL;
82}
83
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080084/*
85 * HPET command line enable / disable
86 */
87static int boot_hpet_disable;
Thomas Gleixnerb17530b2007-10-19 20:35:02 +020088int hpet_force_user;
Andreas Herrmannb98103a2009-02-21 00:09:47 +010089static int hpet_verbose;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080090
Ingo Molnar4588c1f2008-09-06 14:19:17 +020091static int __init hpet_setup(char *str)
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080092{
93 if (str) {
94 if (!strncmp("disable", str, 7))
95 boot_hpet_disable = 1;
Thomas Gleixnerb17530b2007-10-19 20:35:02 +020096 if (!strncmp("force", str, 5))
97 hpet_force_user = 1;
Andreas Herrmannb98103a2009-02-21 00:09:47 +010098 if (!strncmp("verbose", str, 7))
99 hpet_verbose = 1;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800100 }
101 return 1;
102}
103__setup("hpet=", hpet_setup);
104
Thomas Gleixner28769142007-10-12 23:04:06 +0200105static int __init disable_hpet(char *str)
106{
107 boot_hpet_disable = 1;
108 return 1;
109}
110__setup("nohpet", disable_hpet);
111
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800112static inline int is_hpet_capable(void)
113{
Ingo Molnar4588c1f2008-09-06 14:19:17 +0200114 return !boot_hpet_disable && hpet_address;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800115}
116
117/*
118 * HPET timer interrupt enable / disable
119 */
120static int hpet_legacy_int_enabled;
121
122/**
123 * is_hpet_enabled - check whether the hpet timer interrupt is enabled
124 */
125int is_hpet_enabled(void)
126{
127 return is_hpet_capable() && hpet_legacy_int_enabled;
128}
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +0100129EXPORT_SYMBOL_GPL(is_hpet_enabled);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800130
Andreas Herrmannb98103a2009-02-21 00:09:47 +0100131static void _hpet_print_config(const char *function, int line)
132{
133 u32 i, timers, l, h;
134 printk(KERN_INFO "hpet: %s(%d):\n", function, line);
135 l = hpet_readl(HPET_ID);
136 h = hpet_readl(HPET_PERIOD);
137 timers = ((l & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT) + 1;
138 printk(KERN_INFO "hpet: ID: 0x%x, PERIOD: 0x%x\n", l, h);
139 l = hpet_readl(HPET_CFG);
140 h = hpet_readl(HPET_STATUS);
141 printk(KERN_INFO "hpet: CFG: 0x%x, STATUS: 0x%x\n", l, h);
142 l = hpet_readl(HPET_COUNTER);
143 h = hpet_readl(HPET_COUNTER+4);
144 printk(KERN_INFO "hpet: COUNTER_l: 0x%x, COUNTER_h: 0x%x\n", l, h);
145
146 for (i = 0; i < timers; i++) {
147 l = hpet_readl(HPET_Tn_CFG(i));
148 h = hpet_readl(HPET_Tn_CFG(i)+4);
149 printk(KERN_INFO "hpet: T%d: CFG_l: 0x%x, CFG_h: 0x%x\n",
150 i, l, h);
151 l = hpet_readl(HPET_Tn_CMP(i));
152 h = hpet_readl(HPET_Tn_CMP(i)+4);
153 printk(KERN_INFO "hpet: T%d: CMP_l: 0x%x, CMP_h: 0x%x\n",
154 i, l, h);
155 l = hpet_readl(HPET_Tn_ROUTE(i));
156 h = hpet_readl(HPET_Tn_ROUTE(i)+4);
157 printk(KERN_INFO "hpet: T%d ROUTE_l: 0x%x, ROUTE_h: 0x%x\n",
158 i, l, h);
159 }
160}
161
162#define hpet_print_config() \
163do { \
164 if (hpet_verbose) \
165 _hpet_print_config(__FUNCTION__, __LINE__); \
166} while (0)
167
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800168/*
169 * When the hpet driver (/dev/hpet) is enabled, we need to reserve
170 * timer 0 and timer 1 in case of RTC emulation.
171 */
172#ifdef CONFIG_HPET
Venki Pallipadif0ed4e62008-09-08 10:18:40 -0700173
Venki Pallipadi5f79f2f2008-09-24 10:03:17 -0700174static void hpet_reserve_msi_timers(struct hpet_data *hd);
Venki Pallipadif0ed4e62008-09-08 10:18:40 -0700175
Jan Beulich5946fa32009-08-19 08:44:24 +0100176static void hpet_reserve_platform_timers(unsigned int id)
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800177{
178 struct hpet __iomem *hpet = hpet_virt_address;
Balaji Rao37a47db82008-01-30 13:30:03 +0100179 struct hpet_timer __iomem *timer = &hpet->hpet_timers[2];
180 unsigned int nrtimers, i;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800181 struct hpet_data hd;
182
183 nrtimers = ((id & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT) + 1;
184
Ingo Molnar4588c1f2008-09-06 14:19:17 +0200185 memset(&hd, 0, sizeof(hd));
186 hd.hd_phys_address = hpet_address;
187 hd.hd_address = hpet;
188 hd.hd_nirqs = nrtimers;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800189 hpet_reserve_timer(&hd, 0);
190
191#ifdef CONFIG_HPET_EMULATE_RTC
192 hpet_reserve_timer(&hd, 1);
193#endif
Thomas Gleixner5761d642008-04-04 16:26:10 +0200194
David Brownell64a76f62008-07-29 12:47:38 -0700195 /*
196 * NOTE that hd_irq[] reflects IOAPIC input pins (LEGACY_8254
197 * is wrong for i8259!) not the output IRQ. Many BIOS writers
198 * don't bother configuring *any* comparator interrupts.
199 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800200 hd.hd_irq[0] = HPET_LEGACY_8254;
201 hd.hd_irq[1] = HPET_LEGACY_RTC;
202
Ingo Molnarfc3fbc42008-04-27 14:04:14 +0200203 for (i = 2; i < nrtimers; timer++, i++) {
Ingo Molnar4588c1f2008-09-06 14:19:17 +0200204 hd.hd_irq[i] = (readl(&timer->hpet_config) &
205 Tn_INT_ROUTE_CNF_MASK) >> Tn_INT_ROUTE_CNF_SHIFT;
Ingo Molnarfc3fbc42008-04-27 14:04:14 +0200206 }
Thomas Gleixner5761d642008-04-04 16:26:10 +0200207
Venki Pallipadif0ed4e62008-09-08 10:18:40 -0700208 hpet_reserve_msi_timers(&hd);
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700209
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800210 hpet_alloc(&hd);
Thomas Gleixner5761d642008-04-04 16:26:10 +0200211
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800212}
213#else
Jan Beulich5946fa32009-08-19 08:44:24 +0100214static void hpet_reserve_platform_timers(unsigned int id) { }
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800215#endif
216
217/*
218 * Common hpet info
219 */
Thomas Gleixnerab0e08f2011-05-18 21:33:43 +0000220static unsigned long hpet_freq;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800221
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200222static void hpet_legacy_set_mode(enum clock_event_mode mode,
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800223 struct clock_event_device *evt);
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200224static int hpet_legacy_next_event(unsigned long delta,
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800225 struct clock_event_device *evt);
226
227/*
228 * The hpet clock event device
229 */
230static struct clock_event_device hpet_clockevent = {
231 .name = "hpet",
232 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200233 .set_mode = hpet_legacy_set_mode,
234 .set_next_event = hpet_legacy_next_event,
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800235 .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 /*
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200292 * Start hpet with the boot cpu mask and make it
293 * global after the IO_APIC has been initialized.
294 */
Rusty Russell320ab2b2008-12-13 21:20:26 +1030295 hpet_clockevent.cpumask = cpumask_of(smp_processor_id());
Thomas Gleixnerab0e08f2011-05-18 21:33:43 +0000296 clockevents_config_and_register(&hpet_clockevent, hpet_freq,
297 HPET_MIN_PROG_DELTA, 0x7FFFFFFF);
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200298 global_clock_event = &hpet_clockevent;
299 printk(KERN_DEBUG "hpet clockevent registered\n");
300}
301
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700302static int hpet_setup_msi_irq(unsigned int irq);
303
venkatesh.pallipadi@intel.comb40d5752008-09-05 18:02:16 -0700304static void hpet_set_mode(enum clock_event_mode mode,
305 struct clock_event_device *evt, int timer)
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800306{
Jan Beulich5946fa32009-08-19 08:44:24 +0100307 unsigned int cfg, cmp, now;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800308 uint64_t delta;
309
Ingo Molnar4588c1f2008-09-06 14:19:17 +0200310 switch (mode) {
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800311 case CLOCK_EVT_MODE_PERIODIC:
Andreas Herrmannc23e2532009-02-21 00:16:35 +0100312 hpet_stop_counter();
venkatesh.pallipadi@intel.comb40d5752008-09-05 18:02:16 -0700313 delta = ((uint64_t)(NSEC_PER_SEC/HZ)) * evt->mult;
314 delta >>= evt->shift;
Andreas Herrmann7a6f9cb2009-04-21 20:00:37 +0200315 now = hpet_readl(HPET_COUNTER);
Jan Beulich5946fa32009-08-19 08:44:24 +0100316 cmp = now + (unsigned int) delta;
venkatesh.pallipadi@intel.comb40d5752008-09-05 18:02:16 -0700317 cfg = hpet_readl(HPET_Tn_CFG(timer));
john stultzb13e2462009-02-12 18:48:53 -0800318 /* Make sure we use edge triggered interrupts */
319 cfg &= ~HPET_TN_LEVEL;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800320 cfg |= HPET_TN_ENABLE | HPET_TN_PERIODIC |
321 HPET_TN_SETVAL | HPET_TN_32BIT;
venkatesh.pallipadi@intel.comb40d5752008-09-05 18:02:16 -0700322 hpet_writel(cfg, HPET_Tn_CFG(timer));
Andreas Herrmann7a6f9cb2009-04-21 20:00:37 +0200323 hpet_writel(cmp, HPET_Tn_CMP(timer));
324 udelay(1);
325 /*
326 * HPET on AMD 81xx needs a second write (with HPET_TN_SETVAL
327 * cleared) to T0_CMP to set the period. The HPET_TN_SETVAL
328 * bit is automatically cleared after the first write.
329 * (See AMD-8111 HyperTransport I/O Hub Data Sheet,
330 * Publication # 24674)
331 */
Jan Beulich5946fa32009-08-19 08:44:24 +0100332 hpet_writel((unsigned int) delta, HPET_Tn_CMP(timer));
Andreas Herrmannc23e2532009-02-21 00:16:35 +0100333 hpet_start_counter();
Andreas Herrmannb98103a2009-02-21 00:09:47 +0100334 hpet_print_config();
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800335 break;
336
337 case CLOCK_EVT_MODE_ONESHOT:
venkatesh.pallipadi@intel.comb40d5752008-09-05 18:02:16 -0700338 cfg = hpet_readl(HPET_Tn_CFG(timer));
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800339 cfg &= ~HPET_TN_PERIODIC;
340 cfg |= HPET_TN_ENABLE | HPET_TN_32BIT;
venkatesh.pallipadi@intel.comb40d5752008-09-05 18:02:16 -0700341 hpet_writel(cfg, HPET_Tn_CFG(timer));
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800342 break;
343
344 case CLOCK_EVT_MODE_UNUSED:
345 case CLOCK_EVT_MODE_SHUTDOWN:
venkatesh.pallipadi@intel.comb40d5752008-09-05 18:02:16 -0700346 cfg = hpet_readl(HPET_Tn_CFG(timer));
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800347 cfg &= ~HPET_TN_ENABLE;
venkatesh.pallipadi@intel.comb40d5752008-09-05 18:02:16 -0700348 hpet_writel(cfg, HPET_Tn_CFG(timer));
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800349 break;
Thomas Gleixner18de5bc2007-07-21 04:37:34 -0700350
351 case CLOCK_EVT_MODE_RESUME:
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700352 if (timer == 0) {
353 hpet_enable_legacy_int();
354 } else {
355 struct hpet_dev *hdev = EVT_TO_HPET_DEV(evt);
356 hpet_setup_msi_irq(hdev->irq);
357 disable_irq(hdev->irq);
Rusty Russell0de26522008-12-13 21:20:26 +1030358 irq_set_affinity(hdev->irq, cpumask_of(hdev->cpu));
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700359 enable_irq(hdev->irq);
360 }
Andreas Herrmannb98103a2009-02-21 00:09:47 +0100361 hpet_print_config();
Thomas Gleixner18de5bc2007-07-21 04:37:34 -0700362 break;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800363 }
364}
365
venkatesh.pallipadi@intel.comb40d5752008-09-05 18:02:16 -0700366static int hpet_next_event(unsigned long delta,
367 struct clock_event_device *evt, int timer)
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800368{
Thomas Gleixnerf7676252008-09-06 03:03:32 +0200369 u32 cnt;
Thomas Gleixner995bd3b2010-09-15 15:11:57 +0200370 s32 res;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800371
372 cnt = hpet_readl(HPET_COUNTER);
Thomas Gleixnerf7676252008-09-06 03:03:32 +0200373 cnt += (u32) delta;
venkatesh.pallipadi@intel.comb40d5752008-09-05 18:02:16 -0700374 hpet_writel(cnt, HPET_Tn_CMP(timer));
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800375
Thomas Gleixner72d43d92008-09-06 03:06:08 +0200376 /*
Thomas Gleixner995bd3b2010-09-15 15:11:57 +0200377 * HPETs are a complete disaster. The compare register is
378 * based on a equal comparison and neither provides a less
379 * than or equal functionality (which would require to take
380 * the wraparound into account) nor a simple count down event
381 * mode. Further the write to the comparator register is
382 * delayed internally up to two HPET clock cycles in certain
Thomas Gleixnerf1c18072010-12-13 12:43:23 +0100383 * chipsets (ATI, ICH9,10). Some newer AMD chipsets have even
384 * longer delays. We worked around that by reading back the
385 * compare register, but that required another workaround for
386 * ICH9,10 chips where the first readout after write can
387 * return the old stale value. We already had a minimum
388 * programming delta of 5us enforced, but a NMI or SMI hitting
Thomas Gleixner995bd3b2010-09-15 15:11:57 +0200389 * between the counter readout and the comparator write can
390 * move us behind that point easily. Now instead of reading
391 * the compare register back several times, we make the ETIME
392 * decision based on the following: Return ETIME if the
Thomas Gleixnerf1c18072010-12-13 12:43:23 +0100393 * counter value after the write is less than HPET_MIN_CYCLES
Thomas Gleixner995bd3b2010-09-15 15:11:57 +0200394 * away from the event or if the counter is already ahead of
Thomas Gleixnerf1c18072010-12-13 12:43:23 +0100395 * the event. The minimum programming delta for the generic
396 * clockevents code is set to 1.5 * HPET_MIN_CYCLES.
Thomas Gleixner72d43d92008-09-06 03:06:08 +0200397 */
Thomas Gleixner995bd3b2010-09-15 15:11:57 +0200398 res = (s32)(cnt - hpet_readl(HPET_COUNTER));
Thomas Gleixner72d43d92008-09-06 03:06:08 +0200399
Thomas Gleixnerf1c18072010-12-13 12:43:23 +0100400 return res < HPET_MIN_CYCLES ? -ETIME : 0;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800401}
402
venkatesh.pallipadi@intel.comb40d5752008-09-05 18:02:16 -0700403static void hpet_legacy_set_mode(enum clock_event_mode mode,
404 struct clock_event_device *evt)
405{
406 hpet_set_mode(mode, evt, 0);
407}
408
409static int hpet_legacy_next_event(unsigned long delta,
410 struct clock_event_device *evt)
411{
412 return hpet_next_event(delta, evt, 0);
413}
414
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800415/*
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -0700416 * HPET MSI Support
417 */
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700418#ifdef CONFIG_PCI_MSI
Venki Pallipadi5f79f2f2008-09-24 10:03:17 -0700419
420static DEFINE_PER_CPU(struct hpet_dev *, cpu_hpet_dev);
421static struct hpet_dev *hpet_devs;
422
Thomas Gleixnerd0fbca82010-09-28 16:18:39 +0200423void hpet_msi_unmask(struct irq_data *data)
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -0700424{
Thomas Gleixnerd0fbca82010-09-28 16:18:39 +0200425 struct hpet_dev *hdev = data->handler_data;
Jan Beulich5946fa32009-08-19 08:44:24 +0100426 unsigned int cfg;
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -0700427
428 /* unmask it */
429 cfg = hpet_readl(HPET_Tn_CFG(hdev->num));
430 cfg |= HPET_TN_FSB;
431 hpet_writel(cfg, HPET_Tn_CFG(hdev->num));
432}
433
Thomas Gleixnerd0fbca82010-09-28 16:18:39 +0200434void hpet_msi_mask(struct irq_data *data)
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -0700435{
Thomas Gleixnerd0fbca82010-09-28 16:18:39 +0200436 struct hpet_dev *hdev = data->handler_data;
Jan Beulich5946fa32009-08-19 08:44:24 +0100437 unsigned int cfg;
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -0700438
439 /* mask it */
440 cfg = hpet_readl(HPET_Tn_CFG(hdev->num));
441 cfg &= ~HPET_TN_FSB;
442 hpet_writel(cfg, HPET_Tn_CFG(hdev->num));
443}
444
Thomas Gleixnerd0fbca82010-09-28 16:18:39 +0200445void hpet_msi_write(struct hpet_dev *hdev, struct msi_msg *msg)
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -0700446{
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -0700447 hpet_writel(msg->data, HPET_Tn_ROUTE(hdev->num));
448 hpet_writel(msg->address_lo, HPET_Tn_ROUTE(hdev->num) + 4);
449}
450
Thomas Gleixnerd0fbca82010-09-28 16:18:39 +0200451void hpet_msi_read(struct hpet_dev *hdev, struct msi_msg *msg)
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -0700452{
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -0700453 msg->data = hpet_readl(HPET_Tn_ROUTE(hdev->num));
454 msg->address_lo = hpet_readl(HPET_Tn_ROUTE(hdev->num) + 4);
455 msg->address_hi = 0;
456}
457
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700458static void hpet_msi_set_mode(enum clock_event_mode mode,
459 struct clock_event_device *evt)
460{
461 struct hpet_dev *hdev = EVT_TO_HPET_DEV(evt);
462 hpet_set_mode(mode, evt, hdev->num);
463}
464
465static int hpet_msi_next_event(unsigned long delta,
466 struct clock_event_device *evt)
467{
468 struct hpet_dev *hdev = EVT_TO_HPET_DEV(evt);
469 return hpet_next_event(delta, evt, hdev->num);
470}
471
472static int hpet_setup_msi_irq(unsigned int irq)
473{
Suresh Siddhac8bc6f32009-08-04 12:07:09 -0700474 if (arch_setup_hpet_msi(irq, hpet_blockid)) {
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700475 destroy_irq(irq);
476 return -EINVAL;
477 }
478 return 0;
479}
480
481static int hpet_assign_irq(struct hpet_dev *dev)
482{
483 unsigned int irq;
484
Thomas Gleixner02198962010-09-28 23:20:23 +0200485 irq = create_irq_nr(0, -1);
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700486 if (!irq)
487 return -EINVAL;
488
Thomas Gleixner2c778652011-03-12 12:20:43 +0100489 irq_set_handler_data(irq, dev);
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700490
491 if (hpet_setup_msi_irq(irq))
492 return -EINVAL;
493
494 dev->irq = irq;
495 return 0;
496}
497
498static irqreturn_t hpet_interrupt_handler(int irq, void *data)
499{
500 struct hpet_dev *dev = (struct hpet_dev *)data;
501 struct clock_event_device *hevt = &dev->evt;
502
503 if (!hevt->event_handler) {
504 printk(KERN_INFO "Spurious HPET timer interrupt on HPET timer %d\n",
505 dev->num);
506 return IRQ_HANDLED;
507 }
508
509 hevt->event_handler(hevt);
510 return IRQ_HANDLED;
511}
512
513static int hpet_setup_irq(struct hpet_dev *dev)
514{
515
516 if (request_irq(dev->irq, hpet_interrupt_handler,
Thomas Gleixner507fa3a2009-06-14 17:46:01 +0200517 IRQF_TIMER | IRQF_DISABLED | IRQF_NOBALANCING,
518 dev->name, dev))
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700519 return -1;
520
521 disable_irq(dev->irq);
Rusty Russell0de26522008-12-13 21:20:26 +1030522 irq_set_affinity(dev->irq, cpumask_of(dev->cpu));
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700523 enable_irq(dev->irq);
524
Yinghai Luc81bba42008-09-25 11:53:11 -0700525 printk(KERN_DEBUG "hpet: %s irq %d for MSI\n",
526 dev->name, dev->irq);
527
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700528 return 0;
529}
530
531/* This should be called in specific @cpu */
532static void init_one_hpet_msi_clockevent(struct hpet_dev *hdev, int cpu)
533{
534 struct clock_event_device *evt = &hdev->evt;
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700535
536 WARN_ON(cpu != smp_processor_id());
537 if (!(hdev->flags & HPET_DEV_VALID))
538 return;
539
540 if (hpet_setup_msi_irq(hdev->irq))
541 return;
542
543 hdev->cpu = cpu;
544 per_cpu(cpu_hpet_dev, cpu) = hdev;
545 evt->name = hdev->name;
546 hpet_setup_irq(hdev);
547 evt->irq = hdev->irq;
548
549 evt->rating = 110;
550 evt->features = CLOCK_EVT_FEAT_ONESHOT;
551 if (hdev->flags & HPET_DEV_PERI_CAP)
552 evt->features |= CLOCK_EVT_FEAT_PERIODIC;
553
554 evt->set_mode = hpet_msi_set_mode;
555 evt->set_next_event = hpet_msi_next_event;
Rusty Russell320ab2b2008-12-13 21:20:26 +1030556 evt->cpumask = cpumask_of(hdev->cpu);
Thomas Gleixnerab0e08f2011-05-18 21:33:43 +0000557
558 clockevents_config_and_register(evt, hpet_freq, HPET_MIN_PROG_DELTA,
559 0x7FFFFFFF);
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700560}
561
562#ifdef CONFIG_HPET
563/* Reserve at least one timer for userspace (/dev/hpet) */
564#define RESERVE_TIMERS 1
565#else
566#define RESERVE_TIMERS 0
567#endif
Venki Pallipadi5f79f2f2008-09-24 10:03:17 -0700568
569static void hpet_msi_capability_lookup(unsigned int start_timer)
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700570{
571 unsigned int id;
572 unsigned int num_timers;
573 unsigned int num_timers_used = 0;
574 int i;
575
Pallipadi, Venkatesh73472a42010-01-21 11:09:52 -0800576 if (hpet_msi_disable)
577 return;
578
Shaohua Li39fe05e2009-08-12 11:16:12 +0800579 if (boot_cpu_has(X86_FEATURE_ARAT))
580 return;
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700581 id = hpet_readl(HPET_ID);
582
583 num_timers = ((id & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT);
584 num_timers++; /* Value read out starts from 0 */
Andreas Herrmannb98103a2009-02-21 00:09:47 +0100585 hpet_print_config();
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700586
587 hpet_devs = kzalloc(sizeof(struct hpet_dev) * num_timers, GFP_KERNEL);
588 if (!hpet_devs)
589 return;
590
591 hpet_num_timers = num_timers;
592
593 for (i = start_timer; i < num_timers - RESERVE_TIMERS; i++) {
594 struct hpet_dev *hdev = &hpet_devs[num_timers_used];
Jan Beulich5946fa32009-08-19 08:44:24 +0100595 unsigned int cfg = hpet_readl(HPET_Tn_CFG(i));
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700596
597 /* Only consider HPET timer with MSI support */
598 if (!(cfg & HPET_TN_FSB_CAP))
599 continue;
600
601 hdev->flags = 0;
602 if (cfg & HPET_TN_PERIODIC_CAP)
603 hdev->flags |= HPET_DEV_PERI_CAP;
604 hdev->num = i;
605
606 sprintf(hdev->name, "hpet%d", i);
607 if (hpet_assign_irq(hdev))
608 continue;
609
610 hdev->flags |= HPET_DEV_FSB_CAP;
611 hdev->flags |= HPET_DEV_VALID;
612 num_timers_used++;
613 if (num_timers_used == num_possible_cpus())
614 break;
615 }
616
617 printk(KERN_INFO "HPET: %d timers in total, %d timers will be used for per-cpu timer\n",
618 num_timers, num_timers_used);
619}
620
Venki Pallipadi5f79f2f2008-09-24 10:03:17 -0700621#ifdef CONFIG_HPET
622static void hpet_reserve_msi_timers(struct hpet_data *hd)
623{
624 int i;
625
626 if (!hpet_devs)
627 return;
628
629 for (i = 0; i < hpet_num_timers; i++) {
630 struct hpet_dev *hdev = &hpet_devs[i];
631
632 if (!(hdev->flags & HPET_DEV_VALID))
633 continue;
634
635 hd->hd_irq[hdev->num] = hdev->irq;
636 hpet_reserve_timer(hd, hdev->num);
637 }
638}
639#endif
640
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700641static struct hpet_dev *hpet_get_unused_timer(void)
642{
643 int i;
644
645 if (!hpet_devs)
646 return NULL;
647
648 for (i = 0; i < hpet_num_timers; i++) {
649 struct hpet_dev *hdev = &hpet_devs[i];
650
651 if (!(hdev->flags & HPET_DEV_VALID))
652 continue;
653 if (test_and_set_bit(HPET_DEV_USED_BIT,
654 (unsigned long *)&hdev->flags))
655 continue;
656 return hdev;
657 }
658 return NULL;
659}
660
661struct hpet_work_struct {
662 struct delayed_work work;
663 struct completion complete;
664};
665
666static void hpet_work(struct work_struct *w)
667{
668 struct hpet_dev *hdev;
669 int cpu = smp_processor_id();
670 struct hpet_work_struct *hpet_work;
671
672 hpet_work = container_of(w, struct hpet_work_struct, work.work);
673
674 hdev = hpet_get_unused_timer();
675 if (hdev)
676 init_one_hpet_msi_clockevent(hdev, cpu);
677
678 complete(&hpet_work->complete);
679}
680
681static int hpet_cpuhp_notify(struct notifier_block *n,
682 unsigned long action, void *hcpu)
683{
684 unsigned long cpu = (unsigned long)hcpu;
685 struct hpet_work_struct work;
686 struct hpet_dev *hdev = per_cpu(cpu_hpet_dev, cpu);
687
688 switch (action & 0xf) {
689 case CPU_ONLINE:
Andrew Mortonca1cab32010-10-26 14:22:34 -0700690 INIT_DELAYED_WORK_ONSTACK(&work.work, hpet_work);
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700691 init_completion(&work.complete);
692 /* FIXME: add schedule_work_on() */
693 schedule_delayed_work_on(cpu, &work.work, 0);
694 wait_for_completion(&work.complete);
Thomas Gleixner336f6c32009-01-22 09:50:44 +0100695 destroy_timer_on_stack(&work.work.timer);
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700696 break;
697 case CPU_DEAD:
698 if (hdev) {
699 free_irq(hdev->irq, hdev);
700 hdev->flags &= ~HPET_DEV_USED;
701 per_cpu(cpu_hpet_dev, cpu) = NULL;
702 }
703 break;
704 }
705 return NOTIFY_OK;
706}
707#else
708
Steven Noonanba374c92008-09-08 16:19:09 -0700709static int hpet_setup_msi_irq(unsigned int irq)
710{
711 return 0;
712}
Venki Pallipadi5f79f2f2008-09-24 10:03:17 -0700713static void hpet_msi_capability_lookup(unsigned int start_timer)
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700714{
715 return;
716}
717
Venki Pallipadi5f79f2f2008-09-24 10:03:17 -0700718#ifdef CONFIG_HPET
719static void hpet_reserve_msi_timers(struct hpet_data *hd)
720{
721 return;
722}
723#endif
724
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700725static int hpet_cpuhp_notify(struct notifier_block *n,
726 unsigned long action, void *hcpu)
727{
728 return NOTIFY_OK;
729}
730
731#endif
732
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -0700733/*
john stultz6bb74df2007-03-05 00:30:50 -0800734 * Clock source related code
735 */
Magnus Damm8e196082009-04-21 12:24:00 -0700736static cycle_t read_hpet(struct clocksource *cs)
john stultz6bb74df2007-03-05 00:30:50 -0800737{
738 return (cycle_t)hpet_readl(HPET_COUNTER);
739}
740
741static struct clocksource clocksource_hpet = {
742 .name = "hpet",
743 .rating = 250,
744 .read = read_hpet,
745 .mask = HPET_MASK,
john stultz6bb74df2007-03-05 00:30:50 -0800746 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
Andreas Herrmann8d6f0c82009-02-21 00:10:44 +0100747 .resume = hpet_resume_counter,
Thomas Gleixner28769142007-10-12 23:04:06 +0200748#ifdef CONFIG_X86_64
Andy Lutomirski98d0ac32011-07-14 06:47:22 -0400749 .archdata = { .vclock_mode = VCLOCK_HPET },
Thomas Gleixner28769142007-10-12 23:04:06 +0200750#endif
john stultz6bb74df2007-03-05 00:30:50 -0800751};
752
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200753static int hpet_clocksource_register(void)
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800754{
Carlos R. Mafra6fd592d2008-05-05 20:11:22 -0300755 u64 start, now;
Thomas Gleixner075bcd12007-07-21 17:11:12 +0200756 cycle_t t1;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800757
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800758 /* Start the counter */
Andreas Herrmann8d6f0c82009-02-21 00:10:44 +0100759 hpet_restart_counter();
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800760
Thomas Gleixner075bcd12007-07-21 17:11:12 +0200761 /* Verify whether hpet counter works */
Magnus Damm8e196082009-04-21 12:24:00 -0700762 t1 = hpet_readl(HPET_COUNTER);
Thomas Gleixner075bcd12007-07-21 17:11:12 +0200763 rdtscll(start);
764
765 /*
766 * We don't know the TSC frequency yet, but waiting for
767 * 200000 TSC cycles is safe:
768 * 4 GHz == 50us
769 * 1 GHz == 200us
770 */
771 do {
772 rep_nop();
773 rdtscll(now);
774 } while ((now - start) < 200000UL);
775
Magnus Damm8e196082009-04-21 12:24:00 -0700776 if (t1 == hpet_readl(HPET_COUNTER)) {
Thomas Gleixner075bcd12007-07-21 17:11:12 +0200777 printk(KERN_WARNING
778 "HPET counter not counting. HPET disabled\n");
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200779 return -ENODEV;
Thomas Gleixner075bcd12007-07-21 17:11:12 +0200780 }
781
John Stultzf12a15b2010-07-13 17:56:27 -0700782 clocksource_register_hz(&clocksource_hpet, (u32)hpet_freq);
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200783 return 0;
784}
785
Pavel Machekb02a7f22008-02-05 00:48:13 +0100786/**
787 * hpet_enable - Try to setup the HPET timer. Returns 1 on success.
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200788 */
789int __init hpet_enable(void)
790{
Thomas Gleixnerab0e08f2011-05-18 21:33:43 +0000791 unsigned long hpet_period;
Jan Beulich5946fa32009-08-19 08:44:24 +0100792 unsigned int id;
Thomas Gleixnerab0e08f2011-05-18 21:33:43 +0000793 u64 freq;
Thomas Gleixnera6825f12008-08-14 12:17:06 +0200794 int i;
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200795
796 if (!is_hpet_capable())
797 return 0;
798
799 hpet_set_mapping();
800
801 /*
802 * Read the period and check for a sane value:
803 */
804 hpet_period = hpet_readl(HPET_PERIOD);
Thomas Gleixnera6825f12008-08-14 12:17:06 +0200805
806 /*
807 * AMD SB700 based systems with spread spectrum enabled use a
808 * SMM based HPET emulation to provide proper frequency
809 * setting. The SMM code is initialized with the first HPET
810 * register access and takes some time to complete. During
811 * this time the config register reads 0xffffffff. We check
812 * for max. 1000 loops whether the config register reads a non
813 * 0xffffffff value to make sure that HPET is up and running
814 * before we go further. A counting loop is safe, as the HPET
815 * access takes thousands of CPU cycles. On non SB700 based
816 * machines this check is only done once and has no side
817 * effects.
818 */
819 for (i = 0; hpet_readl(HPET_CFG) == 0xFFFFFFFF; i++) {
820 if (i == 1000) {
821 printk(KERN_WARNING
822 "HPET config register value = 0xFFFFFFFF. "
823 "Disabling HPET\n");
824 goto out_nohpet;
825 }
826 }
827
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200828 if (hpet_period < HPET_MIN_PERIOD || hpet_period > HPET_MAX_PERIOD)
829 goto out_nohpet;
830
831 /*
Thomas Gleixnerab0e08f2011-05-18 21:33:43 +0000832 * The period is a femto seconds value. Convert it to a
833 * frequency.
834 */
835 freq = FSEC_PER_SEC;
836 do_div(freq, hpet_period);
837 hpet_freq = freq;
838
839 /*
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200840 * Read the HPET ID register to retrieve the IRQ routing
841 * information and the number of channels
842 */
843 id = hpet_readl(HPET_ID);
Andreas Herrmannb98103a2009-02-21 00:09:47 +0100844 hpet_print_config();
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200845
846#ifdef CONFIG_HPET_EMULATE_RTC
847 /*
848 * The legacy routing mode needs at least two channels, tick timer
849 * and the rtc emulation channel.
850 */
851 if (!(id & HPET_ID_NUMBER))
852 goto out_nohpet;
853#endif
854
855 if (hpet_clocksource_register())
856 goto out_nohpet;
857
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800858 if (id & HPET_ID_LEGSUP) {
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200859 hpet_legacy_clockevent_register();
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800860 return 1;
861 }
862 return 0;
863
864out_nohpet:
Thomas Gleixner06a24de2007-10-12 23:04:06 +0200865 hpet_clear_mapping();
Janne Kulmalabacbe992008-12-16 13:39:57 +0200866 hpet_address = 0;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800867 return 0;
868}
869
Thomas Gleixner28769142007-10-12 23:04:06 +0200870/*
871 * Needs to be late, as the reserve_timer code calls kalloc !
872 *
873 * Not a problem on i386 as hpet_enable is called from late_time_init,
874 * but on x86_64 it is necessary !
875 */
876static __init int hpet_late_init(void)
877{
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700878 int cpu;
879
Venki Pallipadi59c69f22007-10-12 23:04:23 +0200880 if (boot_hpet_disable)
Thomas Gleixner28769142007-10-12 23:04:06 +0200881 return -ENODEV;
882
Venki Pallipadi59c69f22007-10-12 23:04:23 +0200883 if (!hpet_address) {
884 if (!force_hpet_address)
885 return -ENODEV;
886
887 hpet_address = force_hpet_address;
888 hpet_enable();
Venki Pallipadi59c69f22007-10-12 23:04:23 +0200889 }
890
Jeremy Fitzhardinge39c04b52008-12-16 12:32:23 -0800891 if (!hpet_virt_address)
892 return -ENODEV;
893
Shaohua Li39fe05e2009-08-12 11:16:12 +0800894 if (hpet_readl(HPET_ID) & HPET_ID_LEGSUP)
895 hpet_msi_capability_lookup(2);
896 else
897 hpet_msi_capability_lookup(0);
898
Thomas Gleixner28769142007-10-12 23:04:06 +0200899 hpet_reserve_platform_timers(hpet_readl(HPET_ID));
Andreas Herrmannb98103a2009-02-21 00:09:47 +0100900 hpet_print_config();
Venki Pallipadi59c69f22007-10-12 23:04:23 +0200901
Pallipadi, Venkatesh73472a42010-01-21 11:09:52 -0800902 if (hpet_msi_disable)
903 return 0;
904
Shaohua Li39fe05e2009-08-12 11:16:12 +0800905 if (boot_cpu_has(X86_FEATURE_ARAT))
906 return 0;
907
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700908 for_each_online_cpu(cpu) {
909 hpet_cpuhp_notify(NULL, CPU_ONLINE, (void *)(long)cpu);
910 }
911
912 /* This notifier should be called after workqueue is ready */
913 hotcpu_notifier(hpet_cpuhp_notify, -20);
914
Thomas Gleixner28769142007-10-12 23:04:06 +0200915 return 0;
916}
917fs_initcall(hpet_late_init);
918
OGAWA Hirofumic86c7fb2007-12-03 17:17:10 +0100919void hpet_disable(void)
920{
Stefano Stabelliniff487802010-07-21 18:32:37 +0100921 if (is_hpet_capable() && hpet_virt_address) {
Jan Beulich5946fa32009-08-19 08:44:24 +0100922 unsigned int cfg = hpet_readl(HPET_CFG);
OGAWA Hirofumic86c7fb2007-12-03 17:17:10 +0100923
924 if (hpet_legacy_int_enabled) {
925 cfg &= ~HPET_CFG_LEGACY;
926 hpet_legacy_int_enabled = 0;
927 }
928 cfg &= ~HPET_CFG_ENABLE;
929 hpet_writel(cfg, HPET_CFG);
930 }
931}
932
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800933#ifdef CONFIG_HPET_EMULATE_RTC
934
935/* HPET in LegacyReplacement Mode eats up RTC interrupt line. When, HPET
936 * is enabled, we support RTC interrupt functionality in software.
937 * RTC has 3 kinds of interrupts:
938 * 1) Update Interrupt - generate an interrupt, every sec, when RTC clock
939 * is updated
940 * 2) Alarm Interrupt - generate an interrupt at a specific time of day
941 * 3) Periodic Interrupt - generate periodic interrupt, with frequencies
942 * 2Hz-8192Hz (2Hz-64Hz for non-root user) (all freqs in powers of 2)
943 * (1) and (2) above are implemented using polling at a frequency of
944 * 64 Hz. The exact frequency is a tradeoff between accuracy and interrupt
945 * overhead. (DEFAULT_RTC_INT_FREQ)
946 * For (3), we use interrupts at 64Hz or user specified periodic
947 * frequency, whichever is higher.
948 */
949#include <linux/mc146818rtc.h>
950#include <linux/rtc.h>
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +0100951#include <asm/rtc.h>
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800952
953#define DEFAULT_RTC_INT_FREQ 64
954#define DEFAULT_RTC_SHIFT 6
955#define RTC_NUM_INTS 1
956
957static unsigned long hpet_rtc_flags;
David Brownell7e2a31d2008-07-23 21:30:47 -0700958static int hpet_prev_update_sec;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800959static struct rtc_time hpet_alarm_time;
960static unsigned long hpet_pie_count;
Pavel Emelyanovff08f762009-02-04 13:40:31 +0300961static u32 hpet_t1_cmp;
Jan Beulich5946fa32009-08-19 08:44:24 +0100962static u32 hpet_default_delta;
963static u32 hpet_pie_delta;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800964static unsigned long hpet_pie_limit;
965
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +0100966static rtc_irq_handler irq_handler;
967
968/*
Pavel Emelyanovff08f762009-02-04 13:40:31 +0300969 * Check that the hpet counter c1 is ahead of the c2
970 */
971static inline int hpet_cnt_ahead(u32 c1, u32 c2)
972{
973 return (s32)(c2 - c1) < 0;
974}
975
976/*
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +0100977 * Registers a IRQ handler.
978 */
979int hpet_register_irq_handler(rtc_irq_handler handler)
980{
981 if (!is_hpet_enabled())
982 return -ENODEV;
983 if (irq_handler)
984 return -EBUSY;
985
986 irq_handler = handler;
987
988 return 0;
989}
990EXPORT_SYMBOL_GPL(hpet_register_irq_handler);
991
992/*
993 * Deregisters the IRQ handler registered with hpet_register_irq_handler()
994 * and does cleanup.
995 */
996void hpet_unregister_irq_handler(rtc_irq_handler handler)
997{
998 if (!is_hpet_enabled())
999 return;
1000
1001 irq_handler = NULL;
1002 hpet_rtc_flags = 0;
1003}
1004EXPORT_SYMBOL_GPL(hpet_unregister_irq_handler);
1005
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001006/*
1007 * Timer 1 for RTC emulation. We use one shot mode, as periodic mode
1008 * is not supported by all HPET implementations for timer 1.
1009 *
1010 * hpet_rtc_timer_init() is called when the rtc is initialized.
1011 */
1012int hpet_rtc_timer_init(void)
1013{
Jan Beulich5946fa32009-08-19 08:44:24 +01001014 unsigned int cfg, cnt, delta;
1015 unsigned long flags;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001016
1017 if (!is_hpet_enabled())
1018 return 0;
1019
1020 if (!hpet_default_delta) {
1021 uint64_t clc;
1022
1023 clc = (uint64_t) hpet_clockevent.mult * NSEC_PER_SEC;
1024 clc >>= hpet_clockevent.shift + DEFAULT_RTC_SHIFT;
Jan Beulich5946fa32009-08-19 08:44:24 +01001025 hpet_default_delta = clc;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001026 }
1027
1028 if (!(hpet_rtc_flags & RTC_PIE) || hpet_pie_limit)
1029 delta = hpet_default_delta;
1030 else
1031 delta = hpet_pie_delta;
1032
1033 local_irq_save(flags);
1034
1035 cnt = delta + hpet_readl(HPET_COUNTER);
1036 hpet_writel(cnt, HPET_T1_CMP);
1037 hpet_t1_cmp = cnt;
1038
1039 cfg = hpet_readl(HPET_T1_CFG);
1040 cfg &= ~HPET_TN_PERIODIC;
1041 cfg |= HPET_TN_ENABLE | HPET_TN_32BIT;
1042 hpet_writel(cfg, HPET_T1_CFG);
1043
1044 local_irq_restore(flags);
1045
1046 return 1;
1047}
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +01001048EXPORT_SYMBOL_GPL(hpet_rtc_timer_init);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001049
1050/*
1051 * The functions below are called from rtc driver.
1052 * Return 0 if HPET is not being used.
1053 * Otherwise do the necessary changes and return 1.
1054 */
1055int hpet_mask_rtc_irq_bit(unsigned long bit_mask)
1056{
1057 if (!is_hpet_enabled())
1058 return 0;
1059
1060 hpet_rtc_flags &= ~bit_mask;
1061 return 1;
1062}
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +01001063EXPORT_SYMBOL_GPL(hpet_mask_rtc_irq_bit);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001064
1065int hpet_set_rtc_irq_bit(unsigned long bit_mask)
1066{
1067 unsigned long oldbits = hpet_rtc_flags;
1068
1069 if (!is_hpet_enabled())
1070 return 0;
1071
1072 hpet_rtc_flags |= bit_mask;
1073
David Brownell7e2a31d2008-07-23 21:30:47 -07001074 if ((bit_mask & RTC_UIE) && !(oldbits & RTC_UIE))
1075 hpet_prev_update_sec = -1;
1076
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001077 if (!oldbits)
1078 hpet_rtc_timer_init();
1079
1080 return 1;
1081}
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +01001082EXPORT_SYMBOL_GPL(hpet_set_rtc_irq_bit);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001083
1084int hpet_set_alarm_time(unsigned char hrs, unsigned char min,
1085 unsigned char sec)
1086{
1087 if (!is_hpet_enabled())
1088 return 0;
1089
1090 hpet_alarm_time.tm_hour = hrs;
1091 hpet_alarm_time.tm_min = min;
1092 hpet_alarm_time.tm_sec = sec;
1093
1094 return 1;
1095}
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +01001096EXPORT_SYMBOL_GPL(hpet_set_alarm_time);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001097
1098int hpet_set_periodic_freq(unsigned long freq)
1099{
1100 uint64_t clc;
1101
1102 if (!is_hpet_enabled())
1103 return 0;
1104
1105 if (freq <= DEFAULT_RTC_INT_FREQ)
1106 hpet_pie_limit = DEFAULT_RTC_INT_FREQ / freq;
1107 else {
1108 clc = (uint64_t) hpet_clockevent.mult * NSEC_PER_SEC;
1109 do_div(clc, freq);
1110 clc >>= hpet_clockevent.shift;
Jan Beulich5946fa32009-08-19 08:44:24 +01001111 hpet_pie_delta = clc;
Alok Katariab4a5e8a2010-03-11 14:00:16 -08001112 hpet_pie_limit = 0;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001113 }
1114 return 1;
1115}
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +01001116EXPORT_SYMBOL_GPL(hpet_set_periodic_freq);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001117
1118int hpet_rtc_dropped_irq(void)
1119{
1120 return is_hpet_enabled();
1121}
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +01001122EXPORT_SYMBOL_GPL(hpet_rtc_dropped_irq);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001123
1124static void hpet_rtc_timer_reinit(void)
1125{
Jan Beulich5946fa32009-08-19 08:44:24 +01001126 unsigned int cfg, delta;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001127 int lost_ints = -1;
1128
1129 if (unlikely(!hpet_rtc_flags)) {
1130 cfg = hpet_readl(HPET_T1_CFG);
1131 cfg &= ~HPET_TN_ENABLE;
1132 hpet_writel(cfg, HPET_T1_CFG);
1133 return;
1134 }
1135
1136 if (!(hpet_rtc_flags & RTC_PIE) || hpet_pie_limit)
1137 delta = hpet_default_delta;
1138 else
1139 delta = hpet_pie_delta;
1140
1141 /*
1142 * Increment the comparator value until we are ahead of the
1143 * current count.
1144 */
1145 do {
1146 hpet_t1_cmp += delta;
1147 hpet_writel(hpet_t1_cmp, HPET_T1_CMP);
1148 lost_ints++;
Pavel Emelyanovff08f762009-02-04 13:40:31 +03001149 } while (!hpet_cnt_ahead(hpet_t1_cmp, hpet_readl(HPET_COUNTER)));
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001150
1151 if (lost_ints) {
1152 if (hpet_rtc_flags & RTC_PIE)
1153 hpet_pie_count += lost_ints;
1154 if (printk_ratelimit())
David Brownell7e2a31d2008-07-23 21:30:47 -07001155 printk(KERN_WARNING "hpet1: lost %d rtc interrupts\n",
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001156 lost_ints);
1157 }
1158}
1159
1160irqreturn_t hpet_rtc_interrupt(int irq, void *dev_id)
1161{
1162 struct rtc_time curr_time;
1163 unsigned long rtc_int_flag = 0;
1164
1165 hpet_rtc_timer_reinit();
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +01001166 memset(&curr_time, 0, sizeof(struct rtc_time));
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001167
1168 if (hpet_rtc_flags & (RTC_UIE | RTC_AIE))
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +01001169 get_rtc_time(&curr_time);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001170
1171 if (hpet_rtc_flags & RTC_UIE &&
1172 curr_time.tm_sec != hpet_prev_update_sec) {
David Brownell7e2a31d2008-07-23 21:30:47 -07001173 if (hpet_prev_update_sec >= 0)
1174 rtc_int_flag = RTC_UF;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001175 hpet_prev_update_sec = curr_time.tm_sec;
1176 }
1177
1178 if (hpet_rtc_flags & RTC_PIE &&
1179 ++hpet_pie_count >= hpet_pie_limit) {
1180 rtc_int_flag |= RTC_PF;
1181 hpet_pie_count = 0;
1182 }
1183
Bernhard Walle8ee291f2008-01-15 16:44:38 +01001184 if (hpet_rtc_flags & RTC_AIE &&
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001185 (curr_time.tm_sec == hpet_alarm_time.tm_sec) &&
1186 (curr_time.tm_min == hpet_alarm_time.tm_min) &&
1187 (curr_time.tm_hour == hpet_alarm_time.tm_hour))
1188 rtc_int_flag |= RTC_AF;
1189
1190 if (rtc_int_flag) {
1191 rtc_int_flag |= (RTC_IRQF | (RTC_NUM_INTS << 8));
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +01001192 if (irq_handler)
1193 irq_handler(rtc_int_flag, dev_id);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001194 }
1195 return IRQ_HANDLED;
1196}
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +01001197EXPORT_SYMBOL_GPL(hpet_rtc_interrupt);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001198#endif