Chris Zankel | 9a8fd55 | 2005-06-23 22:01:26 -0700 | [diff] [blame] | 1 | /* |
Chris Zankel | 9a8fd55 | 2005-06-23 22:01:26 -0700 | [diff] [blame] | 2 | * Switch an MMU context. |
| 3 | * |
| 4 | * This file is subject to the terms and conditions of the GNU General Public |
| 5 | * License. See the file "COPYING" in the main directory of this archive |
| 6 | * for more details. |
| 7 | * |
Max Filippov | f615136 | 2013-10-17 02:42:26 +0400 | [diff] [blame] | 8 | * Copyright (C) 2001 - 2013 Tensilica Inc. |
Chris Zankel | 9a8fd55 | 2005-06-23 22:01:26 -0700 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #ifndef _XTENSA_MMU_CONTEXT_H |
| 12 | #define _XTENSA_MMU_CONTEXT_H |
| 13 | |
Johannes Weiner | e5083a6 | 2009-03-04 16:21:31 +0100 | [diff] [blame] | 14 | #ifndef CONFIG_MMU |
| 15 | #include <asm/nommu_context.h> |
| 16 | #else |
| 17 | |
Chris Zankel | 9a8fd55 | 2005-06-23 22:01:26 -0700 | [diff] [blame] | 18 | #include <linux/stringify.h> |
Chris Zankel | de4f6e5 | 2007-05-31 17:47:01 -0700 | [diff] [blame] | 19 | #include <linux/sched.h> |
Chris Zankel | 9a8fd55 | 2005-06-23 22:01:26 -0700 | [diff] [blame] | 20 | |
Max Filippov | f615136 | 2013-10-17 02:42:26 +0400 | [diff] [blame] | 21 | #include <asm/vectors.h> |
Johannes Weiner | 4f682fb | 2009-03-04 16:21:28 +0100 | [diff] [blame] | 22 | |
Chris Zankel | 9a8fd55 | 2005-06-23 22:01:26 -0700 | [diff] [blame] | 23 | #include <asm/pgtable.h> |
Chris Zankel | 9a8fd55 | 2005-06-23 22:01:26 -0700 | [diff] [blame] | 24 | #include <asm/cacheflush.h> |
| 25 | #include <asm/tlbflush.h> |
Jeremy Fitzhardinge | d6dd61c | 2007-05-02 19:27:14 +0200 | [diff] [blame] | 26 | #include <asm-generic/mm_hooks.h> |
Max Filippov | f615136 | 2013-10-17 02:42:26 +0400 | [diff] [blame] | 27 | #include <asm-generic/percpu.h> |
Chris Zankel | 9a8fd55 | 2005-06-23 22:01:26 -0700 | [diff] [blame] | 28 | |
Chris Zankel | 9a8fd55 | 2005-06-23 22:01:26 -0700 | [diff] [blame] | 29 | #if (XCHAL_HAVE_TLBS != 1) |
| 30 | # error "Linux must have an MMU!" |
| 31 | #endif |
| 32 | |
Max Filippov | f615136 | 2013-10-17 02:42:26 +0400 | [diff] [blame] | 33 | DECLARE_PER_CPU(unsigned long, asid_cache); |
| 34 | #define cpu_asid_cache(cpu) per_cpu(asid_cache, cpu) |
Chris Zankel | 9a8fd55 | 2005-06-23 22:01:26 -0700 | [diff] [blame] | 35 | |
| 36 | /* |
| 37 | * NO_CONTEXT is the invalid ASID value that we don't ever assign to |
Max Filippov | f615136 | 2013-10-17 02:42:26 +0400 | [diff] [blame] | 38 | * any user or kernel context. We use the reserved values in the |
| 39 | * ASID_INSERT macro below. |
Chris Zankel | 173d6681 | 2006-12-10 02:18:48 -0800 | [diff] [blame] | 40 | * |
| 41 | * 0 invalid |
| 42 | * 1 kernel |
| 43 | * 2 reserved |
| 44 | * 3 reserved |
| 45 | * 4...255 available |
Chris Zankel | 9a8fd55 | 2005-06-23 22:01:26 -0700 | [diff] [blame] | 46 | */ |
| 47 | |
Chris Zankel | 173d6681 | 2006-12-10 02:18:48 -0800 | [diff] [blame] | 48 | #define NO_CONTEXT 0 |
| 49 | #define ASID_USER_FIRST 4 |
| 50 | #define ASID_MASK ((1 << XCHAL_MMU_ASID_BITS) - 1) |
| 51 | #define ASID_INSERT(x) (0x03020001 | (((x) & ASID_MASK) << 8)) |
Chris Zankel | 9a8fd55 | 2005-06-23 22:01:26 -0700 | [diff] [blame] | 52 | |
Max Filippov | c8f3a7d | 2013-10-17 02:42:21 +0400 | [diff] [blame] | 53 | void init_mmu(void); |
Max Filippov | c8f3a7d | 2013-10-17 02:42:21 +0400 | [diff] [blame] | 54 | |
Adrian Bunk | d99cf71 | 2005-09-03 15:57:53 -0700 | [diff] [blame] | 55 | static inline void set_rasid_register (unsigned long val) |
Chris Zankel | 9a8fd55 | 2005-06-23 22:01:26 -0700 | [diff] [blame] | 56 | { |
Max Filippov | bc5378f | 2012-10-15 03:55:38 +0400 | [diff] [blame] | 57 | __asm__ __volatile__ (" wsr %0, rasid\n\t" |
Chris Zankel | 9a8fd55 | 2005-06-23 22:01:26 -0700 | [diff] [blame] | 58 | " isync\n" : : "a" (val)); |
| 59 | } |
| 60 | |
Adrian Bunk | d99cf71 | 2005-09-03 15:57:53 -0700 | [diff] [blame] | 61 | static inline unsigned long get_rasid_register (void) |
Chris Zankel | 9a8fd55 | 2005-06-23 22:01:26 -0700 | [diff] [blame] | 62 | { |
| 63 | unsigned long tmp; |
Max Filippov | bc5378f | 2012-10-15 03:55:38 +0400 | [diff] [blame] | 64 | __asm__ __volatile__ (" rsr %0, rasid\n\t" : "=a" (tmp)); |
Chris Zankel | 9a8fd55 | 2005-06-23 22:01:26 -0700 | [diff] [blame] | 65 | return tmp; |
| 66 | } |
| 67 | |
Max Filippov | f615136 | 2013-10-17 02:42:26 +0400 | [diff] [blame] | 68 | static inline void get_new_mmu_context(struct mm_struct *mm, unsigned int cpu) |
Chris Zankel | 9a8fd55 | 2005-06-23 22:01:26 -0700 | [diff] [blame] | 69 | { |
Max Filippov | f615136 | 2013-10-17 02:42:26 +0400 | [diff] [blame] | 70 | unsigned long asid = cpu_asid_cache(cpu); |
| 71 | if ((++asid & ASID_MASK) == 0) { |
| 72 | /* |
| 73 | * Start new asid cycle; continue counting with next |
| 74 | * incarnation bits; skipping over 0, 1, 2, 3. |
| 75 | */ |
| 76 | local_flush_tlb_all(); |
| 77 | asid += ASID_USER_FIRST; |
Chris Zankel | 9a8fd55 | 2005-06-23 22:01:26 -0700 | [diff] [blame] | 78 | } |
Max Filippov | f615136 | 2013-10-17 02:42:26 +0400 | [diff] [blame] | 79 | cpu_asid_cache(cpu) = asid; |
| 80 | mm->context.asid[cpu] = asid; |
| 81 | mm->context.cpu = cpu; |
Chris Zankel | 9a8fd55 | 2005-06-23 22:01:26 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Max Filippov | f615136 | 2013-10-17 02:42:26 +0400 | [diff] [blame] | 84 | static inline void get_mmu_context(struct mm_struct *mm, unsigned int cpu) |
Chris Zankel | 9a8fd55 | 2005-06-23 22:01:26 -0700 | [diff] [blame] | 85 | { |
Max Filippov | f615136 | 2013-10-17 02:42:26 +0400 | [diff] [blame] | 86 | /* |
| 87 | * Check if our ASID is of an older version and thus invalid. |
| 88 | */ |
| 89 | |
| 90 | if (mm) { |
| 91 | unsigned long asid = mm->context.asid[cpu]; |
| 92 | |
| 93 | if (asid == NO_CONTEXT || |
| 94 | ((asid ^ cpu_asid_cache(cpu)) & ~ASID_MASK)) |
| 95 | get_new_mmu_context(mm, cpu); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | static inline void activate_context(struct mm_struct *mm, unsigned int cpu) |
| 100 | { |
| 101 | get_mmu_context(mm, cpu); |
| 102 | set_rasid_register(ASID_INSERT(mm->context.asid[cpu])); |
Chris Zankel | 173d6681 | 2006-12-10 02:18:48 -0800 | [diff] [blame] | 103 | invalidate_page_directory(); |
Chris Zankel | 9a8fd55 | 2005-06-23 22:01:26 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Chris Zankel | 9a8fd55 | 2005-06-23 22:01:26 -0700 | [diff] [blame] | 106 | /* |
| 107 | * Initialize the context related info for a new mm_struct |
Max Filippov | f615136 | 2013-10-17 02:42:26 +0400 | [diff] [blame] | 108 | * instance. Valid cpu values are 0..(NR_CPUS-1), so initializing |
| 109 | * to -1 says the process has never run on any core. |
Chris Zankel | 9a8fd55 | 2005-06-23 22:01:26 -0700 | [diff] [blame] | 110 | */ |
| 111 | |
Max Filippov | f615136 | 2013-10-17 02:42:26 +0400 | [diff] [blame] | 112 | static inline int init_new_context(struct task_struct *tsk, |
| 113 | struct mm_struct *mm) |
Chris Zankel | 9a8fd55 | 2005-06-23 22:01:26 -0700 | [diff] [blame] | 114 | { |
Max Filippov | f615136 | 2013-10-17 02:42:26 +0400 | [diff] [blame] | 115 | int cpu; |
| 116 | for_each_possible_cpu(cpu) { |
| 117 | mm->context.asid[cpu] = NO_CONTEXT; |
| 118 | } |
| 119 | mm->context.cpu = -1; |
Chris Zankel | 9a8fd55 | 2005-06-23 22:01:26 -0700 | [diff] [blame] | 120 | return 0; |
| 121 | } |
| 122 | |
Adrian Bunk | d99cf71 | 2005-09-03 15:57:53 -0700 | [diff] [blame] | 123 | static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next, |
Chris Zankel | c4c4594 | 2012-11-28 16:53:51 -0800 | [diff] [blame] | 124 | struct task_struct *tsk) |
Chris Zankel | 9a8fd55 | 2005-06-23 22:01:26 -0700 | [diff] [blame] | 125 | { |
Max Filippov | f615136 | 2013-10-17 02:42:26 +0400 | [diff] [blame] | 126 | unsigned int cpu = smp_processor_id(); |
| 127 | int migrated = next->context.cpu != cpu; |
| 128 | /* Flush the icache if we migrated to a new core. */ |
| 129 | if (migrated) { |
| 130 | __invalidate_icache_all(); |
| 131 | next->context.cpu = cpu; |
| 132 | } |
| 133 | if (migrated || prev != next) |
| 134 | activate_context(next, cpu); |
Chris Zankel | 9a8fd55 | 2005-06-23 22:01:26 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Max Filippov | f615136 | 2013-10-17 02:42:26 +0400 | [diff] [blame] | 137 | #define activate_mm(prev, next) switch_mm((prev), (next), NULL) |
| 138 | #define deactivate_mm(tsk, mm) do { } while (0) |
Chris Zankel | 9a8fd55 | 2005-06-23 22:01:26 -0700 | [diff] [blame] | 139 | |
| 140 | /* |
| 141 | * Destroy context related info for an mm_struct that is about |
| 142 | * to be put to rest. |
| 143 | */ |
Adrian Bunk | d99cf71 | 2005-09-03 15:57:53 -0700 | [diff] [blame] | 144 | static inline void destroy_context(struct mm_struct *mm) |
Chris Zankel | 9a8fd55 | 2005-06-23 22:01:26 -0700 | [diff] [blame] | 145 | { |
Chris Zankel | 9a8fd55 | 2005-06-23 22:01:26 -0700 | [diff] [blame] | 146 | invalidate_page_directory(); |
| 147 | } |
| 148 | |
| 149 | |
| 150 | static inline void enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk) |
| 151 | { |
| 152 | /* Nothing to do. */ |
| 153 | |
| 154 | } |
| 155 | |
Johannes Weiner | e5083a6 | 2009-03-04 16:21:31 +0100 | [diff] [blame] | 156 | #endif /* CONFIG_MMU */ |
Chris Zankel | 9a8fd55 | 2005-06-23 22:01:26 -0700 | [diff] [blame] | 157 | #endif /* _XTENSA_MMU_CONTEXT_H */ |