blob: 3328043e990c2b61e1994e439383f22853d2431d [file] [log] [blame]
Adrian Bunkb00dc832008-05-19 16:52:27 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * arch/sparc64/mm/init.c
3 *
4 * Copyright (C) 1996-1999 David S. Miller (davem@caip.rutgers.edu)
5 * Copyright (C) 1997-1999 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
6 */
7
Paul Gortmakercdd4f4c2016-09-19 17:36:29 -04008#include <linux/extable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/kernel.h>
10#include <linux/sched.h>
11#include <linux/string.h>
12#include <linux/init.h>
13#include <linux/bootmem.h>
14#include <linux/mm.h>
15#include <linux/hugetlb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/initrd.h>
17#include <linux/swap.h>
18#include <linux/pagemap.h>
Randy Dunlapc9cf5522006-06-27 02:53:52 -070019#include <linux/poison.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/fs.h>
21#include <linux/seq_file.h>
Prasanna S Panchamukhi05e14cb2005-09-06 15:19:30 -070022#include <linux/kprobes.h>
David S. Miller1ac4f5e2005-09-21 21:49:32 -070023#include <linux/cache.h>
David S. Miller13edad72005-09-29 17:58:26 -070024#include <linux/sort.h>
bob piccof6d4fb52014-03-03 11:54:42 -050025#include <linux/ioport.h>
David S. Miller5cbc3072007-05-25 15:49:59 -070026#include <linux/percpu.h>
Yinghai Lu95f72d12010-07-12 14:36:09 +100027#include <linux/memblock.h>
David S. Miller919ee672008-04-23 05:40:25 -070028#include <linux/mmzone.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31#include <asm/head.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <asm/page.h>
33#include <asm/pgalloc.h>
34#include <asm/pgtable.h>
35#include <asm/oplib.h>
36#include <asm/iommu.h>
37#include <asm/io.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080038#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#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>
David S. Miller517af332006-02-01 15:55:21 -080046#include <asm/tsb.h>
David S. Miller481295f2006-02-07 21:51:08 -080047#include <asm/hypervisor.h>
David S. Miller372b07b2006-06-21 15:35:28 -070048#include <asm/prom.h>
David S. Miller5cbc3072007-05-25 15:49:59 -070049#include <asm/mdesc.h>
David S. Miller3d5ae6b2008-03-25 21:51:40 -070050#include <asm/cpudata.h>
Sam Ravnborg59dec132014-05-16 23:26:07 +020051#include <asm/setup.h>
David S. Miller4f70f7a2008-08-12 18:33:56 -070052#include <asm/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Sam Ravnborg27137e52008-11-16 20:08:45 -080054#include "init_64.h"
David S. Miller9cc3a1a2006-02-21 20:51:13 -080055
David S. Miller4f93d212012-09-06 18:13:58 -070056unsigned long kern_linear_pte_xor[4] __read_mostly;
Khalid Aziz494e5b62015-05-27 10:00:46 -060057static unsigned long page_cache4v_flag;
David S. Miller9cc3a1a2006-02-21 20:51:13 -080058
David S. Miller4f93d212012-09-06 18:13:58 -070059/* 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.
David S. Miller9cc3a1a2006-02-21 20:51:13 -080078 */
David S. Miller9cc3a1a2006-02-21 20:51:13 -080079
David S. Millerd1acb422007-03-16 17:20:28 -070080#ifndef CONFIG_DEBUG_PAGEALLOC
David S. Miller4f93d212012-09-06 18:13:58 -070081/* 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
David S. Miller2d9e2762007-05-29 01:58:31 -070084 */
85extern struct tsb swapper_4m_tsb[KERNEL_TSB4M_NENTRIES];
David S. Millerd1acb422007-03-16 17:20:28 -070086#endif
David S. Miller0dd5b7b2014-09-24 20:56:11 -070087extern struct tsb swapper_tsb[KERNEL_TSB_NENTRIES];
David S. Millerd7744a02006-02-21 22:31:11 -080088
David S. Millerce33fdc2012-09-06 19:01:25 -070089static unsigned long cpu_pgsz_mask;
90
David S. Millerd195b712014-09-27 21:30:57 -070091#define MAX_BANKS 1024
David S. Miller10147572005-09-28 21:46:43 -070092
Greg Kroah-Hartman7c9503b2012-12-21 14:03:26 -080093static struct linux_prom64_registers pavail[MAX_BANKS];
94static int pavail_ents;
David S. Miller10147572005-09-28 21:46:43 -070095
Nitin Gupta52708d62015-11-02 16:30:24 -050096u64 numa_latency[MAX_NUMNODES][MAX_NUMNODES];
97
David S. Miller13edad72005-09-29 17:58:26 -070098static 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
109static void __init read_obp_memory(const char *property,
110 struct linux_prom64_registers *regs,
111 int *num_ents)
112{
Andres Salomon8d125562010-10-08 14:18:11 -0700113 phandle node = prom_finddevice("/memory");
David S. Miller13edad72005-09-29 17:58:26 -0700114 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) {
Akinobu Mita5da444a2012-09-29 03:14:49 +0000127 prom_printf("Couldn't get %s property from /memory.\n",
128 property);
David S. Miller13edad72005-09-29 17:58:26 -0700129 prom_halt();
130 }
131
David S. Miller13edad72005-09-29 17:58:26 -0700132 /* 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 }
David S. Miller0015d3d2007-03-15 00:06:34 -0700150 if (size == 0UL) {
151 /* If it is empty, simply get rid of it.
152 * This simplifies the logic of the other
153 * functions that process these arrays.
154 */
155 memmove(&regs[i], &regs[i + 1],
156 (ents - i - 1) * sizeof(regs[0]));
157 i--;
158 ents--;
159 continue;
160 }
David S. Miller13edad72005-09-29 17:58:26 -0700161 regs[i].phys_addr = base;
162 regs[i].reg_size = size;
163 }
David S. Miller486ad102006-06-22 00:00:00 -0700164
David S. Miller486ad102006-06-22 00:00:00 -0700165 *num_ents = ents;
166
David S. Millerc9c10832005-10-12 12:22:46 -0700167 sort(regs, ents, sizeof(struct linux_prom64_registers),
David S. Miller13edad72005-09-29 17:58:26 -0700168 cmp_p64, NULL);
169}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
David S. Millerd1112012006-03-08 02:16:07 -0800171/* Kernel physical address base and size in bytes. */
David S. Miller1ac4f5e2005-09-21 21:49:32 -0700172unsigned long kern_base __read_mostly;
173unsigned long kern_size __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175/* Initial ramdisk setup */
176extern unsigned long sparc_ramdisk_image64;
177extern unsigned int sparc_ramdisk_image;
178extern unsigned int sparc_ramdisk_size;
179
David S. Miller1ac4f5e2005-09-21 21:49:32 -0700180struct page *mem_map_zero __read_mostly;
Aneesh Kumar K.V35802c02008-04-29 08:11:12 -0400181EXPORT_SYMBOL(mem_map_zero);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
David S. Miller0835ae02005-10-04 15:23:20 -0700183unsigned int sparc64_highest_unlocked_tlb_ent __read_mostly;
184
185unsigned long sparc64_kern_pri_context __read_mostly;
186unsigned long sparc64_kern_pri_nuc_bits __read_mostly;
187unsigned long sparc64_kern_sec_context __read_mostly;
188
David S. Miller64658742008-03-21 17:01:38 -0700189int num_kernel_image_mappings;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191#ifdef CONFIG_DEBUG_DCFLUSH
192atomic_t dcpage_flushes = ATOMIC_INIT(0);
193#ifdef CONFIG_SMP
194atomic_t dcpage_flushes_xcall = ATOMIC_INIT(0);
195#endif
196#endif
197
David S. Miller7a591cf2006-02-26 19:44:50 -0800198inline void flush_dcache_page_impl(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
David S. Miller7a591cf2006-02-26 19:44:50 -0800200 BUG_ON(tlb_type == hypervisor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201#ifdef CONFIG_DEBUG_DCFLUSH
202 atomic_inc(&dcpage_flushes);
203#endif
204
205#ifdef DCACHE_ALIASING_POSSIBLE
206 __flush_dcache_page(page_address(page),
207 ((tlb_type == spitfire) &&
208 page_mapping(page) != NULL));
209#else
210 if (page_mapping(page) != NULL &&
211 tlb_type == spitfire)
212 __flush_icache_page(__pa(page_address(page)));
213#endif
214}
215
216#define PG_dcache_dirty PG_arch_1
David S. Miller22adb352007-05-26 01:14:43 -0700217#define PG_dcache_cpu_shift 32UL
218#define PG_dcache_cpu_mask \
219 ((1UL<<ilog2(roundup_pow_of_two(NR_CPUS)))-1UL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221#define dcache_dirty_cpu(page) \
David S. Miller48b0e542005-07-27 16:08:44 -0700222 (((page)->flags >> PG_dcache_cpu_shift) & PG_dcache_cpu_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
David S. Millerd979f172007-10-27 00:13:04 -0700224static inline void set_dcache_dirty(struct page *page, int this_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
226 unsigned long mask = this_cpu;
David S. Miller48b0e542005-07-27 16:08:44 -0700227 unsigned long non_cpu_bits;
228
229 non_cpu_bits = ~(PG_dcache_cpu_mask << PG_dcache_cpu_shift);
230 mask = (mask << PG_dcache_cpu_shift) | (1UL << PG_dcache_dirty);
231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 __asm__ __volatile__("1:\n\t"
233 "ldx [%2], %%g7\n\t"
234 "and %%g7, %1, %%g1\n\t"
235 "or %%g1, %0, %%g1\n\t"
236 "casx [%2], %%g7, %%g1\n\t"
237 "cmp %%g7, %%g1\n\t"
238 "bne,pn %%xcc, 1b\n\t"
David S. Millerb445e262005-06-27 15:42:04 -0700239 " nop"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 : /* no outputs */
241 : "r" (mask), "r" (non_cpu_bits), "r" (&page->flags)
242 : "g1", "g7");
243}
244
David S. Millerd979f172007-10-27 00:13:04 -0700245static inline void clear_dcache_dirty_cpu(struct page *page, unsigned long cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
247 unsigned long mask = (1UL << PG_dcache_dirty);
248
249 __asm__ __volatile__("! test_and_clear_dcache_dirty\n"
250 "1:\n\t"
251 "ldx [%2], %%g7\n\t"
David S. Miller48b0e542005-07-27 16:08:44 -0700252 "srlx %%g7, %4, %%g1\n\t"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 "and %%g1, %3, %%g1\n\t"
254 "cmp %%g1, %0\n\t"
255 "bne,pn %%icc, 2f\n\t"
256 " andn %%g7, %1, %%g1\n\t"
257 "casx [%2], %%g7, %%g1\n\t"
258 "cmp %%g7, %%g1\n\t"
259 "bne,pn %%xcc, 1b\n\t"
David S. Millerb445e262005-06-27 15:42:04 -0700260 " nop\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 "2:"
262 : /* no outputs */
263 : "r" (cpu), "r" (mask), "r" (&page->flags),
David S. Miller48b0e542005-07-27 16:08:44 -0700264 "i" (PG_dcache_cpu_mask),
265 "i" (PG_dcache_cpu_shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 : "g1", "g7");
267}
268
David S. Miller517af332006-02-01 15:55:21 -0800269static inline void tsb_insert(struct tsb *ent, unsigned long tag, unsigned long pte)
270{
271 unsigned long tsb_addr = (unsigned long) ent;
272
David S. Miller3b3ab2e2006-02-17 09:54:42 -0800273 if (tlb_type == cheetah_plus || tlb_type == hypervisor)
David S. Miller517af332006-02-01 15:55:21 -0800274 tsb_addr = __pa(tsb_addr);
275
276 __tsb_insert(tsb_addr, tag, pte);
277}
278
David S. Millerc4bce902006-02-11 21:57:54 -0800279unsigned long _PAGE_ALL_SZ_BITS __read_mostly;
David S. Millerc4bce902006-02-11 21:57:54 -0800280
Sam Ravnborgff9aefb2009-01-06 12:51:26 -0800281static void flush_dcache(unsigned long pfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
Sam Ravnborgff9aefb2009-01-06 12:51:26 -0800283 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Sam Ravnborgff9aefb2009-01-06 12:51:26 -0800285 page = pfn_to_page(pfn);
David S. Miller1a78ced2009-10-12 03:20:57 -0700286 if (page) {
David S. Miller7a591cf2006-02-26 19:44:50 -0800287 unsigned long pg_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Sam Ravnborgff9aefb2009-01-06 12:51:26 -0800289 pg_flags = page->flags;
290 if (pg_flags & (1UL << PG_dcache_dirty)) {
David S. Miller7a591cf2006-02-26 19:44:50 -0800291 int cpu = ((pg_flags >> PG_dcache_cpu_shift) &
292 PG_dcache_cpu_mask);
293 int this_cpu = get_cpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
David S. Miller7a591cf2006-02-26 19:44:50 -0800295 /* This is just to optimize away some function calls
296 * in the SMP case.
297 */
298 if (cpu == this_cpu)
299 flush_dcache_page_impl(page);
300 else
301 smp_flush_dcache_page_impl(page, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
David S. Miller7a591cf2006-02-26 19:44:50 -0800303 clear_dcache_dirty_cpu(page, cpu);
304
305 put_cpu();
306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 }
Sam Ravnborgff9aefb2009-01-06 12:51:26 -0800308}
309
David Miller9e695d22012-10-08 16:34:29 -0700310/* mm->context.lock must be held */
311static void __update_mmu_tsb_insert(struct mm_struct *mm, unsigned long tsb_index,
312 unsigned long tsb_hash_shift, unsigned long address,
313 unsigned long tte)
314{
315 struct tsb *tsb = mm->context.tsb_block[tsb_index].tsb;
316 unsigned long tag;
317
David S. Millerbcd896b2013-02-19 13:20:08 -0800318 if (unlikely(!tsb))
319 return;
320
David Miller9e695d22012-10-08 16:34:29 -0700321 tsb += ((address >> tsb_hash_shift) &
322 (mm->context.tsb_block[tsb_index].tsb_nentries - 1UL));
323 tag = (address >> 22UL);
324 tsb_insert(tsb, tag, tte);
325}
326
Nitin Guptac7d9f772017-02-01 16:16:36 -0800327#ifdef CONFIG_HUGETLB_PAGE
328static int __init setup_hugepagesz(char *string)
329{
330 unsigned long long hugepage_size;
331 unsigned int hugepage_shift;
332 unsigned short hv_pgsz_idx;
333 unsigned int hv_pgsz_mask;
334 int rc = 0;
335
336 hugepage_size = memparse(string, &string);
337 hugepage_shift = ilog2(hugepage_size);
338
339 switch (hugepage_shift) {
Nitin Gupta85b1da72017-03-09 14:22:23 -0800340 case HPAGE_2GB_SHIFT:
341 hv_pgsz_mask = HV_PGSZ_MASK_2GB;
342 hv_pgsz_idx = HV_PGSZ_IDX_2GB;
343 break;
Nitin Guptac7d9f772017-02-01 16:16:36 -0800344 case HPAGE_256MB_SHIFT:
345 hv_pgsz_mask = HV_PGSZ_MASK_256MB;
346 hv_pgsz_idx = HV_PGSZ_IDX_256MB;
347 break;
348 case HPAGE_SHIFT:
349 hv_pgsz_mask = HV_PGSZ_MASK_4MB;
350 hv_pgsz_idx = HV_PGSZ_IDX_4MB;
351 break;
Nitin Guptadcd19122017-02-06 12:33:26 -0800352 case HPAGE_64K_SHIFT:
353 hv_pgsz_mask = HV_PGSZ_MASK_64K;
354 hv_pgsz_idx = HV_PGSZ_IDX_64K;
355 break;
Nitin Guptac7d9f772017-02-01 16:16:36 -0800356 default:
357 hv_pgsz_mask = 0;
358 }
359
360 if ((hv_pgsz_mask & cpu_pgsz_mask) == 0U) {
361 pr_warn("hugepagesz=%llu not supported by MMU.\n",
362 hugepage_size);
363 goto out;
364 }
365
366 hugetlb_add_hstate(hugepage_shift - PAGE_SHIFT);
367 rc = 1;
368
369out:
370 return rc;
371}
372__setup("hugepagesz=", setup_hugepagesz);
373#endif /* CONFIG_HUGETLB_PAGE */
374
Russell King4b3073e2009-12-18 16:40:18 +0000375void update_mmu_cache(struct vm_area_struct *vma, unsigned long address, pte_t *ptep)
Sam Ravnborgff9aefb2009-01-06 12:51:26 -0800376{
377 struct mm_struct *mm;
David S. Millerbcd896b2013-02-19 13:20:08 -0800378 unsigned long flags;
Russell King4b3073e2009-12-18 16:40:18 +0000379 pte_t pte = *ptep;
Sam Ravnborgff9aefb2009-01-06 12:51:26 -0800380
381 if (tlb_type != hypervisor) {
382 unsigned long pfn = pte_pfn(pte);
383
384 if (pfn_valid(pfn))
385 flush_dcache(pfn);
386 }
David S. Millerbd407912006-01-31 18:31:38 -0800387
388 mm = vma->vm_mm;
David S. Miller7a1ac522006-03-16 02:02:32 -0800389
David S. Miller18f38132014-08-04 16:34:01 -0700390 /* Don't insert a non-valid PTE into the TSB, we'll deadlock. */
391 if (!pte_accessible(mm, pte))
392 return;
393
David S. Miller7a1ac522006-03-16 02:02:32 -0800394 spin_lock_irqsave(&mm->context.lock, flags);
395
David Miller9e695d22012-10-08 16:34:29 -0700396#if defined(CONFIG_HUGETLB_PAGE) || defined(CONFIG_TRANSPARENT_HUGEPAGE)
Mike Kravetzaf1b1a92016-07-15 13:08:42 -0700397 if ((mm->context.hugetlb_pte_count || mm->context.thp_pte_count) &&
Nitin Guptac7d9f772017-02-01 16:16:36 -0800398 is_hugetlb_pmd(__pmd(pte_val(pte)))) {
Nitin Gupta7bc37772016-07-29 00:54:21 -0700399 /* We are fabricating 8MB pages using 4MB real hw pages. */
400 pte_val(pte) |= (address & (1UL << REAL_HPAGE_SHIFT));
David S. Miller37b3a8f2013-09-25 13:48:49 -0700401 __update_mmu_tsb_insert(mm, MM_TSB_HUGE, REAL_HPAGE_SHIFT,
David S. Millerbcd896b2013-02-19 13:20:08 -0800402 address, pte_val(pte));
Nitin Gupta7bc37772016-07-29 00:54:21 -0700403 } else
David S. Millerdcc1e8d2006-03-22 00:49:59 -0800404#endif
David S. Millerbcd896b2013-02-19 13:20:08 -0800405 __update_mmu_tsb_insert(mm, MM_TSB_BASE, PAGE_SHIFT,
406 address, pte_val(pte));
David S. Miller7a1ac522006-03-16 02:02:32 -0800407
408 spin_unlock_irqrestore(&mm->context.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409}
410
411void flush_dcache_page(struct page *page)
412{
David S. Millera9546f52005-04-17 18:03:09 -0700413 struct address_space *mapping;
414 int this_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
David S. Miller7a591cf2006-02-26 19:44:50 -0800416 if (tlb_type == hypervisor)
417 return;
418
David S. Millera9546f52005-04-17 18:03:09 -0700419 /* Do not bother with the expensive D-cache flush if it
420 * is merely the zero page. The 'bigcore' testcase in GDB
421 * causes this case to run millions of times.
422 */
423 if (page == ZERO_PAGE(0))
424 return;
425
426 this_cpu = get_cpu();
427
428 mapping = page_mapping(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 if (mapping && !mapping_mapped(mapping)) {
David S. Millera9546f52005-04-17 18:03:09 -0700430 int dirty = test_bit(PG_dcache_dirty, &page->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 if (dirty) {
David S. Millera9546f52005-04-17 18:03:09 -0700432 int dirty_cpu = dcache_dirty_cpu(page);
433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 if (dirty_cpu == this_cpu)
435 goto out;
436 smp_flush_dcache_page_impl(page, dirty_cpu);
437 }
438 set_dcache_dirty(page, this_cpu);
439 } else {
440 /* We could delay the flush for the !page_mapping
441 * case too. But that case is for exec env/arg
442 * pages and those are %99 certainly going to get
443 * faulted into the tlb (and thus flushed) anyways.
444 */
445 flush_dcache_page_impl(page);
446 }
447
448out:
449 put_cpu();
450}
Sam Ravnborg917c3662009-01-08 16:58:20 -0800451EXPORT_SYMBOL(flush_dcache_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Prasanna S Panchamukhi05e14cb2005-09-06 15:19:30 -0700453void __kprobes flush_icache_range(unsigned long start, unsigned long end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
David S. Millera43fe0e2006-02-04 03:10:53 -0800455 /* Cheetah and Hypervisor platform cpus have coherent I-cache. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 if (tlb_type == spitfire) {
457 unsigned long kaddr;
458
David S. Millera94aa252007-03-15 15:50:11 -0700459 /* This code only runs on Spitfire cpus so this is
460 * why we can assume _PAGE_PADDR_4U.
461 */
462 for (kaddr = start; kaddr < end; kaddr += PAGE_SIZE) {
463 unsigned long paddr, mask = _PAGE_PADDR_4U;
464
465 if (kaddr >= PAGE_OFFSET)
466 paddr = kaddr & mask;
467 else {
468 pgd_t *pgdp = pgd_offset_k(kaddr);
469 pud_t *pudp = pud_offset(pgdp, kaddr);
470 pmd_t *pmdp = pmd_offset(pudp, kaddr);
471 pte_t *ptep = pte_offset_kernel(pmdp, kaddr);
472
473 paddr = pte_val(*ptep) & mask;
474 }
475 __flush_icache_page(paddr);
476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 }
478}
Sam Ravnborg917c3662009-01-08 16:58:20 -0800479EXPORT_SYMBOL(flush_icache_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481void mmu_info(struct seq_file *m)
482{
David S. Millerce33fdc2012-09-06 19:01:25 -0700483 static const char *pgsz_strings[] = {
484 "8K", "64K", "512K", "4MB", "32MB",
485 "256MB", "2GB", "16GB",
486 };
487 int i, printed;
488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 if (tlb_type == cheetah)
490 seq_printf(m, "MMU Type\t: Cheetah\n");
491 else if (tlb_type == cheetah_plus)
492 seq_printf(m, "MMU Type\t: Cheetah+\n");
493 else if (tlb_type == spitfire)
494 seq_printf(m, "MMU Type\t: Spitfire\n");
David S. Millera43fe0e2006-02-04 03:10:53 -0800495 else if (tlb_type == hypervisor)
496 seq_printf(m, "MMU Type\t: Hypervisor (sun4v)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 else
498 seq_printf(m, "MMU Type\t: ???\n");
499
David S. Millerce33fdc2012-09-06 19:01:25 -0700500 seq_printf(m, "MMU PGSZs\t: ");
501 printed = 0;
502 for (i = 0; i < ARRAY_SIZE(pgsz_strings); i++) {
503 if (cpu_pgsz_mask & (1UL << i)) {
504 seq_printf(m, "%s%s",
505 printed ? "," : "", pgsz_strings[i]);
506 printed++;
507 }
508 }
509 seq_putc(m, '\n');
510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511#ifdef CONFIG_DEBUG_DCFLUSH
512 seq_printf(m, "DCPageFlushes\t: %d\n",
513 atomic_read(&dcpage_flushes));
514#ifdef CONFIG_SMP
515 seq_printf(m, "DCPageFlushesXC\t: %d\n",
516 atomic_read(&dcpage_flushes_xcall));
517#endif /* CONFIG_SMP */
518#endif /* CONFIG_DEBUG_DCFLUSH */
519}
520
David S. Millera94aa252007-03-15 15:50:11 -0700521struct linux_prom_translation prom_trans[512] __read_mostly;
522unsigned int prom_trans_ents __read_mostly;
523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524unsigned long kern_locked_tte_data;
525
David S. Miller405599b2005-09-22 00:12:35 -0700526/* The obp translations are saved based on 8k pagesize, since obp can
527 * use a mixture of pagesizes. Misses to the LOW_OBP_ADDRESS ->
David S. Miller74bf4312006-01-31 18:29:18 -0800528 * HI_OBP_ADDRESS range are handled in ktlb.S.
David S. Miller405599b2005-09-22 00:12:35 -0700529 */
David S. Miller5085b4a2005-09-22 00:45:41 -0700530static inline int in_obp_range(unsigned long vaddr)
531{
532 return (vaddr >= LOW_OBP_ADDRESS &&
533 vaddr < HI_OBP_ADDRESS);
534}
535
David S. Millerc9c10832005-10-12 12:22:46 -0700536static int cmp_ptrans(const void *a, const void *b)
David S. Miller405599b2005-09-22 00:12:35 -0700537{
David S. Millerc9c10832005-10-12 12:22:46 -0700538 const struct linux_prom_translation *x = a, *y = b;
David S. Miller405599b2005-09-22 00:12:35 -0700539
David S. Millerc9c10832005-10-12 12:22:46 -0700540 if (x->virt > y->virt)
541 return 1;
542 if (x->virt < y->virt)
543 return -1;
544 return 0;
David S. Miller405599b2005-09-22 00:12:35 -0700545}
546
David S. Millerc9c10832005-10-12 12:22:46 -0700547/* Read OBP translations property into 'prom_trans[]'. */
David S. Miller9ad98c52005-10-05 15:12:00 -0700548static void __init read_obp_translations(void)
David S. Miller405599b2005-09-22 00:12:35 -0700549{
David S. Millerc9c10832005-10-12 12:22:46 -0700550 int n, node, ents, first, last, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
552 node = prom_finddevice("/virtual-memory");
553 n = prom_getproplen(node, "translations");
David S. Miller405599b2005-09-22 00:12:35 -0700554 if (unlikely(n == 0 || n == -1)) {
David S. Millerb206fc42005-09-21 22:31:13 -0700555 prom_printf("prom_mappings: Couldn't get size.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 prom_halt();
557 }
David S. Miller405599b2005-09-22 00:12:35 -0700558 if (unlikely(n > sizeof(prom_trans))) {
Akinobu Mita5da444a2012-09-29 03:14:49 +0000559 prom_printf("prom_mappings: Size %d is too big.\n", n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 prom_halt();
561 }
David S. Miller405599b2005-09-22 00:12:35 -0700562
David S. Millerb206fc42005-09-21 22:31:13 -0700563 if ((n = prom_getproperty(node, "translations",
David S. Miller405599b2005-09-22 00:12:35 -0700564 (char *)&prom_trans[0],
565 sizeof(prom_trans))) == -1) {
David S. Millerb206fc42005-09-21 22:31:13 -0700566 prom_printf("prom_mappings: Couldn't get property.\n");
567 prom_halt();
568 }
David S. Miller9ad98c52005-10-05 15:12:00 -0700569
David S. Millerb206fc42005-09-21 22:31:13 -0700570 n = n / sizeof(struct linux_prom_translation);
David S. Miller9ad98c52005-10-05 15:12:00 -0700571
David S. Millerc9c10832005-10-12 12:22:46 -0700572 ents = n;
573
574 sort(prom_trans, ents, sizeof(struct linux_prom_translation),
575 cmp_ptrans, NULL);
576
577 /* Now kick out all the non-OBP entries. */
578 for (i = 0; i < ents; i++) {
579 if (in_obp_range(prom_trans[i].virt))
580 break;
581 }
582 first = i;
583 for (; i < ents; i++) {
584 if (!in_obp_range(prom_trans[i].virt))
585 break;
586 }
587 last = i;
588
589 for (i = 0; i < (last - first); i++) {
590 struct linux_prom_translation *src = &prom_trans[i + first];
591 struct linux_prom_translation *dest = &prom_trans[i];
592
593 *dest = *src;
594 }
595 for (; i < ents; i++) {
596 struct linux_prom_translation *dest = &prom_trans[i];
597 dest->virt = dest->size = dest->data = 0x0UL;
598 }
599
600 prom_trans_ents = last - first;
601
602 if (tlb_type == spitfire) {
603 /* Clear diag TTE bits. */
604 for (i = 0; i < prom_trans_ents; i++)
605 prom_trans[i].data &= ~0x0003fe0000000000UL;
606 }
David S. Millerf4142cb2011-09-29 12:18:59 -0700607
608 /* Force execute bit on. */
609 for (i = 0; i < prom_trans_ents; i++)
610 prom_trans[i].data |= (tlb_type == hypervisor ?
611 _PAGE_EXEC_4V : _PAGE_EXEC_4U);
David S. Miller405599b2005-09-22 00:12:35 -0700612}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
David S. Millerd82ace72006-02-09 02:52:44 -0800614static void __init hypervisor_tlb_lock(unsigned long vaddr,
615 unsigned long pte,
616 unsigned long mmu)
617{
David S. Miller7db35f32007-05-29 02:22:14 -0700618 unsigned long ret = sun4v_mmu_map_perm_addr(vaddr, 0, pte, mmu);
David S. Millerd82ace72006-02-09 02:52:44 -0800619
David S. Miller7db35f32007-05-29 02:22:14 -0700620 if (ret != 0) {
Akinobu Mita5da444a2012-09-29 03:14:49 +0000621 prom_printf("hypervisor_tlb_lock[%lx:%x:%lx:%lx]: "
David S. Miller7db35f32007-05-29 02:22:14 -0700622 "errors with %lx\n", vaddr, 0, pte, mmu, ret);
David S. Miller12e126a2006-02-17 14:40:30 -0800623 prom_halt();
624 }
David S. Millerd82ace72006-02-09 02:52:44 -0800625}
626
David S. Millerc4bce902006-02-11 21:57:54 -0800627static unsigned long kern_large_tte(unsigned long paddr);
628
David S. Miller898cf0e2005-09-23 11:59:44 -0700629static void __init remap_kernel(void)
David S. Miller405599b2005-09-22 00:12:35 -0700630{
631 unsigned long phys_page, tte_vaddr, tte_data;
David S. Miller64658742008-03-21 17:01:38 -0700632 int i, tlb_ent = sparc64_highest_locked_tlbent();
David S. Miller405599b2005-09-22 00:12:35 -0700633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 tte_vaddr = (unsigned long) KERNBASE;
David S. Miller0eef3312014-05-03 22:52:50 -0700635 phys_page = (prom_boot_mapping_phys_low >> ILOG2_4MB) << ILOG2_4MB;
David S. Millerc4bce902006-02-11 21:57:54 -0800636 tte_data = kern_large_tte(phys_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638 kern_locked_tte_data = tte_data;
639
David S. Millerd82ace72006-02-09 02:52:44 -0800640 /* Now lock us into the TLBs via Hypervisor or OBP. */
641 if (tlb_type == hypervisor) {
David S. Miller64658742008-03-21 17:01:38 -0700642 for (i = 0; i < num_kernel_image_mappings; i++) {
David S. Millerd82ace72006-02-09 02:52:44 -0800643 hypervisor_tlb_lock(tte_vaddr, tte_data, HV_MMU_DMMU);
644 hypervisor_tlb_lock(tte_vaddr, tte_data, HV_MMU_IMMU);
David S. Miller64658742008-03-21 17:01:38 -0700645 tte_vaddr += 0x400000;
646 tte_data += 0x400000;
David S. Millerd82ace72006-02-09 02:52:44 -0800647 }
648 } else {
David S. Miller64658742008-03-21 17:01:38 -0700649 for (i = 0; i < num_kernel_image_mappings; i++) {
650 prom_dtlb_load(tlb_ent - i, tte_data, tte_vaddr);
651 prom_itlb_load(tlb_ent - i, tte_data, tte_vaddr);
652 tte_vaddr += 0x400000;
653 tte_data += 0x400000;
David S. Millerd82ace72006-02-09 02:52:44 -0800654 }
David S. Miller64658742008-03-21 17:01:38 -0700655 sparc64_highest_unlocked_tlb_ent = tlb_ent - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 }
David S. Miller0835ae02005-10-04 15:23:20 -0700657 if (tlb_type == cheetah_plus) {
658 sparc64_kern_pri_context = (CTX_CHEETAH_PLUS_CTX0 |
659 CTX_CHEETAH_PLUS_NUC);
660 sparc64_kern_pri_nuc_bits = CTX_CHEETAH_PLUS_NUC;
661 sparc64_kern_sec_context = CTX_CHEETAH_PLUS_CTX0;
662 }
David S. Miller405599b2005-09-22 00:12:35 -0700663}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
David S. Miller405599b2005-09-22 00:12:35 -0700665
David S. Millerc9c10832005-10-12 12:22:46 -0700666static void __init inherit_prom_mappings(void)
David S. Miller9ad98c52005-10-05 15:12:00 -0700667{
David S. Miller405599b2005-09-22 00:12:35 -0700668 /* Now fixup OBP's idea about where we really are mapped. */
David S. Miller3c62a2d2008-02-17 23:22:50 -0800669 printk("Remapping the kernel... ");
David S. Miller405599b2005-09-22 00:12:35 -0700670 remap_kernel();
David S. Miller3c62a2d2008-02-17 23:22:50 -0800671 printk("done.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672}
673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674void prom_world(int enter)
675{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 if (!enter)
Al Virodff933d2012-09-26 01:21:14 -0400677 set_fs(get_fs());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
David S. Miller3487d1d2006-01-31 18:33:25 -0800679 __asm__ __volatile__("flushw");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680}
681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682void __flush_dcache_range(unsigned long start, unsigned long end)
683{
684 unsigned long va;
685
686 if (tlb_type == spitfire) {
687 int n = 0;
688
689 for (va = start; va < end; va += 32) {
690 spitfire_put_dcache_tag(va & 0x3fe0, 0x0);
691 if (++n >= 512)
692 break;
693 }
David S. Millera43fe0e2006-02-04 03:10:53 -0800694 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 start = __pa(start);
696 end = __pa(end);
697 for (va = start; va < end; va += 32)
698 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
699 "membar #Sync"
700 : /* no outputs */
701 : "r" (va),
702 "i" (ASI_DCACHE_INVALIDATE));
703 }
704}
Sam Ravnborg917c3662009-01-08 16:58:20 -0800705EXPORT_SYMBOL(__flush_dcache_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
David S. Miller85f1e1f2007-03-15 17:51:26 -0700707/* get_new_mmu_context() uses "cache + 1". */
708DEFINE_SPINLOCK(ctx_alloc_lock);
709unsigned long tlb_context_cache = CTX_FIRST_VERSION - 1;
710#define MAX_CTX_NR (1UL << CTX_NR_BITS)
711#define CTX_BMAP_SLOTS BITS_TO_LONGS(MAX_CTX_NR)
712DECLARE_BITMAP(mmu_context_bmap, MAX_CTX_NR);
713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714/* Caller does TLB context flushing on local CPU if necessary.
715 * The caller also ensures that CTX_VALID(mm->context) is false.
716 *
717 * We must be careful about boundary cases so that we never
718 * let the user have CTX 0 (nucleus) or we ever use a CTX
719 * version of zero (and thus NO_CONTEXT would not be caught
720 * by version mis-match tests in mmu_context.h).
David S. Millera0663a72006-02-23 14:19:28 -0800721 *
722 * Always invoked with interrupts disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 */
724void get_new_mmu_context(struct mm_struct *mm)
725{
726 unsigned long ctx, new_ctx;
727 unsigned long orig_pgsz_bits;
David S. Millera0663a72006-02-23 14:19:28 -0800728 int new_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Kirill Tkhai07df8412013-04-09 00:29:46 +0400730 spin_lock(&ctx_alloc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 orig_pgsz_bits = (mm->context.sparc64_ctx_val & CTX_PGSZ_MASK);
732 ctx = (tlb_context_cache + 1) & CTX_NR_MASK;
733 new_ctx = find_next_zero_bit(mmu_context_bmap, 1 << CTX_NR_BITS, ctx);
David S. Millera0663a72006-02-23 14:19:28 -0800734 new_version = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 if (new_ctx >= (1 << CTX_NR_BITS)) {
736 new_ctx = find_next_zero_bit(mmu_context_bmap, ctx, 1);
737 if (new_ctx >= ctx) {
738 int i;
739 new_ctx = (tlb_context_cache & CTX_VERSION_MASK) +
740 CTX_FIRST_VERSION;
741 if (new_ctx == 1)
742 new_ctx = CTX_FIRST_VERSION;
743
744 /* Don't call memset, for 16 entries that's just
745 * plain silly...
746 */
747 mmu_context_bmap[0] = 3;
748 mmu_context_bmap[1] = 0;
749 mmu_context_bmap[2] = 0;
750 mmu_context_bmap[3] = 0;
751 for (i = 4; i < CTX_BMAP_SLOTS; i += 4) {
752 mmu_context_bmap[i + 0] = 0;
753 mmu_context_bmap[i + 1] = 0;
754 mmu_context_bmap[i + 2] = 0;
755 mmu_context_bmap[i + 3] = 0;
756 }
David S. Millera0663a72006-02-23 14:19:28 -0800757 new_version = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 goto out;
759 }
760 }
761 mmu_context_bmap[new_ctx>>6] |= (1UL << (new_ctx & 63));
762 new_ctx |= (tlb_context_cache & CTX_VERSION_MASK);
763out:
764 tlb_context_cache = new_ctx;
765 mm->context.sparc64_ctx_val = new_ctx | orig_pgsz_bits;
Kirill Tkhai07df8412013-04-09 00:29:46 +0400766 spin_unlock(&ctx_alloc_lock);
David S. Millera0663a72006-02-23 14:19:28 -0800767
768 if (unlikely(new_version))
769 smp_new_mmu_context_version();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770}
771
David S. Miller919ee672008-04-23 05:40:25 -0700772static int numa_enabled = 1;
773static int numa_debug;
774
775static int __init early_numa(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
David S. Miller919ee672008-04-23 05:40:25 -0700777 if (!p)
778 return 0;
David S. Millerd1112012006-03-08 02:16:07 -0800779
David S. Miller919ee672008-04-23 05:40:25 -0700780 if (strstr(p, "off"))
781 numa_enabled = 0;
David S. Millerd1112012006-03-08 02:16:07 -0800782
David S. Miller919ee672008-04-23 05:40:25 -0700783 if (strstr(p, "debug"))
784 numa_debug = 1;
785
786 return 0;
David S. Millerd1112012006-03-08 02:16:07 -0800787}
David S. Miller919ee672008-04-23 05:40:25 -0700788early_param("numa", early_numa);
789
790#define numadbg(f, a...) \
791do { if (numa_debug) \
792 printk(KERN_INFO f, ## a); \
793} while (0)
David S. Millerd1112012006-03-08 02:16:07 -0800794
David S. Miller4e82c9a2008-02-13 18:00:03 -0800795static void __init find_ramdisk(unsigned long phys_base)
796{
797#ifdef CONFIG_BLK_DEV_INITRD
798 if (sparc_ramdisk_image || sparc_ramdisk_image64) {
799 unsigned long ramdisk_image;
800
801 /* Older versions of the bootloader only supported a
802 * 32-bit physical address for the ramdisk image
803 * location, stored at sparc_ramdisk_image. Newer
804 * SILO versions set sparc_ramdisk_image to zero and
805 * provide a full 64-bit physical address at
806 * sparc_ramdisk_image64.
807 */
808 ramdisk_image = sparc_ramdisk_image;
809 if (!ramdisk_image)
810 ramdisk_image = sparc_ramdisk_image64;
811
812 /* Another bootloader quirk. The bootloader normalizes
813 * the physical address to KERNBASE, so we have to
814 * factor that back out and add in the lowest valid
815 * physical page address to get the true physical address.
816 */
817 ramdisk_image -= KERNBASE;
818 ramdisk_image += phys_base;
819
David S. Miller919ee672008-04-23 05:40:25 -0700820 numadbg("Found ramdisk at physical address 0x%lx, size %u\n",
821 ramdisk_image, sparc_ramdisk_size);
822
David S. Miller4e82c9a2008-02-13 18:00:03 -0800823 initrd_start = ramdisk_image;
824 initrd_end = ramdisk_image + sparc_ramdisk_size;
David S. Miller3b2a7e22008-02-13 18:13:20 -0800825
Yinghai Lu95f72d12010-07-12 14:36:09 +1000826 memblock_reserve(initrd_start, sparc_ramdisk_size);
David S. Millerd45100f2008-05-06 15:19:54 -0700827
828 initrd_start += PAGE_OFFSET;
829 initrd_end += PAGE_OFFSET;
David S. Miller4e82c9a2008-02-13 18:00:03 -0800830 }
831#endif
832}
833
David S. Miller919ee672008-04-23 05:40:25 -0700834struct node_mem_mask {
835 unsigned long mask;
Pavel Tatashin1537b262017-02-16 15:05:58 -0500836 unsigned long match;
David S. Miller919ee672008-04-23 05:40:25 -0700837};
838static struct node_mem_mask node_masks[MAX_NUMNODES];
839static int num_node_masks;
840
Sam Ravnborg48d37212014-05-16 23:26:12 +0200841#ifdef CONFIG_NEED_MULTIPLE_NODES
842
Pavel Tatashin1537b262017-02-16 15:05:58 -0500843struct mdesc_mlgroup {
844 u64 node;
845 u64 latency;
846 u64 match;
847 u64 mask;
848};
849
850static struct mdesc_mlgroup *mlgroups;
851static int num_mlgroups;
852
David S. Miller919ee672008-04-23 05:40:25 -0700853int numa_cpu_lookup_table[NR_CPUS];
854cpumask_t numa_cpumask_lookup_table[MAX_NUMNODES];
855
David S. Miller919ee672008-04-23 05:40:25 -0700856struct mdesc_mblock {
857 u64 base;
858 u64 size;
859 u64 offset; /* RA-to-PA */
860};
861static struct mdesc_mblock *mblocks;
862static int num_mblocks;
863
Pavel Tatashin1537b262017-02-16 15:05:58 -0500864static struct mdesc_mblock * __init addr_to_mblock(unsigned long addr)
David S. Millerd1112012006-03-08 02:16:07 -0800865{
Pavel Tatashin1537b262017-02-16 15:05:58 -0500866 struct mdesc_mblock *m = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 int i;
868
David S. Miller919ee672008-04-23 05:40:25 -0700869 for (i = 0; i < num_mblocks; i++) {
Pavel Tatashin1537b262017-02-16 15:05:58 -0500870 m = &mblocks[i];
David S. Miller6fc5bae2006-12-28 21:00:23 -0800871
David S. Miller919ee672008-04-23 05:40:25 -0700872 if (addr >= m->base &&
873 addr < (m->base + m->size)) {
David S. Miller919ee672008-04-23 05:40:25 -0700874 break;
875 }
876 }
Pavel Tatashin1537b262017-02-16 15:05:58 -0500877
878 return m;
David S. Miller919ee672008-04-23 05:40:25 -0700879}
880
Pavel Tatashin1537b262017-02-16 15:05:58 -0500881static u64 __init memblock_nid_range_sun4u(u64 start, u64 end, int *nid)
David S. Miller919ee672008-04-23 05:40:25 -0700882{
Pavel Tatashin1537b262017-02-16 15:05:58 -0500883 int prev_nid, new_nid;
David S. Miller919ee672008-04-23 05:40:25 -0700884
Pavel Tatashin1537b262017-02-16 15:05:58 -0500885 prev_nid = -1;
886 for ( ; start < end; start += PAGE_SIZE) {
887 for (new_nid = 0; new_nid < num_node_masks; new_nid++) {
888 struct node_mem_mask *p = &node_masks[new_nid];
David S. Miller919ee672008-04-23 05:40:25 -0700889
Pavel Tatashin1537b262017-02-16 15:05:58 -0500890 if ((start & p->mask) == p->match) {
891 if (prev_nid == -1)
892 prev_nid = new_nid;
893 break;
894 }
Thomas Tai74a5ed52016-11-03 09:19:01 -0700895 }
Thomas Tai74a5ed52016-11-03 09:19:01 -0700896
Pavel Tatashin1537b262017-02-16 15:05:58 -0500897 if (new_nid == num_node_masks) {
898 prev_nid = 0;
899 WARN_ONCE(1, "addr[%Lx] doesn't match a NUMA node rule. Some memory will be owned by node 0.",
900 start);
901 break;
902 }
903
904 if (prev_nid != new_nid)
905 break;
906 }
907 *nid = prev_nid;
908
909 return start > end ? end : start;
David S. Miller919ee672008-04-23 05:40:25 -0700910}
911
Thomas Tai87a349f2016-11-11 16:41:00 -0800912static u64 __init memblock_nid_range(u64 start, u64 end, int *nid)
David S. Miller919ee672008-04-23 05:40:25 -0700913{
Pavel Tatashin1537b262017-02-16 15:05:58 -0500914 u64 ret_end, pa_start, m_mask, m_match, m_end;
915 struct mdesc_mblock *mblock;
916 int _nid, i;
David S. Miller919ee672008-04-23 05:40:25 -0700917
Pavel Tatashin1537b262017-02-16 15:05:58 -0500918 if (tlb_type != hypervisor)
919 return memblock_nid_range_sun4u(start, end, nid);
920
921 mblock = addr_to_mblock(start);
922 if (!mblock) {
923 WARN_ONCE(1, "memblock_nid_range: Can't find mblock addr[%Lx]",
924 start);
925
926 _nid = 0;
927 ret_end = end;
928 goto done;
David S. Miller919ee672008-04-23 05:40:25 -0700929 }
930
Pavel Tatashin1537b262017-02-16 15:05:58 -0500931 pa_start = start + mblock->offset;
932 m_match = 0;
933 m_mask = 0;
David S. Millerc918dcc2008-08-14 01:41:39 -0700934
Pavel Tatashin1537b262017-02-16 15:05:58 -0500935 for (_nid = 0; _nid < num_node_masks; _nid++) {
936 struct node_mem_mask *const m = &node_masks[_nid];
937
938 if ((pa_start & m->mask) == m->match) {
939 m_match = m->match;
940 m_mask = m->mask;
941 break;
942 }
943 }
944
945 if (num_node_masks == _nid) {
946 /* We could not find NUMA group, so default to 0, but lets
947 * search for latency group, so we could calculate the correct
948 * end address that we return
949 */
950 _nid = 0;
951
952 for (i = 0; i < num_mlgroups; i++) {
953 struct mdesc_mlgroup *const m = &mlgroups[i];
954
955 if ((pa_start & m->mask) == m->match) {
956 m_match = m->match;
957 m_mask = m->mask;
958 break;
959 }
960 }
961
962 if (i == num_mlgroups) {
963 WARN_ONCE(1, "memblock_nid_range: Can't find latency group addr[%Lx]",
964 start);
965
966 ret_end = end;
967 goto done;
968 }
969 }
970
971 /*
972 * Each latency group has match and mask, and each memory block has an
973 * offset. An address belongs to a latency group if its address matches
974 * the following formula: ((addr + offset) & mask) == match
975 * It is, however, slow to check every single page if it matches a
976 * particular latency group. As optimization we calculate end value by
977 * using bit arithmetics.
978 */
979 m_end = m_match + (1ul << __ffs(m_mask)) - mblock->offset;
980 m_end += pa_start & ~((1ul << fls64(m_mask)) - 1);
981 ret_end = m_end > end ? end : m_end;
982
983done:
984 *nid = _nid;
985 return ret_end;
David S. Miller919ee672008-04-23 05:40:25 -0700986}
David S. Miller919ee672008-04-23 05:40:25 -0700987#endif
988
989/* This must be invoked after performing all of the necessary
Tejun Heo2a4814d2011-12-08 10:22:08 -0800990 * memblock_set_node() calls for 'nid'. We need to be able to get
David S. Miller919ee672008-04-23 05:40:25 -0700991 * correct data from get_pfn_range_for_nid().
992 */
993static void __init allocate_node_data(int nid)
994{
David S. Miller919ee672008-04-23 05:40:25 -0700995 struct pglist_data *p;
Paul Gortmakeraa6f0792012-05-09 20:44:29 -0400996 unsigned long start_pfn, end_pfn;
David S. Miller919ee672008-04-23 05:40:25 -0700997#ifdef CONFIG_NEED_MULTIPLE_NODES
Paul Gortmakeraa6f0792012-05-09 20:44:29 -0400998 unsigned long paddr;
999
Benjamin Herrenschmidt9d1e2492010-07-06 15:39:17 -07001000 paddr = memblock_alloc_try_nid(sizeof(struct pglist_data), SMP_CACHE_BYTES, nid);
David S. Miller919ee672008-04-23 05:40:25 -07001001 if (!paddr) {
1002 prom_printf("Cannot allocate pglist_data for nid[%d]\n", nid);
1003 prom_halt();
1004 }
1005 NODE_DATA(nid) = __va(paddr);
1006 memset(NODE_DATA(nid), 0, sizeof(struct pglist_data));
1007
David S. Miller625d6932012-04-25 13:13:43 -07001008 NODE_DATA(nid)->node_id = nid;
David S. Miller919ee672008-04-23 05:40:25 -07001009#endif
1010
1011 p = NODE_DATA(nid);
1012
1013 get_pfn_range_for_nid(nid, &start_pfn, &end_pfn);
1014 p->node_start_pfn = start_pfn;
1015 p->node_spanned_pages = end_pfn - start_pfn;
David S. Miller919ee672008-04-23 05:40:25 -07001016}
1017
1018static void init_node_masks_nonnuma(void)
1019{
Sam Ravnborg48d37212014-05-16 23:26:12 +02001020#ifdef CONFIG_NEED_MULTIPLE_NODES
David S. Miller919ee672008-04-23 05:40:25 -07001021 int i;
Sam Ravnborg48d37212014-05-16 23:26:12 +02001022#endif
David S. Miller919ee672008-04-23 05:40:25 -07001023
1024 numadbg("Initializing tables for non-numa.\n");
1025
Pavel Tatashin1537b262017-02-16 15:05:58 -05001026 node_masks[0].mask = 0;
1027 node_masks[0].match = 0;
David S. Miller919ee672008-04-23 05:40:25 -07001028 num_node_masks = 1;
1029
Sam Ravnborg48d37212014-05-16 23:26:12 +02001030#ifdef CONFIG_NEED_MULTIPLE_NODES
David S. Miller919ee672008-04-23 05:40:25 -07001031 for (i = 0; i < NR_CPUS; i++)
1032 numa_cpu_lookup_table[i] = 0;
1033
KOSAKI Motohirofb1fece2011-05-16 13:38:07 -07001034 cpumask_setall(&numa_cpumask_lookup_table[0]);
Sam Ravnborg48d37212014-05-16 23:26:12 +02001035#endif
David S. Miller919ee672008-04-23 05:40:25 -07001036}
1037
1038#ifdef CONFIG_NEED_MULTIPLE_NODES
1039struct pglist_data *node_data[MAX_NUMNODES];
1040
1041EXPORT_SYMBOL(numa_cpu_lookup_table);
1042EXPORT_SYMBOL(numa_cpumask_lookup_table);
1043EXPORT_SYMBOL(node_data);
1044
David S. Miller919ee672008-04-23 05:40:25 -07001045static int scan_pio_for_cfg_handle(struct mdesc_handle *md, u64 pio,
1046 u32 cfg_handle)
1047{
1048 u64 arc;
1049
1050 mdesc_for_each_arc(arc, md, pio, MDESC_ARC_TYPE_FWD) {
1051 u64 target = mdesc_arc_target(md, arc);
1052 const u64 *val;
1053
1054 val = mdesc_get_property(md, target,
1055 "cfg-handle", NULL);
1056 if (val && *val == cfg_handle)
1057 return 0;
1058 }
1059 return -ENODEV;
1060}
1061
1062static int scan_arcs_for_cfg_handle(struct mdesc_handle *md, u64 grp,
1063 u32 cfg_handle)
1064{
1065 u64 arc, candidate, best_latency = ~(u64)0;
1066
1067 candidate = MDESC_NODE_NULL;
1068 mdesc_for_each_arc(arc, md, grp, MDESC_ARC_TYPE_FWD) {
1069 u64 target = mdesc_arc_target(md, arc);
1070 const char *name = mdesc_node_name(md, target);
1071 const u64 *val;
1072
1073 if (strcmp(name, "pio-latency-group"))
1074 continue;
1075
1076 val = mdesc_get_property(md, target, "latency", NULL);
1077 if (!val)
1078 continue;
1079
1080 if (*val < best_latency) {
1081 candidate = target;
1082 best_latency = *val;
1083 }
1084 }
1085
1086 if (candidate == MDESC_NODE_NULL)
1087 return -ENODEV;
1088
1089 return scan_pio_for_cfg_handle(md, candidate, cfg_handle);
1090}
1091
1092int of_node_to_nid(struct device_node *dp)
1093{
1094 const struct linux_prom64_registers *regs;
1095 struct mdesc_handle *md;
1096 u32 cfg_handle;
1097 int count, nid;
1098 u64 grp;
1099
David S. Miller072bd412008-08-18 20:36:17 -07001100 /* This is the right thing to do on currently supported
1101 * SUN4U NUMA platforms as well, as the PCI controller does
1102 * not sit behind any particular memory controller.
1103 */
David S. Miller919ee672008-04-23 05:40:25 -07001104 if (!mlgroups)
1105 return -1;
1106
1107 regs = of_get_property(dp, "reg", NULL);
1108 if (!regs)
1109 return -1;
1110
1111 cfg_handle = (regs->phys_addr >> 32UL) & 0x0fffffff;
1112
1113 md = mdesc_grab();
1114
1115 count = 0;
1116 nid = -1;
1117 mdesc_for_each_node_by_name(md, grp, "group") {
1118 if (!scan_arcs_for_cfg_handle(md, grp, cfg_handle)) {
1119 nid = count;
1120 break;
1121 }
1122 count++;
1123 }
1124
1125 mdesc_release(md);
1126
1127 return nid;
1128}
1129
David S. Miller01c453812009-04-07 01:05:22 -07001130static void __init add_node_ranges(void)
David S. Miller919ee672008-04-23 05:40:25 -07001131{
Benjamin Herrenschmidt08b84792010-08-04 13:43:31 +10001132 struct memblock_region *reg;
Pavel Tatashincd429ce2017-02-16 15:13:54 -05001133 unsigned long prev_max;
1134
1135memblock_resized:
1136 prev_max = memblock.memory.max;
David S. Miller919ee672008-04-23 05:40:25 -07001137
Benjamin Herrenschmidt08b84792010-08-04 13:43:31 +10001138 for_each_memblock(memory, reg) {
1139 unsigned long size = reg->size;
David S. Miller919ee672008-04-23 05:40:25 -07001140 unsigned long start, end;
1141
Benjamin Herrenschmidt08b84792010-08-04 13:43:31 +10001142 start = reg->base;
David S. Miller919ee672008-04-23 05:40:25 -07001143 end = start + size;
1144 while (start < end) {
1145 unsigned long this_end;
1146 int nid;
1147
Benjamin Herrenschmidt35a1f0b2010-07-06 15:38:58 -07001148 this_end = memblock_nid_range(start, end, &nid);
David S. Miller919ee672008-04-23 05:40:25 -07001149
Tejun Heo2a4814d2011-12-08 10:22:08 -08001150 numadbg("Setting memblock NUMA node nid[%d] "
David S. Miller919ee672008-04-23 05:40:25 -07001151 "start[%lx] end[%lx]\n",
1152 nid, start, this_end);
1153
Tang Chene7e8de52014-01-21 15:49:26 -08001154 memblock_set_node(start, this_end - start,
1155 &memblock.memory, nid);
Pavel Tatashincd429ce2017-02-16 15:13:54 -05001156 if (memblock.memory.max != prev_max)
1157 goto memblock_resized;
David S. Miller919ee672008-04-23 05:40:25 -07001158 start = this_end;
1159 }
1160 }
1161}
1162
1163static int __init grab_mlgroups(struct mdesc_handle *md)
1164{
1165 unsigned long paddr;
1166 int count = 0;
1167 u64 node;
1168
1169 mdesc_for_each_node_by_name(md, node, "memory-latency-group")
1170 count++;
1171 if (!count)
1172 return -ENOENT;
1173
Yinghai Lu95f72d12010-07-12 14:36:09 +10001174 paddr = memblock_alloc(count * sizeof(struct mdesc_mlgroup),
David S. Miller919ee672008-04-23 05:40:25 -07001175 SMP_CACHE_BYTES);
1176 if (!paddr)
1177 return -ENOMEM;
1178
1179 mlgroups = __va(paddr);
1180 num_mlgroups = count;
1181
1182 count = 0;
1183 mdesc_for_each_node_by_name(md, node, "memory-latency-group") {
1184 struct mdesc_mlgroup *m = &mlgroups[count++];
1185 const u64 *val;
1186
1187 m->node = node;
1188
1189 val = mdesc_get_property(md, node, "latency", NULL);
1190 m->latency = *val;
1191 val = mdesc_get_property(md, node, "address-match", NULL);
1192 m->match = *val;
1193 val = mdesc_get_property(md, node, "address-mask", NULL);
1194 m->mask = *val;
1195
Sam Ravnborg90181132009-01-06 13:19:28 -08001196 numadbg("MLGROUP[%d]: node[%llx] latency[%llx] "
1197 "match[%llx] mask[%llx]\n",
David S. Miller919ee672008-04-23 05:40:25 -07001198 count - 1, m->node, m->latency, m->match, m->mask);
1199 }
1200
1201 return 0;
1202}
1203
1204static int __init grab_mblocks(struct mdesc_handle *md)
1205{
1206 unsigned long paddr;
1207 int count = 0;
1208 u64 node;
1209
1210 mdesc_for_each_node_by_name(md, node, "mblock")
1211 count++;
1212 if (!count)
1213 return -ENOENT;
1214
Yinghai Lu95f72d12010-07-12 14:36:09 +10001215 paddr = memblock_alloc(count * sizeof(struct mdesc_mblock),
David S. Miller919ee672008-04-23 05:40:25 -07001216 SMP_CACHE_BYTES);
1217 if (!paddr)
1218 return -ENOMEM;
1219
1220 mblocks = __va(paddr);
1221 num_mblocks = count;
1222
1223 count = 0;
1224 mdesc_for_each_node_by_name(md, node, "mblock") {
1225 struct mdesc_mblock *m = &mblocks[count++];
1226 const u64 *val;
1227
1228 val = mdesc_get_property(md, node, "base", NULL);
1229 m->base = *val;
1230 val = mdesc_get_property(md, node, "size", NULL);
1231 m->size = *val;
1232 val = mdesc_get_property(md, node,
1233 "address-congruence-offset", NULL);
bob picco771a37f2013-06-11 14:54:51 -04001234
1235 /* The address-congruence-offset property is optional.
1236 * Explicity zero it be identifty this.
1237 */
1238 if (val)
1239 m->offset = *val;
1240 else
1241 m->offset = 0UL;
David S. Miller919ee672008-04-23 05:40:25 -07001242
Sam Ravnborg90181132009-01-06 13:19:28 -08001243 numadbg("MBLOCK[%d]: base[%llx] size[%llx] offset[%llx]\n",
David S. Miller919ee672008-04-23 05:40:25 -07001244 count - 1, m->base, m->size, m->offset);
1245 }
1246
1247 return 0;
1248}
1249
1250static void __init numa_parse_mdesc_group_cpus(struct mdesc_handle *md,
1251 u64 grp, cpumask_t *mask)
1252{
1253 u64 arc;
1254
KOSAKI Motohirofb1fece2011-05-16 13:38:07 -07001255 cpumask_clear(mask);
David S. Miller919ee672008-04-23 05:40:25 -07001256
1257 mdesc_for_each_arc(arc, md, grp, MDESC_ARC_TYPE_BACK) {
1258 u64 target = mdesc_arc_target(md, arc);
1259 const char *name = mdesc_node_name(md, target);
1260 const u64 *id;
1261
1262 if (strcmp(name, "cpu"))
1263 continue;
1264 id = mdesc_get_property(md, target, "id", NULL);
Rusty Russelle305cb8f2009-03-16 14:40:23 +10301265 if (*id < nr_cpu_ids)
KOSAKI Motohirofb1fece2011-05-16 13:38:07 -07001266 cpumask_set_cpu(*id, mask);
David S. Miller919ee672008-04-23 05:40:25 -07001267 }
1268}
1269
1270static struct mdesc_mlgroup * __init find_mlgroup(u64 node)
1271{
1272 int i;
1273
1274 for (i = 0; i < num_mlgroups; i++) {
1275 struct mdesc_mlgroup *m = &mlgroups[i];
1276 if (m->node == node)
1277 return m;
1278 }
1279 return NULL;
1280}
1281
Nitin Gupta52708d62015-11-02 16:30:24 -05001282int __node_distance(int from, int to)
1283{
1284 if ((from >= MAX_NUMNODES) || (to >= MAX_NUMNODES)) {
1285 pr_warn("Returning default NUMA distance value for %d->%d\n",
1286 from, to);
1287 return (from == to) ? LOCAL_DISTANCE : REMOTE_DISTANCE;
1288 }
1289 return numa_latency[from][to];
1290}
1291
Paul Gortmakerbdf2f592016-08-06 00:31:48 -04001292static int __init find_best_numa_node_for_mlgroup(struct mdesc_mlgroup *grp)
Nitin Gupta52708d62015-11-02 16:30:24 -05001293{
1294 int i;
1295
1296 for (i = 0; i < MAX_NUMNODES; i++) {
1297 struct node_mem_mask *n = &node_masks[i];
1298
Pavel Tatashin1537b262017-02-16 15:05:58 -05001299 if ((grp->mask == n->mask) && (grp->match == n->match))
Nitin Gupta52708d62015-11-02 16:30:24 -05001300 break;
1301 }
1302 return i;
1303}
1304
Paul Gortmakerbdf2f592016-08-06 00:31:48 -04001305static void __init find_numa_latencies_for_group(struct mdesc_handle *md,
1306 u64 grp, int index)
Nitin Gupta52708d62015-11-02 16:30:24 -05001307{
1308 u64 arc;
1309
1310 mdesc_for_each_arc(arc, md, grp, MDESC_ARC_TYPE_FWD) {
1311 int tnode;
1312 u64 target = mdesc_arc_target(md, arc);
1313 struct mdesc_mlgroup *m = find_mlgroup(target);
1314
1315 if (!m)
1316 continue;
1317 tnode = find_best_numa_node_for_mlgroup(m);
1318 if (tnode == MAX_NUMNODES)
1319 continue;
1320 numa_latency[index][tnode] = m->latency;
1321 }
1322}
1323
David S. Miller919ee672008-04-23 05:40:25 -07001324static int __init numa_attach_mlgroup(struct mdesc_handle *md, u64 grp,
1325 int index)
1326{
1327 struct mdesc_mlgroup *candidate = NULL;
1328 u64 arc, best_latency = ~(u64)0;
1329 struct node_mem_mask *n;
1330
1331 mdesc_for_each_arc(arc, md, grp, MDESC_ARC_TYPE_FWD) {
1332 u64 target = mdesc_arc_target(md, arc);
1333 struct mdesc_mlgroup *m = find_mlgroup(target);
1334 if (!m)
1335 continue;
1336 if (m->latency < best_latency) {
1337 candidate = m;
1338 best_latency = m->latency;
1339 }
1340 }
1341 if (!candidate)
1342 return -ENOENT;
1343
1344 if (num_node_masks != index) {
1345 printk(KERN_ERR "Inconsistent NUMA state, "
1346 "index[%d] != num_node_masks[%d]\n",
1347 index, num_node_masks);
1348 return -EINVAL;
1349 }
1350
1351 n = &node_masks[num_node_masks++];
1352
1353 n->mask = candidate->mask;
Pavel Tatashin1537b262017-02-16 15:05:58 -05001354 n->match = candidate->match;
David S. Miller919ee672008-04-23 05:40:25 -07001355
Pavel Tatashin1537b262017-02-16 15:05:58 -05001356 numadbg("NUMA NODE[%d]: mask[%lx] match[%lx] (latency[%llx])\n",
1357 index, n->mask, n->match, candidate->latency);
David S. Miller919ee672008-04-23 05:40:25 -07001358
1359 return 0;
1360}
1361
1362static int __init numa_parse_mdesc_group(struct mdesc_handle *md, u64 grp,
1363 int index)
1364{
1365 cpumask_t mask;
1366 int cpu;
1367
1368 numa_parse_mdesc_group_cpus(md, grp, &mask);
1369
KOSAKI Motohirofb1fece2011-05-16 13:38:07 -07001370 for_each_cpu(cpu, &mask)
David S. Miller919ee672008-04-23 05:40:25 -07001371 numa_cpu_lookup_table[cpu] = index;
KOSAKI Motohirofb1fece2011-05-16 13:38:07 -07001372 cpumask_copy(&numa_cpumask_lookup_table[index], &mask);
David S. Miller919ee672008-04-23 05:40:25 -07001373
1374 if (numa_debug) {
1375 printk(KERN_INFO "NUMA GROUP[%d]: cpus [ ", index);
KOSAKI Motohirofb1fece2011-05-16 13:38:07 -07001376 for_each_cpu(cpu, &mask)
David S. Miller919ee672008-04-23 05:40:25 -07001377 printk("%d ", cpu);
1378 printk("]\n");
1379 }
1380
1381 return numa_attach_mlgroup(md, grp, index);
1382}
1383
1384static int __init numa_parse_mdesc(void)
1385{
1386 struct mdesc_handle *md = mdesc_grab();
Nitin Gupta52708d62015-11-02 16:30:24 -05001387 int i, j, err, count;
David S. Miller919ee672008-04-23 05:40:25 -07001388 u64 node;
1389
1390 node = mdesc_node_by_name(md, MDESC_NODE_NULL, "latency-groups");
1391 if (node == MDESC_NODE_NULL) {
1392 mdesc_release(md);
1393 return -ENOENT;
1394 }
1395
1396 err = grab_mblocks(md);
1397 if (err < 0)
1398 goto out;
1399
1400 err = grab_mlgroups(md);
1401 if (err < 0)
1402 goto out;
1403
1404 count = 0;
1405 mdesc_for_each_node_by_name(md, node, "group") {
1406 err = numa_parse_mdesc_group(md, node, count);
1407 if (err < 0)
1408 break;
1409 count++;
1410 }
1411
Nitin Gupta52708d62015-11-02 16:30:24 -05001412 count = 0;
1413 mdesc_for_each_node_by_name(md, node, "group") {
1414 find_numa_latencies_for_group(md, node, count);
1415 count++;
1416 }
1417
1418 /* Normalize numa latency matrix according to ACPI SLIT spec. */
1419 for (i = 0; i < MAX_NUMNODES; i++) {
1420 u64 self_latency = numa_latency[i][i];
1421
1422 for (j = 0; j < MAX_NUMNODES; j++) {
1423 numa_latency[i][j] =
1424 (numa_latency[i][j] * LOCAL_DISTANCE) /
1425 self_latency;
1426 }
1427 }
1428
David S. Miller919ee672008-04-23 05:40:25 -07001429 add_node_ranges();
1430
1431 for (i = 0; i < num_node_masks; i++) {
1432 allocate_node_data(i);
1433 node_set_online(i);
1434 }
1435
1436 err = 0;
1437out:
1438 mdesc_release(md);
1439 return err;
1440}
1441
David S. Miller072bd412008-08-18 20:36:17 -07001442static int __init numa_parse_jbus(void)
1443{
1444 unsigned long cpu, index;
1445
1446 /* NUMA node id is encoded in bits 36 and higher, and there is
1447 * a 1-to-1 mapping from CPU ID to NUMA node ID.
1448 */
1449 index = 0;
1450 for_each_present_cpu(cpu) {
1451 numa_cpu_lookup_table[cpu] = index;
KOSAKI Motohirofb1fece2011-05-16 13:38:07 -07001452 cpumask_copy(&numa_cpumask_lookup_table[index], cpumask_of(cpu));
David S. Miller072bd412008-08-18 20:36:17 -07001453 node_masks[index].mask = ~((1UL << 36UL) - 1UL);
Pavel Tatashin1537b262017-02-16 15:05:58 -05001454 node_masks[index].match = cpu << 36UL;
David S. Miller072bd412008-08-18 20:36:17 -07001455
1456 index++;
1457 }
1458 num_node_masks = index;
1459
1460 add_node_ranges();
1461
1462 for (index = 0; index < num_node_masks; index++) {
1463 allocate_node_data(index);
1464 node_set_online(index);
1465 }
1466
1467 return 0;
1468}
1469
David S. Miller919ee672008-04-23 05:40:25 -07001470static int __init numa_parse_sun4u(void)
1471{
David S. Miller072bd412008-08-18 20:36:17 -07001472 if (tlb_type == cheetah || tlb_type == cheetah_plus) {
1473 unsigned long ver;
1474
1475 __asm__ ("rdpr %%ver, %0" : "=r" (ver));
1476 if ((ver >> 32UL) == __JALAPENO_ID ||
1477 (ver >> 32UL) == __SERRANO_ID)
1478 return numa_parse_jbus();
1479 }
David S. Miller919ee672008-04-23 05:40:25 -07001480 return -1;
1481}
1482
1483static int __init bootmem_init_numa(void)
1484{
Nitin Gupta36beca62016-01-05 22:35:35 -08001485 int i, j;
David S. Miller919ee672008-04-23 05:40:25 -07001486 int err = -1;
1487
1488 numadbg("bootmem_init_numa()\n");
1489
Nitin Gupta36beca62016-01-05 22:35:35 -08001490 /* Some sane defaults for numa latency values */
1491 for (i = 0; i < MAX_NUMNODES; i++) {
1492 for (j = 0; j < MAX_NUMNODES; j++)
1493 numa_latency[i][j] = (i == j) ?
1494 LOCAL_DISTANCE : REMOTE_DISTANCE;
1495 }
1496
David S. Miller919ee672008-04-23 05:40:25 -07001497 if (numa_enabled) {
1498 if (tlb_type == hypervisor)
1499 err = numa_parse_mdesc();
1500 else
1501 err = numa_parse_sun4u();
1502 }
1503 return err;
1504}
1505
1506#else
1507
1508static int bootmem_init_numa(void)
1509{
1510 return -1;
1511}
1512
1513#endif
1514
1515static void __init bootmem_init_nonnuma(void)
1516{
Yinghai Lu95f72d12010-07-12 14:36:09 +10001517 unsigned long top_of_ram = memblock_end_of_DRAM();
1518 unsigned long total_ram = memblock_phys_mem_size();
David S. Miller919ee672008-04-23 05:40:25 -07001519
1520 numadbg("bootmem_init_nonnuma()\n");
1521
1522 printk(KERN_INFO "Top of RAM: 0x%lx, Total RAM: 0x%lx\n",
1523 top_of_ram, total_ram);
1524 printk(KERN_INFO "Memory hole size: %ldMB\n",
1525 (top_of_ram - total_ram) >> 20);
1526
1527 init_node_masks_nonnuma();
Tang Chene7e8de52014-01-21 15:49:26 -08001528 memblock_set_node(0, (phys_addr_t)ULLONG_MAX, &memblock.memory, 0);
David S. Miller919ee672008-04-23 05:40:25 -07001529 allocate_node_data(0);
David S. Miller919ee672008-04-23 05:40:25 -07001530 node_set_online(0);
1531}
1532
David S. Miller919ee672008-04-23 05:40:25 -07001533static unsigned long __init bootmem_init(unsigned long phys_base)
1534{
1535 unsigned long end_pfn;
David S. Miller919ee672008-04-23 05:40:25 -07001536
Yinghai Lu95f72d12010-07-12 14:36:09 +10001537 end_pfn = memblock_end_of_DRAM() >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 max_pfn = max_low_pfn = end_pfn;
David S. Millerd1112012006-03-08 02:16:07 -08001539 min_low_pfn = (phys_base >> PAGE_SHIFT);
1540
David S. Miller919ee672008-04-23 05:40:25 -07001541 if (bootmem_init_numa() < 0)
1542 bootmem_init_nonnuma();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
David S. Miller625d6932012-04-25 13:13:43 -07001544 /* Dump memblock with node info. */
1545 memblock_dump_all();
1546
David S. Miller919ee672008-04-23 05:40:25 -07001547 /* XXX cpu notifier XXX */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
David S. Miller625d6932012-04-25 13:13:43 -07001549 sparse_memory_present_with_active_regions(MAX_NUMNODES);
David S. Millerd1112012006-03-08 02:16:07 -08001550 sparse_init();
1551
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 return end_pfn;
1553}
1554
David S. Miller9cc3a1a2006-02-21 20:51:13 -08001555static struct linux_prom64_registers pall[MAX_BANKS] __initdata;
1556static int pall_ents __initdata;
1557
David S. Miller0dd5b7b2014-09-24 20:56:11 -07001558static unsigned long max_phys_bits = 40;
1559
1560bool kern_addr_valid(unsigned long addr)
1561{
David S. Miller0dd5b7b2014-09-24 20:56:11 -07001562 pgd_t *pgd;
1563 pud_t *pud;
1564 pmd_t *pmd;
1565 pte_t *pte;
1566
David S. Millerbb4e6e82014-09-27 11:05:21 -07001567 if ((long)addr < 0L) {
1568 unsigned long pa = __pa(addr);
1569
1570 if ((addr >> max_phys_bits) != 0UL)
1571 return false;
1572
1573 return pfn_valid(pa >> PAGE_SHIFT);
1574 }
David S. Miller0dd5b7b2014-09-24 20:56:11 -07001575
1576 if (addr >= (unsigned long) KERNBASE &&
1577 addr < (unsigned long)&_end)
1578 return true;
1579
David S. Miller0dd5b7b2014-09-24 20:56:11 -07001580 pgd = pgd_offset_k(addr);
1581 if (pgd_none(*pgd))
1582 return 0;
1583
1584 pud = pud_offset(pgd, addr);
1585 if (pud_none(*pud))
1586 return 0;
1587
1588 if (pud_large(*pud))
1589 return pfn_valid(pud_pfn(*pud));
1590
1591 pmd = pmd_offset(pud, addr);
1592 if (pmd_none(*pmd))
1593 return 0;
1594
1595 if (pmd_large(*pmd))
1596 return pfn_valid(pmd_pfn(*pmd));
1597
1598 pte = pte_offset_kernel(pmd, addr);
1599 if (pte_none(*pte))
1600 return 0;
1601
1602 return pfn_valid(pte_pfn(*pte));
1603}
1604EXPORT_SYMBOL(kern_addr_valid);
1605
1606static unsigned long __ref kernel_map_hugepud(unsigned long vstart,
1607 unsigned long vend,
1608 pud_t *pud)
1609{
1610 const unsigned long mask16gb = (1UL << 34) - 1UL;
1611 u64 pte_val = vstart;
1612
1613 /* Each PUD is 8GB */
1614 if ((vstart & mask16gb) ||
1615 (vend - vstart <= mask16gb)) {
1616 pte_val ^= kern_linear_pte_xor[2];
1617 pud_val(*pud) = pte_val | _PAGE_PUD_HUGE;
1618
1619 return vstart + PUD_SIZE;
1620 }
1621
1622 pte_val ^= kern_linear_pte_xor[3];
1623 pte_val |= _PAGE_PUD_HUGE;
1624
1625 vend = vstart + mask16gb + 1UL;
1626 while (vstart < vend) {
1627 pud_val(*pud) = pte_val;
1628
1629 pte_val += PUD_SIZE;
1630 vstart += PUD_SIZE;
1631 pud++;
1632 }
1633 return vstart;
1634}
1635
1636static bool kernel_can_map_hugepud(unsigned long vstart, unsigned long vend,
1637 bool guard)
1638{
1639 if (guard && !(vstart & ~PUD_MASK) && (vend - vstart) >= PUD_SIZE)
1640 return true;
1641
1642 return false;
1643}
1644
1645static unsigned long __ref kernel_map_hugepmd(unsigned long vstart,
1646 unsigned long vend,
1647 pmd_t *pmd)
1648{
1649 const unsigned long mask256mb = (1UL << 28) - 1UL;
1650 const unsigned long mask2gb = (1UL << 31) - 1UL;
1651 u64 pte_val = vstart;
1652
1653 /* Each PMD is 8MB */
1654 if ((vstart & mask256mb) ||
1655 (vend - vstart <= mask256mb)) {
1656 pte_val ^= kern_linear_pte_xor[0];
1657 pmd_val(*pmd) = pte_val | _PAGE_PMD_HUGE;
1658
1659 return vstart + PMD_SIZE;
1660 }
1661
1662 if ((vstart & mask2gb) ||
1663 (vend - vstart <= mask2gb)) {
1664 pte_val ^= kern_linear_pte_xor[1];
1665 pte_val |= _PAGE_PMD_HUGE;
1666 vend = vstart + mask256mb + 1UL;
1667 } else {
1668 pte_val ^= kern_linear_pte_xor[2];
1669 pte_val |= _PAGE_PMD_HUGE;
1670 vend = vstart + mask2gb + 1UL;
1671 }
1672
1673 while (vstart < vend) {
1674 pmd_val(*pmd) = pte_val;
1675
1676 pte_val += PMD_SIZE;
1677 vstart += PMD_SIZE;
1678 pmd++;
1679 }
1680
1681 return vstart;
1682}
1683
1684static bool kernel_can_map_hugepmd(unsigned long vstart, unsigned long vend,
1685 bool guard)
1686{
1687 if (guard && !(vstart & ~PMD_MASK) && (vend - vstart) >= PMD_SIZE)
1688 return true;
1689
1690 return false;
1691}
1692
Sam Ravnborg896aef42008-02-24 19:49:52 -08001693static unsigned long __ref kernel_map_range(unsigned long pstart,
David S. Miller0dd5b7b2014-09-24 20:56:11 -07001694 unsigned long pend, pgprot_t prot,
1695 bool use_huge)
David S. Miller56425302005-09-25 16:46:57 -07001696{
1697 unsigned long vstart = PAGE_OFFSET + pstart;
1698 unsigned long vend = PAGE_OFFSET + pend;
1699 unsigned long alloc_bytes = 0UL;
1700
1701 if ((vstart & ~PAGE_MASK) || (vend & ~PAGE_MASK)) {
David S. Miller13edad72005-09-29 17:58:26 -07001702 prom_printf("kernel_map: Unaligned physmem[%lx:%lx]\n",
David S. Miller56425302005-09-25 16:46:57 -07001703 vstart, vend);
1704 prom_halt();
1705 }
1706
1707 while (vstart < vend) {
1708 unsigned long this_end, paddr = __pa(vstart);
1709 pgd_t *pgd = pgd_offset_k(vstart);
1710 pud_t *pud;
1711 pmd_t *pmd;
1712 pte_t *pte;
1713
David S. Millerac55c762014-09-26 21:19:46 -07001714 if (pgd_none(*pgd)) {
1715 pud_t *new;
1716
1717 new = __alloc_bootmem(PAGE_SIZE, PAGE_SIZE, PAGE_SIZE);
1718 alloc_bytes += PAGE_SIZE;
1719 pgd_populate(&init_mm, pgd, new);
1720 }
David S. Miller56425302005-09-25 16:46:57 -07001721 pud = pud_offset(pgd, vstart);
1722 if (pud_none(*pud)) {
1723 pmd_t *new;
1724
David S. Miller0dd5b7b2014-09-24 20:56:11 -07001725 if (kernel_can_map_hugepud(vstart, vend, use_huge)) {
1726 vstart = kernel_map_hugepud(vstart, vend, pud);
1727 continue;
1728 }
David S. Miller56425302005-09-25 16:46:57 -07001729 new = __alloc_bootmem(PAGE_SIZE, PAGE_SIZE, PAGE_SIZE);
1730 alloc_bytes += PAGE_SIZE;
1731 pud_populate(&init_mm, pud, new);
1732 }
1733
1734 pmd = pmd_offset(pud, vstart);
David S. Miller0dd5b7b2014-09-24 20:56:11 -07001735 if (pmd_none(*pmd)) {
David S. Miller56425302005-09-25 16:46:57 -07001736 pte_t *new;
1737
David S. Miller0dd5b7b2014-09-24 20:56:11 -07001738 if (kernel_can_map_hugepmd(vstart, vend, use_huge)) {
1739 vstart = kernel_map_hugepmd(vstart, vend, pmd);
1740 continue;
1741 }
David S. Miller56425302005-09-25 16:46:57 -07001742 new = __alloc_bootmem(PAGE_SIZE, PAGE_SIZE, PAGE_SIZE);
1743 alloc_bytes += PAGE_SIZE;
1744 pmd_populate_kernel(&init_mm, pmd, new);
1745 }
1746
1747 pte = pte_offset_kernel(pmd, vstart);
1748 this_end = (vstart + PMD_SIZE) & PMD_MASK;
1749 if (this_end > vend)
1750 this_end = vend;
1751
1752 while (vstart < this_end) {
1753 pte_val(*pte) = (paddr | pgprot_val(prot));
1754
1755 vstart += PAGE_SIZE;
1756 paddr += PAGE_SIZE;
1757 pte++;
1758 }
1759 }
1760
1761 return alloc_bytes;
1762}
1763
David S. Miller0dd5b7b2014-09-24 20:56:11 -07001764static void __init flush_all_kernel_tsbs(void)
1765{
1766 int i;
1767
1768 for (i = 0; i < KERNEL_TSB_NENTRIES; i++) {
1769 struct tsb *ent = &swapper_tsb[i];
1770
1771 ent->tag = (1UL << TSB_TAG_INVALID_BIT);
1772 }
1773#ifndef CONFIG_DEBUG_PAGEALLOC
1774 for (i = 0; i < KERNEL_TSB4M_NENTRIES; i++) {
1775 struct tsb *ent = &swapper_4m_tsb[i];
1776
1777 ent->tag = (1UL << TSB_TAG_INVALID_BIT);
1778 }
1779#endif
1780}
1781
David S. Miller56425302005-09-25 16:46:57 -07001782extern unsigned int kvmap_linear_patch[1];
David S. Miller9cc3a1a2006-02-21 20:51:13 -08001783
David S. Miller8f3614532007-12-13 06:13:38 -08001784static void __init kernel_physical_mapping_init(void)
1785{
David S. Miller8f3614532007-12-13 06:13:38 -08001786 unsigned long i, mem_alloced = 0UL;
David S. Miller0dd5b7b2014-09-24 20:56:11 -07001787 bool use_huge = true;
David S. Miller8f3614532007-12-13 06:13:38 -08001788
David S. Miller0dd5b7b2014-09-24 20:56:11 -07001789#ifdef CONFIG_DEBUG_PAGEALLOC
1790 use_huge = false;
1791#endif
David S. Miller8f3614532007-12-13 06:13:38 -08001792 for (i = 0; i < pall_ents; i++) {
1793 unsigned long phys_start, phys_end;
1794
1795 phys_start = pall[i].phys_addr;
1796 phys_end = phys_start + pall[i].reg_size;
1797
David S. Miller56425302005-09-25 16:46:57 -07001798 mem_alloced += kernel_map_range(phys_start, phys_end,
David S. Miller0dd5b7b2014-09-24 20:56:11 -07001799 PAGE_KERNEL, use_huge);
David S. Miller56425302005-09-25 16:46:57 -07001800 }
1801
1802 printk("Allocated %ld bytes for kernel page tables.\n",
1803 mem_alloced);
1804
1805 kvmap_linear_patch[0] = 0x01000000; /* nop */
1806 flushi(&kvmap_linear_patch[0]);
1807
David S. Miller0dd5b7b2014-09-24 20:56:11 -07001808 flush_all_kernel_tsbs();
1809
David S. Miller56425302005-09-25 16:46:57 -07001810 __flush_tlb_all();
1811}
1812
David S. Miller9cc3a1a2006-02-21 20:51:13 -08001813#ifdef CONFIG_DEBUG_PAGEALLOC
Joonsoo Kim031bc572014-12-12 16:55:52 -08001814void __kernel_map_pages(struct page *page, int numpages, int enable)
David S. Miller56425302005-09-25 16:46:57 -07001815{
1816 unsigned long phys_start = page_to_pfn(page) << PAGE_SHIFT;
1817 unsigned long phys_end = phys_start + (numpages * PAGE_SIZE);
1818
1819 kernel_map_range(phys_start, phys_end,
David S. Miller0dd5b7b2014-09-24 20:56:11 -07001820 (enable ? PAGE_KERNEL : __pgprot(0)), false);
David S. Miller56425302005-09-25 16:46:57 -07001821
David S. Miller74bf4312006-01-31 18:29:18 -08001822 flush_tsb_kernel_range(PAGE_OFFSET + phys_start,
1823 PAGE_OFFSET + phys_end);
1824
David S. Miller56425302005-09-25 16:46:57 -07001825 /* we should perform an IPI and flush all tlbs,
1826 * but that can deadlock->flush only current cpu.
1827 */
1828 __flush_tlb_kernel_range(PAGE_OFFSET + phys_start,
1829 PAGE_OFFSET + phys_end);
1830}
1831#endif
1832
David S. Miller10147572005-09-28 21:46:43 -07001833unsigned long __init find_ecache_flush_span(unsigned long size)
1834{
David S. Miller13edad72005-09-29 17:58:26 -07001835 int i;
David S. Miller10147572005-09-28 21:46:43 -07001836
David S. Miller13edad72005-09-29 17:58:26 -07001837 for (i = 0; i < pavail_ents; i++) {
1838 if (pavail[i].reg_size >= size)
1839 return pavail[i].phys_addr;
David S. Miller10147572005-09-28 21:46:43 -07001840 }
1841
1842 return ~0UL;
1843}
1844
David S. Millerb2d43832013-09-20 21:50:41 -07001845unsigned long PAGE_OFFSET;
1846EXPORT_SYMBOL(PAGE_OFFSET);
1847
David S. Millerbb4e6e82014-09-27 11:05:21 -07001848unsigned long VMALLOC_END = 0x0000010000000000UL;
1849EXPORT_SYMBOL(VMALLOC_END);
1850
David S. Miller4397bed2014-09-26 21:58:33 -07001851unsigned long sparc64_va_hole_top = 0xfffff80000000000UL;
1852unsigned long sparc64_va_hole_bottom = 0x0000080000000000UL;
1853
David S. Millerb2d43832013-09-20 21:50:41 -07001854static void __init setup_page_offset(void)
1855{
David S. Millerb2d43832013-09-20 21:50:41 -07001856 if (tlb_type == cheetah || tlb_type == cheetah_plus) {
David S. Miller4397bed2014-09-26 21:58:33 -07001857 /* Cheetah/Panther support a full 64-bit virtual
1858 * address, so we can use all that our page tables
1859 * support.
1860 */
1861 sparc64_va_hole_top = 0xfff0000000000000UL;
1862 sparc64_va_hole_bottom = 0x0010000000000000UL;
1863
David S. Millerb2d43832013-09-20 21:50:41 -07001864 max_phys_bits = 42;
1865 } else if (tlb_type == hypervisor) {
1866 switch (sun4v_chip_type) {
1867 case SUN4V_CHIP_NIAGARA1:
1868 case SUN4V_CHIP_NIAGARA2:
David S. Miller4397bed2014-09-26 21:58:33 -07001869 /* T1 and T2 support 48-bit virtual addresses. */
1870 sparc64_va_hole_top = 0xffff800000000000UL;
1871 sparc64_va_hole_bottom = 0x0000800000000000UL;
1872
David S. Millerb2d43832013-09-20 21:50:41 -07001873 max_phys_bits = 39;
1874 break;
1875 case SUN4V_CHIP_NIAGARA3:
David S. Miller4397bed2014-09-26 21:58:33 -07001876 /* T3 supports 48-bit virtual addresses. */
1877 sparc64_va_hole_top = 0xffff800000000000UL;
1878 sparc64_va_hole_bottom = 0x0000800000000000UL;
1879
David S. Millerb2d43832013-09-20 21:50:41 -07001880 max_phys_bits = 43;
1881 break;
1882 case SUN4V_CHIP_NIAGARA4:
1883 case SUN4V_CHIP_NIAGARA5:
1884 case SUN4V_CHIP_SPARC64X:
David S. Miller7c0fa0f2014-09-24 21:49:29 -07001885 case SUN4V_CHIP_SPARC_M6:
David S. Miller4397bed2014-09-26 21:58:33 -07001886 /* T4 and later support 52-bit virtual addresses. */
1887 sparc64_va_hole_top = 0xfff8000000000000UL;
1888 sparc64_va_hole_bottom = 0x0008000000000000UL;
David S. Millerb2d43832013-09-20 21:50:41 -07001889 max_phys_bits = 47;
1890 break;
David S. Miller7c0fa0f2014-09-24 21:49:29 -07001891 case SUN4V_CHIP_SPARC_M7:
Khalid Azizc5b8b5b2016-04-19 11:12:54 -06001892 case SUN4V_CHIP_SPARC_SN:
David S. Miller7c0fa0f2014-09-24 21:49:29 -07001893 default:
1894 /* M7 and later support 52-bit virtual addresses. */
1895 sparc64_va_hole_top = 0xfff8000000000000UL;
1896 sparc64_va_hole_bottom = 0x0008000000000000UL;
1897 max_phys_bits = 49;
1898 break;
David S. Millerb2d43832013-09-20 21:50:41 -07001899 }
1900 }
1901
1902 if (max_phys_bits > MAX_PHYS_ADDRESS_BITS) {
1903 prom_printf("MAX_PHYS_ADDRESS_BITS is too small, need %lu\n",
1904 max_phys_bits);
1905 prom_halt();
1906 }
1907
David S. Millerbb4e6e82014-09-27 11:05:21 -07001908 PAGE_OFFSET = sparc64_va_hole_top;
1909 VMALLOC_END = ((sparc64_va_hole_bottom >> 1) +
1910 (sparc64_va_hole_bottom >> 2));
David S. Millerb2d43832013-09-20 21:50:41 -07001911
David S. Millerbb4e6e82014-09-27 11:05:21 -07001912 pr_info("MM: PAGE_OFFSET is 0x%016lx (max_phys_bits == %lu)\n",
David S. Millerb2d43832013-09-20 21:50:41 -07001913 PAGE_OFFSET, max_phys_bits);
David S. Millerbb4e6e82014-09-27 11:05:21 -07001914 pr_info("MM: VMALLOC [0x%016lx --> 0x%016lx]\n",
1915 VMALLOC_START, VMALLOC_END);
1916 pr_info("MM: VMEMMAP [0x%016lx --> 0x%016lx]\n",
1917 VMEMMAP_BASE, VMEMMAP_BASE << 1);
David S. Millerb2d43832013-09-20 21:50:41 -07001918}
1919
David S. Miller517af332006-02-01 15:55:21 -08001920static void __init tsb_phys_patch(void)
1921{
David S. Millerd257d5d2006-02-06 23:44:37 -08001922 struct tsb_ldquad_phys_patch_entry *pquad;
David S. Miller517af332006-02-01 15:55:21 -08001923 struct tsb_phys_patch_entry *p;
1924
David S. Millerd257d5d2006-02-06 23:44:37 -08001925 pquad = &__tsb_ldquad_phys_patch;
1926 while (pquad < &__tsb_ldquad_phys_patch_end) {
1927 unsigned long addr = pquad->addr;
1928
1929 if (tlb_type == hypervisor)
1930 *(unsigned int *) addr = pquad->sun4v_insn;
1931 else
1932 *(unsigned int *) addr = pquad->sun4u_insn;
1933 wmb();
1934 __asm__ __volatile__("flush %0"
1935 : /* no outputs */
1936 : "r" (addr));
1937
1938 pquad++;
1939 }
1940
David S. Miller517af332006-02-01 15:55:21 -08001941 p = &__tsb_phys_patch;
1942 while (p < &__tsb_phys_patch_end) {
1943 unsigned long addr = p->addr;
1944
1945 *(unsigned int *) addr = p->insn;
1946 wmb();
1947 __asm__ __volatile__("flush %0"
1948 : /* no outputs */
1949 : "r" (addr));
1950
1951 p++;
1952 }
1953}
1954
David S. Miller490384e2006-02-11 14:41:18 -08001955/* Don't mark as init, we give this to the Hypervisor. */
David S. Millerd1acb422007-03-16 17:20:28 -07001956#ifndef CONFIG_DEBUG_PAGEALLOC
1957#define NUM_KTSB_DESCR 2
1958#else
1959#define NUM_KTSB_DESCR 1
1960#endif
1961static struct hv_tsb_descr ktsb_descr[NUM_KTSB_DESCR];
David S. Miller490384e2006-02-11 14:41:18 -08001962
David S. Miller8c82dc02014-09-17 10:14:56 -07001963/* The swapper TSBs are loaded with a base sequence of:
1964 *
1965 * sethi %uhi(SYMBOL), REG1
1966 * sethi %hi(SYMBOL), REG2
1967 * or REG1, %ulo(SYMBOL), REG1
1968 * or REG2, %lo(SYMBOL), REG2
1969 * sllx REG1, 32, REG1
1970 * or REG1, REG2, REG1
1971 *
1972 * When we use physical addressing for the TSB accesses, we patch the
1973 * first four instructions in the above sequence.
1974 */
1975
David S. Miller9076d0e2011-08-05 00:53:57 -07001976static void patch_one_ktsb_phys(unsigned int *start, unsigned int *end, unsigned long pa)
1977{
David S. Miller8c82dc02014-09-17 10:14:56 -07001978 unsigned long high_bits, low_bits;
1979
1980 high_bits = (pa >> 32) & 0xffffffff;
1981 low_bits = (pa >> 0) & 0xffffffff;
David S. Miller9076d0e2011-08-05 00:53:57 -07001982
1983 while (start < end) {
1984 unsigned int *ia = (unsigned int *)(unsigned long)*start;
1985
David S. Miller8c82dc02014-09-17 10:14:56 -07001986 ia[0] = (ia[0] & ~0x3fffff) | (high_bits >> 10);
David S. Miller9076d0e2011-08-05 00:53:57 -07001987 __asm__ __volatile__("flush %0" : : "r" (ia));
1988
David S. Miller8c82dc02014-09-17 10:14:56 -07001989 ia[1] = (ia[1] & ~0x3fffff) | (low_bits >> 10);
David S. Miller9076d0e2011-08-05 00:53:57 -07001990 __asm__ __volatile__("flush %0" : : "r" (ia + 1));
1991
David S. Miller8c82dc02014-09-17 10:14:56 -07001992 ia[2] = (ia[2] & ~0x1fff) | (high_bits & 0x3ff);
1993 __asm__ __volatile__("flush %0" : : "r" (ia + 2));
1994
1995 ia[3] = (ia[3] & ~0x1fff) | (low_bits & 0x3ff);
1996 __asm__ __volatile__("flush %0" : : "r" (ia + 3));
1997
David S. Miller9076d0e2011-08-05 00:53:57 -07001998 start++;
1999 }
2000}
2001
2002static void ktsb_phys_patch(void)
2003{
2004 extern unsigned int __swapper_tsb_phys_patch;
2005 extern unsigned int __swapper_tsb_phys_patch_end;
David S. Miller9076d0e2011-08-05 00:53:57 -07002006 unsigned long ktsb_pa;
2007
2008 ktsb_pa = kern_base + ((unsigned long)&swapper_tsb[0] - KERNBASE);
2009 patch_one_ktsb_phys(&__swapper_tsb_phys_patch,
2010 &__swapper_tsb_phys_patch_end, ktsb_pa);
2011#ifndef CONFIG_DEBUG_PAGEALLOC
David S. Miller0785a8e2011-08-06 05:26:35 -07002012 {
2013 extern unsigned int __swapper_4m_tsb_phys_patch;
2014 extern unsigned int __swapper_4m_tsb_phys_patch_end;
David S. Miller9076d0e2011-08-05 00:53:57 -07002015 ktsb_pa = (kern_base +
2016 ((unsigned long)&swapper_4m_tsb[0] - KERNBASE));
2017 patch_one_ktsb_phys(&__swapper_4m_tsb_phys_patch,
2018 &__swapper_4m_tsb_phys_patch_end, ktsb_pa);
David S. Miller0785a8e2011-08-06 05:26:35 -07002019 }
David S. Miller9076d0e2011-08-05 00:53:57 -07002020#endif
2021}
2022
David S. Miller490384e2006-02-11 14:41:18 -08002023static void __init sun4v_ktsb_init(void)
2024{
2025 unsigned long ktsb_pa;
2026
David S. Millerd7744a02006-02-21 22:31:11 -08002027 /* First KTSB for PAGE_SIZE mappings. */
David S. Miller490384e2006-02-11 14:41:18 -08002028 ktsb_pa = kern_base + ((unsigned long)&swapper_tsb[0] - KERNBASE);
2029
2030 switch (PAGE_SIZE) {
2031 case 8 * 1024:
2032 default:
2033 ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_8K;
2034 ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_8K;
2035 break;
2036
2037 case 64 * 1024:
2038 ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_64K;
2039 ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_64K;
2040 break;
2041
2042 case 512 * 1024:
2043 ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_512K;
2044 ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_512K;
2045 break;
2046
2047 case 4 * 1024 * 1024:
2048 ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_4MB;
2049 ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_4MB;
2050 break;
Joe Perches6cb79b32011-06-03 14:45:23 +00002051 }
David S. Miller490384e2006-02-11 14:41:18 -08002052
David S. Miller3f19a842006-02-17 12:03:20 -08002053 ktsb_descr[0].assoc = 1;
David S. Miller490384e2006-02-11 14:41:18 -08002054 ktsb_descr[0].num_ttes = KERNEL_TSB_NENTRIES;
2055 ktsb_descr[0].ctx_idx = 0;
2056 ktsb_descr[0].tsb_base = ktsb_pa;
2057 ktsb_descr[0].resv = 0;
2058
David S. Millerd1acb422007-03-16 17:20:28 -07002059#ifndef CONFIG_DEBUG_PAGEALLOC
David S. Miller4f93d212012-09-06 18:13:58 -07002060 /* Second KTSB for 4MB/256MB/2GB/16GB mappings. */
David S. Millerd7744a02006-02-21 22:31:11 -08002061 ktsb_pa = (kern_base +
2062 ((unsigned long)&swapper_4m_tsb[0] - KERNBASE));
2063
2064 ktsb_descr[1].pgsz_idx = HV_PGSZ_IDX_4MB;
David S. Millerc69ad0a2012-09-06 20:35:36 -07002065 ktsb_descr[1].pgsz_mask = ((HV_PGSZ_MASK_4MB |
2066 HV_PGSZ_MASK_256MB |
2067 HV_PGSZ_MASK_2GB |
2068 HV_PGSZ_MASK_16GB) &
2069 cpu_pgsz_mask);
David S. Millerd7744a02006-02-21 22:31:11 -08002070 ktsb_descr[1].assoc = 1;
2071 ktsb_descr[1].num_ttes = KERNEL_TSB4M_NENTRIES;
2072 ktsb_descr[1].ctx_idx = 0;
2073 ktsb_descr[1].tsb_base = ktsb_pa;
2074 ktsb_descr[1].resv = 0;
David S. Millerd1acb422007-03-16 17:20:28 -07002075#endif
David S. Miller490384e2006-02-11 14:41:18 -08002076}
2077
Paul Gortmaker2066aad2013-06-17 15:43:14 -04002078void sun4v_ktsb_register(void)
David S. Miller490384e2006-02-11 14:41:18 -08002079{
David S. Miller7db35f32007-05-29 02:22:14 -07002080 unsigned long pa, ret;
David S. Miller490384e2006-02-11 14:41:18 -08002081
2082 pa = kern_base + ((unsigned long)&ktsb_descr[0] - KERNBASE);
2083
David S. Miller7db35f32007-05-29 02:22:14 -07002084 ret = sun4v_mmu_tsb_ctx0(NUM_KTSB_DESCR, pa);
2085 if (ret != 0) {
2086 prom_printf("hypervisor_mmu_tsb_ctx0[%lx]: "
2087 "errors with %lx\n", pa, ret);
2088 prom_halt();
2089 }
David S. Miller490384e2006-02-11 14:41:18 -08002090}
2091
David S. Millerc69ad0a2012-09-06 20:35:36 -07002092static void __init sun4u_linear_pte_xor_finalize(void)
2093{
2094#ifndef CONFIG_DEBUG_PAGEALLOC
2095 /* This is where we would add Panther support for
2096 * 32MB and 256MB pages.
2097 */
2098#endif
2099}
2100
2101static void __init sun4v_linear_pte_xor_finalize(void)
2102{
Khalid Aziz494e5b62015-05-27 10:00:46 -06002103 unsigned long pagecv_flag;
2104
2105 /* Bit 9 of TTE is no longer CV bit on M7 processor and it instead
2106 * enables MCD error. Do not set bit 9 on M7 processor.
2107 */
2108 switch (sun4v_chip_type) {
2109 case SUN4V_CHIP_SPARC_M7:
Khalid Azizc5b8b5b2016-04-19 11:12:54 -06002110 case SUN4V_CHIP_SPARC_SN:
Khalid Aziz494e5b62015-05-27 10:00:46 -06002111 pagecv_flag = 0x00;
2112 break;
2113 default:
2114 pagecv_flag = _PAGE_CV_4V;
2115 break;
2116 }
David S. Millerc69ad0a2012-09-06 20:35:36 -07002117#ifndef CONFIG_DEBUG_PAGEALLOC
2118 if (cpu_pgsz_mask & HV_PGSZ_MASK_256MB) {
2119 kern_linear_pte_xor[1] = (_PAGE_VALID | _PAGE_SZ256MB_4V) ^
David S. Miller922631b2013-09-18 12:00:00 -07002120 PAGE_OFFSET;
Khalid Aziz494e5b62015-05-27 10:00:46 -06002121 kern_linear_pte_xor[1] |= (_PAGE_CP_4V | pagecv_flag |
David S. Millerc69ad0a2012-09-06 20:35:36 -07002122 _PAGE_P_4V | _PAGE_W_4V);
2123 } else {
2124 kern_linear_pte_xor[1] = kern_linear_pte_xor[0];
2125 }
2126
2127 if (cpu_pgsz_mask & HV_PGSZ_MASK_2GB) {
2128 kern_linear_pte_xor[2] = (_PAGE_VALID | _PAGE_SZ2GB_4V) ^
David S. Miller922631b2013-09-18 12:00:00 -07002129 PAGE_OFFSET;
Khalid Aziz494e5b62015-05-27 10:00:46 -06002130 kern_linear_pte_xor[2] |= (_PAGE_CP_4V | pagecv_flag |
David S. Millerc69ad0a2012-09-06 20:35:36 -07002131 _PAGE_P_4V | _PAGE_W_4V);
2132 } else {
2133 kern_linear_pte_xor[2] = kern_linear_pte_xor[1];
2134 }
2135
2136 if (cpu_pgsz_mask & HV_PGSZ_MASK_16GB) {
2137 kern_linear_pte_xor[3] = (_PAGE_VALID | _PAGE_SZ16GB_4V) ^
David S. Miller922631b2013-09-18 12:00:00 -07002138 PAGE_OFFSET;
Khalid Aziz494e5b62015-05-27 10:00:46 -06002139 kern_linear_pte_xor[3] |= (_PAGE_CP_4V | pagecv_flag |
David S. Millerc69ad0a2012-09-06 20:35:36 -07002140 _PAGE_P_4V | _PAGE_W_4V);
2141 } else {
2142 kern_linear_pte_xor[3] = kern_linear_pte_xor[2];
2143 }
2144#endif
2145}
2146
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147/* paging_init() sets up the page tables */
2148
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149static unsigned long last_valid_pfn;
David S. Millerac55c762014-09-26 21:19:46 -07002150
David S. Millerc4bce902006-02-11 21:57:54 -08002151static void sun4u_pgprot_init(void);
2152static void sun4v_pgprot_init(void);
2153
bob picco7c21d532014-09-16 09:29:54 -04002154static phys_addr_t __init available_memory(void)
2155{
2156 phys_addr_t available = 0ULL;
2157 phys_addr_t pa_start, pa_end;
2158 u64 i;
2159
Tony Luckfc6daaf2015-06-24 16:58:09 -07002160 for_each_free_mem_range(i, NUMA_NO_NODE, MEMBLOCK_NONE, &pa_start,
2161 &pa_end, NULL)
bob picco7c21d532014-09-16 09:29:54 -04002162 available = available + (pa_end - pa_start);
2163
2164 return available;
2165}
2166
Khalid Aziz494e5b62015-05-27 10:00:46 -06002167#define _PAGE_CACHE_4U (_PAGE_CP_4U | _PAGE_CV_4U)
2168#define _PAGE_CACHE_4V (_PAGE_CP_4V | _PAGE_CV_4V)
2169#define __DIRTY_BITS_4U (_PAGE_MODIFIED_4U | _PAGE_WRITE_4U | _PAGE_W_4U)
2170#define __DIRTY_BITS_4V (_PAGE_MODIFIED_4V | _PAGE_WRITE_4V | _PAGE_W_4V)
2171#define __ACCESS_BITS_4U (_PAGE_ACCESSED_4U | _PAGE_READ_4U | _PAGE_R)
2172#define __ACCESS_BITS_4V (_PAGE_ACCESSED_4V | _PAGE_READ_4V | _PAGE_R)
2173
bob picco7c21d532014-09-16 09:29:54 -04002174/* We need to exclude reserved regions. This exclusion will include
2175 * vmlinux and initrd. To be more precise the initrd size could be used to
2176 * compute a new lower limit because it is freed later during initialization.
2177 */
2178static void __init reduce_memory(phys_addr_t limit_ram)
2179{
2180 phys_addr_t avail_ram = available_memory();
2181 phys_addr_t pa_start, pa_end;
2182 u64 i;
2183
2184 if (limit_ram >= avail_ram)
2185 return;
2186
Tony Luckfc6daaf2015-06-24 16:58:09 -07002187 for_each_free_mem_range(i, NUMA_NO_NODE, MEMBLOCK_NONE, &pa_start,
2188 &pa_end, NULL) {
bob picco7c21d532014-09-16 09:29:54 -04002189 phys_addr_t region_size = pa_end - pa_start;
2190 phys_addr_t clip_start = pa_start;
2191
2192 avail_ram = avail_ram - region_size;
2193 /* Are we consuming too much? */
2194 if (avail_ram < limit_ram) {
2195 phys_addr_t give_back = limit_ram - avail_ram;
2196
2197 region_size = region_size - give_back;
2198 clip_start = clip_start + give_back;
2199 }
2200
2201 memblock_remove(clip_start, region_size);
2202
2203 if (avail_ram <= limit_ram)
2204 break;
2205 i = 0UL;
2206 }
2207}
2208
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209void __init paging_init(void)
2210{
David S. Miller919ee672008-04-23 05:40:25 -07002211 unsigned long end_pfn, shift, phys_base;
David S. Miller0836a0e2005-09-28 21:38:08 -07002212 unsigned long real_end, i;
2213
David S. Millerb2d43832013-09-20 21:50:41 -07002214 setup_page_offset();
2215
David S. Miller22adb352007-05-26 01:14:43 -07002216 /* These build time checkes make sure that the dcache_dirty_cpu()
2217 * page->flags usage will work.
2218 *
2219 * When a page gets marked as dcache-dirty, we store the
2220 * cpu number starting at bit 32 in the page->flags. Also,
2221 * functions like clear_dcache_dirty_cpu use the cpu mask
2222 * in 13-bit signed-immediate instruction fields.
2223 */
Christoph Lameter9223b4192008-04-28 02:12:48 -07002224
2225 /*
2226 * Page flags must not reach into upper 32 bits that are used
2227 * for the cpu number
2228 */
2229 BUILD_BUG_ON(NR_PAGEFLAGS > 32);
2230
2231 /*
2232 * The bit fields placed in the high range must not reach below
2233 * the 32 bit boundary. Otherwise we cannot place the cpu field
2234 * at the 32 bit boundary.
2235 */
David S. Miller22adb352007-05-26 01:14:43 -07002236 BUILD_BUG_ON(SECTIONS_WIDTH + NODES_WIDTH + ZONES_WIDTH +
Christoph Lameter9223b4192008-04-28 02:12:48 -07002237 ilog2(roundup_pow_of_two(NR_CPUS)) > 32);
2238
David S. Miller22adb352007-05-26 01:14:43 -07002239 BUILD_BUG_ON(NR_CPUS > 4096);
2240
David S. Miller0eef3312014-05-03 22:52:50 -07002241 kern_base = (prom_boot_mapping_phys_low >> ILOG2_4MB) << ILOG2_4MB;
David S. Miller481295f2006-02-07 21:51:08 -08002242 kern_size = (unsigned long)&_end - (unsigned long)KERNBASE;
2243
David S. Millerd7744a02006-02-21 22:31:11 -08002244 /* Invalidate both kernel TSBs. */
David S. Miller8b234272006-02-17 18:01:02 -08002245 memset(swapper_tsb, 0x40, sizeof(swapper_tsb));
David S. Millerd1acb422007-03-16 17:20:28 -07002246#ifndef CONFIG_DEBUG_PAGEALLOC
David S. Millerd7744a02006-02-21 22:31:11 -08002247 memset(swapper_4m_tsb, 0x40, sizeof(swapper_4m_tsb));
David S. Millerd1acb422007-03-16 17:20:28 -07002248#endif
David S. Miller8b234272006-02-17 18:01:02 -08002249
Khalid Aziz494e5b62015-05-27 10:00:46 -06002250 /* TTE.cv bit on sparc v9 occupies the same position as TTE.mcde
2251 * bit on M7 processor. This is a conflicting usage of the same
2252 * bit. Enabling TTE.cv on M7 would turn on Memory Corruption
2253 * Detection error on all pages and this will lead to problems
2254 * later. Kernel does not run with MCD enabled and hence rest
2255 * of the required steps to fully configure memory corruption
2256 * detection are not taken. We need to ensure TTE.mcde is not
2257 * set on M7 processor. Compute the value of cacheability
2258 * flag for use later taking this into consideration.
2259 */
2260 switch (sun4v_chip_type) {
2261 case SUN4V_CHIP_SPARC_M7:
Khalid Azizc5b8b5b2016-04-19 11:12:54 -06002262 case SUN4V_CHIP_SPARC_SN:
Khalid Aziz494e5b62015-05-27 10:00:46 -06002263 page_cache4v_flag = _PAGE_CP_4V;
2264 break;
2265 default:
2266 page_cache4v_flag = _PAGE_CACHE_4V;
2267 break;
2268 }
2269
David S. Millerc4bce902006-02-11 21:57:54 -08002270 if (tlb_type == hypervisor)
2271 sun4v_pgprot_init();
2272 else
2273 sun4u_pgprot_init();
2274
David S. Millerd257d5d2006-02-06 23:44:37 -08002275 if (tlb_type == cheetah_plus ||
David S. Miller9076d0e2011-08-05 00:53:57 -07002276 tlb_type == hypervisor) {
David S. Miller517af332006-02-01 15:55:21 -08002277 tsb_phys_patch();
David S. Miller9076d0e2011-08-05 00:53:57 -07002278 ktsb_phys_patch();
2279 }
David S. Miller517af332006-02-01 15:55:21 -08002280
David S. Millerc69ad0a2012-09-06 20:35:36 -07002281 if (tlb_type == hypervisor)
David S. Millerd257d5d2006-02-06 23:44:37 -08002282 sun4v_patch_tlb_handlers();
2283
David S. Millera94a1722008-05-11 21:04:48 -07002284 /* Find available physical memory...
2285 *
2286 * Read it twice in order to work around a bug in openfirmware.
2287 * The call to grab this table itself can cause openfirmware to
2288 * allocate memory, which in turn can take away some space from
2289 * the list of available memory. Reading it twice makes sure
2290 * we really do get the final value.
2291 */
2292 read_obp_translations();
2293 read_obp_memory("reg", &pall[0], &pall_ents);
2294 read_obp_memory("available", &pavail[0], &pavail_ents);
David S. Miller13edad72005-09-29 17:58:26 -07002295 read_obp_memory("available", &pavail[0], &pavail_ents);
David S. Miller0836a0e2005-09-28 21:38:08 -07002296
2297 phys_base = 0xffffffffffffffffUL;
David S. Miller3b2a7e22008-02-13 18:13:20 -08002298 for (i = 0; i < pavail_ents; i++) {
David S. Miller13edad72005-09-29 17:58:26 -07002299 phys_base = min(phys_base, pavail[i].phys_addr);
Yinghai Lu95f72d12010-07-12 14:36:09 +10002300 memblock_add(pavail[i].phys_addr, pavail[i].reg_size);
David S. Miller3b2a7e22008-02-13 18:13:20 -08002301 }
2302
Yinghai Lu95f72d12010-07-12 14:36:09 +10002303 memblock_reserve(kern_base, kern_size);
David S. Miller0836a0e2005-09-28 21:38:08 -07002304
David S. Miller4e82c9a2008-02-13 18:00:03 -08002305 find_ramdisk(phys_base);
2306
bob picco7c21d532014-09-16 09:29:54 -04002307 if (cmdline_memory_size)
2308 reduce_memory(cmdline_memory_size);
David S. Miller25b0c652008-02-13 18:20:14 -08002309
Tejun Heo1aadc052011-12-08 10:22:08 -08002310 memblock_allow_resize();
Yinghai Lu95f72d12010-07-12 14:36:09 +10002311 memblock_dump_all();
David S. Miller3b2a7e22008-02-13 18:13:20 -08002312
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 set_bit(0, mmu_context_bmap);
2314
David S. Miller2bdb3cb2005-09-22 01:08:57 -07002315 shift = kern_base + PAGE_OFFSET - ((unsigned long)KERNBASE);
2316
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 real_end = (unsigned long)_end;
David S. Miller0eef3312014-05-03 22:52:50 -07002318 num_kernel_image_mappings = DIV_ROUND_UP(real_end - KERNBASE, 1 << ILOG2_4MB);
David S. Miller64658742008-03-21 17:01:38 -07002319 printk("Kernel: Using %d locked TLB entries for main kernel image.\n",
2320 num_kernel_image_mappings);
David S. Miller2bdb3cb2005-09-22 01:08:57 -07002321
2322 /* Set kernel pgd to upper alias so physical page computations
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 * work.
2324 */
2325 init_mm.pgd += ((shift) / (sizeof(pgd_t)));
2326
David S. Millerd195b712014-09-27 21:30:57 -07002327 memset(swapper_pg_dir, 0, sizeof(swapper_pg_dir));
David S. Miller0dd5b7b2014-09-24 20:56:11 -07002328
David S. Millerc9c10832005-10-12 12:22:46 -07002329 inherit_prom_mappings();
David S. Miller5085b4a2005-09-22 00:45:41 -07002330
David S. Millera8b900d2006-01-31 18:33:37 -08002331 /* Ok, we can use our TLB miss and window trap handlers safely. */
2332 setup_tba();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333
David S. Millerc9c10832005-10-12 12:22:46 -07002334 __flush_tlb_all();
David S. Miller9ad98c52005-10-05 15:12:00 -07002335
David S. Millerad072002008-02-13 19:21:51 -08002336 prom_build_devicetree();
David S. Millerb696fdc2009-05-26 22:37:25 -07002337 of_populate_present_mask();
David S. Millerb99c6eb2009-06-18 01:44:19 -07002338#ifndef CONFIG_SMP
2339 of_fill_in_cpu_data();
2340#endif
David S. Millerad072002008-02-13 19:21:51 -08002341
David S. Miller890db402009-04-01 03:13:15 -07002342 if (tlb_type == hypervisor) {
David S. Miller4a283332008-02-13 19:22:23 -08002343 sun4v_mdesc_init();
Stephen Rothwell6ac5c612009-06-15 03:06:18 -07002344 mdesc_populate_present_mask(cpu_all_mask);
David S. Millerb99c6eb2009-06-18 01:44:19 -07002345#ifndef CONFIG_SMP
2346 mdesc_fill_in_cpu_data(cpu_all_mask);
2347#endif
David S. Millerce33fdc2012-09-06 19:01:25 -07002348 mdesc_get_page_sizes(cpu_all_mask, &cpu_pgsz_mask);
David S. Millerc69ad0a2012-09-06 20:35:36 -07002349
2350 sun4v_linear_pte_xor_finalize();
2351
2352 sun4v_ktsb_init();
2353 sun4v_ktsb_register();
David S. Millerce33fdc2012-09-06 19:01:25 -07002354 } else {
2355 unsigned long impl, ver;
2356
2357 cpu_pgsz_mask = (HV_PGSZ_MASK_8K | HV_PGSZ_MASK_64K |
2358 HV_PGSZ_MASK_512K | HV_PGSZ_MASK_4MB);
2359
2360 __asm__ __volatile__("rdpr %%ver, %0" : "=r" (ver));
2361 impl = ((ver >> 32) & 0xffff);
2362 if (impl == PANTHER_IMPL)
2363 cpu_pgsz_mask |= (HV_PGSZ_MASK_32MB |
2364 HV_PGSZ_MASK_256MB);
David S. Millerc69ad0a2012-09-06 20:35:36 -07002365
2366 sun4u_linear_pte_xor_finalize();
David S. Miller890db402009-04-01 03:13:15 -07002367 }
David S. Miller4a283332008-02-13 19:22:23 -08002368
David S. Millerc69ad0a2012-09-06 20:35:36 -07002369 /* Flush the TLBs and the 4M TSB so that the updated linear
2370 * pte XOR settings are realized for all mappings.
2371 */
2372 __flush_tlb_all();
2373#ifndef CONFIG_DEBUG_PAGEALLOC
2374 memset(swapper_4m_tsb, 0x40, sizeof(swapper_4m_tsb));
2375#endif
2376 __flush_tlb_all();
2377
David S. Miller2bdb3cb2005-09-22 01:08:57 -07002378 /* Setup bootmem... */
David S. Miller919ee672008-04-23 05:40:25 -07002379 last_valid_pfn = end_pfn = bootmem_init(phys_base);
David S. Millerd1112012006-03-08 02:16:07 -08002380
David S. Miller56425302005-09-25 16:46:57 -07002381 kernel_physical_mapping_init();
David S. Miller56425302005-09-25 16:46:57 -07002382
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 {
David S. Miller919ee672008-04-23 05:40:25 -07002384 unsigned long max_zone_pfns[MAX_NR_ZONES];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385
David S. Miller919ee672008-04-23 05:40:25 -07002386 memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387
David S. Miller919ee672008-04-23 05:40:25 -07002388 max_zone_pfns[ZONE_NORMAL] = end_pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389
David S. Miller919ee672008-04-23 05:40:25 -07002390 free_area_init_nodes(max_zone_pfns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 }
2392
David S. Miller3c62a2d2008-02-17 23:22:50 -08002393 printk("Booting Linux...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394}
2395
Greg Kroah-Hartman7c9503b2012-12-21 14:03:26 -08002396int page_in_phys_avail(unsigned long paddr)
David S. Miller919ee672008-04-23 05:40:25 -07002397{
2398 int i;
2399
2400 paddr &= PAGE_MASK;
2401
2402 for (i = 0; i < pavail_ents; i++) {
2403 unsigned long start, end;
2404
2405 start = pavail[i].phys_addr;
2406 end = start + pavail[i].reg_size;
2407
2408 if (paddr >= start && paddr < end)
2409 return 1;
2410 }
2411 if (paddr >= kern_base && paddr < (kern_base + kern_size))
2412 return 1;
2413#ifdef CONFIG_BLK_DEV_INITRD
2414 if (paddr >= __pa(initrd_start) &&
2415 paddr < __pa(PAGE_ALIGN(initrd_end)))
2416 return 1;
2417#endif
2418
2419 return 0;
2420}
2421
Yinghai Lu961f8fa2012-11-16 19:39:21 -08002422static void __init register_page_bootmem_info(void)
2423{
2424#ifdef CONFIG_NEED_MULTIPLE_NODES
2425 int i;
2426
2427 for_each_online_node(i)
2428 if (NODE_DATA(i)->node_spanned_pages)
2429 register_page_bootmem_info_node(NODE_DATA(i));
2430#endif
2431}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432void __init mem_init(void)
2433{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 high_memory = __va(last_valid_pfn << PAGE_SHIFT);
2435
Yinghai Lu961f8fa2012-11-16 19:39:21 -08002436 register_page_bootmem_info();
Jiang Liu0c988532013-07-03 15:03:24 -07002437 free_all_bootmem();
David S. Miller919ee672008-04-23 05:40:25 -07002438
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 /*
2440 * Set up the zero page, mark it reserved, so that page count
2441 * is not manipulated when freeing the page from user ptes.
2442 */
2443 mem_map_zero = alloc_pages(GFP_KERNEL|__GFP_ZERO, 0);
2444 if (mem_map_zero == NULL) {
2445 prom_printf("paging_init: Cannot alloc zero page.\n");
2446 prom_halt();
2447 }
Jiang Liu70affe42013-05-07 16:18:08 -07002448 mark_page_reserved(mem_map_zero);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449
Jiang Liudceccbe2013-07-03 15:04:14 -07002450 mem_init_print_info(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451
2452 if (tlb_type == cheetah || tlb_type == cheetah_plus)
2453 cheetah_ecache_flush_init();
2454}
2455
David S. Miller898cf0e2005-09-23 11:59:44 -07002456void free_initmem(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457{
2458 unsigned long addr, initend;
David S. Millerf2b60792008-08-14 01:45:41 -07002459 int do_free = 1;
2460
2461 /* If the physical memory maps were trimmed by kernel command
2462 * line options, don't even try freeing this initmem stuff up.
2463 * The kernel image could have been in the trimmed out region
2464 * and if so the freeing below will free invalid page structs.
2465 */
2466 if (cmdline_memory_size)
2467 do_free = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468
2469 /*
2470 * The init section is aligned to 8k in vmlinux.lds. Page align for >8k pagesizes.
2471 */
2472 addr = PAGE_ALIGN((unsigned long)(__init_begin));
2473 initend = (unsigned long)(__init_end) & PAGE_MASK;
2474 for (; addr < initend; addr += PAGE_SIZE) {
2475 unsigned long page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476
2477 page = (addr +
2478 ((unsigned long) __va(kern_base)) -
2479 ((unsigned long) KERNBASE));
Randy Dunlapc9cf5522006-06-27 02:53:52 -07002480 memset((void *)addr, POISON_FREE_INITMEM, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481
Jiang Liu70affe42013-05-07 16:18:08 -07002482 if (do_free)
2483 free_reserved_page(virt_to_page(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 }
2485}
2486
2487#ifdef CONFIG_BLK_DEV_INITRD
2488void free_initrd_mem(unsigned long start, unsigned long end)
2489{
Jiang Liudceccbe2013-07-03 15:04:14 -07002490 free_reserved_area((void *)start, (void *)end, POISON_FREE_INITMEM,
2491 "initrd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492}
2493#endif
David S. Millerc4bce902006-02-11 21:57:54 -08002494
David S. Millerc4bce902006-02-11 21:57:54 -08002495pgprot_t PAGE_KERNEL __read_mostly;
2496EXPORT_SYMBOL(PAGE_KERNEL);
2497
2498pgprot_t PAGE_KERNEL_LOCKED __read_mostly;
2499pgprot_t PAGE_COPY __read_mostly;
David S. Miller0f159522006-02-18 12:43:16 -08002500
2501pgprot_t PAGE_SHARED __read_mostly;
2502EXPORT_SYMBOL(PAGE_SHARED);
2503
David S. Millerc4bce902006-02-11 21:57:54 -08002504unsigned long pg_iobits __read_mostly;
2505
2506unsigned long _PAGE_IE __read_mostly;
David S. Miller987c74f2006-06-25 01:34:43 -07002507EXPORT_SYMBOL(_PAGE_IE);
David S. Millerb2bef442006-02-23 01:55:55 -08002508
David S. Millerc4bce902006-02-11 21:57:54 -08002509unsigned long _PAGE_E __read_mostly;
David S. Millerb2bef442006-02-23 01:55:55 -08002510EXPORT_SYMBOL(_PAGE_E);
2511
David S. Millerc4bce902006-02-11 21:57:54 -08002512unsigned long _PAGE_CACHE __read_mostly;
David S. Millerb2bef442006-02-23 01:55:55 -08002513EXPORT_SYMBOL(_PAGE_CACHE);
David S. Millerc4bce902006-02-11 21:57:54 -08002514
David Miller46644c22007-10-16 01:24:16 -07002515#ifdef CONFIG_SPARSEMEM_VMEMMAP
Johannes Weiner0aad8182013-04-29 15:07:50 -07002516int __meminit vmemmap_populate(unsigned long vstart, unsigned long vend,
2517 int node)
David Miller46644c22007-10-16 01:24:16 -07002518{
David Miller46644c22007-10-16 01:24:16 -07002519 unsigned long pte_base;
2520
2521 pte_base = (_PAGE_VALID | _PAGE_SZ4MB_4U |
2522 _PAGE_CP_4U | _PAGE_CV_4U |
2523 _PAGE_P_4U | _PAGE_W_4U);
2524 if (tlb_type == hypervisor)
2525 pte_base = (_PAGE_VALID | _PAGE_SZ4MB_4V |
Khalid Aziz494e5b62015-05-27 10:00:46 -06002526 page_cache4v_flag | _PAGE_P_4V | _PAGE_W_4V);
David Miller46644c22007-10-16 01:24:16 -07002527
David S. Millerc06240c2014-09-24 21:20:14 -07002528 pte_base |= _PAGE_PMD_HUGE;
David Miller46644c22007-10-16 01:24:16 -07002529
David S. Millerc06240c2014-09-24 21:20:14 -07002530 vstart = vstart & PMD_MASK;
2531 vend = ALIGN(vend, PMD_SIZE);
2532 for (; vstart < vend; vstart += PMD_SIZE) {
2533 pgd_t *pgd = pgd_offset_k(vstart);
2534 unsigned long pte;
2535 pud_t *pud;
2536 pmd_t *pmd;
2537
2538 if (pgd_none(*pgd)) {
2539 pud_t *new = vmemmap_alloc_block(PAGE_SIZE, node);
2540
2541 if (!new)
2542 return -ENOMEM;
2543 pgd_populate(&init_mm, pgd, new);
2544 }
2545
2546 pud = pud_offset(pgd, vstart);
2547 if (pud_none(*pud)) {
2548 pmd_t *new = vmemmap_alloc_block(PAGE_SIZE, node);
2549
2550 if (!new)
2551 return -ENOMEM;
2552 pud_populate(&init_mm, pud, new);
2553 }
2554
2555 pmd = pmd_offset(pud, vstart);
2556
2557 pte = pmd_val(*pmd);
2558 if (!(pte & _PAGE_VALID)) {
2559 void *block = vmemmap_alloc_block(PMD_SIZE, node);
2560
David Miller46644c22007-10-16 01:24:16 -07002561 if (!block)
2562 return -ENOMEM;
2563
David S. Millerc06240c2014-09-24 21:20:14 -07002564 pmd_val(*pmd) = pte_base | __pa(block);
David Miller46644c22007-10-16 01:24:16 -07002565 }
2566 }
David S. Miller2856cc22012-08-15 00:37:29 -07002567
David S. Millerc06240c2014-09-24 21:20:14 -07002568 return 0;
David S. Miller2856cc22012-08-15 00:37:29 -07002569}
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -08002570
Johannes Weiner0aad8182013-04-29 15:07:50 -07002571void vmemmap_free(unsigned long start, unsigned long end)
Tang Chen01975182013-02-22 16:33:08 -08002572{
2573}
David Miller46644c22007-10-16 01:24:16 -07002574#endif /* CONFIG_SPARSEMEM_VMEMMAP */
2575
David S. Millerc4bce902006-02-11 21:57:54 -08002576static void prot_init_common(unsigned long page_none,
2577 unsigned long page_shared,
2578 unsigned long page_copy,
2579 unsigned long page_readonly,
2580 unsigned long page_exec_bit)
2581{
2582 PAGE_COPY = __pgprot(page_copy);
David S. Miller0f159522006-02-18 12:43:16 -08002583 PAGE_SHARED = __pgprot(page_shared);
David S. Millerc4bce902006-02-11 21:57:54 -08002584
2585 protection_map[0x0] = __pgprot(page_none);
2586 protection_map[0x1] = __pgprot(page_readonly & ~page_exec_bit);
2587 protection_map[0x2] = __pgprot(page_copy & ~page_exec_bit);
2588 protection_map[0x3] = __pgprot(page_copy & ~page_exec_bit);
2589 protection_map[0x4] = __pgprot(page_readonly);
2590 protection_map[0x5] = __pgprot(page_readonly);
2591 protection_map[0x6] = __pgprot(page_copy);
2592 protection_map[0x7] = __pgprot(page_copy);
2593 protection_map[0x8] = __pgprot(page_none);
2594 protection_map[0x9] = __pgprot(page_readonly & ~page_exec_bit);
2595 protection_map[0xa] = __pgprot(page_shared & ~page_exec_bit);
2596 protection_map[0xb] = __pgprot(page_shared & ~page_exec_bit);
2597 protection_map[0xc] = __pgprot(page_readonly);
2598 protection_map[0xd] = __pgprot(page_readonly);
2599 protection_map[0xe] = __pgprot(page_shared);
2600 protection_map[0xf] = __pgprot(page_shared);
2601}
2602
2603static void __init sun4u_pgprot_init(void)
2604{
2605 unsigned long page_none, page_shared, page_copy, page_readonly;
2606 unsigned long page_exec_bit;
David S. Miller4f93d212012-09-06 18:13:58 -07002607 int i;
David S. Millerc4bce902006-02-11 21:57:54 -08002608
2609 PAGE_KERNEL = __pgprot (_PAGE_PRESENT_4U | _PAGE_VALID |
2610 _PAGE_CACHE_4U | _PAGE_P_4U |
2611 __ACCESS_BITS_4U | __DIRTY_BITS_4U |
2612 _PAGE_EXEC_4U);
2613 PAGE_KERNEL_LOCKED = __pgprot (_PAGE_PRESENT_4U | _PAGE_VALID |
2614 _PAGE_CACHE_4U | _PAGE_P_4U |
2615 __ACCESS_BITS_4U | __DIRTY_BITS_4U |
2616 _PAGE_EXEC_4U | _PAGE_L_4U);
David S. Millerc4bce902006-02-11 21:57:54 -08002617
2618 _PAGE_IE = _PAGE_IE_4U;
2619 _PAGE_E = _PAGE_E_4U;
2620 _PAGE_CACHE = _PAGE_CACHE_4U;
2621
2622 pg_iobits = (_PAGE_VALID | _PAGE_PRESENT_4U | __DIRTY_BITS_4U |
2623 __ACCESS_BITS_4U | _PAGE_E_4U);
2624
David S. Millerd1acb422007-03-16 17:20:28 -07002625#ifdef CONFIG_DEBUG_PAGEALLOC
David S. Miller922631b2013-09-18 12:00:00 -07002626 kern_linear_pte_xor[0] = _PAGE_VALID ^ PAGE_OFFSET;
David S. Millerd1acb422007-03-16 17:20:28 -07002627#else
David S. Miller9cc3a1a2006-02-21 20:51:13 -08002628 kern_linear_pte_xor[0] = (_PAGE_VALID | _PAGE_SZ4MB_4U) ^
David S. Miller922631b2013-09-18 12:00:00 -07002629 PAGE_OFFSET;
David S. Millerd1acb422007-03-16 17:20:28 -07002630#endif
David S. Miller9cc3a1a2006-02-21 20:51:13 -08002631 kern_linear_pte_xor[0] |= (_PAGE_CP_4U | _PAGE_CV_4U |
2632 _PAGE_P_4U | _PAGE_W_4U);
2633
David S. Miller4f93d212012-09-06 18:13:58 -07002634 for (i = 1; i < 4; i++)
2635 kern_linear_pte_xor[i] = kern_linear_pte_xor[0];
David S. Millerc4bce902006-02-11 21:57:54 -08002636
David S. Millerc4bce902006-02-11 21:57:54 -08002637 _PAGE_ALL_SZ_BITS = (_PAGE_SZ4MB_4U | _PAGE_SZ512K_4U |
2638 _PAGE_SZ64K_4U | _PAGE_SZ8K_4U |
2639 _PAGE_SZ32MB_4U | _PAGE_SZ256MB_4U);
2640
2641
2642 page_none = _PAGE_PRESENT_4U | _PAGE_ACCESSED_4U | _PAGE_CACHE_4U;
2643 page_shared = (_PAGE_VALID | _PAGE_PRESENT_4U | _PAGE_CACHE_4U |
2644 __ACCESS_BITS_4U | _PAGE_WRITE_4U | _PAGE_EXEC_4U);
2645 page_copy = (_PAGE_VALID | _PAGE_PRESENT_4U | _PAGE_CACHE_4U |
2646 __ACCESS_BITS_4U | _PAGE_EXEC_4U);
2647 page_readonly = (_PAGE_VALID | _PAGE_PRESENT_4U | _PAGE_CACHE_4U |
2648 __ACCESS_BITS_4U | _PAGE_EXEC_4U);
2649
2650 page_exec_bit = _PAGE_EXEC_4U;
2651
2652 prot_init_common(page_none, page_shared, page_copy, page_readonly,
2653 page_exec_bit);
2654}
2655
2656static void __init sun4v_pgprot_init(void)
2657{
2658 unsigned long page_none, page_shared, page_copy, page_readonly;
2659 unsigned long page_exec_bit;
David S. Miller4f93d212012-09-06 18:13:58 -07002660 int i;
David S. Millerc4bce902006-02-11 21:57:54 -08002661
2662 PAGE_KERNEL = __pgprot (_PAGE_PRESENT_4V | _PAGE_VALID |
Khalid Aziz494e5b62015-05-27 10:00:46 -06002663 page_cache4v_flag | _PAGE_P_4V |
David S. Millerc4bce902006-02-11 21:57:54 -08002664 __ACCESS_BITS_4V | __DIRTY_BITS_4V |
2665 _PAGE_EXEC_4V);
2666 PAGE_KERNEL_LOCKED = PAGE_KERNEL;
David S. Millerc4bce902006-02-11 21:57:54 -08002667
2668 _PAGE_IE = _PAGE_IE_4V;
2669 _PAGE_E = _PAGE_E_4V;
Khalid Aziz494e5b62015-05-27 10:00:46 -06002670 _PAGE_CACHE = page_cache4v_flag;
David S. Millerc4bce902006-02-11 21:57:54 -08002671
David S. Millerd1acb422007-03-16 17:20:28 -07002672#ifdef CONFIG_DEBUG_PAGEALLOC
David S. Miller922631b2013-09-18 12:00:00 -07002673 kern_linear_pte_xor[0] = _PAGE_VALID ^ PAGE_OFFSET;
David S. Millerd1acb422007-03-16 17:20:28 -07002674#else
David S. Miller9cc3a1a2006-02-21 20:51:13 -08002675 kern_linear_pte_xor[0] = (_PAGE_VALID | _PAGE_SZ4MB_4V) ^
David S. Miller922631b2013-09-18 12:00:00 -07002676 PAGE_OFFSET;
David S. Millerd1acb422007-03-16 17:20:28 -07002677#endif
Khalid Aziz494e5b62015-05-27 10:00:46 -06002678 kern_linear_pte_xor[0] |= (page_cache4v_flag | _PAGE_P_4V |
2679 _PAGE_W_4V);
David S. Miller9cc3a1a2006-02-21 20:51:13 -08002680
David S. Millerc69ad0a2012-09-06 20:35:36 -07002681 for (i = 1; i < 4; i++)
2682 kern_linear_pte_xor[i] = kern_linear_pte_xor[0];
David S. Miller4f93d212012-09-06 18:13:58 -07002683
David S. Millerc4bce902006-02-11 21:57:54 -08002684 pg_iobits = (_PAGE_VALID | _PAGE_PRESENT_4V | __DIRTY_BITS_4V |
2685 __ACCESS_BITS_4V | _PAGE_E_4V);
2686
David S. Millerc4bce902006-02-11 21:57:54 -08002687 _PAGE_ALL_SZ_BITS = (_PAGE_SZ16GB_4V | _PAGE_SZ2GB_4V |
2688 _PAGE_SZ256MB_4V | _PAGE_SZ32MB_4V |
2689 _PAGE_SZ4MB_4V | _PAGE_SZ512K_4V |
2690 _PAGE_SZ64K_4V | _PAGE_SZ8K_4V);
2691
Khalid Aziz494e5b62015-05-27 10:00:46 -06002692 page_none = _PAGE_PRESENT_4V | _PAGE_ACCESSED_4V | page_cache4v_flag;
2693 page_shared = (_PAGE_VALID | _PAGE_PRESENT_4V | page_cache4v_flag |
David S. Millerc4bce902006-02-11 21:57:54 -08002694 __ACCESS_BITS_4V | _PAGE_WRITE_4V | _PAGE_EXEC_4V);
Khalid Aziz494e5b62015-05-27 10:00:46 -06002695 page_copy = (_PAGE_VALID | _PAGE_PRESENT_4V | page_cache4v_flag |
David S. Millerc4bce902006-02-11 21:57:54 -08002696 __ACCESS_BITS_4V | _PAGE_EXEC_4V);
Khalid Aziz494e5b62015-05-27 10:00:46 -06002697 page_readonly = (_PAGE_VALID | _PAGE_PRESENT_4V | page_cache4v_flag |
David S. Millerc4bce902006-02-11 21:57:54 -08002698 __ACCESS_BITS_4V | _PAGE_EXEC_4V);
2699
2700 page_exec_bit = _PAGE_EXEC_4V;
2701
2702 prot_init_common(page_none, page_shared, page_copy, page_readonly,
2703 page_exec_bit);
2704}
2705
2706unsigned long pte_sz_bits(unsigned long sz)
2707{
2708 if (tlb_type == hypervisor) {
2709 switch (sz) {
2710 case 8 * 1024:
2711 default:
2712 return _PAGE_SZ8K_4V;
2713 case 64 * 1024:
2714 return _PAGE_SZ64K_4V;
2715 case 512 * 1024:
2716 return _PAGE_SZ512K_4V;
2717 case 4 * 1024 * 1024:
2718 return _PAGE_SZ4MB_4V;
Joe Perches6cb79b32011-06-03 14:45:23 +00002719 }
David S. Millerc4bce902006-02-11 21:57:54 -08002720 } else {
2721 switch (sz) {
2722 case 8 * 1024:
2723 default:
2724 return _PAGE_SZ8K_4U;
2725 case 64 * 1024:
2726 return _PAGE_SZ64K_4U;
2727 case 512 * 1024:
2728 return _PAGE_SZ512K_4U;
2729 case 4 * 1024 * 1024:
2730 return _PAGE_SZ4MB_4U;
Joe Perches6cb79b32011-06-03 14:45:23 +00002731 }
David S. Millerc4bce902006-02-11 21:57:54 -08002732 }
2733}
2734
2735pte_t mk_pte_io(unsigned long page, pgprot_t prot, int space, unsigned long page_size)
2736{
2737 pte_t pte;
David S. Millercf627152006-02-12 21:10:07 -08002738
2739 pte_val(pte) = page | pgprot_val(pgprot_noncached(prot));
David S. Millerc4bce902006-02-11 21:57:54 -08002740 pte_val(pte) |= (((unsigned long)space) << 32);
2741 pte_val(pte) |= pte_sz_bits(page_size);
David S. Millercf627152006-02-12 21:10:07 -08002742
David S. Millerc4bce902006-02-11 21:57:54 -08002743 return pte;
2744}
2745
David S. Millerc4bce902006-02-11 21:57:54 -08002746static unsigned long kern_large_tte(unsigned long paddr)
2747{
2748 unsigned long val;
2749
2750 val = (_PAGE_VALID | _PAGE_SZ4MB_4U |
2751 _PAGE_CP_4U | _PAGE_CV_4U | _PAGE_P_4U |
2752 _PAGE_EXEC_4U | _PAGE_L_4U | _PAGE_W_4U);
2753 if (tlb_type == hypervisor)
2754 val = (_PAGE_VALID | _PAGE_SZ4MB_4V |
Khalid Aziz494e5b62015-05-27 10:00:46 -06002755 page_cache4v_flag | _PAGE_P_4V |
David S. Millerc4bce902006-02-11 21:57:54 -08002756 _PAGE_EXEC_4V | _PAGE_W_4V);
2757
2758 return val | paddr;
2759}
2760
David S. Millerc4bce902006-02-11 21:57:54 -08002761/* If not locked, zap it. */
2762void __flush_tlb_all(void)
2763{
2764 unsigned long pstate;
2765 int i;
2766
2767 __asm__ __volatile__("flushw\n\t"
2768 "rdpr %%pstate, %0\n\t"
2769 "wrpr %0, %1, %%pstate"
2770 : "=r" (pstate)
2771 : "i" (PSTATE_IE));
David S. Miller8f3614532007-12-13 06:13:38 -08002772 if (tlb_type == hypervisor) {
2773 sun4v_mmu_demap_all();
2774 } else if (tlb_type == spitfire) {
David S. Millerc4bce902006-02-11 21:57:54 -08002775 for (i = 0; i < 64; i++) {
2776 /* Spitfire Errata #32 workaround */
2777 /* NOTE: Always runs on spitfire, so no
2778 * cheetah+ page size encodings.
2779 */
2780 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
2781 "flush %%g6"
2782 : /* No outputs */
2783 : "r" (0),
2784 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
2785
2786 if (!(spitfire_get_dtlb_data(i) & _PAGE_L_4U)) {
2787 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
2788 "membar #Sync"
2789 : /* no outputs */
2790 : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
2791 spitfire_put_dtlb_data(i, 0x0UL);
2792 }
2793
2794 /* Spitfire Errata #32 workaround */
2795 /* NOTE: Always runs on spitfire, so no
2796 * cheetah+ page size encodings.
2797 */
2798 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
2799 "flush %%g6"
2800 : /* No outputs */
2801 : "r" (0),
2802 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
2803
2804 if (!(spitfire_get_itlb_data(i) & _PAGE_L_4U)) {
2805 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
2806 "membar #Sync"
2807 : /* no outputs */
2808 : "r" (TLB_TAG_ACCESS), "i" (ASI_IMMU));
2809 spitfire_put_itlb_data(i, 0x0UL);
2810 }
2811 }
2812 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
2813 cheetah_flush_dtlb_all();
2814 cheetah_flush_itlb_all();
2815 }
2816 __asm__ __volatile__("wrpr %0, 0, %%pstate"
2817 : : "r" (pstate));
2818}
David Millerc460bec2012-10-08 16:34:22 -07002819
David Millerc460bec2012-10-08 16:34:22 -07002820pte_t *pte_alloc_one_kernel(struct mm_struct *mm,
2821 unsigned long address)
2822{
Michal Hocko32d6bd92016-06-24 14:48:47 -07002823 struct page *page = alloc_page(GFP_KERNEL | __GFP_NOTRACK | __GFP_ZERO);
David S. Miller37b3a8f2013-09-25 13:48:49 -07002824 pte_t *pte = NULL;
David Millerc460bec2012-10-08 16:34:22 -07002825
David Millerc460bec2012-10-08 16:34:22 -07002826 if (page)
2827 pte = (pte_t *) page_address(page);
2828
2829 return pte;
2830}
2831
2832pgtable_t pte_alloc_one(struct mm_struct *mm,
2833 unsigned long address)
2834{
Michal Hocko32d6bd92016-06-24 14:48:47 -07002835 struct page *page = alloc_page(GFP_KERNEL | __GFP_NOTRACK | __GFP_ZERO);
Kirill A. Shutemov1ae9ae52013-11-14 14:31:42 -08002836 if (!page)
2837 return NULL;
2838 if (!pgtable_page_ctor(page)) {
2839 free_hot_cold_page(page, 0);
2840 return NULL;
David Millerc460bec2012-10-08 16:34:22 -07002841 }
Kirill A. Shutemov1ae9ae52013-11-14 14:31:42 -08002842 return (pte_t *) page_address(page);
David Millerc460bec2012-10-08 16:34:22 -07002843}
2844
2845void pte_free_kernel(struct mm_struct *mm, pte_t *pte)
2846{
David S. Miller37b3a8f2013-09-25 13:48:49 -07002847 free_page((unsigned long)pte);
David Millerc460bec2012-10-08 16:34:22 -07002848}
2849
2850static void __pte_free(pgtable_t pte)
2851{
2852 struct page *page = virt_to_page(pte);
David S. Miller37b3a8f2013-09-25 13:48:49 -07002853
2854 pgtable_page_dtor(page);
2855 __free_page(page);
David Millerc460bec2012-10-08 16:34:22 -07002856}
2857
2858void pte_free(struct mm_struct *mm, pgtable_t pte)
2859{
2860 __pte_free(pte);
2861}
2862
2863void pgtable_free(void *table, bool is_page)
2864{
2865 if (is_page)
2866 __pte_free(table);
2867 else
2868 kmem_cache_free(pgtable_cache, table);
2869}
David Miller9e695d22012-10-08 16:34:29 -07002870
2871#ifdef CONFIG_TRANSPARENT_HUGEPAGE
David Miller9e695d22012-10-08 16:34:29 -07002872void update_mmu_cache_pmd(struct vm_area_struct *vma, unsigned long addr,
2873 pmd_t *pmd)
2874{
2875 unsigned long pte, flags;
2876 struct mm_struct *mm;
2877 pmd_t entry = *pmd;
David Miller9e695d22012-10-08 16:34:29 -07002878
2879 if (!pmd_large(entry) || !pmd_young(entry))
2880 return;
2881
David S. Millera7b94032013-09-26 13:45:15 -07002882 pte = pmd_val(entry);
David Miller9e695d22012-10-08 16:34:29 -07002883
David S. Miller18f38132014-08-04 16:34:01 -07002884 /* Don't insert a non-valid PMD into the TSB, we'll deadlock. */
2885 if (!(pte & _PAGE_VALID))
2886 return;
2887
David S. Miller37b3a8f2013-09-25 13:48:49 -07002888 /* We are fabricating 8MB pages using 4MB real hw pages. */
2889 pte |= (addr & (1UL << REAL_HPAGE_SHIFT));
David Miller9e695d22012-10-08 16:34:29 -07002890
2891 mm = vma->vm_mm;
2892
2893 spin_lock_irqsave(&mm->context.lock, flags);
2894
2895 if (mm->context.tsb_block[MM_TSB_HUGE].tsb != NULL)
David S. Miller37b3a8f2013-09-25 13:48:49 -07002896 __update_mmu_tsb_insert(mm, MM_TSB_HUGE, REAL_HPAGE_SHIFT,
David Miller9e695d22012-10-08 16:34:29 -07002897 addr, pte);
2898
2899 spin_unlock_irqrestore(&mm->context.lock, flags);
2900}
2901#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
2902
2903#if defined(CONFIG_HUGETLB_PAGE) || defined(CONFIG_TRANSPARENT_HUGEPAGE)
2904static void context_reload(void *__data)
2905{
2906 struct mm_struct *mm = __data;
2907
2908 if (mm == current->mm)
2909 load_secondary_context(mm);
2910}
2911
David S. Miller0fbebed2013-02-19 22:34:10 -08002912void hugetlb_setup(struct pt_regs *regs)
David Miller9e695d22012-10-08 16:34:29 -07002913{
David S. Miller0fbebed2013-02-19 22:34:10 -08002914 struct mm_struct *mm = current->mm;
2915 struct tsb_config *tp;
David Miller9e695d22012-10-08 16:34:29 -07002916
David Hildenbrand70ffdb92015-05-11 17:52:11 +02002917 if (faulthandler_disabled() || !mm) {
David S. Miller0fbebed2013-02-19 22:34:10 -08002918 const struct exception_table_entry *entry;
David Miller9e695d22012-10-08 16:34:29 -07002919
David S. Miller0fbebed2013-02-19 22:34:10 -08002920 entry = search_exception_tables(regs->tpc);
2921 if (entry) {
2922 regs->tpc = entry->fixup;
2923 regs->tnpc = regs->tpc + 4;
2924 return;
2925 }
2926 pr_alert("Unexpected HugeTLB setup in atomic context.\n");
2927 die_if_kernel("HugeTSB in atomic", regs);
2928 }
2929
2930 tp = &mm->context.tsb_block[MM_TSB_HUGE];
2931 if (likely(tp->tsb == NULL))
2932 tsb_grow(mm, MM_TSB_HUGE, 0);
2933
David Miller9e695d22012-10-08 16:34:29 -07002934 tsb_context_switch(mm);
2935 smp_tsb_sync(mm);
2936
2937 /* On UltraSPARC-III+ and later, configure the second half of
2938 * the Data-TLB for huge pages.
2939 */
2940 if (tlb_type == cheetah_plus) {
David S. Miller9ea46abe2016-05-25 12:51:20 -07002941 bool need_context_reload = false;
David Miller9e695d22012-10-08 16:34:29 -07002942 unsigned long ctx;
2943
David S. Miller9ea46abe2016-05-25 12:51:20 -07002944 spin_lock_irq(&ctx_alloc_lock);
David Miller9e695d22012-10-08 16:34:29 -07002945 ctx = mm->context.sparc64_ctx_val;
2946 ctx &= ~CTX_PGSZ_MASK;
2947 ctx |= CTX_PGSZ_BASE << CTX_PGSZ0_SHIFT;
2948 ctx |= CTX_PGSZ_HUGE << CTX_PGSZ1_SHIFT;
2949
2950 if (ctx != mm->context.sparc64_ctx_val) {
2951 /* When changing the page size fields, we
2952 * must perform a context flush so that no
2953 * stale entries match. This flush must
2954 * occur with the original context register
2955 * settings.
2956 */
2957 do_flush_tlb_mm(mm);
2958
2959 /* Reload the context register of all processors
2960 * also executing in this address space.
2961 */
2962 mm->context.sparc64_ctx_val = ctx;
David S. Miller9ea46abe2016-05-25 12:51:20 -07002963 need_context_reload = true;
David Miller9e695d22012-10-08 16:34:29 -07002964 }
David S. Miller9ea46abe2016-05-25 12:51:20 -07002965 spin_unlock_irq(&ctx_alloc_lock);
2966
2967 if (need_context_reload)
2968 on_each_cpu(context_reload, mm, 0);
David Miller9e695d22012-10-08 16:34:29 -07002969 }
2970}
2971#endif
bob piccof6d4fb52014-03-03 11:54:42 -05002972
2973static struct resource code_resource = {
2974 .name = "Kernel code",
Toshi Kani35d98e92016-01-26 21:57:22 +01002975 .flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM
bob piccof6d4fb52014-03-03 11:54:42 -05002976};
2977
2978static struct resource data_resource = {
2979 .name = "Kernel data",
Toshi Kani35d98e92016-01-26 21:57:22 +01002980 .flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM
bob piccof6d4fb52014-03-03 11:54:42 -05002981};
2982
2983static struct resource bss_resource = {
2984 .name = "Kernel bss",
Toshi Kani35d98e92016-01-26 21:57:22 +01002985 .flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM
bob piccof6d4fb52014-03-03 11:54:42 -05002986};
2987
2988static inline resource_size_t compute_kern_paddr(void *addr)
2989{
2990 return (resource_size_t) (addr - KERNBASE + kern_base);
2991}
2992
2993static void __init kernel_lds_init(void)
2994{
2995 code_resource.start = compute_kern_paddr(_text);
2996 code_resource.end = compute_kern_paddr(_etext - 1);
2997 data_resource.start = compute_kern_paddr(_etext);
2998 data_resource.end = compute_kern_paddr(_edata - 1);
2999 bss_resource.start = compute_kern_paddr(__bss_start);
3000 bss_resource.end = compute_kern_paddr(_end - 1);
3001}
3002
3003static int __init report_memory(void)
3004{
3005 int i;
3006 struct resource *res;
3007
3008 kernel_lds_init();
3009
3010 for (i = 0; i < pavail_ents; i++) {
3011 res = kzalloc(sizeof(struct resource), GFP_KERNEL);
3012
3013 if (!res) {
3014 pr_warn("Failed to allocate source.\n");
3015 break;
3016 }
3017
3018 res->name = "System RAM";
3019 res->start = pavail[i].phys_addr;
3020 res->end = pavail[i].phys_addr + pavail[i].reg_size - 1;
Toshi Kani35d98e92016-01-26 21:57:22 +01003021 res->flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM;
bob piccof6d4fb52014-03-03 11:54:42 -05003022
3023 if (insert_resource(&iomem_resource, res) < 0) {
3024 pr_warn("Resource insertion failed.\n");
3025 break;
3026 }
3027
3028 insert_resource(res, &code_resource);
3029 insert_resource(res, &data_resource);
3030 insert_resource(res, &bss_resource);
3031 }
3032
3033 return 0;
3034}
David S. Miller3c081582015-03-18 19:15:28 -07003035arch_initcall(report_memory);
David S. Millere9011d02014-08-05 18:57:18 -07003036
David S. Miller4ca9a232014-08-04 20:07:37 -07003037#ifdef CONFIG_SMP
3038#define do_flush_tlb_kernel_range smp_flush_tlb_kernel_range
3039#else
3040#define do_flush_tlb_kernel_range __flush_tlb_kernel_range
3041#endif
3042
3043void flush_tlb_kernel_range(unsigned long start, unsigned long end)
3044{
3045 if (start < HI_OBP_ADDRESS && end > LOW_OBP_ADDRESS) {
3046 if (start < LOW_OBP_ADDRESS) {
3047 flush_tsb_kernel_range(start, LOW_OBP_ADDRESS);
3048 do_flush_tlb_kernel_range(start, LOW_OBP_ADDRESS);
3049 }
3050 if (end > HI_OBP_ADDRESS) {
David S. Miller473ad7f2014-10-04 21:05:14 -07003051 flush_tsb_kernel_range(HI_OBP_ADDRESS, end);
3052 do_flush_tlb_kernel_range(HI_OBP_ADDRESS, end);
David S. Miller4ca9a232014-08-04 20:07:37 -07003053 }
3054 } else {
3055 flush_tsb_kernel_range(start, end);
3056 do_flush_tlb_kernel_range(start, end);
3057 }
3058}