topology_ext.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/cpu.h>
  3. #include <asm/apic.h>
  4. #include <asm/memtype.h>
  5. #include <asm/processor.h>
  6. #include "cpu.h"
  7. enum topo_types {
  8. INVALID_TYPE = 0,
  9. SMT_TYPE = 1,
  10. CORE_TYPE = 2,
  11. MAX_TYPE_0B = 3,
  12. MODULE_TYPE = 3,
  13. AMD_CCD_TYPE = 3,
  14. TILE_TYPE = 4,
  15. AMD_SOCKET_TYPE = 4,
  16. MAX_TYPE_80000026 = 5,
  17. DIE_TYPE = 5,
  18. DIEGRP_TYPE = 6,
  19. MAX_TYPE_1F = 7,
  20. };
  21. /*
  22. * Use a lookup table for the case that there are future types > 6 which
  23. * describe an intermediate domain level which does not exist today.
  24. */
  25. static const unsigned int topo_domain_map_0b_1f[MAX_TYPE_1F] = {
  26. [SMT_TYPE] = TOPO_SMT_DOMAIN,
  27. [CORE_TYPE] = TOPO_CORE_DOMAIN,
  28. [MODULE_TYPE] = TOPO_MODULE_DOMAIN,
  29. [TILE_TYPE] = TOPO_TILE_DOMAIN,
  30. [DIE_TYPE] = TOPO_DIE_DOMAIN,
  31. [DIEGRP_TYPE] = TOPO_DIEGRP_DOMAIN,
  32. };
  33. static const unsigned int topo_domain_map_80000026[MAX_TYPE_80000026] = {
  34. [SMT_TYPE] = TOPO_SMT_DOMAIN,
  35. [CORE_TYPE] = TOPO_CORE_DOMAIN,
  36. [AMD_CCD_TYPE] = TOPO_TILE_DOMAIN,
  37. [AMD_SOCKET_TYPE] = TOPO_DIE_DOMAIN,
  38. };
  39. static inline bool topo_subleaf(struct topo_scan *tscan, u32 leaf, u32 subleaf,
  40. unsigned int *last_dom)
  41. {
  42. unsigned int dom, maxtype;
  43. const unsigned int *map;
  44. struct {
  45. // eax
  46. u32 x2apic_shift : 5, // Number of bits to shift APIC ID right
  47. // for the topology ID at the next level
  48. : 27; // Reserved
  49. // ebx
  50. u32 num_processors : 16, // Number of processors at current level
  51. : 16; // Reserved
  52. // ecx
  53. u32 level : 8, // Current topology level. Same as sub leaf number
  54. type : 8, // Level type. If 0, invalid
  55. : 16; // Reserved
  56. // edx
  57. u32 x2apic_id : 32; // X2APIC ID of the current logical processor
  58. } sl;
  59. switch (leaf) {
  60. case 0x0b: maxtype = MAX_TYPE_0B; map = topo_domain_map_0b_1f; break;
  61. case 0x1f: maxtype = MAX_TYPE_1F; map = topo_domain_map_0b_1f; break;
  62. case 0x80000026: maxtype = MAX_TYPE_80000026; map = topo_domain_map_80000026; break;
  63. default: return false;
  64. }
  65. cpuid_subleaf(leaf, subleaf, &sl);
  66. if (!sl.num_processors || sl.type == INVALID_TYPE)
  67. return false;
  68. if (sl.type >= maxtype) {
  69. pr_err_once("Topology: leaf 0x%x:%d Unknown domain type %u\n",
  70. leaf, subleaf, sl.type);
  71. /*
  72. * It really would have been too obvious to make the domain
  73. * type space sparse and leave a few reserved types between
  74. * the points which might change instead of following the
  75. * usual "this can be fixed in software" principle.
  76. */
  77. dom = *last_dom + 1;
  78. } else {
  79. dom = map[sl.type];
  80. *last_dom = dom;
  81. }
  82. if (!dom) {
  83. tscan->c->topo.initial_apicid = sl.x2apic_id;
  84. } else if (tscan->c->topo.initial_apicid != sl.x2apic_id) {
  85. pr_warn_once(FW_BUG "CPUID leaf 0x%x subleaf %d APIC ID mismatch %x != %x\n",
  86. leaf, subleaf, tscan->c->topo.initial_apicid, sl.x2apic_id);
  87. }
  88. topology_set_dom(tscan, dom, sl.x2apic_shift, sl.num_processors);
  89. return true;
  90. }
  91. static bool parse_topology_leaf(struct topo_scan *tscan, u32 leaf)
  92. {
  93. unsigned int last_dom;
  94. u32 subleaf;
  95. /* Read all available subleafs and populate the levels */
  96. for (subleaf = 0, last_dom = 0; topo_subleaf(tscan, leaf, subleaf, &last_dom); subleaf++);
  97. /* If subleaf 0 failed to parse, give up */
  98. if (!subleaf)
  99. return false;
  100. /*
  101. * There are machines in the wild which have shift 0 in the subleaf
  102. * 0, but advertise 2 logical processors at that level. They are
  103. * truly SMT.
  104. */
  105. if (!tscan->dom_shifts[TOPO_SMT_DOMAIN] && tscan->dom_ncpus[TOPO_SMT_DOMAIN] > 1) {
  106. unsigned int sft = get_count_order(tscan->dom_ncpus[TOPO_SMT_DOMAIN]);
  107. pr_warn_once(FW_BUG "CPUID leaf 0x%x subleaf 0 has shift level 0 but %u CPUs. Fixing it up.\n",
  108. leaf, tscan->dom_ncpus[TOPO_SMT_DOMAIN]);
  109. topology_update_dom(tscan, TOPO_SMT_DOMAIN, sft, tscan->dom_ncpus[TOPO_SMT_DOMAIN]);
  110. }
  111. set_cpu_cap(tscan->c, X86_FEATURE_XTOPOLOGY);
  112. return true;
  113. }
  114. bool cpu_parse_topology_ext(struct topo_scan *tscan)
  115. {
  116. /* Intel: Try leaf 0x1F first. */
  117. if (tscan->c->cpuid_level >= 0x1f && parse_topology_leaf(tscan, 0x1f))
  118. return true;
  119. /* AMD: Try leaf 0x80000026 first. */
  120. if (tscan->c->extended_cpuid_level >= 0x80000026 && parse_topology_leaf(tscan, 0x80000026))
  121. return true;
  122. /* Intel/AMD: Fall back to leaf 0xB if available */
  123. return tscan->c->cpuid_level >= 0x0b && parse_topology_leaf(tscan, 0x0b);
  124. }