blob: 1628201c8ceaf16e60d7bea03d14e1813f2cbc2b [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>
30#include <linux/ctype.h>
31#include <linux/cache.h>
32#include <linux/init.h>
33#include <linux/signal.h>
Yinghai Lu95f72d12010-07-12 14:36:09 +100034#include <linux/memblock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <asm/processor.h>
37#include <asm/pgtable.h>
38#include <asm/mmu.h>
39#include <asm/mmu_context.h>
40#include <asm/page.h>
41#include <asm/types.h>
42#include <asm/system.h>
43#include <asm/uaccess.h>
44#include <asm/machdep.h>
David S. Millerd9b2b2a2008-02-13 16:56:49 -080045#include <asm/prom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <asm/abs_addr.h>
47#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58#ifdef DEBUG
59#define DBG(fmt...) udbg_printf(fmt)
60#else
61#define DBG(fmt...)
62#endif
63
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +110064#ifdef DEBUG_LOW
65#define DBG_LOW(fmt...) udbg_printf(fmt)
66#else
67#define DBG_LOW(fmt...)
68#endif
69
70#define KB (1024)
71#define MB (1024*KB)
Jon Tollefson658013e2008-07-23 21:27:54 -070072#define GB (1024L*MB)
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +110073
Linus Torvalds1da177e2005-04-16 15:20:36 -070074/*
75 * Note: pte --> Linux PTE
76 * HPTE --> PowerPC Hashed Page Table Entry
77 *
78 * Execution context:
79 * htab_initialize is called with the MMU off (of course), but
80 * the kernel has been copied down to zero so it can directly
81 * reference global data. At this point it is very difficult
82 * to print debug info.
83 *
84 */
85
86#ifdef CONFIG_U3_DART
87extern unsigned long dart_tablebase;
88#endif /* CONFIG_U3_DART */
89
Paul Mackerras799d6042005-11-10 13:37:51 +110090static unsigned long _SDR1;
91struct mmu_psize_def mmu_psize_defs[MMU_PAGE_COUNT];
92
David Gibson8e561e72007-06-13 14:52:56 +100093struct hash_pte *htab_address;
Michael Ellerman337a7122006-02-21 17:22:55 +110094unsigned long htab_size_bytes;
David Gibson96e28442005-07-13 01:11:42 -070095unsigned long htab_hash_mask;
Alexander Graf4ab79aa2009-10-30 05:47:19 +000096EXPORT_SYMBOL_GPL(htab_hash_mask);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +110097int mmu_linear_psize = MMU_PAGE_4K;
98int mmu_virtual_psize = MMU_PAGE_4K;
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +100099int mmu_vmalloc_psize = MMU_PAGE_4K;
Benjamin Herrenschmidtcec08e72008-04-30 15:41:48 +1000100#ifdef CONFIG_SPARSEMEM_VMEMMAP
101int mmu_vmemmap_psize = MMU_PAGE_4K;
102#endif
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000103int mmu_io_psize = MMU_PAGE_4K;
Paul Mackerras1189be62007-10-11 20:37:10 +1000104int mmu_kernel_ssize = MMU_SEGSIZE_256M;
105int mmu_highuser_ssize = MMU_SEGSIZE_256M;
Michael Neuling584f8b72007-12-06 17:24:48 +1100106u16 mmu_slb_size = 64;
Alexander Graf4ab79aa2009-10-30 05:47:19 +0000107EXPORT_SYMBOL_GPL(mmu_slb_size);
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000108#ifdef CONFIG_PPC_64K_PAGES
109int mmu_ci_restrictions;
110#endif
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000111#ifdef CONFIG_DEBUG_PAGEALLOC
112static u8 *linear_map_hash_slots;
113static unsigned long linear_map_hash_count;
Michael Ellermaned166692007-04-18 11:50:09 +1000114static DEFINE_SPINLOCK(linear_map_hash_lock);
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000115#endif /* CONFIG_DEBUG_PAGEALLOC */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100117/* There are definitions of page sizes arrays to be used when none
118 * is provided by the firmware.
119 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100121/* Pre-POWER4 CPUs (4k pages only)
122 */
Michael Ellerman09de9ff2008-05-08 14:27:07 +1000123static struct mmu_psize_def mmu_psize_defaults_old[] = {
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100124 [MMU_PAGE_4K] = {
125 .shift = 12,
126 .sllp = 0,
127 .penc = 0,
128 .avpnm = 0,
129 .tlbiel = 0,
130 },
131};
132
133/* POWER4, GPUL, POWER5
134 *
135 * Support for 16Mb large pages
136 */
Michael Ellerman09de9ff2008-05-08 14:27:07 +1000137static struct mmu_psize_def mmu_psize_defaults_gp[] = {
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100138 [MMU_PAGE_4K] = {
139 .shift = 12,
140 .sllp = 0,
141 .penc = 0,
142 .avpnm = 0,
143 .tlbiel = 1,
144 },
145 [MMU_PAGE_16M] = {
146 .shift = 24,
147 .sllp = SLB_VSID_L,
148 .penc = 0,
149 .avpnm = 0x1UL,
150 .tlbiel = 0,
151 },
152};
153
Benjamin Herrenschmidtbc033b62008-08-05 16:19:56 +1000154static unsigned long htab_convert_pte_flags(unsigned long pteflags)
155{
156 unsigned long rflags = pteflags & 0x1fa;
157
158 /* _PAGE_EXEC -> NOEXEC */
159 if ((pteflags & _PAGE_EXEC) == 0)
160 rflags |= HPTE_R_N;
161
162 /* PP bits. PAGE_USER is already PP bit 0x2, so we only
163 * need to add in 0x1 if it's a read-only user page
164 */
165 if ((pteflags & _PAGE_USER) && !((pteflags & _PAGE_RW) &&
166 (pteflags & _PAGE_DIRTY)))
167 rflags |= 1;
168
169 /* Always add C */
170 return rflags | HPTE_R_C;
171}
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100172
173int htab_bolt_mapping(unsigned long vstart, unsigned long vend,
Benjamin Herrenschmidtbc033b62008-08-05 16:19:56 +1000174 unsigned long pstart, unsigned long prot,
Paul Mackerras1189be62007-10-11 20:37:10 +1000175 int psize, int ssize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100177 unsigned long vaddr, paddr;
178 unsigned int step, shift;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100179 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100181 shift = mmu_psize_defs[psize].shift;
182 step = 1 << shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Benjamin Herrenschmidtbc033b62008-08-05 16:19:56 +1000184 prot = htab_convert_pte_flags(prot);
185
186 DBG("htab_bolt_mapping(%lx..%lx -> %lx (%lx,%d,%d)\n",
187 vstart, vend, pstart, prot, psize, ssize);
188
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100189 for (vaddr = vstart, paddr = pstart; vaddr < vend;
190 vaddr += step, paddr += step) {
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000191 unsigned long hash, hpteg;
Paul Mackerras1189be62007-10-11 20:37:10 +1000192 unsigned long vsid = get_kernel_vsid(vaddr, ssize);
193 unsigned long va = hpt_va(vaddr, vsid, ssize);
Paul Mackerras9e88ba42008-08-30 11:26:27 +1000194 unsigned long tprot = prot;
195
196 /* Make kernel text executable */
Paul Mackerras549e8152008-08-30 11:43:47 +1000197 if (overlaps_kernel_text(vaddr, vaddr + step))
Paul Mackerras9e88ba42008-08-30 11:26:27 +1000198 tprot &= ~HPTE_R_N;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Paul Mackerras1189be62007-10-11 20:37:10 +1000200 hash = hpt_hash(va, shift, ssize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
202
Michael Ellermanc30a4df2006-06-23 18:16:39 +1000203 BUG_ON(!ppc_md.hpte_insert);
Paul Mackerras9e88ba42008-08-30 11:26:27 +1000204 ret = ppc_md.hpte_insert(hpteg, va, paddr, tprot,
Benjamin Herrenschmidtbc033b62008-08-05 16:19:56 +1000205 HPTE_V_BOLTED, psize, ssize);
Michael Ellermanc30a4df2006-06-23 18:16:39 +1000206
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100207 if (ret < 0)
208 break;
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000209#ifdef CONFIG_DEBUG_PAGEALLOC
210 if ((paddr >> PAGE_SHIFT) < linear_map_hash_count)
211 linear_map_hash_slots[paddr >> PAGE_SHIFT] = ret | 0x80;
212#endif /* CONFIG_DEBUG_PAGEALLOC */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 }
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100214 return ret < 0 ? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215}
216
Stephen Rothwellae86f002008-03-27 16:08:57 +1100217#ifdef CONFIG_MEMORY_HOTPLUG
Badari Pulavarty52db9b42008-03-28 11:37:21 +1100218static int htab_remove_mapping(unsigned long vstart, unsigned long vend,
Badari Pulavartyf8c88032008-01-29 09:19:24 +1100219 int psize, int ssize)
220{
221 unsigned long vaddr;
222 unsigned int step, shift;
223
224 shift = mmu_psize_defs[psize].shift;
225 step = 1 << shift;
226
227 if (!ppc_md.hpte_removebolted) {
Badari Pulavarty52db9b42008-03-28 11:37:21 +1100228 printk(KERN_WARNING "Platform doesn't implement "
229 "hpte_removebolted\n");
230 return -EINVAL;
Badari Pulavartyf8c88032008-01-29 09:19:24 +1100231 }
232
233 for (vaddr = vstart; vaddr < vend; vaddr += step)
234 ppc_md.hpte_removebolted(vaddr, psize, ssize);
Badari Pulavarty52db9b42008-03-28 11:37:21 +1100235
236 return 0;
Badari Pulavartyf8c88032008-01-29 09:19:24 +1100237}
Stephen Rothwellae86f002008-03-27 16:08:57 +1100238#endif /* CONFIG_MEMORY_HOTPLUG */
Badari Pulavartyf8c88032008-01-29 09:19:24 +1100239
Paul Mackerras1189be62007-10-11 20:37:10 +1000240static int __init htab_dt_scan_seg_sizes(unsigned long node,
241 const char *uname, int depth,
242 void *data)
243{
244 char *type = of_get_flat_dt_prop(node, "device_type", NULL);
245 u32 *prop;
246 unsigned long size = 0;
247
248 /* We are scanning "cpu" nodes only */
249 if (type == NULL || strcmp(type, "cpu") != 0)
250 return 0;
251
252 prop = (u32 *)of_get_flat_dt_prop(node, "ibm,processor-segment-sizes",
253 &size);
254 if (prop == NULL)
255 return 0;
256 for (; size >= 4; size -= 4, ++prop) {
257 if (prop[0] == 40) {
258 DBG("1T segment support detected\n");
Matt Evans44ae3ab2011-04-06 19:48:50 +0000259 cur_cpu_spec->mmu_features |= MMU_FTR_1T_SEGMENT;
Olof Johanssonf5534002007-10-12 16:44:55 +1000260 return 1;
Paul Mackerras1189be62007-10-11 20:37:10 +1000261 }
Paul Mackerras1189be62007-10-11 20:37:10 +1000262 }
Matt Evans44ae3ab2011-04-06 19:48:50 +0000263 cur_cpu_spec->mmu_features &= ~MMU_FTR_NO_SLBIE_B;
Paul Mackerras1189be62007-10-11 20:37:10 +1000264 return 0;
265}
266
267static void __init htab_init_seg_sizes(void)
268{
269 of_scan_flat_dt(htab_dt_scan_seg_sizes, NULL);
270}
271
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100272static int __init htab_dt_scan_page_sizes(unsigned long node,
273 const char *uname, int depth,
274 void *data)
275{
276 char *type = of_get_flat_dt_prop(node, "device_type", NULL);
277 u32 *prop;
278 unsigned long size = 0;
279
280 /* We are scanning "cpu" nodes only */
281 if (type == NULL || strcmp(type, "cpu") != 0)
282 return 0;
283
284 prop = (u32 *)of_get_flat_dt_prop(node,
285 "ibm,segment-page-sizes", &size);
286 if (prop != NULL) {
287 DBG("Page sizes from device-tree:\n");
288 size /= 4;
Matt Evans44ae3ab2011-04-06 19:48:50 +0000289 cur_cpu_spec->mmu_features &= ~(MMU_FTR_16M_PAGE);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100290 while(size > 0) {
291 unsigned int shift = prop[0];
292 unsigned int slbenc = prop[1];
293 unsigned int lpnum = prop[2];
294 unsigned int lpenc = 0;
295 struct mmu_psize_def *def;
296 int idx = -1;
297
298 size -= 3; prop += 3;
299 while(size > 0 && lpnum) {
300 if (prop[0] == shift)
301 lpenc = prop[1];
302 prop += 2; size -= 2;
303 lpnum--;
304 }
305 switch(shift) {
306 case 0xc:
307 idx = MMU_PAGE_4K;
308 break;
309 case 0x10:
310 idx = MMU_PAGE_64K;
311 break;
312 case 0x14:
313 idx = MMU_PAGE_1M;
314 break;
315 case 0x18:
316 idx = MMU_PAGE_16M;
Matt Evans44ae3ab2011-04-06 19:48:50 +0000317 cur_cpu_spec->mmu_features |= MMU_FTR_16M_PAGE;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100318 break;
319 case 0x22:
320 idx = MMU_PAGE_16G;
321 break;
322 }
323 if (idx < 0)
324 continue;
325 def = &mmu_psize_defs[idx];
326 def->shift = shift;
327 if (shift <= 23)
328 def->avpnm = 0;
329 else
330 def->avpnm = (1 << (shift - 23)) - 1;
331 def->sllp = slbenc;
332 def->penc = lpenc;
333 /* We don't know for sure what's up with tlbiel, so
334 * for now we only set it for 4K and 64K pages
335 */
336 if (idx == MMU_PAGE_4K || idx == MMU_PAGE_64K)
337 def->tlbiel = 1;
338 else
339 def->tlbiel = 0;
340
Sachin P. Sant5c339912009-12-13 21:15:12 +0000341 DBG(" %d: shift=%02x, sllp=%04lx, avpnm=%08lx, "
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100342 "tlbiel=%d, penc=%d\n",
343 idx, shift, def->sllp, def->avpnm, def->tlbiel,
344 def->penc);
345 }
346 return 1;
347 }
348 return 0;
349}
350
Tony Breedse16a9c02008-07-31 13:51:42 +1000351#ifdef CONFIG_HUGETLB_PAGE
Jon Tollefson658013e2008-07-23 21:27:54 -0700352/* Scan for 16G memory blocks that have been set aside for huge pages
353 * and reserve those blocks for 16G huge pages.
354 */
355static int __init htab_dt_scan_hugepage_blocks(unsigned long node,
356 const char *uname, int depth,
357 void *data) {
358 char *type = of_get_flat_dt_prop(node, "device_type", NULL);
359 unsigned long *addr_prop;
360 u32 *page_count_prop;
361 unsigned int expected_pages;
362 long unsigned int phys_addr;
363 long unsigned int block_size;
364
365 /* We are scanning "memory" nodes only */
366 if (type == NULL || strcmp(type, "memory") != 0)
367 return 0;
368
369 /* This property is the log base 2 of the number of virtual pages that
370 * will represent this memory block. */
371 page_count_prop = of_get_flat_dt_prop(node, "ibm,expected#pages", NULL);
372 if (page_count_prop == NULL)
373 return 0;
374 expected_pages = (1 << page_count_prop[0]);
375 addr_prop = of_get_flat_dt_prop(node, "reg", NULL);
376 if (addr_prop == NULL)
377 return 0;
378 phys_addr = addr_prop[0];
379 block_size = addr_prop[1];
380 if (block_size != (16 * GB))
381 return 0;
382 printk(KERN_INFO "Huge page(16GB) memory: "
383 "addr = 0x%lX size = 0x%lX pages = %d\n",
384 phys_addr, block_size, expected_pages);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000385 if (phys_addr + (16 * GB) <= memblock_end_of_DRAM()) {
386 memblock_reserve(phys_addr, block_size * expected_pages);
Jon Tollefson4792adb2008-10-21 15:27:36 +0000387 add_gpage(phys_addr, block_size, expected_pages);
388 }
Jon Tollefson658013e2008-07-23 21:27:54 -0700389 return 0;
390}
Tony Breedse16a9c02008-07-31 13:51:42 +1000391#endif /* CONFIG_HUGETLB_PAGE */
Jon Tollefson658013e2008-07-23 21:27:54 -0700392
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100393static void __init htab_init_page_sizes(void)
394{
395 int rc;
396
397 /* Default to 4K pages only */
398 memcpy(mmu_psize_defs, mmu_psize_defaults_old,
399 sizeof(mmu_psize_defaults_old));
400
401 /*
402 * Try to find the available page sizes in the device-tree
403 */
404 rc = of_scan_flat_dt(htab_dt_scan_page_sizes, NULL);
405 if (rc != 0) /* Found */
406 goto found;
407
408 /*
409 * Not in the device-tree, let's fallback on known size
410 * list for 16M capable GP & GR
411 */
Matt Evans44ae3ab2011-04-06 19:48:50 +0000412 if (mmu_has_feature(MMU_FTR_16M_PAGE))
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100413 memcpy(mmu_psize_defs, mmu_psize_defaults_gp,
414 sizeof(mmu_psize_defaults_gp));
415 found:
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000416#ifndef CONFIG_DEBUG_PAGEALLOC
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100417 /*
418 * Pick a size for the linear mapping. Currently, we only support
419 * 16M, 1M and 4K which is the default
420 */
421 if (mmu_psize_defs[MMU_PAGE_16M].shift)
422 mmu_linear_psize = MMU_PAGE_16M;
423 else if (mmu_psize_defs[MMU_PAGE_1M].shift)
424 mmu_linear_psize = MMU_PAGE_1M;
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000425#endif /* CONFIG_DEBUG_PAGEALLOC */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100426
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000427#ifdef CONFIG_PPC_64K_PAGES
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100428 /*
429 * Pick a size for the ordinary pages. Default is 4K, we support
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000430 * 64K for user mappings and vmalloc if supported by the processor.
431 * We only use 64k for ioremap if the processor
432 * (and firmware) support cache-inhibited large pages.
433 * If not, we use 4k and set mmu_ci_restrictions so that
434 * hash_page knows to switch processes that use cache-inhibited
435 * mappings to 4k pages.
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100436 */
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000437 if (mmu_psize_defs[MMU_PAGE_64K].shift) {
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100438 mmu_virtual_psize = MMU_PAGE_64K;
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000439 mmu_vmalloc_psize = MMU_PAGE_64K;
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000440 if (mmu_linear_psize == MMU_PAGE_4K)
441 mmu_linear_psize = MMU_PAGE_64K;
Matt Evans44ae3ab2011-04-06 19:48:50 +0000442 if (mmu_has_feature(MMU_FTR_CI_LARGE_PAGE)) {
Paul Mackerrascfe666b2008-03-24 17:41:22 +1100443 /*
444 * Don't use 64k pages for ioremap on pSeries, since
445 * that would stop us accessing the HEA ethernet.
446 */
447 if (!machine_is(pseries))
448 mmu_io_psize = MMU_PAGE_64K;
449 } else
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000450 mmu_ci_restrictions = 1;
451 }
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000452#endif /* CONFIG_PPC_64K_PAGES */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100453
Benjamin Herrenschmidtcec08e72008-04-30 15:41:48 +1000454#ifdef CONFIG_SPARSEMEM_VMEMMAP
455 /* We try to use 16M pages for vmemmap if that is supported
456 * and we have at least 1G of RAM at boot
457 */
458 if (mmu_psize_defs[MMU_PAGE_16M].shift &&
Yinghai Lu95f72d12010-07-12 14:36:09 +1000459 memblock_phys_mem_size() >= 0x40000000)
Benjamin Herrenschmidtcec08e72008-04-30 15:41:48 +1000460 mmu_vmemmap_psize = MMU_PAGE_16M;
461 else if (mmu_psize_defs[MMU_PAGE_64K].shift)
462 mmu_vmemmap_psize = MMU_PAGE_64K;
463 else
464 mmu_vmemmap_psize = MMU_PAGE_4K;
465#endif /* CONFIG_SPARSEMEM_VMEMMAP */
466
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000467 printk(KERN_DEBUG "Page orders: linear mapping = %d, "
Benjamin Herrenschmidtcec08e72008-04-30 15:41:48 +1000468 "virtual = %d, io = %d"
469#ifdef CONFIG_SPARSEMEM_VMEMMAP
470 ", vmemmap = %d"
471#endif
472 "\n",
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100473 mmu_psize_defs[mmu_linear_psize].shift,
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000474 mmu_psize_defs[mmu_virtual_psize].shift,
Benjamin Herrenschmidtcec08e72008-04-30 15:41:48 +1000475 mmu_psize_defs[mmu_io_psize].shift
476#ifdef CONFIG_SPARSEMEM_VMEMMAP
477 ,mmu_psize_defs[mmu_vmemmap_psize].shift
478#endif
479 );
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100480
481#ifdef CONFIG_HUGETLB_PAGE
Jon Tollefson658013e2008-07-23 21:27:54 -0700482 /* Reserve 16G huge page memory sections for huge pages */
483 of_scan_flat_dt(htab_dt_scan_hugepage_blocks, NULL);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100484#endif /* CONFIG_HUGETLB_PAGE */
485}
486
487static int __init htab_dt_scan_pftsize(unsigned long node,
488 const char *uname, int depth,
489 void *data)
490{
491 char *type = of_get_flat_dt_prop(node, "device_type", NULL);
492 u32 *prop;
493
494 /* We are scanning "cpu" nodes only */
495 if (type == NULL || strcmp(type, "cpu") != 0)
496 return 0;
497
498 prop = (u32 *)of_get_flat_dt_prop(node, "ibm,pft-size", NULL);
499 if (prop != NULL) {
500 /* pft_size[0] is the NUMA CEC cookie */
501 ppc64_pft_size = prop[1];
502 return 1;
503 }
504 return 0;
505}
506
507static unsigned long __init htab_get_table_size(void)
Paul Mackerras3eac8c62005-10-12 16:58:53 +1000508{
Anton Blanchard13870b62009-02-13 11:57:30 +0000509 unsigned long mem_size, rnd_mem_size, pteg_count, psize;
Paul Mackerras3eac8c62005-10-12 16:58:53 +1000510
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100511 /* If hash size isn't already provided by the platform, we try to
Adrian Bunk943ffb52006-01-10 00:10:13 +0100512 * retrieve it from the device-tree. If it's not there neither, we
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100513 * calculate it now based on the total RAM size
Paul Mackerras3eac8c62005-10-12 16:58:53 +1000514 */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100515 if (ppc64_pft_size == 0)
516 of_scan_flat_dt(htab_dt_scan_pftsize, NULL);
Paul Mackerras3eac8c62005-10-12 16:58:53 +1000517 if (ppc64_pft_size)
518 return 1UL << ppc64_pft_size;
519
520 /* round mem_size up to next power of 2 */
Yinghai Lu95f72d12010-07-12 14:36:09 +1000521 mem_size = memblock_phys_mem_size();
Paul Mackerras799d6042005-11-10 13:37:51 +1100522 rnd_mem_size = 1UL << __ilog2(mem_size);
523 if (rnd_mem_size < mem_size)
Paul Mackerras3eac8c62005-10-12 16:58:53 +1000524 rnd_mem_size <<= 1;
525
526 /* # pages / 2 */
Anton Blanchard13870b62009-02-13 11:57:30 +0000527 psize = mmu_psize_defs[mmu_virtual_psize].shift;
528 pteg_count = max(rnd_mem_size >> (psize + 1), 1UL << 11);
Paul Mackerras3eac8c62005-10-12 16:58:53 +1000529
530 return pteg_count << 7;
531}
532
Mike Kravetz54b79242005-11-07 16:25:48 -0800533#ifdef CONFIG_MEMORY_HOTPLUG
Anton Blancharda1194092011-08-10 20:44:24 +0000534int create_section_mapping(unsigned long start, unsigned long end)
Mike Kravetz54b79242005-11-07 16:25:48 -0800535{
Anton Blancharda1194092011-08-10 20:44:24 +0000536 return htab_bolt_mapping(start, end, __pa(start),
David Gibsonf5ea64d2008-10-12 17:54:24 +0000537 pgprot_val(PAGE_KERNEL), mmu_linear_psize,
Anton Blancharda1194092011-08-10 20:44:24 +0000538 mmu_kernel_ssize);
Mike Kravetz54b79242005-11-07 16:25:48 -0800539}
Badari Pulavartyf8c88032008-01-29 09:19:24 +1100540
Badari Pulavarty52db9b42008-03-28 11:37:21 +1100541int remove_section_mapping(unsigned long start, unsigned long end)
Badari Pulavartyf8c88032008-01-29 09:19:24 +1100542{
Badari Pulavarty52db9b42008-03-28 11:37:21 +1100543 return htab_remove_mapping(start, end, mmu_linear_psize,
544 mmu_kernel_ssize);
Badari Pulavartyf8c88032008-01-29 09:19:24 +1100545}
Mike Kravetz54b79242005-11-07 16:25:48 -0800546#endif /* CONFIG_MEMORY_HOTPLUG */
547
Anton Blanchardb68a70c2011-04-04 23:56:18 +0000548#define FUNCTION_TEXT(A) ((*(unsigned long *)(A)))
Michael Ellerman7d0daae2006-06-23 18:16:38 +1000549
550static void __init htab_finish_init(void)
551{
552 extern unsigned int *htab_call_hpte_insert1;
553 extern unsigned int *htab_call_hpte_insert2;
554 extern unsigned int *htab_call_hpte_remove;
555 extern unsigned int *htab_call_hpte_updatepp;
556
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +1000557#ifdef CONFIG_PPC_HAS_HASH_64K
Michael Ellerman7d0daae2006-06-23 18:16:38 +1000558 extern unsigned int *ht64_call_hpte_insert1;
559 extern unsigned int *ht64_call_hpte_insert2;
560 extern unsigned int *ht64_call_hpte_remove;
561 extern unsigned int *ht64_call_hpte_updatepp;
562
Anton Blanchardb68a70c2011-04-04 23:56:18 +0000563 patch_branch(ht64_call_hpte_insert1,
564 FUNCTION_TEXT(ppc_md.hpte_insert),
565 BRANCH_SET_LINK);
566 patch_branch(ht64_call_hpte_insert2,
567 FUNCTION_TEXT(ppc_md.hpte_insert),
568 BRANCH_SET_LINK);
569 patch_branch(ht64_call_hpte_remove,
570 FUNCTION_TEXT(ppc_md.hpte_remove),
571 BRANCH_SET_LINK);
572 patch_branch(ht64_call_hpte_updatepp,
573 FUNCTION_TEXT(ppc_md.hpte_updatepp),
574 BRANCH_SET_LINK);
575
Jon Tollefson5b825832007-05-17 04:43:02 +1000576#endif /* CONFIG_PPC_HAS_HASH_64K */
Michael Ellerman7d0daae2006-06-23 18:16:38 +1000577
Anton Blanchardb68a70c2011-04-04 23:56:18 +0000578 patch_branch(htab_call_hpte_insert1,
579 FUNCTION_TEXT(ppc_md.hpte_insert),
580 BRANCH_SET_LINK);
581 patch_branch(htab_call_hpte_insert2,
582 FUNCTION_TEXT(ppc_md.hpte_insert),
583 BRANCH_SET_LINK);
584 patch_branch(htab_call_hpte_remove,
585 FUNCTION_TEXT(ppc_md.hpte_remove),
586 BRANCH_SET_LINK);
587 patch_branch(htab_call_hpte_updatepp,
588 FUNCTION_TEXT(ppc_md.hpte_updatepp),
589 BRANCH_SET_LINK);
Michael Ellerman7d0daae2006-06-23 18:16:38 +1000590}
591
Benjamin Herrenschmidt757c74d2009-03-19 19:34:16 +0000592static void __init htab_initialize(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
Michael Ellerman337a7122006-02-21 17:22:55 +1100594 unsigned long table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 unsigned long pteg_count;
Paul Mackerras9e88ba42008-08-30 11:26:27 +1000596 unsigned long prot;
Michael Ellerman41d824b2008-01-30 01:13:59 +1100597 unsigned long base = 0, size = 0, limit;
Benjamin Herrenschmidt28be7072010-08-04 13:43:53 +1000598 struct memblock_region *reg;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100599
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 DBG(" -> htab_initialize()\n");
601
Paul Mackerras1189be62007-10-11 20:37:10 +1000602 /* Initialize segment sizes */
603 htab_init_seg_sizes();
604
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100605 /* Initialize page sizes */
606 htab_init_page_sizes();
607
Matt Evans44ae3ab2011-04-06 19:48:50 +0000608 if (mmu_has_feature(MMU_FTR_1T_SEGMENT)) {
Paul Mackerras1189be62007-10-11 20:37:10 +1000609 mmu_kernel_ssize = MMU_SEGSIZE_1T;
610 mmu_highuser_ssize = MMU_SEGSIZE_1T;
611 printk(KERN_INFO "Using 1TB segments\n");
612 }
613
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 /*
615 * Calculate the required size of the htab. We want the number of
616 * PTEGs to equal one half the number of real pages.
617 */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100618 htab_size_bytes = htab_get_table_size();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 pteg_count = htab_size_bytes >> 7;
620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 htab_hash_mask = pteg_count - 1;
622
Michael Ellerman57cfb812006-03-21 20:45:59 +1100623 if (firmware_has_feature(FW_FEATURE_LPAR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 /* Using a hypervisor which owns the htab */
625 htab_address = NULL;
626 _SDR1 = 0;
627 } else {
628 /* Find storage for the HPT. Must be contiguous in
Michael Ellerman41d824b2008-01-30 01:13:59 +1100629 * the absolute address space. On cell we want it to be
Michael Ellerman31bf1112008-03-12 18:03:24 +1100630 * in the first 2 Gig so we can use it for IOMMU hacks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 */
Michael Ellerman41d824b2008-01-30 01:13:59 +1100632 if (machine_is(cell))
Michael Ellerman31bf1112008-03-12 18:03:24 +1100633 limit = 0x80000000;
Michael Ellerman41d824b2008-01-30 01:13:59 +1100634 else
Benjamin Herrenschmidt27f574c2010-07-06 15:39:00 -0700635 limit = MEMBLOCK_ALLOC_ANYWHERE;
Michael Ellerman41d824b2008-01-30 01:13:59 +1100636
Yinghai Lu95f72d12010-07-12 14:36:09 +1000637 table = memblock_alloc_base(htab_size_bytes, htab_size_bytes, limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
639 DBG("Hash table allocated at %lx, size: %lx\n", table,
640 htab_size_bytes);
641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 htab_address = abs_to_virt(table);
643
644 /* htab absolute addr + encoded htabsize */
645 _SDR1 = table + __ilog2(pteg_count) - 11;
646
647 /* Initialize the HPT with no entries */
648 memset((void *)table, 0, htab_size_bytes);
Paul Mackerras799d6042005-11-10 13:37:51 +1100649
650 /* Set SDR1 */
651 mtspr(SPRN_SDR1, _SDR1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 }
653
David Gibsonf5ea64d2008-10-12 17:54:24 +0000654 prot = pgprot_val(PAGE_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000656#ifdef CONFIG_DEBUG_PAGEALLOC
Yinghai Lu95f72d12010-07-12 14:36:09 +1000657 linear_map_hash_count = memblock_end_of_DRAM() >> PAGE_SHIFT;
658 linear_map_hash_slots = __va(memblock_alloc_base(linear_map_hash_count,
Benjamin Herrenschmidtcd3db0c2010-07-06 15:39:02 -0700659 1, ppc64_rma_size));
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000660 memset(linear_map_hash_slots, 0, linear_map_hash_count);
661#endif /* CONFIG_DEBUG_PAGEALLOC */
662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 /* On U3 based machines, we need to reserve the DART area and
664 * _NOT_ map it to avoid cache paradoxes as it's remapped non
665 * cacheable later on
666 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
668 /* create bolted the linear mapping in the hash table */
Benjamin Herrenschmidt28be7072010-08-04 13:43:53 +1000669 for_each_memblock(memory, reg) {
670 base = (unsigned long)__va(reg->base);
671 size = reg->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Sachin P. Sant5c339912009-12-13 21:15:12 +0000673 DBG("creating mapping for region: %lx..%lx (prot: %lx)\n",
Paul Mackerras9e88ba42008-08-30 11:26:27 +1000674 base, size, prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
676#ifdef CONFIG_U3_DART
677 /* Do not map the DART space. Fortunately, it will be aligned
Yinghai Lu95f72d12010-07-12 14:36:09 +1000678 * in such a way that it will not cross two memblock regions and
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100679 * will fit within a single 16Mb page.
680 * The DART space is assumed to be a full 16Mb region even if
681 * we only use 2Mb of that space. We will use more of it later
682 * for AGP GART. We have to use a full 16Mb large page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 */
684 DBG("DART base: %lx\n", dart_tablebase);
685
686 if (dart_tablebase != 0 && dart_tablebase >= base
687 && dart_tablebase < (base + size)) {
Michael Ellermancaf80e52006-03-21 20:45:51 +1100688 unsigned long dart_table_end = dart_tablebase + 16 * MB;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 if (base != dart_tablebase)
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100690 BUG_ON(htab_bolt_mapping(base, dart_tablebase,
Paul Mackerras9e88ba42008-08-30 11:26:27 +1000691 __pa(base), prot,
Paul Mackerras1189be62007-10-11 20:37:10 +1000692 mmu_linear_psize,
693 mmu_kernel_ssize));
Michael Ellermancaf80e52006-03-21 20:45:51 +1100694 if ((base + size) > dart_table_end)
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100695 BUG_ON(htab_bolt_mapping(dart_tablebase+16*MB,
Michael Ellermancaf80e52006-03-21 20:45:51 +1100696 base + size,
697 __pa(dart_table_end),
Paul Mackerras9e88ba42008-08-30 11:26:27 +1000698 prot,
Paul Mackerras1189be62007-10-11 20:37:10 +1000699 mmu_linear_psize,
700 mmu_kernel_ssize));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 continue;
702 }
703#endif /* CONFIG_U3_DART */
Michael Ellermancaf80e52006-03-21 20:45:51 +1100704 BUG_ON(htab_bolt_mapping(base, base + size, __pa(base),
Paul Mackerras9e88ba42008-08-30 11:26:27 +1000705 prot, mmu_linear_psize, mmu_kernel_ssize));
Benjamin Herrenschmidte63075a2010-07-06 15:39:01 -0700706 }
707 memblock_set_current_limit(MEMBLOCK_ALLOC_ANYWHERE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709 /*
710 * If we have a memory_limit and we've allocated TCEs then we need to
711 * explicitly map the TCE area at the top of RAM. We also cope with the
712 * case that the TCEs start below memory_limit.
713 * tce_alloc_start/end are 16MB aligned so the mapping should work
714 * for either 4K or 16MB pages.
715 */
716 if (tce_alloc_start) {
Michael Ellermanb5666f72005-12-05 10:24:33 -0600717 tce_alloc_start = (unsigned long)__va(tce_alloc_start);
718 tce_alloc_end = (unsigned long)__va(tce_alloc_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720 if (base + size >= tce_alloc_start)
721 tce_alloc_start = base + size + 1;
722
Michael Ellermancaf80e52006-03-21 20:45:51 +1100723 BUG_ON(htab_bolt_mapping(tce_alloc_start, tce_alloc_end,
Benjamin Herrenschmidtbc033b62008-08-05 16:19:56 +1000724 __pa(tce_alloc_start), prot,
Paul Mackerras1189be62007-10-11 20:37:10 +1000725 mmu_linear_psize, mmu_kernel_ssize));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 }
727
Michael Ellerman7d0daae2006-06-23 18:16:38 +1000728 htab_finish_init();
729
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 DBG(" <- htab_initialize()\n");
731}
732#undef KB
733#undef MB
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Benjamin Herrenschmidt757c74d2009-03-19 19:34:16 +0000735void __init early_init_mmu(void)
Paul Mackerras799d6042005-11-10 13:37:51 +1100736{
Benjamin Herrenschmidt757c74d2009-03-19 19:34:16 +0000737 /* Setup initial STAB address in the PACA */
738 get_paca()->stab_real = __pa((u64)&initial_stab);
739 get_paca()->stab_addr = (u64)&initial_stab;
740
741 /* Initialize the MMU Hash table and create the linear mapping
742 * of memory. Has to be done before stab/slb initialization as
743 * this is currently where the page size encoding is obtained
744 */
745 htab_initialize();
746
747 /* Initialize stab / SLB management except on iSeries
748 */
Matt Evans44ae3ab2011-04-06 19:48:50 +0000749 if (mmu_has_feature(MMU_FTR_SLB))
Benjamin Herrenschmidt757c74d2009-03-19 19:34:16 +0000750 slb_initialize();
751 else if (!firmware_has_feature(FW_FEATURE_ISERIES))
752 stab_initialize(get_paca()->stab_real);
753}
754
755#ifdef CONFIG_SMP
Michael Ellerman24f1ce82009-04-16 04:47:32 +0000756void __cpuinit early_init_mmu_secondary(void)
Benjamin Herrenschmidt757c74d2009-03-19 19:34:16 +0000757{
758 /* Initialize hash table for that CPU */
Michael Ellerman57cfb812006-03-21 20:45:59 +1100759 if (!firmware_has_feature(FW_FEATURE_LPAR))
Paul Mackerras799d6042005-11-10 13:37:51 +1100760 mtspr(SPRN_SDR1, _SDR1);
Benjamin Herrenschmidt757c74d2009-03-19 19:34:16 +0000761
762 /* Initialize STAB/SLB. We use a virtual address as it works
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300763 * in real mode on pSeries and we want a virtual address on
Benjamin Herrenschmidt757c74d2009-03-19 19:34:16 +0000764 * iSeries anyway
765 */
Matt Evans44ae3ab2011-04-06 19:48:50 +0000766 if (mmu_has_feature(MMU_FTR_SLB))
Benjamin Herrenschmidt757c74d2009-03-19 19:34:16 +0000767 slb_initialize();
768 else
769 stab_initialize(get_paca()->stab_addr);
Paul Mackerras799d6042005-11-10 13:37:51 +1100770}
Benjamin Herrenschmidt757c74d2009-03-19 19:34:16 +0000771#endif /* CONFIG_SMP */
Paul Mackerras799d6042005-11-10 13:37:51 +1100772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773/*
774 * Called by asm hashtable.S for doing lazy icache flush
775 */
776unsigned int hash_page_do_lazy_icache(unsigned int pp, pte_t pte, int trap)
777{
778 struct page *page;
779
Benjamin Herrenschmidt76c8e252005-11-08 11:21:05 +1100780 if (!pfn_valid(pte_pfn(pte)))
781 return pp;
782
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 page = pte_page(pte);
784
785 /* page is dirty */
786 if (!test_bit(PG_arch_1, &page->flags) && !PageReserved(page)) {
787 if (trap == 0x400) {
David Gibson0895ecd2009-10-26 19:24:31 +0000788 flush_dcache_icache_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 set_bit(PG_arch_1, &page->flags);
790 } else
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100791 pp |= HPTE_R_N;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 }
793 return pp;
794}
795
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000796#ifdef CONFIG_PPC_MM_SLICES
797unsigned int get_paca_psize(unsigned long addr)
798{
799 unsigned long index, slices;
800
801 if (addr < SLICE_LOW_TOP) {
802 slices = get_paca()->context.low_slices_psize;
803 index = GET_LOW_SLICE_INDEX(addr);
804 } else {
805 slices = get_paca()->context.high_slices_psize;
806 index = GET_HIGH_SLICE_INDEX(addr);
807 }
808 return (slices >> (index * 4)) & 0xF;
809}
810
811#else
812unsigned int get_paca_psize(unsigned long addr)
813{
814 return get_paca()->context.user_psize;
815}
816#endif
817
Paul Mackerras721151d2007-04-03 21:24:02 +1000818/*
819 * Demote a segment to using 4k pages.
820 * For now this makes the whole process use 4k pages.
821 */
Paul Mackerras721151d2007-04-03 21:24:02 +1000822#ifdef CONFIG_PPC_64K_PAGES
Paul Mackerrasfa282372008-01-24 08:35:13 +1100823void demote_segment_4k(struct mm_struct *mm, unsigned long addr)
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +1000824{
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000825 if (get_slice_psize(mm, addr) == MMU_PAGE_4K)
Paul Mackerras721151d2007-04-03 21:24:02 +1000826 return;
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000827 slice_set_range_psize(mm, addr, 1, MMU_PAGE_4K);
Geert Uytterhoeven1e57ba82007-07-17 02:35:38 +1000828#ifdef CONFIG_SPU_BASE
Paul Mackerras721151d2007-04-03 21:24:02 +1000829 spu_flush_all_slbs(mm);
830#endif
Paul Mackerras3a8247c2008-06-18 15:29:12 +1000831 if (get_paca_psize(addr) != MMU_PAGE_4K) {
Paul Mackerrasfa282372008-01-24 08:35:13 +1100832 get_paca()->context = mm->context;
833 slb_flush_and_rebolt();
834 }
Paul Mackerras721151d2007-04-03 21:24:02 +1000835}
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +1000836#endif /* CONFIG_PPC_64K_PAGES */
Paul Mackerras721151d2007-04-03 21:24:02 +1000837
Paul Mackerrasfa282372008-01-24 08:35:13 +1100838#ifdef CONFIG_PPC_SUBPAGE_PROT
839/*
840 * This looks up a 2-bit protection code for a 4k subpage of a 64k page.
841 * Userspace sets the subpage permissions using the subpage_prot system call.
842 *
843 * Result is 0: full permissions, _PAGE_RW: read-only,
844 * _PAGE_USER or _PAGE_USER|_PAGE_RW: no access.
845 */
David Gibsond28513b2009-11-26 18:56:04 +0000846static int subpage_protection(struct mm_struct *mm, unsigned long ea)
Paul Mackerrasfa282372008-01-24 08:35:13 +1100847{
David Gibsond28513b2009-11-26 18:56:04 +0000848 struct subpage_prot_table *spt = &mm->context.spt;
Paul Mackerrasfa282372008-01-24 08:35:13 +1100849 u32 spp = 0;
850 u32 **sbpm, *sbpp;
851
852 if (ea >= spt->maxaddr)
853 return 0;
854 if (ea < 0x100000000) {
855 /* addresses below 4GB use spt->low_prot */
856 sbpm = spt->low_prot;
857 } else {
858 sbpm = spt->protptrs[ea >> SBP_L3_SHIFT];
859 if (!sbpm)
860 return 0;
861 }
862 sbpp = sbpm[(ea >> SBP_L2_SHIFT) & (SBP_L2_COUNT - 1)];
863 if (!sbpp)
864 return 0;
865 spp = sbpp[(ea >> PAGE_SHIFT) & (SBP_L1_COUNT - 1)];
866
867 /* extract 2-bit bitfield for this 4k subpage */
868 spp >>= 30 - 2 * ((ea >> 12) & 0xf);
869
870 /* turn 0,1,2,3 into combination of _PAGE_USER and _PAGE_RW */
871 spp = ((spp & 2) ? _PAGE_USER : 0) | ((spp & 1) ? _PAGE_RW : 0);
872 return spp;
873}
874
875#else /* CONFIG_PPC_SUBPAGE_PROT */
David Gibsond28513b2009-11-26 18:56:04 +0000876static inline int subpage_protection(struct mm_struct *mm, unsigned long ea)
Paul Mackerrasfa282372008-01-24 08:35:13 +1100877{
878 return 0;
879}
880#endif
881
Benjamin Herrenschmidt4b8692c2010-07-23 10:31:13 +1000882void hash_failure_debug(unsigned long ea, unsigned long access,
883 unsigned long vsid, unsigned long trap,
884 int ssize, int psize, unsigned long pte)
885{
886 if (!printk_ratelimit())
887 return;
888 pr_info("mm: Hashing failure ! EA=0x%lx access=0x%lx current=%s\n",
889 ea, access, current->comm);
890 pr_info(" trap=0x%lx vsid=0x%lx ssize=%d psize=%d pte=0x%lx\n",
891 trap, vsid, ssize, psize, pte);
892}
893
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894/* Result code is:
895 * 0 - handled
896 * 1 - normal page fault
897 * -1 - critical hash insertion error
Paul Mackerrasfa282372008-01-24 08:35:13 +1100898 * -2 - access not permitted by subpage protection mechanism
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 */
900int hash_page(unsigned long ea, unsigned long access, unsigned long trap)
901{
David Gibsona1128f82009-12-16 14:29:56 +0000902 pgd_t *pgdir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 unsigned long vsid;
904 struct mm_struct *mm;
905 pte_t *ptep;
David Gibsona4fe3ce2009-10-26 19:24:31 +0000906 unsigned hugeshift;
Rusty Russell56aa4122009-03-15 18:16:43 +0000907 const struct cpumask *tmp;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100908 int rc, user_region = 0, local = 0;
Paul Mackerras1189be62007-10-11 20:37:10 +1000909 int psize, ssize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100911 DBG_LOW("hash_page(ea=%016lx, access=%lx, trap=%lx\n",
912 ea, access, trap);
David Gibson1f8d4192005-05-05 16:15:13 -0700913
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100914 if ((ea & ~REGION_MASK) >= PGTABLE_RANGE) {
915 DBG_LOW(" out of pgtable range !\n");
916 return 1;
917 }
918
919 /* Get region & vsid */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 switch (REGION_ID(ea)) {
921 case USER_REGION_ID:
922 user_region = 1;
923 mm = current->mm;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100924 if (! mm) {
925 DBG_LOW(" user region with no mm !\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 return 1;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100927 }
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +1000928 psize = get_slice_psize(mm, ea);
Paul Mackerras1189be62007-10-11 20:37:10 +1000929 ssize = user_segment_size(ea);
930 vsid = get_vsid(mm->context.id, ea, ssize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 case VMALLOC_REGION_ID:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 mm = &init_mm;
Paul Mackerras1189be62007-10-11 20:37:10 +1000934 vsid = get_kernel_vsid(ea, mmu_kernel_ssize);
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000935 if (ea < VMALLOC_END)
936 psize = mmu_vmalloc_psize;
937 else
938 psize = mmu_io_psize;
Paul Mackerras1189be62007-10-11 20:37:10 +1000939 ssize = mmu_kernel_ssize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 default:
942 /* Not a valid range
943 * Send the problem up to do_page_fault
944 */
945 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 }
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100947 DBG_LOW(" mm=%p, mm->pgdir=%p, vsid=%016lx\n", mm, mm->pgd, vsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100949 /* Get pgdir */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 pgdir = mm->pgd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 if (pgdir == NULL)
952 return 1;
953
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100954 /* Check CPU locality */
Rusty Russell56aa4122009-03-15 18:16:43 +0000955 tmp = cpumask_of(smp_processor_id());
956 if (user_region && cpumask_equal(mm_cpumask(mm), tmp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 local = 1;
958
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +1000959#ifndef CONFIG_PPC_64K_PAGES
David Gibsona4fe3ce2009-10-26 19:24:31 +0000960 /* If we use 4K pages and our psize is not 4K, then we might
961 * be hitting a special driver mapping, and need to align the
962 * address before we fetch the PTE.
963 *
964 * It could also be a hugepage mapping, in which case this is
965 * not necessary, but it's not harmful, either.
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +1000966 */
967 if (psize != MMU_PAGE_4K)
968 ea &= ~((1ul << mmu_psize_defs[psize].shift) - 1);
969#endif /* CONFIG_PPC_64K_PAGES */
970
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100971 /* Get PTE and page size from page tables */
David Gibsona4fe3ce2009-10-26 19:24:31 +0000972 ptep = find_linux_pte_or_hugepte(pgdir, ea, &hugeshift);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100973 if (ptep == NULL || !pte_present(*ptep)) {
974 DBG_LOW(" no PTE !\n");
975 return 1;
976 }
977
Benjamin Herrenschmidtca91e6c2010-07-23 08:53:23 +1000978 /* Add _PAGE_PRESENT to the required access perm */
979 access |= _PAGE_PRESENT;
980
981 /* Pre-check access permissions (will be re-checked atomically
982 * in __hash_page_XX but this pre-check is a fast path
983 */
984 if (access & ~pte_val(*ptep)) {
985 DBG_LOW(" no access !\n");
986 return 1;
987 }
988
David Gibsona4fe3ce2009-10-26 19:24:31 +0000989#ifdef CONFIG_HUGETLB_PAGE
990 if (hugeshift)
991 return __hash_page_huge(ea, access, vsid, ptep, trap, local,
992 ssize, hugeshift, psize);
993#endif /* CONFIG_HUGETLB_PAGE */
994
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100995#ifndef CONFIG_PPC_64K_PAGES
996 DBG_LOW(" i-pte: %016lx\n", pte_val(*ptep));
997#else
998 DBG_LOW(" i-pte: %016lx %016lx\n", pte_val(*ptep),
999 pte_val(*(ptep + PTRS_PER_PTE)));
1000#endif
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001001 /* Do actual hashing */
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +10001002#ifdef CONFIG_PPC_64K_PAGES
Paul Mackerras721151d2007-04-03 21:24:02 +10001003 /* If _PAGE_4K_PFN is set, make sure this is a 4k segment */
Paul Mackerras3a8247c2008-06-18 15:29:12 +10001004 if ((pte_val(*ptep) & _PAGE_4K_PFN) && psize == MMU_PAGE_64K) {
Paul Mackerras721151d2007-04-03 21:24:02 +10001005 demote_segment_4k(mm, ea);
1006 psize = MMU_PAGE_4K;
1007 }
1008
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +10001009 /* If this PTE is non-cacheable and we have restrictions on
1010 * using non cacheable large pages, then we switch to 4k
1011 */
1012 if (mmu_ci_restrictions && psize == MMU_PAGE_64K &&
1013 (pte_val(*ptep) & _PAGE_NO_CACHE)) {
1014 if (user_region) {
1015 demote_segment_4k(mm, ea);
1016 psize = MMU_PAGE_4K;
1017 } else if (ea < VMALLOC_END) {
1018 /*
1019 * some driver did a non-cacheable mapping
1020 * in vmalloc space, so switch vmalloc
1021 * to 4k pages
1022 */
1023 printk(KERN_ALERT "Reducing vmalloc segment "
1024 "to 4kB pages because of "
1025 "non-cacheable mapping\n");
1026 psize = mmu_vmalloc_psize = MMU_PAGE_4K;
Geert Uytterhoeven1e57ba82007-07-17 02:35:38 +10001027#ifdef CONFIG_SPU_BASE
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +01001028 spu_flush_all_slbs(mm);
1029#endif
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +10001030 }
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +10001031 }
1032 if (user_region) {
Paul Mackerras3a8247c2008-06-18 15:29:12 +10001033 if (psize != get_paca_psize(ea)) {
Benjamin Herrenschmidtf6ab0b92007-10-29 12:05:18 +11001034 get_paca()->context = mm->context;
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +10001035 slb_flush_and_rebolt();
1036 }
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +10001037 } else if (get_paca()->vmalloc_sllp !=
1038 mmu_psize_defs[mmu_vmalloc_psize].sllp) {
1039 get_paca()->vmalloc_sllp =
1040 mmu_psize_defs[mmu_vmalloc_psize].sllp;
Michael Neuling67439b72007-08-03 11:55:39 +10001041 slb_vmalloc_update();
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +10001042 }
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +10001043#endif /* CONFIG_PPC_64K_PAGES */
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +10001044
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +10001045#ifdef CONFIG_PPC_HAS_HASH_64K
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +10001046 if (psize == MMU_PAGE_64K)
Paul Mackerras1189be62007-10-11 20:37:10 +10001047 rc = __hash_page_64K(ea, access, vsid, ptep, trap, local, ssize);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001048 else
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +10001049#endif /* CONFIG_PPC_HAS_HASH_64K */
Paul Mackerrasfa282372008-01-24 08:35:13 +11001050 {
David Gibsona1128f82009-12-16 14:29:56 +00001051 int spp = subpage_protection(mm, ea);
Paul Mackerrasfa282372008-01-24 08:35:13 +11001052 if (access & spp)
1053 rc = -2;
1054 else
1055 rc = __hash_page_4K(ea, access, vsid, ptep, trap,
1056 local, ssize, spp);
1057 }
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001058
Benjamin Herrenschmidt4b8692c2010-07-23 10:31:13 +10001059 /* Dump some info in case of hash insertion failure, they should
1060 * never happen so it is really useful to know if/when they do
1061 */
1062 if (rc == -1)
1063 hash_failure_debug(ea, access, vsid, trap, ssize, psize,
1064 pte_val(*ptep));
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001065#ifndef CONFIG_PPC_64K_PAGES
1066 DBG_LOW(" o-pte: %016lx\n", pte_val(*ptep));
1067#else
1068 DBG_LOW(" o-pte: %016lx %016lx\n", pte_val(*ptep),
1069 pte_val(*(ptep + PTRS_PER_PTE)));
1070#endif
1071 DBG_LOW(" -> rc=%d\n", rc);
1072 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073}
Arnd Bergmann67207b92005-11-15 15:53:48 -05001074EXPORT_SYMBOL_GPL(hash_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001076void hash_preload(struct mm_struct *mm, unsigned long ea,
1077 unsigned long access, unsigned long trap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078{
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001079 unsigned long vsid;
Michael Neuling0b97fee2010-11-17 18:52:45 +00001080 pgd_t *pgdir;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001081 pte_t *ptep;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001082 unsigned long flags;
Benjamin Herrenschmidt4b8692c2010-07-23 10:31:13 +10001083 int rc, ssize, local = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +10001085 BUG_ON(REGION_ID(ea) != USER_REGION_ID);
1086
1087#ifdef CONFIG_PPC_MM_SLICES
1088 /* We only prefault standard pages for now */
Ilpo Järvinen2b02d132007-08-16 08:03:35 +10001089 if (unlikely(get_slice_psize(mm, ea) != mm->context.user_psize))
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001090 return;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +10001091#endif
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001092
1093 DBG_LOW("hash_preload(mm=%p, mm->pgdir=%p, ea=%016lx, access=%lx,"
1094 " trap=%lx\n", mm, mm->pgd, ea, access, trap);
1095
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +10001096 /* Get Linux PTE if available */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001097 pgdir = mm->pgd;
1098 if (pgdir == NULL)
1099 return;
1100 ptep = find_linux_pte(pgdir, ea);
1101 if (!ptep)
1102 return;
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +10001103
1104#ifdef CONFIG_PPC_64K_PAGES
1105 /* If either _PAGE_4K_PFN or _PAGE_NO_CACHE is set (and we are on
1106 * a 64K kernel), then we don't preload, hash_page() will take
1107 * care of it once we actually try to access the page.
1108 * That way we don't have to duplicate all of the logic for segment
1109 * page size demotion here
1110 */
1111 if (pte_val(*ptep) & (_PAGE_4K_PFN | _PAGE_NO_CACHE))
1112 return;
1113#endif /* CONFIG_PPC_64K_PAGES */
1114
1115 /* Get VSID */
Paul Mackerras1189be62007-10-11 20:37:10 +10001116 ssize = user_segment_size(ea);
1117 vsid = get_vsid(mm->context.id, ea, ssize);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001118
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +10001119 /* Hash doesn't like irqs */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001120 local_irq_save(flags);
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +10001121
1122 /* Is that local to this CPU ? */
Rusty Russell56aa4122009-03-15 18:16:43 +00001123 if (cpumask_equal(mm_cpumask(mm), cpumask_of(smp_processor_id())))
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001124 local = 1;
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +10001125
1126 /* Hash it in */
1127#ifdef CONFIG_PPC_HAS_HASH_64K
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +10001128 if (mm->context.user_psize == MMU_PAGE_64K)
Benjamin Herrenschmidt4b8692c2010-07-23 10:31:13 +10001129 rc = __hash_page_64K(ea, access, vsid, ptep, trap, local, ssize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 else
Jon Tollefson5b825832007-05-17 04:43:02 +10001131#endif /* CONFIG_PPC_HAS_HASH_64K */
Benjamin Herrenschmidt4b8692c2010-07-23 10:31:13 +10001132 rc = __hash_page_4K(ea, access, vsid, ptep, trap, local, ssize,
Michael Neuling1c2c25c2010-11-17 16:32:59 +00001133 subpage_protection(mm, ea));
Benjamin Herrenschmidt4b8692c2010-07-23 10:31:13 +10001134
1135 /* Dump some info in case of hash insertion failure, they should
1136 * never happen so it is really useful to know if/when they do
1137 */
1138 if (rc == -1)
1139 hash_failure_debug(ea, access, vsid, trap, ssize,
1140 mm->context.user_psize, pte_val(*ptep));
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +10001141
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001142 local_irq_restore(flags);
1143}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
Benjamin Herrenschmidtf6ab0b92007-10-29 12:05:18 +11001145/* WARNING: This is called from hash_low_64.S, if you change this prototype,
1146 * do not forget to update the assembly call site !
1147 */
Paul Mackerras1189be62007-10-11 20:37:10 +10001148void flush_hash_page(unsigned long va, real_pte_t pte, int psize, int ssize,
1149 int local)
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001150{
1151 unsigned long hash, index, shift, hidx, slot;
1152
Sachin P. Sant5c339912009-12-13 21:15:12 +00001153 DBG_LOW("flush_hash_page(va=%016lx)\n", va);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001154 pte_iterate_hashed_subpages(pte, psize, va, index, shift) {
Paul Mackerras1189be62007-10-11 20:37:10 +10001155 hash = hpt_hash(va, shift, ssize);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001156 hidx = __rpte_to_hidx(pte, index);
1157 if (hidx & _PTEIDX_SECONDARY)
1158 hash = ~hash;
1159 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
1160 slot += hidx & _PTEIDX_GROUP_IX;
Sachin P. Sant5c339912009-12-13 21:15:12 +00001161 DBG_LOW(" sub %ld: hash=%lx, hidx=%lx\n", index, slot, hidx);
Paul Mackerras1189be62007-10-11 20:37:10 +10001162 ppc_md.hpte_invalidate(slot, va, psize, ssize, local);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001163 } pte_iterate_hashed_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164}
1165
Benjamin Herrenschmidt61b1a942005-09-20 13:52:50 +10001166void flush_hash_range(unsigned long number, int local)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167{
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001168 if (ppc_md.flush_hash_range)
Benjamin Herrenschmidt61b1a942005-09-20 13:52:50 +10001169 ppc_md.flush_hash_range(number, local);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001170 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 int i;
Benjamin Herrenschmidt61b1a942005-09-20 13:52:50 +10001172 struct ppc64_tlb_batch *batch =
1173 &__get_cpu_var(ppc64_tlb_batch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
1175 for (i = 0; i < number; i++)
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001176 flush_hash_page(batch->vaddr[i], batch->pte[i],
Paul Mackerras1189be62007-10-11 20:37:10 +10001177 batch->psize, batch->ssize, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 }
1179}
1180
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181/*
1182 * low_hash_fault is called when we the low level hash code failed
1183 * to instert a PTE due to an hypervisor error
1184 */
Paul Mackerrasfa282372008-01-24 08:35:13 +11001185void low_hash_fault(struct pt_regs *regs, unsigned long address, int rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186{
1187 if (user_mode(regs)) {
Paul Mackerrasfa282372008-01-24 08:35:13 +11001188#ifdef CONFIG_PPC_SUBPAGE_PROT
1189 if (rc == -2)
1190 _exception(SIGSEGV, regs, SEGV_ACCERR, address);
1191 else
1192#endif
1193 _exception(SIGBUS, regs, BUS_ADRERR, address);
1194 } else
1195 bad_page_fault(regs, address, SIGBUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196}
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +10001197
1198#ifdef CONFIG_DEBUG_PAGEALLOC
1199static void kernel_map_linear_page(unsigned long vaddr, unsigned long lmi)
1200{
Paul Mackerras1189be62007-10-11 20:37:10 +10001201 unsigned long hash, hpteg;
1202 unsigned long vsid = get_kernel_vsid(vaddr, mmu_kernel_ssize);
1203 unsigned long va = hpt_va(vaddr, vsid, mmu_kernel_ssize);
Benjamin Herrenschmidtbc033b62008-08-05 16:19:56 +10001204 unsigned long mode = htab_convert_pte_flags(PAGE_KERNEL);
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +10001205 int ret;
1206
Paul Mackerras1189be62007-10-11 20:37:10 +10001207 hash = hpt_hash(va, PAGE_SHIFT, mmu_kernel_ssize);
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +10001208 hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
1209
1210 ret = ppc_md.hpte_insert(hpteg, va, __pa(vaddr),
Paul Mackerras1189be62007-10-11 20:37:10 +10001211 mode, HPTE_V_BOLTED,
1212 mmu_linear_psize, mmu_kernel_ssize);
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +10001213 BUG_ON (ret < 0);
1214 spin_lock(&linear_map_hash_lock);
1215 BUG_ON(linear_map_hash_slots[lmi] & 0x80);
1216 linear_map_hash_slots[lmi] = ret | 0x80;
1217 spin_unlock(&linear_map_hash_lock);
1218}
1219
1220static void kernel_unmap_linear_page(unsigned long vaddr, unsigned long lmi)
1221{
Paul Mackerras1189be62007-10-11 20:37:10 +10001222 unsigned long hash, hidx, slot;
1223 unsigned long vsid = get_kernel_vsid(vaddr, mmu_kernel_ssize);
1224 unsigned long va = hpt_va(vaddr, vsid, mmu_kernel_ssize);
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +10001225
Paul Mackerras1189be62007-10-11 20:37:10 +10001226 hash = hpt_hash(va, PAGE_SHIFT, mmu_kernel_ssize);
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +10001227 spin_lock(&linear_map_hash_lock);
1228 BUG_ON(!(linear_map_hash_slots[lmi] & 0x80));
1229 hidx = linear_map_hash_slots[lmi] & 0x7f;
1230 linear_map_hash_slots[lmi] = 0;
1231 spin_unlock(&linear_map_hash_lock);
1232 if (hidx & _PTEIDX_SECONDARY)
1233 hash = ~hash;
1234 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
1235 slot += hidx & _PTEIDX_GROUP_IX;
Paul Mackerras1189be62007-10-11 20:37:10 +10001236 ppc_md.hpte_invalidate(slot, va, mmu_linear_psize, mmu_kernel_ssize, 0);
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +10001237}
1238
1239void kernel_map_pages(struct page *page, int numpages, int enable)
1240{
1241 unsigned long flags, vaddr, lmi;
1242 int i;
1243
1244 local_irq_save(flags);
1245 for (i = 0; i < numpages; i++, page++) {
1246 vaddr = (unsigned long)page_address(page);
1247 lmi = __pa(vaddr) >> PAGE_SHIFT;
1248 if (lmi >= linear_map_hash_count)
1249 continue;
1250 if (enable)
1251 kernel_map_linear_page(vaddr, lmi);
1252 else
1253 kernel_unmap_linear_page(vaddr, lmi);
1254 }
1255 local_irq_restore(flags);
1256}
1257#endif /* CONFIG_DEBUG_PAGEALLOC */
Benjamin Herrenschmidtcd3db0c2010-07-06 15:39:02 -07001258
1259void setup_initial_memory_limit(phys_addr_t first_memblock_base,
1260 phys_addr_t first_memblock_size)
1261{
1262 /* We don't currently support the first MEMBLOCK not mapping 0
1263 * physical on those processors
1264 */
1265 BUG_ON(first_memblock_base != 0);
1266
1267 /* On LPAR systems, the first entry is our RMA region,
1268 * non-LPAR 64-bit hash MMU systems don't have a limitation
1269 * on real mode access, but using the first entry works well
1270 * enough. We also clamp it to 1G to avoid some funky things
1271 * such as RTAS bugs etc...
1272 */
1273 ppc64_rma_size = min_t(u64, first_memblock_size, 0x40000000);
1274
1275 /* Finally limit subsequent allocations */
1276 memblock_set_current_limit(ppc64_rma_size);
1277}