74 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
78 #include <linux/kernel.h>
79 #include <linux/module.h>
83 #include <asm/processor.h>
122 #define DEFAULT_UCODE_DATASIZE (2000)
123 #define MC_HEADER_SIZE (sizeof(struct microcode_header_intel))
124 #define DEFAULT_UCODE_TOTALSIZE (DEFAULT_UCODE_DATASIZE + MC_HEADER_SIZE)
125 #define EXT_HEADER_SIZE (sizeof(struct extended_sigtable))
126 #define EXT_SIGNATURE_SIZE (sizeof(struct extended_signature))
127 #define DWSIZE (sizeof(u32))
129 #define get_totalsize(mc) \
130 (((struct microcode_intel *)mc)->hdr.totalsize ? \
131 ((struct microcode_intel *)mc)->hdr.totalsize : \
132 DEFAULT_UCODE_TOTALSIZE)
134 #define get_datasize(mc) \
135 (((struct microcode_intel *)mc)->hdr.datasize ? \
136 ((struct microcode_intel *)mc)->hdr.datasize : DEFAULT_UCODE_DATASIZE)
138 #define sigmatch(s1, s2, p1, p2) \
139 (((s1) == (s2)) && (((p1) & (p2)) || (((p1) == 0) && ((p2) == 0))))
141 #define exttable_size(et) ((et)->count * EXT_SIGNATURE_SIZE + EXT_HEADER_SIZE)
143 static int collect_cpu_info(
int cpu_num,
struct cpu_signature *csig)
148 memset(csig, 0,
sizeof(*csig));
150 csig->
sig = cpuid_eax(0x00000001);
155 csig->
pf = 1 << ((val[1] >> 18) & 7);
159 pr_info(
"CPU%d sig=0x%x, pf=0x%x, revision=0x%x\n",
160 cpu_num, csig->
sig, csig->
pf, csig->
rev);
173 return (mc_header->
rev <= rev) ? 0 : 1;
176 static int microcode_sanity_check(
void *
mc)
181 int sum, orig_sum, ext_sigcount = 0,
i;
188 pr_err(
"error! Bad data size in microcode data file\n");
193 pr_err(
"error! Unknown microcode update format\n");
197 if (ext_table_size) {
200 pr_err(
"error! Small exttable size in microcode data file\n");
205 pr_err(
"error! Bad exttable size in microcode data file\n");
208 ext_sigcount = ext_header->
count;
212 if (ext_table_size) {
213 int ext_table_sum = 0;
214 int *ext_tablep = (
int *)ext_header;
218 ext_table_sum += ext_tablep[
i];
220 pr_warning(
"aborting, bad extended signature table checksum\n");
229 orig_sum += ((
int *)mc)[
i];
231 pr_err(
"aborting, bad checksum\n");
237 for (
i = 0;
i < ext_sigcount;
i++) {
239 EXT_SIGNATURE_SIZE *
i;
241 - (mc_header->
sig + mc_header->
pf + mc_header->
cksum)
242 + (ext_sig->
sig + ext_sig->
pf + ext_sig->
cksum);
244 pr_err(
"aborting, bad checksum\n");
256 get_matching_microcode(
struct cpu_signature *cpu_sig,
void *mc,
int rev)
264 if (!update_match_revision(mc_header, rev))
267 if (update_match_cpu(cpu_sig, mc_header->
sig, mc_header->
pf))
275 ext_sigcount = ext_header->
count;
278 for (i = 0; i < ext_sigcount; i++) {
279 if (update_match_cpu(cpu_sig, ext_sig->
sig, ext_sig->
pf))
286 static int apply_microcode(
int cpu)
300 if (mc_intel ==
NULL)
305 (
unsigned long) mc_intel->
bits,
306 (
unsigned long) mc_intel->
bits >> 16 >> 16);
315 if (val[1] != mc_intel->
hdr.rev) {
316 pr_err(
"CPU%d update to revision 0x%x failed\n",
317 cpu_num, mc_intel->
hdr.rev);
320 pr_info(
"CPU%d updated to revision 0x%x, date = %04x-%02x-%02x\n",
322 mc_intel->
hdr.date & 0xffff,
323 mc_intel->
hdr.date >> 24,
324 (mc_intel->
hdr.date >> 16) & 0xff);
333 int (*get_ucode_data)(
void *,
const void *,
size_t))
337 int new_rev = uci->
cpu_sig.rev;
338 unsigned int leftover =
size;
340 unsigned int curr_mc_size = 0;
344 unsigned int mc_size;
346 if (get_ucode_data(&mc_header, ucode_ptr,
sizeof(mc_header)))
350 if (!mc_size || mc_size > leftover) {
351 pr_err(
"error! Bad data in microcode data file\n");
356 if (!mc || mc_size > curr_mc_size) {
361 curr_mc_size = mc_size;
364 if (get_ucode_data(mc, ucode_ptr, mc_size) ||
365 microcode_sanity_check(mc) < 0) {
369 if (get_matching_microcode(&uci->
cpu_sig, mc, new_rev)) {
371 new_rev = mc_header.
rev;
376 ucode_ptr += mc_size;
396 pr_debug(
"CPU%d found a matching microcode update with version 0x%x (current=0x%x)\n",
397 cpu, new_rev, uci->
cpu_sig.rev);
402 static int get_ucode_fw(
void *to,
const void *
from,
size_t n)
416 sprintf(name,
"intel-ucode/%02x-%02x-%02x",
420 pr_debug(
"data file %s load failed\n", name);
424 ret = generic_load_microcode(cpu, (
void *)firmware->
data,
425 firmware->
size, &get_ucode_fw);
432 static int get_ucode_user(
void *to,
const void *
from,
size_t n)
438 request_microcode_user(
int cpu,
const void __user *
buf,
size_t size)
440 return generic_load_microcode(cpu, (
void *)buf, size, &get_ucode_user);
443 static void microcode_fini_cpu(
int cpu)
465 pr_err(
"Intel CPU family 0x%x not supported\n", c->
x86);
469 return µcode_intel_ops;