microcode_amd.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. unsigned int installed_cpu;
  46. unsigned int fixed_errata_mask;
  47. unsigned int fixed_errata_compare;
  48. unsigned int equiv_cpu;
  49. };
  50. struct microcode_header_amd {
  51. unsigned int data_code;
  52. unsigned int patch_id;
  53. unsigned char mc_patch_data_id[2];
  54. unsigned char mc_patch_data_len;
  55. unsigned char init_flag;
  56. unsigned int mc_patch_data_checksum;
  57. unsigned int nb_dev_id;
  58. unsigned int sb_dev_id;
  59. u16 processor_rev_id;
  60. unsigned char nb_rev_id;
  61. unsigned char sb_rev_id;
  62. unsigned char bios_api_rev;
  63. unsigned char reserved1[3];
  64. unsigned int match_reg[8];
  65. };
  66. struct microcode_amd {
  67. struct microcode_header_amd hdr;
  68. unsigned int mpb[0];
  69. };
  70. #define UCODE_MAX_SIZE (2048)
  71. #define DEFAULT_UCODE_DATASIZE (896)
  72. #define MC_HEADER_SIZE (sizeof(struct microcode_header_amd))
  73. #define DEFAULT_UCODE_TOTALSIZE (DEFAULT_UCODE_DATASIZE + MC_HEADER_SIZE)
  74. #define DWSIZE (sizeof(u32))
  75. /* For now we support a fixed ucode total size only */
  76. #define get_totalsize(mc) \
  77. ((((struct microcode_amd *)mc)->hdr.mc_patch_data_len * 28) \
  78. + MC_HEADER_SIZE)
  79. /* serialize access to the physical write */
  80. static DEFINE_SPINLOCK(microcode_update_lock);
  81. static struct equiv_cpu_entry *equiv_cpu_table;
  82. static int collect_cpu_info_amd(int cpu, struct cpu_signature *csig)
  83. {
  84. struct cpuinfo_x86 *c = &cpu_data(cpu);
  85. memset(csig, 0, sizeof(*csig));
  86. if (c->x86_vendor != X86_VENDOR_AMD || c->x86 < 0x10) {
  87. printk(KERN_ERR "microcode: CPU%d not a capable AMD processor\n",
  88. cpu);
  89. return -1;
  90. }
  91. asm volatile("movl %1, %%ecx; rdmsr"
  92. : "=a" (csig->rev)
  93. : "i" (0x0000008B) : "ecx");
  94. printk(KERN_INFO "microcode: collect_cpu_info_amd : patch_id=0x%x\n",
  95. csig->rev);
  96. return 0;
  97. }
  98. static int get_matching_microcode(int cpu, void *mc, int rev)
  99. {
  100. struct microcode_header_amd *mc_header = mc;
  101. struct pci_dev *nb_pci_dev, *sb_pci_dev;
  102. unsigned int current_cpu_id;
  103. unsigned int equiv_cpu_id = 0x00;
  104. unsigned int i = 0;
  105. BUG_ON(equiv_cpu_table == NULL);
  106. current_cpu_id = cpuid_eax(0x00000001);
  107. while (equiv_cpu_table[i].installed_cpu != 0) {
  108. if (current_cpu_id == equiv_cpu_table[i].installed_cpu) {
  109. equiv_cpu_id = equiv_cpu_table[i].equiv_cpu & 0xffff;
  110. break;
  111. }
  112. i++;
  113. }
  114. if (!equiv_cpu_id) {
  115. printk(KERN_ERR "microcode: CPU%d cpu_id "
  116. "not found in equivalent cpu table\n", cpu);
  117. return 0;
  118. }
  119. if (mc_header->processor_rev_id != equiv_cpu_id) {
  120. printk(KERN_ERR "microcode: CPU%d patch does not match "
  121. "(processor_rev_id: %x, eqiv_cpu_id: %x)\n",
  122. cpu, mc_header->processor_rev_id, equiv_cpu_id);
  123. return 0;
  124. }
  125. /* ucode may be northbridge specific */
  126. if (mc_header->nb_dev_id) {
  127. nb_pci_dev = pci_get_device(PCI_VENDOR_ID_AMD,
  128. (mc_header->nb_dev_id & 0xff),
  129. NULL);
  130. if ((!nb_pci_dev) ||
  131. (mc_header->nb_rev_id != nb_pci_dev->revision)) {
  132. printk(KERN_ERR "microcode: CPU%d NB mismatch\n", cpu);
  133. pci_dev_put(nb_pci_dev);
  134. return 0;
  135. }
  136. pci_dev_put(nb_pci_dev);
  137. }
  138. /* ucode may be southbridge specific */
  139. if (mc_header->sb_dev_id) {
  140. sb_pci_dev = pci_get_device(PCI_VENDOR_ID_AMD,
  141. (mc_header->sb_dev_id & 0xff),
  142. NULL);
  143. if ((!sb_pci_dev) ||
  144. (mc_header->sb_rev_id != sb_pci_dev->revision)) {
  145. printk(KERN_ERR "microcode: CPU%d SB mismatch\n", cpu);
  146. pci_dev_put(sb_pci_dev);
  147. return 0;
  148. }
  149. pci_dev_put(sb_pci_dev);
  150. }
  151. if (mc_header->patch_id <= rev)
  152. return 0;
  153. return 1;
  154. }
  155. static void apply_microcode_amd(int cpu)
  156. {
  157. unsigned long flags;
  158. unsigned int eax, edx;
  159. unsigned int rev;
  160. int cpu_num = raw_smp_processor_id();
  161. struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
  162. struct microcode_amd *mc_amd = uci->mc;
  163. unsigned long addr;
  164. /* We should bind the task to the CPU */
  165. BUG_ON(cpu_num != cpu);
  166. if (mc_amd == NULL)
  167. return;
  168. spin_lock_irqsave(&microcode_update_lock, flags);
  169. addr = (unsigned long)&mc_amd->hdr.data_code;
  170. edx = (unsigned int)(((unsigned long)upper_32_bits(addr)));
  171. eax = (unsigned int)(((unsigned long)lower_32_bits(addr)));
  172. asm volatile("movl %0, %%ecx; wrmsr" :
  173. : "i" (0xc0010020), "a" (eax), "d" (edx) : "ecx");
  174. /* get patch id after patching */
  175. asm volatile("movl %1, %%ecx; rdmsr"
  176. : "=a" (rev)
  177. : "i" (0x0000008B) : "ecx");
  178. spin_unlock_irqrestore(&microcode_update_lock, flags);
  179. /* check current patch id and patch's id for match */
  180. if (rev != mc_amd->hdr.patch_id) {
  181. printk(KERN_ERR "microcode: CPU%d update from revision "
  182. "0x%x to 0x%x failed\n", cpu_num,
  183. mc_amd->hdr.patch_id, rev);
  184. return;
  185. }
  186. printk(KERN_INFO "microcode: CPU%d updated from revision "
  187. "0x%x to 0x%x\n",
  188. cpu_num, uci->cpu_sig.rev, mc_amd->hdr.patch_id);
  189. uci->cpu_sig.rev = rev;
  190. }
  191. static int get_ucode_data(void *to, const u8 *from, size_t n)
  192. {
  193. memcpy(to, from, n);
  194. return 0;
  195. }
  196. static void *get_next_ucode(const u8 *buf, unsigned int size,
  197. unsigned int *mc_size)
  198. {
  199. unsigned int total_size;
  200. #define UCODE_CONTAINER_SECTION_HDR 8
  201. u8 section_hdr[UCODE_CONTAINER_SECTION_HDR];
  202. void *mc;
  203. if (get_ucode_data(section_hdr, buf, UCODE_CONTAINER_SECTION_HDR))
  204. return NULL;
  205. if (section_hdr[0] != UCODE_UCODE_TYPE) {
  206. printk(KERN_ERR "microcode: error! "
  207. "Wrong microcode payload type field\n");
  208. return NULL;
  209. }
  210. total_size = (unsigned long) (section_hdr[4] + (section_hdr[5] << 8));
  211. printk(KERN_INFO "microcode: size %u, total_size %u\n",
  212. size, total_size);
  213. if (total_size > size || total_size > UCODE_MAX_SIZE) {
  214. printk(KERN_ERR "microcode: error! Bad data in microcode data file\n");
  215. return NULL;
  216. }
  217. mc = vmalloc(UCODE_MAX_SIZE);
  218. if (mc) {
  219. memset(mc, 0, UCODE_MAX_SIZE);
  220. if (get_ucode_data(mc, buf + UCODE_CONTAINER_SECTION_HDR,
  221. total_size)) {
  222. vfree(mc);
  223. mc = NULL;
  224. } else
  225. *mc_size = total_size + UCODE_CONTAINER_SECTION_HDR;
  226. }
  227. #undef UCODE_CONTAINER_SECTION_HDR
  228. return mc;
  229. }
  230. static int install_equiv_cpu_table(const u8 *buf)
  231. {
  232. #define UCODE_CONTAINER_HEADER_SIZE 12
  233. u8 *container_hdr[UCODE_CONTAINER_HEADER_SIZE];
  234. unsigned int *buf_pos = (unsigned int *)container_hdr;
  235. unsigned long size;
  236. if (get_ucode_data(&container_hdr, buf, UCODE_CONTAINER_HEADER_SIZE))
  237. return 0;
  238. size = buf_pos[2];
  239. if (buf_pos[1] != UCODE_EQUIV_CPU_TABLE_TYPE || !size) {
  240. printk(KERN_ERR "microcode: error! "
  241. "Wrong microcode equivalent cpu table\n");
  242. return 0;
  243. }
  244. equiv_cpu_table = (struct equiv_cpu_entry *) vmalloc(size);
  245. if (!equiv_cpu_table) {
  246. printk(KERN_ERR "microcode: error, can't allocate memory for equiv CPU table\n");
  247. return 0;
  248. }
  249. buf += UCODE_CONTAINER_HEADER_SIZE;
  250. if (get_ucode_data(equiv_cpu_table, buf, size)) {
  251. vfree(equiv_cpu_table);
  252. return 0;
  253. }
  254. return size + UCODE_CONTAINER_HEADER_SIZE; /* add header length */
  255. #undef UCODE_CONTAINER_HEADER_SIZE
  256. }
  257. static void free_equiv_cpu_table(void)
  258. {
  259. if (equiv_cpu_table) {
  260. vfree(equiv_cpu_table);
  261. equiv_cpu_table = NULL;
  262. }
  263. }
  264. static int generic_load_microcode(int cpu, const u8 *data, size_t size)
  265. {
  266. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  267. const u8 *ucode_ptr = data;
  268. void *new_mc = NULL;
  269. void *mc;
  270. int new_rev = uci->cpu_sig.rev;
  271. unsigned int leftover;
  272. unsigned long offset;
  273. offset = install_equiv_cpu_table(ucode_ptr);
  274. if (!offset) {
  275. printk(KERN_ERR "microcode: installing equivalent cpu table failed\n");
  276. return -EINVAL;
  277. }
  278. ucode_ptr += offset;
  279. leftover = size - offset;
  280. while (leftover) {
  281. unsigned int uninitialized_var(mc_size);
  282. struct microcode_header_amd *mc_header;
  283. mc = get_next_ucode(ucode_ptr, leftover, &mc_size);
  284. if (!mc)
  285. break;
  286. mc_header = (struct microcode_header_amd *)mc;
  287. if (get_matching_microcode(cpu, mc, new_rev)) {
  288. if (new_mc)
  289. vfree(new_mc);
  290. new_rev = mc_header->patch_id;
  291. new_mc = mc;
  292. } else
  293. vfree(mc);
  294. ucode_ptr += mc_size;
  295. leftover -= mc_size;
  296. }
  297. if (new_mc) {
  298. if (!leftover) {
  299. if (uci->mc)
  300. vfree(uci->mc);
  301. uci->mc = new_mc;
  302. pr_debug("microcode: CPU%d found a matching microcode "
  303. "update with version 0x%x (current=0x%x)\n",
  304. cpu, new_rev, uci->cpu_sig.rev);
  305. } else
  306. vfree(new_mc);
  307. }
  308. free_equiv_cpu_table();
  309. return (int)leftover;
  310. }
  311. static int request_microcode_fw(int cpu, struct device *device)
  312. {
  313. const char *fw_name = "amd-ucode/microcode_amd.bin";
  314. const struct firmware *firmware;
  315. int ret;
  316. /* We should bind the task to the CPU */
  317. BUG_ON(cpu != raw_smp_processor_id());
  318. ret = request_firmware(&firmware, fw_name, device);
  319. if (ret) {
  320. printk(KERN_ERR "microcode: ucode data file %s load failed\n",
  321. fw_name);
  322. return ret;
  323. }
  324. ret = generic_load_microcode(cpu, firmware->data, firmware->size);
  325. release_firmware(firmware);
  326. return ret;
  327. }
  328. static int request_microcode_user(int cpu, const void __user *buf, size_t size)
  329. {
  330. printk(KERN_WARNING "microcode: AMD microcode update via "
  331. "/dev/cpu/microcode is not supported\n");
  332. return -1;
  333. }
  334. static void microcode_fini_cpu_amd(int cpu)
  335. {
  336. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  337. vfree(uci->mc);
  338. uci->mc = NULL;
  339. }
  340. static struct microcode_ops microcode_amd_ops = {
  341. .request_microcode_user = request_microcode_user,
  342. .request_microcode_fw = request_microcode_fw,
  343. .collect_cpu_info = collect_cpu_info_amd,
  344. .apply_microcode = apply_microcode_amd,
  345. .microcode_fini_cpu = microcode_fini_cpu_amd,
  346. };
  347. struct microcode_ops * __init init_amd_microcode(void)
  348. {
  349. return &microcode_amd_ops;
  350. }