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