]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - arch/powerpc/mm/book3s64/hash_native.c
UBUNTU: Ubuntu-5.4.0-117.132
[mirror_ubuntu-focal-kernel.git] / arch / powerpc / mm / book3s64 / hash_native.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
1da177e4
LT
2/*
3 * native hashtable management.
4 *
5 * SMP scalability work:
6 * Copyright (C) 2001 Anton Blanchard <anton@au.ibm.com>, IBM
1da177e4 7 */
3c726f8d
BH
8
9#undef DEBUG_LOW
10
1da177e4
LT
11#include <linux/spinlock.h>
12#include <linux/bitops.h>
beacc6da 13#include <linux/of.h>
4e287e65 14#include <linux/processor.h>
1da177e4
LT
15#include <linux/threads.h>
16#include <linux/smp.h>
17
1da177e4
LT
18#include <asm/machdep.h>
19#include <asm/mmu.h>
20#include <asm/mmu_context.h>
21#include <asm/pgtable.h>
0428491c 22#include <asm/trace.h>
1da177e4
LT
23#include <asm/tlb.h>
24#include <asm/cputable.h>
3c726f8d 25#include <asm/udbg.h>
71bf08b6 26#include <asm/kexec.h>
60dbf438 27#include <asm/ppc-opcode.h>
2c86cd18 28#include <asm/feature-fixups.h>
3c726f8d 29
ec249dd8 30#include <misc/cxl-base.h>
4c6d9acc 31
3c726f8d
BH
32#ifdef DEBUG_LOW
33#define DBG_LOW(fmt...) udbg_printf(fmt)
34#else
35#define DBG_LOW(fmt...)
36#endif
1da177e4 37
12f04f2b 38#ifdef __BIG_ENDIAN__
1da177e4 39#define HPTE_LOCK_BIT 3
12f04f2b
AB
40#else
41#define HPTE_LOCK_BIT (56+3)
42#endif
1da177e4 43
d667edc0 44static DEFINE_RAW_SPINLOCK(native_tlbie_lock);
1da177e4 45
d4748276
NP
46static 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 */
6d3ca7e7 59static __always_inline void tlbiel_hash_set_isa300(unsigned int set, unsigned int is,
d4748276
NP
60 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)
71 : : "r"(rb), "r"(rs), "i"(ric), "i"(prs), "r"(r)
72 : "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
85 asm volatile("ptesync": : :"memory");
86}
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 /*
95 * Flush the first set of the TLB, and any caching of partition table
96 * entries. Then flush the remaining sets of the TLB. Hash mode uses
97 * partition scoped TLB translations.
98 */
99 tlbiel_hash_set_isa300(0, is, 0, 2, 0);
100 for (set = 1; set < num_sets; set++)
101 tlbiel_hash_set_isa300(set, is, 0, 0, 0);
102
103 /*
104 * Now invalidate the process table cache.
105 *
106 * From ISA v3.0B p. 1078:
107 * The following forms are invalid.
108 * * PRS=1, R=0, and RIC!=2 (The only process-scoped
109 * HPT caching is of the Process Table.)
110 */
111 tlbiel_hash_set_isa300(0, is, 0, 2, 1);
112
113 asm volatile("ptesync": : :"memory");
bc276ecb 114
fe7946ce 115 asm volatile(PPC_ISA_3_0_INVALIDATE_ERAT "; isync" : : :"memory");
d4748276
NP
116}
117
118void hash__tlbiel_all(unsigned int action)
119{
120 unsigned int is;
121
122 switch (action) {
123 case TLB_INVAL_SCOPE_GLOBAL:
124 is = 3;
125 break;
126 case TLB_INVAL_SCOPE_LPID:
127 is = 2;
128 break;
129 default:
130 BUG();
131 }
132
133 if (early_cpu_has_feature(CPU_FTR_ARCH_300))
134 tlbiel_all_isa300(POWER9_TLB_SETS_HASH, is);
135 else if (early_cpu_has_feature(CPU_FTR_ARCH_207S))
136 tlbiel_all_isa206(POWER8_TLB_SETS, is);
137 else if (early_cpu_has_feature(CPU_FTR_ARCH_206))
138 tlbiel_all_isa206(POWER7_TLB_SETS, is);
139 else
140 WARN(1, "%s called on pre-POWER7 CPU\n", __func__);
d4748276
NP
141}
142
a3961f82
MS
143static inline unsigned long ___tlbie(unsigned long vpn, int psize,
144 int apsize, int ssize)
3c726f8d 145{
5524a27d 146 unsigned long va;
3c726f8d 147 unsigned int penc;
de640959 148 unsigned long sllp;
3c726f8d 149
5524a27d
AK
150 /*
151 * We need 14 to 65 bits of va for a tlibe of 4K page
152 * With vpn we ignore the lower VPN_SHIFT bits already.
153 * And top two bits are already ignored because we can
027dfac6 154 * only accomodate 76 bits in a 64 bit vpn with a VPN_SHIFT
5524a27d
AK
155 * of 12.
156 */
157 va = vpn << VPN_SHIFT;
158 /*
159 * clear top 16 bits of 64bit va, non SLS segment
160 * Older versions of the architecture (2.02 and earler) require the
161 * masking of the top 16 bits.
162 */
accfad7d
AK
163 if (mmu_has_feature(MMU_FTR_TLBIE_CROP_VA))
164 va &= ~(0xffffULL << 48);
3c726f8d
BH
165
166 switch (psize) {
167 case MMU_PAGE_4K:
1f6aaacc
AK
168 /* clear out bits after (52) [0....52.....63] */
169 va &= ~((1ul << (64 - 52)) - 1);
1189be65 170 va |= ssize << 8;
138ee7ee 171 sllp = get_sllp_encoding(apsize);
de640959 172 va |= sllp << 5;
a32e252f 173 asm volatile(ASM_FTR_IFCLR("tlbie %0,0", PPC_TLBIE(%1,%0), %2)
969391c5 174 : : "r" (va), "r"(0), "i" (CPU_FTR_ARCH_206)
60dbf438 175 : "memory");
3c726f8d
BH
176 break;
177 default:
5524a27d 178 /* We need 14 to 14 + i bits of va */
b1022fbd 179 penc = mmu_psize_defs[psize].penc[apsize];
1f6aaacc 180 va &= ~((1ul << mmu_psize_defs[apsize].shift) - 1);
19242b24 181 va |= penc << 12;
1189be65 182 va |= ssize << 8;
29ef7a3e
AK
183 /*
184 * AVAL bits:
185 * We don't need all the bits, but rest of the bits
186 * must be ignored by the processor.
187 * vpn cover upto 65 bits of va. (0...65) and we need
188 * 58..64 bits of va.
189 */
190 va |= (vpn & 0xfe); /* AVAL */
60dbf438 191 va |= 1; /* L */
a32e252f 192 asm volatile(ASM_FTR_IFCLR("tlbie %0,1", PPC_TLBIE(%1,%0), %2)
969391c5 193 : : "r" (va), "r"(0), "i" (CPU_FTR_ARCH_206)
60dbf438 194 : "memory");
3c726f8d
BH
195 break;
196 }
a3961f82
MS
197 return va;
198}
199
047e6575
AK
200static inline void fixup_tlbie_vpn(unsigned long vpn, int psize,
201 int apsize, int ssize)
a5d4b589 202{
047e6575
AK
203 if (cpu_has_feature(CPU_FTR_P9_TLBIE_ERAT_BUG)) {
204 /* Radix flush for a hash guest */
205
206 unsigned long rb,rs,prs,r,ric;
207
208 rb = PPC_BIT(52); /* IS = 2 */
209 rs = 0; /* lpid = 0 */
210 prs = 0; /* partition scoped */
211 r = 1; /* radix format */
212 ric = 0; /* RIC_FLSUH_TLB */
213
214 /*
215 * Need the extra ptesync to make sure we don't
216 * re-order the tlbie
217 */
218 asm volatile("ptesync": : :"memory");
219 asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
220 : : "r"(rb), "i"(r), "i"(prs),
221 "i"(ric), "r"(rs) : "memory");
222 }
223
224
09ce98ca 225 if (cpu_has_feature(CPU_FTR_P9_TLBIE_STQ_BUG)) {
a5d4b589
AK
226 /* Need the extra ptesync to ensure we don't reorder tlbie*/
227 asm volatile("ptesync": : :"memory");
228 ___tlbie(vpn, psize, apsize, ssize);
229 }
230}
231
a3961f82
MS
232static inline void __tlbie(unsigned long vpn, int psize, int apsize, int ssize)
233{
234 unsigned long rb;
235
236 rb = ___tlbie(vpn, psize, apsize, ssize);
237 trace_tlbie(0, 0, rb, 0, 0, 0, 0);
3c726f8d
BH
238}
239
b1022fbd 240static inline void __tlbiel(unsigned long vpn, int psize, int apsize, int ssize)
3c726f8d 241{
5524a27d 242 unsigned long va;
3c726f8d 243 unsigned int penc;
de640959 244 unsigned long sllp;
3c726f8d 245
5524a27d
AK
246 /* VPN_SHIFT can be atmost 12 */
247 va = vpn << VPN_SHIFT;
248 /*
249 * clear top 16 bits of 64 bit va, non SLS segment
250 * Older versions of the architecture (2.02 and earler) require the
251 * masking of the top 16 bits.
252 */
accfad7d
AK
253 if (mmu_has_feature(MMU_FTR_TLBIE_CROP_VA))
254 va &= ~(0xffffULL << 48);
3c726f8d
BH
255
256 switch (psize) {
257 case MMU_PAGE_4K:
1f6aaacc
AK
258 /* clear out bits after(52) [0....52.....63] */
259 va &= ~((1ul << (64 - 52)) - 1);
1189be65 260 va |= ssize << 8;
138ee7ee 261 sllp = get_sllp_encoding(apsize);
de640959 262 va |= sllp << 5;
f923efbc
BS
263 asm volatile(ASM_FTR_IFSET("tlbiel %0", "tlbiel %0,0", %1)
264 : : "r" (va), "i" (CPU_FTR_ARCH_206)
265 : "memory");
3c726f8d
BH
266 break;
267 default:
5524a27d 268 /* We need 14 to 14 + i bits of va */
b1022fbd 269 penc = mmu_psize_defs[psize].penc[apsize];
1f6aaacc 270 va &= ~((1ul << mmu_psize_defs[apsize].shift) - 1);
19242b24 271 va |= penc << 12;
1189be65 272 va |= ssize << 8;
29ef7a3e
AK
273 /*
274 * AVAL bits:
275 * We don't need all the bits, but rest of the bits
276 * must be ignored by the processor.
277 * vpn cover upto 65 bits of va. (0...65) and we need
278 * 58..64 bits of va.
279 */
280 va |= (vpn & 0xfe);
60dbf438 281 va |= 1; /* L */
f923efbc
BS
282 asm volatile(ASM_FTR_IFSET("tlbiel %0", "tlbiel %0,1", %1)
283 : : "r" (va), "i" (CPU_FTR_ARCH_206)
284 : "memory");
3c726f8d
BH
285 break;
286 }
0428491c 287 trace_tlbie(0, 1, va, 0, 0, 0, 0);
3c726f8d
BH
288
289}
290
b1022fbd
AK
291static inline void tlbie(unsigned long vpn, int psize, int apsize,
292 int ssize, int local)
3c726f8d 293{
4c6d9acc 294 unsigned int use_local;
44ae3ab3 295 int lock_tlbie = !mmu_has_feature(MMU_FTR_LOCKLESS_TLBIE);
3c726f8d 296
4c6d9acc
IM
297 use_local = local && mmu_has_feature(MMU_FTR_TLBIEL) && !cxl_ctx_in_use();
298
3c726f8d
BH
299 if (use_local)
300 use_local = mmu_psize_defs[psize].tlbiel;
301 if (lock_tlbie && !use_local)
6b9c9b8a 302 raw_spin_lock(&native_tlbie_lock);
3c726f8d
BH
303 asm volatile("ptesync": : :"memory");
304 if (use_local) {
b1022fbd 305 __tlbiel(vpn, psize, apsize, ssize);
3c726f8d
BH
306 asm volatile("ptesync": : :"memory");
307 } else {
b1022fbd 308 __tlbie(vpn, psize, apsize, ssize);
047e6575 309 fixup_tlbie_vpn(vpn, psize, apsize, ssize);
3c726f8d
BH
310 asm volatile("eieio; tlbsync; ptesync": : :"memory");
311 }
312 if (lock_tlbie && !use_local)
6b9c9b8a 313 raw_spin_unlock(&native_tlbie_lock);
3c726f8d
BH
314}
315
8e561e7e 316static inline void native_lock_hpte(struct hash_pte *hptep)
1da177e4 317{
12f04f2b 318 unsigned long *word = (unsigned long *)&hptep->v;
1da177e4
LT
319
320 while (1) {
66d99b88 321 if (!test_and_set_bit_lock(HPTE_LOCK_BIT, word))
1da177e4 322 break;
4e287e65 323 spin_begin();
1da177e4 324 while(test_bit(HPTE_LOCK_BIT, word))
4e287e65
NP
325 spin_cpu_relax();
326 spin_end();
1da177e4
LT
327 }
328}
329
8e561e7e 330static inline void native_unlock_hpte(struct hash_pte *hptep)
1da177e4 331{
12f04f2b 332 unsigned long *word = (unsigned long *)&hptep->v;
1da177e4 333
66d99b88 334 clear_bit_unlock(HPTE_LOCK_BIT, word);
1da177e4
LT
335}
336
5524a27d 337static long native_hpte_insert(unsigned long hpte_group, unsigned long vpn,
3c726f8d 338 unsigned long pa, unsigned long rflags,
b1022fbd 339 unsigned long vflags, int psize, int apsize, int ssize)
1da177e4 340{
8e561e7e 341 struct hash_pte *hptep = htab_address + hpte_group;
96e28449 342 unsigned long hpte_v, hpte_r;
1da177e4
LT
343 int i;
344
3c726f8d 345 if (!(vflags & HPTE_V_BOLTED)) {
5524a27d 346 DBG_LOW(" insert(group=%lx, vpn=%016lx, pa=%016lx,"
3c726f8d 347 " rflags=%lx, vflags=%lx, psize=%d)\n",
5524a27d 348 hpte_group, vpn, pa, rflags, vflags, psize);
3c726f8d
BH
349 }
350
1da177e4 351 for (i = 0; i < HPTES_PER_GROUP; i++) {
12f04f2b 352 if (! (be64_to_cpu(hptep->v) & HPTE_V_VALID)) {
1da177e4
LT
353 /* retry with lock held */
354 native_lock_hpte(hptep);
12f04f2b 355 if (! (be64_to_cpu(hptep->v) & HPTE_V_VALID))
1da177e4
LT
356 break;
357 native_unlock_hpte(hptep);
358 }
359
360 hptep++;
361 }
362
363 if (i == HPTES_PER_GROUP)
364 return -1;
365
b1022fbd 366 hpte_v = hpte_encode_v(vpn, psize, apsize, ssize) | vflags | HPTE_V_VALID;
6b243fcf 367 hpte_r = hpte_encode_r(pa, psize, apsize) | rflags;
3c726f8d
BH
368
369 if (!(vflags & HPTE_V_BOLTED)) {
370 DBG_LOW(" i=%x hpte_v=%016lx, hpte_r=%016lx\n",
371 i, hpte_v, hpte_r);
372 }
1da177e4 373
6b243fcf
PM
374 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
375 hpte_r = hpte_old_to_new_r(hpte_v, hpte_r);
376 hpte_v = hpte_old_to_new_v(hpte_v);
377 }
378
12f04f2b 379 hptep->r = cpu_to_be64(hpte_r);
1da177e4 380 /* Guarantee the second dword is visible before the valid bit */
74a0ba61 381 eieio();
1da177e4
LT
382 /*
383 * Now set the first dword including the valid bit
384 * NOTE: this also unlocks the hpte
385 */
12f04f2b 386 hptep->v = cpu_to_be64(hpte_v);
1da177e4
LT
387
388 __asm__ __volatile__ ("ptesync" : : : "memory");
389
96e28449 390 return i | (!!(vflags & HPTE_V_SECONDARY) << 3);
1da177e4
LT
391}
392
393static long native_hpte_remove(unsigned long hpte_group)
394{
8e561e7e 395 struct hash_pte *hptep;
1da177e4
LT
396 int i;
397 int slot_offset;
96e28449 398 unsigned long hpte_v;
1da177e4 399
3c726f8d
BH
400 DBG_LOW(" remove(group=%lx)\n", hpte_group);
401
1da177e4
LT
402 /* pick a random entry to start at */
403 slot_offset = mftb() & 0x7;
404
405 for (i = 0; i < HPTES_PER_GROUP; i++) {
406 hptep = htab_address + hpte_group + slot_offset;
12f04f2b 407 hpte_v = be64_to_cpu(hptep->v);
1da177e4 408
96e28449 409 if ((hpte_v & HPTE_V_VALID) && !(hpte_v & HPTE_V_BOLTED)) {
1da177e4
LT
410 /* retry with lock held */
411 native_lock_hpte(hptep);
12f04f2b 412 hpte_v = be64_to_cpu(hptep->v);
96e28449
DG
413 if ((hpte_v & HPTE_V_VALID)
414 && !(hpte_v & HPTE_V_BOLTED))
1da177e4
LT
415 break;
416 native_unlock_hpte(hptep);
417 }
418
419 slot_offset++;
420 slot_offset &= 0x7;
421 }
422
423 if (i == HPTES_PER_GROUP)
424 return -1;
425
426 /* Invalidate the hpte. NOTE: this also unlocks it */
96e28449 427 hptep->v = 0;
1da177e4
LT
428
429 return i;
430}
431
3c726f8d 432static long native_hpte_updatepp(unsigned long slot, unsigned long newpp,
db3d8534 433 unsigned long vpn, int bpsize,
aefa5688 434 int apsize, int ssize, unsigned long flags)
1da177e4 435{
8e561e7e 436 struct hash_pte *hptep = htab_address + slot;
3c726f8d 437 unsigned long hpte_v, want_v;
aefa5688 438 int ret = 0, local = 0;
3c726f8d 439
db3d8534 440 want_v = hpte_encode_avpn(vpn, bpsize, ssize);
3c726f8d 441
5524a27d
AK
442 DBG_LOW(" update(vpn=%016lx, avpnv=%016lx, group=%lx, newpp=%lx)",
443 vpn, want_v & HPTE_V_AVPN, slot, newpp);
3c726f8d 444
a833280b 445 hpte_v = hpte_get_old_v(hptep);
0608d692
AK
446 /*
447 * We need to invalidate the TLB always because hpte_remove doesn't do
448 * a tlb invalidate. If a hash bucket gets full, we "evict" a more/less
449 * random entry from it. When we do that we don't invalidate the TLB
450 * (hpte_remove) because we assume the old translation is still
451 * technically "valid".
452 */
db3d8534 453 if (!HPTE_V_COMPARE(hpte_v, want_v) || !(hpte_v & HPTE_V_VALID)) {
3c726f8d 454 DBG_LOW(" -> miss\n");
3c726f8d
BH
455 ret = -1;
456 } else {
0ec2698f
AK
457 native_lock_hpte(hptep);
458 /* recheck with locks held */
a833280b 459 hpte_v = hpte_get_old_v(hptep);
0ec2698f
AK
460 if (unlikely(!HPTE_V_COMPARE(hpte_v, want_v) ||
461 !(hpte_v & HPTE_V_VALID))) {
462 ret = -1;
463 } else {
464 DBG_LOW(" -> hit\n");
465 /* Update the HPTE */
466 hptep->r = cpu_to_be64((be64_to_cpu(hptep->r) &
8550e2fa
AK
467 ~(HPTE_R_PPP | HPTE_R_N)) |
468 (newpp & (HPTE_R_PPP | HPTE_R_N |
0ec2698f
AK
469 HPTE_R_C)));
470 }
471 native_unlock_hpte(hptep);
3c726f8d 472 }
aefa5688
AK
473
474 if (flags & HPTE_LOCAL_UPDATE)
475 local = 1;
476 /*
477 * Ensure it is out of the tlb too if it is not a nohpte fault
478 */
479 if (!(flags & HPTE_NOHPTE_UPDATE))
480 tlbie(vpn, bpsize, apsize, ssize, local);
481
3c726f8d 482 return ret;
1da177e4
LT
483}
484
5524a27d 485static long native_hpte_find(unsigned long vpn, int psize, int ssize)
1da177e4 486{
8e561e7e 487 struct hash_pte *hptep;
1da177e4 488 unsigned long hash;
1189be65 489 unsigned long i;
1da177e4 490 long slot;
3c726f8d 491 unsigned long want_v, hpte_v;
1da177e4 492
5524a27d 493 hash = hpt_hash(vpn, mmu_psize_defs[psize].shift, ssize);
74f227b2 494 want_v = hpte_encode_avpn(vpn, psize, ssize);
1da177e4 495
1189be65
PM
496 /* Bolted mappings are only ever in the primary group */
497 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
498 for (i = 0; i < HPTES_PER_GROUP; i++) {
1da177e4 499
a833280b
AK
500 hptep = htab_address + slot;
501 hpte_v = hpte_get_old_v(hptep);
1189be65
PM
502 if (HPTE_V_COMPARE(hpte_v, want_v) && (hpte_v & HPTE_V_VALID))
503 /* HPTE matches */
504 return slot;
505 ++slot;
1da177e4
LT
506 }
507
508 return -1;
509}
510
1da177e4
LT
511/*
512 * Update the page protection bits. Intended to be used to create
513 * guard pages for kernel data structures on pages which are bolted
514 * in the HPT. Assumes pages being operated on will not be stolen.
1da177e4
LT
515 *
516 * No need to lock here because we should be the only user.
517 */
3c726f8d 518static void native_hpte_updateboltedpp(unsigned long newpp, unsigned long ea,
1189be65 519 int psize, int ssize)
1da177e4 520{
5524a27d
AK
521 unsigned long vpn;
522 unsigned long vsid;
1da177e4 523 long slot;
8e561e7e 524 struct hash_pte *hptep;
1da177e4 525
1189be65 526 vsid = get_kernel_vsid(ea, ssize);
5524a27d 527 vpn = hpt_vpn(ea, vsid, ssize);
1da177e4 528
5524a27d 529 slot = native_hpte_find(vpn, psize, ssize);
1da177e4
LT
530 if (slot == -1)
531 panic("could not find page to bolt\n");
532 hptep = htab_address + slot;
533
3c726f8d 534 /* Update the HPTE */
12f04f2b 535 hptep->r = cpu_to_be64((be64_to_cpu(hptep->r) &
8550e2fa
AK
536 ~(HPTE_R_PPP | HPTE_R_N)) |
537 (newpp & (HPTE_R_PPP | HPTE_R_N)));
db3d8534
AK
538 /*
539 * Ensure it is out of the tlb too. Bolted entries base and
540 * actual page size will be same.
541 */
542 tlbie(vpn, psize, psize, ssize, 0);
1da177e4
LT
543}
544
1b644f57
AB
545/*
546 * Remove a bolted kernel entry. Memory hotplug uses this.
547 *
548 * No need to lock here because we should be the only user.
549 */
550static int native_hpte_removebolted(unsigned long ea, int psize, int ssize)
551{
552 unsigned long vpn;
553 unsigned long vsid;
554 long slot;
555 struct hash_pte *hptep;
556
557 vsid = get_kernel_vsid(ea, ssize);
558 vpn = hpt_vpn(ea, vsid, ssize);
559
560 slot = native_hpte_find(vpn, psize, ssize);
561 if (slot == -1)
562 return -ENOENT;
563
564 hptep = htab_address + slot;
565
566 VM_WARN_ON(!(be64_to_cpu(hptep->v) & HPTE_V_BOLTED));
567
568 /* Invalidate the hpte */
569 hptep->v = 0;
570
571 /* Invalidate the TLB */
572 tlbie(vpn, psize, psize, ssize, 0);
573 return 0;
574}
575
576
5524a27d 577static void native_hpte_invalidate(unsigned long slot, unsigned long vpn,
db3d8534 578 int bpsize, int apsize, int ssize, int local)
1da177e4 579{
8e561e7e 580 struct hash_pte *hptep = htab_address + slot;
96e28449 581 unsigned long hpte_v;
3c726f8d 582 unsigned long want_v;
1da177e4 583 unsigned long flags;
1da177e4
LT
584
585 local_irq_save(flags);
1da177e4 586
5524a27d 587 DBG_LOW(" invalidate(vpn=%016lx, hash: %lx)\n", vpn, slot);
3c726f8d 588
db3d8534 589 want_v = hpte_encode_avpn(vpn, bpsize, ssize);
a833280b 590 hpte_v = hpte_get_old_v(hptep);
1da177e4 591
27d8959d
AK
592 if (HPTE_V_COMPARE(hpte_v, want_v) && (hpte_v & HPTE_V_VALID)) {
593 native_lock_hpte(hptep);
594 /* recheck with locks held */
595 hpte_v = hpte_get_old_v(hptep);
596
597 if (HPTE_V_COMPARE(hpte_v, want_v) && (hpte_v & HPTE_V_VALID))
598 /* Invalidate the hpte. NOTE: this also unlocks it */
599 hptep->v = 0;
600 else
601 native_unlock_hpte(hptep);
602 }
0608d692
AK
603 /*
604 * We need to invalidate the TLB always because hpte_remove doesn't do
605 * a tlb invalidate. If a hash bucket gets full, we "evict" a more/less
606 * random entry from it. When we do that we don't invalidate the TLB
607 * (hpte_remove) because we assume the old translation is still
608 * technically "valid".
609 */
db3d8534
AK
610 tlbie(vpn, bpsize, apsize, ssize, local);
611
1da177e4
LT
612 local_irq_restore(flags);
613}
614
e34aa03c 615#ifdef CONFIG_TRANSPARENT_HUGEPAGE
fa1f8ae8
AK
616static void native_hugepage_invalidate(unsigned long vsid,
617 unsigned long addr,
1a527286 618 unsigned char *hpte_slot_array,
d557b098 619 int psize, int ssize, int local)
1a527286 620{
969b7b20 621 int i;
1a527286
AK
622 struct hash_pte *hptep;
623 int actual_psize = MMU_PAGE_16M;
624 unsigned int max_hpte_count, valid;
625 unsigned long flags, s_addr = addr;
626 unsigned long hpte_v, want_v, shift;
fa1f8ae8 627 unsigned long hidx, vpn = 0, hash, slot;
1a527286
AK
628
629 shift = mmu_psize_defs[psize].shift;
630 max_hpte_count = 1U << (PMD_SHIFT - shift);
631
632 local_irq_save(flags);
633 for (i = 0; i < max_hpte_count; i++) {
634 valid = hpte_valid(hpte_slot_array, i);
635 if (!valid)
636 continue;
637 hidx = hpte_hash_index(hpte_slot_array, i);
638
639 /* get the vpn */
640 addr = s_addr + (i * (1ul << shift));
1a527286
AK
641 vpn = hpt_vpn(addr, vsid, ssize);
642 hash = hpt_hash(vpn, shift, ssize);
643 if (hidx & _PTEIDX_SECONDARY)
644 hash = ~hash;
645
646 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
647 slot += hidx & _PTEIDX_GROUP_IX;
648
649 hptep = htab_address + slot;
650 want_v = hpte_encode_avpn(vpn, psize, ssize);
a833280b 651 hpte_v = hpte_get_old_v(hptep);
1a527286
AK
652
653 /* Even if we miss, we need to invalidate the TLB */
27d8959d
AK
654 if (HPTE_V_COMPARE(hpte_v, want_v) && (hpte_v & HPTE_V_VALID)) {
655 /* recheck with locks held */
656 native_lock_hpte(hptep);
657 hpte_v = hpte_get_old_v(hptep);
658
659 if (HPTE_V_COMPARE(hpte_v, want_v) && (hpte_v & HPTE_V_VALID)) {
660 /*
661 * Invalidate the hpte. NOTE: this also unlocks it
662 */
663
664 hptep->v = 0;
665 } else
666 native_unlock_hpte(hptep);
667 }
969b7b20
AK
668 /*
669 * We need to do tlb invalidate for all the address, tlbie
670 * instruction compares entry_VA in tlb with the VA specified
671 * here
672 */
d557b098 673 tlbie(vpn, psize, actual_psize, ssize, local);
1a527286 674 }
1a527286
AK
675 local_irq_restore(flags);
676}
e34aa03c
AK
677#else
678static void native_hugepage_invalidate(unsigned long vsid,
679 unsigned long addr,
680 unsigned char *hpte_slot_array,
681 int psize, int ssize, int local)
682{
683 WARN(1, "%s called without THP support\n", __func__);
684}
685#endif
1a527286 686
8e561e7e 687static void hpte_decode(struct hash_pte *hpte, unsigned long slot,
b1022fbd 688 int *psize, int *apsize, int *ssize, unsigned long *vpn)
71bf08b6 689{
dcda287a 690 unsigned long avpn, pteg, vpi;
12f04f2b
AB
691 unsigned long hpte_v = be64_to_cpu(hpte->v);
692 unsigned long hpte_r = be64_to_cpu(hpte->r);
dcda287a 693 unsigned long vsid, seg_off;
7e74c392
AK
694 int size, a_size, shift;
695 /* Look at the 8 bit LP value */
12f04f2b 696 unsigned int lp = (hpte_r >> LP_SHIFT) & ((1 << LP_BITS) - 1);
71bf08b6 697
6b243fcf
PM
698 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
699 hpte_v = hpte_new_to_old_v(hpte_v, hpte_r);
700 hpte_r = hpte_new_to_old_r(hpte_r);
701 }
b1022fbd
AK
702 if (!(hpte_v & HPTE_V_LARGE)) {
703 size = MMU_PAGE_4K;
704 a_size = MMU_PAGE_4K;
705 } else {
0eeede0c
PM
706 size = hpte_page_sizes[lp] & 0xf;
707 a_size = hpte_page_sizes[lp] >> 4;
71bf08b6 708 }
2454c7e9 709 /* This works for all page sizes, and for 256M and 1T segments */
6b243fcf 710 *ssize = hpte_v >> HPTE_V_SSIZE_SHIFT;
71bf08b6 711 shift = mmu_psize_defs[size].shift;
71bf08b6 712
dcda287a
AK
713 avpn = (HPTE_V_AVPN_VAL(hpte_v) & ~mmu_psize_defs[size].avpnm);
714 pteg = slot / HPTES_PER_GROUP;
715 if (hpte_v & HPTE_V_SECONDARY)
716 pteg = ~pteg;
717
718 switch (*ssize) {
719 case MMU_SEGSIZE_256M:
720 /* We only have 28 - 23 bits of seg_off in avpn */
721 seg_off = (avpn & 0x1f) << 23;
722 vsid = avpn >> 5;
723 /* We can find more bits from the pteg value */
724 if (shift < 23) {
725 vpi = (vsid ^ pteg) & htab_hash_mask;
726 seg_off |= vpi << shift;
727 }
5524a27d 728 *vpn = vsid << (SID_SHIFT - VPN_SHIFT) | seg_off >> VPN_SHIFT;
83383b73 729 break;
dcda287a
AK
730 case MMU_SEGSIZE_1T:
731 /* We only have 40 - 23 bits of seg_off in avpn */
732 seg_off = (avpn & 0x1ffff) << 23;
733 vsid = avpn >> 17;
734 if (shift < 23) {
2454c7e9 735 vpi = (vsid ^ (vsid << 25) ^ pteg) & htab_hash_mask;
dcda287a 736 seg_off |= vpi << shift;
71bf08b6 737 }
5524a27d 738 *vpn = vsid << (SID_SHIFT_1T - VPN_SHIFT) | seg_off >> VPN_SHIFT;
83383b73 739 break;
dcda287a 740 default:
5524a27d 741 *vpn = size = 0;
3c726f8d 742 }
b1022fbd
AK
743 *psize = size;
744 *apsize = a_size;
3c726f8d
BH
745}
746
f4c82d51
S
747/*
748 * clear all mappings on kexec. All cpus are in real mode (or they will
749 * be when they isi), and we are the only one left. We rely on our kernel
750 * mapping being 0xC0's and the hardware ignoring those two real bits.
751 *
fdf880a6
CB
752 * This must be called with interrupts disabled.
753 *
754 * Taking the native_tlbie_lock is unsafe here due to the possibility of
755 * lockdep being on. On pre POWER5 hardware, not taking the lock could
756 * cause deadlock. POWER5 and newer not taking the lock is fine. This only
757 * gets called during boot before secondary CPUs have come up and during
758 * crashdump and all bets are off anyway.
759 *
f4c82d51 760 * TODO: add batching support when enabled. remember, no dynamic memory here,
027dfac6 761 * although there is the control page available...
f4c82d51
S
762 */
763static void native_hpte_clear(void)
764{
5524a27d 765 unsigned long vpn = 0;
fdf880a6 766 unsigned long slot, slots;
8e561e7e 767 struct hash_pte *hptep = htab_address;
5524a27d 768 unsigned long hpte_v;
f4c82d51 769 unsigned long pteg_count;
b1022fbd 770 int psize, apsize, ssize;
f4c82d51
S
771
772 pteg_count = htab_hash_mask + 1;
773
f4c82d51
S
774 slots = pteg_count * HPTES_PER_GROUP;
775
776 for (slot = 0; slot < slots; slot++, hptep++) {
777 /*
778 * we could lock the pte here, but we are the only cpu
779 * running, right? and for crash dump, we probably
780 * don't want to wait for a maybe bad cpu.
781 */
12f04f2b 782 hpte_v = be64_to_cpu(hptep->v);
f4c82d51 783
47f78a49 784 /*
fdf880a6
CB
785 * Call __tlbie() here rather than tlbie() since we can't take the
786 * native_tlbie_lock.
47f78a49 787 */
96e28449 788 if (hpte_v & HPTE_V_VALID) {
b1022fbd 789 hpte_decode(hptep, slot, &psize, &apsize, &ssize, &vpn);
96e28449 790 hptep->v = 0;
a3961f82 791 ___tlbie(vpn, psize, apsize, ssize);
f4c82d51
S
792 }
793 }
794
47f78a49 795 asm volatile("eieio; tlbsync; ptesync":::"memory");
f4c82d51
S
796}
797
3c726f8d
BH
798/*
799 * Batched hash table flush, we batch the tlbie's to avoid taking/releasing
800 * the lock all the time
801 */
61b1a942 802static void native_flush_hash_range(unsigned long number, int local)
1da177e4 803{
a5d4b589 804 unsigned long vpn = 0;
5524a27d 805 unsigned long hash, index, hidx, shift, slot;
8e561e7e 806 struct hash_pte *hptep;
96e28449 807 unsigned long hpte_v;
3c726f8d
BH
808 unsigned long want_v;
809 unsigned long flags;
810 real_pte_t pte;
69111bac 811 struct ppc64_tlb_batch *batch = this_cpu_ptr(&ppc64_tlb_batch);
3c726f8d 812 unsigned long psize = batch->psize;
1189be65 813 int ssize = batch->ssize;
3c726f8d 814 int i;
88b1bf72
FB
815 unsigned int use_local;
816
817 use_local = local && mmu_has_feature(MMU_FTR_TLBIEL) &&
818 mmu_psize_defs[psize].tlbiel && !cxl_ctx_in_use();
1da177e4
LT
819
820 local_irq_save(flags);
821
1da177e4 822 for (i = 0; i < number; i++) {
5524a27d 823 vpn = batch->vpn[i];
3c726f8d
BH
824 pte = batch->pte[i];
825
5524a27d
AK
826 pte_iterate_hashed_subpages(pte, psize, vpn, index, shift) {
827 hash = hpt_hash(vpn, shift, ssize);
3c726f8d
BH
828 hidx = __rpte_to_hidx(pte, index);
829 if (hidx & _PTEIDX_SECONDARY)
830 hash = ~hash;
831 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
832 slot += hidx & _PTEIDX_GROUP_IX;
833 hptep = htab_address + slot;
74f227b2 834 want_v = hpte_encode_avpn(vpn, psize, ssize);
27d8959d
AK
835 hpte_v = hpte_get_old_v(hptep);
836
837 if (!HPTE_V_COMPARE(hpte_v, want_v) || !(hpte_v & HPTE_V_VALID))
838 continue;
839 /* lock and try again */
3c726f8d 840 native_lock_hpte(hptep);
a833280b 841 hpte_v = hpte_get_old_v(hptep);
27d8959d
AK
842
843 if (!HPTE_V_COMPARE(hpte_v, want_v) || !(hpte_v & HPTE_V_VALID))
3c726f8d
BH
844 native_unlock_hpte(hptep);
845 else
846 hptep->v = 0;
27d8959d 847
3c726f8d 848 } pte_iterate_hashed_end();
1da177e4
LT
849 }
850
88b1bf72 851 if (use_local) {
1da177e4 852 asm volatile("ptesync":::"memory");
3c726f8d 853 for (i = 0; i < number; i++) {
5524a27d 854 vpn = batch->vpn[i];
3c726f8d
BH
855 pte = batch->pte[i];
856
5524a27d
AK
857 pte_iterate_hashed_subpages(pte, psize,
858 vpn, index, shift) {
b1022fbd 859 __tlbiel(vpn, psize, psize, ssize);
3c726f8d
BH
860 } pte_iterate_hashed_end();
861 }
1da177e4
LT
862 asm volatile("ptesync":::"memory");
863 } else {
44ae3ab3 864 int lock_tlbie = !mmu_has_feature(MMU_FTR_LOCKLESS_TLBIE);
1da177e4
LT
865
866 if (lock_tlbie)
6b9c9b8a 867 raw_spin_lock(&native_tlbie_lock);
1da177e4
LT
868
869 asm volatile("ptesync":::"memory");
3c726f8d 870 for (i = 0; i < number; i++) {
5524a27d 871 vpn = batch->vpn[i];
3c726f8d
BH
872 pte = batch->pte[i];
873
5524a27d
AK
874 pte_iterate_hashed_subpages(pte, psize,
875 vpn, index, shift) {
b1022fbd 876 __tlbie(vpn, psize, psize, ssize);
3c726f8d
BH
877 } pte_iterate_hashed_end();
878 }
a5d4b589
AK
879 /*
880 * Just do one more with the last used values.
881 */
047e6575 882 fixup_tlbie_vpn(vpn, psize, psize, ssize);
1da177e4
LT
883 asm volatile("eieio; tlbsync; ptesync":::"memory");
884
885 if (lock_tlbie)
6b9c9b8a 886 raw_spin_unlock(&native_tlbie_lock);
1da177e4
LT
887 }
888
889 local_irq_restore(flags);
890}
891
7d0daae4 892void __init hpte_init_native(void)
1da177e4 893{
7025776e
BH
894 mmu_hash_ops.hpte_invalidate = native_hpte_invalidate;
895 mmu_hash_ops.hpte_updatepp = native_hpte_updatepp;
896 mmu_hash_ops.hpte_updateboltedpp = native_hpte_updateboltedpp;
1b644f57 897 mmu_hash_ops.hpte_removebolted = native_hpte_removebolted;
7025776e
BH
898 mmu_hash_ops.hpte_insert = native_hpte_insert;
899 mmu_hash_ops.hpte_remove = native_hpte_remove;
900 mmu_hash_ops.hpte_clear_all = native_hpte_clear;
901 mmu_hash_ops.flush_hash_range = native_flush_hash_range;
902 mmu_hash_ops.hugepage_invalidate = native_hugepage_invalidate;
1da177e4 903}