microcode_amd.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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("GPLv2");
  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) /* 896 bytes */
  46. #define MC_HEADER_SIZE (sizeof(struct microcode_header_amd)) /* 64 bytes */
  47. #define DEFAULT_UCODE_TOTALSIZE (DEFAULT_UCODE_DATASIZE + MC_HEADER_SIZE) /* 960 bytes */
  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. extern int microcode_init(void *opaque, struct module *module);
  54. extern void microcode_exit(void);
  55. /* serialize access to the physical write */
  56. static DEFINE_SPINLOCK(microcode_update_lock);
  57. /* no concurrent ->write()s are allowed on /dev/cpu/microcode */
  58. extern struct mutex (microcode_mutex);
  59. struct equiv_cpu_entry *equiv_cpu_table;
  60. extern struct ucode_cpu_info ucode_cpu_info[NR_CPUS];
  61. static void collect_cpu_info_amd(int cpu)
  62. {
  63. struct cpuinfo_x86 *c = &cpu_data(cpu);
  64. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  65. /* We should bind the task to the CPU */
  66. BUG_ON(raw_smp_processor_id() != cpu);
  67. uci->rev = 0;
  68. uci->pf = 0;
  69. uci->mc.mc_amd = NULL;
  70. uci->valid = 1;
  71. if (c->x86_vendor != X86_VENDOR_AMD || c->x86 < 0x10) {
  72. printk(KERN_ERR "microcode: CPU%d not a capable AMD processor\n",
  73. cpu);
  74. uci->valid = 0;
  75. return;
  76. }
  77. asm volatile("movl %1, %%ecx; rdmsr"
  78. : "=a" (uci->rev)
  79. : "i" (0x0000008B) : "ecx");
  80. printk(KERN_INFO "microcode: collect_cpu_info_amd : patch_id=0x%x\n",
  81. uci->rev);
  82. }
  83. static int get_matching_microcode_amd(void *mc, int cpu)
  84. {
  85. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  86. struct microcode_header_amd *mc_header = mc;
  87. unsigned long total_size = get_totalsize(mc_header);
  88. void *new_mc;
  89. struct pci_dev *nb_pci_dev, *sb_pci_dev;
  90. unsigned int current_cpu_id;
  91. unsigned int equiv_cpu_id = 0x00;
  92. unsigned int i = 0;
  93. /* We should bind the task to the CPU */
  94. BUG_ON(cpu != raw_smp_processor_id());
  95. /* This is a tricky part. We might be called from a write operation */
  96. /* to the device file instead of the usual process of firmware */
  97. /* loading. This routine needs to be able to distinguish both */
  98. /* cases. This is done by checking if there alread is a equivalent */
  99. /* CPU table installed. If not, we're written through */
  100. /* /dev/cpu/microcode. */
  101. /* Since we ignore all checks. The error case in which going through */
  102. /* firmware loading and that table is not loaded has already been */
  103. /* checked earlier. */
  104. if (equiv_cpu_table == NULL) {
  105. printk(KERN_INFO "microcode: CPU%d microcode update with "
  106. "version 0x%x (current=0x%x)\n",
  107. cpu, mc_header->patch_id, uci->rev);
  108. goto out;
  109. }
  110. current_cpu_id = cpuid_eax(0x00000001);
  111. while (equiv_cpu_table[i].installed_cpu != 0) {
  112. if (current_cpu_id == equiv_cpu_table[i].installed_cpu) {
  113. equiv_cpu_id = equiv_cpu_table[i].equiv_cpu;
  114. break;
  115. }
  116. i++;
  117. }
  118. if (!equiv_cpu_id) {
  119. printk(KERN_ERR "microcode: CPU%d cpu_id "
  120. "not found in equivalent cpu table \n", cpu);
  121. return 0;
  122. }
  123. if ((mc_header->processor_rev_id[0]) != (equiv_cpu_id & 0xff)) {
  124. printk(KERN_ERR
  125. "microcode: CPU%d patch does not match "
  126. "(patch is %x, cpu extended is %x) \n",
  127. cpu, mc_header->processor_rev_id[0],
  128. (equiv_cpu_id & 0xff));
  129. return 0;
  130. }
  131. if ((mc_header->processor_rev_id[1]) != ((equiv_cpu_id >> 16) & 0xff)) {
  132. printk(KERN_ERR "microcode: CPU%d patch does not match "
  133. "(patch is %x, cpu base id is %x) \n",
  134. cpu, mc_header->processor_rev_id[1],
  135. ((equiv_cpu_id >> 16) & 0xff));
  136. return 0;
  137. }
  138. /* ucode may be northbridge specific */
  139. if (mc_header->nb_dev_id) {
  140. nb_pci_dev = pci_get_device(PCI_VENDOR_ID_AMD,
  141. (mc_header->nb_dev_id & 0xff),
  142. NULL);
  143. if ((!nb_pci_dev) ||
  144. (mc_header->nb_rev_id != nb_pci_dev->revision)) {
  145. printk(KERN_ERR "microcode: CPU%d NB mismatch \n", cpu);
  146. pci_dev_put(nb_pci_dev);
  147. return 0;
  148. }
  149. pci_dev_put(nb_pci_dev);
  150. }
  151. /* ucode may be southbridge specific */
  152. if (mc_header->sb_dev_id) {
  153. sb_pci_dev = pci_get_device(PCI_VENDOR_ID_AMD,
  154. (mc_header->sb_dev_id & 0xff),
  155. NULL);
  156. if ((!sb_pci_dev) ||
  157. (mc_header->sb_rev_id != sb_pci_dev->revision)) {
  158. printk(KERN_ERR "microcode: CPU%d SB mismatch \n", cpu);
  159. pci_dev_put(sb_pci_dev);
  160. return 0;
  161. }
  162. pci_dev_put(sb_pci_dev);
  163. }
  164. if (mc_header->patch_id <= uci->rev)
  165. return 0;
  166. printk(KERN_INFO "microcode: CPU%d found a matching microcode "
  167. "update with version 0x%x (current=0x%x)\n",
  168. cpu, mc_header->patch_id, uci->rev);
  169. out:
  170. new_mc = vmalloc(UCODE_MAX_SIZE);
  171. if (!new_mc) {
  172. printk(KERN_ERR "microcode: error, can't allocate memory\n");
  173. return -ENOMEM;
  174. }
  175. memset(new_mc, 0, UCODE_MAX_SIZE);
  176. /* free previous update file */
  177. vfree(uci->mc.mc_amd);
  178. memcpy(new_mc, mc, total_size);
  179. uci->mc.mc_amd = new_mc;
  180. return 1;
  181. }
  182. static void apply_microcode_amd(int cpu)
  183. {
  184. unsigned long flags;
  185. unsigned int eax, edx;
  186. unsigned int rev;
  187. int cpu_num = raw_smp_processor_id();
  188. struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
  189. /* We should bind the task to the CPU */
  190. BUG_ON(cpu_num != cpu);
  191. if (uci->mc.mc_amd == NULL)
  192. return;
  193. spin_lock_irqsave(&microcode_update_lock, flags);
  194. edx = (unsigned int)(((unsigned long)
  195. &(uci->mc.mc_amd->hdr.data_code)) >> 32);
  196. eax = (unsigned int)(((unsigned long)
  197. &(uci->mc.mc_amd->hdr.data_code)) & 0xffffffffL);
  198. asm volatile("movl %0, %%ecx; wrmsr" :
  199. : "i" (0xc0010020), "a" (eax), "d" (edx) : "ecx");
  200. /* get patch id after patching */
  201. asm volatile("movl %1, %%ecx; rdmsr"
  202. : "=a" (rev)
  203. : "i" (0x0000008B) : "ecx");
  204. spin_unlock_irqrestore(&microcode_update_lock, flags);
  205. /* check current patch id and patch's id for match */
  206. if (rev != uci->mc.mc_amd->hdr.patch_id) {
  207. printk(KERN_ERR "microcode: CPU%d update from revision "
  208. "0x%x to 0x%x failed\n", cpu_num,
  209. uci->mc.mc_amd->hdr.patch_id, rev);
  210. return;
  211. }
  212. printk(KERN_INFO "microcode: CPU%d updated from revision "
  213. "0x%x to 0x%x \n",
  214. cpu_num, uci->rev, uci->mc.mc_amd->hdr.patch_id);
  215. uci->rev = rev;
  216. }
  217. #ifdef CONFIG_MICROCODE_OLD_INTERFACE
  218. extern void __user *user_buffer; /* user area microcode data buffer */
  219. extern unsigned int user_buffer_size; /* it's size */
  220. static long get_next_ucode_amd(void **mc, long offset)
  221. {
  222. struct microcode_header_amd mc_header;
  223. unsigned long total_size;
  224. /* No more data */
  225. if (offset >= user_buffer_size)
  226. return 0;
  227. if (copy_from_user(&mc_header, user_buffer + offset, MC_HEADER_SIZE)) {
  228. printk(KERN_ERR "microcode: error! Can not read user data\n");
  229. return -EFAULT;
  230. }
  231. total_size = get_totalsize(&mc_header);
  232. if (offset + total_size > user_buffer_size) {
  233. printk(KERN_ERR "microcode: error! Bad total size in microcode "
  234. "data file\n");
  235. return -EINVAL;
  236. }
  237. *mc = vmalloc(UCODE_MAX_SIZE);
  238. if (!*mc)
  239. return -ENOMEM;
  240. memset(*mc, 0, UCODE_MAX_SIZE);
  241. if (copy_from_user(*mc, user_buffer + offset, total_size)) {
  242. printk(KERN_ERR "microcode: error! Can not read user data\n");
  243. vfree(*mc);
  244. return -EFAULT;
  245. }
  246. return offset + total_size;
  247. }
  248. #else
  249. #define get_next_ucode_amd() NULL
  250. #endif
  251. static long get_next_ucode_from_buffer_amd(void **mc, void *buf,
  252. unsigned long size, long offset)
  253. {
  254. struct microcode_header_amd *mc_header;
  255. unsigned long total_size;
  256. unsigned char *buf_pos = buf;
  257. /* No more data */
  258. if (offset >= size)
  259. return 0;
  260. if (buf_pos[offset] != UCODE_UCODE_TYPE) {
  261. printk(KERN_ERR "microcode: error! "
  262. "Wrong microcode payload type field\n");
  263. return -EINVAL;
  264. }
  265. mc_header = (struct microcode_header_amd *)(&buf_pos[offset+8]);
  266. total_size = (unsigned long) (buf_pos[offset+4] +
  267. (buf_pos[offset+5] << 8));
  268. printk(KERN_INFO "microcode: size %lu, total_size %lu, offset %ld\n",
  269. size, total_size, offset);
  270. if (offset + total_size > size) {
  271. printk(KERN_ERR "microcode: error! Bad data in microcode data file\n");
  272. return -EINVAL;
  273. }
  274. *mc = vmalloc(UCODE_MAX_SIZE);
  275. if (!*mc) {
  276. printk(KERN_ERR "microcode: error! "
  277. "Can not allocate memory for microcode patch\n");
  278. return -ENOMEM;
  279. }
  280. memset(*mc, 0, UCODE_MAX_SIZE);
  281. memcpy(*mc, buf + offset + 8, total_size);
  282. return offset + total_size + 8;
  283. }
  284. static long install_equiv_cpu_table(void *buf, unsigned long size, long offset)
  285. {
  286. unsigned int *buf_pos = buf;
  287. /* No more data */
  288. if (offset >= size)
  289. return 0;
  290. if (buf_pos[1] != UCODE_EQUIV_CPU_TABLE_TYPE) {
  291. printk(KERN_ERR "microcode: error! "
  292. "Wrong microcode equivalnet cpu table type field\n");
  293. return 0;
  294. }
  295. if (size == 0) {
  296. printk(KERN_ERR "microcode: error! "
  297. "Wrong microcode equivalnet cpu table length\n");
  298. return 0;
  299. }
  300. equiv_cpu_table = (struct equiv_cpu_entry *) vmalloc(size);
  301. if (!equiv_cpu_table) {
  302. printk(KERN_ERR "microcode: error, can't allocate memory for equiv CPU table\n");
  303. return 0;
  304. }
  305. memset(equiv_cpu_table, 0, size);
  306. memcpy(equiv_cpu_table, &buf_pos[3], size);
  307. return size + 12; /* add header length */
  308. }
  309. /* fake device for request_firmware */
  310. extern struct platform_device *microcode_pdev;
  311. static int cpu_request_microcode_amd(int cpu)
  312. {
  313. char name[30];
  314. const struct firmware *firmware;
  315. void *buf;
  316. unsigned int *buf_pos;
  317. unsigned long size;
  318. long offset = 0;
  319. int error;
  320. void *mc;
  321. /* We should bind the task to the CPU */
  322. BUG_ON(cpu != raw_smp_processor_id());
  323. sprintf(name, "amd-ucode/microcode_amd.bin");
  324. error = request_firmware(&firmware, "amd-ucode/microcode_amd.bin",
  325. &microcode_pdev->dev);
  326. if (error) {
  327. printk(KERN_ERR "microcode: ucode data file %s load failed\n",
  328. name);
  329. return error;
  330. }
  331. buf_pos = buf = firmware->data;
  332. size = firmware->size;
  333. if (buf_pos[0] != UCODE_MAGIC) {
  334. printk(KERN_ERR "microcode: error! Wrong microcode patch file magic\n");
  335. return -EINVAL;
  336. }
  337. offset = install_equiv_cpu_table(buf, buf_pos[2], offset);
  338. if (!offset) {
  339. printk(KERN_ERR "microcode: installing equivalent cpu table failed\n");
  340. return -EINVAL;
  341. }
  342. while ((offset =
  343. get_next_ucode_from_buffer_amd(&mc, buf, size, offset)) > 0) {
  344. error = get_matching_microcode_amd(mc, cpu);
  345. if (error < 0)
  346. break;
  347. /*
  348. * It's possible the data file has multiple matching ucode,
  349. * lets keep searching till the latest version
  350. */
  351. if (error == 1) {
  352. apply_microcode_amd(cpu);
  353. error = 0;
  354. }
  355. vfree(mc);
  356. }
  357. if (offset > 0) {
  358. vfree(mc);
  359. vfree(equiv_cpu_table);
  360. equiv_cpu_table = NULL;
  361. }
  362. if (offset < 0)
  363. error = offset;
  364. release_firmware(firmware);
  365. return error;
  366. }
  367. static int apply_microcode_check_cpu_amd(int cpu)
  368. {
  369. struct cpuinfo_x86 *c = &cpu_data(cpu);
  370. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  371. unsigned int rev;
  372. cpumask_t old;
  373. cpumask_of_cpu_ptr(newmask, cpu);
  374. int err = 0;
  375. /* Check if the microcode is available */
  376. if (!uci->mc.mc_amd)
  377. return 0;
  378. old = current->cpus_allowed;
  379. set_cpus_allowed(current, newmask);
  380. /* Check if the microcode we have in memory matches the CPU */
  381. if (c->x86_vendor != X86_VENDOR_AMD || c->x86 < 16)
  382. err = -EINVAL;
  383. if (!err) {
  384. asm volatile("movl %1, %%ecx; rdmsr"
  385. : "=a" (rev)
  386. : "i" (0x0000008B) : "ecx");
  387. if (uci->rev != rev)
  388. err = -EINVAL;
  389. }
  390. if (!err)
  391. apply_microcode_amd(cpu);
  392. else
  393. printk(KERN_ERR "microcode: Could not apply microcode to CPU%d:"
  394. " rev=0x%x\n",
  395. cpu, uci->rev);
  396. set_cpus_allowed(current, old);
  397. return err;
  398. }
  399. static void microcode_fini_cpu_amd(int cpu)
  400. {
  401. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  402. mutex_lock(&microcode_mutex);
  403. uci->valid = 0;
  404. vfree(uci->mc.mc_amd);
  405. uci->mc.mc_amd = NULL;
  406. mutex_unlock(&microcode_mutex);
  407. }
  408. static struct microcode_ops microcode_amd_ops = {
  409. .get_next_ucode = get_next_ucode_amd,
  410. .get_matching_microcode = get_matching_microcode_amd,
  411. .microcode_sanity_check = NULL,
  412. .apply_microcode_check_cpu = apply_microcode_check_cpu_amd,
  413. .cpu_request_microcode = cpu_request_microcode_amd,
  414. .collect_cpu_info = collect_cpu_info_amd,
  415. .apply_microcode = apply_microcode_amd,
  416. .microcode_fini_cpu = microcode_fini_cpu_amd,
  417. };
  418. static int __init microcode_amd_module_init(void)
  419. {
  420. struct cpuinfo_x86 *c = &cpu_data(get_cpu());
  421. equiv_cpu_table = NULL;
  422. if (c->x86_vendor == X86_VENDOR_AMD)
  423. return microcode_init(&microcode_amd_ops, THIS_MODULE);
  424. else
  425. return -ENODEV;
  426. }
  427. static void __exit microcode_amd_module_exit(void)
  428. {
  429. microcode_exit();
  430. }
  431. module_init(microcode_amd_module_init)
  432. module_exit(microcode_amd_module_exit)