[Blackfin] arch: fix bug - a crash on bootup with CONFIG_MPU on the BF548
[safe/jmp/linux-2.6] / arch / blackfin / kernel / cplb-mpu / cplbmgr.c
1 /*
2  *               Blackfin CPLB exception handling.
3  *               Copyright 2004-2007 Analog Devices Inc.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, see the file COPYING, or write
17  * to the Free Software Foundation, Inc.,
18  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  */
20 #include <linux/module.h>
21 #include <linux/mm.h>
22
23 #include <asm/blackfin.h>
24 #include <asm/cplbinit.h>
25 #include <asm/mmu_context.h>
26
27 #ifdef CONFIG_BFIN_ICACHE
28
29 #define FAULT_RW        (1 << 16)
30 #define FAULT_USERSUPV  (1 << 17)
31
32 int page_mask_nelts;
33 int page_mask_order;
34 unsigned long *current_rwx_mask;
35
36 int nr_dcplb_miss, nr_icplb_miss, nr_icplb_supv_miss, nr_dcplb_prot;
37 int nr_cplb_flush;
38
39 static inline void disable_dcplb(void)
40 {
41         unsigned long ctrl;
42         SSYNC();
43         ctrl = bfin_read_DMEM_CONTROL();
44         ctrl &= ~ENDCPLB;
45         bfin_write_DMEM_CONTROL(ctrl);
46         SSYNC();
47 }
48
49 static inline void enable_dcplb(void)
50 {
51         unsigned long ctrl;
52         SSYNC();
53         ctrl = bfin_read_DMEM_CONTROL();
54         ctrl |= ENDCPLB;
55         bfin_write_DMEM_CONTROL(ctrl);
56         SSYNC();
57 }
58
59 static inline void disable_icplb(void)
60 {
61         unsigned long ctrl;
62         SSYNC();
63         ctrl = bfin_read_IMEM_CONTROL();
64         ctrl &= ~ENICPLB;
65         bfin_write_IMEM_CONTROL(ctrl);
66         SSYNC();
67 }
68
69 static inline void enable_icplb(void)
70 {
71         unsigned long ctrl;
72         SSYNC();
73         ctrl = bfin_read_IMEM_CONTROL();
74         ctrl |= ENICPLB;
75         bfin_write_IMEM_CONTROL(ctrl);
76         SSYNC();
77 }
78
79 /*
80  * Given the contents of the status register, return the index of the
81  * CPLB that caused the fault.
82  */
83 static inline int faulting_cplb_index(int status)
84 {
85         int signbits = __builtin_bfin_norm_fr1x32(status & 0xFFFF);
86         return 30 - signbits;
87 }
88
89 /*
90  * Given the contents of the status register and the DCPLB_DATA contents,
91  * return true if a write access should be permitted.
92  */
93 static inline int write_permitted(int status, unsigned long data)
94 {
95         if (status & FAULT_USERSUPV)
96                 return !!(data & CPLB_SUPV_WR);
97         else
98                 return !!(data & CPLB_USER_WR);
99 }
100
101 /* Counters to implement round-robin replacement.  */
102 static int icplb_rr_index, dcplb_rr_index;
103
104 /*
105  * Find an ICPLB entry to be evicted and return its index.
106  */
107 static int evict_one_icplb(void)
108 {
109         int i;
110         for (i = first_switched_icplb; i < MAX_CPLBS; i++)
111                 if ((icplb_tbl[i].data & CPLB_VALID) == 0)
112                         return i;
113         i = first_switched_icplb + icplb_rr_index;
114         if (i >= MAX_CPLBS) {
115                 i -= MAX_CPLBS - first_switched_icplb;
116                 icplb_rr_index -= MAX_CPLBS - first_switched_icplb;
117         }
118         icplb_rr_index++;
119         return i;
120 }
121
122 static int evict_one_dcplb(void)
123 {
124         int i;
125         for (i = first_switched_dcplb; i < MAX_CPLBS; i++)
126                 if ((dcplb_tbl[i].data & CPLB_VALID) == 0)
127                         return i;
128         i = first_switched_dcplb + dcplb_rr_index;
129         if (i >= MAX_CPLBS) {
130                 i -= MAX_CPLBS - first_switched_dcplb;
131                 dcplb_rr_index -= MAX_CPLBS - first_switched_dcplb;
132         }
133         dcplb_rr_index++;
134         return i;
135 }
136
137 static noinline int dcplb_miss(void)
138 {
139         unsigned long addr = bfin_read_DCPLB_FAULT_ADDR();
140         int status = bfin_read_DCPLB_STATUS();
141         unsigned long *mask;
142         int idx;
143         unsigned long d_data;
144
145         nr_dcplb_miss++;
146
147         d_data = CPLB_SUPV_WR | CPLB_VALID | CPLB_DIRTY | PAGE_SIZE_4KB;
148 #ifdef CONFIG_BFIN_DCACHE
149         if (addr < _ramend - DMA_UNCACHED_REGION) {
150                 d_data |= CPLB_L1_CHBL | ANOMALY_05000158_WORKAROUND;
151 #ifdef CONFIG_BFIN_WT
152                 d_data |= CPLB_L1_AOW | CPLB_WT;
153 #endif
154         }
155 #endif
156         if (addr >= _ramend) {
157                 if (addr >= ASYNC_BANK0_BASE && addr < ASYNC_BANK3_BASE + ASYNC_BANK3_SIZE
158                     && (status & FAULT_USERSUPV)) {
159                         addr &= ~0x3fffff;
160                         d_data &= ~PAGE_SIZE_4KB;
161                         d_data |= PAGE_SIZE_4MB;
162                 } else
163                         return CPLB_PROT_VIOL;
164         } else {
165                 mask = current_rwx_mask;
166                 if (mask) {
167                         int page = addr >> PAGE_SHIFT;
168                         int offs = page >> 5;
169                         int bit = 1 << (page & 31);
170
171                         if (mask[offs] & bit)
172                                 d_data |= CPLB_USER_RD;
173
174                         mask += page_mask_nelts;
175                         if (mask[offs] & bit)
176                                 d_data |= CPLB_USER_WR;
177                 }
178         }
179         idx = evict_one_dcplb();
180
181         addr &= PAGE_MASK;
182         dcplb_tbl[idx].addr = addr;
183         dcplb_tbl[idx].data = d_data;
184
185         disable_dcplb();
186         bfin_write32(DCPLB_DATA0 + idx * 4, d_data);
187         bfin_write32(DCPLB_ADDR0 + idx * 4, addr);
188         enable_dcplb();
189
190         return 0;
191 }
192
193 static noinline int icplb_miss(void)
194 {
195         unsigned long addr = bfin_read_ICPLB_FAULT_ADDR();
196         int status = bfin_read_ICPLB_STATUS();
197         int idx;
198         unsigned long i_data;
199
200         nr_icplb_miss++;
201         if (status & FAULT_USERSUPV)
202                 nr_icplb_supv_miss++;
203
204         if (addr >= _ramend)
205                 return CPLB_PROT_VIOL;
206
207         /*
208          * First, try to find a CPLB that matches this address.  If we
209          * find one, then the fact that we're in the miss handler means
210          * that the instruction crosses a page boundary.
211          */
212         for (idx = first_switched_icplb; idx < MAX_CPLBS; idx++) {
213                 if (icplb_tbl[idx].data & CPLB_VALID) {
214                         unsigned long this_addr = icplb_tbl[idx].addr;
215                         if (this_addr <= addr && this_addr + PAGE_SIZE > addr) {
216                                 addr += PAGE_SIZE;
217                                 break;
218                         }
219                 }
220         }
221
222         i_data = CPLB_VALID | CPLB_PORTPRIO | PAGE_SIZE_4KB;
223 #ifdef CONFIG_BFIN_ICACHE
224         i_data |= CPLB_L1_CHBL | ANOMALY_05000158_WORKAROUND;
225 #endif
226
227         /*
228          * Two cases to distinguish - a supervisor access must necessarily
229          * be for a module page; we grant it unconditionally (could do better
230          * here in the future).  Otherwise, check the x bitmap of the current
231          * process.
232          */
233         if (!(status & FAULT_USERSUPV)) {
234                 unsigned long *mask = current_rwx_mask;
235
236                 if (mask) {
237                         int page = addr >> PAGE_SHIFT;
238                         int offs = page >> 5;
239                         int bit = 1 << (page & 31);
240
241                         mask += 2 * page_mask_nelts;
242                         if (mask[offs] & bit)
243                                 i_data |= CPLB_USER_RD;
244                 }
245         }
246
247         idx = evict_one_icplb();
248         addr &= PAGE_MASK;
249         icplb_tbl[idx].addr = addr;
250         icplb_tbl[idx].data = i_data;
251
252         disable_icplb();
253         bfin_write32(ICPLB_DATA0 + idx * 4, i_data);
254         bfin_write32(ICPLB_ADDR0 + idx * 4, addr);
255         enable_icplb();
256
257         return 0;
258 }
259
260 static noinline int dcplb_protection_fault(void)
261 {
262         unsigned long addr = bfin_read_DCPLB_FAULT_ADDR();
263         int status = bfin_read_DCPLB_STATUS();
264
265         nr_dcplb_prot++;
266
267         if (status & FAULT_RW) {
268                 int idx = faulting_cplb_index(status);
269                 unsigned long data = dcplb_tbl[idx].data;
270                 if (!(data & CPLB_WT) && !(data & CPLB_DIRTY) &&
271                     write_permitted(status, data)) {
272                         data |= CPLB_DIRTY;
273                         dcplb_tbl[idx].data = data;
274                         bfin_write32(DCPLB_DATA0 + idx * 4, data);
275                         return 0;
276                 }
277         }
278         return CPLB_PROT_VIOL;
279 }
280
281 int cplb_hdr(int seqstat, struct pt_regs *regs)
282 {
283         int cause = seqstat & 0x3f;
284         switch (cause) {
285         case 0x23:
286                 return dcplb_protection_fault();
287         case 0x2C:
288                 return icplb_miss();
289         case 0x26:
290                 return dcplb_miss();
291         default:
292                 return 1;
293         }
294 }
295
296 void flush_switched_cplbs(void)
297 {
298         int i;
299
300         nr_cplb_flush++;
301
302         disable_icplb();
303         for (i = first_switched_icplb; i < MAX_CPLBS; i++) {
304                 icplb_tbl[i].data = 0;
305                 bfin_write32(ICPLB_DATA0 + i * 4, 0);
306         }
307         enable_icplb();
308
309         disable_dcplb();
310         for (i = first_switched_dcplb; i < MAX_CPLBS; i++) {
311                 dcplb_tbl[i].data = 0;
312                 bfin_write32(DCPLB_DATA0 + i * 4, 0);
313         }
314         enable_dcplb();
315 }
316
317 void set_mask_dcplbs(unsigned long *masks)
318 {
319         int i;
320         unsigned long addr = (unsigned long)masks;
321         unsigned long d_data;
322         current_rwx_mask = masks;
323
324         if (!masks)
325                 return;
326
327         d_data = CPLB_SUPV_WR | CPLB_VALID | CPLB_DIRTY | PAGE_SIZE_4KB;
328 #ifdef CONFIG_BFIN_DCACHE
329         d_data |= CPLB_L1_CHBL;
330 #ifdef CONFIG_BFIN_WT
331         d_data |= CPLB_L1_AOW | CPLB_WT;
332 #endif
333 #endif
334
335         disable_dcplb();
336         for (i = first_mask_dcplb; i < first_switched_dcplb; i++) {
337                 dcplb_tbl[i].addr = addr;
338                 dcplb_tbl[i].data = d_data;
339                 bfin_write32(DCPLB_DATA0 + i * 4, d_data);
340                 bfin_write32(DCPLB_ADDR0 + i * 4, addr);
341                 addr += PAGE_SIZE;
342         }
343         enable_dcplb();
344 }
345
346 #endif