microcode_intel.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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/msr.h>
  15. #include <asm/processor.h>
  16. /**
  17. * struct microcode_update - standard microcode header from Intel
  18. *
  19. * We read this information out of the device tree and use it to determine
  20. * whether the update is applicable or not. We also use the same structure
  21. * to read information from the CPU.
  22. */
  23. struct microcode_update {
  24. uint header_version;
  25. uint update_revision;
  26. uint date_code;
  27. uint processor_signature;
  28. uint checksum;
  29. uint loader_revision;
  30. uint processor_flags;
  31. const void *data;
  32. int size;
  33. };
  34. static int microcode_decode_node(const void *blob, int node,
  35. struct microcode_update *update)
  36. {
  37. update->data = fdt_getprop(blob, node, "data", &update->size);
  38. if (!update->data)
  39. return -EINVAL;
  40. update->header_version = fdtdec_get_int(blob, node,
  41. "intel,header-version", 0);
  42. update->update_revision = fdtdec_get_int(blob, node,
  43. "intel,update-revision", 0);
  44. update->date_code = fdtdec_get_int(blob, node,
  45. "intel,date-code", 0);
  46. update->processor_signature = fdtdec_get_int(blob, node,
  47. "intel.processor-signature", 0);
  48. update->checksum = fdtdec_get_int(blob, node, "intel,checksum", 0);
  49. update->loader_revision = fdtdec_get_int(blob, node,
  50. "loader-revision", 0);
  51. update->processor_flags = fdtdec_get_int(blob, node,
  52. "processor-flags", 0);
  53. return 0;
  54. }
  55. static uint32_t microcode_read_rev(void)
  56. {
  57. /*
  58. * Some Intel CPUs can be very finicky about the CPUID sequence used.
  59. * So this is implemented in assembly so that it works reliably.
  60. */
  61. uint32_t low, high;
  62. asm volatile (
  63. "xorl %%eax, %%eax\n"
  64. "xorl %%edx, %%edx\n"
  65. "movl $0x8b, %%ecx\n"
  66. "wrmsr\n"
  67. "movl $0x01, %%eax\n"
  68. "cpuid\n"
  69. "movl $0x8b, %%ecx\n"
  70. "rdmsr\n"
  71. : /* outputs */
  72. "=a" (low), "=d" (high)
  73. : /* inputs */
  74. : /* clobbers */
  75. "ebx", "ecx"
  76. );
  77. return high;
  78. }
  79. static void microcode_read_cpu(struct microcode_update *cpu)
  80. {
  81. /* CPUID sets MSR 0x8B iff a microcode update has been loaded. */
  82. unsigned int x86_model, x86_family;
  83. struct cpuid_result result;
  84. uint32_t low, high;
  85. wrmsr(0x8b, 0, 0);
  86. result = cpuid(1);
  87. rdmsr(0x8b, low, cpu->update_revision);
  88. x86_model = (result.eax >> 4) & 0x0f;
  89. x86_family = (result.eax >> 8) & 0x0f;
  90. cpu->processor_signature = result.eax;
  91. cpu->processor_flags = 0;
  92. if ((x86_model >= 5) || (x86_family > 6)) {
  93. rdmsr(0x17, low, high);
  94. cpu->processor_flags = 1 << ((high >> 18) & 7);
  95. }
  96. debug("microcode: sig=%#x pf=%#x revision=%#x\n",
  97. cpu->processor_signature, cpu->processor_flags,
  98. cpu->update_revision);
  99. }
  100. /* Get a microcode update from the device tree and apply it */
  101. int microcode_update_intel(void)
  102. {
  103. struct microcode_update cpu, update;
  104. const void *blob = gd->fdt_blob;
  105. int count;
  106. int node;
  107. int ret;
  108. microcode_read_cpu(&cpu);
  109. node = 0;
  110. count = 0;
  111. do {
  112. node = fdtdec_next_compatible(blob, node,
  113. COMPAT_INTEL_MICROCODE);
  114. if (node < 0) {
  115. debug("%s: Found %d updates\n", __func__, count);
  116. return count ? 0 : -ENOENT;
  117. }
  118. ret = microcode_decode_node(blob, node, &update);
  119. if (ret) {
  120. debug("%s: Unable to decode update: %d\n", __func__,
  121. ret);
  122. return ret;
  123. }
  124. if (update.processor_signature == cpu.processor_signature &&
  125. (update.processor_flags & cpu.processor_flags)) {
  126. debug("%s: Update already exists\n", __func__);
  127. return -EEXIST;
  128. }
  129. wrmsr(0x79, (ulong)update.data, 0);
  130. debug("microcode: updated to revision 0x%x date=%04x-%02x-%02x\n",
  131. microcode_read_rev(), update.date_code & 0xffff,
  132. (update.date_code >> 24) & 0xff,
  133. (update.date_code >> 16) & 0xff);
  134. count++;
  135. } while (1);
  136. }