[POWERPC] Add code for removing HPTEs for parts of the linear mapping
[safe/jmp/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                         mmu_io_psize = MMU_PAGE_64K;
375                 else
376                         mmu_ci_restrictions = 1;
377         }
378 #endif /* CONFIG_PPC_64K_PAGES */
379
380         printk(KERN_DEBUG "Page orders: linear mapping = %d, "
381                "virtual = %d, io = %d\n",
382                mmu_psize_defs[mmu_linear_psize].shift,
383                mmu_psize_defs[mmu_virtual_psize].shift,
384                mmu_psize_defs[mmu_io_psize].shift);
385
386 #ifdef CONFIG_HUGETLB_PAGE
387         /* Init large page size. Currently, we pick 16M or 1M depending
388          * on what is available
389          */
390         if (mmu_psize_defs[MMU_PAGE_16M].shift)
391                 set_huge_psize(MMU_PAGE_16M);
392         /* With 4k/4level pagetables, we can't (for now) cope with a
393          * huge page size < PMD_SIZE */
394         else if (mmu_psize_defs[MMU_PAGE_1M].shift)
395                 set_huge_psize(MMU_PAGE_1M);
396 #endif /* CONFIG_HUGETLB_PAGE */
397 }
398
399 static int __init htab_dt_scan_pftsize(unsigned long node,
400                                        const char *uname, int depth,
401                                        void *data)
402 {
403         char *type = of_get_flat_dt_prop(node, "device_type", NULL);
404         u32 *prop;
405
406         /* We are scanning "cpu" nodes only */
407         if (type == NULL || strcmp(type, "cpu") != 0)
408                 return 0;
409
410         prop = (u32 *)of_get_flat_dt_prop(node, "ibm,pft-size", NULL);
411         if (prop != NULL) {
412                 /* pft_size[0] is the NUMA CEC cookie */
413                 ppc64_pft_size = prop[1];
414                 return 1;
415         }
416         return 0;
417 }
418
419 static unsigned long __init htab_get_table_size(void)
420 {
421         unsigned long mem_size, rnd_mem_size, pteg_count;
422
423         /* If hash size isn't already provided by the platform, we try to
424          * retrieve it from the device-tree. If it's not there neither, we
425          * calculate it now based on the total RAM size
426          */
427         if (ppc64_pft_size == 0)
428                 of_scan_flat_dt(htab_dt_scan_pftsize, NULL);
429         if (ppc64_pft_size)
430                 return 1UL << ppc64_pft_size;
431
432         /* round mem_size up to next power of 2 */
433         mem_size = lmb_phys_mem_size();
434         rnd_mem_size = 1UL << __ilog2(mem_size);
435         if (rnd_mem_size < mem_size)
436                 rnd_mem_size <<= 1;
437
438         /* # pages / 2 */
439         pteg_count = max(rnd_mem_size >> (12 + 1), 1UL << 11);
440
441         return pteg_count << 7;
442 }
443
444 #ifdef CONFIG_MEMORY_HOTPLUG
445 void create_section_mapping(unsigned long start, unsigned long end)
446 {
447                 BUG_ON(htab_bolt_mapping(start, end, __pa(start),
448                         _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_COHERENT | PP_RWXX,
449                         mmu_linear_psize, mmu_kernel_ssize));
450 }
451
452 void remove_section_mapping(unsigned long start, unsigned long end)
453 {
454         htab_remove_mapping(start, end, mmu_linear_psize, mmu_kernel_ssize);
455 }
456 #endif /* CONFIG_MEMORY_HOTPLUG */
457
458 static inline void make_bl(unsigned int *insn_addr, void *func)
459 {
460         unsigned long funcp = *((unsigned long *)func);
461         int offset = funcp - (unsigned long)insn_addr;
462
463         *insn_addr = (unsigned int)(0x48000001 | (offset & 0x03fffffc));
464         flush_icache_range((unsigned long)insn_addr, 4+
465                            (unsigned long)insn_addr);
466 }
467
468 static void __init htab_finish_init(void)
469 {
470         extern unsigned int *htab_call_hpte_insert1;
471         extern unsigned int *htab_call_hpte_insert2;
472         extern unsigned int *htab_call_hpte_remove;
473         extern unsigned int *htab_call_hpte_updatepp;
474
475 #ifdef CONFIG_PPC_HAS_HASH_64K
476         extern unsigned int *ht64_call_hpte_insert1;
477         extern unsigned int *ht64_call_hpte_insert2;
478         extern unsigned int *ht64_call_hpte_remove;
479         extern unsigned int *ht64_call_hpte_updatepp;
480
481         make_bl(ht64_call_hpte_insert1, ppc_md.hpte_insert);
482         make_bl(ht64_call_hpte_insert2, ppc_md.hpte_insert);
483         make_bl(ht64_call_hpte_remove, ppc_md.hpte_remove);
484         make_bl(ht64_call_hpte_updatepp, ppc_md.hpte_updatepp);
485 #endif /* CONFIG_PPC_HAS_HASH_64K */
486
487         make_bl(htab_call_hpte_insert1, ppc_md.hpte_insert);
488         make_bl(htab_call_hpte_insert2, ppc_md.hpte_insert);
489         make_bl(htab_call_hpte_remove, ppc_md.hpte_remove);
490         make_bl(htab_call_hpte_updatepp, ppc_md.hpte_updatepp);
491 }
492
493 void __init htab_initialize(void)
494 {
495         unsigned long table;
496         unsigned long pteg_count;
497         unsigned long mode_rw;
498         unsigned long base = 0, size = 0, limit;
499         int i;
500
501         extern unsigned long tce_alloc_start, tce_alloc_end;
502
503         DBG(" -> htab_initialize()\n");
504
505         /* Initialize segment sizes */
506         htab_init_seg_sizes();
507
508         /* Initialize page sizes */
509         htab_init_page_sizes();
510
511         if (cpu_has_feature(CPU_FTR_1T_SEGMENT)) {
512                 mmu_kernel_ssize = MMU_SEGSIZE_1T;
513                 mmu_highuser_ssize = MMU_SEGSIZE_1T;
514                 printk(KERN_INFO "Using 1TB segments\n");
515         }
516
517         /*
518          * Calculate the required size of the htab.  We want the number of
519          * PTEGs to equal one half the number of real pages.
520          */ 
521         htab_size_bytes = htab_get_table_size();
522         pteg_count = htab_size_bytes >> 7;
523
524         htab_hash_mask = pteg_count - 1;
525
526         if (firmware_has_feature(FW_FEATURE_LPAR)) {
527                 /* Using a hypervisor which owns the htab */
528                 htab_address = NULL;
529                 _SDR1 = 0; 
530         } else {
531                 /* Find storage for the HPT.  Must be contiguous in
532                  * the absolute address space. On cell we want it to be
533                  * in the first 1 Gig.
534                  */
535                 if (machine_is(cell))
536                         limit = 0x40000000;
537                 else
538                         limit = 0;
539
540                 table = lmb_alloc_base(htab_size_bytes, htab_size_bytes, limit);
541
542                 DBG("Hash table allocated at %lx, size: %lx\n", table,
543                     htab_size_bytes);
544
545                 htab_address = abs_to_virt(table);
546
547                 /* htab absolute addr + encoded htabsize */
548                 _SDR1 = table + __ilog2(pteg_count) - 11;
549
550                 /* Initialize the HPT with no entries */
551                 memset((void *)table, 0, htab_size_bytes);
552
553                 /* Set SDR1 */
554                 mtspr(SPRN_SDR1, _SDR1);
555         }
556
557         mode_rw = _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_COHERENT | PP_RWXX;
558
559 #ifdef CONFIG_DEBUG_PAGEALLOC
560         linear_map_hash_count = lmb_end_of_DRAM() >> PAGE_SHIFT;
561         linear_map_hash_slots = __va(lmb_alloc_base(linear_map_hash_count,
562                                                     1, lmb.rmo_size));
563         memset(linear_map_hash_slots, 0, linear_map_hash_count);
564 #endif /* CONFIG_DEBUG_PAGEALLOC */
565
566         /* On U3 based machines, we need to reserve the DART area and
567          * _NOT_ map it to avoid cache paradoxes as it's remapped non
568          * cacheable later on
569          */
570
571         /* create bolted the linear mapping in the hash table */
572         for (i=0; i < lmb.memory.cnt; i++) {
573                 base = (unsigned long)__va(lmb.memory.region[i].base);
574                 size = lmb.memory.region[i].size;
575
576                 DBG("creating mapping for region: %lx : %lx\n", base, size);
577
578 #ifdef CONFIG_U3_DART
579                 /* Do not map the DART space. Fortunately, it will be aligned
580                  * in such a way that it will not cross two lmb regions and
581                  * will fit within a single 16Mb page.
582                  * The DART space is assumed to be a full 16Mb region even if
583                  * we only use 2Mb of that space. We will use more of it later
584                  * for AGP GART. We have to use a full 16Mb large page.
585                  */
586                 DBG("DART base: %lx\n", dart_tablebase);
587
588                 if (dart_tablebase != 0 && dart_tablebase >= base
589                     && dart_tablebase < (base + size)) {
590                         unsigned long dart_table_end = dart_tablebase + 16 * MB;
591                         if (base != dart_tablebase)
592                                 BUG_ON(htab_bolt_mapping(base, dart_tablebase,
593                                                         __pa(base), mode_rw,
594                                                         mmu_linear_psize,
595                                                         mmu_kernel_ssize));
596                         if ((base + size) > dart_table_end)
597                                 BUG_ON(htab_bolt_mapping(dart_tablebase+16*MB,
598                                                         base + size,
599                                                         __pa(dart_table_end),
600                                                          mode_rw,
601                                                          mmu_linear_psize,
602                                                          mmu_kernel_ssize));
603                         continue;
604                 }
605 #endif /* CONFIG_U3_DART */
606                 BUG_ON(htab_bolt_mapping(base, base + size, __pa(base),
607                                 mode_rw, mmu_linear_psize, mmu_kernel_ssize));
608        }
609
610         /*
611          * If we have a memory_limit and we've allocated TCEs then we need to
612          * explicitly map the TCE area at the top of RAM. We also cope with the
613          * case that the TCEs start below memory_limit.
614          * tce_alloc_start/end are 16MB aligned so the mapping should work
615          * for either 4K or 16MB pages.
616          */
617         if (tce_alloc_start) {
618                 tce_alloc_start = (unsigned long)__va(tce_alloc_start);
619                 tce_alloc_end = (unsigned long)__va(tce_alloc_end);
620
621                 if (base + size >= tce_alloc_start)
622                         tce_alloc_start = base + size + 1;
623
624                 BUG_ON(htab_bolt_mapping(tce_alloc_start, tce_alloc_end,
625                                          __pa(tce_alloc_start), mode_rw,
626                                          mmu_linear_psize, mmu_kernel_ssize));
627         }
628
629         htab_finish_init();
630
631         DBG(" <- htab_initialize()\n");
632 }
633 #undef KB
634 #undef MB
635
636 void htab_initialize_secondary(void)
637 {
638         if (!firmware_has_feature(FW_FEATURE_LPAR))
639                 mtspr(SPRN_SDR1, _SDR1);
640 }
641
642 /*
643  * Called by asm hashtable.S for doing lazy icache flush
644  */
645 unsigned int hash_page_do_lazy_icache(unsigned int pp, pte_t pte, int trap)
646 {
647         struct page *page;
648
649         if (!pfn_valid(pte_pfn(pte)))
650                 return pp;
651
652         page = pte_page(pte);
653
654         /* page is dirty */
655         if (!test_bit(PG_arch_1, &page->flags) && !PageReserved(page)) {
656                 if (trap == 0x400) {
657                         __flush_dcache_icache(page_address(page));
658                         set_bit(PG_arch_1, &page->flags);
659                 } else
660                         pp |= HPTE_R_N;
661         }
662         return pp;
663 }
664
665 /*
666  * Demote a segment to using 4k pages.
667  * For now this makes the whole process use 4k pages.
668  */
669 #ifdef CONFIG_PPC_64K_PAGES
670 void demote_segment_4k(struct mm_struct *mm, unsigned long addr)
671 {
672         if (mm->context.user_psize == MMU_PAGE_4K)
673                 return;
674         slice_set_user_psize(mm, MMU_PAGE_4K);
675 #ifdef CONFIG_SPU_BASE
676         spu_flush_all_slbs(mm);
677 #endif
678         if (get_paca()->context.user_psize != MMU_PAGE_4K) {
679                 get_paca()->context = mm->context;
680                 slb_flush_and_rebolt();
681         }
682 }
683 #endif /* CONFIG_PPC_64K_PAGES */
684
685 #ifdef CONFIG_PPC_SUBPAGE_PROT
686 /*
687  * This looks up a 2-bit protection code for a 4k subpage of a 64k page.
688  * Userspace sets the subpage permissions using the subpage_prot system call.
689  *
690  * Result is 0: full permissions, _PAGE_RW: read-only,
691  * _PAGE_USER or _PAGE_USER|_PAGE_RW: no access.
692  */
693 static int subpage_protection(pgd_t *pgdir, unsigned long ea)
694 {
695         struct subpage_prot_table *spt = pgd_subpage_prot(pgdir);
696         u32 spp = 0;
697         u32 **sbpm, *sbpp;
698
699         if (ea >= spt->maxaddr)
700                 return 0;
701         if (ea < 0x100000000) {
702                 /* addresses below 4GB use spt->low_prot */
703                 sbpm = spt->low_prot;
704         } else {
705                 sbpm = spt->protptrs[ea >> SBP_L3_SHIFT];
706                 if (!sbpm)
707                         return 0;
708         }
709         sbpp = sbpm[(ea >> SBP_L2_SHIFT) & (SBP_L2_COUNT - 1)];
710         if (!sbpp)
711                 return 0;
712         spp = sbpp[(ea >> PAGE_SHIFT) & (SBP_L1_COUNT - 1)];
713
714         /* extract 2-bit bitfield for this 4k subpage */
715         spp >>= 30 - 2 * ((ea >> 12) & 0xf);
716
717         /* turn 0,1,2,3 into combination of _PAGE_USER and _PAGE_RW */
718         spp = ((spp & 2) ? _PAGE_USER : 0) | ((spp & 1) ? _PAGE_RW : 0);
719         return spp;
720 }
721
722 #else /* CONFIG_PPC_SUBPAGE_PROT */
723 static inline int subpage_protection(pgd_t *pgdir, unsigned long ea)
724 {
725         return 0;
726 }
727 #endif
728
729 /* Result code is:
730  *  0 - handled
731  *  1 - normal page fault
732  * -1 - critical hash insertion error
733  * -2 - access not permitted by subpage protection mechanism
734  */
735 int hash_page(unsigned long ea, unsigned long access, unsigned long trap)
736 {
737         void *pgdir;
738         unsigned long vsid;
739         struct mm_struct *mm;
740         pte_t *ptep;
741         cpumask_t tmp;
742         int rc, user_region = 0, local = 0;
743         int psize, ssize;
744
745         DBG_LOW("hash_page(ea=%016lx, access=%lx, trap=%lx\n",
746                 ea, access, trap);
747
748         if ((ea & ~REGION_MASK) >= PGTABLE_RANGE) {
749                 DBG_LOW(" out of pgtable range !\n");
750                 return 1;
751         }
752
753         /* Get region & vsid */
754         switch (REGION_ID(ea)) {
755         case USER_REGION_ID:
756                 user_region = 1;
757                 mm = current->mm;
758                 if (! mm) {
759                         DBG_LOW(" user region with no mm !\n");
760                         return 1;
761                 }
762 #ifdef CONFIG_PPC_MM_SLICES
763                 psize = get_slice_psize(mm, ea);
764 #else
765                 psize = mm->context.user_psize;
766 #endif
767                 ssize = user_segment_size(ea);
768                 vsid = get_vsid(mm->context.id, ea, ssize);
769                 break;
770         case VMALLOC_REGION_ID:
771                 mm = &init_mm;
772                 vsid = get_kernel_vsid(ea, mmu_kernel_ssize);
773                 if (ea < VMALLOC_END)
774                         psize = mmu_vmalloc_psize;
775                 else
776                         psize = mmu_io_psize;
777                 ssize = mmu_kernel_ssize;
778                 break;
779         default:
780                 /* Not a valid range
781                  * Send the problem up to do_page_fault 
782                  */
783                 return 1;
784         }
785         DBG_LOW(" mm=%p, mm->pgdir=%p, vsid=%016lx\n", mm, mm->pgd, vsid);
786
787         /* Get pgdir */
788         pgdir = mm->pgd;
789         if (pgdir == NULL)
790                 return 1;
791
792         /* Check CPU locality */
793         tmp = cpumask_of_cpu(smp_processor_id());
794         if (user_region && cpus_equal(mm->cpu_vm_mask, tmp))
795                 local = 1;
796
797 #ifdef CONFIG_HUGETLB_PAGE
798         /* Handle hugepage regions */
799         if (HPAGE_SHIFT && psize == mmu_huge_psize) {
800                 DBG_LOW(" -> huge page !\n");
801                 return hash_huge_page(mm, access, ea, vsid, local, trap);
802         }
803 #endif /* CONFIG_HUGETLB_PAGE */
804
805 #ifndef CONFIG_PPC_64K_PAGES
806         /* If we use 4K pages and our psize is not 4K, then we are hitting
807          * a special driver mapping, we need to align the address before
808          * we fetch the PTE
809          */
810         if (psize != MMU_PAGE_4K)
811                 ea &= ~((1ul << mmu_psize_defs[psize].shift) - 1);
812 #endif /* CONFIG_PPC_64K_PAGES */
813
814         /* Get PTE and page size from page tables */
815         ptep = find_linux_pte(pgdir, ea);
816         if (ptep == NULL || !pte_present(*ptep)) {
817                 DBG_LOW(" no PTE !\n");
818                 return 1;
819         }
820
821 #ifndef CONFIG_PPC_64K_PAGES
822         DBG_LOW(" i-pte: %016lx\n", pte_val(*ptep));
823 #else
824         DBG_LOW(" i-pte: %016lx %016lx\n", pte_val(*ptep),
825                 pte_val(*(ptep + PTRS_PER_PTE)));
826 #endif
827         /* Pre-check access permissions (will be re-checked atomically
828          * in __hash_page_XX but this pre-check is a fast path
829          */
830         if (access & ~pte_val(*ptep)) {
831                 DBG_LOW(" no access !\n");
832                 return 1;
833         }
834
835         /* Do actual hashing */
836 #ifdef CONFIG_PPC_64K_PAGES
837         /* If _PAGE_4K_PFN is set, make sure this is a 4k segment */
838         if (pte_val(*ptep) & _PAGE_4K_PFN) {
839                 demote_segment_4k(mm, ea);
840                 psize = MMU_PAGE_4K;
841         }
842
843         /* If this PTE is non-cacheable and we have restrictions on
844          * using non cacheable large pages, then we switch to 4k
845          */
846         if (mmu_ci_restrictions && psize == MMU_PAGE_64K &&
847             (pte_val(*ptep) & _PAGE_NO_CACHE)) {
848                 if (user_region) {
849                         demote_segment_4k(mm, ea);
850                         psize = MMU_PAGE_4K;
851                 } else if (ea < VMALLOC_END) {
852                         /*
853                          * some driver did a non-cacheable mapping
854                          * in vmalloc space, so switch vmalloc
855                          * to 4k pages
856                          */
857                         printk(KERN_ALERT "Reducing vmalloc segment "
858                                "to 4kB pages because of "
859                                "non-cacheable mapping\n");
860                         psize = mmu_vmalloc_psize = MMU_PAGE_4K;
861 #ifdef CONFIG_SPU_BASE
862                         spu_flush_all_slbs(mm);
863 #endif
864                 }
865         }
866         if (user_region) {
867                 if (psize != get_paca()->context.user_psize) {
868                         get_paca()->context = mm->context;
869                         slb_flush_and_rebolt();
870                 }
871         } else if (get_paca()->vmalloc_sllp !=
872                    mmu_psize_defs[mmu_vmalloc_psize].sllp) {
873                 get_paca()->vmalloc_sllp =
874                         mmu_psize_defs[mmu_vmalloc_psize].sllp;
875                 slb_vmalloc_update();
876         }
877 #endif /* CONFIG_PPC_64K_PAGES */
878
879 #ifdef CONFIG_PPC_HAS_HASH_64K
880         if (psize == MMU_PAGE_64K)
881                 rc = __hash_page_64K(ea, access, vsid, ptep, trap, local, ssize);
882         else
883 #endif /* CONFIG_PPC_HAS_HASH_64K */
884         {
885                 int spp = subpage_protection(pgdir, ea);
886                 if (access & spp)
887                         rc = -2;
888                 else
889                         rc = __hash_page_4K(ea, access, vsid, ptep, trap,
890                                             local, ssize, spp);
891         }
892
893 #ifndef CONFIG_PPC_64K_PAGES
894         DBG_LOW(" o-pte: %016lx\n", pte_val(*ptep));
895 #else
896         DBG_LOW(" o-pte: %016lx %016lx\n", pte_val(*ptep),
897                 pte_val(*(ptep + PTRS_PER_PTE)));
898 #endif
899         DBG_LOW(" -> rc=%d\n", rc);
900         return rc;
901 }
902 EXPORT_SYMBOL_GPL(hash_page);
903
904 void hash_preload(struct mm_struct *mm, unsigned long ea,
905                   unsigned long access, unsigned long trap)
906 {
907         unsigned long vsid;
908         void *pgdir;
909         pte_t *ptep;
910         cpumask_t mask;
911         unsigned long flags;
912         int local = 0;
913         int ssize;
914
915         BUG_ON(REGION_ID(ea) != USER_REGION_ID);
916
917 #ifdef CONFIG_PPC_MM_SLICES
918         /* We only prefault standard pages for now */
919         if (unlikely(get_slice_psize(mm, ea) != mm->context.user_psize))
920                 return;
921 #endif
922
923         DBG_LOW("hash_preload(mm=%p, mm->pgdir=%p, ea=%016lx, access=%lx,"
924                 " trap=%lx\n", mm, mm->pgd, ea, access, trap);
925
926         /* Get Linux PTE if available */
927         pgdir = mm->pgd;
928         if (pgdir == NULL)
929                 return;
930         ptep = find_linux_pte(pgdir, ea);
931         if (!ptep)
932                 return;
933
934 #ifdef CONFIG_PPC_64K_PAGES
935         /* If either _PAGE_4K_PFN or _PAGE_NO_CACHE is set (and we are on
936          * a 64K kernel), then we don't preload, hash_page() will take
937          * care of it once we actually try to access the page.
938          * That way we don't have to duplicate all of the logic for segment
939          * page size demotion here
940          */
941         if (pte_val(*ptep) & (_PAGE_4K_PFN | _PAGE_NO_CACHE))
942                 return;
943 #endif /* CONFIG_PPC_64K_PAGES */
944
945         /* Get VSID */
946         ssize = user_segment_size(ea);
947         vsid = get_vsid(mm->context.id, ea, ssize);
948
949         /* Hash doesn't like irqs */
950         local_irq_save(flags);
951
952         /* Is that local to this CPU ? */
953         mask = cpumask_of_cpu(smp_processor_id());
954         if (cpus_equal(mm->cpu_vm_mask, mask))
955                 local = 1;
956
957         /* Hash it in */
958 #ifdef CONFIG_PPC_HAS_HASH_64K
959         if (mm->context.user_psize == MMU_PAGE_64K)
960                 __hash_page_64K(ea, access, vsid, ptep, trap, local, ssize);
961         else
962 #endif /* CONFIG_PPC_HAS_HASH_64K */
963                 __hash_page_4K(ea, access, vsid, ptep, trap, local, ssize,
964                                subpage_protection(pgdir, ea));
965
966         local_irq_restore(flags);
967 }
968
969 /* WARNING: This is called from hash_low_64.S, if you change this prototype,
970  *          do not forget to update the assembly call site !
971  */
972 void flush_hash_page(unsigned long va, real_pte_t pte, int psize, int ssize,
973                      int local)
974 {
975         unsigned long hash, index, shift, hidx, slot;
976
977         DBG_LOW("flush_hash_page(va=%016x)\n", va);
978         pte_iterate_hashed_subpages(pte, psize, va, index, shift) {
979                 hash = hpt_hash(va, shift, ssize);
980                 hidx = __rpte_to_hidx(pte, index);
981                 if (hidx & _PTEIDX_SECONDARY)
982                         hash = ~hash;
983                 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
984                 slot += hidx & _PTEIDX_GROUP_IX;
985                 DBG_LOW(" sub %d: hash=%x, hidx=%x\n", index, slot, hidx);
986                 ppc_md.hpte_invalidate(slot, va, psize, ssize, local);
987         } pte_iterate_hashed_end();
988 }
989
990 void flush_hash_range(unsigned long number, int local)
991 {
992         if (ppc_md.flush_hash_range)
993                 ppc_md.flush_hash_range(number, local);
994         else {
995                 int i;
996                 struct ppc64_tlb_batch *batch =
997                         &__get_cpu_var(ppc64_tlb_batch);
998
999                 for (i = 0; i < number; i++)
1000                         flush_hash_page(batch->vaddr[i], batch->pte[i],
1001                                         batch->psize, batch->ssize, local);
1002         }
1003 }
1004
1005 /*
1006  * low_hash_fault is called when we the low level hash code failed
1007  * to instert a PTE due to an hypervisor error
1008  */
1009 void low_hash_fault(struct pt_regs *regs, unsigned long address, int rc)
1010 {
1011         if (user_mode(regs)) {
1012 #ifdef CONFIG_PPC_SUBPAGE_PROT
1013                 if (rc == -2)
1014                         _exception(SIGSEGV, regs, SEGV_ACCERR, address);
1015                 else
1016 #endif
1017                         _exception(SIGBUS, regs, BUS_ADRERR, address);
1018         } else
1019                 bad_page_fault(regs, address, SIGBUS);
1020 }
1021
1022 #ifdef CONFIG_DEBUG_PAGEALLOC
1023 static void kernel_map_linear_page(unsigned long vaddr, unsigned long lmi)
1024 {
1025         unsigned long hash, hpteg;
1026         unsigned long vsid = get_kernel_vsid(vaddr, mmu_kernel_ssize);
1027         unsigned long va = hpt_va(vaddr, vsid, mmu_kernel_ssize);
1028         unsigned long mode = _PAGE_ACCESSED | _PAGE_DIRTY |
1029                 _PAGE_COHERENT | PP_RWXX | HPTE_R_N;
1030         int ret;
1031
1032         hash = hpt_hash(va, PAGE_SHIFT, mmu_kernel_ssize);
1033         hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
1034
1035         ret = ppc_md.hpte_insert(hpteg, va, __pa(vaddr),
1036                                  mode, HPTE_V_BOLTED,
1037                                  mmu_linear_psize, mmu_kernel_ssize);
1038         BUG_ON (ret < 0);
1039         spin_lock(&linear_map_hash_lock);
1040         BUG_ON(linear_map_hash_slots[lmi] & 0x80);
1041         linear_map_hash_slots[lmi] = ret | 0x80;
1042         spin_unlock(&linear_map_hash_lock);
1043 }
1044
1045 static void kernel_unmap_linear_page(unsigned long vaddr, unsigned long lmi)
1046 {
1047         unsigned long hash, hidx, slot;
1048         unsigned long vsid = get_kernel_vsid(vaddr, mmu_kernel_ssize);
1049         unsigned long va = hpt_va(vaddr, vsid, mmu_kernel_ssize);
1050
1051         hash = hpt_hash(va, PAGE_SHIFT, mmu_kernel_ssize);
1052         spin_lock(&linear_map_hash_lock);
1053         BUG_ON(!(linear_map_hash_slots[lmi] & 0x80));
1054         hidx = linear_map_hash_slots[lmi] & 0x7f;
1055         linear_map_hash_slots[lmi] = 0;
1056         spin_unlock(&linear_map_hash_lock);
1057         if (hidx & _PTEIDX_SECONDARY)
1058                 hash = ~hash;
1059         slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
1060         slot += hidx & _PTEIDX_GROUP_IX;
1061         ppc_md.hpte_invalidate(slot, va, mmu_linear_psize, mmu_kernel_ssize, 0);
1062 }
1063
1064 void kernel_map_pages(struct page *page, int numpages, int enable)
1065 {
1066         unsigned long flags, vaddr, lmi;
1067         int i;
1068
1069         local_irq_save(flags);
1070         for (i = 0; i < numpages; i++, page++) {
1071                 vaddr = (unsigned long)page_address(page);
1072                 lmi = __pa(vaddr) >> PAGE_SHIFT;
1073                 if (lmi >= linear_map_hash_count)
1074                         continue;
1075                 if (enable)
1076                         kernel_map_linear_page(vaddr, lmi);
1077                 else
1078                         kernel_unmap_linear_page(vaddr, lmi);
1079         }
1080         local_irq_restore(flags);
1081 }
1082 #endif /* CONFIG_DEBUG_PAGEALLOC */