GNU Linux-libre 4.4.292-gnu1
[releases.git] / arch / x86 / kernel / cpu / intel.c
1 #include <linux/kernel.h>
2
3 #include <linux/string.h>
4 #include <linux/bitops.h>
5 #include <linux/smp.h>
6 #include <linux/sched.h>
7 #include <linux/thread_info.h>
8 #include <linux/module.h>
9 #include <linux/uaccess.h>
10
11 #include <asm/cpufeature.h>
12 #include <asm/pgtable.h>
13 #include <asm/msr.h>
14 #include <asm/bugs.h>
15 #include <asm/cpu.h>
16 #include <asm/intel-family.h>
17 #include <asm/microcode_intel.h>
18
19 #ifdef CONFIG_X86_64
20 #include <linux/topology.h>
21 #endif
22
23 #include "cpu.h"
24
25 #ifdef CONFIG_X86_LOCAL_APIC
26 #include <asm/mpspec.h>
27 #include <asm/apic.h>
28 #endif
29
30 /*
31  * Early microcode releases for the Spectre v2 mitigation were broken.
32  * Information taken from;
33  * - https://newsroom.intel.com/wp-content/uploads/sites/11/2018/03/microcode-update-guidance.pdf
34  * - https://kb.vmware.com/s/article/52345
35  * - Microcode revisions observed in the wild
36  * - Release note from 20180108 microcode release
37  */
38 struct sku_microcode {
39         u8 model;
40         u8 stepping;
41         u32 microcode;
42 };
43 static const struct sku_microcode spectre_bad_microcodes[] = {
44         { INTEL_FAM6_KABYLAKE_DESKTOP,  0x0B,   0x80 },
45         { INTEL_FAM6_KABYLAKE_DESKTOP,  0x0A,   0x80 },
46         { INTEL_FAM6_KABYLAKE_DESKTOP,  0x09,   0x80 },
47         { INTEL_FAM6_KABYLAKE_MOBILE,   0x0A,   0x80 },
48         { INTEL_FAM6_KABYLAKE_MOBILE,   0x09,   0x80 },
49         { INTEL_FAM6_SKYLAKE_X,         0x03,   0x0100013e },
50         { INTEL_FAM6_SKYLAKE_X,         0x04,   0x0200003c },
51         { INTEL_FAM6_BROADWELL_CORE,    0x04,   0x28 },
52         { INTEL_FAM6_BROADWELL_GT3E,    0x01,   0x1b },
53         { INTEL_FAM6_BROADWELL_XEON_D,  0x02,   0x14 },
54         { INTEL_FAM6_BROADWELL_XEON_D,  0x03,   0x07000011 },
55         { INTEL_FAM6_BROADWELL_X,       0x01,   0x0b000025 },
56         { INTEL_FAM6_HASWELL_ULT,       0x01,   0x21 },
57         { INTEL_FAM6_HASWELL_GT3E,      0x01,   0x18 },
58         { INTEL_FAM6_HASWELL_CORE,      0x03,   0x23 },
59         { INTEL_FAM6_HASWELL_X,         0x02,   0x3b },
60         { INTEL_FAM6_HASWELL_X,         0x04,   0x10 },
61         { INTEL_FAM6_IVYBRIDGE_X,       0x04,   0x42a },
62         /* Observed in the wild */
63         { INTEL_FAM6_SANDYBRIDGE_X,     0x06,   0x61b },
64         { INTEL_FAM6_SANDYBRIDGE_X,     0x07,   0x712 },
65 };
66
67 static bool bad_spectre_microcode(struct cpuinfo_x86 *c)
68 {
69         int i;
70
71         /*
72          * We know that the hypervisor lie to us on the microcode version so
73          * we may as well hope that it is running the correct version.
74          */
75         if (cpu_has(c, X86_FEATURE_HYPERVISOR))
76                 return false;
77
78         if (c->x86 != 6)
79                 return false;
80
81         for (i = 0; i < ARRAY_SIZE(spectre_bad_microcodes); i++) {
82                 if (c->x86_model == spectre_bad_microcodes[i].model &&
83                     c->x86_stepping == spectre_bad_microcodes[i].stepping)
84                         return (c->microcode <= spectre_bad_microcodes[i].microcode);
85         }
86         return false;
87 }
88
89 static void early_init_intel(struct cpuinfo_x86 *c)
90 {
91         u64 misc_enable;
92
93         /* Unmask CPUID levels if masked: */
94         if (c->x86 > 6 || (c->x86 == 6 && c->x86_model >= 0xd)) {
95                 if (msr_clear_bit(MSR_IA32_MISC_ENABLE,
96                                   MSR_IA32_MISC_ENABLE_LIMIT_CPUID_BIT) > 0) {
97                         c->cpuid_level = cpuid_eax(0);
98                         get_cpu_cap(c);
99                 }
100         }
101
102         if ((c->x86 == 0xf && c->x86_model >= 0x03) ||
103                 (c->x86 == 0x6 && c->x86_model >= 0x0e))
104                 set_cpu_cap(c, X86_FEATURE_CONSTANT_TSC);
105
106         if (c->x86 >= 6 && !cpu_has(c, X86_FEATURE_IA64))
107                 c->microcode = intel_get_microcode_revision();
108
109         /* Now if any of them are set, check the blacklist and clear the lot */
110         if ((cpu_has(c, X86_FEATURE_SPEC_CTRL) ||
111              cpu_has(c, X86_FEATURE_INTEL_STIBP) ||
112              cpu_has(c, X86_FEATURE_IBRS) || cpu_has(c, X86_FEATURE_IBPB) ||
113              cpu_has(c, X86_FEATURE_STIBP)) && bad_spectre_microcode(c)) {
114                 pr_warn("Intel Spectre v2 broken microcode detected; disabling Speculation Control\n");
115                 setup_clear_cpu_cap(X86_FEATURE_IBRS);
116                 setup_clear_cpu_cap(X86_FEATURE_IBPB);
117                 setup_clear_cpu_cap(X86_FEATURE_STIBP);
118                 setup_clear_cpu_cap(X86_FEATURE_SPEC_CTRL);
119                 setup_clear_cpu_cap(X86_FEATURE_MSR_SPEC_CTRL);
120                 setup_clear_cpu_cap(X86_FEATURE_INTEL_STIBP);
121                 setup_clear_cpu_cap(X86_FEATURE_SSBD);
122                 setup_clear_cpu_cap(X86_FEATURE_SPEC_CTRL_SSBD);
123         }
124
125         /*
126          * Atom erratum AAE44/AAF40/AAG38/AAH41:
127          *
128          * A race condition between speculative fetches and invalidating
129          * a large page.  This is worked around in microcode, but we
130          * need the microcode to have already been loaded... so if it is
131          * not, recommend a BIOS update and disable large pages.
132          */
133         if (c->x86 == 6 && c->x86_model == 0x1c && c->x86_stepping <= 2 &&
134             c->microcode < 0x20e) {
135                 printk(KERN_WARNING "Atom PSE erratum detected, BIOS microcode update recommended\n");
136                 clear_cpu_cap(c, X86_FEATURE_PSE);
137         }
138
139 #ifdef CONFIG_X86_64
140         set_cpu_cap(c, X86_FEATURE_SYSENTER32);
141 #else
142         /* Netburst reports 64 bytes clflush size, but does IO in 128 bytes */
143         if (c->x86 == 15 && c->x86_cache_alignment == 64)
144                 c->x86_cache_alignment = 128;
145 #endif
146
147         /* CPUID workaround for 0F33/0F34 CPU */
148         if (c->x86 == 0xF && c->x86_model == 0x3
149             && (c->x86_stepping == 0x3 || c->x86_stepping == 0x4))
150                 c->x86_phys_bits = 36;
151
152         /*
153          * c->x86_power is 8000_0007 edx. Bit 8 is TSC runs at constant rate
154          * with P/T states and does not stop in deep C-states.
155          *
156          * It is also reliable across cores and sockets. (but not across
157          * cabinets - we turn it off in that case explicitly.)
158          */
159         if (c->x86_power & (1 << 8)) {
160                 set_cpu_cap(c, X86_FEATURE_CONSTANT_TSC);
161                 set_cpu_cap(c, X86_FEATURE_NONSTOP_TSC);
162                 if (!check_tsc_unstable())
163                         set_sched_clock_stable();
164         }
165
166         /* Penwell and Cloverview have the TSC which doesn't sleep on S3 */
167         if (c->x86 == 6) {
168                 switch (c->x86_model) {
169                 case 0x27:      /* Penwell */
170                 case 0x35:      /* Cloverview */
171                 case 0x4a:      /* Merrifield */
172                         set_cpu_cap(c, X86_FEATURE_NONSTOP_TSC_S3);
173                         break;
174                 default:
175                         break;
176                 }
177         }
178
179         /*
180          * There is a known erratum on Pentium III and Core Solo
181          * and Core Duo CPUs.
182          * " Page with PAT set to WC while associated MTRR is UC
183          *   may consolidate to UC "
184          * Because of this erratum, it is better to stick with
185          * setting WC in MTRR rather than using PAT on these CPUs.
186          *
187          * Enable PAT WC only on P4, Core 2 or later CPUs.
188          */
189         if (c->x86 == 6 && c->x86_model < 15)
190                 clear_cpu_cap(c, X86_FEATURE_PAT);
191
192 #ifdef CONFIG_KMEMCHECK
193         /*
194          * P4s have a "fast strings" feature which causes single-
195          * stepping REP instructions to only generate a #DB on
196          * cache-line boundaries.
197          *
198          * Ingo Molnar reported a Pentium D (model 6) and a Xeon
199          * (model 2) with the same problem.
200          */
201         if (c->x86 == 15)
202                 if (msr_clear_bit(MSR_IA32_MISC_ENABLE,
203                                   MSR_IA32_MISC_ENABLE_FAST_STRING_BIT) > 0)
204                         pr_info("kmemcheck: Disabling fast string operations\n");
205 #endif
206
207         /*
208          * If fast string is not enabled in IA32_MISC_ENABLE for any reason,
209          * clear the fast string and enhanced fast string CPU capabilities.
210          */
211         if (c->x86 > 6 || (c->x86 == 6 && c->x86_model >= 0xd)) {
212                 rdmsrl(MSR_IA32_MISC_ENABLE, misc_enable);
213                 if (!(misc_enable & MSR_IA32_MISC_ENABLE_FAST_STRING)) {
214                         printk(KERN_INFO "Disabled fast string operations\n");
215                         setup_clear_cpu_cap(X86_FEATURE_REP_GOOD);
216                         setup_clear_cpu_cap(X86_FEATURE_ERMS);
217                 }
218         }
219
220         /*
221          * Intel Quark Core DevMan_001.pdf section 6.4.11
222          * "The operating system also is required to invalidate (i.e., flush)
223          *  the TLB when any changes are made to any of the page table entries.
224          *  The operating system must reload CR3 to cause the TLB to be flushed"
225          *
226          * As a result cpu_has_pge() in arch/x86/include/asm/tlbflush.h should
227          * be false so that __flush_tlb_all() causes CR3 insted of CR4.PGE
228          * to be modified
229          */
230         if (c->x86 == 5 && c->x86_model == 9) {
231                 pr_info("Disabling PGE capability bit\n");
232                 setup_clear_cpu_cap(X86_FEATURE_PGE);
233         }
234 }
235
236 #ifdef CONFIG_X86_32
237 /*
238  *      Early probe support logic for ppro memory erratum #50
239  *
240  *      This is called before we do cpu ident work
241  */
242
243 int ppro_with_ram_bug(void)
244 {
245         /* Uses data from early_cpu_detect now */
246         if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL &&
247             boot_cpu_data.x86 == 6 &&
248             boot_cpu_data.x86_model == 1 &&
249             boot_cpu_data.x86_stepping < 8) {
250                 pr_info("Pentium Pro with Errata#50 detected. Taking evasive action.\n");
251                 return 1;
252         }
253         return 0;
254 }
255
256 static void intel_smp_check(struct cpuinfo_x86 *c)
257 {
258         /* calling is from identify_secondary_cpu() ? */
259         if (!c->cpu_index)
260                 return;
261
262         /*
263          * Mask B, Pentium, but not Pentium MMX
264          */
265         if (c->x86 == 5 &&
266             c->x86_stepping >= 1 && c->x86_stepping <= 4 &&
267             c->x86_model <= 3) {
268                 /*
269                  * Remember we have B step Pentia with bugs
270                  */
271                 WARN_ONCE(1, "WARNING: SMP operation may be unreliable"
272                                     "with B stepping processors.\n");
273         }
274 }
275
276 static int forcepae;
277 static int __init forcepae_setup(char *__unused)
278 {
279         forcepae = 1;
280         return 1;
281 }
282 __setup("forcepae", forcepae_setup);
283
284 static void intel_workarounds(struct cpuinfo_x86 *c)
285 {
286 #ifdef CONFIG_X86_F00F_BUG
287         /*
288          * All models of Pentium and Pentium with MMX technology CPUs
289          * have the F0 0F bug, which lets nonprivileged users lock up the
290          * system. Announce that the fault handler will be checking for it.
291          * The Quark is also family 5, but does not have the same bug.
292          */
293         clear_cpu_bug(c, X86_BUG_F00F);
294         if (!paravirt_enabled() && c->x86 == 5 && c->x86_model < 9) {
295                 static int f00f_workaround_enabled;
296
297                 set_cpu_bug(c, X86_BUG_F00F);
298                 if (!f00f_workaround_enabled) {
299                         printk(KERN_NOTICE "Intel Pentium with F0 0F bug - workaround enabled.\n");
300                         f00f_workaround_enabled = 1;
301                 }
302         }
303 #endif
304
305         /*
306          * SEP CPUID bug: Pentium Pro reports SEP but doesn't have it until
307          * model 3 mask 3
308          */
309         if ((c->x86<<8 | c->x86_model<<4 | c->x86_stepping) < 0x633)
310                 clear_cpu_cap(c, X86_FEATURE_SEP);
311
312         /*
313          * PAE CPUID issue: many Pentium M report no PAE but may have a
314          * functionally usable PAE implementation.
315          * Forcefully enable PAE if kernel parameter "forcepae" is present.
316          */
317         if (forcepae) {
318                 printk(KERN_WARNING "PAE forced!\n");
319                 set_cpu_cap(c, X86_FEATURE_PAE);
320                 add_taint(TAINT_CPU_OUT_OF_SPEC, LOCKDEP_NOW_UNRELIABLE);
321         }
322
323         /*
324          * P4 Xeon errata 037 workaround.
325          * Hardware prefetcher may cause stale data to be loaded into the cache.
326          */
327         if ((c->x86 == 15) && (c->x86_model == 1) && (c->x86_stepping == 1)) {
328                 if (msr_set_bit(MSR_IA32_MISC_ENABLE,
329                                 MSR_IA32_MISC_ENABLE_PREFETCH_DISABLE_BIT)
330                     > 0) {
331                         pr_info("CPU: C0 stepping P4 Xeon detected.\n");
332                         pr_info("CPU: Disabling hardware prefetching (Errata 037)\n");
333                 }
334         }
335
336         /*
337          * See if we have a good local APIC by checking for buggy Pentia,
338          * i.e. all B steppings and the C2 stepping of P54C when using their
339          * integrated APIC (see 11AP erratum in "Pentium Processor
340          * Specification Update").
341          */
342         if (cpu_has_apic && (c->x86<<8 | c->x86_model<<4) == 0x520 &&
343             (c->x86_stepping < 0x6 || c->x86_stepping == 0xb))
344                 set_cpu_bug(c, X86_BUG_11AP);
345
346
347 #ifdef CONFIG_X86_INTEL_USERCOPY
348         /*
349          * Set up the preferred alignment for movsl bulk memory moves
350          */
351         switch (c->x86) {
352         case 4:         /* 486: untested */
353                 break;
354         case 5:         /* Old Pentia: untested */
355                 break;
356         case 6:         /* PII/PIII only like movsl with 8-byte alignment */
357                 movsl_mask.mask = 7;
358                 break;
359         case 15:        /* P4 is OK down to 8-byte alignment */
360                 movsl_mask.mask = 7;
361                 break;
362         }
363 #endif
364
365         intel_smp_check(c);
366 }
367 #else
368 static void intel_workarounds(struct cpuinfo_x86 *c)
369 {
370 }
371 #endif
372
373 static void srat_detect_node(struct cpuinfo_x86 *c)
374 {
375 #ifdef CONFIG_NUMA
376         unsigned node;
377         int cpu = smp_processor_id();
378
379         /* Don't do the funky fallback heuristics the AMD version employs
380            for now. */
381         node = numa_cpu_node(cpu);
382         if (node == NUMA_NO_NODE || !node_online(node)) {
383                 /* reuse the value from init_cpu_to_node() */
384                 node = cpu_to_node(cpu);
385         }
386         numa_set_node(cpu, node);
387 #endif
388 }
389
390 /*
391  * find out the number of processor cores on the die
392  */
393 static int intel_num_cpu_cores(struct cpuinfo_x86 *c)
394 {
395         unsigned int eax, ebx, ecx, edx;
396
397         if (c->cpuid_level < 4)
398                 return 1;
399
400         /* Intel has a non-standard dependency on %ecx for this CPUID level. */
401         cpuid_count(4, 0, &eax, &ebx, &ecx, &edx);
402         if (eax & 0x1f)
403                 return (eax >> 26) + 1;
404         else
405                 return 1;
406 }
407
408 static void detect_vmx_virtcap(struct cpuinfo_x86 *c)
409 {
410         /* Intel VMX MSR indicated features */
411 #define X86_VMX_FEATURE_PROC_CTLS_TPR_SHADOW    0x00200000
412 #define X86_VMX_FEATURE_PROC_CTLS_VNMI          0x00400000
413 #define X86_VMX_FEATURE_PROC_CTLS_2ND_CTLS      0x80000000
414 #define X86_VMX_FEATURE_PROC_CTLS2_VIRT_APIC    0x00000001
415 #define X86_VMX_FEATURE_PROC_CTLS2_EPT          0x00000002
416 #define X86_VMX_FEATURE_PROC_CTLS2_VPID         0x00000020
417
418         u32 vmx_msr_low, vmx_msr_high, msr_ctl, msr_ctl2;
419
420         clear_cpu_cap(c, X86_FEATURE_TPR_SHADOW);
421         clear_cpu_cap(c, X86_FEATURE_VNMI);
422         clear_cpu_cap(c, X86_FEATURE_FLEXPRIORITY);
423         clear_cpu_cap(c, X86_FEATURE_EPT);
424         clear_cpu_cap(c, X86_FEATURE_VPID);
425
426         rdmsr(MSR_IA32_VMX_PROCBASED_CTLS, vmx_msr_low, vmx_msr_high);
427         msr_ctl = vmx_msr_high | vmx_msr_low;
428         if (msr_ctl & X86_VMX_FEATURE_PROC_CTLS_TPR_SHADOW)
429                 set_cpu_cap(c, X86_FEATURE_TPR_SHADOW);
430         if (msr_ctl & X86_VMX_FEATURE_PROC_CTLS_VNMI)
431                 set_cpu_cap(c, X86_FEATURE_VNMI);
432         if (msr_ctl & X86_VMX_FEATURE_PROC_CTLS_2ND_CTLS) {
433                 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
434                       vmx_msr_low, vmx_msr_high);
435                 msr_ctl2 = vmx_msr_high | vmx_msr_low;
436                 if ((msr_ctl2 & X86_VMX_FEATURE_PROC_CTLS2_VIRT_APIC) &&
437                     (msr_ctl & X86_VMX_FEATURE_PROC_CTLS_TPR_SHADOW))
438                         set_cpu_cap(c, X86_FEATURE_FLEXPRIORITY);
439                 if (msr_ctl2 & X86_VMX_FEATURE_PROC_CTLS2_EPT)
440                         set_cpu_cap(c, X86_FEATURE_EPT);
441                 if (msr_ctl2 & X86_VMX_FEATURE_PROC_CTLS2_VPID)
442                         set_cpu_cap(c, X86_FEATURE_VPID);
443         }
444 }
445
446 static void init_intel_energy_perf(struct cpuinfo_x86 *c)
447 {
448         u64 epb;
449
450         /*
451          * Initialize MSR_IA32_ENERGY_PERF_BIAS if not already initialized.
452          * (x86_energy_perf_policy(8) is available to change it at run-time.)
453          */
454         if (!cpu_has(c, X86_FEATURE_EPB))
455                 return;
456
457         rdmsrl(MSR_IA32_ENERGY_PERF_BIAS, epb);
458         if ((epb & 0xF) != ENERGY_PERF_BIAS_PERFORMANCE)
459                 return;
460
461         pr_warn_once("ENERGY_PERF_BIAS: Set to 'normal', was 'performance'\n");
462         pr_warn_once("ENERGY_PERF_BIAS: View and update with x86_energy_perf_policy(8)\n");
463         epb = (epb & ~0xF) | ENERGY_PERF_BIAS_NORMAL;
464         wrmsrl(MSR_IA32_ENERGY_PERF_BIAS, epb);
465 }
466
467 static void intel_bsp_resume(struct cpuinfo_x86 *c)
468 {
469         /*
470          * MSR_IA32_ENERGY_PERF_BIAS is lost across suspend/resume,
471          * so reinitialize it properly like during bootup:
472          */
473         init_intel_energy_perf(c);
474 }
475
476 static void init_intel(struct cpuinfo_x86 *c)
477 {
478         unsigned int l2 = 0;
479
480         early_init_intel(c);
481
482         intel_workarounds(c);
483
484         /*
485          * Detect the extended topology information if available. This
486          * will reinitialise the initial_apicid which will be used
487          * in init_intel_cacheinfo()
488          */
489         detect_extended_topology(c);
490
491         if (!cpu_has(c, X86_FEATURE_XTOPOLOGY)) {
492                 /*
493                  * let's use the legacy cpuid vector 0x1 and 0x4 for topology
494                  * detection.
495                  */
496                 c->x86_max_cores = intel_num_cpu_cores(c);
497 #ifdef CONFIG_X86_32
498                 detect_ht(c);
499 #endif
500         }
501
502         l2 = init_intel_cacheinfo(c);
503
504         /* Detect legacy cache sizes if init_intel_cacheinfo did not */
505         if (l2 == 0) {
506                 cpu_detect_cache_sizes(c);
507                 l2 = c->x86_cache_size;
508         }
509
510         if (c->cpuid_level > 9) {
511                 unsigned eax = cpuid_eax(10);
512                 /* Check for version and the number of counters */
513                 if ((eax & 0xff) && (((eax>>8) & 0xff) > 1))
514                         set_cpu_cap(c, X86_FEATURE_ARCH_PERFMON);
515         }
516
517         if (cpu_has_xmm2)
518                 set_cpu_cap(c, X86_FEATURE_LFENCE_RDTSC);
519
520         if (boot_cpu_has(X86_FEATURE_DS)) {
521                 unsigned int l1;
522                 rdmsr(MSR_IA32_MISC_ENABLE, l1, l2);
523                 if (!(l1 & (1<<11)))
524                         set_cpu_cap(c, X86_FEATURE_BTS);
525                 if (!(l1 & (1<<12)))
526                         set_cpu_cap(c, X86_FEATURE_PEBS);
527         }
528
529         if (c->x86 == 6 && cpu_has_clflush &&
530             (c->x86_model == 29 || c->x86_model == 46 || c->x86_model == 47))
531                 set_cpu_bug(c, X86_BUG_CLFLUSH_MONITOR);
532
533 #ifdef CONFIG_X86_64
534         if (c->x86 == 15)
535                 c->x86_cache_alignment = c->x86_clflush_size * 2;
536         if (c->x86 == 6)
537                 set_cpu_cap(c, X86_FEATURE_REP_GOOD);
538 #else
539         /*
540          * Names for the Pentium II/Celeron processors
541          * detectable only by also checking the cache size.
542          * Dixon is NOT a Celeron.
543          */
544         if (c->x86 == 6) {
545                 char *p = NULL;
546
547                 switch (c->x86_model) {
548                 case 5:
549                         if (l2 == 0)
550                                 p = "Celeron (Covington)";
551                         else if (l2 == 256)
552                                 p = "Mobile Pentium II (Dixon)";
553                         break;
554
555                 case 6:
556                         if (l2 == 128)
557                                 p = "Celeron (Mendocino)";
558                         else if (c->x86_stepping == 0 || c->x86_stepping == 5)
559                                 p = "Celeron-A";
560                         break;
561
562                 case 8:
563                         if (l2 == 128)
564                                 p = "Celeron (Coppermine)";
565                         break;
566                 }
567
568                 if (p)
569                         strcpy(c->x86_model_id, p);
570         }
571
572         if (c->x86 == 15)
573                 set_cpu_cap(c, X86_FEATURE_P4);
574         if (c->x86 == 6)
575                 set_cpu_cap(c, X86_FEATURE_P3);
576 #endif
577
578         /* Work around errata */
579         srat_detect_node(c);
580
581         if (cpu_has(c, X86_FEATURE_VMX))
582                 detect_vmx_virtcap(c);
583
584         init_intel_energy_perf(c);
585
586         if (tsx_ctrl_state == TSX_CTRL_ENABLE)
587                 tsx_enable();
588         if (tsx_ctrl_state == TSX_CTRL_DISABLE)
589                 tsx_disable();
590 }
591
592 #ifdef CONFIG_X86_32
593 static unsigned int intel_size_cache(struct cpuinfo_x86 *c, unsigned int size)
594 {
595         /*
596          * Intel PIII Tualatin. This comes in two flavours.
597          * One has 256kb of cache, the other 512. We have no way
598          * to determine which, so we use a boottime override
599          * for the 512kb model, and assume 256 otherwise.
600          */
601         if ((c->x86 == 6) && (c->x86_model == 11) && (size == 0))
602                 size = 256;
603
604         /*
605          * Intel Quark SoC X1000 contains a 4-way set associative
606          * 16K cache with a 16 byte cache line and 256 lines per tag
607          */
608         if ((c->x86 == 5) && (c->x86_model == 9))
609                 size = 16;
610         return size;
611 }
612 #endif
613
614 #define TLB_INST_4K     0x01
615 #define TLB_INST_4M     0x02
616 #define TLB_INST_2M_4M  0x03
617
618 #define TLB_INST_ALL    0x05
619 #define TLB_INST_1G     0x06
620
621 #define TLB_DATA_4K     0x11
622 #define TLB_DATA_4M     0x12
623 #define TLB_DATA_2M_4M  0x13
624 #define TLB_DATA_4K_4M  0x14
625
626 #define TLB_DATA_1G     0x16
627
628 #define TLB_DATA0_4K    0x21
629 #define TLB_DATA0_4M    0x22
630 #define TLB_DATA0_2M_4M 0x23
631
632 #define STLB_4K         0x41
633 #define STLB_4K_2M      0x42
634
635 static const struct _tlb_table intel_tlb_table[] = {
636         { 0x01, TLB_INST_4K,            32,     " TLB_INST 4 KByte pages, 4-way set associative" },
637         { 0x02, TLB_INST_4M,            2,      " TLB_INST 4 MByte pages, full associative" },
638         { 0x03, TLB_DATA_4K,            64,     " TLB_DATA 4 KByte pages, 4-way set associative" },
639         { 0x04, TLB_DATA_4M,            8,      " TLB_DATA 4 MByte pages, 4-way set associative" },
640         { 0x05, TLB_DATA_4M,            32,     " TLB_DATA 4 MByte pages, 4-way set associative" },
641         { 0x0b, TLB_INST_4M,            4,      " TLB_INST 4 MByte pages, 4-way set associative" },
642         { 0x4f, TLB_INST_4K,            32,     " TLB_INST 4 KByte pages */" },
643         { 0x50, TLB_INST_ALL,           64,     " TLB_INST 4 KByte and 2-MByte or 4-MByte pages" },
644         { 0x51, TLB_INST_ALL,           128,    " TLB_INST 4 KByte and 2-MByte or 4-MByte pages" },
645         { 0x52, TLB_INST_ALL,           256,    " TLB_INST 4 KByte and 2-MByte or 4-MByte pages" },
646         { 0x55, TLB_INST_2M_4M,         7,      " TLB_INST 2-MByte or 4-MByte pages, fully associative" },
647         { 0x56, TLB_DATA0_4M,           16,     " TLB_DATA0 4 MByte pages, 4-way set associative" },
648         { 0x57, TLB_DATA0_4K,           16,     " TLB_DATA0 4 KByte pages, 4-way associative" },
649         { 0x59, TLB_DATA0_4K,           16,     " TLB_DATA0 4 KByte pages, fully associative" },
650         { 0x5a, TLB_DATA0_2M_4M,        32,     " TLB_DATA0 2-MByte or 4 MByte pages, 4-way set associative" },
651         { 0x5b, TLB_DATA_4K_4M,         64,     " TLB_DATA 4 KByte and 4 MByte pages" },
652         { 0x5c, TLB_DATA_4K_4M,         128,    " TLB_DATA 4 KByte and 4 MByte pages" },
653         { 0x5d, TLB_DATA_4K_4M,         256,    " TLB_DATA 4 KByte and 4 MByte pages" },
654         { 0x61, TLB_INST_4K,            48,     " TLB_INST 4 KByte pages, full associative" },
655         { 0x63, TLB_DATA_1G,            4,      " TLB_DATA 1 GByte pages, 4-way set associative" },
656         { 0x76, TLB_INST_2M_4M,         8,      " TLB_INST 2-MByte or 4-MByte pages, fully associative" },
657         { 0xb0, TLB_INST_4K,            128,    " TLB_INST 4 KByte pages, 4-way set associative" },
658         { 0xb1, TLB_INST_2M_4M,         4,      " TLB_INST 2M pages, 4-way, 8 entries or 4M pages, 4-way entries" },
659         { 0xb2, TLB_INST_4K,            64,     " TLB_INST 4KByte pages, 4-way set associative" },
660         { 0xb3, TLB_DATA_4K,            128,    " TLB_DATA 4 KByte pages, 4-way set associative" },
661         { 0xb4, TLB_DATA_4K,            256,    " TLB_DATA 4 KByte pages, 4-way associative" },
662         { 0xb5, TLB_INST_4K,            64,     " TLB_INST 4 KByte pages, 8-way set associative" },
663         { 0xb6, TLB_INST_4K,            128,    " TLB_INST 4 KByte pages, 8-way set associative" },
664         { 0xba, TLB_DATA_4K,            64,     " TLB_DATA 4 KByte pages, 4-way associative" },
665         { 0xc0, TLB_DATA_4K_4M,         8,      " TLB_DATA 4 KByte and 4 MByte pages, 4-way associative" },
666         { 0xc1, STLB_4K_2M,             1024,   " STLB 4 KByte and 2 MByte pages, 8-way associative" },
667         { 0xc2, TLB_DATA_2M_4M,         16,     " DTLB 2 MByte/4MByte pages, 4-way associative" },
668         { 0xca, STLB_4K,                512,    " STLB 4 KByte pages, 4-way associative" },
669         { 0x00, 0, 0 }
670 };
671
672 static void intel_tlb_lookup(const unsigned char desc)
673 {
674         unsigned char k;
675         if (desc == 0)
676                 return;
677
678         /* look up this descriptor in the table */
679         for (k = 0; intel_tlb_table[k].descriptor != desc && \
680                         intel_tlb_table[k].descriptor != 0; k++)
681                 ;
682
683         if (intel_tlb_table[k].tlb_type == 0)
684                 return;
685
686         switch (intel_tlb_table[k].tlb_type) {
687         case STLB_4K:
688                 if (tlb_lli_4k[ENTRIES] < intel_tlb_table[k].entries)
689                         tlb_lli_4k[ENTRIES] = intel_tlb_table[k].entries;
690                 if (tlb_lld_4k[ENTRIES] < intel_tlb_table[k].entries)
691                         tlb_lld_4k[ENTRIES] = intel_tlb_table[k].entries;
692                 break;
693         case STLB_4K_2M:
694                 if (tlb_lli_4k[ENTRIES] < intel_tlb_table[k].entries)
695                         tlb_lli_4k[ENTRIES] = intel_tlb_table[k].entries;
696                 if (tlb_lld_4k[ENTRIES] < intel_tlb_table[k].entries)
697                         tlb_lld_4k[ENTRIES] = intel_tlb_table[k].entries;
698                 if (tlb_lli_2m[ENTRIES] < intel_tlb_table[k].entries)
699                         tlb_lli_2m[ENTRIES] = intel_tlb_table[k].entries;
700                 if (tlb_lld_2m[ENTRIES] < intel_tlb_table[k].entries)
701                         tlb_lld_2m[ENTRIES] = intel_tlb_table[k].entries;
702                 if (tlb_lli_4m[ENTRIES] < intel_tlb_table[k].entries)
703                         tlb_lli_4m[ENTRIES] = intel_tlb_table[k].entries;
704                 if (tlb_lld_4m[ENTRIES] < intel_tlb_table[k].entries)
705                         tlb_lld_4m[ENTRIES] = intel_tlb_table[k].entries;
706                 break;
707         case TLB_INST_ALL:
708                 if (tlb_lli_4k[ENTRIES] < intel_tlb_table[k].entries)
709                         tlb_lli_4k[ENTRIES] = intel_tlb_table[k].entries;
710                 if (tlb_lli_2m[ENTRIES] < intel_tlb_table[k].entries)
711                         tlb_lli_2m[ENTRIES] = intel_tlb_table[k].entries;
712                 if (tlb_lli_4m[ENTRIES] < intel_tlb_table[k].entries)
713                         tlb_lli_4m[ENTRIES] = intel_tlb_table[k].entries;
714                 break;
715         case TLB_INST_4K:
716                 if (tlb_lli_4k[ENTRIES] < intel_tlb_table[k].entries)
717                         tlb_lli_4k[ENTRIES] = intel_tlb_table[k].entries;
718                 break;
719         case TLB_INST_4M:
720                 if (tlb_lli_4m[ENTRIES] < intel_tlb_table[k].entries)
721                         tlb_lli_4m[ENTRIES] = intel_tlb_table[k].entries;
722                 break;
723         case TLB_INST_2M_4M:
724                 if (tlb_lli_2m[ENTRIES] < intel_tlb_table[k].entries)
725                         tlb_lli_2m[ENTRIES] = intel_tlb_table[k].entries;
726                 if (tlb_lli_4m[ENTRIES] < intel_tlb_table[k].entries)
727                         tlb_lli_4m[ENTRIES] = intel_tlb_table[k].entries;
728                 break;
729         case TLB_DATA_4K:
730         case TLB_DATA0_4K:
731                 if (tlb_lld_4k[ENTRIES] < intel_tlb_table[k].entries)
732                         tlb_lld_4k[ENTRIES] = intel_tlb_table[k].entries;
733                 break;
734         case TLB_DATA_4M:
735         case TLB_DATA0_4M:
736                 if (tlb_lld_4m[ENTRIES] < intel_tlb_table[k].entries)
737                         tlb_lld_4m[ENTRIES] = intel_tlb_table[k].entries;
738                 break;
739         case TLB_DATA_2M_4M:
740         case TLB_DATA0_2M_4M:
741                 if (tlb_lld_2m[ENTRIES] < intel_tlb_table[k].entries)
742                         tlb_lld_2m[ENTRIES] = intel_tlb_table[k].entries;
743                 if (tlb_lld_4m[ENTRIES] < intel_tlb_table[k].entries)
744                         tlb_lld_4m[ENTRIES] = intel_tlb_table[k].entries;
745                 break;
746         case TLB_DATA_4K_4M:
747                 if (tlb_lld_4k[ENTRIES] < intel_tlb_table[k].entries)
748                         tlb_lld_4k[ENTRIES] = intel_tlb_table[k].entries;
749                 if (tlb_lld_4m[ENTRIES] < intel_tlb_table[k].entries)
750                         tlb_lld_4m[ENTRIES] = intel_tlb_table[k].entries;
751                 break;
752         case TLB_DATA_1G:
753                 if (tlb_lld_1g[ENTRIES] < intel_tlb_table[k].entries)
754                         tlb_lld_1g[ENTRIES] = intel_tlb_table[k].entries;
755                 break;
756         }
757 }
758
759 static void intel_detect_tlb(struct cpuinfo_x86 *c)
760 {
761         int i, j, n;
762         unsigned int regs[4];
763         unsigned char *desc = (unsigned char *)regs;
764
765         if (c->cpuid_level < 2)
766                 return;
767
768         /* Number of times to iterate */
769         n = cpuid_eax(2) & 0xFF;
770
771         for (i = 0 ; i < n ; i++) {
772                 cpuid(2, &regs[0], &regs[1], &regs[2], &regs[3]);
773
774                 /* If bit 31 is set, this is an unknown format */
775                 for (j = 0 ; j < 3 ; j++)
776                         if (regs[j] & (1 << 31))
777                                 regs[j] = 0;
778
779                 /* Byte 0 is level count, not a descriptor */
780                 for (j = 1 ; j < 16 ; j++)
781                         intel_tlb_lookup(desc[j]);
782         }
783 }
784
785 static const struct cpu_dev intel_cpu_dev = {
786         .c_vendor       = "Intel",
787         .c_ident        = { "GenuineIntel" },
788 #ifdef CONFIG_X86_32
789         .legacy_models = {
790                 { .family = 4, .model_names =
791                   {
792                           [0] = "486 DX-25/33",
793                           [1] = "486 DX-50",
794                           [2] = "486 SX",
795                           [3] = "486 DX/2",
796                           [4] = "486 SL",
797                           [5] = "486 SX/2",
798                           [7] = "486 DX/2-WB",
799                           [8] = "486 DX/4",
800                           [9] = "486 DX/4-WB"
801                   }
802                 },
803                 { .family = 5, .model_names =
804                   {
805                           [0] = "Pentium 60/66 A-step",
806                           [1] = "Pentium 60/66",
807                           [2] = "Pentium 75 - 200",
808                           [3] = "OverDrive PODP5V83",
809                           [4] = "Pentium MMX",
810                           [7] = "Mobile Pentium 75 - 200",
811                           [8] = "Mobile Pentium MMX",
812                           [9] = "Quark SoC X1000",
813                   }
814                 },
815                 { .family = 6, .model_names =
816                   {
817                           [0] = "Pentium Pro A-step",
818                           [1] = "Pentium Pro",
819                           [3] = "Pentium II (Klamath)",
820                           [4] = "Pentium II (Deschutes)",
821                           [5] = "Pentium II (Deschutes)",
822                           [6] = "Mobile Pentium II",
823                           [7] = "Pentium III (Katmai)",
824                           [8] = "Pentium III (Coppermine)",
825                           [10] = "Pentium III (Cascades)",
826                           [11] = "Pentium III (Tualatin)",
827                   }
828                 },
829                 { .family = 15, .model_names =
830                   {
831                           [0] = "Pentium 4 (Unknown)",
832                           [1] = "Pentium 4 (Willamette)",
833                           [2] = "Pentium 4 (Northwood)",
834                           [4] = "Pentium 4 (Foster)",
835                           [5] = "Pentium 4 (Foster)",
836                   }
837                 },
838         },
839         .legacy_cache_size = intel_size_cache,
840 #endif
841         .c_detect_tlb   = intel_detect_tlb,
842         .c_early_init   = early_init_intel,
843         .c_init         = init_intel,
844         .c_bsp_resume   = intel_bsp_resume,
845         .c_x86_vendor   = X86_VENDOR_INTEL,
846 };
847
848 cpu_dev_register(intel_cpu_dev);
849