]> err.no Git - linux-2.6/blob - arch/powerpc/mm/hash_utils_64.c
Merge branch 'linux-2.6'
[linux-2.6] / 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 #undef DEBUG_LOW
23
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 #include <linux/lmb.h>
35
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/prom.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/sections.h>
54 #include <asm/spu.h>
55 #include <asm/udbg.h>
56
57 #ifdef DEBUG
58 #define DBG(fmt...) udbg_printf(fmt)
59 #else
60 #define DBG(fmt...)
61 #endif
62
63 #ifdef DEBUG_LOW
64 #define DBG_LOW(fmt...) udbg_printf(fmt)
65 #else
66 #define DBG_LOW(fmt...)
67 #endif
68
69 #define KB (1024)
70 #define MB (1024*KB)
71
72 /*
73  * Note:  pte   --> Linux PTE
74  *        HPTE  --> PowerPC Hashed Page Table Entry
75  *
76  * Execution context:
77  *   htab_initialize is called with the MMU off (of course), but
78  *   the kernel has been copied down to zero so it can directly
79  *   reference global data.  At this point it is very difficult
80  *   to print debug info.
81  *
82  */
83
84 #ifdef CONFIG_U3_DART
85 extern unsigned long dart_tablebase;
86 #endif /* CONFIG_U3_DART */
87
88 static unsigned long _SDR1;
89 struct mmu_psize_def mmu_psize_defs[MMU_PAGE_COUNT];
90
91 struct hash_pte *htab_address;
92 unsigned long htab_size_bytes;
93 unsigned long htab_hash_mask;
94 int mmu_linear_psize = MMU_PAGE_4K;
95 int mmu_virtual_psize = MMU_PAGE_4K;
96 int mmu_vmalloc_psize = MMU_PAGE_4K;
97 int mmu_io_psize = MMU_PAGE_4K;
98 int mmu_kernel_ssize = MMU_SEGSIZE_256M;
99 int mmu_highuser_ssize = MMU_SEGSIZE_256M;
100 u16 mmu_slb_size = 64;
101 #ifdef CONFIG_HUGETLB_PAGE
102 int mmu_huge_psize = MMU_PAGE_16M;
103 unsigned int HPAGE_SHIFT;
104 #endif
105 #ifdef CONFIG_PPC_64K_PAGES
106 int mmu_ci_restrictions;
107 #endif
108 #ifdef CONFIG_DEBUG_PAGEALLOC
109 static u8 *linear_map_hash_slots;
110 static unsigned long linear_map_hash_count;
111 static DEFINE_SPINLOCK(linear_map_hash_lock);
112 #endif /* CONFIG_DEBUG_PAGEALLOC */
113
114 /* There are definitions of page sizes arrays to be used when none
115  * is provided by the firmware.
116  */
117
118 /* Pre-POWER4 CPUs (4k pages only)
119  */
120 struct mmu_psize_def mmu_psize_defaults_old[] = {
121         [MMU_PAGE_4K] = {
122                 .shift  = 12,
123                 .sllp   = 0,
124                 .penc   = 0,
125                 .avpnm  = 0,
126                 .tlbiel = 0,
127         },
128 };
129
130 /* POWER4, GPUL, POWER5
131  *
132  * Support for 16Mb large pages
133  */
134 struct mmu_psize_def mmu_psize_defaults_gp[] = {
135         [MMU_PAGE_4K] = {
136                 .shift  = 12,
137                 .sllp   = 0,
138                 .penc   = 0,
139                 .avpnm  = 0,
140                 .tlbiel = 1,
141         },
142         [MMU_PAGE_16M] = {
143                 .shift  = 24,
144                 .sllp   = SLB_VSID_L,
145                 .penc   = 0,
146                 .avpnm  = 0x1UL,
147                 .tlbiel = 0,
148         },
149 };
150
151
152 int htab_bolt_mapping(unsigned long vstart, unsigned long vend,
153                       unsigned long pstart, unsigned long mode,
154                       int psize, int ssize)
155 {
156         unsigned long vaddr, paddr;
157         unsigned int step, shift;
158         unsigned long tmp_mode;
159         int ret = 0;
160
161         shift = mmu_psize_defs[psize].shift;
162         step = 1 << shift;
163
164         for (vaddr = vstart, paddr = pstart; vaddr < vend;
165              vaddr += step, paddr += step) {
166                 unsigned long hash, hpteg;
167                 unsigned long vsid = get_kernel_vsid(vaddr, ssize);
168                 unsigned long va = hpt_va(vaddr, vsid, ssize);
169
170                 tmp_mode = mode;
171                 
172                 /* Make non-kernel text non-executable */
173                 if (!in_kernel_text(vaddr))
174                         tmp_mode = mode | HPTE_R_N;
175
176                 hash = hpt_hash(va, shift, ssize);
177                 hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
178
179                 DBG("htab_bolt_mapping: calling %p\n", ppc_md.hpte_insert);
180
181                 BUG_ON(!ppc_md.hpte_insert);
182                 ret = ppc_md.hpte_insert(hpteg, va, paddr,
183                                 tmp_mode, HPTE_V_BOLTED, psize, ssize);
184
185                 if (ret < 0)
186                         break;
187 #ifdef CONFIG_DEBUG_PAGEALLOC
188                 if ((paddr >> PAGE_SHIFT) < linear_map_hash_count)
189                         linear_map_hash_slots[paddr >> PAGE_SHIFT] = ret | 0x80;
190 #endif /* CONFIG_DEBUG_PAGEALLOC */
191         }
192         return ret < 0 ? ret : 0;
193 }
194
195 static void htab_remove_mapping(unsigned long vstart, unsigned long vend,
196                       int psize, int ssize)
197 {
198         unsigned long vaddr;
199         unsigned int step, shift;
200
201         shift = mmu_psize_defs[psize].shift;
202         step = 1 << shift;
203
204         if (!ppc_md.hpte_removebolted) {
205                 printk("Sub-arch doesn't implement hpte_removebolted\n");
206                 return;
207         }
208
209         for (vaddr = vstart; vaddr < vend; vaddr += step)
210                 ppc_md.hpte_removebolted(vaddr, psize, ssize);
211 }
212
213 static int __init htab_dt_scan_seg_sizes(unsigned long node,
214                                          const char *uname, int depth,
215                                          void *data)
216 {
217         char *type = of_get_flat_dt_prop(node, "device_type", NULL);
218         u32 *prop;
219         unsigned long size = 0;
220
221         /* We are scanning "cpu" nodes only */
222         if (type == NULL || strcmp(type, "cpu") != 0)
223                 return 0;
224
225         prop = (u32 *)of_get_flat_dt_prop(node, "ibm,processor-segment-sizes",
226                                           &size);
227         if (prop == NULL)
228                 return 0;
229         for (; size >= 4; size -= 4, ++prop) {
230                 if (prop[0] == 40) {
231                         DBG("1T segment support detected\n");
232                         cur_cpu_spec->cpu_features |= CPU_FTR_1T_SEGMENT;
233                         return 1;
234                 }
235         }
236         cur_cpu_spec->cpu_features &= ~CPU_FTR_NO_SLBIE_B;
237         return 0;
238 }
239
240 static void __init htab_init_seg_sizes(void)
241 {
242         of_scan_flat_dt(htab_dt_scan_seg_sizes, NULL);
243 }
244
245 static int __init htab_dt_scan_page_sizes(unsigned long node,
246                                           const char *uname, int depth,
247                                           void *data)
248 {
249         char *type = of_get_flat_dt_prop(node, "device_type", NULL);
250         u32 *prop;
251         unsigned long size = 0;
252
253         /* We are scanning "cpu" nodes only */
254         if (type == NULL || strcmp(type, "cpu") != 0)
255                 return 0;
256
257         prop = (u32 *)of_get_flat_dt_prop(node,
258                                           "ibm,segment-page-sizes", &size);
259         if (prop != NULL) {
260                 DBG("Page sizes from device-tree:\n");
261                 size /= 4;
262                 cur_cpu_spec->cpu_features &= ~(CPU_FTR_16M_PAGE);
263                 while(size > 0) {
264                         unsigned int shift = prop[0];
265                         unsigned int slbenc = prop[1];
266                         unsigned int lpnum = prop[2];
267                         unsigned int lpenc = 0;
268                         struct mmu_psize_def *def;
269                         int idx = -1;
270
271                         size -= 3; prop += 3;
272                         while(size > 0 && lpnum) {
273                                 if (prop[0] == shift)
274                                         lpenc = prop[1];
275                                 prop += 2; size -= 2;
276                                 lpnum--;
277                         }
278                         switch(shift) {
279                         case 0xc:
280                                 idx = MMU_PAGE_4K;
281                                 break;
282                         case 0x10:
283                                 idx = MMU_PAGE_64K;
284                                 break;
285                         case 0x14:
286                                 idx = MMU_PAGE_1M;
287                                 break;
288                         case 0x18:
289                                 idx = MMU_PAGE_16M;
290                                 cur_cpu_spec->cpu_features |= CPU_FTR_16M_PAGE;
291                                 break;
292                         case 0x22:
293                                 idx = MMU_PAGE_16G;
294                                 break;
295                         }
296                         if (idx < 0)
297                                 continue;
298                         def = &mmu_psize_defs[idx];
299                         def->shift = shift;
300                         if (shift <= 23)
301                                 def->avpnm = 0;
302                         else
303                                 def->avpnm = (1 << (shift - 23)) - 1;
304                         def->sllp = slbenc;
305                         def->penc = lpenc;
306                         /* We don't know for sure what's up with tlbiel, so
307                          * for now we only set it for 4K and 64K pages
308                          */
309                         if (idx == MMU_PAGE_4K || idx == MMU_PAGE_64K)
310                                 def->tlbiel = 1;
311                         else
312                                 def->tlbiel = 0;
313
314                         DBG(" %d: shift=%02x, sllp=%04x, avpnm=%08x, "
315                             "tlbiel=%d, penc=%d\n",
316                             idx, shift, def->sllp, def->avpnm, def->tlbiel,
317                             def->penc);
318                 }
319                 return 1;
320         }
321         return 0;
322 }
323
324 static void __init htab_init_page_sizes(void)
325 {
326         int rc;
327
328         /* Default to 4K pages only */
329         memcpy(mmu_psize_defs, mmu_psize_defaults_old,
330                sizeof(mmu_psize_defaults_old));
331
332         /*
333          * Try to find the available page sizes in the device-tree
334          */
335         rc = of_scan_flat_dt(htab_dt_scan_page_sizes, NULL);
336         if (rc != 0)  /* Found */
337                 goto found;
338
339         /*
340          * Not in the device-tree, let's fallback on known size
341          * list for 16M capable GP & GR
342          */
343         if (cpu_has_feature(CPU_FTR_16M_PAGE))
344                 memcpy(mmu_psize_defs, mmu_psize_defaults_gp,
345                        sizeof(mmu_psize_defaults_gp));
346  found:
347 #ifndef CONFIG_DEBUG_PAGEALLOC
348         /*
349          * Pick a size for the linear mapping. Currently, we only support
350          * 16M, 1M and 4K which is the default
351          */
352         if (mmu_psize_defs[MMU_PAGE_16M].shift)
353                 mmu_linear_psize = MMU_PAGE_16M;
354         else if (mmu_psize_defs[MMU_PAGE_1M].shift)
355                 mmu_linear_psize = MMU_PAGE_1M;
356 #endif /* CONFIG_DEBUG_PAGEALLOC */
357
358 #ifdef CONFIG_PPC_64K_PAGES
359         /*
360          * Pick a size for the ordinary pages. Default is 4K, we support
361          * 64K for user mappings and vmalloc if supported by the processor.
362          * We only use 64k for ioremap if the processor
363          * (and firmware) support cache-inhibited large pages.
364          * If not, we use 4k and set mmu_ci_restrictions so that
365          * hash_page knows to switch processes that use cache-inhibited
366          * mappings to 4k pages.
367          */
368         if (mmu_psize_defs[MMU_PAGE_64K].shift) {
369                 mmu_virtual_psize = MMU_PAGE_64K;
370                 mmu_vmalloc_psize = MMU_PAGE_64K;
371                 if (mmu_linear_psize == MMU_PAGE_4K)
372                         mmu_linear_psize = MMU_PAGE_64K;
373                 if (cpu_has_feature(CPU_FTR_CI_LARGE_PAGE)) {
374                         /*
375                          * Don't use 64k pages for ioremap on pSeries, since
376                          * that would stop us accessing the HEA ethernet.
377                          */
378                         if (!machine_is(pseries))
379                                 mmu_io_psize = MMU_PAGE_64K;
380                 } else
381                         mmu_ci_restrictions = 1;
382         }
383 #endif /* CONFIG_PPC_64K_PAGES */
384
385         printk(KERN_DEBUG "Page orders: linear mapping = %d, "
386                "virtual = %d, io = %d\n",
387                mmu_psize_defs[mmu_linear_psize].shift,
388                mmu_psize_defs[mmu_virtual_psize].shift,
389                mmu_psize_defs[mmu_io_psize].shift);
390
391 #ifdef CONFIG_HUGETLB_PAGE
392         /* Init large page size. Currently, we pick 16M or 1M depending
393          * on what is available
394          */
395         if (mmu_psize_defs[MMU_PAGE_16M].shift)
396                 set_huge_psize(MMU_PAGE_16M);
397         /* With 4k/4level pagetables, we can't (for now) cope with a
398          * huge page size < PMD_SIZE */
399         else if (mmu_psize_defs[MMU_PAGE_1M].shift)
400                 set_huge_psize(MMU_PAGE_1M);
401 #endif /* CONFIG_HUGETLB_PAGE */
402 }
403
404 static int __init htab_dt_scan_pftsize(unsigned long node,
405                                        const char *uname, int depth,
406                                        void *data)
407 {
408         char *type = of_get_flat_dt_prop(node, "device_type", NULL);
409         u32 *prop;
410
411         /* We are scanning "cpu" nodes only */
412         if (type == NULL || strcmp(type, "cpu") != 0)
413                 return 0;
414
415         prop = (u32 *)of_get_flat_dt_prop(node, "ibm,pft-size", NULL);
416         if (prop != NULL) {
417                 /* pft_size[0] is the NUMA CEC cookie */
418                 ppc64_pft_size = prop[1];
419                 return 1;
420         }
421         return 0;
422 }
423
424 static unsigned long __init htab_get_table_size(void)
425 {
426         unsigned long mem_size, rnd_mem_size, pteg_count;
427
428         /* If hash size isn't already provided by the platform, we try to
429          * retrieve it from the device-tree. If it's not there neither, we
430          * calculate it now based on the total RAM size
431          */
432         if (ppc64_pft_size == 0)
433                 of_scan_flat_dt(htab_dt_scan_pftsize, NULL);
434         if (ppc64_pft_size)
435                 return 1UL << ppc64_pft_size;
436
437         /* round mem_size up to next power of 2 */
438         mem_size = lmb_phys_mem_size();
439         rnd_mem_size = 1UL << __ilog2(mem_size);
440         if (rnd_mem_size < mem_size)
441                 rnd_mem_size <<= 1;
442
443         /* # pages / 2 */
444         pteg_count = max(rnd_mem_size >> (12 + 1), 1UL << 11);
445
446         return pteg_count << 7;
447 }
448
449 #ifdef CONFIG_MEMORY_HOTPLUG
450 void create_section_mapping(unsigned long start, unsigned long end)
451 {
452                 BUG_ON(htab_bolt_mapping(start, end, __pa(start),
453                         _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_COHERENT | PP_RWXX,
454                         mmu_linear_psize, mmu_kernel_ssize));
455 }
456
457 void remove_section_mapping(unsigned long start, unsigned long end)
458 {
459         htab_remove_mapping(start, end, mmu_linear_psize, mmu_kernel_ssize);
460 }
461 #endif /* CONFIG_MEMORY_HOTPLUG */
462
463 static inline void make_bl(unsigned int *insn_addr, void *func)
464 {
465         unsigned long funcp = *((unsigned long *)func);
466         int offset = funcp - (unsigned long)insn_addr;
467
468         *insn_addr = (unsigned int)(0x48000001 | (offset & 0x03fffffc));
469         flush_icache_range((unsigned long)insn_addr, 4+
470                            (unsigned long)insn_addr);
471 }
472
473 static void __init htab_finish_init(void)
474 {
475         extern unsigned int *htab_call_hpte_insert1;
476         extern unsigned int *htab_call_hpte_insert2;
477         extern unsigned int *htab_call_hpte_remove;
478         extern unsigned int *htab_call_hpte_updatepp;
479
480 #ifdef CONFIG_PPC_HAS_HASH_64K
481         extern unsigned int *ht64_call_hpte_insert1;
482         extern unsigned int *ht64_call_hpte_insert2;
483         extern unsigned int *ht64_call_hpte_remove;
484         extern unsigned int *ht64_call_hpte_updatepp;
485
486         make_bl(ht64_call_hpte_insert1, ppc_md.hpte_insert);
487         make_bl(ht64_call_hpte_insert2, ppc_md.hpte_insert);
488         make_bl(ht64_call_hpte_remove, ppc_md.hpte_remove);
489         make_bl(ht64_call_hpte_updatepp, ppc_md.hpte_updatepp);
490 #endif /* CONFIG_PPC_HAS_HASH_64K */
491
492         make_bl(htab_call_hpte_insert1, ppc_md.hpte_insert);
493         make_bl(htab_call_hpte_insert2, ppc_md.hpte_insert);
494         make_bl(htab_call_hpte_remove, ppc_md.hpte_remove);
495         make_bl(htab_call_hpte_updatepp, ppc_md.hpte_updatepp);
496 }
497
498 void __init htab_initialize(void)
499 {
500         unsigned long table;
501         unsigned long pteg_count;
502         unsigned long mode_rw;
503         unsigned long base = 0, size = 0, limit;
504         int i;
505
506         extern unsigned long tce_alloc_start, tce_alloc_end;
507
508         DBG(" -> htab_initialize()\n");
509
510         /* Initialize segment sizes */
511         htab_init_seg_sizes();
512
513         /* Initialize page sizes */
514         htab_init_page_sizes();
515
516         if (cpu_has_feature(CPU_FTR_1T_SEGMENT)) {
517                 mmu_kernel_ssize = MMU_SEGSIZE_1T;
518                 mmu_highuser_ssize = MMU_SEGSIZE_1T;
519                 printk(KERN_INFO "Using 1TB segments\n");
520         }
521
522         /*
523          * Calculate the required size of the htab.  We want the number of
524          * PTEGs to equal one half the number of real pages.
525          */ 
526         htab_size_bytes = htab_get_table_size();
527         pteg_count = htab_size_bytes >> 7;
528
529         htab_hash_mask = pteg_count - 1;
530
531         if (firmware_has_feature(FW_FEATURE_LPAR)) {
532                 /* Using a hypervisor which owns the htab */
533                 htab_address = NULL;
534                 _SDR1 = 0; 
535         } else {
536                 /* Find storage for the HPT.  Must be contiguous in
537                  * the absolute address space. On cell we want it to be
538                  * in the first 2 Gig so we can use it for IOMMU hacks.
539                  */
540                 if (machine_is(cell))
541                         limit = 0x80000000;
542                 else
543                         limit = 0;
544
545                 table = lmb_alloc_base(htab_size_bytes, htab_size_bytes, limit);
546
547                 DBG("Hash table allocated at %lx, size: %lx\n", table,
548                     htab_size_bytes);
549
550                 htab_address = abs_to_virt(table);
551
552                 /* htab absolute addr + encoded htabsize */
553                 _SDR1 = table + __ilog2(pteg_count) - 11;
554
555                 /* Initialize the HPT with no entries */
556                 memset((void *)table, 0, htab_size_bytes);
557
558                 /* Set SDR1 */
559                 mtspr(SPRN_SDR1, _SDR1);
560         }
561
562         mode_rw = _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_COHERENT | PP_RWXX;
563
564 #ifdef CONFIG_DEBUG_PAGEALLOC
565         linear_map_hash_count = lmb_end_of_DRAM() >> PAGE_SHIFT;
566         linear_map_hash_slots = __va(lmb_alloc_base(linear_map_hash_count,
567                                                     1, lmb.rmo_size));
568         memset(linear_map_hash_slots, 0, linear_map_hash_count);
569 #endif /* CONFIG_DEBUG_PAGEALLOC */
570
571         /* On U3 based machines, we need to reserve the DART area and
572          * _NOT_ map it to avoid cache paradoxes as it's remapped non
573          * cacheable later on
574          */
575
576         /* create bolted the linear mapping in the hash table */
577         for (i=0; i < lmb.memory.cnt; i++) {
578                 base = (unsigned long)__va(lmb.memory.region[i].base);
579                 size = lmb.memory.region[i].size;
580
581                 DBG("creating mapping for region: %lx : %lx\n", base, size);
582
583 #ifdef CONFIG_U3_DART
584                 /* Do not map the DART space. Fortunately, it will be aligned
585                  * in such a way that it will not cross two lmb regions and
586                  * will fit within a single 16Mb page.
587                  * The DART space is assumed to be a full 16Mb region even if
588                  * we only use 2Mb of that space. We will use more of it later
589                  * for AGP GART. We have to use a full 16Mb large page.
590                  */
591                 DBG("DART base: %lx\n", dart_tablebase);
592
593                 if (dart_tablebase != 0 && dart_tablebase >= base
594                     && dart_tablebase < (base + size)) {
595                         unsigned long dart_table_end = dart_tablebase + 16 * MB;
596                         if (base != dart_tablebase)
597                                 BUG_ON(htab_bolt_mapping(base, dart_tablebase,
598                                                         __pa(base), mode_rw,
599                                                         mmu_linear_psize,
600                                                         mmu_kernel_ssize));
601                         if ((base + size) > dart_table_end)
602                                 BUG_ON(htab_bolt_mapping(dart_tablebase+16*MB,
603                                                         base + size,
604                                                         __pa(dart_table_end),
605                                                          mode_rw,
606                                                          mmu_linear_psize,
607                                                          mmu_kernel_ssize));
608                         continue;
609                 }
610 #endif /* CONFIG_U3_DART */
611                 BUG_ON(htab_bolt_mapping(base, base + size, __pa(base),
612                                 mode_rw, mmu_linear_psize, mmu_kernel_ssize));
613        }
614
615         /*
616          * If we have a memory_limit and we've allocated TCEs then we need to
617          * explicitly map the TCE area at the top of RAM. We also cope with the
618          * case that the TCEs start below memory_limit.
619          * tce_alloc_start/end are 16MB aligned so the mapping should work
620          * for either 4K or 16MB pages.
621          */
622         if (tce_alloc_start) {
623                 tce_alloc_start = (unsigned long)__va(tce_alloc_start);
624                 tce_alloc_end = (unsigned long)__va(tce_alloc_end);
625
626                 if (base + size >= tce_alloc_start)
627                         tce_alloc_start = base + size + 1;
628
629                 BUG_ON(htab_bolt_mapping(tce_alloc_start, tce_alloc_end,
630                                          __pa(tce_alloc_start), mode_rw,
631                                          mmu_linear_psize, mmu_kernel_ssize));
632         }
633
634         htab_finish_init();
635
636         DBG(" <- htab_initialize()\n");
637 }
638 #undef KB
639 #undef MB
640
641 void htab_initialize_secondary(void)
642 {
643         if (!firmware_has_feature(FW_FEATURE_LPAR))
644                 mtspr(SPRN_SDR1, _SDR1);
645 }
646
647 /*
648  * Called by asm hashtable.S for doing lazy icache flush
649  */
650 unsigned int hash_page_do_lazy_icache(unsigned int pp, pte_t pte, int trap)
651 {
652         struct page *page;
653
654         if (!pfn_valid(pte_pfn(pte)))
655                 return pp;
656
657         page = pte_page(pte);
658
659         /* page is dirty */
660         if (!test_bit(PG_arch_1, &page->flags) && !PageReserved(page)) {
661                 if (trap == 0x400) {
662                         __flush_dcache_icache(page_address(page));
663                         set_bit(PG_arch_1, &page->flags);
664                 } else
665                         pp |= HPTE_R_N;
666         }
667         return pp;
668 }
669
670 /*
671  * Demote a segment to using 4k pages.
672  * For now this makes the whole process use 4k pages.
673  */
674 #ifdef CONFIG_PPC_64K_PAGES
675 void demote_segment_4k(struct mm_struct *mm, unsigned long addr)
676 {
677         if (mm->context.user_psize == MMU_PAGE_4K)
678                 return;
679         slice_set_user_psize(mm, MMU_PAGE_4K);
680 #ifdef CONFIG_SPU_BASE
681         spu_flush_all_slbs(mm);
682 #endif
683         if (get_paca()->context.user_psize != MMU_PAGE_4K) {
684                 get_paca()->context = mm->context;
685                 slb_flush_and_rebolt();
686         }
687 }
688 #endif /* CONFIG_PPC_64K_PAGES */
689
690 #ifdef CONFIG_PPC_SUBPAGE_PROT
691 /*
692  * This looks up a 2-bit protection code for a 4k subpage of a 64k page.
693  * Userspace sets the subpage permissions using the subpage_prot system call.
694  *
695  * Result is 0: full permissions, _PAGE_RW: read-only,
696  * _PAGE_USER or _PAGE_USER|_PAGE_RW: no access.
697  */
698 static int subpage_protection(pgd_t *pgdir, unsigned long ea)
699 {
700         struct subpage_prot_table *spt = pgd_subpage_prot(pgdir);
701         u32 spp = 0;
702         u32 **sbpm, *sbpp;
703
704         if (ea >= spt->maxaddr)
705                 return 0;
706         if (ea < 0x100000000) {
707                 /* addresses below 4GB use spt->low_prot */
708                 sbpm = spt->low_prot;
709         } else {
710                 sbpm = spt->protptrs[ea >> SBP_L3_SHIFT];
711                 if (!sbpm)
712                         return 0;
713         }
714         sbpp = sbpm[(ea >> SBP_L2_SHIFT) & (SBP_L2_COUNT - 1)];
715         if (!sbpp)
716                 return 0;
717         spp = sbpp[(ea >> PAGE_SHIFT) & (SBP_L1_COUNT - 1)];
718
719         /* extract 2-bit bitfield for this 4k subpage */
720         spp >>= 30 - 2 * ((ea >> 12) & 0xf);
721
722         /* turn 0,1,2,3 into combination of _PAGE_USER and _PAGE_RW */
723         spp = ((spp & 2) ? _PAGE_USER : 0) | ((spp & 1) ? _PAGE_RW : 0);
724         return spp;
725 }
726
727 #else /* CONFIG_PPC_SUBPAGE_PROT */
728 static inline int subpage_protection(pgd_t *pgdir, unsigned long ea)
729 {
730         return 0;
731 }
732 #endif
733
734 /* Result code is:
735  *  0 - handled
736  *  1 - normal page fault
737  * -1 - critical hash insertion error
738  * -2 - access not permitted by subpage protection mechanism
739  */
740 int hash_page(unsigned long ea, unsigned long access, unsigned long trap)
741 {
742         void *pgdir;
743         unsigned long vsid;
744         struct mm_struct *mm;
745         pte_t *ptep;
746         cpumask_t tmp;
747         int rc, user_region = 0, local = 0;
748         int psize, ssize;
749
750         DBG_LOW("hash_page(ea=%016lx, access=%lx, trap=%lx\n",
751                 ea, access, trap);
752
753         if ((ea & ~REGION_MASK) >= PGTABLE_RANGE) {
754                 DBG_LOW(" out of pgtable range !\n");
755                 return 1;
756         }
757
758         /* Get region & vsid */
759         switch (REGION_ID(ea)) {
760         case USER_REGION_ID:
761                 user_region = 1;
762                 mm = current->mm;
763                 if (! mm) {
764                         DBG_LOW(" user region with no mm !\n");
765                         return 1;
766                 }
767 #ifdef CONFIG_PPC_MM_SLICES
768                 psize = get_slice_psize(mm, ea);
769 #else
770                 psize = mm->context.user_psize;
771 #endif
772                 ssize = user_segment_size(ea);
773                 vsid = get_vsid(mm->context.id, ea, ssize);
774                 break;
775         case VMALLOC_REGION_ID:
776                 mm = &init_mm;
777                 vsid = get_kernel_vsid(ea, mmu_kernel_ssize);
778                 if (ea < VMALLOC_END)
779                         psize = mmu_vmalloc_psize;
780                 else
781                         psize = mmu_io_psize;
782                 ssize = mmu_kernel_ssize;
783                 break;
784         default:
785                 /* Not a valid range
786                  * Send the problem up to do_page_fault 
787                  */
788                 return 1;
789         }
790         DBG_LOW(" mm=%p, mm->pgdir=%p, vsid=%016lx\n", mm, mm->pgd, vsid);
791
792         /* Get pgdir */
793         pgdir = mm->pgd;
794         if (pgdir == NULL)
795                 return 1;
796
797         /* Check CPU locality */
798         tmp = cpumask_of_cpu(smp_processor_id());
799         if (user_region && cpus_equal(mm->cpu_vm_mask, tmp))
800                 local = 1;
801
802 #ifdef CONFIG_HUGETLB_PAGE
803         /* Handle hugepage regions */
804         if (HPAGE_SHIFT && psize == mmu_huge_psize) {
805                 DBG_LOW(" -> huge page !\n");
806                 return hash_huge_page(mm, access, ea, vsid, local, trap);
807         }
808 #endif /* CONFIG_HUGETLB_PAGE */
809
810 #ifndef CONFIG_PPC_64K_PAGES
811         /* If we use 4K pages and our psize is not 4K, then we are hitting
812          * a special driver mapping, we need to align the address before
813          * we fetch the PTE
814          */
815         if (psize != MMU_PAGE_4K)
816                 ea &= ~((1ul << mmu_psize_defs[psize].shift) - 1);
817 #endif /* CONFIG_PPC_64K_PAGES */
818
819         /* Get PTE and page size from page tables */
820         ptep = find_linux_pte(pgdir, ea);
821         if (ptep == NULL || !pte_present(*ptep)) {
822                 DBG_LOW(" no PTE !\n");
823                 return 1;
824         }
825
826 #ifndef CONFIG_PPC_64K_PAGES
827         DBG_LOW(" i-pte: %016lx\n", pte_val(*ptep));
828 #else
829         DBG_LOW(" i-pte: %016lx %016lx\n", pte_val(*ptep),
830                 pte_val(*(ptep + PTRS_PER_PTE)));
831 #endif
832         /* Pre-check access permissions (will be re-checked atomically
833          * in __hash_page_XX but this pre-check is a fast path
834          */
835         if (access & ~pte_val(*ptep)) {
836                 DBG_LOW(" no access !\n");
837                 return 1;
838         }
839
840         /* Do actual hashing */
841 #ifdef CONFIG_PPC_64K_PAGES
842         /* If _PAGE_4K_PFN is set, make sure this is a 4k segment */
843         if (pte_val(*ptep) & _PAGE_4K_PFN) {
844                 demote_segment_4k(mm, ea);
845                 psize = MMU_PAGE_4K;
846         }
847
848         /* If this PTE is non-cacheable and we have restrictions on
849          * using non cacheable large pages, then we switch to 4k
850          */
851         if (mmu_ci_restrictions && psize == MMU_PAGE_64K &&
852             (pte_val(*ptep) & _PAGE_NO_CACHE)) {
853                 if (user_region) {
854                         demote_segment_4k(mm, ea);
855                         psize = MMU_PAGE_4K;
856                 } else if (ea < VMALLOC_END) {
857                         /*
858                          * some driver did a non-cacheable mapping
859                          * in vmalloc space, so switch vmalloc
860                          * to 4k pages
861                          */
862                         printk(KERN_ALERT "Reducing vmalloc segment "
863                                "to 4kB pages because of "
864                                "non-cacheable mapping\n");
865                         psize = mmu_vmalloc_psize = MMU_PAGE_4K;
866 #ifdef CONFIG_SPU_BASE
867                         spu_flush_all_slbs(mm);
868 #endif
869                 }
870         }
871         if (user_region) {
872                 if (psize != get_paca()->context.user_psize) {
873                         get_paca()->context = mm->context;
874                         slb_flush_and_rebolt();
875                 }
876         } else if (get_paca()->vmalloc_sllp !=
877                    mmu_psize_defs[mmu_vmalloc_psize].sllp) {
878                 get_paca()->vmalloc_sllp =
879                         mmu_psize_defs[mmu_vmalloc_psize].sllp;
880                 slb_vmalloc_update();
881         }
882 #endif /* CONFIG_PPC_64K_PAGES */
883
884 #ifdef CONFIG_PPC_HAS_HASH_64K
885         if (psize == MMU_PAGE_64K)
886                 rc = __hash_page_64K(ea, access, vsid, ptep, trap, local, ssize);
887         else
888 #endif /* CONFIG_PPC_HAS_HASH_64K */
889         {
890                 int spp = subpage_protection(pgdir, ea);
891                 if (access & spp)
892                         rc = -2;
893                 else
894                         rc = __hash_page_4K(ea, access, vsid, ptep, trap,
895                                             local, ssize, spp);
896         }
897
898 #ifndef CONFIG_PPC_64K_PAGES
899         DBG_LOW(" o-pte: %016lx\n", pte_val(*ptep));
900 #else
901         DBG_LOW(" o-pte: %016lx %016lx\n", pte_val(*ptep),
902                 pte_val(*(ptep + PTRS_PER_PTE)));
903 #endif
904         DBG_LOW(" -> rc=%d\n", rc);
905         return rc;
906 }
907 EXPORT_SYMBOL_GPL(hash_page);
908
909 void hash_preload(struct mm_struct *mm, unsigned long ea,
910                   unsigned long access, unsigned long trap)
911 {
912         unsigned long vsid;
913         void *pgdir;
914         pte_t *ptep;
915         cpumask_t mask;
916         unsigned long flags;
917         int local = 0;
918         int ssize;
919
920         BUG_ON(REGION_ID(ea) != USER_REGION_ID);
921
922 #ifdef CONFIG_PPC_MM_SLICES
923         /* We only prefault standard pages for now */
924         if (unlikely(get_slice_psize(mm, ea) != mm->context.user_psize))
925                 return;
926 #endif
927
928         DBG_LOW("hash_preload(mm=%p, mm->pgdir=%p, ea=%016lx, access=%lx,"
929                 " trap=%lx\n", mm, mm->pgd, ea, access, trap);
930
931         /* Get Linux PTE if available */
932         pgdir = mm->pgd;
933         if (pgdir == NULL)
934                 return;
935         ptep = find_linux_pte(pgdir, ea);
936         if (!ptep)
937                 return;
938
939 #ifdef CONFIG_PPC_64K_PAGES
940         /* If either _PAGE_4K_PFN or _PAGE_NO_CACHE is set (and we are on
941          * a 64K kernel), then we don't preload, hash_page() will take
942          * care of it once we actually try to access the page.
943          * That way we don't have to duplicate all of the logic for segment
944          * page size demotion here
945          */
946         if (pte_val(*ptep) & (_PAGE_4K_PFN | _PAGE_NO_CACHE))
947                 return;
948 #endif /* CONFIG_PPC_64K_PAGES */
949
950         /* Get VSID */
951         ssize = user_segment_size(ea);
952         vsid = get_vsid(mm->context.id, ea, ssize);
953
954         /* Hash doesn't like irqs */
955         local_irq_save(flags);
956
957         /* Is that local to this CPU ? */
958         mask = cpumask_of_cpu(smp_processor_id());
959         if (cpus_equal(mm->cpu_vm_mask, mask))
960                 local = 1;
961
962         /* Hash it in */
963 #ifdef CONFIG_PPC_HAS_HASH_64K
964         if (mm->context.user_psize == MMU_PAGE_64K)
965                 __hash_page_64K(ea, access, vsid, ptep, trap, local, ssize);
966         else
967 #endif /* CONFIG_PPC_HAS_HASH_64K */
968                 __hash_page_4K(ea, access, vsid, ptep, trap, local, ssize,
969                                subpage_protection(pgdir, ea));
970
971         local_irq_restore(flags);
972 }
973
974 /* WARNING: This is called from hash_low_64.S, if you change this prototype,
975  *          do not forget to update the assembly call site !
976  */
977 void flush_hash_page(unsigned long va, real_pte_t pte, int psize, int ssize,
978                      int local)
979 {
980         unsigned long hash, index, shift, hidx, slot;
981
982         DBG_LOW("flush_hash_page(va=%016x)\n", va);
983         pte_iterate_hashed_subpages(pte, psize, va, index, shift) {
984                 hash = hpt_hash(va, shift, ssize);
985                 hidx = __rpte_to_hidx(pte, index);
986                 if (hidx & _PTEIDX_SECONDARY)
987                         hash = ~hash;
988                 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
989                 slot += hidx & _PTEIDX_GROUP_IX;
990                 DBG_LOW(" sub %d: hash=%x, hidx=%x\n", index, slot, hidx);
991                 ppc_md.hpte_invalidate(slot, va, psize, ssize, local);
992         } pte_iterate_hashed_end();
993 }
994
995 void flush_hash_range(unsigned long number, int local)
996 {
997         if (ppc_md.flush_hash_range)
998                 ppc_md.flush_hash_range(number, local);
999         else {
1000                 int i;
1001                 struct ppc64_tlb_batch *batch =
1002                         &__get_cpu_var(ppc64_tlb_batch);
1003
1004                 for (i = 0; i < number; i++)
1005                         flush_hash_page(batch->vaddr[i], batch->pte[i],
1006                                         batch->psize, batch->ssize, local);
1007         }
1008 }
1009
1010 /*
1011  * low_hash_fault is called when we the low level hash code failed
1012  * to instert a PTE due to an hypervisor error
1013  */
1014 void low_hash_fault(struct pt_regs *regs, unsigned long address, int rc)
1015 {
1016         if (user_mode(regs)) {
1017 #ifdef CONFIG_PPC_SUBPAGE_PROT
1018                 if (rc == -2)
1019                         _exception(SIGSEGV, regs, SEGV_ACCERR, address);
1020                 else
1021 #endif
1022                         _exception(SIGBUS, regs, BUS_ADRERR, address);
1023         } else
1024                 bad_page_fault(regs, address, SIGBUS);
1025 }
1026
1027 #ifdef CONFIG_DEBUG_PAGEALLOC
1028 static void kernel_map_linear_page(unsigned long vaddr, unsigned long lmi)
1029 {
1030         unsigned long hash, hpteg;
1031         unsigned long vsid = get_kernel_vsid(vaddr, mmu_kernel_ssize);
1032         unsigned long va = hpt_va(vaddr, vsid, mmu_kernel_ssize);
1033         unsigned long mode = _PAGE_ACCESSED | _PAGE_DIRTY |
1034                 _PAGE_COHERENT | PP_RWXX | HPTE_R_N;
1035         int ret;
1036
1037         hash = hpt_hash(va, PAGE_SHIFT, mmu_kernel_ssize);
1038         hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
1039
1040         ret = ppc_md.hpte_insert(hpteg, va, __pa(vaddr),
1041                                  mode, HPTE_V_BOLTED,
1042                                  mmu_linear_psize, mmu_kernel_ssize);
1043         BUG_ON (ret < 0);
1044         spin_lock(&linear_map_hash_lock);
1045         BUG_ON(linear_map_hash_slots[lmi] & 0x80);
1046         linear_map_hash_slots[lmi] = ret | 0x80;
1047         spin_unlock(&linear_map_hash_lock);
1048 }
1049
1050 static void kernel_unmap_linear_page(unsigned long vaddr, unsigned long lmi)
1051 {
1052         unsigned long hash, hidx, slot;
1053         unsigned long vsid = get_kernel_vsid(vaddr, mmu_kernel_ssize);
1054         unsigned long va = hpt_va(vaddr, vsid, mmu_kernel_ssize);
1055
1056         hash = hpt_hash(va, PAGE_SHIFT, mmu_kernel_ssize);
1057         spin_lock(&linear_map_hash_lock);
1058         BUG_ON(!(linear_map_hash_slots[lmi] & 0x80));
1059         hidx = linear_map_hash_slots[lmi] & 0x7f;
1060         linear_map_hash_slots[lmi] = 0;
1061         spin_unlock(&linear_map_hash_lock);
1062         if (hidx & _PTEIDX_SECONDARY)
1063                 hash = ~hash;
1064         slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
1065         slot += hidx & _PTEIDX_GROUP_IX;
1066         ppc_md.hpte_invalidate(slot, va, mmu_linear_psize, mmu_kernel_ssize, 0);
1067 }
1068
1069 void kernel_map_pages(struct page *page, int numpages, int enable)
1070 {
1071         unsigned long flags, vaddr, lmi;
1072         int i;
1073
1074         local_irq_save(flags);
1075         for (i = 0; i < numpages; i++, page++) {
1076                 vaddr = (unsigned long)page_address(page);
1077                 lmi = __pa(vaddr) >> PAGE_SHIFT;
1078                 if (lmi >= linear_map_hash_count)
1079                         continue;
1080                 if (enable)
1081                         kernel_map_linear_page(vaddr, lmi);
1082                 else
1083                         kernel_unmap_linear_page(vaddr, lmi);
1084         }
1085         local_irq_restore(flags);
1086 }
1087 #endif /* CONFIG_DEBUG_PAGEALLOC */