Skip to content

Commit 6bb74df

Browse files
john stultzLinus Torvalds
authored andcommitted
[PATCH] clocksource init adjustments (fix bug raspberrypi#7426)
This patch resolves the issue found here: http://bugme.osdl.org/show_bug.cgi?id=7426 The basic summary is: Currently we register most of i386/x86_64 clocksources at module_init time. Then we enable clocksource selection at late_initcall time. This causes some problems for drivers that use gettimeofday for init calibration routines (specifically the es1968 driver in this case), where durring module_init, the only clocksource available is the low-res jiffies clocksource. This may cause slight calibration errors, due to the small sampling time used. It should be noted that drivers that require fine grained time may not function on architectures that do not have better then jiffies resolution timekeeping (there are a few). However, this does not discount the reasonable need for such fine-grained timekeeping at init time. Thus the solution here is to register clocksources earlier (ideally when the hardware is being initialized), and then we enable clocksource selection at fs_initcall (before device_initcall). This patch should probably get some testing time in -mm, since clocksource selection is one of the most important issues for correct timekeeping, and I've only been able to test this on a few of my own boxes. Signed-off-by: John Stultz <johnstul@us.ibm.com> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: Ingo Molnar <mingo@elte.hu> Cc: "David S. Miller" <davem@davemloft.net> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 4540768 commit 6bb74df

12 files changed

Lines changed: 135 additions & 166 deletions

File tree

‎arch/i386/kernel/hpet.c‎

Lines changed: 37 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -200,13 +200,31 @@ static int hpet_next_event(unsigned long delta,
200200
return ((long)(hpet_readl(HPET_COUNTER) - cnt ) > 0);
201201
}
202202

203+
/*
204+
* Clock source related code
205+
*/
206+
static cycle_t read_hpet(void)
207+
{
208+
return (cycle_t)hpet_readl(HPET_COUNTER);
209+
}
210+
211+
static struct clocksource clocksource_hpet = {
212+
.name = "hpet",
213+
.rating = 250,
214+
.read = read_hpet,
215+
.mask = HPET_MASK,
216+
.shift = HPET_SHIFT,
217+
.flags = CLOCK_SOURCE_IS_CONTINUOUS,
218+
};
219+
203220
/*
204221
* Try to setup the HPET timer
205222
*/
206223
int __init hpet_enable(void)
207224
{
208225
unsigned long id;
209226
uint64_t hpet_freq;
227+
u64 tmp;
210228

211229
if (!is_hpet_capable())
212230
return 0;
@@ -253,6 +271,25 @@ int __init hpet_enable(void)
253271
/* Start the counter */
254272
hpet_start_counter();
255273

274+
/* Initialize and register HPET clocksource
275+
*
276+
* hpet period is in femto seconds per cycle
277+
* so we need to convert this to ns/cyc units
278+
* aproximated by mult/2^shift
279+
*
280+
* fsec/cyc * 1nsec/1000000fsec = nsec/cyc = mult/2^shift
281+
* fsec/cyc * 1ns/1000000fsec * 2^shift = mult
282+
* fsec/cyc * 2^shift * 1nsec/1000000fsec = mult
283+
* (fsec/cyc << shift)/1000000 = mult
284+
* (hpet_period << shift)/FSEC_PER_NSEC = mult
285+
*/
286+
tmp = (u64)hpet_period << HPET_SHIFT;
287+
do_div(tmp, FSEC_PER_NSEC);
288+
clocksource_hpet.mult = (u32)tmp;
289+
290+
clocksource_register(&clocksource_hpet);
291+
292+
256293
if (id & HPET_ID_LEGSUP) {
257294
hpet_enable_int();
258295
hpet_reserve_platform_timers(id);
@@ -273,49 +310,6 @@ int __init hpet_enable(void)
273310
return 0;
274311
}
275312

276-
/*
277-
* Clock source related code
278-
*/
279-
static cycle_t read_hpet(void)
280-
{
281-
return (cycle_t)hpet_readl(HPET_COUNTER);
282-
}
283-
284-
static struct clocksource clocksource_hpet = {
285-
.name = "hpet",
286-
.rating = 250,
287-
.read = read_hpet,
288-
.mask = HPET_MASK,
289-
.shift = HPET_SHIFT,
290-
.flags = CLOCK_SOURCE_IS_CONTINUOUS,
291-
};
292-
293-
static int __init init_hpet_clocksource(void)
294-
{
295-
u64 tmp;
296-
297-
if (!hpet_virt_address)
298-
return -ENODEV;
299-
300-
/*
301-
* hpet period is in femto seconds per cycle
302-
* so we need to convert this to ns/cyc units
303-
* aproximated by mult/2^shift
304-
*
305-
* fsec/cyc * 1nsec/1000000fsec = nsec/cyc = mult/2^shift
306-
* fsec/cyc * 1ns/1000000fsec * 2^shift = mult
307-
* fsec/cyc * 2^shift * 1nsec/1000000fsec = mult
308-
* (fsec/cyc << shift)/1000000 = mult
309-
* (hpet_period << shift)/FSEC_PER_NSEC = mult
310-
*/
311-
tmp = (u64)hpet_period << HPET_SHIFT;
312-
do_div(tmp, FSEC_PER_NSEC);
313-
clocksource_hpet.mult = (u32)tmp;
314-
315-
return clocksource_register(&clocksource_hpet);
316-
}
317-
318-
module_init(init_hpet_clocksource);
319313

320314
#ifdef CONFIG_HPET_EMULATE_RTC
321315

‎arch/i386/kernel/i8253.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,4 +195,4 @@ static int __init init_pit_clocksource(void)
195195
clocksource_pit.mult = clocksource_hz2mult(CLOCK_TICK_RATE, 20);
196196
return clocksource_register(&clocksource_pit);
197197
}
198-
module_init(init_pit_clocksource);
198+
arch_initcall(init_pit_clocksource);

‎arch/i386/kernel/setup.c‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -657,5 +657,4 @@ void __init setup_arch(char **cmdline_p)
657657
conswitchp = &dummy_con;
658658
#endif
659659
#endif
660-
tsc_init();
661660
}

