44x_mmu.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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/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 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. : "r" (PPC44x_TLB_SW | PPC44x_TLB_SR | PPC44x_TLB_SX | PPC44x_TLB_G),
  70. "r" (phys),
  71. "r" (virt | PPC44x_TLB_VALID | PPC44x_TLB_256M),
  72. "r" (entry),
  73. "i" (PPC44x_TLB_PAGEID),
  74. "i" (PPC44x_TLB_XLAT),
  75. "i" (PPC44x_TLB_ATTRIB));
  76. }
  77. static int __init ppc47x_find_free_bolted(void)
  78. {
  79. unsigned int mmube0 = mfspr(SPRN_MMUBE0);
  80. unsigned int mmube1 = mfspr(SPRN_MMUBE1);
  81. if (!(mmube0 & MMUBE0_VBE0))
  82. return 0;
  83. if (!(mmube0 & MMUBE0_VBE1))
  84. return 1;
  85. if (!(mmube0 & MMUBE0_VBE2))
  86. return 2;
  87. if (!(mmube1 & MMUBE1_VBE3))
  88. return 3;
  89. if (!(mmube1 & MMUBE1_VBE4))
  90. return 4;
  91. if (!(mmube1 & MMUBE1_VBE5))
  92. return 5;
  93. return -1;
  94. }
  95. static void __init ppc47x_update_boltmap(void)
  96. {
  97. unsigned int mmube0 = mfspr(SPRN_MMUBE0);
  98. unsigned int mmube1 = mfspr(SPRN_MMUBE1);
  99. if (mmube0 & MMUBE0_VBE0)
  100. __set_bit((mmube0 >> MMUBE0_IBE0_SHIFT) & 0xff,
  101. tlb_47x_boltmap);
  102. if (mmube0 & MMUBE0_VBE1)
  103. __set_bit((mmube0 >> MMUBE0_IBE1_SHIFT) & 0xff,
  104. tlb_47x_boltmap);
  105. if (mmube0 & MMUBE0_VBE2)
  106. __set_bit((mmube0 >> MMUBE0_IBE2_SHIFT) & 0xff,
  107. tlb_47x_boltmap);
  108. if (mmube1 & MMUBE1_VBE3)
  109. __set_bit((mmube1 >> MMUBE1_IBE3_SHIFT) & 0xff,
  110. tlb_47x_boltmap);
  111. if (mmube1 & MMUBE1_VBE4)
  112. __set_bit((mmube1 >> MMUBE1_IBE4_SHIFT) & 0xff,
  113. tlb_47x_boltmap);
  114. if (mmube1 & MMUBE1_VBE5)
  115. __set_bit((mmube1 >> MMUBE1_IBE5_SHIFT) & 0xff,
  116. tlb_47x_boltmap);
  117. }
  118. /*
  119. * "Pins" a 256MB TLB entry in AS0 for kernel lowmem for 47x type MMU
  120. */
  121. static void ppc47x_pin_tlb(unsigned int virt, unsigned int phys)
  122. {
  123. unsigned int rA;
  124. int bolted;
  125. /* Base rA is HW way select, way 0, bolted bit set */
  126. rA = 0x88000000;
  127. /* Look for a bolted entry slot */
  128. bolted = ppc47x_find_free_bolted();
  129. BUG_ON(bolted < 0);
  130. /* Insert bolted slot number */
  131. rA |= bolted << 24;
  132. pr_debug("256M TLB entry for 0x%08x->0x%08x in bolt slot %d\n",
  133. virt, phys, bolted);
  134. mtspr(SPRN_MMUCR, 0);
  135. __asm__ __volatile__(
  136. "tlbwe %2,%3,0\n"
  137. "tlbwe %1,%3,1\n"
  138. "tlbwe %0,%3,2\n"
  139. :
  140. : "r" (PPC47x_TLB2_SW | PPC47x_TLB2_SR |
  141. PPC47x_TLB2_SX
  142. #ifdef CONFIG_SMP
  143. | PPC47x_TLB2_M
  144. #endif
  145. ),
  146. "r" (phys),
  147. "r" (virt | PPC47x_TLB0_VALID | PPC47x_TLB0_256M),
  148. "r" (rA));
  149. }
  150. void __init MMU_init_hw(void)
  151. {
  152. /* This is not useful on 47x but won't hurt either */
  153. ppc44x_update_tlb_hwater();
  154. flush_instruction_cache();
  155. }
  156. unsigned long __init mmu_mapin_ram(unsigned long top)
  157. {
  158. unsigned long addr;
  159. unsigned long memstart = memstart_addr & ~(PPC_PIN_SIZE - 1);
  160. /* Pin in enough TLBs to cover any lowmem not covered by the
  161. * initial 256M mapping established in head_44x.S */
  162. for (addr = memstart + PPC_PIN_SIZE; addr < lowmem_end_addr;
  163. addr += PPC_PIN_SIZE) {
  164. if (mmu_has_feature(MMU_FTR_TYPE_47x))
  165. ppc47x_pin_tlb(addr + PAGE_OFFSET, addr);
  166. else
  167. ppc44x_pin_tlb(addr + PAGE_OFFSET, addr);
  168. }
  169. if (mmu_has_feature(MMU_FTR_TYPE_47x)) {
  170. ppc47x_update_boltmap();
  171. #ifdef DEBUG
  172. {
  173. int i;
  174. printk(KERN_DEBUG "bolted entries: ");
  175. for (i = 0; i < 255; i++) {
  176. if (test_bit(i, tlb_47x_boltmap))
  177. printk("%d ", i);
  178. }
  179. printk("\n");
  180. }
  181. #endif /* DEBUG */
  182. }
  183. return total_lowmem;
  184. }
  185. void setup_initial_memory_limit(phys_addr_t first_memblock_base,
  186. phys_addr_t first_memblock_size)
  187. {
  188. u64 size;
  189. #ifndef CONFIG_NONSTATIC_KERNEL
  190. /* We don't currently support the first MEMBLOCK not mapping 0
  191. * physical on those processors
  192. */
  193. BUG_ON(first_memblock_base != 0);
  194. #endif
  195. /* 44x has a 256M TLB entry pinned at boot */
  196. size = (min_t(u64, first_memblock_size, PPC_PIN_SIZE));
  197. memblock_set_current_limit(first_memblock_base + size);
  198. }
  199. #ifdef CONFIG_SMP
  200. void mmu_init_secondary(int cpu)
  201. {
  202. unsigned long addr;
  203. unsigned long memstart = memstart_addr & ~(PPC_PIN_SIZE - 1);
  204. /* Pin in enough TLBs to cover any lowmem not covered by the
  205. * initial 256M mapping established in head_44x.S
  206. *
  207. * WARNING: This is called with only the first 256M of the
  208. * linear mapping in the TLB and we can't take faults yet
  209. * so beware of what this code uses. It runs off a temporary
  210. * stack. current (r2) isn't initialized, smp_processor_id()
  211. * will not work, current thread info isn't accessible, ...
  212. */
  213. for (addr = memstart + PPC_PIN_SIZE; addr < lowmem_end_addr;
  214. addr += PPC_PIN_SIZE) {
  215. if (mmu_has_feature(MMU_FTR_TYPE_47x))
  216. ppc47x_pin_tlb(addr + PAGE_OFFSET, addr);
  217. else
  218. ppc44x_pin_tlb(addr + PAGE_OFFSET, addr);
  219. }
  220. }
  221. #endif /* CONFIG_SMP */