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