Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 1 | /* |
Robin Getz | 96f1050 | 2009-09-24 14:11:24 +0000 | [diff] [blame] | 2 | * Copyright 2004-2009 Analog Devices Inc. |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 3 | * |
Robin Getz | 96f1050 | 2009-09-24 14:11:24 +0000 | [diff] [blame] | 4 | * Licensed under the GPL-2 or later. |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #ifndef __BLACKFIN_MMU_CONTEXT_H__ |
| 8 | #define __BLACKFIN_MMU_CONTEXT_H__ |
| 9 | |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 10 | #include <linux/slab.h> |
Bernd Schmidt | b97b8a9 | 2008-01-27 18:39:16 +0800 | [diff] [blame] | 11 | #include <linux/sched.h> |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 12 | #include <asm/setup.h> |
| 13 | #include <asm/page.h> |
| 14 | #include <asm/pgalloc.h> |
Bernd Schmidt | b97b8a9 | 2008-01-27 18:39:16 +0800 | [diff] [blame] | 15 | #include <asm/cplbinit.h> |
Barry Song | e18e7dd | 2009-12-07 10:05:58 +0000 | [diff] [blame] | 16 | #include <asm/sections.h> |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 17 | |
Graf Yang | b8a9898 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 18 | /* Note: L1 stacks are CPU-private things, so we bluntly disable this |
| 19 | feature in SMP mode, and use the per-CPU scratch SRAM bank only to |
| 20 | store the PDA instead. */ |
| 21 | |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 22 | extern void *current_l1_stack_save; |
| 23 | extern int nr_l1stack_tasks; |
| 24 | extern void *l1_stack_base; |
| 25 | extern unsigned long l1_stack_len; |
| 26 | |
| 27 | extern int l1sram_free(const void*); |
| 28 | extern void *l1sram_alloc_max(void*); |
| 29 | |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 30 | static inline void free_l1stack(void) |
| 31 | { |
| 32 | nr_l1stack_tasks--; |
| 33 | if (nr_l1stack_tasks == 0) |
| 34 | l1sram_free(l1_stack_base); |
| 35 | } |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 36 | |
| 37 | static inline unsigned long |
| 38 | alloc_l1stack(unsigned long length, unsigned long *stack_base) |
| 39 | { |
| 40 | if (nr_l1stack_tasks == 0) { |
| 41 | l1_stack_base = l1sram_alloc_max(&l1_stack_len); |
| 42 | if (!l1_stack_base) |
| 43 | return 0; |
| 44 | } |
| 45 | |
| 46 | if (l1_stack_len < length) { |
| 47 | if (nr_l1stack_tasks == 0) |
| 48 | l1sram_free(l1_stack_base); |
| 49 | return 0; |
| 50 | } |
| 51 | *stack_base = (unsigned long)l1_stack_base; |
| 52 | nr_l1stack_tasks++; |
| 53 | return l1_stack_len; |
| 54 | } |
| 55 | |
| 56 | static inline int |
| 57 | activate_l1stack(struct mm_struct *mm, unsigned long sp_base) |
| 58 | { |
| 59 | if (current_l1_stack_save) |
| 60 | memcpy(current_l1_stack_save, l1_stack_base, l1_stack_len); |
| 61 | mm->context.l1_stack_save = current_l1_stack_save = (void*)sp_base; |
| 62 | memcpy(l1_stack_base, current_l1_stack_save, l1_stack_len); |
| 63 | return 1; |
| 64 | } |
| 65 | |
| 66 | #define deactivate_mm(tsk,mm) do { } while (0) |
| 67 | |
Bernd Schmidt | b97b8a9 | 2008-01-27 18:39:16 +0800 | [diff] [blame] | 68 | #define activate_mm(prev, next) switch_mm(prev, next, NULL) |
| 69 | |
Philippe Gerum | 4815b88 | 2009-10-27 22:05:33 +0100 | [diff] [blame] | 70 | static inline void __switch_mm(struct mm_struct *prev_mm, struct mm_struct *next_mm, |
| 71 | struct task_struct *tsk) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 72 | { |
Graf Yang | b8a9898 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 73 | #ifdef CONFIG_MPU |
| 74 | unsigned int cpu = smp_processor_id(); |
| 75 | #endif |
Bernd Schmidt | b97b8a9 | 2008-01-27 18:39:16 +0800 | [diff] [blame] | 76 | if (prev_mm == next_mm) |
| 77 | return; |
| 78 | #ifdef CONFIG_MPU |
Graf Yang | b8a9898 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 79 | if (prev_mm->context.page_rwx_mask == current_rwx_mask[cpu]) { |
| 80 | flush_switched_cplbs(cpu); |
| 81 | set_mask_dcplbs(next_mm->context.page_rwx_mask, cpu); |
Bernd Schmidt | b97b8a9 | 2008-01-27 18:39:16 +0800 | [diff] [blame] | 82 | } |
| 83 | #endif |
| 84 | |
Graf Yang | ca87b7a | 2008-10-08 17:30:01 +0800 | [diff] [blame] | 85 | #ifdef CONFIG_APP_STACK_L1 |
Bernd Schmidt | b97b8a9 | 2008-01-27 18:39:16 +0800 | [diff] [blame] | 86 | /* L1 stack switching. */ |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 87 | if (!next_mm->context.l1_stack_save) |
| 88 | return; |
| 89 | if (next_mm->context.l1_stack_save == current_l1_stack_save) |
| 90 | return; |
| 91 | if (current_l1_stack_save) { |
| 92 | memcpy(current_l1_stack_save, l1_stack_base, l1_stack_len); |
| 93 | } |
| 94 | current_l1_stack_save = next_mm->context.l1_stack_save; |
| 95 | memcpy(l1_stack_base, current_l1_stack_save, l1_stack_len); |
Graf Yang | ca87b7a | 2008-10-08 17:30:01 +0800 | [diff] [blame] | 96 | #endif |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Philippe Gerum | 4815b88 | 2009-10-27 22:05:33 +0100 | [diff] [blame] | 99 | #ifdef CONFIG_IPIPE |
| 100 | #define lock_mm_switch(flags) local_irq_save_hw_cond(flags) |
| 101 | #define unlock_mm_switch(flags) local_irq_restore_hw_cond(flags) |
| 102 | #else |
| 103 | #define lock_mm_switch(flags) do { (void)(flags); } while (0) |
| 104 | #define unlock_mm_switch(flags) do { (void)(flags); } while (0) |
| 105 | #endif /* CONFIG_IPIPE */ |
| 106 | |
Bernd Schmidt | b97b8a9 | 2008-01-27 18:39:16 +0800 | [diff] [blame] | 107 | #ifdef CONFIG_MPU |
Philippe Gerum | 4815b88 | 2009-10-27 22:05:33 +0100 | [diff] [blame] | 108 | static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next, |
| 109 | struct task_struct *tsk) |
| 110 | { |
| 111 | unsigned long flags; |
| 112 | lock_mm_switch(flags); |
| 113 | __switch_mm(prev, next, tsk); |
| 114 | unlock_mm_switch(flags); |
| 115 | } |
| 116 | |
Bernd Schmidt | b97b8a9 | 2008-01-27 18:39:16 +0800 | [diff] [blame] | 117 | static inline void protect_page(struct mm_struct *mm, unsigned long addr, |
| 118 | unsigned long flags) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 119 | { |
Bernd Schmidt | b97b8a9 | 2008-01-27 18:39:16 +0800 | [diff] [blame] | 120 | unsigned long *mask = mm->context.page_rwx_mask; |
Barry Song | e18e7dd | 2009-12-07 10:05:58 +0000 | [diff] [blame] | 121 | unsigned long page; |
| 122 | unsigned long idx; |
| 123 | unsigned long bit; |
| 124 | |
| 125 | if (unlikely(addr >= ASYNC_BANK0_BASE && addr < ASYNC_BANK3_BASE + ASYNC_BANK3_SIZE)) |
| 126 | page = (addr - (ASYNC_BANK0_BASE - _ramend)) >> 12; |
| 127 | else |
| 128 | page = addr >> 12; |
| 129 | idx = page >> 5; |
| 130 | bit = 1 << (page & 31); |
Bernd Schmidt | b97b8a9 | 2008-01-27 18:39:16 +0800 | [diff] [blame] | 131 | |
Graf Yang | 36b8412 | 2009-07-21 02:26:57 +0000 | [diff] [blame] | 132 | if (flags & VM_READ) |
Bernd Schmidt | b97b8a9 | 2008-01-27 18:39:16 +0800 | [diff] [blame] | 133 | mask[idx] |= bit; |
| 134 | else |
| 135 | mask[idx] &= ~bit; |
| 136 | mask += page_mask_nelts; |
Graf Yang | 36b8412 | 2009-07-21 02:26:57 +0000 | [diff] [blame] | 137 | if (flags & VM_WRITE) |
Bernd Schmidt | b97b8a9 | 2008-01-27 18:39:16 +0800 | [diff] [blame] | 138 | mask[idx] |= bit; |
| 139 | else |
| 140 | mask[idx] &= ~bit; |
| 141 | mask += page_mask_nelts; |
Graf Yang | 36b8412 | 2009-07-21 02:26:57 +0000 | [diff] [blame] | 142 | if (flags & VM_EXEC) |
Bernd Schmidt | b97b8a9 | 2008-01-27 18:39:16 +0800 | [diff] [blame] | 143 | mask[idx] |= bit; |
| 144 | else |
| 145 | mask[idx] &= ~bit; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Bernd Schmidt | b97b8a9 | 2008-01-27 18:39:16 +0800 | [diff] [blame] | 148 | static inline void update_protections(struct mm_struct *mm) |
| 149 | { |
Graf Yang | b8a9898 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 150 | unsigned int cpu = smp_processor_id(); |
| 151 | if (mm->context.page_rwx_mask == current_rwx_mask[cpu]) { |
| 152 | flush_switched_cplbs(cpu); |
| 153 | set_mask_dcplbs(mm->context.page_rwx_mask, cpu); |
Bernd Schmidt | 3d9b7a5 | 2008-08-25 18:41:15 +0800 | [diff] [blame] | 154 | } |
Bernd Schmidt | b97b8a9 | 2008-01-27 18:39:16 +0800 | [diff] [blame] | 155 | } |
Philippe Gerum | 4815b88 | 2009-10-27 22:05:33 +0100 | [diff] [blame] | 156 | #else /* !CONFIG_MPU */ |
| 157 | static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next, |
| 158 | struct task_struct *tsk) |
| 159 | { |
| 160 | __switch_mm(prev, next, tsk); |
| 161 | } |
Bernd Schmidt | b97b8a9 | 2008-01-27 18:39:16 +0800 | [diff] [blame] | 162 | #endif |
| 163 | |
Graf Yang | ca87b7a | 2008-10-08 17:30:01 +0800 | [diff] [blame] | 164 | static inline void enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk) |
| 165 | { |
| 166 | } |
| 167 | |
| 168 | /* Called when creating a new context during fork() or execve(). */ |
| 169 | static inline int |
| 170 | init_new_context(struct task_struct *tsk, struct mm_struct *mm) |
| 171 | { |
| 172 | #ifdef CONFIG_MPU |
| 173 | unsigned long p = __get_free_pages(GFP_KERNEL, page_mask_order); |
| 174 | mm->context.page_rwx_mask = (unsigned long *)p; |
| 175 | memset(mm->context.page_rwx_mask, 0, |
| 176 | page_mask_nelts * 3 * sizeof(long)); |
| 177 | #endif |
| 178 | return 0; |
| 179 | } |
| 180 | |
| 181 | static inline void destroy_context(struct mm_struct *mm) |
| 182 | { |
| 183 | struct sram_list_struct *tmp; |
Graf Yang | b8a9898 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 184 | #ifdef CONFIG_MPU |
| 185 | unsigned int cpu = smp_processor_id(); |
| 186 | #endif |
Graf Yang | ca87b7a | 2008-10-08 17:30:01 +0800 | [diff] [blame] | 187 | |
| 188 | #ifdef CONFIG_APP_STACK_L1 |
| 189 | if (current_l1_stack_save == mm->context.l1_stack_save) |
| 190 | current_l1_stack_save = 0; |
| 191 | if (mm->context.l1_stack_save) |
| 192 | free_l1stack(); |
| 193 | #endif |
| 194 | |
| 195 | while ((tmp = mm->context.sram_list)) { |
| 196 | mm->context.sram_list = tmp->next; |
| 197 | sram_free(tmp->addr); |
| 198 | kfree(tmp); |
| 199 | } |
| 200 | #ifdef CONFIG_MPU |
Graf Yang | b8a9898 | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 201 | if (current_rwx_mask[cpu] == mm->context.page_rwx_mask) |
| 202 | current_rwx_mask[cpu] = NULL; |
Graf Yang | ca87b7a | 2008-10-08 17:30:01 +0800 | [diff] [blame] | 203 | free_pages((unsigned long)mm->context.page_rwx_mask, page_mask_order); |
| 204 | #endif |
| 205 | } |
| 206 | |
Philippe Gerum | 4815b88 | 2009-10-27 22:05:33 +0100 | [diff] [blame] | 207 | #define ipipe_mm_switch_protect(flags) \ |
| 208 | local_irq_save_hw_cond(flags) |
| 209 | |
| 210 | #define ipipe_mm_switch_unprotect(flags) \ |
| 211 | local_irq_restore_hw_cond(flags) |
| 212 | |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 213 | #endif |