blob: cfd45245d00932bcd0ed6edf649337bed60f1815 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * PowerPC64 port by Mike Corrigan and Dave Engebretsen
4 * {mikejc|engebret}@us.ibm.com
5 *
6 * Copyright (c) 2000 Mike Corrigan <mikejc@us.ibm.com>
7 *
8 * SMP scalability work:
9 * Copyright (C) 2001 Anton Blanchard <anton@au.ibm.com>, IBM
Mike Rapoportb10d6bc2020-10-13 16:58:08 -070010 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * Module name: htab.c
12 *
13 * Description:
14 * PowerPC Hashed Page Table functions
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 */
16
17#undef DEBUG
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +110018#undef DEBUG_LOW
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Aneesh Kumar K.V7f142662017-10-16 12:31:40 +053020#define pr_fmt(fmt) "hash-mmu: " fmt
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/spinlock.h>
22#include <linux/errno.h>
Ingo Molnar589ee622017-02-04 00:16:44 +010023#include <linux/sched/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/proc_fs.h>
25#include <linux/stat.h>
26#include <linux/sysctl.h>
Paul Gortmaker66b15db2011-05-27 10:46:24 -040027#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/ctype.h>
29#include <linux/cache.h>
30#include <linux/init.h>
31#include <linux/signal.h>
Yinghai Lu95f72d12010-07-12 14:36:09 +100032#include <linux/memblock.h>
Li Zhongba12eed2013-05-13 16:16:41 +000033#include <linux/context_tracking.h>
Benjamin Herrenschmidt5556ecf2016-07-05 15:03:53 +100034#include <linux/libfdt.h>
Ram Pai92e3da32018-01-18 17:50:24 -080035#include <linux/pkeys.h>
Christophe Leroy45d0ba52019-04-26 05:59:47 +000036#include <linux/hugetlb.h>
Gautham R. Shenoyc784be42019-05-15 13:15:52 +053037#include <linux/cpu.h>
Mike Rapoport65fddcf2020-06-08 21:32:42 -070038#include <linux/pgtable.h>
Aneesh Kumar K.Vdbf77fed2021-08-12 18:58:31 +053039#include <linux/debugfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Nicholas Piggin3a965702021-01-30 23:08:38 +100041#include <asm/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <asm/processor.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <asm/mmu.h>
44#include <asm/mmu_context.h>
45#include <asm/page.h>
46#include <asm/types.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080047#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <asm/machdep.h>
David S. Millerd9b2b2a2008-02-13 16:56:49 -080049#include <asm/prom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <asm/io.h>
51#include <asm/eeh.h>
52#include <asm/tlb.h>
53#include <asm/cacheflush.h>
54#include <asm/cputable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <asm/sections.h>
Ian Munsiebe3ebfe2014-10-08 19:54:52 +110056#include <asm/copro.h>
will schmidtaa39be02007-10-30 06:24:19 +110057#include <asm/udbg.h>
Anton Blanchardb68a70c2011-04-04 23:56:18 +000058#include <asm/code-patching.h>
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +000059#include <asm/fadump.h>
Stephen Rothwellf5339272012-03-15 18:18:00 +000060#include <asm/firmware.h>
Michael Neulingbc2a9402013-02-13 16:21:40 +000061#include <asm/tm.h>
Aneesh Kumar K.Vcfcb3d82015-04-14 13:05:57 +053062#include <asm/trace.h>
Benjamin Herrenschmidt166dd7d2016-07-05 15:03:51 +100063#include <asm/ps3.h>
Aneesh Kumar K.V94171b12017-07-27 11:54:53 +053064#include <asm/pte-walk.h>
Simon Guoeacbb212018-05-23 15:01:46 +080065#include <asm/asm-prototypes.h>
Claudio Carvalho52231342019-08-22 00:48:36 -030066#include <asm/ultravisor.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Christophe Leroye4dccf92019-04-26 16:36:39 +000068#include <mm/mmu_decl.h>
69
Nicholas Piggin82a1b8e2020-05-11 22:58:24 +100070#include "internal.h"
71
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#ifdef DEBUG
74#define DBG(fmt...) udbg_printf(fmt)
75#else
76#define DBG(fmt...)
77#endif
78
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +110079#ifdef DEBUG_LOW
80#define DBG_LOW(fmt...) udbg_printf(fmt)
81#else
82#define DBG_LOW(fmt...)
83#endif
84
85#define KB (1024)
86#define MB (1024*KB)
Jon Tollefson658013e2008-07-23 21:27:54 -070087#define GB (1024L*MB)
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +110088
Linus Torvalds1da177e2005-04-16 15:20:36 -070089/*
90 * Note: pte --> Linux PTE
91 * HPTE --> PowerPC Hashed Page Table Entry
92 *
93 * Execution context:
94 * htab_initialize is called with the MMU off (of course), but
95 * the kernel has been copied down to zero so it can directly
96 * reference global data. At this point it is very difficult
97 * to print debug info.
98 *
99 */
100
Paul Mackerras799d6042005-11-10 13:37:51 +1100101static unsigned long _SDR1;
102struct mmu_psize_def mmu_psize_defs[MMU_PAGE_COUNT];
Anton Blancharde1802b02014-08-20 08:00:02 +1000103EXPORT_SYMBOL_GPL(mmu_psize_defs);
Paul Mackerras799d6042005-11-10 13:37:51 +1100104
Paul Mackerras0eeede02016-09-02 17:20:43 +1000105u8 hpte_page_sizes[1 << LP_BITS];
106EXPORT_SYMBOL_GPL(hpte_page_sizes);
107
David Gibson8e561e72007-06-13 14:52:56 +1000108struct hash_pte *htab_address;
Michael Ellerman337a7122006-02-21 17:22:55 +1100109unsigned long htab_size_bytes;
David Gibson96e28442005-07-13 01:11:42 -0700110unsigned long htab_hash_mask;
Alexander Graf4ab79aa2009-10-30 05:47:19 +0000111EXPORT_SYMBOL_GPL(htab_hash_mask);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100112int mmu_linear_psize = MMU_PAGE_4K;
Ian Munsie8ca7a822014-10-08 19:54:54 +1100113EXPORT_SYMBOL_GPL(mmu_linear_psize);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100114int mmu_virtual_psize = MMU_PAGE_4K;
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000115int mmu_vmalloc_psize = MMU_PAGE_4K;
Ganesh Goudar3ba150f2020-11-30 14:00:57 +0530116EXPORT_SYMBOL_GPL(mmu_vmalloc_psize);
Benjamin Herrenschmidtcec08e72008-04-30 15:41:48 +1000117#ifdef CONFIG_SPARSEMEM_VMEMMAP
118int mmu_vmemmap_psize = MMU_PAGE_4K;
119#endif
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000120int mmu_io_psize = MMU_PAGE_4K;
Paul Mackerras1189be62007-10-11 20:37:10 +1000121int mmu_kernel_ssize = MMU_SEGSIZE_256M;
Ian Munsie8ca7a822014-10-08 19:54:54 +1100122EXPORT_SYMBOL_GPL(mmu_kernel_ssize);
Paul Mackerras1189be62007-10-11 20:37:10 +1000123int mmu_highuser_ssize = MMU_SEGSIZE_256M;
Michael Neuling584f8b72007-12-06 17:24:48 +1100124u16 mmu_slb_size = 64;
Alexander Graf4ab79aa2009-10-30 05:47:19 +0000125EXPORT_SYMBOL_GPL(mmu_slb_size);
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000126#ifdef CONFIG_PPC_64K_PAGES
127int mmu_ci_restrictions;
128#endif
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000129#ifdef CONFIG_DEBUG_PAGEALLOC
130static u8 *linear_map_hash_slots;
131static unsigned long linear_map_hash_count;
Michael Ellermaned166692007-04-18 11:50:09 +1000132static DEFINE_SPINLOCK(linear_map_hash_lock);
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000133#endif /* CONFIG_DEBUG_PAGEALLOC */
Benjamin Herrenschmidt70257762016-07-05 15:03:58 +1000134struct mmu_hash_ops mmu_hash_ops;
135EXPORT_SYMBOL(mmu_hash_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Christophe Leroy47d99942019-03-29 10:00:00 +0000137/*
138 * These are definitions of page sizes arrays to be used when none
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100139 * is provided by the firmware.
140 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Nicholas Piggin471d7ff2018-02-21 05:08:29 +1000142/*
143 * Fallback (4k pages only)
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100144 */
Nicholas Piggin471d7ff2018-02-21 05:08:29 +1000145static struct mmu_psize_def mmu_psize_defaults[] = {
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100146 [MMU_PAGE_4K] = {
147 .shift = 12,
148 .sllp = 0,
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000149 .penc = {[MMU_PAGE_4K] = 0, [1 ... MMU_PAGE_COUNT - 1] = -1},
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100150 .avpnm = 0,
151 .tlbiel = 0,
152 },
153};
154
Christophe Leroy47d99942019-03-29 10:00:00 +0000155/*
156 * POWER4, GPUL, POWER5
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100157 *
158 * Support for 16Mb large pages
159 */
Michael Ellerman09de9ff2008-05-08 14:27:07 +1000160static struct mmu_psize_def mmu_psize_defaults_gp[] = {
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100161 [MMU_PAGE_4K] = {
162 .shift = 12,
163 .sllp = 0,
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000164 .penc = {[MMU_PAGE_4K] = 0, [1 ... MMU_PAGE_COUNT - 1] = -1},
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100165 .avpnm = 0,
166 .tlbiel = 1,
167 },
168 [MMU_PAGE_16M] = {
169 .shift = 24,
170 .sllp = SLB_VSID_L,
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000171 .penc = {[0 ... MMU_PAGE_16M - 1] = -1, [MMU_PAGE_16M] = 0,
172 [MMU_PAGE_16M + 1 ... MMU_PAGE_COUNT - 1] = -1 },
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100173 .avpnm = 0x1UL,
174 .tlbiel = 0,
175 },
176};
177
Aneesh Kumar K.Vdc47c0c12016-05-31 11:56:30 +0530178/*
179 * 'R' and 'C' update notes:
180 * - Under pHyp or KVM, the updatepp path will not set C, thus it *will*
181 * create writeable HPTEs without C set, because the hcall H_PROTECT
182 * that we use in that case will not update C
183 * - The above is however not a problem, because we also don't do that
184 * fancy "no flush" variant of eviction and we use H_REMOVE which will
185 * do the right thing and thus we don't have the race I described earlier
186 *
187 * - Under bare metal, we do have the race, so we need R and C set
188 * - We make sure R is always set and never lost
189 * - C is _PAGE_DIRTY, and *should* always be set for a writeable mapping
190 */
Aneesh Kumar K.Vd94b8272020-11-27 10:14:10 +0530191unsigned long htab_convert_pte_flags(unsigned long pteflags, unsigned long flags)
Benjamin Herrenschmidtbc033b62008-08-05 16:19:56 +1000192{
Aneesh Kumar K.Vc6a3c492015-12-01 09:06:50 +0530193 unsigned long rflags = 0;
Benjamin Herrenschmidtbc033b62008-08-05 16:19:56 +1000194
195 /* _PAGE_EXEC -> NOEXEC */
196 if ((pteflags & _PAGE_EXEC) == 0)
197 rflags |= HPTE_R_N;
Aneesh Kumar K.Vc6a3c492015-12-01 09:06:50 +0530198 /*
Aneesh Kumar K.Ve58e87a2016-04-29 23:25:36 +1000199 * PPP bits:
Paul Mackerras1ec3f932016-02-22 13:41:12 +1100200 * Linux uses slb key 0 for kernel and 1 for user.
Aneesh Kumar K.Ve58e87a2016-04-29 23:25:36 +1000201 * kernel RW areas are mapped with PPP=0b000
202 * User area is mapped with PPP=0b010 for read/write
203 * or PPP=0b011 for read-only (including writeable but clean pages).
Benjamin Herrenschmidtbc033b62008-08-05 16:19:56 +1000204 */
Aneesh Kumar K.Ve58e87a2016-04-29 23:25:36 +1000205 if (pteflags & _PAGE_PRIVILEGED) {
206 /*
207 * Kernel read only mapped with ppp bits 0b110
208 */
Aneesh Kumar K.V984d7a12016-11-24 15:09:54 +0530209 if (!(pteflags & _PAGE_WRITE)) {
210 if (mmu_has_feature(MMU_FTR_KERNEL_RO))
211 rflags |= (HPTE_R_PP0 | 0x2);
212 else
213 rflags |= 0x3;
214 }
Aneesh Kumar K.Ve58e87a2016-04-29 23:25:36 +1000215 } else {
Aneesh Kumar K.Vc7d54842016-04-29 23:25:30 +1000216 if (pteflags & _PAGE_RWX)
217 rflags |= 0x2;
218 if (!((pteflags & _PAGE_WRITE) && (pteflags & _PAGE_DIRTY)))
Aneesh Kumar K.Vc6a3c492015-12-01 09:06:50 +0530219 rflags |= 0x1;
220 }
Aneesh Kumar K.Vc8c06f52013-11-18 14:58:10 +0530221 /*
Aneesh Kumar K.Vdc47c0c12016-05-31 11:56:30 +0530222 * We can't allow hardware to update hpte bits. Hence always
223 * set 'R' bit and set 'C' if it is a write fault
Aneesh Kumar K.Vc8c06f52013-11-18 14:58:10 +0530224 */
Aneesh Kumar K.Ve5680062016-06-17 11:32:00 +0530225 rflags |= HPTE_R_R;
Aneesh Kumar K.Vdc47c0c12016-05-31 11:56:30 +0530226
227 if (pteflags & _PAGE_DIRTY)
228 rflags |= HPTE_R_C;
Aneesh Kumar K.V40e85502015-12-01 09:06:51 +0530229 /*
230 * Add in WIG bits
231 */
Aneesh Kumar K.V30bda412016-04-29 23:25:38 +1000232
233 if ((pteflags & _PAGE_CACHE_CTL) == _PAGE_TOLERANT)
Aneesh Kumar K.V40e85502015-12-01 09:06:51 +0530234 rflags |= HPTE_R_I;
Aneesh Kumar K.Ve5680062016-06-17 11:32:00 +0530235 else if ((pteflags & _PAGE_CACHE_CTL) == _PAGE_NON_IDEMPOTENT)
Aneesh Kumar K.V30bda412016-04-29 23:25:38 +1000236 rflags |= (HPTE_R_I | HPTE_R_G);
Shawn Anastasio12564482020-08-21 13:55:56 -0500237 else if ((pteflags & _PAGE_CACHE_CTL) == _PAGE_SAO)
238 rflags |= (HPTE_R_W | HPTE_R_I | HPTE_R_M);
Aneesh Kumar K.Ve5680062016-06-17 11:32:00 +0530239 else
240 /*
241 * Add memory coherence if cache inhibited is not set
242 */
243 rflags |= HPTE_R_M;
Aneesh Kumar K.V40e85502015-12-01 09:06:51 +0530244
Aneesh Kumar K.Vd94b8272020-11-27 10:14:10 +0530245 rflags |= pte_to_hpte_pkey_bits(pteflags, flags);
Aneesh Kumar K.V40e85502015-12-01 09:06:51 +0530246 return rflags;
Benjamin Herrenschmidtbc033b62008-08-05 16:19:56 +1000247}
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100248
249int htab_bolt_mapping(unsigned long vstart, unsigned long vend,
Benjamin Herrenschmidtbc033b62008-08-05 16:19:56 +1000250 unsigned long pstart, unsigned long prot,
Paul Mackerras1189be62007-10-11 20:37:10 +1000251 int psize, int ssize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100253 unsigned long vaddr, paddr;
254 unsigned int step, shift;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100255 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100257 shift = mmu_psize_defs[psize].shift;
258 step = 1 << shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Aneesh Kumar K.Vd94b8272020-11-27 10:14:10 +0530260 prot = htab_convert_pte_flags(prot, HPTE_USE_KERNEL_KEY);
Benjamin Herrenschmidtbc033b62008-08-05 16:19:56 +1000261
262 DBG("htab_bolt_mapping(%lx..%lx -> %lx (%lx,%d,%d)\n",
263 vstart, vend, pstart, prot, psize, ssize);
264
Aneesh Kumar K.V79b123cd2020-09-07 12:55:39 +0530265 /* Carefully map only the possible range */
266 vaddr = ALIGN(vstart, step);
267 paddr = ALIGN(pstart, step);
268 vend = ALIGN_DOWN(vend, step);
269
270 for (; vaddr < vend; vaddr += step, paddr += step) {
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000271 unsigned long hash, hpteg;
Paul Mackerras1189be62007-10-11 20:37:10 +1000272 unsigned long vsid = get_kernel_vsid(vaddr, ssize);
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000273 unsigned long vpn = hpt_vpn(vaddr, vsid, ssize);
Paul Mackerras9e88ba42008-08-30 11:26:27 +1000274 unsigned long tprot = prot;
Aneesh Kumar K.Vd78d5da2019-10-24 15:05:42 +0530275 bool secondary_hash = false;
Paul Mackerras9e88ba42008-08-30 11:26:27 +1000276
Aneesh Kumar K.Vc60ac562013-03-13 03:34:54 +0000277 /*
278 * If we hit a bad address return error.
279 */
280 if (!vsid)
281 return -1;
Paul Mackerras9e88ba42008-08-30 11:26:27 +1000282 /* Make kernel text executable */
Paul Mackerras549e8152008-08-30 11:43:47 +1000283 if (overlaps_kernel_text(vaddr, vaddr + step))
Paul Mackerras9e88ba42008-08-30 11:26:27 +1000284 tprot &= ~HPTE_R_N;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Mahesh Salgaonkar429d2e82014-01-31 00:31:04 +0530286 /*
287 * If relocatable, check if it overlaps interrupt vectors that
288 * are copied down to real 0. For relocatable kernel
289 * (e.g. kdump case) we copy interrupt vectors down to real
290 * address 0. Mark that region as executable. This is
291 * because on p8 system with relocation on exception feature
292 * enabled, exceptions are raised with MMU (IR=DR=1) ON. Hence
293 * in order to execute the interrupt handlers in virtual
294 * mode the vector region need to be marked as executable.
295 */
296 if ((PHYSICAL_START > MEMORY_START) &&
297 overlaps_interrupt_vector_text(vaddr, vaddr + step))
298 tprot &= ~HPTE_R_N;
299
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000300 hash = hpt_hash(vpn, shift, ssize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
302
Benjamin Herrenschmidt70257762016-07-05 15:03:58 +1000303 BUG_ON(!mmu_hash_ops.hpte_insert);
Aneesh Kumar K.Vd78d5da2019-10-24 15:05:42 +0530304repeat:
Benjamin Herrenschmidt70257762016-07-05 15:03:58 +1000305 ret = mmu_hash_ops.hpte_insert(hpteg, vpn, paddr, tprot,
306 HPTE_V_BOLTED, psize, psize,
307 ssize);
Aneesh Kumar K.V75838a32019-10-24 15:05:41 +0530308 if (ret == -1) {
Aneesh Kumar K.Vd78d5da2019-10-24 15:05:42 +0530309 /*
310 * Try to to keep bolted entries in primary.
311 * Remove non bolted entries and try insert again
312 */
Aneesh Kumar K.V75838a32019-10-24 15:05:41 +0530313 ret = mmu_hash_ops.hpte_remove(hpteg);
314 if (ret != -1)
315 ret = mmu_hash_ops.hpte_insert(hpteg, vpn, paddr, tprot,
316 HPTE_V_BOLTED, psize, psize,
317 ssize);
Aneesh Kumar K.Vd78d5da2019-10-24 15:05:42 +0530318 if (ret == -1 && !secondary_hash) {
319 secondary_hash = true;
320 hpteg = ((~hash & htab_hash_mask) * HPTES_PER_GROUP);
321 goto repeat;
322 }
Aneesh Kumar K.V75838a32019-10-24 15:05:41 +0530323 }
Aneesh Kumar K.Vd78d5da2019-10-24 15:05:42 +0530324
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100325 if (ret < 0)
326 break;
Joonsoo Kime7df0d82016-03-17 14:17:59 -0700327
Aneesh Kumar K.V16f6b672019-10-01 14:16:56 +0530328 cond_resched();
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000329#ifdef CONFIG_DEBUG_PAGEALLOC
Joonsoo Kime7df0d82016-03-17 14:17:59 -0700330 if (debug_pagealloc_enabled() &&
331 (paddr >> PAGE_SHIFT) < linear_map_hash_count)
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000332 linear_map_hash_slots[paddr >> PAGE_SHIFT] = ret | 0x80;
333#endif /* CONFIG_DEBUG_PAGEALLOC */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 }
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100335 return ret < 0 ? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336}
337
Li Zhonged5694a2014-06-11 16:23:37 +0800338int htab_remove_mapping(unsigned long vstart, unsigned long vend,
Badari Pulavartyf8c88032008-01-29 09:19:24 +1100339 int psize, int ssize)
340{
Vaibhav Jaina5d6a3e2021-04-04 22:01:48 +0530341 unsigned long vaddr, time_limit;
Badari Pulavartyf8c88032008-01-29 09:19:24 +1100342 unsigned int step, shift;
David Gibson27828f92016-02-09 13:32:41 +1000343 int rc;
344 int ret = 0;
Badari Pulavartyf8c88032008-01-29 09:19:24 +1100345
346 shift = mmu_psize_defs[psize].shift;
347 step = 1 << shift;
348
Benjamin Herrenschmidt70257762016-07-05 15:03:58 +1000349 if (!mmu_hash_ops.hpte_removebolted)
David Gibsonabd0a0e2016-02-09 13:32:40 +1000350 return -ENODEV;
Badari Pulavartyf8c88032008-01-29 09:19:24 +1100351
Aneesh Kumar K.V79b123cd2020-09-07 12:55:39 +0530352 /* Unmap the full range specificied */
353 vaddr = ALIGN_DOWN(vstart, step);
Vaibhav Jaina5d6a3e2021-04-04 22:01:48 +0530354 time_limit = jiffies + HZ;
355
Aneesh Kumar K.V79b123cd2020-09-07 12:55:39 +0530356 for (;vaddr < vend; vaddr += step) {
Benjamin Herrenschmidt70257762016-07-05 15:03:58 +1000357 rc = mmu_hash_ops.hpte_removebolted(vaddr, psize, ssize);
Vaibhav Jaina5d6a3e2021-04-04 22:01:48 +0530358
359 /*
360 * For large number of mappings introduce a cond_resched()
361 * to prevent softlockup warnings.
362 */
363 if (time_after(jiffies, time_limit)) {
364 cond_resched();
365 time_limit = jiffies + HZ;
366 }
David Gibson27828f92016-02-09 13:32:41 +1000367 if (rc == -ENOENT) {
368 ret = -ENOENT;
369 continue;
370 }
371 if (rc < 0)
372 return rc;
Badari Pulavartyf8c88032008-01-29 09:19:24 +1100373 }
374
David Gibson27828f92016-02-09 13:32:41 +1000375 return ret;
Badari Pulavartyf8c88032008-01-29 09:19:24 +1100376}
377
Oliver O'Halloranfaf78822016-07-05 11:43:21 +1000378static bool disable_1tb_segments = false;
379
380static int __init parse_disable_1tb_segments(char *p)
381{
382 disable_1tb_segments = true;
383 return 0;
384}
385early_param("disable_1tb_segments", parse_disable_1tb_segments);
386
Paul Mackerras1189be62007-10-11 20:37:10 +1000387static int __init htab_dt_scan_seg_sizes(unsigned long node,
388 const char *uname, int depth,
389 void *data)
390{
Rob Herring9d0c4df2014-04-01 23:49:03 -0500391 const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
392 const __be32 *prop;
393 int size = 0;
Paul Mackerras1189be62007-10-11 20:37:10 +1000394
395 /* We are scanning "cpu" nodes only */
396 if (type == NULL || strcmp(type, "cpu") != 0)
397 return 0;
398
Anton Blanchard12f04f22013-09-23 12:04:36 +1000399 prop = of_get_flat_dt_prop(node, "ibm,processor-segment-sizes", &size);
Paul Mackerras1189be62007-10-11 20:37:10 +1000400 if (prop == NULL)
401 return 0;
402 for (; size >= 4; size -= 4, ++prop) {
Anton Blanchard12f04f22013-09-23 12:04:36 +1000403 if (be32_to_cpu(prop[0]) == 40) {
Paul Mackerras1189be62007-10-11 20:37:10 +1000404 DBG("1T segment support detected\n");
Oliver O'Halloranfaf78822016-07-05 11:43:21 +1000405
406 if (disable_1tb_segments) {
407 DBG("1T segments disabled by command line\n");
408 break;
409 }
410
Matt Evans44ae3ab2011-04-06 19:48:50 +0000411 cur_cpu_spec->mmu_features |= MMU_FTR_1T_SEGMENT;
Olof Johanssonf5534002007-10-12 16:44:55 +1000412 return 1;
Paul Mackerras1189be62007-10-11 20:37:10 +1000413 }
Paul Mackerras1189be62007-10-11 20:37:10 +1000414 }
Matt Evans44ae3ab2011-04-06 19:48:50 +0000415 cur_cpu_spec->mmu_features &= ~MMU_FTR_NO_SLBIE_B;
Paul Mackerras1189be62007-10-11 20:37:10 +1000416 return 0;
417}
418
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000419static int __init get_idx_from_shift(unsigned int shift)
420{
421 int idx = -1;
422
423 switch (shift) {
424 case 0xc:
425 idx = MMU_PAGE_4K;
426 break;
427 case 0x10:
428 idx = MMU_PAGE_64K;
429 break;
430 case 0x14:
431 idx = MMU_PAGE_1M;
432 break;
433 case 0x18:
434 idx = MMU_PAGE_16M;
435 break;
436 case 0x22:
437 idx = MMU_PAGE_16G;
438 break;
439 }
440 return idx;
441}
442
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100443static int __init htab_dt_scan_page_sizes(unsigned long node,
444 const char *uname, int depth,
445 void *data)
446{
Rob Herring9d0c4df2014-04-01 23:49:03 -0500447 const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
448 const __be32 *prop;
449 int size = 0;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100450
451 /* We are scanning "cpu" nodes only */
452 if (type == NULL || strcmp(type, "cpu") != 0)
453 return 0;
454
Anton Blanchard12f04f22013-09-23 12:04:36 +1000455 prop = of_get_flat_dt_prop(node, "ibm,segment-page-sizes", &size);
Michael Ellerman9e349922014-08-07 17:26:33 +1000456 if (!prop)
457 return 0;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100458
Michael Ellerman9e349922014-08-07 17:26:33 +1000459 pr_info("Page sizes from device-tree:\n");
460 size /= 4;
461 cur_cpu_spec->mmu_features &= ~(MMU_FTR_16M_PAGE);
462 while(size > 0) {
463 unsigned int base_shift = be32_to_cpu(prop[0]);
464 unsigned int slbenc = be32_to_cpu(prop[1]);
465 unsigned int lpnum = be32_to_cpu(prop[2]);
466 struct mmu_psize_def *def;
467 int idx, base_idx;
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000468
Michael Ellerman9e349922014-08-07 17:26:33 +1000469 size -= 3; prop += 3;
470 base_idx = get_idx_from_shift(base_shift);
471 if (base_idx < 0) {
472 /* skip the pte encoding also */
473 prop += lpnum * 2; size -= lpnum * 2;
474 continue;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100475 }
Michael Ellerman9e349922014-08-07 17:26:33 +1000476 def = &mmu_psize_defs[base_idx];
477 if (base_idx == MMU_PAGE_16M)
478 cur_cpu_spec->mmu_features |= MMU_FTR_16M_PAGE;
479
480 def->shift = base_shift;
481 if (base_shift <= 23)
482 def->avpnm = 0;
483 else
484 def->avpnm = (1 << (base_shift - 23)) - 1;
485 def->sllp = slbenc;
486 /*
487 * We don't know for sure what's up with tlbiel, so
488 * for now we only set it for 4K and 64K pages
489 */
490 if (base_idx == MMU_PAGE_4K || base_idx == MMU_PAGE_64K)
491 def->tlbiel = 1;
492 else
493 def->tlbiel = 0;
494
495 while (size > 0 && lpnum) {
496 unsigned int shift = be32_to_cpu(prop[0]);
497 int penc = be32_to_cpu(prop[1]);
498
499 prop += 2; size -= 2;
500 lpnum--;
501
502 idx = get_idx_from_shift(shift);
503 if (idx < 0)
504 continue;
505
506 if (penc == -1)
507 pr_err("Invalid penc for base_shift=%d "
508 "shift=%d\n", base_shift, shift);
509
510 def->penc[idx] = penc;
511 pr_info("base_shift=%d: shift=%d, sllp=0x%04lx,"
512 " avpnm=0x%08lx, tlbiel=%d, penc=%d\n",
513 base_shift, shift, def->sllp,
514 def->avpnm, def->tlbiel, def->penc[idx]);
515 }
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100516 }
Michael Ellerman9e349922014-08-07 17:26:33 +1000517
518 return 1;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100519}
520
Tony Breedse16a9c02008-07-31 13:51:42 +1000521#ifdef CONFIG_HUGETLB_PAGE
Christophe Leroy47d99942019-03-29 10:00:00 +0000522/*
523 * Scan for 16G memory blocks that have been set aside for huge pages
Jon Tollefson658013e2008-07-23 21:27:54 -0700524 * and reserve those blocks for 16G huge pages.
525 */
526static int __init htab_dt_scan_hugepage_blocks(unsigned long node,
527 const char *uname, int depth,
528 void *data) {
Rob Herring9d0c4df2014-04-01 23:49:03 -0500529 const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
530 const __be64 *addr_prop;
531 const __be32 *page_count_prop;
Jon Tollefson658013e2008-07-23 21:27:54 -0700532 unsigned int expected_pages;
533 long unsigned int phys_addr;
534 long unsigned int block_size;
535
536 /* We are scanning "memory" nodes only */
537 if (type == NULL || strcmp(type, "memory") != 0)
538 return 0;
539
Christophe Leroy47d99942019-03-29 10:00:00 +0000540 /*
541 * This property is the log base 2 of the number of virtual pages that
542 * will represent this memory block.
543 */
Jon Tollefson658013e2008-07-23 21:27:54 -0700544 page_count_prop = of_get_flat_dt_prop(node, "ibm,expected#pages", NULL);
545 if (page_count_prop == NULL)
546 return 0;
Anton Blanchard12f04f22013-09-23 12:04:36 +1000547 expected_pages = (1 << be32_to_cpu(page_count_prop[0]));
Jon Tollefson658013e2008-07-23 21:27:54 -0700548 addr_prop = of_get_flat_dt_prop(node, "reg", NULL);
549 if (addr_prop == NULL)
550 return 0;
Anton Blanchard12f04f22013-09-23 12:04:36 +1000551 phys_addr = be64_to_cpu(addr_prop[0]);
552 block_size = be64_to_cpu(addr_prop[1]);
Jon Tollefson658013e2008-07-23 21:27:54 -0700553 if (block_size != (16 * GB))
554 return 0;
555 printk(KERN_INFO "Huge page(16GB) memory: "
556 "addr = 0x%lX size = 0x%lX pages = %d\n",
557 phys_addr, block_size, expected_pages);
Rui Teng23493c12017-01-12 17:09:06 +0800558 if (phys_addr + block_size * expected_pages <= memblock_end_of_DRAM()) {
Yinghai Lu95f72d12010-07-12 14:36:09 +1000559 memblock_reserve(phys_addr, block_size * expected_pages);
Aneesh Kumar K.V79cc38d2017-07-28 10:31:26 +0530560 pseries_add_gpage(phys_addr, block_size, expected_pages);
Jon Tollefson4792adb2008-10-21 15:27:36 +0000561 }
Jon Tollefson658013e2008-07-23 21:27:54 -0700562 return 0;
563}
Tony Breedse16a9c02008-07-31 13:51:42 +1000564#endif /* CONFIG_HUGETLB_PAGE */
Jon Tollefson658013e2008-07-23 21:27:54 -0700565
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000566static void mmu_psize_set_default_penc(void)
567{
568 int bpsize, apsize;
569 for (bpsize = 0; bpsize < MMU_PAGE_COUNT; bpsize++)
570 for (apsize = 0; apsize < MMU_PAGE_COUNT; apsize++)
571 mmu_psize_defs[bpsize].penc[apsize] = -1;
572}
573
Alexander Graf9048e642014-04-01 15:46:05 +0200574#ifdef CONFIG_PPC_64K_PAGES
575
576static bool might_have_hea(void)
577{
578 /*
579 * The HEA ethernet adapter requires awareness of the
580 * GX bus. Without that awareness we can easily assume
581 * we will never see an HEA ethernet device.
582 */
583#ifdef CONFIG_IBMEBUS
Benjamin Herrenschmidt2b4e3ad2016-07-05 15:03:56 +1000584 return !cpu_has_feature(CPU_FTR_ARCH_207S) &&
Michael Ellerman08bf75b2016-10-11 21:15:04 +1100585 firmware_has_feature(FW_FEATURE_SPLPAR);
Alexander Graf9048e642014-04-01 15:46:05 +0200586#else
587 return false;
588#endif
589}
590
591#endif /* #ifdef CONFIG_PPC_64K_PAGES */
592
Michael Ellermanbacf9cf2016-07-26 21:31:59 +1000593static void __init htab_scan_page_sizes(void)
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100594{
595 int rc;
596
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000597 /* se the invalid penc to -1 */
598 mmu_psize_set_default_penc();
599
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100600 /* Default to 4K pages only */
Nicholas Piggin471d7ff2018-02-21 05:08:29 +1000601 memcpy(mmu_psize_defs, mmu_psize_defaults,
602 sizeof(mmu_psize_defaults));
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100603
604 /*
605 * Try to find the available page sizes in the device-tree
606 */
607 rc = of_scan_flat_dt(htab_dt_scan_page_sizes, NULL);
Aneesh Kumar K.Vb8f1b4f2016-07-23 14:42:35 +0530608 if (rc == 0 && early_mmu_has_feature(MMU_FTR_16M_PAGE)) {
Michael Ellermanbacf9cf2016-07-26 21:31:59 +1000609 /*
610 * Nothing in the device-tree, but the CPU supports 16M pages,
611 * so let's fallback on a known size list for 16M capable CPUs.
612 */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100613 memcpy(mmu_psize_defs, mmu_psize_defaults_gp,
614 sizeof(mmu_psize_defaults_gp));
Michael Ellermanbacf9cf2016-07-26 21:31:59 +1000615 }
616
617#ifdef CONFIG_HUGETLB_PAGE
Aneesh Kumar K.V86590e52020-06-22 12:10:19 +0530618 if (!hugetlb_disabled && !early_radix_enabled() ) {
Hari Bathini85975382018-04-10 19:11:31 +0530619 /* Reserve 16G huge page memory sections for huge pages */
620 of_scan_flat_dt(htab_dt_scan_hugepage_blocks, NULL);
621 }
Michael Ellermanbacf9cf2016-07-26 21:31:59 +1000622#endif /* CONFIG_HUGETLB_PAGE */
623}
624
Paul Mackerras0eeede02016-09-02 17:20:43 +1000625/*
626 * Fill in the hpte_page_sizes[] array.
627 * We go through the mmu_psize_defs[] array looking for all the
628 * supported base/actual page size combinations. Each combination
629 * has a unique pagesize encoding (penc) value in the low bits of
630 * the LP field of the HPTE. For actual page sizes less than 1MB,
631 * some of the upper LP bits are used for RPN bits, meaning that
632 * we need to fill in several entries in hpte_page_sizes[].
633 *
634 * In diagrammatic form, with r = RPN bits and z = page size bits:
635 * PTE LP actual page size
636 * rrrr rrrz >=8KB
637 * rrrr rrzz >=16KB
638 * rrrr rzzz >=32KB
639 * rrrr zzzz >=64KB
640 * ...
641 *
642 * The zzzz bits are implementation-specific but are chosen so that
643 * no encoding for a larger page size uses the same value in its
644 * low-order N bits as the encoding for the 2^(12+N) byte page size
645 * (if it exists).
646 */
647static void init_hpte_page_sizes(void)
648{
649 long int ap, bp;
650 long int shift, penc;
651
652 for (bp = 0; bp < MMU_PAGE_COUNT; ++bp) {
653 if (!mmu_psize_defs[bp].shift)
654 continue; /* not a supported page size */
655 for (ap = bp; ap < MMU_PAGE_COUNT; ++ap) {
656 penc = mmu_psize_defs[bp].penc[ap];
Aneesh Kumar K.V10527e82017-11-28 14:04:40 +0530657 if (penc == -1 || !mmu_psize_defs[ap].shift)
Paul Mackerras0eeede02016-09-02 17:20:43 +1000658 continue;
659 shift = mmu_psize_defs[ap].shift - LP_SHIFT;
660 if (shift <= 0)
661 continue; /* should never happen */
662 /*
663 * For page sizes less than 1MB, this loop
664 * replicates the entry for all possible values
665 * of the rrrr bits.
666 */
667 while (penc < (1 << LP_BITS)) {
668 hpte_page_sizes[penc] = (ap << 4) | bp;
669 penc += 1 << shift;
670 }
671 }
672 }
673}
674
Michael Ellermanbacf9cf2016-07-26 21:31:59 +1000675static void __init htab_init_page_sizes(void)
676{
Russell Currey970d54f2019-12-24 17:41:25 +1100677 bool aligned = true;
Paul Mackerras0eeede02016-09-02 17:20:43 +1000678 init_hpte_page_sizes();
679
Joonsoo Kime7df0d82016-03-17 14:17:59 -0700680 if (!debug_pagealloc_enabled()) {
681 /*
682 * Pick a size for the linear mapping. Currently, we only
683 * support 16M, 1M and 4K which is the default
684 */
Joe Perches55bd9ac2020-06-05 07:18:06 -0700685 if (IS_ENABLED(CONFIG_STRICT_KERNEL_RWX) &&
Russell Currey970d54f2019-12-24 17:41:25 +1100686 (unsigned long)_stext % 0x1000000) {
687 if (mmu_psize_defs[MMU_PAGE_16M].shift)
Joe Perches55bd9ac2020-06-05 07:18:06 -0700688 pr_warn("Kernel not 16M aligned, disabling 16M linear map alignment\n");
Russell Currey970d54f2019-12-24 17:41:25 +1100689 aligned = false;
690 }
691
692 if (mmu_psize_defs[MMU_PAGE_16M].shift && aligned)
Joonsoo Kime7df0d82016-03-17 14:17:59 -0700693 mmu_linear_psize = MMU_PAGE_16M;
694 else if (mmu_psize_defs[MMU_PAGE_1M].shift)
695 mmu_linear_psize = MMU_PAGE_1M;
696 }
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100697
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000698#ifdef CONFIG_PPC_64K_PAGES
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100699 /*
700 * Pick a size for the ordinary pages. Default is 4K, we support
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000701 * 64K for user mappings and vmalloc if supported by the processor.
702 * We only use 64k for ioremap if the processor
703 * (and firmware) support cache-inhibited large pages.
704 * If not, we use 4k and set mmu_ci_restrictions so that
705 * hash_page knows to switch processes that use cache-inhibited
706 * mappings to 4k pages.
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100707 */
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000708 if (mmu_psize_defs[MMU_PAGE_64K].shift) {
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100709 mmu_virtual_psize = MMU_PAGE_64K;
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000710 mmu_vmalloc_psize = MMU_PAGE_64K;
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000711 if (mmu_linear_psize == MMU_PAGE_4K)
712 mmu_linear_psize = MMU_PAGE_64K;
Matt Evans44ae3ab2011-04-06 19:48:50 +0000713 if (mmu_has_feature(MMU_FTR_CI_LARGE_PAGE)) {
Paul Mackerrascfe666b2008-03-24 17:41:22 +1100714 /*
Alexander Graf9048e642014-04-01 15:46:05 +0200715 * When running on pSeries using 64k pages for ioremap
716 * would stop us accessing the HEA ethernet. So if we
717 * have the chance of ever seeing one, stay at 4k.
Paul Mackerrascfe666b2008-03-24 17:41:22 +1100718 */
Benjamin Herrenschmidt2b4e3ad2016-07-05 15:03:56 +1000719 if (!might_have_hea())
Paul Mackerrascfe666b2008-03-24 17:41:22 +1100720 mmu_io_psize = MMU_PAGE_64K;
721 } else
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000722 mmu_ci_restrictions = 1;
723 }
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000724#endif /* CONFIG_PPC_64K_PAGES */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100725
Benjamin Herrenschmidtcec08e72008-04-30 15:41:48 +1000726#ifdef CONFIG_SPARSEMEM_VMEMMAP
Christophe Leroy47d99942019-03-29 10:00:00 +0000727 /*
728 * We try to use 16M pages for vmemmap if that is supported
Benjamin Herrenschmidtcec08e72008-04-30 15:41:48 +1000729 * and we have at least 1G of RAM at boot
730 */
731 if (mmu_psize_defs[MMU_PAGE_16M].shift &&
Yinghai Lu95f72d12010-07-12 14:36:09 +1000732 memblock_phys_mem_size() >= 0x40000000)
Benjamin Herrenschmidtcec08e72008-04-30 15:41:48 +1000733 mmu_vmemmap_psize = MMU_PAGE_16M;
Benjamin Herrenschmidtcec08e72008-04-30 15:41:48 +1000734 else
Aneesh Kumar K.V78c94982019-07-01 20:04:41 +0530735 mmu_vmemmap_psize = mmu_virtual_psize;
Benjamin Herrenschmidtcec08e72008-04-30 15:41:48 +1000736#endif /* CONFIG_SPARSEMEM_VMEMMAP */
737
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000738 printk(KERN_DEBUG "Page orders: linear mapping = %d, "
Benjamin Herrenschmidtcec08e72008-04-30 15:41:48 +1000739 "virtual = %d, io = %d"
740#ifdef CONFIG_SPARSEMEM_VMEMMAP
741 ", vmemmap = %d"
742#endif
743 "\n",
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100744 mmu_psize_defs[mmu_linear_psize].shift,
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000745 mmu_psize_defs[mmu_virtual_psize].shift,
Benjamin Herrenschmidtcec08e72008-04-30 15:41:48 +1000746 mmu_psize_defs[mmu_io_psize].shift
747#ifdef CONFIG_SPARSEMEM_VMEMMAP
748 ,mmu_psize_defs[mmu_vmemmap_psize].shift
749#endif
750 );
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100751}
752
753static int __init htab_dt_scan_pftsize(unsigned long node,
754 const char *uname, int depth,
755 void *data)
756{
Rob Herring9d0c4df2014-04-01 23:49:03 -0500757 const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
758 const __be32 *prop;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100759
760 /* We are scanning "cpu" nodes only */
761 if (type == NULL || strcmp(type, "cpu") != 0)
762 return 0;
763
Anton Blanchard12f04f22013-09-23 12:04:36 +1000764 prop = of_get_flat_dt_prop(node, "ibm,pft-size", NULL);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100765 if (prop != NULL) {
766 /* pft_size[0] is the NUMA CEC cookie */
Anton Blanchard12f04f22013-09-23 12:04:36 +1000767 ppc64_pft_size = be32_to_cpu(prop[1]);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100768 return 1;
769 }
770 return 0;
771}
772
David Gibson5c3c7ed2016-02-09 13:32:43 +1000773unsigned htab_shift_for_mem_size(unsigned long mem_size)
774{
775 unsigned memshift = __ilog2(mem_size);
776 unsigned pshift = mmu_psize_defs[mmu_virtual_psize].shift;
777 unsigned pteg_shift;
778
779 /* round mem_size up to next power of 2 */
780 if ((1UL << memshift) < mem_size)
781 memshift += 1;
782
783 /* aim for 2 pages / pteg */
784 pteg_shift = memshift - (pshift + 1);
785
786 /*
787 * 2^11 PTEGS of 128 bytes each, ie. 2^18 bytes is the minimum htab
788 * size permitted by the architecture.
789 */
790 return max(pteg_shift + 7, 18U);
791}
792
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100793static unsigned long __init htab_get_table_size(void)
Paul Mackerras3eac8c62005-10-12 16:58:53 +1000794{
Christophe Leroy47d99942019-03-29 10:00:00 +0000795 /*
796 * If hash size isn't already provided by the platform, we try to
Adrian Bunk943ffb52006-01-10 00:10:13 +0100797 * retrieve it from the device-tree. If it's not there neither, we
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100798 * calculate it now based on the total RAM size
Paul Mackerras3eac8c62005-10-12 16:58:53 +1000799 */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100800 if (ppc64_pft_size == 0)
801 of_scan_flat_dt(htab_dt_scan_pftsize, NULL);
Paul Mackerras3eac8c62005-10-12 16:58:53 +1000802 if (ppc64_pft_size)
803 return 1UL << ppc64_pft_size;
804
David Gibson5c3c7ed2016-02-09 13:32:43 +1000805 return 1UL << htab_shift_for_mem_size(memblock_phys_mem_size());
Paul Mackerras3eac8c62005-10-12 16:58:53 +1000806}
807
Mike Kravetz54b79242005-11-07 16:25:48 -0800808#ifdef CONFIG_MEMORY_HOTPLUG
Bharata B Rao55548a82020-07-27 15:27:04 +0530809static int resize_hpt_for_hotplug(unsigned long new_mem_size)
David Gibson438cc812016-12-09 11:07:38 +1100810{
811 unsigned target_hpt_shift;
812
813 if (!mmu_hash_ops.resize_hpt)
Laurent Vivierf172acb2019-03-13 11:25:28 +0100814 return 0;
David Gibson438cc812016-12-09 11:07:38 +1100815
816 target_hpt_shift = htab_shift_for_mem_size(new_mem_size);
817
818 /*
819 * To avoid lots of HPT resizes if memory size is fluctuating
820 * across a boundary, we deliberately have some hysterisis
821 * here: we immediately increase the HPT size if the target
822 * shift exceeds the current shift, but we won't attempt to
823 * reduce unless the target shift is at least 2 below the
824 * current shift
825 */
Laurent Vivierf172acb2019-03-13 11:25:28 +0100826 if (target_hpt_shift > ppc64_pft_size ||
827 target_hpt_shift < ppc64_pft_size - 1)
828 return mmu_hash_ops.resize_hpt(target_hpt_shift);
David Gibson438cc812016-12-09 11:07:38 +1100829
Laurent Vivierf172acb2019-03-13 11:25:28 +0100830 return 0;
David Gibson438cc812016-12-09 11:07:38 +1100831}
832
Logan Gunthorpe4e00c5a2020-04-10 14:33:32 -0700833int hash__create_section_mapping(unsigned long start, unsigned long end,
834 int nid, pgprot_t prot)
Mike Kravetz54b79242005-11-07 16:25:48 -0800835{
Aneesh Kumar K.Ve0909392019-04-17 18:29:15 +0530836 int rc;
837
838 if (end >= H_VMALLOC_START) {
Colin Ian Kingf341d892019-04-23 16:10:17 +0100839 pr_warn("Outside the supported range\n");
Aneesh Kumar K.Ve0909392019-04-17 18:29:15 +0530840 return -1;
841 }
842
Bharata B Rao55548a82020-07-27 15:27:04 +0530843 resize_hpt_for_hotplug(memblock_phys_mem_size());
844
Aneesh Kumar K.Ve0909392019-04-17 18:29:15 +0530845 rc = htab_bolt_mapping(start, end, __pa(start),
Logan Gunthorpe4e00c5a2020-04-10 14:33:32 -0700846 pgprot_val(prot), mmu_linear_psize,
Aneesh Kumar K.Ve0909392019-04-17 18:29:15 +0530847 mmu_kernel_ssize);
David Gibson1dace6c2016-02-09 13:32:42 +1000848
849 if (rc < 0) {
850 int rc2 = htab_remove_mapping(start, end, mmu_linear_psize,
851 mmu_kernel_ssize);
852 BUG_ON(rc2 && (rc2 != -ENOENT));
853 }
854 return rc;
Mike Kravetz54b79242005-11-07 16:25:48 -0800855}
Badari Pulavartyf8c88032008-01-29 09:19:24 +1100856
Reza Arbab32b53c02017-01-03 14:39:51 -0600857int hash__remove_section_mapping(unsigned long start, unsigned long end)
Badari Pulavartyf8c88032008-01-29 09:19:24 +1100858{
David Gibsonabd0a0e2016-02-09 13:32:40 +1000859 int rc = htab_remove_mapping(start, end, mmu_linear_psize,
860 mmu_kernel_ssize);
Bharata B Rao55548a82020-07-27 15:27:04 +0530861
862 if (resize_hpt_for_hotplug(memblock_phys_mem_size()) == -ENOSPC)
863 pr_warn("Hash collision while resizing HPT\n");
864
David Gibsonabd0a0e2016-02-09 13:32:40 +1000865 return rc;
Badari Pulavartyf8c88032008-01-29 09:19:24 +1100866}
Mike Kravetz54b79242005-11-07 16:25:48 -0800867#endif /* CONFIG_MEMORY_HOTPLUG */
868
Aneesh Kumar K.V50de5962016-04-29 23:25:43 +1000869static void __init hash_init_partition_table(phys_addr_t hash_table,
Aneesh Kumar K.V4b7a3502016-07-13 15:05:26 +0530870 unsigned long htab_size)
Aneesh Kumar K.V50de5962016-04-29 23:25:43 +1000871{
Paul Mackerras9d661952016-11-21 16:00:58 +1100872 mmu_partition_table_init();
Aneesh Kumar K.V50de5962016-04-29 23:25:43 +1000873
874 /*
Paul Mackerras9d661952016-11-21 16:00:58 +1100875 * PS field (VRMA page size) is not used for LPID 0, hence set to 0.
876 * For now, UPRT is 0 and we have no segment table.
Aneesh Kumar K.V50de5962016-04-29 23:25:43 +1000877 */
Aneesh Kumar K.V4b7a3502016-07-13 15:05:26 +0530878 htab_size = __ilog2(htab_size) - 18;
Nicholas Piggin7d805ac2019-09-03 01:29:30 +1000879 mmu_partition_table_set_entry(0, hash_table | htab_size, 0, false);
Aneesh Kumar K.V56547412016-07-13 15:05:25 +0530880 pr_info("Partition table %p\n", partition_tb);
Aneesh Kumar K.V50de5962016-04-29 23:25:43 +1000881}
882
Benjamin Herrenschmidt757c74d2009-03-19 19:34:16 +0000883static void __init htab_initialize(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884{
Michael Ellerman337a7122006-02-21 17:22:55 +1100885 unsigned long table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 unsigned long pteg_count;
Paul Mackerras9e88ba42008-08-30 11:26:27 +1000887 unsigned long prot;
Mike Rapoportb10d6bc2020-10-13 16:58:08 -0700888 phys_addr_t base = 0, size = 0, end;
889 u64 i;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100890
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 DBG(" -> htab_initialize()\n");
892
Matt Evans44ae3ab2011-04-06 19:48:50 +0000893 if (mmu_has_feature(MMU_FTR_1T_SEGMENT)) {
Paul Mackerras1189be62007-10-11 20:37:10 +1000894 mmu_kernel_ssize = MMU_SEGSIZE_1T;
895 mmu_highuser_ssize = MMU_SEGSIZE_1T;
896 printk(KERN_INFO "Using 1TB segments\n");
897 }
898
Nicholas Piggin82a1b8e2020-05-11 22:58:24 +1000899 if (stress_slb_enabled)
900 static_branch_enable(&stress_slb_key);
901
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 /*
903 * Calculate the required size of the htab. We want the number of
904 * PTEGs to equal one half the number of real pages.
Mike Rapoportb10d6bc2020-10-13 16:58:08 -0700905 */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100906 htab_size_bytes = htab_get_table_size();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 pteg_count = htab_size_bytes >> 7;
908
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 htab_hash_mask = pteg_count - 1;
910
Benjamin Herrenschmidt5556ecf2016-07-05 15:03:53 +1000911 if (firmware_has_feature(FW_FEATURE_LPAR) ||
912 firmware_has_feature(FW_FEATURE_PS3_LV1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 /* Using a hypervisor which owns the htab */
914 htab_address = NULL;
Mike Rapoportb10d6bc2020-10-13 16:58:08 -0700915 _SDR1 = 0;
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +0000916#ifdef CONFIG_FA_DUMP
917 /*
918 * If firmware assisted dump is active firmware preserves
919 * the contents of htab along with entire partition memory.
920 * Clear the htab if firmware assisted dump is active so
921 * that we dont end up using old mappings.
922 */
Benjamin Herrenschmidt70257762016-07-05 15:03:58 +1000923 if (is_fadump_active() && mmu_hash_ops.hpte_clear_all)
924 mmu_hash_ops.hpte_clear_all();
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +0000925#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 } else {
Benjamin Herrenschmidt5556ecf2016-07-05 15:03:53 +1000927 unsigned long limit = MEMBLOCK_ALLOC_ANYWHERE;
Michael Ellerman41d824b2008-01-30 01:13:59 +1100928
Benjamin Herrenschmidt5556ecf2016-07-05 15:03:53 +1000929#ifdef CONFIG_PPC_CELL
930 /*
931 * Cell may require the hash table down low when using the
932 * Axon IOMMU in order to fit the dynamic region over it, see
933 * comments in cell/iommu.c
934 */
935 if (fdt_subnode_offset(initial_boot_params, 0, "axon") > 0) {
936 limit = 0x80000000;
937 pr_info("Hash table forced below 2G for Axon IOMMU\n");
938 }
939#endif /* CONFIG_PPC_CELL */
940
Mike Rapoport0ba9e6e2019-03-11 23:29:35 -0700941 table = memblock_phys_alloc_range(htab_size_bytes,
942 htab_size_bytes,
943 0, limit);
944 if (!table)
945 panic("ERROR: Failed to allocate %pa bytes below %pa\n",
946 &htab_size_bytes, &limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
948 DBG("Hash table allocated at %lx, size: %lx\n", table,
949 htab_size_bytes);
950
Michael Ellerman70267a72012-07-25 21:19:50 +0000951 htab_address = __va(table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
953 /* htab absolute addr + encoded htabsize */
Aneesh Kumar K.V4b7a3502016-07-13 15:05:26 +0530954 _SDR1 = table + __ilog2(htab_size_bytes) - 18;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
956 /* Initialize the HPT with no entries */
957 memset((void *)table, 0, htab_size_bytes);
Paul Mackerras799d6042005-11-10 13:37:51 +1100958
Aneesh Kumar K.V50de5962016-04-29 23:25:43 +1000959 if (!cpu_has_feature(CPU_FTR_ARCH_300))
960 /* Set SDR1 */
961 mtspr(SPRN_SDR1, _SDR1);
962 else
Aneesh Kumar K.V4b7a3502016-07-13 15:05:26 +0530963 hash_init_partition_table(table, htab_size_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 }
965
David Gibsonf5ea64d2008-10-12 17:54:24 +0000966 prot = pgprot_val(PAGE_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000968#ifdef CONFIG_DEBUG_PAGEALLOC
Joonsoo Kime7df0d82016-03-17 14:17:59 -0700969 if (debug_pagealloc_enabled()) {
970 linear_map_hash_count = memblock_end_of_DRAM() >> PAGE_SHIFT;
Mike Rapoportf8067142019-03-07 16:30:48 -0800971 linear_map_hash_slots = memblock_alloc_try_nid(
972 linear_map_hash_count, 1, MEMBLOCK_LOW_LIMIT,
973 ppc64_rma_size, NUMA_NO_NODE);
Mike Rapoport8a7f97b2019-03-11 23:30:31 -0700974 if (!linear_map_hash_slots)
975 panic("%s: Failed to allocate %lu bytes max_addr=%pa\n",
976 __func__, linear_map_hash_count, &ppc64_rma_size);
Joonsoo Kime7df0d82016-03-17 14:17:59 -0700977 }
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000978#endif /* CONFIG_DEBUG_PAGEALLOC */
979
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 /* create bolted the linear mapping in the hash table */
Mike Rapoportb10d6bc2020-10-13 16:58:08 -0700981 for_each_mem_range(i, &base, &end) {
982 size = end - base;
983 base = (unsigned long)__va(base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Sachin P. Sant5c339912009-12-13 21:15:12 +0000985 DBG("creating mapping for region: %lx..%lx (prot: %lx)\n",
Paul Mackerras9e88ba42008-08-30 11:26:27 +1000986 base, size, prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
Aneesh Kumar K.Ve0909392019-04-17 18:29:15 +0530988 if ((base + size) >= H_VMALLOC_START) {
Colin Ian Kingf341d892019-04-23 16:10:17 +0100989 pr_warn("Outside the supported range\n");
Aneesh Kumar K.Ve0909392019-04-17 18:29:15 +0530990 continue;
991 }
992
Michael Ellermancaf80e52006-03-21 20:45:51 +1100993 BUG_ON(htab_bolt_mapping(base, base + size, __pa(base),
Paul Mackerras9e88ba42008-08-30 11:26:27 +1000994 prot, mmu_linear_psize, mmu_kernel_ssize));
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -0700995 }
996 memblock_set_current_limit(MEMBLOCK_ALLOC_ANYWHERE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
998 /*
999 * If we have a memory_limit and we've allocated TCEs then we need to
1000 * explicitly map the TCE area at the top of RAM. We also cope with the
1001 * case that the TCEs start below memory_limit.
1002 * tce_alloc_start/end are 16MB aligned so the mapping should work
1003 * for either 4K or 16MB pages.
1004 */
1005 if (tce_alloc_start) {
Michael Ellermanb5666f72005-12-05 10:24:33 -06001006 tce_alloc_start = (unsigned long)__va(tce_alloc_start);
1007 tce_alloc_end = (unsigned long)__va(tce_alloc_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
1009 if (base + size >= tce_alloc_start)
1010 tce_alloc_start = base + size + 1;
1011
Michael Ellermancaf80e52006-03-21 20:45:51 +11001012 BUG_ON(htab_bolt_mapping(tce_alloc_start, tce_alloc_end,
Benjamin Herrenschmidtbc033b62008-08-05 16:19:56 +10001013 __pa(tce_alloc_start), prot,
Paul Mackerras1189be62007-10-11 20:37:10 +10001014 mmu_linear_psize, mmu_kernel_ssize));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 }
1016
Michael Ellerman7d0daae2006-06-23 18:16:38 +10001017
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 DBG(" <- htab_initialize()\n");
1019}
1020#undef KB
1021#undef MB
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Michael Ellermanbacf9cf2016-07-26 21:31:59 +10001023void __init hash__early_init_devtree(void)
1024{
1025 /* Initialize segment sizes */
1026 of_scan_flat_dt(htab_dt_scan_seg_sizes, NULL);
1027
1028 /* Initialize page sizes */
1029 htab_scan_page_sizes();
1030}
1031
YueHaibingd667edc2019-05-04 18:24:27 +08001032static struct hash_mm_context init_hash_mm_context;
Aneesh Kumar K.V756d08d2016-04-29 23:25:57 +10001033void __init hash__early_init_mmu(void)
Paul Mackerras799d6042005-11-10 13:37:51 +11001034{
Ram Pai9d2edb12017-11-06 00:50:47 -08001035#ifndef CONFIG_PPC_64K_PAGES
Aneesh Kumar K.V6aa59f52017-03-28 15:21:12 +11001036 /*
Ram Pai9d2edb12017-11-06 00:50:47 -08001037 * We have code in __hash_page_4K() and elsewhere, which assumes it can
Aneesh Kumar K.V6aa59f52017-03-28 15:21:12 +11001038 * do the following:
1039 * new_pte |= (slot << H_PAGE_F_GIX_SHIFT) & (H_PAGE_F_SECOND | H_PAGE_F_GIX);
1040 *
1041 * Where the slot number is between 0-15, and values of 8-15 indicate
1042 * the secondary bucket. For that code to work H_PAGE_F_SECOND and
1043 * H_PAGE_F_GIX must occupy four contiguous bits in the PTE, and
1044 * H_PAGE_F_SECOND must be placed above H_PAGE_F_GIX. Assert that here
1045 * with a BUILD_BUG_ON().
1046 */
1047 BUILD_BUG_ON(H_PAGE_F_SECOND != (1ul << (H_PAGE_F_GIX_SHIFT + 3)));
Ram Pai9d2edb12017-11-06 00:50:47 -08001048#endif /* CONFIG_PPC_64K_PAGES */
Aneesh Kumar K.V6aa59f52017-03-28 15:21:12 +11001049
Michael Ellermanbacf9cf2016-07-26 21:31:59 +10001050 htab_init_page_sizes();
1051
Aneesh Kumar K.Vdd1842a2016-04-29 23:25:49 +10001052 /*
1053 * initialize page table size
1054 */
Aneesh Kumar K.V5ed7ecd2016-04-29 23:26:23 +10001055 __pte_frag_nr = H_PTE_FRAG_NR;
1056 __pte_frag_size_shift = H_PTE_FRAG_SIZE_SHIFT;
Aneesh Kumar K.V8a6c6972018-04-16 16:57:22 +05301057 __pmd_frag_nr = H_PMD_FRAG_NR;
1058 __pmd_frag_size_shift = H_PMD_FRAG_SIZE_SHIFT;
Aneesh Kumar K.V5ed7ecd2016-04-29 23:26:23 +10001059
Aneesh Kumar K.Vdd1842a2016-04-29 23:25:49 +10001060 __pte_index_size = H_PTE_INDEX_SIZE;
1061 __pmd_index_size = H_PMD_INDEX_SIZE;
1062 __pud_index_size = H_PUD_INDEX_SIZE;
1063 __pgd_index_size = H_PGD_INDEX_SIZE;
Aneesh Kumar K.Vfae22112018-02-11 20:30:06 +05301064 __pud_cache_index = H_PUD_CACHE_INDEX;
Aneesh Kumar K.Vdd1842a2016-04-29 23:25:49 +10001065 __pte_table_size = H_PTE_TABLE_SIZE;
1066 __pmd_table_size = H_PMD_TABLE_SIZE;
1067 __pud_table_size = H_PUD_TABLE_SIZE;
1068 __pgd_table_size = H_PGD_TABLE_SIZE;
Aneesh Kumar K.Va2f41eb2016-04-29 23:26:19 +10001069 /*
1070 * 4k use hugepd format, so for hash set then to
1071 * zero
1072 */
Aneesh Kumar K.Vda7ad362018-09-20 23:39:42 +05301073 __pmd_val_bits = HASH_PMD_VAL_BITS;
1074 __pud_val_bits = HASH_PUD_VAL_BITS;
1075 __pgd_val_bits = HASH_PGD_VAL_BITS;
Aneesh Kumar K.Vd6a99962016-04-29 23:26:21 +10001076
1077 __kernel_virt_start = H_KERN_VIRT_START;
Aneesh Kumar K.Vd6a99962016-04-29 23:26:21 +10001078 __vmalloc_start = H_VMALLOC_START;
1079 __vmalloc_end = H_VMALLOC_END;
Michael Ellerman63ee9b22017-08-01 20:29:22 +10001080 __kernel_io_start = H_KERN_IO_START;
Aneesh Kumar K.Va35a3c62019-04-17 18:29:13 +05301081 __kernel_io_end = H_KERN_IO_END;
Aneesh Kumar K.V0034d392019-04-17 18:29:14 +05301082 vmemmap = (struct page *)H_VMEMMAP_START;
Aneesh Kumar K.Vd6a99962016-04-29 23:26:21 +10001083 ioremap_bot = IOREMAP_BASE;
1084
Darren Stevensbfa37082016-06-29 21:06:28 +01001085#ifdef CONFIG_PCI
1086 pci_io_base = ISA_IO_BASE;
1087#endif
1088
Benjamin Herrenschmidt166dd7d2016-07-05 15:03:51 +10001089 /* Select appropriate backend */
1090 if (firmware_has_feature(FW_FEATURE_PS3_LV1))
1091 ps3_early_mm_init();
1092 else if (firmware_has_feature(FW_FEATURE_LPAR))
Michael Ellerman6364e842016-07-26 10:33:03 +10001093 hpte_init_pseries();
Stephen Rothwellfbef66f2016-07-28 12:35:02 +10001094 else if (IS_ENABLED(CONFIG_PPC_NATIVE))
Benjamin Herrenschmidt166dd7d2016-07-05 15:03:51 +10001095 hpte_init_native();
1096
Michael Ellerman73536442016-07-25 11:54:41 +10001097 if (!mmu_hash_ops.hpte_insert)
1098 panic("hash__early_init_mmu: No MMU hash ops defined!\n");
1099
Christophe Leroy47d99942019-03-29 10:00:00 +00001100 /*
1101 * Initialize the MMU Hash table and create the linear mapping
Michael Ellerman376af592014-07-10 12:29:19 +10001102 * of memory. Has to be done before SLB initialization as this is
1103 * currently where the page size encoding is obtained.
Benjamin Herrenschmidt757c74d2009-03-19 19:34:16 +00001104 */
1105 htab_initialize();
1106
Aneesh Kumar K.V70110182019-04-17 18:33:50 +05301107 init_mm.context.hash_context = &init_hash_mm_context;
Christophe Leroy5953fb42019-04-25 14:29:36 +00001108 mm_ctx_set_slb_addr_limit(&init_mm.context, SLB_ADDR_LIMIT_DEFAULT);
Aneesh Kumar K.V67fda38f2019-04-17 18:33:49 +05301109
Aneesh Kumar K.V56547412016-07-13 15:05:25 +05301110 pr_info("Initializing hash mmu with SLB\n");
Michael Ellerman376af592014-07-10 12:29:19 +10001111 /* Initialize SLB management */
Michael Ellerman13b3d132014-07-10 12:29:20 +10001112 slb_initialize();
Nicholas Piggind4748272017-12-24 01:15:50 +10001113
1114 if (cpu_has_feature(CPU_FTR_ARCH_206)
1115 && cpu_has_feature(CPU_FTR_HVMODE))
1116 tlbiel_all();
Benjamin Herrenschmidt757c74d2009-03-19 19:34:16 +00001117}
1118
1119#ifdef CONFIG_SMP
Aneesh Kumar K.V756d08d2016-04-29 23:25:57 +10001120void hash__early_init_mmu_secondary(void)
Benjamin Herrenschmidt757c74d2009-03-19 19:34:16 +00001121{
1122 /* Initialize hash table for that CPU */
Aneesh Kumar K.Vb5dcc602016-04-29 23:26:12 +10001123 if (!firmware_has_feature(FW_FEATURE_LPAR)) {
Aneesh Kumar K.Vcac4a182016-11-17 15:46:23 +05301124
Aneesh Kumar K.Vb5dcc602016-04-29 23:26:12 +10001125 if (!cpu_has_feature(CPU_FTR_ARCH_300))
1126 mtspr(SPRN_SDR1, _SDR1);
1127 else
Claudio Carvalho52231342019-08-22 00:48:36 -03001128 set_ptcr_when_no_uv(__pa(partition_tb) |
1129 (PATB_SIZE_SHIFT - 12));
Aneesh Kumar K.Vb5dcc602016-04-29 23:26:12 +10001130 }
Michael Ellerman376af592014-07-10 12:29:19 +10001131 /* Initialize SLB */
Michael Ellerman13b3d132014-07-10 12:29:20 +10001132 slb_initialize();
Nicholas Piggind4748272017-12-24 01:15:50 +10001133
1134 if (cpu_has_feature(CPU_FTR_ARCH_206)
1135 && cpu_has_feature(CPU_FTR_HVMODE))
1136 tlbiel_all();
Aneesh Kumar K.Ve0d8e992020-07-09 08:59:42 +05301137
Aneesh Kumar K.V1e4e4bc2020-08-17 16:03:01 +05301138#ifdef CONFIG_PPC_MEM_KEYS
1139 if (mmu_has_feature(MMU_FTR_PKEY))
Aneesh Kumar K.V6553fb72020-08-10 15:56:23 +05301140 mtspr(SPRN_UAMOR, default_uamor);
Aneesh Kumar K.V1e4e4bc2020-08-17 16:03:01 +05301141#endif
Paul Mackerras799d6042005-11-10 13:37:51 +11001142}
Benjamin Herrenschmidt757c74d2009-03-19 19:34:16 +00001143#endif /* CONFIG_SMP */
Paul Mackerras799d6042005-11-10 13:37:51 +11001144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145/*
1146 * Called by asm hashtable.S for doing lazy icache flush
1147 */
1148unsigned int hash_page_do_lazy_icache(unsigned int pp, pte_t pte, int trap)
1149{
1150 struct page *page;
1151
Benjamin Herrenschmidt76c8e252005-11-08 11:21:05 +11001152 if (!pfn_valid(pte_pfn(pte)))
1153 return pp;
1154
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 page = pte_page(pte);
1156
1157 /* page is dirty */
Aneesh Kumar K.Vec94b9b2021-02-03 10:28:11 +05301158 if (!test_bit(PG_dcache_clean, &page->flags) && !PageReserved(page)) {
Xiongwei Song7153d4b2021-04-14 19:00:33 +08001159 if (trap == INTERRUPT_INST_STORAGE) {
David Gibson0895ecd2009-10-26 19:24:31 +00001160 flush_dcache_icache_page(page);
Aneesh Kumar K.Vec94b9b2021-02-03 10:28:11 +05301161 set_bit(PG_dcache_clean, &page->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 } else
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001163 pp |= HPTE_R_N;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 }
1165 return pp;
1166}
1167
Paul Mackerras3a8247c2008-06-18 15:29:12 +10001168#ifdef CONFIG_PPC_MM_SLICES
Michael Ellerman54be0b92018-10-02 23:56:39 +10001169static unsigned int get_paca_psize(unsigned long addr)
Paul Mackerras3a8247c2008-06-18 15:29:12 +10001170{
Christophe Leroy15472422018-02-22 15:27:28 +01001171 unsigned char *psizes;
Aneesh Kumar K.V7aa07272012-09-10 02:52:52 +00001172 unsigned long index, mask_index;
Paul Mackerras3a8247c2008-06-18 15:29:12 +10001173
1174 if (addr < SLICE_LOW_TOP) {
Michael Ellerman54be0b92018-10-02 23:56:39 +10001175 psizes = get_paca()->mm_ctx_low_slices_psize;
Paul Mackerras3a8247c2008-06-18 15:29:12 +10001176 index = GET_LOW_SLICE_INDEX(addr);
Christophe Leroy15472422018-02-22 15:27:28 +01001177 } else {
Michael Ellerman54be0b92018-10-02 23:56:39 +10001178 psizes = get_paca()->mm_ctx_high_slices_psize;
Christophe Leroy15472422018-02-22 15:27:28 +01001179 index = GET_HIGH_SLICE_INDEX(addr);
Paul Mackerras3a8247c2008-06-18 15:29:12 +10001180 }
Aneesh Kumar K.V7aa07272012-09-10 02:52:52 +00001181 mask_index = index & 0x1;
Christophe Leroy15472422018-02-22 15:27:28 +01001182 return (psizes[index >> 1] >> (mask_index * 4)) & 0xF;
Paul Mackerras3a8247c2008-06-18 15:29:12 +10001183}
1184
1185#else
Michael Ellerman54be0b92018-10-02 23:56:39 +10001186unsigned int get_paca_psize(unsigned long addr)
Paul Mackerras3a8247c2008-06-18 15:29:12 +10001187{
Michael Ellerman54be0b92018-10-02 23:56:39 +10001188 return get_paca()->mm_ctx_user_psize;
Paul Mackerras3a8247c2008-06-18 15:29:12 +10001189}
1190#endif
1191
Paul Mackerras721151d2007-04-03 21:24:02 +10001192/*
1193 * Demote a segment to using 4k pages.
1194 * For now this makes the whole process use 4k pages.
1195 */
Paul Mackerras721151d2007-04-03 21:24:02 +10001196#ifdef CONFIG_PPC_64K_PAGES
Paul Mackerrasfa282372008-01-24 08:35:13 +11001197void demote_segment_4k(struct mm_struct *mm, unsigned long addr)
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +10001198{
Michael Ellerman54be0b92018-10-02 23:56:39 +10001199 if (get_slice_psize(mm, addr) == MMU_PAGE_4K)
Paul Mackerras721151d2007-04-03 21:24:02 +10001200 return;
Paul Mackerras3a8247c2008-06-18 15:29:12 +10001201 slice_set_range_psize(mm, addr, 1, MMU_PAGE_4K);
Ian Munsiebe3ebfe2014-10-08 19:54:52 +11001202 copro_flush_all_slbs(mm);
Michael Ellerman54be0b92018-10-02 23:56:39 +10001203 if ((get_paca_psize(addr) != MMU_PAGE_4K) && (current->mm == mm)) {
1204
1205 copy_mm_to_paca(mm);
Nicholas Piggin94ee42722018-10-03 00:27:58 +10001206 slb_flush_and_restore_bolted();
Michael Ellerman54be0b92018-10-02 23:56:39 +10001207 }
Paul Mackerras721151d2007-04-03 21:24:02 +10001208}
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +10001209#endif /* CONFIG_PPC_64K_PAGES */
Paul Mackerras721151d2007-04-03 21:24:02 +10001210
Paul Mackerrasfa282372008-01-24 08:35:13 +11001211#ifdef CONFIG_PPC_SUBPAGE_PROT
1212/*
1213 * This looks up a 2-bit protection code for a 4k subpage of a 64k page.
1214 * Userspace sets the subpage permissions using the subpage_prot system call.
1215 *
1216 * Result is 0: full permissions, _PAGE_RW: read-only,
Aneesh Kumar K.V73a14412016-04-29 23:25:31 +10001217 * _PAGE_RWX: no access.
Paul Mackerrasfa282372008-01-24 08:35:13 +11001218 */
David Gibsond28513b2009-11-26 18:56:04 +00001219static int subpage_protection(struct mm_struct *mm, unsigned long ea)
Paul Mackerrasfa282372008-01-24 08:35:13 +11001220{
Aneesh Kumar K.V60458fb2019-04-17 18:33:48 +05301221 struct subpage_prot_table *spt = mm_ctx_subpage_prot(&mm->context);
Paul Mackerrasfa282372008-01-24 08:35:13 +11001222 u32 spp = 0;
1223 u32 **sbpm, *sbpp;
1224
Aneesh Kumar K.Vef629cc2019-04-17 18:33:51 +05301225 if (!spt)
1226 return 0;
1227
Paul Mackerrasfa282372008-01-24 08:35:13 +11001228 if (ea >= spt->maxaddr)
1229 return 0;
Anton Blanchardb0d436c2013-08-07 02:01:24 +10001230 if (ea < 0x100000000UL) {
Paul Mackerrasfa282372008-01-24 08:35:13 +11001231 /* addresses below 4GB use spt->low_prot */
1232 sbpm = spt->low_prot;
1233 } else {
1234 sbpm = spt->protptrs[ea >> SBP_L3_SHIFT];
1235 if (!sbpm)
1236 return 0;
1237 }
1238 sbpp = sbpm[(ea >> SBP_L2_SHIFT) & (SBP_L2_COUNT - 1)];
1239 if (!sbpp)
1240 return 0;
1241 spp = sbpp[(ea >> PAGE_SHIFT) & (SBP_L1_COUNT - 1)];
1242
1243 /* extract 2-bit bitfield for this 4k subpage */
1244 spp >>= 30 - 2 * ((ea >> 12) & 0xf);
1245
Aneesh Kumar K.V73a14412016-04-29 23:25:31 +10001246 /*
1247 * 0 -> full premission
1248 * 1 -> Read only
1249 * 2 -> no access.
1250 * We return the flag that need to be cleared.
1251 */
1252 spp = ((spp & 2) ? _PAGE_RWX : 0) | ((spp & 1) ? _PAGE_WRITE : 0);
Paul Mackerrasfa282372008-01-24 08:35:13 +11001253 return spp;
1254}
1255
1256#else /* CONFIG_PPC_SUBPAGE_PROT */
David Gibsond28513b2009-11-26 18:56:04 +00001257static inline int subpage_protection(struct mm_struct *mm, unsigned long ea)
Paul Mackerrasfa282372008-01-24 08:35:13 +11001258{
1259 return 0;
1260}
1261#endif
1262
Benjamin Herrenschmidt4b8692c2010-07-23 10:31:13 +10001263void hash_failure_debug(unsigned long ea, unsigned long access,
1264 unsigned long vsid, unsigned long trap,
Aneesh Kumar K.Vd8139eb2013-04-28 09:37:37 +00001265 int ssize, int psize, int lpsize, unsigned long pte)
Benjamin Herrenschmidt4b8692c2010-07-23 10:31:13 +10001266{
1267 if (!printk_ratelimit())
1268 return;
1269 pr_info("mm: Hashing failure ! EA=0x%lx access=0x%lx current=%s\n",
1270 ea, access, current->comm);
Aneesh Kumar K.Vd8139eb2013-04-28 09:37:37 +00001271 pr_info(" trap=0x%lx vsid=0x%lx ssize=%d base psize=%d psize %d pte=0x%lx\n",
1272 trap, vsid, ssize, psize, lpsize, pte);
Benjamin Herrenschmidt4b8692c2010-07-23 10:31:13 +10001273}
1274
Michael Ellerman54be0b92018-10-02 23:56:39 +10001275static void check_paca_psize(unsigned long ea, struct mm_struct *mm,
1276 int psize, bool user_region)
1277{
1278 if (user_region) {
1279 if (psize != get_paca_psize(ea)) {
1280 copy_mm_to_paca(mm);
Nicholas Piggin94ee42722018-10-03 00:27:58 +10001281 slb_flush_and_restore_bolted();
Michael Ellerman54be0b92018-10-02 23:56:39 +10001282 }
1283 } else if (get_paca()->vmalloc_sllp !=
1284 mmu_psize_defs[mmu_vmalloc_psize].sllp) {
1285 get_paca()->vmalloc_sllp =
1286 mmu_psize_defs[mmu_vmalloc_psize].sllp;
1287 slb_vmalloc_update();
1288 }
1289}
1290
Christophe Leroy47d99942019-03-29 10:00:00 +00001291/*
1292 * Result code is:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 * 0 - handled
1294 * 1 - normal page fault
1295 * -1 - critical hash insertion error
Paul Mackerrasfa282372008-01-24 08:35:13 +11001296 * -2 - access not permitted by subpage protection mechanism
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 */
Aneesh Kumar K.Vaefa5682014-12-04 11:00:14 +05301298int hash_page_mm(struct mm_struct *mm, unsigned long ea,
1299 unsigned long access, unsigned long trap,
1300 unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301{
Aneesh Kumar K.V891121e2015-10-09 08:32:21 +05301302 bool is_thp;
David Gibsona1128f82009-12-16 14:29:56 +00001303 pgd_t *pgdir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 unsigned long vsid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 pte_t *ptep;
David Gibsona4fe3ce2009-10-26 19:24:31 +00001306 unsigned hugeshift;
Aneesh Kumar K.Vaefa5682014-12-04 11:00:14 +05301307 int rc, user_region = 0;
Paul Mackerras1189be62007-10-11 20:37:10 +10001308 int psize, ssize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001310 DBG_LOW("hash_page(ea=%016lx, access=%lx, trap=%lx\n",
1311 ea, access, trap);
Aneesh Kumar K.Vcfcb3d82015-04-14 13:05:57 +05301312 trace_hash_fault(ea, access, trap);
David Gibson1f8d4192005-05-05 16:15:13 -07001313
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001314 /* Get region & vsid */
Aneesh Kumar K.V0034d392019-04-17 18:29:14 +05301315 switch (get_region_id(ea)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 case USER_REGION_ID:
1317 user_region = 1;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001318 if (! mm) {
1319 DBG_LOW(" user region with no mm !\n");
Li Zhongba12eed2013-05-13 16:16:41 +00001320 rc = 1;
1321 goto bail;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001322 }
Michael Ellerman54be0b92018-10-02 23:56:39 +10001323 psize = get_slice_psize(mm, ea);
Paul Mackerras1189be62007-10-11 20:37:10 +10001324 ssize = user_segment_size(ea);
Aneesh Kumar K.Vf384796c2018-03-26 15:34:48 +05301325 vsid = get_user_vsid(&mm->context, ea, ssize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 case VMALLOC_REGION_ID:
Paul Mackerras1189be62007-10-11 20:37:10 +10001328 vsid = get_kernel_vsid(ea, mmu_kernel_ssize);
Aneesh Kumar K.V0034d392019-04-17 18:29:14 +05301329 psize = mmu_vmalloc_psize;
1330 ssize = mmu_kernel_ssize;
Aneesh Kumar K.Vd94b8272020-11-27 10:14:10 +05301331 flags |= HPTE_USE_KERNEL_KEY;
Aneesh Kumar K.V0034d392019-04-17 18:29:14 +05301332 break;
1333
1334 case IO_REGION_ID:
1335 vsid = get_kernel_vsid(ea, mmu_kernel_ssize);
1336 psize = mmu_io_psize;
Paul Mackerras1189be62007-10-11 20:37:10 +10001337 ssize = mmu_kernel_ssize;
Aneesh Kumar K.Vd94b8272020-11-27 10:14:10 +05301338 flags |= HPTE_USE_KERNEL_KEY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 default:
Christophe Leroy47d99942019-03-29 10:00:00 +00001341 /*
1342 * Not a valid range
1343 * Send the problem up to do_page_fault()
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 */
Li Zhongba12eed2013-05-13 16:16:41 +00001345 rc = 1;
1346 goto bail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 }
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001348 DBG_LOW(" mm=%p, mm->pgdir=%p, vsid=%016lx\n", mm, mm->pgd, vsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
Aneesh Kumar K.Vc60ac562013-03-13 03:34:54 +00001350 /* Bad address. */
1351 if (!vsid) {
1352 DBG_LOW("Bad address!\n");
Li Zhongba12eed2013-05-13 16:16:41 +00001353 rc = 1;
1354 goto bail;
Aneesh Kumar K.Vc60ac562013-03-13 03:34:54 +00001355 }
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001356 /* Get pgdir */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 pgdir = mm->pgd;
Li Zhongba12eed2013-05-13 16:16:41 +00001358 if (pgdir == NULL) {
1359 rc = 1;
1360 goto bail;
1361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001363 /* Check CPU locality */
Benjamin Herrenschmidtb426e4b2017-07-24 14:28:01 +10001364 if (user_region && mm_is_thread_local(mm))
Aneesh Kumar K.Vaefa5682014-12-04 11:00:14 +05301365 flags |= HPTE_LOCAL_UPDATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +10001367#ifndef CONFIG_PPC_64K_PAGES
Christophe Leroy47d99942019-03-29 10:00:00 +00001368 /*
1369 * If we use 4K pages and our psize is not 4K, then we might
David Gibsona4fe3ce2009-10-26 19:24:31 +00001370 * be hitting a special driver mapping, and need to align the
1371 * address before we fetch the PTE.
1372 *
1373 * It could also be a hugepage mapping, in which case this is
1374 * not necessary, but it's not harmful, either.
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +10001375 */
1376 if (psize != MMU_PAGE_4K)
1377 ea &= ~((1ul << mmu_psize_defs[psize].shift) - 1);
1378#endif /* CONFIG_PPC_64K_PAGES */
1379
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001380 /* Get PTE and page size from page tables */
Aneesh Kumar K.V94171b12017-07-27 11:54:53 +05301381 ptep = find_linux_pte(pgdir, ea, &is_thp, &hugeshift);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001382 if (ptep == NULL || !pte_present(*ptep)) {
1383 DBG_LOW(" no PTE !\n");
Li Zhongba12eed2013-05-13 16:16:41 +00001384 rc = 1;
1385 goto bail;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001386 }
1387
Aneesh Kumar K.Vec4abf12020-05-05 12:47:10 +05301388 /*
1389 * Add _PAGE_PRESENT to the required access perm. If there are parallel
1390 * updates to the pte that can possibly clear _PAGE_PTE, catch that too.
1391 *
1392 * We can safely use the return pte address in rest of the function
1393 * because we do set H_PAGE_BUSY which prevents further updates to pte
1394 * from generic code.
1395 */
1396 access |= _PAGE_PRESENT | _PAGE_PTE;
Benjamin Herrenschmidtca91e6c2010-07-23 08:53:23 +10001397
Christophe Leroy47d99942019-03-29 10:00:00 +00001398 /*
1399 * Pre-check access permissions (will be re-checked atomically
Benjamin Herrenschmidtca91e6c2010-07-23 08:53:23 +10001400 * in __hash_page_XX but this pre-check is a fast path
1401 */
Aneesh Kumar K.Vac29c642016-04-29 23:25:34 +10001402 if (!check_pte_access(access, pte_val(*ptep))) {
Benjamin Herrenschmidtca91e6c2010-07-23 08:53:23 +10001403 DBG_LOW(" no access !\n");
Li Zhongba12eed2013-05-13 16:16:41 +00001404 rc = 1;
1405 goto bail;
Benjamin Herrenschmidtca91e6c2010-07-23 08:53:23 +10001406 }
1407
Li Zhongba12eed2013-05-13 16:16:41 +00001408 if (hugeshift) {
Aneesh Kumar K.V891121e2015-10-09 08:32:21 +05301409 if (is_thp)
Aneesh Kumar K.V6d492ec2013-06-20 14:30:21 +05301410 rc = __hash_page_thp(ea, access, vsid, (pmd_t *)ptep,
Aneesh Kumar K.Vaefa5682014-12-04 11:00:14 +05301411 trap, flags, ssize, psize);
Aneesh Kumar K.V6d492ec2013-06-20 14:30:21 +05301412#ifdef CONFIG_HUGETLB_PAGE
1413 else
1414 rc = __hash_page_huge(ea, access, vsid, ptep, trap,
Aneesh Kumar K.Vaefa5682014-12-04 11:00:14 +05301415 flags, ssize, hugeshift, psize);
Aneesh Kumar K.V6d492ec2013-06-20 14:30:21 +05301416#else
1417 else {
1418 /*
1419 * if we have hugeshift, and is not transhuge with
1420 * hugetlb disabled, something is really wrong.
1421 */
1422 rc = 1;
1423 WARN_ON(1);
1424 }
1425#endif
Michael Ellerman54be0b92018-10-02 23:56:39 +10001426 if (current->mm == mm)
1427 check_paca_psize(ea, mm, psize, user_region);
1428
Li Zhongba12eed2013-05-13 16:16:41 +00001429 goto bail;
1430 }
David Gibsona4fe3ce2009-10-26 19:24:31 +00001431
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001432#ifndef CONFIG_PPC_64K_PAGES
1433 DBG_LOW(" i-pte: %016lx\n", pte_val(*ptep));
1434#else
1435 DBG_LOW(" i-pte: %016lx %016lx\n", pte_val(*ptep),
1436 pte_val(*(ptep + PTRS_PER_PTE)));
1437#endif
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001438 /* Do actual hashing */
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +10001439#ifdef CONFIG_PPC_64K_PAGES
Aneesh Kumar K.V945537d2016-04-29 23:25:45 +10001440 /* If H_PAGE_4K_PFN is set, make sure this is a 4k segment */
1441 if ((pte_val(*ptep) & H_PAGE_4K_PFN) && psize == MMU_PAGE_64K) {
Paul Mackerras721151d2007-04-03 21:24:02 +10001442 demote_segment_4k(mm, ea);
1443 psize = MMU_PAGE_4K;
1444 }
1445
Christophe Leroy47d99942019-03-29 10:00:00 +00001446 /*
1447 * If this PTE is non-cacheable and we have restrictions on
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +10001448 * using non cacheable large pages, then we switch to 4k
1449 */
Aneesh Kumar K.V30bda412016-04-29 23:25:38 +10001450 if (mmu_ci_restrictions && psize == MMU_PAGE_64K && pte_ci(*ptep)) {
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +10001451 if (user_region) {
1452 demote_segment_4k(mm, ea);
1453 psize = MMU_PAGE_4K;
1454 } else if (ea < VMALLOC_END) {
1455 /*
1456 * some driver did a non-cacheable mapping
1457 * in vmalloc space, so switch vmalloc
1458 * to 4k pages
1459 */
1460 printk(KERN_ALERT "Reducing vmalloc segment "
1461 "to 4kB pages because of "
1462 "non-cacheable mapping\n");
1463 psize = mmu_vmalloc_psize = MMU_PAGE_4K;
Ian Munsiebe3ebfe2014-10-08 19:54:52 +11001464 copro_flush_all_slbs(mm);
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +10001465 }
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +10001466 }
Michael Ellerman09567e72014-05-28 18:21:17 +10001467
Aneesh Kumar K.V0863d7f2015-11-28 22:39:33 +05301468#endif /* CONFIG_PPC_64K_PAGES */
1469
Michael Ellerman54be0b92018-10-02 23:56:39 +10001470 if (current->mm == mm)
1471 check_paca_psize(ea, mm, psize, user_region);
1472
Michael Ellerman73b341e2015-08-07 16:19:47 +10001473#ifdef CONFIG_PPC_64K_PAGES
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +10001474 if (psize == MMU_PAGE_64K)
Aneesh Kumar K.Vaefa5682014-12-04 11:00:14 +05301475 rc = __hash_page_64K(ea, access, vsid, ptep, trap,
1476 flags, ssize);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001477 else
Michael Ellerman73b341e2015-08-07 16:19:47 +10001478#endif /* CONFIG_PPC_64K_PAGES */
Paul Mackerrasfa282372008-01-24 08:35:13 +11001479 {
David Gibsona1128f82009-12-16 14:29:56 +00001480 int spp = subpage_protection(mm, ea);
Paul Mackerrasfa282372008-01-24 08:35:13 +11001481 if (access & spp)
1482 rc = -2;
1483 else
1484 rc = __hash_page_4K(ea, access, vsid, ptep, trap,
Aneesh Kumar K.Vaefa5682014-12-04 11:00:14 +05301485 flags, ssize, spp);
Paul Mackerrasfa282372008-01-24 08:35:13 +11001486 }
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001487
Christophe Leroy47d99942019-03-29 10:00:00 +00001488 /*
1489 * Dump some info in case of hash insertion failure, they should
Benjamin Herrenschmidt4b8692c2010-07-23 10:31:13 +10001490 * never happen so it is really useful to know if/when they do
1491 */
1492 if (rc == -1)
1493 hash_failure_debug(ea, access, vsid, trap, ssize, psize,
Aneesh Kumar K.Vd8139eb2013-04-28 09:37:37 +00001494 psize, pte_val(*ptep));
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001495#ifndef CONFIG_PPC_64K_PAGES
1496 DBG_LOW(" o-pte: %016lx\n", pte_val(*ptep));
1497#else
1498 DBG_LOW(" o-pte: %016lx %016lx\n", pte_val(*ptep),
1499 pte_val(*(ptep + PTRS_PER_PTE)));
1500#endif
1501 DBG_LOW(" -> rc=%d\n", rc);
Li Zhongba12eed2013-05-13 16:16:41 +00001502
1503bail:
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001504 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505}
Ian Munsiea1dca3462014-10-08 19:54:58 +11001506EXPORT_SYMBOL_GPL(hash_page_mm);
1507
Aneesh Kumar K.Vaefa5682014-12-04 11:00:14 +05301508int hash_page(unsigned long ea, unsigned long access, unsigned long trap,
1509 unsigned long dsisr)
Ian Munsiea1dca3462014-10-08 19:54:58 +11001510{
Aneesh Kumar K.Vaefa5682014-12-04 11:00:14 +05301511 unsigned long flags = 0;
Ian Munsiea1dca3462014-10-08 19:54:58 +11001512 struct mm_struct *mm = current->mm;
1513
Aneesh Kumar K.V0034d392019-04-17 18:29:14 +05301514 if ((get_region_id(ea) == VMALLOC_REGION_ID) ||
1515 (get_region_id(ea) == IO_REGION_ID))
Ian Munsiea1dca3462014-10-08 19:54:58 +11001516 mm = &init_mm;
1517
Aneesh Kumar K.Vaefa5682014-12-04 11:00:14 +05301518 if (dsisr & DSISR_NOHPTE)
1519 flags |= HPTE_NOHPTE_UPDATE;
1520
1521 return hash_page_mm(mm, ea, access, trap, flags);
Ian Munsiea1dca3462014-10-08 19:54:58 +11001522}
Arnd Bergmann67207b92005-11-15 15:53:48 -05001523EXPORT_SYMBOL_GPL(hash_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
Nicholas Piggin5567b1e2021-06-30 17:46:13 +10001525DECLARE_INTERRUPT_HANDLER(__do_hash_fault);
1526DEFINE_INTERRUPT_HANDLER(__do_hash_fault)
Aneesh Kumar K.V106713a2015-12-01 09:06:44 +05301527{
Nicholas Piggina01a3f22021-01-30 23:08:16 +10001528 unsigned long ea = regs->dar;
1529 unsigned long dsisr = regs->dsisr;
Aneesh Kumar K.Vc7d54842016-04-29 23:25:30 +10001530 unsigned long access = _PAGE_PRESENT | _PAGE_READ;
Aneesh Kumar K.V106713a2015-12-01 09:06:44 +05301531 unsigned long flags = 0;
Nicholas Piggina4922f52021-01-30 23:08:15 +10001532 struct mm_struct *mm;
1533 unsigned int region_id;
Nicholas Piggina01a3f22021-01-30 23:08:16 +10001534 long err;
Aneesh Kumar K.V106713a2015-12-01 09:06:44 +05301535
Nicholas Piggin5567b1e2021-06-30 17:46:13 +10001536 if (unlikely(dsisr & (DSISR_BAD_FAULT_64S | DSISR_KEYFAULT))) {
1537 hash__do_page_fault(regs);
1538 return;
1539 }
1540
Nicholas Piggina4922f52021-01-30 23:08:15 +10001541 region_id = get_region_id(ea);
Aneesh Kumar K.V0034d392019-04-17 18:29:14 +05301542 if ((region_id == VMALLOC_REGION_ID) || (region_id == IO_REGION_ID))
Aneesh Kumar K.V106713a2015-12-01 09:06:44 +05301543 mm = &init_mm;
Nicholas Piggina4922f52021-01-30 23:08:15 +10001544 else
1545 mm = current->mm;
Aneesh Kumar K.V106713a2015-12-01 09:06:44 +05301546
1547 if (dsisr & DSISR_NOHPTE)
1548 flags |= HPTE_NOHPTE_UPDATE;
1549
1550 if (dsisr & DSISR_ISSTORE)
Aneesh Kumar K.Vc7d54842016-04-29 23:25:30 +10001551 access |= _PAGE_WRITE;
Aneesh Kumar K.V106713a2015-12-01 09:06:44 +05301552 /*
Aneesh Kumar K.Vac29c642016-04-29 23:25:34 +10001553 * We set _PAGE_PRIVILEGED only when
1554 * kernel mode access kernel space.
1555 *
1556 * _PAGE_PRIVILEGED is NOT set
1557 * 1) when kernel mode access user space
1558 * 2) user space access kernel space.
Aneesh Kumar K.V106713a2015-12-01 09:06:44 +05301559 */
Aneesh Kumar K.Vac29c642016-04-29 23:25:34 +10001560 access |= _PAGE_PRIVILEGED;
Nicholas Piggina4922f52021-01-30 23:08:15 +10001561 if (user_mode(regs) || (region_id == USER_REGION_ID))
Aneesh Kumar K.Vac29c642016-04-29 23:25:34 +10001562 access &= ~_PAGE_PRIVILEGED;
Aneesh Kumar K.V106713a2015-12-01 09:06:44 +05301563
Xiongwei Song7153d4b2021-04-14 19:00:33 +08001564 if (TRAP(regs) == INTERRUPT_INST_STORAGE)
Aneesh Kumar K.V106713a2015-12-01 09:06:44 +05301565 access |= _PAGE_EXEC;
1566
Nicholas Piggin1479e3d2021-03-16 20:52:05 +10001567 err = hash_page_mm(mm, ea, access, TRAP(regs), flags);
Nicholas Piggina4922f52021-01-30 23:08:15 +10001568 if (unlikely(err < 0)) {
1569 // failed to instert a hash PTE due to an hypervisor error
1570 if (user_mode(regs)) {
1571 if (IS_ENABLED(CONFIG_PPC_SUBPAGE_PROT) && err == -2)
1572 _exception(SIGSEGV, regs, SEGV_ACCERR, ea);
1573 else
1574 _exception(SIGBUS, regs, BUS_ADRERR, ea);
1575 } else {
Nicholas Piggin8458c622021-01-30 23:08:21 +10001576 bad_page_fault(regs, SIGBUS);
Nicholas Piggina4922f52021-01-30 23:08:15 +10001577 }
1578 err = 0;
1579
Nicholas Piggin5567b1e2021-06-30 17:46:13 +10001580 } else if (err) {
1581 hash__do_page_fault(regs);
1582 }
Nicholas Pigginbf0e2372021-01-30 23:08:25 +10001583}
1584
Nicholas Piggin3a965702021-01-30 23:08:38 +10001585/*
1586 * The _RAW interrupt entry checks for the in_nmi() case before
1587 * running the full handler.
1588 */
1589DEFINE_INTERRUPT_HANDLER_RAW(do_hash_fault)
Nicholas Pigginbf0e2372021-01-30 23:08:25 +10001590{
Nicholas Pigginbf0e2372021-01-30 23:08:25 +10001591 /*
1592 * If we are in an "NMI" (e.g., an interrupt when soft-disabled), then
1593 * don't call hash_page, just fail the fault. This is required to
1594 * prevent re-entrancy problems in the hash code, namely perf
1595 * interrupts hitting while something holds H_PAGE_BUSY, and taking a
1596 * hash fault. See the comment in hash_preload().
1597 *
1598 * We come here as a result of a DSI at a point where we don't want
1599 * to call hash_page, such as when we are accessing memory (possibly
1600 * user memory) inside a PMU interrupt that occurred while interrupts
1601 * were soft-disabled. We want to invoke the exception handler for
1602 * the access, or panic if there isn't a handler.
1603 */
1604 if (unlikely(in_nmi())) {
Nicholas Piggin3a965702021-01-30 23:08:38 +10001605 do_bad_page_fault_segv(regs);
Nicholas Pigginbf0e2372021-01-30 23:08:25 +10001606 return 0;
1607 }
1608
Nicholas Piggin5567b1e2021-06-30 17:46:13 +10001609 __do_hash_fault(regs);
Nicholas Piggina4922f52021-01-30 23:08:15 +10001610
Nicholas Pigginc45ba4f42021-03-16 20:42:03 +10001611 return 0;
Aneesh Kumar K.V106713a2015-12-01 09:06:44 +05301612}
1613
Michael Ellerman8bbc9b72016-05-06 16:46:00 +10001614#ifdef CONFIG_PPC_MM_SLICES
1615static bool should_hash_preload(struct mm_struct *mm, unsigned long ea)
1616{
Michael Ellerman54be0b92018-10-02 23:56:39 +10001617 int psize = get_slice_psize(mm, ea);
Michael Ellermanaac55d72016-05-06 16:47:12 +10001618
Michael Ellerman8bbc9b72016-05-06 16:46:00 +10001619 /* We only prefault standard pages for now */
Aneesh Kumar K.V60458fb2019-04-17 18:33:48 +05301620 if (unlikely(psize != mm_ctx_user_psize(&mm->context)))
Michael Ellermanaac55d72016-05-06 16:47:12 +10001621 return false;
1622
1623 /*
1624 * Don't prefault if subpage protection is enabled for the EA.
1625 */
1626 if (unlikely((psize == MMU_PAGE_4K) && subpage_protection(mm, ea)))
Michael Ellerman8bbc9b72016-05-06 16:46:00 +10001627 return false;
1628
1629 return true;
1630}
1631#else
1632static bool should_hash_preload(struct mm_struct *mm, unsigned long ea)
1633{
1634 return true;
1635}
1636#endif
1637
Aneesh Kumar K.V2f924472020-05-05 12:47:12 +05301638static void hash_preload(struct mm_struct *mm, pte_t *ptep, unsigned long ea,
Christophe Leroye5a1edb2019-08-16 05:41:42 +00001639 bool is_exec, unsigned long trap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640{
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001641 unsigned long vsid;
Michael Neuling0b97fee2010-11-17 18:52:45 +00001642 pgd_t *pgdir;
Aneesh Kumar K.Vaefa5682014-12-04 11:00:14 +05301643 int rc, ssize, update_flags = 0;
Christophe Leroy34eb1382018-10-09 13:51:54 +00001644 unsigned long access = _PAGE_PRESENT | _PAGE_READ | (is_exec ? _PAGE_EXEC : 0);
Nicholas Piggin909adfc2020-07-27 16:09:47 +10001645 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
Aneesh Kumar K.V0034d392019-04-17 18:29:14 +05301647 BUG_ON(get_region_id(ea) != USER_REGION_ID);
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +10001648
Michael Ellerman8bbc9b72016-05-06 16:46:00 +10001649 if (!should_hash_preload(mm, ea))
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001650 return;
1651
1652 DBG_LOW("hash_preload(mm=%p, mm->pgdir=%p, ea=%016lx, access=%lx,"
1653 " trap=%lx\n", mm, mm->pgd, ea, access, trap);
1654
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +10001655 /* Get Linux PTE if available */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001656 pgdir = mm->pgd;
1657 if (pgdir == NULL)
1658 return;
Aneesh Kumar K.V0ac52dd2013-06-20 14:30:22 +05301659
1660 /* Get VSID */
1661 ssize = user_segment_size(ea);
Aneesh Kumar K.Vf384796c2018-03-26 15:34:48 +05301662 vsid = get_user_vsid(&mm->context, ea, ssize);
Aneesh Kumar K.V0ac52dd2013-06-20 14:30:22 +05301663 if (!vsid)
1664 return;
Aneesh Kumar K.V0ac52dd2013-06-20 14:30:22 +05301665
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +10001666#ifdef CONFIG_PPC_64K_PAGES
Aneesh Kumar K.V945537d2016-04-29 23:25:45 +10001667 /* If either H_PAGE_4K_PFN or cache inhibited is set (and we are on
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +10001668 * a 64K kernel), then we don't preload, hash_page() will take
1669 * care of it once we actually try to access the page.
1670 * That way we don't have to duplicate all of the logic for segment
1671 * page size demotion here
Aneesh Kumar K.V2f924472020-05-05 12:47:12 +05301672 * Called with PTL held, hence can be sure the value won't change in
1673 * between.
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +10001674 */
Aneesh Kumar K.V945537d2016-04-29 23:25:45 +10001675 if ((pte_val(*ptep) & H_PAGE_4K_PFN) || pte_ci(*ptep))
Aneesh Kumar K.V2f924472020-05-05 12:47:12 +05301676 return;
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +10001677#endif /* CONFIG_PPC_64K_PAGES */
1678
Nicholas Piggin909adfc2020-07-27 16:09:47 +10001679 /*
1680 * __hash_page_* must run with interrupts off, as it sets the
1681 * H_PAGE_BUSY bit. It's possible for perf interrupts to hit at any
1682 * time and may take a hash fault reading the user stack, see
1683 * read_user_stack_slow() in the powerpc/perf code.
1684 *
1685 * If that takes a hash fault on the same page as we lock here, it
1686 * will bail out when seeing H_PAGE_BUSY set, and retry the access
1687 * leading to an infinite loop.
1688 *
1689 * Disabling interrupts here does not prevent perf interrupts, but it
1690 * will prevent them taking hash faults (see the NMI test in
1691 * do_hash_page), then read_user_stack's copy_from_user_nofault will
1692 * fail and perf will fall back to read_user_stack_slow(), which
1693 * walks the Linux page tables.
1694 *
1695 * Interrupts must also be off for the duration of the
1696 * mm_is_thread_local test and update, to prevent preempt running the
1697 * mm on another CPU (XXX: this may be racy vs kthread_use_mm).
1698 */
1699 local_irq_save(flags);
1700
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +10001701 /* Is that local to this CPU ? */
Benjamin Herrenschmidtb426e4b2017-07-24 14:28:01 +10001702 if (mm_is_thread_local(mm))
Aneesh Kumar K.Vaefa5682014-12-04 11:00:14 +05301703 update_flags |= HPTE_LOCAL_UPDATE;
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +10001704
1705 /* Hash it in */
Michael Ellerman73b341e2015-08-07 16:19:47 +10001706#ifdef CONFIG_PPC_64K_PAGES
Aneesh Kumar K.V60458fb2019-04-17 18:33:48 +05301707 if (mm_ctx_user_psize(&mm->context) == MMU_PAGE_64K)
Aneesh Kumar K.Vaefa5682014-12-04 11:00:14 +05301708 rc = __hash_page_64K(ea, access, vsid, ptep, trap,
1709 update_flags, ssize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 else
Michael Ellerman73b341e2015-08-07 16:19:47 +10001711#endif /* CONFIG_PPC_64K_PAGES */
Aneesh Kumar K.Vaefa5682014-12-04 11:00:14 +05301712 rc = __hash_page_4K(ea, access, vsid, ptep, trap, update_flags,
1713 ssize, subpage_protection(mm, ea));
Benjamin Herrenschmidt4b8692c2010-07-23 10:31:13 +10001714
1715 /* Dump some info in case of hash insertion failure, they should
1716 * never happen so it is really useful to know if/when they do
1717 */
1718 if (rc == -1)
1719 hash_failure_debug(ea, access, vsid, trap, ssize,
Aneesh Kumar K.V60458fb2019-04-17 18:33:48 +05301720 mm_ctx_user_psize(&mm->context),
1721 mm_ctx_user_psize(&mm->context),
Aneesh Kumar K.Vd8139eb2013-04-28 09:37:37 +00001722 pte_val(*ptep));
Nicholas Piggin909adfc2020-07-27 16:09:47 +10001723
1724 local_irq_restore(flags);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001725}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
Christophe Leroye5a1edb2019-08-16 05:41:42 +00001727/*
1728 * This is called at the end of handling a user page fault, when the
1729 * fault has been handled by updating a PTE in the linux page tables.
1730 * We use it to preload an HPTE into the hash table corresponding to
1731 * the updated linux PTE.
1732 *
1733 * This must always be called with the pte lock held.
1734 */
1735void update_mmu_cache(struct vm_area_struct *vma, unsigned long address,
1736 pte_t *ptep)
1737{
1738 /*
1739 * We don't need to worry about _PAGE_PRESENT here because we are
1740 * called with either mm->page_table_lock held or ptl lock held
1741 */
1742 unsigned long trap;
1743 bool is_exec;
1744
Nicholas Piggin18594f92020-05-04 22:29:07 +10001745 if (radix_enabled())
Christophe Leroye5a1edb2019-08-16 05:41:42 +00001746 return;
Christophe Leroye5a1edb2019-08-16 05:41:42 +00001747
1748 /* We only want HPTEs for linux PTEs that have _PAGE_ACCESSED set */
1749 if (!pte_young(*ptep) || address >= TASK_SIZE)
1750 return;
1751
1752 /*
1753 * We try to figure out if we are coming from an instruction
1754 * access fault and pass that down to __hash_page so we avoid
1755 * double-faulting on execution of fresh text. We have to test
1756 * for regs NULL since init will get here first thing at boot.
1757 *
1758 * We also avoid filling the hash if not coming from a fault.
1759 */
1760
1761 trap = current->thread.regs ? TRAP(current->thread.regs) : 0UL;
1762 switch (trap) {
1763 case 0x300:
1764 is_exec = false;
1765 break;
1766 case 0x400:
1767 is_exec = true;
1768 break;
1769 default:
1770 return;
1771 }
1772
Aneesh Kumar K.V2f924472020-05-05 12:47:12 +05301773 hash_preload(vma->vm_mm, ptep, address, is_exec, trap);
Christophe Leroye5a1edb2019-08-16 05:41:42 +00001774}
1775
Rui Tengf1a55ce2016-09-02 14:17:26 +08001776#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1777static inline void tm_flush_hash_page(int local)
1778{
1779 /*
1780 * Transactions are not aborted by tlbiel, only tlbie. Without, syncing a
1781 * page back to a block device w/PIO could pick up transactional data
1782 * (bad!) so we force an abort here. Before the sync the page will be
1783 * made read-only, which will flush_hash_page. BIG ISSUE here: if the
1784 * kernel uses a page from userspace without unmapping it first, it may
1785 * see the speculated version.
1786 */
1787 if (local && cpu_has_feature(CPU_FTR_TM) && current->thread.regs &&
1788 MSR_TM_ACTIVE(current->thread.regs->msr)) {
1789 tm_enable();
1790 tm_abort(TM_CAUSE_TLBI);
1791 }
1792}
1793#else
1794static inline void tm_flush_hash_page(int local)
1795{
1796}
1797#endif
1798
Ram Pai318995b2017-11-06 00:50:46 -08001799/*
1800 * Return the global hash slot, corresponding to the given PTE, which contains
1801 * the HPTE.
1802 */
1803unsigned long pte_get_hash_gslot(unsigned long vpn, unsigned long shift,
1804 int ssize, real_pte_t rpte, unsigned int subpg_index)
1805{
1806 unsigned long hash, gslot, hidx;
1807
1808 hash = hpt_hash(vpn, shift, ssize);
1809 hidx = __rpte_to_hidx(rpte, subpg_index);
1810 if (hidx & _PTEIDX_SECONDARY)
1811 hash = ~hash;
1812 gslot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
1813 gslot += hidx & _PTEIDX_GROUP_IX;
1814 return gslot;
1815}
1816
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +00001817void flush_hash_page(unsigned long vpn, real_pte_t pte, int psize, int ssize,
Aneesh Kumar K.Vaefa5682014-12-04 11:00:14 +05301818 unsigned long flags)
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001819{
Ram Paia8548682017-11-06 00:50:51 -08001820 unsigned long index, shift, gslot;
Aneesh Kumar K.Vaefa5682014-12-04 11:00:14 +05301821 int local = flags & HPTE_LOCAL_UPDATE;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001822
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +00001823 DBG_LOW("flush_hash_page(vpn=%016lx)\n", vpn);
1824 pte_iterate_hashed_subpages(pte, psize, vpn, index, shift) {
Ram Paia8548682017-11-06 00:50:51 -08001825 gslot = pte_get_hash_gslot(vpn, shift, ssize, pte, index);
1826 DBG_LOW(" sub %ld: gslot=%lx\n", index, gslot);
Aneesh Kumar K.Vdb3d8532013-06-20 14:30:13 +05301827 /*
1828 * We use same base page size and actual psize, because we don't
1829 * use these functions for hugepage
1830 */
Ram Paia8548682017-11-06 00:50:51 -08001831 mmu_hash_ops.hpte_invalidate(gslot, vpn, psize, psize,
Benjamin Herrenschmidt70257762016-07-05 15:03:58 +10001832 ssize, local);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001833 } pte_iterate_hashed_end();
Michael Neulingbc2a9402013-02-13 16:21:40 +00001834
Rui Tengf1a55ce2016-09-02 14:17:26 +08001835 tm_flush_hash_page(local);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836}
1837
Aneesh Kumar K.Vf1581bf2014-11-02 21:15:27 +05301838#ifdef CONFIG_TRANSPARENT_HUGEPAGE
1839void flush_hash_hugepage(unsigned long vsid, unsigned long addr,
Aneesh Kumar K.Vaefa5682014-12-04 11:00:14 +05301840 pmd_t *pmdp, unsigned int psize, int ssize,
1841 unsigned long flags)
Aneesh Kumar K.Vf1581bf2014-11-02 21:15:27 +05301842{
1843 int i, max_hpte_count, valid;
1844 unsigned long s_addr;
1845 unsigned char *hpte_slot_array;
1846 unsigned long hidx, shift, vpn, hash, slot;
Aneesh Kumar K.Vaefa5682014-12-04 11:00:14 +05301847 int local = flags & HPTE_LOCAL_UPDATE;
Aneesh Kumar K.Vf1581bf2014-11-02 21:15:27 +05301848
1849 s_addr = addr & HPAGE_PMD_MASK;
1850 hpte_slot_array = get_hpte_slot_array(pmdp);
1851 /*
1852 * IF we try to do a HUGE PTE update after a withdraw is done.
1853 * we will find the below NULL. This happens when we do
Kefeng Wang9ef258b2019-09-23 15:37:41 -07001854 * split_huge_pmd
Aneesh Kumar K.Vf1581bf2014-11-02 21:15:27 +05301855 */
1856 if (!hpte_slot_array)
1857 return;
1858
Benjamin Herrenschmidt70257762016-07-05 15:03:58 +10001859 if (mmu_hash_ops.hugepage_invalidate) {
1860 mmu_hash_ops.hugepage_invalidate(vsid, s_addr, hpte_slot_array,
1861 psize, ssize, local);
Aneesh Kumar K.Vd557b092014-11-02 21:15:28 +05301862 goto tm_abort;
1863 }
Aneesh Kumar K.Vf1581bf2014-11-02 21:15:27 +05301864 /*
1865 * No bluk hpte removal support, invalidate each entry
1866 */
1867 shift = mmu_psize_defs[psize].shift;
1868 max_hpte_count = HPAGE_PMD_SIZE >> shift;
1869 for (i = 0; i < max_hpte_count; i++) {
1870 /*
1871 * 8 bits per each hpte entries
1872 * 000| [ secondary group (one bit) | hidx (3 bits) | valid bit]
1873 */
1874 valid = hpte_valid(hpte_slot_array, i);
1875 if (!valid)
1876 continue;
1877 hidx = hpte_hash_index(hpte_slot_array, i);
1878
1879 /* get the vpn */
1880 addr = s_addr + (i * (1ul << shift));
1881 vpn = hpt_vpn(addr, vsid, ssize);
1882 hash = hpt_hash(vpn, shift, ssize);
1883 if (hidx & _PTEIDX_SECONDARY)
1884 hash = ~hash;
1885
1886 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
1887 slot += hidx & _PTEIDX_GROUP_IX;
Benjamin Herrenschmidt70257762016-07-05 15:03:58 +10001888 mmu_hash_ops.hpte_invalidate(slot, vpn, psize,
1889 MMU_PAGE_16M, ssize, local);
Aneesh Kumar K.Vf1581bf2014-11-02 21:15:27 +05301890 }
Aneesh Kumar K.Vd557b092014-11-02 21:15:28 +05301891tm_abort:
Rui Tengf1a55ce2016-09-02 14:17:26 +08001892 tm_flush_hash_page(local);
Aneesh Kumar K.Vf1581bf2014-11-02 21:15:27 +05301893}
1894#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
1895
Benjamin Herrenschmidt61b1a942005-09-20 13:52:50 +10001896void flush_hash_range(unsigned long number, int local)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897{
Benjamin Herrenschmidt70257762016-07-05 15:03:58 +10001898 if (mmu_hash_ops.flush_hash_range)
1899 mmu_hash_ops.flush_hash_range(number, local);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001900 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 int i;
Benjamin Herrenschmidt61b1a942005-09-20 13:52:50 +10001902 struct ppc64_tlb_batch *batch =
Christoph Lameter69111ba2014-10-21 15:23:25 -05001903 this_cpu_ptr(&ppc64_tlb_batch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
1905 for (i = 0; i < number; i++)
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +00001906 flush_hash_page(batch->vpn[i], batch->pte[i],
Paul Mackerras1189be62007-10-11 20:37:10 +10001907 batch->psize, batch->ssize, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 }
1909}
1910
Li Zhongb170bd32013-04-15 16:53:19 +00001911long hpte_insert_repeating(unsigned long hash, unsigned long vpn,
1912 unsigned long pa, unsigned long rflags,
1913 unsigned long vflags, int psize, int ssize)
1914{
1915 unsigned long hpte_group;
1916 long slot;
1917
1918repeat:
Aneesh Kumar K.V1531cff2018-06-29 14:06:29 +05301919 hpte_group = (hash & htab_hash_mask) * HPTES_PER_GROUP;
Li Zhongb170bd32013-04-15 16:53:19 +00001920
1921 /* Insert into the hash table, primary slot */
Benjamin Herrenschmidt70257762016-07-05 15:03:58 +10001922 slot = mmu_hash_ops.hpte_insert(hpte_group, vpn, pa, rflags, vflags,
1923 psize, psize, ssize);
Li Zhongb170bd32013-04-15 16:53:19 +00001924
1925 /* Primary is full, try the secondary */
1926 if (unlikely(slot == -1)) {
Aneesh Kumar K.V1531cff2018-06-29 14:06:29 +05301927 hpte_group = (~hash & htab_hash_mask) * HPTES_PER_GROUP;
Benjamin Herrenschmidt70257762016-07-05 15:03:58 +10001928 slot = mmu_hash_ops.hpte_insert(hpte_group, vpn, pa, rflags,
1929 vflags | HPTE_V_SECONDARY,
1930 psize, psize, ssize);
Li Zhongb170bd32013-04-15 16:53:19 +00001931 if (slot == -1) {
1932 if (mftb() & 0x1)
Aneesh Kumar K.V1531cff2018-06-29 14:06:29 +05301933 hpte_group = (hash & htab_hash_mask) *
1934 HPTES_PER_GROUP;
Li Zhongb170bd32013-04-15 16:53:19 +00001935
Benjamin Herrenschmidt70257762016-07-05 15:03:58 +10001936 mmu_hash_ops.hpte_remove(hpte_group);
Li Zhongb170bd32013-04-15 16:53:19 +00001937 goto repeat;
1938 }
1939 }
1940
1941 return slot;
1942}
1943
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +10001944#ifdef CONFIG_DEBUG_PAGEALLOC
1945static void kernel_map_linear_page(unsigned long vaddr, unsigned long lmi)
1946{
Li Zhong016af592013-04-15 16:53:20 +00001947 unsigned long hash;
Paul Mackerras1189be62007-10-11 20:37:10 +10001948 unsigned long vsid = get_kernel_vsid(vaddr, mmu_kernel_ssize);
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +00001949 unsigned long vpn = hpt_vpn(vaddr, vsid, mmu_kernel_ssize);
Aneesh Kumar K.Vd94b8272020-11-27 10:14:10 +05301950 unsigned long mode = htab_convert_pte_flags(pgprot_val(PAGE_KERNEL), HPTE_USE_KERNEL_KEY);
Li Zhong016af592013-04-15 16:53:20 +00001951 long ret;
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +10001952
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +00001953 hash = hpt_hash(vpn, PAGE_SHIFT, mmu_kernel_ssize);
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +10001954
Aneesh Kumar K.Vc60ac562013-03-13 03:34:54 +00001955 /* Don't create HPTE entries for bad address */
1956 if (!vsid)
1957 return;
Li Zhong016af592013-04-15 16:53:20 +00001958
1959 ret = hpte_insert_repeating(hash, vpn, __pa(vaddr), mode,
1960 HPTE_V_BOLTED,
1961 mmu_linear_psize, mmu_kernel_ssize);
1962
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +10001963 BUG_ON (ret < 0);
1964 spin_lock(&linear_map_hash_lock);
1965 BUG_ON(linear_map_hash_slots[lmi] & 0x80);
1966 linear_map_hash_slots[lmi] = ret | 0x80;
1967 spin_unlock(&linear_map_hash_lock);
1968}
1969
1970static void kernel_unmap_linear_page(unsigned long vaddr, unsigned long lmi)
1971{
Paul Mackerras1189be62007-10-11 20:37:10 +10001972 unsigned long hash, hidx, slot;
1973 unsigned long vsid = get_kernel_vsid(vaddr, mmu_kernel_ssize);
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +00001974 unsigned long vpn = hpt_vpn(vaddr, vsid, mmu_kernel_ssize);
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +10001975
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +00001976 hash = hpt_hash(vpn, PAGE_SHIFT, mmu_kernel_ssize);
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +10001977 spin_lock(&linear_map_hash_lock);
1978 BUG_ON(!(linear_map_hash_slots[lmi] & 0x80));
1979 hidx = linear_map_hash_slots[lmi] & 0x7f;
1980 linear_map_hash_slots[lmi] = 0;
1981 spin_unlock(&linear_map_hash_lock);
1982 if (hidx & _PTEIDX_SECONDARY)
1983 hash = ~hash;
1984 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
1985 slot += hidx & _PTEIDX_GROUP_IX;
Benjamin Herrenschmidt70257762016-07-05 15:03:58 +10001986 mmu_hash_ops.hpte_invalidate(slot, vpn, mmu_linear_psize,
1987 mmu_linear_psize,
1988 mmu_kernel_ssize, 0);
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +10001989}
1990
Joel Stanley4f703e72021-10-14 08:04:38 +10301991void hash__kernel_map_pages(struct page *page, int numpages, int enable)
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +10001992{
1993 unsigned long flags, vaddr, lmi;
1994 int i;
1995
1996 local_irq_save(flags);
1997 for (i = 0; i < numpages; i++, page++) {
1998 vaddr = (unsigned long)page_address(page);
1999 lmi = __pa(vaddr) >> PAGE_SHIFT;
2000 if (lmi >= linear_map_hash_count)
2001 continue;
2002 if (enable)
2003 kernel_map_linear_page(vaddr, lmi);
2004 else
2005 kernel_unmap_linear_page(vaddr, lmi);
2006 }
2007 local_irq_restore(flags);
2008}
2009#endif /* CONFIG_DEBUG_PAGEALLOC */
Benjamin Herrenschmidtcd3db0c2010-07-06 15:39:02 -07002010
Aneesh Kumar K.V756d08d2016-04-29 23:25:57 +10002011void hash__setup_initial_memory_limit(phys_addr_t first_memblock_base,
Benjamin Herrenschmidtcd3db0c2010-07-06 15:39:02 -07002012 phys_addr_t first_memblock_size)
2013{
Christophe Leroy47d99942019-03-29 10:00:00 +00002014 /*
2015 * We don't currently support the first MEMBLOCK not mapping 0
Benjamin Herrenschmidtcd3db0c2010-07-06 15:39:02 -07002016 * physical on those processors
2017 */
2018 BUG_ON(first_memblock_base != 0);
2019
Nicholas Piggin1513c332017-12-22 21:17:08 +10002020 /*
2021 * On virtualized systems the first entry is our RMA region aka VRMA,
2022 * non-virtualized 64-bit hash MMU systems don't have a limitation
2023 * on real mode access.
2024 *
Nicholas Pigginc610d652017-12-22 21:17:12 +10002025 * For guests on platforms before POWER9, we clamp the it limit to 1G
2026 * to avoid some funky things such as RTAS bugs etc...
Suraj Jitindar Singhda0ef932019-07-10 15:20:18 +10002027 *
2028 * On POWER9 we limit to 1TB in case the host erroneously told us that
2029 * the RMA was >1TB. Effective address bits 0:23 are treated as zero
2030 * (meaning the access is aliased to zero i.e. addr = addr % 1TB)
2031 * for virtual real mode addressing and so it doesn't make sense to
2032 * have an area larger than 1TB as it can't be addressed.
Benjamin Herrenschmidtcd3db0c2010-07-06 15:39:02 -07002033 */
Nicholas Piggin1513c332017-12-22 21:17:08 +10002034 if (!early_cpu_has_feature(CPU_FTR_HVMODE)) {
Nicholas Pigginc610d652017-12-22 21:17:12 +10002035 ppc64_rma_size = first_memblock_size;
2036 if (!early_cpu_has_feature(CPU_FTR_ARCH_300))
2037 ppc64_rma_size = min_t(u64, ppc64_rma_size, 0x40000000);
Suraj Jitindar Singhda0ef932019-07-10 15:20:18 +10002038 else
2039 ppc64_rma_size = min_t(u64, ppc64_rma_size,
2040 1UL << SID_SHIFT_1T);
Benjamin Herrenschmidtcd3db0c2010-07-06 15:39:02 -07002041
Nicholas Piggin1513c332017-12-22 21:17:08 +10002042 /* Finally limit subsequent allocations */
2043 memblock_set_current_limit(ppc64_rma_size);
2044 } else {
2045 ppc64_rma_size = ULONG_MAX;
2046 }
Benjamin Herrenschmidtcd3db0c2010-07-06 15:39:02 -07002047}
David Gibsondbcf9292016-12-09 11:07:36 +11002048
2049#ifdef CONFIG_DEBUG_FS
2050
2051static int hpt_order_get(void *data, u64 *val)
2052{
2053 *val = ppc64_pft_size;
2054 return 0;
2055}
2056
2057static int hpt_order_set(void *data, u64 val)
2058{
Gautham R. Shenoyc784be42019-05-15 13:15:52 +05302059 int ret;
2060
David Gibsondbcf9292016-12-09 11:07:36 +11002061 if (!mmu_hash_ops.resize_hpt)
2062 return -ENODEV;
2063
Gautham R. Shenoyc784be42019-05-15 13:15:52 +05302064 cpus_read_lock();
2065 ret = mmu_hash_ops.resize_hpt(val);
2066 cpus_read_unlock();
2067
2068 return ret;
David Gibsondbcf9292016-12-09 11:07:36 +11002069}
2070
YueHaibing7cd47742019-01-09 12:10:58 +00002071DEFINE_DEBUGFS_ATTRIBUTE(fops_hpt_order, hpt_order_get, hpt_order_set, "%llu\n");
David Gibsondbcf9292016-12-09 11:07:36 +11002072
2073static int __init hash64_debugfs(void)
2074{
Aneesh Kumar K.Vdbf77fed2021-08-12 18:58:31 +05302075 debugfs_create_file("hpt_order", 0600, arch_debugfs_dir, NULL,
Greg Kroah-Hartman08f6a792020-02-09 11:58:58 +01002076 &fops_hpt_order);
David Gibsondbcf9292016-12-09 11:07:36 +11002077 return 0;
2078}
2079machine_device_initcall(pseries, hash64_debugfs);
David Gibsondbcf9292016-12-09 11:07:36 +11002080#endif /* CONFIG_DEBUG_FS */
Christophe Leroye4dccf92019-04-26 16:36:39 +00002081
2082void __init print_system_hash_info(void)
2083{
2084 pr_info("ppc64_pft_size = 0x%llx\n", ppc64_pft_size);
2085
2086 if (htab_hash_mask)
2087 pr_info("htab_hash_mask = 0x%lx\n", htab_hash_mask);
Christophe Leroye4dccf92019-04-26 16:36:39 +00002088}