xref: /OK3568_Linux_fs/kernel/arch/powerpc/include/asm/cputhreads.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun #ifndef _ASM_POWERPC_CPUTHREADS_H
3*4882a593Smuzhiyun #define _ASM_POWERPC_CPUTHREADS_H
4*4882a593Smuzhiyun 
5*4882a593Smuzhiyun #ifndef __ASSEMBLY__
6*4882a593Smuzhiyun #include <linux/cpumask.h>
7*4882a593Smuzhiyun #include <asm/cpu_has_feature.h>
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun /*
10*4882a593Smuzhiyun  * Mapping of threads to cores
11*4882a593Smuzhiyun  *
12*4882a593Smuzhiyun  * Note: This implementation is limited to a power of 2 number of
13*4882a593Smuzhiyun  * threads per core and the same number for each core in the system
14*4882a593Smuzhiyun  * (though it would work if some processors had less threads as long
15*4882a593Smuzhiyun  * as the CPU numbers are still allocated, just not brought online).
16*4882a593Smuzhiyun  *
17*4882a593Smuzhiyun  * However, the API allows for a different implementation in the future
18*4882a593Smuzhiyun  * if needed, as long as you only use the functions and not the variables
19*4882a593Smuzhiyun  * directly.
20*4882a593Smuzhiyun  */
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun #ifdef CONFIG_SMP
23*4882a593Smuzhiyun extern int threads_per_core;
24*4882a593Smuzhiyun extern int threads_per_subcore;
25*4882a593Smuzhiyun extern int threads_shift;
26*4882a593Smuzhiyun extern cpumask_t threads_core_mask;
27*4882a593Smuzhiyun #else
28*4882a593Smuzhiyun #define threads_per_core	1
29*4882a593Smuzhiyun #define threads_per_subcore	1
30*4882a593Smuzhiyun #define threads_shift		0
31*4882a593Smuzhiyun #define has_big_cores		0
32*4882a593Smuzhiyun #define threads_core_mask	(*get_cpu_mask(0))
33*4882a593Smuzhiyun #endif
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun /* cpu_thread_mask_to_cores - Return a cpumask of one per cores
36*4882a593Smuzhiyun  *                            hit by the argument
37*4882a593Smuzhiyun  *
38*4882a593Smuzhiyun  * @threads:	a cpumask of online threads
39*4882a593Smuzhiyun  *
40*4882a593Smuzhiyun  * This function returns a cpumask which will have one online cpu's
41*4882a593Smuzhiyun  * bit set for each core that has at least one thread set in the argument.
42*4882a593Smuzhiyun  *
43*4882a593Smuzhiyun  * This can typically be used for things like IPI for tlb invalidations
44*4882a593Smuzhiyun  * since those need to be done only once per core/TLB
45*4882a593Smuzhiyun  */
cpu_thread_mask_to_cores(const struct cpumask * threads)46*4882a593Smuzhiyun static inline cpumask_t cpu_thread_mask_to_cores(const struct cpumask *threads)
47*4882a593Smuzhiyun {
48*4882a593Smuzhiyun 	cpumask_t	tmp, res;
49*4882a593Smuzhiyun 	int		i, cpu;
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun 	cpumask_clear(&res);
52*4882a593Smuzhiyun 	for (i = 0; i < NR_CPUS; i += threads_per_core) {
53*4882a593Smuzhiyun 		cpumask_shift_left(&tmp, &threads_core_mask, i);
54*4882a593Smuzhiyun 		if (cpumask_intersects(threads, &tmp)) {
55*4882a593Smuzhiyun 			cpu = cpumask_next_and(-1, &tmp, cpu_online_mask);
56*4882a593Smuzhiyun 			if (cpu < nr_cpu_ids)
57*4882a593Smuzhiyun 				cpumask_set_cpu(cpu, &res);
58*4882a593Smuzhiyun 		}
59*4882a593Smuzhiyun 	}
60*4882a593Smuzhiyun 	return res;
61*4882a593Smuzhiyun }
62*4882a593Smuzhiyun 
cpu_nr_cores(void)63*4882a593Smuzhiyun static inline int cpu_nr_cores(void)
64*4882a593Smuzhiyun {
65*4882a593Smuzhiyun 	return nr_cpu_ids >> threads_shift;
66*4882a593Smuzhiyun }
67*4882a593Smuzhiyun 
cpu_online_cores_map(void)68*4882a593Smuzhiyun static inline cpumask_t cpu_online_cores_map(void)
69*4882a593Smuzhiyun {
70*4882a593Smuzhiyun 	return cpu_thread_mask_to_cores(cpu_online_mask);
71*4882a593Smuzhiyun }
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun #ifdef CONFIG_SMP
74*4882a593Smuzhiyun int cpu_core_index_of_thread(int cpu);
75*4882a593Smuzhiyun int cpu_first_thread_of_core(int core);
76*4882a593Smuzhiyun #else
cpu_core_index_of_thread(int cpu)77*4882a593Smuzhiyun static inline int cpu_core_index_of_thread(int cpu) { return cpu; }
cpu_first_thread_of_core(int core)78*4882a593Smuzhiyun static inline int cpu_first_thread_of_core(int core) { return core; }
79*4882a593Smuzhiyun #endif
80*4882a593Smuzhiyun 
cpu_thread_in_core(int cpu)81*4882a593Smuzhiyun static inline int cpu_thread_in_core(int cpu)
82*4882a593Smuzhiyun {
83*4882a593Smuzhiyun 	return cpu & (threads_per_core - 1);
84*4882a593Smuzhiyun }
85*4882a593Smuzhiyun 
cpu_thread_in_subcore(int cpu)86*4882a593Smuzhiyun static inline int cpu_thread_in_subcore(int cpu)
87*4882a593Smuzhiyun {
88*4882a593Smuzhiyun 	return cpu & (threads_per_subcore - 1);
89*4882a593Smuzhiyun }
90*4882a593Smuzhiyun 
cpu_first_thread_sibling(int cpu)91*4882a593Smuzhiyun static inline int cpu_first_thread_sibling(int cpu)
92*4882a593Smuzhiyun {
93*4882a593Smuzhiyun 	return cpu & ~(threads_per_core - 1);
94*4882a593Smuzhiyun }
95*4882a593Smuzhiyun 
cpu_last_thread_sibling(int cpu)96*4882a593Smuzhiyun static inline int cpu_last_thread_sibling(int cpu)
97*4882a593Smuzhiyun {
98*4882a593Smuzhiyun 	return cpu | (threads_per_core - 1);
99*4882a593Smuzhiyun }
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun /*
102*4882a593Smuzhiyun  * tlb_thread_siblings are siblings which share a TLB. This is not
103*4882a593Smuzhiyun  * architected, is not something a hypervisor could emulate and a future
104*4882a593Smuzhiyun  * CPU may change behaviour even in compat mode, so this should only be
105*4882a593Smuzhiyun  * used on PowerNV, and only with care.
106*4882a593Smuzhiyun  */
cpu_first_tlb_thread_sibling(int cpu)107*4882a593Smuzhiyun static inline int cpu_first_tlb_thread_sibling(int cpu)
108*4882a593Smuzhiyun {
109*4882a593Smuzhiyun 	if (cpu_has_feature(CPU_FTR_ARCH_300) && (threads_per_core == 8))
110*4882a593Smuzhiyun 		return cpu & ~0x6;	/* Big Core */
111*4882a593Smuzhiyun 	else
112*4882a593Smuzhiyun 		return cpu_first_thread_sibling(cpu);
113*4882a593Smuzhiyun }
114*4882a593Smuzhiyun 
cpu_last_tlb_thread_sibling(int cpu)115*4882a593Smuzhiyun static inline int cpu_last_tlb_thread_sibling(int cpu)
116*4882a593Smuzhiyun {
117*4882a593Smuzhiyun 	if (cpu_has_feature(CPU_FTR_ARCH_300) && (threads_per_core == 8))
118*4882a593Smuzhiyun 		return cpu | 0x6;	/* Big Core */
119*4882a593Smuzhiyun 	else
120*4882a593Smuzhiyun 		return cpu_last_thread_sibling(cpu);
121*4882a593Smuzhiyun }
122*4882a593Smuzhiyun 
cpu_tlb_thread_sibling_step(void)123*4882a593Smuzhiyun static inline int cpu_tlb_thread_sibling_step(void)
124*4882a593Smuzhiyun {
125*4882a593Smuzhiyun 	if (cpu_has_feature(CPU_FTR_ARCH_300) && (threads_per_core == 8))
126*4882a593Smuzhiyun 		return 2;		/* Big Core */
127*4882a593Smuzhiyun 	else
128*4882a593Smuzhiyun 		return 1;
129*4882a593Smuzhiyun }
130*4882a593Smuzhiyun 
get_tensr(void)131*4882a593Smuzhiyun static inline u32 get_tensr(void)
132*4882a593Smuzhiyun {
133*4882a593Smuzhiyun #ifdef	CONFIG_BOOKE
134*4882a593Smuzhiyun 	if (cpu_has_feature(CPU_FTR_SMT))
135*4882a593Smuzhiyun 		return mfspr(SPRN_TENSR);
136*4882a593Smuzhiyun #endif
137*4882a593Smuzhiyun 	return 1;
138*4882a593Smuzhiyun }
139*4882a593Smuzhiyun 
140*4882a593Smuzhiyun void book3e_start_thread(int thread, unsigned long addr);
141*4882a593Smuzhiyun void book3e_stop_thread(int thread);
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun #endif /* __ASSEMBLY__ */
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun #define INVALID_THREAD_HWID	0x0fff
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun #endif /* _ASM_POWERPC_CPUTHREADS_H */
148*4882a593Smuzhiyun 
149