xref: /dragonfly/sys/sys/cpu_topology.h (revision 631c21f2)
1 /*-
2  * Copyright (c) 2021 The DragonFly Project.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
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
13  *    distribution.
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.
17  *
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
29  * SUCH DAMAGE.
30  */
31 
32 #ifndef _CPU_TOPOLOGY_H_
33 #define _CPU_TOPOLOGY_H_
34 
35 #if defined(_KERNEL) || defined(_KERNEL_STRUCTURES)
36 
37 #include <sys/param.h>
38 #include <sys/cpumask.h>
39 
40 /* CPU TOPOLOGY DATA AND FUNCTIONS */
41 struct cpu_node {
42 	struct cpu_node * parent_node;
43 	struct cpu_node * child_node[MAXCPU];
44 	uint32_t child_no;
45 	uint32_t unused01;
46 	cpumask_t members;
47 	uint8_t type;
48 	uint8_t compute_unit_id; /* AMD compute unit ID */
49 	uint8_t unused02;
50 	uint8_t unused03;
51 	long	phys_mem;	/* supplied from vm_numa_organize() */
52 };
53 typedef struct cpu_node cpu_node_t;
54 
55 #define LEVEL_NO 4
56 
57 /* Level type for CPU siblings */
58 #define	PACKAGE_LEVEL	1
59 #define	CHIP_LEVEL	2
60 #define	CORE_LEVEL	3
61 #define	THREAD_LEVEL	4
62 
63 #define CPUSET_FOREACH(cpu, mask)			\
64 	for ((cpu) = 0; (cpu) < ncpus; (cpu)++)		\
65 		if (CPUMASK_TESTBIT(mask, cpu))
66 
67 #endif
68 
69 #if defined(_KERNEL)
70 
71 extern int cpu_topology_levels_number;
72 extern int cpu_topology_ht_ids;
73 extern int cpu_topology_core_ids;
74 extern int cpu_topology_phys_ids;
75 extern cpu_node_t *root_cpu_node;
76 
77 cpumask_t get_cpumask_from_level(int cpuid, uint8_t level_type);
78 cpu_node_t *get_cpu_node_by_cpuid(int cpuid);
79 const cpu_node_t *get_cpu_node_by_chipid(int chip_id);
80 long get_highest_node_memory(void);
81 int get_cpu_ht_id(int cpuid);
82 int get_cpu_core_id(int cpuid);
83 int get_cpu_phys_id(int cpuid);
84 
85 #endif /* _KERNEL */
86 #endif /* _CPU_TOPOLOGY_H_ */
87