ppc_mmu_32.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * This file contains the routines for handling the MMU on those
  3. * PowerPC implementations where the MMU substantially follows the
  4. * architecture specification. This includes the 6xx, 7xx, 7xxx,
  5. * 8260, and POWER3 implementations but excludes the 8xx and 4xx.
  6. * -- paulus
  7. *
  8. * Derived from arch/ppc/mm/init.c:
  9. * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
  10. *
  11. * Modifications by Paul Mackerras (PowerMac) (paulus@cs.anu.edu.au)
  12. * and Cort Dougan (PReP) (cort@cs.nmt.edu)
  13. * Copyright (C) 1996 Paul Mackerras
  14. *
  15. * Derived from "arch/i386/mm/init.c"
  16. * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
  17. *
  18. * This program is free software; you can redistribute it and/or
  19. * modify it under the terms of the GNU General Public License
  20. * as published by the Free Software Foundation; either version
  21. * 2 of the License, or (at your option) any later version.
  22. *
  23. */
  24. #include <linux/kernel.h>
  25. #include <linux/mm.h>
  26. #include <linux/init.h>
  27. #include <linux/highmem.h>
  28. #include <linux/lmb.h>
  29. #include <asm/prom.h>
  30. #include <asm/mmu.h>
  31. #include <asm/machdep.h>
  32. #include "mmu_decl.h"
  33. struct hash_pte *Hash, *Hash_end;
  34. unsigned long Hash_size, Hash_mask;
  35. unsigned long _SDR1;
  36. union ubat { /* BAT register values to be loaded */
  37. struct ppc_bat bat;
  38. u32 word[2];
  39. } BATS[8][2]; /* 8 pairs of IBAT, DBAT */
  40. struct batrange { /* stores address ranges mapped by BATs */
  41. unsigned long start;
  42. unsigned long limit;
  43. unsigned long phys;
  44. } bat_addrs[8];
  45. /*
  46. * Return PA for this VA if it is mapped by a BAT, or 0
  47. */
  48. unsigned long v_mapped_by_bats(unsigned long va)
  49. {
  50. int b;
  51. for (b = 0; b < 4; ++b)
  52. if (va >= bat_addrs[b].start && va < bat_addrs[b].limit)
  53. return bat_addrs[b].phys + (va - bat_addrs[b].start);
  54. return 0;
  55. }
  56. /*
  57. * Return VA for a given PA or 0 if not mapped
  58. */
  59. unsigned long p_mapped_by_bats(unsigned long pa)
  60. {
  61. int b;
  62. for (b = 0; b < 4; ++b)
  63. if (pa >= bat_addrs[b].phys
  64. && pa < (bat_addrs[b].limit-bat_addrs[b].start)
  65. +bat_addrs[b].phys)
  66. return bat_addrs[b].start+(pa-bat_addrs[b].phys);
  67. return 0;
  68. }
  69. unsigned long __init mmu_mapin_ram(void)
  70. {
  71. #ifdef CONFIG_POWER4
  72. return 0;
  73. #else
  74. unsigned long tot, bl, done;
  75. unsigned long max_size = (256<<20);
  76. if (__map_without_bats) {
  77. printk(KERN_DEBUG "RAM mapped without BATs\n");
  78. return 0;
  79. }
  80. /* Set up BAT2 and if necessary BAT3 to cover RAM. */
  81. /* Make sure we don't map a block larger than the
  82. smallest alignment of the physical address. */
  83. tot = total_lowmem;
  84. for (bl = 128<<10; bl < max_size; bl <<= 1) {
  85. if (bl * 2 > tot)
  86. break;
  87. }
  88. setbat(2, KERNELBASE, 0, bl, _PAGE_RAM);
  89. done = (unsigned long)bat_addrs[2].limit - KERNELBASE + 1;
  90. if ((done < tot) && !bat_addrs[3].limit) {
  91. /* use BAT3 to cover a bit more */
  92. tot -= done;
  93. for (bl = 128<<10; bl < max_size; bl <<= 1)
  94. if (bl * 2 > tot)
  95. break;
  96. setbat(3, KERNELBASE+done, done, bl, _PAGE_RAM);
  97. done = (unsigned long)bat_addrs[3].limit - KERNELBASE + 1;
  98. }
  99. return done;
  100. #endif
  101. }
  102. /*
  103. * Set up one of the I/D BAT (block address translation) register pairs.
  104. * The parameters are not checked; in particular size must be a power
  105. * of 2 between 128k and 256M.
  106. */
  107. void __init setbat(int index, unsigned long virt, unsigned long phys,
  108. unsigned int size, int flags)
  109. {
  110. unsigned int bl;
  111. int wimgxpp;
  112. union ubat *bat = BATS[index];
  113. if (((flags & _PAGE_NO_CACHE) == 0) &&
  114. cpu_has_feature(CPU_FTR_NEED_COHERENT))
  115. flags |= _PAGE_COHERENT;
  116. bl = (size >> 17) - 1;
  117. if (PVR_VER(mfspr(SPRN_PVR)) != 1) {
  118. /* 603, 604, etc. */
  119. /* Do DBAT first */
  120. wimgxpp = flags & (_PAGE_WRITETHRU | _PAGE_NO_CACHE
  121. | _PAGE_COHERENT | _PAGE_GUARDED);
  122. wimgxpp |= (flags & _PAGE_RW)? BPP_RW: BPP_RX;
  123. bat[1].word[0] = virt | (bl << 2) | 2; /* Vs=1, Vp=0 */
  124. bat[1].word[1] = phys | wimgxpp;
  125. #ifndef CONFIG_KGDB /* want user access for breakpoints */
  126. if (flags & _PAGE_USER)
  127. #endif
  128. bat[1].bat.batu.vp = 1;
  129. if (flags & _PAGE_GUARDED) {
  130. /* G bit must be zero in IBATs */
  131. bat[0].word[0] = bat[0].word[1] = 0;
  132. } else {
  133. /* make IBAT same as DBAT */
  134. bat[0] = bat[1];
  135. }
  136. } else {
  137. /* 601 cpu */
  138. if (bl > BL_8M)
  139. bl = BL_8M;
  140. wimgxpp = flags & (_PAGE_WRITETHRU | _PAGE_NO_CACHE
  141. | _PAGE_COHERENT);
  142. wimgxpp |= (flags & _PAGE_RW)?
  143. ((flags & _PAGE_USER)? PP_RWRW: PP_RWXX): PP_RXRX;
  144. bat->word[0] = virt | wimgxpp | 4; /* Ks=0, Ku=1 */
  145. bat->word[1] = phys | bl | 0x40; /* V=1 */
  146. }
  147. bat_addrs[index].start = virt;
  148. bat_addrs[index].limit = virt + ((bl + 1) << 17) - 1;
  149. bat_addrs[index].phys = phys;
  150. }
  151. /*
  152. * Preload a translation in the hash table
  153. */
  154. void hash_preload(struct mm_struct *mm, unsigned long ea,
  155. unsigned long access, unsigned long trap)
  156. {
  157. pmd_t *pmd;
  158. if (Hash == 0)
  159. return;
  160. pmd = pmd_offset(pud_offset(pgd_offset(mm, ea), ea), ea);
  161. if (!pmd_none(*pmd))
  162. add_hash_page(mm->context.id, ea, pmd_val(*pmd));
  163. }
  164. /*
  165. * Initialize the hash table and patch the instructions in hashtable.S.
  166. */
  167. void __init MMU_init_hw(void)
  168. {
  169. unsigned int hmask, mb, mb2;
  170. unsigned int n_hpteg, lg_n_hpteg;
  171. extern unsigned int hash_page_patch_A[];
  172. extern unsigned int hash_page_patch_B[], hash_page_patch_C[];
  173. extern unsigned int hash_page[];
  174. extern unsigned int flush_hash_patch_A[], flush_hash_patch_B[];
  175. if (!cpu_has_feature(CPU_FTR_HPTE_TABLE)) {
  176. /*
  177. * Put a blr (procedure return) instruction at the
  178. * start of hash_page, since we can still get DSI
  179. * exceptions on a 603.
  180. */
  181. hash_page[0] = 0x4e800020;
  182. flush_icache_range((unsigned long) &hash_page[0],
  183. (unsigned long) &hash_page[1]);
  184. return;
  185. }
  186. if ( ppc_md.progress ) ppc_md.progress("hash:enter", 0x105);
  187. #define LG_HPTEG_SIZE 6 /* 64 bytes per HPTEG */
  188. #define SDR1_LOW_BITS ((n_hpteg - 1) >> 10)
  189. #define MIN_N_HPTEG 1024 /* min 64kB hash table */
  190. /*
  191. * Allow 1 HPTE (1/8 HPTEG) for each page of memory.
  192. * This is less than the recommended amount, but then
  193. * Linux ain't AIX.
  194. */
  195. n_hpteg = total_memory / (PAGE_SIZE * 8);
  196. if (n_hpteg < MIN_N_HPTEG)
  197. n_hpteg = MIN_N_HPTEG;
  198. lg_n_hpteg = __ilog2(n_hpteg);
  199. if (n_hpteg & (n_hpteg - 1)) {
  200. ++lg_n_hpteg; /* round up if not power of 2 */
  201. n_hpteg = 1 << lg_n_hpteg;
  202. }
  203. Hash_size = n_hpteg << LG_HPTEG_SIZE;
  204. /*
  205. * Find some memory for the hash table.
  206. */
  207. if ( ppc_md.progress ) ppc_md.progress("hash:find piece", 0x322);
  208. Hash = __va(lmb_alloc_base(Hash_size, Hash_size,
  209. __initial_memory_limit_addr));
  210. cacheable_memzero(Hash, Hash_size);
  211. _SDR1 = __pa(Hash) | SDR1_LOW_BITS;
  212. Hash_end = (struct hash_pte *) ((unsigned long)Hash + Hash_size);
  213. printk("Total memory = %ldMB; using %ldkB for hash table (at %p)\n",
  214. total_memory >> 20, Hash_size >> 10, Hash);
  215. /*
  216. * Patch up the instructions in hashtable.S:create_hpte
  217. */
  218. if ( ppc_md.progress ) ppc_md.progress("hash:patch", 0x345);
  219. Hash_mask = n_hpteg - 1;
  220. hmask = Hash_mask >> (16 - LG_HPTEG_SIZE);
  221. mb2 = mb = 32 - LG_HPTEG_SIZE - lg_n_hpteg;
  222. if (lg_n_hpteg > 16)
  223. mb2 = 16 - LG_HPTEG_SIZE;
  224. hash_page_patch_A[0] = (hash_page_patch_A[0] & ~0xffff)
  225. | ((unsigned int)(Hash) >> 16);
  226. hash_page_patch_A[1] = (hash_page_patch_A[1] & ~0x7c0) | (mb << 6);
  227. hash_page_patch_A[2] = (hash_page_patch_A[2] & ~0x7c0) | (mb2 << 6);
  228. hash_page_patch_B[0] = (hash_page_patch_B[0] & ~0xffff) | hmask;
  229. hash_page_patch_C[0] = (hash_page_patch_C[0] & ~0xffff) | hmask;
  230. /*
  231. * Ensure that the locations we've patched have been written
  232. * out from the data cache and invalidated in the instruction
  233. * cache, on those machines with split caches.
  234. */
  235. flush_icache_range((unsigned long) &hash_page_patch_A[0],
  236. (unsigned long) &hash_page_patch_C[1]);
  237. /*
  238. * Patch up the instructions in hashtable.S:flush_hash_page
  239. */
  240. flush_hash_patch_A[0] = (flush_hash_patch_A[0] & ~0xffff)
  241. | ((unsigned int)(Hash) >> 16);
  242. flush_hash_patch_A[1] = (flush_hash_patch_A[1] & ~0x7c0) | (mb << 6);
  243. flush_hash_patch_A[2] = (flush_hash_patch_A[2] & ~0x7c0) | (mb2 << 6);
  244. flush_hash_patch_B[0] = (flush_hash_patch_B[0] & ~0xffff) | hmask;
  245. flush_icache_range((unsigned long) &flush_hash_patch_A[0],
  246. (unsigned long) &flush_hash_patch_B[1]);
  247. if ( ppc_md.progress ) ppc_md.progress("hash:done", 0x205);
  248. }