microcode_amd.c 10 KB

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