vserver 1.9.3
[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/naca.h>
45 #include <asm/machdep.h>
46 #include <asm/lmb.h>
47 #include <asm/abs_addr.h>
48 #include <asm/tlbflush.h>
49 #include <asm/io.h>
50 #include <asm/eeh.h>
51 #include <asm/tlb.h>
52 #include <asm/cacheflush.h>
53 #include <asm/cputable.h>
54 #include <asm/abs_addr.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
75 extern unsigned long dart_tablebase;
76 #endif /* CONFIG_U3_DART */
77
78 HTAB htab_data = {NULL, 0, 0, 0, 0};
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_data.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 << naca->pftSize;
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_data.htab_num_ptegs = pteg_count;
160         htab_data.htab_hash_mask = pteg_count - 1;
161
162         if (systemcfg->platform & PLATFORM_LPAR) {
163                 /* Using a hypervisor which owns the htab */
164                 htab_data.htab = NULL;
165                 _SDR1 = 0; 
166         } else {
167                 /* Find storage for the HPT.  Must be contiguous in
168                  * the absolute address space.
169                  */
170                 table = lmb_alloc(htab_size_bytes, htab_size_bytes);
171
172                 DBG("Hash table allocated at %lx, size: %lx\n", table,
173                     htab_size_bytes);
174
175                 if ( !table ) {
176                         ppc64_terminate_msg(0x20, "hpt space");
177                         loop_forever();
178                 }
179                 htab_data.htab = abs_to_virt(table);
180
181                 /* htab absolute addr + encoded htabsize */
182                 _SDR1 = table + __ilog2(pteg_count) - 11;
183
184                 /* Initialize the HPT with no entries */
185                 memset((void *)table, 0, htab_size_bytes);
186         }
187
188         mode_rw = _PAGE_ACCESSED | _PAGE_COHERENT | PP_RWXX;
189
190         /* On U3 based machines, we need to reserve the DART area and
191          * _NOT_ map it to avoid cache paradoxes as it's remapped non
192          * cacheable later on
193          */
194         if (cur_cpu_spec->cpu_features & CPU_FTR_16M_PAGE)
195                 use_largepages = 1;
196
197         /* create bolted the linear mapping in the hash table */
198         for (i=0; i < lmb.memory.cnt; i++) {
199                 unsigned long base, size;
200
201                 base = lmb.memory.region[i].physbase + KERNELBASE;
202                 size = lmb.memory.region[i].size;
203
204                 DBG("creating mapping for region: %lx : %lx\n", base, size);
205
206 #ifdef CONFIG_U3_DART
207                 /* Do not map the DART space. Fortunately, it will be aligned
208                  * in such a way that it will not cross two lmb regions and will
209                  * fit within a single 16Mb page.
210                  * The DART space is assumed to be a full 16Mb region even if we
211                  * only use 2Mb of that space. We will use more of it later for
212                  * AGP GART. We have to use a full 16Mb large page.
213                  */
214                 DBG("DART base: %lx\n", dart_tablebase);
215
216                 if (dart_tablebase != 0 && dart_tablebase >= base
217                     && dart_tablebase < (base + size)) {
218                         if (base != dart_tablebase)
219                                 create_pte_mapping(base, dart_tablebase, mode_rw,
220                                                    use_largepages);
221                         if ((base + size) > (dart_tablebase + 16*MB))
222                                 create_pte_mapping(dart_tablebase + 16*MB, base + size,
223                                                    mode_rw, use_largepages);
224                         continue;
225                 }
226 #endif /* CONFIG_U3_DART */
227                 create_pte_mapping(base, base + size, mode_rw, use_largepages);
228         }
229         DBG(" <- htab_initialize()\n");
230 }
231 #undef KB
232 #undef MB
233 #endif /* CONFIG_PPC_MULTIPLATFORM */
234
235 /*
236  * Called by asm hashtable.S for doing lazy icache flush
237  */
238 unsigned int hash_page_do_lazy_icache(unsigned int pp, pte_t pte, int trap)
239 {
240         struct page *page;
241
242 #define PPC64_HWNOEXEC (1 << 2)
243
244         if (!pfn_valid(pte_pfn(pte)))
245                 return pp;
246
247         page = pte_page(pte);
248
249         /* page is dirty */
250         if (!test_bit(PG_arch_1, &page->flags) && !PageReserved(page)) {
251                 if (trap == 0x400) {
252                         __flush_dcache_icache(page_address(page));
253                         set_bit(PG_arch_1, &page->flags);
254                 } else
255                         pp |= PPC64_HWNOEXEC;
256         }
257         return pp;
258 }
259
260 /* Result code is:
261  *  0 - handled
262  *  1 - normal page fault
263  * -1 - critical hash insertion error
264  */
265 int hash_page(unsigned long ea, unsigned long access, unsigned long trap)
266 {
267         void *pgdir;
268         unsigned long vsid;
269         struct mm_struct *mm;
270         pte_t *ptep;
271         int ret;
272         int user_region = 0;
273         int local = 0;
274         cpumask_t tmp;
275
276         switch (REGION_ID(ea)) {
277         case USER_REGION_ID:
278                 user_region = 1;
279                 mm = current->mm;
280                 if ((ea > USER_END) || (! mm))
281                         return 1;
282
283                 vsid = get_vsid(mm->context.id, ea);
284                 break;
285         case IO_REGION_ID:
286                 if (ea > IMALLOC_END)
287                         return 1;
288                 mm = &ioremap_mm;
289                 vsid = get_kernel_vsid(ea);
290                 break;
291         case VMALLOC_REGION_ID:
292                 if (ea > VMALLOC_END)
293                         return 1;
294                 mm = &init_mm;
295                 vsid = get_kernel_vsid(ea);
296                 break;
297 #if 0
298         case EEH_REGION_ID:
299                 /*
300                  * Should only be hit if there is an access to MMIO space
301                  * which is protected by EEH.
302                  * Send the problem up to do_page_fault 
303                  */
304         case KERNEL_REGION_ID:
305                 /*
306                  * Should never get here - entire 0xC0... region is bolted.
307                  * Send the problem up to do_page_fault 
308                  */
309 #endif
310         default:
311                 /* Not a valid range
312                  * Send the problem up to do_page_fault 
313                  */
314                 return 1;
315                 break;
316         }
317
318         pgdir = mm->pgd;
319
320         if (pgdir == NULL)
321                 return 1;
322
323         tmp = cpumask_of_cpu(smp_processor_id());
324         if (user_region && cpus_equal(mm->cpu_vm_mask, tmp))
325                 local = 1;
326
327         /* Is this a huge page ? */
328         if (unlikely(in_hugepage_area(mm->context, ea)))
329                 ret = hash_huge_page(mm, access, ea, vsid, local);
330         else {
331                 ptep = find_linux_pte(pgdir, ea);
332                 if (ptep == NULL)
333                         return 1;
334                 ret = __hash_page(ea, access, vsid, ptep, trap, local);
335         }
336
337         return ret;
338 }
339
340 void flush_hash_page(unsigned long context, unsigned long ea, pte_t pte,
341                      int local)
342 {
343         unsigned long vsid, vpn, va, hash, secondary, slot;
344
345         /* XXX fix for large ptes */
346         unsigned long large = 0;
347
348         if ((ea >= USER_START) && (ea <= USER_END))
349                 vsid = get_vsid(context, ea);
350         else
351                 vsid = get_kernel_vsid(ea);
352
353         va = (vsid << 28) | (ea & 0x0fffffff);
354         if (large)
355                 vpn = va >> HPAGE_SHIFT;
356         else
357                 vpn = va >> PAGE_SHIFT;
358         hash = hpt_hash(vpn, large);
359         secondary = (pte_val(pte) & _PAGE_SECONDARY) >> 15;
360         if (secondary)
361                 hash = ~hash;
362         slot = (hash & htab_data.htab_hash_mask) * HPTES_PER_GROUP;
363         slot += (pte_val(pte) & _PAGE_GROUP_IX) >> 12;
364
365         ppc_md.hpte_invalidate(slot, va, large, local);
366 }
367
368 void flush_hash_range(unsigned long context, unsigned long number, int local)
369 {
370         if (ppc_md.flush_hash_range) {
371                 ppc_md.flush_hash_range(context, number, local);
372         } else {
373                 int i;
374                 struct ppc64_tlb_batch *batch = &__get_cpu_var(ppc64_tlb_batch);
375
376                 for (i = 0; i < number; i++)
377                         flush_hash_page(context, batch->addr[i], batch->pte[i],
378                                         local);
379         }
380 }
381
382 static inline void make_bl(unsigned int *insn_addr, void *func)
383 {
384         unsigned long funcp = *((unsigned long *)func);
385         int offset = funcp - (unsigned long)insn_addr;
386
387         *insn_addr = (unsigned int)(0x48000001 | (offset & 0x03fffffc));
388         flush_icache_range((unsigned long)insn_addr, 4+
389                            (unsigned long)insn_addr);
390 }
391
392 /*
393  * low_hash_fault is called when we the low level hash code failed
394  * to instert a PTE due to an hypervisor error
395  */
396 void low_hash_fault(struct pt_regs *regs, unsigned long address)
397 {
398         if (user_mode(regs)) {
399                 siginfo_t info;
400
401                 info.si_signo = SIGBUS;
402                 info.si_errno = 0;
403                 info.si_code = BUS_ADRERR;
404                 info.si_addr = (void *)address;
405                 force_sig_info(SIGBUS, &info, current);
406                 return;
407         }
408         bad_page_fault(regs, address, SIGBUS);
409 }
410
411 void __init htab_finish_init(void)
412 {
413         extern unsigned int *htab_call_hpte_insert1;
414         extern unsigned int *htab_call_hpte_insert2;
415         extern unsigned int *htab_call_hpte_remove;
416         extern unsigned int *htab_call_hpte_updatepp;
417
418         make_bl(htab_call_hpte_insert1, ppc_md.hpte_insert);
419         make_bl(htab_call_hpte_insert2, ppc_md.hpte_insert);
420         make_bl(htab_call_hpte_remove, ppc_md.hpte_remove);
421         make_bl(htab_call_hpte_updatepp, ppc_md.hpte_updatepp);
422 }