core_early.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * X86 CPU microcode early update for Linux
  3. *
  4. * Copyright (C) 2012 Fenghua Yu <fenghua.yu@intel.com>
  5. * H Peter Anvin" <hpa@zytor.com>
  6. *
  7. * This driver allows to early upgrade microcode on Intel processors
  8. * belonging to IA-32 family - PentiumPro, Pentium II,
  9. * Pentium III, Xeon, Pentium 4, etc.
  10. *
  11. * Reference: Section 9.11 of Volume 3, IA-32 Intel Architecture
  12. * Software Developer's Manual.
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License
  16. * as published by the Free Software Foundation; either version
  17. * 2 of the License, or (at your option) any later version.
  18. */
  19. #include <linux/module.h>
  20. #include <asm/microcode.h>
  21. #include <asm/microcode_intel.h>
  22. #include <asm/microcode_amd.h>
  23. #include <asm/processor.h>
  24. #include <asm/cmdline.h>
  25. #define QCHAR(a, b, c, d) ((a) + ((b) << 8) + ((c) << 16) + ((d) << 24))
  26. #define CPUID_INTEL1 QCHAR('G', 'e', 'n', 'u')
  27. #define CPUID_INTEL2 QCHAR('i', 'n', 'e', 'I')
  28. #define CPUID_INTEL3 QCHAR('n', 't', 'e', 'l')
  29. #define CPUID_AMD1 QCHAR('A', 'u', 't', 'h')
  30. #define CPUID_AMD2 QCHAR('e', 'n', 't', 'i')
  31. #define CPUID_AMD3 QCHAR('c', 'A', 'M', 'D')
  32. #define CPUID_IS(a, b, c, ebx, ecx, edx) \
  33. (!((ebx ^ (a))|(edx ^ (b))|(ecx ^ (c))))
  34. /*
  35. * In early loading microcode phase on BSP, boot_cpu_data is not set up yet.
  36. * x86_vendor() gets vendor id for BSP.
  37. *
  38. * In 32 bit AP case, accessing boot_cpu_data needs linear address. To simplify
  39. * coding, we still use x86_vendor() to get vendor id for AP.
  40. *
  41. * x86_vendor() gets vendor information directly through cpuid.
  42. */
  43. static int x86_vendor(void)
  44. {
  45. u32 eax = 0x00000000;
  46. u32 ebx, ecx = 0, edx;
  47. native_cpuid(&eax, &ebx, &ecx, &edx);
  48. if (CPUID_IS(CPUID_INTEL1, CPUID_INTEL2, CPUID_INTEL3, ebx, ecx, edx))
  49. return X86_VENDOR_INTEL;
  50. if (CPUID_IS(CPUID_AMD1, CPUID_AMD2, CPUID_AMD3, ebx, ecx, edx))
  51. return X86_VENDOR_AMD;
  52. return X86_VENDOR_UNKNOWN;
  53. }
  54. static int x86_family(void)
  55. {
  56. u32 eax = 0x00000001;
  57. u32 ebx, ecx = 0, edx;
  58. int x86;
  59. native_cpuid(&eax, &ebx, &ecx, &edx);
  60. x86 = (eax >> 8) & 0xf;
  61. if (x86 == 15)
  62. x86 += (eax >> 20) & 0xff;
  63. return x86;
  64. }
  65. static bool __init check_loader_disabled_bsp(void)
  66. {
  67. #ifdef CONFIG_X86_32
  68. const char *cmdline = (const char *)__pa_nodebug(boot_command_line);
  69. const char *opt = "dis_ucode_ldr";
  70. const char *option = (const char *)__pa_nodebug(opt);
  71. bool *res = (bool *)__pa_nodebug(&dis_ucode_ldr);
  72. #else /* CONFIG_X86_64 */
  73. const char *cmdline = boot_command_line;
  74. const char *option = "dis_ucode_ldr";
  75. bool *res = &dis_ucode_ldr;
  76. #endif
  77. if (cmdline_find_option_bool(cmdline, option))
  78. *res = true;
  79. return *res;
  80. }
  81. void __init load_ucode_bsp(void)
  82. {
  83. int vendor, x86;
  84. if (check_loader_disabled_bsp())
  85. return;
  86. if (!have_cpuid_p())
  87. return;
  88. vendor = x86_vendor();
  89. x86 = x86_family();
  90. switch (vendor) {
  91. case X86_VENDOR_INTEL:
  92. if (x86 >= 6)
  93. load_ucode_intel_bsp();
  94. break;
  95. case X86_VENDOR_AMD:
  96. if (x86 >= 0x10)
  97. load_ucode_amd_bsp();
  98. break;
  99. default:
  100. break;
  101. }
  102. }
  103. static bool check_loader_disabled_ap(void)
  104. {
  105. #ifdef CONFIG_X86_32
  106. return *((bool *)__pa_nodebug(&dis_ucode_ldr));
  107. #else
  108. return dis_ucode_ldr;
  109. #endif
  110. }
  111. void load_ucode_ap(void)
  112. {
  113. int vendor, x86;
  114. if (check_loader_disabled_ap())
  115. return;
  116. if (!have_cpuid_p())
  117. return;
  118. vendor = x86_vendor();
  119. x86 = x86_family();
  120. switch (vendor) {
  121. case X86_VENDOR_INTEL:
  122. if (x86 >= 6)
  123. load_ucode_intel_ap();
  124. break;
  125. case X86_VENDOR_AMD:
  126. if (x86 >= 0x10)
  127. load_ucode_amd_ap();
  128. break;
  129. default:
  130. break;
  131. }
  132. }
  133. int __init save_microcode_in_initrd(void)
  134. {
  135. struct cpuinfo_x86 *c = &boot_cpu_data;
  136. switch (c->x86_vendor) {
  137. case X86_VENDOR_INTEL:
  138. if (c->x86 >= 6)
  139. save_microcode_in_initrd_intel();
  140. break;
  141. case X86_VENDOR_AMD:
  142. if (c->x86 >= 0x10)
  143. save_microcode_in_initrd_amd();
  144. break;
  145. default:
  146. break;
  147. }
  148. return 0;
  149. }
  150. void reload_early_microcode(void)
  151. {
  152. int vendor, x86;
  153. vendor = x86_vendor();
  154. x86 = x86_family();
  155. switch (vendor) {
  156. case X86_VENDOR_INTEL:
  157. if (x86 >= 6)
  158. reload_ucode_intel();
  159. break;
  160. case X86_VENDOR_AMD:
  161. if (x86 >= 0x10)
  162. reload_ucode_amd();
  163. break;
  164. default:
  165. break;
  166. }
  167. }