xref: /openbmc/linux/arch/x86/kernel/cpu/match.c (revision e9d7144597b10ff13ff2264c059f7d4a7fbc89ac)
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
1920d43744SThomas Gleixner  *
2020d43744SThomas Gleixner  * X86_MATCH_VENDOR_FAM_MODEL_FEATURE(INTEL, 6, INTEL_FAM6_BROADWELL,
2120d43744SThomas Gleixner  *				      X86_FEATURE_ANY, NULL);
22644e9cbbSAndi Kleen  *
23644e9cbbSAndi Kleen  * Fields can be wildcarded with %X86_VENDOR_ANY, %X86_FAMILY_ANY,
2420d43744SThomas Gleixner  * %X86_MODEL_ANY, %X86_FEATURE_ANY (except for vendor)
2520d43744SThomas Gleixner  *
2620d43744SThomas Gleixner  * asm/cpu_device_id.h contains a set of useful macros which are shortcuts
2720d43744SThomas Gleixner  * for various common selections. The above can be shortened to:
2820d43744SThomas Gleixner  *
2920d43744SThomas 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 
42*e9d71445SMark Gross 	for (m = match;
43*e9d71445SMark Gross 	     m->vendor | m->family | m->model | m->steppings | m->feature;
44*e9d71445SMark Gross 	     m++) {
45644e9cbbSAndi Kleen 		if (m->vendor != X86_VENDOR_ANY && c->x86_vendor != m->vendor)
46644e9cbbSAndi Kleen 			continue;
47644e9cbbSAndi Kleen 		if (m->family != X86_FAMILY_ANY && c->x86 != m->family)
48644e9cbbSAndi Kleen 			continue;
49644e9cbbSAndi Kleen 		if (m->model != X86_MODEL_ANY && c->x86_model != m->model)
50644e9cbbSAndi Kleen 			continue;
51*e9d71445SMark Gross 		if (m->steppings != X86_STEPPING_ANY &&
52*e9d71445SMark Gross 		    !(BIT(c->x86_stepping) & m->steppings))
53*e9d71445SMark Gross 			continue;
54644e9cbbSAndi Kleen 		if (m->feature != X86_FEATURE_ANY && !cpu_has(c, m->feature))
55644e9cbbSAndi Kleen 			continue;
56644e9cbbSAndi Kleen 		return m;
57644e9cbbSAndi Kleen 	}
58644e9cbbSAndi Kleen 	return NULL;
59644e9cbbSAndi Kleen }
60644e9cbbSAndi Kleen EXPORT_SYMBOL(x86_match_cpu);
610f42b790SKan Liang 
620f42b790SKan Liang static const struct x86_cpu_desc *
630f42b790SKan Liang x86_match_cpu_with_stepping(const struct x86_cpu_desc *match)
640f42b790SKan Liang {
650f42b790SKan Liang 	struct cpuinfo_x86 *c = &boot_cpu_data;
660f42b790SKan Liang 	const struct x86_cpu_desc *m;
670f42b790SKan Liang 
680f42b790SKan Liang 	for (m = match; m->x86_family | m->x86_model; m++) {
690f42b790SKan Liang 		if (c->x86_vendor != m->x86_vendor)
700f42b790SKan Liang 			continue;
710f42b790SKan Liang 		if (c->x86 != m->x86_family)
720f42b790SKan Liang 			continue;
730f42b790SKan Liang 		if (c->x86_model != m->x86_model)
740f42b790SKan Liang 			continue;
750f42b790SKan Liang 		if (c->x86_stepping != m->x86_stepping)
760f42b790SKan Liang 			continue;
770f42b790SKan Liang 		return m;
780f42b790SKan Liang 	}
790f42b790SKan Liang 	return NULL;
800f42b790SKan Liang }
810f42b790SKan Liang 
820f42b790SKan Liang bool x86_cpu_has_min_microcode_rev(const struct x86_cpu_desc *table)
830f42b790SKan Liang {
840f42b790SKan Liang 	const struct x86_cpu_desc *res = x86_match_cpu_with_stepping(table);
850f42b790SKan Liang 
860f42b790SKan Liang 	if (!res || res->x86_microcode_rev > boot_cpu_data.microcode)
870f42b790SKan Liang 		return false;
880f42b790SKan Liang 
890f42b790SKan Liang 	return true;
900f42b790SKan Liang }
910f42b790SKan Liang EXPORT_SYMBOL_GPL(x86_cpu_has_min_microcode_rev);
92