topology.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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/pat.h>
  10. #include <asm/processor.h>
  11. /* leaf 0xb SMT level */
  12. #define SMT_LEVEL 0
  13. /* leaf 0xb sub-leaf types */
  14. #define INVALID_TYPE 0
  15. #define SMT_TYPE 1
  16. #define CORE_TYPE 2
  17. #define LEAFB_SUBTYPE(ecx) (((ecx) >> 8) & 0xff)
  18. #define BITS_SHIFT_NEXT_LEVEL(eax) ((eax) & 0x1f)
  19. #define LEVEL_MAX_SIBLINGS(ebx) ((ebx) & 0xffff)
  20. int detect_extended_topology_early(struct cpuinfo_x86 *c)
  21. {
  22. #ifdef CONFIG_SMP
  23. unsigned int eax, ebx, ecx, edx;
  24. if (c->cpuid_level < 0xb)
  25. return -1;
  26. cpuid_count(0xb, SMT_LEVEL, &eax, &ebx, &ecx, &edx);
  27. /*
  28. * check if the cpuid leaf 0xb is actually implemented.
  29. */
  30. if (ebx == 0 || (LEAFB_SUBTYPE(ecx) != SMT_TYPE))
  31. return -1;
  32. set_cpu_cap(c, X86_FEATURE_XTOPOLOGY);
  33. /*
  34. * initial apic id, which also represents 32-bit extended x2apic id.
  35. */
  36. c->initial_apicid = edx;
  37. smp_num_siblings = LEVEL_MAX_SIBLINGS(ebx);
  38. #endif
  39. return 0;
  40. }
  41. /*
  42. * Check for extended topology enumeration cpuid leaf 0xb and if it
  43. * exists, use it for populating initial_apicid and cpu topology
  44. * detection.
  45. */
  46. int detect_extended_topology(struct cpuinfo_x86 *c)
  47. {
  48. #ifdef CONFIG_SMP
  49. unsigned int eax, ebx, ecx, edx, sub_index;
  50. unsigned int ht_mask_width, core_plus_mask_width;
  51. unsigned int core_select_mask, core_level_siblings;
  52. if (detect_extended_topology_early(c) < 0)
  53. return -1;
  54. /*
  55. * Populate HT related information from sub-leaf level 0.
  56. */
  57. cpuid_count(0xb, SMT_LEVEL, &eax, &ebx, &ecx, &edx);
  58. core_level_siblings = smp_num_siblings = LEVEL_MAX_SIBLINGS(ebx);
  59. core_plus_mask_width = ht_mask_width = BITS_SHIFT_NEXT_LEVEL(eax);
  60. sub_index = 1;
  61. do {
  62. cpuid_count(0xb, sub_index, &eax, &ebx, &ecx, &edx);
  63. /*
  64. * Check for the Core type in the implemented sub leaves.
  65. */
  66. if (LEAFB_SUBTYPE(ecx) == CORE_TYPE) {
  67. core_level_siblings = LEVEL_MAX_SIBLINGS(ebx);
  68. core_plus_mask_width = BITS_SHIFT_NEXT_LEVEL(eax);
  69. break;
  70. }
  71. sub_index++;
  72. } while (LEAFB_SUBTYPE(ecx) != INVALID_TYPE);
  73. core_select_mask = (~(-1 << core_plus_mask_width)) >> ht_mask_width;
  74. c->cpu_core_id = apic->phys_pkg_id(c->initial_apicid, ht_mask_width)
  75. & core_select_mask;
  76. c->phys_proc_id = apic->phys_pkg_id(c->initial_apicid, core_plus_mask_width);
  77. /*
  78. * Reinit the apicid, now that we have extended initial_apicid.
  79. */
  80. c->apicid = apic->phys_pkg_id(c->initial_apicid, 0);
  81. c->x86_max_cores = (core_level_siblings / smp_num_siblings);
  82. #endif
  83. return 0;
  84. }