2 * PowerPC64 port by Mike Corrigan and Dave Engebretsen
3 * {mikejc|engebret}@us.ibm.com
5 * Copyright (c) 2000 Mike Corrigan <mikejc@us.ibm.com>
7 * SMP scalability work:
8 * Copyright (C) 2001 Anton Blanchard <anton@au.ibm.com>, IBM
13 * PowerPC Hashed Page Table functions
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.
24 #include <linux/config.h>
25 #include <linux/spinlock.h>
26 #include <linux/errno.h>
27 #include <linux/sched.h>
28 #include <linux/proc_fs.h>
29 #include <linux/stat.h>
30 #include <linux/sysctl.h>
31 #include <linux/ctype.h>
32 #include <linux/cache.h>
33 #include <linux/init.h>
34 #include <linux/signal.h>
36 #include <asm/ppcdebug.h>
37 #include <asm/processor.h>
38 #include <asm/pgtable.h>
40 #include <asm/mmu_context.h>
42 #include <asm/types.h>
43 #include <asm/system.h>
44 #include <asm/uaccess.h>
45 #include <asm/machdep.h>
47 #include <asm/abs_addr.h>
48 #include <asm/tlbflush.h>
52 #include <asm/cacheflush.h>
53 #include <asm/cputable.h>
54 #include <asm/abs_addr.h>
55 #include <asm/sections.h>
58 #define DBG(fmt...) udbg_printf(fmt)
64 #define DBG_LOW(fmt...) udbg_printf(fmt)
66 #define DBG_LOW(fmt...)
73 * Note: pte --> Linux PTE
74 * HPTE --> PowerPC Hashed Page Table Entry
77 * htab_initialize is called with the MMU off (of course), but
78 * the kernel has been copied down to zero so it can directly
79 * reference global data. At this point it is very difficult
80 * to print debug info.
85 extern unsigned long dart_tablebase;
86 #endif /* CONFIG_U3_DART */
89 unsigned long htab_hash_mask;
91 struct mmu_psize_def mmu_psize_defs[MMU_PAGE_COUNT];
92 int mmu_linear_psize = MMU_PAGE_4K;
93 int mmu_virtual_psize = MMU_PAGE_4K;
94 #ifdef CONFIG_HUGETLB_PAGE
95 int mmu_huge_psize = MMU_PAGE_16M;
96 unsigned int HPAGE_SHIFT;
99 /* There are definitions of page sizes arrays to be used when none
100 * is provided by the firmware.
103 /* Pre-POWER4 CPUs (4k pages only)
105 struct mmu_psize_def mmu_psize_defaults_old[] = {
115 /* POWER4, GPUL, POWER5
117 * Support for 16Mb large pages
119 struct mmu_psize_def mmu_psize_defaults_gp[] = {
137 int htab_bolt_mapping(unsigned long vstart, unsigned long vend,
138 unsigned long pstart, unsigned long mode, int psize)
140 unsigned long vaddr, paddr;
141 unsigned int step, shift;
142 unsigned long tmp_mode;
145 shift = mmu_psize_defs[psize].shift;
148 for (vaddr = vstart, paddr = pstart; vaddr < vend;
149 vaddr += step, paddr += step) {
150 unsigned long vpn, hash, hpteg;
151 unsigned long vsid = get_kernel_vsid(vaddr);
152 unsigned long va = (vsid << 28) | (vaddr & 0x0fffffff);
157 /* Make non-kernel text non-executable */
158 if (!in_kernel_text(vaddr))
159 tmp_mode = mode | HPTE_R_N;
161 hash = hpt_hash(va, shift);
162 hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
164 /* The crap below can be cleaned once ppd_md.probe() can
165 * set up the hash callbacks, thus we can just used the
166 * normal insert callback here.
168 #ifdef CONFIG_PPC_ISERIES
169 if (systemcfg->platform == PLATFORM_ISERIES_LPAR)
170 ret = iSeries_hpte_insert(hpteg, va,
177 #ifdef CONFIG_PPC_PSERIES
178 if (systemcfg->platform & PLATFORM_LPAR)
179 ret = pSeries_lpar_hpte_insert(hpteg, va,
186 #ifdef CONFIG_PPC_MULTIPLATFORM
187 ret = native_hpte_insert(hpteg, va,
189 tmp_mode, HPTE_V_BOLTED,
195 return ret < 0 ? ret : 0;
198 static int __init htab_dt_scan_page_sizes(unsigned long node,
199 const char *uname, int depth,
202 char *type = of_get_flat_dt_prop(node, "device_type", NULL);
204 unsigned long size = 0;
206 /* We are scanning "cpu" nodes only */
207 if (type == NULL || strcmp(type, "cpu") != 0)
210 prop = (u32 *)of_get_flat_dt_prop(node,
211 "ibm,segment-page-sizes", &size);
213 DBG("Page sizes from device-tree:\n");
215 cur_cpu_spec->cpu_features &= ~(CPU_FTR_16M_PAGE);
217 unsigned int shift = prop[0];
218 unsigned int slbenc = prop[1];
219 unsigned int lpnum = prop[2];
220 unsigned int lpenc = 0;
221 struct mmu_psize_def *def;
224 size -= 3; prop += 3;
225 while(size > 0 && lpnum) {
226 if (prop[0] == shift)
228 prop += 2; size -= 2;
243 cur_cpu_spec->cpu_features |= CPU_FTR_16M_PAGE;
251 def = &mmu_psize_defs[idx];
256 def->avpnm = (1 << (shift - 23)) - 1;
259 /* We don't know for sure what's up with tlbiel, so
260 * for now we only set it for 4K and 64K pages
262 if (idx == MMU_PAGE_4K || idx == MMU_PAGE_64K)
267 DBG(" %d: shift=%02x, sllp=%04x, avpnm=%08x, "
268 "tlbiel=%d, penc=%d\n",
269 idx, shift, def->sllp, def->avpnm, def->tlbiel,
278 static void __init htab_init_page_sizes(void)
282 /* Default to 4K pages only */
283 memcpy(mmu_psize_defs, mmu_psize_defaults_old,
284 sizeof(mmu_psize_defaults_old));
287 * Try to find the available page sizes in the device-tree
289 rc = of_scan_flat_dt(htab_dt_scan_page_sizes, NULL);
290 if (rc != 0) /* Found */
294 * Not in the device-tree, let's fallback on known size
295 * list for 16M capable GP & GR
297 if ((systemcfg->platform != PLATFORM_ISERIES_LPAR) &&
298 cpu_has_feature(CPU_FTR_16M_PAGE))
299 memcpy(mmu_psize_defs, mmu_psize_defaults_gp,
300 sizeof(mmu_psize_defaults_gp));
303 * Pick a size for the linear mapping. Currently, we only support
304 * 16M, 1M and 4K which is the default
306 if (mmu_psize_defs[MMU_PAGE_16M].shift)
307 mmu_linear_psize = MMU_PAGE_16M;
308 else if (mmu_psize_defs[MMU_PAGE_1M].shift)
309 mmu_linear_psize = MMU_PAGE_1M;
312 * Pick a size for the ordinary pages. Default is 4K, we support
313 * 64K if cache inhibited large pages are supported by the
316 #ifdef CONFIG_PPC_64K_PAGES
317 if (mmu_psize_defs[MMU_PAGE_64K].shift &&
318 cpu_has_feature(CPU_FTR_CI_LARGE_PAGE))
319 mmu_virtual_psize = MMU_PAGE_64K;
322 printk(KERN_INFO "Page orders: linear mapping = %d, others = %d\n",
323 mmu_psize_defs[mmu_linear_psize].shift,
324 mmu_psize_defs[mmu_virtual_psize].shift);
326 #ifdef CONFIG_HUGETLB_PAGE
327 /* Init large page size. Currently, we pick 16M or 1M depending
328 * on what is available
330 if (mmu_psize_defs[MMU_PAGE_16M].shift)
331 mmu_huge_psize = MMU_PAGE_16M;
332 /* With 4k/4level pagetables, we can't (for now) cope with a
333 * huge page size < PMD_SIZE */
334 else if (mmu_psize_defs[MMU_PAGE_1M].shift)
335 mmu_huge_psize = MMU_PAGE_1M;
337 /* Calculate HPAGE_SHIFT and sanity check it */
338 if (mmu_psize_defs[mmu_huge_psize].shift > MIN_HUGEPTE_SHIFT &&
339 mmu_psize_defs[mmu_huge_psize].shift < SID_SHIFT)
340 HPAGE_SHIFT = mmu_psize_defs[mmu_huge_psize].shift;
342 HPAGE_SHIFT = 0; /* No huge pages dude ! */
343 #endif /* CONFIG_HUGETLB_PAGE */
346 static int __init htab_dt_scan_pftsize(unsigned long node,
347 const char *uname, int depth,
350 char *type = of_get_flat_dt_prop(node, "device_type", NULL);
353 /* We are scanning "cpu" nodes only */
354 if (type == NULL || strcmp(type, "cpu") != 0)
357 prop = (u32 *)of_get_flat_dt_prop(node, "ibm,pft-size", NULL);
359 /* pft_size[0] is the NUMA CEC cookie */
360 ppc64_pft_size = prop[1];
366 static unsigned long __init htab_get_table_size(void)
368 unsigned long rnd_mem_size, pteg_count;
370 /* If hash size isn't already provided by the platform, we try to
371 * retreive it from the device-tree. If it's not there neither, we
372 * calculate it now based on the total RAM size
374 if (ppc64_pft_size == 0)
375 of_scan_flat_dt(htab_dt_scan_pftsize, NULL);
377 return 1UL << ppc64_pft_size;
379 /* round mem_size up to next power of 2 */
380 rnd_mem_size = 1UL << __ilog2(systemcfg->physicalMemorySize);
381 if (rnd_mem_size < systemcfg->physicalMemorySize)
385 pteg_count = max(rnd_mem_size >> (12 + 1), 1UL << 11);
387 return pteg_count << 7;
390 void __init htab_initialize(void)
392 unsigned long table, htab_size_bytes;
393 unsigned long pteg_count;
394 unsigned long mode_rw;
395 unsigned long base = 0, size = 0;
398 extern unsigned long tce_alloc_start, tce_alloc_end;
400 DBG(" -> htab_initialize()\n");
402 /* Initialize page sizes */
403 htab_init_page_sizes();
406 * Calculate the required size of the htab. We want the number of
407 * PTEGs to equal one half the number of real pages.
409 htab_size_bytes = htab_get_table_size();
410 pteg_count = htab_size_bytes >> 7;
412 /* For debug, make the HTAB 1/8 as big as it normally would be. */
413 ifppcdebug(PPCDBG_HTABSIZE) {
415 htab_size_bytes = pteg_count << 7;
418 htab_hash_mask = pteg_count - 1;
420 if (systemcfg->platform & PLATFORM_LPAR) {
421 /* Using a hypervisor which owns the htab */
425 /* Find storage for the HPT. Must be contiguous in
426 * the absolute address space.
428 table = lmb_alloc(htab_size_bytes, htab_size_bytes);
431 DBG("Hash table allocated at %lx, size: %lx\n", table,
434 htab_address = abs_to_virt(table);
436 /* htab absolute addr + encoded htabsize */
437 _SDR1 = table + __ilog2(pteg_count) - 11;
439 /* Initialize the HPT with no entries */
440 memset((void *)table, 0, htab_size_bytes);
443 mode_rw = _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_COHERENT | PP_RWXX;
445 /* On U3 based machines, we need to reserve the DART area and
446 * _NOT_ map it to avoid cache paradoxes as it's remapped non
450 /* create bolted the linear mapping in the hash table */
451 for (i=0; i < lmb.memory.cnt; i++) {
452 base = lmb.memory.region[i].base + KERNELBASE;
453 size = lmb.memory.region[i].size;
455 DBG("creating mapping for region: %lx : %lx\n", base, size);
457 #ifdef CONFIG_U3_DART
458 /* Do not map the DART space. Fortunately, it will be aligned
459 * in such a way that it will not cross two lmb regions and
460 * will fit within a single 16Mb page.
461 * The DART space is assumed to be a full 16Mb region even if
462 * we only use 2Mb of that space. We will use more of it later
463 * for AGP GART. We have to use a full 16Mb large page.
465 DBG("DART base: %lx\n", dart_tablebase);
467 if (dart_tablebase != 0 && dart_tablebase >= base
468 && dart_tablebase < (base + size)) {
469 if (base != dart_tablebase)
470 BUG_ON(htab_bolt_mapping(base, dart_tablebase,
473 if ((base + size) > (dart_tablebase + 16*MB))
474 BUG_ON(htab_bolt_mapping(dart_tablebase+16*MB,
476 dart_tablebase+16*MB,
481 #endif /* CONFIG_U3_DART */
482 BUG_ON(htab_bolt_mapping(base, base + size, base,
483 mode_rw, mmu_linear_psize));
487 * If we have a memory_limit and we've allocated TCEs then we need to
488 * explicitly map the TCE area at the top of RAM. We also cope with the
489 * case that the TCEs start below memory_limit.
490 * tce_alloc_start/end are 16MB aligned so the mapping should work
491 * for either 4K or 16MB pages.
493 if (tce_alloc_start) {
494 tce_alloc_start += KERNELBASE;
495 tce_alloc_end += KERNELBASE;
497 if (base + size >= tce_alloc_start)
498 tce_alloc_start = base + size + 1;
500 BUG_ON(htab_bolt_mapping(tce_alloc_start, tce_alloc_end,
501 tce_alloc_start, mode_rw,
505 DBG(" <- htab_initialize()\n");
511 * Called by asm hashtable.S for doing lazy icache flush
513 unsigned int hash_page_do_lazy_icache(unsigned int pp, pte_t pte, int trap)
517 page = pte_page(pte);
520 if (!test_bit(PG_arch_1, &page->flags) && !PageReserved(page)) {
522 __flush_dcache_icache(page_address(page));
523 set_bit(PG_arch_1, &page->flags);
532 * 1 - normal page fault
533 * -1 - critical hash insertion error
535 int hash_page(unsigned long ea, unsigned long access, unsigned long trap)
539 struct mm_struct *mm;
542 int rc, user_region = 0, local = 0;
544 DBG_LOW("hash_page(ea=%016lx, access=%lx, trap=%lx\n",
547 if ((ea & ~REGION_MASK) >= PGTABLE_RANGE) {
548 DBG_LOW(" out of pgtable range !\n");
552 /* Get region & vsid */
553 switch (REGION_ID(ea)) {
558 DBG_LOW(" user region with no mm !\n");
561 vsid = get_vsid(mm->context.id, ea);
563 case VMALLOC_REGION_ID:
565 vsid = get_kernel_vsid(ea);
569 * Send the problem up to do_page_fault
573 DBG_LOW(" mm=%p, mm->pgdir=%p, vsid=%016lx\n", mm, mm->pgd, vsid);
580 /* Check CPU locality */
581 tmp = cpumask_of_cpu(smp_processor_id());
582 if (user_region && cpus_equal(mm->cpu_vm_mask, tmp))
585 /* Handle hugepage regions */
586 if (unlikely(in_hugepage_area(mm->context, ea))) {
587 DBG_LOW(" -> huge page !\n");
588 return hash_huge_page(mm, access, ea, vsid, local);
591 /* Get PTE and page size from page tables */
592 ptep = find_linux_pte(pgdir, ea);
593 if (ptep == NULL || !pte_present(*ptep)) {
594 DBG_LOW(" no PTE !\n");
598 #ifndef CONFIG_PPC_64K_PAGES
599 DBG_LOW(" i-pte: %016lx\n", pte_val(*ptep));
601 DBG_LOW(" i-pte: %016lx %016lx\n", pte_val(*ptep),
602 pte_val(*(ptep + PTRS_PER_PTE)));
604 /* Pre-check access permissions (will be re-checked atomically
605 * in __hash_page_XX but this pre-check is a fast path
607 if (access & ~pte_val(*ptep)) {
608 DBG_LOW(" no access !\n");
612 /* Do actual hashing */
613 #ifndef CONFIG_PPC_64K_PAGES
614 rc = __hash_page_4K(ea, access, vsid, ptep, trap, local);
616 if (mmu_virtual_psize == MMU_PAGE_64K)
617 rc = __hash_page_64K(ea, access, vsid, ptep, trap, local);
619 rc = __hash_page_4K(ea, access, vsid, ptep, trap, local);
620 #endif /* CONFIG_PPC_64K_PAGES */
622 #ifndef CONFIG_PPC_64K_PAGES
623 DBG_LOW(" o-pte: %016lx\n", pte_val(*ptep));
625 DBG_LOW(" o-pte: %016lx %016lx\n", pte_val(*ptep),
626 pte_val(*(ptep + PTRS_PER_PTE)));
628 DBG_LOW(" -> rc=%d\n", rc);
632 void hash_preload(struct mm_struct *mm, unsigned long ea,
633 unsigned long access, unsigned long trap)
642 /* We don't want huge pages prefaulted for now
644 if (unlikely(in_hugepage_area(mm->context, ea)))
647 DBG_LOW("hash_preload(mm=%p, mm->pgdir=%p, ea=%016lx, access=%lx,"
648 " trap=%lx\n", mm, mm->pgd, ea, access, trap);
650 /* Get PTE, VSID, access mask */
654 ptep = find_linux_pte(pgdir, ea);
657 vsid = get_vsid(mm->context.id, ea);
660 local_irq_save(flags);
661 mask = cpumask_of_cpu(smp_processor_id());
662 if (cpus_equal(mm->cpu_vm_mask, mask))
664 #ifndef CONFIG_PPC_64K_PAGES
665 __hash_page_4K(ea, access, vsid, ptep, trap, local);
667 if (mmu_virtual_psize == MMU_PAGE_64K)
668 __hash_page_64K(ea, access, vsid, ptep, trap, local);
670 __hash_page_4K(ea, access, vsid, ptep, trap, local);
671 #endif /* CONFIG_PPC_64K_PAGES */
672 local_irq_restore(flags);
675 void flush_hash_page(unsigned long va, real_pte_t pte, int psize, int local)
677 unsigned long hash, index, shift, hidx, slot;
679 DBG_LOW("flush_hash_page(va=%016x)\n", va);
680 pte_iterate_hashed_subpages(pte, psize, va, index, shift) {
681 hash = hpt_hash(va, shift);
682 hidx = __rpte_to_hidx(pte, index);
683 if (hidx & _PTEIDX_SECONDARY)
685 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
686 slot += hidx & _PTEIDX_GROUP_IX;
687 DBG_LOW(" sub %d: hash=%x, hidx=%x\n", index, slot, hidx);
688 ppc_md.hpte_invalidate(slot, va, psize, local);
689 } pte_iterate_hashed_end();
692 void flush_hash_range(unsigned long number, int local)
694 if (ppc_md.flush_hash_range)
695 ppc_md.flush_hash_range(number, local);
698 struct ppc64_tlb_batch *batch =
699 &__get_cpu_var(ppc64_tlb_batch);
701 for (i = 0; i < number; i++)
702 flush_hash_page(batch->vaddr[i], batch->pte[i],
703 batch->psize, local);
707 static inline void make_bl(unsigned int *insn_addr, void *func)
709 unsigned long funcp = *((unsigned long *)func);
710 int offset = funcp - (unsigned long)insn_addr;
712 *insn_addr = (unsigned int)(0x48000001 | (offset & 0x03fffffc));
713 flush_icache_range((unsigned long)insn_addr, 4+
714 (unsigned long)insn_addr);
718 * low_hash_fault is called when we the low level hash code failed
719 * to instert a PTE due to an hypervisor error
721 void low_hash_fault(struct pt_regs *regs, unsigned long address)
723 if (user_mode(regs)) {
726 info.si_signo = SIGBUS;
728 info.si_code = BUS_ADRERR;
729 info.si_addr = (void __user *)address;
730 force_sig_info(SIGBUS, &info, current);
733 bad_page_fault(regs, address, SIGBUS);
736 void __init htab_finish_init(void)
738 extern unsigned int *htab_call_hpte_insert1;
739 extern unsigned int *htab_call_hpte_insert2;
740 extern unsigned int *htab_call_hpte_remove;
741 extern unsigned int *htab_call_hpte_updatepp;
743 #ifdef CONFIG_PPC_64K_PAGES
744 extern unsigned int *ht64_call_hpte_insert1;
745 extern unsigned int *ht64_call_hpte_insert2;
746 extern unsigned int *ht64_call_hpte_remove;
747 extern unsigned int *ht64_call_hpte_updatepp;
749 make_bl(ht64_call_hpte_insert1, ppc_md.hpte_insert);
750 make_bl(ht64_call_hpte_insert2, ppc_md.hpte_insert);
751 make_bl(ht64_call_hpte_remove, ppc_md.hpte_remove);
752 make_bl(ht64_call_hpte_updatepp, ppc_md.hpte_updatepp);
753 #endif /* CONFIG_PPC_64K_PAGES */
755 make_bl(htab_call_hpte_insert1, ppc_md.hpte_insert);
756 make_bl(htab_call_hpte_insert2, ppc_md.hpte_insert);
757 make_bl(htab_call_hpte_remove, ppc_md.hpte_remove);
758 make_bl(htab_call_hpte_updatepp, ppc_md.hpte_updatepp);