microcode_amd.c 11 KB

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