1b8b572e1SStephen Rothwell #ifndef _ASM_POWERPC_CPUTHREADS_H 2b8b572e1SStephen Rothwell #define _ASM_POWERPC_CPUTHREADS_H 3b8b572e1SStephen Rothwell 4*6becef7eSchenhui zhao #ifndef __ASSEMBLY__ 5b8b572e1SStephen Rothwell #include <linux/cpumask.h> 6b8b572e1SStephen Rothwell 7b8b572e1SStephen Rothwell /* 8b8b572e1SStephen Rothwell * Mapping of threads to cores 9fcce8109SBenjamin Herrenschmidt * 10fcce8109SBenjamin Herrenschmidt * Note: This implementation is limited to a power of 2 number of 11fcce8109SBenjamin Herrenschmidt * threads per core and the same number for each core in the system 12fcce8109SBenjamin Herrenschmidt * (though it would work if some processors had less threads as long 13933b90a9SAnshuman Khandual * as the CPU numbers are still allocated, just not brought online). 14fcce8109SBenjamin Herrenschmidt * 15fcce8109SBenjamin Herrenschmidt * However, the API allows for a different implementation in the future 16fcce8109SBenjamin Herrenschmidt * if needed, as long as you only use the functions and not the variables 17fcce8109SBenjamin Herrenschmidt * directly. 18b8b572e1SStephen Rothwell */ 19b8b572e1SStephen Rothwell 20b8b572e1SStephen Rothwell #ifdef CONFIG_SMP 21b8b572e1SStephen Rothwell extern int threads_per_core; 225853aef1SMichael Ellerman extern int threads_per_subcore; 23b8b572e1SStephen Rothwell extern int threads_shift; 24b8b572e1SStephen Rothwell extern cpumask_t threads_core_mask; 25b8b572e1SStephen Rothwell #else 26b8b572e1SStephen Rothwell #define threads_per_core 1 275853aef1SMichael Ellerman #define threads_per_subcore 1 28b8b572e1SStephen Rothwell #define threads_shift 0 2987313df7SRusty Russell #define threads_core_mask (*get_cpu_mask(0)) 30b8b572e1SStephen Rothwell #endif 31b8b572e1SStephen Rothwell 32b8b572e1SStephen Rothwell /* cpu_thread_mask_to_cores - Return a cpumask of one per cores 33b8b572e1SStephen Rothwell * hit by the argument 34b8b572e1SStephen Rothwell * 35e602ffb2SShreyas B. Prabhu * @threads: a cpumask of online threads 36b8b572e1SStephen Rothwell * 37e602ffb2SShreyas B. Prabhu * This function returns a cpumask which will have one online cpu's 38b8b572e1SStephen Rothwell * bit set for each core that has at least one thread set in the argument. 39b8b572e1SStephen Rothwell * 40b8b572e1SStephen Rothwell * This can typically be used for things like IPI for tlb invalidations 41b8b572e1SStephen Rothwell * since those need to be done only once per core/TLB 42b8b572e1SStephen Rothwell */ 43104699c0SKOSAKI Motohiro static inline cpumask_t cpu_thread_mask_to_cores(const struct cpumask *threads) 44b8b572e1SStephen Rothwell { 45b8b572e1SStephen Rothwell cpumask_t tmp, res; 46e602ffb2SShreyas B. Prabhu int i, cpu; 47b8b572e1SStephen Rothwell 48104699c0SKOSAKI Motohiro cpumask_clear(&res); 49b8b572e1SStephen Rothwell for (i = 0; i < NR_CPUS; i += threads_per_core) { 50104699c0SKOSAKI Motohiro cpumask_shift_left(&tmp, &threads_core_mask, i); 51e602ffb2SShreyas B. Prabhu if (cpumask_intersects(threads, &tmp)) { 52e602ffb2SShreyas B. Prabhu cpu = cpumask_next_and(-1, &tmp, cpu_online_mask); 53e602ffb2SShreyas B. Prabhu if (cpu < nr_cpu_ids) 54e602ffb2SShreyas B. Prabhu cpumask_set_cpu(cpu, &res); 55e602ffb2SShreyas B. Prabhu } 56b8b572e1SStephen Rothwell } 57b8b572e1SStephen Rothwell return res; 58b8b572e1SStephen Rothwell } 59b8b572e1SStephen Rothwell 60b8b572e1SStephen Rothwell static inline int cpu_nr_cores(void) 61b8b572e1SStephen Rothwell { 62d52356e7SJan Stancek return nr_cpu_ids >> threads_shift; 63b8b572e1SStephen Rothwell } 64b8b572e1SStephen Rothwell 65b8b572e1SStephen Rothwell static inline cpumask_t cpu_online_cores_map(void) 66b8b572e1SStephen Rothwell { 67104699c0SKOSAKI Motohiro return cpu_thread_mask_to_cores(cpu_online_mask); 68b8b572e1SStephen Rothwell } 69b8b572e1SStephen Rothwell 7099d86705SVaidyanathan Srinivasan #ifdef CONFIG_SMP 7199d86705SVaidyanathan Srinivasan int cpu_core_index_of_thread(int cpu); 7299d86705SVaidyanathan Srinivasan int cpu_first_thread_of_core(int core); 7399d86705SVaidyanathan Srinivasan #else 7499d86705SVaidyanathan Srinivasan static inline int cpu_core_index_of_thread(int cpu) { return cpu; } 7599d86705SVaidyanathan Srinivasan static inline int cpu_first_thread_of_core(int core) { return core; } 7699d86705SVaidyanathan Srinivasan #endif 77b8b572e1SStephen Rothwell 78b8b572e1SStephen Rothwell static inline int cpu_thread_in_core(int cpu) 79b8b572e1SStephen Rothwell { 80b8b572e1SStephen Rothwell return cpu & (threads_per_core - 1); 81b8b572e1SStephen Rothwell } 82b8b572e1SStephen Rothwell 835853aef1SMichael Ellerman static inline int cpu_thread_in_subcore(int cpu) 845853aef1SMichael Ellerman { 855853aef1SMichael Ellerman return cpu & (threads_per_subcore - 1); 865853aef1SMichael Ellerman } 875853aef1SMichael Ellerman 8899d86705SVaidyanathan Srinivasan static inline int cpu_first_thread_sibling(int cpu) 89b8b572e1SStephen Rothwell { 90b8b572e1SStephen Rothwell return cpu & ~(threads_per_core - 1); 91b8b572e1SStephen Rothwell } 92b8b572e1SStephen Rothwell 9399d86705SVaidyanathan Srinivasan static inline int cpu_last_thread_sibling(int cpu) 94fcce8109SBenjamin Herrenschmidt { 95fcce8109SBenjamin Herrenschmidt return cpu | (threads_per_core - 1); 96fcce8109SBenjamin Herrenschmidt } 97fcce8109SBenjamin Herrenschmidt 98ebb9d30aSchenhui zhao static inline u32 get_tensr(void) 99ebb9d30aSchenhui zhao { 100ebb9d30aSchenhui zhao #ifdef CONFIG_BOOKE 101ebb9d30aSchenhui zhao if (cpu_has_feature(CPU_FTR_SMT)) 102ebb9d30aSchenhui zhao return mfspr(SPRN_TENSR); 103ebb9d30aSchenhui zhao #endif 104ebb9d30aSchenhui zhao return 1; 105ebb9d30aSchenhui zhao } 106fcce8109SBenjamin Herrenschmidt 107*6becef7eSchenhui zhao void book3e_start_thread(int thread, unsigned long addr); 108d17799f9Schenhui zhao void book3e_stop_thread(int thread); 109fcce8109SBenjamin Herrenschmidt 110*6becef7eSchenhui zhao #endif /* __ASSEMBLY__ */ 111*6becef7eSchenhui zhao 112*6becef7eSchenhui zhao #define INVALID_THREAD_HWID 0x0fff 113*6becef7eSchenhui zhao 114b8b572e1SStephen Rothwell #endif /* _ASM_POWERPC_CPUTHREADS_H */ 115b8b572e1SStephen Rothwell 116