blob: 1a4b4b36b0c6e66c64f87e0d930b1246c6b071f1 [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 */
Michael Ellerman09de9ff2008-05-08 14:27:07 +1000120static struct mmu_psize_def mmu_psize_defaults_old[] = {
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100121 [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 */
Michael Ellerman09de9ff2008-05-08 14:27:07 +1000134static struct mmu_psize_def mmu_psize_defaults_gp[] = {
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100135 [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
Stephen Rothwellae86f002008-03-27 16:08:57 +1100195#ifdef CONFIG_MEMORY_HOTPLUG
Badari Pulavarty52db9b42008-03-28 11:37:21 +1100196static int htab_remove_mapping(unsigned long vstart, unsigned long vend,
Badari Pulavartyf8c88032008-01-29 09:19:24 +1100197 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) {
Badari Pulavarty52db9b42008-03-28 11:37:21 +1100206 printk(KERN_WARNING "Platform doesn't implement "
207 "hpte_removebolted\n");
208 return -EINVAL;
Badari Pulavartyf8c88032008-01-29 09:19:24 +1100209 }
210
211 for (vaddr = vstart; vaddr < vend; vaddr += step)
212 ppc_md.hpte_removebolted(vaddr, psize, ssize);
Badari Pulavarty52db9b42008-03-28 11:37:21 +1100213
214 return 0;
Badari Pulavartyf8c88032008-01-29 09:19:24 +1100215}
Stephen Rothwellae86f002008-03-27 16:08:57 +1100216#endif /* CONFIG_MEMORY_HOTPLUG */
Badari Pulavartyf8c88032008-01-29 09:19:24 +1100217
Paul Mackerras1189be62007-10-11 20:37:10 +1000218static 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;
Olof Johanssonf5534002007-10-12 16:44:55 +1000238 return 1;
Paul Mackerras1189be62007-10-11 20:37:10 +1000239 }
Paul Mackerras1189be62007-10-11 20:37:10 +1000240 }
Olof Johanssonf66bce52007-10-16 00:58:59 +1000241 cur_cpu_spec->cpu_features &= ~CPU_FTR_NO_SLBIE_B;
Paul Mackerras1189be62007-10-11 20:37:10 +1000242 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
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100250static 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);
323 }
324 return 1;
325 }
326 return 0;
327}
328
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100329static 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 */
Stephen Rothwell04704662006-11-30 11:46:22 +1100348 if (cpu_has_feature(CPU_FTR_16M_PAGE))
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100349 memcpy(mmu_psize_defs, mmu_psize_defaults_gp,
350 sizeof(mmu_psize_defaults_gp));
351 found:
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000352#ifndef CONFIG_DEBUG_PAGEALLOC
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100353 /*
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;
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000361#endif /* CONFIG_DEBUG_PAGEALLOC */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100362
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000363#ifdef CONFIG_PPC_64K_PAGES
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100364 /*
365 * Pick a size for the ordinary pages. Default is 4K, we support
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000366 * 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.
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100372 */
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000373 if (mmu_psize_defs[MMU_PAGE_64K].shift) {
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100374 mmu_virtual_psize = MMU_PAGE_64K;
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000375 mmu_vmalloc_psize = MMU_PAGE_64K;
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000376 if (mmu_linear_psize == MMU_PAGE_4K)
377 mmu_linear_psize = MMU_PAGE_64K;
Paul Mackerrascfe666b2008-03-24 17:41:22 +1100378 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
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000386 mmu_ci_restrictions = 1;
387 }
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000388#endif /* CONFIG_PPC_64K_PAGES */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100389
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000390 printk(KERN_DEBUG "Page orders: linear mapping = %d, "
391 "virtual = %d, io = %d\n",
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100392 mmu_psize_defs[mmu_linear_psize].shift,
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000393 mmu_psize_defs[mmu_virtual_psize].shift,
394 mmu_psize_defs[mmu_io_psize].shift);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100395
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)
Jon Tollefson4ec161c2008-01-04 09:59:50 +1100401 set_huge_psize(MMU_PAGE_16M);
David Gibson7d24f0b2005-11-07 00:57:52 -0800402 /* With 4k/4level pagetables, we can't (for now) cope with a
403 * huge page size < PMD_SIZE */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100404 else if (mmu_psize_defs[MMU_PAGE_1M].shift)
Jon Tollefson4ec161c2008-01-04 09:59:50 +1100405 set_huge_psize(MMU_PAGE_1M);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100406#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;
425 }
426 return 0;
427}
428
429static unsigned long __init htab_get_table_size(void)
Paul Mackerras3eac8c62005-10-12 16:58:53 +1000430{
Paul Mackerras799d6042005-11-10 13:37:51 +1100431 unsigned long mem_size, rnd_mem_size, pteg_count;
Paul Mackerras3eac8c62005-10-12 16:58:53 +1000432
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100433 /* If hash size isn't already provided by the platform, we try to
Adrian Bunk943ffb52006-01-10 00:10:13 +0100434 * retrieve it from the device-tree. If it's not there neither, we
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100435 * calculate it now based on the total RAM size
Paul Mackerras3eac8c62005-10-12 16:58:53 +1000436 */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100437 if (ppc64_pft_size == 0)
438 of_scan_flat_dt(htab_dt_scan_pftsize, NULL);
Paul Mackerras3eac8c62005-10-12 16:58:53 +1000439 if (ppc64_pft_size)
440 return 1UL << ppc64_pft_size;
441
442 /* round mem_size up to next power of 2 */
Paul Mackerras799d6042005-11-10 13:37:51 +1100443 mem_size = lmb_phys_mem_size();
444 rnd_mem_size = 1UL << __ilog2(mem_size);
445 if (rnd_mem_size < mem_size)
Paul Mackerras3eac8c62005-10-12 16:58:53 +1000446 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
Mike Kravetz54b79242005-11-07 16:25:48 -0800454#ifdef CONFIG_MEMORY_HOTPLUG
455void create_section_mapping(unsigned long start, unsigned long end)
456{
Michael Ellermancaf80e52006-03-21 20:45:51 +1100457 BUG_ON(htab_bolt_mapping(start, end, __pa(start),
Mike Kravetz54b79242005-11-07 16:25:48 -0800458 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_COHERENT | PP_RWXX,
Paul Mackerras1189be62007-10-11 20:37:10 +1000459 mmu_linear_psize, mmu_kernel_ssize));
Mike Kravetz54b79242005-11-07 16:25:48 -0800460}
Badari Pulavartyf8c88032008-01-29 09:19:24 +1100461
Badari Pulavarty52db9b42008-03-28 11:37:21 +1100462int remove_section_mapping(unsigned long start, unsigned long end)
Badari Pulavartyf8c88032008-01-29 09:19:24 +1100463{
Badari Pulavarty52db9b42008-03-28 11:37:21 +1100464 return htab_remove_mapping(start, end, mmu_linear_psize,
465 mmu_kernel_ssize);
Badari Pulavartyf8c88032008-01-29 09:19:24 +1100466}
Mike Kravetz54b79242005-11-07 16:25:48 -0800467#endif /* CONFIG_MEMORY_HOTPLUG */
468
Michael Ellerman7d0daae2006-06-23 18:16:38 +1000469static 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
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +1000486#ifdef CONFIG_PPC_HAS_HASH_64K
Michael Ellerman7d0daae2006-06-23 18:16:38 +1000487 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);
Jon Tollefson5b825832007-05-17 04:43:02 +1000496#endif /* CONFIG_PPC_HAS_HASH_64K */
Michael Ellerman7d0daae2006-06-23 18:16:38 +1000497
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504void __init htab_initialize(void)
505{
Michael Ellerman337a7122006-02-21 17:22:55 +1100506 unsigned long table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 unsigned long pteg_count;
508 unsigned long mode_rw;
Michael Ellerman41d824b2008-01-30 01:13:59 +1100509 unsigned long base = 0, size = 0, limit;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100510 int i;
511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 extern unsigned long tce_alloc_start, tce_alloc_end;
513
514 DBG(" -> htab_initialize()\n");
515
Paul Mackerras1189be62007-10-11 20:37:10 +1000516 /* Initialize segment sizes */
517 htab_init_seg_sizes();
518
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100519 /* Initialize page sizes */
520 htab_init_page_sizes();
521
Paul Mackerras1189be62007-10-11 20:37:10 +1000522 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 /*
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 */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100532 htab_size_bytes = htab_get_table_size();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 pteg_count = htab_size_bytes >> 7;
534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 htab_hash_mask = pteg_count - 1;
536
Michael Ellerman57cfb812006-03-21 20:45:59 +1100537 if (firmware_has_feature(FW_FEATURE_LPAR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 /* 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
Michael Ellerman41d824b2008-01-30 01:13:59 +1100543 * the absolute address space. On cell we want it to be
Michael Ellerman31bf1112008-03-12 18:03:24 +1100544 * in the first 2 Gig so we can use it for IOMMU hacks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 */
Michael Ellerman41d824b2008-01-30 01:13:59 +1100546 if (machine_is(cell))
Michael Ellerman31bf1112008-03-12 18:03:24 +1100547 limit = 0x80000000;
Michael Ellerman41d824b2008-01-30 01:13:59 +1100548 else
549 limit = 0;
550
551 table = lmb_alloc_base(htab_size_bytes, htab_size_bytes, limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
553 DBG("Hash table allocated at %lx, size: %lx\n", table,
554 htab_size_bytes);
555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 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);
Paul Mackerras799d6042005-11-10 13:37:51 +1100563
564 /* Set SDR1 */
565 mtspr(SPRN_SDR1, _SDR1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 }
567
Anton Blanchard515bae92005-06-21 17:15:55 -0700568 mode_rw = _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_COHERENT | PP_RWXX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000570#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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 /* 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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 /* create bolted the linear mapping in the hash table */
583 for (i=0; i < lmb.memory.cnt; i++) {
Michael Ellermanb5666f72005-12-05 10:24:33 -0600584 base = (unsigned long)__va(lmb.memory.region[i].base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 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
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100591 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 */
597 DBG("DART base: %lx\n", dart_tablebase);
598
599 if (dart_tablebase != 0 && dart_tablebase >= base
600 && dart_tablebase < (base + size)) {
Michael Ellermancaf80e52006-03-21 20:45:51 +1100601 unsigned long dart_table_end = dart_tablebase + 16 * MB;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 if (base != dart_tablebase)
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100603 BUG_ON(htab_bolt_mapping(base, dart_tablebase,
Michael Ellermancaf80e52006-03-21 20:45:51 +1100604 __pa(base), mode_rw,
Paul Mackerras1189be62007-10-11 20:37:10 +1000605 mmu_linear_psize,
606 mmu_kernel_ssize));
Michael Ellermancaf80e52006-03-21 20:45:51 +1100607 if ((base + size) > dart_table_end)
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100608 BUG_ON(htab_bolt_mapping(dart_tablebase+16*MB,
Michael Ellermancaf80e52006-03-21 20:45:51 +1100609 base + size,
610 __pa(dart_table_end),
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100611 mode_rw,
Paul Mackerras1189be62007-10-11 20:37:10 +1000612 mmu_linear_psize,
613 mmu_kernel_ssize));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 continue;
615 }
616#endif /* CONFIG_U3_DART */
Michael Ellermancaf80e52006-03-21 20:45:51 +1100617 BUG_ON(htab_bolt_mapping(base, base + size, __pa(base),
Paul Mackerras1189be62007-10-11 20:37:10 +1000618 mode_rw, mmu_linear_psize, mmu_kernel_ssize));
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100619 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
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) {
Michael Ellermanb5666f72005-12-05 10:24:33 -0600629 tce_alloc_start = (unsigned long)__va(tce_alloc_start);
630 tce_alloc_end = (unsigned long)__va(tce_alloc_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
632 if (base + size >= tce_alloc_start)
633 tce_alloc_start = base + size + 1;
634
Michael Ellermancaf80e52006-03-21 20:45:51 +1100635 BUG_ON(htab_bolt_mapping(tce_alloc_start, tce_alloc_end,
636 __pa(tce_alloc_start), mode_rw,
Paul Mackerras1189be62007-10-11 20:37:10 +1000637 mmu_linear_psize, mmu_kernel_ssize));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 }
639
Michael Ellerman7d0daae2006-06-23 18:16:38 +1000640 htab_finish_init();
641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 DBG(" <- htab_initialize()\n");
643}
644#undef KB
645#undef MB
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Anton Blancharde597cb32005-12-29 10:46:29 +1100647void htab_initialize_secondary(void)
Paul Mackerras799d6042005-11-10 13:37:51 +1100648{
Michael Ellerman57cfb812006-03-21 20:45:59 +1100649 if (!firmware_has_feature(FW_FEATURE_LPAR))
Paul Mackerras799d6042005-11-10 13:37:51 +1100650 mtspr(SPRN_SDR1, _SDR1);
651}
652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653/*
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
Benjamin Herrenschmidt76c8e252005-11-08 11:21:05 +1100660 if (!pfn_valid(pte_pfn(pte)))
661 return pp;
662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 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
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100671 pp |= HPTE_R_N;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 }
673 return pp;
674}
675
Paul Mackerras721151d2007-04-03 21:24:02 +1000676/*
677 * Demote a segment to using 4k pages.
678 * For now this makes the whole process use 4k pages.
679 */
Paul Mackerras721151d2007-04-03 21:24:02 +1000680#ifdef CONFIG_PPC_64K_PAGES
Paul Mackerrasfa282372008-01-24 08:35:13 +1100681void demote_segment_4k(struct mm_struct *mm, unsigned long addr)
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +1000682{
Paul Mackerras721151d2007-04-03 21:24:02 +1000683 if (mm->context.user_psize == MMU_PAGE_4K)
684 return;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000685 slice_set_user_psize(mm, MMU_PAGE_4K);
Geert Uytterhoeven1e57ba82007-07-17 02:35:38 +1000686#ifdef CONFIG_SPU_BASE
Paul Mackerras721151d2007-04-03 21:24:02 +1000687 spu_flush_all_slbs(mm);
688#endif
Paul Mackerrasfa282372008-01-24 08:35:13 +1100689 if (get_paca()->context.user_psize != MMU_PAGE_4K) {
690 get_paca()->context = mm->context;
691 slb_flush_and_rebolt();
692 }
Paul Mackerras721151d2007-04-03 21:24:02 +1000693}
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +1000694#endif /* CONFIG_PPC_64K_PAGES */
Paul Mackerras721151d2007-04-03 21:24:02 +1000695
Paul Mackerrasfa282372008-01-24 08:35:13 +1100696#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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740/* Result code is:
741 * 0 - handled
742 * 1 - normal page fault
743 * -1 - critical hash insertion error
Paul Mackerrasfa282372008-01-24 08:35:13 +1100744 * -2 - access not permitted by subpage protection mechanism
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 */
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 cpumask_t tmp;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100753 int rc, user_region = 0, local = 0;
Paul Mackerras1189be62007-10-11 20:37:10 +1000754 int psize, ssize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100756 DBG_LOW("hash_page(ea=%016lx, access=%lx, trap=%lx\n",
757 ea, access, trap);
David Gibson1f8d4192005-05-05 16:15:13 -0700758
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100759 if ((ea & ~REGION_MASK) >= PGTABLE_RANGE) {
760 DBG_LOW(" out of pgtable range !\n");
761 return 1;
762 }
763
764 /* Get region & vsid */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 switch (REGION_ID(ea)) {
766 case USER_REGION_ID:
767 user_region = 1;
768 mm = current->mm;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100769 if (! mm) {
770 DBG_LOW(" user region with no mm !\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 return 1;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100772 }
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +1000773#ifdef CONFIG_PPC_MM_SLICES
774 psize = get_slice_psize(mm, ea);
775#else
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000776 psize = mm->context.user_psize;
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +1000777#endif
Paul Mackerras1189be62007-10-11 20:37:10 +1000778 ssize = user_segment_size(ea);
779 vsid = get_vsid(mm->context.id, ea, ssize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 case VMALLOC_REGION_ID:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 mm = &init_mm;
Paul Mackerras1189be62007-10-11 20:37:10 +1000783 vsid = get_kernel_vsid(ea, mmu_kernel_ssize);
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000784 if (ea < VMALLOC_END)
785 psize = mmu_vmalloc_psize;
786 else
787 psize = mmu_io_psize;
Paul Mackerras1189be62007-10-11 20:37:10 +1000788 ssize = mmu_kernel_ssize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 default:
791 /* Not a valid range
792 * Send the problem up to do_page_fault
793 */
794 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 }
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100796 DBG_LOW(" mm=%p, mm->pgdir=%p, vsid=%016lx\n", mm, mm->pgd, vsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100798 /* Get pgdir */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 pgdir = mm->pgd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 if (pgdir == NULL)
801 return 1;
802
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100803 /* Check CPU locality */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 tmp = cpumask_of_cpu(smp_processor_id());
805 if (user_region && cpus_equal(mm->cpu_vm_mask, tmp))
806 local = 1;
807
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000808#ifdef CONFIG_HUGETLB_PAGE
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100809 /* Handle hugepage regions */
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +1000810 if (HPAGE_SHIFT && psize == mmu_huge_psize) {
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100811 DBG_LOW(" -> huge page !\n");
David Gibsoncbf52af2005-12-09 14:20:52 +1100812 return hash_huge_page(mm, access, ea, vsid, local, trap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 }
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000814#endif /* CONFIG_HUGETLB_PAGE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +1000816#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
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100825 /* 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;
844 }
845
846 /* Do actual hashing */
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +1000847#ifdef CONFIG_PPC_64K_PAGES
Paul Mackerras721151d2007-04-03 21:24:02 +1000848 /* 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
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +1000854 /* 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;
Geert Uytterhoeven1e57ba82007-07-17 02:35:38 +1000872#ifdef CONFIG_SPU_BASE
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +0100873 spu_flush_all_slbs(mm);
874#endif
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000875 }
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +1000876 }
877 if (user_region) {
878 if (psize != get_paca()->context.user_psize) {
Benjamin Herrenschmidtf6ab0b92007-10-29 12:05:18 +1100879 get_paca()->context = mm->context;
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000880 slb_flush_and_rebolt();
881 }
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +1000882 } 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;
Michael Neuling67439b72007-08-03 11:55:39 +1000886 slb_vmalloc_update();
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000887 }
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +1000888#endif /* CONFIG_PPC_64K_PAGES */
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +1000889
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +1000890#ifdef CONFIG_PPC_HAS_HASH_64K
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000891 if (psize == MMU_PAGE_64K)
Paul Mackerras1189be62007-10-11 20:37:10 +1000892 rc = __hash_page_64K(ea, access, vsid, ptep, trap, local, ssize);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100893 else
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +1000894#endif /* CONFIG_PPC_HAS_HASH_64K */
Paul Mackerrasfa282372008-01-24 08:35:13 +1100895 {
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 }
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100903
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912}
Arnd Bergmann67207b92005-11-15 15:53:48 -0500913EXPORT_SYMBOL_GPL(hash_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100915void hash_preload(struct mm_struct *mm, unsigned long ea,
916 unsigned long access, unsigned long trap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100918 unsigned long vsid;
919 void *pgdir;
920 pte_t *ptep;
921 cpumask_t mask;
922 unsigned long flags;
923 int local = 0;
Paul Mackerras1189be62007-10-11 20:37:10 +1000924 int ssize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000926 BUG_ON(REGION_ID(ea) != USER_REGION_ID);
927
928#ifdef CONFIG_PPC_MM_SLICES
929 /* We only prefault standard pages for now */
Ilpo Järvinen2b02d132007-08-16 08:03:35 +1000930 if (unlikely(get_slice_psize(mm, ea) != mm->context.user_psize))
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100931 return;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000932#endif
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100933
934 DBG_LOW("hash_preload(mm=%p, mm->pgdir=%p, ea=%016lx, access=%lx,"
935 " trap=%lx\n", mm, mm->pgd, ea, access, trap);
936
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +1000937 /* Get Linux PTE if available */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100938 pgdir = mm->pgd;
939 if (pgdir == NULL)
940 return;
941 ptep = find_linux_pte(pgdir, ea);
942 if (!ptep)
943 return;
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +1000944
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 */
Paul Mackerras1189be62007-10-11 20:37:10 +1000957 ssize = user_segment_size(ea);
958 vsid = get_vsid(mm->context.id, ea, ssize);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100959
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +1000960 /* Hash doesn't like irqs */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100961 local_irq_save(flags);
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +1000962
963 /* Is that local to this CPU ? */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100964 mask = cpumask_of_cpu(smp_processor_id());
965 if (cpus_equal(mm->cpu_vm_mask, mask))
966 local = 1;
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +1000967
968 /* Hash it in */
969#ifdef CONFIG_PPC_HAS_HASH_64K
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000970 if (mm->context.user_psize == MMU_PAGE_64K)
Paul Mackerras1189be62007-10-11 20:37:10 +1000971 __hash_page_64K(ea, access, vsid, ptep, trap, local, ssize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 else
Jon Tollefson5b825832007-05-17 04:43:02 +1000973#endif /* CONFIG_PPC_HAS_HASH_64K */
Paul Mackerrasfa282372008-01-24 08:35:13 +1100974 __hash_page_4K(ea, access, vsid, ptep, trap, local, ssize,
975 subpage_protection(pgdir, ea));
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +1000976
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100977 local_irq_restore(flags);
978}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
Benjamin Herrenschmidtf6ab0b92007-10-29 12:05:18 +1100980/* 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 */
Paul Mackerras1189be62007-10-11 20:37:10 +1000983void flush_hash_page(unsigned long va, real_pte_t pte, int psize, int ssize,
984 int local)
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100985{
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) {
Paul Mackerras1189be62007-10-11 20:37:10 +1000990 hash = hpt_hash(va, shift, ssize);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100991 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);
Paul Mackerras1189be62007-10-11 20:37:10 +1000997 ppc_md.hpte_invalidate(slot, va, psize, ssize, local);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100998 } pte_iterate_hashed_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999}
1000
Benjamin Herrenschmidt61b1a942005-09-20 13:52:50 +10001001void flush_hash_range(unsigned long number, int local)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002{
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001003 if (ppc_md.flush_hash_range)
Benjamin Herrenschmidt61b1a942005-09-20 13:52:50 +10001004 ppc_md.flush_hash_range(number, local);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001005 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 int i;
Benjamin Herrenschmidt61b1a942005-09-20 13:52:50 +10001007 struct ppc64_tlb_batch *batch =
1008 &__get_cpu_var(ppc64_tlb_batch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
1010 for (i = 0; i < number; i++)
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001011 flush_hash_page(batch->vaddr[i], batch->pte[i],
Paul Mackerras1189be62007-10-11 20:37:10 +10001012 batch->psize, batch->ssize, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 }
1014}
1015
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016/*
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 */
Paul Mackerrasfa282372008-01-24 08:35:13 +11001020void low_hash_fault(struct pt_regs *regs, unsigned long address, int rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021{
1022 if (user_mode(regs)) {
Paul Mackerrasfa282372008-01-24 08:35:13 +11001023#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);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031}
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +10001032
1033#ifdef CONFIG_DEBUG_PAGEALLOC
1034static void kernel_map_linear_page(unsigned long vaddr, unsigned long lmi)
1035{
Paul Mackerras1189be62007-10-11 20:37:10 +10001036 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);
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +10001039 unsigned long mode = _PAGE_ACCESSED | _PAGE_DIRTY |
1040 _PAGE_COHERENT | PP_RWXX | HPTE_R_N;
1041 int ret;
1042
Paul Mackerras1189be62007-10-11 20:37:10 +10001043 hash = hpt_hash(va, PAGE_SHIFT, mmu_kernel_ssize);
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +10001044 hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
1045
1046 ret = ppc_md.hpte_insert(hpteg, va, __pa(vaddr),
Paul Mackerras1189be62007-10-11 20:37:10 +10001047 mode, HPTE_V_BOLTED,
1048 mmu_linear_psize, mmu_kernel_ssize);
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +10001049 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{
Paul Mackerras1189be62007-10-11 20:37:10 +10001058 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);
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +10001061
Paul Mackerras1189be62007-10-11 20:37:10 +10001062 hash = hpt_hash(va, PAGE_SHIFT, mmu_kernel_ssize);
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +10001063 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;
Paul Mackerras1189be62007-10-11 20:37:10 +10001072 ppc_md.hpte_invalidate(slot, va, mmu_linear_psize, mmu_kernel_ssize, 0);
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +10001073}
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 */