intel.c 22 KB

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