44x.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Modifications by Matt Porter ([email protected]) to support
  4. * PPC44x Book E processors.
  5. *
  6. * This file contains the routines for initializing the MMU
  7. * on the 4xx series of chips.
  8. * -- paulus
  9. *
  10. * Derived from arch/ppc/mm/init.c:
  11. * Copyright (C) 1995-1996 Gary Thomas ([email protected])
  12. *
  13. * Modifications by Paul Mackerras (PowerMac) ([email protected])
  14. * and Cort Dougan (PReP) ([email protected])
  15. * Copyright (C) 1996 Paul Mackerras
  16. *
  17. * Derived from "arch/i386/mm/init.c"
  18. * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
  19. */
  20. #include <linux/init.h>
  21. #include <linux/memblock.h>
  22. #include <asm/mmu.h>
  23. #include <asm/page.h>
  24. #include <asm/cacheflush.h>
  25. #include <asm/code-patching.h>
  26. #include <asm/smp.h>
  27. #include <mm/mmu_decl.h>
  28. /* Used by the 44x TLB replacement exception handler.
  29. * Just needed it declared someplace.
  30. */
  31. unsigned int tlb_44x_index; /* = 0 */
  32. unsigned int tlb_44x_hwater = PPC44x_TLB_SIZE - 1 - PPC44x_EARLY_TLBS;
  33. int icache_44x_need_flush;
  34. unsigned long tlb_47x_boltmap[1024/8];
  35. static void __init ppc44x_update_tlb_hwater(void)
  36. {
  37. /* The TLB miss handlers hard codes the watermark in a cmpli
  38. * instruction to improve performances rather than loading it
  39. * from the global variable. Thus, we patch the instructions
  40. * in the 2 TLB miss handlers when updating the value
  41. */
  42. modify_instruction_site(&patch__tlb_44x_hwater_D, 0xffff, tlb_44x_hwater);
  43. modify_instruction_site(&patch__tlb_44x_hwater_I, 0xffff, tlb_44x_hwater);
  44. }
  45. /*
  46. * "Pins" a 256MB TLB entry in AS0 for kernel lowmem for 44x type MMU
  47. */
  48. static void __init ppc44x_pin_tlb(unsigned int virt, unsigned int phys)
  49. {
  50. unsigned int entry = tlb_44x_hwater--;
  51. ppc44x_update_tlb_hwater();
  52. mtspr(SPRN_MMUCR, 0);
  53. __asm__ __volatile__(
  54. "tlbwe %2,%3,%4\n"
  55. "tlbwe %1,%3,%5\n"
  56. "tlbwe %0,%3,%6\n"
  57. :
  58. : "r" (PPC44x_TLB_SW | PPC44x_TLB_SR | PPC44x_TLB_SX | PPC44x_TLB_G),
  59. "r" (phys),
  60. "r" (virt | PPC44x_TLB_VALID | PPC44x_TLB_256M),
  61. "r" (entry),
  62. "i" (PPC44x_TLB_PAGEID),
  63. "i" (PPC44x_TLB_XLAT),
  64. "i" (PPC44x_TLB_ATTRIB));
  65. }
  66. static int __init ppc47x_find_free_bolted(void)
  67. {
  68. unsigned int mmube0 = mfspr(SPRN_MMUBE0);
  69. unsigned int mmube1 = mfspr(SPRN_MMUBE1);
  70. if (!(mmube0 & MMUBE0_VBE0))
  71. return 0;
  72. if (!(mmube0 & MMUBE0_VBE1))
  73. return 1;
  74. if (!(mmube0 & MMUBE0_VBE2))
  75. return 2;
  76. if (!(mmube1 & MMUBE1_VBE3))
  77. return 3;
  78. if (!(mmube1 & MMUBE1_VBE4))
  79. return 4;
  80. if (!(mmube1 & MMUBE1_VBE5))
  81. return 5;
  82. return -1;
  83. }
  84. static void __init ppc47x_update_boltmap(void)
  85. {
  86. unsigned int mmube0 = mfspr(SPRN_MMUBE0);
  87. unsigned int mmube1 = mfspr(SPRN_MMUBE1);
  88. if (mmube0 & MMUBE0_VBE0)
  89. __set_bit((mmube0 >> MMUBE0_IBE0_SHIFT) & 0xff,
  90. tlb_47x_boltmap);
  91. if (mmube0 & MMUBE0_VBE1)
  92. __set_bit((mmube0 >> MMUBE0_IBE1_SHIFT) & 0xff,
  93. tlb_47x_boltmap);
  94. if (mmube0 & MMUBE0_VBE2)
  95. __set_bit((mmube0 >> MMUBE0_IBE2_SHIFT) & 0xff,
  96. tlb_47x_boltmap);
  97. if (mmube1 & MMUBE1_VBE3)
  98. __set_bit((mmube1 >> MMUBE1_IBE3_SHIFT) & 0xff,
  99. tlb_47x_boltmap);
  100. if (mmube1 & MMUBE1_VBE4)
  101. __set_bit((mmube1 >> MMUBE1_IBE4_SHIFT) & 0xff,
  102. tlb_47x_boltmap);
  103. if (mmube1 & MMUBE1_VBE5)
  104. __set_bit((mmube1 >> MMUBE1_IBE5_SHIFT) & 0xff,
  105. tlb_47x_boltmap);
  106. }
  107. /*
  108. * "Pins" a 256MB TLB entry in AS0 for kernel lowmem for 47x type MMU
  109. */
  110. static void __init ppc47x_pin_tlb(unsigned int virt, unsigned int phys)
  111. {
  112. unsigned int rA;
  113. int bolted;
  114. /* Base rA is HW way select, way 0, bolted bit set */
  115. rA = 0x88000000;
  116. /* Look for a bolted entry slot */
  117. bolted = ppc47x_find_free_bolted();
  118. BUG_ON(bolted < 0);
  119. /* Insert bolted slot number */
  120. rA |= bolted << 24;
  121. pr_debug("256M TLB entry for 0x%08x->0x%08x in bolt slot %d\n",
  122. virt, phys, bolted);
  123. mtspr(SPRN_MMUCR, 0);
  124. __asm__ __volatile__(
  125. "tlbwe %2,%3,0\n"
  126. "tlbwe %1,%3,1\n"
  127. "tlbwe %0,%3,2\n"
  128. :
  129. : "r" (PPC47x_TLB2_SW | PPC47x_TLB2_SR |
  130. PPC47x_TLB2_SX
  131. #ifdef CONFIG_SMP
  132. | PPC47x_TLB2_M
  133. #endif
  134. ),
  135. "r" (phys),
  136. "r" (virt | PPC47x_TLB0_VALID | PPC47x_TLB0_256M),
  137. "r" (rA));
  138. }
  139. void __init MMU_init_hw(void)
  140. {
  141. /* This is not useful on 47x but won't hurt either */
  142. ppc44x_update_tlb_hwater();
  143. flush_instruction_cache();
  144. }
  145. unsigned long __init mmu_mapin_ram(unsigned long base, unsigned long top)
  146. {
  147. unsigned long addr;
  148. unsigned long memstart = memstart_addr & ~(PPC_PIN_SIZE - 1);
  149. /* Pin in enough TLBs to cover any lowmem not covered by the
  150. * initial 256M mapping established in head_44x.S */
  151. for (addr = memstart + PPC_PIN_SIZE; addr < lowmem_end_addr;
  152. addr += PPC_PIN_SIZE) {
  153. if (mmu_has_feature(MMU_FTR_TYPE_47x))
  154. ppc47x_pin_tlb(addr + PAGE_OFFSET, addr);
  155. else
  156. ppc44x_pin_tlb(addr + PAGE_OFFSET, addr);
  157. }
  158. if (mmu_has_feature(MMU_FTR_TYPE_47x)) {
  159. ppc47x_update_boltmap();
  160. #ifdef DEBUG
  161. {
  162. int i;
  163. printk(KERN_DEBUG "bolted entries: ");
  164. for (i = 0; i < 255; i++) {
  165. if (test_bit(i, tlb_47x_boltmap))
  166. printk("%d ", i);
  167. }
  168. printk("\n");
  169. }
  170. #endif /* DEBUG */
  171. }
  172. return total_lowmem;
  173. }
  174. void setup_initial_memory_limit(phys_addr_t first_memblock_base,
  175. phys_addr_t first_memblock_size)
  176. {
  177. u64 size;
  178. #ifndef CONFIG_NONSTATIC_KERNEL
  179. /* We don't currently support the first MEMBLOCK not mapping 0
  180. * physical on those processors
  181. */
  182. BUG_ON(first_memblock_base != 0);
  183. #endif
  184. /* 44x has a 256M TLB entry pinned at boot */
  185. size = (min_t(u64, first_memblock_size, PPC_PIN_SIZE));
  186. memblock_set_current_limit(first_memblock_base + size);
  187. }
  188. #ifdef CONFIG_SMP
  189. void __init mmu_init_secondary(int cpu)
  190. {
  191. unsigned long addr;
  192. unsigned long memstart = memstart_addr & ~(PPC_PIN_SIZE - 1);
  193. /* Pin in enough TLBs to cover any lowmem not covered by the
  194. * initial 256M mapping established in head_44x.S
  195. *
  196. * WARNING: This is called with only the first 256M of the
  197. * linear mapping in the TLB and we can't take faults yet
  198. * so beware of what this code uses. It runs off a temporary
  199. * stack. current (r2) isn't initialized, smp_processor_id()
  200. * will not work, current thread info isn't accessible, ...
  201. */
  202. for (addr = memstart + PPC_PIN_SIZE; addr < lowmem_end_addr;
  203. addr += PPC_PIN_SIZE) {
  204. if (mmu_has_feature(MMU_FTR_TYPE_47x))
  205. ppc47x_pin_tlb(addr + PAGE_OFFSET, addr);
  206. else
  207. ppc44x_pin_tlb(addr + PAGE_OFFSET, addr);
  208. }
  209. }
  210. #endif /* CONFIG_SMP */