match.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. * x86_match_cpu - match current CPU again an array of x86_cpu_ids
  9. * @match: Pointer to array of x86_cpu_ids. Last entry terminated with
  10. * {}.
  11. *
  12. * Return the entry if the current CPU matches the entries in the
  13. * passed x86_cpu_id match table. Otherwise NULL. The match table
  14. * contains vendor (X86_VENDOR_*), family, model and feature bits or
  15. * respective wildcard entries.
  16. *
  17. * A typical table entry would be to match a specific CPU
  18. *
  19. * X86_MATCH_VFM_FEATURE(INTEL_BROADWELL, X86_FEATURE_ANY, NULL);
  20. *
  21. * Fields can be wildcarded with %X86_VENDOR_ANY, %X86_FAMILY_ANY,
  22. * %X86_MODEL_ANY, %X86_FEATURE_ANY (except for vendor)
  23. *
  24. * asm/cpu_device_id.h contains a set of useful macros which are shortcuts
  25. * for various common selections. The above can be shortened to:
  26. *
  27. * X86_MATCH_VFM(INTEL_BROADWELL, NULL);
  28. *
  29. * Arrays used to match for this should also be declared using
  30. * MODULE_DEVICE_TABLE(x86cpu, ...)
  31. *
  32. * This always matches against the boot cpu, assuming models and features are
  33. * consistent over all CPUs.
  34. */
  35. const struct x86_cpu_id *x86_match_cpu(const struct x86_cpu_id *match)
  36. {
  37. const struct x86_cpu_id *m;
  38. struct cpuinfo_x86 *c = &boot_cpu_data;
  39. for (m = match; m->flags & X86_CPU_ID_FLAG_ENTRY_VALID; m++) {
  40. if (m->vendor != X86_VENDOR_ANY && c->x86_vendor != m->vendor)
  41. continue;
  42. if (m->family != X86_FAMILY_ANY && c->x86 != m->family)
  43. continue;
  44. if (m->model != X86_MODEL_ANY && c->x86_model != m->model)
  45. continue;
  46. if (m->steppings != X86_STEPPING_ANY &&
  47. !(BIT(c->x86_stepping) & m->steppings))
  48. continue;
  49. if (m->feature != X86_FEATURE_ANY && !cpu_has(c, m->feature))
  50. continue;
  51. return m;
  52. }
  53. return NULL;
  54. }
  55. EXPORT_SYMBOL(x86_match_cpu);
  56. static const struct x86_cpu_desc *
  57. x86_match_cpu_with_stepping(const struct x86_cpu_desc *match)
  58. {
  59. struct cpuinfo_x86 *c = &boot_cpu_data;
  60. const struct x86_cpu_desc *m;
  61. for (m = match; m->x86_family | m->x86_model; m++) {
  62. if (c->x86_vendor != m->x86_vendor)
  63. continue;
  64. if (c->x86 != m->x86_family)
  65. continue;
  66. if (c->x86_model != m->x86_model)
  67. continue;
  68. if (c->x86_stepping != m->x86_stepping)
  69. continue;
  70. return m;
  71. }
  72. return NULL;
  73. }
  74. bool x86_cpu_has_min_microcode_rev(const struct x86_cpu_desc *table)
  75. {
  76. const struct x86_cpu_desc *res = x86_match_cpu_with_stepping(table);
  77. if (!res || res->x86_microcode_rev > boot_cpu_data.microcode)
  78. return false;
  79. return true;
  80. }
  81. EXPORT_SYMBOL_GPL(x86_cpu_has_min_microcode_rev);