blob: 640cf566e98653ab43c06744b6cf9ef76622fa1c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +110012
13#undef DEBUG_LOW
14
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/spinlock.h>
16#include <linux/bitops.h>
Michael Ellermanbeacc6d2012-07-25 21:20:03 +000017#include <linux/of.h>
Nicholas Piggin4e287e62017-06-06 23:08:32 +100018#include <linux/processor.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/threads.h>
20#include <linux/smp.h>
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#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>
Balbir Singh04284912017-04-11 15:23:25 +100027#include <asm/trace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <asm/tlb.h>
29#include <asm/cputable.h>
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +110030#include <asm/udbg.h>
Luke Browning71bf08b2007-05-03 00:19:11 +100031#include <asm/kexec.h>
Milton Miller60dbf432009-04-29 20:58:01 +000032#include <asm/ppc-opcode.h>
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +110033
Michael Neulingec249dd2015-05-27 16:07:16 +100034#include <misc/cxl-base.h>
Ian Munsie4c6d9ac2014-10-08 19:55:00 +110035
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +110036#ifdef DEBUG_LOW
37#define DBG_LOW(fmt...) udbg_printf(fmt)
38#else
39#define DBG_LOW(fmt...)
40#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Anton Blanchard12f04f22013-09-23 12:04:36 +100042#ifdef __BIG_ENDIAN__
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#define HPTE_LOCK_BIT 3
Anton Blanchard12f04f22013-09-23 12:04:36 +100044#else
45#define HPTE_LOCK_BIT (56+3)
46#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Paul Mackerras9e368f22011-06-29 00:40:08 +000048DEFINE_RAW_SPINLOCK(native_tlbie_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Mahesh Salgaonkara3961f82017-11-22 23:02:07 +053050static inline unsigned long ___tlbie(unsigned long vpn, int psize,
51 int apsize, int ssize)
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +110052{
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +000053 unsigned long va;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +110054 unsigned int penc;
Aneesh Kumar K.Vde640952013-07-04 10:34:45 +053055 unsigned long sllp;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +110056
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +000057 /*
58 * We need 14 to 65 bits of va for a tlibe of 4K page
59 * With vpn we ignore the lower VPN_SHIFT bits already.
60 * And top two bits are already ignored because we can
Michael Ellerman027dfac2016-06-01 16:34:37 +100061 * only accomodate 76 bits in a 64 bit vpn with a VPN_SHIFT
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +000062 * of 12.
63 */
64 va = vpn << VPN_SHIFT;
65 /*
66 * clear top 16 bits of 64bit va, non SLS segment
67 * Older versions of the architecture (2.02 and earler) require the
68 * masking of the top 16 bits.
69 */
Aneesh Kumar K.Vaccfad72016-07-13 15:05:24 +053070 if (mmu_has_feature(MMU_FTR_TLBIE_CROP_VA))
71 va &= ~(0xffffULL << 48);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +110072
73 switch (psize) {
74 case MMU_PAGE_4K:
Aneesh Kumar K.V1f6aaac2013-04-28 09:37:39 +000075 /* clear out bits after (52) [0....52.....63] */
76 va &= ~((1ul << (64 - 52)) - 1);
Paul Mackerras1189be62007-10-11 20:37:10 +100077 va |= ssize << 8;
Aneesh Kumar K.V138ee7e2016-07-13 15:06:37 +053078 sllp = get_sllp_encoding(apsize);
Aneesh Kumar K.Vde640952013-07-04 10:34:45 +053079 va |= sllp << 5;
Michael Neulinga32e2522011-04-06 18:23:29 +000080 asm volatile(ASM_FTR_IFCLR("tlbie %0,0", PPC_TLBIE(%1,%0), %2)
Paul Mackerras969391c2011-06-29 00:26:11 +000081 : : "r" (va), "r"(0), "i" (CPU_FTR_ARCH_206)
Milton Miller60dbf432009-04-29 20:58:01 +000082 : "memory");
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +110083 break;
84 default:
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +000085 /* We need 14 to 14 + i bits of va */
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +000086 penc = mmu_psize_defs[psize].penc[apsize];
Aneesh Kumar K.V1f6aaac2013-04-28 09:37:39 +000087 va &= ~((1ul << mmu_psize_defs[apsize].shift) - 1);
Arnd Bergmann19242b22006-06-15 21:15:44 +100088 va |= penc << 12;
Paul Mackerras1189be62007-10-11 20:37:10 +100089 va |= ssize << 8;
Aneesh Kumar K.V29ef7a32014-04-21 10:37:36 +053090 /*
91 * AVAL bits:
92 * We don't need all the bits, but rest of the bits
93 * must be ignored by the processor.
94 * vpn cover upto 65 bits of va. (0...65) and we need
95 * 58..64 bits of va.
96 */
97 va |= (vpn & 0xfe); /* AVAL */
Milton Miller60dbf432009-04-29 20:58:01 +000098 va |= 1; /* L */
Michael Neulinga32e2522011-04-06 18:23:29 +000099 asm volatile(ASM_FTR_IFCLR("tlbie %0,1", PPC_TLBIE(%1,%0), %2)
Paul Mackerras969391c2011-06-29 00:26:11 +0000100 : : "r" (va), "r"(0), "i" (CPU_FTR_ARCH_206)
Milton Miller60dbf432009-04-29 20:58:01 +0000101 : "memory");
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100102 break;
103 }
Mahesh Salgaonkara3961f82017-11-22 23:02:07 +0530104 return va;
105}
106
107static inline void __tlbie(unsigned long vpn, int psize, int apsize, int ssize)
108{
109 unsigned long rb;
110
111 rb = ___tlbie(vpn, psize, apsize, ssize);
112 trace_tlbie(0, 0, rb, 0, 0, 0, 0);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100113}
114
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000115static inline void __tlbiel(unsigned long vpn, int psize, int apsize, int ssize)
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100116{
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000117 unsigned long va;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100118 unsigned int penc;
Aneesh Kumar K.Vde640952013-07-04 10:34:45 +0530119 unsigned long sllp;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100120
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000121 /* VPN_SHIFT can be atmost 12 */
122 va = vpn << VPN_SHIFT;
123 /*
124 * clear top 16 bits of 64 bit va, non SLS segment
125 * Older versions of the architecture (2.02 and earler) require the
126 * masking of the top 16 bits.
127 */
Aneesh Kumar K.Vaccfad72016-07-13 15:05:24 +0530128 if (mmu_has_feature(MMU_FTR_TLBIE_CROP_VA))
129 va &= ~(0xffffULL << 48);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100130
131 switch (psize) {
132 case MMU_PAGE_4K:
Aneesh Kumar K.V1f6aaac2013-04-28 09:37:39 +0000133 /* clear out bits after(52) [0....52.....63] */
134 va &= ~((1ul << (64 - 52)) - 1);
Paul Mackerras1189be62007-10-11 20:37:10 +1000135 va |= ssize << 8;
Aneesh Kumar K.V138ee7e2016-07-13 15:06:37 +0530136 sllp = get_sllp_encoding(apsize);
Aneesh Kumar K.Vde640952013-07-04 10:34:45 +0530137 va |= sllp << 5;
Balbir Singhf923efb2016-09-28 17:25:52 +1000138 asm volatile(ASM_FTR_IFSET("tlbiel %0", "tlbiel %0,0", %1)
139 : : "r" (va), "i" (CPU_FTR_ARCH_206)
140 : "memory");
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100141 break;
142 default:
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000143 /* We need 14 to 14 + i bits of va */
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000144 penc = mmu_psize_defs[psize].penc[apsize];
Aneesh Kumar K.V1f6aaac2013-04-28 09:37:39 +0000145 va &= ~((1ul << mmu_psize_defs[apsize].shift) - 1);
Arnd Bergmann19242b22006-06-15 21:15:44 +1000146 va |= penc << 12;
Paul Mackerras1189be62007-10-11 20:37:10 +1000147 va |= ssize << 8;
Aneesh Kumar K.V29ef7a32014-04-21 10:37:36 +0530148 /*
149 * AVAL bits:
150 * We don't need all the bits, but rest of the bits
151 * must be ignored by the processor.
152 * vpn cover upto 65 bits of va. (0...65) and we need
153 * 58..64 bits of va.
154 */
155 va |= (vpn & 0xfe);
Milton Miller60dbf432009-04-29 20:58:01 +0000156 va |= 1; /* L */
Balbir Singhf923efb2016-09-28 17:25:52 +1000157 asm volatile(ASM_FTR_IFSET("tlbiel %0", "tlbiel %0,1", %1)
158 : : "r" (va), "i" (CPU_FTR_ARCH_206)
159 : "memory");
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100160 break;
161 }
Balbir Singh04284912017-04-11 15:23:25 +1000162 trace_tlbie(0, 1, va, 0, 0, 0, 0);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100163
164}
165
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000166static inline void tlbie(unsigned long vpn, int psize, int apsize,
167 int ssize, int local)
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100168{
Ian Munsie4c6d9ac2014-10-08 19:55:00 +1100169 unsigned int use_local;
Matt Evans44ae3ab2011-04-06 19:48:50 +0000170 int lock_tlbie = !mmu_has_feature(MMU_FTR_LOCKLESS_TLBIE);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100171
Ian Munsie4c6d9ac2014-10-08 19:55:00 +1100172 use_local = local && mmu_has_feature(MMU_FTR_TLBIEL) && !cxl_ctx_in_use();
173
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100174 if (use_local)
175 use_local = mmu_psize_defs[psize].tlbiel;
176 if (lock_tlbie && !use_local)
Thomas Gleixner6b9c9b82010-02-18 02:22:35 +0000177 raw_spin_lock(&native_tlbie_lock);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100178 asm volatile("ptesync": : :"memory");
179 if (use_local) {
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000180 __tlbiel(vpn, psize, apsize, ssize);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100181 asm volatile("ptesync": : :"memory");
182 } else {
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000183 __tlbie(vpn, psize, apsize, ssize);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100184 asm volatile("eieio; tlbsync; ptesync": : :"memory");
185 }
186 if (lock_tlbie && !use_local)
Thomas Gleixner6b9c9b82010-02-18 02:22:35 +0000187 raw_spin_unlock(&native_tlbie_lock);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100188}
189
David Gibson8e561e72007-06-13 14:52:56 +1000190static inline void native_lock_hpte(struct hash_pte *hptep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
Anton Blanchard12f04f22013-09-23 12:04:36 +1000192 unsigned long *word = (unsigned long *)&hptep->v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194 while (1) {
Anton Blanchard66d99b82010-02-10 01:03:06 +0000195 if (!test_and_set_bit_lock(HPTE_LOCK_BIT, word))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 break;
Nicholas Piggin4e287e62017-06-06 23:08:32 +1000197 spin_begin();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 while(test_bit(HPTE_LOCK_BIT, word))
Nicholas Piggin4e287e62017-06-06 23:08:32 +1000199 spin_cpu_relax();
200 spin_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 }
202}
203
David Gibson8e561e72007-06-13 14:52:56 +1000204static inline void native_unlock_hpte(struct hash_pte *hptep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
Anton Blanchard12f04f22013-09-23 12:04:36 +1000206 unsigned long *word = (unsigned long *)&hptep->v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Anton Blanchard66d99b82010-02-10 01:03:06 +0000208 clear_bit_unlock(HPTE_LOCK_BIT, word);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209}
210
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000211static long native_hpte_insert(unsigned long hpte_group, unsigned long vpn,
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100212 unsigned long pa, unsigned long rflags,
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000213 unsigned long vflags, int psize, int apsize, int ssize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
David Gibson8e561e72007-06-13 14:52:56 +1000215 struct hash_pte *hptep = htab_address + hpte_group;
David Gibson96e28442005-07-13 01:11:42 -0700216 unsigned long hpte_v, hpte_r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 int i;
218
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100219 if (!(vflags & HPTE_V_BOLTED)) {
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000220 DBG_LOW(" insert(group=%lx, vpn=%016lx, pa=%016lx,"
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100221 " rflags=%lx, vflags=%lx, psize=%d)\n",
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000222 hpte_group, vpn, pa, rflags, vflags, psize);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100223 }
224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 for (i = 0; i < HPTES_PER_GROUP; i++) {
Anton Blanchard12f04f22013-09-23 12:04:36 +1000226 if (! (be64_to_cpu(hptep->v) & HPTE_V_VALID)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 /* retry with lock held */
228 native_lock_hpte(hptep);
Anton Blanchard12f04f22013-09-23 12:04:36 +1000229 if (! (be64_to_cpu(hptep->v) & HPTE_V_VALID))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 break;
231 native_unlock_hpte(hptep);
232 }
233
234 hptep++;
235 }
236
237 if (i == HPTES_PER_GROUP)
238 return -1;
239
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000240 hpte_v = hpte_encode_v(vpn, psize, apsize, ssize) | vflags | HPTE_V_VALID;
Paul Mackerras6b243fc2016-11-11 16:55:03 +1100241 hpte_r = hpte_encode_r(pa, psize, apsize) | rflags;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100242
243 if (!(vflags & HPTE_V_BOLTED)) {
244 DBG_LOW(" i=%x hpte_v=%016lx, hpte_r=%016lx\n",
245 i, hpte_v, hpte_r);
246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Paul Mackerras6b243fc2016-11-11 16:55:03 +1100248 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
249 hpte_r = hpte_old_to_new_r(hpte_v, hpte_r);
250 hpte_v = hpte_old_to_new_v(hpte_v);
251 }
252
Anton Blanchard12f04f22013-09-23 12:04:36 +1000253 hptep->r = cpu_to_be64(hpte_r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 /* Guarantee the second dword is visible before the valid bit */
Kumar Gala74a0ba62007-07-09 23:49:09 -0500255 eieio();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 /*
257 * Now set the first dword including the valid bit
258 * NOTE: this also unlocks the hpte
259 */
Anton Blanchard12f04f22013-09-23 12:04:36 +1000260 hptep->v = cpu_to_be64(hpte_v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262 __asm__ __volatile__ ("ptesync" : : : "memory");
263
David Gibson96e28442005-07-13 01:11:42 -0700264 return i | (!!(vflags & HPTE_V_SECONDARY) << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265}
266
267static long native_hpte_remove(unsigned long hpte_group)
268{
David Gibson8e561e72007-06-13 14:52:56 +1000269 struct hash_pte *hptep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 int i;
271 int slot_offset;
David Gibson96e28442005-07-13 01:11:42 -0700272 unsigned long hpte_v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100274 DBG_LOW(" remove(group=%lx)\n", hpte_group);
275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 /* pick a random entry to start at */
277 slot_offset = mftb() & 0x7;
278
279 for (i = 0; i < HPTES_PER_GROUP; i++) {
280 hptep = htab_address + hpte_group + slot_offset;
Anton Blanchard12f04f22013-09-23 12:04:36 +1000281 hpte_v = be64_to_cpu(hptep->v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
David Gibson96e28442005-07-13 01:11:42 -0700283 if ((hpte_v & HPTE_V_VALID) && !(hpte_v & HPTE_V_BOLTED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 /* retry with lock held */
285 native_lock_hpte(hptep);
Anton Blanchard12f04f22013-09-23 12:04:36 +1000286 hpte_v = be64_to_cpu(hptep->v);
David Gibson96e28442005-07-13 01:11:42 -0700287 if ((hpte_v & HPTE_V_VALID)
288 && !(hpte_v & HPTE_V_BOLTED))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 break;
290 native_unlock_hpte(hptep);
291 }
292
293 slot_offset++;
294 slot_offset &= 0x7;
295 }
296
297 if (i == HPTES_PER_GROUP)
298 return -1;
299
300 /* Invalidate the hpte. NOTE: this also unlocks it */
David Gibson96e28442005-07-13 01:11:42 -0700301 hptep->v = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303 return i;
304}
305
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100306static long native_hpte_updatepp(unsigned long slot, unsigned long newpp,
Aneesh Kumar K.Vdb3d8532013-06-20 14:30:13 +0530307 unsigned long vpn, int bpsize,
Aneesh Kumar K.Vaefa5682014-12-04 11:00:14 +0530308 int apsize, int ssize, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
David Gibson8e561e72007-06-13 14:52:56 +1000310 struct hash_pte *hptep = htab_address + slot;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100311 unsigned long hpte_v, want_v;
Aneesh Kumar K.Vaefa5682014-12-04 11:00:14 +0530312 int ret = 0, local = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Aneesh Kumar K.Vdb3d8532013-06-20 14:30:13 +0530314 want_v = hpte_encode_avpn(vpn, bpsize, ssize);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100315
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000316 DBG_LOW(" update(vpn=%016lx, avpnv=%016lx, group=%lx, newpp=%lx)",
317 vpn, want_v & HPTE_V_AVPN, slot, newpp);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100318
Anton Blanchard12f04f22013-09-23 12:04:36 +1000319 hpte_v = be64_to_cpu(hptep->v);
Paul Mackerras6b243fc2016-11-11 16:55:03 +1100320 if (cpu_has_feature(CPU_FTR_ARCH_300))
321 hpte_v = hpte_new_to_old_v(hpte_v, be64_to_cpu(hptep->r));
Aneesh Kumar K.V0608d692013-05-31 01:03:24 +0000322 /*
323 * We need to invalidate the TLB always because hpte_remove doesn't do
324 * a tlb invalidate. If a hash bucket gets full, we "evict" a more/less
325 * random entry from it. When we do that we don't invalidate the TLB
326 * (hpte_remove) because we assume the old translation is still
327 * technically "valid".
328 */
Aneesh Kumar K.Vdb3d8532013-06-20 14:30:13 +0530329 if (!HPTE_V_COMPARE(hpte_v, want_v) || !(hpte_v & HPTE_V_VALID)) {
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100330 DBG_LOW(" -> miss\n");
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100331 ret = -1;
332 } else {
Aneesh Kumar K.V0ec26982014-11-03 20:21:34 +0530333 native_lock_hpte(hptep);
334 /* recheck with locks held */
335 hpte_v = be64_to_cpu(hptep->v);
Paul Mackerras6b243fc2016-11-11 16:55:03 +1100336 if (cpu_has_feature(CPU_FTR_ARCH_300))
337 hpte_v = hpte_new_to_old_v(hpte_v, be64_to_cpu(hptep->r));
Aneesh Kumar K.V0ec26982014-11-03 20:21:34 +0530338 if (unlikely(!HPTE_V_COMPARE(hpte_v, want_v) ||
339 !(hpte_v & HPTE_V_VALID))) {
340 ret = -1;
341 } else {
342 DBG_LOW(" -> hit\n");
343 /* Update the HPTE */
344 hptep->r = cpu_to_be64((be64_to_cpu(hptep->r) &
Aneesh Kumar K.V8550e2f2016-06-08 19:55:55 +0530345 ~(HPTE_R_PPP | HPTE_R_N)) |
346 (newpp & (HPTE_R_PPP | HPTE_R_N |
Aneesh Kumar K.V0ec26982014-11-03 20:21:34 +0530347 HPTE_R_C)));
348 }
349 native_unlock_hpte(hptep);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100350 }
Aneesh Kumar K.Vaefa5682014-12-04 11:00:14 +0530351
352 if (flags & HPTE_LOCAL_UPDATE)
353 local = 1;
354 /*
355 * Ensure it is out of the tlb too if it is not a nohpte fault
356 */
357 if (!(flags & HPTE_NOHPTE_UPDATE))
358 tlbie(vpn, bpsize, apsize, ssize, local);
359
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100360 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
362
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000363static long native_hpte_find(unsigned long vpn, int psize, int ssize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
David Gibson8e561e72007-06-13 14:52:56 +1000365 struct hash_pte *hptep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 unsigned long hash;
Paul Mackerras1189be62007-10-11 20:37:10 +1000367 unsigned long i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 long slot;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100369 unsigned long want_v, hpte_v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000371 hash = hpt_hash(vpn, mmu_psize_defs[psize].shift, ssize);
Aneesh Kumar K.V74f227b2013-04-28 09:37:34 +0000372 want_v = hpte_encode_avpn(vpn, psize, ssize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Paul Mackerras1189be62007-10-11 20:37:10 +1000374 /* Bolted mappings are only ever in the primary group */
375 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
376 for (i = 0; i < HPTES_PER_GROUP; i++) {
377 hptep = htab_address + slot;
Anton Blanchard12f04f22013-09-23 12:04:36 +1000378 hpte_v = be64_to_cpu(hptep->v);
Paul Mackerras6b243fc2016-11-11 16:55:03 +1100379 if (cpu_has_feature(CPU_FTR_ARCH_300))
380 hpte_v = hpte_new_to_old_v(hpte_v, be64_to_cpu(hptep->r));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Paul Mackerras1189be62007-10-11 20:37:10 +1000382 if (HPTE_V_COMPARE(hpte_v, want_v) && (hpte_v & HPTE_V_VALID))
383 /* HPTE matches */
384 return slot;
385 ++slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 }
387
388 return -1;
389}
390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391/*
392 * Update the page protection bits. Intended to be used to create
393 * guard pages for kernel data structures on pages which are bolted
394 * in the HPT. Assumes pages being operated on will not be stolen.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 *
396 * No need to lock here because we should be the only user.
397 */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100398static void native_hpte_updateboltedpp(unsigned long newpp, unsigned long ea,
Paul Mackerras1189be62007-10-11 20:37:10 +1000399 int psize, int ssize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000401 unsigned long vpn;
402 unsigned long vsid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 long slot;
David Gibson8e561e72007-06-13 14:52:56 +1000404 struct hash_pte *hptep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Paul Mackerras1189be62007-10-11 20:37:10 +1000406 vsid = get_kernel_vsid(ea, ssize);
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000407 vpn = hpt_vpn(ea, vsid, ssize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000409 slot = native_hpte_find(vpn, psize, ssize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 if (slot == -1)
411 panic("could not find page to bolt\n");
412 hptep = htab_address + slot;
413
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100414 /* Update the HPTE */
Anton Blanchard12f04f22013-09-23 12:04:36 +1000415 hptep->r = cpu_to_be64((be64_to_cpu(hptep->r) &
Aneesh Kumar K.V8550e2f2016-06-08 19:55:55 +0530416 ~(HPTE_R_PPP | HPTE_R_N)) |
417 (newpp & (HPTE_R_PPP | HPTE_R_N)));
Aneesh Kumar K.Vdb3d8532013-06-20 14:30:13 +0530418 /*
419 * Ensure it is out of the tlb too. Bolted entries base and
420 * actual page size will be same.
421 */
422 tlbie(vpn, psize, psize, ssize, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423}
424
Anton Blanchard1b644f52017-06-28 11:32:35 +1000425/*
426 * Remove a bolted kernel entry. Memory hotplug uses this.
427 *
428 * No need to lock here because we should be the only user.
429 */
430static int native_hpte_removebolted(unsigned long ea, int psize, int ssize)
431{
432 unsigned long vpn;
433 unsigned long vsid;
434 long slot;
435 struct hash_pte *hptep;
436
437 vsid = get_kernel_vsid(ea, ssize);
438 vpn = hpt_vpn(ea, vsid, ssize);
439
440 slot = native_hpte_find(vpn, psize, ssize);
441 if (slot == -1)
442 return -ENOENT;
443
444 hptep = htab_address + slot;
445
446 VM_WARN_ON(!(be64_to_cpu(hptep->v) & HPTE_V_BOLTED));
447
448 /* Invalidate the hpte */
449 hptep->v = 0;
450
451 /* Invalidate the TLB */
452 tlbie(vpn, psize, psize, ssize, 0);
453 return 0;
454}
455
456
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000457static void native_hpte_invalidate(unsigned long slot, unsigned long vpn,
Aneesh Kumar K.Vdb3d8532013-06-20 14:30:13 +0530458 int bpsize, int apsize, int ssize, int local)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
David Gibson8e561e72007-06-13 14:52:56 +1000460 struct hash_pte *hptep = htab_address + slot;
David Gibson96e28442005-07-13 01:11:42 -0700461 unsigned long hpte_v;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100462 unsigned long want_v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
465 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000467 DBG_LOW(" invalidate(vpn=%016lx, hash: %lx)\n", vpn, slot);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100468
Aneesh Kumar K.Vdb3d8532013-06-20 14:30:13 +0530469 want_v = hpte_encode_avpn(vpn, bpsize, ssize);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100470 native_lock_hpte(hptep);
Anton Blanchard12f04f22013-09-23 12:04:36 +1000471 hpte_v = be64_to_cpu(hptep->v);
Paul Mackerras6b243fc2016-11-11 16:55:03 +1100472 if (cpu_has_feature(CPU_FTR_ARCH_300))
473 hpte_v = hpte_new_to_old_v(hpte_v, be64_to_cpu(hptep->r));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Aneesh Kumar K.V0608d692013-05-31 01:03:24 +0000475 /*
476 * We need to invalidate the TLB always because hpte_remove doesn't do
477 * a tlb invalidate. If a hash bucket gets full, we "evict" a more/less
478 * random entry from it. When we do that we don't invalidate the TLB
479 * (hpte_remove) because we assume the old translation is still
480 * technically "valid".
481 */
Aneesh Kumar K.Vdb3d8532013-06-20 14:30:13 +0530482 if (!HPTE_V_COMPARE(hpte_v, want_v) || !(hpte_v & HPTE_V_VALID))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 native_unlock_hpte(hptep);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100484 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 /* Invalidate the hpte. NOTE: this also unlocks it */
David Gibson96e28442005-07-13 01:11:42 -0700486 hptep->v = 0;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100487
488 /* Invalidate the TLB */
Aneesh Kumar K.Vdb3d8532013-06-20 14:30:13 +0530489 tlbie(vpn, bpsize, apsize, ssize, local);
490
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100491 local_irq_restore(flags);
492}
493
Aneesh Kumar K.Ve34aa032015-12-01 09:06:53 +0530494#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Aneesh Kumar K.Vfa1f8ae2014-08-13 12:31:58 +0530495static void native_hugepage_invalidate(unsigned long vsid,
496 unsigned long addr,
Aneesh Kumar K.V1a527282013-06-20 14:30:27 +0530497 unsigned char *hpte_slot_array,
Aneesh Kumar K.Vd557b092014-11-02 21:15:28 +0530498 int psize, int ssize, int local)
Aneesh Kumar K.V1a527282013-06-20 14:30:27 +0530499{
Aneesh Kumar K.V969b7b22014-08-13 12:32:01 +0530500 int i;
Aneesh Kumar K.V1a527282013-06-20 14:30:27 +0530501 struct hash_pte *hptep;
502 int actual_psize = MMU_PAGE_16M;
503 unsigned int max_hpte_count, valid;
504 unsigned long flags, s_addr = addr;
505 unsigned long hpte_v, want_v, shift;
Aneesh Kumar K.Vfa1f8ae2014-08-13 12:31:58 +0530506 unsigned long hidx, vpn = 0, hash, slot;
Aneesh Kumar K.V1a527282013-06-20 14:30:27 +0530507
508 shift = mmu_psize_defs[psize].shift;
509 max_hpte_count = 1U << (PMD_SHIFT - shift);
510
511 local_irq_save(flags);
512 for (i = 0; i < max_hpte_count; i++) {
513 valid = hpte_valid(hpte_slot_array, i);
514 if (!valid)
515 continue;
516 hidx = hpte_hash_index(hpte_slot_array, i);
517
518 /* get the vpn */
519 addr = s_addr + (i * (1ul << shift));
Aneesh Kumar K.V1a527282013-06-20 14:30:27 +0530520 vpn = hpt_vpn(addr, vsid, ssize);
521 hash = hpt_hash(vpn, shift, ssize);
522 if (hidx & _PTEIDX_SECONDARY)
523 hash = ~hash;
524
525 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
526 slot += hidx & _PTEIDX_GROUP_IX;
527
528 hptep = htab_address + slot;
529 want_v = hpte_encode_avpn(vpn, psize, ssize);
530 native_lock_hpte(hptep);
Anton Blanchard12f04f22013-09-23 12:04:36 +1000531 hpte_v = be64_to_cpu(hptep->v);
Paul Mackerras6b243fc2016-11-11 16:55:03 +1100532 if (cpu_has_feature(CPU_FTR_ARCH_300))
533 hpte_v = hpte_new_to_old_v(hpte_v, be64_to_cpu(hptep->r));
Aneesh Kumar K.V1a527282013-06-20 14:30:27 +0530534
535 /* Even if we miss, we need to invalidate the TLB */
536 if (!HPTE_V_COMPARE(hpte_v, want_v) || !(hpte_v & HPTE_V_VALID))
537 native_unlock_hpte(hptep);
538 else
539 /* Invalidate the hpte. NOTE: this also unlocks it */
540 hptep->v = 0;
Aneesh Kumar K.V969b7b22014-08-13 12:32:01 +0530541 /*
542 * We need to do tlb invalidate for all the address, tlbie
543 * instruction compares entry_VA in tlb with the VA specified
544 * here
545 */
Aneesh Kumar K.Vd557b092014-11-02 21:15:28 +0530546 tlbie(vpn, psize, actual_psize, ssize, local);
Aneesh Kumar K.V1a527282013-06-20 14:30:27 +0530547 }
Aneesh Kumar K.V1a527282013-06-20 14:30:27 +0530548 local_irq_restore(flags);
549}
Aneesh Kumar K.Ve34aa032015-12-01 09:06:53 +0530550#else
551static void native_hugepage_invalidate(unsigned long vsid,
552 unsigned long addr,
553 unsigned char *hpte_slot_array,
554 int psize, int ssize, int local)
555{
556 WARN(1, "%s called without THP support\n", __func__);
557}
558#endif
Aneesh Kumar K.V1a527282013-06-20 14:30:27 +0530559
David Gibson8e561e72007-06-13 14:52:56 +1000560static void hpte_decode(struct hash_pte *hpte, unsigned long slot,
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000561 int *psize, int *apsize, int *ssize, unsigned long *vpn)
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100562{
Aneesh Kumar K.Vdcda2872012-09-10 02:52:49 +0000563 unsigned long avpn, pteg, vpi;
Anton Blanchard12f04f22013-09-23 12:04:36 +1000564 unsigned long hpte_v = be64_to_cpu(hpte->v);
565 unsigned long hpte_r = be64_to_cpu(hpte->r);
Aneesh Kumar K.Vdcda2872012-09-10 02:52:49 +0000566 unsigned long vsid, seg_off;
Aneesh Kumar K.V7e74c392013-04-28 09:37:36 +0000567 int size, a_size, shift;
568 /* Look at the 8 bit LP value */
Anton Blanchard12f04f22013-09-23 12:04:36 +1000569 unsigned int lp = (hpte_r >> LP_SHIFT) & ((1 << LP_BITS) - 1);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100570
Paul Mackerras6b243fc2016-11-11 16:55:03 +1100571 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
572 hpte_v = hpte_new_to_old_v(hpte_v, hpte_r);
573 hpte_r = hpte_new_to_old_r(hpte_r);
574 }
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000575 if (!(hpte_v & HPTE_V_LARGE)) {
576 size = MMU_PAGE_4K;
577 a_size = MMU_PAGE_4K;
578 } else {
Paul Mackerras0eeede02016-09-02 17:20:43 +1000579 size = hpte_page_sizes[lp] & 0xf;
580 a_size = hpte_page_sizes[lp] >> 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 }
Paul Mackerras2454c7e2007-05-10 15:28:44 +1000582 /* This works for all page sizes, and for 256M and 1T segments */
Paul Mackerras6b243fc2016-11-11 16:55:03 +1100583 *ssize = hpte_v >> HPTE_V_SSIZE_SHIFT;
Aneesh Kumar K.Vdcda2872012-09-10 02:52:49 +0000584 shift = mmu_psize_defs[size].shift;
585
586 avpn = (HPTE_V_AVPN_VAL(hpte_v) & ~mmu_psize_defs[size].avpnm);
587 pteg = slot / HPTES_PER_GROUP;
588 if (hpte_v & HPTE_V_SECONDARY)
589 pteg = ~pteg;
590
591 switch (*ssize) {
592 case MMU_SEGSIZE_256M:
593 /* We only have 28 - 23 bits of seg_off in avpn */
594 seg_off = (avpn & 0x1f) << 23;
595 vsid = avpn >> 5;
596 /* We can find more bits from the pteg value */
597 if (shift < 23) {
598 vpi = (vsid ^ pteg) & htab_hash_mask;
599 seg_off |= vpi << shift;
600 }
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000601 *vpn = vsid << (SID_SHIFT - VPN_SHIFT) | seg_off >> VPN_SHIFT;
Aneesh Kumar K.V83383b72013-07-03 13:50:03 +0530602 break;
Aneesh Kumar K.Vdcda2872012-09-10 02:52:49 +0000603 case MMU_SEGSIZE_1T:
604 /* We only have 40 - 23 bits of seg_off in avpn */
605 seg_off = (avpn & 0x1ffff) << 23;
606 vsid = avpn >> 17;
607 if (shift < 23) {
608 vpi = (vsid ^ (vsid << 25) ^ pteg) & htab_hash_mask;
609 seg_off |= vpi << shift;
610 }
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000611 *vpn = vsid << (SID_SHIFT_1T - VPN_SHIFT) | seg_off >> VPN_SHIFT;
Aneesh Kumar K.V83383b72013-07-03 13:50:03 +0530612 break;
Aneesh Kumar K.Vdcda2872012-09-10 02:52:49 +0000613 default:
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000614 *vpn = size = 0;
Aneesh Kumar K.Vdcda2872012-09-10 02:52:49 +0000615 }
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000616 *psize = size;
617 *apsize = a_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618}
619
R Sharadaf4c82d52005-06-25 14:58:08 -0700620/*
621 * clear all mappings on kexec. All cpus are in real mode (or they will
622 * be when they isi), and we are the only one left. We rely on our kernel
623 * mapping being 0xC0's and the hardware ignoring those two real bits.
624 *
Cyril Burfdf880a2015-10-08 11:04:26 +1100625 * This must be called with interrupts disabled.
626 *
627 * Taking the native_tlbie_lock is unsafe here due to the possibility of
628 * lockdep being on. On pre POWER5 hardware, not taking the lock could
629 * cause deadlock. POWER5 and newer not taking the lock is fine. This only
630 * gets called during boot before secondary CPUs have come up and during
631 * crashdump and all bets are off anyway.
632 *
R Sharadaf4c82d52005-06-25 14:58:08 -0700633 * TODO: add batching support when enabled. remember, no dynamic memory here,
Michael Ellerman027dfac2016-06-01 16:34:37 +1000634 * although there is the control page available...
R Sharadaf4c82d52005-06-25 14:58:08 -0700635 */
636static void native_hpte_clear(void)
637{
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000638 unsigned long vpn = 0;
Cyril Burfdf880a2015-10-08 11:04:26 +1100639 unsigned long slot, slots;
David Gibson8e561e72007-06-13 14:52:56 +1000640 struct hash_pte *hptep = htab_address;
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000641 unsigned long hpte_v;
R Sharadaf4c82d52005-06-25 14:58:08 -0700642 unsigned long pteg_count;
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000643 int psize, apsize, ssize;
R Sharadaf4c82d52005-06-25 14:58:08 -0700644
645 pteg_count = htab_hash_mask + 1;
646
R Sharadaf4c82d52005-06-25 14:58:08 -0700647 slots = pteg_count * HPTES_PER_GROUP;
648
649 for (slot = 0; slot < slots; slot++, hptep++) {
650 /*
651 * we could lock the pte here, but we are the only cpu
652 * running, right? and for crash dump, we probably
653 * don't want to wait for a maybe bad cpu.
654 */
Anton Blanchard12f04f22013-09-23 12:04:36 +1000655 hpte_v = be64_to_cpu(hptep->v);
R Sharadaf4c82d52005-06-25 14:58:08 -0700656
R Sharada47f78a42006-02-22 21:43:08 +0530657 /*
Cyril Burfdf880a2015-10-08 11:04:26 +1100658 * Call __tlbie() here rather than tlbie() since we can't take the
659 * native_tlbie_lock.
R Sharada47f78a42006-02-22 21:43:08 +0530660 */
David Gibson96e28442005-07-13 01:11:42 -0700661 if (hpte_v & HPTE_V_VALID) {
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000662 hpte_decode(hptep, slot, &psize, &apsize, &ssize, &vpn);
David Gibson96e28442005-07-13 01:11:42 -0700663 hptep->v = 0;
Mahesh Salgaonkara3961f82017-11-22 23:02:07 +0530664 ___tlbie(vpn, psize, apsize, ssize);
R Sharadaf4c82d52005-06-25 14:58:08 -0700665 }
666 }
667
R Sharada47f78a42006-02-22 21:43:08 +0530668 asm volatile("eieio; tlbsync; ptesync":::"memory");
R Sharadaf4c82d52005-06-25 14:58:08 -0700669}
670
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100671/*
672 * Batched hash table flush, we batch the tlbie's to avoid taking/releasing
673 * the lock all the time
674 */
Benjamin Herrenschmidt61b1a942005-09-20 13:52:50 +1000675static void native_flush_hash_range(unsigned long number, int local)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000677 unsigned long vpn;
678 unsigned long hash, index, hidx, shift, slot;
David Gibson8e561e72007-06-13 14:52:56 +1000679 struct hash_pte *hptep;
David Gibson96e28442005-07-13 01:11:42 -0700680 unsigned long hpte_v;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100681 unsigned long want_v;
682 unsigned long flags;
683 real_pte_t pte;
Christoph Lameter69111ba2014-10-21 15:23:25 -0500684 struct ppc64_tlb_batch *batch = this_cpu_ptr(&ppc64_tlb_batch);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100685 unsigned long psize = batch->psize;
Paul Mackerras1189be62007-10-11 20:37:10 +1000686 int ssize = batch->ssize;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100687 int i;
Frederic Barrat88b1bf722017-03-29 19:19:42 +0200688 unsigned int use_local;
689
690 use_local = local && mmu_has_feature(MMU_FTR_TLBIEL) &&
691 mmu_psize_defs[psize].tlbiel && !cxl_ctx_in_use();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693 local_irq_save(flags);
694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 for (i = 0; i < number; i++) {
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000696 vpn = batch->vpn[i];
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100697 pte = batch->pte[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000699 pte_iterate_hashed_subpages(pte, psize, vpn, index, shift) {
700 hash = hpt_hash(vpn, shift, ssize);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100701 hidx = __rpte_to_hidx(pte, index);
702 if (hidx & _PTEIDX_SECONDARY)
703 hash = ~hash;
704 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
705 slot += hidx & _PTEIDX_GROUP_IX;
706 hptep = htab_address + slot;
Aneesh Kumar K.V74f227b2013-04-28 09:37:34 +0000707 want_v = hpte_encode_avpn(vpn, psize, ssize);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100708 native_lock_hpte(hptep);
Anton Blanchard12f04f22013-09-23 12:04:36 +1000709 hpte_v = be64_to_cpu(hptep->v);
Paul Mackerras6b243fc2016-11-11 16:55:03 +1100710 if (cpu_has_feature(CPU_FTR_ARCH_300))
711 hpte_v = hpte_new_to_old_v(hpte_v,
712 be64_to_cpu(hptep->r));
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100713 if (!HPTE_V_COMPARE(hpte_v, want_v) ||
714 !(hpte_v & HPTE_V_VALID))
715 native_unlock_hpte(hptep);
716 else
717 hptep->v = 0;
718 } pte_iterate_hashed_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 }
720
Frederic Barrat88b1bf722017-03-29 19:19:42 +0200721 if (use_local) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 asm volatile("ptesync":::"memory");
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100723 for (i = 0; i < number; i++) {
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000724 vpn = batch->vpn[i];
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100725 pte = batch->pte[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000727 pte_iterate_hashed_subpages(pte, psize,
728 vpn, index, shift) {
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000729 __tlbiel(vpn, psize, psize, ssize);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100730 } pte_iterate_hashed_end();
731 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 asm volatile("ptesync":::"memory");
733 } else {
Matt Evans44ae3ab2011-04-06 19:48:50 +0000734 int lock_tlbie = !mmu_has_feature(MMU_FTR_LOCKLESS_TLBIE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
736 if (lock_tlbie)
Thomas Gleixner6b9c9b82010-02-18 02:22:35 +0000737 raw_spin_lock(&native_tlbie_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
739 asm volatile("ptesync":::"memory");
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100740 for (i = 0; i < number; i++) {
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000741 vpn = batch->vpn[i];
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100742 pte = batch->pte[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000744 pte_iterate_hashed_subpages(pte, psize,
745 vpn, index, shift) {
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000746 __tlbie(vpn, psize, psize, ssize);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100747 } pte_iterate_hashed_end();
748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 asm volatile("eieio; tlbsync; ptesync":::"memory");
750
751 if (lock_tlbie)
Thomas Gleixner6b9c9b82010-02-18 02:22:35 +0000752 raw_spin_unlock(&native_tlbie_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 }
754
755 local_irq_restore(flags);
756}
757
Aneesh Kumar K.V83209bc2016-07-13 15:05:28 +0530758static int native_register_proc_table(unsigned long base, unsigned long page_size,
759 unsigned long table_size)
Aneesh Kumar K.V50de5962016-04-29 23:25:43 +1000760{
Aneesh Kumar K.V83209bc2016-07-13 15:05:28 +0530761 unsigned long patb1 = base << 25; /* VSID */
762
763 patb1 |= (page_size << 5); /* sllp */
764 patb1 |= table_size;
765
Aneesh Kumar K.V50de5962016-04-29 23:25:43 +1000766 partition_tb->patb1 = cpu_to_be64(patb1);
767 return 0;
768}
769
Michael Ellerman7d0daae2006-06-23 18:16:38 +1000770void __init hpte_init_native(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
Benjamin Herrenschmidt70257762016-07-05 15:03:58 +1000772 mmu_hash_ops.hpte_invalidate = native_hpte_invalidate;
773 mmu_hash_ops.hpte_updatepp = native_hpte_updatepp;
774 mmu_hash_ops.hpte_updateboltedpp = native_hpte_updateboltedpp;
Anton Blanchard1b644f52017-06-28 11:32:35 +1000775 mmu_hash_ops.hpte_removebolted = native_hpte_removebolted;
Benjamin Herrenschmidt70257762016-07-05 15:03:58 +1000776 mmu_hash_ops.hpte_insert = native_hpte_insert;
777 mmu_hash_ops.hpte_remove = native_hpte_remove;
778 mmu_hash_ops.hpte_clear_all = native_hpte_clear;
779 mmu_hash_ops.flush_hash_range = native_flush_hash_range;
780 mmu_hash_ops.hugepage_invalidate = native_hugepage_invalidate;
Aneesh Kumar K.V50de5962016-04-29 23:25:43 +1000781
782 if (cpu_has_feature(CPU_FTR_ARCH_300))
Michael Ellermaneea81482016-08-04 15:32:06 +1000783 register_process_table = native_register_proc_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784}