1 // SPDX-License-Identifier: GPL-2.0 2 #include <asm/cpu_device_id.h> 3 #include <asm/cpufeature.h> 4 #include <linux/cpu.h> 5 #include <linux/export.h> 6 #include <linux/slab.h> 7 8 /** 9 * x86_match_vendor_cpu_type - helper function to match the hardware defined 10 * cpu-type for a single entry in the x86_cpu_id 11 * table. Note, this function does not match the 12 * generic cpu-types TOPO_CPU_TYPE_EFFICIENCY and 13 * TOPO_CPU_TYPE_PERFORMANCE. 14 * @c: Pointer to the cpuinfo_x86 structure of the CPU to match. 15 * @m: Pointer to the x86_cpu_id entry to match against. 16 * 17 * Return: true if the cpu-type matches, false otherwise. 18 */ 19 static bool x86_match_vendor_cpu_type(struct cpuinfo_x86 *c, const struct x86_cpu_id *m) 20 { 21 if (m->type == X86_CPU_TYPE_ANY) 22 return true; 23 24 /* Hybrid CPUs are special, they are assumed to match all cpu-types */ 25 if (cpu_feature_enabled(X86_FEATURE_HYBRID_CPU)) 26 return true; 27 28 if (c->x86_vendor == X86_VENDOR_INTEL) 29 return m->type == c->topo.intel_type; 30 if (c->x86_vendor == X86_VENDOR_AMD) 31 return m->type == c->topo.amd_type; 32 33 return false; 34 } 35 36 /** 37 * x86_match_cpu - match current CPU against an array of x86_cpu_ids 38 * @match: Pointer to array of x86_cpu_ids. Last entry terminated with 39 * {}. 40 * 41 * Return the entry if the current CPU matches the entries in the 42 * passed x86_cpu_id match table. Otherwise NULL. The match table 43 * contains vendor (X86_VENDOR_*), family, model and feature bits or 44 * respective wildcard entries. 45 * 46 * A typical table entry would be to match a specific CPU 47 * 48 * X86_MATCH_VFM_FEATURE(INTEL_BROADWELL, X86_FEATURE_ANY, NULL); 49 * 50 * Fields can be wildcarded with %X86_VENDOR_ANY, %X86_FAMILY_ANY, 51 * %X86_MODEL_ANY, %X86_FEATURE_ANY (except for vendor) 52 * 53 * asm/cpu_device_id.h contains a set of useful macros which are shortcuts 54 * for various common selections. The above can be shortened to: 55 * 56 * X86_MATCH_VFM(INTEL_BROADWELL, NULL); 57 * 58 * Arrays used to match for this should also be declared using 59 * MODULE_DEVICE_TABLE(x86cpu, ...) 60 * 61 * This always matches against the boot cpu, assuming models and features are 62 * consistent over all CPUs. 63 */ 64 const struct x86_cpu_id *x86_match_cpu(const struct x86_cpu_id *match) 65 { 66 const struct x86_cpu_id *m; 67 struct cpuinfo_x86 *c = &boot_cpu_data; 68 69 for (m = match; m->flags & X86_CPU_ID_FLAG_ENTRY_VALID; m++) { 70 if (m->vendor != X86_VENDOR_ANY && c->x86_vendor != m->vendor) 71 continue; 72 if (m->family != X86_FAMILY_ANY && c->x86 != m->family) 73 continue; 74 if (m->model != X86_MODEL_ANY && c->x86_model != m->model) 75 continue; 76 if (m->steppings != X86_STEPPING_ANY && 77 !(BIT(c->x86_stepping) & m->steppings)) 78 continue; 79 if (m->feature != X86_FEATURE_ANY && !cpu_has(c, m->feature)) 80 continue; 81 if (!x86_match_vendor_cpu_type(c, m)) 82 continue; 83 return m; 84 } 85 return NULL; 86 } 87 EXPORT_SYMBOL(x86_match_cpu); 88 89 bool x86_match_min_microcode_rev(const struct x86_cpu_id *table) 90 { 91 const struct x86_cpu_id *res = x86_match_cpu(table); 92 93 if (!res || res->driver_data > boot_cpu_data.microcode) 94 return false; 95 96 return true; 97 } 98 EXPORT_SYMBOL_GPL(x86_match_min_microcode_rev); 99