Skip to content

Commit 279f146

Browse files
Suresh SiddhaKAGA-KOKO
authored andcommitted
x86: apic: Use tsc deadline for oneshot when available
If the TSC deadline mode is supported, LAPIC timer one-shot mode can be implemented using IA32_TSC_DEADLINE MSR. An interrupt will be generated when the TSC value equals or exceeds the value in the IA32_TSC_DEADLINE MSR. This enables us to skip the APIC calibration during boot. Also, in xapic mode, this enables us to skip the uncached apic access to re-arm the APIC timer. As this timer ticks at the high frequency TSC rate, we use the TSC_DIVISOR (32) to work with the 32-bit restrictions in the clockevent API's to avoid 64-bit divides etc (frequency is u32 and "unsigned long" in the set_next_event(), max_delta limits the next event to 32-bit for 32-bit kernel). Signed-off-by: Suresh Siddha <suresh.b.siddha@intel.com> Cc: venki@google.com Cc: len.brown@intel.com Link: http://lkml.kernel.org/r/1350941878.6017.31.camel@sbsiddha-desk.sc.intel.com Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
1 parent 1e207eb commit 279f146

3 files changed

Lines changed: 59 additions & 20 deletions

File tree

‎Documentation/kernel-parameters.txt‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1304,6 +1304,10 @@ bytes respectively. Such letter suffixes can also be entirely omitted.
13041304
lapic [X86-32,APIC] Enable the local APIC even if BIOS
13051305
disabled it.
13061306

1307+
lapic= [x86,APIC] "notscdeadline" Do not use TSC deadline
1308+
value for LAPIC timer one-shot implementation. Default
1309+
back to the programmable timer unit in the LAPIC.
1310+
13071311
lapic_timer_c2_ok [X86,APIC] trust the local apic timer
13081312
in C2 power state.
13091313

‎arch/x86/include/asm/msr-index.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,8 @@
337337
#define MSR_IA32_MISC_ENABLE_TURBO_DISABLE (1ULL << 38)
338338
#define MSR_IA32_MISC_ENABLE_IP_PREF_DISABLE (1ULL << 39)
339339

340+
#define MSR_IA32_TSC_DEADLINE 0x000006E0
341+
340342
/* P4/Xeon+ specific */
341343
#define MSR_IA32_MCG_EAX 0x00000180
342344
#define MSR_IA32_MCG_EBX 0x00000181

‎arch/x86/kernel/apic/apic.c‎

Lines changed: 53 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -90,21 +90,6 @@ EXPORT_EARLY_PER_CPU_SYMBOL(x86_bios_cpu_apicid);
9090
*/
9191
DEFINE_EARLY_PER_CPU_READ_MOSTLY(int, x86_cpu_to_logical_apicid, BAD_APICID);
9292

93-
/*
94-
* Knob to control our willingness to enable the local APIC.
95-
*
96-
* +1=force-enable
97-
*/
98-
static int force_enable_local_apic __initdata;
99-
/*
100-
* APIC command line parameters
101-
*/
102-
static int __init parse_lapic(char *arg)
103-
{
104-
force_enable_local_apic = 1;
105-
return 0;
106-
}
107-
early_param("lapic", parse_lapic);
10893
/* Local APIC was disabled by the BIOS and enabled by the kernel */
10994
static int enabled_via_apicbase;
11095

@@ -133,6 +118,25 @@ static inline void imcr_apic_to_pic(void)
133118
}
134119
#endif
135120

121+
/*
122+
* Knob to control our willingness to enable the local APIC.
123+
*
124+
* +1=force-enable
125+
*/
126+
static int force_enable_local_apic __initdata;
127+
/*
128+
* APIC command line parameters
129+
*/
130+
static int __init parse_lapic(char *arg)
131+
{
132+
if (config_enabled(CONFIG_X86_32) && !arg)
133+
force_enable_local_apic = 1;
134+
else if (!strncmp(arg, "notscdeadline", 13))
135+
setup_clear_cpu_cap(X86_FEATURE_TSC_DEADLINE_TIMER);
136+
return 0;
137+
}
138+
early_param("lapic", parse_lapic);
139+
136140
#ifdef CONFIG_X86_64
137141
static int apic_calibrate_pmtmr __initdata;
138142
static __init int setup_apicpmtimer(char *s)
@@ -315,6 +319,7 @@ int lapic_get_maxlvt(void)
315319

