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