Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 1999 Niibe Yutaka |
Paul Mundt | cdcc970 | 2007-11-09 16:37:18 +0900 | [diff] [blame] | 3 | * Copyright (C) 2003 - 2007 Paul Mundt |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
| 5 | * ASID handling idea taken from MIPS implementation. |
| 6 | */ |
| 7 | #ifndef __ASM_SH_MMU_CONTEXT_H |
| 8 | #define __ASM_SH_MMU_CONTEXT_H |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | |
Paul Mundt | cdcc970 | 2007-11-09 16:37:18 +0900 | [diff] [blame] | 10 | #ifdef __KERNEL__ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <asm/cpu/mmu_context.h> |
| 12 | #include <asm/tlbflush.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <asm/uaccess.h> |
| 14 | #include <asm/io.h> |
Jeremy Fitzhardinge | d6dd61c | 2007-05-02 19:27:14 +0200 | [diff] [blame] | 15 | #include <asm-generic/mm_hooks.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | |
| 17 | /* |
| 18 | * The MMU "context" consists of two things: |
| 19 | * (a) TLB cache version (or round, cycle whatever expression you like) |
| 20 | * (b) ASID (Address Space IDentifier) |
| 21 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #define MMU_CONTEXT_ASID_MASK 0x000000ff |
| 23 | #define MMU_CONTEXT_VERSION_MASK 0xffffff00 |
| 24 | #define MMU_CONTEXT_FIRST_VERSION 0x00000100 |
| 25 | #define NO_CONTEXT 0 |
| 26 | |
| 27 | /* ASID is 8-bit value, so it can't be 0x100 */ |
| 28 | #define MMU_NO_ASID 0x100 |
| 29 | |
Paul Mundt | aec5e0e | 2006-12-25 09:51:47 +0900 | [diff] [blame] | 30 | #define asid_cache(cpu) (cpu_data[cpu].asid_cache) |
Paul Mundt | cdcc970 | 2007-11-09 16:37:18 +0900 | [diff] [blame] | 31 | #define cpu_context(cpu, mm) ((mm)->context.id[cpu]) |
| 32 | |
| 33 | #define cpu_asid(cpu, mm) \ |
| 34 | (cpu_context((cpu), (mm)) & MMU_CONTEXT_ASID_MASK) |
Paul Mundt | aec5e0e | 2006-12-25 09:51:47 +0900 | [diff] [blame] | 35 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | /* |
| 37 | * Virtual Page Number mask |
| 38 | */ |
| 39 | #define MMU_VPN_MASK 0xfffff000 |
| 40 | |
| 41 | #ifdef CONFIG_MMU |
Paul Mundt | cdcc970 | 2007-11-09 16:37:18 +0900 | [diff] [blame] | 42 | #if defined(CONFIG_SUPERH32) |
| 43 | #include "mmu_context_32.h" |
| 44 | #else |
| 45 | #include "mmu_context_64.h" |
| 46 | #endif |
| 47 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | /* |
| 49 | * Get MMU context if needed. |
| 50 | */ |
Paul Mundt | aec5e0e | 2006-12-25 09:51:47 +0900 | [diff] [blame] | 51 | static inline void get_mmu_context(struct mm_struct *mm, unsigned int cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | { |
Paul Mundt | aec5e0e | 2006-12-25 09:51:47 +0900 | [diff] [blame] | 53 | unsigned long asid = asid_cache(cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
| 55 | /* Check if we have old version of context. */ |
Paul Mundt | aec5e0e | 2006-12-25 09:51:47 +0900 | [diff] [blame] | 56 | if (((cpu_context(cpu, mm) ^ asid) & MMU_CONTEXT_VERSION_MASK) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | /* It's up to date, do nothing */ |
| 58 | return; |
| 59 | |
| 60 | /* It's old, we need to get new context with new version. */ |
Paul Mundt | aec5e0e | 2006-12-25 09:51:47 +0900 | [diff] [blame] | 61 | if (!(++asid & MMU_CONTEXT_ASID_MASK)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | /* |
| 63 | * We exhaust ASID of this version. |
| 64 | * Flush all TLB and start new cycle. |
| 65 | */ |
| 66 | flush_tlb_all(); |
Stuart Menefy | 6e4662f | 2006-11-21 13:53:44 +0900 | [diff] [blame] | 67 | |
Paul Mundt | cdcc970 | 2007-11-09 16:37:18 +0900 | [diff] [blame] | 68 | #ifdef CONFIG_SUPERH64 |
| 69 | /* |
| 70 | * The SH-5 cache uses the ASIDs, requiring both the I and D |
| 71 | * cache to be flushed when the ASID is exhausted. Weak. |
| 72 | */ |
| 73 | flush_cache_all(); |
| 74 | #endif |
| 75 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | /* |
| 77 | * Fix version; Note that we avoid version #0 |
| 78 | * to distingush NO_CONTEXT. |
| 79 | */ |
Paul Mundt | aec5e0e | 2006-12-25 09:51:47 +0900 | [diff] [blame] | 80 | if (!asid) |
| 81 | asid = MMU_CONTEXT_FIRST_VERSION; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | } |
Paul Mundt | aec5e0e | 2006-12-25 09:51:47 +0900 | [diff] [blame] | 83 | |
| 84 | cpu_context(cpu, mm) = asid_cache(cpu) = asid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | /* |
| 88 | * Initialize the context related info for a new mm_struct |
| 89 | * instance. |
| 90 | */ |
Stuart Menefy | 6e4662f | 2006-11-21 13:53:44 +0900 | [diff] [blame] | 91 | static inline int init_new_context(struct task_struct *tsk, |
Paul Mundt | aec5e0e | 2006-12-25 09:51:47 +0900 | [diff] [blame] | 92 | struct mm_struct *mm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | { |
Paul Mundt | aec5e0e | 2006-12-25 09:51:47 +0900 | [diff] [blame] | 94 | int i; |
| 95 | |
| 96 | for (i = 0; i < num_online_cpus(); i++) |
| 97 | cpu_context(i, mm) = NO_CONTEXT; |
| 98 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | * After we have set current->mm to a new value, this activates |
| 104 | * the context for the new mm so we see the new mappings. |
| 105 | */ |
Paul Mundt | aec5e0e | 2006-12-25 09:51:47 +0900 | [diff] [blame] | 106 | static inline void activate_context(struct mm_struct *mm, unsigned int cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | { |
Paul Mundt | aec5e0e | 2006-12-25 09:51:47 +0900 | [diff] [blame] | 108 | get_mmu_context(mm, cpu); |
| 109 | set_asid(cpu_asid(cpu, mm)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Stuart Menefy | 6e4662f | 2006-11-21 13:53:44 +0900 | [diff] [blame] | 112 | static inline void switch_mm(struct mm_struct *prev, |
| 113 | struct mm_struct *next, |
| 114 | struct task_struct *tsk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | { |
Paul Mundt | aec5e0e | 2006-12-25 09:51:47 +0900 | [diff] [blame] | 116 | unsigned int cpu = smp_processor_id(); |
| 117 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | if (likely(prev != next)) { |
Paul Mundt | aec5e0e | 2006-12-25 09:51:47 +0900 | [diff] [blame] | 119 | cpu_set(cpu, next->cpu_vm_mask); |
Stuart Menefy | 6e4662f | 2006-11-21 13:53:44 +0900 | [diff] [blame] | 120 | set_TTB(next->pgd); |
Paul Mundt | aec5e0e | 2006-12-25 09:51:47 +0900 | [diff] [blame] | 121 | activate_context(next, cpu); |
| 122 | } else |
| 123 | if (!cpu_test_and_set(cpu, next->cpu_vm_mask)) |
| 124 | activate_context(next, cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | } |
Paul Mundt | cdcc970 | 2007-11-09 16:37:18 +0900 | [diff] [blame] | 126 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | #define get_mmu_context(mm) do { } while (0) |
| 128 | #define init_new_context(tsk,mm) (0) |
| 129 | #define destroy_context(mm) do { } while (0) |
| 130 | #define set_asid(asid) do { } while (0) |
| 131 | #define get_asid() (0) |
Paul Mundt | 0106662 | 2007-03-28 16:38:13 +0900 | [diff] [blame] | 132 | #define set_TTB(pgd) do { } while (0) |
| 133 | #define get_TTB() (0) |
Paul Mundt | aec5e0e | 2006-12-25 09:51:47 +0900 | [diff] [blame] | 134 | #define activate_context(mm,cpu) do { } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | #define switch_mm(prev,next,tsk) do { } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | #endif /* CONFIG_MMU */ |
| 137 | |
Paul Mundt | cdcc970 | 2007-11-09 16:37:18 +0900 | [diff] [blame] | 138 | #define activate_mm(prev, next) switch_mm((prev),(next),NULL) |
| 139 | #define deactivate_mm(tsk,mm) do { } while (0) |
| 140 | #define enter_lazy_tlb(mm,tsk) do { } while (0) |
| 141 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | #if defined(CONFIG_CPU_SH3) || defined(CONFIG_CPU_SH4) |
| 143 | /* |
| 144 | * If this processor has an MMU, we need methods to turn it off/on .. |
| 145 | * paging_init() will also have to be updated for the processor in |
| 146 | * question. |
| 147 | */ |
| 148 | static inline void enable_mmu(void) |
| 149 | { |
Paul Mundt | aec5e0e | 2006-12-25 09:51:47 +0900 | [diff] [blame] | 150 | unsigned int cpu = smp_processor_id(); |
| 151 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | /* Enable MMU */ |
| 153 | ctrl_outl(MMU_CONTROL_INIT, MMUCR); |
Paul Mundt | 2984762 | 2006-09-27 14:57:44 +0900 | [diff] [blame] | 154 | ctrl_barrier(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | |
Paul Mundt | aec5e0e | 2006-12-25 09:51:47 +0900 | [diff] [blame] | 156 | if (asid_cache(cpu) == NO_CONTEXT) |
| 157 | asid_cache(cpu) = MMU_CONTEXT_FIRST_VERSION; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | |
Paul Mundt | aec5e0e | 2006-12-25 09:51:47 +0900 | [diff] [blame] | 159 | set_asid(asid_cache(cpu) & MMU_CONTEXT_ASID_MASK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | static inline void disable_mmu(void) |
| 163 | { |
| 164 | unsigned long cr; |
| 165 | |
| 166 | cr = ctrl_inl(MMUCR); |
| 167 | cr &= ~MMU_CONTROL_INIT; |
| 168 | ctrl_outl(cr, MMUCR); |
Paul Mundt | 2984762 | 2006-09-27 14:57:44 +0900 | [diff] [blame] | 169 | |
| 170 | ctrl_barrier(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | } |
| 172 | #else |
| 173 | /* |
| 174 | * MMU control handlers for processors lacking memory |
| 175 | * management hardware. |
| 176 | */ |
Paul Mundt | 0106662 | 2007-03-28 16:38:13 +0900 | [diff] [blame] | 177 | #define enable_mmu() do { } while (0) |
| 178 | #define disable_mmu() do { } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | #endif |
| 180 | |
| 181 | #endif /* __KERNEL__ */ |
| 182 | #endif /* __ASM_SH_MMU_CONTEXT_H */ |