blob: 6e9e05cce02c8448e207a782f3ebc4cdb53f6556 [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
22
23#include <linux/config.h>
24#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
35#include <asm/ppcdebug.h>
36#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>
45#include <asm/lmb.h>
46#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>
53#include <asm/abs_addr.h>
54#include <asm/sections.h>
55
56#ifdef DEBUG
57#define DBG(fmt...) udbg_printf(fmt)
58#else
59#define DBG(fmt...)
60#endif
61
62/*
63 * Note: pte --> Linux PTE
64 * HPTE --> PowerPC Hashed Page Table Entry
65 *
66 * Execution context:
67 * htab_initialize is called with the MMU off (of course), but
68 * the kernel has been copied down to zero so it can directly
69 * reference global data. At this point it is very difficult
70 * to print debug info.
71 *
72 */
73
74#ifdef CONFIG_U3_DART
75extern unsigned long dart_tablebase;
76#endif /* CONFIG_U3_DART */
77
David Gibson96e28442005-07-13 01:11:42 -070078hpte_t *htab_address;
79unsigned long htab_hash_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Paul Mackerrasab1f9da2005-10-10 21:58:35 +100081unsigned long _SDR1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83#define KB (1024)
84#define MB (1024*KB)
85
86static inline void loop_forever(void)
87{
88 volatile unsigned long x = 1;
89 for(;x;x|=1)
90 ;
91}
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093static inline void create_pte_mapping(unsigned long start, unsigned long end,
94 unsigned long mode, int large)
95{
96 unsigned long addr;
97 unsigned int step;
98 unsigned long tmp_mode;
David Gibson96e28442005-07-13 01:11:42 -070099 unsigned long vflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
David Gibson96e28442005-07-13 01:11:42 -0700101 if (large) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 step = 16*MB;
David Gibson96e28442005-07-13 01:11:42 -0700103 vflags = HPTE_V_BOLTED | HPTE_V_LARGE;
104 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 step = 4*KB;
David Gibson96e28442005-07-13 01:11:42 -0700106 vflags = HPTE_V_BOLTED;
107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109 for (addr = start; addr < end; addr += step) {
110 unsigned long vpn, hash, hpteg;
111 unsigned long vsid = get_kernel_vsid(addr);
112 unsigned long va = (vsid << 28) | (addr & 0xfffffff);
Michael Ellerman4c551302005-09-23 14:47:58 +1000113 int ret = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115 if (large)
116 vpn = va >> HPAGE_SHIFT;
117 else
118 vpn = va >> PAGE_SHIFT;
119
120
121 tmp_mode = mode;
122
123 /* Make non-kernel text non-executable */
124 if (!in_kernel_text(addr))
125 tmp_mode = mode | HW_NO_EXEC;
126
127 hash = hpt_hash(vpn, large);
128
129 hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
130
Michael Ellerman4c551302005-09-23 14:47:58 +1000131#ifdef CONFIG_PPC_ISERIES
132 if (systemcfg->platform & PLATFORM_ISERIES_LPAR)
133 ret = iSeries_hpte_bolt_or_insert(hpteg, va,
134 virt_to_abs(addr) >> PAGE_SHIFT,
135 vflags, tmp_mode);
136 else
137#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138#ifdef CONFIG_PPC_PSERIES
139 if (systemcfg->platform & PLATFORM_LPAR)
140 ret = pSeries_lpar_hpte_insert(hpteg, va,
141 virt_to_abs(addr) >> PAGE_SHIFT,
David Gibson96e28442005-07-13 01:11:42 -0700142 vflags, tmp_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 else
Michael Ellerman4c551302005-09-23 14:47:58 +1000144#endif
145#ifdef CONFIG_PPC_MULTIPLATFORM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 ret = native_hpte_insert(hpteg, va,
147 virt_to_abs(addr) >> PAGE_SHIFT,
David Gibson96e28442005-07-13 01:11:42 -0700148 vflags, tmp_mode);
Michael Ellerman4c551302005-09-23 14:47:58 +1000149#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151 if (ret == -1) {
152 ppc64_terminate_msg(0x20, "create_pte_mapping");
153 loop_forever();
154 }
155 }
156}
157
Paul Mackerras3eac8c62005-10-12 16:58:53 +1000158static unsigned long get_hashtable_size(void)
159{
160 unsigned long rnd_mem_size, pteg_count;
161
162 /* If hash size wasn't obtained in prom.c, we calculate it now based on
163 * the total RAM size
164 */
165 if (ppc64_pft_size)
166 return 1UL << ppc64_pft_size;
167
168 /* round mem_size up to next power of 2 */
169 rnd_mem_size = 1UL << __ilog2(systemcfg->physicalMemorySize);
170 if (rnd_mem_size < systemcfg->physicalMemorySize)
171 rnd_mem_size <<= 1;
172
173 /* # pages / 2 */
174 pteg_count = max(rnd_mem_size >> (12 + 1), 1UL << 11);
175
176 return pteg_count << 7;
177}
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179void __init htab_initialize(void)
180{
181 unsigned long table, htab_size_bytes;
182 unsigned long pteg_count;
183 unsigned long mode_rw;
184 int i, use_largepages = 0;
185 unsigned long base = 0, size = 0;
186 extern unsigned long tce_alloc_start, tce_alloc_end;
187
188 DBG(" -> htab_initialize()\n");
189
190 /*
191 * Calculate the required size of the htab. We want the number of
192 * PTEGs to equal one half the number of real pages.
193 */
Paul Mackerras3eac8c62005-10-12 16:58:53 +1000194 htab_size_bytes = get_hashtable_size();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 pteg_count = htab_size_bytes >> 7;
196
197 /* For debug, make the HTAB 1/8 as big as it normally would be. */
198 ifppcdebug(PPCDBG_HTABSIZE) {
199 pteg_count >>= 3;
200 htab_size_bytes = pteg_count << 7;
201 }
202
203 htab_hash_mask = pteg_count - 1;
204
205 if (systemcfg->platform & PLATFORM_LPAR) {
206 /* Using a hypervisor which owns the htab */
207 htab_address = NULL;
208 _SDR1 = 0;
209 } else {
210 /* Find storage for the HPT. Must be contiguous in
211 * the absolute address space.
212 */
213 table = lmb_alloc(htab_size_bytes, htab_size_bytes);
214
215 DBG("Hash table allocated at %lx, size: %lx\n", table,
216 htab_size_bytes);
217
218 if ( !table ) {
219 ppc64_terminate_msg(0x20, "hpt space");
220 loop_forever();
221 }
222 htab_address = abs_to_virt(table);
223
224 /* htab absolute addr + encoded htabsize */
225 _SDR1 = table + __ilog2(pteg_count) - 11;
226
227 /* Initialize the HPT with no entries */
228 memset((void *)table, 0, htab_size_bytes);
229 }
230
Anton Blanchard515bae92005-06-21 17:15:55 -0700231 mode_rw = _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_COHERENT | PP_RWXX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 /* On U3 based machines, we need to reserve the DART area and
234 * _NOT_ map it to avoid cache paradoxes as it's remapped non
235 * cacheable later on
236 */
237 if (cpu_has_feature(CPU_FTR_16M_PAGE))
238 use_largepages = 1;
239
240 /* create bolted the linear mapping in the hash table */
241 for (i=0; i < lmb.memory.cnt; i++) {
Michael Ellerman180379d2005-08-03 20:21:26 +1000242 base = lmb.memory.region[i].base + KERNELBASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 size = lmb.memory.region[i].size;
244
245 DBG("creating mapping for region: %lx : %lx\n", base, size);
246
247#ifdef CONFIG_U3_DART
248 /* Do not map the DART space. Fortunately, it will be aligned
249 * in such a way that it will not cross two lmb regions and will
250 * fit within a single 16Mb page.
251 * The DART space is assumed to be a full 16Mb region even if we
252 * only use 2Mb of that space. We will use more of it later for
253 * AGP GART. We have to use a full 16Mb large page.
254 */
255 DBG("DART base: %lx\n", dart_tablebase);
256
257 if (dart_tablebase != 0 && dart_tablebase >= base
258 && dart_tablebase < (base + size)) {
259 if (base != dart_tablebase)
260 create_pte_mapping(base, dart_tablebase, mode_rw,
261 use_largepages);
262 if ((base + size) > (dart_tablebase + 16*MB))
263 create_pte_mapping(dart_tablebase + 16*MB, base + size,
264 mode_rw, use_largepages);
265 continue;
266 }
267#endif /* CONFIG_U3_DART */
268 create_pte_mapping(base, base + size, mode_rw, use_largepages);
269 }
270
271 /*
272 * If we have a memory_limit and we've allocated TCEs then we need to
273 * explicitly map the TCE area at the top of RAM. We also cope with the
274 * case that the TCEs start below memory_limit.
275 * tce_alloc_start/end are 16MB aligned so the mapping should work
276 * for either 4K or 16MB pages.
277 */
278 if (tce_alloc_start) {
279 tce_alloc_start += KERNELBASE;
280 tce_alloc_end += KERNELBASE;
281
282 if (base + size >= tce_alloc_start)
283 tce_alloc_start = base + size + 1;
284
285 create_pte_mapping(tce_alloc_start, tce_alloc_end,
286 mode_rw, use_largepages);
287 }
288
289 DBG(" <- htab_initialize()\n");
290}
291#undef KB
292#undef MB
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294/*
295 * Called by asm hashtable.S for doing lazy icache flush
296 */
297unsigned int hash_page_do_lazy_icache(unsigned int pp, pte_t pte, int trap)
298{
299 struct page *page;
300
301 if (!pfn_valid(pte_pfn(pte)))
302 return pp;
303
304 page = pte_page(pte);
305
306 /* page is dirty */
307 if (!test_bit(PG_arch_1, &page->flags) && !PageReserved(page)) {
308 if (trap == 0x400) {
309 __flush_dcache_icache(page_address(page));
310 set_bit(PG_arch_1, &page->flags);
311 } else
312 pp |= HW_NO_EXEC;
313 }
314 return pp;
315}
316
317/* Result code is:
318 * 0 - handled
319 * 1 - normal page fault
320 * -1 - critical hash insertion error
321 */
322int hash_page(unsigned long ea, unsigned long access, unsigned long trap)
323{
324 void *pgdir;
325 unsigned long vsid;
326 struct mm_struct *mm;
327 pte_t *ptep;
328 int ret;
329 int user_region = 0;
330 int local = 0;
331 cpumask_t tmp;
332
David Gibsone28f7fa2005-08-05 19:39:06 +1000333 if ((ea & ~REGION_MASK) >= PGTABLE_RANGE)
David Gibson1f8d4192005-05-05 16:15:13 -0700334 return 1;
335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 switch (REGION_ID(ea)) {
337 case USER_REGION_ID:
338 user_region = 1;
339 mm = current->mm;
David Gibson1f8d4192005-05-05 16:15:13 -0700340 if (! mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 return 1;
342
343 vsid = get_vsid(mm->context.id, ea);
344 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 case VMALLOC_REGION_ID:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 mm = &init_mm;
347 vsid = get_kernel_vsid(ea);
348 break;
349#if 0
350 case KERNEL_REGION_ID:
351 /*
352 * Should never get here - entire 0xC0... region is bolted.
353 * Send the problem up to do_page_fault
354 */
355#endif
356 default:
357 /* Not a valid range
358 * Send the problem up to do_page_fault
359 */
360 return 1;
361 break;
362 }
363
364 pgdir = mm->pgd;
365
366 if (pgdir == NULL)
367 return 1;
368
369 tmp = cpumask_of_cpu(smp_processor_id());
370 if (user_region && cpus_equal(mm->cpu_vm_mask, tmp))
371 local = 1;
372
373 /* Is this a huge page ? */
374 if (unlikely(in_hugepage_area(mm->context, ea)))
375 ret = hash_huge_page(mm, access, ea, vsid, local);
376 else {
377 ptep = find_linux_pte(pgdir, ea);
378 if (ptep == NULL)
379 return 1;
380 ret = __hash_page(ea, access, vsid, ptep, trap, local);
381 }
382
383 return ret;
384}
385
Benjamin Herrenschmidt61b1a942005-09-20 13:52:50 +1000386void flush_hash_page(unsigned long va, pte_t pte, int local)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
Benjamin Herrenschmidt61b1a942005-09-20 13:52:50 +1000388 unsigned long vpn, hash, secondary, slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 unsigned long huge = pte_huge(pte);
390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 if (huge)
392 vpn = va >> HPAGE_SHIFT;
393 else
394 vpn = va >> PAGE_SHIFT;
395 hash = hpt_hash(vpn, huge);
396 secondary = (pte_val(pte) & _PAGE_SECONDARY) >> 15;
397 if (secondary)
398 hash = ~hash;
399 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
400 slot += (pte_val(pte) & _PAGE_GROUP_IX) >> 12;
401
402 ppc_md.hpte_invalidate(slot, va, huge, local);
403}
404
Benjamin Herrenschmidt61b1a942005-09-20 13:52:50 +1000405void flush_hash_range(unsigned long number, int local)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
407 if (ppc_md.flush_hash_range) {
Benjamin Herrenschmidt61b1a942005-09-20 13:52:50 +1000408 ppc_md.flush_hash_range(number, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 } else {
410 int i;
Benjamin Herrenschmidt61b1a942005-09-20 13:52:50 +1000411 struct ppc64_tlb_batch *batch =
412 &__get_cpu_var(ppc64_tlb_batch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414 for (i = 0; i < number; i++)
Benjamin Herrenschmidt61b1a942005-09-20 13:52:50 +1000415 flush_hash_page(batch->vaddr[i], batch->pte[i], local);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 }
417}
418
419static inline void make_bl(unsigned int *insn_addr, void *func)
420{
421 unsigned long funcp = *((unsigned long *)func);
422 int offset = funcp - (unsigned long)insn_addr;
423
424 *insn_addr = (unsigned int)(0x48000001 | (offset & 0x03fffffc));
425 flush_icache_range((unsigned long)insn_addr, 4+
426 (unsigned long)insn_addr);
427}
428
429/*
430 * low_hash_fault is called when we the low level hash code failed
431 * to instert a PTE due to an hypervisor error
432 */
433void low_hash_fault(struct pt_regs *regs, unsigned long address)
434{
435 if (user_mode(regs)) {
436 siginfo_t info;
437
438 info.si_signo = SIGBUS;
439 info.si_errno = 0;
440 info.si_code = BUS_ADRERR;
441 info.si_addr = (void __user *)address;
442 force_sig_info(SIGBUS, &info, current);
443 return;
444 }
445 bad_page_fault(regs, address, SIGBUS);
446}
447
448void __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
455 make_bl(htab_call_hpte_insert1, ppc_md.hpte_insert);
456 make_bl(htab_call_hpte_insert2, ppc_md.hpte_insert);
457 make_bl(htab_call_hpte_remove, ppc_md.hpte_remove);
458 make_bl(htab_call_hpte_updatepp, ppc_md.hpte_updatepp);
459}