blob: e1a9b4624f919ddae4675b1a8ff122e89944c764 [file] [log] [blame]
Bryan Wu1394f032007-05-06 14:50:22 -07001/*
Robin Getz96f10502009-09-24 14:11:24 +00002 * Copyright 2004-2009 Analog Devices Inc.
Bryan Wu1394f032007-05-06 14:50:22 -07003 *
Robin Getz96f10502009-09-24 14:11:24 +00004 * Licensed under the GPL-2 or later.
Bryan Wu1394f032007-05-06 14:50:22 -07005 */
6
7#ifndef __BLACKFIN_MMU_CONTEXT_H__
8#define __BLACKFIN_MMU_CONTEXT_H__
9
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Bernd Schmidtb97b8a92008-01-27 18:39:16 +080011#include <linux/sched.h>
Bryan Wu1394f032007-05-06 14:50:22 -070012#include <asm/setup.h>
13#include <asm/page.h>
14#include <asm/pgalloc.h>
Bernd Schmidtb97b8a92008-01-27 18:39:16 +080015#include <asm/cplbinit.h>
Barry Songe18e7dd2009-12-07 10:05:58 +000016#include <asm/sections.h>
Bryan Wu1394f032007-05-06 14:50:22 -070017
Graf Yangb8a98982008-11-18 17:48:22 +080018/* 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 Wu1394f032007-05-06 14:50:22 -070022extern void *current_l1_stack_save;
23extern int nr_l1stack_tasks;
24extern void *l1_stack_base;
25extern unsigned long l1_stack_len;
26
27extern int l1sram_free(const void*);
28extern void *l1sram_alloc_max(void*);
29
Bryan Wu1394f032007-05-06 14:50:22 -070030static inline void free_l1stack(void)
31{
32 nr_l1stack_tasks--;
33 if (nr_l1stack_tasks == 0)
34 l1sram_free(l1_stack_base);
35}
Bryan Wu1394f032007-05-06 14:50:22 -070036
37static inline unsigned long
38alloc_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
56static inline int
57activate_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 Schmidtb97b8a92008-01-27 18:39:16 +080068#define activate_mm(prev, next) switch_mm(prev, next, NULL)
69
Philippe Gerum4815b882009-10-27 22:05:33 +010070static inline void __switch_mm(struct mm_struct *prev_mm, struct mm_struct *next_mm,
71 struct task_struct *tsk)
Bryan Wu1394f032007-05-06 14:50:22 -070072{
Graf Yangb8a98982008-11-18 17:48:22 +080073#ifdef CONFIG_MPU
74 unsigned int cpu = smp_processor_id();
75#endif
Bernd Schmidtb97b8a92008-01-27 18:39:16 +080076 if (prev_mm == next_mm)
77 return;
78#ifdef CONFIG_MPU
Graf Yangb8a98982008-11-18 17:48:22 +080079 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 Schmidtb97b8a92008-01-27 18:39:16 +080082 }
83#endif
84
Graf Yangca87b7a2008-10-08 17:30:01 +080085#ifdef CONFIG_APP_STACK_L1
Bernd Schmidtb97b8a92008-01-27 18:39:16 +080086 /* L1 stack switching. */
Bryan Wu1394f032007-05-06 14:50:22 -070087 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 Yangca87b7a2008-10-08 17:30:01 +080096#endif
Bryan Wu1394f032007-05-06 14:50:22 -070097}
98
Philippe Gerum4815b882009-10-27 22:05:33 +010099#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 Schmidtb97b8a92008-01-27 18:39:16 +0800107#ifdef CONFIG_MPU
Philippe Gerum4815b882009-10-27 22:05:33 +0100108static 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 Schmidtb97b8a92008-01-27 18:39:16 +0800117static inline void protect_page(struct mm_struct *mm, unsigned long addr,
118 unsigned long flags)
Bryan Wu1394f032007-05-06 14:50:22 -0700119{
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800120 unsigned long *mask = mm->context.page_rwx_mask;
Barry Songe18e7dd2009-12-07 10:05:58 +0000121 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 Schmidtb97b8a92008-01-27 18:39:16 +0800131
Graf Yang36b84122009-07-21 02:26:57 +0000132 if (flags & VM_READ)
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800133 mask[idx] |= bit;
134 else
135 mask[idx] &= ~bit;
136 mask += page_mask_nelts;
Graf Yang36b84122009-07-21 02:26:57 +0000137 if (flags & VM_WRITE)
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800138 mask[idx] |= bit;
139 else
140 mask[idx] &= ~bit;
141 mask += page_mask_nelts;
Graf Yang36b84122009-07-21 02:26:57 +0000142 if (flags & VM_EXEC)
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800143 mask[idx] |= bit;
144 else
145 mask[idx] &= ~bit;
Bryan Wu1394f032007-05-06 14:50:22 -0700146}
147
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800148static inline void update_protections(struct mm_struct *mm)
149{
Graf Yangb8a98982008-11-18 17:48:22 +0800150 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 Schmidt3d9b7a52008-08-25 18:41:15 +0800154 }
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800155}
Philippe Gerum4815b882009-10-27 22:05:33 +0100156#else /* !CONFIG_MPU */
157static 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 Schmidtb97b8a92008-01-27 18:39:16 +0800162#endif
163
Graf Yangca87b7a2008-10-08 17:30:01 +0800164static 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(). */
169static inline int
170init_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
181static inline void destroy_context(struct mm_struct *mm)
182{
183 struct sram_list_struct *tmp;
Graf Yangb8a98982008-11-18 17:48:22 +0800184#ifdef CONFIG_MPU
185 unsigned int cpu = smp_processor_id();
186#endif
Graf Yangca87b7a2008-10-08 17:30:01 +0800187
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 Yangb8a98982008-11-18 17:48:22 +0800201 if (current_rwx_mask[cpu] == mm->context.page_rwx_mask)
202 current_rwx_mask[cpu] = NULL;
Graf Yangca87b7a2008-10-08 17:30:01 +0800203 free_pages((unsigned long)mm->context.page_rwx_mask, page_mask_order);
204#endif
205}
206
Philippe Gerum4815b882009-10-27 22:05:33 +0100207#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 Wu1394f032007-05-06 14:50:22 -0700213#endif