xref: /openbmc/linux/arch/powerpc/include/asm/cputhreads.h (revision 5853aef1ac5c5d83076203e840ca463857a7515d)
1b8b572e1SStephen Rothwell #ifndef _ASM_POWERPC_CPUTHREADS_H
2b8b572e1SStephen Rothwell #define _ASM_POWERPC_CPUTHREADS_H
3b8b572e1SStephen Rothwell 
4b8b572e1SStephen Rothwell #include <linux/cpumask.h>
5b8b572e1SStephen Rothwell 
6b8b572e1SStephen Rothwell /*
7b8b572e1SStephen Rothwell  * Mapping of threads to cores
8fcce8109SBenjamin Herrenschmidt  *
9fcce8109SBenjamin Herrenschmidt  * Note: This implementation is limited to a power of 2 number of
10fcce8109SBenjamin Herrenschmidt  * threads per core and the same number for each core in the system
11fcce8109SBenjamin Herrenschmidt  * (though it would work if some processors had less threads as long
12933b90a9SAnshuman Khandual  * as the CPU numbers are still allocated, just not brought online).
13fcce8109SBenjamin Herrenschmidt  *
14fcce8109SBenjamin Herrenschmidt  * However, the API allows for a different implementation in the future
15fcce8109SBenjamin Herrenschmidt  * if needed, as long as you only use the functions and not the variables
16fcce8109SBenjamin Herrenschmidt  * directly.
17b8b572e1SStephen Rothwell  */
18b8b572e1SStephen Rothwell 
19b8b572e1SStephen Rothwell #ifdef CONFIG_SMP
20b8b572e1SStephen Rothwell extern int threads_per_core;
21*5853aef1SMichael Ellerman extern int threads_per_subcore;
22b8b572e1SStephen Rothwell extern int threads_shift;
23b8b572e1SStephen Rothwell extern cpumask_t threads_core_mask;
24b8b572e1SStephen Rothwell #else
25b8b572e1SStephen Rothwell #define threads_per_core	1
26*5853aef1SMichael Ellerman #define threads_per_subcore	1
27b8b572e1SStephen Rothwell #define threads_shift		0
28b8b572e1SStephen Rothwell #define threads_core_mask	(CPU_MASK_CPU0)
29b8b572e1SStephen Rothwell #endif
30b8b572e1SStephen Rothwell 
31b8b572e1SStephen Rothwell /* cpu_thread_mask_to_cores - Return a cpumask of one per cores
32b8b572e1SStephen Rothwell  *                            hit by the argument
33b8b572e1SStephen Rothwell  *
34b8b572e1SStephen Rothwell  * @threads:	a cpumask of threads
35b8b572e1SStephen Rothwell  *
36b8b572e1SStephen Rothwell  * This function returns a cpumask which will have one "cpu" (or thread)
37b8b572e1SStephen Rothwell  * bit set for each core that has at least one thread set in the argument.
38b8b572e1SStephen Rothwell  *
39b8b572e1SStephen Rothwell  * This can typically be used for things like IPI for tlb invalidations
40b8b572e1SStephen Rothwell  * since those need to be done only once per core/TLB
41b8b572e1SStephen Rothwell  */
42104699c0SKOSAKI Motohiro static inline cpumask_t cpu_thread_mask_to_cores(const struct cpumask *threads)
43b8b572e1SStephen Rothwell {
44b8b572e1SStephen Rothwell 	cpumask_t	tmp, res;
45b8b572e1SStephen Rothwell 	int		i;
46b8b572e1SStephen Rothwell 
47104699c0SKOSAKI Motohiro 	cpumask_clear(&res);
48b8b572e1SStephen Rothwell 	for (i = 0; i < NR_CPUS; i += threads_per_core) {
49104699c0SKOSAKI Motohiro 		cpumask_shift_left(&tmp, &threads_core_mask, i);
50104699c0SKOSAKI Motohiro 		if (cpumask_intersects(threads, &tmp))
51104699c0SKOSAKI Motohiro 			cpumask_set_cpu(i, &res);
52b8b572e1SStephen Rothwell 	}
53b8b572e1SStephen Rothwell 	return res;
54b8b572e1SStephen Rothwell }
55b8b572e1SStephen Rothwell 
56b8b572e1SStephen Rothwell static inline int cpu_nr_cores(void)
57b8b572e1SStephen Rothwell {
58b8b572e1SStephen Rothwell 	return NR_CPUS >> threads_shift;
59b8b572e1SStephen Rothwell }
60b8b572e1SStephen Rothwell 
61b8b572e1SStephen Rothwell static inline cpumask_t cpu_online_cores_map(void)
62b8b572e1SStephen Rothwell {
63104699c0SKOSAKI Motohiro 	return cpu_thread_mask_to_cores(cpu_online_mask);
64b8b572e1SStephen Rothwell }
65b8b572e1SStephen Rothwell 
6699d86705SVaidyanathan Srinivasan #ifdef CONFIG_SMP
6799d86705SVaidyanathan Srinivasan int cpu_core_index_of_thread(int cpu);
6899d86705SVaidyanathan Srinivasan int cpu_first_thread_of_core(int core);
6999d86705SVaidyanathan Srinivasan #else
7099d86705SVaidyanathan Srinivasan static inline int cpu_core_index_of_thread(int cpu) { return cpu; }
7199d86705SVaidyanathan Srinivasan static inline int cpu_first_thread_of_core(int core) { return core; }
7299d86705SVaidyanathan Srinivasan #endif
73b8b572e1SStephen Rothwell 
74b8b572e1SStephen Rothwell static inline int cpu_thread_in_core(int cpu)
75b8b572e1SStephen Rothwell {
76b8b572e1SStephen Rothwell 	return cpu & (threads_per_core - 1);
77b8b572e1SStephen Rothwell }
78b8b572e1SStephen Rothwell 
79*5853aef1SMichael Ellerman static inline int cpu_thread_in_subcore(int cpu)
80*5853aef1SMichael Ellerman {
81*5853aef1SMichael Ellerman 	return cpu & (threads_per_subcore - 1);
82*5853aef1SMichael Ellerman }
83*5853aef1SMichael Ellerman 
8499d86705SVaidyanathan Srinivasan static inline int cpu_first_thread_sibling(int cpu)
85b8b572e1SStephen Rothwell {
86b8b572e1SStephen Rothwell 	return cpu & ~(threads_per_core - 1);
87b8b572e1SStephen Rothwell }
88b8b572e1SStephen Rothwell 
8999d86705SVaidyanathan Srinivasan static inline int cpu_last_thread_sibling(int cpu)
90fcce8109SBenjamin Herrenschmidt {
91fcce8109SBenjamin Herrenschmidt 	return cpu | (threads_per_core - 1);
92fcce8109SBenjamin Herrenschmidt }
93fcce8109SBenjamin Herrenschmidt 
94fcce8109SBenjamin Herrenschmidt 
95fcce8109SBenjamin Herrenschmidt 
96b8b572e1SStephen Rothwell #endif /* _ASM_POWERPC_CPUTHREADS_H */
97b8b572e1SStephen Rothwell 
98