4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright 2012 Nexenta Systems, Inc. All rights reserved.
27 * Copyright (c) 2018, Joyent, Inc.
30 #include <sys/asm_linkage.h>
31 #include <sys/bootconf.h>
32 #include <sys/cpuvar.h>
33 #include <sys/cmn_err.h>
34 #include <sys/controlregs.h>
35 #include <sys/debug.h>
37 #include <sys/kobj_impl.h>
38 #include <sys/machsystm.h>
39 #include <sys/ontrap.h>
40 #include <sys/param.h>
41 #include <sys/machparam.h>
42 #include <sys/promif.h>
43 #include <sys/sysmacros.h>
44 #include <sys/systm.h>
45 #include <sys/types.h>
46 #include <sys/thread.h>
47 #include <sys/ucode.h>
48 #include <sys/x86_archext.h>
49 #include <sys/x_call.h>
51 #include <sys/hypervisor.h>
55 * AMD-specific equivalence table
57 static ucode_eqtbl_amd_t
*ucode_eqtbl_amd
;
60 * mcpu_ucode_info for the boot CPU. Statically allocated.
62 static struct cpu_ucode_info cpu_ucode_info0
;
64 static ucode_file_t ucodefile
;
66 static void* ucode_zalloc(processorid_t
, size_t);
67 static void ucode_free(processorid_t
, void *, size_t);
69 static int ucode_capable_amd(cpu_t
*);
70 static int ucode_capable_intel(cpu_t
*);
72 static ucode_errno_t
ucode_extract_amd(ucode_update_t
*, uint8_t *, int);
73 static ucode_errno_t
ucode_extract_intel(ucode_update_t
*, uint8_t *,
76 static void ucode_file_reset_amd(ucode_file_t
*, processorid_t
);
77 static void ucode_file_reset_intel(ucode_file_t
*, processorid_t
);
79 static uint32_t ucode_load_amd(ucode_file_t
*, cpu_ucode_info_t
*, cpu_t
*);
80 static uint32_t ucode_load_intel(ucode_file_t
*, cpu_ucode_info_t
*, cpu_t
*);
83 static void ucode_load_xpv(ucode_update_t
*);
84 static void ucode_chipset_amd(uint8_t *, int);
87 static int ucode_equiv_cpu_amd(cpu_t
*, uint16_t *);
89 static ucode_errno_t
ucode_locate_amd(cpu_t
*, cpu_ucode_info_t
*,
91 static ucode_errno_t
ucode_locate_intel(cpu_t
*, cpu_ucode_info_t
*,
95 static ucode_errno_t
ucode_match_amd(uint16_t, cpu_ucode_info_t
*,
96 ucode_file_amd_t
*, int);
98 static ucode_errno_t
ucode_match_intel(int, cpu_ucode_info_t
*,
99 ucode_header_intel_t
*, ucode_ext_table_intel_t
*);
101 static void ucode_read_rev_amd(cpu_ucode_info_t
*);
102 static void ucode_read_rev_intel(cpu_ucode_info_t
*);
104 static const struct ucode_ops ucode_amd
= {
107 ucode_file_reset_amd
,
115 static const struct ucode_ops ucode_intel
= {
116 MSR_INTC_UCODE_WRITE
,
118 ucode_file_reset_intel
,
119 ucode_read_rev_intel
,
121 ucode_validate_intel
,
126 const struct ucode_ops
*ucode
;
128 static const char ucode_failure_fmt
[] =
129 "cpu%d: failed to update microcode from version 0x%x to 0x%x\n";
130 static const char ucode_success_fmt
[] =
131 "?cpu%d: microcode has been updated from version 0x%x to 0x%x\n";
134 * Force flag. If set, the first microcode binary that matches
135 * signature and platform id will be used for microcode update,
136 * regardless of version. Should only be used for debugging.
138 int ucode_force_update
= 0;
141 * Allocate space for mcpu_ucode_info in the machcpu structure
142 * for all non-boot CPUs.
145 ucode_alloc_space(cpu_t
*cp
)
147 ASSERT(cp
->cpu_id
!= 0);
148 ASSERT(cp
->cpu_m
.mcpu_ucode_info
== NULL
);
149 cp
->cpu_m
.mcpu_ucode_info
=
150 kmem_zalloc(sizeof (*cp
->cpu_m
.mcpu_ucode_info
), KM_SLEEP
);
154 ucode_free_space(cpu_t
*cp
)
156 ASSERT(cp
->cpu_m
.mcpu_ucode_info
!= NULL
);
157 ASSERT(cp
->cpu_m
.mcpu_ucode_info
!= &cpu_ucode_info0
);
158 kmem_free(cp
->cpu_m
.mcpu_ucode_info
,
159 sizeof (*cp
->cpu_m
.mcpu_ucode_info
));
160 cp
->cpu_m
.mcpu_ucode_info
= NULL
;
164 * Called when we are done with microcode update on all processors to free up
165 * space allocated for the microcode file.
173 ucode
->file_reset(&ucodefile
, -1);
177 * Allocate/free a buffer used to hold ucode data. Space for the boot CPU is
178 * allocated with BOP_ALLOC() and does not require a free.
181 ucode_zalloc(processorid_t id
, size_t size
)
184 return (kmem_zalloc(size
, KM_NOSLEEP
));
186 /* BOP_ALLOC() failure results in panic */
187 return (BOP_ALLOC(bootops
, NULL
, size
, MMU_PAGESIZE
));
191 ucode_free(processorid_t id
, void* buf
, size_t size
)
194 kmem_free(buf
, size
);
198 * Check whether or not a processor is capable of microcode operations
199 * Returns 1 if it is capable, 0 if not.
201 * At this point we only support microcode update for:
202 * - Intel processors family 6 and above, and
203 * - AMD processors family 0x10 and above.
205 * We also assume that we don't support a mix of Intel and
206 * AMD processors in the same box.
208 * An i86xpv guest domain or VM can't update the microcode.
211 #define XPVDOMU_OR_HVM \
212 ((hwenv == HW_XEN_PV && !is_controldom()) || (hwenv & HW_VIRTUAL) != 0)
216 ucode_capable_amd(cpu_t
*cp
)
218 int hwenv
= get_hwenv();
223 return (cpuid_getfamily(cp
) >= 0x10);
227 ucode_capable_intel(cpu_t
*cp
)
229 int hwenv
= get_hwenv();
234 return (cpuid_getfamily(cp
) >= 6);
238 * Called when it is no longer necessary to keep the microcode around,
239 * or when the cached microcode doesn't match the CPU being processed.
242 ucode_file_reset_amd(ucode_file_t
*ufp
, processorid_t id
)
244 ucode_file_amd_t
*ucodefp
= ufp
->amd
;
249 ucode_free(id
, ucodefp
, sizeof (ucode_file_amd_t
));
254 ucode_file_reset_intel(ucode_file_t
*ufp
, processorid_t id
)
256 ucode_file_intel_t
*ucodefp
= &ufp
->intel
;
257 int total_size
, body_size
;
259 if (ucodefp
== NULL
|| ucodefp
->uf_header
== NULL
)
262 total_size
= UCODE_TOTAL_SIZE_INTEL(ucodefp
->uf_header
->uh_total_size
);
263 body_size
= UCODE_BODY_SIZE_INTEL(ucodefp
->uf_header
->uh_body_size
);
264 if (ucodefp
->uf_body
) {
265 ucode_free(id
, ucodefp
->uf_body
, body_size
);
266 ucodefp
->uf_body
= NULL
;
269 if (ucodefp
->uf_ext_table
) {
270 int size
= total_size
- body_size
- UCODE_HEADER_SIZE_INTEL
;
272 ucode_free(id
, ucodefp
->uf_ext_table
, size
);
273 ucodefp
->uf_ext_table
= NULL
;
276 ucode_free(id
, ucodefp
->uf_header
, UCODE_HEADER_SIZE_INTEL
);
277 ucodefp
->uf_header
= NULL
;
281 * Find the equivalent CPU id in the equivalence table.
284 ucode_equiv_cpu_amd(cpu_t
*cp
, uint16_t *eq_sig
)
286 char name
[MAXPATHLEN
];
289 int offset
= 0, cpi_sig
= cpuid_getsig(cp
);
290 ucode_eqtbl_amd_t
*eqtbl
= ucode_eqtbl_amd
;
292 (void) snprintf(name
, MAXPATHLEN
, "/%s/%s/equivalence-table",
293 UCODE_INSTALL_PATH
, cpuid_getvendorstr(cp
));
296 * No kmem_zalloc() etc. available on boot cpu.
298 if (cp
->cpu_id
== 0) {
299 if ((fd
= kobj_open(name
)) == -1)
300 return (EM_OPENFILE
);
301 /* ucode_zalloc() cannot fail on boot cpu */
302 eqtbl
= ucode_zalloc(cp
->cpu_id
, sizeof (*eqtbl
));
305 count
= kobj_read(fd
, (int8_t *)eqtbl
,
306 sizeof (*eqtbl
), offset
);
307 if (count
!= sizeof (*eqtbl
)) {
308 (void) kobj_close(fd
);
309 return (EM_HIGHERREV
);
312 } while (eqtbl
->ue_inst_cpu
&& eqtbl
->ue_inst_cpu
!= cpi_sig
);
313 (void) kobj_close(fd
);
317 * If not already done, load the equivalence table.
318 * Not done on boot CPU.
324 if ((eq
= kobj_open_file(name
)) == (struct _buf
*)-1)
325 return (EM_OPENFILE
);
327 if (kobj_get_filesize(eq
, &size
) < 0) {
329 return (EM_OPENFILE
);
332 ucode_eqtbl_amd
= kmem_zalloc(size
, KM_NOSLEEP
);
333 if (ucode_eqtbl_amd
== NULL
) {
338 count
= kobj_read_file(eq
, (char *)ucode_eqtbl_amd
, size
, 0);
342 return (EM_FILESIZE
);
345 /* Get the equivalent CPU id. */
347 for (eqtbl
= ucode_eqtbl_amd
;
348 eqtbl
->ue_inst_cpu
&& eqtbl
->ue_inst_cpu
!= cpi_sig
;
352 *eq_sig
= eqtbl
->ue_equiv_cpu
;
354 /* No equivalent CPU id found, assume outdated microcode file. */
356 return (EM_HIGHERREV
);
362 * xVM cannot check for the presence of PCI devices. Look for chipset-
363 * specific microcode patches in the container file and disable them
364 * by setting their CPU revision to an invalid value.
368 ucode_chipset_amd(uint8_t *buf
, int size
)
370 ucode_header_amd_t
*uh
;
371 uint32_t *ptr
= (uint32_t *)buf
;
374 /* skip to first microcode patch */
375 ptr
+= 2; len
= *ptr
++; ptr
+= len
>> 2; size
-= len
;
377 while (size
>= sizeof (ucode_header_amd_t
) + 8) {
379 uh
= (ucode_header_amd_t
*)ptr
;
380 ptr
+= len
>> 2; size
-= len
;
383 cmn_err(CE_WARN
, "ignoring northbridge-specific ucode: "
384 "chipset id %x, revision %x",
385 uh
->uh_nb_id
, uh
->uh_nb_rev
);
386 uh
->uh_cpu_rev
= 0xffff;
390 cmn_err(CE_WARN
, "ignoring southbridge-specific ucode: "
391 "chipset id %x, revision %x",
392 uh
->uh_sb_id
, uh
->uh_sb_rev
);
393 uh
->uh_cpu_rev
= 0xffff;
400 * Populate the ucode file structure from microcode file corresponding to
401 * this CPU, if exists.
403 * Return EM_OK on success, corresponding error code on failure.
407 ucode_locate_amd(cpu_t
*cp
, cpu_ucode_info_t
*uinfop
, ucode_file_t
*ufp
)
409 char name
[MAXPATHLEN
];
412 ucode_file_amd_t
*ucodefp
= ufp
->amd
;
418 /* get equivalent CPU id */
419 if ((rc
= ucode_equiv_cpu_amd(cp
, &eq_sig
)) != EM_OK
)
423 * Allocate a buffer for the microcode patch. If the buffer has been
424 * allocated before, check for a matching microcode to avoid loading
428 ucodefp
= ucode_zalloc(cp
->cpu_id
, sizeof (*ucodefp
));
429 else if (ucode_match_amd(eq_sig
, uinfop
, ucodefp
, sizeof (*ucodefp
))
439 * Find the patch for this CPU. The patch files are named XXXX-YY, where
440 * XXXX is the equivalent CPU id and YY is the running patch number.
441 * Patches specific to certain chipsets are guaranteed to have lower
442 * numbers than less specific patches, so we can just load the first
443 * patch that matches.
446 for (i
= 0; i
< 0xff; i
++) {
447 (void) snprintf(name
, MAXPATHLEN
, "/%s/%s/%04X-%02X",
448 UCODE_INSTALL_PATH
, cpuid_getvendorstr(cp
), eq_sig
, i
);
449 if ((fd
= kobj_open(name
)) == -1)
451 count
= kobj_read(fd
, (char *)ucodefp
, sizeof (*ucodefp
), 0);
452 (void) kobj_close(fd
);
454 if (ucode_match_amd(eq_sig
, uinfop
, ucodefp
, count
) == EM_OK
)
463 * The xVM case is special. To support mixed-revision systems, the
464 * hypervisor will choose which patch to load for which CPU, so the
465 * whole microcode patch container file will have to be loaded.
467 * Since this code is only run on the boot cpu, we don't have to care
468 * about failing ucode_zalloc() or freeing allocated memory.
471 return (EM_INVALIDARG
);
473 (void) snprintf(name
, MAXPATHLEN
, "/%s/%s/container",
474 UCODE_INSTALL_PATH
, cpuid_getvendorstr(cp
));
476 if ((fd
= kobj_open(name
)) == -1)
477 return (EM_OPENFILE
);
479 /* get the file size by counting bytes */
481 count
= kobj_read(fd
, &c
, 1, size
);
485 ucodefp
= ucode_zalloc(cp
->cpu_id
, sizeof (*ucodefp
));
489 ucodefp
->usize
= size
;
490 ucodefp
->ucodep
= ucode_zalloc(cp
->cpu_id
, size
);
491 ASSERT(ucodefp
->ucodep
);
493 /* load the microcode patch container file */
494 count
= kobj_read(fd
, (char *)ucodefp
->ucodep
, size
, 0);
495 (void) kobj_close(fd
);
498 return (EM_FILESIZE
);
500 /* make sure the container file is valid */
501 rc
= ucode
->validate(ucodefp
->ucodep
, ucodefp
->usize
);
506 /* disable chipset-specific patches */
507 ucode_chipset_amd(ucodefp
->ucodep
, ucodefp
->usize
);
514 ucode_locate_intel(cpu_t
*cp
, cpu_ucode_info_t
*uinfop
, ucode_file_t
*ufp
)
516 char name
[MAXPATHLEN
];
519 int header_size
= UCODE_HEADER_SIZE_INTEL
;
520 int cpi_sig
= cpuid_getsig(cp
);
521 ucode_errno_t rc
= EM_OK
;
522 ucode_file_intel_t
*ucodefp
= &ufp
->intel
;
527 * If the microcode matches the CPU we are processing, use it.
529 if (ucode_match_intel(cpi_sig
, uinfop
, ucodefp
->uf_header
,
530 ucodefp
->uf_ext_table
) == EM_OK
&& ucodefp
->uf_body
!= NULL
) {
535 * Look for microcode file with the right name.
537 (void) snprintf(name
, MAXPATHLEN
, "/%s/%s/%08X-%02X",
538 UCODE_INSTALL_PATH
, cpuid_getvendorstr(cp
), cpi_sig
,
540 if ((fd
= kobj_open(name
)) == -1) {
541 return (EM_OPENFILE
);
545 * We found a microcode file for the CPU we are processing,
546 * reset the microcode data structure and read in the new
549 ucode
->file_reset(ufp
, cp
->cpu_id
);
551 ucodefp
->uf_header
= ucode_zalloc(cp
->cpu_id
, header_size
);
552 if (ucodefp
->uf_header
== NULL
)
555 count
= kobj_read(fd
, (char *)ucodefp
->uf_header
, header_size
, 0);
558 case UCODE_HEADER_SIZE_INTEL
: {
560 ucode_header_intel_t
*uhp
= ucodefp
->uf_header
;
561 uint32_t offset
= header_size
;
562 int total_size
, body_size
, ext_size
;
566 * Make sure that the header contains valid fields.
568 if ((rc
= ucode_header_validate_intel(uhp
)) == EM_OK
) {
569 total_size
= UCODE_TOTAL_SIZE_INTEL(uhp
->uh_total_size
);
570 body_size
= UCODE_BODY_SIZE_INTEL(uhp
->uh_body_size
);
571 ucodefp
->uf_body
= ucode_zalloc(cp
->cpu_id
, body_size
);
572 if (ucodefp
->uf_body
== NULL
) {
577 if (kobj_read(fd
, (char *)ucodefp
->uf_body
,
578 body_size
, offset
) != body_size
)
585 sum
= ucode_checksum_intel(0, header_size
,
586 (uint8_t *)ucodefp
->uf_header
);
587 if (ucode_checksum_intel(sum
, body_size
, ucodefp
->uf_body
)) {
593 * Check to see if there is extended signature table.
595 offset
= body_size
+ header_size
;
596 ext_size
= total_size
- offset
;
601 ucodefp
->uf_ext_table
= ucode_zalloc(cp
->cpu_id
, ext_size
);
602 if (ucodefp
->uf_ext_table
== NULL
) {
607 if (kobj_read(fd
, (char *)ucodefp
->uf_ext_table
,
608 ext_size
, offset
) != ext_size
) {
610 } else if (ucode_checksum_intel(0, ext_size
,
611 (uint8_t *)(ucodefp
->uf_ext_table
))) {
616 ext_size
-= UCODE_EXT_TABLE_SIZE_INTEL
;
617 for (i
= 0; i
< ucodefp
->uf_ext_table
->uet_count
;
619 if (ucode_checksum_intel(0,
620 UCODE_EXT_SIG_SIZE_INTEL
,
621 (uint8_t *)(&(ucodefp
->uf_ext_table
->
641 rc
= ucode_match_intel(cpi_sig
, uinfop
, ucodefp
->uf_header
,
642 ucodefp
->uf_ext_table
);
649 ucode_match_amd(uint16_t eq_sig
, cpu_ucode_info_t
*uinfop
,
650 ucode_file_amd_t
*ucodefp
, int size
)
652 ucode_header_amd_t
*uh
;
654 if (ucodefp
== NULL
|| size
< sizeof (ucode_header_amd_t
))
657 uh
= &ucodefp
->uf_header
;
660 * Don't even think about loading patches that would require code
661 * execution. Does not apply to patches for family 0x14 and beyond.
663 if (uh
->uh_cpu_rev
< 0x5000 &&
664 size
> offsetof(ucode_file_amd_t
, uf_code_present
) &&
665 ucodefp
->uf_code_present
)
668 if (eq_sig
!= uh
->uh_cpu_rev
)
672 cmn_err(CE_WARN
, "ignoring northbridge-specific ucode: "
673 "chipset id %x, revision %x", uh
->uh_nb_id
, uh
->uh_nb_rev
);
678 cmn_err(CE_WARN
, "ignoring southbridge-specific ucode: "
679 "chipset id %x, revision %x", uh
->uh_sb_id
, uh
->uh_sb_rev
);
683 if (uh
->uh_patch_id
<= uinfop
->cui_rev
&& !ucode_force_update
)
684 return (EM_HIGHERREV
);
691 * Returns 1 if the microcode is for this processor; 0 otherwise.
694 ucode_match_intel(int cpi_sig
, cpu_ucode_info_t
*uinfop
,
695 ucode_header_intel_t
*uhp
, ucode_ext_table_intel_t
*uetp
)
700 if (UCODE_MATCH_INTEL(cpi_sig
, uhp
->uh_signature
,
701 uinfop
->cui_platid
, uhp
->uh_proc_flags
)) {
703 if (uinfop
->cui_rev
>= uhp
->uh_rev
&& !ucode_force_update
)
704 return (EM_HIGHERREV
);
712 for (i
= 0; i
< uetp
->uet_count
; i
++) {
713 ucode_ext_sig_intel_t
*uesp
;
715 uesp
= &uetp
->uet_ext_sig
[i
];
717 if (UCODE_MATCH_INTEL(cpi_sig
, uesp
->ues_signature
,
718 uinfop
->cui_platid
, uesp
->ues_proc_flags
)) {
720 if (uinfop
->cui_rev
>= uhp
->uh_rev
&&
722 return (EM_HIGHERREV
);
734 ucode_write(xc_arg_t arg1
, xc_arg_t unused2
, xc_arg_t unused3
)
736 ucode_update_t
*uusp
= (ucode_update_t
*)arg1
;
737 cpu_ucode_info_t
*uinfop
= CPU
->cpu_m
.mcpu_ucode_info
;
743 ASSERT(uusp
->ucodep
);
747 * Check one more time to see if it is really necessary to update
748 * microcode just in case this is a hyperthreaded processor where
749 * the threads share the same microcode.
751 if (!ucode_force_update
) {
752 ucode
->read_rev(uinfop
);
753 uusp
->new_rev
= uinfop
->cui_rev
;
754 if (uinfop
->cui_rev
>= uusp
->expected_rev
)
758 if (!on_trap(&otd
, OT_DATA_ACCESS
)) {
760 * On some platforms a cache invalidation is required for the
761 * ucode update to be successful due to the parts of the
762 * processor that the microcode is updating.
765 wrmsr(ucode
->write_msr
, (uintptr_t)uusp
->ucodep
);
770 ucode
->read_rev(uinfop
);
771 uusp
->new_rev
= uinfop
->cui_rev
;
778 ucode_load_amd(ucode_file_t
*ufp
, cpu_ucode_info_t
*uinfop
, cpu_t
*cp
)
780 ucode_file_amd_t
*ucodefp
= ufp
->amd
;
792 if (on_trap(&otd
, OT_DATA_ACCESS
)) {
797 wrmsr(ucode
->write_msr
, (uintptr_t)ucodefp
);
799 ucode
->read_rev(uinfop
);
802 return (ucodefp
->uf_header
.uh_patch_id
);
804 uus
.ucodep
= ucodefp
->ucodep
;
805 uus
.usize
= ucodefp
->usize
;
806 ucode_load_xpv(&uus
);
807 ucode
->read_rev(uinfop
);
808 uus
.new_rev
= uinfop
->cui_rev
;
810 return (uus
.new_rev
);
816 ucode_load_intel(ucode_file_t
*ufp
, cpu_ucode_info_t
*uinfop
, cpu_t
*cp
)
818 ucode_file_intel_t
*ucodefp
= &ufp
->intel
;
832 * the hypervisor wants the header, data, and extended
833 * signature tables. We can only get here from the boot
834 * CPU (cpu #0), we don't need to free as ucode_zalloc() will
837 usize
= UCODE_TOTAL_SIZE_INTEL(ucodefp
->uf_header
->uh_total_size
);
838 ustart
= ucode_zalloc(cp
->cpu_id
, usize
);
841 body_size
= UCODE_BODY_SIZE_INTEL(ucodefp
->uf_header
->uh_body_size
);
842 ext_offset
= body_size
+ UCODE_HEADER_SIZE_INTEL
;
843 ext_size
= usize
- ext_offset
;
844 ASSERT(ext_size
>= 0);
846 (void) memcpy(ustart
, ucodefp
->uf_header
, UCODE_HEADER_SIZE_INTEL
);
847 (void) memcpy(&ustart
[UCODE_HEADER_SIZE_INTEL
], ucodefp
->uf_body
,
850 (void) memcpy(&ustart
[ext_offset
],
851 ucodefp
->uf_ext_table
, ext_size
);
855 ucode_load_xpv(&uus
);
856 ucode
->read_rev(uinfop
);
857 uus
.new_rev
= uinfop
->cui_rev
;
861 * On some platforms a cache invalidation is required for the
862 * ucode update to be successful due to the parts of the
863 * processor that the microcode is updating.
866 wrmsr(ucode
->write_msr
, (uintptr_t)ucodefp
->uf_body
);
867 ucode
->read_rev(uinfop
);
871 return (ucodefp
->uf_header
->uh_rev
);
877 ucode_load_xpv(ucode_update_t
*uusp
)
879 xen_platform_op_t op
;
882 ASSERT(DOMAIN_IS_INITDOMAIN(xen_info
));
885 op
.cmd
= XENPF_microcode_update
;
886 op
.interface_version
= XENPF_INTERFACE_VERSION
;
887 /*LINTED: constant in conditional context*/
888 set_xen_guest_handle(op
.u
.microcode
.data
, uusp
->ucodep
);
889 op
.u
.microcode
.length
= uusp
->usize
;
890 e
= HYPERVISOR_platform_op(&op
);
892 cmn_err(CE_WARN
, "hypervisor failed to accept uCode update");
899 ucode_read_rev_amd(cpu_ucode_info_t
*uinfop
)
901 uinfop
->cui_rev
= rdmsr(MSR_AMD_PATCHLEVEL
);
905 ucode_read_rev_intel(cpu_ucode_info_t
*uinfop
)
907 struct cpuid_regs crs
;
910 * The Intel 64 and IA-32 Architecture Software Developer's Manual
911 * recommends that MSR_INTC_UCODE_REV be loaded with 0 first, then
912 * execute cpuid to guarantee the correct reading of this register.
914 wrmsr(MSR_INTC_UCODE_REV
, 0);
915 (void) __cpuid_insn(&crs
);
916 uinfop
->cui_rev
= (rdmsr(MSR_INTC_UCODE_REV
) >> INTC_UCODE_REV_SHIFT
);
920 ucode_extract_amd(ucode_update_t
*uusp
, uint8_t *ucodep
, int size
)
923 uint32_t *ptr
= (uint32_t *)ucodep
;
924 ucode_eqtbl_amd_t
*eqtbl
;
925 ucode_file_amd_t
*ufp
;
928 ucode_errno_t rc
= EM_NOMATCH
;
931 /* skip over magic number & equivalence table header */
934 count
= *ptr
++; size
-= 4;
935 for (eqtbl
= (ucode_eqtbl_amd_t
*)ptr
;
936 eqtbl
->ue_inst_cpu
&& eqtbl
->ue_inst_cpu
!= uusp
->sig
;
940 eq_sig
= eqtbl
->ue_equiv_cpu
;
942 /* No equivalent CPU id found, assume outdated microcode file. */
944 return (EM_HIGHERREV
);
946 /* Use the first microcode patch that matches. */
948 ptr
+= count
>> 2; size
-= count
;
951 return (higher
? EM_HIGHERREV
: EM_NOMATCH
);
954 count
= *ptr
++; size
-= 4;
955 ufp
= (ucode_file_amd_t
*)ptr
;
957 rc
= ucode_match_amd(eq_sig
, &uusp
->info
, ufp
, count
);
958 if (rc
== EM_HIGHERREV
)
960 } while (rc
!= EM_OK
);
962 uusp
->ucodep
= (uint8_t *)ufp
;
964 uusp
->expected_rev
= ufp
->uf_header
.uh_patch_id
;
967 * The hypervisor will choose the patch to load, so there is no way to
968 * know the "expected revision" in advance. This is especially true on
969 * mixed-revision systems where more than one patch will be loaded.
971 uusp
->expected_rev
= 0;
972 uusp
->ucodep
= ucodep
;
975 ucode_chipset_amd(ucodep
, size
);
982 ucode_extract_intel(ucode_update_t
*uusp
, uint8_t *ucodep
, int size
)
984 uint32_t header_size
= UCODE_HEADER_SIZE_INTEL
;
987 ucode_errno_t search_rc
= EM_NOMATCH
; /* search result */
990 * Go through the whole buffer in case there are
991 * multiple versions of matching microcode for this
994 for (remaining
= size
; remaining
> 0; ) {
995 int total_size
, body_size
, ext_size
;
996 uint8_t *curbuf
= &ucodep
[size
- remaining
];
997 ucode_header_intel_t
*uhp
= (ucode_header_intel_t
*)curbuf
;
998 ucode_ext_table_intel_t
*uetp
= NULL
;
1001 total_size
= UCODE_TOTAL_SIZE_INTEL(uhp
->uh_total_size
);
1002 body_size
= UCODE_BODY_SIZE_INTEL(uhp
->uh_body_size
);
1003 ext_size
= total_size
- (header_size
+ body_size
);
1006 uetp
= (ucode_ext_table_intel_t
*)
1007 &curbuf
[header_size
+ body_size
];
1009 tmprc
= ucode_match_intel(uusp
->sig
, &uusp
->info
, uhp
, uetp
);
1012 * Since we are searching through a big file
1013 * containing microcode for pretty much all the
1014 * processors, we are bound to get EM_NOMATCH
1015 * at one point. However, if we return
1016 * EM_NOMATCH to users, it will really confuse
1017 * them. Therefore, if we ever find a match of
1018 * a lower rev, we will set return code to
1021 if (tmprc
== EM_HIGHERREV
)
1022 search_rc
= EM_HIGHERREV
;
1024 if (tmprc
== EM_OK
&&
1025 uusp
->expected_rev
< uhp
->uh_rev
) {
1027 uusp
->ucodep
= (uint8_t *)&curbuf
[header_size
];
1029 uusp
->ucodep
= (uint8_t *)curbuf
;
1032 UCODE_TOTAL_SIZE_INTEL(uhp
->uh_total_size
);
1033 uusp
->expected_rev
= uhp
->uh_rev
;
1037 remaining
-= total_size
;
1046 * Entry point to microcode update from the ucode_drv driver.
1048 * Returns EM_OK on success, corresponding error code on failure.
1051 ucode_update(uint8_t *ucodep
, int size
)
1055 ucode_update_t cached
= { 0 };
1056 ucode_update_t
*cachedp
= NULL
;
1057 ucode_errno_t rc
= EM_OK
;
1058 ucode_errno_t search_rc
= EM_NOMATCH
; /* search result */
1063 CPUSET_ZERO(cpuset
);
1065 if (!ucode
->capable(CPU
))
1068 mutex_enter(&cpu_lock
);
1070 for (id
= 0; id
< max_ncpus
; id
++) {
1072 ucode_update_t uus
= { 0 };
1073 ucode_update_t
*uusp
= &uus
;
1076 * If there is no such CPU or it is not xcall ready, skip it.
1078 if ((cpu
= cpu_get(id
)) == NULL
||
1079 !(cpu
->cpu_flags
& CPU_READY
))
1082 uusp
->sig
= cpuid_getsig(cpu
);
1083 bcopy(cpu
->cpu_m
.mcpu_ucode_info
, &uusp
->info
,
1084 sizeof (uusp
->info
));
1087 * If the current CPU has the same signature and platform
1088 * id as the previous one we processed, reuse the information.
1090 if (cachedp
&& cachedp
->sig
== cpuid_getsig(cpu
) &&
1091 cachedp
->info
.cui_platid
== uusp
->info
.cui_platid
) {
1092 uusp
->ucodep
= cachedp
->ucodep
;
1093 uusp
->expected_rev
= cachedp
->expected_rev
;
1095 * Intuitively we should check here to see whether the
1096 * running microcode rev is >= the expected rev, and
1097 * quit if it is. But we choose to proceed with the
1098 * xcall regardless of the running version so that
1099 * the other threads in an HT processor can update
1100 * the cpu_ucode_info structure in machcpu.
1102 } else if ((search_rc
= ucode
->extract(uusp
, ucodep
, size
))
1104 bcopy(uusp
, &cached
, sizeof (cached
));
1110 if (uusp
->ucodep
== NULL
)
1115 * for i86xpv, the hypervisor will update all the CPUs.
1116 * the hypervisor wants the header, data, and extended
1117 * signature tables. ucode_write will just read in the
1118 * updated version on all the CPUs after the update has
1122 ucode_load_xpv(uusp
);
1126 CPUSET_ADD(cpuset
, id
);
1128 xc_sync((xc_arg_t
)uusp
, 0, 0, CPUSET2BV(cpuset
), ucode_write
);
1130 CPUSET_DEL(cpuset
, id
);
1132 if (uusp
->new_rev
!= 0 && uusp
->info
.cui_rev
== uusp
->new_rev
&&
1133 !ucode_force_update
) {
1135 } else if ((uusp
->new_rev
== 0) || (uusp
->expected_rev
!= 0 &&
1136 uusp
->expected_rev
!= uusp
->new_rev
)) {
1137 cmn_err(CE_WARN
, ucode_failure_fmt
,
1138 id
, uusp
->info
.cui_rev
, uusp
->expected_rev
);
1141 cmn_err(CE_CONT
, ucode_success_fmt
,
1142 id
, uusp
->info
.cui_rev
, uusp
->new_rev
);
1146 mutex_exit(&cpu_lock
);
1155 * Initialize mcpu_ucode_info, and perform microcode update if necessary.
1156 * This is the entry point from boot path where pointer to CPU structure
1159 * cpuid_info must be initialized before ucode_check can be called.
1162 ucode_check(cpu_t
*cp
)
1164 cpu_ucode_info_t
*uinfop
;
1165 ucode_errno_t rc
= EM_OK
;
1166 uint32_t new_rev
= 0;
1170 * Space statically allocated for BSP, ensure pointer is set
1172 if (cp
->cpu_id
== 0 && cp
->cpu_m
.mcpu_ucode_info
== NULL
)
1173 cp
->cpu_m
.mcpu_ucode_info
= &cpu_ucode_info0
;
1175 uinfop
= cp
->cpu_m
.mcpu_ucode_info
;
1178 /* set up function pointers if not already done */
1180 switch (cpuid_getvendor(cp
)) {
1181 case X86_VENDOR_AMD
:
1184 case X86_VENDOR_Intel
:
1185 ucode
= &ucode_intel
;
1192 if (!ucode
->capable(cp
))
1196 * The MSR_INTC_PLATFORM_ID is supported in Celeron and Xeon
1197 * (Family 6, model 5 and above) and all processors after.
1199 if ((cpuid_getvendor(cp
) == X86_VENDOR_Intel
) &&
1200 ((cpuid_getmodel(cp
) >= 5) || (cpuid_getfamily(cp
) > 6))) {
1201 uinfop
->cui_platid
= 1 << ((rdmsr(MSR_INTC_PLATFORM_ID
) >>
1202 INTC_PLATFORM_ID_SHIFT
) & INTC_PLATFORM_ID_MASK
);
1205 ucode
->read_rev(uinfop
);
1209 * for i86xpv, the hypervisor will update all the CPUs. We only need
1210 * do do this on one of the CPUs (and there always is a CPU 0).
1212 if (cp
->cpu_id
!= 0) {
1218 * Check to see if we need ucode update
1220 if ((rc
= ucode
->locate(cp
, uinfop
, &ucodefile
)) == EM_OK
) {
1221 new_rev
= ucode
->load(&ucodefile
, uinfop
, cp
);
1223 if (uinfop
->cui_rev
!= new_rev
)
1224 cmn_err(CE_WARN
, ucode_failure_fmt
, cp
->cpu_id
,
1225 uinfop
->cui_rev
, new_rev
);
1229 * If we fail to find a match for any reason, free the file structure
1230 * just in case we have read in a partial file.
1232 * Since the scratch memory for holding the microcode for the boot CPU
1233 * came from BOP_ALLOC, we will reset the data structure as if we
1234 * never did the allocation so we don't have to keep track of this
1235 * special chunk of memory. We free the memory used for the rest
1236 * of the CPUs in start_other_cpus().
1238 if (rc
!= EM_OK
|| cp
->cpu_id
== 0)
1239 ucode
->file_reset(&ucodefile
, cp
->cpu_id
);
1243 * Returns microcode revision from the machcpu structure.
1246 ucode_get_rev(uint32_t *revp
)
1253 if (!ucode
->capable(CPU
))
1256 mutex_enter(&cpu_lock
);
1257 for (i
= 0; i
< max_ncpus
; i
++) {
1260 if ((cpu
= cpu_get(i
)) == NULL
)
1263 revp
[i
] = cpu
->cpu_m
.mcpu_ucode_info
->cui_rev
;
1265 mutex_exit(&cpu_lock
);