xref: /openbmc/linux/arch/powerpc/include/asm/cputhreads.h (revision 425752c63b6f3fed7b5a9cba2b8101a92cf36995)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
2b8b572e1SStephen Rothwell #ifndef _ASM_POWERPC_CPUTHREADS_H
3b8b572e1SStephen Rothwell #define _ASM_POWERPC_CPUTHREADS_H
4b8b572e1SStephen Rothwell 
56becef7eSchenhui zhao #ifndef __ASSEMBLY__
6b8b572e1SStephen Rothwell #include <linux/cpumask.h>
7e340eca9SGuenter Roeck #include <asm/cpu_has_feature.h>
8b8b572e1SStephen Rothwell 
9b8b572e1SStephen Rothwell /*
10b8b572e1SStephen Rothwell  * Mapping of threads to cores
11fcce8109SBenjamin Herrenschmidt  *
12fcce8109SBenjamin Herrenschmidt  * Note: This implementation is limited to a power of 2 number of
13fcce8109SBenjamin Herrenschmidt  * threads per core and the same number for each core in the system
14fcce8109SBenjamin Herrenschmidt  * (though it would work if some processors had less threads as long
15933b90a9SAnshuman Khandual  * as the CPU numbers are still allocated, just not brought online).
16fcce8109SBenjamin Herrenschmidt  *
17fcce8109SBenjamin Herrenschmidt  * However, the API allows for a different implementation in the future
18fcce8109SBenjamin Herrenschmidt  * if needed, as long as you only use the functions and not the variables
19fcce8109SBenjamin Herrenschmidt  * directly.
20b8b572e1SStephen Rothwell  */
21b8b572e1SStephen Rothwell 
22b8b572e1SStephen Rothwell #ifdef CONFIG_SMP
23b8b572e1SStephen Rothwell extern int threads_per_core;
245853aef1SMichael Ellerman extern int threads_per_subcore;
25b8b572e1SStephen Rothwell extern int threads_shift;
26*425752c6SGautham R. Shenoy extern bool has_big_cores;
27b8b572e1SStephen Rothwell extern cpumask_t threads_core_mask;
28b8b572e1SStephen Rothwell #else
29b8b572e1SStephen Rothwell #define threads_per_core	1
305853aef1SMichael Ellerman #define threads_per_subcore	1
31b8b572e1SStephen Rothwell #define threads_shift		0
32*425752c6SGautham R. Shenoy #define has_big_cores		0
3387313df7SRusty Russell #define threads_core_mask	(*get_cpu_mask(0))
34b8b572e1SStephen Rothwell #endif
35b8b572e1SStephen Rothwell 
36b8b572e1SStephen Rothwell /* cpu_thread_mask_to_cores - Return a cpumask of one per cores
37b8b572e1SStephen Rothwell  *                            hit by the argument
38b8b572e1SStephen Rothwell  *
39e602ffb2SShreyas B. Prabhu  * @threads:	a cpumask of online threads
40b8b572e1SStephen Rothwell  *
41e602ffb2SShreyas B. Prabhu  * This function returns a cpumask which will have one online cpu's
42b8b572e1SStephen Rothwell  * bit set for each core that has at least one thread set in the argument.
43b8b572e1SStephen Rothwell  *
44b8b572e1SStephen Rothwell  * This can typically be used for things like IPI for tlb invalidations
45b8b572e1SStephen Rothwell  * since those need to be done only once per core/TLB
46b8b572e1SStephen Rothwell  */
47104699c0SKOSAKI Motohiro static inline cpumask_t cpu_thread_mask_to_cores(const struct cpumask *threads)
48b8b572e1SStephen Rothwell {
49b8b572e1SStephen Rothwell 	cpumask_t	tmp, res;
50e602ffb2SShreyas B. Prabhu 	int		i, cpu;
51b8b572e1SStephen Rothwell 
52104699c0SKOSAKI Motohiro 	cpumask_clear(&res);
53b8b572e1SStephen Rothwell 	for (i = 0; i < NR_CPUS; i += threads_per_core) {
54104699c0SKOSAKI Motohiro 		cpumask_shift_left(&tmp, &threads_core_mask, i);
55e602ffb2SShreyas B. Prabhu 		if (cpumask_intersects(threads, &tmp)) {
56e602ffb2SShreyas B. Prabhu 			cpu = cpumask_next_and(-1, &tmp, cpu_online_mask);
57e602ffb2SShreyas B. Prabhu 			if (cpu < nr_cpu_ids)
58e602ffb2SShreyas B. Prabhu 				cpumask_set_cpu(cpu, &res);
59e602ffb2SShreyas B. Prabhu 		}
60b8b572e1SStephen Rothwell 	}
61b8b572e1SStephen Rothwell 	return res;
62b8b572e1SStephen Rothwell }
63b8b572e1SStephen Rothwell 
64b8b572e1SStephen Rothwell static inline int cpu_nr_cores(void)
65b8b572e1SStephen Rothwell {
66d52356e7SJan Stancek 	return nr_cpu_ids >> threads_shift;
67b8b572e1SStephen Rothwell }
68b8b572e1SStephen Rothwell 
69b8b572e1SStephen Rothwell static inline cpumask_t cpu_online_cores_map(void)
70b8b572e1SStephen Rothwell {
71104699c0SKOSAKI Motohiro 	return cpu_thread_mask_to_cores(cpu_online_mask);
72b8b572e1SStephen Rothwell }
73b8b572e1SStephen Rothwell 
7499d86705SVaidyanathan Srinivasan #ifdef CONFIG_SMP
7599d86705SVaidyanathan Srinivasan int cpu_core_index_of_thread(int cpu);
7699d86705SVaidyanathan Srinivasan int cpu_first_thread_of_core(int core);
7799d86705SVaidyanathan Srinivasan #else
7899d86705SVaidyanathan Srinivasan static inline int cpu_core_index_of_thread(int cpu) { return cpu; }
7999d86705SVaidyanathan Srinivasan static inline int cpu_first_thread_of_core(int core) { return core; }
8099d86705SVaidyanathan Srinivasan #endif
81b8b572e1SStephen Rothwell 
82b8b572e1SStephen Rothwell static inline int cpu_thread_in_core(int cpu)
83b8b572e1SStephen Rothwell {
84b8b572e1SStephen Rothwell 	return cpu & (threads_per_core - 1);
85b8b572e1SStephen Rothwell }
86b8b572e1SStephen Rothwell 
875853aef1SMichael Ellerman static inline int cpu_thread_in_subcore(int cpu)
885853aef1SMichael Ellerman {
895853aef1SMichael Ellerman 	return cpu & (threads_per_subcore - 1);
905853aef1SMichael Ellerman }
915853aef1SMichael Ellerman 
9299d86705SVaidyanathan Srinivasan static inline int cpu_first_thread_sibling(int cpu)
93b8b572e1SStephen Rothwell {
94b8b572e1SStephen Rothwell 	return cpu & ~(threads_per_core - 1);
95b8b572e1SStephen Rothwell }
96b8b572e1SStephen Rothwell 
9799d86705SVaidyanathan Srinivasan static inline int cpu_last_thread_sibling(int cpu)
98fcce8109SBenjamin Herrenschmidt {
99fcce8109SBenjamin Herrenschmidt 	return cpu | (threads_per_core - 1);
100fcce8109SBenjamin Herrenschmidt }
101fcce8109SBenjamin Herrenschmidt 
102ebb9d30aSchenhui zhao static inline u32 get_tensr(void)
103ebb9d30aSchenhui zhao {
104ebb9d30aSchenhui zhao #ifdef	CONFIG_BOOKE
105ebb9d30aSchenhui zhao 	if (cpu_has_feature(CPU_FTR_SMT))
106ebb9d30aSchenhui zhao 		return mfspr(SPRN_TENSR);
107ebb9d30aSchenhui zhao #endif
108ebb9d30aSchenhui zhao 	return 1;
109ebb9d30aSchenhui zhao }
110fcce8109SBenjamin Herrenschmidt 
1116becef7eSchenhui zhao void book3e_start_thread(int thread, unsigned long addr);
112d17799f9Schenhui zhao void book3e_stop_thread(int thread);
113fcce8109SBenjamin Herrenschmidt 
1146becef7eSchenhui zhao #endif /* __ASSEMBLY__ */
1156becef7eSchenhui zhao 
1166becef7eSchenhui zhao #define INVALID_THREAD_HWID	0x0fff
1176becef7eSchenhui zhao 
118b8b572e1SStephen Rothwell #endif /* _ASM_POWERPC_CPUTHREADS_H */
119b8b572e1SStephen Rothwell 
120