blob: 73b78486abfc5b6ee4283d24e35cb30021f6f8ea [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
Paul Mackerras1189be62007-10-11 20:37:10 +1000195static int __init htab_dt_scan_seg_sizes(unsigned long node,
196 const char *uname, int depth,
197 void *data)
198{
199 char *type = of_get_flat_dt_prop(node, "device_type", NULL);
200 u32 *prop;
201 unsigned long size = 0;
202
203 /* We are scanning "cpu" nodes only */
204 if (type == NULL || strcmp(type, "cpu") != 0)
205 return 0;
206
207 prop = (u32 *)of_get_flat_dt_prop(node, "ibm,processor-segment-sizes",
208 &size);
209 if (prop == NULL)
210 return 0;
211 for (; size >= 4; size -= 4, ++prop) {
212 if (prop[0] == 40) {
213 DBG("1T segment support detected\n");
214 cur_cpu_spec->cpu_features |= CPU_FTR_1T_SEGMENT;
Olof Johanssonf5534002007-10-12 16:44:55 +1000215 return 1;
Paul Mackerras1189be62007-10-11 20:37:10 +1000216 }
Paul Mackerras1189be62007-10-11 20:37:10 +1000217 }
Olof Johanssonf66bce52007-10-16 00:58:59 +1000218 cur_cpu_spec->cpu_features &= ~CPU_FTR_NO_SLBIE_B;
Paul Mackerras1189be62007-10-11 20:37:10 +1000219 return 0;
220}
221
222static void __init htab_init_seg_sizes(void)
223{
224 of_scan_flat_dt(htab_dt_scan_seg_sizes, NULL);
225}
226
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100227static int __init htab_dt_scan_page_sizes(unsigned long node,
228 const char *uname, int depth,
229 void *data)
230{
231 char *type = of_get_flat_dt_prop(node, "device_type", NULL);
232 u32 *prop;
233 unsigned long size = 0;
234
235 /* We are scanning "cpu" nodes only */
236 if (type == NULL || strcmp(type, "cpu") != 0)
237 return 0;
238
239 prop = (u32 *)of_get_flat_dt_prop(node,
240 "ibm,segment-page-sizes", &size);
241 if (prop != NULL) {
242 DBG("Page sizes from device-tree:\n");
243 size /= 4;
244 cur_cpu_spec->cpu_features &= ~(CPU_FTR_16M_PAGE);
245 while(size > 0) {
246 unsigned int shift = prop[0];
247 unsigned int slbenc = prop[1];
248 unsigned int lpnum = prop[2];
249 unsigned int lpenc = 0;
250 struct mmu_psize_def *def;
251 int idx = -1;
252
253 size -= 3; prop += 3;
254 while(size > 0 && lpnum) {
255 if (prop[0] == shift)
256 lpenc = prop[1];
257 prop += 2; size -= 2;
258 lpnum--;
259 }
260 switch(shift) {
261 case 0xc:
262 idx = MMU_PAGE_4K;
263 break;
264 case 0x10:
265 idx = MMU_PAGE_64K;
266 break;
267 case 0x14:
268 idx = MMU_PAGE_1M;
269 break;
270 case 0x18:
271 idx = MMU_PAGE_16M;
272 cur_cpu_spec->cpu_features |= CPU_FTR_16M_PAGE;
273 break;
274 case 0x22:
275 idx = MMU_PAGE_16G;
276 break;
277 }
278 if (idx < 0)
279 continue;
280 def = &mmu_psize_defs[idx];
281 def->shift = shift;
282 if (shift <= 23)
283 def->avpnm = 0;
284 else
285 def->avpnm = (1 << (shift - 23)) - 1;
286 def->sllp = slbenc;
287 def->penc = lpenc;
288 /* We don't know for sure what's up with tlbiel, so
289 * for now we only set it for 4K and 64K pages
290 */
291 if (idx == MMU_PAGE_4K || idx == MMU_PAGE_64K)
292 def->tlbiel = 1;
293 else
294 def->tlbiel = 0;
295
296 DBG(" %d: shift=%02x, sllp=%04x, avpnm=%08x, "
297 "tlbiel=%d, penc=%d\n",
298 idx, shift, def->sllp, def->avpnm, def->tlbiel,
299 def->penc);
300 }
301 return 1;
302 }
303 return 0;
304}
305
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100306static void __init htab_init_page_sizes(void)
307{
308 int rc;
309
310 /* Default to 4K pages only */
311 memcpy(mmu_psize_defs, mmu_psize_defaults_old,
312 sizeof(mmu_psize_defaults_old));
313
314 /*
315 * Try to find the available page sizes in the device-tree
316 */
317 rc = of_scan_flat_dt(htab_dt_scan_page_sizes, NULL);
318 if (rc != 0) /* Found */
319 goto found;
320
321 /*
322 * Not in the device-tree, let's fallback on known size
323 * list for 16M capable GP & GR
324 */
Stephen Rothwell04704662006-11-30 11:46:22 +1100325 if (cpu_has_feature(CPU_FTR_16M_PAGE))
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100326 memcpy(mmu_psize_defs, mmu_psize_defaults_gp,
327 sizeof(mmu_psize_defaults_gp));
328 found:
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000329#ifndef CONFIG_DEBUG_PAGEALLOC
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100330 /*
331 * Pick a size for the linear mapping. Currently, we only support
332 * 16M, 1M and 4K which is the default
333 */
334 if (mmu_psize_defs[MMU_PAGE_16M].shift)
335 mmu_linear_psize = MMU_PAGE_16M;
336 else if (mmu_psize_defs[MMU_PAGE_1M].shift)
337 mmu_linear_psize = MMU_PAGE_1M;
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000338#endif /* CONFIG_DEBUG_PAGEALLOC */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100339
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000340#ifdef CONFIG_PPC_64K_PAGES
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100341 /*
342 * Pick a size for the ordinary pages. Default is 4K, we support
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000343 * 64K for user mappings and vmalloc if supported by the processor.
344 * We only use 64k for ioremap if the processor
345 * (and firmware) support cache-inhibited large pages.
346 * If not, we use 4k and set mmu_ci_restrictions so that
347 * hash_page knows to switch processes that use cache-inhibited
348 * mappings to 4k pages.
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100349 */
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000350 if (mmu_psize_defs[MMU_PAGE_64K].shift) {
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100351 mmu_virtual_psize = MMU_PAGE_64K;
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000352 mmu_vmalloc_psize = MMU_PAGE_64K;
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000353 if (mmu_linear_psize == MMU_PAGE_4K)
354 mmu_linear_psize = MMU_PAGE_64K;
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000355 if (cpu_has_feature(CPU_FTR_CI_LARGE_PAGE))
356 mmu_io_psize = MMU_PAGE_64K;
357 else
358 mmu_ci_restrictions = 1;
359 }
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000360#endif /* CONFIG_PPC_64K_PAGES */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100361
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000362 printk(KERN_DEBUG "Page orders: linear mapping = %d, "
363 "virtual = %d, io = %d\n",
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100364 mmu_psize_defs[mmu_linear_psize].shift,
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000365 mmu_psize_defs[mmu_virtual_psize].shift,
366 mmu_psize_defs[mmu_io_psize].shift);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100367
368#ifdef CONFIG_HUGETLB_PAGE
369 /* Init large page size. Currently, we pick 16M or 1M depending
370 * on what is available
371 */
372 if (mmu_psize_defs[MMU_PAGE_16M].shift)
Jon Tollefson4ec161c2008-01-04 09:59:50 +1100373 set_huge_psize(MMU_PAGE_16M);
David Gibson7d24f0b2005-11-07 00:57:52 -0800374 /* With 4k/4level pagetables, we can't (for now) cope with a
375 * huge page size < PMD_SIZE */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100376 else if (mmu_psize_defs[MMU_PAGE_1M].shift)
Jon Tollefson4ec161c2008-01-04 09:59:50 +1100377 set_huge_psize(MMU_PAGE_1M);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100378#endif /* CONFIG_HUGETLB_PAGE */
379}
380
381static int __init htab_dt_scan_pftsize(unsigned long node,
382 const char *uname, int depth,
383 void *data)
384{
385 char *type = of_get_flat_dt_prop(node, "device_type", NULL);
386 u32 *prop;
387
388 /* We are scanning "cpu" nodes only */
389 if (type == NULL || strcmp(type, "cpu") != 0)
390 return 0;
391
392 prop = (u32 *)of_get_flat_dt_prop(node, "ibm,pft-size", NULL);
393 if (prop != NULL) {
394 /* pft_size[0] is the NUMA CEC cookie */
395 ppc64_pft_size = prop[1];
396 return 1;
397 }
398 return 0;
399}
400
401static unsigned long __init htab_get_table_size(void)
Paul Mackerras3eac8c62005-10-12 16:58:53 +1000402{
Paul Mackerras799d6042005-11-10 13:37:51 +1100403 unsigned long mem_size, rnd_mem_size, pteg_count;
Paul Mackerras3eac8c62005-10-12 16:58:53 +1000404
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100405 /* If hash size isn't already provided by the platform, we try to
Adrian Bunk943ffb52006-01-10 00:10:13 +0100406 * retrieve it from the device-tree. If it's not there neither, we
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100407 * calculate it now based on the total RAM size
Paul Mackerras3eac8c62005-10-12 16:58:53 +1000408 */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100409 if (ppc64_pft_size == 0)
410 of_scan_flat_dt(htab_dt_scan_pftsize, NULL);
Paul Mackerras3eac8c62005-10-12 16:58:53 +1000411 if (ppc64_pft_size)
412 return 1UL << ppc64_pft_size;
413
414 /* round mem_size up to next power of 2 */
Paul Mackerras799d6042005-11-10 13:37:51 +1100415 mem_size = lmb_phys_mem_size();
416 rnd_mem_size = 1UL << __ilog2(mem_size);
417 if (rnd_mem_size < mem_size)
Paul Mackerras3eac8c62005-10-12 16:58:53 +1000418 rnd_mem_size <<= 1;
419
420 /* # pages / 2 */
421 pteg_count = max(rnd_mem_size >> (12 + 1), 1UL << 11);
422
423 return pteg_count << 7;
424}
425
Mike Kravetz54b79242005-11-07 16:25:48 -0800426#ifdef CONFIG_MEMORY_HOTPLUG
427void create_section_mapping(unsigned long start, unsigned long end)
428{
Michael Ellermancaf80e52006-03-21 20:45:51 +1100429 BUG_ON(htab_bolt_mapping(start, end, __pa(start),
Mike Kravetz54b79242005-11-07 16:25:48 -0800430 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_COHERENT | PP_RWXX,
Paul Mackerras1189be62007-10-11 20:37:10 +1000431 mmu_linear_psize, mmu_kernel_ssize));
Mike Kravetz54b79242005-11-07 16:25:48 -0800432}
433#endif /* CONFIG_MEMORY_HOTPLUG */
434
Michael Ellerman7d0daae2006-06-23 18:16:38 +1000435static inline void make_bl(unsigned int *insn_addr, void *func)
436{
437 unsigned long funcp = *((unsigned long *)func);
438 int offset = funcp - (unsigned long)insn_addr;
439
440 *insn_addr = (unsigned int)(0x48000001 | (offset & 0x03fffffc));
441 flush_icache_range((unsigned long)insn_addr, 4+
442 (unsigned long)insn_addr);
443}
444
445static void __init htab_finish_init(void)
446{
447 extern unsigned int *htab_call_hpte_insert1;
448 extern unsigned int *htab_call_hpte_insert2;
449 extern unsigned int *htab_call_hpte_remove;
450 extern unsigned int *htab_call_hpte_updatepp;
451
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +1000452#ifdef CONFIG_PPC_HAS_HASH_64K
Michael Ellerman7d0daae2006-06-23 18:16:38 +1000453 extern unsigned int *ht64_call_hpte_insert1;
454 extern unsigned int *ht64_call_hpte_insert2;
455 extern unsigned int *ht64_call_hpte_remove;
456 extern unsigned int *ht64_call_hpte_updatepp;
457
458 make_bl(ht64_call_hpte_insert1, ppc_md.hpte_insert);
459 make_bl(ht64_call_hpte_insert2, ppc_md.hpte_insert);
460 make_bl(ht64_call_hpte_remove, ppc_md.hpte_remove);
461 make_bl(ht64_call_hpte_updatepp, ppc_md.hpte_updatepp);
Jon Tollefson5b825832007-05-17 04:43:02 +1000462#endif /* CONFIG_PPC_HAS_HASH_64K */
Michael Ellerman7d0daae2006-06-23 18:16:38 +1000463
464 make_bl(htab_call_hpte_insert1, ppc_md.hpte_insert);
465 make_bl(htab_call_hpte_insert2, ppc_md.hpte_insert);
466 make_bl(htab_call_hpte_remove, ppc_md.hpte_remove);
467 make_bl(htab_call_hpte_updatepp, ppc_md.hpte_updatepp);
468}
469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470void __init htab_initialize(void)
471{
Michael Ellerman337a7122006-02-21 17:22:55 +1100472 unsigned long table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 unsigned long pteg_count;
474 unsigned long mode_rw;
Michael Ellerman41d824b2008-01-30 01:13:59 +1100475 unsigned long base = 0, size = 0, limit;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100476 int i;
477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 extern unsigned long tce_alloc_start, tce_alloc_end;
479
480 DBG(" -> htab_initialize()\n");
481
Paul Mackerras1189be62007-10-11 20:37:10 +1000482 /* Initialize segment sizes */
483 htab_init_seg_sizes();
484
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100485 /* Initialize page sizes */
486 htab_init_page_sizes();
487
Paul Mackerras1189be62007-10-11 20:37:10 +1000488 if (cpu_has_feature(CPU_FTR_1T_SEGMENT)) {
489 mmu_kernel_ssize = MMU_SEGSIZE_1T;
490 mmu_highuser_ssize = MMU_SEGSIZE_1T;
491 printk(KERN_INFO "Using 1TB segments\n");
492 }
493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 /*
495 * Calculate the required size of the htab. We want the number of
496 * PTEGs to equal one half the number of real pages.
497 */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100498 htab_size_bytes = htab_get_table_size();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 pteg_count = htab_size_bytes >> 7;
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 htab_hash_mask = pteg_count - 1;
502
Michael Ellerman57cfb812006-03-21 20:45:59 +1100503 if (firmware_has_feature(FW_FEATURE_LPAR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 /* Using a hypervisor which owns the htab */
505 htab_address = NULL;
506 _SDR1 = 0;
507 } else {
508 /* Find storage for the HPT. Must be contiguous in
Michael Ellerman41d824b2008-01-30 01:13:59 +1100509 * the absolute address space. On cell we want it to be
510 * in the first 1 Gig.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 */
Michael Ellerman41d824b2008-01-30 01:13:59 +1100512 if (machine_is(cell))
513 limit = 0x40000000;
514 else
515 limit = 0;
516
517 table = lmb_alloc_base(htab_size_bytes, htab_size_bytes, limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
519 DBG("Hash table allocated at %lx, size: %lx\n", table,
520 htab_size_bytes);
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 htab_address = abs_to_virt(table);
523
524 /* htab absolute addr + encoded htabsize */
525 _SDR1 = table + __ilog2(pteg_count) - 11;
526
527 /* Initialize the HPT with no entries */
528 memset((void *)table, 0, htab_size_bytes);
Paul Mackerras799d6042005-11-10 13:37:51 +1100529
530 /* Set SDR1 */
531 mtspr(SPRN_SDR1, _SDR1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 }
533
Anton Blanchard515bae92005-06-21 17:15:55 -0700534 mode_rw = _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_COHERENT | PP_RWXX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000536#ifdef CONFIG_DEBUG_PAGEALLOC
537 linear_map_hash_count = lmb_end_of_DRAM() >> PAGE_SHIFT;
538 linear_map_hash_slots = __va(lmb_alloc_base(linear_map_hash_count,
539 1, lmb.rmo_size));
540 memset(linear_map_hash_slots, 0, linear_map_hash_count);
541#endif /* CONFIG_DEBUG_PAGEALLOC */
542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 /* On U3 based machines, we need to reserve the DART area and
544 * _NOT_ map it to avoid cache paradoxes as it's remapped non
545 * cacheable later on
546 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
548 /* create bolted the linear mapping in the hash table */
549 for (i=0; i < lmb.memory.cnt; i++) {
Michael Ellermanb5666f72005-12-05 10:24:33 -0600550 base = (unsigned long)__va(lmb.memory.region[i].base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 size = lmb.memory.region[i].size;
552
553 DBG("creating mapping for region: %lx : %lx\n", base, size);
554
555#ifdef CONFIG_U3_DART
556 /* Do not map the DART space. Fortunately, it will be aligned
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100557 * in such a way that it will not cross two lmb regions and
558 * will fit within a single 16Mb page.
559 * The DART space is assumed to be a full 16Mb region even if
560 * we only use 2Mb of that space. We will use more of it later
561 * for AGP GART. We have to use a full 16Mb large page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 */
563 DBG("DART base: %lx\n", dart_tablebase);
564
565 if (dart_tablebase != 0 && dart_tablebase >= base
566 && dart_tablebase < (base + size)) {
Michael Ellermancaf80e52006-03-21 20:45:51 +1100567 unsigned long dart_table_end = dart_tablebase + 16 * MB;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 if (base != dart_tablebase)
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100569 BUG_ON(htab_bolt_mapping(base, dart_tablebase,
Michael Ellermancaf80e52006-03-21 20:45:51 +1100570 __pa(base), mode_rw,
Paul Mackerras1189be62007-10-11 20:37:10 +1000571 mmu_linear_psize,
572 mmu_kernel_ssize));
Michael Ellermancaf80e52006-03-21 20:45:51 +1100573 if ((base + size) > dart_table_end)
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100574 BUG_ON(htab_bolt_mapping(dart_tablebase+16*MB,
Michael Ellermancaf80e52006-03-21 20:45:51 +1100575 base + size,
576 __pa(dart_table_end),
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100577 mode_rw,
Paul Mackerras1189be62007-10-11 20:37:10 +1000578 mmu_linear_psize,
579 mmu_kernel_ssize));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 continue;
581 }
582#endif /* CONFIG_U3_DART */
Michael Ellermancaf80e52006-03-21 20:45:51 +1100583 BUG_ON(htab_bolt_mapping(base, base + size, __pa(base),
Paul Mackerras1189be62007-10-11 20:37:10 +1000584 mode_rw, mmu_linear_psize, mmu_kernel_ssize));
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100585 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587 /*
588 * If we have a memory_limit and we've allocated TCEs then we need to
589 * explicitly map the TCE area at the top of RAM. We also cope with the
590 * case that the TCEs start below memory_limit.
591 * tce_alloc_start/end are 16MB aligned so the mapping should work
592 * for either 4K or 16MB pages.
593 */
594 if (tce_alloc_start) {
Michael Ellermanb5666f72005-12-05 10:24:33 -0600595 tce_alloc_start = (unsigned long)__va(tce_alloc_start);
596 tce_alloc_end = (unsigned long)__va(tce_alloc_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598 if (base + size >= tce_alloc_start)
599 tce_alloc_start = base + size + 1;
600
Michael Ellermancaf80e52006-03-21 20:45:51 +1100601 BUG_ON(htab_bolt_mapping(tce_alloc_start, tce_alloc_end,
602 __pa(tce_alloc_start), mode_rw,
Paul Mackerras1189be62007-10-11 20:37:10 +1000603 mmu_linear_psize, mmu_kernel_ssize));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 }
605
Michael Ellerman7d0daae2006-06-23 18:16:38 +1000606 htab_finish_init();
607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 DBG(" <- htab_initialize()\n");
609}
610#undef KB
611#undef MB
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Anton Blancharde597cb322005-12-29 10:46:29 +1100613void htab_initialize_secondary(void)
Paul Mackerras799d6042005-11-10 13:37:51 +1100614{
Michael Ellerman57cfb812006-03-21 20:45:59 +1100615 if (!firmware_has_feature(FW_FEATURE_LPAR))
Paul Mackerras799d6042005-11-10 13:37:51 +1100616 mtspr(SPRN_SDR1, _SDR1);
617}
618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619/*
620 * Called by asm hashtable.S for doing lazy icache flush
621 */
622unsigned int hash_page_do_lazy_icache(unsigned int pp, pte_t pte, int trap)
623{
624 struct page *page;
625
Benjamin Herrenschmidt76c8e252005-11-08 11:21:05 +1100626 if (!pfn_valid(pte_pfn(pte)))
627 return pp;
628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 page = pte_page(pte);
630
631 /* page is dirty */
632 if (!test_bit(PG_arch_1, &page->flags) && !PageReserved(page)) {
633 if (trap == 0x400) {
634 __flush_dcache_icache(page_address(page));
635 set_bit(PG_arch_1, &page->flags);
636 } else
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100637 pp |= HPTE_R_N;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 }
639 return pp;
640}
641
Paul Mackerras721151d2007-04-03 21:24:02 +1000642/*
643 * Demote a segment to using 4k pages.
644 * For now this makes the whole process use 4k pages.
645 */
Paul Mackerras721151d2007-04-03 21:24:02 +1000646#ifdef CONFIG_PPC_64K_PAGES
Paul Mackerrasfa282372008-01-24 08:35:13 +1100647void demote_segment_4k(struct mm_struct *mm, unsigned long addr)
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +1000648{
Paul Mackerras721151d2007-04-03 21:24:02 +1000649 if (mm->context.user_psize == MMU_PAGE_4K)
650 return;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000651 slice_set_user_psize(mm, MMU_PAGE_4K);
Geert Uytterhoeven1e57ba82007-07-17 02:35:38 +1000652#ifdef CONFIG_SPU_BASE
Paul Mackerras721151d2007-04-03 21:24:02 +1000653 spu_flush_all_slbs(mm);
654#endif
Paul Mackerrasfa282372008-01-24 08:35:13 +1100655 if (get_paca()->context.user_psize != MMU_PAGE_4K) {
656 get_paca()->context = mm->context;
657 slb_flush_and_rebolt();
658 }
Paul Mackerras721151d2007-04-03 21:24:02 +1000659}
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +1000660#endif /* CONFIG_PPC_64K_PAGES */
Paul Mackerras721151d2007-04-03 21:24:02 +1000661
Paul Mackerrasfa282372008-01-24 08:35:13 +1100662#ifdef CONFIG_PPC_SUBPAGE_PROT
663/*
664 * This looks up a 2-bit protection code for a 4k subpage of a 64k page.
665 * Userspace sets the subpage permissions using the subpage_prot system call.
666 *
667 * Result is 0: full permissions, _PAGE_RW: read-only,
668 * _PAGE_USER or _PAGE_USER|_PAGE_RW: no access.
669 */
670static int subpage_protection(pgd_t *pgdir, unsigned long ea)
671{
672 struct subpage_prot_table *spt = pgd_subpage_prot(pgdir);
673 u32 spp = 0;
674 u32 **sbpm, *sbpp;
675
676 if (ea >= spt->maxaddr)
677 return 0;
678 if (ea < 0x100000000) {
679 /* addresses below 4GB use spt->low_prot */
680 sbpm = spt->low_prot;
681 } else {
682 sbpm = spt->protptrs[ea >> SBP_L3_SHIFT];
683 if (!sbpm)
684 return 0;
685 }
686 sbpp = sbpm[(ea >> SBP_L2_SHIFT) & (SBP_L2_COUNT - 1)];
687 if (!sbpp)
688 return 0;
689 spp = sbpp[(ea >> PAGE_SHIFT) & (SBP_L1_COUNT - 1)];
690
691 /* extract 2-bit bitfield for this 4k subpage */
692 spp >>= 30 - 2 * ((ea >> 12) & 0xf);
693
694 /* turn 0,1,2,3 into combination of _PAGE_USER and _PAGE_RW */
695 spp = ((spp & 2) ? _PAGE_USER : 0) | ((spp & 1) ? _PAGE_RW : 0);
696 return spp;
697}
698
699#else /* CONFIG_PPC_SUBPAGE_PROT */
700static inline int subpage_protection(pgd_t *pgdir, unsigned long ea)
701{
702 return 0;
703}
704#endif
705
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706/* Result code is:
707 * 0 - handled
708 * 1 - normal page fault
709 * -1 - critical hash insertion error
Paul Mackerrasfa282372008-01-24 08:35:13 +1100710 * -2 - access not permitted by subpage protection mechanism
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 */
712int hash_page(unsigned long ea, unsigned long access, unsigned long trap)
713{
714 void *pgdir;
715 unsigned long vsid;
716 struct mm_struct *mm;
717 pte_t *ptep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 cpumask_t tmp;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100719 int rc, user_region = 0, local = 0;
Paul Mackerras1189be62007-10-11 20:37:10 +1000720 int psize, ssize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100722 DBG_LOW("hash_page(ea=%016lx, access=%lx, trap=%lx\n",
723 ea, access, trap);
David Gibson1f8d4192005-05-05 16:15:13 -0700724
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100725 if ((ea & ~REGION_MASK) >= PGTABLE_RANGE) {
726 DBG_LOW(" out of pgtable range !\n");
727 return 1;
728 }
729
730 /* Get region & vsid */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 switch (REGION_ID(ea)) {
732 case USER_REGION_ID:
733 user_region = 1;
734 mm = current->mm;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100735 if (! mm) {
736 DBG_LOW(" user region with no mm !\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 return 1;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100738 }
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +1000739#ifdef CONFIG_PPC_MM_SLICES
740 psize = get_slice_psize(mm, ea);
741#else
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000742 psize = mm->context.user_psize;
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +1000743#endif
Paul Mackerras1189be62007-10-11 20:37:10 +1000744 ssize = user_segment_size(ea);
745 vsid = get_vsid(mm->context.id, ea, ssize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 case VMALLOC_REGION_ID:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 mm = &init_mm;
Paul Mackerras1189be62007-10-11 20:37:10 +1000749 vsid = get_kernel_vsid(ea, mmu_kernel_ssize);
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000750 if (ea < VMALLOC_END)
751 psize = mmu_vmalloc_psize;
752 else
753 psize = mmu_io_psize;
Paul Mackerras1189be62007-10-11 20:37:10 +1000754 ssize = mmu_kernel_ssize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 default:
757 /* Not a valid range
758 * Send the problem up to do_page_fault
759 */
760 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 }
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100762 DBG_LOW(" mm=%p, mm->pgdir=%p, vsid=%016lx\n", mm, mm->pgd, vsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100764 /* Get pgdir */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 pgdir = mm->pgd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 if (pgdir == NULL)
767 return 1;
768
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100769 /* Check CPU locality */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 tmp = cpumask_of_cpu(smp_processor_id());
771 if (user_region && cpus_equal(mm->cpu_vm_mask, tmp))
772 local = 1;
773
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000774#ifdef CONFIG_HUGETLB_PAGE
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100775 /* Handle hugepage regions */
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +1000776 if (HPAGE_SHIFT && psize == mmu_huge_psize) {
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100777 DBG_LOW(" -> huge page !\n");
David Gibsoncbf52af2005-12-09 14:20:52 +1100778 return hash_huge_page(mm, access, ea, vsid, local, trap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 }
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000780#endif /* CONFIG_HUGETLB_PAGE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +1000782#ifndef CONFIG_PPC_64K_PAGES
783 /* If we use 4K pages and our psize is not 4K, then we are hitting
784 * a special driver mapping, we need to align the address before
785 * we fetch the PTE
786 */
787 if (psize != MMU_PAGE_4K)
788 ea &= ~((1ul << mmu_psize_defs[psize].shift) - 1);
789#endif /* CONFIG_PPC_64K_PAGES */
790
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100791 /* Get PTE and page size from page tables */
792 ptep = find_linux_pte(pgdir, ea);
793 if (ptep == NULL || !pte_present(*ptep)) {
794 DBG_LOW(" no PTE !\n");
795 return 1;
796 }
797
798#ifndef CONFIG_PPC_64K_PAGES
799 DBG_LOW(" i-pte: %016lx\n", pte_val(*ptep));
800#else
801 DBG_LOW(" i-pte: %016lx %016lx\n", pte_val(*ptep),
802 pte_val(*(ptep + PTRS_PER_PTE)));
803#endif
804 /* Pre-check access permissions (will be re-checked atomically
805 * in __hash_page_XX but this pre-check is a fast path
806 */
807 if (access & ~pte_val(*ptep)) {
808 DBG_LOW(" no access !\n");
809 return 1;
810 }
811
812 /* Do actual hashing */
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +1000813#ifdef CONFIG_PPC_64K_PAGES
Paul Mackerras721151d2007-04-03 21:24:02 +1000814 /* If _PAGE_4K_PFN is set, make sure this is a 4k segment */
815 if (pte_val(*ptep) & _PAGE_4K_PFN) {
816 demote_segment_4k(mm, ea);
817 psize = MMU_PAGE_4K;
818 }
819
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +1000820 /* If this PTE is non-cacheable and we have restrictions on
821 * using non cacheable large pages, then we switch to 4k
822 */
823 if (mmu_ci_restrictions && psize == MMU_PAGE_64K &&
824 (pte_val(*ptep) & _PAGE_NO_CACHE)) {
825 if (user_region) {
826 demote_segment_4k(mm, ea);
827 psize = MMU_PAGE_4K;
828 } else if (ea < VMALLOC_END) {
829 /*
830 * some driver did a non-cacheable mapping
831 * in vmalloc space, so switch vmalloc
832 * to 4k pages
833 */
834 printk(KERN_ALERT "Reducing vmalloc segment "
835 "to 4kB pages because of "
836 "non-cacheable mapping\n");
837 psize = mmu_vmalloc_psize = MMU_PAGE_4K;
Geert Uytterhoeven1e57ba82007-07-17 02:35:38 +1000838#ifdef CONFIG_SPU_BASE
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +0100839 spu_flush_all_slbs(mm);
840#endif
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000841 }
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +1000842 }
843 if (user_region) {
844 if (psize != get_paca()->context.user_psize) {
Benjamin Herrenschmidtf6ab0b92007-10-29 12:05:18 +1100845 get_paca()->context = mm->context;
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000846 slb_flush_and_rebolt();
847 }
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +1000848 } else if (get_paca()->vmalloc_sllp !=
849 mmu_psize_defs[mmu_vmalloc_psize].sllp) {
850 get_paca()->vmalloc_sllp =
851 mmu_psize_defs[mmu_vmalloc_psize].sllp;
Michael Neuling67439b72007-08-03 11:55:39 +1000852 slb_vmalloc_update();
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000853 }
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +1000854#endif /* CONFIG_PPC_64K_PAGES */
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +1000855
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +1000856#ifdef CONFIG_PPC_HAS_HASH_64K
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000857 if (psize == MMU_PAGE_64K)
Paul Mackerras1189be62007-10-11 20:37:10 +1000858 rc = __hash_page_64K(ea, access, vsid, ptep, trap, local, ssize);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100859 else
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +1000860#endif /* CONFIG_PPC_HAS_HASH_64K */
Paul Mackerrasfa282372008-01-24 08:35:13 +1100861 {
862 int spp = subpage_protection(pgdir, ea);
863 if (access & spp)
864 rc = -2;
865 else
866 rc = __hash_page_4K(ea, access, vsid, ptep, trap,
867 local, ssize, spp);
868 }
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100869
870#ifndef CONFIG_PPC_64K_PAGES
871 DBG_LOW(" o-pte: %016lx\n", pte_val(*ptep));
872#else
873 DBG_LOW(" o-pte: %016lx %016lx\n", pte_val(*ptep),
874 pte_val(*(ptep + PTRS_PER_PTE)));
875#endif
876 DBG_LOW(" -> rc=%d\n", rc);
877 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878}
Arnd Bergmann67207b92005-11-15 15:53:48 -0500879EXPORT_SYMBOL_GPL(hash_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100881void hash_preload(struct mm_struct *mm, unsigned long ea,
882 unsigned long access, unsigned long trap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883{
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100884 unsigned long vsid;
885 void *pgdir;
886 pte_t *ptep;
887 cpumask_t mask;
888 unsigned long flags;
889 int local = 0;
Paul Mackerras1189be62007-10-11 20:37:10 +1000890 int ssize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000892 BUG_ON(REGION_ID(ea) != USER_REGION_ID);
893
894#ifdef CONFIG_PPC_MM_SLICES
895 /* We only prefault standard pages for now */
Ilpo Järvinen2b02d132007-08-16 08:03:35 +1000896 if (unlikely(get_slice_psize(mm, ea) != mm->context.user_psize))
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100897 return;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000898#endif
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100899
900 DBG_LOW("hash_preload(mm=%p, mm->pgdir=%p, ea=%016lx, access=%lx,"
901 " trap=%lx\n", mm, mm->pgd, ea, access, trap);
902
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +1000903 /* Get Linux PTE if available */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100904 pgdir = mm->pgd;
905 if (pgdir == NULL)
906 return;
907 ptep = find_linux_pte(pgdir, ea);
908 if (!ptep)
909 return;
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +1000910
911#ifdef CONFIG_PPC_64K_PAGES
912 /* If either _PAGE_4K_PFN or _PAGE_NO_CACHE is set (and we are on
913 * a 64K kernel), then we don't preload, hash_page() will take
914 * care of it once we actually try to access the page.
915 * That way we don't have to duplicate all of the logic for segment
916 * page size demotion here
917 */
918 if (pte_val(*ptep) & (_PAGE_4K_PFN | _PAGE_NO_CACHE))
919 return;
920#endif /* CONFIG_PPC_64K_PAGES */
921
922 /* Get VSID */
Paul Mackerras1189be62007-10-11 20:37:10 +1000923 ssize = user_segment_size(ea);
924 vsid = get_vsid(mm->context.id, ea, ssize);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100925
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +1000926 /* Hash doesn't like irqs */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100927 local_irq_save(flags);
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +1000928
929 /* Is that local to this CPU ? */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100930 mask = cpumask_of_cpu(smp_processor_id());
931 if (cpus_equal(mm->cpu_vm_mask, mask))
932 local = 1;
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +1000933
934 /* Hash it in */
935#ifdef CONFIG_PPC_HAS_HASH_64K
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000936 if (mm->context.user_psize == MMU_PAGE_64K)
Paul Mackerras1189be62007-10-11 20:37:10 +1000937 __hash_page_64K(ea, access, vsid, ptep, trap, local, ssize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 else
Jon Tollefson5b825832007-05-17 04:43:02 +1000939#endif /* CONFIG_PPC_HAS_HASH_64K */
Paul Mackerrasfa282372008-01-24 08:35:13 +1100940 __hash_page_4K(ea, access, vsid, ptep, trap, local, ssize,
941 subpage_protection(pgdir, ea));
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +1000942
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100943 local_irq_restore(flags);
944}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Benjamin Herrenschmidtf6ab0b92007-10-29 12:05:18 +1100946/* WARNING: This is called from hash_low_64.S, if you change this prototype,
947 * do not forget to update the assembly call site !
948 */
Paul Mackerras1189be62007-10-11 20:37:10 +1000949void flush_hash_page(unsigned long va, real_pte_t pte, int psize, int ssize,
950 int local)
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100951{
952 unsigned long hash, index, shift, hidx, slot;
953
954 DBG_LOW("flush_hash_page(va=%016x)\n", va);
955 pte_iterate_hashed_subpages(pte, psize, va, index, shift) {
Paul Mackerras1189be62007-10-11 20:37:10 +1000956 hash = hpt_hash(va, shift, ssize);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100957 hidx = __rpte_to_hidx(pte, index);
958 if (hidx & _PTEIDX_SECONDARY)
959 hash = ~hash;
960 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
961 slot += hidx & _PTEIDX_GROUP_IX;
962 DBG_LOW(" sub %d: hash=%x, hidx=%x\n", index, slot, hidx);
Paul Mackerras1189be62007-10-11 20:37:10 +1000963 ppc_md.hpte_invalidate(slot, va, psize, ssize, local);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100964 } pte_iterate_hashed_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965}
966
Benjamin Herrenschmidt61b1a942005-09-20 13:52:50 +1000967void flush_hash_range(unsigned long number, int local)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968{
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100969 if (ppc_md.flush_hash_range)
Benjamin Herrenschmidt61b1a942005-09-20 13:52:50 +1000970 ppc_md.flush_hash_range(number, local);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100971 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 int i;
Benjamin Herrenschmidt61b1a942005-09-20 13:52:50 +1000973 struct ppc64_tlb_batch *batch =
974 &__get_cpu_var(ppc64_tlb_batch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
976 for (i = 0; i < number; i++)
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100977 flush_hash_page(batch->vaddr[i], batch->pte[i],
Paul Mackerras1189be62007-10-11 20:37:10 +1000978 batch->psize, batch->ssize, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 }
980}
981
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982/*
983 * low_hash_fault is called when we the low level hash code failed
984 * to instert a PTE due to an hypervisor error
985 */
Paul Mackerrasfa282372008-01-24 08:35:13 +1100986void low_hash_fault(struct pt_regs *regs, unsigned long address, int rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987{
988 if (user_mode(regs)) {
Paul Mackerrasfa282372008-01-24 08:35:13 +1100989#ifdef CONFIG_PPC_SUBPAGE_PROT
990 if (rc == -2)
991 _exception(SIGSEGV, regs, SEGV_ACCERR, address);
992 else
993#endif
994 _exception(SIGBUS, regs, BUS_ADRERR, address);
995 } else
996 bad_page_fault(regs, address, SIGBUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997}
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000998
999#ifdef CONFIG_DEBUG_PAGEALLOC
1000static void kernel_map_linear_page(unsigned long vaddr, unsigned long lmi)
1001{
Paul Mackerras1189be62007-10-11 20:37:10 +10001002 unsigned long hash, hpteg;
1003 unsigned long vsid = get_kernel_vsid(vaddr, mmu_kernel_ssize);
1004 unsigned long va = hpt_va(vaddr, vsid, mmu_kernel_ssize);
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +10001005 unsigned long mode = _PAGE_ACCESSED | _PAGE_DIRTY |
1006 _PAGE_COHERENT | PP_RWXX | HPTE_R_N;
1007 int ret;
1008
Paul Mackerras1189be62007-10-11 20:37:10 +10001009 hash = hpt_hash(va, PAGE_SHIFT, mmu_kernel_ssize);
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +10001010 hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
1011
1012 ret = ppc_md.hpte_insert(hpteg, va, __pa(vaddr),
Paul Mackerras1189be62007-10-11 20:37:10 +10001013 mode, HPTE_V_BOLTED,
1014 mmu_linear_psize, mmu_kernel_ssize);
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +10001015 BUG_ON (ret < 0);
1016 spin_lock(&linear_map_hash_lock);
1017 BUG_ON(linear_map_hash_slots[lmi] & 0x80);
1018 linear_map_hash_slots[lmi] = ret | 0x80;
1019 spin_unlock(&linear_map_hash_lock);
1020}
1021
1022static void kernel_unmap_linear_page(unsigned long vaddr, unsigned long lmi)
1023{
Paul Mackerras1189be62007-10-11 20:37:10 +10001024 unsigned long hash, hidx, slot;
1025 unsigned long vsid = get_kernel_vsid(vaddr, mmu_kernel_ssize);
1026 unsigned long va = hpt_va(vaddr, vsid, mmu_kernel_ssize);
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +10001027
Paul Mackerras1189be62007-10-11 20:37:10 +10001028 hash = hpt_hash(va, PAGE_SHIFT, mmu_kernel_ssize);
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +10001029 spin_lock(&linear_map_hash_lock);
1030 BUG_ON(!(linear_map_hash_slots[lmi] & 0x80));
1031 hidx = linear_map_hash_slots[lmi] & 0x7f;
1032 linear_map_hash_slots[lmi] = 0;
1033 spin_unlock(&linear_map_hash_lock);
1034 if (hidx & _PTEIDX_SECONDARY)
1035 hash = ~hash;
1036 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
1037 slot += hidx & _PTEIDX_GROUP_IX;
Paul Mackerras1189be62007-10-11 20:37:10 +10001038 ppc_md.hpte_invalidate(slot, va, mmu_linear_psize, mmu_kernel_ssize, 0);
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +10001039}
1040
1041void kernel_map_pages(struct page *page, int numpages, int enable)
1042{
1043 unsigned long flags, vaddr, lmi;
1044 int i;
1045
1046 local_irq_save(flags);
1047 for (i = 0; i < numpages; i++, page++) {
1048 vaddr = (unsigned long)page_address(page);
1049 lmi = __pa(vaddr) >> PAGE_SHIFT;
1050 if (lmi >= linear_map_hash_count)
1051 continue;
1052 if (enable)
1053 kernel_map_linear_page(vaddr, lmi);
1054 else
1055 kernel_unmap_linear_page(vaddr, lmi);
1056 }
1057 local_irq_restore(flags);
1058}
1059#endif /* CONFIG_DEBUG_PAGEALLOC */