microcode_amd.c 11 KB

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