blob: 09da90b5385045dac3b9348a507aea13b94120c7 [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>
34
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <asm/processor.h>
36#include <asm/pgtable.h>
37#include <asm/mmu.h>
38#include <asm/mmu_context.h>
39#include <asm/page.h>
40#include <asm/types.h>
41#include <asm/system.h>
42#include <asm/uaccess.h>
43#include <asm/machdep.h>
44#include <asm/lmb.h>
45#include <asm/abs_addr.h>
46#include <asm/tlbflush.h>
47#include <asm/io.h>
48#include <asm/eeh.h>
49#include <asm/tlb.h>
50#include <asm/cacheflush.h>
51#include <asm/cputable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <asm/sections.h>
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +100053#include <asm/spu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55#ifdef DEBUG
56#define DBG(fmt...) udbg_printf(fmt)
57#else
58#define DBG(fmt...)
59#endif
60
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +110061#ifdef DEBUG_LOW
62#define DBG_LOW(fmt...) udbg_printf(fmt)
63#else
64#define DBG_LOW(fmt...)
65#endif
66
67#define KB (1024)
68#define MB (1024*KB)
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070/*
71 * Note: pte --> Linux PTE
72 * HPTE --> PowerPC Hashed Page Table Entry
73 *
74 * Execution context:
75 * htab_initialize is called with the MMU off (of course), but
76 * the kernel has been copied down to zero so it can directly
77 * reference global data. At this point it is very difficult
78 * to print debug info.
79 *
80 */
81
82#ifdef CONFIG_U3_DART
83extern unsigned long dart_tablebase;
84#endif /* CONFIG_U3_DART */
85
Paul Mackerras799d6042005-11-10 13:37:51 +110086static unsigned long _SDR1;
87struct mmu_psize_def mmu_psize_defs[MMU_PAGE_COUNT];
88
David Gibson8e561e72007-06-13 14:52:56 +100089struct hash_pte *htab_address;
Michael Ellerman337a7122006-02-21 17:22:55 +110090unsigned long htab_size_bytes;
David Gibson96e28442005-07-13 01:11:42 -070091unsigned long htab_hash_mask;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +110092int mmu_linear_psize = MMU_PAGE_4K;
93int mmu_virtual_psize = MMU_PAGE_4K;
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +100094int mmu_vmalloc_psize = MMU_PAGE_4K;
95int mmu_io_psize = MMU_PAGE_4K;
Paul Mackerras1189be62007-10-11 20:37:10 +100096int mmu_kernel_ssize = MMU_SEGSIZE_256M;
97int mmu_highuser_ssize = MMU_SEGSIZE_256M;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +110098#ifdef CONFIG_HUGETLB_PAGE
99int mmu_huge_psize = MMU_PAGE_16M;
100unsigned int HPAGE_SHIFT;
101#endif
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000102#ifdef CONFIG_PPC_64K_PAGES
103int mmu_ci_restrictions;
104#endif
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000105#ifdef CONFIG_DEBUG_PAGEALLOC
106static u8 *linear_map_hash_slots;
107static unsigned long linear_map_hash_count;
Michael Ellermaned166692007-04-18 11:50:09 +1000108static DEFINE_SPINLOCK(linear_map_hash_lock);
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000109#endif /* CONFIG_DEBUG_PAGEALLOC */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100111/* There are definitions of page sizes arrays to be used when none
112 * is provided by the firmware.
113 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100115/* Pre-POWER4 CPUs (4k pages only)
116 */
117struct mmu_psize_def mmu_psize_defaults_old[] = {
118 [MMU_PAGE_4K] = {
119 .shift = 12,
120 .sllp = 0,
121 .penc = 0,
122 .avpnm = 0,
123 .tlbiel = 0,
124 },
125};
126
127/* POWER4, GPUL, POWER5
128 *
129 * Support for 16Mb large pages
130 */
131struct mmu_psize_def mmu_psize_defaults_gp[] = {
132 [MMU_PAGE_4K] = {
133 .shift = 12,
134 .sllp = 0,
135 .penc = 0,
136 .avpnm = 0,
137 .tlbiel = 1,
138 },
139 [MMU_PAGE_16M] = {
140 .shift = 24,
141 .sllp = SLB_VSID_L,
142 .penc = 0,
143 .avpnm = 0x1UL,
144 .tlbiel = 0,
145 },
146};
147
148
149int htab_bolt_mapping(unsigned long vstart, unsigned long vend,
Paul Mackerras1189be62007-10-11 20:37:10 +1000150 unsigned long pstart, unsigned long mode,
151 int psize, int ssize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100153 unsigned long vaddr, paddr;
154 unsigned int step, shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 unsigned long tmp_mode;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100156 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100158 shift = mmu_psize_defs[psize].shift;
159 step = 1 << shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100161 for (vaddr = vstart, paddr = pstart; vaddr < vend;
162 vaddr += step, paddr += step) {
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000163 unsigned long hash, hpteg;
Paul Mackerras1189be62007-10-11 20:37:10 +1000164 unsigned long vsid = get_kernel_vsid(vaddr, ssize);
165 unsigned long va = hpt_va(vaddr, vsid, ssize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 tmp_mode = mode;
168
169 /* Make non-kernel text non-executable */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100170 if (!in_kernel_text(vaddr))
171 tmp_mode = mode | HPTE_R_N;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Paul Mackerras1189be62007-10-11 20:37:10 +1000173 hash = hpt_hash(va, shift, ssize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
175
Michael Ellermanc30a4df2006-06-23 18:16:39 +1000176 DBG("htab_bolt_mapping: calling %p\n", ppc_md.hpte_insert);
177
178 BUG_ON(!ppc_md.hpte_insert);
179 ret = ppc_md.hpte_insert(hpteg, va, paddr,
Paul Mackerras1189be62007-10-11 20:37:10 +1000180 tmp_mode, HPTE_V_BOLTED, psize, ssize);
Michael Ellermanc30a4df2006-06-23 18:16:39 +1000181
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100182 if (ret < 0)
183 break;
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000184#ifdef CONFIG_DEBUG_PAGEALLOC
185 if ((paddr >> PAGE_SHIFT) < linear_map_hash_count)
186 linear_map_hash_slots[paddr >> PAGE_SHIFT] = ret | 0x80;
187#endif /* CONFIG_DEBUG_PAGEALLOC */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 }
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100189 return ret < 0 ? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190}
191
Paul Mackerras1189be62007-10-11 20:37:10 +1000192static int __init htab_dt_scan_seg_sizes(unsigned long node,
193 const char *uname, int depth,
194 void *data)
195{
196 char *type = of_get_flat_dt_prop(node, "device_type", NULL);
197 u32 *prop;
198 unsigned long size = 0;
199
200 /* We are scanning "cpu" nodes only */
201 if (type == NULL || strcmp(type, "cpu") != 0)
202 return 0;
203
204 prop = (u32 *)of_get_flat_dt_prop(node, "ibm,processor-segment-sizes",
205 &size);
206 if (prop == NULL)
207 return 0;
208 for (; size >= 4; size -= 4, ++prop) {
209 if (prop[0] == 40) {
210 DBG("1T segment support detected\n");
211 cur_cpu_spec->cpu_features |= CPU_FTR_1T_SEGMENT;
Olof Johanssonf5534002007-10-12 16:44:55 +1000212 return 1;
Paul Mackerras1189be62007-10-11 20:37:10 +1000213 }
Paul Mackerras1189be62007-10-11 20:37:10 +1000214 }
215 return 0;
216}
217
218static void __init htab_init_seg_sizes(void)
219{
220 of_scan_flat_dt(htab_dt_scan_seg_sizes, NULL);
221}
222
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100223static int __init htab_dt_scan_page_sizes(unsigned long node,
224 const char *uname, int depth,
225 void *data)
226{
227 char *type = of_get_flat_dt_prop(node, "device_type", NULL);
228 u32 *prop;
229 unsigned long size = 0;
230
231 /* We are scanning "cpu" nodes only */
232 if (type == NULL || strcmp(type, "cpu") != 0)
233 return 0;
234
235 prop = (u32 *)of_get_flat_dt_prop(node,
236 "ibm,segment-page-sizes", &size);
237 if (prop != NULL) {
238 DBG("Page sizes from device-tree:\n");
239 size /= 4;
240 cur_cpu_spec->cpu_features &= ~(CPU_FTR_16M_PAGE);
241 while(size > 0) {
242 unsigned int shift = prop[0];
243 unsigned int slbenc = prop[1];
244 unsigned int lpnum = prop[2];
245 unsigned int lpenc = 0;
246 struct mmu_psize_def *def;
247 int idx = -1;
248
249 size -= 3; prop += 3;
250 while(size > 0 && lpnum) {
251 if (prop[0] == shift)
252 lpenc = prop[1];
253 prop += 2; size -= 2;
254 lpnum--;
255 }
256 switch(shift) {
257 case 0xc:
258 idx = MMU_PAGE_4K;
259 break;
260 case 0x10:
261 idx = MMU_PAGE_64K;
262 break;
263 case 0x14:
264 idx = MMU_PAGE_1M;
265 break;
266 case 0x18:
267 idx = MMU_PAGE_16M;
268 cur_cpu_spec->cpu_features |= CPU_FTR_16M_PAGE;
269 break;
270 case 0x22:
271 idx = MMU_PAGE_16G;
272 break;
273 }
274 if (idx < 0)
275 continue;
276 def = &mmu_psize_defs[idx];
277 def->shift = shift;
278 if (shift <= 23)
279 def->avpnm = 0;
280 else
281 def->avpnm = (1 << (shift - 23)) - 1;
282 def->sllp = slbenc;
283 def->penc = lpenc;
284 /* We don't know for sure what's up with tlbiel, so
285 * for now we only set it for 4K and 64K pages
286 */
287 if (idx == MMU_PAGE_4K || idx == MMU_PAGE_64K)
288 def->tlbiel = 1;
289 else
290 def->tlbiel = 0;
291
292 DBG(" %d: shift=%02x, sllp=%04x, avpnm=%08x, "
293 "tlbiel=%d, penc=%d\n",
294 idx, shift, def->sllp, def->avpnm, def->tlbiel,
295 def->penc);
296 }
297 return 1;
298 }
299 return 0;
300}
301
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100302static void __init htab_init_page_sizes(void)
303{
304 int rc;
305
306 /* Default to 4K pages only */
307 memcpy(mmu_psize_defs, mmu_psize_defaults_old,
308 sizeof(mmu_psize_defaults_old));
309
310 /*
311 * Try to find the available page sizes in the device-tree
312 */
313 rc = of_scan_flat_dt(htab_dt_scan_page_sizes, NULL);
314 if (rc != 0) /* Found */
315 goto found;
316
317 /*
318 * Not in the device-tree, let's fallback on known size
319 * list for 16M capable GP & GR
320 */
Stephen Rothwell04704662006-11-30 11:46:22 +1100321 if (cpu_has_feature(CPU_FTR_16M_PAGE))
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100322 memcpy(mmu_psize_defs, mmu_psize_defaults_gp,
323 sizeof(mmu_psize_defaults_gp));
324 found:
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000325#ifndef CONFIG_DEBUG_PAGEALLOC
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100326 /*
327 * Pick a size for the linear mapping. Currently, we only support
328 * 16M, 1M and 4K which is the default
329 */
330 if (mmu_psize_defs[MMU_PAGE_16M].shift)
331 mmu_linear_psize = MMU_PAGE_16M;
332 else if (mmu_psize_defs[MMU_PAGE_1M].shift)
333 mmu_linear_psize = MMU_PAGE_1M;
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000334#endif /* CONFIG_DEBUG_PAGEALLOC */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100335
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000336#ifdef CONFIG_PPC_64K_PAGES
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100337 /*
338 * Pick a size for the ordinary pages. Default is 4K, we support
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000339 * 64K for user mappings and vmalloc if supported by the processor.
340 * We only use 64k for ioremap if the processor
341 * (and firmware) support cache-inhibited large pages.
342 * If not, we use 4k and set mmu_ci_restrictions so that
343 * hash_page knows to switch processes that use cache-inhibited
344 * mappings to 4k pages.
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100345 */
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000346 if (mmu_psize_defs[MMU_PAGE_64K].shift) {
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100347 mmu_virtual_psize = MMU_PAGE_64K;
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000348 mmu_vmalloc_psize = MMU_PAGE_64K;
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000349 if (mmu_linear_psize == MMU_PAGE_4K)
350 mmu_linear_psize = MMU_PAGE_64K;
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000351 if (cpu_has_feature(CPU_FTR_CI_LARGE_PAGE))
352 mmu_io_psize = MMU_PAGE_64K;
353 else
354 mmu_ci_restrictions = 1;
355 }
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000356#endif /* CONFIG_PPC_64K_PAGES */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100357
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000358 printk(KERN_DEBUG "Page orders: linear mapping = %d, "
359 "virtual = %d, io = %d\n",
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100360 mmu_psize_defs[mmu_linear_psize].shift,
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000361 mmu_psize_defs[mmu_virtual_psize].shift,
362 mmu_psize_defs[mmu_io_psize].shift);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100363
364#ifdef CONFIG_HUGETLB_PAGE
365 /* Init large page size. Currently, we pick 16M or 1M depending
366 * on what is available
367 */
368 if (mmu_psize_defs[MMU_PAGE_16M].shift)
369 mmu_huge_psize = MMU_PAGE_16M;
David Gibson7d24f0b2005-11-07 00:57:52 -0800370 /* With 4k/4level pagetables, we can't (for now) cope with a
371 * huge page size < PMD_SIZE */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100372 else if (mmu_psize_defs[MMU_PAGE_1M].shift)
373 mmu_huge_psize = MMU_PAGE_1M;
374
375 /* Calculate HPAGE_SHIFT and sanity check it */
David Gibson7d24f0b2005-11-07 00:57:52 -0800376 if (mmu_psize_defs[mmu_huge_psize].shift > MIN_HUGEPTE_SHIFT &&
377 mmu_psize_defs[mmu_huge_psize].shift < SID_SHIFT)
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100378 HPAGE_SHIFT = mmu_psize_defs[mmu_huge_psize].shift;
379 else
380 HPAGE_SHIFT = 0; /* No huge pages dude ! */
381#endif /* CONFIG_HUGETLB_PAGE */
382}
383
384static int __init htab_dt_scan_pftsize(unsigned long node,
385 const char *uname, int depth,
386 void *data)
387{
388 char *type = of_get_flat_dt_prop(node, "device_type", NULL);
389 u32 *prop;
390
391 /* We are scanning "cpu" nodes only */
392 if (type == NULL || strcmp(type, "cpu") != 0)
393 return 0;
394
395 prop = (u32 *)of_get_flat_dt_prop(node, "ibm,pft-size", NULL);
396 if (prop != NULL) {
397 /* pft_size[0] is the NUMA CEC cookie */
398 ppc64_pft_size = prop[1];
399 return 1;
400 }
401 return 0;
402}
403
404static unsigned long __init htab_get_table_size(void)
Paul Mackerras3eac8c62005-10-12 16:58:53 +1000405{
Paul Mackerras799d6042005-11-10 13:37:51 +1100406 unsigned long mem_size, rnd_mem_size, pteg_count;
Paul Mackerras3eac8c62005-10-12 16:58:53 +1000407
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100408 /* If hash size isn't already provided by the platform, we try to
Adrian Bunk943ffb52006-01-10 00:10:13 +0100409 * retrieve it from the device-tree. If it's not there neither, we
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100410 * calculate it now based on the total RAM size
Paul Mackerras3eac8c62005-10-12 16:58:53 +1000411 */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100412 if (ppc64_pft_size == 0)
413 of_scan_flat_dt(htab_dt_scan_pftsize, NULL);
Paul Mackerras3eac8c62005-10-12 16:58:53 +1000414 if (ppc64_pft_size)
415 return 1UL << ppc64_pft_size;
416
417 /* round mem_size up to next power of 2 */
Paul Mackerras799d6042005-11-10 13:37:51 +1100418 mem_size = lmb_phys_mem_size();
419 rnd_mem_size = 1UL << __ilog2(mem_size);
420 if (rnd_mem_size < mem_size)
Paul Mackerras3eac8c62005-10-12 16:58:53 +1000421 rnd_mem_size <<= 1;
422
423 /* # pages / 2 */
424 pteg_count = max(rnd_mem_size >> (12 + 1), 1UL << 11);
425
426 return pteg_count << 7;
427}
428
Mike Kravetz54b79242005-11-07 16:25:48 -0800429#ifdef CONFIG_MEMORY_HOTPLUG
430void create_section_mapping(unsigned long start, unsigned long end)
431{
Michael Ellermancaf80e52006-03-21 20:45:51 +1100432 BUG_ON(htab_bolt_mapping(start, end, __pa(start),
Mike Kravetz54b79242005-11-07 16:25:48 -0800433 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_COHERENT | PP_RWXX,
Paul Mackerras1189be62007-10-11 20:37:10 +1000434 mmu_linear_psize, mmu_kernel_ssize));
Mike Kravetz54b79242005-11-07 16:25:48 -0800435}
436#endif /* CONFIG_MEMORY_HOTPLUG */
437
Michael Ellerman7d0daae2006-06-23 18:16:38 +1000438static inline void make_bl(unsigned int *insn_addr, void *func)
439{
440 unsigned long funcp = *((unsigned long *)func);
441 int offset = funcp - (unsigned long)insn_addr;
442
443 *insn_addr = (unsigned int)(0x48000001 | (offset & 0x03fffffc));
444 flush_icache_range((unsigned long)insn_addr, 4+
445 (unsigned long)insn_addr);
446}
447
448static void __init htab_finish_init(void)
449{
450 extern unsigned int *htab_call_hpte_insert1;
451 extern unsigned int *htab_call_hpte_insert2;
452 extern unsigned int *htab_call_hpte_remove;
453 extern unsigned int *htab_call_hpte_updatepp;
454
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +1000455#ifdef CONFIG_PPC_HAS_HASH_64K
Michael Ellerman7d0daae2006-06-23 18:16:38 +1000456 extern unsigned int *ht64_call_hpte_insert1;
457 extern unsigned int *ht64_call_hpte_insert2;
458 extern unsigned int *ht64_call_hpte_remove;
459 extern unsigned int *ht64_call_hpte_updatepp;
460
461 make_bl(ht64_call_hpte_insert1, ppc_md.hpte_insert);
462 make_bl(ht64_call_hpte_insert2, ppc_md.hpte_insert);
463 make_bl(ht64_call_hpte_remove, ppc_md.hpte_remove);
464 make_bl(ht64_call_hpte_updatepp, ppc_md.hpte_updatepp);
Jon Tollefson5b825832007-05-17 04:43:02 +1000465#endif /* CONFIG_PPC_HAS_HASH_64K */
Michael Ellerman7d0daae2006-06-23 18:16:38 +1000466
467 make_bl(htab_call_hpte_insert1, ppc_md.hpte_insert);
468 make_bl(htab_call_hpte_insert2, ppc_md.hpte_insert);
469 make_bl(htab_call_hpte_remove, ppc_md.hpte_remove);
470 make_bl(htab_call_hpte_updatepp, ppc_md.hpte_updatepp);
471}
472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473void __init htab_initialize(void)
474{
Michael Ellerman337a7122006-02-21 17:22:55 +1100475 unsigned long table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 unsigned long pteg_count;
477 unsigned long mode_rw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 unsigned long base = 0, size = 0;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100479 int i;
480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 extern unsigned long tce_alloc_start, tce_alloc_end;
482
483 DBG(" -> htab_initialize()\n");
484
Paul Mackerras1189be62007-10-11 20:37:10 +1000485 /* Initialize segment sizes */
486 htab_init_seg_sizes();
487
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100488 /* Initialize page sizes */
489 htab_init_page_sizes();
490
Paul Mackerras1189be62007-10-11 20:37:10 +1000491 if (cpu_has_feature(CPU_FTR_1T_SEGMENT)) {
492 mmu_kernel_ssize = MMU_SEGSIZE_1T;
493 mmu_highuser_ssize = MMU_SEGSIZE_1T;
494 printk(KERN_INFO "Using 1TB segments\n");
495 }
496
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 /*
498 * Calculate the required size of the htab. We want the number of
499 * PTEGs to equal one half the number of real pages.
500 */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100501 htab_size_bytes = htab_get_table_size();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 pteg_count = htab_size_bytes >> 7;
503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 htab_hash_mask = pteg_count - 1;
505
Michael Ellerman57cfb812006-03-21 20:45:59 +1100506 if (firmware_has_feature(FW_FEATURE_LPAR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 /* Using a hypervisor which owns the htab */
508 htab_address = NULL;
509 _SDR1 = 0;
510 } else {
511 /* Find storage for the HPT. Must be contiguous in
512 * the absolute address space.
513 */
514 table = lmb_alloc(htab_size_bytes, htab_size_bytes);
515
516 DBG("Hash table allocated at %lx, size: %lx\n", table,
517 htab_size_bytes);
518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 htab_address = abs_to_virt(table);
520
521 /* htab absolute addr + encoded htabsize */
522 _SDR1 = table + __ilog2(pteg_count) - 11;
523
524 /* Initialize the HPT with no entries */
525 memset((void *)table, 0, htab_size_bytes);
Paul Mackerras799d6042005-11-10 13:37:51 +1100526
527 /* Set SDR1 */
528 mtspr(SPRN_SDR1, _SDR1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 }
530
Anton Blanchard515bae92005-06-21 17:15:55 -0700531 mode_rw = _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_COHERENT | PP_RWXX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000533#ifdef CONFIG_DEBUG_PAGEALLOC
534 linear_map_hash_count = lmb_end_of_DRAM() >> PAGE_SHIFT;
535 linear_map_hash_slots = __va(lmb_alloc_base(linear_map_hash_count,
536 1, lmb.rmo_size));
537 memset(linear_map_hash_slots, 0, linear_map_hash_count);
538#endif /* CONFIG_DEBUG_PAGEALLOC */
539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 /* On U3 based machines, we need to reserve the DART area and
541 * _NOT_ map it to avoid cache paradoxes as it's remapped non
542 * cacheable later on
543 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545 /* create bolted the linear mapping in the hash table */
546 for (i=0; i < lmb.memory.cnt; i++) {
Michael Ellermanb5666f72005-12-05 10:24:33 -0600547 base = (unsigned long)__va(lmb.memory.region[i].base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 size = lmb.memory.region[i].size;
549
550 DBG("creating mapping for region: %lx : %lx\n", base, size);
551
552#ifdef CONFIG_U3_DART
553 /* Do not map the DART space. Fortunately, it will be aligned
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100554 * in such a way that it will not cross two lmb regions and
555 * will fit within a single 16Mb page.
556 * The DART space is assumed to be a full 16Mb region even if
557 * we only use 2Mb of that space. We will use more of it later
558 * for AGP GART. We have to use a full 16Mb large page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 */
560 DBG("DART base: %lx\n", dart_tablebase);
561
562 if (dart_tablebase != 0 && dart_tablebase >= base
563 && dart_tablebase < (base + size)) {
Michael Ellermancaf80e52006-03-21 20:45:51 +1100564 unsigned long dart_table_end = dart_tablebase + 16 * MB;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 if (base != dart_tablebase)
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100566 BUG_ON(htab_bolt_mapping(base, dart_tablebase,
Michael Ellermancaf80e52006-03-21 20:45:51 +1100567 __pa(base), mode_rw,
Paul Mackerras1189be62007-10-11 20:37:10 +1000568 mmu_linear_psize,
569 mmu_kernel_ssize));
Michael Ellermancaf80e52006-03-21 20:45:51 +1100570 if ((base + size) > dart_table_end)
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100571 BUG_ON(htab_bolt_mapping(dart_tablebase+16*MB,
Michael Ellermancaf80e52006-03-21 20:45:51 +1100572 base + size,
573 __pa(dart_table_end),
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100574 mode_rw,
Paul Mackerras1189be62007-10-11 20:37:10 +1000575 mmu_linear_psize,
576 mmu_kernel_ssize));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 continue;
578 }
579#endif /* CONFIG_U3_DART */
Michael Ellermancaf80e52006-03-21 20:45:51 +1100580 BUG_ON(htab_bolt_mapping(base, base + size, __pa(base),
Paul Mackerras1189be62007-10-11 20:37:10 +1000581 mode_rw, mmu_linear_psize, mmu_kernel_ssize));
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100582 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
584 /*
585 * If we have a memory_limit and we've allocated TCEs then we need to
586 * explicitly map the TCE area at the top of RAM. We also cope with the
587 * case that the TCEs start below memory_limit.
588 * tce_alloc_start/end are 16MB aligned so the mapping should work
589 * for either 4K or 16MB pages.
590 */
591 if (tce_alloc_start) {
Michael Ellermanb5666f72005-12-05 10:24:33 -0600592 tce_alloc_start = (unsigned long)__va(tce_alloc_start);
593 tce_alloc_end = (unsigned long)__va(tce_alloc_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595 if (base + size >= tce_alloc_start)
596 tce_alloc_start = base + size + 1;
597
Michael Ellermancaf80e52006-03-21 20:45:51 +1100598 BUG_ON(htab_bolt_mapping(tce_alloc_start, tce_alloc_end,
599 __pa(tce_alloc_start), mode_rw,
Paul Mackerras1189be62007-10-11 20:37:10 +1000600 mmu_linear_psize, mmu_kernel_ssize));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 }
602
Michael Ellerman7d0daae2006-06-23 18:16:38 +1000603 htab_finish_init();
604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 DBG(" <- htab_initialize()\n");
606}
607#undef KB
608#undef MB
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Anton Blancharde597cb322005-12-29 10:46:29 +1100610void htab_initialize_secondary(void)
Paul Mackerras799d6042005-11-10 13:37:51 +1100611{
Michael Ellerman57cfb812006-03-21 20:45:59 +1100612 if (!firmware_has_feature(FW_FEATURE_LPAR))
Paul Mackerras799d6042005-11-10 13:37:51 +1100613 mtspr(SPRN_SDR1, _SDR1);
614}
615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616/*
617 * Called by asm hashtable.S for doing lazy icache flush
618 */
619unsigned int hash_page_do_lazy_icache(unsigned int pp, pte_t pte, int trap)
620{
621 struct page *page;
622
Benjamin Herrenschmidt76c8e252005-11-08 11:21:05 +1100623 if (!pfn_valid(pte_pfn(pte)))
624 return pp;
625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 page = pte_page(pte);
627
628 /* page is dirty */
629 if (!test_bit(PG_arch_1, &page->flags) && !PageReserved(page)) {
630 if (trap == 0x400) {
631 __flush_dcache_icache(page_address(page));
632 set_bit(PG_arch_1, &page->flags);
633 } else
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100634 pp |= HPTE_R_N;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 }
636 return pp;
637}
638
Paul Mackerras721151d2007-04-03 21:24:02 +1000639/*
640 * Demote a segment to using 4k pages.
641 * For now this makes the whole process use 4k pages.
642 */
Paul Mackerras721151d2007-04-03 21:24:02 +1000643#ifdef CONFIG_PPC_64K_PAGES
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +1000644static void demote_segment_4k(struct mm_struct *mm, unsigned long addr)
645{
Paul Mackerras721151d2007-04-03 21:24:02 +1000646 if (mm->context.user_psize == MMU_PAGE_4K)
647 return;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000648 slice_set_user_psize(mm, MMU_PAGE_4K);
Geert Uytterhoeven1e57ba82007-07-17 02:35:38 +1000649#ifdef CONFIG_SPU_BASE
Paul Mackerras721151d2007-04-03 21:24:02 +1000650 spu_flush_all_slbs(mm);
651#endif
Paul Mackerras721151d2007-04-03 21:24:02 +1000652}
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +1000653#endif /* CONFIG_PPC_64K_PAGES */
Paul Mackerras721151d2007-04-03 21:24:02 +1000654
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655/* Result code is:
656 * 0 - handled
657 * 1 - normal page fault
658 * -1 - critical hash insertion error
659 */
660int hash_page(unsigned long ea, unsigned long access, unsigned long trap)
661{
662 void *pgdir;
663 unsigned long vsid;
664 struct mm_struct *mm;
665 pte_t *ptep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 cpumask_t tmp;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100667 int rc, user_region = 0, local = 0;
Paul Mackerras1189be62007-10-11 20:37:10 +1000668 int psize, ssize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100670 DBG_LOW("hash_page(ea=%016lx, access=%lx, trap=%lx\n",
671 ea, access, trap);
David Gibson1f8d4192005-05-05 16:15:13 -0700672
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100673 if ((ea & ~REGION_MASK) >= PGTABLE_RANGE) {
674 DBG_LOW(" out of pgtable range !\n");
675 return 1;
676 }
677
678 /* Get region & vsid */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 switch (REGION_ID(ea)) {
680 case USER_REGION_ID:
681 user_region = 1;
682 mm = current->mm;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100683 if (! mm) {
684 DBG_LOW(" user region with no mm !\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 return 1;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100686 }
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +1000687#ifdef CONFIG_PPC_MM_SLICES
688 psize = get_slice_psize(mm, ea);
689#else
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000690 psize = mm->context.user_psize;
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +1000691#endif
Paul Mackerras1189be62007-10-11 20:37:10 +1000692 ssize = user_segment_size(ea);
693 vsid = get_vsid(mm->context.id, ea, ssize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 case VMALLOC_REGION_ID:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 mm = &init_mm;
Paul Mackerras1189be62007-10-11 20:37:10 +1000697 vsid = get_kernel_vsid(ea, mmu_kernel_ssize);
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000698 if (ea < VMALLOC_END)
699 psize = mmu_vmalloc_psize;
700 else
701 psize = mmu_io_psize;
Paul Mackerras1189be62007-10-11 20:37:10 +1000702 ssize = mmu_kernel_ssize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 default:
705 /* Not a valid range
706 * Send the problem up to do_page_fault
707 */
708 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 }
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100710 DBG_LOW(" mm=%p, mm->pgdir=%p, vsid=%016lx\n", mm, mm->pgd, vsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100712 /* Get pgdir */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 pgdir = mm->pgd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 if (pgdir == NULL)
715 return 1;
716
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100717 /* Check CPU locality */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 tmp = cpumask_of_cpu(smp_processor_id());
719 if (user_region && cpus_equal(mm->cpu_vm_mask, tmp))
720 local = 1;
721
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000722#ifdef CONFIG_HUGETLB_PAGE
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100723 /* Handle hugepage regions */
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +1000724 if (HPAGE_SHIFT && psize == mmu_huge_psize) {
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100725 DBG_LOW(" -> huge page !\n");
David Gibsoncbf52af2005-12-09 14:20:52 +1100726 return hash_huge_page(mm, access, ea, vsid, local, trap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 }
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000728#endif /* CONFIG_HUGETLB_PAGE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +1000730#ifndef CONFIG_PPC_64K_PAGES
731 /* If we use 4K pages and our psize is not 4K, then we are hitting
732 * a special driver mapping, we need to align the address before
733 * we fetch the PTE
734 */
735 if (psize != MMU_PAGE_4K)
736 ea &= ~((1ul << mmu_psize_defs[psize].shift) - 1);
737#endif /* CONFIG_PPC_64K_PAGES */
738
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100739 /* Get PTE and page size from page tables */
740 ptep = find_linux_pte(pgdir, ea);
741 if (ptep == NULL || !pte_present(*ptep)) {
742 DBG_LOW(" no PTE !\n");
743 return 1;
744 }
745
746#ifndef CONFIG_PPC_64K_PAGES
747 DBG_LOW(" i-pte: %016lx\n", pte_val(*ptep));
748#else
749 DBG_LOW(" i-pte: %016lx %016lx\n", pte_val(*ptep),
750 pte_val(*(ptep + PTRS_PER_PTE)));
751#endif
752 /* Pre-check access permissions (will be re-checked atomically
753 * in __hash_page_XX but this pre-check is a fast path
754 */
755 if (access & ~pte_val(*ptep)) {
756 DBG_LOW(" no access !\n");
757 return 1;
758 }
759
760 /* Do actual hashing */
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +1000761#ifdef CONFIG_PPC_64K_PAGES
Paul Mackerras721151d2007-04-03 21:24:02 +1000762 /* If _PAGE_4K_PFN is set, make sure this is a 4k segment */
763 if (pte_val(*ptep) & _PAGE_4K_PFN) {
764 demote_segment_4k(mm, ea);
765 psize = MMU_PAGE_4K;
766 }
767
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +1000768 /* If this PTE is non-cacheable and we have restrictions on
769 * using non cacheable large pages, then we switch to 4k
770 */
771 if (mmu_ci_restrictions && psize == MMU_PAGE_64K &&
772 (pte_val(*ptep) & _PAGE_NO_CACHE)) {
773 if (user_region) {
774 demote_segment_4k(mm, ea);
775 psize = MMU_PAGE_4K;
776 } else if (ea < VMALLOC_END) {
777 /*
778 * some driver did a non-cacheable mapping
779 * in vmalloc space, so switch vmalloc
780 * to 4k pages
781 */
782 printk(KERN_ALERT "Reducing vmalloc segment "
783 "to 4kB pages because of "
784 "non-cacheable mapping\n");
785 psize = mmu_vmalloc_psize = MMU_PAGE_4K;
Geert Uytterhoeven1e57ba82007-07-17 02:35:38 +1000786#ifdef CONFIG_SPU_BASE
Benjamin Herrenschmidt94b2a432007-03-10 00:05:37 +0100787 spu_flush_all_slbs(mm);
788#endif
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000789 }
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +1000790 }
791 if (user_region) {
792 if (psize != get_paca()->context.user_psize) {
793 get_paca()->context.user_psize =
794 mm->context.user_psize;
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000795 slb_flush_and_rebolt();
796 }
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +1000797 } else if (get_paca()->vmalloc_sllp !=
798 mmu_psize_defs[mmu_vmalloc_psize].sllp) {
799 get_paca()->vmalloc_sllp =
800 mmu_psize_defs[mmu_vmalloc_psize].sllp;
Michael Neuling67439b72007-08-03 11:55:39 +1000801 slb_vmalloc_update();
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000802 }
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +1000803#endif /* CONFIG_PPC_64K_PAGES */
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +1000804
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +1000805#ifdef CONFIG_PPC_HAS_HASH_64K
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000806 if (psize == MMU_PAGE_64K)
Paul Mackerras1189be62007-10-11 20:37:10 +1000807 rc = __hash_page_64K(ea, access, vsid, ptep, trap, local, ssize);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100808 else
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +1000809#endif /* CONFIG_PPC_HAS_HASH_64K */
Paul Mackerras1189be62007-10-11 20:37:10 +1000810 rc = __hash_page_4K(ea, access, vsid, ptep, trap, local, ssize);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100811
812#ifndef CONFIG_PPC_64K_PAGES
813 DBG_LOW(" o-pte: %016lx\n", pte_val(*ptep));
814#else
815 DBG_LOW(" o-pte: %016lx %016lx\n", pte_val(*ptep),
816 pte_val(*(ptep + PTRS_PER_PTE)));
817#endif
818 DBG_LOW(" -> rc=%d\n", rc);
819 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820}
Arnd Bergmann67207b92005-11-15 15:53:48 -0500821EXPORT_SYMBOL_GPL(hash_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100823void hash_preload(struct mm_struct *mm, unsigned long ea,
824 unsigned long access, unsigned long trap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100826 unsigned long vsid;
827 void *pgdir;
828 pte_t *ptep;
829 cpumask_t mask;
830 unsigned long flags;
831 int local = 0;
Paul Mackerras1189be62007-10-11 20:37:10 +1000832 int ssize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000834 BUG_ON(REGION_ID(ea) != USER_REGION_ID);
835
836#ifdef CONFIG_PPC_MM_SLICES
837 /* We only prefault standard pages for now */
Ilpo Järvinen2b02d132007-08-16 08:03:35 +1000838 if (unlikely(get_slice_psize(mm, ea) != mm->context.user_psize))
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100839 return;
Benjamin Herrenschmidtd0f13e32007-05-08 16:27:27 +1000840#endif
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100841
842 DBG_LOW("hash_preload(mm=%p, mm->pgdir=%p, ea=%016lx, access=%lx,"
843 " trap=%lx\n", mm, mm->pgd, ea, access, trap);
844
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +1000845 /* Get Linux PTE if available */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100846 pgdir = mm->pgd;
847 if (pgdir == NULL)
848 return;
849 ptep = find_linux_pte(pgdir, ea);
850 if (!ptep)
851 return;
Benjamin Herrenschmidt16f1c742007-05-08 16:27:27 +1000852
853#ifdef CONFIG_PPC_64K_PAGES
854 /* If either _PAGE_4K_PFN or _PAGE_NO_CACHE is set (and we are on
855 * a 64K kernel), then we don't preload, hash_page() will take
856 * care of it once we actually try to access the page.
857 * That way we don't have to duplicate all of the logic for segment
858 * page size demotion here
859 */
860 if (pte_val(*ptep) & (_PAGE_4K_PFN | _PAGE_NO_CACHE))
861 return;
862#endif /* CONFIG_PPC_64K_PAGES */
863
864 /* Get VSID */
Paul Mackerras1189be62007-10-11 20:37:10 +1000865 ssize = user_segment_size(ea);
866 vsid = get_vsid(mm->context.id, ea, ssize);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100867
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +1000868 /* Hash doesn't like irqs */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100869 local_irq_save(flags);
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +1000870
871 /* Is that local to this CPU ? */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100872 mask = cpumask_of_cpu(smp_processor_id());
873 if (cpus_equal(mm->cpu_vm_mask, mask))
874 local = 1;
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +1000875
876 /* Hash it in */
877#ifdef CONFIG_PPC_HAS_HASH_64K
Paul Mackerrasbf72aeb2006-06-15 10:45:18 +1000878 if (mm->context.user_psize == MMU_PAGE_64K)
Paul Mackerras1189be62007-10-11 20:37:10 +1000879 __hash_page_64K(ea, access, vsid, ptep, trap, local, ssize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 else
Jon Tollefson5b825832007-05-17 04:43:02 +1000881#endif /* CONFIG_PPC_HAS_HASH_64K */
Paul Mackerras1189be62007-10-11 20:37:10 +1000882 __hash_page_4K(ea, access, vsid, ptep, trap, local, ssize);
Benjamin Herrenschmidt16c2d472007-05-08 16:27:28 +1000883
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100884 local_irq_restore(flags);
885}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Paul Mackerras1189be62007-10-11 20:37:10 +1000887void flush_hash_page(unsigned long va, real_pte_t pte, int psize, int ssize,
888 int local)
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100889{
890 unsigned long hash, index, shift, hidx, slot;
891
892 DBG_LOW("flush_hash_page(va=%016x)\n", va);
893 pte_iterate_hashed_subpages(pte, psize, va, index, shift) {
Paul Mackerras1189be62007-10-11 20:37:10 +1000894 hash = hpt_hash(va, shift, ssize);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100895 hidx = __rpte_to_hidx(pte, index);
896 if (hidx & _PTEIDX_SECONDARY)
897 hash = ~hash;
898 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
899 slot += hidx & _PTEIDX_GROUP_IX;
900 DBG_LOW(" sub %d: hash=%x, hidx=%x\n", index, slot, hidx);
Paul Mackerras1189be62007-10-11 20:37:10 +1000901 ppc_md.hpte_invalidate(slot, va, psize, ssize, local);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100902 } pte_iterate_hashed_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903}
904
Benjamin Herrenschmidt61b1a942005-09-20 13:52:50 +1000905void flush_hash_range(unsigned long number, int local)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100907 if (ppc_md.flush_hash_range)
Benjamin Herrenschmidt61b1a942005-09-20 13:52:50 +1000908 ppc_md.flush_hash_range(number, local);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100909 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 int i;
Benjamin Herrenschmidt61b1a942005-09-20 13:52:50 +1000911 struct ppc64_tlb_batch *batch =
912 &__get_cpu_var(ppc64_tlb_batch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
914 for (i = 0; i < number; i++)
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100915 flush_hash_page(batch->vaddr[i], batch->pte[i],
Paul Mackerras1189be62007-10-11 20:37:10 +1000916 batch->psize, batch->ssize, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 }
918}
919
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920/*
921 * low_hash_fault is called when we the low level hash code failed
922 * to instert a PTE due to an hypervisor error
923 */
924void low_hash_fault(struct pt_regs *regs, unsigned long address)
925{
926 if (user_mode(regs)) {
927 siginfo_t info;
928
929 info.si_signo = SIGBUS;
930 info.si_errno = 0;
931 info.si_code = BUS_ADRERR;
932 info.si_addr = (void __user *)address;
933 force_sig_info(SIGBUS, &info, current);
934 return;
935 }
936 bad_page_fault(regs, address, SIGBUS);
937}
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000938
939#ifdef CONFIG_DEBUG_PAGEALLOC
940static void kernel_map_linear_page(unsigned long vaddr, unsigned long lmi)
941{
Paul Mackerras1189be62007-10-11 20:37:10 +1000942 unsigned long hash, hpteg;
943 unsigned long vsid = get_kernel_vsid(vaddr, mmu_kernel_ssize);
944 unsigned long va = hpt_va(vaddr, vsid, mmu_kernel_ssize);
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000945 unsigned long mode = _PAGE_ACCESSED | _PAGE_DIRTY |
946 _PAGE_COHERENT | PP_RWXX | HPTE_R_N;
947 int ret;
948
Paul Mackerras1189be62007-10-11 20:37:10 +1000949 hash = hpt_hash(va, PAGE_SHIFT, mmu_kernel_ssize);
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000950 hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
951
952 ret = ppc_md.hpte_insert(hpteg, va, __pa(vaddr),
Paul Mackerras1189be62007-10-11 20:37:10 +1000953 mode, HPTE_V_BOLTED,
954 mmu_linear_psize, mmu_kernel_ssize);
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000955 BUG_ON (ret < 0);
956 spin_lock(&linear_map_hash_lock);
957 BUG_ON(linear_map_hash_slots[lmi] & 0x80);
958 linear_map_hash_slots[lmi] = ret | 0x80;
959 spin_unlock(&linear_map_hash_lock);
960}
961
962static void kernel_unmap_linear_page(unsigned long vaddr, unsigned long lmi)
963{
Paul Mackerras1189be62007-10-11 20:37:10 +1000964 unsigned long hash, hidx, slot;
965 unsigned long vsid = get_kernel_vsid(vaddr, mmu_kernel_ssize);
966 unsigned long va = hpt_va(vaddr, vsid, mmu_kernel_ssize);
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000967
Paul Mackerras1189be62007-10-11 20:37:10 +1000968 hash = hpt_hash(va, PAGE_SHIFT, mmu_kernel_ssize);
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000969 spin_lock(&linear_map_hash_lock);
970 BUG_ON(!(linear_map_hash_slots[lmi] & 0x80));
971 hidx = linear_map_hash_slots[lmi] & 0x7f;
972 linear_map_hash_slots[lmi] = 0;
973 spin_unlock(&linear_map_hash_lock);
974 if (hidx & _PTEIDX_SECONDARY)
975 hash = ~hash;
976 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
977 slot += hidx & _PTEIDX_GROUP_IX;
Paul Mackerras1189be62007-10-11 20:37:10 +1000978 ppc_md.hpte_invalidate(slot, va, mmu_linear_psize, mmu_kernel_ssize, 0);
Benjamin Herrenschmidt370a9082007-04-12 15:30:23 +1000979}
980
981void kernel_map_pages(struct page *page, int numpages, int enable)
982{
983 unsigned long flags, vaddr, lmi;
984 int i;
985
986 local_irq_save(flags);
987 for (i = 0; i < numpages; i++, page++) {
988 vaddr = (unsigned long)page_address(page);
989 lmi = __pa(vaddr) >> PAGE_SHIFT;
990 if (lmi >= linear_map_hash_count)
991 continue;
992 if (enable)
993 kernel_map_linear_page(vaddr, lmi);
994 else
995 kernel_unmap_linear_page(vaddr, lmi);
996 }
997 local_irq_restore(flags);
998}
999#endif /* CONFIG_DEBUG_PAGEALLOC */