amd.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975
  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. *
  9. * Author: Peter Oruba <peter.oruba@amd.com>
  10. *
  11. * Based on work by:
  12. * Tigran Aivazian <tigran@aivazian.fsnet.co.uk>
  13. *
  14. * early loader:
  15. * Copyright (C) 2013 Advanced Micro Devices, Inc.
  16. *
  17. * Author: Jacob Shin <jacob.shin@amd.com>
  18. * Fixes: Borislav Petkov <bp@suse.de>
  19. *
  20. * Licensed under the terms of the GNU General Public
  21. * License version 2. See file COPYING for details.
  22. */
  23. #define pr_fmt(fmt) "microcode: " fmt
  24. #include <linux/earlycpio.h>
  25. #include <linux/firmware.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 <asm/microcode_amd.h>
  32. #include <asm/microcode.h>
  33. #include <asm/processor.h>
  34. #include <asm/setup.h>
  35. #include <asm/cpu.h>
  36. #include <asm/msr.h>
  37. static struct equiv_cpu_entry *equiv_cpu_table;
  38. struct ucode_patch {
  39. struct list_head plist;
  40. void *data;
  41. u32 patch_id;
  42. u16 equiv_cpu;
  43. };
  44. static LIST_HEAD(pcache);
  45. /*
  46. * This points to the current valid container of microcode patches which we will
  47. * save from the initrd before jettisoning its contents.
  48. */
  49. static u8 *container;
  50. static size_t container_size;
  51. static bool ucode_builtin;
  52. static u32 ucode_new_rev;
  53. static u8 amd_ucode_patch[PATCH_MAX_SIZE];
  54. static u16 this_equiv_id;
  55. static struct cpio_data ucode_cpio;
  56. static struct cpio_data __init find_ucode_in_initrd(void)
  57. {
  58. #ifdef CONFIG_BLK_DEV_INITRD
  59. char *path;
  60. void *start;
  61. size_t size;
  62. /*
  63. * Microcode patch container file is prepended to the initrd in cpio
  64. * format. See Documentation/x86/early-microcode.txt
  65. */
  66. static __initdata char ucode_path[] = "kernel/x86/microcode/AuthenticAMD.bin";
  67. #ifdef CONFIG_X86_32
  68. struct boot_params *p;
  69. /*
  70. * On 32-bit, early load occurs before paging is turned on so we need
  71. * to use physical addresses.
  72. */
  73. p = (struct boot_params *)__pa_nodebug(&boot_params);
  74. path = (char *)__pa_nodebug(ucode_path);
  75. start = (void *)p->hdr.ramdisk_image;
  76. size = p->hdr.ramdisk_size;
  77. #else
  78. path = ucode_path;
  79. start = (void *)(boot_params.hdr.ramdisk_image + PAGE_OFFSET);
  80. size = boot_params.hdr.ramdisk_size;
  81. #endif /* !CONFIG_X86_32 */
  82. return find_cpio_data(path, start, size, NULL);
  83. #else
  84. return (struct cpio_data){ NULL, 0, "" };
  85. #endif
  86. }
  87. static size_t compute_container_size(u8 *data, u32 total_size)
  88. {
  89. size_t size = 0;
  90. u32 *header = (u32 *)data;
  91. if (header[0] != UCODE_MAGIC ||
  92. header[1] != UCODE_EQUIV_CPU_TABLE_TYPE || /* type */
  93. header[2] == 0) /* size */
  94. return size;
  95. size = header[2] + CONTAINER_HDR_SZ;
  96. total_size -= size;
  97. data += size;
  98. while (total_size) {
  99. u16 patch_size;
  100. header = (u32 *)data;
  101. if (header[0] != UCODE_UCODE_TYPE)
  102. break;
  103. /*
  104. * Sanity-check patch size.
  105. */
  106. patch_size = header[1];
  107. if (patch_size > PATCH_MAX_SIZE)
  108. break;
  109. size += patch_size + SECTION_HDR_SIZE;
  110. data += patch_size + SECTION_HDR_SIZE;
  111. total_size -= patch_size + SECTION_HDR_SIZE;
  112. }
  113. return size;
  114. }
  115. /*
  116. * Early load occurs before we can vmalloc(). So we look for the microcode
  117. * patch container file in initrd, traverse equivalent cpu table, look for a
  118. * matching microcode patch, and update, all in initrd memory in place.
  119. * When vmalloc() is available for use later -- on 64-bit during first AP load,
  120. * and on 32-bit during save_microcode_in_initrd_amd() -- we can call
  121. * load_microcode_amd() to save equivalent cpu table and microcode patches in
  122. * kernel heap memory.
  123. */
  124. static void apply_ucode_in_initrd(void *ucode, size_t size, bool save_patch)
  125. {
  126. struct equiv_cpu_entry *eq;
  127. size_t *cont_sz;
  128. u32 *header;
  129. u8 *data, **cont;
  130. u8 (*patch)[PATCH_MAX_SIZE];
  131. u16 eq_id = 0;
  132. int offset, left;
  133. u32 rev, eax, ebx, ecx, edx;
  134. u32 *new_rev;
  135. #ifdef CONFIG_X86_32
  136. new_rev = (u32 *)__pa_nodebug(&ucode_new_rev);
  137. cont_sz = (size_t *)__pa_nodebug(&container_size);
  138. cont = (u8 **)__pa_nodebug(&container);
  139. patch = (u8 (*)[PATCH_MAX_SIZE])__pa_nodebug(&amd_ucode_patch);
  140. #else
  141. new_rev = &ucode_new_rev;
  142. cont_sz = &container_size;
  143. cont = &container;
  144. patch = &amd_ucode_patch;
  145. #endif
  146. data = ucode;
  147. left = size;
  148. header = (u32 *)data;
  149. /* find equiv cpu table */
  150. if (header[0] != UCODE_MAGIC ||
  151. header[1] != UCODE_EQUIV_CPU_TABLE_TYPE || /* type */
  152. header[2] == 0) /* size */
  153. return;
  154. eax = 0x00000001;
  155. ecx = 0;
  156. native_cpuid(&eax, &ebx, &ecx, &edx);
  157. while (left > 0) {
  158. eq = (struct equiv_cpu_entry *)(data + CONTAINER_HDR_SZ);
  159. *cont = data;
  160. /* Advance past the container header */
  161. offset = header[2] + CONTAINER_HDR_SZ;
  162. data += offset;
  163. left -= offset;
  164. eq_id = find_equiv_id(eq, eax);
  165. if (eq_id) {
  166. this_equiv_id = eq_id;
  167. *cont_sz = compute_container_size(*cont, left + offset);
  168. /*
  169. * truncate how much we need to iterate over in the
  170. * ucode update loop below
  171. */
  172. left = *cont_sz - offset;
  173. break;
  174. }
  175. /*
  176. * support multiple container files appended together. if this
  177. * one does not have a matching equivalent cpu entry, we fast
  178. * forward to the next container file.
  179. */
  180. while (left > 0) {
  181. header = (u32 *)data;
  182. if (header[0] == UCODE_MAGIC &&
  183. header[1] == UCODE_EQUIV_CPU_TABLE_TYPE)
  184. break;
  185. offset = header[1] + SECTION_HDR_SIZE;
  186. data += offset;
  187. left -= offset;
  188. }
  189. /* mark where the next microcode container file starts */
  190. offset = data - (u8 *)ucode;
  191. ucode = data;
  192. }
  193. if (!eq_id) {
  194. *cont = NULL;
  195. *cont_sz = 0;
  196. return;
  197. }
  198. if (check_current_patch_level(&rev, true))
  199. return;
  200. while (left > 0) {
  201. struct microcode_amd *mc;
  202. header = (u32 *)data;
  203. if (header[0] != UCODE_UCODE_TYPE || /* type */
  204. header[1] == 0) /* size */
  205. break;
  206. mc = (struct microcode_amd *)(data + SECTION_HDR_SIZE);
  207. if (eq_id == mc->hdr.processor_rev_id && rev < mc->hdr.patch_id) {
  208. if (!__apply_microcode_amd(mc)) {
  209. rev = mc->hdr.patch_id;
  210. *new_rev = rev;
  211. if (save_patch)
  212. memcpy(patch, mc,
  213. min_t(u32, header[1], PATCH_MAX_SIZE));
  214. }
  215. }
  216. offset = header[1] + SECTION_HDR_SIZE;
  217. data += offset;
  218. left -= offset;
  219. }
  220. }
  221. static bool __init load_builtin_amd_microcode(struct cpio_data *cp,
  222. unsigned int family)
  223. {
  224. #ifdef CONFIG_X86_64
  225. char fw_name[36] = "amd-ucode/microcode_amd.bin";
  226. if (family >= 0x15)
  227. snprintf(fw_name, sizeof(fw_name),
  228. "amd-ucode/microcode_amd_fam%.2xh.bin", family);
  229. return get_builtin_firmware(cp, fw_name);
  230. #else
  231. return false;
  232. #endif
  233. }
  234. void __init load_ucode_amd_bsp(unsigned int family)
  235. {
  236. struct cpio_data cp;
  237. bool *builtin;
  238. void **data;
  239. size_t *size;
  240. #ifdef CONFIG_X86_32
  241. data = (void **)__pa_nodebug(&ucode_cpio.data);
  242. size = (size_t *)__pa_nodebug(&ucode_cpio.size);
  243. builtin = (bool *)__pa_nodebug(&ucode_builtin);
  244. #else
  245. data = &ucode_cpio.data;
  246. size = &ucode_cpio.size;
  247. builtin = &ucode_builtin;
  248. #endif
  249. *builtin = load_builtin_amd_microcode(&cp, family);
  250. if (!*builtin)
  251. cp = find_ucode_in_initrd();
  252. if (!(cp.data && cp.size))
  253. return;
  254. *data = cp.data;
  255. *size = cp.size;
  256. apply_ucode_in_initrd(cp.data, cp.size, true);
  257. }
  258. #ifdef CONFIG_X86_32
  259. /*
  260. * On 32-bit, since AP's early load occurs before paging is turned on, we
  261. * cannot traverse cpu_equiv_table and pcache in kernel heap memory. So during
  262. * cold boot, AP will apply_ucode_in_initrd() just like the BSP. During
  263. * save_microcode_in_initrd_amd() BSP's patch is copied to amd_ucode_patch,
  264. * which is used upon resume from suspend.
  265. */
  266. void load_ucode_amd_ap(void)
  267. {
  268. struct microcode_amd *mc;
  269. size_t *usize;
  270. void **ucode;
  271. mc = (struct microcode_amd *)__pa_nodebug(amd_ucode_patch);
  272. if (mc->hdr.patch_id && mc->hdr.processor_rev_id) {
  273. __apply_microcode_amd(mc);
  274. return;
  275. }
  276. ucode = (void *)__pa_nodebug(&container);
  277. usize = (size_t *)__pa_nodebug(&container_size);
  278. if (!*ucode || !*usize)
  279. return;
  280. apply_ucode_in_initrd(*ucode, *usize, false);
  281. }
  282. static void __init collect_cpu_sig_on_bsp(void *arg)
  283. {
  284. unsigned int cpu = smp_processor_id();
  285. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  286. uci->cpu_sig.sig = cpuid_eax(0x00000001);
  287. }
  288. static void __init get_bsp_sig(void)
  289. {
  290. unsigned int bsp = boot_cpu_data.cpu_index;
  291. struct ucode_cpu_info *uci = ucode_cpu_info + bsp;
  292. if (!uci->cpu_sig.sig)
  293. smp_call_function_single(bsp, collect_cpu_sig_on_bsp, NULL, 1);
  294. }
  295. #else
  296. void load_ucode_amd_ap(void)
  297. {
  298. struct equiv_cpu_entry *eq;
  299. struct microcode_amd *mc;
  300. u8 *cont = container;
  301. u32 rev, eax;
  302. u16 eq_id;
  303. if (!container)
  304. return;
  305. /*
  306. * 64-bit runs with paging enabled, thus early==false.
  307. */
  308. if (check_current_patch_level(&rev, false))
  309. return;
  310. /* Add CONFIG_RANDOMIZE_MEMORY offset. */
  311. if (!ucode_builtin)
  312. cont += PAGE_OFFSET - __PAGE_OFFSET_BASE;
  313. eax = cpuid_eax(0x00000001);
  314. eq = (struct equiv_cpu_entry *)(cont + CONTAINER_HDR_SZ);
  315. eq_id = find_equiv_id(eq, eax);
  316. if (!eq_id)
  317. return;
  318. if (eq_id == this_equiv_id) {
  319. mc = (struct microcode_amd *)amd_ucode_patch;
  320. if (mc && rev < mc->hdr.patch_id) {
  321. if (!__apply_microcode_amd(mc))
  322. ucode_new_rev = mc->hdr.patch_id;
  323. }
  324. } else {
  325. if (!ucode_cpio.data)
  326. return;
  327. /*
  328. * AP has a different equivalence ID than BSP, looks like
  329. * mixed-steppings silicon so go through the ucode blob anew.
  330. */
  331. apply_ucode_in_initrd(ucode_cpio.data, ucode_cpio.size, false);
  332. }
  333. }
  334. #endif
  335. int __init save_microcode_in_initrd_amd(void)
  336. {
  337. unsigned long cont;
  338. int retval = 0;
  339. enum ucode_state ret;
  340. u8 *cont_va;
  341. u32 eax;
  342. if (!container)
  343. return -EINVAL;
  344. #ifdef CONFIG_X86_32
  345. get_bsp_sig();
  346. cont = (unsigned long)container;
  347. cont_va = __va(container);
  348. #else
  349. /*
  350. * We need the physical address of the container for both bitness since
  351. * boot_params.hdr.ramdisk_image is a physical address.
  352. */
  353. cont = __pa(container);
  354. cont_va = container;
  355. #endif
  356. /*
  357. * Take into account the fact that the ramdisk might get relocated and
  358. * therefore we need to recompute the container's position in virtual
  359. * memory space.
  360. */
  361. if (relocated_ramdisk)
  362. container = (u8 *)(__va(relocated_ramdisk) +
  363. (cont - boot_params.hdr.ramdisk_image));
  364. else
  365. container = cont_va;
  366. /* Add CONFIG_RANDOMIZE_MEMORY offset. */
  367. if (!ucode_builtin)
  368. container += PAGE_OFFSET - __PAGE_OFFSET_BASE;
  369. eax = cpuid_eax(0x00000001);
  370. eax = ((eax >> 8) & 0xf) + ((eax >> 20) & 0xff);
  371. ret = load_microcode_amd(smp_processor_id(), eax, container, container_size);
  372. if (ret != UCODE_OK)
  373. retval = -EINVAL;
  374. /*
  375. * This will be freed any msec now, stash patches for the current
  376. * family and switch to patch cache for cpu hotplug, etc later.
  377. */
  378. container = NULL;
  379. container_size = 0;
  380. return retval;
  381. }
  382. void reload_ucode_amd(void)
  383. {
  384. struct microcode_amd *mc;
  385. u32 rev;
  386. /*
  387. * early==false because this is a syscore ->resume path and by
  388. * that time paging is long enabled.
  389. */
  390. if (check_current_patch_level(&rev, false))
  391. return;
  392. mc = (struct microcode_amd *)amd_ucode_patch;
  393. if (mc && rev < mc->hdr.patch_id) {
  394. if (!__apply_microcode_amd(mc)) {
  395. ucode_new_rev = mc->hdr.patch_id;
  396. pr_info("reload patch_level=0x%08x\n", ucode_new_rev);
  397. }
  398. }
  399. }
  400. static u16 __find_equiv_id(unsigned int cpu)
  401. {
  402. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  403. return find_equiv_id(equiv_cpu_table, uci->cpu_sig.sig);
  404. }
  405. static u32 find_cpu_family_by_equiv_cpu(u16 equiv_cpu)
  406. {
  407. int i = 0;
  408. BUG_ON(!equiv_cpu_table);
  409. while (equiv_cpu_table[i].equiv_cpu != 0) {
  410. if (equiv_cpu == equiv_cpu_table[i].equiv_cpu)
  411. return equiv_cpu_table[i].installed_cpu;
  412. i++;
  413. }
  414. return 0;
  415. }
  416. /*
  417. * a small, trivial cache of per-family ucode patches
  418. */
  419. static struct ucode_patch *cache_find_patch(u16 equiv_cpu)
  420. {
  421. struct ucode_patch *p;
  422. list_for_each_entry(p, &pcache, plist)
  423. if (p->equiv_cpu == equiv_cpu)
  424. return p;
  425. return NULL;
  426. }
  427. static void update_cache(struct ucode_patch *new_patch)
  428. {
  429. struct ucode_patch *p;
  430. list_for_each_entry(p, &pcache, plist) {
  431. if (p->equiv_cpu == new_patch->equiv_cpu) {
  432. if (p->patch_id >= new_patch->patch_id)
  433. /* we already have the latest patch */
  434. return;
  435. list_replace(&p->plist, &new_patch->plist);
  436. kfree(p->data);
  437. kfree(p);
  438. return;
  439. }
  440. }
  441. /* no patch found, add it */
  442. list_add_tail(&new_patch->plist, &pcache);
  443. }
  444. static void free_cache(void)
  445. {
  446. struct ucode_patch *p, *tmp;
  447. list_for_each_entry_safe(p, tmp, &pcache, plist) {
  448. __list_del(p->plist.prev, p->plist.next);
  449. kfree(p->data);
  450. kfree(p);
  451. }
  452. }
  453. static struct ucode_patch *find_patch(unsigned int cpu)
  454. {
  455. u16 equiv_id;
  456. equiv_id = __find_equiv_id(cpu);
  457. if (!equiv_id)
  458. return NULL;
  459. return cache_find_patch(equiv_id);
  460. }
  461. static int collect_cpu_info_amd(int cpu, struct cpu_signature *csig)
  462. {
  463. struct cpuinfo_x86 *c = &cpu_data(cpu);
  464. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  465. struct ucode_patch *p;
  466. csig->sig = cpuid_eax(0x00000001);
  467. csig->rev = c->microcode;
  468. /*
  469. * a patch could have been loaded early, set uci->mc so that
  470. * mc_bp_resume() can call apply_microcode()
  471. */
  472. p = find_patch(cpu);
  473. if (p && (p->patch_id == csig->rev))
  474. uci->mc = p->data;
  475. pr_info("CPU%d: patch_level=0x%08x\n", cpu, csig->rev);
  476. return 0;
  477. }
  478. static unsigned int verify_patch_size(u8 family, u32 patch_size,
  479. unsigned int size)
  480. {
  481. u32 max_size;
  482. #define F1XH_MPB_MAX_SIZE 2048
  483. #define F14H_MPB_MAX_SIZE 1824
  484. #define F15H_MPB_MAX_SIZE 4096
  485. #define F16H_MPB_MAX_SIZE 3458
  486. switch (family) {
  487. case 0x14:
  488. max_size = F14H_MPB_MAX_SIZE;
  489. break;
  490. case 0x15:
  491. max_size = F15H_MPB_MAX_SIZE;
  492. break;
  493. case 0x16:
  494. max_size = F16H_MPB_MAX_SIZE;
  495. break;
  496. default:
  497. max_size = F1XH_MPB_MAX_SIZE;
  498. break;
  499. }
  500. if (patch_size > min_t(u32, size, max_size)) {
  501. pr_err("patch size mismatch\n");
  502. return 0;
  503. }
  504. return patch_size;
  505. }
  506. /*
  507. * Those patch levels cannot be updated to newer ones and thus should be final.
  508. */
  509. static u32 final_levels[] = {
  510. 0x01000098,
  511. 0x0100009f,
  512. 0x010000af,
  513. 0, /* T-101 terminator */
  514. };
  515. /*
  516. * Check the current patch level on this CPU.
  517. *
  518. * @rev: Use it to return the patch level. It is set to 0 in the case of
  519. * error.
  520. *
  521. * Returns:
  522. * - true: if update should stop
  523. * - false: otherwise
  524. */
  525. bool check_current_patch_level(u32 *rev, bool early)
  526. {
  527. u32 lvl, dummy, i;
  528. bool ret = false;
  529. u32 *levels;
  530. native_rdmsr(MSR_AMD64_PATCH_LEVEL, lvl, dummy);
  531. if (IS_ENABLED(CONFIG_X86_32) && early)
  532. levels = (u32 *)__pa_nodebug(&final_levels);
  533. else
  534. levels = final_levels;
  535. for (i = 0; levels[i]; i++) {
  536. if (lvl == levels[i]) {
  537. lvl = 0;
  538. ret = true;
  539. break;
  540. }
  541. }
  542. if (rev)
  543. *rev = lvl;
  544. return ret;
  545. }
  546. int __apply_microcode_amd(struct microcode_amd *mc_amd)
  547. {
  548. u32 rev, dummy;
  549. native_wrmsrl(MSR_AMD64_PATCH_LOADER, (u64)(long)&mc_amd->hdr.data_code);
  550. /* verify patch application was successful */
  551. native_rdmsr(MSR_AMD64_PATCH_LEVEL, rev, dummy);
  552. if (rev != mc_amd->hdr.patch_id)
  553. return -1;
  554. return 0;
  555. }
  556. int apply_microcode_amd(int cpu)
  557. {
  558. struct cpuinfo_x86 *c = &cpu_data(cpu);
  559. struct microcode_amd *mc_amd;
  560. struct ucode_cpu_info *uci;
  561. struct ucode_patch *p;
  562. u32 rev;
  563. BUG_ON(raw_smp_processor_id() != cpu);
  564. uci = ucode_cpu_info + cpu;
  565. p = find_patch(cpu);
  566. if (!p)
  567. return 0;
  568. mc_amd = p->data;
  569. uci->mc = p->data;
  570. if (check_current_patch_level(&rev, false))
  571. return -1;
  572. /* need to apply patch? */
  573. if (rev >= mc_amd->hdr.patch_id) {
  574. c->microcode = rev;
  575. uci->cpu_sig.rev = rev;
  576. return 0;
  577. }
  578. if (__apply_microcode_amd(mc_amd)) {
  579. pr_err("CPU%d: update failed for patch_level=0x%08x\n",
  580. cpu, mc_amd->hdr.patch_id);
  581. return -1;
  582. }
  583. pr_info("CPU%d: new patch_level=0x%08x\n", cpu,
  584. mc_amd->hdr.patch_id);
  585. uci->cpu_sig.rev = mc_amd->hdr.patch_id;
  586. c->microcode = mc_amd->hdr.patch_id;
  587. return 0;
  588. }
  589. static int install_equiv_cpu_table(const u8 *buf)
  590. {
  591. unsigned int *ibuf = (unsigned int *)buf;
  592. unsigned int type = ibuf[1];
  593. unsigned int size = ibuf[2];
  594. if (type != UCODE_EQUIV_CPU_TABLE_TYPE || !size) {
  595. pr_err("empty section/"
  596. "invalid type field in container file section header\n");
  597. return -EINVAL;
  598. }
  599. equiv_cpu_table = vmalloc(size);
  600. if (!equiv_cpu_table) {
  601. pr_err("failed to allocate equivalent CPU table\n");
  602. return -ENOMEM;
  603. }
  604. memcpy(equiv_cpu_table, buf + CONTAINER_HDR_SZ, size);
  605. /* add header length */
  606. return size + CONTAINER_HDR_SZ;
  607. }
  608. static void free_equiv_cpu_table(void)
  609. {
  610. vfree(equiv_cpu_table);
  611. equiv_cpu_table = NULL;
  612. }
  613. static void cleanup(void)
  614. {
  615. free_equiv_cpu_table();
  616. free_cache();
  617. }
  618. /*
  619. * We return the current size even if some of the checks failed so that
  620. * we can skip over the next patch. If we return a negative value, we
  621. * signal a grave error like a memory allocation has failed and the
  622. * driver cannot continue functioning normally. In such cases, we tear
  623. * down everything we've used up so far and exit.
  624. */
  625. static int verify_and_add_patch(u8 family, u8 *fw, unsigned int leftover)
  626. {
  627. struct microcode_header_amd *mc_hdr;
  628. struct ucode_patch *patch;
  629. unsigned int patch_size, crnt_size, ret;
  630. u32 proc_fam;
  631. u16 proc_id;
  632. patch_size = *(u32 *)(fw + 4);
  633. crnt_size = patch_size + SECTION_HDR_SIZE;
  634. mc_hdr = (struct microcode_header_amd *)(fw + SECTION_HDR_SIZE);
  635. proc_id = mc_hdr->processor_rev_id;
  636. proc_fam = find_cpu_family_by_equiv_cpu(proc_id);
  637. if (!proc_fam) {
  638. pr_err("No patch family for equiv ID: 0x%04x\n", proc_id);
  639. return crnt_size;
  640. }
  641. /* check if patch is for the current family */
  642. proc_fam = ((proc_fam >> 8) & 0xf) + ((proc_fam >> 20) & 0xff);
  643. if (proc_fam != family)
  644. return crnt_size;
  645. if (mc_hdr->nb_dev_id || mc_hdr->sb_dev_id) {
  646. pr_err("Patch-ID 0x%08x: chipset-specific code unsupported.\n",
  647. mc_hdr->patch_id);
  648. return crnt_size;
  649. }
  650. ret = verify_patch_size(family, patch_size, leftover);
  651. if (!ret) {
  652. pr_err("Patch-ID 0x%08x: size mismatch.\n", mc_hdr->patch_id);
  653. return crnt_size;
  654. }
  655. patch = kzalloc(sizeof(*patch), GFP_KERNEL);
  656. if (!patch) {
  657. pr_err("Patch allocation failure.\n");
  658. return -EINVAL;
  659. }
  660. patch->data = kmemdup(fw + SECTION_HDR_SIZE, patch_size, GFP_KERNEL);
  661. if (!patch->data) {
  662. pr_err("Patch data allocation failure.\n");
  663. kfree(patch);
  664. return -EINVAL;
  665. }
  666. INIT_LIST_HEAD(&patch->plist);
  667. patch->patch_id = mc_hdr->patch_id;
  668. patch->equiv_cpu = proc_id;
  669. pr_debug("%s: Added patch_id: 0x%08x, proc_id: 0x%04x\n",
  670. __func__, patch->patch_id, proc_id);
  671. /* ... and add to cache. */
  672. update_cache(patch);
  673. return crnt_size;
  674. }
  675. static enum ucode_state __load_microcode_amd(u8 family, const u8 *data,
  676. size_t size)
  677. {
  678. enum ucode_state ret = UCODE_ERROR;
  679. unsigned int leftover;
  680. u8 *fw = (u8 *)data;
  681. int crnt_size = 0;
  682. int offset;
  683. offset = install_equiv_cpu_table(data);
  684. if (offset < 0) {
  685. pr_err("failed to create equivalent cpu table\n");
  686. return ret;
  687. }
  688. fw += offset;
  689. leftover = size - offset;
  690. if (*(u32 *)fw != UCODE_UCODE_TYPE) {
  691. pr_err("invalid type field in container file section header\n");
  692. free_equiv_cpu_table();
  693. return ret;
  694. }
  695. while (leftover) {
  696. crnt_size = verify_and_add_patch(family, fw, leftover);
  697. if (crnt_size < 0)
  698. return ret;
  699. fw += crnt_size;
  700. leftover -= crnt_size;
  701. }
  702. return UCODE_OK;
  703. }
  704. enum ucode_state load_microcode_amd(int cpu, u8 family, const u8 *data, size_t size)
  705. {
  706. enum ucode_state ret;
  707. /* free old equiv table */
  708. free_equiv_cpu_table();
  709. ret = __load_microcode_amd(family, data, size);
  710. if (ret != UCODE_OK)
  711. cleanup();
  712. #ifdef CONFIG_X86_32
  713. /* save BSP's matching patch for early load */
  714. if (cpu_data(cpu).cpu_index == boot_cpu_data.cpu_index) {
  715. struct ucode_patch *p = find_patch(cpu);
  716. if (p) {
  717. memset(amd_ucode_patch, 0, PATCH_MAX_SIZE);
  718. memcpy(amd_ucode_patch, p->data, min_t(u32, ksize(p->data),
  719. PATCH_MAX_SIZE));
  720. }
  721. }
  722. #endif
  723. return ret;
  724. }
  725. /*
  726. * AMD microcode firmware naming convention, up to family 15h they are in
  727. * the legacy file:
  728. *
  729. * amd-ucode/microcode_amd.bin
  730. *
  731. * This legacy file is always smaller than 2K in size.
  732. *
  733. * Beginning with family 15h, they are in family-specific firmware files:
  734. *
  735. * amd-ucode/microcode_amd_fam15h.bin
  736. * amd-ucode/microcode_amd_fam16h.bin
  737. * ...
  738. *
  739. * These might be larger than 2K.
  740. */
  741. static enum ucode_state request_microcode_amd(int cpu, struct device *device,
  742. bool refresh_fw)
  743. {
  744. char fw_name[36] = "amd-ucode/microcode_amd.bin";
  745. struct cpuinfo_x86 *c = &cpu_data(cpu);
  746. enum ucode_state ret = UCODE_NFOUND;
  747. const struct firmware *fw;
  748. /* reload ucode container only on the boot cpu */
  749. if (!refresh_fw || c->cpu_index != boot_cpu_data.cpu_index)
  750. return UCODE_OK;
  751. if (c->x86 >= 0x15)
  752. snprintf(fw_name, sizeof(fw_name), "amd-ucode/microcode_amd_fam%.2xh.bin", c->x86);
  753. if (request_firmware_direct(&fw, (const char *)fw_name, device)) {
  754. pr_debug("failed to load file %s\n", fw_name);
  755. goto out;
  756. }
  757. ret = UCODE_ERROR;
  758. if (*(u32 *)fw->data != UCODE_MAGIC) {
  759. pr_err("invalid magic value (0x%08x)\n", *(u32 *)fw->data);
  760. goto fw_release;
  761. }
  762. ret = load_microcode_amd(cpu, c->x86, fw->data, fw->size);
  763. fw_release:
  764. release_firmware(fw);
  765. out:
  766. return ret;
  767. }
  768. static enum ucode_state
  769. request_microcode_user(int cpu, const void __user *buf, size_t size)
  770. {
  771. return UCODE_ERROR;
  772. }
  773. static void microcode_fini_cpu_amd(int cpu)
  774. {
  775. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  776. uci->mc = NULL;
  777. }
  778. static struct microcode_ops microcode_amd_ops = {
  779. .request_microcode_user = request_microcode_user,
  780. .request_microcode_fw = request_microcode_amd,
  781. .collect_cpu_info = collect_cpu_info_amd,
  782. .apply_microcode = apply_microcode_amd,
  783. .microcode_fini_cpu = microcode_fini_cpu_amd,
  784. };
  785. struct microcode_ops * __init init_amd_microcode(void)
  786. {
  787. struct cpuinfo_x86 *c = &boot_cpu_data;
  788. if (c->x86_vendor != X86_VENDOR_AMD || c->x86 < 0x10) {
  789. pr_warn("AMD CPU family 0x%x not supported\n", c->x86);
  790. return NULL;
  791. }
  792. if (ucode_new_rev)
  793. pr_info_once("microcode updated early to new patch_level=0x%08x\n",
  794. ucode_new_rev);
  795. return &microcode_amd_ops;
  796. }
  797. void __exit exit_amd_microcode(void)
  798. {
  799. cleanup();
  800. }