// SPDX-License-Identifier: GPL-2.01#include <asm/cpu_device_id.h>2#include <asm/cpufeature.h>3#include <linux/cpu.h>4#include <linux/export.h>5#include <linux/slab.h>67/**8* x86_match_vendor_cpu_type - helper function to match the hardware defined9* cpu-type for a single entry in the x86_cpu_id10* table. Note, this function does not match the11* generic cpu-types TOPO_CPU_TYPE_EFFICIENCY and12* TOPO_CPU_TYPE_PERFORMANCE.13* @c: Pointer to the cpuinfo_x86 structure of the CPU to match.14* @m: Pointer to the x86_cpu_id entry to match against.15*16* Return: true if the cpu-type matches, false otherwise.17*/18static bool x86_match_vendor_cpu_type(struct cpuinfo_x86 *c, const struct x86_cpu_id *m)19{20if (m->type == X86_CPU_TYPE_ANY)21return true;2223/* Hybrid CPUs are special, they are assumed to match all cpu-types */24if (cpu_feature_enabled(X86_FEATURE_HYBRID_CPU))25return true;2627if (c->x86_vendor == X86_VENDOR_INTEL)28return m->type == c->topo.intel_type;29if (c->x86_vendor == X86_VENDOR_AMD)30return m->type == c->topo.amd_type;3132return false;33}3435/**36* x86_match_cpu - match current CPU against an array of x86_cpu_ids37* @match: Pointer to array of x86_cpu_ids. Last entry terminated with38* {}.39*40* Return the entry if the current CPU matches the entries in the41* passed x86_cpu_id match table. Otherwise NULL. The match table42* contains vendor (X86_VENDOR_*), family, model and feature bits or43* respective wildcard entries.44*45* A typical table entry would be to match a specific CPU46*47* X86_MATCH_VFM_FEATURE(INTEL_BROADWELL, X86_FEATURE_ANY, NULL);48*49* Fields can be wildcarded with %X86_VENDOR_ANY, %X86_FAMILY_ANY,50* %X86_MODEL_ANY, %X86_FEATURE_ANY (except for vendor)51*52* asm/cpu_device_id.h contains a set of useful macros which are shortcuts53* for various common selections. The above can be shortened to:54*55* X86_MATCH_VFM(INTEL_BROADWELL, NULL);56*57* Arrays used to match for this should also be declared using58* MODULE_DEVICE_TABLE(x86cpu, ...)59*60* This always matches against the boot cpu, assuming models and features are61* consistent over all CPUs.62*/63const struct x86_cpu_id *x86_match_cpu(const struct x86_cpu_id *match)64{65const struct x86_cpu_id *m;66struct cpuinfo_x86 *c = &boot_cpu_data;6768for (m = match; m->flags & X86_CPU_ID_FLAG_ENTRY_VALID; m++) {69if (m->vendor != X86_VENDOR_ANY && c->x86_vendor != m->vendor)70continue;71if (m->family != X86_FAMILY_ANY && c->x86 != m->family)72continue;73if (m->model != X86_MODEL_ANY && c->x86_model != m->model)74continue;75if (m->steppings != X86_STEPPING_ANY &&76!(BIT(c->x86_stepping) & m->steppings))77continue;78if (m->feature != X86_FEATURE_ANY && !cpu_has(c, m->feature))79continue;80if (!x86_match_vendor_cpu_type(c, m))81continue;82return m;83}84return NULL;85}86EXPORT_SYMBOL(x86_match_cpu);8788bool x86_match_min_microcode_rev(const struct x86_cpu_id *table)89{90const struct x86_cpu_id *res = x86_match_cpu(table);9192if (!res || res->driver_data > boot_cpu_data.microcode)93return false;9495return true;96}97EXPORT_SYMBOL_GPL(x86_match_min_microcode_rev);9899100