@@ -33,6 +33,8 @@ EXPORT_SYMBOL(cpu_khz);
33
33
unsigned int __read_mostly tsc_khz ;
34
34
EXPORT_SYMBOL (tsc_khz );
35
35
36
+ #define KHZ 1000
37
+
36
38
/*
37
39
* TSC can be unstable due to cpufreq or due to unsynced TSCs
38
40
*/
@@ -1335,34 +1337,10 @@ static int __init init_tsc_clocksource(void)
1335
1337
*/
1336
1338
device_initcall (init_tsc_clocksource );
1337
1339
1338
- void __init tsc_early_delay_calibrate (void )
1339
- {
1340
- unsigned long lpj ;
1341
-
1342
- if (!boot_cpu_has (X86_FEATURE_TSC ))
1343
- return ;
1344
-
1345
- cpu_khz = x86_platform .calibrate_cpu ();
1346
- tsc_khz = x86_platform .calibrate_tsc ();
1347
-
1348
- tsc_khz = tsc_khz ? : cpu_khz ;
1349
- if (!tsc_khz )
1350
- return ;
1351
-
1352
- lpj = tsc_khz * 1000 ;
1353
- do_div (lpj , HZ );
1354
- loops_per_jiffy = lpj ;
1355
- }
1356
-
1357
- void __init tsc_init (void )
1340
+ static bool __init determine_cpu_tsc_frequencies (void )
1358
1341
{
1359
- u64 lpj , cyc ;
1360
- int cpu ;
1361
-
1362
- if (!boot_cpu_has (X86_FEATURE_TSC )) {
1363
- setup_clear_cpu_cap (X86_FEATURE_TSC_DEADLINE_TIMER );
1364
- return ;
1365
- }
1342
+ /* Make sure that cpu and tsc are not already calibrated */
1343
+ WARN_ON (cpu_khz || tsc_khz );
1366
1344
1367
1345
cpu_khz = x86_platform .calibrate_cpu ();
1368
1346
tsc_khz = x86_platform .calibrate_tsc ();
@@ -1377,20 +1355,52 @@ void __init tsc_init(void)
1377
1355
else if (abs (cpu_khz - tsc_khz ) * 10 > tsc_khz )
1378
1356
cpu_khz = tsc_khz ;
1379
1357
1380
- if (!tsc_khz ) {
1381
- mark_tsc_unstable ("could not calculate TSC khz" );
1382
- setup_clear_cpu_cap (X86_FEATURE_TSC_DEADLINE_TIMER );
1383
- return ;
1384
- }
1358
+ if (tsc_khz == 0 )
1359
+ return false;
1385
1360
1386
1361
pr_info ("Detected %lu.%03lu MHz processor\n" ,
1387
- (unsigned long )cpu_khz / 1000 ,
1388
- (unsigned long )cpu_khz % 1000 );
1362
+ (unsigned long )cpu_khz / KHZ ,
1363
+ (unsigned long )cpu_khz % KHZ );
1389
1364
1390
1365
if (cpu_khz != tsc_khz ) {
1391
1366
pr_info ("Detected %lu.%03lu MHz TSC" ,
1392
- (unsigned long )tsc_khz / 1000 ,
1393
- (unsigned long )tsc_khz % 1000 );
1367
+ (unsigned long )tsc_khz / KHZ ,
1368
+ (unsigned long )tsc_khz % KHZ );
1369
+ }
1370
+ return true;
1371
+ }
1372
+
1373
+ static unsigned long __init get_loops_per_jiffy (void )
1374
+ {
1375
+ unsigned long lpj = tsc_khz * KHZ ;
1376
+
1377
+ do_div (lpj , HZ );
1378
+ return lpj ;
1379
+ }
1380
+
1381
+ void __init tsc_early_init (void )
1382
+ {
1383
+ if (!boot_cpu_has (X86_FEATURE_TSC ))
1384
+ return ;
1385
+ if (!determine_cpu_tsc_frequencies ())
1386
+ return ;
1387
+ loops_per_jiffy = get_loops_per_jiffy ();
1388
+ }
1389
+
1390
+ void __init tsc_init (void )
1391
+ {
1392
+ if (!boot_cpu_has (X86_FEATURE_TSC )) {
1393
+ setup_clear_cpu_cap (X86_FEATURE_TSC_DEADLINE_TIMER );
1394
+ return ;
1395
+ }
1396
+
1397
+ if (!tsc_khz ) {
1398
+ /* We failed to determine frequencies earlier, try again */
1399
+ if (!determine_cpu_tsc_frequencies ()) {
1400
+ mark_tsc_unstable ("could not calculate TSC khz" );
1401
+ setup_clear_cpu_cap (X86_FEATURE_TSC_DEADLINE_TIMER );
1402
+ return ;
1403
+ }
1394
1404
}
1395
1405
1396
1406
/* Sanitize TSC ADJUST before cyc2ns gets initialized */
@@ -1413,10 +1423,7 @@ void __init tsc_init(void)
1413
1423
if (!no_sched_irq_time )
1414
1424
enable_sched_clock_irqtime ();
1415
1425
1416
- lpj = ((u64 )tsc_khz * 1000 );
1417
- do_div (lpj , HZ );
1418
- lpj_fine = lpj ;
1419
-
1426
+ lpj_fine = get_loops_per_jiffy ();
1420
1427
use_tsc_delay ();
1421
1428
1422
1429
check_system_tsc_reliable ();
0 commit comments