xref: /openbmc/linux/arch/x86/kernel/apic/apic.c (revision b47dcbdc)
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 
3883ab8514SSteven Rostedt (Red Hat) #include <asm/trace/irq_vectors.h>
398a8f422dSSuresh Siddha #include <asm/irq_remapping.h>
40cdd6c482SIngo Molnar #include <asm/perf_event.h>
41736decacSThomas Gleixner #include <asm/x86_init.h>
42f62bae50SIngo Molnar #include <asm/pgalloc.h>
4360063497SArun Sharma #include <linux/atomic.h>
44f62bae50SIngo Molnar #include <asm/mpspec.h>
45f62bae50SIngo Molnar #include <asm/i8259.h>
46f62bae50SIngo Molnar #include <asm/proto.h>
47f62bae50SIngo Molnar #include <asm/apic.h>
487167d08eSHenrik Kretzschmar #include <asm/io_apic.h>
49f62bae50SIngo Molnar #include <asm/desc.h>
50f62bae50SIngo Molnar #include <asm/hpet.h>
51f62bae50SIngo Molnar #include <asm/idle.h>
52f62bae50SIngo Molnar #include <asm/mtrr.h>
5316f871bcSRalf Baechle #include <asm/time.h>
54f62bae50SIngo Molnar #include <asm/smp.h>
55638bee71SH. Peter Anvin #include <asm/mce.h>
568c3ba8d0SKerstin Jonsson #include <asm/tsc.h>
572904ed8dSSheng Yang #include <asm/hypervisor.h>
58f62bae50SIngo Molnar 
59f62bae50SIngo Molnar unsigned int num_processors;
60f62bae50SIngo Molnar 
61148f9bb8SPaul Gortmaker unsigned disabled_cpus;
62f62bae50SIngo Molnar 
63f62bae50SIngo Molnar /* Processor that is doing the boot up */
64f62bae50SIngo Molnar unsigned int boot_cpu_physical_apicid = -1U;
65cc08e04cSDavid Rientjes EXPORT_SYMBOL_GPL(boot_cpu_physical_apicid);
66f62bae50SIngo Molnar 
67f62bae50SIngo Molnar /*
68f62bae50SIngo Molnar  * The highest APIC ID seen during enumeration.
69f62bae50SIngo Molnar  */
70a491cc90SJiang Liu static unsigned int max_physical_apicid;
71f62bae50SIngo Molnar 
72f62bae50SIngo Molnar /*
73f62bae50SIngo Molnar  * Bitmask of physically existing CPUs:
74f62bae50SIngo Molnar  */
75f62bae50SIngo Molnar physid_mask_t phys_cpu_present_map;
76f62bae50SIngo Molnar 
77f62bae50SIngo Molnar /*
78151e0c7dSHATAYAMA Daisuke  * Processor to be disabled specified by kernel parameter
79151e0c7dSHATAYAMA Daisuke  * disable_cpu_apicid=<int>, mostly used for the kdump 2nd kernel to
80151e0c7dSHATAYAMA Daisuke  * avoid undefined behaviour caused by sending INIT from AP to BSP.
81151e0c7dSHATAYAMA Daisuke  */
825b4d1dbcSH. Peter Anvin static unsigned int disabled_cpu_apicid __read_mostly = BAD_APICID;
83151e0c7dSHATAYAMA Daisuke 
84151e0c7dSHATAYAMA Daisuke /*
85f62bae50SIngo Molnar  * Map cpu index to physical APIC ID
86f62bae50SIngo Molnar  */
870816b0f0SVlad Zolotarov DEFINE_EARLY_PER_CPU_READ_MOSTLY(u16, x86_cpu_to_apicid, BAD_APICID);
880816b0f0SVlad Zolotarov DEFINE_EARLY_PER_CPU_READ_MOSTLY(u16, x86_bios_cpu_apicid, BAD_APICID);
89f62bae50SIngo Molnar EXPORT_EARLY_PER_CPU_SYMBOL(x86_cpu_to_apicid);
90f62bae50SIngo Molnar EXPORT_EARLY_PER_CPU_SYMBOL(x86_bios_cpu_apicid);
91f62bae50SIngo Molnar 
92f62bae50SIngo Molnar #ifdef CONFIG_X86_32
934c321ff8STejun Heo 
944c321ff8STejun Heo /*
954c321ff8STejun Heo  * On x86_32, the mapping between cpu and logical apicid may vary
964c321ff8STejun Heo  * depending on apic in use.  The following early percpu variable is
974c321ff8STejun Heo  * used for the mapping.  This is where the behaviors of x86_64 and 32
984c321ff8STejun Heo  * actually diverge.  Let's keep it ugly for now.
994c321ff8STejun Heo  */
1000816b0f0SVlad Zolotarov DEFINE_EARLY_PER_CPU_READ_MOSTLY(int, x86_cpu_to_logical_apicid, BAD_APICID);
1014c321ff8STejun Heo 
102f62bae50SIngo Molnar /* Local APIC was disabled by the BIOS and enabled by the kernel */
103f62bae50SIngo Molnar static int enabled_via_apicbase;
104f62bae50SIngo Molnar 
105c0eaa453SCyrill Gorcunov /*
106c0eaa453SCyrill Gorcunov  * Handle interrupt mode configuration register (IMCR).
107c0eaa453SCyrill Gorcunov  * This register controls whether the interrupt signals
108c0eaa453SCyrill Gorcunov  * that reach the BSP come from the master PIC or from the
109c0eaa453SCyrill Gorcunov  * local APIC. Before entering Symmetric I/O Mode, either
110c0eaa453SCyrill Gorcunov  * the BIOS or the operating system must switch out of
111c0eaa453SCyrill Gorcunov  * PIC Mode by changing the IMCR.
112c0eaa453SCyrill Gorcunov  */
1135cda395fSAlexander van Heukelum static inline void imcr_pic_to_apic(void)
114c0eaa453SCyrill Gorcunov {
115c0eaa453SCyrill Gorcunov 	/* select IMCR register */
116c0eaa453SCyrill Gorcunov 	outb(0x70, 0x22);
117c0eaa453SCyrill Gorcunov 	/* NMI and 8259 INTR go through APIC */
118c0eaa453SCyrill Gorcunov 	outb(0x01, 0x23);
119c0eaa453SCyrill Gorcunov }
120c0eaa453SCyrill Gorcunov 
1215cda395fSAlexander van Heukelum static inline void imcr_apic_to_pic(void)
122c0eaa453SCyrill Gorcunov {
123c0eaa453SCyrill Gorcunov 	/* select IMCR register */
124c0eaa453SCyrill Gorcunov 	outb(0x70, 0x22);
125c0eaa453SCyrill Gorcunov 	/* NMI and 8259 INTR go directly to BSP */
126c0eaa453SCyrill Gorcunov 	outb(0x00, 0x23);
127c0eaa453SCyrill Gorcunov }
128f62bae50SIngo Molnar #endif
129f62bae50SIngo Molnar 
130279f1461SSuresh Siddha /*
131279f1461SSuresh Siddha  * Knob to control our willingness to enable the local APIC.
132279f1461SSuresh Siddha  *
133279f1461SSuresh Siddha  * +1=force-enable
134279f1461SSuresh Siddha  */
135279f1461SSuresh Siddha static int force_enable_local_apic __initdata;
136dc9788f4SDavid Rientjes 
137dc9788f4SDavid Rientjes /* Control whether x2APIC mode is enabled or not */
138dc9788f4SDavid Rientjes static bool nox2apic __initdata;
139dc9788f4SDavid Rientjes 
140279f1461SSuresh Siddha /*
141279f1461SSuresh Siddha  * APIC command line parameters
142279f1461SSuresh Siddha  */
143279f1461SSuresh Siddha static int __init parse_lapic(char *arg)
144279f1461SSuresh Siddha {
145279f1461SSuresh Siddha 	if (config_enabled(CONFIG_X86_32) && !arg)
146279f1461SSuresh Siddha 		force_enable_local_apic = 1;
14727cf9298SMathias Krause 	else if (arg && !strncmp(arg, "notscdeadline", 13))
148279f1461SSuresh Siddha 		setup_clear_cpu_cap(X86_FEATURE_TSC_DEADLINE_TIMER);
149279f1461SSuresh Siddha 	return 0;
150279f1461SSuresh Siddha }
151279f1461SSuresh Siddha early_param("lapic", parse_lapic);
152279f1461SSuresh Siddha 
153f62bae50SIngo Molnar #ifdef CONFIG_X86_64
154f62bae50SIngo Molnar static int apic_calibrate_pmtmr __initdata;
155f62bae50SIngo Molnar static __init int setup_apicpmtimer(char *s)
156f62bae50SIngo Molnar {
157f62bae50SIngo Molnar 	apic_calibrate_pmtmr = 1;
158f62bae50SIngo Molnar 	notsc_setup(NULL);
159f62bae50SIngo Molnar 	return 0;
160f62bae50SIngo Molnar }
161f62bae50SIngo Molnar __setup("apicpmtimer", setup_apicpmtimer);
162f62bae50SIngo Molnar #endif
163f62bae50SIngo Molnar 
164fc1edaf9SSuresh Siddha int x2apic_mode;
165f62bae50SIngo Molnar #ifdef CONFIG_X86_X2APIC
166f62bae50SIngo Molnar /* x2apic enabled before OS handover */
167fb209bd8SYinghai Lu int x2apic_preenabled;
168fb209bd8SYinghai Lu static int x2apic_disabled;
169dc9788f4SDavid Rientjes static int __init setup_nox2apic(char *str)
170f62bae50SIngo Molnar {
17139d83a5dSSuresh Siddha 	if (x2apic_enabled()) {
172a31bc327SYinghai Lu 		int apicid = native_apic_msr_read(APIC_ID);
173a31bc327SYinghai Lu 
174a31bc327SYinghai Lu 		if (apicid >= 255) {
175a31bc327SYinghai Lu 			pr_warning("Apicid: %08x, cannot enforce nox2apic\n",
176a31bc327SYinghai Lu 				   apicid);
17739d83a5dSSuresh Siddha 			return 0;
17839d83a5dSSuresh Siddha 		}
17939d83a5dSSuresh Siddha 
180a31bc327SYinghai Lu 		pr_warning("x2apic already enabled. will disable it\n");
181a31bc327SYinghai Lu 	} else
182f62bae50SIngo Molnar 		setup_clear_cpu_cap(X86_FEATURE_X2APIC);
183a31bc327SYinghai Lu 
184dc9788f4SDavid Rientjes 	nox2apic = true;
185a31bc327SYinghai Lu 
186f62bae50SIngo Molnar 	return 0;
187f62bae50SIngo Molnar }
188f62bae50SIngo Molnar early_param("nox2apic", setup_nox2apic);
189f62bae50SIngo Molnar #endif
190f62bae50SIngo Molnar 
191f62bae50SIngo Molnar unsigned long mp_lapic_addr;
192f62bae50SIngo Molnar int disable_apic;
193f62bae50SIngo Molnar /* Disable local APIC timer from the kernel commandline or via dmi quirk */
19425874a29SHenrik Kretzschmar static int disable_apic_timer __initdata;
195f62bae50SIngo Molnar /* Local APIC timer works in C2 */
196f62bae50SIngo Molnar int local_apic_timer_c2_ok;
197f62bae50SIngo Molnar EXPORT_SYMBOL_GPL(local_apic_timer_c2_ok);
198f62bae50SIngo Molnar 
199f62bae50SIngo Molnar int first_system_vector = 0xfe;
200f62bae50SIngo Molnar 
201f62bae50SIngo Molnar /*
202f62bae50SIngo Molnar  * Debug level, exported for io_apic.c
203f62bae50SIngo Molnar  */
204f62bae50SIngo Molnar unsigned int apic_verbosity;
205f62bae50SIngo Molnar 
206f62bae50SIngo Molnar int pic_mode;
207f62bae50SIngo Molnar 
208f62bae50SIngo Molnar /* Have we found an MP table */
209f62bae50SIngo Molnar int smp_found_config;
210f62bae50SIngo Molnar 
211f62bae50SIngo Molnar static struct resource lapic_resource = {
212f62bae50SIngo Molnar 	.name = "Local APIC",
213f62bae50SIngo Molnar 	.flags = IORESOURCE_MEM | IORESOURCE_BUSY,
214f62bae50SIngo Molnar };
215f62bae50SIngo Molnar 
2161ade93efSJacob Pan unsigned int lapic_timer_frequency = 0;
217f62bae50SIngo Molnar 
218f62bae50SIngo Molnar static void apic_pm_activate(void);
219f62bae50SIngo Molnar 
220f62bae50SIngo Molnar static unsigned long apic_phys;
221f62bae50SIngo Molnar 
222f62bae50SIngo Molnar /*
223f62bae50SIngo Molnar  * Get the LAPIC version
224f62bae50SIngo Molnar  */
225f62bae50SIngo Molnar static inline int lapic_get_version(void)
226f62bae50SIngo Molnar {
227f62bae50SIngo Molnar 	return GET_APIC_VERSION(apic_read(APIC_LVR));
228f62bae50SIngo Molnar }
229f62bae50SIngo Molnar 
230f62bae50SIngo Molnar /*
231f62bae50SIngo Molnar  * Check, if the APIC is integrated or a separate chip
232f62bae50SIngo Molnar  */
233f62bae50SIngo Molnar static inline int lapic_is_integrated(void)
234f62bae50SIngo Molnar {
235f62bae50SIngo Molnar #ifdef CONFIG_X86_64
236f62bae50SIngo Molnar 	return 1;
237f62bae50SIngo Molnar #else
238f62bae50SIngo Molnar 	return APIC_INTEGRATED(lapic_get_version());
239f62bae50SIngo Molnar #endif
240f62bae50SIngo Molnar }
241f62bae50SIngo Molnar 
242f62bae50SIngo Molnar /*
243f62bae50SIngo Molnar  * Check, whether this is a modern or a first generation APIC
244f62bae50SIngo Molnar  */
245f62bae50SIngo Molnar static int modern_apic(void)
246f62bae50SIngo Molnar {
247f62bae50SIngo Molnar 	/* AMD systems use old APIC versions, so check the CPU */
248f62bae50SIngo Molnar 	if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
249f62bae50SIngo Molnar 	    boot_cpu_data.x86 >= 0xf)
250f62bae50SIngo Molnar 		return 1;
251f62bae50SIngo Molnar 	return lapic_get_version() >= 0x14;
252f62bae50SIngo Molnar }
253f62bae50SIngo Molnar 
25408306ce6SCyrill Gorcunov /*
255a933c618SCyrill Gorcunov  * right after this call apic become NOOP driven
256a933c618SCyrill Gorcunov  * so apic->write/read doesn't do anything
25708306ce6SCyrill Gorcunov  */
25825874a29SHenrik Kretzschmar static void __init apic_disable(void)
25908306ce6SCyrill Gorcunov {
260f88f2b4fSCyrill Gorcunov 	pr_info("APIC: switched to apic NOOP\n");
261a933c618SCyrill Gorcunov 	apic = &apic_noop;
26208306ce6SCyrill Gorcunov }
26308306ce6SCyrill Gorcunov 
264f62bae50SIngo Molnar void native_apic_wait_icr_idle(void)
265f62bae50SIngo Molnar {
266f62bae50SIngo Molnar 	while (apic_read(APIC_ICR) & APIC_ICR_BUSY)
267f62bae50SIngo Molnar 		cpu_relax();
268f62bae50SIngo Molnar }
269f62bae50SIngo Molnar 
270f62bae50SIngo Molnar u32 native_safe_apic_wait_icr_idle(void)
271f62bae50SIngo Molnar {
272f62bae50SIngo Molnar 	u32 send_status;
273f62bae50SIngo Molnar 	int timeout;
274f62bae50SIngo Molnar 
275f62bae50SIngo Molnar 	timeout = 0;
276f62bae50SIngo Molnar 	do {
277f62bae50SIngo Molnar 		send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
278f62bae50SIngo Molnar 		if (!send_status)
279f62bae50SIngo Molnar 			break;
280b49d7d87SFernando Luis Vazquez Cao 		inc_irq_stat(icr_read_retry_count);
281f62bae50SIngo Molnar 		udelay(100);
282f62bae50SIngo Molnar 	} while (timeout++ < 1000);
283f62bae50SIngo Molnar 
284f62bae50SIngo Molnar 	return send_status;
285f62bae50SIngo Molnar }
286f62bae50SIngo Molnar 
287f62bae50SIngo Molnar void native_apic_icr_write(u32 low, u32 id)
288f62bae50SIngo Molnar {
289ea7bdc65SJan Kiszka 	unsigned long flags;
290ea7bdc65SJan Kiszka 
291ea7bdc65SJan Kiszka 	local_irq_save(flags);
292f62bae50SIngo Molnar 	apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(id));
293f62bae50SIngo Molnar 	apic_write(APIC_ICR, low);
294ea7bdc65SJan Kiszka 	local_irq_restore(flags);
295f62bae50SIngo Molnar }
296f62bae50SIngo Molnar 
297f62bae50SIngo Molnar u64 native_apic_icr_read(void)
298f62bae50SIngo Molnar {
299f62bae50SIngo Molnar 	u32 icr1, icr2;
300f62bae50SIngo Molnar 
301f62bae50SIngo Molnar 	icr2 = apic_read(APIC_ICR2);
302f62bae50SIngo Molnar 	icr1 = apic_read(APIC_ICR);
303f62bae50SIngo Molnar 
304f62bae50SIngo Molnar 	return icr1 | ((u64)icr2 << 32);
305f62bae50SIngo Molnar }
306f62bae50SIngo Molnar 
307f62bae50SIngo Molnar #ifdef CONFIG_X86_32
308f62bae50SIngo Molnar /**
309f62bae50SIngo Molnar  * get_physical_broadcast - Get number of physical broadcast IDs
310f62bae50SIngo Molnar  */
311f62bae50SIngo Molnar int get_physical_broadcast(void)
312f62bae50SIngo Molnar {
313f62bae50SIngo Molnar 	return modern_apic() ? 0xff : 0xf;
314f62bae50SIngo Molnar }
315f62bae50SIngo Molnar #endif
316f62bae50SIngo Molnar 
317f62bae50SIngo Molnar /**
318f62bae50SIngo Molnar  * lapic_get_maxlvt - get the maximum number of local vector table entries
319f62bae50SIngo Molnar  */
320f62bae50SIngo Molnar int lapic_get_maxlvt(void)
321f62bae50SIngo Molnar {
322f62bae50SIngo Molnar 	unsigned int v;
323f62bae50SIngo Molnar 
324f62bae50SIngo Molnar 	v = apic_read(APIC_LVR);
325f62bae50SIngo Molnar 	/*
326f62bae50SIngo Molnar 	 * - we always have APIC integrated on 64bit mode
327f62bae50SIngo Molnar 	 * - 82489DXs do not report # of LVT entries
328f62bae50SIngo Molnar 	 */
329f62bae50SIngo Molnar 	return APIC_INTEGRATED(GET_APIC_VERSION(v)) ? GET_APIC_MAXLVT(v) : 2;
330f62bae50SIngo Molnar }
331f62bae50SIngo Molnar 
332f62bae50SIngo Molnar /*
333f62bae50SIngo Molnar  * Local APIC timer
334f62bae50SIngo Molnar  */
335f62bae50SIngo Molnar 
336f62bae50SIngo Molnar /* Clock divisor */
337f62bae50SIngo Molnar #define APIC_DIVISOR 16
338279f1461SSuresh Siddha #define TSC_DIVISOR  32
339f62bae50SIngo Molnar 
340f62bae50SIngo Molnar /*
341f62bae50SIngo Molnar  * This function sets up the local APIC timer, with a timeout of
342f62bae50SIngo Molnar  * 'clocks' APIC bus clock. During calibration we actually call
343f62bae50SIngo Molnar  * this function twice on the boot CPU, once with a bogus timeout
344f62bae50SIngo Molnar  * value, second time for real. The other (noncalibrating) CPUs
345f62bae50SIngo Molnar  * call this function only once, with the real, calibrated value.
346f62bae50SIngo Molnar  *
347f62bae50SIngo Molnar  * We do reads before writes even if unnecessary, to get around the
348f62bae50SIngo Molnar  * P5 APIC double write bug.
349f62bae50SIngo Molnar  */
350f62bae50SIngo Molnar static void __setup_APIC_LVTT(unsigned int clocks, int oneshot, int irqen)
351f62bae50SIngo Molnar {
352f62bae50SIngo Molnar 	unsigned int lvtt_value, tmp_value;
353f62bae50SIngo Molnar 
354f62bae50SIngo Molnar 	lvtt_value = LOCAL_TIMER_VECTOR;
355f62bae50SIngo Molnar 	if (!oneshot)
356f62bae50SIngo Molnar 		lvtt_value |= APIC_LVT_TIMER_PERIODIC;
357279f1461SSuresh Siddha 	else if (boot_cpu_has(X86_FEATURE_TSC_DEADLINE_TIMER))
358279f1461SSuresh Siddha 		lvtt_value |= APIC_LVT_TIMER_TSCDEADLINE;
359279f1461SSuresh Siddha 
360f62bae50SIngo Molnar 	if (!lapic_is_integrated())
361f62bae50SIngo Molnar 		lvtt_value |= SET_APIC_TIMER_BASE(APIC_TIMER_BASE_DIV);
362f62bae50SIngo Molnar 
363f62bae50SIngo Molnar 	if (!irqen)
364f62bae50SIngo Molnar 		lvtt_value |= APIC_LVT_MASKED;
365f62bae50SIngo Molnar 
366f62bae50SIngo Molnar 	apic_write(APIC_LVTT, lvtt_value);
367f62bae50SIngo Molnar 
368279f1461SSuresh Siddha 	if (lvtt_value & APIC_LVT_TIMER_TSCDEADLINE) {
369279f1461SSuresh Siddha 		printk_once(KERN_DEBUG "TSC deadline timer enabled\n");
370279f1461SSuresh Siddha 		return;
371279f1461SSuresh Siddha 	}
372279f1461SSuresh Siddha 
373f62bae50SIngo Molnar 	/*
374f62bae50SIngo Molnar 	 * Divide PICLK by 16
375f62bae50SIngo Molnar 	 */
376f62bae50SIngo Molnar 	tmp_value = apic_read(APIC_TDCR);
377f62bae50SIngo Molnar 	apic_write(APIC_TDCR,
378f62bae50SIngo Molnar 		(tmp_value & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE)) |
379f62bae50SIngo Molnar 		APIC_TDR_DIV_16);
380f62bae50SIngo Molnar 
381f62bae50SIngo Molnar 	if (!oneshot)
382f62bae50SIngo Molnar 		apic_write(APIC_TMICT, clocks / APIC_DIVISOR);
383f62bae50SIngo Molnar }
384f62bae50SIngo Molnar 
385f62bae50SIngo Molnar /*
386a68c439bSRobert Richter  * Setup extended LVT, AMD specific
387f62bae50SIngo Molnar  *
388a68c439bSRobert Richter  * Software should use the LVT offsets the BIOS provides.  The offsets
389a68c439bSRobert Richter  * are determined by the subsystems using it like those for MCE
390a68c439bSRobert Richter  * threshold or IBS.  On K8 only offset 0 (APIC500) and MCE interrupts
391a68c439bSRobert Richter  * are supported. Beginning with family 10h at least 4 offsets are
392a68c439bSRobert Richter  * available.
393f62bae50SIngo Molnar  *
394a68c439bSRobert Richter  * Since the offsets must be consistent for all cores, we keep track
395a68c439bSRobert Richter  * of the LVT offsets in software and reserve the offset for the same
396a68c439bSRobert Richter  * vector also to be used on other cores. An offset is freed by
397a68c439bSRobert Richter  * setting the entry to APIC_EILVT_MASKED.
398a68c439bSRobert Richter  *
399a68c439bSRobert Richter  * If the BIOS is right, there should be no conflicts. Otherwise a
400a68c439bSRobert Richter  * "[Firmware Bug]: ..." error message is generated. However, if
401a68c439bSRobert Richter  * software does not properly determines the offsets, it is not
402a68c439bSRobert Richter  * necessarily a BIOS bug.
403f62bae50SIngo Molnar  */
404f62bae50SIngo Molnar 
405a68c439bSRobert Richter static atomic_t eilvt_offsets[APIC_EILVT_NR_MAX];
406f62bae50SIngo Molnar 
407a68c439bSRobert Richter static inline int eilvt_entry_is_changeable(unsigned int old, unsigned int new)
408a68c439bSRobert Richter {
409a68c439bSRobert Richter 	return (old & APIC_EILVT_MASKED)
410a68c439bSRobert Richter 		|| (new == APIC_EILVT_MASKED)
411a68c439bSRobert Richter 		|| ((new & ~APIC_EILVT_MASKED) == old);
412a68c439bSRobert Richter }
413a68c439bSRobert Richter 
414a68c439bSRobert Richter static unsigned int reserve_eilvt_offset(int offset, unsigned int new)
415a68c439bSRobert Richter {
4168abc3122SRobert Richter 	unsigned int rsvd, vector;
417a68c439bSRobert Richter 
418a68c439bSRobert Richter 	if (offset >= APIC_EILVT_NR_MAX)
419a68c439bSRobert Richter 		return ~0;
420a68c439bSRobert Richter 
4218abc3122SRobert Richter 	rsvd = atomic_read(&eilvt_offsets[offset]);
422a68c439bSRobert Richter 	do {
4238abc3122SRobert Richter 		vector = rsvd & ~APIC_EILVT_MASKED;	/* 0: unassigned */
4248abc3122SRobert Richter 		if (vector && !eilvt_entry_is_changeable(vector, new))
425a68c439bSRobert Richter 			/* may not change if vectors are different */
426a68c439bSRobert Richter 			return rsvd;
427a68c439bSRobert Richter 		rsvd = atomic_cmpxchg(&eilvt_offsets[offset], rsvd, new);
428a68c439bSRobert Richter 	} while (rsvd != new);
429a68c439bSRobert Richter 
4308abc3122SRobert Richter 	rsvd &= ~APIC_EILVT_MASKED;
4318abc3122SRobert Richter 	if (rsvd && rsvd != vector)
4328abc3122SRobert Richter 		pr_info("LVT offset %d assigned for vector 0x%02x\n",
4338abc3122SRobert Richter 			offset, rsvd);
4348abc3122SRobert Richter 
435a68c439bSRobert Richter 	return new;
436a68c439bSRobert Richter }
437a68c439bSRobert Richter 
438a68c439bSRobert Richter /*
439a68c439bSRobert Richter  * If mask=1, the LVT entry does not generate interrupts while mask=0
440cbf74ceaSRobert Richter  * enables the vector. See also the BKDGs. Must be called with
441cbf74ceaSRobert Richter  * preemption disabled.
442a68c439bSRobert Richter  */
443a68c439bSRobert Richter 
44427afdf20SRobert Richter int setup_APIC_eilvt(u8 offset, u8 vector, u8 msg_type, u8 mask)
445a68c439bSRobert Richter {
446a68c439bSRobert Richter 	unsigned long reg = APIC_EILVTn(offset);
447a68c439bSRobert Richter 	unsigned int new, old, reserved;
448a68c439bSRobert Richter 
449a68c439bSRobert Richter 	new = (mask << 16) | (msg_type << 8) | vector;
450a68c439bSRobert Richter 	old = apic_read(reg);
451a68c439bSRobert Richter 	reserved = reserve_eilvt_offset(offset, new);
452a68c439bSRobert Richter 
453a68c439bSRobert Richter 	if (reserved != new) {
454eb48c9cbSRobert Richter 		pr_err(FW_BUG "cpu %d, try to use APIC%lX (LVT offset %d) for "
455eb48c9cbSRobert Richter 		       "vector 0x%x, but the register is already in use for "
456eb48c9cbSRobert Richter 		       "vector 0x%x on another cpu\n",
457eb48c9cbSRobert Richter 		       smp_processor_id(), reg, offset, new, reserved);
458a68c439bSRobert Richter 		return -EINVAL;
459a68c439bSRobert Richter 	}
460a68c439bSRobert Richter 
461a68c439bSRobert Richter 	if (!eilvt_entry_is_changeable(old, new)) {
462eb48c9cbSRobert Richter 		pr_err(FW_BUG "cpu %d, try to use APIC%lX (LVT offset %d) for "
463eb48c9cbSRobert Richter 		       "vector 0x%x, but the register is already in use for "
464eb48c9cbSRobert Richter 		       "vector 0x%x on this cpu\n",
465eb48c9cbSRobert Richter 		       smp_processor_id(), reg, offset, new, old);
466a68c439bSRobert Richter 		return -EBUSY;
467a68c439bSRobert Richter 	}
468a68c439bSRobert Richter 
469a68c439bSRobert Richter 	apic_write(reg, new);
470a68c439bSRobert Richter 
471a68c439bSRobert Richter 	return 0;
472f62bae50SIngo Molnar }
47327afdf20SRobert Richter EXPORT_SYMBOL_GPL(setup_APIC_eilvt);
474f62bae50SIngo Molnar 
475f62bae50SIngo Molnar /*
476f62bae50SIngo Molnar  * Program the next event, relative to now
477f62bae50SIngo Molnar  */
478f62bae50SIngo Molnar static int lapic_next_event(unsigned long delta,
479f62bae50SIngo Molnar 			    struct clock_event_device *evt)
480f62bae50SIngo Molnar {
481f62bae50SIngo Molnar 	apic_write(APIC_TMICT, delta);
482f62bae50SIngo Molnar 	return 0;
483f62bae50SIngo Molnar }
484f62bae50SIngo Molnar 
485279f1461SSuresh Siddha static int lapic_next_deadline(unsigned long delta,
486279f1461SSuresh Siddha 			       struct clock_event_device *evt)
487279f1461SSuresh Siddha {
488279f1461SSuresh Siddha 	u64 tsc;
489279f1461SSuresh Siddha 
490279f1461SSuresh Siddha 	rdtscll(tsc);
491279f1461SSuresh Siddha 	wrmsrl(MSR_IA32_TSC_DEADLINE, tsc + (((u64) delta) * TSC_DIVISOR));
492279f1461SSuresh Siddha 	return 0;
493279f1461SSuresh Siddha }
494279f1461SSuresh Siddha 
495f62bae50SIngo Molnar /*
496f62bae50SIngo Molnar  * Setup the lapic timer in periodic or oneshot mode
497f62bae50SIngo Molnar  */
498f62bae50SIngo Molnar static void lapic_timer_setup(enum clock_event_mode mode,
499f62bae50SIngo Molnar 			      struct clock_event_device *evt)
500f62bae50SIngo Molnar {
501f62bae50SIngo Molnar 	unsigned long flags;
502f62bae50SIngo Molnar 	unsigned int v;
503f62bae50SIngo Molnar 
504f62bae50SIngo Molnar 	/* Lapic used as dummy for broadcast ? */
505f62bae50SIngo Molnar 	if (evt->features & CLOCK_EVT_FEAT_DUMMY)
506f62bae50SIngo Molnar 		return;
507f62bae50SIngo Molnar 
508f62bae50SIngo Molnar 	local_irq_save(flags);
509f62bae50SIngo Molnar 
510f62bae50SIngo Molnar 	switch (mode) {
511f62bae50SIngo Molnar 	case CLOCK_EVT_MODE_PERIODIC:
512f62bae50SIngo Molnar 	case CLOCK_EVT_MODE_ONESHOT:
5131ade93efSJacob Pan 		__setup_APIC_LVTT(lapic_timer_frequency,
514f62bae50SIngo Molnar 				  mode != CLOCK_EVT_MODE_PERIODIC, 1);
515f62bae50SIngo Molnar 		break;
516f62bae50SIngo Molnar 	case CLOCK_EVT_MODE_UNUSED:
517f62bae50SIngo Molnar 	case CLOCK_EVT_MODE_SHUTDOWN:
518f62bae50SIngo Molnar 		v = apic_read(APIC_LVTT);
519f62bae50SIngo Molnar 		v |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
520f62bae50SIngo Molnar 		apic_write(APIC_LVTT, v);
5216f9b4100SAndreas Herrmann 		apic_write(APIC_TMICT, 0);
522f62bae50SIngo Molnar 		break;
523f62bae50SIngo Molnar 	case CLOCK_EVT_MODE_RESUME:
524f62bae50SIngo Molnar 		/* Nothing to do here */
525f62bae50SIngo Molnar 		break;
526f62bae50SIngo Molnar 	}
527f62bae50SIngo Molnar 
528f62bae50SIngo Molnar 	local_irq_restore(flags);
529f62bae50SIngo Molnar }
530f62bae50SIngo Molnar 
531f62bae50SIngo Molnar /*
532f62bae50SIngo Molnar  * Local APIC timer broadcast function
533f62bae50SIngo Molnar  */
534f62bae50SIngo Molnar static void lapic_timer_broadcast(const struct cpumask *mask)
535f62bae50SIngo Molnar {
536f62bae50SIngo Molnar #ifdef CONFIG_SMP
537f62bae50SIngo Molnar 	apic->send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
538f62bae50SIngo Molnar #endif
539f62bae50SIngo Molnar }
540f62bae50SIngo Molnar 
54125874a29SHenrik Kretzschmar 
54225874a29SHenrik Kretzschmar /*
54325874a29SHenrik Kretzschmar  * The local apic timer can be used for any function which is CPU local.
54425874a29SHenrik Kretzschmar  */
54525874a29SHenrik Kretzschmar static struct clock_event_device lapic_clockevent = {
54625874a29SHenrik Kretzschmar 	.name		= "lapic",
54725874a29SHenrik Kretzschmar 	.features	= CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT
54825874a29SHenrik Kretzschmar 			| CLOCK_EVT_FEAT_C3STOP | CLOCK_EVT_FEAT_DUMMY,
54925874a29SHenrik Kretzschmar 	.shift		= 32,
55025874a29SHenrik Kretzschmar 	.set_mode	= lapic_timer_setup,
55125874a29SHenrik Kretzschmar 	.set_next_event	= lapic_next_event,
55225874a29SHenrik Kretzschmar 	.broadcast	= lapic_timer_broadcast,
55325874a29SHenrik Kretzschmar 	.rating		= 100,
55425874a29SHenrik Kretzschmar 	.irq		= -1,
55525874a29SHenrik Kretzschmar };
55625874a29SHenrik Kretzschmar static DEFINE_PER_CPU(struct clock_event_device, lapic_events);
55725874a29SHenrik Kretzschmar 
558f62bae50SIngo Molnar /*
559421f91d2SUwe Kleine-König  * Setup the local APIC timer for this CPU. Copy the initialized values
560f62bae50SIngo Molnar  * of the boot CPU and register the clock event in the framework.
561f62bae50SIngo Molnar  */
562148f9bb8SPaul Gortmaker static void setup_APIC_timer(void)
563f62bae50SIngo Molnar {
564f62bae50SIngo Molnar 	struct clock_event_device *levt = &__get_cpu_var(lapic_events);
565f62bae50SIngo Molnar 
566349c004eSChristoph Lameter 	if (this_cpu_has(X86_FEATURE_ARAT)) {
567db954b58SVenkatesh Pallipadi 		lapic_clockevent.features &= ~CLOCK_EVT_FEAT_C3STOP;
568db954b58SVenkatesh Pallipadi 		/* Make LAPIC timer preferrable over percpu HPET */
569db954b58SVenkatesh Pallipadi 		lapic_clockevent.rating = 150;
570db954b58SVenkatesh Pallipadi 	}
571db954b58SVenkatesh Pallipadi 
572f62bae50SIngo Molnar 	memcpy(levt, &lapic_clockevent, sizeof(*levt));
573f62bae50SIngo Molnar 	levt->cpumask = cpumask_of(smp_processor_id());
574f62bae50SIngo Molnar 
575279f1461SSuresh Siddha 	if (this_cpu_has(X86_FEATURE_TSC_DEADLINE_TIMER)) {
576279f1461SSuresh Siddha 		levt->features &= ~(CLOCK_EVT_FEAT_PERIODIC |
577279f1461SSuresh Siddha 				    CLOCK_EVT_FEAT_DUMMY);
578279f1461SSuresh Siddha 		levt->set_next_event = lapic_next_deadline;
579279f1461SSuresh Siddha 		clockevents_config_and_register(levt,
580279f1461SSuresh Siddha 						(tsc_khz / TSC_DIVISOR) * 1000,
581279f1461SSuresh Siddha 						0xF, ~0UL);
582279f1461SSuresh Siddha 	} else
583f62bae50SIngo Molnar 		clockevents_register_device(levt);
584f62bae50SIngo Molnar }
585f62bae50SIngo Molnar 
586f62bae50SIngo Molnar /*
587f62bae50SIngo Molnar  * In this functions we calibrate APIC bus clocks to the external timer.
588f62bae50SIngo Molnar  *
589f62bae50SIngo Molnar  * We want to do the calibration only once since we want to have local timer
590f62bae50SIngo Molnar  * irqs syncron. CPUs connected by the same APIC bus have the very same bus
591f62bae50SIngo Molnar  * frequency.
592f62bae50SIngo Molnar  *
593f62bae50SIngo Molnar  * This was previously done by reading the PIT/HPET and waiting for a wrap
594f62bae50SIngo Molnar  * around to find out, that a tick has elapsed. I have a box, where the PIT
595f62bae50SIngo Molnar  * readout is broken, so it never gets out of the wait loop again. This was
596f62bae50SIngo Molnar  * also reported by others.
597f62bae50SIngo Molnar  *
598f62bae50SIngo Molnar  * Monitoring the jiffies value is inaccurate and the clockevents
599f62bae50SIngo Molnar  * infrastructure allows us to do a simple substitution of the interrupt
600f62bae50SIngo Molnar  * handler.
601f62bae50SIngo Molnar  *
602f62bae50SIngo Molnar  * The calibration routine also uses the pm_timer when possible, as the PIT
603f62bae50SIngo Molnar  * happens to run way too slow (factor 2.3 on my VAIO CoreDuo, which goes
604f62bae50SIngo Molnar  * back to normal later in the boot process).
605f62bae50SIngo Molnar  */
606f62bae50SIngo Molnar 
607f62bae50SIngo Molnar #define LAPIC_CAL_LOOPS		(HZ/10)
608f62bae50SIngo Molnar 
609f62bae50SIngo Molnar static __initdata int lapic_cal_loops = -1;
610f62bae50SIngo Molnar static __initdata long lapic_cal_t1, lapic_cal_t2;
611f62bae50SIngo Molnar static __initdata unsigned long long lapic_cal_tsc1, lapic_cal_tsc2;
612f62bae50SIngo Molnar static __initdata unsigned long lapic_cal_pm1, lapic_cal_pm2;
613f62bae50SIngo Molnar static __initdata unsigned long lapic_cal_j1, lapic_cal_j2;
614f62bae50SIngo Molnar 
615f62bae50SIngo Molnar /*
616f62bae50SIngo Molnar  * Temporary interrupt handler.
617f62bae50SIngo Molnar  */
618f62bae50SIngo Molnar static void __init lapic_cal_handler(struct clock_event_device *dev)
619f62bae50SIngo Molnar {
620f62bae50SIngo Molnar 	unsigned long long tsc = 0;
621f62bae50SIngo Molnar 	long tapic = apic_read(APIC_TMCCT);
622f62bae50SIngo Molnar 	unsigned long pm = acpi_pm_read_early();
623f62bae50SIngo Molnar 
624f62bae50SIngo Molnar 	if (cpu_has_tsc)
625f62bae50SIngo Molnar 		rdtscll(tsc);
626f62bae50SIngo Molnar 
627f62bae50SIngo Molnar 	switch (lapic_cal_loops++) {
628f62bae50SIngo Molnar 	case 0:
629f62bae50SIngo Molnar 		lapic_cal_t1 = tapic;
630f62bae50SIngo Molnar 		lapic_cal_tsc1 = tsc;
631f62bae50SIngo Molnar 		lapic_cal_pm1 = pm;
632f62bae50SIngo Molnar 		lapic_cal_j1 = jiffies;
633f62bae50SIngo Molnar 		break;
634f62bae50SIngo Molnar 
635f62bae50SIngo Molnar 	case LAPIC_CAL_LOOPS:
636f62bae50SIngo Molnar 		lapic_cal_t2 = tapic;
637f62bae50SIngo Molnar 		lapic_cal_tsc2 = tsc;
638f62bae50SIngo Molnar 		if (pm < lapic_cal_pm1)
639f62bae50SIngo Molnar 			pm += ACPI_PM_OVRRUN;
640f62bae50SIngo Molnar 		lapic_cal_pm2 = pm;
641f62bae50SIngo Molnar 		lapic_cal_j2 = jiffies;
642f62bae50SIngo Molnar 		break;
643f62bae50SIngo Molnar 	}
644f62bae50SIngo Molnar }
645f62bae50SIngo Molnar 
646f62bae50SIngo Molnar static int __init
647f62bae50SIngo Molnar calibrate_by_pmtimer(long deltapm, long *delta, long *deltatsc)
648f62bae50SIngo Molnar {
649f62bae50SIngo Molnar 	const long pm_100ms = PMTMR_TICKS_PER_SEC / 10;
650f62bae50SIngo Molnar 	const long pm_thresh = pm_100ms / 100;
651f62bae50SIngo Molnar 	unsigned long mult;
652f62bae50SIngo Molnar 	u64 res;
653f62bae50SIngo Molnar 
654f62bae50SIngo Molnar #ifndef CONFIG_X86_PM_TIMER
655f62bae50SIngo Molnar 	return -1;
656f62bae50SIngo Molnar #endif
657f62bae50SIngo Molnar 
658f62bae50SIngo Molnar 	apic_printk(APIC_VERBOSE, "... PM-Timer delta = %ld\n", deltapm);
659f62bae50SIngo Molnar 
660f62bae50SIngo Molnar 	/* Check, if the PM timer is available */
661f62bae50SIngo Molnar 	if (!deltapm)
662f62bae50SIngo Molnar 		return -1;
663f62bae50SIngo Molnar 
664f62bae50SIngo Molnar 	mult = clocksource_hz2mult(PMTMR_TICKS_PER_SEC, 22);
665f62bae50SIngo Molnar 
666f62bae50SIngo Molnar 	if (deltapm > (pm_100ms - pm_thresh) &&
667f62bae50SIngo Molnar 	    deltapm < (pm_100ms + pm_thresh)) {
668f62bae50SIngo Molnar 		apic_printk(APIC_VERBOSE, "... PM-Timer result ok\n");
669f62bae50SIngo Molnar 		return 0;
670f62bae50SIngo Molnar 	}
671f62bae50SIngo Molnar 
672f62bae50SIngo Molnar 	res = (((u64)deltapm) *  mult) >> 22;
673f62bae50SIngo Molnar 	do_div(res, 1000000);
674f62bae50SIngo Molnar 	pr_warning("APIC calibration not consistent "
675f62bae50SIngo Molnar 		   "with PM-Timer: %ldms instead of 100ms\n",(long)res);
676f62bae50SIngo Molnar 
677f62bae50SIngo Molnar 	/* Correct the lapic counter value */
678f62bae50SIngo Molnar 	res = (((u64)(*delta)) * pm_100ms);
679f62bae50SIngo Molnar 	do_div(res, deltapm);
680f62bae50SIngo Molnar 	pr_info("APIC delta adjusted to PM-Timer: "
681f62bae50SIngo Molnar 		"%lu (%ld)\n", (unsigned long)res, *delta);
682f62bae50SIngo Molnar 	*delta = (long)res;
683f62bae50SIngo Molnar 
684f62bae50SIngo Molnar 	/* Correct the tsc counter value */
685f62bae50SIngo Molnar 	if (cpu_has_tsc) {
686f62bae50SIngo Molnar 		res = (((u64)(*deltatsc)) * pm_100ms);
687f62bae50SIngo Molnar 		do_div(res, deltapm);
688f62bae50SIngo Molnar 		apic_printk(APIC_VERBOSE, "TSC delta adjusted to "
689f62bae50SIngo Molnar 					  "PM-Timer: %lu (%ld)\n",
690f62bae50SIngo Molnar 					(unsigned long)res, *deltatsc);
691f62bae50SIngo Molnar 		*deltatsc = (long)res;
692f62bae50SIngo Molnar 	}
693f62bae50SIngo Molnar 
694f62bae50SIngo Molnar 	return 0;
695f62bae50SIngo Molnar }
696f62bae50SIngo Molnar 
697f62bae50SIngo Molnar static int __init calibrate_APIC_clock(void)
698f62bae50SIngo Molnar {
699f62bae50SIngo Molnar 	struct clock_event_device *levt = &__get_cpu_var(lapic_events);
700f62bae50SIngo Molnar 	void (*real_handler)(struct clock_event_device *dev);
701f62bae50SIngo Molnar 	unsigned long deltaj;
702f62bae50SIngo Molnar 	long delta, deltatsc;
703f62bae50SIngo Molnar 	int pm_referenced = 0;
704f62bae50SIngo Molnar 
7051ade93efSJacob Pan 	/**
7061ade93efSJacob Pan 	 * check if lapic timer has already been calibrated by platform
7071ade93efSJacob Pan 	 * specific routine, such as tsc calibration code. if so, we just fill
7081ade93efSJacob Pan 	 * in the clockevent structure and return.
7091ade93efSJacob Pan 	 */
7101ade93efSJacob Pan 
711279f1461SSuresh Siddha 	if (boot_cpu_has(X86_FEATURE_TSC_DEADLINE_TIMER)) {
712279f1461SSuresh Siddha 		return 0;
713279f1461SSuresh Siddha 	} else if (lapic_timer_frequency) {
7141ade93efSJacob Pan 		apic_printk(APIC_VERBOSE, "lapic timer already calibrated %d\n",
7151ade93efSJacob Pan 				lapic_timer_frequency);
7161ade93efSJacob Pan 		lapic_clockevent.mult = div_sc(lapic_timer_frequency/APIC_DIVISOR,
7171ade93efSJacob Pan 					TICK_NSEC, lapic_clockevent.shift);
7181ade93efSJacob Pan 		lapic_clockevent.max_delta_ns =
7191ade93efSJacob Pan 			clockevent_delta2ns(0x7FFFFF, &lapic_clockevent);
7201ade93efSJacob Pan 		lapic_clockevent.min_delta_ns =
7211ade93efSJacob Pan 			clockevent_delta2ns(0xF, &lapic_clockevent);
7221ade93efSJacob Pan 		lapic_clockevent.features &= ~CLOCK_EVT_FEAT_DUMMY;
7231ade93efSJacob Pan 		return 0;
7241ade93efSJacob Pan 	}
7251ade93efSJacob Pan 
726279f1461SSuresh Siddha 	apic_printk(APIC_VERBOSE, "Using local APIC timer interrupts.\n"
727279f1461SSuresh Siddha 		    "calibrating APIC timer ...\n");
728279f1461SSuresh Siddha 
729f62bae50SIngo Molnar 	local_irq_disable();
730f62bae50SIngo Molnar 
731f62bae50SIngo Molnar 	/* Replace the global interrupt handler */
732f62bae50SIngo Molnar 	real_handler = global_clock_event->event_handler;
733f62bae50SIngo Molnar 	global_clock_event->event_handler = lapic_cal_handler;
734f62bae50SIngo Molnar 
735f62bae50SIngo Molnar 	/*
736f62bae50SIngo Molnar 	 * Setup the APIC counter to maximum. There is no way the lapic
737f62bae50SIngo Molnar 	 * can underflow in the 100ms detection time frame
738f62bae50SIngo Molnar 	 */
739f62bae50SIngo Molnar 	__setup_APIC_LVTT(0xffffffff, 0, 0);
740f62bae50SIngo Molnar 
741f62bae50SIngo Molnar 	/* Let the interrupts run */
742f62bae50SIngo Molnar 	local_irq_enable();
743f62bae50SIngo Molnar 
744f62bae50SIngo Molnar 	while (lapic_cal_loops <= LAPIC_CAL_LOOPS)
745f62bae50SIngo Molnar 		cpu_relax();
746f62bae50SIngo Molnar 
747f62bae50SIngo Molnar 	local_irq_disable();
748f62bae50SIngo Molnar 
749f62bae50SIngo Molnar 	/* Restore the real event handler */
750f62bae50SIngo Molnar 	global_clock_event->event_handler = real_handler;
751f62bae50SIngo Molnar 
752f62bae50SIngo Molnar 	/* Build delta t1-t2 as apic timer counts down */
753f62bae50SIngo Molnar 	delta = lapic_cal_t1 - lapic_cal_t2;
754f62bae50SIngo Molnar 	apic_printk(APIC_VERBOSE, "... lapic delta = %ld\n", delta);
755f62bae50SIngo Molnar 
756f62bae50SIngo Molnar 	deltatsc = (long)(lapic_cal_tsc2 - lapic_cal_tsc1);
757f62bae50SIngo Molnar 
758f62bae50SIngo Molnar 	/* we trust the PM based calibration if possible */
759f62bae50SIngo Molnar 	pm_referenced = !calibrate_by_pmtimer(lapic_cal_pm2 - lapic_cal_pm1,
760f62bae50SIngo Molnar 					&delta, &deltatsc);
761f62bae50SIngo Molnar 
762f62bae50SIngo Molnar 	/* Calculate the scaled math multiplication factor */
763f62bae50SIngo Molnar 	lapic_clockevent.mult = div_sc(delta, TICK_NSEC * LAPIC_CAL_LOOPS,
764f62bae50SIngo Molnar 				       lapic_clockevent.shift);
765f62bae50SIngo Molnar 	lapic_clockevent.max_delta_ns =
7664aed89d6SPierre Tardy 		clockevent_delta2ns(0x7FFFFFFF, &lapic_clockevent);
767f62bae50SIngo Molnar 	lapic_clockevent.min_delta_ns =
768f62bae50SIngo Molnar 		clockevent_delta2ns(0xF, &lapic_clockevent);
769f62bae50SIngo Molnar 
7701ade93efSJacob Pan 	lapic_timer_frequency = (delta * APIC_DIVISOR) / LAPIC_CAL_LOOPS;
771f62bae50SIngo Molnar 
772f62bae50SIngo Molnar 	apic_printk(APIC_VERBOSE, "..... delta %ld\n", delta);
773411462f6SThomas Gleixner 	apic_printk(APIC_VERBOSE, "..... mult: %u\n", lapic_clockevent.mult);
774f62bae50SIngo Molnar 	apic_printk(APIC_VERBOSE, "..... calibration result: %u\n",
7751ade93efSJacob Pan 		    lapic_timer_frequency);
776f62bae50SIngo Molnar 
777f62bae50SIngo Molnar 	if (cpu_has_tsc) {
778f62bae50SIngo Molnar 		apic_printk(APIC_VERBOSE, "..... CPU clock speed is "
779f62bae50SIngo Molnar 			    "%ld.%04ld MHz.\n",
780f62bae50SIngo Molnar 			    (deltatsc / LAPIC_CAL_LOOPS) / (1000000 / HZ),
781f62bae50SIngo Molnar 			    (deltatsc / LAPIC_CAL_LOOPS) % (1000000 / HZ));
782f62bae50SIngo Molnar 	}
783f62bae50SIngo Molnar 
784f62bae50SIngo Molnar 	apic_printk(APIC_VERBOSE, "..... host bus clock speed is "
785f62bae50SIngo Molnar 		    "%u.%04u MHz.\n",
7861ade93efSJacob Pan 		    lapic_timer_frequency / (1000000 / HZ),
7871ade93efSJacob Pan 		    lapic_timer_frequency % (1000000 / HZ));
788f62bae50SIngo Molnar 
789f62bae50SIngo Molnar 	/*
790f62bae50SIngo Molnar 	 * Do a sanity check on the APIC calibration result
791f62bae50SIngo Molnar 	 */
7921ade93efSJacob Pan 	if (lapic_timer_frequency < (1000000 / HZ)) {
793f62bae50SIngo Molnar 		local_irq_enable();
794f62bae50SIngo Molnar 		pr_warning("APIC frequency too slow, disabling apic timer\n");
795f62bae50SIngo Molnar 		return -1;
796f62bae50SIngo Molnar 	}
797f62bae50SIngo Molnar 
798f62bae50SIngo Molnar 	levt->features &= ~CLOCK_EVT_FEAT_DUMMY;
799f62bae50SIngo Molnar 
800f62bae50SIngo Molnar 	/*
801f62bae50SIngo Molnar 	 * PM timer calibration failed or not turned on
802f62bae50SIngo Molnar 	 * so lets try APIC timer based calibration
803f62bae50SIngo Molnar 	 */
804f62bae50SIngo Molnar 	if (!pm_referenced) {
805f62bae50SIngo Molnar 		apic_printk(APIC_VERBOSE, "... verify APIC timer\n");
806f62bae50SIngo Molnar 
807f62bae50SIngo Molnar 		/*
808f62bae50SIngo Molnar 		 * Setup the apic timer manually
809f62bae50SIngo Molnar 		 */
810f62bae50SIngo Molnar 		levt->event_handler = lapic_cal_handler;
811f62bae50SIngo Molnar 		lapic_timer_setup(CLOCK_EVT_MODE_PERIODIC, levt);
812f62bae50SIngo Molnar 		lapic_cal_loops = -1;
813f62bae50SIngo Molnar 
814f62bae50SIngo Molnar 		/* Let the interrupts run */
815f62bae50SIngo Molnar 		local_irq_enable();
816f62bae50SIngo Molnar 
817f62bae50SIngo Molnar 		while (lapic_cal_loops <= LAPIC_CAL_LOOPS)
818f62bae50SIngo Molnar 			cpu_relax();
819f62bae50SIngo Molnar 
820f62bae50SIngo Molnar 		/* Stop the lapic timer */
821f62bae50SIngo Molnar 		lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, levt);
822f62bae50SIngo Molnar 
823f62bae50SIngo Molnar 		/* Jiffies delta */
824f62bae50SIngo Molnar 		deltaj = lapic_cal_j2 - lapic_cal_j1;
825f62bae50SIngo Molnar 		apic_printk(APIC_VERBOSE, "... jiffies delta = %lu\n", deltaj);
826f62bae50SIngo Molnar 
827f62bae50SIngo Molnar 		/* Check, if the jiffies result is consistent */
828f62bae50SIngo Molnar 		if (deltaj >= LAPIC_CAL_LOOPS-2 && deltaj <= LAPIC_CAL_LOOPS+2)
829f62bae50SIngo Molnar 			apic_printk(APIC_VERBOSE, "... jiffies result ok\n");
830f62bae50SIngo Molnar 		else
831f62bae50SIngo Molnar 			levt->features |= CLOCK_EVT_FEAT_DUMMY;
832f62bae50SIngo Molnar 	} else
833f62bae50SIngo Molnar 		local_irq_enable();
834f62bae50SIngo Molnar 
835f62bae50SIngo Molnar 	if (levt->features & CLOCK_EVT_FEAT_DUMMY) {
836f62bae50SIngo Molnar 		pr_warning("APIC timer disabled due to verification failure\n");
837f62bae50SIngo Molnar 			return -1;
838f62bae50SIngo Molnar 	}
839f62bae50SIngo Molnar 
840f62bae50SIngo Molnar 	return 0;
841f62bae50SIngo Molnar }
842f62bae50SIngo Molnar 
843f62bae50SIngo Molnar /*
844f62bae50SIngo Molnar  * Setup the boot APIC
845f62bae50SIngo Molnar  *
846f62bae50SIngo Molnar  * Calibrate and verify the result.
847f62bae50SIngo Molnar  */
848f62bae50SIngo Molnar void __init setup_boot_APIC_clock(void)
849f62bae50SIngo Molnar {
850f62bae50SIngo Molnar 	/*
851f62bae50SIngo Molnar 	 * The local apic timer can be disabled via the kernel
852f62bae50SIngo Molnar 	 * commandline or from the CPU detection code. Register the lapic
853f62bae50SIngo Molnar 	 * timer as a dummy clock event source on SMP systems, so the
854f62bae50SIngo Molnar 	 * broadcast mechanism is used. On UP systems simply ignore it.
855f62bae50SIngo Molnar 	 */
856f62bae50SIngo Molnar 	if (disable_apic_timer) {
857f62bae50SIngo Molnar 		pr_info("Disabling APIC timer\n");
858f62bae50SIngo Molnar 		/* No broadcast on UP ! */
859f62bae50SIngo Molnar 		if (num_possible_cpus() > 1) {
860f62bae50SIngo Molnar 			lapic_clockevent.mult = 1;
861f62bae50SIngo Molnar 			setup_APIC_timer();
862f62bae50SIngo Molnar 		}
863f62bae50SIngo Molnar 		return;
864f62bae50SIngo Molnar 	}
865f62bae50SIngo Molnar 
866f62bae50SIngo Molnar 	if (calibrate_APIC_clock()) {
867f62bae50SIngo Molnar 		/* No broadcast on UP ! */
868f62bae50SIngo Molnar 		if (num_possible_cpus() > 1)
869f62bae50SIngo Molnar 			setup_APIC_timer();
870f62bae50SIngo Molnar 		return;
871f62bae50SIngo Molnar 	}
872f62bae50SIngo Molnar 
873f62bae50SIngo Molnar 	/*
874f62bae50SIngo Molnar 	 * If nmi_watchdog is set to IO_APIC, we need the
875f62bae50SIngo Molnar 	 * PIT/HPET going.  Otherwise register lapic as a dummy
876f62bae50SIngo Molnar 	 * device.
877f62bae50SIngo Molnar 	 */
878f62bae50SIngo Molnar 	lapic_clockevent.features &= ~CLOCK_EVT_FEAT_DUMMY;
879f62bae50SIngo Molnar 
880f62bae50SIngo Molnar 	/* Setup the lapic or request the broadcast */
881f62bae50SIngo Molnar 	setup_APIC_timer();
882f62bae50SIngo Molnar }
883f62bae50SIngo Molnar 
884148f9bb8SPaul Gortmaker void setup_secondary_APIC_clock(void)
885f62bae50SIngo Molnar {
886f62bae50SIngo Molnar 	setup_APIC_timer();
887f62bae50SIngo Molnar }
888f62bae50SIngo Molnar 
889f62bae50SIngo Molnar /*
890f62bae50SIngo Molnar  * The guts of the apic timer interrupt
891f62bae50SIngo Molnar  */
892f62bae50SIngo Molnar static void local_apic_timer_interrupt(void)
893f62bae50SIngo Molnar {
894f62bae50SIngo Molnar 	int cpu = smp_processor_id();
895f62bae50SIngo Molnar 	struct clock_event_device *evt = &per_cpu(lapic_events, cpu);
896f62bae50SIngo Molnar 
897f62bae50SIngo Molnar 	/*
898f62bae50SIngo Molnar 	 * Normally we should not be here till LAPIC has been initialized but
899f62bae50SIngo Molnar 	 * in some cases like kdump, its possible that there is a pending LAPIC
900f62bae50SIngo Molnar 	 * timer interrupt from previous kernel's context and is delivered in
901f62bae50SIngo Molnar 	 * new kernel the moment interrupts are enabled.
902f62bae50SIngo Molnar 	 *
903f62bae50SIngo Molnar 	 * Interrupts are enabled early and LAPIC is setup much later, hence
904f62bae50SIngo Molnar 	 * its possible that when we get here evt->event_handler is NULL.
905f62bae50SIngo Molnar 	 * Check for event_handler being NULL and discard the interrupt as
906f62bae50SIngo Molnar 	 * spurious.
907f62bae50SIngo Molnar 	 */
908f62bae50SIngo Molnar 	if (!evt->event_handler) {
909f62bae50SIngo Molnar 		pr_warning("Spurious LAPIC timer interrupt on cpu %d\n", cpu);
910f62bae50SIngo Molnar 		/* Switch it off */
911f62bae50SIngo Molnar 		lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, evt);
912f62bae50SIngo Molnar 		return;
913f62bae50SIngo Molnar 	}
914f62bae50SIngo Molnar 
915f62bae50SIngo Molnar 	/*
916f62bae50SIngo Molnar 	 * the NMI deadlock-detector uses this.
917f62bae50SIngo Molnar 	 */
918f62bae50SIngo Molnar 	inc_irq_stat(apic_timer_irqs);
919f62bae50SIngo Molnar 
920f62bae50SIngo Molnar 	evt->event_handler(evt);
921f62bae50SIngo Molnar }
922f62bae50SIngo Molnar 
923f62bae50SIngo Molnar /*
924f62bae50SIngo Molnar  * Local APIC timer interrupt. This is the most natural way for doing
925f62bae50SIngo Molnar  * local interrupts, but local timer interrupts can be emulated by
926f62bae50SIngo Molnar  * broadcast interrupts too. [in case the hw doesn't support APIC timers]
927f62bae50SIngo Molnar  *
928f62bae50SIngo Molnar  * [ if a single-CPU system runs an SMP kernel then we call the local
929f62bae50SIngo Molnar  *   interrupt as well. Thus we cannot inline the local irq ... ]
930f62bae50SIngo Molnar  */
9311d9090e2SAndi Kleen __visible void __irq_entry smp_apic_timer_interrupt(struct pt_regs *regs)
932f62bae50SIngo Molnar {
933f62bae50SIngo Molnar 	struct pt_regs *old_regs = set_irq_regs(regs);
934f62bae50SIngo Molnar 
935f62bae50SIngo Molnar 	/*
936f62bae50SIngo Molnar 	 * NOTE! We'd better ACK the irq immediately,
937f62bae50SIngo Molnar 	 * because timer handling can be slow.
938eddc0e92SSeiji Aguchi 	 *
939f62bae50SIngo Molnar 	 * update_process_times() expects us to have done irq_enter().
940f62bae50SIngo Molnar 	 * Besides, if we don't timer interrupts ignore the global
941f62bae50SIngo Molnar 	 * interrupt lock, which is the WrongThing (tm) to do.
942f62bae50SIngo Molnar 	 */
943eddc0e92SSeiji Aguchi 	entering_ack_irq();
944f62bae50SIngo Molnar 	local_apic_timer_interrupt();
945eddc0e92SSeiji Aguchi 	exiting_irq();
946f62bae50SIngo Molnar 
947f62bae50SIngo Molnar 	set_irq_regs(old_regs);
948f62bae50SIngo Molnar }
949f62bae50SIngo Molnar 
9501d9090e2SAndi Kleen __visible void __irq_entry smp_trace_apic_timer_interrupt(struct pt_regs *regs)
951cf910e83SSeiji Aguchi {
952cf910e83SSeiji Aguchi 	struct pt_regs *old_regs = set_irq_regs(regs);
953cf910e83SSeiji Aguchi 
954cf910e83SSeiji Aguchi 	/*
955cf910e83SSeiji Aguchi 	 * NOTE! We'd better ACK the irq immediately,
956cf910e83SSeiji Aguchi 	 * because timer handling can be slow.
957cf910e83SSeiji Aguchi 	 *
958cf910e83SSeiji Aguchi 	 * update_process_times() expects us to have done irq_enter().
959cf910e83SSeiji Aguchi 	 * Besides, if we don't timer interrupts ignore the global
960cf910e83SSeiji Aguchi 	 * interrupt lock, which is the WrongThing (tm) to do.
961cf910e83SSeiji Aguchi 	 */
962cf910e83SSeiji Aguchi 	entering_ack_irq();
963cf910e83SSeiji Aguchi 	trace_local_timer_entry(LOCAL_TIMER_VECTOR);
964cf910e83SSeiji Aguchi 	local_apic_timer_interrupt();
965cf910e83SSeiji Aguchi 	trace_local_timer_exit(LOCAL_TIMER_VECTOR);
966cf910e83SSeiji Aguchi 	exiting_irq();
967f62bae50SIngo Molnar 
968f62bae50SIngo Molnar 	set_irq_regs(old_regs);
969f62bae50SIngo Molnar }
970f62bae50SIngo Molnar 
971f62bae50SIngo Molnar int setup_profiling_timer(unsigned int multiplier)
972f62bae50SIngo Molnar {
973f62bae50SIngo Molnar 	return -EINVAL;
974f62bae50SIngo Molnar }
975f62bae50SIngo Molnar 
976f62bae50SIngo Molnar /*
977f62bae50SIngo Molnar  * Local APIC start and shutdown
978f62bae50SIngo Molnar  */
979f62bae50SIngo Molnar 
980f62bae50SIngo Molnar /**
981f62bae50SIngo Molnar  * clear_local_APIC - shutdown the local APIC
982f62bae50SIngo Molnar  *
983f62bae50SIngo Molnar  * This is called, when a CPU is disabled and before rebooting, so the state of
984f62bae50SIngo Molnar  * the local APIC has no dangling leftovers. Also used to cleanout any BIOS
985f62bae50SIngo Molnar  * leftovers during boot.
986f62bae50SIngo Molnar  */
987f62bae50SIngo Molnar void clear_local_APIC(void)
988f62bae50SIngo Molnar {
989f62bae50SIngo Molnar 	int maxlvt;
990f62bae50SIngo Molnar 	u32 v;
991f62bae50SIngo Molnar 
992f62bae50SIngo Molnar 	/* APIC hasn't been mapped yet */
993fc1edaf9SSuresh Siddha 	if (!x2apic_mode && !apic_phys)
994f62bae50SIngo Molnar 		return;
995f62bae50SIngo Molnar 
996f62bae50SIngo Molnar 	maxlvt = lapic_get_maxlvt();
997f62bae50SIngo Molnar 	/*
998f62bae50SIngo Molnar 	 * Masking an LVT entry can trigger a local APIC error
999f62bae50SIngo Molnar 	 * if the vector is zero. Mask LVTERR first to prevent this.
1000f62bae50SIngo Molnar 	 */
1001f62bae50SIngo Molnar 	if (maxlvt >= 3) {
1002f62bae50SIngo Molnar 		v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
1003f62bae50SIngo Molnar 		apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
1004f62bae50SIngo Molnar 	}
1005f62bae50SIngo Molnar 	/*
1006f62bae50SIngo Molnar 	 * Careful: we have to set masks only first to deassert
1007f62bae50SIngo Molnar 	 * any level-triggered sources.
1008f62bae50SIngo Molnar 	 */
1009f62bae50SIngo Molnar 	v = apic_read(APIC_LVTT);
1010f62bae50SIngo Molnar 	apic_write(APIC_LVTT, v | APIC_LVT_MASKED);
1011f62bae50SIngo Molnar 	v = apic_read(APIC_LVT0);
1012f62bae50SIngo Molnar 	apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
1013f62bae50SIngo Molnar 	v = apic_read(APIC_LVT1);
1014f62bae50SIngo Molnar 	apic_write(APIC_LVT1, v | APIC_LVT_MASKED);
1015f62bae50SIngo Molnar 	if (maxlvt >= 4) {
1016f62bae50SIngo Molnar 		v = apic_read(APIC_LVTPC);
1017f62bae50SIngo Molnar 		apic_write(APIC_LVTPC, v | APIC_LVT_MASKED);
1018f62bae50SIngo Molnar 	}
1019f62bae50SIngo Molnar 
1020f62bae50SIngo Molnar 	/* lets not touch this if we didn't frob it */
10214efc0670SAndi Kleen #ifdef CONFIG_X86_THERMAL_VECTOR
1022f62bae50SIngo Molnar 	if (maxlvt >= 5) {
1023f62bae50SIngo Molnar 		v = apic_read(APIC_LVTTHMR);
1024f62bae50SIngo Molnar 		apic_write(APIC_LVTTHMR, v | APIC_LVT_MASKED);
1025f62bae50SIngo Molnar 	}
1026f62bae50SIngo Molnar #endif
1027638bee71SH. Peter Anvin #ifdef CONFIG_X86_MCE_INTEL
1028638bee71SH. Peter Anvin 	if (maxlvt >= 6) {
1029638bee71SH. Peter Anvin 		v = apic_read(APIC_LVTCMCI);
1030638bee71SH. Peter Anvin 		if (!(v & APIC_LVT_MASKED))
1031638bee71SH. Peter Anvin 			apic_write(APIC_LVTCMCI, v | APIC_LVT_MASKED);
1032638bee71SH. Peter Anvin 	}
1033638bee71SH. Peter Anvin #endif
1034638bee71SH. Peter Anvin 
1035f62bae50SIngo Molnar 	/*
1036f62bae50SIngo Molnar 	 * Clean APIC state for other OSs:
1037f62bae50SIngo Molnar 	 */
1038f62bae50SIngo Molnar 	apic_write(APIC_LVTT, APIC_LVT_MASKED);
1039f62bae50SIngo Molnar 	apic_write(APIC_LVT0, APIC_LVT_MASKED);
1040f62bae50SIngo Molnar 	apic_write(APIC_LVT1, APIC_LVT_MASKED);
1041f62bae50SIngo Molnar 	if (maxlvt >= 3)
1042f62bae50SIngo Molnar 		apic_write(APIC_LVTERR, APIC_LVT_MASKED);
1043f62bae50SIngo Molnar 	if (maxlvt >= 4)
1044f62bae50SIngo Molnar 		apic_write(APIC_LVTPC, APIC_LVT_MASKED);
1045f62bae50SIngo Molnar 
1046f62bae50SIngo Molnar 	/* Integrated APIC (!82489DX) ? */
1047f62bae50SIngo Molnar 	if (lapic_is_integrated()) {
1048f62bae50SIngo Molnar 		if (maxlvt > 3)
1049f62bae50SIngo Molnar 			/* Clear ESR due to Pentium errata 3AP and 11AP */
1050f62bae50SIngo Molnar 			apic_write(APIC_ESR, 0);
1051f62bae50SIngo Molnar 		apic_read(APIC_ESR);
1052f62bae50SIngo Molnar 	}
1053f62bae50SIngo Molnar }
1054f62bae50SIngo Molnar 
1055f62bae50SIngo Molnar /**
1056f62bae50SIngo Molnar  * disable_local_APIC - clear and disable the local APIC
1057f62bae50SIngo Molnar  */
1058f62bae50SIngo Molnar void disable_local_APIC(void)
1059f62bae50SIngo Molnar {
1060f62bae50SIngo Molnar 	unsigned int value;
1061f62bae50SIngo Molnar 
1062f62bae50SIngo Molnar 	/* APIC hasn't been mapped yet */
1063fd19dce7SYinghai Lu 	if (!x2apic_mode && !apic_phys)
1064f62bae50SIngo Molnar 		return;
1065f62bae50SIngo Molnar 
1066f62bae50SIngo Molnar 	clear_local_APIC();
1067f62bae50SIngo Molnar 
1068f62bae50SIngo Molnar 	/*
1069f62bae50SIngo Molnar 	 * Disable APIC (implies clearing of registers
1070f62bae50SIngo Molnar 	 * for 82489DX!).
1071f62bae50SIngo Molnar 	 */
1072f62bae50SIngo Molnar 	value = apic_read(APIC_SPIV);
1073f62bae50SIngo Molnar 	value &= ~APIC_SPIV_APIC_ENABLED;
1074f62bae50SIngo Molnar 	apic_write(APIC_SPIV, value);
1075f62bae50SIngo Molnar 
1076f62bae50SIngo Molnar #ifdef CONFIG_X86_32
1077f62bae50SIngo Molnar 	/*
1078f62bae50SIngo Molnar 	 * When LAPIC was disabled by the BIOS and enabled by the kernel,
1079f62bae50SIngo Molnar 	 * restore the disabled state.
1080f62bae50SIngo Molnar 	 */
1081f62bae50SIngo Molnar 	if (enabled_via_apicbase) {
1082f62bae50SIngo Molnar 		unsigned int l, h;
1083f62bae50SIngo Molnar 
1084f62bae50SIngo Molnar 		rdmsr(MSR_IA32_APICBASE, l, h);
1085f62bae50SIngo Molnar 		l &= ~MSR_IA32_APICBASE_ENABLE;
1086f62bae50SIngo Molnar 		wrmsr(MSR_IA32_APICBASE, l, h);
1087f62bae50SIngo Molnar 	}
1088f62bae50SIngo Molnar #endif
1089f62bae50SIngo Molnar }
1090f62bae50SIngo Molnar 
1091f62bae50SIngo Molnar /*
1092f62bae50SIngo Molnar  * If Linux enabled the LAPIC against the BIOS default disable it down before
1093f62bae50SIngo Molnar  * re-entering the BIOS on shutdown.  Otherwise the BIOS may get confused and
1094f62bae50SIngo Molnar  * not power-off.  Additionally clear all LVT entries before disable_local_APIC
1095f62bae50SIngo Molnar  * for the case where Linux didn't enable the LAPIC.
1096f62bae50SIngo Molnar  */
1097f62bae50SIngo Molnar void lapic_shutdown(void)
1098f62bae50SIngo Molnar {
1099f62bae50SIngo Molnar 	unsigned long flags;
1100f62bae50SIngo Molnar 
11018312136fSCyrill Gorcunov 	if (!cpu_has_apic && !apic_from_smp_config())
1102f62bae50SIngo Molnar 		return;
1103f62bae50SIngo Molnar 
1104f62bae50SIngo Molnar 	local_irq_save(flags);
1105f62bae50SIngo Molnar 
1106f62bae50SIngo Molnar #ifdef CONFIG_X86_32
1107f62bae50SIngo Molnar 	if (!enabled_via_apicbase)
1108f62bae50SIngo Molnar 		clear_local_APIC();
1109f62bae50SIngo Molnar 	else
1110f62bae50SIngo Molnar #endif
1111f62bae50SIngo Molnar 		disable_local_APIC();
1112f62bae50SIngo Molnar 
1113f62bae50SIngo Molnar 
1114f62bae50SIngo Molnar 	local_irq_restore(flags);
1115f62bae50SIngo Molnar }
1116f62bae50SIngo Molnar 
1117f62bae50SIngo Molnar /*
1118f62bae50SIngo Molnar  * This is to verify that we're looking at a real local APIC.
1119f62bae50SIngo Molnar  * Check these against your board if the CPUs aren't getting
1120f62bae50SIngo Molnar  * started for no apparent reason.
1121f62bae50SIngo Molnar  */
1122f62bae50SIngo Molnar int __init verify_local_APIC(void)
1123f62bae50SIngo Molnar {
1124f62bae50SIngo Molnar 	unsigned int reg0, reg1;
1125f62bae50SIngo Molnar 
1126f62bae50SIngo Molnar 	/*
1127f62bae50SIngo Molnar 	 * The version register is read-only in a real APIC.
1128f62bae50SIngo Molnar 	 */
1129f62bae50SIngo Molnar 	reg0 = apic_read(APIC_LVR);
1130f62bae50SIngo Molnar 	apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
1131f62bae50SIngo Molnar 	apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
1132f62bae50SIngo Molnar 	reg1 = apic_read(APIC_LVR);
1133f62bae50SIngo Molnar 	apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
1134f62bae50SIngo Molnar 
1135f62bae50SIngo Molnar 	/*
1136f62bae50SIngo Molnar 	 * The two version reads above should print the same
1137f62bae50SIngo Molnar 	 * numbers.  If the second one is different, then we
1138f62bae50SIngo Molnar 	 * poke at a non-APIC.
1139f62bae50SIngo Molnar 	 */
1140f62bae50SIngo Molnar 	if (reg1 != reg0)
1141f62bae50SIngo Molnar 		return 0;
1142f62bae50SIngo Molnar 
1143f62bae50SIngo Molnar 	/*
1144f62bae50SIngo Molnar 	 * Check if the version looks reasonably.
1145f62bae50SIngo Molnar 	 */
1146f62bae50SIngo Molnar 	reg1 = GET_APIC_VERSION(reg0);
1147f62bae50SIngo Molnar 	if (reg1 == 0x00 || reg1 == 0xff)
1148f62bae50SIngo Molnar 		return 0;
1149f62bae50SIngo Molnar 	reg1 = lapic_get_maxlvt();
1150f62bae50SIngo Molnar 	if (reg1 < 0x02 || reg1 == 0xff)
1151f62bae50SIngo Molnar 		return 0;
1152f62bae50SIngo Molnar 
1153f62bae50SIngo Molnar 	/*
1154f62bae50SIngo Molnar 	 * The ID register is read/write in a real APIC.
1155f62bae50SIngo Molnar 	 */
1156f62bae50SIngo Molnar 	reg0 = apic_read(APIC_ID);
1157f62bae50SIngo Molnar 	apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
1158f62bae50SIngo Molnar 	apic_write(APIC_ID, reg0 ^ apic->apic_id_mask);
1159f62bae50SIngo Molnar 	reg1 = apic_read(APIC_ID);
1160f62bae50SIngo Molnar 	apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg1);
1161f62bae50SIngo Molnar 	apic_write(APIC_ID, reg0);
1162f62bae50SIngo Molnar 	if (reg1 != (reg0 ^ apic->apic_id_mask))
1163f62bae50SIngo Molnar 		return 0;
1164f62bae50SIngo Molnar 
1165f62bae50SIngo Molnar 	/*
1166f62bae50SIngo Molnar 	 * The next two are just to see if we have sane values.
1167f62bae50SIngo Molnar 	 * They're only really relevant if we're in Virtual Wire
1168f62bae50SIngo Molnar 	 * compatibility mode, but most boxes are anymore.
1169f62bae50SIngo Molnar 	 */
1170f62bae50SIngo Molnar 	reg0 = apic_read(APIC_LVT0);
1171f62bae50SIngo Molnar 	apic_printk(APIC_DEBUG, "Getting LVT0: %x\n", reg0);
1172f62bae50SIngo Molnar 	reg1 = apic_read(APIC_LVT1);
1173f62bae50SIngo Molnar 	apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
1174f62bae50SIngo Molnar 
1175f62bae50SIngo Molnar 	return 1;
1176f62bae50SIngo Molnar }
1177f62bae50SIngo Molnar 
1178f62bae50SIngo Molnar /**
1179f62bae50SIngo Molnar  * sync_Arb_IDs - synchronize APIC bus arbitration IDs
1180f62bae50SIngo Molnar  */
1181f62bae50SIngo Molnar void __init sync_Arb_IDs(void)
1182f62bae50SIngo Molnar {
1183f62bae50SIngo Molnar 	/*
1184f62bae50SIngo Molnar 	 * Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 And not
1185f62bae50SIngo Molnar 	 * needed on AMD.
1186f62bae50SIngo Molnar 	 */
1187f62bae50SIngo Molnar 	if (modern_apic() || boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
1188f62bae50SIngo Molnar 		return;
1189f62bae50SIngo Molnar 
1190f62bae50SIngo Molnar 	/*
1191f62bae50SIngo Molnar 	 * Wait for idle.
1192f62bae50SIngo Molnar 	 */
1193f62bae50SIngo Molnar 	apic_wait_icr_idle();
1194f62bae50SIngo Molnar 
1195f62bae50SIngo Molnar 	apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
1196f62bae50SIngo Molnar 	apic_write(APIC_ICR, APIC_DEST_ALLINC |
1197f62bae50SIngo Molnar 			APIC_INT_LEVELTRIG | APIC_DM_INIT);
1198f62bae50SIngo Molnar }
1199f62bae50SIngo Molnar 
1200f62bae50SIngo Molnar /*
1201f62bae50SIngo Molnar  * An initial setup of the virtual wire mode.
1202f62bae50SIngo Molnar  */
1203f62bae50SIngo Molnar void __init init_bsp_APIC(void)
1204f62bae50SIngo Molnar {
1205f62bae50SIngo Molnar 	unsigned int value;
1206f62bae50SIngo Molnar 
1207f62bae50SIngo Molnar 	/*
1208f62bae50SIngo Molnar 	 * Don't do the setup now if we have a SMP BIOS as the
1209f62bae50SIngo Molnar 	 * through-I/O-APIC virtual wire mode might be active.
1210f62bae50SIngo Molnar 	 */
1211f62bae50SIngo Molnar 	if (smp_found_config || !cpu_has_apic)
1212f62bae50SIngo Molnar 		return;
1213f62bae50SIngo Molnar 
1214f62bae50SIngo Molnar 	/*
1215f62bae50SIngo Molnar 	 * Do not trust the local APIC being empty at bootup.
1216f62bae50SIngo Molnar 	 */
1217f62bae50SIngo Molnar 	clear_local_APIC();
1218f62bae50SIngo Molnar 
1219f62bae50SIngo Molnar 	/*
1220f62bae50SIngo Molnar 	 * Enable APIC.
1221f62bae50SIngo Molnar 	 */
1222f62bae50SIngo Molnar 	value = apic_read(APIC_SPIV);
1223f62bae50SIngo Molnar 	value &= ~APIC_VECTOR_MASK;
1224f62bae50SIngo Molnar 	value |= APIC_SPIV_APIC_ENABLED;
1225f62bae50SIngo Molnar 
1226f62bae50SIngo Molnar #ifdef CONFIG_X86_32
1227f62bae50SIngo Molnar 	/* This bit is reserved on P4/Xeon and should be cleared */
1228f62bae50SIngo Molnar 	if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) &&
1229f62bae50SIngo Molnar 	    (boot_cpu_data.x86 == 15))
1230f62bae50SIngo Molnar 		value &= ~APIC_SPIV_FOCUS_DISABLED;
1231f62bae50SIngo Molnar 	else
1232f62bae50SIngo Molnar #endif
1233f62bae50SIngo Molnar 		value |= APIC_SPIV_FOCUS_DISABLED;
1234f62bae50SIngo Molnar 	value |= SPURIOUS_APIC_VECTOR;
1235f62bae50SIngo Molnar 	apic_write(APIC_SPIV, value);
1236f62bae50SIngo Molnar 
1237f62bae50SIngo Molnar 	/*
1238f62bae50SIngo Molnar 	 * Set up the virtual wire mode.
1239f62bae50SIngo Molnar 	 */
1240f62bae50SIngo Molnar 	apic_write(APIC_LVT0, APIC_DM_EXTINT);
1241f62bae50SIngo Molnar 	value = APIC_DM_NMI;
1242f62bae50SIngo Molnar 	if (!lapic_is_integrated())		/* 82489DX */
1243f62bae50SIngo Molnar 		value |= APIC_LVT_LEVEL_TRIGGER;
1244f62bae50SIngo Molnar 	apic_write(APIC_LVT1, value);
1245f62bae50SIngo Molnar }
1246f62bae50SIngo Molnar 
1247148f9bb8SPaul Gortmaker static void lapic_setup_esr(void)
1248f62bae50SIngo Molnar {
1249f62bae50SIngo Molnar 	unsigned int oldvalue, value, maxlvt;
1250f62bae50SIngo Molnar 
1251f62bae50SIngo Molnar 	if (!lapic_is_integrated()) {
1252f62bae50SIngo Molnar 		pr_info("No ESR for 82489DX.\n");
1253f62bae50SIngo Molnar 		return;
1254f62bae50SIngo Molnar 	}
1255f62bae50SIngo Molnar 
1256f62bae50SIngo Molnar 	if (apic->disable_esr) {
1257f62bae50SIngo Molnar 		/*
1258f62bae50SIngo Molnar 		 * Something untraceable is creating bad interrupts on
1259f62bae50SIngo Molnar 		 * secondary quads ... for the moment, just leave the
1260f62bae50SIngo Molnar 		 * ESR disabled - we can't do anything useful with the
1261f62bae50SIngo Molnar 		 * errors anyway - mbligh
1262f62bae50SIngo Molnar 		 */
1263f62bae50SIngo Molnar 		pr_info("Leaving ESR disabled.\n");
1264f62bae50SIngo Molnar 		return;
1265f62bae50SIngo Molnar 	}
1266f62bae50SIngo Molnar 
1267f62bae50SIngo Molnar 	maxlvt = lapic_get_maxlvt();
1268f62bae50SIngo Molnar 	if (maxlvt > 3)		/* Due to the Pentium erratum 3AP. */
1269f62bae50SIngo Molnar 		apic_write(APIC_ESR, 0);
1270f62bae50SIngo Molnar 	oldvalue = apic_read(APIC_ESR);
1271f62bae50SIngo Molnar 
1272f62bae50SIngo Molnar 	/* enables sending errors */
1273f62bae50SIngo Molnar 	value = ERROR_APIC_VECTOR;
1274f62bae50SIngo Molnar 	apic_write(APIC_LVTERR, value);
1275f62bae50SIngo Molnar 
1276f62bae50SIngo Molnar 	/*
1277f62bae50SIngo Molnar 	 * spec says clear errors after enabling vector.
1278f62bae50SIngo Molnar 	 */
1279f62bae50SIngo Molnar 	if (maxlvt > 3)
1280f62bae50SIngo Molnar 		apic_write(APIC_ESR, 0);
1281f62bae50SIngo Molnar 	value = apic_read(APIC_ESR);
1282f62bae50SIngo Molnar 	if (value != oldvalue)
1283f62bae50SIngo Molnar 		apic_printk(APIC_VERBOSE, "ESR value before enabling "
1284f62bae50SIngo Molnar 			"vector: 0x%08x  after: 0x%08x\n",
1285f62bae50SIngo Molnar 			oldvalue, value);
1286f62bae50SIngo Molnar }
1287f62bae50SIngo Molnar 
1288f62bae50SIngo Molnar /**
1289f62bae50SIngo Molnar  * setup_local_APIC - setup the local APIC
12900aa002feSTejun Heo  *
12910aa002feSTejun Heo  * Used to setup local APIC while initializing BSP or bringin up APs.
12920aa002feSTejun Heo  * Always called with preemption disabled.
1293f62bae50SIngo Molnar  */
1294148f9bb8SPaul Gortmaker void setup_local_APIC(void)
1295f62bae50SIngo Molnar {
12960aa002feSTejun Heo 	int cpu = smp_processor_id();
12978c3ba8d0SKerstin Jonsson 	unsigned int value, queued;
12988c3ba8d0SKerstin Jonsson 	int i, j, acked = 0;
12998c3ba8d0SKerstin Jonsson 	unsigned long long tsc = 0, ntsc;
1300b47dcbdcSAndy Lutomirski 	long long max_loops = cpu_khz ? cpu_khz : 1000000;
13018c3ba8d0SKerstin Jonsson 
13028c3ba8d0SKerstin Jonsson 	if (cpu_has_tsc)
13038c3ba8d0SKerstin Jonsson 		rdtscll(tsc);
1304f62bae50SIngo Molnar 
1305f62bae50SIngo Molnar 	if (disable_apic) {
13067167d08eSHenrik Kretzschmar 		disable_ioapic_support();
1307f62bae50SIngo Molnar 		return;
1308f62bae50SIngo Molnar 	}
1309f62bae50SIngo Molnar 
1310f62bae50SIngo Molnar #ifdef CONFIG_X86_32
1311f62bae50SIngo Molnar 	/* Pound the ESR really hard over the head with a big hammer - mbligh */
1312f62bae50SIngo Molnar 	if (lapic_is_integrated() && apic->disable_esr) {
1313f62bae50SIngo Molnar 		apic_write(APIC_ESR, 0);
1314f62bae50SIngo Molnar 		apic_write(APIC_ESR, 0);
1315f62bae50SIngo Molnar 		apic_write(APIC_ESR, 0);
1316f62bae50SIngo Molnar 		apic_write(APIC_ESR, 0);
1317f62bae50SIngo Molnar 	}
1318f62bae50SIngo Molnar #endif
1319cdd6c482SIngo Molnar 	perf_events_lapic_init();
1320f62bae50SIngo Molnar 
1321f62bae50SIngo Molnar 	/*
1322f62bae50SIngo Molnar 	 * Double-check whether this APIC is really registered.
1323f62bae50SIngo Molnar 	 * This is meaningless in clustered apic mode, so we skip it.
1324f62bae50SIngo Molnar 	 */
1325c2777f98SDaniel Walker 	BUG_ON(!apic->apic_id_registered());
1326f62bae50SIngo Molnar 
1327f62bae50SIngo Molnar 	/*
1328f62bae50SIngo Molnar 	 * Intel recommends to set DFR, LDR and TPR before enabling
1329f62bae50SIngo Molnar 	 * an APIC.  See e.g. "AP-388 82489DX User's Manual" (Intel
1330f62bae50SIngo Molnar 	 * document number 292116).  So here it goes...
1331f62bae50SIngo Molnar 	 */
1332f62bae50SIngo Molnar 	apic->init_apic_ldr();
1333f62bae50SIngo Molnar 
13346f802c4bSTejun Heo #ifdef CONFIG_X86_32
13356f802c4bSTejun Heo 	/*
1336acb8bc09STejun Heo 	 * APIC LDR is initialized.  If logical_apicid mapping was
1337acb8bc09STejun Heo 	 * initialized during get_smp_config(), make sure it matches the
1338acb8bc09STejun Heo 	 * actual value.
13396f802c4bSTejun Heo 	 */
1340acb8bc09STejun Heo 	i = early_per_cpu(x86_cpu_to_logical_apicid, cpu);
1341acb8bc09STejun Heo 	WARN_ON(i != BAD_APICID && i != logical_smp_processor_id());
1342acb8bc09STejun Heo 	/* always use the value from LDR */
13436f802c4bSTejun Heo 	early_per_cpu(x86_cpu_to_logical_apicid, cpu) =
13446f802c4bSTejun Heo 		logical_smp_processor_id();
13456f802c4bSTejun Heo #endif
13466f802c4bSTejun Heo 
1347f62bae50SIngo Molnar 	/*
1348f62bae50SIngo Molnar 	 * Set Task Priority to 'accept all'. We never change this
1349f62bae50SIngo Molnar 	 * later on.
1350f62bae50SIngo Molnar 	 */
1351f62bae50SIngo Molnar 	value = apic_read(APIC_TASKPRI);
1352f62bae50SIngo Molnar 	value &= ~APIC_TPRI_MASK;
1353f62bae50SIngo Molnar 	apic_write(APIC_TASKPRI, value);
1354f62bae50SIngo Molnar 
1355f62bae50SIngo Molnar 	/*
1356f62bae50SIngo Molnar 	 * After a crash, we no longer service the interrupts and a pending
1357f62bae50SIngo Molnar 	 * interrupt from previous kernel might still have ISR bit set.
1358f62bae50SIngo Molnar 	 *
1359f62bae50SIngo Molnar 	 * Most probably by now CPU has serviced that pending interrupt and
1360f62bae50SIngo Molnar 	 * it might not have done the ack_APIC_irq() because it thought,
1361f62bae50SIngo Molnar 	 * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
1362f62bae50SIngo Molnar 	 * does not clear the ISR bit and cpu thinks it has already serivced
1363f62bae50SIngo Molnar 	 * the interrupt. Hence a vector might get locked. It was noticed
1364f62bae50SIngo Molnar 	 * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
1365f62bae50SIngo Molnar 	 */
13668c3ba8d0SKerstin Jonsson 	do {
13678c3ba8d0SKerstin Jonsson 		queued = 0;
13688c3ba8d0SKerstin Jonsson 		for (i = APIC_ISR_NR - 1; i >= 0; i--)
13698c3ba8d0SKerstin Jonsson 			queued |= apic_read(APIC_IRR + i*0x10);
13708c3ba8d0SKerstin Jonsson 
1371f62bae50SIngo Molnar 		for (i = APIC_ISR_NR - 1; i >= 0; i--) {
1372f62bae50SIngo Molnar 			value = apic_read(APIC_ISR + i*0x10);
1373f62bae50SIngo Molnar 			for (j = 31; j >= 0; j--) {
13748c3ba8d0SKerstin Jonsson 				if (value & (1<<j)) {
1375f62bae50SIngo Molnar 					ack_APIC_irq();
13768c3ba8d0SKerstin Jonsson 					acked++;
1377f62bae50SIngo Molnar 				}
1378f62bae50SIngo Molnar 			}
13798c3ba8d0SKerstin Jonsson 		}
13808c3ba8d0SKerstin Jonsson 		if (acked > 256) {
13818c3ba8d0SKerstin Jonsson 			printk(KERN_ERR "LAPIC pending interrupts after %d EOI\n",
13828c3ba8d0SKerstin Jonsson 			       acked);
13838c3ba8d0SKerstin Jonsson 			break;
13848c3ba8d0SKerstin Jonsson 		}
138542fa4250SShai Fultheim 		if (queued) {
1386b47dcbdcSAndy Lutomirski 			if (cpu_has_tsc && cpu_khz) {
13878c3ba8d0SKerstin Jonsson 				rdtscll(ntsc);
13888c3ba8d0SKerstin Jonsson 				max_loops = (cpu_khz << 10) - (ntsc - tsc);
13898c3ba8d0SKerstin Jonsson 			} else
13908c3ba8d0SKerstin Jonsson 				max_loops--;
139142fa4250SShai Fultheim 		}
13928c3ba8d0SKerstin Jonsson 	} while (queued && max_loops > 0);
13938c3ba8d0SKerstin Jonsson 	WARN_ON(max_loops <= 0);
1394f62bae50SIngo Molnar 
1395f62bae50SIngo Molnar 	/*
1396f62bae50SIngo Molnar 	 * Now that we are all set up, enable the APIC
1397f62bae50SIngo Molnar 	 */
1398f62bae50SIngo Molnar 	value = apic_read(APIC_SPIV);
1399f62bae50SIngo Molnar 	value &= ~APIC_VECTOR_MASK;
1400f62bae50SIngo Molnar 	/*
1401f62bae50SIngo Molnar 	 * Enable APIC
1402f62bae50SIngo Molnar 	 */
1403f62bae50SIngo Molnar 	value |= APIC_SPIV_APIC_ENABLED;
1404f62bae50SIngo Molnar 
1405f62bae50SIngo Molnar #ifdef CONFIG_X86_32
1406f62bae50SIngo Molnar 	/*
1407f62bae50SIngo Molnar 	 * Some unknown Intel IO/APIC (or APIC) errata is biting us with
1408f62bae50SIngo Molnar 	 * certain networking cards. If high frequency interrupts are
1409f62bae50SIngo Molnar 	 * happening on a particular IOAPIC pin, plus the IOAPIC routing
1410f62bae50SIngo Molnar 	 * entry is masked/unmasked at a high rate as well then sooner or
1411f62bae50SIngo Molnar 	 * later IOAPIC line gets 'stuck', no more interrupts are received
1412f62bae50SIngo Molnar 	 * from the device. If focus CPU is disabled then the hang goes
1413f62bae50SIngo Molnar 	 * away, oh well :-(
1414f62bae50SIngo Molnar 	 *
1415f62bae50SIngo Molnar 	 * [ This bug can be reproduced easily with a level-triggered
1416f62bae50SIngo Molnar 	 *   PCI Ne2000 networking cards and PII/PIII processors, dual
1417f62bae50SIngo Molnar 	 *   BX chipset. ]
1418f62bae50SIngo Molnar 	 */
1419f62bae50SIngo Molnar 	/*
1420f62bae50SIngo Molnar 	 * Actually disabling the focus CPU check just makes the hang less
1421f62bae50SIngo Molnar 	 * frequent as it makes the interrupt distributon model be more
1422f62bae50SIngo Molnar 	 * like LRU than MRU (the short-term load is more even across CPUs).
1423f62bae50SIngo Molnar 	 * See also the comment in end_level_ioapic_irq().  --macro
1424f62bae50SIngo Molnar 	 */
1425f62bae50SIngo Molnar 
1426f62bae50SIngo Molnar 	/*
1427f62bae50SIngo Molnar 	 * - enable focus processor (bit==0)
1428f62bae50SIngo Molnar 	 * - 64bit mode always use processor focus
1429f62bae50SIngo Molnar 	 *   so no need to set it
1430f62bae50SIngo Molnar 	 */
1431f62bae50SIngo Molnar 	value &= ~APIC_SPIV_FOCUS_DISABLED;
1432f62bae50SIngo Molnar #endif
1433f62bae50SIngo Molnar 
1434f62bae50SIngo Molnar 	/*
1435f62bae50SIngo Molnar 	 * Set spurious IRQ vector
1436f62bae50SIngo Molnar 	 */
1437f62bae50SIngo Molnar 	value |= SPURIOUS_APIC_VECTOR;
1438f62bae50SIngo Molnar 	apic_write(APIC_SPIV, value);
1439f62bae50SIngo Molnar 
1440f62bae50SIngo Molnar 	/*
1441f62bae50SIngo Molnar 	 * Set up LVT0, LVT1:
1442f62bae50SIngo Molnar 	 *
1443f62bae50SIngo Molnar 	 * set up through-local-APIC on the BP's LINT0. This is not
1444f62bae50SIngo Molnar 	 * strictly necessary in pure symmetric-IO mode, but sometimes
1445f62bae50SIngo Molnar 	 * we delegate interrupts to the 8259A.
1446f62bae50SIngo Molnar 	 */
1447f62bae50SIngo Molnar 	/*
1448f62bae50SIngo Molnar 	 * TODO: set up through-local-APIC from through-I/O-APIC? --macro
1449f62bae50SIngo Molnar 	 */
1450f62bae50SIngo Molnar 	value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
14510aa002feSTejun Heo 	if (!cpu && (pic_mode || !value)) {
1452f62bae50SIngo Molnar 		value = APIC_DM_EXTINT;
14530aa002feSTejun Heo 		apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n", cpu);
1454f62bae50SIngo Molnar 	} else {
1455f62bae50SIngo Molnar 		value = APIC_DM_EXTINT | APIC_LVT_MASKED;
14560aa002feSTejun Heo 		apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n", cpu);
1457f62bae50SIngo Molnar 	}
1458f62bae50SIngo Molnar 	apic_write(APIC_LVT0, value);
1459f62bae50SIngo Molnar 
1460f62bae50SIngo Molnar 	/*
1461f62bae50SIngo Molnar 	 * only the BP should see the LINT1 NMI signal, obviously.
1462f62bae50SIngo Molnar 	 */
14630aa002feSTejun Heo 	if (!cpu)
1464f62bae50SIngo Molnar 		value = APIC_DM_NMI;
1465f62bae50SIngo Molnar 	else
1466f62bae50SIngo Molnar 		value = APIC_DM_NMI | APIC_LVT_MASKED;
1467f62bae50SIngo Molnar 	if (!lapic_is_integrated())		/* 82489DX */
1468f62bae50SIngo Molnar 		value |= APIC_LVT_LEVEL_TRIGGER;
1469f62bae50SIngo Molnar 	apic_write(APIC_LVT1, value);
1470f62bae50SIngo Molnar 
1471638bee71SH. Peter Anvin #ifdef CONFIG_X86_MCE_INTEL
1472638bee71SH. Peter Anvin 	/* Recheck CMCI information after local APIC is up on CPU #0 */
14730aa002feSTejun Heo 	if (!cpu)
1474638bee71SH. Peter Anvin 		cmci_recheck();
1475638bee71SH. Peter Anvin #endif
1476f62bae50SIngo Molnar }
1477f62bae50SIngo Molnar 
1478148f9bb8SPaul Gortmaker void end_local_APIC_setup(void)
1479f62bae50SIngo Molnar {
1480f62bae50SIngo Molnar 	lapic_setup_esr();
1481f62bae50SIngo Molnar 
1482f62bae50SIngo Molnar #ifdef CONFIG_X86_32
1483f62bae50SIngo Molnar 	{
1484f62bae50SIngo Molnar 		unsigned int value;
1485f62bae50SIngo Molnar 		/* Disable the local apic timer */
1486f62bae50SIngo Molnar 		value = apic_read(APIC_LVTT);
1487f62bae50SIngo Molnar 		value |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
1488f62bae50SIngo Molnar 		apic_write(APIC_LVTT, value);
1489f62bae50SIngo Molnar 	}
1490f62bae50SIngo Molnar #endif
1491f62bae50SIngo Molnar 
1492f62bae50SIngo Molnar 	apic_pm_activate();
14932fb270f3SJan Beulich }
14942fb270f3SJan Beulich 
14952fb270f3SJan Beulich void __init bsp_end_local_APIC_setup(void)
14962fb270f3SJan Beulich {
14972fb270f3SJan Beulich 	end_local_APIC_setup();
14987f7fbf45SKenji Kaneshige 
14997f7fbf45SKenji Kaneshige 	/*
15007f7fbf45SKenji Kaneshige 	 * Now that local APIC setup is completed for BP, configure the fault
15017f7fbf45SKenji Kaneshige 	 * handling for interrupt remapping.
15027f7fbf45SKenji Kaneshige 	 */
150395a02e97SSuresh Siddha 	irq_remap_enable_fault_handling();
15047f7fbf45SKenji Kaneshige 
1505f62bae50SIngo Molnar }
1506f62bae50SIngo Molnar 
1507f62bae50SIngo Molnar #ifdef CONFIG_X86_X2APIC
1508fb209bd8SYinghai Lu /*
1509fb209bd8SYinghai Lu  * Need to disable xapic and x2apic at the same time and then enable xapic mode
1510fb209bd8SYinghai Lu  */
1511fb209bd8SYinghai Lu static inline void __disable_x2apic(u64 msr)
1512fb209bd8SYinghai Lu {
1513fb209bd8SYinghai Lu 	wrmsrl(MSR_IA32_APICBASE,
1514fb209bd8SYinghai Lu 	       msr & ~(X2APIC_ENABLE | XAPIC_ENABLE));
1515fb209bd8SYinghai Lu 	wrmsrl(MSR_IA32_APICBASE, msr & ~X2APIC_ENABLE);
1516fb209bd8SYinghai Lu }
1517fb209bd8SYinghai Lu 
1518a31bc327SYinghai Lu static __init void disable_x2apic(void)
1519fb209bd8SYinghai Lu {
1520fb209bd8SYinghai Lu 	u64 msr;
1521fb209bd8SYinghai Lu 
1522fb209bd8SYinghai Lu 	if (!cpu_has_x2apic)
1523fb209bd8SYinghai Lu 		return;
1524fb209bd8SYinghai Lu 
1525fb209bd8SYinghai Lu 	rdmsrl(MSR_IA32_APICBASE, msr);
1526fb209bd8SYinghai Lu 	if (msr & X2APIC_ENABLE) {
1527fb209bd8SYinghai Lu 		u32 x2apic_id = read_apic_id();
1528fb209bd8SYinghai Lu 
1529fb209bd8SYinghai Lu 		if (x2apic_id >= 255)
1530fb209bd8SYinghai Lu 			panic("Cannot disable x2apic, id: %08x\n", x2apic_id);
1531fb209bd8SYinghai Lu 
1532fb209bd8SYinghai Lu 		pr_info("Disabling x2apic\n");
1533fb209bd8SYinghai Lu 		__disable_x2apic(msr);
1534fb209bd8SYinghai Lu 
1535a31bc327SYinghai Lu 		if (nox2apic) {
1536a31bc327SYinghai Lu 			clear_cpu_cap(&cpu_data(0), X86_FEATURE_X2APIC);
1537a31bc327SYinghai Lu 			setup_clear_cpu_cap(X86_FEATURE_X2APIC);
1538a31bc327SYinghai Lu 		}
1539a31bc327SYinghai Lu 
1540fb209bd8SYinghai Lu 		x2apic_disabled = 1;
1541fb209bd8SYinghai Lu 		x2apic_mode = 0;
1542fb209bd8SYinghai Lu 
1543fb209bd8SYinghai Lu 		register_lapic_address(mp_lapic_addr);
1544fb209bd8SYinghai Lu 	}
1545fb209bd8SYinghai Lu }
1546fb209bd8SYinghai Lu 
1547f62bae50SIngo Molnar void check_x2apic(void)
1548f62bae50SIngo Molnar {
1549ef1f87aaSSuresh Siddha 	if (x2apic_enabled()) {
1550f62bae50SIngo Molnar 		pr_info("x2apic enabled by BIOS, switching to x2apic ops\n");
1551fc1edaf9SSuresh Siddha 		x2apic_preenabled = x2apic_mode = 1;
1552f62bae50SIngo Molnar 	}
1553f62bae50SIngo Molnar }
1554f62bae50SIngo Molnar 
1555f62bae50SIngo Molnar void enable_x2apic(void)
1556f62bae50SIngo Molnar {
1557fb209bd8SYinghai Lu 	u64 msr;
1558fb209bd8SYinghai Lu 
1559fb209bd8SYinghai Lu 	rdmsrl(MSR_IA32_APICBASE, msr);
1560fb209bd8SYinghai Lu 	if (x2apic_disabled) {
1561fb209bd8SYinghai Lu 		__disable_x2apic(msr);
1562fb209bd8SYinghai Lu 		return;
1563fb209bd8SYinghai Lu 	}
1564f62bae50SIngo Molnar 
1565fc1edaf9SSuresh Siddha 	if (!x2apic_mode)
1566f62bae50SIngo Molnar 		return;
1567f62bae50SIngo Molnar 
1568f62bae50SIngo Molnar 	if (!(msr & X2APIC_ENABLE)) {
1569450b1e8dSMike Travis 		printk_once(KERN_INFO "Enabling x2apic\n");
1570fb209bd8SYinghai Lu 		wrmsrl(MSR_IA32_APICBASE, msr | X2APIC_ENABLE);
1571f62bae50SIngo Molnar 	}
1572f62bae50SIngo Molnar }
157393758238SWeidong Han #endif /* CONFIG_X86_X2APIC */
1574f62bae50SIngo Molnar 
1575ce69a784SGleb Natapov int __init enable_IR(void)
1576f62bae50SIngo Molnar {
1577d3f13810SSuresh Siddha #ifdef CONFIG_IRQ_REMAP
157895a02e97SSuresh Siddha 	if (!irq_remapping_supported()) {
157993758238SWeidong Han 		pr_debug("intr-remapping not supported\n");
158041750d31SSuresh Siddha 		return -1;
158193758238SWeidong Han 	}
158293758238SWeidong Han 
158393758238SWeidong Han 	if (!x2apic_preenabled && skip_ioapic_setup) {
158493758238SWeidong Han 		pr_info("Skipped enabling intr-remap because of skipping "
158593758238SWeidong Han 			"io-apic setup\n");
158641750d31SSuresh Siddha 		return -1;
1587f62bae50SIngo Molnar 	}
1588f62bae50SIngo Molnar 
158995a02e97SSuresh Siddha 	return irq_remapping_enable();
1590ce69a784SGleb Natapov #endif
159141750d31SSuresh Siddha 	return -1;
1592ce69a784SGleb Natapov }
1593ce69a784SGleb Natapov 
1594ce69a784SGleb Natapov void __init enable_IR_x2apic(void)
1595ce69a784SGleb Natapov {
1596ce69a784SGleb Natapov 	unsigned long flags;
1597ce69a784SGleb Natapov 	int ret, x2apic_enabled = 0;
1598736baef4SJoerg Roedel 	int hardware_init_ret;
1599b7f42ab2SYinghai Lu 
1600736baef4SJoerg Roedel 	/* Make sure irq_remap_ops are initialized */
160195a02e97SSuresh Siddha 	setup_irq_remapping_ops();
1602736baef4SJoerg Roedel 
160395a02e97SSuresh Siddha 	hardware_init_ret = irq_remapping_prepare();
1604736baef4SJoerg Roedel 	if (hardware_init_ret && !x2apic_supported())
1605e670761fSYinghai Lu 		return;
1606ce69a784SGleb Natapov 
160731dce14aSSuresh Siddha 	ret = save_ioapic_entries();
1608f62bae50SIngo Molnar 	if (ret) {
1609f62bae50SIngo Molnar 		pr_info("Saving IO-APIC state failed: %d\n", ret);
1610fb209bd8SYinghai Lu 		return;
1611f62bae50SIngo Molnar 	}
1612f62bae50SIngo Molnar 
161305c3dc2cSSuresh Siddha 	local_irq_save(flags);
1614b81bb373SJacob Pan 	legacy_pic->mask_all();
161531dce14aSSuresh Siddha 	mask_ioapic_entries();
161605c3dc2cSSuresh Siddha 
1617a31bc327SYinghai Lu 	if (x2apic_preenabled && nox2apic)
1618a31bc327SYinghai Lu 		disable_x2apic();
1619a31bc327SYinghai Lu 
1620736baef4SJoerg Roedel 	if (hardware_init_ret)
162141750d31SSuresh Siddha 		ret = -1;
1622b7f42ab2SYinghai Lu 	else
1623ce69a784SGleb Natapov 		ret = enable_IR();
1624b7f42ab2SYinghai Lu 
1625fb209bd8SYinghai Lu 	if (!x2apic_supported())
1626a31bc327SYinghai Lu 		goto skip_x2apic;
1627fb209bd8SYinghai Lu 
162841750d31SSuresh Siddha 	if (ret < 0) {
1629ce69a784SGleb Natapov 		/* IR is required if there is APIC ID > 255 even when running
1630ce69a784SGleb Natapov 		 * under KVM
1631ce69a784SGleb Natapov 		 */
16322904ed8dSSheng Yang 		if (max_physical_apicid > 255 ||
1633fb209bd8SYinghai Lu 		    !hypervisor_x2apic_available()) {
1634fb209bd8SYinghai Lu 			if (x2apic_preenabled)
1635fb209bd8SYinghai Lu 				disable_x2apic();
1636a31bc327SYinghai Lu 			goto skip_x2apic;
1637fb209bd8SYinghai Lu 		}
1638ce69a784SGleb Natapov 		/*
1639ce69a784SGleb Natapov 		 * without IR all CPUs can be addressed by IOAPIC/MSI
1640ce69a784SGleb Natapov 		 * only in physical mode
1641ce69a784SGleb Natapov 		 */
1642ce69a784SGleb Natapov 		x2apic_force_phys();
1643ce69a784SGleb Natapov 	}
1644f62bae50SIngo Molnar 
1645fb209bd8SYinghai Lu 	if (ret == IRQ_REMAP_XAPIC_MODE) {
1646fb209bd8SYinghai Lu 		pr_info("x2apic not enabled, IRQ remapping is in xapic mode\n");
1647a31bc327SYinghai Lu 		goto skip_x2apic;
1648fb209bd8SYinghai Lu 	}
164941750d31SSuresh Siddha 
1650ce69a784SGleb Natapov 	x2apic_enabled = 1;
165193758238SWeidong Han 
1652fc1edaf9SSuresh Siddha 	if (x2apic_supported() && !x2apic_mode) {
1653fc1edaf9SSuresh Siddha 		x2apic_mode = 1;
1654f62bae50SIngo Molnar 		enable_x2apic();
165593758238SWeidong Han 		pr_info("Enabled x2apic\n");
1656f62bae50SIngo Molnar 	}
1657f62bae50SIngo Molnar 
1658a31bc327SYinghai Lu skip_x2apic:
165941750d31SSuresh Siddha 	if (ret < 0) /* IR enabling failed */
166031dce14aSSuresh Siddha 		restore_ioapic_entries();
1661b81bb373SJacob Pan 	legacy_pic->restore_mask();
1662f62bae50SIngo Molnar 	local_irq_restore(flags);
1663f62bae50SIngo Molnar }
166493758238SWeidong Han 
1665f62bae50SIngo Molnar #ifdef CONFIG_X86_64
1666f62bae50SIngo Molnar /*
1667f62bae50SIngo Molnar  * Detect and enable local APICs on non-SMP boards.
1668f62bae50SIngo Molnar  * Original code written by Keir Fraser.
1669f62bae50SIngo Molnar  * On AMD64 we trust the BIOS - if it says no APIC it is likely
1670f62bae50SIngo Molnar  * not correctly set up (usually the APIC timer won't work etc.)
1671f62bae50SIngo Molnar  */
1672f62bae50SIngo Molnar static int __init detect_init_APIC(void)
1673f62bae50SIngo Molnar {
1674f62bae50SIngo Molnar 	if (!cpu_has_apic) {
1675f62bae50SIngo Molnar 		pr_info("No local APIC present\n");
1676f62bae50SIngo Molnar 		return -1;
1677f62bae50SIngo Molnar 	}
1678f62bae50SIngo Molnar 
1679f62bae50SIngo Molnar 	mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
1680f62bae50SIngo Molnar 	return 0;
1681f62bae50SIngo Molnar }
1682f62bae50SIngo Molnar #else
16835a7ae78fSThomas Gleixner 
168425874a29SHenrik Kretzschmar static int __init apic_verify(void)
16855a7ae78fSThomas Gleixner {
16865a7ae78fSThomas Gleixner 	u32 features, h, l;
16875a7ae78fSThomas Gleixner 
16885a7ae78fSThomas Gleixner 	/*
16895a7ae78fSThomas Gleixner 	 * The APIC feature bit should now be enabled
16905a7ae78fSThomas Gleixner 	 * in `cpuid'
16915a7ae78fSThomas Gleixner 	 */
16925a7ae78fSThomas Gleixner 	features = cpuid_edx(1);
16935a7ae78fSThomas Gleixner 	if (!(features & (1 << X86_FEATURE_APIC))) {
16945a7ae78fSThomas Gleixner 		pr_warning("Could not enable APIC!\n");
16955a7ae78fSThomas Gleixner 		return -1;
16965a7ae78fSThomas Gleixner 	}
16975a7ae78fSThomas Gleixner 	set_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
16985a7ae78fSThomas Gleixner 	mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
16995a7ae78fSThomas Gleixner 
17005a7ae78fSThomas Gleixner 	/* The BIOS may have set up the APIC at some other address */
1701cbf2829bSBryan O'Donoghue 	if (boot_cpu_data.x86 >= 6) {
17025a7ae78fSThomas Gleixner 		rdmsr(MSR_IA32_APICBASE, l, h);
17035a7ae78fSThomas Gleixner 		if (l & MSR_IA32_APICBASE_ENABLE)
17045a7ae78fSThomas Gleixner 			mp_lapic_addr = l & MSR_IA32_APICBASE_BASE;
1705cbf2829bSBryan O'Donoghue 	}
17065a7ae78fSThomas Gleixner 
17075a7ae78fSThomas Gleixner 	pr_info("Found and enabled local APIC!\n");
17085a7ae78fSThomas Gleixner 	return 0;
17095a7ae78fSThomas Gleixner }
17105a7ae78fSThomas Gleixner 
171125874a29SHenrik Kretzschmar int __init apic_force_enable(unsigned long addr)
17125a7ae78fSThomas Gleixner {
17135a7ae78fSThomas Gleixner 	u32 h, l;
17145a7ae78fSThomas Gleixner 
17155a7ae78fSThomas Gleixner 	if (disable_apic)
17165a7ae78fSThomas Gleixner 		return -1;
17175a7ae78fSThomas Gleixner 
17185a7ae78fSThomas Gleixner 	/*
17195a7ae78fSThomas Gleixner 	 * Some BIOSes disable the local APIC in the APIC_BASE
17205a7ae78fSThomas Gleixner 	 * MSR. This can only be done in software for Intel P6 or later
17215a7ae78fSThomas Gleixner 	 * and AMD K7 (Model > 1) or later.
17225a7ae78fSThomas Gleixner 	 */
1723cbf2829bSBryan O'Donoghue 	if (boot_cpu_data.x86 >= 6) {
17245a7ae78fSThomas Gleixner 		rdmsr(MSR_IA32_APICBASE, l, h);
17255a7ae78fSThomas Gleixner 		if (!(l & MSR_IA32_APICBASE_ENABLE)) {
17265a7ae78fSThomas Gleixner 			pr_info("Local APIC disabled by BIOS -- reenabling.\n");
17275a7ae78fSThomas Gleixner 			l &= ~MSR_IA32_APICBASE_BASE;
1728a906fdaaSThomas Gleixner 			l |= MSR_IA32_APICBASE_ENABLE | addr;
17295a7ae78fSThomas Gleixner 			wrmsr(MSR_IA32_APICBASE, l, h);
17305a7ae78fSThomas Gleixner 			enabled_via_apicbase = 1;
17315a7ae78fSThomas Gleixner 		}
1732cbf2829bSBryan O'Donoghue 	}
17335a7ae78fSThomas Gleixner 	return apic_verify();
17345a7ae78fSThomas Gleixner }
17355a7ae78fSThomas Gleixner 
1736f62bae50SIngo Molnar /*
1737f62bae50SIngo Molnar  * Detect and initialize APIC
1738f62bae50SIngo Molnar  */
1739f62bae50SIngo Molnar static int __init detect_init_APIC(void)
1740f62bae50SIngo Molnar {
1741f62bae50SIngo Molnar 	/* Disabled by kernel option? */
1742f62bae50SIngo Molnar 	if (disable_apic)
1743f62bae50SIngo Molnar 		return -1;
1744f62bae50SIngo Molnar 
1745f62bae50SIngo Molnar 	switch (boot_cpu_data.x86_vendor) {
1746f62bae50SIngo Molnar 	case X86_VENDOR_AMD:
1747f62bae50SIngo Molnar 		if ((boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model > 1) ||
1748f62bae50SIngo Molnar 		    (boot_cpu_data.x86 >= 15))
1749f62bae50SIngo Molnar 			break;
1750f62bae50SIngo Molnar 		goto no_apic;
1751f62bae50SIngo Molnar 	case X86_VENDOR_INTEL:
1752f62bae50SIngo Molnar 		if (boot_cpu_data.x86 == 6 || boot_cpu_data.x86 == 15 ||
1753f62bae50SIngo Molnar 		    (boot_cpu_data.x86 == 5 && cpu_has_apic))
1754f62bae50SIngo Molnar 			break;
1755f62bae50SIngo Molnar 		goto no_apic;
1756f62bae50SIngo Molnar 	default:
1757f62bae50SIngo Molnar 		goto no_apic;
1758f62bae50SIngo Molnar 	}
1759f62bae50SIngo Molnar 
1760f62bae50SIngo Molnar 	if (!cpu_has_apic) {
1761f62bae50SIngo Molnar 		/*
1762f62bae50SIngo Molnar 		 * Over-ride BIOS and try to enable the local APIC only if
1763f62bae50SIngo Molnar 		 * "lapic" specified.
1764f62bae50SIngo Molnar 		 */
1765f62bae50SIngo Molnar 		if (!force_enable_local_apic) {
1766f62bae50SIngo Molnar 			pr_info("Local APIC disabled by BIOS -- "
1767f62bae50SIngo Molnar 				"you can enable it with \"lapic\"\n");
1768f62bae50SIngo Molnar 			return -1;
1769f62bae50SIngo Molnar 		}
1770a906fdaaSThomas Gleixner 		if (apic_force_enable(APIC_DEFAULT_PHYS_BASE))
17715a7ae78fSThomas Gleixner 			return -1;
17725a7ae78fSThomas Gleixner 	} else {
17735a7ae78fSThomas Gleixner 		if (apic_verify())
1774f62bae50SIngo Molnar 			return -1;
1775f62bae50SIngo Molnar 	}
1776f62bae50SIngo Molnar 
1777f62bae50SIngo Molnar 	apic_pm_activate();
1778f62bae50SIngo Molnar 
1779f62bae50SIngo Molnar 	return 0;
1780f62bae50SIngo Molnar 
1781f62bae50SIngo Molnar no_apic:
1782f62bae50SIngo Molnar 	pr_info("No local APIC present or hardware disabled\n");
1783f62bae50SIngo Molnar 	return -1;
1784f62bae50SIngo Molnar }
1785f62bae50SIngo Molnar #endif
1786f62bae50SIngo Molnar 
1787f62bae50SIngo Molnar /**
1788f62bae50SIngo Molnar  * init_apic_mappings - initialize APIC mappings
1789f62bae50SIngo Molnar  */
1790f62bae50SIngo Molnar void __init init_apic_mappings(void)
1791f62bae50SIngo Molnar {
17924401da61SYinghai Lu 	unsigned int new_apicid;
17934401da61SYinghai Lu 
1794fc1edaf9SSuresh Siddha 	if (x2apic_mode) {
1795f62bae50SIngo Molnar 		boot_cpu_physical_apicid = read_apic_id();
1796f62bae50SIngo Molnar 		return;
1797f62bae50SIngo Molnar 	}
1798f62bae50SIngo Molnar 
17994797f6b0SYinghai Lu 	/* If no local APIC can be found return early */
1800f62bae50SIngo Molnar 	if (!smp_found_config && detect_init_APIC()) {
18014797f6b0SYinghai Lu 		/* lets NOP'ify apic operations */
18024797f6b0SYinghai Lu 		pr_info("APIC: disable apic facility\n");
18034797f6b0SYinghai Lu 		apic_disable();
18044797f6b0SYinghai Lu 	} else {
1805f62bae50SIngo Molnar 		apic_phys = mp_lapic_addr;
1806f62bae50SIngo Molnar 
18074401da61SYinghai Lu 		/*
18084401da61SYinghai Lu 		 * acpi lapic path already maps that address in
18094401da61SYinghai Lu 		 * acpi_register_lapic_address()
18104401da61SYinghai Lu 		 */
18115989cd6aSEric W. Biederman 		if (!acpi_lapic && !smp_found_config)
1812326a2e6bSYinghai Lu 			register_lapic_address(apic_phys);
1813cec6be6dSCyrill Gorcunov 	}
1814f62bae50SIngo Molnar 
1815f62bae50SIngo Molnar 	/*
1816f62bae50SIngo Molnar 	 * Fetch the APIC ID of the BSP in case we have a
1817f62bae50SIngo Molnar 	 * default configuration (or the MP table is broken).
1818f62bae50SIngo Molnar 	 */
18194401da61SYinghai Lu 	new_apicid = read_apic_id();
18204401da61SYinghai Lu 	if (boot_cpu_physical_apicid != new_apicid) {
18214401da61SYinghai Lu 		boot_cpu_physical_apicid = new_apicid;
1822103428e5SCyrill Gorcunov 		/*
1823103428e5SCyrill Gorcunov 		 * yeah -- we lie about apic_version
1824103428e5SCyrill Gorcunov 		 * in case if apic was disabled via boot option
1825103428e5SCyrill Gorcunov 		 * but it's not a problem for SMP compiled kernel
1826103428e5SCyrill Gorcunov 		 * since smp_sanity_check is prepared for such a case
1827103428e5SCyrill Gorcunov 		 * and disable smp mode
1828103428e5SCyrill Gorcunov 		 */
18294401da61SYinghai Lu 		apic_version[new_apicid] =
18304401da61SYinghai Lu 			 GET_APIC_VERSION(apic_read(APIC_LVR));
183108306ce6SCyrill Gorcunov 	}
1832f62bae50SIngo Molnar }
1833f62bae50SIngo Molnar 
1834c0104d38SYinghai Lu void __init register_lapic_address(unsigned long address)
1835c0104d38SYinghai Lu {
1836c0104d38SYinghai Lu 	mp_lapic_addr = address;
1837c0104d38SYinghai Lu 
18380450193bSYinghai Lu 	if (!x2apic_mode) {
1839c0104d38SYinghai Lu 		set_fixmap_nocache(FIX_APIC_BASE, address);
1840f1157141SYinghai Lu 		apic_printk(APIC_VERBOSE, "mapped APIC to %16lx (%16lx)\n",
1841f1157141SYinghai Lu 			    APIC_BASE, mp_lapic_addr);
18420450193bSYinghai Lu 	}
1843c0104d38SYinghai Lu 	if (boot_cpu_physical_apicid == -1U) {
1844c0104d38SYinghai Lu 		boot_cpu_physical_apicid  = read_apic_id();
1845c0104d38SYinghai Lu 		apic_version[boot_cpu_physical_apicid] =
1846c0104d38SYinghai Lu 			 GET_APIC_VERSION(apic_read(APIC_LVR));
1847c0104d38SYinghai Lu 	}
1848c0104d38SYinghai Lu }
1849c0104d38SYinghai Lu 
1850f62bae50SIngo Molnar /*
1851f62bae50SIngo Molnar  * This initializes the IO-APIC and APIC hardware if this is
1852f62bae50SIngo Molnar  * a UP kernel.
1853f62bae50SIngo Molnar  */
185456d91f13SYinghai Lu int apic_version[MAX_LOCAL_APIC];
1855f62bae50SIngo Molnar 
1856f62bae50SIngo Molnar int __init APIC_init_uniprocessor(void)
1857f62bae50SIngo Molnar {
1858f62bae50SIngo Molnar 	if (disable_apic) {
1859f62bae50SIngo Molnar 		pr_info("Apic disabled\n");
1860f62bae50SIngo Molnar 		return -1;
1861f62bae50SIngo Molnar 	}
1862f62bae50SIngo Molnar #ifdef CONFIG_X86_64
1863f62bae50SIngo Molnar 	if (!cpu_has_apic) {
1864f62bae50SIngo Molnar 		disable_apic = 1;
1865f62bae50SIngo Molnar 		pr_info("Apic disabled by BIOS\n");
1866f62bae50SIngo Molnar 		return -1;
1867f62bae50SIngo Molnar 	}
1868f62bae50SIngo Molnar #else
1869f62bae50SIngo Molnar 	if (!smp_found_config && !cpu_has_apic)
1870f62bae50SIngo Molnar 		return -1;
1871f62bae50SIngo Molnar 
1872f62bae50SIngo Molnar 	/*
1873f62bae50SIngo Molnar 	 * Complain if the BIOS pretends there is one.
1874f62bae50SIngo Molnar 	 */
1875f62bae50SIngo Molnar 	if (!cpu_has_apic &&
1876f62bae50SIngo Molnar 	    APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid])) {
1877f62bae50SIngo Molnar 		pr_err("BIOS bug, local APIC 0x%x not detected!...\n",
1878f62bae50SIngo Molnar 			boot_cpu_physical_apicid);
1879f62bae50SIngo Molnar 		return -1;
1880f62bae50SIngo Molnar 	}
1881f62bae50SIngo Molnar #endif
1882f62bae50SIngo Molnar 
1883f62bae50SIngo Molnar 	default_setup_apic_routing();
1884f62bae50SIngo Molnar 
1885f62bae50SIngo Molnar 	verify_local_APIC();
1886f62bae50SIngo Molnar 	connect_bsp_APIC();
1887f62bae50SIngo Molnar 
1888f62bae50SIngo Molnar #ifdef CONFIG_X86_64
1889f62bae50SIngo Molnar 	apic_write(APIC_ID, SET_APIC_ID(boot_cpu_physical_apicid));
1890f62bae50SIngo Molnar #else
1891f62bae50SIngo Molnar 	/*
1892f62bae50SIngo Molnar 	 * Hack: In case of kdump, after a crash, kernel might be booting
1893f62bae50SIngo Molnar 	 * on a cpu with non-zero lapic id. But boot_cpu_physical_apicid
1894f62bae50SIngo Molnar 	 * might be zero if read from MP tables. Get it from LAPIC.
1895f62bae50SIngo Molnar 	 */
1896f62bae50SIngo Molnar # ifdef CONFIG_CRASH_DUMP
1897f62bae50SIngo Molnar 	boot_cpu_physical_apicid = read_apic_id();
1898f62bae50SIngo Molnar # endif
1899f62bae50SIngo Molnar #endif
1900f62bae50SIngo Molnar 	physid_set_mask_of_physid(boot_cpu_physical_apicid, &phys_cpu_present_map);
1901f62bae50SIngo Molnar 	setup_local_APIC();
1902f62bae50SIngo Molnar 
1903f62bae50SIngo Molnar #ifdef CONFIG_X86_IO_APIC
1904f62bae50SIngo Molnar 	/*
1905f62bae50SIngo Molnar 	 * Now enable IO-APICs, actually call clear_IO_APIC
1906f62bae50SIngo Molnar 	 * We need clear_IO_APIC before enabling error vector
1907f62bae50SIngo Molnar 	 */
1908f62bae50SIngo Molnar 	if (!skip_ioapic_setup && nr_ioapics)
1909f62bae50SIngo Molnar 		enable_IO_APIC();
1910f62bae50SIngo Molnar #endif
1911f62bae50SIngo Molnar 
19122fb270f3SJan Beulich 	bsp_end_local_APIC_setup();
1913f62bae50SIngo Molnar 
1914f62bae50SIngo Molnar #ifdef CONFIG_X86_IO_APIC
1915f62bae50SIngo Molnar 	if (smp_found_config && !skip_ioapic_setup && nr_ioapics)
1916f62bae50SIngo Molnar 		setup_IO_APIC();
1917f62bae50SIngo Molnar 	else {
1918f62bae50SIngo Molnar 		nr_ioapics = 0;
1919f62bae50SIngo Molnar 	}
1920f62bae50SIngo Molnar #endif
1921f62bae50SIngo Molnar 
1922736decacSThomas Gleixner 	x86_init.timers.setup_percpu_clockev();
1923f62bae50SIngo Molnar 	return 0;
1924f62bae50SIngo Molnar }
1925f62bae50SIngo Molnar 
1926f62bae50SIngo Molnar /*
1927f62bae50SIngo Molnar  * Local APIC interrupts
1928f62bae50SIngo Molnar  */
1929f62bae50SIngo Molnar 
1930f62bae50SIngo Molnar /*
1931f62bae50SIngo Molnar  * This interrupt should _never_ happen with our APIC/SMP architecture
1932f62bae50SIngo Molnar  */
1933eddc0e92SSeiji Aguchi static inline void __smp_spurious_interrupt(void)
1934f62bae50SIngo Molnar {
1935f62bae50SIngo Molnar 	u32 v;
1936f62bae50SIngo Molnar 
1937f62bae50SIngo Molnar 	/*
1938f62bae50SIngo Molnar 	 * Check if this really is a spurious interrupt and ACK it
1939f62bae50SIngo Molnar 	 * if it is a vectored one.  Just in case...
1940f62bae50SIngo Molnar 	 * Spurious interrupts should not be ACKed.
1941f62bae50SIngo Molnar 	 */
1942f62bae50SIngo Molnar 	v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
1943f62bae50SIngo Molnar 	if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
1944f62bae50SIngo Molnar 		ack_APIC_irq();
1945f62bae50SIngo Molnar 
1946f62bae50SIngo Molnar 	inc_irq_stat(irq_spurious_count);
1947f62bae50SIngo Molnar 
1948f62bae50SIngo Molnar 	/* see sw-dev-man vol 3, chapter 7.4.13.5 */
1949f62bae50SIngo Molnar 	pr_info("spurious APIC interrupt on CPU#%d, "
1950f62bae50SIngo Molnar 		"should never happen.\n", smp_processor_id());
1951eddc0e92SSeiji Aguchi }
1952eddc0e92SSeiji Aguchi 
19531d9090e2SAndi Kleen __visible void smp_spurious_interrupt(struct pt_regs *regs)
1954eddc0e92SSeiji Aguchi {
1955eddc0e92SSeiji Aguchi 	entering_irq();
1956eddc0e92SSeiji Aguchi 	__smp_spurious_interrupt();
1957eddc0e92SSeiji Aguchi 	exiting_irq();
1958f62bae50SIngo Molnar }
1959f62bae50SIngo Molnar 
19601d9090e2SAndi Kleen __visible void smp_trace_spurious_interrupt(struct pt_regs *regs)
1961cf910e83SSeiji Aguchi {
1962cf910e83SSeiji Aguchi 	entering_irq();
1963cf910e83SSeiji Aguchi 	trace_spurious_apic_entry(SPURIOUS_APIC_VECTOR);
1964cf910e83SSeiji Aguchi 	__smp_spurious_interrupt();
1965cf910e83SSeiji Aguchi 	trace_spurious_apic_exit(SPURIOUS_APIC_VECTOR);
1966cf910e83SSeiji Aguchi 	exiting_irq();
1967f62bae50SIngo Molnar }
1968f62bae50SIngo Molnar 
1969f62bae50SIngo Molnar /*
1970f62bae50SIngo Molnar  * This interrupt should never happen with our APIC/SMP architecture
1971f62bae50SIngo Molnar  */
1972eddc0e92SSeiji Aguchi static inline void __smp_error_interrupt(struct pt_regs *regs)
1973f62bae50SIngo Molnar {
197460283df7SRichard Weinberger 	u32 v;
19752b398bd9SYouquan Song 	u32 i = 0;
19762b398bd9SYouquan Song 	static const char * const error_interrupt_reason[] = {
19772b398bd9SYouquan Song 		"Send CS error",		/* APIC Error Bit 0 */
19782b398bd9SYouquan Song 		"Receive CS error",		/* APIC Error Bit 1 */
19792b398bd9SYouquan Song 		"Send accept error",		/* APIC Error Bit 2 */
19802b398bd9SYouquan Song 		"Receive accept error",		/* APIC Error Bit 3 */
19812b398bd9SYouquan Song 		"Redirectable IPI",		/* APIC Error Bit 4 */
19822b398bd9SYouquan Song 		"Send illegal vector",		/* APIC Error Bit 5 */
19832b398bd9SYouquan Song 		"Received illegal vector",	/* APIC Error Bit 6 */
19842b398bd9SYouquan Song 		"Illegal register address",	/* APIC Error Bit 7 */
19852b398bd9SYouquan Song 	};
1986f62bae50SIngo Molnar 
1987f62bae50SIngo Molnar 	/* First tickle the hardware, only then report what went on. -- REW */
1988023de4a0SMaciej W. Rozycki 	if (lapic_get_maxlvt() > 3)	/* Due to the Pentium erratum 3AP. */
1989f62bae50SIngo Molnar 		apic_write(APIC_ESR, 0);
199060283df7SRichard Weinberger 	v = apic_read(APIC_ESR);
1991f62bae50SIngo Molnar 	ack_APIC_irq();
1992f62bae50SIngo Molnar 	atomic_inc(&irq_err_count);
1993f62bae50SIngo Molnar 
199460283df7SRichard Weinberger 	apic_printk(APIC_DEBUG, KERN_DEBUG "APIC error on CPU%d: %02x",
199560283df7SRichard Weinberger 		    smp_processor_id(), v);
19962b398bd9SYouquan Song 
199760283df7SRichard Weinberger 	v &= 0xff;
199860283df7SRichard Weinberger 	while (v) {
199960283df7SRichard Weinberger 		if (v & 0x1)
20002b398bd9SYouquan Song 			apic_printk(APIC_DEBUG, KERN_CONT " : %s", error_interrupt_reason[i]);
20012b398bd9SYouquan Song 		i++;
200260283df7SRichard Weinberger 		v >>= 1;
20034b8073e4SPeter Senna Tschudin 	}
20042b398bd9SYouquan Song 
20052b398bd9SYouquan Song 	apic_printk(APIC_DEBUG, KERN_CONT "\n");
20062b398bd9SYouquan Song 
2007eddc0e92SSeiji Aguchi }
2008eddc0e92SSeiji Aguchi 
20091d9090e2SAndi Kleen __visible void smp_error_interrupt(struct pt_regs *regs)
2010eddc0e92SSeiji Aguchi {
2011eddc0e92SSeiji Aguchi 	entering_irq();
2012eddc0e92SSeiji Aguchi 	__smp_error_interrupt(regs);
2013eddc0e92SSeiji Aguchi 	exiting_irq();
2014f62bae50SIngo Molnar }
2015f62bae50SIngo Molnar 
20161d9090e2SAndi Kleen __visible void smp_trace_error_interrupt(struct pt_regs *regs)
2017cf910e83SSeiji Aguchi {
2018cf910e83SSeiji Aguchi 	entering_irq();
2019cf910e83SSeiji Aguchi 	trace_error_apic_entry(ERROR_APIC_VECTOR);
2020cf910e83SSeiji Aguchi 	__smp_error_interrupt(regs);
2021cf910e83SSeiji Aguchi 	trace_error_apic_exit(ERROR_APIC_VECTOR);
2022cf910e83SSeiji Aguchi 	exiting_irq();
2023f62bae50SIngo Molnar }
2024f62bae50SIngo Molnar 
2025f62bae50SIngo Molnar /**
2026f62bae50SIngo Molnar  * connect_bsp_APIC - attach the APIC to the interrupt system
2027f62bae50SIngo Molnar  */
2028f62bae50SIngo Molnar void __init connect_bsp_APIC(void)
2029f62bae50SIngo Molnar {
2030f62bae50SIngo Molnar #ifdef CONFIG_X86_32
2031f62bae50SIngo Molnar 	if (pic_mode) {
2032f62bae50SIngo Molnar 		/*
2033f62bae50SIngo Molnar 		 * Do not trust the local APIC being empty at bootup.
2034f62bae50SIngo Molnar 		 */
2035f62bae50SIngo Molnar 		clear_local_APIC();
2036f62bae50SIngo Molnar 		/*
2037f62bae50SIngo Molnar 		 * PIC mode, enable APIC mode in the IMCR, i.e.  connect BSP's
2038f62bae50SIngo Molnar 		 * local APIC to INT and NMI lines.
2039f62bae50SIngo Molnar 		 */
2040f62bae50SIngo Molnar 		apic_printk(APIC_VERBOSE, "leaving PIC mode, "
2041f62bae50SIngo Molnar 				"enabling APIC mode.\n");
2042c0eaa453SCyrill Gorcunov 		imcr_pic_to_apic();
2043f62bae50SIngo Molnar 	}
2044f62bae50SIngo Molnar #endif
2045f62bae50SIngo Molnar }
2046f62bae50SIngo Molnar 
2047f62bae50SIngo Molnar /**
2048f62bae50SIngo Molnar  * disconnect_bsp_APIC - detach the APIC from the interrupt system
2049f62bae50SIngo Molnar  * @virt_wire_setup:	indicates, whether virtual wire mode is selected
2050f62bae50SIngo Molnar  *
2051f62bae50SIngo Molnar  * Virtual wire mode is necessary to deliver legacy interrupts even when the
2052f62bae50SIngo Molnar  * APIC is disabled.
2053f62bae50SIngo Molnar  */
2054f62bae50SIngo Molnar void disconnect_bsp_APIC(int virt_wire_setup)
2055f62bae50SIngo Molnar {
2056f62bae50SIngo Molnar 	unsigned int value;
2057f62bae50SIngo Molnar 
2058f62bae50SIngo Molnar #ifdef CONFIG_X86_32
2059f62bae50SIngo Molnar 	if (pic_mode) {
2060f62bae50SIngo Molnar 		/*
2061f62bae50SIngo Molnar 		 * Put the board back into PIC mode (has an effect only on
2062f62bae50SIngo Molnar 		 * certain older boards).  Note that APIC interrupts, including
2063f62bae50SIngo Molnar 		 * IPIs, won't work beyond this point!  The only exception are
2064f62bae50SIngo Molnar 		 * INIT IPIs.
2065f62bae50SIngo Molnar 		 */
2066f62bae50SIngo Molnar 		apic_printk(APIC_VERBOSE, "disabling APIC mode, "
2067f62bae50SIngo Molnar 				"entering PIC mode.\n");
2068c0eaa453SCyrill Gorcunov 		imcr_apic_to_pic();
2069f62bae50SIngo Molnar 		return;
2070f62bae50SIngo Molnar 	}
2071f62bae50SIngo Molnar #endif
2072f62bae50SIngo Molnar 
2073f62bae50SIngo Molnar 	/* Go back to Virtual Wire compatibility mode */
2074f62bae50SIngo Molnar 
2075f62bae50SIngo Molnar 	/* For the spurious interrupt use vector F, and enable it */
2076f62bae50SIngo Molnar 	value = apic_read(APIC_SPIV);
2077f62bae50SIngo Molnar 	value &= ~APIC_VECTOR_MASK;
2078f62bae50SIngo Molnar 	value |= APIC_SPIV_APIC_ENABLED;
2079f62bae50SIngo Molnar 	value |= 0xf;
2080f62bae50SIngo Molnar 	apic_write(APIC_SPIV, value);
2081f62bae50SIngo Molnar 
2082f62bae50SIngo Molnar 	if (!virt_wire_setup) {
2083f62bae50SIngo Molnar 		/*
2084f62bae50SIngo Molnar 		 * For LVT0 make it edge triggered, active high,
2085f62bae50SIngo Molnar 		 * external and enabled
2086f62bae50SIngo Molnar 		 */
2087f62bae50SIngo Molnar 		value = apic_read(APIC_LVT0);
2088f62bae50SIngo Molnar 		value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
2089f62bae50SIngo Molnar 			APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
2090f62bae50SIngo Molnar 			APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
2091f62bae50SIngo Molnar 		value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
2092f62bae50SIngo Molnar 		value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
2093f62bae50SIngo Molnar 		apic_write(APIC_LVT0, value);
2094f62bae50SIngo Molnar 	} else {
2095f62bae50SIngo Molnar 		/* Disable LVT0 */
2096f62bae50SIngo Molnar 		apic_write(APIC_LVT0, APIC_LVT_MASKED);
2097f62bae50SIngo Molnar 	}
2098f62bae50SIngo Molnar 
2099f62bae50SIngo Molnar 	/*
2100f62bae50SIngo Molnar 	 * For LVT1 make it edge triggered, active high,
2101f62bae50SIngo Molnar 	 * nmi and enabled
2102f62bae50SIngo Molnar 	 */
2103f62bae50SIngo Molnar 	value = apic_read(APIC_LVT1);
2104f62bae50SIngo Molnar 	value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
2105f62bae50SIngo Molnar 			APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
2106f62bae50SIngo Molnar 			APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
2107f62bae50SIngo Molnar 	value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
2108f62bae50SIngo Molnar 	value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
2109f62bae50SIngo Molnar 	apic_write(APIC_LVT1, value);
2110f62bae50SIngo Molnar }
2111f62bae50SIngo Molnar 
21127e1f85f9SJiang Liu int generic_processor_info(int apicid, int version)
2113f62bae50SIngo Molnar {
211414cb6dcfSVivek Goyal 	int cpu, max = nr_cpu_ids;
211514cb6dcfSVivek Goyal 	bool boot_cpu_detected = physid_isset(boot_cpu_physical_apicid,
211614cb6dcfSVivek Goyal 				phys_cpu_present_map);
211714cb6dcfSVivek Goyal 
211814cb6dcfSVivek Goyal 	/*
2119151e0c7dSHATAYAMA Daisuke 	 * boot_cpu_physical_apicid is designed to have the apicid
2120151e0c7dSHATAYAMA Daisuke 	 * returned by read_apic_id(), i.e, the apicid of the
2121151e0c7dSHATAYAMA Daisuke 	 * currently booting-up processor. However, on some platforms,
21225b4d1dbcSH. Peter Anvin 	 * it is temporarily modified by the apicid reported as BSP
2123151e0c7dSHATAYAMA Daisuke 	 * through MP table. Concretely:
2124151e0c7dSHATAYAMA Daisuke 	 *
2125151e0c7dSHATAYAMA Daisuke 	 * - arch/x86/kernel/mpparse.c: MP_processor_info()
2126151e0c7dSHATAYAMA Daisuke 	 * - arch/x86/mm/amdtopology.c: amd_numa_init()
2127151e0c7dSHATAYAMA Daisuke 	 *
2128151e0c7dSHATAYAMA Daisuke 	 * This function is executed with the modified
2129151e0c7dSHATAYAMA Daisuke 	 * boot_cpu_physical_apicid. So, disabled_cpu_apicid kernel
2130151e0c7dSHATAYAMA Daisuke 	 * parameter doesn't work to disable APs on kdump 2nd kernel.
2131151e0c7dSHATAYAMA Daisuke 	 *
2132151e0c7dSHATAYAMA Daisuke 	 * Since fixing handling of boot_cpu_physical_apicid requires
2133151e0c7dSHATAYAMA Daisuke 	 * another discussion and tests on each platform, we leave it
2134151e0c7dSHATAYAMA Daisuke 	 * for now and here we use read_apic_id() directly in this
2135151e0c7dSHATAYAMA Daisuke 	 * function, generic_processor_info().
2136151e0c7dSHATAYAMA Daisuke 	 */
2137151e0c7dSHATAYAMA Daisuke 	if (disabled_cpu_apicid != BAD_APICID &&
2138151e0c7dSHATAYAMA Daisuke 	    disabled_cpu_apicid != read_apic_id() &&
2139151e0c7dSHATAYAMA Daisuke 	    disabled_cpu_apicid == apicid) {
2140151e0c7dSHATAYAMA Daisuke 		int thiscpu = num_processors + disabled_cpus;
2141151e0c7dSHATAYAMA Daisuke 
21425b4d1dbcSH. Peter Anvin 		pr_warning("APIC: Disabling requested cpu."
2143151e0c7dSHATAYAMA Daisuke 			   " Processor %d/0x%x ignored.\n",
2144151e0c7dSHATAYAMA Daisuke 			   thiscpu, apicid);
2145151e0c7dSHATAYAMA Daisuke 
2146151e0c7dSHATAYAMA Daisuke 		disabled_cpus++;
2147151e0c7dSHATAYAMA Daisuke 		return -ENODEV;
2148151e0c7dSHATAYAMA Daisuke 	}
2149151e0c7dSHATAYAMA Daisuke 
2150151e0c7dSHATAYAMA Daisuke 	/*
215114cb6dcfSVivek Goyal 	 * If boot cpu has not been detected yet, then only allow upto
215214cb6dcfSVivek Goyal 	 * nr_cpu_ids - 1 processors and keep one slot free for boot cpu
215314cb6dcfSVivek Goyal 	 */
215414cb6dcfSVivek Goyal 	if (!boot_cpu_detected && num_processors >= nr_cpu_ids - 1 &&
215514cb6dcfSVivek Goyal 	    apicid != boot_cpu_physical_apicid) {
215614cb6dcfSVivek Goyal 		int thiscpu = max + disabled_cpus - 1;
215714cb6dcfSVivek Goyal 
215814cb6dcfSVivek Goyal 		pr_warning(
215914cb6dcfSVivek Goyal 			"ACPI: NR_CPUS/possible_cpus limit of %i almost"
216014cb6dcfSVivek Goyal 			" reached. Keeping one slot for boot cpu."
216114cb6dcfSVivek Goyal 			"  Processor %d/0x%x ignored.\n", max, thiscpu, apicid);
216214cb6dcfSVivek Goyal 
216314cb6dcfSVivek Goyal 		disabled_cpus++;
21647e1f85f9SJiang Liu 		return -ENODEV;
216514cb6dcfSVivek Goyal 	}
2166f62bae50SIngo Molnar 
2167f62bae50SIngo Molnar 	if (num_processors >= nr_cpu_ids) {
2168f62bae50SIngo Molnar 		int thiscpu = max + disabled_cpus;
2169f62bae50SIngo Molnar 
2170f62bae50SIngo Molnar 		pr_warning(
2171f62bae50SIngo Molnar 			"ACPI: NR_CPUS/possible_cpus limit of %i reached."
2172f62bae50SIngo Molnar 			"  Processor %d/0x%x ignored.\n", max, thiscpu, apicid);
2173f62bae50SIngo Molnar 
2174f62bae50SIngo Molnar 		disabled_cpus++;
21757e1f85f9SJiang Liu 		return -EINVAL;
2176f62bae50SIngo Molnar 	}
2177f62bae50SIngo Molnar 
2178f62bae50SIngo Molnar 	num_processors++;
2179f62bae50SIngo Molnar 	if (apicid == boot_cpu_physical_apicid) {
2180f62bae50SIngo Molnar 		/*
2181f62bae50SIngo Molnar 		 * x86_bios_cpu_apicid is required to have processors listed
2182f62bae50SIngo Molnar 		 * in same order as logical cpu numbers. Hence the first
2183f62bae50SIngo Molnar 		 * entry is BSP, and so on.
2184e5fea868SYinghai Lu 		 * boot_cpu_init() already hold bit 0 in cpu_present_mask
2185e5fea868SYinghai Lu 		 * for BSP.
2186f62bae50SIngo Molnar 		 */
2187f62bae50SIngo Molnar 		cpu = 0;
2188e5fea868SYinghai Lu 	} else
2189e5fea868SYinghai Lu 		cpu = cpumask_next_zero(-1, cpu_present_mask);
2190e5fea868SYinghai Lu 
2191e5fea868SYinghai Lu 	/*
2192e5fea868SYinghai Lu 	 * Validate version
2193e5fea868SYinghai Lu 	 */
2194e5fea868SYinghai Lu 	if (version == 0x0) {
2195e5fea868SYinghai Lu 		pr_warning("BIOS bug: APIC version is 0 for CPU %d/0x%x, fixing up to 0x10\n",
2196e5fea868SYinghai Lu 			   cpu, apicid);
2197e5fea868SYinghai Lu 		version = 0x10;
2198f62bae50SIngo Molnar 	}
2199e5fea868SYinghai Lu 	apic_version[apicid] = version;
2200e5fea868SYinghai Lu 
2201e5fea868SYinghai Lu 	if (version != apic_version[boot_cpu_physical_apicid]) {
2202e5fea868SYinghai Lu 		pr_warning("BIOS bug: APIC version mismatch, boot CPU: %x, CPU %d: version %x\n",
2203e5fea868SYinghai Lu 			apic_version[boot_cpu_physical_apicid], cpu, version);
2204e5fea868SYinghai Lu 	}
2205e5fea868SYinghai Lu 
2206e5fea868SYinghai Lu 	physid_set(apicid, phys_cpu_present_map);
2207f62bae50SIngo Molnar 	if (apicid > max_physical_apicid)
2208f62bae50SIngo Molnar 		max_physical_apicid = apicid;
2209f62bae50SIngo Molnar 
2210f62bae50SIngo Molnar #if defined(CONFIG_SMP) || defined(CONFIG_X86_64)
2211f62bae50SIngo Molnar 	early_per_cpu(x86_cpu_to_apicid, cpu) = apicid;
2212f62bae50SIngo Molnar 	early_per_cpu(x86_bios_cpu_apicid, cpu) = apicid;
2213f62bae50SIngo Molnar #endif
2214acb8bc09STejun Heo #ifdef CONFIG_X86_32
2215acb8bc09STejun Heo 	early_per_cpu(x86_cpu_to_logical_apicid, cpu) =
2216acb8bc09STejun Heo 		apic->x86_32_early_logical_apicid(cpu);
2217acb8bc09STejun Heo #endif
2218f62bae50SIngo Molnar 	set_cpu_possible(cpu, true);
2219f62bae50SIngo Molnar 	set_cpu_present(cpu, true);
22207e1f85f9SJiang Liu 
22217e1f85f9SJiang Liu 	return cpu;
2222f62bae50SIngo Molnar }
2223f62bae50SIngo Molnar 
2224f62bae50SIngo Molnar int hard_smp_processor_id(void)
2225f62bae50SIngo Molnar {
2226f62bae50SIngo Molnar 	return read_apic_id();
2227f62bae50SIngo Molnar }
2228f62bae50SIngo Molnar 
2229f62bae50SIngo Molnar void default_init_apic_ldr(void)
2230f62bae50SIngo Molnar {
2231f62bae50SIngo Molnar 	unsigned long val;
2232f62bae50SIngo Molnar 
2233f62bae50SIngo Molnar 	apic_write(APIC_DFR, APIC_DFR_VALUE);
2234f62bae50SIngo Molnar 	val = apic_read(APIC_LDR) & ~APIC_LDR_MASK;
2235f62bae50SIngo Molnar 	val |= SET_APIC_LOGICAL_ID(1UL << smp_processor_id());
2236f62bae50SIngo Molnar 	apic_write(APIC_LDR, val);
2237f62bae50SIngo Molnar }
2238f62bae50SIngo Molnar 
2239ff164324SAlexander Gordeev int default_cpu_mask_to_apicid_and(const struct cpumask *cpumask,
2240ff164324SAlexander Gordeev 				   const struct cpumask *andmask,
2241ff164324SAlexander Gordeev 				   unsigned int *apicid)
22426398268dSAlexander Gordeev {
2243ea3807eaSAlexander Gordeev 	unsigned int cpu;
22446398268dSAlexander Gordeev 
22456398268dSAlexander Gordeev 	for_each_cpu_and(cpu, cpumask, andmask) {
22466398268dSAlexander Gordeev 		if (cpumask_test_cpu(cpu, cpu_online_mask))
22476398268dSAlexander Gordeev 			break;
22486398268dSAlexander Gordeev 	}
2249ff164324SAlexander Gordeev 
2250ea3807eaSAlexander Gordeev 	if (likely(cpu < nr_cpu_ids)) {
2251a5a39156SAlexander Gordeev 		*apicid = per_cpu(x86_cpu_to_apicid, cpu);
2252a5a39156SAlexander Gordeev 		return 0;
2253a5a39156SAlexander Gordeev 	}
2254ea3807eaSAlexander Gordeev 
2255ea3807eaSAlexander Gordeev 	return -EINVAL;
22566398268dSAlexander Gordeev }
22576398268dSAlexander Gordeev 
2258f62bae50SIngo Molnar /*
22591551df64SMichael S. Tsirkin  * Override the generic EOI implementation with an optimized version.
22601551df64SMichael S. Tsirkin  * Only called during early boot when only one CPU is active and with
22611551df64SMichael S. Tsirkin  * interrupts disabled, so we know this does not race with actual APIC driver
22621551df64SMichael S. Tsirkin  * use.
22631551df64SMichael S. Tsirkin  */
22641551df64SMichael S. Tsirkin void __init apic_set_eoi_write(void (*eoi_write)(u32 reg, u32 v))
22651551df64SMichael S. Tsirkin {
22661551df64SMichael S. Tsirkin 	struct apic **drv;
22671551df64SMichael S. Tsirkin 
22681551df64SMichael S. Tsirkin 	for (drv = __apicdrivers; drv < __apicdrivers_end; drv++) {
22691551df64SMichael S. Tsirkin 		/* Should happen once for each apic */
22701551df64SMichael S. Tsirkin 		WARN_ON((*drv)->eoi_write == eoi_write);
22711551df64SMichael S. Tsirkin 		(*drv)->eoi_write = eoi_write;
22721551df64SMichael S. Tsirkin 	}
22731551df64SMichael S. Tsirkin }
22741551df64SMichael S. Tsirkin 
22751551df64SMichael S. Tsirkin /*
2276f62bae50SIngo Molnar  * Power management
2277f62bae50SIngo Molnar  */
2278f62bae50SIngo Molnar #ifdef CONFIG_PM
2279f62bae50SIngo Molnar 
2280f62bae50SIngo Molnar static struct {
2281f62bae50SIngo Molnar 	/*
2282f62bae50SIngo Molnar 	 * 'active' is true if the local APIC was enabled by us and
2283f62bae50SIngo Molnar 	 * not the BIOS; this signifies that we are also responsible
2284f62bae50SIngo Molnar 	 * for disabling it before entering apm/acpi suspend
2285f62bae50SIngo Molnar 	 */
2286f62bae50SIngo Molnar 	int active;
2287f62bae50SIngo Molnar 	/* r/w apic fields */
2288f62bae50SIngo Molnar 	unsigned int apic_id;
2289f62bae50SIngo Molnar 	unsigned int apic_taskpri;
2290f62bae50SIngo Molnar 	unsigned int apic_ldr;
2291f62bae50SIngo Molnar 	unsigned int apic_dfr;
2292f62bae50SIngo Molnar 	unsigned int apic_spiv;
2293f62bae50SIngo Molnar 	unsigned int apic_lvtt;
2294f62bae50SIngo Molnar 	unsigned int apic_lvtpc;
2295f62bae50SIngo Molnar 	unsigned int apic_lvt0;
2296f62bae50SIngo Molnar 	unsigned int apic_lvt1;
2297f62bae50SIngo Molnar 	unsigned int apic_lvterr;
2298f62bae50SIngo Molnar 	unsigned int apic_tmict;
2299f62bae50SIngo Molnar 	unsigned int apic_tdcr;
2300f62bae50SIngo Molnar 	unsigned int apic_thmr;
2301f62bae50SIngo Molnar } apic_pm_state;
2302f62bae50SIngo Molnar 
2303f3c6ea1bSRafael J. Wysocki static int lapic_suspend(void)
2304f62bae50SIngo Molnar {
2305f62bae50SIngo Molnar 	unsigned long flags;
2306f62bae50SIngo Molnar 	int maxlvt;
2307f62bae50SIngo Molnar 
2308f62bae50SIngo Molnar 	if (!apic_pm_state.active)
2309f62bae50SIngo Molnar 		return 0;
2310f62bae50SIngo Molnar 
2311f62bae50SIngo Molnar 	maxlvt = lapic_get_maxlvt();
2312f62bae50SIngo Molnar 
2313f62bae50SIngo Molnar 	apic_pm_state.apic_id = apic_read(APIC_ID);
2314f62bae50SIngo Molnar 	apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
2315f62bae50SIngo Molnar 	apic_pm_state.apic_ldr = apic_read(APIC_LDR);
2316f62bae50SIngo Molnar 	apic_pm_state.apic_dfr = apic_read(APIC_DFR);
2317f62bae50SIngo Molnar 	apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
2318f62bae50SIngo Molnar 	apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
2319f62bae50SIngo Molnar 	if (maxlvt >= 4)
2320f62bae50SIngo Molnar 		apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
2321f62bae50SIngo Molnar 	apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
2322f62bae50SIngo Molnar 	apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
2323f62bae50SIngo Molnar 	apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
2324f62bae50SIngo Molnar 	apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
2325f62bae50SIngo Molnar 	apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
23264efc0670SAndi Kleen #ifdef CONFIG_X86_THERMAL_VECTOR
2327f62bae50SIngo Molnar 	if (maxlvt >= 5)
2328f62bae50SIngo Molnar 		apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
2329f62bae50SIngo Molnar #endif
2330f62bae50SIngo Molnar 
2331f62bae50SIngo Molnar 	local_irq_save(flags);
2332f62bae50SIngo Molnar 	disable_local_APIC();
2333fc1edaf9SSuresh Siddha 
233495a02e97SSuresh Siddha 	irq_remapping_disable();
2335fc1edaf9SSuresh Siddha 
2336f62bae50SIngo Molnar 	local_irq_restore(flags);
2337f62bae50SIngo Molnar 	return 0;
2338f62bae50SIngo Molnar }
2339f62bae50SIngo Molnar 
2340f3c6ea1bSRafael J. Wysocki static void lapic_resume(void)
2341f62bae50SIngo Molnar {
2342f62bae50SIngo Molnar 	unsigned int l, h;
2343f62bae50SIngo Molnar 	unsigned long flags;
234431dce14aSSuresh Siddha 	int maxlvt;
2345b24696bcSFenghua Yu 
2346f62bae50SIngo Molnar 	if (!apic_pm_state.active)
2347f3c6ea1bSRafael J. Wysocki 		return;
2348f62bae50SIngo Molnar 
2349b24696bcSFenghua Yu 	local_irq_save(flags);
2350336224baSJoerg Roedel 
235131dce14aSSuresh Siddha 	/*
235231dce14aSSuresh Siddha 	 * IO-APIC and PIC have their own resume routines.
235331dce14aSSuresh Siddha 	 * We just mask them here to make sure the interrupt
235431dce14aSSuresh Siddha 	 * subsystem is completely quiet while we enable x2apic
235531dce14aSSuresh Siddha 	 * and interrupt-remapping.
235631dce14aSSuresh Siddha 	 */
235731dce14aSSuresh Siddha 	mask_ioapic_entries();
2358b81bb373SJacob Pan 	legacy_pic->mask_all();
2359f62bae50SIngo Molnar 
2360fc1edaf9SSuresh Siddha 	if (x2apic_mode)
2361f62bae50SIngo Molnar 		enable_x2apic();
2362cf6567feSSuresh Siddha 	else {
2363f62bae50SIngo Molnar 		/*
2364f62bae50SIngo Molnar 		 * Make sure the APICBASE points to the right address
2365f62bae50SIngo Molnar 		 *
2366f62bae50SIngo Molnar 		 * FIXME! This will be wrong if we ever support suspend on
2367f62bae50SIngo Molnar 		 * SMP! We'll need to do this as part of the CPU restore!
2368f62bae50SIngo Molnar 		 */
2369cbf2829bSBryan O'Donoghue 		if (boot_cpu_data.x86 >= 6) {
2370f62bae50SIngo Molnar 			rdmsr(MSR_IA32_APICBASE, l, h);
2371f62bae50SIngo Molnar 			l &= ~MSR_IA32_APICBASE_BASE;
2372f62bae50SIngo Molnar 			l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
2373f62bae50SIngo Molnar 			wrmsr(MSR_IA32_APICBASE, l, h);
2374f62bae50SIngo Molnar 		}
2375cbf2829bSBryan O'Donoghue 	}
2376f62bae50SIngo Molnar 
2377b24696bcSFenghua Yu 	maxlvt = lapic_get_maxlvt();
2378f62bae50SIngo Molnar 	apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
2379f62bae50SIngo Molnar 	apic_write(APIC_ID, apic_pm_state.apic_id);
2380f62bae50SIngo Molnar 	apic_write(APIC_DFR, apic_pm_state.apic_dfr);
2381f62bae50SIngo Molnar 	apic_write(APIC_LDR, apic_pm_state.apic_ldr);
2382f62bae50SIngo Molnar 	apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
2383f62bae50SIngo Molnar 	apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
2384f62bae50SIngo Molnar 	apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
2385f62bae50SIngo Molnar 	apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
238671c69f7fSPaul Bolle #if defined(CONFIG_X86_MCE_INTEL)
2387f62bae50SIngo Molnar 	if (maxlvt >= 5)
2388f62bae50SIngo Molnar 		apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
2389f62bae50SIngo Molnar #endif
2390f62bae50SIngo Molnar 	if (maxlvt >= 4)
2391f62bae50SIngo Molnar 		apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
2392f62bae50SIngo Molnar 	apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
2393f62bae50SIngo Molnar 	apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
2394f62bae50SIngo Molnar 	apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
2395f62bae50SIngo Molnar 	apic_write(APIC_ESR, 0);
2396f62bae50SIngo Molnar 	apic_read(APIC_ESR);
2397f62bae50SIngo Molnar 	apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
2398f62bae50SIngo Molnar 	apic_write(APIC_ESR, 0);
2399f62bae50SIngo Molnar 	apic_read(APIC_ESR);
2400f62bae50SIngo Molnar 
240195a02e97SSuresh Siddha 	irq_remapping_reenable(x2apic_mode);
240231dce14aSSuresh Siddha 
2403f62bae50SIngo Molnar 	local_irq_restore(flags);
2404f62bae50SIngo Molnar }
2405f62bae50SIngo Molnar 
2406f62bae50SIngo Molnar /*
2407f62bae50SIngo Molnar  * This device has no shutdown method - fully functioning local APICs
2408f62bae50SIngo Molnar  * are needed on every CPU up until machine_halt/restart/poweroff.
2409f62bae50SIngo Molnar  */
2410f62bae50SIngo Molnar 
2411f3c6ea1bSRafael J. Wysocki static struct syscore_ops lapic_syscore_ops = {
2412f62bae50SIngo Molnar 	.resume		= lapic_resume,
2413f62bae50SIngo Molnar 	.suspend	= lapic_suspend,
2414f62bae50SIngo Molnar };
2415f62bae50SIngo Molnar 
2416148f9bb8SPaul Gortmaker static void apic_pm_activate(void)
2417f62bae50SIngo Molnar {
2418f62bae50SIngo Molnar 	apic_pm_state.active = 1;
2419f62bae50SIngo Molnar }
2420f62bae50SIngo Molnar 
2421f62bae50SIngo Molnar static int __init init_lapic_sysfs(void)
2422f62bae50SIngo Molnar {
2423f62bae50SIngo Molnar 	/* XXX: remove suspend/resume procs if !apic_pm_state.active? */
2424f3c6ea1bSRafael J. Wysocki 	if (cpu_has_apic)
2425f3c6ea1bSRafael J. Wysocki 		register_syscore_ops(&lapic_syscore_ops);
2426f62bae50SIngo Molnar 
2427f3c6ea1bSRafael J. Wysocki 	return 0;
2428f62bae50SIngo Molnar }
2429b24696bcSFenghua Yu 
2430b24696bcSFenghua Yu /* local apic needs to resume before other devices access its registers. */
2431b24696bcSFenghua Yu core_initcall(init_lapic_sysfs);
2432f62bae50SIngo Molnar 
2433f62bae50SIngo Molnar #else	/* CONFIG_PM */
2434f62bae50SIngo Molnar 
2435f62bae50SIngo Molnar static void apic_pm_activate(void) { }
2436f62bae50SIngo Molnar 
2437f62bae50SIngo Molnar #endif	/* CONFIG_PM */
2438f62bae50SIngo Molnar 
2439f62bae50SIngo Molnar #ifdef CONFIG_X86_64
2440e0e42142SYinghai Lu 
2441148f9bb8SPaul Gortmaker static int multi_checked;
2442148f9bb8SPaul Gortmaker static int multi;
2443e0e42142SYinghai Lu 
2444148f9bb8SPaul Gortmaker static int set_multi(const struct dmi_system_id *d)
2445e0e42142SYinghai Lu {
2446e0e42142SYinghai Lu 	if (multi)
2447e0e42142SYinghai Lu 		return 0;
24486f0aced6SCyrill Gorcunov 	pr_info("APIC: %s detected, Multi Chassis\n", d->ident);
2449e0e42142SYinghai Lu 	multi = 1;
2450e0e42142SYinghai Lu 	return 0;
2451e0e42142SYinghai Lu }
2452e0e42142SYinghai Lu 
2453148f9bb8SPaul Gortmaker static const struct dmi_system_id multi_dmi_table[] = {
2454e0e42142SYinghai Lu 	{
2455e0e42142SYinghai Lu 		.callback = set_multi,
2456e0e42142SYinghai Lu 		.ident = "IBM System Summit2",
2457e0e42142SYinghai Lu 		.matches = {
2458e0e42142SYinghai Lu 			DMI_MATCH(DMI_SYS_VENDOR, "IBM"),
2459e0e42142SYinghai Lu 			DMI_MATCH(DMI_PRODUCT_NAME, "Summit2"),
2460e0e42142SYinghai Lu 		},
2461e0e42142SYinghai Lu 	},
2462e0e42142SYinghai Lu 	{}
2463e0e42142SYinghai Lu };
2464e0e42142SYinghai Lu 
2465148f9bb8SPaul Gortmaker static void dmi_check_multi(void)
2466e0e42142SYinghai Lu {
2467e0e42142SYinghai Lu 	if (multi_checked)
2468e0e42142SYinghai Lu 		return;
2469e0e42142SYinghai Lu 
2470e0e42142SYinghai Lu 	dmi_check_system(multi_dmi_table);
2471e0e42142SYinghai Lu 	multi_checked = 1;
2472e0e42142SYinghai Lu }
2473f62bae50SIngo Molnar 
2474f62bae50SIngo Molnar /*
2475e0e42142SYinghai Lu  * apic_is_clustered_box() -- Check if we can expect good TSC
2476e0e42142SYinghai Lu  *
2477e0e42142SYinghai Lu  * Thus far, the major user of this is IBM's Summit2 series:
2478e0e42142SYinghai Lu  * Clustered boxes may have unsynced TSC problems if they are
2479e0e42142SYinghai Lu  * multi-chassis.
2480e0e42142SYinghai Lu  * Use DMI to check them
2481f62bae50SIngo Molnar  */
2482148f9bb8SPaul Gortmaker int apic_is_clustered_box(void)
2483e0e42142SYinghai Lu {
2484e0e42142SYinghai Lu 	dmi_check_multi();
2485411cf9eeSOren Twaig 	return multi;
2486f62bae50SIngo Molnar }
2487f62bae50SIngo Molnar #endif
2488f62bae50SIngo Molnar 
2489f62bae50SIngo Molnar /*
2490f62bae50SIngo Molnar  * APIC command line parameters
2491f62bae50SIngo Molnar  */
2492f62bae50SIngo Molnar static int __init setup_disableapic(char *arg)
2493f62bae50SIngo Molnar {
2494f62bae50SIngo Molnar 	disable_apic = 1;
2495f62bae50SIngo Molnar 	setup_clear_cpu_cap(X86_FEATURE_APIC);
2496f62bae50SIngo Molnar 	return 0;
2497f62bae50SIngo Molnar }
2498f62bae50SIngo Molnar early_param("disableapic", setup_disableapic);
2499f62bae50SIngo Molnar 
2500f62bae50SIngo Molnar /* same as disableapic, for compatibility */
2501f62bae50SIngo Molnar static int __init setup_nolapic(char *arg)
2502f62bae50SIngo Molnar {
2503f62bae50SIngo Molnar 	return setup_disableapic(arg);
2504f62bae50SIngo Molnar }
2505f62bae50SIngo Molnar early_param("nolapic", setup_nolapic);
2506f62bae50SIngo Molnar 
2507f62bae50SIngo Molnar static int __init parse_lapic_timer_c2_ok(char *arg)
2508f62bae50SIngo Molnar {
2509f62bae50SIngo Molnar 	local_apic_timer_c2_ok = 1;
2510f62bae50SIngo Molnar 	return 0;
2511f62bae50SIngo Molnar }
2512f62bae50SIngo Molnar early_param("lapic_timer_c2_ok", parse_lapic_timer_c2_ok);
2513f62bae50SIngo Molnar 
2514f62bae50SIngo Molnar static int __init parse_disable_apic_timer(char *arg)
2515f62bae50SIngo Molnar {
2516f62bae50SIngo Molnar 	disable_apic_timer = 1;
2517f62bae50SIngo Molnar 	return 0;
2518f62bae50SIngo Molnar }
2519f62bae50SIngo Molnar early_param("noapictimer", parse_disable_apic_timer);
2520f62bae50SIngo Molnar 
2521f62bae50SIngo Molnar static int __init parse_nolapic_timer(char *arg)
2522f62bae50SIngo Molnar {
2523f62bae50SIngo Molnar 	disable_apic_timer = 1;
2524f62bae50SIngo Molnar 	return 0;
2525f62bae50SIngo Molnar }
2526f62bae50SIngo Molnar early_param("nolapic_timer", parse_nolapic_timer);
2527f62bae50SIngo Molnar 
2528f62bae50SIngo Molnar static int __init apic_set_verbosity(char *arg)
2529f62bae50SIngo Molnar {
2530f62bae50SIngo Molnar 	if (!arg)  {
2531f62bae50SIngo Molnar #ifdef CONFIG_X86_64
2532f62bae50SIngo Molnar 		skip_ioapic_setup = 0;
2533f62bae50SIngo Molnar 		return 0;
2534f62bae50SIngo Molnar #endif
2535f62bae50SIngo Molnar 		return -EINVAL;
2536f62bae50SIngo Molnar 	}
2537f62bae50SIngo Molnar 
2538f62bae50SIngo Molnar 	if (strcmp("debug", arg) == 0)
2539f62bae50SIngo Molnar 		apic_verbosity = APIC_DEBUG;
2540f62bae50SIngo Molnar 	else if (strcmp("verbose", arg) == 0)
2541f62bae50SIngo Molnar 		apic_verbosity = APIC_VERBOSE;
2542f62bae50SIngo Molnar 	else {
2543f62bae50SIngo Molnar 		pr_warning("APIC Verbosity level %s not recognised"
2544f62bae50SIngo Molnar 			" use apic=verbose or apic=debug\n", arg);
2545f62bae50SIngo Molnar 		return -EINVAL;
2546f62bae50SIngo Molnar 	}
2547f62bae50SIngo Molnar 
2548f62bae50SIngo Molnar 	return 0;
2549f62bae50SIngo Molnar }
2550f62bae50SIngo Molnar early_param("apic", apic_set_verbosity);
2551f62bae50SIngo Molnar 
2552f62bae50SIngo Molnar static int __init lapic_insert_resource(void)
2553f62bae50SIngo Molnar {
2554f62bae50SIngo Molnar 	if (!apic_phys)
2555f62bae50SIngo Molnar 		return -1;
2556f62bae50SIngo Molnar 
2557f62bae50SIngo Molnar 	/* Put local APIC into the resource map. */
2558f62bae50SIngo Molnar 	lapic_resource.start = apic_phys;
2559f62bae50SIngo Molnar 	lapic_resource.end = lapic_resource.start + PAGE_SIZE - 1;
2560f62bae50SIngo Molnar 	insert_resource(&iomem_resource, &lapic_resource);
2561f62bae50SIngo Molnar 
2562f62bae50SIngo Molnar 	return 0;
2563f62bae50SIngo Molnar }
2564f62bae50SIngo Molnar 
2565f62bae50SIngo Molnar /*
2566f62bae50SIngo Molnar  * need call insert after e820_reserve_resources()
2567f62bae50SIngo Molnar  * that is using request_resource
2568f62bae50SIngo Molnar  */
2569f62bae50SIngo Molnar late_initcall(lapic_insert_resource);
2570151e0c7dSHATAYAMA Daisuke 
2571151e0c7dSHATAYAMA Daisuke static int __init apic_set_disabled_cpu_apicid(char *arg)
2572151e0c7dSHATAYAMA Daisuke {
2573151e0c7dSHATAYAMA Daisuke 	if (!arg || !get_option(&arg, &disabled_cpu_apicid))
2574151e0c7dSHATAYAMA Daisuke 		return -EINVAL;
2575151e0c7dSHATAYAMA Daisuke 
2576151e0c7dSHATAYAMA Daisuke 	return 0;
2577151e0c7dSHATAYAMA Daisuke }
2578151e0c7dSHATAYAMA Daisuke early_param("disable_cpu_apicid", apic_set_disabled_cpu_apicid);
2579