2013-06-20 17:00:21 +08:00
|
|
|
/*
|
|
|
|
* Copyright IBM Corporation, 2013
|
|
|
|
* Author Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of version 2.1 of the GNU Lesser General Public License
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it would be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* PPC64 THP Support for hash based MMUs
|
|
|
|
*/
|
|
|
|
#include <linux/mm.h>
|
|
|
|
#include <asm/machdep.h>
|
|
|
|
|
|
|
|
int __hash_page_thp(unsigned long ea, unsigned long access, unsigned long vsid,
|
2014-12-04 13:30:14 +08:00
|
|
|
pmd_t *pmdp, unsigned long trap, unsigned long flags,
|
|
|
|
int ssize, unsigned int psize)
|
2013-06-20 17:00:21 +08:00
|
|
|
{
|
|
|
|
unsigned int index, valid;
|
|
|
|
unsigned char *hpte_slot_array;
|
|
|
|
unsigned long rflags, pa, hidx;
|
|
|
|
unsigned long old_pmd, new_pmd;
|
|
|
|
int ret, lpsize = MMU_PAGE_16M;
|
|
|
|
unsigned long vpn, hash, shift, slot;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* atomically mark the linux large page PMD busy and dirty
|
|
|
|
*/
|
|
|
|
do {
|
2015-03-25 17:11:57 +08:00
|
|
|
pmd_t pmd = READ_ONCE(*pmdp);
|
2014-08-13 15:02:02 +08:00
|
|
|
|
|
|
|
old_pmd = pmd_val(pmd);
|
2013-06-20 17:00:21 +08:00
|
|
|
/* If PMD busy, retry the access */
|
2016-04-29 21:25:45 +08:00
|
|
|
if (unlikely(old_pmd & H_PAGE_BUSY))
|
2013-06-20 17:00:21 +08:00
|
|
|
return 0;
|
|
|
|
/* If PMD permissions don't match, take page fault */
|
2016-04-29 21:25:34 +08:00
|
|
|
if (unlikely(!check_pte_access(access, old_pmd)))
|
2013-06-20 17:00:21 +08:00
|
|
|
return 1;
|
|
|
|
/*
|
|
|
|
* Try to lock the PTE, add ACCESSED and DIRTY if it was
|
|
|
|
* a write access
|
|
|
|
*/
|
2016-04-29 21:25:45 +08:00
|
|
|
new_pmd = old_pmd | H_PAGE_BUSY | _PAGE_ACCESSED;
|
2016-04-29 21:25:30 +08:00
|
|
|
if (access & _PAGE_WRITE)
|
2013-06-20 17:00:21 +08:00
|
|
|
new_pmd |= _PAGE_DIRTY;
|
2016-04-29 21:25:28 +08:00
|
|
|
} while (!pmd_xchg(pmdp, __pmd(old_pmd), __pmd(new_pmd)));
|
|
|
|
|
2018-09-21 02:09:45 +08:00
|
|
|
/*
|
|
|
|
* Make sure this is thp or devmap entry
|
|
|
|
*/
|
|
|
|
if (!(old_pmd & (H_PAGE_THP_HUGE | _PAGE_DEVMAP)))
|
|
|
|
return 0;
|
|
|
|
|
2015-12-01 11:36:50 +08:00
|
|
|
rflags = htab_convert_pte_flags(new_pmd);
|
2013-06-20 17:00:21 +08:00
|
|
|
|
|
|
|
#if 0
|
|
|
|
if (!cpu_has_feature(CPU_FTR_COHERENT_ICACHE)) {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* No CPU has hugepages but lacks no execute, so we
|
|
|
|
* don't need to worry about that case
|
|
|
|
*/
|
|
|
|
rflags = hash_page_do_lazy_icache(rflags, __pte(old_pte), trap);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
/*
|
|
|
|
* Find the slot index details for this ea, using base page size.
|
|
|
|
*/
|
|
|
|
shift = mmu_psize_defs[psize].shift;
|
|
|
|
index = (ea & ~HPAGE_PMD_MASK) >> shift;
|
2015-12-01 11:37:00 +08:00
|
|
|
BUG_ON(index >= PTE_FRAG_SIZE);
|
2013-06-20 17:00:21 +08:00
|
|
|
|
|
|
|
vpn = hpt_vpn(ea, vsid, ssize);
|
|
|
|
hpte_slot_array = get_hpte_slot_array(pmdp);
|
2014-08-13 15:01:59 +08:00
|
|
|
if (psize == MMU_PAGE_4K) {
|
|
|
|
/*
|
|
|
|
* invalidate the old hpte entry if we have that mapped via 64K
|
|
|
|
* base page size. This is because demote_segment won't flush
|
|
|
|
* hash page table entries.
|
|
|
|
*/
|
2016-04-29 21:25:45 +08:00
|
|
|
if ((old_pmd & H_PAGE_HASHPTE) && !(old_pmd & H_PAGE_COMBO)) {
|
2014-11-02 23:45:27 +08:00
|
|
|
flush_hash_hugepage(vsid, ea, pmdp, MMU_PAGE_64K,
|
2014-12-04 13:30:14 +08:00
|
|
|
ssize, flags);
|
2016-02-20 23:11:54 +08:00
|
|
|
/*
|
|
|
|
* With THP, we also clear the slot information with
|
|
|
|
* respect to all the 64K hash pte mapping the 16MB
|
|
|
|
* page. They are all invalid now. This make sure we
|
|
|
|
* don't find the slot valid when we fault with 4k
|
|
|
|
* base page size.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
memset(hpte_slot_array, 0, PTE_FRAG_SIZE);
|
|
|
|
}
|
2014-08-13 15:01:59 +08:00
|
|
|
}
|
2013-06-20 17:00:21 +08:00
|
|
|
|
|
|
|
valid = hpte_valid(hpte_slot_array, index);
|
|
|
|
if (valid) {
|
|
|
|
/* update the hpte bits */
|
2015-09-15 15:00:08 +08:00
|
|
|
hash = hpt_hash(vpn, shift, ssize);
|
2013-06-20 17:00:21 +08:00
|
|
|
hidx = hpte_hash_index(hpte_slot_array, index);
|
|
|
|
if (hidx & _PTEIDX_SECONDARY)
|
|
|
|
hash = ~hash;
|
|
|
|
slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
|
|
|
|
slot += hidx & _PTEIDX_GROUP_IX;
|
|
|
|
|
2016-07-05 13:03:58 +08:00
|
|
|
ret = mmu_hash_ops.hpte_updatepp(slot, rflags, vpn,
|
|
|
|
psize, lpsize, ssize, flags);
|
2013-06-20 17:00:21 +08:00
|
|
|
/*
|
|
|
|
* We failed to update, try to insert a new entry.
|
|
|
|
*/
|
|
|
|
if (ret == -1) {
|
|
|
|
/*
|
|
|
|
* large pte is marked busy, so we can be sure
|
|
|
|
* nobody is looking at hpte_slot_array. hence we can
|
|
|
|
* safely update this here.
|
|
|
|
*/
|
|
|
|
valid = 0;
|
|
|
|
hpte_slot_array[index] = 0;
|
2014-08-13 15:01:59 +08:00
|
|
|
}
|
2013-06-20 17:00:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!valid) {
|
|
|
|
unsigned long hpte_group;
|
|
|
|
|
2015-09-15 15:00:08 +08:00
|
|
|
hash = hpt_hash(vpn, shift, ssize);
|
2013-06-20 17:00:21 +08:00
|
|
|
/* insert new entry */
|
|
|
|
pa = pmd_pfn(__pmd(old_pmd)) << PAGE_SHIFT;
|
2016-04-29 21:25:45 +08:00
|
|
|
new_pmd |= H_PAGE_HASHPTE;
|
2013-06-20 17:00:21 +08:00
|
|
|
|
2014-08-13 15:01:59 +08:00
|
|
|
repeat:
|
2018-06-29 16:36:29 +08:00
|
|
|
hpte_group = (hash & htab_hash_mask) * HPTES_PER_GROUP;
|
2013-06-20 17:00:21 +08:00
|
|
|
|
|
|
|
/* Insert into the hash table, primary slot */
|
2016-07-05 13:03:58 +08:00
|
|
|
slot = mmu_hash_ops.hpte_insert(hpte_group, vpn, pa, rflags, 0,
|
|
|
|
psize, lpsize, ssize);
|
2013-06-20 17:00:21 +08:00
|
|
|
/*
|
|
|
|
* Primary is full, try the secondary
|
|
|
|
*/
|
|
|
|
if (unlikely(slot == -1)) {
|
2018-06-29 16:36:29 +08:00
|
|
|
hpte_group = (~hash & htab_hash_mask) * HPTES_PER_GROUP;
|
2016-07-05 13:03:58 +08:00
|
|
|
slot = mmu_hash_ops.hpte_insert(hpte_group, vpn, pa,
|
|
|
|
rflags,
|
|
|
|
HPTE_V_SECONDARY,
|
|
|
|
psize, lpsize, ssize);
|
2013-06-20 17:00:21 +08:00
|
|
|
if (slot == -1) {
|
|
|
|
if (mftb() & 0x1)
|
2018-06-29 16:36:29 +08:00
|
|
|
hpte_group = (hash & htab_hash_mask) *
|
|
|
|
HPTES_PER_GROUP;
|
2013-06-20 17:00:21 +08:00
|
|
|
|
2016-07-05 13:03:58 +08:00
|
|
|
mmu_hash_ops.hpte_remove(hpte_group);
|
2013-06-20 17:00:21 +08:00
|
|
|
goto repeat;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Hypervisor failure. Restore old pmd and return -1
|
|
|
|
* similar to __hash_page_*
|
|
|
|
*/
|
|
|
|
if (unlikely(slot == -2)) {
|
|
|
|
*pmdp = __pmd(old_pmd);
|
|
|
|
hash_failure_debug(ea, access, vsid, trap, ssize,
|
|
|
|
psize, lpsize, old_pmd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* large pte is marked busy, so we can be sure
|
|
|
|
* nobody is looking at hpte_slot_array. hence we can
|
|
|
|
* safely update this here.
|
|
|
|
*/
|
|
|
|
mark_hpte_slot_valid(hpte_slot_array, index, slot);
|
|
|
|
}
|
2014-08-13 15:01:59 +08:00
|
|
|
/*
|
2016-04-29 21:25:45 +08:00
|
|
|
* Mark the pte with H_PAGE_COMBO, if we are trying to hash it with
|
2014-08-13 15:01:59 +08:00
|
|
|
* base page size 4k.
|
|
|
|
*/
|
|
|
|
if (psize == MMU_PAGE_4K)
|
2016-04-29 21:25:45 +08:00
|
|
|
new_pmd |= H_PAGE_COMBO;
|
2013-06-20 17:00:21 +08:00
|
|
|
/*
|
2014-08-13 15:01:57 +08:00
|
|
|
* The hpte valid is stored in the pgtable whose address is in the
|
|
|
|
* second half of the PMD. Order this against clearing of the busy bit in
|
|
|
|
* huge pmd.
|
2013-06-20 17:00:21 +08:00
|
|
|
*/
|
2014-08-13 15:01:57 +08:00
|
|
|
smp_wmb();
|
2016-04-29 21:25:45 +08:00
|
|
|
*pmdp = __pmd(new_pmd & ~H_PAGE_BUSY);
|
2013-06-20 17:00:21 +08:00
|
|
|
return 0;
|
|
|
|
}
|