blob: d8279bfe68ea929309b0d29f5c5b4d08e2f325cb [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * native hashtable management.
4 *
5 * SMP scalability work:
6 * Copyright (C) 2001 Anton Blanchard <anton@au.ibm.com>, IBM
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11008
9#undef DEBUG_LOW
10
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/spinlock.h>
12#include <linux/bitops.h>
Michael Ellermanbeacc6d2012-07-25 21:20:03 +000013#include <linux/of.h>
Nicholas Piggin4e287e62017-06-06 23:08:32 +100014#include <linux/processor.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/threads.h>
16#include <linux/smp.h>
Mike Rapoport65fddcf2020-06-08 21:32:42 -070017#include <linux/pgtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <asm/machdep.h>
20#include <asm/mmu.h>
21#include <asm/mmu_context.h>
Balbir Singh04284912017-04-11 15:23:25 +100022#include <asm/trace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <asm/tlb.h>
24#include <asm/cputable.h>
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +110025#include <asm/udbg.h>
Luke Browning71bf08b2007-05-03 00:19:11 +100026#include <asm/kexec.h>
Milton Miller60dbf432009-04-29 20:58:01 +000027#include <asm/ppc-opcode.h>
Christophe Leroy2c86cd12018-07-05 16:25:01 +000028#include <asm/feature-fixups.h>
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +110029
Michael Neulingec249dd2015-05-27 16:07:16 +100030#include <misc/cxl-base.h>
Ian Munsie4c6d9ac2014-10-08 19:55:00 +110031
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +110032#ifdef DEBUG_LOW
33#define DBG_LOW(fmt...) udbg_printf(fmt)
34#else
35#define DBG_LOW(fmt...)
36#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Anton Blanchard12f04f22013-09-23 12:04:36 +100038#ifdef __BIG_ENDIAN__
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#define HPTE_LOCK_BIT 3
Anton Blanchard12f04f22013-09-23 12:04:36 +100040#else
41#define HPTE_LOCK_BIT (56+3)
42#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
YueHaibingd667edc2019-05-04 18:24:27 +080044static DEFINE_RAW_SPINLOCK(native_tlbie_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Nicholas Piggind4748272017-12-24 01:15:50 +100046static inline void tlbiel_hash_set_isa206(unsigned int set, unsigned int is)
47{
48 unsigned long rb;
49
50 rb = (set << PPC_BITLSHIFT(51)) | (is << PPC_BITLSHIFT(53));
51
52 asm volatile("tlbiel %0" : : "r" (rb));
53}
54
55/*
56 * tlbiel instruction for hash, set invalidation
57 * i.e., r=1 and is=01 or is=10 or is=11
58 */
Masahiro Yamada6d3ca7e2019-05-21 22:13:24 +090059static __always_inline void tlbiel_hash_set_isa300(unsigned int set, unsigned int is,
Nicholas Piggind4748272017-12-24 01:15:50 +100060 unsigned int pid,
61 unsigned int ric, unsigned int prs)
62{
63 unsigned long rb;
64 unsigned long rs;
65 unsigned int r = 0; /* hash format */
66
67 rb = (set << PPC_BITLSHIFT(51)) | (is << PPC_BITLSHIFT(53));
68 rs = ((unsigned long)pid << PPC_BITLSHIFT(31));
69
70 asm volatile(PPC_TLBIEL(%0, %1, %2, %3, %4)
Nicholas Piggin5844cc22020-11-26 20:25:27 +100071 : : "r"(rb), "r"(rs), "i"(ric), "i"(prs), "i"(r)
Nicholas Piggind4748272017-12-24 01:15:50 +100072 : "memory");
73}
74
75
76static void tlbiel_all_isa206(unsigned int num_sets, unsigned int is)
77{
78 unsigned int set;
79
80 asm volatile("ptesync": : :"memory");
81
82 for (set = 0; set < num_sets; set++)
83 tlbiel_hash_set_isa206(set, is);
84
Nicholas Piggin05504b42020-09-16 13:02:34 +100085 ppc_after_tlbiel_barrier();
Nicholas Piggind4748272017-12-24 01:15:50 +100086}
87
88static void tlbiel_all_isa300(unsigned int num_sets, unsigned int is)
89{
90 unsigned int set;
91
92 asm volatile("ptesync": : :"memory");
93
94 /*
Nicholas Pigginc0b27c52020-11-26 20:25:28 +100095 * Flush the partition table cache if this is HV mode.
Nicholas Piggind4748272017-12-24 01:15:50 +100096 */
Nicholas Pigginc0b27c52020-11-26 20:25:28 +100097 if (early_cpu_has_feature(CPU_FTR_HVMODE))
98 tlbiel_hash_set_isa300(0, is, 0, 2, 0);
Nicholas Piggind4748272017-12-24 01:15:50 +100099
100 /*
Nicholas Pigginc0b27c52020-11-26 20:25:28 +1000101 * Now invalidate the process table cache. UPRT=0 HPT modes (what
102 * current hardware implements) do not use the process table, but
103 * add the flushes anyway.
Nicholas Piggind4748272017-12-24 01:15:50 +1000104 *
105 * From ISA v3.0B p. 1078:
106 * The following forms are invalid.
107 * * PRS=1, R=0, and RIC!=2 (The only process-scoped
108 * HPT caching is of the Process Table.)
109 */
110 tlbiel_hash_set_isa300(0, is, 0, 2, 1);
111
Nicholas Pigginc0b27c52020-11-26 20:25:28 +1000112 /*
113 * Then flush the sets of the TLB proper. Hash mode uses
114 * partition scoped TLB translations, which may be flushed
115 * in !HV mode.
116 */
117 for (set = 0; set < num_sets; set++)
118 tlbiel_hash_set_isa300(set, is, 0, 0, 0);
119
Nicholas Piggin05504b42020-09-16 13:02:34 +1000120 ppc_after_tlbiel_barrier();
Nicholas Pigginbc276ec2018-08-27 13:03:01 +1000121
Nicholas Pigginfe7946c2019-06-23 20:41:51 +1000122 asm volatile(PPC_ISA_3_0_INVALIDATE_ERAT "; isync" : : :"memory");
Nicholas Piggind4748272017-12-24 01:15:50 +1000123}
124
125void hash__tlbiel_all(unsigned int action)
126{
127 unsigned int is;
128
129 switch (action) {
130 case TLB_INVAL_SCOPE_GLOBAL:
131 is = 3;
132 break;
133 case TLB_INVAL_SCOPE_LPID:
134 is = 2;
135 break;
136 default:
137 BUG();
138 }
139
140 if (early_cpu_has_feature(CPU_FTR_ARCH_300))
141 tlbiel_all_isa300(POWER9_TLB_SETS_HASH, is);
142 else if (early_cpu_has_feature(CPU_FTR_ARCH_207S))
143 tlbiel_all_isa206(POWER8_TLB_SETS, is);
144 else if (early_cpu_has_feature(CPU_FTR_ARCH_206))
145 tlbiel_all_isa206(POWER7_TLB_SETS, is);
146 else
147 WARN(1, "%s called on pre-POWER7 CPU\n", __func__);
Nicholas Piggind4748272017-12-24 01:15:50 +1000148}
149
Mahesh Salgaonkara3961f82017-11-22 23:02:07 +0530150static inline unsigned long ___tlbie(unsigned long vpn, int psize,
151 int apsize, int ssize)
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100152{
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000153 unsigned long va;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100154 unsigned int penc;
Aneesh Kumar K.Vde640952013-07-04 10:34:45 +0530155 unsigned long sllp;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100156
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000157 /*
158 * We need 14 to 65 bits of va for a tlibe of 4K page
159 * With vpn we ignore the lower VPN_SHIFT bits already.
160 * And top two bits are already ignored because we can
Michael Ellerman027dfac2016-06-01 16:34:37 +1000161 * only accomodate 76 bits in a 64 bit vpn with a VPN_SHIFT
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000162 * of 12.
163 */
164 va = vpn << VPN_SHIFT;
165 /*
166 * clear top 16 bits of 64bit va, non SLS segment
167 * Older versions of the architecture (2.02 and earler) require the
168 * masking of the top 16 bits.
169 */
Aneesh Kumar K.Vaccfad72016-07-13 15:05:24 +0530170 if (mmu_has_feature(MMU_FTR_TLBIE_CROP_VA))
171 va &= ~(0xffffULL << 48);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100172
173 switch (psize) {
174 case MMU_PAGE_4K:
Aneesh Kumar K.V1f6aaac2013-04-28 09:37:39 +0000175 /* clear out bits after (52) [0....52.....63] */
176 va &= ~((1ul << (64 - 52)) - 1);
Paul Mackerras1189be62007-10-11 20:37:10 +1000177 va |= ssize << 8;
Aneesh Kumar K.V138ee7e2016-07-13 15:06:37 +0530178 sllp = get_sllp_encoding(apsize);
Aneesh Kumar K.Vde640952013-07-04 10:34:45 +0530179 va |= sllp << 5;
Michael Neulinga32e2522011-04-06 18:23:29 +0000180 asm volatile(ASM_FTR_IFCLR("tlbie %0,0", PPC_TLBIE(%1,%0), %2)
Paul Mackerras969391c2011-06-29 00:26:11 +0000181 : : "r" (va), "r"(0), "i" (CPU_FTR_ARCH_206)
Milton Miller60dbf432009-04-29 20:58:01 +0000182 : "memory");
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100183 break;
184 default:
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000185 /* We need 14 to 14 + i bits of va */
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000186 penc = mmu_psize_defs[psize].penc[apsize];
Aneesh Kumar K.V1f6aaac2013-04-28 09:37:39 +0000187 va &= ~((1ul << mmu_psize_defs[apsize].shift) - 1);
Arnd Bergmann19242b22006-06-15 21:15:44 +1000188 va |= penc << 12;
Paul Mackerras1189be62007-10-11 20:37:10 +1000189 va |= ssize << 8;
Aneesh Kumar K.V29ef7a32014-04-21 10:37:36 +0530190 /*
191 * AVAL bits:
192 * We don't need all the bits, but rest of the bits
193 * must be ignored by the processor.
194 * vpn cover upto 65 bits of va. (0...65) and we need
195 * 58..64 bits of va.
196 */
197 va |= (vpn & 0xfe); /* AVAL */
Milton Miller60dbf432009-04-29 20:58:01 +0000198 va |= 1; /* L */
Michael Neulinga32e2522011-04-06 18:23:29 +0000199 asm volatile(ASM_FTR_IFCLR("tlbie %0,1", PPC_TLBIE(%1,%0), %2)
Paul Mackerras969391c2011-06-29 00:26:11 +0000200 : : "r" (va), "r"(0), "i" (CPU_FTR_ARCH_206)
Milton Miller60dbf432009-04-29 20:58:01 +0000201 : "memory");
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100202 break;
203 }
Mahesh Salgaonkara3961f82017-11-22 23:02:07 +0530204 return va;
205}
206
Aneesh Kumar K.V047e6572019-09-24 09:22:53 +0530207static inline void fixup_tlbie_vpn(unsigned long vpn, int psize,
208 int apsize, int ssize)
Aneesh Kumar K.Va5d4b582018-03-23 10:26:27 +0530209{
Aneesh Kumar K.V047e6572019-09-24 09:22:53 +0530210 if (cpu_has_feature(CPU_FTR_P9_TLBIE_ERAT_BUG)) {
211 /* Radix flush for a hash guest */
212
213 unsigned long rb,rs,prs,r,ric;
214
215 rb = PPC_BIT(52); /* IS = 2 */
216 rs = 0; /* lpid = 0 */
217 prs = 0; /* partition scoped */
218 r = 1; /* radix format */
219 ric = 0; /* RIC_FLSUH_TLB */
220
221 /*
222 * Need the extra ptesync to make sure we don't
223 * re-order the tlbie
224 */
225 asm volatile("ptesync": : :"memory");
226 asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
227 : : "r"(rb), "i"(r), "i"(prs),
228 "i"(ric), "r"(rs) : "memory");
229 }
230
231
Aneesh Kumar K.V09ce98c2019-09-24 09:22:52 +0530232 if (cpu_has_feature(CPU_FTR_P9_TLBIE_STQ_BUG)) {
Aneesh Kumar K.Va5d4b582018-03-23 10:26:27 +0530233 /* Need the extra ptesync to ensure we don't reorder tlbie*/
234 asm volatile("ptesync": : :"memory");
235 ___tlbie(vpn, psize, apsize, ssize);
236 }
237}
238
Mahesh Salgaonkara3961f82017-11-22 23:02:07 +0530239static inline void __tlbie(unsigned long vpn, int psize, int apsize, int ssize)
240{
241 unsigned long rb;
242
243 rb = ___tlbie(vpn, psize, apsize, ssize);
244 trace_tlbie(0, 0, rb, 0, 0, 0, 0);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100245}
246
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000247static inline void __tlbiel(unsigned long vpn, int psize, int apsize, int ssize)
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100248{
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000249 unsigned long va;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100250 unsigned int penc;
Aneesh Kumar K.Vde640952013-07-04 10:34:45 +0530251 unsigned long sllp;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100252
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000253 /* VPN_SHIFT can be atmost 12 */
254 va = vpn << VPN_SHIFT;
255 /*
256 * clear top 16 bits of 64 bit va, non SLS segment
257 * Older versions of the architecture (2.02 and earler) require the
258 * masking of the top 16 bits.
259 */
Aneesh Kumar K.Vaccfad72016-07-13 15:05:24 +0530260 if (mmu_has_feature(MMU_FTR_TLBIE_CROP_VA))
261 va &= ~(0xffffULL << 48);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100262
263 switch (psize) {
264 case MMU_PAGE_4K:
Aneesh Kumar K.V1f6aaac2013-04-28 09:37:39 +0000265 /* clear out bits after(52) [0....52.....63] */
266 va &= ~((1ul << (64 - 52)) - 1);
Paul Mackerras1189be62007-10-11 20:37:10 +1000267 va |= ssize << 8;
Aneesh Kumar K.V138ee7e2016-07-13 15:06:37 +0530268 sllp = get_sllp_encoding(apsize);
Aneesh Kumar K.Vde640952013-07-04 10:34:45 +0530269 va |= sllp << 5;
Balbir Singhf923efb2016-09-28 17:25:52 +1000270 asm volatile(ASM_FTR_IFSET("tlbiel %0", "tlbiel %0,0", %1)
271 : : "r" (va), "i" (CPU_FTR_ARCH_206)
272 : "memory");
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100273 break;
274 default:
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000275 /* We need 14 to 14 + i bits of va */
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000276 penc = mmu_psize_defs[psize].penc[apsize];
Aneesh Kumar K.V1f6aaac2013-04-28 09:37:39 +0000277 va &= ~((1ul << mmu_psize_defs[apsize].shift) - 1);
Arnd Bergmann19242b22006-06-15 21:15:44 +1000278 va |= penc << 12;
Paul Mackerras1189be62007-10-11 20:37:10 +1000279 va |= ssize << 8;
Aneesh Kumar K.V29ef7a32014-04-21 10:37:36 +0530280 /*
281 * AVAL bits:
282 * We don't need all the bits, but rest of the bits
283 * must be ignored by the processor.
284 * vpn cover upto 65 bits of va. (0...65) and we need
285 * 58..64 bits of va.
286 */
287 va |= (vpn & 0xfe);
Milton Miller60dbf432009-04-29 20:58:01 +0000288 va |= 1; /* L */
Balbir Singhf923efb2016-09-28 17:25:52 +1000289 asm volatile(ASM_FTR_IFSET("tlbiel %0", "tlbiel %0,1", %1)
290 : : "r" (va), "i" (CPU_FTR_ARCH_206)
291 : "memory");
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100292 break;
293 }
Balbir Singh04284912017-04-11 15:23:25 +1000294 trace_tlbie(0, 1, va, 0, 0, 0, 0);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100295
296}
297
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000298static inline void tlbie(unsigned long vpn, int psize, int apsize,
299 int ssize, int local)
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100300{
Ian Munsie4c6d9ac2014-10-08 19:55:00 +1100301 unsigned int use_local;
Matt Evans44ae3ab2011-04-06 19:48:50 +0000302 int lock_tlbie = !mmu_has_feature(MMU_FTR_LOCKLESS_TLBIE);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100303
Ian Munsie4c6d9ac2014-10-08 19:55:00 +1100304 use_local = local && mmu_has_feature(MMU_FTR_TLBIEL) && !cxl_ctx_in_use();
305
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100306 if (use_local)
307 use_local = mmu_psize_defs[psize].tlbiel;
308 if (lock_tlbie && !use_local)
Thomas Gleixner6b9c9b82010-02-18 02:22:35 +0000309 raw_spin_lock(&native_tlbie_lock);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100310 asm volatile("ptesync": : :"memory");
311 if (use_local) {
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000312 __tlbiel(vpn, psize, apsize, ssize);
Nicholas Piggin05504b42020-09-16 13:02:34 +1000313 ppc_after_tlbiel_barrier();
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100314 } else {
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000315 __tlbie(vpn, psize, apsize, ssize);
Aneesh Kumar K.V047e6572019-09-24 09:22:53 +0530316 fixup_tlbie_vpn(vpn, psize, apsize, ssize);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100317 asm volatile("eieio; tlbsync; ptesync": : :"memory");
318 }
319 if (lock_tlbie && !use_local)
Thomas Gleixner6b9c9b82010-02-18 02:22:35 +0000320 raw_spin_unlock(&native_tlbie_lock);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100321}
322
David Gibson8e561e72007-06-13 14:52:56 +1000323static inline void native_lock_hpte(struct hash_pte *hptep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
Anton Blanchard12f04f22013-09-23 12:04:36 +1000325 unsigned long *word = (unsigned long *)&hptep->v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
327 while (1) {
Anton Blanchard66d99b82010-02-10 01:03:06 +0000328 if (!test_and_set_bit_lock(HPTE_LOCK_BIT, word))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 break;
Nicholas Piggin4e287e62017-06-06 23:08:32 +1000330 spin_begin();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 while(test_bit(HPTE_LOCK_BIT, word))
Nicholas Piggin4e287e62017-06-06 23:08:32 +1000332 spin_cpu_relax();
333 spin_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 }
335}
336
David Gibson8e561e72007-06-13 14:52:56 +1000337static inline void native_unlock_hpte(struct hash_pte *hptep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
Anton Blanchard12f04f22013-09-23 12:04:36 +1000339 unsigned long *word = (unsigned long *)&hptep->v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Anton Blanchard66d99b82010-02-10 01:03:06 +0000341 clear_bit_unlock(HPTE_LOCK_BIT, word);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342}
343
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000344static long native_hpte_insert(unsigned long hpte_group, unsigned long vpn,
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100345 unsigned long pa, unsigned long rflags,
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000346 unsigned long vflags, int psize, int apsize, int ssize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
David Gibson8e561e72007-06-13 14:52:56 +1000348 struct hash_pte *hptep = htab_address + hpte_group;
David Gibson96e28442005-07-13 01:11:42 -0700349 unsigned long hpte_v, hpte_r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 int i;
351
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100352 if (!(vflags & HPTE_V_BOLTED)) {
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000353 DBG_LOW(" insert(group=%lx, vpn=%016lx, pa=%016lx,"
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100354 " rflags=%lx, vflags=%lx, psize=%d)\n",
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000355 hpte_group, vpn, pa, rflags, vflags, psize);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100356 }
357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 for (i = 0; i < HPTES_PER_GROUP; i++) {
Anton Blanchard12f04f22013-09-23 12:04:36 +1000359 if (! (be64_to_cpu(hptep->v) & HPTE_V_VALID)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 /* retry with lock held */
361 native_lock_hpte(hptep);
Anton Blanchard12f04f22013-09-23 12:04:36 +1000362 if (! (be64_to_cpu(hptep->v) & HPTE_V_VALID))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 break;
364 native_unlock_hpte(hptep);
365 }
366
367 hptep++;
368 }
369
370 if (i == HPTES_PER_GROUP)
371 return -1;
372
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000373 hpte_v = hpte_encode_v(vpn, psize, apsize, ssize) | vflags | HPTE_V_VALID;
Paul Mackerras6b243fc2016-11-11 16:55:03 +1100374 hpte_r = hpte_encode_r(pa, psize, apsize) | rflags;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100375
376 if (!(vflags & HPTE_V_BOLTED)) {
377 DBG_LOW(" i=%x hpte_v=%016lx, hpte_r=%016lx\n",
378 i, hpte_v, hpte_r);
379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Paul Mackerras6b243fc2016-11-11 16:55:03 +1100381 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
382 hpte_r = hpte_old_to_new_r(hpte_v, hpte_r);
383 hpte_v = hpte_old_to_new_v(hpte_v);
384 }
385
Anton Blanchard12f04f22013-09-23 12:04:36 +1000386 hptep->r = cpu_to_be64(hpte_r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 /* Guarantee the second dword is visible before the valid bit */
Kumar Gala74a0ba62007-07-09 23:49:09 -0500388 eieio();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 /*
390 * Now set the first dword including the valid bit
391 * NOTE: this also unlocks the hpte
392 */
Anton Blanchard12f04f22013-09-23 12:04:36 +1000393 hptep->v = cpu_to_be64(hpte_v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 __asm__ __volatile__ ("ptesync" : : : "memory");
396
David Gibson96e28442005-07-13 01:11:42 -0700397 return i | (!!(vflags & HPTE_V_SECONDARY) << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398}
399
400static long native_hpte_remove(unsigned long hpte_group)
401{
David Gibson8e561e72007-06-13 14:52:56 +1000402 struct hash_pte *hptep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 int i;
404 int slot_offset;
David Gibson96e28442005-07-13 01:11:42 -0700405 unsigned long hpte_v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100407 DBG_LOW(" remove(group=%lx)\n", hpte_group);
408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 /* pick a random entry to start at */
410 slot_offset = mftb() & 0x7;
411
412 for (i = 0; i < HPTES_PER_GROUP; i++) {
413 hptep = htab_address + hpte_group + slot_offset;
Anton Blanchard12f04f22013-09-23 12:04:36 +1000414 hpte_v = be64_to_cpu(hptep->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
David Gibson96e28442005-07-13 01:11:42 -0700416 if ((hpte_v & HPTE_V_VALID) && !(hpte_v & HPTE_V_BOLTED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 /* retry with lock held */
418 native_lock_hpte(hptep);
Anton Blanchard12f04f22013-09-23 12:04:36 +1000419 hpte_v = be64_to_cpu(hptep->v);
David Gibson96e28442005-07-13 01:11:42 -0700420 if ((hpte_v & HPTE_V_VALID)
421 && !(hpte_v & HPTE_V_BOLTED))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 break;
423 native_unlock_hpte(hptep);
424 }
425
426 slot_offset++;
427 slot_offset &= 0x7;
428 }
429
430 if (i == HPTES_PER_GROUP)
431 return -1;
432
433 /* Invalidate the hpte. NOTE: this also unlocks it */
David Gibson96e28442005-07-13 01:11:42 -0700434 hptep->v = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 return i;
437}
438
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100439static long native_hpte_updatepp(unsigned long slot, unsigned long newpp,
Aneesh Kumar K.Vdb3d8532013-06-20 14:30:13 +0530440 unsigned long vpn, int bpsize,
Aneesh Kumar K.Vaefa5682014-12-04 11:00:14 +0530441 int apsize, int ssize, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442{
David Gibson8e561e72007-06-13 14:52:56 +1000443 struct hash_pte *hptep = htab_address + slot;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100444 unsigned long hpte_v, want_v;
Aneesh Kumar K.Vaefa5682014-12-04 11:00:14 +0530445 int ret = 0, local = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Aneesh Kumar K.Vdb3d8532013-06-20 14:30:13 +0530447 want_v = hpte_encode_avpn(vpn, bpsize, ssize);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100448
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000449 DBG_LOW(" update(vpn=%016lx, avpnv=%016lx, group=%lx, newpp=%lx)",
450 vpn, want_v & HPTE_V_AVPN, slot, newpp);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100451
Aneesh Kumar K.Va8332802018-06-29 14:06:30 +0530452 hpte_v = hpte_get_old_v(hptep);
Aneesh Kumar K.V0608d692013-05-31 01:03:24 +0000453 /*
454 * We need to invalidate the TLB always because hpte_remove doesn't do
455 * a tlb invalidate. If a hash bucket gets full, we "evict" a more/less
456 * random entry from it. When we do that we don't invalidate the TLB
457 * (hpte_remove) because we assume the old translation is still
458 * technically "valid".
459 */
Aneesh Kumar K.Vdb3d8532013-06-20 14:30:13 +0530460 if (!HPTE_V_COMPARE(hpte_v, want_v) || !(hpte_v & HPTE_V_VALID)) {
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100461 DBG_LOW(" -> miss\n");
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100462 ret = -1;
463 } else {
Aneesh Kumar K.V0ec26982014-11-03 20:21:34 +0530464 native_lock_hpte(hptep);
465 /* recheck with locks held */
Aneesh Kumar K.Va8332802018-06-29 14:06:30 +0530466 hpte_v = hpte_get_old_v(hptep);
Aneesh Kumar K.V0ec26982014-11-03 20:21:34 +0530467 if (unlikely(!HPTE_V_COMPARE(hpte_v, want_v) ||
468 !(hpte_v & HPTE_V_VALID))) {
469 ret = -1;
470 } else {
471 DBG_LOW(" -> hit\n");
472 /* Update the HPTE */
473 hptep->r = cpu_to_be64((be64_to_cpu(hptep->r) &
Aneesh Kumar K.V8550e2f2016-06-08 19:55:55 +0530474 ~(HPTE_R_PPP | HPTE_R_N)) |
475 (newpp & (HPTE_R_PPP | HPTE_R_N |
Aneesh Kumar K.V0ec26982014-11-03 20:21:34 +0530476 HPTE_R_C)));
477 }
478 native_unlock_hpte(hptep);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100479 }
Aneesh Kumar K.Vaefa5682014-12-04 11:00:14 +0530480
481 if (flags & HPTE_LOCAL_UPDATE)
482 local = 1;
483 /*
484 * Ensure it is out of the tlb too if it is not a nohpte fault
485 */
486 if (!(flags & HPTE_NOHPTE_UPDATE))
487 tlbie(vpn, bpsize, apsize, ssize, local);
488
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100489 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490}
491
Aneesh Kumar K.Vd78d5da2019-10-24 15:05:42 +0530492static long __native_hpte_find(unsigned long want_v, unsigned long slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
David Gibson8e561e72007-06-13 14:52:56 +1000494 struct hash_pte *hptep;
Aneesh Kumar K.Vd78d5da2019-10-24 15:05:42 +0530495 unsigned long hpte_v;
Paul Mackerras1189be62007-10-11 20:37:10 +1000496 unsigned long i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Paul Mackerras1189be62007-10-11 20:37:10 +1000498 for (i = 0; i < HPTES_PER_GROUP; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Aneesh Kumar K.Va8332802018-06-29 14:06:30 +0530500 hptep = htab_address + slot;
501 hpte_v = hpte_get_old_v(hptep);
Paul Mackerras1189be62007-10-11 20:37:10 +1000502 if (HPTE_V_COMPARE(hpte_v, want_v) && (hpte_v & HPTE_V_VALID))
503 /* HPTE matches */
504 return slot;
505 ++slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 }
507
508 return -1;
509}
510
Aneesh Kumar K.Vd78d5da2019-10-24 15:05:42 +0530511static long native_hpte_find(unsigned long vpn, int psize, int ssize)
512{
513 unsigned long hpte_group;
514 unsigned long want_v;
515 unsigned long hash;
516 long slot;
517
518 hash = hpt_hash(vpn, mmu_psize_defs[psize].shift, ssize);
519 want_v = hpte_encode_avpn(vpn, psize, ssize);
520
521 /*
522 * We try to keep bolted entries always in primary hash
523 * But in some case we can find them in secondary too.
524 */
525 hpte_group = (hash & htab_hash_mask) * HPTES_PER_GROUP;
526 slot = __native_hpte_find(want_v, hpte_group);
527 if (slot < 0) {
528 /* Try in secondary */
529 hpte_group = (~hash & htab_hash_mask) * HPTES_PER_GROUP;
530 slot = __native_hpte_find(want_v, hpte_group);
531 if (slot < 0)
532 return -1;
533 }
534
535 return slot;
536}
537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538/*
539 * Update the page protection bits. Intended to be used to create
540 * guard pages for kernel data structures on pages which are bolted
541 * in the HPT. Assumes pages being operated on will not be stolen.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 *
543 * No need to lock here because we should be the only user.
544 */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100545static void native_hpte_updateboltedpp(unsigned long newpp, unsigned long ea,
Paul Mackerras1189be62007-10-11 20:37:10 +1000546 int psize, int ssize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000548 unsigned long vpn;
549 unsigned long vsid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 long slot;
David Gibson8e561e72007-06-13 14:52:56 +1000551 struct hash_pte *hptep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Paul Mackerras1189be62007-10-11 20:37:10 +1000553 vsid = get_kernel_vsid(ea, ssize);
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000554 vpn = hpt_vpn(ea, vsid, ssize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000556 slot = native_hpte_find(vpn, psize, ssize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 if (slot == -1)
558 panic("could not find page to bolt\n");
559 hptep = htab_address + slot;
560
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100561 /* Update the HPTE */
Anton Blanchard12f04f22013-09-23 12:04:36 +1000562 hptep->r = cpu_to_be64((be64_to_cpu(hptep->r) &
Aneesh Kumar K.V8550e2f2016-06-08 19:55:55 +0530563 ~(HPTE_R_PPP | HPTE_R_N)) |
564 (newpp & (HPTE_R_PPP | HPTE_R_N)));
Aneesh Kumar K.Vdb3d8532013-06-20 14:30:13 +0530565 /*
566 * Ensure it is out of the tlb too. Bolted entries base and
567 * actual page size will be same.
568 */
569 tlbie(vpn, psize, psize, ssize, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570}
571
Anton Blanchard1b644f52017-06-28 11:32:35 +1000572/*
573 * Remove a bolted kernel entry. Memory hotplug uses this.
574 *
575 * No need to lock here because we should be the only user.
576 */
577static int native_hpte_removebolted(unsigned long ea, int psize, int ssize)
578{
579 unsigned long vpn;
580 unsigned long vsid;
581 long slot;
582 struct hash_pte *hptep;
583
584 vsid = get_kernel_vsid(ea, ssize);
585 vpn = hpt_vpn(ea, vsid, ssize);
586
587 slot = native_hpte_find(vpn, psize, ssize);
588 if (slot == -1)
589 return -ENOENT;
590
591 hptep = htab_address + slot;
592
593 VM_WARN_ON(!(be64_to_cpu(hptep->v) & HPTE_V_BOLTED));
594
595 /* Invalidate the hpte */
596 hptep->v = 0;
597
598 /* Invalidate the TLB */
599 tlbie(vpn, psize, psize, ssize, 0);
600 return 0;
601}
602
603
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000604static void native_hpte_invalidate(unsigned long slot, unsigned long vpn,
Aneesh Kumar K.Vdb3d8532013-06-20 14:30:13 +0530605 int bpsize, int apsize, int ssize, int local)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
David Gibson8e561e72007-06-13 14:52:56 +1000607 struct hash_pte *hptep = htab_address + slot;
David Gibson96e28442005-07-13 01:11:42 -0700608 unsigned long hpte_v;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100609 unsigned long want_v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
612 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000614 DBG_LOW(" invalidate(vpn=%016lx, hash: %lx)\n", vpn, slot);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100615
Aneesh Kumar K.Vdb3d8532013-06-20 14:30:13 +0530616 want_v = hpte_encode_avpn(vpn, bpsize, ssize);
Aneesh Kumar K.Va8332802018-06-29 14:06:30 +0530617 hpte_v = hpte_get_old_v(hptep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Aneesh Kumar K.V27d89592018-06-29 14:06:31 +0530619 if (HPTE_V_COMPARE(hpte_v, want_v) && (hpte_v & HPTE_V_VALID)) {
620 native_lock_hpte(hptep);
621 /* recheck with locks held */
622 hpte_v = hpte_get_old_v(hptep);
623
624 if (HPTE_V_COMPARE(hpte_v, want_v) && (hpte_v & HPTE_V_VALID))
625 /* Invalidate the hpte. NOTE: this also unlocks it */
626 hptep->v = 0;
627 else
628 native_unlock_hpte(hptep);
629 }
Aneesh Kumar K.V0608d692013-05-31 01:03:24 +0000630 /*
631 * We need to invalidate the TLB always because hpte_remove doesn't do
632 * a tlb invalidate. If a hash bucket gets full, we "evict" a more/less
633 * random entry from it. When we do that we don't invalidate the TLB
634 * (hpte_remove) because we assume the old translation is still
635 * technically "valid".
636 */
Aneesh Kumar K.Vdb3d8532013-06-20 14:30:13 +0530637 tlbie(vpn, bpsize, apsize, ssize, local);
638
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100639 local_irq_restore(flags);
640}
641
Aneesh Kumar K.Ve34aa032015-12-01 09:06:53 +0530642#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Aneesh Kumar K.Vfa1f8ae2014-08-13 12:31:58 +0530643static void native_hugepage_invalidate(unsigned long vsid,
644 unsigned long addr,
Aneesh Kumar K.V1a527282013-06-20 14:30:27 +0530645 unsigned char *hpte_slot_array,
Aneesh Kumar K.Vd557b092014-11-02 21:15:28 +0530646 int psize, int ssize, int local)
Aneesh Kumar K.V1a527282013-06-20 14:30:27 +0530647{
Aneesh Kumar K.V969b7b22014-08-13 12:32:01 +0530648 int i;
Aneesh Kumar K.V1a527282013-06-20 14:30:27 +0530649 struct hash_pte *hptep;
650 int actual_psize = MMU_PAGE_16M;
651 unsigned int max_hpte_count, valid;
652 unsigned long flags, s_addr = addr;
653 unsigned long hpte_v, want_v, shift;
Aneesh Kumar K.Vfa1f8ae2014-08-13 12:31:58 +0530654 unsigned long hidx, vpn = 0, hash, slot;
Aneesh Kumar K.V1a527282013-06-20 14:30:27 +0530655
656 shift = mmu_psize_defs[psize].shift;
657 max_hpte_count = 1U << (PMD_SHIFT - shift);
658
659 local_irq_save(flags);
660 for (i = 0; i < max_hpte_count; i++) {
661 valid = hpte_valid(hpte_slot_array, i);
662 if (!valid)
663 continue;
664 hidx = hpte_hash_index(hpte_slot_array, i);
665
666 /* get the vpn */
667 addr = s_addr + (i * (1ul << shift));
Aneesh Kumar K.V1a527282013-06-20 14:30:27 +0530668 vpn = hpt_vpn(addr, vsid, ssize);
669 hash = hpt_hash(vpn, shift, ssize);
670 if (hidx & _PTEIDX_SECONDARY)
671 hash = ~hash;
672
673 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
674 slot += hidx & _PTEIDX_GROUP_IX;
675
676 hptep = htab_address + slot;
677 want_v = hpte_encode_avpn(vpn, psize, ssize);
Aneesh Kumar K.Va8332802018-06-29 14:06:30 +0530678 hpte_v = hpte_get_old_v(hptep);
Aneesh Kumar K.V1a527282013-06-20 14:30:27 +0530679
680 /* Even if we miss, we need to invalidate the TLB */
Aneesh Kumar K.V27d89592018-06-29 14:06:31 +0530681 if (HPTE_V_COMPARE(hpte_v, want_v) && (hpte_v & HPTE_V_VALID)) {
682 /* recheck with locks held */
683 native_lock_hpte(hptep);
684 hpte_v = hpte_get_old_v(hptep);
685
686 if (HPTE_V_COMPARE(hpte_v, want_v) && (hpte_v & HPTE_V_VALID)) {
687 /*
688 * Invalidate the hpte. NOTE: this also unlocks it
689 */
690
691 hptep->v = 0;
692 } else
693 native_unlock_hpte(hptep);
694 }
Aneesh Kumar K.V969b7b22014-08-13 12:32:01 +0530695 /*
696 * We need to do tlb invalidate for all the address, tlbie
697 * instruction compares entry_VA in tlb with the VA specified
698 * here
699 */
Aneesh Kumar K.Vd557b092014-11-02 21:15:28 +0530700 tlbie(vpn, psize, actual_psize, ssize, local);
Aneesh Kumar K.V1a527282013-06-20 14:30:27 +0530701 }
Aneesh Kumar K.V1a527282013-06-20 14:30:27 +0530702 local_irq_restore(flags);
703}
Aneesh Kumar K.Ve34aa032015-12-01 09:06:53 +0530704#else
705static void native_hugepage_invalidate(unsigned long vsid,
706 unsigned long addr,
707 unsigned char *hpte_slot_array,
708 int psize, int ssize, int local)
709{
710 WARN(1, "%s called without THP support\n", __func__);
711}
712#endif
Aneesh Kumar K.V1a527282013-06-20 14:30:27 +0530713
David Gibson8e561e72007-06-13 14:52:56 +1000714static void hpte_decode(struct hash_pte *hpte, unsigned long slot,
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000715 int *psize, int *apsize, int *ssize, unsigned long *vpn)
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100716{
Aneesh Kumar K.Vdcda2872012-09-10 02:52:49 +0000717 unsigned long avpn, pteg, vpi;
Anton Blanchard12f04f22013-09-23 12:04:36 +1000718 unsigned long hpte_v = be64_to_cpu(hpte->v);
719 unsigned long hpte_r = be64_to_cpu(hpte->r);
Aneesh Kumar K.Vdcda2872012-09-10 02:52:49 +0000720 unsigned long vsid, seg_off;
Aneesh Kumar K.V7e74c392013-04-28 09:37:36 +0000721 int size, a_size, shift;
722 /* Look at the 8 bit LP value */
Anton Blanchard12f04f22013-09-23 12:04:36 +1000723 unsigned int lp = (hpte_r >> LP_SHIFT) & ((1 << LP_BITS) - 1);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100724
Paul Mackerras6b243fc2016-11-11 16:55:03 +1100725 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
726 hpte_v = hpte_new_to_old_v(hpte_v, hpte_r);
727 hpte_r = hpte_new_to_old_r(hpte_r);
728 }
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000729 if (!(hpte_v & HPTE_V_LARGE)) {
730 size = MMU_PAGE_4K;
731 a_size = MMU_PAGE_4K;
732 } else {
Paul Mackerras0eeede02016-09-02 17:20:43 +1000733 size = hpte_page_sizes[lp] & 0xf;
734 a_size = hpte_page_sizes[lp] >> 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 }
Paul Mackerras2454c7e2007-05-10 15:28:44 +1000736 /* This works for all page sizes, and for 256M and 1T segments */
Paul Mackerras6b243fc2016-11-11 16:55:03 +1100737 *ssize = hpte_v >> HPTE_V_SSIZE_SHIFT;
Aneesh Kumar K.Vdcda2872012-09-10 02:52:49 +0000738 shift = mmu_psize_defs[size].shift;
739
740 avpn = (HPTE_V_AVPN_VAL(hpte_v) & ~mmu_psize_defs[size].avpnm);
741 pteg = slot / HPTES_PER_GROUP;
742 if (hpte_v & HPTE_V_SECONDARY)
743 pteg = ~pteg;
744
745 switch (*ssize) {
746 case MMU_SEGSIZE_256M:
747 /* We only have 28 - 23 bits of seg_off in avpn */
748 seg_off = (avpn & 0x1f) << 23;
749 vsid = avpn >> 5;
750 /* We can find more bits from the pteg value */
751 if (shift < 23) {
752 vpi = (vsid ^ pteg) & htab_hash_mask;
753 seg_off |= vpi << shift;
754 }
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000755 *vpn = vsid << (SID_SHIFT - VPN_SHIFT) | seg_off >> VPN_SHIFT;
Aneesh Kumar K.V83383b72013-07-03 13:50:03 +0530756 break;
Aneesh Kumar K.Vdcda2872012-09-10 02:52:49 +0000757 case MMU_SEGSIZE_1T:
758 /* We only have 40 - 23 bits of seg_off in avpn */
759 seg_off = (avpn & 0x1ffff) << 23;
760 vsid = avpn >> 17;
761 if (shift < 23) {
762 vpi = (vsid ^ (vsid << 25) ^ pteg) & htab_hash_mask;
763 seg_off |= vpi << shift;
764 }
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000765 *vpn = vsid << (SID_SHIFT_1T - VPN_SHIFT) | seg_off >> VPN_SHIFT;
Aneesh Kumar K.V83383b72013-07-03 13:50:03 +0530766 break;
Aneesh Kumar K.Vdcda2872012-09-10 02:52:49 +0000767 default:
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000768 *vpn = size = 0;
Aneesh Kumar K.Vdcda2872012-09-10 02:52:49 +0000769 }
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000770 *psize = size;
771 *apsize = a_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772}
773
R Sharadaf4c82d52005-06-25 14:58:08 -0700774/*
775 * clear all mappings on kexec. All cpus are in real mode (or they will
776 * be when they isi), and we are the only one left. We rely on our kernel
777 * mapping being 0xC0's and the hardware ignoring those two real bits.
778 *
Cyril Burfdf880a2015-10-08 11:04:26 +1100779 * This must be called with interrupts disabled.
780 *
781 * Taking the native_tlbie_lock is unsafe here due to the possibility of
782 * lockdep being on. On pre POWER5 hardware, not taking the lock could
783 * cause deadlock. POWER5 and newer not taking the lock is fine. This only
784 * gets called during boot before secondary CPUs have come up and during
785 * crashdump and all bets are off anyway.
786 *
R Sharadaf4c82d52005-06-25 14:58:08 -0700787 * TODO: add batching support when enabled. remember, no dynamic memory here,
Michael Ellerman027dfac2016-06-01 16:34:37 +1000788 * although there is the control page available...
R Sharadaf4c82d52005-06-25 14:58:08 -0700789 */
Hari Bathini8119cef2021-07-14 18:17:58 +0530790static notrace void native_hpte_clear(void)
R Sharadaf4c82d52005-06-25 14:58:08 -0700791{
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000792 unsigned long vpn = 0;
Cyril Burfdf880a2015-10-08 11:04:26 +1100793 unsigned long slot, slots;
David Gibson8e561e72007-06-13 14:52:56 +1000794 struct hash_pte *hptep = htab_address;
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000795 unsigned long hpte_v;
R Sharadaf4c82d52005-06-25 14:58:08 -0700796 unsigned long pteg_count;
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000797 int psize, apsize, ssize;
R Sharadaf4c82d52005-06-25 14:58:08 -0700798
799 pteg_count = htab_hash_mask + 1;
800
R Sharadaf4c82d52005-06-25 14:58:08 -0700801 slots = pteg_count * HPTES_PER_GROUP;
802
803 for (slot = 0; slot < slots; slot++, hptep++) {
804 /*
805 * we could lock the pte here, but we are the only cpu
806 * running, right? and for crash dump, we probably
807 * don't want to wait for a maybe bad cpu.
808 */
Anton Blanchard12f04f22013-09-23 12:04:36 +1000809 hpte_v = be64_to_cpu(hptep->v);
R Sharadaf4c82d52005-06-25 14:58:08 -0700810
R Sharada47f78a42006-02-22 21:43:08 +0530811 /*
Cyril Burfdf880a2015-10-08 11:04:26 +1100812 * Call __tlbie() here rather than tlbie() since we can't take the
813 * native_tlbie_lock.
R Sharada47f78a42006-02-22 21:43:08 +0530814 */
David Gibson96e28442005-07-13 01:11:42 -0700815 if (hpte_v & HPTE_V_VALID) {
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000816 hpte_decode(hptep, slot, &psize, &apsize, &ssize, &vpn);
David Gibson96e28442005-07-13 01:11:42 -0700817 hptep->v = 0;
Mahesh Salgaonkara3961f82017-11-22 23:02:07 +0530818 ___tlbie(vpn, psize, apsize, ssize);
R Sharadaf4c82d52005-06-25 14:58:08 -0700819 }
820 }
821
R Sharada47f78a42006-02-22 21:43:08 +0530822 asm volatile("eieio; tlbsync; ptesync":::"memory");
R Sharadaf4c82d52005-06-25 14:58:08 -0700823}
824
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100825/*
826 * Batched hash table flush, we batch the tlbie's to avoid taking/releasing
827 * the lock all the time
828 */
Benjamin Herrenschmidt61b1a942005-09-20 13:52:50 +1000829static void native_flush_hash_range(unsigned long number, int local)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830{
Aneesh Kumar K.Va5d4b582018-03-23 10:26:27 +0530831 unsigned long vpn = 0;
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000832 unsigned long hash, index, hidx, shift, slot;
David Gibson8e561e72007-06-13 14:52:56 +1000833 struct hash_pte *hptep;
David Gibson96e28442005-07-13 01:11:42 -0700834 unsigned long hpte_v;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100835 unsigned long want_v;
836 unsigned long flags;
837 real_pte_t pte;
Christoph Lameter69111ba2014-10-21 15:23:25 -0500838 struct ppc64_tlb_batch *batch = this_cpu_ptr(&ppc64_tlb_batch);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100839 unsigned long psize = batch->psize;
Paul Mackerras1189be62007-10-11 20:37:10 +1000840 int ssize = batch->ssize;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100841 int i;
Frederic Barrat88b1bf722017-03-29 19:19:42 +0200842 unsigned int use_local;
843
844 use_local = local && mmu_has_feature(MMU_FTR_TLBIEL) &&
845 mmu_psize_defs[psize].tlbiel && !cxl_ctx_in_use();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
847 local_irq_save(flags);
848
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 for (i = 0; i < number; i++) {
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000850 vpn = batch->vpn[i];
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100851 pte = batch->pte[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000853 pte_iterate_hashed_subpages(pte, psize, vpn, index, shift) {
854 hash = hpt_hash(vpn, shift, ssize);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100855 hidx = __rpte_to_hidx(pte, index);
856 if (hidx & _PTEIDX_SECONDARY)
857 hash = ~hash;
858 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
859 slot += hidx & _PTEIDX_GROUP_IX;
860 hptep = htab_address + slot;
Aneesh Kumar K.V74f227b2013-04-28 09:37:34 +0000861 want_v = hpte_encode_avpn(vpn, psize, ssize);
Aneesh Kumar K.V27d89592018-06-29 14:06:31 +0530862 hpte_v = hpte_get_old_v(hptep);
863
864 if (!HPTE_V_COMPARE(hpte_v, want_v) || !(hpte_v & HPTE_V_VALID))
865 continue;
866 /* lock and try again */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100867 native_lock_hpte(hptep);
Aneesh Kumar K.Va8332802018-06-29 14:06:30 +0530868 hpte_v = hpte_get_old_v(hptep);
Aneesh Kumar K.V27d89592018-06-29 14:06:31 +0530869
870 if (!HPTE_V_COMPARE(hpte_v, want_v) || !(hpte_v & HPTE_V_VALID))
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100871 native_unlock_hpte(hptep);
872 else
873 hptep->v = 0;
Aneesh Kumar K.V27d89592018-06-29 14:06:31 +0530874
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100875 } pte_iterate_hashed_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 }
877
Frederic Barrat88b1bf722017-03-29 19:19:42 +0200878 if (use_local) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 asm volatile("ptesync":::"memory");
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100880 for (i = 0; i < number; i++) {
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000881 vpn = batch->vpn[i];
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100882 pte = batch->pte[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000884 pte_iterate_hashed_subpages(pte, psize,
885 vpn, index, shift) {
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000886 __tlbiel(vpn, psize, psize, ssize);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100887 } pte_iterate_hashed_end();
888 }
Nicholas Piggin05504b42020-09-16 13:02:34 +1000889 ppc_after_tlbiel_barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 } else {
Matt Evans44ae3ab2011-04-06 19:48:50 +0000891 int lock_tlbie = !mmu_has_feature(MMU_FTR_LOCKLESS_TLBIE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
893 if (lock_tlbie)
Thomas Gleixner6b9c9b82010-02-18 02:22:35 +0000894 raw_spin_lock(&native_tlbie_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
896 asm volatile("ptesync":::"memory");
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100897 for (i = 0; i < number; i++) {
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000898 vpn = batch->vpn[i];
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100899 pte = batch->pte[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000901 pte_iterate_hashed_subpages(pte, psize,
902 vpn, index, shift) {
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000903 __tlbie(vpn, psize, psize, ssize);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100904 } pte_iterate_hashed_end();
905 }
Aneesh Kumar K.Va5d4b582018-03-23 10:26:27 +0530906 /*
907 * Just do one more with the last used values.
908 */
Aneesh Kumar K.V047e6572019-09-24 09:22:53 +0530909 fixup_tlbie_vpn(vpn, psize, psize, ssize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 asm volatile("eieio; tlbsync; ptesync":::"memory");
911
912 if (lock_tlbie)
Thomas Gleixner6b9c9b82010-02-18 02:22:35 +0000913 raw_spin_unlock(&native_tlbie_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 }
915
916 local_irq_restore(flags);
917}
918
Michael Ellerman7d0daae2006-06-23 18:16:38 +1000919void __init hpte_init_native(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
Benjamin Herrenschmidt70257762016-07-05 15:03:58 +1000921 mmu_hash_ops.hpte_invalidate = native_hpte_invalidate;
922 mmu_hash_ops.hpte_updatepp = native_hpte_updatepp;
923 mmu_hash_ops.hpte_updateboltedpp = native_hpte_updateboltedpp;
Anton Blanchard1b644f52017-06-28 11:32:35 +1000924 mmu_hash_ops.hpte_removebolted = native_hpte_removebolted;
Benjamin Herrenschmidt70257762016-07-05 15:03:58 +1000925 mmu_hash_ops.hpte_insert = native_hpte_insert;
926 mmu_hash_ops.hpte_remove = native_hpte_remove;
927 mmu_hash_ops.hpte_clear_all = native_hpte_clear;
928 mmu_hash_ops.flush_hash_range = native_flush_hash_range;
929 mmu_hash_ops.hugepage_invalidate = native_hugepage_invalidate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930}