intel.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058
  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. #ifdef DEBUG
  319. static void show_saved_mc(void)
  320. {
  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. }
  367. #else
  368. static inline void show_saved_mc(void)
  369. {
  370. }
  371. #endif
  372. #ifdef CONFIG_HOTPLUG_CPU
  373. static DEFINE_MUTEX(x86_cpu_microcode_mutex);
  374. /*
  375. * Save this mc into mc_saved_data. So it will be loaded early when a CPU is
  376. * hot added or resumes.
  377. *
  378. * Please make sure this mc should be a valid microcode patch before calling
  379. * this function.
  380. */
  381. int save_mc_for_early(u8 *mc)
  382. {
  383. struct microcode_intel *mc_saved_tmp[MAX_UCODE_COUNT];
  384. unsigned int mc_saved_count_init;
  385. unsigned int mc_saved_count;
  386. struct microcode_intel **mc_saved;
  387. int ret = 0;
  388. int i;
  389. /*
  390. * Hold hotplug lock so mc_saved_data is not accessed by a CPU in
  391. * hotplug.
  392. */
  393. mutex_lock(&x86_cpu_microcode_mutex);
  394. mc_saved_count_init = mc_saved_data.mc_saved_count;
  395. mc_saved_count = mc_saved_data.mc_saved_count;
  396. mc_saved = mc_saved_data.mc_saved;
  397. if (mc_saved && mc_saved_count)
  398. memcpy(mc_saved_tmp, mc_saved,
  399. mc_saved_count * sizeof(struct microcode_intel *));
  400. /*
  401. * Save the microcode patch mc in mc_save_tmp structure if it's a newer
  402. * version.
  403. */
  404. mc_saved_count = _save_mc(mc_saved_tmp, mc, mc_saved_count);
  405. /*
  406. * Save the mc_save_tmp in global mc_saved_data.
  407. */
  408. ret = save_microcode(&mc_saved_data, mc_saved_tmp, mc_saved_count);
  409. if (ret) {
  410. pr_err("Cannot save microcode patch.\n");
  411. goto out;
  412. }
  413. show_saved_mc();
  414. /*
  415. * Free old saved microcode data.
  416. */
  417. if (mc_saved) {
  418. for (i = 0; i < mc_saved_count_init; i++)
  419. kfree(mc_saved[i]);
  420. kfree(mc_saved);
  421. }
  422. out:
  423. mutex_unlock(&x86_cpu_microcode_mutex);
  424. return ret;
  425. }
  426. EXPORT_SYMBOL_GPL(save_mc_for_early);
  427. #endif
  428. static bool __init load_builtin_intel_microcode(struct cpio_data *cp)
  429. {
  430. #ifdef CONFIG_X86_64
  431. unsigned int eax = 0x00000001, ebx, ecx = 0, edx;
  432. unsigned int family, model, stepping;
  433. char name[30];
  434. native_cpuid(&eax, &ebx, &ecx, &edx);
  435. family = __x86_family(eax);
  436. model = x86_model(eax);
  437. stepping = eax & 0xf;
  438. sprintf(name, "intel-ucode/%02x-%02x-%02x", family, model, stepping);
  439. return get_builtin_firmware(cp, name);
  440. #else
  441. return false;
  442. #endif
  443. }
  444. static __initdata char ucode_name[] = "kernel/x86/microcode/GenuineIntel.bin";
  445. static __init enum ucode_state
  446. scan_microcode(struct mc_saved_data *mc_saved_data, unsigned long *initrd,
  447. unsigned long start, unsigned long size,
  448. struct ucode_cpu_info *uci)
  449. {
  450. struct cpio_data cd;
  451. long offset = 0;
  452. #ifdef CONFIG_X86_32
  453. char *p = (char *)__pa_nodebug(ucode_name);
  454. #else
  455. char *p = ucode_name;
  456. #endif
  457. cd.data = NULL;
  458. cd.size = 0;
  459. cd = find_cpio_data(p, (void *)start, size, &offset);
  460. if (!cd.data) {
  461. if (!load_builtin_intel_microcode(&cd))
  462. return UCODE_ERROR;
  463. }
  464. return get_matching_model_microcode(0, start, cd.data, cd.size,
  465. mc_saved_data, initrd, uci);
  466. }
  467. /*
  468. * Print ucode update info.
  469. */
  470. static void
  471. print_ucode_info(struct ucode_cpu_info *uci, unsigned int date)
  472. {
  473. int cpu = smp_processor_id();
  474. pr_info("CPU%d microcode updated early to revision 0x%x, date = %04x-%02x-%02x\n",
  475. cpu,
  476. uci->cpu_sig.rev,
  477. date & 0xffff,
  478. date >> 24,
  479. (date >> 16) & 0xff);
  480. }
  481. #ifdef CONFIG_X86_32
  482. static int delay_ucode_info;
  483. static int current_mc_date;
  484. /*
  485. * Print early updated ucode info after printk works. This is delayed info dump.
  486. */
  487. void show_ucode_info_early(void)
  488. {
  489. struct ucode_cpu_info uci;
  490. if (delay_ucode_info) {
  491. collect_cpu_info_early(&uci);
  492. print_ucode_info(&uci, current_mc_date);
  493. delay_ucode_info = 0;
  494. }
  495. }
  496. /*
  497. * At this point, we can not call printk() yet. Keep microcode patch number in
  498. * mc_saved_data.mc_saved and delay printing microcode info in
  499. * show_ucode_info_early() until printk() works.
  500. */
  501. static void print_ucode(struct ucode_cpu_info *uci)
  502. {
  503. struct microcode_intel *mc_intel;
  504. int *delay_ucode_info_p;
  505. int *current_mc_date_p;
  506. mc_intel = uci->mc;
  507. if (mc_intel == NULL)
  508. return;
  509. delay_ucode_info_p = (int *)__pa_nodebug(&delay_ucode_info);
  510. current_mc_date_p = (int *)__pa_nodebug(&current_mc_date);
  511. *delay_ucode_info_p = 1;
  512. *current_mc_date_p = mc_intel->hdr.date;
  513. }
  514. #else
  515. /*
  516. * Flush global tlb. We only do this in x86_64 where paging has been enabled
  517. * already and PGE should be enabled as well.
  518. */
  519. static inline void flush_tlb_early(void)
  520. {
  521. __native_flush_tlb_global_irq_disabled();
  522. }
  523. static inline void print_ucode(struct ucode_cpu_info *uci)
  524. {
  525. struct microcode_intel *mc_intel;
  526. mc_intel = uci->mc;
  527. if (mc_intel == NULL)
  528. return;
  529. print_ucode_info(uci, mc_intel->hdr.date);
  530. }
  531. #endif
  532. static int apply_microcode_early(struct ucode_cpu_info *uci, bool early)
  533. {
  534. struct microcode_intel *mc_intel;
  535. unsigned int val[2];
  536. mc_intel = uci->mc;
  537. if (mc_intel == NULL)
  538. return 0;
  539. /* write microcode via MSR 0x79 */
  540. native_wrmsr(MSR_IA32_UCODE_WRITE,
  541. (unsigned long) mc_intel->bits,
  542. (unsigned long) mc_intel->bits >> 16 >> 16);
  543. native_wrmsr(MSR_IA32_UCODE_REV, 0, 0);
  544. /* As documented in the SDM: Do a CPUID 1 here */
  545. sync_core();
  546. /* get the current revision from MSR 0x8B */
  547. native_rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]);
  548. if (val[1] != mc_intel->hdr.rev)
  549. return -1;
  550. #ifdef CONFIG_X86_64
  551. /* Flush global tlb. This is precaution. */
  552. flush_tlb_early();
  553. #endif
  554. uci->cpu_sig.rev = val[1];
  555. if (early)
  556. print_ucode(uci);
  557. else
  558. print_ucode_info(uci, mc_intel->hdr.date);
  559. return 0;
  560. }
  561. /*
  562. * This function converts microcode patch offsets previously stored in
  563. * mc_saved_in_initrd to pointers and stores the pointers in mc_saved_data.
  564. */
  565. int __init save_microcode_in_initrd_intel(void)
  566. {
  567. unsigned int count = mc_saved_data.mc_saved_count;
  568. struct microcode_intel *mc_saved[MAX_UCODE_COUNT];
  569. int ret = 0;
  570. if (count == 0)
  571. return ret;
  572. copy_initrd_ptrs(mc_saved, mc_saved_in_initrd, initrd_start, count);
  573. ret = save_microcode(&mc_saved_data, mc_saved, count);
  574. if (ret)
  575. pr_err("Cannot save microcode patches from initrd.\n");
  576. show_saved_mc();
  577. return ret;
  578. }
  579. static void __init
  580. _load_ucode_intel_bsp(struct mc_saved_data *mc_saved_data,
  581. unsigned long *initrd,
  582. unsigned long start, unsigned long size)
  583. {
  584. struct ucode_cpu_info uci;
  585. enum ucode_state ret;
  586. collect_cpu_info_early(&uci);
  587. ret = scan_microcode(mc_saved_data, initrd, start, size, &uci);
  588. if (ret != UCODE_OK)
  589. return;
  590. ret = load_microcode(mc_saved_data, initrd, start, &uci);
  591. if (ret != UCODE_OK)
  592. return;
  593. apply_microcode_early(&uci, true);
  594. }
  595. void __init load_ucode_intel_bsp(void)
  596. {
  597. u64 start, size;
  598. #ifdef CONFIG_X86_32
  599. struct boot_params *p;
  600. p = (struct boot_params *)__pa_nodebug(&boot_params);
  601. start = p->hdr.ramdisk_image;
  602. size = p->hdr.ramdisk_size;
  603. _load_ucode_intel_bsp(
  604. (struct mc_saved_data *)__pa_nodebug(&mc_saved_data),
  605. (unsigned long *)__pa_nodebug(&mc_saved_in_initrd),
  606. start, size);
  607. #else
  608. start = boot_params.hdr.ramdisk_image + PAGE_OFFSET;
  609. size = boot_params.hdr.ramdisk_size;
  610. _load_ucode_intel_bsp(&mc_saved_data, mc_saved_in_initrd, start, size);
  611. #endif
  612. }
  613. void load_ucode_intel_ap(void)
  614. {
  615. struct mc_saved_data *mc_saved_data_p;
  616. struct ucode_cpu_info uci;
  617. unsigned long *mc_saved_in_initrd_p;
  618. unsigned long initrd_start_addr;
  619. enum ucode_state ret;
  620. #ifdef CONFIG_X86_32
  621. unsigned long *initrd_start_p;
  622. mc_saved_in_initrd_p =
  623. (unsigned long *)__pa_nodebug(mc_saved_in_initrd);
  624. mc_saved_data_p = (struct mc_saved_data *)__pa_nodebug(&mc_saved_data);
  625. initrd_start_p = (unsigned long *)__pa_nodebug(&initrd_start);
  626. initrd_start_addr = (unsigned long)__pa_nodebug(*initrd_start_p);
  627. #else
  628. mc_saved_data_p = &mc_saved_data;
  629. mc_saved_in_initrd_p = mc_saved_in_initrd;
  630. initrd_start_addr = initrd_start;
  631. #endif
  632. /*
  633. * If there is no valid ucode previously saved in memory, no need to
  634. * update ucode on this AP.
  635. */
  636. if (mc_saved_data_p->mc_saved_count == 0)
  637. return;
  638. collect_cpu_info_early(&uci);
  639. ret = load_microcode(mc_saved_data_p, mc_saved_in_initrd_p,
  640. initrd_start_addr, &uci);
  641. if (ret != UCODE_OK)
  642. return;
  643. apply_microcode_early(&uci, true);
  644. }
  645. void reload_ucode_intel(void)
  646. {
  647. struct ucode_cpu_info uci;
  648. enum ucode_state ret;
  649. if (!mc_saved_data.mc_saved_count)
  650. return;
  651. collect_cpu_info_early(&uci);
  652. ret = load_microcode_early(mc_saved_data.mc_saved,
  653. mc_saved_data.mc_saved_count, &uci);
  654. if (ret != UCODE_OK)
  655. return;
  656. apply_microcode_early(&uci, false);
  657. }
  658. static int collect_cpu_info(int cpu_num, struct cpu_signature *csig)
  659. {
  660. struct cpuinfo_x86 *c = &cpu_data(cpu_num);
  661. unsigned int val[2];
  662. memset(csig, 0, sizeof(*csig));
  663. csig->sig = cpuid_eax(0x00000001);
  664. if ((c->x86_model >= 5) || (c->x86 > 6)) {
  665. /* get processor flags from MSR 0x17 */
  666. rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]);
  667. csig->pf = 1 << ((val[1] >> 18) & 7);
  668. }
  669. csig->rev = c->microcode;
  670. pr_info("CPU%d sig=0x%x, pf=0x%x, revision=0x%x\n",
  671. cpu_num, csig->sig, csig->pf, csig->rev);
  672. return 0;
  673. }
  674. /*
  675. * return 0 - no update found
  676. * return 1 - found update
  677. */
  678. static int get_matching_mc(struct microcode_intel *mc_intel, int cpu)
  679. {
  680. struct cpu_signature cpu_sig;
  681. unsigned int csig, cpf, crev;
  682. collect_cpu_info(cpu, &cpu_sig);
  683. csig = cpu_sig.sig;
  684. cpf = cpu_sig.pf;
  685. crev = cpu_sig.rev;
  686. return has_newer_microcode(mc_intel, csig, cpf, crev);
  687. }
  688. static int apply_microcode_intel(int cpu)
  689. {
  690. struct microcode_intel *mc_intel;
  691. struct ucode_cpu_info *uci;
  692. unsigned int val[2];
  693. int cpu_num = raw_smp_processor_id();
  694. struct cpuinfo_x86 *c = &cpu_data(cpu_num);
  695. uci = ucode_cpu_info + cpu;
  696. mc_intel = uci->mc;
  697. /* We should bind the task to the CPU */
  698. BUG_ON(cpu_num != cpu);
  699. if (mc_intel == NULL)
  700. return 0;
  701. /*
  702. * Microcode on this CPU could be updated earlier. Only apply the
  703. * microcode patch in mc_intel when it is newer than the one on this
  704. * CPU.
  705. */
  706. if (get_matching_mc(mc_intel, cpu) == 0)
  707. return 0;
  708. /* write microcode via MSR 0x79 */
  709. wrmsr(MSR_IA32_UCODE_WRITE,
  710. (unsigned long) mc_intel->bits,
  711. (unsigned long) mc_intel->bits >> 16 >> 16);
  712. wrmsr(MSR_IA32_UCODE_REV, 0, 0);
  713. /* As documented in the SDM: Do a CPUID 1 here */
  714. sync_core();
  715. /* get the current revision from MSR 0x8B */
  716. rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]);
  717. if (val[1] != mc_intel->hdr.rev) {
  718. pr_err("CPU%d update to revision 0x%x failed\n",
  719. cpu_num, mc_intel->hdr.rev);
  720. return -1;
  721. }
  722. pr_info("CPU%d updated to revision 0x%x, date = %04x-%02x-%02x\n",
  723. cpu_num, val[1],
  724. mc_intel->hdr.date & 0xffff,
  725. mc_intel->hdr.date >> 24,
  726. (mc_intel->hdr.date >> 16) & 0xff);
  727. uci->cpu_sig.rev = val[1];
  728. c->microcode = val[1];
  729. return 0;
  730. }
  731. static enum ucode_state generic_load_microcode(int cpu, void *data, size_t size,
  732. int (*get_ucode_data)(void *, const void *, size_t))
  733. {
  734. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  735. u8 *ucode_ptr = data, *new_mc = NULL, *mc = NULL;
  736. int new_rev = uci->cpu_sig.rev;
  737. unsigned int leftover = size;
  738. enum ucode_state state = UCODE_OK;
  739. unsigned int curr_mc_size = 0;
  740. unsigned int csig, cpf;
  741. while (leftover) {
  742. struct microcode_header_intel mc_header;
  743. unsigned int mc_size;
  744. if (leftover < sizeof(mc_header)) {
  745. pr_err("error! Truncated header in microcode data file\n");
  746. break;
  747. }
  748. if (get_ucode_data(&mc_header, ucode_ptr, sizeof(mc_header)))
  749. break;
  750. mc_size = get_totalsize(&mc_header);
  751. if (!mc_size || mc_size > leftover) {
  752. pr_err("error! Bad data in microcode data file\n");
  753. break;
  754. }
  755. /* For performance reasons, reuse mc area when possible */
  756. if (!mc || mc_size > curr_mc_size) {
  757. vfree(mc);
  758. mc = vmalloc(mc_size);
  759. if (!mc)
  760. break;
  761. curr_mc_size = mc_size;
  762. }
  763. if (get_ucode_data(mc, ucode_ptr, mc_size) ||
  764. microcode_sanity_check(mc, 1) < 0) {
  765. break;
  766. }
  767. csig = uci->cpu_sig.sig;
  768. cpf = uci->cpu_sig.pf;
  769. if (has_newer_microcode(mc, csig, cpf, new_rev)) {
  770. vfree(new_mc);
  771. new_rev = mc_header.rev;
  772. new_mc = mc;
  773. mc = NULL; /* trigger new vmalloc */
  774. }
  775. ucode_ptr += mc_size;
  776. leftover -= mc_size;
  777. }
  778. vfree(mc);
  779. if (leftover) {
  780. vfree(new_mc);
  781. state = UCODE_ERROR;
  782. goto out;
  783. }
  784. if (!new_mc) {
  785. state = UCODE_NFOUND;
  786. goto out;
  787. }
  788. vfree(uci->mc);
  789. uci->mc = (struct microcode_intel *)new_mc;
  790. /*
  791. * If early loading microcode is supported, save this mc into
  792. * permanent memory. So it will be loaded early when a CPU is hot added
  793. * or resumes.
  794. */
  795. save_mc_for_early(new_mc);
  796. pr_debug("CPU%d found a matching microcode update with version 0x%x (current=0x%x)\n",
  797. cpu, new_rev, uci->cpu_sig.rev);
  798. out:
  799. return state;
  800. }
  801. static int get_ucode_fw(void *to, const void *from, size_t n)
  802. {
  803. memcpy(to, from, n);
  804. return 0;
  805. }
  806. static enum ucode_state request_microcode_fw(int cpu, struct device *device,
  807. bool refresh_fw)
  808. {
  809. char name[30];
  810. struct cpuinfo_x86 *c = &cpu_data(cpu);
  811. const struct firmware *firmware;
  812. enum ucode_state ret;
  813. sprintf(name, "intel-ucode/%02x-%02x-%02x",
  814. c->x86, c->x86_model, c->x86_mask);
  815. if (request_firmware_direct(&firmware, name, device)) {
  816. pr_debug("data file %s load failed\n", name);
  817. return UCODE_NFOUND;
  818. }
  819. ret = generic_load_microcode(cpu, (void *)firmware->data,
  820. firmware->size, &get_ucode_fw);
  821. release_firmware(firmware);
  822. return ret;
  823. }
  824. static int get_ucode_user(void *to, const void *from, size_t n)
  825. {
  826. return copy_from_user(to, from, n);
  827. }
  828. static enum ucode_state
  829. request_microcode_user(int cpu, const void __user *buf, size_t size)
  830. {
  831. return generic_load_microcode(cpu, (void *)buf, size, &get_ucode_user);
  832. }
  833. static void microcode_fini_cpu(int cpu)
  834. {
  835. struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
  836. vfree(uci->mc);
  837. uci->mc = NULL;
  838. }
  839. static struct microcode_ops microcode_intel_ops = {
  840. .request_microcode_user = request_microcode_user,
  841. .request_microcode_fw = request_microcode_fw,
  842. .collect_cpu_info = collect_cpu_info,
  843. .apply_microcode = apply_microcode_intel,
  844. .microcode_fini_cpu = microcode_fini_cpu,
  845. };
  846. struct microcode_ops * __init init_intel_microcode(void)
  847. {
  848. struct cpuinfo_x86 *c = &boot_cpu_data;
  849. if (c->x86_vendor != X86_VENDOR_INTEL || c->x86 < 6 ||
  850. cpu_has(c, X86_FEATURE_IA64)) {
  851. pr_err("Intel CPU family 0x%x not supported\n", c->x86);
  852. return NULL;
  853. }
  854. return &microcode_intel_ops;
  855. }