ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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 #include <linux/config.h>
22 #include <linux/spinlock.h>
23 #include <linux/errno.h>
24 #include <linux/sched.h>
25 #include <linux/proc_fs.h>
26 #include <linux/stat.h>
27 #include <linux/sysctl.h>
28 #include <linux/ctype.h>
29 #include <linux/cache.h>
30 #include <linux/init.h>
31
32 #include <asm/ppcdebug.h>
33 #include <asm/processor.h>
34 #include <asm/pgtable.h>
35 #include <asm/mmu.h>
36 #include <asm/mmu_context.h>
37 #include <asm/page.h>
38 #include <asm/types.h>
39 #include <asm/system.h>
40 #include <asm/uaccess.h>
41 #include <asm/naca.h>
42 #include <asm/machdep.h>
43 #include <asm/lmb.h>
44 #include <asm/abs_addr.h>
45 #include <asm/tlbflush.h>
46 #include <asm/io.h>
47 #include <asm/eeh.h>
48 #include <asm/tlb.h>
49 #include <asm/cacheflush.h>
50 #include <asm/cputable.h>
51 #include <asm/abs_addr.h>
52
53 /*
54  * Note:  pte   --> Linux PTE
55  *        HPTE  --> PowerPC Hashed Page Table Entry
56  *
57  * Execution context:
58  *   htab_initialize is called with the MMU off (of course), but
59  *   the kernel has been copied down to zero so it can directly
60  *   reference global data.  At this point it is very difficult
61  *   to print debug info.
62  *
63  */
64
65 #ifdef CONFIG_PMAC_DART
66 extern unsigned long dart_tablebase;
67 #endif /* CONFIG_PMAC_DART */
68
69 HTAB htab_data = {NULL, 0, 0, 0, 0};
70
71 extern unsigned long _SDR1;
72
73 #define KB (1024)
74 #define MB (1024*KB)
75
76 static inline void loop_forever(void)
77 {
78         volatile unsigned long x = 1;
79         for(;x;x|=1)
80                 ;
81 }
82
83 #ifdef CONFIG_PPC_PSERIES
84 static inline void create_pte_mapping(unsigned long start, unsigned long end,
85                                       unsigned long mode, int large)
86 {
87         unsigned long addr;
88         unsigned int step;
89
90         if (large)
91                 step = 16*MB;
92         else
93                 step = 4*KB;
94
95         for (addr = start; addr < end; addr += step) {
96                 unsigned long vpn, hash, hpteg;
97                 unsigned long vsid = get_kernel_vsid(addr);
98                 unsigned long va = (vsid << 28) | (addr & 0xfffffff);
99                 int ret;
100
101                 if (large)
102                         vpn = va >> LARGE_PAGE_SHIFT;
103                 else
104                         vpn = va >> PAGE_SHIFT;
105
106                 hash = hpt_hash(vpn, large);
107
108                 hpteg = ((hash & htab_data.htab_hash_mask)*HPTES_PER_GROUP);
109
110                 if (systemcfg->platform == PLATFORM_PSERIES_LPAR)
111                         ret = pSeries_lpar_hpte_insert(hpteg, va,
112                                 virt_to_abs(addr) >> PAGE_SHIFT,
113                                 0, mode, 1, large);
114                 else
115                         ret = pSeries_hpte_insert(hpteg, va,
116                                 virt_to_abs(addr) >> PAGE_SHIFT,
117                                 0, mode, 1, large);
118
119                 if (ret == -1) {
120                         ppc64_terminate_msg(0x20, "create_pte_mapping");
121                         loop_forever();
122                 }
123         }
124 }
125
126 void __init htab_initialize(void)
127 {
128         unsigned long table, htab_size_bytes;
129         unsigned long pteg_count;
130         unsigned long mode_rw;
131         int i, use_largepages = 0;
132
133         /*
134          * Calculate the required size of the htab.  We want the number of
135          * PTEGs to equal one half the number of real pages.
136          */ 
137         htab_size_bytes = 1UL << naca->pftSize;
138         pteg_count = htab_size_bytes >> 7;
139
140         /* For debug, make the HTAB 1/8 as big as it normally would be. */
141         ifppcdebug(PPCDBG_HTABSIZE) {
142                 pteg_count >>= 3;
143                 htab_size_bytes = pteg_count << 7;
144         }
145
146         htab_data.htab_num_ptegs = pteg_count;
147         htab_data.htab_hash_mask = pteg_count - 1;
148
149         if (systemcfg->platform == PLATFORM_PSERIES ||
150             systemcfg->platform == PLATFORM_POWERMAC) {
151                 /* Find storage for the HPT.  Must be contiguous in
152                  * the absolute address space.
153                  */
154                 table = lmb_alloc(htab_size_bytes, htab_size_bytes);
155                 if ( !table ) {
156                         ppc64_terminate_msg(0x20, "hpt space");
157                         loop_forever();
158                 }
159                 htab_data.htab = abs_to_virt(table);
160
161                 /* htab absolute addr + encoded htabsize */
162                 _SDR1 = table + __ilog2(pteg_count) - 11;
163
164                 /* Initialize the HPT with no entries */
165                 memset((void *)table, 0, htab_size_bytes);
166         } else {
167                 /* Using a hypervisor which owns the htab */
168                 htab_data.htab = NULL;
169                 _SDR1 = 0; 
170         }
171
172         mode_rw = _PAGE_ACCESSED | _PAGE_COHERENT | PP_RWXX;
173
174         /* On U3 based machines, we need to reserve the DART area and
175          * _NOT_ map it to avoid cache paradoxes as it's remapped non
176          * cacheable later on
177          */
178         if (cur_cpu_spec->cpu_features & CPU_FTR_16M_PAGE)
179                 use_largepages = 1;
180
181         /* add all physical memory to the bootmem map */
182         for (i=0; i < lmb.memory.cnt; i++) {
183                 unsigned long base, size;
184
185                 base = lmb.memory.region[i].physbase + KERNELBASE;
186                 size = lmb.memory.region[i].size;
187
188 #ifdef CONFIG_PMAC_DART
189                 /* Do not map the DART space. Fortunately, it will be aligned
190                  * in such a way that it will not cross two lmb regions and will
191                  * fit within a single 16Mb page.
192                  * The DART space is assumed to be a full 16Mb region even if we
193                  * only use 2Mb of that space. We will use more of it later for
194                  * AGP GART. We have to use a full 16Mb large page.
195                  */
196                 if (dart_tablebase != 0 && dart_tablebase >= base
197                     && dart_tablebase < (base + size)) {
198                         if (base != dart_tablebase)
199                                 create_pte_mapping(base, dart_tablebase, mode_rw,
200                                                    use_largepages);
201                         if ((base + size) > (dart_tablebase + 16*MB))
202                                 create_pte_mapping(dart_tablebase + 16*MB, base + size,
203                                                    mode_rw, use_largepages);
204                         continue;
205                 }
206 #endif /* CONFIG_PMAC_DART */
207                 create_pte_mapping(base, base + size, mode_rw, use_largepages);
208         }
209 }
210 #undef KB
211 #undef MB
212 #endif
213
214 /*
215  * Called by asm hashtable.S for doing lazy icache flush
216  */
217 unsigned int hash_page_do_lazy_icache(unsigned int pp, pte_t pte, int trap)
218 {
219         struct page *page;
220
221 #define PPC64_HWNOEXEC (1 << 2)
222
223         if (!pfn_valid(pte_pfn(pte)))
224                 return pp;
225
226         page = pte_page(pte);
227
228         /* page is dirty */
229         if (!test_bit(PG_arch_1, &page->flags) && !PageReserved(page)) {
230                 if (trap == 0x400) {
231                         __flush_dcache_icache(page_address(page));
232                         set_bit(PG_arch_1, &page->flags);
233                 } else
234                         pp |= PPC64_HWNOEXEC;
235         }
236         return pp;
237 }
238
239 /*
240  * Called by asm hashtable.S in case of critical insert failure
241  */
242 void htab_insert_failure(void)
243 {
244         panic("hash_page: pte_insert failed\n");
245 }
246
247 int hash_page(unsigned long ea, unsigned long access, unsigned long trap)
248 {
249         void *pgdir;
250         unsigned long vsid;
251         struct mm_struct *mm;
252         pte_t *ptep;
253         int ret;
254         int user_region = 0;
255         int local = 0;
256         cpumask_t tmp;
257
258         /* Check for invalid addresses. */
259         if (!IS_VALID_EA(ea))
260                 return 1;
261
262         switch (REGION_ID(ea)) {
263         case USER_REGION_ID:
264                 user_region = 1;
265                 mm = current->mm;
266                 if (mm == NULL)
267                         return 1;
268
269                 vsid = get_vsid(mm->context.id, ea);
270                 break;
271         case IO_REGION_ID:
272                 mm = &ioremap_mm;
273                 vsid = get_kernel_vsid(ea);
274                 break;
275         case VMALLOC_REGION_ID:
276                 mm = &init_mm;
277                 vsid = get_kernel_vsid(ea);
278                 break;
279 #if 0
280         case EEH_REGION_ID:
281                 /*
282                  * Should only be hit if there is an access to MMIO space
283                  * which is protected by EEH.
284                  * Send the problem up to do_page_fault 
285                  */
286         case KERNEL_REGION_ID:
287                 /*
288                  * Should never get here - entire 0xC0... region is bolted.
289                  * Send the problem up to do_page_fault 
290                  */
291 #endif
292         default:
293                 /* Not a valid range
294                  * Send the problem up to do_page_fault 
295                  */
296                 return 1;
297                 break;
298         }
299
300         pgdir = mm->pgd;
301
302         if (pgdir == NULL)
303                 return 1;
304
305         tmp = cpumask_of_cpu(smp_processor_id());
306         if (user_region && cpus_equal(mm->cpu_vm_mask, tmp))
307                 local = 1;
308
309         /* Is this a huge page ? */
310         if (unlikely(in_hugepage_area(mm->context, ea)))
311                 ret = hash_huge_page(mm, access, ea, vsid, local);
312         else {
313                 ptep = find_linux_pte(pgdir, ea);
314                 if (ptep == NULL)
315                         return 1;
316                 ret = __hash_page(ea, access, vsid, ptep, trap, local);
317         }
318
319
320         return ret;
321 }
322
323 void flush_hash_page(unsigned long context, unsigned long ea, pte_t pte,
324                      int local)
325 {
326         unsigned long vsid, vpn, va, hash, secondary, slot;
327
328         /* XXX fix for large ptes */
329         unsigned long large = 0;
330
331         if ((ea >= USER_START) && (ea <= USER_END))
332                 vsid = get_vsid(context, ea);
333         else
334                 vsid = get_kernel_vsid(ea);
335
336         va = (vsid << 28) | (ea & 0x0fffffff);
337         if (large)
338                 vpn = va >> LARGE_PAGE_SHIFT;
339         else
340                 vpn = va >> PAGE_SHIFT;
341         hash = hpt_hash(vpn, large);
342         secondary = (pte_val(pte) & _PAGE_SECONDARY) >> 15;
343         if (secondary)
344                 hash = ~hash;
345         slot = (hash & htab_data.htab_hash_mask) * HPTES_PER_GROUP;
346         slot += (pte_val(pte) & _PAGE_GROUP_IX) >> 12;
347
348         ppc_md.hpte_invalidate(slot, va, large, local);
349 }
350
351 void flush_hash_range(unsigned long context, unsigned long number, int local)
352 {
353         if (ppc_md.flush_hash_range) {
354                 ppc_md.flush_hash_range(context, number, local);
355         } else {
356                 int i;
357                 struct ppc64_tlb_batch *batch = &__get_cpu_var(ppc64_tlb_batch);
358
359                 for (i = 0; i < number; i++)
360                         flush_hash_page(context, batch->addr[i], batch->pte[i],
361                                         local);
362         }
363 }
364
365 static inline void make_bl(unsigned int *insn_addr, void *func)
366 {
367         unsigned long funcp = *((unsigned long *)func);
368         int offset = funcp - (unsigned long)insn_addr;
369
370         *insn_addr = (unsigned int)(0x48000001 | (offset & 0x03fffffc));
371         flush_icache_range((unsigned long)insn_addr, 4+
372                            (unsigned long)insn_addr);
373 }
374
375 void __init htab_finish_init(void)
376 {
377         extern unsigned int *htab_call_hpte_insert1;
378         extern unsigned int *htab_call_hpte_insert2;
379         extern unsigned int *htab_call_hpte_remove;
380         extern unsigned int *htab_call_hpte_updatepp;
381
382         make_bl(htab_call_hpte_insert1, ppc_md.hpte_insert);
383         make_bl(htab_call_hpte_insert2, ppc_md.hpte_insert);
384         make_bl(htab_call_hpte_remove, ppc_md.hpte_remove);
385         make_bl(htab_call_hpte_updatepp, ppc_md.hpte_updatepp);
386 }