microcode_amd.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /*
  2. * AMD CPU Microcode Update Driver for Linux
  3. * Copyright (C) 2008 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. * This driver allows to upgrade microcode on AMD
  11. * family 0x10 and 0x11 processors.
  12. *
  13. * Licensed under the terms of the GNU General Public
  14. * License version 2. See file COPYING for details.
  15. */
  16. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  17. #include <linux/firmware.h>
  18. #include <linux/pci_ids.h>
  19. #include <linux/uaccess.h>
  20. #include <linux/vmalloc.h>
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/pci.h>
  24. #include <asm/microcode.h>
  25. #include <asm/processor.h>
  26. #include <asm/msr.h>
  27. MODULE_DESCRIPTION("AMD Microcode Update Driver");
  28. MODULE_AUTHOR("Peter Oruba");
  29. MODULE_LICENSE("GPL v2");
  30. #define UCODE_MAGIC 0x00414d44
  31. #define UCODE_EQUIV_CPU_TABLE_TYPE 0x00000000
  32. #define UCODE_UCODE_TYPE 0x00000001
  33. struct equiv_cpu_entry {
  34. u32 installed_cpu;
  35. u32 fixed_errata_mask;
  36. u32 fixed_errata_compare;
  37. u16 equiv_cpu;
  38. u16 res;
  39. } __attribute__((packed));
  40. struct microcode_header_amd {
  41. u32 data_code;
  42. u32 patch_id;
  43. u16 mc_patch_data_id;
  44. u8 mc_patch_data_len;
  45. u8 init_flag;
  46. u32 mc_patch_data_checksum;
  47. u32 nb_dev_id;
  48. u32 sb_dev_id;
  49. u16 processor_rev_id;
  50. u8 nb_rev_id;
  51. u8 sb_rev_id;
  52. u8 bios_api_rev;
  53. u8 reserved1[3];
  54. u32 match_reg[8];
  55. } __attribute__((packed));
  56. struct microcode_amd {
  57. struct microcode_header_amd hdr;
  58. unsigned int mpb[0];
  59. };
  60. #define SECTION_HDR_SIZE 8
  61. #define CONTAINER_HDR_SZ 12
  62. static struct equiv_cpu_entry *equiv_cpu_table;
  63. static int collect_cpu_info_amd(int cpu, struct cpu_signature *csig)
  64. {
  65. struct cpuinfo_x86 *c = &cpu_data(cpu);
  66. u32 dummy;
  67. if (c->x86_vendor != X86_VENDOR_AMD || c->x86 < 0x10) {
  68. pr_warning("CPU%d: family %d not supported\n", cpu, c->x86);
  69. return -1;
  70. }
  71. rdmsr(MSR_AMD64_PATCH_LEVEL, csig->rev, dummy);
  72. pr_info("CPU%d: patch_level=0x%08x\n", cpu, csig->rev);
  73. return 0;
  74. }
  75. static int get_matching_microcode(int cpu, struct microcode_header_amd *mc_hdr,
  76. int rev)
  77. {
  78. unsigned int current_cpu_id;
  79. u16 equiv_cpu_id = 0;
  80. unsigned int i = 0;
  81. BUG_ON(equiv_cpu_table == NULL);
  82. current_cpu_id = cpuid_eax(0x00000001);
  83. while (equiv_cpu_table[i].installed_cpu != 0) {
  84. if (current_cpu_id == equiv_cpu_table[i].installed_cpu) {
  85. equiv_cpu_id = equiv_cpu_table[i].equiv_cpu;
  86. break;
  87. }
  88. i++;
  89. }
  90. if (!equiv_cpu_id)
  91. return 0;
  92. if (mc_hdr->processor_rev_id != equiv_cpu_id)
  93. return 0;
  94. /* ucode might be chipset specific -- currently we don't support this */
  95. if (mc_hdr->nb_dev_id || mc_hdr->sb_dev_id) {
  96. pr_err("CPU%d: chipset specific code not yet supported\n",
  97. cpu);
  98. return 0;
  99. }
  100. if (mc_hdr->patch_id <= rev)
  101. return 0;
  102. return 1;
  103. }
  104. static int apply_microcode_amd(int cpu)
  105. {
  106. u32 rev, dummy;
  107. int cpu_num = raw_smp_processor_id();
  108. struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
  109. struct microcode_amd *mc_amd = uci->mc;
  110. /* We should bind the task to the CPU */
  111. BUG_ON(cpu_num != cpu);
  112. if (mc_amd == NULL)
  113. return 0;
  114. wrmsrl(MSR_AMD64_PATCH_LOADER, (u64)(long)&mc_amd->hdr.data_code);
  115. /* get patch id after patching */
  116. rdmsr(MSR_AMD64_PATCH_LEVEL, rev, dummy);
  117. /* check current patch id and patch's id for match */
  118. if (rev != mc_amd->hdr.patch_id) {
  119. pr_err("CPU%d: update failed for patch_level=0x%08x\n",
  120. cpu, mc_amd->hdr.patch_id);
  121. return -1;
  122. }
  123. pr_info("CPU%d: new patch_level=0x%08x\n", cpu, rev);
  124. uci->cpu_sig.rev = rev;
  125. return 0;
  126. }
  127. static unsigned int verify_ucode_size(int cpu, const u8 *buf, unsigned int size)
  128. {
  129. struct cpuinfo_x86 *c = &cpu_data(cpu);
  130. u32 max_size, actual_size;
  131. #define F1XH_MPB_MAX_SIZE 2048
  132. #define F14H_MPB_MAX_SIZE 1824
  133. #define F15H_MPB_MAX_SIZE 4096
  134. switch (c->x86) {
  135. case 0x14:
  136. max_size = F14H_MPB_MAX_SIZE;
  137. break;
  138. case 0x15:
  139. max_size = F15H_MPB_MAX_SIZE;
  140. break;
  141. default:
  142. max_size = F1XH_MPB_MAX_SIZE;
  143. break;
  144. }
  145. actual_size = *(u32 *)(buf + 4);
  146. if (actual_size + SECTION_HDR_SIZE > size || actual_size > max_size) {
  147. pr_err("section size mismatch\n");
  148. return 0;
  149. }
  150. return actual_size;
  151. }
  152. static struct microcode_header_amd *
  153. get_next_ucode(int cpu, const u8 *buf, unsigned int size, unsigned int *mc_size)
  154. {
  155. struct microcode_header_amd *mc = NULL;
  156. unsigned int actual_size = 0;
  157. if (*(u32 *)buf != UCODE_UCODE_TYPE) {
  158. pr_err("invalid type field in container file section header\n");
  159. goto out;
  160. }
  161. actual_size = verify_ucode_size(cpu, buf, size);
  162. if (!actual_size)
  163. goto out;
  164. mc = vzalloc(actual_size);
  165. if (!mc)
  166. goto out;
  167. get_ucode_data(mc, buf + SECTION_HDR_SIZE, actual_size);
  168. *mc_size = actual_size + SECTION_HDR_SIZE;
  169. out:
  170. return mc;
  171. }
  172. static int install_equiv_cpu_table(const u8 *buf)
  173. {
  174. unsigned int *ibuf = (unsigned int *)buf;
  175. unsigned int type = ibuf[1];
  176. unsigned int size = ibuf[2];
  177. if (type != UCODE_EQUIV_CPU_TABLE_TYPE || !size) {
  178. pr_err("empty section/"
  179. "invalid type field in container file section header\n");
  180. return -EINVAL;
  181. }
  182. equiv_cpu_table = vmalloc(size);
  183. if (!equiv_cpu_table) {
  184. pr_err("failed to allocate equivalent CPU table\n");
  185. return -ENOMEM;
  186. }
  187. get_ucode_data(equiv_cpu_table, buf + CONTAINER_HDR_SZ, size);
  188. /* add header length */
  189. return size + CONTAINER_HDR_SZ;
  190. }
  191. static void free_equiv_cpu_table(void)
  192. {
  193. vfree(equiv_cpu_table);
  194. equiv_cpu_table = NULL;
  195. }
  196. static enum ucode_state
  197. generic_load_microcode(int cpu, const u8 *data, size_t size)
  198. {
  199. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  200. struct microcode_header_amd *mc_hdr = NULL;
  201. unsigned int mc_size, leftover;
  202. int offset;
  203. const u8 *ucode_ptr = data;
  204. void *new_mc = NULL;
  205. unsigned int new_rev = uci->cpu_sig.rev;
  206. enum ucode_state state = UCODE_OK;
  207. offset = install_equiv_cpu_table(ucode_ptr);
  208. if (offset < 0) {
  209. pr_err("failed to create equivalent cpu table\n");
  210. return UCODE_ERROR;
  211. }
  212. ucode_ptr += offset;
  213. leftover = size - offset;
  214. while (leftover) {
  215. mc_hdr = get_next_ucode(cpu, ucode_ptr, leftover, &mc_size);
  216. if (!mc_hdr)
  217. break;
  218. if (get_matching_microcode(cpu, mc_hdr, new_rev)) {
  219. vfree(new_mc);
  220. new_rev = mc_hdr->patch_id;
  221. new_mc = mc_hdr;
  222. } else
  223. vfree(mc_hdr);
  224. ucode_ptr += mc_size;
  225. leftover -= mc_size;
  226. }
  227. if (!new_mc) {
  228. state = UCODE_NFOUND;
  229. goto free_table;
  230. }
  231. if (!leftover) {
  232. vfree(uci->mc);
  233. uci->mc = new_mc;
  234. pr_debug("CPU%d update ucode (0x%08x -> 0x%08x)\n",
  235. cpu, uci->cpu_sig.rev, new_rev);
  236. } else {
  237. vfree(new_mc);
  238. state = UCODE_ERROR;
  239. }
  240. free_table:
  241. free_equiv_cpu_table();
  242. return state;
  243. }
  244. static enum ucode_state request_microcode_amd(int cpu, struct device *device)
  245. {
  246. const char *fw_name = "amd-ucode/microcode_amd.bin";
  247. const struct firmware *fw;
  248. enum ucode_state ret = UCODE_NFOUND;
  249. if (request_firmware(&fw, fw_name, device)) {
  250. pr_err("failed to load file %s\n", fw_name);
  251. goto out;
  252. }
  253. ret = UCODE_ERROR;
  254. if (*(u32 *)fw->data != UCODE_MAGIC) {
  255. pr_err("invalid magic value (0x%08x)\n", *(u32 *)fw->data);
  256. goto fw_release;
  257. }
  258. ret = generic_load_microcode(cpu, fw->data, fw->size);
  259. fw_release:
  260. release_firmware(fw);
  261. out:
  262. return ret;
  263. }
  264. static enum ucode_state
  265. request_microcode_user(int cpu, const void __user *buf, size_t size)
  266. {
  267. pr_info("AMD microcode update via /dev/cpu/microcode not supported\n");
  268. return UCODE_ERROR;
  269. }
  270. static void microcode_fini_cpu_amd(int cpu)
  271. {
  272. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  273. vfree(uci->mc);
  274. uci->mc = NULL;
  275. }
  276. static struct microcode_ops microcode_amd_ops = {
  277. .request_microcode_user = request_microcode_user,
  278. .request_microcode_fw = request_microcode_amd,
  279. .collect_cpu_info = collect_cpu_info_amd,
  280. .apply_microcode = apply_microcode_amd,
  281. .microcode_fini_cpu = microcode_fini_cpu_amd,
  282. };
  283. struct microcode_ops * __init init_amd_microcode(void)
  284. {
  285. return &microcode_amd_ops;
  286. }