amd_early.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /*
  2. * Copyright (C) 2013 Advanced Micro Devices, Inc.
  3. *
  4. * Author: Jacob Shin <jacob.shin@amd.com>
  5. * Fixes: Borislav Petkov <bp@suse.de>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/earlycpio.h>
  12. #include <linux/initrd.h>
  13. #include <asm/cpu.h>
  14. #include <asm/setup.h>
  15. #include <asm/microcode_amd.h>
  16. /*
  17. * This points to the current valid container of microcode patches which we will
  18. * save from the initrd before jettisoning its contents.
  19. */
  20. static u8 *container;
  21. static size_t container_size;
  22. static u32 ucode_new_rev;
  23. u8 amd_ucode_patch[PATCH_MAX_SIZE];
  24. static u16 this_equiv_id;
  25. static struct cpio_data ucode_cpio;
  26. /*
  27. * Microcode patch container file is prepended to the initrd in cpio format.
  28. * See Documentation/x86/early-microcode.txt
  29. */
  30. static __initdata char ucode_path[] = "kernel/x86/microcode/AuthenticAMD.bin";
  31. static struct cpio_data __init find_ucode_in_initrd(void)
  32. {
  33. long offset = 0;
  34. char *path;
  35. void *start;
  36. size_t size;
  37. #ifdef CONFIG_X86_32
  38. struct boot_params *p;
  39. /*
  40. * On 32-bit, early load occurs before paging is turned on so we need
  41. * to use physical addresses.
  42. */
  43. p = (struct boot_params *)__pa_nodebug(&boot_params);
  44. path = (char *)__pa_nodebug(ucode_path);
  45. start = (void *)p->hdr.ramdisk_image;
  46. size = p->hdr.ramdisk_size;
  47. #else
  48. path = ucode_path;
  49. start = (void *)(boot_params.hdr.ramdisk_image + PAGE_OFFSET);
  50. size = boot_params.hdr.ramdisk_size;
  51. #endif
  52. return find_cpio_data(path, start, size, &offset);
  53. }
  54. static size_t compute_container_size(u8 *data, u32 total_size)
  55. {
  56. size_t size = 0;
  57. u32 *header = (u32 *)data;
  58. if (header[0] != UCODE_MAGIC ||
  59. header[1] != UCODE_EQUIV_CPU_TABLE_TYPE || /* type */
  60. header[2] == 0) /* size */
  61. return size;
  62. size = header[2] + CONTAINER_HDR_SZ;
  63. total_size -= size;
  64. data += size;
  65. while (total_size) {
  66. u16 patch_size;
  67. header = (u32 *)data;
  68. if (header[0] != UCODE_UCODE_TYPE)
  69. break;
  70. /*
  71. * Sanity-check patch size.
  72. */
  73. patch_size = header[1];
  74. if (patch_size > PATCH_MAX_SIZE)
  75. break;
  76. size += patch_size + SECTION_HDR_SIZE;
  77. data += patch_size + SECTION_HDR_SIZE;
  78. total_size -= patch_size + SECTION_HDR_SIZE;
  79. }
  80. return size;
  81. }
  82. /*
  83. * Early load occurs before we can vmalloc(). So we look for the microcode
  84. * patch container file in initrd, traverse equivalent cpu table, look for a
  85. * matching microcode patch, and update, all in initrd memory in place.
  86. * When vmalloc() is available for use later -- on 64-bit during first AP load,
  87. * and on 32-bit during save_microcode_in_initrd_amd() -- we can call
  88. * load_microcode_amd() to save equivalent cpu table and microcode patches in
  89. * kernel heap memory.
  90. */
  91. static void apply_ucode_in_initrd(void *ucode, size_t size, bool save_patch)
  92. {
  93. struct equiv_cpu_entry *eq;
  94. size_t *cont_sz;
  95. u32 *header;
  96. u8 *data, **cont;
  97. u8 (*patch)[PATCH_MAX_SIZE];
  98. u16 eq_id = 0;
  99. int offset, left;
  100. u32 rev, eax, ebx, ecx, edx;
  101. u32 *new_rev;
  102. #ifdef CONFIG_X86_32
  103. new_rev = (u32 *)__pa_nodebug(&ucode_new_rev);
  104. cont_sz = (size_t *)__pa_nodebug(&container_size);
  105. cont = (u8 **)__pa_nodebug(&container);
  106. patch = (u8 (*)[PATCH_MAX_SIZE])__pa_nodebug(&amd_ucode_patch);
  107. #else
  108. new_rev = &ucode_new_rev;
  109. cont_sz = &container_size;
  110. cont = &container;
  111. patch = &amd_ucode_patch;
  112. #endif
  113. data = ucode;
  114. left = size;
  115. header = (u32 *)data;
  116. /* find equiv cpu table */
  117. if (header[0] != UCODE_MAGIC ||
  118. header[1] != UCODE_EQUIV_CPU_TABLE_TYPE || /* type */
  119. header[2] == 0) /* size */
  120. return;
  121. eax = 0x00000001;
  122. ecx = 0;
  123. native_cpuid(&eax, &ebx, &ecx, &edx);
  124. while (left > 0) {
  125. eq = (struct equiv_cpu_entry *)(data + CONTAINER_HDR_SZ);
  126. *cont = data;
  127. /* Advance past the container header */
  128. offset = header[2] + CONTAINER_HDR_SZ;
  129. data += offset;
  130. left -= offset;
  131. eq_id = find_equiv_id(eq, eax);
  132. if (eq_id) {
  133. this_equiv_id = eq_id;
  134. *cont_sz = compute_container_size(*cont, left + offset);
  135. /*
  136. * truncate how much we need to iterate over in the
  137. * ucode update loop below
  138. */
  139. left = *cont_sz - offset;
  140. break;
  141. }
  142. /*
  143. * support multiple container files appended together. if this
  144. * one does not have a matching equivalent cpu entry, we fast
  145. * forward to the next container file.
  146. */
  147. while (left > 0) {
  148. header = (u32 *)data;
  149. if (header[0] == UCODE_MAGIC &&
  150. header[1] == UCODE_EQUIV_CPU_TABLE_TYPE)
  151. break;
  152. offset = header[1] + SECTION_HDR_SIZE;
  153. data += offset;
  154. left -= offset;
  155. }
  156. /* mark where the next microcode container file starts */
  157. offset = data - (u8 *)ucode;
  158. ucode = data;
  159. }
  160. if (!eq_id) {
  161. *cont = NULL;
  162. *cont_sz = 0;
  163. return;
  164. }
  165. /* find ucode and update if needed */
  166. native_rdmsr(MSR_AMD64_PATCH_LEVEL, rev, eax);
  167. while (left > 0) {
  168. struct microcode_amd *mc;
  169. header = (u32 *)data;
  170. if (header[0] != UCODE_UCODE_TYPE || /* type */
  171. header[1] == 0) /* size */
  172. break;
  173. mc = (struct microcode_amd *)(data + SECTION_HDR_SIZE);
  174. if (eq_id == mc->hdr.processor_rev_id && rev < mc->hdr.patch_id) {
  175. if (!__apply_microcode_amd(mc)) {
  176. rev = mc->hdr.patch_id;
  177. *new_rev = rev;
  178. if (save_patch)
  179. memcpy(patch, mc,
  180. min_t(u32, header[1], PATCH_MAX_SIZE));
  181. }
  182. }
  183. offset = header[1] + SECTION_HDR_SIZE;
  184. data += offset;
  185. left -= offset;
  186. }
  187. }
  188. static bool __init load_builtin_amd_microcode(struct cpio_data *cp,
  189. unsigned int family)
  190. {
  191. #ifdef CONFIG_X86_64
  192. char fw_name[36] = "amd-ucode/microcode_amd.bin";
  193. if (family >= 0x15)
  194. snprintf(fw_name, sizeof(fw_name),
  195. "amd-ucode/microcode_amd_fam%.2xh.bin", family);
  196. return get_builtin_firmware(cp, fw_name);
  197. #else
  198. return false;
  199. #endif
  200. }
  201. void __init load_ucode_amd_bsp(unsigned int family)
  202. {
  203. struct cpio_data cp;
  204. void **data;
  205. size_t *size;
  206. #ifdef CONFIG_X86_32
  207. data = (void **)__pa_nodebug(&ucode_cpio.data);
  208. size = (size_t *)__pa_nodebug(&ucode_cpio.size);
  209. #else
  210. data = &ucode_cpio.data;
  211. size = &ucode_cpio.size;
  212. #endif
  213. cp = find_ucode_in_initrd();
  214. if (!cp.data) {
  215. if (!load_builtin_amd_microcode(&cp, family))
  216. return;
  217. }
  218. *data = cp.data;
  219. *size = cp.size;
  220. apply_ucode_in_initrd(cp.data, cp.size, true);
  221. }
  222. #ifdef CONFIG_X86_32
  223. /*
  224. * On 32-bit, since AP's early load occurs before paging is turned on, we
  225. * cannot traverse cpu_equiv_table and pcache in kernel heap memory. So during
  226. * cold boot, AP will apply_ucode_in_initrd() just like the BSP. During
  227. * save_microcode_in_initrd_amd() BSP's patch is copied to amd_ucode_patch,
  228. * which is used upon resume from suspend.
  229. */
  230. void load_ucode_amd_ap(void)
  231. {
  232. struct microcode_amd *mc;
  233. size_t *usize;
  234. void **ucode;
  235. mc = (struct microcode_amd *)__pa_nodebug(amd_ucode_patch);
  236. if (mc->hdr.patch_id && mc->hdr.processor_rev_id) {
  237. __apply_microcode_amd(mc);
  238. return;
  239. }
  240. ucode = (void *)__pa_nodebug(&container);
  241. usize = (size_t *)__pa_nodebug(&container_size);
  242. if (!*ucode || !*usize)
  243. return;
  244. apply_ucode_in_initrd(*ucode, *usize, false);
  245. }
  246. static void __init collect_cpu_sig_on_bsp(void *arg)
  247. {
  248. unsigned int cpu = smp_processor_id();
  249. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  250. uci->cpu_sig.sig = cpuid_eax(0x00000001);
  251. }
  252. static void __init get_bsp_sig(void)
  253. {
  254. unsigned int bsp = boot_cpu_data.cpu_index;
  255. struct ucode_cpu_info *uci = ucode_cpu_info + bsp;
  256. if (!uci->cpu_sig.sig)
  257. smp_call_function_single(bsp, collect_cpu_sig_on_bsp, NULL, 1);
  258. }
  259. #else
  260. void load_ucode_amd_ap(void)
  261. {
  262. unsigned int cpu = smp_processor_id();
  263. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  264. struct equiv_cpu_entry *eq;
  265. struct microcode_amd *mc;
  266. u32 rev, eax;
  267. u16 eq_id;
  268. /* Exit if called on the BSP. */
  269. if (!cpu)
  270. return;
  271. if (!container)
  272. return;
  273. rdmsr(MSR_AMD64_PATCH_LEVEL, rev, eax);
  274. uci->cpu_sig.rev = rev;
  275. uci->cpu_sig.sig = eax;
  276. eax = cpuid_eax(0x00000001);
  277. eq = (struct equiv_cpu_entry *)(container + CONTAINER_HDR_SZ);
  278. eq_id = find_equiv_id(eq, eax);
  279. if (!eq_id)
  280. return;
  281. if (eq_id == this_equiv_id) {
  282. mc = (struct microcode_amd *)amd_ucode_patch;
  283. if (mc && rev < mc->hdr.patch_id) {
  284. if (!__apply_microcode_amd(mc))
  285. ucode_new_rev = mc->hdr.patch_id;
  286. }
  287. } else {
  288. if (!ucode_cpio.data)
  289. return;
  290. /*
  291. * AP has a different equivalence ID than BSP, looks like
  292. * mixed-steppings silicon so go through the ucode blob anew.
  293. */
  294. apply_ucode_in_initrd(ucode_cpio.data, ucode_cpio.size, false);
  295. }
  296. }
  297. #endif
  298. int __init save_microcode_in_initrd_amd(void)
  299. {
  300. unsigned long cont;
  301. int retval = 0;
  302. enum ucode_state ret;
  303. u8 *cont_va;
  304. u32 eax;
  305. if (!container)
  306. return -EINVAL;
  307. #ifdef CONFIG_X86_32
  308. get_bsp_sig();
  309. cont = (unsigned long)container;
  310. cont_va = __va(container);
  311. #else
  312. /*
  313. * We need the physical address of the container for both bitness since
  314. * boot_params.hdr.ramdisk_image is a physical address.
  315. */
  316. cont = __pa(container);
  317. cont_va = container;
  318. #endif
  319. /*
  320. * Take into account the fact that the ramdisk might get relocated and
  321. * therefore we need to recompute the container's position in virtual
  322. * memory space.
  323. */
  324. if (relocated_ramdisk)
  325. container = (u8 *)(__va(relocated_ramdisk) +
  326. (cont - boot_params.hdr.ramdisk_image));
  327. else
  328. container = cont_va;
  329. if (ucode_new_rev)
  330. pr_info("microcode: updated early to new patch_level=0x%08x\n",
  331. ucode_new_rev);
  332. eax = cpuid_eax(0x00000001);
  333. eax = ((eax >> 8) & 0xf) + ((eax >> 20) & 0xff);
  334. ret = load_microcode_amd(smp_processor_id(), eax, container, container_size);
  335. if (ret != UCODE_OK)
  336. retval = -EINVAL;
  337. /*
  338. * This will be freed any msec now, stash patches for the current
  339. * family and switch to patch cache for cpu hotplug, etc later.
  340. */
  341. container = NULL;
  342. container_size = 0;
  343. return retval;
  344. }
  345. void reload_ucode_amd(void)
  346. {
  347. struct microcode_amd *mc;
  348. u32 rev, eax;
  349. rdmsr(MSR_AMD64_PATCH_LEVEL, rev, eax);
  350. mc = (struct microcode_amd *)amd_ucode_patch;
  351. if (mc && rev < mc->hdr.patch_id) {
  352. if (!__apply_microcode_amd(mc)) {
  353. ucode_new_rev = mc->hdr.patch_id;
  354. pr_info("microcode: reload patch_level=0x%08x\n",
  355. ucode_new_rev);
  356. }
  357. }
  358. }