microcode_amd.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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(u8 family, u32 patch_size,
  122. unsigned int size)
  123. {
  124. u32 max_size;
  125. #define F1XH_MPB_MAX_SIZE 2048
  126. #define F14H_MPB_MAX_SIZE 1824
  127. #define F15H_MPB_MAX_SIZE 4096
  128. #define F16H_MPB_MAX_SIZE 3458
  129. switch (family) {
  130. case 0x14:
  131. max_size = F14H_MPB_MAX_SIZE;
  132. break;
  133. case 0x15:
  134. max_size = F15H_MPB_MAX_SIZE;
  135. break;
  136. case 0x16:
  137. max_size = F16H_MPB_MAX_SIZE;
  138. break;
  139. default:
  140. max_size = F1XH_MPB_MAX_SIZE;
  141. break;
  142. }
  143. if (patch_size > min_t(u32, size, max_size)) {
  144. pr_err("patch size mismatch\n");
  145. return 0;
  146. }
  147. return patch_size;
  148. }
  149. int __apply_microcode_amd(struct microcode_amd *mc_amd)
  150. {
  151. u32 rev, dummy;
  152. wrmsrl(MSR_AMD64_PATCH_LOADER, (u64)(long)&mc_amd->hdr.data_code);
  153. /* verify patch application was successful */
  154. rdmsr(MSR_AMD64_PATCH_LEVEL, rev, dummy);
  155. if (rev != mc_amd->hdr.patch_id)
  156. return -1;
  157. return 0;
  158. }
  159. int apply_microcode_amd(int cpu)
  160. {
  161. struct cpuinfo_x86 *c = &cpu_data(cpu);
  162. struct microcode_amd *mc_amd;
  163. struct ucode_cpu_info *uci;
  164. struct ucode_patch *p;
  165. u32 rev, dummy;
  166. BUG_ON(raw_smp_processor_id() != cpu);
  167. uci = ucode_cpu_info + cpu;
  168. p = find_patch(cpu);
  169. if (!p)
  170. return 0;
  171. mc_amd = p->data;
  172. uci->mc = p->data;
  173. rdmsr(MSR_AMD64_PATCH_LEVEL, rev, dummy);
  174. /* need to apply patch? */
  175. if (rev >= mc_amd->hdr.patch_id) {
  176. c->microcode = rev;
  177. return 0;
  178. }
  179. if (__apply_microcode_amd(mc_amd)) {
  180. pr_err("CPU%d: update failed for patch_level=0x%08x\n",
  181. cpu, mc_amd->hdr.patch_id);
  182. return -1;
  183. }
  184. pr_info("CPU%d: new patch_level=0x%08x\n", cpu,
  185. mc_amd->hdr.patch_id);
  186. uci->cpu_sig.rev = mc_amd->hdr.patch_id;
  187. c->microcode = mc_amd->hdr.patch_id;
  188. return 0;
  189. }
  190. static int install_equiv_cpu_table(const u8 *buf)
  191. {
  192. unsigned int *ibuf = (unsigned int *)buf;
  193. unsigned int type = ibuf[1];
  194. unsigned int size = ibuf[2];
  195. if (type != UCODE_EQUIV_CPU_TABLE_TYPE || !size) {
  196. pr_err("empty section/"
  197. "invalid type field in container file section header\n");
  198. return -EINVAL;
  199. }
  200. equiv_cpu_table = vmalloc(size);
  201. if (!equiv_cpu_table) {
  202. pr_err("failed to allocate equivalent CPU table\n");
  203. return -ENOMEM;
  204. }
  205. memcpy(equiv_cpu_table, buf + CONTAINER_HDR_SZ, size);
  206. /* add header length */
  207. return size + CONTAINER_HDR_SZ;
  208. }
  209. static void free_equiv_cpu_table(void)
  210. {
  211. vfree(equiv_cpu_table);
  212. equiv_cpu_table = NULL;
  213. }
  214. static void cleanup(void)
  215. {
  216. free_equiv_cpu_table();
  217. free_cache();
  218. }
  219. /*
  220. * We return the current size even if some of the checks failed so that
  221. * we can skip over the next patch. If we return a negative value, we
  222. * signal a grave error like a memory allocation has failed and the
  223. * driver cannot continue functioning normally. In such cases, we tear
  224. * down everything we've used up so far and exit.
  225. */
  226. static int verify_and_add_patch(u8 family, u8 *fw, unsigned int leftover)
  227. {
  228. struct microcode_header_amd *mc_hdr;
  229. struct ucode_patch *patch;
  230. unsigned int patch_size, crnt_size, ret;
  231. u32 proc_fam;
  232. u16 proc_id;
  233. patch_size = *(u32 *)(fw + 4);
  234. crnt_size = patch_size + SECTION_HDR_SIZE;
  235. mc_hdr = (struct microcode_header_amd *)(fw + SECTION_HDR_SIZE);
  236. proc_id = mc_hdr->processor_rev_id;
  237. proc_fam = find_cpu_family_by_equiv_cpu(proc_id);
  238. if (!proc_fam) {
  239. pr_err("No patch family for equiv ID: 0x%04x\n", proc_id);
  240. return crnt_size;
  241. }
  242. /* check if patch is for the current family */
  243. proc_fam = ((proc_fam >> 8) & 0xf) + ((proc_fam >> 20) & 0xff);
  244. if (proc_fam != family)
  245. return crnt_size;
  246. if (mc_hdr->nb_dev_id || mc_hdr->sb_dev_id) {
  247. pr_err("Patch-ID 0x%08x: chipset-specific code unsupported.\n",
  248. mc_hdr->patch_id);
  249. return crnt_size;
  250. }
  251. ret = verify_patch_size(family, patch_size, leftover);
  252. if (!ret) {
  253. pr_err("Patch-ID 0x%08x: size mismatch.\n", mc_hdr->patch_id);
  254. return crnt_size;
  255. }
  256. patch = kzalloc(sizeof(*patch), GFP_KERNEL);
  257. if (!patch) {
  258. pr_err("Patch allocation failure.\n");
  259. return -EINVAL;
  260. }
  261. patch->data = kzalloc(patch_size, GFP_KERNEL);
  262. if (!patch->data) {
  263. pr_err("Patch data allocation failure.\n");
  264. kfree(patch);
  265. return -EINVAL;
  266. }
  267. /* All looks ok, copy patch... */
  268. memcpy(patch->data, fw + SECTION_HDR_SIZE, patch_size);
  269. INIT_LIST_HEAD(&patch->plist);
  270. patch->patch_id = mc_hdr->patch_id;
  271. patch->equiv_cpu = proc_id;
  272. /* ... and add to cache. */
  273. update_cache(patch);
  274. return crnt_size;
  275. }
  276. static enum ucode_state __load_microcode_amd(u8 family, const u8 *data,
  277. size_t size)
  278. {
  279. enum ucode_state ret = UCODE_ERROR;
  280. unsigned int leftover;
  281. u8 *fw = (u8 *)data;
  282. int crnt_size = 0;
  283. int offset;
  284. offset = install_equiv_cpu_table(data);
  285. if (offset < 0) {
  286. pr_err("failed to create equivalent cpu table\n");
  287. return ret;
  288. }
  289. fw += offset;
  290. leftover = size - offset;
  291. if (*(u32 *)fw != UCODE_UCODE_TYPE) {
  292. pr_err("invalid type field in container file section header\n");
  293. free_equiv_cpu_table();
  294. return ret;
  295. }
  296. while (leftover) {
  297. crnt_size = verify_and_add_patch(family, fw, leftover);
  298. if (crnt_size < 0)
  299. return ret;
  300. fw += crnt_size;
  301. leftover -= crnt_size;
  302. }
  303. return UCODE_OK;
  304. }
  305. enum ucode_state load_microcode_amd(u8 family, const u8 *data, size_t size)
  306. {
  307. enum ucode_state ret;
  308. /* free old equiv table */
  309. free_equiv_cpu_table();
  310. ret = __load_microcode_amd(family, data, size);
  311. if (ret != UCODE_OK)
  312. cleanup();
  313. #if defined(CONFIG_MICROCODE_AMD_EARLY) && defined(CONFIG_X86_32)
  314. /* save BSP's matching patch for early load */
  315. if (cpu_data(smp_processor_id()).cpu_index == boot_cpu_data.cpu_index) {
  316. struct ucode_patch *p = find_patch(smp_processor_id());
  317. if (p) {
  318. memset(amd_bsp_mpb, 0, MPB_MAX_SIZE);
  319. memcpy(amd_bsp_mpb, p->data, min_t(u32, ksize(p->data),
  320. MPB_MAX_SIZE));
  321. }
  322. }
  323. #endif
  324. return ret;
  325. }
  326. /*
  327. * AMD microcode firmware naming convention, up to family 15h they are in
  328. * the legacy file:
  329. *
  330. * amd-ucode/microcode_amd.bin
  331. *
  332. * This legacy file is always smaller than 2K in size.
  333. *
  334. * Beginning with family 15h, they are in family-specific firmware files:
  335. *
  336. * amd-ucode/microcode_amd_fam15h.bin
  337. * amd-ucode/microcode_amd_fam16h.bin
  338. * ...
  339. *
  340. * These might be larger than 2K.
  341. */
  342. static enum ucode_state request_microcode_amd(int cpu, struct device *device,
  343. bool refresh_fw)
  344. {
  345. char fw_name[36] = "amd-ucode/microcode_amd.bin";
  346. struct cpuinfo_x86 *c = &cpu_data(cpu);
  347. enum ucode_state ret = UCODE_NFOUND;
  348. const struct firmware *fw;
  349. /* reload ucode container only on the boot cpu */
  350. if (!refresh_fw || c->cpu_index != boot_cpu_data.cpu_index)
  351. return UCODE_OK;
  352. if (c->x86 >= 0x15)
  353. snprintf(fw_name, sizeof(fw_name), "amd-ucode/microcode_amd_fam%.2xh.bin", c->x86);
  354. if (request_firmware(&fw, (const char *)fw_name, device)) {
  355. pr_err("failed to load file %s\n", fw_name);
  356. goto out;
  357. }
  358. ret = UCODE_ERROR;
  359. if (*(u32 *)fw->data != UCODE_MAGIC) {
  360. pr_err("invalid magic value (0x%08x)\n", *(u32 *)fw->data);
  361. goto fw_release;
  362. }
  363. ret = load_microcode_amd(c->x86, fw->data, fw->size);
  364. fw_release:
  365. release_firmware(fw);
  366. out:
  367. return ret;
  368. }
  369. static enum ucode_state
  370. request_microcode_user(int cpu, const void __user *buf, size_t size)
  371. {
  372. return UCODE_ERROR;
  373. }
  374. static void microcode_fini_cpu_amd(int cpu)
  375. {
  376. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  377. uci->mc = NULL;
  378. }
  379. static struct microcode_ops microcode_amd_ops = {
  380. .request_microcode_user = request_microcode_user,
  381. .request_microcode_fw = request_microcode_amd,
  382. .collect_cpu_info = collect_cpu_info_amd,
  383. .apply_microcode = apply_microcode_amd,
  384. .microcode_fini_cpu = microcode_fini_cpu_amd,
  385. };
  386. struct microcode_ops * __init init_amd_microcode(void)
  387. {
  388. struct cpuinfo_x86 *c = &cpu_data(0);
  389. if (c->x86_vendor != X86_VENDOR_AMD || c->x86 < 0x10) {
  390. pr_warning("AMD CPU family 0x%x not supported\n", c->x86);
  391. return NULL;
  392. }
  393. return &microcode_amd_ops;
  394. }
  395. void __exit exit_amd_microcode(void)
  396. {
  397. cleanup();
  398. }