[PATCH] powerpc: Make ppc_md.set_dabr non 64-bit specific
[powerpc.git] / arch / powerpc / mm / hash_utils_64.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/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>
52 #include <asm/abs_addr.h>
53 #include <asm/sections.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_t *htab_address;
78 unsigned long htab_hash_mask;
79
80 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 static inline void create_pte_mapping(unsigned long start, unsigned long end,
93                                       unsigned long mode, int large)
94 {
95         unsigned long addr;
96         unsigned int step;
97         unsigned long tmp_mode;
98         unsigned long vflags;
99
100         if (large) {
101                 step = 16*MB;
102                 vflags = HPTE_V_BOLTED | HPTE_V_LARGE;
103         } else {
104                 step = 4*KB;
105                 vflags = HPTE_V_BOLTED;
106         }
107
108         for (addr = start; addr < end; addr += step) {
109                 unsigned long vpn, hash, hpteg;
110                 unsigned long vsid = get_kernel_vsid(addr);
111                 unsigned long va = (vsid << 28) | (addr & 0xfffffff);
112                 int ret = -1;
113
114                 if (large)
115                         vpn = va >> HPAGE_SHIFT;
116                 else
117                         vpn = va >> PAGE_SHIFT;
118
119
120                 tmp_mode = mode;
121                 
122                 /* Make non-kernel text non-executable */
123                 if (!in_kernel_text(addr))
124                         tmp_mode = mode | HW_NO_EXEC;
125
126                 hash = hpt_hash(vpn, large);
127
128                 hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
129
130 #ifdef CONFIG_PPC_ISERIES
131                 if (systemcfg->platform & PLATFORM_ISERIES_LPAR)
132                         ret = iSeries_hpte_bolt_or_insert(hpteg, va,
133                                 virt_to_abs(addr) >> PAGE_SHIFT,
134                                 vflags, tmp_mode);
135                 else
136 #endif
137 #ifdef CONFIG_PPC_PSERIES
138                 if (systemcfg->platform & PLATFORM_LPAR)
139                         ret = pSeries_lpar_hpte_insert(hpteg, va,
140                                 virt_to_abs(addr) >> PAGE_SHIFT,
141                                 vflags, tmp_mode);
142                 else
143 #endif
144 #ifdef CONFIG_PPC_MULTIPLATFORM
145                         ret = native_hpte_insert(hpteg, va,
146                                 virt_to_abs(addr) >> PAGE_SHIFT,
147                                 vflags, tmp_mode);
148 #endif
149
150                 if (ret == -1) {
151                         ppc64_terminate_msg(0x20, "create_pte_mapping");
152                         loop_forever();
153                 }
154         }
155 }
156
157 static unsigned long get_hashtable_size(void)
158 {
159         unsigned long rnd_mem_size, pteg_count;
160
161         /* If hash size wasn't obtained in prom.c, we calculate it now based on
162          * the total RAM size
163          */
164         if (ppc64_pft_size)
165                 return 1UL << ppc64_pft_size;
166
167         /* round mem_size up to next power of 2 */
168         rnd_mem_size = 1UL << __ilog2(systemcfg->physicalMemorySize);
169         if (rnd_mem_size < systemcfg->physicalMemorySize)
170                 rnd_mem_size <<= 1;
171
172         /* # pages / 2 */
173         pteg_count = max(rnd_mem_size >> (12 + 1), 1UL << 11);
174
175         return pteg_count << 7;
176 }
177
178 void __init htab_initialize(void)
179 {
180         unsigned long table, htab_size_bytes;
181         unsigned long pteg_count;
182         unsigned long mode_rw;
183         int i, use_largepages = 0;
184         unsigned long base = 0, size = 0;
185         extern unsigned long tce_alloc_start, tce_alloc_end;
186
187         DBG(" -> htab_initialize()\n");
188
189         /*
190          * Calculate the required size of the htab.  We want the number of
191          * PTEGs to equal one half the number of real pages.
192          */ 
193         htab_size_bytes = get_hashtable_size();
194         pteg_count = htab_size_bytes >> 7;
195
196         htab_hash_mask = pteg_count - 1;
197
198         if (systemcfg->platform & PLATFORM_LPAR) {
199                 /* Using a hypervisor which owns the htab */
200                 htab_address = NULL;
201                 _SDR1 = 0; 
202         } else {
203                 /* Find storage for the HPT.  Must be contiguous in
204                  * the absolute address space.
205                  */
206                 table = lmb_alloc(htab_size_bytes, htab_size_bytes);
207
208                 DBG("Hash table allocated at %lx, size: %lx\n", table,
209                     htab_size_bytes);
210
211                 if ( !table ) {
212                         ppc64_terminate_msg(0x20, "hpt space");
213                         loop_forever();
214                 }
215                 htab_address = abs_to_virt(table);
216
217                 /* htab absolute addr + encoded htabsize */
218                 _SDR1 = table + __ilog2(pteg_count) - 11;
219
220                 /* Initialize the HPT with no entries */
221                 memset((void *)table, 0, htab_size_bytes);
222         }
223
224         mode_rw = _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_COHERENT | PP_RWXX;
225
226         /* On U3 based machines, we need to reserve the DART area and
227          * _NOT_ map it to avoid cache paradoxes as it's remapped non
228          * cacheable later on
229          */
230         if (cpu_has_feature(CPU_FTR_16M_PAGE))
231                 use_largepages = 1;
232
233         /* create bolted the linear mapping in the hash table */
234         for (i=0; i < lmb.memory.cnt; i++) {
235                 base = lmb.memory.region[i].base + KERNELBASE;
236                 size = lmb.memory.region[i].size;
237
238                 DBG("creating mapping for region: %lx : %lx\n", base, size);
239
240 #ifdef CONFIG_U3_DART
241                 /* Do not map the DART space. Fortunately, it will be aligned
242                  * in such a way that it will not cross two lmb regions and will
243                  * fit within a single 16Mb page.
244                  * The DART space is assumed to be a full 16Mb region even if we
245                  * only use 2Mb of that space. We will use more of it later for
246                  * AGP GART. We have to use a full 16Mb large page.
247                  */
248                 DBG("DART base: %lx\n", dart_tablebase);
249
250                 if (dart_tablebase != 0 && dart_tablebase >= base
251                     && dart_tablebase < (base + size)) {
252                         if (base != dart_tablebase)
253                                 create_pte_mapping(base, dart_tablebase, mode_rw,
254                                                    use_largepages);
255                         if ((base + size) > (dart_tablebase + 16*MB))
256                                 create_pte_mapping(dart_tablebase + 16*MB, base + size,
257                                                    mode_rw, use_largepages);
258                         continue;
259                 }
260 #endif /* CONFIG_U3_DART */
261                 create_pte_mapping(base, base + size, mode_rw, use_largepages);
262         }
263
264         /*
265          * If we have a memory_limit and we've allocated TCEs then we need to
266          * explicitly map the TCE area at the top of RAM. We also cope with the
267          * case that the TCEs start below memory_limit.
268          * tce_alloc_start/end are 16MB aligned so the mapping should work
269          * for either 4K or 16MB pages.
270          */
271         if (tce_alloc_start) {
272                 tce_alloc_start += KERNELBASE;
273                 tce_alloc_end += KERNELBASE;
274
275                 if (base + size >= tce_alloc_start)
276                         tce_alloc_start = base + size + 1;
277
278                 create_pte_mapping(tce_alloc_start, tce_alloc_end,
279                         mode_rw, use_largepages);
280         }
281
282         DBG(" <- htab_initialize()\n");
283 }
284 #undef KB
285 #undef MB
286
287 /*
288  * Called by asm hashtable.S for doing lazy icache flush
289  */
290 unsigned int hash_page_do_lazy_icache(unsigned int pp, pte_t pte, int trap)
291 {
292         struct page *page;
293
294         if (!pfn_valid(pte_pfn(pte)))
295                 return pp;
296
297         page = pte_page(pte);
298
299         /* page is dirty */
300         if (!test_bit(PG_arch_1, &page->flags) && !PageReserved(page)) {
301                 if (trap == 0x400) {
302                         __flush_dcache_icache(page_address(page));
303                         set_bit(PG_arch_1, &page->flags);
304                 } else
305                         pp |= HW_NO_EXEC;
306         }
307         return pp;
308 }
309
310 /* Result code is:
311  *  0 - handled
312  *  1 - normal page fault
313  * -1 - critical hash insertion error
314  */
315 int hash_page(unsigned long ea, unsigned long access, unsigned long trap)
316 {
317         void *pgdir;
318         unsigned long vsid;
319         struct mm_struct *mm;
320         pte_t *ptep;
321         int ret;
322         int user_region = 0;
323         int local = 0;
324         cpumask_t tmp;
325
326         if ((ea & ~REGION_MASK) >= PGTABLE_RANGE)
327                 return 1;
328
329         switch (REGION_ID(ea)) {
330         case USER_REGION_ID:
331                 user_region = 1;
332                 mm = current->mm;
333                 if (! mm)
334                         return 1;
335
336                 vsid = get_vsid(mm->context.id, ea);
337                 break;
338         case VMALLOC_REGION_ID:
339                 mm = &init_mm;
340                 vsid = get_kernel_vsid(ea);
341                 break;
342 #if 0
343         case KERNEL_REGION_ID:
344                 /*
345                  * Should never get here - entire 0xC0... region is bolted.
346                  * Send the problem up to do_page_fault 
347                  */
348 #endif
349         default:
350                 /* Not a valid range
351                  * Send the problem up to do_page_fault 
352                  */
353                 return 1;
354                 break;
355         }
356
357         pgdir = mm->pgd;
358
359         if (pgdir == NULL)
360                 return 1;
361
362         tmp = cpumask_of_cpu(smp_processor_id());
363         if (user_region && cpus_equal(mm->cpu_vm_mask, tmp))
364                 local = 1;
365
366         /* Is this a huge page ? */
367         if (unlikely(in_hugepage_area(mm->context, ea)))
368                 ret = hash_huge_page(mm, access, ea, vsid, local);
369         else {
370                 ptep = find_linux_pte(pgdir, ea);
371                 if (ptep == NULL)
372                         return 1;
373                 ret = __hash_page(ea, access, vsid, ptep, trap, local);
374         }
375
376         return ret;
377 }
378
379 void flush_hash_page(unsigned long va, pte_t pte, int local)
380 {
381         unsigned long vpn, hash, secondary, slot;
382         unsigned long huge = pte_huge(pte);
383
384         if (huge)
385                 vpn = va >> HPAGE_SHIFT;
386         else
387                 vpn = va >> PAGE_SHIFT;
388         hash = hpt_hash(vpn, huge);
389         secondary = (pte_val(pte) & _PAGE_SECONDARY) >> 15;
390         if (secondary)
391                 hash = ~hash;
392         slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
393         slot += (pte_val(pte) & _PAGE_GROUP_IX) >> 12;
394
395         ppc_md.hpte_invalidate(slot, va, huge, local);
396 }
397
398 void flush_hash_range(unsigned long number, int local)
399 {
400         if (ppc_md.flush_hash_range) {
401                 ppc_md.flush_hash_range(number, local);
402         } else {
403                 int i;
404                 struct ppc64_tlb_batch *batch =
405                         &__get_cpu_var(ppc64_tlb_batch);
406
407                 for (i = 0; i < number; i++)
408                         flush_hash_page(batch->vaddr[i], batch->pte[i], local);
409         }
410 }
411
412 static inline void make_bl(unsigned int *insn_addr, void *func)
413 {
414         unsigned long funcp = *((unsigned long *)func);
415         int offset = funcp - (unsigned long)insn_addr;
416
417         *insn_addr = (unsigned int)(0x48000001 | (offset & 0x03fffffc));
418         flush_icache_range((unsigned long)insn_addr, 4+
419                            (unsigned long)insn_addr);
420 }
421
422 /*
423  * low_hash_fault is called when we the low level hash code failed
424  * to instert a PTE due to an hypervisor error
425  */
426 void low_hash_fault(struct pt_regs *regs, unsigned long address)
427 {
428         if (user_mode(regs)) {
429                 siginfo_t info;
430
431                 info.si_signo = SIGBUS;
432                 info.si_errno = 0;
433                 info.si_code = BUS_ADRERR;
434                 info.si_addr = (void __user *)address;
435                 force_sig_info(SIGBUS, &info, current);
436                 return;
437         }
438         bad_page_fault(regs, address, SIGBUS);
439 }
440
441 void __init htab_finish_init(void)
442 {
443         extern unsigned int *htab_call_hpte_insert1;
444         extern unsigned int *htab_call_hpte_insert2;
445         extern unsigned int *htab_call_hpte_remove;
446         extern unsigned int *htab_call_hpte_updatepp;
447
448         make_bl(htab_call_hpte_insert1, ppc_md.hpte_insert);
449         make_bl(htab_call_hpte_insert2, ppc_md.hpte_insert);
450         make_bl(htab_call_hpte_remove, ppc_md.hpte_remove);
451         make_bl(htab_call_hpte_updatepp, ppc_md.hpte_updatepp);
452 }