2 * Copyright (c) 2003-2016 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 #include <vm/vm_kern.h>
29 #include <kern/kalloc.h>
30 #include <kern/timer_queue.h>
31 #include <mach/machine.h>
32 #include <i386/cpu_threads.h>
33 #include <i386/cpuid.h>
34 #include <i386/machine_cpu.h>
35 #include <i386/pmCPU.h>
36 #include <i386/bit_routines.h>
39 #include <kern/monotonic.h>
40 #endif /* MONOTONIC */
42 #define DIVISOR_GUARD(denom) \
44 kprintf("%s: %d Zero divisor: " #denom, \
45 __FILE__, __LINE__); \
48 static void debug_topology_print(void);
50 boolean_t topo_dbg
= FALSE
;
52 x86_pkg_t
*x86_pkgs
= NULL
;
53 uint32_t num_Lx_caches
[MAX_CACHE_DEPTH
] = { 0 };
55 static x86_pkg_t
*free_pkgs
= NULL
;
56 static x86_die_t
*free_dies
= NULL
;
57 static x86_core_t
*free_cores
= NULL
;
58 static uint32_t num_dies
= 0;
60 static x86_cpu_cache_t
*x86_caches
= NULL
;
61 static uint32_t num_caches
= 0;
63 static boolean_t topoParmsInited
= FALSE
;
64 x86_topology_parameters_t topoParms
;
66 decl_simple_lock_data(, x86_topo_lock
);
68 static struct cpu_cache
{
70 } cpu_caches
[LCACHE_MAX
] = {
71 [L1D
] = { 1, CPU_CACHE_TYPE_DATA
},
72 [L1I
] = { 1, CPU_CACHE_TYPE_INST
},
73 [L2U
] = { 2, CPU_CACHE_TYPE_UNIF
},
74 [L3U
] = { 3, CPU_CACHE_TYPE_UNIF
},
78 cpu_is_hyperthreaded(void)
80 i386_cpu_info_t
*cpuinfo
;
82 cpuinfo
= cpuid_info();
83 return(cpuinfo
->thread_count
> cpuinfo
->core_count
);
86 static x86_cpu_cache_t
*
89 x86_cpu_cache_t
*cache
;
92 if (x86_caches
== NULL
) {
93 cache
= kalloc(sizeof(x86_cpu_cache_t
) + (MAX_CPUS
* sizeof(x86_lcpu_t
*)));
98 x86_caches
= cache
->next
;
102 bzero(cache
, sizeof(x86_cpu_cache_t
));
104 cache
->maxcpus
= MAX_CPUS
;
105 for (i
= 0; i
< cache
->maxcpus
; i
+= 1) {
106 cache
->cpus
[i
] = NULL
;
118 uint32_t nCPUsSharing
= 1;
119 i386_cpu_info_t
*cpuinfo
;
120 struct cpu_cache
*cachep
;
123 cpuinfo
= cpuid_info();
125 for (i
= 0, cachep
= &cpu_caches
[0]; i
< LCACHE_MAX
; i
++, cachep
++) {
127 if (cachep
->type
== 0 || cpuid_info()->cache_size
[i
] == 0)
131 * Only worry about it if it's a deeper level than
132 * what we've seen before.
134 if (cachep
->level
> cache_level
) {
135 cache_level
= cachep
->level
;
138 * Save the number of CPUs sharing this cache.
140 nCPUsSharing
= cpuinfo
->cache_sharing
[i
];
145 * Make the level of the LLC be 0 based.
147 topoParms
.LLCDepth
= cache_level
- 1;
150 * nCPUsSharing represents the *maximum* number of cores or
151 * logical CPUs sharing the cache.
153 topoParms
.maxSharingLLC
= nCPUsSharing
;
155 topoParms
.nCoresSharingLLC
= nCPUsSharing
/ (cpuinfo
->thread_count
/
156 cpuinfo
->core_count
);
157 topoParms
.nLCPUsSharingLLC
= nCPUsSharing
;
160 * nCPUsSharing may not be the number of *active* cores or
161 * threads that are sharing the cache.
163 if (nCPUsSharing
> cpuinfo
->core_count
)
164 topoParms
.nCoresSharingLLC
= cpuinfo
->core_count
;
165 if (nCPUsSharing
> cpuinfo
->thread_count
)
166 topoParms
.nLCPUsSharingLLC
= cpuinfo
->thread_count
;
172 i386_cpu_info_t
*cpuinfo
;
174 topoParms
.stable
= FALSE
;
176 cpuinfo
= cpuid_info();
178 PE_parse_boot_argn("-topo", &topo_dbg
, sizeof(topo_dbg
));
181 * We need to start with getting the LLC information correct.
186 * Compute the number of threads (logical CPUs) per core.
188 DIVISOR_GUARD(cpuinfo
->core_count
);
189 topoParms
.nLThreadsPerCore
= cpuinfo
->thread_count
/ cpuinfo
->core_count
;
190 DIVISOR_GUARD(cpuinfo
->cpuid_cores_per_package
);
191 topoParms
.nPThreadsPerCore
= cpuinfo
->cpuid_logical_per_package
/ cpuinfo
->cpuid_cores_per_package
;
194 * Compute the number of dies per package.
196 DIVISOR_GUARD(topoParms
.nCoresSharingLLC
);
197 topoParms
.nLDiesPerPackage
= cpuinfo
->core_count
/ topoParms
.nCoresSharingLLC
;
198 DIVISOR_GUARD(topoParms
.nPThreadsPerCore
);
199 DIVISOR_GUARD(topoParms
.maxSharingLLC
/ topoParms
.nPThreadsPerCore
);
200 topoParms
.nPDiesPerPackage
= cpuinfo
->cpuid_cores_per_package
/ (topoParms
.maxSharingLLC
/ topoParms
.nPThreadsPerCore
);
204 * Compute the number of cores per die.
206 topoParms
.nLCoresPerDie
= topoParms
.nCoresSharingLLC
;
207 topoParms
.nPCoresPerDie
= (topoParms
.maxSharingLLC
/ topoParms
.nPThreadsPerCore
);
210 * Compute the number of threads per die.
212 topoParms
.nLThreadsPerDie
= topoParms
.nLThreadsPerCore
* topoParms
.nLCoresPerDie
;
213 topoParms
.nPThreadsPerDie
= topoParms
.nPThreadsPerCore
* topoParms
.nPCoresPerDie
;
216 * Compute the number of cores per package.
218 topoParms
.nLCoresPerPackage
= topoParms
.nLCoresPerDie
* topoParms
.nLDiesPerPackage
;
219 topoParms
.nPCoresPerPackage
= topoParms
.nPCoresPerDie
* topoParms
.nPDiesPerPackage
;
222 * Compute the number of threads per package.
224 topoParms
.nLThreadsPerPackage
= topoParms
.nLThreadsPerCore
* topoParms
.nLCoresPerPackage
;
225 topoParms
.nPThreadsPerPackage
= topoParms
.nPThreadsPerCore
* topoParms
.nPCoresPerPackage
;
227 TOPO_DBG("\nCache Topology Parameters:\n");
228 TOPO_DBG("\tLLC Depth: %d\n", topoParms
.LLCDepth
);
229 TOPO_DBG("\tCores Sharing LLC: %d\n", topoParms
.nCoresSharingLLC
);
230 TOPO_DBG("\tThreads Sharing LLC: %d\n", topoParms
.nLCPUsSharingLLC
);
231 TOPO_DBG("\tmax Sharing of LLC: %d\n", topoParms
.maxSharingLLC
);
233 TOPO_DBG("\nLogical Topology Parameters:\n");
234 TOPO_DBG("\tThreads per Core: %d\n", topoParms
.nLThreadsPerCore
);
235 TOPO_DBG("\tCores per Die: %d\n", topoParms
.nLCoresPerDie
);
236 TOPO_DBG("\tThreads per Die: %d\n", topoParms
.nLThreadsPerDie
);
237 TOPO_DBG("\tDies per Package: %d\n", topoParms
.nLDiesPerPackage
);
238 TOPO_DBG("\tCores per Package: %d\n", topoParms
.nLCoresPerPackage
);
239 TOPO_DBG("\tThreads per Package: %d\n", topoParms
.nLThreadsPerPackage
);
241 TOPO_DBG("\nPhysical Topology Parameters:\n");
242 TOPO_DBG("\tThreads per Core: %d\n", topoParms
.nPThreadsPerCore
);
243 TOPO_DBG("\tCores per Die: %d\n", topoParms
.nPCoresPerDie
);
244 TOPO_DBG("\tThreads per Die: %d\n", topoParms
.nPThreadsPerDie
);
245 TOPO_DBG("\tDies per Package: %d\n", topoParms
.nPDiesPerPackage
);
246 TOPO_DBG("\tCores per Package: %d\n", topoParms
.nPCoresPerPackage
);
247 TOPO_DBG("\tThreads per Package: %d\n", topoParms
.nPThreadsPerPackage
);
249 topoParmsInited
= TRUE
;
253 x86_cache_free(x86_cpu_cache_t
*cache
)
256 if (cache
->level
> 0 && cache
->level
<= MAX_CACHE_DEPTH
)
257 num_Lx_caches
[cache
->level
- 1] -= 1;
258 cache
->next
= x86_caches
;
263 * This returns a list of cache structures that represent the
264 * caches for a CPU. Some of the structures may have to be
265 * "freed" if they are actually shared between CPUs.
267 static x86_cpu_cache_t
*
270 x86_cpu_cache_t
*root
= NULL
;
271 x86_cpu_cache_t
*cur
= NULL
;
272 x86_cpu_cache_t
*last
= NULL
;
273 struct cpu_cache
*cachep
;
277 * Cons up a list driven not by CPUID leaf 4 (deterministic cache params)
278 * but by the table above plus parameters already cracked from cpuid...
280 for (i
= 0, cachep
= &cpu_caches
[0]; i
< LCACHE_MAX
; i
++, cachep
++) {
282 if (cachep
->type
== 0 || cpuid_info()->cache_size
[i
] == 0)
285 cur
= x86_cache_alloc();
289 cur
->type
= cachep
->type
;
290 cur
->level
= cachep
->level
;
292 cur
->maxcpus
= cpuid_info()->cache_sharing
[i
];
293 cur
->partitions
= cpuid_info()->cache_partitions
[i
];
294 cur
->cache_size
= cpuid_info()->cache_size
[i
];
295 cur
->line_size
= cpuid_info()->cache_linesize
;
304 num_Lx_caches
[cur
->level
- 1] += 1;
310 static x86_cpu_cache_t
*
311 x86_match_cache(x86_cpu_cache_t
*list
, x86_cpu_cache_t
*matcher
)
313 x86_cpu_cache_t
*cur_cache
;
316 while (cur_cache
!= NULL
) {
317 if (cur_cache
->maxcpus
== matcher
->maxcpus
318 && cur_cache
->type
== matcher
->type
319 && cur_cache
->level
== matcher
->level
320 && cur_cache
->partitions
== matcher
->partitions
321 && cur_cache
->line_size
== matcher
->line_size
322 && cur_cache
->cache_size
== matcher
->cache_size
)
325 cur_cache
= cur_cache
->next
;
332 x86_lcpu_init(int cpu
)
338 cpup
= cpu_datap(cpu
);
343 lcpu
->next_in_core
= NULL
;
344 lcpu
->next_in_die
= NULL
;
345 lcpu
->next_in_pkg
= NULL
;
348 lcpu
->package
= NULL
;
351 lcpu
->pnum
= cpup
->cpu_phys_number
;
352 lcpu
->state
= LCPU_OFF
;
353 for (i
= 0; i
< MAX_CACHE_DEPTH
; i
+= 1)
354 lcpu
->caches
[i
] = NULL
;
358 x86_core_alloc(int cpu
)
363 cpup
= cpu_datap(cpu
);
365 mp_safe_spin_lock(&x86_topo_lock
);
366 if (free_cores
!= NULL
) {
368 free_cores
= core
->next_in_die
;
369 core
->next_in_die
= NULL
;
370 simple_unlock(&x86_topo_lock
);
372 simple_unlock(&x86_topo_lock
);
373 core
= kalloc(sizeof(x86_core_t
));
375 panic("x86_core_alloc() kalloc of x86_core_t failed!\n");
378 bzero((void *) core
, sizeof(x86_core_t
));
380 core
->pcore_num
= cpup
->cpu_phys_number
/ topoParms
.nPThreadsPerCore
;
381 core
->lcore_num
= core
->pcore_num
% topoParms
.nPCoresPerPackage
;
383 core
->flags
= X86CORE_FL_PRESENT
| X86CORE_FL_READY
384 | X86CORE_FL_HALTED
| X86CORE_FL_IDLE
;
390 x86_core_free(x86_core_t
*core
)
392 mp_safe_spin_lock(&x86_topo_lock
);
393 core
->next_in_die
= free_cores
;
395 simple_unlock(&x86_topo_lock
);
399 x86_package_find(int cpu
)
405 cpup
= cpu_datap(cpu
);
407 pkg_num
= cpup
->cpu_phys_number
/ topoParms
.nPThreadsPerPackage
;
410 while (pkg
!= NULL
) {
411 if (pkg
->ppkg_num
== pkg_num
)
420 x86_die_find(int cpu
)
427 cpup
= cpu_datap(cpu
);
429 die_num
= cpup
->cpu_phys_number
/ topoParms
.nPThreadsPerDie
;
431 pkg
= x86_package_find(cpu
);
436 while (die
!= NULL
) {
437 if (die
->pdie_num
== die_num
)
439 die
= die
->next_in_pkg
;
446 x86_core_find(int cpu
)
453 cpup
= cpu_datap(cpu
);
455 core_num
= cpup
->cpu_phys_number
/ topoParms
.nPThreadsPerCore
;
457 die
= x86_die_find(cpu
);
462 while (core
!= NULL
) {
463 if (core
->pcore_num
== core_num
)
465 core
= core
->next_in_die
;
472 x86_set_logical_topology(x86_lcpu_t
*lcpu
, int pnum
, int lnum
)
474 x86_core_t
*core
= lcpu
->core
;
475 x86_die_t
*die
= lcpu
->die
;
476 x86_pkg_t
*pkg
= lcpu
->package
;
478 assert(core
!= NULL
);
482 lcpu
->cpu_num
= lnum
;
484 lcpu
->master
= (lnum
== master_cpu
);
485 lcpu
->primary
= (lnum
% topoParms
.nLThreadsPerPackage
) == 0;
487 lcpu
->lnum
= lnum
% topoParms
.nLThreadsPerCore
;
489 core
->pcore_num
= lnum
/ topoParms
.nLThreadsPerCore
;
490 core
->lcore_num
= core
->pcore_num
% topoParms
.nLCoresPerDie
;
492 die
->pdie_num
= lnum
/ (topoParms
.nLThreadsPerCore
*topoParms
.nLCoresPerDie
);
493 die
->ldie_num
= die
->pdie_num
% topoParms
.nLDiesPerPackage
;
495 pkg
->ppkg_num
= lnum
/ topoParms
.nLThreadsPerPackage
;
496 pkg
->lpkg_num
= pkg
->ppkg_num
;
501 x86_die_alloc(int cpu
)
506 cpup
= cpu_datap(cpu
);
508 mp_safe_spin_lock(&x86_topo_lock
);
509 if (free_dies
!= NULL
) {
511 free_dies
= die
->next_in_pkg
;
512 die
->next_in_pkg
= NULL
;
513 simple_unlock(&x86_topo_lock
);
515 simple_unlock(&x86_topo_lock
);
516 die
= kalloc(sizeof(x86_die_t
));
518 panic("x86_die_alloc() kalloc of x86_die_t failed!\n");
521 bzero((void *) die
, sizeof(x86_die_t
));
523 die
->pdie_num
= cpup
->cpu_phys_number
/ topoParms
.nPThreadsPerDie
;
525 die
->ldie_num
= num_dies
;
526 atomic_incl((long *) &num_dies
, 1);
528 die
->flags
= X86DIE_FL_PRESENT
;
533 x86_die_free(x86_die_t
*die
)
535 mp_safe_spin_lock(&x86_topo_lock
);
536 die
->next_in_pkg
= free_dies
;
538 atomic_decl((long *) &num_dies
, 1);
539 simple_unlock(&x86_topo_lock
);
543 x86_package_alloc(int cpu
)
548 cpup
= cpu_datap(cpu
);
550 mp_safe_spin_lock(&x86_topo_lock
);
551 if (free_pkgs
!= NULL
) {
553 free_pkgs
= pkg
->next
;
555 simple_unlock(&x86_topo_lock
);
557 simple_unlock(&x86_topo_lock
);
558 pkg
= kalloc(sizeof(x86_pkg_t
));
560 panic("x86_package_alloc() kalloc of x86_pkg_t failed!\n");
563 bzero((void *) pkg
, sizeof(x86_pkg_t
));
565 pkg
->ppkg_num
= cpup
->cpu_phys_number
/ topoParms
.nPThreadsPerPackage
;
567 pkg
->lpkg_num
= topoParms
.nPackages
;
568 atomic_incl((long *) &topoParms
.nPackages
, 1);
570 pkg
->flags
= X86PKG_FL_PRESENT
| X86PKG_FL_READY
;
575 x86_package_free(x86_pkg_t
*pkg
)
577 mp_safe_spin_lock(&x86_topo_lock
);
578 pkg
->next
= free_pkgs
;
580 atomic_decl((long *) &topoParms
.nPackages
, 1);
581 simple_unlock(&x86_topo_lock
);
585 x86_cache_add_lcpu(x86_cpu_cache_t
*cache
, x86_lcpu_t
*lcpu
)
587 x86_cpu_cache_t
*cur_cache
;
591 * Put the new CPU into the list of the cache.
593 cur_cache
= lcpu
->caches
[cache
->level
- 1];
594 lcpu
->caches
[cache
->level
- 1] = cache
;
595 cache
->next
= cur_cache
;
597 for (i
= 0; i
< cache
->nlcpus
; i
+= 1) {
598 if (cache
->cpus
[i
] == NULL
) {
599 cache
->cpus
[i
] = lcpu
;
606 x86_lcpu_add_caches(x86_lcpu_t
*lcpu
)
608 x86_cpu_cache_t
*list
;
609 x86_cpu_cache_t
*cur
;
610 x86_cpu_cache_t
*match
;
613 x86_lcpu_t
*cur_lcpu
;
615 boolean_t found
= FALSE
;
617 assert(lcpu
!= NULL
);
620 * Add the cache data to the topology.
622 list
= x86_cache_list();
624 mp_safe_spin_lock(&x86_topo_lock
);
626 while (list
!= NULL
) {
628 * Remove the cache from the front of the list.
633 level
= cur
->level
- 1;
636 * If the cache isn't shared then just put it where it
639 if (cur
->maxcpus
== 1) {
640 x86_cache_add_lcpu(cur
, lcpu
);
645 * We'll assume that all of the caches at a particular level
646 * have the same sharing. So if we have a cache already at
647 * this level, we'll just skip looking for the match.
649 if (lcpu
->caches
[level
] != NULL
) {
655 * This is a shared cache, so we have to figure out if
656 * this is the first time we've seen this cache. We do
657 * this by searching through the topology and seeing if
658 * this cache is already described.
660 * Assume that L{LLC-1} are all at the core level and that
661 * LLC is shared at the die level.
663 if (level
< topoParms
.LLCDepth
) {
665 * Shared at the core.
668 cur_lcpu
= core
->lcpus
;
669 while (cur_lcpu
!= NULL
) {
673 if (cur_lcpu
== lcpu
) {
674 cur_lcpu
= cur_lcpu
->next_in_core
;
679 * If there's a cache on this logical CPU,
682 match
= x86_match_cache(cur_lcpu
->caches
[level
], cur
);
685 x86_cache_add_lcpu(match
, lcpu
);
690 cur_lcpu
= cur_lcpu
->next_in_core
;
697 cur_lcpu
= die
->lcpus
;
698 while (cur_lcpu
!= NULL
) {
702 if (cur_lcpu
== lcpu
) {
703 cur_lcpu
= cur_lcpu
->next_in_die
;
708 * If there's a cache on this logical CPU,
711 match
= x86_match_cache(cur_lcpu
->caches
[level
], cur
);
714 x86_cache_add_lcpu(match
, lcpu
);
719 cur_lcpu
= cur_lcpu
->next_in_die
;
724 * If a shared cache wasn't found, then this logical CPU must
725 * be the first one encountered.
728 x86_cache_add_lcpu(cur
, lcpu
);
732 simple_unlock(&x86_topo_lock
);
736 x86_core_add_lcpu(x86_core_t
*core
, x86_lcpu_t
*lcpu
)
738 assert(core
!= NULL
);
739 assert(lcpu
!= NULL
);
741 mp_safe_spin_lock(&x86_topo_lock
);
743 lcpu
->next_in_core
= core
->lcpus
;
746 core
->num_lcpus
+= 1;
747 simple_unlock(&x86_topo_lock
);
751 x86_die_add_lcpu(x86_die_t
*die
, x86_lcpu_t
*lcpu
)
754 assert(lcpu
!= NULL
);
756 lcpu
->next_in_die
= die
->lcpus
;
762 x86_die_add_core(x86_die_t
*die
, x86_core_t
*core
)
765 assert(core
!= NULL
);
767 core
->next_in_die
= die
->cores
;
774 x86_package_add_lcpu(x86_pkg_t
*pkg
, x86_lcpu_t
*lcpu
)
777 assert(lcpu
!= NULL
);
779 lcpu
->next_in_pkg
= pkg
->lcpus
;
785 x86_package_add_core(x86_pkg_t
*pkg
, x86_core_t
*core
)
788 assert(core
!= NULL
);
790 core
->next_in_pkg
= pkg
->cores
;
796 x86_package_add_die(x86_pkg_t
*pkg
, x86_die_t
*die
)
801 die
->next_in_pkg
= pkg
->dies
;
808 cpu_thread_alloc(int cpu
)
810 x86_core_t
*core
= NULL
;
811 x86_die_t
*die
= NULL
;
812 x86_pkg_t
*pkg
= NULL
;
817 * Only allow one to manipulate the topology at a time.
819 mp_safe_spin_lock(&x86_topo_lock
);
822 * Make sure all of the topology parameters have been initialized.
824 if (!topoParmsInited
)
827 cpup
= cpu_datap(cpu
);
829 phys_cpu
= cpup
->cpu_phys_number
;
834 * Assume that all cpus have the same features.
836 if (cpu_is_hyperthreaded()) {
837 cpup
->cpu_threadtype
= CPU_THREADTYPE_INTEL_HTT
;
839 cpup
->cpu_threadtype
= CPU_THREADTYPE_NONE
;
843 * Get the package that the logical CPU is in.
846 pkg
= x86_package_find(cpu
);
849 * Package structure hasn't been created yet, do it now.
851 simple_unlock(&x86_topo_lock
);
852 pkg
= x86_package_alloc(cpu
);
853 mp_safe_spin_lock(&x86_topo_lock
);
854 if (x86_package_find(cpu
) != NULL
) {
855 x86_package_free(pkg
);
860 * Add the new package to the global list of packages.
862 pkg
->next
= x86_pkgs
;
865 } while (pkg
== NULL
);
868 * Get the die that the logical CPU is in.
871 die
= x86_die_find(cpu
);
874 * Die structure hasn't been created yet, do it now.
876 simple_unlock(&x86_topo_lock
);
877 die
= x86_die_alloc(cpu
);
878 mp_safe_spin_lock(&x86_topo_lock
);
879 if (x86_die_find(cpu
) != NULL
) {
885 * Add the die to the package.
887 x86_package_add_die(pkg
, die
);
889 } while (die
== NULL
);
892 * Get the core for this logical CPU.
895 core
= x86_core_find(cpu
);
898 * Allocate the core structure now.
900 simple_unlock(&x86_topo_lock
);
901 core
= x86_core_alloc(cpu
);
902 mp_safe_spin_lock(&x86_topo_lock
);
903 if (x86_core_find(cpu
) != NULL
) {
909 * Add the core to the die & package.
911 x86_die_add_core(die
, core
);
912 x86_package_add_core(pkg
, core
);
913 machine_info
.physical_cpu_max
+= 1;
915 } while (core
== NULL
);
919 * Done manipulating the topology, so others can get in.
921 machine_info
.logical_cpu_max
+= 1;
922 simple_unlock(&x86_topo_lock
);
925 * Add the logical CPU to the other topology structures.
927 x86_core_add_lcpu(core
, &cpup
->lcpu
);
928 x86_die_add_lcpu(core
->die
, &cpup
->lcpu
);
929 x86_package_add_lcpu(core
->package
, &cpup
->lcpu
);
930 x86_lcpu_add_caches(&cpup
->lcpu
);
932 return (void *) core
;
936 cpu_thread_init(void)
938 int my_cpu
= get_cpu_number();
939 cpu_data_t
*cpup
= current_cpu_datap();
941 static int initialized
= 0;
944 * If we're the boot processor, we do all of the initialization of
945 * the CPU topology infrastructure.
947 if (my_cpu
== master_cpu
&& !initialized
) {
948 simple_lock_init(&x86_topo_lock
, 0);
951 * Put this logical CPU into the physical CPU topology.
953 cpup
->lcpu
.core
= cpu_thread_alloc(my_cpu
);
959 * Do the CPU accounting.
961 core
= cpup
->lcpu
.core
;
962 mp_safe_spin_lock(&x86_topo_lock
);
963 machine_info
.logical_cpu
+= 1;
964 if (core
->active_lcpus
== 0)
965 machine_info
.physical_cpu
+= 1;
966 core
->active_lcpus
+= 1;
967 simple_unlock(&x86_topo_lock
);
969 pmCPUMarkRunning(cpup
);
970 timer_resync_deadlines();
974 * Called for a cpu to halt permanently
975 * (as opposed to halting and expecting an interrupt to awaken it).
977 __attribute__((noreturn
))
979 cpu_thread_halt(void)
982 cpu_data_t
*cpup
= current_cpu_datap();
984 mp_safe_spin_lock(&x86_topo_lock
);
985 machine_info
.logical_cpu
-= 1;
986 core
= cpup
->lcpu
.core
;
987 core
->active_lcpus
-= 1;
988 if (core
->active_lcpus
== 0)
989 machine_info
.physical_cpu
-= 1;
990 simple_unlock(&x86_topo_lock
);
993 * Let the power management code determine the best way to "stop"
996 ml_set_interrupts_enabled(FALSE
);
998 pmCPUHalt(PM_HALT_NORMAL
);
1004 * Validates that the topology was built correctly. Must be called only
1005 * after the complete topology is built and no other changes are being made.
1008 x86_validate_topology(void)
1019 debug_topology_print();
1024 * Right now this only works if the number of CPUs started is the total
1025 * number of CPUs. However, when specifying cpus=n the topology is only
1026 * partially constructed and the checks below will fail.
1028 * We should *always* build the complete topology and only start the CPUs
1029 * indicated by cpus=n. Until that happens, this code will not check the
1030 * topology if the number of cpus defined is < that described the the
1031 * topology parameters.
1033 nCPUs
= topoParms
.nPackages
* topoParms
.nLThreadsPerPackage
;
1034 if (nCPUs
> real_ncpus
)
1038 while (pkg
!= NULL
) {
1040 * Make sure that the package has the correct number of dies.
1044 while (die
!= NULL
) {
1045 if (die
->package
== NULL
)
1046 panic("Die(%d)->package is NULL",
1048 if (die
->package
!= pkg
)
1049 panic("Die %d points to package %d, should be %d",
1050 die
->pdie_num
, die
->package
->lpkg_num
, pkg
->lpkg_num
);
1052 TOPO_DBG("Die(%d)->package %d\n",
1053 die
->pdie_num
, pkg
->lpkg_num
);
1056 * Make sure that the die has the correct number of cores.
1058 TOPO_DBG("Die(%d)->cores: ", die
->pdie_num
);
1061 while (core
!= NULL
) {
1062 if (core
->die
== NULL
)
1063 panic("Core(%d)->die is NULL",
1065 if (core
->die
!= die
)
1066 panic("Core %d points to die %d, should be %d",
1067 core
->pcore_num
, core
->die
->pdie_num
, die
->pdie_num
);
1069 TOPO_DBG("%d ", core
->pcore_num
);
1070 core
= core
->next_in_die
;
1074 if (nCores
!= topoParms
.nLCoresPerDie
)
1075 panic("Should have %d Cores, but only found %d for Die %d",
1076 topoParms
.nLCoresPerDie
, nCores
, die
->pdie_num
);
1079 * Make sure that the die has the correct number of CPUs.
1081 TOPO_DBG("Die(%d)->lcpus: ", die
->pdie_num
);
1084 while (lcpu
!= NULL
) {
1085 if (lcpu
->die
== NULL
)
1086 panic("CPU(%d)->die is NULL",
1088 if (lcpu
->die
!= die
)
1089 panic("CPU %d points to die %d, should be %d",
1090 lcpu
->cpu_num
, lcpu
->die
->pdie_num
, die
->pdie_num
);
1092 TOPO_DBG("%d ", lcpu
->cpu_num
);
1093 lcpu
= lcpu
->next_in_die
;
1097 if (nCPUs
!= topoParms
.nLThreadsPerDie
)
1098 panic("Should have %d Threads, but only found %d for Die %d",
1099 topoParms
.nLThreadsPerDie
, nCPUs
, die
->pdie_num
);
1102 die
= die
->next_in_pkg
;
1105 if (nDies
!= topoParms
.nLDiesPerPackage
)
1106 panic("Should have %d Dies, but only found %d for package %d",
1107 topoParms
.nLDiesPerPackage
, nDies
, pkg
->lpkg_num
);
1110 * Make sure that the package has the correct number of cores.
1114 while (core
!= NULL
) {
1115 if (core
->package
== NULL
)
1116 panic("Core(%d)->package is NULL",
1118 if (core
->package
!= pkg
)
1119 panic("Core %d points to package %d, should be %d",
1120 core
->pcore_num
, core
->package
->lpkg_num
, pkg
->lpkg_num
);
1121 TOPO_DBG("Core(%d)->package %d\n",
1122 core
->pcore_num
, pkg
->lpkg_num
);
1125 * Make sure that the core has the correct number of CPUs.
1129 TOPO_DBG("Core(%d)->lcpus: ", core
->pcore_num
);
1130 while (lcpu
!= NULL
) {
1131 if (lcpu
->core
== NULL
)
1132 panic("CPU(%d)->core is NULL",
1134 if (lcpu
->core
!= core
)
1135 panic("CPU %d points to core %d, should be %d",
1136 lcpu
->cpu_num
, lcpu
->core
->pcore_num
, core
->pcore_num
);
1137 TOPO_DBG("%d ", lcpu
->cpu_num
);
1139 lcpu
= lcpu
->next_in_core
;
1143 if (nCPUs
!= topoParms
.nLThreadsPerCore
)
1144 panic("Should have %d Threads, but only found %d for Core %d",
1145 topoParms
.nLThreadsPerCore
, nCPUs
, core
->pcore_num
);
1147 core
= core
->next_in_pkg
;
1150 if (nCores
!= topoParms
.nLCoresPerPackage
)
1151 panic("Should have %d Cores, but only found %d for package %d",
1152 topoParms
.nLCoresPerPackage
, nCores
, pkg
->lpkg_num
);
1155 * Make sure that the package has the correct number of CPUs.
1159 while (lcpu
!= NULL
) {
1160 if (lcpu
->package
== NULL
)
1161 panic("CPU(%d)->package is NULL",
1163 if (lcpu
->package
!= pkg
)
1164 panic("CPU %d points to package %d, should be %d",
1165 lcpu
->cpu_num
, lcpu
->package
->lpkg_num
, pkg
->lpkg_num
);
1166 TOPO_DBG("CPU(%d)->package %d\n",
1167 lcpu
->cpu_num
, pkg
->lpkg_num
);
1169 lcpu
= lcpu
->next_in_pkg
;
1172 if (nCPUs
!= topoParms
.nLThreadsPerPackage
)
1173 panic("Should have %d Threads, but only found %d for package %d",
1174 topoParms
.nLThreadsPerPackage
, nCPUs
, pkg
->lpkg_num
);
1181 * Prints out the topology
1184 debug_topology_print(void)
1192 while (pkg
!= NULL
) {
1193 kprintf("Package:\n");
1194 kprintf(" Physical: %d\n", pkg
->ppkg_num
);
1195 kprintf(" Logical: %d\n", pkg
->lpkg_num
);
1198 while (die
!= NULL
) {
1200 kprintf(" Physical: %d\n", die
->pdie_num
);
1201 kprintf(" Logical: %d\n", die
->ldie_num
);
1204 while (core
!= NULL
) {
1205 kprintf(" Core:\n");
1206 kprintf(" Physical: %d\n", core
->pcore_num
);
1207 kprintf(" Logical: %d\n", core
->lcore_num
);
1210 while (cpu
!= NULL
) {
1211 kprintf(" LCPU:\n");
1212 kprintf(" CPU #: %d\n", cpu
->cpu_num
);
1213 kprintf(" Physical: %d\n", cpu
->pnum
);
1214 kprintf(" Logical: %d\n", cpu
->lnum
);
1215 kprintf(" Flags: ");
1220 if (!cpu
->master
&& !cpu
->primary
)
1224 cpu
= cpu
->next_in_core
;
1227 core
= core
->next_in_die
;
1230 die
= die
->next_in_pkg
;