topology.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * CPU/APIC topology
  4. *
  5. * The APIC IDs describe the system topology in multiple domain levels.
  6. * The CPUID topology parser provides the information which part of the
  7. * APIC ID is associated to the individual levels:
  8. *
  9. * [PACKAGE][DIEGRP][DIE][TILE][MODULE][CORE][THREAD]
  10. *
  11. * The root space contains the package (socket) IDs.
  12. *
  13. * Not enumerated levels consume 0 bits space, but conceptually they are
  14. * always represented. If e.g. only CORE and THREAD levels are enumerated
  15. * then the DIE, MODULE and TILE have the same physical ID as the PACKAGE.
  16. *
  17. * If SMT is not supported, then the THREAD domain is still used. It then
  18. * has the same physical ID as the CORE domain and is the only child of
  19. * the core domain.
  20. *
  21. * This allows a unified view on the system independent of the enumerated
  22. * domain levels without requiring any conditionals in the code.
  23. */
  24. #define pr_fmt(fmt) "CPU topo: " fmt
  25. #include <linux/cpu.h>
  26. #include <xen/xen.h>
  27. #include <asm/apic.h>
  28. #include <asm/hypervisor.h>
  29. #include <asm/io_apic.h>
  30. #include <asm/mpspec.h>
  31. #include <asm/smp.h>
  32. #include "cpu.h"
  33. /*
  34. * Map cpu index to physical APIC ID
  35. */
  36. DEFINE_EARLY_PER_CPU_READ_MOSTLY(u32, x86_cpu_to_apicid, BAD_APICID);
  37. DEFINE_EARLY_PER_CPU_READ_MOSTLY(u32, x86_cpu_to_acpiid, CPU_ACPIID_INVALID);
  38. EXPORT_EARLY_PER_CPU_SYMBOL(x86_cpu_to_apicid);
  39. EXPORT_EARLY_PER_CPU_SYMBOL(x86_cpu_to_acpiid);
  40. /* Bitmap of physically present CPUs. */
  41. DECLARE_BITMAP(phys_cpu_present_map, MAX_LOCAL_APIC) __read_mostly;
  42. /* Used for CPU number allocation and parallel CPU bringup */
  43. u32 cpuid_to_apicid[] __ro_after_init = { [0 ... NR_CPUS - 1] = BAD_APICID, };
  44. /* Bitmaps to mark registered APICs at each topology domain */
  45. static struct { DECLARE_BITMAP(map, MAX_LOCAL_APIC); } apic_maps[TOPO_MAX_DOMAIN] __ro_after_init;
  46. /*
  47. * Keep track of assigned, disabled and rejected CPUs. Present assigned
  48. * with 1 as CPU #0 is reserved for the boot CPU.
  49. */
  50. static struct {
  51. unsigned int nr_assigned_cpus;
  52. unsigned int nr_disabled_cpus;
  53. unsigned int nr_rejected_cpus;
  54. u32 boot_cpu_apic_id;
  55. u32 real_bsp_apic_id;
  56. } topo_info __ro_after_init = {
  57. .nr_assigned_cpus = 1,
  58. .boot_cpu_apic_id = BAD_APICID,
  59. .real_bsp_apic_id = BAD_APICID,
  60. };
  61. #define domain_weight(_dom) bitmap_weight(apic_maps[_dom].map, MAX_LOCAL_APIC)
  62. bool arch_match_cpu_phys_id(int cpu, u64 phys_id)
  63. {
  64. return phys_id == (u64)cpuid_to_apicid[cpu];
  65. }
  66. #ifdef CONFIG_SMP
  67. static void cpu_mark_primary_thread(unsigned int cpu, unsigned int apicid)
  68. {
  69. if (!(apicid & (__max_threads_per_core - 1)))
  70. cpumask_set_cpu(cpu, &__cpu_primary_thread_mask);
  71. }
  72. #else
  73. static inline void cpu_mark_primary_thread(unsigned int cpu, unsigned int apicid) { }
  74. #endif
  75. /*
  76. * Convert the APIC ID to a domain level ID by masking out the low bits
  77. * below the domain level @dom.
  78. */
  79. static inline u32 topo_apicid(u32 apicid, enum x86_topology_domains dom)
  80. {
  81. if (dom == TOPO_SMT_DOMAIN)
  82. return apicid;
  83. return apicid & (UINT_MAX << x86_topo_system.dom_shifts[dom - 1]);
  84. }
  85. static int topo_lookup_cpuid(u32 apic_id)
  86. {
  87. int i;
  88. /* CPU# to APICID mapping is persistent once it is established */
  89. for (i = 0; i < topo_info.nr_assigned_cpus; i++) {
  90. if (cpuid_to_apicid[i] == apic_id)
  91. return i;
  92. }
  93. return -ENODEV;
  94. }
  95. static __init int topo_get_cpunr(u32 apic_id)
  96. {
  97. int cpu = topo_lookup_cpuid(apic_id);
  98. if (cpu >= 0)
  99. return cpu;
  100. return topo_info.nr_assigned_cpus++;
  101. }
  102. static void topo_set_cpuids(unsigned int cpu, u32 apic_id, u32 acpi_id)
  103. {
  104. #if defined(CONFIG_SMP) || defined(CONFIG_X86_64)
  105. early_per_cpu(x86_cpu_to_apicid, cpu) = apic_id;
  106. early_per_cpu(x86_cpu_to_acpiid, cpu) = acpi_id;
  107. #endif
  108. set_cpu_present(cpu, true);
  109. }
  110. static __init bool check_for_real_bsp(u32 apic_id)
  111. {
  112. bool is_bsp = false, has_apic_base = boot_cpu_data.x86 >= 6;
  113. u64 msr;
  114. /*
  115. * There is no real good way to detect whether this a kdump()
  116. * kernel, but except on the Voyager SMP monstrosity which is not
  117. * longer supported, the real BSP APIC ID is the first one which is
  118. * enumerated by firmware. That allows to detect whether the boot
  119. * CPU is the real BSP. If it is not, then do not register the APIC
  120. * because sending INIT to the real BSP would reset the whole
  121. * system.
  122. *
  123. * The first APIC ID which is enumerated by firmware is detectable
  124. * because the boot CPU APIC ID is registered before that without
  125. * invoking this code.
  126. */
  127. if (topo_info.real_bsp_apic_id != BAD_APICID)
  128. return false;
  129. /*
  130. * Check whether the enumeration order is broken by evaluating the
  131. * BSP bit in the APICBASE MSR. If the CPU does not have the
  132. * APICBASE MSR then the BSP detection is not possible and the
  133. * kernel must rely on the firmware enumeration order.
  134. */
  135. if (has_apic_base) {
  136. rdmsrl(MSR_IA32_APICBASE, msr);
  137. is_bsp = !!(msr & MSR_IA32_APICBASE_BSP);
  138. }
  139. if (apic_id == topo_info.boot_cpu_apic_id) {
  140. /*
  141. * If the boot CPU has the APIC BSP bit set then the
  142. * firmware enumeration is agreeing. If the CPU does not
  143. * have the APICBASE MSR then the only choice is to trust
  144. * the enumeration order.
  145. */
  146. if (is_bsp || !has_apic_base) {
  147. topo_info.real_bsp_apic_id = apic_id;
  148. return false;
  149. }
  150. /*
  151. * If the boot APIC is enumerated first, but the APICBASE
  152. * MSR does not have the BSP bit set, then there is no way
  153. * to discover the real BSP here. Assume a crash kernel and
  154. * limit the number of CPUs to 1 as an INIT to the real BSP
  155. * would reset the machine.
  156. */
  157. pr_warn("Enumerated BSP APIC %x is not marked in APICBASE MSR\n", apic_id);
  158. pr_warn("Assuming crash kernel. Limiting to one CPU to prevent machine INIT\n");
  159. set_nr_cpu_ids(1);
  160. goto fwbug;
  161. }
  162. pr_warn("Boot CPU APIC ID not the first enumerated APIC ID: %x != %x\n",
  163. topo_info.boot_cpu_apic_id, apic_id);
  164. if (is_bsp) {
  165. /*
  166. * The boot CPU has the APIC BSP bit set. Use it and complain
  167. * about the broken firmware enumeration.
  168. */
  169. topo_info.real_bsp_apic_id = topo_info.boot_cpu_apic_id;
  170. goto fwbug;
  171. }
  172. pr_warn("Crash kernel detected. Disabling real BSP to prevent machine INIT\n");
  173. topo_info.real_bsp_apic_id = apic_id;
  174. return true;
  175. fwbug:
  176. pr_warn(FW_BUG "APIC enumeration order not specification compliant\n");
  177. return false;
  178. }
  179. static unsigned int topo_unit_count(u32 lvlid, enum x86_topology_domains at_level,
  180. unsigned long *map)
  181. {
  182. unsigned int id, end, cnt = 0;
  183. /* Calculate the exclusive end */
  184. end = lvlid + (1U << x86_topo_system.dom_shifts[at_level]);
  185. /* Unfortunately there is no bitmap_weight_range() */
  186. for (id = find_next_bit(map, end, lvlid); id < end; id = find_next_bit(map, end, ++id))
  187. cnt++;
  188. return cnt;
  189. }
  190. static __init void topo_register_apic(u32 apic_id, u32 acpi_id, bool present)
  191. {
  192. int cpu, dom;
  193. if (present) {
  194. set_bit(apic_id, phys_cpu_present_map);
  195. /*
  196. * Double registration is valid in case of the boot CPU
  197. * APIC because that is registered before the enumeration
  198. * of the APICs via firmware parsers or VM guest
  199. * mechanisms.
  200. */
  201. if (apic_id == topo_info.boot_cpu_apic_id)
  202. cpu = 0;
  203. else
  204. cpu = topo_get_cpunr(apic_id);
  205. cpuid_to_apicid[cpu] = apic_id;
  206. topo_set_cpuids(cpu, apic_id, acpi_id);
  207. } else {
  208. u32 pkgid = topo_apicid(apic_id, TOPO_PKG_DOMAIN);
  209. /*
  210. * Check for present APICs in the same package when running
  211. * on bare metal. Allow the bogosity in a guest.
  212. */
  213. if (hypervisor_is_type(X86_HYPER_NATIVE) &&
  214. topo_unit_count(pkgid, TOPO_PKG_DOMAIN, phys_cpu_present_map)) {
  215. pr_info_once("Ignoring hot-pluggable APIC ID %x in present package.\n",
  216. apic_id);
  217. topo_info.nr_rejected_cpus++;
  218. return;
  219. }
  220. topo_info.nr_disabled_cpus++;
  221. }
  222. /*
  223. * Register present and possible CPUs in the domain
  224. * maps. cpu_possible_map will be updated in
  225. * topology_init_possible_cpus() after enumeration is done.
  226. */
  227. for (dom = TOPO_SMT_DOMAIN; dom < TOPO_MAX_DOMAIN; dom++)
  228. set_bit(topo_apicid(apic_id, dom), apic_maps[dom].map);
  229. }
  230. /**
  231. * topology_register_apic - Register an APIC in early topology maps
  232. * @apic_id: The APIC ID to set up
  233. * @acpi_id: The ACPI ID associated to the APIC
  234. * @present: True if the corresponding CPU is present
  235. */
  236. void __init topology_register_apic(u32 apic_id, u32 acpi_id, bool present)
  237. {
  238. if (apic_id >= MAX_LOCAL_APIC) {
  239. pr_err_once("APIC ID %x exceeds kernel limit of: %x\n", apic_id, MAX_LOCAL_APIC - 1);
  240. topo_info.nr_rejected_cpus++;
  241. return;
  242. }
  243. if (check_for_real_bsp(apic_id)) {
  244. topo_info.nr_rejected_cpus++;
  245. return;
  246. }
  247. /* CPU numbers exhausted? */
  248. if (apic_id != topo_info.boot_cpu_apic_id && topo_info.nr_assigned_cpus >= nr_cpu_ids) {
  249. pr_warn_once("CPU limit of %d reached. Ignoring further CPUs\n", nr_cpu_ids);
  250. topo_info.nr_rejected_cpus++;
  251. return;
  252. }
  253. topo_register_apic(apic_id, acpi_id, present);
  254. }
  255. /**
  256. * topology_register_boot_apic - Register the boot CPU APIC
  257. * @apic_id: The APIC ID to set up
  258. *
  259. * Separate so CPU #0 can be assigned
  260. */
  261. void __init topology_register_boot_apic(u32 apic_id)
  262. {
  263. WARN_ON_ONCE(topo_info.boot_cpu_apic_id != BAD_APICID);
  264. topo_info.boot_cpu_apic_id = apic_id;
  265. topo_register_apic(apic_id, CPU_ACPIID_INVALID, true);
  266. }
  267. /**
  268. * topology_get_logical_id - Retrieve the logical ID at a given topology domain level
  269. * @apicid: The APIC ID for which to lookup the logical ID
  270. * @at_level: The topology domain level to use
  271. *
  272. * @apicid must be a full APIC ID, not the normalized variant. It's valid to have
  273. * all bits below the domain level specified by @at_level to be clear. So both
  274. * real APIC IDs and backshifted normalized APIC IDs work correctly.
  275. *
  276. * Returns:
  277. * - >= 0: The requested logical ID
  278. * - -ERANGE: @apicid is out of range
  279. * - -ENODEV: @apicid is not registered
  280. */
  281. int topology_get_logical_id(u32 apicid, enum x86_topology_domains at_level)
  282. {
  283. /* Remove the bits below @at_level to get the proper level ID of @apicid */
  284. unsigned int lvlid = topo_apicid(apicid, at_level);
  285. if (lvlid >= MAX_LOCAL_APIC)
  286. return -ERANGE;
  287. if (!test_bit(lvlid, apic_maps[at_level].map))
  288. return -ENODEV;
  289. /* Get the number of set bits before @lvlid. */
  290. return bitmap_weight(apic_maps[at_level].map, lvlid);
  291. }
  292. EXPORT_SYMBOL_GPL(topology_get_logical_id);
  293. /**
  294. * topology_unit_count - Retrieve the count of specified units at a given topology domain level
  295. * @apicid: The APIC ID which specifies the search range
  296. * @which_units: The domain level specifying the units to count
  297. * @at_level: The domain level at which @which_units have to be counted
  298. *
  299. * This returns the number of possible units according to the enumerated
  300. * information.
  301. *
  302. * E.g. topology_count_units(apicid, TOPO_CORE_DOMAIN, TOPO_PKG_DOMAIN)
  303. * counts the number of possible cores in the package to which @apicid
  304. * belongs.
  305. *
  306. * @at_level must obviously be greater than @which_level to produce useful
  307. * results. If @at_level is equal to @which_units the result is
  308. * unsurprisingly 1. If @at_level is less than @which_units the results
  309. * is by definition undefined and the function returns 0.
  310. */
  311. unsigned int topology_unit_count(u32 apicid, enum x86_topology_domains which_units,
  312. enum x86_topology_domains at_level)
  313. {
  314. /* Remove the bits below @at_level to get the proper level ID of @apicid */
  315. unsigned int lvlid = topo_apicid(apicid, at_level);
  316. if (lvlid >= MAX_LOCAL_APIC)
  317. return 0;
  318. if (!test_bit(lvlid, apic_maps[at_level].map))
  319. return 0;
  320. if (which_units > at_level)
  321. return 0;
  322. if (which_units == at_level)
  323. return 1;
  324. return topo_unit_count(lvlid, at_level, apic_maps[which_units].map);
  325. }
  326. #ifdef CONFIG_ACPI_HOTPLUG_CPU
  327. /**
  328. * topology_hotplug_apic - Handle a physical hotplugged APIC after boot
  329. * @apic_id: The APIC ID to set up
  330. * @acpi_id: The ACPI ID associated to the APIC
  331. */
  332. int topology_hotplug_apic(u32 apic_id, u32 acpi_id)
  333. {
  334. int cpu;
  335. if (apic_id >= MAX_LOCAL_APIC)
  336. return -EINVAL;
  337. /* Reject if the APIC ID was not registered during enumeration. */
  338. if (!test_bit(apic_id, apic_maps[TOPO_SMT_DOMAIN].map))
  339. return -ENODEV;
  340. cpu = topo_lookup_cpuid(apic_id);
  341. if (cpu < 0)
  342. return -ENOSPC;
  343. set_bit(apic_id, phys_cpu_present_map);
  344. topo_set_cpuids(cpu, apic_id, acpi_id);
  345. cpu_mark_primary_thread(cpu, apic_id);
  346. return cpu;
  347. }
  348. /**
  349. * topology_hotunplug_apic - Remove a physical hotplugged APIC after boot
  350. * @cpu: The CPU number for which the APIC ID is removed
  351. */
  352. void topology_hotunplug_apic(unsigned int cpu)
  353. {
  354. u32 apic_id = cpuid_to_apicid[cpu];
  355. if (apic_id == BAD_APICID)
  356. return;
  357. per_cpu(x86_cpu_to_apicid, cpu) = BAD_APICID;
  358. clear_bit(apic_id, phys_cpu_present_map);
  359. set_cpu_present(cpu, false);
  360. }
  361. #endif
  362. #ifdef CONFIG_X86_LOCAL_APIC
  363. static unsigned int max_possible_cpus __initdata = NR_CPUS;
  364. /**
  365. * topology_apply_cmdline_limits_early - Apply topology command line limits early
  366. *
  367. * Ensure that command line limits are in effect before firmware parsing
  368. * takes place.
  369. */
  370. void __init topology_apply_cmdline_limits_early(void)
  371. {
  372. unsigned int possible = nr_cpu_ids;
  373. /* 'maxcpus=0' 'nosmp' 'nolapic' 'disableapic' */
  374. if (!setup_max_cpus || apic_is_disabled)
  375. possible = 1;
  376. /* 'possible_cpus=N' */
  377. possible = min_t(unsigned int, max_possible_cpus, possible);
  378. if (possible < nr_cpu_ids) {
  379. pr_info("Limiting to %u possible CPUs\n", possible);
  380. set_nr_cpu_ids(possible);
  381. }
  382. }
  383. static __init bool restrict_to_up(void)
  384. {
  385. if (!smp_found_config)
  386. return true;
  387. /*
  388. * XEN PV is special as it does not advertise the local APIC
  389. * properly, but provides a fake topology for it so that the
  390. * infrastructure works. So don't apply the restrictions vs. APIC
  391. * here.
  392. */
  393. if (xen_pv_domain())
  394. return false;
  395. return apic_is_disabled;
  396. }
  397. void __init topology_init_possible_cpus(void)
  398. {
  399. unsigned int assigned = topo_info.nr_assigned_cpus;
  400. unsigned int disabled = topo_info.nr_disabled_cpus;
  401. unsigned int cnta, cntb, cpu, allowed = 1;
  402. unsigned int total = assigned + disabled;
  403. u32 apicid, firstid;
  404. /*
  405. * If there was no APIC registered, then fake one so that the
  406. * topology bitmap is populated. That ensures that the code below
  407. * is valid and the various query interfaces can be used
  408. * unconditionally. This does not affect the actual APIC code in
  409. * any way because either the local APIC address has not been
  410. * registered or the local APIC was disabled on the command line.
  411. */
  412. if (topo_info.boot_cpu_apic_id == BAD_APICID)
  413. topology_register_boot_apic(0);
  414. if (!restrict_to_up()) {
  415. if (WARN_ON_ONCE(assigned > nr_cpu_ids)) {
  416. disabled += assigned - nr_cpu_ids;
  417. assigned = nr_cpu_ids;
  418. }
  419. allowed = min_t(unsigned int, total, nr_cpu_ids);
  420. }
  421. if (total > allowed)
  422. pr_warn("%u possible CPUs exceed the limit of %u\n", total, allowed);
  423. assigned = min_t(unsigned int, allowed, assigned);
  424. disabled = allowed - assigned;
  425. topo_info.nr_assigned_cpus = assigned;
  426. topo_info.nr_disabled_cpus = disabled;
  427. total_cpus = allowed;
  428. set_nr_cpu_ids(allowed);
  429. cnta = domain_weight(TOPO_PKG_DOMAIN);
  430. cntb = domain_weight(TOPO_DIE_DOMAIN);
  431. __max_logical_packages = cnta;
  432. __max_dies_per_package = 1U << (get_count_order(cntb) - get_count_order(cnta));
  433. pr_info("Max. logical packages: %3u\n", cnta);
  434. pr_info("Max. logical dies: %3u\n", cntb);
  435. pr_info("Max. dies per package: %3u\n", __max_dies_per_package);
  436. cnta = domain_weight(TOPO_CORE_DOMAIN);
  437. cntb = domain_weight(TOPO_SMT_DOMAIN);
  438. /*
  439. * Can't use order delta here as order(cnta) can be equal
  440. * order(cntb) even if cnta != cntb.
  441. */
  442. __max_threads_per_core = DIV_ROUND_UP(cntb, cnta);
  443. pr_info("Max. threads per core: %3u\n", __max_threads_per_core);
  444. firstid = find_first_bit(apic_maps[TOPO_SMT_DOMAIN].map, MAX_LOCAL_APIC);
  445. __num_cores_per_package = topology_unit_count(firstid, TOPO_CORE_DOMAIN, TOPO_PKG_DOMAIN);
  446. pr_info("Num. cores per package: %3u\n", __num_cores_per_package);
  447. __num_threads_per_package = topology_unit_count(firstid, TOPO_SMT_DOMAIN, TOPO_PKG_DOMAIN);
  448. pr_info("Num. threads per package: %3u\n", __num_threads_per_package);
  449. pr_info("Allowing %u present CPUs plus %u hotplug CPUs\n", assigned, disabled);
  450. if (topo_info.nr_rejected_cpus)
  451. pr_info("Rejected CPUs %u\n", topo_info.nr_rejected_cpus);
  452. init_cpu_present(cpumask_of(0));
  453. init_cpu_possible(cpumask_of(0));
  454. /* Assign CPU numbers to non-present CPUs */
  455. for (apicid = 0; disabled; disabled--, apicid++) {
  456. apicid = find_next_andnot_bit(apic_maps[TOPO_SMT_DOMAIN].map, phys_cpu_present_map,
  457. MAX_LOCAL_APIC, apicid);
  458. if (apicid >= MAX_LOCAL_APIC)
  459. break;
  460. cpuid_to_apicid[topo_info.nr_assigned_cpus++] = apicid;
  461. }
  462. for (cpu = 0; cpu < allowed; cpu++) {
  463. apicid = cpuid_to_apicid[cpu];
  464. set_cpu_possible(cpu, true);
  465. if (apicid == BAD_APICID)
  466. continue;
  467. cpu_mark_primary_thread(cpu, apicid);
  468. set_cpu_present(cpu, test_bit(apicid, phys_cpu_present_map));
  469. }
  470. }
  471. /*
  472. * Late SMP disable after sizing CPU masks when APIC/IOAPIC setup failed.
  473. */
  474. void __init topology_reset_possible_cpus_up(void)
  475. {
  476. init_cpu_present(cpumask_of(0));
  477. init_cpu_possible(cpumask_of(0));
  478. bitmap_zero(phys_cpu_present_map, MAX_LOCAL_APIC);
  479. if (topo_info.boot_cpu_apic_id != BAD_APICID)
  480. set_bit(topo_info.boot_cpu_apic_id, phys_cpu_present_map);
  481. }
  482. static int __init setup_possible_cpus(char *str)
  483. {
  484. get_option(&str, &max_possible_cpus);
  485. return 0;
  486. }
  487. early_param("possible_cpus", setup_possible_cpus);
  488. #endif