blob: 0c2a91df3210e23b24c1657b3cf722242b6e206c [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
David Gibson883a3e52009-10-26 19:24:31 +00002/*
3 * PPC64 Huge TLB Page Support for hash based MMUs (POWER4 and later)
4 *
5 * Copyright (C) 2003 David Gibson, IBM Corporation.
6 *
7 * Based on the IA-32 version:
8 * Copyright (C) 2002, Rohit Seth <rohit.seth@intel.com>
9 */
10
11#include <linux/mm.h>
12#include <linux/hugetlb.h>
13#include <asm/pgtable.h>
14#include <asm/pgalloc.h>
15#include <asm/cacheflush.h>
16#include <asm/machdep.h>
17
Li Zhongb170bd32013-04-15 16:53:19 +000018extern long hpte_insert_repeating(unsigned long hash, unsigned long vpn,
19 unsigned long pa, unsigned long rlags,
20 unsigned long vflags, int psize, int ssize);
21
David Gibson883a3e52009-10-26 19:24:31 +000022int __hash_page_huge(unsigned long ea, unsigned long access, unsigned long vsid,
Aneesh Kumar K.Vaefa5682014-12-04 11:00:14 +053023 pte_t *ptep, unsigned long trap, unsigned long flags,
24 int ssize, unsigned int shift, unsigned int mmu_psize)
David Gibson883a3e52009-10-26 19:24:31 +000025{
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +000026 unsigned long vpn;
David Gibson883a3e52009-10-26 19:24:31 +000027 unsigned long old_pte, new_pte;
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +000028 unsigned long rflags, pa, sz;
David Gibson883a3e52009-10-26 19:24:31 +000029 long slot;
David Gibson883a3e52009-10-26 19:24:31 +000030
31 BUG_ON(shift != mmu_psize_defs[mmu_psize].shift);
32
33 /* Search the Linux page table for a match with va */
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +000034 vpn = hpt_vpn(ea, vsid, ssize);
David Gibson883a3e52009-10-26 19:24:31 +000035
Benjamin Herrenschmidt171aa2c2010-07-23 09:02:27 +100036 /* At this point, we have a pte (old_pte) which can be used to build
David Gibson883a3e52009-10-26 19:24:31 +000037 * or update an HPTE. There are 2 cases:
38 *
39 * 1. There is a valid (present) pte with no associated HPTE (this is
40 * the most common case)
41 * 2. There is a valid (present) pte with an associated HPTE. The
42 * current values of the pp bits in the HPTE prevent access
43 * because we are doing software DIRTY bit management and the
44 * page is currently not DIRTY.
45 */
46
47
48 do {
49 old_pte = pte_val(*ptep);
Benjamin Herrenschmidt171aa2c2010-07-23 09:02:27 +100050 /* If PTE busy, retry the access */
Aneesh Kumar K.V945537d2016-04-29 23:25:45 +100051 if (unlikely(old_pte & H_PAGE_BUSY))
Benjamin Herrenschmidt171aa2c2010-07-23 09:02:27 +100052 return 0;
53 /* If PTE permissions don't match, take page fault */
Aneesh Kumar K.Vac29c642016-04-29 23:25:34 +100054 if (unlikely(!check_pte_access(access, old_pte)))
Benjamin Herrenschmidt171aa2c2010-07-23 09:02:27 +100055 return 1;
Aneesh Kumar K.Vac29c642016-04-29 23:25:34 +100056
Benjamin Herrenschmidt171aa2c2010-07-23 09:02:27 +100057 /* Try to lock the PTE, add ACCESSED and DIRTY if it was
58 * a write access */
Aneesh Kumar K.V945537d2016-04-29 23:25:45 +100059 new_pte = old_pte | H_PAGE_BUSY | _PAGE_ACCESSED;
Aneesh Kumar K.Vc7d54842016-04-29 23:25:30 +100060 if (access & _PAGE_WRITE)
Benjamin Herrenschmidt171aa2c2010-07-23 09:02:27 +100061 new_pte |= _PAGE_DIRTY;
Michael Ellerman3910a7f2016-04-29 23:25:27 +100062 } while(!pte_xchg(ptep, __pte(old_pte), __pte(new_pte)));
63
Aneesh Kumar K.Vc6a3c492015-12-01 09:06:50 +053064 rflags = htab_convert_pte_flags(new_pte);
David Gibson883a3e52009-10-26 19:24:31 +000065
David Gibson883a3e52009-10-26 19:24:31 +000066 sz = ((1UL) << shift);
67 if (!cpu_has_feature(CPU_FTR_COHERENT_ICACHE))
68 /* No CPU has hugepages but lacks no execute, so we
69 * don't need to worry about that case */
David Gibson0895ecd2009-10-26 19:24:31 +000070 rflags = hash_page_do_lazy_icache(rflags, __pte(old_pte), trap);
David Gibson883a3e52009-10-26 19:24:31 +000071
72 /* Check if pte already has an hpte (case 2) */
Aneesh Kumar K.V945537d2016-04-29 23:25:45 +100073 if (unlikely(old_pte & H_PAGE_HASHPTE)) {
David Gibson883a3e52009-10-26 19:24:31 +000074 /* There MIGHT be an HPTE for this pte */
75 unsigned long hash, slot;
76
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +000077 hash = hpt_hash(vpn, shift, ssize);
Aneesh Kumar K.V945537d2016-04-29 23:25:45 +100078 if (old_pte & H_PAGE_F_SECOND)
David Gibson883a3e52009-10-26 19:24:31 +000079 hash = ~hash;
80 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
Aneesh Kumar K.V945537d2016-04-29 23:25:45 +100081 slot += (old_pte & H_PAGE_F_GIX) >> H_PAGE_F_GIX_SHIFT;
David Gibson883a3e52009-10-26 19:24:31 +000082
Benjamin Herrenschmidt70257762016-07-05 15:03:58 +100083 if (mmu_hash_ops.hpte_updatepp(slot, rflags, vpn, mmu_psize,
84 mmu_psize, ssize, flags) == -1)
David Gibson883a3e52009-10-26 19:24:31 +000085 old_pte &= ~_PAGE_HPTEFLAGS;
86 }
87
Aneesh Kumar K.V945537d2016-04-29 23:25:45 +100088 if (likely(!(old_pte & H_PAGE_HASHPTE))) {
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +000089 unsigned long hash = hpt_hash(vpn, shift, ssize);
David Gibson883a3e52009-10-26 19:24:31 +000090
91 pa = pte_pfn(__pte(old_pte)) << PAGE_SHIFT;
92
David Gibson883a3e52009-10-26 19:24:31 +000093 /* clear HPTE slot informations in new PTE */
Aneesh Kumar K.V945537d2016-04-29 23:25:45 +100094 new_pte = (new_pte & ~_PAGE_HPTEFLAGS) | H_PAGE_HASHPTE;
Aneesh Kumar K.Vbf680d52015-12-01 09:06:45 +053095
Li Zhongb170bd32013-04-15 16:53:19 +000096 slot = hpte_insert_repeating(hash, vpn, pa, rflags, 0,
97 mmu_psize, ssize);
David Gibson883a3e52009-10-26 19:24:31 +000098
Anton Blanchardb1623e72010-07-14 19:31:48 +000099 /*
100 * Hypervisor failure. Restore old pte and return -1
101 * similar to __hash_page_*
102 */
103 if (unlikely(slot == -2)) {
104 *ptep = __pte(old_pte);
Benjamin Herrenschmidt4b8692c2010-07-23 10:31:13 +1000105 hash_failure_debug(ea, access, vsid, trap, ssize,
Aneesh Kumar K.Vd8139eb2013-04-28 09:37:37 +0000106 mmu_psize, mmu_psize, old_pte);
Benjamin Herrenschmidt171aa2c2010-07-23 09:02:27 +1000107 return -1;
Anton Blanchardb1623e72010-07-14 19:31:48 +0000108 }
David Gibson883a3e52009-10-26 19:24:31 +0000109
Aneesh Kumar K.V945537d2016-04-29 23:25:45 +1000110 new_pte |= (slot << H_PAGE_F_GIX_SHIFT) &
111 (H_PAGE_F_SECOND | H_PAGE_F_GIX);
David Gibson883a3e52009-10-26 19:24:31 +0000112 }
113
114 /*
115 * No need to use ldarx/stdcx here
116 */
Aneesh Kumar K.V945537d2016-04-29 23:25:45 +1000117 *ptep = __pte(new_pte & ~H_PAGE_BUSY);
Benjamin Herrenschmidt171aa2c2010-07-23 09:02:27 +1000118 return 0;
David Gibson883a3e52009-10-26 19:24:31 +0000119}