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>
46 #define INDENT_BUF_SIZE LEVEL_NO*3
49 /* Per-cpu sysctl nodes and info */
50 struct per_cpu_sysctl_info {
51 struct sysctl_ctx_list sysctl_ctx;
52 struct sysctl_oid *sysctl_tree;
56 char physical_siblings[8*MAXCPU];
57 char core_siblings[8*MAXCPU];
59 typedef struct per_cpu_sysctl_info per_cpu_sysctl_info_t;
61 static cpu_node_t cpu_topology_nodes[MAXCPU]; /* Memory for topology */
62 static cpu_node_t *cpu_root_node; /* Root node pointer */
64 static struct sysctl_ctx_list cpu_topology_sysctl_ctx;
65 static struct sysctl_oid *cpu_topology_sysctl_tree;
66 static char cpu_topology_members[8*MAXCPU];
67 static per_cpu_sysctl_info_t pcpu_sysctl[MAXCPU];
69 int cpu_topology_levels_number = 1;
70 cpu_node_t *root_cpu_node;
72 /* Get the next valid apicid starting
73 * from current apicid (curr_apicid
76 get_next_valid_apicid(int curr_apicid)
78 int next_apicid = curr_apicid;
82 while(get_cpuid_from_apicid(next_apicid) == -1 &&
83 next_apicid < NAPICID);
84 if (next_apicid == NAPICID) {
85 kprintf("Warning: No next valid APICID found. Returning -1\n");
91 /* Generic topology tree. The parameters have the following meaning:
92 * - children_no_per_level : the number of children on each level
93 * - level_types : the type of the level (THREAD, CORE, CHIP, etc)
94 * - cur_level : the current level of the tree
95 * - node : the current node
96 * - last_free_node : the last free node in the global array.
97 * - cpuid : basicly this are the ids of the leafs
100 build_topology_tree(int *children_no_per_level,
101 uint8_t *level_types,
104 cpu_node_t **last_free_node,
109 node->child_no = children_no_per_level[cur_level];
110 node->type = level_types[cur_level];
112 node->compute_unit_id = -1;
114 if (node->child_no == 0) {
115 *apicid = get_next_valid_apicid(*apicid);
116 node->members = CPUMASK(get_cpuid_from_apicid(*apicid));
120 if (node->parent_node == NULL)
121 root_cpu_node = node;
123 for (i = 0; i < node->child_no; i++) {
124 node->child_node[i] = *last_free_node;
127 node->child_node[i]->parent_node = node;
129 build_topology_tree(children_no_per_level,
136 node->members |= node->child_node[i]->members;
140 #if defined(__x86_64__) && !defined(_KERNEL_VIRTUAL)
142 migrate_elements(cpu_node_t **a, int n, int pos)
146 for (i = pos; i < n - 1 ; i++) {
153 /* Build CPU topology. The detection is made by comparing the
154 * chip, core and logical IDs of each CPU with the IDs of the
155 * BSP. When we found a match, at that level the CPUs are siblings.
158 build_cpu_topology(void)
160 detect_cpu_topology();
163 int threads_per_core = 0;
164 int cores_per_chip = 0;
165 int chips_per_package = 0;
166 int children_no_per_level[LEVEL_NO];
167 uint8_t level_types[LEVEL_NO];
170 cpu_node_t *root = &cpu_topology_nodes[0];
171 cpu_node_t *last_free_node = root + 1;
173 /* Assume that the topology is uniform.
174 * Find the number of siblings within chip
175 * and witin core to build up the topology
177 for (i = 0; i < ncpus; i++) {
179 cpumask_t mask = CPUMASK(i);
181 if ((mask & smp_active_mask) == 0)
184 if (get_chip_ID(BSPID) == get_chip_ID(i))
189 if (get_core_number_within_chip(BSPID) ==
190 get_core_number_within_chip(i))
194 cores_per_chip /= threads_per_core;
195 chips_per_package = ncpus / (cores_per_chip * threads_per_core);
198 kprintf("CPU Topology: cores_per_chip: %d; threads_per_core: %d; chips_per_package: %d;\n",
199 cores_per_chip, threads_per_core, chips_per_package);
201 if (threads_per_core > 1) { /* HT available - 4 levels */
203 children_no_per_level[0] = chips_per_package;
204 children_no_per_level[1] = cores_per_chip;
205 children_no_per_level[2] = threads_per_core;
206 children_no_per_level[3] = 0;
208 level_types[0] = PACKAGE_LEVEL;
209 level_types[1] = CHIP_LEVEL;
210 level_types[2] = CORE_LEVEL;
211 level_types[3] = THREAD_LEVEL;
213 build_topology_tree(children_no_per_level,
220 cpu_topology_levels_number = 4;
222 } else if (cores_per_chip > 1) { /* No HT available - 3 levels */
224 children_no_per_level[0] = chips_per_package;
225 children_no_per_level[1] = cores_per_chip;
226 children_no_per_level[2] = 0;
228 level_types[0] = PACKAGE_LEVEL;
229 level_types[1] = CHIP_LEVEL;
230 level_types[2] = CORE_LEVEL;
232 build_topology_tree(children_no_per_level,
239 cpu_topology_levels_number = 3;
241 } else { /* No HT and no Multi-Core - 2 levels */
243 children_no_per_level[0] = chips_per_package;
244 children_no_per_level[1] = 0;
246 level_types[0] = PACKAGE_LEVEL;
247 level_types[1] = CHIP_LEVEL;
249 build_topology_tree(children_no_per_level,
256 cpu_topology_levels_number = 2;
260 cpu_root_node = root;
263 #if defined(__x86_64__) && !defined(_KERNEL_VIRTUAL)
264 if (fix_amd_topology() == 0) {
265 int visited[MAXCPU], i, j, pos, cpuid;
266 cpu_node_t *leaf, *parent;
268 bzero(visited, MAXCPU * sizeof(int));
270 for (i = 0; i < ncpus; i++) {
271 if (visited[i] == 0) {
274 leaf = get_cpu_node_by_cpuid(i);
276 if (leaf->type == CORE_LEVEL) {
277 parent = leaf->parent_node;
279 last_free_node->child_node[0] = leaf;
280 last_free_node->child_no = 1;
281 last_free_node->members = leaf->members;
282 last_free_node->compute_unit_id = leaf->compute_unit_id;
283 last_free_node->parent_node = parent;
284 last_free_node->type = CORE_LEVEL;
287 for (j = 0; j < parent->child_no; j++) {
288 if (parent->child_node[j] != leaf) {
290 cpuid = BSFCPUMASK(parent->child_node[j]->members);
291 if (visited[cpuid] == 0 &&
292 parent->child_node[j]->compute_unit_id == leaf->compute_unit_id) {
294 last_free_node->child_node[last_free_node->child_no] = parent->child_node[j];
295 last_free_node->child_no++;
296 last_free_node->members |= parent->child_node[j]->members;
298 parent->child_node[j]->type = THREAD_LEVEL;
299 parent->child_node[j]->parent_node = last_free_node;
302 migrate_elements(parent->child_node, parent->child_no, j);
310 if (last_free_node->child_no > 1) {
311 parent->child_node[pos] = last_free_node;
312 leaf->type = THREAD_LEVEL;
313 leaf->parent_node = last_free_node;
323 /* Recursive function helper to print the CPU topology tree */
325 print_cpu_topology_tree_sysctl_helper(cpu_node_t *node,
334 sbuf_bcat(sb, buf, buf_len);
336 sbuf_printf(sb, "\\-");
337 buf[buf_len] = ' ';buf_len++;
338 buf[buf_len] = ' ';buf_len++;
340 sbuf_printf(sb, "|-");
341 buf[buf_len] = '|';buf_len++;
342 buf[buf_len] = ' ';buf_len++;
345 bsr_member = BSRCPUMASK(node->members);
347 if (node->type == PACKAGE_LEVEL) {
348 sbuf_printf(sb,"PACKAGE MEMBERS: ");
349 } else if (node->type == CHIP_LEVEL) {
350 sbuf_printf(sb,"CHIP ID %d: ",
351 get_chip_ID(bsr_member));
352 } else if (node->type == CORE_LEVEL) {
353 if (node->compute_unit_id != (uint8_t)-1) {
354 sbuf_printf(sb,"Compute Unit ID %d: ",
355 node->compute_unit_id);
357 sbuf_printf(sb,"CORE ID %d: ",
358 get_core_number_within_chip(bsr_member));
360 } else if (node->type == THREAD_LEVEL) {
361 if (node->compute_unit_id != (uint8_t)-1) {
362 sbuf_printf(sb,"CORE ID %d: ",
363 get_core_number_within_chip(bsr_member));
365 sbuf_printf(sb,"THREAD ID %d: ",
366 get_logical_CPU_number_within_core(bsr_member));
369 sbuf_printf(sb,"UNKNOWN: ");
371 CPUSET_FOREACH(i, node->members) {
372 sbuf_printf(sb,"cpu%d ", i);
375 sbuf_printf(sb,"\n");
377 for (i = 0; i < node->child_no; i++) {
378 print_cpu_topology_tree_sysctl_helper(node->child_node[i],
379 sb, buf, buf_len, i == (node->child_no -1));
383 /* SYSCTL PROCEDURE for printing the CPU Topology tree */
385 print_cpu_topology_tree_sysctl(SYSCTL_HANDLER_ARGS)
389 char buf[INDENT_BUF_SIZE];
391 KASSERT(cpu_root_node != NULL, ("cpu_root_node isn't initialized"));
393 sb = sbuf_new(NULL, NULL, 500, SBUF_AUTOEXTEND);
397 sbuf_printf(sb,"\n");
398 print_cpu_topology_tree_sysctl_helper(cpu_root_node, sb, buf, 0, 1);
402 ret = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb));
409 /* SYSCTL PROCEDURE for printing the CPU Topology level description */
411 print_cpu_topology_level_description_sysctl(SYSCTL_HANDLER_ARGS)
416 sb = sbuf_new(NULL, NULL, 500, SBUF_AUTOEXTEND);
420 if (cpu_topology_levels_number == 4) /* HT available */
421 sbuf_printf(sb, "0 - thread; 1 - core; 2 - socket; 3 - anything");
422 else if (cpu_topology_levels_number == 3) /* No HT available */
423 sbuf_printf(sb, "0 - core; 1 - socket; 2 - anything");
424 else if (cpu_topology_levels_number == 2) /* No HT and no Multi-Core */
425 sbuf_printf(sb, "0 - socket; 1 - anything");
427 sbuf_printf(sb, "Unknown");
431 ret = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb));
438 /* Find a cpu_node_t by a mask */
440 get_cpu_node_by_cpumask(cpu_node_t * node,
443 cpu_node_t * found = NULL;
446 if (node->members == mask) {
450 for (i = 0; i < node->child_no; i++) {
451 found = get_cpu_node_by_cpumask(node->child_node[i], mask);
460 get_cpu_node_by_cpuid(int cpuid) {
461 cpumask_t mask = CPUMASK(cpuid);
463 KASSERT(cpu_root_node != NULL, ("cpu_root_node isn't initialized"));
465 return get_cpu_node_by_cpumask(cpu_root_node, mask);
468 /* Get the mask of siblings for level_type of a cpuid */
470 get_cpumask_from_level(int cpuid,
474 cpumask_t mask = CPUMASK(cpuid);
476 KASSERT(cpu_root_node != NULL, ("cpu_root_node isn't initialized"));
478 node = get_cpu_node_by_cpumask(cpu_root_node, mask);
484 while (node != NULL) {
485 if (node->type == level_type) {
486 return node->members;
488 node = node->parent_node;
494 /* init pcpu_sysctl structure info */
496 init_pcpu_topology_sysctl(void)
503 for (i = 0; i < ncpus; i++) {
505 sbuf_new(&sb, pcpu_sysctl[i].cpu_name,
506 sizeof(pcpu_sysctl[i].cpu_name), SBUF_FIXEDLEN);
507 sbuf_printf(&sb,"cpu%d", i);
511 /* Get physical siblings */
512 mask = get_cpumask_from_level(i, CHIP_LEVEL);
514 pcpu_sysctl[i].physical_id = INVALID_ID;
518 sbuf_new(&sb, pcpu_sysctl[i].physical_siblings,
519 sizeof(pcpu_sysctl[i].physical_siblings), SBUF_FIXEDLEN);
520 CPUSET_FOREACH(cpu, mask) {
521 sbuf_printf(&sb,"cpu%d ", cpu);
526 pcpu_sysctl[i].physical_id = get_chip_ID(i);
528 /* Get core siblings */
529 mask = get_cpumask_from_level(i, CORE_LEVEL);
531 pcpu_sysctl[i].core_id = INVALID_ID;
535 sbuf_new(&sb, pcpu_sysctl[i].core_siblings,
536 sizeof(pcpu_sysctl[i].core_siblings), SBUF_FIXEDLEN);
537 CPUSET_FOREACH(cpu, mask) {
538 sbuf_printf(&sb,"cpu%d ", cpu);
543 pcpu_sysctl[i].core_id = get_core_number_within_chip(i);
548 /* Build SYSCTL structure for revealing
549 * the CPU Topology to user-space.
552 build_sysctl_cpu_topology(void)
557 /* SYSCTL new leaf for "cpu_topology" */
558 sysctl_ctx_init(&cpu_topology_sysctl_ctx);
559 cpu_topology_sysctl_tree = SYSCTL_ADD_NODE(&cpu_topology_sysctl_ctx,
560 SYSCTL_STATIC_CHILDREN(_hw),
565 /* SYSCTL cpu_topology "tree" entry */
566 SYSCTL_ADD_PROC(&cpu_topology_sysctl_ctx,
567 SYSCTL_CHILDREN(cpu_topology_sysctl_tree),
568 OID_AUTO, "tree", CTLTYPE_STRING | CTLFLAG_RD,
569 NULL, 0, print_cpu_topology_tree_sysctl, "A",
570 "Tree print of CPU topology");
572 /* SYSCTL cpu_topology "level_description" entry */
573 SYSCTL_ADD_PROC(&cpu_topology_sysctl_ctx,
574 SYSCTL_CHILDREN(cpu_topology_sysctl_tree),
575 OID_AUTO, "level_description", CTLTYPE_STRING | CTLFLAG_RD,
576 NULL, 0, print_cpu_topology_level_description_sysctl, "A",
577 "Level description of CPU topology");
579 /* SYSCTL cpu_topology "members" entry */
580 sbuf_new(&sb, cpu_topology_members,
581 sizeof(cpu_topology_members), SBUF_FIXEDLEN);
582 CPUSET_FOREACH(i, cpu_root_node->members) {
583 sbuf_printf(&sb,"cpu%d ", i);
587 SYSCTL_ADD_STRING(&cpu_topology_sysctl_ctx,
588 SYSCTL_CHILDREN(cpu_topology_sysctl_tree),
589 OID_AUTO, "members", CTLFLAG_RD,
590 cpu_topology_members, 0,
591 "Members of the CPU Topology");
593 /* SYSCTL per_cpu info */
594 for (i = 0; i < ncpus; i++) {
595 /* New leaf : hw.cpu_topology.cpux */
596 sysctl_ctx_init(&pcpu_sysctl[i].sysctl_ctx);
597 pcpu_sysctl[i].sysctl_tree = SYSCTL_ADD_NODE(&pcpu_sysctl[i].sysctl_ctx,
598 SYSCTL_CHILDREN(cpu_topology_sysctl_tree),
600 pcpu_sysctl[i].cpu_name,
603 /* Check if the physical_id found is valid */
604 if (pcpu_sysctl[i].physical_id == INVALID_ID) {
608 /* Add physical id info */
609 SYSCTL_ADD_INT(&pcpu_sysctl[i].sysctl_ctx,
610 SYSCTL_CHILDREN(pcpu_sysctl[i].sysctl_tree),
611 OID_AUTO, "physical_id", CTLFLAG_RD,
612 &pcpu_sysctl[i].physical_id, 0,
615 /* Add physical siblings */
616 SYSCTL_ADD_STRING(&pcpu_sysctl[i].sysctl_ctx,
617 SYSCTL_CHILDREN(pcpu_sysctl[i].sysctl_tree),
618 OID_AUTO, "physical_siblings", CTLFLAG_RD,
619 pcpu_sysctl[i].physical_siblings, 0,
620 "Physical siblings");
622 /* Check if the core_id found is valid */
623 if (pcpu_sysctl[i].core_id == INVALID_ID) {
627 /* Add core id info */
628 SYSCTL_ADD_INT(&pcpu_sysctl[i].sysctl_ctx,
629 SYSCTL_CHILDREN(pcpu_sysctl[i].sysctl_tree),
630 OID_AUTO, "core_id", CTLFLAG_RD,
631 &pcpu_sysctl[i].core_id, 0,
634 /*Add core siblings */
635 SYSCTL_ADD_STRING(&pcpu_sysctl[i].sysctl_ctx,
636 SYSCTL_CHILDREN(pcpu_sysctl[i].sysctl_tree),
637 OID_AUTO, "core_siblings", CTLFLAG_RD,
638 pcpu_sysctl[i].core_siblings, 0,
643 /* Build the CPU Topology and SYSCTL Topology tree */
645 init_cpu_topology(void)
647 build_cpu_topology();
649 init_pcpu_topology_sysctl();
650 build_sysctl_cpu_topology();
652 SYSINIT(cpu_topology, SI_BOOT2_CPU_TOPOLOGY, SI_ORDER_FIRST,
653 init_cpu_topology, NULL)