blob: bdaac28193f7d4374e73b69e1361e96e0ef770f9 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/threads.h>
19#include <linux/smp.h>
20
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <asm/machdep.h>
22#include <asm/mmu.h>
23#include <asm/mmu_context.h>
24#include <asm/pgtable.h>
25#include <asm/tlbflush.h>
Balbir Singh04284912017-04-11 15:23:25 +100026#include <asm/trace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <asm/tlb.h>
28#include <asm/cputable.h>
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +110029#include <asm/udbg.h>
Luke Browning71bf08b2007-05-03 00:19:11 +100030#include <asm/kexec.h>
Milton Miller60dbf432009-04-29 20:58:01 +000031#include <asm/ppc-opcode.h>
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +110032
Michael Neulingec249dd2015-05-27 16:07:16 +100033#include <misc/cxl-base.h>
Ian Munsie4c6d9ac2014-10-08 19:55:00 +110034
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +110035#ifdef DEBUG_LOW
36#define DBG_LOW(fmt...) udbg_printf(fmt)
37#else
38#define DBG_LOW(fmt...)
39#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Anton Blanchard12f04f22013-09-23 12:04:36 +100041#ifdef __BIG_ENDIAN__
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#define HPTE_LOCK_BIT 3
Anton Blanchard12f04f22013-09-23 12:04:36 +100043#else
44#define HPTE_LOCK_BIT (56+3)
45#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Paul Mackerras9e368f22011-06-29 00:40:08 +000047DEFINE_RAW_SPINLOCK(native_tlbie_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +000049static inline void __tlbie(unsigned long vpn, int psize, int apsize, int ssize)
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +110050{
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +000051 unsigned long va;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +110052 unsigned int penc;
Aneesh Kumar K.Vde640952013-07-04 10:34:45 +053053 unsigned long sllp;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +110054
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +000055 /*
56 * We need 14 to 65 bits of va for a tlibe of 4K page
57 * With vpn we ignore the lower VPN_SHIFT bits already.
58 * And top two bits are already ignored because we can
Michael Ellerman027dfac2016-06-01 16:34:37 +100059 * only accomodate 76 bits in a 64 bit vpn with a VPN_SHIFT
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +000060 * of 12.
61 */
62 va = vpn << VPN_SHIFT;
63 /*
64 * clear top 16 bits of 64bit va, non SLS segment
65 * Older versions of the architecture (2.02 and earler) require the
66 * masking of the top 16 bits.
67 */
Aneesh Kumar K.Vaccfad72016-07-13 15:05:24 +053068 if (mmu_has_feature(MMU_FTR_TLBIE_CROP_VA))
69 va &= ~(0xffffULL << 48);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +110070
71 switch (psize) {
72 case MMU_PAGE_4K:
Aneesh Kumar K.V1f6aaac2013-04-28 09:37:39 +000073 /* clear out bits after (52) [0....52.....63] */
74 va &= ~((1ul << (64 - 52)) - 1);
Paul Mackerras1189be62007-10-11 20:37:10 +100075 va |= ssize << 8;
Aneesh Kumar K.V138ee7e2016-07-13 15:06:37 +053076 sllp = get_sllp_encoding(apsize);
Aneesh Kumar K.Vde640952013-07-04 10:34:45 +053077 va |= sllp << 5;
Michael Neulinga32e2522011-04-06 18:23:29 +000078 asm volatile(ASM_FTR_IFCLR("tlbie %0,0", PPC_TLBIE(%1,%0), %2)
Paul Mackerras969391c2011-06-29 00:26:11 +000079 : : "r" (va), "r"(0), "i" (CPU_FTR_ARCH_206)
Milton Miller60dbf432009-04-29 20:58:01 +000080 : "memory");
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +110081 break;
82 default:
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +000083 /* We need 14 to 14 + i bits of va */
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +000084 penc = mmu_psize_defs[psize].penc[apsize];
Aneesh Kumar K.V1f6aaac2013-04-28 09:37:39 +000085 va &= ~((1ul << mmu_psize_defs[apsize].shift) - 1);
Arnd Bergmann19242b22006-06-15 21:15:44 +100086 va |= penc << 12;
Paul Mackerras1189be62007-10-11 20:37:10 +100087 va |= ssize << 8;
Aneesh Kumar K.V29ef7a32014-04-21 10:37:36 +053088 /*
89 * AVAL bits:
90 * We don't need all the bits, but rest of the bits
91 * must be ignored by the processor.
92 * vpn cover upto 65 bits of va. (0...65) and we need
93 * 58..64 bits of va.
94 */
95 va |= (vpn & 0xfe); /* AVAL */
Milton Miller60dbf432009-04-29 20:58:01 +000096 va |= 1; /* L */
Michael Neulinga32e2522011-04-06 18:23:29 +000097 asm volatile(ASM_FTR_IFCLR("tlbie %0,1", PPC_TLBIE(%1,%0), %2)
Paul Mackerras969391c2011-06-29 00:26:11 +000098 : : "r" (va), "r"(0), "i" (CPU_FTR_ARCH_206)
Milton Miller60dbf432009-04-29 20:58:01 +000099 : "memory");
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100100 break;
101 }
Balbir Singh04284912017-04-11 15:23:25 +1000102 trace_tlbie(0, 0, va, 0, 0, 0, 0);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100103}
104
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000105static inline void __tlbiel(unsigned long vpn, int psize, int apsize, int ssize)
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100106{
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000107 unsigned long va;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100108 unsigned int penc;
Aneesh Kumar K.Vde640952013-07-04 10:34:45 +0530109 unsigned long sllp;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100110
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000111 /* VPN_SHIFT can be atmost 12 */
112 va = vpn << VPN_SHIFT;
113 /*
114 * clear top 16 bits of 64 bit va, non SLS segment
115 * Older versions of the architecture (2.02 and earler) require the
116 * masking of the top 16 bits.
117 */
Aneesh Kumar K.Vaccfad72016-07-13 15:05:24 +0530118 if (mmu_has_feature(MMU_FTR_TLBIE_CROP_VA))
119 va &= ~(0xffffULL << 48);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100120
121 switch (psize) {
122 case MMU_PAGE_4K:
Aneesh Kumar K.V1f6aaac2013-04-28 09:37:39 +0000123 /* clear out bits after(52) [0....52.....63] */
124 va &= ~((1ul << (64 - 52)) - 1);
Paul Mackerras1189be62007-10-11 20:37:10 +1000125 va |= ssize << 8;
Aneesh Kumar K.V138ee7e2016-07-13 15:06:37 +0530126 sllp = get_sllp_encoding(apsize);
Aneesh Kumar K.Vde640952013-07-04 10:34:45 +0530127 va |= sllp << 5;
Balbir Singhf923efb2016-09-28 17:25:52 +1000128 asm volatile(ASM_FTR_IFSET("tlbiel %0", "tlbiel %0,0", %1)
129 : : "r" (va), "i" (CPU_FTR_ARCH_206)
130 : "memory");
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100131 break;
132 default:
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000133 /* We need 14 to 14 + i bits of va */
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000134 penc = mmu_psize_defs[psize].penc[apsize];
Aneesh Kumar K.V1f6aaac2013-04-28 09:37:39 +0000135 va &= ~((1ul << mmu_psize_defs[apsize].shift) - 1);
Arnd Bergmann19242b22006-06-15 21:15:44 +1000136 va |= penc << 12;
Paul Mackerras1189be62007-10-11 20:37:10 +1000137 va |= ssize << 8;
Aneesh Kumar K.V29ef7a32014-04-21 10:37:36 +0530138 /*
139 * AVAL bits:
140 * We don't need all the bits, but rest of the bits
141 * must be ignored by the processor.
142 * vpn cover upto 65 bits of va. (0...65) and we need
143 * 58..64 bits of va.
144 */
145 va |= (vpn & 0xfe);
Milton Miller60dbf432009-04-29 20:58:01 +0000146 va |= 1; /* L */
Balbir Singhf923efb2016-09-28 17:25:52 +1000147 asm volatile(ASM_FTR_IFSET("tlbiel %0", "tlbiel %0,1", %1)
148 : : "r" (va), "i" (CPU_FTR_ARCH_206)
149 : "memory");
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100150 break;
151 }
Balbir Singh04284912017-04-11 15:23:25 +1000152 trace_tlbie(0, 1, va, 0, 0, 0, 0);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100153
154}
155
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000156static inline void tlbie(unsigned long vpn, int psize, int apsize,
157 int ssize, int local)
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100158{
Ian Munsie4c6d9ac2014-10-08 19:55:00 +1100159 unsigned int use_local;
Matt Evans44ae3ab2011-04-06 19:48:50 +0000160 int lock_tlbie = !mmu_has_feature(MMU_FTR_LOCKLESS_TLBIE);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100161
Ian Munsie4c6d9ac2014-10-08 19:55:00 +1100162 use_local = local && mmu_has_feature(MMU_FTR_TLBIEL) && !cxl_ctx_in_use();
163
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100164 if (use_local)
165 use_local = mmu_psize_defs[psize].tlbiel;
166 if (lock_tlbie && !use_local)
Thomas Gleixner6b9c9b82010-02-18 02:22:35 +0000167 raw_spin_lock(&native_tlbie_lock);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100168 asm volatile("ptesync": : :"memory");
169 if (use_local) {
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000170 __tlbiel(vpn, psize, apsize, ssize);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100171 asm volatile("ptesync": : :"memory");
172 } else {
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000173 __tlbie(vpn, psize, apsize, ssize);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100174 asm volatile("eieio; tlbsync; ptesync": : :"memory");
175 }
176 if (lock_tlbie && !use_local)
Thomas Gleixner6b9c9b82010-02-18 02:22:35 +0000177 raw_spin_unlock(&native_tlbie_lock);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100178}
179
David Gibson8e561e72007-06-13 14:52:56 +1000180static inline void native_lock_hpte(struct hash_pte *hptep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
Anton Blanchard12f04f22013-09-23 12:04:36 +1000182 unsigned long *word = (unsigned long *)&hptep->v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184 while (1) {
Anton Blanchard66d99b82010-02-10 01:03:06 +0000185 if (!test_and_set_bit_lock(HPTE_LOCK_BIT, word))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 break;
187 while(test_bit(HPTE_LOCK_BIT, word))
188 cpu_relax();
189 }
190}
191
David Gibson8e561e72007-06-13 14:52:56 +1000192static inline void native_unlock_hpte(struct hash_pte *hptep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
Anton Blanchard12f04f22013-09-23 12:04:36 +1000194 unsigned long *word = (unsigned long *)&hptep->v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Anton Blanchard66d99b82010-02-10 01:03:06 +0000196 clear_bit_unlock(HPTE_LOCK_BIT, word);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197}
198
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000199static long native_hpte_insert(unsigned long hpte_group, unsigned long vpn,
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100200 unsigned long pa, unsigned long rflags,
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000201 unsigned long vflags, int psize, int apsize, int ssize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
David Gibson8e561e72007-06-13 14:52:56 +1000203 struct hash_pte *hptep = htab_address + hpte_group;
David Gibson96e28442005-07-13 01:11:42 -0700204 unsigned long hpte_v, hpte_r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 int i;
206
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100207 if (!(vflags & HPTE_V_BOLTED)) {
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000208 DBG_LOW(" insert(group=%lx, vpn=%016lx, pa=%016lx,"
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100209 " rflags=%lx, vflags=%lx, psize=%d)\n",
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000210 hpte_group, vpn, pa, rflags, vflags, psize);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100211 }
212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 for (i = 0; i < HPTES_PER_GROUP; i++) {
Anton Blanchard12f04f22013-09-23 12:04:36 +1000214 if (! (be64_to_cpu(hptep->v) & HPTE_V_VALID)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 /* retry with lock held */
216 native_lock_hpte(hptep);
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 break;
219 native_unlock_hpte(hptep);
220 }
221
222 hptep++;
223 }
224
225 if (i == HPTES_PER_GROUP)
226 return -1;
227
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000228 hpte_v = hpte_encode_v(vpn, psize, apsize, ssize) | vflags | HPTE_V_VALID;
Paul Mackerras6b243fc2016-11-11 16:55:03 +1100229 hpte_r = hpte_encode_r(pa, psize, apsize) | rflags;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100230
231 if (!(vflags & HPTE_V_BOLTED)) {
232 DBG_LOW(" i=%x hpte_v=%016lx, hpte_r=%016lx\n",
233 i, hpte_v, hpte_r);
234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Paul Mackerras6b243fc2016-11-11 16:55:03 +1100236 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
237 hpte_r = hpte_old_to_new_r(hpte_v, hpte_r);
238 hpte_v = hpte_old_to_new_v(hpte_v);
239 }
240
Anton Blanchard12f04f22013-09-23 12:04:36 +1000241 hptep->r = cpu_to_be64(hpte_r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 /* Guarantee the second dword is visible before the valid bit */
Kumar Gala74a0ba62007-07-09 23:49:09 -0500243 eieio();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 /*
245 * Now set the first dword including the valid bit
246 * NOTE: this also unlocks the hpte
247 */
Anton Blanchard12f04f22013-09-23 12:04:36 +1000248 hptep->v = cpu_to_be64(hpte_v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250 __asm__ __volatile__ ("ptesync" : : : "memory");
251
David Gibson96e28442005-07-13 01:11:42 -0700252 return i | (!!(vflags & HPTE_V_SECONDARY) << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
254
255static long native_hpte_remove(unsigned long hpte_group)
256{
David Gibson8e561e72007-06-13 14:52:56 +1000257 struct hash_pte *hptep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 int i;
259 int slot_offset;
David Gibson96e28442005-07-13 01:11:42 -0700260 unsigned long hpte_v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100262 DBG_LOW(" remove(group=%lx)\n", hpte_group);
263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 /* pick a random entry to start at */
265 slot_offset = mftb() & 0x7;
266
267 for (i = 0; i < HPTES_PER_GROUP; i++) {
268 hptep = htab_address + hpte_group + slot_offset;
Anton Blanchard12f04f22013-09-23 12:04:36 +1000269 hpte_v = be64_to_cpu(hptep->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
David Gibson96e28442005-07-13 01:11:42 -0700271 if ((hpte_v & HPTE_V_VALID) && !(hpte_v & HPTE_V_BOLTED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 /* retry with lock held */
273 native_lock_hpte(hptep);
Anton Blanchard12f04f22013-09-23 12:04:36 +1000274 hpte_v = be64_to_cpu(hptep->v);
David Gibson96e28442005-07-13 01:11:42 -0700275 if ((hpte_v & HPTE_V_VALID)
276 && !(hpte_v & HPTE_V_BOLTED))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 break;
278 native_unlock_hpte(hptep);
279 }
280
281 slot_offset++;
282 slot_offset &= 0x7;
283 }
284
285 if (i == HPTES_PER_GROUP)
286 return -1;
287
288 /* Invalidate the hpte. NOTE: this also unlocks it */
David Gibson96e28442005-07-13 01:11:42 -0700289 hptep->v = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291 return i;
292}
293
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100294static long native_hpte_updatepp(unsigned long slot, unsigned long newpp,
Aneesh Kumar K.Vdb3d8532013-06-20 14:30:13 +0530295 unsigned long vpn, int bpsize,
Aneesh Kumar K.Vaefa5682014-12-04 11:00:14 +0530296 int apsize, int ssize, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
David Gibson8e561e72007-06-13 14:52:56 +1000298 struct hash_pte *hptep = htab_address + slot;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100299 unsigned long hpte_v, want_v;
Aneesh Kumar K.Vaefa5682014-12-04 11:00:14 +0530300 int ret = 0, local = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Aneesh Kumar K.Vdb3d8532013-06-20 14:30:13 +0530302 want_v = hpte_encode_avpn(vpn, bpsize, ssize);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100303
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000304 DBG_LOW(" update(vpn=%016lx, avpnv=%016lx, group=%lx, newpp=%lx)",
305 vpn, want_v & HPTE_V_AVPN, slot, newpp);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100306
Anton Blanchard12f04f22013-09-23 12:04:36 +1000307 hpte_v = be64_to_cpu(hptep->v);
Paul Mackerras6b243fc2016-11-11 16:55:03 +1100308 if (cpu_has_feature(CPU_FTR_ARCH_300))
309 hpte_v = hpte_new_to_old_v(hpte_v, be64_to_cpu(hptep->r));
Aneesh Kumar K.V0608d692013-05-31 01:03:24 +0000310 /*
311 * We need to invalidate the TLB always because hpte_remove doesn't do
312 * a tlb invalidate. If a hash bucket gets full, we "evict" a more/less
313 * random entry from it. When we do that we don't invalidate the TLB
314 * (hpte_remove) because we assume the old translation is still
315 * technically "valid".
316 */
Aneesh Kumar K.Vdb3d8532013-06-20 14:30:13 +0530317 if (!HPTE_V_COMPARE(hpte_v, want_v) || !(hpte_v & HPTE_V_VALID)) {
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100318 DBG_LOW(" -> miss\n");
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100319 ret = -1;
320 } else {
Aneesh Kumar K.V0ec26982014-11-03 20:21:34 +0530321 native_lock_hpte(hptep);
322 /* recheck with locks held */
323 hpte_v = be64_to_cpu(hptep->v);
Paul Mackerras6b243fc2016-11-11 16:55:03 +1100324 if (cpu_has_feature(CPU_FTR_ARCH_300))
325 hpte_v = hpte_new_to_old_v(hpte_v, be64_to_cpu(hptep->r));
Aneesh Kumar K.V0ec26982014-11-03 20:21:34 +0530326 if (unlikely(!HPTE_V_COMPARE(hpte_v, want_v) ||
327 !(hpte_v & HPTE_V_VALID))) {
328 ret = -1;
329 } else {
330 DBG_LOW(" -> hit\n");
331 /* Update the HPTE */
332 hptep->r = cpu_to_be64((be64_to_cpu(hptep->r) &
Aneesh Kumar K.V8550e2f2016-06-08 19:55:55 +0530333 ~(HPTE_R_PPP | HPTE_R_N)) |
334 (newpp & (HPTE_R_PPP | HPTE_R_N |
Aneesh Kumar K.V0ec26982014-11-03 20:21:34 +0530335 HPTE_R_C)));
336 }
337 native_unlock_hpte(hptep);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100338 }
Aneesh Kumar K.Vaefa5682014-12-04 11:00:14 +0530339
340 if (flags & HPTE_LOCAL_UPDATE)
341 local = 1;
342 /*
343 * Ensure it is out of the tlb too if it is not a nohpte fault
344 */
345 if (!(flags & HPTE_NOHPTE_UPDATE))
346 tlbie(vpn, bpsize, apsize, ssize, local);
347
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100348 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349}
350
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000351static long native_hpte_find(unsigned long vpn, int psize, int ssize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
David Gibson8e561e72007-06-13 14:52:56 +1000353 struct hash_pte *hptep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 unsigned long hash;
Paul Mackerras1189be62007-10-11 20:37:10 +1000355 unsigned long i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 long slot;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100357 unsigned long want_v, hpte_v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000359 hash = hpt_hash(vpn, mmu_psize_defs[psize].shift, ssize);
Aneesh Kumar K.V74f227b2013-04-28 09:37:34 +0000360 want_v = hpte_encode_avpn(vpn, psize, ssize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Paul Mackerras1189be62007-10-11 20:37:10 +1000362 /* Bolted mappings are only ever in the primary group */
363 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
364 for (i = 0; i < HPTES_PER_GROUP; i++) {
365 hptep = htab_address + slot;
Anton Blanchard12f04f22013-09-23 12:04:36 +1000366 hpte_v = be64_to_cpu(hptep->v);
Paul Mackerras6b243fc2016-11-11 16:55:03 +1100367 if (cpu_has_feature(CPU_FTR_ARCH_300))
368 hpte_v = hpte_new_to_old_v(hpte_v, be64_to_cpu(hptep->r));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Paul Mackerras1189be62007-10-11 20:37:10 +1000370 if (HPTE_V_COMPARE(hpte_v, want_v) && (hpte_v & HPTE_V_VALID))
371 /* HPTE matches */
372 return slot;
373 ++slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 }
375
376 return -1;
377}
378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379/*
380 * Update the page protection bits. Intended to be used to create
381 * guard pages for kernel data structures on pages which are bolted
382 * in the HPT. Assumes pages being operated on will not be stolen.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 *
384 * No need to lock here because we should be the only user.
385 */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100386static void native_hpte_updateboltedpp(unsigned long newpp, unsigned long ea,
Paul Mackerras1189be62007-10-11 20:37:10 +1000387 int psize, int ssize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000389 unsigned long vpn;
390 unsigned long vsid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 long slot;
David Gibson8e561e72007-06-13 14:52:56 +1000392 struct hash_pte *hptep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Paul Mackerras1189be62007-10-11 20:37:10 +1000394 vsid = get_kernel_vsid(ea, ssize);
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000395 vpn = hpt_vpn(ea, vsid, ssize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000397 slot = native_hpte_find(vpn, psize, ssize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 if (slot == -1)
399 panic("could not find page to bolt\n");
400 hptep = htab_address + slot;
401
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100402 /* Update the HPTE */
Anton Blanchard12f04f22013-09-23 12:04:36 +1000403 hptep->r = cpu_to_be64((be64_to_cpu(hptep->r) &
Aneesh Kumar K.V8550e2f2016-06-08 19:55:55 +0530404 ~(HPTE_R_PPP | HPTE_R_N)) |
405 (newpp & (HPTE_R_PPP | HPTE_R_N)));
Aneesh Kumar K.Vdb3d8532013-06-20 14:30:13 +0530406 /*
407 * Ensure it is out of the tlb too. Bolted entries base and
408 * actual page size will be same.
409 */
410 tlbie(vpn, psize, psize, ssize, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411}
412
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000413static void native_hpte_invalidate(unsigned long slot, unsigned long vpn,
Aneesh Kumar K.Vdb3d8532013-06-20 14:30:13 +0530414 int bpsize, int apsize, int ssize, int local)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
David Gibson8e561e72007-06-13 14:52:56 +1000416 struct hash_pte *hptep = htab_address + slot;
David Gibson96e28442005-07-13 01:11:42 -0700417 unsigned long hpte_v;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100418 unsigned long want_v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000423 DBG_LOW(" invalidate(vpn=%016lx, hash: %lx)\n", vpn, slot);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100424
Aneesh Kumar K.Vdb3d8532013-06-20 14:30:13 +0530425 want_v = hpte_encode_avpn(vpn, bpsize, ssize);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100426 native_lock_hpte(hptep);
Anton Blanchard12f04f22013-09-23 12:04:36 +1000427 hpte_v = be64_to_cpu(hptep->v);
Paul Mackerras6b243fc2016-11-11 16:55:03 +1100428 if (cpu_has_feature(CPU_FTR_ARCH_300))
429 hpte_v = hpte_new_to_old_v(hpte_v, be64_to_cpu(hptep->r));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Aneesh Kumar K.V0608d692013-05-31 01:03:24 +0000431 /*
432 * We need to invalidate the TLB always because hpte_remove doesn't do
433 * a tlb invalidate. If a hash bucket gets full, we "evict" a more/less
434 * random entry from it. When we do that we don't invalidate the TLB
435 * (hpte_remove) because we assume the old translation is still
436 * technically "valid".
437 */
Aneesh Kumar K.Vdb3d8532013-06-20 14:30:13 +0530438 if (!HPTE_V_COMPARE(hpte_v, want_v) || !(hpte_v & HPTE_V_VALID))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 native_unlock_hpte(hptep);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100440 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 /* Invalidate the hpte. NOTE: this also unlocks it */
David Gibson96e28442005-07-13 01:11:42 -0700442 hptep->v = 0;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100443
444 /* Invalidate the TLB */
Aneesh Kumar K.Vdb3d8532013-06-20 14:30:13 +0530445 tlbie(vpn, bpsize, apsize, ssize, local);
446
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100447 local_irq_restore(flags);
448}
449
Aneesh Kumar K.Ve34aa032015-12-01 09:06:53 +0530450#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Aneesh Kumar K.Vfa1f8ae2014-08-13 12:31:58 +0530451static void native_hugepage_invalidate(unsigned long vsid,
452 unsigned long addr,
Aneesh Kumar K.V1a527282013-06-20 14:30:27 +0530453 unsigned char *hpte_slot_array,
Aneesh Kumar K.Vd557b092014-11-02 21:15:28 +0530454 int psize, int ssize, int local)
Aneesh Kumar K.V1a527282013-06-20 14:30:27 +0530455{
Aneesh Kumar K.V969b7b22014-08-13 12:32:01 +0530456 int i;
Aneesh Kumar K.V1a527282013-06-20 14:30:27 +0530457 struct hash_pte *hptep;
458 int actual_psize = MMU_PAGE_16M;
459 unsigned int max_hpte_count, valid;
460 unsigned long flags, s_addr = addr;
461 unsigned long hpte_v, want_v, shift;
Aneesh Kumar K.Vfa1f8ae2014-08-13 12:31:58 +0530462 unsigned long hidx, vpn = 0, hash, slot;
Aneesh Kumar K.V1a527282013-06-20 14:30:27 +0530463
464 shift = mmu_psize_defs[psize].shift;
465 max_hpte_count = 1U << (PMD_SHIFT - shift);
466
467 local_irq_save(flags);
468 for (i = 0; i < max_hpte_count; i++) {
469 valid = hpte_valid(hpte_slot_array, i);
470 if (!valid)
471 continue;
472 hidx = hpte_hash_index(hpte_slot_array, i);
473
474 /* get the vpn */
475 addr = s_addr + (i * (1ul << shift));
Aneesh Kumar K.V1a527282013-06-20 14:30:27 +0530476 vpn = hpt_vpn(addr, vsid, ssize);
477 hash = hpt_hash(vpn, shift, ssize);
478 if (hidx & _PTEIDX_SECONDARY)
479 hash = ~hash;
480
481 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
482 slot += hidx & _PTEIDX_GROUP_IX;
483
484 hptep = htab_address + slot;
485 want_v = hpte_encode_avpn(vpn, psize, ssize);
486 native_lock_hpte(hptep);
Anton Blanchard12f04f22013-09-23 12:04:36 +1000487 hpte_v = be64_to_cpu(hptep->v);
Paul Mackerras6b243fc2016-11-11 16:55:03 +1100488 if (cpu_has_feature(CPU_FTR_ARCH_300))
489 hpte_v = hpte_new_to_old_v(hpte_v, be64_to_cpu(hptep->r));
Aneesh Kumar K.V1a527282013-06-20 14:30:27 +0530490
491 /* Even if we miss, we need to invalidate the TLB */
492 if (!HPTE_V_COMPARE(hpte_v, want_v) || !(hpte_v & HPTE_V_VALID))
493 native_unlock_hpte(hptep);
494 else
495 /* Invalidate the hpte. NOTE: this also unlocks it */
496 hptep->v = 0;
Aneesh Kumar K.V969b7b22014-08-13 12:32:01 +0530497 /*
498 * We need to do tlb invalidate for all the address, tlbie
499 * instruction compares entry_VA in tlb with the VA specified
500 * here
501 */
Aneesh Kumar K.Vd557b092014-11-02 21:15:28 +0530502 tlbie(vpn, psize, actual_psize, ssize, local);
Aneesh Kumar K.V1a527282013-06-20 14:30:27 +0530503 }
Aneesh Kumar K.V1a527282013-06-20 14:30:27 +0530504 local_irq_restore(flags);
505}
Aneesh Kumar K.Ve34aa032015-12-01 09:06:53 +0530506#else
507static void native_hugepage_invalidate(unsigned long vsid,
508 unsigned long addr,
509 unsigned char *hpte_slot_array,
510 int psize, int ssize, int local)
511{
512 WARN(1, "%s called without THP support\n", __func__);
513}
514#endif
Aneesh Kumar K.V1a527282013-06-20 14:30:27 +0530515
David Gibson8e561e72007-06-13 14:52:56 +1000516static void hpte_decode(struct hash_pte *hpte, unsigned long slot,
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000517 int *psize, int *apsize, int *ssize, unsigned long *vpn)
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100518{
Aneesh Kumar K.Vdcda2872012-09-10 02:52:49 +0000519 unsigned long avpn, pteg, vpi;
Anton Blanchard12f04f22013-09-23 12:04:36 +1000520 unsigned long hpte_v = be64_to_cpu(hpte->v);
521 unsigned long hpte_r = be64_to_cpu(hpte->r);
Aneesh Kumar K.Vdcda2872012-09-10 02:52:49 +0000522 unsigned long vsid, seg_off;
Aneesh Kumar K.V7e74c392013-04-28 09:37:36 +0000523 int size, a_size, shift;
524 /* Look at the 8 bit LP value */
Anton Blanchard12f04f22013-09-23 12:04:36 +1000525 unsigned int lp = (hpte_r >> LP_SHIFT) & ((1 << LP_BITS) - 1);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100526
Paul Mackerras6b243fc2016-11-11 16:55:03 +1100527 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
528 hpte_v = hpte_new_to_old_v(hpte_v, hpte_r);
529 hpte_r = hpte_new_to_old_r(hpte_r);
530 }
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000531 if (!(hpte_v & HPTE_V_LARGE)) {
532 size = MMU_PAGE_4K;
533 a_size = MMU_PAGE_4K;
534 } else {
Paul Mackerras0eeede02016-09-02 17:20:43 +1000535 size = hpte_page_sizes[lp] & 0xf;
536 a_size = hpte_page_sizes[lp] >> 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 }
Paul Mackerras2454c7e2007-05-10 15:28:44 +1000538 /* This works for all page sizes, and for 256M and 1T segments */
Paul Mackerras6b243fc2016-11-11 16:55:03 +1100539 *ssize = hpte_v >> HPTE_V_SSIZE_SHIFT;
Aneesh Kumar K.Vdcda2872012-09-10 02:52:49 +0000540 shift = mmu_psize_defs[size].shift;
541
542 avpn = (HPTE_V_AVPN_VAL(hpte_v) & ~mmu_psize_defs[size].avpnm);
543 pteg = slot / HPTES_PER_GROUP;
544 if (hpte_v & HPTE_V_SECONDARY)
545 pteg = ~pteg;
546
547 switch (*ssize) {
548 case MMU_SEGSIZE_256M:
549 /* We only have 28 - 23 bits of seg_off in avpn */
550 seg_off = (avpn & 0x1f) << 23;
551 vsid = avpn >> 5;
552 /* We can find more bits from the pteg value */
553 if (shift < 23) {
554 vpi = (vsid ^ pteg) & htab_hash_mask;
555 seg_off |= vpi << shift;
556 }
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000557 *vpn = vsid << (SID_SHIFT - VPN_SHIFT) | seg_off >> VPN_SHIFT;
Aneesh Kumar K.V83383b72013-07-03 13:50:03 +0530558 break;
Aneesh Kumar K.Vdcda2872012-09-10 02:52:49 +0000559 case MMU_SEGSIZE_1T:
560 /* We only have 40 - 23 bits of seg_off in avpn */
561 seg_off = (avpn & 0x1ffff) << 23;
562 vsid = avpn >> 17;
563 if (shift < 23) {
564 vpi = (vsid ^ (vsid << 25) ^ pteg) & htab_hash_mask;
565 seg_off |= vpi << shift;
566 }
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000567 *vpn = vsid << (SID_SHIFT_1T - VPN_SHIFT) | seg_off >> VPN_SHIFT;
Aneesh Kumar K.V83383b72013-07-03 13:50:03 +0530568 break;
Aneesh Kumar K.Vdcda2872012-09-10 02:52:49 +0000569 default:
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000570 *vpn = size = 0;
Aneesh Kumar K.Vdcda2872012-09-10 02:52:49 +0000571 }
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000572 *psize = size;
573 *apsize = a_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
575
R Sharadaf4c82d52005-06-25 14:58:08 -0700576/*
577 * clear all mappings on kexec. All cpus are in real mode (or they will
578 * be when they isi), and we are the only one left. We rely on our kernel
579 * mapping being 0xC0's and the hardware ignoring those two real bits.
580 *
Cyril Burfdf880a2015-10-08 11:04:26 +1100581 * This must be called with interrupts disabled.
582 *
583 * Taking the native_tlbie_lock is unsafe here due to the possibility of
584 * lockdep being on. On pre POWER5 hardware, not taking the lock could
585 * cause deadlock. POWER5 and newer not taking the lock is fine. This only
586 * gets called during boot before secondary CPUs have come up and during
587 * crashdump and all bets are off anyway.
588 *
R Sharadaf4c82d52005-06-25 14:58:08 -0700589 * TODO: add batching support when enabled. remember, no dynamic memory here,
Michael Ellerman027dfac2016-06-01 16:34:37 +1000590 * although there is the control page available...
R Sharadaf4c82d52005-06-25 14:58:08 -0700591 */
592static void native_hpte_clear(void)
593{
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000594 unsigned long vpn = 0;
Cyril Burfdf880a2015-10-08 11:04:26 +1100595 unsigned long slot, slots;
David Gibson8e561e72007-06-13 14:52:56 +1000596 struct hash_pte *hptep = htab_address;
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000597 unsigned long hpte_v;
R Sharadaf4c82d52005-06-25 14:58:08 -0700598 unsigned long pteg_count;
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000599 int psize, apsize, ssize;
R Sharadaf4c82d52005-06-25 14:58:08 -0700600
601 pteg_count = htab_hash_mask + 1;
602
R Sharadaf4c82d52005-06-25 14:58:08 -0700603 slots = pteg_count * HPTES_PER_GROUP;
604
605 for (slot = 0; slot < slots; slot++, hptep++) {
606 /*
607 * we could lock the pte here, but we are the only cpu
608 * running, right? and for crash dump, we probably
609 * don't want to wait for a maybe bad cpu.
610 */
Anton Blanchard12f04f22013-09-23 12:04:36 +1000611 hpte_v = be64_to_cpu(hptep->v);
R Sharadaf4c82d52005-06-25 14:58:08 -0700612
R Sharada47f78a42006-02-22 21:43:08 +0530613 /*
Cyril Burfdf880a2015-10-08 11:04:26 +1100614 * Call __tlbie() here rather than tlbie() since we can't take the
615 * native_tlbie_lock.
R Sharada47f78a42006-02-22 21:43:08 +0530616 */
David Gibson96e28442005-07-13 01:11:42 -0700617 if (hpte_v & HPTE_V_VALID) {
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000618 hpte_decode(hptep, slot, &psize, &apsize, &ssize, &vpn);
David Gibson96e28442005-07-13 01:11:42 -0700619 hptep->v = 0;
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000620 __tlbie(vpn, psize, apsize, ssize);
R Sharadaf4c82d52005-06-25 14:58:08 -0700621 }
622 }
623
R Sharada47f78a42006-02-22 21:43:08 +0530624 asm volatile("eieio; tlbsync; ptesync":::"memory");
R Sharadaf4c82d52005-06-25 14:58:08 -0700625}
626
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100627/*
628 * Batched hash table flush, we batch the tlbie's to avoid taking/releasing
629 * the lock all the time
630 */
Benjamin Herrenschmidt61b1a942005-09-20 13:52:50 +1000631static void native_flush_hash_range(unsigned long number, int local)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000633 unsigned long vpn;
634 unsigned long hash, index, hidx, shift, slot;
David Gibson8e561e72007-06-13 14:52:56 +1000635 struct hash_pte *hptep;
David Gibson96e28442005-07-13 01:11:42 -0700636 unsigned long hpte_v;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100637 unsigned long want_v;
638 unsigned long flags;
639 real_pte_t pte;
Christoph Lameter69111ba2014-10-21 15:23:25 -0500640 struct ppc64_tlb_batch *batch = this_cpu_ptr(&ppc64_tlb_batch);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100641 unsigned long psize = batch->psize;
Paul Mackerras1189be62007-10-11 20:37:10 +1000642 int ssize = batch->ssize;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100643 int i;
Frederic Barrat88b1bf722017-03-29 19:19:42 +0200644 unsigned int use_local;
645
646 use_local = local && mmu_has_feature(MMU_FTR_TLBIEL) &&
647 mmu_psize_defs[psize].tlbiel && !cxl_ctx_in_use();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
649 local_irq_save(flags);
650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 for (i = 0; i < number; i++) {
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000652 vpn = batch->vpn[i];
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100653 pte = batch->pte[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000655 pte_iterate_hashed_subpages(pte, psize, vpn, index, shift) {
656 hash = hpt_hash(vpn, shift, ssize);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100657 hidx = __rpte_to_hidx(pte, index);
658 if (hidx & _PTEIDX_SECONDARY)
659 hash = ~hash;
660 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
661 slot += hidx & _PTEIDX_GROUP_IX;
662 hptep = htab_address + slot;
Aneesh Kumar K.V74f227b2013-04-28 09:37:34 +0000663 want_v = hpte_encode_avpn(vpn, psize, ssize);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100664 native_lock_hpte(hptep);
Anton Blanchard12f04f22013-09-23 12:04:36 +1000665 hpte_v = be64_to_cpu(hptep->v);
Paul Mackerras6b243fc2016-11-11 16:55:03 +1100666 if (cpu_has_feature(CPU_FTR_ARCH_300))
667 hpte_v = hpte_new_to_old_v(hpte_v,
668 be64_to_cpu(hptep->r));
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100669 if (!HPTE_V_COMPARE(hpte_v, want_v) ||
670 !(hpte_v & HPTE_V_VALID))
671 native_unlock_hpte(hptep);
672 else
673 hptep->v = 0;
674 } pte_iterate_hashed_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 }
676
Frederic Barrat88b1bf722017-03-29 19:19:42 +0200677 if (use_local) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 asm volatile("ptesync":::"memory");
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100679 for (i = 0; i < number; i++) {
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000680 vpn = batch->vpn[i];
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100681 pte = batch->pte[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000683 pte_iterate_hashed_subpages(pte, psize,
684 vpn, index, shift) {
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000685 __tlbiel(vpn, psize, psize, ssize);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100686 } pte_iterate_hashed_end();
687 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 asm volatile("ptesync":::"memory");
689 } else {
Matt Evans44ae3ab2011-04-06 19:48:50 +0000690 int lock_tlbie = !mmu_has_feature(MMU_FTR_LOCKLESS_TLBIE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
692 if (lock_tlbie)
Thomas Gleixner6b9c9b82010-02-18 02:22:35 +0000693 raw_spin_lock(&native_tlbie_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 asm volatile("ptesync":::"memory");
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100696 for (i = 0; i < number; i++) {
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000697 vpn = batch->vpn[i];
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100698 pte = batch->pte[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000700 pte_iterate_hashed_subpages(pte, psize,
701 vpn, index, shift) {
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000702 __tlbie(vpn, psize, psize, ssize);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100703 } pte_iterate_hashed_end();
704 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 asm volatile("eieio; tlbsync; ptesync":::"memory");
706
707 if (lock_tlbie)
Thomas Gleixner6b9c9b82010-02-18 02:22:35 +0000708 raw_spin_unlock(&native_tlbie_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 }
710
711 local_irq_restore(flags);
712}
713
Aneesh Kumar K.V83209bc2016-07-13 15:05:28 +0530714static int native_register_proc_table(unsigned long base, unsigned long page_size,
715 unsigned long table_size)
Aneesh Kumar K.V50de5962016-04-29 23:25:43 +1000716{
Aneesh Kumar K.V83209bc2016-07-13 15:05:28 +0530717 unsigned long patb1 = base << 25; /* VSID */
718
719 patb1 |= (page_size << 5); /* sllp */
720 patb1 |= table_size;
721
Aneesh Kumar K.V50de5962016-04-29 23:25:43 +1000722 partition_tb->patb1 = cpu_to_be64(patb1);
723 return 0;
724}
725
Michael Ellerman7d0daae2006-06-23 18:16:38 +1000726void __init hpte_init_native(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727{
Benjamin Herrenschmidt70257762016-07-05 15:03:58 +1000728 mmu_hash_ops.hpte_invalidate = native_hpte_invalidate;
729 mmu_hash_ops.hpte_updatepp = native_hpte_updatepp;
730 mmu_hash_ops.hpte_updateboltedpp = native_hpte_updateboltedpp;
731 mmu_hash_ops.hpte_insert = native_hpte_insert;
732 mmu_hash_ops.hpte_remove = native_hpte_remove;
733 mmu_hash_ops.hpte_clear_all = native_hpte_clear;
734 mmu_hash_ops.flush_hash_range = native_flush_hash_range;
735 mmu_hash_ops.hugepage_invalidate = native_hugepage_invalidate;
Aneesh Kumar K.V50de5962016-04-29 23:25:43 +1000736
737 if (cpu_has_feature(CPU_FTR_ARCH_300))
Michael Ellermaneea81482016-08-04 15:32:06 +1000738 register_process_table = native_register_proc_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739}