44x_mmu.c 6.8 KB

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