amd_early.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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. void __init load_ucode_amd_bsp(void)
  189. {
  190. struct cpio_data cp;
  191. void **data;
  192. size_t *size;
  193. #ifdef CONFIG_X86_32
  194. data = (void **)__pa_nodebug(&ucode_cpio.data);
  195. size = (size_t *)__pa_nodebug(&ucode_cpio.size);
  196. #else
  197. data = &ucode_cpio.data;
  198. size = &ucode_cpio.size;
  199. #endif
  200. cp = find_ucode_in_initrd();
  201. if (!cp.data)
  202. return;
  203. *data = cp.data;
  204. *size = cp.size;
  205. apply_ucode_in_initrd(cp.data, cp.size, true);
  206. }
  207. #ifdef CONFIG_X86_32
  208. /*
  209. * On 32-bit, since AP's early load occurs before paging is turned on, we
  210. * cannot traverse cpu_equiv_table and pcache in kernel heap memory. So during
  211. * cold boot, AP will apply_ucode_in_initrd() just like the BSP. During
  212. * save_microcode_in_initrd_amd() BSP's patch is copied to amd_ucode_patch,
  213. * which is used upon resume from suspend.
  214. */
  215. void load_ucode_amd_ap(void)
  216. {
  217. struct microcode_amd *mc;
  218. size_t *usize;
  219. void **ucode;
  220. mc = (struct microcode_amd *)__pa_nodebug(amd_ucode_patch);
  221. if (mc->hdr.patch_id && mc->hdr.processor_rev_id) {
  222. __apply_microcode_amd(mc);
  223. return;
  224. }
  225. ucode = (void *)__pa_nodebug(&container);
  226. usize = (size_t *)__pa_nodebug(&container_size);
  227. if (!*ucode || !*usize)
  228. return;
  229. apply_ucode_in_initrd(*ucode, *usize, false);
  230. }
  231. static void __init collect_cpu_sig_on_bsp(void *arg)
  232. {
  233. unsigned int cpu = smp_processor_id();
  234. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  235. uci->cpu_sig.sig = cpuid_eax(0x00000001);
  236. }
  237. static void __init get_bsp_sig(void)
  238. {
  239. unsigned int bsp = boot_cpu_data.cpu_index;
  240. struct ucode_cpu_info *uci = ucode_cpu_info + bsp;
  241. if (!uci->cpu_sig.sig)
  242. smp_call_function_single(bsp, collect_cpu_sig_on_bsp, NULL, 1);
  243. }
  244. #else
  245. void load_ucode_amd_ap(void)
  246. {
  247. unsigned int cpu = smp_processor_id();
  248. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  249. struct equiv_cpu_entry *eq;
  250. struct microcode_amd *mc;
  251. u32 rev, eax;
  252. u16 eq_id;
  253. /* Exit if called on the BSP. */
  254. if (!cpu)
  255. return;
  256. if (!container)
  257. return;
  258. rdmsr(MSR_AMD64_PATCH_LEVEL, rev, eax);
  259. uci->cpu_sig.rev = rev;
  260. uci->cpu_sig.sig = eax;
  261. eax = cpuid_eax(0x00000001);
  262. eq = (struct equiv_cpu_entry *)(container + CONTAINER_HDR_SZ);
  263. eq_id = find_equiv_id(eq, eax);
  264. if (!eq_id)
  265. return;
  266. if (eq_id == this_equiv_id) {
  267. mc = (struct microcode_amd *)amd_ucode_patch;
  268. if (mc && rev < mc->hdr.patch_id) {
  269. if (!__apply_microcode_amd(mc))
  270. ucode_new_rev = mc->hdr.patch_id;
  271. }
  272. } else {
  273. if (!ucode_cpio.data)
  274. return;
  275. /*
  276. * AP has a different equivalence ID than BSP, looks like
  277. * mixed-steppings silicon so go through the ucode blob anew.
  278. */
  279. apply_ucode_in_initrd(ucode_cpio.data, ucode_cpio.size, false);
  280. }
  281. }
  282. #endif
  283. int __init save_microcode_in_initrd_amd(void)
  284. {
  285. unsigned long cont;
  286. int retval = 0;
  287. enum ucode_state ret;
  288. u8 *cont_va;
  289. u32 eax;
  290. if (!container)
  291. return -EINVAL;
  292. #ifdef CONFIG_X86_32
  293. get_bsp_sig();
  294. cont = (unsigned long)container;
  295. cont_va = __va(container);
  296. #else
  297. /*
  298. * We need the physical address of the container for both bitness since
  299. * boot_params.hdr.ramdisk_image is a physical address.
  300. */
  301. cont = __pa(container);
  302. cont_va = container;
  303. #endif
  304. /*
  305. * Take into account the fact that the ramdisk might get relocated and
  306. * therefore we need to recompute the container's position in virtual
  307. * memory space.
  308. */
  309. if (relocated_ramdisk)
  310. container = (u8 *)(__va(relocated_ramdisk) +
  311. (cont - boot_params.hdr.ramdisk_image));
  312. else
  313. container = cont_va;
  314. if (ucode_new_rev)
  315. pr_info("microcode: updated early to new patch_level=0x%08x\n",
  316. ucode_new_rev);
  317. eax = cpuid_eax(0x00000001);
  318. eax = ((eax >> 8) & 0xf) + ((eax >> 20) & 0xff);
  319. ret = load_microcode_amd(smp_processor_id(), eax, container, container_size);
  320. if (ret != UCODE_OK)
  321. retval = -EINVAL;
  322. /*
  323. * This will be freed any msec now, stash patches for the current
  324. * family and switch to patch cache for cpu hotplug, etc later.
  325. */
  326. container = NULL;
  327. container_size = 0;
  328. return retval;
  329. }
  330. void reload_ucode_amd(void)
  331. {
  332. struct microcode_amd *mc;
  333. u32 rev, eax;
  334. rdmsr(MSR_AMD64_PATCH_LEVEL, rev, eax);
  335. mc = (struct microcode_amd *)amd_ucode_patch;
  336. if (mc && rev < mc->hdr.patch_id) {
  337. if (!__apply_microcode_amd(mc)) {
  338. ucode_new_rev = mc->hdr.patch_id;
  339. pr_info("microcode: reload patch_level=0x%08x\n",
  340. ucode_new_rev);
  341. }
  342. }
  343. }