microcode_amd_early.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * Copyright (C) 2013 Advanced Micro Devices, Inc.
  3. *
  4. * Author: Jacob Shin <jacob.shin@amd.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/earlycpio.h>
  11. #include <asm/cpu.h>
  12. #include <asm/setup.h>
  13. #include <asm/microcode_amd.h>
  14. static bool ucode_loaded;
  15. static u32 ucode_new_rev;
  16. /*
  17. * Microcode patch container file is prepended to the initrd in cpio format.
  18. * See Documentation/x86/early-microcode.txt
  19. */
  20. static __cpuinitdata char ucode_path[] = "kernel/x86/microcode/AuthenticAMD.bin";
  21. static struct cpio_data __cpuinit find_ucode_in_initrd(void)
  22. {
  23. long offset = 0;
  24. struct cpio_data cd;
  25. #ifdef CONFIG_X86_32
  26. /*
  27. * On 32-bit, early load occurs before paging is turned on so we need
  28. * to use physical addresses.
  29. */
  30. if (!(read_cr0() & X86_CR0_PG)) {
  31. struct boot_params *p;
  32. p = (struct boot_params *)__pa_nodebug(&boot_params);
  33. cd = find_cpio_data((char *)__pa_nodebug(ucode_path),
  34. (void *)p->hdr.ramdisk_image, p->hdr.ramdisk_size,
  35. &offset);
  36. } else
  37. #endif
  38. cd = find_cpio_data(ucode_path,
  39. (void *)(boot_params.hdr.ramdisk_image + PAGE_OFFSET),
  40. boot_params.hdr.ramdisk_size, &offset);
  41. if (*(u32 *)cd.data != UCODE_MAGIC) {
  42. cd.data = NULL;
  43. cd.size = 0;
  44. }
  45. return cd;
  46. }
  47. /*
  48. * Early load occurs before we can vmalloc(). So we look for the microcode
  49. * patch container file in initrd, traverse equivalent cpu table, look for a
  50. * matching microcode patch, and update, all in initrd memory in place.
  51. * When vmalloc() is available for use later -- on 64-bit during first AP load,
  52. * and on 32-bit during save_microcode_in_initrd_amd() -- we can call
  53. * load_microcode_amd() to save equivalent cpu table and microcode patches in
  54. * kernel heap memory.
  55. */
  56. static void __cpuinit apply_ucode_in_initrd(void)
  57. {
  58. struct cpio_data cd;
  59. struct equiv_cpu_entry *eq;
  60. u32 *header;
  61. u8 *data;
  62. u16 eq_id;
  63. int offset, left;
  64. u32 rev, dummy;
  65. u32 *new_rev;
  66. #ifdef CONFIG_X86_32
  67. new_rev = (u32 *)__pa_nodebug(&ucode_new_rev);
  68. #else
  69. new_rev = &ucode_new_rev;
  70. #endif
  71. cd = find_ucode_in_initrd();
  72. if (!cd.data)
  73. return;
  74. data = cd.data;
  75. left = cd.size;
  76. header = (u32 *)data;
  77. /* find equiv cpu table */
  78. if (header[1] != UCODE_EQUIV_CPU_TABLE_TYPE || /* type */
  79. header[2] == 0) /* size */
  80. return;
  81. eq = (struct equiv_cpu_entry *)(data + CONTAINER_HDR_SZ);
  82. offset = header[2] + CONTAINER_HDR_SZ;
  83. data += offset;
  84. left -= offset;
  85. eq_id = find_equiv_id(eq, cpuid_eax(0x00000001));
  86. if (!eq_id)
  87. return;
  88. /* find ucode and update if needed */
  89. rdmsr(MSR_AMD64_PATCH_LEVEL, rev, dummy);
  90. while (left > 0) {
  91. struct microcode_amd *mc;
  92. header = (u32 *)data;
  93. if (header[0] != UCODE_UCODE_TYPE || /* type */
  94. header[1] == 0) /* size */
  95. break;
  96. mc = (struct microcode_amd *)(data + SECTION_HDR_SIZE);
  97. if (eq_id == mc->hdr.processor_rev_id && rev < mc->hdr.patch_id)
  98. if (__apply_microcode_amd(mc) == 0) {
  99. if (!(*new_rev))
  100. *new_rev = mc->hdr.patch_id;
  101. break;
  102. }
  103. offset = header[1] + SECTION_HDR_SIZE;
  104. data += offset;
  105. left -= offset;
  106. }
  107. }
  108. void __init load_ucode_amd_bsp(void)
  109. {
  110. apply_ucode_in_initrd();
  111. }
  112. #ifdef CONFIG_X86_32
  113. u8 amd_bsp_mpb[MPB_MAX_SIZE];
  114. /*
  115. * On 32-bit, since AP's early load occurs before paging is turned on, we
  116. * cannot traverse cpu_equiv_table and pcache in kernel heap memory. So during
  117. * cold boot, AP will apply_ucode_in_initrd() just like the BSP. During
  118. * save_microcode_in_initrd_amd() BSP's patch is copied to amd_bsp_mpb, which
  119. * is used upon resume from suspend.
  120. */
  121. void __cpuinit load_ucode_amd_ap(void)
  122. {
  123. struct microcode_amd *mc;
  124. mc = (struct microcode_amd *)__pa_nodebug(amd_bsp_mpb);
  125. if (mc->hdr.patch_id && mc->hdr.processor_rev_id)
  126. __apply_microcode_amd(mc);
  127. else
  128. apply_ucode_in_initrd();
  129. }
  130. static void __init collect_cpu_sig_on_bsp(void *arg)
  131. {
  132. unsigned int cpu = smp_processor_id();
  133. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  134. uci->cpu_sig.sig = cpuid_eax(0x00000001);
  135. }
  136. #else
  137. static void __cpuinit collect_cpu_info_amd_early(struct cpuinfo_x86 *c,
  138. struct ucode_cpu_info *uci)
  139. {
  140. u32 rev, eax;
  141. rdmsr(MSR_AMD64_PATCH_LEVEL, rev, eax);
  142. eax = cpuid_eax(0x00000001);
  143. uci->cpu_sig.sig = eax;
  144. uci->cpu_sig.rev = rev;
  145. c->microcode = rev;
  146. c->x86 = ((eax >> 8) & 0xf) + ((eax >> 20) & 0xff);
  147. }
  148. void __cpuinit load_ucode_amd_ap(void)
  149. {
  150. unsigned int cpu = smp_processor_id();
  151. collect_cpu_info_amd_early(&cpu_data(cpu), ucode_cpu_info + cpu);
  152. if (cpu && !ucode_loaded) {
  153. struct cpio_data cd = find_ucode_in_initrd();
  154. if (load_microcode_amd(0, cd.data, cd.size) != UCODE_OK)
  155. return;
  156. ucode_loaded = true;
  157. }
  158. apply_microcode_amd(cpu);
  159. }
  160. #endif
  161. int __init save_microcode_in_initrd_amd(void)
  162. {
  163. enum ucode_state ret;
  164. struct cpio_data cd;
  165. #ifdef CONFIG_X86_32
  166. unsigned int bsp = boot_cpu_data.cpu_index;
  167. struct ucode_cpu_info *uci = ucode_cpu_info + bsp;
  168. if (!uci->cpu_sig.sig)
  169. smp_call_function_single(bsp, collect_cpu_sig_on_bsp, NULL, 1);
  170. #endif
  171. if (ucode_new_rev)
  172. pr_info("microcode: updated early to new patch_level=0x%08x\n",
  173. ucode_new_rev);
  174. if (ucode_loaded)
  175. return 0;
  176. cd = find_ucode_in_initrd();
  177. if (!cd.data)
  178. return -EINVAL;
  179. ret = load_microcode_amd(0, cd.data, cd.size);
  180. if (ret != UCODE_OK)
  181. return -EINVAL;
  182. ucode_loaded = true;
  183. return 0;
  184. }