Merge branch 'mv-merge'
[sfrench/cifs-2.6.git] / arch / i386 / kernel / timers / timer_pm.c
1 /*
2  * (C) Dominik Brodowski <linux@brodo.de> 2003
3  *
4  * Driver to use the Power Management Timer (PMTMR) available in some
5  * southbridges as primary timing source for the Linux kernel.
6  *
7  * Based on parts of linux/drivers/acpi/hardware/hwtimer.c, timer_pit.c,
8  * timer_hpet.c, and on Arjan van de Ven's implementation for 2.4.
9  *
10  * This file is licensed under the GPL v2.
11  */
12
13
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/device.h>
17 #include <linux/init.h>
18 #include <linux/pci.h>
19 #include <asm/types.h>
20 #include <asm/timer.h>
21 #include <asm/smp.h>
22 #include <asm/io.h>
23 #include <asm/arch_hooks.h>
24
25 #include <linux/timex.h>
26 #include "mach_timer.h"
27
28 /* Number of PMTMR ticks expected during calibration run */
29 #define PMTMR_TICKS_PER_SEC 3579545
30 #define PMTMR_EXPECTED_RATE \
31   ((CALIBRATE_LATCH * (PMTMR_TICKS_PER_SEC >> 10)) / (CLOCK_TICK_RATE>>10))
32
33
34 /* The I/O port the PMTMR resides at.
35  * The location is detected during setup_arch(),
36  * in arch/i386/acpi/boot.c */
37 u32 pmtmr_ioport = 0;
38
39
40 /* value of the Power timer at last timer interrupt */
41 static u32 offset_tick;
42 static u32 offset_delay;
43
44 static unsigned long long monotonic_base;
45 static seqlock_t monotonic_lock = SEQLOCK_UNLOCKED;
46
47 #define ACPI_PM_MASK 0xFFFFFF /* limit it to 24 bits */
48
49 static int pmtmr_need_workaround __read_mostly = 1;
50
51 /*helper function to safely read acpi pm timesource*/
52 static inline u32 read_pmtmr(void)
53 {
54         if (pmtmr_need_workaround) {
55                 u32 v1, v2, v3;
56
57                 /* It has been reported that because of various broken
58                  * chipsets (ICH4, PIIX4 and PIIX4E) where the ACPI PM time
59                  * source is not latched, so you must read it multiple
60                  * times to insure a safe value is read.
61                  */
62                 do {
63                         v1 = inl(pmtmr_ioport);
64                         v2 = inl(pmtmr_ioport);
65                         v3 = inl(pmtmr_ioport);
66                 } while ((v1 > v2 && v1 < v3) || (v2 > v3 && v2 < v1)
67                          || (v3 > v1 && v3 < v2));
68
69                 /* mask the output to 24 bits */
70                 return v2 & ACPI_PM_MASK;
71         }
72
73         return inl(pmtmr_ioport) & ACPI_PM_MASK;
74 }
75
76
77 /*
78  * Some boards have the PMTMR running way too fast. We check
79  * the PMTMR rate against PIT channel 2 to catch these cases.
80  */
81 static int verify_pmtmr_rate(void)
82 {
83         u32 value1, value2;
84         unsigned long count, delta;
85
86         mach_prepare_counter();
87         value1 = read_pmtmr();
88         mach_countup(&count);
89         value2 = read_pmtmr();
90         delta = (value2 - value1) & ACPI_PM_MASK;
91
92         /* Check that the PMTMR delta is within 5% of what we expect */
93         if (delta < (PMTMR_EXPECTED_RATE * 19) / 20 ||
94             delta > (PMTMR_EXPECTED_RATE * 21) / 20) {
95                 printk(KERN_INFO "PM-Timer running at invalid rate: %lu%% of normal - aborting.\n", 100UL * delta / PMTMR_EXPECTED_RATE);
96                 return -1;
97         }
98
99         return 0;
100 }
101
102
103 static int init_pmtmr(char* override)
104 {
105         u32 value1, value2;
106         unsigned int i;
107
108         if (override[0] && strncmp(override,"pmtmr",5))
109                 return -ENODEV;
110
111         if (!pmtmr_ioport)
112                 return -ENODEV;
113
114         /* we use the TSC for delay_pmtmr, so make sure it exists */
115         if (!cpu_has_tsc)
116                 return -ENODEV;
117
118         /* "verify" this timing source */
119         value1 = read_pmtmr();
120         for (i = 0; i < 10000; i++) {
121                 value2 = read_pmtmr();
122                 if (value2 == value1)
123                         continue;
124                 if (value2 > value1)
125                         goto pm_good;
126                 if ((value2 < value1) && ((value2) < 0xFFF))
127                         goto pm_good;
128                 printk(KERN_INFO "PM-Timer had inconsistent results: 0x%#x, 0x%#x - aborting.\n", value1, value2);
129                 return -EINVAL;
130         }
131         printk(KERN_INFO "PM-Timer had no reasonable result: 0x%#x - aborting.\n", value1);
132         return -ENODEV;
133
134 pm_good:
135         if (verify_pmtmr_rate() != 0)
136                 return -ENODEV;
137
138         init_cpu_khz();
139         return 0;
140 }
141
142 static inline u32 cyc2us(u32 cycles)
143 {
144         /* The Power Management Timer ticks at 3.579545 ticks per microsecond.
145          * 1 / PM_TIMER_FREQUENCY == 0.27936511 =~ 286/1024 [error: 0.024%]
146          *
147          * Even with HZ = 100, delta is at maximum 35796 ticks, so it can
148          * easily be multiplied with 286 (=0x11E) without having to fear
149          * u32 overflows.
150          */
151         cycles *= 286;
152         return (cycles >> 10);
153 }
154
155 /*
156  * this gets called during each timer interrupt
157  *   - Called while holding the writer xtime_lock
158  */
159 static void mark_offset_pmtmr(void)
160 {
161         u32 lost, delta, last_offset;
162         static int first_run = 1;
163         last_offset = offset_tick;
164
165         write_seqlock(&monotonic_lock);
166
167         offset_tick = read_pmtmr();
168
169         /* calculate tick interval */
170         delta = (offset_tick - last_offset) & ACPI_PM_MASK;
171
172         /* convert to usecs */
173         delta = cyc2us(delta);
174
175         /* update the monotonic base value */
176         monotonic_base += delta * NSEC_PER_USEC;
177         write_sequnlock(&monotonic_lock);
178
179         /* convert to ticks */
180         delta += offset_delay;
181         lost = delta / (USEC_PER_SEC / HZ);
182         offset_delay = delta % (USEC_PER_SEC / HZ);
183
184
185         /* compensate for lost ticks */
186         if (lost >= 2)
187                 jiffies_64 += lost - 1;
188
189         /* don't calculate delay for first run,
190            or if we've got less then a tick */
191         if (first_run || (lost < 1)) {
192                 first_run = 0;
193                 offset_delay = 0;
194         }
195 }
196
197 static int pmtmr_resume(void)
198 {
199         write_seqlock(&monotonic_lock);
200         /* Assume this is the last mark offset time */
201         offset_tick = read_pmtmr();
202         write_sequnlock(&monotonic_lock);
203         return 0;
204 }
205
206 static unsigned long long monotonic_clock_pmtmr(void)
207 {
208         u32 last_offset, this_offset;
209         unsigned long long base, ret;
210         unsigned seq;
211
212
213         /* atomically read monotonic base & last_offset */
214         do {
215                 seq = read_seqbegin(&monotonic_lock);
216                 last_offset = offset_tick;
217                 base = monotonic_base;
218         } while (read_seqretry(&monotonic_lock, seq));
219
220         /* Read the pmtmr */
221         this_offset =  read_pmtmr();
222
223         /* convert to nanoseconds */
224         ret = (this_offset - last_offset) & ACPI_PM_MASK;
225         ret = base + (cyc2us(ret) * NSEC_PER_USEC);
226         return ret;
227 }
228
229 static void delay_pmtmr(unsigned long loops)
230 {
231         unsigned long bclock, now;
232
233         rdtscl(bclock);
234         do
235         {
236                 rep_nop();
237                 rdtscl(now);
238         } while ((now-bclock) < loops);
239 }
240
241
242 /*
243  * get the offset (in microseconds) from the last call to mark_offset()
244  *      - Called holding a reader xtime_lock
245  */
246 static unsigned long get_offset_pmtmr(void)
247 {
248         u32 now, offset, delta = 0;
249
250         offset = offset_tick;
251         now = read_pmtmr();
252         delta = (now - offset)&ACPI_PM_MASK;
253
254         return (unsigned long) offset_delay + cyc2us(delta);
255 }
256
257
258 /* acpi timer_opts struct */
259 static struct timer_opts timer_pmtmr = {
260         .name                   = "pmtmr",
261         .mark_offset            = mark_offset_pmtmr,
262         .get_offset             = get_offset_pmtmr,
263         .monotonic_clock        = monotonic_clock_pmtmr,
264         .delay                  = delay_pmtmr,
265         .read_timer             = read_timer_tsc,
266         .resume                 = pmtmr_resume,
267 };
268
269 struct init_timer_opts __initdata timer_pmtmr_init = {
270         .init = init_pmtmr,
271         .opts = &timer_pmtmr,
272 };
273
274 #ifdef CONFIG_PCI
275 /*
276  * PIIX4 Errata:
277  *
278  * The power management timer may return improper results when read.
279  * Although the timer value settles properly after incrementing,
280  * while incrementing there is a 3 ns window every 69.8 ns where the
281  * timer value is indeterminate (a 4.2% chance that the data will be
282  * incorrect when read). As a result, the ACPI free running count up
283  * timer specification is violated due to erroneous reads.
284  */
285 static int __init pmtmr_bug_check(void)
286 {
287         static struct pci_device_id gray_list[] __initdata = {
288                 /* these chipsets may have bug. */
289                 { PCI_DEVICE(PCI_VENDOR_ID_INTEL,
290                                 PCI_DEVICE_ID_INTEL_82801DB_0) },
291                 { },
292         };
293         struct pci_dev *dev;
294         int pmtmr_has_bug = 0;
295         u8 rev;
296
297         if (cur_timer != &timer_pmtmr || !pmtmr_need_workaround)
298                 return 0;
299
300         dev = pci_get_device(PCI_VENDOR_ID_INTEL,
301                              PCI_DEVICE_ID_INTEL_82371AB_3, NULL);
302         if (dev) {
303                 pci_read_config_byte(dev, PCI_REVISION_ID, &rev);
304                 /* the bug has been fixed in PIIX4M */
305                 if (rev < 3) {
306                         printk(KERN_WARNING "* Found PM-Timer Bug on this "
307                                 "chipset. Due to workarounds for a bug,\n"
308                                 "* this time source is slow.  Consider trying "
309                                 "other time sources (clock=)\n");
310                         pmtmr_has_bug = 1;
311                 }
312                 pci_dev_put(dev);
313         }
314
315         if (pci_dev_present(gray_list)) {
316                 printk(KERN_WARNING "* This chipset may have PM-Timer Bug.  Due"
317                         " to workarounds for a bug,\n"
318                         "* this time source is slow. If you are sure your timer"
319                         " does not have\n"
320                         "* this bug, please use \"pmtmr_good\" to disable the "
321                         "workaround\n");
322                 pmtmr_has_bug = 1;
323         }
324
325         if (!pmtmr_has_bug)
326                 pmtmr_need_workaround = 0;
327
328         return 0;
329 }
330 device_initcall(pmtmr_bug_check);
331 #endif
332
333 static int __init pmtr_good_setup(char *__str)
334 {
335         pmtmr_need_workaround = 0;
336         return 1;
337 }
338 __setup("pmtmr_good", pmtr_good_setup);
339
340 MODULE_LICENSE("GPL");
341 MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>");
342 MODULE_DESCRIPTION("Power Management Timer (PMTMR) as primary timing source for x86");