PageRenderTime 26ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/arch/x86/kernel/microcode_amd.c

https://github.com/jankeromnes/linux
C | 355 lines | 267 code | 68 blank | 20 comment | 40 complexity | 1ec48dca695c347fea70d3b32816090c MD5 | raw file
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  17. #include <linux/firmware.h>
  18. #include <linux/pci_ids.h>
  19. #include <linux/uaccess.h>
  20. #include <linux/vmalloc.h>
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/pci.h>
  24. #include <asm/microcode.h>
  25. #include <asm/processor.h>
  26. #include <asm/msr.h>
  27. MODULE_DESCRIPTION("AMD Microcode Update Driver");
  28. MODULE_AUTHOR("Peter Oruba");
  29. MODULE_LICENSE("GPL v2");
  30. #define UCODE_MAGIC 0x00414d44
  31. #define UCODE_EQUIV_CPU_TABLE_TYPE 0x00000000
  32. #define UCODE_UCODE_TYPE 0x00000001
  33. struct equiv_cpu_entry {
  34. u32 installed_cpu;
  35. u32 fixed_errata_mask;
  36. u32 fixed_errata_compare;
  37. u16 equiv_cpu;
  38. u16 res;
  39. } __attribute__((packed));
  40. struct microcode_header_amd {
  41. u32 data_code;
  42. u32 patch_id;
  43. u16 mc_patch_data_id;
  44. u8 mc_patch_data_len;
  45. u8 init_flag;
  46. u32 mc_patch_data_checksum;
  47. u32 nb_dev_id;
  48. u32 sb_dev_id;
  49. u16 processor_rev_id;
  50. u8 nb_rev_id;
  51. u8 sb_rev_id;
  52. u8 bios_api_rev;
  53. u8 reserved1[3];
  54. u32 match_reg[8];
  55. } __attribute__((packed));
  56. struct microcode_amd {
  57. struct microcode_header_amd hdr;
  58. unsigned int mpb[0];
  59. };
  60. #define SECTION_HDR_SIZE 8
  61. #define CONTAINER_HDR_SZ 12
  62. static struct equiv_cpu_entry *equiv_cpu_table;
  63. static int collect_cpu_info_amd(int cpu, struct cpu_signature *csig)
  64. {
  65. struct cpuinfo_x86 *c = &cpu_data(cpu);
  66. if (c->x86_vendor != X86_VENDOR_AMD || c->x86 < 0x10) {
  67. pr_warning("CPU%d: family %d not supported\n", cpu, c->x86);
  68. return -1;
  69. }
  70. csig->rev = c->microcode;
  71. pr_info("CPU%d: patch_level=0x%08x\n", cpu, csig->rev);
  72. return 0;
  73. }
  74. static int get_matching_microcode(int cpu, struct microcode_header_amd *mc_hdr,
  75. int rev)
  76. {
  77. unsigned int current_cpu_id;
  78. u16 equiv_cpu_id = 0;
  79. unsigned int i = 0;
  80. BUG_ON(equiv_cpu_table == NULL);
  81. current_cpu_id = cpuid_eax(0x00000001);
  82. while (equiv_cpu_table[i].installed_cpu != 0) {
  83. if (current_cpu_id == equiv_cpu_table[i].installed_cpu) {
  84. equiv_cpu_id = equiv_cpu_table[i].equiv_cpu;
  85. break;
  86. }
  87. i++;
  88. }
  89. if (!equiv_cpu_id)
  90. return 0;
  91. if (mc_hdr->processor_rev_id != equiv_cpu_id)
  92. return 0;
  93. /* ucode might be chipset specific -- currently we don't support this */
  94. if (mc_hdr->nb_dev_id || mc_hdr->sb_dev_id) {
  95. pr_err("CPU%d: chipset specific code not yet supported\n",
  96. cpu);
  97. return 0;
  98. }
  99. if (mc_hdr->patch_id <= rev)
  100. return 0;
  101. return 1;
  102. }
  103. static int apply_microcode_amd(int cpu)
  104. {
  105. u32 rev, dummy;
  106. int cpu_num = raw_smp_processor_id();
  107. struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
  108. struct microcode_amd *mc_amd = uci->mc;
  109. struct cpuinfo_x86 *c = &cpu_data(cpu);
  110. /* We should bind the task to the CPU */
  111. BUG_ON(cpu_num != cpu);
  112. if (mc_amd == NULL)
  113. return 0;
  114. wrmsrl(MSR_AMD64_PATCH_LOADER, (u64)(long)&mc_amd->hdr.data_code);
  115. /* get patch id after patching */
  116. rdmsr(MSR_AMD64_PATCH_LEVEL, rev, dummy);
  117. /* check current patch id and patch's id for match */
  118. if (rev != mc_amd->hdr.patch_id) {
  119. pr_err("CPU%d: update failed for patch_level=0x%08x\n",
  120. cpu, mc_amd->hdr.patch_id);
  121. return -1;
  122. }
  123. pr_info("CPU%d: new patch_level=0x%08x\n", cpu, rev);
  124. uci->cpu_sig.rev = rev;
  125. c->microcode = rev;
  126. return 0;
  127. }
  128. static unsigned int verify_ucode_size(int cpu, const u8 *buf, unsigned int size)
  129. {
  130. struct cpuinfo_x86 *c = &cpu_data(cpu);
  131. u32 max_size, actual_size;
  132. #define F1XH_MPB_MAX_SIZE 2048
  133. #define F14H_MPB_MAX_SIZE 1824
  134. #define F15H_MPB_MAX_SIZE 4096
  135. switch (c->x86) {
  136. case 0x14:
  137. max_size = F14H_MPB_MAX_SIZE;
  138. break;
  139. case 0x15:
  140. max_size = F15H_MPB_MAX_SIZE;
  141. break;
  142. default:
  143. max_size = F1XH_MPB_MAX_SIZE;
  144. break;
  145. }
  146. actual_size = *(u32 *)(buf + 4);
  147. if (actual_size + SECTION_HDR_SIZE > size || actual_size > max_size) {
  148. pr_err("section size mismatch\n");
  149. return 0;
  150. }
  151. return actual_size;
  152. }
  153. static struct microcode_header_amd *
  154. get_next_ucode(int cpu, const u8 *buf, unsigned int size, unsigned int *mc_size)
  155. {
  156. struct microcode_header_amd *mc = NULL;
  157. unsigned int actual_size = 0;
  158. if (*(u32 *)buf != UCODE_UCODE_TYPE) {
  159. pr_err("invalid type field in container file section header\n");
  160. goto out;
  161. }
  162. actual_size = verify_ucode_size(cpu, buf, size);
  163. if (!actual_size)
  164. goto out;
  165. mc = vzalloc(actual_size);
  166. if (!mc)
  167. goto out;
  168. get_ucode_data(mc, buf + SECTION_HDR_SIZE, actual_size);
  169. *mc_size = actual_size + SECTION_HDR_SIZE;
  170. out:
  171. return mc;
  172. }
  173. static int install_equiv_cpu_table(const u8 *buf)
  174. {
  175. unsigned int *ibuf = (unsigned int *)buf;
  176. unsigned int type = ibuf[1];
  177. unsigned int size = ibuf[2];
  178. if (type != UCODE_EQUIV_CPU_TABLE_TYPE || !size) {
  179. pr_err("empty section/"
  180. "invalid type field in container file section header\n");
  181. return -EINVAL;
  182. }
  183. equiv_cpu_table = vmalloc(size);
  184. if (!equiv_cpu_table) {
  185. pr_err("failed to allocate equivalent CPU table\n");
  186. return -ENOMEM;
  187. }
  188. get_ucode_data(equiv_cpu_table, buf + CONTAINER_HDR_SZ, size);
  189. /* add header length */
  190. return size + CONTAINER_HDR_SZ;
  191. }
  192. static void free_equiv_cpu_table(void)
  193. {
  194. vfree(equiv_cpu_table);
  195. equiv_cpu_table = NULL;
  196. }
  197. static enum ucode_state
  198. generic_load_microcode(int cpu, const u8 *data, size_t size)
  199. {
  200. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  201. struct microcode_header_amd *mc_hdr = NULL;
  202. unsigned int mc_size, leftover;
  203. int offset;
  204. const u8 *ucode_ptr = data;
  205. void *new_mc = NULL;
  206. unsigned int new_rev = uci->cpu_sig.rev;
  207. enum ucode_state state = UCODE_OK;
  208. offset = install_equiv_cpu_table(ucode_ptr);
  209. if (offset < 0) {
  210. pr_err("failed to create equivalent cpu table\n");
  211. return UCODE_ERROR;
  212. }
  213. ucode_ptr += offset;
  214. leftover = size - offset;
  215. while (leftover) {
  216. mc_hdr = get_next_ucode(cpu, ucode_ptr, leftover, &mc_size);
  217. if (!mc_hdr)
  218. break;
  219. if (get_matching_microcode(cpu, mc_hdr, new_rev)) {
  220. vfree(new_mc);
  221. new_rev = mc_hdr->patch_id;
  222. new_mc = mc_hdr;
  223. } else
  224. vfree(mc_hdr);
  225. ucode_ptr += mc_size;
  226. leftover -= mc_size;
  227. }
  228. if (!new_mc) {
  229. state = UCODE_NFOUND;
  230. goto free_table;
  231. }
  232. if (!leftover) {
  233. vfree(uci->mc);
  234. uci->mc = new_mc;
  235. pr_debug("CPU%d update ucode (0x%08x -> 0x%08x)\n",
  236. cpu, uci->cpu_sig.rev, new_rev);
  237. } else {
  238. vfree(new_mc);
  239. state = UCODE_ERROR;
  240. }
  241. free_table:
  242. free_equiv_cpu_table();
  243. return state;
  244. }
  245. static enum ucode_state request_microcode_amd(int cpu, struct device *device)
  246. {
  247. const char *fw_name = "amd-ucode/microcode_amd.bin";
  248. const struct firmware *fw;
  249. enum ucode_state ret = UCODE_NFOUND;
  250. if (request_firmware(&fw, fw_name, device)) {
  251. pr_err("failed to load file %s\n", fw_name);
  252. goto out;
  253. }
  254. ret = UCODE_ERROR;
  255. if (*(u32 *)fw->data != UCODE_MAGIC) {
  256. pr_err("invalid magic value (0x%08x)\n", *(u32 *)fw->data);
  257. goto fw_release;
  258. }
  259. ret = generic_load_microcode(cpu, fw->data, fw->size);
  260. fw_release:
  261. release_firmware(fw);
  262. out:
  263. return ret;
  264. }
  265. static enum ucode_state
  266. request_microcode_user(int cpu, const void __user *buf, size_t size)
  267. {
  268. pr_info("AMD microcode update via /dev/cpu/microcode not supported\n");
  269. return UCODE_ERROR;
  270. }
  271. static void microcode_fini_cpu_amd(int cpu)
  272. {
  273. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  274. vfree(uci->mc);
  275. uci->mc = NULL;
  276. }
  277. static struct microcode_ops microcode_amd_ops = {
  278. .request_microcode_user = request_microcode_user,
  279. .request_microcode_fw = request_microcode_amd,
  280. .collect_cpu_info = collect_cpu_info_amd,
  281. .apply_microcode = apply_microcode_amd,
  282. .microcode_fini_cpu = microcode_fini_cpu_amd,
  283. };
  284. struct microcode_ops * __init init_amd_microcode(void)
  285. {
  286. return &microcode_amd_ops;
  287. }