amd.c 22 KB

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