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