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

/arch/x86/kernel/microcode_amd.c

https://gitlab.com/billyprice1/samsung_codina_kernel
C | 373 lines | 269 code | 69 blank | 35 comment | 41 complexity | ad220b60d93d8fc9cfa511f2adda4aec 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 UCODE_CONTAINER_SECTION_HDR 8
  61. #define UCODE_CONTAINER_HEADER_SIZE 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. u32 dummy;
  67. if (c->x86_vendor != X86_VENDOR_AMD || c->x86 < 0x10) {
  68. pr_warning("CPU%d: family %d not supported\n", cpu, c->x86);
  69. return -1;
  70. }
  71. rdmsr(MSR_AMD64_PATCH_LEVEL, csig->rev, dummy);
  72. pr_info("CPU%d: patch_level=0x%08x\n", cpu, csig->rev);
  73. return 0;
  74. }
  75. static int get_matching_microcode(int cpu, struct microcode_header_amd *mc_hdr,
  76. int rev)
  77. {
  78. unsigned int current_cpu_id;
  79. u16 equiv_cpu_id = 0;
  80. unsigned int i = 0;
  81. BUG_ON(equiv_cpu_table == NULL);
  82. current_cpu_id = cpuid_eax(0x00000001);
  83. while (equiv_cpu_table[i].installed_cpu != 0) {
  84. if (current_cpu_id == equiv_cpu_table[i].installed_cpu) {
  85. equiv_cpu_id = equiv_cpu_table[i].equiv_cpu;
  86. break;
  87. }
  88. i++;
  89. }
  90. if (!equiv_cpu_id)
  91. return 0;
  92. if (mc_hdr->processor_rev_id != equiv_cpu_id)
  93. return 0;
  94. /* ucode might be chipset specific -- currently we don't support this */
  95. if (mc_hdr->nb_dev_id || mc_hdr->sb_dev_id) {
  96. pr_err("CPU%d: chipset specific code not yet supported\n",
  97. cpu);
  98. return 0;
  99. }
  100. if (mc_hdr->patch_id <= rev)
  101. return 0;
  102. return 1;
  103. }
  104. static int apply_microcode_amd(int cpu)
  105. {
  106. u32 rev, dummy;
  107. int cpu_num = raw_smp_processor_id();
  108. struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
  109. struct microcode_amd *mc_amd = uci->mc;
  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. return 0;
  126. }
  127. static unsigned int verify_ucode_size(int cpu, const u8 *buf, unsigned int size)
  128. {
  129. struct cpuinfo_x86 *c = &cpu_data(cpu);
  130. unsigned int max_size, actual_size;
  131. #define F1XH_MPB_MAX_SIZE 2048
  132. #define F14H_MPB_MAX_SIZE 1824
  133. #define F15H_MPB_MAX_SIZE 4096
  134. switch (c->x86) {
  135. case 0x14:
  136. max_size = F14H_MPB_MAX_SIZE;
  137. break;
  138. case 0x15:
  139. max_size = F15H_MPB_MAX_SIZE;
  140. break;
  141. default:
  142. max_size = F1XH_MPB_MAX_SIZE;
  143. break;
  144. }
  145. actual_size = buf[4] + (buf[5] << 8);
  146. if (actual_size > size || actual_size > max_size) {
  147. pr_err("section size mismatch\n");
  148. return 0;
  149. }
  150. return actual_size;
  151. }
  152. static struct microcode_header_amd *
  153. get_next_ucode(int cpu, const u8 *buf, unsigned int size, unsigned int *mc_size)
  154. {
  155. struct microcode_header_amd *mc = NULL;
  156. unsigned int actual_size = 0;
  157. if (buf[0] != UCODE_UCODE_TYPE) {
  158. pr_err("invalid type field in container file section header\n");
  159. goto out;
  160. }
  161. actual_size = verify_ucode_size(cpu, buf, size);
  162. if (!actual_size)
  163. goto out;
  164. mc = vzalloc(actual_size);
  165. if (!mc)
  166. goto out;
  167. get_ucode_data(mc, buf + UCODE_CONTAINER_SECTION_HDR, actual_size);
  168. *mc_size = actual_size + UCODE_CONTAINER_SECTION_HDR;
  169. out:
  170. return mc;
  171. }
  172. static int install_equiv_cpu_table(const u8 *buf)
  173. {
  174. unsigned int *ibuf = (unsigned int *)buf;
  175. unsigned int type = ibuf[1];
  176. unsigned int size = ibuf[2];
  177. if (type != UCODE_EQUIV_CPU_TABLE_TYPE || !size) {
  178. pr_err("empty section/"
  179. "invalid type field in container file section header\n");
  180. return -EINVAL;
  181. }
  182. equiv_cpu_table = vmalloc(size);
  183. if (!equiv_cpu_table) {
  184. pr_err("failed to allocate equivalent CPU table\n");
  185. return -ENOMEM;
  186. }
  187. get_ucode_data(equiv_cpu_table, buf + UCODE_CONTAINER_HEADER_SIZE, size);
  188. return size + UCODE_CONTAINER_HEADER_SIZE; /* add header length */
  189. }
  190. static void free_equiv_cpu_table(void)
  191. {
  192. vfree(equiv_cpu_table);
  193. equiv_cpu_table = NULL;
  194. }
  195. static enum ucode_state
  196. generic_load_microcode(int cpu, const u8 *data, size_t size)
  197. {
  198. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  199. struct microcode_header_amd *mc_hdr = NULL;
  200. unsigned int mc_size, leftover;
  201. int offset;
  202. const u8 *ucode_ptr = data;
  203. void *new_mc = NULL;
  204. unsigned int new_rev = uci->cpu_sig.rev;
  205. enum ucode_state state = UCODE_OK;
  206. offset = install_equiv_cpu_table(ucode_ptr);
  207. if (offset < 0) {
  208. pr_err("failed to create equivalent cpu table\n");
  209. return UCODE_ERROR;
  210. }
  211. ucode_ptr += offset;
  212. leftover = size - offset;
  213. while (leftover) {
  214. mc_hdr = get_next_ucode(cpu, ucode_ptr, leftover, &mc_size);
  215. if (!mc_hdr)
  216. break;
  217. if (get_matching_microcode(cpu, mc_hdr, new_rev)) {
  218. vfree(new_mc);
  219. new_rev = mc_hdr->patch_id;
  220. new_mc = mc_hdr;
  221. } else
  222. vfree(mc_hdr);
  223. ucode_ptr += mc_size;
  224. leftover -= mc_size;
  225. }
  226. if (!new_mc) {
  227. state = UCODE_NFOUND;
  228. goto free_table;
  229. }
  230. if (!leftover) {
  231. vfree(uci->mc);
  232. uci->mc = new_mc;
  233. pr_debug("CPU%d update ucode (0x%08x -> 0x%08x)\n",
  234. cpu, uci->cpu_sig.rev, new_rev);
  235. } else {
  236. vfree(new_mc);
  237. state = UCODE_ERROR;
  238. }
  239. free_table:
  240. free_equiv_cpu_table();
  241. return state;
  242. }
  243. /*
  244. * AMD microcode firmware naming convention, up to family 15h they are in
  245. * the legacy file:
  246. *
  247. * amd-ucode/microcode_amd.bin
  248. *
  249. * This legacy file is always smaller than 2K in size.
  250. *
  251. * Starting at family 15h they are in family specific firmware files:
  252. *
  253. * amd-ucode/microcode_amd_fam15h.bin
  254. * amd-ucode/microcode_amd_fam16h.bin
  255. * ...
  256. *
  257. * These might be larger than 2K.
  258. */
  259. static enum ucode_state request_microcode_amd(int cpu, struct device *device)
  260. {
  261. char fw_name[36] = "amd-ucode/microcode_amd.bin";
  262. const struct firmware *fw;
  263. enum ucode_state ret = UCODE_NFOUND;
  264. struct cpuinfo_x86 *c = &cpu_data(cpu);
  265. if (c->x86 >= 0x15)
  266. snprintf(fw_name, sizeof(fw_name), "amd-ucode/microcode_amd_fam%.2xh.bin", c->x86);
  267. if (request_firmware(&fw, (const char *)fw_name, device)) {
  268. pr_err("failed to load file %s\n", fw_name);
  269. goto out;
  270. }
  271. ret = UCODE_ERROR;
  272. if (*(u32 *)fw->data != UCODE_MAGIC) {
  273. pr_err("invalid magic value (0x%08x)\n", *(u32 *)fw->data);
  274. goto fw_release;
  275. }
  276. ret = generic_load_microcode(cpu, fw->data, fw->size);
  277. fw_release:
  278. release_firmware(fw);
  279. out:
  280. return ret;
  281. }
  282. static enum ucode_state
  283. request_microcode_user(int cpu, const void __user *buf, size_t size)
  284. {
  285. pr_info("AMD microcode update via /dev/cpu/microcode not supported\n");
  286. return UCODE_ERROR;
  287. }
  288. static void microcode_fini_cpu_amd(int cpu)
  289. {
  290. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  291. vfree(uci->mc);
  292. uci->mc = NULL;
  293. }
  294. static struct microcode_ops microcode_amd_ops = {
  295. .request_microcode_user = request_microcode_user,
  296. .request_microcode_fw = request_microcode_amd,
  297. .collect_cpu_info = collect_cpu_info_amd,
  298. .apply_microcode = apply_microcode_amd,
  299. .microcode_fini_cpu = microcode_fini_cpu_amd,
  300. };
  301. struct microcode_ops * __init init_amd_microcode(void)
  302. {
  303. return &microcode_amd_ops;
  304. }