microcode_amd.c 10 KB

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