2 * Blackfin CPLB exception handling.
3 * Copyright 2004-2007 Analog Devices Inc.
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.
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.
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
20 #include <linux/module.h>
23 #include <asm/blackfin.h>
24 #include <asm/cplbinit.h>
25 #include <asm/mmu_context.h>
27 #ifdef CONFIG_BFIN_ICACHE
29 #define FAULT_RW (1 << 16)
30 #define FAULT_USERSUPV (1 << 17)
34 unsigned long *current_rwx_mask;
36 int nr_dcplb_miss, nr_icplb_miss, nr_icplb_supv_miss, nr_dcplb_prot;
39 static inline void disable_dcplb(void)
43 ctrl = bfin_read_DMEM_CONTROL();
45 bfin_write_DMEM_CONTROL(ctrl);
49 static inline void enable_dcplb(void)
53 ctrl = bfin_read_DMEM_CONTROL();
55 bfin_write_DMEM_CONTROL(ctrl);
59 static inline void disable_icplb(void)
63 ctrl = bfin_read_IMEM_CONTROL();
65 bfin_write_IMEM_CONTROL(ctrl);
69 static inline void enable_icplb(void)
73 ctrl = bfin_read_IMEM_CONTROL();
75 bfin_write_IMEM_CONTROL(ctrl);
80 * Given the contents of the status register, return the index of the
81 * CPLB that caused the fault.
83 static inline int faulting_cplb_index(int status)
85 int signbits = __builtin_bfin_norm_fr1x32(status & 0xFFFF);
90 * Given the contents of the status register and the DCPLB_DATA contents,
91 * return true if a write access should be permitted.
93 static inline int write_permitted(int status, unsigned long data)
95 if (status & FAULT_USERSUPV)
96 return !!(data & CPLB_SUPV_WR);
98 return !!(data & CPLB_USER_WR);
101 /* Counters to implement round-robin replacement. */
102 static int icplb_rr_index, dcplb_rr_index;
105 * Find an ICPLB entry to be evicted and return its index.
107 static int evict_one_icplb(void)
110 for (i = first_switched_icplb; i < MAX_CPLBS; i++)
111 if ((icplb_tbl[i].data & CPLB_VALID) == 0)
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;
122 static int evict_one_dcplb(void)
125 for (i = first_switched_dcplb; i < MAX_CPLBS; i++)
126 if ((dcplb_tbl[i].data & CPLB_VALID) == 0)
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;
137 static noinline int dcplb_miss(void)
139 unsigned long addr = bfin_read_DCPLB_FAULT_ADDR();
140 int status = bfin_read_DCPLB_STATUS();
143 unsigned long d_data;
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 (reserved_mem_dcache_on && addr >= _ramend &&
151 addr < physical_mem_end)) {
152 d_data |= CPLB_L1_CHBL | ANOMALY_05000158_WORKAROUND;
153 #ifdef CONFIG_BFIN_WT
154 d_data |= CPLB_L1_AOW | CPLB_WT;
158 if (addr >= physical_mem_end) {
159 if (addr >= ASYNC_BANK0_BASE && addr < ASYNC_BANK3_BASE + ASYNC_BANK3_SIZE
160 && (status & FAULT_USERSUPV)) {
162 d_data &= ~PAGE_SIZE_4KB;
163 d_data |= PAGE_SIZE_4MB;
164 } 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;
170 return CPLB_PROT_VIOL;
171 } else if (addr >= _ramend) {
172 d_data |= CPLB_USER_RD | CPLB_USER_WR;
174 mask = current_rwx_mask;
176 int page = addr >> PAGE_SHIFT;
177 int offs = page >> 5;
178 int bit = 1 << (page & 31);
180 if (mask[offs] & bit)
181 d_data |= CPLB_USER_RD;
183 mask += page_mask_nelts;
184 if (mask[offs] & bit)
185 d_data |= CPLB_USER_WR;
188 idx = evict_one_dcplb();
191 dcplb_tbl[idx].addr = addr;
192 dcplb_tbl[idx].data = d_data;
195 bfin_write32(DCPLB_DATA0 + idx * 4, d_data);
196 bfin_write32(DCPLB_ADDR0 + idx * 4, addr);
202 static noinline int icplb_miss(void)
204 unsigned long addr = bfin_read_ICPLB_FAULT_ADDR();
205 int status = bfin_read_ICPLB_STATUS();
207 unsigned long i_data;
211 /* If inside the uncached DMA region, fault. */
212 if (addr >= _ramend - DMA_UNCACHED_REGION && addr < _ramend)
213 return CPLB_PROT_VIOL;
215 if (status & FAULT_USERSUPV)
216 nr_icplb_supv_miss++;
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.
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) {
233 i_data = CPLB_VALID | CPLB_PORTPRIO | PAGE_SIZE_4KB;
235 #ifdef CONFIG_BFIN_ICACHE
237 * Normal RAM, and possibly the reserved memory area, are
240 if (addr < _ramend ||
241 (addr < physical_mem_end && reserved_mem_icache_on))
242 i_data |= CPLB_L1_CHBL | ANOMALY_05000158_WORKAROUND;
245 if (addr >= physical_mem_end) {
246 if (addr >= BOOT_ROM_START && addr < BOOT_ROM_START + BOOT_ROM_LENGTH
247 && (status & FAULT_USERSUPV)) {
248 addr &= ~(1 * 1024 * 1024 - 1);
249 i_data &= ~PAGE_SIZE_4KB;
250 i_data |= PAGE_SIZE_1MB;
252 return CPLB_PROT_VIOL;
253 } else if (addr >= _ramend) {
254 i_data |= CPLB_USER_RD;
257 * Two cases to distinguish - a supervisor access must
258 * necessarily be for a module page; we grant it
259 * unconditionally (could do better here in the future).
260 * Otherwise, check the x bitmap of the current process.
262 if (!(status & FAULT_USERSUPV)) {
263 unsigned long *mask = current_rwx_mask;
266 int page = addr >> PAGE_SHIFT;
267 int offs = page >> 5;
268 int bit = 1 << (page & 31);
270 mask += 2 * page_mask_nelts;
271 if (mask[offs] & bit)
272 i_data |= CPLB_USER_RD;
276 idx = evict_one_icplb();
278 icplb_tbl[idx].addr = addr;
279 icplb_tbl[idx].data = i_data;
282 bfin_write32(ICPLB_DATA0 + idx * 4, i_data);
283 bfin_write32(ICPLB_ADDR0 + idx * 4, addr);
289 static noinline int dcplb_protection_fault(void)
291 int status = bfin_read_DCPLB_STATUS();
295 if (status & FAULT_RW) {
296 int idx = faulting_cplb_index(status);
297 unsigned long data = dcplb_tbl[idx].data;
298 if (!(data & CPLB_WT) && !(data & CPLB_DIRTY) &&
299 write_permitted(status, data)) {
301 dcplb_tbl[idx].data = data;
302 bfin_write32(DCPLB_DATA0 + idx * 4, data);
306 return CPLB_PROT_VIOL;
309 int cplb_hdr(int seqstat, struct pt_regs *regs)
311 int cause = seqstat & 0x3f;
314 return dcplb_protection_fault();
324 void flush_switched_cplbs(void)
331 for (i = first_switched_icplb; i < MAX_CPLBS; i++) {
332 icplb_tbl[i].data = 0;
333 bfin_write32(ICPLB_DATA0 + i * 4, 0);
338 for (i = first_switched_dcplb; i < MAX_CPLBS; i++) {
339 dcplb_tbl[i].data = 0;
340 bfin_write32(DCPLB_DATA0 + i * 4, 0);
345 void set_mask_dcplbs(unsigned long *masks)
348 unsigned long addr = (unsigned long)masks;
349 unsigned long d_data;
350 current_rwx_mask = masks;
355 d_data = CPLB_SUPV_WR | CPLB_VALID | CPLB_DIRTY | PAGE_SIZE_4KB;
356 #ifdef CONFIG_BFIN_DCACHE
357 d_data |= CPLB_L1_CHBL;
358 #ifdef CONFIG_BFIN_WT
359 d_data |= CPLB_L1_AOW | CPLB_WT;
364 for (i = first_mask_dcplb; i < first_switched_dcplb; i++) {
365 dcplb_tbl[i].addr = addr;
366 dcplb_tbl[i].data = d_data;
367 bfin_write32(DCPLB_DATA0 + i * 4, d_data);
368 bfin_write32(DCPLB_ADDR0 + i * 4, addr);