8xx_mmu.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * This file contains the routines for initializing the MMU
  3. * on the 8xx series of chips.
  4. * -- christophe
  5. *
  6. * Derived from arch/powerpc/mm/40x_mmu.c:
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version
  11. * 2 of the License, or (at your option) any later version.
  12. *
  13. */
  14. #include <linux/memblock.h>
  15. #include <asm/fixmap.h>
  16. #include <asm/code-patching.h>
  17. #include "mmu_decl.h"
  18. #define IMMR_SIZE (FIX_IMMR_SIZE << PAGE_SHIFT)
  19. extern int __map_without_ltlbs;
  20. static unsigned long block_mapped_ram;
  21. /*
  22. * Return PA for this VA if it is in an area mapped with LTLBs.
  23. * Otherwise, returns 0
  24. */
  25. phys_addr_t v_block_mapped(unsigned long va)
  26. {
  27. unsigned long p = PHYS_IMMR_BASE;
  28. if (__map_without_ltlbs)
  29. return 0;
  30. if (va >= VIRT_IMMR_BASE && va < VIRT_IMMR_BASE + IMMR_SIZE)
  31. return p + va - VIRT_IMMR_BASE;
  32. if (va >= PAGE_OFFSET && va < PAGE_OFFSET + block_mapped_ram)
  33. return __pa(va);
  34. return 0;
  35. }
  36. /*
  37. * Return VA for a given PA mapped with LTLBs or 0 if not mapped
  38. */
  39. unsigned long p_block_mapped(phys_addr_t pa)
  40. {
  41. unsigned long p = PHYS_IMMR_BASE;
  42. if (__map_without_ltlbs)
  43. return 0;
  44. if (pa >= p && pa < p + IMMR_SIZE)
  45. return VIRT_IMMR_BASE + pa - p;
  46. if (pa < block_mapped_ram)
  47. return (unsigned long)__va(pa);
  48. return 0;
  49. }
  50. #define LARGE_PAGE_SIZE_8M (1<<23)
  51. /*
  52. * MMU_init_hw does the chip-specific initialization of the MMU hardware.
  53. */
  54. void __init MMU_init_hw(void)
  55. {
  56. /* PIN up to the 3 first 8Mb after IMMR in DTLB table */
  57. #ifdef CONFIG_PIN_TLB_DATA
  58. unsigned long ctr = mfspr(SPRN_MD_CTR) & 0xfe000000;
  59. unsigned long flags = 0xf0 | MD_SPS16K | _PAGE_PRIVILEGED | _PAGE_DIRTY;
  60. #ifdef CONFIG_PIN_TLB_IMMR
  61. int i = 29;
  62. #else
  63. int i = 28;
  64. #endif
  65. unsigned long addr = 0;
  66. unsigned long mem = total_lowmem;
  67. for (; i < 32 && mem >= LARGE_PAGE_SIZE_8M; i++) {
  68. mtspr(SPRN_MD_CTR, ctr | (i << 8));
  69. mtspr(SPRN_MD_EPN, (unsigned long)__va(addr) | MD_EVALID);
  70. mtspr(SPRN_MD_TWC, MD_PS8MEG | MD_SVALID | M_APG2);
  71. mtspr(SPRN_MD_RPN, addr | flags | _PAGE_PRESENT);
  72. addr += LARGE_PAGE_SIZE_8M;
  73. mem -= LARGE_PAGE_SIZE_8M;
  74. }
  75. #endif
  76. }
  77. static void __init mmu_mapin_immr(void)
  78. {
  79. unsigned long p = PHYS_IMMR_BASE;
  80. unsigned long v = VIRT_IMMR_BASE;
  81. unsigned long f = pgprot_val(PAGE_KERNEL_NCG);
  82. int offset;
  83. for (offset = 0; offset < IMMR_SIZE; offset += PAGE_SIZE)
  84. map_kernel_page(v + offset, p + offset, f);
  85. }
  86. /* Address of instructions to patch */
  87. #ifndef CONFIG_PIN_TLB_IMMR
  88. extern unsigned int DTLBMiss_jmp;
  89. #endif
  90. extern unsigned int DTLBMiss_cmp, FixupDAR_cmp;
  91. #ifndef CONFIG_PIN_TLB_TEXT
  92. extern unsigned int ITLBMiss_cmp;
  93. #endif
  94. static void __init mmu_patch_cmp_limit(unsigned int *addr, unsigned long mapped)
  95. {
  96. unsigned int instr = *addr;
  97. instr &= 0xffff0000;
  98. instr |= (unsigned long)__va(mapped) >> 16;
  99. patch_instruction(addr, instr);
  100. }
  101. unsigned long __init mmu_mapin_ram(unsigned long top)
  102. {
  103. unsigned long mapped;
  104. if (__map_without_ltlbs) {
  105. mapped = 0;
  106. mmu_mapin_immr();
  107. #ifndef CONFIG_PIN_TLB_IMMR
  108. patch_instruction(&DTLBMiss_jmp, PPC_INST_NOP);
  109. #endif
  110. #ifndef CONFIG_PIN_TLB_TEXT
  111. mmu_patch_cmp_limit(&ITLBMiss_cmp, 0);
  112. #endif
  113. } else {
  114. mapped = top & ~(LARGE_PAGE_SIZE_8M - 1);
  115. }
  116. mmu_patch_cmp_limit(&DTLBMiss_cmp, mapped);
  117. mmu_patch_cmp_limit(&FixupDAR_cmp, mapped);
  118. /* If the size of RAM is not an exact power of two, we may not
  119. * have covered RAM in its entirety with 8 MiB
  120. * pages. Consequently, restrict the top end of RAM currently
  121. * allocable so that calls to the MEMBLOCK to allocate PTEs for "tail"
  122. * coverage with normal-sized pages (or other reasons) do not
  123. * attempt to allocate outside the allowed range.
  124. */
  125. if (mapped)
  126. memblock_set_current_limit(mapped);
  127. block_mapped_ram = mapped;
  128. return mapped;
  129. }
  130. void __init setup_initial_memory_limit(phys_addr_t first_memblock_base,
  131. phys_addr_t first_memblock_size)
  132. {
  133. /* We don't currently support the first MEMBLOCK not mapping 0
  134. * physical on those processors
  135. */
  136. BUG_ON(first_memblock_base != 0);
  137. /* 8xx can only access 24MB at the moment */
  138. memblock_set_current_limit(min_t(u64, first_memblock_size, 0x01800000));
  139. }
  140. /*
  141. * Set up to use a given MMU context.
  142. * id is context number, pgd is PGD pointer.
  143. *
  144. * We place the physical address of the new task page directory loaded
  145. * into the MMU base register, and set the ASID compare register with
  146. * the new "context."
  147. */
  148. void set_context(unsigned long id, pgd_t *pgd)
  149. {
  150. s16 offset = (s16)(__pa(swapper_pg_dir));
  151. #ifdef CONFIG_BDI_SWITCH
  152. pgd_t **ptr = *(pgd_t ***)(KERNELBASE + 0xf0);
  153. /* Context switch the PTE pointer for the Abatron BDI2000.
  154. * The PGDIR is passed as second argument.
  155. */
  156. *(ptr + 1) = pgd;
  157. #endif
  158. /* Register M_TW will contain base address of level 1 table minus the
  159. * lower part of the kernel PGDIR base address, so that all accesses to
  160. * level 1 table are done relative to lower part of kernel PGDIR base
  161. * address.
  162. */
  163. mtspr(SPRN_M_TW, __pa(pgd) - offset);
  164. /* Update context */
  165. mtspr(SPRN_M_CASID, id - 1);
  166. /* sync */
  167. mb();
  168. }
  169. void flush_instruction_cache(void)
  170. {
  171. isync();
  172. mtspr(SPRN_IC_CST, IDC_INVALL);
  173. isync();
  174. }