blob: f15dfb92dec052d51f6c9212a8323ea2cf517945 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * PowerPC64 port by Mike Corrigan and Dave Engebretsen
3 * {mikejc|engebret}@us.ibm.com
4 *
5 * Copyright (c) 2000 Mike Corrigan <mikejc@us.ibm.com>
6 *
7 * SMP scalability work:
8 * Copyright (C) 2001 Anton Blanchard <anton@au.ibm.com>, IBM
9 *
10 * Module name: htab.c
11 *
12 * Description:
13 * PowerPC Hashed Page Table functions
14 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version
18 * 2 of the License, or (at your option) any later version.
19 */
20
21#undef DEBUG
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +110022#undef DEBUG_LOW
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24#include <linux/config.h>
25#include <linux/spinlock.h>
26#include <linux/errno.h>
27#include <linux/sched.h>
28#include <linux/proc_fs.h>
29#include <linux/stat.h>
30#include <linux/sysctl.h>
31#include <linux/ctype.h>
32#include <linux/cache.h>
33#include <linux/init.h>
34#include <linux/signal.h>
35
36#include <asm/ppcdebug.h>
37#include <asm/processor.h>
38#include <asm/pgtable.h>
39#include <asm/mmu.h>
40#include <asm/mmu_context.h>
41#include <asm/page.h>
42#include <asm/types.h>
43#include <asm/system.h>
44#include <asm/uaccess.h>
45#include <asm/machdep.h>
46#include <asm/lmb.h>
47#include <asm/abs_addr.h>
48#include <asm/tlbflush.h>
49#include <asm/io.h>
50#include <asm/eeh.h>
51#include <asm/tlb.h>
52#include <asm/cacheflush.h>
53#include <asm/cputable.h>
54#include <asm/abs_addr.h>
55#include <asm/sections.h>
56
57#ifdef DEBUG
58#define DBG(fmt...) udbg_printf(fmt)
59#else
60#define DBG(fmt...)
61#endif
62
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +110063#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
Linus Torvalds1da177e2005-04-16 15:20:36 -070072/*
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
David Gibson96e28442005-07-13 01:11:42 -070088hpte_t *htab_address;
89unsigned long htab_hash_mask;
Paul Mackerrasab1f9da2005-10-10 21:58:35 +100090unsigned long _SDR1;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +110091struct mmu_psize_def mmu_psize_defs[MMU_PAGE_COUNT];
92int mmu_linear_psize = MMU_PAGE_4K;
93int mmu_virtual_psize = MMU_PAGE_4K;
94#ifdef CONFIG_HUGETLB_PAGE
95int mmu_huge_psize = MMU_PAGE_16M;
96unsigned int HPAGE_SHIFT;
97#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +110099/* There are definitions of page sizes arrays to be used when none
100 * is provided by the firmware.
101 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100103/* Pre-POWER4 CPUs (4k pages only)
104 */
105struct mmu_psize_def mmu_psize_defaults_old[] = {
106 [MMU_PAGE_4K] = {
107 .shift = 12,
108 .sllp = 0,
109 .penc = 0,
110 .avpnm = 0,
111 .tlbiel = 0,
112 },
113};
114
115/* POWER4, GPUL, POWER5
116 *
117 * Support for 16Mb large pages
118 */
119struct mmu_psize_def mmu_psize_defaults_gp[] = {
120 [MMU_PAGE_4K] = {
121 .shift = 12,
122 .sllp = 0,
123 .penc = 0,
124 .avpnm = 0,
125 .tlbiel = 1,
126 },
127 [MMU_PAGE_16M] = {
128 .shift = 24,
129 .sllp = SLB_VSID_L,
130 .penc = 0,
131 .avpnm = 0x1UL,
132 .tlbiel = 0,
133 },
134};
135
136
137int htab_bolt_mapping(unsigned long vstart, unsigned long vend,
138 unsigned long pstart, unsigned long mode, int psize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100140 unsigned long vaddr, paddr;
141 unsigned int step, shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 unsigned long tmp_mode;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100143 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100145 shift = mmu_psize_defs[psize].shift;
146 step = 1 << shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100148 for (vaddr = vstart, paddr = pstart; vaddr < vend;
149 vaddr += step, paddr += step) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 unsigned long vpn, hash, hpteg;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100151 unsigned long vsid = get_kernel_vsid(vaddr);
152 unsigned long va = (vsid << 28) | (vaddr & 0x0fffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100154 vpn = va >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 tmp_mode = mode;
156
157 /* Make non-kernel text non-executable */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100158 if (!in_kernel_text(vaddr))
159 tmp_mode = mode | HPTE_R_N;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100161 hash = hpt_hash(va, shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
163
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100164 /* The crap below can be cleaned once ppd_md.probe() can
165 * set up the hash callbacks, thus we can just used the
166 * normal insert callback here.
167 */
Michael Ellerman4c551302005-09-23 14:47:58 +1000168#ifdef CONFIG_PPC_ISERIES
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100169 if (systemcfg->platform == PLATFORM_ISERIES_LPAR)
170 ret = iSeries_hpte_insert(hpteg, va,
171 virt_to_abs(paddr),
172 tmp_mode,
173 HPTE_V_BOLTED,
174 psize);
Michael Ellerman4c551302005-09-23 14:47:58 +1000175 else
176#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177#ifdef CONFIG_PPC_PSERIES
178 if (systemcfg->platform & PLATFORM_LPAR)
179 ret = pSeries_lpar_hpte_insert(hpteg, va,
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100180 virt_to_abs(paddr),
181 tmp_mode,
182 HPTE_V_BOLTED,
183 psize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 else
Michael Ellerman4c551302005-09-23 14:47:58 +1000185#endif
186#ifdef CONFIG_PPC_MULTIPLATFORM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 ret = native_hpte_insert(hpteg, va,
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100188 virt_to_abs(paddr),
189 tmp_mode, HPTE_V_BOLTED,
190 psize);
Michael Ellerman4c551302005-09-23 14:47:58 +1000191#endif
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100192 if (ret < 0)
193 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 }
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100195 return ret < 0 ? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196}
197
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100198static int __init htab_dt_scan_page_sizes(unsigned long node,
199 const char *uname, int depth,
200 void *data)
201{
202 char *type = of_get_flat_dt_prop(node, "device_type", NULL);
203 u32 *prop;
204 unsigned long size = 0;
205
206 /* We are scanning "cpu" nodes only */
207 if (type == NULL || strcmp(type, "cpu") != 0)
208 return 0;
209
210 prop = (u32 *)of_get_flat_dt_prop(node,
211 "ibm,segment-page-sizes", &size);
212 if (prop != NULL) {
213 DBG("Page sizes from device-tree:\n");
214 size /= 4;
215 cur_cpu_spec->cpu_features &= ~(CPU_FTR_16M_PAGE);
216 while(size > 0) {
217 unsigned int shift = prop[0];
218 unsigned int slbenc = prop[1];
219 unsigned int lpnum = prop[2];
220 unsigned int lpenc = 0;
221 struct mmu_psize_def *def;
222 int idx = -1;
223
224 size -= 3; prop += 3;
225 while(size > 0 && lpnum) {
226 if (prop[0] == shift)
227 lpenc = prop[1];
228 prop += 2; size -= 2;
229 lpnum--;
230 }
231 switch(shift) {
232 case 0xc:
233 idx = MMU_PAGE_4K;
234 break;
235 case 0x10:
236 idx = MMU_PAGE_64K;
237 break;
238 case 0x14:
239 idx = MMU_PAGE_1M;
240 break;
241 case 0x18:
242 idx = MMU_PAGE_16M;
243 cur_cpu_spec->cpu_features |= CPU_FTR_16M_PAGE;
244 break;
245 case 0x22:
246 idx = MMU_PAGE_16G;
247 break;
248 }
249 if (idx < 0)
250 continue;
251 def = &mmu_psize_defs[idx];
252 def->shift = shift;
253 if (shift <= 23)
254 def->avpnm = 0;
255 else
256 def->avpnm = (1 << (shift - 23)) - 1;
257 def->sllp = slbenc;
258 def->penc = lpenc;
259 /* We don't know for sure what's up with tlbiel, so
260 * for now we only set it for 4K and 64K pages
261 */
262 if (idx == MMU_PAGE_4K || idx == MMU_PAGE_64K)
263 def->tlbiel = 1;
264 else
265 def->tlbiel = 0;
266
267 DBG(" %d: shift=%02x, sllp=%04x, avpnm=%08x, "
268 "tlbiel=%d, penc=%d\n",
269 idx, shift, def->sllp, def->avpnm, def->tlbiel,
270 def->penc);
271 }
272 return 1;
273 }
274 return 0;
275}
276
277
278static void __init htab_init_page_sizes(void)
279{
280 int rc;
281
282 /* Default to 4K pages only */
283 memcpy(mmu_psize_defs, mmu_psize_defaults_old,
284 sizeof(mmu_psize_defaults_old));
285
286 /*
287 * Try to find the available page sizes in the device-tree
288 */
289 rc = of_scan_flat_dt(htab_dt_scan_page_sizes, NULL);
290 if (rc != 0) /* Found */
291 goto found;
292
293 /*
294 * Not in the device-tree, let's fallback on known size
295 * list for 16M capable GP & GR
296 */
297 if ((systemcfg->platform != PLATFORM_ISERIES_LPAR) &&
298 cpu_has_feature(CPU_FTR_16M_PAGE))
299 memcpy(mmu_psize_defs, mmu_psize_defaults_gp,
300 sizeof(mmu_psize_defaults_gp));
301 found:
302 /*
303 * Pick a size for the linear mapping. Currently, we only support
304 * 16M, 1M and 4K which is the default
305 */
306 if (mmu_psize_defs[MMU_PAGE_16M].shift)
307 mmu_linear_psize = MMU_PAGE_16M;
308 else if (mmu_psize_defs[MMU_PAGE_1M].shift)
309 mmu_linear_psize = MMU_PAGE_1M;
310
311 /*
312 * Pick a size for the ordinary pages. Default is 4K, we support
313 * 64K if cache inhibited large pages are supported by the
314 * processor
315 */
316#ifdef CONFIG_PPC_64K_PAGES
317 if (mmu_psize_defs[MMU_PAGE_64K].shift &&
318 cpu_has_feature(CPU_FTR_CI_LARGE_PAGE))
319 mmu_virtual_psize = MMU_PAGE_64K;
320#endif
321
322 printk(KERN_INFO "Page orders: linear mapping = %d, others = %d\n",
323 mmu_psize_defs[mmu_linear_psize].shift,
324 mmu_psize_defs[mmu_virtual_psize].shift);
325
326#ifdef CONFIG_HUGETLB_PAGE
327 /* Init large page size. Currently, we pick 16M or 1M depending
328 * on what is available
329 */
330 if (mmu_psize_defs[MMU_PAGE_16M].shift)
331 mmu_huge_psize = MMU_PAGE_16M;
David Gibson7d24f0b2005-11-07 00:57:52 -0800332 /* With 4k/4level pagetables, we can't (for now) cope with a
333 * huge page size < PMD_SIZE */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100334 else if (mmu_psize_defs[MMU_PAGE_1M].shift)
335 mmu_huge_psize = MMU_PAGE_1M;
336
337 /* Calculate HPAGE_SHIFT and sanity check it */
David Gibson7d24f0b2005-11-07 00:57:52 -0800338 if (mmu_psize_defs[mmu_huge_psize].shift > MIN_HUGEPTE_SHIFT &&
339 mmu_psize_defs[mmu_huge_psize].shift < SID_SHIFT)
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100340 HPAGE_SHIFT = mmu_psize_defs[mmu_huge_psize].shift;
341 else
342 HPAGE_SHIFT = 0; /* No huge pages dude ! */
343#endif /* CONFIG_HUGETLB_PAGE */
344}
345
346static int __init htab_dt_scan_pftsize(unsigned long node,
347 const char *uname, int depth,
348 void *data)
349{
350 char *type = of_get_flat_dt_prop(node, "device_type", NULL);
351 u32 *prop;
352
353 /* We are scanning "cpu" nodes only */
354 if (type == NULL || strcmp(type, "cpu") != 0)
355 return 0;
356
357 prop = (u32 *)of_get_flat_dt_prop(node, "ibm,pft-size", NULL);
358 if (prop != NULL) {
359 /* pft_size[0] is the NUMA CEC cookie */
360 ppc64_pft_size = prop[1];
361 return 1;
362 }
363 return 0;
364}
365
366static unsigned long __init htab_get_table_size(void)
Paul Mackerras3eac8c62005-10-12 16:58:53 +1000367{
368 unsigned long rnd_mem_size, pteg_count;
369
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100370 /* If hash size isn't already provided by the platform, we try to
371 * retreive it from the device-tree. If it's not there neither, we
372 * calculate it now based on the total RAM size
Paul Mackerras3eac8c62005-10-12 16:58:53 +1000373 */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100374 if (ppc64_pft_size == 0)
375 of_scan_flat_dt(htab_dt_scan_pftsize, NULL);
Paul Mackerras3eac8c62005-10-12 16:58:53 +1000376 if (ppc64_pft_size)
377 return 1UL << ppc64_pft_size;
378
379 /* round mem_size up to next power of 2 */
380 rnd_mem_size = 1UL << __ilog2(systemcfg->physicalMemorySize);
381 if (rnd_mem_size < systemcfg->physicalMemorySize)
382 rnd_mem_size <<= 1;
383
384 /* # pages / 2 */
385 pteg_count = max(rnd_mem_size >> (12 + 1), 1UL << 11);
386
387 return pteg_count << 7;
388}
389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390void __init htab_initialize(void)
391{
392 unsigned long table, htab_size_bytes;
393 unsigned long pteg_count;
394 unsigned long mode_rw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 unsigned long base = 0, size = 0;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100396 int i;
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 extern unsigned long tce_alloc_start, tce_alloc_end;
399
400 DBG(" -> htab_initialize()\n");
401
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100402 /* Initialize page sizes */
403 htab_init_page_sizes();
404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 /*
406 * Calculate the required size of the htab. We want the number of
407 * PTEGs to equal one half the number of real pages.
408 */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100409 htab_size_bytes = htab_get_table_size();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 pteg_count = htab_size_bytes >> 7;
411
412 /* For debug, make the HTAB 1/8 as big as it normally would be. */
413 ifppcdebug(PPCDBG_HTABSIZE) {
414 pteg_count >>= 3;
415 htab_size_bytes = pteg_count << 7;
416 }
417
418 htab_hash_mask = pteg_count - 1;
419
420 if (systemcfg->platform & PLATFORM_LPAR) {
421 /* Using a hypervisor which owns the htab */
422 htab_address = NULL;
423 _SDR1 = 0;
424 } else {
425 /* Find storage for the HPT. Must be contiguous in
426 * the absolute address space.
427 */
428 table = lmb_alloc(htab_size_bytes, htab_size_bytes);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100429 BUG_ON(table == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 DBG("Hash table allocated at %lx, size: %lx\n", table,
432 htab_size_bytes);
433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 htab_address = abs_to_virt(table);
435
436 /* htab absolute addr + encoded htabsize */
437 _SDR1 = table + __ilog2(pteg_count) - 11;
438
439 /* Initialize the HPT with no entries */
440 memset((void *)table, 0, htab_size_bytes);
441 }
442
Anton Blanchard515bae92005-06-21 17:15:55 -0700443 mode_rw = _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_COHERENT | PP_RWXX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 /* On U3 based machines, we need to reserve the DART area and
446 * _NOT_ map it to avoid cache paradoxes as it's remapped non
447 * cacheable later on
448 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
450 /* create bolted the linear mapping in the hash table */
451 for (i=0; i < lmb.memory.cnt; i++) {
Michael Ellerman180379d2005-08-03 20:21:26 +1000452 base = lmb.memory.region[i].base + KERNELBASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 size = lmb.memory.region[i].size;
454
455 DBG("creating mapping for region: %lx : %lx\n", base, size);
456
457#ifdef CONFIG_U3_DART
458 /* Do not map the DART space. Fortunately, it will be aligned
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100459 * in such a way that it will not cross two lmb regions and
460 * will fit within a single 16Mb page.
461 * The DART space is assumed to be a full 16Mb region even if
462 * we only use 2Mb of that space. We will use more of it later
463 * for AGP GART. We have to use a full 16Mb large page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 */
465 DBG("DART base: %lx\n", dart_tablebase);
466
467 if (dart_tablebase != 0 && dart_tablebase >= base
468 && dart_tablebase < (base + size)) {
469 if (base != dart_tablebase)
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100470 BUG_ON(htab_bolt_mapping(base, dart_tablebase,
471 base, mode_rw,
472 mmu_linear_psize));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 if ((base + size) > (dart_tablebase + 16*MB))
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100474 BUG_ON(htab_bolt_mapping(dart_tablebase+16*MB,
475 base + size,
476 dart_tablebase+16*MB,
477 mode_rw,
478 mmu_linear_psize));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 continue;
480 }
481#endif /* CONFIG_U3_DART */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100482 BUG_ON(htab_bolt_mapping(base, base + size, base,
483 mode_rw, mmu_linear_psize));
484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486 /*
487 * If we have a memory_limit and we've allocated TCEs then we need to
488 * explicitly map the TCE area at the top of RAM. We also cope with the
489 * case that the TCEs start below memory_limit.
490 * tce_alloc_start/end are 16MB aligned so the mapping should work
491 * for either 4K or 16MB pages.
492 */
493 if (tce_alloc_start) {
494 tce_alloc_start += KERNELBASE;
495 tce_alloc_end += KERNELBASE;
496
497 if (base + size >= tce_alloc_start)
498 tce_alloc_start = base + size + 1;
499
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100500 BUG_ON(htab_bolt_mapping(tce_alloc_start, tce_alloc_end,
501 tce_alloc_start, mode_rw,
502 mmu_linear_psize));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 }
504
505 DBG(" <- htab_initialize()\n");
506}
507#undef KB
508#undef MB
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
510/*
511 * Called by asm hashtable.S for doing lazy icache flush
512 */
513unsigned int hash_page_do_lazy_icache(unsigned int pp, pte_t pte, int trap)
514{
515 struct page *page;
516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 page = pte_page(pte);
518
519 /* page is dirty */
520 if (!test_bit(PG_arch_1, &page->flags) && !PageReserved(page)) {
521 if (trap == 0x400) {
522 __flush_dcache_icache(page_address(page));
523 set_bit(PG_arch_1, &page->flags);
524 } else
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100525 pp |= HPTE_R_N;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 }
527 return pp;
528}
529
530/* Result code is:
531 * 0 - handled
532 * 1 - normal page fault
533 * -1 - critical hash insertion error
534 */
535int hash_page(unsigned long ea, unsigned long access, unsigned long trap)
536{
537 void *pgdir;
538 unsigned long vsid;
539 struct mm_struct *mm;
540 pte_t *ptep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 cpumask_t tmp;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100542 int rc, user_region = 0, local = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100544 DBG_LOW("hash_page(ea=%016lx, access=%lx, trap=%lx\n",
545 ea, access, trap);
David Gibson1f8d4192005-05-05 16:15:13 -0700546
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100547 if ((ea & ~REGION_MASK) >= PGTABLE_RANGE) {
548 DBG_LOW(" out of pgtable range !\n");
549 return 1;
550 }
551
552 /* Get region & vsid */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 switch (REGION_ID(ea)) {
554 case USER_REGION_ID:
555 user_region = 1;
556 mm = current->mm;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100557 if (! mm) {
558 DBG_LOW(" user region with no mm !\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 return 1;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100560 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 vsid = get_vsid(mm->context.id, ea);
562 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 case VMALLOC_REGION_ID:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 mm = &init_mm;
565 vsid = get_kernel_vsid(ea);
566 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 default:
568 /* Not a valid range
569 * Send the problem up to do_page_fault
570 */
571 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 }
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100573 DBG_LOW(" mm=%p, mm->pgdir=%p, vsid=%016lx\n", mm, mm->pgd, vsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100575 /* Get pgdir */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 pgdir = mm->pgd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 if (pgdir == NULL)
578 return 1;
579
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100580 /* Check CPU locality */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 tmp = cpumask_of_cpu(smp_processor_id());
582 if (user_region && cpus_equal(mm->cpu_vm_mask, tmp))
583 local = 1;
584
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100585 /* Handle hugepage regions */
586 if (unlikely(in_hugepage_area(mm->context, ea))) {
587 DBG_LOW(" -> huge page !\n");
588 return hash_huge_page(mm, access, ea, vsid, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 }
590
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100591 /* Get PTE and page size from page tables */
592 ptep = find_linux_pte(pgdir, ea);
593 if (ptep == NULL || !pte_present(*ptep)) {
594 DBG_LOW(" no PTE !\n");
595 return 1;
596 }
597
598#ifndef CONFIG_PPC_64K_PAGES
599 DBG_LOW(" i-pte: %016lx\n", pte_val(*ptep));
600#else
601 DBG_LOW(" i-pte: %016lx %016lx\n", pte_val(*ptep),
602 pte_val(*(ptep + PTRS_PER_PTE)));
603#endif
604 /* Pre-check access permissions (will be re-checked atomically
605 * in __hash_page_XX but this pre-check is a fast path
606 */
607 if (access & ~pte_val(*ptep)) {
608 DBG_LOW(" no access !\n");
609 return 1;
610 }
611
612 /* Do actual hashing */
613#ifndef CONFIG_PPC_64K_PAGES
614 rc = __hash_page_4K(ea, access, vsid, ptep, trap, local);
615#else
616 if (mmu_virtual_psize == MMU_PAGE_64K)
617 rc = __hash_page_64K(ea, access, vsid, ptep, trap, local);
618 else
619 rc = __hash_page_4K(ea, access, vsid, ptep, trap, local);
620#endif /* CONFIG_PPC_64K_PAGES */
621
622#ifndef CONFIG_PPC_64K_PAGES
623 DBG_LOW(" o-pte: %016lx\n", pte_val(*ptep));
624#else
625 DBG_LOW(" o-pte: %016lx %016lx\n", pte_val(*ptep),
626 pte_val(*(ptep + PTRS_PER_PTE)));
627#endif
628 DBG_LOW(" -> rc=%d\n", rc);
629 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630}
631
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100632void hash_preload(struct mm_struct *mm, unsigned long ea,
633 unsigned long access, unsigned long trap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100635 unsigned long vsid;
636 void *pgdir;
637 pte_t *ptep;
638 cpumask_t mask;
639 unsigned long flags;
640 int local = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100642 /* We don't want huge pages prefaulted for now
643 */
644 if (unlikely(in_hugepage_area(mm->context, ea)))
645 return;
646
647 DBG_LOW("hash_preload(mm=%p, mm->pgdir=%p, ea=%016lx, access=%lx,"
648 " trap=%lx\n", mm, mm->pgd, ea, access, trap);
649
650 /* Get PTE, VSID, access mask */
651 pgdir = mm->pgd;
652 if (pgdir == NULL)
653 return;
654 ptep = find_linux_pte(pgdir, ea);
655 if (!ptep)
656 return;
657 vsid = get_vsid(mm->context.id, ea);
658
659 /* Hash it in */
660 local_irq_save(flags);
661 mask = cpumask_of_cpu(smp_processor_id());
662 if (cpus_equal(mm->cpu_vm_mask, mask))
663 local = 1;
664#ifndef CONFIG_PPC_64K_PAGES
665 __hash_page_4K(ea, access, vsid, ptep, trap, local);
666#else
667 if (mmu_virtual_psize == MMU_PAGE_64K)
668 __hash_page_64K(ea, access, vsid, ptep, trap, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 else
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100670 __hash_page_4K(ea, access, vsid, ptep, trap, local);
671#endif /* CONFIG_PPC_64K_PAGES */
672 local_irq_restore(flags);
673}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100675void flush_hash_page(unsigned long va, real_pte_t pte, int psize, int local)
676{
677 unsigned long hash, index, shift, hidx, slot;
678
679 DBG_LOW("flush_hash_page(va=%016x)\n", va);
680 pte_iterate_hashed_subpages(pte, psize, va, index, shift) {
681 hash = hpt_hash(va, shift);
682 hidx = __rpte_to_hidx(pte, index);
683 if (hidx & _PTEIDX_SECONDARY)
684 hash = ~hash;
685 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
686 slot += hidx & _PTEIDX_GROUP_IX;
687 DBG_LOW(" sub %d: hash=%x, hidx=%x\n", index, slot, hidx);
688 ppc_md.hpte_invalidate(slot, va, psize, local);
689 } pte_iterate_hashed_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690}
691
Benjamin Herrenschmidt61b1a942005-09-20 13:52:50 +1000692void flush_hash_range(unsigned long number, int local)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100694 if (ppc_md.flush_hash_range)
Benjamin Herrenschmidt61b1a942005-09-20 13:52:50 +1000695 ppc_md.flush_hash_range(number, local);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100696 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 int i;
Benjamin Herrenschmidt61b1a942005-09-20 13:52:50 +1000698 struct ppc64_tlb_batch *batch =
699 &__get_cpu_var(ppc64_tlb_batch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
701 for (i = 0; i < number; i++)
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100702 flush_hash_page(batch->vaddr[i], batch->pte[i],
703 batch->psize, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 }
705}
706
707static inline void make_bl(unsigned int *insn_addr, void *func)
708{
709 unsigned long funcp = *((unsigned long *)func);
710 int offset = funcp - (unsigned long)insn_addr;
711
712 *insn_addr = (unsigned int)(0x48000001 | (offset & 0x03fffffc));
713 flush_icache_range((unsigned long)insn_addr, 4+
714 (unsigned long)insn_addr);
715}
716
717/*
718 * low_hash_fault is called when we the low level hash code failed
719 * to instert a PTE due to an hypervisor error
720 */
721void low_hash_fault(struct pt_regs *regs, unsigned long address)
722{
723 if (user_mode(regs)) {
724 siginfo_t info;
725
726 info.si_signo = SIGBUS;
727 info.si_errno = 0;
728 info.si_code = BUS_ADRERR;
729 info.si_addr = (void __user *)address;
730 force_sig_info(SIGBUS, &info, current);
731 return;
732 }
733 bad_page_fault(regs, address, SIGBUS);
734}
735
736void __init htab_finish_init(void)
737{
738 extern unsigned int *htab_call_hpte_insert1;
739 extern unsigned int *htab_call_hpte_insert2;
740 extern unsigned int *htab_call_hpte_remove;
741 extern unsigned int *htab_call_hpte_updatepp;
742
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100743#ifdef CONFIG_PPC_64K_PAGES
744 extern unsigned int *ht64_call_hpte_insert1;
745 extern unsigned int *ht64_call_hpte_insert2;
746 extern unsigned int *ht64_call_hpte_remove;
747 extern unsigned int *ht64_call_hpte_updatepp;
748
749 make_bl(ht64_call_hpte_insert1, ppc_md.hpte_insert);
750 make_bl(ht64_call_hpte_insert2, ppc_md.hpte_insert);
751 make_bl(ht64_call_hpte_remove, ppc_md.hpte_remove);
752 make_bl(ht64_call_hpte_updatepp, ppc_md.hpte_updatepp);
753#endif /* CONFIG_PPC_64K_PAGES */
754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 make_bl(htab_call_hpte_insert1, ppc_md.hpte_insert);
756 make_bl(htab_call_hpte_insert2, ppc_md.hpte_insert);
757 make_bl(htab_call_hpte_remove, ppc_md.hpte_remove);
758 make_bl(htab_call_hpte_updatepp, ppc_md.hpte_updatepp);
759}