microcode.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Copyright (c) 2014 Google, Inc
  3. * Copyright (C) 2000 Ronald G. Minnich
  4. *
  5. * Microcode update for Intel PIII and later CPUs
  6. *
  7. * SPDX-License-Identifier: GPL-2.0
  8. */
  9. #include <common.h>
  10. #include <errno.h>
  11. #include <fdtdec.h>
  12. #include <libfdt.h>
  13. #include <asm/cpu.h>
  14. #include <asm/microcode.h>
  15. #include <asm/msr.h>
  16. #include <asm/msr-index.h>
  17. #include <asm/processor.h>
  18. DECLARE_GLOBAL_DATA_PTR;
  19. /**
  20. * struct microcode_update - standard microcode header from Intel
  21. *
  22. * We read this information out of the device tree and use it to determine
  23. * whether the update is applicable or not. We also use the same structure
  24. * to read information from the CPU.
  25. */
  26. struct microcode_update {
  27. uint header_version;
  28. uint update_revision;
  29. uint date_code;
  30. uint processor_signature;
  31. uint checksum;
  32. uint loader_revision;
  33. uint processor_flags;
  34. const void *data;
  35. int size;
  36. };
  37. static int microcode_decode_node(const void *blob, int node,
  38. struct microcode_update *update)
  39. {
  40. update->data = fdt_getprop(blob, node, "data", &update->size);
  41. if (!update->data)
  42. return -ENOENT;
  43. update->data += UCODE_HEADER_LEN;
  44. update->size -= UCODE_HEADER_LEN;
  45. update->header_version = fdtdec_get_int(blob, node,
  46. "intel,header-version", 0);
  47. update->update_revision = fdtdec_get_int(blob, node,
  48. "intel,update-revision", 0);
  49. update->date_code = fdtdec_get_int(blob, node,
  50. "intel,date-code", 0);
  51. update->processor_signature = fdtdec_get_int(blob, node,
  52. "intel,processor-signature", 0);
  53. update->checksum = fdtdec_get_int(blob, node, "intel,checksum", 0);
  54. update->loader_revision = fdtdec_get_int(blob, node,
  55. "intel,loader-revision", 0);
  56. update->processor_flags = fdtdec_get_int(blob, node,
  57. "intel,processor-flags", 0);
  58. return 0;
  59. }
  60. int microcode_read_rev(void)
  61. {
  62. /* Quark does not have microcode MSRs */
  63. #ifdef CONFIG_INTEL_QUARK
  64. return 0;
  65. #else
  66. /*
  67. * Some Intel CPUs can be very finicky about the CPUID sequence used.
  68. * So this is implemented in assembly so that it works reliably.
  69. */
  70. uint32_t low, high;
  71. asm volatile (
  72. "xorl %%eax, %%eax\n"
  73. "xorl %%edx, %%edx\n"
  74. "movl %2, %%ecx\n"
  75. "wrmsr\n"
  76. "movl $0x01, %%eax\n"
  77. "cpuid\n"
  78. "movl %2, %%ecx\n"
  79. "rdmsr\n"
  80. : /* outputs */
  81. "=a" (low), "=d" (high)
  82. : /* inputs */
  83. "i" (MSR_IA32_UCODE_REV)
  84. : /* clobbers */
  85. "ebx", "ecx"
  86. );
  87. return high;
  88. #endif
  89. }
  90. static void microcode_read_cpu(struct microcode_update *cpu)
  91. {
  92. /* CPUID sets MSR 0x8B iff a microcode update has been loaded. */
  93. unsigned int x86_model, x86_family;
  94. struct cpuid_result result;
  95. uint32_t low, high;
  96. wrmsr(MSR_IA32_UCODE_REV, 0, 0);
  97. result = cpuid(1);
  98. rdmsr(MSR_IA32_UCODE_REV, low, cpu->update_revision);
  99. x86_model = (result.eax >> 4) & 0x0f;
  100. x86_family = (result.eax >> 8) & 0x0f;
  101. cpu->processor_signature = result.eax;
  102. cpu->processor_flags = 0;
  103. if ((x86_model >= 5) || (x86_family > 6)) {
  104. rdmsr(0x17, low, high);
  105. cpu->processor_flags = 1 << ((high >> 18) & 7);
  106. }
  107. debug("microcode: sig=%#x pf=%#x revision=%#x\n",
  108. cpu->processor_signature, cpu->processor_flags,
  109. cpu->update_revision);
  110. }
  111. /* Get a microcode update from the device tree and apply it */
  112. int microcode_update_intel(void)
  113. {
  114. struct microcode_update cpu, update;
  115. const void *blob = gd->fdt_blob;
  116. int skipped;
  117. int count;
  118. int node;
  119. int ret;
  120. int rev;
  121. microcode_read_cpu(&cpu);
  122. node = 0;
  123. count = 0;
  124. skipped = 0;
  125. do {
  126. node = fdtdec_next_compatible(blob, node,
  127. COMPAT_INTEL_MICROCODE);
  128. if (node < 0) {
  129. debug("%s: Found %d updates\n", __func__, count);
  130. return count ? 0 : skipped ? -EEXIST : -ENOENT;
  131. }
  132. ret = microcode_decode_node(blob, node, &update);
  133. if (ret == -ENOENT && ucode_base) {
  134. /*
  135. * The microcode has been removed from the device tree
  136. * in the build system. In that case it will have
  137. * already been updated in car_init().
  138. */
  139. debug("%s: Microcode data not available\n", __func__);
  140. skipped++;
  141. continue;
  142. }
  143. if (ret) {
  144. debug("%s: Unable to decode update: %d\n", __func__,
  145. ret);
  146. return ret;
  147. }
  148. if (!(update.processor_signature == cpu.processor_signature &&
  149. (update.processor_flags & cpu.processor_flags))) {
  150. debug("%s: Skipping non-matching update, sig=%x, pf=%x\n",
  151. __func__, update.processor_signature,
  152. update.processor_flags);
  153. skipped++;
  154. continue;
  155. }
  156. wrmsr(MSR_IA32_UCODE_WRITE, (ulong)update.data, 0);
  157. rev = microcode_read_rev();
  158. debug("microcode: updated to revision 0x%x date=%04x-%02x-%02x\n",
  159. rev, update.date_code & 0xffff,
  160. (update.date_code >> 24) & 0xff,
  161. (update.date_code >> 16) & 0xff);
  162. if (update.update_revision != rev) {
  163. printf("Microcode update failed\n");
  164. return -EFAULT;
  165. }
  166. count++;
  167. } while (1);
  168. }