‎arch/i386/kernel/time.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,5 +279,6 @@ void __init hpet_time_init(void)
279279
*/
280280
void __init time_init(void)
281281
{
282+
tsc_init();
282283
late_time_init = choose_time_init();
283284
}

‎arch/i386/kernel/tsc.c‎

Lines changed: 37 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -184,34 +184,6 @@ int recalibrate_cpu_khz(void)
184184

185185
EXPORT_SYMBOL(recalibrate_cpu_khz);
186186

187-
void __init tsc_init(void)
188-
{
189-
if (!cpu_has_tsc || tsc_disable)
190-
goto out_no_tsc;
191-
192-
cpu_khz = calculate_cpu_khz();
193-
tsc_khz = cpu_khz;
194-
195-
if (!cpu_khz)
196-
goto out_no_tsc;
197-
198-
printk("Detected %lu.%03lu MHz processor.\n",
199-
(unsigned long)cpu_khz / 1000,
200-
(unsigned long)cpu_khz % 1000);
201-
202-
set_cyc2ns_scale(cpu_khz);
203-
use_tsc_delay();
204-
return;
205-
206-
out_no_tsc:
207-
/*
208-
* Set the tsc_disable flag if there's no TSC support, this
209-
* makes it a fast flag for the kernel to see whether it
210-
* should be using the TSC.
211-
*/
212-
tsc_disable = 1;
213-
}
214-
215187
#ifdef CONFIG_CPU_FREQ
216188

217189
/*
@@ -381,28 +353,47 @@ static void __init check_geode_tsc_reliable(void)
381353
static inline void check_geode_tsc_reliable(void) { }
382354
#endif
383355

384-
static int __init init_tsc_clocksource(void)
356+
357+
void __init tsc_init(void)
385358
{
359+
if (!cpu_has_tsc || tsc_disable)
360+
goto out_no_tsc;
386361

387-
if (cpu_has_tsc && tsc_khz && !tsc_disable) {
388-
/* check blacklist */
389-
dmi_check_system(bad_tsc_dmi_table);
362+
cpu_khz = calculate_cpu_khz();
363+
tsc_khz = cpu_khz;
390364

