blob: 76bd99177de574d4d67a728c2d62bd6b1ba6344e [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>
Mike Frysingera92946b2008-10-16 23:25:34 +080024#include <asm/cacheflush.h>
Bernd Schmidtb97b8a92008-01-27 18:39:16 +080025#include <asm/cplbinit.h>
26#include <asm/mmu_context.h>
27
Bernd Schmidtb97b8a92008-01-27 18:39:16 +080028#define FAULT_RW (1 << 16)
29#define FAULT_USERSUPV (1 << 17)
30
31int page_mask_nelts;
32int page_mask_order;
Graf Yangb8a98982008-11-18 17:48:22 +080033unsigned long *current_rwx_mask[NR_CPUS];
Bernd Schmidtb97b8a92008-01-27 18:39:16 +080034
Graf Yangb8a98982008-11-18 17:48:22 +080035int nr_dcplb_miss[NR_CPUS], nr_icplb_miss[NR_CPUS];
36int nr_icplb_supv_miss[NR_CPUS], nr_dcplb_prot[NR_CPUS];
37int nr_cplb_flush[NR_CPUS];
Bernd Schmidtb97b8a92008-01-27 18:39:16 +080038
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. */
Graf Yangb8a98982008-11-18 17:48:22 +0800102static int icplb_rr_index[NR_CPUS], dcplb_rr_index[NR_CPUS];
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800103
104/*
105 * Find an ICPLB entry to be evicted and return its index.
106 */
Graf Yangb8a98982008-11-18 17:48:22 +0800107static int evict_one_icplb(unsigned int cpu)
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800108{
109 int i;
110 for (i = first_switched_icplb; i < MAX_CPLBS; i++)
Graf Yangb8a98982008-11-18 17:48:22 +0800111 if ((icplb_tbl[cpu][i].data & CPLB_VALID) == 0)
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800112 return i;
Graf Yangb8a98982008-11-18 17:48:22 +0800113 i = first_switched_icplb + icplb_rr_index[cpu];
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800114 if (i >= MAX_CPLBS) {
115 i -= MAX_CPLBS - first_switched_icplb;
Graf Yangb8a98982008-11-18 17:48:22 +0800116 icplb_rr_index[cpu] -= MAX_CPLBS - first_switched_icplb;
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800117 }
Graf Yangb8a98982008-11-18 17:48:22 +0800118 icplb_rr_index[cpu]++;
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800119 return i;
120}
121
Graf Yangb8a98982008-11-18 17:48:22 +0800122static int evict_one_dcplb(unsigned int cpu)
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800123{
124 int i;
125 for (i = first_switched_dcplb; i < MAX_CPLBS; i++)
Graf Yangb8a98982008-11-18 17:48:22 +0800126 if ((dcplb_tbl[cpu][i].data & CPLB_VALID) == 0)
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800127 return i;
Graf Yangb8a98982008-11-18 17:48:22 +0800128 i = first_switched_dcplb + dcplb_rr_index[cpu];
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800129 if (i >= MAX_CPLBS) {
130 i -= MAX_CPLBS - first_switched_dcplb;
Graf Yangb8a98982008-11-18 17:48:22 +0800131 dcplb_rr_index[cpu] -= MAX_CPLBS - first_switched_dcplb;
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800132 }
Graf Yangb8a98982008-11-18 17:48:22 +0800133 dcplb_rr_index[cpu]++;
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800134 return i;
135}
136
Graf Yangb8a98982008-11-18 17:48:22 +0800137static noinline int dcplb_miss(unsigned int cpu)
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800138{
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
Graf Yangb8a98982008-11-18 17:48:22 +0800145 nr_dcplb_miss[cpu]++;
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
Mike Frysingera92946b2008-10-16 23:25:34 +0800149 if (bfin_addr_dcachable(addr)) {
Bernd Schmidtb4bb68f2008-04-23 07:26:23 +0800150 d_data |= CPLB_L1_CHBL | ANOMALY_05000158_WORKAROUND;
Bernd Schmidtdbfe44f2008-04-23 07:11:55 +0800151#ifdef CONFIG_BFIN_WT
Bernd Schmidtb4bb68f2008-04-23 07:26:23 +0800152 d_data |= CPLB_L1_AOW | CPLB_WT;
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800153#endif
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800154 }
Bernd Schmidtb4bb68f2008-04-23 07:26:23 +0800155#endif
Bernd Schmidt1ebc7232008-04-24 02:58:26 +0800156 if (addr >= physical_mem_end) {
Bernd Schmidtb4bb68f2008-04-23 07:26:23 +0800157 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;
Mike Frysinger4e354b52008-04-24 05:44:32 +0800162 } else if (addr >= BOOT_ROM_START && addr < BOOT_ROM_START + BOOT_ROM_LENGTH
163 && (status & (FAULT_RW | FAULT_USERSUPV)) == FAULT_USERSUPV) {
164 addr &= ~(1 * 1024 * 1024 - 1);
165 d_data &= ~PAGE_SIZE_4KB;
Mike Frysinger4bea8b22008-04-24 07:23:36 +0800166 d_data |= PAGE_SIZE_1MB;
Bernd Schmidtb4bb68f2008-04-23 07:26:23 +0800167 } else
168 return CPLB_PROT_VIOL;
Bernd Schmidt1ebc7232008-04-24 02:58:26 +0800169 } else if (addr >= _ramend) {
170 d_data |= CPLB_USER_RD | CPLB_USER_WR;
Bernd Schmidtb4bb68f2008-04-23 07:26:23 +0800171 } else {
Graf Yangb8a98982008-11-18 17:48:22 +0800172 mask = current_rwx_mask[cpu];
Bernd Schmidtb4bb68f2008-04-23 07:26:23 +0800173 if (mask) {
174 int page = addr >> PAGE_SHIFT;
Graf Yangb8a98982008-11-18 17:48:22 +0800175 int idx = page >> 5;
Bernd Schmidtb4bb68f2008-04-23 07:26:23 +0800176 int bit = 1 << (page & 31);
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800177
Graf Yangb8a98982008-11-18 17:48:22 +0800178 if (mask[idx] & bit)
Bernd Schmidtb4bb68f2008-04-23 07:26:23 +0800179 d_data |= CPLB_USER_RD;
180
181 mask += page_mask_nelts;
Graf Yangb8a98982008-11-18 17:48:22 +0800182 if (mask[idx] & bit)
Bernd Schmidtb4bb68f2008-04-23 07:26:23 +0800183 d_data |= CPLB_USER_WR;
184 }
185 }
Graf Yangb8a98982008-11-18 17:48:22 +0800186 idx = evict_one_dcplb(cpu);
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800187
188 addr &= PAGE_MASK;
Graf Yangb8a98982008-11-18 17:48:22 +0800189 dcplb_tbl[cpu][idx].addr = addr;
190 dcplb_tbl[cpu][idx].data = d_data;
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800191
192 disable_dcplb();
193 bfin_write32(DCPLB_DATA0 + idx * 4, d_data);
194 bfin_write32(DCPLB_ADDR0 + idx * 4, addr);
195 enable_dcplb();
196
197 return 0;
198}
199
Graf Yangb8a98982008-11-18 17:48:22 +0800200static noinline int icplb_miss(unsigned int cpu)
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800201{
202 unsigned long addr = bfin_read_ICPLB_FAULT_ADDR();
203 int status = bfin_read_ICPLB_STATUS();
204 int idx;
205 unsigned long i_data;
206
Graf Yangb8a98982008-11-18 17:48:22 +0800207 nr_icplb_miss[cpu]++;
Bernd Schmidt1ebc7232008-04-24 02:58:26 +0800208
209 /* If inside the uncached DMA region, fault. */
210 if (addr >= _ramend - DMA_UNCACHED_REGION && addr < _ramend)
211 return CPLB_PROT_VIOL;
212
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800213 if (status & FAULT_USERSUPV)
Graf Yangb8a98982008-11-18 17:48:22 +0800214 nr_icplb_supv_miss[cpu]++;
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800215
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800216 /*
217 * First, try to find a CPLB that matches this address. If we
218 * find one, then the fact that we're in the miss handler means
219 * that the instruction crosses a page boundary.
220 */
221 for (idx = first_switched_icplb; idx < MAX_CPLBS; idx++) {
Graf Yangb8a98982008-11-18 17:48:22 +0800222 if (icplb_tbl[cpu][idx].data & CPLB_VALID) {
223 unsigned long this_addr = icplb_tbl[cpu][idx].addr;
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800224 if (this_addr <= addr && this_addr + PAGE_SIZE > addr) {
225 addr += PAGE_SIZE;
226 break;
227 }
228 }
229 }
230
231 i_data = CPLB_VALID | CPLB_PORTPRIO | PAGE_SIZE_4KB;
Bernd Schmidt1ebc7232008-04-24 02:58:26 +0800232
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800233#ifdef CONFIG_BFIN_ICACHE
Bernd Schmidt1ebc7232008-04-24 02:58:26 +0800234 /*
235 * Normal RAM, and possibly the reserved memory area, are
236 * cacheable.
237 */
238 if (addr < _ramend ||
239 (addr < physical_mem_end && reserved_mem_icache_on))
240 i_data |= CPLB_L1_CHBL | ANOMALY_05000158_WORKAROUND;
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800241#endif
242
Bernd Schmidt1ebc7232008-04-24 02:58:26 +0800243 if (addr >= physical_mem_end) {
Mike Frysinger4bea8b22008-04-24 07:23:36 +0800244 if (addr >= BOOT_ROM_START && addr < BOOT_ROM_START + BOOT_ROM_LENGTH
245 && (status & FAULT_USERSUPV)) {
246 addr &= ~(1 * 1024 * 1024 - 1);
247 i_data &= ~PAGE_SIZE_4KB;
248 i_data |= PAGE_SIZE_1MB;
249 } else
250 return CPLB_PROT_VIOL;
Bernd Schmidt1ebc7232008-04-24 02:58:26 +0800251 } else if (addr >= _ramend) {
252 i_data |= CPLB_USER_RD;
253 } else {
254 /*
255 * Two cases to distinguish - a supervisor access must
256 * necessarily be for a module page; we grant it
257 * unconditionally (could do better here in the future).
258 * Otherwise, check the x bitmap of the current process.
259 */
260 if (!(status & FAULT_USERSUPV)) {
Graf Yangb8a98982008-11-18 17:48:22 +0800261 unsigned long *mask = current_rwx_mask[cpu];
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800262
Bernd Schmidt1ebc7232008-04-24 02:58:26 +0800263 if (mask) {
264 int page = addr >> PAGE_SHIFT;
Graf Yangb8a98982008-11-18 17:48:22 +0800265 int idx = page >> 5;
Bernd Schmidt1ebc7232008-04-24 02:58:26 +0800266 int bit = 1 << (page & 31);
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800267
Bernd Schmidt1ebc7232008-04-24 02:58:26 +0800268 mask += 2 * page_mask_nelts;
Graf Yangb8a98982008-11-18 17:48:22 +0800269 if (mask[idx] & bit)
Bernd Schmidt1ebc7232008-04-24 02:58:26 +0800270 i_data |= CPLB_USER_RD;
271 }
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800272 }
273 }
Graf Yangb8a98982008-11-18 17:48:22 +0800274 idx = evict_one_icplb(cpu);
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800275 addr &= PAGE_MASK;
Graf Yangb8a98982008-11-18 17:48:22 +0800276 icplb_tbl[cpu][idx].addr = addr;
277 icplb_tbl[cpu][idx].data = i_data;
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800278
279 disable_icplb();
280 bfin_write32(ICPLB_DATA0 + idx * 4, i_data);
281 bfin_write32(ICPLB_ADDR0 + idx * 4, addr);
282 enable_icplb();
283
284 return 0;
285}
286
Graf Yangb8a98982008-11-18 17:48:22 +0800287static noinline int dcplb_protection_fault(unsigned int cpu)
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800288{
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800289 int status = bfin_read_DCPLB_STATUS();
290
Graf Yangb8a98982008-11-18 17:48:22 +0800291 nr_dcplb_prot[cpu]++;
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800292
293 if (status & FAULT_RW) {
294 int idx = faulting_cplb_index(status);
Graf Yangb8a98982008-11-18 17:48:22 +0800295 unsigned long data = dcplb_tbl[cpu][idx].data;
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800296 if (!(data & CPLB_WT) && !(data & CPLB_DIRTY) &&
297 write_permitted(status, data)) {
298 data |= CPLB_DIRTY;
Graf Yangb8a98982008-11-18 17:48:22 +0800299 dcplb_tbl[cpu][idx].data = data;
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800300 bfin_write32(DCPLB_DATA0 + idx * 4, data);
301 return 0;
302 }
303 }
304 return CPLB_PROT_VIOL;
305}
306
307int cplb_hdr(int seqstat, struct pt_regs *regs)
308{
309 int cause = seqstat & 0x3f;
Graf Yangb8a98982008-11-18 17:48:22 +0800310 unsigned int cpu = smp_processor_id();
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800311 switch (cause) {
312 case 0x23:
Graf Yangb8a98982008-11-18 17:48:22 +0800313 return dcplb_protection_fault(cpu);
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800314 case 0x2C:
Graf Yangb8a98982008-11-18 17:48:22 +0800315 return icplb_miss(cpu);
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800316 case 0x26:
Graf Yangb8a98982008-11-18 17:48:22 +0800317 return dcplb_miss(cpu);
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800318 default:
Bernd Schmidtb4bb68f2008-04-23 07:26:23 +0800319 return 1;
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800320 }
321}
322
Graf Yangb8a98982008-11-18 17:48:22 +0800323void flush_switched_cplbs(unsigned int cpu)
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800324{
325 int i;
Bernd Schmidt5d2e3212008-10-07 16:27:01 +0800326 unsigned long flags;
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800327
Graf Yangb8a98982008-11-18 17:48:22 +0800328 nr_cplb_flush[cpu]++;
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800329
Bernd Schmidt5d2e3212008-10-07 16:27:01 +0800330 local_irq_save(flags);
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800331 disable_icplb();
332 for (i = first_switched_icplb; i < MAX_CPLBS; i++) {
Graf Yangb8a98982008-11-18 17:48:22 +0800333 icplb_tbl[cpu][i].data = 0;
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800334 bfin_write32(ICPLB_DATA0 + i * 4, 0);
335 }
336 enable_icplb();
337
338 disable_dcplb();
Bernd Schmidtd56daae2008-04-24 02:56:36 +0800339 for (i = first_switched_dcplb; i < MAX_CPLBS; i++) {
Graf Yangb8a98982008-11-18 17:48:22 +0800340 dcplb_tbl[cpu][i].data = 0;
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800341 bfin_write32(DCPLB_DATA0 + i * 4, 0);
342 }
343 enable_dcplb();
Bernd Schmidt5d2e3212008-10-07 16:27:01 +0800344 local_irq_restore(flags);
345
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800346}
347
Graf Yangb8a98982008-11-18 17:48:22 +0800348void set_mask_dcplbs(unsigned long *masks, unsigned int cpu)
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800349{
350 int i;
351 unsigned long addr = (unsigned long)masks;
352 unsigned long d_data;
Bernd Schmidt5d2e3212008-10-07 16:27:01 +0800353 unsigned long flags;
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800354
Bernd Schmidt5d2e3212008-10-07 16:27:01 +0800355 if (!masks) {
Graf Yangb8a98982008-11-18 17:48:22 +0800356 current_rwx_mask[cpu] = masks;
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800357 return;
Bernd Schmidt5d2e3212008-10-07 16:27:01 +0800358 }
359
360 local_irq_save(flags);
Graf Yangb8a98982008-11-18 17:48:22 +0800361 current_rwx_mask[cpu] = masks;
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800362
363 d_data = CPLB_SUPV_WR | CPLB_VALID | CPLB_DIRTY | PAGE_SIZE_4KB;
364#ifdef CONFIG_BFIN_DCACHE
365 d_data |= CPLB_L1_CHBL;
Bernd Schmidtdbfe44f2008-04-23 07:11:55 +0800366#ifdef CONFIG_BFIN_WT
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800367 d_data |= CPLB_L1_AOW | CPLB_WT;
368#endif
369#endif
370
371 disable_dcplb();
372 for (i = first_mask_dcplb; i < first_switched_dcplb; i++) {
Graf Yangb8a98982008-11-18 17:48:22 +0800373 dcplb_tbl[cpu][i].addr = addr;
374 dcplb_tbl[cpu][i].data = d_data;
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800375 bfin_write32(DCPLB_DATA0 + i * 4, d_data);
376 bfin_write32(DCPLB_ADDR0 + i * 4, addr);
377 addr += PAGE_SIZE;
378 }
379 enable_dcplb();
Bernd Schmidt5d2e3212008-10-07 16:27:01 +0800380 local_irq_restore(flags);
Bernd Schmidtb97b8a92008-01-27 18:39:16 +0800381}