blob: fbd1acc3b8d91f88006c09c233402f5151f90866 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * native hashtable management.
3 *
4 * SMP scalability work:
5 * Copyright (C) 2001 Anton Blanchard <anton@au.ibm.com>, IBM
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +110012
13#undef DEBUG_LOW
14
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/spinlock.h>
16#include <linux/bitops.h>
Michael Ellermanbeacc6d2012-07-25 21:20:03 +000017#include <linux/of.h>
Nicholas Piggin4e287e62017-06-06 23:08:32 +100018#include <linux/processor.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/threads.h>
20#include <linux/smp.h>
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <asm/machdep.h>
23#include <asm/mmu.h>
24#include <asm/mmu_context.h>
25#include <asm/pgtable.h>
26#include <asm/tlbflush.h>
Balbir Singh04284912017-04-11 15:23:25 +100027#include <asm/trace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <asm/tlb.h>
29#include <asm/cputable.h>
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +110030#include <asm/udbg.h>
Luke Browning71bf08b2007-05-03 00:19:11 +100031#include <asm/kexec.h>
Milton Miller60dbf432009-04-29 20:58:01 +000032#include <asm/ppc-opcode.h>
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +110033
Michael Neulingec249dd2015-05-27 16:07:16 +100034#include <misc/cxl-base.h>
Ian Munsie4c6d9ac2014-10-08 19:55:00 +110035
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +110036#ifdef DEBUG_LOW
37#define DBG_LOW(fmt...) udbg_printf(fmt)
38#else
39#define DBG_LOW(fmt...)
40#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Anton Blanchard12f04f22013-09-23 12:04:36 +100042#ifdef __BIG_ENDIAN__
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#define HPTE_LOCK_BIT 3
Anton Blanchard12f04f22013-09-23 12:04:36 +100044#else
45#define HPTE_LOCK_BIT (56+3)
46#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Paul Mackerras9e368f22011-06-29 00:40:08 +000048DEFINE_RAW_SPINLOCK(native_tlbie_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +000050static inline void __tlbie(unsigned long vpn, int psize, int apsize, int ssize)
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +110051{
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +000052 unsigned long va;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +110053 unsigned int penc;
Aneesh Kumar K.Vde640952013-07-04 10:34:45 +053054 unsigned long sllp;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +110055
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +000056 /*
57 * We need 14 to 65 bits of va for a tlibe of 4K page
58 * With vpn we ignore the lower VPN_SHIFT bits already.
59 * And top two bits are already ignored because we can
Michael Ellerman027dfac2016-06-01 16:34:37 +100060 * only accomodate 76 bits in a 64 bit vpn with a VPN_SHIFT
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +000061 * of 12.
62 */
63 va = vpn << VPN_SHIFT;
64 /*
65 * clear top 16 bits of 64bit va, non SLS segment
66 * Older versions of the architecture (2.02 and earler) require the
67 * masking of the top 16 bits.
68 */
Aneesh Kumar K.Vaccfad72016-07-13 15:05:24 +053069 if (mmu_has_feature(MMU_FTR_TLBIE_CROP_VA))
70 va &= ~(0xffffULL << 48);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +110071
72 switch (psize) {
73 case MMU_PAGE_4K:
Aneesh Kumar K.V1f6aaac2013-04-28 09:37:39 +000074 /* clear out bits after (52) [0....52.....63] */
75 va &= ~((1ul << (64 - 52)) - 1);
Paul Mackerras1189be62007-10-11 20:37:10 +100076 va |= ssize << 8;
Aneesh Kumar K.V138ee7e2016-07-13 15:06:37 +053077 sllp = get_sllp_encoding(apsize);
Aneesh Kumar K.Vde640952013-07-04 10:34:45 +053078 va |= sllp << 5;
Michael Neulinga32e2522011-04-06 18:23:29 +000079 asm volatile(ASM_FTR_IFCLR("tlbie %0,0", PPC_TLBIE(%1,%0), %2)
Paul Mackerras969391c2011-06-29 00:26:11 +000080 : : "r" (va), "r"(0), "i" (CPU_FTR_ARCH_206)
Milton Miller60dbf432009-04-29 20:58:01 +000081 : "memory");
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +110082 break;
83 default:
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +000084 /* We need 14 to 14 + i bits of va */
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +000085 penc = mmu_psize_defs[psize].penc[apsize];
Aneesh Kumar K.V1f6aaac2013-04-28 09:37:39 +000086 va &= ~((1ul << mmu_psize_defs[apsize].shift) - 1);
Arnd Bergmann19242b22006-06-15 21:15:44 +100087 va |= penc << 12;
Paul Mackerras1189be62007-10-11 20:37:10 +100088 va |= ssize << 8;
Aneesh Kumar K.V29ef7a32014-04-21 10:37:36 +053089 /*
90 * AVAL bits:
91 * We don't need all the bits, but rest of the bits
92 * must be ignored by the processor.
93 * vpn cover upto 65 bits of va. (0...65) and we need
94 * 58..64 bits of va.
95 */
96 va |= (vpn & 0xfe); /* AVAL */
Milton Miller60dbf432009-04-29 20:58:01 +000097 va |= 1; /* L */
Michael Neulinga32e2522011-04-06 18:23:29 +000098 asm volatile(ASM_FTR_IFCLR("tlbie %0,1", PPC_TLBIE(%1,%0), %2)
Paul Mackerras969391c2011-06-29 00:26:11 +000099 : : "r" (va), "r"(0), "i" (CPU_FTR_ARCH_206)
Milton Miller60dbf432009-04-29 20:58:01 +0000100 : "memory");
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100101 break;
102 }
Balbir Singh04284912017-04-11 15:23:25 +1000103 trace_tlbie(0, 0, va, 0, 0, 0, 0);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100104}
105
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000106static inline void __tlbiel(unsigned long vpn, int psize, int apsize, int ssize)
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100107{
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000108 unsigned long va;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100109 unsigned int penc;
Aneesh Kumar K.Vde640952013-07-04 10:34:45 +0530110 unsigned long sllp;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100111
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000112 /* VPN_SHIFT can be atmost 12 */
113 va = vpn << VPN_SHIFT;
114 /*
115 * clear top 16 bits of 64 bit va, non SLS segment
116 * Older versions of the architecture (2.02 and earler) require the
117 * masking of the top 16 bits.
118 */
Aneesh Kumar K.Vaccfad72016-07-13 15:05:24 +0530119 if (mmu_has_feature(MMU_FTR_TLBIE_CROP_VA))
120 va &= ~(0xffffULL << 48);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100121
122 switch (psize) {
123 case MMU_PAGE_4K:
Aneesh Kumar K.V1f6aaac2013-04-28 09:37:39 +0000124 /* clear out bits after(52) [0....52.....63] */
125 va &= ~((1ul << (64 - 52)) - 1);
Paul Mackerras1189be62007-10-11 20:37:10 +1000126 va |= ssize << 8;
Aneesh Kumar K.V138ee7e2016-07-13 15:06:37 +0530127 sllp = get_sllp_encoding(apsize);
Aneesh Kumar K.Vde640952013-07-04 10:34:45 +0530128 va |= sllp << 5;
Balbir Singhf923efb2016-09-28 17:25:52 +1000129 asm volatile(ASM_FTR_IFSET("tlbiel %0", "tlbiel %0,0", %1)
130 : : "r" (va), "i" (CPU_FTR_ARCH_206)
131 : "memory");
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100132 break;
133 default:
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000134 /* We need 14 to 14 + i bits of va */
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000135 penc = mmu_psize_defs[psize].penc[apsize];
Aneesh Kumar K.V1f6aaac2013-04-28 09:37:39 +0000136 va &= ~((1ul << mmu_psize_defs[apsize].shift) - 1);
Arnd Bergmann19242b22006-06-15 21:15:44 +1000137 va |= penc << 12;
Paul Mackerras1189be62007-10-11 20:37:10 +1000138 va |= ssize << 8;
Aneesh Kumar K.V29ef7a32014-04-21 10:37:36 +0530139 /*
140 * AVAL bits:
141 * We don't need all the bits, but rest of the bits
142 * must be ignored by the processor.
143 * vpn cover upto 65 bits of va. (0...65) and we need
144 * 58..64 bits of va.
145 */
146 va |= (vpn & 0xfe);
Milton Miller60dbf432009-04-29 20:58:01 +0000147 va |= 1; /* L */
Balbir Singhf923efb2016-09-28 17:25:52 +1000148 asm volatile(ASM_FTR_IFSET("tlbiel %0", "tlbiel %0,1", %1)
149 : : "r" (va), "i" (CPU_FTR_ARCH_206)
150 : "memory");
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100151 break;
152 }
Balbir Singh04284912017-04-11 15:23:25 +1000153 trace_tlbie(0, 1, va, 0, 0, 0, 0);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100154
155}
156
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000157static inline void tlbie(unsigned long vpn, int psize, int apsize,
158 int ssize, int local)
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100159{
Ian Munsie4c6d9ac2014-10-08 19:55:00 +1100160 unsigned int use_local;
Matt Evans44ae3ab2011-04-06 19:48:50 +0000161 int lock_tlbie = !mmu_has_feature(MMU_FTR_LOCKLESS_TLBIE);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100162
Ian Munsie4c6d9ac2014-10-08 19:55:00 +1100163 use_local = local && mmu_has_feature(MMU_FTR_TLBIEL) && !cxl_ctx_in_use();
164
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100165 if (use_local)
166 use_local = mmu_psize_defs[psize].tlbiel;
167 if (lock_tlbie && !use_local)
Thomas Gleixner6b9c9b82010-02-18 02:22:35 +0000168 raw_spin_lock(&native_tlbie_lock);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100169 asm volatile("ptesync": : :"memory");
170 if (use_local) {
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000171 __tlbiel(vpn, psize, apsize, ssize);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100172 asm volatile("ptesync": : :"memory");
173 } else {
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000174 __tlbie(vpn, psize, apsize, ssize);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100175 asm volatile("eieio; tlbsync; ptesync": : :"memory");
176 }
177 if (lock_tlbie && !use_local)
Thomas Gleixner6b9c9b82010-02-18 02:22:35 +0000178 raw_spin_unlock(&native_tlbie_lock);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100179}
180
David Gibson8e561e72007-06-13 14:52:56 +1000181static inline void native_lock_hpte(struct hash_pte *hptep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
Anton Blanchard12f04f22013-09-23 12:04:36 +1000183 unsigned long *word = (unsigned long *)&hptep->v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185 while (1) {
Anton Blanchard66d99b82010-02-10 01:03:06 +0000186 if (!test_and_set_bit_lock(HPTE_LOCK_BIT, word))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 break;
Nicholas Piggin4e287e62017-06-06 23:08:32 +1000188 spin_begin();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 while(test_bit(HPTE_LOCK_BIT, word))
Nicholas Piggin4e287e62017-06-06 23:08:32 +1000190 spin_cpu_relax();
191 spin_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 }
193}
194
David Gibson8e561e72007-06-13 14:52:56 +1000195static inline void native_unlock_hpte(struct hash_pte *hptep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
Anton Blanchard12f04f22013-09-23 12:04:36 +1000197 unsigned long *word = (unsigned long *)&hptep->v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Anton Blanchard66d99b82010-02-10 01:03:06 +0000199 clear_bit_unlock(HPTE_LOCK_BIT, word);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
201
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000202static long native_hpte_insert(unsigned long hpte_group, unsigned long vpn,
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100203 unsigned long pa, unsigned long rflags,
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000204 unsigned long vflags, int psize, int apsize, int ssize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
David Gibson8e561e72007-06-13 14:52:56 +1000206 struct hash_pte *hptep = htab_address + hpte_group;
David Gibson96e28442005-07-13 01:11:42 -0700207 unsigned long hpte_v, hpte_r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 int i;
209
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100210 if (!(vflags & HPTE_V_BOLTED)) {
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000211 DBG_LOW(" insert(group=%lx, vpn=%016lx, pa=%016lx,"
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100212 " rflags=%lx, vflags=%lx, psize=%d)\n",
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000213 hpte_group, vpn, pa, rflags, vflags, psize);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100214 }
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 for (i = 0; i < HPTES_PER_GROUP; i++) {
Anton Blanchard12f04f22013-09-23 12:04:36 +1000217 if (! (be64_to_cpu(hptep->v) & HPTE_V_VALID)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 /* retry with lock held */
219 native_lock_hpte(hptep);
Anton Blanchard12f04f22013-09-23 12:04:36 +1000220 if (! (be64_to_cpu(hptep->v) & HPTE_V_VALID))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 break;
222 native_unlock_hpte(hptep);
223 }
224
225 hptep++;
226 }
227
228 if (i == HPTES_PER_GROUP)
229 return -1;
230
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000231 hpte_v = hpte_encode_v(vpn, psize, apsize, ssize) | vflags | HPTE_V_VALID;
Paul Mackerras6b243fc2016-11-11 16:55:03 +1100232 hpte_r = hpte_encode_r(pa, psize, apsize) | rflags;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100233
234 if (!(vflags & HPTE_V_BOLTED)) {
235 DBG_LOW(" i=%x hpte_v=%016lx, hpte_r=%016lx\n",
236 i, hpte_v, hpte_r);
237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Paul Mackerras6b243fc2016-11-11 16:55:03 +1100239 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
240 hpte_r = hpte_old_to_new_r(hpte_v, hpte_r);
241 hpte_v = hpte_old_to_new_v(hpte_v);
242 }
243
Anton Blanchard12f04f22013-09-23 12:04:36 +1000244 hptep->r = cpu_to_be64(hpte_r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 /* Guarantee the second dword is visible before the valid bit */
Kumar Gala74a0ba62007-07-09 23:49:09 -0500246 eieio();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 /*
248 * Now set the first dword including the valid bit
249 * NOTE: this also unlocks the hpte
250 */
Anton Blanchard12f04f22013-09-23 12:04:36 +1000251 hptep->v = cpu_to_be64(hpte_v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 __asm__ __volatile__ ("ptesync" : : : "memory");
254
David Gibson96e28442005-07-13 01:11:42 -0700255 return i | (!!(vflags & HPTE_V_SECONDARY) << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256}
257
258static long native_hpte_remove(unsigned long hpte_group)
259{
David Gibson8e561e72007-06-13 14:52:56 +1000260 struct hash_pte *hptep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 int i;
262 int slot_offset;
David Gibson96e28442005-07-13 01:11:42 -0700263 unsigned long hpte_v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100265 DBG_LOW(" remove(group=%lx)\n", hpte_group);
266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 /* pick a random entry to start at */
268 slot_offset = mftb() & 0x7;
269
270 for (i = 0; i < HPTES_PER_GROUP; i++) {
271 hptep = htab_address + hpte_group + slot_offset;
Anton Blanchard12f04f22013-09-23 12:04:36 +1000272 hpte_v = be64_to_cpu(hptep->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
David Gibson96e28442005-07-13 01:11:42 -0700274 if ((hpte_v & HPTE_V_VALID) && !(hpte_v & HPTE_V_BOLTED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 /* retry with lock held */
276 native_lock_hpte(hptep);
Anton Blanchard12f04f22013-09-23 12:04:36 +1000277 hpte_v = be64_to_cpu(hptep->v);
David Gibson96e28442005-07-13 01:11:42 -0700278 if ((hpte_v & HPTE_V_VALID)
279 && !(hpte_v & HPTE_V_BOLTED))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 break;
281 native_unlock_hpte(hptep);
282 }
283
284 slot_offset++;
285 slot_offset &= 0x7;
286 }
287
288 if (i == HPTES_PER_GROUP)
289 return -1;
290
291 /* Invalidate the hpte. NOTE: this also unlocks it */
David Gibson96e28442005-07-13 01:11:42 -0700292 hptep->v = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294 return i;
295}
296
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100297static long native_hpte_updatepp(unsigned long slot, unsigned long newpp,
Aneesh Kumar K.Vdb3d8532013-06-20 14:30:13 +0530298 unsigned long vpn, int bpsize,
Aneesh Kumar K.Vaefa5682014-12-04 11:00:14 +0530299 int apsize, int ssize, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
David Gibson8e561e72007-06-13 14:52:56 +1000301 struct hash_pte *hptep = htab_address + slot;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100302 unsigned long hpte_v, want_v;
Aneesh Kumar K.Vaefa5682014-12-04 11:00:14 +0530303 int ret = 0, local = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Aneesh Kumar K.Vdb3d8532013-06-20 14:30:13 +0530305 want_v = hpte_encode_avpn(vpn, bpsize, ssize);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100306
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000307 DBG_LOW(" update(vpn=%016lx, avpnv=%016lx, group=%lx, newpp=%lx)",
308 vpn, want_v & HPTE_V_AVPN, slot, newpp);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100309
Anton Blanchard12f04f22013-09-23 12:04:36 +1000310 hpte_v = be64_to_cpu(hptep->v);
Paul Mackerras6b243fc2016-11-11 16:55:03 +1100311 if (cpu_has_feature(CPU_FTR_ARCH_300))
312 hpte_v = hpte_new_to_old_v(hpte_v, be64_to_cpu(hptep->r));
Aneesh Kumar K.V0608d692013-05-31 01:03:24 +0000313 /*
314 * We need to invalidate the TLB always because hpte_remove doesn't do
315 * a tlb invalidate. If a hash bucket gets full, we "evict" a more/less
316 * random entry from it. When we do that we don't invalidate the TLB
317 * (hpte_remove) because we assume the old translation is still
318 * technically "valid".
319 */
Aneesh Kumar K.Vdb3d8532013-06-20 14:30:13 +0530320 if (!HPTE_V_COMPARE(hpte_v, want_v) || !(hpte_v & HPTE_V_VALID)) {
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100321 DBG_LOW(" -> miss\n");
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100322 ret = -1;
323 } else {
Aneesh Kumar K.V0ec26982014-11-03 20:21:34 +0530324 native_lock_hpte(hptep);
325 /* recheck with locks held */
326 hpte_v = be64_to_cpu(hptep->v);
Paul Mackerras6b243fc2016-11-11 16:55:03 +1100327 if (cpu_has_feature(CPU_FTR_ARCH_300))
328 hpte_v = hpte_new_to_old_v(hpte_v, be64_to_cpu(hptep->r));
Aneesh Kumar K.V0ec26982014-11-03 20:21:34 +0530329 if (unlikely(!HPTE_V_COMPARE(hpte_v, want_v) ||
330 !(hpte_v & HPTE_V_VALID))) {
331 ret = -1;
332 } else {
333 DBG_LOW(" -> hit\n");
334 /* Update the HPTE */
335 hptep->r = cpu_to_be64((be64_to_cpu(hptep->r) &
Aneesh Kumar K.V8550e2f2016-06-08 19:55:55 +0530336 ~(HPTE_R_PPP | HPTE_R_N)) |
337 (newpp & (HPTE_R_PPP | HPTE_R_N |
Aneesh Kumar K.V0ec26982014-11-03 20:21:34 +0530338 HPTE_R_C)));
339 }
340 native_unlock_hpte(hptep);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100341 }
Aneesh Kumar K.Vaefa5682014-12-04 11:00:14 +0530342
343 if (flags & HPTE_LOCAL_UPDATE)
344 local = 1;
345 /*
346 * Ensure it is out of the tlb too if it is not a nohpte fault
347 */
348 if (!(flags & HPTE_NOHPTE_UPDATE))
349 tlbie(vpn, bpsize, apsize, ssize, local);
350
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100351 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352}
353
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000354static long native_hpte_find(unsigned long vpn, int psize, int ssize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
David Gibson8e561e72007-06-13 14:52:56 +1000356 struct hash_pte *hptep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 unsigned long hash;
Paul Mackerras1189be62007-10-11 20:37:10 +1000358 unsigned long i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 long slot;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100360 unsigned long want_v, hpte_v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000362 hash = hpt_hash(vpn, mmu_psize_defs[psize].shift, ssize);
Aneesh Kumar K.V74f227b2013-04-28 09:37:34 +0000363 want_v = hpte_encode_avpn(vpn, psize, ssize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Paul Mackerras1189be62007-10-11 20:37:10 +1000365 /* Bolted mappings are only ever in the primary group */
366 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
367 for (i = 0; i < HPTES_PER_GROUP; i++) {
368 hptep = htab_address + slot;
Anton Blanchard12f04f22013-09-23 12:04:36 +1000369 hpte_v = be64_to_cpu(hptep->v);
Paul Mackerras6b243fc2016-11-11 16:55:03 +1100370 if (cpu_has_feature(CPU_FTR_ARCH_300))
371 hpte_v = hpte_new_to_old_v(hpte_v, be64_to_cpu(hptep->r));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Paul Mackerras1189be62007-10-11 20:37:10 +1000373 if (HPTE_V_COMPARE(hpte_v, want_v) && (hpte_v & HPTE_V_VALID))
374 /* HPTE matches */
375 return slot;
376 ++slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 }
378
379 return -1;
380}
381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382/*
383 * Update the page protection bits. Intended to be used to create
384 * guard pages for kernel data structures on pages which are bolted
385 * in the HPT. Assumes pages being operated on will not be stolen.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 *
387 * No need to lock here because we should be the only user.
388 */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100389static void native_hpte_updateboltedpp(unsigned long newpp, unsigned long ea,
Paul Mackerras1189be62007-10-11 20:37:10 +1000390 int psize, int ssize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000392 unsigned long vpn;
393 unsigned long vsid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 long slot;
David Gibson8e561e72007-06-13 14:52:56 +1000395 struct hash_pte *hptep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Paul Mackerras1189be62007-10-11 20:37:10 +1000397 vsid = get_kernel_vsid(ea, ssize);
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000398 vpn = hpt_vpn(ea, vsid, ssize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000400 slot = native_hpte_find(vpn, psize, ssize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 if (slot == -1)
402 panic("could not find page to bolt\n");
403 hptep = htab_address + slot;
404
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100405 /* Update the HPTE */
Anton Blanchard12f04f22013-09-23 12:04:36 +1000406 hptep->r = cpu_to_be64((be64_to_cpu(hptep->r) &
Aneesh Kumar K.V8550e2f2016-06-08 19:55:55 +0530407 ~(HPTE_R_PPP | HPTE_R_N)) |
408 (newpp & (HPTE_R_PPP | HPTE_R_N)));
Aneesh Kumar K.Vdb3d8532013-06-20 14:30:13 +0530409 /*
410 * Ensure it is out of the tlb too. Bolted entries base and
411 * actual page size will be same.
412 */
413 tlbie(vpn, psize, psize, ssize, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414}
415
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000416static void native_hpte_invalidate(unsigned long slot, unsigned long vpn,
Aneesh Kumar K.Vdb3d8532013-06-20 14:30:13 +0530417 int bpsize, int apsize, int ssize, int local)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
David Gibson8e561e72007-06-13 14:52:56 +1000419 struct hash_pte *hptep = htab_address + slot;
David Gibson96e28442005-07-13 01:11:42 -0700420 unsigned long hpte_v;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100421 unsigned long want_v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
424 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000426 DBG_LOW(" invalidate(vpn=%016lx, hash: %lx)\n", vpn, slot);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100427
Aneesh Kumar K.Vdb3d8532013-06-20 14:30:13 +0530428 want_v = hpte_encode_avpn(vpn, bpsize, ssize);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100429 native_lock_hpte(hptep);
Anton Blanchard12f04f22013-09-23 12:04:36 +1000430 hpte_v = be64_to_cpu(hptep->v);
Paul Mackerras6b243fc2016-11-11 16:55:03 +1100431 if (cpu_has_feature(CPU_FTR_ARCH_300))
432 hpte_v = hpte_new_to_old_v(hpte_v, be64_to_cpu(hptep->r));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Aneesh Kumar K.V0608d692013-05-31 01:03:24 +0000434 /*
435 * We need to invalidate the TLB always because hpte_remove doesn't do
436 * a tlb invalidate. If a hash bucket gets full, we "evict" a more/less
437 * random entry from it. When we do that we don't invalidate the TLB
438 * (hpte_remove) because we assume the old translation is still
439 * technically "valid".
440 */
Aneesh Kumar K.Vdb3d8532013-06-20 14:30:13 +0530441 if (!HPTE_V_COMPARE(hpte_v, want_v) || !(hpte_v & HPTE_V_VALID))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 native_unlock_hpte(hptep);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100443 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 /* Invalidate the hpte. NOTE: this also unlocks it */
David Gibson96e28442005-07-13 01:11:42 -0700445 hptep->v = 0;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100446
447 /* Invalidate the TLB */
Aneesh Kumar K.Vdb3d8532013-06-20 14:30:13 +0530448 tlbie(vpn, bpsize, apsize, ssize, local);
449
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100450 local_irq_restore(flags);
451}
452
Aneesh Kumar K.Ve34aa032015-12-01 09:06:53 +0530453#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Aneesh Kumar K.Vfa1f8ae2014-08-13 12:31:58 +0530454static void native_hugepage_invalidate(unsigned long vsid,
455 unsigned long addr,
Aneesh Kumar K.V1a527282013-06-20 14:30:27 +0530456 unsigned char *hpte_slot_array,
Aneesh Kumar K.Vd557b092014-11-02 21:15:28 +0530457 int psize, int ssize, int local)
Aneesh Kumar K.V1a527282013-06-20 14:30:27 +0530458{
Aneesh Kumar K.V969b7b22014-08-13 12:32:01 +0530459 int i;
Aneesh Kumar K.V1a527282013-06-20 14:30:27 +0530460 struct hash_pte *hptep;
461 int actual_psize = MMU_PAGE_16M;
462 unsigned int max_hpte_count, valid;
463 unsigned long flags, s_addr = addr;
464 unsigned long hpte_v, want_v, shift;
Aneesh Kumar K.Vfa1f8ae2014-08-13 12:31:58 +0530465 unsigned long hidx, vpn = 0, hash, slot;
Aneesh Kumar K.V1a527282013-06-20 14:30:27 +0530466
467 shift = mmu_psize_defs[psize].shift;
468 max_hpte_count = 1U << (PMD_SHIFT - shift);
469
470 local_irq_save(flags);
471 for (i = 0; i < max_hpte_count; i++) {
472 valid = hpte_valid(hpte_slot_array, i);
473 if (!valid)
474 continue;
475 hidx = hpte_hash_index(hpte_slot_array, i);
476
477 /* get the vpn */
478 addr = s_addr + (i * (1ul << shift));
Aneesh Kumar K.V1a527282013-06-20 14:30:27 +0530479 vpn = hpt_vpn(addr, vsid, ssize);
480 hash = hpt_hash(vpn, shift, ssize);
481 if (hidx & _PTEIDX_SECONDARY)
482 hash = ~hash;
483
484 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
485 slot += hidx & _PTEIDX_GROUP_IX;
486
487 hptep = htab_address + slot;
488 want_v = hpte_encode_avpn(vpn, psize, ssize);
489 native_lock_hpte(hptep);
Anton Blanchard12f04f22013-09-23 12:04:36 +1000490 hpte_v = be64_to_cpu(hptep->v);
Paul Mackerras6b243fc2016-11-11 16:55:03 +1100491 if (cpu_has_feature(CPU_FTR_ARCH_300))
492 hpte_v = hpte_new_to_old_v(hpte_v, be64_to_cpu(hptep->r));
Aneesh Kumar K.V1a527282013-06-20 14:30:27 +0530493
494 /* Even if we miss, we need to invalidate the TLB */
495 if (!HPTE_V_COMPARE(hpte_v, want_v) || !(hpte_v & HPTE_V_VALID))
496 native_unlock_hpte(hptep);
497 else
498 /* Invalidate the hpte. NOTE: this also unlocks it */
499 hptep->v = 0;
Aneesh Kumar K.V969b7b22014-08-13 12:32:01 +0530500 /*
501 * We need to do tlb invalidate for all the address, tlbie
502 * instruction compares entry_VA in tlb with the VA specified
503 * here
504 */
Aneesh Kumar K.Vd557b092014-11-02 21:15:28 +0530505 tlbie(vpn, psize, actual_psize, ssize, local);
Aneesh Kumar K.V1a527282013-06-20 14:30:27 +0530506 }
Aneesh Kumar K.V1a527282013-06-20 14:30:27 +0530507 local_irq_restore(flags);
508}
Aneesh Kumar K.Ve34aa032015-12-01 09:06:53 +0530509#else
510static void native_hugepage_invalidate(unsigned long vsid,
511 unsigned long addr,
512 unsigned char *hpte_slot_array,
513 int psize, int ssize, int local)
514{
515 WARN(1, "%s called without THP support\n", __func__);
516}
517#endif
Aneesh Kumar K.V1a527282013-06-20 14:30:27 +0530518
David Gibson8e561e72007-06-13 14:52:56 +1000519static void hpte_decode(struct hash_pte *hpte, unsigned long slot,
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000520 int *psize, int *apsize, int *ssize, unsigned long *vpn)
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100521{
Aneesh Kumar K.Vdcda2872012-09-10 02:52:49 +0000522 unsigned long avpn, pteg, vpi;
Anton Blanchard12f04f22013-09-23 12:04:36 +1000523 unsigned long hpte_v = be64_to_cpu(hpte->v);
524 unsigned long hpte_r = be64_to_cpu(hpte->r);
Aneesh Kumar K.Vdcda2872012-09-10 02:52:49 +0000525 unsigned long vsid, seg_off;
Aneesh Kumar K.V7e74c392013-04-28 09:37:36 +0000526 int size, a_size, shift;
527 /* Look at the 8 bit LP value */
Anton Blanchard12f04f22013-09-23 12:04:36 +1000528 unsigned int lp = (hpte_r >> LP_SHIFT) & ((1 << LP_BITS) - 1);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100529
Paul Mackerras6b243fc2016-11-11 16:55:03 +1100530 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
531 hpte_v = hpte_new_to_old_v(hpte_v, hpte_r);
532 hpte_r = hpte_new_to_old_r(hpte_r);
533 }
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000534 if (!(hpte_v & HPTE_V_LARGE)) {
535 size = MMU_PAGE_4K;
536 a_size = MMU_PAGE_4K;
537 } else {
Paul Mackerras0eeede02016-09-02 17:20:43 +1000538 size = hpte_page_sizes[lp] & 0xf;
539 a_size = hpte_page_sizes[lp] >> 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 }
Paul Mackerras2454c7e2007-05-10 15:28:44 +1000541 /* This works for all page sizes, and for 256M and 1T segments */
Paul Mackerras6b243fc2016-11-11 16:55:03 +1100542 *ssize = hpte_v >> HPTE_V_SSIZE_SHIFT;
Aneesh Kumar K.Vdcda2872012-09-10 02:52:49 +0000543 shift = mmu_psize_defs[size].shift;
544
545 avpn = (HPTE_V_AVPN_VAL(hpte_v) & ~mmu_psize_defs[size].avpnm);
546 pteg = slot / HPTES_PER_GROUP;
547 if (hpte_v & HPTE_V_SECONDARY)
548 pteg = ~pteg;
549
550 switch (*ssize) {
551 case MMU_SEGSIZE_256M:
552 /* We only have 28 - 23 bits of seg_off in avpn */
553 seg_off = (avpn & 0x1f) << 23;
554 vsid = avpn >> 5;
555 /* We can find more bits from the pteg value */
556 if (shift < 23) {
557 vpi = (vsid ^ pteg) & htab_hash_mask;
558 seg_off |= vpi << shift;
559 }
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000560 *vpn = vsid << (SID_SHIFT - VPN_SHIFT) | seg_off >> VPN_SHIFT;
Aneesh Kumar K.V83383b72013-07-03 13:50:03 +0530561 break;
Aneesh Kumar K.Vdcda2872012-09-10 02:52:49 +0000562 case MMU_SEGSIZE_1T:
563 /* We only have 40 - 23 bits of seg_off in avpn */
564 seg_off = (avpn & 0x1ffff) << 23;
565 vsid = avpn >> 17;
566 if (shift < 23) {
567 vpi = (vsid ^ (vsid << 25) ^ pteg) & htab_hash_mask;
568 seg_off |= vpi << shift;
569 }
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000570 *vpn = vsid << (SID_SHIFT_1T - VPN_SHIFT) | seg_off >> VPN_SHIFT;
Aneesh Kumar K.V83383b72013-07-03 13:50:03 +0530571 break;
Aneesh Kumar K.Vdcda2872012-09-10 02:52:49 +0000572 default:
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000573 *vpn = size = 0;
Aneesh Kumar K.Vdcda2872012-09-10 02:52:49 +0000574 }
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000575 *psize = size;
576 *apsize = a_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577}
578
R Sharadaf4c82d52005-06-25 14:58:08 -0700579/*
580 * clear all mappings on kexec. All cpus are in real mode (or they will
581 * be when they isi), and we are the only one left. We rely on our kernel
582 * mapping being 0xC0's and the hardware ignoring those two real bits.
583 *
Cyril Burfdf880a2015-10-08 11:04:26 +1100584 * This must be called with interrupts disabled.
585 *
586 * Taking the native_tlbie_lock is unsafe here due to the possibility of
587 * lockdep being on. On pre POWER5 hardware, not taking the lock could
588 * cause deadlock. POWER5 and newer not taking the lock is fine. This only
589 * gets called during boot before secondary CPUs have come up and during
590 * crashdump and all bets are off anyway.
591 *
R Sharadaf4c82d52005-06-25 14:58:08 -0700592 * TODO: add batching support when enabled. remember, no dynamic memory here,
Michael Ellerman027dfac2016-06-01 16:34:37 +1000593 * although there is the control page available...
R Sharadaf4c82d52005-06-25 14:58:08 -0700594 */
595static void native_hpte_clear(void)
596{
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000597 unsigned long vpn = 0;
Cyril Burfdf880a2015-10-08 11:04:26 +1100598 unsigned long slot, slots;
David Gibson8e561e72007-06-13 14:52:56 +1000599 struct hash_pte *hptep = htab_address;
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000600 unsigned long hpte_v;
R Sharadaf4c82d52005-06-25 14:58:08 -0700601 unsigned long pteg_count;
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000602 int psize, apsize, ssize;
R Sharadaf4c82d52005-06-25 14:58:08 -0700603
604 pteg_count = htab_hash_mask + 1;
605
R Sharadaf4c82d52005-06-25 14:58:08 -0700606 slots = pteg_count * HPTES_PER_GROUP;
607
608 for (slot = 0; slot < slots; slot++, hptep++) {
609 /*
610 * we could lock the pte here, but we are the only cpu
611 * running, right? and for crash dump, we probably
612 * don't want to wait for a maybe bad cpu.
613 */
Anton Blanchard12f04f22013-09-23 12:04:36 +1000614 hpte_v = be64_to_cpu(hptep->v);
R Sharadaf4c82d52005-06-25 14:58:08 -0700615
R Sharada47f78a42006-02-22 21:43:08 +0530616 /*
Cyril Burfdf880a2015-10-08 11:04:26 +1100617 * Call __tlbie() here rather than tlbie() since we can't take the
618 * native_tlbie_lock.
R Sharada47f78a42006-02-22 21:43:08 +0530619 */
David Gibson96e28442005-07-13 01:11:42 -0700620 if (hpte_v & HPTE_V_VALID) {
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000621 hpte_decode(hptep, slot, &psize, &apsize, &ssize, &vpn);
David Gibson96e28442005-07-13 01:11:42 -0700622 hptep->v = 0;
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000623 __tlbie(vpn, psize, apsize, ssize);
R Sharadaf4c82d52005-06-25 14:58:08 -0700624 }
625 }
626
R Sharada47f78a42006-02-22 21:43:08 +0530627 asm volatile("eieio; tlbsync; ptesync":::"memory");
R Sharadaf4c82d52005-06-25 14:58:08 -0700628}
629
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100630/*
631 * Batched hash table flush, we batch the tlbie's to avoid taking/releasing
632 * the lock all the time
633 */
Benjamin Herrenschmidt61b1a942005-09-20 13:52:50 +1000634static void native_flush_hash_range(unsigned long number, int local)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000636 unsigned long vpn;
637 unsigned long hash, index, hidx, shift, slot;
David Gibson8e561e72007-06-13 14:52:56 +1000638 struct hash_pte *hptep;
David Gibson96e28442005-07-13 01:11:42 -0700639 unsigned long hpte_v;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100640 unsigned long want_v;
641 unsigned long flags;
642 real_pte_t pte;
Christoph Lameter69111ba2014-10-21 15:23:25 -0500643 struct ppc64_tlb_batch *batch = this_cpu_ptr(&ppc64_tlb_batch);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100644 unsigned long psize = batch->psize;
Paul Mackerras1189be62007-10-11 20:37:10 +1000645 int ssize = batch->ssize;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100646 int i;
Frederic Barrat88b1bf722017-03-29 19:19:42 +0200647 unsigned int use_local;
648
649 use_local = local && mmu_has_feature(MMU_FTR_TLBIEL) &&
650 mmu_psize_defs[psize].tlbiel && !cxl_ctx_in_use();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
652 local_irq_save(flags);
653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 for (i = 0; i < number; i++) {
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000655 vpn = batch->vpn[i];
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100656 pte = batch->pte[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000658 pte_iterate_hashed_subpages(pte, psize, vpn, index, shift) {
659 hash = hpt_hash(vpn, shift, ssize);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100660 hidx = __rpte_to_hidx(pte, index);
661 if (hidx & _PTEIDX_SECONDARY)
662 hash = ~hash;
663 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
664 slot += hidx & _PTEIDX_GROUP_IX;
665 hptep = htab_address + slot;
Aneesh Kumar K.V74f227b2013-04-28 09:37:34 +0000666 want_v = hpte_encode_avpn(vpn, psize, ssize);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100667 native_lock_hpte(hptep);
Anton Blanchard12f04f22013-09-23 12:04:36 +1000668 hpte_v = be64_to_cpu(hptep->v);
Paul Mackerras6b243fc2016-11-11 16:55:03 +1100669 if (cpu_has_feature(CPU_FTR_ARCH_300))
670 hpte_v = hpte_new_to_old_v(hpte_v,
671 be64_to_cpu(hptep->r));
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100672 if (!HPTE_V_COMPARE(hpte_v, want_v) ||
673 !(hpte_v & HPTE_V_VALID))
674 native_unlock_hpte(hptep);
675 else
676 hptep->v = 0;
677 } pte_iterate_hashed_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 }
679
Frederic Barrat88b1bf722017-03-29 19:19:42 +0200680 if (use_local) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 asm volatile("ptesync":::"memory");
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100682 for (i = 0; i < number; i++) {
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000683 vpn = batch->vpn[i];
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100684 pte = batch->pte[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000686 pte_iterate_hashed_subpages(pte, psize,
687 vpn, index, shift) {
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000688 __tlbiel(vpn, psize, psize, ssize);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100689 } pte_iterate_hashed_end();
690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 asm volatile("ptesync":::"memory");
692 } else {
Matt Evans44ae3ab2011-04-06 19:48:50 +0000693 int lock_tlbie = !mmu_has_feature(MMU_FTR_LOCKLESS_TLBIE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 if (lock_tlbie)
Thomas Gleixner6b9c9b82010-02-18 02:22:35 +0000696 raw_spin_lock(&native_tlbie_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698 asm volatile("ptesync":::"memory");
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100699 for (i = 0; i < number; i++) {
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000700 vpn = batch->vpn[i];
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100701 pte = batch->pte[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000703 pte_iterate_hashed_subpages(pte, psize,
704 vpn, index, shift) {
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000705 __tlbie(vpn, psize, psize, ssize);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100706 } pte_iterate_hashed_end();
707 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 asm volatile("eieio; tlbsync; ptesync":::"memory");
709
710 if (lock_tlbie)
Thomas Gleixner6b9c9b82010-02-18 02:22:35 +0000711 raw_spin_unlock(&native_tlbie_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 }
713
714 local_irq_restore(flags);
715}
716
Aneesh Kumar K.V83209bc2016-07-13 15:05:28 +0530717static int native_register_proc_table(unsigned long base, unsigned long page_size,
718 unsigned long table_size)
Aneesh Kumar K.V50de5962016-04-29 23:25:43 +1000719{
Aneesh Kumar K.V83209bc2016-07-13 15:05:28 +0530720 unsigned long patb1 = base << 25; /* VSID */
721
722 patb1 |= (page_size << 5); /* sllp */
723 patb1 |= table_size;
724
Aneesh Kumar K.V50de5962016-04-29 23:25:43 +1000725 partition_tb->patb1 = cpu_to_be64(patb1);
726 return 0;
727}
728
Michael Ellerman7d0daae2006-06-23 18:16:38 +1000729void __init hpte_init_native(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730{
Benjamin Herrenschmidt70257762016-07-05 15:03:58 +1000731 mmu_hash_ops.hpte_invalidate = native_hpte_invalidate;
732 mmu_hash_ops.hpte_updatepp = native_hpte_updatepp;
733 mmu_hash_ops.hpte_updateboltedpp = native_hpte_updateboltedpp;
734 mmu_hash_ops.hpte_insert = native_hpte_insert;
735 mmu_hash_ops.hpte_remove = native_hpte_remove;
736 mmu_hash_ops.hpte_clear_all = native_hpte_clear;
737 mmu_hash_ops.flush_hash_range = native_flush_hash_range;
738 mmu_hash_ops.hugepage_invalidate = native_hugepage_invalidate;
Aneesh Kumar K.V50de5962016-04-29 23:25:43 +1000739
740 if (cpu_has_feature(CPU_FTR_ARCH_300))
Michael Ellermaneea81482016-08-04 15:32:06 +1000741 register_process_table = native_register_proc_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742}