hc
2024-02-19 1c055e55a242a33e574e48be530e06770a210dcd
kernel/arch/x86/kernel/cpu/microcode/amd.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * AMD CPU Microcode Update Driver for Linux
34 *
....@@ -5,7 +6,7 @@
56 * CPUs and later.
67 *
78 * Copyright (C) 2008-2011 Advanced Micro Devices Inc.
8
- * 2013-2016 Borislav Petkov <bp@alien8.de>
9
+ * 2013-2018 Borislav Petkov <bp@alien8.de>
910 *
1011 * Author: Peter Oruba <peter.oruba@amd.com>
1112 *
....@@ -17,9 +18,6 @@
1718 *
1819 * Author: Jacob Shin <jacob.shin@amd.com>
1920 * Fixes: Borislav Petkov <bp@suse.de>
20
- *
21
- * Licensed under the terms of the GNU General Public
22
- * License version 2. See file COPYING for details.
2321 */
2422 #define pr_fmt(fmt) "microcode: " fmt
2523
....@@ -38,7 +36,10 @@
3836 #include <asm/cpu.h>
3937 #include <asm/msr.h>
4038
41
-static struct equiv_cpu_entry *equiv_cpu_table;
39
+static struct equiv_cpu_table {
40
+ unsigned int num_entries;
41
+ struct equiv_cpu_entry *entry;
42
+} equiv_table;
4243
4344 /*
4445 * This points to the current valid container of microcode patches which we will
....@@ -54,21 +55,235 @@
5455 };
5556
5657 static u32 ucode_new_rev;
57
-static u8 amd_ucode_patch[PATCH_MAX_SIZE];
58
+
59
+/* One blob per node. */
60
+static u8 amd_ucode_patch[MAX_NUMNODES][PATCH_MAX_SIZE];
5861
5962 /*
6063 * Microcode patch container file is prepended to the initrd in cpio
61
- * format. See Documentation/x86/microcode.txt
64
+ * format. See Documentation/x86/microcode.rst
6265 */
6366 static const char
6467 ucode_path[] __maybe_unused = "kernel/x86/microcode/AuthenticAMD.bin";
6568
66
-static u16 find_equiv_id(struct equiv_cpu_entry *equiv_table, u32 sig)
69
+static u16 find_equiv_id(struct equiv_cpu_table *et, u32 sig)
6770 {
68
- for (; equiv_table && equiv_table->installed_cpu; equiv_table++) {
69
- if (sig == equiv_table->installed_cpu)
70
- return equiv_table->equiv_cpu;
71
+ unsigned int i;
72
+
73
+ if (!et || !et->num_entries)
74
+ return 0;
75
+
76
+ for (i = 0; i < et->num_entries; i++) {
77
+ struct equiv_cpu_entry *e = &et->entry[i];
78
+
79
+ if (sig == e->installed_cpu)
80
+ return e->equiv_cpu;
81
+
82
+ e++;
7183 }
84
+ return 0;
85
+}
86
+
87
+/*
88
+ * Check whether there is a valid microcode container file at the beginning
89
+ * of @buf of size @buf_size. Set @early to use this function in the early path.
90
+ */
91
+static bool verify_container(const u8 *buf, size_t buf_size, bool early)
92
+{
93
+ u32 cont_magic;
94
+
95
+ if (buf_size <= CONTAINER_HDR_SZ) {
96
+ if (!early)
97
+ pr_debug("Truncated microcode container header.\n");
98
+
99
+ return false;
100
+ }
101
+
102
+ cont_magic = *(const u32 *)buf;
103
+ if (cont_magic != UCODE_MAGIC) {
104
+ if (!early)
105
+ pr_debug("Invalid magic value (0x%08x).\n", cont_magic);
106
+
107
+ return false;
108
+ }
109
+
110
+ return true;
111
+}
112
+
113
+/*
114
+ * Check whether there is a valid, non-truncated CPU equivalence table at the
115
+ * beginning of @buf of size @buf_size. Set @early to use this function in the
116
+ * early path.
117
+ */
118
+static bool verify_equivalence_table(const u8 *buf, size_t buf_size, bool early)
119
+{
120
+ const u32 *hdr = (const u32 *)buf;
121
+ u32 cont_type, equiv_tbl_len;
122
+
123
+ if (!verify_container(buf, buf_size, early))
124
+ return false;
125
+
126
+ cont_type = hdr[1];
127
+ if (cont_type != UCODE_EQUIV_CPU_TABLE_TYPE) {
128
+ if (!early)
129
+ pr_debug("Wrong microcode container equivalence table type: %u.\n",
130
+ cont_type);
131
+
132
+ return false;
133
+ }
134
+
135
+ buf_size -= CONTAINER_HDR_SZ;
136
+
137
+ equiv_tbl_len = hdr[2];
138
+ if (equiv_tbl_len < sizeof(struct equiv_cpu_entry) ||
139
+ buf_size < equiv_tbl_len) {
140
+ if (!early)
141
+ pr_debug("Truncated equivalence table.\n");
142
+
143
+ return false;
144
+ }
145
+
146
+ return true;
147
+}
148
+
149
+/*
150
+ * Check whether there is a valid, non-truncated microcode patch section at the
151
+ * beginning of @buf of size @buf_size. Set @early to use this function in the
152
+ * early path.
153
+ *
154
+ * On success, @sh_psize returns the patch size according to the section header,
155
+ * to the caller.
156
+ */
157
+static bool
158
+__verify_patch_section(const u8 *buf, size_t buf_size, u32 *sh_psize, bool early)
159
+{
160
+ u32 p_type, p_size;
161
+ const u32 *hdr;
162
+
163
+ if (buf_size < SECTION_HDR_SIZE) {
164
+ if (!early)
165
+ pr_debug("Truncated patch section.\n");
166
+
167
+ return false;
168
+ }
169
+
170
+ hdr = (const u32 *)buf;
171
+ p_type = hdr[0];
172
+ p_size = hdr[1];
173
+
174
+ if (p_type != UCODE_UCODE_TYPE) {
175
+ if (!early)
176
+ pr_debug("Invalid type field (0x%x) in container file section header.\n",
177
+ p_type);
178
+
179
+ return false;
180
+ }
181
+
182
+ if (p_size < sizeof(struct microcode_header_amd)) {
183
+ if (!early)
184
+ pr_debug("Patch of size %u too short.\n", p_size);
185
+
186
+ return false;
187
+ }
188
+
189
+ *sh_psize = p_size;
190
+
191
+ return true;
192
+}
193
+
194
+/*
195
+ * Check whether the passed remaining file @buf_size is large enough to contain
196
+ * a patch of the indicated @sh_psize (and also whether this size does not
197
+ * exceed the per-family maximum). @sh_psize is the size read from the section
198
+ * header.
199
+ */
200
+static unsigned int __verify_patch_size(u8 family, u32 sh_psize, size_t buf_size)
201
+{
202
+ u32 max_size;
203
+
204
+ if (family >= 0x15)
205
+ return min_t(u32, sh_psize, buf_size);
206
+
207
+#define F1XH_MPB_MAX_SIZE 2048
208
+#define F14H_MPB_MAX_SIZE 1824
209
+
210
+ switch (family) {
211
+ case 0x10 ... 0x12:
212
+ max_size = F1XH_MPB_MAX_SIZE;
213
+ break;
214
+ case 0x14:
215
+ max_size = F14H_MPB_MAX_SIZE;
216
+ break;
217
+ default:
218
+ WARN(1, "%s: WTF family: 0x%x\n", __func__, family);
219
+ return 0;
220
+ break;
221
+ }
222
+
223
+ if (sh_psize > min_t(u32, buf_size, max_size))
224
+ return 0;
225
+
226
+ return sh_psize;
227
+}
228
+
229
+/*
230
+ * Verify the patch in @buf.
231
+ *
232
+ * Returns:
233
+ * negative: on error
234
+ * positive: patch is not for this family, skip it
235
+ * 0: success
236
+ */
237
+static int
238
+verify_patch(u8 family, const u8 *buf, size_t buf_size, u32 *patch_size, bool early)
239
+{
240
+ struct microcode_header_amd *mc_hdr;
241
+ unsigned int ret;
242
+ u32 sh_psize;
243
+ u16 proc_id;
244
+ u8 patch_fam;
245
+
246
+ if (!__verify_patch_section(buf, buf_size, &sh_psize, early))
247
+ return -1;
248
+
249
+ /*
250
+ * The section header length is not included in this indicated size
251
+ * but is present in the leftover file length so we need to subtract
252
+ * it before passing this value to the function below.
253
+ */
254
+ buf_size -= SECTION_HDR_SIZE;
255
+
256
+ /*
257
+ * Check if the remaining buffer is big enough to contain a patch of
258
+ * size sh_psize, as the section claims.
259
+ */
260
+ if (buf_size < sh_psize) {
261
+ if (!early)
262
+ pr_debug("Patch of size %u truncated.\n", sh_psize);
263
+
264
+ return -1;
265
+ }
266
+
267
+ ret = __verify_patch_size(family, sh_psize, buf_size);
268
+ if (!ret) {
269
+ if (!early)
270
+ pr_debug("Per-family patch size mismatch.\n");
271
+ return -1;
272
+ }
273
+
274
+ *patch_size = sh_psize;
275
+
276
+ mc_hdr = (struct microcode_header_amd *)(buf + SECTION_HDR_SIZE);
277
+ if (mc_hdr->nb_dev_id || mc_hdr->sb_dev_id) {
278
+ if (!early)
279
+ pr_err("Patch-ID 0x%08x: chipset-specific code unsupported.\n", mc_hdr->patch_id);
280
+ return -1;
281
+ }
282
+
283
+ proc_id = mc_hdr->processor_rev_id;
284
+ patch_fam = 0xf + (proc_id >> 12);
285
+ if (patch_fam != family)
286
+ return 1;
72287
73288 return 0;
74289 }
....@@ -80,26 +295,28 @@
80295 * Returns the amount of bytes consumed while scanning. @desc contains all the
81296 * data we're going to use in later stages of the application.
82297 */
83
-static ssize_t parse_container(u8 *ucode, ssize_t size, struct cont_desc *desc)
298
+static size_t parse_container(u8 *ucode, size_t size, struct cont_desc *desc)
84299 {
85
- struct equiv_cpu_entry *eq;
86
- ssize_t orig_size = size;
300
+ struct equiv_cpu_table table;
301
+ size_t orig_size = size;
87302 u32 *hdr = (u32 *)ucode;
88303 u16 eq_id;
89304 u8 *buf;
90305
91
- /* Am I looking at an equivalence table header? */
92
- if (hdr[0] != UCODE_MAGIC ||
93
- hdr[1] != UCODE_EQUIV_CPU_TABLE_TYPE ||
94
- hdr[2] == 0)
95
- return CONTAINER_HDR_SZ;
306
+ if (!verify_equivalence_table(ucode, size, true))
307
+ return 0;
96308
97309 buf = ucode;
98310
99
- eq = (struct equiv_cpu_entry *)(buf + CONTAINER_HDR_SZ);
311
+ table.entry = (struct equiv_cpu_entry *)(buf + CONTAINER_HDR_SZ);
312
+ table.num_entries = hdr[2] / sizeof(struct equiv_cpu_entry);
100313
101
- /* Find the equivalence ID of our CPU in this table: */
102
- eq_id = find_equiv_id(eq, desc->cpuid_1_eax);
314
+ /*
315
+ * Find the equivalence ID of our CPU in this table. Even if this table
316
+ * doesn't contain a patch for the CPU, scan through the whole container
317
+ * so that it can be skipped in case there are other containers appended.
318
+ */
319
+ eq_id = find_equiv_id(&table, desc->cpuid_1_eax);
103320
104321 buf += hdr[2] + CONTAINER_HDR_SZ;
105322 size -= hdr[2] + CONTAINER_HDR_SZ;
....@@ -111,29 +328,29 @@
111328 while (size > 0) {
112329 struct microcode_amd *mc;
113330 u32 patch_size;
331
+ int ret;
114332
115
- hdr = (u32 *)buf;
333
+ ret = verify_patch(x86_family(desc->cpuid_1_eax), buf, size, &patch_size, true);
334
+ if (ret < 0) {
335
+ /*
336
+ * Patch verification failed, skip to the next
337
+ * container, if there's one:
338
+ */
339
+ goto out;
340
+ } else if (ret > 0) {
341
+ goto skip;
342
+ }
116343
117
- if (hdr[0] != UCODE_UCODE_TYPE)
118
- break;
119
-
120
- /* Sanity-check patch size. */
121
- patch_size = hdr[1];
122
- if (patch_size > PATCH_MAX_SIZE)
123
- break;
124
-
125
- /* Skip patch section header: */
126
- buf += SECTION_HDR_SIZE;
127
- size -= SECTION_HDR_SIZE;
128
-
129
- mc = (struct microcode_amd *)buf;
344
+ mc = (struct microcode_amd *)(buf + SECTION_HDR_SIZE);
130345 if (eq_id == mc->hdr.processor_rev_id) {
131346 desc->psize = patch_size;
132347 desc->mc = mc;
133348 }
134349
135
- buf += patch_size;
136
- size -= patch_size;
350
+skip:
351
+ /* Skip patch section header too: */
352
+ buf += patch_size + SECTION_HDR_SIZE;
353
+ size -= patch_size + SECTION_HDR_SIZE;
137354 }
138355
139356 /*
....@@ -150,6 +367,7 @@
150367 return 0;
151368 }
152369
370
+out:
153371 return orig_size - size;
154372 }
155373
....@@ -159,15 +377,18 @@
159377 */
160378 static void scan_containers(u8 *ucode, size_t size, struct cont_desc *desc)
161379 {
162
- ssize_t rem = size;
163
-
164
- while (rem >= 0) {
165
- ssize_t s = parse_container(ucode, rem, desc);
380
+ while (size) {
381
+ size_t s = parse_container(ucode, size, desc);
166382 if (!s)
167383 return;
168384
169
- ucode += s;
170
- rem -= s;
385
+ /* catch wraparound */
386
+ if (size >= s) {
387
+ ucode += s;
388
+ size -= s;
389
+ } else {
390
+ return;
391
+ }
171392 }
172393 }
173394
....@@ -210,7 +431,7 @@
210431 patch = (u8 (*)[PATCH_MAX_SIZE])__pa_nodebug(&amd_ucode_patch);
211432 #else
212433 new_rev = &ucode_new_rev;
213
- patch = &amd_ucode_patch;
434
+ patch = &amd_ucode_patch[0];
214435 #endif
215436
216437 desc.cpuid_1_eax = cpuid_1_eax;
....@@ -222,7 +443,13 @@
222443 return ret;
223444
224445 native_rdmsr(MSR_AMD64_PATCH_LEVEL, rev, dummy);
225
- if (rev >= mc->hdr.patch_id)
446
+
447
+ /*
448
+ * Allow application of the same revision to pick up SMT-specific
449
+ * changes even if the revision of the other SMT thread is already
450
+ * up-to-date.
451
+ */
452
+ if (rev > mc->hdr.patch_id)
226453 return ret;
227454
228455 if (!__apply_microcode_amd(mc)) {
....@@ -304,8 +531,12 @@
304531
305532 native_rdmsr(MSR_AMD64_PATCH_LEVEL, rev, dummy);
306533
307
- /* Check whether we have saved a new patch already: */
308
- if (*new_rev && rev < mc->hdr.patch_id) {
534
+ /*
535
+ * Check whether a new patch has been saved already. Also, allow application of
536
+ * the same revision in order to pick up SMT-thread-specific configuration even
537
+ * if the sibling SMT thread already has an up-to-date revision.
538
+ */
539
+ if (*new_rev && rev <= mc->hdr.patch_id) {
309540 if (!__apply_microcode_amd(mc)) {
310541 *new_rev = mc->hdr.patch_id;
311542 return;
....@@ -319,8 +550,7 @@
319550 apply_microcode_early_amd(cpuid_1_eax, cp.data, cp.size, false);
320551 }
321552
322
-static enum ucode_state
323
-load_microcode_amd(bool save, u8 family, const u8 *data, size_t size);
553
+static enum ucode_state load_microcode_amd(u8 family, const u8 *data, size_t size);
324554
325555 int __init save_microcode_in_initrd_amd(unsigned int cpuid_1_eax)
326556 {
....@@ -338,19 +568,19 @@
338568 if (!desc.mc)
339569 return -EINVAL;
340570
341
- ret = load_microcode_amd(true, x86_family(cpuid_1_eax), desc.data, desc.size);
571
+ ret = load_microcode_amd(x86_family(cpuid_1_eax), desc.data, desc.size);
342572 if (ret > UCODE_UPDATED)
343573 return -EINVAL;
344574
345575 return 0;
346576 }
347577
348
-void reload_ucode_amd(void)
578
+void reload_ucode_amd(unsigned int cpu)
349579 {
580
+ u32 rev, dummy __always_unused;
350581 struct microcode_amd *mc;
351
- u32 rev, dummy;
352582
353
- mc = (struct microcode_amd *)amd_ucode_patch;
583
+ mc = (struct microcode_amd *)amd_ucode_patch[cpu_to_node(cpu)];
354584
355585 rdmsr(MSR_AMD64_PATCH_LEVEL, rev, dummy);
356586
....@@ -364,21 +594,7 @@
364594 static u16 __find_equiv_id(unsigned int cpu)
365595 {
366596 struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
367
- return find_equiv_id(equiv_cpu_table, uci->cpu_sig.sig);
368
-}
369
-
370
-static u32 find_cpu_family_by_equiv_cpu(u16 equiv_cpu)
371
-{
372
- int i = 0;
373
-
374
- BUG_ON(!equiv_cpu_table);
375
-
376
- while (equiv_cpu_table[i].equiv_cpu != 0) {
377
- if (equiv_cpu == equiv_cpu_table[i].equiv_cpu)
378
- return equiv_cpu_table[i].installed_cpu;
379
- i++;
380
- }
381
- return 0;
597
+ return find_equiv_id(&equiv_table, uci->cpu_sig.sig);
382598 }
383599
384600 /*
....@@ -461,43 +677,6 @@
461677 return 0;
462678 }
463679
464
-static unsigned int verify_patch_size(u8 family, u32 patch_size,
465
- unsigned int size)
466
-{
467
- u32 max_size;
468
-
469
-#define F1XH_MPB_MAX_SIZE 2048
470
-#define F14H_MPB_MAX_SIZE 1824
471
-#define F15H_MPB_MAX_SIZE 4096
472
-#define F16H_MPB_MAX_SIZE 3458
473
-#define F17H_MPB_MAX_SIZE 3200
474
-
475
- switch (family) {
476
- case 0x14:
477
- max_size = F14H_MPB_MAX_SIZE;
478
- break;
479
- case 0x15:
480
- max_size = F15H_MPB_MAX_SIZE;
481
- break;
482
- case 0x16:
483
- max_size = F16H_MPB_MAX_SIZE;
484
- break;
485
- case 0x17:
486
- max_size = F17H_MPB_MAX_SIZE;
487
- break;
488
- default:
489
- max_size = F1XH_MPB_MAX_SIZE;
490
- break;
491
- }
492
-
493
- if (patch_size > min_t(u32, size, max_size)) {
494
- pr_err("patch size mismatch\n");
495
- return 0;
496
- }
497
-
498
- return patch_size;
499
-}
500
-
501680 static enum ucode_state apply_microcode_amd(int cpu)
502681 {
503682 struct cpuinfo_x86 *c = &cpu_data(cpu);
....@@ -505,7 +684,7 @@
505684 struct ucode_cpu_info *uci;
506685 struct ucode_patch *p;
507686 enum ucode_state ret;
508
- u32 rev, dummy;
687
+ u32 rev, dummy __always_unused;
509688
510689 BUG_ON(raw_smp_processor_id() != cpu);
511690
....@@ -521,7 +700,7 @@
521700 rdmsr(MSR_AMD64_PATCH_LEVEL, rev, dummy);
522701
523702 /* need to apply patch? */
524
- if (rev >= mc_amd->hdr.patch_id) {
703
+ if (rev > mc_amd->hdr.patch_id) {
525704 ret = UCODE_OK;
526705 goto out;
527706 }
....@@ -548,34 +727,34 @@
548727 return ret;
549728 }
550729
551
-static int install_equiv_cpu_table(const u8 *buf)
730
+static size_t install_equiv_cpu_table(const u8 *buf, size_t buf_size)
552731 {
553
- unsigned int *ibuf = (unsigned int *)buf;
554
- unsigned int type = ibuf[1];
555
- unsigned int size = ibuf[2];
732
+ u32 equiv_tbl_len;
733
+ const u32 *hdr;
556734
557
- if (type != UCODE_EQUIV_CPU_TABLE_TYPE || !size) {
558
- pr_err("empty section/"
559
- "invalid type field in container file section header\n");
560
- return -EINVAL;
561
- }
735
+ if (!verify_equivalence_table(buf, buf_size, false))
736
+ return 0;
562737
563
- equiv_cpu_table = vmalloc(size);
564
- if (!equiv_cpu_table) {
738
+ hdr = (const u32 *)buf;
739
+ equiv_tbl_len = hdr[2];
740
+
741
+ equiv_table.entry = vmalloc(equiv_tbl_len);
742
+ if (!equiv_table.entry) {
565743 pr_err("failed to allocate equivalent CPU table\n");
566
- return -ENOMEM;
744
+ return 0;
567745 }
568746
569
- memcpy(equiv_cpu_table, buf + CONTAINER_HDR_SZ, size);
747
+ memcpy(equiv_table.entry, buf + CONTAINER_HDR_SZ, equiv_tbl_len);
748
+ equiv_table.num_entries = equiv_tbl_len / sizeof(struct equiv_cpu_entry);
570749
571750 /* add header length */
572
- return size + CONTAINER_HDR_SZ;
751
+ return equiv_tbl_len + CONTAINER_HDR_SZ;
573752 }
574753
575754 static void free_equiv_cpu_table(void)
576755 {
577
- vfree(equiv_cpu_table);
578
- equiv_cpu_table = NULL;
756
+ vfree(equiv_table.entry);
757
+ memset(&equiv_table, 0, sizeof(equiv_table));
579758 }
580759
581760 static void cleanup(void)
....@@ -585,47 +764,23 @@
585764 }
586765
587766 /*
588
- * We return the current size even if some of the checks failed so that
767
+ * Return a non-negative value even if some of the checks failed so that
589768 * we can skip over the next patch. If we return a negative value, we
590769 * signal a grave error like a memory allocation has failed and the
591770 * driver cannot continue functioning normally. In such cases, we tear
592771 * down everything we've used up so far and exit.
593772 */
594
-static int verify_and_add_patch(u8 family, u8 *fw, unsigned int leftover)
773
+static int verify_and_add_patch(u8 family, u8 *fw, unsigned int leftover,
774
+ unsigned int *patch_size)
595775 {
596776 struct microcode_header_amd *mc_hdr;
597777 struct ucode_patch *patch;
598
- unsigned int patch_size, crnt_size, ret;
599
- u32 proc_fam;
600778 u16 proc_id;
779
+ int ret;
601780
602
- patch_size = *(u32 *)(fw + 4);
603
- crnt_size = patch_size + SECTION_HDR_SIZE;
604
- mc_hdr = (struct microcode_header_amd *)(fw + SECTION_HDR_SIZE);
605
- proc_id = mc_hdr->processor_rev_id;
606
-
607
- proc_fam = find_cpu_family_by_equiv_cpu(proc_id);
608
- if (!proc_fam) {
609
- pr_err("No patch family for equiv ID: 0x%04x\n", proc_id);
610
- return crnt_size;
611
- }
612
-
613
- /* check if patch is for the current family */
614
- proc_fam = ((proc_fam >> 8) & 0xf) + ((proc_fam >> 20) & 0xff);
615
- if (proc_fam != family)
616
- return crnt_size;
617
-
618
- if (mc_hdr->nb_dev_id || mc_hdr->sb_dev_id) {
619
- pr_err("Patch-ID 0x%08x: chipset-specific code unsupported.\n",
620
- mc_hdr->patch_id);
621
- return crnt_size;
622
- }
623
-
624
- ret = verify_patch_size(family, patch_size, leftover);
625
- if (!ret) {
626
- pr_err("Patch-ID 0x%08x: size mismatch.\n", mc_hdr->patch_id);
627
- return crnt_size;
628
- }
781
+ ret = verify_patch(family, fw, leftover, patch_size, false);
782
+ if (ret)
783
+ return ret;
629784
630785 patch = kzalloc(sizeof(*patch), GFP_KERNEL);
631786 if (!patch) {
....@@ -633,12 +788,16 @@
633788 return -EINVAL;
634789 }
635790
636
- patch->data = kmemdup(fw + SECTION_HDR_SIZE, patch_size, GFP_KERNEL);
791
+ patch->data = kmemdup(fw + SECTION_HDR_SIZE, *patch_size, GFP_KERNEL);
637792 if (!patch->data) {
638793 pr_err("Patch data allocation failure.\n");
639794 kfree(patch);
640795 return -EINVAL;
641796 }
797
+ patch->size = *patch_size;
798
+
799
+ mc_hdr = (struct microcode_header_amd *)(fw + SECTION_HDR_SIZE);
800
+ proc_id = mc_hdr->processor_rev_id;
642801
643802 INIT_LIST_HEAD(&patch->plist);
644803 patch->patch_id = mc_hdr->patch_id;
....@@ -650,47 +809,47 @@
650809 /* ... and add to cache. */
651810 update_cache(patch);
652811
653
- return crnt_size;
812
+ return 0;
654813 }
655814
656815 static enum ucode_state __load_microcode_amd(u8 family, const u8 *data,
657816 size_t size)
658817 {
659
- enum ucode_state ret = UCODE_ERROR;
660
- unsigned int leftover;
661818 u8 *fw = (u8 *)data;
662
- int crnt_size = 0;
663
- int offset;
819
+ size_t offset;
664820
665
- offset = install_equiv_cpu_table(data);
666
- if (offset < 0) {
667
- pr_err("failed to create equivalent cpu table\n");
668
- return ret;
669
- }
670
- fw += offset;
671
- leftover = size - offset;
821
+ offset = install_equiv_cpu_table(data, size);
822
+ if (!offset)
823
+ return UCODE_ERROR;
824
+
825
+ fw += offset;
826
+ size -= offset;
672827
673828 if (*(u32 *)fw != UCODE_UCODE_TYPE) {
674829 pr_err("invalid type field in container file section header\n");
675830 free_equiv_cpu_table();
676
- return ret;
831
+ return UCODE_ERROR;
677832 }
678833
679
- while (leftover) {
680
- crnt_size = verify_and_add_patch(family, fw, leftover);
681
- if (crnt_size < 0)
682
- return ret;
834
+ while (size > 0) {
835
+ unsigned int crnt_size = 0;
836
+ int ret;
683837
684
- fw += crnt_size;
685
- leftover -= crnt_size;
838
+ ret = verify_and_add_patch(family, fw, size, &crnt_size);
839
+ if (ret < 0)
840
+ return UCODE_ERROR;
841
+
842
+ fw += crnt_size + SECTION_HDR_SIZE;
843
+ size -= (crnt_size + SECTION_HDR_SIZE);
686844 }
687845
688846 return UCODE_OK;
689847 }
690848
691
-static enum ucode_state
692
-load_microcode_amd(bool save, u8 family, const u8 *data, size_t size)
849
+static enum ucode_state load_microcode_amd(u8 family, const u8 *data, size_t size)
693850 {
851
+ struct cpuinfo_x86 *c;
852
+ unsigned int nid, cpu;
694853 struct ucode_patch *p;
695854 enum ucode_state ret;
696855
....@@ -703,22 +862,22 @@
703862 return ret;
704863 }
705864
706
- p = find_patch(0);
707
- if (!p) {
708
- return ret;
709
- } else {
710
- if (boot_cpu_data.microcode >= p->patch_id)
711
- return ret;
865
+ for_each_node(nid) {
866
+ cpu = cpumask_first(cpumask_of_node(nid));
867
+ c = &cpu_data(cpu);
868
+
869
+ p = find_patch(cpu);
870
+ if (!p)
871
+ continue;
872
+
873
+ if (c->microcode >= p->patch_id)
874
+ continue;
712875
713876 ret = UCODE_NEW;
877
+
878
+ memset(&amd_ucode_patch[nid], 0, PATCH_MAX_SIZE);
879
+ memcpy(&amd_ucode_patch[nid], p->data, min_t(u32, p->size, PATCH_MAX_SIZE));
714880 }
715
-
716
- /* save BSP's matching patch for early load */
717
- if (!save)
718
- return ret;
719
-
720
- memset(amd_ucode_patch, 0, PATCH_MAX_SIZE);
721
- memcpy(amd_ucode_patch, p->data, min_t(u32, ksize(p->data), PATCH_MAX_SIZE));
722881
723882 return ret;
724883 }
....@@ -744,12 +903,11 @@
744903 {
745904 char fw_name[36] = "amd-ucode/microcode_amd.bin";
746905 struct cpuinfo_x86 *c = &cpu_data(cpu);
747
- bool bsp = c->cpu_index == boot_cpu_data.cpu_index;
748906 enum ucode_state ret = UCODE_NFOUND;
749907 const struct firmware *fw;
750908
751909 /* reload ucode container only on the boot cpu */
752
- if (!refresh_fw || !bsp)
910
+ if (!refresh_fw)
753911 return UCODE_OK;
754912
755913 if (c->x86 >= 0x15)
....@@ -761,12 +919,10 @@
761919 }
762920
763921 ret = UCODE_ERROR;
764
- if (*(u32 *)fw->data != UCODE_MAGIC) {
765
- pr_err("invalid magic value (0x%08x)\n", *(u32 *)fw->data);
922
+ if (!verify_container(fw->data, fw->size, false))
766923 goto fw_release;
767
- }
768924
769
- ret = load_microcode_amd(bsp, c->x86, fw->data, fw->size);
925
+ ret = load_microcode_amd(c->x86, fw->data, fw->size);
770926
771927 fw_release:
772928 release_firmware(fw);