hash_utils_64.c 11 KB

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