amd.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. /*
  2. * AMD CPU Microcode Update Driver for Linux
  3. * Copyright (C) 2008-2011 Advanced Micro Devices Inc.
  4. *
  5. * Author: Peter Oruba <peter.oruba@amd.com>
  6. *
  7. * Based on work by:
  8. * Tigran Aivazian <tigran@aivazian.fsnet.co.uk>
  9. *
  10. * Maintainers:
  11. * Andreas Herrmann <herrmann.der.user@googlemail.com>
  12. * Borislav Petkov <bp@alien8.de>
  13. *
  14. * This driver allows to upgrade microcode on F10h AMD
  15. * CPUs and later.
  16. *
  17. * Licensed under the terms of the GNU General Public
  18. * License version 2. See file COPYING for details.
  19. */
  20. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  21. #include <linux/firmware.h>
  22. #include <linux/uaccess.h>
  23. #include <linux/vmalloc.h>
  24. #include <linux/kernel.h>
  25. #include <linux/module.h>
  26. #include <linux/pci.h>
  27. #include <asm/microcode.h>
  28. #include <asm/processor.h>
  29. #include <asm/msr.h>
  30. #include <asm/microcode_amd.h>
  31. MODULE_DESCRIPTION("AMD Microcode Update Driver");
  32. MODULE_AUTHOR("Peter Oruba");
  33. MODULE_LICENSE("GPL v2");
  34. static struct equiv_cpu_entry *equiv_cpu_table;
  35. struct ucode_patch {
  36. struct list_head plist;
  37. void *data;
  38. u32 patch_id;
  39. u16 equiv_cpu;
  40. };
  41. static LIST_HEAD(pcache);
  42. static u16 __find_equiv_id(unsigned int cpu)
  43. {
  44. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  45. return find_equiv_id(equiv_cpu_table, uci->cpu_sig.sig);
  46. }
  47. static u32 find_cpu_family_by_equiv_cpu(u16 equiv_cpu)
  48. {
  49. int i = 0;
  50. BUG_ON(!equiv_cpu_table);
  51. while (equiv_cpu_table[i].equiv_cpu != 0) {
  52. if (equiv_cpu == equiv_cpu_table[i].equiv_cpu)
  53. return equiv_cpu_table[i].installed_cpu;
  54. i++;
  55. }
  56. return 0;
  57. }
  58. /*
  59. * a small, trivial cache of per-family ucode patches
  60. */
  61. static struct ucode_patch *cache_find_patch(u16 equiv_cpu)
  62. {
  63. struct ucode_patch *p;
  64. list_for_each_entry(p, &pcache, plist)
  65. if (p->equiv_cpu == equiv_cpu)
  66. return p;
  67. return NULL;
  68. }
  69. static void update_cache(struct ucode_patch *new_patch)
  70. {
  71. struct ucode_patch *p;
  72. list_for_each_entry(p, &pcache, plist) {
  73. if (p->equiv_cpu == new_patch->equiv_cpu) {
  74. if (p->patch_id >= new_patch->patch_id)
  75. /* we already have the latest patch */
  76. return;
  77. list_replace(&p->plist, &new_patch->plist);
  78. kfree(p->data);
  79. kfree(p);
  80. return;
  81. }
  82. }
  83. /* no patch found, add it */
  84. list_add_tail(&new_patch->plist, &pcache);
  85. }
  86. static void free_cache(void)
  87. {
  88. struct ucode_patch *p, *tmp;
  89. list_for_each_entry_safe(p, tmp, &pcache, plist) {
  90. __list_del(p->plist.prev, p->plist.next);
  91. kfree(p->data);
  92. kfree(p);
  93. }
  94. }
  95. static struct ucode_patch *find_patch(unsigned int cpu)
  96. {
  97. u16 equiv_id;
  98. equiv_id = __find_equiv_id(cpu);
  99. if (!equiv_id)
  100. return NULL;
  101. return cache_find_patch(equiv_id);
  102. }
  103. static int collect_cpu_info_amd(int cpu, struct cpu_signature *csig)
  104. {
  105. struct cpuinfo_x86 *c = &cpu_data(cpu);
  106. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  107. struct ucode_patch *p;
  108. csig->sig = cpuid_eax(0x00000001);
  109. csig->rev = c->microcode;
  110. /*
  111. * a patch could have been loaded early, set uci->mc so that
  112. * mc_bp_resume() can call apply_microcode()
  113. */
  114. p = find_patch(cpu);
  115. if (p && (p->patch_id == csig->rev))
  116. uci->mc = p->data;
  117. pr_info("CPU%d: patch_level=0x%08x\n", cpu, csig->rev);
  118. return 0;
  119. }
  120. static unsigned int verify_patch_size(u8 family, u32 patch_size,
  121. unsigned int size)
  122. {
  123. u32 max_size;
  124. #define F1XH_MPB_MAX_SIZE 2048
  125. #define F14H_MPB_MAX_SIZE 1824
  126. #define F15H_MPB_MAX_SIZE 4096
  127. #define F16H_MPB_MAX_SIZE 3458
  128. switch (family) {
  129. case 0x14:
  130. max_size = F14H_MPB_MAX_SIZE;
  131. break;
  132. case 0x15:
  133. max_size = F15H_MPB_MAX_SIZE;
  134. break;
  135. case 0x16:
  136. max_size = F16H_MPB_MAX_SIZE;
  137. break;
  138. default:
  139. max_size = F1XH_MPB_MAX_SIZE;
  140. break;
  141. }
  142. if (patch_size > min_t(u32, size, max_size)) {
  143. pr_err("patch size mismatch\n");
  144. return 0;
  145. }
  146. return patch_size;
  147. }
  148. /*
  149. * Those patch levels cannot be updated to newer ones and thus should be final.
  150. */
  151. static u32 final_levels[] = {
  152. 0x01000098,
  153. 0x0100009f,
  154. 0x010000af,
  155. 0, /* T-101 terminator */
  156. };
  157. /*
  158. * Check the current patch level on this CPU.
  159. *
  160. * @rev: Use it to return the patch level. It is set to 0 in the case of
  161. * error.
  162. *
  163. * Returns:
  164. * - true: if update should stop
  165. * - false: otherwise
  166. */
  167. bool check_current_patch_level(u32 *rev, bool early)
  168. {
  169. u32 lvl, dummy, i;
  170. bool ret = false;
  171. u32 *levels;
  172. native_rdmsr(MSR_AMD64_PATCH_LEVEL, lvl, dummy);
  173. if (IS_ENABLED(CONFIG_X86_32) && early)
  174. levels = (u32 *)__pa_nodebug(&final_levels);
  175. else
  176. levels = final_levels;
  177. for (i = 0; levels[i]; i++) {
  178. if (lvl == levels[i]) {
  179. lvl = 0;
  180. ret = true;
  181. break;
  182. }
  183. }
  184. if (rev)
  185. *rev = lvl;
  186. return ret;
  187. }
  188. int __apply_microcode_amd(struct microcode_amd *mc_amd)
  189. {
  190. u32 rev, dummy;
  191. native_wrmsrl(MSR_AMD64_PATCH_LOADER, (u64)(long)&mc_amd->hdr.data_code);
  192. /* verify patch application was successful */
  193. native_rdmsr(MSR_AMD64_PATCH_LEVEL, rev, dummy);
  194. if (rev != mc_amd->hdr.patch_id)
  195. return -1;
  196. return 0;
  197. }
  198. int apply_microcode_amd(int cpu)
  199. {
  200. struct cpuinfo_x86 *c = &cpu_data(cpu);
  201. struct microcode_amd *mc_amd;
  202. struct ucode_cpu_info *uci;
  203. struct ucode_patch *p;
  204. u32 rev;
  205. BUG_ON(raw_smp_processor_id() != cpu);
  206. uci = ucode_cpu_info + cpu;
  207. p = find_patch(cpu);
  208. if (!p)
  209. return 0;
  210. mc_amd = p->data;
  211. uci->mc = p->data;
  212. if (check_current_patch_level(&rev, false))
  213. return -1;
  214. /* need to apply patch? */
  215. if (rev >= mc_amd->hdr.patch_id) {
  216. c->microcode = rev;
  217. uci->cpu_sig.rev = rev;
  218. return 0;
  219. }
  220. if (__apply_microcode_amd(mc_amd)) {
  221. pr_err("CPU%d: update failed for patch_level=0x%08x\n",
  222. cpu, mc_amd->hdr.patch_id);
  223. return -1;
  224. }
  225. pr_info("CPU%d: new patch_level=0x%08x\n", cpu,
  226. mc_amd->hdr.patch_id);
  227. uci->cpu_sig.rev = mc_amd->hdr.patch_id;
  228. c->microcode = mc_amd->hdr.patch_id;
  229. return 0;
  230. }
  231. static int install_equiv_cpu_table(const u8 *buf)
  232. {
  233. unsigned int *ibuf = (unsigned int *)buf;
  234. unsigned int type = ibuf[1];
  235. unsigned int size = ibuf[2];
  236. if (type != UCODE_EQUIV_CPU_TABLE_TYPE || !size) {
  237. pr_err("empty section/"
  238. "invalid type field in container file section header\n");
  239. return -EINVAL;
  240. }
  241. equiv_cpu_table = vmalloc(size);
  242. if (!equiv_cpu_table) {
  243. pr_err("failed to allocate equivalent CPU table\n");
  244. return -ENOMEM;
  245. }
  246. memcpy(equiv_cpu_table, buf + CONTAINER_HDR_SZ, size);
  247. /* add header length */
  248. return size + CONTAINER_HDR_SZ;
  249. }
  250. static void free_equiv_cpu_table(void)
  251. {
  252. vfree(equiv_cpu_table);
  253. equiv_cpu_table = NULL;
  254. }
  255. static void cleanup(void)
  256. {
  257. free_equiv_cpu_table();
  258. free_cache();
  259. }
  260. /*
  261. * We return the current size even if some of the checks failed so that
  262. * we can skip over the next patch. If we return a negative value, we
  263. * signal a grave error like a memory allocation has failed and the
  264. * driver cannot continue functioning normally. In such cases, we tear
  265. * down everything we've used up so far and exit.
  266. */
  267. static int verify_and_add_patch(u8 family, u8 *fw, unsigned int leftover)
  268. {
  269. struct microcode_header_amd *mc_hdr;
  270. struct ucode_patch *patch;
  271. unsigned int patch_size, crnt_size, ret;
  272. u32 proc_fam;
  273. u16 proc_id;
  274. patch_size = *(u32 *)(fw + 4);
  275. crnt_size = patch_size + SECTION_HDR_SIZE;
  276. mc_hdr = (struct microcode_header_amd *)(fw + SECTION_HDR_SIZE);
  277. proc_id = mc_hdr->processor_rev_id;
  278. proc_fam = find_cpu_family_by_equiv_cpu(proc_id);
  279. if (!proc_fam) {
  280. pr_err("No patch family for equiv ID: 0x%04x\n", proc_id);
  281. return crnt_size;
  282. }
  283. /* check if patch is for the current family */
  284. proc_fam = ((proc_fam >> 8) & 0xf) + ((proc_fam >> 20) & 0xff);
  285. if (proc_fam != family)
  286. return crnt_size;
  287. if (mc_hdr->nb_dev_id || mc_hdr->sb_dev_id) {
  288. pr_err("Patch-ID 0x%08x: chipset-specific code unsupported.\n",
  289. mc_hdr->patch_id);
  290. return crnt_size;
  291. }
  292. ret = verify_patch_size(family, patch_size, leftover);
  293. if (!ret) {
  294. pr_err("Patch-ID 0x%08x: size mismatch.\n", mc_hdr->patch_id);
  295. return crnt_size;
  296. }
  297. patch = kzalloc(sizeof(*patch), GFP_KERNEL);
  298. if (!patch) {
  299. pr_err("Patch allocation failure.\n");
  300. return -EINVAL;
  301. }
  302. patch->data = kzalloc(patch_size, GFP_KERNEL);
  303. if (!patch->data) {
  304. pr_err("Patch data allocation failure.\n");
  305. kfree(patch);
  306. return -EINVAL;
  307. }
  308. /* All looks ok, copy patch... */
  309. memcpy(patch->data, fw + SECTION_HDR_SIZE, patch_size);
  310. INIT_LIST_HEAD(&patch->plist);
  311. patch->patch_id = mc_hdr->patch_id;
  312. patch->equiv_cpu = proc_id;
  313. pr_debug("%s: Added patch_id: 0x%08x, proc_id: 0x%04x\n",
  314. __func__, patch->patch_id, proc_id);
  315. /* ... and add to cache. */
  316. update_cache(patch);
  317. return crnt_size;
  318. }
  319. static enum ucode_state __load_microcode_amd(u8 family, const u8 *data,
  320. size_t size)
  321. {
  322. enum ucode_state ret = UCODE_ERROR;
  323. unsigned int leftover;
  324. u8 *fw = (u8 *)data;
  325. int crnt_size = 0;
  326. int offset;
  327. offset = install_equiv_cpu_table(data);
  328. if (offset < 0) {
  329. pr_err("failed to create equivalent cpu table\n");
  330. return ret;
  331. }
  332. fw += offset;
  333. leftover = size - offset;
  334. if (*(u32 *)fw != UCODE_UCODE_TYPE) {
  335. pr_err("invalid type field in container file section header\n");
  336. free_equiv_cpu_table();
  337. return ret;
  338. }
  339. while (leftover) {
  340. crnt_size = verify_and_add_patch(family, fw, leftover);
  341. if (crnt_size < 0)
  342. return ret;
  343. fw += crnt_size;
  344. leftover -= crnt_size;
  345. }
  346. return UCODE_OK;
  347. }
  348. enum ucode_state load_microcode_amd(int cpu, u8 family, const u8 *data, size_t size)
  349. {
  350. enum ucode_state ret;
  351. /* free old equiv table */
  352. free_equiv_cpu_table();
  353. ret = __load_microcode_amd(family, data, size);
  354. if (ret != UCODE_OK)
  355. cleanup();
  356. #if defined(CONFIG_MICROCODE_AMD_EARLY) && defined(CONFIG_X86_32)
  357. /* save BSP's matching patch for early load */
  358. if (cpu_data(cpu).cpu_index == boot_cpu_data.cpu_index) {
  359. struct ucode_patch *p = find_patch(cpu);
  360. if (p) {
  361. memset(amd_ucode_patch, 0, PATCH_MAX_SIZE);
  362. memcpy(amd_ucode_patch, p->data, min_t(u32, ksize(p->data),
  363. PATCH_MAX_SIZE));
  364. }
  365. }
  366. #endif
  367. return ret;
  368. }
  369. /*
  370. * AMD microcode firmware naming convention, up to family 15h they are in
  371. * the legacy file:
  372. *
  373. * amd-ucode/microcode_amd.bin
  374. *
  375. * This legacy file is always smaller than 2K in size.
  376. *
  377. * Beginning with family 15h, they are in family-specific firmware files:
  378. *
  379. * amd-ucode/microcode_amd_fam15h.bin
  380. * amd-ucode/microcode_amd_fam16h.bin
  381. * ...
  382. *
  383. * These might be larger than 2K.
  384. */
  385. static enum ucode_state request_microcode_amd(int cpu, struct device *device,
  386. bool refresh_fw)
  387. {
  388. char fw_name[36] = "amd-ucode/microcode_amd.bin";
  389. struct cpuinfo_x86 *c = &cpu_data(cpu);
  390. enum ucode_state ret = UCODE_NFOUND;
  391. const struct firmware *fw;
  392. /* reload ucode container only on the boot cpu */
  393. if (!refresh_fw || c->cpu_index != boot_cpu_data.cpu_index)
  394. return UCODE_OK;
  395. if (c->x86 >= 0x15)
  396. snprintf(fw_name, sizeof(fw_name), "amd-ucode/microcode_amd_fam%.2xh.bin", c->x86);
  397. if (request_firmware_direct(&fw, (const char *)fw_name, device)) {
  398. pr_debug("failed to load file %s\n", fw_name);
  399. goto out;
  400. }
  401. ret = UCODE_ERROR;
  402. if (*(u32 *)fw->data != UCODE_MAGIC) {
  403. pr_err("invalid magic value (0x%08x)\n", *(u32 *)fw->data);
  404. goto fw_release;
  405. }
  406. ret = load_microcode_amd(cpu, c->x86, fw->data, fw->size);
  407. fw_release:
  408. release_firmware(fw);
  409. out:
  410. return ret;
  411. }
  412. static enum ucode_state
  413. request_microcode_user(int cpu, const void __user *buf, size_t size)
  414. {
  415. return UCODE_ERROR;
  416. }
  417. static void microcode_fini_cpu_amd(int cpu)
  418. {
  419. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  420. uci->mc = NULL;
  421. }
  422. static struct microcode_ops microcode_amd_ops = {
  423. .request_microcode_user = request_microcode_user,
  424. .request_microcode_fw = request_microcode_amd,
  425. .collect_cpu_info = collect_cpu_info_amd,
  426. .apply_microcode = apply_microcode_amd,
  427. .microcode_fini_cpu = microcode_fini_cpu_amd,
  428. };
  429. struct microcode_ops * __init init_amd_microcode(void)
  430. {
  431. struct cpuinfo_x86 *c = &boot_cpu_data;
  432. if (c->x86_vendor != X86_VENDOR_AMD || c->x86 < 0x10) {
  433. pr_warning("AMD CPU family 0x%x not supported\n", c->x86);
  434. return NULL;
  435. }
  436. return &microcode_amd_ops;
  437. }
  438. void __exit exit_amd_microcode(void)
  439. {
  440. cleanup();
  441. }