blob: ce516e5eb0d3523d5027f3957877cfe144ee7a3f [file] [log] [blame]
Benjamin Herrenschmidt8d089082007-10-25 15:27:44 +10001#ifndef _ASM_POWERPC_CPUTHREADS_H
2#define _ASM_POWERPC_CPUTHREADS_H
3
4#include <linux/cpumask.h>
5
6/*
7 * Mapping of threads to cores
Benjamin Herrenschmidtfcce8102009-07-23 23:15:10 +00008 *
9 * Note: This implementation is limited to a power of 2 number of
10 * threads per core and the same number for each core in the system
11 * (though it would work if some processors had less threads as long
12 * as the CPU numbers are still allocated, just not brought offline).
13 *
14 * However, the API allows for a different implementation in the future
15 * if needed, as long as you only use the functions and not the variables
16 * directly.
Benjamin Herrenschmidt8d089082007-10-25 15:27:44 +100017 */
18
19#ifdef CONFIG_SMP
20extern int threads_per_core;
21extern int threads_shift;
22extern cpumask_t threads_core_mask;
23#else
24#define threads_per_core 1
25#define threads_shift 0
26#define threads_core_mask (CPU_MASK_CPU0)
27#endif
28
29/* cpu_thread_mask_to_cores - Return a cpumask of one per cores
30 * hit by the argument
31 *
32 * @threads: a cpumask of threads
33 *
34 * This function returns a cpumask which will have one "cpu" (or thread)
35 * bit set for each core that has at least one thread set in the argument.
36 *
37 * This can typically be used for things like IPI for tlb invalidations
38 * since those need to be done only once per core/TLB
39 */
KOSAKI Motohiro104699c2011-04-28 05:07:23 +000040static inline cpumask_t cpu_thread_mask_to_cores(const struct cpumask *threads)
Benjamin Herrenschmidt8d089082007-10-25 15:27:44 +100041{
42 cpumask_t tmp, res;
43 int i;
44
KOSAKI Motohiro104699c2011-04-28 05:07:23 +000045 cpumask_clear(&res);
Benjamin Herrenschmidt8d089082007-10-25 15:27:44 +100046 for (i = 0; i < NR_CPUS; i += threads_per_core) {
KOSAKI Motohiro104699c2011-04-28 05:07:23 +000047 cpumask_shift_left(&tmp, &threads_core_mask, i);
48 if (cpumask_intersects(threads, &tmp))
49 cpumask_set_cpu(i, &res);
Benjamin Herrenschmidt8d089082007-10-25 15:27:44 +100050 }
51 return res;
52}
53
54static inline int cpu_nr_cores(void)
55{
56 return NR_CPUS >> threads_shift;
57}
58
59static inline cpumask_t cpu_online_cores_map(void)
60{
KOSAKI Motohiro104699c2011-04-28 05:07:23 +000061 return cpu_thread_mask_to_cores(cpu_online_mask);
Benjamin Herrenschmidt8d089082007-10-25 15:27:44 +100062}
63
Vaidyanathan Srinivasan99d86702010-10-06 08:36:59 +000064#ifdef CONFIG_SMP
65int cpu_core_index_of_thread(int cpu);
66int cpu_first_thread_of_core(int core);
67#else
68static inline int cpu_core_index_of_thread(int cpu) { return cpu; }
69static inline int cpu_first_thread_of_core(int core) { return core; }
70#endif
Benjamin Herrenschmidt8d089082007-10-25 15:27:44 +100071
72static inline int cpu_thread_in_core(int cpu)
73{
74 return cpu & (threads_per_core - 1);
75}
76
Vaidyanathan Srinivasan99d86702010-10-06 08:36:59 +000077static inline int cpu_first_thread_sibling(int cpu)
Benjamin Herrenschmidt8d089082007-10-25 15:27:44 +100078{
79 return cpu & ~(threads_per_core - 1);
80}
81
Vaidyanathan Srinivasan99d86702010-10-06 08:36:59 +000082static inline int cpu_last_thread_sibling(int cpu)
Benjamin Herrenschmidtfcce8102009-07-23 23:15:10 +000083{
84 return cpu | (threads_per_core - 1);
85}
86
87
88
Benjamin Herrenschmidt8d089082007-10-25 15:27:44 +100089#endif /* _ASM_POWERPC_CPUTHREADS_H */
90