blob: 3b165bbe90fa96ed64ad3121131a1b445166546e [file] [log] [blame]
Bernd Schmidtb97b8a92008-01-27 18:39:16 +08001/*
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
32int page_mask_nelts;
33int page_mask_order;
34unsigned long *current_rwx_mask;
35
36int nr_dcplb_miss, nr_icplb_miss, nr_icplb_supv_miss, nr_dcplb_prot;
37int nr_cplb_flush;
38
39static 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
49static 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
59static 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
69static 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 */
83static 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 */
93static 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. */
102static int icplb_rr_index, dcplb_rr_index;
103
104/*
105 * Find an ICPLB entry to be evicted and return its index.
106 */
107static 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
122static 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
137static 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++;
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800146
147 d_data = CPLB_SUPV_WR | CPLB_VALID | CPLB_DIRTY | PAGE_SIZE_4KB;
148#ifdef CONFIG_BFIN_DCACHE
Bernd Schmidt1ebc7232008-04-24 02:58:26 +0800149 if (addr < _ramend - DMA_UNCACHED_REGION ||
150 (reserved_mem_dcache_on && addr >= _ramend &&
151 addr < physical_mem_end)) {
Bernd Schmidtb4bb68f2008-04-23 07:26:23 +0800152 d_data |= CPLB_L1_CHBL | ANOMALY_05000158_WORKAROUND;
Bernd Schmidtdbfe44f2008-04-23 07:11:55 +0800153#ifdef CONFIG_BFIN_WT
Bernd Schmidtb4bb68f2008-04-23 07:26:23 +0800154 d_data |= CPLB_L1_AOW | CPLB_WT;
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800155#endif
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800156 }
Bernd Schmidtb4bb68f2008-04-23 07:26:23 +0800157#endif
Bernd Schmidt1ebc7232008-04-24 02:58:26 +0800158 if (addr >= physical_mem_end) {
Bernd Schmidtb4bb68f2008-04-23 07:26:23 +0800159 if (addr >= ASYNC_BANK0_BASE && addr < ASYNC_BANK3_BASE + ASYNC_BANK3_SIZE
160 && (status & FAULT_USERSUPV)) {
161 addr &= ~0x3fffff;
162 d_data &= ~PAGE_SIZE_4KB;
163 d_data |= PAGE_SIZE_4MB;
Mike Frysinger4e354b52008-04-24 05:44:32 +0800164 } else if (addr >= BOOT_ROM_START && addr < BOOT_ROM_START + BOOT_ROM_LENGTH
165 && (status & (FAULT_RW | FAULT_USERSUPV)) == FAULT_USERSUPV) {
166 addr &= ~(1 * 1024 * 1024 - 1);
167 d_data &= ~PAGE_SIZE_4KB;
168 d_data |= PAGE_SIZE_1MB | CPLB_USER_RD;
Bernd Schmidtb4bb68f2008-04-23 07:26:23 +0800169 } else
170 return CPLB_PROT_VIOL;
Bernd Schmidt1ebc7232008-04-24 02:58:26 +0800171 } else if (addr >= _ramend) {
172 d_data |= CPLB_USER_RD | CPLB_USER_WR;
Bernd Schmidtb4bb68f2008-04-23 07:26:23 +0800173 } else {
174 mask = current_rwx_mask;
175 if (mask) {
176 int page = addr >> PAGE_SHIFT;
177 int offs = page >> 5;
178 int bit = 1 << (page & 31);
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800179
Bernd Schmidtb4bb68f2008-04-23 07:26:23 +0800180 if (mask[offs] & bit)
181 d_data |= CPLB_USER_RD;
182
183 mask += page_mask_nelts;
184 if (mask[offs] & bit)
185 d_data |= CPLB_USER_WR;
186 }
187 }
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800188 idx = evict_one_dcplb();
189
190 addr &= PAGE_MASK;
191 dcplb_tbl[idx].addr = addr;
192 dcplb_tbl[idx].data = d_data;
193
194 disable_dcplb();
195 bfin_write32(DCPLB_DATA0 + idx * 4, d_data);
196 bfin_write32(DCPLB_ADDR0 + idx * 4, addr);
197 enable_dcplb();
198
199 return 0;
200}
201
202static noinline int icplb_miss(void)
203{
204 unsigned long addr = bfin_read_ICPLB_FAULT_ADDR();
205 int status = bfin_read_ICPLB_STATUS();
206 int idx;
207 unsigned long i_data;
208
209 nr_icplb_miss++;
Bernd Schmidt1ebc7232008-04-24 02:58:26 +0800210
211 /* If inside the uncached DMA region, fault. */
212 if (addr >= _ramend - DMA_UNCACHED_REGION && addr < _ramend)
213 return CPLB_PROT_VIOL;
214
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800215 if (status & FAULT_USERSUPV)
216 nr_icplb_supv_miss++;
217
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800218 /*
219 * First, try to find a CPLB that matches this address. If we
220 * find one, then the fact that we're in the miss handler means
221 * that the instruction crosses a page boundary.
222 */
223 for (idx = first_switched_icplb; idx < MAX_CPLBS; idx++) {
224 if (icplb_tbl[idx].data & CPLB_VALID) {
225 unsigned long this_addr = icplb_tbl[idx].addr;
226 if (this_addr <= addr && this_addr + PAGE_SIZE > addr) {
227 addr += PAGE_SIZE;
228 break;
229 }
230 }
231 }
232
233 i_data = CPLB_VALID | CPLB_PORTPRIO | PAGE_SIZE_4KB;
Bernd Schmidt1ebc7232008-04-24 02:58:26 +0800234
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800235#ifdef CONFIG_BFIN_ICACHE
Bernd Schmidt1ebc7232008-04-24 02:58:26 +0800236 /*
237 * Normal RAM, and possibly the reserved memory area, are
238 * cacheable.
239 */
240 if (addr < _ramend ||
241 (addr < physical_mem_end && reserved_mem_icache_on))
242 i_data |= CPLB_L1_CHBL | ANOMALY_05000158_WORKAROUND;
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800243#endif
244
Bernd Schmidt1ebc7232008-04-24 02:58:26 +0800245 if (addr >= physical_mem_end) {
246 return CPLB_PROT_VIOL;
247 } else if (addr >= _ramend) {
248 i_data |= CPLB_USER_RD;
249 } else {
250 /*
251 * Two cases to distinguish - a supervisor access must
252 * necessarily be for a module page; we grant it
253 * unconditionally (could do better here in the future).
254 * Otherwise, check the x bitmap of the current process.
255 */
256 if (!(status & FAULT_USERSUPV)) {
257 unsigned long *mask = current_rwx_mask;
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800258
Bernd Schmidt1ebc7232008-04-24 02:58:26 +0800259 if (mask) {
260 int page = addr >> PAGE_SHIFT;
261 int offs = page >> 5;
262 int bit = 1 << (page & 31);
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800263
Bernd Schmidt1ebc7232008-04-24 02:58:26 +0800264 mask += 2 * page_mask_nelts;
265 if (mask[offs] & bit)
266 i_data |= CPLB_USER_RD;
267 }
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800268 }
269 }
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800270 idx = evict_one_icplb();
271 addr &= PAGE_MASK;
272 icplb_tbl[idx].addr = addr;
273 icplb_tbl[idx].data = i_data;
274
275 disable_icplb();
276 bfin_write32(ICPLB_DATA0 + idx * 4, i_data);
277 bfin_write32(ICPLB_ADDR0 + idx * 4, addr);
278 enable_icplb();
279
280 return 0;
281}
282
283static noinline int dcplb_protection_fault(void)
284{
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800285 int status = bfin_read_DCPLB_STATUS();
286
287 nr_dcplb_prot++;
288
289 if (status & FAULT_RW) {
290 int idx = faulting_cplb_index(status);
291 unsigned long data = dcplb_tbl[idx].data;
292 if (!(data & CPLB_WT) && !(data & CPLB_DIRTY) &&
293 write_permitted(status, data)) {
294 data |= CPLB_DIRTY;
295 dcplb_tbl[idx].data = data;
296 bfin_write32(DCPLB_DATA0 + idx * 4, data);
297 return 0;
298 }
299 }
300 return CPLB_PROT_VIOL;
301}
302
303int cplb_hdr(int seqstat, struct pt_regs *regs)
304{
305 int cause = seqstat & 0x3f;
306 switch (cause) {
307 case 0x23:
308 return dcplb_protection_fault();
309 case 0x2C:
310 return icplb_miss();
311 case 0x26:
312 return dcplb_miss();
313 default:
Bernd Schmidtb4bb68f2008-04-23 07:26:23 +0800314 return 1;
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800315 }
316}
317
318void flush_switched_cplbs(void)
319{
320 int i;
321
322 nr_cplb_flush++;
323
324 disable_icplb();
325 for (i = first_switched_icplb; i < MAX_CPLBS; i++) {
326 icplb_tbl[i].data = 0;
327 bfin_write32(ICPLB_DATA0 + i * 4, 0);
328 }
329 enable_icplb();
330
331 disable_dcplb();
Bernd Schmidtd56daae2008-04-24 02:56:36 +0800332 for (i = first_switched_dcplb; i < MAX_CPLBS; i++) {
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800333 dcplb_tbl[i].data = 0;
334 bfin_write32(DCPLB_DATA0 + i * 4, 0);
335 }
336 enable_dcplb();
337}
338
339void set_mask_dcplbs(unsigned long *masks)
340{
341 int i;
342 unsigned long addr = (unsigned long)masks;
343 unsigned long d_data;
344 current_rwx_mask = masks;
345
346 if (!masks)
347 return;
348
349 d_data = CPLB_SUPV_WR | CPLB_VALID | CPLB_DIRTY | PAGE_SIZE_4KB;
350#ifdef CONFIG_BFIN_DCACHE
351 d_data |= CPLB_L1_CHBL;
Bernd Schmidtdbfe44f2008-04-23 07:11:55 +0800352#ifdef CONFIG_BFIN_WT
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800353 d_data |= CPLB_L1_AOW | CPLB_WT;
354#endif
355#endif
356
357 disable_dcplb();
358 for (i = first_mask_dcplb; i < first_switched_dcplb; i++) {
359 dcplb_tbl[i].addr = addr;
360 dcplb_tbl[i].data = d_data;
361 bfin_write32(DCPLB_DATA0 + i * 4, d_data);
362 bfin_write32(DCPLB_ADDR0 + i * 4, addr);
363 addr += PAGE_SIZE;
364 }
365 enable_dcplb();
366}
367
368#endif