blob: 666bef4ebfae72a88c4bceebacbc18a7bfa7b390 [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>
6
7/*
8 * Mapping of threads to cores
Benjamin Herrenschmidtfcce8102009-07-23 23:15:10 +00009 *
10 * Note: This implementation is limited to a power of 2 number of
11 * threads per core and the same number for each core in the system
12 * (though it would work if some processors had less threads as long
Anshuman Khandual933b90a2012-05-14 05:32:00 +000013 * as the CPU numbers are still allocated, just not brought online).
Benjamin Herrenschmidtfcce8102009-07-23 23:15:10 +000014 *
15 * However, the API allows for a different implementation in the future
16 * if needed, as long as you only use the functions and not the variables
17 * directly.
Benjamin Herrenschmidt8d089082007-10-25 15:27:44 +100018 */
19
20#ifdef CONFIG_SMP
21extern int threads_per_core;
Michael Ellerman5853aef2014-05-23 18:15:27 +100022extern int threads_per_subcore;
Benjamin Herrenschmidt8d089082007-10-25 15:27:44 +100023extern int threads_shift;
24extern cpumask_t threads_core_mask;
25#else
26#define threads_per_core 1
Michael Ellerman5853aef2014-05-23 18:15:27 +100027#define threads_per_subcore 1
Benjamin Herrenschmidt8d089082007-10-25 15:27:44 +100028#define threads_shift 0
Rusty Russell87313df2015-03-10 12:28:29 +103029#define threads_core_mask (*get_cpu_mask(0))
Benjamin Herrenschmidt8d089082007-10-25 15:27:44 +100030#endif
31
32/* cpu_thread_mask_to_cores - Return a cpumask of one per cores
33 * hit by the argument
34 *
Shreyas B. Prabhue602ffb2015-04-20 10:32:56 +053035 * @threads: a cpumask of online threads
Benjamin Herrenschmidt8d089082007-10-25 15:27:44 +100036 *
Shreyas B. Prabhue602ffb2015-04-20 10:32:56 +053037 * This function returns a cpumask which will have one online cpu's
Benjamin Herrenschmidt8d089082007-10-25 15:27:44 +100038 * bit set for each core that has at least one thread set in the argument.
39 *
40 * This can typically be used for things like IPI for tlb invalidations
41 * since those need to be done only once per core/TLB
42 */
KOSAKI Motohiro104699c2011-04-28 05:07:23 +000043static inline cpumask_t cpu_thread_mask_to_cores(const struct cpumask *threads)
Benjamin Herrenschmidt8d089082007-10-25 15:27:44 +100044{
45 cpumask_t tmp, res;
Shreyas B. Prabhue602ffb2015-04-20 10:32:56 +053046 int i, cpu;
Benjamin Herrenschmidt8d089082007-10-25 15:27:44 +100047
KOSAKI Motohiro104699c2011-04-28 05:07:23 +000048 cpumask_clear(&res);
Benjamin Herrenschmidt8d089082007-10-25 15:27:44 +100049 for (i = 0; i < NR_CPUS; i += threads_per_core) {
KOSAKI Motohiro104699c2011-04-28 05:07:23 +000050 cpumask_shift_left(&tmp, &threads_core_mask, i);
Shreyas B. Prabhue602ffb2015-04-20 10:32:56 +053051 if (cpumask_intersects(threads, &tmp)) {
52 cpu = cpumask_next_and(-1, &tmp, cpu_online_mask);
53 if (cpu < nr_cpu_ids)
54 cpumask_set_cpu(cpu, &res);
55 }
Benjamin Herrenschmidt8d089082007-10-25 15:27:44 +100056 }
57 return res;
58}
59
60static inline int cpu_nr_cores(void)
61{
Jan Stancekd52356e2015-03-31 18:11:46 +020062 return nr_cpu_ids >> threads_shift;
Benjamin Herrenschmidt8d089082007-10-25 15:27:44 +100063}
64
65static inline cpumask_t cpu_online_cores_map(void)
66{
KOSAKI Motohiro104699c2011-04-28 05:07:23 +000067 return cpu_thread_mask_to_cores(cpu_online_mask);
Benjamin Herrenschmidt8d089082007-10-25 15:27:44 +100068}
69
Vaidyanathan Srinivasan99d86702010-10-06 08:36:59 +000070#ifdef CONFIG_SMP
71int cpu_core_index_of_thread(int cpu);
72int cpu_first_thread_of_core(int core);
73#else
74static inline int cpu_core_index_of_thread(int cpu) { return cpu; }
75static inline int cpu_first_thread_of_core(int core) { return core; }
76#endif
Benjamin Herrenschmidt8d089082007-10-25 15:27:44 +100077
78static inline int cpu_thread_in_core(int cpu)
79{
80 return cpu & (threads_per_core - 1);
81}
82
Michael Ellerman5853aef2014-05-23 18:15:27 +100083static inline int cpu_thread_in_subcore(int cpu)
84{
85 return cpu & (threads_per_subcore - 1);
86}
87
Vaidyanathan Srinivasan99d86702010-10-06 08:36:59 +000088static inline int cpu_first_thread_sibling(int cpu)
Benjamin Herrenschmidt8d089082007-10-25 15:27:44 +100089{
90 return cpu & ~(threads_per_core - 1);
91}
92
Vaidyanathan Srinivasan99d86702010-10-06 08:36:59 +000093static inline int cpu_last_thread_sibling(int cpu)
Benjamin Herrenschmidtfcce8102009-07-23 23:15:10 +000094{
95 return cpu | (threads_per_core - 1);
96}
97
chenhui zhaoebb9d302015-12-24 08:39:57 +080098static inline u32 get_tensr(void)
99{
100#ifdef CONFIG_BOOKE
101 if (cpu_has_feature(CPU_FTR_SMT))
102 return mfspr(SPRN_TENSR);
103#endif
104 return 1;
105}
Benjamin Herrenschmidtfcce8102009-07-23 23:15:10 +0000106
chenhui zhao6becef72015-11-20 17:14:02 +0800107void book3e_start_thread(int thread, unsigned long addr);
chenhui zhaod17799f2015-11-20 17:13:59 +0800108void book3e_stop_thread(int thread);
Benjamin Herrenschmidtfcce8102009-07-23 23:15:10 +0000109
chenhui zhao6becef72015-11-20 17:14:02 +0800110#endif /* __ASSEMBLY__ */
111
112#define INVALID_THREAD_HWID 0x0fff
113
Benjamin Herrenschmidt8d089082007-10-25 15:27:44 +1000114#endif /* _ASM_POWERPC_CPUTHREADS_H */
115