147d99948SChristophe Leroy /* 247d99948SChristophe Leroy * Copyright IBM Corporation, 2015 347d99948SChristophe Leroy * Author Aneesh Kumar K.V <aneesh.kumar@linux.ibm.com> 447d99948SChristophe Leroy * 547d99948SChristophe Leroy * This program is free software; you can redistribute it and/or modify it 647d99948SChristophe Leroy * under the terms of version 2 of the GNU Lesser General Public License 747d99948SChristophe Leroy * as published by the Free Software Foundation. 847d99948SChristophe Leroy * 947d99948SChristophe Leroy * This program is distributed in the hope that it would be useful, but 1047d99948SChristophe Leroy * WITHOUT ANY WARRANTY; without even the implied warranty of 1147d99948SChristophe Leroy * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 1247d99948SChristophe Leroy * 1347d99948SChristophe Leroy */ 1447d99948SChristophe Leroy 1547d99948SChristophe Leroy #include <linux/mm.h> 1647d99948SChristophe Leroy #include <asm/machdep.h> 1747d99948SChristophe Leroy #include <asm/mmu.h> 1847d99948SChristophe Leroy 1947d99948SChristophe Leroy int __hash_page_4K(unsigned long ea, unsigned long access, unsigned long vsid, 2047d99948SChristophe Leroy pte_t *ptep, unsigned long trap, unsigned long flags, 2147d99948SChristophe Leroy int ssize, int subpg_prot) 2247d99948SChristophe Leroy { 2347d99948SChristophe Leroy real_pte_t rpte; 2447d99948SChristophe Leroy unsigned long hpte_group; 2547d99948SChristophe Leroy unsigned long rflags, pa; 2647d99948SChristophe Leroy unsigned long old_pte, new_pte; 2747d99948SChristophe Leroy unsigned long vpn, hash, slot; 2847d99948SChristophe Leroy unsigned long shift = mmu_psize_defs[MMU_PAGE_4K].shift; 2947d99948SChristophe Leroy 3047d99948SChristophe Leroy /* 3147d99948SChristophe Leroy * atomically mark the linux large page PTE busy and dirty 3247d99948SChristophe Leroy */ 3347d99948SChristophe Leroy do { 3447d99948SChristophe Leroy pte_t pte = READ_ONCE(*ptep); 3547d99948SChristophe Leroy 3647d99948SChristophe Leroy old_pte = pte_val(pte); 3747d99948SChristophe Leroy /* If PTE busy, retry the access */ 3847d99948SChristophe Leroy if (unlikely(old_pte & H_PAGE_BUSY)) 3947d99948SChristophe Leroy return 0; 4047d99948SChristophe Leroy /* If PTE permissions don't match, take page fault */ 4147d99948SChristophe Leroy if (unlikely(!check_pte_access(access, old_pte))) 4247d99948SChristophe Leroy return 1; 4347d99948SChristophe Leroy /* 4447d99948SChristophe Leroy * Try to lock the PTE, add ACCESSED and DIRTY if it was 4547d99948SChristophe Leroy * a write access. Since this is 4K insert of 64K page size 4647d99948SChristophe Leroy * also add H_PAGE_COMBO 4747d99948SChristophe Leroy */ 4847d99948SChristophe Leroy new_pte = old_pte | H_PAGE_BUSY | _PAGE_ACCESSED; 4947d99948SChristophe Leroy if (access & _PAGE_WRITE) 5047d99948SChristophe Leroy new_pte |= _PAGE_DIRTY; 5147d99948SChristophe Leroy } while (!pte_xchg(ptep, __pte(old_pte), __pte(new_pte))); 5247d99948SChristophe Leroy 5347d99948SChristophe Leroy /* 5447d99948SChristophe Leroy * PP bits. _PAGE_USER is already PP bit 0x2, so we only 5547d99948SChristophe Leroy * need to add in 0x1 if it's a read-only user page 5647d99948SChristophe Leroy */ 57*d94b827eSAneesh Kumar K.V rflags = htab_convert_pte_flags(new_pte, flags); 5847d99948SChristophe Leroy rpte = __real_pte(__pte(old_pte), ptep, PTRS_PER_PTE); 5947d99948SChristophe Leroy 6047d99948SChristophe Leroy if (cpu_has_feature(CPU_FTR_NOEXECUTE) && 6147d99948SChristophe Leroy !cpu_has_feature(CPU_FTR_COHERENT_ICACHE)) 6247d99948SChristophe Leroy rflags = hash_page_do_lazy_icache(rflags, __pte(old_pte), trap); 6347d99948SChristophe Leroy 6447d99948SChristophe Leroy vpn = hpt_vpn(ea, vsid, ssize); 6547d99948SChristophe Leroy if (unlikely(old_pte & H_PAGE_HASHPTE)) { 6647d99948SChristophe Leroy /* 6747d99948SChristophe Leroy * There MIGHT be an HPTE for this pte 6847d99948SChristophe Leroy */ 6947d99948SChristophe Leroy unsigned long gslot = pte_get_hash_gslot(vpn, shift, ssize, 7047d99948SChristophe Leroy rpte, 0); 7147d99948SChristophe Leroy 7247d99948SChristophe Leroy if (mmu_hash_ops.hpte_updatepp(gslot, rflags, vpn, MMU_PAGE_4K, 7347d99948SChristophe Leroy MMU_PAGE_4K, ssize, flags) == -1) 7447d99948SChristophe Leroy old_pte &= ~_PAGE_HPTEFLAGS; 7547d99948SChristophe Leroy } 7647d99948SChristophe Leroy 7747d99948SChristophe Leroy if (likely(!(old_pte & H_PAGE_HASHPTE))) { 7847d99948SChristophe Leroy 7947d99948SChristophe Leroy pa = pte_pfn(__pte(old_pte)) << PAGE_SHIFT; 8047d99948SChristophe Leroy hash = hpt_hash(vpn, shift, ssize); 8147d99948SChristophe Leroy 8247d99948SChristophe Leroy repeat: 8347d99948SChristophe Leroy hpte_group = (hash & htab_hash_mask) * HPTES_PER_GROUP; 8447d99948SChristophe Leroy 8547d99948SChristophe Leroy /* Insert into the hash table, primary slot */ 8647d99948SChristophe Leroy slot = mmu_hash_ops.hpte_insert(hpte_group, vpn, pa, rflags, 0, 8747d99948SChristophe Leroy MMU_PAGE_4K, MMU_PAGE_4K, ssize); 8847d99948SChristophe Leroy /* 8947d99948SChristophe Leroy * Primary is full, try the secondary 9047d99948SChristophe Leroy */ 9147d99948SChristophe Leroy if (unlikely(slot == -1)) { 9247d99948SChristophe Leroy hpte_group = (~hash & htab_hash_mask) * HPTES_PER_GROUP; 9347d99948SChristophe Leroy slot = mmu_hash_ops.hpte_insert(hpte_group, vpn, pa, 9447d99948SChristophe Leroy rflags, 9547d99948SChristophe Leroy HPTE_V_SECONDARY, 9647d99948SChristophe Leroy MMU_PAGE_4K, 9747d99948SChristophe Leroy MMU_PAGE_4K, ssize); 9847d99948SChristophe Leroy if (slot == -1) { 9947d99948SChristophe Leroy if (mftb() & 0x1) 10047d99948SChristophe Leroy hpte_group = (hash & htab_hash_mask) * 10147d99948SChristophe Leroy HPTES_PER_GROUP; 10247d99948SChristophe Leroy mmu_hash_ops.hpte_remove(hpte_group); 10347d99948SChristophe Leroy /* 10447d99948SChristophe Leroy * FIXME!! Should be try the group from which we removed ? 10547d99948SChristophe Leroy */ 10647d99948SChristophe Leroy goto repeat; 10747d99948SChristophe Leroy } 10847d99948SChristophe Leroy } 10947d99948SChristophe Leroy /* 11047d99948SChristophe Leroy * Hypervisor failure. Restore old pte and return -1 11147d99948SChristophe Leroy * similar to __hash_page_* 11247d99948SChristophe Leroy */ 11347d99948SChristophe Leroy if (unlikely(slot == -2)) { 11447d99948SChristophe Leroy *ptep = __pte(old_pte); 11547d99948SChristophe Leroy hash_failure_debug(ea, access, vsid, trap, ssize, 11647d99948SChristophe Leroy MMU_PAGE_4K, MMU_PAGE_4K, old_pte); 11747d99948SChristophe Leroy return -1; 11847d99948SChristophe Leroy } 11947d99948SChristophe Leroy new_pte = (new_pte & ~_PAGE_HPTEFLAGS) | H_PAGE_HASHPTE; 12047d99948SChristophe Leroy new_pte |= pte_set_hidx(ptep, rpte, 0, slot, PTRS_PER_PTE); 12147d99948SChristophe Leroy } 12247d99948SChristophe Leroy *ptep = __pte(new_pte & ~H_PAGE_BUSY); 12347d99948SChristophe Leroy return 0; 12447d99948SChristophe Leroy } 125