vserver 1.9.5.x5
[linux-2.6.git] / arch / ppc64 / mm / hash_utils.c
1 /*
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
55 #ifdef DEBUG
56 #define DBG(fmt...) udbg_printf(fmt)
57 #else
58 #define DBG(fmt...)
59 #endif
60
61 /*
62  * Note:  pte   --> Linux PTE
63  *        HPTE  --> PowerPC Hashed Page Table Entry
64  *
65  * Execution context:
66  *   htab_initialize is called with the MMU off (of course), but
67  *   the kernel has been copied down to zero so it can directly
68  *   reference global data.  At this point it is very difficult
69  *   to print debug info.
70  *
71  */
72
73 #ifdef CONFIG_U3_DART
74 extern unsigned long dart_tablebase;
75 #endif /* CONFIG_U3_DART */
76
77 HPTE            *htab_address;
78 unsigned long   htab_hash_mask;
79
80 extern unsigned long _SDR1;
81
82 #define KB (1024)
83 #define MB (1024*KB)
84
85 static inline void loop_forever(void)
86 {
87         volatile unsigned long x = 1;
88         for(;x;x|=1)
89                 ;
90 }
91
92 #ifdef CONFIG_PPC_MULTIPLATFORM
93 static 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
99         if (large)
100                 step = 16*MB;
101         else
102                 step = 4*KB;
103
104         for (addr = start; addr < end; addr += step) {
105                 unsigned long vpn, hash, hpteg;
106                 unsigned long vsid = get_kernel_vsid(addr);
107                 unsigned long va = (vsid << 28) | (addr & 0xfffffff);
108                 int ret;
109
110                 if (large)
111                         vpn = va >> HPAGE_SHIFT;
112                 else
113                         vpn = va >> PAGE_SHIFT;
114
115                 hash = hpt_hash(vpn, large);
116
117                 hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
118
119 #ifdef CONFIG_PPC_PSERIES
120                 if (systemcfg->platform & PLATFORM_LPAR)
121                         ret = pSeries_lpar_hpte_insert(hpteg, va,
122                                 virt_to_abs(addr) >> PAGE_SHIFT,
123                                 0, mode, 1, large);
124                 else
125 #endif /* CONFIG_PPC_PSERIES */
126                         ret = native_hpte_insert(hpteg, va,
127                                 virt_to_abs(addr) >> PAGE_SHIFT,
128                                 0, mode, 1, large);
129
130                 if (ret == -1) {
131                         ppc64_terminate_msg(0x20, "create_pte_mapping");
132                         loop_forever();
133                 }
134         }
135 }
136
137 void __init htab_initialize(void)
138 {
139         unsigned long table, htab_size_bytes;
140         unsigned long pteg_count;
141         unsigned long mode_rw;
142         int i, use_largepages = 0;
143
144         DBG(" -> htab_initialize()\n");
145
146         /*
147          * Calculate the required size of the htab.  We want the number of
148          * PTEGs to equal one half the number of real pages.
149          */ 
150         htab_size_bytes = 1UL << ppc64_pft_size;
151         pteg_count = htab_size_bytes >> 7;
152
153         /* For debug, make the HTAB 1/8 as big as it normally would be. */
154         ifppcdebug(PPCDBG_HTABSIZE) {
155                 pteg_count >>= 3;
156                 htab_size_bytes = pteg_count << 7;
157         }
158
159         htab_hash_mask = pteg_count - 1;
160
161         if (systemcfg->platform & PLATFORM_LPAR) {
162                 /* Using a hypervisor which owns the htab */
163                 htab_address = NULL;
164                 _SDR1 = 0; 
165         } else {
166                 /* Find storage for the HPT.  Must be contiguous in
167                  * the absolute address space.
168                  */
169                 table = lmb_alloc(htab_size_bytes, htab_size_bytes);
170
171                 DBG("Hash table allocated at %lx, size: %lx\n", table,
172                     htab_size_bytes);
173
174                 if ( !table ) {
175                         ppc64_terminate_msg(0x20, "hpt space");
176                         loop_forever();
177                 }
178                 htab_address = abs_to_virt(table);
179
180                 /* htab absolute addr + encoded htabsize */
181                 _SDR1 = table + __ilog2(pteg_count) - 11;
182
183                 /* Initialize the HPT with no entries */
184                 memset((void *)table, 0, htab_size_bytes);
185         }
186
187         mode_rw = _PAGE_ACCESSED | _PAGE_COHERENT | PP_RWXX;
188
189         /* On U3 based machines, we need to reserve the DART area and
190          * _NOT_ map it to avoid cache paradoxes as it's remapped non
191          * cacheable later on
192          */
193         if (cur_cpu_spec->cpu_features & CPU_FTR_16M_PAGE)
194                 use_largepages = 1;
195
196         /* create bolted the linear mapping in the hash table */
197         for (i=0; i < lmb.memory.cnt; i++) {
198                 unsigned long base, size;
199
200                 base = lmb.memory.region[i].physbase + KERNELBASE;
201                 size = lmb.memory.region[i].size;
202
203                 DBG("creating mapping for region: %lx : %lx\n", base, size);
204
205 #ifdef CONFIG_U3_DART
206                 /* Do not map the DART space. Fortunately, it will be aligned
207                  * in such a way that it will not cross two lmb regions and will
208                  * fit within a single 16Mb page.
209                  * The DART space is assumed to be a full 16Mb region even if we
210                  * only use 2Mb of that space. We will use more of it later for
211                  * AGP GART. We have to use a full 16Mb large page.
212                  */
213                 DBG("DART base: %lx\n", dart_tablebase);
214
215                 if (dart_tablebase != 0 && dart_tablebase >= base
216                     && dart_tablebase < (base + size)) {
217                         if (base != dart_tablebase)
218                                 create_pte_mapping(base, dart_tablebase, mode_rw,
219                                                    use_largepages);
220                         if ((base + size) > (dart_tablebase + 16*MB))
221                                 create_pte_mapping(dart_tablebase + 16*MB, base + size,
222                                                    mode_rw, use_largepages);
223                         continue;
224                 }
225 #endif /* CONFIG_U3_DART */
226                 create_pte_mapping(base, base + size, mode_rw, use_largepages);
227         }
228         DBG(" <- htab_initialize()\n");
229 }
230 #undef KB
231 #undef MB
232 #endif /* CONFIG_PPC_MULTIPLATFORM */
233
234 /*
235  * Called by asm hashtable.S for doing lazy icache flush
236  */
237 unsigned int hash_page_do_lazy_icache(unsigned int pp, pte_t pte, int trap)
238 {
239         struct page *page;
240
241 #define PPC64_HWNOEXEC (1 << 2)
242
243         if (!pfn_valid(pte_pfn(pte)))
244                 return pp;
245
246         page = pte_page(pte);
247
248         /* page is dirty */
249         if (!test_bit(PG_arch_1, &page->flags) && !PageReserved(page)) {
250                 if (trap == 0x400) {
251                         __flush_dcache_icache(page_address(page));
252                         set_bit(PG_arch_1, &page->flags);
253                 } else
254                         pp |= PPC64_HWNOEXEC;
255         }
256         return pp;
257 }
258
259 /* Result code is:
260  *  0 - handled
261  *  1 - normal page fault
262  * -1 - critical hash insertion error
263  */
264 int hash_page(unsigned long ea, unsigned long access, unsigned long trap)
265 {
266         void *pgdir;
267         unsigned long vsid;
268         struct mm_struct *mm;
269         pte_t *ptep;
270         int ret;
271         int user_region = 0;
272         int local = 0;
273         cpumask_t tmp;
274
275         switch (REGION_ID(ea)) {
276         case USER_REGION_ID:
277                 user_region = 1;
278                 mm = current->mm;
279                 if ((ea > USER_END) || (! mm))
280                         return 1;
281
282                 vsid = get_vsid(mm->context.id, ea);
283                 break;
284         case IO_REGION_ID:
285                 if (ea > IMALLOC_END)
286                         return 1;
287                 mm = &ioremap_mm;
288                 vsid = get_kernel_vsid(ea);
289                 break;
290         case VMALLOC_REGION_ID:
291                 if (ea > VMALLOC_END)
292                         return 1;
293                 mm = &init_mm;
294                 vsid = get_kernel_vsid(ea);
295                 break;
296 #if 0
297         case KERNEL_REGION_ID:
298                 /*
299                  * Should never get here - entire 0xC0... region is bolted.
300                  * Send the problem up to do_page_fault 
301                  */
302 #endif
303         default:
304                 /* Not a valid range
305                  * Send the problem up to do_page_fault 
306                  */
307                 return 1;
308                 break;
309         }
310
311         pgdir = mm->pgd;
312
313         if (pgdir == NULL)
314                 return 1;
315
316         tmp = cpumask_of_cpu(smp_processor_id());
317         if (user_region && cpus_equal(mm->cpu_vm_mask, tmp))
318                 local = 1;
319
320         /* Is this a huge page ? */
321         if (unlikely(in_hugepage_area(mm->context, ea)))
322                 ret = hash_huge_page(mm, access, ea, vsid, local);
323         else {
324                 ptep = find_linux_pte(pgdir, ea);
325                 if (ptep == NULL)
326                         return 1;
327                 ret = __hash_page(ea, access, vsid, ptep, trap, local);
328         }
329
330         return ret;
331 }
332
333 void flush_hash_page(unsigned long context, unsigned long ea, pte_t pte,
334                      int local)
335 {
336         unsigned long vsid, vpn, va, hash, secondary, slot;
337         unsigned long huge = pte_huge(pte);
338
339         if ((ea >= USER_START) && (ea <= USER_END))
340                 vsid = get_vsid(context, ea);
341         else
342                 vsid = get_kernel_vsid(ea);
343
344         va = (vsid << 28) | (ea & 0x0fffffff);
345         if (huge)
346                 vpn = va >> HPAGE_SHIFT;
347         else
348                 vpn = va >> PAGE_SHIFT;
349         hash = hpt_hash(vpn, huge);
350         secondary = (pte_val(pte) & _PAGE_SECONDARY) >> 15;
351         if (secondary)
352                 hash = ~hash;
353         slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
354         slot += (pte_val(pte) & _PAGE_GROUP_IX) >> 12;
355
356         ppc_md.hpte_invalidate(slot, va, huge, local);
357 }
358
359 void flush_hash_range(unsigned long context, unsigned long number, int local)
360 {
361         if (ppc_md.flush_hash_range) {
362                 ppc_md.flush_hash_range(context, number, local);
363         } else {
364                 int i;
365                 struct ppc64_tlb_batch *batch = &__get_cpu_var(ppc64_tlb_batch);
366
367                 for (i = 0; i < number; i++)
368                         flush_hash_page(context, batch->addr[i], batch->pte[i],
369                                         local);
370         }
371 }
372
373 static inline void make_bl(unsigned int *insn_addr, void *func)
374 {
375         unsigned long funcp = *((unsigned long *)func);
376         int offset = funcp - (unsigned long)insn_addr;
377
378         *insn_addr = (unsigned int)(0x48000001 | (offset & 0x03fffffc));
379         flush_icache_range((unsigned long)insn_addr, 4+
380                            (unsigned long)insn_addr);
381 }
382
383 /*
384  * low_hash_fault is called when we the low level hash code failed
385  * to instert a PTE due to an hypervisor error
386  */
387 void low_hash_fault(struct pt_regs *regs, unsigned long address)
388 {
389         if (user_mode(regs)) {
390                 siginfo_t info;
391
392                 info.si_signo = SIGBUS;
393                 info.si_errno = 0;
394                 info.si_code = BUS_ADRERR;
395                 info.si_addr = (void __user *)address;
396                 force_sig_info(SIGBUS, &info, current);
397                 return;
398         }
399         bad_page_fault(regs, address, SIGBUS);
400 }
401
402 void __init htab_finish_init(void)
403 {
404         extern unsigned int *htab_call_hpte_insert1;
405         extern unsigned int *htab_call_hpte_insert2;
406         extern unsigned int *htab_call_hpte_remove;
407         extern unsigned int *htab_call_hpte_updatepp;
408
409         make_bl(htab_call_hpte_insert1, ppc_md.hpte_insert);
410         make_bl(htab_call_hpte_insert2, ppc_md.hpte_insert);
411         make_bl(htab_call_hpte_remove, ppc_md.hpte_remove);
412         make_bl(htab_call_hpte_updatepp, ppc_md.hpte_updatepp);
413 }