intel.c 21 KB

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