]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - arch/powerpc/mm/hash_utils_64.c
Pull regset into release branch
[mirror_ubuntu-artful-kernel.git] / arch / powerpc / mm / hash_utils_64.c
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
22 #undef DEBUG_LOW
23
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>
30 #include <linux/ctype.h>
31 #include <linux/cache.h>
32 #include <linux/init.h>
33 #include <linux/signal.h>
34
35 #include <asm/processor.h>
36 #include <asm/pgtable.h>
37 #include <asm/mmu.h>
38 #include <asm/mmu_context.h>
39 #include <asm/page.h>
40 #include <asm/types.h>
41 #include <asm/system.h>
42 #include <asm/uaccess.h>
43 #include <asm/machdep.h>
44 #include <asm/lmb.h>
45 #include <asm/abs_addr.h>
46 #include <asm/tlbflush.h>
47 #include <asm/io.h>
48 #include <asm/eeh.h>
49 #include <asm/tlb.h>
50 #include <asm/cacheflush.h>
51 #include <asm/cputable.h>
52 #include <asm/sections.h>
53 #include <asm/spu.h>
54 #include <asm/udbg.h>
55
56 #ifdef DEBUG
57 #define DBG(fmt...) udbg_printf(fmt)
58 #else
59 #define DBG(fmt...)
60 #endif
61
62 #ifdef DEBUG_LOW
63 #define DBG_LOW(fmt...) udbg_printf(fmt)
64 #else
65 #define DBG_LOW(fmt...)
66 #endif
67
68 #define KB (1024)
69 #define MB (1024*KB)
70
71 /*
72 * Note: pte --> Linux PTE
73 * HPTE --> PowerPC Hashed Page Table Entry
74 *
75 * Execution context:
76 * htab_initialize is called with the MMU off (of course), but
77 * the kernel has been copied down to zero so it can directly
78 * reference global data. At this point it is very difficult
79 * to print debug info.
80 *
81 */
82
83 #ifdef CONFIG_U3_DART
84 extern unsigned long dart_tablebase;
85 #endif /* CONFIG_U3_DART */
86
87 static unsigned long _SDR1;
88 struct mmu_psize_def mmu_psize_defs[MMU_PAGE_COUNT];
89
90 struct hash_pte *htab_address;
91 unsigned long htab_size_bytes;
92 unsigned long htab_hash_mask;
93 int mmu_linear_psize = MMU_PAGE_4K;
94 int mmu_virtual_psize = MMU_PAGE_4K;
95 int mmu_vmalloc_psize = MMU_PAGE_4K;
96 int mmu_io_psize = MMU_PAGE_4K;
97 int mmu_kernel_ssize = MMU_SEGSIZE_256M;
98 int mmu_highuser_ssize = MMU_SEGSIZE_256M;
99 u16 mmu_slb_size = 64;
100 #ifdef CONFIG_HUGETLB_PAGE
101 int mmu_huge_psize = MMU_PAGE_16M;
102 unsigned int HPAGE_SHIFT;
103 #endif
104 #ifdef CONFIG_PPC_64K_PAGES
105 int mmu_ci_restrictions;
106 #endif
107 #ifdef CONFIG_DEBUG_PAGEALLOC
108 static u8 *linear_map_hash_slots;
109 static unsigned long linear_map_hash_count;
110 static DEFINE_SPINLOCK(linear_map_hash_lock);
111 #endif /* CONFIG_DEBUG_PAGEALLOC */
112
113 /* There are definitions of page sizes arrays to be used when none
114 * is provided by the firmware.
115 */
116
117 /* Pre-POWER4 CPUs (4k pages only)
118 */
119 struct mmu_psize_def mmu_psize_defaults_old[] = {
120 [MMU_PAGE_4K] = {
121 .shift = 12,
122 .sllp = 0,
123 .penc = 0,
124 .avpnm = 0,
125 .tlbiel = 0,
126 },
127 };
128
129 /* POWER4, GPUL, POWER5
130 *
131 * Support for 16Mb large pages
132 */
133 struct mmu_psize_def mmu_psize_defaults_gp[] = {
134 [MMU_PAGE_4K] = {
135 .shift = 12,
136 .sllp = 0,
137 .penc = 0,
138 .avpnm = 0,
139 .tlbiel = 1,
140 },
141 [MMU_PAGE_16M] = {
142 .shift = 24,
143 .sllp = SLB_VSID_L,
144 .penc = 0,
145 .avpnm = 0x1UL,
146 .tlbiel = 0,
147 },
148 };
149
150
151 int htab_bolt_mapping(unsigned long vstart, unsigned long vend,
152 unsigned long pstart, unsigned long mode,
153 int psize, int ssize)
154 {
155 unsigned long vaddr, paddr;
156 unsigned int step, shift;
157 unsigned long tmp_mode;
158 int ret = 0;
159
160 shift = mmu_psize_defs[psize].shift;
161 step = 1 << shift;
162
163 for (vaddr = vstart, paddr = pstart; vaddr < vend;
164 vaddr += step, paddr += step) {
165 unsigned long hash, hpteg;
166 unsigned long vsid = get_kernel_vsid(vaddr, ssize);
167 unsigned long va = hpt_va(vaddr, vsid, ssize);
168
169 tmp_mode = mode;
170
171 /* Make non-kernel text non-executable */
172 if (!in_kernel_text(vaddr))
173 tmp_mode = mode | HPTE_R_N;
174
175 hash = hpt_hash(va, shift, ssize);
176 hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
177
178 DBG("htab_bolt_mapping: calling %p\n", ppc_md.hpte_insert);
179
180 BUG_ON(!ppc_md.hpte_insert);
181 ret = ppc_md.hpte_insert(hpteg, va, paddr,
182 tmp_mode, HPTE_V_BOLTED, psize, ssize);
183
184 if (ret < 0)
185 break;
186 #ifdef CONFIG_DEBUG_PAGEALLOC
187 if ((paddr >> PAGE_SHIFT) < linear_map_hash_count)
188 linear_map_hash_slots[paddr >> PAGE_SHIFT] = ret | 0x80;
189 #endif /* CONFIG_DEBUG_PAGEALLOC */
190 }
191 return ret < 0 ? ret : 0;
192 }
193
194 static int __init htab_dt_scan_seg_sizes(unsigned long node,
195 const char *uname, int depth,
196 void *data)
197 {
198 char *type = of_get_flat_dt_prop(node, "device_type", NULL);
199 u32 *prop;
200 unsigned long size = 0;
201
202 /* We are scanning "cpu" nodes only */
203 if (type == NULL || strcmp(type, "cpu") != 0)
204 return 0;
205
206 prop = (u32 *)of_get_flat_dt_prop(node, "ibm,processor-segment-sizes",
207 &size);
208 if (prop == NULL)
209 return 0;
210 for (; size >= 4; size -= 4, ++prop) {
211 if (prop[0] == 40) {
212 DBG("1T segment support detected\n");
213 cur_cpu_spec->cpu_features |= CPU_FTR_1T_SEGMENT;
214 return 1;
215 }
216 }
217 cur_cpu_spec->cpu_features &= ~CPU_FTR_NO_SLBIE_B;
218 return 0;
219 }
220
221 static void __init htab_init_seg_sizes(void)
222 {
223 of_scan_flat_dt(htab_dt_scan_seg_sizes, NULL);
224 }
225
226 static int __init htab_dt_scan_page_sizes(unsigned long node,
227 const char *uname, int depth,
228 void *data)
229 {
230 char *type = of_get_flat_dt_prop(node, "device_type", NULL);
231 u32 *prop;
232 unsigned long size = 0;
233
234 /* We are scanning "cpu" nodes only */
235 if (type == NULL || strcmp(type, "cpu") != 0)
236 return 0;
237
238 prop = (u32 *)of_get_flat_dt_prop(node,
239 "ibm,segment-page-sizes", &size);
240 if (prop != NULL) {
241 DBG("Page sizes from device-tree:\n");
242 size /= 4;
243 cur_cpu_spec->cpu_features &= ~(CPU_FTR_16M_PAGE);
244 while(size > 0) {
245 unsigned int shift = prop[0];
246 unsigned int slbenc = prop[1];
247 unsigned int lpnum = prop[2];
248 unsigned int lpenc = 0;
249 struct mmu_psize_def *def;
250 int idx = -1;
251
252 size -= 3; prop += 3;
253 while(size > 0 && lpnum) {
254 if (prop[0] == shift)
255 lpenc = prop[1];
256 prop += 2; size -= 2;
257 lpnum--;
258 }
259 switch(shift) {
260 case 0xc:
261 idx = MMU_PAGE_4K;
262 break;
263 case 0x10:
264 idx = MMU_PAGE_64K;
265 break;
266 case 0x14:
267 idx = MMU_PAGE_1M;
268 break;
269 case 0x18:
270 idx = MMU_PAGE_16M;
271 cur_cpu_spec->cpu_features |= CPU_FTR_16M_PAGE;
272 break;
273 case 0x22:
274 idx = MMU_PAGE_16G;
275 break;
276 }
277 if (idx < 0)
278 continue;
279 def = &mmu_psize_defs[idx];
280 def->shift = shift;
281 if (shift <= 23)
282 def->avpnm = 0;
283 else
284 def->avpnm = (1 << (shift - 23)) - 1;
285 def->sllp = slbenc;
286 def->penc = lpenc;
287 /* We don't know for sure what's up with tlbiel, so
288 * for now we only set it for 4K and 64K pages
289 */
290 if (idx == MMU_PAGE_4K || idx == MMU_PAGE_64K)
291 def->tlbiel = 1;
292 else
293 def->tlbiel = 0;
294
295 DBG(" %d: shift=%02x, sllp=%04x, avpnm=%08x, "
296 "tlbiel=%d, penc=%d\n",
297 idx, shift, def->sllp, def->avpnm, def->tlbiel,
298 def->penc);
299 }
300 return 1;
301 }
302 return 0;
303 }
304
305 static void __init htab_init_page_sizes(void)
306 {
307 int rc;
308
309 /* Default to 4K pages only */
310 memcpy(mmu_psize_defs, mmu_psize_defaults_old,
311 sizeof(mmu_psize_defaults_old));
312
313 /*
314 * Try to find the available page sizes in the device-tree
315 */
316 rc = of_scan_flat_dt(htab_dt_scan_page_sizes, NULL);
317 if (rc != 0) /* Found */
318 goto found;
319
320 /*
321 * Not in the device-tree, let's fallback on known size
322 * list for 16M capable GP & GR
323 */
324 if (cpu_has_feature(CPU_FTR_16M_PAGE))
325 memcpy(mmu_psize_defs, mmu_psize_defaults_gp,
326 sizeof(mmu_psize_defaults_gp));
327 found:
328 #ifndef CONFIG_DEBUG_PAGEALLOC
329 /*
330 * Pick a size for the linear mapping. Currently, we only support
331 * 16M, 1M and 4K which is the default
332 */
333 if (mmu_psize_defs[MMU_PAGE_16M].shift)
334 mmu_linear_psize = MMU_PAGE_16M;
335 else if (mmu_psize_defs[MMU_PAGE_1M].shift)
336 mmu_linear_psize = MMU_PAGE_1M;
337 #endif /* CONFIG_DEBUG_PAGEALLOC */
338
339 #ifdef CONFIG_PPC_64K_PAGES
340 /*
341 * Pick a size for the ordinary pages. Default is 4K, we support
342 * 64K for user mappings and vmalloc if supported by the processor.
343 * We only use 64k for ioremap if the processor
344 * (and firmware) support cache-inhibited large pages.
345 * If not, we use 4k and set mmu_ci_restrictions so that
346 * hash_page knows to switch processes that use cache-inhibited
347 * mappings to 4k pages.
348 */
349 if (mmu_psize_defs[MMU_PAGE_64K].shift) {
350 mmu_virtual_psize = MMU_PAGE_64K;
351 mmu_vmalloc_psize = MMU_PAGE_64K;
352 if (mmu_linear_psize == MMU_PAGE_4K)
353 mmu_linear_psize = MMU_PAGE_64K;
354 if (cpu_has_feature(CPU_FTR_CI_LARGE_PAGE)) {
355 /*
356 * Don't use 64k pages for ioremap on pSeries, since
357 * that would stop us accessing the HEA ethernet.
358 */
359 if (!machine_is(pseries))
360 mmu_io_psize = MMU_PAGE_64K;
361 } else
362 mmu_ci_restrictions = 1;
363 }
364 #endif /* CONFIG_PPC_64K_PAGES */
365
366 printk(KERN_DEBUG "Page orders: linear mapping = %d, "
367 "virtual = %d, io = %d\n",
368 mmu_psize_defs[mmu_linear_psize].shift,
369 mmu_psize_defs[mmu_virtual_psize].shift,
370 mmu_psize_defs[mmu_io_psize].shift);
371
372 #ifdef CONFIG_HUGETLB_PAGE
373 /* Init large page size. Currently, we pick 16M or 1M depending
374 * on what is available
375 */
376 if (mmu_psize_defs[MMU_PAGE_16M].shift)
377 set_huge_psize(MMU_PAGE_16M);
378 /* With 4k/4level pagetables, we can't (for now) cope with a
379 * huge page size < PMD_SIZE */
380 else if (mmu_psize_defs[MMU_PAGE_1M].shift)
381 set_huge_psize(MMU_PAGE_1M);
382 #endif /* CONFIG_HUGETLB_PAGE */
383 }
384
385 static int __init htab_dt_scan_pftsize(unsigned long node,
386 const char *uname, int depth,
387 void *data)
388 {
389 char *type = of_get_flat_dt_prop(node, "device_type", NULL);
390 u32 *prop;
391
392 /* We are scanning "cpu" nodes only */
393 if (type == NULL || strcmp(type, "cpu") != 0)
394 return 0;
395
396 prop = (u32 *)of_get_flat_dt_prop(node, "ibm,pft-size", NULL);
397 if (prop != NULL) {
398 /* pft_size[0] is the NUMA CEC cookie */
399 ppc64_pft_size = prop[1];
400 return 1;
401 }
402 return 0;
403 }
404
405 static unsigned long __init htab_get_table_size(void)
406 {
407 unsigned long mem_size, rnd_mem_size, pteg_count;
408
409 /* If hash size isn't already provided by the platform, we try to
410 * retrieve it from the device-tree. If it's not there neither, we
411 * calculate it now based on the total RAM size
412 */
413 if (ppc64_pft_size == 0)
414 of_scan_flat_dt(htab_dt_scan_pftsize, NULL);
415 if (ppc64_pft_size)
416 return 1UL << ppc64_pft_size;
417
418 /* round mem_size up to next power of 2 */
419 mem_size = lmb_phys_mem_size();
420 rnd_mem_size = 1UL << __ilog2(mem_size);
421 if (rnd_mem_size < mem_size)
422 rnd_mem_size <<= 1;
423
424 /* # pages / 2 */
425 pteg_count = max(rnd_mem_size >> (12 + 1), 1UL << 11);
426
427 return pteg_count << 7;
428 }
429
430 #ifdef CONFIG_MEMORY_HOTPLUG
431 void create_section_mapping(unsigned long start, unsigned long end)
432 {
433 BUG_ON(htab_bolt_mapping(start, end, __pa(start),
434 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_COHERENT | PP_RWXX,
435 mmu_linear_psize, mmu_kernel_ssize));
436 }
437 #endif /* CONFIG_MEMORY_HOTPLUG */
438
439 static inline void make_bl(unsigned int *insn_addr, void *func)
440 {
441 unsigned long funcp = *((unsigned long *)func);
442 int offset = funcp - (unsigned long)insn_addr;
443
444 *insn_addr = (unsigned int)(0x48000001 | (offset & 0x03fffffc));
445 flush_icache_range((unsigned long)insn_addr, 4+
446 (unsigned long)insn_addr);
447 }
448
449 static void __init htab_finish_init(void)
450 {
451 extern unsigned int *htab_call_hpte_insert1;
452 extern unsigned int *htab_call_hpte_insert2;
453 extern unsigned int *htab_call_hpte_remove;
454 extern unsigned int *htab_call_hpte_updatepp;
455
456 #ifdef CONFIG_PPC_HAS_HASH_64K
457 extern unsigned int *ht64_call_hpte_insert1;
458 extern unsigned int *ht64_call_hpte_insert2;
459 extern unsigned int *ht64_call_hpte_remove;
460 extern unsigned int *ht64_call_hpte_updatepp;
461
462 make_bl(ht64_call_hpte_insert1, ppc_md.hpte_insert);
463 make_bl(ht64_call_hpte_insert2, ppc_md.hpte_insert);
464 make_bl(ht64_call_hpte_remove, ppc_md.hpte_remove);
465 make_bl(ht64_call_hpte_updatepp, ppc_md.hpte_updatepp);
466 #endif /* CONFIG_PPC_HAS_HASH_64K */
467
468 make_bl(htab_call_hpte_insert1, ppc_md.hpte_insert);
469 make_bl(htab_call_hpte_insert2, ppc_md.hpte_insert);
470 make_bl(htab_call_hpte_remove, ppc_md.hpte_remove);
471 make_bl(htab_call_hpte_updatepp, ppc_md.hpte_updatepp);
472 }
473
474 void __init htab_initialize(void)
475 {
476 unsigned long table;
477 unsigned long pteg_count;
478 unsigned long mode_rw;
479 unsigned long base = 0, size = 0, limit;
480 int i;
481
482 extern unsigned long tce_alloc_start, tce_alloc_end;
483
484 DBG(" -> htab_initialize()\n");
485
486 /* Initialize segment sizes */
487 htab_init_seg_sizes();
488
489 /* Initialize page sizes */
490 htab_init_page_sizes();
491
492 if (cpu_has_feature(CPU_FTR_1T_SEGMENT)) {
493 mmu_kernel_ssize = MMU_SEGSIZE_1T;
494 mmu_highuser_ssize = MMU_SEGSIZE_1T;
495 printk(KERN_INFO "Using 1TB segments\n");
496 }
497
498 /*
499 * Calculate the required size of the htab. We want the number of
500 * PTEGs to equal one half the number of real pages.
501 */
502 htab_size_bytes = htab_get_table_size();
503 pteg_count = htab_size_bytes >> 7;
504
505 htab_hash_mask = pteg_count - 1;
506
507 if (firmware_has_feature(FW_FEATURE_LPAR)) {
508 /* Using a hypervisor which owns the htab */
509 htab_address = NULL;
510 _SDR1 = 0;
511 } else {
512 /* Find storage for the HPT. Must be contiguous in
513 * the absolute address space. On cell we want it to be
514 * in the first 2 Gig so we can use it for IOMMU hacks.
515 */
516 if (machine_is(cell))
517 limit = 0x80000000;
518 else
519 limit = 0;
520
521 table = lmb_alloc_base(htab_size_bytes, htab_size_bytes, limit);
522
523 DBG("Hash table allocated at %lx, size: %lx\n", table,
524 htab_size_bytes);
525
526 htab_address = abs_to_virt(table);
527
528 /* htab absolute addr + encoded htabsize */
529 _SDR1 = table + __ilog2(pteg_count) - 11;
530
531 /* Initialize the HPT with no entries */
532 memset((void *)table, 0, htab_size_bytes);
533
534 /* Set SDR1 */
535 mtspr(SPRN_SDR1, _SDR1);
536 }
537
538 mode_rw = _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_COHERENT | PP_RWXX;
539
540 #ifdef CONFIG_DEBUG_PAGEALLOC
541 linear_map_hash_count = lmb_end_of_DRAM() >> PAGE_SHIFT;
542 linear_map_hash_slots = __va(lmb_alloc_base(linear_map_hash_count,
543 1, lmb.rmo_size));
544 memset(linear_map_hash_slots, 0, linear_map_hash_count);
545 #endif /* CONFIG_DEBUG_PAGEALLOC */
546
547 /* On U3 based machines, we need to reserve the DART area and
548 * _NOT_ map it to avoid cache paradoxes as it's remapped non
549 * cacheable later on
550 */
551
552 /* create bolted the linear mapping in the hash table */
553 for (i=0; i < lmb.memory.cnt; i++) {
554 base = (unsigned long)__va(lmb.memory.region[i].base);
555 size = lmb.memory.region[i].size;
556
557 DBG("creating mapping for region: %lx : %lx\n", base, size);
558
559 #ifdef CONFIG_U3_DART
560 /* Do not map the DART space. Fortunately, it will be aligned
561 * in such a way that it will not cross two lmb regions and
562 * will fit within a single 16Mb page.
563 * The DART space is assumed to be a full 16Mb region even if
564 * we only use 2Mb of that space. We will use more of it later
565 * for AGP GART. We have to use a full 16Mb large page.
566 */
567 DBG("DART base: %lx\n", dart_tablebase);
568
569 if (dart_tablebase != 0 && dart_tablebase >= base
570 && dart_tablebase < (base + size)) {
571 unsigned long dart_table_end = dart_tablebase + 16 * MB;
572 if (base != dart_tablebase)
573 BUG_ON(htab_bolt_mapping(base, dart_tablebase,
574 __pa(base), mode_rw,
575 mmu_linear_psize,
576 mmu_kernel_ssize));
577 if ((base + size) > dart_table_end)
578 BUG_ON(htab_bolt_mapping(dart_tablebase+16*MB,
579 base + size,
580 __pa(dart_table_end),
581 mode_rw,
582 mmu_linear_psize,
583 mmu_kernel_ssize));
584 continue;
585 }
586 #endif /* CONFIG_U3_DART */
587 BUG_ON(htab_bolt_mapping(base, base + size, __pa(base),
588 mode_rw, mmu_linear_psize, mmu_kernel_ssize));
589 }
590
591 /*
592 * If we have a memory_limit and we've allocated TCEs then we need to
593 * explicitly map the TCE area at the top of RAM. We also cope with the
594 * case that the TCEs start below memory_limit.
595 * tce_alloc_start/end are 16MB aligned so the mapping should work
596 * for either 4K or 16MB pages.
597 */
598 if (tce_alloc_start) {
599 tce_alloc_start = (unsigned long)__va(tce_alloc_start);
600 tce_alloc_end = (unsigned long)__va(tce_alloc_end);
601
602 if (base + size >= tce_alloc_start)
603 tce_alloc_start = base + size + 1;
604
605 BUG_ON(htab_bolt_mapping(tce_alloc_start, tce_alloc_end,
606 __pa(tce_alloc_start), mode_rw,
607 mmu_linear_psize, mmu_kernel_ssize));
608 }
609
610 htab_finish_init();
611
612 DBG(" <- htab_initialize()\n");
613 }
614 #undef KB
615 #undef MB
616
617 void htab_initialize_secondary(void)
618 {
619 if (!firmware_has_feature(FW_FEATURE_LPAR))
620 mtspr(SPRN_SDR1, _SDR1);
621 }
622
623 /*
624 * Called by asm hashtable.S for doing lazy icache flush
625 */
626 unsigned int hash_page_do_lazy_icache(unsigned int pp, pte_t pte, int trap)
627 {
628 struct page *page;
629
630 if (!pfn_valid(pte_pfn(pte)))
631 return pp;
632
633 page = pte_page(pte);
634
635 /* page is dirty */
636 if (!test_bit(PG_arch_1, &page->flags) && !PageReserved(page)) {
637 if (trap == 0x400) {
638 __flush_dcache_icache(page_address(page));
639 set_bit(PG_arch_1, &page->flags);
640 } else
641 pp |= HPTE_R_N;
642 }
643 return pp;
644 }
645
646 /*
647 * Demote a segment to using 4k pages.
648 * For now this makes the whole process use 4k pages.
649 */
650 #ifdef CONFIG_PPC_64K_PAGES
651 void demote_segment_4k(struct mm_struct *mm, unsigned long addr)
652 {
653 if (mm->context.user_psize == MMU_PAGE_4K)
654 return;
655 slice_set_user_psize(mm, MMU_PAGE_4K);
656 #ifdef CONFIG_SPU_BASE
657 spu_flush_all_slbs(mm);
658 #endif
659 if (get_paca()->context.user_psize != MMU_PAGE_4K) {
660 get_paca()->context = mm->context;
661 slb_flush_and_rebolt();
662 }
663 }
664 #endif /* CONFIG_PPC_64K_PAGES */
665
666 #ifdef CONFIG_PPC_SUBPAGE_PROT
667 /*
668 * This looks up a 2-bit protection code for a 4k subpage of a 64k page.
669 * Userspace sets the subpage permissions using the subpage_prot system call.
670 *
671 * Result is 0: full permissions, _PAGE_RW: read-only,
672 * _PAGE_USER or _PAGE_USER|_PAGE_RW: no access.
673 */
674 static int subpage_protection(pgd_t *pgdir, unsigned long ea)
675 {
676 struct subpage_prot_table *spt = pgd_subpage_prot(pgdir);
677 u32 spp = 0;
678 u32 **sbpm, *sbpp;
679
680 if (ea >= spt->maxaddr)
681 return 0;
682 if (ea < 0x100000000) {
683 /* addresses below 4GB use spt->low_prot */
684 sbpm = spt->low_prot;
685 } else {
686 sbpm = spt->protptrs[ea >> SBP_L3_SHIFT];
687 if (!sbpm)
688 return 0;
689 }
690 sbpp = sbpm[(ea >> SBP_L2_SHIFT) & (SBP_L2_COUNT - 1)];
691 if (!sbpp)
692 return 0;
693 spp = sbpp[(ea >> PAGE_SHIFT) & (SBP_L1_COUNT - 1)];
694
695 /* extract 2-bit bitfield for this 4k subpage */
696 spp >>= 30 - 2 * ((ea >> 12) & 0xf);
697
698 /* turn 0,1,2,3 into combination of _PAGE_USER and _PAGE_RW */
699 spp = ((spp & 2) ? _PAGE_USER : 0) | ((spp & 1) ? _PAGE_RW : 0);
700 return spp;
701 }
702
703 #else /* CONFIG_PPC_SUBPAGE_PROT */
704 static inline int subpage_protection(pgd_t *pgdir, unsigned long ea)
705 {
706 return 0;
707 }
708 #endif
709
710 /* Result code is:
711 * 0 - handled
712 * 1 - normal page fault
713 * -1 - critical hash insertion error
714 * -2 - access not permitted by subpage protection mechanism
715 */
716 int hash_page(unsigned long ea, unsigned long access, unsigned long trap)
717 {
718 void *pgdir;
719 unsigned long vsid;
720 struct mm_struct *mm;
721 pte_t *ptep;
722 cpumask_t tmp;
723 int rc, user_region = 0, local = 0;
724 int psize, ssize;
725
726 DBG_LOW("hash_page(ea=%016lx, access=%lx, trap=%lx\n",
727 ea, access, trap);
728
729 if ((ea & ~REGION_MASK) >= PGTABLE_RANGE) {
730 DBG_LOW(" out of pgtable range !\n");
731 return 1;
732 }
733
734 /* Get region & vsid */
735 switch (REGION_ID(ea)) {
736 case USER_REGION_ID:
737 user_region = 1;
738 mm = current->mm;
739 if (! mm) {
740 DBG_LOW(" user region with no mm !\n");
741 return 1;
742 }
743 #ifdef CONFIG_PPC_MM_SLICES
744 psize = get_slice_psize(mm, ea);
745 #else
746 psize = mm->context.user_psize;
747 #endif
748 ssize = user_segment_size(ea);
749 vsid = get_vsid(mm->context.id, ea, ssize);
750 break;
751 case VMALLOC_REGION_ID:
752 mm = &init_mm;
753 vsid = get_kernel_vsid(ea, mmu_kernel_ssize);
754 if (ea < VMALLOC_END)
755 psize = mmu_vmalloc_psize;
756 else
757 psize = mmu_io_psize;
758 ssize = mmu_kernel_ssize;
759 break;
760 default:
761 /* Not a valid range
762 * Send the problem up to do_page_fault
763 */
764 return 1;
765 }
766 DBG_LOW(" mm=%p, mm->pgdir=%p, vsid=%016lx\n", mm, mm->pgd, vsid);
767
768 /* Get pgdir */
769 pgdir = mm->pgd;
770 if (pgdir == NULL)
771 return 1;
772
773 /* Check CPU locality */
774 tmp = cpumask_of_cpu(smp_processor_id());
775 if (user_region && cpus_equal(mm->cpu_vm_mask, tmp))
776 local = 1;
777
778 #ifdef CONFIG_HUGETLB_PAGE
779 /* Handle hugepage regions */
780 if (HPAGE_SHIFT && psize == mmu_huge_psize) {
781 DBG_LOW(" -> huge page !\n");
782 return hash_huge_page(mm, access, ea, vsid, local, trap);
783 }
784 #endif /* CONFIG_HUGETLB_PAGE */
785
786 #ifndef CONFIG_PPC_64K_PAGES
787 /* If we use 4K pages and our psize is not 4K, then we are hitting
788 * a special driver mapping, we need to align the address before
789 * we fetch the PTE
790 */
791 if (psize != MMU_PAGE_4K)
792 ea &= ~((1ul << mmu_psize_defs[psize].shift) - 1);
793 #endif /* CONFIG_PPC_64K_PAGES */
794
795 /* Get PTE and page size from page tables */
796 ptep = find_linux_pte(pgdir, ea);
797 if (ptep == NULL || !pte_present(*ptep)) {
798 DBG_LOW(" no PTE !\n");
799 return 1;
800 }
801
802 #ifndef CONFIG_PPC_64K_PAGES
803 DBG_LOW(" i-pte: %016lx\n", pte_val(*ptep));
804 #else
805 DBG_LOW(" i-pte: %016lx %016lx\n", pte_val(*ptep),
806 pte_val(*(ptep + PTRS_PER_PTE)));
807 #endif
808 /* Pre-check access permissions (will be re-checked atomically
809 * in __hash_page_XX but this pre-check is a fast path
810 */
811 if (access & ~pte_val(*ptep)) {
812 DBG_LOW(" no access !\n");
813 return 1;
814 }
815
816 /* Do actual hashing */
817 #ifdef CONFIG_PPC_64K_PAGES
818 /* If _PAGE_4K_PFN is set, make sure this is a 4k segment */
819 if (pte_val(*ptep) & _PAGE_4K_PFN) {
820 demote_segment_4k(mm, ea);
821 psize = MMU_PAGE_4K;
822 }
823
824 /* If this PTE is non-cacheable and we have restrictions on
825 * using non cacheable large pages, then we switch to 4k
826 */
827 if (mmu_ci_restrictions && psize == MMU_PAGE_64K &&
828 (pte_val(*ptep) & _PAGE_NO_CACHE)) {
829 if (user_region) {
830 demote_segment_4k(mm, ea);
831 psize = MMU_PAGE_4K;
832 } else if (ea < VMALLOC_END) {
833 /*
834 * some driver did a non-cacheable mapping
835 * in vmalloc space, so switch vmalloc
836 * to 4k pages
837 */
838 printk(KERN_ALERT "Reducing vmalloc segment "
839 "to 4kB pages because of "
840 "non-cacheable mapping\n");
841 psize = mmu_vmalloc_psize = MMU_PAGE_4K;
842 #ifdef CONFIG_SPU_BASE
843 spu_flush_all_slbs(mm);
844 #endif
845 }
846 }
847 if (user_region) {
848 if (psize != get_paca()->context.user_psize) {
849 get_paca()->context = mm->context;
850 slb_flush_and_rebolt();
851 }
852 } else if (get_paca()->vmalloc_sllp !=
853 mmu_psize_defs[mmu_vmalloc_psize].sllp) {
854 get_paca()->vmalloc_sllp =
855 mmu_psize_defs[mmu_vmalloc_psize].sllp;
856 slb_vmalloc_update();
857 }
858 #endif /* CONFIG_PPC_64K_PAGES */
859
860 #ifdef CONFIG_PPC_HAS_HASH_64K
861 if (psize == MMU_PAGE_64K)
862 rc = __hash_page_64K(ea, access, vsid, ptep, trap, local, ssize);
863 else
864 #endif /* CONFIG_PPC_HAS_HASH_64K */
865 {
866 int spp = subpage_protection(pgdir, ea);
867 if (access & spp)
868 rc = -2;
869 else
870 rc = __hash_page_4K(ea, access, vsid, ptep, trap,
871 local, ssize, spp);
872 }
873
874 #ifndef CONFIG_PPC_64K_PAGES
875 DBG_LOW(" o-pte: %016lx\n", pte_val(*ptep));
876 #else
877 DBG_LOW(" o-pte: %016lx %016lx\n", pte_val(*ptep),
878 pte_val(*(ptep + PTRS_PER_PTE)));
879 #endif
880 DBG_LOW(" -> rc=%d\n", rc);
881 return rc;
882 }
883 EXPORT_SYMBOL_GPL(hash_page);
884
885 void hash_preload(struct mm_struct *mm, unsigned long ea,
886 unsigned long access, unsigned long trap)
887 {
888 unsigned long vsid;
889 void *pgdir;
890 pte_t *ptep;
891 cpumask_t mask;
892 unsigned long flags;
893 int local = 0;
894 int ssize;
895
896 BUG_ON(REGION_ID(ea) != USER_REGION_ID);
897
898 #ifdef CONFIG_PPC_MM_SLICES
899 /* We only prefault standard pages for now */
900 if (unlikely(get_slice_psize(mm, ea) != mm->context.user_psize))
901 return;
902 #endif
903
904 DBG_LOW("hash_preload(mm=%p, mm->pgdir=%p, ea=%016lx, access=%lx,"
905 " trap=%lx\n", mm, mm->pgd, ea, access, trap);
906
907 /* Get Linux PTE if available */
908 pgdir = mm->pgd;
909 if (pgdir == NULL)
910 return;
911 ptep = find_linux_pte(pgdir, ea);
912 if (!ptep)
913 return;
914
915 #ifdef CONFIG_PPC_64K_PAGES
916 /* If either _PAGE_4K_PFN or _PAGE_NO_CACHE is set (and we are on
917 * a 64K kernel), then we don't preload, hash_page() will take
918 * care of it once we actually try to access the page.
919 * That way we don't have to duplicate all of the logic for segment
920 * page size demotion here
921 */
922 if (pte_val(*ptep) & (_PAGE_4K_PFN | _PAGE_NO_CACHE))
923 return;
924 #endif /* CONFIG_PPC_64K_PAGES */
925
926 /* Get VSID */
927 ssize = user_segment_size(ea);
928 vsid = get_vsid(mm->context.id, ea, ssize);
929
930 /* Hash doesn't like irqs */
931 local_irq_save(flags);
932
933 /* Is that local to this CPU ? */
934 mask = cpumask_of_cpu(smp_processor_id());
935 if (cpus_equal(mm->cpu_vm_mask, mask))
936 local = 1;
937
938 /* Hash it in */
939 #ifdef CONFIG_PPC_HAS_HASH_64K
940 if (mm->context.user_psize == MMU_PAGE_64K)
941 __hash_page_64K(ea, access, vsid, ptep, trap, local, ssize);
942 else
943 #endif /* CONFIG_PPC_HAS_HASH_64K */
944 __hash_page_4K(ea, access, vsid, ptep, trap, local, ssize,
945 subpage_protection(pgdir, ea));
946
947 local_irq_restore(flags);
948 }
949
950 /* WARNING: This is called from hash_low_64.S, if you change this prototype,
951 * do not forget to update the assembly call site !
952 */
953 void flush_hash_page(unsigned long va, real_pte_t pte, int psize, int ssize,
954 int local)
955 {
956 unsigned long hash, index, shift, hidx, slot;
957
958 DBG_LOW("flush_hash_page(va=%016x)\n", va);
959 pte_iterate_hashed_subpages(pte, psize, va, index, shift) {
960 hash = hpt_hash(va, shift, ssize);
961 hidx = __rpte_to_hidx(pte, index);
962 if (hidx & _PTEIDX_SECONDARY)
963 hash = ~hash;
964 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
965 slot += hidx & _PTEIDX_GROUP_IX;
966 DBG_LOW(" sub %d: hash=%x, hidx=%x\n", index, slot, hidx);
967 ppc_md.hpte_invalidate(slot, va, psize, ssize, local);
968 } pte_iterate_hashed_end();
969 }
970
971 void flush_hash_range(unsigned long number, int local)
972 {
973 if (ppc_md.flush_hash_range)
974 ppc_md.flush_hash_range(number, local);
975 else {
976 int i;
977 struct ppc64_tlb_batch *batch =
978 &__get_cpu_var(ppc64_tlb_batch);
979
980 for (i = 0; i < number; i++)
981 flush_hash_page(batch->vaddr[i], batch->pte[i],
982 batch->psize, batch->ssize, local);
983 }
984 }
985
986 /*
987 * low_hash_fault is called when we the low level hash code failed
988 * to instert a PTE due to an hypervisor error
989 */
990 void low_hash_fault(struct pt_regs *regs, unsigned long address, int rc)
991 {
992 if (user_mode(regs)) {
993 #ifdef CONFIG_PPC_SUBPAGE_PROT
994 if (rc == -2)
995 _exception(SIGSEGV, regs, SEGV_ACCERR, address);
996 else
997 #endif
998 _exception(SIGBUS, regs, BUS_ADRERR, address);
999 } else
1000 bad_page_fault(regs, address, SIGBUS);
1001 }
1002
1003 #ifdef CONFIG_DEBUG_PAGEALLOC
1004 static void kernel_map_linear_page(unsigned long vaddr, unsigned long lmi)
1005 {
1006 unsigned long hash, hpteg;
1007 unsigned long vsid = get_kernel_vsid(vaddr, mmu_kernel_ssize);
1008 unsigned long va = hpt_va(vaddr, vsid, mmu_kernel_ssize);
1009 unsigned long mode = _PAGE_ACCESSED | _PAGE_DIRTY |
1010 _PAGE_COHERENT | PP_RWXX | HPTE_R_N;
1011 int ret;
1012
1013 hash = hpt_hash(va, PAGE_SHIFT, mmu_kernel_ssize);
1014 hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
1015
1016 ret = ppc_md.hpte_insert(hpteg, va, __pa(vaddr),
1017 mode, HPTE_V_BOLTED,
1018 mmu_linear_psize, mmu_kernel_ssize);
1019 BUG_ON (ret < 0);
1020 spin_lock(&linear_map_hash_lock);
1021 BUG_ON(linear_map_hash_slots[lmi] & 0x80);
1022 linear_map_hash_slots[lmi] = ret | 0x80;
1023 spin_unlock(&linear_map_hash_lock);
1024 }
1025
1026 static void kernel_unmap_linear_page(unsigned long vaddr, unsigned long lmi)
1027 {
1028 unsigned long hash, hidx, slot;
1029 unsigned long vsid = get_kernel_vsid(vaddr, mmu_kernel_ssize);
1030 unsigned long va = hpt_va(vaddr, vsid, mmu_kernel_ssize);
1031
1032 hash = hpt_hash(va, PAGE_SHIFT, mmu_kernel_ssize);
1033 spin_lock(&linear_map_hash_lock);
1034 BUG_ON(!(linear_map_hash_slots[lmi] & 0x80));
1035 hidx = linear_map_hash_slots[lmi] & 0x7f;
1036 linear_map_hash_slots[lmi] = 0;
1037 spin_unlock(&linear_map_hash_lock);
1038 if (hidx & _PTEIDX_SECONDARY)
1039 hash = ~hash;
1040 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
1041 slot += hidx & _PTEIDX_GROUP_IX;
1042 ppc_md.hpte_invalidate(slot, va, mmu_linear_psize, mmu_kernel_ssize, 0);
1043 }
1044
1045 void kernel_map_pages(struct page *page, int numpages, int enable)
1046 {
1047 unsigned long flags, vaddr, lmi;
1048 int i;
1049
1050 local_irq_save(flags);
1051 for (i = 0; i < numpages; i++, page++) {
1052 vaddr = (unsigned long)page_address(page);
1053 lmi = __pa(vaddr) >> PAGE_SHIFT;
1054 if (lmi >= linear_map_hash_count)
1055 continue;
1056 if (enable)
1057 kernel_map_linear_page(vaddr, lmi);
1058 else
1059 kernel_unmap_linear_page(vaddr, lmi);
1060 }
1061 local_irq_restore(flags);
1062 }
1063 #endif /* CONFIG_DEBUG_PAGEALLOC */