2 * File: arch/blackfin/mach-common/cplbmgtr.S
4 * Author: LG Soft India
7 * Description: CPLB replacement routine for CPLB mismatch
10 * Copyright 2004-2006 Analog Devices Inc.
12 * Bugs: Enter bugs at http://blackfin.uclinux.org/
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, see the file COPYING, or write
26 * to the Free Software Foundation, Inc.,
27 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30 /* Usage: int _cplb_mgr(is_data_miss,int enable_cache)
31 * is_data_miss==2 => Mark as Dirty, write to the clean data page
32 * is_data_miss==1 => Replace a data CPLB.
33 * is_data_miss==0 => Replace an instruction CPLB.
36 * CPLB_RELOADED => Successfully updated CPLB table.
37 * CPLB_NO_UNLOCKED => All CPLBs are locked, so cannot be evicted.
38 * This indicates that the CPLBs in the configuration
39 * tablei are badly configured, as this should never
41 * CPLB_NO_ADDR_MATCH => The address being accessed, that triggered the
42 * exception, is not covered by any of the CPLBs in
43 * the configuration table. The application is
44 * presumably misbehaving.
45 * CPLB_PROT_VIOL => The address being accessed, that triggered the
46 * exception, was not a first-write to a clean Write
47 * Back Data page, and so presumably is a genuine
48 * violation of the page's protection attributes.
49 * The application is misbehaving.
52 #include <linux/linkage.h>
53 #include <asm/blackfin.h>
55 #include <asm/asm-offsets.h>
57 #ifdef CONFIG_EXCPT_IRQ_SYSC_L1
69 IF CC JUMP .Ldcplb_write;
72 IF !CC JUMP .Ldcplb_miss_compare;
74 /* ICPLB Miss Exception. We need to choose one of the
75 * currently-installed CPLBs, and replace it with one
76 * from the configuration table.
79 /* A multi-word instruction can cross a page boundary. This means the
80 * first part of the instruction can be in a valid page, but the
81 * second part is not, and hence generates the instruction miss.
82 * However, the fault address is for the start of the instruction,
83 * not the part that's in the bad page. Therefore, we have to check
84 * whether the fault address applies to a page that is already present
88 P4.L = LO(ICPLB_FAULT_ADDR);
89 P4.H = HI(ICPLB_FAULT_ADDR);
92 P5.L = _page_size_table;
93 P5.H = _page_size_table;
95 P0.L = LO(ICPLB_DATA0);
96 P0.H = HI(ICPLB_DATA0);
97 R4 = [P4]; /* Get faulting address*/
98 R6 = 64; /* Advance past the fault address, which*/
99 R6 = R6 + R4; /* we'll use if we find a match*/
100 R3 = ((16 << 8) | 2); /* Extract mask, two bits at posn 16 */
105 R1 = [P0-0x100]; /* Address for this CPLB */
107 R0 = [P0++]; /* Info for this CPLB*/
108 CC = BITTST(R0,0); /* Is the CPLB valid?*/
109 IF !CC JUMP .Lnomatch; /* Skip it, if not.*/
110 CC = R4 < R1(IU); /* If fault address less than page start*/
111 IF CC JUMP .Lnomatch; /* then skip this one.*/
112 R2 = EXTRACT(R0,R3.L) (Z); /* Get page size*/
114 P1 = P5 + (P1<<2); /* index into page-size table*/
115 R2 = [P1]; /* Get the page size*/
116 R1 = R1 + R2; /* and add to page start, to get page end*/
117 CC = R4 < R1(IU); /* and see whether fault addr is in page.*/
118 IF !CC R4 = R6; /* If so, advance the address and finish loop.*/
119 IF !CC JUMP .Lisearch_done;
123 CC = BITTST(R5, 4); /* i.e CC = R5 >= 16*/
124 IF !CC JUMP .Lisearch;
127 I0 = R4; /* Fault address we'll search for*/
129 /* set up pointers */
130 P0.L = LO(ICPLB_DATA0);
131 P0.H = HI(ICPLB_DATA0);
133 /* The replacement procedure for ICPLBs */
135 P4.L = LO(IMEM_CONTROL);
136 P4.H = HI(IMEM_CONTROL);
138 /* Turn off CPLBs while we work, necessary according to HRM before
139 * modifying CPLB descriptors
141 R5 = [P4]; /* Control Register*/
142 BITCLR(R5,ENICPLB_P);
144 SSYNC; /* SSYNC required before writing to IMEM_CONTROL. */
150 R1 = -1; /* end point comparison */
151 R3 = 16; /* counter */
153 /* Search through CPLBs for first non-locked entry */
154 /* Overwrite it by moving everyone else up by 1 */
159 IF CC JUMP .Lall_locked;
160 CC = BITTST(R0, 0); /* an invalid entry is good */
161 IF !CC JUMP .Lifound_victim;
162 CC = BITTST(R0,1); /* but a locked entry isn't */
163 IF CC JUMP .Licheck_lock;
166 #ifdef CONFIG_CPLB_INFO
169 P3 = [P2 + PDA_IPDT_SWAPCOUNT];
170 P2 = [P2 + PDA_IPDT];
173 R2 = [P2]; /* address from config table */
177 IF CC JUMP .Licount_done;
179 IF !CC JUMP .Licount;
187 LSETUP(.Lis_move,.Lie_move) LC0;
196 /* Clear ICPLB_DATA15, in case we don't find a replacement
197 * otherwise, we would have a duplicate entry, and will crash
202 /* We've made space in the ICPLB table, so that ICPLB15
203 * is now free to be overwritten. Next, we have to determine
204 * which CPLB we need to install, from the configuration
205 * table. This is a matter of getting the start-of-page
206 * addresses and page-lengths from the config table, and
207 * determining whether the fault address falls within that
212 P2 = [P3 + PDA_IPDT];
213 #ifdef CONFIG_CPLB_INFO
214 P3 = [P3 + PDA_IPDT_SWAPCOUNT];
217 P0.L = _page_size_table;
218 P0.H = _page_size_table;
220 /* Retrieve our fault address (which may have been advanced
221 * because the faulting instruction crossed a page boundary).
226 /* An extraction pattern, to get the page-size bits from
227 * the CPLB data entry. Bits 16-17, so two bits at posn 16.
231 .Linext: R4 = [P2++]; /* address from config table */
232 R2 = [P2++]; /* data from config table */
233 #ifdef CONFIG_CPLB_INFO
237 CC = R4 == -1; /* End of config table*/
238 IF CC JUMP .Lno_page_in_table;
240 /* See if failed address > start address */
244 /* extract page size (17:16)*/
245 R3 = EXTRACT(R2, R1.L) (Z);
247 /* add page size to addr to get range */
250 P5 = P0 + (P5 << 2); /* scaled, for int access*/
254 /* See if failed address < (start address + page size) */
258 /* We've found a CPLB in the config table that covers
259 * the faulting address, so install this CPLB into the
260 * last entry of the table.
263 P1.L = LO(ICPLB_DATA15); /* ICPLB_DATA15 */
264 P1.H = HI(ICPLB_DATA15);
267 #ifdef CONFIG_CPLB_INFO
273 /* P4 points to IMEM_CONTROL, and R5 contains its old
274 * value, after we disabled ICPLBS. Re-enable them.
277 BITSET(R5,ENICPLB_P);
279 SSYNC; /* SSYNC required before writing to IMEM_CONTROL. */
285 ( R7:4,P5:3 ) = [SP++];
291 R0 = CPLB_NO_ADDR_MATCH;
295 R0 = CPLB_NO_UNLOCKED;
302 /* Make sure we turn protection/cache back on, even in the failing case */
303 BITSET(R5,ENICPLB_P);
305 SSYNC; /* SSYNC required before writing to IMEM_CONTROL. */
311 ( R7:4,P5:3 ) = [SP++];
316 /* if a DCPLB is marked as write-back (CPLB_WT==0), and
317 * it is clean (CPLB_DIRTY==0), then a write to the
318 * CPLB's page triggers a protection violation. We have to
319 * mark the CPLB as dirty, to indicate that there are
320 * pending writes associated with the CPLB.
323 P4.L = LO(DCPLB_STATUS);
324 P4.H = HI(DCPLB_STATUS);
325 P3.L = LO(DCPLB_DATA0);
326 P3.H = HI(DCPLB_DATA0);
329 /* A protection violation can be caused by more than just writes
330 * to a clean WB page, so we have to ensure that:
332 * - to a clean WB page
333 * - and is allowed in the mode the access occurred.
336 CC = BITTST(R5, 16); /* ensure it was a write*/
337 IF !CC JUMP .Lprot_violation;
339 /* to check the rest, we have to retrieve the DCPLB.*/
341 /* The low half of DCPLB_STATUS is a bit mask*/
343 R2 = R5.L (Z); /* indicating which CPLB triggered the event.*/
344 R3 = 30; /* so we can use this to determine the offset*/
346 R2 = R2.L (Z); /* into the DCPLB table.*/
350 R3 = [P3]; /* Retrieve the CPLB*/
352 /* Now we can check whether it's a clean WB page*/
354 CC = BITTST(R3, 14); /* 0==WB, 1==WT*/
355 IF CC JUMP .Lprot_violation;
356 CC = BITTST(R3, 7); /* 0 == clean, 1 == dirty*/
357 IF CC JUMP .Lprot_violation;
359 /* Check whether the write is allowed in the mode that was active.*/
361 R2 = 1<<3; /* checking write in user mode*/
362 CC = BITTST(R5, 17); /* 0==was user, 1==was super*/
364 R2 <<= R5; /* if was super, check write in super mode*/
367 IF CC JUMP .Lprot_violation;
369 /* It's a genuine write-to-clean-page.*/
371 BITSET(R3, 7); /* mark as dirty*/
372 [P3] = R3; /* and write back.*/
375 ( R7:4,P5:3 ) = [SP++];
379 .Ldcplb_miss_compare:
381 /* Data CPLB Miss event. We need to choose a CPLB to
382 * evict, and then locate a new CPLB to install from the
383 * config table, that covers the faulting address.
386 P1.L = LO(DCPLB_DATA15);
387 P1.H = HI(DCPLB_DATA15);
389 P4.L = LO(DCPLB_FAULT_ADDR);
390 P4.H = HI(DCPLB_FAULT_ADDR);
394 /* The replacement procedure for DCPLBs*/
396 R6 = R1; /* Save for later*/
398 /* Turn off CPLBs while we work.*/
399 P4.L = LO(DMEM_CONTROL);
400 P4.H = HI(DMEM_CONTROL);
402 BITCLR(R5,ENDCPLB_P);
404 SSYNC; /* SSYNC required before writing to DMEM_CONTROL. */
410 /* Start looking for a CPLB to evict. Our order of preference
411 * is: invalid CPLBs, clean CPLBs, dirty CPLBs. Locked CPLBs
415 I1.L = LO(DCPLB_DATA0);
416 I1.H = HI(DCPLB_DATA0);
419 I2.L = _dcplb_preference;
420 I2.H = _dcplb_preference;
421 LSETUP(.Lsdsearch1, .Ledsearch1) LC0 = P1;
423 R0 = [I2++]; /* Get the bits we're interested in*/
424 P0 = I1; /* Go back to start of table*/
425 LSETUP (.Lsdsearch2, .Ledsearch2) LC1 = P2;
427 R1 = [P0++]; /* Fetch each installed CPLB in turn*/
428 R2 = R1 & R0; /* and test for interesting bits.*/
429 CC = R2 == 0; /* If none are set, it'll do.*/
430 IF !CC JUMP .Lskip_stack_check;
432 R2 = [P0 - 0x104]; /* R2 - PageStart */
433 P3.L = _page_size_table; /* retrieve end address */
434 P3.H = _page_size_table; /* retrieve end address */
435 R3 = 0x1002; /* 16th - position, 2 bits -length */
437 nop; /* Anomaly 05000209 */
439 R7 = EXTRACT(R1,R3.l);
440 R7 = R7 << 2; /* Page size index offset */
443 R7 = [P3]; /* page size in bytes */
445 R7 = R2 + R7; /* R7 - PageEnd */
446 R4 = SP; /* Test SP is in range */
448 CC = R7 < R4; /* if PageEnd < SP */
449 IF CC JUMP .Ldfound_victim;
450 R3 = 0x284; /* stack length from start of trap till
452 * 20 stack locations for future modifications
455 CC = R4 < R2; /* if SP + stacklen < PageStart */
456 IF CC JUMP .Ldfound_victim;
462 /* If we got here, we didn't find a DCPLB we considered
463 * replacable, which means all of them were locked.
469 #ifdef CONFIG_CPLB_INFO
472 P3 = [P2 + PDA_DPDT_SWAPCOUNT];
473 P2 = [P2 + PDA_DPDT];
480 IF CC JUMP .Ldicount_done;
482 IF !CC JUMP .Ldicount;
489 /* Clean down the hardware loops*/
494 /* There's a suitable victim in [P0-4] (because we've
500 /* [P0-4] is a suitable victim CPLB, so we want to
501 * overwrite it by moving all the following CPLBs
502 * one space closer to the start.
505 R1.L = LO(DCPLB_DATA16); /* DCPLB_DATA15 + 4 */
506 R1.H = HI(DCPLB_DATA16);
509 /* If the victim happens to be in DCPLB15,
510 * we don't need to move anything.
514 IF CC JUMP .Lde_moved;
518 LSETUP(.Lds_move, .Lde_move) LC0=P1;
520 R0 = [P0++]; /* move data */
522 R0 = [P0-0x104] /* move address */
529 /* Clear DCPLB_DATA15, in case we don't find a replacement
530 * otherwise, we would have a duplicate entry, and will crash
535 /* We've now made space in DCPLB15 for the new CPLB to be
536 * installed. The next stage is to locate a CPLB in the
537 * config table that covers the faulting address.
540 R0 = I0; /* Our faulting address */
543 P2 = [P3 + PDA_DPDT];
544 #ifdef CONFIG_CPLB_INFO
545 P3 = [P3 + PDA_DPDT_SWAPCOUNT];
549 P1.L = _page_size_table;
550 P1.H = _page_size_table;
552 /* An extraction pattern, to retrieve bits 17:16.*/
555 .Ldnext: R4 = [P2++]; /* address */
556 R2 = [P2++]; /* data */
557 #ifdef CONFIG_CPLB_INFO
562 IF CC JUMP .Lno_page_in_table;
564 /* See if failed address > start address */
568 /* extract page size (17:16)*/
569 R3 = EXTRACT(R2, R1.L) (Z);
571 /* add page size to addr to get range */
578 /* See if failed address < (start address + page size) */
582 /* We've found the CPLB that should be installed, so
583 * write it into CPLB15, masking off any caching bits
587 P1.L = LO(DCPLB_DATA15);
588 P1.H = HI(DCPLB_DATA15);
590 /* If the DCPLB has cache bits set, but caching hasn't
591 * been enabled, then we want to mask off the cache-in-L1
592 * bit before installing. Moreover, if caching is off, we
593 * also want to ensure that the DCPLB has WT mode set, rather
594 * than WB, since WB pages still trigger first-write exceptions
595 * even when not caching is off, and the page isn't marked as
596 * cachable. Finally, we could mark the page as clean, not dirty,
597 * but we choose to leave that decision to the user; if the user
598 * chooses to have a CPLB pre-defined as dirty, then they always
599 * pay the cost of flushing during eviction, but don't pay the
600 * cost of first-write exceptions to mark the page as dirty.
603 #ifdef CONFIG_BFIN_WT
604 BITSET(R6, 14); /* Set WT*/
609 #ifdef CONFIG_CPLB_INFO
615 /* We've installed the CPLB, so re-enable CPLBs. P4
616 * points to DMEM_CONTROL, and R5 is the value we
617 * last wrote to it, when we were disabling CPLBs.
620 BITSET(R5,ENDCPLB_P);
627 ( R7:4,P5:3 ) = [SP++];
632 #ifdef CONFIG_CPLB_SWITCH_TAB_L1
638 ENTRY(_page_size_table)
639 .byte4 0x00000400; /* 1K */
640 .byte4 0x00001000; /* 4K */
641 .byte4 0x00100000; /* 1M */
642 .byte4 0x00400000; /* 4M */
643 END(_page_size_table)
645 ENTRY(_dcplb_preference)
646 .byte4 0x00000001; /* valid bit */
647 .byte4 0x00000002; /* lock bit */
648 END(_dcplb_preference)