162306a36Sopenharmony_ci/* 262306a36Sopenharmony_ci * Copyright IBM Corporation, 2015 362306a36Sopenharmony_ci * Author Aneesh Kumar K.V <aneesh.kumar@linux.ibm.com> 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * This program is free software; you can redistribute it and/or modify it 662306a36Sopenharmony_ci * under the terms of version 2 of the GNU Lesser General Public License 762306a36Sopenharmony_ci * as published by the Free Software Foundation. 862306a36Sopenharmony_ci * 962306a36Sopenharmony_ci * This program is distributed in the hope that it would be useful, but 1062306a36Sopenharmony_ci * WITHOUT ANY WARRANTY; without even the implied warranty of 1162306a36Sopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 1262306a36Sopenharmony_ci * 1362306a36Sopenharmony_ci */ 1462306a36Sopenharmony_ci 1562306a36Sopenharmony_ci#include <linux/mm.h> 1662306a36Sopenharmony_ci#include <asm/machdep.h> 1762306a36Sopenharmony_ci#include <asm/mmu.h> 1862306a36Sopenharmony_ci 1962306a36Sopenharmony_ci#include "internal.h" 2062306a36Sopenharmony_ci 2162306a36Sopenharmony_ciint __hash_page_4K(unsigned long ea, unsigned long access, unsigned long vsid, 2262306a36Sopenharmony_ci pte_t *ptep, unsigned long trap, unsigned long flags, 2362306a36Sopenharmony_ci int ssize, int subpg_prot) 2462306a36Sopenharmony_ci{ 2562306a36Sopenharmony_ci real_pte_t rpte; 2662306a36Sopenharmony_ci unsigned long hpte_group; 2762306a36Sopenharmony_ci unsigned long rflags, pa; 2862306a36Sopenharmony_ci unsigned long old_pte, new_pte; 2962306a36Sopenharmony_ci unsigned long vpn, hash, slot; 3062306a36Sopenharmony_ci unsigned long shift = mmu_psize_defs[MMU_PAGE_4K].shift; 3162306a36Sopenharmony_ci 3262306a36Sopenharmony_ci /* 3362306a36Sopenharmony_ci * atomically mark the linux large page PTE busy and dirty 3462306a36Sopenharmony_ci */ 3562306a36Sopenharmony_ci do { 3662306a36Sopenharmony_ci pte_t pte = READ_ONCE(*ptep); 3762306a36Sopenharmony_ci 3862306a36Sopenharmony_ci old_pte = pte_val(pte); 3962306a36Sopenharmony_ci /* If PTE busy, retry the access */ 4062306a36Sopenharmony_ci if (unlikely(old_pte & H_PAGE_BUSY)) 4162306a36Sopenharmony_ci return 0; 4262306a36Sopenharmony_ci /* If PTE permissions don't match, take page fault */ 4362306a36Sopenharmony_ci if (unlikely(!check_pte_access(access, old_pte))) 4462306a36Sopenharmony_ci return 1; 4562306a36Sopenharmony_ci /* 4662306a36Sopenharmony_ci * Try to lock the PTE, add ACCESSED and DIRTY if it was 4762306a36Sopenharmony_ci * a write access. Since this is 4K insert of 64K page size 4862306a36Sopenharmony_ci * also add H_PAGE_COMBO 4962306a36Sopenharmony_ci */ 5062306a36Sopenharmony_ci new_pte = old_pte | H_PAGE_BUSY | _PAGE_ACCESSED; 5162306a36Sopenharmony_ci if (access & _PAGE_WRITE) 5262306a36Sopenharmony_ci new_pte |= _PAGE_DIRTY; 5362306a36Sopenharmony_ci } while (!pte_xchg(ptep, __pte(old_pte), __pte(new_pte))); 5462306a36Sopenharmony_ci 5562306a36Sopenharmony_ci /* 5662306a36Sopenharmony_ci * PP bits. _PAGE_USER is already PP bit 0x2, so we only 5762306a36Sopenharmony_ci * need to add in 0x1 if it's a read-only user page 5862306a36Sopenharmony_ci */ 5962306a36Sopenharmony_ci rflags = htab_convert_pte_flags(new_pte, flags); 6062306a36Sopenharmony_ci rpte = __real_pte(__pte(old_pte), ptep, PTRS_PER_PTE); 6162306a36Sopenharmony_ci 6262306a36Sopenharmony_ci if (cpu_has_feature(CPU_FTR_NOEXECUTE) && 6362306a36Sopenharmony_ci !cpu_has_feature(CPU_FTR_COHERENT_ICACHE)) 6462306a36Sopenharmony_ci rflags = hash_page_do_lazy_icache(rflags, __pte(old_pte), trap); 6562306a36Sopenharmony_ci 6662306a36Sopenharmony_ci vpn = hpt_vpn(ea, vsid, ssize); 6762306a36Sopenharmony_ci if (unlikely(old_pte & H_PAGE_HASHPTE)) { 6862306a36Sopenharmony_ci /* 6962306a36Sopenharmony_ci * There MIGHT be an HPTE for this pte 7062306a36Sopenharmony_ci */ 7162306a36Sopenharmony_ci unsigned long gslot = pte_get_hash_gslot(vpn, shift, ssize, 7262306a36Sopenharmony_ci rpte, 0); 7362306a36Sopenharmony_ci 7462306a36Sopenharmony_ci if (mmu_hash_ops.hpte_updatepp(gslot, rflags, vpn, MMU_PAGE_4K, 7562306a36Sopenharmony_ci MMU_PAGE_4K, ssize, flags) == -1) 7662306a36Sopenharmony_ci old_pte &= ~_PAGE_HPTEFLAGS; 7762306a36Sopenharmony_ci } 7862306a36Sopenharmony_ci 7962306a36Sopenharmony_ci if (likely(!(old_pte & H_PAGE_HASHPTE))) { 8062306a36Sopenharmony_ci 8162306a36Sopenharmony_ci pa = pte_pfn(__pte(old_pte)) << PAGE_SHIFT; 8262306a36Sopenharmony_ci hash = hpt_hash(vpn, shift, ssize); 8362306a36Sopenharmony_ci 8462306a36Sopenharmony_cirepeat: 8562306a36Sopenharmony_ci hpte_group = (hash & htab_hash_mask) * HPTES_PER_GROUP; 8662306a36Sopenharmony_ci 8762306a36Sopenharmony_ci /* Insert into the hash table, primary slot */ 8862306a36Sopenharmony_ci slot = mmu_hash_ops.hpte_insert(hpte_group, vpn, pa, rflags, 0, 8962306a36Sopenharmony_ci MMU_PAGE_4K, MMU_PAGE_4K, ssize); 9062306a36Sopenharmony_ci /* 9162306a36Sopenharmony_ci * Primary is full, try the secondary 9262306a36Sopenharmony_ci */ 9362306a36Sopenharmony_ci if (unlikely(slot == -1)) { 9462306a36Sopenharmony_ci hpte_group = (~hash & htab_hash_mask) * HPTES_PER_GROUP; 9562306a36Sopenharmony_ci slot = mmu_hash_ops.hpte_insert(hpte_group, vpn, pa, 9662306a36Sopenharmony_ci rflags, 9762306a36Sopenharmony_ci HPTE_V_SECONDARY, 9862306a36Sopenharmony_ci MMU_PAGE_4K, 9962306a36Sopenharmony_ci MMU_PAGE_4K, ssize); 10062306a36Sopenharmony_ci if (slot == -1) { 10162306a36Sopenharmony_ci if (mftb() & 0x1) 10262306a36Sopenharmony_ci hpte_group = (hash & htab_hash_mask) * 10362306a36Sopenharmony_ci HPTES_PER_GROUP; 10462306a36Sopenharmony_ci mmu_hash_ops.hpte_remove(hpte_group); 10562306a36Sopenharmony_ci /* 10662306a36Sopenharmony_ci * FIXME!! Should be try the group from which we removed ? 10762306a36Sopenharmony_ci */ 10862306a36Sopenharmony_ci goto repeat; 10962306a36Sopenharmony_ci } 11062306a36Sopenharmony_ci } 11162306a36Sopenharmony_ci /* 11262306a36Sopenharmony_ci * Hypervisor failure. Restore old pte and return -1 11362306a36Sopenharmony_ci * similar to __hash_page_* 11462306a36Sopenharmony_ci */ 11562306a36Sopenharmony_ci if (unlikely(slot == -2)) { 11662306a36Sopenharmony_ci *ptep = __pte(old_pte); 11762306a36Sopenharmony_ci hash_failure_debug(ea, access, vsid, trap, ssize, 11862306a36Sopenharmony_ci MMU_PAGE_4K, MMU_PAGE_4K, old_pte); 11962306a36Sopenharmony_ci return -1; 12062306a36Sopenharmony_ci } 12162306a36Sopenharmony_ci new_pte = (new_pte & ~_PAGE_HPTEFLAGS) | H_PAGE_HASHPTE; 12262306a36Sopenharmony_ci new_pte |= pte_set_hidx(ptep, rpte, 0, slot, PTRS_PER_PTE); 12362306a36Sopenharmony_ci 12462306a36Sopenharmony_ci if (stress_hpt()) 12562306a36Sopenharmony_ci hpt_do_stress(ea, hpte_group); 12662306a36Sopenharmony_ci } 12762306a36Sopenharmony_ci *ptep = __pte(new_pte & ~H_PAGE_BUSY); 12862306a36Sopenharmony_ci return 0; 12962306a36Sopenharmony_ci} 130