topology.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Check for extended topology enumeration cpuid leaf 0xb and if it
  4. * exists, use it for populating initial_apicid and cpu topology
  5. * detection.
  6. */
  7. #include <linux/cpu.h>
  8. #include <asm/apic.h>
  9. #include <asm/memtype.h>
  10. #include <asm/processor.h>
  11. #include "cpu.h"
  12. /* leaf 0xb SMT level */
  13. #define SMT_LEVEL 0
  14. /* extended topology sub-leaf types */
  15. #define INVALID_TYPE 0
  16. #define SMT_TYPE 1
  17. #define CORE_TYPE 2
  18. #define DIE_TYPE 5
  19. #define LEAFB_SUBTYPE(ecx) (((ecx) >> 8) & 0xff)
  20. #define BITS_SHIFT_NEXT_LEVEL(eax) ((eax) & 0x1f)
  21. #define LEVEL_MAX_SIBLINGS(ebx) ((ebx) & 0xffff)
  22. unsigned int __max_die_per_package __read_mostly = 1;
  23. EXPORT_SYMBOL(__max_die_per_package);
  24. #ifdef CONFIG_SMP
  25. /*
  26. * Check if given CPUID extended topology "leaf" is implemented
  27. */
  28. static int check_extended_topology_leaf(int leaf)
  29. {
  30. unsigned int eax, ebx, ecx, edx;
  31. cpuid_count(leaf, SMT_LEVEL, &eax, &ebx, &ecx, &edx);
  32. if (ebx == 0 || (LEAFB_SUBTYPE(ecx) != SMT_TYPE))
  33. return -1;
  34. return 0;
  35. }
  36. /*
  37. * Return best CPUID Extended Topology Leaf supported
  38. */
  39. static int detect_extended_topology_leaf(struct cpuinfo_x86 *c)
  40. {
  41. if (c->cpuid_level >= 0x1f) {
  42. if (check_extended_topology_leaf(0x1f) == 0)
  43. return 0x1f;
  44. }
  45. if (c->cpuid_level >= 0xb) {
  46. if (check_extended_topology_leaf(0xb) == 0)
  47. return 0xb;
  48. }
  49. return -1;
  50. }
  51. #endif
  52. int detect_extended_topology_early(struct cpuinfo_x86 *c)
  53. {
  54. #ifdef CONFIG_SMP
  55. unsigned int eax, ebx, ecx, edx;
  56. int leaf;
  57. leaf = detect_extended_topology_leaf(c);
  58. if (leaf < 0)
  59. return -1;
  60. set_cpu_cap(c, X86_FEATURE_XTOPOLOGY);
  61. cpuid_count(leaf, SMT_LEVEL, &eax, &ebx, &ecx, &edx);
  62. /*
  63. * initial apic id, which also represents 32-bit extended x2apic id.
  64. */
  65. c->initial_apicid = edx;
  66. smp_num_siblings = max_t(int, smp_num_siblings, LEVEL_MAX_SIBLINGS(ebx));
  67. #endif
  68. return 0;
  69. }
  70. /*
  71. * Check for extended topology enumeration cpuid leaf, and if it
  72. * exists, use it for populating initial_apicid and cpu topology
  73. * detection.
  74. */
  75. int detect_extended_topology(struct cpuinfo_x86 *c)
  76. {
  77. #ifdef CONFIG_SMP
  78. unsigned int eax, ebx, ecx, edx, sub_index;
  79. unsigned int ht_mask_width, core_plus_mask_width, die_plus_mask_width;
  80. unsigned int core_select_mask, core_level_siblings;
  81. unsigned int die_select_mask, die_level_siblings;
  82. unsigned int pkg_mask_width;
  83. bool die_level_present = false;
  84. int leaf;
  85. leaf = detect_extended_topology_leaf(c);
  86. if (leaf < 0)
  87. return -1;
  88. /*
  89. * Populate HT related information from sub-leaf level 0.
  90. */
  91. cpuid_count(leaf, SMT_LEVEL, &eax, &ebx, &ecx, &edx);
  92. c->initial_apicid = edx;
  93. core_level_siblings = LEVEL_MAX_SIBLINGS(ebx);
  94. smp_num_siblings = max_t(int, smp_num_siblings, LEVEL_MAX_SIBLINGS(ebx));
  95. core_plus_mask_width = ht_mask_width = BITS_SHIFT_NEXT_LEVEL(eax);
  96. die_level_siblings = LEVEL_MAX_SIBLINGS(ebx);
  97. pkg_mask_width = die_plus_mask_width = BITS_SHIFT_NEXT_LEVEL(eax);
  98. sub_index = 1;
  99. while (true) {
  100. cpuid_count(leaf, sub_index, &eax, &ebx, &ecx, &edx);
  101. /*
  102. * Check for the Core type in the implemented sub leaves.
  103. */
  104. if (LEAFB_SUBTYPE(ecx) == CORE_TYPE) {
  105. core_level_siblings = LEVEL_MAX_SIBLINGS(ebx);
  106. core_plus_mask_width = BITS_SHIFT_NEXT_LEVEL(eax);
  107. die_level_siblings = core_level_siblings;
  108. die_plus_mask_width = BITS_SHIFT_NEXT_LEVEL(eax);
  109. }
  110. if (LEAFB_SUBTYPE(ecx) == DIE_TYPE) {
  111. die_level_present = true;
  112. die_level_siblings = LEVEL_MAX_SIBLINGS(ebx);
  113. die_plus_mask_width = BITS_SHIFT_NEXT_LEVEL(eax);
  114. }
  115. if (LEAFB_SUBTYPE(ecx) != INVALID_TYPE)
  116. pkg_mask_width = BITS_SHIFT_NEXT_LEVEL(eax);
  117. else
  118. break;
  119. sub_index++;
  120. }
  121. core_select_mask = (~(-1 << pkg_mask_width)) >> ht_mask_width;
  122. die_select_mask = (~(-1 << die_plus_mask_width)) >>
  123. core_plus_mask_width;
  124. c->cpu_core_id = apic->phys_pkg_id(c->initial_apicid,
  125. ht_mask_width) & core_select_mask;
  126. if (die_level_present) {
  127. c->cpu_die_id = apic->phys_pkg_id(c->initial_apicid,
  128. core_plus_mask_width) & die_select_mask;
  129. }
  130. c->phys_proc_id = apic->phys_pkg_id(c->initial_apicid,
  131. pkg_mask_width);
  132. /*
  133. * Reinit the apicid, now that we have extended initial_apicid.
  134. */
  135. c->apicid = apic->phys_pkg_id(c->initial_apicid, 0);
  136. c->x86_max_cores = (core_level_siblings / smp_num_siblings);
  137. __max_die_per_package = (die_level_siblings / core_level_siblings);
  138. #endif
  139. return 0;
  140. }