amd.c 22 KB

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