microcode_amd.c 13 KB

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