amd.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * AMD CPU Microcode Update Driver for Linux
  4. *
  5. * This driver allows to upgrade microcode on F10h AMD
  6. * CPUs and later.
  7. *
  8. * Copyright (C) 2008-2011 Advanced Micro Devices Inc.
  9. * 2013-2018 Borislav Petkov <bp@alien8.de>
  10. *
  11. * Author: Peter Oruba <peter.oruba@amd.com>
  12. *
  13. * Based on work by:
  14. * Tigran Aivazian <aivazian.tigran@gmail.com>
  15. *
  16. * early loader:
  17. * Copyright (C) 2013 Advanced Micro Devices, Inc.
  18. *
  19. * Author: Jacob Shin <jacob.shin@amd.com>
  20. * Fixes: Borislav Petkov <bp@suse.de>
  21. */
  22. #define pr_fmt(fmt) "microcode: " fmt
  23. #include <linux/earlycpio.h>
  24. #include <linux/firmware.h>
  25. #include <linux/bsearch.h>
  26. #include <linux/uaccess.h>
  27. #include <linux/vmalloc.h>
  28. #include <linux/initrd.h>
  29. #include <linux/kernel.h>
  30. #include <linux/pci.h>
  31. #include <crypto/sha2.h>
  32. #include <asm/microcode.h>
  33. #include <asm/processor.h>
  34. #include <asm/cmdline.h>
  35. #include <asm/setup.h>
  36. #include <asm/cpu.h>
  37. #include <asm/msr.h>
  38. #include <asm/tlb.h>
  39. #include "internal.h"
  40. struct ucode_patch {
  41. struct list_head plist;
  42. void *data;
  43. unsigned int size;
  44. u32 patch_id;
  45. u16 equiv_cpu;
  46. };
  47. static LIST_HEAD(microcode_cache);
  48. #define UCODE_MAGIC 0x00414d44
  49. #define UCODE_EQUIV_CPU_TABLE_TYPE 0x00000000
  50. #define UCODE_UCODE_TYPE 0x00000001
  51. #define SECTION_HDR_SIZE 8
  52. #define CONTAINER_HDR_SZ 12
  53. struct equiv_cpu_entry {
  54. u32 installed_cpu;
  55. u32 fixed_errata_mask;
  56. u32 fixed_errata_compare;
  57. u16 equiv_cpu;
  58. u16 res;
  59. } __packed;
  60. struct microcode_header_amd {
  61. u32 data_code;
  62. u32 patch_id;
  63. u16 mc_patch_data_id;
  64. u8 mc_patch_data_len;
  65. u8 init_flag;
  66. u32 mc_patch_data_checksum;
  67. u32 nb_dev_id;
  68. u32 sb_dev_id;
  69. u16 processor_rev_id;
  70. u8 nb_rev_id;
  71. u8 sb_rev_id;
  72. u8 bios_api_rev;
  73. u8 reserved1[3];
  74. u32 match_reg[8];
  75. } __packed;
  76. struct microcode_amd {
  77. struct microcode_header_amd hdr;
  78. unsigned int mpb[];
  79. };
  80. static struct equiv_cpu_table {
  81. unsigned int num_entries;
  82. struct equiv_cpu_entry *entry;
  83. } equiv_table;
  84. union cpuid_1_eax {
  85. struct {
  86. __u32 stepping : 4,
  87. model : 4,
  88. family : 4,
  89. __reserved0 : 4,
  90. ext_model : 4,
  91. ext_fam : 8,
  92. __reserved1 : 4;
  93. };
  94. __u32 full;
  95. };
  96. /*
  97. * This points to the current valid container of microcode patches which we will
  98. * save from the initrd/builtin before jettisoning its contents. @mc is the
  99. * microcode patch we found to match.
  100. */
  101. struct cont_desc {
  102. struct microcode_amd *mc;
  103. u32 psize;
  104. u8 *data;
  105. size_t size;
  106. };
  107. /*
  108. * Microcode patch container file is prepended to the initrd in cpio
  109. * format. See Documentation/arch/x86/microcode.rst
  110. */
  111. static const char
  112. ucode_path[] __maybe_unused = "kernel/x86/microcode/AuthenticAMD.bin";
  113. /*
  114. * This is CPUID(1).EAX on the BSP. It is used in two ways:
  115. *
  116. * 1. To ignore the equivalence table on Zen1 and newer.
  117. *
  118. * 2. To match which patches to load because the patch revision ID
  119. * already contains the f/m/s for which the microcode is destined
  120. * for.
  121. */
  122. static u32 bsp_cpuid_1_eax __ro_after_init;
  123. static bool sha_check = true;
  124. struct patch_digest {
  125. u32 patch_id;
  126. u8 sha256[SHA256_DIGEST_SIZE];
  127. };
  128. #include "amd_shas.c"
  129. static int cmp_id(const void *key, const void *elem)
  130. {
  131. struct patch_digest *pd = (struct patch_digest *)elem;
  132. u32 patch_id = *(u32 *)key;
  133. if (patch_id == pd->patch_id)
  134. return 0;
  135. else if (patch_id < pd->patch_id)
  136. return -1;
  137. else
  138. return 1;
  139. }
  140. static u32 cpuid_to_ucode_rev(unsigned int val)
  141. {
  142. union zen_patch_rev p = {};
  143. union cpuid_1_eax c;
  144. c.full = val;
  145. p.stepping = c.stepping;
  146. p.model = c.model;
  147. p.ext_model = c.ext_model;
  148. p.ext_fam = c.ext_fam;
  149. return p.ucode_rev;
  150. }
  151. static bool need_sha_check(u32 cur_rev)
  152. {
  153. if (!cur_rev) {
  154. cur_rev = cpuid_to_ucode_rev(bsp_cpuid_1_eax);
  155. pr_info_once("No current revision, generating the lowest one: 0x%x\n", cur_rev);
  156. }
  157. switch (cur_rev >> 8) {
  158. case 0x80012: return cur_rev <= 0x8001277; break;
  159. case 0x80082: return cur_rev <= 0x800820f; break;
  160. case 0x83010: return cur_rev <= 0x830107c; break;
  161. case 0x86001: return cur_rev <= 0x860010e; break;
  162. case 0x86081: return cur_rev <= 0x8608108; break;
  163. case 0x87010: return cur_rev <= 0x8701034; break;
  164. case 0x8a000: return cur_rev <= 0x8a0000a; break;
  165. case 0xa0010: return cur_rev <= 0xa00107a; break;
  166. case 0xa0011: return cur_rev <= 0xa0011da; break;
  167. case 0xa0012: return cur_rev <= 0xa001243; break;
  168. case 0xa0082: return cur_rev <= 0xa00820e; break;
  169. case 0xa1011: return cur_rev <= 0xa101153; break;
  170. case 0xa1012: return cur_rev <= 0xa10124e; break;
  171. case 0xa1081: return cur_rev <= 0xa108109; break;
  172. case 0xa2010: return cur_rev <= 0xa20102f; break;
  173. case 0xa2012: return cur_rev <= 0xa201212; break;
  174. case 0xa4041: return cur_rev <= 0xa404109; break;
  175. case 0xa5000: return cur_rev <= 0xa500013; break;
  176. case 0xa6012: return cur_rev <= 0xa60120a; break;
  177. case 0xa7041: return cur_rev <= 0xa704109; break;
  178. case 0xa7052: return cur_rev <= 0xa705208; break;
  179. case 0xa7080: return cur_rev <= 0xa708009; break;
  180. case 0xa70c0: return cur_rev <= 0xa70C009; break;
  181. case 0xaa001: return cur_rev <= 0xaa00116; break;
  182. case 0xaa002: return cur_rev <= 0xaa00218; break;
  183. case 0xb0021: return cur_rev <= 0xb002146; break;
  184. case 0xb1010: return cur_rev <= 0xb101046; break;
  185. case 0xb2040: return cur_rev <= 0xb204031; break;
  186. case 0xb4040: return cur_rev <= 0xb404031; break;
  187. case 0xb6000: return cur_rev <= 0xb600031; break;
  188. case 0xb7000: return cur_rev <= 0xb700031; break;
  189. default: break;
  190. }
  191. pr_info("You should not be seeing this. Please send the following couple of lines to x86-<at>-kernel.org\n");
  192. pr_info("CPUID(1).EAX: 0x%x, current revision: 0x%x\n", bsp_cpuid_1_eax, cur_rev);
  193. return true;
  194. }
  195. static bool verify_sha256_digest(u32 patch_id, u32 cur_rev, const u8 *data, unsigned int len)
  196. {
  197. struct patch_digest *pd = NULL;
  198. u8 digest[SHA256_DIGEST_SIZE];
  199. struct sha256_state s;
  200. int i;
  201. if (x86_family(bsp_cpuid_1_eax) < 0x17)
  202. return true;
  203. if (!need_sha_check(cur_rev))
  204. return true;
  205. if (!sha_check)
  206. return true;
  207. pd = bsearch(&patch_id, phashes, ARRAY_SIZE(phashes), sizeof(struct patch_digest), cmp_id);
  208. if (!pd) {
  209. pr_err("No sha256 digest for patch ID: 0x%x found\n", patch_id);
  210. return false;
  211. }
  212. sha256_init(&s);
  213. sha256_update(&s, data, len);
  214. sha256_final(&s, digest);
  215. if (memcmp(digest, pd->sha256, sizeof(digest))) {
  216. pr_err("Patch 0x%x SHA256 digest mismatch!\n", patch_id);
  217. for (i = 0; i < SHA256_DIGEST_SIZE; i++)
  218. pr_cont("0x%x ", digest[i]);
  219. pr_info("\n");
  220. return false;
  221. }
  222. return true;
  223. }
  224. static u32 get_patch_level(void)
  225. {
  226. u32 rev, dummy __always_unused;
  227. native_rdmsr(MSR_AMD64_PATCH_LEVEL, rev, dummy);
  228. return rev;
  229. }
  230. static union cpuid_1_eax ucode_rev_to_cpuid(unsigned int val)
  231. {
  232. union zen_patch_rev p;
  233. union cpuid_1_eax c;
  234. p.ucode_rev = val;
  235. c.full = 0;
  236. c.stepping = p.stepping;
  237. c.model = p.model;
  238. c.ext_model = p.ext_model;
  239. c.family = 0xf;
  240. c.ext_fam = p.ext_fam;
  241. return c;
  242. }
  243. static u16 find_equiv_id(struct equiv_cpu_table *et, u32 sig)
  244. {
  245. unsigned int i;
  246. /* Zen and newer do not need an equivalence table. */
  247. if (x86_family(bsp_cpuid_1_eax) >= 0x17)
  248. return 0;
  249. if (!et || !et->num_entries)
  250. return 0;
  251. for (i = 0; i < et->num_entries; i++) {
  252. struct equiv_cpu_entry *e = &et->entry[i];
  253. if (sig == e->installed_cpu)
  254. return e->equiv_cpu;
  255. }
  256. return 0;
  257. }
  258. /*
  259. * Check whether there is a valid microcode container file at the beginning
  260. * of @buf of size @buf_size.
  261. */
  262. static bool verify_container(const u8 *buf, size_t buf_size)
  263. {
  264. u32 cont_magic;
  265. if (buf_size <= CONTAINER_HDR_SZ) {
  266. pr_debug("Truncated microcode container header.\n");
  267. return false;
  268. }
  269. cont_magic = *(const u32 *)buf;
  270. if (cont_magic != UCODE_MAGIC) {
  271. pr_debug("Invalid magic value (0x%08x).\n", cont_magic);
  272. return false;
  273. }
  274. return true;
  275. }
  276. /*
  277. * Check whether there is a valid, non-truncated CPU equivalence table at the
  278. * beginning of @buf of size @buf_size.
  279. */
  280. static bool verify_equivalence_table(const u8 *buf, size_t buf_size)
  281. {
  282. const u32 *hdr = (const u32 *)buf;
  283. u32 cont_type, equiv_tbl_len;
  284. if (!verify_container(buf, buf_size))
  285. return false;
  286. /* Zen and newer do not need an equivalence table. */
  287. if (x86_family(bsp_cpuid_1_eax) >= 0x17)
  288. return true;
  289. cont_type = hdr[1];
  290. if (cont_type != UCODE_EQUIV_CPU_TABLE_TYPE) {
  291. pr_debug("Wrong microcode container equivalence table type: %u.\n",
  292. cont_type);
  293. return false;
  294. }
  295. buf_size -= CONTAINER_HDR_SZ;
  296. equiv_tbl_len = hdr[2];
  297. if (equiv_tbl_len < sizeof(struct equiv_cpu_entry) ||
  298. buf_size < equiv_tbl_len) {
  299. pr_debug("Truncated equivalence table.\n");
  300. return false;
  301. }
  302. return true;
  303. }
  304. /*
  305. * Check whether there is a valid, non-truncated microcode patch section at the
  306. * beginning of @buf of size @buf_size.
  307. *
  308. * On success, @sh_psize returns the patch size according to the section header,
  309. * to the caller.
  310. */
  311. static bool __verify_patch_section(const u8 *buf, size_t buf_size, u32 *sh_psize)
  312. {
  313. u32 p_type, p_size;
  314. const u32 *hdr;
  315. if (buf_size < SECTION_HDR_SIZE) {
  316. pr_debug("Truncated patch section.\n");
  317. return false;
  318. }
  319. hdr = (const u32 *)buf;
  320. p_type = hdr[0];
  321. p_size = hdr[1];
  322. if (p_type != UCODE_UCODE_TYPE) {
  323. pr_debug("Invalid type field (0x%x) in container file section header.\n",
  324. p_type);
  325. return false;
  326. }
  327. if (p_size < sizeof(struct microcode_header_amd)) {
  328. pr_debug("Patch of size %u too short.\n", p_size);
  329. return false;
  330. }
  331. *sh_psize = p_size;
  332. return true;
  333. }
  334. /*
  335. * Check whether the passed remaining file @buf_size is large enough to contain
  336. * a patch of the indicated @sh_psize (and also whether this size does not
  337. * exceed the per-family maximum). @sh_psize is the size read from the section
  338. * header.
  339. */
  340. static unsigned int __verify_patch_size(u32 sh_psize, size_t buf_size)
  341. {
  342. u8 family = x86_family(bsp_cpuid_1_eax);
  343. u32 max_size;
  344. if (family >= 0x15)
  345. return min_t(u32, sh_psize, buf_size);
  346. #define F1XH_MPB_MAX_SIZE 2048
  347. #define F14H_MPB_MAX_SIZE 1824
  348. switch (family) {
  349. case 0x10 ... 0x12:
  350. max_size = F1XH_MPB_MAX_SIZE;
  351. break;
  352. case 0x14:
  353. max_size = F14H_MPB_MAX_SIZE;
  354. break;
  355. default:
  356. WARN(1, "%s: WTF family: 0x%x\n", __func__, family);
  357. return 0;
  358. }
  359. if (sh_psize > min_t(u32, buf_size, max_size))
  360. return 0;
  361. return sh_psize;
  362. }
  363. /*
  364. * Verify the patch in @buf.
  365. *
  366. * Returns:
  367. * negative: on error
  368. * positive: patch is not for this family, skip it
  369. * 0: success
  370. */
  371. static int verify_patch(const u8 *buf, size_t buf_size, u32 *patch_size)
  372. {
  373. u8 family = x86_family(bsp_cpuid_1_eax);
  374. struct microcode_header_amd *mc_hdr;
  375. unsigned int ret;
  376. u32 sh_psize;
  377. u16 proc_id;
  378. u8 patch_fam;
  379. if (!__verify_patch_section(buf, buf_size, &sh_psize))
  380. return -1;
  381. /*
  382. * The section header length is not included in this indicated size
  383. * but is present in the leftover file length so we need to subtract
  384. * it before passing this value to the function below.
  385. */
  386. buf_size -= SECTION_HDR_SIZE;
  387. /*
  388. * Check if the remaining buffer is big enough to contain a patch of
  389. * size sh_psize, as the section claims.
  390. */
  391. if (buf_size < sh_psize) {
  392. pr_debug("Patch of size %u truncated.\n", sh_psize);
  393. return -1;
  394. }
  395. ret = __verify_patch_size(sh_psize, buf_size);
  396. if (!ret) {
  397. pr_debug("Per-family patch size mismatch.\n");
  398. return -1;
  399. }
  400. *patch_size = sh_psize;
  401. mc_hdr = (struct microcode_header_amd *)(buf + SECTION_HDR_SIZE);
  402. if (mc_hdr->nb_dev_id || mc_hdr->sb_dev_id) {
  403. pr_err("Patch-ID 0x%08x: chipset-specific code unsupported.\n", mc_hdr->patch_id);
  404. return -1;
  405. }
  406. proc_id = mc_hdr->processor_rev_id;
  407. patch_fam = 0xf + (proc_id >> 12);
  408. if (patch_fam != family)
  409. return 1;
  410. return 0;
  411. }
  412. static bool mc_patch_matches(struct microcode_amd *mc, u16 eq_id)
  413. {
  414. /* Zen and newer do not need an equivalence table. */
  415. if (x86_family(bsp_cpuid_1_eax) >= 0x17)
  416. return ucode_rev_to_cpuid(mc->hdr.patch_id).full == bsp_cpuid_1_eax;
  417. else
  418. return eq_id == mc->hdr.processor_rev_id;
  419. }
  420. /*
  421. * This scans the ucode blob for the proper container as we can have multiple
  422. * containers glued together. Returns the equivalence ID from the equivalence
  423. * table or 0 if none found.
  424. * Returns the amount of bytes consumed while scanning. @desc contains all the
  425. * data we're going to use in later stages of the application.
  426. */
  427. static size_t parse_container(u8 *ucode, size_t size, struct cont_desc *desc)
  428. {
  429. struct equiv_cpu_table table;
  430. size_t orig_size = size;
  431. u32 *hdr = (u32 *)ucode;
  432. u16 eq_id;
  433. u8 *buf;
  434. if (!verify_equivalence_table(ucode, size))
  435. return 0;
  436. buf = ucode;
  437. table.entry = (struct equiv_cpu_entry *)(buf + CONTAINER_HDR_SZ);
  438. table.num_entries = hdr[2] / sizeof(struct equiv_cpu_entry);
  439. /*
  440. * Find the equivalence ID of our CPU in this table. Even if this table
  441. * doesn't contain a patch for the CPU, scan through the whole container
  442. * so that it can be skipped in case there are other containers appended.
  443. */
  444. eq_id = find_equiv_id(&table, bsp_cpuid_1_eax);
  445. buf += hdr[2] + CONTAINER_HDR_SZ;
  446. size -= hdr[2] + CONTAINER_HDR_SZ;
  447. /*
  448. * Scan through the rest of the container to find where it ends. We do
  449. * some basic sanity-checking too.
  450. */
  451. while (size > 0) {
  452. struct microcode_amd *mc;
  453. u32 patch_size;
  454. int ret;
  455. ret = verify_patch(buf, size, &patch_size);
  456. if (ret < 0) {
  457. /*
  458. * Patch verification failed, skip to the next container, if
  459. * there is one. Before exit, check whether that container has
  460. * found a patch already. If so, use it.
  461. */
  462. goto out;
  463. } else if (ret > 0) {
  464. goto skip;
  465. }
  466. mc = (struct microcode_amd *)(buf + SECTION_HDR_SIZE);
  467. if (mc_patch_matches(mc, eq_id)) {
  468. desc->psize = patch_size;
  469. desc->mc = mc;
  470. }
  471. skip:
  472. /* Skip patch section header too: */
  473. buf += patch_size + SECTION_HDR_SIZE;
  474. size -= patch_size + SECTION_HDR_SIZE;
  475. }
  476. out:
  477. /*
  478. * If we have found a patch (desc->mc), it means we're looking at the
  479. * container which has a patch for this CPU so return 0 to mean, @ucode
  480. * already points to the proper container. Otherwise, we return the size
  481. * we scanned so that we can advance to the next container in the
  482. * buffer.
  483. */
  484. if (desc->mc) {
  485. desc->data = ucode;
  486. desc->size = orig_size - size;
  487. return 0;
  488. }
  489. return orig_size - size;
  490. }
  491. /*
  492. * Scan the ucode blob for the proper container as we can have multiple
  493. * containers glued together.
  494. */
  495. static void scan_containers(u8 *ucode, size_t size, struct cont_desc *desc)
  496. {
  497. while (size) {
  498. size_t s = parse_container(ucode, size, desc);
  499. if (!s)
  500. return;
  501. /* catch wraparound */
  502. if (size >= s) {
  503. ucode += s;
  504. size -= s;
  505. } else {
  506. return;
  507. }
  508. }
  509. }
  510. static bool __apply_microcode_amd(struct microcode_amd *mc, u32 *cur_rev,
  511. unsigned int psize)
  512. {
  513. unsigned long p_addr = (unsigned long)&mc->hdr.data_code;
  514. if (!verify_sha256_digest(mc->hdr.patch_id, *cur_rev, (const u8 *)p_addr, psize))
  515. return false;
  516. native_wrmsrl(MSR_AMD64_PATCH_LOADER, p_addr);
  517. if (x86_family(bsp_cpuid_1_eax) == 0x17) {
  518. unsigned long p_addr_end = p_addr + psize - 1;
  519. invlpg(p_addr);
  520. /*
  521. * Flush next page too if patch image is crossing a page
  522. * boundary.
  523. */
  524. if (p_addr >> PAGE_SHIFT != p_addr_end >> PAGE_SHIFT)
  525. invlpg(p_addr_end);
  526. }
  527. /* verify patch application was successful */
  528. *cur_rev = get_patch_level();
  529. if (*cur_rev != mc->hdr.patch_id)
  530. return false;
  531. return true;
  532. }
  533. static bool get_builtin_microcode(struct cpio_data *cp)
  534. {
  535. char fw_name[36] = "amd-ucode/microcode_amd.bin";
  536. u8 family = x86_family(bsp_cpuid_1_eax);
  537. struct firmware fw;
  538. if (IS_ENABLED(CONFIG_X86_32))
  539. return false;
  540. if (family >= 0x15)
  541. snprintf(fw_name, sizeof(fw_name),
  542. "amd-ucode/microcode_amd_fam%02hhxh.bin", family);
  543. if (firmware_request_builtin(&fw, fw_name)) {
  544. cp->size = fw.size;
  545. cp->data = (void *)fw.data;
  546. return true;
  547. }
  548. return false;
  549. }
  550. static bool __init find_blobs_in_containers(struct cpio_data *ret)
  551. {
  552. struct cpio_data cp;
  553. bool found;
  554. if (!get_builtin_microcode(&cp))
  555. cp = find_microcode_in_initrd(ucode_path);
  556. found = cp.data && cp.size;
  557. if (found)
  558. *ret = cp;
  559. return found;
  560. }
  561. /*
  562. * Early load occurs before we can vmalloc(). So we look for the microcode
  563. * patch container file in initrd, traverse equivalent cpu table, look for a
  564. * matching microcode patch, and update, all in initrd memory in place.
  565. * When vmalloc() is available for use later -- on 64-bit during first AP load,
  566. * and on 32-bit during save_microcode_in_initrd() -- we can call
  567. * load_microcode_amd() to save equivalent cpu table and microcode patches in
  568. * kernel heap memory.
  569. */
  570. void __init load_ucode_amd_bsp(struct early_load_data *ed, unsigned int cpuid_1_eax)
  571. {
  572. struct cont_desc desc = { };
  573. struct microcode_amd *mc;
  574. struct cpio_data cp = { };
  575. char buf[4];
  576. u32 rev;
  577. if (cmdline_find_option(boot_command_line, "microcode.amd_sha_check", buf, 4)) {
  578. if (!strncmp(buf, "off", 3)) {
  579. sha_check = false;
  580. pr_warn_once("It is a very very bad idea to disable the blobs SHA check!\n");
  581. add_taint(TAINT_CPU_OUT_OF_SPEC, LOCKDEP_STILL_OK);
  582. }
  583. }
  584. bsp_cpuid_1_eax = cpuid_1_eax;
  585. rev = get_patch_level();
  586. ed->old_rev = rev;
  587. /* Needed in load_microcode_amd() */
  588. ucode_cpu_info[0].cpu_sig.sig = cpuid_1_eax;
  589. if (!find_blobs_in_containers(&cp))
  590. return;
  591. scan_containers(cp.data, cp.size, &desc);
  592. mc = desc.mc;
  593. if (!mc)
  594. return;
  595. /*
  596. * Allow application of the same revision to pick up SMT-specific
  597. * changes even if the revision of the other SMT thread is already
  598. * up-to-date.
  599. */
  600. if (ed->old_rev > mc->hdr.patch_id)
  601. return;
  602. if (__apply_microcode_amd(mc, &rev, desc.psize))
  603. ed->new_rev = rev;
  604. }
  605. static inline bool patch_cpus_equivalent(struct ucode_patch *p,
  606. struct ucode_patch *n,
  607. bool ignore_stepping)
  608. {
  609. /* Zen and newer hardcode the f/m/s in the patch ID */
  610. if (x86_family(bsp_cpuid_1_eax) >= 0x17) {
  611. union cpuid_1_eax p_cid = ucode_rev_to_cpuid(p->patch_id);
  612. union cpuid_1_eax n_cid = ucode_rev_to_cpuid(n->patch_id);
  613. if (ignore_stepping) {
  614. p_cid.stepping = 0;
  615. n_cid.stepping = 0;
  616. }
  617. return p_cid.full == n_cid.full;
  618. } else {
  619. return p->equiv_cpu == n->equiv_cpu;
  620. }
  621. }
  622. /*
  623. * a small, trivial cache of per-family ucode patches
  624. */
  625. static struct ucode_patch *cache_find_patch(struct ucode_cpu_info *uci, u16 equiv_cpu)
  626. {
  627. struct ucode_patch *p;
  628. struct ucode_patch n;
  629. n.equiv_cpu = equiv_cpu;
  630. n.patch_id = uci->cpu_sig.rev;
  631. list_for_each_entry(p, &microcode_cache, plist)
  632. if (patch_cpus_equivalent(p, &n, false))
  633. return p;
  634. return NULL;
  635. }
  636. static inline int patch_newer(struct ucode_patch *p, struct ucode_patch *n)
  637. {
  638. /* Zen and newer hardcode the f/m/s in the patch ID */
  639. if (x86_family(bsp_cpuid_1_eax) >= 0x17) {
  640. union zen_patch_rev zp, zn;
  641. zp.ucode_rev = p->patch_id;
  642. zn.ucode_rev = n->patch_id;
  643. if (zn.stepping != zp.stepping)
  644. return -1;
  645. return zn.rev > zp.rev;
  646. } else {
  647. return n->patch_id > p->patch_id;
  648. }
  649. }
  650. static void update_cache(struct ucode_patch *new_patch)
  651. {
  652. struct ucode_patch *p;
  653. int ret;
  654. list_for_each_entry(p, &microcode_cache, plist) {
  655. if (patch_cpus_equivalent(p, new_patch, true)) {
  656. ret = patch_newer(p, new_patch);
  657. if (ret < 0)
  658. continue;
  659. else if (!ret) {
  660. /* we already have the latest patch */
  661. kfree(new_patch->data);
  662. kfree(new_patch);
  663. return;
  664. }
  665. list_replace(&p->plist, &new_patch->plist);
  666. kfree(p->data);
  667. kfree(p);
  668. return;
  669. }
  670. }
  671. /* no patch found, add it */
  672. list_add_tail(&new_patch->plist, &microcode_cache);
  673. }
  674. static void free_cache(void)
  675. {
  676. struct ucode_patch *p, *tmp;
  677. list_for_each_entry_safe(p, tmp, &microcode_cache, plist) {
  678. __list_del(p->plist.prev, p->plist.next);
  679. kfree(p->data);
  680. kfree(p);
  681. }
  682. }
  683. static struct ucode_patch *find_patch(unsigned int cpu)
  684. {
  685. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  686. u16 equiv_id = 0;
  687. uci->cpu_sig.rev = get_patch_level();
  688. if (x86_family(bsp_cpuid_1_eax) < 0x17) {
  689. equiv_id = find_equiv_id(&equiv_table, uci->cpu_sig.sig);
  690. if (!equiv_id)
  691. return NULL;
  692. }
  693. return cache_find_patch(uci, equiv_id);
  694. }
  695. void reload_ucode_amd(unsigned int cpu)
  696. {
  697. u32 rev, dummy __always_unused;
  698. struct microcode_amd *mc;
  699. struct ucode_patch *p;
  700. p = find_patch(cpu);
  701. if (!p)
  702. return;
  703. mc = p->data;
  704. rev = get_patch_level();
  705. if (rev < mc->hdr.patch_id) {
  706. if (__apply_microcode_amd(mc, &rev, p->size))
  707. pr_info_once("reload revision: 0x%08x\n", rev);
  708. }
  709. }
  710. static int collect_cpu_info_amd(int cpu, struct cpu_signature *csig)
  711. {
  712. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  713. struct ucode_patch *p;
  714. csig->sig = cpuid_eax(0x00000001);
  715. csig->rev = get_patch_level();
  716. /*
  717. * a patch could have been loaded early, set uci->mc so that
  718. * mc_bp_resume() can call apply_microcode()
  719. */
  720. p = find_patch(cpu);
  721. if (p && (p->patch_id == csig->rev))
  722. uci->mc = p->data;
  723. return 0;
  724. }
  725. static enum ucode_state apply_microcode_amd(int cpu)
  726. {
  727. struct cpuinfo_x86 *c = &cpu_data(cpu);
  728. struct microcode_amd *mc_amd;
  729. struct ucode_cpu_info *uci;
  730. struct ucode_patch *p;
  731. enum ucode_state ret;
  732. u32 rev;
  733. BUG_ON(raw_smp_processor_id() != cpu);
  734. uci = ucode_cpu_info + cpu;
  735. p = find_patch(cpu);
  736. if (!p)
  737. return UCODE_NFOUND;
  738. rev = uci->cpu_sig.rev;
  739. mc_amd = p->data;
  740. uci->mc = p->data;
  741. /* need to apply patch? */
  742. if (rev > mc_amd->hdr.patch_id) {
  743. ret = UCODE_OK;
  744. goto out;
  745. }
  746. if (!__apply_microcode_amd(mc_amd, &rev, p->size)) {
  747. pr_err("CPU%d: update failed for patch_level=0x%08x\n",
  748. cpu, mc_amd->hdr.patch_id);
  749. return UCODE_ERROR;
  750. }
  751. rev = mc_amd->hdr.patch_id;
  752. ret = UCODE_UPDATED;
  753. out:
  754. uci->cpu_sig.rev = rev;
  755. c->microcode = rev;
  756. /* Update boot_cpu_data's revision too, if we're on the BSP: */
  757. if (c->cpu_index == boot_cpu_data.cpu_index)
  758. boot_cpu_data.microcode = rev;
  759. return ret;
  760. }
  761. void load_ucode_amd_ap(unsigned int cpuid_1_eax)
  762. {
  763. unsigned int cpu = smp_processor_id();
  764. ucode_cpu_info[cpu].cpu_sig.sig = cpuid_1_eax;
  765. apply_microcode_amd(cpu);
  766. }
  767. static size_t install_equiv_cpu_table(const u8 *buf, size_t buf_size)
  768. {
  769. u32 equiv_tbl_len;
  770. const u32 *hdr;
  771. if (!verify_equivalence_table(buf, buf_size))
  772. return 0;
  773. hdr = (const u32 *)buf;
  774. equiv_tbl_len = hdr[2];
  775. /* Zen and newer do not need an equivalence table. */
  776. if (x86_family(bsp_cpuid_1_eax) >= 0x17)
  777. goto out;
  778. equiv_table.entry = vmalloc(equiv_tbl_len);
  779. if (!equiv_table.entry) {
  780. pr_err("failed to allocate equivalent CPU table\n");
  781. return 0;
  782. }
  783. memcpy(equiv_table.entry, buf + CONTAINER_HDR_SZ, equiv_tbl_len);
  784. equiv_table.num_entries = equiv_tbl_len / sizeof(struct equiv_cpu_entry);
  785. out:
  786. /* add header length */
  787. return equiv_tbl_len + CONTAINER_HDR_SZ;
  788. }
  789. static void free_equiv_cpu_table(void)
  790. {
  791. if (x86_family(bsp_cpuid_1_eax) >= 0x17)
  792. return;
  793. vfree(equiv_table.entry);
  794. memset(&equiv_table, 0, sizeof(equiv_table));
  795. }
  796. static void cleanup(void)
  797. {
  798. free_equiv_cpu_table();
  799. free_cache();
  800. }
  801. /*
  802. * Return a non-negative value even if some of the checks failed so that
  803. * we can skip over the next patch. If we return a negative value, we
  804. * signal a grave error like a memory allocation has failed and the
  805. * driver cannot continue functioning normally. In such cases, we tear
  806. * down everything we've used up so far and exit.
  807. */
  808. static int verify_and_add_patch(u8 family, u8 *fw, unsigned int leftover,
  809. unsigned int *patch_size)
  810. {
  811. struct microcode_header_amd *mc_hdr;
  812. struct ucode_patch *patch;
  813. u16 proc_id;
  814. int ret;
  815. ret = verify_patch(fw, leftover, patch_size);
  816. if (ret)
  817. return ret;
  818. patch = kzalloc(sizeof(*patch), GFP_KERNEL);
  819. if (!patch) {
  820. pr_err("Patch allocation failure.\n");
  821. return -EINVAL;
  822. }
  823. patch->data = kmemdup(fw + SECTION_HDR_SIZE, *patch_size, GFP_KERNEL);
  824. if (!patch->data) {
  825. pr_err("Patch data allocation failure.\n");
  826. kfree(patch);
  827. return -EINVAL;
  828. }
  829. patch->size = *patch_size;
  830. mc_hdr = (struct microcode_header_amd *)(fw + SECTION_HDR_SIZE);
  831. proc_id = mc_hdr->processor_rev_id;
  832. INIT_LIST_HEAD(&patch->plist);
  833. patch->patch_id = mc_hdr->patch_id;
  834. patch->equiv_cpu = proc_id;
  835. pr_debug("%s: Adding patch_id: 0x%08x, proc_id: 0x%04x\n",
  836. __func__, patch->patch_id, proc_id);
  837. /* ... and add to cache. */
  838. update_cache(patch);
  839. return 0;
  840. }
  841. /* Scan the blob in @data and add microcode patches to the cache. */
  842. static enum ucode_state __load_microcode_amd(u8 family, const u8 *data, size_t size)
  843. {
  844. u8 *fw = (u8 *)data;
  845. size_t offset;
  846. offset = install_equiv_cpu_table(data, size);
  847. if (!offset)
  848. return UCODE_ERROR;
  849. fw += offset;
  850. size -= offset;
  851. if (*(u32 *)fw != UCODE_UCODE_TYPE) {
  852. pr_err("invalid type field in container file section header\n");
  853. free_equiv_cpu_table();
  854. return UCODE_ERROR;
  855. }
  856. while (size > 0) {
  857. unsigned int crnt_size = 0;
  858. int ret;
  859. ret = verify_and_add_patch(family, fw, size, &crnt_size);
  860. if (ret < 0)
  861. return UCODE_ERROR;
  862. fw += crnt_size + SECTION_HDR_SIZE;
  863. size -= (crnt_size + SECTION_HDR_SIZE);
  864. }
  865. return UCODE_OK;
  866. }
  867. static enum ucode_state _load_microcode_amd(u8 family, const u8 *data, size_t size)
  868. {
  869. enum ucode_state ret;
  870. /* free old equiv table */
  871. free_equiv_cpu_table();
  872. ret = __load_microcode_amd(family, data, size);
  873. if (ret != UCODE_OK)
  874. cleanup();
  875. return ret;
  876. }
  877. static enum ucode_state load_microcode_amd(u8 family, const u8 *data, size_t size)
  878. {
  879. struct cpuinfo_x86 *c;
  880. unsigned int nid, cpu;
  881. struct ucode_patch *p;
  882. enum ucode_state ret;
  883. ret = _load_microcode_amd(family, data, size);
  884. if (ret != UCODE_OK)
  885. return ret;
  886. for_each_node_with_cpus(nid) {
  887. cpu = cpumask_first(cpumask_of_node(nid));
  888. c = &cpu_data(cpu);
  889. p = find_patch(cpu);
  890. if (!p)
  891. continue;
  892. if (c->microcode >= p->patch_id)
  893. continue;
  894. ret = UCODE_NEW;
  895. }
  896. return ret;
  897. }
  898. static int __init save_microcode_in_initrd(void)
  899. {
  900. struct cpuinfo_x86 *c = &boot_cpu_data;
  901. struct cont_desc desc = { 0 };
  902. unsigned int cpuid_1_eax;
  903. enum ucode_state ret;
  904. struct cpio_data cp;
  905. if (microcode_loader_disabled() || c->x86_vendor != X86_VENDOR_AMD || c->x86 < 0x10)
  906. return 0;
  907. cpuid_1_eax = native_cpuid_eax(1);
  908. if (!find_blobs_in_containers(&cp))
  909. return -EINVAL;
  910. scan_containers(cp.data, cp.size, &desc);
  911. if (!desc.mc)
  912. return -EINVAL;
  913. ret = _load_microcode_amd(x86_family(cpuid_1_eax), desc.data, desc.size);
  914. if (ret > UCODE_UPDATED)
  915. return -EINVAL;
  916. return 0;
  917. }
  918. early_initcall(save_microcode_in_initrd);
  919. /*
  920. * AMD microcode firmware naming convention, up to family 15h they are in
  921. * the legacy file:
  922. *
  923. * amd-ucode/microcode_amd.bin
  924. *
  925. * This legacy file is always smaller than 2K in size.
  926. *
  927. * Beginning with family 15h, they are in family-specific firmware files:
  928. *
  929. * amd-ucode/microcode_amd_fam15h.bin
  930. * amd-ucode/microcode_amd_fam16h.bin
  931. * ...
  932. *
  933. * These might be larger than 2K.
  934. */
  935. static enum ucode_state request_microcode_amd(int cpu, struct device *device)
  936. {
  937. char fw_name[36] = "amd-ucode/microcode_amd.bin";
  938. struct cpuinfo_x86 *c = &cpu_data(cpu);
  939. enum ucode_state ret = UCODE_NFOUND;
  940. const struct firmware *fw;
  941. if (force_minrev)
  942. return UCODE_NFOUND;
  943. if (c->x86 >= 0x15)
  944. snprintf(fw_name, sizeof(fw_name), "amd-ucode/microcode_amd_fam%.2xh.bin", c->x86);
  945. if (request_firmware_direct(&fw, (const char *)fw_name, device)) {
  946. pr_debug("failed to load file %s\n", fw_name);
  947. goto out;
  948. }
  949. ret = UCODE_ERROR;
  950. if (!verify_container(fw->data, fw->size))
  951. goto fw_release;
  952. ret = load_microcode_amd(c->x86, fw->data, fw->size);
  953. fw_release:
  954. release_firmware(fw);
  955. out:
  956. return ret;
  957. }
  958. static void microcode_fini_cpu_amd(int cpu)
  959. {
  960. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  961. uci->mc = NULL;
  962. }
  963. static struct microcode_ops microcode_amd_ops = {
  964. .request_microcode_fw = request_microcode_amd,
  965. .collect_cpu_info = collect_cpu_info_amd,
  966. .apply_microcode = apply_microcode_amd,
  967. .microcode_fini_cpu = microcode_fini_cpu_amd,
  968. .nmi_safe = true,
  969. };
  970. struct microcode_ops * __init init_amd_microcode(void)
  971. {
  972. struct cpuinfo_x86 *c = &boot_cpu_data;
  973. if (c->x86_vendor != X86_VENDOR_AMD || c->x86 < 0x10) {
  974. pr_warn("AMD CPU family 0x%x not supported\n", c->x86);
  975. return NULL;
  976. }
  977. return &microcode_amd_ops;
  978. }
  979. void __exit exit_amd_microcode(void)
  980. {
  981. cleanup();
  982. }