24475b73657a1fb3b01b8126a1f5971a3a585583
[linux-2.6-microblaze.git] / arch / sparc / mm / init_64.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  arch/sparc64/mm/init.c
4  *
5  *  Copyright (C) 1996-1999 David S. Miller (davem@caip.rutgers.edu)
6  *  Copyright (C) 1997-1999 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
7  */
8  
9 #include <linux/extable.h>
10 #include <linux/kernel.h>
11 #include <linux/sched.h>
12 #include <linux/string.h>
13 #include <linux/init.h>
14 #include <linux/memblock.h>
15 #include <linux/mm.h>
16 #include <linux/hugetlb.h>
17 #include <linux/initrd.h>
18 #include <linux/swap.h>
19 #include <linux/pagemap.h>
20 #include <linux/poison.h>
21 #include <linux/fs.h>
22 #include <linux/seq_file.h>
23 #include <linux/kprobes.h>
24 #include <linux/cache.h>
25 #include <linux/sort.h>
26 #include <linux/ioport.h>
27 #include <linux/percpu.h>
28 #include <linux/mmzone.h>
29 #include <linux/gfp.h>
30
31 #include <asm/head.h>
32 #include <asm/page.h>
33 #include <asm/pgalloc.h>
34 #include <asm/pgtable.h>
35 #include <asm/oplib.h>
36 #include <asm/iommu.h>
37 #include <asm/io.h>
38 #include <linux/uaccess.h>
39 #include <asm/mmu_context.h>
40 #include <asm/tlbflush.h>
41 #include <asm/dma.h>
42 #include <asm/starfire.h>
43 #include <asm/tlb.h>
44 #include <asm/spitfire.h>
45 #include <asm/sections.h>
46 #include <asm/tsb.h>
47 #include <asm/hypervisor.h>
48 #include <asm/prom.h>
49 #include <asm/mdesc.h>
50 #include <asm/cpudata.h>
51 #include <asm/setup.h>
52 #include <asm/irq.h>
53
54 #include "init_64.h"
55
56 unsigned long kern_linear_pte_xor[4] __read_mostly;
57 static unsigned long page_cache4v_flag;
58
59 /* A bitmap, two bits for every 256MB of physical memory.  These two
60  * bits determine what page size we use for kernel linear
61  * translations.  They form an index into kern_linear_pte_xor[].  The
62  * value in the indexed slot is XOR'd with the TLB miss virtual
63  * address to form the resulting TTE.  The mapping is:
64  *
65  *      0       ==>     4MB
66  *      1       ==>     256MB
67  *      2       ==>     2GB
68  *      3       ==>     16GB
69  *
70  * All sun4v chips support 256MB pages.  Only SPARC-T4 and later
71  * support 2GB pages, and hopefully future cpus will support the 16GB
72  * pages as well.  For slots 2 and 3, we encode a 256MB TTE xor there
73  * if these larger page sizes are not supported by the cpu.
74  *
75  * It would be nice to determine this from the machine description
76  * 'cpu' properties, but we need to have this table setup before the
77  * MDESC is initialized.
78  */
79
80 #ifndef CONFIG_DEBUG_PAGEALLOC
81 /* A special kernel TSB for 4MB, 256MB, 2GB and 16GB linear mappings.
82  * Space is allocated for this right after the trap table in
83  * arch/sparc64/kernel/head.S
84  */
85 extern struct tsb swapper_4m_tsb[KERNEL_TSB4M_NENTRIES];
86 #endif
87 extern struct tsb swapper_tsb[KERNEL_TSB_NENTRIES];
88
89 static unsigned long cpu_pgsz_mask;
90
91 #define MAX_BANKS       1024
92
93 static struct linux_prom64_registers pavail[MAX_BANKS];
94 static int pavail_ents;
95
96 u64 numa_latency[MAX_NUMNODES][MAX_NUMNODES];
97
98 static int cmp_p64(const void *a, const void *b)
99 {
100         const struct linux_prom64_registers *x = a, *y = b;
101
102         if (x->phys_addr > y->phys_addr)
103                 return 1;
104         if (x->phys_addr < y->phys_addr)
105                 return -1;
106         return 0;
107 }
108
109 static void __init read_obp_memory(const char *property,
110                                    struct linux_prom64_registers *regs,
111                                    int *num_ents)
112 {
113         phandle node = prom_finddevice("/memory");
114         int prop_size = prom_getproplen(node, property);
115         int ents, ret, i;
116
117         ents = prop_size / sizeof(struct linux_prom64_registers);
118         if (ents > MAX_BANKS) {
119                 prom_printf("The machine has more %s property entries than "
120                             "this kernel can support (%d).\n",
121                             property, MAX_BANKS);
122                 prom_halt();
123         }
124
125         ret = prom_getproperty(node, property, (char *) regs, prop_size);
126         if (ret == -1) {
127                 prom_printf("Couldn't get %s property from /memory.\n",
128                                 property);
129                 prom_halt();
130         }
131
132         /* Sanitize what we got from the firmware, by page aligning
133          * everything.
134          */
135         for (i = 0; i < ents; i++) {
136                 unsigned long base, size;
137
138                 base = regs[i].phys_addr;
139                 size = regs[i].reg_size;
140
141                 size &= PAGE_MASK;
142                 if (base & ~PAGE_MASK) {
143                         unsigned long new_base = PAGE_ALIGN(base);
144
145                         size -= new_base - base;
146                         if ((long) size < 0L)
147                                 size = 0UL;
148                         base = new_base;
149                 }
150                 if (size == 0UL) {
151                         /* If it is empty, simply get rid of it.
152                          * This simplifies the logic of the other
153                          * functions that process these arrays.
154                          */
155                         memmove(&regs[i], &regs[i + 1],
156                                 (ents - i - 1) * sizeof(regs[0]));
157                         i--;
158                         ents--;
159                         continue;
160                 }
161                 regs[i].phys_addr = base;
162                 regs[i].reg_size = size;
163         }
164
165         *num_ents = ents;
166
167         sort(regs, ents, sizeof(struct linux_prom64_registers),
168              cmp_p64, NULL);
169 }
170
171 /* Kernel physical address base and size in bytes.  */
172 unsigned long kern_base __read_mostly;
173 unsigned long kern_size __read_mostly;
174
175 /* Initial ramdisk setup */
176 extern unsigned long sparc_ramdisk_image64;
177 extern unsigned int sparc_ramdisk_image;
178 extern unsigned int sparc_ramdisk_size;
179
180 struct page *mem_map_zero __read_mostly;
181 EXPORT_SYMBOL(mem_map_zero);
182
183 unsigned int sparc64_highest_unlocked_tlb_ent __read_mostly;
184
185 unsigned long sparc64_kern_pri_context __read_mostly;
186 unsigned long sparc64_kern_pri_nuc_bits __read_mostly;
187 unsigned long sparc64_kern_sec_context __read_mostly;
188
189 int num_kernel_image_mappings;
190
191 #ifdef CONFIG_DEBUG_DCFLUSH
192 atomic_t dcpage_flushes = ATOMIC_INIT(0);
193 #ifdef CONFIG_SMP
194 atomic_t dcpage_flushes_xcall = ATOMIC_INIT(0);
195 #endif
196 #endif
197
198 inline void flush_dcache_page_impl(struct page *page)
199 {
200         BUG_ON(tlb_type == hypervisor);
201 #ifdef CONFIG_DEBUG_DCFLUSH
202         atomic_inc(&dcpage_flushes);
203 #endif
204
205 #ifdef DCACHE_ALIASING_POSSIBLE
206         __flush_dcache_page(page_address(page),
207                             ((tlb_type == spitfire) &&
208                              page_mapping_file(page) != NULL));
209 #else
210         if (page_mapping_file(page) != NULL &&
211             tlb_type == spitfire)
212                 __flush_icache_page(__pa(page_address(page)));
213 #endif
214 }
215
216 #define PG_dcache_dirty         PG_arch_1
217 #define PG_dcache_cpu_shift     32UL
218 #define PG_dcache_cpu_mask      \
219         ((1UL<<ilog2(roundup_pow_of_two(NR_CPUS)))-1UL)
220
221 #define dcache_dirty_cpu(page) \
222         (((page)->flags >> PG_dcache_cpu_shift) & PG_dcache_cpu_mask)
223
224 static inline void set_dcache_dirty(struct page *page, int this_cpu)
225 {
226         unsigned long mask = this_cpu;
227         unsigned long non_cpu_bits;
228
229         non_cpu_bits = ~(PG_dcache_cpu_mask << PG_dcache_cpu_shift);
230         mask = (mask << PG_dcache_cpu_shift) | (1UL << PG_dcache_dirty);
231
232         __asm__ __volatile__("1:\n\t"
233                              "ldx       [%2], %%g7\n\t"
234                              "and       %%g7, %1, %%g1\n\t"
235                              "or        %%g1, %0, %%g1\n\t"
236                              "casx      [%2], %%g7, %%g1\n\t"
237                              "cmp       %%g7, %%g1\n\t"
238                              "bne,pn    %%xcc, 1b\n\t"
239                              " nop"
240                              : /* no outputs */
241                              : "r" (mask), "r" (non_cpu_bits), "r" (&page->flags)
242                              : "g1", "g7");
243 }
244
245 static inline void clear_dcache_dirty_cpu(struct page *page, unsigned long cpu)
246 {
247         unsigned long mask = (1UL << PG_dcache_dirty);
248
249         __asm__ __volatile__("! test_and_clear_dcache_dirty\n"
250                              "1:\n\t"
251                              "ldx       [%2], %%g7\n\t"
252                              "srlx      %%g7, %4, %%g1\n\t"
253                              "and       %%g1, %3, %%g1\n\t"
254                              "cmp       %%g1, %0\n\t"
255                              "bne,pn    %%icc, 2f\n\t"
256                              " andn     %%g7, %1, %%g1\n\t"
257                              "casx      [%2], %%g7, %%g1\n\t"
258                              "cmp       %%g7, %%g1\n\t"
259                              "bne,pn    %%xcc, 1b\n\t"
260                              " nop\n"
261                              "2:"
262                              : /* no outputs */
263                              : "r" (cpu), "r" (mask), "r" (&page->flags),
264                                "i" (PG_dcache_cpu_mask),
265                                "i" (PG_dcache_cpu_shift)
266                              : "g1", "g7");
267 }
268
269 static inline void tsb_insert(struct tsb *ent, unsigned long tag, unsigned long pte)
270 {
271         unsigned long tsb_addr = (unsigned long) ent;
272
273         if (tlb_type == cheetah_plus || tlb_type == hypervisor)
274                 tsb_addr = __pa(tsb_addr);
275
276         __tsb_insert(tsb_addr, tag, pte);
277 }
278
279 unsigned long _PAGE_ALL_SZ_BITS __read_mostly;
280
281 static void flush_dcache(unsigned long pfn)
282 {
283         struct page *page;
284
285         page = pfn_to_page(pfn);
286         if (page) {
287                 unsigned long pg_flags;
288
289                 pg_flags = page->flags;
290                 if (pg_flags & (1UL << PG_dcache_dirty)) {
291                         int cpu = ((pg_flags >> PG_dcache_cpu_shift) &
292                                    PG_dcache_cpu_mask);
293                         int this_cpu = get_cpu();
294
295                         /* This is just to optimize away some function calls
296                          * in the SMP case.
297                          */
298                         if (cpu == this_cpu)
299                                 flush_dcache_page_impl(page);
300                         else
301                                 smp_flush_dcache_page_impl(page, cpu);
302
303                         clear_dcache_dirty_cpu(page, cpu);
304
305                         put_cpu();
306                 }
307         }
308 }
309
310 /* mm->context.lock must be held */
311 static void __update_mmu_tsb_insert(struct mm_struct *mm, unsigned long tsb_index,
312                                     unsigned long tsb_hash_shift, unsigned long address,
313                                     unsigned long tte)
314 {
315         struct tsb *tsb = mm->context.tsb_block[tsb_index].tsb;
316         unsigned long tag;
317
318         if (unlikely(!tsb))
319                 return;
320
321         tsb += ((address >> tsb_hash_shift) &
322                 (mm->context.tsb_block[tsb_index].tsb_nentries - 1UL));
323         tag = (address >> 22UL);
324         tsb_insert(tsb, tag, tte);
325 }
326
327 #ifdef CONFIG_HUGETLB_PAGE
328 static void __init add_huge_page_size(unsigned long size)
329 {
330         unsigned int order;
331
332         if (size_to_hstate(size))
333                 return;
334
335         order = ilog2(size) - PAGE_SHIFT;
336         hugetlb_add_hstate(order);
337 }
338
339 static int __init hugetlbpage_init(void)
340 {
341         add_huge_page_size(1UL << HPAGE_64K_SHIFT);
342         add_huge_page_size(1UL << HPAGE_SHIFT);
343         add_huge_page_size(1UL << HPAGE_256MB_SHIFT);
344         add_huge_page_size(1UL << HPAGE_2GB_SHIFT);
345
346         return 0;
347 }
348
349 arch_initcall(hugetlbpage_init);
350
351 static void __init pud_huge_patch(void)
352 {
353         struct pud_huge_patch_entry *p;
354         unsigned long addr;
355
356         p = &__pud_huge_patch;
357         addr = p->addr;
358         *(unsigned int *)addr = p->insn;
359
360         __asm__ __volatile__("flush %0" : : "r" (addr));
361 }
362
363 bool __init arch_hugetlb_valid_size(unsigned long size)
364 {
365         unsigned int hugepage_shift = ilog2(size);
366         unsigned short hv_pgsz_idx;
367         unsigned int hv_pgsz_mask;
368
369         switch (hugepage_shift) {
370         case HPAGE_16GB_SHIFT:
371                 hv_pgsz_mask = HV_PGSZ_MASK_16GB;
372                 hv_pgsz_idx = HV_PGSZ_IDX_16GB;
373                 pud_huge_patch();
374                 break;
375         case HPAGE_2GB_SHIFT:
376                 hv_pgsz_mask = HV_PGSZ_MASK_2GB;
377                 hv_pgsz_idx = HV_PGSZ_IDX_2GB;
378                 break;
379         case HPAGE_256MB_SHIFT:
380                 hv_pgsz_mask = HV_PGSZ_MASK_256MB;
381                 hv_pgsz_idx = HV_PGSZ_IDX_256MB;
382                 break;
383         case HPAGE_SHIFT:
384                 hv_pgsz_mask = HV_PGSZ_MASK_4MB;
385                 hv_pgsz_idx = HV_PGSZ_IDX_4MB;
386                 break;
387         case HPAGE_64K_SHIFT:
388                 hv_pgsz_mask = HV_PGSZ_MASK_64K;
389                 hv_pgsz_idx = HV_PGSZ_IDX_64K;
390                 break;
391         default:
392                 hv_pgsz_mask = 0;
393         }
394
395         if ((hv_pgsz_mask & cpu_pgsz_mask) == 0U)
396                 return false;
397
398         return true;
399 }
400
401 static int __init setup_hugepagesz(char *string)
402 {
403         unsigned long long hugepage_size;
404         int rc = 0;
405
406         hugepage_size = memparse(string, &string);
407
408         if (!arch_hugetlb_valid_size((unsigned long)hugepage_size)) {
409                 hugetlb_bad_size();
410                 pr_err("hugepagesz=%llu not supported by MMU.\n",
411                         hugepage_size);
412                 goto out;
413         }
414
415         add_huge_page_size(hugepage_size);
416         rc = 1;
417
418 out:
419         return rc;
420 }
421 __setup("hugepagesz=", setup_hugepagesz);
422 #endif  /* CONFIG_HUGETLB_PAGE */
423
424 void update_mmu_cache(struct vm_area_struct *vma, unsigned long address, pte_t *ptep)
425 {
426         struct mm_struct *mm;
427         unsigned long flags;
428         bool is_huge_tsb;
429         pte_t pte = *ptep;
430
431         if (tlb_type != hypervisor) {
432                 unsigned long pfn = pte_pfn(pte);
433
434                 if (pfn_valid(pfn))
435                         flush_dcache(pfn);
436         }
437
438         mm = vma->vm_mm;
439
440         /* Don't insert a non-valid PTE into the TSB, we'll deadlock.  */
441         if (!pte_accessible(mm, pte))
442                 return;
443
444         spin_lock_irqsave(&mm->context.lock, flags);
445
446         is_huge_tsb = false;
447 #if defined(CONFIG_HUGETLB_PAGE) || defined(CONFIG_TRANSPARENT_HUGEPAGE)
448         if (mm->context.hugetlb_pte_count || mm->context.thp_pte_count) {
449                 unsigned long hugepage_size = PAGE_SIZE;
450
451                 if (is_vm_hugetlb_page(vma))
452                         hugepage_size = huge_page_size(hstate_vma(vma));
453
454                 if (hugepage_size >= PUD_SIZE) {
455                         unsigned long mask = 0x1ffc00000UL;
456
457                         /* Transfer bits [32:22] from address to resolve
458                          * at 4M granularity.
459                          */
460                         pte_val(pte) &= ~mask;
461                         pte_val(pte) |= (address & mask);
462                 } else if (hugepage_size >= PMD_SIZE) {
463                         /* We are fabricating 8MB pages using 4MB
464                          * real hw pages.
465                          */
466                         pte_val(pte) |= (address & (1UL << REAL_HPAGE_SHIFT));
467                 }
468
469                 if (hugepage_size >= PMD_SIZE) {
470                         __update_mmu_tsb_insert(mm, MM_TSB_HUGE,
471                                 REAL_HPAGE_SHIFT, address, pte_val(pte));
472                         is_huge_tsb = true;
473                 }
474         }
475 #endif
476         if (!is_huge_tsb)
477                 __update_mmu_tsb_insert(mm, MM_TSB_BASE, PAGE_SHIFT,
478                                         address, pte_val(pte));
479
480         spin_unlock_irqrestore(&mm->context.lock, flags);
481 }
482
483 void flush_dcache_page(struct page *page)
484 {
485         struct address_space *mapping;
486         int this_cpu;
487
488         if (tlb_type == hypervisor)
489                 return;
490
491         /* Do not bother with the expensive D-cache flush if it
492          * is merely the zero page.  The 'bigcore' testcase in GDB
493          * causes this case to run millions of times.
494          */
495         if (page == ZERO_PAGE(0))
496                 return;
497
498         this_cpu = get_cpu();
499
500         mapping = page_mapping_file(page);
501         if (mapping && !mapping_mapped(mapping)) {
502                 int dirty = test_bit(PG_dcache_dirty, &page->flags);
503                 if (dirty) {
504                         int dirty_cpu = dcache_dirty_cpu(page);
505
506                         if (dirty_cpu == this_cpu)
507                                 goto out;
508                         smp_flush_dcache_page_impl(page, dirty_cpu);
509                 }
510                 set_dcache_dirty(page, this_cpu);
511         } else {
512                 /* We could delay the flush for the !page_mapping
513                  * case too.  But that case is for exec env/arg
514                  * pages and those are %99 certainly going to get
515                  * faulted into the tlb (and thus flushed) anyways.
516                  */
517                 flush_dcache_page_impl(page);
518         }
519
520 out:
521         put_cpu();
522 }
523 EXPORT_SYMBOL(flush_dcache_page);
524
525 void __kprobes flush_icache_range(unsigned long start, unsigned long end)
526 {
527         /* Cheetah and Hypervisor platform cpus have coherent I-cache. */
528         if (tlb_type == spitfire) {
529                 unsigned long kaddr;
530
531                 /* This code only runs on Spitfire cpus so this is
532                  * why we can assume _PAGE_PADDR_4U.
533                  */
534                 for (kaddr = start; kaddr < end; kaddr += PAGE_SIZE) {
535                         unsigned long paddr, mask = _PAGE_PADDR_4U;
536
537                         if (kaddr >= PAGE_OFFSET)
538                                 paddr = kaddr & mask;
539                         else {
540                                 pgd_t *pgdp = pgd_offset_k(kaddr);
541                                 p4d_t *p4dp = p4d_offset(pgdp, kaddr);
542                                 pud_t *pudp = pud_offset(p4dp, kaddr);
543                                 pmd_t *pmdp = pmd_offset(pudp, kaddr);
544                                 pte_t *ptep = pte_offset_kernel(pmdp, kaddr);
545
546                                 paddr = pte_val(*ptep) & mask;
547                         }
548                         __flush_icache_page(paddr);
549                 }
550         }
551 }
552 EXPORT_SYMBOL(flush_icache_range);
553
554 void mmu_info(struct seq_file *m)
555 {
556         static const char *pgsz_strings[] = {
557                 "8K", "64K", "512K", "4MB", "32MB",
558                 "256MB", "2GB", "16GB",
559         };
560         int i, printed;
561
562         if (tlb_type == cheetah)
563                 seq_printf(m, "MMU Type\t: Cheetah\n");
564         else if (tlb_type == cheetah_plus)
565                 seq_printf(m, "MMU Type\t: Cheetah+\n");
566         else if (tlb_type == spitfire)
567                 seq_printf(m, "MMU Type\t: Spitfire\n");
568         else if (tlb_type == hypervisor)
569                 seq_printf(m, "MMU Type\t: Hypervisor (sun4v)\n");
570         else
571                 seq_printf(m, "MMU Type\t: ???\n");
572
573         seq_printf(m, "MMU PGSZs\t: ");
574         printed = 0;
575         for (i = 0; i < ARRAY_SIZE(pgsz_strings); i++) {
576                 if (cpu_pgsz_mask & (1UL << i)) {
577                         seq_printf(m, "%s%s",
578                                    printed ? "," : "", pgsz_strings[i]);
579                         printed++;
580                 }
581         }
582         seq_putc(m, '\n');
583
584 #ifdef CONFIG_DEBUG_DCFLUSH
585         seq_printf(m, "DCPageFlushes\t: %d\n",
586                    atomic_read(&dcpage_flushes));
587 #ifdef CONFIG_SMP
588         seq_printf(m, "DCPageFlushesXC\t: %d\n",
589                    atomic_read(&dcpage_flushes_xcall));
590 #endif /* CONFIG_SMP */
591 #endif /* CONFIG_DEBUG_DCFLUSH */
592 }
593
594 struct linux_prom_translation prom_trans[512] __read_mostly;
595 unsigned int prom_trans_ents __read_mostly;
596
597 unsigned long kern_locked_tte_data;
598
599 /* The obp translations are saved based on 8k pagesize, since obp can
600  * use a mixture of pagesizes. Misses to the LOW_OBP_ADDRESS ->
601  * HI_OBP_ADDRESS range are handled in ktlb.S.
602  */
603 static inline int in_obp_range(unsigned long vaddr)
604 {
605         return (vaddr >= LOW_OBP_ADDRESS &&
606                 vaddr < HI_OBP_ADDRESS);
607 }
608
609 static int cmp_ptrans(const void *a, const void *b)
610 {
611         const struct linux_prom_translation *x = a, *y = b;
612
613         if (x->virt > y->virt)
614                 return 1;
615         if (x->virt < y->virt)
616                 return -1;
617         return 0;
618 }
619
620 /* Read OBP translations property into 'prom_trans[]'.  */
621 static void __init read_obp_translations(void)
622 {
623         int n, node, ents, first, last, i;
624
625         node = prom_finddevice("/virtual-memory");
626         n = prom_getproplen(node, "translations");
627         if (unlikely(n == 0 || n == -1)) {
628                 prom_printf("prom_mappings: Couldn't get size.\n");
629                 prom_halt();
630         }
631         if (unlikely(n > sizeof(prom_trans))) {
632                 prom_printf("prom_mappings: Size %d is too big.\n", n);
633                 prom_halt();
634         }
635
636         if ((n = prom_getproperty(node, "translations",
637                                   (char *)&prom_trans[0],
638                                   sizeof(prom_trans))) == -1) {
639                 prom_printf("prom_mappings: Couldn't get property.\n");
640                 prom_halt();
641         }
642
643         n = n / sizeof(struct linux_prom_translation);
644
645         ents = n;
646
647         sort(prom_trans, ents, sizeof(struct linux_prom_translation),
648              cmp_ptrans, NULL);
649
650         /* Now kick out all the non-OBP entries.  */
651         for (i = 0; i < ents; i++) {
652                 if (in_obp_range(prom_trans[i].virt))
653                         break;
654         }
655         first = i;
656         for (; i < ents; i++) {
657                 if (!in_obp_range(prom_trans[i].virt))
658                         break;
659         }
660         last = i;
661
662         for (i = 0; i < (last - first); i++) {
663                 struct linux_prom_translation *src = &prom_trans[i + first];
664                 struct linux_prom_translation *dest = &prom_trans[i];
665
666                 *dest = *src;
667         }
668         for (; i < ents; i++) {
669                 struct linux_prom_translation *dest = &prom_trans[i];
670                 dest->virt = dest->size = dest->data = 0x0UL;
671         }
672
673         prom_trans_ents = last - first;
674
675         if (tlb_type == spitfire) {
676                 /* Clear diag TTE bits. */
677                 for (i = 0; i < prom_trans_ents; i++)
678                         prom_trans[i].data &= ~0x0003fe0000000000UL;
679         }
680
681         /* Force execute bit on.  */
682         for (i = 0; i < prom_trans_ents; i++)
683                 prom_trans[i].data |= (tlb_type == hypervisor ?
684                                        _PAGE_EXEC_4V : _PAGE_EXEC_4U);
685 }
686
687 static void __init hypervisor_tlb_lock(unsigned long vaddr,
688                                        unsigned long pte,
689                                        unsigned long mmu)
690 {
691         unsigned long ret = sun4v_mmu_map_perm_addr(vaddr, 0, pte, mmu);
692
693         if (ret != 0) {
694                 prom_printf("hypervisor_tlb_lock[%lx:%x:%lx:%lx]: "
695                             "errors with %lx\n", vaddr, 0, pte, mmu, ret);
696                 prom_halt();
697         }
698 }
699
700 static unsigned long kern_large_tte(unsigned long paddr);
701
702 static void __init remap_kernel(void)
703 {
704         unsigned long phys_page, tte_vaddr, tte_data;
705         int i, tlb_ent = sparc64_highest_locked_tlbent();
706
707         tte_vaddr = (unsigned long) KERNBASE;
708         phys_page = (prom_boot_mapping_phys_low >> ILOG2_4MB) << ILOG2_4MB;
709         tte_data = kern_large_tte(phys_page);
710
711         kern_locked_tte_data = tte_data;
712
713         /* Now lock us into the TLBs via Hypervisor or OBP. */
714         if (tlb_type == hypervisor) {
715                 for (i = 0; i < num_kernel_image_mappings; i++) {
716                         hypervisor_tlb_lock(tte_vaddr, tte_data, HV_MMU_DMMU);
717                         hypervisor_tlb_lock(tte_vaddr, tte_data, HV_MMU_IMMU);
718                         tte_vaddr += 0x400000;
719                         tte_data += 0x400000;
720                 }
721         } else {
722                 for (i = 0; i < num_kernel_image_mappings; i++) {
723                         prom_dtlb_load(tlb_ent - i, tte_data, tte_vaddr);
724                         prom_itlb_load(tlb_ent - i, tte_data, tte_vaddr);
725                         tte_vaddr += 0x400000;
726                         tte_data += 0x400000;
727                 }
728                 sparc64_highest_unlocked_tlb_ent = tlb_ent - i;
729         }
730         if (tlb_type == cheetah_plus) {
731                 sparc64_kern_pri_context = (CTX_CHEETAH_PLUS_CTX0 |
732                                             CTX_CHEETAH_PLUS_NUC);
733                 sparc64_kern_pri_nuc_bits = CTX_CHEETAH_PLUS_NUC;
734                 sparc64_kern_sec_context = CTX_CHEETAH_PLUS_CTX0;
735         }
736 }
737
738
739 static void __init inherit_prom_mappings(void)
740 {
741         /* Now fixup OBP's idea about where we really are mapped. */
742         printk("Remapping the kernel... ");
743         remap_kernel();
744         printk("done.\n");
745 }
746
747 void prom_world(int enter)
748 {
749         if (!enter)
750                 set_fs(get_fs());
751
752         __asm__ __volatile__("flushw");
753 }
754
755 void __flush_dcache_range(unsigned long start, unsigned long end)
756 {
757         unsigned long va;
758
759         if (tlb_type == spitfire) {
760                 int n = 0;
761
762                 for (va = start; va < end; va += 32) {
763                         spitfire_put_dcache_tag(va & 0x3fe0, 0x0);
764                         if (++n >= 512)
765                                 break;
766                 }
767         } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
768                 start = __pa(start);
769                 end = __pa(end);
770                 for (va = start; va < end; va += 32)
771                         __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
772                                              "membar #Sync"
773                                              : /* no outputs */
774                                              : "r" (va),
775                                                "i" (ASI_DCACHE_INVALIDATE));
776         }
777 }
778 EXPORT_SYMBOL(__flush_dcache_range);
779
780 /* get_new_mmu_context() uses "cache + 1".  */
781 DEFINE_SPINLOCK(ctx_alloc_lock);
782 unsigned long tlb_context_cache = CTX_FIRST_VERSION;
783 #define MAX_CTX_NR      (1UL << CTX_NR_BITS)
784 #define CTX_BMAP_SLOTS  BITS_TO_LONGS(MAX_CTX_NR)
785 DECLARE_BITMAP(mmu_context_bmap, MAX_CTX_NR);
786 DEFINE_PER_CPU(struct mm_struct *, per_cpu_secondary_mm) = {0};
787
788 static void mmu_context_wrap(void)
789 {
790         unsigned long old_ver = tlb_context_cache & CTX_VERSION_MASK;
791         unsigned long new_ver, new_ctx, old_ctx;
792         struct mm_struct *mm;
793         int cpu;
794
795         bitmap_zero(mmu_context_bmap, 1 << CTX_NR_BITS);
796
797         /* Reserve kernel context */
798         set_bit(0, mmu_context_bmap);
799
800         new_ver = (tlb_context_cache & CTX_VERSION_MASK) + CTX_FIRST_VERSION;
801         if (unlikely(new_ver == 0))
802                 new_ver = CTX_FIRST_VERSION;
803         tlb_context_cache = new_ver;
804
805         /*
806          * Make sure that any new mm that are added into per_cpu_secondary_mm,
807          * are going to go through get_new_mmu_context() path.
808          */
809         mb();
810
811         /*
812          * Updated versions to current on those CPUs that had valid secondary
813          * contexts
814          */
815         for_each_online_cpu(cpu) {
816                 /*
817                  * If a new mm is stored after we took this mm from the array,
818                  * it will go into get_new_mmu_context() path, because we
819                  * already bumped the version in tlb_context_cache.
820                  */
821                 mm = per_cpu(per_cpu_secondary_mm, cpu);
822
823                 if (unlikely(!mm || mm == &init_mm))
824                         continue;
825
826                 old_ctx = mm->context.sparc64_ctx_val;
827                 if (likely((old_ctx & CTX_VERSION_MASK) == old_ver)) {
828                         new_ctx = (old_ctx & ~CTX_VERSION_MASK) | new_ver;
829                         set_bit(new_ctx & CTX_NR_MASK, mmu_context_bmap);
830                         mm->context.sparc64_ctx_val = new_ctx;
831                 }
832         }
833 }
834
835 /* Caller does TLB context flushing on local CPU if necessary.
836  * The caller also ensures that CTX_VALID(mm->context) is false.
837  *
838  * We must be careful about boundary cases so that we never
839  * let the user have CTX 0 (nucleus) or we ever use a CTX
840  * version of zero (and thus NO_CONTEXT would not be caught
841  * by version mis-match tests in mmu_context.h).
842  *
843  * Always invoked with interrupts disabled.
844  */
845 void get_new_mmu_context(struct mm_struct *mm)
846 {
847         unsigned long ctx, new_ctx;
848         unsigned long orig_pgsz_bits;
849
850         spin_lock(&ctx_alloc_lock);
851 retry:
852         /* wrap might have happened, test again if our context became valid */
853         if (unlikely(CTX_VALID(mm->context)))
854                 goto out;
855         orig_pgsz_bits = (mm->context.sparc64_ctx_val & CTX_PGSZ_MASK);
856         ctx = (tlb_context_cache + 1) & CTX_NR_MASK;
857         new_ctx = find_next_zero_bit(mmu_context_bmap, 1 << CTX_NR_BITS, ctx);
858         if (new_ctx >= (1 << CTX_NR_BITS)) {
859                 new_ctx = find_next_zero_bit(mmu_context_bmap, ctx, 1);
860                 if (new_ctx >= ctx) {
861                         mmu_context_wrap();
862                         goto retry;
863                 }
864         }
865         if (mm->context.sparc64_ctx_val)
866                 cpumask_clear(mm_cpumask(mm));
867         mmu_context_bmap[new_ctx>>6] |= (1UL << (new_ctx & 63));
868         new_ctx |= (tlb_context_cache & CTX_VERSION_MASK);
869         tlb_context_cache = new_ctx;
870         mm->context.sparc64_ctx_val = new_ctx | orig_pgsz_bits;
871 out:
872         spin_unlock(&ctx_alloc_lock);
873 }
874
875 static int numa_enabled = 1;
876 static int numa_debug;
877
878 static int __init early_numa(char *p)
879 {
880         if (!p)
881                 return 0;
882
883         if (strstr(p, "off"))
884                 numa_enabled = 0;
885
886         if (strstr(p, "debug"))
887                 numa_debug = 1;
888
889         return 0;
890 }
891 early_param("numa", early_numa);
892
893 #define numadbg(f, a...) \
894 do {    if (numa_debug) \
895                 printk(KERN_INFO f, ## a); \
896 } while (0)
897
898 static void __init find_ramdisk(unsigned long phys_base)
899 {
900 #ifdef CONFIG_BLK_DEV_INITRD
901         if (sparc_ramdisk_image || sparc_ramdisk_image64) {
902                 unsigned long ramdisk_image;
903
904                 /* Older versions of the bootloader only supported a
905                  * 32-bit physical address for the ramdisk image
906                  * location, stored at sparc_ramdisk_image.  Newer
907                  * SILO versions set sparc_ramdisk_image to zero and
908                  * provide a full 64-bit physical address at
909                  * sparc_ramdisk_image64.
910                  */
911                 ramdisk_image = sparc_ramdisk_image;
912                 if (!ramdisk_image)
913                         ramdisk_image = sparc_ramdisk_image64;
914
915                 /* Another bootloader quirk.  The bootloader normalizes
916                  * the physical address to KERNBASE, so we have to
917                  * factor that back out and add in the lowest valid
918                  * physical page address to get the true physical address.
919                  */
920                 ramdisk_image -= KERNBASE;
921                 ramdisk_image += phys_base;
922
923                 numadbg("Found ramdisk at physical address 0x%lx, size %u\n",
924                         ramdisk_image, sparc_ramdisk_size);
925
926                 initrd_start = ramdisk_image;
927                 initrd_end = ramdisk_image + sparc_ramdisk_size;
928
929                 memblock_reserve(initrd_start, sparc_ramdisk_size);
930
931                 initrd_start += PAGE_OFFSET;
932                 initrd_end += PAGE_OFFSET;
933         }
934 #endif
935 }
936
937 struct node_mem_mask {
938         unsigned long mask;
939         unsigned long match;
940 };
941 static struct node_mem_mask node_masks[MAX_NUMNODES];
942 static int num_node_masks;
943
944 #ifdef CONFIG_NEED_MULTIPLE_NODES
945
946 struct mdesc_mlgroup {
947         u64     node;
948         u64     latency;
949         u64     match;
950         u64     mask;
951 };
952
953 static struct mdesc_mlgroup *mlgroups;
954 static int num_mlgroups;
955
956 int numa_cpu_lookup_table[NR_CPUS];
957 cpumask_t numa_cpumask_lookup_table[MAX_NUMNODES];
958
959 struct mdesc_mblock {
960         u64     base;
961         u64     size;
962         u64     offset; /* RA-to-PA */
963 };
964 static struct mdesc_mblock *mblocks;
965 static int num_mblocks;
966
967 static struct mdesc_mblock * __init addr_to_mblock(unsigned long addr)
968 {
969         struct mdesc_mblock *m = NULL;
970         int i;
971
972         for (i = 0; i < num_mblocks; i++) {
973                 m = &mblocks[i];
974
975                 if (addr >= m->base &&
976                     addr < (m->base + m->size)) {
977                         break;
978                 }
979         }
980
981         return m;
982 }
983
984 static u64 __init memblock_nid_range_sun4u(u64 start, u64 end, int *nid)
985 {
986         int prev_nid, new_nid;
987
988         prev_nid = NUMA_NO_NODE;
989         for ( ; start < end; start += PAGE_SIZE) {
990                 for (new_nid = 0; new_nid < num_node_masks; new_nid++) {
991                         struct node_mem_mask *p = &node_masks[new_nid];
992
993                         if ((start & p->mask) == p->match) {
994                                 if (prev_nid == NUMA_NO_NODE)
995                                         prev_nid = new_nid;
996                                 break;
997                         }
998                 }
999
1000                 if (new_nid == num_node_masks) {
1001                         prev_nid = 0;
1002                         WARN_ONCE(1, "addr[%Lx] doesn't match a NUMA node rule. Some memory will be owned by node 0.",
1003                                   start);
1004                         break;
1005                 }
1006
1007                 if (prev_nid != new_nid)
1008                         break;
1009         }
1010         *nid = prev_nid;
1011
1012         return start > end ? end : start;
1013 }
1014
1015 static u64 __init memblock_nid_range(u64 start, u64 end, int *nid)
1016 {
1017         u64 ret_end, pa_start, m_mask, m_match, m_end;
1018         struct mdesc_mblock *mblock;
1019         int _nid, i;
1020
1021         if (tlb_type != hypervisor)
1022                 return memblock_nid_range_sun4u(start, end, nid);
1023
1024         mblock = addr_to_mblock(start);
1025         if (!mblock) {
1026                 WARN_ONCE(1, "memblock_nid_range: Can't find mblock addr[%Lx]",
1027                           start);
1028
1029                 _nid = 0;
1030                 ret_end = end;
1031                 goto done;
1032         }
1033
1034         pa_start = start + mblock->offset;
1035         m_match = 0;
1036         m_mask = 0;
1037
1038         for (_nid = 0; _nid < num_node_masks; _nid++) {
1039                 struct node_mem_mask *const m = &node_masks[_nid];
1040
1041                 if ((pa_start & m->mask) == m->match) {
1042                         m_match = m->match;
1043                         m_mask = m->mask;
1044                         break;
1045                 }
1046         }
1047
1048         if (num_node_masks == _nid) {
1049                 /* We could not find NUMA group, so default to 0, but lets
1050                  * search for latency group, so we could calculate the correct
1051                  * end address that we return
1052                  */
1053                 _nid = 0;
1054
1055                 for (i = 0; i < num_mlgroups; i++) {
1056                         struct mdesc_mlgroup *const m = &mlgroups[i];
1057
1058                         if ((pa_start & m->mask) == m->match) {
1059                                 m_match = m->match;
1060                                 m_mask = m->mask;
1061                                 break;
1062                         }
1063                 }
1064
1065                 if (i == num_mlgroups) {
1066                         WARN_ONCE(1, "memblock_nid_range: Can't find latency group addr[%Lx]",
1067                                   start);
1068
1069                         ret_end = end;
1070                         goto done;
1071                 }
1072         }
1073
1074         /*
1075          * Each latency group has match and mask, and each memory block has an
1076          * offset.  An address belongs to a latency group if its address matches
1077          * the following formula: ((addr + offset) & mask) == match
1078          * It is, however, slow to check every single page if it matches a
1079          * particular latency group. As optimization we calculate end value by
1080          * using bit arithmetics.
1081          */
1082         m_end = m_match + (1ul << __ffs(m_mask)) - mblock->offset;
1083         m_end += pa_start & ~((1ul << fls64(m_mask)) - 1);
1084         ret_end = m_end > end ? end : m_end;
1085
1086 done:
1087         *nid = _nid;
1088         return ret_end;
1089 }
1090 #endif
1091
1092 /* This must be invoked after performing all of the necessary
1093  * memblock_set_node() calls for 'nid'.  We need to be able to get
1094  * correct data from get_pfn_range_for_nid().
1095  */
1096 static void __init allocate_node_data(int nid)
1097 {
1098         struct pglist_data *p;
1099         unsigned long start_pfn, end_pfn;
1100 #ifdef CONFIG_NEED_MULTIPLE_NODES
1101
1102         NODE_DATA(nid) = memblock_alloc_node(sizeof(struct pglist_data),
1103                                              SMP_CACHE_BYTES, nid);
1104         if (!NODE_DATA(nid)) {
1105                 prom_printf("Cannot allocate pglist_data for nid[%d]\n", nid);
1106                 prom_halt();
1107         }
1108
1109         NODE_DATA(nid)->node_id = nid;
1110 #endif
1111
1112         p = NODE_DATA(nid);
1113
1114         get_pfn_range_for_nid(nid, &start_pfn, &end_pfn);
1115         p->node_start_pfn = start_pfn;
1116         p->node_spanned_pages = end_pfn - start_pfn;
1117 }
1118
1119 static void init_node_masks_nonnuma(void)
1120 {
1121 #ifdef CONFIG_NEED_MULTIPLE_NODES
1122         int i;
1123 #endif
1124
1125         numadbg("Initializing tables for non-numa.\n");
1126
1127         node_masks[0].mask = 0;
1128         node_masks[0].match = 0;
1129         num_node_masks = 1;
1130
1131 #ifdef CONFIG_NEED_MULTIPLE_NODES
1132         for (i = 0; i < NR_CPUS; i++)
1133                 numa_cpu_lookup_table[i] = 0;
1134
1135         cpumask_setall(&numa_cpumask_lookup_table[0]);
1136 #endif
1137 }
1138
1139 #ifdef CONFIG_NEED_MULTIPLE_NODES
1140 struct pglist_data *node_data[MAX_NUMNODES];
1141
1142 EXPORT_SYMBOL(numa_cpu_lookup_table);
1143 EXPORT_SYMBOL(numa_cpumask_lookup_table);
1144 EXPORT_SYMBOL(node_data);
1145
1146 static int scan_pio_for_cfg_handle(struct mdesc_handle *md, u64 pio,
1147                                    u32 cfg_handle)
1148 {
1149         u64 arc;
1150
1151         mdesc_for_each_arc(arc, md, pio, MDESC_ARC_TYPE_FWD) {
1152                 u64 target = mdesc_arc_target(md, arc);
1153                 const u64 *val;
1154
1155                 val = mdesc_get_property(md, target,
1156                                          "cfg-handle", NULL);
1157                 if (val && *val == cfg_handle)
1158                         return 0;
1159         }
1160         return -ENODEV;
1161 }
1162
1163 static int scan_arcs_for_cfg_handle(struct mdesc_handle *md, u64 grp,
1164                                     u32 cfg_handle)
1165 {
1166         u64 arc, candidate, best_latency = ~(u64)0;
1167
1168         candidate = MDESC_NODE_NULL;
1169         mdesc_for_each_arc(arc, md, grp, MDESC_ARC_TYPE_FWD) {
1170                 u64 target = mdesc_arc_target(md, arc);
1171                 const char *name = mdesc_node_name(md, target);
1172                 const u64 *val;
1173
1174                 if (strcmp(name, "pio-latency-group"))
1175                         continue;
1176
1177                 val = mdesc_get_property(md, target, "latency", NULL);
1178                 if (!val)
1179                         continue;
1180
1181                 if (*val < best_latency) {
1182                         candidate = target;
1183                         best_latency = *val;
1184                 }
1185         }
1186
1187         if (candidate == MDESC_NODE_NULL)
1188                 return -ENODEV;
1189
1190         return scan_pio_for_cfg_handle(md, candidate, cfg_handle);
1191 }
1192
1193 int of_node_to_nid(struct device_node *dp)
1194 {
1195         const struct linux_prom64_registers *regs;
1196         struct mdesc_handle *md;
1197         u32 cfg_handle;
1198         int count, nid;
1199         u64 grp;
1200
1201         /* This is the right thing to do on currently supported
1202          * SUN4U NUMA platforms as well, as the PCI controller does
1203          * not sit behind any particular memory controller.
1204          */
1205         if (!mlgroups)
1206                 return -1;
1207
1208         regs = of_get_property(dp, "reg", NULL);
1209         if (!regs)
1210                 return -1;
1211
1212         cfg_handle = (regs->phys_addr >> 32UL) & 0x0fffffff;
1213
1214         md = mdesc_grab();
1215
1216         count = 0;
1217         nid = NUMA_NO_NODE;
1218         mdesc_for_each_node_by_name(md, grp, "group") {
1219                 if (!scan_arcs_for_cfg_handle(md, grp, cfg_handle)) {
1220                         nid = count;
1221                         break;
1222                 }
1223                 count++;
1224         }
1225
1226         mdesc_release(md);
1227
1228         return nid;
1229 }
1230
1231 static void __init add_node_ranges(void)
1232 {
1233         struct memblock_region *reg;
1234         unsigned long prev_max;
1235
1236 memblock_resized:
1237         prev_max = memblock.memory.max;
1238
1239         for_each_memblock(memory, reg) {
1240                 unsigned long size = reg->size;
1241                 unsigned long start, end;
1242
1243                 start = reg->base;
1244                 end = start + size;
1245                 while (start < end) {
1246                         unsigned long this_end;
1247                         int nid;
1248
1249                         this_end = memblock_nid_range(start, end, &nid);
1250
1251                         numadbg("Setting memblock NUMA node nid[%d] "
1252                                 "start[%lx] end[%lx]\n",
1253                                 nid, start, this_end);
1254
1255                         memblock_set_node(start, this_end - start,
1256                                           &memblock.memory, nid);
1257                         if (memblock.memory.max != prev_max)
1258                                 goto memblock_resized;
1259                         start = this_end;
1260                 }
1261         }
1262 }
1263
1264 static int __init grab_mlgroups(struct mdesc_handle *md)
1265 {
1266         unsigned long paddr;
1267         int count = 0;
1268         u64 node;
1269
1270         mdesc_for_each_node_by_name(md, node, "memory-latency-group")
1271                 count++;
1272         if (!count)
1273                 return -ENOENT;
1274
1275         paddr = memblock_phys_alloc(count * sizeof(struct mdesc_mlgroup),
1276                                     SMP_CACHE_BYTES);
1277         if (!paddr)
1278                 return -ENOMEM;
1279
1280         mlgroups = __va(paddr);
1281         num_mlgroups = count;
1282
1283         count = 0;
1284         mdesc_for_each_node_by_name(md, node, "memory-latency-group") {
1285                 struct mdesc_mlgroup *m = &mlgroups[count++];
1286                 const u64 *val;
1287
1288                 m->node = node;
1289
1290                 val = mdesc_get_property(md, node, "latency", NULL);
1291                 m->latency = *val;
1292                 val = mdesc_get_property(md, node, "address-match", NULL);
1293                 m->match = *val;
1294                 val = mdesc_get_property(md, node, "address-mask", NULL);
1295                 m->mask = *val;
1296
1297                 numadbg("MLGROUP[%d]: node[%llx] latency[%llx] "
1298                         "match[%llx] mask[%llx]\n",
1299                         count - 1, m->node, m->latency, m->match, m->mask);
1300         }
1301
1302         return 0;
1303 }
1304
1305 static int __init grab_mblocks(struct mdesc_handle *md)
1306 {
1307         unsigned long paddr;
1308         int count = 0;
1309         u64 node;
1310
1311         mdesc_for_each_node_by_name(md, node, "mblock")
1312                 count++;
1313         if (!count)
1314                 return -ENOENT;
1315
1316         paddr = memblock_phys_alloc(count * sizeof(struct mdesc_mblock),
1317                                     SMP_CACHE_BYTES);
1318         if (!paddr)
1319                 return -ENOMEM;
1320
1321         mblocks = __va(paddr);
1322         num_mblocks = count;
1323
1324         count = 0;
1325         mdesc_for_each_node_by_name(md, node, "mblock") {
1326                 struct mdesc_mblock *m = &mblocks[count++];
1327                 const u64 *val;
1328
1329                 val = mdesc_get_property(md, node, "base", NULL);
1330                 m->base = *val;
1331                 val = mdesc_get_property(md, node, "size", NULL);
1332                 m->size = *val;
1333                 val = mdesc_get_property(md, node,
1334                                          "address-congruence-offset", NULL);
1335
1336                 /* The address-congruence-offset property is optional.
1337                  * Explicity zero it be identifty this.
1338                  */
1339                 if (val)
1340                         m->offset = *val;
1341                 else
1342                         m->offset = 0UL;
1343
1344                 numadbg("MBLOCK[%d]: base[%llx] size[%llx] offset[%llx]\n",
1345                         count - 1, m->base, m->size, m->offset);
1346         }
1347
1348         return 0;
1349 }
1350
1351 static void __init numa_parse_mdesc_group_cpus(struct mdesc_handle *md,
1352                                                u64 grp, cpumask_t *mask)
1353 {
1354         u64 arc;
1355
1356         cpumask_clear(mask);
1357
1358         mdesc_for_each_arc(arc, md, grp, MDESC_ARC_TYPE_BACK) {
1359                 u64 target = mdesc_arc_target(md, arc);
1360                 const char *name = mdesc_node_name(md, target);
1361                 const u64 *id;
1362
1363                 if (strcmp(name, "cpu"))
1364                         continue;
1365                 id = mdesc_get_property(md, target, "id", NULL);
1366                 if (*id < nr_cpu_ids)
1367                         cpumask_set_cpu(*id, mask);
1368         }
1369 }
1370
1371 static struct mdesc_mlgroup * __init find_mlgroup(u64 node)
1372 {
1373         int i;
1374
1375         for (i = 0; i < num_mlgroups; i++) {
1376                 struct mdesc_mlgroup *m = &mlgroups[i];
1377                 if (m->node == node)
1378                         return m;
1379         }
1380         return NULL;
1381 }
1382
1383 int __node_distance(int from, int to)
1384 {
1385         if ((from >= MAX_NUMNODES) || (to >= MAX_NUMNODES)) {
1386                 pr_warn("Returning default NUMA distance value for %d->%d\n",
1387                         from, to);
1388                 return (from == to) ? LOCAL_DISTANCE : REMOTE_DISTANCE;
1389         }
1390         return numa_latency[from][to];
1391 }
1392 EXPORT_SYMBOL(__node_distance);
1393
1394 static int __init find_best_numa_node_for_mlgroup(struct mdesc_mlgroup *grp)
1395 {
1396         int i;
1397
1398         for (i = 0; i < MAX_NUMNODES; i++) {
1399                 struct node_mem_mask *n = &node_masks[i];
1400
1401                 if ((grp->mask == n->mask) && (grp->match == n->match))
1402                         break;
1403         }
1404         return i;
1405 }
1406
1407 static void __init find_numa_latencies_for_group(struct mdesc_handle *md,
1408                                                  u64 grp, int index)
1409 {
1410         u64 arc;
1411
1412         mdesc_for_each_arc(arc, md, grp, MDESC_ARC_TYPE_FWD) {
1413                 int tnode;
1414                 u64 target = mdesc_arc_target(md, arc);
1415                 struct mdesc_mlgroup *m = find_mlgroup(target);
1416
1417                 if (!m)
1418                         continue;
1419                 tnode = find_best_numa_node_for_mlgroup(m);
1420                 if (tnode == MAX_NUMNODES)
1421                         continue;
1422                 numa_latency[index][tnode] = m->latency;
1423         }
1424 }
1425
1426 static int __init numa_attach_mlgroup(struct mdesc_handle *md, u64 grp,
1427                                       int index)
1428 {
1429         struct mdesc_mlgroup *candidate = NULL;
1430         u64 arc, best_latency = ~(u64)0;
1431         struct node_mem_mask *n;
1432
1433         mdesc_for_each_arc(arc, md, grp, MDESC_ARC_TYPE_FWD) {
1434                 u64 target = mdesc_arc_target(md, arc);
1435                 struct mdesc_mlgroup *m = find_mlgroup(target);
1436                 if (!m)
1437                         continue;
1438                 if (m->latency < best_latency) {
1439                         candidate = m;
1440                         best_latency = m->latency;
1441                 }
1442         }
1443         if (!candidate)
1444                 return -ENOENT;
1445
1446         if (num_node_masks != index) {
1447                 printk(KERN_ERR "Inconsistent NUMA state, "
1448                        "index[%d] != num_node_masks[%d]\n",
1449                        index, num_node_masks);
1450                 return -EINVAL;
1451         }
1452
1453         n = &node_masks[num_node_masks++];
1454
1455         n->mask = candidate->mask;
1456         n->match = candidate->match;
1457
1458         numadbg("NUMA NODE[%d]: mask[%lx] match[%lx] (latency[%llx])\n",
1459                 index, n->mask, n->match, candidate->latency);
1460
1461         return 0;
1462 }
1463
1464 static int __init numa_parse_mdesc_group(struct mdesc_handle *md, u64 grp,
1465                                          int index)
1466 {
1467         cpumask_t mask;
1468         int cpu;
1469
1470         numa_parse_mdesc_group_cpus(md, grp, &mask);
1471
1472         for_each_cpu(cpu, &mask)
1473                 numa_cpu_lookup_table[cpu] = index;
1474         cpumask_copy(&numa_cpumask_lookup_table[index], &mask);
1475
1476         if (numa_debug) {
1477                 printk(KERN_INFO "NUMA GROUP[%d]: cpus [ ", index);
1478                 for_each_cpu(cpu, &mask)
1479                         printk("%d ", cpu);
1480                 printk("]\n");
1481         }
1482
1483         return numa_attach_mlgroup(md, grp, index);
1484 }
1485
1486 static int __init numa_parse_mdesc(void)
1487 {
1488         struct mdesc_handle *md = mdesc_grab();
1489         int i, j, err, count;
1490         u64 node;
1491
1492         node = mdesc_node_by_name(md, MDESC_NODE_NULL, "latency-groups");
1493         if (node == MDESC_NODE_NULL) {
1494                 mdesc_release(md);
1495                 return -ENOENT;
1496         }
1497
1498         err = grab_mblocks(md);
1499         if (err < 0)
1500                 goto out;
1501
1502         err = grab_mlgroups(md);
1503         if (err < 0)
1504                 goto out;
1505
1506         count = 0;
1507         mdesc_for_each_node_by_name(md, node, "group") {
1508                 err = numa_parse_mdesc_group(md, node, count);
1509                 if (err < 0)
1510                         break;
1511                 count++;
1512         }
1513
1514         count = 0;
1515         mdesc_for_each_node_by_name(md, node, "group") {
1516                 find_numa_latencies_for_group(md, node, count);
1517                 count++;
1518         }
1519
1520         /* Normalize numa latency matrix according to ACPI SLIT spec. */
1521         for (i = 0; i < MAX_NUMNODES; i++) {
1522                 u64 self_latency = numa_latency[i][i];
1523
1524                 for (j = 0; j < MAX_NUMNODES; j++) {
1525                         numa_latency[i][j] =
1526                                 (numa_latency[i][j] * LOCAL_DISTANCE) /
1527                                 self_latency;
1528                 }
1529         }
1530
1531         add_node_ranges();
1532
1533         for (i = 0; i < num_node_masks; i++) {
1534                 allocate_node_data(i);
1535                 node_set_online(i);
1536         }
1537
1538         err = 0;
1539 out:
1540         mdesc_release(md);
1541         return err;
1542 }
1543
1544 static int __init numa_parse_jbus(void)
1545 {
1546         unsigned long cpu, index;
1547
1548         /* NUMA node id is encoded in bits 36 and higher, and there is
1549          * a 1-to-1 mapping from CPU ID to NUMA node ID.
1550          */
1551         index = 0;
1552         for_each_present_cpu(cpu) {
1553                 numa_cpu_lookup_table[cpu] = index;
1554                 cpumask_copy(&numa_cpumask_lookup_table[index], cpumask_of(cpu));
1555                 node_masks[index].mask = ~((1UL << 36UL) - 1UL);
1556                 node_masks[index].match = cpu << 36UL;
1557
1558                 index++;
1559         }
1560         num_node_masks = index;
1561
1562         add_node_ranges();
1563
1564         for (index = 0; index < num_node_masks; index++) {
1565                 allocate_node_data(index);
1566                 node_set_online(index);
1567         }
1568
1569         return 0;
1570 }
1571
1572 static int __init numa_parse_sun4u(void)
1573 {
1574         if (tlb_type == cheetah || tlb_type == cheetah_plus) {
1575                 unsigned long ver;
1576
1577                 __asm__ ("rdpr %%ver, %0" : "=r" (ver));
1578                 if ((ver >> 32UL) == __JALAPENO_ID ||
1579                     (ver >> 32UL) == __SERRANO_ID)
1580                         return numa_parse_jbus();
1581         }
1582         return -1;
1583 }
1584
1585 static int __init bootmem_init_numa(void)
1586 {
1587         int i, j;
1588         int err = -1;
1589
1590         numadbg("bootmem_init_numa()\n");
1591
1592         /* Some sane defaults for numa latency values */
1593         for (i = 0; i < MAX_NUMNODES; i++) {
1594                 for (j = 0; j < MAX_NUMNODES; j++)
1595                         numa_latency[i][j] = (i == j) ?
1596                                 LOCAL_DISTANCE : REMOTE_DISTANCE;
1597         }
1598
1599         if (numa_enabled) {
1600                 if (tlb_type == hypervisor)
1601                         err = numa_parse_mdesc();
1602                 else
1603                         err = numa_parse_sun4u();
1604         }
1605         return err;
1606 }
1607
1608 #else
1609
1610 static int bootmem_init_numa(void)
1611 {
1612         return -1;
1613 }
1614
1615 #endif
1616
1617 static void __init bootmem_init_nonnuma(void)
1618 {
1619         unsigned long top_of_ram = memblock_end_of_DRAM();
1620         unsigned long total_ram = memblock_phys_mem_size();
1621
1622         numadbg("bootmem_init_nonnuma()\n");
1623
1624         printk(KERN_INFO "Top of RAM: 0x%lx, Total RAM: 0x%lx\n",
1625                top_of_ram, total_ram);
1626         printk(KERN_INFO "Memory hole size: %ldMB\n",
1627                (top_of_ram - total_ram) >> 20);
1628
1629         init_node_masks_nonnuma();
1630         memblock_set_node(0, PHYS_ADDR_MAX, &memblock.memory, 0);
1631         allocate_node_data(0);
1632         node_set_online(0);
1633 }
1634
1635 static unsigned long __init bootmem_init(unsigned long phys_base)
1636 {
1637         unsigned long end_pfn;
1638
1639         end_pfn = memblock_end_of_DRAM() >> PAGE_SHIFT;
1640         max_pfn = max_low_pfn = end_pfn;
1641         min_low_pfn = (phys_base >> PAGE_SHIFT);
1642
1643         if (bootmem_init_numa() < 0)
1644                 bootmem_init_nonnuma();
1645
1646         /* Dump memblock with node info. */
1647         memblock_dump_all();
1648
1649         /* XXX cpu notifier XXX */
1650
1651         sparse_memory_present_with_active_regions(MAX_NUMNODES);
1652         sparse_init();
1653
1654         return end_pfn;
1655 }
1656
1657 static struct linux_prom64_registers pall[MAX_BANKS] __initdata;
1658 static int pall_ents __initdata;
1659
1660 static unsigned long max_phys_bits = 40;
1661
1662 bool kern_addr_valid(unsigned long addr)
1663 {
1664         pgd_t *pgd;
1665         p4d_t *p4d;
1666         pud_t *pud;
1667         pmd_t *pmd;
1668         pte_t *pte;
1669
1670         if ((long)addr < 0L) {
1671                 unsigned long pa = __pa(addr);
1672
1673                 if ((pa >> max_phys_bits) != 0UL)
1674                         return false;
1675
1676                 return pfn_valid(pa >> PAGE_SHIFT);
1677         }
1678
1679         if (addr >= (unsigned long) KERNBASE &&
1680             addr < (unsigned long)&_end)
1681                 return true;
1682
1683         pgd = pgd_offset_k(addr);
1684         if (pgd_none(*pgd))
1685                 return 0;
1686
1687         p4d = p4d_offset(pgd, addr);
1688         if (p4d_none(*p4d))
1689                 return 0;
1690
1691         pud = pud_offset(p4d, addr);
1692         if (pud_none(*pud))
1693                 return 0;
1694
1695         if (pud_large(*pud))
1696                 return pfn_valid(pud_pfn(*pud));
1697
1698         pmd = pmd_offset(pud, addr);
1699         if (pmd_none(*pmd))
1700                 return 0;
1701
1702         if (pmd_large(*pmd))
1703                 return pfn_valid(pmd_pfn(*pmd));
1704
1705         pte = pte_offset_kernel(pmd, addr);
1706         if (pte_none(*pte))
1707                 return 0;
1708
1709         return pfn_valid(pte_pfn(*pte));
1710 }
1711 EXPORT_SYMBOL(kern_addr_valid);
1712
1713 static unsigned long __ref kernel_map_hugepud(unsigned long vstart,
1714                                               unsigned long vend,
1715                                               pud_t *pud)
1716 {
1717         const unsigned long mask16gb = (1UL << 34) - 1UL;
1718         u64 pte_val = vstart;
1719
1720         /* Each PUD is 8GB */
1721         if ((vstart & mask16gb) ||
1722             (vend - vstart <= mask16gb)) {
1723                 pte_val ^= kern_linear_pte_xor[2];
1724                 pud_val(*pud) = pte_val | _PAGE_PUD_HUGE;
1725
1726                 return vstart + PUD_SIZE;
1727         }
1728
1729         pte_val ^= kern_linear_pte_xor[3];
1730         pte_val |= _PAGE_PUD_HUGE;
1731
1732         vend = vstart + mask16gb + 1UL;
1733         while (vstart < vend) {
1734                 pud_val(*pud) = pte_val;
1735
1736                 pte_val += PUD_SIZE;
1737                 vstart += PUD_SIZE;
1738                 pud++;
1739         }
1740         return vstart;
1741 }
1742
1743 static bool kernel_can_map_hugepud(unsigned long vstart, unsigned long vend,
1744                                    bool guard)
1745 {
1746         if (guard && !(vstart & ~PUD_MASK) && (vend - vstart) >= PUD_SIZE)
1747                 return true;
1748
1749         return false;
1750 }
1751
1752 static unsigned long __ref kernel_map_hugepmd(unsigned long vstart,
1753                                               unsigned long vend,
1754                                               pmd_t *pmd)
1755 {
1756         const unsigned long mask256mb = (1UL << 28) - 1UL;
1757         const unsigned long mask2gb = (1UL << 31) - 1UL;
1758         u64 pte_val = vstart;
1759
1760         /* Each PMD is 8MB */
1761         if ((vstart & mask256mb) ||
1762             (vend - vstart <= mask256mb)) {
1763                 pte_val ^= kern_linear_pte_xor[0];
1764                 pmd_val(*pmd) = pte_val | _PAGE_PMD_HUGE;
1765
1766                 return vstart + PMD_SIZE;
1767         }
1768
1769         if ((vstart & mask2gb) ||
1770             (vend - vstart <= mask2gb)) {
1771                 pte_val ^= kern_linear_pte_xor[1];
1772                 pte_val |= _PAGE_PMD_HUGE;
1773                 vend = vstart + mask256mb + 1UL;
1774         } else {
1775                 pte_val ^= kern_linear_pte_xor[2];
1776                 pte_val |= _PAGE_PMD_HUGE;
1777                 vend = vstart + mask2gb + 1UL;
1778         }
1779
1780         while (vstart < vend) {
1781                 pmd_val(*pmd) = pte_val;
1782
1783                 pte_val += PMD_SIZE;
1784                 vstart += PMD_SIZE;
1785                 pmd++;
1786         }
1787
1788         return vstart;
1789 }
1790
1791 static bool kernel_can_map_hugepmd(unsigned long vstart, unsigned long vend,
1792                                    bool guard)
1793 {
1794         if (guard && !(vstart & ~PMD_MASK) && (vend - vstart) >= PMD_SIZE)
1795                 return true;
1796
1797         return false;
1798 }
1799
1800 static unsigned long __ref kernel_map_range(unsigned long pstart,
1801                                             unsigned long pend, pgprot_t prot,
1802                                             bool use_huge)
1803 {
1804         unsigned long vstart = PAGE_OFFSET + pstart;
1805         unsigned long vend = PAGE_OFFSET + pend;
1806         unsigned long alloc_bytes = 0UL;
1807
1808         if ((vstart & ~PAGE_MASK) || (vend & ~PAGE_MASK)) {
1809                 prom_printf("kernel_map: Unaligned physmem[%lx:%lx]\n",
1810                             vstart, vend);
1811                 prom_halt();
1812         }
1813
1814         while (vstart < vend) {
1815                 unsigned long this_end, paddr = __pa(vstart);
1816                 pgd_t *pgd = pgd_offset_k(vstart);
1817                 p4d_t *p4d;
1818                 pud_t *pud;
1819                 pmd_t *pmd;
1820                 pte_t *pte;
1821
1822                 if (pgd_none(*pgd)) {
1823                         pud_t *new;
1824
1825                         new = memblock_alloc_from(PAGE_SIZE, PAGE_SIZE,
1826                                                   PAGE_SIZE);
1827                         if (!new)
1828                                 goto err_alloc;
1829                         alloc_bytes += PAGE_SIZE;
1830                         pgd_populate(&init_mm, pgd, new);
1831                 }
1832
1833                 p4d = p4d_offset(pgd, vstart);
1834                 if (p4d_none(*p4d)) {
1835                         pud_t *new;
1836
1837                         new = memblock_alloc_from(PAGE_SIZE, PAGE_SIZE,
1838                                                   PAGE_SIZE);
1839                         if (!new)
1840                                 goto err_alloc;
1841                         alloc_bytes += PAGE_SIZE;
1842                         p4d_populate(&init_mm, p4d, new);
1843                 }
1844
1845                 pud = pud_offset(p4d, vstart);
1846                 if (pud_none(*pud)) {
1847                         pmd_t *new;
1848
1849                         if (kernel_can_map_hugepud(vstart, vend, use_huge)) {
1850                                 vstart = kernel_map_hugepud(vstart, vend, pud);
1851                                 continue;
1852                         }
1853                         new = memblock_alloc_from(PAGE_SIZE, PAGE_SIZE,
1854                                                   PAGE_SIZE);
1855                         if (!new)
1856                                 goto err_alloc;
1857                         alloc_bytes += PAGE_SIZE;
1858                         pud_populate(&init_mm, pud, new);
1859                 }
1860
1861                 pmd = pmd_offset(pud, vstart);
1862                 if (pmd_none(*pmd)) {
1863                         pte_t *new;
1864
1865                         if (kernel_can_map_hugepmd(vstart, vend, use_huge)) {
1866                                 vstart = kernel_map_hugepmd(vstart, vend, pmd);
1867                                 continue;
1868                         }
1869                         new = memblock_alloc_from(PAGE_SIZE, PAGE_SIZE,
1870                                                   PAGE_SIZE);
1871                         if (!new)
1872                                 goto err_alloc;
1873                         alloc_bytes += PAGE_SIZE;
1874                         pmd_populate_kernel(&init_mm, pmd, new);
1875                 }
1876
1877                 pte = pte_offset_kernel(pmd, vstart);
1878                 this_end = (vstart + PMD_SIZE) & PMD_MASK;
1879                 if (this_end > vend)
1880                         this_end = vend;
1881
1882                 while (vstart < this_end) {
1883                         pte_val(*pte) = (paddr | pgprot_val(prot));
1884
1885                         vstart += PAGE_SIZE;
1886                         paddr += PAGE_SIZE;
1887                         pte++;
1888                 }
1889         }
1890
1891         return alloc_bytes;
1892
1893 err_alloc:
1894         panic("%s: Failed to allocate %lu bytes align=%lx from=%lx\n",
1895               __func__, PAGE_SIZE, PAGE_SIZE, PAGE_SIZE);
1896         return -ENOMEM;
1897 }
1898
1899 static void __init flush_all_kernel_tsbs(void)
1900 {
1901         int i;
1902
1903         for (i = 0; i < KERNEL_TSB_NENTRIES; i++) {
1904                 struct tsb *ent = &swapper_tsb[i];
1905
1906                 ent->tag = (1UL << TSB_TAG_INVALID_BIT);
1907         }
1908 #ifndef CONFIG_DEBUG_PAGEALLOC
1909         for (i = 0; i < KERNEL_TSB4M_NENTRIES; i++) {
1910                 struct tsb *ent = &swapper_4m_tsb[i];
1911
1912                 ent->tag = (1UL << TSB_TAG_INVALID_BIT);
1913         }
1914 #endif
1915 }
1916
1917 extern unsigned int kvmap_linear_patch[1];
1918
1919 static void __init kernel_physical_mapping_init(void)
1920 {
1921         unsigned long i, mem_alloced = 0UL;
1922         bool use_huge = true;
1923
1924 #ifdef CONFIG_DEBUG_PAGEALLOC
1925         use_huge = false;
1926 #endif
1927         for (i = 0; i < pall_ents; i++) {
1928                 unsigned long phys_start, phys_end;
1929
1930                 phys_start = pall[i].phys_addr;
1931                 phys_end = phys_start + pall[i].reg_size;
1932
1933                 mem_alloced += kernel_map_range(phys_start, phys_end,
1934                                                 PAGE_KERNEL, use_huge);
1935         }
1936
1937         printk("Allocated %ld bytes for kernel page tables.\n",
1938                mem_alloced);
1939
1940         kvmap_linear_patch[0] = 0x01000000; /* nop */
1941         flushi(&kvmap_linear_patch[0]);
1942
1943         flush_all_kernel_tsbs();
1944
1945         __flush_tlb_all();
1946 }
1947
1948 #ifdef CONFIG_DEBUG_PAGEALLOC
1949 void __kernel_map_pages(struct page *page, int numpages, int enable)
1950 {
1951         unsigned long phys_start = page_to_pfn(page) << PAGE_SHIFT;
1952         unsigned long phys_end = phys_start + (numpages * PAGE_SIZE);
1953
1954         kernel_map_range(phys_start, phys_end,
1955                          (enable ? PAGE_KERNEL : __pgprot(0)), false);
1956
1957         flush_tsb_kernel_range(PAGE_OFFSET + phys_start,
1958                                PAGE_OFFSET + phys_end);
1959
1960         /* we should perform an IPI and flush all tlbs,
1961          * but that can deadlock->flush only current cpu.
1962          */
1963         __flush_tlb_kernel_range(PAGE_OFFSET + phys_start,
1964                                  PAGE_OFFSET + phys_end);
1965 }
1966 #endif
1967
1968 unsigned long __init find_ecache_flush_span(unsigned long size)
1969 {
1970         int i;
1971
1972         for (i = 0; i < pavail_ents; i++) {
1973                 if (pavail[i].reg_size >= size)
1974                         return pavail[i].phys_addr;
1975         }
1976
1977         return ~0UL;
1978 }
1979
1980 unsigned long PAGE_OFFSET;
1981 EXPORT_SYMBOL(PAGE_OFFSET);
1982
1983 unsigned long VMALLOC_END   = 0x0000010000000000UL;
1984 EXPORT_SYMBOL(VMALLOC_END);
1985
1986 unsigned long sparc64_va_hole_top =    0xfffff80000000000UL;
1987 unsigned long sparc64_va_hole_bottom = 0x0000080000000000UL;
1988
1989 static void __init setup_page_offset(void)
1990 {
1991         if (tlb_type == cheetah || tlb_type == cheetah_plus) {
1992                 /* Cheetah/Panther support a full 64-bit virtual
1993                  * address, so we can use all that our page tables
1994                  * support.
1995                  */
1996                 sparc64_va_hole_top =    0xfff0000000000000UL;
1997                 sparc64_va_hole_bottom = 0x0010000000000000UL;
1998
1999                 max_phys_bits = 42;
2000         } else if (tlb_type == hypervisor) {
2001                 switch (sun4v_chip_type) {
2002                 case SUN4V_CHIP_NIAGARA1:
2003                 case SUN4V_CHIP_NIAGARA2:
2004                         /* T1 and T2 support 48-bit virtual addresses.  */
2005                         sparc64_va_hole_top =    0xffff800000000000UL;
2006                         sparc64_va_hole_bottom = 0x0000800000000000UL;
2007
2008                         max_phys_bits = 39;
2009                         break;
2010                 case SUN4V_CHIP_NIAGARA3:
2011                         /* T3 supports 48-bit virtual addresses.  */
2012                         sparc64_va_hole_top =    0xffff800000000000UL;
2013                         sparc64_va_hole_bottom = 0x0000800000000000UL;
2014
2015                         max_phys_bits = 43;
2016                         break;
2017                 case SUN4V_CHIP_NIAGARA4:
2018                 case SUN4V_CHIP_NIAGARA5:
2019                 case SUN4V_CHIP_SPARC64X:
2020                 case SUN4V_CHIP_SPARC_M6:
2021                         /* T4 and later support 52-bit virtual addresses.  */
2022                         sparc64_va_hole_top =    0xfff8000000000000UL;
2023                         sparc64_va_hole_bottom = 0x0008000000000000UL;
2024                         max_phys_bits = 47;
2025                         break;
2026                 case SUN4V_CHIP_SPARC_M7:
2027                 case SUN4V_CHIP_SPARC_SN:
2028                         /* M7 and later support 52-bit virtual addresses.  */
2029                         sparc64_va_hole_top =    0xfff8000000000000UL;
2030                         sparc64_va_hole_bottom = 0x0008000000000000UL;
2031                         max_phys_bits = 49;
2032                         break;
2033                 case SUN4V_CHIP_SPARC_M8:
2034                 default:
2035                         /* M8 and later support 54-bit virtual addresses.
2036                          * However, restricting M8 and above VA bits to 53
2037                          * as 4-level page table cannot support more than
2038                          * 53 VA bits.
2039                          */
2040                         sparc64_va_hole_top =    0xfff0000000000000UL;
2041                         sparc64_va_hole_bottom = 0x0010000000000000UL;
2042                         max_phys_bits = 51;
2043                         break;
2044                 }
2045         }
2046
2047         if (max_phys_bits > MAX_PHYS_ADDRESS_BITS) {
2048                 prom_printf("MAX_PHYS_ADDRESS_BITS is too small, need %lu\n",
2049                             max_phys_bits);
2050                 prom_halt();
2051         }
2052
2053         PAGE_OFFSET = sparc64_va_hole_top;
2054         VMALLOC_END = ((sparc64_va_hole_bottom >> 1) +
2055                        (sparc64_va_hole_bottom >> 2));
2056
2057         pr_info("MM: PAGE_OFFSET is 0x%016lx (max_phys_bits == %lu)\n",
2058                 PAGE_OFFSET, max_phys_bits);
2059         pr_info("MM: VMALLOC [0x%016lx --> 0x%016lx]\n",
2060                 VMALLOC_START, VMALLOC_END);
2061         pr_info("MM: VMEMMAP [0x%016lx --> 0x%016lx]\n",
2062                 VMEMMAP_BASE, VMEMMAP_BASE << 1);
2063 }
2064
2065 static void __init tsb_phys_patch(void)
2066 {
2067         struct tsb_ldquad_phys_patch_entry *pquad;
2068         struct tsb_phys_patch_entry *p;
2069
2070         pquad = &__tsb_ldquad_phys_patch;
2071         while (pquad < &__tsb_ldquad_phys_patch_end) {
2072                 unsigned long addr = pquad->addr;
2073
2074                 if (tlb_type == hypervisor)
2075                         *(unsigned int *) addr = pquad->sun4v_insn;
2076                 else
2077                         *(unsigned int *) addr = pquad->sun4u_insn;
2078                 wmb();
2079                 __asm__ __volatile__("flush     %0"
2080                                      : /* no outputs */
2081                                      : "r" (addr));
2082
2083                 pquad++;
2084         }
2085
2086         p = &__tsb_phys_patch;
2087         while (p < &__tsb_phys_patch_end) {
2088                 unsigned long addr = p->addr;
2089
2090                 *(unsigned int *) addr = p->insn;
2091                 wmb();
2092                 __asm__ __volatile__("flush     %0"
2093                                      : /* no outputs */
2094                                      : "r" (addr));
2095
2096                 p++;
2097         }
2098 }
2099
2100 /* Don't mark as init, we give this to the Hypervisor.  */
2101 #ifndef CONFIG_DEBUG_PAGEALLOC
2102 #define NUM_KTSB_DESCR  2
2103 #else
2104 #define NUM_KTSB_DESCR  1
2105 #endif
2106 static struct hv_tsb_descr ktsb_descr[NUM_KTSB_DESCR];
2107
2108 /* The swapper TSBs are loaded with a base sequence of:
2109  *
2110  *      sethi   %uhi(SYMBOL), REG1
2111  *      sethi   %hi(SYMBOL), REG2
2112  *      or      REG1, %ulo(SYMBOL), REG1
2113  *      or      REG2, %lo(SYMBOL), REG2
2114  *      sllx    REG1, 32, REG1
2115  *      or      REG1, REG2, REG1
2116  *
2117  * When we use physical addressing for the TSB accesses, we patch the
2118  * first four instructions in the above sequence.
2119  */
2120
2121 static void patch_one_ktsb_phys(unsigned int *start, unsigned int *end, unsigned long pa)
2122 {
2123         unsigned long high_bits, low_bits;
2124
2125         high_bits = (pa >> 32) & 0xffffffff;
2126         low_bits = (pa >> 0) & 0xffffffff;
2127
2128         while (start < end) {
2129                 unsigned int *ia = (unsigned int *)(unsigned long)*start;
2130
2131                 ia[0] = (ia[0] & ~0x3fffff) | (high_bits >> 10);
2132                 __asm__ __volatile__("flush     %0" : : "r" (ia));
2133
2134                 ia[1] = (ia[1] & ~0x3fffff) | (low_bits >> 10);
2135                 __asm__ __volatile__("flush     %0" : : "r" (ia + 1));
2136
2137                 ia[2] = (ia[2] & ~0x1fff) | (high_bits & 0x3ff);
2138                 __asm__ __volatile__("flush     %0" : : "r" (ia + 2));
2139
2140                 ia[3] = (ia[3] & ~0x1fff) | (low_bits & 0x3ff);
2141                 __asm__ __volatile__("flush     %0" : : "r" (ia + 3));
2142
2143                 start++;
2144         }
2145 }
2146
2147 static void ktsb_phys_patch(void)
2148 {
2149         extern unsigned int __swapper_tsb_phys_patch;
2150         extern unsigned int __swapper_tsb_phys_patch_end;
2151         unsigned long ktsb_pa;
2152
2153         ktsb_pa = kern_base + ((unsigned long)&swapper_tsb[0] - KERNBASE);
2154         patch_one_ktsb_phys(&__swapper_tsb_phys_patch,
2155                             &__swapper_tsb_phys_patch_end, ktsb_pa);
2156 #ifndef CONFIG_DEBUG_PAGEALLOC
2157         {
2158         extern unsigned int __swapper_4m_tsb_phys_patch;
2159         extern unsigned int __swapper_4m_tsb_phys_patch_end;
2160         ktsb_pa = (kern_base +
2161                    ((unsigned long)&swapper_4m_tsb[0] - KERNBASE));
2162         patch_one_ktsb_phys(&__swapper_4m_tsb_phys_patch,
2163                             &__swapper_4m_tsb_phys_patch_end, ktsb_pa);
2164         }
2165 #endif
2166 }
2167
2168 static void __init sun4v_ktsb_init(void)
2169 {
2170         unsigned long ktsb_pa;
2171
2172         /* First KTSB for PAGE_SIZE mappings.  */
2173         ktsb_pa = kern_base + ((unsigned long)&swapper_tsb[0] - KERNBASE);
2174
2175         switch (PAGE_SIZE) {
2176         case 8 * 1024:
2177         default:
2178                 ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_8K;
2179                 ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_8K;
2180                 break;
2181
2182         case 64 * 1024:
2183                 ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_64K;
2184                 ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_64K;
2185                 break;
2186
2187         case 512 * 1024:
2188                 ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_512K;
2189                 ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_512K;
2190                 break;
2191
2192         case 4 * 1024 * 1024:
2193                 ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_4MB;
2194                 ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_4MB;
2195                 break;
2196         }
2197
2198         ktsb_descr[0].assoc = 1;
2199         ktsb_descr[0].num_ttes = KERNEL_TSB_NENTRIES;
2200         ktsb_descr[0].ctx_idx = 0;
2201         ktsb_descr[0].tsb_base = ktsb_pa;
2202         ktsb_descr[0].resv = 0;
2203
2204 #ifndef CONFIG_DEBUG_PAGEALLOC
2205         /* Second KTSB for 4MB/256MB/2GB/16GB mappings.  */
2206         ktsb_pa = (kern_base +
2207                    ((unsigned long)&swapper_4m_tsb[0] - KERNBASE));
2208
2209         ktsb_descr[1].pgsz_idx = HV_PGSZ_IDX_4MB;
2210         ktsb_descr[1].pgsz_mask = ((HV_PGSZ_MASK_4MB |
2211                                     HV_PGSZ_MASK_256MB |
2212                                     HV_PGSZ_MASK_2GB |
2213                                     HV_PGSZ_MASK_16GB) &
2214                                    cpu_pgsz_mask);
2215         ktsb_descr[1].assoc = 1;
2216         ktsb_descr[1].num_ttes = KERNEL_TSB4M_NENTRIES;
2217         ktsb_descr[1].ctx_idx = 0;
2218         ktsb_descr[1].tsb_base = ktsb_pa;
2219         ktsb_descr[1].resv = 0;
2220 #endif
2221 }
2222
2223 void sun4v_ktsb_register(void)
2224 {
2225         unsigned long pa, ret;
2226
2227         pa = kern_base + ((unsigned long)&ktsb_descr[0] - KERNBASE);
2228
2229         ret = sun4v_mmu_tsb_ctx0(NUM_KTSB_DESCR, pa);
2230         if (ret != 0) {
2231                 prom_printf("hypervisor_mmu_tsb_ctx0[%lx]: "
2232                             "errors with %lx\n", pa, ret);
2233                 prom_halt();
2234         }
2235 }
2236
2237 static void __init sun4u_linear_pte_xor_finalize(void)
2238 {
2239 #ifndef CONFIG_DEBUG_PAGEALLOC
2240         /* This is where we would add Panther support for
2241          * 32MB and 256MB pages.
2242          */
2243 #endif
2244 }
2245
2246 static void __init sun4v_linear_pte_xor_finalize(void)
2247 {
2248         unsigned long pagecv_flag;
2249
2250         /* Bit 9 of TTE is no longer CV bit on M7 processor and it instead
2251          * enables MCD error. Do not set bit 9 on M7 processor.
2252          */
2253         switch (sun4v_chip_type) {
2254         case SUN4V_CHIP_SPARC_M7:
2255         case SUN4V_CHIP_SPARC_M8:
2256         case SUN4V_CHIP_SPARC_SN:
2257                 pagecv_flag = 0x00;
2258                 break;
2259         default:
2260                 pagecv_flag = _PAGE_CV_4V;
2261                 break;
2262         }
2263 #ifndef CONFIG_DEBUG_PAGEALLOC
2264         if (cpu_pgsz_mask & HV_PGSZ_MASK_256MB) {
2265                 kern_linear_pte_xor[1] = (_PAGE_VALID | _PAGE_SZ256MB_4V) ^
2266                         PAGE_OFFSET;
2267                 kern_linear_pte_xor[1] |= (_PAGE_CP_4V | pagecv_flag |
2268                                            _PAGE_P_4V | _PAGE_W_4V);
2269         } else {
2270                 kern_linear_pte_xor[1] = kern_linear_pte_xor[0];
2271         }
2272
2273         if (cpu_pgsz_mask & HV_PGSZ_MASK_2GB) {
2274                 kern_linear_pte_xor[2] = (_PAGE_VALID | _PAGE_SZ2GB_4V) ^
2275                         PAGE_OFFSET;
2276                 kern_linear_pte_xor[2] |= (_PAGE_CP_4V | pagecv_flag |
2277                                            _PAGE_P_4V | _PAGE_W_4V);
2278         } else {
2279                 kern_linear_pte_xor[2] = kern_linear_pte_xor[1];
2280         }
2281
2282         if (cpu_pgsz_mask & HV_PGSZ_MASK_16GB) {
2283                 kern_linear_pte_xor[3] = (_PAGE_VALID | _PAGE_SZ16GB_4V) ^
2284                         PAGE_OFFSET;
2285                 kern_linear_pte_xor[3] |= (_PAGE_CP_4V | pagecv_flag |
2286                                            _PAGE_P_4V | _PAGE_W_4V);
2287         } else {
2288                 kern_linear_pte_xor[3] = kern_linear_pte_xor[2];
2289         }
2290 #endif
2291 }
2292
2293 /* paging_init() sets up the page tables */
2294
2295 static unsigned long last_valid_pfn;
2296
2297 static void sun4u_pgprot_init(void);
2298 static void sun4v_pgprot_init(void);
2299
2300 #define _PAGE_CACHE_4U  (_PAGE_CP_4U | _PAGE_CV_4U)
2301 #define _PAGE_CACHE_4V  (_PAGE_CP_4V | _PAGE_CV_4V)
2302 #define __DIRTY_BITS_4U  (_PAGE_MODIFIED_4U | _PAGE_WRITE_4U | _PAGE_W_4U)
2303 #define __DIRTY_BITS_4V  (_PAGE_MODIFIED_4V | _PAGE_WRITE_4V | _PAGE_W_4V)
2304 #define __ACCESS_BITS_4U (_PAGE_ACCESSED_4U | _PAGE_READ_4U | _PAGE_R)
2305 #define __ACCESS_BITS_4V (_PAGE_ACCESSED_4V | _PAGE_READ_4V | _PAGE_R)
2306
2307 /* We need to exclude reserved regions. This exclusion will include
2308  * vmlinux and initrd. To be more precise the initrd size could be used to
2309  * compute a new lower limit because it is freed later during initialization.
2310  */
2311 static void __init reduce_memory(phys_addr_t limit_ram)
2312 {
2313         limit_ram += memblock_reserved_size();
2314         memblock_enforce_memory_limit(limit_ram);
2315 }
2316
2317 void __init paging_init(void)
2318 {
2319         unsigned long end_pfn, shift, phys_base;
2320         unsigned long real_end, i;
2321
2322         setup_page_offset();
2323
2324         /* These build time checkes make sure that the dcache_dirty_cpu()
2325          * page->flags usage will work.
2326          *
2327          * When a page gets marked as dcache-dirty, we store the
2328          * cpu number starting at bit 32 in the page->flags.  Also,
2329          * functions like clear_dcache_dirty_cpu use the cpu mask
2330          * in 13-bit signed-immediate instruction fields.
2331          */
2332
2333         /*
2334          * Page flags must not reach into upper 32 bits that are used
2335          * for the cpu number
2336          */
2337         BUILD_BUG_ON(NR_PAGEFLAGS > 32);
2338
2339         /*
2340          * The bit fields placed in the high range must not reach below
2341          * the 32 bit boundary. Otherwise we cannot place the cpu field
2342          * at the 32 bit boundary.
2343          */
2344         BUILD_BUG_ON(SECTIONS_WIDTH + NODES_WIDTH + ZONES_WIDTH +
2345                 ilog2(roundup_pow_of_two(NR_CPUS)) > 32);
2346
2347         BUILD_BUG_ON(NR_CPUS > 4096);
2348
2349         kern_base = (prom_boot_mapping_phys_low >> ILOG2_4MB) << ILOG2_4MB;
2350         kern_size = (unsigned long)&_end - (unsigned long)KERNBASE;
2351
2352         /* Invalidate both kernel TSBs.  */
2353         memset(swapper_tsb, 0x40, sizeof(swapper_tsb));
2354 #ifndef CONFIG_DEBUG_PAGEALLOC
2355         memset(swapper_4m_tsb, 0x40, sizeof(swapper_4m_tsb));
2356 #endif
2357
2358         /* TTE.cv bit on sparc v9 occupies the same position as TTE.mcde
2359          * bit on M7 processor. This is a conflicting usage of the same
2360          * bit. Enabling TTE.cv on M7 would turn on Memory Corruption
2361          * Detection error on all pages and this will lead to problems
2362          * later. Kernel does not run with MCD enabled and hence rest
2363          * of the required steps to fully configure memory corruption
2364          * detection are not taken. We need to ensure TTE.mcde is not
2365          * set on M7 processor. Compute the value of cacheability
2366          * flag for use later taking this into consideration.
2367          */
2368         switch (sun4v_chip_type) {
2369         case SUN4V_CHIP_SPARC_M7:
2370         case SUN4V_CHIP_SPARC_M8:
2371         case SUN4V_CHIP_SPARC_SN:
2372                 page_cache4v_flag = _PAGE_CP_4V;
2373                 break;
2374         default:
2375                 page_cache4v_flag = _PAGE_CACHE_4V;
2376                 break;
2377         }
2378
2379         if (tlb_type == hypervisor)
2380                 sun4v_pgprot_init();
2381         else
2382                 sun4u_pgprot_init();
2383
2384         if (tlb_type == cheetah_plus ||
2385             tlb_type == hypervisor) {
2386                 tsb_phys_patch();
2387                 ktsb_phys_patch();
2388         }
2389
2390         if (tlb_type == hypervisor)
2391                 sun4v_patch_tlb_handlers();
2392
2393         /* Find available physical memory...
2394          *
2395          * Read it twice in order to work around a bug in openfirmware.
2396          * The call to grab this table itself can cause openfirmware to
2397          * allocate memory, which in turn can take away some space from
2398          * the list of available memory.  Reading it twice makes sure
2399          * we really do get the final value.
2400          */
2401         read_obp_translations();
2402         read_obp_memory("reg", &pall[0], &pall_ents);
2403         read_obp_memory("available", &pavail[0], &pavail_ents);
2404         read_obp_memory("available", &pavail[0], &pavail_ents);
2405
2406         phys_base = 0xffffffffffffffffUL;
2407         for (i = 0; i < pavail_ents; i++) {
2408                 phys_base = min(phys_base, pavail[i].phys_addr);
2409                 memblock_add(pavail[i].phys_addr, pavail[i].reg_size);
2410         }
2411
2412         memblock_reserve(kern_base, kern_size);
2413
2414         find_ramdisk(phys_base);
2415
2416         if (cmdline_memory_size)
2417                 reduce_memory(cmdline_memory_size);
2418
2419         memblock_allow_resize();
2420         memblock_dump_all();
2421
2422         set_bit(0, mmu_context_bmap);
2423
2424         shift = kern_base + PAGE_OFFSET - ((unsigned long)KERNBASE);
2425
2426         real_end = (unsigned long)_end;
2427         num_kernel_image_mappings = DIV_ROUND_UP(real_end - KERNBASE, 1 << ILOG2_4MB);
2428         printk("Kernel: Using %d locked TLB entries for main kernel image.\n",
2429                num_kernel_image_mappings);
2430
2431         /* Set kernel pgd to upper alias so physical page computations
2432          * work.
2433          */
2434         init_mm.pgd += ((shift) / (sizeof(pgd_t)));
2435         
2436         memset(swapper_pg_dir, 0, sizeof(swapper_pg_dir));
2437
2438         inherit_prom_mappings();
2439         
2440         /* Ok, we can use our TLB miss and window trap handlers safely.  */
2441         setup_tba();
2442
2443         __flush_tlb_all();
2444
2445         prom_build_devicetree();
2446         of_populate_present_mask();
2447 #ifndef CONFIG_SMP
2448         of_fill_in_cpu_data();
2449 #endif
2450
2451         if (tlb_type == hypervisor) {
2452                 sun4v_mdesc_init();
2453                 mdesc_populate_present_mask(cpu_all_mask);
2454 #ifndef CONFIG_SMP
2455                 mdesc_fill_in_cpu_data(cpu_all_mask);
2456 #endif
2457                 mdesc_get_page_sizes(cpu_all_mask, &cpu_pgsz_mask);
2458
2459                 sun4v_linear_pte_xor_finalize();
2460
2461                 sun4v_ktsb_init();
2462                 sun4v_ktsb_register();
2463         } else {
2464                 unsigned long impl, ver;
2465
2466                 cpu_pgsz_mask = (HV_PGSZ_MASK_8K | HV_PGSZ_MASK_64K |
2467                                  HV_PGSZ_MASK_512K | HV_PGSZ_MASK_4MB);
2468
2469                 __asm__ __volatile__("rdpr %%ver, %0" : "=r" (ver));
2470                 impl = ((ver >> 32) & 0xffff);
2471                 if (impl == PANTHER_IMPL)
2472                         cpu_pgsz_mask |= (HV_PGSZ_MASK_32MB |
2473                                           HV_PGSZ_MASK_256MB);
2474
2475                 sun4u_linear_pte_xor_finalize();
2476         }
2477
2478         /* Flush the TLBs and the 4M TSB so that the updated linear
2479          * pte XOR settings are realized for all mappings.
2480          */
2481         __flush_tlb_all();
2482 #ifndef CONFIG_DEBUG_PAGEALLOC
2483         memset(swapper_4m_tsb, 0x40, sizeof(swapper_4m_tsb));
2484 #endif
2485         __flush_tlb_all();
2486
2487         /* Setup bootmem... */
2488         last_valid_pfn = end_pfn = bootmem_init(phys_base);
2489
2490         kernel_physical_mapping_init();
2491
2492         {
2493                 unsigned long max_zone_pfns[MAX_NR_ZONES];
2494
2495                 memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
2496
2497                 max_zone_pfns[ZONE_NORMAL] = end_pfn;
2498
2499                 free_area_init(max_zone_pfns);
2500         }
2501
2502         printk("Booting Linux...\n");
2503 }
2504
2505 int page_in_phys_avail(unsigned long paddr)
2506 {
2507         int i;
2508
2509         paddr &= PAGE_MASK;
2510
2511         for (i = 0; i < pavail_ents; i++) {
2512                 unsigned long start, end;
2513
2514                 start = pavail[i].phys_addr;
2515                 end = start + pavail[i].reg_size;
2516
2517                 if (paddr >= start && paddr < end)
2518                         return 1;
2519         }
2520         if (paddr >= kern_base && paddr < (kern_base + kern_size))
2521                 return 1;
2522 #ifdef CONFIG_BLK_DEV_INITRD
2523         if (paddr >= __pa(initrd_start) &&
2524             paddr < __pa(PAGE_ALIGN(initrd_end)))
2525                 return 1;
2526 #endif
2527
2528         return 0;
2529 }
2530
2531 static void __init register_page_bootmem_info(void)
2532 {
2533 #ifdef CONFIG_NEED_MULTIPLE_NODES
2534         int i;
2535
2536         for_each_online_node(i)
2537                 if (NODE_DATA(i)->node_spanned_pages)
2538                         register_page_bootmem_info_node(NODE_DATA(i));
2539 #endif
2540 }
2541 void __init mem_init(void)
2542 {
2543         high_memory = __va(last_valid_pfn << PAGE_SHIFT);
2544
2545         memblock_free_all();
2546
2547         /*
2548          * Must be done after boot memory is put on freelist, because here we
2549          * might set fields in deferred struct pages that have not yet been
2550          * initialized, and memblock_free_all() initializes all the reserved
2551          * deferred pages for us.
2552          */
2553         register_page_bootmem_info();
2554
2555         /*
2556          * Set up the zero page, mark it reserved, so that page count
2557          * is not manipulated when freeing the page from user ptes.
2558          */
2559         mem_map_zero = alloc_pages(GFP_KERNEL|__GFP_ZERO, 0);
2560         if (mem_map_zero == NULL) {
2561                 prom_printf("paging_init: Cannot alloc zero page.\n");
2562                 prom_halt();
2563         }
2564         mark_page_reserved(mem_map_zero);
2565
2566         mem_init_print_info(NULL);
2567
2568         if (tlb_type == cheetah || tlb_type == cheetah_plus)
2569                 cheetah_ecache_flush_init();
2570 }
2571
2572 void free_initmem(void)
2573 {
2574         unsigned long addr, initend;
2575         int do_free = 1;
2576
2577         /* If the physical memory maps were trimmed by kernel command
2578          * line options, don't even try freeing this initmem stuff up.
2579          * The kernel image could have been in the trimmed out region
2580          * and if so the freeing below will free invalid page structs.
2581          */
2582         if (cmdline_memory_size)
2583                 do_free = 0;
2584
2585         /*
2586          * The init section is aligned to 8k in vmlinux.lds. Page align for >8k pagesizes.
2587          */
2588         addr = PAGE_ALIGN((unsigned long)(__init_begin));
2589         initend = (unsigned long)(__init_end) & PAGE_MASK;
2590         for (; addr < initend; addr += PAGE_SIZE) {
2591                 unsigned long page;
2592
2593                 page = (addr +
2594                         ((unsigned long) __va(kern_base)) -
2595                         ((unsigned long) KERNBASE));
2596                 memset((void *)addr, POISON_FREE_INITMEM, PAGE_SIZE);
2597
2598                 if (do_free)
2599                         free_reserved_page(virt_to_page(page));
2600         }
2601 }
2602
2603 pgprot_t PAGE_KERNEL __read_mostly;
2604 EXPORT_SYMBOL(PAGE_KERNEL);
2605
2606 pgprot_t PAGE_KERNEL_LOCKED __read_mostly;
2607 pgprot_t PAGE_COPY __read_mostly;
2608
2609 pgprot_t PAGE_SHARED __read_mostly;
2610 EXPORT_SYMBOL(PAGE_SHARED);
2611
2612 unsigned long pg_iobits __read_mostly;
2613
2614 unsigned long _PAGE_IE __read_mostly;
2615 EXPORT_SYMBOL(_PAGE_IE);
2616
2617 unsigned long _PAGE_E __read_mostly;
2618 EXPORT_SYMBOL(_PAGE_E);
2619
2620 unsigned long _PAGE_CACHE __read_mostly;
2621 EXPORT_SYMBOL(_PAGE_CACHE);
2622
2623 #ifdef CONFIG_SPARSEMEM_VMEMMAP
2624 int __meminit vmemmap_populate(unsigned long vstart, unsigned long vend,
2625                                int node, struct vmem_altmap *altmap)
2626 {
2627         unsigned long pte_base;
2628
2629         pte_base = (_PAGE_VALID | _PAGE_SZ4MB_4U |
2630                     _PAGE_CP_4U | _PAGE_CV_4U |
2631                     _PAGE_P_4U | _PAGE_W_4U);
2632         if (tlb_type == hypervisor)
2633                 pte_base = (_PAGE_VALID | _PAGE_SZ4MB_4V |
2634                             page_cache4v_flag | _PAGE_P_4V | _PAGE_W_4V);
2635
2636         pte_base |= _PAGE_PMD_HUGE;
2637
2638         vstart = vstart & PMD_MASK;
2639         vend = ALIGN(vend, PMD_SIZE);
2640         for (; vstart < vend; vstart += PMD_SIZE) {
2641                 pgd_t *pgd = vmemmap_pgd_populate(vstart, node);
2642                 unsigned long pte;
2643                 p4d_t *p4d;
2644                 pud_t *pud;
2645                 pmd_t *pmd;
2646
2647                 if (!pgd)
2648                         return -ENOMEM;
2649
2650                 p4d = vmemmap_p4d_populate(pgd, vstart, node);
2651                 if (!p4d)
2652                         return -ENOMEM;
2653
2654                 pud = vmemmap_pud_populate(p4d, vstart, node);
2655                 if (!pud)
2656                         return -ENOMEM;
2657
2658                 pmd = pmd_offset(pud, vstart);
2659                 pte = pmd_val(*pmd);
2660                 if (!(pte & _PAGE_VALID)) {
2661                         void *block = vmemmap_alloc_block(PMD_SIZE, node);
2662
2663                         if (!block)
2664                                 return -ENOMEM;
2665
2666                         pmd_val(*pmd) = pte_base | __pa(block);
2667                 }
2668         }
2669
2670         return 0;
2671 }
2672
2673 void vmemmap_free(unsigned long start, unsigned long end,
2674                 struct vmem_altmap *altmap)
2675 {
2676 }
2677 #endif /* CONFIG_SPARSEMEM_VMEMMAP */
2678
2679 static void prot_init_common(unsigned long page_none,
2680                              unsigned long page_shared,
2681                              unsigned long page_copy,
2682                              unsigned long page_readonly,
2683                              unsigned long page_exec_bit)
2684 {
2685         PAGE_COPY = __pgprot(page_copy);
2686         PAGE_SHARED = __pgprot(page_shared);
2687
2688         protection_map[0x0] = __pgprot(page_none);
2689         protection_map[0x1] = __pgprot(page_readonly & ~page_exec_bit);
2690         protection_map[0x2] = __pgprot(page_copy & ~page_exec_bit);
2691         protection_map[0x3] = __pgprot(page_copy & ~page_exec_bit);
2692         protection_map[0x4] = __pgprot(page_readonly);
2693         protection_map[0x5] = __pgprot(page_readonly);
2694         protection_map[0x6] = __pgprot(page_copy);
2695         protection_map[0x7] = __pgprot(page_copy);
2696         protection_map[0x8] = __pgprot(page_none);
2697         protection_map[0x9] = __pgprot(page_readonly & ~page_exec_bit);
2698         protection_map[0xa] = __pgprot(page_shared & ~page_exec_bit);
2699         protection_map[0xb] = __pgprot(page_shared & ~page_exec_bit);
2700         protection_map[0xc] = __pgprot(page_readonly);
2701         protection_map[0xd] = __pgprot(page_readonly);
2702         protection_map[0xe] = __pgprot(page_shared);
2703         protection_map[0xf] = __pgprot(page_shared);
2704 }
2705
2706 static void __init sun4u_pgprot_init(void)
2707 {
2708         unsigned long page_none, page_shared, page_copy, page_readonly;
2709         unsigned long page_exec_bit;
2710         int i;
2711
2712         PAGE_KERNEL = __pgprot (_PAGE_PRESENT_4U | _PAGE_VALID |
2713                                 _PAGE_CACHE_4U | _PAGE_P_4U |
2714                                 __ACCESS_BITS_4U | __DIRTY_BITS_4U |
2715                                 _PAGE_EXEC_4U);
2716         PAGE_KERNEL_LOCKED = __pgprot (_PAGE_PRESENT_4U | _PAGE_VALID |
2717                                        _PAGE_CACHE_4U | _PAGE_P_4U |
2718                                        __ACCESS_BITS_4U | __DIRTY_BITS_4U |
2719                                        _PAGE_EXEC_4U | _PAGE_L_4U);
2720
2721         _PAGE_IE = _PAGE_IE_4U;
2722         _PAGE_E = _PAGE_E_4U;
2723         _PAGE_CACHE = _PAGE_CACHE_4U;
2724
2725         pg_iobits = (_PAGE_VALID | _PAGE_PRESENT_4U | __DIRTY_BITS_4U |
2726                      __ACCESS_BITS_4U | _PAGE_E_4U);
2727
2728 #ifdef CONFIG_DEBUG_PAGEALLOC
2729         kern_linear_pte_xor[0] = _PAGE_VALID ^ PAGE_OFFSET;
2730 #else
2731         kern_linear_pte_xor[0] = (_PAGE_VALID | _PAGE_SZ4MB_4U) ^
2732                 PAGE_OFFSET;
2733 #endif
2734         kern_linear_pte_xor[0] |= (_PAGE_CP_4U | _PAGE_CV_4U |
2735                                    _PAGE_P_4U | _PAGE_W_4U);
2736
2737         for (i = 1; i < 4; i++)
2738                 kern_linear_pte_xor[i] = kern_linear_pte_xor[0];
2739
2740         _PAGE_ALL_SZ_BITS =  (_PAGE_SZ4MB_4U | _PAGE_SZ512K_4U |
2741                               _PAGE_SZ64K_4U | _PAGE_SZ8K_4U |
2742                               _PAGE_SZ32MB_4U | _PAGE_SZ256MB_4U);
2743
2744
2745         page_none = _PAGE_PRESENT_4U | _PAGE_ACCESSED_4U | _PAGE_CACHE_4U;
2746         page_shared = (_PAGE_VALID | _PAGE_PRESENT_4U | _PAGE_CACHE_4U |
2747                        __ACCESS_BITS_4U | _PAGE_WRITE_4U | _PAGE_EXEC_4U);
2748         page_copy   = (_PAGE_VALID | _PAGE_PRESENT_4U | _PAGE_CACHE_4U |
2749                        __ACCESS_BITS_4U | _PAGE_EXEC_4U);
2750         page_readonly   = (_PAGE_VALID | _PAGE_PRESENT_4U | _PAGE_CACHE_4U |
2751                            __ACCESS_BITS_4U | _PAGE_EXEC_4U);
2752
2753         page_exec_bit = _PAGE_EXEC_4U;
2754
2755         prot_init_common(page_none, page_shared, page_copy, page_readonly,
2756                          page_exec_bit);
2757 }
2758
2759 static void __init sun4v_pgprot_init(void)
2760 {
2761         unsigned long page_none, page_shared, page_copy, page_readonly;
2762         unsigned long page_exec_bit;
2763         int i;
2764
2765         PAGE_KERNEL = __pgprot (_PAGE_PRESENT_4V | _PAGE_VALID |
2766                                 page_cache4v_flag | _PAGE_P_4V |
2767                                 __ACCESS_BITS_4V | __DIRTY_BITS_4V |
2768                                 _PAGE_EXEC_4V);
2769         PAGE_KERNEL_LOCKED = PAGE_KERNEL;
2770
2771         _PAGE_IE = _PAGE_IE_4V;
2772         _PAGE_E = _PAGE_E_4V;
2773         _PAGE_CACHE = page_cache4v_flag;
2774
2775 #ifdef CONFIG_DEBUG_PAGEALLOC
2776         kern_linear_pte_xor[0] = _PAGE_VALID ^ PAGE_OFFSET;
2777 #else
2778         kern_linear_pte_xor[0] = (_PAGE_VALID | _PAGE_SZ4MB_4V) ^
2779                 PAGE_OFFSET;
2780 #endif
2781         kern_linear_pte_xor[0] |= (page_cache4v_flag | _PAGE_P_4V |
2782                                    _PAGE_W_4V);
2783
2784         for (i = 1; i < 4; i++)
2785                 kern_linear_pte_xor[i] = kern_linear_pte_xor[0];
2786
2787         pg_iobits = (_PAGE_VALID | _PAGE_PRESENT_4V | __DIRTY_BITS_4V |
2788                      __ACCESS_BITS_4V | _PAGE_E_4V);
2789
2790         _PAGE_ALL_SZ_BITS = (_PAGE_SZ16GB_4V | _PAGE_SZ2GB_4V |
2791                              _PAGE_SZ256MB_4V | _PAGE_SZ32MB_4V |
2792                              _PAGE_SZ4MB_4V | _PAGE_SZ512K_4V |
2793                              _PAGE_SZ64K_4V | _PAGE_SZ8K_4V);
2794
2795         page_none = _PAGE_PRESENT_4V | _PAGE_ACCESSED_4V | page_cache4v_flag;
2796         page_shared = (_PAGE_VALID | _PAGE_PRESENT_4V | page_cache4v_flag |
2797                        __ACCESS_BITS_4V | _PAGE_WRITE_4V | _PAGE_EXEC_4V);
2798         page_copy   = (_PAGE_VALID | _PAGE_PRESENT_4V | page_cache4v_flag |
2799                        __ACCESS_BITS_4V | _PAGE_EXEC_4V);
2800         page_readonly = (_PAGE_VALID | _PAGE_PRESENT_4V | page_cache4v_flag |
2801                          __ACCESS_BITS_4V | _PAGE_EXEC_4V);
2802
2803         page_exec_bit = _PAGE_EXEC_4V;
2804
2805         prot_init_common(page_none, page_shared, page_copy, page_readonly,
2806                          page_exec_bit);
2807 }
2808
2809 unsigned long pte_sz_bits(unsigned long sz)
2810 {
2811         if (tlb_type == hypervisor) {
2812                 switch (sz) {
2813                 case 8 * 1024:
2814                 default:
2815                         return _PAGE_SZ8K_4V;
2816                 case 64 * 1024:
2817                         return _PAGE_SZ64K_4V;
2818                 case 512 * 1024:
2819                         return _PAGE_SZ512K_4V;
2820                 case 4 * 1024 * 1024:
2821                         return _PAGE_SZ4MB_4V;
2822                 }
2823         } else {
2824                 switch (sz) {
2825                 case 8 * 1024:
2826                 default:
2827                         return _PAGE_SZ8K_4U;
2828                 case 64 * 1024:
2829                         return _PAGE_SZ64K_4U;
2830                 case 512 * 1024:
2831                         return _PAGE_SZ512K_4U;
2832                 case 4 * 1024 * 1024:
2833                         return _PAGE_SZ4MB_4U;
2834                 }
2835         }
2836 }
2837
2838 pte_t mk_pte_io(unsigned long page, pgprot_t prot, int space, unsigned long page_size)
2839 {
2840         pte_t pte;
2841
2842         pte_val(pte)  = page | pgprot_val(pgprot_noncached(prot));
2843         pte_val(pte) |= (((unsigned long)space) << 32);
2844         pte_val(pte) |= pte_sz_bits(page_size);
2845
2846         return pte;
2847 }
2848
2849 static unsigned long kern_large_tte(unsigned long paddr)
2850 {
2851         unsigned long val;
2852
2853         val = (_PAGE_VALID | _PAGE_SZ4MB_4U |
2854                _PAGE_CP_4U | _PAGE_CV_4U | _PAGE_P_4U |
2855                _PAGE_EXEC_4U | _PAGE_L_4U | _PAGE_W_4U);
2856         if (tlb_type == hypervisor)
2857                 val = (_PAGE_VALID | _PAGE_SZ4MB_4V |
2858                        page_cache4v_flag | _PAGE_P_4V |
2859                        _PAGE_EXEC_4V | _PAGE_W_4V);
2860
2861         return val | paddr;
2862 }
2863
2864 /* If not locked, zap it. */
2865 void __flush_tlb_all(void)
2866 {
2867         unsigned long pstate;
2868         int i;
2869
2870         __asm__ __volatile__("flushw\n\t"
2871                              "rdpr      %%pstate, %0\n\t"
2872                              "wrpr      %0, %1, %%pstate"
2873                              : "=r" (pstate)
2874                              : "i" (PSTATE_IE));
2875         if (tlb_type == hypervisor) {
2876                 sun4v_mmu_demap_all();
2877         } else if (tlb_type == spitfire) {
2878                 for (i = 0; i < 64; i++) {
2879                         /* Spitfire Errata #32 workaround */
2880                         /* NOTE: Always runs on spitfire, so no
2881                          *       cheetah+ page size encodings.
2882                          */
2883                         __asm__ __volatile__("stxa      %0, [%1] %2\n\t"
2884                                              "flush     %%g6"
2885                                              : /* No outputs */
2886                                              : "r" (0),
2887                                              "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
2888
2889                         if (!(spitfire_get_dtlb_data(i) & _PAGE_L_4U)) {
2890                                 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
2891                                                      "membar #Sync"
2892                                                      : /* no outputs */
2893                                                      : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
2894                                 spitfire_put_dtlb_data(i, 0x0UL);
2895                         }
2896
2897                         /* Spitfire Errata #32 workaround */
2898                         /* NOTE: Always runs on spitfire, so no
2899                          *       cheetah+ page size encodings.
2900                          */
2901                         __asm__ __volatile__("stxa      %0, [%1] %2\n\t"
2902                                              "flush     %%g6"
2903                                              : /* No outputs */
2904                                              : "r" (0),
2905                                              "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
2906
2907                         if (!(spitfire_get_itlb_data(i) & _PAGE_L_4U)) {
2908                                 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
2909                                                      "membar #Sync"
2910                                                      : /* no outputs */
2911                                                      : "r" (TLB_TAG_ACCESS), "i" (ASI_IMMU));
2912                                 spitfire_put_itlb_data(i, 0x0UL);
2913                         }
2914                 }
2915         } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
2916                 cheetah_flush_dtlb_all();
2917                 cheetah_flush_itlb_all();
2918         }
2919         __asm__ __volatile__("wrpr      %0, 0, %%pstate"
2920                              : : "r" (pstate));
2921 }
2922
2923 pte_t *pte_alloc_one_kernel(struct mm_struct *mm)
2924 {
2925         struct page *page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2926         pte_t *pte = NULL;
2927
2928         if (page)
2929                 pte = (pte_t *) page_address(page);
2930
2931         return pte;
2932 }
2933
2934 pgtable_t pte_alloc_one(struct mm_struct *mm)
2935 {
2936         struct page *page = alloc_page(GFP_KERNEL | __GFP_ZERO);
2937         if (!page)
2938                 return NULL;
2939         if (!pgtable_pte_page_ctor(page)) {
2940                 free_unref_page(page);
2941                 return NULL;
2942         }
2943         return (pte_t *) page_address(page);
2944 }
2945
2946 void pte_free_kernel(struct mm_struct *mm, pte_t *pte)
2947 {
2948         free_page((unsigned long)pte);
2949 }
2950
2951 static void __pte_free(pgtable_t pte)
2952 {
2953         struct page *page = virt_to_page(pte);
2954
2955         pgtable_pte_page_dtor(page);
2956         __free_page(page);
2957 }
2958
2959 void pte_free(struct mm_struct *mm, pgtable_t pte)
2960 {
2961         __pte_free(pte);
2962 }
2963
2964 void pgtable_free(void *table, bool is_page)
2965 {
2966         if (is_page)
2967                 __pte_free(table);
2968         else
2969                 kmem_cache_free(pgtable_cache, table);
2970 }
2971
2972 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
2973 void update_mmu_cache_pmd(struct vm_area_struct *vma, unsigned long addr,
2974                           pmd_t *pmd)
2975 {
2976         unsigned long pte, flags;
2977         struct mm_struct *mm;
2978         pmd_t entry = *pmd;
2979
2980         if (!pmd_large(entry) || !pmd_young(entry))
2981                 return;
2982
2983         pte = pmd_val(entry);
2984
2985         /* Don't insert a non-valid PMD into the TSB, we'll deadlock.  */
2986         if (!(pte & _PAGE_VALID))
2987                 return;
2988
2989         /* We are fabricating 8MB pages using 4MB real hw pages.  */
2990         pte |= (addr & (1UL << REAL_HPAGE_SHIFT));
2991
2992         mm = vma->vm_mm;
2993
2994         spin_lock_irqsave(&mm->context.lock, flags);
2995
2996         if (mm->context.tsb_block[MM_TSB_HUGE].tsb != NULL)
2997                 __update_mmu_tsb_insert(mm, MM_TSB_HUGE, REAL_HPAGE_SHIFT,
2998                                         addr, pte);
2999
3000         spin_unlock_irqrestore(&mm->context.lock, flags);
3001 }
3002 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
3003
3004 #if defined(CONFIG_HUGETLB_PAGE) || defined(CONFIG_TRANSPARENT_HUGEPAGE)
3005 static void context_reload(void *__data)
3006 {
3007         struct mm_struct *mm = __data;
3008
3009         if (mm == current->mm)
3010                 load_secondary_context(mm);
3011 }
3012
3013 void hugetlb_setup(struct pt_regs *regs)
3014 {
3015         struct mm_struct *mm = current->mm;
3016         struct tsb_config *tp;
3017
3018         if (faulthandler_disabled() || !mm) {
3019                 const struct exception_table_entry *entry;
3020
3021                 entry = search_exception_tables(regs->tpc);
3022                 if (entry) {
3023                         regs->tpc = entry->fixup;
3024                         regs->tnpc = regs->tpc + 4;
3025                         return;
3026                 }
3027                 pr_alert("Unexpected HugeTLB setup in atomic context.\n");
3028                 die_if_kernel("HugeTSB in atomic", regs);
3029         }
3030
3031         tp = &mm->context.tsb_block[MM_TSB_HUGE];
3032         if (likely(tp->tsb == NULL))
3033                 tsb_grow(mm, MM_TSB_HUGE, 0);
3034
3035         tsb_context_switch(mm);
3036         smp_tsb_sync(mm);
3037
3038         /* On UltraSPARC-III+ and later, configure the second half of
3039          * the Data-TLB for huge pages.
3040          */
3041         if (tlb_type == cheetah_plus) {
3042                 bool need_context_reload = false;
3043                 unsigned long ctx;
3044
3045                 spin_lock_irq(&ctx_alloc_lock);
3046                 ctx = mm->context.sparc64_ctx_val;
3047                 ctx &= ~CTX_PGSZ_MASK;
3048                 ctx |= CTX_PGSZ_BASE << CTX_PGSZ0_SHIFT;
3049                 ctx |= CTX_PGSZ_HUGE << CTX_PGSZ1_SHIFT;
3050
3051                 if (ctx != mm->context.sparc64_ctx_val) {
3052                         /* When changing the page size fields, we
3053                          * must perform a context flush so that no
3054                          * stale entries match.  This flush must
3055                          * occur with the original context register
3056                          * settings.
3057                          */
3058                         do_flush_tlb_mm(mm);
3059
3060                         /* Reload the context register of all processors
3061                          * also executing in this address space.
3062                          */
3063                         mm->context.sparc64_ctx_val = ctx;
3064                         need_context_reload = true;
3065                 }
3066                 spin_unlock_irq(&ctx_alloc_lock);
3067
3068                 if (need_context_reload)
3069                         on_each_cpu(context_reload, mm, 0);
3070         }
3071 }
3072 #endif
3073
3074 static struct resource code_resource = {
3075         .name   = "Kernel code",
3076         .flags  = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM
3077 };
3078
3079 static struct resource data_resource = {
3080         .name   = "Kernel data",
3081         .flags  = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM
3082 };
3083
3084 static struct resource bss_resource = {
3085         .name   = "Kernel bss",
3086         .flags  = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM
3087 };
3088
3089 static inline resource_size_t compute_kern_paddr(void *addr)
3090 {
3091         return (resource_size_t) (addr - KERNBASE + kern_base);
3092 }
3093
3094 static void __init kernel_lds_init(void)
3095 {
3096         code_resource.start = compute_kern_paddr(_text);
3097         code_resource.end   = compute_kern_paddr(_etext - 1);
3098         data_resource.start = compute_kern_paddr(_etext);
3099         data_resource.end   = compute_kern_paddr(_edata - 1);
3100         bss_resource.start  = compute_kern_paddr(__bss_start);
3101         bss_resource.end    = compute_kern_paddr(_end - 1);
3102 }
3103
3104 static int __init report_memory(void)
3105 {
3106         int i;
3107         struct resource *res;
3108
3109         kernel_lds_init();
3110
3111         for (i = 0; i < pavail_ents; i++) {
3112                 res = kzalloc(sizeof(struct resource), GFP_KERNEL);
3113
3114                 if (!res) {
3115                         pr_warn("Failed to allocate source.\n");
3116                         break;
3117                 }
3118
3119                 res->name = "System RAM";
3120                 res->start = pavail[i].phys_addr;
3121                 res->end = pavail[i].phys_addr + pavail[i].reg_size - 1;
3122                 res->flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM;
3123
3124                 if (insert_resource(&iomem_resource, res) < 0) {
3125                         pr_warn("Resource insertion failed.\n");
3126                         break;
3127                 }
3128
3129                 insert_resource(res, &code_resource);
3130                 insert_resource(res, &data_resource);
3131                 insert_resource(res, &bss_resource);
3132         }
3133
3134         return 0;
3135 }
3136 arch_initcall(report_memory);
3137
3138 #ifdef CONFIG_SMP
3139 #define do_flush_tlb_kernel_range       smp_flush_tlb_kernel_range
3140 #else
3141 #define do_flush_tlb_kernel_range       __flush_tlb_kernel_range
3142 #endif
3143
3144 void flush_tlb_kernel_range(unsigned long start, unsigned long end)
3145 {
3146         if (start < HI_OBP_ADDRESS && end > LOW_OBP_ADDRESS) {
3147                 if (start < LOW_OBP_ADDRESS) {
3148                         flush_tsb_kernel_range(start, LOW_OBP_ADDRESS);
3149                         do_flush_tlb_kernel_range(start, LOW_OBP_ADDRESS);
3150                 }
3151                 if (end > HI_OBP_ADDRESS) {
3152                         flush_tsb_kernel_range(HI_OBP_ADDRESS, end);
3153                         do_flush_tlb_kernel_range(HI_OBP_ADDRESS, end);
3154                 }
3155         } else {
3156                 flush_tsb_kernel_range(start, end);
3157                 do_flush_tlb_kernel_range(start, end);
3158         }
3159 }
3160
3161 void copy_user_highpage(struct page *to, struct page *from,
3162         unsigned long vaddr, struct vm_area_struct *vma)
3163 {
3164         char *vfrom, *vto;
3165
3166         vfrom = kmap_atomic(from);
3167         vto = kmap_atomic(to);
3168         copy_user_page(vto, vfrom, vaddr, to);
3169         kunmap_atomic(vto);
3170         kunmap_atomic(vfrom);
3171
3172         /* If this page has ADI enabled, copy over any ADI tags
3173          * as well
3174          */
3175         if (vma->vm_flags & VM_SPARC_ADI) {
3176                 unsigned long pfrom, pto, i, adi_tag;
3177
3178                 pfrom = page_to_phys(from);
3179                 pto = page_to_phys(to);
3180
3181                 for (i = pfrom; i < (pfrom + PAGE_SIZE); i += adi_blksize()) {
3182                         asm volatile("ldxa [%1] %2, %0\n\t"
3183                                         : "=r" (adi_tag)
3184                                         :  "r" (i), "i" (ASI_MCD_REAL));
3185                         asm volatile("stxa %0, [%1] %2\n\t"
3186                                         :
3187                                         : "r" (adi_tag), "r" (pto),
3188                                           "i" (ASI_MCD_REAL));
3189                         pto += adi_blksize();
3190                 }
3191                 asm volatile("membar #Sync\n\t");
3192         }
3193 }
3194 EXPORT_SYMBOL(copy_user_highpage);
3195
3196 void copy_highpage(struct page *to, struct page *from)
3197 {
3198         char *vfrom, *vto;
3199
3200         vfrom = kmap_atomic(from);
3201         vto = kmap_atomic(to);
3202         copy_page(vto, vfrom);
3203         kunmap_atomic(vto);
3204         kunmap_atomic(vfrom);
3205
3206         /* If this platform is ADI enabled, copy any ADI tags
3207          * as well
3208          */
3209         if (adi_capable()) {
3210                 unsigned long pfrom, pto, i, adi_tag;
3211
3212                 pfrom = page_to_phys(from);
3213                 pto = page_to_phys(to);
3214
3215                 for (i = pfrom; i < (pfrom + PAGE_SIZE); i += adi_blksize()) {
3216                         asm volatile("ldxa [%1] %2, %0\n\t"
3217                                         : "=r" (adi_tag)
3218                                         :  "r" (i), "i" (ASI_MCD_REAL));
3219                         asm volatile("stxa %0, [%1] %2\n\t"
3220                                         :
3221                                         : "r" (adi_tag), "r" (pto),
3222                                           "i" (ASI_MCD_REAL));
3223                         pto += adi_blksize();
3224                 }
3225                 asm volatile("membar #Sync\n\t");
3226         }
3227 }
3228 EXPORT_SYMBOL(copy_highpage);