microcode_amd.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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 <peter.oruba@amd.com>");
  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. #define UCODE_MAX_SIZE (2048)
  45. #define DEFAULT_UCODE_DATASIZE (896)
  46. #define MC_HEADER_SIZE (sizeof(struct microcode_header_amd))
  47. #define DEFAULT_UCODE_TOTALSIZE (DEFAULT_UCODE_DATASIZE + MC_HEADER_SIZE)
  48. #define DWSIZE (sizeof(u32))
  49. /* For now we support a fixed ucode total size only */
  50. #define get_totalsize(mc) \
  51. ((((struct microcode_amd *)mc)->hdr.mc_patch_data_len * 28) \
  52. + MC_HEADER_SIZE)
  53. /* serialize access to the physical write */
  54. static DEFINE_SPINLOCK(microcode_update_lock);
  55. static struct equiv_cpu_entry *equiv_cpu_table;
  56. static int collect_cpu_info_amd(int cpu, struct cpu_signature *csig)
  57. {
  58. struct cpuinfo_x86 *c = &cpu_data(cpu);
  59. memset(csig, 0, sizeof(*csig));
  60. if (c->x86_vendor != X86_VENDOR_AMD || c->x86 < 0x10) {
  61. printk(KERN_ERR "microcode: CPU%d not a capable AMD processor\n",
  62. cpu);
  63. return -1;
  64. }
  65. asm volatile("movl %1, %%ecx; rdmsr"
  66. : "=a" (csig->rev)
  67. : "i" (0x0000008B) : "ecx");
  68. printk(KERN_INFO "microcode: collect_cpu_info_amd : patch_id=0x%x\n",
  69. csig->rev);
  70. return 0;
  71. }
  72. static int get_matching_microcode(int cpu, void *mc, int rev)
  73. {
  74. struct microcode_header_amd *mc_header = mc;
  75. struct pci_dev *nb_pci_dev, *sb_pci_dev;
  76. unsigned int current_cpu_id;
  77. unsigned int equiv_cpu_id = 0x00;
  78. unsigned int i = 0;
  79. /*
  80. * dimm: do we need this? Why an update via /dev/... is different
  81. * from the one via firmware?
  82. *
  83. * This is a tricky part. We might be called from a write operation
  84. * to the device file instead of the usual process of firmware
  85. * loading. This routine needs to be able to distinguish both
  86. * cases. This is done by checking if there alread is a equivalent
  87. * CPU table installed. If not, we're written through
  88. * /dev/cpu/microcode.
  89. * Since we ignore all checks. The error case in which going through
  90. * firmware loading and that table is not loaded has already been
  91. * checked earlier.
  92. */
  93. BUG_ON(equiv_cpu_table == NULL);
  94. #if 0
  95. if (equiv_cpu_table == NULL) {
  96. printk(KERN_INFO "microcode: CPU%d microcode update with "
  97. "version 0x%x (current=0x%x)\n",
  98. cpu, mc_header->patch_id, uci->cpu_sig.rev);
  99. goto out;
  100. }
  101. #endif
  102. current_cpu_id = cpuid_eax(0x00000001);
  103. while (equiv_cpu_table[i].installed_cpu != 0) {
  104. if (current_cpu_id == equiv_cpu_table[i].installed_cpu) {
  105. equiv_cpu_id = equiv_cpu_table[i].equiv_cpu;
  106. break;
  107. }
  108. i++;
  109. }
  110. if (!equiv_cpu_id) {
  111. printk(KERN_ERR "microcode: CPU%d cpu_id "
  112. "not found in equivalent cpu table \n", cpu);
  113. return 0;
  114. }
  115. if ((mc_header->processor_rev_id[0]) != (equiv_cpu_id & 0xff)) {
  116. printk(KERN_ERR
  117. "microcode: CPU%d patch does not match "
  118. "(patch is %x, cpu extended is %x) \n",
  119. cpu, mc_header->processor_rev_id[0],
  120. (equiv_cpu_id & 0xff));
  121. return 0;
  122. }
  123. if ((mc_header->processor_rev_id[1]) != ((equiv_cpu_id >> 16) & 0xff)) {
  124. printk(KERN_ERR "microcode: CPU%d patch does not match "
  125. "(patch is %x, cpu base id is %x) \n",
  126. cpu, mc_header->processor_rev_id[1],
  127. ((equiv_cpu_id >> 16) & 0xff));
  128. return 0;
  129. }
  130. /* ucode may be northbridge specific */
  131. if (mc_header->nb_dev_id) {
  132. nb_pci_dev = pci_get_device(PCI_VENDOR_ID_AMD,
  133. (mc_header->nb_dev_id & 0xff),
  134. NULL);
  135. if ((!nb_pci_dev) ||
  136. (mc_header->nb_rev_id != nb_pci_dev->revision)) {
  137. printk(KERN_ERR "microcode: CPU%d NB mismatch \n", cpu);
  138. pci_dev_put(nb_pci_dev);
  139. return 0;
  140. }
  141. pci_dev_put(nb_pci_dev);
  142. }
  143. /* ucode may be southbridge specific */
  144. if (mc_header->sb_dev_id) {
  145. sb_pci_dev = pci_get_device(PCI_VENDOR_ID_AMD,
  146. (mc_header->sb_dev_id & 0xff),
  147. NULL);
  148. if ((!sb_pci_dev) ||
  149. (mc_header->sb_rev_id != sb_pci_dev->revision)) {
  150. printk(KERN_ERR "microcode: CPU%d SB mismatch \n", cpu);
  151. pci_dev_put(sb_pci_dev);
  152. return 0;
  153. }
  154. pci_dev_put(sb_pci_dev);
  155. }
  156. if (mc_header->patch_id <= rev)
  157. return 0;
  158. return 1;
  159. }
  160. static void apply_microcode_amd(int cpu)
  161. {
  162. unsigned long flags;
  163. unsigned int eax, edx;
  164. unsigned int rev;
  165. int cpu_num = raw_smp_processor_id();
  166. struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
  167. unsigned long addr;
  168. /* We should bind the task to the CPU */
  169. BUG_ON(cpu_num != cpu);
  170. if (uci->mc.mc_amd == NULL)
  171. return;
  172. spin_lock_irqsave(&microcode_update_lock, flags);
  173. addr = (unsigned long)&uci->mc.mc_amd->hdr.data_code;
  174. edx = (unsigned int)(((unsigned long)upper_32_bits(addr)));
  175. eax = (unsigned int)(((unsigned long)lower_32_bits(addr)));
  176. asm volatile("movl %0, %%ecx; wrmsr" :
  177. : "i" (0xc0010020), "a" (eax), "d" (edx) : "ecx");
  178. /* get patch id after patching */
  179. asm volatile("movl %1, %%ecx; rdmsr"
  180. : "=a" (rev)
  181. : "i" (0x0000008B) : "ecx");
  182. spin_unlock_irqrestore(&microcode_update_lock, flags);
  183. /* check current patch id and patch's id for match */
  184. if (rev != uci->mc.mc_amd->hdr.patch_id) {
  185. printk(KERN_ERR "microcode: CPU%d update from revision "
  186. "0x%x to 0x%x failed\n", cpu_num,
  187. uci->mc.mc_amd->hdr.patch_id, rev);
  188. return;
  189. }
  190. printk(KERN_INFO "microcode: CPU%d updated from revision "
  191. "0x%x to 0x%x \n",
  192. cpu_num, uci->cpu_sig.rev, uci->mc.mc_amd->hdr.patch_id);
  193. uci->cpu_sig.rev = rev;
  194. }
  195. static void * get_next_ucode(u8 *buf, unsigned int size,
  196. int (*get_ucode_data)(void *, const void *, size_t),
  197. unsigned int *mc_size)
  198. {
  199. unsigned int total_size;
  200. #define UCODE_UNKNOWN_HDR 8
  201. u8 hdr[UCODE_UNKNOWN_HDR];
  202. void *mc;
  203. if (get_ucode_data(hdr, buf, UCODE_UNKNOWN_HDR))
  204. return NULL;
  205. if (hdr[0] != UCODE_UCODE_TYPE) {
  206. printk(KERN_ERR "microcode: error! "
  207. "Wrong microcode payload type field\n");
  208. return NULL;
  209. }
  210. /* Why not by means of get_totalsize(hdr)? */
  211. total_size = (unsigned long) (hdr[4] + (hdr[5] << 8));
  212. printk(KERN_INFO "microcode: size %u, total_size %u\n",
  213. size, total_size);
  214. if (total_size > size || total_size > UCODE_MAX_SIZE) {
  215. printk(KERN_ERR "microcode: error! Bad data in microcode data file\n");
  216. return NULL;
  217. }
  218. mc = vmalloc(UCODE_MAX_SIZE);
  219. if (mc) {
  220. memset(mc, 0, UCODE_MAX_SIZE);
  221. if (get_ucode_data(mc, buf + UCODE_UNKNOWN_HDR, total_size)) {
  222. vfree(mc);
  223. mc = NULL;
  224. } else
  225. *mc_size = total_size + UCODE_UNKNOWN_HDR;
  226. }
  227. #undef UCODE_UNKNOWN_HDR
  228. return mc;
  229. }
  230. static int install_equiv_cpu_table(u8 *buf,
  231. int (*get_ucode_data)(void *, const void *, size_t))
  232. {
  233. #define UCODE_HEADER_SIZE 12
  234. u8 *hdr[UCODE_HEADER_SIZE];
  235. unsigned int *buf_pos = (unsigned int *)hdr;
  236. unsigned long size;
  237. if (get_ucode_data(&hdr, buf, UCODE_HEADER_SIZE))
  238. return 0;
  239. size = buf_pos[2];
  240. if (buf_pos[1] != UCODE_EQUIV_CPU_TABLE_TYPE || !size) {
  241. printk(KERN_ERR "microcode: error! "
  242. "Wrong microcode equivalnet cpu table\n");
  243. return 0;
  244. }
  245. equiv_cpu_table = (struct equiv_cpu_entry *) vmalloc(size);
  246. if (!equiv_cpu_table) {
  247. printk(KERN_ERR "microcode: error, can't allocate memory for equiv CPU table\n");
  248. return 0;
  249. }
  250. buf += UCODE_HEADER_SIZE;
  251. if (get_ucode_data(equiv_cpu_table, buf, size)) {
  252. vfree(equiv_cpu_table);
  253. return 0;
  254. }
  255. return size + UCODE_HEADER_SIZE; /* add header length */
  256. #undef UCODE_HEADER_SIZE
  257. }
  258. static void free_equiv_cpu_table(void)
  259. {
  260. if (equiv_cpu_table) {
  261. vfree(equiv_cpu_table);
  262. equiv_cpu_table = NULL;
  263. }
  264. }
  265. static int generic_load_microcode(int cpu, void *data, size_t size,
  266. int (*get_ucode_data)(void *, const void *, size_t))
  267. {
  268. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  269. u8 *ucode_ptr = data, *new_mc = NULL, *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, get_ucode_data);
  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 mc_size;
  282. struct microcode_header_amd *mc_header;
  283. mc = get_next_ucode(ucode_ptr, leftover, get_ucode_data, &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. new_rev = mc_header->patch_id;
  289. new_mc = mc;
  290. } else
  291. vfree(mc);
  292. ucode_ptr += mc_size;
  293. leftover -= mc_size;
  294. }
  295. if (new_mc) {
  296. if (!leftover) {
  297. if (uci->mc.mc_amd)
  298. vfree(uci->mc.mc_amd);
  299. uci->mc.mc_amd = (struct microcode_amd *)new_mc;
  300. pr_debug("microcode: CPU%d found a matching microcode update with"
  301. " version 0x%x (current=0x%x)\n",
  302. cpu, uci->mc.mc_amd->hdr.patch_id, uci->cpu_sig.rev);
  303. } else
  304. vfree(new_mc);
  305. }
  306. free_equiv_cpu_table();
  307. return (int)leftover;
  308. }
  309. static int get_ucode_fw(void *to, const void *from, size_t n)
  310. {
  311. memcpy(to, from, n);
  312. return 0;
  313. }
  314. static int request_microcode_fw(int cpu, struct device *device)
  315. {
  316. const char *fw_name = "amd-ucode/microcode_amd.bin";
  317. const struct firmware *firmware;
  318. int ret;
  319. /* We should bind the task to the CPU */
  320. BUG_ON(cpu != raw_smp_processor_id());
  321. ret = request_firmware(&firmware, fw_name, device);
  322. if (ret) {
  323. printk(KERN_ERR "microcode: ucode data file %s load failed\n", fw_name);
  324. return ret;
  325. }
  326. ret = generic_load_microcode(cpu, (void*)firmware->data, firmware->size,
  327. &get_ucode_fw);
  328. release_firmware(firmware);
  329. return ret;
  330. }
  331. static int get_ucode_user(void *to, const void *from, size_t n)
  332. {
  333. return copy_from_user(to, from, n);
  334. }
  335. static int request_microcode_user(int cpu, const void __user *buf, size_t size)
  336. {
  337. /* We should bind the task to the CPU */
  338. BUG_ON(cpu != raw_smp_processor_id());
  339. return generic_load_microcode(cpu, (void*)buf, size, &get_ucode_user);
  340. }
  341. static void microcode_fini_cpu_amd(int cpu)
  342. {
  343. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  344. vfree(uci->mc.mc_amd);
  345. uci->mc.mc_amd = NULL;
  346. }
  347. static struct microcode_ops microcode_amd_ops = {
  348. .request_microcode_user = request_microcode_user,
  349. .request_microcode_fw = request_microcode_fw,
  350. .collect_cpu_info = collect_cpu_info_amd,
  351. .apply_microcode = apply_microcode_amd,
  352. .microcode_fini_cpu = microcode_fini_cpu_amd,
  353. };
  354. static int __init microcode_amd_module_init(void)
  355. {
  356. struct cpuinfo_x86 *c = &cpu_data(0);
  357. equiv_cpu_table = NULL;
  358. if (c->x86_vendor != X86_VENDOR_AMD) {
  359. printk(KERN_ERR "microcode: CPU platform is not AMD-capable\n");
  360. return -ENODEV;
  361. }
  362. return microcode_init(&microcode_amd_ops, THIS_MODULE);
  363. }
  364. static void __exit microcode_amd_module_exit(void)
  365. {
  366. microcode_exit();
  367. }
  368. module_init(microcode_amd_module_init)
  369. module_exit(microcode_amd_module_exit)