xref: /titanic_50/usr/src/uts/i86pc/io/pcplusmp/apic_regops.c (revision 2ef9abdc6ea9bad985430325b12b90938a8cd18f)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <sys/cpuvar.h>
27 #include <sys/psm.h>
28 #include <sys/archsystm.h>
29 #include <sys/apic.h>
30 #include <sys/sunddi.h>
31 #include <sys/ddi_impldefs.h>
32 #include <sys/mach_intr.h>
33 #include <sys/sysmacros.h>
34 #include <sys/trap.h>
35 #include <sys/x86_archext.h>
36 #include <sys/privregs.h>
37 #include <sys/psm_common.h>
38 
39 /* Function prototypes of local apic and X2APIC */
40 static uint64_t local_apic_read(uint32_t reg);
41 static void local_apic_write(uint32_t reg, uint64_t value);
42 static int get_local_apic_pri(void);
43 static void local_apic_write_task_reg(uint64_t value);
44 static void local_apic_write_int_cmd(uint32_t cpu_id, uint32_t cmd1);
45 static uint64_t local_x2apic_read(uint32_t msr);
46 static void local_x2apic_write(uint32_t msr, uint64_t value);
47 static int get_local_x2apic_pri(void);
48 static void local_x2apic_write_task_reg(uint64_t value);
49 static void local_x2apic_write_int_cmd(uint32_t cpu_id, uint32_t cmd1);
50 
51 /*
52  * According to the X2APIC specification:
53  *
54  *   xAPIC global enable    X2APIC enable         Description
55  *   (IA32_APIC_BASE[11])   (IA32_APIC_BASE[10])
56  * -----------------------------------------------------------
57  *      0 			0 	APIC is disabled
58  * 	0			1	Invalid
59  *	1			0	APIC is enabled in xAPIC mode
60  *	1			1	APIC is enabled in X2APIC mode
61  * -----------------------------------------------------------
62  */
63 int	x2apic_enable = 1;
64 int 	apic_mode = LOCAL_APIC;		/* Default mode is Local APIC */
65 
66 /* Uses MMIO (Memory Mapped IO) */
67 static apic_reg_ops_t local_apic_regs_ops = {
68 	local_apic_read,
69 	local_apic_write,
70 	get_local_apic_pri,
71 	local_apic_write_task_reg,
72 	local_apic_write_int_cmd,
73 	apic_send_EOI,
74 };
75 
76 /* X2APIC : Uses RDMSR/WRMSR instructions to access APIC registers */
77 static apic_reg_ops_t x2apic_regs_ops = {
78 	local_x2apic_read,
79 	local_x2apic_write,
80 	get_local_x2apic_pri,
81 	local_x2apic_write_task_reg,
82 	local_x2apic_write_int_cmd,
83 	apic_send_EOI,
84 };
85 
86 
87 /* The default ops is local APIC (Memory Mapped IO) */
88 apic_reg_ops_t *apic_reg_ops = &local_apic_regs_ops;
89 
90 /*
91  * APIC register ops related data sturctures and functions.
92  */
93 int	apic_direct_EOI = 0;			/* Directed EOI Support */
94 
95 void apic_send_EOI();
96 void apic_send_directed_EOI(uint32_t irq);
97 
98 #define	X2APIC_CPUID_BIT	21
99 #define	X2APIC_ENABLE_BIT	10
100 
101 /*
102  * Local APIC Implementation
103  */
104 static uint64_t
105 local_apic_read(uint32_t reg)
106 {
107 	return ((uint32_t)apicadr[reg]);
108 }
109 
110 static void
111 local_apic_write(uint32_t reg, uint64_t value)
112 {
113 	apicadr[reg] = (uint32_t)value;
114 }
115 
116 static int
117 get_local_apic_pri(void)
118 {
119 #if defined(__amd64)
120 	return ((int)getcr8());
121 #else
122 	return (apicadr[APIC_TASK_REG]);
123 #endif
124 }
125 
126 static void
127 local_apic_write_task_reg(uint64_t value)
128 {
129 #if defined(__amd64)
130 	setcr8((ulong_t)(value >> APIC_IPL_SHIFT));
131 #else
132 	apicadr[APIC_TASK_REG] = (uint32_t)value;
133 #endif
134 }
135 
136 static void
137 local_apic_write_int_cmd(uint32_t cpu_id, uint32_t cmd1)
138 {
139 	apicadr[APIC_INT_CMD2] = cpu_id << APIC_ICR_ID_BIT_OFFSET;
140 	apicadr[APIC_INT_CMD1] = cmd1;
141 }
142 
143 /*
144  * X2APIC Implementation.
145  */
146 static uint64_t
147 local_x2apic_read(uint32_t msr)
148 {
149 	uint64_t i;
150 
151 	i = (uint64_t)(rdmsr(REG_X2APIC_BASE_MSR + (msr >> 2)) & 0xffffffff);
152 	return (i);
153 }
154 
155 static void
156 local_x2apic_write(uint32_t msr, uint64_t value)
157 {
158 	uint64_t tmp;
159 
160 	if (msr != APIC_EOI_REG) {
161 		tmp = rdmsr(REG_X2APIC_BASE_MSR + (msr >> 2));
162 		tmp = (tmp & 0xffffffff00000000) | value;
163 	} else {
164 		tmp = 0;
165 	}
166 
167 	wrmsr((REG_X2APIC_BASE_MSR + (msr >> 2)), tmp);
168 }
169 
170 static int
171 get_local_x2apic_pri(void)
172 {
173 	return (rdmsr(REG_X2APIC_BASE_MSR + (APIC_TASK_REG >> 2)));
174 }
175 
176 static void
177 local_x2apic_write_task_reg(uint64_t value)
178 {
179 	X2APIC_WRITE(APIC_TASK_REG, value);
180 }
181 
182 static void
183 local_x2apic_write_int_cmd(uint32_t cpu_id, uint32_t cmd1)
184 {
185 	wrmsr((REG_X2APIC_BASE_MSR + (APIC_INT_CMD1 >> 2)),
186 	    (((uint64_t)cpu_id << 32) | cmd1));
187 }
188 
189 /*ARGSUSED*/
190 void
191 apic_send_EOI(uint32_t irq)
192 {
193 	apic_reg_ops->apic_write(APIC_EOI_REG, 0);
194 }
195 
196 /*
197  * Support for Directed EOI capability is available in both the xAPIC
198  * and x2APIC mode.
199  */
200 void
201 apic_send_directed_EOI(uint32_t irq)
202 {
203 	uchar_t ioapicindex;
204 	uchar_t vector;
205 	apic_irq_t *apic_irq;
206 	short intr_index;
207 
208 	/*
209 	 * Following the EOI to the local APIC unit, perform a directed
210 	 * EOI to the IOxAPIC generating the interrupt by writing to its
211 	 * EOI register.
212 	 *
213 	 * A broadcast EOI is not generated.
214 	 */
215 	apic_reg_ops->apic_write(APIC_EOI_REG, 0);
216 
217 	apic_irq = apic_irq_table[irq];
218 	while (apic_irq) {
219 		intr_index = apic_irq->airq_mps_intr_index;
220 		if (intr_index == ACPI_INDEX || intr_index >= 0) {
221 			ioapicindex = apic_irq->airq_ioapicindex;
222 			vector = apic_irq->airq_vector;
223 			ioapic_write_eoi(ioapicindex, vector);
224 		}
225 		apic_irq = apic_irq->airq_next;
226 	}
227 }
228 
229 int
230 apic_detect_x2apic(void)
231 {
232 	struct cpuid_regs cp;
233 
234 	if (x2apic_enable == 0)
235 		return (0);
236 
237 	cp.cp_eax = 1;
238 	(void) __cpuid_insn(&cp);
239 
240 	return ((cp.cp_ecx & (0x1 << X2APIC_CPUID_BIT)) ? 1 : 0);
241 }
242 
243 void
244 apic_enable_x2apic(void)
245 {
246 	uint64_t apic_base_msr;
247 
248 	if (apic_local_mode() == LOCAL_X2APIC) {
249 		/* BIOS apparently has enabled X2APIC */
250 		if (apic_mode != LOCAL_X2APIC)
251 			x2apic_update_psm();
252 		return;
253 	}
254 
255 	/*
256 	 * This is the first time we are enabling X2APIC on this CPU
257 	 */
258 	apic_base_msr = rdmsr(REG_APIC_BASE_MSR);
259 	apic_base_msr = apic_base_msr | (0x1 << X2APIC_ENABLE_BIT);
260 	wrmsr(REG_APIC_BASE_MSR, apic_base_msr);
261 
262 	if (apic_mode != LOCAL_X2APIC)
263 		x2apic_update_psm();
264 }
265 
266 /*
267  * Determine which mode the current CPU is in. See the table above.
268  * (IA32_APIC_BASE[11])   (IA32_APIC_BASE[10])
269  */
270 int
271 apic_local_mode(void)
272 {
273 	uint64_t apic_base_msr;
274 	int bit = ((0x1 << (X2APIC_ENABLE_BIT + 1)) |
275 	    (0x1 << X2APIC_ENABLE_BIT));
276 
277 	apic_base_msr = rdmsr(REG_APIC_BASE_MSR);
278 
279 	if ((apic_base_msr & bit) == bit)
280 		return (LOCAL_X2APIC);
281 	else
282 		return (LOCAL_APIC);
283 }
284 
285 void
286 apic_change_eoi()
287 {
288 	apic_reg_ops->apic_send_eoi = apic_send_directed_EOI;
289 }
290 
291 /*
292  * Change apic_reg_ops depending upon the apic_mode.
293  */
294 void
295 apic_change_ops()
296 {
297 	if (apic_mode == LOCAL_APIC)
298 		apic_reg_ops = &local_apic_regs_ops;
299 	else if (apic_mode == LOCAL_X2APIC)
300 		apic_reg_ops = &x2apic_regs_ops;
301 }
302 
303 /*
304  * Generates an interprocessor interrupt to another CPU when X2APIC mode is
305  * enabled.
306  */
307 void
308 x2apic_send_ipi(int cpun, int ipl)
309 {
310 	int vector;
311 	ulong_t flag;
312 
313 	/*
314 	 * With X2APIC, Intel relaxed the semantics of the
315 	 * WRMSR instruction such that references to the X2APIC
316 	 * MSR registers are no longer serializing instructions.
317 	 * The code that initiates IPIs assumes that some sort
318 	 * of memory serialization occurs. The old APIC code
319 	 * did a write to uncachable memory mapped registers.
320 	 * Any reference to uncached memory is a serializing
321 	 * operation. To mimic those semantics here, we do an
322 	 * atomic operation, which translates to a LOCK OR instruction,
323 	 * which is serializing.
324 	 */
325 	atomic_or_ulong(&flag, 1);
326 
327 	vector = apic_resv_vector[ipl];
328 
329 	flag = intr_clear();
330 
331 	/*
332 	 * According to X2APIC specification in section '2.3.5.1' of
333 	 * Interrupt Command Register Semantics, the semantics of
334 	 * programming Interrupt Command Register to dispatch an interrupt
335 	 * is simplified. A single MSR write to the 64-bit ICR is required
336 	 * for dispatching an interrupt. Specifically with the 64-bit MSR
337 	 * interface to ICR, system software is not required to check the
338 	 * status of the delivery status bit prior to writing to the ICR
339 	 * to send an IPI. With the removal of the Delivery Status bit,
340 	 * system software no longer has a reason to read the ICR. It remains
341 	 * readable only to aid in debugging.
342 	 */
343 #ifdef	DEBUG
344 	APIC_AV_PENDING_SET();
345 #endif	/* DEBUG */
346 
347 	if ((cpun == psm_get_cpu_id()))
348 		apic_reg_ops->apic_write(X2APIC_SELF_IPI, vector);
349 	else
350 		apic_reg_ops->apic_write_int_cmd(
351 		    apic_cpus[cpun].aci_local_id, vector);
352 
353 	intr_restore(flag);
354 }
355 
356 /*
357  * Generates IPI to another CPU depending on the local APIC mode.
358  * apic_send_ipi() and x2apic_send_ipi() depends on the configured
359  * mode of the local APIC, but that may not match the actual mode
360  * early in CPU startup.
361  *
362  * Any changes made to this routine must be accompanied by similar
363  * changes to apic_send_ipi().
364  */
365 void
366 apic_common_send_ipi(int cpun, int ipl)
367 {
368 	int vector;
369 	ulong_t flag;
370 	int mode = apic_local_mode();
371 
372 	if (mode == LOCAL_X2APIC) {
373 		x2apic_send_ipi(cpun, ipl);
374 		return;
375 	}
376 
377 	ASSERT(mode == LOCAL_APIC);
378 
379 	vector = apic_resv_vector[ipl];
380 	ASSERT((vector >= APIC_BASE_VECT) && (vector <= APIC_SPUR_INTR));
381 	flag = intr_clear();
382 	while (local_apic_regs_ops.apic_read(APIC_INT_CMD1) & AV_PENDING)
383 		apic_ret();
384 	local_apic_regs_ops.apic_write_int_cmd(apic_cpus[cpun].aci_local_id,
385 	    vector);
386 	intr_restore(flag);
387 }
388