amd_early.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. 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)
  92. {
  93. struct equiv_cpu_entry *eq;
  94. size_t *cont_sz;
  95. u32 *header;
  96. u8 *data, **cont;
  97. u16 eq_id = 0;
  98. int offset, left;
  99. u32 rev, eax, ebx, ecx, edx;
  100. u32 *new_rev;
  101. #ifdef CONFIG_X86_32
  102. new_rev = (u32 *)__pa_nodebug(&ucode_new_rev);
  103. cont_sz = (size_t *)__pa_nodebug(&container_size);
  104. cont = (u8 **)__pa_nodebug(&container);
  105. #else
  106. new_rev = &ucode_new_rev;
  107. cont_sz = &container_size;
  108. cont = &container;
  109. #endif
  110. data = ucode;
  111. left = size;
  112. header = (u32 *)data;
  113. /* find equiv cpu table */
  114. if (header[0] != UCODE_MAGIC ||
  115. header[1] != UCODE_EQUIV_CPU_TABLE_TYPE || /* type */
  116. header[2] == 0) /* size */
  117. return;
  118. eax = 0x00000001;
  119. ecx = 0;
  120. native_cpuid(&eax, &ebx, &ecx, &edx);
  121. while (left > 0) {
  122. eq = (struct equiv_cpu_entry *)(data + CONTAINER_HDR_SZ);
  123. *cont = data;
  124. /* Advance past the container header */
  125. offset = header[2] + CONTAINER_HDR_SZ;
  126. data += offset;
  127. left -= offset;
  128. eq_id = find_equiv_id(eq, eax);
  129. if (eq_id) {
  130. this_equiv_id = eq_id;
  131. *cont_sz = compute_container_size(*cont, left + offset);
  132. /*
  133. * truncate how much we need to iterate over in the
  134. * ucode update loop below
  135. */
  136. left = *cont_sz - offset;
  137. break;
  138. }
  139. /*
  140. * support multiple container files appended together. if this
  141. * one does not have a matching equivalent cpu entry, we fast
  142. * forward to the next container file.
  143. */
  144. while (left > 0) {
  145. header = (u32 *)data;
  146. if (header[0] == UCODE_MAGIC &&
  147. header[1] == UCODE_EQUIV_CPU_TABLE_TYPE)
  148. break;
  149. offset = header[1] + SECTION_HDR_SIZE;
  150. data += offset;
  151. left -= offset;
  152. }
  153. /* mark where the next microcode container file starts */
  154. offset = data - (u8 *)ucode;
  155. ucode = data;
  156. }
  157. if (!eq_id) {
  158. *cont = NULL;
  159. *cont_sz = 0;
  160. return;
  161. }
  162. /* find ucode and update if needed */
  163. native_rdmsr(MSR_AMD64_PATCH_LEVEL, rev, eax);
  164. while (left > 0) {
  165. struct microcode_amd *mc;
  166. header = (u32 *)data;
  167. if (header[0] != UCODE_UCODE_TYPE || /* type */
  168. header[1] == 0) /* size */
  169. break;
  170. mc = (struct microcode_amd *)(data + SECTION_HDR_SIZE);
  171. if (eq_id == mc->hdr.processor_rev_id && rev < mc->hdr.patch_id) {
  172. if (!__apply_microcode_amd(mc)) {
  173. rev = mc->hdr.patch_id;
  174. *new_rev = rev;
  175. /* save ucode patch */
  176. memcpy(amd_ucode_patch, mc,
  177. min_t(u32, header[1], PATCH_MAX_SIZE));
  178. }
  179. }
  180. offset = header[1] + SECTION_HDR_SIZE;
  181. data += offset;
  182. left -= offset;
  183. }
  184. }
  185. void __init load_ucode_amd_bsp(void)
  186. {
  187. struct cpio_data cp;
  188. void **data;
  189. size_t *size;
  190. #ifdef CONFIG_X86_32
  191. data = (void **)__pa_nodebug(&ucode_cpio.data);
  192. size = (size_t *)__pa_nodebug(&ucode_cpio.size);
  193. #else
  194. data = &ucode_cpio.data;
  195. size = &ucode_cpio.size;
  196. #endif
  197. cp = find_ucode_in_initrd();
  198. if (!cp.data)
  199. return;
  200. *data = cp.data;
  201. *size = cp.size;
  202. apply_ucode_in_initrd(cp.data, cp.size);
  203. }
  204. #ifdef CONFIG_X86_32
  205. /*
  206. * On 32-bit, since AP's early load occurs before paging is turned on, we
  207. * cannot traverse cpu_equiv_table and pcache in kernel heap memory. So during
  208. * cold boot, AP will apply_ucode_in_initrd() just like the BSP. During
  209. * save_microcode_in_initrd_amd() BSP's patch is copied to amd_ucode_patch,
  210. * which is used upon resume from suspend.
  211. */
  212. void load_ucode_amd_ap(void)
  213. {
  214. struct microcode_amd *mc;
  215. size_t *usize;
  216. void **ucode;
  217. mc = (struct microcode_amd *)__pa(amd_ucode_patch);
  218. if (mc->hdr.patch_id && mc->hdr.processor_rev_id) {
  219. __apply_microcode_amd(mc);
  220. return;
  221. }
  222. ucode = (void *)__pa_nodebug(&container);
  223. usize = (size_t *)__pa_nodebug(&container_size);
  224. if (!*ucode || !*usize)
  225. return;
  226. apply_ucode_in_initrd(*ucode, *usize);
  227. }
  228. static void __init collect_cpu_sig_on_bsp(void *arg)
  229. {
  230. unsigned int cpu = smp_processor_id();
  231. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  232. uci->cpu_sig.sig = cpuid_eax(0x00000001);
  233. }
  234. static void __init get_bsp_sig(void)
  235. {
  236. unsigned int bsp = boot_cpu_data.cpu_index;
  237. struct ucode_cpu_info *uci = ucode_cpu_info + bsp;
  238. if (!uci->cpu_sig.sig)
  239. smp_call_function_single(bsp, collect_cpu_sig_on_bsp, NULL, 1);
  240. }
  241. #else
  242. void load_ucode_amd_ap(void)
  243. {
  244. unsigned int cpu = smp_processor_id();
  245. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  246. struct equiv_cpu_entry *eq;
  247. struct microcode_amd *mc;
  248. u32 rev, eax;
  249. u16 eq_id;
  250. /* Exit if called on the BSP. */
  251. if (!cpu)
  252. return;
  253. if (!container)
  254. return;
  255. rdmsr(MSR_AMD64_PATCH_LEVEL, rev, eax);
  256. uci->cpu_sig.rev = rev;
  257. uci->cpu_sig.sig = eax;
  258. eax = cpuid_eax(0x00000001);
  259. eq = (struct equiv_cpu_entry *)(container + CONTAINER_HDR_SZ);
  260. eq_id = find_equiv_id(eq, eax);
  261. if (!eq_id)
  262. return;
  263. if (eq_id == this_equiv_id) {
  264. mc = (struct microcode_amd *)amd_ucode_patch;
  265. if (mc && rev < mc->hdr.patch_id) {
  266. if (!__apply_microcode_amd(mc))
  267. ucode_new_rev = mc->hdr.patch_id;
  268. }
  269. } else {
  270. if (!ucode_cpio.data)
  271. return;
  272. /*
  273. * AP has a different equivalence ID than BSP, looks like
  274. * mixed-steppings silicon so go through the ucode blob anew.
  275. */
  276. apply_ucode_in_initrd(ucode_cpio.data, ucode_cpio.size);
  277. }
  278. }
  279. #endif
  280. int __init save_microcode_in_initrd_amd(void)
  281. {
  282. unsigned long cont;
  283. enum ucode_state ret;
  284. u32 eax;
  285. if (!container)
  286. return -EINVAL;
  287. #ifdef CONFIG_X86_32
  288. get_bsp_sig();
  289. cont = (unsigned long)container;
  290. #else
  291. /*
  292. * We need the physical address of the container for both bitness since
  293. * boot_params.hdr.ramdisk_image is a physical address.
  294. */
  295. cont = __pa(container);
  296. #endif
  297. /*
  298. * Take into account the fact that the ramdisk might get relocated and
  299. * therefore we need to recompute the container's position in virtual
  300. * memory space.
  301. */
  302. if (relocated_ramdisk)
  303. container = (u8 *)(__va(relocated_ramdisk) +
  304. (cont - boot_params.hdr.ramdisk_image));
  305. if (ucode_new_rev)
  306. pr_info("microcode: updated early to new patch_level=0x%08x\n",
  307. ucode_new_rev);
  308. eax = cpuid_eax(0x00000001);
  309. eax = ((eax >> 8) & 0xf) + ((eax >> 20) & 0xff);
  310. ret = load_microcode_amd(eax, container, container_size);
  311. if (ret != UCODE_OK)
  312. return -EINVAL;
  313. /*
  314. * This will be freed any msec now, stash patches for the current
  315. * family and switch to patch cache for cpu hotplug, etc later.
  316. */
  317. container = NULL;
  318. container_size = 0;
  319. return 0;
  320. }