blob: 3848af167df9de4b183d7cb8472b5d20ce97d0a4 [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
Anton Blanchard1b644f52017-06-28 11:32:35 +1000416/*
417 * Remove a bolted kernel entry. Memory hotplug uses this.
418 *
419 * No need to lock here because we should be the only user.
420 */
421static int native_hpte_removebolted(unsigned long ea, int psize, int ssize)
422{
423 unsigned long vpn;
424 unsigned long vsid;
425 long slot;
426 struct hash_pte *hptep;
427
428 vsid = get_kernel_vsid(ea, ssize);
429 vpn = hpt_vpn(ea, vsid, ssize);
430
431 slot = native_hpte_find(vpn, psize, ssize);
432 if (slot == -1)
433 return -ENOENT;
434
435 hptep = htab_address + slot;
436
437 VM_WARN_ON(!(be64_to_cpu(hptep->v) & HPTE_V_BOLTED));
438
439 /* Invalidate the hpte */
440 hptep->v = 0;
441
442 /* Invalidate the TLB */
443 tlbie(vpn, psize, psize, ssize, 0);
444 return 0;
445}
446
447
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000448static void native_hpte_invalidate(unsigned long slot, unsigned long vpn,
Aneesh Kumar K.Vdb3d8532013-06-20 14:30:13 +0530449 int bpsize, int apsize, int ssize, int local)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
David Gibson8e561e72007-06-13 14:52:56 +1000451 struct hash_pte *hptep = htab_address + slot;
David Gibson96e28442005-07-13 01:11:42 -0700452 unsigned long hpte_v;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100453 unsigned long want_v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000458 DBG_LOW(" invalidate(vpn=%016lx, hash: %lx)\n", vpn, slot);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100459
Aneesh Kumar K.Vdb3d8532013-06-20 14:30:13 +0530460 want_v = hpte_encode_avpn(vpn, bpsize, ssize);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100461 native_lock_hpte(hptep);
Anton Blanchard12f04f22013-09-23 12:04:36 +1000462 hpte_v = be64_to_cpu(hptep->v);
Paul Mackerras6b243fc2016-11-11 16:55:03 +1100463 if (cpu_has_feature(CPU_FTR_ARCH_300))
464 hpte_v = hpte_new_to_old_v(hpte_v, be64_to_cpu(hptep->r));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Aneesh Kumar K.V0608d692013-05-31 01:03:24 +0000466 /*
467 * We need to invalidate the TLB always because hpte_remove doesn't do
468 * a tlb invalidate. If a hash bucket gets full, we "evict" a more/less
469 * random entry from it. When we do that we don't invalidate the TLB
470 * (hpte_remove) because we assume the old translation is still
471 * technically "valid".
472 */
Aneesh Kumar K.Vdb3d8532013-06-20 14:30:13 +0530473 if (!HPTE_V_COMPARE(hpte_v, want_v) || !(hpte_v & HPTE_V_VALID))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 native_unlock_hpte(hptep);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100475 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 /* Invalidate the hpte. NOTE: this also unlocks it */
David Gibson96e28442005-07-13 01:11:42 -0700477 hptep->v = 0;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100478
479 /* Invalidate the TLB */
Aneesh Kumar K.Vdb3d8532013-06-20 14:30:13 +0530480 tlbie(vpn, bpsize, apsize, ssize, local);
481
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100482 local_irq_restore(flags);
483}
484
Aneesh Kumar K.Ve34aa032015-12-01 09:06:53 +0530485#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Aneesh Kumar K.Vfa1f8ae2014-08-13 12:31:58 +0530486static void native_hugepage_invalidate(unsigned long vsid,
487 unsigned long addr,
Aneesh Kumar K.V1a527282013-06-20 14:30:27 +0530488 unsigned char *hpte_slot_array,
Aneesh Kumar K.Vd557b092014-11-02 21:15:28 +0530489 int psize, int ssize, int local)
Aneesh Kumar K.V1a527282013-06-20 14:30:27 +0530490{
Aneesh Kumar K.V969b7b22014-08-13 12:32:01 +0530491 int i;
Aneesh Kumar K.V1a527282013-06-20 14:30:27 +0530492 struct hash_pte *hptep;
493 int actual_psize = MMU_PAGE_16M;
494 unsigned int max_hpte_count, valid;
495 unsigned long flags, s_addr = addr;
496 unsigned long hpte_v, want_v, shift;
Aneesh Kumar K.Vfa1f8ae2014-08-13 12:31:58 +0530497 unsigned long hidx, vpn = 0, hash, slot;
Aneesh Kumar K.V1a527282013-06-20 14:30:27 +0530498
499 shift = mmu_psize_defs[psize].shift;
500 max_hpte_count = 1U << (PMD_SHIFT - shift);
501
502 local_irq_save(flags);
503 for (i = 0; i < max_hpte_count; i++) {
504 valid = hpte_valid(hpte_slot_array, i);
505 if (!valid)
506 continue;
507 hidx = hpte_hash_index(hpte_slot_array, i);
508
509 /* get the vpn */
510 addr = s_addr + (i * (1ul << shift));
Aneesh Kumar K.V1a527282013-06-20 14:30:27 +0530511 vpn = hpt_vpn(addr, vsid, ssize);
512 hash = hpt_hash(vpn, shift, ssize);
513 if (hidx & _PTEIDX_SECONDARY)
514 hash = ~hash;
515
516 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
517 slot += hidx & _PTEIDX_GROUP_IX;
518
519 hptep = htab_address + slot;
520 want_v = hpte_encode_avpn(vpn, psize, ssize);
521 native_lock_hpte(hptep);
Anton Blanchard12f04f22013-09-23 12:04:36 +1000522 hpte_v = be64_to_cpu(hptep->v);
Paul Mackerras6b243fc2016-11-11 16:55:03 +1100523 if (cpu_has_feature(CPU_FTR_ARCH_300))
524 hpte_v = hpte_new_to_old_v(hpte_v, be64_to_cpu(hptep->r));
Aneesh Kumar K.V1a527282013-06-20 14:30:27 +0530525
526 /* Even if we miss, we need to invalidate the TLB */
527 if (!HPTE_V_COMPARE(hpte_v, want_v) || !(hpte_v & HPTE_V_VALID))
528 native_unlock_hpte(hptep);
529 else
530 /* Invalidate the hpte. NOTE: this also unlocks it */
531 hptep->v = 0;
Aneesh Kumar K.V969b7b22014-08-13 12:32:01 +0530532 /*
533 * We need to do tlb invalidate for all the address, tlbie
534 * instruction compares entry_VA in tlb with the VA specified
535 * here
536 */
Aneesh Kumar K.Vd557b092014-11-02 21:15:28 +0530537 tlbie(vpn, psize, actual_psize, ssize, local);
Aneesh Kumar K.V1a527282013-06-20 14:30:27 +0530538 }
Aneesh Kumar K.V1a527282013-06-20 14:30:27 +0530539 local_irq_restore(flags);
540}
Aneesh Kumar K.Ve34aa032015-12-01 09:06:53 +0530541#else
542static void native_hugepage_invalidate(unsigned long vsid,
543 unsigned long addr,
544 unsigned char *hpte_slot_array,
545 int psize, int ssize, int local)
546{
547 WARN(1, "%s called without THP support\n", __func__);
548}
549#endif
Aneesh Kumar K.V1a527282013-06-20 14:30:27 +0530550
David Gibson8e561e72007-06-13 14:52:56 +1000551static void hpte_decode(struct hash_pte *hpte, unsigned long slot,
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000552 int *psize, int *apsize, int *ssize, unsigned long *vpn)
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100553{
Aneesh Kumar K.Vdcda2872012-09-10 02:52:49 +0000554 unsigned long avpn, pteg, vpi;
Anton Blanchard12f04f22013-09-23 12:04:36 +1000555 unsigned long hpte_v = be64_to_cpu(hpte->v);
556 unsigned long hpte_r = be64_to_cpu(hpte->r);
Aneesh Kumar K.Vdcda2872012-09-10 02:52:49 +0000557 unsigned long vsid, seg_off;
Aneesh Kumar K.V7e74c392013-04-28 09:37:36 +0000558 int size, a_size, shift;
559 /* Look at the 8 bit LP value */
Anton Blanchard12f04f22013-09-23 12:04:36 +1000560 unsigned int lp = (hpte_r >> LP_SHIFT) & ((1 << LP_BITS) - 1);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100561
Paul Mackerras6b243fc2016-11-11 16:55:03 +1100562 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
563 hpte_v = hpte_new_to_old_v(hpte_v, hpte_r);
564 hpte_r = hpte_new_to_old_r(hpte_r);
565 }
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000566 if (!(hpte_v & HPTE_V_LARGE)) {
567 size = MMU_PAGE_4K;
568 a_size = MMU_PAGE_4K;
569 } else {
Paul Mackerras0eeede02016-09-02 17:20:43 +1000570 size = hpte_page_sizes[lp] & 0xf;
571 a_size = hpte_page_sizes[lp] >> 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 }
Paul Mackerras2454c7e2007-05-10 15:28:44 +1000573 /* This works for all page sizes, and for 256M and 1T segments */
Paul Mackerras6b243fc2016-11-11 16:55:03 +1100574 *ssize = hpte_v >> HPTE_V_SSIZE_SHIFT;
Aneesh Kumar K.Vdcda2872012-09-10 02:52:49 +0000575 shift = mmu_psize_defs[size].shift;
576
577 avpn = (HPTE_V_AVPN_VAL(hpte_v) & ~mmu_psize_defs[size].avpnm);
578 pteg = slot / HPTES_PER_GROUP;
579 if (hpte_v & HPTE_V_SECONDARY)
580 pteg = ~pteg;
581
582 switch (*ssize) {
583 case MMU_SEGSIZE_256M:
584 /* We only have 28 - 23 bits of seg_off in avpn */
585 seg_off = (avpn & 0x1f) << 23;
586 vsid = avpn >> 5;
587 /* We can find more bits from the pteg value */
588 if (shift < 23) {
589 vpi = (vsid ^ pteg) & htab_hash_mask;
590 seg_off |= vpi << shift;
591 }
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000592 *vpn = vsid << (SID_SHIFT - VPN_SHIFT) | seg_off >> VPN_SHIFT;
Aneesh Kumar K.V83383b72013-07-03 13:50:03 +0530593 break;
Aneesh Kumar K.Vdcda2872012-09-10 02:52:49 +0000594 case MMU_SEGSIZE_1T:
595 /* We only have 40 - 23 bits of seg_off in avpn */
596 seg_off = (avpn & 0x1ffff) << 23;
597 vsid = avpn >> 17;
598 if (shift < 23) {
599 vpi = (vsid ^ (vsid << 25) ^ pteg) & htab_hash_mask;
600 seg_off |= vpi << shift;
601 }
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000602 *vpn = vsid << (SID_SHIFT_1T - VPN_SHIFT) | seg_off >> VPN_SHIFT;
Aneesh Kumar K.V83383b72013-07-03 13:50:03 +0530603 break;
Aneesh Kumar K.Vdcda2872012-09-10 02:52:49 +0000604 default:
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000605 *vpn = size = 0;
Aneesh Kumar K.Vdcda2872012-09-10 02:52:49 +0000606 }
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000607 *psize = size;
608 *apsize = a_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609}
610
R Sharadaf4c82d52005-06-25 14:58:08 -0700611/*
612 * clear all mappings on kexec. All cpus are in real mode (or they will
613 * be when they isi), and we are the only one left. We rely on our kernel
614 * mapping being 0xC0's and the hardware ignoring those two real bits.
615 *
Cyril Burfdf880a2015-10-08 11:04:26 +1100616 * This must be called with interrupts disabled.
617 *
618 * Taking the native_tlbie_lock is unsafe here due to the possibility of
619 * lockdep being on. On pre POWER5 hardware, not taking the lock could
620 * cause deadlock. POWER5 and newer not taking the lock is fine. This only
621 * gets called during boot before secondary CPUs have come up and during
622 * crashdump and all bets are off anyway.
623 *
R Sharadaf4c82d52005-06-25 14:58:08 -0700624 * TODO: add batching support when enabled. remember, no dynamic memory here,
Michael Ellerman027dfac2016-06-01 16:34:37 +1000625 * although there is the control page available...
R Sharadaf4c82d52005-06-25 14:58:08 -0700626 */
627static void native_hpte_clear(void)
628{
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000629 unsigned long vpn = 0;
Cyril Burfdf880a2015-10-08 11:04:26 +1100630 unsigned long slot, slots;
David Gibson8e561e72007-06-13 14:52:56 +1000631 struct hash_pte *hptep = htab_address;
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000632 unsigned long hpte_v;
R Sharadaf4c82d52005-06-25 14:58:08 -0700633 unsigned long pteg_count;
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000634 int psize, apsize, ssize;
R Sharadaf4c82d52005-06-25 14:58:08 -0700635
636 pteg_count = htab_hash_mask + 1;
637
R Sharadaf4c82d52005-06-25 14:58:08 -0700638 slots = pteg_count * HPTES_PER_GROUP;
639
640 for (slot = 0; slot < slots; slot++, hptep++) {
641 /*
642 * we could lock the pte here, but we are the only cpu
643 * running, right? and for crash dump, we probably
644 * don't want to wait for a maybe bad cpu.
645 */
Anton Blanchard12f04f22013-09-23 12:04:36 +1000646 hpte_v = be64_to_cpu(hptep->v);
R Sharadaf4c82d52005-06-25 14:58:08 -0700647
R Sharada47f78a42006-02-22 21:43:08 +0530648 /*
Cyril Burfdf880a2015-10-08 11:04:26 +1100649 * Call __tlbie() here rather than tlbie() since we can't take the
650 * native_tlbie_lock.
R Sharada47f78a42006-02-22 21:43:08 +0530651 */
David Gibson96e28442005-07-13 01:11:42 -0700652 if (hpte_v & HPTE_V_VALID) {
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000653 hpte_decode(hptep, slot, &psize, &apsize, &ssize, &vpn);
David Gibson96e28442005-07-13 01:11:42 -0700654 hptep->v = 0;
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000655 __tlbie(vpn, psize, apsize, ssize);
R Sharadaf4c82d52005-06-25 14:58:08 -0700656 }
657 }
658
R Sharada47f78a42006-02-22 21:43:08 +0530659 asm volatile("eieio; tlbsync; ptesync":::"memory");
R Sharadaf4c82d52005-06-25 14:58:08 -0700660}
661
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100662/*
663 * Batched hash table flush, we batch the tlbie's to avoid taking/releasing
664 * the lock all the time
665 */
Benjamin Herrenschmidt61b1a942005-09-20 13:52:50 +1000666static void native_flush_hash_range(unsigned long number, int local)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000668 unsigned long vpn;
669 unsigned long hash, index, hidx, shift, slot;
David Gibson8e561e72007-06-13 14:52:56 +1000670 struct hash_pte *hptep;
David Gibson96e28442005-07-13 01:11:42 -0700671 unsigned long hpte_v;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100672 unsigned long want_v;
673 unsigned long flags;
674 real_pte_t pte;
Christoph Lameter69111ba2014-10-21 15:23:25 -0500675 struct ppc64_tlb_batch *batch = this_cpu_ptr(&ppc64_tlb_batch);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100676 unsigned long psize = batch->psize;
Paul Mackerras1189be62007-10-11 20:37:10 +1000677 int ssize = batch->ssize;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100678 int i;
Frederic Barrat88b1bf722017-03-29 19:19:42 +0200679 unsigned int use_local;
680
681 use_local = local && mmu_has_feature(MMU_FTR_TLBIEL) &&
682 mmu_psize_defs[psize].tlbiel && !cxl_ctx_in_use();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
684 local_irq_save(flags);
685
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 for (i = 0; i < number; i++) {
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000687 vpn = batch->vpn[i];
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100688 pte = batch->pte[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000690 pte_iterate_hashed_subpages(pte, psize, vpn, index, shift) {
691 hash = hpt_hash(vpn, shift, ssize);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100692 hidx = __rpte_to_hidx(pte, index);
693 if (hidx & _PTEIDX_SECONDARY)
694 hash = ~hash;
695 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
696 slot += hidx & _PTEIDX_GROUP_IX;
697 hptep = htab_address + slot;
Aneesh Kumar K.V74f227b2013-04-28 09:37:34 +0000698 want_v = hpte_encode_avpn(vpn, psize, ssize);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100699 native_lock_hpte(hptep);
Anton Blanchard12f04f22013-09-23 12:04:36 +1000700 hpte_v = be64_to_cpu(hptep->v);
Paul Mackerras6b243fc2016-11-11 16:55:03 +1100701 if (cpu_has_feature(CPU_FTR_ARCH_300))
702 hpte_v = hpte_new_to_old_v(hpte_v,
703 be64_to_cpu(hptep->r));
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100704 if (!HPTE_V_COMPARE(hpte_v, want_v) ||
705 !(hpte_v & HPTE_V_VALID))
706 native_unlock_hpte(hptep);
707 else
708 hptep->v = 0;
709 } pte_iterate_hashed_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 }
711
Frederic Barrat88b1bf722017-03-29 19:19:42 +0200712 if (use_local) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 asm volatile("ptesync":::"memory");
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100714 for (i = 0; i < number; i++) {
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000715 vpn = batch->vpn[i];
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100716 pte = batch->pte[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000718 pte_iterate_hashed_subpages(pte, psize,
719 vpn, index, shift) {
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000720 __tlbiel(vpn, psize, psize, ssize);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100721 } pte_iterate_hashed_end();
722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 asm volatile("ptesync":::"memory");
724 } else {
Matt Evans44ae3ab2011-04-06 19:48:50 +0000725 int lock_tlbie = !mmu_has_feature(MMU_FTR_LOCKLESS_TLBIE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
727 if (lock_tlbie)
Thomas Gleixner6b9c9b82010-02-18 02:22:35 +0000728 raw_spin_lock(&native_tlbie_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
730 asm volatile("ptesync":::"memory");
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100731 for (i = 0; i < number; i++) {
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000732 vpn = batch->vpn[i];
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100733 pte = batch->pte[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000735 pte_iterate_hashed_subpages(pte, psize,
736 vpn, index, shift) {
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000737 __tlbie(vpn, psize, psize, ssize);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100738 } pte_iterate_hashed_end();
739 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 asm volatile("eieio; tlbsync; ptesync":::"memory");
741
742 if (lock_tlbie)
Thomas Gleixner6b9c9b82010-02-18 02:22:35 +0000743 raw_spin_unlock(&native_tlbie_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 }
745
746 local_irq_restore(flags);
747}
748
Aneesh Kumar K.V83209bc2016-07-13 15:05:28 +0530749static int native_register_proc_table(unsigned long base, unsigned long page_size,
750 unsigned long table_size)
Aneesh Kumar K.V50de5962016-04-29 23:25:43 +1000751{
Aneesh Kumar K.V83209bc2016-07-13 15:05:28 +0530752 unsigned long patb1 = base << 25; /* VSID */
753
754 patb1 |= (page_size << 5); /* sllp */
755 patb1 |= table_size;
756
Aneesh Kumar K.V50de5962016-04-29 23:25:43 +1000757 partition_tb->patb1 = cpu_to_be64(patb1);
758 return 0;
759}
760
Michael Ellerman7d0daae2006-06-23 18:16:38 +1000761void __init hpte_init_native(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
Benjamin Herrenschmidt70257762016-07-05 15:03:58 +1000763 mmu_hash_ops.hpte_invalidate = native_hpte_invalidate;
764 mmu_hash_ops.hpte_updatepp = native_hpte_updatepp;
765 mmu_hash_ops.hpte_updateboltedpp = native_hpte_updateboltedpp;
Anton Blanchard1b644f52017-06-28 11:32:35 +1000766 mmu_hash_ops.hpte_removebolted = native_hpte_removebolted;
Benjamin Herrenschmidt70257762016-07-05 15:03:58 +1000767 mmu_hash_ops.hpte_insert = native_hpte_insert;
768 mmu_hash_ops.hpte_remove = native_hpte_remove;
769 mmu_hash_ops.hpte_clear_all = native_hpte_clear;
770 mmu_hash_ops.flush_hash_range = native_flush_hash_range;
771 mmu_hash_ops.hugepage_invalidate = native_hugepage_invalidate;
Aneesh Kumar K.V50de5962016-04-29 23:25:43 +1000772
773 if (cpu_has_feature(CPU_FTR_ARCH_300))
Michael Ellermaneea81482016-08-04 15:32:06 +1000774 register_process_table = native_register_proc_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775}