microcode_amd.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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 <andreas.herrmann3@amd.com>
  12. * Borislav Petkov <borislav.petkov@amd.com>
  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. /* page-sized ucode patch buffer */
  68. void *patch;
  69. static int collect_cpu_info_amd(int cpu, struct cpu_signature *csig)
  70. {
  71. struct cpuinfo_x86 *c = &cpu_data(cpu);
  72. csig->sig = cpuid_eax(0x00000001);
  73. csig->rev = c->microcode;
  74. pr_info("CPU%d: patch_level=0x%08x\n", cpu, csig->rev);
  75. return 0;
  76. }
  77. static unsigned int verify_ucode_size(int cpu, u32 patch_size,
  78. unsigned int size)
  79. {
  80. struct cpuinfo_x86 *c = &cpu_data(cpu);
  81. u32 max_size;
  82. #define F1XH_MPB_MAX_SIZE 2048
  83. #define F14H_MPB_MAX_SIZE 1824
  84. #define F15H_MPB_MAX_SIZE 4096
  85. switch (c->x86) {
  86. case 0x14:
  87. max_size = F14H_MPB_MAX_SIZE;
  88. break;
  89. case 0x15:
  90. max_size = F15H_MPB_MAX_SIZE;
  91. break;
  92. default:
  93. max_size = F1XH_MPB_MAX_SIZE;
  94. break;
  95. }
  96. if (patch_size > min_t(u32, size, max_size)) {
  97. pr_err("patch size mismatch\n");
  98. return 0;
  99. }
  100. return patch_size;
  101. }
  102. static u16 find_equiv_id(unsigned int cpu)
  103. {
  104. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  105. int i = 0;
  106. BUG_ON(equiv_cpu_table == NULL);
  107. while (equiv_cpu_table[i].installed_cpu != 0) {
  108. if (uci->cpu_sig.sig == equiv_cpu_table[i].installed_cpu)
  109. return equiv_cpu_table[i].equiv_cpu;
  110. i++;
  111. }
  112. return 0;
  113. }
  114. /*
  115. * we signal a good patch is found by returning its size > 0
  116. */
  117. static int get_matching_microcode(int cpu, const u8 *ucode_ptr,
  118. unsigned int leftover_size, int rev,
  119. unsigned int *current_size)
  120. {
  121. struct microcode_header_amd *mc_hdr;
  122. unsigned int actual_size, patch_size;
  123. u16 equiv_cpu_id;
  124. /* size of the current patch we're staring at */
  125. patch_size = *(u32 *)(ucode_ptr + 4);
  126. *current_size = patch_size + SECTION_HDR_SIZE;
  127. equiv_cpu_id = find_equiv_id(cpu);
  128. if (!equiv_cpu_id)
  129. return 0;
  130. /*
  131. * let's look at the patch header itself now
  132. */
  133. mc_hdr = (struct microcode_header_amd *)(ucode_ptr + SECTION_HDR_SIZE);
  134. if (mc_hdr->processor_rev_id != equiv_cpu_id)
  135. return 0;
  136. /* ucode might be chipset specific -- currently we don't support this */
  137. if (mc_hdr->nb_dev_id || mc_hdr->sb_dev_id) {
  138. pr_err("CPU%d: chipset specific code not yet supported\n",
  139. cpu);
  140. return 0;
  141. }
  142. if (mc_hdr->patch_id <= rev)
  143. return 0;
  144. /*
  145. * now that the header looks sane, verify its size
  146. */
  147. actual_size = verify_ucode_size(cpu, patch_size, leftover_size);
  148. if (!actual_size)
  149. return 0;
  150. /* clear the patch buffer */
  151. memset(patch, 0, PAGE_SIZE);
  152. /* all looks ok, get the binary patch */
  153. memcpy(patch, ucode_ptr + SECTION_HDR_SIZE, actual_size);
  154. return actual_size;
  155. }
  156. static int apply_microcode_amd(int cpu)
  157. {
  158. u32 rev, dummy;
  159. int cpu_num = raw_smp_processor_id();
  160. struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
  161. struct microcode_amd *mc_amd = uci->mc;
  162. struct cpuinfo_x86 *c = &cpu_data(cpu);
  163. /* We should bind the task to the CPU */
  164. BUG_ON(cpu_num != cpu);
  165. if (mc_amd == NULL)
  166. return 0;
  167. rdmsr(MSR_AMD64_PATCH_LEVEL, rev, dummy);
  168. /* need to apply patch? */
  169. if (rev >= mc_amd->hdr.patch_id) {
  170. c->microcode = rev;
  171. return 0;
  172. }
  173. wrmsrl(MSR_AMD64_PATCH_LOADER, (u64)(long)&mc_amd->hdr.data_code);
  174. /* verify patch application was successful */
  175. rdmsr(MSR_AMD64_PATCH_LEVEL, rev, dummy);
  176. if (rev != mc_amd->hdr.patch_id) {
  177. pr_err("CPU%d: update failed for patch_level=0x%08x\n",
  178. cpu, mc_amd->hdr.patch_id);
  179. return -1;
  180. }
  181. pr_info("CPU%d: new patch_level=0x%08x\n", cpu, rev);
  182. uci->cpu_sig.rev = rev;
  183. c->microcode = rev;
  184. return 0;
  185. }
  186. static int install_equiv_cpu_table(const u8 *buf)
  187. {
  188. unsigned int *ibuf = (unsigned int *)buf;
  189. unsigned int type = ibuf[1];
  190. unsigned int size = ibuf[2];
  191. if (type != UCODE_EQUIV_CPU_TABLE_TYPE || !size) {
  192. pr_err("empty section/"
  193. "invalid type field in container file section header\n");
  194. return -EINVAL;
  195. }
  196. equiv_cpu_table = vmalloc(size);
  197. if (!equiv_cpu_table) {
  198. pr_err("failed to allocate equivalent CPU table\n");
  199. return -ENOMEM;
  200. }
  201. memcpy(equiv_cpu_table, buf + CONTAINER_HDR_SZ, size);
  202. /* add header length */
  203. return size + CONTAINER_HDR_SZ;
  204. }
  205. static void free_equiv_cpu_table(void)
  206. {
  207. vfree(equiv_cpu_table);
  208. equiv_cpu_table = NULL;
  209. }
  210. static enum ucode_state
  211. generic_load_microcode(int cpu, const u8 *data, size_t size)
  212. {
  213. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  214. struct microcode_header_amd *mc_hdr = NULL;
  215. unsigned int mc_size, leftover, current_size = 0;
  216. int offset;
  217. const u8 *ucode_ptr = data;
  218. void *new_mc = NULL;
  219. unsigned int new_rev = uci->cpu_sig.rev;
  220. enum ucode_state state = UCODE_ERROR;
  221. offset = install_equiv_cpu_table(ucode_ptr);
  222. if (offset < 0) {
  223. pr_err("failed to create equivalent cpu table\n");
  224. goto out;
  225. }
  226. ucode_ptr += offset;
  227. leftover = size - offset;
  228. if (*(u32 *)ucode_ptr != UCODE_UCODE_TYPE) {
  229. pr_err("invalid type field in container file section header\n");
  230. goto free_table;
  231. }
  232. while (leftover) {
  233. mc_size = get_matching_microcode(cpu, ucode_ptr, leftover,
  234. new_rev, &current_size);
  235. if (mc_size) {
  236. mc_hdr = patch;
  237. new_mc = patch;
  238. new_rev = mc_hdr->patch_id;
  239. goto out_ok;
  240. }
  241. ucode_ptr += current_size;
  242. leftover -= current_size;
  243. }
  244. if (!new_mc) {
  245. state = UCODE_NFOUND;
  246. goto free_table;
  247. }
  248. out_ok:
  249. uci->mc = new_mc;
  250. state = UCODE_OK;
  251. pr_debug("CPU%d update ucode (0x%08x -> 0x%08x)\n",
  252. cpu, uci->cpu_sig.rev, new_rev);
  253. free_table:
  254. free_equiv_cpu_table();
  255. out:
  256. return state;
  257. }
  258. /*
  259. * AMD microcode firmware naming convention, up to family 15h they are in
  260. * the legacy file:
  261. *
  262. * amd-ucode/microcode_amd.bin
  263. *
  264. * This legacy file is always smaller than 2K in size.
  265. *
  266. * Starting at family 15h they are in family specific firmware files:
  267. *
  268. * amd-ucode/microcode_amd_fam15h.bin
  269. * amd-ucode/microcode_amd_fam16h.bin
  270. * ...
  271. *
  272. * These might be larger than 2K.
  273. */
  274. static enum ucode_state request_microcode_amd(int cpu, struct device *device,
  275. bool refresh_fw)
  276. {
  277. char fw_name[36] = "amd-ucode/microcode_amd.bin";
  278. const struct firmware *fw;
  279. enum ucode_state ret = UCODE_NFOUND;
  280. struct cpuinfo_x86 *c = &cpu_data(cpu);
  281. if (c->x86 >= 0x15)
  282. snprintf(fw_name, sizeof(fw_name), "amd-ucode/microcode_amd_fam%.2xh.bin", c->x86);
  283. if (request_firmware(&fw, (const char *)fw_name, device)) {
  284. pr_err("failed to load file %s\n", fw_name);
  285. goto out;
  286. }
  287. ret = UCODE_ERROR;
  288. if (*(u32 *)fw->data != UCODE_MAGIC) {
  289. pr_err("invalid magic value (0x%08x)\n", *(u32 *)fw->data);
  290. goto fw_release;
  291. }
  292. ret = generic_load_microcode(cpu, fw->data, fw->size);
  293. fw_release:
  294. release_firmware(fw);
  295. out:
  296. return ret;
  297. }
  298. static enum ucode_state
  299. request_microcode_user(int cpu, const void __user *buf, size_t size)
  300. {
  301. return UCODE_ERROR;
  302. }
  303. static void microcode_fini_cpu_amd(int cpu)
  304. {
  305. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  306. uci->mc = NULL;
  307. }
  308. static struct microcode_ops microcode_amd_ops = {
  309. .request_microcode_user = request_microcode_user,
  310. .request_microcode_fw = request_microcode_amd,
  311. .collect_cpu_info = collect_cpu_info_amd,
  312. .apply_microcode = apply_microcode_amd,
  313. .microcode_fini_cpu = microcode_fini_cpu_amd,
  314. };
  315. struct microcode_ops * __init init_amd_microcode(void)
  316. {
  317. struct cpuinfo_x86 *c = &cpu_data(0);
  318. if (c->x86_vendor != X86_VENDOR_AMD || c->x86 < 0x10) {
  319. pr_warning("AMD CPU family 0x%x not supported\n", c->x86);
  320. return NULL;
  321. }
  322. patch = (void *)get_zeroed_page(GFP_KERNEL);
  323. if (!patch)
  324. return NULL;
  325. return &microcode_amd_ops;
  326. }
  327. void __exit exit_amd_microcode(void)
  328. {
  329. free_page((unsigned long)patch);
  330. }