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 *
20*d7ac1856STony Luck * X86_MATCH_VFM_FEATURE(INTEL_BROADWELL, X86_FEATURE_ANY, NULL);
21644e9cbbSAndi Kleen *
22644e9cbbSAndi Kleen * Fields can be wildcarded with %X86_VENDOR_ANY, %X86_FAMILY_ANY,
2320d43744SThomas Gleixner * %X86_MODEL_ANY, %X86_FEATURE_ANY (except for vendor)
2420d43744SThomas Gleixner *
2520d43744SThomas Gleixner * asm/cpu_device_id.h contains a set of useful macros which are shortcuts
2620d43744SThomas Gleixner * for various common selections. The above can be shortened to:
2720d43744SThomas Gleixner *
2820d43744SThomas Gleixner * X86_MATCH_INTEL_FAM6_MODEL(BROADWELL, NULL);
29644e9cbbSAndi Kleen *
30644e9cbbSAndi Kleen * Arrays used to match for this should also be declared using
31a7e0e4e9SJosh Triplett * MODULE_DEVICE_TABLE(x86cpu, ...)
32644e9cbbSAndi Kleen *
33644e9cbbSAndi Kleen * This always matches against the boot cpu, assuming models and features are
34644e9cbbSAndi Kleen * consistent over all CPUs.
35644e9cbbSAndi Kleen */
x86_match_cpu(const struct x86_cpu_id * match)36644e9cbbSAndi Kleen const struct x86_cpu_id *x86_match_cpu(const struct x86_cpu_id *match)
37644e9cbbSAndi Kleen {
38644e9cbbSAndi Kleen const struct x86_cpu_id *m;
39644e9cbbSAndi Kleen struct cpuinfo_x86 *c = &boot_cpu_data;
40644e9cbbSAndi Kleen
4165ac09c9STony Luck for (m = match; m->flags & X86_CPU_ID_FLAG_ENTRY_VALID; m++) {
42644e9cbbSAndi Kleen if (m->vendor != X86_VENDOR_ANY && c->x86_vendor != m->vendor)
43644e9cbbSAndi Kleen continue;
44644e9cbbSAndi Kleen if (m->family != X86_FAMILY_ANY && c->x86 != m->family)
45644e9cbbSAndi Kleen continue;
46644e9cbbSAndi Kleen if (m->model != X86_MODEL_ANY && c->x86_model != m->model)
47644e9cbbSAndi Kleen continue;
48e9d71445SMark Gross if (m->steppings != X86_STEPPING_ANY &&
49e9d71445SMark Gross !(BIT(c->x86_stepping) & m->steppings))
50e9d71445SMark Gross continue;
51644e9cbbSAndi Kleen if (m->feature != X86_FEATURE_ANY && !cpu_has(c, m->feature))
52644e9cbbSAndi Kleen continue;
53644e9cbbSAndi Kleen return m;
54644e9cbbSAndi Kleen }
55644e9cbbSAndi Kleen return NULL;
56644e9cbbSAndi Kleen }
57644e9cbbSAndi Kleen EXPORT_SYMBOL(x86_match_cpu);
580f42b790SKan Liang
590f42b790SKan Liang static const struct x86_cpu_desc *
x86_match_cpu_with_stepping(const struct x86_cpu_desc * match)600f42b790SKan Liang x86_match_cpu_with_stepping(const struct x86_cpu_desc *match)
610f42b790SKan Liang {
620f42b790SKan Liang struct cpuinfo_x86 *c = &boot_cpu_data;
630f42b790SKan Liang const struct x86_cpu_desc *m;
640f42b790SKan Liang
650f42b790SKan Liang for (m = match; m->x86_family | m->x86_model; m++) {
660f42b790SKan Liang if (c->x86_vendor != m->x86_vendor)
670f42b790SKan Liang continue;
680f42b790SKan Liang if (c->x86 != m->x86_family)
690f42b790SKan Liang continue;
700f42b790SKan Liang if (c->x86_model != m->x86_model)
710f42b790SKan Liang continue;
720f42b790SKan Liang if (c->x86_stepping != m->x86_stepping)
730f42b790SKan Liang continue;
740f42b790SKan Liang return m;
750f42b790SKan Liang }
760f42b790SKan Liang return NULL;
770f42b790SKan Liang }
780f42b790SKan Liang
x86_cpu_has_min_microcode_rev(const struct x86_cpu_desc * table)790f42b790SKan Liang bool x86_cpu_has_min_microcode_rev(const struct x86_cpu_desc *table)
800f42b790SKan Liang {
810f42b790SKan Liang const struct x86_cpu_desc *res = x86_match_cpu_with_stepping(table);
820f42b790SKan Liang
830f42b790SKan Liang if (!res || res->x86_microcode_rev > boot_cpu_data.microcode)
840f42b790SKan Liang return false;
850f42b790SKan Liang
860f42b790SKan Liang return true;
870f42b790SKan Liang }
880f42b790SKan Liang EXPORT_SYMBOL_GPL(x86_cpu_has_min_microcode_rev);
89