mmu-meta1.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Copyright (C) 2005,2006,2007,2008,2009 Imagination Technologies
  3. *
  4. * Meta 1 MMU handling code.
  5. *
  6. */
  7. #include <linux/sched.h>
  8. #include <linux/mm.h>
  9. #include <linux/io.h>
  10. #include <asm/mmu.h>
  11. #define DM3_BASE (LINSYSDIRECT_BASE + (MMCU_DIRECTMAPn_ADDR_SCALE * 3))
  12. /*
  13. * This contains the physical address of the top level 2k pgd table.
  14. */
  15. static unsigned long mmu_base_phys;
  16. /*
  17. * Given a physical address, return a mapped virtual address that can be used
  18. * to access that location.
  19. * In practice, we use the DirectMap region to make this happen.
  20. */
  21. static unsigned long map_addr(unsigned long phys)
  22. {
  23. static unsigned long dm_base = 0xFFFFFFFF;
  24. int offset;
  25. offset = phys - dm_base;
  26. /* Are we in the current map range ? */
  27. if ((offset < 0) || (offset >= MMCU_DIRECTMAPn_ADDR_SCALE)) {
  28. /* Calculate new DM area */
  29. dm_base = phys & ~(MMCU_DIRECTMAPn_ADDR_SCALE - 1);
  30. /* Actually map it in! */
  31. metag_out32(dm_base, MMCU_DIRECTMAP3_ADDR);
  32. /* And calculate how far into that area our reference is */
  33. offset = phys - dm_base;
  34. }
  35. return DM3_BASE + offset;
  36. }
  37. /*
  38. * Return the physical address of the base of our pgd table.
  39. */
  40. static inline unsigned long __get_mmu_base(void)
  41. {
  42. unsigned long base_phys;
  43. unsigned int stride;
  44. if (is_global_space(PAGE_OFFSET))
  45. stride = 4;
  46. else
  47. stride = hard_processor_id(); /* [0..3] */
  48. base_phys = metag_in32(MMCU_TABLE_PHYS_ADDR);
  49. base_phys += (0x800 * stride);
  50. return base_phys;
  51. }
  52. /* Given a virtual address, return the virtual address of the relevant pgd */
  53. static unsigned long pgd_entry_addr(unsigned long virt)
  54. {
  55. unsigned long pgd_phys;
  56. unsigned long pgd_virt;
  57. if (!mmu_base_phys)
  58. mmu_base_phys = __get_mmu_base();
  59. /*
  60. * Are we trying to map a global address. If so, then index
  61. * the global pgd table instead of our local one.
  62. */
  63. if (is_global_space(virt)) {
  64. /* Scale into 2gig map */
  65. virt &= ~0x80000000;
  66. }
  67. /* Base of the pgd table plus our 4Meg entry, 4bytes each */
  68. pgd_phys = mmu_base_phys + ((virt >> PGDIR_SHIFT) * 4);
  69. pgd_virt = map_addr(pgd_phys);
  70. return pgd_virt;
  71. }
  72. /* Given a virtual address, return the virtual address of the relevant pte */
  73. static unsigned long pgtable_entry_addr(unsigned long virt)
  74. {
  75. unsigned long pgtable_phys;
  76. unsigned long pgtable_virt, pte_virt;
  77. /* Find the physical address of the 4MB page table*/
  78. pgtable_phys = metag_in32(pgd_entry_addr(virt)) & MMCU_ENTRY_ADDR_BITS;
  79. /* Map it to a virtual address */
  80. pgtable_virt = map_addr(pgtable_phys);
  81. /* And index into it for our pte */
  82. pte_virt = pgtable_virt + ((virt >> PAGE_SHIFT) & 0x3FF) * 4;
  83. return pte_virt;
  84. }
  85. unsigned long mmu_read_first_level_page(unsigned long vaddr)
  86. {
  87. return metag_in32(pgd_entry_addr(vaddr));
  88. }
  89. unsigned long mmu_read_second_level_page(unsigned long vaddr)
  90. {
  91. return metag_in32(pgtable_entry_addr(vaddr));
  92. }
  93. unsigned long mmu_get_base(void)
  94. {
  95. static unsigned long __base;
  96. /* Find the base of our MMU pgd table */
  97. if (!__base)
  98. __base = pgd_entry_addr(0);
  99. return __base;
  100. }
  101. void __init mmu_init(unsigned long mem_end)
  102. {
  103. unsigned long entry, addr;
  104. pgd_t *p_swapper_pg_dir;
  105. /*
  106. * Now copy over any MMU pgd entries already in the mmu page tables
  107. * over to our root init process (swapper_pg_dir) map. This map is
  108. * then inherited by all other processes, which means all processes
  109. * inherit a map of the kernel space.
  110. */
  111. addr = PAGE_OFFSET;
  112. entry = pgd_index(PAGE_OFFSET);
  113. p_swapper_pg_dir = pgd_offset_k(0) + entry;
  114. while (addr <= META_MEMORY_LIMIT) {
  115. unsigned long pgd_entry;
  116. /* copy over the current MMU value */
  117. pgd_entry = mmu_read_first_level_page(addr);
  118. pgd_val(*p_swapper_pg_dir) = pgd_entry;
  119. p_swapper_pg_dir++;
  120. addr += PGDIR_SIZE;
  121. entry++;
  122. }
  123. }