hash64_64k.c 9.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Copyright IBM Corporation, 2015
 * 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 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.
 *
 */

#include <linux/mm.h>
#include <asm/machdep.h>
#include <asm/mmu.h>
18

19
/*
20 21
 * Return true, if the entry has a slot value which
 * the software considers as invalid.
22
 */
23
static inline bool hpte_soft_invalid(unsigned long hidx)
24
{
25
	return ((hidx & 0xfUL) == 0xfUL);
26
}
27

28 29 30
/*
 * index from 0 - 15
 */
31
bool __rpte_sub_valid(real_pte_t rpte, unsigned long index)
32
{
33
	return !(hpte_soft_invalid(__rpte_to_hidx(rpte, index)));
34
}
35 36 37 38 39 40 41 42

int __hash_page_4K(unsigned long ea, unsigned long access, unsigned long vsid,
		   pte_t *ptep, unsigned long trap, unsigned long flags,
		   int ssize, int subpg_prot)
{
	real_pte_t rpte;
	unsigned long hpte_group;
	unsigned int subpg_index;
43
	unsigned long rflags, pa;
44
	unsigned long old_pte, new_pte, subpg_pte;
45
	unsigned long vpn, hash, slot, gslot;
46 47 48 49 50 51 52 53 54 55
	unsigned long shift = mmu_psize_defs[MMU_PAGE_4K].shift;

	/*
	 * atomically mark the linux large page PTE busy and dirty
	 */
	do {
		pte_t pte = READ_ONCE(*ptep);

		old_pte = pte_val(pte);
		/* If PTE busy, retry the access */
56
		if (unlikely(old_pte & H_PAGE_BUSY))
57 58
			return 0;
		/* If PTE permissions don't match, take page fault */
59
		if (unlikely(!check_pte_access(access, old_pte)))
60 61 62 63
			return 1;
		/*
		 * Try to lock the PTE, add ACCESSED and DIRTY if it was
		 * a write access. Since this is 4K insert of 64K page size
64
		 * also add H_PAGE_COMBO
65
		 */
66
		new_pte = old_pte | H_PAGE_BUSY | _PAGE_ACCESSED | H_PAGE_COMBO;
67
		if (access & _PAGE_WRITE)
68
			new_pte |= _PAGE_DIRTY;
69 70
	} while (!pte_xchg(ptep, __pte(old_pte), __pte(new_pte)));

71 72 73 74
	/*
	 * Handle the subpage protection bits
	 */
	subpg_pte = new_pte & ~subpg_prot;
75
	rflags = htab_convert_pte_flags(subpg_pte);
76

77
	if (cpu_has_feature(CPU_FTR_NOEXECUTE) &&
78 79 80 81 82 83 84 85 86 87 88
	    !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);
	}

	subpg_index = (ea & (PAGE_SIZE - 1)) >> shift;
	vpn  = hpt_vpn(ea, vsid, ssize);
89
	rpte = __real_pte(__pte(old_pte), ptep, PTRS_PER_PTE);
90 91 92
	/*
	 *None of the sub 4k page is hashed
	 */
93
	if (!(old_pte & H_PAGE_HASHPTE))
94 95 96 97 98
		goto htab_insert_hpte;
	/*
	 * Check if the pte was already inserted into the hash table
	 * as a 64k HW page, and invalidate the 64k HPTE if so.
	 */
99
	if (!(old_pte & H_PAGE_COMBO)) {
100
		flush_hash_page(vpn, rpte, MMU_PAGE_64K, ssize, flags);
101 102 103 104 105
		/*
		 * clear the old slot details from the old and new pte.
		 * On hash insert failure we use old pte value and we don't
		 * want slot information there if we have a insert failure.
		 */
106 107
		old_pte &= ~H_PAGE_HASHPTE;
		new_pte &= ~H_PAGE_HASHPTE;
108 109 110 111 112 113 114 115
		goto htab_insert_hpte;
	}
	/*
	 * Check for sub page valid and update
	 */
	if (__rpte_sub_valid(rpte, subpg_index)) {
		int ret;

116 117 118
		gslot = pte_get_hash_gslot(vpn, shift, ssize, rpte,
					   subpg_index);
		ret = mmu_hash_ops.hpte_updatepp(gslot, rflags, vpn,
119 120
						 MMU_PAGE_4K, MMU_PAGE_4K,
						 ssize, flags);
121

122
		/*
123
		 * If we failed because typically the HPTE wasn't really here
124 125 126 127 128
		 * we try an insertion.
		 */
		if (ret == -1)
			goto htab_insert_hpte;

129
		*ptep = __pte(new_pte & ~H_PAGE_BUSY);
130 131 132 133
		return 0;
	}

