microcode_amd.c 9.0 KB

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