microcode_amd.c 8.0 KB

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