amd.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  1. /*
  2. * AMD CPU Microcode Update Driver for Linux
  3. *
  4. * This driver allows to upgrade microcode on F10h AMD
  5. * CPUs and later.
  6. *
  7. * Copyright (C) 2008-2011 Advanced Micro Devices Inc.
  8. * 2013-2016 Borislav Petkov <bp@alien8.de>
  9. *
  10. * Author: Peter Oruba <peter.oruba@amd.com>
  11. *
  12. * Based on work by:
  13. * Tigran Aivazian <aivazian.tigran@gmail.com>
  14. *
  15. * early loader:
  16. * Copyright (C) 2013 Advanced Micro Devices, Inc.
  17. *
  18. * Author: Jacob Shin <jacob.shin@amd.com>
  19. * 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.
  23. */
  24. #define pr_fmt(fmt) "microcode: " fmt
  25. #include <linux/earlycpio.h>
  26. #include <linux/firmware.h>
  27. #include <linux/uaccess.h>
  28. #include <linux/vmalloc.h>
  29. #include <linux/initrd.h>
  30. #include <linux/kernel.h>
  31. #include <linux/pci.h>
  32. #include <asm/microcode_amd.h>
  33. #include <asm/microcode.h>
  34. #include <asm/processor.h>
  35. #include <asm/setup.h>
  36. #include <asm/cpu.h>
  37. #include <asm/msr.h>
  38. static struct equiv_cpu_entry *equiv_cpu_table;
  39. /*
  40. * This points to the current valid container of microcode patches which we will
  41. * save from the initrd/builtin before jettisoning its contents. @mc is the
  42. * microcode patch we found to match.
  43. */
  44. struct cont_desc {
  45. struct microcode_amd *mc;
  46. u32 cpuid_1_eax;
  47. u32 psize;
  48. u8 *data;
  49. size_t size;
  50. };
  51. static u32 ucode_new_rev;
  52. static u8 amd_ucode_patch[PATCH_MAX_SIZE];
  53. /*
  54. * Microcode patch container file is prepended to the initrd in cpio
  55. * format. See Documentation/x86/early-microcode.txt
  56. */
  57. static const char
  58. ucode_path[] __maybe_unused = "kernel/x86/microcode/AuthenticAMD.bin";
  59. static u16 find_equiv_id(struct equiv_cpu_entry *equiv_table, u32 sig)
  60. {
  61. for (; equiv_table && equiv_table->installed_cpu; equiv_table++) {
  62. if (sig == equiv_table->installed_cpu)
  63. return equiv_table->equiv_cpu;
  64. }
  65. return 0;
  66. }
  67. /*
  68. * This scans the ucode blob for the proper container as we can have multiple
  69. * containers glued together. Returns the equivalence ID from the equivalence
  70. * table or 0 if none found.
  71. * Returns the amount of bytes consumed while scanning. @desc contains all the
  72. * data we're going to use in later stages of the application.
  73. */
  74. static ssize_t parse_container(u8 *ucode, ssize_t size, struct cont_desc *desc)
  75. {
  76. struct equiv_cpu_entry *eq;
  77. ssize_t orig_size = size;
  78. u32 *hdr = (u32 *)ucode;
  79. u16 eq_id;
  80. u8 *buf;
  81. /* Am I looking at an equivalence table header? */
  82. if (hdr[0] != UCODE_MAGIC ||
  83. hdr[1] != UCODE_EQUIV_CPU_TABLE_TYPE ||
  84. hdr[2] == 0)
  85. return CONTAINER_HDR_SZ;
  86. buf = ucode;
  87. eq = (struct equiv_cpu_entry *)(buf + CONTAINER_HDR_SZ);
  88. /* Find the equivalence ID of our CPU in this table: */
  89. eq_id = find_equiv_id(eq, desc->cpuid_1_eax);
  90. buf += hdr[2] + CONTAINER_HDR_SZ;
  91. size -= hdr[2] + CONTAINER_HDR_SZ;
  92. /*
  93. * Scan through the rest of the container to find where it ends. We do
  94. * some basic sanity-checking too.
  95. */
  96. while (size > 0) {
  97. struct microcode_amd *mc;
  98. u32 patch_size;
  99. hdr = (u32 *)buf;
  100. if (hdr[0] != UCODE_UCODE_TYPE)
  101. break;
  102. /* Sanity-check patch size. */
  103. patch_size = hdr[1];
  104. if (patch_size > PATCH_MAX_SIZE)
  105. break;
  106. /* Skip patch section header: */
  107. buf += SECTION_HDR_SIZE;
  108. size -= SECTION_HDR_SIZE;
  109. mc = (struct microcode_amd *)buf;
  110. if (eq_id == mc->hdr.processor_rev_id) {
  111. desc->psize = patch_size;
  112. desc->mc = mc;
  113. }
  114. buf += patch_size;
  115. size -= patch_size;
  116. }
  117. /*
  118. * If we have found a patch (desc->mc), it means we're looking at the
  119. * container which has a patch for this CPU so return 0 to mean, @ucode
  120. * already points to the proper container. Otherwise, we return the size
  121. * we scanned so that we can advance to the next container in the
  122. * buffer.
  123. */
  124. if (desc->mc) {
  125. desc->data = ucode;
  126. desc->size = orig_size - size;
  127. return 0;
  128. }
  129. return orig_size - size;
  130. }
  131. /*
  132. * Scan the ucode blob for the proper container as we can have multiple
  133. * containers glued together.
  134. */
  135. static void scan_containers(u8 *ucode, size_t size, struct cont_desc *desc)
  136. {
  137. ssize_t rem = size;
  138. while (rem >= 0) {
  139. ssize_t s = parse_container(ucode, rem, desc);
  140. if (!s)
  141. return;
  142. ucode += s;
  143. rem -= s;
  144. }
  145. }
  146. static int __apply_microcode_amd(struct microcode_amd *mc)
  147. {
  148. u32 rev, dummy;
  149. native_wrmsrl(MSR_AMD64_PATCH_LOADER, (u64)(long)&mc->hdr.data_code);
  150. /* verify patch application was successful */
  151. native_rdmsr(MSR_AMD64_PATCH_LEVEL, rev, dummy);
  152. if (rev != mc->hdr.patch_id)
  153. return -1;
  154. return 0;
  155. }
  156. /*
  157. * Early load occurs before we can vmalloc(). So we look for the microcode
  158. * patch container file in initrd, traverse equivalent cpu table, look for a
  159. * matching microcode patch, and update, all in initrd memory in place.
  160. * When vmalloc() is available for use later -- on 64-bit during first AP load,
  161. * and on 32-bit during save_microcode_in_initrd_amd() -- we can call
  162. * load_microcode_amd() to save equivalent cpu table and microcode patches in
  163. * kernel heap memory.
  164. *
  165. * Returns true if container found (sets @desc), false otherwise.
  166. */
  167. static bool
  168. apply_microcode_early_amd(u32 cpuid_1_eax, void *ucode, size_t size, bool save_patch)
  169. {
  170. struct cont_desc desc = { 0 };
  171. u8 (*patch)[PATCH_MAX_SIZE];
  172. struct microcode_amd *mc;
  173. u32 rev, dummy, *new_rev;
  174. bool ret = false;
  175. #ifdef CONFIG_X86_32
  176. new_rev = (u32 *)__pa_nodebug(&ucode_new_rev);
  177. patch = (u8 (*)[PATCH_MAX_SIZE])__pa_nodebug(&amd_ucode_patch);
  178. #else
  179. new_rev = &ucode_new_rev;
  180. patch = &amd_ucode_patch;
  181. #endif
  182. desc.cpuid_1_eax = cpuid_1_eax;
  183. scan_containers(ucode, size, &desc);
  184. mc = desc.mc;
  185. if (!mc)
  186. return ret;
  187. native_rdmsr(MSR_AMD64_PATCH_LEVEL, rev, dummy);
  188. if (rev >= mc->hdr.patch_id)
  189. return ret;
  190. if (!__apply_microcode_amd(mc)) {
  191. *new_rev = mc->hdr.patch_id;
  192. ret = true;
  193. if (save_patch)
  194. memcpy(patch, mc, min_t(u32, desc.psize, PATCH_MAX_SIZE));
  195. }
  196. return ret;
  197. }
  198. static bool get_builtin_microcode(struct cpio_data *cp, unsigned int family)
  199. {
  200. #ifdef CONFIG_X86_64
  201. char fw_name[36] = "amd-ucode/microcode_amd.bin";
  202. if (family >= 0x15)
  203. snprintf(fw_name, sizeof(fw_name),
  204. "amd-ucode/microcode_amd_fam%.2xh.bin", family);
  205. return get_builtin_firmware(cp, fw_name);
  206. #else
  207. return false;
  208. #endif
  209. }
  210. void __load_ucode_amd(unsigned int cpuid_1_eax, struct cpio_data *ret)
  211. {
  212. struct ucode_cpu_info *uci;
  213. struct cpio_data cp;
  214. const char *path;
  215. bool use_pa;
  216. if (IS_ENABLED(CONFIG_X86_32)) {
  217. uci = (struct ucode_cpu_info *)__pa_nodebug(ucode_cpu_info);
  218. path = (const char *)__pa_nodebug(ucode_path);
  219. use_pa = true;
  220. } else {
  221. uci = ucode_cpu_info;
  222. path = ucode_path;
  223. use_pa = false;
  224. }
  225. if (!get_builtin_microcode(&cp, x86_family(cpuid_1_eax)))
  226. cp = find_microcode_in_initrd(path, use_pa);
  227. /* Needed in load_microcode_amd() */
  228. uci->cpu_sig.sig = cpuid_1_eax;
  229. *ret = cp;
  230. }
  231. void __init load_ucode_amd_bsp(unsigned int cpuid_1_eax)
  232. {
  233. struct cpio_data cp = { };
  234. __load_ucode_amd(cpuid_1_eax, &cp);
  235. if (!(cp.data && cp.size))
  236. return;
  237. apply_microcode_early_amd(cpuid_1_eax, cp.data, cp.size, true);
  238. }
  239. void load_ucode_amd_ap(unsigned int cpuid_1_eax)
  240. {
  241. struct microcode_amd *mc;
  242. struct cpio_data cp;
  243. u32 *new_rev, rev, dummy;
  244. if (IS_ENABLED(CONFIG_X86_32)) {
  245. mc = (struct microcode_amd *)__pa_nodebug(amd_ucode_patch);
  246. new_rev = (u32 *)__pa_nodebug(&ucode_new_rev);
  247. } else {
  248. mc = (struct microcode_amd *)amd_ucode_patch;
  249. new_rev = &ucode_new_rev;
  250. }
  251. native_rdmsr(MSR_AMD64_PATCH_LEVEL, rev, dummy);
  252. /* Check whether we have saved a new patch already: */
  253. if (*new_rev && rev < mc->hdr.patch_id) {
  254. if (!__apply_microcode_amd(mc)) {
  255. *new_rev = mc->hdr.patch_id;
  256. return;
  257. }
  258. }
  259. __load_ucode_amd(cpuid_1_eax, &cp);
  260. if (!(cp.data && cp.size))
  261. return;
  262. apply_microcode_early_amd(cpuid_1_eax, cp.data, cp.size, false);
  263. }
  264. static enum ucode_state
  265. load_microcode_amd(bool save, u8 family, const u8 *data, size_t size);
  266. int __init save_microcode_in_initrd_amd(unsigned int cpuid_1_eax)
  267. {
  268. struct cont_desc desc = { 0 };
  269. enum ucode_state ret;
  270. struct cpio_data cp;
  271. cp = find_microcode_in_initrd(ucode_path, false);
  272. if (!(cp.data && cp.size))
  273. return -EINVAL;
  274. desc.cpuid_1_eax = cpuid_1_eax;
  275. scan_containers(cp.data, cp.size, &desc);
  276. if (!desc.mc)
  277. return -EINVAL;
  278. ret = load_microcode_amd(true, x86_family(cpuid_1_eax), desc.data, desc.size);
  279. if (ret != UCODE_OK)
  280. return -EINVAL;
  281. return 0;
  282. }
  283. void reload_ucode_amd(void)
  284. {
  285. struct microcode_amd *mc;
  286. u32 rev, dummy;
  287. mc = (struct microcode_amd *)amd_ucode_patch;
  288. rdmsr(MSR_AMD64_PATCH_LEVEL, rev, dummy);
  289. if (rev < mc->hdr.patch_id) {
  290. if (!__apply_microcode_amd(mc)) {
  291. ucode_new_rev = mc->hdr.patch_id;
  292. pr_info("reload patch_level=0x%08x\n", ucode_new_rev);
  293. }
  294. }
  295. }
  296. static u16 __find_equiv_id(unsigned int cpu)
  297. {
  298. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  299. return find_equiv_id(equiv_cpu_table, uci->cpu_sig.sig);
  300. }
  301. static u32 find_cpu_family_by_equiv_cpu(u16 equiv_cpu)
  302. {
  303. int i = 0;
  304. BUG_ON(!equiv_cpu_table);
  305. while (equiv_cpu_table[i].equiv_cpu != 0) {
  306. if (equiv_cpu == equiv_cpu_table[i].equiv_cpu)
  307. return equiv_cpu_table[i].installed_cpu;
  308. i++;
  309. }
  310. return 0;
  311. }
  312. /*
  313. * a small, trivial cache of per-family ucode patches
  314. */
  315. static struct ucode_patch *cache_find_patch(u16 equiv_cpu)
  316. {
  317. struct ucode_patch *p;
  318. list_for_each_entry(p, &microcode_cache, plist)
  319. if (p->equiv_cpu == equiv_cpu)
  320. return p;
  321. return NULL;
  322. }
  323. static void update_cache(struct ucode_patch *new_patch)
  324. {
  325. struct ucode_patch *p;
  326. list_for_each_entry(p, &microcode_cache, plist) {
  327. if (p->equiv_cpu == new_patch->equiv_cpu) {
  328. if (p->patch_id >= new_patch->patch_id)
  329. /* we already have the latest patch */
  330. return;
  331. list_replace(&p->plist, &new_patch->plist);
  332. kfree(p->data);
  333. kfree(p);
  334. return;
  335. }
  336. }
  337. /* no patch found, add it */
  338. list_add_tail(&new_patch->plist, &microcode_cache);
  339. }
  340. static void free_cache(void)
  341. {
  342. struct ucode_patch *p, *tmp;
  343. list_for_each_entry_safe(p, tmp, &microcode_cache, plist) {
  344. __list_del(p->plist.prev, p->plist.next);
  345. kfree(p->data);
  346. kfree(p);
  347. }
  348. }
  349. static struct ucode_patch *find_patch(unsigned int cpu)
  350. {
  351. u16 equiv_id;
  352. equiv_id = __find_equiv_id(cpu);
  353. if (!equiv_id)
  354. return NULL;
  355. return cache_find_patch(equiv_id);
  356. }
  357. static int collect_cpu_info_amd(int cpu, struct cpu_signature *csig)
  358. {
  359. struct cpuinfo_x86 *c = &cpu_data(cpu);
  360. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  361. struct ucode_patch *p;
  362. csig->sig = cpuid_eax(0x00000001);
  363. csig->rev = c->microcode;
  364. /*
  365. * a patch could have been loaded early, set uci->mc so that
  366. * mc_bp_resume() can call apply_microcode()
  367. */
  368. p = find_patch(cpu);
  369. if (p && (p->patch_id == csig->rev))
  370. uci->mc = p->data;
  371. pr_info("CPU%d: patch_level=0x%08x\n", cpu, csig->rev);
  372. return 0;
  373. }
  374. static unsigned int verify_patch_size(u8 family, u32 patch_size,
  375. unsigned int size)
  376. {
  377. u32 max_size;
  378. #define F1XH_MPB_MAX_SIZE 2048
  379. #define F14H_MPB_MAX_SIZE 1824
  380. #define F15H_MPB_MAX_SIZE 4096
  381. #define F16H_MPB_MAX_SIZE 3458
  382. switch (family) {
  383. case 0x14:
  384. max_size = F14H_MPB_MAX_SIZE;
  385. break;
  386. case 0x15:
  387. max_size = F15H_MPB_MAX_SIZE;
  388. break;
  389. case 0x16:
  390. max_size = F16H_MPB_MAX_SIZE;
  391. break;
  392. default:
  393. max_size = F1XH_MPB_MAX_SIZE;
  394. break;
  395. }
  396. if (patch_size > min_t(u32, size, max_size)) {
  397. pr_err("patch size mismatch\n");
  398. return 0;
  399. }
  400. return patch_size;
  401. }
  402. static int apply_microcode_amd(int cpu)
  403. {
  404. struct cpuinfo_x86 *c = &cpu_data(cpu);
  405. struct microcode_amd *mc_amd;
  406. struct ucode_cpu_info *uci;
  407. struct ucode_patch *p;
  408. u32 rev, dummy;
  409. BUG_ON(raw_smp_processor_id() != cpu);
  410. uci = ucode_cpu_info + cpu;
  411. p = find_patch(cpu);
  412. if (!p)
  413. return 0;
  414. mc_amd = p->data;
  415. uci->mc = p->data;
  416. rdmsr(MSR_AMD64_PATCH_LEVEL, rev, dummy);
  417. /* need to apply patch? */
  418. if (rev >= mc_amd->hdr.patch_id) {
  419. c->microcode = rev;
  420. uci->cpu_sig.rev = rev;
  421. return 0;
  422. }
  423. if (__apply_microcode_amd(mc_amd)) {
  424. pr_err("CPU%d: update failed for patch_level=0x%08x\n",
  425. cpu, mc_amd->hdr.patch_id);
  426. return -1;
  427. }
  428. pr_info("CPU%d: new patch_level=0x%08x\n", cpu,
  429. mc_amd->hdr.patch_id);
  430. uci->cpu_sig.rev = mc_amd->hdr.patch_id;
  431. c->microcode = mc_amd->hdr.patch_id;
  432. return 0;
  433. }
  434. static int install_equiv_cpu_table(const u8 *buf)
  435. {
  436. unsigned int *ibuf = (unsigned int *)buf;
  437. unsigned int type = ibuf[1];
  438. unsigned int size = ibuf[2];
  439. if (type != UCODE_EQUIV_CPU_TABLE_TYPE || !size) {
  440. pr_err("empty section/"
  441. "invalid type field in container file section header\n");
  442. return -EINVAL;
  443. }
  444. equiv_cpu_table = vmalloc(size);
  445. if (!equiv_cpu_table) {
  446. pr_err("failed to allocate equivalent CPU table\n");
  447. return -ENOMEM;
  448. }
  449. memcpy(equiv_cpu_table, buf + CONTAINER_HDR_SZ, size);
  450. /* add header length */
  451. return size + CONTAINER_HDR_SZ;
  452. }
  453. static void free_equiv_cpu_table(void)
  454. {
  455. vfree(equiv_cpu_table);
  456. equiv_cpu_table = NULL;
  457. }
  458. static void cleanup(void)
  459. {
  460. free_equiv_cpu_table();
  461. free_cache();
  462. }
  463. /*
  464. * We return the current size even if some of the checks failed so that
  465. * we can skip over the next patch. If we return a negative value, we
  466. * signal a grave error like a memory allocation has failed and the
  467. * driver cannot continue functioning normally. In such cases, we tear
  468. * down everything we've used up so far and exit.
  469. */
  470. static int verify_and_add_patch(u8 family, u8 *fw, unsigned int leftover)
  471. {
  472. struct microcode_header_amd *mc_hdr;
  473. struct ucode_patch *patch;
  474. unsigned int patch_size, crnt_size, ret;
  475. u32 proc_fam;
  476. u16 proc_id;
  477. patch_size = *(u32 *)(fw + 4);
  478. crnt_size = patch_size + SECTION_HDR_SIZE;
  479. mc_hdr = (struct microcode_header_amd *)(fw + SECTION_HDR_SIZE);
  480. proc_id = mc_hdr->processor_rev_id;
  481. proc_fam = find_cpu_family_by_equiv_cpu(proc_id);
  482. if (!proc_fam) {
  483. pr_err("No patch family for equiv ID: 0x%04x\n", proc_id);
  484. return crnt_size;
  485. }
  486. /* check if patch is for the current family */
  487. proc_fam = ((proc_fam >> 8) & 0xf) + ((proc_fam >> 20) & 0xff);
  488. if (proc_fam != family)
  489. return crnt_size;
  490. if (mc_hdr->nb_dev_id || mc_hdr->sb_dev_id) {
  491. pr_err("Patch-ID 0x%08x: chipset-specific code unsupported.\n",
  492. mc_hdr->patch_id);
  493. return crnt_size;
  494. }
  495. ret = verify_patch_size(family, patch_size, leftover);
  496. if (!ret) {
  497. pr_err("Patch-ID 0x%08x: size mismatch.\n", mc_hdr->patch_id);
  498. return crnt_size;
  499. }
  500. patch = kzalloc(sizeof(*patch), GFP_KERNEL);
  501. if (!patch) {
  502. pr_err("Patch allocation failure.\n");
  503. return -EINVAL;
  504. }
  505. patch->data = kmemdup(fw + SECTION_HDR_SIZE, patch_size, GFP_KERNEL);
  506. if (!patch->data) {
  507. pr_err("Patch data allocation failure.\n");
  508. kfree(patch);
  509. return -EINVAL;
  510. }
  511. INIT_LIST_HEAD(&patch->plist);
  512. patch->patch_id = mc_hdr->patch_id;
  513. patch->equiv_cpu = proc_id;
  514. pr_debug("%s: Added patch_id: 0x%08x, proc_id: 0x%04x\n",
  515. __func__, patch->patch_id, proc_id);
  516. /* ... and add to cache. */
  517. update_cache(patch);
  518. return crnt_size;
  519. }
  520. static enum ucode_state __load_microcode_amd(u8 family, const u8 *data,
  521. size_t size)
  522. {
  523. enum ucode_state ret = UCODE_ERROR;
  524. unsigned int leftover;
  525. u8 *fw = (u8 *)data;
  526. int crnt_size = 0;
  527. int offset;
  528. offset = install_equiv_cpu_table(data);
  529. if (offset < 0) {
  530. pr_err("failed to create equivalent cpu table\n");
  531. return ret;
  532. }
  533. fw += offset;
  534. leftover = size - offset;
  535. if (*(u32 *)fw != UCODE_UCODE_TYPE) {
  536. pr_err("invalid type field in container file section header\n");
  537. free_equiv_cpu_table();
  538. return ret;
  539. }
  540. while (leftover) {
  541. crnt_size = verify_and_add_patch(family, fw, leftover);
  542. if (crnt_size < 0)
  543. return ret;
  544. fw += crnt_size;
  545. leftover -= crnt_size;
  546. }
  547. return UCODE_OK;
  548. }
  549. static enum ucode_state
  550. load_microcode_amd(bool save, u8 family, const u8 *data, size_t size)
  551. {
  552. enum ucode_state ret;
  553. /* free old equiv table */
  554. free_equiv_cpu_table();
  555. ret = __load_microcode_amd(family, data, size);
  556. if (ret != UCODE_OK)
  557. cleanup();
  558. #ifdef CONFIG_X86_32
  559. /* save BSP's matching patch for early load */
  560. if (save) {
  561. struct ucode_patch *p = find_patch(0);
  562. if (p) {
  563. memset(amd_ucode_patch, 0, PATCH_MAX_SIZE);
  564. memcpy(amd_ucode_patch, p->data, min_t(u32, ksize(p->data),
  565. PATCH_MAX_SIZE));
  566. }
  567. }
  568. #endif
  569. return ret;
  570. }
  571. /*
  572. * AMD microcode firmware naming convention, up to family 15h they are in
  573. * the legacy file:
  574. *
  575. * amd-ucode/microcode_amd.bin
  576. *
  577. * This legacy file is always smaller than 2K in size.
  578. *
  579. * Beginning with family 15h, they are in family-specific firmware files:
  580. *
  581. * amd-ucode/microcode_amd_fam15h.bin
  582. * amd-ucode/microcode_amd_fam16h.bin
  583. * ...
  584. *
  585. * These might be larger than 2K.
  586. */
  587. static enum ucode_state request_microcode_amd(int cpu, struct device *device,
  588. bool refresh_fw)
  589. {
  590. char fw_name[36] = "amd-ucode/microcode_amd.bin";
  591. struct cpuinfo_x86 *c = &cpu_data(cpu);
  592. bool bsp = c->cpu_index == boot_cpu_data.cpu_index;
  593. enum ucode_state ret = UCODE_NFOUND;
  594. const struct firmware *fw;
  595. /* reload ucode container only on the boot cpu */
  596. if (!refresh_fw || !bsp)
  597. return UCODE_OK;
  598. if (c->x86 >= 0x15)
  599. snprintf(fw_name, sizeof(fw_name), "amd-ucode/microcode_amd_fam%.2xh.bin", c->x86);
  600. if (request_firmware_direct(&fw, (const char *)fw_name, device)) {
  601. pr_debug("failed to load file %s\n", fw_name);
  602. goto out;
  603. }
  604. ret = UCODE_ERROR;
  605. if (*(u32 *)fw->data != UCODE_MAGIC) {
  606. pr_err("invalid magic value (0x%08x)\n", *(u32 *)fw->data);
  607. goto fw_release;
  608. }
  609. ret = load_microcode_amd(bsp, c->x86, fw->data, fw->size);
  610. fw_release:
  611. release_firmware(fw);
  612. out:
  613. return ret;
  614. }
  615. static enum ucode_state
  616. request_microcode_user(int cpu, const void __user *buf, size_t size)
  617. {
  618. return UCODE_ERROR;
  619. }
  620. static void microcode_fini_cpu_amd(int cpu)
  621. {
  622. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  623. uci->mc = NULL;
  624. }
  625. static struct microcode_ops microcode_amd_ops = {
  626. .request_microcode_user = request_microcode_user,
  627. .request_microcode_fw = request_microcode_amd,
  628. .collect_cpu_info = collect_cpu_info_amd,
  629. .apply_microcode = apply_microcode_amd,
  630. .microcode_fini_cpu = microcode_fini_cpu_amd,
  631. };
  632. struct microcode_ops * __init init_amd_microcode(void)
  633. {
  634. struct cpuinfo_x86 *c = &boot_cpu_data;
  635. if (c->x86_vendor != X86_VENDOR_AMD || c->x86 < 0x10) {
  636. pr_warn("AMD CPU family 0x%x not supported\n", c->x86);
  637. return NULL;
  638. }
  639. if (ucode_new_rev)
  640. pr_info_once("microcode updated early to new patch_level=0x%08x\n",
  641. ucode_new_rev);
  642. return &microcode_amd_ops;
  643. }
  644. void __exit exit_amd_microcode(void)
  645. {
  646. cleanup();
  647. }