microcode_amd.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. const struct firmware *firmware;
  34. static int supported_cpu;
  35. struct equiv_cpu_entry {
  36. u32 installed_cpu;
  37. u32 fixed_errata_mask;
  38. u32 fixed_errata_compare;
  39. u16 equiv_cpu;
  40. u16 res;
  41. } __attribute__((packed));
  42. struct microcode_header_amd {
  43. u32 data_code;
  44. u32 patch_id;
  45. u16 mc_patch_data_id;
  46. u8 mc_patch_data_len;
  47. u8 init_flag;
  48. u32 mc_patch_data_checksum;
  49. u32 nb_dev_id;
  50. u32 sb_dev_id;
  51. u16 processor_rev_id;
  52. u8 nb_rev_id;
  53. u8 sb_rev_id;
  54. u8 bios_api_rev;
  55. u8 reserved1[3];
  56. u32 match_reg[8];
  57. } __attribute__((packed));
  58. struct microcode_amd {
  59. struct microcode_header_amd hdr;
  60. unsigned int mpb[0];
  61. };
  62. #define UCODE_MAX_SIZE 2048
  63. #define UCODE_CONTAINER_SECTION_HDR 8
  64. #define UCODE_CONTAINER_HEADER_SIZE 12
  65. static struct equiv_cpu_entry *equiv_cpu_table;
  66. static int collect_cpu_info_amd(int cpu, struct cpu_signature *csig)
  67. {
  68. u32 dummy;
  69. if (!supported_cpu)
  70. return -1;
  71. memset(csig, 0, sizeof(*csig));
  72. rdmsr(MSR_AMD64_PATCH_LEVEL, csig->rev, dummy);
  73. pr_info("CPU%d: patch_level=0x%x\n", cpu, csig->rev);
  74. return 0;
  75. }
  76. static int get_matching_microcode(int cpu, void *mc, int rev)
  77. {
  78. struct microcode_header_amd *mc_header = mc;
  79. unsigned int current_cpu_id;
  80. u16 equiv_cpu_id = 0;
  81. unsigned int i = 0;
  82. BUG_ON(equiv_cpu_table == NULL);
  83. current_cpu_id = cpuid_eax(0x00000001);
  84. while (equiv_cpu_table[i].installed_cpu != 0) {
  85. if (current_cpu_id == equiv_cpu_table[i].installed_cpu) {
  86. equiv_cpu_id = equiv_cpu_table[i].equiv_cpu;
  87. break;
  88. }
  89. i++;
  90. }
  91. if (!equiv_cpu_id)
  92. return 0;
  93. if (mc_header->processor_rev_id != equiv_cpu_id)
  94. return 0;
  95. /* ucode might be chipset specific -- currently we don't support this */
  96. if (mc_header->nb_dev_id || mc_header->sb_dev_id) {
  97. pr_err("CPU%d: loading of chipset specific code not yet supported\n",
  98. cpu);
  99. return 0;
  100. }
  101. if (mc_header->patch_id <= rev)
  102. return 0;
  103. return 1;
  104. }
  105. static int apply_microcode_amd(int cpu)
  106. {
  107. u32 rev, dummy;
  108. int cpu_num = raw_smp_processor_id();
  109. struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
  110. struct microcode_amd *mc_amd = uci->mc;
  111. /* We should bind the task to the CPU */
  112. BUG_ON(cpu_num != cpu);
  113. if (mc_amd == NULL)
  114. return 0;
  115. wrmsrl(MSR_AMD64_PATCH_LOADER, (u64)(long)&mc_amd->hdr.data_code);
  116. /* get patch id after patching */
  117. rdmsr(MSR_AMD64_PATCH_LEVEL, rev, dummy);
  118. /* check current patch id and patch's id for match */
  119. if (rev != mc_amd->hdr.patch_id) {
  120. pr_err("CPU%d: update failed (for patch_level=0x%x)\n",
  121. cpu, mc_amd->hdr.patch_id);
  122. return -1;
  123. }
  124. pr_info("CPU%d: updated (new patch_level=0x%x)\n", cpu, rev);
  125. uci->cpu_sig.rev = rev;
  126. return 0;
  127. }
  128. static int get_ucode_data(void *to, const u8 *from, size_t n)
  129. {
  130. memcpy(to, from, n);
  131. return 0;
  132. }
  133. static void *
  134. get_next_ucode(const u8 *buf, unsigned int size, unsigned int *mc_size)
  135. {
  136. unsigned int total_size;
  137. u8 section_hdr[UCODE_CONTAINER_SECTION_HDR];
  138. void *mc;
  139. if (get_ucode_data(section_hdr, buf, UCODE_CONTAINER_SECTION_HDR))
  140. return NULL;
  141. if (section_hdr[0] != UCODE_UCODE_TYPE) {
  142. pr_err("error: invalid type field in container file section header\n");
  143. return NULL;
  144. }
  145. total_size = (unsigned long) (section_hdr[4] + (section_hdr[5] << 8));
  146. if (total_size > size || total_size > UCODE_MAX_SIZE) {
  147. pr_err("error: size mismatch\n");
  148. return NULL;
  149. }
  150. mc = vmalloc(UCODE_MAX_SIZE);
  151. if (mc) {
  152. memset(mc, 0, UCODE_MAX_SIZE);
  153. if (get_ucode_data(mc, buf + UCODE_CONTAINER_SECTION_HDR,
  154. total_size)) {
  155. vfree(mc);
  156. mc = NULL;
  157. } else
  158. *mc_size = total_size + UCODE_CONTAINER_SECTION_HDR;
  159. }
  160. return mc;
  161. }
  162. static int install_equiv_cpu_table(const u8 *buf)
  163. {
  164. u8 *container_hdr[UCODE_CONTAINER_HEADER_SIZE];
  165. unsigned int *buf_pos = (unsigned int *)container_hdr;
  166. unsigned long size;
  167. if (get_ucode_data(&container_hdr, buf, UCODE_CONTAINER_HEADER_SIZE))
  168. return 0;
  169. size = buf_pos[2];
  170. if (buf_pos[1] != UCODE_EQUIV_CPU_TABLE_TYPE || !size) {
  171. pr_err("error: invalid type field in container file section header\n");
  172. return 0;
  173. }
  174. equiv_cpu_table = (struct equiv_cpu_entry *) vmalloc(size);
  175. if (!equiv_cpu_table) {
  176. pr_err("failed to allocate equivalent CPU table\n");
  177. return 0;
  178. }
  179. buf += UCODE_CONTAINER_HEADER_SIZE;
  180. if (get_ucode_data(equiv_cpu_table, buf, size)) {
  181. vfree(equiv_cpu_table);
  182. return 0;
  183. }
  184. return size + UCODE_CONTAINER_HEADER_SIZE; /* add header length */
  185. }
  186. static void free_equiv_cpu_table(void)
  187. {
  188. vfree(equiv_cpu_table);
  189. equiv_cpu_table = NULL;
  190. }
  191. static enum ucode_state
  192. generic_load_microcode(int cpu, const u8 *data, size_t size)
  193. {
  194. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  195. const u8 *ucode_ptr = data;
  196. void *new_mc = NULL;
  197. void *mc;
  198. int new_rev = uci->cpu_sig.rev;
  199. unsigned int leftover;
  200. unsigned long offset;
  201. enum ucode_state state = UCODE_OK;
  202. offset = install_equiv_cpu_table(ucode_ptr);
  203. if (!offset) {
  204. pr_err("failed to create equivalent cpu table\n");
  205. return UCODE_ERROR;
  206. }
  207. ucode_ptr += offset;
  208. leftover = size - offset;
  209. while (leftover) {
  210. unsigned int uninitialized_var(mc_size);
  211. struct microcode_header_amd *mc_header;
  212. mc = get_next_ucode(ucode_ptr, leftover, &mc_size);
  213. if (!mc)
  214. break;
  215. mc_header = (struct microcode_header_amd *)mc;
  216. if (get_matching_microcode(cpu, mc, new_rev)) {
  217. vfree(new_mc);
  218. new_rev = mc_header->patch_id;
  219. new_mc = mc;
  220. } else
  221. vfree(mc);
  222. ucode_ptr += mc_size;
  223. leftover -= mc_size;
  224. }
  225. if (new_mc) {
  226. if (!leftover) {
  227. vfree(uci->mc);
  228. uci->mc = new_mc;
  229. pr_debug("CPU%d found a matching microcode update with version 0x%x (current=0x%x)\n",
  230. cpu, new_rev, uci->cpu_sig.rev);
  231. } else {
  232. vfree(new_mc);
  233. state = UCODE_ERROR;
  234. }
  235. } else
  236. state = UCODE_NFOUND;
  237. free_equiv_cpu_table();
  238. return state;
  239. }
  240. static enum ucode_state request_microcode_fw(int cpu, struct device *device)
  241. {
  242. enum ucode_state ret;
  243. if (firmware == NULL)
  244. return UCODE_NFOUND;
  245. if (*(u32 *)firmware->data != UCODE_MAGIC) {
  246. pr_err("invalid UCODE_MAGIC (0x%08x)\n",
  247. *(u32 *)firmware->data);
  248. return UCODE_ERROR;
  249. }
  250. ret = generic_load_microcode(cpu, firmware->data, firmware->size);
  251. return ret;
  252. }
  253. static enum ucode_state
  254. request_microcode_user(int cpu, const void __user *buf, size_t size)
  255. {
  256. pr_info("AMD microcode update via /dev/cpu/microcode not supported\n");
  257. return UCODE_ERROR;
  258. }
  259. static void microcode_fini_cpu_amd(int cpu)
  260. {
  261. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  262. vfree(uci->mc);
  263. uci->mc = NULL;
  264. }
  265. void init_microcode_amd(struct device *device)
  266. {
  267. const char *fw_name = "amd-ucode/microcode_amd.bin";
  268. struct cpuinfo_x86 *c = &boot_cpu_data;
  269. WARN_ON(c->x86_vendor != X86_VENDOR_AMD);
  270. if (c->x86 < 0x10) {
  271. pr_warning("AMD CPU family 0x%x not supported\n", c->x86);
  272. return;
  273. }
  274. supported_cpu = 1;
  275. if (request_firmware(&firmware, fw_name, device))
  276. pr_err("failed to load file %s\n", fw_name);
  277. }
  278. void fini_microcode_amd(void)
  279. {
  280. release_firmware(firmware);
  281. }
  282. static struct microcode_ops microcode_amd_ops = {
  283. .init = init_microcode_amd,
  284. .fini = fini_microcode_amd,
  285. .request_microcode_user = request_microcode_user,
  286. .request_microcode_fw = request_microcode_fw,
  287. .collect_cpu_info = collect_cpu_info_amd,
  288. .apply_microcode = apply_microcode_amd,
  289. .microcode_fini_cpu = microcode_fini_cpu_amd,
  290. };
  291. struct microcode_ops * __init init_amd_microcode(void)
  292. {
  293. return &microcode_amd_ops;
  294. }