ppc_mmu.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. * Amiga/APUS changes by Jesper Skov (jskov@cygnus.co.uk).
  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/config.h>
  26. #include <linux/kernel.h>
  27. #include <linux/mm.h>
  28. #include <linux/init.h>
  29. #include <linux/highmem.h>
  30. #include <asm/prom.h>
  31. #include <asm/mmu.h>
  32. #include <asm/machdep.h>
  33. #include "mmu_decl.h"
  34. #include "mem_pieces.h"
  35. PTE *Hash, *Hash_end;
  36. unsigned long Hash_size, Hash_mask;
  37. unsigned long _SDR1;
  38. union ubat { /* BAT register values to be loaded */
  39. BAT bat;
  40. #ifdef CONFIG_PPC64BRIDGE
  41. u64 word[2];
  42. #else
  43. u32 word[2];
  44. #endif
  45. } BATS[4][2]; /* 4 pairs of IBAT, DBAT */
  46. struct batrange { /* stores address ranges mapped by BATs */
  47. unsigned long start;
  48. unsigned long limit;
  49. unsigned long phys;
  50. } bat_addrs[4];
  51. /*
  52. * Return PA for this VA if it is mapped by a BAT, or 0
  53. */
  54. unsigned long v_mapped_by_bats(unsigned long va)
  55. {
  56. int b;
  57. for (b = 0; b < 4; ++b)
  58. if (va >= bat_addrs[b].start && va < bat_addrs[b].limit)
  59. return bat_addrs[b].phys + (va - bat_addrs[b].start);
  60. return 0;
  61. }
  62. /*
  63. * Return VA for a given PA or 0 if not mapped
  64. */
  65. unsigned long p_mapped_by_bats(unsigned long pa)
  66. {
  67. int b;
  68. for (b = 0; b < 4; ++b)
  69. if (pa >= bat_addrs[b].phys
  70. && pa < (bat_addrs[b].limit-bat_addrs[b].start)
  71. +bat_addrs[b].phys)
  72. return bat_addrs[b].start+(pa-bat_addrs[b].phys);
  73. return 0;
  74. }
  75. unsigned long __init mmu_mapin_ram(void)
  76. {
  77. #ifdef CONFIG_POWER4
  78. return 0;
  79. #else
  80. unsigned long tot, bl, done;
  81. unsigned long max_size = (256<<20);
  82. unsigned long align;
  83. if (__map_without_bats)
  84. return 0;
  85. /* Set up BAT2 and if necessary BAT3 to cover RAM. */
  86. /* Make sure we don't map a block larger than the
  87. smallest alignment of the physical address. */
  88. /* alignment of PPC_MEMSTART */
  89. align = ~(PPC_MEMSTART-1) & PPC_MEMSTART;
  90. /* set BAT block size to MIN(max_size, align) */
  91. if (align && align < max_size)
  92. max_size = align;
  93. tot = total_lowmem;
  94. for (bl = 128<<10; bl < max_size; bl <<= 1) {
  95. if (bl * 2 > tot)
  96. break;
  97. }
  98. setbat(2, KERNELBASE, PPC_MEMSTART, bl, _PAGE_RAM);
  99. done = (unsigned long)bat_addrs[2].limit - KERNELBASE + 1;
  100. if ((done < tot) && !bat_addrs[3].limit) {
  101. /* use BAT3 to cover a bit more */
  102. tot -= done;
  103. for (bl = 128<<10; bl < max_size; bl <<= 1)
  104. if (bl * 2 > tot)
  105. break;
  106. setbat(3, KERNELBASE+done, PPC_MEMSTART+done, bl, _PAGE_RAM);
  107. done = (unsigned long)bat_addrs[3].limit - KERNELBASE + 1;
  108. }
  109. return done;
  110. #endif
  111. }
  112. /*
  113. * Set up one of the I/D BAT (block address translation) register pairs.
  114. * The parameters are not checked; in particular size must be a power
  115. * of 2 between 128k and 256M.
  116. */
  117. void __init setbat(int index, unsigned long virt, unsigned long phys,
  118. unsigned int size, int flags)
  119. {
  120. unsigned int bl;
  121. int wimgxpp;
  122. union ubat *bat = BATS[index];
  123. if (((flags & _PAGE_NO_CACHE) == 0) &&
  124. cpu_has_feature(CPU_FTR_NEED_COHERENT))
  125. flags |= _PAGE_COHERENT;
  126. bl = (size >> 17) - 1;
  127. if (PVR_VER(mfspr(SPRN_PVR)) != 1) {
  128. /* 603, 604, etc. */
  129. /* Do DBAT first */
  130. wimgxpp = flags & (_PAGE_WRITETHRU | _PAGE_NO_CACHE
  131. | _PAGE_COHERENT | _PAGE_GUARDED);
  132. wimgxpp |= (flags & _PAGE_RW)? BPP_RW: BPP_RX;
  133. bat[1].word[0] = virt | (bl << 2) | 2; /* Vs=1, Vp=0 */
  134. bat[1].word[1] = phys | wimgxpp;
  135. #ifndef CONFIG_KGDB /* want user access for breakpoints */
  136. if (flags & _PAGE_USER)
  137. #endif
  138. bat[1].bat.batu.vp = 1;
  139. if (flags & _PAGE_GUARDED) {
  140. /* G bit must be zero in IBATs */
  141. bat[0].word[0] = bat[0].word[1] = 0;
  142. } else {
  143. /* make IBAT same as DBAT */
  144. bat[0] = bat[1];
  145. }
  146. } else {
  147. /* 601 cpu */
  148. if (bl > BL_8M)
  149. bl = BL_8M;
  150. wimgxpp = flags & (_PAGE_WRITETHRU | _PAGE_NO_CACHE
  151. | _PAGE_COHERENT);
  152. wimgxpp |= (flags & _PAGE_RW)?
  153. ((flags & _PAGE_USER)? PP_RWRW: PP_RWXX): PP_RXRX;
  154. bat->word[0] = virt | wimgxpp | 4; /* Ks=0, Ku=1 */
  155. bat->word[1] = phys | bl | 0x40; /* V=1 */
  156. }
  157. bat_addrs[index].start = virt;
  158. bat_addrs[index].limit = virt + ((bl + 1) << 17) - 1;
  159. bat_addrs[index].phys = phys;
  160. }
  161. /*
  162. * Initialize the hash table and patch the instructions in hashtable.S.
  163. */
  164. void __init MMU_init_hw(void)
  165. {
  166. unsigned int hmask, mb, mb2;
  167. unsigned int n_hpteg, lg_n_hpteg;
  168. extern unsigned int hash_page_patch_A[];
  169. extern unsigned int hash_page_patch_B[], hash_page_patch_C[];
  170. extern unsigned int hash_page[];
  171. extern unsigned int flush_hash_patch_A[], flush_hash_patch_B[];
  172. if (!cpu_has_feature(CPU_FTR_HPTE_TABLE)) {
  173. /*
  174. * Put a blr (procedure return) instruction at the
  175. * start of hash_page, since we can still get DSI
  176. * exceptions on a 603.
  177. */
  178. hash_page[0] = 0x4e800020;
  179. flush_icache_range((unsigned long) &hash_page[0],
  180. (unsigned long) &hash_page[1]);
  181. return;
  182. }
  183. if ( ppc_md.progress ) ppc_md.progress("hash:enter", 0x105);
  184. #ifdef CONFIG_PPC64BRIDGE
  185. #define LG_HPTEG_SIZE 7 /* 128 bytes per HPTEG */
  186. #define SDR1_LOW_BITS (lg_n_hpteg - 11)
  187. #define MIN_N_HPTEG 2048 /* min 256kB hash table */
  188. #else
  189. #define LG_HPTEG_SIZE 6 /* 64 bytes per HPTEG */
  190. #define SDR1_LOW_BITS ((n_hpteg - 1) >> 10)
  191. #define MIN_N_HPTEG 1024 /* min 64kB hash table */
  192. #endif
  193. #ifdef CONFIG_POWER4
  194. /* The hash table has already been allocated and initialized
  195. in prom.c */
  196. n_hpteg = Hash_size >> LG_HPTEG_SIZE;
  197. lg_n_hpteg = __ilog2(n_hpteg);
  198. /* Remove the hash table from the available memory */
  199. if (Hash)
  200. reserve_phys_mem(__pa(Hash), Hash_size);
  201. #else /* CONFIG_POWER4 */
  202. /*
  203. * Allow 1 HPTE (1/8 HPTEG) for each page of memory.
  204. * This is less than the recommended amount, but then
  205. * Linux ain't AIX.
  206. */
  207. n_hpteg = total_memory / (PAGE_SIZE * 8);
  208. if (n_hpteg < MIN_N_HPTEG)
  209. n_hpteg = MIN_N_HPTEG;
  210. lg_n_hpteg = __ilog2(n_hpteg);
  211. if (n_hpteg & (n_hpteg - 1)) {
  212. ++lg_n_hpteg; /* round up if not power of 2 */
  213. n_hpteg = 1 << lg_n_hpteg;
  214. }
  215. Hash_size = n_hpteg << LG_HPTEG_SIZE;
  216. /*
  217. * Find some memory for the hash table.
  218. */
  219. if ( ppc_md.progress ) ppc_md.progress("hash:find piece", 0x322);
  220. Hash = mem_pieces_find(Hash_size, Hash_size);
  221. cacheable_memzero(Hash, Hash_size);
  222. _SDR1 = __pa(Hash) | SDR1_LOW_BITS;
  223. #endif /* CONFIG_POWER4 */
  224. Hash_end = (PTE *) ((unsigned long)Hash + Hash_size);
  225. printk("Total memory = %ldMB; using %ldkB for hash table (at %p)\n",
  226. total_memory >> 20, Hash_size >> 10, Hash);
  227. /*
  228. * Patch up the instructions in hashtable.S:create_hpte
  229. */
  230. if ( ppc_md.progress ) ppc_md.progress("hash:patch", 0x345);
  231. Hash_mask = n_hpteg - 1;
  232. hmask = Hash_mask >> (16 - LG_HPTEG_SIZE);
  233. mb2 = mb = 32 - LG_HPTEG_SIZE - lg_n_hpteg;
  234. if (lg_n_hpteg > 16)
  235. mb2 = 16 - LG_HPTEG_SIZE;
  236. hash_page_patch_A[0] = (hash_page_patch_A[0] & ~0xffff)
  237. | ((unsigned int)(Hash) >> 16);
  238. hash_page_patch_A[1] = (hash_page_patch_A[1] & ~0x7c0) | (mb << 6);
  239. hash_page_patch_A[2] = (hash_page_patch_A[2] & ~0x7c0) | (mb2 << 6);
  240. hash_page_patch_B[0] = (hash_page_patch_B[0] & ~0xffff) | hmask;
  241. hash_page_patch_C[0] = (hash_page_patch_C[0] & ~0xffff) | hmask;
  242. /*
  243. * Ensure that the locations we've patched have been written
  244. * out from the data cache and invalidated in the instruction
  245. * cache, on those machines with split caches.
  246. */
  247. flush_icache_range((unsigned long) &hash_page_patch_A[0],
  248. (unsigned long) &hash_page_patch_C[1]);
  249. /*
  250. * Patch up the instructions in hashtable.S:flush_hash_page
  251. */
  252. flush_hash_patch_A[0] = (flush_hash_patch_A[0] & ~0xffff)
  253. | ((unsigned int)(Hash) >> 16);
  254. flush_hash_patch_A[1] = (flush_hash_patch_A[1] & ~0x7c0) | (mb << 6);
  255. flush_hash_patch_A[2] = (flush_hash_patch_A[2] & ~0x7c0) | (mb2 << 6);
  256. flush_hash_patch_B[0] = (flush_hash_patch_B[0] & ~0xffff) | hmask;
  257. flush_icache_range((unsigned long) &flush_hash_patch_A[0],
  258. (unsigned long) &flush_hash_patch_B[1]);
  259. if ( ppc_md.progress ) ppc_md.progress("hash:done", 0x205);
  260. }