316320
/* Clock divisor */
317321
#define APIC_DIVISOR 16
322+
#define TSC_DIVISOR 32
318323

319324
/*
320325
* This function sets up the local APIC timer, with a timeout of
@@ -333,6 +338,9 @@ static void __setup_APIC_LVTT(unsigned int clocks, int oneshot, int irqen)
333338
lvtt_value = LOCAL_TIMER_VECTOR;
334339
if (!oneshot)
335340
lvtt_value |= APIC_LVT_TIMER_PERIODIC;
341+
else if (boot_cpu_has(X86_FEATURE_TSC_DEADLINE_TIMER))
342+
lvtt_value |= APIC_LVT_TIMER_TSCDEADLINE;
343+
336344
if (!lapic_is_integrated())
337345
lvtt_value |= SET_APIC_TIMER_BASE(APIC_TIMER_BASE_DIV);
338346

@@ -341,6 +349,11 @@ static void __setup_APIC_LVTT(unsigned int clocks, int oneshot, int irqen)
341349

342350
apic_write(APIC_LVTT, lvtt_value);
343351

352+
if (lvtt_value & APIC_LVT_TIMER_TSCDEADLINE) {
353+
printk_once(KERN_DEBUG "TSC deadline timer enabled\n");
354+
return;
355+
}
356+
344357
/*
345358
* Divide PICLK by 16
346359
*/
@@ -453,6 +466,16 @@ static int lapic_next_event(unsigned long delta,
453466
return 0;
454467
}
455468

469+
static int lapic_next_deadline(unsigned long delta,
470+
struct clock_event_device *evt)
471+
{
472+
u64 tsc;
473+
474+
rdtscll(tsc);
475+
wrmsrl(MSR_IA32_TSC_DEADLINE, tsc + (((u64) delta) * TSC_DIVISOR));
476+
return 0;
477+
}
478+
456479
/*
457480
* Setup the lapic timer in periodic or oneshot mode
458481
*/
@@ -533,7 +556,15 @@ static void __cpuinit setup_APIC_timer(void)
533556
memcpy(levt, &lapic_clockevent, sizeof(*levt));
534557
levt->cpumask = cpumask_of(smp_processor_id());
535558

536-
clockevents_register_device(levt);
559+
if (this_cpu_has(X86_FEATURE_TSC_DEADLINE_TIMER)) {
560+
levt->features &= ~(CLOCK_EVT_FEAT_PERIODIC |
561+
CLOCK_EVT_FEAT_DUMMY);
562+
levt->set_next_event = lapic_next_deadline;
563+
clockevents_config_and_register(levt,
564+
(tsc_khz / TSC_DIVISOR) * 1000,
565+
0xF, ~0UL);
566+
} else
567+
clockevents_register_device(levt);
537568
}
538569

539570
/*
@@ -661,7 +692,9 @@ static int __init calibrate_APIC_clock(void)
661692
* in the clockevent structure and return.
662693
*/
663694

664-
if (lapic_timer_frequency) {
695+
if (boot_cpu_has(X86_FEATURE_TSC_DEADLINE_TIMER)) {
696+
return 0;
697+
} else if (lapic_timer_frequency) {
665698
apic_printk(APIC_VERBOSE, "lapic timer already calibrated %d\n",
666699
lapic_timer_frequency);
667700
lapic_clockevent.mult = div_sc(lapic_timer_frequency/APIC_DIVISOR,
@@ -674,6 +707,9 @@ static int __init calibrate_APIC_clock(void)
674707
return 0;
675708
}
676709

710+
apic_printk(APIC_VERBOSE, "Using local APIC timer interrupts.\n"
711+
"calibrating APIC timer ...\n");
712+
677713
local_irq_disable();
678714

679715
/* Replace the global interrupt handler */
@@ -811,9 +847,6 @@ void __init setup_boot_APIC_clock(void)
811847
return;
812848
}
813849

814-
apic_printk(APIC_VERBOSE, "Using local APIC timer interrupts.\n"
815-
"calibrating APIC timer ...\n");
816-
817850
if (calibrate_APIC_clock()) {
818851
/* No broadcast on UP ! */
819852
if (num_possible_cpus() > 1)

0 commit comments

Comments
 (0)