amd.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  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. static 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_UPDATED)
  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. kfree(new_patch->data);
  331. kfree(new_patch);
  332. return;
  333. }
  334. list_replace(&p->plist, &new_patch->plist);
  335. kfree(p->data);
  336. kfree(p);
  337. return;
  338. }
  339. }
  340. /* no patch found, add it */
  341. list_add_tail(&new_patch->plist, &microcode_cache);
  342. }
  343. static void free_cache(void)
  344. {
  345. struct ucode_patch *p, *tmp;
  346. list_for_each_entry_safe(p, tmp, &microcode_cache, plist) {
  347. __list_del(p->plist.prev, p->plist.next);
  348. kfree(p->data);
  349. kfree(p);
  350. }
  351. }
  352. static struct ucode_patch *find_patch(unsigned int cpu)
  353. {
  354. u16 equiv_id;
  355. equiv_id = __find_equiv_id(cpu);
  356. if (!equiv_id)
  357. return NULL;
  358. return cache_find_patch(equiv_id);
  359. }
  360. static int collect_cpu_info_amd(int cpu, struct cpu_signature *csig)
  361. {
  362. struct cpuinfo_x86 *c = &cpu_data(cpu);
  363. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  364. struct ucode_patch *p;
  365. csig->sig = cpuid_eax(0x00000001);
  366. csig->rev = c->microcode;
  367. /*
  368. * a patch could have been loaded early, set uci->mc so that
  369. * mc_bp_resume() can call apply_microcode()
  370. */
  371. p = find_patch(cpu);
  372. if (p && (p->patch_id == csig->rev))
  373. uci->mc = p->data;
  374. pr_info("CPU%d: patch_level=0x%08x\n", cpu, csig->rev);
  375. return 0;
  376. }
  377. static unsigned int verify_patch_size(u8 family, u32 patch_size,
  378. unsigned int size)
  379. {
  380. u32 max_size;
  381. #define F1XH_MPB_MAX_SIZE 2048
  382. #define F14H_MPB_MAX_SIZE 1824
  383. #define F15H_MPB_MAX_SIZE 4096
  384. #define F16H_MPB_MAX_SIZE 3458
  385. #define F17H_MPB_MAX_SIZE 3200
  386. switch (family) {
  387. case 0x14:
  388. max_size = F14H_MPB_MAX_SIZE;
  389. break;
  390. case 0x15:
  391. max_size = F15H_MPB_MAX_SIZE;
  392. break;
  393. case 0x16:
  394. max_size = F16H_MPB_MAX_SIZE;
  395. break;
  396. case 0x17:
  397. max_size = F17H_MPB_MAX_SIZE;
  398. break;
  399. default:
  400. max_size = F1XH_MPB_MAX_SIZE;
  401. break;
  402. }
  403. if (patch_size > min_t(u32, size, max_size)) {
  404. pr_err("patch size mismatch\n");
  405. return 0;
  406. }
  407. return patch_size;
  408. }
  409. static enum ucode_state apply_microcode_amd(int cpu)
  410. {
  411. struct cpuinfo_x86 *c = &cpu_data(cpu);
  412. struct microcode_amd *mc_amd;
  413. struct ucode_cpu_info *uci;
  414. struct ucode_patch *p;
  415. u32 rev, dummy;
  416. BUG_ON(raw_smp_processor_id() != cpu);
  417. uci = ucode_cpu_info + cpu;
  418. p = find_patch(cpu);
  419. if (!p)
  420. return UCODE_NFOUND;
  421. mc_amd = p->data;
  422. uci->mc = p->data;
  423. rdmsr(MSR_AMD64_PATCH_LEVEL, rev, dummy);
  424. /* need to apply patch? */
  425. if (rev >= mc_amd->hdr.patch_id) {
  426. c->microcode = rev;
  427. uci->cpu_sig.rev = rev;
  428. return UCODE_OK;
  429. }
  430. if (__apply_microcode_amd(mc_amd)) {
  431. pr_err("CPU%d: update failed for patch_level=0x%08x\n",
  432. cpu, mc_amd->hdr.patch_id);
  433. return UCODE_ERROR;
  434. }
  435. pr_info("CPU%d: new patch_level=0x%08x\n", cpu,
  436. mc_amd->hdr.patch_id);
  437. uci->cpu_sig.rev = mc_amd->hdr.patch_id;
  438. c->microcode = mc_amd->hdr.patch_id;
  439. return UCODE_UPDATED;
  440. }
  441. static int install_equiv_cpu_table(const u8 *buf)
  442. {
  443. unsigned int *ibuf = (unsigned int *)buf;
  444. unsigned int type = ibuf[1];
  445. unsigned int size = ibuf[2];
  446. if (type != UCODE_EQUIV_CPU_TABLE_TYPE || !size) {
  447. pr_err("empty section/"
  448. "invalid type field in container file section header\n");
  449. return -EINVAL;
  450. }
  451. equiv_cpu_table = vmalloc(size);
  452. if (!equiv_cpu_table) {
  453. pr_err("failed to allocate equivalent CPU table\n");
  454. return -ENOMEM;
  455. }
  456. memcpy(equiv_cpu_table, buf + CONTAINER_HDR_SZ, size);
  457. /* add header length */
  458. return size + CONTAINER_HDR_SZ;
  459. }
  460. static void free_equiv_cpu_table(void)
  461. {
  462. vfree(equiv_cpu_table);
  463. equiv_cpu_table = NULL;
  464. }
  465. static void cleanup(void)
  466. {
  467. free_equiv_cpu_table();
  468. free_cache();
  469. }
  470. /*
  471. * We return the current size even if some of the checks failed so that
  472. * we can skip over the next patch. If we return a negative value, we
  473. * signal a grave error like a memory allocation has failed and the
  474. * driver cannot continue functioning normally. In such cases, we tear
  475. * down everything we've used up so far and exit.
  476. */
  477. static int verify_and_add_patch(u8 family, u8 *fw, unsigned int leftover)
  478. {
  479. struct microcode_header_amd *mc_hdr;
  480. struct ucode_patch *patch;
  481. unsigned int patch_size, crnt_size, ret;
  482. u32 proc_fam;
  483. u16 proc_id;
  484. patch_size = *(u32 *)(fw + 4);
  485. crnt_size = patch_size + SECTION_HDR_SIZE;
  486. mc_hdr = (struct microcode_header_amd *)(fw + SECTION_HDR_SIZE);
  487. proc_id = mc_hdr->processor_rev_id;
  488. proc_fam = find_cpu_family_by_equiv_cpu(proc_id);
  489. if (!proc_fam) {
  490. pr_err("No patch family for equiv ID: 0x%04x\n", proc_id);
  491. return crnt_size;
  492. }
  493. /* check if patch is for the current family */
  494. proc_fam = ((proc_fam >> 8) & 0xf) + ((proc_fam >> 20) & 0xff);
  495. if (proc_fam != family)
  496. return crnt_size;
  497. if (mc_hdr->nb_dev_id || mc_hdr->sb_dev_id) {
  498. pr_err("Patch-ID 0x%08x: chipset-specific code unsupported.\n",
  499. mc_hdr->patch_id);
  500. return crnt_size;
  501. }
  502. ret = verify_patch_size(family, patch_size, leftover);
  503. if (!ret) {
  504. pr_err("Patch-ID 0x%08x: size mismatch.\n", mc_hdr->patch_id);
  505. return crnt_size;
  506. }
  507. patch = kzalloc(sizeof(*patch), GFP_KERNEL);
  508. if (!patch) {
  509. pr_err("Patch allocation failure.\n");
  510. return -EINVAL;
  511. }
  512. patch->data = kmemdup(fw + SECTION_HDR_SIZE, patch_size, GFP_KERNEL);
  513. if (!patch->data) {
  514. pr_err("Patch data allocation failure.\n");
  515. kfree(patch);
  516. return -EINVAL;
  517. }
  518. INIT_LIST_HEAD(&patch->plist);
  519. patch->patch_id = mc_hdr->patch_id;
  520. patch->equiv_cpu = proc_id;
  521. pr_debug("%s: Added patch_id: 0x%08x, proc_id: 0x%04x\n",
  522. __func__, patch->patch_id, proc_id);
  523. /* ... and add to cache. */
  524. update_cache(patch);
  525. return crnt_size;
  526. }
  527. static enum ucode_state __load_microcode_amd(u8 family, const u8 *data,
  528. size_t size)
  529. {
  530. enum ucode_state ret = UCODE_ERROR;
  531. unsigned int leftover;
  532. u8 *fw = (u8 *)data;
  533. int crnt_size = 0;
  534. int offset;
  535. offset = install_equiv_cpu_table(data);
  536. if (offset < 0) {
  537. pr_err("failed to create equivalent cpu table\n");
  538. return ret;
  539. }
  540. fw += offset;
  541. leftover = size - offset;
  542. if (*(u32 *)fw != UCODE_UCODE_TYPE) {
  543. pr_err("invalid type field in container file section header\n");
  544. free_equiv_cpu_table();
  545. return ret;
  546. }
  547. while (leftover) {
  548. crnt_size = verify_and_add_patch(family, fw, leftover);
  549. if (crnt_size < 0)
  550. return ret;
  551. fw += crnt_size;
  552. leftover -= crnt_size;
  553. }
  554. return UCODE_OK;
  555. }
  556. static enum ucode_state
  557. load_microcode_amd(bool save, u8 family, const u8 *data, size_t size)
  558. {
  559. struct ucode_patch *p;
  560. enum ucode_state ret;
  561. /* free old equiv table */
  562. free_equiv_cpu_table();
  563. ret = __load_microcode_amd(family, data, size);
  564. if (ret != UCODE_OK) {
  565. cleanup();
  566. return ret;
  567. }
  568. p = find_patch(0);
  569. if (!p) {
  570. return ret;
  571. } else {
  572. if (boot_cpu_data.microcode == p->patch_id)
  573. return ret;
  574. ret = UCODE_NEW;
  575. }
  576. /* save BSP's matching patch for early load */
  577. if (!save)
  578. return ret;
  579. memset(amd_ucode_patch, 0, PATCH_MAX_SIZE);
  580. memcpy(amd_ucode_patch, p->data, min_t(u32, ksize(p->data), PATCH_MAX_SIZE));
  581. return ret;
  582. }
  583. /*
  584. * AMD microcode firmware naming convention, up to family 15h they are in
  585. * the legacy file:
  586. *
  587. * amd-ucode/microcode_amd.bin
  588. *
  589. * This legacy file is always smaller than 2K in size.
  590. *
  591. * Beginning with family 15h, they are in family-specific firmware files:
  592. *
  593. * amd-ucode/microcode_amd_fam15h.bin
  594. * amd-ucode/microcode_amd_fam16h.bin
  595. * ...
  596. *
  597. * These might be larger than 2K.
  598. */
  599. static enum ucode_state request_microcode_amd(int cpu, struct device *device,
  600. bool refresh_fw)
  601. {
  602. char fw_name[36] = "amd-ucode/microcode_amd.bin";
  603. struct cpuinfo_x86 *c = &cpu_data(cpu);
  604. bool bsp = c->cpu_index == boot_cpu_data.cpu_index;
  605. enum ucode_state ret = UCODE_NFOUND;
  606. const struct firmware *fw;
  607. /* reload ucode container only on the boot cpu */
  608. if (!refresh_fw || !bsp)
  609. return UCODE_OK;
  610. if (c->x86 >= 0x15)
  611. snprintf(fw_name, sizeof(fw_name), "amd-ucode/microcode_amd_fam%.2xh.bin", c->x86);
  612. if (request_firmware_direct(&fw, (const char *)fw_name, device)) {
  613. pr_debug("failed to load file %s\n", fw_name);
  614. goto out;
  615. }
  616. ret = UCODE_ERROR;
  617. if (*(u32 *)fw->data != UCODE_MAGIC) {
  618. pr_err("invalid magic value (0x%08x)\n", *(u32 *)fw->data);
  619. goto fw_release;
  620. }
  621. ret = load_microcode_amd(bsp, c->x86, fw->data, fw->size);
  622. fw_release:
  623. release_firmware(fw);
  624. out:
  625. return ret;
  626. }
  627. static enum ucode_state
  628. request_microcode_user(int cpu, const void __user *buf, size_t size)
  629. {
  630. return UCODE_ERROR;
  631. }
  632. static void microcode_fini_cpu_amd(int cpu)
  633. {
  634. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  635. uci->mc = NULL;
  636. }
  637. static struct microcode_ops microcode_amd_ops = {
  638. .request_microcode_user = request_microcode_user,
  639. .request_microcode_fw = request_microcode_amd,
  640. .collect_cpu_info = collect_cpu_info_amd,
  641. .apply_microcode = apply_microcode_amd,
  642. .microcode_fini_cpu = microcode_fini_cpu_amd,
  643. };
  644. struct microcode_ops * __init init_amd_microcode(void)
  645. {
  646. struct cpuinfo_x86 *c = &boot_cpu_data;
  647. if (c->x86_vendor != X86_VENDOR_AMD || c->x86 < 0x10) {
  648. pr_warn("AMD CPU family 0x%x not supported\n", c->x86);
  649. return NULL;
  650. }
  651. if (ucode_new_rev)
  652. pr_info_once("microcode updated early to new patch_level=0x%08x\n",
  653. ucode_new_rev);
  654. return &microcode_amd_ops;
  655. }
  656. void __exit exit_amd_microcode(void)
  657. {
  658. cleanup();
  659. }