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];
68 static void sbuf_print_cpuset(struct sbuf *sb, cpumask_t *mask);
70 int cpu_topology_levels_number = 1;
71 cpu_node_t *root_cpu_node;
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];
112 CPUMASK_ASSZERO(node->members);
113 node->compute_unit_id = -1;
115 if (node->child_no == 0) {
116 *apicid = get_next_valid_apicid(*apicid);
117 CPUMASK_ASSBIT(node->members, get_cpuid_from_apicid(*apicid));
121 if (node->parent_node == NULL)
122 root_cpu_node = node;
124 for (i = 0; i < node->child_no; i++) {
125 node->child_node[i] = *last_free_node;
128 node->child_node[i]->parent_node = node;
130 build_topology_tree(children_no_per_level,
137 CPUMASK_ORMASK(node->members, node->child_node[i]->members);
141 #if defined(__x86_64__) && !defined(_KERNEL_VIRTUAL)
143 migrate_elements(cpu_node_t **a, int n, int pos)
147 for (i = pos; i < n - 1 ; i++) {
154 /* Build CPU topology. The detection is made by comparing the
155 * chip, core and logical IDs of each CPU with the IDs of the
156 * BSP. When we found a match, at that level the CPUs are siblings.
159 build_cpu_topology(void)
161 detect_cpu_topology();
164 int threads_per_core = 0;
165 int cores_per_chip = 0;
166 int chips_per_package = 0;
167 int children_no_per_level[LEVEL_NO];
168 uint8_t level_types[LEVEL_NO];
171 cpu_node_t *root = &cpu_topology_nodes[0];
172 cpu_node_t *last_free_node = root + 1;
174 /* Assume that the topology is uniform.
175 * Find the number of siblings within chip
176 * and witin core to build up the topology
178 for (i = 0; i < ncpus; i++) {
181 CPUMASK_ASSBIT(mask, i);
183 if (CPUMASK_TESTMASK(mask, smp_active_mask) == 0)
186 if (get_chip_ID(BSPID) == get_chip_ID(i))
191 if (get_core_number_within_chip(BSPID) ==
192 get_core_number_within_chip(i))
196 cores_per_chip /= threads_per_core;
197 chips_per_package = ncpus / (cores_per_chip * threads_per_core);
200 kprintf("CPU Topology: cores_per_chip: %d; threads_per_core: %d; chips_per_package: %d;\n",
201 cores_per_chip, threads_per_core, chips_per_package);
203 if (threads_per_core > 1) { /* HT available - 4 levels */
205 children_no_per_level[0] = chips_per_package;
206 children_no_per_level[1] = cores_per_chip;
207 children_no_per_level[2] = threads_per_core;
208 children_no_per_level[3] = 0;
210 level_types[0] = PACKAGE_LEVEL;
211 level_types[1] = CHIP_LEVEL;
212 level_types[2] = CORE_LEVEL;
213 level_types[3] = THREAD_LEVEL;
215 build_topology_tree(children_no_per_level,
222 cpu_topology_levels_number = 4;
224 } else if (cores_per_chip > 1) { /* No HT available - 3 levels */
226 children_no_per_level[0] = chips_per_package;
227 children_no_per_level[1] = cores_per_chip;
228 children_no_per_level[2] = 0;
230 level_types[0] = PACKAGE_LEVEL;
231 level_types[1] = CHIP_LEVEL;
232 level_types[2] = CORE_LEVEL;
234 build_topology_tree(children_no_per_level,
241 cpu_topology_levels_number = 3;
243 } else { /* No HT and no Multi-Core - 2 levels */
245 children_no_per_level[0] = chips_per_package;
246 children_no_per_level[1] = 0;
248 level_types[0] = PACKAGE_LEVEL;
249 level_types[1] = CHIP_LEVEL;
251 build_topology_tree(children_no_per_level,
258 cpu_topology_levels_number = 2;
262 cpu_root_node = root;
265 #if defined(__x86_64__) && !defined(_KERNEL_VIRTUAL)
266 if (fix_amd_topology() == 0) {
267 int visited[MAXCPU], i, j, pos, cpuid;
268 cpu_node_t *leaf, *parent;
270 bzero(visited, MAXCPU * sizeof(int));
272 for (i = 0; i < ncpus; i++) {
273 if (visited[i] == 0) {
276 leaf = get_cpu_node_by_cpuid(i);
278 if (leaf->type == CORE_LEVEL) {
279 parent = leaf->parent_node;
281 last_free_node->child_node[0] = leaf;
282 last_free_node->child_no = 1;
283 last_free_node->members = leaf->members;
284 last_free_node->compute_unit_id = leaf->compute_unit_id;
285 last_free_node->parent_node = parent;
286 last_free_node->type = CORE_LEVEL;
289 for (j = 0; j < parent->child_no; j++) {
290 if (parent->child_node[j] != leaf) {
292 cpuid = BSFCPUMASK(parent->child_node[j]->members);
293 if (visited[cpuid] == 0 &&
294 parent->child_node[j]->compute_unit_id == leaf->compute_unit_id) {
296 last_free_node->child_node[last_free_node->child_no] = parent->child_node[j];
297 last_free_node->child_no++;
298 CPUMASK_ORMASK(last_free_node->members, parent->child_node[j]->members);
300 parent->child_node[j]->type = THREAD_LEVEL;
301 parent->child_node[j]->parent_node = last_free_node;
304 migrate_elements(parent->child_node, parent->child_no, j);
312 if (last_free_node->child_no > 1) {
313 parent->child_node[pos] = last_free_node;
314 leaf->type = THREAD_LEVEL;
315 leaf->parent_node = last_free_node;
325 /* Recursive function helper to print the CPU topology tree */
327 print_cpu_topology_tree_sysctl_helper(cpu_node_t *node,
336 sbuf_bcat(sb, buf, buf_len);
338 sbuf_printf(sb, "\\-");
339 buf[buf_len] = ' ';buf_len++;
340 buf[buf_len] = ' ';buf_len++;
342 sbuf_printf(sb, "|-");
343 buf[buf_len] = '|';buf_len++;
344 buf[buf_len] = ' ';buf_len++;
347 bsr_member = BSRCPUMASK(node->members);
349 if (node->type == PACKAGE_LEVEL) {
350 sbuf_printf(sb,"PACKAGE MEMBERS: ");
351 } else if (node->type == CHIP_LEVEL) {
352 sbuf_printf(sb,"CHIP ID %d: ",
353 get_chip_ID(bsr_member));
354 } else if (node->type == CORE_LEVEL) {
355 if (node->compute_unit_id != (uint8_t)-1) {
356 sbuf_printf(sb,"Compute Unit ID %d: ",
357 node->compute_unit_id);
359 sbuf_printf(sb,"CORE ID %d: ",
360 get_core_number_within_chip(bsr_member));
362 } else if (node->type == THREAD_LEVEL) {
363 if (node->compute_unit_id != (uint8_t)-1) {
364 sbuf_printf(sb,"CORE ID %d: ",
365 get_core_number_within_chip(bsr_member));
367 sbuf_printf(sb,"THREAD ID %d: ",
368 get_logical_CPU_number_within_core(bsr_member));
371 sbuf_printf(sb,"UNKNOWN: ");
373 sbuf_print_cpuset(sb, &node->members);
374 sbuf_printf(sb,"\n");
376 for (i = 0; i < node->child_no; i++) {
377 print_cpu_topology_tree_sysctl_helper(node->child_node[i],
378 sb, buf, buf_len, i == (node->child_no -1));
382 /* SYSCTL PROCEDURE for printing the CPU Topology tree */
384 print_cpu_topology_tree_sysctl(SYSCTL_HANDLER_ARGS)
388 char buf[INDENT_BUF_SIZE];
390 KASSERT(cpu_root_node != NULL, ("cpu_root_node isn't initialized"));
392 sb = sbuf_new(NULL, NULL, 500, SBUF_AUTOEXTEND);
396 sbuf_printf(sb,"\n");
397 print_cpu_topology_tree_sysctl_helper(cpu_root_node, sb, buf, 0, 1);
401 ret = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb));
408 /* SYSCTL PROCEDURE for printing the CPU Topology level description */
410 print_cpu_topology_level_description_sysctl(SYSCTL_HANDLER_ARGS)
415 sb = sbuf_new(NULL, NULL, 500, SBUF_AUTOEXTEND);
419 if (cpu_topology_levels_number == 4) /* HT available */
420 sbuf_printf(sb, "0 - thread; 1 - core; 2 - socket; 3 - anything");
421 else if (cpu_topology_levels_number == 3) /* No HT available */
422 sbuf_printf(sb, "0 - core; 1 - socket; 2 - anything");
423 else if (cpu_topology_levels_number == 2) /* No HT and no Multi-Core */
424 sbuf_printf(sb, "0 - socket; 1 - anything");
426 sbuf_printf(sb, "Unknown");
430 ret = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb));
437 /* Find a cpu_node_t by a mask */
439 get_cpu_node_by_cpumask(cpu_node_t * node,
442 cpu_node_t * found = NULL;
445 if (CPUMASK_CMPMASKEQ(node->members, mask))
448 for (i = 0; i < node->child_no; i++) {
449 found = get_cpu_node_by_cpumask(node->child_node[i], mask);
458 get_cpu_node_by_cpuid(int cpuid) {
461 CPUMASK_ASSBIT(mask, 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,
476 CPUMASK_ASSBIT(mask, cpuid);
478 KASSERT(cpu_root_node != NULL, ("cpu_root_node isn't initialized"));
480 node = get_cpu_node_by_cpumask(cpu_root_node, mask);
483 CPUMASK_ASSZERO(mask);
487 while (node != NULL) {
488 if (node->type == level_type) {
489 return node->members;
491 node = node->parent_node;
493 CPUMASK_ASSZERO(mask);
498 /* init pcpu_sysctl structure info */
500 init_pcpu_topology_sysctl(void)
506 for (i = 0; i < ncpus; i++) {
508 sbuf_new(&sb, pcpu_sysctl[i].cpu_name,
509 sizeof(pcpu_sysctl[i].cpu_name), SBUF_FIXEDLEN);
510 sbuf_printf(&sb,"cpu%d", i);
514 /* Get physical siblings */
515 mask = get_cpumask_from_level(i, CHIP_LEVEL);
516 if (CPUMASK_TESTZERO(mask)) {
517 pcpu_sysctl[i].physical_id = INVALID_ID;
521 sbuf_new(&sb, pcpu_sysctl[i].physical_siblings,
522 sizeof(pcpu_sysctl[i].physical_siblings), SBUF_FIXEDLEN);
523 sbuf_print_cpuset(&sb, &mask);
527 pcpu_sysctl[i].physical_id = get_chip_ID(i);
529 /* Get core siblings */
530 mask = get_cpumask_from_level(i, CORE_LEVEL);
531 if (CPUMASK_TESTZERO(mask)) {
532 pcpu_sysctl[i].core_id = INVALID_ID;
536 sbuf_new(&sb, pcpu_sysctl[i].core_siblings,
537 sizeof(pcpu_sysctl[i].core_siblings), SBUF_FIXEDLEN);
538 sbuf_print_cpuset(&sb, &mask);
542 pcpu_sysctl[i].core_id = get_core_number_within_chip(i);
547 /* Build SYSCTL structure for revealing
548 * the CPU Topology to user-space.
551 build_sysctl_cpu_topology(void)
556 /* SYSCTL new leaf for "cpu_topology" */
557 sysctl_ctx_init(&cpu_topology_sysctl_ctx);
558 cpu_topology_sysctl_tree = SYSCTL_ADD_NODE(&cpu_topology_sysctl_ctx,
559 SYSCTL_STATIC_CHILDREN(_hw),
564 /* SYSCTL cpu_topology "tree" entry */
565 SYSCTL_ADD_PROC(&cpu_topology_sysctl_ctx,
566 SYSCTL_CHILDREN(cpu_topology_sysctl_tree),
567 OID_AUTO, "tree", CTLTYPE_STRING | CTLFLAG_RD,
568 NULL, 0, print_cpu_topology_tree_sysctl, "A",
569 "Tree print of CPU topology");
571 /* SYSCTL cpu_topology "level_description" entry */
572 SYSCTL_ADD_PROC(&cpu_topology_sysctl_ctx,
573 SYSCTL_CHILDREN(cpu_topology_sysctl_tree),
574 OID_AUTO, "level_description", CTLTYPE_STRING | CTLFLAG_RD,
575 NULL, 0, print_cpu_topology_level_description_sysctl, "A",
576 "Level description of CPU topology");
578 /* SYSCTL cpu_topology "members" entry */
579 sbuf_new(&sb, cpu_topology_members,
580 sizeof(cpu_topology_members), SBUF_FIXEDLEN);
581 sbuf_print_cpuset(&sb, &cpu_root_node->members);
584 SYSCTL_ADD_STRING(&cpu_topology_sysctl_ctx,
585 SYSCTL_CHILDREN(cpu_topology_sysctl_tree),
586 OID_AUTO, "members", CTLFLAG_RD,
587 cpu_topology_members, 0,
588 "Members of the CPU Topology");
590 /* SYSCTL per_cpu info */
591 for (i = 0; i < ncpus; i++) {
592 /* New leaf : hw.cpu_topology.cpux */
593 sysctl_ctx_init(&pcpu_sysctl[i].sysctl_ctx);
594 pcpu_sysctl[i].sysctl_tree = SYSCTL_ADD_NODE(&pcpu_sysctl[i].sysctl_ctx,
595 SYSCTL_CHILDREN(cpu_topology_sysctl_tree),
597 pcpu_sysctl[i].cpu_name,
600 /* Check if the physical_id found is valid */
601 if (pcpu_sysctl[i].physical_id == INVALID_ID) {
605 /* Add physical id info */
606 SYSCTL_ADD_INT(&pcpu_sysctl[i].sysctl_ctx,
607 SYSCTL_CHILDREN(pcpu_sysctl[i].sysctl_tree),
608 OID_AUTO, "physical_id", CTLFLAG_RD,
609 &pcpu_sysctl[i].physical_id, 0,
612 /* Add physical siblings */
613 SYSCTL_ADD_STRING(&pcpu_sysctl[i].sysctl_ctx,
614 SYSCTL_CHILDREN(pcpu_sysctl[i].sysctl_tree),
615 OID_AUTO, "physical_siblings", CTLFLAG_RD,
616 pcpu_sysctl[i].physical_siblings, 0,
617 "Physical siblings");
619 /* Check if the core_id found is valid */
620 if (pcpu_sysctl[i].core_id == INVALID_ID) {
624 /* Add core id info */
625 SYSCTL_ADD_INT(&pcpu_sysctl[i].sysctl_ctx,
626 SYSCTL_CHILDREN(pcpu_sysctl[i].sysctl_tree),
627 OID_AUTO, "core_id", CTLFLAG_RD,
628 &pcpu_sysctl[i].core_id, 0,
631 /*Add core siblings */
632 SYSCTL_ADD_STRING(&pcpu_sysctl[i].sysctl_ctx,
633 SYSCTL_CHILDREN(pcpu_sysctl[i].sysctl_tree),
634 OID_AUTO, "core_siblings", CTLFLAG_RD,
635 pcpu_sysctl[i].core_siblings, 0,
642 sbuf_print_cpuset(struct sbuf *sb, cpumask_t *mask)
649 sbuf_printf(sb, "cpus(");
650 CPUSET_FOREACH(i, *mask) {
661 sbuf_printf(sb, ", ");
663 sbuf_printf(sb, "%d", b);
665 sbuf_printf(sb, "%d-%d", b, e - 1);
672 sbuf_printf(sb, ", ");
675 sbuf_printf(sb, "%d", b);
677 sbuf_printf(sb, "%d-%d", b, e - 1);
680 sbuf_printf(sb, ") ");
683 /* Build the CPU Topology and SYSCTL Topology tree */
685 init_cpu_topology(void)
687 build_cpu_topology();
689 init_pcpu_topology_sysctl();
690 build_sysctl_cpu_topology();
692 SYSINIT(cpu_topology, SI_BOOT2_CPU_TOPOLOGY, SI_ORDER_FIRST,
693 init_cpu_topology, NULL);