intel.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996
  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. * Intel CPU microcode early update for Linux
  8. *
  9. * Copyright (C) 2012 Fenghua Yu <fenghua.yu@intel.com>
  10. * H Peter Anvin" <hpa@zytor.com>
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; either version
  15. * 2 of the License, or (at your option) any later version.
  16. */
  17. /*
  18. * This needs to be before all headers so that pr_debug in printk.h doesn't turn
  19. * printk calls into no_printk().
  20. *
  21. *#define DEBUG
  22. */
  23. #define pr_fmt(fmt) "microcode: " fmt
  24. #include <linux/earlycpio.h>
  25. #include <linux/firmware.h>
  26. #include <linux/uaccess.h>
  27. #include <linux/vmalloc.h>
  28. #include <linux/initrd.h>
  29. #include <linux/kernel.h>
  30. #include <linux/slab.h>
  31. #include <linux/cpu.h>
  32. #include <linux/mm.h>
  33. #include <asm/microcode_intel.h>
  34. #include <asm/processor.h>
  35. #include <asm/tlbflush.h>
  36. #include <asm/setup.h>
  37. #include <asm/msr.h>
  38. static const char ucode_path[] = "kernel/x86/microcode/GenuineIntel.bin";
  39. /* Current microcode patch used in early patching */
  40. struct microcode_intel *intel_ucode_patch;
  41. static inline bool cpu_signatures_match(unsigned int s1, unsigned int p1,
  42. unsigned int s2, unsigned int p2)
  43. {
  44. if (s1 != s2)
  45. return false;
  46. /* Processor flags are either both 0 ... */
  47. if (!p1 && !p2)
  48. return true;
  49. /* ... or they intersect. */
  50. return p1 & p2;
  51. }
  52. /*
  53. * Returns 1 if update has been found, 0 otherwise.
  54. */
  55. static int find_matching_signature(void *mc, unsigned int csig, int cpf)
  56. {
  57. struct microcode_header_intel *mc_hdr = mc;
  58. struct extended_sigtable *ext_hdr;
  59. struct extended_signature *ext_sig;
  60. int i;
  61. if (cpu_signatures_match(csig, cpf, mc_hdr->sig, mc_hdr->pf))
  62. return 1;
  63. /* Look for ext. headers: */
  64. if (get_totalsize(mc_hdr) <= get_datasize(mc_hdr) + MC_HEADER_SIZE)
  65. return 0;
  66. ext_hdr = mc + get_datasize(mc_hdr) + MC_HEADER_SIZE;
  67. ext_sig = (void *)ext_hdr + EXT_HEADER_SIZE;
  68. for (i = 0; i < ext_hdr->count; i++) {
  69. if (cpu_signatures_match(csig, cpf, ext_sig->sig, ext_sig->pf))
  70. return 1;
  71. ext_sig++;
  72. }
  73. return 0;
  74. }
  75. /*
  76. * Returns 1 if update has been found, 0 otherwise.
  77. */
  78. static int has_newer_microcode(void *mc, unsigned int csig, int cpf, int new_rev)
  79. {
  80. struct microcode_header_intel *mc_hdr = mc;
  81. if (mc_hdr->rev <= new_rev)
  82. return 0;
  83. return find_matching_signature(mc, csig, cpf);
  84. }
  85. /*
  86. * Given CPU signature and a microcode patch, this function finds if the
  87. * microcode patch has matching family and model with the CPU.
  88. *
  89. * %true - if there's a match
  90. * %false - otherwise
  91. */
  92. static bool microcode_matches(struct microcode_header_intel *mc_header,
  93. unsigned long sig)
  94. {
  95. unsigned long total_size = get_totalsize(mc_header);
  96. unsigned long data_size = get_datasize(mc_header);
  97. struct extended_sigtable *ext_header;
  98. unsigned int fam_ucode, model_ucode;
  99. struct extended_signature *ext_sig;
  100. unsigned int fam, model;
  101. int ext_sigcount, i;
  102. fam = x86_family(sig);
  103. model = x86_model(sig);
  104. fam_ucode = x86_family(mc_header->sig);
  105. model_ucode = x86_model(mc_header->sig);
  106. if (fam == fam_ucode && model == model_ucode)
  107. return true;
  108. /* Look for ext. headers: */
  109. if (total_size <= data_size + MC_HEADER_SIZE)
  110. return false;
  111. ext_header = (void *) mc_header + data_size + MC_HEADER_SIZE;
  112. ext_sig = (void *)ext_header + EXT_HEADER_SIZE;
  113. ext_sigcount = ext_header->count;
  114. for (i = 0; i < ext_sigcount; i++) {
  115. fam_ucode = x86_family(ext_sig->sig);
  116. model_ucode = x86_model(ext_sig->sig);
  117. if (fam == fam_ucode && model == model_ucode)
  118. return true;
  119. ext_sig++;
  120. }
  121. return false;
  122. }
  123. static struct ucode_patch *__alloc_microcode_buf(void *data, unsigned int size)
  124. {
  125. struct ucode_patch *p;
  126. p = kzalloc(size, GFP_KERNEL);
  127. if (!p)
  128. return ERR_PTR(-ENOMEM);
  129. p->data = kmemdup(data, size, GFP_KERNEL);
  130. if (!p->data) {
  131. kfree(p);
  132. return ERR_PTR(-ENOMEM);
  133. }
  134. return p;
  135. }
  136. static void save_microcode_patch(void *data, unsigned int size)
  137. {
  138. struct microcode_header_intel *mc_hdr, *mc_saved_hdr;
  139. struct ucode_patch *iter, *tmp, *p;
  140. bool prev_found = false;
  141. unsigned int sig, pf;
  142. mc_hdr = (struct microcode_header_intel *)data;
  143. list_for_each_entry_safe(iter, tmp, &microcode_cache, plist) {
  144. mc_saved_hdr = (struct microcode_header_intel *)iter->data;
  145. sig = mc_saved_hdr->sig;
  146. pf = mc_saved_hdr->pf;
  147. if (find_matching_signature(data, sig, pf)) {
  148. prev_found = true;
  149. if (mc_hdr->rev <= mc_saved_hdr->rev)
  150. continue;
  151. p = __alloc_microcode_buf(data, size);
  152. if (IS_ERR(p))
  153. pr_err("Error allocating buffer %p\n", data);
  154. else
  155. list_replace(&iter->plist, &p->plist);
  156. }
  157. }
  158. /*
  159. * There weren't any previous patches found in the list cache; save the
  160. * newly found.
  161. */
  162. if (!prev_found) {
  163. p = __alloc_microcode_buf(data, size);
  164. if (IS_ERR(p))
  165. pr_err("Error allocating buffer for %p\n", data);
  166. else
  167. list_add_tail(&p->plist, &microcode_cache);
  168. }
  169. }
  170. static int microcode_sanity_check(void *mc, int print_err)
  171. {
  172. unsigned long total_size, data_size, ext_table_size;
  173. struct microcode_header_intel *mc_header = mc;
  174. struct extended_sigtable *ext_header = NULL;
  175. u32 sum, orig_sum, ext_sigcount = 0, i;
  176. struct extended_signature *ext_sig;
  177. total_size = get_totalsize(mc_header);
  178. data_size = get_datasize(mc_header);
  179. if (data_size + MC_HEADER_SIZE > total_size) {
  180. if (print_err)
  181. pr_err("Error: bad microcode data file size.\n");
  182. return -EINVAL;
  183. }
  184. if (mc_header->ldrver != 1 || mc_header->hdrver != 1) {
  185. if (print_err)
  186. pr_err("Error: invalid/unknown microcode update format.\n");
  187. return -EINVAL;
  188. }
  189. ext_table_size = total_size - (MC_HEADER_SIZE + data_size);
  190. if (ext_table_size) {
  191. u32 ext_table_sum = 0;
  192. u32 *ext_tablep;
  193. if ((ext_table_size < EXT_HEADER_SIZE)
  194. || ((ext_table_size - EXT_HEADER_SIZE) % EXT_SIGNATURE_SIZE)) {
  195. if (print_err)
  196. pr_err("Error: truncated extended signature table.\n");
  197. return -EINVAL;
  198. }
  199. ext_header = mc + MC_HEADER_SIZE + data_size;
  200. if (ext_table_size != exttable_size(ext_header)) {
  201. if (print_err)
  202. pr_err("Error: extended signature table size mismatch.\n");
  203. return -EFAULT;
  204. }
  205. ext_sigcount = ext_header->count;
  206. /*
  207. * Check extended table checksum: the sum of all dwords that
  208. * comprise a valid table must be 0.
  209. */
  210. ext_tablep = (u32 *)ext_header;
  211. i = ext_table_size / sizeof(u32);
  212. while (i--)
  213. ext_table_sum += ext_tablep[i];
  214. if (ext_table_sum) {
  215. if (print_err)
  216. pr_warn("Bad extended signature table checksum, aborting.\n");
  217. return -EINVAL;
  218. }
  219. }
  220. /*
  221. * Calculate the checksum of update data and header. The checksum of
  222. * valid update data and header including the extended signature table
  223. * must be 0.
  224. */
  225. orig_sum = 0;
  226. i = (MC_HEADER_SIZE + data_size) / sizeof(u32);
  227. while (i--)
  228. orig_sum += ((u32 *)mc)[i];
  229. if (orig_sum) {
  230. if (print_err)
  231. pr_err("Bad microcode data checksum, aborting.\n");
  232. return -EINVAL;
  233. }
  234. if (!ext_table_size)
  235. return 0;
  236. /*
  237. * Check extended signature checksum: 0 => valid.
  238. */
  239. for (i = 0; i < ext_sigcount; i++) {
  240. ext_sig = (void *)ext_header + EXT_HEADER_SIZE +
  241. EXT_SIGNATURE_SIZE * i;
  242. sum = (mc_header->sig + mc_header->pf + mc_header->cksum) -
  243. (ext_sig->sig + ext_sig->pf + ext_sig->cksum);
  244. if (sum) {
  245. if (print_err)
  246. pr_err("Bad extended signature checksum, aborting.\n");
  247. return -EINVAL;
  248. }
  249. }
  250. return 0;
  251. }
  252. /*
  253. * Get microcode matching with BSP's model. Only CPUs with the same model as
  254. * BSP can stay in the platform.
  255. */
  256. static struct microcode_intel *
  257. scan_microcode(void *data, size_t size, struct ucode_cpu_info *uci, bool save)
  258. {
  259. struct microcode_header_intel *mc_header;
  260. struct microcode_intel *patch = NULL;
  261. unsigned int mc_size;
  262. while (size) {
  263. if (size < sizeof(struct microcode_header_intel))
  264. break;
  265. mc_header = (struct microcode_header_intel *)data;
  266. mc_size = get_totalsize(mc_header);
  267. if (!mc_size ||
  268. mc_size > size ||
  269. microcode_sanity_check(data, 0) < 0)
  270. break;
  271. size -= mc_size;
  272. if (!microcode_matches(mc_header, uci->cpu_sig.sig)) {
  273. data += mc_size;
  274. continue;
  275. }
  276. if (save) {
  277. save_microcode_patch(data, mc_size);
  278. goto next;
  279. }
  280. if (!patch) {
  281. if (!has_newer_microcode(data,
  282. uci->cpu_sig.sig,
  283. uci->cpu_sig.pf,
  284. uci->cpu_sig.rev))
  285. goto next;
  286. } else {
  287. struct microcode_header_intel *phdr = &patch->hdr;
  288. if (!has_newer_microcode(data,
  289. phdr->sig,
  290. phdr->pf,
  291. phdr->rev))
  292. goto next;
  293. }
  294. /* We have a newer patch, save it. */
  295. patch = data;
  296. next:
  297. data += mc_size;
  298. }
  299. if (size)
  300. return NULL;
  301. return patch;
  302. }
  303. static void cpuid_1(void)
  304. {
  305. /*
  306. * According to the Intel SDM, Volume 3, 9.11.7:
  307. *
  308. * CPUID returns a value in a model specific register in
  309. * addition to its usual register return values. The
  310. * semantics of CPUID cause it to deposit an update ID value
  311. * in the 64-bit model-specific register at address 08BH
  312. * (IA32_BIOS_SIGN_ID). If no update is present in the
  313. * processor, the value in the MSR remains unmodified.
  314. *
  315. * Use native_cpuid -- this code runs very early and we don't
  316. * want to mess with paravirt.
  317. */
  318. unsigned int eax = 1, ebx, ecx = 0, edx;
  319. native_cpuid(&eax, &ebx, &ecx, &edx);
  320. }
  321. static int collect_cpu_info_early(struct ucode_cpu_info *uci)
  322. {
  323. unsigned int val[2];
  324. unsigned int family, model;
  325. struct cpu_signature csig = { 0 };
  326. unsigned int eax, ebx, ecx, edx;
  327. memset(uci, 0, sizeof(*uci));
  328. eax = 0x00000001;
  329. ecx = 0;
  330. native_cpuid(&eax, &ebx, &ecx, &edx);
  331. csig.sig = eax;
  332. family = x86_family(eax);
  333. model = x86_model(eax);
  334. if ((model >= 5) || (family > 6)) {
  335. /* get processor flags from MSR 0x17 */
  336. native_rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]);
  337. csig.pf = 1 << ((val[1] >> 18) & 7);
  338. }
  339. native_wrmsrl(MSR_IA32_UCODE_REV, 0);
  340. /* As documented in the SDM: Do a CPUID 1 here */
  341. cpuid_1();
  342. /* get the current revision from MSR 0x8B */
  343. native_rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]);
  344. csig.rev = val[1];
  345. uci->cpu_sig = csig;
  346. uci->valid = 1;
  347. return 0;
  348. }
  349. static void show_saved_mc(void)
  350. {
  351. #ifdef DEBUG
  352. int i = 0, j;
  353. unsigned int sig, pf, rev, total_size, data_size, date;
  354. struct ucode_cpu_info uci;
  355. struct ucode_patch *p;
  356. if (list_empty(&microcode_cache)) {
  357. pr_debug("no microcode data saved.\n");
  358. return;
  359. }
  360. collect_cpu_info_early(&uci);
  361. sig = uci.cpu_sig.sig;
  362. pf = uci.cpu_sig.pf;
  363. rev = uci.cpu_sig.rev;
  364. pr_debug("CPU: sig=0x%x, pf=0x%x, rev=0x%x\n", sig, pf, rev);
  365. list_for_each_entry(p, &microcode_cache, plist) {
  366. struct microcode_header_intel *mc_saved_header;
  367. struct extended_sigtable *ext_header;
  368. struct extended_signature *ext_sig;
  369. int ext_sigcount;
  370. mc_saved_header = (struct microcode_header_intel *)p->data;
  371. sig = mc_saved_header->sig;
  372. pf = mc_saved_header->pf;
  373. rev = mc_saved_header->rev;
  374. date = mc_saved_header->date;
  375. total_size = get_totalsize(mc_saved_header);
  376. data_size = get_datasize(mc_saved_header);
  377. pr_debug("mc_saved[%d]: sig=0x%x, pf=0x%x, rev=0x%x, total size=0x%x, date = %04x-%02x-%02x\n",
  378. i++, sig, pf, rev, total_size,
  379. date & 0xffff,
  380. date >> 24,
  381. (date >> 16) & 0xff);
  382. /* Look for ext. headers: */
  383. if (total_size <= data_size + MC_HEADER_SIZE)
  384. continue;
  385. ext_header = (void *)mc_saved_header + data_size + MC_HEADER_SIZE;
  386. ext_sigcount = ext_header->count;
  387. ext_sig = (void *)ext_header + EXT_HEADER_SIZE;
  388. for (j = 0; j < ext_sigcount; j++) {
  389. sig = ext_sig->sig;
  390. pf = ext_sig->pf;
  391. pr_debug("\tExtended[%d]: sig=0x%x, pf=0x%x\n",
  392. j, sig, pf);
  393. ext_sig++;
  394. }
  395. }
  396. #endif
  397. }
  398. /*
  399. * Save this microcode patch. It will be loaded early when a CPU is
  400. * hot-added or resumes.
  401. */
  402. static void save_mc_for_early(u8 *mc, unsigned int size)
  403. {
  404. #ifdef CONFIG_HOTPLUG_CPU
  405. /* Synchronization during CPU hotplug. */
  406. static DEFINE_MUTEX(x86_cpu_microcode_mutex);
  407. mutex_lock(&x86_cpu_microcode_mutex);
  408. save_microcode_patch(mc, size);
  409. show_saved_mc();
  410. mutex_unlock(&x86_cpu_microcode_mutex);
  411. #endif
  412. }
  413. static bool load_builtin_intel_microcode(struct cpio_data *cp)
  414. {
  415. unsigned int eax = 1, ebx, ecx = 0, edx;
  416. char name[30];
  417. if (IS_ENABLED(CONFIG_X86_32))
  418. return false;
  419. native_cpuid(&eax, &ebx, &ecx, &edx);
  420. sprintf(name, "intel-ucode/%02x-%02x-%02x",
  421. x86_family(eax), x86_model(eax), x86_stepping(eax));
  422. return get_builtin_firmware(cp, name);
  423. }
  424. /*
  425. * Print ucode update info.
  426. */
  427. static void
  428. print_ucode_info(struct ucode_cpu_info *uci, unsigned int date)
  429. {
  430. pr_info_once("microcode updated early to revision 0x%x, date = %04x-%02x-%02x\n",
  431. uci->cpu_sig.rev,
  432. date & 0xffff,
  433. date >> 24,
  434. (date >> 16) & 0xff);
  435. }
  436. #ifdef CONFIG_X86_32
  437. static int delay_ucode_info;
  438. static int current_mc_date;
  439. /*
  440. * Print early updated ucode info after printk works. This is delayed info dump.
  441. */
  442. void show_ucode_info_early(void)
  443. {
  444. struct ucode_cpu_info uci;
  445. if (delay_ucode_info) {
  446. collect_cpu_info_early(&uci);
  447. print_ucode_info(&uci, current_mc_date);
  448. delay_ucode_info = 0;
  449. }
  450. }
  451. /*
  452. * At this point, we can not call printk() yet. Delay printing microcode info in
  453. * show_ucode_info_early() until printk() works.
  454. */
  455. static void print_ucode(struct ucode_cpu_info *uci)
  456. {
  457. struct microcode_intel *mc;
  458. int *delay_ucode_info_p;
  459. int *current_mc_date_p;
  460. mc = uci->mc;
  461. if (!mc)
  462. return;
  463. delay_ucode_info_p = (int *)__pa_nodebug(&delay_ucode_info);
  464. current_mc_date_p = (int *)__pa_nodebug(&current_mc_date);
  465. *delay_ucode_info_p = 1;
  466. *current_mc_date_p = mc->hdr.date;
  467. }
  468. #else
  469. /*
  470. * Flush global tlb. We only do this in x86_64 where paging has been enabled
  471. * already and PGE should be enabled as well.
  472. */
  473. static inline void flush_tlb_early(void)
  474. {
  475. __native_flush_tlb_global_irq_disabled();
  476. }
  477. static inline void print_ucode(struct ucode_cpu_info *uci)
  478. {
  479. struct microcode_intel *mc;
  480. mc = uci->mc;
  481. if (!mc)
  482. return;
  483. print_ucode_info(uci, mc->hdr.date);
  484. }
  485. #endif
  486. static int apply_microcode_early(struct ucode_cpu_info *uci, bool early)
  487. {
  488. struct microcode_intel *mc;
  489. unsigned int val[2];
  490. mc = uci->mc;
  491. if (!mc)
  492. return 0;
  493. /* write microcode via MSR 0x79 */
  494. native_wrmsrl(MSR_IA32_UCODE_WRITE, (unsigned long)mc->bits);
  495. native_wrmsrl(MSR_IA32_UCODE_REV, 0);
  496. /* As documented in the SDM: Do a CPUID 1 here */
  497. cpuid_1();
  498. /* get the current revision from MSR 0x8B */
  499. native_rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]);
  500. if (val[1] != mc->hdr.rev)
  501. return -1;
  502. #ifdef CONFIG_X86_64
  503. /* Flush global tlb. This is precaution. */
  504. flush_tlb_early();
  505. #endif
  506. uci->cpu_sig.rev = val[1];
  507. if (early)
  508. print_ucode(uci);
  509. else
  510. print_ucode_info(uci, mc->hdr.date);
  511. return 0;
  512. }
  513. int __init save_microcode_in_initrd_intel(void)
  514. {
  515. struct ucode_cpu_info uci;
  516. struct cpio_data cp;
  517. /*
  518. * AP loading didn't find any microcode patch, no need to save anything.
  519. */
  520. if (!intel_ucode_patch || IS_ERR(intel_ucode_patch))
  521. return 0;
  522. if (!load_builtin_intel_microcode(&cp))
  523. cp = find_microcode_in_initrd(ucode_path, false);
  524. if (!(cp.data && cp.size))
  525. return 0;
  526. collect_cpu_info_early(&uci);
  527. scan_microcode(cp.data, cp.size, &uci, true);
  528. show_saved_mc();
  529. return 0;
  530. }
  531. /*
  532. * @res_patch, output: a pointer to the patch we found.
  533. */
  534. static struct microcode_intel *__load_ucode_intel(struct ucode_cpu_info *uci)
  535. {
  536. static const char *path;
  537. struct cpio_data cp;
  538. bool use_pa;
  539. if (IS_ENABLED(CONFIG_X86_32)) {
  540. path = (const char *)__pa_nodebug(ucode_path);
  541. use_pa = true;
  542. } else {
  543. path = ucode_path;
  544. use_pa = false;
  545. }
  546. /* try built-in microcode first */
  547. if (!load_builtin_intel_microcode(&cp))
  548. cp = find_microcode_in_initrd(path, use_pa);
  549. if (!(cp.data && cp.size))
  550. return NULL;
  551. collect_cpu_info_early(uci);
  552. return scan_microcode(cp.data, cp.size, uci, false);
  553. }
  554. void __init load_ucode_intel_bsp(void)
  555. {
  556. struct microcode_intel *patch;
  557. struct ucode_cpu_info uci;
  558. patch = __load_ucode_intel(&uci);
  559. if (!patch)
  560. return;
  561. uci.mc = patch;
  562. apply_microcode_early(&uci, true);
  563. }
  564. void load_ucode_intel_ap(void)
  565. {
  566. struct microcode_intel *patch, **iup;
  567. struct ucode_cpu_info uci;
  568. if (IS_ENABLED(CONFIG_X86_32))
  569. iup = (struct microcode_intel **) __pa_nodebug(&intel_ucode_patch);
  570. else
  571. iup = &intel_ucode_patch;
  572. reget:
  573. if (!*iup) {
  574. patch = __load_ucode_intel(&uci);
  575. if (!patch)
  576. return;
  577. *iup = patch;
  578. }
  579. uci.mc = *iup;
  580. if (apply_microcode_early(&uci, true)) {
  581. /* Mixed-silicon system? Try to refetch the proper patch: */
  582. *iup = NULL;
  583. goto reget;
  584. }
  585. }
  586. static struct microcode_intel *find_patch(struct ucode_cpu_info *uci)
  587. {
  588. struct microcode_header_intel *phdr;
  589. struct ucode_patch *iter, *tmp;
  590. list_for_each_entry_safe(iter, tmp, &microcode_cache, plist) {
  591. phdr = (struct microcode_header_intel *)iter->data;
  592. if (phdr->rev <= uci->cpu_sig.rev)
  593. continue;
  594. if (!find_matching_signature(phdr,
  595. uci->cpu_sig.sig,
  596. uci->cpu_sig.pf))
  597. continue;
  598. return iter->data;
  599. }
  600. return NULL;
  601. }
  602. void reload_ucode_intel(void)
  603. {
  604. struct microcode_intel *p;
  605. struct ucode_cpu_info uci;
  606. collect_cpu_info_early(&uci);
  607. p = find_patch(&uci);
  608. if (!p)
  609. return;
  610. uci.mc = p;
  611. apply_microcode_early(&uci, false);
  612. }
  613. static int collect_cpu_info(int cpu_num, struct cpu_signature *csig)
  614. {
  615. static struct cpu_signature prev;
  616. struct cpuinfo_x86 *c = &cpu_data(cpu_num);
  617. unsigned int val[2];
  618. memset(csig, 0, sizeof(*csig));
  619. csig->sig = cpuid_eax(0x00000001);
  620. if ((c->x86_model >= 5) || (c->x86 > 6)) {
  621. /* get processor flags from MSR 0x17 */
  622. rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]);
  623. csig->pf = 1 << ((val[1] >> 18) & 7);
  624. }
  625. csig->rev = c->microcode;
  626. /* No extra locking on prev, races are harmless. */
  627. if (csig->sig != prev.sig || csig->pf != prev.pf || csig->rev != prev.rev) {
  628. pr_info("sig=0x%x, pf=0x%x, revision=0x%x\n",
  629. csig->sig, csig->pf, csig->rev);
  630. prev = *csig;
  631. }
  632. return 0;
  633. }
  634. static int apply_microcode_intel(int cpu)
  635. {
  636. struct microcode_intel *mc;
  637. struct ucode_cpu_info *uci;
  638. struct cpuinfo_x86 *c;
  639. unsigned int val[2];
  640. static int prev_rev;
  641. /* We should bind the task to the CPU */
  642. if (WARN_ON(raw_smp_processor_id() != cpu))
  643. return -1;
  644. uci = ucode_cpu_info + cpu;
  645. mc = uci->mc;
  646. if (!mc) {
  647. /* Look for a newer patch in our cache: */
  648. mc = find_patch(uci);
  649. if (!mc)
  650. return 0;
  651. }
  652. /* write microcode via MSR 0x79 */
  653. wrmsrl(MSR_IA32_UCODE_WRITE, (unsigned long)mc->bits);
  654. wrmsrl(MSR_IA32_UCODE_REV, 0);
  655. /* As documented in the SDM: Do a CPUID 1 here */
  656. cpuid_1();
  657. /* get the current revision from MSR 0x8B */
  658. rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]);
  659. if (val[1] != mc->hdr.rev) {
  660. pr_err("CPU%d update to revision 0x%x failed\n",
  661. cpu, mc->hdr.rev);
  662. return -1;
  663. }
  664. if (val[1] != prev_rev) {
  665. pr_info("updated to revision 0x%x, date = %04x-%02x-%02x\n",
  666. val[1],
  667. mc->hdr.date & 0xffff,
  668. mc->hdr.date >> 24,
  669. (mc->hdr.date >> 16) & 0xff);
  670. prev_rev = val[1];
  671. }
  672. c = &cpu_data(cpu);
  673. uci->cpu_sig.rev = val[1];
  674. c->microcode = val[1];
  675. return 0;
  676. }
  677. static enum ucode_state generic_load_microcode(int cpu, void *data, size_t size,
  678. int (*get_ucode_data)(void *, const void *, size_t))
  679. {
  680. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  681. u8 *ucode_ptr = data, *new_mc = NULL, *mc = NULL;
  682. int new_rev = uci->cpu_sig.rev;
  683. unsigned int leftover = size;
  684. unsigned int curr_mc_size = 0;
  685. unsigned int csig, cpf;
  686. while (leftover) {
  687. struct microcode_header_intel mc_header;
  688. unsigned int mc_size;
  689. if (leftover < sizeof(mc_header)) {
  690. pr_err("error! Truncated header in microcode data file\n");
  691. break;
  692. }
  693. if (get_ucode_data(&mc_header, ucode_ptr, sizeof(mc_header)))
  694. break;
  695. mc_size = get_totalsize(&mc_header);
  696. if (!mc_size || mc_size > leftover) {
  697. pr_err("error! Bad data in microcode data file\n");
  698. break;
  699. }
  700. /* For performance reasons, reuse mc area when possible */
  701. if (!mc || mc_size > curr_mc_size) {
  702. vfree(mc);
  703. mc = vmalloc(mc_size);
  704. if (!mc)
  705. break;
  706. curr_mc_size = mc_size;
  707. }
  708. if (get_ucode_data(mc, ucode_ptr, mc_size) ||
  709. microcode_sanity_check(mc, 1) < 0) {
  710. break;
  711. }
  712. csig = uci->cpu_sig.sig;
  713. cpf = uci->cpu_sig.pf;
  714. if (has_newer_microcode(mc, csig, cpf, new_rev)) {
  715. vfree(new_mc);
  716. new_rev = mc_header.rev;
  717. new_mc = mc;
  718. mc = NULL; /* trigger new vmalloc */
  719. }
  720. ucode_ptr += mc_size;
  721. leftover -= mc_size;
  722. }
  723. vfree(mc);
  724. if (leftover) {
  725. vfree(new_mc);
  726. return UCODE_ERROR;
  727. }
  728. if (!new_mc)
  729. return UCODE_NFOUND;
  730. vfree(uci->mc);
  731. uci->mc = (struct microcode_intel *)new_mc;
  732. /*
  733. * If early loading microcode is supported, save this mc into
  734. * permanent memory. So it will be loaded early when a CPU is hot added
  735. * or resumes.
  736. */
  737. save_mc_for_early(new_mc, curr_mc_size);
  738. pr_debug("CPU%d found a matching microcode update with version 0x%x (current=0x%x)\n",
  739. cpu, new_rev, uci->cpu_sig.rev);
  740. return UCODE_OK;
  741. }
  742. static int get_ucode_fw(void *to, const void *from, size_t n)
  743. {
  744. memcpy(to, from, n);
  745. return 0;
  746. }
  747. static enum ucode_state request_microcode_fw(int cpu, struct device *device,
  748. bool refresh_fw)
  749. {
  750. char name[30];
  751. struct cpuinfo_x86 *c = &cpu_data(cpu);
  752. const struct firmware *firmware;
  753. enum ucode_state ret;
  754. sprintf(name, "intel-ucode/%02x-%02x-%02x",
  755. c->x86, c->x86_model, c->x86_mask);
  756. if (request_firmware_direct(&firmware, name, device)) {
  757. pr_debug("data file %s load failed\n", name);
  758. return UCODE_NFOUND;
  759. }
  760. ret = generic_load_microcode(cpu, (void *)firmware->data,
  761. firmware->size, &get_ucode_fw);
  762. release_firmware(firmware);
  763. return ret;
  764. }
  765. static int get_ucode_user(void *to, const void *from, size_t n)
  766. {
  767. return copy_from_user(to, from, n);
  768. }
  769. static enum ucode_state
  770. request_microcode_user(int cpu, const void __user *buf, size_t size)
  771. {
  772. return generic_load_microcode(cpu, (void *)buf, size, &get_ucode_user);
  773. }
  774. static struct microcode_ops microcode_intel_ops = {
  775. .request_microcode_user = request_microcode_user,
  776. .request_microcode_fw = request_microcode_fw,
  777. .collect_cpu_info = collect_cpu_info,
  778. .apply_microcode = apply_microcode_intel,
  779. };
  780. struct microcode_ops * __init init_intel_microcode(void)
  781. {
  782. struct cpuinfo_x86 *c = &boot_cpu_data;
  783. if (c->x86_vendor != X86_VENDOR_INTEL || c->x86 < 6 ||
  784. cpu_has(c, X86_FEATURE_IA64)) {
  785. pr_err("Intel CPU family 0x%x not supported\n", c->x86);
  786. return NULL;
  787. }
  788. return &microcode_intel_ops;
  789. }