8xx_mmu.c 4.7 KB

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