Bernd Schmidt | b97b8a9 | 2008-01-27 18:39:16 +0800 | [diff] [blame] | 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++; |
Bernd Schmidt | b97b8a9 | 2008-01-27 18:39:16 +0800 | [diff] [blame] | 146 | |
| 147 | d_data = CPLB_SUPV_WR | CPLB_VALID | CPLB_DIRTY | PAGE_SIZE_4KB; |
| 148 | #ifdef CONFIG_BFIN_DCACHE |
Bernd Schmidt | 1ebc723 | 2008-04-24 02:58:26 +0800 | [diff] [blame^] | 149 | if (addr < _ramend - DMA_UNCACHED_REGION || |
| 150 | (reserved_mem_dcache_on && addr >= _ramend && |
| 151 | addr < physical_mem_end)) { |
Bernd Schmidt | b4bb68f | 2008-04-23 07:26:23 +0800 | [diff] [blame] | 152 | d_data |= CPLB_L1_CHBL | ANOMALY_05000158_WORKAROUND; |
Bernd Schmidt | dbfe44f | 2008-04-23 07:11:55 +0800 | [diff] [blame] | 153 | #ifdef CONFIG_BFIN_WT |
Bernd Schmidt | b4bb68f | 2008-04-23 07:26:23 +0800 | [diff] [blame] | 154 | d_data |= CPLB_L1_AOW | CPLB_WT; |
Bernd Schmidt | b97b8a9 | 2008-01-27 18:39:16 +0800 | [diff] [blame] | 155 | #endif |
Bernd Schmidt | b97b8a9 | 2008-01-27 18:39:16 +0800 | [diff] [blame] | 156 | } |
Bernd Schmidt | b4bb68f | 2008-04-23 07:26:23 +0800 | [diff] [blame] | 157 | #endif |
Bernd Schmidt | 1ebc723 | 2008-04-24 02:58:26 +0800 | [diff] [blame^] | 158 | if (addr >= physical_mem_end) { |
Bernd Schmidt | b4bb68f | 2008-04-23 07:26:23 +0800 | [diff] [blame] | 159 | 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; |
| 164 | } else |
| 165 | return CPLB_PROT_VIOL; |
Bernd Schmidt | 1ebc723 | 2008-04-24 02:58:26 +0800 | [diff] [blame^] | 166 | } else if (addr >= _ramend) { |
| 167 | d_data |= CPLB_USER_RD | CPLB_USER_WR; |
Bernd Schmidt | b4bb68f | 2008-04-23 07:26:23 +0800 | [diff] [blame] | 168 | } else { |
| 169 | mask = current_rwx_mask; |
| 170 | if (mask) { |
| 171 | int page = addr >> PAGE_SHIFT; |
| 172 | int offs = page >> 5; |
| 173 | int bit = 1 << (page & 31); |
Bernd Schmidt | b97b8a9 | 2008-01-27 18:39:16 +0800 | [diff] [blame] | 174 | |
Bernd Schmidt | b4bb68f | 2008-04-23 07:26:23 +0800 | [diff] [blame] | 175 | if (mask[offs] & bit) |
| 176 | d_data |= CPLB_USER_RD; |
| 177 | |
| 178 | mask += page_mask_nelts; |
| 179 | if (mask[offs] & bit) |
| 180 | d_data |= CPLB_USER_WR; |
| 181 | } |
| 182 | } |
Bernd Schmidt | b97b8a9 | 2008-01-27 18:39:16 +0800 | [diff] [blame] | 183 | idx = evict_one_dcplb(); |
| 184 | |
| 185 | addr &= PAGE_MASK; |
| 186 | dcplb_tbl[idx].addr = addr; |
| 187 | dcplb_tbl[idx].data = d_data; |
| 188 | |
| 189 | disable_dcplb(); |
| 190 | bfin_write32(DCPLB_DATA0 + idx * 4, d_data); |
| 191 | bfin_write32(DCPLB_ADDR0 + idx * 4, addr); |
| 192 | enable_dcplb(); |
| 193 | |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | static noinline int icplb_miss(void) |
| 198 | { |
| 199 | unsigned long addr = bfin_read_ICPLB_FAULT_ADDR(); |
| 200 | int status = bfin_read_ICPLB_STATUS(); |
| 201 | int idx; |
| 202 | unsigned long i_data; |
| 203 | |
| 204 | nr_icplb_miss++; |
Bernd Schmidt | 1ebc723 | 2008-04-24 02:58:26 +0800 | [diff] [blame^] | 205 | |
| 206 | /* If inside the uncached DMA region, fault. */ |
| 207 | if (addr >= _ramend - DMA_UNCACHED_REGION && addr < _ramend) |
| 208 | return CPLB_PROT_VIOL; |
| 209 | |
Bernd Schmidt | b97b8a9 | 2008-01-27 18:39:16 +0800 | [diff] [blame] | 210 | if (status & FAULT_USERSUPV) |
| 211 | nr_icplb_supv_miss++; |
| 212 | |
Bernd Schmidt | b97b8a9 | 2008-01-27 18:39:16 +0800 | [diff] [blame] | 213 | /* |
| 214 | * First, try to find a CPLB that matches this address. If we |
| 215 | * find one, then the fact that we're in the miss handler means |
| 216 | * that the instruction crosses a page boundary. |
| 217 | */ |
| 218 | for (idx = first_switched_icplb; idx < MAX_CPLBS; idx++) { |
| 219 | if (icplb_tbl[idx].data & CPLB_VALID) { |
| 220 | unsigned long this_addr = icplb_tbl[idx].addr; |
| 221 | if (this_addr <= addr && this_addr + PAGE_SIZE > addr) { |
| 222 | addr += PAGE_SIZE; |
| 223 | break; |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | i_data = CPLB_VALID | CPLB_PORTPRIO | PAGE_SIZE_4KB; |
Bernd Schmidt | 1ebc723 | 2008-04-24 02:58:26 +0800 | [diff] [blame^] | 229 | |
Bernd Schmidt | b97b8a9 | 2008-01-27 18:39:16 +0800 | [diff] [blame] | 230 | #ifdef CONFIG_BFIN_ICACHE |
Bernd Schmidt | 1ebc723 | 2008-04-24 02:58:26 +0800 | [diff] [blame^] | 231 | /* |
| 232 | * Normal RAM, and possibly the reserved memory area, are |
| 233 | * cacheable. |
| 234 | */ |
| 235 | if (addr < _ramend || |
| 236 | (addr < physical_mem_end && reserved_mem_icache_on)) |
| 237 | i_data |= CPLB_L1_CHBL | ANOMALY_05000158_WORKAROUND; |
Bernd Schmidt | b97b8a9 | 2008-01-27 18:39:16 +0800 | [diff] [blame] | 238 | #endif |
| 239 | |
Bernd Schmidt | 1ebc723 | 2008-04-24 02:58:26 +0800 | [diff] [blame^] | 240 | if (addr >= physical_mem_end) { |
| 241 | return CPLB_PROT_VIOL; |
| 242 | } else if (addr >= _ramend) { |
| 243 | i_data |= CPLB_USER_RD; |
| 244 | } else { |
| 245 | /* |
| 246 | * Two cases to distinguish - a supervisor access must |
| 247 | * necessarily be for a module page; we grant it |
| 248 | * unconditionally (could do better here in the future). |
| 249 | * Otherwise, check the x bitmap of the current process. |
| 250 | */ |
| 251 | if (!(status & FAULT_USERSUPV)) { |
| 252 | unsigned long *mask = current_rwx_mask; |
Bernd Schmidt | b97b8a9 | 2008-01-27 18:39:16 +0800 | [diff] [blame] | 253 | |
Bernd Schmidt | 1ebc723 | 2008-04-24 02:58:26 +0800 | [diff] [blame^] | 254 | if (mask) { |
| 255 | int page = addr >> PAGE_SHIFT; |
| 256 | int offs = page >> 5; |
| 257 | int bit = 1 << (page & 31); |
Bernd Schmidt | b97b8a9 | 2008-01-27 18:39:16 +0800 | [diff] [blame] | 258 | |
Bernd Schmidt | 1ebc723 | 2008-04-24 02:58:26 +0800 | [diff] [blame^] | 259 | mask += 2 * page_mask_nelts; |
| 260 | if (mask[offs] & bit) |
| 261 | i_data |= CPLB_USER_RD; |
| 262 | } |
Bernd Schmidt | b97b8a9 | 2008-01-27 18:39:16 +0800 | [diff] [blame] | 263 | } |
| 264 | } |
Bernd Schmidt | b97b8a9 | 2008-01-27 18:39:16 +0800 | [diff] [blame] | 265 | idx = evict_one_icplb(); |
| 266 | addr &= PAGE_MASK; |
| 267 | icplb_tbl[idx].addr = addr; |
| 268 | icplb_tbl[idx].data = i_data; |
| 269 | |
| 270 | disable_icplb(); |
| 271 | bfin_write32(ICPLB_DATA0 + idx * 4, i_data); |
| 272 | bfin_write32(ICPLB_ADDR0 + idx * 4, addr); |
| 273 | enable_icplb(); |
| 274 | |
| 275 | return 0; |
| 276 | } |
| 277 | |
| 278 | static noinline int dcplb_protection_fault(void) |
| 279 | { |
| 280 | unsigned long addr = bfin_read_DCPLB_FAULT_ADDR(); |
| 281 | int status = bfin_read_DCPLB_STATUS(); |
| 282 | |
| 283 | nr_dcplb_prot++; |
| 284 | |
| 285 | if (status & FAULT_RW) { |
| 286 | int idx = faulting_cplb_index(status); |
| 287 | unsigned long data = dcplb_tbl[idx].data; |
| 288 | if (!(data & CPLB_WT) && !(data & CPLB_DIRTY) && |
| 289 | write_permitted(status, data)) { |
| 290 | data |= CPLB_DIRTY; |
| 291 | dcplb_tbl[idx].data = data; |
| 292 | bfin_write32(DCPLB_DATA0 + idx * 4, data); |
| 293 | return 0; |
| 294 | } |
| 295 | } |
| 296 | return CPLB_PROT_VIOL; |
| 297 | } |
| 298 | |
| 299 | int cplb_hdr(int seqstat, struct pt_regs *regs) |
| 300 | { |
| 301 | int cause = seqstat & 0x3f; |
| 302 | switch (cause) { |
| 303 | case 0x23: |
| 304 | return dcplb_protection_fault(); |
| 305 | case 0x2C: |
| 306 | return icplb_miss(); |
| 307 | case 0x26: |
| 308 | return dcplb_miss(); |
| 309 | default: |
Bernd Schmidt | b4bb68f | 2008-04-23 07:26:23 +0800 | [diff] [blame] | 310 | return 1; |
Bernd Schmidt | b97b8a9 | 2008-01-27 18:39:16 +0800 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | |
| 314 | void flush_switched_cplbs(void) |
| 315 | { |
| 316 | int i; |
| 317 | |
| 318 | nr_cplb_flush++; |
| 319 | |
| 320 | disable_icplb(); |
| 321 | for (i = first_switched_icplb; i < MAX_CPLBS; i++) { |
| 322 | icplb_tbl[i].data = 0; |
| 323 | bfin_write32(ICPLB_DATA0 + i * 4, 0); |
| 324 | } |
| 325 | enable_icplb(); |
| 326 | |
| 327 | disable_dcplb(); |
Bernd Schmidt | d56daae | 2008-04-24 02:56:36 +0800 | [diff] [blame] | 328 | for (i = first_switched_dcplb; i < MAX_CPLBS; i++) { |
Bernd Schmidt | b97b8a9 | 2008-01-27 18:39:16 +0800 | [diff] [blame] | 329 | dcplb_tbl[i].data = 0; |
| 330 | bfin_write32(DCPLB_DATA0 + i * 4, 0); |
| 331 | } |
| 332 | enable_dcplb(); |
| 333 | } |
| 334 | |
| 335 | void set_mask_dcplbs(unsigned long *masks) |
| 336 | { |
| 337 | int i; |
| 338 | unsigned long addr = (unsigned long)masks; |
| 339 | unsigned long d_data; |
| 340 | current_rwx_mask = masks; |
| 341 | |
| 342 | if (!masks) |
| 343 | return; |
| 344 | |
| 345 | d_data = CPLB_SUPV_WR | CPLB_VALID | CPLB_DIRTY | PAGE_SIZE_4KB; |
| 346 | #ifdef CONFIG_BFIN_DCACHE |
| 347 | d_data |= CPLB_L1_CHBL; |
Bernd Schmidt | dbfe44f | 2008-04-23 07:11:55 +0800 | [diff] [blame] | 348 | #ifdef CONFIG_BFIN_WT |
Bernd Schmidt | b97b8a9 | 2008-01-27 18:39:16 +0800 | [diff] [blame] | 349 | d_data |= CPLB_L1_AOW | CPLB_WT; |
| 350 | #endif |
| 351 | #endif |
| 352 | |
| 353 | disable_dcplb(); |
| 354 | for (i = first_mask_dcplb; i < first_switched_dcplb; i++) { |
| 355 | dcplb_tbl[i].addr = addr; |
| 356 | dcplb_tbl[i].data = d_data; |
| 357 | bfin_write32(DCPLB_DATA0 + i * 4, d_data); |
| 358 | bfin_write32(DCPLB_ADDR0 + i * 4, addr); |
| 359 | addr += PAGE_SIZE; |
| 360 | } |
| 361 | enable_dcplb(); |
| 362 | } |
| 363 | |
| 364 | #endif |