xref: /openbmc/linux/arch/x86/kernel/cpu/match.c (revision 20d437447c0089cda46c683db219d3b4e2cde40e)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2644e9cbbSAndi Kleen #include <asm/cpu_device_id.h>
3cd4d09ecSBorislav Petkov #include <asm/cpufeature.h>
4644e9cbbSAndi Kleen #include <linux/cpu.h>
5186f4360SPaul Gortmaker #include <linux/export.h>
6fad12ac8SThomas Renninger #include <linux/slab.h>
7644e9cbbSAndi Kleen 
8644e9cbbSAndi Kleen /**
9644e9cbbSAndi Kleen  * x86_match_cpu - match current CPU again an array of x86_cpu_ids
10644e9cbbSAndi Kleen  * @match: Pointer to array of x86_cpu_ids. Last entry terminated with
11644e9cbbSAndi Kleen  *         {}.
12644e9cbbSAndi Kleen  *
13644e9cbbSAndi Kleen  * Return the entry if the current CPU matches the entries in the
14644e9cbbSAndi Kleen  * passed x86_cpu_id match table. Otherwise NULL.  The match table
15644e9cbbSAndi Kleen  * contains vendor (X86_VENDOR_*), family, model and feature bits or
16644e9cbbSAndi Kleen  * respective wildcard entries.
17644e9cbbSAndi Kleen  *
18644e9cbbSAndi Kleen  * A typical table entry would be to match a specific CPU
19*20d43744SThomas Gleixner  *
20*20d43744SThomas Gleixner  * X86_MATCH_VENDOR_FAM_MODEL_FEATURE(INTEL, 6, INTEL_FAM6_BROADWELL,
21*20d43744SThomas Gleixner  *				      X86_FEATURE_ANY, NULL);
22644e9cbbSAndi Kleen  *
23644e9cbbSAndi Kleen  * Fields can be wildcarded with %X86_VENDOR_ANY, %X86_FAMILY_ANY,
24*20d43744SThomas Gleixner  * %X86_MODEL_ANY, %X86_FEATURE_ANY (except for vendor)
25*20d43744SThomas Gleixner  *
26*20d43744SThomas Gleixner  * asm/cpu_device_id.h contains a set of useful macros which are shortcuts
27*20d43744SThomas Gleixner  * for various common selections. The above can be shortened to:
28*20d43744SThomas Gleixner  *
29*20d43744SThomas Gleixner  * X86_MATCH_INTEL_FAM6_MODEL(BROADWELL, NULL);
30644e9cbbSAndi Kleen  *
31644e9cbbSAndi Kleen  * Arrays used to match for this should also be declared using
32a7e0e4e9SJosh Triplett  * MODULE_DEVICE_TABLE(x86cpu, ...)
33644e9cbbSAndi Kleen  *
34644e9cbbSAndi Kleen  * This always matches against the boot cpu, assuming models and features are
35644e9cbbSAndi Kleen  * consistent over all CPUs.
36644e9cbbSAndi Kleen  */
37644e9cbbSAndi Kleen const struct x86_cpu_id *x86_match_cpu(const struct x86_cpu_id *match)
38644e9cbbSAndi Kleen {
39644e9cbbSAndi Kleen 	const struct x86_cpu_id *m;
40644e9cbbSAndi Kleen 	struct cpuinfo_x86 *c = &boot_cpu_data;
41644e9cbbSAndi Kleen 
42644e9cbbSAndi Kleen 	for (m = match; m->vendor | m->family | m->model | m->feature; m++) {
43644e9cbbSAndi Kleen 		if (m->vendor != X86_VENDOR_ANY && c->x86_vendor != m->vendor)
44644e9cbbSAndi Kleen 			continue;
45644e9cbbSAndi Kleen 		if (m->family != X86_FAMILY_ANY && c->x86 != m->family)
46644e9cbbSAndi Kleen 			continue;
47644e9cbbSAndi Kleen 		if (m->model != X86_MODEL_ANY && c->x86_model != m->model)
48644e9cbbSAndi Kleen 			continue;
49644e9cbbSAndi Kleen 		if (m->feature != X86_FEATURE_ANY && !cpu_has(c, m->feature))
50644e9cbbSAndi Kleen 			continue;
51644e9cbbSAndi Kleen 		return m;
52644e9cbbSAndi Kleen 	}
53644e9cbbSAndi Kleen 	return NULL;
54644e9cbbSAndi Kleen }
55644e9cbbSAndi Kleen EXPORT_SYMBOL(x86_match_cpu);
560f42b790SKan Liang 
570f42b790SKan Liang static const struct x86_cpu_desc *
580f42b790SKan Liang x86_match_cpu_with_stepping(const struct x86_cpu_desc *match)
590f42b790SKan Liang {
600f42b790SKan Liang 	struct cpuinfo_x86 *c = &boot_cpu_data;
610f42b790SKan Liang 	const struct x86_cpu_desc *m;
620f42b790SKan Liang 
630f42b790SKan Liang 	for (m = match; m->x86_family | m->x86_model; m++) {
640f42b790SKan Liang 		if (c->x86_vendor != m->x86_vendor)
650f42b790SKan Liang 			continue;
660f42b790SKan Liang 		if (c->x86 != m->x86_family)
670f42b790SKan Liang 			continue;
680f42b790SKan Liang 		if (c->x86_model != m->x86_model)
690f42b790SKan Liang 			continue;
700f42b790SKan Liang 		if (c->x86_stepping != m->x86_stepping)
710f42b790SKan Liang 			continue;
720f42b790SKan Liang 		return m;
730f42b790SKan Liang 	}
740f42b790SKan Liang 	return NULL;
750f42b790SKan Liang }
760f42b790SKan Liang 
770f42b790SKan Liang bool x86_cpu_has_min_microcode_rev(const struct x86_cpu_desc *table)
780f42b790SKan Liang {
790f42b790SKan Liang 	const struct x86_cpu_desc *res = x86_match_cpu_with_stepping(table);
800f42b790SKan Liang 
810f42b790SKan Liang 	if (!res || res->x86_microcode_rev > boot_cpu_data.microcode)
820f42b790SKan Liang 		return false;
830f42b790SKan Liang 
840f42b790SKan Liang 	return true;
850f42b790SKan Liang }
860f42b790SKan Liang EXPORT_SYMBOL_GPL(x86_cpu_has_min_microcode_rev);
87