Aneesh Kumar K.V | a43c0eb | 2015-12-01 09:06:49 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright IBM Corporation, 2015 |
| 3 | * Author Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of version 2 of the GNU Lesser General Public License |
| 7 | * as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it would be useful, but |
| 10 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 | * |
| 13 | */ |
| 14 | |
| 15 | #include <linux/mm.h> |
| 16 | #include <asm/machdep.h> |
| 17 | #include <asm/mmu.h> |
| 18 | |
| 19 | int __hash_page_4K(unsigned long ea, unsigned long access, unsigned long vsid, |
| 20 | pte_t *ptep, unsigned long trap, unsigned long flags, |
| 21 | int ssize, int subpg_prot) |
| 22 | { |
| 23 | unsigned long hpte_group; |
| 24 | unsigned long rflags, pa; |
| 25 | unsigned long old_pte, new_pte; |
| 26 | unsigned long vpn, hash, slot; |
| 27 | unsigned long shift = mmu_psize_defs[MMU_PAGE_4K].shift; |
| 28 | |
| 29 | /* |
| 30 | * atomically mark the linux large page PTE busy and dirty |
| 31 | */ |
| 32 | do { |
| 33 | pte_t pte = READ_ONCE(*ptep); |
| 34 | |
| 35 | old_pte = pte_val(pte); |
| 36 | /* If PTE busy, retry the access */ |
| 37 | if (unlikely(old_pte & _PAGE_BUSY)) |
| 38 | return 0; |
| 39 | /* If PTE permissions don't match, take page fault */ |
| 40 | if (unlikely(access & ~old_pte)) |
| 41 | return 1; |
| 42 | /* |
| 43 | * Try to lock the PTE, add ACCESSED and DIRTY if it was |
| 44 | * a write access. Since this is 4K insert of 64K page size |
| 45 | * also add _PAGE_COMBO |
| 46 | */ |
| 47 | new_pte = old_pte | _PAGE_BUSY | _PAGE_ACCESSED | _PAGE_HASHPTE; |
| 48 | if (access & _PAGE_RW) |
| 49 | new_pte |= _PAGE_DIRTY; |
| 50 | } while (old_pte != __cmpxchg_u64((unsigned long *)ptep, |
| 51 | old_pte, new_pte)); |
| 52 | /* |
| 53 | * PP bits. _PAGE_USER is already PP bit 0x2, so we only |
| 54 | * need to add in 0x1 if it's a read-only user page |
| 55 | */ |
Aneesh Kumar K.V | c6a3c49 | 2015-12-01 09:06:50 +0530 | [diff] [blame] | 56 | rflags = htab_convert_pte_flags(new_pte); |
Aneesh Kumar K.V | a43c0eb | 2015-12-01 09:06:49 +0530 | [diff] [blame] | 57 | |
| 58 | if (!cpu_has_feature(CPU_FTR_NOEXECUTE) && |
| 59 | !cpu_has_feature(CPU_FTR_COHERENT_ICACHE)) |
| 60 | rflags = hash_page_do_lazy_icache(rflags, __pte(old_pte), trap); |
| 61 | |
| 62 | vpn = hpt_vpn(ea, vsid, ssize); |
| 63 | if (unlikely(old_pte & _PAGE_HASHPTE)) { |
| 64 | /* |
| 65 | * There MIGHT be an HPTE for this pte |
| 66 | */ |
| 67 | hash = hpt_hash(vpn, shift, ssize); |
| 68 | if (old_pte & _PAGE_F_SECOND) |
| 69 | hash = ~hash; |
| 70 | slot = (hash & htab_hash_mask) * HPTES_PER_GROUP; |
| 71 | slot += (old_pte & _PAGE_F_GIX) >> _PAGE_F_GIX_SHIFT; |
| 72 | |
| 73 | if (ppc_md.hpte_updatepp(slot, rflags, vpn, MMU_PAGE_4K, |
| 74 | MMU_PAGE_4K, ssize, flags) == -1) |
| 75 | old_pte &= ~_PAGE_HPTEFLAGS; |
| 76 | } |
| 77 | |
| 78 | if (likely(!(old_pte & _PAGE_HASHPTE))) { |
| 79 | |
| 80 | pa = pte_pfn(__pte(old_pte)) << PAGE_SHIFT; |
| 81 | hash = hpt_hash(vpn, shift, ssize); |
| 82 | |
| 83 | repeat: |
| 84 | hpte_group = ((hash & htab_hash_mask) * HPTES_PER_GROUP) & ~0x7UL; |
| 85 | |
| 86 | /* Insert into the hash table, primary slot */ |
| 87 | slot = ppc_md.hpte_insert(hpte_group, vpn, pa, rflags, 0, |
| 88 | MMU_PAGE_4K, MMU_PAGE_4K, ssize); |
| 89 | /* |
| 90 | * Primary is full, try the secondary |
| 91 | */ |
| 92 | if (unlikely(slot == -1)) { |
| 93 | hpte_group = ((~hash & htab_hash_mask) * HPTES_PER_GROUP) & ~0x7UL; |
| 94 | slot = ppc_md.hpte_insert(hpte_group, vpn, pa, |
| 95 | rflags, HPTE_V_SECONDARY, |
| 96 | MMU_PAGE_4K, MMU_PAGE_4K, ssize); |
| 97 | if (slot == -1) { |
| 98 | if (mftb() & 0x1) |
| 99 | hpte_group = ((hash & htab_hash_mask) * |
| 100 | HPTES_PER_GROUP) & ~0x7UL; |
| 101 | ppc_md.hpte_remove(hpte_group); |
| 102 | /* |
| 103 | * FIXME!! Should be try the group from which we removed ? |
| 104 | */ |
| 105 | goto repeat; |
| 106 | } |
| 107 | } |
| 108 | /* |
| 109 | * Hypervisor failure. Restore old pmd and return -1 |
| 110 | * similar to __hash_page_* |
| 111 | */ |
| 112 | if (unlikely(slot == -2)) { |
| 113 | *ptep = __pte(old_pte); |
| 114 | hash_failure_debug(ea, access, vsid, trap, ssize, |
| 115 | MMU_PAGE_4K, MMU_PAGE_4K, old_pte); |
| 116 | return -1; |
| 117 | } |
| 118 | new_pte = (new_pte & ~_PAGE_HPTEFLAGS) | _PAGE_HASHPTE; |
| 119 | new_pte |= (slot << _PAGE_F_GIX_SHIFT) & (_PAGE_F_SECOND | _PAGE_F_GIX); |
| 120 | } |
| 121 | *ptep = __pte(new_pte & ~_PAGE_BUSY); |
| 122 | return 0; |
| 123 | } |