microcode_amd.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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 unter 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 <asm/msr.h>
  35. #include <asm/uaccess.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. unsigned char processor_rev_id[2];
  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;
  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[0]) != (equiv_cpu_id & 0xff)) {
  120. printk(KERN_ERR
  121. "microcode: CPU%d patch does not match "
  122. "(patch is %x, cpu extended is %x) \n",
  123. cpu, mc_header->processor_rev_id[0],
  124. (equiv_cpu_id & 0xff));
  125. return 0;
  126. }
  127. if ((mc_header->processor_rev_id[1]) != ((equiv_cpu_id >> 16) & 0xff)) {
  128. printk(KERN_ERR "microcode: CPU%d patch does not match "
  129. "(patch is %x, cpu base id is %x) \n",
  130. cpu, mc_header->processor_rev_id[1],
  131. ((equiv_cpu_id >> 16) & 0xff));
  132. return 0;
  133. }
  134. /* ucode may be northbridge specific */
  135. if (mc_header->nb_dev_id) {
  136. nb_pci_dev = pci_get_device(PCI_VENDOR_ID_AMD,
  137. (mc_header->nb_dev_id & 0xff),
  138. NULL);
  139. if ((!nb_pci_dev) ||
  140. (mc_header->nb_rev_id != nb_pci_dev->revision)) {
  141. printk(KERN_ERR "microcode: CPU%d NB mismatch \n", cpu);
  142. pci_dev_put(nb_pci_dev);
  143. return 0;
  144. }
  145. pci_dev_put(nb_pci_dev);
  146. }
  147. /* ucode may be southbridge specific */
  148. if (mc_header->sb_dev_id) {
  149. sb_pci_dev = pci_get_device(PCI_VENDOR_ID_AMD,
  150. (mc_header->sb_dev_id & 0xff),
  151. NULL);
  152. if ((!sb_pci_dev) ||
  153. (mc_header->sb_rev_id != sb_pci_dev->revision)) {
  154. printk(KERN_ERR "microcode: CPU%d SB mismatch \n", cpu);
  155. pci_dev_put(sb_pci_dev);
  156. return 0;
  157. }
  158. pci_dev_put(sb_pci_dev);
  159. }
  160. if (mc_header->patch_id <= rev)
  161. return 0;
  162. return 1;
  163. }
  164. static void apply_microcode_amd(int cpu)
  165. {
  166. unsigned long flags;
  167. unsigned int eax, edx;
  168. unsigned int rev;
  169. int cpu_num = raw_smp_processor_id();
  170. struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
  171. struct microcode_amd *mc_amd = uci->mc;
  172. unsigned long addr;
  173. /* We should bind the task to the CPU */
  174. BUG_ON(cpu_num != cpu);
  175. if (mc_amd == NULL)
  176. return;
  177. spin_lock_irqsave(&microcode_update_lock, flags);
  178. addr = (unsigned long)&mc_amd->hdr.data_code;
  179. edx = (unsigned int)(((unsigned long)upper_32_bits(addr)));
  180. eax = (unsigned int)(((unsigned long)lower_32_bits(addr)));
  181. asm volatile("movl %0, %%ecx; wrmsr" :
  182. : "i" (0xc0010020), "a" (eax), "d" (edx) : "ecx");
  183. /* get patch id after patching */
  184. asm volatile("movl %1, %%ecx; rdmsr"
  185. : "=a" (rev)
  186. : "i" (0x0000008B) : "ecx");
  187. spin_unlock_irqrestore(&microcode_update_lock, flags);
  188. /* check current patch id and patch's id for match */
  189. if (rev != mc_amd->hdr.patch_id) {
  190. printk(KERN_ERR "microcode: CPU%d update from revision "
  191. "0x%x to 0x%x failed\n", cpu_num,
  192. mc_amd->hdr.patch_id, rev);
  193. return;
  194. }
  195. printk(KERN_INFO "microcode: CPU%d updated from revision "
  196. "0x%x to 0x%x \n",
  197. cpu_num, uci->cpu_sig.rev, mc_amd->hdr.patch_id);
  198. uci->cpu_sig.rev = rev;
  199. }
  200. static void * get_next_ucode(u8 *buf, unsigned int size,
  201. int (*get_ucode_data)(void *, const void *, size_t),
  202. unsigned int *mc_size)
  203. {
  204. unsigned int total_size;
  205. #define UCODE_CONTAINER_SECTION_HDR 8
  206. u8 section_hdr[UCODE_CONTAINER_SECTION_HDR];
  207. void *mc;
  208. if (get_ucode_data(section_hdr, buf, UCODE_CONTAINER_SECTION_HDR))
  209. return NULL;
  210. if (section_hdr[0] != UCODE_UCODE_TYPE) {
  211. printk(KERN_ERR "microcode: error! "
  212. "Wrong microcode payload type field\n");
  213. return NULL;
  214. }
  215. total_size = (unsigned long) (section_hdr[4] + (section_hdr[5] << 8));
  216. printk(KERN_INFO "microcode: size %u, total_size %u\n",
  217. size, total_size);
  218. if (total_size > size || total_size > UCODE_MAX_SIZE) {
  219. printk(KERN_ERR "microcode: error! Bad data in microcode data file\n");
  220. return NULL;
  221. }
  222. mc = vmalloc(UCODE_MAX_SIZE);
  223. if (mc) {
  224. memset(mc, 0, UCODE_MAX_SIZE);
  225. if (get_ucode_data(mc, buf + UCODE_CONTAINER_SECTION_HDR, total_size)) {
  226. vfree(mc);
  227. mc = NULL;
  228. } else
  229. *mc_size = total_size + UCODE_CONTAINER_SECTION_HDR;
  230. }
  231. #undef UCODE_CONTAINER_SECTION_HDR
  232. return mc;
  233. }
  234. static int install_equiv_cpu_table(u8 *buf,
  235. int (*get_ucode_data)(void *, const void *, size_t))
  236. {
  237. #define UCODE_CONTAINER_HEADER_SIZE 12
  238. u8 *container_hdr[UCODE_CONTAINER_HEADER_SIZE];
  239. unsigned int *buf_pos = (unsigned int *)container_hdr;
  240. unsigned long size;
  241. if (get_ucode_data(&container_hdr, buf, UCODE_CONTAINER_HEADER_SIZE))
  242. return 0;
  243. size = buf_pos[2];
  244. if (buf_pos[1] != UCODE_EQUIV_CPU_TABLE_TYPE || !size) {
  245. printk(KERN_ERR "microcode: error! "
  246. "Wrong microcode equivalnet cpu table\n");
  247. return 0;
  248. }
  249. equiv_cpu_table = (struct equiv_cpu_entry *) vmalloc(size);
  250. if (!equiv_cpu_table) {
  251. printk(KERN_ERR "microcode: error, can't allocate memory for equiv CPU table\n");
  252. return 0;
  253. }
  254. buf += UCODE_CONTAINER_HEADER_SIZE;
  255. if (get_ucode_data(equiv_cpu_table, buf, size)) {
  256. vfree(equiv_cpu_table);
  257. return 0;
  258. }
  259. return size + UCODE_CONTAINER_HEADER_SIZE; /* add header length */
  260. #undef UCODE_CONTAINER_HEADER_SIZE
  261. }
  262. static void free_equiv_cpu_table(void)
  263. {
  264. if (equiv_cpu_table) {
  265. vfree(equiv_cpu_table);
  266. equiv_cpu_table = NULL;
  267. }
  268. }
  269. static int generic_load_microcode(int cpu, void *data, size_t size,
  270. int (*get_ucode_data)(void *, const void *, size_t))
  271. {
  272. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  273. u8 *ucode_ptr = data, *new_mc = NULL, *mc;
  274. int new_rev = uci->cpu_sig.rev;
  275. unsigned int leftover;
  276. unsigned long offset;
  277. offset = install_equiv_cpu_table(ucode_ptr, get_ucode_data);
  278. if (!offset) {
  279. printk(KERN_ERR "microcode: installing equivalent cpu table failed\n");
  280. return -EINVAL;
  281. }
  282. ucode_ptr += offset;
  283. leftover = size - offset;
  284. while (leftover) {
  285. unsigned int uninitialized_var(mc_size);
  286. struct microcode_header_amd *mc_header;
  287. mc = get_next_ucode(ucode_ptr, leftover, get_ucode_data, &mc_size);
  288. if (!mc)
  289. break;
  290. mc_header = (struct microcode_header_amd *)mc;
  291. if (get_matching_microcode(cpu, mc, new_rev)) {
  292. if (new_mc)
  293. vfree(new_mc);
  294. new_rev = mc_header->patch_id;
  295. new_mc = mc;
  296. } else
  297. vfree(mc);
  298. ucode_ptr += mc_size;
  299. leftover -= mc_size;
  300. }
  301. if (new_mc) {
  302. if (!leftover) {
  303. if (uci->mc)
  304. vfree(uci->mc);
  305. uci->mc = new_mc;
  306. pr_debug("microcode: CPU%d found a matching microcode update with"
  307. " version 0x%x (current=0x%x)\n",
  308. cpu, new_rev, uci->cpu_sig.rev);
  309. } else
  310. vfree(new_mc);
  311. }
  312. free_equiv_cpu_table();
  313. return (int)leftover;
  314. }
  315. static int get_ucode_fw(void *to, const void *from, size_t n)
  316. {
  317. memcpy(to, from, n);
  318. return 0;
  319. }
  320. static int request_microcode_fw(int cpu, struct device *device)
  321. {
  322. const char *fw_name = "amd-ucode/microcode_amd.bin";
  323. const struct firmware *firmware;
  324. int ret;
  325. /* We should bind the task to the CPU */
  326. BUG_ON(cpu != raw_smp_processor_id());
  327. ret = request_firmware(&firmware, fw_name, device);
  328. if (ret) {
  329. printk(KERN_ERR "microcode: ucode data file %s load failed\n", fw_name);
  330. return ret;
  331. }
  332. ret = generic_load_microcode(cpu, (void*)firmware->data, firmware->size,
  333. &get_ucode_fw);
  334. release_firmware(firmware);
  335. return ret;
  336. }
  337. static int request_microcode_user(int cpu, const void __user *buf, size_t size)
  338. {
  339. printk(KERN_WARNING "microcode: AMD microcode update via /dev/cpu/microcode"
  340. "is not supported\n");
  341. return -1;
  342. }
  343. static void microcode_fini_cpu_amd(int cpu)
  344. {
  345. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  346. vfree(uci->mc);
  347. uci->mc = NULL;
  348. }
  349. static struct microcode_ops microcode_amd_ops = {
  350. .request_microcode_user = request_microcode_user,
  351. .request_microcode_fw = request_microcode_fw,
  352. .collect_cpu_info = collect_cpu_info_amd,
  353. .apply_microcode = apply_microcode_amd,
  354. .microcode_fini_cpu = microcode_fini_cpu_amd,
  355. };
  356. struct microcode_ops * __init init_amd_microcode(void)
  357. {
  358. return &microcode_amd_ops;
  359. }