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/lock_group.h>
31 #include <kern/timer_queue.h>
32 #include <mach/machine.h>
33 #include <i386/cpu_threads.h>
34 #include <i386/cpuid.h>
35 #include <i386/machine_cpu.h>
36 #include <i386/pmCPU.h>
37 #include <i386/bit_routines.h>
40 #include <kern/monotonic.h>
41 #endif /* MONOTONIC */
43 #define DIVISOR_GUARD(denom) \
45 kprintf("%s: %d Zero divisor: " #denom, \
46 __FILE__, __LINE__); \
49 static void debug_topology_print(void);
51 boolean_t topo_dbg
= FALSE
;
53 x86_pkg_t
*x86_pkgs
= NULL
;
54 uint32_t num_Lx_caches
[MAX_CACHE_DEPTH
] = { 0 };
56 static x86_pkg_t
*free_pkgs
= NULL
;
57 static x86_die_t
*free_dies
= NULL
;
58 static x86_core_t
*free_cores
= NULL
;
59 static uint32_t num_dies
= 0;
61 static x86_cpu_cache_t
*x86_caches
= NULL
;
62 static uint32_t num_caches
= 0;
64 static boolean_t topoParmsInited
= FALSE
;
65 x86_topology_parameters_t topoParms
;
67 decl_simple_lock_data(, x86_topo_lock
);
69 static struct cpu_cache
{
71 } cpu_caches
[LCACHE_MAX
] = {
72 [L1D
] = { 1, CPU_CACHE_TYPE_DATA
},
73 [L1I
] = { 1, CPU_CACHE_TYPE_INST
},
74 [L2U
] = { 2, CPU_CACHE_TYPE_UNIF
},
75 [L3U
] = { 3, CPU_CACHE_TYPE_UNIF
},
79 cpu_is_hyperthreaded(void)
81 i386_cpu_info_t
*cpuinfo
;
83 cpuinfo
= cpuid_info();
84 return cpuinfo
->thread_count
> cpuinfo
->core_count
;
87 static x86_cpu_cache_t
*
90 x86_cpu_cache_t
*cache
;
93 if (x86_caches
== NULL
) {
94 cache
= kalloc(sizeof(x86_cpu_cache_t
) + (MAX_CPUS
* sizeof(x86_lcpu_t
*)));
100 x86_caches
= cache
->next
;
104 bzero(cache
, sizeof(x86_cpu_cache_t
));
106 cache
->maxcpus
= MAX_CPUS
;
107 for (i
= 0; i
< cache
->maxcpus
; i
+= 1) {
108 cache
->cpus
[i
] = NULL
;
120 uint32_t nCPUsSharing
= 1;
121 i386_cpu_info_t
*cpuinfo
;
122 struct cpu_cache
*cachep
;
125 cpuinfo
= cpuid_info();
127 for (i
= 0, cachep
= &cpu_caches
[0]; i
< LCACHE_MAX
; i
++, cachep
++) {
128 if (cachep
->type
== 0 || cpuid_info()->cache_size
[i
] == 0) {
133 * Only worry about it if it's a deeper level than
134 * what we've seen before.
136 if (cachep
->level
> cache_level
) {
137 cache_level
= cachep
->level
;
140 * Save the number of CPUs sharing this cache.
142 nCPUsSharing
= cpuinfo
->cache_sharing
[i
];
147 * Make the level of the LLC be 0 based.
149 topoParms
.LLCDepth
= cache_level
- 1;
152 * nCPUsSharing represents the *maximum* number of cores or
153 * logical CPUs sharing the cache.
155 topoParms
.maxSharingLLC
= nCPUsSharing
;
157 topoParms
.nCoresSharingLLC
= nCPUsSharing
/ (cpuinfo
->thread_count
/
158 cpuinfo
->core_count
);
159 topoParms
.nLCPUsSharingLLC
= nCPUsSharing
;
162 * nCPUsSharing may not be the number of *active* cores or
163 * threads that are sharing the cache.
165 if (nCPUsSharing
> cpuinfo
->core_count
) {
166 topoParms
.nCoresSharingLLC
= cpuinfo
->core_count
;
168 if (nCPUsSharing
> cpuinfo
->thread_count
) {
169 topoParms
.nLCPUsSharingLLC
= cpuinfo
->thread_count
;
176 i386_cpu_info_t
*cpuinfo
;
178 topoParms
.stable
= FALSE
;
180 cpuinfo
= cpuid_info();
182 PE_parse_boot_argn("-topo", &topo_dbg
, sizeof(topo_dbg
));
185 * We need to start with getting the LLC information correct.
190 * Compute the number of threads (logical CPUs) per core.
192 DIVISOR_GUARD(cpuinfo
->core_count
);
193 topoParms
.nLThreadsPerCore
= cpuinfo
->thread_count
/ cpuinfo
->core_count
;
194 DIVISOR_GUARD(cpuinfo
->cpuid_cores_per_package
);
195 topoParms
.nPThreadsPerCore
= cpuinfo
->cpuid_logical_per_package
/ cpuinfo
->cpuid_cores_per_package
;
198 * Compute the number of dies per package.
200 DIVISOR_GUARD(topoParms
.nCoresSharingLLC
);
201 topoParms
.nLDiesPerPackage
= cpuinfo
->core_count
/ topoParms
.nCoresSharingLLC
;
202 DIVISOR_GUARD(topoParms
.nPThreadsPerCore
);
203 DIVISOR_GUARD(topoParms
.maxSharingLLC
/ topoParms
.nPThreadsPerCore
);
204 topoParms
.nPDiesPerPackage
= cpuinfo
->cpuid_cores_per_package
/ (topoParms
.maxSharingLLC
/ topoParms
.nPThreadsPerCore
);
208 * Compute the number of cores per die.
210 topoParms
.nLCoresPerDie
= topoParms
.nCoresSharingLLC
;
211 topoParms
.nPCoresPerDie
= (topoParms
.maxSharingLLC
/ topoParms
.nPThreadsPerCore
);
214 * Compute the number of threads per die.
216 topoParms
.nLThreadsPerDie
= topoParms
.nLThreadsPerCore
* topoParms
.nLCoresPerDie
;
217 topoParms
.nPThreadsPerDie
= topoParms
.nPThreadsPerCore
* topoParms
.nPCoresPerDie
;
220 * Compute the number of cores per package.
222 topoParms
.nLCoresPerPackage
= topoParms
.nLCoresPerDie
* topoParms
.nLDiesPerPackage
;
223 topoParms
.nPCoresPerPackage
= topoParms
.nPCoresPerDie
* topoParms
.nPDiesPerPackage
;
226 * Compute the number of threads per package.
228 topoParms
.nLThreadsPerPackage
= topoParms
.nLThreadsPerCore
* topoParms
.nLCoresPerPackage
;
229 topoParms
.nPThreadsPerPackage
= topoParms
.nPThreadsPerCore
* topoParms
.nPCoresPerPackage
;
231 TOPO_DBG("\nCache Topology Parameters:\n");
232 TOPO_DBG("\tLLC Depth: %d\n", topoParms
.LLCDepth
);
233 TOPO_DBG("\tCores Sharing LLC: %d\n", topoParms
.nCoresSharingLLC
);
234 TOPO_DBG("\tThreads Sharing LLC: %d\n", topoParms
.nLCPUsSharingLLC
);
235 TOPO_DBG("\tmax Sharing of LLC: %d\n", topoParms
.maxSharingLLC
);
237 TOPO_DBG("\nLogical Topology Parameters:\n");
238 TOPO_DBG("\tThreads per Core: %d\n", topoParms
.nLThreadsPerCore
);
239 TOPO_DBG("\tCores per Die: %d\n", topoParms
.nLCoresPerDie
);
240 TOPO_DBG("\tThreads per Die: %d\n", topoParms
.nLThreadsPerDie
);
241 TOPO_DBG("\tDies per Package: %d\n", topoParms
.nLDiesPerPackage
);
242 TOPO_DBG("\tCores per Package: %d\n", topoParms
.nLCoresPerPackage
);
243 TOPO_DBG("\tThreads per Package: %d\n", topoParms
.nLThreadsPerPackage
);
245 TOPO_DBG("\nPhysical Topology Parameters:\n");
246 TOPO_DBG("\tThreads per Core: %d\n", topoParms
.nPThreadsPerCore
);
247 TOPO_DBG("\tCores per Die: %d\n", topoParms
.nPCoresPerDie
);
248 TOPO_DBG("\tThreads per Die: %d\n", topoParms
.nPThreadsPerDie
);
249 TOPO_DBG("\tDies per Package: %d\n", topoParms
.nPDiesPerPackage
);
250 TOPO_DBG("\tCores per Package: %d\n", topoParms
.nPCoresPerPackage
);
251 TOPO_DBG("\tThreads per Package: %d\n", topoParms
.nPThreadsPerPackage
);
253 topoParmsInited
= TRUE
;
257 x86_cache_free(x86_cpu_cache_t
*cache
)
260 if (cache
->level
> 0 && cache
->level
<= MAX_CACHE_DEPTH
) {
261 num_Lx_caches
[cache
->level
- 1] -= 1;
263 cache
->next
= x86_caches
;
268 * This returns a list of cache structures that represent the
269 * caches for a CPU. Some of the structures may have to be
270 * "freed" if they are actually shared between CPUs.
272 static x86_cpu_cache_t
*
275 x86_cpu_cache_t
*root
= NULL
;
276 x86_cpu_cache_t
*cur
= NULL
;
277 x86_cpu_cache_t
*last
= NULL
;
278 struct cpu_cache
*cachep
;
282 * Cons up a list driven not by CPUID leaf 4 (deterministic cache params)
283 * but by the table above plus parameters already cracked from cpuid...
285 for (i
= 0, cachep
= &cpu_caches
[0]; i
< LCACHE_MAX
; i
++, cachep
++) {
286 if (cachep
->type
== 0 || cpuid_info()->cache_size
[i
] == 0) {
290 cur
= x86_cache_alloc();
295 cur
->type
= cachep
->type
;
296 cur
->level
= cachep
->level
;
298 cur
->maxcpus
= cpuid_info()->cache_sharing
[i
];
299 cur
->partitions
= cpuid_info()->cache_partitions
[i
];
300 cur
->cache_size
= cpuid_info()->cache_size
[i
];
301 cur
->line_size
= cpuid_info()->cache_linesize
;
310 num_Lx_caches
[cur
->level
- 1] += 1;
316 static x86_cpu_cache_t
*
317 x86_match_cache(x86_cpu_cache_t
*list
, x86_cpu_cache_t
*matcher
)
319 x86_cpu_cache_t
*cur_cache
;
322 while (cur_cache
!= NULL
) {
323 if (cur_cache
->maxcpus
== matcher
->maxcpus
324 && cur_cache
->type
== matcher
->type
325 && cur_cache
->level
== matcher
->level
326 && cur_cache
->partitions
== matcher
->partitions
327 && cur_cache
->line_size
== matcher
->line_size
328 && cur_cache
->cache_size
== matcher
->cache_size
) {
332 cur_cache
= cur_cache
->next
;
339 x86_lcpu_init(int cpu
)
345 cpup
= cpu_datap(cpu
);
350 lcpu
->next_in_core
= NULL
;
351 lcpu
->next_in_die
= NULL
;
352 lcpu
->next_in_pkg
= NULL
;
355 lcpu
->package
= NULL
;
358 lcpu
->pnum
= cpup
->cpu_phys_number
;
359 lcpu
->state
= LCPU_OFF
;
360 for (i
= 0; i
< MAX_CACHE_DEPTH
; i
+= 1) {
361 lcpu
->caches
[i
] = NULL
;
366 x86_core_alloc(int cpu
)
371 cpup
= cpu_datap(cpu
);
373 mp_safe_spin_lock(&x86_topo_lock
);
374 if (free_cores
!= NULL
) {
376 free_cores
= core
->next_in_die
;
377 core
->next_in_die
= NULL
;
378 simple_unlock(&x86_topo_lock
);
380 simple_unlock(&x86_topo_lock
);
381 core
= kalloc(sizeof(x86_core_t
));
383 panic("x86_core_alloc() kalloc of x86_core_t failed!\n");
387 bzero((void *) core
, sizeof(x86_core_t
));
389 core
->pcore_num
= cpup
->cpu_phys_number
/ topoParms
.nPThreadsPerCore
;
390 core
->lcore_num
= core
->pcore_num
% topoParms
.nPCoresPerPackage
;
392 core
->flags
= X86CORE_FL_PRESENT
| X86CORE_FL_READY
393 | X86CORE_FL_HALTED
| X86CORE_FL_IDLE
;
399 x86_core_free(x86_core_t
*core
)
401 mp_safe_spin_lock(&x86_topo_lock
);
402 core
->next_in_die
= free_cores
;
404 simple_unlock(&x86_topo_lock
);
408 x86_package_find(int cpu
)
414 cpup
= cpu_datap(cpu
);
416 pkg_num
= cpup
->cpu_phys_number
/ topoParms
.nPThreadsPerPackage
;
419 while (pkg
!= NULL
) {
420 if (pkg
->ppkg_num
== pkg_num
) {
430 x86_die_find(int cpu
)
437 cpup
= cpu_datap(cpu
);
439 die_num
= cpup
->cpu_phys_number
/ topoParms
.nPThreadsPerDie
;
441 pkg
= x86_package_find(cpu
);
447 while (die
!= NULL
) {
448 if (die
->pdie_num
== die_num
) {
451 die
= die
->next_in_pkg
;
458 x86_core_find(int cpu
)
465 cpup
= cpu_datap(cpu
);
467 core_num
= cpup
->cpu_phys_number
/ topoParms
.nPThreadsPerCore
;
469 die
= x86_die_find(cpu
);
475 while (core
!= NULL
) {
476 if (core
->pcore_num
== core_num
) {
479 core
= core
->next_in_die
;
486 x86_set_logical_topology(x86_lcpu_t
*lcpu
, int pnum
, int lnum
)
488 x86_core_t
*core
= lcpu
->core
;
489 x86_die_t
*die
= lcpu
->die
;
490 x86_pkg_t
*pkg
= lcpu
->package
;
492 assert(core
!= NULL
);
496 lcpu
->cpu_num
= lnum
;
498 lcpu
->master
= (lnum
== master_cpu
);
499 lcpu
->primary
= (lnum
% topoParms
.nLThreadsPerPackage
) == 0;
501 lcpu
->lnum
= lnum
% topoParms
.nLThreadsPerCore
;
503 core
->pcore_num
= lnum
/ topoParms
.nLThreadsPerCore
;
504 core
->lcore_num
= core
->pcore_num
% topoParms
.nLCoresPerDie
;
506 die
->pdie_num
= lnum
/ (topoParms
.nLThreadsPerCore
* topoParms
.nLCoresPerDie
);
507 die
->ldie_num
= die
->pdie_num
% topoParms
.nLDiesPerPackage
;
509 pkg
->ppkg_num
= lnum
/ topoParms
.nLThreadsPerPackage
;
510 pkg
->lpkg_num
= pkg
->ppkg_num
;
514 x86_die_alloc(int cpu
)
519 cpup
= cpu_datap(cpu
);
521 mp_safe_spin_lock(&x86_topo_lock
);
522 if (free_dies
!= NULL
) {
524 free_dies
= die
->next_in_pkg
;
525 die
->next_in_pkg
= NULL
;
526 simple_unlock(&x86_topo_lock
);
528 simple_unlock(&x86_topo_lock
);
529 die
= kalloc(sizeof(x86_die_t
));
531 panic("x86_die_alloc() kalloc of x86_die_t failed!\n");
535 bzero((void *) die
, sizeof(x86_die_t
));
537 die
->pdie_num
= cpup
->cpu_phys_number
/ topoParms
.nPThreadsPerDie
;
539 die
->ldie_num
= num_dies
;
540 atomic_incl((long *) &num_dies
, 1);
542 die
->flags
= X86DIE_FL_PRESENT
;
547 x86_die_free(x86_die_t
*die
)
549 mp_safe_spin_lock(&x86_topo_lock
);
550 die
->next_in_pkg
= free_dies
;
552 atomic_decl((long *) &num_dies
, 1);
553 simple_unlock(&x86_topo_lock
);
557 x86_package_alloc(int cpu
)
562 cpup
= cpu_datap(cpu
);
564 mp_safe_spin_lock(&x86_topo_lock
);
565 if (free_pkgs
!= NULL
) {
567 free_pkgs
= pkg
->next
;
569 simple_unlock(&x86_topo_lock
);
571 simple_unlock(&x86_topo_lock
);
572 pkg
= kalloc(sizeof(x86_pkg_t
));
574 panic("x86_package_alloc() kalloc of x86_pkg_t failed!\n");
578 bzero((void *) pkg
, sizeof(x86_pkg_t
));
580 pkg
->ppkg_num
= cpup
->cpu_phys_number
/ topoParms
.nPThreadsPerPackage
;
582 pkg
->lpkg_num
= topoParms
.nPackages
;
583 atomic_incl((long *) &topoParms
.nPackages
, 1);
585 pkg
->flags
= X86PKG_FL_PRESENT
| X86PKG_FL_READY
;
590 x86_package_free(x86_pkg_t
*pkg
)
592 mp_safe_spin_lock(&x86_topo_lock
);
593 pkg
->next
= free_pkgs
;
595 atomic_decl((long *) &topoParms
.nPackages
, 1);
596 simple_unlock(&x86_topo_lock
);
600 x86_cache_add_lcpu(x86_cpu_cache_t
*cache
, x86_lcpu_t
*lcpu
)
602 x86_cpu_cache_t
*cur_cache
;
606 * Put the new CPU into the list of the cache.
608 cur_cache
= lcpu
->caches
[cache
->level
- 1];
609 lcpu
->caches
[cache
->level
- 1] = cache
;
610 cache
->next
= cur_cache
;
612 for (i
= 0; i
< cache
->nlcpus
; i
+= 1) {
613 if (cache
->cpus
[i
] == NULL
) {
614 cache
->cpus
[i
] = lcpu
;
621 x86_lcpu_add_caches(x86_lcpu_t
*lcpu
)
623 x86_cpu_cache_t
*list
;
624 x86_cpu_cache_t
*cur
;
625 x86_cpu_cache_t
*match
;
628 x86_lcpu_t
*cur_lcpu
;
630 boolean_t found
= FALSE
;
632 assert(lcpu
!= NULL
);
635 * Add the cache data to the topology.
637 list
= x86_cache_list();
639 mp_safe_spin_lock(&x86_topo_lock
);
641 while (list
!= NULL
) {
643 * Remove the cache from the front of the list.
648 level
= cur
->level
- 1;
651 * If the cache isn't shared then just put it where it
654 if (cur
->maxcpus
== 1) {
655 x86_cache_add_lcpu(cur
, lcpu
);
660 * We'll assume that all of the caches at a particular level
661 * have the same sharing. So if we have a cache already at
662 * this level, we'll just skip looking for the match.
664 if (lcpu
->caches
[level
] != NULL
) {
670 * This is a shared cache, so we have to figure out if
671 * this is the first time we've seen this cache. We do
672 * this by searching through the topology and seeing if
673 * this cache is already described.
675 * Assume that L{LLC-1} are all at the core level and that
676 * LLC is shared at the die level.
678 if (level
< topoParms
.LLCDepth
) {
680 * Shared at the core.
683 cur_lcpu
= core
->lcpus
;
684 while (cur_lcpu
!= NULL
) {
688 if (cur_lcpu
== lcpu
) {
689 cur_lcpu
= cur_lcpu
->next_in_core
;
694 * If there's a cache on this logical CPU,
697 match
= x86_match_cache(cur_lcpu
->caches
[level
], cur
);
700 x86_cache_add_lcpu(match
, lcpu
);
705 cur_lcpu
= cur_lcpu
->next_in_core
;
712 cur_lcpu
= die
->lcpus
;
713 while (cur_lcpu
!= NULL
) {
717 if (cur_lcpu
== lcpu
) {
718 cur_lcpu
= cur_lcpu
->next_in_die
;
723 * If there's a cache on this logical CPU,
726 match
= x86_match_cache(cur_lcpu
->caches
[level
], cur
);
729 x86_cache_add_lcpu(match
, lcpu
);
734 cur_lcpu
= cur_lcpu
->next_in_die
;
739 * If a shared cache wasn't found, then this logical CPU must
740 * be the first one encountered.
743 x86_cache_add_lcpu(cur
, lcpu
);
747 simple_unlock(&x86_topo_lock
);
751 x86_core_add_lcpu(x86_core_t
*core
, x86_lcpu_t
*lcpu
)
753 assert(core
!= NULL
);
754 assert(lcpu
!= NULL
);
756 mp_safe_spin_lock(&x86_topo_lock
);
758 lcpu
->next_in_core
= core
->lcpus
;
761 core
->num_lcpus
+= 1;
762 simple_unlock(&x86_topo_lock
);
766 x86_die_add_lcpu(x86_die_t
*die
, x86_lcpu_t
*lcpu
)
769 assert(lcpu
!= NULL
);
771 lcpu
->next_in_die
= die
->lcpus
;
777 x86_die_add_core(x86_die_t
*die
, x86_core_t
*core
)
780 assert(core
!= NULL
);
782 core
->next_in_die
= die
->cores
;
789 x86_package_add_lcpu(x86_pkg_t
*pkg
, x86_lcpu_t
*lcpu
)
792 assert(lcpu
!= NULL
);
794 lcpu
->next_in_pkg
= pkg
->lcpus
;
800 x86_package_add_core(x86_pkg_t
*pkg
, x86_core_t
*core
)
803 assert(core
!= NULL
);
805 core
->next_in_pkg
= pkg
->cores
;
811 x86_package_add_die(x86_pkg_t
*pkg
, x86_die_t
*die
)
816 die
->next_in_pkg
= pkg
->dies
;
823 cpu_thread_alloc(int cpu
)
825 x86_core_t
*core
= NULL
;
826 x86_die_t
*die
= NULL
;
827 x86_pkg_t
*pkg
= NULL
;
832 * Only allow one to manipulate the topology at a time.
834 mp_safe_spin_lock(&x86_topo_lock
);
837 * Make sure all of the topology parameters have been initialized.
839 if (!topoParmsInited
) {
843 cpup
= cpu_datap(cpu
);
845 phys_cpu
= cpup
->cpu_phys_number
;
850 * Assume that all cpus have the same features.
852 if (cpu_is_hyperthreaded()) {
853 cpup
->cpu_threadtype
= CPU_THREADTYPE_INTEL_HTT
;
855 cpup
->cpu_threadtype
= CPU_THREADTYPE_NONE
;
859 * Get the package that the logical CPU is in.
862 pkg
= x86_package_find(cpu
);
865 * Package structure hasn't been created yet, do it now.
867 simple_unlock(&x86_topo_lock
);
868 pkg
= x86_package_alloc(cpu
);
869 mp_safe_spin_lock(&x86_topo_lock
);
870 if (x86_package_find(cpu
) != NULL
) {
871 x86_package_free(pkg
);
876 * Add the new package to the global list of packages.
878 pkg
->next
= x86_pkgs
;
881 } while (pkg
== NULL
);
884 * Get the die that the logical CPU is in.
887 die
= x86_die_find(cpu
);
890 * Die structure hasn't been created yet, do it now.
892 simple_unlock(&x86_topo_lock
);
893 die
= x86_die_alloc(cpu
);
894 mp_safe_spin_lock(&x86_topo_lock
);
895 if (x86_die_find(cpu
) != NULL
) {
901 * Add the die to the package.
903 x86_package_add_die(pkg
, die
);
905 } while (die
== NULL
);
908 * Get the core for this logical CPU.
911 core
= x86_core_find(cpu
);
914 * Allocate the core structure now.
916 simple_unlock(&x86_topo_lock
);
917 core
= x86_core_alloc(cpu
);
918 mp_safe_spin_lock(&x86_topo_lock
);
919 if (x86_core_find(cpu
) != NULL
) {
925 * Add the core to the die & package.
927 x86_die_add_core(die
, core
);
928 x86_package_add_core(pkg
, core
);
929 machine_info
.physical_cpu_max
+= 1;
931 } while (core
== NULL
);
935 * Done manipulating the topology, so others can get in.
937 machine_info
.logical_cpu_max
+= 1;
938 simple_unlock(&x86_topo_lock
);
941 * Add the logical CPU to the other topology structures.
943 x86_core_add_lcpu(core
, &cpup
->lcpu
);
944 x86_die_add_lcpu(core
->die
, &cpup
->lcpu
);
945 x86_package_add_lcpu(core
->package
, &cpup
->lcpu
);
946 x86_lcpu_add_caches(&cpup
->lcpu
);
948 return (void *) core
;
952 cpu_thread_init(void)
954 int my_cpu
= get_cpu_number();
955 cpu_data_t
*cpup
= current_cpu_datap();
957 static int initialized
= 0;
960 * If we're the boot processor, we do all of the initialization of
961 * the CPU topology infrastructure.
963 if (my_cpu
== master_cpu
&& !initialized
) {
964 simple_lock_init(&x86_topo_lock
, 0);
967 * Put this logical CPU into the physical CPU topology.
969 cpup
->lcpu
.core
= cpu_thread_alloc(my_cpu
);
975 * Do the CPU accounting.
977 core
= cpup
->lcpu
.core
;
978 mp_safe_spin_lock(&x86_topo_lock
);
979 machine_info
.logical_cpu
+= 1;
980 if (core
->active_lcpus
== 0) {
981 machine_info
.physical_cpu
+= 1;
983 core
->active_lcpus
+= 1;
984 simple_unlock(&x86_topo_lock
);
986 pmCPUMarkRunning(cpup
);
987 timer_resync_deadlines();
991 * Called for a cpu to halt permanently
992 * (as opposed to halting and expecting an interrupt to awaken it).
994 __attribute__((noreturn
))
996 cpu_thread_halt(void)
999 cpu_data_t
*cpup
= current_cpu_datap();
1001 mp_safe_spin_lock(&x86_topo_lock
);
1002 machine_info
.logical_cpu
-= 1;
1003 core
= cpup
->lcpu
.core
;
1004 core
->active_lcpus
-= 1;
1005 if (core
->active_lcpus
== 0) {
1006 machine_info
.physical_cpu
-= 1;
1008 simple_unlock(&x86_topo_lock
);
1011 * Let the power management code determine the best way to "stop"
1014 ml_set_interrupts_enabled(FALSE
);
1016 pmCPUHalt(PM_HALT_NORMAL
);
1022 * Validates that the topology was built correctly. Must be called only
1023 * after the complete topology is built and no other changes are being made.
1026 x86_validate_topology(void)
1037 debug_topology_print();
1041 * Called after processors are registered but before non-boot processors
1043 * - real_ncpus: number of registered processors driven from MADT
1044 * - max_ncpus: max number of processors that will be started
1046 nCPUs
= topoParms
.nPackages
* topoParms
.nLThreadsPerPackage
;
1047 if (nCPUs
!= real_ncpus
) {
1048 panic("x86_validate_topology() %d threads but %d registered from MADT",
1053 while (pkg
!= NULL
) {
1055 * Make sure that the package has the correct number of dies.
1059 while (die
!= NULL
) {
1060 if (die
->package
== NULL
) {
1061 panic("Die(%d)->package is NULL",
1064 if (die
->package
!= pkg
) {
1065 panic("Die %d points to package %d, should be %d",
1066 die
->pdie_num
, die
->package
->lpkg_num
, pkg
->lpkg_num
);
1069 TOPO_DBG("Die(%d)->package %d\n",
1070 die
->pdie_num
, pkg
->lpkg_num
);
1073 * Make sure that the die has the correct number of cores.
1075 TOPO_DBG("Die(%d)->cores: ", die
->pdie_num
);
1078 while (core
!= NULL
) {
1079 if (core
->die
== NULL
) {
1080 panic("Core(%d)->die is NULL",
1083 if (core
->die
!= die
) {
1084 panic("Core %d points to die %d, should be %d",
1085 core
->pcore_num
, core
->die
->pdie_num
, die
->pdie_num
);
1088 TOPO_DBG("%d ", core
->pcore_num
);
1089 core
= core
->next_in_die
;
1093 if (nCores
!= topoParms
.nLCoresPerDie
) {
1094 panic("Should have %d Cores, but only found %d for Die %d",
1095 topoParms
.nLCoresPerDie
, nCores
, die
->pdie_num
);
1099 * Make sure that the die has the correct number of CPUs.
1101 TOPO_DBG("Die(%d)->lcpus: ", die
->pdie_num
);
1104 while (lcpu
!= NULL
) {
1105 if (lcpu
->die
== NULL
) {
1106 panic("CPU(%d)->die is NULL",
1109 if (lcpu
->die
!= die
) {
1110 panic("CPU %d points to die %d, should be %d",
1111 lcpu
->cpu_num
, lcpu
->die
->pdie_num
, die
->pdie_num
);
1114 TOPO_DBG("%d ", lcpu
->cpu_num
);
1115 lcpu
= lcpu
->next_in_die
;
1119 if (nCPUs
!= topoParms
.nLThreadsPerDie
) {
1120 panic("Should have %d Threads, but only found %d for Die %d",
1121 topoParms
.nLThreadsPerDie
, nCPUs
, die
->pdie_num
);
1125 die
= die
->next_in_pkg
;
1128 if (nDies
!= topoParms
.nLDiesPerPackage
) {
1129 panic("Should have %d Dies, but only found %d for package %d",
1130 topoParms
.nLDiesPerPackage
, nDies
, pkg
->lpkg_num
);
1134 * Make sure that the package has the correct number of cores.
1138 while (core
!= NULL
) {
1139 if (core
->package
== NULL
) {
1140 panic("Core(%d)->package is NULL",
1143 if (core
->package
!= pkg
) {
1144 panic("Core %d points to package %d, should be %d",
1145 core
->pcore_num
, core
->package
->lpkg_num
, pkg
->lpkg_num
);
1147 TOPO_DBG("Core(%d)->package %d\n",
1148 core
->pcore_num
, pkg
->lpkg_num
);
1151 * Make sure that the core has the correct number of CPUs.
1155 TOPO_DBG("Core(%d)->lcpus: ", core
->pcore_num
);
1156 while (lcpu
!= NULL
) {
1157 if (lcpu
->core
== NULL
) {
1158 panic("CPU(%d)->core is NULL",
1161 if (lcpu
->core
!= core
) {
1162 panic("CPU %d points to core %d, should be %d",
1163 lcpu
->cpu_num
, lcpu
->core
->pcore_num
, core
->pcore_num
);
1165 TOPO_DBG("%d ", lcpu
->cpu_num
);
1167 lcpu
= lcpu
->next_in_core
;
1171 if (nCPUs
!= topoParms
.nLThreadsPerCore
) {
1172 panic("Should have %d Threads, but only found %d for Core %d",
1173 topoParms
.nLThreadsPerCore
, nCPUs
, core
->pcore_num
);
1176 core
= core
->next_in_pkg
;
1179 if (nCores
!= topoParms
.nLCoresPerPackage
) {
1180 panic("Should have %d Cores, but only found %d for package %d",
1181 topoParms
.nLCoresPerPackage
, nCores
, pkg
->lpkg_num
);
1185 * Make sure that the package has the correct number of CPUs.
1189 while (lcpu
!= NULL
) {
1190 if (lcpu
->package
== NULL
) {
1191 panic("CPU(%d)->package is NULL",
1194 if (lcpu
->package
!= pkg
) {
1195 panic("CPU %d points to package %d, should be %d",
1196 lcpu
->cpu_num
, lcpu
->package
->lpkg_num
, pkg
->lpkg_num
);
1198 TOPO_DBG("CPU(%d)->package %d\n",
1199 lcpu
->cpu_num
, pkg
->lpkg_num
);
1201 lcpu
= lcpu
->next_in_pkg
;
1204 if (nCPUs
!= topoParms
.nLThreadsPerPackage
) {
1205 panic("Should have %d Threads, but only found %d for package %d",
1206 topoParms
.nLThreadsPerPackage
, nCPUs
, pkg
->lpkg_num
);
1214 * Prints out the topology
1217 debug_topology_print(void)
1225 while (pkg
!= NULL
) {
1226 kprintf("Package:\n");
1227 kprintf(" Physical: %d\n", pkg
->ppkg_num
);
1228 kprintf(" Logical: %d\n", pkg
->lpkg_num
);
1231 while (die
!= NULL
) {
1233 kprintf(" Physical: %d\n", die
->pdie_num
);
1234 kprintf(" Logical: %d\n", die
->ldie_num
);
1237 while (core
!= NULL
) {
1238 kprintf(" Core:\n");
1239 kprintf(" Physical: %d\n", core
->pcore_num
);
1240 kprintf(" Logical: %d\n", core
->lcore_num
);
1243 while (cpu
!= NULL
) {
1244 kprintf(" LCPU:\n");
1245 kprintf(" CPU #: %d\n", cpu
->cpu_num
);
1246 kprintf(" Physical: %d\n", cpu
->pnum
);
1247 kprintf(" Logical: %d\n", cpu
->lnum
);
1248 kprintf(" Flags: ");
1255 if (!cpu
->master
&& !cpu
->primary
) {
1260 cpu
= cpu
->next_in_core
;
1263 core
= core
->next_in_die
;
1266 die
= die
->next_in_pkg
;