Haavard Skinnemoen | c6f292f | 2010-08-12 13:52:54 +0700 | [diff] [blame^] | 1 | #include <common.h> |
| 2 | #include <asm/arch/mmu.h> |
| 3 | #include <asm/sysreg.h> |
| 4 | |
| 5 | void mmu_init_r(unsigned long dest_addr) |
| 6 | { |
| 7 | uintptr_t vmr_table_addr; |
| 8 | |
| 9 | /* Round monitor address down to the nearest page boundary */ |
| 10 | dest_addr &= PAGE_ADDR_MASK; |
| 11 | |
| 12 | /* Initialize TLB entry 0 to cover the monitor, and lock it */ |
| 13 | sysreg_write(TLBEHI, dest_addr | SYSREG_BIT(TLBEHI_V)); |
| 14 | sysreg_write(TLBELO, dest_addr | MMU_VMR_CACHE_WRBACK); |
| 15 | sysreg_write(MMUCR, SYSREG_BF(DRP, 0) | SYSREG_BF(DLA, 1) |
| 16 | | SYSREG_BIT(MMUCR_S) | SYSREG_BIT(M)); |
| 17 | __builtin_tlbw(); |
| 18 | |
| 19 | /* |
| 20 | * Calculate the address of the VM range table in a PC-relative |
| 21 | * manner to make sure we hit the SDRAM and not the flash. |
| 22 | */ |
| 23 | vmr_table_addr = (uintptr_t)&mmu_vmr_table; |
| 24 | sysreg_write(PTBR, vmr_table_addr); |
| 25 | printf("VMR table @ 0x%08x\n", vmr_table_addr); |
| 26 | |
| 27 | /* Enable paging */ |
| 28 | sysreg_write(MMUCR, SYSREG_BF(DRP, 1) | SYSREG_BF(DLA, 1) |
| 29 | | SYSREG_BIT(MMUCR_S) | SYSREG_BIT(M) | SYSREG_BIT(E)); |
| 30 | } |
| 31 | |
| 32 | int mmu_handle_tlb_miss(void) |
| 33 | { |
| 34 | const struct mmu_vm_range *vmr_table; |
| 35 | const struct mmu_vm_range *vmr; |
| 36 | unsigned int fault_pgno; |
| 37 | int first, last; |
| 38 | |
| 39 | fault_pgno = sysreg_read(TLBEAR) >> PAGE_SHIFT; |
| 40 | vmr_table = (const struct mmu_vm_range *)sysreg_read(PTBR); |
| 41 | |
| 42 | /* Do a binary search through the VM ranges */ |
| 43 | first = 0; |
| 44 | last = CONFIG_SYS_NR_VM_REGIONS; |
| 45 | while (first < last) { |
| 46 | unsigned int start; |
| 47 | int middle; |
| 48 | |
| 49 | /* Pick the entry in the middle of the remaining range */ |
| 50 | middle = (first + last) >> 1; |
| 51 | vmr = &vmr_table[middle]; |
| 52 | start = vmr->virt_pgno; |
| 53 | |
| 54 | /* Do the bisection thing */ |
| 55 | if (fault_pgno < start) { |
| 56 | last = middle; |
| 57 | } else if (fault_pgno >= (start + vmr->nr_pages)) { |
| 58 | first = middle + 1; |
| 59 | } else { |
| 60 | /* Got it; let's slam it into the TLB */ |
| 61 | uint32_t tlbelo; |
| 62 | |
| 63 | tlbelo = vmr->phys & ~PAGE_ADDR_MASK; |
| 64 | tlbelo |= fault_pgno << PAGE_SHIFT; |
| 65 | sysreg_write(TLBELO, tlbelo); |
| 66 | __builtin_tlbw(); |
| 67 | |
| 68 | /* Zero means success */ |
| 69 | return 0; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | /* |
| 74 | * Didn't find any matching entries. Return a nonzero value to |
| 75 | * indicate that this should be treated as a fatal exception. |
| 76 | */ |
| 77 | return -1; |
| 78 | } |