391-
unsynchronized_tsc();
392-
check_geode_tsc_reliable();
393-
current_tsc_khz = tsc_khz;
394-
clocksource_tsc.mult = clocksource_khz2mult(current_tsc_khz,
395-
clocksource_tsc.shift);
396-
/* lower the rating if we already know its unstable: */
397-
if (check_tsc_unstable()) {
398-
clocksource_tsc.rating = 0;
399-
clocksource_tsc.flags &= ~CLOCK_SOURCE_IS_CONTINUOUS;
400-
}
365+
if (!cpu_khz)
366+
goto out_no_tsc;
401367

402-
return clocksource_register(&clocksource_tsc);
368+
printk("Detected %lu.%03lu MHz processor.\n",
369+
(unsigned long)cpu_khz / 1000,
370+
(unsigned long)cpu_khz % 1000);
371+
372+
set_cyc2ns_scale(cpu_khz);
373+
use_tsc_delay();
374+
375+
/* Check and install the TSC clocksource */
376+
dmi_check_system(bad_tsc_dmi_table);
377+
378+
unsynchronized_tsc();
379+
check_geode_tsc_reliable();
380+
current_tsc_khz = tsc_khz;
381+
clocksource_tsc.mult = clocksource_khz2mult(current_tsc_khz,
382+
clocksource_tsc.shift);
383+
/* lower the rating if we already know its unstable: */
384+
if (check_tsc_unstable()) {
385+
clocksource_tsc.rating = 0;
386+
clocksource_tsc.flags &= ~CLOCK_SOURCE_IS_CONTINUOUS;
403387
}
388+
clocksource_register(&clocksource_tsc);
404389

405-
return 0;
406-
}
390+
return;
407391

408-
module_init(init_tsc_clocksource);
392+
out_no_tsc:
393+
/*
394+
* Set the tsc_disable flag if there's no TSC support, this
395+
* makes it a fast flag for the kernel to see whether it
396+
* should be using the TSC.
397+
*/
398+
tsc_disable = 1;
399+
}

‎arch/x86_64/kernel/hpet.c‎

Lines changed: 44 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212
#include <asm/timex.h>
1313
#include <asm/hpet.h>
1414

15+
#define HPET_MASK 0xFFFFFFFF
16+
#define HPET_SHIFT 22
17+
18+
/* FSEC = 10^-15 NSEC = 10^-9 */
19+
#define FSEC_PER_NSEC 1000000
20+
1521
int nohpet __initdata;
1622

1723
unsigned long hpet_address;
@@ -106,9 +112,31 @@ int hpet_timer_stop_set_go(unsigned long tick)
106112
return 0;
107113
}
108114

115+
static cycle_t read_hpet(void)
116+
{
117+
return (cycle_t)hpet_readl(HPET_COUNTER);
118+
}
119+
120+
static cycle_t __vsyscall_fn vread_hpet(void)
121+
{
122+
return readl((void __iomem *)fix_to_virt(VSYSCALL_HPET) + 0xf0);
123+
}
124+
125+
struct clocksource clocksource_hpet = {
126+
.name = "hpet",
127+
.rating = 250,
128+
.read = read_hpet,
129+
.mask = (cycle_t)HPET_MASK,
130+
.mult = 0, /* set below */
131+
.shift = HPET_SHIFT,
132+
.flags = CLOCK_SOURCE_IS_CONTINUOUS,
133+
.vread = vread_hpet,
134+
};
135+
109136
int hpet_arch_init(void)
110137
{
111138
unsigned int id;
139+
u64 tmp;
112140

113141
if (!hpet_address)
114142
return -1;
@@ -132,6 +160,22 @@ int hpet_arch_init(void)
132160

133161
hpet_use_timer = (id & HPET_ID_LEGSUP);
134162

163+
/*
164+
* hpet period is in femto seconds per cycle
165+
* so we need to convert this to ns/cyc units
166+
* aproximated by mult/2^shift
167+
*
168+
* fsec/cyc * 1nsec/1000000fsec = nsec/cyc = mult/2^shift
169+
* fsec/cyc * 1ns/1000000fsec * 2^shift = mult
170+
* fsec/cyc * 2^shift * 1nsec/1000000fsec = mult
171+
* (fsec/cyc << shift)/1000000 = mult
172+
* (hpet_period << shift)/FSEC_PER_NSEC = mult
173+
*/
174+
tmp = (u64)hpet_period << HPET_SHIFT;
175+
do_div(tmp, FSEC_PER_NSEC);
176+
clocksource_hpet.mult = (u32)tmp;
177+
clocksource_register(&clocksource_hpet);
178+
135179
return hpet_timer_stop_set_go(hpet_tick);
136180
}
137181

