PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/arch/powerpc/mm/ppc_mmu_32.c

https://gitlab.com/CadeLaRen/linux
C | 289 lines | 175 code | 37 blank | 77 comment | 37 complexity | 8cad0b9f986193bf4407b79c02acaea0 MD5 | raw file
  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. * and 8260 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/memblock.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. struct ppc_bat BATS[8][2]; /* 8 pairs of IBAT, DBAT */
  37. struct batrange { /* stores address ranges mapped by BATs */
  38. unsigned long start;
  39. unsigned long limit;
  40. phys_addr_t phys;
  41. } bat_addrs[8];
  42. /*
  43. * Return PA for this VA if it is mapped by a BAT, or 0
  44. */
  45. phys_addr_t v_block_mapped(unsigned long va)
  46. {
  47. int b;
  48. for (b = 0; b < 4; ++b)
  49. if (va >= bat_addrs[b].start && va < bat_addrs[b].limit)
  50. return bat_addrs[b].phys + (va - bat_addrs[b].start);
  51. return 0;
  52. }
  53. /*
  54. * Return VA for a given PA or 0 if not mapped
  55. */
  56. unsigned long p_block_mapped(phys_addr_t pa)
  57. {
  58. int b;
  59. for (b = 0; b < 4; ++b)
  60. if (pa >= bat_addrs[b].phys
  61. && pa < (bat_addrs[b].limit-bat_addrs[b].start)
  62. +bat_addrs[b].phys)
  63. return bat_addrs[b].start+(pa-bat_addrs[b].phys);
  64. return 0;
  65. }
  66. unsigned long __init mmu_mapin_ram(unsigned long top)
  67. {
  68. unsigned long tot, bl, done;
  69. unsigned long max_size = (256<<20);
  70. if (__map_without_bats) {
  71. printk(KERN_DEBUG "RAM mapped without BATs\n");
  72. return 0;
  73. }
  74. /* Set up BAT2 and if necessary BAT3 to cover RAM. */
  75. /* Make sure we don't map a block larger than the
  76. smallest alignment of the physical address. */
  77. tot = top;
  78. for (bl = 128<<10; bl < max_size; bl <<= 1) {
  79. if (bl * 2 > tot)
  80. break;
  81. }
  82. setbat(2, PAGE_OFFSET, 0, bl, PAGE_KERNEL_X);
  83. done = (unsigned long)bat_addrs[2].limit - PAGE_OFFSET + 1;
  84. if ((done < tot) && !bat_addrs[3].limit) {
  85. /* use BAT3 to cover a bit more */
  86. tot -= done;
  87. for (bl = 128<<10; bl < max_size; bl <<= 1)
  88. if (bl * 2 > tot)
  89. break;
  90. setbat(3, PAGE_OFFSET+done, done, bl, PAGE_KERNEL_X);
  91. done = (unsigned long)bat_addrs[3].limit - PAGE_OFFSET + 1;
  92. }
  93. return done;
  94. }
  95. /*
  96. * Set up one of the I/D BAT (block address translation) register pairs.
  97. * The parameters are not checked; in particular size must be a power
  98. * of 2 between 128k and 256M.
  99. */
  100. void __init setbat(int index, unsigned long virt, phys_addr_t phys,
  101. unsigned int size, pgprot_t prot)
  102. {
  103. unsigned int bl;
  104. int wimgxpp;
  105. struct ppc_bat *bat = BATS[index];
  106. unsigned long flags = pgprot_val(prot);
  107. if ((flags & _PAGE_NO_CACHE) ||
  108. (cpu_has_feature(CPU_FTR_NEED_COHERENT) == 0))
  109. flags &= ~_PAGE_COHERENT;
  110. bl = (size >> 17) - 1;
  111. if (PVR_VER(mfspr(SPRN_PVR)) != 1) {
  112. /* 603, 604, etc. */
  113. /* Do DBAT first */
  114. wimgxpp = flags & (_PAGE_WRITETHRU | _PAGE_NO_CACHE
  115. | _PAGE_COHERENT | _PAGE_GUARDED);
  116. wimgxpp |= (flags & _PAGE_RW)? BPP_RW: BPP_RX;
  117. bat[1].batu = virt | (bl << 2) | 2; /* Vs=1, Vp=0 */
  118. bat[1].batl = BAT_PHYS_ADDR(phys) | wimgxpp;
  119. if (flags & _PAGE_USER)
  120. bat[1].batu |= 1; /* Vp = 1 */
  121. if (flags & _PAGE_GUARDED) {
  122. /* G bit must be zero in IBATs */
  123. bat[0].batu = bat[0].batl = 0;
  124. } else {
  125. /* make IBAT same as DBAT */
  126. bat[0] = bat[1];
  127. }
  128. } else {
  129. /* 601 cpu */
  130. if (bl > BL_8M)
  131. bl = BL_8M;
  132. wimgxpp = flags & (_PAGE_WRITETHRU | _PAGE_NO_CACHE
  133. | _PAGE_COHERENT);
  134. wimgxpp |= (flags & _PAGE_RW)?
  135. ((flags & _PAGE_USER)? PP_RWRW: PP_RWXX): PP_RXRX;
  136. bat->batu = virt | wimgxpp | 4; /* Ks=0, Ku=1 */
  137. bat->batl = phys | bl | 0x40; /* V=1 */
  138. }
  139. bat_addrs[index].start = virt;
  140. bat_addrs[index].limit = virt + ((bl + 1) << 17) - 1;
  141. bat_addrs[index].phys = phys;
  142. }
  143. /*
  144. * Preload a translation in the hash table
  145. */
  146. void hash_preload(struct mm_struct *mm, unsigned long ea,
  147. unsigned long access, unsigned long trap)
  148. {
  149. pmd_t *pmd;
  150. if (Hash == 0)
  151. return;
  152. pmd = pmd_offset(pud_offset(pgd_offset(mm, ea), ea), ea);
  153. if (!pmd_none(*pmd))
  154. add_hash_page(mm->context.id, ea, pmd_val(*pmd));
  155. }
  156. /*
  157. * Initialize the hash table and patch the instructions in hashtable.S.
  158. */
  159. void __init MMU_init_hw(void)
  160. {
  161. unsigned int hmask, mb, mb2;
  162. unsigned int n_hpteg, lg_n_hpteg;
  163. extern unsigned int hash_page_patch_A[];
  164. extern unsigned int hash_page_patch_B[], hash_page_patch_C[];
  165. extern unsigned int hash_page[];
  166. extern unsigned int flush_hash_patch_A[], flush_hash_patch_B[];
  167. if (!mmu_has_feature(MMU_FTR_HPTE_TABLE)) {
  168. /*
  169. * Put a blr (procedure return) instruction at the
  170. * start of hash_page, since we can still get DSI
  171. * exceptions on a 603.
  172. */
  173. hash_page[0] = 0x4e800020;
  174. flush_icache_range((unsigned long) &hash_page[0],
  175. (unsigned long) &hash_page[1]);
  176. return;
  177. }
  178. if ( ppc_md.progress ) ppc_md.progress("hash:enter", 0x105);
  179. #define LG_HPTEG_SIZE 6 /* 64 bytes per HPTEG */
  180. #define SDR1_LOW_BITS ((n_hpteg - 1) >> 10)
  181. #define MIN_N_HPTEG 1024 /* min 64kB hash table */
  182. /*
  183. * Allow 1 HPTE (1/8 HPTEG) for each page of memory.
  184. * This is less than the recommended amount, but then
  185. * Linux ain't AIX.
  186. */
  187. n_hpteg = total_memory / (PAGE_SIZE * 8);
  188. if (n_hpteg < MIN_N_HPTEG)
  189. n_hpteg = MIN_N_HPTEG;
  190. lg_n_hpteg = __ilog2(n_hpteg);
  191. if (n_hpteg & (n_hpteg - 1)) {
  192. ++lg_n_hpteg; /* round up if not power of 2 */
  193. n_hpteg = 1 << lg_n_hpteg;
  194. }
  195. Hash_size = n_hpteg << LG_HPTEG_SIZE;
  196. /*
  197. * Find some memory for the hash table.
  198. */
  199. if ( ppc_md.progress ) ppc_md.progress("hash:find piece", 0x322);
  200. Hash = __va(memblock_alloc(Hash_size, Hash_size));
  201. memset(Hash, 0, Hash_size);
  202. _SDR1 = __pa(Hash) | SDR1_LOW_BITS;
  203. Hash_end = (struct hash_pte *) ((unsigned long)Hash + Hash_size);
  204. printk("Total memory = %lldMB; using %ldkB for hash table (at %p)\n",
  205. (unsigned long long)(total_memory >> 20), Hash_size >> 10, Hash);
  206. /*
  207. * Patch up the instructions in hashtable.S:create_hpte
  208. */
  209. if ( ppc_md.progress ) ppc_md.progress("hash:patch", 0x345);
  210. Hash_mask = n_hpteg - 1;
  211. hmask = Hash_mask >> (16 - LG_HPTEG_SIZE);
  212. mb2 = mb = 32 - LG_HPTEG_SIZE - lg_n_hpteg;
  213. if (lg_n_hpteg > 16)
  214. mb2 = 16 - LG_HPTEG_SIZE;
  215. hash_page_patch_A[0] = (hash_page_patch_A[0] & ~0xffff)
  216. | ((unsigned int)(Hash) >> 16);
  217. hash_page_patch_A[1] = (hash_page_patch_A[1] & ~0x7c0) | (mb << 6);
  218. hash_page_patch_A[2] = (hash_page_patch_A[2] & ~0x7c0) | (mb2 << 6);
  219. hash_page_patch_B[0] = (hash_page_patch_B[0] & ~0xffff) | hmask;
  220. hash_page_patch_C[0] = (hash_page_patch_C[0] & ~0xffff) | hmask;
  221. /*
  222. * Ensure that the locations we've patched have been written
  223. * out from the data cache and invalidated in the instruction
  224. * cache, on those machines with split caches.
  225. */
  226. flush_icache_range((unsigned long) &hash_page_patch_A[0],
  227. (unsigned long) &hash_page_patch_C[1]);
  228. /*
  229. * Patch up the instructions in hashtable.S:flush_hash_page
  230. */
  231. flush_hash_patch_A[0] = (flush_hash_patch_A[0] & ~0xffff)
  232. | ((unsigned int)(Hash) >> 16);
  233. flush_hash_patch_A[1] = (flush_hash_patch_A[1] & ~0x7c0) | (mb << 6);
  234. flush_hash_patch_A[2] = (flush_hash_patch_A[2] & ~0x7c0) | (mb2 << 6);
  235. flush_hash_patch_B[0] = (flush_hash_patch_B[0] & ~0xffff) | hmask;
  236. flush_icache_range((unsigned long) &flush_hash_patch_A[0],
  237. (unsigned long) &flush_hash_patch_B[1]);
  238. if ( ppc_md.progress ) ppc_md.progress("hash:done", 0x205);
  239. }
  240. void setup_initial_memory_limit(phys_addr_t first_memblock_base,
  241. phys_addr_t first_memblock_size)
  242. {
  243. /* We don't currently support the first MEMBLOCK not mapping 0
  244. * physical on those processors
  245. */
  246. BUG_ON(first_memblock_base != 0);
  247. /* 601 can only access 16MB at the moment */
  248. if (PVR_VER(mfspr(SPRN_PVR)) == 1)
  249. memblock_set_current_limit(min_t(u64, first_memblock_size, 0x01000000));
  250. else /* Anything else has 256M mapped */
  251. memblock_set_current_limit(min_t(u64, first_memblock_size, 0x10000000));
  252. }