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