2 * Copyright (c) 2012 The DragonFly Project. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
14 * 3. Neither the name of The DragonFly Project nor the names of its
15 * contributors may be used to endorse or promote products derived
16 * from this software without specific, prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
28 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/sysctl.h>
38 #include <sys/cpu_topology.h>
40 #include <machine/smp.h>
48 #define INDENT_BUF_SIZE LEVEL_NO*3
51 /* Per-cpu sysctl nodes and info */
52 struct per_cpu_sysctl_info {
53 struct sysctl_ctx_list sysctl_ctx;
54 struct sysctl_oid *sysctl_tree;
58 char physical_siblings[8*MAXCPU];
59 char core_siblings[8*MAXCPU];
61 typedef struct per_cpu_sysctl_info per_cpu_sysctl_info_t;
63 static cpu_node_t cpu_topology_nodes[MAXCPU]; /* Memory for topology */
64 static cpu_node_t *cpu_root_node; /* Root node pointer */
66 static struct sysctl_ctx_list cpu_topology_sysctl_ctx;
67 static struct sysctl_oid *cpu_topology_sysctl_tree;
68 static char cpu_topology_members[8*MAXCPU];
69 static per_cpu_sysctl_info_t pcpu_sysctl[MAXCPU];
71 int cpu_topology_levels_number = 1;
73 /* Get the next valid apicid starting
74 * from current apicid (curr_apicid
77 get_next_valid_apicid(int curr_apicid)
79 int next_apicid = curr_apicid;
83 while(get_cpuid_from_apicid(next_apicid) == -1 &&
84 next_apicid < NAPICID);
85 if (next_apicid == NAPICID) {
86 kprintf("Warning: No next valid APICID found. Returning -1\n");
92 /* Generic topology tree. The parameters have the following meaning:
93 * - children_no_per_level : the number of children on each level
94 * - level_types : the type of the level (THREAD, CORE, CHIP, etc)
95 * - cur_level : the current level of the tree
96 * - node : the current node
97 * - last_free_node : the last free node in the global array.
98 * - cpuid : basicly this are the ids of the leafs
101 build_topology_tree(int *children_no_per_level,
102 uint8_t *level_types,
105 cpu_node_t **last_free_node,
110 node->child_no = children_no_per_level[cur_level];
111 node->type = level_types[cur_level];
114 if (node->child_no == 0) {
115 node->child_node = NULL;
116 *apicid = get_next_valid_apicid(*apicid);
117 node->members = CPUMASK(get_cpuid_from_apicid(*apicid));
121 node->child_node = *last_free_node;
122 (*last_free_node) += node->child_no;
124 for (i = 0; i < node->child_no; i++) {
126 node->child_node[i].parent_node = node;
128 build_topology_tree(children_no_per_level,
131 &(node->child_node[i]),
135 node->members |= node->child_node[i].members;
139 /* Build CPU topology. The detection is made by comparing the
140 * chip, core and logical IDs of each CPU with the IDs of the
141 * BSP. When we found a match, at that level the CPUs are siblings.
144 build_cpu_topology(void)
146 detect_cpu_topology();
149 int threads_per_core = 0;
150 int cores_per_chip = 0;
151 int chips_per_package = 0;
152 int children_no_per_level[LEVEL_NO];
153 uint8_t level_types[LEVEL_NO];
156 cpu_node_t *root = &cpu_topology_nodes[0];
157 cpu_node_t *last_free_node = root + 1;
159 /* Assume that the topology is uniform.
160 * Find the number of siblings within chip
161 * and witin core to build up the topology
163 for (i = 0; i < ncpus; i++) {
165 cpumask_t mask = CPUMASK(i);
167 if ((mask & smp_active_mask) == 0)
170 if (get_chip_ID(BSPID) == get_chip_ID(i))
175 if (get_core_number_within_chip(BSPID) ==
176 get_core_number_within_chip(i))
180 cores_per_chip /= threads_per_core;
181 chips_per_package = ncpus / (cores_per_chip * threads_per_core);
184 kprintf("CPU Topology: cores_per_chip: %d; threads_per_core: %d; chips_per_package: %d;\n",
185 cores_per_chip, threads_per_core, chips_per_package);
187 if (threads_per_core > 1) { /* HT available - 4 levels */
189 children_no_per_level[0] = chips_per_package;
190 children_no_per_level[1] = cores_per_chip;
191 children_no_per_level[2] = threads_per_core;
192 children_no_per_level[3] = 0;
194 level_types[0] = PACKAGE_LEVEL;
195 level_types[1] = CHIP_LEVEL;
196 level_types[2] = CORE_LEVEL;
197 level_types[3] = THREAD_LEVEL;
199 build_topology_tree(children_no_per_level,
206 cpu_topology_levels_number = 4;
208 } else if (cores_per_chip > 1) { /* No HT available - 3 levels */
210 children_no_per_level[0] = chips_per_package;
211 children_no_per_level[1] = cores_per_chip;
212 children_no_per_level[2] = 0;
214 level_types[0] = PACKAGE_LEVEL;
215 level_types[1] = CHIP_LEVEL;
216 level_types[2] = CORE_LEVEL;
218 build_topology_tree(children_no_per_level,
225 cpu_topology_levels_number = 3;
227 } else { /* No HT and no Multi-Core - 2 levels */
229 children_no_per_level[0] = chips_per_package;
230 children_no_per_level[1] = 0;
232 level_types[0] = PACKAGE_LEVEL;
233 level_types[1] = CHIP_LEVEL;
235 build_topology_tree(children_no_per_level,
242 cpu_topology_levels_number = 2;
249 /* Recursive function helper to print the CPU topology tree */
251 print_cpu_topology_tree_sysctl_helper(cpu_node_t *node,
260 sbuf_bcat(sb, buf, buf_len);
262 sbuf_printf(sb, "\\-");
263 buf[buf_len] = ' ';buf_len++;
264 buf[buf_len] = ' ';buf_len++;
266 sbuf_printf(sb, "|-");
267 buf[buf_len] = '|';buf_len++;
268 buf[buf_len] = ' ';buf_len++;
271 bsr_member = BSRCPUMASK(node->members);
273 if (node->type == PACKAGE_LEVEL) {
274 sbuf_printf(sb,"PACKAGE MEMBERS: ");
275 } else if (node->type == CHIP_LEVEL) {
276 sbuf_printf(sb,"CHIP ID %d: ",
277 get_chip_ID(bsr_member));
278 } else if (node->type == CORE_LEVEL) {
279 sbuf_printf(sb,"CORE ID %d: ",
280 get_core_number_within_chip(bsr_member));
281 } else if (node->type == THREAD_LEVEL) {
282 sbuf_printf(sb,"THREAD ID %d: ",
283 get_logical_CPU_number_within_core(bsr_member));
285 sbuf_printf(sb,"UNKNOWN: ");
287 CPUSET_FOREACH(i, node->members) {
288 sbuf_printf(sb,"cpu%d ", i);
291 sbuf_printf(sb,"\n");
293 for (i = 0; i < node->child_no; i++) {
294 print_cpu_topology_tree_sysctl_helper(&(node->child_node[i]),
295 sb, buf, buf_len, i == (node->child_no -1));
299 /* SYSCTL PROCEDURE for printing the CPU Topology tree */
301 print_cpu_topology_tree_sysctl(SYSCTL_HANDLER_ARGS)
305 char buf[INDENT_BUF_SIZE];
307 KASSERT(cpu_root_node != NULL, ("cpu_root_node isn't initialized"));
309 sb = sbuf_new(NULL, NULL, 500, SBUF_AUTOEXTEND);
313 sbuf_printf(sb,"\n");
314 print_cpu_topology_tree_sysctl_helper(cpu_root_node, sb, buf, 0, 1);
318 ret = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb));
325 /* SYSCTL PROCEDURE for printing the CPU Topology level description */
327 print_cpu_topology_level_description_sysctl(SYSCTL_HANDLER_ARGS)
332 sb = sbuf_new(NULL, NULL, 500, SBUF_AUTOEXTEND);
336 if (cpu_topology_levels_number == 4) /* HT available */
337 sbuf_printf(sb, "0 - thread; 1 - core; 2 - socket; 3 - anything");
338 else if (cpu_topology_levels_number == 3) /* No HT available */
339 sbuf_printf(sb, "0 - core; 1 - socket; 2 - anything");
340 else if (cpu_topology_levels_number == 2) /* No HT and no Multi-Core */
341 sbuf_printf(sb, "0 - socket; 1 - anything");
343 sbuf_printf(sb, "Unknown");
347 ret = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb));
354 /* Find a cpu_node_t by a mask */
356 get_cpu_node_by_cpumask(cpu_node_t * node,
359 cpu_node_t * found = NULL;
362 if (node->members == mask) {
366 for (i = 0; i < node->child_no; i++) {
367 found = get_cpu_node_by_cpumask(&(node->child_node[i]), mask);
376 get_cpu_node_by_cpuid(int cpuid) {
377 cpumask_t mask = CPUMASK(cpuid);
379 KASSERT(cpu_root_node != NULL, ("cpu_root_node isn't initialized"));
381 return get_cpu_node_by_cpumask(cpu_root_node, mask);
384 /* Get the mask of siblings for level_type of a cpuid */
386 get_cpumask_from_level(int cpuid,
390 cpumask_t mask = CPUMASK(cpuid);
392 KASSERT(cpu_root_node != NULL, ("cpu_root_node isn't initialized"));
394 node = get_cpu_node_by_cpumask(cpu_root_node, mask);
400 while (node != NULL) {
401 if (node->type == level_type) {
402 return node->members;
404 node = node->parent_node;
410 /* init pcpu_sysctl structure info */
412 init_pcpu_topology_sysctl(void)
419 for (i = 0; i < ncpus; i++) {
421 sbuf_new(&sb, pcpu_sysctl[i].cpu_name,
422 sizeof(pcpu_sysctl[i].cpu_name), SBUF_FIXEDLEN);
423 sbuf_printf(&sb,"cpu%d", i);
427 /* Get physical siblings */
428 mask = get_cpumask_from_level(i, CHIP_LEVEL);
430 pcpu_sysctl[i].physical_id = INVALID_ID;
434 sbuf_new(&sb, pcpu_sysctl[i].physical_siblings,
435 sizeof(pcpu_sysctl[i].physical_siblings), SBUF_FIXEDLEN);
436 CPUSET_FOREACH(cpu, mask) {
437 sbuf_printf(&sb,"cpu%d ", cpu);
442 pcpu_sysctl[i].physical_id = get_chip_ID(i);
444 /* Get core siblings */
445 mask = get_cpumask_from_level(i, CORE_LEVEL);
447 pcpu_sysctl[i].core_id = INVALID_ID;
451 sbuf_new(&sb, pcpu_sysctl[i].core_siblings,
452 sizeof(pcpu_sysctl[i].core_siblings), SBUF_FIXEDLEN);
453 CPUSET_FOREACH(cpu, mask) {
454 sbuf_printf(&sb,"cpu%d ", cpu);
459 pcpu_sysctl[i].core_id = get_core_number_within_chip(i);
464 /* Build SYSCTL structure for revealing
465 * the CPU Topology to user-space.
468 build_sysctl_cpu_topology(void)
473 /* SYSCTL new leaf for "cpu_topology" */
474 sysctl_ctx_init(&cpu_topology_sysctl_ctx);
475 cpu_topology_sysctl_tree = SYSCTL_ADD_NODE(&cpu_topology_sysctl_ctx,
476 SYSCTL_STATIC_CHILDREN(_hw),
481 /* SYSCTL cpu_topology "tree" entry */
482 SYSCTL_ADD_PROC(&cpu_topology_sysctl_ctx,
483 SYSCTL_CHILDREN(cpu_topology_sysctl_tree),
484 OID_AUTO, "tree", CTLTYPE_STRING | CTLFLAG_RD,
485 NULL, 0, print_cpu_topology_tree_sysctl, "A",
486 "Tree print of CPU topology");
488 /* SYSCTL cpu_topology "level_description" entry */
489 SYSCTL_ADD_PROC(&cpu_topology_sysctl_ctx,
490 SYSCTL_CHILDREN(cpu_topology_sysctl_tree),
491 OID_AUTO, "level_description", CTLTYPE_STRING | CTLFLAG_RD,
492 NULL, 0, print_cpu_topology_level_description_sysctl, "A",
493 "Level description of CPU topology");
495 /* SYSCTL cpu_topology "members" entry */
496 sbuf_new(&sb, cpu_topology_members,
497 sizeof(cpu_topology_members), SBUF_FIXEDLEN);
498 CPUSET_FOREACH(i, cpu_root_node->members) {
499 sbuf_printf(&sb,"cpu%d ", i);
503 SYSCTL_ADD_STRING(&cpu_topology_sysctl_ctx,
504 SYSCTL_CHILDREN(cpu_topology_sysctl_tree),
505 OID_AUTO, "members", CTLFLAG_RD,
506 cpu_topology_members, 0,
507 "Members of the CPU Topology");
509 /* SYSCTL per_cpu info */
510 for (i = 0; i < ncpus; i++) {
511 /* New leaf : hw.cpu_topology.cpux */
512 sysctl_ctx_init(&pcpu_sysctl[i].sysctl_ctx);
513 pcpu_sysctl[i].sysctl_tree = SYSCTL_ADD_NODE(&pcpu_sysctl[i].sysctl_ctx,
514 SYSCTL_CHILDREN(cpu_topology_sysctl_tree),
516 pcpu_sysctl[i].cpu_name,
519 /* Check if the physical_id found is valid */
520 if (pcpu_sysctl[i].physical_id == INVALID_ID) {
524 /* Add physical id info */
525 SYSCTL_ADD_INT(&pcpu_sysctl[i].sysctl_ctx,
526 SYSCTL_CHILDREN(pcpu_sysctl[i].sysctl_tree),
527 OID_AUTO, "physical_id", CTLFLAG_RD,
528 &pcpu_sysctl[i].physical_id, 0,
531 /* Add physical siblings */
532 SYSCTL_ADD_STRING(&pcpu_sysctl[i].sysctl_ctx,
533 SYSCTL_CHILDREN(pcpu_sysctl[i].sysctl_tree),
534 OID_AUTO, "physical_siblings", CTLFLAG_RD,
535 pcpu_sysctl[i].physical_siblings, 0,
536 "Physical siblings");
538 /* Check if the core_id found is valid */
539 if (pcpu_sysctl[i].core_id == INVALID_ID) {
543 /* Add core id info */
544 SYSCTL_ADD_INT(&pcpu_sysctl[i].sysctl_ctx,
545 SYSCTL_CHILDREN(pcpu_sysctl[i].sysctl_tree),
546 OID_AUTO, "core_id", CTLFLAG_RD,
547 &pcpu_sysctl[i].core_id, 0,
550 /*Add core siblings */
551 SYSCTL_ADD_STRING(&pcpu_sysctl[i].sysctl_ctx,
552 SYSCTL_CHILDREN(pcpu_sysctl[i].sysctl_tree),
553 OID_AUTO, "core_siblings", CTLFLAG_RD,
554 pcpu_sysctl[i].core_siblings, 0,
559 /* Build the CPU Topology and SYSCTL Topology tree */
561 init_cpu_topology(void)
563 cpu_root_node = build_cpu_topology();
565 init_pcpu_topology_sysctl();
566 build_sysctl_cpu_topology();
568 SYSINIT(cpu_topology, SI_BOOT2_CPU_TOPOLOGY, SI_ORDER_FIRST,
569 init_cpu_topology, NULL)