intel.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. * Intel CPU Microcode Update Driver for Linux
  3. *
  4. * Copyright (C) 2000-2006 Tigran Aivazian <tigran@aivazian.fsnet.co.uk>
  5. * 2006 Shaohua Li <shaohua.li@intel.com>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. */
  12. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  13. #include <linux/firmware.h>
  14. #include <linux/uaccess.h>
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/vmalloc.h>
  18. #include <asm/microcode_intel.h>
  19. #include <asm/processor.h>
  20. #include <asm/msr.h>
  21. MODULE_DESCRIPTION("Microcode Update Driver");
  22. MODULE_AUTHOR("Tigran Aivazian <tigran@aivazian.fsnet.co.uk>");
  23. MODULE_LICENSE("GPL");
  24. static int collect_cpu_info(int cpu_num, struct cpu_signature *csig)
  25. {
  26. struct cpuinfo_x86 *c = &cpu_data(cpu_num);
  27. unsigned int val[2];
  28. memset(csig, 0, sizeof(*csig));
  29. csig->sig = cpuid_eax(0x00000001);
  30. if ((c->x86_model >= 5) || (c->x86 > 6)) {
  31. /* get processor flags from MSR 0x17 */
  32. rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]);
  33. csig->pf = 1 << ((val[1] >> 18) & 7);
  34. }
  35. csig->rev = c->microcode;
  36. pr_info("CPU%d sig=0x%x, pf=0x%x, revision=0x%x\n",
  37. cpu_num, csig->sig, csig->pf, csig->rev);
  38. return 0;
  39. }
  40. /*
  41. * return 0 - no update found
  42. * return 1 - found update
  43. */
  44. static int get_matching_mc(struct microcode_intel *mc_intel, int cpu)
  45. {
  46. struct cpu_signature cpu_sig;
  47. unsigned int csig, cpf, crev;
  48. collect_cpu_info(cpu, &cpu_sig);
  49. csig = cpu_sig.sig;
  50. cpf = cpu_sig.pf;
  51. crev = cpu_sig.rev;
  52. return has_newer_microcode(mc_intel, csig, cpf, crev);
  53. }
  54. static int apply_microcode_intel(int cpu)
  55. {
  56. struct microcode_intel *mc_intel;
  57. struct ucode_cpu_info *uci;
  58. unsigned int val[2];
  59. int cpu_num = raw_smp_processor_id();
  60. struct cpuinfo_x86 *c = &cpu_data(cpu_num);
  61. uci = ucode_cpu_info + cpu;
  62. mc_intel = uci->mc;
  63. /* We should bind the task to the CPU */
  64. BUG_ON(cpu_num != cpu);
  65. if (mc_intel == NULL)
  66. return 0;
  67. /*
  68. * Microcode on this CPU could be updated earlier. Only apply the
  69. * microcode patch in mc_intel when it is newer than the one on this
  70. * CPU.
  71. */
  72. if (get_matching_mc(mc_intel, cpu) == 0)
  73. return 0;
  74. /* write microcode via MSR 0x79 */
  75. wrmsr(MSR_IA32_UCODE_WRITE,
  76. (unsigned long) mc_intel->bits,
  77. (unsigned long) mc_intel->bits >> 16 >> 16);
  78. wrmsr(MSR_IA32_UCODE_REV, 0, 0);
  79. /* As documented in the SDM: Do a CPUID 1 here */
  80. sync_core();
  81. /* get the current revision from MSR 0x8B */
  82. rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]);
  83. if (val[1] != mc_intel->hdr.rev) {
  84. pr_err("CPU%d update to revision 0x%x failed\n",
  85. cpu_num, mc_intel->hdr.rev);
  86. return -1;
  87. }
  88. pr_info("CPU%d updated to revision 0x%x, date = %04x-%02x-%02x\n",
  89. cpu_num, val[1],
  90. mc_intel->hdr.date & 0xffff,
  91. mc_intel->hdr.date >> 24,
  92. (mc_intel->hdr.date >> 16) & 0xff);
  93. uci->cpu_sig.rev = val[1];
  94. c->microcode = val[1];
  95. return 0;
  96. }
  97. static enum ucode_state generic_load_microcode(int cpu, void *data, size_t size,
  98. int (*get_ucode_data)(void *, const void *, size_t))
  99. {
  100. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  101. u8 *ucode_ptr = data, *new_mc = NULL, *mc = NULL;
  102. int new_rev = uci->cpu_sig.rev;
  103. unsigned int leftover = size;
  104. enum ucode_state state = UCODE_OK;
  105. unsigned int curr_mc_size = 0;
  106. unsigned int csig, cpf;
  107. while (leftover) {
  108. struct microcode_header_intel mc_header;
  109. unsigned int mc_size;
  110. if (leftover < sizeof(mc_header)) {
  111. pr_err("error! Truncated header in microcode data file\n");
  112. break;
  113. }
  114. if (get_ucode_data(&mc_header, ucode_ptr, sizeof(mc_header)))
  115. break;
  116. mc_size = get_totalsize(&mc_header);
  117. if (!mc_size || mc_size > leftover) {
  118. pr_err("error! Bad data in microcode data file\n");
  119. break;
  120. }
  121. /* For performance reasons, reuse mc area when possible */
  122. if (!mc || mc_size > curr_mc_size) {
  123. vfree(mc);
  124. mc = vmalloc(mc_size);
  125. if (!mc)
  126. break;
  127. curr_mc_size = mc_size;
  128. }
  129. if (get_ucode_data(mc, ucode_ptr, mc_size) ||
  130. microcode_sanity_check(mc, 1) < 0) {
  131. break;
  132. }
  133. csig = uci->cpu_sig.sig;
  134. cpf = uci->cpu_sig.pf;
  135. if (has_newer_microcode(mc, csig, cpf, new_rev)) {
  136. vfree(new_mc);
  137. new_rev = mc_header.rev;
  138. new_mc = mc;
  139. mc = NULL; /* trigger new vmalloc */
  140. }
  141. ucode_ptr += mc_size;
  142. leftover -= mc_size;
  143. }
  144. vfree(mc);
  145. if (leftover) {
  146. vfree(new_mc);
  147. state = UCODE_ERROR;
  148. goto out;
  149. }
  150. if (!new_mc) {
  151. state = UCODE_NFOUND;
  152. goto out;
  153. }
  154. vfree(uci->mc);
  155. uci->mc = (struct microcode_intel *)new_mc;
  156. /*
  157. * If early loading microcode is supported, save this mc into
  158. * permanent memory. So it will be loaded early when a CPU is hot added
  159. * or resumes.
  160. */
  161. save_mc_for_early(new_mc);
  162. pr_debug("CPU%d found a matching microcode update with version 0x%x (current=0x%x)\n",
  163. cpu, new_rev, uci->cpu_sig.rev);
  164. out:
  165. return state;
  166. }
  167. static int get_ucode_fw(void *to, const void *from, size_t n)
  168. {
  169. memcpy(to, from, n);
  170. return 0;
  171. }
  172. static enum ucode_state request_microcode_fw(int cpu, struct device *device,
  173. bool refresh_fw)
  174. {
  175. char name[30];
  176. struct cpuinfo_x86 *c = &cpu_data(cpu);
  177. const struct firmware *firmware;
  178. enum ucode_state ret;
  179. sprintf(name, "/*(DEBLOBBED)*/",
  180. c->x86, c->x86_model, c->x86_mask);
  181. if (reject_firmware_direct(&firmware, name, device)) {
  182. pr_debug("data file %s load failed\n", name);
  183. return UCODE_NFOUND;
  184. }
  185. ret = generic_load_microcode(cpu, (void *)firmware->data,
  186. firmware->size, &get_ucode_fw);
  187. release_firmware(firmware);
  188. return ret;
  189. }
  190. static int get_ucode_user(void *to, const void *from, size_t n)
  191. {
  192. return copy_from_user(to, from, n);
  193. }
  194. static enum ucode_state
  195. request_microcode_user(int cpu, const void __user *buf, size_t size)
  196. {
  197. return generic_load_microcode(cpu, (void *)buf, size, &get_ucode_user);
  198. }
  199. static void microcode_fini_cpu(int cpu)
  200. {
  201. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  202. vfree(uci->mc);
  203. uci->mc = NULL;
  204. }
  205. static struct microcode_ops microcode_intel_ops = {
  206. .request_microcode_user = request_microcode_user,
  207. .request_microcode_fw = request_microcode_fw,
  208. .collect_cpu_info = collect_cpu_info,
  209. .apply_microcode = apply_microcode_intel,
  210. .microcode_fini_cpu = microcode_fini_cpu,
  211. };
  212. struct microcode_ops * __init init_intel_microcode(void)
  213. {
  214. struct cpuinfo_x86 *c = &cpu_data(0);
  215. if (c->x86_vendor != X86_VENDOR_INTEL || c->x86 < 6 ||
  216. cpu_has(c, X86_FEATURE_IA64)) {
  217. pr_err("Intel CPU family 0x%x not supported\n", c->x86);
  218. return NULL;
  219. }
  220. return &microcode_intel_ops;
  221. }