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