xref: /linux/arch/x86/kernel/cpu/microcode/intel.c (revision 65b09bfa8aa7ebe087093b591525385efb2d58b0)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Intel CPU Microcode Update Driver for Linux
4  *
5  * Copyright (C) 2000-2006 Tigran Aivazian <aivazian.tigran@gmail.com>
6  *		 2006 Shaohua Li <shaohua.li@intel.com>
7  *
8  * Intel CPU microcode early update for Linux
9  *
10  * Copyright (C) 2012 Fenghua Yu <fenghua.yu@intel.com>
11  *		      H Peter Anvin" <hpa@zytor.com>
12  */
13 #define pr_fmt(fmt) "microcode: " fmt
14 #include <linux/earlycpio.h>
15 #include <linux/firmware.h>
16 #include <linux/pci_ids.h>
17 #include <linux/uaccess.h>
18 #include <linux/initrd.h>
19 #include <linux/kernel.h>
20 #include <linux/delay.h>
21 #include <linux/slab.h>
22 #include <linux/cpu.h>
23 #include <linux/uio.h>
24 #include <linux/io.h>
25 #include <linux/mm.h>
26 
27 #include <asm/cpu_device_id.h>
28 #include <asm/processor.h>
29 #include <asm/tlbflush.h>
30 #include <asm/setup.h>
31 #include <asm/msr.h>
32 
33 #include "internal.h"
34 
35 static const char ucode_path[] = "kernel/x86/microcode/GenuineIntel.bin";
36 
37 #define UCODE_BSP_LOADED	((struct microcode_intel *)0x1UL)
38 
39 /* Defines for the microcode staging mailbox interface */
40 #define MBOX_REG_NUM		4
41 #define MBOX_REG_SIZE		sizeof(u32)
42 
43 #define MBOX_CONTROL_OFFSET	0x0
44 #define MBOX_STATUS_OFFSET	0x4
45 #define MBOX_WRDATA_OFFSET	0x8
46 #define MBOX_RDDATA_OFFSET	0xc
47 
48 #define MASK_MBOX_CTRL_ABORT	BIT(0)
49 #define MASK_MBOX_CTRL_GO	BIT(31)
50 
51 #define MASK_MBOX_STATUS_ERROR	BIT(2)
52 #define MASK_MBOX_STATUS_READY	BIT(31)
53 
54 #define MASK_MBOX_RESP_SUCCESS	BIT(0)
55 #define MASK_MBOX_RESP_PROGRESS	BIT(1)
56 #define MASK_MBOX_RESP_ERROR	BIT(2)
57 
58 #define MBOX_CMD_LOAD		0x3
59 #define MBOX_OBJ_STAGING	0xb
60 #define MBOX_HEADER(size)	((PCI_VENDOR_ID_INTEL)    | \
61 				 (MBOX_OBJ_STAGING << 16) | \
62 				 ((u64)((size) / sizeof(u32)) << 32))
63 
64 /* The size of each mailbox header */
65 #define MBOX_HEADER_SIZE	sizeof(u64)
66 /* The size of staging hardware response */
67 #define MBOX_RESPONSE_SIZE	sizeof(u64)
68 
69 #define MBOX_XACTION_TIMEOUT_MS	(10 * MSEC_PER_SEC)
70 
71 /* Current microcode patch used in early patching on the APs. */
72 static struct microcode_intel *ucode_patch_va __read_mostly;
73 static struct microcode_intel *ucode_patch_late __read_mostly;
74 
75 /* last level cache size per core */
76 static unsigned int llc_size_per_core __ro_after_init;
77 
78 /* microcode format is extended from prescott processors */
79 struct extended_signature {
80 	unsigned int	sig;
81 	unsigned int	pf;
82 	unsigned int	cksum;
83 };
84 
85 struct extended_sigtable {
86 	unsigned int			count;
87 	unsigned int			cksum;
88 	unsigned int			reserved[3];
89 	struct extended_signature	sigs[];
90 };
91 
92 /**
93  * struct staging_state - Track the current staging process state
94  *
95  * @mmio_base:		MMIO base address for staging
96  * @ucode_len:		Total size of the microcode image
97  * @chunk_size:		Size of each data piece
98  * @bytes_sent:		Total bytes transmitted so far
99  * @offset:		Current offset in the microcode image
100  */
101 struct staging_state {
102 	void __iomem		*mmio_base;
103 	unsigned int		ucode_len;
104 	unsigned int		chunk_size;
105 	unsigned int		bytes_sent;
106 	unsigned int		offset;
107 };
108 
109 #define DEFAULT_UCODE_TOTALSIZE (DEFAULT_UCODE_DATASIZE + MC_HEADER_SIZE)
110 #define EXT_HEADER_SIZE		(sizeof(struct extended_sigtable))
111 #define EXT_SIGNATURE_SIZE	(sizeof(struct extended_signature))
112 
113 static inline unsigned int get_totalsize(struct microcode_header_intel *hdr)
114 {
115 	return hdr->datasize ? hdr->totalsize : DEFAULT_UCODE_TOTALSIZE;
116 }
117 
118 static inline unsigned int exttable_size(struct extended_sigtable *et)
119 {
120 	return et->count * EXT_SIGNATURE_SIZE + EXT_HEADER_SIZE;
121 }
122 
123 
124 /*
125  * Use CPUID to generate a "vfm" value. Useful before cpuinfo_x86
126  * structures are populated.
127  */
128 static u32 intel_cpuid_vfm(void)
129 {
130 	u32 eax   = cpuid_eax(1);
131 	u32 fam   = x86_family(eax);
132 	u32 model = x86_model(eax);
133 
134 	return IFM(fam, model);
135 }
136 
137 u32 intel_get_platform_id(void)
138 {
139 	unsigned int val[2];
140 
141 	if (x86_hypervisor_present)
142 		return 0;
143 
144 	/*
145 	 * This can be called early. Use CPUID directly instead of
146 	 * relying on cpuinfo_x86 which may not be fully initialized.
147 	 * The PII does not have MSR_IA32_PLATFORM_ID. Everything
148 	 * before _it_ has no microcode (for Linux at least).
149 	 */
150 	if (intel_cpuid_vfm() <= INTEL_PENTIUM_II_KLAMATH)
151 		return 0;
152 
153 	/* get processor flags from MSR 0x17 */
154 	native_rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]);
155 
156 	return (val[1] >> 18) & 7;
157 }
158 
159 void intel_collect_cpu_info(struct cpu_signature *sig)
160 {
161 	sig->sig = cpuid_eax(1);
162 	sig->rev = intel_get_microcode_revision();
163 	sig->pf  = 1 << intel_get_platform_id();
164 }
165 EXPORT_SYMBOL_GPL(intel_collect_cpu_info);
166 
167 static inline bool cpu_signatures_match(struct cpu_signature *s1, unsigned int sig2,
168 					unsigned int pf2)
169 {
170 	if (s1->sig != sig2)
171 		return false;
172 
173 	/*
174 	 * Consider an empty mask to match everything. This
175 	 * should only occur for one CPU model, the PII.
176 	 */
177 	if (!pf2)
178 		return true;
179 
180 	/* Is the CPU's platform ID in the signature mask? */
181 	return s1->pf & pf2;
182 }
183 
184 bool intel_find_matching_signature(void *mc, struct cpu_signature *sig)
185 {
186 	struct microcode_header_intel *mc_hdr = mc;
187 	struct extended_signature *ext_sig;
188 	struct extended_sigtable *ext_hdr;
189 	int i;
190 
191 	if (cpu_signatures_match(sig, mc_hdr->sig, mc_hdr->pf))
192 		return true;
193 
194 	/* Look for ext. headers: */
195 	if (get_totalsize(mc_hdr) <= intel_microcode_get_datasize(mc_hdr) + MC_HEADER_SIZE)
196 		return false;
197 
198 	ext_hdr = mc + intel_microcode_get_datasize(mc_hdr) + MC_HEADER_SIZE;
199 	ext_sig = (void *)ext_hdr + EXT_HEADER_SIZE;
200 
201 	for (i = 0; i < ext_hdr->count; i++) {
202 		if (cpu_signatures_match(sig, ext_sig->sig, ext_sig->pf))
203 			return true;
204 		ext_sig++;
205 	}
206 	return 0;
207 }
208 EXPORT_SYMBOL_GPL(intel_find_matching_signature);
209 
210 /**
211  * intel_microcode_sanity_check() - Sanity check microcode file.
212  * @mc: Pointer to the microcode file contents.
213  * @print_err: Display failure reason if true, silent if false.
214  * @hdr_type: Type of file, i.e. normal microcode file or In Field Scan file.
215  *            Validate if the microcode header type matches with the type
216  *            specified here.
217  *
218  * Validate certain header fields and verify if computed checksum matches
219  * with the one specified in the header.
220  *
221  * Return: 0 if the file passes all the checks, -EINVAL if any of the checks
222  * fail.
223  */
224 int intel_microcode_sanity_check(void *mc, bool print_err, int hdr_type)
225 {
226 	unsigned long total_size, data_size, ext_table_size;
227 	struct microcode_header_intel *mc_header = mc;
228 	struct extended_sigtable *ext_header = NULL;
229 	u32 sum, orig_sum, ext_sigcount = 0, i;
230 	struct extended_signature *ext_sig;
231 
232 	total_size = get_totalsize(mc_header);
233 	data_size = intel_microcode_get_datasize(mc_header);
234 
235 	if (data_size + MC_HEADER_SIZE > total_size) {
236 		if (print_err)
237 			pr_err("Error: bad microcode data file size.\n");
238 		return -EINVAL;
239 	}
240 
241 	if (mc_header->ldrver != 1 || mc_header->hdrver != hdr_type) {
242 		if (print_err)
243 			pr_err("Error: invalid/unknown microcode update format. Header type %d\n",
244 			       mc_header->hdrver);
245 		return -EINVAL;
246 	}
247 
248 	ext_table_size = total_size - (MC_HEADER_SIZE + data_size);
249 	if (ext_table_size) {
250 		u32 ext_table_sum = 0;
251 		u32 *ext_tablep;
252 
253 		if (ext_table_size < EXT_HEADER_SIZE ||
254 		    ((ext_table_size - EXT_HEADER_SIZE) % EXT_SIGNATURE_SIZE)) {
255 			if (print_err)
256 				pr_err("Error: truncated extended signature table.\n");
257 			return -EINVAL;
258 		}
259 
260 		ext_header = mc + MC_HEADER_SIZE + data_size;
261 		if (ext_table_size != exttable_size(ext_header)) {
262 			if (print_err)
263 				pr_err("Error: extended signature table size mismatch.\n");
264 			return -EFAULT;
265 		}
266 
267 		ext_sigcount = ext_header->count;
268 
269 		/*
270 		 * Check extended table checksum: the sum of all dwords that
271 		 * comprise a valid table must be 0.
272 		 */
273 		ext_tablep = (u32 *)ext_header;
274 
275 		i = ext_table_size / sizeof(u32);
276 		while (i--)
277 			ext_table_sum += ext_tablep[i];
278 
279 		if (ext_table_sum) {
280 			if (print_err)
281 				pr_warn("Bad extended signature table checksum, aborting.\n");
282 			return -EINVAL;
283 		}
284 	}
285 
286 	/*
287 	 * Calculate the checksum of update data and header. The checksum of
288 	 * valid update data and header including the extended signature table
289 	 * must be 0.
290 	 */
291 	orig_sum = 0;
292 	i = (MC_HEADER_SIZE + data_size) / sizeof(u32);
293 	while (i--)
294 		orig_sum += ((u32 *)mc)[i];
295 
296 	if (orig_sum) {
297 		if (print_err)
298 			pr_err("Bad microcode data checksum, aborting.\n");
299 		return -EINVAL;
300 	}
301 
302 	if (!ext_table_size)
303 		return 0;
304 
305 	/*
306 	 * Check extended signature checksum: 0 => valid.
307 	 */
308 	for (i = 0; i < ext_sigcount; i++) {
309 		ext_sig = (void *)ext_header + EXT_HEADER_SIZE +
310 			  EXT_SIGNATURE_SIZE * i;
311 
312 		sum = (mc_header->sig + mc_header->pf + mc_header->cksum) -
313 		      (ext_sig->sig + ext_sig->pf + ext_sig->cksum);
314 		if (sum) {
315 			if (print_err)
316 				pr_err("Bad extended signature checksum, aborting.\n");
317 			return -EINVAL;
318 		}
319 	}
320 	return 0;
321 }
322 EXPORT_SYMBOL_GPL(intel_microcode_sanity_check);
323 
324 static void update_ucode_pointer(struct microcode_intel *mc)
325 {
326 	kvfree(ucode_patch_va);
327 
328 	/*
329 	 * Save the virtual address for early loading and for eventual free
330 	 * on late loading.
331 	 */
332 	ucode_patch_va = mc;
333 }
334 
335 static void save_microcode_patch(struct microcode_intel *patch)
336 {
337 	unsigned int size = get_totalsize(&patch->hdr);
338 	struct microcode_intel *mc;
339 
340 	mc = kvmemdup(patch, size, GFP_KERNEL);
341 	if (mc)
342 		update_ucode_pointer(mc);
343 	else
344 		pr_err("Unable to allocate microcode memory size: %u\n", size);
345 }
346 
347 /* Scan blob for microcode matching the boot CPUs family, model, stepping */
348 static __init struct microcode_intel *scan_microcode(void *data, size_t size,
349 						     struct ucode_cpu_info *uci,
350 						     bool save)
351 {
352 	struct microcode_header_intel *mc_header;
353 	struct microcode_intel *patch = NULL;
354 	u32 cur_rev = uci->cpu_sig.rev;
355 	unsigned int mc_size;
356 
357 	for (; size >= sizeof(struct microcode_header_intel); size -= mc_size, data += mc_size) {
358 		mc_header = (struct microcode_header_intel *)data;
359 
360 		mc_size = get_totalsize(mc_header);
361 		if (!mc_size || mc_size > size ||
362 		    intel_microcode_sanity_check(data, false, MC_HEADER_TYPE_MICROCODE) < 0)
363 			break;
364 
365 		if (!intel_find_matching_signature(data, &uci->cpu_sig))
366 			continue;
367 
368 		/*
369 		 * For saving the early microcode, find the matching revision which
370 		 * was loaded on the BSP.
371 		 *
372 		 * On the BSP during early boot, find a newer revision than
373 		 * actually loaded in the CPU.
374 		 */
375 		if (save) {
376 			if (cur_rev != mc_header->rev)
377 				continue;
378 		} else if (cur_rev >= mc_header->rev) {
379 			continue;
380 		}
381 
382 		patch = data;
383 		cur_rev = mc_header->rev;
384 	}
385 
386 	return size ? NULL : patch;
387 }
388 
389 static inline u32 read_mbox_dword(void __iomem *mmio_base)
390 {
391 	u32 dword = readl(mmio_base + MBOX_RDDATA_OFFSET);
392 
393 	/* Acknowledge read completion to the staging hardware */
394 	writel(0, mmio_base + MBOX_RDDATA_OFFSET);
395 	return dword;
396 }
397 
398 static inline void write_mbox_dword(void __iomem *mmio_base, u32 dword)
399 {
400 	writel(dword, mmio_base + MBOX_WRDATA_OFFSET);
401 }
402 
403 static inline u64 read_mbox_header(void __iomem *mmio_base)
404 {
405 	u32 high, low;
406 
407 	low  = read_mbox_dword(mmio_base);
408 	high = read_mbox_dword(mmio_base);
409 
410 	return ((u64)high << 32) | low;
411 }
412 
413 static inline void write_mbox_header(void __iomem *mmio_base, u64 value)
414 {
415 	write_mbox_dword(mmio_base, value);
416 	write_mbox_dword(mmio_base, value >> 32);
417 }
418 
419 static void write_mbox_data(void __iomem *mmio_base, u32 *chunk, unsigned int chunk_bytes)
420 {
421 	int i;
422 
423 	/*
424 	 * The MMIO space is mapped as Uncached (UC). Each write arrives
425 	 * at the device as an individual transaction in program order.
426 	 * The device can then reassemble the sequence accordingly.
427 	 */
428 	for (i = 0; i < chunk_bytes / sizeof(u32); i++)
429 		write_mbox_dword(mmio_base, chunk[i]);
430 }
431 
432 /*
433  * Prepare for a new microcode transfer: reset hardware and record the
434  * image size.
435  */
436 static void init_stage(struct staging_state *ss)
437 {
438 	ss->ucode_len = get_totalsize(&ucode_patch_late->hdr);
439 
440 	/*
441 	 * Abort any ongoing process, effectively resetting the device.
442 	 * Unlike regular mailbox data processing requests, this
443 	 * operation does not require a status check.
444 	 */
445 	writel(MASK_MBOX_CTRL_ABORT, ss->mmio_base + MBOX_CONTROL_OFFSET);
446 }
447 
448 /*
449  * Update the chunk size and decide whether another chunk can be sent.
450  * This accounts for remaining data and retry limits.
451  */
452 static bool can_send_next_chunk(struct staging_state *ss, int *err)
453 {
454 	/* A page size or remaining bytes if this is the final chunk */
455 	ss->chunk_size = min(PAGE_SIZE, ss->ucode_len - ss->offset);
456 
457 	/*
458 	 * Each microcode image is divided into chunks, each at most
459 	 * one page size. A 10-chunk image would typically require 10
460 	 * transactions.
461 	 *
462 	 * However, the hardware managing the mailbox has limited
463 	 * resources and may not cache the entire image, potentially
464 	 * requesting the same chunk multiple times.
465 	 *
466 	 * To tolerate this behavior, allow up to twice the expected
467 	 * number of transactions (i.e., a 10-chunk image can take up to
468 	 * 20 attempts).
469 	 *
470 	 * If the number of attempts exceeds this limit, treat it as
471 	 * exceeding the maximum allowed transfer size.
472 	 */
473 	if (ss->bytes_sent + ss->chunk_size > ss->ucode_len * 2) {
474 		*err = -EMSGSIZE;
475 		return false;
476 	}
477 
478 	*err = 0;
479 	return true;
480 }
481 
482 /*
483  * The hardware indicates completion by returning a sentinel end offset.
484  */
485 static inline bool is_end_offset(u32 offset)
486 {
487 	return offset == UINT_MAX;
488 }
489 
490 /*
491  * Determine whether staging is complete: either the hardware signaled
492  * the end offset, or no more transactions are permitted (retry limit
493  * reached).
494  */
495 static inline bool staging_is_complete(struct staging_state *ss, int *err)
496 {
497 	return is_end_offset(ss->offset) || !can_send_next_chunk(ss, err);
498 }
499 
500 /*
501  * Wait for the hardware to complete a transaction.
502  * Return 0 on success, or an error code on failure.
503  */
504 static int wait_for_transaction(struct staging_state *ss)
505 {
506 	u32 timeout, status;
507 
508 	/* Allow time for hardware to complete the operation: */
509 	for (timeout = 0; timeout < MBOX_XACTION_TIMEOUT_MS; timeout++) {
510 		msleep(1);
511 
512 		status = readl(ss->mmio_base + MBOX_STATUS_OFFSET);
513 		/* Break out early if the hardware is ready: */
514 		if (status & MASK_MBOX_STATUS_READY)
515 			break;
516 	}
517 
518 	/* Check for explicit error response */
519 	if (status & MASK_MBOX_STATUS_ERROR)
520 		return -EIO;
521 
522 	/*
523 	 * Hardware has neither responded to the action nor signaled any
524 	 * error. Treat this as a timeout.
525 	 */
526 	if (!(status & MASK_MBOX_STATUS_READY))
527 		return -ETIMEDOUT;
528 
529 	return 0;
530 }
531 
532 /*
533  * Transmit a chunk of the microcode image to the hardware.
534  * Return 0 on success, or an error code on failure.
535  */
536 static int send_data_chunk(struct staging_state *ss, void *ucode_ptr)
537 {
538 	u32 *src_chunk = ucode_ptr + ss->offset;
539 	u16 mbox_size;
540 
541 	/*
542 	 * Write a 'request' mailbox object in this order:
543 	 *  1. Mailbox header includes total size
544 	 *  2. Command header specifies the load operation
545 	 *  3. Data section contains a microcode chunk
546 	 *
547 	 * Thus, the mailbox size is two headers plus the chunk size.
548 	 */
549 	mbox_size = MBOX_HEADER_SIZE * 2 + ss->chunk_size;
550 	write_mbox_header(ss->mmio_base, MBOX_HEADER(mbox_size));
551 	write_mbox_header(ss->mmio_base, MBOX_CMD_LOAD);
552 	write_mbox_data(ss->mmio_base, src_chunk, ss->chunk_size);
553 	ss->bytes_sent += ss->chunk_size;
554 
555 	/* Notify the hardware that the mailbox is ready for processing. */
556 	writel(MASK_MBOX_CTRL_GO, ss->mmio_base + MBOX_CONTROL_OFFSET);
557 
558 	return wait_for_transaction(ss);
559 }
560 
561 /*
562  * Retrieve the next offset from the hardware response.
563  * Return 0 on success, or an error code on failure.
564  */
565 static int fetch_next_offset(struct staging_state *ss)
566 {
567 	const u64 expected_header = MBOX_HEADER(MBOX_HEADER_SIZE + MBOX_RESPONSE_SIZE);
568 	u32 offset, status;
569 	u64 header;
570 
571 	/*
572 	 * The 'response' mailbox returns three fields, in order:
573 	 *  1. Header
574 	 *  2. Next offset in the microcode image
575 	 *  3. Status flags
576 	 */
577 	header = read_mbox_header(ss->mmio_base);
578 	offset = read_mbox_dword(ss->mmio_base);
579 	status = read_mbox_dword(ss->mmio_base);
580 
581 	/* All valid responses must start with the expected header. */
582 	if (header != expected_header) {
583 		pr_err_once("staging: invalid response header (0x%llx)\n", header);
584 		return -EBADR;
585 	}
586 
587 	/*
588 	 * Verify the offset: If not at the end marker, it must not
589 	 * exceed the microcode image length.
590 	 */
591 	if (!is_end_offset(offset) && offset > ss->ucode_len) {
592 		pr_err_once("staging: invalid offset (%u) past the image end (%u)\n",
593 			    offset, ss->ucode_len);
594 		return -EINVAL;
595 	}
596 
597 	/* Hardware may report errors explicitly in the status field */
598 	if (status & MASK_MBOX_RESP_ERROR)
599 		return -EPROTO;
600 
601 	ss->offset = offset;
602 	return 0;
603 }
604 
605 /*
606  * Handle the staging process using the mailbox MMIO interface. The
607  * microcode image is transferred in chunks until completion.
608  * Return 0 on success or an error code on failure.
609  */
610 static int do_stage(u64 mmio_pa)
611 {
612 	struct staging_state ss = {};
613 	int err;
614 
615 	ss.mmio_base = ioremap(mmio_pa, MBOX_REG_NUM * MBOX_REG_SIZE);
616 	if (WARN_ON_ONCE(!ss.mmio_base))
617 		return -EADDRNOTAVAIL;
618 
619 	init_stage(&ss);
620 
621 	/* Perform the staging process while within the retry limit */
622 	while (!staging_is_complete(&ss, &err)) {
623 		/* Send a chunk of microcode each time: */
624 		err = send_data_chunk(&ss, ucode_patch_late);
625 		if (err)
626 			break;
627 		/*
628 		 * Then, ask the hardware which piece of the image it
629 		 * needs next. The same piece may be sent more than once.
630 		 */
631 		err = fetch_next_offset(&ss);
632 		if (err)
633 			break;
634 	}
635 
636 	iounmap(ss.mmio_base);
637 
638 	return err;
639 }
640 
641 static void stage_microcode(void)
642 {
643 	unsigned int pkg_id = UINT_MAX;
644 	int cpu, err;
645 	u64 mmio_pa;
646 
647 	if (!IS_ALIGNED(get_totalsize(&ucode_patch_late->hdr), sizeof(u32))) {
648 		pr_err("Microcode image 32-bit misaligned (0x%x), staging failed.\n",
649 			get_totalsize(&ucode_patch_late->hdr));
650 		return;
651 	}
652 
653 	lockdep_assert_cpus_held();
654 
655 	/*
656 	 * The MMIO address is unique per package, and all the SMT
657 	 * primary threads are online here. Find each MMIO space by
658 	 * their package IDs to avoid duplicate staging.
659 	 */
660 	for_each_cpu(cpu, cpu_primary_thread_mask) {
661 		if (topology_logical_package_id(cpu) == pkg_id)
662 			continue;
663 
664 		pkg_id = topology_logical_package_id(cpu);
665 
666 		err = rdmsrq_on_cpu(cpu, MSR_IA32_MCU_STAGING_MBOX_ADDR, &mmio_pa);
667 		if (WARN_ON_ONCE(err))
668 			return;
669 
670 		err = do_stage(mmio_pa);
671 		if (err) {
672 			pr_err("Error: staging failed (%d) for CPU%d at package %u.\n",
673 			       err, cpu, pkg_id);
674 			return;
675 		}
676 	}
677 
678 	pr_info("Staging of patch revision 0x%x succeeded.\n", ucode_patch_late->hdr.rev);
679 }
680 
681 static enum ucode_state __apply_microcode(struct ucode_cpu_info *uci,
682 					  struct microcode_intel *mc,
683 					  u32 *cur_rev)
684 {
685 	u32 rev;
686 
687 	if (!mc)
688 		return UCODE_NFOUND;
689 
690 	/*
691 	 * Save us the MSR write below - which is a particular expensive
692 	 * operation - when the other hyperthread has updated the microcode
693 	 * already.
694 	 */
695 	*cur_rev = intel_get_microcode_revision();
696 	if (*cur_rev >= mc->hdr.rev) {
697 		uci->cpu_sig.rev = *cur_rev;
698 		return UCODE_OK;
699 	}
700 
701 	/* write microcode via MSR 0x79 */
702 	native_wrmsrq(MSR_IA32_UCODE_WRITE, (unsigned long)mc->bits);
703 
704 	rev = intel_get_microcode_revision();
705 	if (rev != mc->hdr.rev)
706 		return UCODE_ERROR;
707 
708 	uci->cpu_sig.rev = rev;
709 	return UCODE_UPDATED;
710 }
711 
712 static enum ucode_state apply_microcode_early(struct ucode_cpu_info *uci)
713 {
714 	struct microcode_intel *mc = uci->mc;
715 	u32 cur_rev;
716 
717 	return __apply_microcode(uci, mc, &cur_rev);
718 }
719 
720 static __init bool load_builtin_intel_microcode(struct cpio_data *cp)
721 {
722 	unsigned int eax = 1, ebx, ecx = 0, edx;
723 	struct firmware fw;
724 	char name[30];
725 
726 	if (IS_ENABLED(CONFIG_X86_32))
727 		return false;
728 
729 	native_cpuid(&eax, &ebx, &ecx, &edx);
730 
731 	sprintf(name, "intel-ucode/%02x-%02x-%02x",
732 		x86_family(eax), x86_model(eax), x86_stepping(eax));
733 
734 	if (firmware_request_builtin(&fw, name)) {
735 		cp->size = fw.size;
736 		cp->data = (void *)fw.data;
737 		return true;
738 	}
739 	return false;
740 }
741 
742 static __init struct microcode_intel *get_microcode_blob(struct ucode_cpu_info *uci, bool save)
743 {
744 	struct cpio_data cp;
745 
746 	intel_collect_cpu_info(&uci->cpu_sig);
747 
748 	if (!load_builtin_intel_microcode(&cp))
749 		cp = find_microcode_in_initrd(ucode_path);
750 
751 	if (!(cp.data && cp.size))
752 		return NULL;
753 
754 	return scan_microcode(cp.data, cp.size, uci, save);
755 }
756 
757 /*
758  * Invoked from an early init call to save the microcode blob which was
759  * selected during early boot when mm was not usable. The microcode must be
760  * saved because initrd is going away. It's an early init call so the APs
761  * just can use the pointer and do not have to scan initrd/builtin firmware
762  * again.
763  */
764 static int __init save_builtin_microcode(void)
765 {
766 	struct ucode_cpu_info uci;
767 
768 	if (xchg(&ucode_patch_va, NULL) != UCODE_BSP_LOADED)
769 		return 0;
770 
771 	if (microcode_loader_disabled() || boot_cpu_data.x86_vendor != X86_VENDOR_INTEL)
772 		return 0;
773 
774 	uci.mc = get_microcode_blob(&uci, true);
775 	if (uci.mc)
776 		save_microcode_patch(uci.mc);
777 	return 0;
778 }
779 early_initcall(save_builtin_microcode);
780 
781 /* Load microcode on BSP from initrd or builtin blobs */
782 void __init load_ucode_intel_bsp(struct early_load_data *ed)
783 {
784 	struct ucode_cpu_info uci;
785 
786 	uci.mc = get_microcode_blob(&uci, false);
787 	ed->old_rev = uci.cpu_sig.rev;
788 
789 	if (uci.mc && apply_microcode_early(&uci) == UCODE_UPDATED) {
790 		ucode_patch_va = UCODE_BSP_LOADED;
791 		ed->new_rev = uci.cpu_sig.rev;
792 	}
793 }
794 
795 void load_ucode_intel_ap(void)
796 {
797 	struct ucode_cpu_info uci;
798 
799 	uci.mc = ucode_patch_va;
800 	if (uci.mc)
801 		apply_microcode_early(&uci);
802 }
803 
804 /* Reload microcode on resume */
805 void reload_ucode_intel(void)
806 {
807 	struct ucode_cpu_info uci = { .mc = ucode_patch_va, };
808 
809 	if (uci.mc)
810 		apply_microcode_early(&uci);
811 }
812 
813 static int collect_cpu_info(int cpu_num, struct cpu_signature *csig)
814 {
815 	intel_collect_cpu_info(csig);
816 	return 0;
817 }
818 
819 static enum ucode_state apply_microcode_late(int cpu)
820 {
821 	struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
822 	struct microcode_intel *mc = ucode_patch_late;
823 	enum ucode_state ret;
824 	u32 cur_rev;
825 
826 	if (WARN_ON_ONCE(smp_processor_id() != cpu))
827 		return UCODE_ERROR;
828 
829 	ret = __apply_microcode(uci, mc, &cur_rev);
830 	if (ret != UCODE_UPDATED && ret != UCODE_OK)
831 		return ret;
832 
833 	cpu_data(cpu).microcode	 = uci->cpu_sig.rev;
834 	if (!cpu)
835 		boot_cpu_data.microcode = uci->cpu_sig.rev;
836 
837 	return ret;
838 }
839 
840 static bool ucode_validate_minrev(struct microcode_header_intel *mc_header)
841 {
842 	int cur_rev = boot_cpu_data.microcode;
843 
844 	/*
845 	 * When late-loading, ensure the header declares a minimum revision
846 	 * required to perform a late-load. The previously reserved field
847 	 * is 0 in older microcode blobs.
848 	 */
849 	if (!mc_header->min_req_ver) {
850 		pr_info("Unsafe microcode update: Microcode header does not specify a required min version\n");
851 		return false;
852 	}
853 
854 	/*
855 	 * Check whether the current revision is either greater or equal to
856 	 * to the minimum revision specified in the header.
857 	 */
858 	if (cur_rev < mc_header->min_req_ver) {
859 		pr_info("Unsafe microcode update: Current revision 0x%x too old\n", cur_rev);
860 		pr_info("Current should be at 0x%x or higher. Use early loading instead\n", mc_header->min_req_ver);
861 		return false;
862 	}
863 	return true;
864 }
865 
866 static enum ucode_state parse_microcode_blobs(int cpu, struct iov_iter *iter)
867 {
868 	struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
869 	bool is_safe, new_is_safe = false;
870 	int cur_rev = uci->cpu_sig.rev;
871 	unsigned int curr_mc_size = 0;
872 	u8 *new_mc = NULL, *mc = NULL;
873 
874 	while (iov_iter_count(iter)) {
875 		struct microcode_header_intel mc_header;
876 		unsigned int mc_size, data_size;
877 		u8 *data;
878 
879 		if (!copy_from_iter_full(&mc_header, sizeof(mc_header), iter)) {
880 			pr_err("error! Truncated or inaccessible header in microcode data file\n");
881 			goto fail;
882 		}
883 
884 		mc_size = get_totalsize(&mc_header);
885 		if (mc_size < sizeof(mc_header)) {
886 			pr_err("error! Bad data in microcode data file (totalsize too small)\n");
887 			goto fail;
888 		}
889 		data_size = mc_size - sizeof(mc_header);
890 		if (data_size > iov_iter_count(iter)) {
891 			pr_err("error! Bad data in microcode data file (truncated file?)\n");
892 			goto fail;
893 		}
894 
895 		/* For performance reasons, reuse mc area when possible */
896 		if (!mc || mc_size > curr_mc_size) {
897 			kvfree(mc);
898 			mc = kvmalloc(mc_size, GFP_KERNEL);
899 			if (!mc)
900 				goto fail;
901 			curr_mc_size = mc_size;
902 		}
903 
904 		memcpy(mc, &mc_header, sizeof(mc_header));
905 		data = mc + sizeof(mc_header);
906 		if (!copy_from_iter_full(data, data_size, iter) ||
907 		    intel_microcode_sanity_check(mc, true, MC_HEADER_TYPE_MICROCODE) < 0)
908 			goto fail;
909 
910 		if (cur_rev >= mc_header.rev)
911 			continue;
912 
913 		if (!intel_find_matching_signature(mc, &uci->cpu_sig))
914 			continue;
915 
916 		is_safe = ucode_validate_minrev(&mc_header);
917 		if (force_minrev && !is_safe)
918 			continue;
919 
920 		kvfree(new_mc);
921 		cur_rev = mc_header.rev;
922 		new_mc  = mc;
923 		new_is_safe = is_safe;
924 		mc = NULL;
925 	}
926 
927 	if (iov_iter_count(iter))
928 		goto fail;
929 
930 	kvfree(mc);
931 	if (!new_mc)
932 		return UCODE_NFOUND;
933 
934 	ucode_patch_late = (struct microcode_intel *)new_mc;
935 	return new_is_safe ? UCODE_NEW_SAFE : UCODE_NEW;
936 
937 fail:
938 	kvfree(mc);
939 	kvfree(new_mc);
940 	return UCODE_ERROR;
941 }
942 
943 static bool is_blacklisted(unsigned int cpu)
944 {
945 	struct cpuinfo_x86 *c = &cpu_data(cpu);
946 
947 	/*
948 	 * Late loading on model 79 with microcode revision less than 0x0b000021
949 	 * and LLC size per core bigger than 2.5MB may result in a system hang.
950 	 * This behavior is documented in item BDX90, #334165 (Intel Xeon
951 	 * Processor E7-8800/4800 v4 Product Family).
952 	 */
953 	if (c->x86_vfm == INTEL_BROADWELL_X &&
954 	    c->x86_stepping == 0x01 &&
955 	    llc_size_per_core > 2621440 &&
956 	    c->microcode < 0x0b000021) {
957 		pr_err_once("Erratum BDX90: late loading with revision < 0x0b000021 (0x%x) disabled.\n", c->microcode);
958 		pr_err_once("Please consider either early loading through initrd/built-in or a potential BIOS update.\n");
959 		return true;
960 	}
961 
962 	return false;
963 }
964 
965 static enum ucode_state request_microcode_fw(int cpu, struct device *device)
966 {
967 	struct cpuinfo_x86 *c = &cpu_data(cpu);
968 	const struct firmware *firmware;
969 	struct iov_iter iter;
970 	enum ucode_state ret;
971 	struct kvec kvec;
972 	char name[30];
973 
974 	if (is_blacklisted(cpu))
975 		return UCODE_NFOUND;
976 
977 	sprintf(name, "intel-ucode/%02x-%02x-%02x",
978 		c->x86, c->x86_model, c->x86_stepping);
979 
980 	if (request_firmware_direct(&firmware, name, device)) {
981 		pr_debug("data file %s load failed\n", name);
982 		return UCODE_NFOUND;
983 	}
984 
985 	kvec.iov_base = (void *)firmware->data;
986 	kvec.iov_len = firmware->size;
987 	iov_iter_kvec(&iter, ITER_SOURCE, &kvec, 1, firmware->size);
988 	ret = parse_microcode_blobs(cpu, &iter);
989 
990 	release_firmware(firmware);
991 
992 	return ret;
993 }
994 
995 static void finalize_late_load(int result)
996 {
997 	if (!result)
998 		update_ucode_pointer(ucode_patch_late);
999 	else
1000 		kvfree(ucode_patch_late);
1001 	ucode_patch_late = NULL;
1002 }
1003 
1004 static struct microcode_ops microcode_intel_ops = {
1005 	.request_microcode_fw	= request_microcode_fw,
1006 	.collect_cpu_info	= collect_cpu_info,
1007 	.apply_microcode	= apply_microcode_late,
1008 	.finalize_late_load	= finalize_late_load,
1009 	.stage_microcode	= stage_microcode,
1010 	.use_nmi		= IS_ENABLED(CONFIG_X86_64),
1011 };
1012 
1013 static __init void calc_llc_size_per_core(struct cpuinfo_x86 *c)
1014 {
1015 	u64 llc_size = c->x86_cache_size * 1024ULL;
1016 
1017 	do_div(llc_size, topology_num_cores_per_package());
1018 	llc_size_per_core = (unsigned int)llc_size;
1019 }
1020 
1021 static __init bool staging_available(void)
1022 {
1023 	u64 val;
1024 
1025 	val = x86_read_arch_cap_msr();
1026 	if (!(val & ARCH_CAP_MCU_ENUM))
1027 		return false;
1028 
1029 	rdmsrq(MSR_IA32_MCU_ENUMERATION, val);
1030 	return !!(val & MCU_STAGING);
1031 }
1032 
1033 struct microcode_ops * __init init_intel_microcode(void)
1034 {
1035 	struct cpuinfo_x86 *c = &boot_cpu_data;
1036 
1037 	if (c->x86_vendor != X86_VENDOR_INTEL || c->x86 < 6 ||
1038 	    cpu_has(c, X86_FEATURE_IA64)) {
1039 		pr_err("Intel CPU family 0x%x not supported\n", c->x86);
1040 		return NULL;
1041 	}
1042 
1043 	if (staging_available()) {
1044 		microcode_intel_ops.use_staging = true;
1045 		pr_info("Enabled staging feature.\n");
1046 	}
1047 
1048 	calc_llc_size_per_core(c);
1049 
1050 	return &microcode_intel_ops;
1051 }
1052