mmu.c 2.0 KB

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