@@ -444,68 +488,3 @@ static int __init nohpet_setup(char *s)
444488
}
445489

446490
__setup("nohpet", nohpet_setup);
447-
448-
#define HPET_MASK 0xFFFFFFFF
449-
#define HPET_SHIFT 22
450-
451-
/* FSEC = 10^-15 NSEC = 10^-9 */
452-
#define FSEC_PER_NSEC 1000000
453-
454-
static void *hpet_ptr;
455-
456-
static cycle_t read_hpet(void)
457-
{
458-
return (cycle_t)readl(hpet_ptr);
459-
}
460-
461-
static cycle_t __vsyscall_fn vread_hpet(void)
462-
{
463-
return readl((void __iomem *)fix_to_virt(VSYSCALL_HPET) + 0xf0);
464-
}
465-
466-
struct clocksource clocksource_hpet = {
467-
.name = "hpet",
468-
.rating = 250,
469-
.read = read_hpet,
470-
.mask = (cycle_t)HPET_MASK,
471-
.mult = 0, /* set below */
472-
.shift = HPET_SHIFT,
473-
.flags = CLOCK_SOURCE_IS_CONTINUOUS,
474-
.vread = vread_hpet,
475-
};
476-
477-
static int __init init_hpet_clocksource(void)
478-
{
479-
unsigned long hpet_period;
480-
void __iomem *hpet_base;
481-
u64 tmp;
482-
483-
if (!hpet_address)
484-
return -ENODEV;
485-
486-
/* calculate the hpet address: */
487-
hpet_base = ioremap_nocache(hpet_address, HPET_MMAP_SIZE);
488-
hpet_ptr = hpet_base + HPET_COUNTER;
489-
490-
/* calculate the frequency: */
491-
hpet_period = readl(hpet_base + HPET_PERIOD);
492-
493-
/*
494-
* hpet period is in femto seconds per cycle
495-
* so we need to convert this to ns/cyc units
496-
* aproximated by mult/2^shift
497-
*
498-
* fsec/cyc * 1nsec/1000000fsec = nsec/cyc = mult/2^shift
499-
* fsec/cyc * 1ns/1000000fsec * 2^shift = mult
500-
* fsec/cyc * 2^shift * 1nsec/1000000fsec = mult
501-
* (fsec/cyc << shift)/1000000 = mult
502-
* (hpet_period << shift)/FSEC_PER_NSEC = mult
503-
*/
504-
tmp = (u64)hpet_period << HPET_SHIFT;
505-
do_div(tmp, FSEC_PER_NSEC);
506-
clocksource_hpet.mult = (u32)tmp;
507-
508-
return clocksource_register(&clocksource_hpet);
509-
}
510-
511-
module_init(init_hpet_clocksource);

‎arch/x86_64/kernel/time.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,8 @@ void __init time_init(void)
358358
set_cyc2ns_scale(cpu_khz);
359359
printk(KERN_INFO "time.c: Detected %d.%03d MHz processor.\n",
360360
cpu_khz / 1000, cpu_khz % 1000);
361+
init_tsc_clocksource();
362+
361363
setup_irq(0, &irq0);
362364
}
363365

‎arch/x86_64/kernel/tsc.c‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -210,17 +210,14 @@ void mark_tsc_unstable(void)
210210
}
211211
EXPORT_SYMBOL_GPL(mark_tsc_unstable);
212212

213-
static int __init init_tsc_clocksource(void)
213+
void __init init_tsc_clocksource(void)
214214
{
215215
if (!notsc) {
216216
clocksource_tsc.mult = clocksource_khz2mult(cpu_khz,
217217
clocksource_tsc.shift);
218218
if (check_tsc_unstable())
219219
clocksource_tsc.rating = 0;
220220

221-
return clocksource_register(&clocksource_tsc);
221+
clocksource_register(&clocksource_tsc);
222222
}
223-
return 0;
224223
}
225-
226-
module_init(init_tsc_clocksource);

0 commit comments

Comments
 (0)