xref: /titanic_51/usr/src/uts/i86pc/os/microcode.c (revision 5e53ed34920bc03b619b50f459f2475e7bf80b6f)
12449e17fSsherrym /*
22449e17fSsherrym  * CDDL HEADER START
32449e17fSsherrym  *
42449e17fSsherrym  * The contents of this file are subject to the terms of the
52449e17fSsherrym  * Common Development and Distribution License (the "License").
62449e17fSsherrym  * You may not use this file except in compliance with the License.
72449e17fSsherrym  *
82449e17fSsherrym  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
92449e17fSsherrym  * or http://www.opensolaris.org/os/licensing.
102449e17fSsherrym  * See the License for the specific language governing permissions
112449e17fSsherrym  * and limitations under the License.
122449e17fSsherrym  *
132449e17fSsherrym  * When distributing Covered Code, include this CDDL HEADER in each
142449e17fSsherrym  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
152449e17fSsherrym  * If applicable, add the following below this CDDL HEADER, with the
162449e17fSsherrym  * fields enclosed by brackets "[]" replaced with your own identifying
172449e17fSsherrym  * information: Portions Copyright [yyyy] [name of copyright owner]
182449e17fSsherrym  *
192449e17fSsherrym  * CDDL HEADER END
202449e17fSsherrym  */
212449e17fSsherrym 
222449e17fSsherrym /*
230ba6f73dSMark Johnson  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
242449e17fSsherrym  * Use is subject to license terms.
252449e17fSsherrym  */
262449e17fSsherrym 
272449e17fSsherrym #include <sys/asm_linkage.h>
282449e17fSsherrym #include <sys/bootconf.h>
292449e17fSsherrym #include <sys/cpuvar.h>
302449e17fSsherrym #include <sys/cmn_err.h>
312449e17fSsherrym #include <sys/controlregs.h>
322449e17fSsherrym #include <sys/debug.h>
332449e17fSsherrym #include <sys/kobj.h>
342449e17fSsherrym #include <sys/kobj_impl.h>
352449e17fSsherrym #include <sys/machsystm.h>
36*5e53ed34SHans Rosenfeld #include <sys/ontrap.h>
372449e17fSsherrym #include <sys/param.h>
382449e17fSsherrym #include <sys/machparam.h>
392449e17fSsherrym #include <sys/promif.h>
402449e17fSsherrym #include <sys/sysmacros.h>
412449e17fSsherrym #include <sys/systm.h>
422449e17fSsherrym #include <sys/types.h>
432449e17fSsherrym #include <sys/thread.h>
442449e17fSsherrym #include <sys/ucode.h>
452449e17fSsherrym #include <sys/x86_archext.h>
462449e17fSsherrym #include <sys/x_call.h>
47843e1988Sjohnlev #ifdef	__xpv
48843e1988Sjohnlev #include <sys/hypervisor.h>
49843e1988Sjohnlev #endif
502449e17fSsherrym 
512449e17fSsherrym /*
52adc586deSMark Johnson  * AMD-specific equivalence table
532449e17fSsherrym  */
54adc586deSMark Johnson static ucode_eqtbl_amd_t *ucode_eqtbl_amd;
552449e17fSsherrym 
562449e17fSsherrym /*
572449e17fSsherrym  * mcpu_ucode_info for the boot CPU.  Statically allocated.
582449e17fSsherrym  */
592449e17fSsherrym static struct cpu_ucode_info cpu_ucode_info0;
602449e17fSsherrym 
61adc586deSMark Johnson static ucode_file_t ucodefile;
622449e17fSsherrym 
63adc586deSMark Johnson static void* ucode_zalloc(processorid_t, size_t);
64adc586deSMark Johnson static void ucode_free(processorid_t, void *, size_t);
65adc586deSMark Johnson 
66adc586deSMark Johnson static int ucode_capable_amd(cpu_t *);
67adc586deSMark Johnson static int ucode_capable_intel(cpu_t *);
68adc586deSMark Johnson 
69adc586deSMark Johnson static ucode_errno_t ucode_extract_amd(ucode_update_t *, uint8_t *, int);
70adc586deSMark Johnson static ucode_errno_t ucode_extract_intel(ucode_update_t *, uint8_t *,
71adc586deSMark Johnson     int);
72adc586deSMark Johnson 
73adc586deSMark Johnson static void ucode_file_reset_amd(ucode_file_t *, processorid_t);
74adc586deSMark Johnson static void ucode_file_reset_intel(ucode_file_t *, processorid_t);
75adc586deSMark Johnson 
76adc586deSMark Johnson static uint32_t ucode_load_amd(ucode_file_t *, cpu_ucode_info_t *, cpu_t *);
77adc586deSMark Johnson static uint32_t ucode_load_intel(ucode_file_t *, cpu_ucode_info_t *, cpu_t *);
78adc586deSMark Johnson 
79882a7af5SMark Johnson #ifdef	__xpv
80adc586deSMark Johnson static void ucode_load_xpv(ucode_update_t *);
810ba6f73dSMark Johnson static void ucode_chipset_amd(uint8_t *, int);
82882a7af5SMark Johnson #endif
832449e17fSsherrym 
840ba6f73dSMark Johnson static int ucode_equiv_cpu_amd(cpu_t *, uint16_t *);
85adc586deSMark Johnson 
86adc586deSMark Johnson static ucode_errno_t ucode_locate_amd(cpu_t *, cpu_ucode_info_t *,
87adc586deSMark Johnson     ucode_file_t *);
88adc586deSMark Johnson static ucode_errno_t ucode_locate_intel(cpu_t *, cpu_ucode_info_t *,
89adc586deSMark Johnson     ucode_file_t *);
90adc586deSMark Johnson 
910ba6f73dSMark Johnson #ifndef __xpv
920ba6f73dSMark Johnson static ucode_errno_t ucode_match_amd(uint16_t, cpu_ucode_info_t *,
93adc586deSMark Johnson     ucode_file_amd_t *, int);
940ba6f73dSMark Johnson #endif
95adc586deSMark Johnson static ucode_errno_t ucode_match_intel(int, cpu_ucode_info_t *,
96adc586deSMark Johnson     ucode_header_intel_t *, ucode_ext_table_intel_t *);
97adc586deSMark Johnson 
98adc586deSMark Johnson static void ucode_read_rev_amd(cpu_ucode_info_t *);
99adc586deSMark Johnson static void ucode_read_rev_intel(cpu_ucode_info_t *);
100adc586deSMark Johnson 
101adc586deSMark Johnson static const struct ucode_ops ucode_amd = {
102adc586deSMark Johnson 	MSR_AMD_PATCHLOADER,
103adc586deSMark Johnson 	ucode_capable_amd,
104adc586deSMark Johnson 	ucode_file_reset_amd,
105adc586deSMark Johnson 	ucode_read_rev_amd,
106adc586deSMark Johnson 	ucode_load_amd,
107adc586deSMark Johnson 	ucode_validate_amd,
108adc586deSMark Johnson 	ucode_extract_amd,
109adc586deSMark Johnson 	ucode_locate_amd
110adc586deSMark Johnson };
111adc586deSMark Johnson 
112adc586deSMark Johnson static const struct ucode_ops ucode_intel = {
113adc586deSMark Johnson 	MSR_INTC_UCODE_WRITE,
114adc586deSMark Johnson 	ucode_capable_intel,
115adc586deSMark Johnson 	ucode_file_reset_intel,
116adc586deSMark Johnson 	ucode_read_rev_intel,
117adc586deSMark Johnson 	ucode_load_intel,
118adc586deSMark Johnson 	ucode_validate_intel,
119adc586deSMark Johnson 	ucode_extract_intel,
120adc586deSMark Johnson 	ucode_locate_intel
121adc586deSMark Johnson };
122adc586deSMark Johnson 
123adc586deSMark Johnson const struct ucode_ops *ucode;
124adc586deSMark Johnson 
1252449e17fSsherrym static const char ucode_failure_fmt[] =
12688699bddSsherrym 	"cpu%d: failed to update microcode from version 0x%x to 0x%x\n";
1272449e17fSsherrym static const char ucode_success_fmt[] =
12888699bddSsherrym 	"?cpu%d: microcode has been updated from version 0x%x to 0x%x\n";
1292449e17fSsherrym 
1302449e17fSsherrym /*
1312449e17fSsherrym  * Force flag.  If set, the first microcode binary that matches
1322449e17fSsherrym  * signature and platform id will be used for microcode update,
1332449e17fSsherrym  * regardless of version.  Should only be used for debugging.
1342449e17fSsherrym  */
1352449e17fSsherrym int ucode_force_update = 0;
1362449e17fSsherrym 
1372449e17fSsherrym /*
1382449e17fSsherrym  * Allocate space for mcpu_ucode_info in the machcpu structure
1392449e17fSsherrym  * for all non-boot CPUs.
1402449e17fSsherrym  */
1412449e17fSsherrym void
1422449e17fSsherrym ucode_alloc_space(cpu_t *cp)
1432449e17fSsherrym {
1442449e17fSsherrym 	ASSERT(cp->cpu_id != 0);
145a3114836SGerry Liu 	ASSERT(cp->cpu_m.mcpu_ucode_info == NULL);
1462449e17fSsherrym 	cp->cpu_m.mcpu_ucode_info =
1472449e17fSsherrym 	    kmem_zalloc(sizeof (*cp->cpu_m.mcpu_ucode_info), KM_SLEEP);
1482449e17fSsherrym }
1492449e17fSsherrym 
1502449e17fSsherrym void
1512449e17fSsherrym ucode_free_space(cpu_t *cp)
1522449e17fSsherrym {
153a3114836SGerry Liu 	ASSERT(cp->cpu_m.mcpu_ucode_info != NULL);
154a3114836SGerry Liu 	ASSERT(cp->cpu_m.mcpu_ucode_info != &cpu_ucode_info0);
1552449e17fSsherrym 	kmem_free(cp->cpu_m.mcpu_ucode_info,
1562449e17fSsherrym 	    sizeof (*cp->cpu_m.mcpu_ucode_info));
157a3114836SGerry Liu 	cp->cpu_m.mcpu_ucode_info = NULL;
1582449e17fSsherrym }
1592449e17fSsherrym 
1602449e17fSsherrym /*
1612449e17fSsherrym  * Called when we are done with microcode update on all processors to free up
1622449e17fSsherrym  * space allocated for the microcode file.
1632449e17fSsherrym  */
1642449e17fSsherrym void
165adc586deSMark Johnson ucode_cleanup()
1662449e17fSsherrym {
167126b0a71SMark Johnson 	if (ucode == NULL)
168126b0a71SMark Johnson 		return;
169adc586deSMark Johnson 
170adc586deSMark Johnson 	ucode->file_reset(&ucodefile, -1);
171adc586deSMark Johnson }
172adc586deSMark Johnson 
173adc586deSMark Johnson /*
174adc586deSMark Johnson  * Allocate/free a buffer used to hold ucode data. Space for the boot CPU is
175adc586deSMark Johnson  * allocated with BOP_ALLOC() and does not require a free.
176adc586deSMark Johnson  */
177adc586deSMark Johnson static void*
178adc586deSMark Johnson ucode_zalloc(processorid_t id, size_t size)
179adc586deSMark Johnson {
180adc586deSMark Johnson 	if (id)
181adc586deSMark Johnson 		return (kmem_zalloc(size, KM_NOSLEEP));
182adc586deSMark Johnson 
183adc586deSMark Johnson 	/* BOP_ALLOC() failure results in panic */
184adc586deSMark Johnson 	return (BOP_ALLOC(bootops, NULL, size, MMU_PAGESIZE));
185adc586deSMark Johnson }
186adc586deSMark Johnson 
187adc586deSMark Johnson static void
188adc586deSMark Johnson ucode_free(processorid_t id, void* buf, size_t size)
189adc586deSMark Johnson {
190adc586deSMark Johnson 	if (id)
191adc586deSMark Johnson 		kmem_free(buf, size);
1922449e17fSsherrym }
1932449e17fSsherrym 
1942449e17fSsherrym /*
1952449e17fSsherrym  * Check whether or not a processor is capable of microcode operations
1962449e17fSsherrym  * Returns 1 if it is capable, 0 if not.
197adc586deSMark Johnson  *
198adc586deSMark Johnson  * At this point we only support microcode update for:
199adc586deSMark Johnson  * - Intel processors family 6 and above, and
200adc586deSMark Johnson  * - AMD processors family 0x10 and above.
201adc586deSMark Johnson  *
202adc586deSMark Johnson  * We also assume that we don't support a mix of Intel and
203adc586deSMark Johnson  * AMD processors in the same box.
204adc586deSMark Johnson  *
205adc586deSMark Johnson  * An i86xpv guest domain can't update the microcode.
2062449e17fSsherrym  */
207adc586deSMark Johnson /*ARGSUSED*/
2082449e17fSsherrym static int
209adc586deSMark Johnson ucode_capable_amd(cpu_t *cp)
2102449e17fSsherrym {
211b9bfdccdSStuart Maybee 	int hwenv = get_hwenv();
212b9bfdccdSStuart Maybee 
213b9bfdccdSStuart Maybee 	if (hwenv == HW_XEN_HVM || (hwenv == HW_XEN_PV && !is_controldom())) {
214adc586deSMark Johnson 		return (0);
215adc586deSMark Johnson 	}
2160ba6f73dSMark Johnson 	return (cpuid_getfamily(cp) >= 0x10);
217adc586deSMark Johnson }
2180ba6f73dSMark Johnson 
219adc586deSMark Johnson static int
220adc586deSMark Johnson ucode_capable_intel(cpu_t *cp)
221adc586deSMark Johnson {
222b9bfdccdSStuart Maybee 	int hwenv = get_hwenv();
223b9bfdccdSStuart Maybee 
224b9bfdccdSStuart Maybee 	if (hwenv == HW_XEN_HVM || (hwenv == HW_XEN_PV && !is_controldom())) {
225882a7af5SMark Johnson 		return (0);
226882a7af5SMark Johnson 	}
227adc586deSMark Johnson 	return (cpuid_getfamily(cp) >= 6);
2282449e17fSsherrym }
2292449e17fSsherrym 
2302449e17fSsherrym /*
2312449e17fSsherrym  * Called when it is no longer necessary to keep the microcode around,
2322449e17fSsherrym  * or when the cached microcode doesn't match the CPU being processed.
2332449e17fSsherrym  */
2342449e17fSsherrym static void
235adc586deSMark Johnson ucode_file_reset_amd(ucode_file_t *ufp, processorid_t id)
2362449e17fSsherrym {
237adc586deSMark Johnson 	ucode_file_amd_t *ucodefp = ufp->amd;
2382449e17fSsherrym 
2392449e17fSsherrym 	if (ucodefp == NULL)
2402449e17fSsherrym 		return;
2412449e17fSsherrym 
242adc586deSMark Johnson 	ucode_free(id, ucodefp, sizeof (ucode_file_amd_t));
243adc586deSMark Johnson 	ufp->amd = NULL;
244adc586deSMark Johnson }
245adc586deSMark Johnson 
246adc586deSMark Johnson static void
247adc586deSMark Johnson ucode_file_reset_intel(ucode_file_t *ufp, processorid_t id)
248adc586deSMark Johnson {
249adc586deSMark Johnson 	ucode_file_intel_t *ucodefp = &ufp->intel;
250adc586deSMark Johnson 	int total_size, body_size;
251adc586deSMark Johnson 
252adc586deSMark Johnson 	if (ucodefp == NULL || ucodefp->uf_header == NULL)
253adc586deSMark Johnson 		return;
254adc586deSMark Johnson 
255adc586deSMark Johnson 	total_size = UCODE_TOTAL_SIZE_INTEL(ucodefp->uf_header->uh_total_size);
256adc586deSMark Johnson 	body_size = UCODE_BODY_SIZE_INTEL(ucodefp->uf_header->uh_body_size);
2572449e17fSsherrym 	if (ucodefp->uf_body) {
258adc586deSMark Johnson 		ucode_free(id, ucodefp->uf_body, body_size);
2592449e17fSsherrym 		ucodefp->uf_body = NULL;
2602449e17fSsherrym 	}
2612449e17fSsherrym 
2622449e17fSsherrym 	if (ucodefp->uf_ext_table) {
263adc586deSMark Johnson 		int size = total_size - body_size - UCODE_HEADER_SIZE_INTEL;
264adc586deSMark Johnson 
265adc586deSMark Johnson 		ucode_free(id, ucodefp->uf_ext_table, size);
2662449e17fSsherrym 		ucodefp->uf_ext_table = NULL;
2672449e17fSsherrym 	}
2682449e17fSsherrym 
269adc586deSMark Johnson 	ucode_free(id, ucodefp->uf_header, UCODE_HEADER_SIZE_INTEL);
270adc586deSMark Johnson 	ucodefp->uf_header = NULL;
271adc586deSMark Johnson }
272adc586deSMark Johnson 
273adc586deSMark Johnson /*
274adc586deSMark Johnson  * Find the equivalent CPU id in the equivalence table.
275adc586deSMark Johnson  */
276adc586deSMark Johnson static int
2770ba6f73dSMark Johnson ucode_equiv_cpu_amd(cpu_t *cp, uint16_t *eq_sig)
278adc586deSMark Johnson {
279adc586deSMark Johnson 	char name[MAXPATHLEN];
280adc586deSMark Johnson 	intptr_t fd;
281adc586deSMark Johnson 	int count;
282adc586deSMark Johnson 	int offset = 0, cpi_sig = cpuid_getsig(cp);
283adc586deSMark Johnson 	ucode_eqtbl_amd_t *eqtbl = ucode_eqtbl_amd;
284adc586deSMark Johnson 
285adc586deSMark Johnson 	(void) snprintf(name, MAXPATHLEN, "/%s/%s/equivalence-table",
286adc586deSMark Johnson 	    UCODE_INSTALL_PATH, cpuid_getvendorstr(cp));
287adc586deSMark Johnson 
288adc586deSMark Johnson 	/*
289adc586deSMark Johnson 	 * No kmem_zalloc() etc. available on boot cpu.
290adc586deSMark Johnson 	 */
291adc586deSMark Johnson 	if (cp->cpu_id == 0) {
292adc586deSMark Johnson 		if ((fd = kobj_open(name)) == -1)
293adc586deSMark Johnson 			return (EM_OPENFILE);
294adc586deSMark Johnson 		/* ucode_zalloc() cannot fail on boot cpu */
295adc586deSMark Johnson 		eqtbl = ucode_zalloc(cp->cpu_id, sizeof (*eqtbl));
296adc586deSMark Johnson 		ASSERT(eqtbl);
297adc586deSMark Johnson 		do {
298adc586deSMark Johnson 			count = kobj_read(fd, (int8_t *)eqtbl,
299adc586deSMark Johnson 			    sizeof (*eqtbl), offset);
300adc586deSMark Johnson 			if (count != sizeof (*eqtbl)) {
301adc586deSMark Johnson 				(void) kobj_close(fd);
302adc586deSMark Johnson 				return (EM_HIGHERREV);
303adc586deSMark Johnson 			}
304adc586deSMark Johnson 			offset += count;
305adc586deSMark Johnson 		} while (eqtbl->ue_inst_cpu && eqtbl->ue_inst_cpu != cpi_sig);
306adc586deSMark Johnson 		(void) kobj_close(fd);
307adc586deSMark Johnson 	}
308adc586deSMark Johnson 
309adc586deSMark Johnson 	/*
310adc586deSMark Johnson 	 * If not already done, load the equivalence table.
311adc586deSMark Johnson 	 * Not done on boot CPU.
312adc586deSMark Johnson 	 */
313adc586deSMark Johnson 	if (eqtbl == NULL) {
314adc586deSMark Johnson 		struct _buf *eq;
315adc586deSMark Johnson 		uint64_t size;
316adc586deSMark Johnson 
317adc586deSMark Johnson 		if ((eq = kobj_open_file(name)) == (struct _buf *)-1)
318adc586deSMark Johnson 			return (EM_OPENFILE);
319adc586deSMark Johnson 
320adc586deSMark Johnson 		if (kobj_get_filesize(eq, &size) < 0) {
321adc586deSMark Johnson 			kobj_close_file(eq);
322adc586deSMark Johnson 			return (EM_OPENFILE);
323adc586deSMark Johnson 		}
324adc586deSMark Johnson 
325adc586deSMark Johnson 		ucode_eqtbl_amd = kmem_zalloc(size, KM_NOSLEEP);
326adc586deSMark Johnson 		if (ucode_eqtbl_amd == NULL) {
327adc586deSMark Johnson 			kobj_close_file(eq);
328adc586deSMark Johnson 			return (EM_NOMEM);
329adc586deSMark Johnson 		}
330adc586deSMark Johnson 
331adc586deSMark Johnson 		count = kobj_read_file(eq, (char *)ucode_eqtbl_amd, size, 0);
332adc586deSMark Johnson 		kobj_close_file(eq);
333adc586deSMark Johnson 
334adc586deSMark Johnson 		if (count != size)
335adc586deSMark Johnson 			return (EM_FILESIZE);
336adc586deSMark Johnson 	}
337adc586deSMark Johnson 
338adc586deSMark Johnson 	/* Get the equivalent CPU id. */
339adc586deSMark Johnson 	if (cp->cpu_id)
340adc586deSMark Johnson 		for (eqtbl = ucode_eqtbl_amd;
341adc586deSMark Johnson 		    eqtbl->ue_inst_cpu && eqtbl->ue_inst_cpu != cpi_sig;
342adc586deSMark Johnson 		    eqtbl++)
343adc586deSMark Johnson 			;
344adc586deSMark Johnson 
345adc586deSMark Johnson 	*eq_sig = eqtbl->ue_equiv_cpu;
346adc586deSMark Johnson 
347adc586deSMark Johnson 	/* No equivalent CPU id found, assume outdated microcode file. */
348adc586deSMark Johnson 	if (*eq_sig == 0)
349adc586deSMark Johnson 		return (EM_HIGHERREV);
350adc586deSMark Johnson 
351adc586deSMark Johnson 	return (EM_OK);
3522449e17fSsherrym }
3532449e17fSsherrym 
3542449e17fSsherrym /*
3550ba6f73dSMark Johnson  * xVM cannot check for the presence of PCI devices. Look for chipset-
3560ba6f73dSMark Johnson  * specific microcode patches in the container file and disable them
3570ba6f73dSMark Johnson  * by setting their CPU revision to an invalid value.
3580ba6f73dSMark Johnson  */
3590ba6f73dSMark Johnson #ifdef __xpv
3600ba6f73dSMark Johnson static void
3610ba6f73dSMark Johnson ucode_chipset_amd(uint8_t *buf, int size)
3620ba6f73dSMark Johnson {
3630ba6f73dSMark Johnson 	ucode_header_amd_t *uh;
3640ba6f73dSMark Johnson 	uint32_t *ptr = (uint32_t *)buf;
3650ba6f73dSMark Johnson 	int len = 0;
3660ba6f73dSMark Johnson 
3670ba6f73dSMark Johnson 	/* skip to first microcode patch */
3680ba6f73dSMark Johnson 	ptr += 2; len = *ptr++; ptr += len >> 2; size -= len;
3690ba6f73dSMark Johnson 
3700ba6f73dSMark Johnson 	while (size >= sizeof (ucode_header_amd_t) + 8) {
3710ba6f73dSMark Johnson 		ptr++; len = *ptr++;
3720ba6f73dSMark Johnson 		uh = (ucode_header_amd_t *)ptr;
3730ba6f73dSMark Johnson 		ptr += len >> 2; size -= len;
3740ba6f73dSMark Johnson 
3750ba6f73dSMark Johnson 		if (uh->uh_nb_id) {
3760ba6f73dSMark Johnson 			cmn_err(CE_WARN, "ignoring northbridge-specific ucode: "
3770ba6f73dSMark Johnson 			    "chipset id %x, revision %x",
3780ba6f73dSMark Johnson 			    uh->uh_nb_id, uh->uh_nb_rev);
3790ba6f73dSMark Johnson 			uh->uh_cpu_rev = 0xffff;
3800ba6f73dSMark Johnson 		}
3810ba6f73dSMark Johnson 
3820ba6f73dSMark Johnson 		if (uh->uh_sb_id) {
3830ba6f73dSMark Johnson 			cmn_err(CE_WARN, "ignoring southbridge-specific ucode: "
3840ba6f73dSMark Johnson 			    "chipset id %x, revision %x",
3850ba6f73dSMark Johnson 			    uh->uh_sb_id, uh->uh_sb_rev);
3860ba6f73dSMark Johnson 			uh->uh_cpu_rev = 0xffff;
3870ba6f73dSMark Johnson 		}
3880ba6f73dSMark Johnson 	}
3890ba6f73dSMark Johnson }
3900ba6f73dSMark Johnson #endif
3910ba6f73dSMark Johnson 
3920ba6f73dSMark Johnson /*
3932449e17fSsherrym  * Populate the ucode file structure from microcode file corresponding to
3942449e17fSsherrym  * this CPU, if exists.
3952449e17fSsherrym  *
3962449e17fSsherrym  * Return EM_OK on success, corresponding error code on failure.
3972449e17fSsherrym  */
3980ba6f73dSMark Johnson /*ARGSUSED*/
3992449e17fSsherrym static ucode_errno_t
400adc586deSMark Johnson ucode_locate_amd(cpu_t *cp, cpu_ucode_info_t *uinfop, ucode_file_t *ufp)
401adc586deSMark Johnson {
402adc586deSMark Johnson 	char name[MAXPATHLEN];
403adc586deSMark Johnson 	intptr_t fd;
4040ba6f73dSMark Johnson 	int count, rc;
405adc586deSMark Johnson 	ucode_file_amd_t *ucodefp = ufp->amd;
406adc586deSMark Johnson 
4070ba6f73dSMark Johnson #ifndef __xpv
4080ba6f73dSMark Johnson 	uint16_t eq_sig = 0;
4090ba6f73dSMark Johnson 	int i;
4100ba6f73dSMark Johnson 
411adc586deSMark Johnson 	/* get equivalent CPU id */
412adc586deSMark Johnson 	if ((rc = ucode_equiv_cpu_amd(cp, &eq_sig)) != EM_OK)
413adc586deSMark Johnson 		return (rc);
414adc586deSMark Johnson 
415adc586deSMark Johnson 	/*
416adc586deSMark Johnson 	 * Allocate a buffer for the microcode patch. If the buffer has been
417adc586deSMark Johnson 	 * allocated before, check for a matching microcode to avoid loading
418adc586deSMark Johnson 	 * the file again.
419adc586deSMark Johnson 	 */
420adc586deSMark Johnson 	if (ucodefp == NULL)
421adc586deSMark Johnson 		ucodefp = ucode_zalloc(cp->cpu_id, sizeof (*ucodefp));
422adc586deSMark Johnson 	else if (ucode_match_amd(eq_sig, uinfop, ucodefp, sizeof (*ucodefp))
423adc586deSMark Johnson 	    == EM_OK)
424adc586deSMark Johnson 		return (EM_OK);
425adc586deSMark Johnson 
426adc586deSMark Johnson 	if (ucodefp == NULL)
427adc586deSMark Johnson 		return (EM_NOMEM);
428adc586deSMark Johnson 
429adc586deSMark Johnson 	ufp->amd = ucodefp;
430adc586deSMark Johnson 
431adc586deSMark Johnson 	/*
432adc586deSMark Johnson 	 * Find the patch for this CPU. The patch files are named XXXX-YY, where
433adc586deSMark Johnson 	 * XXXX is the equivalent CPU id and YY is the running patch number.
434adc586deSMark Johnson 	 * Patches specific to certain chipsets are guaranteed to have lower
435adc586deSMark Johnson 	 * numbers than less specific patches, so we can just load the first
436adc586deSMark Johnson 	 * patch that matches.
437adc586deSMark Johnson 	 */
438adc586deSMark Johnson 
439adc586deSMark Johnson 	for (i = 0; i < 0xff; i++) {
440adc586deSMark Johnson 		(void) snprintf(name, MAXPATHLEN, "/%s/%s/%04X-%02X",
441adc586deSMark Johnson 		    UCODE_INSTALL_PATH, cpuid_getvendorstr(cp), eq_sig, i);
442adc586deSMark Johnson 		if ((fd = kobj_open(name)) == -1)
443adc586deSMark Johnson 			return (EM_NOMATCH);
444adc586deSMark Johnson 		count = kobj_read(fd, (char *)ucodefp, sizeof (*ucodefp), 0);
445adc586deSMark Johnson 		(void) kobj_close(fd);
446adc586deSMark Johnson 
447adc586deSMark Johnson 		if (ucode_match_amd(eq_sig, uinfop, ucodefp, count) == EM_OK)
448adc586deSMark Johnson 			return (EM_OK);
449adc586deSMark Johnson 	}
450adc586deSMark Johnson 	return (EM_NOMATCH);
4510ba6f73dSMark Johnson #else
4520ba6f73dSMark Johnson 	int size = 0;
4530ba6f73dSMark Johnson 	char c;
4540ba6f73dSMark Johnson 
4550ba6f73dSMark Johnson 	/*
4560ba6f73dSMark Johnson 	 * The xVM case is special. To support mixed-revision systems, the
4570ba6f73dSMark Johnson 	 * hypervisor will choose which patch to load for which CPU, so the
4580ba6f73dSMark Johnson 	 * whole microcode patch container file will have to be loaded.
4590ba6f73dSMark Johnson 	 *
4600ba6f73dSMark Johnson 	 * Since this code is only run on the boot cpu, we don't have to care
4610ba6f73dSMark Johnson 	 * about failing ucode_zalloc() or freeing allocated memory.
4620ba6f73dSMark Johnson 	 */
4630ba6f73dSMark Johnson 	if (cp->cpu_id != 0)
4640ba6f73dSMark Johnson 		return (EM_INVALIDARG);
4650ba6f73dSMark Johnson 
4660ba6f73dSMark Johnson 	(void) snprintf(name, MAXPATHLEN, "/%s/%s/container",
4670ba6f73dSMark Johnson 	    UCODE_INSTALL_PATH, cpuid_getvendorstr(cp));
4680ba6f73dSMark Johnson 
4690ba6f73dSMark Johnson 	if ((fd = kobj_open(name)) == -1)
4700ba6f73dSMark Johnson 		return (EM_OPENFILE);
4710ba6f73dSMark Johnson 
4720ba6f73dSMark Johnson 	/* get the file size by counting bytes */
4730ba6f73dSMark Johnson 	do {
4740ba6f73dSMark Johnson 		count = kobj_read(fd, &c, 1, size);
4750ba6f73dSMark Johnson 		size += count;
4760ba6f73dSMark Johnson 	} while (count);
4770ba6f73dSMark Johnson 
4780ba6f73dSMark Johnson 	ucodefp = ucode_zalloc(cp->cpu_id, sizeof (*ucodefp));
4790ba6f73dSMark Johnson 	ASSERT(ucodefp);
4800ba6f73dSMark Johnson 	ufp->amd = ucodefp;
4810ba6f73dSMark Johnson 
4820ba6f73dSMark Johnson 	ucodefp->usize = size;
4830ba6f73dSMark Johnson 	ucodefp->ucodep = ucode_zalloc(cp->cpu_id, size);
4840ba6f73dSMark Johnson 	ASSERT(ucodefp->ucodep);
4850ba6f73dSMark Johnson 
4860ba6f73dSMark Johnson 	/* load the microcode patch container file */
4870ba6f73dSMark Johnson 	count = kobj_read(fd, (char *)ucodefp->ucodep, size, 0);
4880ba6f73dSMark Johnson 	(void) kobj_close(fd);
4890ba6f73dSMark Johnson 
4900ba6f73dSMark Johnson 	if (count != size)
4910ba6f73dSMark Johnson 		return (EM_FILESIZE);
4920ba6f73dSMark Johnson 
4930ba6f73dSMark Johnson 	/* make sure the container file is valid */
4940ba6f73dSMark Johnson 	rc = ucode->validate(ucodefp->ucodep, ucodefp->usize);
4950ba6f73dSMark Johnson 
4960ba6f73dSMark Johnson 	if (rc != EM_OK)
4970ba6f73dSMark Johnson 		return (rc);
4980ba6f73dSMark Johnson 
4990ba6f73dSMark Johnson 	/* disable chipset-specific patches */
5000ba6f73dSMark Johnson 	ucode_chipset_amd(ucodefp->ucodep, ucodefp->usize);
5010ba6f73dSMark Johnson 
5020ba6f73dSMark Johnson 	return (EM_OK);
5030ba6f73dSMark Johnson #endif
504adc586deSMark Johnson }
505adc586deSMark Johnson 
506adc586deSMark Johnson static ucode_errno_t
507adc586deSMark Johnson ucode_locate_intel(cpu_t *cp, cpu_ucode_info_t *uinfop, ucode_file_t *ufp)
5082449e17fSsherrym {
5092449e17fSsherrym 	char		name[MAXPATHLEN];
5102449e17fSsherrym 	intptr_t	fd;
5112449e17fSsherrym 	int		count;
512adc586deSMark Johnson 	int		header_size = UCODE_HEADER_SIZE_INTEL;
5132449e17fSsherrym 	int		cpi_sig = cpuid_getsig(cp);
5142449e17fSsherrym 	ucode_errno_t	rc = EM_OK;
515adc586deSMark Johnson 	ucode_file_intel_t *ucodefp = &ufp->intel;
516adc586deSMark Johnson 
517adc586deSMark Johnson 	ASSERT(ucode);
5182449e17fSsherrym 
5192449e17fSsherrym 	/*
5202449e17fSsherrym 	 * If the microcode matches the CPU we are processing, use it.
5212449e17fSsherrym 	 */
522adc586deSMark Johnson 	if (ucode_match_intel(cpi_sig, uinfop, ucodefp->uf_header,
5232449e17fSsherrym 	    ucodefp->uf_ext_table) == EM_OK && ucodefp->uf_body != NULL) {
5242449e17fSsherrym 		return (EM_OK);
5252449e17fSsherrym 	}
5262449e17fSsherrym 
5272449e17fSsherrym 	/*
5282449e17fSsherrym 	 * Look for microcode file with the right name.
5292449e17fSsherrym 	 */
5302449e17fSsherrym 	(void) snprintf(name, MAXPATHLEN, "/%s/%s/%08X-%02X",
5312449e17fSsherrym 	    UCODE_INSTALL_PATH, cpuid_getvendorstr(cp), cpi_sig,
5322449e17fSsherrym 	    uinfop->cui_platid);
5332449e17fSsherrym 	if ((fd = kobj_open(name)) == -1) {
5342449e17fSsherrym 		return (EM_OPENFILE);
5352449e17fSsherrym 	}
5362449e17fSsherrym 
5372449e17fSsherrym 	/*
5382449e17fSsherrym 	 * We found a microcode file for the CPU we are processing,
5392449e17fSsherrym 	 * reset the microcode data structure and read in the new
5402449e17fSsherrym 	 * file.
5412449e17fSsherrym 	 */
542adc586deSMark Johnson 	ucode->file_reset(ufp, cp->cpu_id);
5432449e17fSsherrym 
544adc586deSMark Johnson 	ucodefp->uf_header = ucode_zalloc(cp->cpu_id, header_size);
545adc586deSMark Johnson 	if (ucodefp->uf_header == NULL)
546adc586deSMark Johnson 		return (EM_NOMEM);
547adc586deSMark Johnson 
548adc586deSMark Johnson 	count = kobj_read(fd, (char *)ucodefp->uf_header, header_size, 0);
5492449e17fSsherrym 
5502449e17fSsherrym 	switch (count) {
551adc586deSMark Johnson 	case UCODE_HEADER_SIZE_INTEL: {
5522449e17fSsherrym 
553adc586deSMark Johnson 		ucode_header_intel_t	*uhp = ucodefp->uf_header;
5542449e17fSsherrym 		uint32_t	offset = header_size;
5552449e17fSsherrym 		int		total_size, body_size, ext_size;
5562449e17fSsherrym 		uint32_t	sum = 0;
5572449e17fSsherrym 
5582449e17fSsherrym 		/*
5592449e17fSsherrym 		 * Make sure that the header contains valid fields.
5602449e17fSsherrym 		 */
561adc586deSMark Johnson 		if ((rc = ucode_header_validate_intel(uhp)) == EM_OK) {
562adc586deSMark Johnson 			total_size = UCODE_TOTAL_SIZE_INTEL(uhp->uh_total_size);
563adc586deSMark Johnson 			body_size = UCODE_BODY_SIZE_INTEL(uhp->uh_body_size);
564adc586deSMark Johnson 			ucodefp->uf_body = ucode_zalloc(cp->cpu_id, body_size);
565adc586deSMark Johnson 			if (ucodefp->uf_body == NULL) {
5662449e17fSsherrym 				rc = EM_NOMEM;
5672449e17fSsherrym 				break;
5682449e17fSsherrym 			}
5692449e17fSsherrym 
5702449e17fSsherrym 			if (kobj_read(fd, (char *)ucodefp->uf_body,
5712449e17fSsherrym 			    body_size, offset) != body_size)
5722449e17fSsherrym 				rc = EM_FILESIZE;
5732449e17fSsherrym 		}
5742449e17fSsherrym 
5752449e17fSsherrym 		if (rc)
5762449e17fSsherrym 			break;
5772449e17fSsherrym 
578adc586deSMark Johnson 		sum = ucode_checksum_intel(0, header_size,
579adc586deSMark Johnson 		    (uint8_t *)ucodefp->uf_header);
580adc586deSMark Johnson 		if (ucode_checksum_intel(sum, body_size, ucodefp->uf_body)) {
5812449e17fSsherrym 			rc = EM_CHECKSUM;
5822449e17fSsherrym 			break;
5832449e17fSsherrym 		}
5842449e17fSsherrym 
5852449e17fSsherrym 		/*
5862449e17fSsherrym 		 * Check to see if there is extended signature table.
5872449e17fSsherrym 		 */
5882449e17fSsherrym 		offset = body_size + header_size;
5892449e17fSsherrym 		ext_size = total_size - offset;
5902449e17fSsherrym 
5912449e17fSsherrym 		if (ext_size <= 0)
5922449e17fSsherrym 			break;
5932449e17fSsherrym 
594adc586deSMark Johnson 		ucodefp->uf_ext_table = ucode_zalloc(cp->cpu_id, ext_size);
595adc586deSMark Johnson 		if (ucodefp->uf_ext_table == NULL) {
5962449e17fSsherrym 			rc = EM_NOMEM;
5972449e17fSsherrym 			break;
5982449e17fSsherrym 		}
5992449e17fSsherrym 
6002449e17fSsherrym 		if (kobj_read(fd, (char *)ucodefp->uf_ext_table,
6012449e17fSsherrym 		    ext_size, offset) != ext_size) {
6022449e17fSsherrym 			rc = EM_FILESIZE;
603adc586deSMark Johnson 		} else if (ucode_checksum_intel(0, ext_size,
6042449e17fSsherrym 		    (uint8_t *)(ucodefp->uf_ext_table))) {
6052449e17fSsherrym 			rc = EM_CHECKSUM;
6062449e17fSsherrym 		} else {
6072449e17fSsherrym 			int i;
6082449e17fSsherrym 
609adc586deSMark Johnson 			ext_size -= UCODE_EXT_TABLE_SIZE_INTEL;
6102449e17fSsherrym 			for (i = 0; i < ucodefp->uf_ext_table->uet_count;
6112449e17fSsherrym 			    i++) {
612adc586deSMark Johnson 				if (ucode_checksum_intel(0,
613adc586deSMark Johnson 				    UCODE_EXT_SIG_SIZE_INTEL,
6142449e17fSsherrym 				    (uint8_t *)(&(ucodefp->uf_ext_table->
6152449e17fSsherrym 				    uet_ext_sig[i])))) {
6162449e17fSsherrym 					rc = EM_CHECKSUM;
6172449e17fSsherrym 					break;
6182449e17fSsherrym 				}
6192449e17fSsherrym 			}
6202449e17fSsherrym 		}
6212449e17fSsherrym 		break;
6222449e17fSsherrym 	}
6232449e17fSsherrym 
6242449e17fSsherrym 	default:
6252449e17fSsherrym 		rc = EM_FILESIZE;
6262449e17fSsherrym 		break;
6272449e17fSsherrym 	}
6282449e17fSsherrym 
6292449e17fSsherrym 	kobj_close(fd);
6302449e17fSsherrym 
6312449e17fSsherrym 	if (rc != EM_OK)
6322449e17fSsherrym 		return (rc);
6332449e17fSsherrym 
634adc586deSMark Johnson 	rc = ucode_match_intel(cpi_sig, uinfop, ucodefp->uf_header,
6352449e17fSsherrym 	    ucodefp->uf_ext_table);
6362449e17fSsherrym 
6372449e17fSsherrym 	return (rc);
6382449e17fSsherrym }
6392449e17fSsherrym 
6400ba6f73dSMark Johnson #ifndef __xpv
641adc586deSMark Johnson static ucode_errno_t
6420ba6f73dSMark Johnson ucode_match_amd(uint16_t eq_sig, cpu_ucode_info_t *uinfop,
6430ba6f73dSMark Johnson     ucode_file_amd_t *ucodefp, int size)
644adc586deSMark Johnson {
645adc586deSMark Johnson 	ucode_header_amd_t *uh;
646adc586deSMark Johnson 
647adc586deSMark Johnson 	if (ucodefp == NULL || size < sizeof (ucode_header_amd_t))
648adc586deSMark Johnson 		return (EM_NOMATCH);
649adc586deSMark Johnson 
650*5e53ed34SHans Rosenfeld 	uh = &ucodefp->uf_header;
651*5e53ed34SHans Rosenfeld 
652adc586deSMark Johnson 	/*
653adc586deSMark Johnson 	 * Don't even think about loading patches that would require code
654*5e53ed34SHans Rosenfeld 	 * execution. Does not apply to patches for family 0x14 and beyond.
655adc586deSMark Johnson 	 */
656*5e53ed34SHans Rosenfeld 	if (uh->uh_cpu_rev < 0x5000 &&
657*5e53ed34SHans Rosenfeld 	    size > offsetof(ucode_file_amd_t, uf_code_present) &&
658adc586deSMark Johnson 	    ucodefp->uf_code_present)
659adc586deSMark Johnson 		return (EM_NOMATCH);
660adc586deSMark Johnson 
661adc586deSMark Johnson 	if (eq_sig != uh->uh_cpu_rev)
662adc586deSMark Johnson 		return (EM_NOMATCH);
663adc586deSMark Johnson 
664adc586deSMark Johnson 	if (uh->uh_nb_id) {
665adc586deSMark Johnson 		cmn_err(CE_WARN, "ignoring northbridge-specific ucode: "
666adc586deSMark Johnson 		    "chipset id %x, revision %x", uh->uh_nb_id, uh->uh_nb_rev);
667adc586deSMark Johnson 		return (EM_NOMATCH);
668adc586deSMark Johnson 	}
669adc586deSMark Johnson 
670adc586deSMark Johnson 	if (uh->uh_sb_id) {
671adc586deSMark Johnson 		cmn_err(CE_WARN, "ignoring southbridge-specific ucode: "
672adc586deSMark Johnson 		    "chipset id %x, revision %x", uh->uh_sb_id, uh->uh_sb_rev);
673adc586deSMark Johnson 		return (EM_NOMATCH);
674adc586deSMark Johnson 	}
675adc586deSMark Johnson 
676*5e53ed34SHans Rosenfeld 	if (uh->uh_patch_id <= uinfop->cui_rev && !ucode_force_update)
677adc586deSMark Johnson 		return (EM_HIGHERREV);
678adc586deSMark Johnson 
679adc586deSMark Johnson 	return (EM_OK);
680adc586deSMark Johnson }
6810ba6f73dSMark Johnson #endif
6822449e17fSsherrym 
6832449e17fSsherrym /*
6842449e17fSsherrym  * Returns 1 if the microcode is for this processor; 0 otherwise.
6852449e17fSsherrym  */
6862449e17fSsherrym static ucode_errno_t
687adc586deSMark Johnson ucode_match_intel(int cpi_sig, cpu_ucode_info_t *uinfop,
688adc586deSMark Johnson     ucode_header_intel_t *uhp, ucode_ext_table_intel_t *uetp)
6892449e17fSsherrym {
690adc586deSMark Johnson 	if (uhp == NULL)
691adc586deSMark Johnson 		return (EM_NOMATCH);
6922449e17fSsherrym 
693adc586deSMark Johnson 	if (UCODE_MATCH_INTEL(cpi_sig, uhp->uh_signature,
6942449e17fSsherrym 	    uinfop->cui_platid, uhp->uh_proc_flags)) {
6952449e17fSsherrym 
6962449e17fSsherrym 		if (uinfop->cui_rev >= uhp->uh_rev && !ucode_force_update)
6972449e17fSsherrym 			return (EM_HIGHERREV);
6982449e17fSsherrym 
6992449e17fSsherrym 		return (EM_OK);
7002449e17fSsherrym 	}
7012449e17fSsherrym 
7022449e17fSsherrym 	if (uetp != NULL) {
7032449e17fSsherrym 		int i;
7042449e17fSsherrym 
7052449e17fSsherrym 		for (i = 0; i < uetp->uet_count; i++) {
706adc586deSMark Johnson 			ucode_ext_sig_intel_t *uesp;
7072449e17fSsherrym 
7082449e17fSsherrym 			uesp = &uetp->uet_ext_sig[i];
7092449e17fSsherrym 
710adc586deSMark Johnson 			if (UCODE_MATCH_INTEL(cpi_sig, uesp->ues_signature,
7112449e17fSsherrym 			    uinfop->cui_platid, uesp->ues_proc_flags)) {
7122449e17fSsherrym 
7132449e17fSsherrym 				if (uinfop->cui_rev >= uhp->uh_rev &&
7142449e17fSsherrym 				    !ucode_force_update)
7152449e17fSsherrym 					return (EM_HIGHERREV);
7162449e17fSsherrym 
7172449e17fSsherrym 				return (EM_OK);
7182449e17fSsherrym 			}
7192449e17fSsherrym 		}
7202449e17fSsherrym 	}
7212449e17fSsherrym 
7222449e17fSsherrym 	return (EM_NOMATCH);
7232449e17fSsherrym }
7242449e17fSsherrym 
7252449e17fSsherrym /*ARGSUSED*/
7262449e17fSsherrym static int
7272449e17fSsherrym ucode_write(xc_arg_t arg1, xc_arg_t unused2, xc_arg_t unused3)
7282449e17fSsherrym {
729adc586deSMark Johnson 	ucode_update_t *uusp = (ucode_update_t *)arg1;
730adc586deSMark Johnson 	cpu_ucode_info_t *uinfop = CPU->cpu_m.mcpu_ucode_info;
731*5e53ed34SHans Rosenfeld #ifndef __xpv
732*5e53ed34SHans Rosenfeld 	on_trap_data_t otd;
733*5e53ed34SHans Rosenfeld #endif
7342449e17fSsherrym 
735adc586deSMark Johnson 	ASSERT(ucode);
7362449e17fSsherrym 	ASSERT(uusp->ucodep);
7372449e17fSsherrym 
738882a7af5SMark Johnson #ifndef	__xpv
7392449e17fSsherrym 	/*
7402449e17fSsherrym 	 * Check one more time to see if it is really necessary to update
7412449e17fSsherrym 	 * microcode just in case this is a hyperthreaded processor where
7422449e17fSsherrym 	 * the threads share the same microcode.
7432449e17fSsherrym 	 */
7442449e17fSsherrym 	if (!ucode_force_update) {
745adc586deSMark Johnson 		ucode->read_rev(uinfop);
7462449e17fSsherrym 		uusp->new_rev = uinfop->cui_rev;
7472449e17fSsherrym 		if (uinfop->cui_rev >= uusp->expected_rev)
7482449e17fSsherrym 			return (0);
7492449e17fSsherrym 	}
7502449e17fSsherrym 
751*5e53ed34SHans Rosenfeld 	if (!on_trap(&otd, OT_DATA_ACCESS))
752adc586deSMark Johnson 		wrmsr(ucode->write_msr, (uintptr_t)uusp->ucodep);
753*5e53ed34SHans Rosenfeld 
754*5e53ed34SHans Rosenfeld 	no_trap();
755882a7af5SMark Johnson #endif
756adc586deSMark Johnson 	ucode->read_rev(uinfop);
7572449e17fSsherrym 	uusp->new_rev = uinfop->cui_rev;
7582449e17fSsherrym 
7592449e17fSsherrym 	return (0);
7602449e17fSsherrym }
7612449e17fSsherrym 
762adc586deSMark Johnson /*ARGSUSED*/
763adc586deSMark Johnson static uint32_t
764adc586deSMark Johnson ucode_load_amd(ucode_file_t *ufp, cpu_ucode_info_t *uinfop, cpu_t *cp)
7652449e17fSsherrym {
766adc586deSMark Johnson 	ucode_file_amd_t *ucodefp = ufp->amd;
767adc586deSMark Johnson #ifdef	__xpv
768adc586deSMark Johnson 	ucode_update_t uus;
769*5e53ed34SHans Rosenfeld #else
770*5e53ed34SHans Rosenfeld 	on_trap_data_t otd;
771adc586deSMark Johnson #endif
772adc586deSMark Johnson 
773adc586deSMark Johnson 	ASSERT(ucode);
774adc586deSMark Johnson 	ASSERT(ucodefp);
775adc586deSMark Johnson 
776adc586deSMark Johnson #ifndef	__xpv
7772449e17fSsherrym 	kpreempt_disable();
778*5e53ed34SHans Rosenfeld 	if (on_trap(&otd, OT_DATA_ACCESS)) {
779*5e53ed34SHans Rosenfeld 		no_trap();
780*5e53ed34SHans Rosenfeld 		kpreempt_enable();
781*5e53ed34SHans Rosenfeld 		return (0);
782*5e53ed34SHans Rosenfeld 	}
783adc586deSMark Johnson 	wrmsr(ucode->write_msr, (uintptr_t)ucodefp);
784*5e53ed34SHans Rosenfeld 	no_trap();
785adc586deSMark Johnson 	ucode->read_rev(uinfop);
7862449e17fSsherrym 	kpreempt_enable();
7870ba6f73dSMark Johnson 
7880ba6f73dSMark Johnson 	return (ucodefp->uf_header.uh_patch_id);
789adc586deSMark Johnson #else
7900ba6f73dSMark Johnson 	uus.ucodep = ucodefp->ucodep;
7910ba6f73dSMark Johnson 	uus.usize = ucodefp->usize;
792adc586deSMark Johnson 	ucode_load_xpv(&uus);
793adc586deSMark Johnson 	ucode->read_rev(uinfop);
794adc586deSMark Johnson 	uus.new_rev = uinfop->cui_rev;
795adc586deSMark Johnson 
7960ba6f73dSMark Johnson 	return (uus.new_rev);
7970ba6f73dSMark Johnson #endif
798adc586deSMark Johnson }
799adc586deSMark Johnson 
800adc586deSMark Johnson /*ARGSUSED2*/
801adc586deSMark Johnson static uint32_t
802adc586deSMark Johnson ucode_load_intel(ucode_file_t *ufp, cpu_ucode_info_t *uinfop, cpu_t *cp)
803adc586deSMark Johnson {
804adc586deSMark Johnson 	ucode_file_intel_t *ucodefp = &ufp->intel;
805adc586deSMark Johnson #ifdef __xpv
806adc586deSMark Johnson 	uint32_t ext_offset;
807adc586deSMark Johnson 	uint32_t body_size;
808adc586deSMark Johnson 	uint32_t ext_size;
809adc586deSMark Johnson 	uint8_t *ustart;
810adc586deSMark Johnson 	uint32_t usize;
811adc586deSMark Johnson 	ucode_update_t uus;
812adc586deSMark Johnson #endif
813adc586deSMark Johnson 
814adc586deSMark Johnson 	ASSERT(ucode);
815adc586deSMark Johnson 
816adc586deSMark Johnson #ifdef __xpv
817adc586deSMark Johnson 	/*
818adc586deSMark Johnson 	 * the hypervisor wants the header, data, and extended
819adc586deSMark Johnson 	 * signature tables. We can only get here from the boot
820adc586deSMark Johnson 	 * CPU (cpu #0), we don't need to free as ucode_zalloc() will
821adc586deSMark Johnson 	 * use BOP_ALLOC().
822adc586deSMark Johnson 	 */
823adc586deSMark Johnson 	usize = UCODE_TOTAL_SIZE_INTEL(ucodefp->uf_header->uh_total_size);
824adc586deSMark Johnson 	ustart = ucode_zalloc(cp->cpu_id, usize);
825adc586deSMark Johnson 	ASSERT(ustart);
826adc586deSMark Johnson 
827adc586deSMark Johnson 	body_size = UCODE_BODY_SIZE_INTEL(ucodefp->uf_header->uh_body_size);
828adc586deSMark Johnson 	ext_offset = body_size + UCODE_HEADER_SIZE_INTEL;
829adc586deSMark Johnson 	ext_size = usize - ext_offset;
830adc586deSMark Johnson 	ASSERT(ext_size >= 0);
831adc586deSMark Johnson 
832adc586deSMark Johnson 	(void) memcpy(ustart, ucodefp->uf_header, UCODE_HEADER_SIZE_INTEL);
833adc586deSMark Johnson 	(void) memcpy(&ustart[UCODE_HEADER_SIZE_INTEL], ucodefp->uf_body,
834adc586deSMark Johnson 	    body_size);
835adc586deSMark Johnson 	if (ext_size > 0) {
836adc586deSMark Johnson 		(void) memcpy(&ustart[ext_offset],
837adc586deSMark Johnson 		    ucodefp->uf_ext_table, ext_size);
838adc586deSMark Johnson 	}
839adc586deSMark Johnson 	uus.ucodep = ustart;
840adc586deSMark Johnson 	uus.usize = usize;
841adc586deSMark Johnson 	ucode_load_xpv(&uus);
842adc586deSMark Johnson 	ucode->read_rev(uinfop);
843adc586deSMark Johnson 	uus.new_rev = uinfop->cui_rev;
844adc586deSMark Johnson #else
845adc586deSMark Johnson 	kpreempt_disable();
846adc586deSMark Johnson 	wrmsr(ucode->write_msr, (uintptr_t)ucodefp->uf_body);
847adc586deSMark Johnson 	ucode->read_rev(uinfop);
848adc586deSMark Johnson 	kpreempt_enable();
849adc586deSMark Johnson #endif
850adc586deSMark Johnson 
851adc586deSMark Johnson 	return (ucodefp->uf_header->uh_rev);
8522449e17fSsherrym }
8532449e17fSsherrym 
854882a7af5SMark Johnson 
855882a7af5SMark Johnson #ifdef	__xpv
856882a7af5SMark Johnson static void
857adc586deSMark Johnson ucode_load_xpv(ucode_update_t *uusp)
858882a7af5SMark Johnson {
859882a7af5SMark Johnson 	xen_platform_op_t op;
860882a7af5SMark Johnson 	int e;
861882a7af5SMark Johnson 
862882a7af5SMark Johnson 	ASSERT(DOMAIN_IS_INITDOMAIN(xen_info));
863882a7af5SMark Johnson 
864882a7af5SMark Johnson 	kpreempt_disable();
865882a7af5SMark Johnson 	op.cmd = XENPF_microcode_update;
866882a7af5SMark Johnson 	op.interface_version = XENPF_INTERFACE_VERSION;
867882a7af5SMark Johnson 	/*LINTED: constant in conditional context*/
868adc586deSMark Johnson 	set_xen_guest_handle(op.u.microcode.data, uusp->ucodep);
869adc586deSMark Johnson 	op.u.microcode.length = uusp->usize;
870882a7af5SMark Johnson 	e = HYPERVISOR_platform_op(&op);
871882a7af5SMark Johnson 	if (e != 0) {
872882a7af5SMark Johnson 		cmn_err(CE_WARN, "hypervisor failed to accept uCode update");
873882a7af5SMark Johnson 	}
874882a7af5SMark Johnson 	kpreempt_enable();
875882a7af5SMark Johnson }
876882a7af5SMark Johnson #endif /* __xpv */
877882a7af5SMark Johnson 
878adc586deSMark Johnson static void
879adc586deSMark Johnson ucode_read_rev_amd(cpu_ucode_info_t *uinfop)
880adc586deSMark Johnson {
881adc586deSMark Johnson 	uinfop->cui_rev = rdmsr(MSR_AMD_PATCHLEVEL);
882adc586deSMark Johnson }
883882a7af5SMark Johnson 
8842449e17fSsherrym static void
885adc586deSMark Johnson ucode_read_rev_intel(cpu_ucode_info_t *uinfop)
8862449e17fSsherrym {
8872449e17fSsherrym 	struct cpuid_regs crs;
8882449e17fSsherrym 
8892449e17fSsherrym 	/*
8902449e17fSsherrym 	 * The Intel 64 and IA-32 Architecture Software Developer's Manual
8912449e17fSsherrym 	 * recommends that MSR_INTC_UCODE_REV be loaded with 0 first, then
8922449e17fSsherrym 	 * execute cpuid to guarantee the correct reading of this register.
8932449e17fSsherrym 	 */
8942449e17fSsherrym 	wrmsr(MSR_INTC_UCODE_REV, 0);
8952449e17fSsherrym 	(void) __cpuid_insn(&crs);
8962449e17fSsherrym 	uinfop->cui_rev = (rdmsr(MSR_INTC_UCODE_REV) >> INTC_UCODE_REV_SHIFT);
8972449e17fSsherrym }
8982449e17fSsherrym 
899adc586deSMark Johnson static ucode_errno_t
900adc586deSMark Johnson ucode_extract_amd(ucode_update_t *uusp, uint8_t *ucodep, int size)
901adc586deSMark Johnson {
9020ba6f73dSMark Johnson #ifndef __xpv
903adc586deSMark Johnson 	uint32_t *ptr = (uint32_t *)ucodep;
904adc586deSMark Johnson 	ucode_eqtbl_amd_t *eqtbl;
905adc586deSMark Johnson 	ucode_file_amd_t *ufp;
9060ba6f73dSMark Johnson 	int count;
9070ba6f73dSMark Johnson 	int higher = 0;
9080ba6f73dSMark Johnson 	ucode_errno_t rc = EM_NOMATCH;
9090ba6f73dSMark Johnson 	uint16_t eq_sig;
910adc586deSMark Johnson 
911adc586deSMark Johnson 	/* skip over magic number & equivalence table header */
912adc586deSMark Johnson 	ptr += 2; size -= 8;
913adc586deSMark Johnson 
914adc586deSMark Johnson 	count = *ptr++; size -= 4;
915adc586deSMark Johnson 	for (eqtbl = (ucode_eqtbl_amd_t *)ptr;
916adc586deSMark Johnson 	    eqtbl->ue_inst_cpu && eqtbl->ue_inst_cpu != uusp->sig;
917adc586deSMark Johnson 	    eqtbl++)
918adc586deSMark Johnson 		;
919adc586deSMark Johnson 
920adc586deSMark Johnson 	eq_sig = eqtbl->ue_equiv_cpu;
921adc586deSMark Johnson 
922adc586deSMark Johnson 	/* No equivalent CPU id found, assume outdated microcode file. */
923adc586deSMark Johnson 	if (eq_sig == 0)
924adc586deSMark Johnson 		return (EM_HIGHERREV);
925adc586deSMark Johnson 
926adc586deSMark Johnson 	/* Use the first microcode patch that matches. */
927adc586deSMark Johnson 	do {
928adc586deSMark Johnson 		ptr += count >> 2; size -= count;
929adc586deSMark Johnson 
930adc586deSMark Johnson 		if (!size)
9310ba6f73dSMark Johnson 			return (higher ? EM_HIGHERREV : EM_NOMATCH);
932adc586deSMark Johnson 
933adc586deSMark Johnson 		ptr++; size -= 4;
934adc586deSMark Johnson 		count = *ptr++; size -= 4;
935adc586deSMark Johnson 		ufp = (ucode_file_amd_t *)ptr;
9360ba6f73dSMark Johnson 
9370ba6f73dSMark Johnson 		rc = ucode_match_amd(eq_sig, &uusp->info, ufp, count);
9380ba6f73dSMark Johnson 		if (rc == EM_HIGHERREV)
9390ba6f73dSMark Johnson 			higher = 1;
9400ba6f73dSMark Johnson 	} while (rc != EM_OK);
941adc586deSMark Johnson 
942adc586deSMark Johnson 	uusp->ucodep = (uint8_t *)ufp;
943adc586deSMark Johnson 	uusp->usize = count;
944adc586deSMark Johnson 	uusp->expected_rev = ufp->uf_header.uh_patch_id;
9450ba6f73dSMark Johnson #else
9460ba6f73dSMark Johnson 	/*
9470ba6f73dSMark Johnson 	 * The hypervisor will choose the patch to load, so there is no way to
9480ba6f73dSMark Johnson 	 * know the "expected revision" in advance. This is especially true on
9490ba6f73dSMark Johnson 	 * mixed-revision systems where more than one patch will be loaded.
9500ba6f73dSMark Johnson 	 */
9510ba6f73dSMark Johnson 	uusp->expected_rev = 0;
9520ba6f73dSMark Johnson 	uusp->ucodep = ucodep;
9530ba6f73dSMark Johnson 	uusp->usize = size;
9540ba6f73dSMark Johnson 
9550ba6f73dSMark Johnson 	ucode_chipset_amd(ucodep, size);
9560ba6f73dSMark Johnson #endif
957adc586deSMark Johnson 
958adc586deSMark Johnson 	return (EM_OK);
959adc586deSMark Johnson }
960adc586deSMark Johnson 
961adc586deSMark Johnson static ucode_errno_t
962adc586deSMark Johnson ucode_extract_intel(ucode_update_t *uusp, uint8_t *ucodep, int size)
963adc586deSMark Johnson {
964adc586deSMark Johnson 	uint32_t	header_size = UCODE_HEADER_SIZE_INTEL;
965adc586deSMark Johnson 	int		remaining;
966adc586deSMark Johnson 	int		found = 0;
967adc586deSMark Johnson 	ucode_errno_t	search_rc = EM_NOMATCH; /* search result */
968adc586deSMark Johnson 
969adc586deSMark Johnson 	/*
970adc586deSMark Johnson 	 * Go through the whole buffer in case there are
971adc586deSMark Johnson 	 * multiple versions of matching microcode for this
972adc586deSMark Johnson 	 * processor.
973adc586deSMark Johnson 	 */
974adc586deSMark Johnson 	for (remaining = size; remaining > 0; ) {
975adc586deSMark Johnson 		int	total_size, body_size, ext_size;
976adc586deSMark Johnson 		uint8_t	*curbuf = &ucodep[size - remaining];
977adc586deSMark Johnson 		ucode_header_intel_t *uhp = (ucode_header_intel_t *)curbuf;
978adc586deSMark Johnson 		ucode_ext_table_intel_t *uetp = NULL;
979adc586deSMark Johnson 		ucode_errno_t tmprc;
980adc586deSMark Johnson 
981adc586deSMark Johnson 		total_size = UCODE_TOTAL_SIZE_INTEL(uhp->uh_total_size);
982adc586deSMark Johnson 		body_size = UCODE_BODY_SIZE_INTEL(uhp->uh_body_size);
983adc586deSMark Johnson 		ext_size = total_size - (header_size + body_size);
984adc586deSMark Johnson 
985adc586deSMark Johnson 		if (ext_size > 0)
986adc586deSMark Johnson 			uetp = (ucode_ext_table_intel_t *)
987adc586deSMark Johnson 			    &curbuf[header_size + body_size];
988adc586deSMark Johnson 
989adc586deSMark Johnson 		tmprc = ucode_match_intel(uusp->sig, &uusp->info, uhp, uetp);
990adc586deSMark Johnson 
991adc586deSMark Johnson 		/*
992adc586deSMark Johnson 		 * Since we are searching through a big file
993adc586deSMark Johnson 		 * containing microcode for pretty much all the
994adc586deSMark Johnson 		 * processors, we are bound to get EM_NOMATCH
995adc586deSMark Johnson 		 * at one point.  However, if we return
996adc586deSMark Johnson 		 * EM_NOMATCH to users, it will really confuse
997adc586deSMark Johnson 		 * them.  Therefore, if we ever find a match of
998adc586deSMark Johnson 		 * a lower rev, we will set return code to
999adc586deSMark Johnson 		 * EM_HIGHERREV.
1000adc586deSMark Johnson 		 */
1001adc586deSMark Johnson 		if (tmprc == EM_HIGHERREV)
1002adc586deSMark Johnson 			search_rc = EM_HIGHERREV;
1003adc586deSMark Johnson 
1004adc586deSMark Johnson 		if (tmprc == EM_OK &&
1005adc586deSMark Johnson 		    uusp->expected_rev < uhp->uh_rev) {
1006adc586deSMark Johnson #ifndef __xpv
1007adc586deSMark Johnson 			uusp->ucodep = (uint8_t *)&curbuf[header_size];
1008adc586deSMark Johnson #else
1009adc586deSMark Johnson 			uusp->ucodep = (uint8_t *)curbuf;
1010adc586deSMark Johnson #endif
1011adc586deSMark Johnson 			uusp->usize =
1012adc586deSMark Johnson 			    UCODE_TOTAL_SIZE_INTEL(uhp->uh_total_size);
1013adc586deSMark Johnson 			uusp->expected_rev = uhp->uh_rev;
1014adc586deSMark Johnson 			found = 1;
1015adc586deSMark Johnson 		}
1016adc586deSMark Johnson 
1017adc586deSMark Johnson 		remaining -= total_size;
1018adc586deSMark Johnson 	}
1019adc586deSMark Johnson 
1020adc586deSMark Johnson 	if (!found)
1021adc586deSMark Johnson 		return (search_rc);
1022adc586deSMark Johnson 
1023adc586deSMark Johnson 	return (EM_OK);
1024adc586deSMark Johnson }
10252449e17fSsherrym /*
10262449e17fSsherrym  * Entry point to microcode update from the ucode_drv driver.
10272449e17fSsherrym  *
10282449e17fSsherrym  * Returns EM_OK on success, corresponding error code on failure.
10292449e17fSsherrym  */
10302449e17fSsherrym ucode_errno_t
10312449e17fSsherrym ucode_update(uint8_t *ucodep, int size)
10322449e17fSsherrym {
10332449e17fSsherrym 	int		found = 0;
10342449e17fSsherrym 	processorid_t	id;
1035adc586deSMark Johnson 	ucode_update_t	cached = { 0 };
1036adc586deSMark Johnson 	ucode_update_t	*cachedp = NULL;
10372449e17fSsherrym 	ucode_errno_t	rc = EM_OK;
10382449e17fSsherrym 	ucode_errno_t	search_rc = EM_NOMATCH; /* search result */
10392449e17fSsherrym 	cpuset_t cpuset;
10402449e17fSsherrym 
1041adc586deSMark Johnson 	ASSERT(ucode);
10422449e17fSsherrym 	ASSERT(ucodep);
10432449e17fSsherrym 	CPUSET_ZERO(cpuset);
10442449e17fSsherrym 
1045adc586deSMark Johnson 	if (!ucode->capable(CPU))
10462449e17fSsherrym 		return (EM_NOTSUP);
10472449e17fSsherrym 
10482449e17fSsherrym 	mutex_enter(&cpu_lock);
10492449e17fSsherrym 
10502449e17fSsherrym 	for (id = 0; id < max_ncpus; id++) {
10512449e17fSsherrym 		cpu_t *cpu;
1052adc586deSMark Johnson 		ucode_update_t uus = { 0 };
1053adc586deSMark Johnson 		ucode_update_t *uusp = &uus;
10542449e17fSsherrym 
10552449e17fSsherrym 		/*
10562449e17fSsherrym 		 * If there is no such CPU or it is not xcall ready, skip it.
10572449e17fSsherrym 		 */
10582449e17fSsherrym 		if ((cpu = cpu_get(id)) == NULL ||
10592449e17fSsherrym 		    !(cpu->cpu_flags & CPU_READY))
10602449e17fSsherrym 			continue;
10612449e17fSsherrym 
10622449e17fSsherrym 		uusp->sig = cpuid_getsig(cpu);
10632449e17fSsherrym 		bcopy(cpu->cpu_m.mcpu_ucode_info, &uusp->info,
10642449e17fSsherrym 		    sizeof (uusp->info));
10652449e17fSsherrym 
10662449e17fSsherrym 		/*
10672449e17fSsherrym 		 * If the current CPU has the same signature and platform
10682449e17fSsherrym 		 * id as the previous one we processed, reuse the information.
10692449e17fSsherrym 		 */
10702449e17fSsherrym 		if (cachedp && cachedp->sig == cpuid_getsig(cpu) &&
10712449e17fSsherrym 		    cachedp->info.cui_platid == uusp->info.cui_platid) {
10722449e17fSsherrym 			uusp->ucodep = cachedp->ucodep;
10732449e17fSsherrym 			uusp->expected_rev = cachedp->expected_rev;
10742449e17fSsherrym 			/*
10752449e17fSsherrym 			 * Intuitively we should check here to see whether the
10762449e17fSsherrym 			 * running microcode rev is >= the expected rev, and
10772449e17fSsherrym 			 * quit if it is.  But we choose to proceed with the
10782449e17fSsherrym 			 * xcall regardless of the running version so that
10792449e17fSsherrym 			 * the other threads in an HT processor can update
10802449e17fSsherrym 			 * the cpu_ucode_info structure in machcpu.
10812449e17fSsherrym 			 */
1082adc586deSMark Johnson 		} else if ((search_rc = ucode->extract(uusp, ucodep, size))
1083adc586deSMark Johnson 		    == EM_OK) {
10842449e17fSsherrym 			bcopy(uusp, &cached, sizeof (cached));
10852449e17fSsherrym 			cachedp = &cached;
10862449e17fSsherrym 			found = 1;
10872449e17fSsherrym 		}
10882449e17fSsherrym 
10892449e17fSsherrym 		/* Nothing to do */
10902449e17fSsherrym 		if (uusp->ucodep == NULL)
10912449e17fSsherrym 			continue;
10922449e17fSsherrym 
1093882a7af5SMark Johnson #ifdef	__xpv
1094882a7af5SMark Johnson 		/*
1095882a7af5SMark Johnson 		 * for i86xpv, the hypervisor will update all the CPUs.
1096882a7af5SMark Johnson 		 * the hypervisor wants the header, data, and extended
1097882a7af5SMark Johnson 		 * signature tables. ucode_write will just read in the
1098882a7af5SMark Johnson 		 * updated version on all the CPUs after the update has
1099882a7af5SMark Johnson 		 * completed.
1100882a7af5SMark Johnson 		 */
1101c9b5d7d2SMark Johnson 		if (id == 0) {
1102adc586deSMark Johnson 			ucode_load_xpv(uusp);
1103c9b5d7d2SMark Johnson 		}
1104882a7af5SMark Johnson #endif
1105882a7af5SMark Johnson 
11062449e17fSsherrym 		CPUSET_ADD(cpuset, id);
11072449e17fSsherrym 		kpreempt_disable();
1108f34a7178SJoe Bonasera 		xc_sync((xc_arg_t)uusp, 0, 0, CPUSET2BV(cpuset), ucode_write);
11092449e17fSsherrym 		kpreempt_enable();
11102449e17fSsherrym 		CPUSET_DEL(cpuset, id);
11112449e17fSsherrym 
1112*5e53ed34SHans Rosenfeld 		if (uusp->new_rev != 0 && uusp->info.cui_rev == uusp->new_rev &&
1113*5e53ed34SHans Rosenfeld 		    !ucode_force_update) {
11140ba6f73dSMark Johnson 			rc = EM_HIGHERREV;
11150ba6f73dSMark Johnson 		} else if ((uusp->new_rev == 0) || (uusp->expected_rev != 0 &&
11160ba6f73dSMark Johnson 		    uusp->expected_rev != uusp->new_rev)) {
11172449e17fSsherrym 			cmn_err(CE_WARN, ucode_failure_fmt,
11182449e17fSsherrym 			    id, uusp->info.cui_rev, uusp->expected_rev);
11192449e17fSsherrym 			rc = EM_UPDATE;
11200ba6f73dSMark Johnson 		} else {
11210ba6f73dSMark Johnson 			cmn_err(CE_CONT, ucode_success_fmt,
11220ba6f73dSMark Johnson 			    id, uusp->info.cui_rev, uusp->new_rev);
11232449e17fSsherrym 		}
11242449e17fSsherrym 	}
11252449e17fSsherrym 
11262449e17fSsherrym 	mutex_exit(&cpu_lock);
11272449e17fSsherrym 
11282449e17fSsherrym 	if (!found)
11292449e17fSsherrym 		rc = search_rc;
11302449e17fSsherrym 
11312449e17fSsherrym 	return (rc);
11322449e17fSsherrym }
11332449e17fSsherrym 
11342449e17fSsherrym /*
11352449e17fSsherrym  * Initialize mcpu_ucode_info, and perform microcode update if necessary.
11362449e17fSsherrym  * This is the entry point from boot path where pointer to CPU structure
11372449e17fSsherrym  * is available.
11382449e17fSsherrym  *
11392449e17fSsherrym  * cpuid_info must be initialized before ucode_check can be called.
11402449e17fSsherrym  */
11412449e17fSsherrym void
11422449e17fSsherrym ucode_check(cpu_t *cp)
11432449e17fSsherrym {
1144adc586deSMark Johnson 	cpu_ucode_info_t *uinfop;
11452449e17fSsherrym 	ucode_errno_t rc = EM_OK;
1146adc586deSMark Johnson 	uint32_t new_rev = 0;
11472449e17fSsherrym 
11482449e17fSsherrym 	ASSERT(cp);
1149a3114836SGerry Liu 	/*
1150a3114836SGerry Liu 	 * Space statically allocated for BSP, ensure pointer is set
1151a3114836SGerry Liu 	 */
1152a3114836SGerry Liu 	if (cp->cpu_id == 0 && cp->cpu_m.mcpu_ucode_info == NULL)
11532449e17fSsherrym 		cp->cpu_m.mcpu_ucode_info = &cpu_ucode_info0;
11542449e17fSsherrym 
11552449e17fSsherrym 	uinfop = cp->cpu_m.mcpu_ucode_info;
11562449e17fSsherrym 	ASSERT(uinfop);
11572449e17fSsherrym 
1158adc586deSMark Johnson 	/* set up function pointers if not already done */
1159adc586deSMark Johnson 	if (!ucode)
1160adc586deSMark Johnson 		switch (cpuid_getvendor(cp)) {
1161adc586deSMark Johnson 		case X86_VENDOR_AMD:
1162adc586deSMark Johnson 			ucode = &ucode_amd;
1163adc586deSMark Johnson 			break;
1164adc586deSMark Johnson 		case X86_VENDOR_Intel:
1165adc586deSMark Johnson 			ucode = &ucode_intel;
1166adc586deSMark Johnson 			break;
1167adc586deSMark Johnson 		default:
1168126b0a71SMark Johnson 			ucode = NULL;
1169adc586deSMark Johnson 			return;
1170adc586deSMark Johnson 		}
1171adc586deSMark Johnson 
1172adc586deSMark Johnson 	if (!ucode->capable(cp))
11732449e17fSsherrym 		return;
11742449e17fSsherrym 
11752449e17fSsherrym 	/*
11762449e17fSsherrym 	 * The MSR_INTC_PLATFORM_ID is supported in Celeron and Xeon
11772449e17fSsherrym 	 * (Family 6, model 5 and above) and all processors after.
11782449e17fSsherrym 	 */
1179adc586deSMark Johnson 	if ((cpuid_getvendor(cp) == X86_VENDOR_Intel) &&
1180adc586deSMark Johnson 	    ((cpuid_getmodel(cp) >= 5) || (cpuid_getfamily(cp) > 6))) {
11812449e17fSsherrym 		uinfop->cui_platid = 1 << ((rdmsr(MSR_INTC_PLATFORM_ID) >>
11822449e17fSsherrym 		    INTC_PLATFORM_ID_SHIFT) & INTC_PLATFORM_ID_MASK);
11832449e17fSsherrym 	}
11842449e17fSsherrym 
1185adc586deSMark Johnson 	ucode->read_rev(uinfop);
11862449e17fSsherrym 
1187c9b5d7d2SMark Johnson #ifdef	__xpv
1188c9b5d7d2SMark Johnson 	/*
1189c9b5d7d2SMark Johnson 	 * for i86xpv, the hypervisor will update all the CPUs. We only need
1190c9b5d7d2SMark Johnson 	 * do do this on one of the CPUs (and there always is a CPU 0).
1191c9b5d7d2SMark Johnson 	 */
1192c9b5d7d2SMark Johnson 	if (cp->cpu_id != 0) {
1193c9b5d7d2SMark Johnson 		return;
1194c9b5d7d2SMark Johnson 	}
1195c9b5d7d2SMark Johnson #endif
1196c9b5d7d2SMark Johnson 
11972449e17fSsherrym 	/*
11982449e17fSsherrym 	 * Check to see if we need ucode update
11992449e17fSsherrym 	 */
1200adc586deSMark Johnson 	if ((rc = ucode->locate(cp, uinfop, &ucodefile)) == EM_OK) {
1201adc586deSMark Johnson 		new_rev = ucode->load(&ucodefile, uinfop, cp);
1202882a7af5SMark Johnson 
1203adc586deSMark Johnson 		if (uinfop->cui_rev != new_rev)
12042449e17fSsherrym 			cmn_err(CE_WARN, ucode_failure_fmt, cp->cpu_id,
1205adc586deSMark Johnson 			    uinfop->cui_rev, new_rev);
12062449e17fSsherrym 	}
12072449e17fSsherrym 
12082449e17fSsherrym 	/*
12092449e17fSsherrym 	 * If we fail to find a match for any reason, free the file structure
12102449e17fSsherrym 	 * just in case we have read in a partial file.
12112449e17fSsherrym 	 *
12122449e17fSsherrym 	 * Since the scratch memory for holding the microcode for the boot CPU
12132449e17fSsherrym 	 * came from BOP_ALLOC, we will reset the data structure as if we
12142449e17fSsherrym 	 * never did the allocation so we don't have to keep track of this
12152449e17fSsherrym 	 * special chunk of memory.  We free the memory used for the rest
12162449e17fSsherrym 	 * of the CPUs in start_other_cpus().
12172449e17fSsherrym 	 */
12182449e17fSsherrym 	if (rc != EM_OK || cp->cpu_id == 0)
1219adc586deSMark Johnson 		ucode->file_reset(&ucodefile, cp->cpu_id);
12202449e17fSsherrym }
12212449e17fSsherrym 
12222449e17fSsherrym /*
12232449e17fSsherrym  * Returns microcode revision from the machcpu structure.
12242449e17fSsherrym  */
12252449e17fSsherrym ucode_errno_t
12262449e17fSsherrym ucode_get_rev(uint32_t *revp)
12272449e17fSsherrym {
12282449e17fSsherrym 	int i;
12292449e17fSsherrym 
1230adc586deSMark Johnson 	ASSERT(ucode);
12312449e17fSsherrym 	ASSERT(revp);
12322449e17fSsherrym 
1233adc586deSMark Johnson 	if (!ucode->capable(CPU))
12342449e17fSsherrym 		return (EM_NOTSUP);
12352449e17fSsherrym 
12362449e17fSsherrym 	mutex_enter(&cpu_lock);
12372449e17fSsherrym 	for (i = 0; i < max_ncpus; i++) {
12382449e17fSsherrym 		cpu_t *cpu;
12392449e17fSsherrym 
12402449e17fSsherrym 		if ((cpu = cpu_get(i)) == NULL)
12412449e17fSsherrym 			continue;
12422449e17fSsherrym 
12432449e17fSsherrym 		revp[i] = cpu->cpu_m.mcpu_ucode_info->cui_rev;
12442449e17fSsherrym 	}
12452449e17fSsherrym 	mutex_exit(&cpu_lock);
12462449e17fSsherrym 
12472449e17fSsherrym 	return (EM_OK);
12482449e17fSsherrym }
1249