blob: 9377bdf42eb8aafa983b038afc15a2e96bf33873 [file] [log] [blame]
Benjamin Herrenschmidt8d089082007-10-25 15:27:44 +10001#ifndef _ASM_POWERPC_CPUTHREADS_H
2#define _ASM_POWERPC_CPUTHREADS_H
3
chenhui zhao6becef72015-11-20 17:14:02 +08004#ifndef __ASSEMBLY__
Benjamin Herrenschmidt8d089082007-10-25 15:27:44 +10005#include <linux/cpumask.h>
Guenter Roecke340eca2016-08-14 13:53:17 -07006#include <asm/cpu_has_feature.h>
Benjamin Herrenschmidt8d089082007-10-25 15:27:44 +10007
8/*
9 * Mapping of threads to cores
Benjamin Herrenschmidtfcce8102009-07-23 23:15:10 +000010 *
11 * Note: This implementation is limited to a power of 2 number of
12 * threads per core and the same number for each core in the system
13 * (though it would work if some processors had less threads as long
Anshuman Khandual933b90a2012-05-14 05:32:00 +000014 * as the CPU numbers are still allocated, just not brought online).
Benjamin Herrenschmidtfcce8102009-07-23 23:15:10 +000015 *
16 * However, the API allows for a different implementation in the future
17 * if needed, as long as you only use the functions and not the variables
18 * directly.
Benjamin Herrenschmidt8d089082007-10-25 15:27:44 +100019 */
20
21#ifdef CONFIG_SMP
22extern int threads_per_core;
Michael Ellerman5853aef2014-05-23 18:15:27 +100023extern int threads_per_subcore;
Benjamin Herrenschmidt8d089082007-10-25 15:27:44 +100024extern int threads_shift;
25extern cpumask_t threads_core_mask;
26#else
27#define threads_per_core 1
Michael Ellerman5853aef2014-05-23 18:15:27 +100028#define threads_per_subcore 1
Benjamin Herrenschmidt8d089082007-10-25 15:27:44 +100029#define threads_shift 0
Rusty Russell87313df2015-03-10 12:28:29 +103030#define threads_core_mask (*get_cpu_mask(0))
Benjamin Herrenschmidt8d089082007-10-25 15:27:44 +100031#endif
32
33/* cpu_thread_mask_to_cores - Return a cpumask of one per cores
34 * hit by the argument
35 *
Shreyas B. Prabhue602ffb2015-04-20 10:32:56 +053036 * @threads: a cpumask of online threads
Benjamin Herrenschmidt8d089082007-10-25 15:27:44 +100037 *
Shreyas B. Prabhue602ffb2015-04-20 10:32:56 +053038 * This function returns a cpumask which will have one online cpu's
Benjamin Herrenschmidt8d089082007-10-25 15:27:44 +100039 * bit set for each core that has at least one thread set in the argument.
40 *
41 * This can typically be used for things like IPI for tlb invalidations
42 * since those need to be done only once per core/TLB
43 */
KOSAKI Motohiro104699c2011-04-28 05:07:23 +000044static inline cpumask_t cpu_thread_mask_to_cores(const struct cpumask *threads)
Benjamin Herrenschmidt8d089082007-10-25 15:27:44 +100045{
46 cpumask_t tmp, res;
Shreyas B. Prabhue602ffb2015-04-20 10:32:56 +053047 int i, cpu;
Benjamin Herrenschmidt8d089082007-10-25 15:27:44 +100048
KOSAKI Motohiro104699c2011-04-28 05:07:23 +000049 cpumask_clear(&res);
Benjamin Herrenschmidt8d089082007-10-25 15:27:44 +100050 for (i = 0; i < NR_CPUS; i += threads_per_core) {
KOSAKI Motohiro104699c2011-04-28 05:07:23 +000051 cpumask_shift_left(&tmp, &threads_core_mask, i);
Shreyas B. Prabhue602ffb2015-04-20 10:32:56 +053052 if (cpumask_intersects(threads, &tmp)) {
53 cpu = cpumask_next_and(-1, &tmp, cpu_online_mask);
54 if (cpu < nr_cpu_ids)
55 cpumask_set_cpu(cpu, &res);
56 }
Benjamin Herrenschmidt8d089082007-10-25 15:27:44 +100057 }
58 return res;
59}
60
61static inline int cpu_nr_cores(void)
62{
Jan Stancekd52356e2015-03-31 18:11:46 +020063 return nr_cpu_ids >> threads_shift;
Benjamin Herrenschmidt8d089082007-10-25 15:27:44 +100064}
65
66static inline cpumask_t cpu_online_cores_map(void)
67{
KOSAKI Motohiro104699c2011-04-28 05:07:23 +000068 return cpu_thread_mask_to_cores(cpu_online_mask);
Benjamin Herrenschmidt8d089082007-10-25 15:27:44 +100069}
70
Vaidyanathan Srinivasan99d86702010-10-06 08:36:59 +000071#ifdef CONFIG_SMP
72int cpu_core_index_of_thread(int cpu);
73int cpu_first_thread_of_core(int core);
74#else
75static inline int cpu_core_index_of_thread(int cpu) { return cpu; }
76static inline int cpu_first_thread_of_core(int core) { return core; }
77#endif
Benjamin Herrenschmidt8d089082007-10-25 15:27:44 +100078
79static inline int cpu_thread_in_core(int cpu)
80{
81 return cpu & (threads_per_core - 1);
82}
83
Michael Ellerman5853aef2014-05-23 18:15:27 +100084static inline int cpu_thread_in_subcore(int cpu)
85{
86 return cpu & (threads_per_subcore - 1);
87}
88
Vaidyanathan Srinivasan99d86702010-10-06 08:36:59 +000089static inline int cpu_first_thread_sibling(int cpu)
Benjamin Herrenschmidt8d089082007-10-25 15:27:44 +100090{
91 return cpu & ~(threads_per_core - 1);
92}
93
Vaidyanathan Srinivasan99d86702010-10-06 08:36:59 +000094static inline int cpu_last_thread_sibling(int cpu)
Benjamin Herrenschmidtfcce8102009-07-23 23:15:10 +000095{
96 return cpu | (threads_per_core - 1);
97}
98
chenhui zhaoebb9d302015-12-24 08:39:57 +080099static inline u32 get_tensr(void)
100{
101#ifdef CONFIG_BOOKE
102 if (cpu_has_feature(CPU_FTR_SMT))
103 return mfspr(SPRN_TENSR);
104#endif
105 return 1;
106}
Benjamin Herrenschmidtfcce8102009-07-23 23:15:10 +0000107
chenhui zhao6becef72015-11-20 17:14:02 +0800108void book3e_start_thread(int thread, unsigned long addr);
chenhui zhaod17799f2015-11-20 17:13:59 +0800109void book3e_stop_thread(int thread);
Benjamin Herrenschmidtfcce8102009-07-23 23:15:10 +0000110
chenhui zhao6becef72015-11-20 17:14:02 +0800111#endif /* __ASSEMBLY__ */
112
113#define INVALID_THREAD_HWID 0x0fff
114
Benjamin Herrenschmidt8d089082007-10-25 15:27:44 +1000115#endif /* _ASM_POWERPC_CPUTHREADS_H */
116