blob: 4cc53f92377e7d65eef6c46c3bdf3f26a4d0c018 [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 Pulavartyf8c88032008-01-29 09:19:24 +1100195static void htab_remove_mapping(unsigned long vstart, unsigned long vend,
196 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) {
205 printk("Sub-arch doesn't implement hpte_removebolted\n");
206 return;
207 }
208
209 for (vaddr = vstart; vaddr < vend; vaddr += step)
210 ppc_md.hpte_removebolted(vaddr, psize, ssize);
211}
212
Paul Mackerras1189be62007-10-11 20:37:10 +1000213static int __init htab_dt_scan_seg_sizes(unsigned long node,
214 const char *uname, int depth,
215 void *data)
216{
217 char *type = of_get_flat_dt_prop(node, "device_type", NULL);
218 u32 *prop;
219 unsigned long size = 0;
220
221 /* We are scanning "cpu" nodes only */
222 if (type == NULL || strcmp(type, "cpu") != 0)
223 return 0;
224
225 prop = (u32 *)of_get_flat_dt_prop(node, "ibm,processor-segment-sizes",
226 &size);
227 if (prop == NULL)
228 return 0;
229 for (; size >= 4; size -= 4, ++prop) {
230 if (prop[0] == 40) {
231 DBG("1T segment support detected\n");
232 cur_cpu_spec->cpu_features |= CPU_FTR_1T_SEGMENT;
Olof Johanssonf5534002007-10-12 16:44:55 +1000233 return 1;
Paul Mackerras1189be62007-10-11 20:37:10 +1000234 }
Paul Mackerras1189be62007-10-11 20:37:10 +1000235 }
Olof Johanssonf66bce52007-10-16 00:58:59 +1000236 cur_cpu_spec->cpu_features &= ~CPU_FTR_NO_SLBIE_B;
Paul Mackerras1189be62007-10-11 20:37:10 +1000237 return 0;
238}
239
240static void __init htab_init_seg_sizes(void)
241{
242 of_scan_flat_dt(htab_dt_scan_seg_sizes, NULL);
243}
244
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100245static int __init htab_dt_scan_page_sizes(unsigned long node,
246 const char *uname, int depth,
247 void *data)
248{
249 char *type = of_get_flat_dt_prop(node, "device_type", NULL);
250 u32 *prop;
251 unsigned long size = 0;
252
253 /* We are scanning "cpu" nodes only */
254 if (type == NULL || strcmp(type, "cpu") != 0)
255 return 0;
256
257 prop = (u32 *)of_get_flat_dt_prop(node,
258 "ibm,segment-page-sizes", &size);
259 if (prop != NULL) {
260 DBG("Page sizes from device-tree:\n");
261 size /= 4;
262 cur_cpu_spec->cpu_features &= ~(CPU_FTR_16M_PAGE);
263 while(size > 0) {
264 unsigned int shift = prop[0];
265 unsigned int slbenc = prop[1];
266 unsigned int lpnum = prop[2];
267 unsigned int lpenc = 0;
268 struct mmu_psize_def *def;
269 int idx = -1;
270
271 size -= 3; prop += 3;
272 while(size > 0 && lpnum) {
273 if (prop[0] == shift)
274 lpenc = prop[1];
275 prop += 2; size -= 2;
276 lpnum--;
277 }
278 switch(shift) {
279 case 0xc:
280 idx = MMU_PAGE_4K;
281 break;
282 case 0x10:
283 idx = MMU_PAGE_64K;
284 break;
285 case 0x14:
286 idx = MMU_PAGE_1M;
287 break;
288 case 0x18:
289 idx = MMU_PAGE_16M;
290 cur_cpu_spec->cpu_features |= CPU_FTR_16M_PAGE;
291 break;
292 case 0x22:
293 idx = MMU_PAGE_16G;
294 break;
295 }
296 if (idx < 0)
297 continue;
298 def = &mmu_psize_defs[idx];
299 def->shift = shift;
300 if (shift <= 23)
301 def->avpnm = 0;
302 else
303 def->avpnm = (1 << (shift - 23)) - 1;
304 def->sllp = slbenc;
305 def->penc = lpenc;
306 /* We don't know for sure what's up with tlbiel, so
307 * for now we only set it for 4K and 64K pages
308 */
309 if (idx == MMU_PAGE_4K || idx == MMU_PAGE_64K)
310 def->tlbiel = 1;
311 else
312 def->tlbiel = 0;
313
314 DBG(" %d: shift=%02x, sllp=%04x, avpnm=%08x, "
315 "tlbiel=%d, penc=%d\n",
316 idx, shift, def->sllp, def->avpnm, def->tlbiel,
317 def->penc);
318 }
319 return 1;
320 }
321 return 0;
322}
323
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100324static void __init htab_init_page_sizes(void)
325{
326 int rc;
327
328 /* Default to 4K pages only */
329 memcpy(mmu_psize_defs, mmu_psize_defaults_old,
330 sizeof(mmu_psize_defaults_old));
331
332 /*
333 * Try to find the available page sizes in the device-tree
334 */
335 rc = of_scan_flat_dt(htab_dt_scan_page_sizes, NULL);
336 if (rc != 0) /* Found */
337 goto found;
338
339 /*
340 * Not in the device-tree, let's fallback on known size
341 * list for 16M capable GP & GR
342 */
Stephen Rothwell04704662006-11-30 11:46:22 +1100343 if (cpu_has_feature(CPU_FTR_16M_PAGE))
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100344 memcpy(mmu_psize_defs, mmu_psize_defaults_gp,
345 sizeof(mmu_psize_defaults_gp));
346 found:
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000347#ifndef CONFIG_DEBUG_PAGEALLOC
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100348 /*
349 * Pick a size for the linear mapping. Currently, we only support
350 * 16M, 1M and 4K which is the default
351 */
352 if (mmu_psize_defs[MMU_PAGE_16M].shift)
353 mmu_linear_psize = MMU_PAGE_16M;
354 else if (mmu_psize_defs[MMU_PAGE_1M].shift)
355 mmu_linear_psize = MMU_PAGE_1M;
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000356#endif /* CONFIG_DEBUG_PAGEALLOC */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100357
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000358#ifdef CONFIG_PPC_64K_PAGES
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100359 /*
360 * Pick a size for the ordinary pages. Default is 4K, we support
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000361 * 64K for user mappings and vmalloc if supported by the processor.
362 * We only use 64k for ioremap if the processor
363 * (and firmware) support cache-inhibited large pages.
364 * If not, we use 4k and set mmu_ci_restrictions so that
365 * hash_page knows to switch processes that use cache-inhibited
366 * mappings to 4k pages.
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100367 */
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000368 if (mmu_psize_defs[MMU_PAGE_64K].shift) {
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100369 mmu_virtual_psize = MMU_PAGE_64K;
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000370 mmu_vmalloc_psize = MMU_PAGE_64K;
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000371 if (mmu_linear_psize == MMU_PAGE_4K)
372 mmu_linear_psize = MMU_PAGE_64K;
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000373 if (cpu_has_feature(CPU_FTR_CI_LARGE_PAGE))
374 mmu_io_psize = MMU_PAGE_64K;
375 else
376 mmu_ci_restrictions = 1;
377 }
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000378#endif /* CONFIG_PPC_64K_PAGES */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100379
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000380 printk(KERN_DEBUG "Page orders: linear mapping = %d, "
381 "virtual = %d, io = %d\n",
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100382 mmu_psize_defs[mmu_linear_psize].shift,
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000383 mmu_psize_defs[mmu_virtual_psize].shift,
384 mmu_psize_defs[mmu_io_psize].shift);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100385
386#ifdef CONFIG_HUGETLB_PAGE
387 /* Init large page size. Currently, we pick 16M or 1M depending
388 * on what is available
389 */
390 if (mmu_psize_defs[MMU_PAGE_16M].shift)
Jon Tollefson4ec161c2008-01-04 09:59:50 +1100391 set_huge_psize(MMU_PAGE_16M);
David Gibson7d24f0b2005-11-07 00:57:52 -0800392 /* With 4k/4level pagetables, we can't (for now) cope with a
393 * huge page size < PMD_SIZE */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100394 else if (mmu_psize_defs[MMU_PAGE_1M].shift)
Jon Tollefson4ec161c2008-01-04 09:59:50 +1100395 set_huge_psize(MMU_PAGE_1M);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100396#endif /* CONFIG_HUGETLB_PAGE */
397}
398
399static int __init htab_dt_scan_pftsize(unsigned long node,
400 const char *uname, int depth,
401 void *data)
402{
403 char *type = of_get_flat_dt_prop(node, "device_type", NULL);
404 u32 *prop;
405
406 /* We are scanning "cpu" nodes only */
407 if (type == NULL || strcmp(type, "cpu") != 0)
408 return 0;
409
410 prop = (u32 *)of_get_flat_dt_prop(node, "ibm,pft-size", NULL);
411 if (prop != NULL) {
412 /* pft_size[0] is the NUMA CEC cookie */
413 ppc64_pft_size = prop[1];
414 return 1;
415 }
416 return 0;
417}
418
419static unsigned long __init htab_get_table_size(void)
Paul Mackerras3eac8c62005-10-12 16:58:53 +1000420{
Paul Mackerras799d6042005-11-10 13:37:51 +1100421 unsigned long mem_size, rnd_mem_size, pteg_count;
Paul Mackerras3eac8c62005-10-12 16:58:53 +1000422
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100423 /* If hash size isn't already provided by the platform, we try to
Adrian Bunk943ffb52006-01-10 00:10:13 +0100424 * retrieve it from the device-tree. If it's not there neither, we
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100425 * calculate it now based on the total RAM size
Paul Mackerras3eac8c62005-10-12 16:58:53 +1000426 */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100427 if (ppc64_pft_size == 0)
428 of_scan_flat_dt(htab_dt_scan_pftsize, NULL);
Paul Mackerras3eac8c62005-10-12 16:58:53 +1000429 if (ppc64_pft_size)
430 return 1UL << ppc64_pft_size;
431
432 /* round mem_size up to next power of 2 */
Paul Mackerras799d6042005-11-10 13:37:51 +1100433 mem_size = lmb_phys_mem_size();
434 rnd_mem_size = 1UL << __ilog2(mem_size);
435 if (rnd_mem_size < mem_size)
Paul Mackerras3eac8c62005-10-12 16:58:53 +1000436 rnd_mem_size <<= 1;
437
438 /* # pages / 2 */
439 pteg_count = max(rnd_mem_size >> (12 + 1), 1UL << 11);
440
441 return pteg_count << 7;
442}
443
Mike Kravetz54b79242005-11-07 16:25:48 -0800444#ifdef CONFIG_MEMORY_HOTPLUG
445void create_section_mapping(unsigned long start, unsigned long end)
446{
Michael Ellermancaf80e52006-03-21 20:45:51 +1100447 BUG_ON(htab_bolt_mapping(start, end, __pa(start),
Mike Kravetz54b79242005-11-07 16:25:48 -0800448 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_COHERENT | PP_RWXX,
Paul Mackerras1189be62007-10-11 20:37:10 +1000449 mmu_linear_psize, mmu_kernel_ssize));
Mike Kravetz54b79242005-11-07 16:25:48 -0800450}
Badari Pulavartyf8c88032008-01-29 09:19:24 +1100451
452void remove_section_mapping(unsigned long start, unsigned long end)
453{
454 htab_remove_mapping(start, end, mmu_linear_psize, mmu_kernel_ssize);
455}
Mike Kravetz54b79242005-11-07 16:25:48 -0800456#endif /* CONFIG_MEMORY_HOTPLUG */
457
Michael Ellerman7d0daae2006-06-23 18:16:38 +1000458static inline void make_bl(unsigned int *insn_addr, void *func)
459{
460 unsigned long funcp = *((unsigned long *)func);
461 int offset = funcp - (unsigned long)insn_addr;
462
463 *insn_addr = (unsigned int)(0x48000001 | (offset & 0x03fffffc));
464 flush_icache_range((unsigned long)insn_addr, 4+
465 (unsigned long)insn_addr);
466}
467
468static void __init htab_finish_init(void)
469{
470 extern unsigned int *htab_call_hpte_insert1;
471 extern unsigned int *htab_call_hpte_insert2;
472 extern unsigned int *htab_call_hpte_remove;
473 extern unsigned int *htab_call_hpte_updatepp;
474
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +1000475#ifdef CONFIG_PPC_HAS_HASH_64K
Michael Ellerman7d0daae2006-06-23 18:16:38 +1000476 extern unsigned int *ht64_call_hpte_insert1;
477 extern unsigned int *ht64_call_hpte_insert2;
478 extern unsigned int *ht64_call_hpte_remove;
479 extern unsigned int *ht64_call_hpte_updatepp;
480
481 make_bl(ht64_call_hpte_insert1, ppc_md.hpte_insert);
482 make_bl(ht64_call_hpte_insert2, ppc_md.hpte_insert);
483 make_bl(ht64_call_hpte_remove, ppc_md.hpte_remove);
484 make_bl(ht64_call_hpte_updatepp, ppc_md.hpte_updatepp);
Jon Tollefson5b825832007-05-17 04:43:02 +1000485#endif /* CONFIG_PPC_HAS_HASH_64K */
Michael Ellerman7d0daae2006-06-23 18:16:38 +1000486
487 make_bl(htab_call_hpte_insert1, ppc_md.hpte_insert);
488 make_bl(htab_call_hpte_insert2, ppc_md.hpte_insert);
489 make_bl(htab_call_hpte_remove, ppc_md.hpte_remove);
490 make_bl(htab_call_hpte_updatepp, ppc_md.hpte_updatepp);
491}
492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493void __init htab_initialize(void)
494{
Michael Ellerman337a7122006-02-21 17:22:55 +1100495 unsigned long table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 unsigned long pteg_count;
497 unsigned long mode_rw;
Michael Ellerman41d824b2008-01-30 01:13:59 +1100498 unsigned long base = 0, size = 0, limit;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100499 int i;
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 extern unsigned long tce_alloc_start, tce_alloc_end;
502
503 DBG(" -> htab_initialize()\n");
504
Paul Mackerras1189be62007-10-11 20:37:10 +1000505 /* Initialize segment sizes */
506 htab_init_seg_sizes();
507
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100508 /* Initialize page sizes */
509 htab_init_page_sizes();
510
Paul Mackerras1189be62007-10-11 20:37:10 +1000511 if (cpu_has_feature(CPU_FTR_1T_SEGMENT)) {
512 mmu_kernel_ssize = MMU_SEGSIZE_1T;
513 mmu_highuser_ssize = MMU_SEGSIZE_1T;
514 printk(KERN_INFO "Using 1TB segments\n");
515 }
516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 /*
518 * Calculate the required size of the htab. We want the number of
519 * PTEGs to equal one half the number of real pages.
520 */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100521 htab_size_bytes = htab_get_table_size();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 pteg_count = htab_size_bytes >> 7;
523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 htab_hash_mask = pteg_count - 1;
525
Michael Ellerman57cfb812006-03-21 20:45:59 +1100526 if (firmware_has_feature(FW_FEATURE_LPAR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 /* Using a hypervisor which owns the htab */
528 htab_address = NULL;
529 _SDR1 = 0;
530 } else {
531 /* Find storage for the HPT. Must be contiguous in
Michael Ellerman41d824b2008-01-30 01:13:59 +1100532 * the absolute address space. On cell we want it to be
533 * in the first 1 Gig.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 */
Michael Ellerman41d824b2008-01-30 01:13:59 +1100535 if (machine_is(cell))
536 limit = 0x40000000;
537 else
538 limit = 0;
539
540 table = lmb_alloc_base(htab_size_bytes, htab_size_bytes, limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542 DBG("Hash table allocated at %lx, size: %lx\n", table,
543 htab_size_bytes);
544
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 htab_address = abs_to_virt(table);
546
547 /* htab absolute addr + encoded htabsize */
548 _SDR1 = table + __ilog2(pteg_count) - 11;
549
550 /* Initialize the HPT with no entries */
551 memset((void *)table, 0, htab_size_bytes);
Paul Mackerras799d6042005-11-10 13:37:51 +1100552
553 /* Set SDR1 */
554 mtspr(SPRN_SDR1, _SDR1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 }
556
Anton Blanchard515bae92005-06-21 17:15:55 -0700557 mode_rw = _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_COHERENT | PP_RWXX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000559#ifdef CONFIG_DEBUG_PAGEALLOC
560 linear_map_hash_count = lmb_end_of_DRAM() >> PAGE_SHIFT;
561 linear_map_hash_slots = __va(lmb_alloc_base(linear_map_hash_count,
562 1, lmb.rmo_size));
563 memset(linear_map_hash_slots, 0, linear_map_hash_count);
564#endif /* CONFIG_DEBUG_PAGEALLOC */
565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 /* On U3 based machines, we need to reserve the DART area and
567 * _NOT_ map it to avoid cache paradoxes as it's remapped non
568 * cacheable later on
569 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
571 /* create bolted the linear mapping in the hash table */
572 for (i=0; i < lmb.memory.cnt; i++) {
Michael Ellermanb5666f72005-12-05 10:24:33 -0600573 base = (unsigned long)__va(lmb.memory.region[i].base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 size = lmb.memory.region[i].size;
575
576 DBG("creating mapping for region: %lx : %lx\n", base, size);
577
578#ifdef CONFIG_U3_DART
579 /* Do not map the DART space. Fortunately, it will be aligned
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100580 * in such a way that it will not cross two lmb regions and
581 * will fit within a single 16Mb page.
582 * The DART space is assumed to be a full 16Mb region even if
583 * we only use 2Mb of that space. We will use more of it later
584 * for AGP GART. We have to use a full 16Mb large page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 */
586 DBG("DART base: %lx\n", dart_tablebase);
587
588 if (dart_tablebase != 0 && dart_tablebase >= base
589 && dart_tablebase < (base + size)) {
Michael Ellermancaf80e52006-03-21 20:45:51 +1100590 unsigned long dart_table_end = dart_tablebase + 16 * MB;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 if (base != dart_tablebase)
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100592 BUG_ON(htab_bolt_mapping(base, dart_tablebase,
Michael Ellermancaf80e52006-03-21 20:45:51 +1100593 __pa(base), mode_rw,
Paul Mackerras1189be62007-10-11 20:37:10 +1000594 mmu_linear_psize,
595 mmu_kernel_ssize));
Michael Ellermancaf80e52006-03-21 20:45:51 +1100596 if ((base + size) > dart_table_end)
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100597 BUG_ON(htab_bolt_mapping(dart_tablebase+16*MB,
Michael Ellermancaf80e52006-03-21 20:45:51 +1100598 base + size,
599 __pa(dart_table_end),
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100600 mode_rw,
Paul Mackerras1189be62007-10-11 20:37:10 +1000601 mmu_linear_psize,
602 mmu_kernel_ssize));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 continue;
604 }
605#endif /* CONFIG_U3_DART */
Michael Ellermancaf80e52006-03-21 20:45:51 +1100606 BUG_ON(htab_bolt_mapping(base, base + size, __pa(base),
Paul Mackerras1189be62007-10-11 20:37:10 +1000607 mode_rw, mmu_linear_psize, mmu_kernel_ssize));
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100608 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610 /*
611 * If we have a memory_limit and we've allocated TCEs then we need to
612 * explicitly map the TCE area at the top of RAM. We also cope with the
613 * case that the TCEs start below memory_limit.
614 * tce_alloc_start/end are 16MB aligned so the mapping should work
615 * for either 4K or 16MB pages.
616 */
617 if (tce_alloc_start) {
Michael Ellermanb5666f72005-12-05 10:24:33 -0600618 tce_alloc_start = (unsigned long)__va(tce_alloc_start);
619 tce_alloc_end = (unsigned long)__va(tce_alloc_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621 if (base + size >= tce_alloc_start)
622 tce_alloc_start = base + size + 1;
623
Michael Ellermancaf80e52006-03-21 20:45:51 +1100624 BUG_ON(htab_bolt_mapping(tce_alloc_start, tce_alloc_end,
625 __pa(tce_alloc_start), mode_rw,
Paul Mackerras1189be62007-10-11 20:37:10 +1000626 mmu_linear_psize, mmu_kernel_ssize));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 }
628
Michael Ellerman7d0daae2006-06-23 18:16:38 +1000629 htab_finish_init();
630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 DBG(" <- htab_initialize()\n");
632}
633#undef KB
634#undef MB
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Anton Blancharde597cb32005-12-29 10:46:29 +1100636void htab_initialize_secondary(void)
Paul Mackerras799d6042005-11-10 13:37:51 +1100637{
Michael Ellerman57cfb812006-03-21 20:45:59 +1100638 if (!firmware_has_feature(FW_FEATURE_LPAR))
Paul Mackerras799d6042005-11-10 13:37:51 +1100639 mtspr(SPRN_SDR1, _SDR1);
640}
641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642/*
643 * Called by asm hashtable.S for doing lazy icache flush
644 */
645unsigned int hash_page_do_lazy_icache(unsigned int pp, pte_t pte, int trap)
646{
647 struct page *page;
648
Benjamin Herrenschmidt76c8e252005-11-08 11:21:05 +1100649 if (!pfn_valid(pte_pfn(pte)))
650 return pp;
651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 page = pte_page(pte);
653
654 /* page is dirty */
655 if (!test_bit(PG_arch_1, &page->flags) && !PageReserved(page)) {
656 if (trap == 0x400) {
657 __flush_dcache_icache(page_address(page));
658 set_bit(PG_arch_1, &page->flags);
659 } else
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100660 pp |= HPTE_R_N;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 }
662 return pp;
663}
664
Paul Mackerras721151d2007-04-03 21:24:02 +1000665/*
666 * Demote a segment to using 4k pages.
667 * For now this makes the whole process use 4k pages.
668 */
Paul Mackerras721151d2007-04-03 21:24:02 +1000669#ifdef CONFIG_PPC_64K_PAGES
Paul Mackerrasfa282372008-01-24 08:35:13 +1100670void demote_segment_4k(struct mm_struct *mm, unsigned long addr)
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +1000671{
Paul Mackerras721151d2007-04-03 21:24:02 +1000672 if (mm->context.user_psize == MMU_PAGE_4K)
673 return;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000674 slice_set_user_psize(mm, MMU_PAGE_4K);
Geert Uytterhoeven1e57ba82007-07-17 02:35:38 +1000675#ifdef CONFIG_SPU_BASE
Paul Mackerras721151d2007-04-03 21:24:02 +1000676 spu_flush_all_slbs(mm);
677#endif
Paul Mackerrasfa282372008-01-24 08:35:13 +1100678 if (get_paca()->context.user_psize != MMU_PAGE_4K) {
679 get_paca()->context = mm->context;
680 slb_flush_and_rebolt();
681 }
Paul Mackerras721151d2007-04-03 21:24:02 +1000682}
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +1000683#endif /* CONFIG_PPC_64K_PAGES */
Paul Mackerras721151d2007-04-03 21:24:02 +1000684
Paul Mackerrasfa282372008-01-24 08:35:13 +1100685#ifdef CONFIG_PPC_SUBPAGE_PROT
686/*
687 * This looks up a 2-bit protection code for a 4k subpage of a 64k page.
688 * Userspace sets the subpage permissions using the subpage_prot system call.
689 *
690 * Result is 0: full permissions, _PAGE_RW: read-only,
691 * _PAGE_USER or _PAGE_USER|_PAGE_RW: no access.
692 */
693static int subpage_protection(pgd_t *pgdir, unsigned long ea)
694{
695 struct subpage_prot_table *spt = pgd_subpage_prot(pgdir);
696 u32 spp = 0;
697 u32 **sbpm, *sbpp;
698
699 if (ea >= spt->maxaddr)
700 return 0;
701 if (ea < 0x100000000) {
702 /* addresses below 4GB use spt->low_prot */
703 sbpm = spt->low_prot;
704 } else {
705 sbpm = spt->protptrs[ea >> SBP_L3_SHIFT];
706 if (!sbpm)
707 return 0;
708 }
709 sbpp = sbpm[(ea >> SBP_L2_SHIFT) & (SBP_L2_COUNT - 1)];
710 if (!sbpp)
711 return 0;
712 spp = sbpp[(ea >> PAGE_SHIFT) & (SBP_L1_COUNT - 1)];
713
714 /* extract 2-bit bitfield for this 4k subpage */
715 spp >>= 30 - 2 * ((ea >> 12) & 0xf);
716
717 /* turn 0,1,2,3 into combination of _PAGE_USER and _PAGE_RW */
718 spp = ((spp & 2) ? _PAGE_USER : 0) | ((spp & 1) ? _PAGE_RW : 0);
719 return spp;
720}
721
722#else /* CONFIG_PPC_SUBPAGE_PROT */
723static inline int subpage_protection(pgd_t *pgdir, unsigned long ea)
724{
725 return 0;
726}
727#endif
728
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729/* Result code is:
730 * 0 - handled
731 * 1 - normal page fault
732 * -1 - critical hash insertion error
Paul Mackerrasfa282372008-01-24 08:35:13 +1100733 * -2 - access not permitted by subpage protection mechanism
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 */
735int hash_page(unsigned long ea, unsigned long access, unsigned long trap)
736{
737 void *pgdir;
738 unsigned long vsid;
739 struct mm_struct *mm;
740 pte_t *ptep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 cpumask_t tmp;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100742 int rc, user_region = 0, local = 0;
Paul Mackerras1189be62007-10-11 20:37:10 +1000743 int psize, ssize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100745 DBG_LOW("hash_page(ea=%016lx, access=%lx, trap=%lx\n",
746 ea, access, trap);
David Gibson1f8d4192005-05-05 16:15:13 -0700747
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100748 if ((ea & ~REGION_MASK) >= PGTABLE_RANGE) {
749 DBG_LOW(" out of pgtable range !\n");
750 return 1;
751 }
752
753 /* Get region & vsid */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 switch (REGION_ID(ea)) {
755 case USER_REGION_ID:
756 user_region = 1;
757 mm = current->mm;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100758 if (! mm) {
759 DBG_LOW(" user region with no mm !\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 return 1;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100761 }
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +1000762#ifdef CONFIG_PPC_MM_SLICES
763 psize = get_slice_psize(mm, ea);
764#else
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000765 psize = mm->context.user_psize;
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +1000766#endif
Paul Mackerras1189be62007-10-11 20:37:10 +1000767 ssize = user_segment_size(ea);
768 vsid = get_vsid(mm->context.id, ea, ssize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 case VMALLOC_REGION_ID:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 mm = &init_mm;
Paul Mackerras1189be62007-10-11 20:37:10 +1000772 vsid = get_kernel_vsid(ea, mmu_kernel_ssize);
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000773 if (ea < VMALLOC_END)
774 psize = mmu_vmalloc_psize;
775 else
776 psize = mmu_io_psize;
Paul Mackerras1189be62007-10-11 20:37:10 +1000777 ssize = mmu_kernel_ssize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 default:
780 /* Not a valid range
781 * Send the problem up to do_page_fault
782 */
783 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 }
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100785 DBG_LOW(" mm=%p, mm->pgdir=%p, vsid=%016lx\n", mm, mm->pgd, vsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100787 /* Get pgdir */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 pgdir = mm->pgd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 if (pgdir == NULL)
790 return 1;
791
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100792 /* Check CPU locality */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 tmp = cpumask_of_cpu(smp_processor_id());
794 if (user_region && cpus_equal(mm->cpu_vm_mask, tmp))
795 local = 1;
796
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000797#ifdef CONFIG_HUGETLB_PAGE
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100798 /* Handle hugepage regions */
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +1000799 if (HPAGE_SHIFT && psize == mmu_huge_psize) {
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100800 DBG_LOW(" -> huge page !\n");
David Gibsoncbf52af2005-12-09 14:20:52 +1100801 return hash_huge_page(mm, access, ea, vsid, local, trap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 }
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000803#endif /* CONFIG_HUGETLB_PAGE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +1000805#ifndef CONFIG_PPC_64K_PAGES
806 /* If we use 4K pages and our psize is not 4K, then we are hitting
807 * a special driver mapping, we need to align the address before
808 * we fetch the PTE
809 */
810 if (psize != MMU_PAGE_4K)
811 ea &= ~((1ul << mmu_psize_defs[psize].shift) - 1);
812#endif /* CONFIG_PPC_64K_PAGES */
813
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100814 /* Get PTE and page size from page tables */
815 ptep = find_linux_pte(pgdir, ea);
816 if (ptep == NULL || !pte_present(*ptep)) {
817 DBG_LOW(" no PTE !\n");
818 return 1;
819 }
820
821#ifndef CONFIG_PPC_64K_PAGES
822 DBG_LOW(" i-pte: %016lx\n", pte_val(*ptep));
823#else
824 DBG_LOW(" i-pte: %016lx %016lx\n", pte_val(*ptep),
825 pte_val(*(ptep + PTRS_PER_PTE)));
826#endif
827 /* Pre-check access permissions (will be re-checked atomically
828 * in __hash_page_XX but this pre-check is a fast path
829 */
830 if (access & ~pte_val(*ptep)) {
831 DBG_LOW(" no access !\n");
832 return 1;
833 }
834
835 /* Do actual hashing */
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +1000836#ifdef CONFIG_PPC_64K_PAGES
Paul Mackerras721151d2007-04-03 21:24:02 +1000837 /* If _PAGE_4K_PFN is set, make sure this is a 4k segment */
838 if (pte_val(*ptep) & _PAGE_4K_PFN) {
839 demote_segment_4k(mm, ea);
840 psize = MMU_PAGE_4K;
841 }
842
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +1000843 /* If this PTE is non-cacheable and we have restrictions on
844 * using non cacheable large pages, then we switch to 4k
845 */
846 if (mmu_ci_restrictions && psize == MMU_PAGE_64K &&
847 (pte_val(*ptep) & _PAGE_NO_CACHE)) {
848 if (user_region) {
849 demote_segment_4k(mm, ea);
850 psize = MMU_PAGE_4K;
851 } else if (ea < VMALLOC_END) {
852 /*
853 * some driver did a non-cacheable mapping
854 * in vmalloc space, so switch vmalloc
855 * to 4k pages
856 */
857 printk(KERN_ALERT "Reducing vmalloc segment "
858 "to 4kB pages because of "
859 "non-cacheable mapping\n");
860 psize = mmu_vmalloc_psize = MMU_PAGE_4K;
Geert Uytterhoeven1e57ba82007-07-17 02:35:38 +1000861#ifdef CONFIG_SPU_BASE
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +0100862 spu_flush_all_slbs(mm);
863#endif
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000864 }
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +1000865 }
866 if (user_region) {
867 if (psize != get_paca()->context.user_psize) {
Benjamin Herrenschmidtf6ab0b92007-10-29 12:05:18 +1100868 get_paca()->context = mm->context;
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000869 slb_flush_and_rebolt();
870 }
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +1000871 } else if (get_paca()->vmalloc_sllp !=
872 mmu_psize_defs[mmu_vmalloc_psize].sllp) {
873 get_paca()->vmalloc_sllp =
874 mmu_psize_defs[mmu_vmalloc_psize].sllp;
Michael Neuling67439b72007-08-03 11:55:39 +1000875 slb_vmalloc_update();
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000876 }
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +1000877#endif /* CONFIG_PPC_64K_PAGES */
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +1000878
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +1000879#ifdef CONFIG_PPC_HAS_HASH_64K
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000880 if (psize == MMU_PAGE_64K)
Paul Mackerras1189be62007-10-11 20:37:10 +1000881 rc = __hash_page_64K(ea, access, vsid, ptep, trap, local, ssize);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100882 else
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +1000883#endif /* CONFIG_PPC_HAS_HASH_64K */
Paul Mackerrasfa282372008-01-24 08:35:13 +1100884 {
885 int spp = subpage_protection(pgdir, ea);
886 if (access & spp)
887 rc = -2;
888 else
889 rc = __hash_page_4K(ea, access, vsid, ptep, trap,
890 local, ssize, spp);
891 }
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100892
893#ifndef CONFIG_PPC_64K_PAGES
894 DBG_LOW(" o-pte: %016lx\n", pte_val(*ptep));
895#else
896 DBG_LOW(" o-pte: %016lx %016lx\n", pte_val(*ptep),
897 pte_val(*(ptep + PTRS_PER_PTE)));
898#endif
899 DBG_LOW(" -> rc=%d\n", rc);
900 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901}
Arnd Bergmann67207b92005-11-15 15:53:48 -0500902EXPORT_SYMBOL_GPL(hash_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100904void hash_preload(struct mm_struct *mm, unsigned long ea,
905 unsigned long access, unsigned long trap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100907 unsigned long vsid;
908 void *pgdir;
909 pte_t *ptep;
910 cpumask_t mask;
911 unsigned long flags;
912 int local = 0;
Paul Mackerras1189be62007-10-11 20:37:10 +1000913 int ssize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000915 BUG_ON(REGION_ID(ea) != USER_REGION_ID);
916
917#ifdef CONFIG_PPC_MM_SLICES
918 /* We only prefault standard pages for now */
Ilpo Järvinen2b02d132007-08-16 08:03:35 +1000919 if (unlikely(get_slice_psize(mm, ea) != mm->context.user_psize))
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100920 return;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000921#endif
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100922
923 DBG_LOW("hash_preload(mm=%p, mm->pgdir=%p, ea=%016lx, access=%lx,"
924 " trap=%lx\n", mm, mm->pgd, ea, access, trap);
925
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +1000926 /* Get Linux PTE if available */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100927 pgdir = mm->pgd;
928 if (pgdir == NULL)
929 return;
930 ptep = find_linux_pte(pgdir, ea);
931 if (!ptep)
932 return;
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +1000933
934#ifdef CONFIG_PPC_64K_PAGES
935 /* If either _PAGE_4K_PFN or _PAGE_NO_CACHE is set (and we are on
936 * a 64K kernel), then we don't preload, hash_page() will take
937 * care of it once we actually try to access the page.
938 * That way we don't have to duplicate all of the logic for segment
939 * page size demotion here
940 */
941 if (pte_val(*ptep) & (_PAGE_4K_PFN | _PAGE_NO_CACHE))
942 return;
943#endif /* CONFIG_PPC_64K_PAGES */
944
945 /* Get VSID */
Paul Mackerras1189be62007-10-11 20:37:10 +1000946 ssize = user_segment_size(ea);
947 vsid = get_vsid(mm->context.id, ea, ssize);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100948
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +1000949 /* Hash doesn't like irqs */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100950 local_irq_save(flags);
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +1000951
952 /* Is that local to this CPU ? */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100953 mask = cpumask_of_cpu(smp_processor_id());
954 if (cpus_equal(mm->cpu_vm_mask, mask))
955 local = 1;
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +1000956
957 /* Hash it in */
958#ifdef CONFIG_PPC_HAS_HASH_64K
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000959 if (mm->context.user_psize == MMU_PAGE_64K)
Paul Mackerras1189be62007-10-11 20:37:10 +1000960 __hash_page_64K(ea, access, vsid, ptep, trap, local, ssize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 else
Jon Tollefson5b825832007-05-17 04:43:02 +1000962#endif /* CONFIG_PPC_HAS_HASH_64K */
Paul Mackerrasfa282372008-01-24 08:35:13 +1100963 __hash_page_4K(ea, access, vsid, ptep, trap, local, ssize,
964 subpage_protection(pgdir, ea));
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +1000965
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100966 local_irq_restore(flags);
967}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
Benjamin Herrenschmidtf6ab0b92007-10-29 12:05:18 +1100969/* WARNING: This is called from hash_low_64.S, if you change this prototype,
970 * do not forget to update the assembly call site !
971 */
Paul Mackerras1189be62007-10-11 20:37:10 +1000972void flush_hash_page(unsigned long va, real_pte_t pte, int psize, int ssize,
973 int local)
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100974{
975 unsigned long hash, index, shift, hidx, slot;
976
977 DBG_LOW("flush_hash_page(va=%016x)\n", va);
978 pte_iterate_hashed_subpages(pte, psize, va, index, shift) {
Paul Mackerras1189be62007-10-11 20:37:10 +1000979 hash = hpt_hash(va, shift, ssize);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100980 hidx = __rpte_to_hidx(pte, index);
981 if (hidx & _PTEIDX_SECONDARY)
982 hash = ~hash;
983 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
984 slot += hidx & _PTEIDX_GROUP_IX;
985 DBG_LOW(" sub %d: hash=%x, hidx=%x\n", index, slot, hidx);
Paul Mackerras1189be62007-10-11 20:37:10 +1000986 ppc_md.hpte_invalidate(slot, va, psize, ssize, local);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100987 } pte_iterate_hashed_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988}
989
Benjamin Herrenschmidt61b1a942005-09-20 13:52:50 +1000990void flush_hash_range(unsigned long number, int local)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991{
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100992 if (ppc_md.flush_hash_range)
Benjamin Herrenschmidt61b1a942005-09-20 13:52:50 +1000993 ppc_md.flush_hash_range(number, local);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100994 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 int i;
Benjamin Herrenschmidt61b1a942005-09-20 13:52:50 +1000996 struct ppc64_tlb_batch *batch =
997 &__get_cpu_var(ppc64_tlb_batch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
999 for (i = 0; i < number; i++)
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +11001000 flush_hash_page(batch->vaddr[i], batch->pte[i],
Paul Mackerras1189be62007-10-11 20:37:10 +10001001 batch->psize, batch->ssize, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 }
1003}
1004
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005/*
1006 * low_hash_fault is called when we the low level hash code failed
1007 * to instert a PTE due to an hypervisor error
1008 */
Paul Mackerrasfa282372008-01-24 08:35:13 +11001009void low_hash_fault(struct pt_regs *regs, unsigned long address, int rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010{
1011 if (user_mode(regs)) {
Paul Mackerrasfa282372008-01-24 08:35:13 +11001012#ifdef CONFIG_PPC_SUBPAGE_PROT
1013 if (rc == -2)
1014 _exception(SIGSEGV, regs, SEGV_ACCERR, address);
1015 else
1016#endif
1017 _exception(SIGBUS, regs, BUS_ADRERR, address);
1018 } else
1019 bad_page_fault(regs, address, SIGBUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020}
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +10001021
1022#ifdef CONFIG_DEBUG_PAGEALLOC
1023static void kernel_map_linear_page(unsigned long vaddr, unsigned long lmi)
1024{
Paul Mackerras1189be62007-10-11 20:37:10 +10001025 unsigned long hash, hpteg;
1026 unsigned long vsid = get_kernel_vsid(vaddr, mmu_kernel_ssize);
1027 unsigned long va = hpt_va(vaddr, vsid, mmu_kernel_ssize);
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +10001028 unsigned long mode = _PAGE_ACCESSED | _PAGE_DIRTY |
1029 _PAGE_COHERENT | PP_RWXX | HPTE_R_N;
1030 int ret;
1031
Paul Mackerras1189be62007-10-11 20:37:10 +10001032 hash = hpt_hash(va, PAGE_SHIFT, mmu_kernel_ssize);
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +10001033 hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
1034
1035 ret = ppc_md.hpte_insert(hpteg, va, __pa(vaddr),
Paul Mackerras1189be62007-10-11 20:37:10 +10001036 mode, HPTE_V_BOLTED,
1037 mmu_linear_psize, mmu_kernel_ssize);
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +10001038 BUG_ON (ret < 0);
1039 spin_lock(&linear_map_hash_lock);
1040 BUG_ON(linear_map_hash_slots[lmi] & 0x80);
1041 linear_map_hash_slots[lmi] = ret | 0x80;
1042 spin_unlock(&linear_map_hash_lock);
1043}
1044
1045static void kernel_unmap_linear_page(unsigned long vaddr, unsigned long lmi)
1046{
Paul Mackerras1189be62007-10-11 20:37:10 +10001047 unsigned long hash, hidx, slot;
1048 unsigned long vsid = get_kernel_vsid(vaddr, mmu_kernel_ssize);
1049 unsigned long va = hpt_va(vaddr, vsid, mmu_kernel_ssize);
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +10001050
Paul Mackerras1189be62007-10-11 20:37:10 +10001051 hash = hpt_hash(va, PAGE_SHIFT, mmu_kernel_ssize);
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +10001052 spin_lock(&linear_map_hash_lock);
1053 BUG_ON(!(linear_map_hash_slots[lmi] & 0x80));
1054 hidx = linear_map_hash_slots[lmi] & 0x7f;
1055 linear_map_hash_slots[lmi] = 0;
1056 spin_unlock(&linear_map_hash_lock);
1057 if (hidx & _PTEIDX_SECONDARY)
1058 hash = ~hash;
1059 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
1060 slot += hidx & _PTEIDX_GROUP_IX;
Paul Mackerras1189be62007-10-11 20:37:10 +10001061 ppc_md.hpte_invalidate(slot, va, mmu_linear_psize, mmu_kernel_ssize, 0);
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +10001062}
1063
1064void kernel_map_pages(struct page *page, int numpages, int enable)
1065{
1066 unsigned long flags, vaddr, lmi;
1067 int i;
1068
1069 local_irq_save(flags);
1070 for (i = 0; i < numpages; i++, page++) {
1071 vaddr = (unsigned long)page_address(page);
1072 lmi = __pa(vaddr) >> PAGE_SHIFT;
1073 if (lmi >= linear_map_hash_count)
1074 continue;
1075 if (enable)
1076 kernel_map_linear_page(vaddr, lmi);
1077 else
1078 kernel_unmap_linear_page(vaddr, lmi);
1079 }
1080 local_irq_restore(flags);
1081}
1082#endif /* CONFIG_DEBUG_PAGEALLOC */