microcode_amd.c 9.0 KB

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