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