amd.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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/uaccess.h>
  23. #include <linux/vmalloc.h>
  24. #include <linux/kernel.h>
  25. #include <linux/module.h>
  26. #include <linux/pci.h>
  27. #include <asm/microcode.h>
  28. #include <asm/processor.h>
  29. #include <asm/msr.h>
  30. #include <asm/microcode_amd.h>
  31. MODULE_DESCRIPTION("AMD Microcode Update Driver");
  32. MODULE_AUTHOR("Peter Oruba");
  33. MODULE_LICENSE("GPL v2");
  34. static struct equiv_cpu_entry *equiv_cpu_table;
  35. struct ucode_patch {
  36. struct list_head plist;
  37. void *data;
  38. u32 patch_id;
  39. u16 equiv_cpu;
  40. };
  41. static LIST_HEAD(pcache);
  42. static u16 __find_equiv_id(unsigned int cpu)
  43. {
  44. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  45. return find_equiv_id(equiv_cpu_table, uci->cpu_sig.sig);
  46. }
  47. static u32 find_cpu_family_by_equiv_cpu(u16 equiv_cpu)
  48. {
  49. int i = 0;
  50. BUG_ON(!equiv_cpu_table);
  51. while (equiv_cpu_table[i].equiv_cpu != 0) {
  52. if (equiv_cpu == equiv_cpu_table[i].equiv_cpu)
  53. return equiv_cpu_table[i].installed_cpu;
  54. i++;
  55. }
  56. return 0;
  57. }
  58. /*
  59. * a small, trivial cache of per-family ucode patches
  60. */
  61. static struct ucode_patch *cache_find_patch(u16 equiv_cpu)
  62. {
  63. struct ucode_patch *p;
  64. list_for_each_entry(p, &pcache, plist)
  65. if (p->equiv_cpu == equiv_cpu)
  66. return p;
  67. return NULL;
  68. }
  69. static void update_cache(struct ucode_patch *new_patch)
  70. {
  71. struct ucode_patch *p;
  72. list_for_each_entry(p, &pcache, plist) {
  73. if (p->equiv_cpu == new_patch->equiv_cpu) {
  74. if (p->patch_id >= new_patch->patch_id)
  75. /* we already have the latest patch */
  76. return;
  77. list_replace(&p->plist, &new_patch->plist);
  78. kfree(p->data);
  79. kfree(p);
  80. return;
  81. }
  82. }
  83. /* no patch found, add it */
  84. list_add_tail(&new_patch->plist, &pcache);
  85. }
  86. static void free_cache(void)
  87. {
  88. struct ucode_patch *p, *tmp;
  89. list_for_each_entry_safe(p, tmp, &pcache, plist) {
  90. __list_del(p->plist.prev, p->plist.next);
  91. kfree(p->data);
  92. kfree(p);
  93. }
  94. }
  95. static struct ucode_patch *find_patch(unsigned int cpu)
  96. {
  97. u16 equiv_id;
  98. equiv_id = __find_equiv_id(cpu);
  99. if (!equiv_id)
  100. return NULL;
  101. return cache_find_patch(equiv_id);
  102. }
  103. static int collect_cpu_info_amd(int cpu, struct cpu_signature *csig)
  104. {
  105. struct cpuinfo_x86 *c = &cpu_data(cpu);
  106. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  107. struct ucode_patch *p;
  108. csig->sig = cpuid_eax(0x00000001);
  109. csig->rev = c->microcode;
  110. /*
  111. * a patch could have been loaded early, set uci->mc so that
  112. * mc_bp_resume() can call apply_microcode()
  113. */
  114. p = find_patch(cpu);
  115. if (p && (p->patch_id == csig->rev))
  116. uci->mc = p->data;
  117. pr_info("CPU%d: patch_level=0x%08x\n", cpu, csig->rev);
  118. return 0;
  119. }
  120. static unsigned int verify_patch_size(u8 family, u32 patch_size,
  121. unsigned int size)
  122. {
  123. u32 max_size;
  124. #define F1XH_MPB_MAX_SIZE 2048
  125. #define F14H_MPB_MAX_SIZE 1824
  126. #define F15H_MPB_MAX_SIZE 4096
  127. #define F16H_MPB_MAX_SIZE 3458
  128. switch (family) {
  129. case 0x14:
  130. max_size = F14H_MPB_MAX_SIZE;
  131. break;
  132. case 0x15:
  133. max_size = F15H_MPB_MAX_SIZE;
  134. break;
  135. case 0x16:
  136. max_size = F16H_MPB_MAX_SIZE;
  137. break;
  138. default:
  139. max_size = F1XH_MPB_MAX_SIZE;
  140. break;
  141. }
  142. if (patch_size > min_t(u32, size, max_size)) {
  143. pr_err("patch size mismatch\n");
  144. return 0;
  145. }
  146. return patch_size;
  147. }
  148. int __apply_microcode_amd(struct microcode_amd *mc_amd)
  149. {
  150. u32 rev, dummy;
  151. native_wrmsrl(MSR_AMD64_PATCH_LOADER, (u64)(long)&mc_amd->hdr.data_code);
  152. /* verify patch application was successful */
  153. native_rdmsr(MSR_AMD64_PATCH_LEVEL, rev, dummy);
  154. if (rev != mc_amd->hdr.patch_id)
  155. return -1;
  156. return 0;
  157. }
  158. int apply_microcode_amd(int cpu)
  159. {
  160. struct cpuinfo_x86 *c = &cpu_data(cpu);
  161. struct microcode_amd *mc_amd;
  162. struct ucode_cpu_info *uci;
  163. struct ucode_patch *p;
  164. u32 rev, dummy;
  165. BUG_ON(raw_smp_processor_id() != cpu);
  166. uci = ucode_cpu_info + cpu;
  167. p = find_patch(cpu);
  168. if (!p)
  169. return 0;
  170. mc_amd = p->data;
  171. uci->mc = p->data;
  172. rdmsr(MSR_AMD64_PATCH_LEVEL, rev, dummy);
  173. /* need to apply patch? */
  174. if (rev >= mc_amd->hdr.patch_id) {
  175. c->microcode = rev;
  176. uci->cpu_sig.rev = 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. pr_debug("%s: Added patch_id: 0x%08x, proc_id: 0x%04x\n",
  273. __func__, patch->patch_id, proc_id);
  274. /* ... and add to cache. */
  275. update_cache(patch);
  276. return crnt_size;
  277. }
  278. static enum ucode_state __load_microcode_amd(u8 family, const u8 *data,
  279. size_t size)
  280. {
  281. enum ucode_state ret = UCODE_ERROR;
  282. unsigned int leftover;
  283. u8 *fw = (u8 *)data;
  284. int crnt_size = 0;
  285. int offset;
  286. offset = install_equiv_cpu_table(data);
  287. if (offset < 0) {
  288. pr_err("failed to create equivalent cpu table\n");
  289. return ret;
  290. }
  291. fw += offset;
  292. leftover = size - offset;
  293. if (*(u32 *)fw != UCODE_UCODE_TYPE) {
  294. pr_err("invalid type field in container file section header\n");
  295. free_equiv_cpu_table();
  296. return ret;
  297. }
  298. while (leftover) {
  299. crnt_size = verify_and_add_patch(family, fw, leftover);
  300. if (crnt_size < 0)
  301. return ret;
  302. fw += crnt_size;
  303. leftover -= crnt_size;
  304. }
  305. return UCODE_OK;
  306. }
  307. enum ucode_state load_microcode_amd(int cpu, u8 family, const u8 *data, size_t size)
  308. {
  309. enum ucode_state ret;
  310. /* free old equiv table */
  311. free_equiv_cpu_table();
  312. ret = __load_microcode_amd(family, data, size);
  313. if (ret != UCODE_OK)
  314. cleanup();
  315. #if defined(CONFIG_MICROCODE_AMD_EARLY) && defined(CONFIG_X86_32)
  316. /* save BSP's matching patch for early load */
  317. if (cpu_data(cpu).cpu_index == boot_cpu_data.cpu_index) {
  318. struct ucode_patch *p = find_patch(cpu);
  319. if (p) {
  320. memset(amd_ucode_patch, 0, PATCH_MAX_SIZE);
  321. memcpy(amd_ucode_patch, p->data, min_t(u32, ksize(p->data),
  322. PATCH_MAX_SIZE));
  323. }
  324. }
  325. #endif
  326. return ret;
  327. }
  328. /*(DEBLOBBED)*/
  329. static enum ucode_state request_microcode_amd(int cpu, struct device *device,
  330. bool refresh_fw)
  331. {
  332. char fw_name[36] = "/*(DEBLOBBED)*/";
  333. struct cpuinfo_x86 *c = &cpu_data(cpu);
  334. enum ucode_state ret = UCODE_NFOUND;
  335. const struct firmware *fw;
  336. /* reload ucode container only on the boot cpu */
  337. if (!refresh_fw || c->cpu_index != boot_cpu_data.cpu_index)
  338. return UCODE_OK;
  339. if (c->x86 >= 0x15)
  340. snprintf(fw_name, sizeof(fw_name), "/*(DEBLOBBED)*/", c->x86);
  341. if (reject_firmware_direct(&fw, (const char *)fw_name, device)) {
  342. pr_debug("failed to load file %s\n", fw_name);
  343. goto out;
  344. }
  345. ret = UCODE_ERROR;
  346. if (*(u32 *)fw->data != UCODE_MAGIC) {
  347. pr_err("invalid magic value (0x%08x)\n", *(u32 *)fw->data);
  348. goto fw_release;
  349. }
  350. ret = load_microcode_amd(cpu, c->x86, fw->data, fw->size);
  351. fw_release:
  352. release_firmware(fw);
  353. out:
  354. return ret;
  355. }
  356. static enum ucode_state
  357. request_microcode_user(int cpu, const void __user *buf, size_t size)
  358. {
  359. return UCODE_ERROR;
  360. }
  361. static void microcode_fini_cpu_amd(int cpu)
  362. {
  363. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  364. uci->mc = NULL;
  365. }
  366. static struct microcode_ops microcode_amd_ops = {
  367. .request_microcode_user = request_microcode_user,
  368. .request_microcode_fw = request_microcode_amd,
  369. .collect_cpu_info = collect_cpu_info_amd,
  370. .apply_microcode = apply_microcode_amd,
  371. .microcode_fini_cpu = microcode_fini_cpu_amd,
  372. };
  373. struct microcode_ops * __init init_amd_microcode(void)
  374. {
  375. struct cpuinfo_x86 *c = &cpu_data(0);
  376. if (c->x86_vendor != X86_VENDOR_AMD || c->x86 < 0x10) {
  377. pr_warning("AMD CPU family 0x%x not supported\n", c->x86);
  378. return NULL;
  379. }
  380. return &microcode_amd_ops;
  381. }
  382. void __exit exit_amd_microcode(void)
  383. {
  384. cleanup();
  385. }