xref: /dflybsd-src/sys/sys/cpu_topology.h (revision 68b1c3c8bbcc7f2e5d05b7973293e4d8fd741cba)
1884bc00cSSascha Wildner /*-
2884bc00cSSascha Wildner  * Copyright (c) 2021 The DragonFly Project.  All rights reserved.
3884bc00cSSascha Wildner  *
4884bc00cSSascha Wildner  * Redistribution and use in source and binary forms, with or without
5884bc00cSSascha Wildner  * modification, are permitted provided that the following conditions
6884bc00cSSascha Wildner  * are met:
7884bc00cSSascha Wildner  *
8884bc00cSSascha Wildner  * 1. Redistributions of source code must retain the above copyright
9884bc00cSSascha Wildner  *    notice, this list of conditions and the following disclaimer.
10884bc00cSSascha Wildner  * 2. Redistributions in binary form must reproduce the above copyright
11884bc00cSSascha Wildner  *    notice, this list of conditions and the following disclaimer in
12884bc00cSSascha Wildner  *    the documentation and/or other materials provided with the
13884bc00cSSascha Wildner  *    distribution.
14884bc00cSSascha Wildner  * 3. Neither the name of The DragonFly Project nor the names of its
15884bc00cSSascha Wildner  *    contributors may be used to endorse or promote products derived
16884bc00cSSascha Wildner  *    from this software without specific, prior written permission.
17884bc00cSSascha Wildner  *
18884bc00cSSascha Wildner  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19884bc00cSSascha Wildner  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20884bc00cSSascha Wildner  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21884bc00cSSascha Wildner  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
22884bc00cSSascha Wildner  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23884bc00cSSascha Wildner  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
24884bc00cSSascha Wildner  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25884bc00cSSascha Wildner  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26884bc00cSSascha Wildner  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27884bc00cSSascha Wildner  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
28884bc00cSSascha Wildner  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29884bc00cSSascha Wildner  * SUCH DAMAGE.
30884bc00cSSascha Wildner  */
31884bc00cSSascha Wildner 
321f2cfeb1SSascha Wildner #ifndef _SYS_CPU_TOPOLOGY_H_
331f2cfeb1SSascha Wildner #define _SYS_CPU_TOPOLOGY_H_
34f77c018aSMihai Carabas 
35c70d4562SMatthew Dillon #if defined(_KERNEL) || defined(_KERNEL_STRUCTURES)
36f77c018aSMihai Carabas 
37884bc00cSSascha Wildner #include <sys/param.h>
38da82a65aSzrj #include <sys/cpumask.h>
39b976e2c2Szrj 
40f77c018aSMihai Carabas /* CPU TOPOLOGY DATA AND FUNCTIONS */
41f77c018aSMihai Carabas struct cpu_node {
42f77c018aSMihai Carabas 	struct cpu_node * parent_node;
430e9325d3SMihai Carabas 	struct cpu_node * child_node[MAXCPU];
44f77c018aSMihai Carabas 	uint32_t child_no;
45c70d4562SMatthew Dillon 	uint32_t unused01;
46f77c018aSMihai Carabas 	cpumask_t members;
47f77c018aSMihai Carabas 	uint8_t type;
480e9325d3SMihai Carabas 	uint8_t compute_unit_id; /* AMD compute unit ID */
49c70d4562SMatthew Dillon 	uint8_t unused02;
50c70d4562SMatthew Dillon 	uint8_t unused03;
51c70d4562SMatthew Dillon 	long	phys_mem;	/* supplied from vm_numa_organize() */
52f77c018aSMihai Carabas };
53f77c018aSMihai Carabas typedef struct cpu_node cpu_node_t;
54f77c018aSMihai Carabas 
55f77c018aSMihai Carabas #define LEVEL_NO 4
56f77c018aSMihai Carabas 
57f77c018aSMihai Carabas /* Level type for CPU siblings */
58f77c018aSMihai Carabas #define	PACKAGE_LEVEL	1
59f77c018aSMihai Carabas #define	CHIP_LEVEL	2
60f77c018aSMihai Carabas #define	CORE_LEVEL	3
61f77c018aSMihai Carabas #define	THREAD_LEVEL	4
62f77c018aSMihai Carabas 
63f77c018aSMihai Carabas #define CPUSET_FOREACH(cpu, mask)			\
64f77c018aSMihai Carabas 	for ((cpu) = 0; (cpu) < ncpus; (cpu)++)		\
65c07315c4SMatthew Dillon 		if (CPUMASK_TESTBIT(mask, cpu))
66f77c018aSMihai Carabas 
67c70d4562SMatthew Dillon #endif
68c70d4562SMatthew Dillon 
69c70d4562SMatthew Dillon #if defined(_KERNEL)
70c70d4562SMatthew Dillon 
71c70d4562SMatthew Dillon extern int cpu_topology_levels_number;
728e5d7c42SMatthew Dillon extern int cpu_topology_ht_ids;
73c70d4562SMatthew Dillon extern int cpu_topology_core_ids;
74c70d4562SMatthew Dillon extern int cpu_topology_phys_ids;
75c70d4562SMatthew Dillon extern cpu_node_t *root_cpu_node;
76c70d4562SMatthew Dillon 
77c70d4562SMatthew Dillon cpumask_t get_cpumask_from_level(int cpuid, uint8_t level_type);
78c70d4562SMatthew Dillon cpu_node_t *get_cpu_node_by_cpuid(int cpuid);
79c70d4562SMatthew Dillon const cpu_node_t *get_cpu_node_by_chipid(int chip_id);
80c70d4562SMatthew Dillon long get_highest_node_memory(void);
818e5d7c42SMatthew Dillon int get_cpu_ht_id(int cpuid);
82c70d4562SMatthew Dillon int get_cpu_core_id(int cpuid);
83c70d4562SMatthew Dillon int get_cpu_phys_id(int cpuid);
84f77c018aSMihai Carabas 
85f77c018aSMihai Carabas #endif /* _KERNEL */
86*68b1c3c8SSascha Wildner #endif /* !_SYS_CPU_TOPOLOGY_H_ */
87