blob: 6c67bd0bec55743ba99c67381934fde6960c8da1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * PowerPC64 port by Mike Corrigan and Dave Engebretsen
3 * {mikejc|engebret}@us.ibm.com
4 *
5 * Copyright (c) 2000 Mike Corrigan <mikejc@us.ibm.com>
6 *
7 * SMP scalability work:
8 * Copyright (C) 2001 Anton Blanchard <anton@au.ibm.com>, IBM
9 *
10 * Module name: htab.c
11 *
12 * Description:
13 * PowerPC Hashed Page Table functions
14 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version
18 * 2 of the License, or (at your option) any later version.
19 */
20
21#undef DEBUG
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +110022#undef DEBUG_LOW
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/spinlock.h>
25#include <linux/errno.h>
26#include <linux/sched.h>
27#include <linux/proc_fs.h>
28#include <linux/stat.h>
29#include <linux/sysctl.h>
Paul Gortmaker66b15db2011-05-27 10:46:24 -040030#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/ctype.h>
32#include <linux/cache.h>
33#include <linux/init.h>
34#include <linux/signal.h>
Yinghai Lu95f72d12010-07-12 14:36:09 +100035#include <linux/memblock.h>
Li Zhongba12eed2013-05-13 16:16:41 +000036#include <linux/context_tracking.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <asm/processor.h>
39#include <asm/pgtable.h>
40#include <asm/mmu.h>
41#include <asm/mmu_context.h>
42#include <asm/page.h>
43#include <asm/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <asm/uaccess.h>
45#include <asm/machdep.h>
David S. Millerd9b2b2a2008-02-13 16:56:49 -080046#include <asm/prom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <asm/tlbflush.h>
48#include <asm/io.h>
49#include <asm/eeh.h>
50#include <asm/tlb.h>
51#include <asm/cacheflush.h>
52#include <asm/cputable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <asm/sections.h>
Ian Munsiebe3ebfe2014-10-08 19:54:52 +110054#include <asm/copro.h>
will schmidtaa39be02007-10-30 06:24:19 +110055#include <asm/udbg.h>
Anton Blanchardb68a70c2011-04-04 23:56:18 +000056#include <asm/code-patching.h>
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +000057#include <asm/fadump.h>
Stephen Rothwellf5339272012-03-15 18:18:00 +000058#include <asm/firmware.h>
Michael Neulingbc2a9402013-02-13 16:21:40 +000059#include <asm/tm.h>
Aneesh Kumar K.Vcfcb3d82015-04-14 13:05:57 +053060#include <asm/trace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62#ifdef DEBUG
63#define DBG(fmt...) udbg_printf(fmt)
64#else
65#define DBG(fmt...)
66#endif
67
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +110068#ifdef DEBUG_LOW
69#define DBG_LOW(fmt...) udbg_printf(fmt)
70#else
71#define DBG_LOW(fmt...)
72#endif
73
74#define KB (1024)
75#define MB (1024*KB)
Jon Tollefson658013e2008-07-23 21:27:54 -070076#define GB (1024L*MB)
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +110077
Linus Torvalds1da177e2005-04-16 15:20:36 -070078/*
79 * Note: pte --> Linux PTE
80 * HPTE --> PowerPC Hashed Page Table Entry
81 *
82 * Execution context:
83 * htab_initialize is called with the MMU off (of course), but
84 * the kernel has been copied down to zero so it can directly
85 * reference global data. At this point it is very difficult
86 * to print debug info.
87 *
88 */
89
90#ifdef CONFIG_U3_DART
91extern unsigned long dart_tablebase;
92#endif /* CONFIG_U3_DART */
93
Paul Mackerras799d6042005-11-10 13:37:51 +110094static unsigned long _SDR1;
95struct mmu_psize_def mmu_psize_defs[MMU_PAGE_COUNT];
Anton Blancharde1802b02014-08-20 08:00:02 +100096EXPORT_SYMBOL_GPL(mmu_psize_defs);
Paul Mackerras799d6042005-11-10 13:37:51 +110097
David Gibson8e561e72007-06-13 14:52:56 +100098struct hash_pte *htab_address;
Michael Ellerman337a7122006-02-21 17:22:55 +110099unsigned long htab_size_bytes;
David Gibson96e28442005-07-13 01:11:42 -0700100unsigned long htab_hash_mask;
Alexander Graf4ab79aa2009-10-30 05:47:19 +0000101EXPORT_SYMBOL_GPL(htab_hash_mask);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100102int mmu_linear_psize = MMU_PAGE_4K;
Ian Munsie8ca7a822014-10-08 19:54:54 +1100103EXPORT_SYMBOL_GPL(mmu_linear_psize);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100104int mmu_virtual_psize = MMU_PAGE_4K;
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000105int mmu_vmalloc_psize = MMU_PAGE_4K;
Benjamin Herrenschmidtcec08e72008-04-30 15:41:48 +1000106#ifdef CONFIG_SPARSEMEM_VMEMMAP
107int mmu_vmemmap_psize = MMU_PAGE_4K;
108#endif
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000109int mmu_io_psize = MMU_PAGE_4K;
Paul Mackerras1189be62007-10-11 20:37:10 +1000110int mmu_kernel_ssize = MMU_SEGSIZE_256M;
Ian Munsie8ca7a822014-10-08 19:54:54 +1100111EXPORT_SYMBOL_GPL(mmu_kernel_ssize);
Paul Mackerras1189be62007-10-11 20:37:10 +1000112int mmu_highuser_ssize = MMU_SEGSIZE_256M;
Michael Neuling584f8b72007-12-06 17:24:48 +1100113u16 mmu_slb_size = 64;
Alexander Graf4ab79aa2009-10-30 05:47:19 +0000114EXPORT_SYMBOL_GPL(mmu_slb_size);
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000115#ifdef CONFIG_PPC_64K_PAGES
116int mmu_ci_restrictions;
117#endif
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000118#ifdef CONFIG_DEBUG_PAGEALLOC
119static u8 *linear_map_hash_slots;
120static unsigned long linear_map_hash_count;
Michael Ellermaned166692007-04-18 11:50:09 +1000121static DEFINE_SPINLOCK(linear_map_hash_lock);
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000122#endif /* CONFIG_DEBUG_PAGEALLOC */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100124/* There are definitions of page sizes arrays to be used when none
125 * is provided by the firmware.
126 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100128/* Pre-POWER4 CPUs (4k pages only)
129 */
Michael Ellerman09de9ff2008-05-08 14:27:07 +1000130static struct mmu_psize_def mmu_psize_defaults_old[] = {
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100131 [MMU_PAGE_4K] = {
132 .shift = 12,
133 .sllp = 0,
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000134 .penc = {[MMU_PAGE_4K] = 0, [1 ... MMU_PAGE_COUNT - 1] = -1},
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100135 .avpnm = 0,
136 .tlbiel = 0,
137 },
138};
139
140/* POWER4, GPUL, POWER5
141 *
142 * Support for 16Mb large pages
143 */
Michael Ellerman09de9ff2008-05-08 14:27:07 +1000144static struct mmu_psize_def mmu_psize_defaults_gp[] = {
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100145 [MMU_PAGE_4K] = {
146 .shift = 12,
147 .sllp = 0,
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000148 .penc = {[MMU_PAGE_4K] = 0, [1 ... MMU_PAGE_COUNT - 1] = -1},
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100149 .avpnm = 0,
150 .tlbiel = 1,
151 },
152 [MMU_PAGE_16M] = {
153 .shift = 24,
154 .sllp = SLB_VSID_L,
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000155 .penc = {[0 ... MMU_PAGE_16M - 1] = -1, [MMU_PAGE_16M] = 0,
156 [MMU_PAGE_16M + 1 ... MMU_PAGE_COUNT - 1] = -1 },
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100157 .avpnm = 0x1UL,
158 .tlbiel = 0,
159 },
160};
161
Aneesh Kumar K.Vc6a3c492015-12-01 09:06:50 +0530162unsigned long htab_convert_pte_flags(unsigned long pteflags)
Benjamin Herrenschmidtbc033b62008-08-05 16:19:56 +1000163{
Aneesh Kumar K.Vc6a3c492015-12-01 09:06:50 +0530164 unsigned long rflags = 0;
Benjamin Herrenschmidtbc033b62008-08-05 16:19:56 +1000165
166 /* _PAGE_EXEC -> NOEXEC */
167 if ((pteflags & _PAGE_EXEC) == 0)
168 rflags |= HPTE_R_N;
Aneesh Kumar K.Vc6a3c492015-12-01 09:06:50 +0530169 /*
170 * PP bits:
171 * Linux use slb key 0 for kernel and 1 for user.
172 * kernel areas are mapped by PP bits 00
173 * and and there is no kernel RO (_PAGE_KERNEL_RO).
174 * User area mapped by 0x2 and read only use by
175 * 0x3.
Benjamin Herrenschmidtbc033b62008-08-05 16:19:56 +1000176 */
Aneesh Kumar K.Vc6a3c492015-12-01 09:06:50 +0530177 if (pteflags & _PAGE_USER) {
178 rflags |= 0x2;
179 if (!((pteflags & _PAGE_RW) && (pteflags & _PAGE_DIRTY)))
180 rflags |= 0x1;
181 }
Aneesh Kumar K.Vc8c06f52013-11-18 14:58:10 +0530182 /*
183 * Always add "C" bit for perf. Memory coherence is always enabled
184 */
185 return rflags | HPTE_R_C | HPTE_R_M;
Benjamin Herrenschmidtbc033b62008-08-05 16:19:56 +1000186}
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100187
188int htab_bolt_mapping(unsigned long vstart, unsigned long vend,
Benjamin Herrenschmidtbc033b62008-08-05 16:19:56 +1000189 unsigned long pstart, unsigned long prot,
Paul Mackerras1189be62007-10-11 20:37:10 +1000190 int psize, int ssize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100192 unsigned long vaddr, paddr;
193 unsigned int step, shift;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100194 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100196 shift = mmu_psize_defs[psize].shift;
197 step = 1 << shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Benjamin Herrenschmidtbc033b62008-08-05 16:19:56 +1000199 prot = htab_convert_pte_flags(prot);
200
201 DBG("htab_bolt_mapping(%lx..%lx -> %lx (%lx,%d,%d)\n",
202 vstart, vend, pstart, prot, psize, ssize);
203
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100204 for (vaddr = vstart, paddr = pstart; vaddr < vend;
205 vaddr += step, paddr += step) {
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000206 unsigned long hash, hpteg;
Paul Mackerras1189be62007-10-11 20:37:10 +1000207 unsigned long vsid = get_kernel_vsid(vaddr, ssize);
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000208 unsigned long vpn = hpt_vpn(vaddr, vsid, ssize);
Paul Mackerras9e88ba42008-08-30 11:26:27 +1000209 unsigned long tprot = prot;
210
Aneesh Kumar K.Vc60ac562013-03-13 03:34:54 +0000211 /*
212 * If we hit a bad address return error.
213 */
214 if (!vsid)
215 return -1;
Paul Mackerras9e88ba42008-08-30 11:26:27 +1000216 /* Make kernel text executable */
Paul Mackerras549e8152008-08-30 11:43:47 +1000217 if (overlaps_kernel_text(vaddr, vaddr + step))
Paul Mackerras9e88ba42008-08-30 11:26:27 +1000218 tprot &= ~HPTE_R_N;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Alexander Grafb18db0b2014-04-29 12:17:26 +0200220 /* Make kvm guest trampolines executable */
221 if (overlaps_kvm_tmp(vaddr, vaddr + step))
222 tprot &= ~HPTE_R_N;
223
Mahesh Salgaonkar429d2e82014-01-31 00:31:04 +0530224 /*
225 * If relocatable, check if it overlaps interrupt vectors that
226 * are copied down to real 0. For relocatable kernel
227 * (e.g. kdump case) we copy interrupt vectors down to real
228 * address 0. Mark that region as executable. This is
229 * because on p8 system with relocation on exception feature
230 * enabled, exceptions are raised with MMU (IR=DR=1) ON. Hence
231 * in order to execute the interrupt handlers in virtual
232 * mode the vector region need to be marked as executable.
233 */
234 if ((PHYSICAL_START > MEMORY_START) &&
235 overlaps_interrupt_vector_text(vaddr, vaddr + step))
236 tprot &= ~HPTE_R_N;
237
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000238 hash = hpt_hash(vpn, shift, ssize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
240
Michael Ellermanc30a4df2006-06-23 18:16:39 +1000241 BUG_ON(!ppc_md.hpte_insert);
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000242 ret = ppc_md.hpte_insert(hpteg, vpn, paddr, tprot,
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000243 HPTE_V_BOLTED, psize, psize, ssize);
Michael Ellermanc30a4df2006-06-23 18:16:39 +1000244
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100245 if (ret < 0)
246 break;
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000247#ifdef CONFIG_DEBUG_PAGEALLOC
248 if ((paddr >> PAGE_SHIFT) < linear_map_hash_count)
249 linear_map_hash_slots[paddr >> PAGE_SHIFT] = ret | 0x80;
250#endif /* CONFIG_DEBUG_PAGEALLOC */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 }
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100252 return ret < 0 ? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
254
Stephen Rothwellae86f002008-03-27 16:08:57 +1100255#ifdef CONFIG_MEMORY_HOTPLUG
Li Zhonged5694a2014-06-11 16:23:37 +0800256int htab_remove_mapping(unsigned long vstart, unsigned long vend,
Badari Pulavartyf8c88032008-01-29 09:19:24 +1100257 int psize, int ssize)
258{
259 unsigned long vaddr;
260 unsigned int step, shift;
261
262 shift = mmu_psize_defs[psize].shift;
263 step = 1 << shift;
264
265 if (!ppc_md.hpte_removebolted) {
Badari Pulavarty52db9b42008-03-28 11:37:21 +1100266 printk(KERN_WARNING "Platform doesn't implement "
267 "hpte_removebolted\n");
268 return -EINVAL;
Badari Pulavartyf8c88032008-01-29 09:19:24 +1100269 }
270
271 for (vaddr = vstart; vaddr < vend; vaddr += step)
272 ppc_md.hpte_removebolted(vaddr, psize, ssize);
Badari Pulavarty52db9b42008-03-28 11:37:21 +1100273
274 return 0;
Badari Pulavartyf8c88032008-01-29 09:19:24 +1100275}
Stephen Rothwellae86f002008-03-27 16:08:57 +1100276#endif /* CONFIG_MEMORY_HOTPLUG */
Badari Pulavartyf8c88032008-01-29 09:19:24 +1100277
Paul Mackerras1189be62007-10-11 20:37:10 +1000278static int __init htab_dt_scan_seg_sizes(unsigned long node,
279 const char *uname, int depth,
280 void *data)
281{
Rob Herring9d0c4df2014-04-01 23:49:03 -0500282 const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
283 const __be32 *prop;
284 int size = 0;
Paul Mackerras1189be62007-10-11 20:37:10 +1000285
286 /* We are scanning "cpu" nodes only */
287 if (type == NULL || strcmp(type, "cpu") != 0)
288 return 0;
289
Anton Blanchard12f04f22013-09-23 12:04:36 +1000290 prop = of_get_flat_dt_prop(node, "ibm,processor-segment-sizes", &size);
Paul Mackerras1189be62007-10-11 20:37:10 +1000291 if (prop == NULL)
292 return 0;
293 for (; size >= 4; size -= 4, ++prop) {
Anton Blanchard12f04f22013-09-23 12:04:36 +1000294 if (be32_to_cpu(prop[0]) == 40) {
Paul Mackerras1189be62007-10-11 20:37:10 +1000295 DBG("1T segment support detected\n");
Matt Evans44ae3ab2011-04-06 19:48:50 +0000296 cur_cpu_spec->mmu_features |= MMU_FTR_1T_SEGMENT;
Olof Johanssonf5534002007-10-12 16:44:55 +1000297 return 1;
Paul Mackerras1189be62007-10-11 20:37:10 +1000298 }
Paul Mackerras1189be62007-10-11 20:37:10 +1000299 }
Matt Evans44ae3ab2011-04-06 19:48:50 +0000300 cur_cpu_spec->mmu_features &= ~MMU_FTR_NO_SLBIE_B;
Paul Mackerras1189be62007-10-11 20:37:10 +1000301 return 0;
302}
303
304static void __init htab_init_seg_sizes(void)
305{
306 of_scan_flat_dt(htab_dt_scan_seg_sizes, NULL);
307}
308
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000309static int __init get_idx_from_shift(unsigned int shift)
310{
311 int idx = -1;
312
313 switch (shift) {
314 case 0xc:
315 idx = MMU_PAGE_4K;
316 break;
317 case 0x10:
318 idx = MMU_PAGE_64K;
319 break;
320 case 0x14:
321 idx = MMU_PAGE_1M;
322 break;
323 case 0x18:
324 idx = MMU_PAGE_16M;
325 break;
326 case 0x22:
327 idx = MMU_PAGE_16G;
328 break;
329 }
330 return idx;
331}
332
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100333static int __init htab_dt_scan_page_sizes(unsigned long node,
334 const char *uname, int depth,
335 void *data)
336{
Rob Herring9d0c4df2014-04-01 23:49:03 -0500337 const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
338 const __be32 *prop;
339 int size = 0;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100340
341 /* We are scanning "cpu" nodes only */
342 if (type == NULL || strcmp(type, "cpu") != 0)
343 return 0;
344
Anton Blanchard12f04f22013-09-23 12:04:36 +1000345 prop = of_get_flat_dt_prop(node, "ibm,segment-page-sizes", &size);
Michael Ellerman9e349922014-08-07 17:26:33 +1000346 if (!prop)
347 return 0;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100348
Michael Ellerman9e349922014-08-07 17:26:33 +1000349 pr_info("Page sizes from device-tree:\n");
350 size /= 4;
351 cur_cpu_spec->mmu_features &= ~(MMU_FTR_16M_PAGE);
352 while(size > 0) {
353 unsigned int base_shift = be32_to_cpu(prop[0]);
354 unsigned int slbenc = be32_to_cpu(prop[1]);
355 unsigned int lpnum = be32_to_cpu(prop[2]);
356 struct mmu_psize_def *def;
357 int idx, base_idx;
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000358
Michael Ellerman9e349922014-08-07 17:26:33 +1000359 size -= 3; prop += 3;
360 base_idx = get_idx_from_shift(base_shift);
361 if (base_idx < 0) {
362 /* skip the pte encoding also */
363 prop += lpnum * 2; size -= lpnum * 2;
364 continue;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100365 }
Michael Ellerman9e349922014-08-07 17:26:33 +1000366 def = &mmu_psize_defs[base_idx];
367 if (base_idx == MMU_PAGE_16M)
368 cur_cpu_spec->mmu_features |= MMU_FTR_16M_PAGE;
369
370 def->shift = base_shift;
371 if (base_shift <= 23)
372 def->avpnm = 0;
373 else
374 def->avpnm = (1 << (base_shift - 23)) - 1;
375 def->sllp = slbenc;
376 /*
377 * We don't know for sure what's up with tlbiel, so
378 * for now we only set it for 4K and 64K pages
379 */
380 if (base_idx == MMU_PAGE_4K || base_idx == MMU_PAGE_64K)
381 def->tlbiel = 1;
382 else
383 def->tlbiel = 0;
384
385 while (size > 0 && lpnum) {
386 unsigned int shift = be32_to_cpu(prop[0]);
387 int penc = be32_to_cpu(prop[1]);
388
389 prop += 2; size -= 2;
390 lpnum--;
391
392 idx = get_idx_from_shift(shift);
393 if (idx < 0)
394 continue;
395
396 if (penc == -1)
397 pr_err("Invalid penc for base_shift=%d "
398 "shift=%d\n", base_shift, shift);
399
400 def->penc[idx] = penc;
401 pr_info("base_shift=%d: shift=%d, sllp=0x%04lx,"
402 " avpnm=0x%08lx, tlbiel=%d, penc=%d\n",
403 base_shift, shift, def->sllp,
404 def->avpnm, def->tlbiel, def->penc[idx]);
405 }
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100406 }
Michael Ellerman9e349922014-08-07 17:26:33 +1000407
408 return 1;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100409}
410
Tony Breedse16a9c02008-07-31 13:51:42 +1000411#ifdef CONFIG_HUGETLB_PAGE
Jon Tollefson658013e2008-07-23 21:27:54 -0700412/* Scan for 16G memory blocks that have been set aside for huge pages
413 * and reserve those blocks for 16G huge pages.
414 */
415static int __init htab_dt_scan_hugepage_blocks(unsigned long node,
416 const char *uname, int depth,
417 void *data) {
Rob Herring9d0c4df2014-04-01 23:49:03 -0500418 const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
419 const __be64 *addr_prop;
420 const __be32 *page_count_prop;
Jon Tollefson658013e2008-07-23 21:27:54 -0700421 unsigned int expected_pages;
422 long unsigned int phys_addr;
423 long unsigned int block_size;
424
425 /* We are scanning "memory" nodes only */
426 if (type == NULL || strcmp(type, "memory") != 0)
427 return 0;
428
429 /* This property is the log base 2 of the number of virtual pages that
430 * will represent this memory block. */
431 page_count_prop = of_get_flat_dt_prop(node, "ibm,expected#pages", NULL);
432 if (page_count_prop == NULL)
433 return 0;
Anton Blanchard12f04f22013-09-23 12:04:36 +1000434 expected_pages = (1 << be32_to_cpu(page_count_prop[0]));
Jon Tollefson658013e2008-07-23 21:27:54 -0700435 addr_prop = of_get_flat_dt_prop(node, "reg", NULL);
436 if (addr_prop == NULL)
437 return 0;
Anton Blanchard12f04f22013-09-23 12:04:36 +1000438 phys_addr = be64_to_cpu(addr_prop[0]);
439 block_size = be64_to_cpu(addr_prop[1]);
Jon Tollefson658013e2008-07-23 21:27:54 -0700440 if (block_size != (16 * GB))
441 return 0;
442 printk(KERN_INFO "Huge page(16GB) memory: "
443 "addr = 0x%lX size = 0x%lX pages = %d\n",
444 phys_addr, block_size, expected_pages);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000445 if (phys_addr + (16 * GB) <= memblock_end_of_DRAM()) {
446 memblock_reserve(phys_addr, block_size * expected_pages);
Jon Tollefson4792adb2008-10-21 15:27:36 +0000447 add_gpage(phys_addr, block_size, expected_pages);
448 }
Jon Tollefson658013e2008-07-23 21:27:54 -0700449 return 0;
450}
Tony Breedse16a9c02008-07-31 13:51:42 +1000451#endif /* CONFIG_HUGETLB_PAGE */
Jon Tollefson658013e2008-07-23 21:27:54 -0700452
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000453static void mmu_psize_set_default_penc(void)
454{
455 int bpsize, apsize;
456 for (bpsize = 0; bpsize < MMU_PAGE_COUNT; bpsize++)
457 for (apsize = 0; apsize < MMU_PAGE_COUNT; apsize++)
458 mmu_psize_defs[bpsize].penc[apsize] = -1;
459}
460
Alexander Graf9048e642014-04-01 15:46:05 +0200461#ifdef CONFIG_PPC_64K_PAGES
462
463static bool might_have_hea(void)
464{
465 /*
466 * The HEA ethernet adapter requires awareness of the
467 * GX bus. Without that awareness we can easily assume
468 * we will never see an HEA ethernet device.
469 */
470#ifdef CONFIG_IBMEBUS
471 return !cpu_has_feature(CPU_FTR_ARCH_207S);
472#else
473 return false;
474#endif
475}
476
477#endif /* #ifdef CONFIG_PPC_64K_PAGES */
478
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100479static void __init htab_init_page_sizes(void)
480{
481 int rc;
482
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000483 /* se the invalid penc to -1 */
484 mmu_psize_set_default_penc();
485
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100486 /* Default to 4K pages only */
487 memcpy(mmu_psize_defs, mmu_psize_defaults_old,
488 sizeof(mmu_psize_defaults_old));
489
490 /*
491 * Try to find the available page sizes in the device-tree
492 */
493 rc = of_scan_flat_dt(htab_dt_scan_page_sizes, NULL);
494 if (rc != 0) /* Found */
495 goto found;
496
497 /*
498 * Not in the device-tree, let's fallback on known size
499 * list for 16M capable GP & GR
500 */
Matt Evans44ae3ab2011-04-06 19:48:50 +0000501 if (mmu_has_feature(MMU_FTR_16M_PAGE))
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100502 memcpy(mmu_psize_defs, mmu_psize_defaults_gp,
503 sizeof(mmu_psize_defaults_gp));
504 found:
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000505#ifndef CONFIG_DEBUG_PAGEALLOC
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100506 /*
507 * Pick a size for the linear mapping. Currently, we only support
508 * 16M, 1M and 4K which is the default
509 */
510 if (mmu_psize_defs[MMU_PAGE_16M].shift)
511 mmu_linear_psize = MMU_PAGE_16M;
512 else if (mmu_psize_defs[MMU_PAGE_1M].shift)
513 mmu_linear_psize = MMU_PAGE_1M;
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000514#endif /* CONFIG_DEBUG_PAGEALLOC */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100515
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000516#ifdef CONFIG_PPC_64K_PAGES
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100517 /*
518 * Pick a size for the ordinary pages. Default is 4K, we support
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000519 * 64K for user mappings and vmalloc if supported by the processor.
520 * We only use 64k for ioremap if the processor
521 * (and firmware) support cache-inhibited large pages.
522 * If not, we use 4k and set mmu_ci_restrictions so that
523 * hash_page knows to switch processes that use cache-inhibited
524 * mappings to 4k pages.
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100525 */
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000526 if (mmu_psize_defs[MMU_PAGE_64K].shift) {
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100527 mmu_virtual_psize = MMU_PAGE_64K;
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000528 mmu_vmalloc_psize = MMU_PAGE_64K;
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000529 if (mmu_linear_psize == MMU_PAGE_4K)
530 mmu_linear_psize = MMU_PAGE_64K;
Matt Evans44ae3ab2011-04-06 19:48:50 +0000531 if (mmu_has_feature(MMU_FTR_CI_LARGE_PAGE)) {
Paul Mackerrascfe666b2008-03-24 17:41:22 +1100532 /*
Alexander Graf9048e642014-04-01 15:46:05 +0200533 * When running on pSeries using 64k pages for ioremap
534 * would stop us accessing the HEA ethernet. So if we
535 * have the chance of ever seeing one, stay at 4k.
Paul Mackerrascfe666b2008-03-24 17:41:22 +1100536 */
Alexander Graf9048e642014-04-01 15:46:05 +0200537 if (!might_have_hea() || !machine_is(pseries))
Paul Mackerrascfe666b2008-03-24 17:41:22 +1100538 mmu_io_psize = MMU_PAGE_64K;
539 } else
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000540 mmu_ci_restrictions = 1;
541 }
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000542#endif /* CONFIG_PPC_64K_PAGES */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100543
Benjamin Herrenschmidtcec08e72008-04-30 15:41:48 +1000544#ifdef CONFIG_SPARSEMEM_VMEMMAP
545 /* We try to use 16M pages for vmemmap if that is supported
546 * and we have at least 1G of RAM at boot
547 */
548 if (mmu_psize_defs[MMU_PAGE_16M].shift &&
Yinghai Lu95f72d12010-07-12 14:36:09 +1000549 memblock_phys_mem_size() >= 0x40000000)
Benjamin Herrenschmidtcec08e72008-04-30 15:41:48 +1000550 mmu_vmemmap_psize = MMU_PAGE_16M;
551 else if (mmu_psize_defs[MMU_PAGE_64K].shift)
552 mmu_vmemmap_psize = MMU_PAGE_64K;
553 else
554 mmu_vmemmap_psize = MMU_PAGE_4K;
555#endif /* CONFIG_SPARSEMEM_VMEMMAP */
556
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000557 printk(KERN_DEBUG "Page orders: linear mapping = %d, "
Benjamin Herrenschmidtcec08e72008-04-30 15:41:48 +1000558 "virtual = %d, io = %d"
559#ifdef CONFIG_SPARSEMEM_VMEMMAP
560 ", vmemmap = %d"
561#endif
562 "\n",
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100563 mmu_psize_defs[mmu_linear_psize].shift,
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000564 mmu_psize_defs[mmu_virtual_psize].shift,
Benjamin Herrenschmidtcec08e72008-04-30 15:41:48 +1000565 mmu_psize_defs[mmu_io_psize].shift
566#ifdef CONFIG_SPARSEMEM_VMEMMAP
567 ,mmu_psize_defs[mmu_vmemmap_psize].shift
568#endif
569 );
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100570
571#ifdef CONFIG_HUGETLB_PAGE
Jon Tollefson658013e2008-07-23 21:27:54 -0700572 /* Reserve 16G huge page memory sections for huge pages */
573 of_scan_flat_dt(htab_dt_scan_hugepage_blocks, NULL);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100574#endif /* CONFIG_HUGETLB_PAGE */
575}
576
577static int __init htab_dt_scan_pftsize(unsigned long node,
578 const char *uname, int depth,
579 void *data)
580{
Rob Herring9d0c4df2014-04-01 23:49:03 -0500581 const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
582 const __be32 *prop;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100583
584 /* We are scanning "cpu" nodes only */
585 if (type == NULL || strcmp(type, "cpu") != 0)
586 return 0;
587
Anton Blanchard12f04f22013-09-23 12:04:36 +1000588 prop = of_get_flat_dt_prop(node, "ibm,pft-size", NULL);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100589 if (prop != NULL) {
590 /* pft_size[0] is the NUMA CEC cookie */
Anton Blanchard12f04f22013-09-23 12:04:36 +1000591 ppc64_pft_size = be32_to_cpu(prop[1]);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100592 return 1;
593 }
594 return 0;
595}
596
597static unsigned long __init htab_get_table_size(void)
Paul Mackerras3eac8c62005-10-12 16:58:53 +1000598{
Anton Blanchard13870b62009-02-13 11:57:30 +0000599 unsigned long mem_size, rnd_mem_size, pteg_count, psize;
Paul Mackerras3eac8c62005-10-12 16:58:53 +1000600
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100601 /* If hash size isn't already provided by the platform, we try to
Adrian Bunk943ffb52006-01-10 00:10:13 +0100602 * retrieve it from the device-tree. If it's not there neither, we
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100603 * calculate it now based on the total RAM size
Paul Mackerras3eac8c62005-10-12 16:58:53 +1000604 */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100605 if (ppc64_pft_size == 0)
606 of_scan_flat_dt(htab_dt_scan_pftsize, NULL);
Paul Mackerras3eac8c62005-10-12 16:58:53 +1000607 if (ppc64_pft_size)
608 return 1UL << ppc64_pft_size;
609
610 /* round mem_size up to next power of 2 */
Yinghai Lu95f72d12010-07-12 14:36:09 +1000611 mem_size = memblock_phys_mem_size();
Paul Mackerras799d6042005-11-10 13:37:51 +1100612 rnd_mem_size = 1UL << __ilog2(mem_size);
613 if (rnd_mem_size < mem_size)
Paul Mackerras3eac8c62005-10-12 16:58:53 +1000614 rnd_mem_size <<= 1;
615
616 /* # pages / 2 */
Anton Blanchard13870b62009-02-13 11:57:30 +0000617 psize = mmu_psize_defs[mmu_virtual_psize].shift;
618 pteg_count = max(rnd_mem_size >> (psize + 1), 1UL << 11);
Paul Mackerras3eac8c62005-10-12 16:58:53 +1000619
620 return pteg_count << 7;
621}
622
Mike Kravetz54b79242005-11-07 16:25:48 -0800623#ifdef CONFIG_MEMORY_HOTPLUG
Anton Blancharda1194092011-08-10 20:44:24 +0000624int create_section_mapping(unsigned long start, unsigned long end)
Mike Kravetz54b79242005-11-07 16:25:48 -0800625{
Anton Blancharda1194092011-08-10 20:44:24 +0000626 return htab_bolt_mapping(start, end, __pa(start),
David Gibsonf5ea64d2008-10-12 17:54:24 +0000627 pgprot_val(PAGE_KERNEL), mmu_linear_psize,
Anton Blancharda1194092011-08-10 20:44:24 +0000628 mmu_kernel_ssize);
Mike Kravetz54b79242005-11-07 16:25:48 -0800629}
Badari Pulavartyf8c88032008-01-29 09:19:24 +1100630
Badari Pulavarty52db9b42008-03-28 11:37:21 +1100631int remove_section_mapping(unsigned long start, unsigned long end)
Badari Pulavartyf8c88032008-01-29 09:19:24 +1100632{
Badari Pulavarty52db9b42008-03-28 11:37:21 +1100633 return htab_remove_mapping(start, end, mmu_linear_psize,
634 mmu_kernel_ssize);
Badari Pulavartyf8c88032008-01-29 09:19:24 +1100635}
Mike Kravetz54b79242005-11-07 16:25:48 -0800636#endif /* CONFIG_MEMORY_HOTPLUG */
637
Benjamin Herrenschmidt757c74d2009-03-19 19:34:16 +0000638static void __init htab_initialize(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
Michael Ellerman337a7122006-02-21 17:22:55 +1100640 unsigned long table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 unsigned long pteg_count;
Paul Mackerras9e88ba42008-08-30 11:26:27 +1000642 unsigned long prot;
Michael Ellerman41d824b2008-01-30 01:13:59 +1100643 unsigned long base = 0, size = 0, limit;
Benjamin Herrenschmidt28be7072010-08-04 13:43:53 +1000644 struct memblock_region *reg;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 DBG(" -> htab_initialize()\n");
647
Paul Mackerras1189be62007-10-11 20:37:10 +1000648 /* Initialize segment sizes */
649 htab_init_seg_sizes();
650
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100651 /* Initialize page sizes */
652 htab_init_page_sizes();
653
Matt Evans44ae3ab2011-04-06 19:48:50 +0000654 if (mmu_has_feature(MMU_FTR_1T_SEGMENT)) {
Paul Mackerras1189be62007-10-11 20:37:10 +1000655 mmu_kernel_ssize = MMU_SEGSIZE_1T;
656 mmu_highuser_ssize = MMU_SEGSIZE_1T;
657 printk(KERN_INFO "Using 1TB segments\n");
658 }
659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 /*
661 * Calculate the required size of the htab. We want the number of
662 * PTEGs to equal one half the number of real pages.
663 */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100664 htab_size_bytes = htab_get_table_size();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 pteg_count = htab_size_bytes >> 7;
666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 htab_hash_mask = pteg_count - 1;
668
Michael Ellerman57cfb812006-03-21 20:45:59 +1100669 if (firmware_has_feature(FW_FEATURE_LPAR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 /* Using a hypervisor which owns the htab */
671 htab_address = NULL;
672 _SDR1 = 0;
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +0000673#ifdef CONFIG_FA_DUMP
674 /*
675 * If firmware assisted dump is active firmware preserves
676 * the contents of htab along with entire partition memory.
677 * Clear the htab if firmware assisted dump is active so
678 * that we dont end up using old mappings.
679 */
680 if (is_fadump_active() && ppc_md.hpte_clear_all)
681 ppc_md.hpte_clear_all();
682#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 } else {
684 /* Find storage for the HPT. Must be contiguous in
Michael Ellerman41d824b2008-01-30 01:13:59 +1100685 * the absolute address space. On cell we want it to be
Michael Ellerman31bf1112008-03-12 18:03:24 +1100686 * in the first 2 Gig so we can use it for IOMMU hacks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 */
Michael Ellerman41d824b2008-01-30 01:13:59 +1100688 if (machine_is(cell))
Michael Ellerman31bf1112008-03-12 18:03:24 +1100689 limit = 0x80000000;
Michael Ellerman41d824b2008-01-30 01:13:59 +1100690 else
Benjamin Herrenschmidt27f574c2010-07-06 15:39:00 -0700691 limit = MEMBLOCK_ALLOC_ANYWHERE;
Michael Ellerman41d824b2008-01-30 01:13:59 +1100692
Yinghai Lu95f72d12010-07-12 14:36:09 +1000693 table = memblock_alloc_base(htab_size_bytes, htab_size_bytes, limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 DBG("Hash table allocated at %lx, size: %lx\n", table,
696 htab_size_bytes);
697
Michael Ellerman70267a72012-07-25 21:19:50 +0000698 htab_address = __va(table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
700 /* htab absolute addr + encoded htabsize */
701 _SDR1 = table + __ilog2(pteg_count) - 11;
702
703 /* Initialize the HPT with no entries */
704 memset((void *)table, 0, htab_size_bytes);
Paul Mackerras799d6042005-11-10 13:37:51 +1100705
706 /* Set SDR1 */
707 mtspr(SPRN_SDR1, _SDR1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 }
709
David Gibsonf5ea64d2008-10-12 17:54:24 +0000710 prot = pgprot_val(PAGE_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000712#ifdef CONFIG_DEBUG_PAGEALLOC
Yinghai Lu95f72d12010-07-12 14:36:09 +1000713 linear_map_hash_count = memblock_end_of_DRAM() >> PAGE_SHIFT;
714 linear_map_hash_slots = __va(memblock_alloc_base(linear_map_hash_count,
Benjamin Herrenschmidtcd3db0c2010-07-06 15:39:02 -0700715 1, ppc64_rma_size));
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000716 memset(linear_map_hash_slots, 0, linear_map_hash_count);
717#endif /* CONFIG_DEBUG_PAGEALLOC */
718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 /* On U3 based machines, we need to reserve the DART area and
720 * _NOT_ map it to avoid cache paradoxes as it's remapped non
721 * cacheable later on
722 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
724 /* create bolted the linear mapping in the hash table */
Benjamin Herrenschmidt28be7072010-08-04 13:43:53 +1000725 for_each_memblock(memory, reg) {
726 base = (unsigned long)__va(reg->base);
727 size = reg->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Sachin P. Sant5c339912009-12-13 21:15:12 +0000729 DBG("creating mapping for region: %lx..%lx (prot: %lx)\n",
Paul Mackerras9e88ba42008-08-30 11:26:27 +1000730 base, size, prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
732#ifdef CONFIG_U3_DART
733 /* Do not map the DART space. Fortunately, it will be aligned
Yinghai Lu95f72d12010-07-12 14:36:09 +1000734 * in such a way that it will not cross two memblock regions and
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100735 * will fit within a single 16Mb page.
736 * The DART space is assumed to be a full 16Mb region even if
737 * we only use 2Mb of that space. We will use more of it later
738 * for AGP GART. We have to use a full 16Mb large page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 */
740 DBG("DART base: %lx\n", dart_tablebase);
741
742 if (dart_tablebase != 0 && dart_tablebase >= base
743 && dart_tablebase < (base + size)) {
Michael Ellermancaf80e52006-03-21 20:45:51 +1100744 unsigned long dart_table_end = dart_tablebase + 16 * MB;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 if (base != dart_tablebase)
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100746 BUG_ON(htab_bolt_mapping(base, dart_tablebase,
Paul Mackerras9e88ba42008-08-30 11:26:27 +1000747 __pa(base), prot,
Paul Mackerras1189be62007-10-11 20:37:10 +1000748 mmu_linear_psize,
749 mmu_kernel_ssize));
Michael Ellermancaf80e52006-03-21 20:45:51 +1100750 if ((base + size) > dart_table_end)
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100751 BUG_ON(htab_bolt_mapping(dart_tablebase+16*MB,
Michael Ellermancaf80e52006-03-21 20:45:51 +1100752 base + size,
753 __pa(dart_table_end),
Paul Mackerras9e88ba42008-08-30 11:26:27 +1000754 prot,
Paul Mackerras1189be62007-10-11 20:37:10 +1000755 mmu_linear_psize,
756 mmu_kernel_ssize));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 continue;
758 }
759#endif /* CONFIG_U3_DART */
Michael Ellermancaf80e52006-03-21 20:45:51 +1100760 BUG_ON(htab_bolt_mapping(base, base + size, __pa(base),
Paul Mackerras9e88ba42008-08-30 11:26:27 +1000761 prot, mmu_linear_psize, mmu_kernel_ssize));
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -0700762 }
763 memblock_set_current_limit(MEMBLOCK_ALLOC_ANYWHERE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
765 /*
766 * If we have a memory_limit and we've allocated TCEs then we need to
767 * explicitly map the TCE area at the top of RAM. We also cope with the
768 * case that the TCEs start below memory_limit.
769 * tce_alloc_start/end are 16MB aligned so the mapping should work
770 * for either 4K or 16MB pages.
771 */
772 if (tce_alloc_start) {
Michael Ellermanb5666f72005-12-05 10:24:33 -0600773 tce_alloc_start = (unsigned long)__va(tce_alloc_start);
774 tce_alloc_end = (unsigned long)__va(tce_alloc_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
776 if (base + size >= tce_alloc_start)
777 tce_alloc_start = base + size + 1;
778
Michael Ellermancaf80e52006-03-21 20:45:51 +1100779 BUG_ON(htab_bolt_mapping(tce_alloc_start, tce_alloc_end,
Benjamin Herrenschmidtbc033b62008-08-05 16:19:56 +1000780 __pa(tce_alloc_start), prot,
Paul Mackerras1189be62007-10-11 20:37:10 +1000781 mmu_linear_psize, mmu_kernel_ssize));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 }
783
Michael Ellerman7d0daae2006-06-23 18:16:38 +1000784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 DBG(" <- htab_initialize()\n");
786}
787#undef KB
788#undef MB
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Benjamin Herrenschmidt757c74d2009-03-19 19:34:16 +0000790void __init early_init_mmu(void)
Paul Mackerras799d6042005-11-10 13:37:51 +1100791{
Benjamin Herrenschmidt757c74d2009-03-19 19:34:16 +0000792 /* Initialize the MMU Hash table and create the linear mapping
Michael Ellerman376af592014-07-10 12:29:19 +1000793 * of memory. Has to be done before SLB initialization as this is
794 * currently where the page size encoding is obtained.
Benjamin Herrenschmidt757c74d2009-03-19 19:34:16 +0000795 */
796 htab_initialize();
797
Michael Ellerman376af592014-07-10 12:29:19 +1000798 /* Initialize SLB management */
Michael Ellerman13b3d132014-07-10 12:29:20 +1000799 slb_initialize();
Benjamin Herrenschmidt757c74d2009-03-19 19:34:16 +0000800}
801
802#ifdef CONFIG_SMP
Paul Gortmaker061d19f2013-06-24 15:30:09 -0400803void early_init_mmu_secondary(void)
Benjamin Herrenschmidt757c74d2009-03-19 19:34:16 +0000804{
805 /* Initialize hash table for that CPU */
Michael Ellerman57cfb812006-03-21 20:45:59 +1100806 if (!firmware_has_feature(FW_FEATURE_LPAR))
Paul Mackerras799d6042005-11-10 13:37:51 +1100807 mtspr(SPRN_SDR1, _SDR1);
Benjamin Herrenschmidt757c74d2009-03-19 19:34:16 +0000808
Michael Ellerman376af592014-07-10 12:29:19 +1000809 /* Initialize SLB */
Michael Ellerman13b3d132014-07-10 12:29:20 +1000810 slb_initialize();
Paul Mackerras799d6042005-11-10 13:37:51 +1100811}
Benjamin Herrenschmidt757c74d2009-03-19 19:34:16 +0000812#endif /* CONFIG_SMP */
Paul Mackerras799d6042005-11-10 13:37:51 +1100813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814/*
815 * Called by asm hashtable.S for doing lazy icache flush
816 */
817unsigned int hash_page_do_lazy_icache(unsigned int pp, pte_t pte, int trap)
818{
819 struct page *page;
820
Benjamin Herrenschmidt76c8e252005-11-08 11:21:05 +1100821 if (!pfn_valid(pte_pfn(pte)))
822 return pp;
823
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 page = pte_page(pte);
825
826 /* page is dirty */
827 if (!test_bit(PG_arch_1, &page->flags) && !PageReserved(page)) {
828 if (trap == 0x400) {
David Gibson0895ecd2009-10-26 19:24:31 +0000829 flush_dcache_icache_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 set_bit(PG_arch_1, &page->flags);
831 } else
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100832 pp |= HPTE_R_N;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 }
834 return pp;
835}
836
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000837#ifdef CONFIG_PPC_MM_SLICES
Anton Blancharde51df2c2014-08-20 08:55:18 +1000838static unsigned int get_paca_psize(unsigned long addr)
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000839{
Aneesh Kumar K.V7aa07272012-09-10 02:52:52 +0000840 u64 lpsizes;
841 unsigned char *hpsizes;
842 unsigned long index, mask_index;
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000843
844 if (addr < SLICE_LOW_TOP) {
Aneesh Kumar K.V7aa07272012-09-10 02:52:52 +0000845 lpsizes = get_paca()->context.low_slices_psize;
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000846 index = GET_LOW_SLICE_INDEX(addr);
Aneesh Kumar K.V7aa07272012-09-10 02:52:52 +0000847 return (lpsizes >> (index * 4)) & 0xF;
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000848 }
Aneesh Kumar K.V7aa07272012-09-10 02:52:52 +0000849 hpsizes = get_paca()->context.high_slices_psize;
850 index = GET_HIGH_SLICE_INDEX(addr);
851 mask_index = index & 0x1;
852 return (hpsizes[index >> 1] >> (mask_index * 4)) & 0xF;
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000853}
854
855#else
856unsigned int get_paca_psize(unsigned long addr)
857{
858 return get_paca()->context.user_psize;
859}
860#endif
861
Paul Mackerras721151d2007-04-03 21:24:02 +1000862/*
863 * Demote a segment to using 4k pages.
864 * For now this makes the whole process use 4k pages.
865 */
Paul Mackerras721151d2007-04-03 21:24:02 +1000866#ifdef CONFIG_PPC_64K_PAGES
Paul Mackerrasfa282372008-01-24 08:35:13 +1100867void demote_segment_4k(struct mm_struct *mm, unsigned long addr)
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +1000868{
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000869 if (get_slice_psize(mm, addr) == MMU_PAGE_4K)
Paul Mackerras721151d2007-04-03 21:24:02 +1000870 return;
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000871 slice_set_range_psize(mm, addr, 1, MMU_PAGE_4K);
Ian Munsiebe3ebfe2014-10-08 19:54:52 +1100872 copro_flush_all_slbs(mm);
Ian Munsiea1dca3462014-10-08 19:54:58 +1100873 if ((get_paca_psize(addr) != MMU_PAGE_4K) && (current->mm == mm)) {
Paul Mackerrasfa282372008-01-24 08:35:13 +1100874 get_paca()->context = mm->context;
875 slb_flush_and_rebolt();
876 }
Paul Mackerras721151d2007-04-03 21:24:02 +1000877}
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +1000878#endif /* CONFIG_PPC_64K_PAGES */
Paul Mackerras721151d2007-04-03 21:24:02 +1000879
Paul Mackerrasfa282372008-01-24 08:35:13 +1100880#ifdef CONFIG_PPC_SUBPAGE_PROT
881/*
882 * This looks up a 2-bit protection code for a 4k subpage of a 64k page.
883 * Userspace sets the subpage permissions using the subpage_prot system call.
884 *
885 * Result is 0: full permissions, _PAGE_RW: read-only,
886 * _PAGE_USER or _PAGE_USER|_PAGE_RW: no access.
887 */
David Gibsond28513b2009-11-26 18:56:04 +0000888static int subpage_protection(struct mm_struct *mm, unsigned long ea)
Paul Mackerrasfa282372008-01-24 08:35:13 +1100889{
David Gibsond28513b2009-11-26 18:56:04 +0000890 struct subpage_prot_table *spt = &mm->context.spt;
Paul Mackerrasfa282372008-01-24 08:35:13 +1100891 u32 spp = 0;
892 u32 **sbpm, *sbpp;
893
894 if (ea >= spt->maxaddr)
895 return 0;
Anton Blanchardb0d436c2013-08-07 02:01:24 +1000896 if (ea < 0x100000000UL) {
Paul Mackerrasfa282372008-01-24 08:35:13 +1100897 /* addresses below 4GB use spt->low_prot */
898 sbpm = spt->low_prot;
899 } else {
900 sbpm = spt->protptrs[ea >> SBP_L3_SHIFT];
901 if (!sbpm)
902 return 0;
903 }
904 sbpp = sbpm[(ea >> SBP_L2_SHIFT) & (SBP_L2_COUNT - 1)];
905 if (!sbpp)
906 return 0;
907 spp = sbpp[(ea >> PAGE_SHIFT) & (SBP_L1_COUNT - 1)];
908
909 /* extract 2-bit bitfield for this 4k subpage */
910 spp >>= 30 - 2 * ((ea >> 12) & 0xf);
911
912 /* turn 0,1,2,3 into combination of _PAGE_USER and _PAGE_RW */
913 spp = ((spp & 2) ? _PAGE_USER : 0) | ((spp & 1) ? _PAGE_RW : 0);
914 return spp;
915}
916
917#else /* CONFIG_PPC_SUBPAGE_PROT */
David Gibsond28513b2009-11-26 18:56:04 +0000918static inline int subpage_protection(struct mm_struct *mm, unsigned long ea)
Paul Mackerrasfa282372008-01-24 08:35:13 +1100919{
920 return 0;
921}
922#endif
923
Benjamin Herrenschmidt4b8692c2010-07-23 10:31:13 +1000924void hash_failure_debug(unsigned long ea, unsigned long access,
925 unsigned long vsid, unsigned long trap,
Aneesh Kumar K.Vd8139eb2013-04-28 09:37:37 +0000926 int ssize, int psize, int lpsize, unsigned long pte)
Benjamin Herrenschmidt4b8692c2010-07-23 10:31:13 +1000927{
928 if (!printk_ratelimit())
929 return;
930 pr_info("mm: Hashing failure ! EA=0x%lx access=0x%lx current=%s\n",
931 ea, access, current->comm);
Aneesh Kumar K.Vd8139eb2013-04-28 09:37:37 +0000932 pr_info(" trap=0x%lx vsid=0x%lx ssize=%d base psize=%d psize %d pte=0x%lx\n",
933 trap, vsid, ssize, psize, lpsize, pte);
Benjamin Herrenschmidt4b8692c2010-07-23 10:31:13 +1000934}
935
Michael Ellerman09567e72014-05-28 18:21:17 +1000936static void check_paca_psize(unsigned long ea, struct mm_struct *mm,
937 int psize, bool user_region)
938{
939 if (user_region) {
940 if (psize != get_paca_psize(ea)) {
941 get_paca()->context = mm->context;
942 slb_flush_and_rebolt();
943 }
944 } else if (get_paca()->vmalloc_sllp !=
945 mmu_psize_defs[mmu_vmalloc_psize].sllp) {
946 get_paca()->vmalloc_sllp =
947 mmu_psize_defs[mmu_vmalloc_psize].sllp;
948 slb_vmalloc_update();
949 }
950}
951
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952/* Result code is:
953 * 0 - handled
954 * 1 - normal page fault
955 * -1 - critical hash insertion error
Paul Mackerrasfa282372008-01-24 08:35:13 +1100956 * -2 - access not permitted by subpage protection mechanism
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 */
Aneesh Kumar K.Vaefa5682014-12-04 11:00:14 +0530958int hash_page_mm(struct mm_struct *mm, unsigned long ea,
959 unsigned long access, unsigned long trap,
960 unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961{
Aneesh Kumar K.V891121e2015-10-09 08:32:21 +0530962 bool is_thp;
Li Zhongba12eed2013-05-13 16:16:41 +0000963 enum ctx_state prev_state = exception_enter();
David Gibsona1128f82009-12-16 14:29:56 +0000964 pgd_t *pgdir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 unsigned long vsid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 pte_t *ptep;
David Gibsona4fe3ce2009-10-26 19:24:31 +0000967 unsigned hugeshift;
Rusty Russell56aa4122009-03-15 18:16:43 +0000968 const struct cpumask *tmp;
Aneesh Kumar K.Vaefa5682014-12-04 11:00:14 +0530969 int rc, user_region = 0;
Paul Mackerras1189be62007-10-11 20:37:10 +1000970 int psize, ssize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100972 DBG_LOW("hash_page(ea=%016lx, access=%lx, trap=%lx\n",
973 ea, access, trap);
Aneesh Kumar K.Vcfcb3d82015-04-14 13:05:57 +0530974 trace_hash_fault(ea, access, trap);
David Gibson1f8d4192005-05-05 16:15:13 -0700975
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100976 /* Get region & vsid */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 switch (REGION_ID(ea)) {
978 case USER_REGION_ID:
979 user_region = 1;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100980 if (! mm) {
981 DBG_LOW(" user region with no mm !\n");
Li Zhongba12eed2013-05-13 16:16:41 +0000982 rc = 1;
983 goto bail;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100984 }
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +1000985 psize = get_slice_psize(mm, ea);
Paul Mackerras1189be62007-10-11 20:37:10 +1000986 ssize = user_segment_size(ea);
987 vsid = get_vsid(mm->context.id, ea, ssize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 case VMALLOC_REGION_ID:
Paul Mackerras1189be62007-10-11 20:37:10 +1000990 vsid = get_kernel_vsid(ea, mmu_kernel_ssize);
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000991 if (ea < VMALLOC_END)
992 psize = mmu_vmalloc_psize;
993 else
994 psize = mmu_io_psize;
Paul Mackerras1189be62007-10-11 20:37:10 +1000995 ssize = mmu_kernel_ssize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 default:
998 /* Not a valid range
999 * Send the problem up to do_page_fault
1000 */
Li Zhongba12eed2013-05-13 16:16:41 +00001001 rc = 1;
1002 goto bail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 }
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001004 DBG_LOW(" mm=%p, mm->pgdir=%p, vsid=%016lx\n", mm, mm->pgd, vsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
Aneesh Kumar K.Vc60ac562013-03-13 03:34:54 +00001006 /* Bad address. */
1007 if (!vsid) {
1008 DBG_LOW("Bad address!\n");
Li Zhongba12eed2013-05-13 16:16:41 +00001009 rc = 1;
1010 goto bail;
Aneesh Kumar K.Vc60ac562013-03-13 03:34:54 +00001011 }
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001012 /* Get pgdir */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 pgdir = mm->pgd;
Li Zhongba12eed2013-05-13 16:16:41 +00001014 if (pgdir == NULL) {
1015 rc = 1;
1016 goto bail;
1017 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001019 /* Check CPU locality */
Rusty Russell56aa4122009-03-15 18:16:43 +00001020 tmp = cpumask_of(smp_processor_id());
1021 if (user_region && cpumask_equal(mm_cpumask(mm), tmp))
Aneesh Kumar K.Vaefa5682014-12-04 11:00:14 +05301022 flags |= HPTE_LOCAL_UPDATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +10001024#ifndef CONFIG_PPC_64K_PAGES
David Gibsona4fe3ce2009-10-26 19:24:31 +00001025 /* If we use 4K pages and our psize is not 4K, then we might
1026 * be hitting a special driver mapping, and need to align the
1027 * address before we fetch the PTE.
1028 *
1029 * It could also be a hugepage mapping, in which case this is
1030 * not necessary, but it's not harmful, either.
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +10001031 */
1032 if (psize != MMU_PAGE_4K)
1033 ea &= ~((1ul << mmu_psize_defs[psize].shift) - 1);
1034#endif /* CONFIG_PPC_64K_PAGES */
1035
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001036 /* Get PTE and page size from page tables */
Aneesh Kumar K.V891121e2015-10-09 08:32:21 +05301037 ptep = __find_linux_pte_or_hugepte(pgdir, ea, &is_thp, &hugeshift);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001038 if (ptep == NULL || !pte_present(*ptep)) {
1039 DBG_LOW(" no PTE !\n");
Li Zhongba12eed2013-05-13 16:16:41 +00001040 rc = 1;
1041 goto bail;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001042 }
1043
Benjamin Herrenschmidtca91e6c2010-07-23 08:53:23 +10001044 /* Add _PAGE_PRESENT to the required access perm */
1045 access |= _PAGE_PRESENT;
1046
1047 /* Pre-check access permissions (will be re-checked atomically
1048 * in __hash_page_XX but this pre-check is a fast path
1049 */
1050 if (access & ~pte_val(*ptep)) {
1051 DBG_LOW(" no access !\n");
Li Zhongba12eed2013-05-13 16:16:41 +00001052 rc = 1;
1053 goto bail;
Benjamin Herrenschmidtca91e6c2010-07-23 08:53:23 +10001054 }
1055
Li Zhongba12eed2013-05-13 16:16:41 +00001056 if (hugeshift) {
Aneesh Kumar K.V891121e2015-10-09 08:32:21 +05301057 if (is_thp)
Aneesh Kumar K.V6d492ec2013-06-20 14:30:21 +05301058 rc = __hash_page_thp(ea, access, vsid, (pmd_t *)ptep,
Aneesh Kumar K.Vaefa5682014-12-04 11:00:14 +05301059 trap, flags, ssize, psize);
Aneesh Kumar K.V6d492ec2013-06-20 14:30:21 +05301060#ifdef CONFIG_HUGETLB_PAGE
1061 else
1062 rc = __hash_page_huge(ea, access, vsid, ptep, trap,
Aneesh Kumar K.Vaefa5682014-12-04 11:00:14 +05301063 flags, ssize, hugeshift, psize);
Aneesh Kumar K.V6d492ec2013-06-20 14:30:21 +05301064#else
1065 else {
1066 /*
1067 * if we have hugeshift, and is not transhuge with
1068 * hugetlb disabled, something is really wrong.
1069 */
1070 rc = 1;
1071 WARN_ON(1);
1072 }
1073#endif
Ian Munsiea1dca3462014-10-08 19:54:58 +11001074 if (current->mm == mm)
1075 check_paca_psize(ea, mm, psize, user_region);
Michael Ellerman09567e72014-05-28 18:21:17 +10001076
Li Zhongba12eed2013-05-13 16:16:41 +00001077 goto bail;
1078 }
David Gibsona4fe3ce2009-10-26 19:24:31 +00001079
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001080#ifndef CONFIG_PPC_64K_PAGES
1081 DBG_LOW(" i-pte: %016lx\n", pte_val(*ptep));
1082#else
1083 DBG_LOW(" i-pte: %016lx %016lx\n", pte_val(*ptep),
1084 pte_val(*(ptep + PTRS_PER_PTE)));
1085#endif
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001086 /* Do actual hashing */
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +10001087#ifdef CONFIG_PPC_64K_PAGES
Paul Mackerras721151d2007-04-03 21:24:02 +10001088 /* If _PAGE_4K_PFN is set, make sure this is a 4k segment */
Paul Mackerras3a8247c2008-06-18 15:29:12 +10001089 if ((pte_val(*ptep) & _PAGE_4K_PFN) && psize == MMU_PAGE_64K) {
Paul Mackerras721151d2007-04-03 21:24:02 +10001090 demote_segment_4k(mm, ea);
1091 psize = MMU_PAGE_4K;
1092 }
1093
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +10001094 /* If this PTE is non-cacheable and we have restrictions on
1095 * using non cacheable large pages, then we switch to 4k
1096 */
1097 if (mmu_ci_restrictions && psize == MMU_PAGE_64K &&
1098 (pte_val(*ptep) & _PAGE_NO_CACHE)) {
1099 if (user_region) {
1100 demote_segment_4k(mm, ea);
1101 psize = MMU_PAGE_4K;
1102 } else if (ea < VMALLOC_END) {
1103 /*
1104 * some driver did a non-cacheable mapping
1105 * in vmalloc space, so switch vmalloc
1106 * to 4k pages
1107 */
1108 printk(KERN_ALERT "Reducing vmalloc segment "
1109 "to 4kB pages because of "
1110 "non-cacheable mapping\n");
1111 psize = mmu_vmalloc_psize = MMU_PAGE_4K;
Ian Munsiebe3ebfe2014-10-08 19:54:52 +11001112 copro_flush_all_slbs(mm);
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +10001113 }
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +10001114 }
Michael Ellerman09567e72014-05-28 18:21:17 +10001115
Aneesh Kumar K.V0863d7f2015-11-28 22:39:33 +05301116#endif /* CONFIG_PPC_64K_PAGES */
1117
Ian Munsiea1dca3462014-10-08 19:54:58 +11001118 if (current->mm == mm)
1119 check_paca_psize(ea, mm, psize, user_region);
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +10001120
Michael Ellerman73b341e2015-08-07 16:19:47 +10001121#ifdef CONFIG_PPC_64K_PAGES
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +10001122 if (psize == MMU_PAGE_64K)
Aneesh Kumar K.Vaefa5682014-12-04 11:00:14 +05301123 rc = __hash_page_64K(ea, access, vsid, ptep, trap,
1124 flags, ssize);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001125 else
Michael Ellerman73b341e2015-08-07 16:19:47 +10001126#endif /* CONFIG_PPC_64K_PAGES */
Paul Mackerrasfa282372008-01-24 08:35:13 +11001127 {
David Gibsona1128f82009-12-16 14:29:56 +00001128 int spp = subpage_protection(mm, ea);
Paul Mackerrasfa282372008-01-24 08:35:13 +11001129 if (access & spp)
1130 rc = -2;
1131 else
1132 rc = __hash_page_4K(ea, access, vsid, ptep, trap,
Aneesh Kumar K.Vaefa5682014-12-04 11:00:14 +05301133 flags, ssize, spp);
Paul Mackerrasfa282372008-01-24 08:35:13 +11001134 }
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001135
Benjamin Herrenschmidt4b8692c2010-07-23 10:31:13 +10001136 /* Dump some info in case of hash insertion failure, they should
1137 * never happen so it is really useful to know if/when they do
1138 */
1139 if (rc == -1)
1140 hash_failure_debug(ea, access, vsid, trap, ssize, psize,
Aneesh Kumar K.Vd8139eb2013-04-28 09:37:37 +00001141 psize, pte_val(*ptep));
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001142#ifndef CONFIG_PPC_64K_PAGES
1143 DBG_LOW(" o-pte: %016lx\n", pte_val(*ptep));
1144#else
1145 DBG_LOW(" o-pte: %016lx %016lx\n", pte_val(*ptep),
1146 pte_val(*(ptep + PTRS_PER_PTE)));
1147#endif
1148 DBG_LOW(" -> rc=%d\n", rc);
Li Zhongba12eed2013-05-13 16:16:41 +00001149
1150bail:
1151 exception_exit(prev_state);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001152 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153}
Ian Munsiea1dca3462014-10-08 19:54:58 +11001154EXPORT_SYMBOL_GPL(hash_page_mm);
1155
Aneesh Kumar K.Vaefa5682014-12-04 11:00:14 +05301156int hash_page(unsigned long ea, unsigned long access, unsigned long trap,
1157 unsigned long dsisr)
Ian Munsiea1dca3462014-10-08 19:54:58 +11001158{
Aneesh Kumar K.Vaefa5682014-12-04 11:00:14 +05301159 unsigned long flags = 0;
Ian Munsiea1dca3462014-10-08 19:54:58 +11001160 struct mm_struct *mm = current->mm;
1161
1162 if (REGION_ID(ea) == VMALLOC_REGION_ID)
1163 mm = &init_mm;
1164
Aneesh Kumar K.Vaefa5682014-12-04 11:00:14 +05301165 if (dsisr & DSISR_NOHPTE)
1166 flags |= HPTE_NOHPTE_UPDATE;
1167
1168 return hash_page_mm(mm, ea, access, trap, flags);
Ian Munsiea1dca3462014-10-08 19:54:58 +11001169}
Arnd Bergmann67207b92005-11-15 15:53:48 -05001170EXPORT_SYMBOL_GPL(hash_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
Aneesh Kumar K.V106713a2015-12-01 09:06:44 +05301172int __hash_page(unsigned long ea, unsigned long msr, unsigned long trap,
1173 unsigned long dsisr)
1174{
1175 unsigned long access = _PAGE_PRESENT;
1176 unsigned long flags = 0;
1177 struct mm_struct *mm = current->mm;
1178
1179 if (REGION_ID(ea) == VMALLOC_REGION_ID)
1180 mm = &init_mm;
1181
1182 if (dsisr & DSISR_NOHPTE)
1183 flags |= HPTE_NOHPTE_UPDATE;
1184
1185 if (dsisr & DSISR_ISSTORE)
1186 access |= _PAGE_RW;
1187 /*
1188 * We need to set the _PAGE_USER bit if MSR_PR is set or if we are
1189 * accessing a userspace segment (even from the kernel). We assume
1190 * kernel addresses always have the high bit set.
1191 */
1192 if ((msr & MSR_PR) || (REGION_ID(ea) == USER_REGION_ID))
1193 access |= _PAGE_USER;
1194
1195 if (trap == 0x400)
1196 access |= _PAGE_EXEC;
1197
1198 return hash_page_mm(mm, ea, access, trap, flags);
1199}
1200
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001201void hash_preload(struct mm_struct *mm, unsigned long ea,
1202 unsigned long access, unsigned long trap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203{
Aneesh Kumar K.V12bc9f62013-06-20 14:30:18 +05301204 int hugepage_shift;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001205 unsigned long vsid;
Michael Neuling0b97fee2010-11-17 18:52:45 +00001206 pgd_t *pgdir;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001207 pte_t *ptep;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001208 unsigned long flags;
Aneesh Kumar K.Vaefa5682014-12-04 11:00:14 +05301209 int rc, ssize, update_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +10001211 BUG_ON(REGION_ID(ea) != USER_REGION_ID);
1212
1213#ifdef CONFIG_PPC_MM_SLICES
1214 /* We only prefault standard pages for now */
Ilpo Järvinen2b02d132007-08-16 08:03:35 +10001215 if (unlikely(get_slice_psize(mm, ea) != mm->context.user_psize))
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001216 return;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +10001217#endif
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001218
1219 DBG_LOW("hash_preload(mm=%p, mm->pgdir=%p, ea=%016lx, access=%lx,"
1220 " trap=%lx\n", mm, mm->pgd, ea, access, trap);
1221
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +10001222 /* Get Linux PTE if available */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001223 pgdir = mm->pgd;
1224 if (pgdir == NULL)
1225 return;
Aneesh Kumar K.V0ac52dd2013-06-20 14:30:22 +05301226
1227 /* Get VSID */
1228 ssize = user_segment_size(ea);
1229 vsid = get_vsid(mm->context.id, ea, ssize);
1230 if (!vsid)
1231 return;
1232 /*
1233 * Hash doesn't like irqs. Walking linux page table with irq disabled
1234 * saves us from holding multiple locks.
1235 */
1236 local_irq_save(flags);
1237
Aneesh Kumar K.V12bc9f62013-06-20 14:30:18 +05301238 /*
1239 * THP pages use update_mmu_cache_pmd. We don't do
1240 * hash preload there. Hence can ignore THP here
1241 */
Aneesh Kumar K.V891121e2015-10-09 08:32:21 +05301242 ptep = find_linux_pte_or_hugepte(pgdir, ea, NULL, &hugepage_shift);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001243 if (!ptep)
Aneesh Kumar K.V0ac52dd2013-06-20 14:30:22 +05301244 goto out_exit;
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +10001245
Aneesh Kumar K.V12bc9f62013-06-20 14:30:18 +05301246 WARN_ON(hugepage_shift);
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +10001247#ifdef CONFIG_PPC_64K_PAGES
1248 /* If either _PAGE_4K_PFN or _PAGE_NO_CACHE is set (and we are on
1249 * a 64K kernel), then we don't preload, hash_page() will take
1250 * care of it once we actually try to access the page.
1251 * That way we don't have to duplicate all of the logic for segment
1252 * page size demotion here
1253 */
1254 if (pte_val(*ptep) & (_PAGE_4K_PFN | _PAGE_NO_CACHE))
Aneesh Kumar K.V0ac52dd2013-06-20 14:30:22 +05301255 goto out_exit;
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +10001256#endif /* CONFIG_PPC_64K_PAGES */
1257
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +10001258 /* Is that local to this CPU ? */
Rusty Russell56aa4122009-03-15 18:16:43 +00001259 if (cpumask_equal(mm_cpumask(mm), cpumask_of(smp_processor_id())))
Aneesh Kumar K.Vaefa5682014-12-04 11:00:14 +05301260 update_flags |= HPTE_LOCAL_UPDATE;
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +10001261
1262 /* Hash it in */
Michael Ellerman73b341e2015-08-07 16:19:47 +10001263#ifdef CONFIG_PPC_64K_PAGES
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +10001264 if (mm->context.user_psize == MMU_PAGE_64K)
Aneesh Kumar K.Vaefa5682014-12-04 11:00:14 +05301265 rc = __hash_page_64K(ea, access, vsid, ptep, trap,
1266 update_flags, ssize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 else
Michael Ellerman73b341e2015-08-07 16:19:47 +10001268#endif /* CONFIG_PPC_64K_PAGES */
Aneesh Kumar K.Vaefa5682014-12-04 11:00:14 +05301269 rc = __hash_page_4K(ea, access, vsid, ptep, trap, update_flags,
1270 ssize, subpage_protection(mm, ea));
Benjamin Herrenschmidt4b8692c2010-07-23 10:31:13 +10001271
1272 /* Dump some info in case of hash insertion failure, they should
1273 * never happen so it is really useful to know if/when they do
1274 */
1275 if (rc == -1)
1276 hash_failure_debug(ea, access, vsid, trap, ssize,
Aneesh Kumar K.Vd8139eb2013-04-28 09:37:37 +00001277 mm->context.user_psize,
1278 mm->context.user_psize,
1279 pte_val(*ptep));
Aneesh Kumar K.V0ac52dd2013-06-20 14:30:22 +05301280out_exit:
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001281 local_irq_restore(flags);
1282}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
Benjamin Herrenschmidtf6ab0b92007-10-29 12:05:18 +11001284/* WARNING: This is called from hash_low_64.S, if you change this prototype,
1285 * do not forget to update the assembly call site !
1286 */
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +00001287void flush_hash_page(unsigned long vpn, real_pte_t pte, int psize, int ssize,
Aneesh Kumar K.Vaefa5682014-12-04 11:00:14 +05301288 unsigned long flags)
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001289{
1290 unsigned long hash, index, shift, hidx, slot;
Aneesh Kumar K.Vaefa5682014-12-04 11:00:14 +05301291 int local = flags & HPTE_LOCAL_UPDATE;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001292
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +00001293 DBG_LOW("flush_hash_page(vpn=%016lx)\n", vpn);
1294 pte_iterate_hashed_subpages(pte, psize, vpn, index, shift) {
1295 hash = hpt_hash(vpn, shift, ssize);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001296 hidx = __rpte_to_hidx(pte, index);
1297 if (hidx & _PTEIDX_SECONDARY)
1298 hash = ~hash;
1299 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
1300 slot += hidx & _PTEIDX_GROUP_IX;
Sachin P. Sant5c339912009-12-13 21:15:12 +00001301 DBG_LOW(" sub %ld: hash=%lx, hidx=%lx\n", index, slot, hidx);
Aneesh Kumar K.Vdb3d8532013-06-20 14:30:13 +05301302 /*
1303 * We use same base page size and actual psize, because we don't
1304 * use these functions for hugepage
1305 */
1306 ppc_md.hpte_invalidate(slot, vpn, psize, psize, ssize, local);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001307 } pte_iterate_hashed_end();
Michael Neulingbc2a9402013-02-13 16:21:40 +00001308
1309#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1310 /* Transactions are not aborted by tlbiel, only tlbie.
1311 * Without, syncing a page back to a block device w/ PIO could pick up
1312 * transactional data (bad!) so we force an abort here. Before the
1313 * sync the page will be made read-only, which will flush_hash_page.
1314 * BIG ISSUE here: if the kernel uses a page from userspace without
1315 * unmapping it first, it may see the speculated version.
1316 */
1317 if (local && cpu_has_feature(CPU_FTR_TM) &&
Michael Neulingc2fd22d2013-05-02 15:36:14 +00001318 current->thread.regs &&
Michael Neulingbc2a9402013-02-13 16:21:40 +00001319 MSR_TM_ACTIVE(current->thread.regs->msr)) {
1320 tm_enable();
1321 tm_abort(TM_CAUSE_TLBI);
1322 }
1323#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324}
1325
Aneesh Kumar K.Vf1581bf2014-11-02 21:15:27 +05301326#ifdef CONFIG_TRANSPARENT_HUGEPAGE
1327void flush_hash_hugepage(unsigned long vsid, unsigned long addr,
Aneesh Kumar K.Vaefa5682014-12-04 11:00:14 +05301328 pmd_t *pmdp, unsigned int psize, int ssize,
1329 unsigned long flags)
Aneesh Kumar K.Vf1581bf2014-11-02 21:15:27 +05301330{
1331 int i, max_hpte_count, valid;
1332 unsigned long s_addr;
1333 unsigned char *hpte_slot_array;
1334 unsigned long hidx, shift, vpn, hash, slot;
Aneesh Kumar K.Vaefa5682014-12-04 11:00:14 +05301335 int local = flags & HPTE_LOCAL_UPDATE;
Aneesh Kumar K.Vf1581bf2014-11-02 21:15:27 +05301336
1337 s_addr = addr & HPAGE_PMD_MASK;
1338 hpte_slot_array = get_hpte_slot_array(pmdp);
1339 /*
1340 * IF we try to do a HUGE PTE update after a withdraw is done.
1341 * we will find the below NULL. This happens when we do
1342 * split_huge_page_pmd
1343 */
1344 if (!hpte_slot_array)
1345 return;
1346
Aneesh Kumar K.Vd557b092014-11-02 21:15:28 +05301347 if (ppc_md.hugepage_invalidate) {
1348 ppc_md.hugepage_invalidate(vsid, s_addr, hpte_slot_array,
1349 psize, ssize, local);
1350 goto tm_abort;
1351 }
Aneesh Kumar K.Vf1581bf2014-11-02 21:15:27 +05301352 /*
1353 * No bluk hpte removal support, invalidate each entry
1354 */
1355 shift = mmu_psize_defs[psize].shift;
1356 max_hpte_count = HPAGE_PMD_SIZE >> shift;
1357 for (i = 0; i < max_hpte_count; i++) {
1358 /*
1359 * 8 bits per each hpte entries
1360 * 000| [ secondary group (one bit) | hidx (3 bits) | valid bit]
1361 */
1362 valid = hpte_valid(hpte_slot_array, i);
1363 if (!valid)
1364 continue;
1365 hidx = hpte_hash_index(hpte_slot_array, i);
1366
1367 /* get the vpn */
1368 addr = s_addr + (i * (1ul << shift));
1369 vpn = hpt_vpn(addr, vsid, ssize);
1370 hash = hpt_hash(vpn, shift, ssize);
1371 if (hidx & _PTEIDX_SECONDARY)
1372 hash = ~hash;
1373
1374 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
1375 slot += hidx & _PTEIDX_GROUP_IX;
1376 ppc_md.hpte_invalidate(slot, vpn, psize,
Aneesh Kumar K.Vd557b092014-11-02 21:15:28 +05301377 MMU_PAGE_16M, ssize, local);
Aneesh Kumar K.Vf1581bf2014-11-02 21:15:27 +05301378 }
Aneesh Kumar K.Vd557b092014-11-02 21:15:28 +05301379tm_abort:
1380#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1381 /* Transactions are not aborted by tlbiel, only tlbie.
1382 * Without, syncing a page back to a block device w/ PIO could pick up
1383 * transactional data (bad!) so we force an abort here. Before the
1384 * sync the page will be made read-only, which will flush_hash_page.
1385 * BIG ISSUE here: if the kernel uses a page from userspace without
1386 * unmapping it first, it may see the speculated version.
1387 */
1388 if (local && cpu_has_feature(CPU_FTR_TM) &&
1389 current->thread.regs &&
1390 MSR_TM_ACTIVE(current->thread.regs->msr)) {
1391 tm_enable();
1392 tm_abort(TM_CAUSE_TLBI);
1393 }
1394#endif
Aneesh Kumar K.V2e8266952015-04-21 20:10:26 +05301395 return;
Aneesh Kumar K.Vf1581bf2014-11-02 21:15:27 +05301396}
1397#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
1398
Benjamin Herrenschmidt61b1a942005-09-20 13:52:50 +10001399void flush_hash_range(unsigned long number, int local)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400{
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001401 if (ppc_md.flush_hash_range)
Benjamin Herrenschmidt61b1a942005-09-20 13:52:50 +10001402 ppc_md.flush_hash_range(number, local);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001403 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 int i;
Benjamin Herrenschmidt61b1a942005-09-20 13:52:50 +10001405 struct ppc64_tlb_batch *batch =
Christoph Lameter69111ba2014-10-21 15:23:25 -05001406 this_cpu_ptr(&ppc64_tlb_batch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
1408 for (i = 0; i < number; i++)
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +00001409 flush_hash_page(batch->vpn[i], batch->pte[i],
Paul Mackerras1189be62007-10-11 20:37:10 +10001410 batch->psize, batch->ssize, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 }
1412}
1413
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414/*
1415 * low_hash_fault is called when we the low level hash code failed
1416 * to instert a PTE due to an hypervisor error
1417 */
Paul Mackerrasfa282372008-01-24 08:35:13 +11001418void low_hash_fault(struct pt_regs *regs, unsigned long address, int rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419{
Li Zhongba12eed2013-05-13 16:16:41 +00001420 enum ctx_state prev_state = exception_enter();
1421
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 if (user_mode(regs)) {
Paul Mackerrasfa282372008-01-24 08:35:13 +11001423#ifdef CONFIG_PPC_SUBPAGE_PROT
1424 if (rc == -2)
1425 _exception(SIGSEGV, regs, SEGV_ACCERR, address);
1426 else
1427#endif
1428 _exception(SIGBUS, regs, BUS_ADRERR, address);
1429 } else
1430 bad_page_fault(regs, address, SIGBUS);
Li Zhongba12eed2013-05-13 16:16:41 +00001431
1432 exception_exit(prev_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433}
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +10001434
Li Zhongb170bd32013-04-15 16:53:19 +00001435long hpte_insert_repeating(unsigned long hash, unsigned long vpn,
1436 unsigned long pa, unsigned long rflags,
1437 unsigned long vflags, int psize, int ssize)
1438{
1439 unsigned long hpte_group;
1440 long slot;
1441
1442repeat:
1443 hpte_group = ((hash & htab_hash_mask) *
1444 HPTES_PER_GROUP) & ~0x7UL;
1445
1446 /* Insert into the hash table, primary slot */
1447 slot = ppc_md.hpte_insert(hpte_group, vpn, pa, rflags, vflags,
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +00001448 psize, psize, ssize);
Li Zhongb170bd32013-04-15 16:53:19 +00001449
1450 /* Primary is full, try the secondary */
1451 if (unlikely(slot == -1)) {
1452 hpte_group = ((~hash & htab_hash_mask) *
1453 HPTES_PER_GROUP) & ~0x7UL;
1454 slot = ppc_md.hpte_insert(hpte_group, vpn, pa, rflags,
1455 vflags | HPTE_V_SECONDARY,
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +00001456 psize, psize, ssize);
Li Zhongb170bd32013-04-15 16:53:19 +00001457 if (slot == -1) {
1458 if (mftb() & 0x1)
1459 hpte_group = ((hash & htab_hash_mask) *
1460 HPTES_PER_GROUP)&~0x7UL;
1461
1462 ppc_md.hpte_remove(hpte_group);
1463 goto repeat;
1464 }
1465 }
1466
1467 return slot;
1468}
1469
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +10001470#ifdef CONFIG_DEBUG_PAGEALLOC
1471static void kernel_map_linear_page(unsigned long vaddr, unsigned long lmi)
1472{
Li Zhong016af592013-04-15 16:53:20 +00001473 unsigned long hash;
Paul Mackerras1189be62007-10-11 20:37:10 +10001474 unsigned long vsid = get_kernel_vsid(vaddr, mmu_kernel_ssize);
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +00001475 unsigned long vpn = hpt_vpn(vaddr, vsid, mmu_kernel_ssize);
Michael Ellerman09f3f322015-06-01 21:11:35 +10001476 unsigned long mode = htab_convert_pte_flags(pgprot_val(PAGE_KERNEL));
Li Zhong016af592013-04-15 16:53:20 +00001477 long ret;
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +10001478
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +00001479 hash = hpt_hash(vpn, PAGE_SHIFT, mmu_kernel_ssize);
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +10001480
Aneesh Kumar K.Vc60ac562013-03-13 03:34:54 +00001481 /* Don't create HPTE entries for bad address */
1482 if (!vsid)
1483 return;
Li Zhong016af592013-04-15 16:53:20 +00001484
1485 ret = hpte_insert_repeating(hash, vpn, __pa(vaddr), mode,
1486 HPTE_V_BOLTED,
1487 mmu_linear_psize, mmu_kernel_ssize);
1488
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +10001489 BUG_ON (ret < 0);
1490 spin_lock(&linear_map_hash_lock);
1491 BUG_ON(linear_map_hash_slots[lmi] & 0x80);
1492 linear_map_hash_slots[lmi] = ret | 0x80;
1493 spin_unlock(&linear_map_hash_lock);
1494}
1495
1496static void kernel_unmap_linear_page(unsigned long vaddr, unsigned long lmi)
1497{
Paul Mackerras1189be62007-10-11 20:37:10 +10001498 unsigned long hash, hidx, slot;
1499 unsigned long vsid = get_kernel_vsid(vaddr, mmu_kernel_ssize);
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +00001500 unsigned long vpn = hpt_vpn(vaddr, vsid, mmu_kernel_ssize);
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +10001501
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +00001502 hash = hpt_hash(vpn, PAGE_SHIFT, mmu_kernel_ssize);
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +10001503 spin_lock(&linear_map_hash_lock);
1504 BUG_ON(!(linear_map_hash_slots[lmi] & 0x80));
1505 hidx = linear_map_hash_slots[lmi] & 0x7f;
1506 linear_map_hash_slots[lmi] = 0;
1507 spin_unlock(&linear_map_hash_lock);
1508 if (hidx & _PTEIDX_SECONDARY)
1509 hash = ~hash;
1510 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
1511 slot += hidx & _PTEIDX_GROUP_IX;
Aneesh Kumar K.Vdb3d8532013-06-20 14:30:13 +05301512 ppc_md.hpte_invalidate(slot, vpn, mmu_linear_psize, mmu_linear_psize,
1513 mmu_kernel_ssize, 0);
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +10001514}
1515
Joonsoo Kim031bc572014-12-12 16:55:52 -08001516void __kernel_map_pages(struct page *page, int numpages, int enable)
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +10001517{
1518 unsigned long flags, vaddr, lmi;
1519 int i;
1520
1521 local_irq_save(flags);
1522 for (i = 0; i < numpages; i++, page++) {
1523 vaddr = (unsigned long)page_address(page);
1524 lmi = __pa(vaddr) >> PAGE_SHIFT;
1525 if (lmi >= linear_map_hash_count)
1526 continue;
1527 if (enable)
1528 kernel_map_linear_page(vaddr, lmi);
1529 else
1530 kernel_unmap_linear_page(vaddr, lmi);
1531 }
1532 local_irq_restore(flags);
1533}
1534#endif /* CONFIG_DEBUG_PAGEALLOC */
Benjamin Herrenschmidtcd3db0c2010-07-06 15:39:02 -07001535
1536void setup_initial_memory_limit(phys_addr_t first_memblock_base,
1537 phys_addr_t first_memblock_size)
1538{
1539 /* We don't currently support the first MEMBLOCK not mapping 0
1540 * physical on those processors
1541 */
1542 BUG_ON(first_memblock_base != 0);
1543
1544 /* On LPAR systems, the first entry is our RMA region,
1545 * non-LPAR 64-bit hash MMU systems don't have a limitation
1546 * on real mode access, but using the first entry works well
1547 * enough. We also clamp it to 1G to avoid some funky things
1548 * such as RTAS bugs etc...
1549 */
1550 ppc64_rma_size = min_t(u64, first_memblock_size, 0x40000000);
1551
1552 /* Finally limit subsequent allocations */
1553 memblock_set_current_limit(ppc64_rma_size);
1554}