htab_insert_hpte:
134 135 136 137 138 139 140 141

	/*
	 * Initialize all hidx entries to invalid value, the first time
	 * the PTE is about to allocate a 4K HPTE.
	 */
	if (!(old_pte & H_PAGE_COMBO))
		rpte.hidx = INVALID_RPTE_HIDX;

142
	/*
143
	 * handle H_PAGE_4K_PFN case
144
	 */
145
	if (old_pte & H_PAGE_4K_PFN) {
146 147 148 149 150 151 152 153 154 155 156 157 158 159
		/*
		 * All the sub 4k page have the same
		 * physical address.
		 */
		pa = pte_pfn(__pte(old_pte)) << HW_PAGE_SHIFT;
	} else {
		pa = pte_pfn(__pte(old_pte)) << PAGE_SHIFT;
		pa += (subpg_index << shift);
	}
	hash = hpt_hash(vpn, shift, ssize);
repeat:
	hpte_group = ((hash & htab_hash_mask) * HPTES_PER_GROUP) & ~0x7UL;

	/* Insert into the hash table, primary slot */
160 161
	slot = mmu_hash_ops.hpte_insert(hpte_group, vpn, pa, rflags, 0,
					MMU_PAGE_4K, MMU_PAGE_4K, ssize);
162 163 164 165
	/*
	 * Primary is full, try the secondary
	 */
	if (unlikely(slot == -1)) {
166 167
		bool soft_invalid;

168
		hpte_group = ((~hash & htab_hash_mask) * HPTES_PER_GROUP) & ~0x7UL;
169 170 171 172
		slot = mmu_hash_ops.hpte_insert(hpte_group, vpn, pa,
						rflags, HPTE_V_SECONDARY,
						MMU_PAGE_4K, MMU_PAGE_4K,
						ssize);
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195

		soft_invalid = hpte_soft_invalid(slot);
		if (unlikely(soft_invalid)) {
			/*
			 * We got a valid slot from a hardware point of view.
			 * but we cannot use it, because we use this special
			 * value; as defined by hpte_soft_invalid(), to track
			 * invalid slots. We cannot use it. So invalidate it.
			 */
			gslot = slot & _PTEIDX_GROUP_IX;
			mmu_hash_ops.hpte_invalidate(hpte_group + gslot, vpn,
						     MMU_PAGE_4K, MMU_PAGE_4K,
						     ssize, 0);
		}

		if (unlikely(slot == -1 || soft_invalid)) {
			/*
			 * For soft invalid slot, let's ensure that we release a
			 * slot from the primary, with the hope that we will
			 * acquire that slot next time we try. This will ensure
			 * that we do not get the same soft-invalid slot.
			 */
			if (soft_invalid || (mftb() & 0x1))
196 197
				hpte_group = ((hash & htab_hash_mask) *
					      HPTES_PER_GROUP) & ~0x7UL;
198

199
			mmu_hash_ops.hpte_remove(hpte_group);
200 201 202 203 204 205 206
			/*
			 * FIXME!! Should be try the group from which we removed ?
			 */
			goto repeat;
		}
	}
	/*
207
	 * Hypervisor failure. Restore old pte and return -1
208 209 210 211 212 213 214 215
	 * similar to __hash_page_*
	 */
	if (unlikely(slot == -2)) {
		*ptep = __pte(old_pte);
		hash_failure_debug(ea, access, vsid, trap, ssize,
				   MMU_PAGE_4K, MMU_PAGE_4K, old_pte);
		return -1;
	}
216

217
	new_pte |= pte_set_hidx(ptep, rpte, subpg_index, slot, PTRS_PER_PTE);
218 219
	new_pte |= H_PAGE_HASHPTE;

220
	*ptep = __pte(new_pte & ~H_PAGE_BUSY);
221 222
	return 0;
}
223 224 225 226 227

