2 * Copyright (c) 2003-2008 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 <mach/machine.h>
31 #include <i386/cpu_threads.h>
32 #include <i386/cpuid.h>
33 #include <i386/machine_cpu.h>
34 #include <i386/lock.h>
35 #include <i386/perfmon.h>
36 #include <i386/pmCPU.h>
38 //#define TOPO_DEBUG 1
40 void debug_topology_print(void);
41 #define DBG(x...) kprintf("DBG: " x)
44 #endif /* TOPO_DEBUG */
46 void validate_topology(void);
48 #define bitmask(h,l) ((bit(h)|(bit(h)-1)) & ~(bit(l)-1))
49 #define bitfield(x,h,l) (((x) & bitmask(h,l)) >> l)
51 x86_pkg_t
*x86_pkgs
= NULL
;
52 uint32_t num_Lx_caches
[MAX_CACHE_DEPTH
] = { 0 };
54 static x86_pkg_t
*free_pkgs
= NULL
;
55 static x86_die_t
*free_dies
= NULL
;
56 static x86_core_t
*free_cores
= NULL
;
57 static uint32_t num_dies
= 0;
59 static x86_cpu_cache_t
*x86_caches
= NULL
;
60 static uint32_t num_caches
= 0;
62 static boolean_t topoParmsInited
= FALSE
;
63 x86_topology_parameters_t topoParms
;
65 decl_simple_lock_data(, x86_topo_lock
);
68 cpu_is_hyperthreaded(void)
70 i386_cpu_info_t
*cpuinfo
;
72 cpuinfo
= cpuid_info();
73 return(cpuinfo
->thread_count
> cpuinfo
->core_count
);
76 static x86_cpu_cache_t
*
79 x86_cpu_cache_t
*cache
;
82 if (x86_caches
== NULL
) {
83 cache
= kalloc(sizeof(x86_cpu_cache_t
) + (MAX_CPUS
* sizeof(x86_lcpu_t
*)));
88 x86_caches
= cache
->next
;
92 bzero(cache
, sizeof(x86_cpu_cache_t
));
94 cache
->maxcpus
= MAX_CPUS
;
95 for (i
= 0; i
< cache
->maxcpus
; i
+= 1) {
96 cache
->cpus
[i
] = NULL
;
108 uint32_t cache_info
[4];
109 uint32_t cache_level
= 0;
110 uint32_t nCPUsSharing
= 1;
111 i386_cpu_info_t
*cpuinfo
;
113 cpuinfo
= cpuid_info();
115 do_cpuid(0, cache_info
);
117 if (cache_info
[eax
] < 4) {
119 * Processor does not support deterministic
120 * cache information. Set LLC sharing to 1, since
121 * we have no better information.
123 if (cpu_is_hyperthreaded()) {
124 topoParms
.nCoresSharingLLC
= 1;
125 topoParms
.nLCPUsSharingLLC
= 2;
126 topoParms
.maxSharingLLC
= 2;
128 topoParms
.nCoresSharingLLC
= 1;
129 topoParms
.nLCPUsSharingLLC
= 1;
130 topoParms
.maxSharingLLC
= 1;
135 for (index
= 0; ; index
+= 1) {
139 cache_info
[ecx
] = index
;
146 * See if all levels have been queried.
148 if (bitfield(cache_info
[eax
], 4, 0) == 0)
152 * Get the current level.
154 this_level
= bitfield(cache_info
[eax
], 7, 5);
157 * Only worry about it if it's a deeper level than
158 * what we've seen before.
160 if (this_level
> cache_level
) {
161 cache_level
= this_level
;
164 * Save the number of CPUs sharing this cache.
166 nCPUsSharing
= bitfield(cache_info
[eax
], 25, 14) + 1;
171 * Make the level of the LLC be 0 based.
173 topoParms
.LLCDepth
= cache_level
- 1;
176 * nCPUsSharing represents the *maximum* number of cores or
177 * logical CPUs sharing the cache.
179 topoParms
.maxSharingLLC
= nCPUsSharing
;
181 topoParms
.nCoresSharingLLC
= nCPUsSharing
;
182 topoParms
.nLCPUsSharingLLC
= nCPUsSharing
;
185 * nCPUsSharing may not be the number of *active* cores or
186 * threads that are sharing the cache.
188 if (nCPUsSharing
> cpuinfo
->core_count
)
189 topoParms
.nCoresSharingLLC
= cpuinfo
->core_count
;
190 if (nCPUsSharing
> cpuinfo
->thread_count
)
191 topoParms
.nLCPUsSharingLLC
= cpuinfo
->thread_count
;
197 i386_cpu_info_t
*cpuinfo
;
199 cpuinfo
= cpuid_info();
202 * We need to start with getting the LLC information correct.
207 * Compute the number of threads (logical CPUs) per core.
209 topoParms
.nLThreadsPerCore
= cpuinfo
->thread_count
/ cpuinfo
->core_count
;
210 topoParms
.nPThreadsPerCore
= cpuinfo
->cpuid_logical_per_package
/ cpuinfo
->cpuid_cores_per_package
;
213 * Compute the number of dies per package.
215 topoParms
.nLDiesPerPackage
= cpuinfo
->core_count
/ topoParms
.nCoresSharingLLC
;
216 topoParms
.nPDiesPerPackage
= cpuinfo
->cpuid_cores_per_package
/ (topoParms
.maxSharingLLC
/ topoParms
.nPThreadsPerCore
);
219 * Compute the number of cores per die.
221 topoParms
.nLCoresPerDie
= topoParms
.nCoresSharingLLC
;
222 topoParms
.nPCoresPerDie
= (topoParms
.maxSharingLLC
/ topoParms
.nPThreadsPerCore
);
225 * Compute the number of threads per die.
227 topoParms
.nLThreadsPerDie
= topoParms
.nLThreadsPerCore
* topoParms
.nLCoresPerDie
;
228 topoParms
.nPThreadsPerDie
= topoParms
.nPThreadsPerCore
* topoParms
.nPCoresPerDie
;
231 * Compute the number of cores per package.
233 topoParms
.nLCoresPerPackage
= topoParms
.nLCoresPerDie
* topoParms
.nLDiesPerPackage
;
234 topoParms
.nPCoresPerPackage
= topoParms
.nPCoresPerDie
* topoParms
.nPDiesPerPackage
;
237 * Compute the number of threads per package.
239 topoParms
.nLThreadsPerPackage
= topoParms
.nLThreadsPerCore
* topoParms
.nLCoresPerPackage
;
240 topoParms
.nPThreadsPerPackage
= topoParms
.nPThreadsPerCore
* topoParms
.nPCoresPerPackage
;
242 DBG("\nLogical Topology Parameters:\n");
243 DBG("\tThreads per Core: %d\n", topoParms
.nLThreadsPerCore
);
244 DBG("\tCores per Die: %d\n", topoParms
.nLCoresPerDie
);
245 DBG("\tThreads per Die: %d\n", topoParms
.nLThreadsPerDie
);
246 DBG("\tDies per Package: %d\n", topoParms
.nLDiesPerPackage
);
247 DBG("\tCores per Package: %d\n", topoParms
.nLCoresPerPackage
);
248 DBG("\tThreads per Package: %d\n", topoParms
.nLThreadsPerPackage
);
250 DBG("\nPhysical Topology Parameters:\n");
251 DBG("\tThreads per Core: %d\n", topoParms
.nPThreadsPerCore
);
252 DBG("\tCores per Die: %d\n", topoParms
.nPCoresPerDie
);
253 DBG("\tThreads per Die: %d\n", topoParms
.nPThreadsPerDie
);
254 DBG("\tDies per Package: %d\n", topoParms
.nPDiesPerPackage
);
255 DBG("\tCores per Package: %d\n", topoParms
.nPCoresPerPackage
);
256 DBG("\tThreads per Package: %d\n", topoParms
.nPThreadsPerPackage
);
258 topoParmsInited
= TRUE
;
262 x86_cache_free(x86_cpu_cache_t
*cache
)
265 if (cache
->level
> 0 && cache
->level
<= MAX_CACHE_DEPTH
)
266 num_Lx_caches
[cache
->level
- 1] -= 1;
267 cache
->next
= x86_caches
;
272 * This returns a list of cache structures that represent the
273 * caches for a CPU. Some of the structures may have to be
274 * "freed" if they are actually shared between CPUs.
276 static x86_cpu_cache_t
*
279 x86_cpu_cache_t
*root
= NULL
;
280 x86_cpu_cache_t
*cur
= NULL
;
281 x86_cpu_cache_t
*last
= NULL
;
283 uint32_t cache_info
[4];
286 do_cpuid(0, cache_info
);
288 if (cache_info
[eax
] < 4) {
290 * Processor does not support deterministic
291 * cache information. Don't report anything
296 for (index
= 0; ; index
+= 1) {
298 cache_info
[ecx
] = index
;
305 * See if all levels have been queried.
307 if (bitfield(cache_info
[eax
], 4, 0) == 0)
310 cur
= x86_cache_alloc();
315 cur
->type
= bitfield(cache_info
[eax
], 4, 0);
316 cur
->level
= bitfield(cache_info
[eax
], 7, 5);
317 cur
->maxcpus
= (bitfield(cache_info
[eax
], 25, 14) + 1);
318 cur
->line_size
= bitfield(cache_info
[ebx
], 11, 0) + 1;
319 cur
->partitions
= bitfield(cache_info
[ebx
], 21, 12) + 1;
320 cur
->ways
= bitfield(cache_info
[ebx
], 31, 22) + 1;
321 nsets
= bitfield(cache_info
[ecx
], 31, 0) + 1;
322 cur
->cache_size
= cur
->line_size
* cur
->ways
* cur
->partitions
* nsets
;
333 num_Lx_caches
[cur
->level
- 1] += 1;
339 static x86_cpu_cache_t
*
340 x86_match_cache(x86_cpu_cache_t
*list
, x86_cpu_cache_t
*matcher
)
342 x86_cpu_cache_t
*cur_cache
;
345 while (cur_cache
!= NULL
) {
346 if (cur_cache
->maxcpus
== matcher
->maxcpus
347 && cur_cache
->type
== matcher
->type
348 && cur_cache
->level
== matcher
->level
349 && cur_cache
->ways
== matcher
->ways
350 && cur_cache
->partitions
== matcher
->partitions
351 && cur_cache
->line_size
== matcher
->line_size
352 && cur_cache
->cache_size
== matcher
->cache_size
)
355 cur_cache
= cur_cache
->next
;
362 x86_lcpu_init(int cpu
)
368 cpup
= cpu_datap(cpu
);
373 lcpu
->next_in_core
= NULL
;
374 lcpu
->next_in_die
= NULL
;
375 lcpu
->next_in_pkg
= NULL
;
378 lcpu
->package
= NULL
;
381 lcpu
->pnum
= cpup
->cpu_phys_number
;
382 lcpu
->state
= LCPU_OFF
;
383 for (i
= 0; i
< MAX_CACHE_DEPTH
; i
+= 1)
384 lcpu
->caches
[i
] = NULL
;
386 lcpu
->master
= (lcpu
->cpu_num
== (unsigned int) master_cpu
);
387 lcpu
->primary
= (lcpu
->pnum
% topoParms
.nPThreadsPerPackage
) == 0;
391 x86_core_alloc(int cpu
)
396 cpup
= cpu_datap(cpu
);
398 simple_lock(&x86_topo_lock
);
399 if (free_cores
!= NULL
) {
401 free_cores
= core
->next_in_die
;
402 core
->next_in_die
= NULL
;
403 simple_unlock(&x86_topo_lock
);
405 simple_unlock(&x86_topo_lock
);
406 core
= kalloc(sizeof(x86_core_t
));
408 panic("x86_core_alloc() kalloc of x86_core_t failed!\n");
411 bzero((void *) core
, sizeof(x86_core_t
));
413 core
->pcore_num
= cpup
->cpu_phys_number
/ topoParms
.nPThreadsPerCore
;
414 core
->lcore_num
= core
->pcore_num
% topoParms
.nPCoresPerPackage
;
416 core
->flags
= X86CORE_FL_PRESENT
| X86CORE_FL_READY
417 | X86CORE_FL_HALTED
| X86CORE_FL_IDLE
;
423 x86_core_free(x86_core_t
*core
)
425 simple_lock(&x86_topo_lock
);
426 core
->next_in_die
= free_cores
;
428 simple_unlock(&x86_topo_lock
);
432 x86_package_find(int cpu
)
438 cpup
= cpu_datap(cpu
);
440 pkg_num
= cpup
->cpu_phys_number
/ topoParms
.nPThreadsPerPackage
;
443 while (pkg
!= NULL
) {
444 if (pkg
->ppkg_num
== pkg_num
)
453 x86_die_find(int cpu
)
460 cpup
= cpu_datap(cpu
);
462 die_num
= cpup
->cpu_phys_number
/ topoParms
.nPThreadsPerDie
;
464 pkg
= x86_package_find(cpu
);
469 while (die
!= NULL
) {
470 if (die
->pdie_num
== die_num
)
472 die
= die
->next_in_pkg
;
479 x86_core_find(int cpu
)
486 cpup
= cpu_datap(cpu
);
488 core_num
= cpup
->cpu_phys_number
/ topoParms
.nPThreadsPerCore
;
490 die
= x86_die_find(cpu
);
495 while (core
!= NULL
) {
496 if (core
->pcore_num
== core_num
)
498 core
= core
->next_in_die
;
505 x86_set_lcpu_numbers(x86_lcpu_t
*lcpu
)
507 lcpu
->lnum
= lcpu
->cpu_num
% topoParms
.nLThreadsPerCore
;
511 x86_set_core_numbers(x86_core_t
*core
, x86_lcpu_t
*lcpu
)
513 core
->pcore_num
= lcpu
->cpu_num
/ topoParms
.nLThreadsPerCore
;
514 core
->lcore_num
= core
->pcore_num
% topoParms
.nLCoresPerDie
;
518 x86_set_die_numbers(x86_die_t
*die
, x86_lcpu_t
*lcpu
)
520 die
->pdie_num
= lcpu
->cpu_num
/ (topoParms
.nLThreadsPerCore
* topoParms
.nLCoresPerDie
);
521 die
->ldie_num
= die
->pdie_num
% topoParms
.nLDiesPerPackage
;
525 x86_set_pkg_numbers(x86_pkg_t
*pkg
, x86_lcpu_t
*lcpu
)
527 pkg
->ppkg_num
= lcpu
->cpu_num
/ topoParms
.nLThreadsPerPackage
;
528 pkg
->lpkg_num
= pkg
->ppkg_num
;
532 x86_die_alloc(int cpu
)
537 cpup
= cpu_datap(cpu
);
539 simple_lock(&x86_topo_lock
);
540 if (free_dies
!= NULL
) {
542 free_dies
= die
->next_in_pkg
;
543 die
->next_in_pkg
= NULL
;
544 simple_unlock(&x86_topo_lock
);
546 simple_unlock(&x86_topo_lock
);
547 die
= kalloc(sizeof(x86_die_t
));
549 panic("x86_die_alloc() kalloc of x86_die_t failed!\n");
552 bzero((void *) die
, sizeof(x86_die_t
));
554 die
->pdie_num
= cpup
->cpu_phys_number
/ topoParms
.nPThreadsPerDie
;
556 die
->ldie_num
= num_dies
;
557 atomic_incl((long *) &num_dies
, 1);
559 die
->flags
= X86DIE_FL_PRESENT
;
564 x86_die_free(x86_die_t
*die
)
566 simple_lock(&x86_topo_lock
);
567 die
->next_in_pkg
= free_dies
;
569 atomic_decl((long *) &num_dies
, 1);
570 simple_unlock(&x86_topo_lock
);
574 x86_package_alloc(int cpu
)
579 cpup
= cpu_datap(cpu
);
581 simple_lock(&x86_topo_lock
);
582 if (free_pkgs
!= NULL
) {
584 free_pkgs
= pkg
->next
;
586 simple_unlock(&x86_topo_lock
);
588 simple_unlock(&x86_topo_lock
);
589 pkg
= kalloc(sizeof(x86_pkg_t
));
591 panic("x86_package_alloc() kalloc of x86_pkg_t failed!\n");
594 bzero((void *) pkg
, sizeof(x86_pkg_t
));
596 pkg
->ppkg_num
= cpup
->cpu_phys_number
/ topoParms
.nPThreadsPerPackage
;
598 pkg
->lpkg_num
= topoParms
.nPackages
;
599 atomic_incl((long *) &topoParms
.nPackages
, 1);
601 pkg
->flags
= X86PKG_FL_PRESENT
| X86PKG_FL_READY
;
606 x86_package_free(x86_pkg_t
*pkg
)
608 simple_lock(&x86_topo_lock
);
609 pkg
->next
= free_pkgs
;
611 atomic_decl((long *) &topoParms
.nPackages
, 1);
612 simple_unlock(&x86_topo_lock
);
616 x86_cache_add_lcpu(x86_cpu_cache_t
*cache
, x86_lcpu_t
*lcpu
)
618 x86_cpu_cache_t
*cur_cache
;
622 * Put the new CPU into the list of the cache.
624 cur_cache
= lcpu
->caches
[cache
->level
- 1];
625 lcpu
->caches
[cache
->level
- 1] = cache
;
626 cache
->next
= cur_cache
;
628 for (i
= 0; i
< cache
->nlcpus
; i
+= 1) {
629 if (cache
->cpus
[i
] == NULL
) {
630 cache
->cpus
[i
] = lcpu
;
637 x86_lcpu_add_caches(x86_lcpu_t
*lcpu
)
639 x86_cpu_cache_t
*list
;
640 x86_cpu_cache_t
*cur
;
641 x86_cpu_cache_t
*match
;
644 x86_lcpu_t
*cur_lcpu
;
646 boolean_t found
= FALSE
;
648 assert(lcpu
!= NULL
);
651 * Add the cache data to the topology.
653 list
= x86_cache_list();
655 simple_lock(&x86_topo_lock
);
657 while (list
!= NULL
) {
659 * Remove the cache from the front of the list.
664 level
= cur
->level
- 1;
667 * If the cache isn't shared then just put it where it
670 if (cur
->maxcpus
== 1) {
671 x86_cache_add_lcpu(cur
, lcpu
);
676 * We'll assume that all of the caches at a particular level
677 * have the same sharing. So if we have a cache already at
678 * this level, we'll just skip looking for the match.
680 if (lcpu
->caches
[level
] != NULL
) {
686 * This is a shared cache, so we have to figure out if
687 * this is the first time we've seen this cache. We do
688 * this by searching through the topology and seeing if
689 * this cache is already described.
691 * Assume that L{LLC-1} are all at the core level and that
692 * LLC is shared at the die level.
694 if (level
< topoParms
.LLCDepth
) {
696 * Shared at the core.
699 cur_lcpu
= core
->lcpus
;
700 while (cur_lcpu
!= NULL
) {
704 if (cur_lcpu
== lcpu
) {
705 cur_lcpu
= cur_lcpu
->next_in_core
;
710 * If there's a cache on this logical CPU,
713 match
= x86_match_cache(cur_lcpu
->caches
[level
], cur
);
716 x86_cache_add_lcpu(match
, lcpu
);
721 cur_lcpu
= cur_lcpu
->next_in_core
;
728 cur_lcpu
= die
->lcpus
;
729 while (cur_lcpu
!= NULL
) {
733 if (cur_lcpu
== lcpu
) {
734 cur_lcpu
= cur_lcpu
->next_in_die
;
739 * If there's a cache on this logical CPU,
742 match
= x86_match_cache(cur_lcpu
->caches
[level
], cur
);
745 x86_cache_add_lcpu(match
, lcpu
);
750 cur_lcpu
= cur_lcpu
->next_in_die
;
755 * If a shared cache wasn't found, then this logical CPU must
756 * be the first one encountered.
759 x86_cache_add_lcpu(cur
, lcpu
);
763 simple_unlock(&x86_topo_lock
);
767 x86_core_add_lcpu(x86_core_t
*core
, x86_lcpu_t
*lcpu
)
769 assert(core
!= NULL
);
770 assert(lcpu
!= NULL
);
772 simple_lock(&x86_topo_lock
);
774 lcpu
->next_in_core
= core
->lcpus
;
777 core
->num_lcpus
+= 1;
778 simple_unlock(&x86_topo_lock
);
782 x86_die_add_lcpu(x86_die_t
*die
, x86_lcpu_t
*lcpu
)
785 assert(lcpu
!= NULL
);
787 lcpu
->next_in_die
= die
->lcpus
;
793 x86_die_add_core(x86_die_t
*die
, x86_core_t
*core
)
796 assert(core
!= NULL
);
798 core
->next_in_die
= die
->cores
;
805 x86_package_add_lcpu(x86_pkg_t
*pkg
, x86_lcpu_t
*lcpu
)
808 assert(lcpu
!= NULL
);
810 lcpu
->next_in_pkg
= pkg
->lcpus
;
816 x86_package_add_core(x86_pkg_t
*pkg
, x86_core_t
*core
)
819 assert(core
!= NULL
);
821 core
->next_in_pkg
= pkg
->cores
;
827 x86_package_add_die(x86_pkg_t
*pkg
, x86_die_t
*die
)
832 die
->next_in_pkg
= pkg
->dies
;
839 cpu_thread_alloc(int cpu
)
841 x86_core_t
*core
= NULL
;
842 x86_die_t
*die
= NULL
;
843 x86_pkg_t
*pkg
= NULL
;
848 * Only allow one to manipulate the topology at a time.
850 simple_lock(&x86_topo_lock
);
853 * Make sure all of the topology parameters have been initialized.
855 if (!topoParmsInited
)
858 cpup
= cpu_datap(cpu
);
860 phys_cpu
= cpup
->cpu_phys_number
;
865 * Allocate performance counter structure.
867 simple_unlock(&x86_topo_lock
);
868 cpup
->lcpu
.pmc
= pmc_alloc();
869 simple_lock(&x86_topo_lock
);
872 * Assume that all cpus have the same features.
874 if (cpu_is_hyperthreaded()) {
875 cpup
->cpu_threadtype
= CPU_THREADTYPE_INTEL_HTT
;
877 cpup
->cpu_threadtype
= CPU_THREADTYPE_NONE
;
881 * Get the package that the logical CPU is in.
884 pkg
= x86_package_find(cpu
);
887 * Package structure hasn't been created yet, do it now.
889 simple_unlock(&x86_topo_lock
);
890 pkg
= x86_package_alloc(cpu
);
891 simple_lock(&x86_topo_lock
);
892 if (x86_package_find(cpu
) != NULL
) {
893 x86_package_free(pkg
);
898 * Add the new package to the global list of packages.
900 pkg
->next
= x86_pkgs
;
903 } while (pkg
== NULL
);
906 * Get the die that the logical CPU is in.
909 die
= x86_die_find(cpu
);
912 * Die structure hasn't been created yet, do it now.
914 simple_unlock(&x86_topo_lock
);
915 die
= x86_die_alloc(cpu
);
916 simple_lock(&x86_topo_lock
);
917 if (x86_die_find(cpu
) != NULL
) {
923 * Add the die to the package.
925 x86_package_add_die(pkg
, die
);
927 } while (die
== NULL
);
930 * Get the core for this logical CPU.
933 core
= x86_core_find(cpu
);
936 * Allocate the core structure now.
938 simple_unlock(&x86_topo_lock
);
939 core
= x86_core_alloc(cpu
);
940 simple_lock(&x86_topo_lock
);
941 if (x86_core_find(cpu
) != NULL
) {
947 * Add the core to the die & package.
949 x86_die_add_core(die
, core
);
950 x86_package_add_core(pkg
, core
);
951 machine_info
.physical_cpu_max
+= 1;
953 } while (core
== NULL
);
957 * Done manipulating the topology, so others can get in.
959 machine_info
.logical_cpu_max
+= 1;
960 simple_unlock(&x86_topo_lock
);
963 * Add the logical CPU to the other topology structures.
965 x86_core_add_lcpu(core
, &cpup
->lcpu
);
966 x86_die_add_lcpu(core
->die
, &cpup
->lcpu
);
967 x86_package_add_lcpu(core
->package
, &cpup
->lcpu
);
968 x86_lcpu_add_caches(&cpup
->lcpu
);
970 return (void *) core
;
974 cpu_thread_init(void)
976 int my_cpu
= get_cpu_number();
977 cpu_data_t
*cpup
= current_cpu_datap();
979 static int initialized
= 0;
982 * If we're the boot processor, we do all of the initialization of
983 * the CPU topology infrastructure.
985 if (my_cpu
== master_cpu
&& !initialized
) {
986 simple_lock_init(&x86_topo_lock
, 0);
989 * Put this logical CPU into the physical CPU topology.
991 cpup
->lcpu
.core
= cpu_thread_alloc(my_cpu
);
997 * Do the CPU accounting.
999 core
= cpup
->lcpu
.core
;
1000 simple_lock(&x86_topo_lock
);
1001 machine_info
.logical_cpu
+= 1;
1002 if (core
->active_lcpus
== 0)
1003 machine_info
.physical_cpu
+= 1;
1004 core
->active_lcpus
+= 1;
1005 simple_unlock(&x86_topo_lock
);
1007 pmCPUMarkRunning(cpup
);
1008 etimer_resync_deadlines();
1012 * Called for a cpu to halt permanently
1013 * (as opposed to halting and expecting an interrupt to awaken it).
1016 cpu_thread_halt(void)
1019 cpu_data_t
*cpup
= current_cpu_datap();
1021 simple_lock(&x86_topo_lock
);
1022 machine_info
.logical_cpu
-= 1;
1023 core
= cpup
->lcpu
.core
;
1024 core
->active_lcpus
-= 1;
1025 if (core
->active_lcpus
== 0)
1026 machine_info
.physical_cpu
-= 1;
1027 simple_unlock(&x86_topo_lock
);
1030 * Let the power management code determine the best way to "stop"
1033 ml_set_interrupts_enabled(FALSE
);
1035 pmCPUHalt(PM_HALT_NORMAL
);
1041 * Validates that the topology was built correctly. Must be called only
1042 * after the complete topology is built and no other changes are being made.
1045 validate_topology(void)
1058 * Right now this only works if the number of CPUs started is the total
1059 * number of CPUs. However, when specifying cpus=n the topology is only
1060 * partially constructed and the checks below will fail.
1062 * We should *always* build the complete topology and only start the CPUs
1063 * indicated by cpus=n. Until that happens, this code will not check the
1064 * topology if the number of cpus defined is < that described the the
1065 * topology parameters.
1067 nCPUs
= topoParms
.nPackages
* topoParms
.nLThreadsPerPackage
;
1068 if (nCPUs
> real_ncpus
)
1072 while (pkg
!= NULL
) {
1074 * Make sure that the package has the correct number of dies.
1078 while (die
!= NULL
) {
1079 if (die
->package
== NULL
)
1080 panic("Die(%d)->package is NULL",
1082 if (die
->package
!= pkg
)
1083 panic("Die %d points to package %d, should be %d",
1084 die
->pdie_num
, die
->package
->lpkg_num
, pkg
->lpkg_num
);
1086 DBG("Die(%d)->package %d\n",
1087 die
->pdie_num
, pkg
->lpkg_num
);
1090 * Make sure that the die has the correct number of cores.
1092 DBG("Die(%d)->cores: ");
1095 while (core
!= NULL
) {
1096 if (core
->die
== NULL
)
1097 panic("Core(%d)->die is NULL",
1099 if (core
->die
!= die
)
1100 panic("Core %d points to die %d, should be %d",
1101 core
->pcore_num
, core
->die
->pdie_num
, die
->pdie_num
);
1103 DBG("%d ", core
->pcore_num
);
1104 core
= core
->next_in_die
;
1108 if (nCores
!= topoParms
.nLCoresPerDie
)
1109 panic("Should have %d Cores, but only found %d for Die %d",
1110 topoParms
.nLCoresPerDie
, nCores
, die
->pdie_num
);
1113 * Make sure that the die has the correct number of CPUs.
1115 DBG("Die(%d)->lcpus: ", die
->pdie_num
);
1118 while (lcpu
!= NULL
) {
1119 if (lcpu
->die
== NULL
)
1120 panic("CPU(%d)->die is NULL",
1122 if (lcpu
->die
!= die
)
1123 panic("CPU %d points to die %d, should be %d",
1124 lcpu
->cpu_num
, lcpu
->die
->pdie_num
, die
->pdie_num
);
1126 DBG("%d ", lcpu
->cpu_num
);
1127 lcpu
= lcpu
->next_in_die
;
1131 if (nCPUs
!= topoParms
.nLThreadsPerDie
)
1132 panic("Should have %d Threads, but only found %d for Die %d",
1133 topoParms
.nLThreadsPerDie
, nCPUs
, die
->pdie_num
);
1136 die
= die
->next_in_pkg
;
1139 if (nDies
!= topoParms
.nLDiesPerPackage
)
1140 panic("Should have %d Dies, but only found %d for package %d",
1141 topoParms
.nLDiesPerPackage
, nDies
, pkg
->lpkg_num
);
1144 * Make sure that the package has the correct number of cores.
1148 while (core
!= NULL
) {
1149 if (core
->package
== NULL
)
1150 panic("Core(%d)->package is NULL",
1152 if (core
->package
!= pkg
)
1153 panic("Core %d points to package %d, should be %d",
1154 core
->pcore_num
, core
->package
->lpkg_num
, pkg
->lpkg_num
);
1155 DBG("Core(%d)->package %d\n",
1156 core
->pcore_num
, pkg
->lpkg_num
);
1159 * Make sure that the core has the correct number of CPUs.
1163 DBG("Core(%d)->lcpus: ");
1164 while (lcpu
!= NULL
) {
1165 if (lcpu
->core
== NULL
)
1166 panic("CPU(%d)->core is NULL",
1168 if (lcpu
->core
!= core
)
1169 panic("CPU %d points to core %d, should be %d",
1170 lcpu
->cpu_num
, lcpu
->core
->pcore_num
, core
->pcore_num
);
1171 DBG("%d ", lcpu
->cpu_num
);
1173 lcpu
= lcpu
->next_in_core
;
1177 if (nCPUs
!= topoParms
.nLThreadsPerCore
)
1178 panic("Should have %d Threads, but only found %d for Core %d",
1179 topoParms
.nLThreadsPerCore
, nCPUs
, core
->pcore_num
);
1181 core
= core
->next_in_pkg
;
1184 if (nCores
!= topoParms
.nLCoresPerPackage
)
1185 panic("Should have %d Cores, but only found %d for package %d",
1186 topoParms
.nLCoresPerPackage
, nCores
, pkg
->lpkg_num
);
1189 * Make sure that the package has the correct number of CPUs.
1193 while (lcpu
!= NULL
) {
1194 if (lcpu
->package
== NULL
)
1195 panic("CPU(%d)->package is NULL",
1197 if (lcpu
->package
!= pkg
)
1198 panic("CPU %d points to package %d, should be %d",
1199 lcpu
->cpu_num
, lcpu
->package
->lpkg_num
, pkg
->lpkg_num
);
1200 DBG("CPU(%d)->package %d\n",
1201 lcpu
->cpu_num
, pkg
->lpkg_num
);
1203 lcpu
= lcpu
->next_in_pkg
;
1206 if (nCPUs
!= topoParms
.nLThreadsPerPackage
)
1207 panic("Should have %d Threads, but only found %d for package %d",
1208 topoParms
.nLThreadsPerPackage
, nCPUs
, pkg
->lpkg_num
);
1216 * Prints out the topology
1219 debug_topology_print(void)
1227 while (pkg
!= NULL
) {
1228 kprintf("Package:\n");
1229 kprintf(" Physical: %d\n", pkg
->ppkg_num
);
1230 kprintf(" Logical: %d\n", pkg
->lpkg_num
);
1233 while (die
!= NULL
) {
1235 kprintf(" Physical: %d\n", die
->pdie_num
);
1236 kprintf(" Logical: %d\n", die
->ldie_num
);
1239 while (core
!= NULL
) {
1240 kprintf(" Core:\n");
1241 kprintf(" Physical: %d\n", core
->pcore_num
);
1242 kprintf(" Logical: %d\n", core
->lcore_num
);
1245 while (cpu
!= NULL
) {
1246 kprintf(" LCPU:\n");
1247 kprintf(" CPU #: %d\n", cpu
->cpu_num
);
1248 kprintf(" Physical: %d\n", cpu
->pnum
);
1249 kprintf(" Logical: %d\n", cpu
->lnum
);
1250 kprintf(" Flags: ");
1255 if (!cpu
->master
&& !cpu
->primary
)
1259 cpu
= cpu
->next_in_core
;
1262 core
= core
->next_in_die
;
1265 die
= die
->next_in_pkg
;
1271 #endif /* TOPO_DEBUG */