blob: bc6cc2ad037b7bba7ee01b9d6e5cf2d3c90559a3 [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>
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +100054#include <asm/spu.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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61#ifdef DEBUG
62#define DBG(fmt...) udbg_printf(fmt)
63#else
64#define DBG(fmt...)
65#endif
66
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +110067#ifdef DEBUG_LOW
68#define DBG_LOW(fmt...) udbg_printf(fmt)
69#else
70#define DBG_LOW(fmt...)
71#endif
72
73#define KB (1024)
74#define MB (1024*KB)
Jon Tollefson658013e2008-07-23 21:27:54 -070075#define GB (1024L*MB)
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +110076
Linus Torvalds1da177e2005-04-16 15:20:36 -070077/*
78 * Note: pte --> Linux PTE
79 * HPTE --> PowerPC Hashed Page Table Entry
80 *
81 * Execution context:
82 * htab_initialize is called with the MMU off (of course), but
83 * the kernel has been copied down to zero so it can directly
84 * reference global data. At this point it is very difficult
85 * to print debug info.
86 *
87 */
88
89#ifdef CONFIG_U3_DART
90extern unsigned long dart_tablebase;
91#endif /* CONFIG_U3_DART */
92
Paul Mackerras799d6042005-11-10 13:37:51 +110093static unsigned long _SDR1;
94struct mmu_psize_def mmu_psize_defs[MMU_PAGE_COUNT];
Anton Blancharde1802b02014-08-20 08:00:02 +100095EXPORT_SYMBOL_GPL(mmu_psize_defs);
Paul Mackerras799d6042005-11-10 13:37:51 +110096
David Gibson8e561e72007-06-13 14:52:56 +100097struct hash_pte *htab_address;
Michael Ellerman337a7122006-02-21 17:22:55 +110098unsigned long htab_size_bytes;
David Gibson96e28442005-07-13 01:11:42 -070099unsigned long htab_hash_mask;
Alexander Graf4ab79aa2009-10-30 05:47:19 +0000100EXPORT_SYMBOL_GPL(htab_hash_mask);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100101int mmu_linear_psize = MMU_PAGE_4K;
102int mmu_virtual_psize = MMU_PAGE_4K;
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000103int mmu_vmalloc_psize = MMU_PAGE_4K;
Benjamin Herrenschmidtcec08e72008-04-30 15:41:48 +1000104#ifdef CONFIG_SPARSEMEM_VMEMMAP
105int mmu_vmemmap_psize = MMU_PAGE_4K;
106#endif
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000107int mmu_io_psize = MMU_PAGE_4K;
Paul Mackerras1189be62007-10-11 20:37:10 +1000108int mmu_kernel_ssize = MMU_SEGSIZE_256M;
109int mmu_highuser_ssize = MMU_SEGSIZE_256M;
Michael Neuling584f8b72007-12-06 17:24:48 +1100110u16 mmu_slb_size = 64;
Alexander Graf4ab79aa2009-10-30 05:47:19 +0000111EXPORT_SYMBOL_GPL(mmu_slb_size);
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000112#ifdef CONFIG_PPC_64K_PAGES
113int mmu_ci_restrictions;
114#endif
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000115#ifdef CONFIG_DEBUG_PAGEALLOC
116static u8 *linear_map_hash_slots;
117static unsigned long linear_map_hash_count;
Michael Ellermaned166692007-04-18 11:50:09 +1000118static DEFINE_SPINLOCK(linear_map_hash_lock);
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000119#endif /* CONFIG_DEBUG_PAGEALLOC */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100121/* There are definitions of page sizes arrays to be used when none
122 * is provided by the firmware.
123 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100125/* Pre-POWER4 CPUs (4k pages only)
126 */
Michael Ellerman09de9ff2008-05-08 14:27:07 +1000127static struct mmu_psize_def mmu_psize_defaults_old[] = {
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100128 [MMU_PAGE_4K] = {
129 .shift = 12,
130 .sllp = 0,
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000131 .penc = {[MMU_PAGE_4K] = 0, [1 ... MMU_PAGE_COUNT - 1] = -1},
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100132 .avpnm = 0,
133 .tlbiel = 0,
134 },
135};
136
137/* POWER4, GPUL, POWER5
138 *
139 * Support for 16Mb large pages
140 */
Michael Ellerman09de9ff2008-05-08 14:27:07 +1000141static struct mmu_psize_def mmu_psize_defaults_gp[] = {
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100142 [MMU_PAGE_4K] = {
143 .shift = 12,
144 .sllp = 0,
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000145 .penc = {[MMU_PAGE_4K] = 0, [1 ... MMU_PAGE_COUNT - 1] = -1},
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100146 .avpnm = 0,
147 .tlbiel = 1,
148 },
149 [MMU_PAGE_16M] = {
150 .shift = 24,
151 .sllp = SLB_VSID_L,
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000152 .penc = {[0 ... MMU_PAGE_16M - 1] = -1, [MMU_PAGE_16M] = 0,
153 [MMU_PAGE_16M + 1 ... MMU_PAGE_COUNT - 1] = -1 },
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100154 .avpnm = 0x1UL,
155 .tlbiel = 0,
156 },
157};
158
Benjamin Herrenschmidtbc033b62008-08-05 16:19:56 +1000159static unsigned long htab_convert_pte_flags(unsigned long pteflags)
160{
161 unsigned long rflags = pteflags & 0x1fa;
162
163 /* _PAGE_EXEC -> NOEXEC */
164 if ((pteflags & _PAGE_EXEC) == 0)
165 rflags |= HPTE_R_N;
166
167 /* PP bits. PAGE_USER is already PP bit 0x2, so we only
168 * need to add in 0x1 if it's a read-only user page
169 */
170 if ((pteflags & _PAGE_USER) && !((pteflags & _PAGE_RW) &&
171 (pteflags & _PAGE_DIRTY)))
172 rflags |= 1;
Aneesh Kumar K.Vc8c06f52013-11-18 14:58:10 +0530173 /*
174 * Always add "C" bit for perf. Memory coherence is always enabled
175 */
176 return rflags | HPTE_R_C | HPTE_R_M;
Benjamin Herrenschmidtbc033b62008-08-05 16:19:56 +1000177}
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100178
179int htab_bolt_mapping(unsigned long vstart, unsigned long vend,
Benjamin Herrenschmidtbc033b62008-08-05 16:19:56 +1000180 unsigned long pstart, unsigned long prot,
Paul Mackerras1189be62007-10-11 20:37:10 +1000181 int psize, int ssize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100183 unsigned long vaddr, paddr;
184 unsigned int step, shift;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100185 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100187 shift = mmu_psize_defs[psize].shift;
188 step = 1 << shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Benjamin Herrenschmidtbc033b62008-08-05 16:19:56 +1000190 prot = htab_convert_pte_flags(prot);
191
192 DBG("htab_bolt_mapping(%lx..%lx -> %lx (%lx,%d,%d)\n",
193 vstart, vend, pstart, prot, psize, ssize);
194
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100195 for (vaddr = vstart, paddr = pstart; vaddr < vend;
196 vaddr += step, paddr += step) {
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000197 unsigned long hash, hpteg;
Paul Mackerras1189be62007-10-11 20:37:10 +1000198 unsigned long vsid = get_kernel_vsid(vaddr, ssize);
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000199 unsigned long vpn = hpt_vpn(vaddr, vsid, ssize);
Paul Mackerras9e88ba42008-08-30 11:26:27 +1000200 unsigned long tprot = prot;
201
Aneesh Kumar K.Vc60ac562013-03-13 03:34:54 +0000202 /*
203 * If we hit a bad address return error.
204 */
205 if (!vsid)
206 return -1;
Paul Mackerras9e88ba42008-08-30 11:26:27 +1000207 /* Make kernel text executable */
Paul Mackerras549e8152008-08-30 11:43:47 +1000208 if (overlaps_kernel_text(vaddr, vaddr + step))
Paul Mackerras9e88ba42008-08-30 11:26:27 +1000209 tprot &= ~HPTE_R_N;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Alexander Grafb18db0b2014-04-29 12:17:26 +0200211 /* Make kvm guest trampolines executable */
212 if (overlaps_kvm_tmp(vaddr, vaddr + step))
213 tprot &= ~HPTE_R_N;
214
Mahesh Salgaonkar429d2e82014-01-31 00:31:04 +0530215 /*
216 * If relocatable, check if it overlaps interrupt vectors that
217 * are copied down to real 0. For relocatable kernel
218 * (e.g. kdump case) we copy interrupt vectors down to real
219 * address 0. Mark that region as executable. This is
220 * because on p8 system with relocation on exception feature
221 * enabled, exceptions are raised with MMU (IR=DR=1) ON. Hence
222 * in order to execute the interrupt handlers in virtual
223 * mode the vector region need to be marked as executable.
224 */
225 if ((PHYSICAL_START > MEMORY_START) &&
226 overlaps_interrupt_vector_text(vaddr, vaddr + step))
227 tprot &= ~HPTE_R_N;
228
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000229 hash = hpt_hash(vpn, shift, ssize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
231
Michael Ellermanc30a4df2006-06-23 18:16:39 +1000232 BUG_ON(!ppc_md.hpte_insert);
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000233 ret = ppc_md.hpte_insert(hpteg, vpn, paddr, tprot,
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000234 HPTE_V_BOLTED, psize, psize, ssize);
Michael Ellermanc30a4df2006-06-23 18:16:39 +1000235
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100236 if (ret < 0)
237 break;
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000238#ifdef CONFIG_DEBUG_PAGEALLOC
239 if ((paddr >> PAGE_SHIFT) < linear_map_hash_count)
240 linear_map_hash_slots[paddr >> PAGE_SHIFT] = ret | 0x80;
241#endif /* CONFIG_DEBUG_PAGEALLOC */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 }
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100243 return ret < 0 ? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244}
245
Stephen Rothwellae86f002008-03-27 16:08:57 +1100246#ifdef CONFIG_MEMORY_HOTPLUG
Li Zhonged5694a2014-06-11 16:23:37 +0800247int htab_remove_mapping(unsigned long vstart, unsigned long vend,
Badari Pulavartyf8c88032008-01-29 09:19:24 +1100248 int psize, int ssize)
249{
250 unsigned long vaddr;
251 unsigned int step, shift;
252
253 shift = mmu_psize_defs[psize].shift;
254 step = 1 << shift;
255
256 if (!ppc_md.hpte_removebolted) {
Badari Pulavarty52db9b42008-03-28 11:37:21 +1100257 printk(KERN_WARNING "Platform doesn't implement "
258 "hpte_removebolted\n");
259 return -EINVAL;
Badari Pulavartyf8c88032008-01-29 09:19:24 +1100260 }
261
262 for (vaddr = vstart; vaddr < vend; vaddr += step)
263 ppc_md.hpte_removebolted(vaddr, psize, ssize);
Badari Pulavarty52db9b42008-03-28 11:37:21 +1100264
265 return 0;
Badari Pulavartyf8c88032008-01-29 09:19:24 +1100266}
Stephen Rothwellae86f002008-03-27 16:08:57 +1100267#endif /* CONFIG_MEMORY_HOTPLUG */
Badari Pulavartyf8c88032008-01-29 09:19:24 +1100268
Paul Mackerras1189be62007-10-11 20:37:10 +1000269static int __init htab_dt_scan_seg_sizes(unsigned long node,
270 const char *uname, int depth,
271 void *data)
272{
Rob Herring9d0c4df2014-04-01 23:49:03 -0500273 const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
274 const __be32 *prop;
275 int size = 0;
Paul Mackerras1189be62007-10-11 20:37:10 +1000276
277 /* We are scanning "cpu" nodes only */
278 if (type == NULL || strcmp(type, "cpu") != 0)
279 return 0;
280
Anton Blanchard12f04f22013-09-23 12:04:36 +1000281 prop = of_get_flat_dt_prop(node, "ibm,processor-segment-sizes", &size);
Paul Mackerras1189be62007-10-11 20:37:10 +1000282 if (prop == NULL)
283 return 0;
284 for (; size >= 4; size -= 4, ++prop) {
Anton Blanchard12f04f22013-09-23 12:04:36 +1000285 if (be32_to_cpu(prop[0]) == 40) {
Paul Mackerras1189be62007-10-11 20:37:10 +1000286 DBG("1T segment support detected\n");
Matt Evans44ae3ab2011-04-06 19:48:50 +0000287 cur_cpu_spec->mmu_features |= MMU_FTR_1T_SEGMENT;
Olof Johanssonf5534002007-10-12 16:44:55 +1000288 return 1;
Paul Mackerras1189be62007-10-11 20:37:10 +1000289 }
Paul Mackerras1189be62007-10-11 20:37:10 +1000290 }
Matt Evans44ae3ab2011-04-06 19:48:50 +0000291 cur_cpu_spec->mmu_features &= ~MMU_FTR_NO_SLBIE_B;
Paul Mackerras1189be62007-10-11 20:37:10 +1000292 return 0;
293}
294
295static void __init htab_init_seg_sizes(void)
296{
297 of_scan_flat_dt(htab_dt_scan_seg_sizes, NULL);
298}
299
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000300static int __init get_idx_from_shift(unsigned int shift)
301{
302 int idx = -1;
303
304 switch (shift) {
305 case 0xc:
306 idx = MMU_PAGE_4K;
307 break;
308 case 0x10:
309 idx = MMU_PAGE_64K;
310 break;
311 case 0x14:
312 idx = MMU_PAGE_1M;
313 break;
314 case 0x18:
315 idx = MMU_PAGE_16M;
316 break;
317 case 0x22:
318 idx = MMU_PAGE_16G;
319 break;
320 }
321 return idx;
322}
323
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100324static int __init htab_dt_scan_page_sizes(unsigned long node,
325 const char *uname, int depth,
326 void *data)
327{
Rob Herring9d0c4df2014-04-01 23:49:03 -0500328 const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
329 const __be32 *prop;
330 int size = 0;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100331
332 /* We are scanning "cpu" nodes only */
333 if (type == NULL || strcmp(type, "cpu") != 0)
334 return 0;
335
Anton Blanchard12f04f22013-09-23 12:04:36 +1000336 prop = of_get_flat_dt_prop(node, "ibm,segment-page-sizes", &size);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100337 if (prop != NULL) {
Aneesh Kumar K.V3dc4fec2013-04-28 09:37:38 +0000338 pr_info("Page sizes from device-tree:\n");
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100339 size /= 4;
Matt Evans44ae3ab2011-04-06 19:48:50 +0000340 cur_cpu_spec->mmu_features &= ~(MMU_FTR_16M_PAGE);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100341 while(size > 0) {
Anton Blanchard12f04f22013-09-23 12:04:36 +1000342 unsigned int base_shift = be32_to_cpu(prop[0]);
343 unsigned int slbenc = be32_to_cpu(prop[1]);
344 unsigned int lpnum = be32_to_cpu(prop[2]);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100345 struct mmu_psize_def *def;
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000346 int idx, base_idx;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100347
348 size -= 3; prop += 3;
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000349 base_idx = get_idx_from_shift(base_shift);
350 if (base_idx < 0) {
351 /*
352 * skip the pte encoding also
353 */
354 prop += lpnum * 2; size -= lpnum * 2;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100355 continue;
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000356 }
357 def = &mmu_psize_defs[base_idx];
358 if (base_idx == MMU_PAGE_16M)
359 cur_cpu_spec->mmu_features |= MMU_FTR_16M_PAGE;
360
361 def->shift = base_shift;
362 if (base_shift <= 23)
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100363 def->avpnm = 0;
364 else
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000365 def->avpnm = (1 << (base_shift - 23)) - 1;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100366 def->sllp = slbenc;
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000367 /*
368 * We don't know for sure what's up with tlbiel, so
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100369 * for now we only set it for 4K and 64K pages
370 */
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000371 if (base_idx == MMU_PAGE_4K || base_idx == MMU_PAGE_64K)
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100372 def->tlbiel = 1;
373 else
374 def->tlbiel = 0;
375
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000376 while (size > 0 && lpnum) {
Anton Blanchard12f04f22013-09-23 12:04:36 +1000377 unsigned int shift = be32_to_cpu(prop[0]);
378 int penc = be32_to_cpu(prop[1]);
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000379
380 prop += 2; size -= 2;
381 lpnum--;
382
383 idx = get_idx_from_shift(shift);
384 if (idx < 0)
385 continue;
386
387 if (penc == -1)
388 pr_err("Invalid penc for base_shift=%d "
389 "shift=%d\n", base_shift, shift);
390
391 def->penc[idx] = penc;
Aneesh Kumar K.V3dc4fec2013-04-28 09:37:38 +0000392 pr_info("base_shift=%d: shift=%d, sllp=0x%04lx,"
393 " avpnm=0x%08lx, tlbiel=%d, penc=%d\n",
394 base_shift, shift, def->sllp,
395 def->avpnm, def->tlbiel, def->penc[idx]);
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000396 }
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100397 }
398 return 1;
399 }
400 return 0;
401}
402
Tony Breedse16a9c02008-07-31 13:51:42 +1000403#ifdef CONFIG_HUGETLB_PAGE
Jon Tollefson658013e2008-07-23 21:27:54 -0700404/* Scan for 16G memory blocks that have been set aside for huge pages
405 * and reserve those blocks for 16G huge pages.
406 */
407static int __init htab_dt_scan_hugepage_blocks(unsigned long node,
408 const char *uname, int depth,
409 void *data) {
Rob Herring9d0c4df2014-04-01 23:49:03 -0500410 const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
411 const __be64 *addr_prop;
412 const __be32 *page_count_prop;
Jon Tollefson658013e2008-07-23 21:27:54 -0700413 unsigned int expected_pages;
414 long unsigned int phys_addr;
415 long unsigned int block_size;
416
417 /* We are scanning "memory" nodes only */
418 if (type == NULL || strcmp(type, "memory") != 0)
419 return 0;
420
421 /* This property is the log base 2 of the number of virtual pages that
422 * will represent this memory block. */
423 page_count_prop = of_get_flat_dt_prop(node, "ibm,expected#pages", NULL);
424 if (page_count_prop == NULL)
425 return 0;
Anton Blanchard12f04f22013-09-23 12:04:36 +1000426 expected_pages = (1 << be32_to_cpu(page_count_prop[0]));
Jon Tollefson658013e2008-07-23 21:27:54 -0700427 addr_prop = of_get_flat_dt_prop(node, "reg", NULL);
428 if (addr_prop == NULL)
429 return 0;
Anton Blanchard12f04f22013-09-23 12:04:36 +1000430 phys_addr = be64_to_cpu(addr_prop[0]);
431 block_size = be64_to_cpu(addr_prop[1]);
Jon Tollefson658013e2008-07-23 21:27:54 -0700432 if (block_size != (16 * GB))
433 return 0;
434 printk(KERN_INFO "Huge page(16GB) memory: "
435 "addr = 0x%lX size = 0x%lX pages = %d\n",
436 phys_addr, block_size, expected_pages);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000437 if (phys_addr + (16 * GB) <= memblock_end_of_DRAM()) {
438 memblock_reserve(phys_addr, block_size * expected_pages);
Jon Tollefson4792adb2008-10-21 15:27:36 +0000439 add_gpage(phys_addr, block_size, expected_pages);
440 }
Jon Tollefson658013e2008-07-23 21:27:54 -0700441 return 0;
442}
Tony Breedse16a9c02008-07-31 13:51:42 +1000443#endif /* CONFIG_HUGETLB_PAGE */
Jon Tollefson658013e2008-07-23 21:27:54 -0700444
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000445static void mmu_psize_set_default_penc(void)
446{
447 int bpsize, apsize;
448 for (bpsize = 0; bpsize < MMU_PAGE_COUNT; bpsize++)
449 for (apsize = 0; apsize < MMU_PAGE_COUNT; apsize++)
450 mmu_psize_defs[bpsize].penc[apsize] = -1;
451}
452
Alexander Graf9048e642014-04-01 15:46:05 +0200453#ifdef CONFIG_PPC_64K_PAGES
454
455static bool might_have_hea(void)
456{
457 /*
458 * The HEA ethernet adapter requires awareness of the
459 * GX bus. Without that awareness we can easily assume
460 * we will never see an HEA ethernet device.
461 */
462#ifdef CONFIG_IBMEBUS
463 return !cpu_has_feature(CPU_FTR_ARCH_207S);
464#else
465 return false;
466#endif
467}
468
469#endif /* #ifdef CONFIG_PPC_64K_PAGES */
470
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100471static void __init htab_init_page_sizes(void)
472{
473 int rc;
474
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000475 /* se the invalid penc to -1 */
476 mmu_psize_set_default_penc();
477
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100478 /* Default to 4K pages only */
479 memcpy(mmu_psize_defs, mmu_psize_defaults_old,
480 sizeof(mmu_psize_defaults_old));
481
482 /*
483 * Try to find the available page sizes in the device-tree
484 */
485 rc = of_scan_flat_dt(htab_dt_scan_page_sizes, NULL);
486 if (rc != 0) /* Found */
487 goto found;
488
489 /*
490 * Not in the device-tree, let's fallback on known size
491 * list for 16M capable GP & GR
492 */
Matt Evans44ae3ab2011-04-06 19:48:50 +0000493 if (mmu_has_feature(MMU_FTR_16M_PAGE))
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100494 memcpy(mmu_psize_defs, mmu_psize_defaults_gp,
495 sizeof(mmu_psize_defaults_gp));
496 found:
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000497#ifndef CONFIG_DEBUG_PAGEALLOC
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100498 /*
499 * Pick a size for the linear mapping. Currently, we only support
500 * 16M, 1M and 4K which is the default
501 */
502 if (mmu_psize_defs[MMU_PAGE_16M].shift)
503 mmu_linear_psize = MMU_PAGE_16M;
504 else if (mmu_psize_defs[MMU_PAGE_1M].shift)
505 mmu_linear_psize = MMU_PAGE_1M;
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000506#endif /* CONFIG_DEBUG_PAGEALLOC */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100507
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000508#ifdef CONFIG_PPC_64K_PAGES
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100509 /*
510 * Pick a size for the ordinary pages. Default is 4K, we support
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000511 * 64K for user mappings and vmalloc if supported by the processor.
512 * We only use 64k for ioremap if the processor
513 * (and firmware) support cache-inhibited large pages.
514 * If not, we use 4k and set mmu_ci_restrictions so that
515 * hash_page knows to switch processes that use cache-inhibited
516 * mappings to 4k pages.
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100517 */
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000518 if (mmu_psize_defs[MMU_PAGE_64K].shift) {
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100519 mmu_virtual_psize = MMU_PAGE_64K;
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000520 mmu_vmalloc_psize = MMU_PAGE_64K;
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000521 if (mmu_linear_psize == MMU_PAGE_4K)
522 mmu_linear_psize = MMU_PAGE_64K;
Matt Evans44ae3ab2011-04-06 19:48:50 +0000523 if (mmu_has_feature(MMU_FTR_CI_LARGE_PAGE)) {
Paul Mackerrascfe666b2008-03-24 17:41:22 +1100524 /*
Alexander Graf9048e642014-04-01 15:46:05 +0200525 * When running on pSeries using 64k pages for ioremap
526 * would stop us accessing the HEA ethernet. So if we
527 * have the chance of ever seeing one, stay at 4k.
Paul Mackerrascfe666b2008-03-24 17:41:22 +1100528 */
Alexander Graf9048e642014-04-01 15:46:05 +0200529 if (!might_have_hea() || !machine_is(pseries))
Paul Mackerrascfe666b2008-03-24 17:41:22 +1100530 mmu_io_psize = MMU_PAGE_64K;
531 } else
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000532 mmu_ci_restrictions = 1;
533 }
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000534#endif /* CONFIG_PPC_64K_PAGES */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100535
Benjamin Herrenschmidtcec08e72008-04-30 15:41:48 +1000536#ifdef CONFIG_SPARSEMEM_VMEMMAP
537 /* We try to use 16M pages for vmemmap if that is supported
538 * and we have at least 1G of RAM at boot
539 */
540 if (mmu_psize_defs[MMU_PAGE_16M].shift &&
Yinghai Lu95f72d12010-07-12 14:36:09 +1000541 memblock_phys_mem_size() >= 0x40000000)
Benjamin Herrenschmidtcec08e72008-04-30 15:41:48 +1000542 mmu_vmemmap_psize = MMU_PAGE_16M;
543 else if (mmu_psize_defs[MMU_PAGE_64K].shift)
544 mmu_vmemmap_psize = MMU_PAGE_64K;
545 else
546 mmu_vmemmap_psize = MMU_PAGE_4K;
547#endif /* CONFIG_SPARSEMEM_VMEMMAP */
548
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000549 printk(KERN_DEBUG "Page orders: linear mapping = %d, "
Benjamin Herrenschmidtcec08e72008-04-30 15:41:48 +1000550 "virtual = %d, io = %d"
551#ifdef CONFIG_SPARSEMEM_VMEMMAP
552 ", vmemmap = %d"
553#endif
554 "\n",
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100555 mmu_psize_defs[mmu_linear_psize].shift,
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000556 mmu_psize_defs[mmu_virtual_psize].shift,
Benjamin Herrenschmidtcec08e72008-04-30 15:41:48 +1000557 mmu_psize_defs[mmu_io_psize].shift
558#ifdef CONFIG_SPARSEMEM_VMEMMAP
559 ,mmu_psize_defs[mmu_vmemmap_psize].shift
560#endif
561 );
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100562
563#ifdef CONFIG_HUGETLB_PAGE
Jon Tollefson658013e2008-07-23 21:27:54 -0700564 /* Reserve 16G huge page memory sections for huge pages */
565 of_scan_flat_dt(htab_dt_scan_hugepage_blocks, NULL);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100566#endif /* CONFIG_HUGETLB_PAGE */
567}
568
569static int __init htab_dt_scan_pftsize(unsigned long node,
570 const char *uname, int depth,
571 void *data)
572{
Rob Herring9d0c4df2014-04-01 23:49:03 -0500573 const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
574 const __be32 *prop;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100575
576 /* We are scanning "cpu" nodes only */
577 if (type == NULL || strcmp(type, "cpu") != 0)
578 return 0;
579
Anton Blanchard12f04f22013-09-23 12:04:36 +1000580 prop = of_get_flat_dt_prop(node, "ibm,pft-size", NULL);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100581 if (prop != NULL) {
582 /* pft_size[0] is the NUMA CEC cookie */
Anton Blanchard12f04f22013-09-23 12:04:36 +1000583 ppc64_pft_size = be32_to_cpu(prop[1]);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100584 return 1;
585 }
586 return 0;
587}
588
589static unsigned long __init htab_get_table_size(void)
Paul Mackerras3eac8c62005-10-12 16:58:53 +1000590{
Anton Blanchard13870b62009-02-13 11:57:30 +0000591 unsigned long mem_size, rnd_mem_size, pteg_count, psize;
Paul Mackerras3eac8c62005-10-12 16:58:53 +1000592
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100593 /* If hash size isn't already provided by the platform, we try to
Adrian Bunk943ffb52006-01-10 00:10:13 +0100594 * retrieve it from the device-tree. If it's not there neither, we
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100595 * calculate it now based on the total RAM size
Paul Mackerras3eac8c62005-10-12 16:58:53 +1000596 */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100597 if (ppc64_pft_size == 0)
598 of_scan_flat_dt(htab_dt_scan_pftsize, NULL);
Paul Mackerras3eac8c62005-10-12 16:58:53 +1000599 if (ppc64_pft_size)
600 return 1UL << ppc64_pft_size;
601
602 /* round mem_size up to next power of 2 */
Yinghai Lu95f72d12010-07-12 14:36:09 +1000603 mem_size = memblock_phys_mem_size();
Paul Mackerras799d6042005-11-10 13:37:51 +1100604 rnd_mem_size = 1UL << __ilog2(mem_size);
605 if (rnd_mem_size < mem_size)
Paul Mackerras3eac8c62005-10-12 16:58:53 +1000606 rnd_mem_size <<= 1;
607
608 /* # pages / 2 */
Anton Blanchard13870b62009-02-13 11:57:30 +0000609 psize = mmu_psize_defs[mmu_virtual_psize].shift;
610 pteg_count = max(rnd_mem_size >> (psize + 1), 1UL << 11);
Paul Mackerras3eac8c62005-10-12 16:58:53 +1000611
612 return pteg_count << 7;
613}
614
Mike Kravetz54b79242005-11-07 16:25:48 -0800615#ifdef CONFIG_MEMORY_HOTPLUG
Anton Blancharda1194092011-08-10 20:44:24 +0000616int create_section_mapping(unsigned long start, unsigned long end)
Mike Kravetz54b79242005-11-07 16:25:48 -0800617{
Anton Blancharda1194092011-08-10 20:44:24 +0000618 return htab_bolt_mapping(start, end, __pa(start),
David Gibsonf5ea64d2008-10-12 17:54:24 +0000619 pgprot_val(PAGE_KERNEL), mmu_linear_psize,
Anton Blancharda1194092011-08-10 20:44:24 +0000620 mmu_kernel_ssize);
Mike Kravetz54b79242005-11-07 16:25:48 -0800621}
Badari Pulavartyf8c88032008-01-29 09:19:24 +1100622
Badari Pulavarty52db9b42008-03-28 11:37:21 +1100623int remove_section_mapping(unsigned long start, unsigned long end)
Badari Pulavartyf8c88032008-01-29 09:19:24 +1100624{
Badari Pulavarty52db9b42008-03-28 11:37:21 +1100625 return htab_remove_mapping(start, end, mmu_linear_psize,
626 mmu_kernel_ssize);
Badari Pulavartyf8c88032008-01-29 09:19:24 +1100627}
Mike Kravetz54b79242005-11-07 16:25:48 -0800628#endif /* CONFIG_MEMORY_HOTPLUG */
629
Anton Blanchardb86206e2014-03-10 09:44:22 +1100630extern u32 htab_call_hpte_insert1[];
631extern u32 htab_call_hpte_insert2[];
632extern u32 htab_call_hpte_remove[];
633extern u32 htab_call_hpte_updatepp[];
634extern u32 ht64_call_hpte_insert1[];
635extern u32 ht64_call_hpte_insert2[];
636extern u32 ht64_call_hpte_remove[];
637extern u32 ht64_call_hpte_updatepp[];
Michael Ellerman7d0daae2006-06-23 18:16:38 +1000638
639static void __init htab_finish_init(void)
640{
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +1000641#ifdef CONFIG_PPC_HAS_HASH_64K
Anton Blanchardb68a70c2011-04-04 23:56:18 +0000642 patch_branch(ht64_call_hpte_insert1,
Anton Blanchard26f92062014-03-10 09:40:26 +1100643 ppc_function_entry(ppc_md.hpte_insert),
Anton Blanchardb68a70c2011-04-04 23:56:18 +0000644 BRANCH_SET_LINK);
645 patch_branch(ht64_call_hpte_insert2,
Anton Blanchard26f92062014-03-10 09:40:26 +1100646 ppc_function_entry(ppc_md.hpte_insert),
Anton Blanchardb68a70c2011-04-04 23:56:18 +0000647 BRANCH_SET_LINK);
648 patch_branch(ht64_call_hpte_remove,
Anton Blanchard26f92062014-03-10 09:40:26 +1100649 ppc_function_entry(ppc_md.hpte_remove),
Anton Blanchardb68a70c2011-04-04 23:56:18 +0000650 BRANCH_SET_LINK);
651 patch_branch(ht64_call_hpte_updatepp,
Anton Blanchard26f92062014-03-10 09:40:26 +1100652 ppc_function_entry(ppc_md.hpte_updatepp),
Anton Blanchardb68a70c2011-04-04 23:56:18 +0000653 BRANCH_SET_LINK);
Jon Tollefson5b825832007-05-17 04:43:02 +1000654#endif /* CONFIG_PPC_HAS_HASH_64K */
Michael Ellerman7d0daae2006-06-23 18:16:38 +1000655
Anton Blanchardb68a70c2011-04-04 23:56:18 +0000656 patch_branch(htab_call_hpte_insert1,
Anton Blanchard26f92062014-03-10 09:40:26 +1100657 ppc_function_entry(ppc_md.hpte_insert),
Anton Blanchardb68a70c2011-04-04 23:56:18 +0000658 BRANCH_SET_LINK);
659 patch_branch(htab_call_hpte_insert2,
Anton Blanchard26f92062014-03-10 09:40:26 +1100660 ppc_function_entry(ppc_md.hpte_insert),
Anton Blanchardb68a70c2011-04-04 23:56:18 +0000661 BRANCH_SET_LINK);
662 patch_branch(htab_call_hpte_remove,
Anton Blanchard26f92062014-03-10 09:40:26 +1100663 ppc_function_entry(ppc_md.hpte_remove),
Anton Blanchardb68a70c2011-04-04 23:56:18 +0000664 BRANCH_SET_LINK);
665 patch_branch(htab_call_hpte_updatepp,
Anton Blanchard26f92062014-03-10 09:40:26 +1100666 ppc_function_entry(ppc_md.hpte_updatepp),
Anton Blanchardb68a70c2011-04-04 23:56:18 +0000667 BRANCH_SET_LINK);
Michael Ellerman7d0daae2006-06-23 18:16:38 +1000668}
669
Benjamin Herrenschmidt757c74d2009-03-19 19:34:16 +0000670static void __init htab_initialize(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
Michael Ellerman337a7122006-02-21 17:22:55 +1100672 unsigned long table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 unsigned long pteg_count;
Paul Mackerras9e88ba42008-08-30 11:26:27 +1000674 unsigned long prot;
Michael Ellerman41d824b2008-01-30 01:13:59 +1100675 unsigned long base = 0, size = 0, limit;
Benjamin Herrenschmidt28be7072010-08-04 13:43:53 +1000676 struct memblock_region *reg;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 DBG(" -> htab_initialize()\n");
679
Paul Mackerras1189be62007-10-11 20:37:10 +1000680 /* Initialize segment sizes */
681 htab_init_seg_sizes();
682
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100683 /* Initialize page sizes */
684 htab_init_page_sizes();
685
Matt Evans44ae3ab2011-04-06 19:48:50 +0000686 if (mmu_has_feature(MMU_FTR_1T_SEGMENT)) {
Paul Mackerras1189be62007-10-11 20:37:10 +1000687 mmu_kernel_ssize = MMU_SEGSIZE_1T;
688 mmu_highuser_ssize = MMU_SEGSIZE_1T;
689 printk(KERN_INFO "Using 1TB segments\n");
690 }
691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 /*
693 * Calculate the required size of the htab. We want the number of
694 * PTEGs to equal one half the number of real pages.
695 */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100696 htab_size_bytes = htab_get_table_size();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 pteg_count = htab_size_bytes >> 7;
698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 htab_hash_mask = pteg_count - 1;
700
Michael Ellerman57cfb812006-03-21 20:45:59 +1100701 if (firmware_has_feature(FW_FEATURE_LPAR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 /* Using a hypervisor which owns the htab */
703 htab_address = NULL;
704 _SDR1 = 0;
Mahesh Salgaonkar3ccc00a2012-02-20 02:15:03 +0000705#ifdef CONFIG_FA_DUMP
706 /*
707 * If firmware assisted dump is active firmware preserves
708 * the contents of htab along with entire partition memory.
709 * Clear the htab if firmware assisted dump is active so
710 * that we dont end up using old mappings.
711 */
712 if (is_fadump_active() && ppc_md.hpte_clear_all)
713 ppc_md.hpte_clear_all();
714#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 } else {
716 /* Find storage for the HPT. Must be contiguous in
Michael Ellerman41d824b2008-01-30 01:13:59 +1100717 * the absolute address space. On cell we want it to be
Michael Ellerman31bf1112008-03-12 18:03:24 +1100718 * in the first 2 Gig so we can use it for IOMMU hacks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 */
Michael Ellerman41d824b2008-01-30 01:13:59 +1100720 if (machine_is(cell))
Michael Ellerman31bf1112008-03-12 18:03:24 +1100721 limit = 0x80000000;
Michael Ellerman41d824b2008-01-30 01:13:59 +1100722 else
Benjamin Herrenschmidt27f574c2010-07-06 15:39:00 -0700723 limit = MEMBLOCK_ALLOC_ANYWHERE;
Michael Ellerman41d824b2008-01-30 01:13:59 +1100724
Yinghai Lu95f72d12010-07-12 14:36:09 +1000725 table = memblock_alloc_base(htab_size_bytes, htab_size_bytes, limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
727 DBG("Hash table allocated at %lx, size: %lx\n", table,
728 htab_size_bytes);
729
Michael Ellerman70267a72012-07-25 21:19:50 +0000730 htab_address = __va(table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
732 /* htab absolute addr + encoded htabsize */
733 _SDR1 = table + __ilog2(pteg_count) - 11;
734
735 /* Initialize the HPT with no entries */
736 memset((void *)table, 0, htab_size_bytes);
Paul Mackerras799d6042005-11-10 13:37:51 +1100737
738 /* Set SDR1 */
739 mtspr(SPRN_SDR1, _SDR1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 }
741
David Gibsonf5ea64d2008-10-12 17:54:24 +0000742 prot = pgprot_val(PAGE_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000744#ifdef CONFIG_DEBUG_PAGEALLOC
Yinghai Lu95f72d12010-07-12 14:36:09 +1000745 linear_map_hash_count = memblock_end_of_DRAM() >> PAGE_SHIFT;
746 linear_map_hash_slots = __va(memblock_alloc_base(linear_map_hash_count,
Benjamin Herrenschmidtcd3db0c2010-07-06 15:39:02 -0700747 1, ppc64_rma_size));
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000748 memset(linear_map_hash_slots, 0, linear_map_hash_count);
749#endif /* CONFIG_DEBUG_PAGEALLOC */
750
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 /* On U3 based machines, we need to reserve the DART area and
752 * _NOT_ map it to avoid cache paradoxes as it's remapped non
753 * cacheable later on
754 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
756 /* create bolted the linear mapping in the hash table */
Benjamin Herrenschmidt28be7072010-08-04 13:43:53 +1000757 for_each_memblock(memory, reg) {
758 base = (unsigned long)__va(reg->base);
759 size = reg->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Sachin P. Sant5c339912009-12-13 21:15:12 +0000761 DBG("creating mapping for region: %lx..%lx (prot: %lx)\n",
Paul Mackerras9e88ba42008-08-30 11:26:27 +1000762 base, size, prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
764#ifdef CONFIG_U3_DART
765 /* Do not map the DART space. Fortunately, it will be aligned
Yinghai Lu95f72d12010-07-12 14:36:09 +1000766 * in such a way that it will not cross two memblock regions and
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100767 * will fit within a single 16Mb page.
768 * The DART space is assumed to be a full 16Mb region even if
769 * we only use 2Mb of that space. We will use more of it later
770 * for AGP GART. We have to use a full 16Mb large page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 */
772 DBG("DART base: %lx\n", dart_tablebase);
773
774 if (dart_tablebase != 0 && dart_tablebase >= base
775 && dart_tablebase < (base + size)) {
Michael Ellermancaf80e52006-03-21 20:45:51 +1100776 unsigned long dart_table_end = dart_tablebase + 16 * MB;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 if (base != dart_tablebase)
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100778 BUG_ON(htab_bolt_mapping(base, dart_tablebase,
Paul Mackerras9e88ba42008-08-30 11:26:27 +1000779 __pa(base), prot,
Paul Mackerras1189be62007-10-11 20:37:10 +1000780 mmu_linear_psize,
781 mmu_kernel_ssize));
Michael Ellermancaf80e52006-03-21 20:45:51 +1100782 if ((base + size) > dart_table_end)
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100783 BUG_ON(htab_bolt_mapping(dart_tablebase+16*MB,
Michael Ellermancaf80e52006-03-21 20:45:51 +1100784 base + size,
785 __pa(dart_table_end),
Paul Mackerras9e88ba42008-08-30 11:26:27 +1000786 prot,
Paul Mackerras1189be62007-10-11 20:37:10 +1000787 mmu_linear_psize,
788 mmu_kernel_ssize));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 continue;
790 }
791#endif /* CONFIG_U3_DART */
Michael Ellermancaf80e52006-03-21 20:45:51 +1100792 BUG_ON(htab_bolt_mapping(base, base + size, __pa(base),
Paul Mackerras9e88ba42008-08-30 11:26:27 +1000793 prot, mmu_linear_psize, mmu_kernel_ssize));
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -0700794 }
795 memblock_set_current_limit(MEMBLOCK_ALLOC_ANYWHERE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
797 /*
798 * If we have a memory_limit and we've allocated TCEs then we need to
799 * explicitly map the TCE area at the top of RAM. We also cope with the
800 * case that the TCEs start below memory_limit.
801 * tce_alloc_start/end are 16MB aligned so the mapping should work
802 * for either 4K or 16MB pages.
803 */
804 if (tce_alloc_start) {
Michael Ellermanb5666f72005-12-05 10:24:33 -0600805 tce_alloc_start = (unsigned long)__va(tce_alloc_start);
806 tce_alloc_end = (unsigned long)__va(tce_alloc_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
808 if (base + size >= tce_alloc_start)
809 tce_alloc_start = base + size + 1;
810
Michael Ellermancaf80e52006-03-21 20:45:51 +1100811 BUG_ON(htab_bolt_mapping(tce_alloc_start, tce_alloc_end,
Benjamin Herrenschmidtbc033b62008-08-05 16:19:56 +1000812 __pa(tce_alloc_start), prot,
Paul Mackerras1189be62007-10-11 20:37:10 +1000813 mmu_linear_psize, mmu_kernel_ssize));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 }
815
Michael Ellerman7d0daae2006-06-23 18:16:38 +1000816 htab_finish_init();
817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 DBG(" <- htab_initialize()\n");
819}
820#undef KB
821#undef MB
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
Benjamin Herrenschmidt757c74d2009-03-19 19:34:16 +0000823void __init early_init_mmu(void)
Paul Mackerras799d6042005-11-10 13:37:51 +1100824{
Benjamin Herrenschmidt757c74d2009-03-19 19:34:16 +0000825 /* Initialize the MMU Hash table and create the linear mapping
Michael Ellerman376af592014-07-10 12:29:19 +1000826 * of memory. Has to be done before SLB initialization as this is
827 * currently where the page size encoding is obtained.
Benjamin Herrenschmidt757c74d2009-03-19 19:34:16 +0000828 */
829 htab_initialize();
830
Michael Ellerman376af592014-07-10 12:29:19 +1000831 /* Initialize SLB management */
Michael Ellerman13b3d132014-07-10 12:29:20 +1000832 slb_initialize();
Benjamin Herrenschmidt757c74d2009-03-19 19:34:16 +0000833}
834
835#ifdef CONFIG_SMP
Paul Gortmaker061d19f2013-06-24 15:30:09 -0400836void early_init_mmu_secondary(void)
Benjamin Herrenschmidt757c74d2009-03-19 19:34:16 +0000837{
838 /* Initialize hash table for that CPU */
Michael Ellerman57cfb812006-03-21 20:45:59 +1100839 if (!firmware_has_feature(FW_FEATURE_LPAR))
Paul Mackerras799d6042005-11-10 13:37:51 +1100840 mtspr(SPRN_SDR1, _SDR1);
Benjamin Herrenschmidt757c74d2009-03-19 19:34:16 +0000841
Michael Ellerman376af592014-07-10 12:29:19 +1000842 /* Initialize SLB */
Michael Ellerman13b3d132014-07-10 12:29:20 +1000843 slb_initialize();
Paul Mackerras799d6042005-11-10 13:37:51 +1100844}
Benjamin Herrenschmidt757c74d2009-03-19 19:34:16 +0000845#endif /* CONFIG_SMP */
Paul Mackerras799d6042005-11-10 13:37:51 +1100846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847/*
848 * Called by asm hashtable.S for doing lazy icache flush
849 */
850unsigned int hash_page_do_lazy_icache(unsigned int pp, pte_t pte, int trap)
851{
852 struct page *page;
853
Benjamin Herrenschmidt76c8e252005-11-08 11:21:05 +1100854 if (!pfn_valid(pte_pfn(pte)))
855 return pp;
856
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 page = pte_page(pte);
858
859 /* page is dirty */
860 if (!test_bit(PG_arch_1, &page->flags) && !PageReserved(page)) {
861 if (trap == 0x400) {
David Gibson0895ecd2009-10-26 19:24:31 +0000862 flush_dcache_icache_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 set_bit(PG_arch_1, &page->flags);
864 } else
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100865 pp |= HPTE_R_N;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 }
867 return pp;
868}
869
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000870#ifdef CONFIG_PPC_MM_SLICES
871unsigned int get_paca_psize(unsigned long addr)
872{
Aneesh Kumar K.V7aa07272012-09-10 02:52:52 +0000873 u64 lpsizes;
874 unsigned char *hpsizes;
875 unsigned long index, mask_index;
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000876
877 if (addr < SLICE_LOW_TOP) {
Aneesh Kumar K.V7aa07272012-09-10 02:52:52 +0000878 lpsizes = get_paca()->context.low_slices_psize;
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000879 index = GET_LOW_SLICE_INDEX(addr);
Aneesh Kumar K.V7aa07272012-09-10 02:52:52 +0000880 return (lpsizes >> (index * 4)) & 0xF;
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000881 }
Aneesh Kumar K.V7aa07272012-09-10 02:52:52 +0000882 hpsizes = get_paca()->context.high_slices_psize;
883 index = GET_HIGH_SLICE_INDEX(addr);
884 mask_index = index & 0x1;
885 return (hpsizes[index >> 1] >> (mask_index * 4)) & 0xF;
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000886}
887
888#else
889unsigned int get_paca_psize(unsigned long addr)
890{
891 return get_paca()->context.user_psize;
892}
893#endif
894
Paul Mackerras721151d2007-04-03 21:24:02 +1000895/*
896 * Demote a segment to using 4k pages.
897 * For now this makes the whole process use 4k pages.
898 */
Paul Mackerras721151d2007-04-03 21:24:02 +1000899#ifdef CONFIG_PPC_64K_PAGES
Paul Mackerrasfa282372008-01-24 08:35:13 +1100900void demote_segment_4k(struct mm_struct *mm, unsigned long addr)
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +1000901{
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000902 if (get_slice_psize(mm, addr) == MMU_PAGE_4K)
Paul Mackerras721151d2007-04-03 21:24:02 +1000903 return;
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000904 slice_set_range_psize(mm, addr, 1, MMU_PAGE_4K);
Geert Uytterhoeven1e57ba82007-07-17 02:35:38 +1000905#ifdef CONFIG_SPU_BASE
Paul Mackerras721151d2007-04-03 21:24:02 +1000906 spu_flush_all_slbs(mm);
907#endif
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000908 if (get_paca_psize(addr) != MMU_PAGE_4K) {
Paul Mackerrasfa282372008-01-24 08:35:13 +1100909 get_paca()->context = mm->context;
910 slb_flush_and_rebolt();
911 }
Paul Mackerras721151d2007-04-03 21:24:02 +1000912}
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +1000913#endif /* CONFIG_PPC_64K_PAGES */
Paul Mackerras721151d2007-04-03 21:24:02 +1000914
Paul Mackerrasfa282372008-01-24 08:35:13 +1100915#ifdef CONFIG_PPC_SUBPAGE_PROT
916/*
917 * This looks up a 2-bit protection code for a 4k subpage of a 64k page.
918 * Userspace sets the subpage permissions using the subpage_prot system call.
919 *
920 * Result is 0: full permissions, _PAGE_RW: read-only,
921 * _PAGE_USER or _PAGE_USER|_PAGE_RW: no access.
922 */
David Gibsond28513b2009-11-26 18:56:04 +0000923static int subpage_protection(struct mm_struct *mm, unsigned long ea)
Paul Mackerrasfa282372008-01-24 08:35:13 +1100924{
David Gibsond28513b2009-11-26 18:56:04 +0000925 struct subpage_prot_table *spt = &mm->context.spt;
Paul Mackerrasfa282372008-01-24 08:35:13 +1100926 u32 spp = 0;
927 u32 **sbpm, *sbpp;
928
929 if (ea >= spt->maxaddr)
930 return 0;
Anton Blanchardb0d436c2013-08-07 02:01:24 +1000931 if (ea < 0x100000000UL) {
Paul Mackerrasfa282372008-01-24 08:35:13 +1100932 /* addresses below 4GB use spt->low_prot */
933 sbpm = spt->low_prot;
934 } else {
935 sbpm = spt->protptrs[ea >> SBP_L3_SHIFT];
936 if (!sbpm)
937 return 0;
938 }
939 sbpp = sbpm[(ea >> SBP_L2_SHIFT) & (SBP_L2_COUNT - 1)];
940 if (!sbpp)
941 return 0;
942 spp = sbpp[(ea >> PAGE_SHIFT) & (SBP_L1_COUNT - 1)];
943
944 /* extract 2-bit bitfield for this 4k subpage */
945 spp >>= 30 - 2 * ((ea >> 12) & 0xf);
946
947 /* turn 0,1,2,3 into combination of _PAGE_USER and _PAGE_RW */
948 spp = ((spp & 2) ? _PAGE_USER : 0) | ((spp & 1) ? _PAGE_RW : 0);
949 return spp;
950}
951
952#else /* CONFIG_PPC_SUBPAGE_PROT */
David Gibsond28513b2009-11-26 18:56:04 +0000953static inline int subpage_protection(struct mm_struct *mm, unsigned long ea)
Paul Mackerrasfa282372008-01-24 08:35:13 +1100954{
955 return 0;
956}
957#endif
958
Benjamin Herrenschmidt4b8692c2010-07-23 10:31:13 +1000959void hash_failure_debug(unsigned long ea, unsigned long access,
960 unsigned long vsid, unsigned long trap,
Aneesh Kumar K.Vd8139eb2013-04-28 09:37:37 +0000961 int ssize, int psize, int lpsize, unsigned long pte)
Benjamin Herrenschmidt4b8692c2010-07-23 10:31:13 +1000962{
963 if (!printk_ratelimit())
964 return;
965 pr_info("mm: Hashing failure ! EA=0x%lx access=0x%lx current=%s\n",
966 ea, access, current->comm);
Aneesh Kumar K.Vd8139eb2013-04-28 09:37:37 +0000967 pr_info(" trap=0x%lx vsid=0x%lx ssize=%d base psize=%d psize %d pte=0x%lx\n",
968 trap, vsid, ssize, psize, lpsize, pte);
Benjamin Herrenschmidt4b8692c2010-07-23 10:31:13 +1000969}
970
Michael Ellerman09567e72014-05-28 18:21:17 +1000971static void check_paca_psize(unsigned long ea, struct mm_struct *mm,
972 int psize, bool user_region)
973{
974 if (user_region) {
975 if (psize != get_paca_psize(ea)) {
976 get_paca()->context = mm->context;
977 slb_flush_and_rebolt();
978 }
979 } else if (get_paca()->vmalloc_sllp !=
980 mmu_psize_defs[mmu_vmalloc_psize].sllp) {
981 get_paca()->vmalloc_sllp =
982 mmu_psize_defs[mmu_vmalloc_psize].sllp;
983 slb_vmalloc_update();
984 }
985}
986
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987/* Result code is:
988 * 0 - handled
989 * 1 - normal page fault
990 * -1 - critical hash insertion error
Paul Mackerrasfa282372008-01-24 08:35:13 +1100991 * -2 - access not permitted by subpage protection mechanism
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 */
993int hash_page(unsigned long ea, unsigned long access, unsigned long trap)
994{
Li Zhongba12eed2013-05-13 16:16:41 +0000995 enum ctx_state prev_state = exception_enter();
David Gibsona1128f82009-12-16 14:29:56 +0000996 pgd_t *pgdir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 unsigned long vsid;
998 struct mm_struct *mm;
999 pte_t *ptep;
David Gibsona4fe3ce2009-10-26 19:24:31 +00001000 unsigned hugeshift;
Rusty Russell56aa4122009-03-15 18:16:43 +00001001 const struct cpumask *tmp;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001002 int rc, user_region = 0, local = 0;
Paul Mackerras1189be62007-10-11 20:37:10 +10001003 int psize, ssize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001005 DBG_LOW("hash_page(ea=%016lx, access=%lx, trap=%lx\n",
1006 ea, access, trap);
David Gibson1f8d4192005-05-05 16:15:13 -07001007
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001008 /* Get region & vsid */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 switch (REGION_ID(ea)) {
1010 case USER_REGION_ID:
1011 user_region = 1;
1012 mm = current->mm;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001013 if (! mm) {
1014 DBG_LOW(" user region with no mm !\n");
Li Zhongba12eed2013-05-13 16:16:41 +00001015 rc = 1;
1016 goto bail;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001017 }
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +10001018 psize = get_slice_psize(mm, ea);
Paul Mackerras1189be62007-10-11 20:37:10 +10001019 ssize = user_segment_size(ea);
1020 vsid = get_vsid(mm->context.id, ea, ssize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 case VMALLOC_REGION_ID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 mm = &init_mm;
Paul Mackerras1189be62007-10-11 20:37:10 +10001024 vsid = get_kernel_vsid(ea, mmu_kernel_ssize);
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +10001025 if (ea < VMALLOC_END)
1026 psize = mmu_vmalloc_psize;
1027 else
1028 psize = mmu_io_psize;
Paul Mackerras1189be62007-10-11 20:37:10 +10001029 ssize = mmu_kernel_ssize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 default:
1032 /* Not a valid range
1033 * Send the problem up to do_page_fault
1034 */
Li Zhongba12eed2013-05-13 16:16:41 +00001035 rc = 1;
1036 goto bail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 }
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001038 DBG_LOW(" mm=%p, mm->pgdir=%p, vsid=%016lx\n", mm, mm->pgd, vsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
Aneesh Kumar K.Vc60ac562013-03-13 03:34:54 +00001040 /* Bad address. */
1041 if (!vsid) {
1042 DBG_LOW("Bad address!\n");
Li Zhongba12eed2013-05-13 16:16:41 +00001043 rc = 1;
1044 goto bail;
Aneesh Kumar K.Vc60ac562013-03-13 03:34:54 +00001045 }
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001046 /* Get pgdir */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 pgdir = mm->pgd;
Li Zhongba12eed2013-05-13 16:16:41 +00001048 if (pgdir == NULL) {
1049 rc = 1;
1050 goto bail;
1051 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001053 /* Check CPU locality */
Rusty Russell56aa4122009-03-15 18:16:43 +00001054 tmp = cpumask_of(smp_processor_id());
1055 if (user_region && cpumask_equal(mm_cpumask(mm), tmp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 local = 1;
1057
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +10001058#ifndef CONFIG_PPC_64K_PAGES
David Gibsona4fe3ce2009-10-26 19:24:31 +00001059 /* If we use 4K pages and our psize is not 4K, then we might
1060 * be hitting a special driver mapping, and need to align the
1061 * address before we fetch the PTE.
1062 *
1063 * It could also be a hugepage mapping, in which case this is
1064 * not necessary, but it's not harmful, either.
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +10001065 */
1066 if (psize != MMU_PAGE_4K)
1067 ea &= ~((1ul << mmu_psize_defs[psize].shift) - 1);
1068#endif /* CONFIG_PPC_64K_PAGES */
1069
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001070 /* Get PTE and page size from page tables */
David Gibsona4fe3ce2009-10-26 19:24:31 +00001071 ptep = find_linux_pte_or_hugepte(pgdir, ea, &hugeshift);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001072 if (ptep == NULL || !pte_present(*ptep)) {
1073 DBG_LOW(" no PTE !\n");
Li Zhongba12eed2013-05-13 16:16:41 +00001074 rc = 1;
1075 goto bail;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001076 }
1077
Benjamin Herrenschmidtca91e6c2010-07-23 08:53:23 +10001078 /* Add _PAGE_PRESENT to the required access perm */
1079 access |= _PAGE_PRESENT;
1080
1081 /* Pre-check access permissions (will be re-checked atomically
1082 * in __hash_page_XX but this pre-check is a fast path
1083 */
1084 if (access & ~pte_val(*ptep)) {
1085 DBG_LOW(" no access !\n");
Li Zhongba12eed2013-05-13 16:16:41 +00001086 rc = 1;
1087 goto bail;
Benjamin Herrenschmidtca91e6c2010-07-23 08:53:23 +10001088 }
1089
Li Zhongba12eed2013-05-13 16:16:41 +00001090 if (hugeshift) {
Aneesh Kumar K.V6d492ec2013-06-20 14:30:21 +05301091 if (pmd_trans_huge(*(pmd_t *)ptep))
1092 rc = __hash_page_thp(ea, access, vsid, (pmd_t *)ptep,
1093 trap, local, ssize, psize);
1094#ifdef CONFIG_HUGETLB_PAGE
1095 else
1096 rc = __hash_page_huge(ea, access, vsid, ptep, trap,
1097 local, ssize, hugeshift, psize);
1098#else
1099 else {
1100 /*
1101 * if we have hugeshift, and is not transhuge with
1102 * hugetlb disabled, something is really wrong.
1103 */
1104 rc = 1;
1105 WARN_ON(1);
1106 }
1107#endif
Michael Ellerman09567e72014-05-28 18:21:17 +10001108 check_paca_psize(ea, mm, psize, user_region);
1109
Li Zhongba12eed2013-05-13 16:16:41 +00001110 goto bail;
1111 }
David Gibsona4fe3ce2009-10-26 19:24:31 +00001112
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001113#ifndef CONFIG_PPC_64K_PAGES
1114 DBG_LOW(" i-pte: %016lx\n", pte_val(*ptep));
1115#else
1116 DBG_LOW(" i-pte: %016lx %016lx\n", pte_val(*ptep),
1117 pte_val(*(ptep + PTRS_PER_PTE)));
1118#endif
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001119 /* Do actual hashing */
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +10001120#ifdef CONFIG_PPC_64K_PAGES
Paul Mackerras721151d2007-04-03 21:24:02 +10001121 /* If _PAGE_4K_PFN is set, make sure this is a 4k segment */
Paul Mackerras3a8247c2008-06-18 15:29:12 +10001122 if ((pte_val(*ptep) & _PAGE_4K_PFN) && psize == MMU_PAGE_64K) {
Paul Mackerras721151d2007-04-03 21:24:02 +10001123 demote_segment_4k(mm, ea);
1124 psize = MMU_PAGE_4K;
1125 }
1126
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +10001127 /* If this PTE is non-cacheable and we have restrictions on
1128 * using non cacheable large pages, then we switch to 4k
1129 */
1130 if (mmu_ci_restrictions && psize == MMU_PAGE_64K &&
1131 (pte_val(*ptep) & _PAGE_NO_CACHE)) {
1132 if (user_region) {
1133 demote_segment_4k(mm, ea);
1134 psize = MMU_PAGE_4K;
1135 } else if (ea < VMALLOC_END) {
1136 /*
1137 * some driver did a non-cacheable mapping
1138 * in vmalloc space, so switch vmalloc
1139 * to 4k pages
1140 */
1141 printk(KERN_ALERT "Reducing vmalloc segment "
1142 "to 4kB pages because of "
1143 "non-cacheable mapping\n");
1144 psize = mmu_vmalloc_psize = MMU_PAGE_4K;
Geert Uytterhoeven1e57ba82007-07-17 02:35:38 +10001145#ifdef CONFIG_SPU_BASE
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +01001146 spu_flush_all_slbs(mm);
1147#endif
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +10001148 }
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +10001149 }
Michael Ellerman09567e72014-05-28 18:21:17 +10001150
1151 check_paca_psize(ea, mm, psize, user_region);
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +10001152#endif /* CONFIG_PPC_64K_PAGES */
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +10001153
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +10001154#ifdef CONFIG_PPC_HAS_HASH_64K
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +10001155 if (psize == MMU_PAGE_64K)
Paul Mackerras1189be62007-10-11 20:37:10 +10001156 rc = __hash_page_64K(ea, access, vsid, ptep, trap, local, ssize);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001157 else
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +10001158#endif /* CONFIG_PPC_HAS_HASH_64K */
Paul Mackerrasfa282372008-01-24 08:35:13 +11001159 {
David Gibsona1128f82009-12-16 14:29:56 +00001160 int spp = subpage_protection(mm, ea);
Paul Mackerrasfa282372008-01-24 08:35:13 +11001161 if (access & spp)
1162 rc = -2;
1163 else
1164 rc = __hash_page_4K(ea, access, vsid, ptep, trap,
1165 local, ssize, spp);
1166 }
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001167
Benjamin Herrenschmidt4b8692c2010-07-23 10:31:13 +10001168 /* Dump some info in case of hash insertion failure, they should
1169 * never happen so it is really useful to know if/when they do
1170 */
1171 if (rc == -1)
1172 hash_failure_debug(ea, access, vsid, trap, ssize, psize,
Aneesh Kumar K.Vd8139eb2013-04-28 09:37:37 +00001173 psize, pte_val(*ptep));
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001174#ifndef CONFIG_PPC_64K_PAGES
1175 DBG_LOW(" o-pte: %016lx\n", pte_val(*ptep));
1176#else
1177 DBG_LOW(" o-pte: %016lx %016lx\n", pte_val(*ptep),
1178 pte_val(*(ptep + PTRS_PER_PTE)));
1179#endif
1180 DBG_LOW(" -> rc=%d\n", rc);
Li Zhongba12eed2013-05-13 16:16:41 +00001181
1182bail:
1183 exception_exit(prev_state);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001184 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185}
Arnd Bergmann67207b92005-11-15 15:53:48 -05001186EXPORT_SYMBOL_GPL(hash_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001188void hash_preload(struct mm_struct *mm, unsigned long ea,
1189 unsigned long access, unsigned long trap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190{
Aneesh Kumar K.V12bc9f62013-06-20 14:30:18 +05301191 int hugepage_shift;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001192 unsigned long vsid;
Michael Neuling0b97fee2010-11-17 18:52:45 +00001193 pgd_t *pgdir;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001194 pte_t *ptep;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001195 unsigned long flags;
Benjamin Herrenschmidt4b8692c2010-07-23 10:31:13 +10001196 int rc, ssize, local = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +10001198 BUG_ON(REGION_ID(ea) != USER_REGION_ID);
1199
1200#ifdef CONFIG_PPC_MM_SLICES
1201 /* We only prefault standard pages for now */
Ilpo Järvinen2b02d132007-08-16 08:03:35 +10001202 if (unlikely(get_slice_psize(mm, ea) != mm->context.user_psize))
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001203 return;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +10001204#endif
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001205
1206 DBG_LOW("hash_preload(mm=%p, mm->pgdir=%p, ea=%016lx, access=%lx,"
1207 " trap=%lx\n", mm, mm->pgd, ea, access, trap);
1208
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +10001209 /* Get Linux PTE if available */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001210 pgdir = mm->pgd;
1211 if (pgdir == NULL)
1212 return;
Aneesh Kumar K.V0ac52dd2013-06-20 14:30:22 +05301213
1214 /* Get VSID */
1215 ssize = user_segment_size(ea);
1216 vsid = get_vsid(mm->context.id, ea, ssize);
1217 if (!vsid)
1218 return;
1219 /*
1220 * Hash doesn't like irqs. Walking linux page table with irq disabled
1221 * saves us from holding multiple locks.
1222 */
1223 local_irq_save(flags);
1224
Aneesh Kumar K.V12bc9f62013-06-20 14:30:18 +05301225 /*
1226 * THP pages use update_mmu_cache_pmd. We don't do
1227 * hash preload there. Hence can ignore THP here
1228 */
1229 ptep = find_linux_pte_or_hugepte(pgdir, ea, &hugepage_shift);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001230 if (!ptep)
Aneesh Kumar K.V0ac52dd2013-06-20 14:30:22 +05301231 goto out_exit;
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +10001232
Aneesh Kumar K.V12bc9f62013-06-20 14:30:18 +05301233 WARN_ON(hugepage_shift);
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +10001234#ifdef CONFIG_PPC_64K_PAGES
1235 /* If either _PAGE_4K_PFN or _PAGE_NO_CACHE is set (and we are on
1236 * a 64K kernel), then we don't preload, hash_page() will take
1237 * care of it once we actually try to access the page.
1238 * That way we don't have to duplicate all of the logic for segment
1239 * page size demotion here
1240 */
1241 if (pte_val(*ptep) & (_PAGE_4K_PFN | _PAGE_NO_CACHE))
Aneesh Kumar K.V0ac52dd2013-06-20 14:30:22 +05301242 goto out_exit;
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +10001243#endif /* CONFIG_PPC_64K_PAGES */
1244
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +10001245 /* Is that local to this CPU ? */
Rusty Russell56aa4122009-03-15 18:16:43 +00001246 if (cpumask_equal(mm_cpumask(mm), cpumask_of(smp_processor_id())))
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001247 local = 1;
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +10001248
1249 /* Hash it in */
1250#ifdef CONFIG_PPC_HAS_HASH_64K
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +10001251 if (mm->context.user_psize == MMU_PAGE_64K)
Benjamin Herrenschmidt4b8692c2010-07-23 10:31:13 +10001252 rc = __hash_page_64K(ea, access, vsid, ptep, trap, local, ssize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 else
Jon Tollefson5b825832007-05-17 04:43:02 +10001254#endif /* CONFIG_PPC_HAS_HASH_64K */
Benjamin Herrenschmidt4b8692c2010-07-23 10:31:13 +10001255 rc = __hash_page_4K(ea, access, vsid, ptep, trap, local, ssize,
Michael Neuling1c2c25c2010-11-17 16:32:59 +00001256 subpage_protection(mm, ea));
Benjamin Herrenschmidt4b8692c2010-07-23 10:31:13 +10001257
1258 /* Dump some info in case of hash insertion failure, they should
1259 * never happen so it is really useful to know if/when they do
1260 */
1261 if (rc == -1)
1262 hash_failure_debug(ea, access, vsid, trap, ssize,
Aneesh Kumar K.Vd8139eb2013-04-28 09:37:37 +00001263 mm->context.user_psize,
1264 mm->context.user_psize,
1265 pte_val(*ptep));
Aneesh Kumar K.V0ac52dd2013-06-20 14:30:22 +05301266out_exit:
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001267 local_irq_restore(flags);
1268}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
Benjamin Herrenschmidtf6ab0b92007-10-29 12:05:18 +11001270/* WARNING: This is called from hash_low_64.S, if you change this prototype,
1271 * do not forget to update the assembly call site !
1272 */
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +00001273void flush_hash_page(unsigned long vpn, real_pte_t pte, int psize, int ssize,
Paul Mackerras1189be62007-10-11 20:37:10 +10001274 int local)
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001275{
1276 unsigned long hash, index, shift, hidx, slot;
1277
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +00001278 DBG_LOW("flush_hash_page(vpn=%016lx)\n", vpn);
1279 pte_iterate_hashed_subpages(pte, psize, vpn, index, shift) {
1280 hash = hpt_hash(vpn, shift, ssize);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001281 hidx = __rpte_to_hidx(pte, index);
1282 if (hidx & _PTEIDX_SECONDARY)
1283 hash = ~hash;
1284 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
1285 slot += hidx & _PTEIDX_GROUP_IX;
Sachin P. Sant5c339912009-12-13 21:15:12 +00001286 DBG_LOW(" sub %ld: hash=%lx, hidx=%lx\n", index, slot, hidx);
Aneesh Kumar K.Vdb3d8532013-06-20 14:30:13 +05301287 /*
1288 * We use same base page size and actual psize, because we don't
1289 * use these functions for hugepage
1290 */
1291 ppc_md.hpte_invalidate(slot, vpn, psize, psize, ssize, local);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001292 } pte_iterate_hashed_end();
Michael Neulingbc2a9402013-02-13 16:21:40 +00001293
1294#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1295 /* Transactions are not aborted by tlbiel, only tlbie.
1296 * Without, syncing a page back to a block device w/ PIO could pick up
1297 * transactional data (bad!) so we force an abort here. Before the
1298 * sync the page will be made read-only, which will flush_hash_page.
1299 * BIG ISSUE here: if the kernel uses a page from userspace without
1300 * unmapping it first, it may see the speculated version.
1301 */
1302 if (local && cpu_has_feature(CPU_FTR_TM) &&
Michael Neulingc2fd22d2013-05-02 15:36:14 +00001303 current->thread.regs &&
Michael Neulingbc2a9402013-02-13 16:21:40 +00001304 MSR_TM_ACTIVE(current->thread.regs->msr)) {
1305 tm_enable();
1306 tm_abort(TM_CAUSE_TLBI);
1307 }
1308#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309}
1310
Benjamin Herrenschmidt61b1a942005-09-20 13:52:50 +10001311void flush_hash_range(unsigned long number, int local)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312{
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001313 if (ppc_md.flush_hash_range)
Benjamin Herrenschmidt61b1a942005-09-20 13:52:50 +10001314 ppc_md.flush_hash_range(number, local);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001315 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 int i;
Benjamin Herrenschmidt61b1a942005-09-20 13:52:50 +10001317 struct ppc64_tlb_batch *batch =
1318 &__get_cpu_var(ppc64_tlb_batch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
1320 for (i = 0; i < number; i++)
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +00001321 flush_hash_page(batch->vpn[i], batch->pte[i],
Paul Mackerras1189be62007-10-11 20:37:10 +10001322 batch->psize, batch->ssize, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 }
1324}
1325
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326/*
1327 * low_hash_fault is called when we the low level hash code failed
1328 * to instert a PTE due to an hypervisor error
1329 */
Paul Mackerrasfa282372008-01-24 08:35:13 +11001330void low_hash_fault(struct pt_regs *regs, unsigned long address, int rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331{
Li Zhongba12eed2013-05-13 16:16:41 +00001332 enum ctx_state prev_state = exception_enter();
1333
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 if (user_mode(regs)) {
Paul Mackerrasfa282372008-01-24 08:35:13 +11001335#ifdef CONFIG_PPC_SUBPAGE_PROT
1336 if (rc == -2)
1337 _exception(SIGSEGV, regs, SEGV_ACCERR, address);
1338 else
1339#endif
1340 _exception(SIGBUS, regs, BUS_ADRERR, address);
1341 } else
1342 bad_page_fault(regs, address, SIGBUS);
Li Zhongba12eed2013-05-13 16:16:41 +00001343
1344 exception_exit(prev_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345}
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +10001346
Li Zhongb170bd32013-04-15 16:53:19 +00001347long hpte_insert_repeating(unsigned long hash, unsigned long vpn,
1348 unsigned long pa, unsigned long rflags,
1349 unsigned long vflags, int psize, int ssize)
1350{
1351 unsigned long hpte_group;
1352 long slot;
1353
1354repeat:
1355 hpte_group = ((hash & htab_hash_mask) *
1356 HPTES_PER_GROUP) & ~0x7UL;
1357
1358 /* Insert into the hash table, primary slot */
1359 slot = ppc_md.hpte_insert(hpte_group, vpn, pa, rflags, vflags,
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +00001360 psize, psize, ssize);
Li Zhongb170bd32013-04-15 16:53:19 +00001361
1362 /* Primary is full, try the secondary */
1363 if (unlikely(slot == -1)) {
1364 hpte_group = ((~hash & htab_hash_mask) *
1365 HPTES_PER_GROUP) & ~0x7UL;
1366 slot = ppc_md.hpte_insert(hpte_group, vpn, pa, rflags,
1367 vflags | HPTE_V_SECONDARY,
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +00001368 psize, psize, ssize);
Li Zhongb170bd32013-04-15 16:53:19 +00001369 if (slot == -1) {
1370 if (mftb() & 0x1)
1371 hpte_group = ((hash & htab_hash_mask) *
1372 HPTES_PER_GROUP)&~0x7UL;
1373
1374 ppc_md.hpte_remove(hpte_group);
1375 goto repeat;
1376 }
1377 }
1378
1379 return slot;
1380}
1381
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +10001382#ifdef CONFIG_DEBUG_PAGEALLOC
1383static void kernel_map_linear_page(unsigned long vaddr, unsigned long lmi)
1384{
Li Zhong016af592013-04-15 16:53:20 +00001385 unsigned long hash;
Paul Mackerras1189be62007-10-11 20:37:10 +10001386 unsigned long vsid = get_kernel_vsid(vaddr, mmu_kernel_ssize);
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +00001387 unsigned long vpn = hpt_vpn(vaddr, vsid, mmu_kernel_ssize);
Benjamin Herrenschmidtbc033b62008-08-05 16:19:56 +10001388 unsigned long mode = htab_convert_pte_flags(PAGE_KERNEL);
Li Zhong016af592013-04-15 16:53:20 +00001389 long ret;
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +10001390
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +00001391 hash = hpt_hash(vpn, PAGE_SHIFT, mmu_kernel_ssize);
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +10001392
Aneesh Kumar K.Vc60ac562013-03-13 03:34:54 +00001393 /* Don't create HPTE entries for bad address */
1394 if (!vsid)
1395 return;
Li Zhong016af592013-04-15 16:53:20 +00001396
1397 ret = hpte_insert_repeating(hash, vpn, __pa(vaddr), mode,
1398 HPTE_V_BOLTED,
1399 mmu_linear_psize, mmu_kernel_ssize);
1400
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +10001401 BUG_ON (ret < 0);
1402 spin_lock(&linear_map_hash_lock);
1403 BUG_ON(linear_map_hash_slots[lmi] & 0x80);
1404 linear_map_hash_slots[lmi] = ret | 0x80;
1405 spin_unlock(&linear_map_hash_lock);
1406}
1407
1408static void kernel_unmap_linear_page(unsigned long vaddr, unsigned long lmi)
1409{
Paul Mackerras1189be62007-10-11 20:37:10 +10001410 unsigned long hash, hidx, slot;
1411 unsigned long vsid = get_kernel_vsid(vaddr, mmu_kernel_ssize);
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +00001412 unsigned long vpn = hpt_vpn(vaddr, vsid, mmu_kernel_ssize);
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +10001413
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +00001414 hash = hpt_hash(vpn, PAGE_SHIFT, mmu_kernel_ssize);
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +10001415 spin_lock(&linear_map_hash_lock);
1416 BUG_ON(!(linear_map_hash_slots[lmi] & 0x80));
1417 hidx = linear_map_hash_slots[lmi] & 0x7f;
1418 linear_map_hash_slots[lmi] = 0;
1419 spin_unlock(&linear_map_hash_lock);
1420 if (hidx & _PTEIDX_SECONDARY)
1421 hash = ~hash;
1422 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
1423 slot += hidx & _PTEIDX_GROUP_IX;
Aneesh Kumar K.Vdb3d8532013-06-20 14:30:13 +05301424 ppc_md.hpte_invalidate(slot, vpn, mmu_linear_psize, mmu_linear_psize,
1425 mmu_kernel_ssize, 0);
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +10001426}
1427
1428void kernel_map_pages(struct page *page, int numpages, int enable)
1429{
1430 unsigned long flags, vaddr, lmi;
1431 int i;
1432
1433 local_irq_save(flags);
1434 for (i = 0; i < numpages; i++, page++) {
1435 vaddr = (unsigned long)page_address(page);
1436 lmi = __pa(vaddr) >> PAGE_SHIFT;
1437 if (lmi >= linear_map_hash_count)
1438 continue;
1439 if (enable)
1440 kernel_map_linear_page(vaddr, lmi);
1441 else
1442 kernel_unmap_linear_page(vaddr, lmi);
1443 }
1444 local_irq_restore(flags);
1445}
1446#endif /* CONFIG_DEBUG_PAGEALLOC */
Benjamin Herrenschmidtcd3db0c2010-07-06 15:39:02 -07001447
1448void setup_initial_memory_limit(phys_addr_t first_memblock_base,
1449 phys_addr_t first_memblock_size)
1450{
1451 /* We don't currently support the first MEMBLOCK not mapping 0
1452 * physical on those processors
1453 */
1454 BUG_ON(first_memblock_base != 0);
1455
1456 /* On LPAR systems, the first entry is our RMA region,
1457 * non-LPAR 64-bit hash MMU systems don't have a limitation
1458 * on real mode access, but using the first entry works well
1459 * enough. We also clamp it to 1G to avoid some funky things
1460 * such as RTAS bugs etc...
1461 */
1462 ppc64_rma_size = min_t(u64, first_memblock_size, 0x40000000);
1463
1464 /* Finally limit subsequent allocations */
1465 memblock_set_current_limit(ppc64_rma_size);
1466}