xref: /titanic_51/usr/src/uts/i86pc/os/microcode.c (revision 0ba6f73dcd1c9160a44b2872ed0e0d81b51d168f)
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 /*
23*0ba6f73dSMark 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>
362449e17fSsherrym #include <sys/param.h>
372449e17fSsherrym #include <sys/machparam.h>
382449e17fSsherrym #include <sys/promif.h>
392449e17fSsherrym #include <sys/sysmacros.h>
402449e17fSsherrym #include <sys/systm.h>
412449e17fSsherrym #include <sys/types.h>
422449e17fSsherrym #include <sys/thread.h>
432449e17fSsherrym #include <sys/ucode.h>
442449e17fSsherrym #include <sys/x86_archext.h>
452449e17fSsherrym #include <sys/x_call.h>
46843e1988Sjohnlev #ifdef	__xpv
47843e1988Sjohnlev #include <sys/hypervisor.h>
48843e1988Sjohnlev #endif
492449e17fSsherrym 
502449e17fSsherrym /*
51adc586deSMark Johnson  * AMD-specific equivalence table
522449e17fSsherrym  */
53adc586deSMark Johnson static ucode_eqtbl_amd_t *ucode_eqtbl_amd;
542449e17fSsherrym 
552449e17fSsherrym /*
562449e17fSsherrym  * mcpu_ucode_info for the boot CPU.  Statically allocated.
572449e17fSsherrym  */
582449e17fSsherrym static struct cpu_ucode_info cpu_ucode_info0;
592449e17fSsherrym 
60adc586deSMark Johnson static ucode_file_t ucodefile;
612449e17fSsherrym 
62adc586deSMark Johnson static void* ucode_zalloc(processorid_t, size_t);
63adc586deSMark Johnson static void ucode_free(processorid_t, void *, size_t);
64adc586deSMark Johnson 
65adc586deSMark Johnson static int ucode_capable_amd(cpu_t *);
66adc586deSMark Johnson static int ucode_capable_intel(cpu_t *);
67adc586deSMark Johnson 
68adc586deSMark Johnson static ucode_errno_t ucode_extract_amd(ucode_update_t *, uint8_t *, int);
69adc586deSMark Johnson static ucode_errno_t ucode_extract_intel(ucode_update_t *, uint8_t *,
70adc586deSMark Johnson     int);
71adc586deSMark Johnson 
72adc586deSMark Johnson static void ucode_file_reset_amd(ucode_file_t *, processorid_t);
73adc586deSMark Johnson static void ucode_file_reset_intel(ucode_file_t *, processorid_t);
74adc586deSMark Johnson 
75adc586deSMark Johnson static uint32_t ucode_load_amd(ucode_file_t *, cpu_ucode_info_t *, cpu_t *);
76adc586deSMark Johnson static uint32_t ucode_load_intel(ucode_file_t *, cpu_ucode_info_t *, cpu_t *);
77adc586deSMark Johnson 
78882a7af5SMark Johnson #ifdef	__xpv
79adc586deSMark Johnson static void ucode_load_xpv(ucode_update_t *);
80*0ba6f73dSMark Johnson static void ucode_chipset_amd(uint8_t *, int);
81882a7af5SMark Johnson #endif
822449e17fSsherrym 
83*0ba6f73dSMark Johnson static int ucode_equiv_cpu_amd(cpu_t *, uint16_t *);
84adc586deSMark Johnson 
85adc586deSMark Johnson static ucode_errno_t ucode_locate_amd(cpu_t *, cpu_ucode_info_t *,
86adc586deSMark Johnson     ucode_file_t *);
87adc586deSMark Johnson static ucode_errno_t ucode_locate_intel(cpu_t *, cpu_ucode_info_t *,
88adc586deSMark Johnson     ucode_file_t *);
89adc586deSMark Johnson 
90*0ba6f73dSMark Johnson #ifndef __xpv
91*0ba6f73dSMark Johnson static ucode_errno_t ucode_match_amd(uint16_t, cpu_ucode_info_t *,
92adc586deSMark Johnson     ucode_file_amd_t *, int);
93*0ba6f73dSMark Johnson #endif
94adc586deSMark Johnson static ucode_errno_t ucode_match_intel(int, cpu_ucode_info_t *,
95adc586deSMark Johnson     ucode_header_intel_t *, ucode_ext_table_intel_t *);
96adc586deSMark Johnson 
97adc586deSMark Johnson static void ucode_read_rev_amd(cpu_ucode_info_t *);
98adc586deSMark Johnson static void ucode_read_rev_intel(cpu_ucode_info_t *);
99adc586deSMark Johnson 
100adc586deSMark Johnson static const struct ucode_ops ucode_amd = {
101adc586deSMark Johnson 	MSR_AMD_PATCHLOADER,
102adc586deSMark Johnson 	ucode_capable_amd,
103adc586deSMark Johnson 	ucode_file_reset_amd,
104adc586deSMark Johnson 	ucode_read_rev_amd,
105adc586deSMark Johnson 	ucode_load_amd,
106adc586deSMark Johnson 	ucode_validate_amd,
107adc586deSMark Johnson 	ucode_extract_amd,
108adc586deSMark Johnson 	ucode_locate_amd
109adc586deSMark Johnson };
110adc586deSMark Johnson 
111adc586deSMark Johnson static const struct ucode_ops ucode_intel = {
112adc586deSMark Johnson 	MSR_INTC_UCODE_WRITE,
113adc586deSMark Johnson 	ucode_capable_intel,
114adc586deSMark Johnson 	ucode_file_reset_intel,
115adc586deSMark Johnson 	ucode_read_rev_intel,
116adc586deSMark Johnson 	ucode_load_intel,
117adc586deSMark Johnson 	ucode_validate_intel,
118adc586deSMark Johnson 	ucode_extract_intel,
119adc586deSMark Johnson 	ucode_locate_intel
120adc586deSMark Johnson };
121adc586deSMark Johnson 
122adc586deSMark Johnson const struct ucode_ops *ucode;
123adc586deSMark Johnson 
1242449e17fSsherrym static const char ucode_failure_fmt[] =
12588699bddSsherrym 	"cpu%d: failed to update microcode from version 0x%x to 0x%x\n";
1262449e17fSsherrym static const char ucode_success_fmt[] =
12788699bddSsherrym 	"?cpu%d: microcode has been updated from version 0x%x to 0x%x\n";
1282449e17fSsherrym 
1292449e17fSsherrym /*
1302449e17fSsherrym  * Force flag.  If set, the first microcode binary that matches
1312449e17fSsherrym  * signature and platform id will be used for microcode update,
1322449e17fSsherrym  * regardless of version.  Should only be used for debugging.
1332449e17fSsherrym  */
1342449e17fSsherrym int ucode_force_update = 0;
1352449e17fSsherrym 
1362449e17fSsherrym /*
1372449e17fSsherrym  * Allocate space for mcpu_ucode_info in the machcpu structure
1382449e17fSsherrym  * for all non-boot CPUs.
1392449e17fSsherrym  */
1402449e17fSsherrym void
1412449e17fSsherrym ucode_alloc_space(cpu_t *cp)
1422449e17fSsherrym {
1432449e17fSsherrym 	ASSERT(cp->cpu_id != 0);
1442449e17fSsherrym 	cp->cpu_m.mcpu_ucode_info =
1452449e17fSsherrym 	    kmem_zalloc(sizeof (*cp->cpu_m.mcpu_ucode_info), KM_SLEEP);
1462449e17fSsherrym }
1472449e17fSsherrym 
1482449e17fSsherrym void
1492449e17fSsherrym ucode_free_space(cpu_t *cp)
1502449e17fSsherrym {
1512449e17fSsherrym 	ASSERT(cp->cpu_id != 0);
1522449e17fSsherrym 	kmem_free(cp->cpu_m.mcpu_ucode_info,
1532449e17fSsherrym 	    sizeof (*cp->cpu_m.mcpu_ucode_info));
1542449e17fSsherrym }
1552449e17fSsherrym 
1562449e17fSsherrym /*
1572449e17fSsherrym  * Called when we are done with microcode update on all processors to free up
1582449e17fSsherrym  * space allocated for the microcode file.
1592449e17fSsherrym  */
1602449e17fSsherrym void
161adc586deSMark Johnson ucode_cleanup()
1622449e17fSsherrym {
163adc586deSMark Johnson 	ASSERT(ucode);
164adc586deSMark Johnson 
165adc586deSMark Johnson 	ucode->file_reset(&ucodefile, -1);
166adc586deSMark Johnson }
167adc586deSMark Johnson 
168adc586deSMark Johnson /*
169adc586deSMark Johnson  * Allocate/free a buffer used to hold ucode data. Space for the boot CPU is
170adc586deSMark Johnson  * allocated with BOP_ALLOC() and does not require a free.
171adc586deSMark Johnson  */
172adc586deSMark Johnson static void*
173adc586deSMark Johnson ucode_zalloc(processorid_t id, size_t size)
174adc586deSMark Johnson {
175adc586deSMark Johnson 	if (id)
176adc586deSMark Johnson 		return (kmem_zalloc(size, KM_NOSLEEP));
177adc586deSMark Johnson 
178adc586deSMark Johnson 	/* BOP_ALLOC() failure results in panic */
179adc586deSMark Johnson 	return (BOP_ALLOC(bootops, NULL, size, MMU_PAGESIZE));
180adc586deSMark Johnson }
181adc586deSMark Johnson 
182adc586deSMark Johnson static void
183adc586deSMark Johnson ucode_free(processorid_t id, void* buf, size_t size)
184adc586deSMark Johnson {
185adc586deSMark Johnson 	if (id)
186adc586deSMark Johnson 		kmem_free(buf, size);
1872449e17fSsherrym }
1882449e17fSsherrym 
1892449e17fSsherrym /*
1902449e17fSsherrym  * Check whether or not a processor is capable of microcode operations
1912449e17fSsherrym  * Returns 1 if it is capable, 0 if not.
192adc586deSMark Johnson  *
193adc586deSMark Johnson  * At this point we only support microcode update for:
194adc586deSMark Johnson  * - Intel processors family 6 and above, and
195adc586deSMark Johnson  * - AMD processors family 0x10 and above.
196adc586deSMark Johnson  *
197adc586deSMark Johnson  * We also assume that we don't support a mix of Intel and
198adc586deSMark Johnson  * AMD processors in the same box.
199adc586deSMark Johnson  *
200adc586deSMark Johnson  * An i86xpv guest domain can't update the microcode.
2012449e17fSsherrym  */
202adc586deSMark Johnson /*ARGSUSED*/
2032449e17fSsherrym static int
204adc586deSMark Johnson ucode_capable_amd(cpu_t *cp)
2052449e17fSsherrym {
206adc586deSMark Johnson #ifndef	__xpv
207adc586deSMark Johnson 	extern int xpv_is_hvm;
208adc586deSMark Johnson 	if (xpv_is_hvm) {
209adc586deSMark Johnson 		return (0);
210adc586deSMark Johnson 	}
211adc586deSMark Johnson #else
212adc586deSMark Johnson 	if (!DOMAIN_IS_INITDOMAIN(xen_info)) {
213adc586deSMark Johnson 		return (0);
214adc586deSMark Johnson 	}
215adc586deSMark Johnson #endif
216*0ba6f73dSMark Johnson 	return (cpuid_getfamily(cp) >= 0x10);
217adc586deSMark Johnson }
218*0ba6f73dSMark Johnson 
219adc586deSMark Johnson static int
220adc586deSMark Johnson ucode_capable_intel(cpu_t *cp)
221adc586deSMark Johnson {
222882a7af5SMark Johnson #ifndef	__xpv
223882a7af5SMark Johnson 	extern int xpv_is_hvm;
224882a7af5SMark Johnson 	if (xpv_is_hvm) {
225882a7af5SMark Johnson 		return (0);
226882a7af5SMark Johnson 	}
227882a7af5SMark Johnson #else
228843e1988Sjohnlev 	if (!DOMAIN_IS_INITDOMAIN(xen_info)) {
229843e1988Sjohnlev 		return (0);
230843e1988Sjohnlev 	}
231843e1988Sjohnlev #endif
232adc586deSMark Johnson 	return (cpuid_getfamily(cp) >= 6);
2332449e17fSsherrym }
2342449e17fSsherrym 
2352449e17fSsherrym /*
2362449e17fSsherrym  * Called when it is no longer necessary to keep the microcode around,
2372449e17fSsherrym  * or when the cached microcode doesn't match the CPU being processed.
2382449e17fSsherrym  */
2392449e17fSsherrym static void
240adc586deSMark Johnson ucode_file_reset_amd(ucode_file_t *ufp, processorid_t id)
2412449e17fSsherrym {
242adc586deSMark Johnson 	ucode_file_amd_t *ucodefp = ufp->amd;
2432449e17fSsherrym 
2442449e17fSsherrym 	if (ucodefp == NULL)
2452449e17fSsherrym 		return;
2462449e17fSsherrym 
247adc586deSMark Johnson 	ucode_free(id, ucodefp, sizeof (ucode_file_amd_t));
248adc586deSMark Johnson 	ufp->amd = NULL;
249adc586deSMark Johnson }
250adc586deSMark Johnson 
251adc586deSMark Johnson static void
252adc586deSMark Johnson ucode_file_reset_intel(ucode_file_t *ufp, processorid_t id)
253adc586deSMark Johnson {
254adc586deSMark Johnson 	ucode_file_intel_t *ucodefp = &ufp->intel;
255adc586deSMark Johnson 	int total_size, body_size;
256adc586deSMark Johnson 
257adc586deSMark Johnson 	if (ucodefp == NULL || ucodefp->uf_header == NULL)
258adc586deSMark Johnson 		return;
259adc586deSMark Johnson 
260adc586deSMark Johnson 	total_size = UCODE_TOTAL_SIZE_INTEL(ucodefp->uf_header->uh_total_size);
261adc586deSMark Johnson 	body_size = UCODE_BODY_SIZE_INTEL(ucodefp->uf_header->uh_body_size);
2622449e17fSsherrym 	if (ucodefp->uf_body) {
263adc586deSMark Johnson 		ucode_free(id, ucodefp->uf_body, body_size);
2642449e17fSsherrym 		ucodefp->uf_body = NULL;
2652449e17fSsherrym 	}
2662449e17fSsherrym 
2672449e17fSsherrym 	if (ucodefp->uf_ext_table) {
268adc586deSMark Johnson 		int size = total_size - body_size - UCODE_HEADER_SIZE_INTEL;
269adc586deSMark Johnson 
270adc586deSMark Johnson 		ucode_free(id, ucodefp->uf_ext_table, size);
2712449e17fSsherrym 		ucodefp->uf_ext_table = NULL;
2722449e17fSsherrym 	}
2732449e17fSsherrym 
274adc586deSMark Johnson 	ucode_free(id, ucodefp->uf_header, UCODE_HEADER_SIZE_INTEL);
275adc586deSMark Johnson 	ucodefp->uf_header = NULL;
276adc586deSMark Johnson }
277adc586deSMark Johnson 
278adc586deSMark Johnson /*
279adc586deSMark Johnson  * Find the equivalent CPU id in the equivalence table.
280adc586deSMark Johnson  */
281adc586deSMark Johnson static int
282*0ba6f73dSMark Johnson ucode_equiv_cpu_amd(cpu_t *cp, uint16_t *eq_sig)
283adc586deSMark Johnson {
284adc586deSMark Johnson 	char name[MAXPATHLEN];
285adc586deSMark Johnson 	intptr_t fd;
286adc586deSMark Johnson 	int count;
287adc586deSMark Johnson 	int offset = 0, cpi_sig = cpuid_getsig(cp);
288adc586deSMark Johnson 	ucode_eqtbl_amd_t *eqtbl = ucode_eqtbl_amd;
289adc586deSMark Johnson 
290adc586deSMark Johnson 	(void) snprintf(name, MAXPATHLEN, "/%s/%s/equivalence-table",
291adc586deSMark Johnson 	    UCODE_INSTALL_PATH, cpuid_getvendorstr(cp));
292adc586deSMark Johnson 
293adc586deSMark Johnson 	/*
294adc586deSMark Johnson 	 * No kmem_zalloc() etc. available on boot cpu.
295adc586deSMark Johnson 	 */
296adc586deSMark Johnson 	if (cp->cpu_id == 0) {
297adc586deSMark Johnson 		if ((fd = kobj_open(name)) == -1)
298adc586deSMark Johnson 			return (EM_OPENFILE);
299adc586deSMark Johnson 		/* ucode_zalloc() cannot fail on boot cpu */
300adc586deSMark Johnson 		eqtbl = ucode_zalloc(cp->cpu_id, sizeof (*eqtbl));
301adc586deSMark Johnson 		ASSERT(eqtbl);
302adc586deSMark Johnson 		do {
303adc586deSMark Johnson 			count = kobj_read(fd, (int8_t *)eqtbl,
304adc586deSMark Johnson 			    sizeof (*eqtbl), offset);
305adc586deSMark Johnson 			if (count != sizeof (*eqtbl)) {
306adc586deSMark Johnson 				(void) kobj_close(fd);
307adc586deSMark Johnson 				return (EM_HIGHERREV);
308adc586deSMark Johnson 			}
309adc586deSMark Johnson 			offset += count;
310adc586deSMark Johnson 		} while (eqtbl->ue_inst_cpu && eqtbl->ue_inst_cpu != cpi_sig);
311adc586deSMark Johnson 		(void) kobj_close(fd);
312adc586deSMark Johnson 	}
313adc586deSMark Johnson 
314adc586deSMark Johnson 	/*
315adc586deSMark Johnson 	 * If not already done, load the equivalence table.
316adc586deSMark Johnson 	 * Not done on boot CPU.
317adc586deSMark Johnson 	 */
318adc586deSMark Johnson 	if (eqtbl == NULL) {
319adc586deSMark Johnson 		struct _buf *eq;
320adc586deSMark Johnson 		uint64_t size;
321adc586deSMark Johnson 
322adc586deSMark Johnson 		if ((eq = kobj_open_file(name)) == (struct _buf *)-1)
323adc586deSMark Johnson 			return (EM_OPENFILE);
324adc586deSMark Johnson 
325adc586deSMark Johnson 		if (kobj_get_filesize(eq, &size) < 0) {
326adc586deSMark Johnson 			kobj_close_file(eq);
327adc586deSMark Johnson 			return (EM_OPENFILE);
328adc586deSMark Johnson 		}
329adc586deSMark Johnson 
330adc586deSMark Johnson 		ucode_eqtbl_amd = kmem_zalloc(size, KM_NOSLEEP);
331adc586deSMark Johnson 		if (ucode_eqtbl_amd == NULL) {
332adc586deSMark Johnson 			kobj_close_file(eq);
333adc586deSMark Johnson 			return (EM_NOMEM);
334adc586deSMark Johnson 		}
335adc586deSMark Johnson 
336adc586deSMark Johnson 		count = kobj_read_file(eq, (char *)ucode_eqtbl_amd, size, 0);
337adc586deSMark Johnson 		kobj_close_file(eq);
338adc586deSMark Johnson 
339adc586deSMark Johnson 		if (count != size)
340adc586deSMark Johnson 			return (EM_FILESIZE);
341adc586deSMark Johnson 	}
342adc586deSMark Johnson 
343adc586deSMark Johnson 	/* Get the equivalent CPU id. */
344adc586deSMark Johnson 	if (cp->cpu_id)
345adc586deSMark Johnson 		for (eqtbl = ucode_eqtbl_amd;
346adc586deSMark Johnson 		    eqtbl->ue_inst_cpu && eqtbl->ue_inst_cpu != cpi_sig;
347adc586deSMark Johnson 		    eqtbl++)
348adc586deSMark Johnson 			;
349adc586deSMark Johnson 
350adc586deSMark Johnson 	*eq_sig = eqtbl->ue_equiv_cpu;
351adc586deSMark Johnson 
352adc586deSMark Johnson 	/* No equivalent CPU id found, assume outdated microcode file. */
353adc586deSMark Johnson 	if (*eq_sig == 0)
354adc586deSMark Johnson 		return (EM_HIGHERREV);
355adc586deSMark Johnson 
356adc586deSMark Johnson 	return (EM_OK);
3572449e17fSsherrym }
3582449e17fSsherrym 
3592449e17fSsherrym /*
360*0ba6f73dSMark Johnson  * xVM cannot check for the presence of PCI devices. Look for chipset-
361*0ba6f73dSMark Johnson  * specific microcode patches in the container file and disable them
362*0ba6f73dSMark Johnson  * by setting their CPU revision to an invalid value.
363*0ba6f73dSMark Johnson  */
364*0ba6f73dSMark Johnson #ifdef __xpv
365*0ba6f73dSMark Johnson static void
366*0ba6f73dSMark Johnson ucode_chipset_amd(uint8_t *buf, int size)
367*0ba6f73dSMark Johnson {
368*0ba6f73dSMark Johnson 	ucode_header_amd_t *uh;
369*0ba6f73dSMark Johnson 	uint32_t *ptr = (uint32_t *)buf;
370*0ba6f73dSMark Johnson 	int len = 0;
371*0ba6f73dSMark Johnson 
372*0ba6f73dSMark Johnson 	/* skip to first microcode patch */
373*0ba6f73dSMark Johnson 	ptr += 2; len = *ptr++; ptr += len >> 2; size -= len;
374*0ba6f73dSMark Johnson 
375*0ba6f73dSMark Johnson 	while (size >= sizeof (ucode_header_amd_t) + 8) {
376*0ba6f73dSMark Johnson 		ptr++; len = *ptr++;
377*0ba6f73dSMark Johnson 		uh = (ucode_header_amd_t *)ptr;
378*0ba6f73dSMark Johnson 		ptr += len >> 2; size -= len;
379*0ba6f73dSMark Johnson 
380*0ba6f73dSMark Johnson 		if (uh->uh_nb_id) {
381*0ba6f73dSMark Johnson 			cmn_err(CE_WARN, "ignoring northbridge-specific ucode: "
382*0ba6f73dSMark Johnson 			    "chipset id %x, revision %x",
383*0ba6f73dSMark Johnson 			    uh->uh_nb_id, uh->uh_nb_rev);
384*0ba6f73dSMark Johnson 			uh->uh_cpu_rev = 0xffff;
385*0ba6f73dSMark Johnson 		}
386*0ba6f73dSMark Johnson 
387*0ba6f73dSMark Johnson 		if (uh->uh_sb_id) {
388*0ba6f73dSMark Johnson 			cmn_err(CE_WARN, "ignoring southbridge-specific ucode: "
389*0ba6f73dSMark Johnson 			    "chipset id %x, revision %x",
390*0ba6f73dSMark Johnson 			    uh->uh_sb_id, uh->uh_sb_rev);
391*0ba6f73dSMark Johnson 			uh->uh_cpu_rev = 0xffff;
392*0ba6f73dSMark Johnson 		}
393*0ba6f73dSMark Johnson 	}
394*0ba6f73dSMark Johnson }
395*0ba6f73dSMark Johnson #endif
396*0ba6f73dSMark Johnson 
397*0ba6f73dSMark Johnson /*
3982449e17fSsherrym  * Populate the ucode file structure from microcode file corresponding to
3992449e17fSsherrym  * this CPU, if exists.
4002449e17fSsherrym  *
4012449e17fSsherrym  * Return EM_OK on success, corresponding error code on failure.
4022449e17fSsherrym  */
403*0ba6f73dSMark Johnson /*ARGSUSED*/
4042449e17fSsherrym static ucode_errno_t
405adc586deSMark Johnson ucode_locate_amd(cpu_t *cp, cpu_ucode_info_t *uinfop, ucode_file_t *ufp)
406adc586deSMark Johnson {
407adc586deSMark Johnson 	char name[MAXPATHLEN];
408adc586deSMark Johnson 	intptr_t fd;
409*0ba6f73dSMark Johnson 	int count, rc;
410adc586deSMark Johnson 	ucode_file_amd_t *ucodefp = ufp->amd;
411adc586deSMark Johnson 
412*0ba6f73dSMark Johnson #ifndef __xpv
413*0ba6f73dSMark Johnson 	uint16_t eq_sig = 0;
414*0ba6f73dSMark Johnson 	int i;
415*0ba6f73dSMark Johnson 
416adc586deSMark Johnson 	/* get equivalent CPU id */
417adc586deSMark Johnson 	if ((rc = ucode_equiv_cpu_amd(cp, &eq_sig)) != EM_OK)
418adc586deSMark Johnson 		return (rc);
419adc586deSMark Johnson 
420adc586deSMark Johnson 	/*
421adc586deSMark Johnson 	 * Allocate a buffer for the microcode patch. If the buffer has been
422adc586deSMark Johnson 	 * allocated before, check for a matching microcode to avoid loading
423adc586deSMark Johnson 	 * the file again.
424adc586deSMark Johnson 	 */
425adc586deSMark Johnson 	if (ucodefp == NULL)
426adc586deSMark Johnson 		ucodefp = ucode_zalloc(cp->cpu_id, sizeof (*ucodefp));
427adc586deSMark Johnson 	else if (ucode_match_amd(eq_sig, uinfop, ucodefp, sizeof (*ucodefp))
428adc586deSMark Johnson 	    == EM_OK)
429adc586deSMark Johnson 		return (EM_OK);
430adc586deSMark Johnson 
431adc586deSMark Johnson 	if (ucodefp == NULL)
432adc586deSMark Johnson 		return (EM_NOMEM);
433adc586deSMark Johnson 
434adc586deSMark Johnson 	ufp->amd = ucodefp;
435adc586deSMark Johnson 
436adc586deSMark Johnson 	/*
437adc586deSMark Johnson 	 * Find the patch for this CPU. The patch files are named XXXX-YY, where
438adc586deSMark Johnson 	 * XXXX is the equivalent CPU id and YY is the running patch number.
439adc586deSMark Johnson 	 * Patches specific to certain chipsets are guaranteed to have lower
440adc586deSMark Johnson 	 * numbers than less specific patches, so we can just load the first
441adc586deSMark Johnson 	 * patch that matches.
442adc586deSMark Johnson 	 */
443adc586deSMark Johnson 
444adc586deSMark Johnson 	for (i = 0; i < 0xff; i++) {
445adc586deSMark Johnson 		(void) snprintf(name, MAXPATHLEN, "/%s/%s/%04X-%02X",
446adc586deSMark Johnson 		    UCODE_INSTALL_PATH, cpuid_getvendorstr(cp), eq_sig, i);
447adc586deSMark Johnson 		if ((fd = kobj_open(name)) == -1)
448adc586deSMark Johnson 			return (EM_NOMATCH);
449adc586deSMark Johnson 		count = kobj_read(fd, (char *)ucodefp, sizeof (*ucodefp), 0);
450adc586deSMark Johnson 		(void) kobj_close(fd);
451adc586deSMark Johnson 
452adc586deSMark Johnson 		if (ucode_match_amd(eq_sig, uinfop, ucodefp, count) == EM_OK)
453adc586deSMark Johnson 			return (EM_OK);
454adc586deSMark Johnson 	}
455adc586deSMark Johnson 	return (EM_NOMATCH);
456*0ba6f73dSMark Johnson #else
457*0ba6f73dSMark Johnson 	int size = 0;
458*0ba6f73dSMark Johnson 	char c;
459*0ba6f73dSMark Johnson 
460*0ba6f73dSMark Johnson 	/*
461*0ba6f73dSMark Johnson 	 * The xVM case is special. To support mixed-revision systems, the
462*0ba6f73dSMark Johnson 	 * hypervisor will choose which patch to load for which CPU, so the
463*0ba6f73dSMark Johnson 	 * whole microcode patch container file will have to be loaded.
464*0ba6f73dSMark Johnson 	 *
465*0ba6f73dSMark Johnson 	 * Since this code is only run on the boot cpu, we don't have to care
466*0ba6f73dSMark Johnson 	 * about failing ucode_zalloc() or freeing allocated memory.
467*0ba6f73dSMark Johnson 	 */
468*0ba6f73dSMark Johnson 	if (cp->cpu_id != 0)
469*0ba6f73dSMark Johnson 		return (EM_INVALIDARG);
470*0ba6f73dSMark Johnson 
471*0ba6f73dSMark Johnson 	(void) snprintf(name, MAXPATHLEN, "/%s/%s/container",
472*0ba6f73dSMark Johnson 	    UCODE_INSTALL_PATH, cpuid_getvendorstr(cp));
473*0ba6f73dSMark Johnson 
474*0ba6f73dSMark Johnson 	if ((fd = kobj_open(name)) == -1)
475*0ba6f73dSMark Johnson 		return (EM_OPENFILE);
476*0ba6f73dSMark Johnson 
477*0ba6f73dSMark Johnson 	/* get the file size by counting bytes */
478*0ba6f73dSMark Johnson 	do {
479*0ba6f73dSMark Johnson 		count = kobj_read(fd, &c, 1, size);
480*0ba6f73dSMark Johnson 		size += count;
481*0ba6f73dSMark Johnson 	} while (count);
482*0ba6f73dSMark Johnson 
483*0ba6f73dSMark Johnson 	ucodefp = ucode_zalloc(cp->cpu_id, sizeof (*ucodefp));
484*0ba6f73dSMark Johnson 	ASSERT(ucodefp);
485*0ba6f73dSMark Johnson 	ufp->amd = ucodefp;
486*0ba6f73dSMark Johnson 
487*0ba6f73dSMark Johnson 	ucodefp->usize = size;
488*0ba6f73dSMark Johnson 	ucodefp->ucodep = ucode_zalloc(cp->cpu_id, size);
489*0ba6f73dSMark Johnson 	ASSERT(ucodefp->ucodep);
490*0ba6f73dSMark Johnson 
491*0ba6f73dSMark Johnson 	/* load the microcode patch container file */
492*0ba6f73dSMark Johnson 	count = kobj_read(fd, (char *)ucodefp->ucodep, size, 0);
493*0ba6f73dSMark Johnson 	(void) kobj_close(fd);
494*0ba6f73dSMark Johnson 
495*0ba6f73dSMark Johnson 	if (count != size)
496*0ba6f73dSMark Johnson 		return (EM_FILESIZE);
497*0ba6f73dSMark Johnson 
498*0ba6f73dSMark Johnson 	/* make sure the container file is valid */
499*0ba6f73dSMark Johnson 	rc = ucode->validate(ucodefp->ucodep, ucodefp->usize);
500*0ba6f73dSMark Johnson 
501*0ba6f73dSMark Johnson 	if (rc != EM_OK)
502*0ba6f73dSMark Johnson 		return (rc);
503*0ba6f73dSMark Johnson 
504*0ba6f73dSMark Johnson 	/* disable chipset-specific patches */
505*0ba6f73dSMark Johnson 	ucode_chipset_amd(ucodefp->ucodep, ucodefp->usize);
506*0ba6f73dSMark Johnson 
507*0ba6f73dSMark Johnson 	return (EM_OK);
508*0ba6f73dSMark Johnson #endif
509adc586deSMark Johnson }
510adc586deSMark Johnson 
511adc586deSMark Johnson static ucode_errno_t
512adc586deSMark Johnson ucode_locate_intel(cpu_t *cp, cpu_ucode_info_t *uinfop, ucode_file_t *ufp)
5132449e17fSsherrym {
5142449e17fSsherrym 	char		name[MAXPATHLEN];
5152449e17fSsherrym 	intptr_t	fd;
5162449e17fSsherrym 	int		count;
517adc586deSMark Johnson 	int		header_size = UCODE_HEADER_SIZE_INTEL;
5182449e17fSsherrym 	int		cpi_sig = cpuid_getsig(cp);
5192449e17fSsherrym 	ucode_errno_t	rc = EM_OK;
520adc586deSMark Johnson 	ucode_file_intel_t *ucodefp = &ufp->intel;
521adc586deSMark Johnson 
522adc586deSMark Johnson 	ASSERT(ucode);
5232449e17fSsherrym 
5242449e17fSsherrym 	/*
5252449e17fSsherrym 	 * If the microcode matches the CPU we are processing, use it.
5262449e17fSsherrym 	 */
527adc586deSMark Johnson 	if (ucode_match_intel(cpi_sig, uinfop, ucodefp->uf_header,
5282449e17fSsherrym 	    ucodefp->uf_ext_table) == EM_OK && ucodefp->uf_body != NULL) {
5292449e17fSsherrym 		return (EM_OK);
5302449e17fSsherrym 	}
5312449e17fSsherrym 
5322449e17fSsherrym 	/*
5332449e17fSsherrym 	 * Look for microcode file with the right name.
5342449e17fSsherrym 	 */
5352449e17fSsherrym 	(void) snprintf(name, MAXPATHLEN, "/%s/%s/%08X-%02X",
5362449e17fSsherrym 	    UCODE_INSTALL_PATH, cpuid_getvendorstr(cp), cpi_sig,
5372449e17fSsherrym 	    uinfop->cui_platid);
5382449e17fSsherrym 	if ((fd = kobj_open(name)) == -1) {
5392449e17fSsherrym 		return (EM_OPENFILE);
5402449e17fSsherrym 	}
5412449e17fSsherrym 
5422449e17fSsherrym 	/*
5432449e17fSsherrym 	 * We found a microcode file for the CPU we are processing,
5442449e17fSsherrym 	 * reset the microcode data structure and read in the new
5452449e17fSsherrym 	 * file.
5462449e17fSsherrym 	 */
547adc586deSMark Johnson 	ucode->file_reset(ufp, cp->cpu_id);
5482449e17fSsherrym 
549adc586deSMark Johnson 	ucodefp->uf_header = ucode_zalloc(cp->cpu_id, header_size);
550adc586deSMark Johnson 	if (ucodefp->uf_header == NULL)
551adc586deSMark Johnson 		return (EM_NOMEM);
552adc586deSMark Johnson 
553adc586deSMark Johnson 	count = kobj_read(fd, (char *)ucodefp->uf_header, header_size, 0);
5542449e17fSsherrym 
5552449e17fSsherrym 	switch (count) {
556adc586deSMark Johnson 	case UCODE_HEADER_SIZE_INTEL: {
5572449e17fSsherrym 
558adc586deSMark Johnson 		ucode_header_intel_t	*uhp = ucodefp->uf_header;
5592449e17fSsherrym 		uint32_t	offset = header_size;
5602449e17fSsherrym 		int		total_size, body_size, ext_size;
5612449e17fSsherrym 		uint32_t	sum = 0;
5622449e17fSsherrym 
5632449e17fSsherrym 		/*
5642449e17fSsherrym 		 * Make sure that the header contains valid fields.
5652449e17fSsherrym 		 */
566adc586deSMark Johnson 		if ((rc = ucode_header_validate_intel(uhp)) == EM_OK) {
567adc586deSMark Johnson 			total_size = UCODE_TOTAL_SIZE_INTEL(uhp->uh_total_size);
568adc586deSMark Johnson 			body_size = UCODE_BODY_SIZE_INTEL(uhp->uh_body_size);
569adc586deSMark Johnson 			ucodefp->uf_body = ucode_zalloc(cp->cpu_id, body_size);
570adc586deSMark Johnson 			if (ucodefp->uf_body == NULL) {
5712449e17fSsherrym 				rc = EM_NOMEM;
5722449e17fSsherrym 				break;
5732449e17fSsherrym 			}
5742449e17fSsherrym 
5752449e17fSsherrym 			if (kobj_read(fd, (char *)ucodefp->uf_body,
5762449e17fSsherrym 			    body_size, offset) != body_size)
5772449e17fSsherrym 				rc = EM_FILESIZE;
5782449e17fSsherrym 		}
5792449e17fSsherrym 
5802449e17fSsherrym 		if (rc)
5812449e17fSsherrym 			break;
5822449e17fSsherrym 
583adc586deSMark Johnson 		sum = ucode_checksum_intel(0, header_size,
584adc586deSMark Johnson 		    (uint8_t *)ucodefp->uf_header);
585adc586deSMark Johnson 		if (ucode_checksum_intel(sum, body_size, ucodefp->uf_body)) {
5862449e17fSsherrym 			rc = EM_CHECKSUM;
5872449e17fSsherrym 			break;
5882449e17fSsherrym 		}
5892449e17fSsherrym 
5902449e17fSsherrym 		/*
5912449e17fSsherrym 		 * Check to see if there is extended signature table.
5922449e17fSsherrym 		 */
5932449e17fSsherrym 		offset = body_size + header_size;
5942449e17fSsherrym 		ext_size = total_size - offset;
5952449e17fSsherrym 
5962449e17fSsherrym 		if (ext_size <= 0)
5972449e17fSsherrym 			break;
5982449e17fSsherrym 
599adc586deSMark Johnson 		ucodefp->uf_ext_table = ucode_zalloc(cp->cpu_id, ext_size);
600adc586deSMark Johnson 		if (ucodefp->uf_ext_table == NULL) {
6012449e17fSsherrym 			rc = EM_NOMEM;
6022449e17fSsherrym 			break;
6032449e17fSsherrym 		}
6042449e17fSsherrym 
6052449e17fSsherrym 		if (kobj_read(fd, (char *)ucodefp->uf_ext_table,
6062449e17fSsherrym 		    ext_size, offset) != ext_size) {
6072449e17fSsherrym 			rc = EM_FILESIZE;
608adc586deSMark Johnson 		} else if (ucode_checksum_intel(0, ext_size,
6092449e17fSsherrym 		    (uint8_t *)(ucodefp->uf_ext_table))) {
6102449e17fSsherrym 			rc = EM_CHECKSUM;
6112449e17fSsherrym 		} else {
6122449e17fSsherrym 			int i;
6132449e17fSsherrym 
614adc586deSMark Johnson 			ext_size -= UCODE_EXT_TABLE_SIZE_INTEL;
6152449e17fSsherrym 			for (i = 0; i < ucodefp->uf_ext_table->uet_count;
6162449e17fSsherrym 			    i++) {
617adc586deSMark Johnson 				if (ucode_checksum_intel(0,
618adc586deSMark Johnson 				    UCODE_EXT_SIG_SIZE_INTEL,
6192449e17fSsherrym 				    (uint8_t *)(&(ucodefp->uf_ext_table->
6202449e17fSsherrym 				    uet_ext_sig[i])))) {
6212449e17fSsherrym 					rc = EM_CHECKSUM;
6222449e17fSsherrym 					break;
6232449e17fSsherrym 				}
6242449e17fSsherrym 			}
6252449e17fSsherrym 		}
6262449e17fSsherrym 		break;
6272449e17fSsherrym 	}
6282449e17fSsherrym 
6292449e17fSsherrym 	default:
6302449e17fSsherrym 		rc = EM_FILESIZE;
6312449e17fSsherrym 		break;
6322449e17fSsherrym 	}
6332449e17fSsherrym 
6342449e17fSsherrym 	kobj_close(fd);
6352449e17fSsherrym 
6362449e17fSsherrym 	if (rc != EM_OK)
6372449e17fSsherrym 		return (rc);
6382449e17fSsherrym 
639adc586deSMark Johnson 	rc = ucode_match_intel(cpi_sig, uinfop, ucodefp->uf_header,
6402449e17fSsherrym 	    ucodefp->uf_ext_table);
6412449e17fSsherrym 
6422449e17fSsherrym 	return (rc);
6432449e17fSsherrym }
6442449e17fSsherrym 
645*0ba6f73dSMark Johnson #ifndef __xpv
646adc586deSMark Johnson static ucode_errno_t
647*0ba6f73dSMark Johnson ucode_match_amd(uint16_t eq_sig, cpu_ucode_info_t *uinfop,
648*0ba6f73dSMark Johnson     ucode_file_amd_t *ucodefp, int size)
649adc586deSMark Johnson {
650adc586deSMark Johnson 	ucode_header_amd_t *uh;
651adc586deSMark Johnson 
652adc586deSMark Johnson 	if (ucodefp == NULL || size < sizeof (ucode_header_amd_t))
653adc586deSMark Johnson 		return (EM_NOMATCH);
654adc586deSMark Johnson 
655adc586deSMark Johnson 	/*
656adc586deSMark Johnson 	 * Don't even think about loading patches that would require code
657adc586deSMark Johnson 	 * execution.
658adc586deSMark Johnson 	 */
659adc586deSMark Johnson 	if (size > offsetof(ucode_file_amd_t, uf_code_present) &&
660adc586deSMark Johnson 	    ucodefp->uf_code_present)
661adc586deSMark Johnson 		return (EM_NOMATCH);
662adc586deSMark Johnson 
663adc586deSMark Johnson 	uh = &ucodefp->uf_header;
664adc586deSMark Johnson 
665adc586deSMark Johnson 	if (eq_sig != uh->uh_cpu_rev)
666adc586deSMark Johnson 		return (EM_NOMATCH);
667adc586deSMark Johnson 
668adc586deSMark Johnson 	if (uh->uh_nb_id) {
669adc586deSMark Johnson 		cmn_err(CE_WARN, "ignoring northbridge-specific ucode: "
670adc586deSMark Johnson 		    "chipset id %x, revision %x", uh->uh_nb_id, uh->uh_nb_rev);
671adc586deSMark Johnson 		return (EM_NOMATCH);
672adc586deSMark Johnson 	}
673adc586deSMark Johnson 
674adc586deSMark Johnson 	if (uh->uh_sb_id) {
675adc586deSMark Johnson 		cmn_err(CE_WARN, "ignoring southbridge-specific ucode: "
676adc586deSMark Johnson 		    "chipset id %x, revision %x", uh->uh_sb_id, uh->uh_sb_rev);
677adc586deSMark Johnson 		return (EM_NOMATCH);
678adc586deSMark Johnson 	}
679adc586deSMark Johnson 
680adc586deSMark Johnson 	if (uh->uh_patch_id <= uinfop->cui_rev)
681adc586deSMark Johnson 		return (EM_HIGHERREV);
682adc586deSMark Johnson 
683adc586deSMark Johnson 	return (EM_OK);
684adc586deSMark Johnson }
685*0ba6f73dSMark Johnson #endif
6862449e17fSsherrym 
6872449e17fSsherrym /*
6882449e17fSsherrym  * Returns 1 if the microcode is for this processor; 0 otherwise.
6892449e17fSsherrym  */
6902449e17fSsherrym static ucode_errno_t
691adc586deSMark Johnson ucode_match_intel(int cpi_sig, cpu_ucode_info_t *uinfop,
692adc586deSMark Johnson     ucode_header_intel_t *uhp, ucode_ext_table_intel_t *uetp)
6932449e17fSsherrym {
694adc586deSMark Johnson 	if (uhp == NULL)
695adc586deSMark Johnson 		return (EM_NOMATCH);
6962449e17fSsherrym 
697adc586deSMark Johnson 	if (UCODE_MATCH_INTEL(cpi_sig, uhp->uh_signature,
6982449e17fSsherrym 	    uinfop->cui_platid, uhp->uh_proc_flags)) {
6992449e17fSsherrym 
7002449e17fSsherrym 		if (uinfop->cui_rev >= uhp->uh_rev && !ucode_force_update)
7012449e17fSsherrym 			return (EM_HIGHERREV);
7022449e17fSsherrym 
7032449e17fSsherrym 		return (EM_OK);
7042449e17fSsherrym 	}
7052449e17fSsherrym 
7062449e17fSsherrym 	if (uetp != NULL) {
7072449e17fSsherrym 		int i;
7082449e17fSsherrym 
7092449e17fSsherrym 		for (i = 0; i < uetp->uet_count; i++) {
710adc586deSMark Johnson 			ucode_ext_sig_intel_t *uesp;
7112449e17fSsherrym 
7122449e17fSsherrym 			uesp = &uetp->uet_ext_sig[i];
7132449e17fSsherrym 
714adc586deSMark Johnson 			if (UCODE_MATCH_INTEL(cpi_sig, uesp->ues_signature,
7152449e17fSsherrym 			    uinfop->cui_platid, uesp->ues_proc_flags)) {
7162449e17fSsherrym 
7172449e17fSsherrym 				if (uinfop->cui_rev >= uhp->uh_rev &&
7182449e17fSsherrym 				    !ucode_force_update)
7192449e17fSsherrym 					return (EM_HIGHERREV);
7202449e17fSsherrym 
7212449e17fSsherrym 				return (EM_OK);
7222449e17fSsherrym 			}
7232449e17fSsherrym 		}
7242449e17fSsherrym 	}
7252449e17fSsherrym 
7262449e17fSsherrym 	return (EM_NOMATCH);
7272449e17fSsherrym }
7282449e17fSsherrym 
7292449e17fSsherrym /*ARGSUSED*/
7302449e17fSsherrym static int
7312449e17fSsherrym ucode_write(xc_arg_t arg1, xc_arg_t unused2, xc_arg_t unused3)
7322449e17fSsherrym {
733adc586deSMark Johnson 	ucode_update_t *uusp = (ucode_update_t *)arg1;
734adc586deSMark Johnson 	cpu_ucode_info_t *uinfop = CPU->cpu_m.mcpu_ucode_info;
7352449e17fSsherrym 
736adc586deSMark Johnson 	ASSERT(ucode);
7372449e17fSsherrym 	ASSERT(uusp->ucodep);
7382449e17fSsherrym 
739882a7af5SMark Johnson #ifndef	__xpv
7402449e17fSsherrym 	/*
7412449e17fSsherrym 	 * Check one more time to see if it is really necessary to update
7422449e17fSsherrym 	 * microcode just in case this is a hyperthreaded processor where
7432449e17fSsherrym 	 * the threads share the same microcode.
7442449e17fSsherrym 	 */
7452449e17fSsherrym 	if (!ucode_force_update) {
746adc586deSMark Johnson 		ucode->read_rev(uinfop);
7472449e17fSsherrym 		uusp->new_rev = uinfop->cui_rev;
7482449e17fSsherrym 		if (uinfop->cui_rev >= uusp->expected_rev)
7492449e17fSsherrym 			return (0);
7502449e17fSsherrym 	}
7512449e17fSsherrym 
752adc586deSMark Johnson 	wrmsr(ucode->write_msr, (uintptr_t)uusp->ucodep);
753882a7af5SMark Johnson #endif
754adc586deSMark Johnson 	ucode->read_rev(uinfop);
7552449e17fSsherrym 	uusp->new_rev = uinfop->cui_rev;
7562449e17fSsherrym 
7572449e17fSsherrym 	return (0);
7582449e17fSsherrym }
7592449e17fSsherrym 
760adc586deSMark Johnson /*ARGSUSED*/
761adc586deSMark Johnson static uint32_t
762adc586deSMark Johnson ucode_load_amd(ucode_file_t *ufp, cpu_ucode_info_t *uinfop, cpu_t *cp)
7632449e17fSsherrym {
764adc586deSMark Johnson 	ucode_file_amd_t *ucodefp = ufp->amd;
765adc586deSMark Johnson #ifdef	__xpv
766adc586deSMark Johnson 	ucode_update_t uus;
767adc586deSMark Johnson #endif
768adc586deSMark Johnson 
769adc586deSMark Johnson 	ASSERT(ucode);
770adc586deSMark Johnson 	ASSERT(ucodefp);
771adc586deSMark Johnson 
772adc586deSMark Johnson #ifndef	__xpv
7732449e17fSsherrym 	kpreempt_disable();
774adc586deSMark Johnson 	wrmsr(ucode->write_msr, (uintptr_t)ucodefp);
775adc586deSMark Johnson 	ucode->read_rev(uinfop);
7762449e17fSsherrym 	kpreempt_enable();
777*0ba6f73dSMark Johnson 
778*0ba6f73dSMark Johnson 	return (ucodefp->uf_header.uh_patch_id);
779adc586deSMark Johnson #else
780*0ba6f73dSMark Johnson 	uus.ucodep = ucodefp->ucodep;
781*0ba6f73dSMark Johnson 	uus.usize = ucodefp->usize;
782adc586deSMark Johnson 	ucode_load_xpv(&uus);
783adc586deSMark Johnson 	ucode->read_rev(uinfop);
784adc586deSMark Johnson 	uus.new_rev = uinfop->cui_rev;
785adc586deSMark Johnson 
786*0ba6f73dSMark Johnson 	return (uus.new_rev);
787*0ba6f73dSMark Johnson #endif
788adc586deSMark Johnson }
789adc586deSMark Johnson 
790adc586deSMark Johnson /*ARGSUSED2*/
791adc586deSMark Johnson static uint32_t
792adc586deSMark Johnson ucode_load_intel(ucode_file_t *ufp, cpu_ucode_info_t *uinfop, cpu_t *cp)
793adc586deSMark Johnson {
794adc586deSMark Johnson 	ucode_file_intel_t *ucodefp = &ufp->intel;
795adc586deSMark Johnson #ifdef __xpv
796adc586deSMark Johnson 	uint32_t ext_offset;
797adc586deSMark Johnson 	uint32_t body_size;
798adc586deSMark Johnson 	uint32_t ext_size;
799adc586deSMark Johnson 	uint8_t *ustart;
800adc586deSMark Johnson 	uint32_t usize;
801adc586deSMark Johnson 	ucode_update_t uus;
802adc586deSMark Johnson #endif
803adc586deSMark Johnson 
804adc586deSMark Johnson 	ASSERT(ucode);
805adc586deSMark Johnson 
806adc586deSMark Johnson #ifdef __xpv
807adc586deSMark Johnson 	/*
808adc586deSMark Johnson 	 * the hypervisor wants the header, data, and extended
809adc586deSMark Johnson 	 * signature tables. We can only get here from the boot
810adc586deSMark Johnson 	 * CPU (cpu #0), we don't need to free as ucode_zalloc() will
811adc586deSMark Johnson 	 * use BOP_ALLOC().
812adc586deSMark Johnson 	 */
813adc586deSMark Johnson 	usize = UCODE_TOTAL_SIZE_INTEL(ucodefp->uf_header->uh_total_size);
814adc586deSMark Johnson 	ustart = ucode_zalloc(cp->cpu_id, usize);
815adc586deSMark Johnson 	ASSERT(ustart);
816adc586deSMark Johnson 
817adc586deSMark Johnson 	body_size = UCODE_BODY_SIZE_INTEL(ucodefp->uf_header->uh_body_size);
818adc586deSMark Johnson 	ext_offset = body_size + UCODE_HEADER_SIZE_INTEL;
819adc586deSMark Johnson 	ext_size = usize - ext_offset;
820adc586deSMark Johnson 	ASSERT(ext_size >= 0);
821adc586deSMark Johnson 
822adc586deSMark Johnson 	(void) memcpy(ustart, ucodefp->uf_header, UCODE_HEADER_SIZE_INTEL);
823adc586deSMark Johnson 	(void) memcpy(&ustart[UCODE_HEADER_SIZE_INTEL], ucodefp->uf_body,
824adc586deSMark Johnson 	    body_size);
825adc586deSMark Johnson 	if (ext_size > 0) {
826adc586deSMark Johnson 		(void) memcpy(&ustart[ext_offset],
827adc586deSMark Johnson 		    ucodefp->uf_ext_table, ext_size);
828adc586deSMark Johnson 	}
829adc586deSMark Johnson 	uus.ucodep = ustart;
830adc586deSMark Johnson 	uus.usize = usize;
831adc586deSMark Johnson 	ucode_load_xpv(&uus);
832adc586deSMark Johnson 	ucode->read_rev(uinfop);
833adc586deSMark Johnson 	uus.new_rev = uinfop->cui_rev;
834adc586deSMark Johnson #else
835adc586deSMark Johnson 	kpreempt_disable();
836adc586deSMark Johnson 	wrmsr(ucode->write_msr, (uintptr_t)ucodefp->uf_body);
837adc586deSMark Johnson 	ucode->read_rev(uinfop);
838adc586deSMark Johnson 	kpreempt_enable();
839adc586deSMark Johnson #endif
840adc586deSMark Johnson 
841adc586deSMark Johnson 	return (ucodefp->uf_header->uh_rev);
8422449e17fSsherrym }
8432449e17fSsherrym 
844882a7af5SMark Johnson 
845882a7af5SMark Johnson #ifdef	__xpv
846882a7af5SMark Johnson static void
847adc586deSMark Johnson ucode_load_xpv(ucode_update_t *uusp)
848882a7af5SMark Johnson {
849882a7af5SMark Johnson 	xen_platform_op_t op;
850882a7af5SMark Johnson 	int e;
851882a7af5SMark Johnson 
852882a7af5SMark Johnson 	ASSERT(DOMAIN_IS_INITDOMAIN(xen_info));
853882a7af5SMark Johnson 
854882a7af5SMark Johnson 	kpreempt_disable();
855882a7af5SMark Johnson 	op.cmd = XENPF_microcode_update;
856882a7af5SMark Johnson 	op.interface_version = XENPF_INTERFACE_VERSION;
857882a7af5SMark Johnson 	/*LINTED: constant in conditional context*/
858adc586deSMark Johnson 	set_xen_guest_handle(op.u.microcode.data, uusp->ucodep);
859adc586deSMark Johnson 	op.u.microcode.length = uusp->usize;
860882a7af5SMark Johnson 	e = HYPERVISOR_platform_op(&op);
861882a7af5SMark Johnson 	if (e != 0) {
862882a7af5SMark Johnson 		cmn_err(CE_WARN, "hypervisor failed to accept uCode update");
863882a7af5SMark Johnson 	}
864882a7af5SMark Johnson 	kpreempt_enable();
865882a7af5SMark Johnson }
866882a7af5SMark Johnson #endif /* __xpv */
867882a7af5SMark Johnson 
868adc586deSMark Johnson static void
869adc586deSMark Johnson ucode_read_rev_amd(cpu_ucode_info_t *uinfop)
870adc586deSMark Johnson {
871adc586deSMark Johnson 	uinfop->cui_rev = rdmsr(MSR_AMD_PATCHLEVEL);
872adc586deSMark Johnson }
873882a7af5SMark Johnson 
8742449e17fSsherrym static void
875adc586deSMark Johnson ucode_read_rev_intel(cpu_ucode_info_t *uinfop)
8762449e17fSsherrym {
8772449e17fSsherrym 	struct cpuid_regs crs;
8782449e17fSsherrym 
8792449e17fSsherrym 	/*
8802449e17fSsherrym 	 * The Intel 64 and IA-32 Architecture Software Developer's Manual
8812449e17fSsherrym 	 * recommends that MSR_INTC_UCODE_REV be loaded with 0 first, then
8822449e17fSsherrym 	 * execute cpuid to guarantee the correct reading of this register.
8832449e17fSsherrym 	 */
8842449e17fSsherrym 	wrmsr(MSR_INTC_UCODE_REV, 0);
8852449e17fSsherrym 	(void) __cpuid_insn(&crs);
8862449e17fSsherrym 	uinfop->cui_rev = (rdmsr(MSR_INTC_UCODE_REV) >> INTC_UCODE_REV_SHIFT);
8872449e17fSsherrym }
8882449e17fSsherrym 
889adc586deSMark Johnson static ucode_errno_t
890adc586deSMark Johnson ucode_extract_amd(ucode_update_t *uusp, uint8_t *ucodep, int size)
891adc586deSMark Johnson {
892*0ba6f73dSMark Johnson #ifndef __xpv
893adc586deSMark Johnson 	uint32_t *ptr = (uint32_t *)ucodep;
894adc586deSMark Johnson 	ucode_eqtbl_amd_t *eqtbl;
895adc586deSMark Johnson 	ucode_file_amd_t *ufp;
896*0ba6f73dSMark Johnson 	int count;
897*0ba6f73dSMark Johnson 	int higher = 0;
898*0ba6f73dSMark Johnson 	ucode_errno_t rc = EM_NOMATCH;
899*0ba6f73dSMark Johnson 	uint16_t eq_sig;
900adc586deSMark Johnson 
901adc586deSMark Johnson 	/* skip over magic number & equivalence table header */
902adc586deSMark Johnson 	ptr += 2; size -= 8;
903adc586deSMark Johnson 
904adc586deSMark Johnson 	count = *ptr++; size -= 4;
905adc586deSMark Johnson 	for (eqtbl = (ucode_eqtbl_amd_t *)ptr;
906adc586deSMark Johnson 	    eqtbl->ue_inst_cpu && eqtbl->ue_inst_cpu != uusp->sig;
907adc586deSMark Johnson 	    eqtbl++)
908adc586deSMark Johnson 		;
909adc586deSMark Johnson 
910adc586deSMark Johnson 	eq_sig = eqtbl->ue_equiv_cpu;
911adc586deSMark Johnson 
912adc586deSMark Johnson 	/* No equivalent CPU id found, assume outdated microcode file. */
913adc586deSMark Johnson 	if (eq_sig == 0)
914adc586deSMark Johnson 		return (EM_HIGHERREV);
915adc586deSMark Johnson 
916adc586deSMark Johnson 	/* Use the first microcode patch that matches. */
917adc586deSMark Johnson 	do {
918adc586deSMark Johnson 		ptr += count >> 2; size -= count;
919adc586deSMark Johnson 
920adc586deSMark Johnson 		if (!size)
921*0ba6f73dSMark Johnson 			return (higher ? EM_HIGHERREV : EM_NOMATCH);
922adc586deSMark Johnson 
923adc586deSMark Johnson 		ptr++; size -= 4;
924adc586deSMark Johnson 		count = *ptr++; size -= 4;
925adc586deSMark Johnson 		ufp = (ucode_file_amd_t *)ptr;
926*0ba6f73dSMark Johnson 
927*0ba6f73dSMark Johnson 		rc = ucode_match_amd(eq_sig, &uusp->info, ufp, count);
928*0ba6f73dSMark Johnson 		if (rc == EM_HIGHERREV)
929*0ba6f73dSMark Johnson 			higher = 1;
930*0ba6f73dSMark Johnson 	} while (rc != EM_OK);
931adc586deSMark Johnson 
932adc586deSMark Johnson 	uusp->ucodep = (uint8_t *)ufp;
933adc586deSMark Johnson 	uusp->usize = count;
934adc586deSMark Johnson 	uusp->expected_rev = ufp->uf_header.uh_patch_id;
935*0ba6f73dSMark Johnson #else
936*0ba6f73dSMark Johnson 	/*
937*0ba6f73dSMark Johnson 	 * The hypervisor will choose the patch to load, so there is no way to
938*0ba6f73dSMark Johnson 	 * know the "expected revision" in advance. This is especially true on
939*0ba6f73dSMark Johnson 	 * mixed-revision systems where more than one patch will be loaded.
940*0ba6f73dSMark Johnson 	 */
941*0ba6f73dSMark Johnson 	uusp->expected_rev = 0;
942*0ba6f73dSMark Johnson 	uusp->ucodep = ucodep;
943*0ba6f73dSMark Johnson 	uusp->usize = size;
944*0ba6f73dSMark Johnson 
945*0ba6f73dSMark Johnson 	ucode_chipset_amd(ucodep, size);
946*0ba6f73dSMark Johnson #endif
947adc586deSMark Johnson 
948adc586deSMark Johnson 	return (EM_OK);
949adc586deSMark Johnson }
950adc586deSMark Johnson 
951adc586deSMark Johnson static ucode_errno_t
952adc586deSMark Johnson ucode_extract_intel(ucode_update_t *uusp, uint8_t *ucodep, int size)
953adc586deSMark Johnson {
954adc586deSMark Johnson 	uint32_t	header_size = UCODE_HEADER_SIZE_INTEL;
955adc586deSMark Johnson 	int		remaining;
956adc586deSMark Johnson 	int		found = 0;
957adc586deSMark Johnson 	ucode_errno_t	search_rc = EM_NOMATCH; /* search result */
958adc586deSMark Johnson 
959adc586deSMark Johnson 	/*
960adc586deSMark Johnson 	 * Go through the whole buffer in case there are
961adc586deSMark Johnson 	 * multiple versions of matching microcode for this
962adc586deSMark Johnson 	 * processor.
963adc586deSMark Johnson 	 */
964adc586deSMark Johnson 	for (remaining = size; remaining > 0; ) {
965adc586deSMark Johnson 		int	total_size, body_size, ext_size;
966adc586deSMark Johnson 		uint8_t	*curbuf = &ucodep[size - remaining];
967adc586deSMark Johnson 		ucode_header_intel_t *uhp = (ucode_header_intel_t *)curbuf;
968adc586deSMark Johnson 		ucode_ext_table_intel_t *uetp = NULL;
969adc586deSMark Johnson 		ucode_errno_t tmprc;
970adc586deSMark Johnson 
971adc586deSMark Johnson 		total_size = UCODE_TOTAL_SIZE_INTEL(uhp->uh_total_size);
972adc586deSMark Johnson 		body_size = UCODE_BODY_SIZE_INTEL(uhp->uh_body_size);
973adc586deSMark Johnson 		ext_size = total_size - (header_size + body_size);
974adc586deSMark Johnson 
975adc586deSMark Johnson 		if (ext_size > 0)
976adc586deSMark Johnson 			uetp = (ucode_ext_table_intel_t *)
977adc586deSMark Johnson 			    &curbuf[header_size + body_size];
978adc586deSMark Johnson 
979adc586deSMark Johnson 		tmprc = ucode_match_intel(uusp->sig, &uusp->info, uhp, uetp);
980adc586deSMark Johnson 
981adc586deSMark Johnson 		/*
982adc586deSMark Johnson 		 * Since we are searching through a big file
983adc586deSMark Johnson 		 * containing microcode for pretty much all the
984adc586deSMark Johnson 		 * processors, we are bound to get EM_NOMATCH
985adc586deSMark Johnson 		 * at one point.  However, if we return
986adc586deSMark Johnson 		 * EM_NOMATCH to users, it will really confuse
987adc586deSMark Johnson 		 * them.  Therefore, if we ever find a match of
988adc586deSMark Johnson 		 * a lower rev, we will set return code to
989adc586deSMark Johnson 		 * EM_HIGHERREV.
990adc586deSMark Johnson 		 */
991adc586deSMark Johnson 		if (tmprc == EM_HIGHERREV)
992adc586deSMark Johnson 			search_rc = EM_HIGHERREV;
993adc586deSMark Johnson 
994adc586deSMark Johnson 		if (tmprc == EM_OK &&
995adc586deSMark Johnson 		    uusp->expected_rev < uhp->uh_rev) {
996adc586deSMark Johnson #ifndef __xpv
997adc586deSMark Johnson 			uusp->ucodep = (uint8_t *)&curbuf[header_size];
998adc586deSMark Johnson #else
999adc586deSMark Johnson 			uusp->ucodep = (uint8_t *)curbuf;
1000adc586deSMark Johnson #endif
1001adc586deSMark Johnson 			uusp->usize =
1002adc586deSMark Johnson 			    UCODE_TOTAL_SIZE_INTEL(uhp->uh_total_size);
1003adc586deSMark Johnson 			uusp->expected_rev = uhp->uh_rev;
1004adc586deSMark Johnson 			found = 1;
1005adc586deSMark Johnson 		}
1006adc586deSMark Johnson 
1007adc586deSMark Johnson 		remaining -= total_size;
1008adc586deSMark Johnson 	}
1009adc586deSMark Johnson 
1010adc586deSMark Johnson 	if (!found)
1011adc586deSMark Johnson 		return (search_rc);
1012adc586deSMark Johnson 
1013adc586deSMark Johnson 	return (EM_OK);
1014adc586deSMark Johnson }
10152449e17fSsherrym /*
10162449e17fSsherrym  * Entry point to microcode update from the ucode_drv driver.
10172449e17fSsherrym  *
10182449e17fSsherrym  * Returns EM_OK on success, corresponding error code on failure.
10192449e17fSsherrym  */
10202449e17fSsherrym ucode_errno_t
10212449e17fSsherrym ucode_update(uint8_t *ucodep, int size)
10222449e17fSsherrym {
10232449e17fSsherrym 	int		found = 0;
10242449e17fSsherrym 	processorid_t	id;
1025adc586deSMark Johnson 	ucode_update_t	cached = { 0 };
1026adc586deSMark Johnson 	ucode_update_t	*cachedp = NULL;
10272449e17fSsherrym 	ucode_errno_t	rc = EM_OK;
10282449e17fSsherrym 	ucode_errno_t	search_rc = EM_NOMATCH; /* search result */
10292449e17fSsherrym 	cpuset_t cpuset;
10302449e17fSsherrym 
1031adc586deSMark Johnson 	ASSERT(ucode);
10322449e17fSsherrym 	ASSERT(ucodep);
10332449e17fSsherrym 	CPUSET_ZERO(cpuset);
10342449e17fSsherrym 
1035adc586deSMark Johnson 	if (!ucode->capable(CPU))
10362449e17fSsherrym 		return (EM_NOTSUP);
10372449e17fSsherrym 
10382449e17fSsherrym 	mutex_enter(&cpu_lock);
10392449e17fSsherrym 
10402449e17fSsherrym 	for (id = 0; id < max_ncpus; id++) {
10412449e17fSsherrym 		cpu_t *cpu;
1042adc586deSMark Johnson 		ucode_update_t uus = { 0 };
1043adc586deSMark Johnson 		ucode_update_t *uusp = &uus;
10442449e17fSsherrym 
10452449e17fSsherrym 		/*
10462449e17fSsherrym 		 * If there is no such CPU or it is not xcall ready, skip it.
10472449e17fSsherrym 		 */
10482449e17fSsherrym 		if ((cpu = cpu_get(id)) == NULL ||
10492449e17fSsherrym 		    !(cpu->cpu_flags & CPU_READY))
10502449e17fSsherrym 			continue;
10512449e17fSsherrym 
10522449e17fSsherrym 		uusp->sig = cpuid_getsig(cpu);
10532449e17fSsherrym 		bcopy(cpu->cpu_m.mcpu_ucode_info, &uusp->info,
10542449e17fSsherrym 		    sizeof (uusp->info));
10552449e17fSsherrym 
10562449e17fSsherrym 		/*
10572449e17fSsherrym 		 * If the current CPU has the same signature and platform
10582449e17fSsherrym 		 * id as the previous one we processed, reuse the information.
10592449e17fSsherrym 		 */
10602449e17fSsherrym 		if (cachedp && cachedp->sig == cpuid_getsig(cpu) &&
10612449e17fSsherrym 		    cachedp->info.cui_platid == uusp->info.cui_platid) {
10622449e17fSsherrym 			uusp->ucodep = cachedp->ucodep;
10632449e17fSsherrym 			uusp->expected_rev = cachedp->expected_rev;
10642449e17fSsherrym 			/*
10652449e17fSsherrym 			 * Intuitively we should check here to see whether the
10662449e17fSsherrym 			 * running microcode rev is >= the expected rev, and
10672449e17fSsherrym 			 * quit if it is.  But we choose to proceed with the
10682449e17fSsherrym 			 * xcall regardless of the running version so that
10692449e17fSsherrym 			 * the other threads in an HT processor can update
10702449e17fSsherrym 			 * the cpu_ucode_info structure in machcpu.
10712449e17fSsherrym 			 */
1072adc586deSMark Johnson 		} else if ((search_rc = ucode->extract(uusp, ucodep, size))
1073adc586deSMark Johnson 		    == EM_OK) {
10742449e17fSsherrym 			bcopy(uusp, &cached, sizeof (cached));
10752449e17fSsherrym 			cachedp = &cached;
10762449e17fSsherrym 			found = 1;
10772449e17fSsherrym 		}
10782449e17fSsherrym 
10792449e17fSsherrym 		/* Nothing to do */
10802449e17fSsherrym 		if (uusp->ucodep == NULL)
10812449e17fSsherrym 			continue;
10822449e17fSsherrym 
1083882a7af5SMark Johnson #ifdef	__xpv
1084882a7af5SMark Johnson 		/*
1085882a7af5SMark Johnson 		 * for i86xpv, the hypervisor will update all the CPUs.
1086882a7af5SMark Johnson 		 * the hypervisor wants the header, data, and extended
1087882a7af5SMark Johnson 		 * signature tables. ucode_write will just read in the
1088882a7af5SMark Johnson 		 * updated version on all the CPUs after the update has
1089882a7af5SMark Johnson 		 * completed.
1090882a7af5SMark Johnson 		 */
1091c9b5d7d2SMark Johnson 		if (id == 0) {
1092adc586deSMark Johnson 			ucode_load_xpv(uusp);
1093c9b5d7d2SMark Johnson 		}
1094882a7af5SMark Johnson #endif
1095882a7af5SMark Johnson 
10962449e17fSsherrym 		CPUSET_ADD(cpuset, id);
10972449e17fSsherrym 		kpreempt_disable();
10982449e17fSsherrym 		xc_sync((xc_arg_t)uusp, 0, 0, X_CALL_HIPRI, cpuset,
10992449e17fSsherrym 		    ucode_write);
11002449e17fSsherrym 		kpreempt_enable();
11012449e17fSsherrym 		CPUSET_DEL(cpuset, id);
11022449e17fSsherrym 
1103*0ba6f73dSMark Johnson 		if (uusp->new_rev != 0 && uusp->info.cui_rev == uusp->new_rev) {
1104*0ba6f73dSMark Johnson 			rc = EM_HIGHERREV;
1105*0ba6f73dSMark Johnson 		} else if ((uusp->new_rev == 0) || (uusp->expected_rev != 0 &&
1106*0ba6f73dSMark Johnson 		    uusp->expected_rev != uusp->new_rev)) {
11072449e17fSsherrym 			cmn_err(CE_WARN, ucode_failure_fmt,
11082449e17fSsherrym 			    id, uusp->info.cui_rev, uusp->expected_rev);
11092449e17fSsherrym 			rc = EM_UPDATE;
1110*0ba6f73dSMark Johnson 		} else {
1111*0ba6f73dSMark Johnson 			cmn_err(CE_CONT, ucode_success_fmt,
1112*0ba6f73dSMark Johnson 			    id, uusp->info.cui_rev, uusp->new_rev);
11132449e17fSsherrym 		}
11142449e17fSsherrym 	}
11152449e17fSsherrym 
11162449e17fSsherrym 	mutex_exit(&cpu_lock);
11172449e17fSsherrym 
11182449e17fSsherrym 	if (!found)
11192449e17fSsherrym 		rc = search_rc;
11202449e17fSsherrym 
11212449e17fSsherrym 	return (rc);
11222449e17fSsherrym }
11232449e17fSsherrym 
11242449e17fSsherrym /*
11252449e17fSsherrym  * Initialize mcpu_ucode_info, and perform microcode update if necessary.
11262449e17fSsherrym  * This is the entry point from boot path where pointer to CPU structure
11272449e17fSsherrym  * is available.
11282449e17fSsherrym  *
11292449e17fSsherrym  * cpuid_info must be initialized before ucode_check can be called.
11302449e17fSsherrym  */
11312449e17fSsherrym void
11322449e17fSsherrym ucode_check(cpu_t *cp)
11332449e17fSsherrym {
1134adc586deSMark Johnson 	cpu_ucode_info_t *uinfop;
11352449e17fSsherrym 	ucode_errno_t rc = EM_OK;
1136adc586deSMark Johnson 	uint32_t new_rev = 0;
11372449e17fSsherrym 
11382449e17fSsherrym 	ASSERT(cp);
11392449e17fSsherrym 	if (cp->cpu_id == 0)
11402449e17fSsherrym 		cp->cpu_m.mcpu_ucode_info = &cpu_ucode_info0;
11412449e17fSsherrym 
11422449e17fSsherrym 	uinfop = cp->cpu_m.mcpu_ucode_info;
11432449e17fSsherrym 	ASSERT(uinfop);
11442449e17fSsherrym 
1145adc586deSMark Johnson 	/* set up function pointers if not already done */
1146adc586deSMark Johnson 	if (!ucode)
1147adc586deSMark Johnson 		switch (cpuid_getvendor(cp)) {
1148adc586deSMark Johnson 		case X86_VENDOR_AMD:
1149adc586deSMark Johnson 			ucode = &ucode_amd;
1150adc586deSMark Johnson 			break;
1151adc586deSMark Johnson 		case X86_VENDOR_Intel:
1152adc586deSMark Johnson 			ucode = &ucode_intel;
1153adc586deSMark Johnson 			break;
1154adc586deSMark Johnson 		default:
1155adc586deSMark Johnson 			return;
1156adc586deSMark Johnson 		}
1157adc586deSMark Johnson 
1158adc586deSMark Johnson 	if (!ucode->capable(cp))
11592449e17fSsherrym 		return;
11602449e17fSsherrym 
11612449e17fSsherrym 	/*
11622449e17fSsherrym 	 * The MSR_INTC_PLATFORM_ID is supported in Celeron and Xeon
11632449e17fSsherrym 	 * (Family 6, model 5 and above) and all processors after.
11642449e17fSsherrym 	 */
1165adc586deSMark Johnson 	if ((cpuid_getvendor(cp) == X86_VENDOR_Intel) &&
1166adc586deSMark Johnson 	    ((cpuid_getmodel(cp) >= 5) || (cpuid_getfamily(cp) > 6))) {
11672449e17fSsherrym 		uinfop->cui_platid = 1 << ((rdmsr(MSR_INTC_PLATFORM_ID) >>
11682449e17fSsherrym 		    INTC_PLATFORM_ID_SHIFT) & INTC_PLATFORM_ID_MASK);
11692449e17fSsherrym 	}
11702449e17fSsherrym 
1171adc586deSMark Johnson 	ucode->read_rev(uinfop);
11722449e17fSsherrym 
1173c9b5d7d2SMark Johnson #ifdef	__xpv
1174c9b5d7d2SMark Johnson 	/*
1175c9b5d7d2SMark Johnson 	 * for i86xpv, the hypervisor will update all the CPUs. We only need
1176c9b5d7d2SMark Johnson 	 * do do this on one of the CPUs (and there always is a CPU 0).
1177c9b5d7d2SMark Johnson 	 */
1178c9b5d7d2SMark Johnson 	if (cp->cpu_id != 0) {
1179c9b5d7d2SMark Johnson 		return;
1180c9b5d7d2SMark Johnson 	}
1181c9b5d7d2SMark Johnson #endif
1182c9b5d7d2SMark Johnson 
11832449e17fSsherrym 	/*
11842449e17fSsherrym 	 * Check to see if we need ucode update
11852449e17fSsherrym 	 */
1186adc586deSMark Johnson 	if ((rc = ucode->locate(cp, uinfop, &ucodefile)) == EM_OK) {
1187adc586deSMark Johnson 		new_rev = ucode->load(&ucodefile, uinfop, cp);
1188882a7af5SMark Johnson 
1189adc586deSMark Johnson 		if (uinfop->cui_rev != new_rev)
11902449e17fSsherrym 			cmn_err(CE_WARN, ucode_failure_fmt, cp->cpu_id,
1191adc586deSMark Johnson 			    uinfop->cui_rev, new_rev);
11922449e17fSsherrym 	}
11932449e17fSsherrym 
11942449e17fSsherrym 	/*
11952449e17fSsherrym 	 * If we fail to find a match for any reason, free the file structure
11962449e17fSsherrym 	 * just in case we have read in a partial file.
11972449e17fSsherrym 	 *
11982449e17fSsherrym 	 * Since the scratch memory for holding the microcode for the boot CPU
11992449e17fSsherrym 	 * came from BOP_ALLOC, we will reset the data structure as if we
12002449e17fSsherrym 	 * never did the allocation so we don't have to keep track of this
12012449e17fSsherrym 	 * special chunk of memory.  We free the memory used for the rest
12022449e17fSsherrym 	 * of the CPUs in start_other_cpus().
12032449e17fSsherrym 	 */
12042449e17fSsherrym 	if (rc != EM_OK || cp->cpu_id == 0)
1205adc586deSMark Johnson 		ucode->file_reset(&ucodefile, cp->cpu_id);
12062449e17fSsherrym }
12072449e17fSsherrym 
12082449e17fSsherrym /*
12092449e17fSsherrym  * Returns microcode revision from the machcpu structure.
12102449e17fSsherrym  */
12112449e17fSsherrym ucode_errno_t
12122449e17fSsherrym ucode_get_rev(uint32_t *revp)
12132449e17fSsherrym {
12142449e17fSsherrym 	int i;
12152449e17fSsherrym 
1216adc586deSMark Johnson 	ASSERT(ucode);
12172449e17fSsherrym 	ASSERT(revp);
12182449e17fSsherrym 
1219adc586deSMark Johnson 	if (!ucode->capable(CPU))
12202449e17fSsherrym 		return (EM_NOTSUP);
12212449e17fSsherrym 
12222449e17fSsherrym 	mutex_enter(&cpu_lock);
12232449e17fSsherrym 	for (i = 0; i < max_ncpus; i++) {
12242449e17fSsherrym 		cpu_t *cpu;
12252449e17fSsherrym 
12262449e17fSsherrym 		if ((cpu = cpu_get(i)) == NULL)
12272449e17fSsherrym 			continue;
12282449e17fSsherrym 
12292449e17fSsherrym 		revp[i] = cpu->cpu_m.mcpu_ucode_info->cui_rev;
12302449e17fSsherrym 	}
12312449e17fSsherrym 	mutex_exit(&cpu_lock);
12322449e17fSsherrym 
12332449e17fSsherrym 	return (EM_OK);
12342449e17fSsherrym }
1235