xref: /openbmc/linux/arch/x86/kernel/apic/apic.c (revision a5a39156)
1f62bae50SIngo Molnar /*
2f62bae50SIngo Molnar  *	Local APIC handling, local APIC timers
3f62bae50SIngo Molnar  *
4f62bae50SIngo Molnar  *	(c) 1999, 2000, 2009 Ingo Molnar <mingo@redhat.com>
5f62bae50SIngo Molnar  *
6f62bae50SIngo Molnar  *	Fixes
7f62bae50SIngo Molnar  *	Maciej W. Rozycki	:	Bits for genuine 82489DX APICs;
8f62bae50SIngo Molnar  *					thanks to Eric Gilmore
9f62bae50SIngo Molnar  *					and Rolf G. Tews
10f62bae50SIngo Molnar  *					for testing these extensively.
11f62bae50SIngo Molnar  *	Maciej W. Rozycki	:	Various updates and fixes.
12f62bae50SIngo Molnar  *	Mikael Pettersson	:	Power Management for UP-APIC.
13f62bae50SIngo Molnar  *	Pavel Machek and
14f62bae50SIngo Molnar  *	Mikael Pettersson	:	PM converted to driver model.
15f62bae50SIngo Molnar  */
16f62bae50SIngo Molnar 
17cdd6c482SIngo Molnar #include <linux/perf_event.h>
18f62bae50SIngo Molnar #include <linux/kernel_stat.h>
19f62bae50SIngo Molnar #include <linux/mc146818rtc.h>
20f62bae50SIngo Molnar #include <linux/acpi_pmtmr.h>
21f62bae50SIngo Molnar #include <linux/clockchips.h>
22f62bae50SIngo Molnar #include <linux/interrupt.h>
23f62bae50SIngo Molnar #include <linux/bootmem.h>
24f62bae50SIngo Molnar #include <linux/ftrace.h>
25f62bae50SIngo Molnar #include <linux/ioport.h>
26f62bae50SIngo Molnar #include <linux/module.h>
27f3c6ea1bSRafael J. Wysocki #include <linux/syscore_ops.h>
28f62bae50SIngo Molnar #include <linux/delay.h>
29f62bae50SIngo Molnar #include <linux/timex.h>
30334955efSRalf Baechle #include <linux/i8253.h>
31f62bae50SIngo Molnar #include <linux/dmar.h>
32f62bae50SIngo Molnar #include <linux/init.h>
33f62bae50SIngo Molnar #include <linux/cpu.h>
34f62bae50SIngo Molnar #include <linux/dmi.h>
35f62bae50SIngo Molnar #include <linux/smp.h>
36f62bae50SIngo Molnar #include <linux/mm.h>
37f62bae50SIngo Molnar 
388a8f422dSSuresh Siddha #include <asm/irq_remapping.h>
39cdd6c482SIngo Molnar #include <asm/perf_event.h>
40736decacSThomas Gleixner #include <asm/x86_init.h>
41f62bae50SIngo Molnar #include <asm/pgalloc.h>
4260063497SArun Sharma #include <linux/atomic.h>
43f62bae50SIngo Molnar #include <asm/mpspec.h>
44f62bae50SIngo Molnar #include <asm/i8259.h>
45f62bae50SIngo Molnar #include <asm/proto.h>
46f62bae50SIngo Molnar #include <asm/apic.h>
477167d08eSHenrik Kretzschmar #include <asm/io_apic.h>
48f62bae50SIngo Molnar #include <asm/desc.h>
49f62bae50SIngo Molnar #include <asm/hpet.h>
50f62bae50SIngo Molnar #include <asm/idle.h>
51f62bae50SIngo Molnar #include <asm/mtrr.h>
5216f871bcSRalf Baechle #include <asm/time.h>
53f62bae50SIngo Molnar #include <asm/smp.h>
54638bee71SH. Peter Anvin #include <asm/mce.h>
558c3ba8d0SKerstin Jonsson #include <asm/tsc.h>
562904ed8dSSheng Yang #include <asm/hypervisor.h>
57f62bae50SIngo Molnar 
58f62bae50SIngo Molnar unsigned int num_processors;
59f62bae50SIngo Molnar 
60f62bae50SIngo Molnar unsigned disabled_cpus __cpuinitdata;
61f62bae50SIngo Molnar 
62f62bae50SIngo Molnar /* Processor that is doing the boot up */
63f62bae50SIngo Molnar unsigned int boot_cpu_physical_apicid = -1U;
64f62bae50SIngo Molnar 
65f62bae50SIngo Molnar /*
66f62bae50SIngo Molnar  * The highest APIC ID seen during enumeration.
67f62bae50SIngo Molnar  */
68f62bae50SIngo Molnar unsigned int max_physical_apicid;
69f62bae50SIngo Molnar 
70f62bae50SIngo Molnar /*
71f62bae50SIngo Molnar  * Bitmask of physically existing CPUs:
72f62bae50SIngo Molnar  */
73f62bae50SIngo Molnar physid_mask_t phys_cpu_present_map;
74f62bae50SIngo Molnar 
75f62bae50SIngo Molnar /*
76f62bae50SIngo Molnar  * Map cpu index to physical APIC ID
77f62bae50SIngo Molnar  */
78f62bae50SIngo Molnar DEFINE_EARLY_PER_CPU(u16, x86_cpu_to_apicid, BAD_APICID);
79f62bae50SIngo Molnar DEFINE_EARLY_PER_CPU(u16, x86_bios_cpu_apicid, BAD_APICID);
80f62bae50SIngo Molnar EXPORT_EARLY_PER_CPU_SYMBOL(x86_cpu_to_apicid);
81f62bae50SIngo Molnar EXPORT_EARLY_PER_CPU_SYMBOL(x86_bios_cpu_apicid);
82f62bae50SIngo Molnar 
83f62bae50SIngo Molnar #ifdef CONFIG_X86_32
844c321ff8STejun Heo 
854c321ff8STejun Heo /*
864c321ff8STejun Heo  * On x86_32, the mapping between cpu and logical apicid may vary
874c321ff8STejun Heo  * depending on apic in use.  The following early percpu variable is
884c321ff8STejun Heo  * used for the mapping.  This is where the behaviors of x86_64 and 32
894c321ff8STejun Heo  * actually diverge.  Let's keep it ugly for now.
904c321ff8STejun Heo  */
914c321ff8STejun Heo DEFINE_EARLY_PER_CPU(int, x86_cpu_to_logical_apicid, BAD_APICID);
924c321ff8STejun Heo 
93f62bae50SIngo Molnar /*
94f62bae50SIngo Molnar  * Knob to control our willingness to enable the local APIC.
95f62bae50SIngo Molnar  *
96f62bae50SIngo Molnar  * +1=force-enable
97f62bae50SIngo Molnar  */
9825874a29SHenrik Kretzschmar static int force_enable_local_apic __initdata;
99f62bae50SIngo Molnar /*
100f62bae50SIngo Molnar  * APIC command line parameters
101f62bae50SIngo Molnar  */
102f62bae50SIngo Molnar static int __init parse_lapic(char *arg)
103f62bae50SIngo Molnar {
104f62bae50SIngo Molnar 	force_enable_local_apic = 1;
105f62bae50SIngo Molnar 	return 0;
106f62bae50SIngo Molnar }
107f62bae50SIngo Molnar early_param("lapic", parse_lapic);
108f62bae50SIngo Molnar /* Local APIC was disabled by the BIOS and enabled by the kernel */
109f62bae50SIngo Molnar static int enabled_via_apicbase;
110f62bae50SIngo Molnar 
111c0eaa453SCyrill Gorcunov /*
112c0eaa453SCyrill Gorcunov  * Handle interrupt mode configuration register (IMCR).
113c0eaa453SCyrill Gorcunov  * This register controls whether the interrupt signals
114c0eaa453SCyrill Gorcunov  * that reach the BSP come from the master PIC or from the
115c0eaa453SCyrill Gorcunov  * local APIC. Before entering Symmetric I/O Mode, either
116c0eaa453SCyrill Gorcunov  * the BIOS or the operating system must switch out of
117c0eaa453SCyrill Gorcunov  * PIC Mode by changing the IMCR.
118c0eaa453SCyrill Gorcunov  */
1195cda395fSAlexander van Heukelum static inline void imcr_pic_to_apic(void)
120c0eaa453SCyrill Gorcunov {
121c0eaa453SCyrill Gorcunov 	/* select IMCR register */
122c0eaa453SCyrill Gorcunov 	outb(0x70, 0x22);
123c0eaa453SCyrill Gorcunov 	/* NMI and 8259 INTR go through APIC */
124c0eaa453SCyrill Gorcunov 	outb(0x01, 0x23);
125c0eaa453SCyrill Gorcunov }
126c0eaa453SCyrill Gorcunov 
1275cda395fSAlexander van Heukelum static inline void imcr_apic_to_pic(void)
128c0eaa453SCyrill Gorcunov {
129c0eaa453SCyrill Gorcunov 	/* select IMCR register */
130c0eaa453SCyrill Gorcunov 	outb(0x70, 0x22);
131c0eaa453SCyrill Gorcunov 	/* NMI and 8259 INTR go directly to BSP */
132c0eaa453SCyrill Gorcunov 	outb(0x00, 0x23);
133c0eaa453SCyrill Gorcunov }
134f62bae50SIngo Molnar #endif
135f62bae50SIngo Molnar 
136f62bae50SIngo Molnar #ifdef CONFIG_X86_64
137f62bae50SIngo Molnar static int apic_calibrate_pmtmr __initdata;
138f62bae50SIngo Molnar static __init int setup_apicpmtimer(char *s)
139f62bae50SIngo Molnar {
140f62bae50SIngo Molnar 	apic_calibrate_pmtmr = 1;
141f62bae50SIngo Molnar 	notsc_setup(NULL);
142f62bae50SIngo Molnar 	return 0;
143f62bae50SIngo Molnar }
144f62bae50SIngo Molnar __setup("apicpmtimer", setup_apicpmtimer);
145f62bae50SIngo Molnar #endif
146f62bae50SIngo Molnar 
147fc1edaf9SSuresh Siddha int x2apic_mode;
148f62bae50SIngo Molnar #ifdef CONFIG_X86_X2APIC
149f62bae50SIngo Molnar /* x2apic enabled before OS handover */
150fb209bd8SYinghai Lu int x2apic_preenabled;
151fb209bd8SYinghai Lu static int x2apic_disabled;
152a31bc327SYinghai Lu static int nox2apic;
153f62bae50SIngo Molnar static __init int setup_nox2apic(char *str)
154f62bae50SIngo Molnar {
15539d83a5dSSuresh Siddha 	if (x2apic_enabled()) {
156a31bc327SYinghai Lu 		int apicid = native_apic_msr_read(APIC_ID);
157a31bc327SYinghai Lu 
158a31bc327SYinghai Lu 		if (apicid >= 255) {
159a31bc327SYinghai Lu 			pr_warning("Apicid: %08x, cannot enforce nox2apic\n",
160a31bc327SYinghai Lu 				   apicid);
16139d83a5dSSuresh Siddha 			return 0;
16239d83a5dSSuresh Siddha 		}
16339d83a5dSSuresh Siddha 
164a31bc327SYinghai Lu 		pr_warning("x2apic already enabled. will disable it\n");
165a31bc327SYinghai Lu 	} else
166f62bae50SIngo Molnar 		setup_clear_cpu_cap(X86_FEATURE_X2APIC);
167a31bc327SYinghai Lu 
168a31bc327SYinghai Lu 	nox2apic = 1;
169a31bc327SYinghai Lu 
170f62bae50SIngo Molnar 	return 0;
171f62bae50SIngo Molnar }
172f62bae50SIngo Molnar early_param("nox2apic", setup_nox2apic);
173f62bae50SIngo Molnar #endif
174f62bae50SIngo Molnar 
175f62bae50SIngo Molnar unsigned long mp_lapic_addr;
176f62bae50SIngo Molnar int disable_apic;
177f62bae50SIngo Molnar /* Disable local APIC timer from the kernel commandline or via dmi quirk */
17825874a29SHenrik Kretzschmar static int disable_apic_timer __initdata;
179f62bae50SIngo Molnar /* Local APIC timer works in C2 */
180f62bae50SIngo Molnar int local_apic_timer_c2_ok;
181f62bae50SIngo Molnar EXPORT_SYMBOL_GPL(local_apic_timer_c2_ok);
182f62bae50SIngo Molnar 
183f62bae50SIngo Molnar int first_system_vector = 0xfe;
184f62bae50SIngo Molnar 
185f62bae50SIngo Molnar /*
186f62bae50SIngo Molnar  * Debug level, exported for io_apic.c
187f62bae50SIngo Molnar  */
188f62bae50SIngo Molnar unsigned int apic_verbosity;
189f62bae50SIngo Molnar 
190f62bae50SIngo Molnar int pic_mode;
191f62bae50SIngo Molnar 
192f62bae50SIngo Molnar /* Have we found an MP table */
193f62bae50SIngo Molnar int smp_found_config;
194f62bae50SIngo Molnar 
195f62bae50SIngo Molnar static struct resource lapic_resource = {
196f62bae50SIngo Molnar 	.name = "Local APIC",
197f62bae50SIngo Molnar 	.flags = IORESOURCE_MEM | IORESOURCE_BUSY,
198f62bae50SIngo Molnar };
199f62bae50SIngo Molnar 
2001ade93efSJacob Pan unsigned int lapic_timer_frequency = 0;
201f62bae50SIngo Molnar 
202f62bae50SIngo Molnar static void apic_pm_activate(void);
203f62bae50SIngo Molnar 
204f62bae50SIngo Molnar static unsigned long apic_phys;
205f62bae50SIngo Molnar 
206f62bae50SIngo Molnar /*
207f62bae50SIngo Molnar  * Get the LAPIC version
208f62bae50SIngo Molnar  */
209f62bae50SIngo Molnar static inline int lapic_get_version(void)
210f62bae50SIngo Molnar {
211f62bae50SIngo Molnar 	return GET_APIC_VERSION(apic_read(APIC_LVR));
212f62bae50SIngo Molnar }
213f62bae50SIngo Molnar 
214f62bae50SIngo Molnar /*
215f62bae50SIngo Molnar  * Check, if the APIC is integrated or a separate chip
216f62bae50SIngo Molnar  */
217f62bae50SIngo Molnar static inline int lapic_is_integrated(void)
218f62bae50SIngo Molnar {
219f62bae50SIngo Molnar #ifdef CONFIG_X86_64
220f62bae50SIngo Molnar 	return 1;
221f62bae50SIngo Molnar #else
222f62bae50SIngo Molnar 	return APIC_INTEGRATED(lapic_get_version());
223f62bae50SIngo Molnar #endif
224f62bae50SIngo Molnar }
225f62bae50SIngo Molnar 
226f62bae50SIngo Molnar /*
227f62bae50SIngo Molnar  * Check, whether this is a modern or a first generation APIC
228f62bae50SIngo Molnar  */
229f62bae50SIngo Molnar static int modern_apic(void)
230f62bae50SIngo Molnar {
231f62bae50SIngo Molnar 	/* AMD systems use old APIC versions, so check the CPU */
232f62bae50SIngo Molnar 	if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
233f62bae50SIngo Molnar 	    boot_cpu_data.x86 >= 0xf)
234f62bae50SIngo Molnar 		return 1;
235f62bae50SIngo Molnar 	return lapic_get_version() >= 0x14;
236f62bae50SIngo Molnar }
237f62bae50SIngo Molnar 
23808306ce6SCyrill Gorcunov /*
239a933c618SCyrill Gorcunov  * right after this call apic become NOOP driven
240a933c618SCyrill Gorcunov  * so apic->write/read doesn't do anything
24108306ce6SCyrill Gorcunov  */
24225874a29SHenrik Kretzschmar static void __init apic_disable(void)
24308306ce6SCyrill Gorcunov {
244f88f2b4fSCyrill Gorcunov 	pr_info("APIC: switched to apic NOOP\n");
245a933c618SCyrill Gorcunov 	apic = &apic_noop;
24608306ce6SCyrill Gorcunov }
24708306ce6SCyrill Gorcunov 
248f62bae50SIngo Molnar void native_apic_wait_icr_idle(void)
249f62bae50SIngo Molnar {
250f62bae50SIngo Molnar 	while (apic_read(APIC_ICR) & APIC_ICR_BUSY)
251f62bae50SIngo Molnar 		cpu_relax();
252f62bae50SIngo Molnar }
253f62bae50SIngo Molnar 
254f62bae50SIngo Molnar u32 native_safe_apic_wait_icr_idle(void)
255f62bae50SIngo Molnar {
256f62bae50SIngo Molnar 	u32 send_status;
257f62bae50SIngo Molnar 	int timeout;
258f62bae50SIngo Molnar 
259f62bae50SIngo Molnar 	timeout = 0;
260f62bae50SIngo Molnar 	do {
261f62bae50SIngo Molnar 		send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
262f62bae50SIngo Molnar 		if (!send_status)
263f62bae50SIngo Molnar 			break;
264b49d7d87SFernando Luis Vazquez Cao 		inc_irq_stat(icr_read_retry_count);
265f62bae50SIngo Molnar 		udelay(100);
266f62bae50SIngo Molnar 	} while (timeout++ < 1000);
267f62bae50SIngo Molnar 
268f62bae50SIngo Molnar 	return send_status;
269f62bae50SIngo Molnar }
270f62bae50SIngo Molnar 
271f62bae50SIngo Molnar void native_apic_icr_write(u32 low, u32 id)
272f62bae50SIngo Molnar {
273f62bae50SIngo Molnar 	apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(id));
274f62bae50SIngo Molnar 	apic_write(APIC_ICR, low);
275f62bae50SIngo Molnar }
276f62bae50SIngo Molnar 
277f62bae50SIngo Molnar u64 native_apic_icr_read(void)
278f62bae50SIngo Molnar {
279f62bae50SIngo Molnar 	u32 icr1, icr2;
280f62bae50SIngo Molnar 
281f62bae50SIngo Molnar 	icr2 = apic_read(APIC_ICR2);
282f62bae50SIngo Molnar 	icr1 = apic_read(APIC_ICR);
283f62bae50SIngo Molnar 
284f62bae50SIngo Molnar 	return icr1 | ((u64)icr2 << 32);
285f62bae50SIngo Molnar }
286f62bae50SIngo Molnar 
287f62bae50SIngo Molnar #ifdef CONFIG_X86_32
288f62bae50SIngo Molnar /**
289f62bae50SIngo Molnar  * get_physical_broadcast - Get number of physical broadcast IDs
290f62bae50SIngo Molnar  */
291f62bae50SIngo Molnar int get_physical_broadcast(void)
292f62bae50SIngo Molnar {
293f62bae50SIngo Molnar 	return modern_apic() ? 0xff : 0xf;
294f62bae50SIngo Molnar }
295f62bae50SIngo Molnar #endif
296f62bae50SIngo Molnar 
297f62bae50SIngo Molnar /**
298f62bae50SIngo Molnar  * lapic_get_maxlvt - get the maximum number of local vector table entries
299f62bae50SIngo Molnar  */
300f62bae50SIngo Molnar int lapic_get_maxlvt(void)
301f62bae50SIngo Molnar {
302f62bae50SIngo Molnar 	unsigned int v;
303f62bae50SIngo Molnar 
304f62bae50SIngo Molnar 	v = apic_read(APIC_LVR);
305f62bae50SIngo Molnar 	/*
306f62bae50SIngo Molnar 	 * - we always have APIC integrated on 64bit mode
307f62bae50SIngo Molnar 	 * - 82489DXs do not report # of LVT entries
308f62bae50SIngo Molnar 	 */
309f62bae50SIngo Molnar 	return APIC_INTEGRATED(GET_APIC_VERSION(v)) ? GET_APIC_MAXLVT(v) : 2;
310f62bae50SIngo Molnar }
311f62bae50SIngo Molnar 
312f62bae50SIngo Molnar /*
313f62bae50SIngo Molnar  * Local APIC timer
314f62bae50SIngo Molnar  */
315f62bae50SIngo Molnar 
316f62bae50SIngo Molnar /* Clock divisor */
317f62bae50SIngo Molnar #define APIC_DIVISOR 16
318f62bae50SIngo Molnar 
319f62bae50SIngo Molnar /*
320f62bae50SIngo Molnar  * This function sets up the local APIC timer, with a timeout of
321f62bae50SIngo Molnar  * 'clocks' APIC bus clock. During calibration we actually call
322f62bae50SIngo Molnar  * this function twice on the boot CPU, once with a bogus timeout
323f62bae50SIngo Molnar  * value, second time for real. The other (noncalibrating) CPUs
324f62bae50SIngo Molnar  * call this function only once, with the real, calibrated value.
325f62bae50SIngo Molnar  *
326f62bae50SIngo Molnar  * We do reads before writes even if unnecessary, to get around the
327f62bae50SIngo Molnar  * P5 APIC double write bug.
328f62bae50SIngo Molnar  */
329f62bae50SIngo Molnar static void __setup_APIC_LVTT(unsigned int clocks, int oneshot, int irqen)
330f62bae50SIngo Molnar {
331f62bae50SIngo Molnar 	unsigned int lvtt_value, tmp_value;
332f62bae50SIngo Molnar 
333f62bae50SIngo Molnar 	lvtt_value = LOCAL_TIMER_VECTOR;
334f62bae50SIngo Molnar 	if (!oneshot)
335f62bae50SIngo Molnar 		lvtt_value |= APIC_LVT_TIMER_PERIODIC;
336f62bae50SIngo Molnar 	if (!lapic_is_integrated())
337f62bae50SIngo Molnar 		lvtt_value |= SET_APIC_TIMER_BASE(APIC_TIMER_BASE_DIV);
338f62bae50SIngo Molnar 
339f62bae50SIngo Molnar 	if (!irqen)
340f62bae50SIngo Molnar 		lvtt_value |= APIC_LVT_MASKED;
341f62bae50SIngo Molnar 
342f62bae50SIngo Molnar 	apic_write(APIC_LVTT, lvtt_value);
343f62bae50SIngo Molnar 
344f62bae50SIngo Molnar 	/*
345f62bae50SIngo Molnar 	 * Divide PICLK by 16
346f62bae50SIngo Molnar 	 */
347f62bae50SIngo Molnar 	tmp_value = apic_read(APIC_TDCR);
348f62bae50SIngo Molnar 	apic_write(APIC_TDCR,
349f62bae50SIngo Molnar 		(tmp_value & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE)) |
350f62bae50SIngo Molnar 		APIC_TDR_DIV_16);
351f62bae50SIngo Molnar 
352f62bae50SIngo Molnar 	if (!oneshot)
353f62bae50SIngo Molnar 		apic_write(APIC_TMICT, clocks / APIC_DIVISOR);
354f62bae50SIngo Molnar }
355f62bae50SIngo Molnar 
356f62bae50SIngo Molnar /*
357a68c439bSRobert Richter  * Setup extended LVT, AMD specific
358f62bae50SIngo Molnar  *
359a68c439bSRobert Richter  * Software should use the LVT offsets the BIOS provides.  The offsets
360a68c439bSRobert Richter  * are determined by the subsystems using it like those for MCE
361a68c439bSRobert Richter  * threshold or IBS.  On K8 only offset 0 (APIC500) and MCE interrupts
362a68c439bSRobert Richter  * are supported. Beginning with family 10h at least 4 offsets are
363a68c439bSRobert Richter  * available.
364f62bae50SIngo Molnar  *
365a68c439bSRobert Richter  * Since the offsets must be consistent for all cores, we keep track
366a68c439bSRobert Richter  * of the LVT offsets in software and reserve the offset for the same
367a68c439bSRobert Richter  * vector also to be used on other cores. An offset is freed by
368a68c439bSRobert Richter  * setting the entry to APIC_EILVT_MASKED.
369a68c439bSRobert Richter  *
370a68c439bSRobert Richter  * If the BIOS is right, there should be no conflicts. Otherwise a
371a68c439bSRobert Richter  * "[Firmware Bug]: ..." error message is generated. However, if
372a68c439bSRobert Richter  * software does not properly determines the offsets, it is not
373a68c439bSRobert Richter  * necessarily a BIOS bug.
374f62bae50SIngo Molnar  */
375f62bae50SIngo Molnar 
376a68c439bSRobert Richter static atomic_t eilvt_offsets[APIC_EILVT_NR_MAX];
377f62bae50SIngo Molnar 
378a68c439bSRobert Richter static inline int eilvt_entry_is_changeable(unsigned int old, unsigned int new)
379a68c439bSRobert Richter {
380a68c439bSRobert Richter 	return (old & APIC_EILVT_MASKED)
381a68c439bSRobert Richter 		|| (new == APIC_EILVT_MASKED)
382a68c439bSRobert Richter 		|| ((new & ~APIC_EILVT_MASKED) == old);
383a68c439bSRobert Richter }
384a68c439bSRobert Richter 
385a68c439bSRobert Richter static unsigned int reserve_eilvt_offset(int offset, unsigned int new)
386a68c439bSRobert Richter {
3878abc3122SRobert Richter 	unsigned int rsvd, vector;
388a68c439bSRobert Richter 
389a68c439bSRobert Richter 	if (offset >= APIC_EILVT_NR_MAX)
390a68c439bSRobert Richter 		return ~0;
391a68c439bSRobert Richter 
3928abc3122SRobert Richter 	rsvd = atomic_read(&eilvt_offsets[offset]);
393a68c439bSRobert Richter 	do {
3948abc3122SRobert Richter 		vector = rsvd & ~APIC_EILVT_MASKED;	/* 0: unassigned */
3958abc3122SRobert Richter 		if (vector && !eilvt_entry_is_changeable(vector, new))
396a68c439bSRobert Richter 			/* may not change if vectors are different */
397a68c439bSRobert Richter 			return rsvd;
398a68c439bSRobert Richter 		rsvd = atomic_cmpxchg(&eilvt_offsets[offset], rsvd, new);
399a68c439bSRobert Richter 	} while (rsvd != new);
400a68c439bSRobert Richter 
4018abc3122SRobert Richter 	rsvd &= ~APIC_EILVT_MASKED;
4028abc3122SRobert Richter 	if (rsvd && rsvd != vector)
4038abc3122SRobert Richter 		pr_info("LVT offset %d assigned for vector 0x%02x\n",
4048abc3122SRobert Richter 			offset, rsvd);
4058abc3122SRobert Richter 
406a68c439bSRobert Richter 	return new;
407a68c439bSRobert Richter }
408a68c439bSRobert Richter 
409a68c439bSRobert Richter /*
410a68c439bSRobert Richter  * If mask=1, the LVT entry does not generate interrupts while mask=0
411cbf74ceaSRobert Richter  * enables the vector. See also the BKDGs. Must be called with
412cbf74ceaSRobert Richter  * preemption disabled.
413a68c439bSRobert Richter  */
414a68c439bSRobert Richter 
41527afdf20SRobert Richter int setup_APIC_eilvt(u8 offset, u8 vector, u8 msg_type, u8 mask)
416a68c439bSRobert Richter {
417a68c439bSRobert Richter 	unsigned long reg = APIC_EILVTn(offset);
418a68c439bSRobert Richter 	unsigned int new, old, reserved;
419a68c439bSRobert Richter 
420a68c439bSRobert Richter 	new = (mask << 16) | (msg_type << 8) | vector;
421a68c439bSRobert Richter 	old = apic_read(reg);
422a68c439bSRobert Richter 	reserved = reserve_eilvt_offset(offset, new);
423a68c439bSRobert Richter 
424a68c439bSRobert Richter 	if (reserved != new) {
425eb48c9cbSRobert Richter 		pr_err(FW_BUG "cpu %d, try to use APIC%lX (LVT offset %d) for "
426eb48c9cbSRobert Richter 		       "vector 0x%x, but the register is already in use for "
427eb48c9cbSRobert Richter 		       "vector 0x%x on another cpu\n",
428eb48c9cbSRobert Richter 		       smp_processor_id(), reg, offset, new, reserved);
429a68c439bSRobert Richter 		return -EINVAL;
430a68c439bSRobert Richter 	}
431a68c439bSRobert Richter 
432a68c439bSRobert Richter 	if (!eilvt_entry_is_changeable(old, new)) {
433eb48c9cbSRobert Richter 		pr_err(FW_BUG "cpu %d, try to use APIC%lX (LVT offset %d) for "
434eb48c9cbSRobert Richter 		       "vector 0x%x, but the register is already in use for "
435eb48c9cbSRobert Richter 		       "vector 0x%x on this cpu\n",
436eb48c9cbSRobert Richter 		       smp_processor_id(), reg, offset, new, old);
437a68c439bSRobert Richter 		return -EBUSY;
438a68c439bSRobert Richter 	}
439a68c439bSRobert Richter 
440a68c439bSRobert Richter 	apic_write(reg, new);
441a68c439bSRobert Richter 
442a68c439bSRobert Richter 	return 0;
443f62bae50SIngo Molnar }
44427afdf20SRobert Richter EXPORT_SYMBOL_GPL(setup_APIC_eilvt);
445f62bae50SIngo Molnar 
446f62bae50SIngo Molnar /*
447f62bae50SIngo Molnar  * Program the next event, relative to now
448f62bae50SIngo Molnar  */
449f62bae50SIngo Molnar static int lapic_next_event(unsigned long delta,
450f62bae50SIngo Molnar 			    struct clock_event_device *evt)
451f62bae50SIngo Molnar {
452f62bae50SIngo Molnar 	apic_write(APIC_TMICT, delta);
453f62bae50SIngo Molnar 	return 0;
454f62bae50SIngo Molnar }
455f62bae50SIngo Molnar 
456f62bae50SIngo Molnar /*
457f62bae50SIngo Molnar  * Setup the lapic timer in periodic or oneshot mode
458f62bae50SIngo Molnar  */
459f62bae50SIngo Molnar static void lapic_timer_setup(enum clock_event_mode mode,
460f62bae50SIngo Molnar 			      struct clock_event_device *evt)
461f62bae50SIngo Molnar {
462f62bae50SIngo Molnar 	unsigned long flags;
463f62bae50SIngo Molnar 	unsigned int v;
464f62bae50SIngo Molnar 
465f62bae50SIngo Molnar 	/* Lapic used as dummy for broadcast ? */
466f62bae50SIngo Molnar 	if (evt->features & CLOCK_EVT_FEAT_DUMMY)
467f62bae50SIngo Molnar 		return;
468f62bae50SIngo Molnar 
469f62bae50SIngo Molnar 	local_irq_save(flags);
470f62bae50SIngo Molnar 
471f62bae50SIngo Molnar 	switch (mode) {
472f62bae50SIngo Molnar 	case CLOCK_EVT_MODE_PERIODIC:
473f62bae50SIngo Molnar 	case CLOCK_EVT_MODE_ONESHOT:
4741ade93efSJacob Pan 		__setup_APIC_LVTT(lapic_timer_frequency,
475f62bae50SIngo Molnar 				  mode != CLOCK_EVT_MODE_PERIODIC, 1);
476f62bae50SIngo Molnar 		break;
477f62bae50SIngo Molnar 	case CLOCK_EVT_MODE_UNUSED:
478f62bae50SIngo Molnar 	case CLOCK_EVT_MODE_SHUTDOWN:
479f62bae50SIngo Molnar 		v = apic_read(APIC_LVTT);
480f62bae50SIngo Molnar 		v |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
481f62bae50SIngo Molnar 		apic_write(APIC_LVTT, v);
4826f9b4100SAndreas Herrmann 		apic_write(APIC_TMICT, 0);
483f62bae50SIngo Molnar 		break;
484f62bae50SIngo Molnar 	case CLOCK_EVT_MODE_RESUME:
485f62bae50SIngo Molnar 		/* Nothing to do here */
486f62bae50SIngo Molnar 		break;
487f62bae50SIngo Molnar 	}
488f62bae50SIngo Molnar 
489f62bae50SIngo Molnar 	local_irq_restore(flags);
490f62bae50SIngo Molnar }
491f62bae50SIngo Molnar 
492f62bae50SIngo Molnar /*
493f62bae50SIngo Molnar  * Local APIC timer broadcast function
494f62bae50SIngo Molnar  */
495f62bae50SIngo Molnar static void lapic_timer_broadcast(const struct cpumask *mask)
496f62bae50SIngo Molnar {
497f62bae50SIngo Molnar #ifdef CONFIG_SMP
498f62bae50SIngo Molnar 	apic->send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
499f62bae50SIngo Molnar #endif
500f62bae50SIngo Molnar }
501f62bae50SIngo Molnar 
50225874a29SHenrik Kretzschmar 
50325874a29SHenrik Kretzschmar /*
50425874a29SHenrik Kretzschmar  * The local apic timer can be used for any function which is CPU local.
50525874a29SHenrik Kretzschmar  */
50625874a29SHenrik Kretzschmar static struct clock_event_device lapic_clockevent = {
50725874a29SHenrik Kretzschmar 	.name		= "lapic",
50825874a29SHenrik Kretzschmar 	.features	= CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT
50925874a29SHenrik Kretzschmar 			| CLOCK_EVT_FEAT_C3STOP | CLOCK_EVT_FEAT_DUMMY,
51025874a29SHenrik Kretzschmar 	.shift		= 32,
51125874a29SHenrik Kretzschmar 	.set_mode	= lapic_timer_setup,
51225874a29SHenrik Kretzschmar 	.set_next_event	= lapic_next_event,
51325874a29SHenrik Kretzschmar 	.broadcast	= lapic_timer_broadcast,
51425874a29SHenrik Kretzschmar 	.rating		= 100,
51525874a29SHenrik Kretzschmar 	.irq		= -1,
51625874a29SHenrik Kretzschmar };
51725874a29SHenrik Kretzschmar static DEFINE_PER_CPU(struct clock_event_device, lapic_events);
51825874a29SHenrik Kretzschmar 
519f62bae50SIngo Molnar /*
520421f91d2SUwe Kleine-König  * Setup the local APIC timer for this CPU. Copy the initialized values
521f62bae50SIngo Molnar  * of the boot CPU and register the clock event in the framework.
522f62bae50SIngo Molnar  */
523f62bae50SIngo Molnar static void __cpuinit setup_APIC_timer(void)
524f62bae50SIngo Molnar {
525f62bae50SIngo Molnar 	struct clock_event_device *levt = &__get_cpu_var(lapic_events);
526f62bae50SIngo Molnar 
527349c004eSChristoph Lameter 	if (this_cpu_has(X86_FEATURE_ARAT)) {
528db954b58SVenkatesh Pallipadi 		lapic_clockevent.features &= ~CLOCK_EVT_FEAT_C3STOP;
529db954b58SVenkatesh Pallipadi 		/* Make LAPIC timer preferrable over percpu HPET */
530db954b58SVenkatesh Pallipadi 		lapic_clockevent.rating = 150;
531db954b58SVenkatesh Pallipadi 	}
532db954b58SVenkatesh Pallipadi 
533f62bae50SIngo Molnar 	memcpy(levt, &lapic_clockevent, sizeof(*levt));
534f62bae50SIngo Molnar 	levt->cpumask = cpumask_of(smp_processor_id());
535f62bae50SIngo Molnar 
536f62bae50SIngo Molnar 	clockevents_register_device(levt);
537f62bae50SIngo Molnar }
538f62bae50SIngo Molnar 
539f62bae50SIngo Molnar /*
540f62bae50SIngo Molnar  * In this functions we calibrate APIC bus clocks to the external timer.
541f62bae50SIngo Molnar  *
542f62bae50SIngo Molnar  * We want to do the calibration only once since we want to have local timer
543f62bae50SIngo Molnar  * irqs syncron. CPUs connected by the same APIC bus have the very same bus
544f62bae50SIngo Molnar  * frequency.
545f62bae50SIngo Molnar  *
546f62bae50SIngo Molnar  * This was previously done by reading the PIT/HPET and waiting for a wrap
547f62bae50SIngo Molnar  * around to find out, that a tick has elapsed. I have a box, where the PIT
548f62bae50SIngo Molnar  * readout is broken, so it never gets out of the wait loop again. This was
549f62bae50SIngo Molnar  * also reported by others.
550f62bae50SIngo Molnar  *
551f62bae50SIngo Molnar  * Monitoring the jiffies value is inaccurate and the clockevents
552f62bae50SIngo Molnar  * infrastructure allows us to do a simple substitution of the interrupt
553f62bae50SIngo Molnar  * handler.
554f62bae50SIngo Molnar  *
555f62bae50SIngo Molnar  * The calibration routine also uses the pm_timer when possible, as the PIT
556f62bae50SIngo Molnar  * happens to run way too slow (factor 2.3 on my VAIO CoreDuo, which goes
557f62bae50SIngo Molnar  * back to normal later in the boot process).
558f62bae50SIngo Molnar  */
559f62bae50SIngo Molnar 
560f62bae50SIngo Molnar #define LAPIC_CAL_LOOPS		(HZ/10)
561f62bae50SIngo Molnar 
562f62bae50SIngo Molnar static __initdata int lapic_cal_loops = -1;
563f62bae50SIngo Molnar static __initdata long lapic_cal_t1, lapic_cal_t2;
564f62bae50SIngo Molnar static __initdata unsigned long long lapic_cal_tsc1, lapic_cal_tsc2;
565f62bae50SIngo Molnar static __initdata unsigned long lapic_cal_pm1, lapic_cal_pm2;
566f62bae50SIngo Molnar static __initdata unsigned long lapic_cal_j1, lapic_cal_j2;
567f62bae50SIngo Molnar 
568f62bae50SIngo Molnar /*
569f62bae50SIngo Molnar  * Temporary interrupt handler.
570f62bae50SIngo Molnar  */
571f62bae50SIngo Molnar static void __init lapic_cal_handler(struct clock_event_device *dev)
572f62bae50SIngo Molnar {
573f62bae50SIngo Molnar 	unsigned long long tsc = 0;
574f62bae50SIngo Molnar 	long tapic = apic_read(APIC_TMCCT);
575f62bae50SIngo Molnar 	unsigned long pm = acpi_pm_read_early();
576f62bae50SIngo Molnar 
577f62bae50SIngo Molnar 	if (cpu_has_tsc)
578f62bae50SIngo Molnar 		rdtscll(tsc);
579f62bae50SIngo Molnar 
580f62bae50SIngo Molnar 	switch (lapic_cal_loops++) {
581f62bae50SIngo Molnar 	case 0:
582f62bae50SIngo Molnar 		lapic_cal_t1 = tapic;
583f62bae50SIngo Molnar 		lapic_cal_tsc1 = tsc;
584f62bae50SIngo Molnar 		lapic_cal_pm1 = pm;
585f62bae50SIngo Molnar 		lapic_cal_j1 = jiffies;
586f62bae50SIngo Molnar 		break;
587f62bae50SIngo Molnar 
588f62bae50SIngo Molnar 	case LAPIC_CAL_LOOPS:
589f62bae50SIngo Molnar 		lapic_cal_t2 = tapic;
590f62bae50SIngo Molnar 		lapic_cal_tsc2 = tsc;
591f62bae50SIngo Molnar 		if (pm < lapic_cal_pm1)
592f62bae50SIngo Molnar 			pm += ACPI_PM_OVRRUN;
593f62bae50SIngo Molnar 		lapic_cal_pm2 = pm;
594f62bae50SIngo Molnar 		lapic_cal_j2 = jiffies;
595f62bae50SIngo Molnar 		break;
596f62bae50SIngo Molnar 	}
597f62bae50SIngo Molnar }
598f62bae50SIngo Molnar 
599f62bae50SIngo Molnar static int __init
600f62bae50SIngo Molnar calibrate_by_pmtimer(long deltapm, long *delta, long *deltatsc)
601f62bae50SIngo Molnar {
602f62bae50SIngo Molnar 	const long pm_100ms = PMTMR_TICKS_PER_SEC / 10;
603f62bae50SIngo Molnar 	const long pm_thresh = pm_100ms / 100;
604f62bae50SIngo Molnar 	unsigned long mult;
605f62bae50SIngo Molnar 	u64 res;
606f62bae50SIngo Molnar 
607f62bae50SIngo Molnar #ifndef CONFIG_X86_PM_TIMER
608f62bae50SIngo Molnar 	return -1;
609f62bae50SIngo Molnar #endif
610f62bae50SIngo Molnar 
611f62bae50SIngo Molnar 	apic_printk(APIC_VERBOSE, "... PM-Timer delta = %ld\n", deltapm);
612f62bae50SIngo Molnar 
613f62bae50SIngo Molnar 	/* Check, if the PM timer is available */
614f62bae50SIngo Molnar 	if (!deltapm)
615f62bae50SIngo Molnar 		return -1;
616f62bae50SIngo Molnar 
617f62bae50SIngo Molnar 	mult = clocksource_hz2mult(PMTMR_TICKS_PER_SEC, 22);
618f62bae50SIngo Molnar 
619f62bae50SIngo Molnar 	if (deltapm > (pm_100ms - pm_thresh) &&
620f62bae50SIngo Molnar 	    deltapm < (pm_100ms + pm_thresh)) {
621f62bae50SIngo Molnar 		apic_printk(APIC_VERBOSE, "... PM-Timer result ok\n");
622f62bae50SIngo Molnar 		return 0;
623f62bae50SIngo Molnar 	}
624f62bae50SIngo Molnar 
625f62bae50SIngo Molnar 	res = (((u64)deltapm) *  mult) >> 22;
626f62bae50SIngo Molnar 	do_div(res, 1000000);
627f62bae50SIngo Molnar 	pr_warning("APIC calibration not consistent "
628f62bae50SIngo Molnar 		   "with PM-Timer: %ldms instead of 100ms\n",(long)res);
629f62bae50SIngo Molnar 
630f62bae50SIngo Molnar 	/* Correct the lapic counter value */
631f62bae50SIngo Molnar 	res = (((u64)(*delta)) * pm_100ms);
632f62bae50SIngo Molnar 	do_div(res, deltapm);
633f62bae50SIngo Molnar 	pr_info("APIC delta adjusted to PM-Timer: "
634f62bae50SIngo Molnar 		"%lu (%ld)\n", (unsigned long)res, *delta);
635f62bae50SIngo Molnar 	*delta = (long)res;
636f62bae50SIngo Molnar 
637f62bae50SIngo Molnar 	/* Correct the tsc counter value */
638f62bae50SIngo Molnar 	if (cpu_has_tsc) {
639f62bae50SIngo Molnar 		res = (((u64)(*deltatsc)) * pm_100ms);
640f62bae50SIngo Molnar 		do_div(res, deltapm);
641f62bae50SIngo Molnar 		apic_printk(APIC_VERBOSE, "TSC delta adjusted to "
642f62bae50SIngo Molnar 					  "PM-Timer: %lu (%ld)\n",
643f62bae50SIngo Molnar 					(unsigned long)res, *deltatsc);
644f62bae50SIngo Molnar 		*deltatsc = (long)res;
645f62bae50SIngo Molnar 	}
646f62bae50SIngo Molnar 
647f62bae50SIngo Molnar 	return 0;
648f62bae50SIngo Molnar }
649f62bae50SIngo Molnar 
650f62bae50SIngo Molnar static int __init calibrate_APIC_clock(void)
651f62bae50SIngo Molnar {
652f62bae50SIngo Molnar 	struct clock_event_device *levt = &__get_cpu_var(lapic_events);
653f62bae50SIngo Molnar 	void (*real_handler)(struct clock_event_device *dev);
654f62bae50SIngo Molnar 	unsigned long deltaj;
655f62bae50SIngo Molnar 	long delta, deltatsc;
656f62bae50SIngo Molnar 	int pm_referenced = 0;
657f62bae50SIngo Molnar 
6581ade93efSJacob Pan 	/**
6591ade93efSJacob Pan 	 * check if lapic timer has already been calibrated by platform
6601ade93efSJacob Pan 	 * specific routine, such as tsc calibration code. if so, we just fill
6611ade93efSJacob Pan 	 * in the clockevent structure and return.
6621ade93efSJacob Pan 	 */
6631ade93efSJacob Pan 
6641ade93efSJacob Pan 	if (lapic_timer_frequency) {
6651ade93efSJacob Pan 		apic_printk(APIC_VERBOSE, "lapic timer already calibrated %d\n",
6661ade93efSJacob Pan 				lapic_timer_frequency);
6671ade93efSJacob Pan 		lapic_clockevent.mult = div_sc(lapic_timer_frequency/APIC_DIVISOR,
6681ade93efSJacob Pan 					TICK_NSEC, lapic_clockevent.shift);
6691ade93efSJacob Pan 		lapic_clockevent.max_delta_ns =
6701ade93efSJacob Pan 			clockevent_delta2ns(0x7FFFFF, &lapic_clockevent);
6711ade93efSJacob Pan 		lapic_clockevent.min_delta_ns =
6721ade93efSJacob Pan 			clockevent_delta2ns(0xF, &lapic_clockevent);
6731ade93efSJacob Pan 		lapic_clockevent.features &= ~CLOCK_EVT_FEAT_DUMMY;
6741ade93efSJacob Pan 		return 0;
6751ade93efSJacob Pan 	}
6761ade93efSJacob Pan 
677f62bae50SIngo Molnar 	local_irq_disable();
678f62bae50SIngo Molnar 
679f62bae50SIngo Molnar 	/* Replace the global interrupt handler */
680f62bae50SIngo Molnar 	real_handler = global_clock_event->event_handler;
681f62bae50SIngo Molnar 	global_clock_event->event_handler = lapic_cal_handler;
682f62bae50SIngo Molnar 
683f62bae50SIngo Molnar 	/*
684f62bae50SIngo Molnar 	 * Setup the APIC counter to maximum. There is no way the lapic
685f62bae50SIngo Molnar 	 * can underflow in the 100ms detection time frame
686f62bae50SIngo Molnar 	 */
687f62bae50SIngo Molnar 	__setup_APIC_LVTT(0xffffffff, 0, 0);
688f62bae50SIngo Molnar 
689f62bae50SIngo Molnar 	/* Let the interrupts run */
690f62bae50SIngo Molnar 	local_irq_enable();
691f62bae50SIngo Molnar 
692f62bae50SIngo Molnar 	while (lapic_cal_loops <= LAPIC_CAL_LOOPS)
693f62bae50SIngo Molnar 		cpu_relax();
694f62bae50SIngo Molnar 
695f62bae50SIngo Molnar 	local_irq_disable();
696f62bae50SIngo Molnar 
697f62bae50SIngo Molnar 	/* Restore the real event handler */
698f62bae50SIngo Molnar 	global_clock_event->event_handler = real_handler;
699f62bae50SIngo Molnar 
700f62bae50SIngo Molnar 	/* Build delta t1-t2 as apic timer counts down */
701f62bae50SIngo Molnar 	delta = lapic_cal_t1 - lapic_cal_t2;
702f62bae50SIngo Molnar 	apic_printk(APIC_VERBOSE, "... lapic delta = %ld\n", delta);
703f62bae50SIngo Molnar 
704f62bae50SIngo Molnar 	deltatsc = (long)(lapic_cal_tsc2 - lapic_cal_tsc1);
705f62bae50SIngo Molnar 
706f62bae50SIngo Molnar 	/* we trust the PM based calibration if possible */
707f62bae50SIngo Molnar 	pm_referenced = !calibrate_by_pmtimer(lapic_cal_pm2 - lapic_cal_pm1,
708f62bae50SIngo Molnar 					&delta, &deltatsc);
709f62bae50SIngo Molnar 
710f62bae50SIngo Molnar 	/* Calculate the scaled math multiplication factor */
711f62bae50SIngo Molnar 	lapic_clockevent.mult = div_sc(delta, TICK_NSEC * LAPIC_CAL_LOOPS,
712f62bae50SIngo Molnar 				       lapic_clockevent.shift);
713f62bae50SIngo Molnar 	lapic_clockevent.max_delta_ns =
7144aed89d6SPierre Tardy 		clockevent_delta2ns(0x7FFFFFFF, &lapic_clockevent);
715f62bae50SIngo Molnar 	lapic_clockevent.min_delta_ns =
716f62bae50SIngo Molnar 		clockevent_delta2ns(0xF, &lapic_clockevent);
717f62bae50SIngo Molnar 
7181ade93efSJacob Pan 	lapic_timer_frequency = (delta * APIC_DIVISOR) / LAPIC_CAL_LOOPS;
719f62bae50SIngo Molnar 
720f62bae50SIngo Molnar 	apic_printk(APIC_VERBOSE, "..... delta %ld\n", delta);
721411462f6SThomas Gleixner 	apic_printk(APIC_VERBOSE, "..... mult: %u\n", lapic_clockevent.mult);
722f62bae50SIngo Molnar 	apic_printk(APIC_VERBOSE, "..... calibration result: %u\n",
7231ade93efSJacob Pan 		    lapic_timer_frequency);
724f62bae50SIngo Molnar 
725f62bae50SIngo Molnar 	if (cpu_has_tsc) {
726f62bae50SIngo Molnar 		apic_printk(APIC_VERBOSE, "..... CPU clock speed is "
727f62bae50SIngo Molnar 			    "%ld.%04ld MHz.\n",
728f62bae50SIngo Molnar 			    (deltatsc / LAPIC_CAL_LOOPS) / (1000000 / HZ),
729f62bae50SIngo Molnar 			    (deltatsc / LAPIC_CAL_LOOPS) % (1000000 / HZ));
730f62bae50SIngo Molnar 	}
731f62bae50SIngo Molnar 
732f62bae50SIngo Molnar 	apic_printk(APIC_VERBOSE, "..... host bus clock speed is "
733f62bae50SIngo Molnar 		    "%u.%04u MHz.\n",
7341ade93efSJacob Pan 		    lapic_timer_frequency / (1000000 / HZ),
7351ade93efSJacob Pan 		    lapic_timer_frequency % (1000000 / HZ));
736f62bae50SIngo Molnar 
737f62bae50SIngo Molnar 	/*
738f62bae50SIngo Molnar 	 * Do a sanity check on the APIC calibration result
739f62bae50SIngo Molnar 	 */
7401ade93efSJacob Pan 	if (lapic_timer_frequency < (1000000 / HZ)) {
741f62bae50SIngo Molnar 		local_irq_enable();
742f62bae50SIngo Molnar 		pr_warning("APIC frequency too slow, disabling apic timer\n");
743f62bae50SIngo Molnar 		return -1;
744f62bae50SIngo Molnar 	}
745f62bae50SIngo Molnar 
746f62bae50SIngo Molnar 	levt->features &= ~CLOCK_EVT_FEAT_DUMMY;
747f62bae50SIngo Molnar 
748f62bae50SIngo Molnar 	/*
749f62bae50SIngo Molnar 	 * PM timer calibration failed or not turned on
750f62bae50SIngo Molnar 	 * so lets try APIC timer based calibration
751f62bae50SIngo Molnar 	 */
752f62bae50SIngo Molnar 	if (!pm_referenced) {
753f62bae50SIngo Molnar 		apic_printk(APIC_VERBOSE, "... verify APIC timer\n");
754f62bae50SIngo Molnar 
755f62bae50SIngo Molnar 		/*
756f62bae50SIngo Molnar 		 * Setup the apic timer manually
757f62bae50SIngo Molnar 		 */
758f62bae50SIngo Molnar 		levt->event_handler = lapic_cal_handler;
759f62bae50SIngo Molnar 		lapic_timer_setup(CLOCK_EVT_MODE_PERIODIC, levt);
760f62bae50SIngo Molnar 		lapic_cal_loops = -1;
761f62bae50SIngo Molnar 
762f62bae50SIngo Molnar 		/* Let the interrupts run */
763f62bae50SIngo Molnar 		local_irq_enable();
764f62bae50SIngo Molnar 
765f62bae50SIngo Molnar 		while (lapic_cal_loops <= LAPIC_CAL_LOOPS)
766f62bae50SIngo Molnar 			cpu_relax();
767f62bae50SIngo Molnar 
768f62bae50SIngo Molnar 		/* Stop the lapic timer */
769f62bae50SIngo Molnar 		lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, levt);
770f62bae50SIngo Molnar 
771f62bae50SIngo Molnar 		/* Jiffies delta */
772f62bae50SIngo Molnar 		deltaj = lapic_cal_j2 - lapic_cal_j1;
773f62bae50SIngo Molnar 		apic_printk(APIC_VERBOSE, "... jiffies delta = %lu\n", deltaj);
774f62bae50SIngo Molnar 
775f62bae50SIngo Molnar 		/* Check, if the jiffies result is consistent */
776f62bae50SIngo Molnar 		if (deltaj >= LAPIC_CAL_LOOPS-2 && deltaj <= LAPIC_CAL_LOOPS+2)
777f62bae50SIngo Molnar 			apic_printk(APIC_VERBOSE, "... jiffies result ok\n");
778f62bae50SIngo Molnar 		else
779f62bae50SIngo Molnar 			levt->features |= CLOCK_EVT_FEAT_DUMMY;
780f62bae50SIngo Molnar 	} else
781f62bae50SIngo Molnar 		local_irq_enable();
782f62bae50SIngo Molnar 
783f62bae50SIngo Molnar 	if (levt->features & CLOCK_EVT_FEAT_DUMMY) {
784f62bae50SIngo Molnar 		pr_warning("APIC timer disabled due to verification failure\n");
785f62bae50SIngo Molnar 			return -1;
786f62bae50SIngo Molnar 	}
787f62bae50SIngo Molnar 
788f62bae50SIngo Molnar 	return 0;
789f62bae50SIngo Molnar }
790f62bae50SIngo Molnar 
791f62bae50SIngo Molnar /*
792f62bae50SIngo Molnar  * Setup the boot APIC
793f62bae50SIngo Molnar  *
794f62bae50SIngo Molnar  * Calibrate and verify the result.
795f62bae50SIngo Molnar  */
796f62bae50SIngo Molnar void __init setup_boot_APIC_clock(void)
797f62bae50SIngo Molnar {
798f62bae50SIngo Molnar 	/*
799f62bae50SIngo Molnar 	 * The local apic timer can be disabled via the kernel
800f62bae50SIngo Molnar 	 * commandline or from the CPU detection code. Register the lapic
801f62bae50SIngo Molnar 	 * timer as a dummy clock event source on SMP systems, so the
802f62bae50SIngo Molnar 	 * broadcast mechanism is used. On UP systems simply ignore it.
803f62bae50SIngo Molnar 	 */
804f62bae50SIngo Molnar 	if (disable_apic_timer) {
805f62bae50SIngo Molnar 		pr_info("Disabling APIC timer\n");
806f62bae50SIngo Molnar 		/* No broadcast on UP ! */
807f62bae50SIngo Molnar 		if (num_possible_cpus() > 1) {
808f62bae50SIngo Molnar 			lapic_clockevent.mult = 1;
809f62bae50SIngo Molnar 			setup_APIC_timer();
810f62bae50SIngo Molnar 		}
811f62bae50SIngo Molnar 		return;
812f62bae50SIngo Molnar 	}
813f62bae50SIngo Molnar 
814f62bae50SIngo Molnar 	apic_printk(APIC_VERBOSE, "Using local APIC timer interrupts.\n"
815f62bae50SIngo Molnar 		    "calibrating APIC timer ...\n");
816f62bae50SIngo Molnar 
817f62bae50SIngo Molnar 	if (calibrate_APIC_clock()) {
818f62bae50SIngo Molnar 		/* No broadcast on UP ! */
819f62bae50SIngo Molnar 		if (num_possible_cpus() > 1)
820f62bae50SIngo Molnar 			setup_APIC_timer();
821f62bae50SIngo Molnar 		return;
822f62bae50SIngo Molnar 	}
823f62bae50SIngo Molnar 
824f62bae50SIngo Molnar 	/*
825f62bae50SIngo Molnar 	 * If nmi_watchdog is set to IO_APIC, we need the
826f62bae50SIngo Molnar 	 * PIT/HPET going.  Otherwise register lapic as a dummy
827f62bae50SIngo Molnar 	 * device.
828f62bae50SIngo Molnar 	 */
829f62bae50SIngo Molnar 	lapic_clockevent.features &= ~CLOCK_EVT_FEAT_DUMMY;
830f62bae50SIngo Molnar 
831f62bae50SIngo Molnar 	/* Setup the lapic or request the broadcast */
832f62bae50SIngo Molnar 	setup_APIC_timer();
833f62bae50SIngo Molnar }
834f62bae50SIngo Molnar 
835f62bae50SIngo Molnar void __cpuinit setup_secondary_APIC_clock(void)
836f62bae50SIngo Molnar {
837f62bae50SIngo Molnar 	setup_APIC_timer();
838f62bae50SIngo Molnar }
839f62bae50SIngo Molnar 
840f62bae50SIngo Molnar /*
841f62bae50SIngo Molnar  * The guts of the apic timer interrupt
842f62bae50SIngo Molnar  */
843f62bae50SIngo Molnar static void local_apic_timer_interrupt(void)
844f62bae50SIngo Molnar {
845f62bae50SIngo Molnar 	int cpu = smp_processor_id();
846f62bae50SIngo Molnar 	struct clock_event_device *evt = &per_cpu(lapic_events, cpu);
847f62bae50SIngo Molnar 
848f62bae50SIngo Molnar 	/*
849f62bae50SIngo Molnar 	 * Normally we should not be here till LAPIC has been initialized but
850f62bae50SIngo Molnar 	 * in some cases like kdump, its possible that there is a pending LAPIC
851f62bae50SIngo Molnar 	 * timer interrupt from previous kernel's context and is delivered in
852f62bae50SIngo Molnar 	 * new kernel the moment interrupts are enabled.
853f62bae50SIngo Molnar 	 *
854f62bae50SIngo Molnar 	 * Interrupts are enabled early and LAPIC is setup much later, hence
855f62bae50SIngo Molnar 	 * its possible that when we get here evt->event_handler is NULL.
856f62bae50SIngo Molnar 	 * Check for event_handler being NULL and discard the interrupt as
857f62bae50SIngo Molnar 	 * spurious.
858f62bae50SIngo Molnar 	 */
859f62bae50SIngo Molnar 	if (!evt->event_handler) {
860f62bae50SIngo Molnar 		pr_warning("Spurious LAPIC timer interrupt on cpu %d\n", cpu);
861f62bae50SIngo Molnar 		/* Switch it off */
862f62bae50SIngo Molnar 		lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, evt);
863f62bae50SIngo Molnar 		return;
864f62bae50SIngo Molnar 	}
865f62bae50SIngo Molnar 
866f62bae50SIngo Molnar 	/*
867f62bae50SIngo Molnar 	 * the NMI deadlock-detector uses this.
868f62bae50SIngo Molnar 	 */
869f62bae50SIngo Molnar 	inc_irq_stat(apic_timer_irqs);
870f62bae50SIngo Molnar 
871f62bae50SIngo Molnar 	evt->event_handler(evt);
872f62bae50SIngo Molnar }
873f62bae50SIngo Molnar 
874f62bae50SIngo Molnar /*
875f62bae50SIngo Molnar  * Local APIC timer interrupt. This is the most natural way for doing
876f62bae50SIngo Molnar  * local interrupts, but local timer interrupts can be emulated by
877f62bae50SIngo Molnar  * broadcast interrupts too. [in case the hw doesn't support APIC timers]
878f62bae50SIngo Molnar  *
879f62bae50SIngo Molnar  * [ if a single-CPU system runs an SMP kernel then we call the local
880f62bae50SIngo Molnar  *   interrupt as well. Thus we cannot inline the local irq ... ]
881f62bae50SIngo Molnar  */
882f62bae50SIngo Molnar void __irq_entry smp_apic_timer_interrupt(struct pt_regs *regs)
883f62bae50SIngo Molnar {
884f62bae50SIngo Molnar 	struct pt_regs *old_regs = set_irq_regs(regs);
885f62bae50SIngo Molnar 
886f62bae50SIngo Molnar 	/*
887f62bae50SIngo Molnar 	 * NOTE! We'd better ACK the irq immediately,
888f62bae50SIngo Molnar 	 * because timer handling can be slow.
889f62bae50SIngo Molnar 	 */
890f62bae50SIngo Molnar 	ack_APIC_irq();
891f62bae50SIngo Molnar 	/*
892f62bae50SIngo Molnar 	 * update_process_times() expects us to have done irq_enter().
893f62bae50SIngo Molnar 	 * Besides, if we don't timer interrupts ignore the global
894f62bae50SIngo Molnar 	 * interrupt lock, which is the WrongThing (tm) to do.
895f62bae50SIngo Molnar 	 */
896f62bae50SIngo Molnar 	irq_enter();
89798ad1cc1SFrederic Weisbecker 	exit_idle();
898f62bae50SIngo Molnar 	local_apic_timer_interrupt();
899f62bae50SIngo Molnar 	irq_exit();
900f62bae50SIngo Molnar 
901f62bae50SIngo Molnar 	set_irq_regs(old_regs);
902f62bae50SIngo Molnar }
903f62bae50SIngo Molnar 
904f62bae50SIngo Molnar int setup_profiling_timer(unsigned int multiplier)
905f62bae50SIngo Molnar {
906f62bae50SIngo Molnar 	return -EINVAL;
907f62bae50SIngo Molnar }
908f62bae50SIngo Molnar 
909f62bae50SIngo Molnar /*
910f62bae50SIngo Molnar  * Local APIC start and shutdown
911f62bae50SIngo Molnar  */
912f62bae50SIngo Molnar 
913f62bae50SIngo Molnar /**
914f62bae50SIngo Molnar  * clear_local_APIC - shutdown the local APIC
915f62bae50SIngo Molnar  *
916f62bae50SIngo Molnar  * This is called, when a CPU is disabled and before rebooting, so the state of
917f62bae50SIngo Molnar  * the local APIC has no dangling leftovers. Also used to cleanout any BIOS
918f62bae50SIngo Molnar  * leftovers during boot.
919f62bae50SIngo Molnar  */
920f62bae50SIngo Molnar void clear_local_APIC(void)
921f62bae50SIngo Molnar {
922f62bae50SIngo Molnar 	int maxlvt;
923f62bae50SIngo Molnar 	u32 v;
924f62bae50SIngo Molnar 
925f62bae50SIngo Molnar 	/* APIC hasn't been mapped yet */
926fc1edaf9SSuresh Siddha 	if (!x2apic_mode && !apic_phys)
927f62bae50SIngo Molnar 		return;
928f62bae50SIngo Molnar 
929f62bae50SIngo Molnar 	maxlvt = lapic_get_maxlvt();
930f62bae50SIngo Molnar 	/*
931f62bae50SIngo Molnar 	 * Masking an LVT entry can trigger a local APIC error
932f62bae50SIngo Molnar 	 * if the vector is zero. Mask LVTERR first to prevent this.
933f62bae50SIngo Molnar 	 */
934f62bae50SIngo Molnar 	if (maxlvt >= 3) {
935f62bae50SIngo Molnar 		v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
936f62bae50SIngo Molnar 		apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
937f62bae50SIngo Molnar 	}
938f62bae50SIngo Molnar 	/*
939f62bae50SIngo Molnar 	 * Careful: we have to set masks only first to deassert
940f62bae50SIngo Molnar 	 * any level-triggered sources.
941f62bae50SIngo Molnar 	 */
942f62bae50SIngo Molnar 	v = apic_read(APIC_LVTT);
943f62bae50SIngo Molnar 	apic_write(APIC_LVTT, v | APIC_LVT_MASKED);
944f62bae50SIngo Molnar 	v = apic_read(APIC_LVT0);
945f62bae50SIngo Molnar 	apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
946f62bae50SIngo Molnar 	v = apic_read(APIC_LVT1);
947f62bae50SIngo Molnar 	apic_write(APIC_LVT1, v | APIC_LVT_MASKED);
948f62bae50SIngo Molnar 	if (maxlvt >= 4) {
949f62bae50SIngo Molnar 		v = apic_read(APIC_LVTPC);
950f62bae50SIngo Molnar 		apic_write(APIC_LVTPC, v | APIC_LVT_MASKED);
951f62bae50SIngo Molnar 	}
952f62bae50SIngo Molnar 
953f62bae50SIngo Molnar 	/* lets not touch this if we didn't frob it */
9544efc0670SAndi Kleen #ifdef CONFIG_X86_THERMAL_VECTOR
955f62bae50SIngo Molnar 	if (maxlvt >= 5) {
956f62bae50SIngo Molnar 		v = apic_read(APIC_LVTTHMR);
957f62bae50SIngo Molnar 		apic_write(APIC_LVTTHMR, v | APIC_LVT_MASKED);
958f62bae50SIngo Molnar 	}
959f62bae50SIngo Molnar #endif
960638bee71SH. Peter Anvin #ifdef CONFIG_X86_MCE_INTEL
961638bee71SH. Peter Anvin 	if (maxlvt >= 6) {
962638bee71SH. Peter Anvin 		v = apic_read(APIC_LVTCMCI);
963638bee71SH. Peter Anvin 		if (!(v & APIC_LVT_MASKED))
964638bee71SH. Peter Anvin 			apic_write(APIC_LVTCMCI, v | APIC_LVT_MASKED);
965638bee71SH. Peter Anvin 	}
966638bee71SH. Peter Anvin #endif
967638bee71SH. Peter Anvin 
968f62bae50SIngo Molnar 	/*
969f62bae50SIngo Molnar 	 * Clean APIC state for other OSs:
970f62bae50SIngo Molnar 	 */
971f62bae50SIngo Molnar 	apic_write(APIC_LVTT, APIC_LVT_MASKED);
972f62bae50SIngo Molnar 	apic_write(APIC_LVT0, APIC_LVT_MASKED);
973f62bae50SIngo Molnar 	apic_write(APIC_LVT1, APIC_LVT_MASKED);
974f62bae50SIngo Molnar 	if (maxlvt >= 3)
975f62bae50SIngo Molnar 		apic_write(APIC_LVTERR, APIC_LVT_MASKED);
976f62bae50SIngo Molnar 	if (maxlvt >= 4)
977f62bae50SIngo Molnar 		apic_write(APIC_LVTPC, APIC_LVT_MASKED);
978f62bae50SIngo Molnar 
979f62bae50SIngo Molnar 	/* Integrated APIC (!82489DX) ? */
980f62bae50SIngo Molnar 	if (lapic_is_integrated()) {
981f62bae50SIngo Molnar 		if (maxlvt > 3)
982f62bae50SIngo Molnar 			/* Clear ESR due to Pentium errata 3AP and 11AP */
983f62bae50SIngo Molnar 			apic_write(APIC_ESR, 0);
984f62bae50SIngo Molnar 		apic_read(APIC_ESR);
985f62bae50SIngo Molnar 	}
986f62bae50SIngo Molnar }
987f62bae50SIngo Molnar 
988f62bae50SIngo Molnar /**
989f62bae50SIngo Molnar  * disable_local_APIC - clear and disable the local APIC
990f62bae50SIngo Molnar  */
991f62bae50SIngo Molnar void disable_local_APIC(void)
992f62bae50SIngo Molnar {
993f62bae50SIngo Molnar 	unsigned int value;
994f62bae50SIngo Molnar 
995f62bae50SIngo Molnar 	/* APIC hasn't been mapped yet */
996fd19dce7SYinghai Lu 	if (!x2apic_mode && !apic_phys)
997f62bae50SIngo Molnar 		return;
998f62bae50SIngo Molnar 
999f62bae50SIngo Molnar 	clear_local_APIC();
1000f62bae50SIngo Molnar 
1001f62bae50SIngo Molnar 	/*
1002f62bae50SIngo Molnar 	 * Disable APIC (implies clearing of registers
1003f62bae50SIngo Molnar 	 * for 82489DX!).
1004f62bae50SIngo Molnar 	 */
1005f62bae50SIngo Molnar 	value = apic_read(APIC_SPIV);
1006f62bae50SIngo Molnar 	value &= ~APIC_SPIV_APIC_ENABLED;
1007f62bae50SIngo Molnar 	apic_write(APIC_SPIV, value);
1008f62bae50SIngo Molnar 
1009f62bae50SIngo Molnar #ifdef CONFIG_X86_32
1010f62bae50SIngo Molnar 	/*
1011f62bae50SIngo Molnar 	 * When LAPIC was disabled by the BIOS and enabled by the kernel,
1012f62bae50SIngo Molnar 	 * restore the disabled state.
1013f62bae50SIngo Molnar 	 */
1014f62bae50SIngo Molnar 	if (enabled_via_apicbase) {
1015f62bae50SIngo Molnar 		unsigned int l, h;
1016f62bae50SIngo Molnar 
1017f62bae50SIngo Molnar 		rdmsr(MSR_IA32_APICBASE, l, h);
1018f62bae50SIngo Molnar 		l &= ~MSR_IA32_APICBASE_ENABLE;
1019f62bae50SIngo Molnar 		wrmsr(MSR_IA32_APICBASE, l, h);
1020f62bae50SIngo Molnar 	}
1021f62bae50SIngo Molnar #endif
1022f62bae50SIngo Molnar }
1023f62bae50SIngo Molnar 
1024f62bae50SIngo Molnar /*
1025f62bae50SIngo Molnar  * If Linux enabled the LAPIC against the BIOS default disable it down before
1026f62bae50SIngo Molnar  * re-entering the BIOS on shutdown.  Otherwise the BIOS may get confused and
1027f62bae50SIngo Molnar  * not power-off.  Additionally clear all LVT entries before disable_local_APIC
1028f62bae50SIngo Molnar  * for the case where Linux didn't enable the LAPIC.
1029f62bae50SIngo Molnar  */
1030f62bae50SIngo Molnar void lapic_shutdown(void)
1031f62bae50SIngo Molnar {
1032f62bae50SIngo Molnar 	unsigned long flags;
1033f62bae50SIngo Molnar 
10348312136fSCyrill Gorcunov 	if (!cpu_has_apic && !apic_from_smp_config())
1035f62bae50SIngo Molnar 		return;
1036f62bae50SIngo Molnar 
1037f62bae50SIngo Molnar 	local_irq_save(flags);
1038f62bae50SIngo Molnar 
1039f62bae50SIngo Molnar #ifdef CONFIG_X86_32
1040f62bae50SIngo Molnar 	if (!enabled_via_apicbase)
1041f62bae50SIngo Molnar 		clear_local_APIC();
1042f62bae50SIngo Molnar 	else
1043f62bae50SIngo Molnar #endif
1044f62bae50SIngo Molnar 		disable_local_APIC();
1045f62bae50SIngo Molnar 
1046f62bae50SIngo Molnar 
1047f62bae50SIngo Molnar 	local_irq_restore(flags);
1048f62bae50SIngo Molnar }
1049f62bae50SIngo Molnar 
1050f62bae50SIngo Molnar /*
1051f62bae50SIngo Molnar  * This is to verify that we're looking at a real local APIC.
1052f62bae50SIngo Molnar  * Check these against your board if the CPUs aren't getting
1053f62bae50SIngo Molnar  * started for no apparent reason.
1054f62bae50SIngo Molnar  */
1055f62bae50SIngo Molnar int __init verify_local_APIC(void)
1056f62bae50SIngo Molnar {
1057f62bae50SIngo Molnar 	unsigned int reg0, reg1;
1058f62bae50SIngo Molnar 
1059f62bae50SIngo Molnar 	/*
1060f62bae50SIngo Molnar 	 * The version register is read-only in a real APIC.
1061f62bae50SIngo Molnar 	 */
1062f62bae50SIngo Molnar 	reg0 = apic_read(APIC_LVR);
1063f62bae50SIngo Molnar 	apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
1064f62bae50SIngo Molnar 	apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
1065f62bae50SIngo Molnar 	reg1 = apic_read(APIC_LVR);
1066f62bae50SIngo Molnar 	apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
1067f62bae50SIngo Molnar 
1068f62bae50SIngo Molnar 	/*
1069f62bae50SIngo Molnar 	 * The two version reads above should print the same
1070f62bae50SIngo Molnar 	 * numbers.  If the second one is different, then we
1071f62bae50SIngo Molnar 	 * poke at a non-APIC.
1072f62bae50SIngo Molnar 	 */
1073f62bae50SIngo Molnar 	if (reg1 != reg0)
1074f62bae50SIngo Molnar 		return 0;
1075f62bae50SIngo Molnar 
1076f62bae50SIngo Molnar 	/*
1077f62bae50SIngo Molnar 	 * Check if the version looks reasonably.
1078f62bae50SIngo Molnar 	 */
1079f62bae50SIngo Molnar 	reg1 = GET_APIC_VERSION(reg0);
1080f62bae50SIngo Molnar 	if (reg1 == 0x00 || reg1 == 0xff)
1081f62bae50SIngo Molnar 		return 0;
1082f62bae50SIngo Molnar 	reg1 = lapic_get_maxlvt();
1083f62bae50SIngo Molnar 	if (reg1 < 0x02 || reg1 == 0xff)
1084f62bae50SIngo Molnar 		return 0;
1085f62bae50SIngo Molnar 
1086f62bae50SIngo Molnar 	/*
1087f62bae50SIngo Molnar 	 * The ID register is read/write in a real APIC.
1088f62bae50SIngo Molnar 	 */
1089f62bae50SIngo Molnar 	reg0 = apic_read(APIC_ID);
1090f62bae50SIngo Molnar 	apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
1091f62bae50SIngo Molnar 	apic_write(APIC_ID, reg0 ^ apic->apic_id_mask);
1092f62bae50SIngo Molnar 	reg1 = apic_read(APIC_ID);
1093f62bae50SIngo Molnar 	apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg1);
1094f62bae50SIngo Molnar 	apic_write(APIC_ID, reg0);
1095f62bae50SIngo Molnar 	if (reg1 != (reg0 ^ apic->apic_id_mask))
1096f62bae50SIngo Molnar 		return 0;
1097f62bae50SIngo Molnar 
1098f62bae50SIngo Molnar 	/*
1099f62bae50SIngo Molnar 	 * The next two are just to see if we have sane values.
1100f62bae50SIngo Molnar 	 * They're only really relevant if we're in Virtual Wire
1101f62bae50SIngo Molnar 	 * compatibility mode, but most boxes are anymore.
1102f62bae50SIngo Molnar 	 */
1103f62bae50SIngo Molnar 	reg0 = apic_read(APIC_LVT0);
1104f62bae50SIngo Molnar 	apic_printk(APIC_DEBUG, "Getting LVT0: %x\n", reg0);
1105f62bae50SIngo Molnar 	reg1 = apic_read(APIC_LVT1);
1106f62bae50SIngo Molnar 	apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
1107f62bae50SIngo Molnar 
1108f62bae50SIngo Molnar 	return 1;
1109f62bae50SIngo Molnar }
1110f62bae50SIngo Molnar 
1111f62bae50SIngo Molnar /**
1112f62bae50SIngo Molnar  * sync_Arb_IDs - synchronize APIC bus arbitration IDs
1113f62bae50SIngo Molnar  */
1114f62bae50SIngo Molnar void __init sync_Arb_IDs(void)
1115f62bae50SIngo Molnar {
1116f62bae50SIngo Molnar 	/*
1117f62bae50SIngo Molnar 	 * Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 And not
1118f62bae50SIngo Molnar 	 * needed on AMD.
1119f62bae50SIngo Molnar 	 */
1120f62bae50SIngo Molnar 	if (modern_apic() || boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
1121f62bae50SIngo Molnar 		return;
1122f62bae50SIngo Molnar 
1123f62bae50SIngo Molnar 	/*
1124f62bae50SIngo Molnar 	 * Wait for idle.
1125f62bae50SIngo Molnar 	 */
1126f62bae50SIngo Molnar 	apic_wait_icr_idle();
1127f62bae50SIngo Molnar 
1128f62bae50SIngo Molnar 	apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
1129f62bae50SIngo Molnar 	apic_write(APIC_ICR, APIC_DEST_ALLINC |
1130f62bae50SIngo Molnar 			APIC_INT_LEVELTRIG | APIC_DM_INIT);
1131f62bae50SIngo Molnar }
1132f62bae50SIngo Molnar 
1133f62bae50SIngo Molnar /*
1134f62bae50SIngo Molnar  * An initial setup of the virtual wire mode.
1135f62bae50SIngo Molnar  */
1136f62bae50SIngo Molnar void __init init_bsp_APIC(void)
1137f62bae50SIngo Molnar {
1138f62bae50SIngo Molnar 	unsigned int value;
1139f62bae50SIngo Molnar 
1140f62bae50SIngo Molnar 	/*
1141f62bae50SIngo Molnar 	 * Don't do the setup now if we have a SMP BIOS as the
1142f62bae50SIngo Molnar 	 * through-I/O-APIC virtual wire mode might be active.
1143f62bae50SIngo Molnar 	 */
1144f62bae50SIngo Molnar 	if (smp_found_config || !cpu_has_apic)
1145f62bae50SIngo Molnar 		return;
1146f62bae50SIngo Molnar 
1147f62bae50SIngo Molnar 	/*
1148f62bae50SIngo Molnar 	 * Do not trust the local APIC being empty at bootup.
1149f62bae50SIngo Molnar 	 */
1150f62bae50SIngo Molnar 	clear_local_APIC();
1151f62bae50SIngo Molnar 
1152f62bae50SIngo Molnar 	/*
1153f62bae50SIngo Molnar 	 * Enable APIC.
1154f62bae50SIngo Molnar 	 */
1155f62bae50SIngo Molnar 	value = apic_read(APIC_SPIV);
1156f62bae50SIngo Molnar 	value &= ~APIC_VECTOR_MASK;
1157f62bae50SIngo Molnar 	value |= APIC_SPIV_APIC_ENABLED;
1158f62bae50SIngo Molnar 
1159f62bae50SIngo Molnar #ifdef CONFIG_X86_32
1160f62bae50SIngo Molnar 	/* This bit is reserved on P4/Xeon and should be cleared */
1161f62bae50SIngo Molnar 	if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) &&
1162f62bae50SIngo Molnar 	    (boot_cpu_data.x86 == 15))
1163f62bae50SIngo Molnar 		value &= ~APIC_SPIV_FOCUS_DISABLED;
1164f62bae50SIngo Molnar 	else
1165f62bae50SIngo Molnar #endif
1166f62bae50SIngo Molnar 		value |= APIC_SPIV_FOCUS_DISABLED;
1167f62bae50SIngo Molnar 	value |= SPURIOUS_APIC_VECTOR;
1168f62bae50SIngo Molnar 	apic_write(APIC_SPIV, value);
1169f62bae50SIngo Molnar 
1170f62bae50SIngo Molnar 	/*
1171f62bae50SIngo Molnar 	 * Set up the virtual wire mode.
1172f62bae50SIngo Molnar 	 */
1173f62bae50SIngo Molnar 	apic_write(APIC_LVT0, APIC_DM_EXTINT);
1174f62bae50SIngo Molnar 	value = APIC_DM_NMI;
1175f62bae50SIngo Molnar 	if (!lapic_is_integrated())		/* 82489DX */
1176f62bae50SIngo Molnar 		value |= APIC_LVT_LEVEL_TRIGGER;
1177f62bae50SIngo Molnar 	apic_write(APIC_LVT1, value);
1178f62bae50SIngo Molnar }
1179f62bae50SIngo Molnar 
1180f62bae50SIngo Molnar static void __cpuinit lapic_setup_esr(void)
1181f62bae50SIngo Molnar {
1182f62bae50SIngo Molnar 	unsigned int oldvalue, value, maxlvt;
1183f62bae50SIngo Molnar 
1184f62bae50SIngo Molnar 	if (!lapic_is_integrated()) {
1185f62bae50SIngo Molnar 		pr_info("No ESR for 82489DX.\n");
1186f62bae50SIngo Molnar 		return;
1187f62bae50SIngo Molnar 	}
1188f62bae50SIngo Molnar 
1189f62bae50SIngo Molnar 	if (apic->disable_esr) {
1190f62bae50SIngo Molnar 		/*
1191f62bae50SIngo Molnar 		 * Something untraceable is creating bad interrupts on
1192f62bae50SIngo Molnar 		 * secondary quads ... for the moment, just leave the
1193f62bae50SIngo Molnar 		 * ESR disabled - we can't do anything useful with the
1194f62bae50SIngo Molnar 		 * errors anyway - mbligh
1195f62bae50SIngo Molnar 		 */
1196f62bae50SIngo Molnar 		pr_info("Leaving ESR disabled.\n");
1197f62bae50SIngo Molnar 		return;
1198f62bae50SIngo Molnar 	}
1199f62bae50SIngo Molnar 
1200f62bae50SIngo Molnar 	maxlvt = lapic_get_maxlvt();
1201f62bae50SIngo Molnar 	if (maxlvt > 3)		/* Due to the Pentium erratum 3AP. */
1202f62bae50SIngo Molnar 		apic_write(APIC_ESR, 0);
1203f62bae50SIngo Molnar 	oldvalue = apic_read(APIC_ESR);
1204f62bae50SIngo Molnar 
1205f62bae50SIngo Molnar 	/* enables sending errors */
1206f62bae50SIngo Molnar 	value = ERROR_APIC_VECTOR;
1207f62bae50SIngo Molnar 	apic_write(APIC_LVTERR, value);
1208f62bae50SIngo Molnar 
1209f62bae50SIngo Molnar 	/*
1210f62bae50SIngo Molnar 	 * spec says clear errors after enabling vector.
1211f62bae50SIngo Molnar 	 */
1212f62bae50SIngo Molnar 	if (maxlvt > 3)
1213f62bae50SIngo Molnar 		apic_write(APIC_ESR, 0);
1214f62bae50SIngo Molnar 	value = apic_read(APIC_ESR);
1215f62bae50SIngo Molnar 	if (value != oldvalue)
1216f62bae50SIngo Molnar 		apic_printk(APIC_VERBOSE, "ESR value before enabling "
1217f62bae50SIngo Molnar 			"vector: 0x%08x  after: 0x%08x\n",
1218f62bae50SIngo Molnar 			oldvalue, value);
1219f62bae50SIngo Molnar }
1220f62bae50SIngo Molnar 
1221f62bae50SIngo Molnar /**
1222f62bae50SIngo Molnar  * setup_local_APIC - setup the local APIC
12230aa002feSTejun Heo  *
12240aa002feSTejun Heo  * Used to setup local APIC while initializing BSP or bringin up APs.
12250aa002feSTejun Heo  * Always called with preemption disabled.
1226f62bae50SIngo Molnar  */
1227f62bae50SIngo Molnar void __cpuinit setup_local_APIC(void)
1228f62bae50SIngo Molnar {
12290aa002feSTejun Heo 	int cpu = smp_processor_id();
12308c3ba8d0SKerstin Jonsson 	unsigned int value, queued;
12318c3ba8d0SKerstin Jonsson 	int i, j, acked = 0;
12328c3ba8d0SKerstin Jonsson 	unsigned long long tsc = 0, ntsc;
12338c3ba8d0SKerstin Jonsson 	long long max_loops = cpu_khz;
12348c3ba8d0SKerstin Jonsson 
12358c3ba8d0SKerstin Jonsson 	if (cpu_has_tsc)
12368c3ba8d0SKerstin Jonsson 		rdtscll(tsc);
1237f62bae50SIngo Molnar 
1238f62bae50SIngo Molnar 	if (disable_apic) {
12397167d08eSHenrik Kretzschmar 		disable_ioapic_support();
1240f62bae50SIngo Molnar 		return;
1241f62bae50SIngo Molnar 	}
1242f62bae50SIngo Molnar 
1243f62bae50SIngo Molnar #ifdef CONFIG_X86_32
1244f62bae50SIngo Molnar 	/* Pound the ESR really hard over the head with a big hammer - mbligh */
1245f62bae50SIngo Molnar 	if (lapic_is_integrated() && apic->disable_esr) {
1246f62bae50SIngo Molnar 		apic_write(APIC_ESR, 0);
1247f62bae50SIngo Molnar 		apic_write(APIC_ESR, 0);
1248f62bae50SIngo Molnar 		apic_write(APIC_ESR, 0);
1249f62bae50SIngo Molnar 		apic_write(APIC_ESR, 0);
1250f62bae50SIngo Molnar 	}
1251f62bae50SIngo Molnar #endif
1252cdd6c482SIngo Molnar 	perf_events_lapic_init();
1253f62bae50SIngo Molnar 
1254f62bae50SIngo Molnar 	/*
1255f62bae50SIngo Molnar 	 * Double-check whether this APIC is really registered.
1256f62bae50SIngo Molnar 	 * This is meaningless in clustered apic mode, so we skip it.
1257f62bae50SIngo Molnar 	 */
1258c2777f98SDaniel Walker 	BUG_ON(!apic->apic_id_registered());
1259f62bae50SIngo Molnar 
1260f62bae50SIngo Molnar 	/*
1261f62bae50SIngo Molnar 	 * Intel recommends to set DFR, LDR and TPR before enabling
1262f62bae50SIngo Molnar 	 * an APIC.  See e.g. "AP-388 82489DX User's Manual" (Intel
1263f62bae50SIngo Molnar 	 * document number 292116).  So here it goes...
1264f62bae50SIngo Molnar 	 */
1265f62bae50SIngo Molnar 	apic->init_apic_ldr();
1266f62bae50SIngo Molnar 
12676f802c4bSTejun Heo #ifdef CONFIG_X86_32
12686f802c4bSTejun Heo 	/*
1269acb8bc09STejun Heo 	 * APIC LDR is initialized.  If logical_apicid mapping was
1270acb8bc09STejun Heo 	 * initialized during get_smp_config(), make sure it matches the
1271acb8bc09STejun Heo 	 * actual value.
12726f802c4bSTejun Heo 	 */
1273acb8bc09STejun Heo 	i = early_per_cpu(x86_cpu_to_logical_apicid, cpu);
1274acb8bc09STejun Heo 	WARN_ON(i != BAD_APICID && i != logical_smp_processor_id());
1275acb8bc09STejun Heo 	/* always use the value from LDR */
12766f802c4bSTejun Heo 	early_per_cpu(x86_cpu_to_logical_apicid, cpu) =
12776f802c4bSTejun Heo 		logical_smp_processor_id();
1278c4b90c11STejun Heo 
1279c4b90c11STejun Heo 	/*
1280c4b90c11STejun Heo 	 * Some NUMA implementations (NUMAQ) don't initialize apicid to
1281c4b90c11STejun Heo 	 * node mapping during NUMA init.  Now that logical apicid is
1282c4b90c11STejun Heo 	 * guaranteed to be known, give it another chance.  This is already
1283c4b90c11STejun Heo 	 * a bit too late - percpu allocation has already happened without
1284c4b90c11STejun Heo 	 * proper NUMA affinity.
1285c4b90c11STejun Heo 	 */
128684914ed0STejun Heo 	if (apic->x86_32_numa_cpu_node)
1287c4b90c11STejun Heo 		set_apicid_to_node(early_per_cpu(x86_cpu_to_apicid, cpu),
1288c4b90c11STejun Heo 				   apic->x86_32_numa_cpu_node(cpu));
12896f802c4bSTejun Heo #endif
12906f802c4bSTejun Heo 
1291f62bae50SIngo Molnar 	/*
1292f62bae50SIngo Molnar 	 * Set Task Priority to 'accept all'. We never change this
1293f62bae50SIngo Molnar 	 * later on.
1294f62bae50SIngo Molnar 	 */
1295f62bae50SIngo Molnar 	value = apic_read(APIC_TASKPRI);
1296f62bae50SIngo Molnar 	value &= ~APIC_TPRI_MASK;
1297f62bae50SIngo Molnar 	apic_write(APIC_TASKPRI, value);
1298f62bae50SIngo Molnar 
1299f62bae50SIngo Molnar 	/*
1300f62bae50SIngo Molnar 	 * After a crash, we no longer service the interrupts and a pending
1301f62bae50SIngo Molnar 	 * interrupt from previous kernel might still have ISR bit set.
1302f62bae50SIngo Molnar 	 *
1303f62bae50SIngo Molnar 	 * Most probably by now CPU has serviced that pending interrupt and
1304f62bae50SIngo Molnar 	 * it might not have done the ack_APIC_irq() because it thought,
1305f62bae50SIngo Molnar 	 * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
1306f62bae50SIngo Molnar 	 * does not clear the ISR bit and cpu thinks it has already serivced
1307f62bae50SIngo Molnar 	 * the interrupt. Hence a vector might get locked. It was noticed
1308f62bae50SIngo Molnar 	 * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
1309f62bae50SIngo Molnar 	 */
13108c3ba8d0SKerstin Jonsson 	do {
13118c3ba8d0SKerstin Jonsson 		queued = 0;
13128c3ba8d0SKerstin Jonsson 		for (i = APIC_ISR_NR - 1; i >= 0; i--)
13138c3ba8d0SKerstin Jonsson 			queued |= apic_read(APIC_IRR + i*0x10);
13148c3ba8d0SKerstin Jonsson 
1315f62bae50SIngo Molnar 		for (i = APIC_ISR_NR - 1; i >= 0; i--) {
1316f62bae50SIngo Molnar 			value = apic_read(APIC_ISR + i*0x10);
1317f62bae50SIngo Molnar 			for (j = 31; j >= 0; j--) {
13188c3ba8d0SKerstin Jonsson 				if (value & (1<<j)) {
1319f62bae50SIngo Molnar 					ack_APIC_irq();
13208c3ba8d0SKerstin Jonsson 					acked++;
1321f62bae50SIngo Molnar 				}
1322f62bae50SIngo Molnar 			}
13238c3ba8d0SKerstin Jonsson 		}
13248c3ba8d0SKerstin Jonsson 		if (acked > 256) {
13258c3ba8d0SKerstin Jonsson 			printk(KERN_ERR "LAPIC pending interrupts after %d EOI\n",
13268c3ba8d0SKerstin Jonsson 			       acked);
13278c3ba8d0SKerstin Jonsson 			break;
13288c3ba8d0SKerstin Jonsson 		}
132942fa4250SShai Fultheim 		if (queued) {
13308c3ba8d0SKerstin Jonsson 			if (cpu_has_tsc) {
13318c3ba8d0SKerstin Jonsson 				rdtscll(ntsc);
13328c3ba8d0SKerstin Jonsson 				max_loops = (cpu_khz << 10) - (ntsc - tsc);
13338c3ba8d0SKerstin Jonsson 			} else
13348c3ba8d0SKerstin Jonsson 				max_loops--;
133542fa4250SShai Fultheim 		}
13368c3ba8d0SKerstin Jonsson 	} while (queued && max_loops > 0);
13378c3ba8d0SKerstin Jonsson 	WARN_ON(max_loops <= 0);
1338f62bae50SIngo Molnar 
1339f62bae50SIngo Molnar 	/*
1340f62bae50SIngo Molnar 	 * Now that we are all set up, enable the APIC
1341f62bae50SIngo Molnar 	 */
1342f62bae50SIngo Molnar 	value = apic_read(APIC_SPIV);
1343f62bae50SIngo Molnar 	value &= ~APIC_VECTOR_MASK;
1344f62bae50SIngo Molnar 	/*
1345f62bae50SIngo Molnar 	 * Enable APIC
1346f62bae50SIngo Molnar 	 */
1347f62bae50SIngo Molnar 	value |= APIC_SPIV_APIC_ENABLED;
1348f62bae50SIngo Molnar 
1349f62bae50SIngo Molnar #ifdef CONFIG_X86_32
1350f62bae50SIngo Molnar 	/*
1351f62bae50SIngo Molnar 	 * Some unknown Intel IO/APIC (or APIC) errata is biting us with
1352f62bae50SIngo Molnar 	 * certain networking cards. If high frequency interrupts are
1353f62bae50SIngo Molnar 	 * happening on a particular IOAPIC pin, plus the IOAPIC routing
1354f62bae50SIngo Molnar 	 * entry is masked/unmasked at a high rate as well then sooner or
1355f62bae50SIngo Molnar 	 * later IOAPIC line gets 'stuck', no more interrupts are received
1356f62bae50SIngo Molnar 	 * from the device. If focus CPU is disabled then the hang goes
1357f62bae50SIngo Molnar 	 * away, oh well :-(
1358f62bae50SIngo Molnar 	 *
1359f62bae50SIngo Molnar 	 * [ This bug can be reproduced easily with a level-triggered
1360f62bae50SIngo Molnar 	 *   PCI Ne2000 networking cards and PII/PIII processors, dual
1361f62bae50SIngo Molnar 	 *   BX chipset. ]
1362f62bae50SIngo Molnar 	 */
1363f62bae50SIngo Molnar 	/*
1364f62bae50SIngo Molnar 	 * Actually disabling the focus CPU check just makes the hang less
1365f62bae50SIngo Molnar 	 * frequent as it makes the interrupt distributon model be more
1366f62bae50SIngo Molnar 	 * like LRU than MRU (the short-term load is more even across CPUs).
1367f62bae50SIngo Molnar 	 * See also the comment in end_level_ioapic_irq().  --macro
1368f62bae50SIngo Molnar 	 */
1369f62bae50SIngo Molnar 
1370f62bae50SIngo Molnar 	/*
1371f62bae50SIngo Molnar 	 * - enable focus processor (bit==0)
1372f62bae50SIngo Molnar 	 * - 64bit mode always use processor focus
1373f62bae50SIngo Molnar 	 *   so no need to set it
1374f62bae50SIngo Molnar 	 */
1375f62bae50SIngo Molnar 	value &= ~APIC_SPIV_FOCUS_DISABLED;
1376f62bae50SIngo Molnar #endif
1377f62bae50SIngo Molnar 
1378f62bae50SIngo Molnar 	/*
1379f62bae50SIngo Molnar 	 * Set spurious IRQ vector
1380f62bae50SIngo Molnar 	 */
1381f62bae50SIngo Molnar 	value |= SPURIOUS_APIC_VECTOR;
1382f62bae50SIngo Molnar 	apic_write(APIC_SPIV, value);
1383f62bae50SIngo Molnar 
1384f62bae50SIngo Molnar 	/*
1385f62bae50SIngo Molnar 	 * Set up LVT0, LVT1:
1386f62bae50SIngo Molnar 	 *
1387f62bae50SIngo Molnar 	 * set up through-local-APIC on the BP's LINT0. This is not
1388f62bae50SIngo Molnar 	 * strictly necessary in pure symmetric-IO mode, but sometimes
1389f62bae50SIngo Molnar 	 * we delegate interrupts to the 8259A.
1390f62bae50SIngo Molnar 	 */
1391f62bae50SIngo Molnar 	/*
1392f62bae50SIngo Molnar 	 * TODO: set up through-local-APIC from through-I/O-APIC? --macro
1393f62bae50SIngo Molnar 	 */
1394f62bae50SIngo Molnar 	value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
13950aa002feSTejun Heo 	if (!cpu && (pic_mode || !value)) {
1396f62bae50SIngo Molnar 		value = APIC_DM_EXTINT;
13970aa002feSTejun Heo 		apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n", cpu);
1398f62bae50SIngo Molnar 	} else {
1399f62bae50SIngo Molnar 		value = APIC_DM_EXTINT | APIC_LVT_MASKED;
14000aa002feSTejun Heo 		apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n", cpu);
1401f62bae50SIngo Molnar 	}
1402f62bae50SIngo Molnar 	apic_write(APIC_LVT0, value);
1403f62bae50SIngo Molnar 
1404f62bae50SIngo Molnar 	/*
1405f62bae50SIngo Molnar 	 * only the BP should see the LINT1 NMI signal, obviously.
1406f62bae50SIngo Molnar 	 */
14070aa002feSTejun Heo 	if (!cpu)
1408f62bae50SIngo Molnar 		value = APIC_DM_NMI;
1409f62bae50SIngo Molnar 	else
1410f62bae50SIngo Molnar 		value = APIC_DM_NMI | APIC_LVT_MASKED;
1411f62bae50SIngo Molnar 	if (!lapic_is_integrated())		/* 82489DX */
1412f62bae50SIngo Molnar 		value |= APIC_LVT_LEVEL_TRIGGER;
1413f62bae50SIngo Molnar 	apic_write(APIC_LVT1, value);
1414f62bae50SIngo Molnar 
1415638bee71SH. Peter Anvin #ifdef CONFIG_X86_MCE_INTEL
1416638bee71SH. Peter Anvin 	/* Recheck CMCI information after local APIC is up on CPU #0 */
14170aa002feSTejun Heo 	if (!cpu)
1418638bee71SH. Peter Anvin 		cmci_recheck();
1419638bee71SH. Peter Anvin #endif
1420f62bae50SIngo Molnar }
1421f62bae50SIngo Molnar 
1422f62bae50SIngo Molnar void __cpuinit end_local_APIC_setup(void)
1423f62bae50SIngo Molnar {
1424f62bae50SIngo Molnar 	lapic_setup_esr();
1425f62bae50SIngo Molnar 
1426f62bae50SIngo Molnar #ifdef CONFIG_X86_32
1427f62bae50SIngo Molnar 	{
1428f62bae50SIngo Molnar 		unsigned int value;
1429f62bae50SIngo Molnar 		/* Disable the local apic timer */
1430f62bae50SIngo Molnar 		value = apic_read(APIC_LVTT);
1431f62bae50SIngo Molnar 		value |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
1432f62bae50SIngo Molnar 		apic_write(APIC_LVTT, value);
1433f62bae50SIngo Molnar 	}
1434f62bae50SIngo Molnar #endif
1435f62bae50SIngo Molnar 
1436f62bae50SIngo Molnar 	apic_pm_activate();
14372fb270f3SJan Beulich }
14382fb270f3SJan Beulich 
14392fb270f3SJan Beulich void __init bsp_end_local_APIC_setup(void)
14402fb270f3SJan Beulich {
14412fb270f3SJan Beulich 	end_local_APIC_setup();
14427f7fbf45SKenji Kaneshige 
14437f7fbf45SKenji Kaneshige 	/*
14447f7fbf45SKenji Kaneshige 	 * Now that local APIC setup is completed for BP, configure the fault
14457f7fbf45SKenji Kaneshige 	 * handling for interrupt remapping.
14467f7fbf45SKenji Kaneshige 	 */
144795a02e97SSuresh Siddha 	if (irq_remapping_enabled)
144895a02e97SSuresh Siddha 		irq_remap_enable_fault_handling();
14497f7fbf45SKenji Kaneshige 
1450f62bae50SIngo Molnar }
1451f62bae50SIngo Molnar 
1452f62bae50SIngo Molnar #ifdef CONFIG_X86_X2APIC
1453fb209bd8SYinghai Lu /*
1454fb209bd8SYinghai Lu  * Need to disable xapic and x2apic at the same time and then enable xapic mode
1455fb209bd8SYinghai Lu  */
1456fb209bd8SYinghai Lu static inline void __disable_x2apic(u64 msr)
1457fb209bd8SYinghai Lu {
1458fb209bd8SYinghai Lu 	wrmsrl(MSR_IA32_APICBASE,
1459fb209bd8SYinghai Lu 	       msr & ~(X2APIC_ENABLE | XAPIC_ENABLE));
1460fb209bd8SYinghai Lu 	wrmsrl(MSR_IA32_APICBASE, msr & ~X2APIC_ENABLE);
1461fb209bd8SYinghai Lu }
1462fb209bd8SYinghai Lu 
1463a31bc327SYinghai Lu static __init void disable_x2apic(void)
1464fb209bd8SYinghai Lu {
1465fb209bd8SYinghai Lu 	u64 msr;
1466fb209bd8SYinghai Lu 
1467fb209bd8SYinghai Lu 	if (!cpu_has_x2apic)
1468fb209bd8SYinghai Lu 		return;
1469fb209bd8SYinghai Lu 
1470fb209bd8SYinghai Lu 	rdmsrl(MSR_IA32_APICBASE, msr);
1471fb209bd8SYinghai Lu 	if (msr & X2APIC_ENABLE) {
1472fb209bd8SYinghai Lu 		u32 x2apic_id = read_apic_id();
1473fb209bd8SYinghai Lu 
1474fb209bd8SYinghai Lu 		if (x2apic_id >= 255)
1475fb209bd8SYinghai Lu 			panic("Cannot disable x2apic, id: %08x\n", x2apic_id);
1476fb209bd8SYinghai Lu 
1477fb209bd8SYinghai Lu 		pr_info("Disabling x2apic\n");
1478fb209bd8SYinghai Lu 		__disable_x2apic(msr);
1479fb209bd8SYinghai Lu 
1480a31bc327SYinghai Lu 		if (nox2apic) {
1481a31bc327SYinghai Lu 			clear_cpu_cap(&cpu_data(0), X86_FEATURE_X2APIC);
1482a31bc327SYinghai Lu 			setup_clear_cpu_cap(X86_FEATURE_X2APIC);
1483a31bc327SYinghai Lu 		}
1484a31bc327SYinghai Lu 
1485fb209bd8SYinghai Lu 		x2apic_disabled = 1;
1486fb209bd8SYinghai Lu 		x2apic_mode = 0;
1487fb209bd8SYinghai Lu 
1488fb209bd8SYinghai Lu 		register_lapic_address(mp_lapic_addr);
1489fb209bd8SYinghai Lu 	}
1490fb209bd8SYinghai Lu }
1491fb209bd8SYinghai Lu 
1492f62bae50SIngo Molnar void check_x2apic(void)
1493f62bae50SIngo Molnar {
1494ef1f87aaSSuresh Siddha 	if (x2apic_enabled()) {
1495f62bae50SIngo Molnar 		pr_info("x2apic enabled by BIOS, switching to x2apic ops\n");
1496fc1edaf9SSuresh Siddha 		x2apic_preenabled = x2apic_mode = 1;
1497f62bae50SIngo Molnar 	}
1498f62bae50SIngo Molnar }
1499f62bae50SIngo Molnar 
1500f62bae50SIngo Molnar void enable_x2apic(void)
1501f62bae50SIngo Molnar {
1502fb209bd8SYinghai Lu 	u64 msr;
1503fb209bd8SYinghai Lu 
1504fb209bd8SYinghai Lu 	rdmsrl(MSR_IA32_APICBASE, msr);
1505fb209bd8SYinghai Lu 	if (x2apic_disabled) {
1506fb209bd8SYinghai Lu 		__disable_x2apic(msr);
1507fb209bd8SYinghai Lu 		return;
1508fb209bd8SYinghai Lu 	}
1509f62bae50SIngo Molnar 
1510fc1edaf9SSuresh Siddha 	if (!x2apic_mode)
1511f62bae50SIngo Molnar 		return;
1512f62bae50SIngo Molnar 
1513f62bae50SIngo Molnar 	if (!(msr & X2APIC_ENABLE)) {
1514450b1e8dSMike Travis 		printk_once(KERN_INFO "Enabling x2apic\n");
1515fb209bd8SYinghai Lu 		wrmsrl(MSR_IA32_APICBASE, msr | X2APIC_ENABLE);
1516f62bae50SIngo Molnar 	}
1517f62bae50SIngo Molnar }
151893758238SWeidong Han #endif /* CONFIG_X86_X2APIC */
1519f62bae50SIngo Molnar 
1520ce69a784SGleb Natapov int __init enable_IR(void)
1521f62bae50SIngo Molnar {
1522d3f13810SSuresh Siddha #ifdef CONFIG_IRQ_REMAP
152395a02e97SSuresh Siddha 	if (!irq_remapping_supported()) {
152493758238SWeidong Han 		pr_debug("intr-remapping not supported\n");
152541750d31SSuresh Siddha 		return -1;
152693758238SWeidong Han 	}
152793758238SWeidong Han 
152893758238SWeidong Han 	if (!x2apic_preenabled && skip_ioapic_setup) {
152993758238SWeidong Han 		pr_info("Skipped enabling intr-remap because of skipping "
153093758238SWeidong Han 			"io-apic setup\n");
153141750d31SSuresh Siddha 		return -1;
1532f62bae50SIngo Molnar 	}
1533f62bae50SIngo Molnar 
153495a02e97SSuresh Siddha 	return irq_remapping_enable();
1535ce69a784SGleb Natapov #endif
153641750d31SSuresh Siddha 	return -1;
1537ce69a784SGleb Natapov }
1538ce69a784SGleb Natapov 
1539ce69a784SGleb Natapov void __init enable_IR_x2apic(void)
1540ce69a784SGleb Natapov {
1541ce69a784SGleb Natapov 	unsigned long flags;
1542ce69a784SGleb Natapov 	int ret, x2apic_enabled = 0;
1543736baef4SJoerg Roedel 	int hardware_init_ret;
1544b7f42ab2SYinghai Lu 
1545736baef4SJoerg Roedel 	/* Make sure irq_remap_ops are initialized */
154695a02e97SSuresh Siddha 	setup_irq_remapping_ops();
1547736baef4SJoerg Roedel 
154895a02e97SSuresh Siddha 	hardware_init_ret = irq_remapping_prepare();
1549736baef4SJoerg Roedel 	if (hardware_init_ret && !x2apic_supported())
1550e670761fSYinghai Lu 		return;
1551ce69a784SGleb Natapov 
155231dce14aSSuresh Siddha 	ret = save_ioapic_entries();
1553f62bae50SIngo Molnar 	if (ret) {
1554f62bae50SIngo Molnar 		pr_info("Saving IO-APIC state failed: %d\n", ret);
1555fb209bd8SYinghai Lu 		return;
1556f62bae50SIngo Molnar 	}
1557f62bae50SIngo Molnar 
155805c3dc2cSSuresh Siddha 	local_irq_save(flags);
1559b81bb373SJacob Pan 	legacy_pic->mask_all();
156031dce14aSSuresh Siddha 	mask_ioapic_entries();
156105c3dc2cSSuresh Siddha 
1562a31bc327SYinghai Lu 	if (x2apic_preenabled && nox2apic)
1563a31bc327SYinghai Lu 		disable_x2apic();
1564a31bc327SYinghai Lu 
1565736baef4SJoerg Roedel 	if (hardware_init_ret)
156641750d31SSuresh Siddha 		ret = -1;
1567b7f42ab2SYinghai Lu 	else
1568ce69a784SGleb Natapov 		ret = enable_IR();
1569b7f42ab2SYinghai Lu 
1570fb209bd8SYinghai Lu 	if (!x2apic_supported())
1571a31bc327SYinghai Lu 		goto skip_x2apic;
1572fb209bd8SYinghai Lu 
157341750d31SSuresh Siddha 	if (ret < 0) {
1574ce69a784SGleb Natapov 		/* IR is required if there is APIC ID > 255 even when running
1575ce69a784SGleb Natapov 		 * under KVM
1576ce69a784SGleb Natapov 		 */
15772904ed8dSSheng Yang 		if (max_physical_apicid > 255 ||
1578fb209bd8SYinghai Lu 		    !hypervisor_x2apic_available()) {
1579fb209bd8SYinghai Lu 			if (x2apic_preenabled)
1580fb209bd8SYinghai Lu 				disable_x2apic();
1581a31bc327SYinghai Lu 			goto skip_x2apic;
1582fb209bd8SYinghai Lu 		}
1583ce69a784SGleb Natapov 		/*
1584ce69a784SGleb Natapov 		 * without IR all CPUs can be addressed by IOAPIC/MSI
1585ce69a784SGleb Natapov 		 * only in physical mode
1586ce69a784SGleb Natapov 		 */
1587ce69a784SGleb Natapov 		x2apic_force_phys();
1588ce69a784SGleb Natapov 	}
1589f62bae50SIngo Molnar 
1590fb209bd8SYinghai Lu 	if (ret == IRQ_REMAP_XAPIC_MODE) {
1591fb209bd8SYinghai Lu 		pr_info("x2apic not enabled, IRQ remapping is in xapic mode\n");
1592a31bc327SYinghai Lu 		goto skip_x2apic;
1593fb209bd8SYinghai Lu 	}
159441750d31SSuresh Siddha 
1595ce69a784SGleb Natapov 	x2apic_enabled = 1;
159693758238SWeidong Han 
1597fc1edaf9SSuresh Siddha 	if (x2apic_supported() && !x2apic_mode) {
1598fc1edaf9SSuresh Siddha 		x2apic_mode = 1;
1599f62bae50SIngo Molnar 		enable_x2apic();
160093758238SWeidong Han 		pr_info("Enabled x2apic\n");
1601f62bae50SIngo Molnar 	}
1602f62bae50SIngo Molnar 
1603a31bc327SYinghai Lu skip_x2apic:
160441750d31SSuresh Siddha 	if (ret < 0) /* IR enabling failed */
160531dce14aSSuresh Siddha 		restore_ioapic_entries();
1606b81bb373SJacob Pan 	legacy_pic->restore_mask();
1607f62bae50SIngo Molnar 	local_irq_restore(flags);
1608f62bae50SIngo Molnar }
160993758238SWeidong Han 
1610f62bae50SIngo Molnar #ifdef CONFIG_X86_64
1611f62bae50SIngo Molnar /*
1612f62bae50SIngo Molnar  * Detect and enable local APICs on non-SMP boards.
1613f62bae50SIngo Molnar  * Original code written by Keir Fraser.
1614f62bae50SIngo Molnar  * On AMD64 we trust the BIOS - if it says no APIC it is likely
1615f62bae50SIngo Molnar  * not correctly set up (usually the APIC timer won't work etc.)
1616f62bae50SIngo Molnar  */
1617f62bae50SIngo Molnar static int __init detect_init_APIC(void)
1618f62bae50SIngo Molnar {
1619f62bae50SIngo Molnar 	if (!cpu_has_apic) {
1620f62bae50SIngo Molnar 		pr_info("No local APIC present\n");
1621f62bae50SIngo Molnar 		return -1;
1622f62bae50SIngo Molnar 	}
1623f62bae50SIngo Molnar 
1624f62bae50SIngo Molnar 	mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
1625f62bae50SIngo Molnar 	return 0;
1626f62bae50SIngo Molnar }
1627f62bae50SIngo Molnar #else
16285a7ae78fSThomas Gleixner 
162925874a29SHenrik Kretzschmar static int __init apic_verify(void)
16305a7ae78fSThomas Gleixner {
16315a7ae78fSThomas Gleixner 	u32 features, h, l;
16325a7ae78fSThomas Gleixner 
16335a7ae78fSThomas Gleixner 	/*
16345a7ae78fSThomas Gleixner 	 * The APIC feature bit should now be enabled
16355a7ae78fSThomas Gleixner 	 * in `cpuid'
16365a7ae78fSThomas Gleixner 	 */
16375a7ae78fSThomas Gleixner 	features = cpuid_edx(1);
16385a7ae78fSThomas Gleixner 	if (!(features & (1 << X86_FEATURE_APIC))) {
16395a7ae78fSThomas Gleixner 		pr_warning("Could not enable APIC!\n");
16405a7ae78fSThomas Gleixner 		return -1;
16415a7ae78fSThomas Gleixner 	}
16425a7ae78fSThomas Gleixner 	set_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
16435a7ae78fSThomas Gleixner 	mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
16445a7ae78fSThomas Gleixner 
16455a7ae78fSThomas Gleixner 	/* The BIOS may have set up the APIC at some other address */
1646cbf2829bSBryan O'Donoghue 	if (boot_cpu_data.x86 >= 6) {
16475a7ae78fSThomas Gleixner 		rdmsr(MSR_IA32_APICBASE, l, h);
16485a7ae78fSThomas Gleixner 		if (l & MSR_IA32_APICBASE_ENABLE)
16495a7ae78fSThomas Gleixner 			mp_lapic_addr = l & MSR_IA32_APICBASE_BASE;
1650cbf2829bSBryan O'Donoghue 	}
16515a7ae78fSThomas Gleixner 
16525a7ae78fSThomas Gleixner 	pr_info("Found and enabled local APIC!\n");
16535a7ae78fSThomas Gleixner 	return 0;
16545a7ae78fSThomas Gleixner }
16555a7ae78fSThomas Gleixner 
165625874a29SHenrik Kretzschmar int __init apic_force_enable(unsigned long addr)
16575a7ae78fSThomas Gleixner {
16585a7ae78fSThomas Gleixner 	u32 h, l;
16595a7ae78fSThomas Gleixner 
16605a7ae78fSThomas Gleixner 	if (disable_apic)
16615a7ae78fSThomas Gleixner 		return -1;
16625a7ae78fSThomas Gleixner 
16635a7ae78fSThomas Gleixner 	/*
16645a7ae78fSThomas Gleixner 	 * Some BIOSes disable the local APIC in the APIC_BASE
16655a7ae78fSThomas Gleixner 	 * MSR. This can only be done in software for Intel P6 or later
16665a7ae78fSThomas Gleixner 	 * and AMD K7 (Model > 1) or later.
16675a7ae78fSThomas Gleixner 	 */
1668cbf2829bSBryan O'Donoghue 	if (boot_cpu_data.x86 >= 6) {
16695a7ae78fSThomas Gleixner 		rdmsr(MSR_IA32_APICBASE, l, h);
16705a7ae78fSThomas Gleixner 		if (!(l & MSR_IA32_APICBASE_ENABLE)) {
16715a7ae78fSThomas Gleixner 			pr_info("Local APIC disabled by BIOS -- reenabling.\n");
16725a7ae78fSThomas Gleixner 			l &= ~MSR_IA32_APICBASE_BASE;
1673a906fdaaSThomas Gleixner 			l |= MSR_IA32_APICBASE_ENABLE | addr;
16745a7ae78fSThomas Gleixner 			wrmsr(MSR_IA32_APICBASE, l, h);
16755a7ae78fSThomas Gleixner 			enabled_via_apicbase = 1;
16765a7ae78fSThomas Gleixner 		}
1677cbf2829bSBryan O'Donoghue 	}
16785a7ae78fSThomas Gleixner 	return apic_verify();
16795a7ae78fSThomas Gleixner }
16805a7ae78fSThomas Gleixner 
1681f62bae50SIngo Molnar /*
1682f62bae50SIngo Molnar  * Detect and initialize APIC
1683f62bae50SIngo Molnar  */
1684f62bae50SIngo Molnar static int __init detect_init_APIC(void)
1685f62bae50SIngo Molnar {
1686f62bae50SIngo Molnar 	/* Disabled by kernel option? */
1687f62bae50SIngo Molnar 	if (disable_apic)
1688f62bae50SIngo Molnar 		return -1;
1689f62bae50SIngo Molnar 
1690f62bae50SIngo Molnar 	switch (boot_cpu_data.x86_vendor) {
1691f62bae50SIngo Molnar 	case X86_VENDOR_AMD:
1692f62bae50SIngo Molnar 		if ((boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model > 1) ||
1693f62bae50SIngo Molnar 		    (boot_cpu_data.x86 >= 15))
1694f62bae50SIngo Molnar 			break;
1695f62bae50SIngo Molnar 		goto no_apic;
1696f62bae50SIngo Molnar 	case X86_VENDOR_INTEL:
1697f62bae50SIngo Molnar 		if (boot_cpu_data.x86 == 6 || boot_cpu_data.x86 == 15 ||
1698f62bae50SIngo Molnar 		    (boot_cpu_data.x86 == 5 && cpu_has_apic))
1699f62bae50SIngo Molnar 			break;
1700f62bae50SIngo Molnar 		goto no_apic;
1701f62bae50SIngo Molnar 	default:
1702f62bae50SIngo Molnar 		goto no_apic;
1703f62bae50SIngo Molnar 	}
1704f62bae50SIngo Molnar 
1705f62bae50SIngo Molnar 	if (!cpu_has_apic) {
1706f62bae50SIngo Molnar 		/*
1707f62bae50SIngo Molnar 		 * Over-ride BIOS and try to enable the local APIC only if
1708f62bae50SIngo Molnar 		 * "lapic" specified.
1709f62bae50SIngo Molnar 		 */
1710f62bae50SIngo Molnar 		if (!force_enable_local_apic) {
1711f62bae50SIngo Molnar 			pr_info("Local APIC disabled by BIOS -- "
1712f62bae50SIngo Molnar 				"you can enable it with \"lapic\"\n");
1713f62bae50SIngo Molnar 			return -1;
1714f62bae50SIngo Molnar 		}
1715a906fdaaSThomas Gleixner 		if (apic_force_enable(APIC_DEFAULT_PHYS_BASE))
17165a7ae78fSThomas Gleixner 			return -1;
17175a7ae78fSThomas Gleixner 	} else {
17185a7ae78fSThomas Gleixner 		if (apic_verify())
1719f62bae50SIngo Molnar 			return -1;
1720f62bae50SIngo Molnar 	}
1721f62bae50SIngo Molnar 
1722f62bae50SIngo Molnar 	apic_pm_activate();
1723f62bae50SIngo Molnar 
1724f62bae50SIngo Molnar 	return 0;
1725f62bae50SIngo Molnar 
1726f62bae50SIngo Molnar no_apic:
1727f62bae50SIngo Molnar 	pr_info("No local APIC present or hardware disabled\n");
1728f62bae50SIngo Molnar 	return -1;
1729f62bae50SIngo Molnar }
1730f62bae50SIngo Molnar #endif
1731f62bae50SIngo Molnar 
1732f62bae50SIngo Molnar /**
1733f62bae50SIngo Molnar  * init_apic_mappings - initialize APIC mappings
1734f62bae50SIngo Molnar  */
1735f62bae50SIngo Molnar void __init init_apic_mappings(void)
1736f62bae50SIngo Molnar {
17374401da61SYinghai Lu 	unsigned int new_apicid;
17384401da61SYinghai Lu 
1739fc1edaf9SSuresh Siddha 	if (x2apic_mode) {
1740f62bae50SIngo Molnar 		boot_cpu_physical_apicid = read_apic_id();
1741f62bae50SIngo Molnar 		return;
1742f62bae50SIngo Molnar 	}
1743f62bae50SIngo Molnar 
17444797f6b0SYinghai Lu 	/* If no local APIC can be found return early */
1745f62bae50SIngo Molnar 	if (!smp_found_config && detect_init_APIC()) {
17464797f6b0SYinghai Lu 		/* lets NOP'ify apic operations */
17474797f6b0SYinghai Lu 		pr_info("APIC: disable apic facility\n");
17484797f6b0SYinghai Lu 		apic_disable();
17494797f6b0SYinghai Lu 	} else {
1750f62bae50SIngo Molnar 		apic_phys = mp_lapic_addr;
1751f62bae50SIngo Molnar 
17524401da61SYinghai Lu 		/*
17534401da61SYinghai Lu 		 * acpi lapic path already maps that address in
17544401da61SYinghai Lu 		 * acpi_register_lapic_address()
17554401da61SYinghai Lu 		 */
17565989cd6aSEric W. Biederman 		if (!acpi_lapic && !smp_found_config)
1757326a2e6bSYinghai Lu 			register_lapic_address(apic_phys);
1758cec6be6dSCyrill Gorcunov 	}
1759f62bae50SIngo Molnar 
1760f62bae50SIngo Molnar 	/*
1761f62bae50SIngo Molnar 	 * Fetch the APIC ID of the BSP in case we have a
1762f62bae50SIngo Molnar 	 * default configuration (or the MP table is broken).
1763f62bae50SIngo Molnar 	 */
17644401da61SYinghai Lu 	new_apicid = read_apic_id();
17654401da61SYinghai Lu 	if (boot_cpu_physical_apicid != new_apicid) {
17664401da61SYinghai Lu 		boot_cpu_physical_apicid = new_apicid;
1767103428e5SCyrill Gorcunov 		/*
1768103428e5SCyrill Gorcunov 		 * yeah -- we lie about apic_version
1769103428e5SCyrill Gorcunov 		 * in case if apic was disabled via boot option
1770103428e5SCyrill Gorcunov 		 * but it's not a problem for SMP compiled kernel
1771103428e5SCyrill Gorcunov 		 * since smp_sanity_check is prepared for such a case
1772103428e5SCyrill Gorcunov 		 * and disable smp mode
1773103428e5SCyrill Gorcunov 		 */
17744401da61SYinghai Lu 		apic_version[new_apicid] =
17754401da61SYinghai Lu 			 GET_APIC_VERSION(apic_read(APIC_LVR));
177608306ce6SCyrill Gorcunov 	}
1777f62bae50SIngo Molnar }
1778f62bae50SIngo Molnar 
1779c0104d38SYinghai Lu void __init register_lapic_address(unsigned long address)
1780c0104d38SYinghai Lu {
1781c0104d38SYinghai Lu 	mp_lapic_addr = address;
1782c0104d38SYinghai Lu 
17830450193bSYinghai Lu 	if (!x2apic_mode) {
1784c0104d38SYinghai Lu 		set_fixmap_nocache(FIX_APIC_BASE, address);
1785f1157141SYinghai Lu 		apic_printk(APIC_VERBOSE, "mapped APIC to %16lx (%16lx)\n",
1786f1157141SYinghai Lu 			    APIC_BASE, mp_lapic_addr);
17870450193bSYinghai Lu 	}
1788c0104d38SYinghai Lu 	if (boot_cpu_physical_apicid == -1U) {
1789c0104d38SYinghai Lu 		boot_cpu_physical_apicid  = read_apic_id();
1790c0104d38SYinghai Lu 		apic_version[boot_cpu_physical_apicid] =
1791c0104d38SYinghai Lu 			 GET_APIC_VERSION(apic_read(APIC_LVR));
1792c0104d38SYinghai Lu 	}
1793c0104d38SYinghai Lu }
1794c0104d38SYinghai Lu 
1795f62bae50SIngo Molnar /*
1796f62bae50SIngo Molnar  * This initializes the IO-APIC and APIC hardware if this is
1797f62bae50SIngo Molnar  * a UP kernel.
1798f62bae50SIngo Molnar  */
179956d91f13SYinghai Lu int apic_version[MAX_LOCAL_APIC];
1800f62bae50SIngo Molnar 
1801f62bae50SIngo Molnar int __init APIC_init_uniprocessor(void)
1802f62bae50SIngo Molnar {
1803f62bae50SIngo Molnar 	if (disable_apic) {
1804f62bae50SIngo Molnar 		pr_info("Apic disabled\n");
1805f62bae50SIngo Molnar 		return -1;
1806f62bae50SIngo Molnar 	}
1807f62bae50SIngo Molnar #ifdef CONFIG_X86_64
1808f62bae50SIngo Molnar 	if (!cpu_has_apic) {
1809f62bae50SIngo Molnar 		disable_apic = 1;
1810f62bae50SIngo Molnar 		pr_info("Apic disabled by BIOS\n");
1811f62bae50SIngo Molnar 		return -1;
1812f62bae50SIngo Molnar 	}
1813f62bae50SIngo Molnar #else
1814f62bae50SIngo Molnar 	if (!smp_found_config && !cpu_has_apic)
1815f62bae50SIngo Molnar 		return -1;
1816f62bae50SIngo Molnar 
1817f62bae50SIngo Molnar 	/*
1818f62bae50SIngo Molnar 	 * Complain if the BIOS pretends there is one.
1819f62bae50SIngo Molnar 	 */
1820f62bae50SIngo Molnar 	if (!cpu_has_apic &&
1821f62bae50SIngo Molnar 	    APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid])) {
1822f62bae50SIngo Molnar 		pr_err("BIOS bug, local APIC 0x%x not detected!...\n",
1823f62bae50SIngo Molnar 			boot_cpu_physical_apicid);
1824f62bae50SIngo Molnar 		return -1;
1825f62bae50SIngo Molnar 	}
1826f62bae50SIngo Molnar #endif
1827f62bae50SIngo Molnar 
1828f62bae50SIngo Molnar 	default_setup_apic_routing();
1829f62bae50SIngo Molnar 
1830f62bae50SIngo Molnar 	verify_local_APIC();
1831f62bae50SIngo Molnar 	connect_bsp_APIC();
1832f62bae50SIngo Molnar 
1833f62bae50SIngo Molnar #ifdef CONFIG_X86_64
1834f62bae50SIngo Molnar 	apic_write(APIC_ID, SET_APIC_ID(boot_cpu_physical_apicid));
1835f62bae50SIngo Molnar #else
1836f62bae50SIngo Molnar 	/*
1837f62bae50SIngo Molnar 	 * Hack: In case of kdump, after a crash, kernel might be booting
1838f62bae50SIngo Molnar 	 * on a cpu with non-zero lapic id. But boot_cpu_physical_apicid
1839f62bae50SIngo Molnar 	 * might be zero if read from MP tables. Get it from LAPIC.
1840f62bae50SIngo Molnar 	 */
1841f62bae50SIngo Molnar # ifdef CONFIG_CRASH_DUMP
1842f62bae50SIngo Molnar 	boot_cpu_physical_apicid = read_apic_id();
1843f62bae50SIngo Molnar # endif
1844f62bae50SIngo Molnar #endif
1845f62bae50SIngo Molnar 	physid_set_mask_of_physid(boot_cpu_physical_apicid, &phys_cpu_present_map);
1846f62bae50SIngo Molnar 	setup_local_APIC();
1847f62bae50SIngo Molnar 
1848f62bae50SIngo Molnar #ifdef CONFIG_X86_IO_APIC
1849f62bae50SIngo Molnar 	/*
1850f62bae50SIngo Molnar 	 * Now enable IO-APICs, actually call clear_IO_APIC
1851f62bae50SIngo Molnar 	 * We need clear_IO_APIC before enabling error vector
1852f62bae50SIngo Molnar 	 */
1853f62bae50SIngo Molnar 	if (!skip_ioapic_setup && nr_ioapics)
1854f62bae50SIngo Molnar 		enable_IO_APIC();
1855f62bae50SIngo Molnar #endif
1856f62bae50SIngo Molnar 
18572fb270f3SJan Beulich 	bsp_end_local_APIC_setup();
1858f62bae50SIngo Molnar 
1859f62bae50SIngo Molnar #ifdef CONFIG_X86_IO_APIC
1860f62bae50SIngo Molnar 	if (smp_found_config && !skip_ioapic_setup && nr_ioapics)
1861f62bae50SIngo Molnar 		setup_IO_APIC();
1862f62bae50SIngo Molnar 	else {
1863f62bae50SIngo Molnar 		nr_ioapics = 0;
1864f62bae50SIngo Molnar 	}
1865f62bae50SIngo Molnar #endif
1866f62bae50SIngo Molnar 
1867736decacSThomas Gleixner 	x86_init.timers.setup_percpu_clockev();
1868f62bae50SIngo Molnar 	return 0;
1869f62bae50SIngo Molnar }
1870f62bae50SIngo Molnar 
1871f62bae50SIngo Molnar /*
1872f62bae50SIngo Molnar  * Local APIC interrupts
1873f62bae50SIngo Molnar  */
1874f62bae50SIngo Molnar 
1875f62bae50SIngo Molnar /*
1876f62bae50SIngo Molnar  * This interrupt should _never_ happen with our APIC/SMP architecture
1877f62bae50SIngo Molnar  */
1878f62bae50SIngo Molnar void smp_spurious_interrupt(struct pt_regs *regs)
1879f62bae50SIngo Molnar {
1880f62bae50SIngo Molnar 	u32 v;
1881f62bae50SIngo Molnar 
1882f62bae50SIngo Molnar 	irq_enter();
188398ad1cc1SFrederic Weisbecker 	exit_idle();
1884f62bae50SIngo Molnar 	/*
1885f62bae50SIngo Molnar 	 * Check if this really is a spurious interrupt and ACK it
1886f62bae50SIngo Molnar 	 * if it is a vectored one.  Just in case...
1887f62bae50SIngo Molnar 	 * Spurious interrupts should not be ACKed.
1888f62bae50SIngo Molnar 	 */
1889f62bae50SIngo Molnar 	v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
1890f62bae50SIngo Molnar 	if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
1891f62bae50SIngo Molnar 		ack_APIC_irq();
1892f62bae50SIngo Molnar 
1893f62bae50SIngo Molnar 	inc_irq_stat(irq_spurious_count);
1894f62bae50SIngo Molnar 
1895f62bae50SIngo Molnar 	/* see sw-dev-man vol 3, chapter 7.4.13.5 */
1896f62bae50SIngo Molnar 	pr_info("spurious APIC interrupt on CPU#%d, "
1897f62bae50SIngo Molnar 		"should never happen.\n", smp_processor_id());
1898f62bae50SIngo Molnar 	irq_exit();
1899f62bae50SIngo Molnar }
1900f62bae50SIngo Molnar 
1901f62bae50SIngo Molnar /*
1902f62bae50SIngo Molnar  * This interrupt should never happen with our APIC/SMP architecture
1903f62bae50SIngo Molnar  */
1904f62bae50SIngo Molnar void smp_error_interrupt(struct pt_regs *regs)
1905f62bae50SIngo Molnar {
19062b398bd9SYouquan Song 	u32 v0, v1;
19072b398bd9SYouquan Song 	u32 i = 0;
19082b398bd9SYouquan Song 	static const char * const error_interrupt_reason[] = {
19092b398bd9SYouquan Song 		"Send CS error",		/* APIC Error Bit 0 */
19102b398bd9SYouquan Song 		"Receive CS error",		/* APIC Error Bit 1 */
19112b398bd9SYouquan Song 		"Send accept error",		/* APIC Error Bit 2 */
19122b398bd9SYouquan Song 		"Receive accept error",		/* APIC Error Bit 3 */
19132b398bd9SYouquan Song 		"Redirectable IPI",		/* APIC Error Bit 4 */
19142b398bd9SYouquan Song 		"Send illegal vector",		/* APIC Error Bit 5 */
19152b398bd9SYouquan Song 		"Received illegal vector",	/* APIC Error Bit 6 */
19162b398bd9SYouquan Song 		"Illegal register address",	/* APIC Error Bit 7 */
19172b398bd9SYouquan Song 	};
1918f62bae50SIngo Molnar 
1919f62bae50SIngo Molnar 	irq_enter();
192098ad1cc1SFrederic Weisbecker 	exit_idle();
1921f62bae50SIngo Molnar 	/* First tickle the hardware, only then report what went on. -- REW */
19222b398bd9SYouquan Song 	v0 = apic_read(APIC_ESR);
1923f62bae50SIngo Molnar 	apic_write(APIC_ESR, 0);
1924f62bae50SIngo Molnar 	v1 = apic_read(APIC_ESR);
1925f62bae50SIngo Molnar 	ack_APIC_irq();
1926f62bae50SIngo Molnar 	atomic_inc(&irq_err_count);
1927f62bae50SIngo Molnar 
19282b398bd9SYouquan Song 	apic_printk(APIC_DEBUG, KERN_DEBUG "APIC error on CPU%d: %02x(%02x)",
19292b398bd9SYouquan Song 		    smp_processor_id(), v0 , v1);
19302b398bd9SYouquan Song 
19312b398bd9SYouquan Song 	v1 = v1 & 0xff;
19322b398bd9SYouquan Song 	while (v1) {
19332b398bd9SYouquan Song 		if (v1 & 0x1)
19342b398bd9SYouquan Song 			apic_printk(APIC_DEBUG, KERN_CONT " : %s", error_interrupt_reason[i]);
19352b398bd9SYouquan Song 		i++;
19362b398bd9SYouquan Song 		v1 >>= 1;
19372b398bd9SYouquan Song 	};
19382b398bd9SYouquan Song 
19392b398bd9SYouquan Song 	apic_printk(APIC_DEBUG, KERN_CONT "\n");
19402b398bd9SYouquan Song 
1941f62bae50SIngo Molnar 	irq_exit();
1942f62bae50SIngo Molnar }
1943f62bae50SIngo Molnar 
1944f62bae50SIngo Molnar /**
1945f62bae50SIngo Molnar  * connect_bsp_APIC - attach the APIC to the interrupt system
1946f62bae50SIngo Molnar  */
1947f62bae50SIngo Molnar void __init connect_bsp_APIC(void)
1948f62bae50SIngo Molnar {
1949f62bae50SIngo Molnar #ifdef CONFIG_X86_32
1950f62bae50SIngo Molnar 	if (pic_mode) {
1951f62bae50SIngo Molnar 		/*
1952f62bae50SIngo Molnar 		 * Do not trust the local APIC being empty at bootup.
1953f62bae50SIngo Molnar 		 */
1954f62bae50SIngo Molnar 		clear_local_APIC();
1955f62bae50SIngo Molnar 		/*
1956f62bae50SIngo Molnar 		 * PIC mode, enable APIC mode in the IMCR, i.e.  connect BSP's
1957f62bae50SIngo Molnar 		 * local APIC to INT and NMI lines.
1958f62bae50SIngo Molnar 		 */
1959f62bae50SIngo Molnar 		apic_printk(APIC_VERBOSE, "leaving PIC mode, "
1960f62bae50SIngo Molnar 				"enabling APIC mode.\n");
1961c0eaa453SCyrill Gorcunov 		imcr_pic_to_apic();
1962f62bae50SIngo Molnar 	}
1963f62bae50SIngo Molnar #endif
1964f62bae50SIngo Molnar 	if (apic->enable_apic_mode)
1965f62bae50SIngo Molnar 		apic->enable_apic_mode();
1966f62bae50SIngo Molnar }
1967f62bae50SIngo Molnar 
1968f62bae50SIngo Molnar /**
1969f62bae50SIngo Molnar  * disconnect_bsp_APIC - detach the APIC from the interrupt system
1970f62bae50SIngo Molnar  * @virt_wire_setup:	indicates, whether virtual wire mode is selected
1971f62bae50SIngo Molnar  *
1972f62bae50SIngo Molnar  * Virtual wire mode is necessary to deliver legacy interrupts even when the
1973f62bae50SIngo Molnar  * APIC is disabled.
1974f62bae50SIngo Molnar  */
1975f62bae50SIngo Molnar void disconnect_bsp_APIC(int virt_wire_setup)
1976f62bae50SIngo Molnar {
1977f62bae50SIngo Molnar 	unsigned int value;
1978f62bae50SIngo Molnar 
1979f62bae50SIngo Molnar #ifdef CONFIG_X86_32
1980f62bae50SIngo Molnar 	if (pic_mode) {
1981f62bae50SIngo Molnar 		/*
1982f62bae50SIngo Molnar 		 * Put the board back into PIC mode (has an effect only on
1983f62bae50SIngo Molnar 		 * certain older boards).  Note that APIC interrupts, including
1984f62bae50SIngo Molnar 		 * IPIs, won't work beyond this point!  The only exception are
1985f62bae50SIngo Molnar 		 * INIT IPIs.
1986f62bae50SIngo Molnar 		 */
1987f62bae50SIngo Molnar 		apic_printk(APIC_VERBOSE, "disabling APIC mode, "
1988f62bae50SIngo Molnar 				"entering PIC mode.\n");
1989c0eaa453SCyrill Gorcunov 		imcr_apic_to_pic();
1990f62bae50SIngo Molnar 		return;
1991f62bae50SIngo Molnar 	}
1992f62bae50SIngo Molnar #endif
1993f62bae50SIngo Molnar 
1994f62bae50SIngo Molnar 	/* Go back to Virtual Wire compatibility mode */
1995f62bae50SIngo Molnar 
1996f62bae50SIngo Molnar 	/* For the spurious interrupt use vector F, and enable it */
1997f62bae50SIngo Molnar 	value = apic_read(APIC_SPIV);
1998f62bae50SIngo Molnar 	value &= ~APIC_VECTOR_MASK;
1999f62bae50SIngo Molnar 	value |= APIC_SPIV_APIC_ENABLED;
2000f62bae50SIngo Molnar 	value |= 0xf;
2001f62bae50SIngo Molnar 	apic_write(APIC_SPIV, value);
2002f62bae50SIngo Molnar 
2003f62bae50SIngo Molnar 	if (!virt_wire_setup) {
2004f62bae50SIngo Molnar 		/*
2005f62bae50SIngo Molnar 		 * For LVT0 make it edge triggered, active high,
2006f62bae50SIngo Molnar 		 * external and enabled
2007f62bae50SIngo Molnar 		 */
2008f62bae50SIngo Molnar 		value = apic_read(APIC_LVT0);
2009f62bae50SIngo Molnar 		value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
2010f62bae50SIngo Molnar 			APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
2011f62bae50SIngo Molnar 			APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
2012f62bae50SIngo Molnar 		value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
2013f62bae50SIngo Molnar 		value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
2014f62bae50SIngo Molnar 		apic_write(APIC_LVT0, value);
2015f62bae50SIngo Molnar 	} else {
2016f62bae50SIngo Molnar 		/* Disable LVT0 */
2017f62bae50SIngo Molnar 		apic_write(APIC_LVT0, APIC_LVT_MASKED);
2018f62bae50SIngo Molnar 	}
2019f62bae50SIngo Molnar 
2020f62bae50SIngo Molnar 	/*
2021f62bae50SIngo Molnar 	 * For LVT1 make it edge triggered, active high,
2022f62bae50SIngo Molnar 	 * nmi and enabled
2023f62bae50SIngo Molnar 	 */
2024f62bae50SIngo Molnar 	value = apic_read(APIC_LVT1);
2025f62bae50SIngo Molnar 	value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
2026f62bae50SIngo Molnar 			APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
2027f62bae50SIngo Molnar 			APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
2028f62bae50SIngo Molnar 	value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
2029f62bae50SIngo Molnar 	value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
2030f62bae50SIngo Molnar 	apic_write(APIC_LVT1, value);
2031f62bae50SIngo Molnar }
2032f62bae50SIngo Molnar 
2033f62bae50SIngo Molnar void __cpuinit generic_processor_info(int apicid, int version)
2034f62bae50SIngo Molnar {
203514cb6dcfSVivek Goyal 	int cpu, max = nr_cpu_ids;
203614cb6dcfSVivek Goyal 	bool boot_cpu_detected = physid_isset(boot_cpu_physical_apicid,
203714cb6dcfSVivek Goyal 				phys_cpu_present_map);
203814cb6dcfSVivek Goyal 
203914cb6dcfSVivek Goyal 	/*
204014cb6dcfSVivek Goyal 	 * If boot cpu has not been detected yet, then only allow upto
204114cb6dcfSVivek Goyal 	 * nr_cpu_ids - 1 processors and keep one slot free for boot cpu
204214cb6dcfSVivek Goyal 	 */
204314cb6dcfSVivek Goyal 	if (!boot_cpu_detected && num_processors >= nr_cpu_ids - 1 &&
204414cb6dcfSVivek Goyal 	    apicid != boot_cpu_physical_apicid) {
204514cb6dcfSVivek Goyal 		int thiscpu = max + disabled_cpus - 1;
204614cb6dcfSVivek Goyal 
204714cb6dcfSVivek Goyal 		pr_warning(
204814cb6dcfSVivek Goyal 			"ACPI: NR_CPUS/possible_cpus limit of %i almost"
204914cb6dcfSVivek Goyal 			" reached. Keeping one slot for boot cpu."
205014cb6dcfSVivek Goyal 			"  Processor %d/0x%x ignored.\n", max, thiscpu, apicid);
205114cb6dcfSVivek Goyal 
205214cb6dcfSVivek Goyal 		disabled_cpus++;
205314cb6dcfSVivek Goyal 		return;
205414cb6dcfSVivek Goyal 	}
2055f62bae50SIngo Molnar 
2056f62bae50SIngo Molnar 	if (num_processors >= nr_cpu_ids) {
2057f62bae50SIngo Molnar 		int thiscpu = max + disabled_cpus;
2058f62bae50SIngo Molnar 
2059f62bae50SIngo Molnar 		pr_warning(
2060f62bae50SIngo Molnar 			"ACPI: NR_CPUS/possible_cpus limit of %i reached."
2061f62bae50SIngo Molnar 			"  Processor %d/0x%x ignored.\n", max, thiscpu, apicid);
2062f62bae50SIngo Molnar 
2063f62bae50SIngo Molnar 		disabled_cpus++;
2064f62bae50SIngo Molnar 		return;
2065f62bae50SIngo Molnar 	}
2066f62bae50SIngo Molnar 
2067f62bae50SIngo Molnar 	num_processors++;
2068f62bae50SIngo Molnar 	if (apicid == boot_cpu_physical_apicid) {
2069f62bae50SIngo Molnar 		/*
2070f62bae50SIngo Molnar 		 * x86_bios_cpu_apicid is required to have processors listed
2071f62bae50SIngo Molnar 		 * in same order as logical cpu numbers. Hence the first
2072f62bae50SIngo Molnar 		 * entry is BSP, and so on.
2073e5fea868SYinghai Lu 		 * boot_cpu_init() already hold bit 0 in cpu_present_mask
2074e5fea868SYinghai Lu 		 * for BSP.
2075f62bae50SIngo Molnar 		 */
2076f62bae50SIngo Molnar 		cpu = 0;
2077e5fea868SYinghai Lu 	} else
2078e5fea868SYinghai Lu 		cpu = cpumask_next_zero(-1, cpu_present_mask);
2079e5fea868SYinghai Lu 
2080e5fea868SYinghai Lu 	/*
2081e5fea868SYinghai Lu 	 * Validate version
2082e5fea868SYinghai Lu 	 */
2083e5fea868SYinghai Lu 	if (version == 0x0) {
2084e5fea868SYinghai Lu 		pr_warning("BIOS bug: APIC version is 0 for CPU %d/0x%x, fixing up to 0x10\n",
2085e5fea868SYinghai Lu 			   cpu, apicid);
2086e5fea868SYinghai Lu 		version = 0x10;
2087f62bae50SIngo Molnar 	}
2088e5fea868SYinghai Lu 	apic_version[apicid] = version;
2089e5fea868SYinghai Lu 
2090e5fea868SYinghai Lu 	if (version != apic_version[boot_cpu_physical_apicid]) {
2091e5fea868SYinghai Lu 		pr_warning("BIOS bug: APIC version mismatch, boot CPU: %x, CPU %d: version %x\n",
2092e5fea868SYinghai Lu 			apic_version[boot_cpu_physical_apicid], cpu, version);
2093e5fea868SYinghai Lu 	}
2094e5fea868SYinghai Lu 
2095e5fea868SYinghai Lu 	physid_set(apicid, phys_cpu_present_map);
2096f62bae50SIngo Molnar 	if (apicid > max_physical_apicid)
2097f62bae50SIngo Molnar 		max_physical_apicid = apicid;
2098f62bae50SIngo Molnar 
2099f62bae50SIngo Molnar #if defined(CONFIG_SMP) || defined(CONFIG_X86_64)
2100f62bae50SIngo Molnar 	early_per_cpu(x86_cpu_to_apicid, cpu) = apicid;
2101f62bae50SIngo Molnar 	early_per_cpu(x86_bios_cpu_apicid, cpu) = apicid;
2102f62bae50SIngo Molnar #endif
2103acb8bc09STejun Heo #ifdef CONFIG_X86_32
2104acb8bc09STejun Heo 	early_per_cpu(x86_cpu_to_logical_apicid, cpu) =
2105acb8bc09STejun Heo 		apic->x86_32_early_logical_apicid(cpu);
2106acb8bc09STejun Heo #endif
2107f62bae50SIngo Molnar 	set_cpu_possible(cpu, true);
2108f62bae50SIngo Molnar 	set_cpu_present(cpu, true);
2109f62bae50SIngo Molnar }
2110f62bae50SIngo Molnar 
2111f62bae50SIngo Molnar int hard_smp_processor_id(void)
2112f62bae50SIngo Molnar {
2113f62bae50SIngo Molnar 	return read_apic_id();
2114f62bae50SIngo Molnar }
2115f62bae50SIngo Molnar 
2116f62bae50SIngo Molnar void default_init_apic_ldr(void)
2117f62bae50SIngo Molnar {
2118f62bae50SIngo Molnar 	unsigned long val;
2119f62bae50SIngo Molnar 
2120f62bae50SIngo Molnar 	apic_write(APIC_DFR, APIC_DFR_VALUE);
2121f62bae50SIngo Molnar 	val = apic_read(APIC_LDR) & ~APIC_LDR_MASK;
2122f62bae50SIngo Molnar 	val |= SET_APIC_LOGICAL_ID(1UL << smp_processor_id());
2123f62bae50SIngo Molnar 	apic_write(APIC_LDR, val);
2124f62bae50SIngo Molnar }
2125f62bae50SIngo Molnar 
2126ff164324SAlexander Gordeev int default_cpu_mask_to_apicid_and(const struct cpumask *cpumask,
2127ff164324SAlexander Gordeev 				   const struct cpumask *andmask,
2128ff164324SAlexander Gordeev 				   unsigned int *apicid)
21296398268dSAlexander Gordeev {
21306398268dSAlexander Gordeev 	int cpu;
21316398268dSAlexander Gordeev 
21326398268dSAlexander Gordeev 	for_each_cpu_and(cpu, cpumask, andmask) {
21336398268dSAlexander Gordeev 		if (cpumask_test_cpu(cpu, cpu_online_mask))
21346398268dSAlexander Gordeev 			break;
21356398268dSAlexander Gordeev 	}
2136ff164324SAlexander Gordeev 
2137a5a39156SAlexander Gordeev 	if (likely((unsigned int)cpu < nr_cpu_ids)) {
2138a5a39156SAlexander Gordeev 		*apicid = per_cpu(x86_cpu_to_apicid, cpu);
2139a5a39156SAlexander Gordeev 		return 0;
2140a5a39156SAlexander Gordeev 	} else {
2141a5a39156SAlexander Gordeev 		return -EINVAL;
2142a5a39156SAlexander Gordeev 	}
21436398268dSAlexander Gordeev }
21446398268dSAlexander Gordeev 
2145f62bae50SIngo Molnar /*
2146f62bae50SIngo Molnar  * Power management
2147f62bae50SIngo Molnar  */
2148f62bae50SIngo Molnar #ifdef CONFIG_PM
2149f62bae50SIngo Molnar 
2150f62bae50SIngo Molnar static struct {
2151f62bae50SIngo Molnar 	/*
2152f62bae50SIngo Molnar 	 * 'active' is true if the local APIC was enabled by us and
2153f62bae50SIngo Molnar 	 * not the BIOS; this signifies that we are also responsible
2154f62bae50SIngo Molnar 	 * for disabling it before entering apm/acpi suspend
2155f62bae50SIngo Molnar 	 */
2156f62bae50SIngo Molnar 	int active;
2157f62bae50SIngo Molnar 	/* r/w apic fields */
2158f62bae50SIngo Molnar 	unsigned int apic_id;
2159f62bae50SIngo Molnar 	unsigned int apic_taskpri;
2160f62bae50SIngo Molnar 	unsigned int apic_ldr;
2161f62bae50SIngo Molnar 	unsigned int apic_dfr;
2162f62bae50SIngo Molnar 	unsigned int apic_spiv;
2163f62bae50SIngo Molnar 	unsigned int apic_lvtt;
2164f62bae50SIngo Molnar 	unsigned int apic_lvtpc;
2165f62bae50SIngo Molnar 	unsigned int apic_lvt0;
2166f62bae50SIngo Molnar 	unsigned int apic_lvt1;
2167f62bae50SIngo Molnar 	unsigned int apic_lvterr;
2168f62bae50SIngo Molnar 	unsigned int apic_tmict;
2169f62bae50SIngo Molnar 	unsigned int apic_tdcr;
2170f62bae50SIngo Molnar 	unsigned int apic_thmr;
2171f62bae50SIngo Molnar } apic_pm_state;
2172f62bae50SIngo Molnar 
2173f3c6ea1bSRafael J. Wysocki static int lapic_suspend(void)
2174f62bae50SIngo Molnar {
2175f62bae50SIngo Molnar 	unsigned long flags;
2176f62bae50SIngo Molnar 	int maxlvt;
2177f62bae50SIngo Molnar 
2178f62bae50SIngo Molnar 	if (!apic_pm_state.active)
2179f62bae50SIngo Molnar 		return 0;
2180f62bae50SIngo Molnar 
2181f62bae50SIngo Molnar 	maxlvt = lapic_get_maxlvt();
2182f62bae50SIngo Molnar 
2183f62bae50SIngo Molnar 	apic_pm_state.apic_id = apic_read(APIC_ID);
2184f62bae50SIngo Molnar 	apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
2185f62bae50SIngo Molnar 	apic_pm_state.apic_ldr = apic_read(APIC_LDR);
2186f62bae50SIngo Molnar 	apic_pm_state.apic_dfr = apic_read(APIC_DFR);
2187f62bae50SIngo Molnar 	apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
2188f62bae50SIngo Molnar 	apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
2189f62bae50SIngo Molnar 	if (maxlvt >= 4)
2190f62bae50SIngo Molnar 		apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
2191f62bae50SIngo Molnar 	apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
2192f62bae50SIngo Molnar 	apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
2193f62bae50SIngo Molnar 	apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
2194f62bae50SIngo Molnar 	apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
2195f62bae50SIngo Molnar 	apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
21964efc0670SAndi Kleen #ifdef CONFIG_X86_THERMAL_VECTOR
2197f62bae50SIngo Molnar 	if (maxlvt >= 5)
2198f62bae50SIngo Molnar 		apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
2199f62bae50SIngo Molnar #endif
2200f62bae50SIngo Molnar 
2201f62bae50SIngo Molnar 	local_irq_save(flags);
2202f62bae50SIngo Molnar 	disable_local_APIC();
2203fc1edaf9SSuresh Siddha 
220495a02e97SSuresh Siddha 	if (irq_remapping_enabled)
220595a02e97SSuresh Siddha 		irq_remapping_disable();
2206fc1edaf9SSuresh Siddha 
2207f62bae50SIngo Molnar 	local_irq_restore(flags);
2208f62bae50SIngo Molnar 	return 0;
2209f62bae50SIngo Molnar }
2210f62bae50SIngo Molnar 
2211f3c6ea1bSRafael J. Wysocki static void lapic_resume(void)
2212f62bae50SIngo Molnar {
2213f62bae50SIngo Molnar 	unsigned int l, h;
2214f62bae50SIngo Molnar 	unsigned long flags;
221531dce14aSSuresh Siddha 	int maxlvt;
2216b24696bcSFenghua Yu 
2217f62bae50SIngo Molnar 	if (!apic_pm_state.active)
2218f3c6ea1bSRafael J. Wysocki 		return;
2219f62bae50SIngo Molnar 
2220b24696bcSFenghua Yu 	local_irq_save(flags);
222195a02e97SSuresh Siddha 	if (irq_remapping_enabled) {
222231dce14aSSuresh Siddha 		/*
222331dce14aSSuresh Siddha 		 * IO-APIC and PIC have their own resume routines.
222431dce14aSSuresh Siddha 		 * We just mask them here to make sure the interrupt
222531dce14aSSuresh Siddha 		 * subsystem is completely quiet while we enable x2apic
222631dce14aSSuresh Siddha 		 * and interrupt-remapping.
222731dce14aSSuresh Siddha 		 */
222831dce14aSSuresh Siddha 		mask_ioapic_entries();
2229b81bb373SJacob Pan 		legacy_pic->mask_all();
2230b24696bcSFenghua Yu 	}
2231f62bae50SIngo Molnar 
2232fc1edaf9SSuresh Siddha 	if (x2apic_mode)
2233f62bae50SIngo Molnar 		enable_x2apic();
2234cf6567feSSuresh Siddha 	else {
2235f62bae50SIngo Molnar 		/*
2236f62bae50SIngo Molnar 		 * Make sure the APICBASE points to the right address
2237f62bae50SIngo Molnar 		 *
2238f62bae50SIngo Molnar 		 * FIXME! This will be wrong if we ever support suspend on
2239f62bae50SIngo Molnar 		 * SMP! We'll need to do this as part of the CPU restore!
2240f62bae50SIngo Molnar 		 */
2241cbf2829bSBryan O'Donoghue 		if (boot_cpu_data.x86 >= 6) {
2242f62bae50SIngo Molnar 			rdmsr(MSR_IA32_APICBASE, l, h);
2243f62bae50SIngo Molnar 			l &= ~MSR_IA32_APICBASE_BASE;
2244f62bae50SIngo Molnar 			l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
2245f62bae50SIngo Molnar 			wrmsr(MSR_IA32_APICBASE, l, h);
2246f62bae50SIngo Molnar 		}
2247cbf2829bSBryan O'Donoghue 	}
2248f62bae50SIngo Molnar 
2249b24696bcSFenghua Yu 	maxlvt = lapic_get_maxlvt();
2250f62bae50SIngo Molnar 	apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
2251f62bae50SIngo Molnar 	apic_write(APIC_ID, apic_pm_state.apic_id);
2252f62bae50SIngo Molnar 	apic_write(APIC_DFR, apic_pm_state.apic_dfr);
2253f62bae50SIngo Molnar 	apic_write(APIC_LDR, apic_pm_state.apic_ldr);
2254f62bae50SIngo Molnar 	apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
2255f62bae50SIngo Molnar 	apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
2256f62bae50SIngo Molnar 	apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
2257f62bae50SIngo Molnar 	apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
2258f62bae50SIngo Molnar #if defined(CONFIG_X86_MCE_P4THERMAL) || defined(CONFIG_X86_MCE_INTEL)
2259f62bae50SIngo Molnar 	if (maxlvt >= 5)
2260f62bae50SIngo Molnar 		apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
2261f62bae50SIngo Molnar #endif
2262f62bae50SIngo Molnar 	if (maxlvt >= 4)
2263f62bae50SIngo Molnar 		apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
2264f62bae50SIngo Molnar 	apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
2265f62bae50SIngo Molnar 	apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
2266f62bae50SIngo Molnar 	apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
2267f62bae50SIngo Molnar 	apic_write(APIC_ESR, 0);
2268f62bae50SIngo Molnar 	apic_read(APIC_ESR);
2269f62bae50SIngo Molnar 	apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
2270f62bae50SIngo Molnar 	apic_write(APIC_ESR, 0);
2271f62bae50SIngo Molnar 	apic_read(APIC_ESR);
2272f62bae50SIngo Molnar 
227395a02e97SSuresh Siddha 	if (irq_remapping_enabled)
227495a02e97SSuresh Siddha 		irq_remapping_reenable(x2apic_mode);
227531dce14aSSuresh Siddha 
2276f62bae50SIngo Molnar 	local_irq_restore(flags);
2277f62bae50SIngo Molnar }
2278f62bae50SIngo Molnar 
2279f62bae50SIngo Molnar /*
2280f62bae50SIngo Molnar  * This device has no shutdown method - fully functioning local APICs
2281f62bae50SIngo Molnar  * are needed on every CPU up until machine_halt/restart/poweroff.
2282f62bae50SIngo Molnar  */
2283f62bae50SIngo Molnar 
2284f3c6ea1bSRafael J. Wysocki static struct syscore_ops lapic_syscore_ops = {
2285f62bae50SIngo Molnar 	.resume		= lapic_resume,
2286f62bae50SIngo Molnar 	.suspend	= lapic_suspend,
2287f62bae50SIngo Molnar };
2288f62bae50SIngo Molnar 
2289f62bae50SIngo Molnar static void __cpuinit apic_pm_activate(void)
2290f62bae50SIngo Molnar {
2291f62bae50SIngo Molnar 	apic_pm_state.active = 1;
2292f62bae50SIngo Molnar }
2293f62bae50SIngo Molnar 
2294f62bae50SIngo Molnar static int __init init_lapic_sysfs(void)
2295f62bae50SIngo Molnar {
2296f62bae50SIngo Molnar 	/* XXX: remove suspend/resume procs if !apic_pm_state.active? */
2297f3c6ea1bSRafael J. Wysocki 	if (cpu_has_apic)
2298f3c6ea1bSRafael J. Wysocki 		register_syscore_ops(&lapic_syscore_ops);
2299f62bae50SIngo Molnar 
2300f3c6ea1bSRafael J. Wysocki 	return 0;
2301f62bae50SIngo Molnar }
2302b24696bcSFenghua Yu 
2303b24696bcSFenghua Yu /* local apic needs to resume before other devices access its registers. */
2304b24696bcSFenghua Yu core_initcall(init_lapic_sysfs);
2305f62bae50SIngo Molnar 
2306f62bae50SIngo Molnar #else	/* CONFIG_PM */
2307f62bae50SIngo Molnar 
2308f62bae50SIngo Molnar static void apic_pm_activate(void) { }
2309f62bae50SIngo Molnar 
2310f62bae50SIngo Molnar #endif	/* CONFIG_PM */
2311f62bae50SIngo Molnar 
2312f62bae50SIngo Molnar #ifdef CONFIG_X86_64
2313e0e42142SYinghai Lu 
2314e0e42142SYinghai Lu static int __cpuinit apic_cluster_num(void)
2315f62bae50SIngo Molnar {
2316f62bae50SIngo Molnar 	int i, clusters, zeros;
2317f62bae50SIngo Molnar 	unsigned id;
2318f62bae50SIngo Molnar 	u16 *bios_cpu_apicid;
2319f62bae50SIngo Molnar 	DECLARE_BITMAP(clustermap, NUM_APIC_CLUSTERS);
2320f62bae50SIngo Molnar 
2321f62bae50SIngo Molnar 	bios_cpu_apicid = early_per_cpu_ptr(x86_bios_cpu_apicid);
2322f62bae50SIngo Molnar 	bitmap_zero(clustermap, NUM_APIC_CLUSTERS);
2323f62bae50SIngo Molnar 
2324f62bae50SIngo Molnar 	for (i = 0; i < nr_cpu_ids; i++) {
2325f62bae50SIngo Molnar 		/* are we being called early in kernel startup? */
2326f62bae50SIngo Molnar 		if (bios_cpu_apicid) {
2327f62bae50SIngo Molnar 			id = bios_cpu_apicid[i];
2328f62bae50SIngo Molnar 		} else if (i < nr_cpu_ids) {
2329f62bae50SIngo Molnar 			if (cpu_present(i))
2330f62bae50SIngo Molnar 				id = per_cpu(x86_bios_cpu_apicid, i);
2331f62bae50SIngo Molnar 			else
2332f62bae50SIngo Molnar 				continue;
2333f62bae50SIngo Molnar 		} else
2334f62bae50SIngo Molnar 			break;
2335f62bae50SIngo Molnar 
2336f62bae50SIngo Molnar 		if (id != BAD_APICID)
2337f62bae50SIngo Molnar 			__set_bit(APIC_CLUSTERID(id), clustermap);
2338f62bae50SIngo Molnar 	}
2339f62bae50SIngo Molnar 
2340f62bae50SIngo Molnar 	/* Problem:  Partially populated chassis may not have CPUs in some of
2341f62bae50SIngo Molnar 	 * the APIC clusters they have been allocated.  Only present CPUs have
2342f62bae50SIngo Molnar 	 * x86_bios_cpu_apicid entries, thus causing zeroes in the bitmap.
2343f62bae50SIngo Molnar 	 * Since clusters are allocated sequentially, count zeros only if
2344f62bae50SIngo Molnar 	 * they are bounded by ones.
2345f62bae50SIngo Molnar 	 */
2346f62bae50SIngo Molnar 	clusters = 0;
2347f62bae50SIngo Molnar 	zeros = 0;
2348f62bae50SIngo Molnar 	for (i = 0; i < NUM_APIC_CLUSTERS; i++) {
2349f62bae50SIngo Molnar 		if (test_bit(i, clustermap)) {
2350f62bae50SIngo Molnar 			clusters += 1 + zeros;
2351f62bae50SIngo Molnar 			zeros = 0;
2352f62bae50SIngo Molnar 		} else
2353f62bae50SIngo Molnar 			++zeros;
2354f62bae50SIngo Molnar 	}
2355f62bae50SIngo Molnar 
2356e0e42142SYinghai Lu 	return clusters;
2357e0e42142SYinghai Lu }
2358e0e42142SYinghai Lu 
2359e0e42142SYinghai Lu static int __cpuinitdata multi_checked;
2360e0e42142SYinghai Lu static int __cpuinitdata multi;
2361e0e42142SYinghai Lu 
2362e0e42142SYinghai Lu static int __cpuinit set_multi(const struct dmi_system_id *d)
2363e0e42142SYinghai Lu {
2364e0e42142SYinghai Lu 	if (multi)
2365e0e42142SYinghai Lu 		return 0;
23666f0aced6SCyrill Gorcunov 	pr_info("APIC: %s detected, Multi Chassis\n", d->ident);
2367e0e42142SYinghai Lu 	multi = 1;
2368e0e42142SYinghai Lu 	return 0;
2369e0e42142SYinghai Lu }
2370e0e42142SYinghai Lu 
2371e0e42142SYinghai Lu static const __cpuinitconst struct dmi_system_id multi_dmi_table[] = {
2372e0e42142SYinghai Lu 	{
2373e0e42142SYinghai Lu 		.callback = set_multi,
2374e0e42142SYinghai Lu 		.ident = "IBM System Summit2",
2375e0e42142SYinghai Lu 		.matches = {
2376e0e42142SYinghai Lu 			DMI_MATCH(DMI_SYS_VENDOR, "IBM"),
2377e0e42142SYinghai Lu 			DMI_MATCH(DMI_PRODUCT_NAME, "Summit2"),
2378e0e42142SYinghai Lu 		},
2379e0e42142SYinghai Lu 	},
2380e0e42142SYinghai Lu 	{}
2381e0e42142SYinghai Lu };
2382e0e42142SYinghai Lu 
2383e0e42142SYinghai Lu static void __cpuinit dmi_check_multi(void)
2384e0e42142SYinghai Lu {
2385e0e42142SYinghai Lu 	if (multi_checked)
2386e0e42142SYinghai Lu 		return;
2387e0e42142SYinghai Lu 
2388e0e42142SYinghai Lu 	dmi_check_system(multi_dmi_table);
2389e0e42142SYinghai Lu 	multi_checked = 1;
2390e0e42142SYinghai Lu }
2391f62bae50SIngo Molnar 
2392f62bae50SIngo Molnar /*
2393e0e42142SYinghai Lu  * apic_is_clustered_box() -- Check if we can expect good TSC
2394e0e42142SYinghai Lu  *
2395e0e42142SYinghai Lu  * Thus far, the major user of this is IBM's Summit2 series:
2396e0e42142SYinghai Lu  * Clustered boxes may have unsynced TSC problems if they are
2397e0e42142SYinghai Lu  * multi-chassis.
2398e0e42142SYinghai Lu  * Use DMI to check them
2399f62bae50SIngo Molnar  */
2400e0e42142SYinghai Lu __cpuinit int apic_is_clustered_box(void)
2401e0e42142SYinghai Lu {
2402e0e42142SYinghai Lu 	dmi_check_multi();
2403e0e42142SYinghai Lu 	if (multi)
2404e0e42142SYinghai Lu 		return 1;
2405e0e42142SYinghai Lu 
2406e0e42142SYinghai Lu 	if (!is_vsmp_box())
2407e0e42142SYinghai Lu 		return 0;
2408e0e42142SYinghai Lu 
2409e0e42142SYinghai Lu 	/*
2410e0e42142SYinghai Lu 	 * ScaleMP vSMPowered boxes have one cluster per board and TSCs are
2411e0e42142SYinghai Lu 	 * not guaranteed to be synced between boards
2412e0e42142SYinghai Lu 	 */
2413e0e42142SYinghai Lu 	if (apic_cluster_num() > 1)
2414e0e42142SYinghai Lu 		return 1;
2415e0e42142SYinghai Lu 
2416e0e42142SYinghai Lu 	return 0;
2417f62bae50SIngo Molnar }
2418f62bae50SIngo Molnar #endif
2419f62bae50SIngo Molnar 
2420f62bae50SIngo Molnar /*
2421f62bae50SIngo Molnar  * APIC command line parameters
2422f62bae50SIngo Molnar  */
2423f62bae50SIngo Molnar static int __init setup_disableapic(char *arg)
2424f62bae50SIngo Molnar {
2425f62bae50SIngo Molnar 	disable_apic = 1;
2426f62bae50SIngo Molnar 	setup_clear_cpu_cap(X86_FEATURE_APIC);
2427f62bae50SIngo Molnar 	return 0;
2428f62bae50SIngo Molnar }
2429f62bae50SIngo Molnar early_param("disableapic", setup_disableapic);
2430f62bae50SIngo Molnar 
2431f62bae50SIngo Molnar /* same as disableapic, for compatibility */
2432f62bae50SIngo Molnar static int __init setup_nolapic(char *arg)
2433f62bae50SIngo Molnar {
2434f62bae50SIngo Molnar 	return setup_disableapic(arg);
2435f62bae50SIngo Molnar }
2436f62bae50SIngo Molnar early_param("nolapic", setup_nolapic);
2437f62bae50SIngo Molnar 
2438f62bae50SIngo Molnar static int __init parse_lapic_timer_c2_ok(char *arg)
2439f62bae50SIngo Molnar {
2440f62bae50SIngo Molnar 	local_apic_timer_c2_ok = 1;
2441f62bae50SIngo Molnar 	return 0;
2442f62bae50SIngo Molnar }
2443f62bae50SIngo Molnar early_param("lapic_timer_c2_ok", parse_lapic_timer_c2_ok);
2444f62bae50SIngo Molnar 
2445f62bae50SIngo Molnar static int __init parse_disable_apic_timer(char *arg)
2446f62bae50SIngo Molnar {
2447f62bae50SIngo Molnar 	disable_apic_timer = 1;
2448f62bae50SIngo Molnar 	return 0;
2449f62bae50SIngo Molnar }
2450f62bae50SIngo Molnar early_param("noapictimer", parse_disable_apic_timer);
2451f62bae50SIngo Molnar 
2452f62bae50SIngo Molnar static int __init parse_nolapic_timer(char *arg)
2453f62bae50SIngo Molnar {
2454f62bae50SIngo Molnar 	disable_apic_timer = 1;
2455f62bae50SIngo Molnar 	return 0;
2456f62bae50SIngo Molnar }
2457f62bae50SIngo Molnar early_param("nolapic_timer", parse_nolapic_timer);
2458f62bae50SIngo Molnar 
2459f62bae50SIngo Molnar static int __init apic_set_verbosity(char *arg)
2460f62bae50SIngo Molnar {
2461f62bae50SIngo Molnar 	if (!arg)  {
2462f62bae50SIngo Molnar #ifdef CONFIG_X86_64
2463f62bae50SIngo Molnar 		skip_ioapic_setup = 0;
2464f62bae50SIngo Molnar 		return 0;
2465f62bae50SIngo Molnar #endif
2466f62bae50SIngo Molnar 		return -EINVAL;
2467f62bae50SIngo Molnar 	}
2468f62bae50SIngo Molnar 
2469f62bae50SIngo Molnar 	if (strcmp("debug", arg) == 0)
2470f62bae50SIngo Molnar 		apic_verbosity = APIC_DEBUG;
2471f62bae50SIngo Molnar 	else if (strcmp("verbose", arg) == 0)
2472f62bae50SIngo Molnar 		apic_verbosity = APIC_VERBOSE;
2473f62bae50SIngo Molnar 	else {
2474f62bae50SIngo Molnar 		pr_warning("APIC Verbosity level %s not recognised"
2475f62bae50SIngo Molnar 			" use apic=verbose or apic=debug\n", arg);
2476f62bae50SIngo Molnar 		return -EINVAL;
2477f62bae50SIngo Molnar 	}
2478f62bae50SIngo Molnar 
2479f62bae50SIngo Molnar 	return 0;
2480f62bae50SIngo Molnar }
2481f62bae50SIngo Molnar early_param("apic", apic_set_verbosity);
2482f62bae50SIngo Molnar 
2483f62bae50SIngo Molnar static int __init lapic_insert_resource(void)
2484f62bae50SIngo Molnar {
2485f62bae50SIngo Molnar 	if (!apic_phys)
2486f62bae50SIngo Molnar 		return -1;
2487f62bae50SIngo Molnar 
2488f62bae50SIngo Molnar 	/* Put local APIC into the resource map. */
2489f62bae50SIngo Molnar 	lapic_resource.start = apic_phys;
2490f62bae50SIngo Molnar 	lapic_resource.end = lapic_resource.start + PAGE_SIZE - 1;
2491f62bae50SIngo Molnar 	insert_resource(&iomem_resource, &lapic_resource);
2492f62bae50SIngo Molnar 
2493f62bae50SIngo Molnar 	return 0;
2494f62bae50SIngo Molnar }
2495f62bae50SIngo Molnar 
2496f62bae50SIngo Molnar /*
2497f62bae50SIngo Molnar  * need call insert after e820_reserve_resources()
2498f62bae50SIngo Molnar  * that is using request_resource
2499f62bae50SIngo Molnar  */
2500f62bae50SIngo Molnar late_initcall(lapic_insert_resource);
2501