xref: /openbmc/linux/arch/powerpc/include/asm/cputhreads.h (revision 77bbbc0cf84834ed130838f7ac1988567f4d0288)
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;
26b8b572e1SStephen Rothwell extern cpumask_t threads_core_mask;
27b8b572e1SStephen Rothwell #else
28b8b572e1SStephen Rothwell #define threads_per_core	1
295853aef1SMichael Ellerman #define threads_per_subcore	1
30b8b572e1SStephen Rothwell #define threads_shift		0
31425752c6SGautham R. Shenoy #define has_big_cores		0
3287313df7SRusty Russell #define threads_core_mask	(*get_cpu_mask(0))
33b8b572e1SStephen Rothwell #endif
34b8b572e1SStephen Rothwell 
35b8b572e1SStephen Rothwell /* cpu_thread_mask_to_cores - Return a cpumask of one per cores
36b8b572e1SStephen Rothwell  *                            hit by the argument
37b8b572e1SStephen Rothwell  *
38e602ffb2SShreyas B. Prabhu  * @threads:	a cpumask of online threads
39b8b572e1SStephen Rothwell  *
40e602ffb2SShreyas B. Prabhu  * This function returns a cpumask which will have one online cpu's
41b8b572e1SStephen Rothwell  * bit set for each core that has at least one thread set in the argument.
42b8b572e1SStephen Rothwell  *
43b8b572e1SStephen Rothwell  * This can typically be used for things like IPI for tlb invalidations
44b8b572e1SStephen Rothwell  * since those need to be done only once per core/TLB
45b8b572e1SStephen Rothwell  */
46104699c0SKOSAKI Motohiro static inline cpumask_t cpu_thread_mask_to_cores(const struct cpumask *threads)
47b8b572e1SStephen Rothwell {
48b8b572e1SStephen Rothwell 	cpumask_t	tmp, res;
49e602ffb2SShreyas B. Prabhu 	int		i, cpu;
50b8b572e1SStephen Rothwell 
51104699c0SKOSAKI Motohiro 	cpumask_clear(&res);
52b8b572e1SStephen Rothwell 	for (i = 0; i < NR_CPUS; i += threads_per_core) {
53104699c0SKOSAKI Motohiro 		cpumask_shift_left(&tmp, &threads_core_mask, i);
54e602ffb2SShreyas B. Prabhu 		if (cpumask_intersects(threads, &tmp)) {
55e602ffb2SShreyas B. Prabhu 			cpu = cpumask_next_and(-1, &tmp, cpu_online_mask);
56e602ffb2SShreyas B. Prabhu 			if (cpu < nr_cpu_ids)
57e602ffb2SShreyas B. Prabhu 				cpumask_set_cpu(cpu, &res);
58e602ffb2SShreyas B. Prabhu 		}
59b8b572e1SStephen Rothwell 	}
60b8b572e1SStephen Rothwell 	return res;
61b8b572e1SStephen Rothwell }
62b8b572e1SStephen Rothwell 
63b8b572e1SStephen Rothwell static inline int cpu_nr_cores(void)
64b8b572e1SStephen Rothwell {
65d52356e7SJan Stancek 	return nr_cpu_ids >> threads_shift;
66b8b572e1SStephen Rothwell }
67b8b572e1SStephen Rothwell 
68b8b572e1SStephen Rothwell static inline cpumask_t cpu_online_cores_map(void)
69b8b572e1SStephen Rothwell {
70104699c0SKOSAKI Motohiro 	return cpu_thread_mask_to_cores(cpu_online_mask);
71b8b572e1SStephen Rothwell }
72b8b572e1SStephen Rothwell 
7399d86705SVaidyanathan Srinivasan #ifdef CONFIG_SMP
7499d86705SVaidyanathan Srinivasan int cpu_core_index_of_thread(int cpu);
7599d86705SVaidyanathan Srinivasan int cpu_first_thread_of_core(int core);
7699d86705SVaidyanathan Srinivasan #else
7799d86705SVaidyanathan Srinivasan static inline int cpu_core_index_of_thread(int cpu) { return cpu; }
7899d86705SVaidyanathan Srinivasan static inline int cpu_first_thread_of_core(int core) { return core; }
7999d86705SVaidyanathan Srinivasan #endif
80b8b572e1SStephen Rothwell 
81b8b572e1SStephen Rothwell static inline int cpu_thread_in_core(int cpu)
82b8b572e1SStephen Rothwell {
83b8b572e1SStephen Rothwell 	return cpu & (threads_per_core - 1);
84b8b572e1SStephen Rothwell }
85b8b572e1SStephen Rothwell 
865853aef1SMichael Ellerman static inline int cpu_thread_in_subcore(int cpu)
875853aef1SMichael Ellerman {
885853aef1SMichael Ellerman 	return cpu & (threads_per_subcore - 1);
895853aef1SMichael Ellerman }
905853aef1SMichael Ellerman 
9199d86705SVaidyanathan Srinivasan static inline int cpu_first_thread_sibling(int cpu)
92b8b572e1SStephen Rothwell {
93b8b572e1SStephen Rothwell 	return cpu & ~(threads_per_core - 1);
94b8b572e1SStephen Rothwell }
95b8b572e1SStephen Rothwell 
9699d86705SVaidyanathan Srinivasan static inline int cpu_last_thread_sibling(int cpu)
97fcce8109SBenjamin Herrenschmidt {
98fcce8109SBenjamin Herrenschmidt 	return cpu | (threads_per_core - 1);
99fcce8109SBenjamin Herrenschmidt }
100fcce8109SBenjamin Herrenschmidt 
101*77bbbc0cSSuraj Jitindar Singh /*
102*77bbbc0cSSuraj Jitindar Singh  * tlb_thread_siblings are siblings which share a TLB. This is not
103*77bbbc0cSSuraj Jitindar Singh  * architected, is not something a hypervisor could emulate and a future
104*77bbbc0cSSuraj Jitindar Singh  * CPU may change behaviour even in compat mode, so this should only be
105*77bbbc0cSSuraj Jitindar Singh  * used on PowerNV, and only with care.
106*77bbbc0cSSuraj Jitindar Singh  */
107*77bbbc0cSSuraj Jitindar Singh static inline int cpu_first_tlb_thread_sibling(int cpu)
108*77bbbc0cSSuraj Jitindar Singh {
109*77bbbc0cSSuraj Jitindar Singh 	if (cpu_has_feature(CPU_FTR_ARCH_300) && (threads_per_core == 8))
110*77bbbc0cSSuraj Jitindar Singh 		return cpu & ~0x6;	/* Big Core */
111*77bbbc0cSSuraj Jitindar Singh 	else
112*77bbbc0cSSuraj Jitindar Singh 		return cpu_first_thread_sibling(cpu);
113*77bbbc0cSSuraj Jitindar Singh }
114*77bbbc0cSSuraj Jitindar Singh 
115*77bbbc0cSSuraj Jitindar Singh static inline int cpu_last_tlb_thread_sibling(int cpu)
116*77bbbc0cSSuraj Jitindar Singh {
117*77bbbc0cSSuraj Jitindar Singh 	if (cpu_has_feature(CPU_FTR_ARCH_300) && (threads_per_core == 8))
118*77bbbc0cSSuraj Jitindar Singh 		return cpu | 0x6;	/* Big Core */
119*77bbbc0cSSuraj Jitindar Singh 	else
120*77bbbc0cSSuraj Jitindar Singh 		return cpu_last_thread_sibling(cpu);
121*77bbbc0cSSuraj Jitindar Singh }
122*77bbbc0cSSuraj Jitindar Singh 
123*77bbbc0cSSuraj Jitindar Singh static inline int cpu_tlb_thread_sibling_step(void)
124*77bbbc0cSSuraj Jitindar Singh {
125*77bbbc0cSSuraj Jitindar Singh 	if (cpu_has_feature(CPU_FTR_ARCH_300) && (threads_per_core == 8))
126*77bbbc0cSSuraj Jitindar Singh 		return 2;		/* Big Core */
127*77bbbc0cSSuraj Jitindar Singh 	else
128*77bbbc0cSSuraj Jitindar Singh 		return 1;
129*77bbbc0cSSuraj Jitindar Singh }
130*77bbbc0cSSuraj Jitindar Singh 
131ebb9d30aSchenhui zhao static inline u32 get_tensr(void)
132ebb9d30aSchenhui zhao {
133ebb9d30aSchenhui zhao #ifdef	CONFIG_BOOKE
134ebb9d30aSchenhui zhao 	if (cpu_has_feature(CPU_FTR_SMT))
135ebb9d30aSchenhui zhao 		return mfspr(SPRN_TENSR);
136ebb9d30aSchenhui zhao #endif
137ebb9d30aSchenhui zhao 	return 1;
138ebb9d30aSchenhui zhao }
139fcce8109SBenjamin Herrenschmidt 
1406becef7eSchenhui zhao void book3e_start_thread(int thread, unsigned long addr);
141d17799f9Schenhui zhao void book3e_stop_thread(int thread);
142fcce8109SBenjamin Herrenschmidt 
1436becef7eSchenhui zhao #endif /* __ASSEMBLY__ */
1446becef7eSchenhui zhao 
1456becef7eSchenhui zhao #define INVALID_THREAD_HWID	0x0fff
1466becef7eSchenhui zhao 
147b8b572e1SStephen Rothwell #endif /* _ASM_POWERPC_CPUTHREADS_H */
148b8b572e1SStephen Rothwell 
149