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