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