Benjamin Herrenschmidt | 8d08908 | 2007-10-25 15:27:44 +1000 | [diff] [blame] | 1 | #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 Herrenschmidt | fcce810 | 2009-07-23 23:15:10 +0000 | [diff] [blame] | 8 | * |
| 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 |
Anshuman Khandual | 933b90a | 2012-05-14 05:32:00 +0000 | [diff] [blame] | 12 | * as the CPU numbers are still allocated, just not brought online). |
Benjamin Herrenschmidt | fcce810 | 2009-07-23 23:15:10 +0000 | [diff] [blame] | 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 Herrenschmidt | 8d08908 | 2007-10-25 15:27:44 +1000 | [diff] [blame] | 17 | */ |
| 18 | |
| 19 | #ifdef CONFIG_SMP |
| 20 | extern int threads_per_core; |
Michael Ellerman | 5853aef | 2014-05-23 18:15:27 +1000 | [diff] [blame^] | 21 | extern int threads_per_subcore; |
Benjamin Herrenschmidt | 8d08908 | 2007-10-25 15:27:44 +1000 | [diff] [blame] | 22 | extern int threads_shift; |
| 23 | extern cpumask_t threads_core_mask; |
| 24 | #else |
| 25 | #define threads_per_core 1 |
Michael Ellerman | 5853aef | 2014-05-23 18:15:27 +1000 | [diff] [blame^] | 26 | #define threads_per_subcore 1 |
Benjamin Herrenschmidt | 8d08908 | 2007-10-25 15:27:44 +1000 | [diff] [blame] | 27 | #define threads_shift 0 |
| 28 | #define threads_core_mask (CPU_MASK_CPU0) |
| 29 | #endif |
| 30 | |
| 31 | /* cpu_thread_mask_to_cores - Return a cpumask of one per cores |
| 32 | * hit by the argument |
| 33 | * |
| 34 | * @threads: a cpumask of threads |
| 35 | * |
| 36 | * This function returns a cpumask which will have one "cpu" (or thread) |
| 37 | * bit set for each core that has at least one thread set in the argument. |
| 38 | * |
| 39 | * This can typically be used for things like IPI for tlb invalidations |
| 40 | * since those need to be done only once per core/TLB |
| 41 | */ |
KOSAKI Motohiro | 104699c | 2011-04-28 05:07:23 +0000 | [diff] [blame] | 42 | static inline cpumask_t cpu_thread_mask_to_cores(const struct cpumask *threads) |
Benjamin Herrenschmidt | 8d08908 | 2007-10-25 15:27:44 +1000 | [diff] [blame] | 43 | { |
| 44 | cpumask_t tmp, res; |
| 45 | int i; |
| 46 | |
KOSAKI Motohiro | 104699c | 2011-04-28 05:07:23 +0000 | [diff] [blame] | 47 | cpumask_clear(&res); |
Benjamin Herrenschmidt | 8d08908 | 2007-10-25 15:27:44 +1000 | [diff] [blame] | 48 | for (i = 0; i < NR_CPUS; i += threads_per_core) { |
KOSAKI Motohiro | 104699c | 2011-04-28 05:07:23 +0000 | [diff] [blame] | 49 | cpumask_shift_left(&tmp, &threads_core_mask, i); |
| 50 | if (cpumask_intersects(threads, &tmp)) |
| 51 | cpumask_set_cpu(i, &res); |
Benjamin Herrenschmidt | 8d08908 | 2007-10-25 15:27:44 +1000 | [diff] [blame] | 52 | } |
| 53 | return res; |
| 54 | } |
| 55 | |
| 56 | static inline int cpu_nr_cores(void) |
| 57 | { |
| 58 | return NR_CPUS >> threads_shift; |
| 59 | } |
| 60 | |
| 61 | static inline cpumask_t cpu_online_cores_map(void) |
| 62 | { |
KOSAKI Motohiro | 104699c | 2011-04-28 05:07:23 +0000 | [diff] [blame] | 63 | return cpu_thread_mask_to_cores(cpu_online_mask); |
Benjamin Herrenschmidt | 8d08908 | 2007-10-25 15:27:44 +1000 | [diff] [blame] | 64 | } |
| 65 | |
Vaidyanathan Srinivasan | 99d8670 | 2010-10-06 08:36:59 +0000 | [diff] [blame] | 66 | #ifdef CONFIG_SMP |
| 67 | int cpu_core_index_of_thread(int cpu); |
| 68 | int cpu_first_thread_of_core(int core); |
| 69 | #else |
| 70 | static inline int cpu_core_index_of_thread(int cpu) { return cpu; } |
| 71 | static inline int cpu_first_thread_of_core(int core) { return core; } |
| 72 | #endif |
Benjamin Herrenschmidt | 8d08908 | 2007-10-25 15:27:44 +1000 | [diff] [blame] | 73 | |
| 74 | static inline int cpu_thread_in_core(int cpu) |
| 75 | { |
| 76 | return cpu & (threads_per_core - 1); |
| 77 | } |
| 78 | |
Michael Ellerman | 5853aef | 2014-05-23 18:15:27 +1000 | [diff] [blame^] | 79 | static inline int cpu_thread_in_subcore(int cpu) |
| 80 | { |
| 81 | return cpu & (threads_per_subcore - 1); |
| 82 | } |
| 83 | |
Vaidyanathan Srinivasan | 99d8670 | 2010-10-06 08:36:59 +0000 | [diff] [blame] | 84 | static inline int cpu_first_thread_sibling(int cpu) |
Benjamin Herrenschmidt | 8d08908 | 2007-10-25 15:27:44 +1000 | [diff] [blame] | 85 | { |
| 86 | return cpu & ~(threads_per_core - 1); |
| 87 | } |
| 88 | |
Vaidyanathan Srinivasan | 99d8670 | 2010-10-06 08:36:59 +0000 | [diff] [blame] | 89 | static inline int cpu_last_thread_sibling(int cpu) |
Benjamin Herrenschmidt | fcce810 | 2009-07-23 23:15:10 +0000 | [diff] [blame] | 90 | { |
| 91 | return cpu | (threads_per_core - 1); |
| 92 | } |
| 93 | |
| 94 | |
| 95 | |
Benjamin Herrenschmidt | 8d08908 | 2007-10-25 15:27:44 +1000 | [diff] [blame] | 96 | #endif /* _ASM_POWERPC_CPUTHREADS_H */ |
| 97 | |