blob: 2f3dfcc7cdd3d3e3b36f9482f1e1493b0c276865 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * PowerPC64 port by Mike Corrigan and Dave Engebretsen
3 * {mikejc|engebret}@us.ibm.com
4 *
5 * Copyright (c) 2000 Mike Corrigan <mikejc@us.ibm.com>
6 *
7 * SMP scalability work:
8 * Copyright (C) 2001 Anton Blanchard <anton@au.ibm.com>, IBM
9 *
10 * Module name: htab.c
11 *
12 * Description:
13 * PowerPC Hashed Page Table functions
14 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version
18 * 2 of the License, or (at your option) any later version.
19 */
20
21#undef DEBUG
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +110022#undef DEBUG_LOW
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/spinlock.h>
25#include <linux/errno.h>
26#include <linux/sched.h>
27#include <linux/proc_fs.h>
28#include <linux/stat.h>
29#include <linux/sysctl.h>
30#include <linux/ctype.h>
31#include <linux/cache.h>
32#include <linux/init.h>
33#include <linux/signal.h>
David S. Millerd9b2b2a2008-02-13 16:56:49 -080034#include <linux/lmb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <asm/processor.h>
37#include <asm/pgtable.h>
38#include <asm/mmu.h>
39#include <asm/mmu_context.h>
40#include <asm/page.h>
41#include <asm/types.h>
42#include <asm/system.h>
43#include <asm/uaccess.h>
44#include <asm/machdep.h>
David S. Millerd9b2b2a2008-02-13 16:56:49 -080045#include <asm/prom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <asm/abs_addr.h>
47#include <asm/tlbflush.h>
48#include <asm/io.h>
49#include <asm/eeh.h>
50#include <asm/tlb.h>
51#include <asm/cacheflush.h>
52#include <asm/cputable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <asm/sections.h>
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +100054#include <asm/spu.h>
will schmidtaa39be02007-10-30 06:24:19 +110055#include <asm/udbg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
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
Paul Mackerras799d6042005-11-10 13:37:51 +110088static unsigned long _SDR1;
89struct mmu_psize_def mmu_psize_defs[MMU_PAGE_COUNT];
90
David Gibson8e561e72007-06-13 14:52:56 +100091struct hash_pte *htab_address;
Michael Ellerman337a7122006-02-21 17:22:55 +110092unsigned long htab_size_bytes;
David Gibson96e28442005-07-13 01:11:42 -070093unsigned long htab_hash_mask;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +110094int mmu_linear_psize = MMU_PAGE_4K;
95int mmu_virtual_psize = MMU_PAGE_4K;
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +100096int mmu_vmalloc_psize = MMU_PAGE_4K;
97int mmu_io_psize = MMU_PAGE_4K;
Paul Mackerras1189be62007-10-11 20:37:10 +100098int mmu_kernel_ssize = MMU_SEGSIZE_256M;
99int mmu_highuser_ssize = MMU_SEGSIZE_256M;
Michael Neuling584f8b72007-12-06 17:24:48 +1100100u16 mmu_slb_size = 64;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100101#ifdef CONFIG_HUGETLB_PAGE
102int mmu_huge_psize = MMU_PAGE_16M;
103unsigned int HPAGE_SHIFT;
104#endif
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000105#ifdef CONFIG_PPC_64K_PAGES
106int mmu_ci_restrictions;
107#endif
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000108#ifdef CONFIG_DEBUG_PAGEALLOC
109static u8 *linear_map_hash_slots;
110static unsigned long linear_map_hash_count;
Michael Ellermaned166692007-04-18 11:50:09 +1000111static DEFINE_SPINLOCK(linear_map_hash_lock);
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000112#endif /* CONFIG_DEBUG_PAGEALLOC */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100114/* There are definitions of page sizes arrays to be used when none
115 * is provided by the firmware.
116 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100118/* 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,
Paul Mackerras1189be62007-10-11 20:37:10 +1000153 unsigned long pstart, unsigned long mode,
154 int psize, int ssize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100156 unsigned long vaddr, paddr;
157 unsigned int step, shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 unsigned long tmp_mode;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100159 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100161 shift = mmu_psize_defs[psize].shift;
162 step = 1 << shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100164 for (vaddr = vstart, paddr = pstart; vaddr < vend;
165 vaddr += step, paddr += step) {
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000166 unsigned long hash, hpteg;
Paul Mackerras1189be62007-10-11 20:37:10 +1000167 unsigned long vsid = get_kernel_vsid(vaddr, ssize);
168 unsigned long va = hpt_va(vaddr, vsid, ssize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 tmp_mode = mode;
171
172 /* Make non-kernel text non-executable */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100173 if (!in_kernel_text(vaddr))
174 tmp_mode = mode | HPTE_R_N;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Paul Mackerras1189be62007-10-11 20:37:10 +1000176 hash = hpt_hash(va, shift, ssize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
178
Michael Ellermanc30a4df2006-06-23 18:16:39 +1000179 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,
Paul Mackerras1189be62007-10-11 20:37:10 +1000183 tmp_mode, HPTE_V_BOLTED, psize, ssize);
Michael Ellermanc30a4df2006-06-23 18:16:39 +1000184
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100185 if (ret < 0)
186 break;
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000187#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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 }
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100192 return ret < 0 ? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193}
194
Badari Pulavarty52db9b42008-03-28 11:37:21 +1100195static int htab_remove_mapping(unsigned long vstart, unsigned long vend,
Badari Pulavartyf8c88032008-01-29 09:19:24 +1100196 int psize, int ssize)
197{
198 unsigned long vaddr;
199 unsigned int step, shift;
200
201 shift = mmu_psize_defs[psize].shift;
202 step = 1 << shift;
203
204 if (!ppc_md.hpte_removebolted) {
Badari Pulavarty52db9b42008-03-28 11:37:21 +1100205 printk(KERN_WARNING "Platform doesn't implement "
206 "hpte_removebolted\n");
207 return -EINVAL;
Badari Pulavartyf8c88032008-01-29 09:19:24 +1100208 }
209
210 for (vaddr = vstart; vaddr < vend; vaddr += step)
211 ppc_md.hpte_removebolted(vaddr, psize, ssize);
Badari Pulavarty52db9b42008-03-28 11:37:21 +1100212
213 return 0;
Badari Pulavartyf8c88032008-01-29 09:19:24 +1100214}
215
Paul Mackerras1189be62007-10-11 20:37:10 +1000216static int __init htab_dt_scan_seg_sizes(unsigned long node,
217 const char *uname, int depth,
218 void *data)
219{
220 char *type = of_get_flat_dt_prop(node, "device_type", NULL);
221 u32 *prop;
222 unsigned long size = 0;
223
224 /* We are scanning "cpu" nodes only */
225 if (type == NULL || strcmp(type, "cpu") != 0)
226 return 0;
227
228 prop = (u32 *)of_get_flat_dt_prop(node, "ibm,processor-segment-sizes",
229 &size);
230 if (prop == NULL)
231 return 0;
232 for (; size >= 4; size -= 4, ++prop) {
233 if (prop[0] == 40) {
234 DBG("1T segment support detected\n");
235 cur_cpu_spec->cpu_features |= CPU_FTR_1T_SEGMENT;
Olof Johanssonf5534002007-10-12 16:44:55 +1000236 return 1;
Paul Mackerras1189be62007-10-11 20:37:10 +1000237 }
Paul Mackerras1189be62007-10-11 20:37:10 +1000238 }
Olof Johanssonf66bce52007-10-16 00:58:59 +1000239 cur_cpu_spec->cpu_features &= ~CPU_FTR_NO_SLBIE_B;
Paul Mackerras1189be62007-10-11 20:37:10 +1000240 return 0;
241}
242
243static void __init htab_init_seg_sizes(void)
244{
245 of_scan_flat_dt(htab_dt_scan_seg_sizes, NULL);
246}
247
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100248static int __init htab_dt_scan_page_sizes(unsigned long node,
249 const char *uname, int depth,
250 void *data)
251{
252 char *type = of_get_flat_dt_prop(node, "device_type", NULL);
253 u32 *prop;
254 unsigned long size = 0;
255
256 /* We are scanning "cpu" nodes only */
257 if (type == NULL || strcmp(type, "cpu") != 0)
258 return 0;
259
260 prop = (u32 *)of_get_flat_dt_prop(node,
261 "ibm,segment-page-sizes", &size);
262 if (prop != NULL) {
263 DBG("Page sizes from device-tree:\n");
264 size /= 4;
265 cur_cpu_spec->cpu_features &= ~(CPU_FTR_16M_PAGE);
266 while(size > 0) {
267 unsigned int shift = prop[0];
268 unsigned int slbenc = prop[1];
269 unsigned int lpnum = prop[2];
270 unsigned int lpenc = 0;
271 struct mmu_psize_def *def;
272 int idx = -1;
273
274 size -= 3; prop += 3;
275 while(size > 0 && lpnum) {
276 if (prop[0] == shift)
277 lpenc = prop[1];
278 prop += 2; size -= 2;
279 lpnum--;
280 }
281 switch(shift) {
282 case 0xc:
283 idx = MMU_PAGE_4K;
284 break;
285 case 0x10:
286 idx = MMU_PAGE_64K;
287 break;
288 case 0x14:
289 idx = MMU_PAGE_1M;
290 break;
291 case 0x18:
292 idx = MMU_PAGE_16M;
293 cur_cpu_spec->cpu_features |= CPU_FTR_16M_PAGE;
294 break;
295 case 0x22:
296 idx = MMU_PAGE_16G;
297 break;
298 }
299 if (idx < 0)
300 continue;
301 def = &mmu_psize_defs[idx];
302 def->shift = shift;
303 if (shift <= 23)
304 def->avpnm = 0;
305 else
306 def->avpnm = (1 << (shift - 23)) - 1;
307 def->sllp = slbenc;
308 def->penc = lpenc;
309 /* We don't know for sure what's up with tlbiel, so
310 * for now we only set it for 4K and 64K pages
311 */
312 if (idx == MMU_PAGE_4K || idx == MMU_PAGE_64K)
313 def->tlbiel = 1;
314 else
315 def->tlbiel = 0;
316
317 DBG(" %d: shift=%02x, sllp=%04x, avpnm=%08x, "
318 "tlbiel=%d, penc=%d\n",
319 idx, shift, def->sllp, def->avpnm, def->tlbiel,
320 def->penc);
321 }
322 return 1;
323 }
324 return 0;
325}
326
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100327static void __init htab_init_page_sizes(void)
328{
329 int rc;
330
331 /* Default to 4K pages only */
332 memcpy(mmu_psize_defs, mmu_psize_defaults_old,
333 sizeof(mmu_psize_defaults_old));
334
335 /*
336 * Try to find the available page sizes in the device-tree
337 */
338 rc = of_scan_flat_dt(htab_dt_scan_page_sizes, NULL);
339 if (rc != 0) /* Found */
340 goto found;
341
342 /*
343 * Not in the device-tree, let's fallback on known size
344 * list for 16M capable GP & GR
345 */
Stephen Rothwell04704662006-11-30 11:46:22 +1100346 if (cpu_has_feature(CPU_FTR_16M_PAGE))
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100347 memcpy(mmu_psize_defs, mmu_psize_defaults_gp,
348 sizeof(mmu_psize_defaults_gp));
349 found:
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000350#ifndef CONFIG_DEBUG_PAGEALLOC
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100351 /*
352 * Pick a size for the linear mapping. Currently, we only support
353 * 16M, 1M and 4K which is the default
354 */
355 if (mmu_psize_defs[MMU_PAGE_16M].shift)
356 mmu_linear_psize = MMU_PAGE_16M;
357 else if (mmu_psize_defs[MMU_PAGE_1M].shift)
358 mmu_linear_psize = MMU_PAGE_1M;
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000359#endif /* CONFIG_DEBUG_PAGEALLOC */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100360
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000361#ifdef CONFIG_PPC_64K_PAGES
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100362 /*
363 * Pick a size for the ordinary pages. Default is 4K, we support
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000364 * 64K for user mappings and vmalloc if supported by the processor.
365 * We only use 64k for ioremap if the processor
366 * (and firmware) support cache-inhibited large pages.
367 * If not, we use 4k and set mmu_ci_restrictions so that
368 * hash_page knows to switch processes that use cache-inhibited
369 * mappings to 4k pages.
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100370 */
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000371 if (mmu_psize_defs[MMU_PAGE_64K].shift) {
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100372 mmu_virtual_psize = MMU_PAGE_64K;
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000373 mmu_vmalloc_psize = MMU_PAGE_64K;
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000374 if (mmu_linear_psize == MMU_PAGE_4K)
375 mmu_linear_psize = MMU_PAGE_64K;
Paul Mackerrascfe666b2008-03-24 17:41:22 +1100376 if (cpu_has_feature(CPU_FTR_CI_LARGE_PAGE)) {
377 /*
378 * Don't use 64k pages for ioremap on pSeries, since
379 * that would stop us accessing the HEA ethernet.
380 */
381 if (!machine_is(pseries))
382 mmu_io_psize = MMU_PAGE_64K;
383 } else
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000384 mmu_ci_restrictions = 1;
385 }
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000386#endif /* CONFIG_PPC_64K_PAGES */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100387
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000388 printk(KERN_DEBUG "Page orders: linear mapping = %d, "
389 "virtual = %d, io = %d\n",
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100390 mmu_psize_defs[mmu_linear_psize].shift,
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000391 mmu_psize_defs[mmu_virtual_psize].shift,
392 mmu_psize_defs[mmu_io_psize].shift);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100393
394#ifdef CONFIG_HUGETLB_PAGE
395 /* Init large page size. Currently, we pick 16M or 1M depending
396 * on what is available
397 */
398 if (mmu_psize_defs[MMU_PAGE_16M].shift)
Jon Tollefson4ec161c2008-01-04 09:59:50 +1100399 set_huge_psize(MMU_PAGE_16M);
David Gibson7d24f0b2005-11-07 00:57:52 -0800400 /* With 4k/4level pagetables, we can't (for now) cope with a
401 * huge page size < PMD_SIZE */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100402 else if (mmu_psize_defs[MMU_PAGE_1M].shift)
Jon Tollefson4ec161c2008-01-04 09:59:50 +1100403 set_huge_psize(MMU_PAGE_1M);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100404#endif /* CONFIG_HUGETLB_PAGE */
405}
406
407static int __init htab_dt_scan_pftsize(unsigned long node,
408 const char *uname, int depth,
409 void *data)
410{
411 char *type = of_get_flat_dt_prop(node, "device_type", NULL);
412 u32 *prop;
413
414 /* We are scanning "cpu" nodes only */
415 if (type == NULL || strcmp(type, "cpu") != 0)
416 return 0;
417
418 prop = (u32 *)of_get_flat_dt_prop(node, "ibm,pft-size", NULL);
419 if (prop != NULL) {
420 /* pft_size[0] is the NUMA CEC cookie */
421 ppc64_pft_size = prop[1];
422 return 1;
423 }
424 return 0;
425}
426
427static unsigned long __init htab_get_table_size(void)
Paul Mackerras3eac8c62005-10-12 16:58:53 +1000428{
Paul Mackerras799d6042005-11-10 13:37:51 +1100429 unsigned long mem_size, rnd_mem_size, pteg_count;
Paul Mackerras3eac8c62005-10-12 16:58:53 +1000430
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100431 /* If hash size isn't already provided by the platform, we try to
Adrian Bunk943ffb52006-01-10 00:10:13 +0100432 * retrieve it from the device-tree. If it's not there neither, we
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100433 * calculate it now based on the total RAM size
Paul Mackerras3eac8c62005-10-12 16:58:53 +1000434 */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100435 if (ppc64_pft_size == 0)
436 of_scan_flat_dt(htab_dt_scan_pftsize, NULL);
Paul Mackerras3eac8c62005-10-12 16:58:53 +1000437 if (ppc64_pft_size)
438 return 1UL << ppc64_pft_size;
439
440 /* round mem_size up to next power of 2 */
Paul Mackerras799d6042005-11-10 13:37:51 +1100441 mem_size = lmb_phys_mem_size();
442 rnd_mem_size = 1UL << __ilog2(mem_size);
443 if (rnd_mem_size < mem_size)
Paul Mackerras3eac8c62005-10-12 16:58:53 +1000444 rnd_mem_size <<= 1;
445
446 /* # pages / 2 */
447 pteg_count = max(rnd_mem_size >> (12 + 1), 1UL << 11);
448
449 return pteg_count << 7;
450}
451
Mike Kravetz54b79242005-11-07 16:25:48 -0800452#ifdef CONFIG_MEMORY_HOTPLUG
453void create_section_mapping(unsigned long start, unsigned long end)
454{
Michael Ellermancaf80e52006-03-21 20:45:51 +1100455 BUG_ON(htab_bolt_mapping(start, end, __pa(start),
Mike Kravetz54b79242005-11-07 16:25:48 -0800456 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_COHERENT | PP_RWXX,
Paul Mackerras1189be62007-10-11 20:37:10 +1000457 mmu_linear_psize, mmu_kernel_ssize));
Mike Kravetz54b79242005-11-07 16:25:48 -0800458}
Badari Pulavartyf8c88032008-01-29 09:19:24 +1100459
Badari Pulavarty52db9b42008-03-28 11:37:21 +1100460int remove_section_mapping(unsigned long start, unsigned long end)
Badari Pulavartyf8c88032008-01-29 09:19:24 +1100461{
Badari Pulavarty52db9b42008-03-28 11:37:21 +1100462 return htab_remove_mapping(start, end, mmu_linear_psize,
463 mmu_kernel_ssize);
Badari Pulavartyf8c88032008-01-29 09:19:24 +1100464}
Mike Kravetz54b79242005-11-07 16:25:48 -0800465#endif /* CONFIG_MEMORY_HOTPLUG */
466
Michael Ellerman7d0daae2006-06-23 18:16:38 +1000467static inline void make_bl(unsigned int *insn_addr, void *func)
468{
469 unsigned long funcp = *((unsigned long *)func);
470 int offset = funcp - (unsigned long)insn_addr;
471
472 *insn_addr = (unsigned int)(0x48000001 | (offset & 0x03fffffc));
473 flush_icache_range((unsigned long)insn_addr, 4+
474 (unsigned long)insn_addr);
475}
476
477static void __init htab_finish_init(void)
478{
479 extern unsigned int *htab_call_hpte_insert1;
480 extern unsigned int *htab_call_hpte_insert2;
481 extern unsigned int *htab_call_hpte_remove;
482 extern unsigned int *htab_call_hpte_updatepp;
483
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +1000484#ifdef CONFIG_PPC_HAS_HASH_64K
Michael Ellerman7d0daae2006-06-23 18:16:38 +1000485 extern unsigned int *ht64_call_hpte_insert1;
486 extern unsigned int *ht64_call_hpte_insert2;
487 extern unsigned int *ht64_call_hpte_remove;
488 extern unsigned int *ht64_call_hpte_updatepp;
489
490 make_bl(ht64_call_hpte_insert1, ppc_md.hpte_insert);
491 make_bl(ht64_call_hpte_insert2, ppc_md.hpte_insert);
492 make_bl(ht64_call_hpte_remove, ppc_md.hpte_remove);
493 make_bl(ht64_call_hpte_updatepp, ppc_md.hpte_updatepp);
Jon Tollefson5b825832007-05-17 04:43:02 +1000494#endif /* CONFIG_PPC_HAS_HASH_64K */
Michael Ellerman7d0daae2006-06-23 18:16:38 +1000495
496 make_bl(htab_call_hpte_insert1, ppc_md.hpte_insert);
497 make_bl(htab_call_hpte_insert2, ppc_md.hpte_insert);
498 make_bl(htab_call_hpte_remove, ppc_md.hpte_remove);
499 make_bl(htab_call_hpte_updatepp, ppc_md.hpte_updatepp);
500}
501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502void __init htab_initialize(void)
503{
Michael Ellerman337a7122006-02-21 17:22:55 +1100504 unsigned long table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 unsigned long pteg_count;
506 unsigned long mode_rw;
Michael Ellerman41d824b2008-01-30 01:13:59 +1100507 unsigned long base = 0, size = 0, limit;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100508 int i;
509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 extern unsigned long tce_alloc_start, tce_alloc_end;
511
512 DBG(" -> htab_initialize()\n");
513
Paul Mackerras1189be62007-10-11 20:37:10 +1000514 /* Initialize segment sizes */
515 htab_init_seg_sizes();
516
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100517 /* Initialize page sizes */
518 htab_init_page_sizes();
519
Paul Mackerras1189be62007-10-11 20:37:10 +1000520 if (cpu_has_feature(CPU_FTR_1T_SEGMENT)) {
521 mmu_kernel_ssize = MMU_SEGSIZE_1T;
522 mmu_highuser_ssize = MMU_SEGSIZE_1T;
523 printk(KERN_INFO "Using 1TB segments\n");
524 }
525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 /*
527 * Calculate the required size of the htab. We want the number of
528 * PTEGs to equal one half the number of real pages.
529 */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100530 htab_size_bytes = htab_get_table_size();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 pteg_count = htab_size_bytes >> 7;
532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 htab_hash_mask = pteg_count - 1;
534
Michael Ellerman57cfb812006-03-21 20:45:59 +1100535 if (firmware_has_feature(FW_FEATURE_LPAR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 /* Using a hypervisor which owns the htab */
537 htab_address = NULL;
538 _SDR1 = 0;
539 } else {
540 /* Find storage for the HPT. Must be contiguous in
Michael Ellerman41d824b2008-01-30 01:13:59 +1100541 * the absolute address space. On cell we want it to be
Michael Ellerman31bf1112008-03-12 18:03:24 +1100542 * in the first 2 Gig so we can use it for IOMMU hacks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 */
Michael Ellerman41d824b2008-01-30 01:13:59 +1100544 if (machine_is(cell))
Michael Ellerman31bf1112008-03-12 18:03:24 +1100545 limit = 0x80000000;
Michael Ellerman41d824b2008-01-30 01:13:59 +1100546 else
547 limit = 0;
548
549 table = lmb_alloc_base(htab_size_bytes, htab_size_bytes, limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 DBG("Hash table allocated at %lx, size: %lx\n", table,
552 htab_size_bytes);
553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 htab_address = abs_to_virt(table);
555
556 /* htab absolute addr + encoded htabsize */
557 _SDR1 = table + __ilog2(pteg_count) - 11;
558
559 /* Initialize the HPT with no entries */
560 memset((void *)table, 0, htab_size_bytes);
Paul Mackerras799d6042005-11-10 13:37:51 +1100561
562 /* Set SDR1 */
563 mtspr(SPRN_SDR1, _SDR1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 }
565
Anton Blanchard515bae92005-06-21 17:15:55 -0700566 mode_rw = _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_COHERENT | PP_RWXX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000568#ifdef CONFIG_DEBUG_PAGEALLOC
569 linear_map_hash_count = lmb_end_of_DRAM() >> PAGE_SHIFT;
570 linear_map_hash_slots = __va(lmb_alloc_base(linear_map_hash_count,
571 1, lmb.rmo_size));
572 memset(linear_map_hash_slots, 0, linear_map_hash_count);
573#endif /* CONFIG_DEBUG_PAGEALLOC */
574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 /* On U3 based machines, we need to reserve the DART area and
576 * _NOT_ map it to avoid cache paradoxes as it's remapped non
577 * cacheable later on
578 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580 /* create bolted the linear mapping in the hash table */
581 for (i=0; i < lmb.memory.cnt; i++) {
Michael Ellermanb5666f72005-12-05 10:24:33 -0600582 base = (unsigned long)__va(lmb.memory.region[i].base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 size = lmb.memory.region[i].size;
584
585 DBG("creating mapping for region: %lx : %lx\n", base, size);
586
587#ifdef CONFIG_U3_DART
588 /* Do not map the DART space. Fortunately, it will be aligned
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100589 * in such a way that it will not cross two lmb regions and
590 * will fit within a single 16Mb page.
591 * The DART space is assumed to be a full 16Mb region even if
592 * we only use 2Mb of that space. We will use more of it later
593 * for AGP GART. We have to use a full 16Mb large page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 */
595 DBG("DART base: %lx\n", dart_tablebase);
596
597 if (dart_tablebase != 0 && dart_tablebase >= base
598 && dart_tablebase < (base + size)) {
Michael Ellermancaf80e52006-03-21 20:45:51 +1100599 unsigned long dart_table_end = dart_tablebase + 16 * MB;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 if (base != dart_tablebase)
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100601 BUG_ON(htab_bolt_mapping(base, dart_tablebase,
Michael Ellermancaf80e52006-03-21 20:45:51 +1100602 __pa(base), mode_rw,
Paul Mackerras1189be62007-10-11 20:37:10 +1000603 mmu_linear_psize,
604 mmu_kernel_ssize));
Michael Ellermancaf80e52006-03-21 20:45:51 +1100605 if ((base + size) > dart_table_end)
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100606 BUG_ON(htab_bolt_mapping(dart_tablebase+16*MB,
Michael Ellermancaf80e52006-03-21 20:45:51 +1100607 base + size,
608 __pa(dart_table_end),
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100609 mode_rw,
Paul Mackerras1189be62007-10-11 20:37:10 +1000610 mmu_linear_psize,
611 mmu_kernel_ssize));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 continue;
613 }
614#endif /* CONFIG_U3_DART */
Michael Ellermancaf80e52006-03-21 20:45:51 +1100615 BUG_ON(htab_bolt_mapping(base, base + size, __pa(base),
Paul Mackerras1189be62007-10-11 20:37:10 +1000616 mode_rw, mmu_linear_psize, mmu_kernel_ssize));
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100617 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619 /*
620 * If we have a memory_limit and we've allocated TCEs then we need to
621 * explicitly map the TCE area at the top of RAM. We also cope with the
622 * case that the TCEs start below memory_limit.
623 * tce_alloc_start/end are 16MB aligned so the mapping should work
624 * for either 4K or 16MB pages.
625 */
626 if (tce_alloc_start) {
Michael Ellermanb5666f72005-12-05 10:24:33 -0600627 tce_alloc_start = (unsigned long)__va(tce_alloc_start);
628 tce_alloc_end = (unsigned long)__va(tce_alloc_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
630 if (base + size >= tce_alloc_start)
631 tce_alloc_start = base + size + 1;
632
Michael Ellermancaf80e52006-03-21 20:45:51 +1100633 BUG_ON(htab_bolt_mapping(tce_alloc_start, tce_alloc_end,
634 __pa(tce_alloc_start), mode_rw,
Paul Mackerras1189be62007-10-11 20:37:10 +1000635 mmu_linear_psize, mmu_kernel_ssize));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 }
637
Michael Ellerman7d0daae2006-06-23 18:16:38 +1000638 htab_finish_init();
639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 DBG(" <- htab_initialize()\n");
641}
642#undef KB
643#undef MB
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Anton Blancharde597cb322005-12-29 10:46:29 +1100645void htab_initialize_secondary(void)
Paul Mackerras799d6042005-11-10 13:37:51 +1100646{
Michael Ellerman57cfb812006-03-21 20:45:59 +1100647 if (!firmware_has_feature(FW_FEATURE_LPAR))
Paul Mackerras799d6042005-11-10 13:37:51 +1100648 mtspr(SPRN_SDR1, _SDR1);
649}
650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651/*
652 * Called by asm hashtable.S for doing lazy icache flush
653 */
654unsigned int hash_page_do_lazy_icache(unsigned int pp, pte_t pte, int trap)
655{
656 struct page *page;
657
Benjamin Herrenschmidt76c8e252005-11-08 11:21:05 +1100658 if (!pfn_valid(pte_pfn(pte)))
659 return pp;
660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 page = pte_page(pte);
662
663 /* page is dirty */
664 if (!test_bit(PG_arch_1, &page->flags) && !PageReserved(page)) {
665 if (trap == 0x400) {
666 __flush_dcache_icache(page_address(page));
667 set_bit(PG_arch_1, &page->flags);
668 } else
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100669 pp |= HPTE_R_N;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 }
671 return pp;
672}
673
Paul Mackerras721151d2007-04-03 21:24:02 +1000674/*
675 * Demote a segment to using 4k pages.
676 * For now this makes the whole process use 4k pages.
677 */
Paul Mackerras721151d2007-04-03 21:24:02 +1000678#ifdef CONFIG_PPC_64K_PAGES
Paul Mackerrasfa282372008-01-24 08:35:13 +1100679void demote_segment_4k(struct mm_struct *mm, unsigned long addr)
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +1000680{
Paul Mackerras721151d2007-04-03 21:24:02 +1000681 if (mm->context.user_psize == MMU_PAGE_4K)
682 return;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000683 slice_set_user_psize(mm, MMU_PAGE_4K);
Geert Uytterhoeven1e57ba82007-07-17 02:35:38 +1000684#ifdef CONFIG_SPU_BASE
Paul Mackerras721151d2007-04-03 21:24:02 +1000685 spu_flush_all_slbs(mm);
686#endif
Paul Mackerrasfa282372008-01-24 08:35:13 +1100687 if (get_paca()->context.user_psize != MMU_PAGE_4K) {
688 get_paca()->context = mm->context;
689 slb_flush_and_rebolt();
690 }
Paul Mackerras721151d2007-04-03 21:24:02 +1000691}
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +1000692#endif /* CONFIG_PPC_64K_PAGES */
Paul Mackerras721151d2007-04-03 21:24:02 +1000693
Paul Mackerrasfa282372008-01-24 08:35:13 +1100694#ifdef CONFIG_PPC_SUBPAGE_PROT
695/*
696 * This looks up a 2-bit protection code for a 4k subpage of a 64k page.
697 * Userspace sets the subpage permissions using the subpage_prot system call.
698 *
699 * Result is 0: full permissions, _PAGE_RW: read-only,
700 * _PAGE_USER or _PAGE_USER|_PAGE_RW: no access.
701 */
702static int subpage_protection(pgd_t *pgdir, unsigned long ea)
703{
704 struct subpage_prot_table *spt = pgd_subpage_prot(pgdir);
705 u32 spp = 0;
706 u32 **sbpm, *sbpp;
707
708 if (ea >= spt->maxaddr)
709 return 0;
710 if (ea < 0x100000000) {
711 /* addresses below 4GB use spt->low_prot */
712 sbpm = spt->low_prot;
713 } else {
714 sbpm = spt->protptrs[ea >> SBP_L3_SHIFT];
715 if (!sbpm)
716 return 0;
717 }
718 sbpp = sbpm[(ea >> SBP_L2_SHIFT) & (SBP_L2_COUNT - 1)];
719 if (!sbpp)
720 return 0;
721 spp = sbpp[(ea >> PAGE_SHIFT) & (SBP_L1_COUNT - 1)];
722
723 /* extract 2-bit bitfield for this 4k subpage */
724 spp >>= 30 - 2 * ((ea >> 12) & 0xf);
725
726 /* turn 0,1,2,3 into combination of _PAGE_USER and _PAGE_RW */
727 spp = ((spp & 2) ? _PAGE_USER : 0) | ((spp & 1) ? _PAGE_RW : 0);
728 return spp;
729}
730
731#else /* CONFIG_PPC_SUBPAGE_PROT */
732static inline int subpage_protection(pgd_t *pgdir, unsigned long ea)
733{
734 return 0;
735}
736#endif
737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738/* Result code is:
739 * 0 - handled
740 * 1 - normal page fault
741 * -1 - critical hash insertion error
Paul Mackerrasfa282372008-01-24 08:35:13 +1100742 * -2 - access not permitted by subpage protection mechanism
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 */
744int hash_page(unsigned long ea, unsigned long access, unsigned long trap)
745{
746 void *pgdir;
747 unsigned long vsid;
748 struct mm_struct *mm;
749 pte_t *ptep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 cpumask_t tmp;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100751 int rc, user_region = 0, local = 0;
Paul Mackerras1189be62007-10-11 20:37:10 +1000752 int psize, ssize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100754 DBG_LOW("hash_page(ea=%016lx, access=%lx, trap=%lx\n",
755 ea, access, trap);
David Gibson1f8d4192005-05-05 16:15:13 -0700756
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100757 if ((ea & ~REGION_MASK) >= PGTABLE_RANGE) {
758 DBG_LOW(" out of pgtable range !\n");
759 return 1;
760 }
761
762 /* Get region & vsid */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 switch (REGION_ID(ea)) {
764 case USER_REGION_ID:
765 user_region = 1;
766 mm = current->mm;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100767 if (! mm) {
768 DBG_LOW(" user region with no mm !\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 return 1;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100770 }
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +1000771#ifdef CONFIG_PPC_MM_SLICES
772 psize = get_slice_psize(mm, ea);
773#else
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000774 psize = mm->context.user_psize;
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +1000775#endif
Paul Mackerras1189be62007-10-11 20:37:10 +1000776 ssize = user_segment_size(ea);
777 vsid = get_vsid(mm->context.id, ea, ssize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 case VMALLOC_REGION_ID:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 mm = &init_mm;
Paul Mackerras1189be62007-10-11 20:37:10 +1000781 vsid = get_kernel_vsid(ea, mmu_kernel_ssize);
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000782 if (ea < VMALLOC_END)
783 psize = mmu_vmalloc_psize;
784 else
785 psize = mmu_io_psize;
Paul Mackerras1189be62007-10-11 20:37:10 +1000786 ssize = mmu_kernel_ssize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 default:
789 /* Not a valid range
790 * Send the problem up to do_page_fault
791 */
792 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 }
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100794 DBG_LOW(" mm=%p, mm->pgdir=%p, vsid=%016lx\n", mm, mm->pgd, vsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100796 /* Get pgdir */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 pgdir = mm->pgd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 if (pgdir == NULL)
799 return 1;
800
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100801 /* Check CPU locality */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 tmp = cpumask_of_cpu(smp_processor_id());
803 if (user_region && cpus_equal(mm->cpu_vm_mask, tmp))
804 local = 1;
805
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000806#ifdef CONFIG_HUGETLB_PAGE
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100807 /* Handle hugepage regions */
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +1000808 if (HPAGE_SHIFT && psize == mmu_huge_psize) {
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100809 DBG_LOW(" -> huge page !\n");
David Gibsoncbf52af2005-12-09 14:20:52 +1100810 return hash_huge_page(mm, access, ea, vsid, local, trap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 }
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000812#endif /* CONFIG_HUGETLB_PAGE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +1000814#ifndef CONFIG_PPC_64K_PAGES
815 /* If we use 4K pages and our psize is not 4K, then we are hitting
816 * a special driver mapping, we need to align the address before
817 * we fetch the PTE
818 */
819 if (psize != MMU_PAGE_4K)
820 ea &= ~((1ul << mmu_psize_defs[psize].shift) - 1);
821#endif /* CONFIG_PPC_64K_PAGES */
822
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100823 /* Get PTE and page size from page tables */
824 ptep = find_linux_pte(pgdir, ea);
825 if (ptep == NULL || !pte_present(*ptep)) {
826 DBG_LOW(" no PTE !\n");
827 return 1;
828 }
829
830#ifndef CONFIG_PPC_64K_PAGES
831 DBG_LOW(" i-pte: %016lx\n", pte_val(*ptep));
832#else
833 DBG_LOW(" i-pte: %016lx %016lx\n", pte_val(*ptep),
834 pte_val(*(ptep + PTRS_PER_PTE)));
835#endif
836 /* Pre-check access permissions (will be re-checked atomically
837 * in __hash_page_XX but this pre-check is a fast path
838 */
839 if (access & ~pte_val(*ptep)) {
840 DBG_LOW(" no access !\n");
841 return 1;
842 }
843
844 /* Do actual hashing */
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +1000845#ifdef CONFIG_PPC_64K_PAGES
Paul Mackerras721151d2007-04-03 21:24:02 +1000846 /* If _PAGE_4K_PFN is set, make sure this is a 4k segment */
847 if (pte_val(*ptep) & _PAGE_4K_PFN) {
848 demote_segment_4k(mm, ea);
849 psize = MMU_PAGE_4K;
850 }
851
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +1000852 /* If this PTE is non-cacheable and we have restrictions on
853 * using non cacheable large pages, then we switch to 4k
854 */
855 if (mmu_ci_restrictions && psize == MMU_PAGE_64K &&
856 (pte_val(*ptep) & _PAGE_NO_CACHE)) {
857 if (user_region) {
858 demote_segment_4k(mm, ea);
859 psize = MMU_PAGE_4K;
860 } else if (ea < VMALLOC_END) {
861 /*
862 * some driver did a non-cacheable mapping
863 * in vmalloc space, so switch vmalloc
864 * to 4k pages
865 */
866 printk(KERN_ALERT "Reducing vmalloc segment "
867 "to 4kB pages because of "
868 "non-cacheable mapping\n");
869 psize = mmu_vmalloc_psize = MMU_PAGE_4K;
Geert Uytterhoeven1e57ba82007-07-17 02:35:38 +1000870#ifdef CONFIG_SPU_BASE
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +0100871 spu_flush_all_slbs(mm);
872#endif
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000873 }
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +1000874 }
875 if (user_region) {
876 if (psize != get_paca()->context.user_psize) {
Benjamin Herrenschmidtf6ab0b92007-10-29 12:05:18 +1100877 get_paca()->context = mm->context;
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000878 slb_flush_and_rebolt();
879 }
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +1000880 } else if (get_paca()->vmalloc_sllp !=
881 mmu_psize_defs[mmu_vmalloc_psize].sllp) {
882 get_paca()->vmalloc_sllp =
883 mmu_psize_defs[mmu_vmalloc_psize].sllp;
Michael Neuling67439b72007-08-03 11:55:39 +1000884 slb_vmalloc_update();
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000885 }
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +1000886#endif /* CONFIG_PPC_64K_PAGES */
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +1000887
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +1000888#ifdef CONFIG_PPC_HAS_HASH_64K
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000889 if (psize == MMU_PAGE_64K)
Paul Mackerras1189be62007-10-11 20:37:10 +1000890 rc = __hash_page_64K(ea, access, vsid, ptep, trap, local, ssize);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100891 else
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +1000892#endif /* CONFIG_PPC_HAS_HASH_64K */
Paul Mackerrasfa282372008-01-24 08:35:13 +1100893 {
894 int spp = subpage_protection(pgdir, ea);
895 if (access & spp)
896 rc = -2;
897 else
898 rc = __hash_page_4K(ea, access, vsid, ptep, trap,
899 local, ssize, spp);
900 }
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100901
902#ifndef CONFIG_PPC_64K_PAGES
903 DBG_LOW(" o-pte: %016lx\n", pte_val(*ptep));
904#else
905 DBG_LOW(" o-pte: %016lx %016lx\n", pte_val(*ptep),
906 pte_val(*(ptep + PTRS_PER_PTE)));
907#endif
908 DBG_LOW(" -> rc=%d\n", rc);
909 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910}
Arnd Bergmann67207b92005-11-15 15:53:48 -0500911EXPORT_SYMBOL_GPL(hash_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100913void hash_preload(struct mm_struct *mm, unsigned long ea,
914 unsigned long access, unsigned long trap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915{
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100916 unsigned long vsid;
917 void *pgdir;
918 pte_t *ptep;
919 cpumask_t mask;
920 unsigned long flags;
921 int local = 0;
Paul Mackerras1189be62007-10-11 20:37:10 +1000922 int ssize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000924 BUG_ON(REGION_ID(ea) != USER_REGION_ID);
925
926#ifdef CONFIG_PPC_MM_SLICES
927 /* We only prefault standard pages for now */
Ilpo Järvinen2b02d132007-08-16 08:03:35 +1000928 if (unlikely(get_slice_psize(mm, ea) != mm->context.user_psize))
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100929 return;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000930#endif
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100931
932 DBG_LOW("hash_preload(mm=%p, mm->pgdir=%p, ea=%016lx, access=%lx,"
933 " trap=%lx\n", mm, mm->pgd, ea, access, trap);
934
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +1000935 /* Get Linux PTE if available */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100936 pgdir = mm->pgd;
937 if (pgdir == NULL)
938 return;
939 ptep = find_linux_pte(pgdir, ea);
940 if (!ptep)
941 return;
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +1000942
943#ifdef CONFIG_PPC_64K_PAGES
944 /* If either _PAGE_4K_PFN or _PAGE_NO_CACHE is set (and we are on
945 * a 64K kernel), then we don't preload, hash_page() will take
946 * care of it once we actually try to access the page.
947 * That way we don't have to duplicate all of the logic for segment
948 * page size demotion here
949 */
950 if (pte_val(*ptep) & (_PAGE_4K_PFN | _PAGE_NO_CACHE))
951 return;
952#endif /* CONFIG_PPC_64K_PAGES */
953
954 /* Get VSID */
Paul Mackerras1189be62007-10-11 20:37:10 +1000955 ssize = user_segment_size(ea);
956 vsid = get_vsid(mm->context.id, ea, ssize);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100957
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +1000958 /* Hash doesn't like irqs */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100959 local_irq_save(flags);
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +1000960
961 /* Is that local to this CPU ? */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100962 mask = cpumask_of_cpu(smp_processor_id());
963 if (cpus_equal(mm->cpu_vm_mask, mask))
964 local = 1;
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +1000965
966 /* Hash it in */
967#ifdef CONFIG_PPC_HAS_HASH_64K
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000968 if (mm->context.user_psize == MMU_PAGE_64K)
Paul Mackerras1189be62007-10-11 20:37:10 +1000969 __hash_page_64K(ea, access, vsid, ptep, trap, local, ssize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 else
Jon Tollefson5b825832007-05-17 04:43:02 +1000971#endif /* CONFIG_PPC_HAS_HASH_64K */
Paul Mackerrasfa282372008-01-24 08:35:13 +1100972 __hash_page_4K(ea, access, vsid, ptep, trap, local, ssize,
973 subpage_protection(pgdir, ea));
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +1000974
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100975 local_irq_restore(flags);
976}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Benjamin Herrenschmidtf6ab0b92007-10-29 12:05:18 +1100978/* WARNING: This is called from hash_low_64.S, if you change this prototype,
979 * do not forget to update the assembly call site !
980 */
Paul Mackerras1189be62007-10-11 20:37:10 +1000981void flush_hash_page(unsigned long va, real_pte_t pte, int psize, int ssize,
982 int local)
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100983{
984 unsigned long hash, index, shift, hidx, slot;
985
986 DBG_LOW("flush_hash_page(va=%016x)\n", va);
987 pte_iterate_hashed_subpages(pte, psize, va, index, shift) {
Paul Mackerras1189be62007-10-11 20:37:10 +1000988 hash = hpt_hash(va, shift, ssize);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100989 hidx = __rpte_to_hidx(pte, index);
990 if (hidx & _PTEIDX_SECONDARY)
991 hash = ~hash;
992 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
993 slot += hidx & _PTEIDX_GROUP_IX;
994 DBG_LOW(" sub %d: hash=%x, hidx=%x\n", index, slot, hidx);
Paul Mackerras1189be62007-10-11 20:37:10 +1000995 ppc_md.hpte_invalidate(slot, va, psize, ssize, local);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100996 } pte_iterate_hashed_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997}
998
Benjamin Herrenschmidt61b1a942005-09-20 13:52:50 +1000999void flush_hash_range(unsigned long number, int local)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000{
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001001 if (ppc_md.flush_hash_range)
Benjamin Herrenschmidt61b1a942005-09-20 13:52:50 +10001002 ppc_md.flush_hash_range(number, local);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001003 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 int i;
Benjamin Herrenschmidt61b1a942005-09-20 13:52:50 +10001005 struct ppc64_tlb_batch *batch =
1006 &__get_cpu_var(ppc64_tlb_batch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
1008 for (i = 0; i < number; i++)
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001009 flush_hash_page(batch->vaddr[i], batch->pte[i],
Paul Mackerras1189be62007-10-11 20:37:10 +10001010 batch->psize, batch->ssize, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 }
1012}
1013
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014/*
1015 * low_hash_fault is called when we the low level hash code failed
1016 * to instert a PTE due to an hypervisor error
1017 */
Paul Mackerrasfa282372008-01-24 08:35:13 +11001018void low_hash_fault(struct pt_regs *regs, unsigned long address, int rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019{
1020 if (user_mode(regs)) {
Paul Mackerrasfa282372008-01-24 08:35:13 +11001021#ifdef CONFIG_PPC_SUBPAGE_PROT
1022 if (rc == -2)
1023 _exception(SIGSEGV, regs, SEGV_ACCERR, address);
1024 else
1025#endif
1026 _exception(SIGBUS, regs, BUS_ADRERR, address);
1027 } else
1028 bad_page_fault(regs, address, SIGBUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029}
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +10001030
1031#ifdef CONFIG_DEBUG_PAGEALLOC
1032static void kernel_map_linear_page(unsigned long vaddr, unsigned long lmi)
1033{
Paul Mackerras1189be62007-10-11 20:37:10 +10001034 unsigned long hash, hpteg;
1035 unsigned long vsid = get_kernel_vsid(vaddr, mmu_kernel_ssize);
1036 unsigned long va = hpt_va(vaddr, vsid, mmu_kernel_ssize);
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +10001037 unsigned long mode = _PAGE_ACCESSED | _PAGE_DIRTY |
1038 _PAGE_COHERENT | PP_RWXX | HPTE_R_N;
1039 int ret;
1040
Paul Mackerras1189be62007-10-11 20:37:10 +10001041 hash = hpt_hash(va, PAGE_SHIFT, mmu_kernel_ssize);
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +10001042 hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
1043
1044 ret = ppc_md.hpte_insert(hpteg, va, __pa(vaddr),
Paul Mackerras1189be62007-10-11 20:37:10 +10001045 mode, HPTE_V_BOLTED,
1046 mmu_linear_psize, mmu_kernel_ssize);
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +10001047 BUG_ON (ret < 0);
1048 spin_lock(&linear_map_hash_lock);
1049 BUG_ON(linear_map_hash_slots[lmi] & 0x80);
1050 linear_map_hash_slots[lmi] = ret | 0x80;
1051 spin_unlock(&linear_map_hash_lock);
1052}
1053
1054static void kernel_unmap_linear_page(unsigned long vaddr, unsigned long lmi)
1055{
Paul Mackerras1189be62007-10-11 20:37:10 +10001056 unsigned long hash, hidx, slot;
1057 unsigned long vsid = get_kernel_vsid(vaddr, mmu_kernel_ssize);
1058 unsigned long va = hpt_va(vaddr, vsid, mmu_kernel_ssize);
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +10001059
Paul Mackerras1189be62007-10-11 20:37:10 +10001060 hash = hpt_hash(va, PAGE_SHIFT, mmu_kernel_ssize);
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +10001061 spin_lock(&linear_map_hash_lock);
1062 BUG_ON(!(linear_map_hash_slots[lmi] & 0x80));
1063 hidx = linear_map_hash_slots[lmi] & 0x7f;
1064 linear_map_hash_slots[lmi] = 0;
1065 spin_unlock(&linear_map_hash_lock);
1066 if (hidx & _PTEIDX_SECONDARY)
1067 hash = ~hash;
1068 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
1069 slot += hidx & _PTEIDX_GROUP_IX;
Paul Mackerras1189be62007-10-11 20:37:10 +10001070 ppc_md.hpte_invalidate(slot, va, mmu_linear_psize, mmu_kernel_ssize, 0);
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +10001071}
1072
1073void kernel_map_pages(struct page *page, int numpages, int enable)
1074{
1075 unsigned long flags, vaddr, lmi;
1076 int i;
1077
1078 local_irq_save(flags);
1079 for (i = 0; i < numpages; i++, page++) {
1080 vaddr = (unsigned long)page_address(page);
1081 lmi = __pa(vaddr) >> PAGE_SHIFT;
1082 if (lmi >= linear_map_hash_count)
1083 continue;
1084 if (enable)
1085 kernel_map_linear_page(vaddr, lmi);
1086 else
1087 kernel_unmap_linear_page(vaddr, lmi);
1088 }
1089 local_irq_restore(flags);
1090}
1091#endif /* CONFIG_DEBUG_PAGEALLOC */