Merge remote-tracking branches 'asoc/fix/ak4613', 'asoc/fix/arizona', 'asoc/fix/cx204...
[linux-2.6-block.git] / arch / powerpc / mm / hash_utils_64.c
CommitLineData
1da177e4
LT
1/*
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
3c726f8d 22#undef DEBUG_LOW
1da177e4 23
1da177e4
LT
24#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>
66b15db6 30#include <linux/export.h>
1da177e4
LT
31#include <linux/ctype.h>
32#include <linux/cache.h>
33#include <linux/init.h>
34#include <linux/signal.h>
95f72d1e 35#include <linux/memblock.h>
ba12eede 36#include <linux/context_tracking.h>
1da177e4 37
1da177e4
LT
38#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>
1da177e4
LT
44#include <asm/uaccess.h>
45#include <asm/machdep.h>
d9b2b2a2 46#include <asm/prom.h>
1da177e4
LT
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>
1da177e4 53#include <asm/sections.h>
be3ebfe8 54#include <asm/copro.h>
aa39be09 55#include <asm/udbg.h>
b68a70c4 56#include <asm/code-patching.h>
3ccc00a7 57#include <asm/fadump.h>
f5339277 58#include <asm/firmware.h>
bc2a9408 59#include <asm/tm.h>
cfcb3d80 60#include <asm/trace.h>
1da177e4
LT
61
62#ifdef DEBUG
63#define DBG(fmt...) udbg_printf(fmt)
64#else
65#define DBG(fmt...)
66#endif
67
3c726f8d
BH
68#ifdef DEBUG_LOW
69#define DBG_LOW(fmt...) udbg_printf(fmt)
70#else
71#define DBG_LOW(fmt...)
72#endif
73
74#define KB (1024)
75#define MB (1024*KB)
658013e9 76#define GB (1024L*MB)
3c726f8d 77
1da177e4
LT
78/*
79 * Note: pte --> Linux PTE
80 * HPTE --> PowerPC Hashed Page Table Entry
81 *
82 * Execution context:
83 * htab_initialize is called with the MMU off (of course), but
84 * the kernel has been copied down to zero so it can directly
85 * reference global data. At this point it is very difficult
86 * to print debug info.
87 *
88 */
89
90#ifdef CONFIG_U3_DART
91extern unsigned long dart_tablebase;
92#endif /* CONFIG_U3_DART */
93
799d6046
PM
94static unsigned long _SDR1;
95struct mmu_psize_def mmu_psize_defs[MMU_PAGE_COUNT];
e1802b06 96EXPORT_SYMBOL_GPL(mmu_psize_defs);
799d6046 97
8e561e7e 98struct hash_pte *htab_address;
337a7128 99unsigned long htab_size_bytes;
96e28449 100unsigned long htab_hash_mask;
4ab79aa8 101EXPORT_SYMBOL_GPL(htab_hash_mask);
3c726f8d 102int mmu_linear_psize = MMU_PAGE_4K;
8ca7a82f 103EXPORT_SYMBOL_GPL(mmu_linear_psize);
3c726f8d 104int mmu_virtual_psize = MMU_PAGE_4K;
bf72aeba 105int mmu_vmalloc_psize = MMU_PAGE_4K;
cec08e7a
BH
106#ifdef CONFIG_SPARSEMEM_VMEMMAP
107int mmu_vmemmap_psize = MMU_PAGE_4K;
108#endif
bf72aeba 109int mmu_io_psize = MMU_PAGE_4K;
1189be65 110int mmu_kernel_ssize = MMU_SEGSIZE_256M;
8ca7a82f 111EXPORT_SYMBOL_GPL(mmu_kernel_ssize);
1189be65 112int mmu_highuser_ssize = MMU_SEGSIZE_256M;
584f8b71 113u16 mmu_slb_size = 64;
4ab79aa8 114EXPORT_SYMBOL_GPL(mmu_slb_size);
bf72aeba
PM
115#ifdef CONFIG_PPC_64K_PAGES
116int mmu_ci_restrictions;
117#endif
370a908d
BH
118#ifdef CONFIG_DEBUG_PAGEALLOC
119static u8 *linear_map_hash_slots;
120static unsigned long linear_map_hash_count;
ed166692 121static DEFINE_SPINLOCK(linear_map_hash_lock);
370a908d 122#endif /* CONFIG_DEBUG_PAGEALLOC */
1da177e4 123
3c726f8d
BH
124/* There are definitions of page sizes arrays to be used when none
125 * is provided by the firmware.
126 */
1da177e4 127
3c726f8d
BH
128/* Pre-POWER4 CPUs (4k pages only)
129 */
09de9ff8 130static struct mmu_psize_def mmu_psize_defaults_old[] = {
3c726f8d
BH
131 [MMU_PAGE_4K] = {
132 .shift = 12,
133 .sllp = 0,
b1022fbd 134 .penc = {[MMU_PAGE_4K] = 0, [1 ... MMU_PAGE_COUNT - 1] = -1},
3c726f8d
BH
135 .avpnm = 0,
136 .tlbiel = 0,
137 },
138};
139
140/* POWER4, GPUL, POWER5
141 *
142 * Support for 16Mb large pages
143 */
09de9ff8 144static struct mmu_psize_def mmu_psize_defaults_gp[] = {
3c726f8d
BH
145 [MMU_PAGE_4K] = {
146 .shift = 12,
147 .sllp = 0,
b1022fbd 148 .penc = {[MMU_PAGE_4K] = 0, [1 ... MMU_PAGE_COUNT - 1] = -1},
3c726f8d
BH
149 .avpnm = 0,
150 .tlbiel = 1,
151 },
152 [MMU_PAGE_16M] = {
153 .shift = 24,
154 .sllp = SLB_VSID_L,
b1022fbd
AK
155 .penc = {[0 ... MMU_PAGE_16M - 1] = -1, [MMU_PAGE_16M] = 0,
156 [MMU_PAGE_16M + 1 ... MMU_PAGE_COUNT - 1] = -1 },
3c726f8d
BH
157 .avpnm = 0x1UL,
158 .tlbiel = 0,
159 },
160};
161
dc47c0c1
AK
162/*
163 * 'R' and 'C' update notes:
164 * - Under pHyp or KVM, the updatepp path will not set C, thus it *will*
165 * create writeable HPTEs without C set, because the hcall H_PROTECT
166 * that we use in that case will not update C
167 * - The above is however not a problem, because we also don't do that
168 * fancy "no flush" variant of eviction and we use H_REMOVE which will
169 * do the right thing and thus we don't have the race I described earlier
170 *
171 * - Under bare metal, we do have the race, so we need R and C set
172 * - We make sure R is always set and never lost
173 * - C is _PAGE_DIRTY, and *should* always be set for a writeable mapping
174 */
c6a3c495 175unsigned long htab_convert_pte_flags(unsigned long pteflags)
bc033b63 176{
c6a3c495 177 unsigned long rflags = 0;
bc033b63
BH
178
179 /* _PAGE_EXEC -> NOEXEC */
180 if ((pteflags & _PAGE_EXEC) == 0)
181 rflags |= HPTE_R_N;
c6a3c495 182 /*
e58e87ad 183 * PPP bits:
1ec3f937 184 * Linux uses slb key 0 for kernel and 1 for user.
e58e87ad
AK
185 * kernel RW areas are mapped with PPP=0b000
186 * User area is mapped with PPP=0b010 for read/write
187 * or PPP=0b011 for read-only (including writeable but clean pages).
bc033b63 188 */
e58e87ad
AK
189 if (pteflags & _PAGE_PRIVILEGED) {
190 /*
191 * Kernel read only mapped with ppp bits 0b110
192 */
193 if (!(pteflags & _PAGE_WRITE))
194 rflags |= (HPTE_R_PP0 | 0x2);
195 } else {
c7d54842
AK
196 if (pteflags & _PAGE_RWX)
197 rflags |= 0x2;
198 if (!((pteflags & _PAGE_WRITE) && (pteflags & _PAGE_DIRTY)))
c6a3c495
AK
199 rflags |= 0x1;
200 }
c8c06f5a 201 /*
dc47c0c1
AK
202 * We can't allow hardware to update hpte bits. Hence always
203 * set 'R' bit and set 'C' if it is a write fault
c8c06f5a 204 */
e568006b 205 rflags |= HPTE_R_R;
dc47c0c1
AK
206
207 if (pteflags & _PAGE_DIRTY)
208 rflags |= HPTE_R_C;
40e8550a
AK
209 /*
210 * Add in WIG bits
211 */
30bda41a
AK
212
213 if ((pteflags & _PAGE_CACHE_CTL) == _PAGE_TOLERANT)
40e8550a 214 rflags |= HPTE_R_I;
e568006b 215 else if ((pteflags & _PAGE_CACHE_CTL) == _PAGE_NON_IDEMPOTENT)
30bda41a 216 rflags |= (HPTE_R_I | HPTE_R_G);
e568006b
AK
217 else if ((pteflags & _PAGE_CACHE_CTL) == _PAGE_SAO)
218 rflags |= (HPTE_R_W | HPTE_R_I | HPTE_R_M);
219 else
220 /*
221 * Add memory coherence if cache inhibited is not set
222 */
223 rflags |= HPTE_R_M;
40e8550a
AK
224
225 return rflags;
bc033b63 226}
3c726f8d
BH
227
228int htab_bolt_mapping(unsigned long vstart, unsigned long vend,
bc033b63 229 unsigned long pstart, unsigned long prot,
1189be65 230 int psize, int ssize)
1da177e4 231{
3c726f8d
BH
232 unsigned long vaddr, paddr;
233 unsigned int step, shift;
3c726f8d 234 int ret = 0;
1da177e4 235
3c726f8d
BH
236 shift = mmu_psize_defs[psize].shift;
237 step = 1 << shift;
1da177e4 238
bc033b63
BH
239 prot = htab_convert_pte_flags(prot);
240
241 DBG("htab_bolt_mapping(%lx..%lx -> %lx (%lx,%d,%d)\n",
242 vstart, vend, pstart, prot, psize, ssize);
243
3c726f8d
BH
244 for (vaddr = vstart, paddr = pstart; vaddr < vend;
245 vaddr += step, paddr += step) {
370a908d 246 unsigned long hash, hpteg;
1189be65 247 unsigned long vsid = get_kernel_vsid(vaddr, ssize);
5524a27d 248 unsigned long vpn = hpt_vpn(vaddr, vsid, ssize);
9e88ba4e
PM
249 unsigned long tprot = prot;
250
c60ac569
AK
251 /*
252 * If we hit a bad address return error.
253 */
254 if (!vsid)
255 return -1;
9e88ba4e 256 /* Make kernel text executable */
549e8152 257 if (overlaps_kernel_text(vaddr, vaddr + step))
9e88ba4e 258 tprot &= ~HPTE_R_N;
1da177e4 259
b18db0b8
AG
260 /* Make kvm guest trampolines executable */
261 if (overlaps_kvm_tmp(vaddr, vaddr + step))
262 tprot &= ~HPTE_R_N;
263
429d2e83
MS
264 /*
265 * If relocatable, check if it overlaps interrupt vectors that
266 * are copied down to real 0. For relocatable kernel
267 * (e.g. kdump case) we copy interrupt vectors down to real
268 * address 0. Mark that region as executable. This is
269 * because on p8 system with relocation on exception feature
270 * enabled, exceptions are raised with MMU (IR=DR=1) ON. Hence
271 * in order to execute the interrupt handlers in virtual
272 * mode the vector region need to be marked as executable.
273 */
274 if ((PHYSICAL_START > MEMORY_START) &&
275 overlaps_interrupt_vector_text(vaddr, vaddr + step))
276 tprot &= ~HPTE_R_N;
277
5524a27d 278 hash = hpt_hash(vpn, shift, ssize);
1da177e4
LT
279 hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
280
c30a4df3 281 BUG_ON(!ppc_md.hpte_insert);
5524a27d 282 ret = ppc_md.hpte_insert(hpteg, vpn, paddr, tprot,
b1022fbd 283 HPTE_V_BOLTED, psize, psize, ssize);
c30a4df3 284
3c726f8d
BH
285 if (ret < 0)
286 break;
e7df0d88 287
370a908d 288#ifdef CONFIG_DEBUG_PAGEALLOC
e7df0d88
JK
289 if (debug_pagealloc_enabled() &&
290 (paddr >> PAGE_SHIFT) < linear_map_hash_count)
370a908d
BH
291 linear_map_hash_slots[paddr >> PAGE_SHIFT] = ret | 0x80;
292#endif /* CONFIG_DEBUG_PAGEALLOC */
3c726f8d
BH
293 }
294 return ret < 0 ? ret : 0;
295}
1da177e4 296
ed5694a8 297int htab_remove_mapping(unsigned long vstart, unsigned long vend,
f8c8803b
BP
298 int psize, int ssize)
299{
300 unsigned long vaddr;
301 unsigned int step, shift;
27828f98
DG
302 int rc;
303 int ret = 0;
f8c8803b
BP
304
305 shift = mmu_psize_defs[psize].shift;
306 step = 1 << shift;
307
abd0a0e7
DG
308 if (!ppc_md.hpte_removebolted)
309 return -ENODEV;
f8c8803b 310
27828f98
DG
311 for (vaddr = vstart; vaddr < vend; vaddr += step) {
312 rc = ppc_md.hpte_removebolted(vaddr, psize, ssize);
313 if (rc == -ENOENT) {
314 ret = -ENOENT;
315 continue;
316 }
317 if (rc < 0)
318 return rc;
319 }
52db9b44 320
27828f98 321 return ret;
f8c8803b
BP
322}
323
1189be65
PM
324static int __init htab_dt_scan_seg_sizes(unsigned long node,
325 const char *uname, int depth,
326 void *data)
327{
9d0c4dfe
RH
328 const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
329 const __be32 *prop;
330 int size = 0;
1189be65
PM
331
332 /* We are scanning "cpu" nodes only */
333 if (type == NULL || strcmp(type, "cpu") != 0)
334 return 0;
335
12f04f2b 336 prop = of_get_flat_dt_prop(node, "ibm,processor-segment-sizes", &size);
1189be65
PM
337 if (prop == NULL)
338 return 0;
339 for (; size >= 4; size -= 4, ++prop) {
12f04f2b 340 if (be32_to_cpu(prop[0]) == 40) {
1189be65 341 DBG("1T segment support detected\n");
44ae3ab3 342 cur_cpu_spec->mmu_features |= MMU_FTR_1T_SEGMENT;
f5534004 343 return 1;
1189be65 344 }
1189be65 345 }
44ae3ab3 346 cur_cpu_spec->mmu_features &= ~MMU_FTR_NO_SLBIE_B;
1189be65
PM
347 return 0;
348}
349
350static void __init htab_init_seg_sizes(void)
351{
352 of_scan_flat_dt(htab_dt_scan_seg_sizes, NULL);
353}
354
b1022fbd
AK
355static int __init get_idx_from_shift(unsigned int shift)
356{
357 int idx = -1;
358
359 switch (shift) {
360 case 0xc:
361 idx = MMU_PAGE_4K;
362 break;
363 case 0x10:
364 idx = MMU_PAGE_64K;
365 break;
366 case 0x14:
367 idx = MMU_PAGE_1M;
368 break;
369 case 0x18:
370 idx = MMU_PAGE_16M;
371 break;
372 case 0x22:
373 idx = MMU_PAGE_16G;
374 break;
375 }
376 return idx;
377}
378
3c726f8d
BH
379static int __init htab_dt_scan_page_sizes(unsigned long node,
380 const char *uname, int depth,
381 void *data)
382{
9d0c4dfe
RH
383 const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
384 const __be32 *prop;
385 int size = 0;
3c726f8d
BH
386
387 /* We are scanning "cpu" nodes only */
388 if (type == NULL || strcmp(type, "cpu") != 0)
389 return 0;
390
12f04f2b 391 prop = of_get_flat_dt_prop(node, "ibm,segment-page-sizes", &size);
9e34992a
ME
392 if (!prop)
393 return 0;
394
395 pr_info("Page sizes from device-tree:\n");
396 size /= 4;
397 cur_cpu_spec->mmu_features &= ~(MMU_FTR_16M_PAGE);
398 while(size > 0) {
399 unsigned int base_shift = be32_to_cpu(prop[0]);
400 unsigned int slbenc = be32_to_cpu(prop[1]);
401 unsigned int lpnum = be32_to_cpu(prop[2]);
402 struct mmu_psize_def *def;
403 int idx, base_idx;
404
405 size -= 3; prop += 3;
406 base_idx = get_idx_from_shift(base_shift);
407 if (base_idx < 0) {
408 /* skip the pte encoding also */
409 prop += lpnum * 2; size -= lpnum * 2;
410 continue;
411 }
412 def = &mmu_psize_defs[base_idx];
413 if (base_idx == MMU_PAGE_16M)
414 cur_cpu_spec->mmu_features |= MMU_FTR_16M_PAGE;
415
416 def->shift = base_shift;
417 if (base_shift <= 23)
418 def->avpnm = 0;
419 else
420 def->avpnm = (1 << (base_shift - 23)) - 1;
421 def->sllp = slbenc;
422 /*
423 * We don't know for sure what's up with tlbiel, so
424 * for now we only set it for 4K and 64K pages
425 */
426 if (base_idx == MMU_PAGE_4K || base_idx == MMU_PAGE_64K)
427 def->tlbiel = 1;
428 else
429 def->tlbiel = 0;
430
431 while (size > 0 && lpnum) {
432 unsigned int shift = be32_to_cpu(prop[0]);
433 int penc = be32_to_cpu(prop[1]);
434
435 prop += 2; size -= 2;
436 lpnum--;
437
438 idx = get_idx_from_shift(shift);
439 if (idx < 0)
b1022fbd 440 continue;
9e34992a
ME
441
442 if (penc == -1)
443 pr_err("Invalid penc for base_shift=%d "
444 "shift=%d\n", base_shift, shift);
445
446 def->penc[idx] = penc;
447 pr_info("base_shift=%d: shift=%d, sllp=0x%04lx,"
448 " avpnm=0x%08lx, tlbiel=%d, penc=%d\n",
449 base_shift, shift, def->sllp,
450 def->avpnm, def->tlbiel, def->penc[idx]);
1da177e4 451 }
3c726f8d 452 }
9e34992a
ME
453
454 return 1;
3c726f8d
BH
455}
456
e16a9c09 457#ifdef CONFIG_HUGETLB_PAGE
658013e9
JT
458/* Scan for 16G memory blocks that have been set aside for huge pages
459 * and reserve those blocks for 16G huge pages.
460 */
461static int __init htab_dt_scan_hugepage_blocks(unsigned long node,
462 const char *uname, int depth,
463 void *data) {
9d0c4dfe
RH
464 const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
465 const __be64 *addr_prop;
466 const __be32 *page_count_prop;
658013e9
JT
467 unsigned int expected_pages;
468 long unsigned int phys_addr;
469 long unsigned int block_size;
470
471 /* We are scanning "memory" nodes only */
472 if (type == NULL || strcmp(type, "memory") != 0)
473 return 0;
474
475 /* This property is the log base 2 of the number of virtual pages that
476 * will represent this memory block. */
477 page_count_prop = of_get_flat_dt_prop(node, "ibm,expected#pages", NULL);
478 if (page_count_prop == NULL)
479 return 0;
12f04f2b 480 expected_pages = (1 << be32_to_cpu(page_count_prop[0]));
658013e9
JT
481 addr_prop = of_get_flat_dt_prop(node, "reg", NULL);
482 if (addr_prop == NULL)
483 return 0;
12f04f2b
AB
484 phys_addr = be64_to_cpu(addr_prop[0]);
485 block_size = be64_to_cpu(addr_prop[1]);
658013e9
JT
486 if (block_size != (16 * GB))
487 return 0;
488 printk(KERN_INFO "Huge page(16GB) memory: "
489 "addr = 0x%lX size = 0x%lX pages = %d\n",
490 phys_addr, block_size, expected_pages);
95f72d1e
YL
491 if (phys_addr + (16 * GB) <= memblock_end_of_DRAM()) {
492 memblock_reserve(phys_addr, block_size * expected_pages);
4792adba
JT
493 add_gpage(phys_addr, block_size, expected_pages);
494 }
658013e9
JT
495 return 0;
496}
e16a9c09 497#endif /* CONFIG_HUGETLB_PAGE */
658013e9 498
b1022fbd
AK
499static void mmu_psize_set_default_penc(void)
500{
501 int bpsize, apsize;
502 for (bpsize = 0; bpsize < MMU_PAGE_COUNT; bpsize++)
503 for (apsize = 0; apsize < MMU_PAGE_COUNT; apsize++)
504 mmu_psize_defs[bpsize].penc[apsize] = -1;
505}
506
9048e648
AG
507#ifdef CONFIG_PPC_64K_PAGES
508
509static bool might_have_hea(void)
510{
511 /*
512 * The HEA ethernet adapter requires awareness of the
513 * GX bus. Without that awareness we can easily assume
514 * we will never see an HEA ethernet device.
515 */
516#ifdef CONFIG_IBMEBUS
517 return !cpu_has_feature(CPU_FTR_ARCH_207S);
518#else
519 return false;
520#endif
521}
522
523#endif /* #ifdef CONFIG_PPC_64K_PAGES */
524
3c726f8d
BH
525static void __init htab_init_page_sizes(void)
526{
527 int rc;
528
b1022fbd
AK
529 /* se the invalid penc to -1 */
530 mmu_psize_set_default_penc();
531
3c726f8d
BH
532 /* Default to 4K pages only */
533 memcpy(mmu_psize_defs, mmu_psize_defaults_old,
534 sizeof(mmu_psize_defaults_old));
535
536 /*
537 * Try to find the available page sizes in the device-tree
538 */
539 rc = of_scan_flat_dt(htab_dt_scan_page_sizes, NULL);
540 if (rc != 0) /* Found */
541 goto found;
542
543 /*
544 * Not in the device-tree, let's fallback on known size
545 * list for 16M capable GP & GR
546 */
44ae3ab3 547 if (mmu_has_feature(MMU_FTR_16M_PAGE))
3c726f8d
BH
548 memcpy(mmu_psize_defs, mmu_psize_defaults_gp,
549 sizeof(mmu_psize_defaults_gp));
e7df0d88
JK
550found:
551 if (!debug_pagealloc_enabled()) {
552 /*
553 * Pick a size for the linear mapping. Currently, we only
554 * support 16M, 1M and 4K which is the default
555 */
556 if (mmu_psize_defs[MMU_PAGE_16M].shift)
557 mmu_linear_psize = MMU_PAGE_16M;
558 else if (mmu_psize_defs[MMU_PAGE_1M].shift)
559 mmu_linear_psize = MMU_PAGE_1M;
560 }
3c726f8d 561
bf72aeba 562#ifdef CONFIG_PPC_64K_PAGES
3c726f8d
BH
563 /*
564 * Pick a size for the ordinary pages. Default is 4K, we support
bf72aeba
PM
565 * 64K for user mappings and vmalloc if supported by the processor.
566 * We only use 64k for ioremap if the processor
567 * (and firmware) support cache-inhibited large pages.
568 * If not, we use 4k and set mmu_ci_restrictions so that
569 * hash_page knows to switch processes that use cache-inhibited
570 * mappings to 4k pages.
3c726f8d 571 */
bf72aeba 572 if (mmu_psize_defs[MMU_PAGE_64K].shift) {
3c726f8d 573 mmu_virtual_psize = MMU_PAGE_64K;
bf72aeba 574 mmu_vmalloc_psize = MMU_PAGE_64K;
370a908d
BH
575 if (mmu_linear_psize == MMU_PAGE_4K)
576 mmu_linear_psize = MMU_PAGE_64K;
44ae3ab3 577 if (mmu_has_feature(MMU_FTR_CI_LARGE_PAGE)) {
cfe666b1 578 /*
9048e648
AG
579 * When running on pSeries using 64k pages for ioremap
580 * would stop us accessing the HEA ethernet. So if we
581 * have the chance of ever seeing one, stay at 4k.
cfe666b1 582 */
9048e648 583 if (!might_have_hea() || !machine_is(pseries))
cfe666b1
PM
584 mmu_io_psize = MMU_PAGE_64K;
585 } else
bf72aeba
PM
586 mmu_ci_restrictions = 1;
587 }
370a908d 588#endif /* CONFIG_PPC_64K_PAGES */
3c726f8d 589
cec08e7a
BH
590#ifdef CONFIG_SPARSEMEM_VMEMMAP
591 /* We try to use 16M pages for vmemmap if that is supported
592 * and we have at least 1G of RAM at boot
593 */
594 if (mmu_psize_defs[MMU_PAGE_16M].shift &&
95f72d1e 595 memblock_phys_mem_size() >= 0x40000000)
cec08e7a
BH
596 mmu_vmemmap_psize = MMU_PAGE_16M;
597 else if (mmu_psize_defs[MMU_PAGE_64K].shift)
598 mmu_vmemmap_psize = MMU_PAGE_64K;
599 else
600 mmu_vmemmap_psize = MMU_PAGE_4K;
601#endif /* CONFIG_SPARSEMEM_VMEMMAP */
602
bf72aeba 603 printk(KERN_DEBUG "Page orders: linear mapping = %d, "
cec08e7a
BH
604 "virtual = %d, io = %d"
605#ifdef CONFIG_SPARSEMEM_VMEMMAP
606 ", vmemmap = %d"
607#endif
608 "\n",
3c726f8d 609 mmu_psize_defs[mmu_linear_psize].shift,
bf72aeba 610 mmu_psize_defs[mmu_virtual_psize].shift,
cec08e7a
BH
611 mmu_psize_defs[mmu_io_psize].shift
612#ifdef CONFIG_SPARSEMEM_VMEMMAP
613 ,mmu_psize_defs[mmu_vmemmap_psize].shift
614#endif
615 );
3c726f8d
BH
616
617#ifdef CONFIG_HUGETLB_PAGE
658013e9
JT
618 /* Reserve 16G huge page memory sections for huge pages */
619 of_scan_flat_dt(htab_dt_scan_hugepage_blocks, NULL);
3c726f8d
BH
620#endif /* CONFIG_HUGETLB_PAGE */
621}
622
623static int __init htab_dt_scan_pftsize(unsigned long node,
624 const char *uname, int depth,
625 void *data)
626{
9d0c4dfe
RH
627 const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
628 const __be32 *prop;
3c726f8d
BH
629
630 /* We are scanning "cpu" nodes only */
631 if (type == NULL || strcmp(type, "cpu") != 0)
632 return 0;
633
12f04f2b 634 prop = of_get_flat_dt_prop(node, "ibm,pft-size", NULL);
3c726f8d
BH
635 if (prop != NULL) {
636 /* pft_size[0] is the NUMA CEC cookie */
12f04f2b 637 ppc64_pft_size = be32_to_cpu(prop[1]);
3c726f8d 638 return 1;
1da177e4 639 }
3c726f8d 640 return 0;
1da177e4
LT
641}
642
5c3c7ede 643unsigned htab_shift_for_mem_size(unsigned long mem_size)
3eac8c69 644{
5c3c7ede
DG
645 unsigned memshift = __ilog2(mem_size);
646 unsigned pshift = mmu_psize_defs[mmu_virtual_psize].shift;
647 unsigned pteg_shift;
648
649 /* round mem_size up to next power of 2 */
650 if ((1UL << memshift) < mem_size)
651 memshift += 1;
3eac8c69 652
5c3c7ede
DG
653 /* aim for 2 pages / pteg */
654 pteg_shift = memshift - (pshift + 1);
3eac8c69 655
5c3c7ede
DG
656 /*
657 * 2^11 PTEGS of 128 bytes each, ie. 2^18 bytes is the minimum htab
658 * size permitted by the architecture.
659 */
660 return max(pteg_shift + 7, 18U);
661}
662
663static unsigned long __init htab_get_table_size(void)
664{
3c726f8d 665 /* If hash size isn't already provided by the platform, we try to
943ffb58 666 * retrieve it from the device-tree. If it's not there neither, we
3c726f8d 667 * calculate it now based on the total RAM size
3eac8c69 668 */
3c726f8d
BH
669 if (ppc64_pft_size == 0)
670 of_scan_flat_dt(htab_dt_scan_pftsize, NULL);
3eac8c69
PM
671 if (ppc64_pft_size)
672 return 1UL << ppc64_pft_size;
673
5c3c7ede 674 return 1UL << htab_shift_for_mem_size(memblock_phys_mem_size());
3eac8c69
PM
675}
676
54b79248 677#ifdef CONFIG_MEMORY_HOTPLUG
a1194097 678int create_section_mapping(unsigned long start, unsigned long end)
54b79248 679{
1dace6c6
DG
680 int rc = htab_bolt_mapping(start, end, __pa(start),
681 pgprot_val(PAGE_KERNEL), mmu_linear_psize,
682 mmu_kernel_ssize);
683
684 if (rc < 0) {
685 int rc2 = htab_remove_mapping(start, end, mmu_linear_psize,
686 mmu_kernel_ssize);
687 BUG_ON(rc2 && (rc2 != -ENOENT));
688 }
689 return rc;
54b79248 690}
f8c8803b 691
52db9b44 692int remove_section_mapping(unsigned long start, unsigned long end)
f8c8803b 693{
abd0a0e7
DG
694 int rc = htab_remove_mapping(start, end, mmu_linear_psize,
695 mmu_kernel_ssize);
696 WARN_ON(rc < 0);
697 return rc;
f8c8803b 698}
54b79248
MK
699#endif /* CONFIG_MEMORY_HOTPLUG */
700
50de596d
AK
701static void __init hash_init_partition_table(phys_addr_t hash_table,
702 unsigned long pteg_count)
703{
704 unsigned long ps_field;
705 unsigned long htab_size;
706 unsigned long patb_size = 1UL << PATB_SIZE_SHIFT;
707
708 /*
709 * slb llp encoding for the page size used in VPM real mode.
710 * We can ignore that for lpid 0
711 */
712 ps_field = 0;
713 htab_size = __ilog2(pteg_count) - 11;
714
715 BUILD_BUG_ON_MSG((PATB_SIZE_SHIFT > 24), "Partition table size too large.");
716 partition_tb = __va(memblock_alloc_base(patb_size, patb_size,
717 MEMBLOCK_ALLOC_ANYWHERE));
718
719 /* Initialize the Partition Table with no entries */
720 memset((void *)partition_tb, 0, patb_size);
721 partition_tb->patb0 = cpu_to_be64(ps_field | hash_table | htab_size);
722 /*
723 * FIXME!! This should be done via update_partition table
724 * For now UPRT is 0 for us.
725 */
726 partition_tb->patb1 = 0;
727 DBG("Partition table %p\n", partition_tb);
728 /*
729 * update partition table control register,
730 * 64 K size.
731 */
732 mtspr(SPRN_PTCR, __pa(partition_tb) | (PATB_SIZE_SHIFT - 12));
733
734}
735
757c74d2 736static void __init htab_initialize(void)
1da177e4 737{
337a7128 738 unsigned long table;
1da177e4 739 unsigned long pteg_count;
9e88ba4e 740 unsigned long prot;
41d824bf 741 unsigned long base = 0, size = 0, limit;
28be7072 742 struct memblock_region *reg;
3c726f8d 743
1da177e4
LT
744 DBG(" -> htab_initialize()\n");
745
1189be65
PM
746 /* Initialize segment sizes */
747 htab_init_seg_sizes();
748
3c726f8d
BH
749 /* Initialize page sizes */
750 htab_init_page_sizes();
751
44ae3ab3 752 if (mmu_has_feature(MMU_FTR_1T_SEGMENT)) {
1189be65
PM
753 mmu_kernel_ssize = MMU_SEGSIZE_1T;
754 mmu_highuser_ssize = MMU_SEGSIZE_1T;
755 printk(KERN_INFO "Using 1TB segments\n");
756 }
757
1da177e4
LT
758 /*
759 * Calculate the required size of the htab. We want the number of
760 * PTEGs to equal one half the number of real pages.
761 */
3c726f8d 762 htab_size_bytes = htab_get_table_size();
1da177e4
LT
763 pteg_count = htab_size_bytes >> 7;
764
1da177e4
LT
765 htab_hash_mask = pteg_count - 1;
766
57cfb814 767 if (firmware_has_feature(FW_FEATURE_LPAR)) {
1da177e4
LT
768 /* Using a hypervisor which owns the htab */
769 htab_address = NULL;
770 _SDR1 = 0;
3ccc00a7
MS
771#ifdef CONFIG_FA_DUMP
772 /*
773 * If firmware assisted dump is active firmware preserves
774 * the contents of htab along with entire partition memory.
775 * Clear the htab if firmware assisted dump is active so
776 * that we dont end up using old mappings.
777 */
778 if (is_fadump_active() && ppc_md.hpte_clear_all)
779 ppc_md.hpte_clear_all();
780#endif
1da177e4
LT
781 } else {
782 /* Find storage for the HPT. Must be contiguous in
41d824bf 783 * the absolute address space. On cell we want it to be
31bf1119 784 * in the first 2 Gig so we can use it for IOMMU hacks.
1da177e4 785 */
41d824bf 786 if (machine_is(cell))
31bf1119 787 limit = 0x80000000;
41d824bf 788 else
27f574c2 789 limit = MEMBLOCK_ALLOC_ANYWHERE;
41d824bf 790
95f72d1e 791 table = memblock_alloc_base(htab_size_bytes, htab_size_bytes, limit);
1da177e4
LT
792
793 DBG("Hash table allocated at %lx, size: %lx\n", table,
794 htab_size_bytes);
795
70267a7f 796 htab_address = __va(table);
1da177e4
LT
797
798 /* htab absolute addr + encoded htabsize */
799 _SDR1 = table + __ilog2(pteg_count) - 11;
800
801 /* Initialize the HPT with no entries */
802 memset((void *)table, 0, htab_size_bytes);
799d6046 803
50de596d
AK
804 if (!cpu_has_feature(CPU_FTR_ARCH_300))
805 /* Set SDR1 */
806 mtspr(SPRN_SDR1, _SDR1);
807 else
808 hash_init_partition_table(table, pteg_count);
1da177e4
LT
809 }
810
f5ea64dc 811 prot = pgprot_val(PAGE_KERNEL);
1da177e4 812
370a908d 813#ifdef CONFIG_DEBUG_PAGEALLOC
e7df0d88
JK
814 if (debug_pagealloc_enabled()) {
815 linear_map_hash_count = memblock_end_of_DRAM() >> PAGE_SHIFT;
816 linear_map_hash_slots = __va(memblock_alloc_base(
817 linear_map_hash_count, 1, ppc64_rma_size));
818 memset(linear_map_hash_slots, 0, linear_map_hash_count);
819 }
370a908d
BH
820#endif /* CONFIG_DEBUG_PAGEALLOC */
821
1da177e4
LT
822 /* On U3 based machines, we need to reserve the DART area and
823 * _NOT_ map it to avoid cache paradoxes as it's remapped non
824 * cacheable later on
825 */
1da177e4
LT
826
827 /* create bolted the linear mapping in the hash table */
28be7072
BH
828 for_each_memblock(memory, reg) {
829 base = (unsigned long)__va(reg->base);
830 size = reg->size;
1da177e4 831
5c339919 832 DBG("creating mapping for region: %lx..%lx (prot: %lx)\n",
9e88ba4e 833 base, size, prot);
1da177e4
LT
834
835#ifdef CONFIG_U3_DART
836 /* Do not map the DART space. Fortunately, it will be aligned
95f72d1e 837 * in such a way that it will not cross two memblock regions and
3c726f8d
BH
838 * will fit within a single 16Mb page.
839 * The DART space is assumed to be a full 16Mb region even if
840 * we only use 2Mb of that space. We will use more of it later
841 * for AGP GART. We have to use a full 16Mb large page.
1da177e4
LT
842 */
843 DBG("DART base: %lx\n", dart_tablebase);
844
845 if (dart_tablebase != 0 && dart_tablebase >= base
846 && dart_tablebase < (base + size)) {
caf80e57 847 unsigned long dart_table_end = dart_tablebase + 16 * MB;
1da177e4 848 if (base != dart_tablebase)
3c726f8d 849 BUG_ON(htab_bolt_mapping(base, dart_tablebase,
9e88ba4e 850 __pa(base), prot,
1189be65
PM
851 mmu_linear_psize,
852 mmu_kernel_ssize));
caf80e57 853 if ((base + size) > dart_table_end)
3c726f8d 854 BUG_ON(htab_bolt_mapping(dart_tablebase+16*MB,
caf80e57
ME
855 base + size,
856 __pa(dart_table_end),
9e88ba4e 857 prot,
1189be65
PM
858 mmu_linear_psize,
859 mmu_kernel_ssize));
1da177e4
LT
860 continue;
861 }
862#endif /* CONFIG_U3_DART */
caf80e57 863 BUG_ON(htab_bolt_mapping(base, base + size, __pa(base),
9e88ba4e 864 prot, mmu_linear_psize, mmu_kernel_ssize));
e63075a3
BH
865 }
866 memblock_set_current_limit(MEMBLOCK_ALLOC_ANYWHERE);
1da177e4
LT
867
868 /*
869 * If we have a memory_limit and we've allocated TCEs then we need to
870 * explicitly map the TCE area at the top of RAM. We also cope with the
871 * case that the TCEs start below memory_limit.
872 * tce_alloc_start/end are 16MB aligned so the mapping should work
873 * for either 4K or 16MB pages.
874 */
875 if (tce_alloc_start) {
b5666f70
ME
876 tce_alloc_start = (unsigned long)__va(tce_alloc_start);
877 tce_alloc_end = (unsigned long)__va(tce_alloc_end);
1da177e4
LT
878
879 if (base + size >= tce_alloc_start)
880 tce_alloc_start = base + size + 1;
881
caf80e57 882 BUG_ON(htab_bolt_mapping(tce_alloc_start, tce_alloc_end,
bc033b63 883 __pa(tce_alloc_start), prot,
1189be65 884 mmu_linear_psize, mmu_kernel_ssize));
1da177e4
LT
885 }
886
7d0daae4 887
1da177e4
LT
888 DBG(" <- htab_initialize()\n");
889}
890#undef KB
891#undef MB
1da177e4 892
756d08d1 893void __init hash__early_init_mmu(void)
799d6046 894{
dd1842a2
AK
895 /*
896 * initialize page table size
897 */
5ed7ecd0
AK
898 __pte_frag_nr = H_PTE_FRAG_NR;
899 __pte_frag_size_shift = H_PTE_FRAG_SIZE_SHIFT;
900
dd1842a2
AK
901 __pte_index_size = H_PTE_INDEX_SIZE;
902 __pmd_index_size = H_PMD_INDEX_SIZE;
903 __pud_index_size = H_PUD_INDEX_SIZE;
904 __pgd_index_size = H_PGD_INDEX_SIZE;
905 __pmd_cache_index = H_PMD_CACHE_INDEX;
906 __pte_table_size = H_PTE_TABLE_SIZE;
907 __pmd_table_size = H_PMD_TABLE_SIZE;
908 __pud_table_size = H_PUD_TABLE_SIZE;
909 __pgd_table_size = H_PGD_TABLE_SIZE;
a2f41eb9
AK
910 /*
911 * 4k use hugepd format, so for hash set then to
912 * zero
913 */
914 __pmd_val_bits = 0;
915 __pud_val_bits = 0;
916 __pgd_val_bits = 0;
d6a9996e
AK
917
918 __kernel_virt_start = H_KERN_VIRT_START;
919 __kernel_virt_size = H_KERN_VIRT_SIZE;
920 __vmalloc_start = H_VMALLOC_START;
921 __vmalloc_end = H_VMALLOC_END;
922 vmemmap = (struct page *)H_VMEMMAP_BASE;
923 ioremap_bot = IOREMAP_BASE;
924
757c74d2 925 /* Initialize the MMU Hash table and create the linear mapping
376af594
ME
926 * of memory. Has to be done before SLB initialization as this is
927 * currently where the page size encoding is obtained.
757c74d2
BH
928 */
929 htab_initialize();
930
376af594 931 /* Initialize SLB management */
13b3d13b 932 slb_initialize();
757c74d2
BH
933}
934
935#ifdef CONFIG_SMP
756d08d1 936void hash__early_init_mmu_secondary(void)
757c74d2
BH
937{
938 /* Initialize hash table for that CPU */
b5dcc609
AK
939 if (!firmware_has_feature(FW_FEATURE_LPAR)) {
940 if (!cpu_has_feature(CPU_FTR_ARCH_300))
941 mtspr(SPRN_SDR1, _SDR1);
942 else
943 mtspr(SPRN_PTCR,
944 __pa(partition_tb) | (PATB_SIZE_SHIFT - 12));
945 }
376af594 946 /* Initialize SLB */
13b3d13b 947 slb_initialize();
799d6046 948}
757c74d2 949#endif /* CONFIG_SMP */
799d6046 950
1da177e4
LT
951/*
952 * Called by asm hashtable.S for doing lazy icache flush
953 */
954unsigned int hash_page_do_lazy_icache(unsigned int pp, pte_t pte, int trap)
955{
956 struct page *page;
957
76c8e25b
BH
958 if (!pfn_valid(pte_pfn(pte)))
959 return pp;
960
1da177e4
LT
961 page = pte_page(pte);
962
963 /* page is dirty */
964 if (!test_bit(PG_arch_1, &page->flags) && !PageReserved(page)) {
965 if (trap == 0x400) {
0895ecda 966 flush_dcache_icache_page(page);
1da177e4
LT
967 set_bit(PG_arch_1, &page->flags);
968 } else
3c726f8d 969 pp |= HPTE_R_N;
1da177e4
LT
970 }
971 return pp;
972}
973
3a8247cc 974#ifdef CONFIG_PPC_MM_SLICES
e51df2c1 975static unsigned int get_paca_psize(unsigned long addr)
3a8247cc 976{
7aa0727f
AK
977 u64 lpsizes;
978 unsigned char *hpsizes;
979 unsigned long index, mask_index;
3a8247cc
PM
980
981 if (addr < SLICE_LOW_TOP) {
2fc251a8 982 lpsizes = get_paca()->mm_ctx_low_slices_psize;
3a8247cc 983 index = GET_LOW_SLICE_INDEX(addr);
7aa0727f 984 return (lpsizes >> (index * 4)) & 0xF;
3a8247cc 985 }
2fc251a8 986 hpsizes = get_paca()->mm_ctx_high_slices_psize;
7aa0727f
AK
987 index = GET_HIGH_SLICE_INDEX(addr);
988 mask_index = index & 0x1;
989 return (hpsizes[index >> 1] >> (mask_index * 4)) & 0xF;
3a8247cc
PM
990}
991
992#else
993unsigned int get_paca_psize(unsigned long addr)
994{
c33e54fa 995 return get_paca()->mm_ctx_user_psize;
3a8247cc
PM
996}
997#endif
998
721151d0
PM
999/*
1000 * Demote a segment to using 4k pages.
1001 * For now this makes the whole process use 4k pages.
1002 */
721151d0 1003#ifdef CONFIG_PPC_64K_PAGES
fa28237c 1004void demote_segment_4k(struct mm_struct *mm, unsigned long addr)
16f1c746 1005{
3a8247cc 1006 if (get_slice_psize(mm, addr) == MMU_PAGE_4K)
721151d0 1007 return;
3a8247cc 1008 slice_set_range_psize(mm, addr, 1, MMU_PAGE_4K);
be3ebfe8 1009 copro_flush_all_slbs(mm);
a1dca346 1010 if ((get_paca_psize(addr) != MMU_PAGE_4K) && (current->mm == mm)) {
c395465d
MN
1011
1012 copy_mm_to_paca(&mm->context);
fa28237c
PM
1013 slb_flush_and_rebolt();
1014 }
721151d0 1015}
16f1c746 1016#endif /* CONFIG_PPC_64K_PAGES */
721151d0 1017
fa28237c
PM
1018#ifdef CONFIG_PPC_SUBPAGE_PROT
1019/*
1020 * This looks up a 2-bit protection code for a 4k subpage of a 64k page.
1021 * Userspace sets the subpage permissions using the subpage_prot system call.
1022 *
1023 * Result is 0: full permissions, _PAGE_RW: read-only,
73a1441a 1024 * _PAGE_RWX: no access.
fa28237c 1025 */
d28513bc 1026static int subpage_protection(struct mm_struct *mm, unsigned long ea)
fa28237c 1027{
d28513bc 1028 struct subpage_prot_table *spt = &mm->context.spt;
fa28237c
PM
1029 u32 spp = 0;
1030 u32 **sbpm, *sbpp;
1031
1032 if (ea >= spt->maxaddr)
1033 return 0;
b0d436c7 1034 if (ea < 0x100000000UL) {
fa28237c
PM
1035 /* addresses below 4GB use spt->low_prot */
1036 sbpm = spt->low_prot;
1037 } else {
1038 sbpm = spt->protptrs[ea >> SBP_L3_SHIFT];
1039 if (!sbpm)
1040 return 0;
1041 }
1042 sbpp = sbpm[(ea >> SBP_L2_SHIFT) & (SBP_L2_COUNT - 1)];
1043 if (!sbpp)
1044 return 0;
1045 spp = sbpp[(ea >> PAGE_SHIFT) & (SBP_L1_COUNT - 1)];
1046
1047 /* extract 2-bit bitfield for this 4k subpage */
1048 spp >>= 30 - 2 * ((ea >> 12) & 0xf);
1049
73a1441a
AK
1050 /*
1051 * 0 -> full premission
1052 * 1 -> Read only
1053 * 2 -> no access.
1054 * We return the flag that need to be cleared.
1055 */
1056 spp = ((spp & 2) ? _PAGE_RWX : 0) | ((spp & 1) ? _PAGE_WRITE : 0);
fa28237c
PM
1057 return spp;
1058}
1059
1060#else /* CONFIG_PPC_SUBPAGE_PROT */
d28513bc 1061static inline int subpage_protection(struct mm_struct *mm, unsigned long ea)
fa28237c
PM
1062{
1063 return 0;
1064}
1065#endif
1066
4b8692c0
BH
1067void hash_failure_debug(unsigned long ea, unsigned long access,
1068 unsigned long vsid, unsigned long trap,
d8139ebf 1069 int ssize, int psize, int lpsize, unsigned long pte)
4b8692c0
BH
1070{
1071 if (!printk_ratelimit())
1072 return;
1073 pr_info("mm: Hashing failure ! EA=0x%lx access=0x%lx current=%s\n",
1074 ea, access, current->comm);
d8139ebf
AK
1075 pr_info(" trap=0x%lx vsid=0x%lx ssize=%d base psize=%d psize %d pte=0x%lx\n",
1076 trap, vsid, ssize, psize, lpsize, pte);
4b8692c0
BH
1077}
1078
09567e7f
ME
1079static void check_paca_psize(unsigned long ea, struct mm_struct *mm,
1080 int psize, bool user_region)
1081{
1082 if (user_region) {
1083 if (psize != get_paca_psize(ea)) {
c395465d 1084 copy_mm_to_paca(&mm->context);
09567e7f
ME
1085 slb_flush_and_rebolt();
1086 }
1087 } else if (get_paca()->vmalloc_sllp !=
1088 mmu_psize_defs[mmu_vmalloc_psize].sllp) {
1089 get_paca()->vmalloc_sllp =
1090 mmu_psize_defs[mmu_vmalloc_psize].sllp;
1091 slb_vmalloc_update();
1092 }
1093}
1094
1da177e4
LT
1095/* Result code is:
1096 * 0 - handled
1097 * 1 - normal page fault
1098 * -1 - critical hash insertion error
fa28237c 1099 * -2 - access not permitted by subpage protection mechanism
1da177e4 1100 */
aefa5688
AK
1101int hash_page_mm(struct mm_struct *mm, unsigned long ea,
1102 unsigned long access, unsigned long trap,
1103 unsigned long flags)
1da177e4 1104{
891121e6 1105 bool is_thp;
ba12eede 1106 enum ctx_state prev_state = exception_enter();
a1128f8f 1107 pgd_t *pgdir;
1da177e4 1108 unsigned long vsid;
1da177e4 1109 pte_t *ptep;
a4fe3ce7 1110 unsigned hugeshift;
56aa4129 1111 const struct cpumask *tmp;
aefa5688 1112 int rc, user_region = 0;
1189be65 1113 int psize, ssize;
1da177e4 1114
3c726f8d
BH
1115 DBG_LOW("hash_page(ea=%016lx, access=%lx, trap=%lx\n",
1116 ea, access, trap);
cfcb3d80 1117 trace_hash_fault(ea, access, trap);
1f8d419e 1118
3c726f8d 1119 /* Get region & vsid */
1da177e4
LT
1120 switch (REGION_ID(ea)) {
1121 case USER_REGION_ID:
1122 user_region = 1;
3c726f8d
BH
1123 if (! mm) {
1124 DBG_LOW(" user region with no mm !\n");
ba12eede
LZ
1125 rc = 1;
1126 goto bail;
3c726f8d 1127 }
16c2d476 1128 psize = get_slice_psize(mm, ea);
1189be65
PM
1129 ssize = user_segment_size(ea);
1130 vsid = get_vsid(mm->context.id, ea, ssize);
1da177e4 1131 break;
1da177e4 1132 case VMALLOC_REGION_ID:
1189be65 1133 vsid = get_kernel_vsid(ea, mmu_kernel_ssize);
bf72aeba
PM
1134 if (ea < VMALLOC_END)
1135 psize = mmu_vmalloc_psize;
1136 else
1137 psize = mmu_io_psize;
1189be65 1138 ssize = mmu_kernel_ssize;
1da177e4 1139 break;
1da177e4
LT
1140 default:
1141 /* Not a valid range
1142 * Send the problem up to do_page_fault
1143 */
ba12eede
LZ
1144 rc = 1;
1145 goto bail;
1da177e4 1146 }
3c726f8d 1147 DBG_LOW(" mm=%p, mm->pgdir=%p, vsid=%016lx\n", mm, mm->pgd, vsid);
1da177e4 1148
c60ac569
AK
1149 /* Bad address. */
1150 if (!vsid) {
1151 DBG_LOW("Bad address!\n");
ba12eede
LZ
1152 rc = 1;
1153 goto bail;
c60ac569 1154 }
3c726f8d 1155 /* Get pgdir */
1da177e4 1156 pgdir = mm->pgd;
ba12eede
LZ
1157 if (pgdir == NULL) {
1158 rc = 1;
1159 goto bail;
1160 }
1da177e4 1161
3c726f8d 1162 /* Check CPU locality */
56aa4129
RR
1163 tmp = cpumask_of(smp_processor_id());
1164 if (user_region && cpumask_equal(mm_cpumask(mm), tmp))
aefa5688 1165 flags |= HPTE_LOCAL_UPDATE;
1da177e4 1166
16c2d476 1167#ifndef CONFIG_PPC_64K_PAGES
a4fe3ce7
DG
1168 /* If we use 4K pages and our psize is not 4K, then we might
1169 * be hitting a special driver mapping, and need to align the
1170 * address before we fetch the PTE.
1171 *
1172 * It could also be a hugepage mapping, in which case this is
1173 * not necessary, but it's not harmful, either.
16c2d476
BH
1174 */
1175 if (psize != MMU_PAGE_4K)
1176 ea &= ~((1ul << mmu_psize_defs[psize].shift) - 1);
1177#endif /* CONFIG_PPC_64K_PAGES */
1178
3c726f8d 1179 /* Get PTE and page size from page tables */
891121e6 1180 ptep = __find_linux_pte_or_hugepte(pgdir, ea, &is_thp, &hugeshift);
3c726f8d
BH
1181 if (ptep == NULL || !pte_present(*ptep)) {
1182 DBG_LOW(" no PTE !\n");
ba12eede
LZ
1183 rc = 1;
1184 goto bail;
3c726f8d
BH
1185 }
1186
ca91e6c0
BH
1187 /* Add _PAGE_PRESENT to the required access perm */
1188 access |= _PAGE_PRESENT;
1189
1190 /* Pre-check access permissions (will be re-checked atomically
1191 * in __hash_page_XX but this pre-check is a fast path
1192 */
ac29c640 1193 if (!check_pte_access(access, pte_val(*ptep))) {
ca91e6c0 1194 DBG_LOW(" no access !\n");
ba12eede
LZ
1195 rc = 1;
1196 goto bail;
ca91e6c0
BH
1197 }
1198
ba12eede 1199 if (hugeshift) {
891121e6 1200 if (is_thp)
6d492ecc 1201 rc = __hash_page_thp(ea, access, vsid, (pmd_t *)ptep,
aefa5688 1202 trap, flags, ssize, psize);
6d492ecc
AK
1203#ifdef CONFIG_HUGETLB_PAGE
1204 else
1205 rc = __hash_page_huge(ea, access, vsid, ptep, trap,
aefa5688 1206 flags, ssize, hugeshift, psize);
6d492ecc
AK
1207#else
1208 else {
1209 /*
1210 * if we have hugeshift, and is not transhuge with
1211 * hugetlb disabled, something is really wrong.
1212 */
1213 rc = 1;
1214 WARN_ON(1);
1215 }
1216#endif
a1dca346
IM
1217 if (current->mm == mm)
1218 check_paca_psize(ea, mm, psize, user_region);
09567e7f 1219
ba12eede
LZ
1220 goto bail;
1221 }
a4fe3ce7 1222
3c726f8d
BH
1223#ifndef CONFIG_PPC_64K_PAGES
1224 DBG_LOW(" i-pte: %016lx\n", pte_val(*ptep));
1225#else
1226 DBG_LOW(" i-pte: %016lx %016lx\n", pte_val(*ptep),
1227 pte_val(*(ptep + PTRS_PER_PTE)));
1228#endif
3c726f8d 1229 /* Do actual hashing */
16c2d476 1230#ifdef CONFIG_PPC_64K_PAGES
945537df
AK
1231 /* If H_PAGE_4K_PFN is set, make sure this is a 4k segment */
1232 if ((pte_val(*ptep) & H_PAGE_4K_PFN) && psize == MMU_PAGE_64K) {
721151d0
PM
1233 demote_segment_4k(mm, ea);
1234 psize = MMU_PAGE_4K;
1235 }
1236
16f1c746
BH
1237 /* If this PTE is non-cacheable and we have restrictions on
1238 * using non cacheable large pages, then we switch to 4k
1239 */
30bda41a 1240 if (mmu_ci_restrictions && psize == MMU_PAGE_64K && pte_ci(*ptep)) {
16f1c746
BH
1241 if (user_region) {
1242 demote_segment_4k(mm, ea);
1243 psize = MMU_PAGE_4K;
1244 } else if (ea < VMALLOC_END) {
1245 /*
1246 * some driver did a non-cacheable mapping
1247 * in vmalloc space, so switch vmalloc
1248 * to 4k pages
1249 */
1250 printk(KERN_ALERT "Reducing vmalloc segment "
1251 "to 4kB pages because of "
1252 "non-cacheable mapping\n");
1253 psize = mmu_vmalloc_psize = MMU_PAGE_4K;
be3ebfe8 1254 copro_flush_all_slbs(mm);
bf72aeba 1255 }
16f1c746 1256 }
09567e7f 1257
0863d7f2
AK
1258#endif /* CONFIG_PPC_64K_PAGES */
1259
a1dca346
IM
1260 if (current->mm == mm)
1261 check_paca_psize(ea, mm, psize, user_region);
16f1c746 1262
73b341ef 1263#ifdef CONFIG_PPC_64K_PAGES
bf72aeba 1264 if (psize == MMU_PAGE_64K)
aefa5688
AK
1265 rc = __hash_page_64K(ea, access, vsid, ptep, trap,
1266 flags, ssize);
3c726f8d 1267 else
73b341ef 1268#endif /* CONFIG_PPC_64K_PAGES */
fa28237c 1269 {
a1128f8f 1270 int spp = subpage_protection(mm, ea);
fa28237c
PM
1271 if (access & spp)
1272 rc = -2;
1273 else
1274 rc = __hash_page_4K(ea, access, vsid, ptep, trap,
aefa5688 1275 flags, ssize, spp);
fa28237c 1276 }
3c726f8d 1277
4b8692c0
BH
1278 /* Dump some info in case of hash insertion failure, they should
1279 * never happen so it is really useful to know if/when they do
1280 */
1281 if (rc == -1)
1282 hash_failure_debug(ea, access, vsid, trap, ssize, psize,
d8139ebf 1283 psize, pte_val(*ptep));
3c726f8d
BH
1284#ifndef CONFIG_PPC_64K_PAGES
1285 DBG_LOW(" o-pte: %016lx\n", pte_val(*ptep));
1286#else
1287 DBG_LOW(" o-pte: %016lx %016lx\n", pte_val(*ptep),
1288 pte_val(*(ptep + PTRS_PER_PTE)));
1289#endif
1290 DBG_LOW(" -> rc=%d\n", rc);
ba12eede
LZ
1291
1292bail:
1293 exception_exit(prev_state);
3c726f8d 1294 return rc;
1da177e4 1295}
a1dca346
IM
1296EXPORT_SYMBOL_GPL(hash_page_mm);
1297
aefa5688
AK
1298int hash_page(unsigned long ea, unsigned long access, unsigned long trap,
1299 unsigned long dsisr)
a1dca346 1300{
aefa5688 1301 unsigned long flags = 0;
a1dca346
IM
1302 struct mm_struct *mm = current->mm;
1303
1304 if (REGION_ID(ea) == VMALLOC_REGION_ID)
1305 mm = &init_mm;
1306
aefa5688
AK
1307 if (dsisr & DSISR_NOHPTE)
1308 flags |= HPTE_NOHPTE_UPDATE;
1309
1310 return hash_page_mm(mm, ea, access, trap, flags);
a1dca346 1311}
67207b96 1312EXPORT_SYMBOL_GPL(hash_page);
1da177e4 1313
106713a1
AK
1314int __hash_page(unsigned long ea, unsigned long msr, unsigned long trap,
1315 unsigned long dsisr)
1316{
c7d54842 1317 unsigned long access = _PAGE_PRESENT | _PAGE_READ;
106713a1
AK
1318 unsigned long flags = 0;
1319 struct mm_struct *mm = current->mm;
1320
1321 if (REGION_ID(ea) == VMALLOC_REGION_ID)
1322 mm = &init_mm;
1323
1324 if (dsisr & DSISR_NOHPTE)
1325 flags |= HPTE_NOHPTE_UPDATE;
1326
1327 if (dsisr & DSISR_ISSTORE)
c7d54842 1328 access |= _PAGE_WRITE;
106713a1 1329 /*
ac29c640
AK
1330 * We set _PAGE_PRIVILEGED only when
1331 * kernel mode access kernel space.
1332 *
1333 * _PAGE_PRIVILEGED is NOT set
1334 * 1) when kernel mode access user space
1335 * 2) user space access kernel space.
106713a1 1336 */
ac29c640 1337 access |= _PAGE_PRIVILEGED;
106713a1 1338 if ((msr & MSR_PR) || (REGION_ID(ea) == USER_REGION_ID))
ac29c640 1339 access &= ~_PAGE_PRIVILEGED;
106713a1
AK
1340
1341 if (trap == 0x400)
1342 access |= _PAGE_EXEC;
1343
1344 return hash_page_mm(mm, ea, access, trap, flags);
1345}
1346
8bbc9b7b
ME
1347#ifdef CONFIG_PPC_MM_SLICES
1348static bool should_hash_preload(struct mm_struct *mm, unsigned long ea)
1349{
aac55d75
ME
1350 int psize = get_slice_psize(mm, ea);
1351
8bbc9b7b 1352 /* We only prefault standard pages for now */
aac55d75
ME
1353 if (unlikely(psize != mm->context.user_psize))
1354 return false;
1355
1356 /*
1357 * Don't prefault if subpage protection is enabled for the EA.
1358 */
1359 if (unlikely((psize == MMU_PAGE_4K) && subpage_protection(mm, ea)))
8bbc9b7b
ME
1360 return false;
1361
1362 return true;
1363}
1364#else
1365static bool should_hash_preload(struct mm_struct *mm, unsigned long ea)
1366{
1367 return true;
1368}
1369#endif
1370
3c726f8d
BH
1371void hash_preload(struct mm_struct *mm, unsigned long ea,
1372 unsigned long access, unsigned long trap)
1da177e4 1373{
12bc9f6f 1374 int hugepage_shift;
3c726f8d 1375 unsigned long vsid;
0b97fee0 1376 pgd_t *pgdir;
3c726f8d 1377 pte_t *ptep;
3c726f8d 1378 unsigned long flags;
aefa5688 1379 int rc, ssize, update_flags = 0;
3c726f8d 1380
d0f13e3c
BH
1381 BUG_ON(REGION_ID(ea) != USER_REGION_ID);
1382
8bbc9b7b 1383 if (!should_hash_preload(mm, ea))
3c726f8d
BH
1384 return;
1385
1386 DBG_LOW("hash_preload(mm=%p, mm->pgdir=%p, ea=%016lx, access=%lx,"
1387 " trap=%lx\n", mm, mm->pgd, ea, access, trap);
1da177e4 1388
16f1c746 1389 /* Get Linux PTE if available */
3c726f8d
BH
1390 pgdir = mm->pgd;
1391 if (pgdir == NULL)
1392 return;
0ac52dd7
AK
1393
1394 /* Get VSID */
1395 ssize = user_segment_size(ea);
1396 vsid = get_vsid(mm->context.id, ea, ssize);
1397 if (!vsid)
1398 return;
1399 /*
1400 * Hash doesn't like irqs. Walking linux page table with irq disabled
1401 * saves us from holding multiple locks.
1402 */
1403 local_irq_save(flags);
1404
12bc9f6f
AK
1405 /*
1406 * THP pages use update_mmu_cache_pmd. We don't do
1407 * hash preload there. Hence can ignore THP here
1408 */
891121e6 1409 ptep = find_linux_pte_or_hugepte(pgdir, ea, NULL, &hugepage_shift);
3c726f8d 1410 if (!ptep)
0ac52dd7 1411 goto out_exit;
16f1c746 1412
12bc9f6f 1413 WARN_ON(hugepage_shift);
16f1c746 1414#ifdef CONFIG_PPC_64K_PAGES
945537df 1415 /* If either H_PAGE_4K_PFN or cache inhibited is set (and we are on
16f1c746
BH
1416 * a 64K kernel), then we don't preload, hash_page() will take
1417 * care of it once we actually try to access the page.
1418 * That way we don't have to duplicate all of the logic for segment
1419 * page size demotion here
1420 */
945537df 1421 if ((pte_val(*ptep) & H_PAGE_4K_PFN) || pte_ci(*ptep))
0ac52dd7 1422 goto out_exit;
16f1c746
BH
1423#endif /* CONFIG_PPC_64K_PAGES */
1424
16c2d476 1425 /* Is that local to this CPU ? */
56aa4129 1426 if (cpumask_equal(mm_cpumask(mm), cpumask_of(smp_processor_id())))
aefa5688 1427 update_flags |= HPTE_LOCAL_UPDATE;
16c2d476
BH
1428
1429 /* Hash it in */
73b341ef 1430#ifdef CONFIG_PPC_64K_PAGES
bf72aeba 1431 if (mm->context.user_psize == MMU_PAGE_64K)
aefa5688
AK
1432 rc = __hash_page_64K(ea, access, vsid, ptep, trap,
1433 update_flags, ssize);
1da177e4 1434 else
73b341ef 1435#endif /* CONFIG_PPC_64K_PAGES */
aefa5688
AK
1436 rc = __hash_page_4K(ea, access, vsid, ptep, trap, update_flags,
1437 ssize, subpage_protection(mm, ea));
4b8692c0
BH
1438
1439 /* Dump some info in case of hash insertion failure, they should
1440 * never happen so it is really useful to know if/when they do
1441 */
1442 if (rc == -1)
1443 hash_failure_debug(ea, access, vsid, trap, ssize,
d8139ebf
AK
1444 mm->context.user_psize,
1445 mm->context.user_psize,
1446 pte_val(*ptep));
0ac52dd7 1447out_exit:
3c726f8d
BH
1448 local_irq_restore(flags);
1449}
1450
f6ab0b92
BH
1451/* WARNING: This is called from hash_low_64.S, if you change this prototype,
1452 * do not forget to update the assembly call site !
1453 */
5524a27d 1454void flush_hash_page(unsigned long vpn, real_pte_t pte, int psize, int ssize,
aefa5688 1455 unsigned long flags)
3c726f8d
BH
1456{
1457 unsigned long hash, index, shift, hidx, slot;
aefa5688 1458 int local = flags & HPTE_LOCAL_UPDATE;
3c726f8d 1459
5524a27d
AK
1460 DBG_LOW("flush_hash_page(vpn=%016lx)\n", vpn);
1461 pte_iterate_hashed_subpages(pte, psize, vpn, index, shift) {
1462 hash = hpt_hash(vpn, shift, ssize);
3c726f8d
BH
1463 hidx = __rpte_to_hidx(pte, index);
1464 if (hidx & _PTEIDX_SECONDARY)
1465 hash = ~hash;
1466 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
1467 slot += hidx & _PTEIDX_GROUP_IX;
5c339919 1468 DBG_LOW(" sub %ld: hash=%lx, hidx=%lx\n", index, slot, hidx);
db3d8534
AK
1469 /*
1470 * We use same base page size and actual psize, because we don't
1471 * use these functions for hugepage
1472 */
1473 ppc_md.hpte_invalidate(slot, vpn, psize, psize, ssize, local);
3c726f8d 1474 } pte_iterate_hashed_end();
bc2a9408
MN
1475
1476#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1477 /* Transactions are not aborted by tlbiel, only tlbie.
1478 * Without, syncing a page back to a block device w/ PIO could pick up
1479 * transactional data (bad!) so we force an abort here. Before the
1480 * sync the page will be made read-only, which will flush_hash_page.
1481 * BIG ISSUE here: if the kernel uses a page from userspace without
1482 * unmapping it first, it may see the speculated version.
1483 */
1484 if (local && cpu_has_feature(CPU_FTR_TM) &&
c2fd22df 1485 current->thread.regs &&
bc2a9408
MN
1486 MSR_TM_ACTIVE(current->thread.regs->msr)) {
1487 tm_enable();
1488 tm_abort(TM_CAUSE_TLBI);
1489 }
1490#endif
1da177e4
LT
1491}
1492
f1581bf1
AK
1493#ifdef CONFIG_TRANSPARENT_HUGEPAGE
1494void flush_hash_hugepage(unsigned long vsid, unsigned long addr,
aefa5688
AK
1495 pmd_t *pmdp, unsigned int psize, int ssize,
1496 unsigned long flags)
f1581bf1
AK
1497{
1498 int i, max_hpte_count, valid;
1499 unsigned long s_addr;
1500 unsigned char *hpte_slot_array;
1501 unsigned long hidx, shift, vpn, hash, slot;
aefa5688 1502 int local = flags & HPTE_LOCAL_UPDATE;
f1581bf1
AK
1503
1504 s_addr = addr & HPAGE_PMD_MASK;
1505 hpte_slot_array = get_hpte_slot_array(pmdp);
1506 /*
1507 * IF we try to do a HUGE PTE update after a withdraw is done.
1508 * we will find the below NULL. This happens when we do
1509 * split_huge_page_pmd
1510 */
1511 if (!hpte_slot_array)
1512 return;
1513
d557b098
AK
1514 if (ppc_md.hugepage_invalidate) {
1515 ppc_md.hugepage_invalidate(vsid, s_addr, hpte_slot_array,
1516 psize, ssize, local);
1517 goto tm_abort;
1518 }
f1581bf1
AK
1519 /*
1520 * No bluk hpte removal support, invalidate each entry
1521 */
1522 shift = mmu_psize_defs[psize].shift;
1523 max_hpte_count = HPAGE_PMD_SIZE >> shift;
1524 for (i = 0; i < max_hpte_count; i++) {
1525 /*
1526 * 8 bits per each hpte entries
1527 * 000| [ secondary group (one bit) | hidx (3 bits) | valid bit]
1528 */
1529 valid = hpte_valid(hpte_slot_array, i);
1530 if (!valid)
1531 continue;
1532 hidx = hpte_hash_index(hpte_slot_array, i);
1533
1534 /* get the vpn */
1535 addr = s_addr + (i * (1ul << shift));
1536 vpn = hpt_vpn(addr, vsid, ssize);
1537 hash = hpt_hash(vpn, shift, ssize);
1538 if (hidx & _PTEIDX_SECONDARY)
1539 hash = ~hash;
1540
1541 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
1542 slot += hidx & _PTEIDX_GROUP_IX;
1543 ppc_md.hpte_invalidate(slot, vpn, psize,
d557b098
AK
1544 MMU_PAGE_16M, ssize, local);
1545 }
1546tm_abort:
1547#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1548 /* Transactions are not aborted by tlbiel, only tlbie.
1549 * Without, syncing a page back to a block device w/ PIO could pick up
1550 * transactional data (bad!) so we force an abort here. Before the
1551 * sync the page will be made read-only, which will flush_hash_page.
1552 * BIG ISSUE here: if the kernel uses a page from userspace without
1553 * unmapping it first, it may see the speculated version.
1554 */
1555 if (local && cpu_has_feature(CPU_FTR_TM) &&
1556 current->thread.regs &&
1557 MSR_TM_ACTIVE(current->thread.regs->msr)) {
1558 tm_enable();
1559 tm_abort(TM_CAUSE_TLBI);
f1581bf1 1560 }
d557b098 1561#endif
2e826695 1562 return;
f1581bf1
AK
1563}
1564#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
1565
61b1a942 1566void flush_hash_range(unsigned long number, int local)
1da177e4 1567{
3c726f8d 1568 if (ppc_md.flush_hash_range)
61b1a942 1569 ppc_md.flush_hash_range(number, local);
3c726f8d 1570 else {
1da177e4 1571 int i;
61b1a942 1572 struct ppc64_tlb_batch *batch =
69111bac 1573 this_cpu_ptr(&ppc64_tlb_batch);
1da177e4
LT
1574
1575 for (i = 0; i < number; i++)
5524a27d 1576 flush_hash_page(batch->vpn[i], batch->pte[i],
1189be65 1577 batch->psize, batch->ssize, local);
1da177e4
LT
1578 }
1579}
1580
1da177e4
LT
1581/*
1582 * low_hash_fault is called when we the low level hash code failed
1583 * to instert a PTE due to an hypervisor error
1584 */
fa28237c 1585void low_hash_fault(struct pt_regs *regs, unsigned long address, int rc)
1da177e4 1586{
ba12eede
LZ
1587 enum ctx_state prev_state = exception_enter();
1588
1da177e4 1589 if (user_mode(regs)) {
fa28237c
PM
1590#ifdef CONFIG_PPC_SUBPAGE_PROT
1591 if (rc == -2)
1592 _exception(SIGSEGV, regs, SEGV_ACCERR, address);
1593 else
1594#endif
1595 _exception(SIGBUS, regs, BUS_ADRERR, address);
1596 } else
1597 bad_page_fault(regs, address, SIGBUS);
ba12eede
LZ
1598
1599 exception_exit(prev_state);
1da177e4 1600}
370a908d 1601
b170bd3d
LZ
1602long hpte_insert_repeating(unsigned long hash, unsigned long vpn,
1603 unsigned long pa, unsigned long rflags,
1604 unsigned long vflags, int psize, int ssize)
1605{
1606 unsigned long hpte_group;
1607 long slot;
1608
1609repeat:
1610 hpte_group = ((hash & htab_hash_mask) *
1611 HPTES_PER_GROUP) & ~0x7UL;
1612
1613 /* Insert into the hash table, primary slot */
1614 slot = ppc_md.hpte_insert(hpte_group, vpn, pa, rflags, vflags,
b1022fbd 1615 psize, psize, ssize);
b170bd3d
LZ
1616
1617 /* Primary is full, try the secondary */
1618 if (unlikely(slot == -1)) {
1619 hpte_group = ((~hash & htab_hash_mask) *
1620 HPTES_PER_GROUP) & ~0x7UL;
1621 slot = ppc_md.hpte_insert(hpte_group, vpn, pa, rflags,
1622 vflags | HPTE_V_SECONDARY,
b1022fbd 1623 psize, psize, ssize);
b170bd3d
LZ
1624 if (slot == -1) {
1625 if (mftb() & 0x1)
1626 hpte_group = ((hash & htab_hash_mask) *
1627 HPTES_PER_GROUP)&~0x7UL;
1628
1629 ppc_md.hpte_remove(hpte_group);
1630 goto repeat;
1631 }
1632 }
1633
1634 return slot;
1635}
1636
370a908d
BH
1637#ifdef CONFIG_DEBUG_PAGEALLOC
1638static void kernel_map_linear_page(unsigned long vaddr, unsigned long lmi)
1639{
016af59f 1640 unsigned long hash;
1189be65 1641 unsigned long vsid = get_kernel_vsid(vaddr, mmu_kernel_ssize);
5524a27d 1642 unsigned long vpn = hpt_vpn(vaddr, vsid, mmu_kernel_ssize);
09f3f326 1643 unsigned long mode = htab_convert_pte_flags(pgprot_val(PAGE_KERNEL));
016af59f 1644 long ret;
370a908d 1645
5524a27d 1646 hash = hpt_hash(vpn, PAGE_SHIFT, mmu_kernel_ssize);
370a908d 1647
c60ac569
AK
1648 /* Don't create HPTE entries for bad address */
1649 if (!vsid)
1650 return;
016af59f
LZ
1651
1652 ret = hpte_insert_repeating(hash, vpn, __pa(vaddr), mode,
1653 HPTE_V_BOLTED,
1654 mmu_linear_psize, mmu_kernel_ssize);
1655
370a908d
BH
1656 BUG_ON (ret < 0);
1657 spin_lock(&linear_map_hash_lock);
1658 BUG_ON(linear_map_hash_slots[lmi] & 0x80);
1659 linear_map_hash_slots[lmi] = ret | 0x80;
1660 spin_unlock(&linear_map_hash_lock);
1661}
1662
1663static void kernel_unmap_linear_page(unsigned long vaddr, unsigned long lmi)
1664{
1189be65
PM
1665 unsigned long hash, hidx, slot;
1666 unsigned long vsid = get_kernel_vsid(vaddr, mmu_kernel_ssize);
5524a27d 1667 unsigned long vpn = hpt_vpn(vaddr, vsid, mmu_kernel_ssize);
370a908d 1668
5524a27d 1669 hash = hpt_hash(vpn, PAGE_SHIFT, mmu_kernel_ssize);
370a908d
BH
1670 spin_lock(&linear_map_hash_lock);
1671 BUG_ON(!(linear_map_hash_slots[lmi] & 0x80));
1672 hidx = linear_map_hash_slots[lmi] & 0x7f;
1673 linear_map_hash_slots[lmi] = 0;
1674 spin_unlock(&linear_map_hash_lock);
1675 if (hidx & _PTEIDX_SECONDARY)
1676 hash = ~hash;
1677 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
1678 slot += hidx & _PTEIDX_GROUP_IX;
db3d8534
AK
1679 ppc_md.hpte_invalidate(slot, vpn, mmu_linear_psize, mmu_linear_psize,
1680 mmu_kernel_ssize, 0);
370a908d
BH
1681}
1682
031bc574 1683void __kernel_map_pages(struct page *page, int numpages, int enable)
370a908d
BH
1684{
1685 unsigned long flags, vaddr, lmi;
1686 int i;
1687
1688 local_irq_save(flags);
1689 for (i = 0; i < numpages; i++, page++) {
1690 vaddr = (unsigned long)page_address(page);
1691 lmi = __pa(vaddr) >> PAGE_SHIFT;
1692 if (lmi >= linear_map_hash_count)
1693 continue;
1694 if (enable)
1695 kernel_map_linear_page(vaddr, lmi);
1696 else
1697 kernel_unmap_linear_page(vaddr, lmi);
1698 }
1699 local_irq_restore(flags);
1700}
1701#endif /* CONFIG_DEBUG_PAGEALLOC */
cd3db0c4 1702
756d08d1 1703void hash__setup_initial_memory_limit(phys_addr_t first_memblock_base,
cd3db0c4
BH
1704 phys_addr_t first_memblock_size)
1705{
1706 /* We don't currently support the first MEMBLOCK not mapping 0
1707 * physical on those processors
1708 */
1709 BUG_ON(first_memblock_base != 0);
1710
1711 /* On LPAR systems, the first entry is our RMA region,
1712 * non-LPAR 64-bit hash MMU systems don't have a limitation
1713 * on real mode access, but using the first entry works well
1714 * enough. We also clamp it to 1G to avoid some funky things
1715 * such as RTAS bugs etc...
1716 */
1717 ppc64_rma_size = min_t(u64, first_memblock_size, 0x40000000);
1718
1719 /* Finally limit subsequent allocations */
1720 memblock_set_current_limit(ppc64_rma_size);
1721}