44x_mmu.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 <asm/mmu.h>
  27. #include <asm/system.h>
  28. #include <asm/page.h>
  29. #include <asm/cacheflush.h>
  30. #include "mmu_decl.h"
  31. /* Used by the 44x TLB replacement exception handler.
  32. * Just needed it declared someplace.
  33. */
  34. unsigned int tlb_44x_index; /* = 0 */
  35. unsigned int tlb_44x_hwater = PPC44x_TLB_SIZE - 1 - PPC44x_EARLY_TLBS;
  36. int icache_44x_need_flush;
  37. unsigned long tlb_47x_boltmap[1024/8];
  38. static void __cpuinit ppc44x_update_tlb_hwater(void)
  39. {
  40. extern unsigned int tlb_44x_patch_hwater_D[];
  41. extern unsigned int tlb_44x_patch_hwater_I[];
  42. /* The TLB miss handlers hard codes the watermark in a cmpli
  43. * instruction to improve performances rather than loading it
  44. * from the global variable. Thus, we patch the instructions
  45. * in the 2 TLB miss handlers when updating the value
  46. */
  47. tlb_44x_patch_hwater_D[0] = (tlb_44x_patch_hwater_D[0] & 0xffff0000) |
  48. tlb_44x_hwater;
  49. flush_icache_range((unsigned long)&tlb_44x_patch_hwater_D[0],
  50. (unsigned long)&tlb_44x_patch_hwater_D[1]);
  51. tlb_44x_patch_hwater_I[0] = (tlb_44x_patch_hwater_I[0] & 0xffff0000) |
  52. tlb_44x_hwater;
  53. flush_icache_range((unsigned long)&tlb_44x_patch_hwater_I[0],
  54. (unsigned long)&tlb_44x_patch_hwater_I[1]);
  55. }
  56. /*
  57. * "Pins" a 256MB TLB entry in AS0 for kernel lowmem for 44x type MMU
  58. */
  59. static void __init ppc44x_pin_tlb(unsigned int virt, unsigned int phys)
  60. {
  61. unsigned int entry = tlb_44x_hwater--;
  62. ppc44x_update_tlb_hwater();
  63. mtspr(SPRN_MMUCR, 0);
  64. __asm__ __volatile__(
  65. "tlbwe %2,%3,%4\n"
  66. "tlbwe %1,%3,%5\n"
  67. "tlbwe %0,%3,%6\n"
  68. :
  69. #ifdef CONFIG_PPC47x
  70. : "r" (PPC47x_TLB2_S_RWX),
  71. #else
  72. : "r" (PPC44x_TLB_SW | PPC44x_TLB_SR | PPC44x_TLB_SX | PPC44x_TLB_G),
  73. #endif
  74. "r" (phys),
  75. "r" (virt | PPC44x_TLB_VALID | PPC44x_TLB_256M),
  76. "r" (entry),
  77. "i" (PPC44x_TLB_PAGEID),
  78. "i" (PPC44x_TLB_XLAT),
  79. "i" (PPC44x_TLB_ATTRIB));
  80. }
  81. static int __init ppc47x_find_free_bolted(void)
  82. {
  83. unsigned int mmube0 = mfspr(SPRN_MMUBE0);
  84. unsigned int mmube1 = mfspr(SPRN_MMUBE1);
  85. if (!(mmube0 & MMUBE0_VBE0))
  86. return 0;
  87. if (!(mmube0 & MMUBE0_VBE1))
  88. return 1;
  89. if (!(mmube0 & MMUBE0_VBE2))
  90. return 2;
  91. if (!(mmube1 & MMUBE1_VBE3))
  92. return 3;
  93. if (!(mmube1 & MMUBE1_VBE4))
  94. return 4;
  95. if (!(mmube1 & MMUBE1_VBE5))
  96. return 5;
  97. return -1;
  98. }
  99. static void __init ppc47x_update_boltmap(void)
  100. {
  101. unsigned int mmube0 = mfspr(SPRN_MMUBE0);
  102. unsigned int mmube1 = mfspr(SPRN_MMUBE1);
  103. if (mmube0 & MMUBE0_VBE0)
  104. __set_bit((mmube0 >> MMUBE0_IBE0_SHIFT) & 0xff,
  105. tlb_47x_boltmap);
  106. if (mmube0 & MMUBE0_VBE1)
  107. __set_bit((mmube0 >> MMUBE0_IBE1_SHIFT) & 0xff,
  108. tlb_47x_boltmap);
  109. if (mmube0 & MMUBE0_VBE2)
  110. __set_bit((mmube0 >> MMUBE0_IBE2_SHIFT) & 0xff,
  111. tlb_47x_boltmap);
  112. if (mmube1 & MMUBE1_VBE3)
  113. __set_bit((mmube1 >> MMUBE1_IBE3_SHIFT) & 0xff,
  114. tlb_47x_boltmap);
  115. if (mmube1 & MMUBE1_VBE4)
  116. __set_bit((mmube1 >> MMUBE1_IBE4_SHIFT) & 0xff,
  117. tlb_47x_boltmap);
  118. if (mmube1 & MMUBE1_VBE5)
  119. __set_bit((mmube1 >> MMUBE1_IBE5_SHIFT) & 0xff,
  120. tlb_47x_boltmap);
  121. }
  122. /*
  123. * "Pins" a 256MB TLB entry in AS0 for kernel lowmem for 47x type MMU
  124. */
  125. static void __cpuinit ppc47x_pin_tlb(unsigned int virt, unsigned int phys)
  126. {
  127. unsigned int rA;
  128. int bolted;
  129. /* Base rA is HW way select, way 0, bolted bit set */
  130. rA = 0x88000000;
  131. /* Look for a bolted entry slot */
  132. bolted = ppc47x_find_free_bolted();
  133. BUG_ON(bolted < 0);
  134. /* Insert bolted slot number */
  135. rA |= bolted << 24;
  136. pr_debug("256M TLB entry for 0x%08x->0x%08x in bolt slot %d\n",
  137. virt, phys, bolted);
  138. mtspr(SPRN_MMUCR, 0);
  139. __asm__ __volatile__(
  140. "tlbwe %2,%3,0\n"
  141. "tlbwe %1,%3,1\n"
  142. "tlbwe %0,%3,2\n"
  143. :
  144. : "r" (PPC47x_TLB2_SW | PPC47x_TLB2_SR |
  145. PPC47x_TLB2_SX
  146. #ifdef CONFIG_SMP
  147. | PPC47x_TLB2_M
  148. #endif
  149. ),
  150. "r" (phys),
  151. "r" (virt | PPC47x_TLB0_VALID | PPC47x_TLB0_256M),
  152. "r" (rA));
  153. }
  154. void __init MMU_init_hw(void)
  155. {
  156. /* This is not useful on 47x but won't hurt either */
  157. ppc44x_update_tlb_hwater();
  158. flush_instruction_cache();
  159. }
  160. unsigned long __init mmu_mapin_ram(unsigned long top)
  161. {
  162. unsigned long addr;
  163. /* Pin in enough TLBs to cover any lowmem not covered by the
  164. * initial 256M mapping established in head_44x.S */
  165. for (addr = PPC_PIN_SIZE; addr < lowmem_end_addr;
  166. addr += PPC_PIN_SIZE) {
  167. if (mmu_has_feature(MMU_FTR_TYPE_47x))
  168. ppc47x_pin_tlb(addr + PAGE_OFFSET, addr);
  169. else
  170. ppc44x_pin_tlb(addr + PAGE_OFFSET, addr);
  171. }
  172. if (mmu_has_feature(MMU_FTR_TYPE_47x)) {
  173. ppc47x_update_boltmap();
  174. #ifdef DEBUG
  175. {
  176. int i;
  177. printk(KERN_DEBUG "bolted entries: ");
  178. for (i = 0; i < 255; i++) {
  179. if (test_bit(i, tlb_47x_boltmap))
  180. printk("%d ", i);
  181. }
  182. printk("\n");
  183. }
  184. #endif /* DEBUG */
  185. }
  186. return total_lowmem;
  187. }
  188. #ifdef CONFIG_SMP
  189. void __cpuinit mmu_init_secondary(int cpu)
  190. {
  191. unsigned long addr;
  192. /* Pin in enough TLBs to cover any lowmem not covered by the
  193. * initial 256M mapping established in head_44x.S
  194. *
  195. * WARNING: This is called with only the first 256M of the
  196. * linear mapping in the TLB and we can't take faults yet
  197. * so beware of what this code uses. It runs off a temporary
  198. * stack. current (r2) isn't initialized, smp_processor_id()
  199. * will not work, current thread info isn't accessible, ...
  200. */
  201. for (addr = PPC_PIN_SIZE; addr < lowmem_end_addr;
  202. addr += PPC_PIN_SIZE) {
  203. if (mmu_has_feature(MMU_FTR_TYPE_47x))
  204. ppc47x_pin_tlb(addr + PAGE_OFFSET, addr);
  205. else
  206. ppc44x_pin_tlb(addr + PAGE_OFFSET, addr);
  207. }
  208. }
  209. #endif /* CONFIG_SMP */