int __hash_page_64K(unsigned long ea, unsigned long access,
		    unsigned long vsid, pte_t *ptep, unsigned long trap,
		    unsigned long flags, int ssize)
{
228
	real_pte_t rpte;
229 230 231 232 233 234 235 236 237 238 239 240 241 242
	unsigned long hpte_group;
	unsigned long rflags, pa;
	unsigned long old_pte, new_pte;
	unsigned long vpn, hash, slot;
	unsigned long shift = mmu_psize_defs[MMU_PAGE_64K].shift;

	/*
	 * atomically mark the linux large page PTE busy and dirty
	 */
	do {
		pte_t pte = READ_ONCE(*ptep);

		old_pte = pte_val(pte);
		/* If PTE busy, retry the access */
243
		if (unlikely(old_pte & H_PAGE_BUSY))
244 245
			return 0;
		/* If PTE permissions don't match, take page fault */
246
		if (unlikely(!check_pte_access(access, old_pte)))
247 248 249 250 251 252
			return 1;
		/*
		 * Check if PTE has the cache-inhibit bit set
		 * If so, bail out and refault as a 4k page
		 */
		if (!mmu_has_feature(MMU_FTR_CI_LARGE_PAGE) &&
253
		    unlikely(pte_ci(pte)))
254 255 256
			return 0;
		/*
		 * Try to lock the PTE, add ACCESSED and DIRTY if it was
257
		 * a write access.
258
		 */
259
		new_pte = old_pte | H_PAGE_BUSY | _PAGE_ACCESSED;
260
		if (access & _PAGE_WRITE)
261
			new_pte |= _PAGE_DIRTY;
262
	} while (!pte_xchg(ptep, __pte(old_pte), __pte(new_pte)));
263 264

	rflags = htab_convert_pte_flags(new_pte);
265
	rpte = __real_pte(__pte(old_pte), ptep, PTRS_PER_PTE);
266

267
	if (cpu_has_feature(CPU_FTR_NOEXECUTE) &&
268 269 270 271
	    !cpu_has_feature(CPU_FTR_COHERENT_ICACHE))
		rflags = hash_page_do_lazy_icache(rflags, __pte(old_pte), trap);

	vpn  = hpt_vpn(ea, vsid, ssize);
272
	if (unlikely(old_pte & H_PAGE_HASHPTE)) {
273 274
		unsigned long gslot;

275 276 277
		/*
		 * There MIGHT be an HPTE for this pte
		 */
278 279
		gslot = pte_get_hash_gslot(vpn, shift, ssize, rpte, 0);
		if (mmu_hash_ops.hpte_updatepp(gslot, rflags, vpn, MMU_PAGE_64K,
280 281
					       MMU_PAGE_64K, ssize,
					       flags) == -1)
282 283 284
			old_pte &= ~_PAGE_HPTEFLAGS;
	}

285
	if (likely(!(old_pte & H_PAGE_HASHPTE))) {
286 287 288 289 290 291 292 293

		pa = pte_pfn(__pte(old_pte)) << PAGE_SHIFT;
		hash = hpt_hash(vpn, shift, ssize);

repeat:
		hpte_group = ((hash & htab_hash_mask) * HPTES_PER_GROUP) & ~0x7UL;

		/* Insert into the hash table, primary slot */
294 295 296
		slot = mmu_hash_ops.hpte_insert(hpte_group, vpn, pa, rflags, 0,
						MMU_PAGE_64K, MMU_PAGE_64K,
						ssize);
297 298 299 300 301
		/*
		 * Primary is full, try the secondary
		 */
		if (unlikely(slot == -1)) {
			hpte_group = ((~hash & htab_hash_mask) * HPTES_PER_GROUP) & ~0x7UL;
302 303 304 305 306
			slot = mmu_hash_ops.hpte_insert(hpte_group, vpn, pa,
							rflags,
							HPTE_V_SECONDARY,
							MMU_PAGE_64K,
							MMU_PAGE_64K, ssize);
307 308 309 310
			if (slot == -1) {
				if (mftb() & 0x1)
					hpte_group = ((hash & htab_hash_mask) *
						      HPTES_PER_GROUP) & ~0x7UL;
311
				mmu_hash_ops.hpte_remove(hpte_group);
312 313 314 315 316 317 318
				/*
				 * FIXME!! Should be try the group from which we removed ?
				 */
				goto repeat;
			}
		}
		/*
319
		 * Hypervisor failure. Restore old pte and return -1
320 321 322 323 324 325 326 327
		 * similar to __hash_page_*
		 */
		if (unlikely(slot == -2)) {
			*ptep = __pte(old_pte);
			hash_failure_debug(ea, access, vsid, trap, ssize,
					   MMU_PAGE_64K, MMU_PAGE_64K, old_pte);
			return -1;
		}
328

329
		new_pte = (new_pte & ~_PAGE_HPTEFLAGS) | H_PAGE_HASHPTE;
330
		new_pte |= pte_set_hidx(ptep, rpte, 0, slot, PTRS_PER_PTE);
331
	}
332
	*ptep = __pte(new_pte & ~H_PAGE_BUSY);
333 334
	return 0;
}