18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci#ifndef _ASM_POWERPC_CPUTHREADS_H
38c2ecf20Sopenharmony_ci#define _ASM_POWERPC_CPUTHREADS_H
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci#ifndef __ASSEMBLY__
68c2ecf20Sopenharmony_ci#include <linux/cpumask.h>
78c2ecf20Sopenharmony_ci#include <asm/cpu_has_feature.h>
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci/*
108c2ecf20Sopenharmony_ci * Mapping of threads to cores
118c2ecf20Sopenharmony_ci *
128c2ecf20Sopenharmony_ci * Note: This implementation is limited to a power of 2 number of
138c2ecf20Sopenharmony_ci * threads per core and the same number for each core in the system
148c2ecf20Sopenharmony_ci * (though it would work if some processors had less threads as long
158c2ecf20Sopenharmony_ci * as the CPU numbers are still allocated, just not brought online).
168c2ecf20Sopenharmony_ci *
178c2ecf20Sopenharmony_ci * However, the API allows for a different implementation in the future
188c2ecf20Sopenharmony_ci * if needed, as long as you only use the functions and not the variables
198c2ecf20Sopenharmony_ci * directly.
208c2ecf20Sopenharmony_ci */
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci#ifdef CONFIG_SMP
238c2ecf20Sopenharmony_ciextern int threads_per_core;
248c2ecf20Sopenharmony_ciextern int threads_per_subcore;
258c2ecf20Sopenharmony_ciextern int threads_shift;
268c2ecf20Sopenharmony_ciextern cpumask_t threads_core_mask;
278c2ecf20Sopenharmony_ci#else
288c2ecf20Sopenharmony_ci#define threads_per_core	1
298c2ecf20Sopenharmony_ci#define threads_per_subcore	1
308c2ecf20Sopenharmony_ci#define threads_shift		0
318c2ecf20Sopenharmony_ci#define has_big_cores		0
328c2ecf20Sopenharmony_ci#define threads_core_mask	(*get_cpu_mask(0))
338c2ecf20Sopenharmony_ci#endif
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci/* cpu_thread_mask_to_cores - Return a cpumask of one per cores
368c2ecf20Sopenharmony_ci *                            hit by the argument
378c2ecf20Sopenharmony_ci *
388c2ecf20Sopenharmony_ci * @threads:	a cpumask of online threads
398c2ecf20Sopenharmony_ci *
408c2ecf20Sopenharmony_ci * This function returns a cpumask which will have one online cpu's
418c2ecf20Sopenharmony_ci * bit set for each core that has at least one thread set in the argument.
428c2ecf20Sopenharmony_ci *
438c2ecf20Sopenharmony_ci * This can typically be used for things like IPI for tlb invalidations
448c2ecf20Sopenharmony_ci * since those need to be done only once per core/TLB
458c2ecf20Sopenharmony_ci */
468c2ecf20Sopenharmony_cistatic inline cpumask_t cpu_thread_mask_to_cores(const struct cpumask *threads)
478c2ecf20Sopenharmony_ci{
488c2ecf20Sopenharmony_ci	cpumask_t	tmp, res;
498c2ecf20Sopenharmony_ci	int		i, cpu;
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci	cpumask_clear(&res);
528c2ecf20Sopenharmony_ci	for (i = 0; i < NR_CPUS; i += threads_per_core) {
538c2ecf20Sopenharmony_ci		cpumask_shift_left(&tmp, &threads_core_mask, i);
548c2ecf20Sopenharmony_ci		if (cpumask_intersects(threads, &tmp)) {
558c2ecf20Sopenharmony_ci			cpu = cpumask_next_and(-1, &tmp, cpu_online_mask);
568c2ecf20Sopenharmony_ci			if (cpu < nr_cpu_ids)
578c2ecf20Sopenharmony_ci				cpumask_set_cpu(cpu, &res);
588c2ecf20Sopenharmony_ci		}
598c2ecf20Sopenharmony_ci	}
608c2ecf20Sopenharmony_ci	return res;
618c2ecf20Sopenharmony_ci}
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_cistatic inline int cpu_nr_cores(void)
648c2ecf20Sopenharmony_ci{
658c2ecf20Sopenharmony_ci	return nr_cpu_ids >> threads_shift;
668c2ecf20Sopenharmony_ci}
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_cistatic inline cpumask_t cpu_online_cores_map(void)
698c2ecf20Sopenharmony_ci{
708c2ecf20Sopenharmony_ci	return cpu_thread_mask_to_cores(cpu_online_mask);
718c2ecf20Sopenharmony_ci}
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci#ifdef CONFIG_SMP
748c2ecf20Sopenharmony_ciint cpu_core_index_of_thread(int cpu);
758c2ecf20Sopenharmony_ciint cpu_first_thread_of_core(int core);
768c2ecf20Sopenharmony_ci#else
778c2ecf20Sopenharmony_cistatic inline int cpu_core_index_of_thread(int cpu) { return cpu; }
788c2ecf20Sopenharmony_cistatic inline int cpu_first_thread_of_core(int core) { return core; }
798c2ecf20Sopenharmony_ci#endif
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_cistatic inline int cpu_thread_in_core(int cpu)
828c2ecf20Sopenharmony_ci{
838c2ecf20Sopenharmony_ci	return cpu & (threads_per_core - 1);
848c2ecf20Sopenharmony_ci}
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_cistatic inline int cpu_thread_in_subcore(int cpu)
878c2ecf20Sopenharmony_ci{
888c2ecf20Sopenharmony_ci	return cpu & (threads_per_subcore - 1);
898c2ecf20Sopenharmony_ci}
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_cistatic inline int cpu_first_thread_sibling(int cpu)
928c2ecf20Sopenharmony_ci{
938c2ecf20Sopenharmony_ci	return cpu & ~(threads_per_core - 1);
948c2ecf20Sopenharmony_ci}
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_cistatic inline int cpu_last_thread_sibling(int cpu)
978c2ecf20Sopenharmony_ci{
988c2ecf20Sopenharmony_ci	return cpu | (threads_per_core - 1);
998c2ecf20Sopenharmony_ci}
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci/*
1028c2ecf20Sopenharmony_ci * tlb_thread_siblings are siblings which share a TLB. This is not
1038c2ecf20Sopenharmony_ci * architected, is not something a hypervisor could emulate and a future
1048c2ecf20Sopenharmony_ci * CPU may change behaviour even in compat mode, so this should only be
1058c2ecf20Sopenharmony_ci * used on PowerNV, and only with care.
1068c2ecf20Sopenharmony_ci */
1078c2ecf20Sopenharmony_cistatic inline int cpu_first_tlb_thread_sibling(int cpu)
1088c2ecf20Sopenharmony_ci{
1098c2ecf20Sopenharmony_ci	if (cpu_has_feature(CPU_FTR_ARCH_300) && (threads_per_core == 8))
1108c2ecf20Sopenharmony_ci		return cpu & ~0x6;	/* Big Core */
1118c2ecf20Sopenharmony_ci	else
1128c2ecf20Sopenharmony_ci		return cpu_first_thread_sibling(cpu);
1138c2ecf20Sopenharmony_ci}
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_cistatic inline int cpu_last_tlb_thread_sibling(int cpu)
1168c2ecf20Sopenharmony_ci{
1178c2ecf20Sopenharmony_ci	if (cpu_has_feature(CPU_FTR_ARCH_300) && (threads_per_core == 8))
1188c2ecf20Sopenharmony_ci		return cpu | 0x6;	/* Big Core */
1198c2ecf20Sopenharmony_ci	else
1208c2ecf20Sopenharmony_ci		return cpu_last_thread_sibling(cpu);
1218c2ecf20Sopenharmony_ci}
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_cistatic inline int cpu_tlb_thread_sibling_step(void)
1248c2ecf20Sopenharmony_ci{
1258c2ecf20Sopenharmony_ci	if (cpu_has_feature(CPU_FTR_ARCH_300) && (threads_per_core == 8))
1268c2ecf20Sopenharmony_ci		return 2;		/* Big Core */
1278c2ecf20Sopenharmony_ci	else
1288c2ecf20Sopenharmony_ci		return 1;
1298c2ecf20Sopenharmony_ci}
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_cistatic inline u32 get_tensr(void)
1328c2ecf20Sopenharmony_ci{
1338c2ecf20Sopenharmony_ci#ifdef	CONFIG_BOOKE
1348c2ecf20Sopenharmony_ci	if (cpu_has_feature(CPU_FTR_SMT))
1358c2ecf20Sopenharmony_ci		return mfspr(SPRN_TENSR);
1368c2ecf20Sopenharmony_ci#endif
1378c2ecf20Sopenharmony_ci	return 1;
1388c2ecf20Sopenharmony_ci}
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_civoid book3e_start_thread(int thread, unsigned long addr);
1418c2ecf20Sopenharmony_civoid book3e_stop_thread(int thread);
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci#endif /* __ASSEMBLY__ */
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci#define INVALID_THREAD_HWID	0x0fff
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci#endif /* _ASM_POWERPC_CPUTHREADS_H */
1488c2ecf20Sopenharmony_ci
149