intel.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  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 unsigned long mc_saved_in_initrd[MAX_UCODE_COUNT];
  39. static struct mc_saved_data {
  40. unsigned int mc_saved_count;
  41. struct microcode_intel **mc_saved;
  42. } mc_saved_data;
  43. static enum ucode_state
  44. load_microcode_early(struct microcode_intel **saved,
  45. unsigned int num_saved, struct ucode_cpu_info *uci)
  46. {
  47. struct microcode_intel *ucode_ptr, *new_mc = NULL;
  48. struct microcode_header_intel *mc_hdr;
  49. int new_rev, ret, i;
  50. new_rev = uci->cpu_sig.rev;
  51. for (i = 0; i < num_saved; i++) {
  52. ucode_ptr = saved[i];
  53. mc_hdr = (struct microcode_header_intel *)ucode_ptr;
  54. ret = has_newer_microcode(ucode_ptr,
  55. uci->cpu_sig.sig,
  56. uci->cpu_sig.pf,
  57. new_rev);
  58. if (!ret)
  59. continue;
  60. new_rev = mc_hdr->rev;
  61. new_mc = ucode_ptr;
  62. }
  63. if (!new_mc)
  64. return UCODE_NFOUND;
  65. uci->mc = (struct microcode_intel *)new_mc;
  66. return UCODE_OK;
  67. }
  68. static inline void
  69. copy_initrd_ptrs(struct microcode_intel **mc_saved, unsigned long *initrd,
  70. unsigned long off, int num_saved)
  71. {
  72. int i;
  73. for (i = 0; i < num_saved; i++)
  74. mc_saved[i] = (struct microcode_intel *)(initrd[i] + off);
  75. }
  76. #ifdef CONFIG_X86_32
  77. static void
  78. microcode_phys(struct microcode_intel **mc_saved_tmp,
  79. struct mc_saved_data *mc_saved_data)
  80. {
  81. int i;
  82. struct microcode_intel ***mc_saved;
  83. mc_saved = (struct microcode_intel ***)
  84. __pa_nodebug(&mc_saved_data->mc_saved);
  85. for (i = 0; i < mc_saved_data->mc_saved_count; i++) {
  86. struct microcode_intel *p;
  87. p = *(struct microcode_intel **)
  88. __pa_nodebug(mc_saved_data->mc_saved + i);
  89. mc_saved_tmp[i] = (struct microcode_intel *)__pa_nodebug(p);
  90. }
  91. }
  92. #endif
  93. static enum ucode_state
  94. load_microcode(struct mc_saved_data *mc_saved_data, unsigned long *initrd,
  95. unsigned long initrd_start, struct ucode_cpu_info *uci)
  96. {
  97. struct microcode_intel *mc_saved_tmp[MAX_UCODE_COUNT];
  98. unsigned int count = mc_saved_data->mc_saved_count;
  99. if (!mc_saved_data->mc_saved) {
  100. copy_initrd_ptrs(mc_saved_tmp, initrd, initrd_start, count);
  101. return load_microcode_early(mc_saved_tmp, count, uci);
  102. } else {
  103. #ifdef CONFIG_X86_32
  104. microcode_phys(mc_saved_tmp, mc_saved_data);
  105. return load_microcode_early(mc_saved_tmp, count, uci);
  106. #else
  107. return load_microcode_early(mc_saved_data->mc_saved,
  108. count, uci);
  109. #endif
  110. }
  111. }
  112. /*
  113. * Given CPU signature and a microcode patch, this function finds if the
  114. * microcode patch has matching family and model with the CPU.
  115. */
  116. static enum ucode_state
  117. matching_model_microcode(struct microcode_header_intel *mc_header,
  118. unsigned long sig)
  119. {
  120. unsigned int fam, model;
  121. unsigned int fam_ucode, model_ucode;
  122. struct extended_sigtable *ext_header;
  123. unsigned long total_size = get_totalsize(mc_header);
  124. unsigned long data_size = get_datasize(mc_header);
  125. int ext_sigcount, i;
  126. struct extended_signature *ext_sig;
  127. fam = x86_family(sig);
  128. model = x86_model(sig);
  129. fam_ucode = x86_family(mc_header->sig);
  130. model_ucode = x86_model(mc_header->sig);
  131. if (fam == fam_ucode && model == model_ucode)
  132. return UCODE_OK;
  133. /* Look for ext. headers: */
  134. if (total_size <= data_size + MC_HEADER_SIZE)
  135. return UCODE_NFOUND;
  136. ext_header = (void *) mc_header + data_size + MC_HEADER_SIZE;
  137. ext_sig = (void *)ext_header + EXT_HEADER_SIZE;
  138. ext_sigcount = ext_header->count;
  139. for (i = 0; i < ext_sigcount; i++) {
  140. fam_ucode = x86_family(ext_sig->sig);
  141. model_ucode = x86_model(ext_sig->sig);
  142. if (fam == fam_ucode && model == model_ucode)
  143. return UCODE_OK;
  144. ext_sig++;
  145. }
  146. return UCODE_NFOUND;
  147. }
  148. static int
  149. save_microcode(struct mc_saved_data *mc_saved_data,
  150. struct microcode_intel **mc_saved_src,
  151. unsigned int mc_saved_count)
  152. {
  153. int i, j;
  154. struct microcode_intel **saved_ptr;
  155. int ret;
  156. if (!mc_saved_count)
  157. return -EINVAL;
  158. /*
  159. * Copy new microcode data.
  160. */
  161. saved_ptr = kcalloc(mc_saved_count, sizeof(struct microcode_intel *), GFP_KERNEL);
  162. if (!saved_ptr)
  163. return -ENOMEM;
  164. for (i = 0; i < mc_saved_count; i++) {
  165. struct microcode_header_intel *mc_hdr;
  166. struct microcode_intel *mc;
  167. unsigned long size;
  168. if (!mc_saved_src[i]) {
  169. ret = -EINVAL;
  170. goto err;
  171. }
  172. mc = mc_saved_src[i];
  173. mc_hdr = &mc->hdr;
  174. size = get_totalsize(mc_hdr);
  175. saved_ptr[i] = kmalloc(size, GFP_KERNEL);
  176. if (!saved_ptr[i]) {
  177. ret = -ENOMEM;
  178. goto err;
  179. }
  180. memcpy(saved_ptr[i], mc, size);
  181. }
  182. /*
  183. * Point to newly saved microcode.
  184. */
  185. mc_saved_data->mc_saved = saved_ptr;
  186. mc_saved_data->mc_saved_count = mc_saved_count;
  187. return 0;
  188. err:
  189. for (j = 0; j <= i; j++)
  190. kfree(saved_ptr[j]);
  191. kfree(saved_ptr);
  192. return ret;
  193. }
  194. /*
  195. * A microcode patch in ucode_ptr is saved into mc_saved
  196. * - if it has matching signature and newer revision compared to an existing
  197. * patch mc_saved.
  198. * - or if it is a newly discovered microcode patch.
  199. *
  200. * The microcode patch should have matching model with CPU.
  201. *
  202. * Returns: The updated number @num_saved of saved microcode patches.
  203. */
  204. static unsigned int _save_mc(struct microcode_intel **mc_saved,
  205. u8 *ucode_ptr, unsigned int num_saved)
  206. {
  207. struct microcode_header_intel *mc_hdr, *mc_saved_hdr;
  208. unsigned int sig, pf;
  209. int found = 0, i;
  210. mc_hdr = (struct microcode_header_intel *)ucode_ptr;
  211. for (i = 0; i < num_saved; i++) {
  212. mc_saved_hdr = (struct microcode_header_intel *)mc_saved[i];
  213. sig = mc_saved_hdr->sig;
  214. pf = mc_saved_hdr->pf;
  215. if (!find_matching_signature(ucode_ptr, sig, pf))
  216. continue;
  217. found = 1;
  218. if (mc_hdr->rev <= mc_saved_hdr->rev)
  219. continue;
  220. /*
  221. * Found an older ucode saved earlier. Replace it with
  222. * this newer one.
  223. */
  224. mc_saved[i] = (struct microcode_intel *)ucode_ptr;
  225. break;
  226. }
  227. /* Newly detected microcode, save it to memory. */
  228. if (i >= num_saved && !found)
  229. mc_saved[num_saved++] = (struct microcode_intel *)ucode_ptr;
  230. return num_saved;
  231. }
  232. /*
  233. * Get microcode matching with BSP's model. Only CPUs with the same model as
  234. * BSP can stay in the platform.
  235. */
  236. static enum ucode_state __init
  237. get_matching_model_microcode(int cpu, unsigned long start,
  238. void *data, size_t size,
  239. struct mc_saved_data *mc_saved_data,
  240. unsigned long *mc_saved_in_initrd,
  241. struct ucode_cpu_info *uci)
  242. {
  243. u8 *ucode_ptr = data;
  244. unsigned int leftover = size;
  245. enum ucode_state state = UCODE_OK;
  246. unsigned int mc_size;
  247. struct microcode_header_intel *mc_header;
  248. struct microcode_intel *mc_saved_tmp[MAX_UCODE_COUNT];
  249. unsigned int mc_saved_count = mc_saved_data->mc_saved_count;
  250. int i;
  251. while (leftover && mc_saved_count < ARRAY_SIZE(mc_saved_tmp)) {
  252. if (leftover < sizeof(mc_header))
  253. break;
  254. mc_header = (struct microcode_header_intel *)ucode_ptr;
  255. mc_size = get_totalsize(mc_header);
  256. if (!mc_size || mc_size > leftover ||
  257. microcode_sanity_check(ucode_ptr, 0) < 0)
  258. break;
  259. leftover -= mc_size;
  260. /*
  261. * Since APs with same family and model as the BSP may boot in
  262. * the platform, we need to find and save microcode patches
  263. * with the same family and model as the BSP.
  264. */
  265. if (matching_model_microcode(mc_header, uci->cpu_sig.sig) !=
  266. UCODE_OK) {
  267. ucode_ptr += mc_size;
  268. continue;
  269. }
  270. mc_saved_count = _save_mc(mc_saved_tmp, ucode_ptr, mc_saved_count);
  271. ucode_ptr += mc_size;
  272. }
  273. if (leftover) {
  274. state = UCODE_ERROR;
  275. goto out;
  276. }
  277. if (mc_saved_count == 0) {
  278. state = UCODE_NFOUND;
  279. goto out;
  280. }
  281. for (i = 0; i < mc_saved_count; i++)
  282. mc_saved_in_initrd[i] = (unsigned long)mc_saved_tmp[i] - start;
  283. mc_saved_data->mc_saved_count = mc_saved_count;
  284. out:
  285. return state;
  286. }
  287. static int collect_cpu_info_early(struct ucode_cpu_info *uci)
  288. {
  289. unsigned int val[2];
  290. unsigned int family, model;
  291. struct cpu_signature csig;
  292. unsigned int eax, ebx, ecx, edx;
  293. csig.sig = 0;
  294. csig.pf = 0;
  295. csig.rev = 0;
  296. memset(uci, 0, sizeof(*uci));
  297. eax = 0x00000001;
  298. ecx = 0;
  299. native_cpuid(&eax, &ebx, &ecx, &edx);
  300. csig.sig = eax;
  301. family = x86_family(csig.sig);
  302. model = x86_model(csig.sig);
  303. if ((model >= 5) || (family > 6)) {
  304. /* get processor flags from MSR 0x17 */
  305. native_rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]);
  306. csig.pf = 1 << ((val[1] >> 18) & 7);
  307. }
  308. native_wrmsr(MSR_IA32_UCODE_REV, 0, 0);
  309. /* As documented in the SDM: Do a CPUID 1 here */
  310. sync_core();
  311. /* get the current revision from MSR 0x8B */
  312. native_rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]);
  313. csig.rev = val[1];
  314. uci->cpu_sig = csig;
  315. uci->valid = 1;
  316. return 0;
  317. }
  318. static void show_saved_mc(void)
  319. {
  320. #ifdef DEBUG
  321. int i, j;
  322. unsigned int sig, pf, rev, total_size, data_size, date;
  323. struct ucode_cpu_info uci;
  324. if (mc_saved_data.mc_saved_count == 0) {
  325. pr_debug("no microcode data saved.\n");
  326. return;
  327. }
  328. pr_debug("Total microcode saved: %d\n", mc_saved_data.mc_saved_count);
  329. collect_cpu_info_early(&uci);
  330. sig = uci.cpu_sig.sig;
  331. pf = uci.cpu_sig.pf;
  332. rev = uci.cpu_sig.rev;
  333. pr_debug("CPU: sig=0x%x, pf=0x%x, rev=0x%x\n", sig, pf, rev);
  334. for (i = 0; i < mc_saved_data.mc_saved_count; i++) {
  335. struct microcode_header_intel *mc_saved_header;
  336. struct extended_sigtable *ext_header;
  337. int ext_sigcount;
  338. struct extended_signature *ext_sig;
  339. mc_saved_header = (struct microcode_header_intel *)
  340. mc_saved_data.mc_saved[i];
  341. sig = mc_saved_header->sig;
  342. pf = mc_saved_header->pf;
  343. rev = mc_saved_header->rev;
  344. total_size = get_totalsize(mc_saved_header);
  345. data_size = get_datasize(mc_saved_header);
  346. date = mc_saved_header->date;
  347. pr_debug("mc_saved[%d]: sig=0x%x, pf=0x%x, rev=0x%x, toal size=0x%x, date = %04x-%02x-%02x\n",
  348. i, sig, pf, rev, total_size,
  349. date & 0xffff,
  350. date >> 24,
  351. (date >> 16) & 0xff);
  352. /* Look for ext. headers: */
  353. if (total_size <= data_size + MC_HEADER_SIZE)
  354. continue;
  355. ext_header = (void *) mc_saved_header + data_size + MC_HEADER_SIZE;
  356. ext_sigcount = ext_header->count;
  357. ext_sig = (void *)ext_header + EXT_HEADER_SIZE;
  358. for (j = 0; j < ext_sigcount; j++) {
  359. sig = ext_sig->sig;
  360. pf = ext_sig->pf;
  361. pr_debug("\tExtended[%d]: sig=0x%x, pf=0x%x\n",
  362. j, sig, pf);
  363. ext_sig++;
  364. }
  365. }
  366. #endif
  367. }
  368. #ifdef CONFIG_HOTPLUG_CPU
  369. static DEFINE_MUTEX(x86_cpu_microcode_mutex);
  370. /*
  371. * Save this mc into mc_saved_data. So it will be loaded early when a CPU is
  372. * hot added or resumes.
  373. *
  374. * Please make sure this mc should be a valid microcode patch before calling
  375. * this function.
  376. */
  377. int save_mc_for_early(u8 *mc)
  378. {
  379. struct microcode_intel *mc_saved_tmp[MAX_UCODE_COUNT];
  380. unsigned int mc_saved_count_init;
  381. unsigned int mc_saved_count;
  382. struct microcode_intel **mc_saved;
  383. int ret = 0;
  384. int i;
  385. /*
  386. * Hold hotplug lock so mc_saved_data is not accessed by a CPU in
  387. * hotplug.
  388. */
  389. mutex_lock(&x86_cpu_microcode_mutex);
  390. mc_saved_count_init = mc_saved_data.mc_saved_count;
  391. mc_saved_count = mc_saved_data.mc_saved_count;
  392. mc_saved = mc_saved_data.mc_saved;
  393. if (mc_saved && mc_saved_count)
  394. memcpy(mc_saved_tmp, mc_saved,
  395. mc_saved_count * sizeof(struct microcode_intel *));
  396. /*
  397. * Save the microcode patch mc in mc_save_tmp structure if it's a newer
  398. * version.
  399. */
  400. mc_saved_count = _save_mc(mc_saved_tmp, mc, mc_saved_count);
  401. /*
  402. * Save the mc_save_tmp in global mc_saved_data.
  403. */
  404. ret = save_microcode(&mc_saved_data, mc_saved_tmp, mc_saved_count);
  405. if (ret) {
  406. pr_err("Cannot save microcode patch.\n");
  407. goto out;
  408. }
  409. show_saved_mc();
  410. /*
  411. * Free old saved microcode data.
  412. */
  413. if (mc_saved) {
  414. for (i = 0; i < mc_saved_count_init; i++)
  415. kfree(mc_saved[i]);
  416. kfree(mc_saved);
  417. }
  418. out:
  419. mutex_unlock(&x86_cpu_microcode_mutex);
  420. return ret;
  421. }
  422. EXPORT_SYMBOL_GPL(save_mc_for_early);
  423. #endif
  424. static bool __init load_builtin_intel_microcode(struct cpio_data *cp)
  425. {
  426. #ifdef CONFIG_X86_64
  427. unsigned int eax = 0x00000001, ebx, ecx = 0, edx;
  428. char name[30];
  429. native_cpuid(&eax, &ebx, &ecx, &edx);
  430. sprintf(name, "intel-ucode/%02x-%02x-%02x",
  431. x86_family(eax), x86_model(eax), x86_stepping(eax));
  432. return get_builtin_firmware(cp, name);
  433. #else
  434. return false;
  435. #endif
  436. }
  437. static __initdata char ucode_name[] = "kernel/x86/microcode/GenuineIntel.bin";
  438. static __init enum ucode_state
  439. scan_microcode(struct mc_saved_data *mc_saved_data, unsigned long *initrd,
  440. unsigned long start, unsigned long size,
  441. struct ucode_cpu_info *uci)
  442. {
  443. struct cpio_data cd;
  444. long offset = 0;
  445. #ifdef CONFIG_X86_32
  446. char *p = (char *)__pa_nodebug(ucode_name);
  447. #else
  448. char *p = ucode_name;
  449. #endif
  450. cd.data = NULL;
  451. cd.size = 0;
  452. /* try built-in microcode if no initrd */
  453. if (!size) {
  454. if (!load_builtin_intel_microcode(&cd))
  455. return UCODE_ERROR;
  456. } else {
  457. cd = find_cpio_data(p, (void *)start, size, &offset);
  458. if (!cd.data)
  459. return UCODE_ERROR;
  460. }
  461. return get_matching_model_microcode(0, start, cd.data, cd.size,
  462. mc_saved_data, initrd, uci);
  463. }
  464. /*
  465. * Print ucode update info.
  466. */
  467. static void
  468. print_ucode_info(struct ucode_cpu_info *uci, unsigned int date)
  469. {
  470. pr_info_once("microcode updated early to revision 0x%x, date = %04x-%02x-%02x\n",
  471. uci->cpu_sig.rev,
  472. date & 0xffff,
  473. date >> 24,
  474. (date >> 16) & 0xff);
  475. }
  476. #ifdef CONFIG_X86_32
  477. static int delay_ucode_info;
  478. static int current_mc_date;
  479. /*
  480. * Print early updated ucode info after printk works. This is delayed info dump.
  481. */
  482. void show_ucode_info_early(void)
  483. {
  484. struct ucode_cpu_info uci;
  485. if (delay_ucode_info) {
  486. collect_cpu_info_early(&uci);
  487. print_ucode_info(&uci, current_mc_date);
  488. delay_ucode_info = 0;
  489. }
  490. }
  491. /*
  492. * At this point, we can not call printk() yet. Keep microcode patch number in
  493. * mc_saved_data.mc_saved and delay printing microcode info in
  494. * show_ucode_info_early() until printk() works.
  495. */
  496. static void print_ucode(struct ucode_cpu_info *uci)
  497. {
  498. struct microcode_intel *mc_intel;
  499. int *delay_ucode_info_p;
  500. int *current_mc_date_p;
  501. mc_intel = uci->mc;
  502. if (mc_intel == NULL)
  503. return;
  504. delay_ucode_info_p = (int *)__pa_nodebug(&delay_ucode_info);
  505. current_mc_date_p = (int *)__pa_nodebug(&current_mc_date);
  506. *delay_ucode_info_p = 1;
  507. *current_mc_date_p = mc_intel->hdr.date;
  508. }
  509. #else
  510. /*
  511. * Flush global tlb. We only do this in x86_64 where paging has been enabled
  512. * already and PGE should be enabled as well.
  513. */
  514. static inline void flush_tlb_early(void)
  515. {
  516. __native_flush_tlb_global_irq_disabled();
  517. }
  518. static inline void print_ucode(struct ucode_cpu_info *uci)
  519. {
  520. struct microcode_intel *mc_intel;
  521. mc_intel = uci->mc;
  522. if (mc_intel == NULL)
  523. return;
  524. print_ucode_info(uci, mc_intel->hdr.date);
  525. }
  526. #endif
  527. static int apply_microcode_early(struct ucode_cpu_info *uci, bool early)
  528. {
  529. struct microcode_intel *mc_intel;
  530. unsigned int val[2];
  531. mc_intel = uci->mc;
  532. if (mc_intel == NULL)
  533. return 0;
  534. /* write microcode via MSR 0x79 */
  535. native_wrmsr(MSR_IA32_UCODE_WRITE,
  536. (unsigned long) mc_intel->bits,
  537. (unsigned long) mc_intel->bits >> 16 >> 16);
  538. native_wrmsr(MSR_IA32_UCODE_REV, 0, 0);
  539. /* As documented in the SDM: Do a CPUID 1 here */
  540. sync_core();
  541. /* get the current revision from MSR 0x8B */
  542. native_rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]);
  543. if (val[1] != mc_intel->hdr.rev)
  544. return -1;
  545. #ifdef CONFIG_X86_64
  546. /* Flush global tlb. This is precaution. */
  547. flush_tlb_early();
  548. #endif
  549. uci->cpu_sig.rev = val[1];
  550. if (early)
  551. print_ucode(uci);
  552. else
  553. print_ucode_info(uci, mc_intel->hdr.date);
  554. return 0;
  555. }
  556. /*
  557. * This function converts microcode patch offsets previously stored in
  558. * mc_saved_in_initrd to pointers and stores the pointers in mc_saved_data.
  559. */
  560. int __init save_microcode_in_initrd_intel(void)
  561. {
  562. unsigned int count = mc_saved_data.mc_saved_count;
  563. struct microcode_intel *mc_saved[MAX_UCODE_COUNT];
  564. int ret = 0;
  565. if (count == 0)
  566. return ret;
  567. copy_initrd_ptrs(mc_saved, mc_saved_in_initrd, get_initrd_start(), count);
  568. ret = save_microcode(&mc_saved_data, mc_saved, count);
  569. if (ret)
  570. pr_err("Cannot save microcode patches from initrd.\n");
  571. show_saved_mc();
  572. return ret;
  573. }
  574. static void __init
  575. _load_ucode_intel_bsp(struct mc_saved_data *mc_saved_data,
  576. unsigned long *initrd,
  577. unsigned long start, unsigned long size)
  578. {
  579. struct ucode_cpu_info uci;
  580. enum ucode_state ret;
  581. collect_cpu_info_early(&uci);
  582. ret = scan_microcode(mc_saved_data, initrd, start, size, &uci);
  583. if (ret != UCODE_OK)
  584. return;
  585. ret = load_microcode(mc_saved_data, initrd, start, &uci);
  586. if (ret != UCODE_OK)
  587. return;
  588. apply_microcode_early(&uci, true);
  589. }
  590. void __init load_ucode_intel_bsp(void)
  591. {
  592. u64 start, size;
  593. #ifdef CONFIG_X86_32
  594. struct boot_params *p;
  595. p = (struct boot_params *)__pa_nodebug(&boot_params);
  596. size = p->hdr.ramdisk_size;
  597. /*
  598. * Set start only if we have an initrd image. We cannot use initrd_start
  599. * because it is not set that early yet.
  600. */
  601. start = (size ? p->hdr.ramdisk_image : 0);
  602. _load_ucode_intel_bsp((struct mc_saved_data *)__pa_nodebug(&mc_saved_data),
  603. (unsigned long *)__pa_nodebug(&mc_saved_in_initrd),
  604. start, size);
  605. #else
  606. size = boot_params.hdr.ramdisk_size;
  607. start = (size ? boot_params.hdr.ramdisk_image + PAGE_OFFSET : 0);
  608. _load_ucode_intel_bsp(&mc_saved_data, mc_saved_in_initrd, start, size);
  609. #endif
  610. }
  611. void load_ucode_intel_ap(void)
  612. {
  613. struct mc_saved_data *mc_saved_data_p;
  614. struct ucode_cpu_info uci;
  615. unsigned long *mc_saved_in_initrd_p;
  616. enum ucode_state ret;
  617. #ifdef CONFIG_X86_32
  618. mc_saved_in_initrd_p = (unsigned long *)__pa_nodebug(mc_saved_in_initrd);
  619. mc_saved_data_p = (struct mc_saved_data *)__pa_nodebug(&mc_saved_data);
  620. #else
  621. mc_saved_in_initrd_p = mc_saved_in_initrd;
  622. mc_saved_data_p = &mc_saved_data;
  623. #endif
  624. /*
  625. * If there is no valid ucode previously saved in memory, no need to
  626. * update ucode on this AP.
  627. */
  628. if (mc_saved_data_p->mc_saved_count == 0)
  629. return;
  630. collect_cpu_info_early(&uci);
  631. ret = load_microcode(mc_saved_data_p, mc_saved_in_initrd_p,
  632. get_initrd_start_addr(), &uci);
  633. if (ret != UCODE_OK)
  634. return;
  635. apply_microcode_early(&uci, true);
  636. }
  637. void reload_ucode_intel(void)
  638. {
  639. struct ucode_cpu_info uci;
  640. enum ucode_state ret;
  641. if (!mc_saved_data.mc_saved_count)
  642. return;
  643. collect_cpu_info_early(&uci);
  644. ret = load_microcode_early(mc_saved_data.mc_saved,
  645. mc_saved_data.mc_saved_count, &uci);
  646. if (ret != UCODE_OK)
  647. return;
  648. apply_microcode_early(&uci, false);
  649. }
  650. static int collect_cpu_info(int cpu_num, struct cpu_signature *csig)
  651. {
  652. struct cpuinfo_x86 *c = &cpu_data(cpu_num);
  653. unsigned int val[2];
  654. memset(csig, 0, sizeof(*csig));
  655. csig->sig = cpuid_eax(0x00000001);
  656. if ((c->x86_model >= 5) || (c->x86 > 6)) {
  657. /* get processor flags from MSR 0x17 */
  658. rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]);
  659. csig->pf = 1 << ((val[1] >> 18) & 7);
  660. }
  661. csig->rev = c->microcode;
  662. pr_info("CPU%d sig=0x%x, pf=0x%x, revision=0x%x\n",
  663. cpu_num, csig->sig, csig->pf, csig->rev);
  664. return 0;
  665. }
  666. /*
  667. * return 0 - no update found
  668. * return 1 - found update
  669. */
  670. static int get_matching_mc(struct microcode_intel *mc_intel, int cpu)
  671. {
  672. struct cpu_signature cpu_sig;
  673. unsigned int csig, cpf, crev;
  674. collect_cpu_info(cpu, &cpu_sig);
  675. csig = cpu_sig.sig;
  676. cpf = cpu_sig.pf;
  677. crev = cpu_sig.rev;
  678. return has_newer_microcode(mc_intel, csig, cpf, crev);
  679. }
  680. static int apply_microcode_intel(int cpu)
  681. {
  682. struct microcode_intel *mc_intel;
  683. struct ucode_cpu_info *uci;
  684. unsigned int val[2];
  685. int cpu_num = raw_smp_processor_id();
  686. struct cpuinfo_x86 *c = &cpu_data(cpu_num);
  687. uci = ucode_cpu_info + cpu;
  688. mc_intel = uci->mc;
  689. /* We should bind the task to the CPU */
  690. BUG_ON(cpu_num != cpu);
  691. if (mc_intel == NULL)
  692. return 0;
  693. /*
  694. * Microcode on this CPU could be updated earlier. Only apply the
  695. * microcode patch in mc_intel when it is newer than the one on this
  696. * CPU.
  697. */
  698. if (get_matching_mc(mc_intel, cpu) == 0)
  699. return 0;
  700. /* write microcode via MSR 0x79 */
  701. wrmsr(MSR_IA32_UCODE_WRITE,
  702. (unsigned long) mc_intel->bits,
  703. (unsigned long) mc_intel->bits >> 16 >> 16);
  704. wrmsr(MSR_IA32_UCODE_REV, 0, 0);
  705. /* As documented in the SDM: Do a CPUID 1 here */
  706. sync_core();
  707. /* get the current revision from MSR 0x8B */
  708. rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]);
  709. if (val[1] != mc_intel->hdr.rev) {
  710. pr_err("CPU%d update to revision 0x%x failed\n",
  711. cpu_num, mc_intel->hdr.rev);
  712. return -1;
  713. }
  714. pr_info("CPU%d updated to revision 0x%x, date = %04x-%02x-%02x\n",
  715. cpu_num, val[1],
  716. mc_intel->hdr.date & 0xffff,
  717. mc_intel->hdr.date >> 24,
  718. (mc_intel->hdr.date >> 16) & 0xff);
  719. uci->cpu_sig.rev = val[1];
  720. c->microcode = val[1];
  721. return 0;
  722. }
  723. static enum ucode_state generic_load_microcode(int cpu, void *data, size_t size,
  724. int (*get_ucode_data)(void *, const void *, size_t))
  725. {
  726. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  727. u8 *ucode_ptr = data, *new_mc = NULL, *mc = NULL;
  728. int new_rev = uci->cpu_sig.rev;
  729. unsigned int leftover = size;
  730. enum ucode_state state = UCODE_OK;
  731. unsigned int curr_mc_size = 0;
  732. unsigned int csig, cpf;
  733. while (leftover) {
  734. struct microcode_header_intel mc_header;
  735. unsigned int mc_size;
  736. if (leftover < sizeof(mc_header)) {
  737. pr_err("error! Truncated header in microcode data file\n");
  738. break;
  739. }
  740. if (get_ucode_data(&mc_header, ucode_ptr, sizeof(mc_header)))
  741. break;
  742. mc_size = get_totalsize(&mc_header);
  743. if (!mc_size || mc_size > leftover) {
  744. pr_err("error! Bad data in microcode data file\n");
  745. break;
  746. }
  747. /* For performance reasons, reuse mc area when possible */
  748. if (!mc || mc_size > curr_mc_size) {
  749. vfree(mc);
  750. mc = vmalloc(mc_size);
  751. if (!mc)
  752. break;
  753. curr_mc_size = mc_size;
  754. }
  755. if (get_ucode_data(mc, ucode_ptr, mc_size) ||
  756. microcode_sanity_check(mc, 1) < 0) {
  757. break;
  758. }
  759. csig = uci->cpu_sig.sig;
  760. cpf = uci->cpu_sig.pf;
  761. if (has_newer_microcode(mc, csig, cpf, new_rev)) {
  762. vfree(new_mc);
  763. new_rev = mc_header.rev;
  764. new_mc = mc;
  765. mc = NULL; /* trigger new vmalloc */
  766. }
  767. ucode_ptr += mc_size;
  768. leftover -= mc_size;
  769. }
  770. vfree(mc);
  771. if (leftover) {
  772. vfree(new_mc);
  773. state = UCODE_ERROR;
  774. goto out;
  775. }
  776. if (!new_mc) {
  777. state = UCODE_NFOUND;
  778. goto out;
  779. }
  780. vfree(uci->mc);
  781. uci->mc = (struct microcode_intel *)new_mc;
  782. /*
  783. * If early loading microcode is supported, save this mc into
  784. * permanent memory. So it will be loaded early when a CPU is hot added
  785. * or resumes.
  786. */
  787. save_mc_for_early(new_mc);
  788. pr_debug("CPU%d found a matching microcode update with version 0x%x (current=0x%x)\n",
  789. cpu, new_rev, uci->cpu_sig.rev);
  790. out:
  791. return state;
  792. }
  793. static int get_ucode_fw(void *to, const void *from, size_t n)
  794. {
  795. memcpy(to, from, n);
  796. return 0;
  797. }
  798. static enum ucode_state request_microcode_fw(int cpu, struct device *device,
  799. bool refresh_fw)
  800. {
  801. char name[30];
  802. struct cpuinfo_x86 *c = &cpu_data(cpu);
  803. const struct firmware *firmware;
  804. enum ucode_state ret;
  805. sprintf(name, "intel-ucode/%02x-%02x-%02x",
  806. c->x86, c->x86_model, c->x86_mask);
  807. if (request_firmware_direct(&firmware, name, device)) {
  808. pr_debug("data file %s load failed\n", name);
  809. return UCODE_NFOUND;
  810. }
  811. ret = generic_load_microcode(cpu, (void *)firmware->data,
  812. firmware->size, &get_ucode_fw);
  813. release_firmware(firmware);
  814. return ret;
  815. }
  816. static int get_ucode_user(void *to, const void *from, size_t n)
  817. {
  818. return copy_from_user(to, from, n);
  819. }
  820. static enum ucode_state
  821. request_microcode_user(int cpu, const void __user *buf, size_t size)
  822. {
  823. return generic_load_microcode(cpu, (void *)buf, size, &get_ucode_user);
  824. }
  825. static void microcode_fini_cpu(int cpu)
  826. {
  827. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  828. vfree(uci->mc);
  829. uci->mc = NULL;
  830. }
  831. static struct microcode_ops microcode_intel_ops = {
  832. .request_microcode_user = request_microcode_user,
  833. .request_microcode_fw = request_microcode_fw,
  834. .collect_cpu_info = collect_cpu_info,
  835. .apply_microcode = apply_microcode_intel,
  836. .microcode_fini_cpu = microcode_fini_cpu,
  837. };
  838. struct microcode_ops * __init init_intel_microcode(void)
  839. {
  840. struct cpuinfo_x86 *c = &boot_cpu_data;
  841. if (c->x86_vendor != X86_VENDOR_INTEL || c->x86 < 6 ||
  842. cpu_has(c, X86_FEATURE_IA64)) {
  843. pr_err("Intel CPU family 0x%x not supported\n", c->x86);
  844. return NULL;
  845. }
  846. return &microcode_intel_ops;
  847. }