hash_utils.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /*
  2. * PowerPC64 port by Mike Corrigan and Dave Engebretsen
  3. * {mikejc|engebret}@us.ibm.com
  4. *
  5. * Copyright (c) 2000 Mike Corrigan <mikejc@us.ibm.com>
  6. *
  7. * SMP scalability work:
  8. * Copyright (C) 2001 Anton Blanchard <anton@au.ibm.com>, IBM
  9. *
  10. * Module name: htab.c
  11. *
  12. * Description:
  13. * PowerPC Hashed Page Table functions
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License
  17. * as published by the Free Software Foundation; either version
  18. * 2 of the License, or (at your option) any later version.
  19. */
  20. #undef DEBUG
  21. #include <linux/config.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/errno.h>
  24. #include <linux/sched.h>
  25. #include <linux/proc_fs.h>
  26. #include <linux/stat.h>
  27. #include <linux/sysctl.h>
  28. #include <linux/ctype.h>
  29. #include <linux/cache.h>
  30. #include <linux/init.h>
  31. #include <linux/signal.h>
  32. #include <asm/ppcdebug.h>
  33. #include <asm/processor.h>
  34. #include <asm/pgtable.h>
  35. #include <asm/mmu.h>
  36. #include <asm/mmu_context.h>
  37. #include <asm/page.h>
  38. #include <asm/types.h>
  39. #include <asm/system.h>
  40. #include <asm/uaccess.h>
  41. #include <asm/machdep.h>
  42. #include <asm/lmb.h>
  43. #include <asm/abs_addr.h>
  44. #include <asm/tlbflush.h>
  45. #include <asm/io.h>
  46. #include <asm/eeh.h>
  47. #include <asm/tlb.h>
  48. #include <asm/cacheflush.h>
  49. #include <asm/cputable.h>
  50. #include <asm/abs_addr.h>
  51. #include <asm/sections.h>
  52. #ifdef DEBUG
  53. #define DBG(fmt...) udbg_printf(fmt)
  54. #else
  55. #define DBG(fmt...)
  56. #endif
  57. /*
  58. * Note: pte --> Linux PTE
  59. * HPTE --> PowerPC Hashed Page Table Entry
  60. *
  61. * Execution context:
  62. * htab_initialize is called with the MMU off (of course), but
  63. * the kernel has been copied down to zero so it can directly
  64. * reference global data. At this point it is very difficult
  65. * to print debug info.
  66. *
  67. */
  68. #ifdef CONFIG_U3_DART
  69. extern unsigned long dart_tablebase;
  70. #endif /* CONFIG_U3_DART */
  71. hpte_t *htab_address;
  72. unsigned long htab_hash_mask;
  73. extern unsigned long _SDR1;
  74. #define KB (1024)
  75. #define MB (1024*KB)
  76. static inline void loop_forever(void)
  77. {
  78. volatile unsigned long x = 1;
  79. for(;x;x|=1)
  80. ;
  81. }
  82. #ifdef CONFIG_PPC_MULTIPLATFORM
  83. static inline void create_pte_mapping(unsigned long start, unsigned long end,
  84. unsigned long mode, int large)
  85. {
  86. unsigned long addr;
  87. unsigned int step;
  88. unsigned long tmp_mode;
  89. unsigned long vflags;
  90. if (large) {
  91. step = 16*MB;
  92. vflags = HPTE_V_BOLTED | HPTE_V_LARGE;
  93. } else {
  94. step = 4*KB;
  95. vflags = HPTE_V_BOLTED;
  96. }
  97. for (addr = start; addr < end; addr += step) {
  98. unsigned long vpn, hash, hpteg;
  99. unsigned long vsid = get_kernel_vsid(addr);
  100. unsigned long va = (vsid << 28) | (addr & 0xfffffff);
  101. int ret;
  102. if (large)
  103. vpn = va >> HPAGE_SHIFT;
  104. else
  105. vpn = va >> PAGE_SHIFT;
  106. tmp_mode = mode;
  107. /* Make non-kernel text non-executable */
  108. if (!in_kernel_text(addr))
  109. tmp_mode = mode | HW_NO_EXEC;
  110. hash = hpt_hash(vpn, large);
  111. hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
  112. #ifdef CONFIG_PPC_PSERIES
  113. if (systemcfg->platform & PLATFORM_LPAR)
  114. ret = pSeries_lpar_hpte_insert(hpteg, va,
  115. virt_to_abs(addr) >> PAGE_SHIFT,
  116. vflags, tmp_mode);
  117. else
  118. #endif /* CONFIG_PPC_PSERIES */
  119. ret = native_hpte_insert(hpteg, va,
  120. virt_to_abs(addr) >> PAGE_SHIFT,
  121. vflags, tmp_mode);
  122. if (ret == -1) {
  123. ppc64_terminate_msg(0x20, "create_pte_mapping");
  124. loop_forever();
  125. }
  126. }
  127. }
  128. void __init htab_initialize(void)
  129. {
  130. unsigned long table, htab_size_bytes;
  131. unsigned long pteg_count;
  132. unsigned long mode_rw;
  133. int i, use_largepages = 0;
  134. unsigned long base = 0, size = 0;
  135. extern unsigned long tce_alloc_start, tce_alloc_end;
  136. DBG(" -> htab_initialize()\n");
  137. /*
  138. * Calculate the required size of the htab. We want the number of
  139. * PTEGs to equal one half the number of real pages.
  140. */
  141. htab_size_bytes = 1UL << ppc64_pft_size;
  142. pteg_count = htab_size_bytes >> 7;
  143. /* For debug, make the HTAB 1/8 as big as it normally would be. */
  144. ifppcdebug(PPCDBG_HTABSIZE) {
  145. pteg_count >>= 3;
  146. htab_size_bytes = pteg_count << 7;
  147. }
  148. htab_hash_mask = pteg_count - 1;
  149. if (systemcfg->platform & PLATFORM_LPAR) {
  150. /* Using a hypervisor which owns the htab */
  151. htab_address = NULL;
  152. _SDR1 = 0;
  153. } else {
  154. /* Find storage for the HPT. Must be contiguous in
  155. * the absolute address space.
  156. */
  157. table = lmb_alloc(htab_size_bytes, htab_size_bytes);
  158. DBG("Hash table allocated at %lx, size: %lx\n", table,
  159. htab_size_bytes);
  160. if ( !table ) {
  161. ppc64_terminate_msg(0x20, "hpt space");
  162. loop_forever();
  163. }
  164. htab_address = abs_to_virt(table);
  165. /* htab absolute addr + encoded htabsize */
  166. _SDR1 = table + __ilog2(pteg_count) - 11;
  167. /* Initialize the HPT with no entries */
  168. memset((void *)table, 0, htab_size_bytes);
  169. }
  170. mode_rw = _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_COHERENT | PP_RWXX;
  171. /* On U3 based machines, we need to reserve the DART area and
  172. * _NOT_ map it to avoid cache paradoxes as it's remapped non
  173. * cacheable later on
  174. */
  175. if (cpu_has_feature(CPU_FTR_16M_PAGE))
  176. use_largepages = 1;
  177. /* create bolted the linear mapping in the hash table */
  178. for (i=0; i < lmb.memory.cnt; i++) {
  179. base = lmb.memory.region[i].base + KERNELBASE;
  180. size = lmb.memory.region[i].size;
  181. DBG("creating mapping for region: %lx : %lx\n", base, size);
  182. #ifdef CONFIG_U3_DART
  183. /* Do not map the DART space. Fortunately, it will be aligned
  184. * in such a way that it will not cross two lmb regions and will
  185. * fit within a single 16Mb page.
  186. * The DART space is assumed to be a full 16Mb region even if we
  187. * only use 2Mb of that space. We will use more of it later for
  188. * AGP GART. We have to use a full 16Mb large page.
  189. */
  190. DBG("DART base: %lx\n", dart_tablebase);
  191. if (dart_tablebase != 0 && dart_tablebase >= base
  192. && dart_tablebase < (base + size)) {
  193. if (base != dart_tablebase)
  194. create_pte_mapping(base, dart_tablebase, mode_rw,
  195. use_largepages);
  196. if ((base + size) > (dart_tablebase + 16*MB))
  197. create_pte_mapping(dart_tablebase + 16*MB, base + size,
  198. mode_rw, use_largepages);
  199. continue;
  200. }
  201. #endif /* CONFIG_U3_DART */
  202. create_pte_mapping(base, base + size, mode_rw, use_largepages);
  203. }
  204. /*
  205. * If we have a memory_limit and we've allocated TCEs then we need to
  206. * explicitly map the TCE area at the top of RAM. We also cope with the
  207. * case that the TCEs start below memory_limit.
  208. * tce_alloc_start/end are 16MB aligned so the mapping should work
  209. * for either 4K or 16MB pages.
  210. */
  211. if (tce_alloc_start) {
  212. tce_alloc_start += KERNELBASE;
  213. tce_alloc_end += KERNELBASE;
  214. if (base + size >= tce_alloc_start)
  215. tce_alloc_start = base + size + 1;
  216. create_pte_mapping(tce_alloc_start, tce_alloc_end,
  217. mode_rw, use_largepages);
  218. }
  219. DBG(" <- htab_initialize()\n");
  220. }
  221. #undef KB
  222. #undef MB
  223. #endif /* CONFIG_PPC_MULTIPLATFORM */
  224. /*
  225. * Called by asm hashtable.S for doing lazy icache flush
  226. */
  227. unsigned int hash_page_do_lazy_icache(unsigned int pp, pte_t pte, int trap)
  228. {
  229. struct page *page;
  230. if (!pfn_valid(pte_pfn(pte)))
  231. return pp;
  232. page = pte_page(pte);
  233. /* page is dirty */
  234. if (!test_bit(PG_arch_1, &page->flags) && !PageReserved(page)) {
  235. if (trap == 0x400) {
  236. __flush_dcache_icache(page_address(page));
  237. set_bit(PG_arch_1, &page->flags);
  238. } else
  239. pp |= HW_NO_EXEC;
  240. }
  241. return pp;
  242. }
  243. /* Result code is:
  244. * 0 - handled
  245. * 1 - normal page fault
  246. * -1 - critical hash insertion error
  247. */
  248. int hash_page(unsigned long ea, unsigned long access, unsigned long trap)
  249. {
  250. void *pgdir;
  251. unsigned long vsid;
  252. struct mm_struct *mm;
  253. pte_t *ptep;
  254. int ret;
  255. int user_region = 0;
  256. int local = 0;
  257. cpumask_t tmp;
  258. if ((ea & ~REGION_MASK) >= PGTABLE_RANGE)
  259. return 1;
  260. switch (REGION_ID(ea)) {
  261. case USER_REGION_ID:
  262. user_region = 1;
  263. mm = current->mm;
  264. if (! mm)
  265. return 1;
  266. vsid = get_vsid(mm->context.id, ea);
  267. break;
  268. case VMALLOC_REGION_ID:
  269. mm = &init_mm;
  270. vsid = get_kernel_vsid(ea);
  271. break;
  272. #if 0
  273. case KERNEL_REGION_ID:
  274. /*
  275. * Should never get here - entire 0xC0... region is bolted.
  276. * Send the problem up to do_page_fault
  277. */
  278. #endif
  279. default:
  280. /* Not a valid range
  281. * Send the problem up to do_page_fault
  282. */
  283. return 1;
  284. break;
  285. }
  286. pgdir = mm->pgd;
  287. if (pgdir == NULL)
  288. return 1;
  289. tmp = cpumask_of_cpu(smp_processor_id());
  290. if (user_region && cpus_equal(mm->cpu_vm_mask, tmp))
  291. local = 1;
  292. /* Is this a huge page ? */
  293. if (unlikely(in_hugepage_area(mm->context, ea)))
  294. ret = hash_huge_page(mm, access, ea, vsid, local);
  295. else {
  296. ptep = find_linux_pte(pgdir, ea);
  297. if (ptep == NULL)
  298. return 1;
  299. ret = __hash_page(ea, access, vsid, ptep, trap, local);
  300. }
  301. return ret;
  302. }
  303. void flush_hash_page(unsigned long context, unsigned long ea, pte_t pte,
  304. int local)
  305. {
  306. unsigned long vsid, vpn, va, hash, secondary, slot;
  307. unsigned long huge = pte_huge(pte);
  308. if (ea < KERNELBASE)
  309. vsid = get_vsid(context, ea);
  310. else
  311. vsid = get_kernel_vsid(ea);
  312. va = (vsid << 28) | (ea & 0x0fffffff);
  313. if (huge)
  314. vpn = va >> HPAGE_SHIFT;
  315. else
  316. vpn = va >> PAGE_SHIFT;
  317. hash = hpt_hash(vpn, huge);
  318. secondary = (pte_val(pte) & _PAGE_SECONDARY) >> 15;
  319. if (secondary)
  320. hash = ~hash;
  321. slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
  322. slot += (pte_val(pte) & _PAGE_GROUP_IX) >> 12;
  323. ppc_md.hpte_invalidate(slot, va, huge, local);
  324. }
  325. void flush_hash_range(unsigned long context, unsigned long number, int local)
  326. {
  327. if (ppc_md.flush_hash_range) {
  328. ppc_md.flush_hash_range(context, number, local);
  329. } else {
  330. int i;
  331. struct ppc64_tlb_batch *batch = &__get_cpu_var(ppc64_tlb_batch);
  332. for (i = 0; i < number; i++)
  333. flush_hash_page(context, batch->addr[i], batch->pte[i],
  334. local);
  335. }
  336. }
  337. static inline void make_bl(unsigned int *insn_addr, void *func)
  338. {
  339. unsigned long funcp = *((unsigned long *)func);
  340. int offset = funcp - (unsigned long)insn_addr;
  341. *insn_addr = (unsigned int)(0x48000001 | (offset & 0x03fffffc));
  342. flush_icache_range((unsigned long)insn_addr, 4+
  343. (unsigned long)insn_addr);
  344. }
  345. /*
  346. * low_hash_fault is called when we the low level hash code failed
  347. * to instert a PTE due to an hypervisor error
  348. */
  349. void low_hash_fault(struct pt_regs *regs, unsigned long address)
  350. {
  351. if (user_mode(regs)) {
  352. siginfo_t info;
  353. info.si_signo = SIGBUS;
  354. info.si_errno = 0;
  355. info.si_code = BUS_ADRERR;
  356. info.si_addr = (void __user *)address;
  357. force_sig_info(SIGBUS, &info, current);
  358. return;
  359. }
  360. bad_page_fault(regs, address, SIGBUS);
  361. }
  362. void __init htab_finish_init(void)
  363. {
  364. extern unsigned int *htab_call_hpte_insert1;
  365. extern unsigned int *htab_call_hpte_insert2;
  366. extern unsigned int *htab_call_hpte_remove;
  367. extern unsigned int *htab_call_hpte_updatepp;
  368. make_bl(htab_call_hpte_insert1, ppc_md.hpte_insert);
  369. make_bl(htab_call_hpte_insert2, ppc_md.hpte_insert);
  370. make_bl(htab_call_hpte_remove, ppc_md.hpte_remove);
  371. make_bl(htab_call_hpte_updatepp, ppc_md.hpte_updatepp);
  372. }