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/cacheflush.h>
25 #include <asm/cplbinit.h>
26 #include <asm/mmu_context.h>
31 * This file is compiled with certain -ffixed-reg options. We have to
32 * make sure not to call any functions here that could clobber these
38 unsigned long *current_rwx_mask[NR_CPUS];
40 int nr_dcplb_miss[NR_CPUS], nr_icplb_miss[NR_CPUS];
41 int nr_icplb_supv_miss[NR_CPUS], nr_dcplb_prot[NR_CPUS];
42 int nr_cplb_flush[NR_CPUS];
44 static inline void disable_dcplb(void)
48 ctrl = bfin_read_DMEM_CONTROL();
50 bfin_write_DMEM_CONTROL(ctrl);
54 static inline void enable_dcplb(void)
58 ctrl = bfin_read_DMEM_CONTROL();
60 bfin_write_DMEM_CONTROL(ctrl);
64 static inline void disable_icplb(void)
68 ctrl = bfin_read_IMEM_CONTROL();
70 bfin_write_IMEM_CONTROL(ctrl);
74 static inline void enable_icplb(void)
78 ctrl = bfin_read_IMEM_CONTROL();
80 bfin_write_IMEM_CONTROL(ctrl);
85 * Given the contents of the status register, return the index of the
86 * CPLB that caused the fault.
88 static inline int faulting_cplb_index(int status)
90 int signbits = __builtin_bfin_norm_fr1x32(status & 0xFFFF);
95 * Given the contents of the status register and the DCPLB_DATA contents,
96 * return true if a write access should be permitted.
98 static inline int write_permitted(int status, unsigned long data)
100 if (status & FAULT_USERSUPV)
101 return !!(data & CPLB_SUPV_WR);
103 return !!(data & CPLB_USER_WR);
106 /* Counters to implement round-robin replacement. */
107 static int icplb_rr_index[NR_CPUS], dcplb_rr_index[NR_CPUS];
110 * Find an ICPLB entry to be evicted and return its index.
112 static int evict_one_icplb(unsigned int cpu)
115 for (i = first_switched_icplb; i < MAX_CPLBS; i++)
116 if ((icplb_tbl[cpu][i].data & CPLB_VALID) == 0)
118 i = first_switched_icplb + icplb_rr_index[cpu];
119 if (i >= MAX_CPLBS) {
120 i -= MAX_CPLBS - first_switched_icplb;
121 icplb_rr_index[cpu] -= MAX_CPLBS - first_switched_icplb;
123 icplb_rr_index[cpu]++;
127 static int evict_one_dcplb(unsigned int cpu)
130 for (i = first_switched_dcplb; i < MAX_CPLBS; i++)
131 if ((dcplb_tbl[cpu][i].data & CPLB_VALID) == 0)
133 i = first_switched_dcplb + dcplb_rr_index[cpu];
134 if (i >= MAX_CPLBS) {
135 i -= MAX_CPLBS - first_switched_dcplb;
136 dcplb_rr_index[cpu] -= MAX_CPLBS - first_switched_dcplb;
138 dcplb_rr_index[cpu]++;
142 static noinline int dcplb_miss(unsigned int cpu)
144 unsigned long addr = bfin_read_DCPLB_FAULT_ADDR();
145 int status = bfin_read_DCPLB_STATUS();
148 unsigned long d_data;
150 nr_dcplb_miss[cpu]++;
152 d_data = CPLB_SUPV_WR | CPLB_VALID | CPLB_DIRTY | PAGE_SIZE_4KB;
153 #ifdef CONFIG_BFIN_DCACHE
154 if (bfin_addr_dcachable(addr)) {
155 d_data |= CPLB_L1_CHBL | ANOMALY_05000158_WORKAROUND;
156 #ifdef CONFIG_BFIN_WT
157 d_data |= CPLB_L1_AOW | CPLB_WT;
161 if (addr >= physical_mem_end) {
162 if (addr >= ASYNC_BANK0_BASE && addr < ASYNC_BANK3_BASE + ASYNC_BANK3_SIZE
163 && (status & FAULT_USERSUPV)) {
165 d_data &= ~PAGE_SIZE_4KB;
166 d_data |= PAGE_SIZE_4MB;
167 } else if (addr >= BOOT_ROM_START && addr < BOOT_ROM_START + BOOT_ROM_LENGTH
168 && (status & (FAULT_RW | FAULT_USERSUPV)) == FAULT_USERSUPV) {
169 addr &= ~(1 * 1024 * 1024 - 1);
170 d_data &= ~PAGE_SIZE_4KB;
171 d_data |= PAGE_SIZE_1MB;
173 return CPLB_PROT_VIOL;
174 } else if (addr >= _ramend) {
175 d_data |= CPLB_USER_RD | CPLB_USER_WR;
177 mask = current_rwx_mask[cpu];
179 int page = addr >> PAGE_SHIFT;
181 int bit = 1 << (page & 31);
184 d_data |= CPLB_USER_RD;
186 mask += page_mask_nelts;
188 d_data |= CPLB_USER_WR;
191 idx = evict_one_dcplb(cpu);
194 dcplb_tbl[cpu][idx].addr = addr;
195 dcplb_tbl[cpu][idx].data = d_data;
198 bfin_write32(DCPLB_DATA0 + idx * 4, d_data);
199 bfin_write32(DCPLB_ADDR0 + idx * 4, addr);
205 static noinline int icplb_miss(unsigned int cpu)
207 unsigned long addr = bfin_read_ICPLB_FAULT_ADDR();
208 int status = bfin_read_ICPLB_STATUS();
210 unsigned long i_data;
212 nr_icplb_miss[cpu]++;
214 /* If inside the uncached DMA region, fault. */
215 if (addr >= _ramend - DMA_UNCACHED_REGION && addr < _ramend)
216 return CPLB_PROT_VIOL;
218 if (status & FAULT_USERSUPV)
219 nr_icplb_supv_miss[cpu]++;
222 * First, try to find a CPLB that matches this address. If we
223 * find one, then the fact that we're in the miss handler means
224 * that the instruction crosses a page boundary.
226 for (idx = first_switched_icplb; idx < MAX_CPLBS; idx++) {
227 if (icplb_tbl[cpu][idx].data & CPLB_VALID) {
228 unsigned long this_addr = icplb_tbl[cpu][idx].addr;
229 if (this_addr <= addr && this_addr + PAGE_SIZE > addr) {
236 i_data = CPLB_VALID | CPLB_PORTPRIO | PAGE_SIZE_4KB;
238 #ifdef CONFIG_BFIN_ICACHE
240 * Normal RAM, and possibly the reserved memory area, are
243 if (addr < _ramend ||
244 (addr < physical_mem_end && reserved_mem_icache_on))
245 i_data |= CPLB_L1_CHBL | ANOMALY_05000158_WORKAROUND;
248 if (addr >= physical_mem_end) {
249 if (addr >= BOOT_ROM_START && addr < BOOT_ROM_START + BOOT_ROM_LENGTH
250 && (status & FAULT_USERSUPV)) {
251 addr &= ~(1 * 1024 * 1024 - 1);
252 i_data &= ~PAGE_SIZE_4KB;
253 i_data |= PAGE_SIZE_1MB;
255 return CPLB_PROT_VIOL;
256 } else if (addr >= _ramend) {
257 i_data |= CPLB_USER_RD;
260 * Two cases to distinguish - a supervisor access must
261 * necessarily be for a module page; we grant it
262 * unconditionally (could do better here in the future).
263 * Otherwise, check the x bitmap of the current process.
265 if (!(status & FAULT_USERSUPV)) {
266 unsigned long *mask = current_rwx_mask[cpu];
269 int page = addr >> PAGE_SHIFT;
271 int bit = 1 << (page & 31);
273 mask += 2 * page_mask_nelts;
275 i_data |= CPLB_USER_RD;
279 idx = evict_one_icplb(cpu);
281 icplb_tbl[cpu][idx].addr = addr;
282 icplb_tbl[cpu][idx].data = i_data;
285 bfin_write32(ICPLB_DATA0 + idx * 4, i_data);
286 bfin_write32(ICPLB_ADDR0 + idx * 4, addr);
292 static noinline int dcplb_protection_fault(unsigned int cpu)
294 int status = bfin_read_DCPLB_STATUS();
296 nr_dcplb_prot[cpu]++;
298 if (status & FAULT_RW) {
299 int idx = faulting_cplb_index(status);
300 unsigned long data = dcplb_tbl[cpu][idx].data;
301 if (!(data & CPLB_WT) && !(data & CPLB_DIRTY) &&
302 write_permitted(status, data)) {
304 dcplb_tbl[cpu][idx].data = data;
305 bfin_write32(DCPLB_DATA0 + idx * 4, data);
309 return CPLB_PROT_VIOL;
312 int cplb_hdr(int seqstat, struct pt_regs *regs)
314 int cause = seqstat & 0x3f;
315 unsigned int cpu = smp_processor_id();
318 return dcplb_protection_fault(cpu);
320 return icplb_miss(cpu);
322 return dcplb_miss(cpu);
328 void flush_switched_cplbs(unsigned int cpu)
333 nr_cplb_flush[cpu]++;
335 local_irq_save_hw(flags);
337 for (i = first_switched_icplb; i < MAX_CPLBS; i++) {
338 icplb_tbl[cpu][i].data = 0;
339 bfin_write32(ICPLB_DATA0 + i * 4, 0);
344 for (i = first_switched_dcplb; i < MAX_CPLBS; i++) {
345 dcplb_tbl[cpu][i].data = 0;
346 bfin_write32(DCPLB_DATA0 + i * 4, 0);
349 local_irq_restore_hw(flags);
353 void set_mask_dcplbs(unsigned long *masks, unsigned int cpu)
356 unsigned long addr = (unsigned long)masks;
357 unsigned long d_data;
361 current_rwx_mask[cpu] = masks;
365 local_irq_save_hw(flags);
366 current_rwx_mask[cpu] = masks;
368 d_data = CPLB_SUPV_WR | CPLB_VALID | CPLB_DIRTY | PAGE_SIZE_4KB;
369 #ifdef CONFIG_BFIN_DCACHE
370 d_data |= CPLB_L1_CHBL;
371 #ifdef CONFIG_BFIN_WT
372 d_data |= CPLB_L1_AOW | CPLB_WT;
377 for (i = first_mask_dcplb; i < first_switched_dcplb; i++) {
378 dcplb_tbl[cpu][i].addr = addr;
379 dcplb_tbl[cpu][i].data = d_data;
380 bfin_write32(DCPLB_DATA0 + i * 4, d_data);
381 bfin_write32(DCPLB_ADDR0 + i * 4, addr);
385 local_irq_restore_hw(flags);