blob: b2ba410b26f4a013493a51772ce2eb76c9ddc9f3 [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
Nitin Gupta8399e4b2017-07-19 17:12:54 -0700328static void __init add_huge_page_size(unsigned long size)
329{
330 unsigned int order;
331
332 if (size_to_hstate(size))
333 return;
334
335 order = ilog2(size) - PAGE_SHIFT;
336 hugetlb_add_hstate(order);
337}
338
339static int __init hugetlbpage_init(void)
340{
341 add_huge_page_size(1UL << HPAGE_64K_SHIFT);
342 add_huge_page_size(1UL << HPAGE_SHIFT);
343 add_huge_page_size(1UL << HPAGE_256MB_SHIFT);
344 add_huge_page_size(1UL << HPAGE_2GB_SHIFT);
345
346 return 0;
347}
348
349arch_initcall(hugetlbpage_init);
350
Nitin Guptadf7b2152017-08-11 16:46:50 -0700351static void __init pud_huge_patch(void)
352{
353 struct pud_huge_patch_entry *p;
354 unsigned long addr;
355
356 p = &__pud_huge_patch;
357 addr = p->addr;
358 *(unsigned int *)addr = p->insn;
359
360 __asm__ __volatile__("flush %0" : : "r" (addr));
361}
362
Nitin Guptac7d9f772017-02-01 16:16:36 -0800363static int __init setup_hugepagesz(char *string)
364{
365 unsigned long long hugepage_size;
366 unsigned int hugepage_shift;
367 unsigned short hv_pgsz_idx;
368 unsigned int hv_pgsz_mask;
369 int rc = 0;
370
371 hugepage_size = memparse(string, &string);
372 hugepage_shift = ilog2(hugepage_size);
373
374 switch (hugepage_shift) {
Nitin Guptadf7b2152017-08-11 16:46:50 -0700375 case HPAGE_16GB_SHIFT:
376 hv_pgsz_mask = HV_PGSZ_MASK_16GB;
377 hv_pgsz_idx = HV_PGSZ_IDX_16GB;
378 pud_huge_patch();
379 break;
Nitin Gupta85b1da72017-03-09 14:22:23 -0800380 case HPAGE_2GB_SHIFT:
381 hv_pgsz_mask = HV_PGSZ_MASK_2GB;
382 hv_pgsz_idx = HV_PGSZ_IDX_2GB;
383 break;
Nitin Guptac7d9f772017-02-01 16:16:36 -0800384 case HPAGE_256MB_SHIFT:
385 hv_pgsz_mask = HV_PGSZ_MASK_256MB;
386 hv_pgsz_idx = HV_PGSZ_IDX_256MB;
387 break;
388 case HPAGE_SHIFT:
389 hv_pgsz_mask = HV_PGSZ_MASK_4MB;
390 hv_pgsz_idx = HV_PGSZ_IDX_4MB;
391 break;
Nitin Guptadcd19122017-02-06 12:33:26 -0800392 case HPAGE_64K_SHIFT:
393 hv_pgsz_mask = HV_PGSZ_MASK_64K;
394 hv_pgsz_idx = HV_PGSZ_IDX_64K;
395 break;
Nitin Guptac7d9f772017-02-01 16:16:36 -0800396 default:
397 hv_pgsz_mask = 0;
398 }
399
400 if ((hv_pgsz_mask & cpu_pgsz_mask) == 0U) {
Liam R. Howlettf3229802017-05-30 15:45:00 -0400401 hugetlb_bad_size();
402 pr_err("hugepagesz=%llu not supported by MMU.\n",
Nitin Guptac7d9f772017-02-01 16:16:36 -0800403 hugepage_size);
404 goto out;
405 }
406
Nitin Gupta8399e4b2017-07-19 17:12:54 -0700407 add_huge_page_size(hugepage_size);
Nitin Guptac7d9f772017-02-01 16:16:36 -0800408 rc = 1;
409
410out:
411 return rc;
412}
413__setup("hugepagesz=", setup_hugepagesz);
414#endif /* CONFIG_HUGETLB_PAGE */
415
Russell King4b3073e2009-12-18 16:40:18 +0000416void update_mmu_cache(struct vm_area_struct *vma, unsigned long address, pte_t *ptep)
Sam Ravnborgff9aefb2009-01-06 12:51:26 -0800417{
418 struct mm_struct *mm;
David S. Millerbcd896b2013-02-19 13:20:08 -0800419 unsigned long flags;
Nitin Guptadf7b2152017-08-11 16:46:50 -0700420 bool is_huge_tsb;
Russell King4b3073e2009-12-18 16:40:18 +0000421 pte_t pte = *ptep;
Sam Ravnborgff9aefb2009-01-06 12:51:26 -0800422
423 if (tlb_type != hypervisor) {
424 unsigned long pfn = pte_pfn(pte);
425
426 if (pfn_valid(pfn))
427 flush_dcache(pfn);
428 }
David S. Millerbd407912006-01-31 18:31:38 -0800429
430 mm = vma->vm_mm;
David S. Miller7a1ac522006-03-16 02:02:32 -0800431
David S. Miller18f38132014-08-04 16:34:01 -0700432 /* Don't insert a non-valid PTE into the TSB, we'll deadlock. */
433 if (!pte_accessible(mm, pte))
434 return;
435
David S. Miller7a1ac522006-03-16 02:02:32 -0800436 spin_lock_irqsave(&mm->context.lock, flags);
437
Nitin Guptadf7b2152017-08-11 16:46:50 -0700438 is_huge_tsb = false;
David Miller9e695d22012-10-08 16:34:29 -0700439#if defined(CONFIG_HUGETLB_PAGE) || defined(CONFIG_TRANSPARENT_HUGEPAGE)
Nitin Guptadf7b2152017-08-11 16:46:50 -0700440 if (mm->context.hugetlb_pte_count || mm->context.thp_pte_count) {
441 unsigned long hugepage_size = PAGE_SIZE;
442
443 if (is_vm_hugetlb_page(vma))
444 hugepage_size = huge_page_size(hstate_vma(vma));
445
446 if (hugepage_size >= PUD_SIZE) {
447 unsigned long mask = 0x1ffc00000UL;
448
449 /* Transfer bits [32:22] from address to resolve
450 * at 4M granularity.
451 */
452 pte_val(pte) &= ~mask;
453 pte_val(pte) |= (address & mask);
454 } else if (hugepage_size >= PMD_SIZE) {
455 /* We are fabricating 8MB pages using 4MB
456 * real hw pages.
457 */
458 pte_val(pte) |= (address & (1UL << REAL_HPAGE_SHIFT));
459 }
460
461 if (hugepage_size >= PMD_SIZE) {
462 __update_mmu_tsb_insert(mm, MM_TSB_HUGE,
463 REAL_HPAGE_SHIFT, address, pte_val(pte));
464 is_huge_tsb = true;
465 }
466 }
David S. Millerdcc1e8d2006-03-22 00:49:59 -0800467#endif
Nitin Guptadf7b2152017-08-11 16:46:50 -0700468 if (!is_huge_tsb)
David S. Millerbcd896b2013-02-19 13:20:08 -0800469 __update_mmu_tsb_insert(mm, MM_TSB_BASE, PAGE_SHIFT,
470 address, pte_val(pte));
David S. Miller7a1ac522006-03-16 02:02:32 -0800471
472 spin_unlock_irqrestore(&mm->context.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473}
474
475void flush_dcache_page(struct page *page)
476{
David S. Millera9546f52005-04-17 18:03:09 -0700477 struct address_space *mapping;
478 int this_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
David S. Miller7a591cf2006-02-26 19:44:50 -0800480 if (tlb_type == hypervisor)
481 return;
482
David S. Millera9546f52005-04-17 18:03:09 -0700483 /* Do not bother with the expensive D-cache flush if it
484 * is merely the zero page. The 'bigcore' testcase in GDB
485 * causes this case to run millions of times.
486 */
487 if (page == ZERO_PAGE(0))
488 return;
489
490 this_cpu = get_cpu();
491
492 mapping = page_mapping(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 if (mapping && !mapping_mapped(mapping)) {
David S. Millera9546f52005-04-17 18:03:09 -0700494 int dirty = test_bit(PG_dcache_dirty, &page->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 if (dirty) {
David S. Millera9546f52005-04-17 18:03:09 -0700496 int dirty_cpu = dcache_dirty_cpu(page);
497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 if (dirty_cpu == this_cpu)
499 goto out;
500 smp_flush_dcache_page_impl(page, dirty_cpu);
501 }
502 set_dcache_dirty(page, this_cpu);
503 } else {
504 /* We could delay the flush for the !page_mapping
505 * case too. But that case is for exec env/arg
506 * pages and those are %99 certainly going to get
507 * faulted into the tlb (and thus flushed) anyways.
508 */
509 flush_dcache_page_impl(page);
510 }
511
512out:
513 put_cpu();
514}
Sam Ravnborg917c3662009-01-08 16:58:20 -0800515EXPORT_SYMBOL(flush_dcache_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Prasanna S Panchamukhi05e14cb2005-09-06 15:19:30 -0700517void __kprobes flush_icache_range(unsigned long start, unsigned long end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
David S. Millera43fe0e2006-02-04 03:10:53 -0800519 /* Cheetah and Hypervisor platform cpus have coherent I-cache. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 if (tlb_type == spitfire) {
521 unsigned long kaddr;
522
David S. Millera94aa252007-03-15 15:50:11 -0700523 /* This code only runs on Spitfire cpus so this is
524 * why we can assume _PAGE_PADDR_4U.
525 */
526 for (kaddr = start; kaddr < end; kaddr += PAGE_SIZE) {
527 unsigned long paddr, mask = _PAGE_PADDR_4U;
528
529 if (kaddr >= PAGE_OFFSET)
530 paddr = kaddr & mask;
531 else {
532 pgd_t *pgdp = pgd_offset_k(kaddr);
533 pud_t *pudp = pud_offset(pgdp, kaddr);
534 pmd_t *pmdp = pmd_offset(pudp, kaddr);
535 pte_t *ptep = pte_offset_kernel(pmdp, kaddr);
536
537 paddr = pte_val(*ptep) & mask;
538 }
539 __flush_icache_page(paddr);
540 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 }
542}
Sam Ravnborg917c3662009-01-08 16:58:20 -0800543EXPORT_SYMBOL(flush_icache_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545void mmu_info(struct seq_file *m)
546{
David S. Millerce33fdc2012-09-06 19:01:25 -0700547 static const char *pgsz_strings[] = {
548 "8K", "64K", "512K", "4MB", "32MB",
549 "256MB", "2GB", "16GB",
550 };
551 int i, printed;
552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 if (tlb_type == cheetah)
554 seq_printf(m, "MMU Type\t: Cheetah\n");
555 else if (tlb_type == cheetah_plus)
556 seq_printf(m, "MMU Type\t: Cheetah+\n");
557 else if (tlb_type == spitfire)
558 seq_printf(m, "MMU Type\t: Spitfire\n");
David S. Millera43fe0e2006-02-04 03:10:53 -0800559 else if (tlb_type == hypervisor)
560 seq_printf(m, "MMU Type\t: Hypervisor (sun4v)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 else
562 seq_printf(m, "MMU Type\t: ???\n");
563
David S. Millerce33fdc2012-09-06 19:01:25 -0700564 seq_printf(m, "MMU PGSZs\t: ");
565 printed = 0;
566 for (i = 0; i < ARRAY_SIZE(pgsz_strings); i++) {
567 if (cpu_pgsz_mask & (1UL << i)) {
568 seq_printf(m, "%s%s",
569 printed ? "," : "", pgsz_strings[i]);
570 printed++;
571 }
572 }
573 seq_putc(m, '\n');
574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575#ifdef CONFIG_DEBUG_DCFLUSH
576 seq_printf(m, "DCPageFlushes\t: %d\n",
577 atomic_read(&dcpage_flushes));
578#ifdef CONFIG_SMP
579 seq_printf(m, "DCPageFlushesXC\t: %d\n",
580 atomic_read(&dcpage_flushes_xcall));
581#endif /* CONFIG_SMP */
582#endif /* CONFIG_DEBUG_DCFLUSH */
583}
584
David S. Millera94aa252007-03-15 15:50:11 -0700585struct linux_prom_translation prom_trans[512] __read_mostly;
586unsigned int prom_trans_ents __read_mostly;
587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588unsigned long kern_locked_tte_data;
589
David S. Miller405599b2005-09-22 00:12:35 -0700590/* The obp translations are saved based on 8k pagesize, since obp can
591 * use a mixture of pagesizes. Misses to the LOW_OBP_ADDRESS ->
David S. Miller74bf4312006-01-31 18:29:18 -0800592 * HI_OBP_ADDRESS range are handled in ktlb.S.
David S. Miller405599b2005-09-22 00:12:35 -0700593 */
David S. Miller5085b4a2005-09-22 00:45:41 -0700594static inline int in_obp_range(unsigned long vaddr)
595{
596 return (vaddr >= LOW_OBP_ADDRESS &&
597 vaddr < HI_OBP_ADDRESS);
598}
599
David S. Millerc9c10832005-10-12 12:22:46 -0700600static int cmp_ptrans(const void *a, const void *b)
David S. Miller405599b2005-09-22 00:12:35 -0700601{
David S. Millerc9c10832005-10-12 12:22:46 -0700602 const struct linux_prom_translation *x = a, *y = b;
David S. Miller405599b2005-09-22 00:12:35 -0700603
David S. Millerc9c10832005-10-12 12:22:46 -0700604 if (x->virt > y->virt)
605 return 1;
606 if (x->virt < y->virt)
607 return -1;
608 return 0;
David S. Miller405599b2005-09-22 00:12:35 -0700609}
610
David S. Millerc9c10832005-10-12 12:22:46 -0700611/* Read OBP translations property into 'prom_trans[]'. */
David S. Miller9ad98c52005-10-05 15:12:00 -0700612static void __init read_obp_translations(void)
David S. Miller405599b2005-09-22 00:12:35 -0700613{
David S. Millerc9c10832005-10-12 12:22:46 -0700614 int n, node, ents, first, last, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616 node = prom_finddevice("/virtual-memory");
617 n = prom_getproplen(node, "translations");
David S. Miller405599b2005-09-22 00:12:35 -0700618 if (unlikely(n == 0 || n == -1)) {
David S. Millerb206fc42005-09-21 22:31:13 -0700619 prom_printf("prom_mappings: Couldn't get size.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 prom_halt();
621 }
David S. Miller405599b2005-09-22 00:12:35 -0700622 if (unlikely(n > sizeof(prom_trans))) {
Akinobu Mita5da444a2012-09-29 03:14:49 +0000623 prom_printf("prom_mappings: Size %d is too big.\n", n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 prom_halt();
625 }
David S. Miller405599b2005-09-22 00:12:35 -0700626
David S. Millerb206fc42005-09-21 22:31:13 -0700627 if ((n = prom_getproperty(node, "translations",
David S. Miller405599b2005-09-22 00:12:35 -0700628 (char *)&prom_trans[0],
629 sizeof(prom_trans))) == -1) {
David S. Millerb206fc42005-09-21 22:31:13 -0700630 prom_printf("prom_mappings: Couldn't get property.\n");
631 prom_halt();
632 }
David S. Miller9ad98c52005-10-05 15:12:00 -0700633
David S. Millerb206fc42005-09-21 22:31:13 -0700634 n = n / sizeof(struct linux_prom_translation);
David S. Miller9ad98c52005-10-05 15:12:00 -0700635
David S. Millerc9c10832005-10-12 12:22:46 -0700636 ents = n;
637
638 sort(prom_trans, ents, sizeof(struct linux_prom_translation),
639 cmp_ptrans, NULL);
640
641 /* Now kick out all the non-OBP entries. */
642 for (i = 0; i < ents; i++) {
643 if (in_obp_range(prom_trans[i].virt))
644 break;
645 }
646 first = i;
647 for (; i < ents; i++) {
648 if (!in_obp_range(prom_trans[i].virt))
649 break;
650 }
651 last = i;
652
653 for (i = 0; i < (last - first); i++) {
654 struct linux_prom_translation *src = &prom_trans[i + first];
655 struct linux_prom_translation *dest = &prom_trans[i];
656
657 *dest = *src;
658 }
659 for (; i < ents; i++) {
660 struct linux_prom_translation *dest = &prom_trans[i];
661 dest->virt = dest->size = dest->data = 0x0UL;
662 }
663
664 prom_trans_ents = last - first;
665
666 if (tlb_type == spitfire) {
667 /* Clear diag TTE bits. */
668 for (i = 0; i < prom_trans_ents; i++)
669 prom_trans[i].data &= ~0x0003fe0000000000UL;
670 }
David S. Millerf4142cb2011-09-29 12:18:59 -0700671
672 /* Force execute bit on. */
673 for (i = 0; i < prom_trans_ents; i++)
674 prom_trans[i].data |= (tlb_type == hypervisor ?
675 _PAGE_EXEC_4V : _PAGE_EXEC_4U);
David S. Miller405599b2005-09-22 00:12:35 -0700676}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
David S. Millerd82ace72006-02-09 02:52:44 -0800678static void __init hypervisor_tlb_lock(unsigned long vaddr,
679 unsigned long pte,
680 unsigned long mmu)
681{
David S. Miller7db35f32007-05-29 02:22:14 -0700682 unsigned long ret = sun4v_mmu_map_perm_addr(vaddr, 0, pte, mmu);
David S. Millerd82ace72006-02-09 02:52:44 -0800683
David S. Miller7db35f32007-05-29 02:22:14 -0700684 if (ret != 0) {
Akinobu Mita5da444a2012-09-29 03:14:49 +0000685 prom_printf("hypervisor_tlb_lock[%lx:%x:%lx:%lx]: "
David S. Miller7db35f32007-05-29 02:22:14 -0700686 "errors with %lx\n", vaddr, 0, pte, mmu, ret);
David S. Miller12e126a2006-02-17 14:40:30 -0800687 prom_halt();
688 }
David S. Millerd82ace72006-02-09 02:52:44 -0800689}
690
David S. Millerc4bce902006-02-11 21:57:54 -0800691static unsigned long kern_large_tte(unsigned long paddr);
692
David S. Miller898cf0e2005-09-23 11:59:44 -0700693static void __init remap_kernel(void)
David S. Miller405599b2005-09-22 00:12:35 -0700694{
695 unsigned long phys_page, tte_vaddr, tte_data;
David S. Miller64658742008-03-21 17:01:38 -0700696 int i, tlb_ent = sparc64_highest_locked_tlbent();
David S. Miller405599b2005-09-22 00:12:35 -0700697
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 tte_vaddr = (unsigned long) KERNBASE;
David S. Miller0eef3312014-05-03 22:52:50 -0700699 phys_page = (prom_boot_mapping_phys_low >> ILOG2_4MB) << ILOG2_4MB;
David S. Millerc4bce902006-02-11 21:57:54 -0800700 tte_data = kern_large_tte(phys_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
702 kern_locked_tte_data = tte_data;
703
David S. Millerd82ace72006-02-09 02:52:44 -0800704 /* Now lock us into the TLBs via Hypervisor or OBP. */
705 if (tlb_type == hypervisor) {
David S. Miller64658742008-03-21 17:01:38 -0700706 for (i = 0; i < num_kernel_image_mappings; i++) {
David S. Millerd82ace72006-02-09 02:52:44 -0800707 hypervisor_tlb_lock(tte_vaddr, tte_data, HV_MMU_DMMU);
708 hypervisor_tlb_lock(tte_vaddr, tte_data, HV_MMU_IMMU);
David S. Miller64658742008-03-21 17:01:38 -0700709 tte_vaddr += 0x400000;
710 tte_data += 0x400000;
David S. Millerd82ace72006-02-09 02:52:44 -0800711 }
712 } else {
David S. Miller64658742008-03-21 17:01:38 -0700713 for (i = 0; i < num_kernel_image_mappings; i++) {
714 prom_dtlb_load(tlb_ent - i, tte_data, tte_vaddr);
715 prom_itlb_load(tlb_ent - i, tte_data, tte_vaddr);
716 tte_vaddr += 0x400000;
717 tte_data += 0x400000;
David S. Millerd82ace72006-02-09 02:52:44 -0800718 }
David S. Miller64658742008-03-21 17:01:38 -0700719 sparc64_highest_unlocked_tlb_ent = tlb_ent - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 }
David S. Miller0835ae02005-10-04 15:23:20 -0700721 if (tlb_type == cheetah_plus) {
722 sparc64_kern_pri_context = (CTX_CHEETAH_PLUS_CTX0 |
723 CTX_CHEETAH_PLUS_NUC);
724 sparc64_kern_pri_nuc_bits = CTX_CHEETAH_PLUS_NUC;
725 sparc64_kern_sec_context = CTX_CHEETAH_PLUS_CTX0;
726 }
David S. Miller405599b2005-09-22 00:12:35 -0700727}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
David S. Miller405599b2005-09-22 00:12:35 -0700729
David S. Millerc9c10832005-10-12 12:22:46 -0700730static void __init inherit_prom_mappings(void)
David S. Miller9ad98c52005-10-05 15:12:00 -0700731{
David S. Miller405599b2005-09-22 00:12:35 -0700732 /* Now fixup OBP's idea about where we really are mapped. */
David S. Miller3c62a2d2008-02-17 23:22:50 -0800733 printk("Remapping the kernel... ");
David S. Miller405599b2005-09-22 00:12:35 -0700734 remap_kernel();
David S. Miller3c62a2d2008-02-17 23:22:50 -0800735 printk("done.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736}
737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738void prom_world(int enter)
739{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 if (!enter)
Al Virodff933d2012-09-26 01:21:14 -0400741 set_fs(get_fs());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
David S. Miller3487d1d2006-01-31 18:33:25 -0800743 __asm__ __volatile__("flushw");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744}
745
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746void __flush_dcache_range(unsigned long start, unsigned long end)
747{
748 unsigned long va;
749
750 if (tlb_type == spitfire) {
751 int n = 0;
752
753 for (va = start; va < end; va += 32) {
754 spitfire_put_dcache_tag(va & 0x3fe0, 0x0);
755 if (++n >= 512)
756 break;
757 }
David S. Millera43fe0e2006-02-04 03:10:53 -0800758 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 start = __pa(start);
760 end = __pa(end);
761 for (va = start; va < end; va += 32)
762 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
763 "membar #Sync"
764 : /* no outputs */
765 : "r" (va),
766 "i" (ASI_DCACHE_INVALIDATE));
767 }
768}
Sam Ravnborg917c3662009-01-08 16:58:20 -0800769EXPORT_SYMBOL(__flush_dcache_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
David S. Miller85f1e1f2007-03-15 17:51:26 -0700771/* get_new_mmu_context() uses "cache + 1". */
772DEFINE_SPINLOCK(ctx_alloc_lock);
Pavel Tatashinc4415232017-05-31 11:25:22 -0400773unsigned long tlb_context_cache = CTX_FIRST_VERSION;
David S. Miller85f1e1f2007-03-15 17:51:26 -0700774#define MAX_CTX_NR (1UL << CTX_NR_BITS)
775#define CTX_BMAP_SLOTS BITS_TO_LONGS(MAX_CTX_NR)
776DECLARE_BITMAP(mmu_context_bmap, MAX_CTX_NR);
Pavel Tatashin7a5b4bb2017-05-31 11:25:23 -0400777DEFINE_PER_CPU(struct mm_struct *, per_cpu_secondary_mm) = {0};
David S. Miller85f1e1f2007-03-15 17:51:26 -0700778
Pavel Tatashina0582f22017-05-31 11:25:24 -0400779static void mmu_context_wrap(void)
780{
781 unsigned long old_ver = tlb_context_cache & CTX_VERSION_MASK;
782 unsigned long new_ver, new_ctx, old_ctx;
783 struct mm_struct *mm;
784 int cpu;
785
786 bitmap_zero(mmu_context_bmap, 1 << CTX_NR_BITS);
787
788 /* Reserve kernel context */
789 set_bit(0, mmu_context_bmap);
790
791 new_ver = (tlb_context_cache & CTX_VERSION_MASK) + CTX_FIRST_VERSION;
792 if (unlikely(new_ver == 0))
793 new_ver = CTX_FIRST_VERSION;
794 tlb_context_cache = new_ver;
795
796 /*
797 * Make sure that any new mm that are added into per_cpu_secondary_mm,
798 * are going to go through get_new_mmu_context() path.
799 */
800 mb();
801
802 /*
803 * Updated versions to current on those CPUs that had valid secondary
804 * contexts
805 */
806 for_each_online_cpu(cpu) {
807 /*
808 * If a new mm is stored after we took this mm from the array,
809 * it will go into get_new_mmu_context() path, because we
810 * already bumped the version in tlb_context_cache.
811 */
812 mm = per_cpu(per_cpu_secondary_mm, cpu);
813
814 if (unlikely(!mm || mm == &init_mm))
815 continue;
816
817 old_ctx = mm->context.sparc64_ctx_val;
818 if (likely((old_ctx & CTX_VERSION_MASK) == old_ver)) {
819 new_ctx = (old_ctx & ~CTX_VERSION_MASK) | new_ver;
820 set_bit(new_ctx & CTX_NR_MASK, mmu_context_bmap);
821 mm->context.sparc64_ctx_val = new_ctx;
822 }
823 }
824}
825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826/* Caller does TLB context flushing on local CPU if necessary.
827 * The caller also ensures that CTX_VALID(mm->context) is false.
828 *
829 * We must be careful about boundary cases so that we never
830 * let the user have CTX 0 (nucleus) or we ever use a CTX
831 * version of zero (and thus NO_CONTEXT would not be caught
832 * by version mis-match tests in mmu_context.h).
David S. Millera0663a72006-02-23 14:19:28 -0800833 *
834 * Always invoked with interrupts disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 */
836void get_new_mmu_context(struct mm_struct *mm)
837{
838 unsigned long ctx, new_ctx;
839 unsigned long orig_pgsz_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
Kirill Tkhai07df8412013-04-09 00:29:46 +0400841 spin_lock(&ctx_alloc_lock);
Pavel Tatashina0582f22017-05-31 11:25:24 -0400842retry:
843 /* wrap might have happened, test again if our context became valid */
844 if (unlikely(CTX_VALID(mm->context)))
845 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 orig_pgsz_bits = (mm->context.sparc64_ctx_val & CTX_PGSZ_MASK);
847 ctx = (tlb_context_cache + 1) & CTX_NR_MASK;
848 new_ctx = find_next_zero_bit(mmu_context_bmap, 1 << CTX_NR_BITS, ctx);
849 if (new_ctx >= (1 << CTX_NR_BITS)) {
850 new_ctx = find_next_zero_bit(mmu_context_bmap, ctx, 1);
851 if (new_ctx >= ctx) {
Pavel Tatashina0582f22017-05-31 11:25:24 -0400852 mmu_context_wrap();
853 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 }
855 }
Pavel Tatashin58897482017-05-31 11:25:20 -0400856 if (mm->context.sparc64_ctx_val)
857 cpumask_clear(mm_cpumask(mm));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 mmu_context_bmap[new_ctx>>6] |= (1UL << (new_ctx & 63));
859 new_ctx |= (tlb_context_cache & CTX_VERSION_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 tlb_context_cache = new_ctx;
861 mm->context.sparc64_ctx_val = new_ctx | orig_pgsz_bits;
Pavel Tatashina0582f22017-05-31 11:25:24 -0400862out:
Kirill Tkhai07df8412013-04-09 00:29:46 +0400863 spin_unlock(&ctx_alloc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864}
865
David S. Miller919ee672008-04-23 05:40:25 -0700866static int numa_enabled = 1;
867static int numa_debug;
868
869static int __init early_numa(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870{
David S. Miller919ee672008-04-23 05:40:25 -0700871 if (!p)
872 return 0;
David S. Millerd1112012006-03-08 02:16:07 -0800873
David S. Miller919ee672008-04-23 05:40:25 -0700874 if (strstr(p, "off"))
875 numa_enabled = 0;
David S. Millerd1112012006-03-08 02:16:07 -0800876
David S. Miller919ee672008-04-23 05:40:25 -0700877 if (strstr(p, "debug"))
878 numa_debug = 1;
879
880 return 0;
David S. Millerd1112012006-03-08 02:16:07 -0800881}
David S. Miller919ee672008-04-23 05:40:25 -0700882early_param("numa", early_numa);
883
884#define numadbg(f, a...) \
885do { if (numa_debug) \
886 printk(KERN_INFO f, ## a); \
887} while (0)
David S. Millerd1112012006-03-08 02:16:07 -0800888
David S. Miller4e82c9a2008-02-13 18:00:03 -0800889static void __init find_ramdisk(unsigned long phys_base)
890{
891#ifdef CONFIG_BLK_DEV_INITRD
892 if (sparc_ramdisk_image || sparc_ramdisk_image64) {
893 unsigned long ramdisk_image;
894
895 /* Older versions of the bootloader only supported a
896 * 32-bit physical address for the ramdisk image
897 * location, stored at sparc_ramdisk_image. Newer
898 * SILO versions set sparc_ramdisk_image to zero and
899 * provide a full 64-bit physical address at
900 * sparc_ramdisk_image64.
901 */
902 ramdisk_image = sparc_ramdisk_image;
903 if (!ramdisk_image)
904 ramdisk_image = sparc_ramdisk_image64;
905
906 /* Another bootloader quirk. The bootloader normalizes
907 * the physical address to KERNBASE, so we have to
908 * factor that back out and add in the lowest valid
909 * physical page address to get the true physical address.
910 */
911 ramdisk_image -= KERNBASE;
912 ramdisk_image += phys_base;
913
David S. Miller919ee672008-04-23 05:40:25 -0700914 numadbg("Found ramdisk at physical address 0x%lx, size %u\n",
915 ramdisk_image, sparc_ramdisk_size);
916
David S. Miller4e82c9a2008-02-13 18:00:03 -0800917 initrd_start = ramdisk_image;
918 initrd_end = ramdisk_image + sparc_ramdisk_size;
David S. Miller3b2a7e22008-02-13 18:13:20 -0800919
Yinghai Lu95f72d12010-07-12 14:36:09 +1000920 memblock_reserve(initrd_start, sparc_ramdisk_size);
David S. Millerd45100f2008-05-06 15:19:54 -0700921
922 initrd_start += PAGE_OFFSET;
923 initrd_end += PAGE_OFFSET;
David S. Miller4e82c9a2008-02-13 18:00:03 -0800924 }
925#endif
926}
927
David S. Miller919ee672008-04-23 05:40:25 -0700928struct node_mem_mask {
929 unsigned long mask;
Pavel Tatashin1537b262017-02-16 15:05:58 -0500930 unsigned long match;
David S. Miller919ee672008-04-23 05:40:25 -0700931};
932static struct node_mem_mask node_masks[MAX_NUMNODES];
933static int num_node_masks;
934
Sam Ravnborg48d37212014-05-16 23:26:12 +0200935#ifdef CONFIG_NEED_MULTIPLE_NODES
936
Pavel Tatashin1537b262017-02-16 15:05:58 -0500937struct mdesc_mlgroup {
938 u64 node;
939 u64 latency;
940 u64 match;
941 u64 mask;
942};
943
944static struct mdesc_mlgroup *mlgroups;
945static int num_mlgroups;
946
David S. Miller919ee672008-04-23 05:40:25 -0700947int numa_cpu_lookup_table[NR_CPUS];
948cpumask_t numa_cpumask_lookup_table[MAX_NUMNODES];
949
David S. Miller919ee672008-04-23 05:40:25 -0700950struct mdesc_mblock {
951 u64 base;
952 u64 size;
953 u64 offset; /* RA-to-PA */
954};
955static struct mdesc_mblock *mblocks;
956static int num_mblocks;
957
Pavel Tatashin1537b262017-02-16 15:05:58 -0500958static struct mdesc_mblock * __init addr_to_mblock(unsigned long addr)
David S. Millerd1112012006-03-08 02:16:07 -0800959{
Pavel Tatashin1537b262017-02-16 15:05:58 -0500960 struct mdesc_mblock *m = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 int i;
962
David S. Miller919ee672008-04-23 05:40:25 -0700963 for (i = 0; i < num_mblocks; i++) {
Pavel Tatashin1537b262017-02-16 15:05:58 -0500964 m = &mblocks[i];
David S. Miller6fc5bae2006-12-28 21:00:23 -0800965
David S. Miller919ee672008-04-23 05:40:25 -0700966 if (addr >= m->base &&
967 addr < (m->base + m->size)) {
David S. Miller919ee672008-04-23 05:40:25 -0700968 break;
969 }
970 }
Pavel Tatashin1537b262017-02-16 15:05:58 -0500971
972 return m;
David S. Miller919ee672008-04-23 05:40:25 -0700973}
974
Pavel Tatashin1537b262017-02-16 15:05:58 -0500975static u64 __init memblock_nid_range_sun4u(u64 start, u64 end, int *nid)
David S. Miller919ee672008-04-23 05:40:25 -0700976{
Pavel Tatashin1537b262017-02-16 15:05:58 -0500977 int prev_nid, new_nid;
David S. Miller919ee672008-04-23 05:40:25 -0700978
Pavel Tatashin1537b262017-02-16 15:05:58 -0500979 prev_nid = -1;
980 for ( ; start < end; start += PAGE_SIZE) {
981 for (new_nid = 0; new_nid < num_node_masks; new_nid++) {
982 struct node_mem_mask *p = &node_masks[new_nid];
David S. Miller919ee672008-04-23 05:40:25 -0700983
Pavel Tatashin1537b262017-02-16 15:05:58 -0500984 if ((start & p->mask) == p->match) {
985 if (prev_nid == -1)
986 prev_nid = new_nid;
987 break;
988 }
Thomas Tai74a5ed52016-11-03 09:19:01 -0700989 }
Thomas Tai74a5ed52016-11-03 09:19:01 -0700990
Pavel Tatashin1537b262017-02-16 15:05:58 -0500991 if (new_nid == num_node_masks) {
992 prev_nid = 0;
993 WARN_ONCE(1, "addr[%Lx] doesn't match a NUMA node rule. Some memory will be owned by node 0.",
994 start);
995 break;
996 }
997
998 if (prev_nid != new_nid)
999 break;
1000 }
1001 *nid = prev_nid;
1002
1003 return start > end ? end : start;
David S. Miller919ee672008-04-23 05:40:25 -07001004}
1005
Thomas Tai87a349f2016-11-11 16:41:00 -08001006static u64 __init memblock_nid_range(u64 start, u64 end, int *nid)
David S. Miller919ee672008-04-23 05:40:25 -07001007{
Pavel Tatashin1537b262017-02-16 15:05:58 -05001008 u64 ret_end, pa_start, m_mask, m_match, m_end;
1009 struct mdesc_mblock *mblock;
1010 int _nid, i;
David S. Miller919ee672008-04-23 05:40:25 -07001011
Pavel Tatashin1537b262017-02-16 15:05:58 -05001012 if (tlb_type != hypervisor)
1013 return memblock_nid_range_sun4u(start, end, nid);
1014
1015 mblock = addr_to_mblock(start);
1016 if (!mblock) {
1017 WARN_ONCE(1, "memblock_nid_range: Can't find mblock addr[%Lx]",
1018 start);
1019
1020 _nid = 0;
1021 ret_end = end;
1022 goto done;
David S. Miller919ee672008-04-23 05:40:25 -07001023 }
1024
Pavel Tatashin1537b262017-02-16 15:05:58 -05001025 pa_start = start + mblock->offset;
1026 m_match = 0;
1027 m_mask = 0;
David S. Millerc918dcc2008-08-14 01:41:39 -07001028
Pavel Tatashin1537b262017-02-16 15:05:58 -05001029 for (_nid = 0; _nid < num_node_masks; _nid++) {
1030 struct node_mem_mask *const m = &node_masks[_nid];
1031
1032 if ((pa_start & m->mask) == m->match) {
1033 m_match = m->match;
1034 m_mask = m->mask;
1035 break;
1036 }
1037 }
1038
1039 if (num_node_masks == _nid) {
1040 /* We could not find NUMA group, so default to 0, but lets
1041 * search for latency group, so we could calculate the correct
1042 * end address that we return
1043 */
1044 _nid = 0;
1045
1046 for (i = 0; i < num_mlgroups; i++) {
1047 struct mdesc_mlgroup *const m = &mlgroups[i];
1048
1049 if ((pa_start & m->mask) == m->match) {
1050 m_match = m->match;
1051 m_mask = m->mask;
1052 break;
1053 }
1054 }
1055
1056 if (i == num_mlgroups) {
1057 WARN_ONCE(1, "memblock_nid_range: Can't find latency group addr[%Lx]",
1058 start);
1059
1060 ret_end = end;
1061 goto done;
1062 }
1063 }
1064
1065 /*
1066 * Each latency group has match and mask, and each memory block has an
1067 * offset. An address belongs to a latency group if its address matches
1068 * the following formula: ((addr + offset) & mask) == match
1069 * It is, however, slow to check every single page if it matches a
1070 * particular latency group. As optimization we calculate end value by
1071 * using bit arithmetics.
1072 */
1073 m_end = m_match + (1ul << __ffs(m_mask)) - mblock->offset;
1074 m_end += pa_start & ~((1ul << fls64(m_mask)) - 1);
1075 ret_end = m_end > end ? end : m_end;
1076
1077done:
1078 *nid = _nid;
1079 return ret_end;
David S. Miller919ee672008-04-23 05:40:25 -07001080}
David S. Miller919ee672008-04-23 05:40:25 -07001081#endif
1082
1083/* This must be invoked after performing all of the necessary
Tejun Heo2a4814d2011-12-08 10:22:08 -08001084 * memblock_set_node() calls for 'nid'. We need to be able to get
David S. Miller919ee672008-04-23 05:40:25 -07001085 * correct data from get_pfn_range_for_nid().
1086 */
1087static void __init allocate_node_data(int nid)
1088{
David S. Miller919ee672008-04-23 05:40:25 -07001089 struct pglist_data *p;
Paul Gortmakeraa6f0792012-05-09 20:44:29 -04001090 unsigned long start_pfn, end_pfn;
David S. Miller919ee672008-04-23 05:40:25 -07001091#ifdef CONFIG_NEED_MULTIPLE_NODES
Paul Gortmakeraa6f0792012-05-09 20:44:29 -04001092 unsigned long paddr;
1093
Benjamin Herrenschmidt9d1e2492010-07-06 15:39:17 -07001094 paddr = memblock_alloc_try_nid(sizeof(struct pglist_data), SMP_CACHE_BYTES, nid);
David S. Miller919ee672008-04-23 05:40:25 -07001095 if (!paddr) {
1096 prom_printf("Cannot allocate pglist_data for nid[%d]\n", nid);
1097 prom_halt();
1098 }
1099 NODE_DATA(nid) = __va(paddr);
1100 memset(NODE_DATA(nid), 0, sizeof(struct pglist_data));
1101
David S. Miller625d6932012-04-25 13:13:43 -07001102 NODE_DATA(nid)->node_id = nid;
David S. Miller919ee672008-04-23 05:40:25 -07001103#endif
1104
1105 p = NODE_DATA(nid);
1106
1107 get_pfn_range_for_nid(nid, &start_pfn, &end_pfn);
1108 p->node_start_pfn = start_pfn;
1109 p->node_spanned_pages = end_pfn - start_pfn;
David S. Miller919ee672008-04-23 05:40:25 -07001110}
1111
1112static void init_node_masks_nonnuma(void)
1113{
Sam Ravnborg48d37212014-05-16 23:26:12 +02001114#ifdef CONFIG_NEED_MULTIPLE_NODES
David S. Miller919ee672008-04-23 05:40:25 -07001115 int i;
Sam Ravnborg48d37212014-05-16 23:26:12 +02001116#endif
David S. Miller919ee672008-04-23 05:40:25 -07001117
1118 numadbg("Initializing tables for non-numa.\n");
1119
Pavel Tatashin1537b262017-02-16 15:05:58 -05001120 node_masks[0].mask = 0;
1121 node_masks[0].match = 0;
David S. Miller919ee672008-04-23 05:40:25 -07001122 num_node_masks = 1;
1123
Sam Ravnborg48d37212014-05-16 23:26:12 +02001124#ifdef CONFIG_NEED_MULTIPLE_NODES
David S. Miller919ee672008-04-23 05:40:25 -07001125 for (i = 0; i < NR_CPUS; i++)
1126 numa_cpu_lookup_table[i] = 0;
1127
KOSAKI Motohirofb1fece2011-05-16 13:38:07 -07001128 cpumask_setall(&numa_cpumask_lookup_table[0]);
Sam Ravnborg48d37212014-05-16 23:26:12 +02001129#endif
David S. Miller919ee672008-04-23 05:40:25 -07001130}
1131
1132#ifdef CONFIG_NEED_MULTIPLE_NODES
1133struct pglist_data *node_data[MAX_NUMNODES];
1134
1135EXPORT_SYMBOL(numa_cpu_lookup_table);
1136EXPORT_SYMBOL(numa_cpumask_lookup_table);
1137EXPORT_SYMBOL(node_data);
1138
David S. Miller919ee672008-04-23 05:40:25 -07001139static int scan_pio_for_cfg_handle(struct mdesc_handle *md, u64 pio,
1140 u32 cfg_handle)
1141{
1142 u64 arc;
1143
1144 mdesc_for_each_arc(arc, md, pio, MDESC_ARC_TYPE_FWD) {
1145 u64 target = mdesc_arc_target(md, arc);
1146 const u64 *val;
1147
1148 val = mdesc_get_property(md, target,
1149 "cfg-handle", NULL);
1150 if (val && *val == cfg_handle)
1151 return 0;
1152 }
1153 return -ENODEV;
1154}
1155
1156static int scan_arcs_for_cfg_handle(struct mdesc_handle *md, u64 grp,
1157 u32 cfg_handle)
1158{
1159 u64 arc, candidate, best_latency = ~(u64)0;
1160
1161 candidate = MDESC_NODE_NULL;
1162 mdesc_for_each_arc(arc, md, grp, MDESC_ARC_TYPE_FWD) {
1163 u64 target = mdesc_arc_target(md, arc);
1164 const char *name = mdesc_node_name(md, target);
1165 const u64 *val;
1166
1167 if (strcmp(name, "pio-latency-group"))
1168 continue;
1169
1170 val = mdesc_get_property(md, target, "latency", NULL);
1171 if (!val)
1172 continue;
1173
1174 if (*val < best_latency) {
1175 candidate = target;
1176 best_latency = *val;
1177 }
1178 }
1179
1180 if (candidate == MDESC_NODE_NULL)
1181 return -ENODEV;
1182
1183 return scan_pio_for_cfg_handle(md, candidate, cfg_handle);
1184}
1185
1186int of_node_to_nid(struct device_node *dp)
1187{
1188 const struct linux_prom64_registers *regs;
1189 struct mdesc_handle *md;
1190 u32 cfg_handle;
1191 int count, nid;
1192 u64 grp;
1193
David S. Miller072bd412008-08-18 20:36:17 -07001194 /* This is the right thing to do on currently supported
1195 * SUN4U NUMA platforms as well, as the PCI controller does
1196 * not sit behind any particular memory controller.
1197 */
David S. Miller919ee672008-04-23 05:40:25 -07001198 if (!mlgroups)
1199 return -1;
1200
1201 regs = of_get_property(dp, "reg", NULL);
1202 if (!regs)
1203 return -1;
1204
1205 cfg_handle = (regs->phys_addr >> 32UL) & 0x0fffffff;
1206
1207 md = mdesc_grab();
1208
1209 count = 0;
1210 nid = -1;
1211 mdesc_for_each_node_by_name(md, grp, "group") {
1212 if (!scan_arcs_for_cfg_handle(md, grp, cfg_handle)) {
1213 nid = count;
1214 break;
1215 }
1216 count++;
1217 }
1218
1219 mdesc_release(md);
1220
1221 return nid;
1222}
1223
David S. Miller01c453812009-04-07 01:05:22 -07001224static void __init add_node_ranges(void)
David S. Miller919ee672008-04-23 05:40:25 -07001225{
Benjamin Herrenschmidt08b84792010-08-04 13:43:31 +10001226 struct memblock_region *reg;
Pavel Tatashincd429ce2017-02-16 15:13:54 -05001227 unsigned long prev_max;
1228
1229memblock_resized:
1230 prev_max = memblock.memory.max;
David S. Miller919ee672008-04-23 05:40:25 -07001231
Benjamin Herrenschmidt08b84792010-08-04 13:43:31 +10001232 for_each_memblock(memory, reg) {
1233 unsigned long size = reg->size;
David S. Miller919ee672008-04-23 05:40:25 -07001234 unsigned long start, end;
1235
Benjamin Herrenschmidt08b84792010-08-04 13:43:31 +10001236 start = reg->base;
David S. Miller919ee672008-04-23 05:40:25 -07001237 end = start + size;
1238 while (start < end) {
1239 unsigned long this_end;
1240 int nid;
1241
Benjamin Herrenschmidt35a1f0b2010-07-06 15:38:58 -07001242 this_end = memblock_nid_range(start, end, &nid);
David S. Miller919ee672008-04-23 05:40:25 -07001243
Tejun Heo2a4814d2011-12-08 10:22:08 -08001244 numadbg("Setting memblock NUMA node nid[%d] "
David S. Miller919ee672008-04-23 05:40:25 -07001245 "start[%lx] end[%lx]\n",
1246 nid, start, this_end);
1247
Tang Chene7e8de52014-01-21 15:49:26 -08001248 memblock_set_node(start, this_end - start,
1249 &memblock.memory, nid);
Pavel Tatashincd429ce2017-02-16 15:13:54 -05001250 if (memblock.memory.max != prev_max)
1251 goto memblock_resized;
David S. Miller919ee672008-04-23 05:40:25 -07001252 start = this_end;
1253 }
1254 }
1255}
1256
1257static int __init grab_mlgroups(struct mdesc_handle *md)
1258{
1259 unsigned long paddr;
1260 int count = 0;
1261 u64 node;
1262
1263 mdesc_for_each_node_by_name(md, node, "memory-latency-group")
1264 count++;
1265 if (!count)
1266 return -ENOENT;
1267
Yinghai Lu95f72d12010-07-12 14:36:09 +10001268 paddr = memblock_alloc(count * sizeof(struct mdesc_mlgroup),
David S. Miller919ee672008-04-23 05:40:25 -07001269 SMP_CACHE_BYTES);
1270 if (!paddr)
1271 return -ENOMEM;
1272
1273 mlgroups = __va(paddr);
1274 num_mlgroups = count;
1275
1276 count = 0;
1277 mdesc_for_each_node_by_name(md, node, "memory-latency-group") {
1278 struct mdesc_mlgroup *m = &mlgroups[count++];
1279 const u64 *val;
1280
1281 m->node = node;
1282
1283 val = mdesc_get_property(md, node, "latency", NULL);
1284 m->latency = *val;
1285 val = mdesc_get_property(md, node, "address-match", NULL);
1286 m->match = *val;
1287 val = mdesc_get_property(md, node, "address-mask", NULL);
1288 m->mask = *val;
1289
Sam Ravnborg90181132009-01-06 13:19:28 -08001290 numadbg("MLGROUP[%d]: node[%llx] latency[%llx] "
1291 "match[%llx] mask[%llx]\n",
David S. Miller919ee672008-04-23 05:40:25 -07001292 count - 1, m->node, m->latency, m->match, m->mask);
1293 }
1294
1295 return 0;
1296}
1297
1298static int __init grab_mblocks(struct mdesc_handle *md)
1299{
1300 unsigned long paddr;
1301 int count = 0;
1302 u64 node;
1303
1304 mdesc_for_each_node_by_name(md, node, "mblock")
1305 count++;
1306 if (!count)
1307 return -ENOENT;
1308
Yinghai Lu95f72d12010-07-12 14:36:09 +10001309 paddr = memblock_alloc(count * sizeof(struct mdesc_mblock),
David S. Miller919ee672008-04-23 05:40:25 -07001310 SMP_CACHE_BYTES);
1311 if (!paddr)
1312 return -ENOMEM;
1313
1314 mblocks = __va(paddr);
1315 num_mblocks = count;
1316
1317 count = 0;
1318 mdesc_for_each_node_by_name(md, node, "mblock") {
1319 struct mdesc_mblock *m = &mblocks[count++];
1320 const u64 *val;
1321
1322 val = mdesc_get_property(md, node, "base", NULL);
1323 m->base = *val;
1324 val = mdesc_get_property(md, node, "size", NULL);
1325 m->size = *val;
1326 val = mdesc_get_property(md, node,
1327 "address-congruence-offset", NULL);
bob picco771a37f2013-06-11 14:54:51 -04001328
1329 /* The address-congruence-offset property is optional.
1330 * Explicity zero it be identifty this.
1331 */
1332 if (val)
1333 m->offset = *val;
1334 else
1335 m->offset = 0UL;
David S. Miller919ee672008-04-23 05:40:25 -07001336
Sam Ravnborg90181132009-01-06 13:19:28 -08001337 numadbg("MBLOCK[%d]: base[%llx] size[%llx] offset[%llx]\n",
David S. Miller919ee672008-04-23 05:40:25 -07001338 count - 1, m->base, m->size, m->offset);
1339 }
1340
1341 return 0;
1342}
1343
1344static void __init numa_parse_mdesc_group_cpus(struct mdesc_handle *md,
1345 u64 grp, cpumask_t *mask)
1346{
1347 u64 arc;
1348
KOSAKI Motohirofb1fece2011-05-16 13:38:07 -07001349 cpumask_clear(mask);
David S. Miller919ee672008-04-23 05:40:25 -07001350
1351 mdesc_for_each_arc(arc, md, grp, MDESC_ARC_TYPE_BACK) {
1352 u64 target = mdesc_arc_target(md, arc);
1353 const char *name = mdesc_node_name(md, target);
1354 const u64 *id;
1355
1356 if (strcmp(name, "cpu"))
1357 continue;
1358 id = mdesc_get_property(md, target, "id", NULL);
Rusty Russelle305cb8f2009-03-16 14:40:23 +10301359 if (*id < nr_cpu_ids)
KOSAKI Motohirofb1fece2011-05-16 13:38:07 -07001360 cpumask_set_cpu(*id, mask);
David S. Miller919ee672008-04-23 05:40:25 -07001361 }
1362}
1363
1364static struct mdesc_mlgroup * __init find_mlgroup(u64 node)
1365{
1366 int i;
1367
1368 for (i = 0; i < num_mlgroups; i++) {
1369 struct mdesc_mlgroup *m = &mlgroups[i];
1370 if (m->node == node)
1371 return m;
1372 }
1373 return NULL;
1374}
1375
Nitin Gupta52708d62015-11-02 16:30:24 -05001376int __node_distance(int from, int to)
1377{
1378 if ((from >= MAX_NUMNODES) || (to >= MAX_NUMNODES)) {
1379 pr_warn("Returning default NUMA distance value for %d->%d\n",
1380 from, to);
1381 return (from == to) ? LOCAL_DISTANCE : REMOTE_DISTANCE;
1382 }
1383 return numa_latency[from][to];
1384}
1385
Paul Gortmakerbdf2f592016-08-06 00:31:48 -04001386static int __init find_best_numa_node_for_mlgroup(struct mdesc_mlgroup *grp)
Nitin Gupta52708d62015-11-02 16:30:24 -05001387{
1388 int i;
1389
1390 for (i = 0; i < MAX_NUMNODES; i++) {
1391 struct node_mem_mask *n = &node_masks[i];
1392
Pavel Tatashin1537b262017-02-16 15:05:58 -05001393 if ((grp->mask == n->mask) && (grp->match == n->match))
Nitin Gupta52708d62015-11-02 16:30:24 -05001394 break;
1395 }
1396 return i;
1397}
1398
Paul Gortmakerbdf2f592016-08-06 00:31:48 -04001399static void __init find_numa_latencies_for_group(struct mdesc_handle *md,
1400 u64 grp, int index)
Nitin Gupta52708d62015-11-02 16:30:24 -05001401{
1402 u64 arc;
1403
1404 mdesc_for_each_arc(arc, md, grp, MDESC_ARC_TYPE_FWD) {
1405 int tnode;
1406 u64 target = mdesc_arc_target(md, arc);
1407 struct mdesc_mlgroup *m = find_mlgroup(target);
1408
1409 if (!m)
1410 continue;
1411 tnode = find_best_numa_node_for_mlgroup(m);
1412 if (tnode == MAX_NUMNODES)
1413 continue;
1414 numa_latency[index][tnode] = m->latency;
1415 }
1416}
1417
David S. Miller919ee672008-04-23 05:40:25 -07001418static int __init numa_attach_mlgroup(struct mdesc_handle *md, u64 grp,
1419 int index)
1420{
1421 struct mdesc_mlgroup *candidate = NULL;
1422 u64 arc, best_latency = ~(u64)0;
1423 struct node_mem_mask *n;
1424
1425 mdesc_for_each_arc(arc, md, grp, MDESC_ARC_TYPE_FWD) {
1426 u64 target = mdesc_arc_target(md, arc);
1427 struct mdesc_mlgroup *m = find_mlgroup(target);
1428 if (!m)
1429 continue;
1430 if (m->latency < best_latency) {
1431 candidate = m;
1432 best_latency = m->latency;
1433 }
1434 }
1435 if (!candidate)
1436 return -ENOENT;
1437
1438 if (num_node_masks != index) {
1439 printk(KERN_ERR "Inconsistent NUMA state, "
1440 "index[%d] != num_node_masks[%d]\n",
1441 index, num_node_masks);
1442 return -EINVAL;
1443 }
1444
1445 n = &node_masks[num_node_masks++];
1446
1447 n->mask = candidate->mask;
Pavel Tatashin1537b262017-02-16 15:05:58 -05001448 n->match = candidate->match;
David S. Miller919ee672008-04-23 05:40:25 -07001449
Pavel Tatashin1537b262017-02-16 15:05:58 -05001450 numadbg("NUMA NODE[%d]: mask[%lx] match[%lx] (latency[%llx])\n",
1451 index, n->mask, n->match, candidate->latency);
David S. Miller919ee672008-04-23 05:40:25 -07001452
1453 return 0;
1454}
1455
1456static int __init numa_parse_mdesc_group(struct mdesc_handle *md, u64 grp,
1457 int index)
1458{
1459 cpumask_t mask;
1460 int cpu;
1461
1462 numa_parse_mdesc_group_cpus(md, grp, &mask);
1463
KOSAKI Motohirofb1fece2011-05-16 13:38:07 -07001464 for_each_cpu(cpu, &mask)
David S. Miller919ee672008-04-23 05:40:25 -07001465 numa_cpu_lookup_table[cpu] = index;
KOSAKI Motohirofb1fece2011-05-16 13:38:07 -07001466 cpumask_copy(&numa_cpumask_lookup_table[index], &mask);
David S. Miller919ee672008-04-23 05:40:25 -07001467
1468 if (numa_debug) {
1469 printk(KERN_INFO "NUMA GROUP[%d]: cpus [ ", index);
KOSAKI Motohirofb1fece2011-05-16 13:38:07 -07001470 for_each_cpu(cpu, &mask)
David S. Miller919ee672008-04-23 05:40:25 -07001471 printk("%d ", cpu);
1472 printk("]\n");
1473 }
1474
1475 return numa_attach_mlgroup(md, grp, index);
1476}
1477
1478static int __init numa_parse_mdesc(void)
1479{
1480 struct mdesc_handle *md = mdesc_grab();
Nitin Gupta52708d62015-11-02 16:30:24 -05001481 int i, j, err, count;
David S. Miller919ee672008-04-23 05:40:25 -07001482 u64 node;
1483
1484 node = mdesc_node_by_name(md, MDESC_NODE_NULL, "latency-groups");
1485 if (node == MDESC_NODE_NULL) {
1486 mdesc_release(md);
1487 return -ENOENT;
1488 }
1489
1490 err = grab_mblocks(md);
1491 if (err < 0)
1492 goto out;
1493
1494 err = grab_mlgroups(md);
1495 if (err < 0)
1496 goto out;
1497
1498 count = 0;
1499 mdesc_for_each_node_by_name(md, node, "group") {
1500 err = numa_parse_mdesc_group(md, node, count);
1501 if (err < 0)
1502 break;
1503 count++;
1504 }
1505
Nitin Gupta52708d62015-11-02 16:30:24 -05001506 count = 0;
1507 mdesc_for_each_node_by_name(md, node, "group") {
1508 find_numa_latencies_for_group(md, node, count);
1509 count++;
1510 }
1511
1512 /* Normalize numa latency matrix according to ACPI SLIT spec. */
1513 for (i = 0; i < MAX_NUMNODES; i++) {
1514 u64 self_latency = numa_latency[i][i];
1515
1516 for (j = 0; j < MAX_NUMNODES; j++) {
1517 numa_latency[i][j] =
1518 (numa_latency[i][j] * LOCAL_DISTANCE) /
1519 self_latency;
1520 }
1521 }
1522
David S. Miller919ee672008-04-23 05:40:25 -07001523 add_node_ranges();
1524
1525 for (i = 0; i < num_node_masks; i++) {
1526 allocate_node_data(i);
1527 node_set_online(i);
1528 }
1529
1530 err = 0;
1531out:
1532 mdesc_release(md);
1533 return err;
1534}
1535
David S. Miller072bd412008-08-18 20:36:17 -07001536static int __init numa_parse_jbus(void)
1537{
1538 unsigned long cpu, index;
1539
1540 /* NUMA node id is encoded in bits 36 and higher, and there is
1541 * a 1-to-1 mapping from CPU ID to NUMA node ID.
1542 */
1543 index = 0;
1544 for_each_present_cpu(cpu) {
1545 numa_cpu_lookup_table[cpu] = index;
KOSAKI Motohirofb1fece2011-05-16 13:38:07 -07001546 cpumask_copy(&numa_cpumask_lookup_table[index], cpumask_of(cpu));
David S. Miller072bd412008-08-18 20:36:17 -07001547 node_masks[index].mask = ~((1UL << 36UL) - 1UL);
Pavel Tatashin1537b262017-02-16 15:05:58 -05001548 node_masks[index].match = cpu << 36UL;
David S. Miller072bd412008-08-18 20:36:17 -07001549
1550 index++;
1551 }
1552 num_node_masks = index;
1553
1554 add_node_ranges();
1555
1556 for (index = 0; index < num_node_masks; index++) {
1557 allocate_node_data(index);
1558 node_set_online(index);
1559 }
1560
1561 return 0;
1562}
1563
David S. Miller919ee672008-04-23 05:40:25 -07001564static int __init numa_parse_sun4u(void)
1565{
David S. Miller072bd412008-08-18 20:36:17 -07001566 if (tlb_type == cheetah || tlb_type == cheetah_plus) {
1567 unsigned long ver;
1568
1569 __asm__ ("rdpr %%ver, %0" : "=r" (ver));
1570 if ((ver >> 32UL) == __JALAPENO_ID ||
1571 (ver >> 32UL) == __SERRANO_ID)
1572 return numa_parse_jbus();
1573 }
David S. Miller919ee672008-04-23 05:40:25 -07001574 return -1;
1575}
1576
1577static int __init bootmem_init_numa(void)
1578{
Nitin Gupta36beca62016-01-05 22:35:35 -08001579 int i, j;
David S. Miller919ee672008-04-23 05:40:25 -07001580 int err = -1;
1581
1582 numadbg("bootmem_init_numa()\n");
1583
Nitin Gupta36beca62016-01-05 22:35:35 -08001584 /* Some sane defaults for numa latency values */
1585 for (i = 0; i < MAX_NUMNODES; i++) {
1586 for (j = 0; j < MAX_NUMNODES; j++)
1587 numa_latency[i][j] = (i == j) ?
1588 LOCAL_DISTANCE : REMOTE_DISTANCE;
1589 }
1590
David S. Miller919ee672008-04-23 05:40:25 -07001591 if (numa_enabled) {
1592 if (tlb_type == hypervisor)
1593 err = numa_parse_mdesc();
1594 else
1595 err = numa_parse_sun4u();
1596 }
1597 return err;
1598}
1599
1600#else
1601
1602static int bootmem_init_numa(void)
1603{
1604 return -1;
1605}
1606
1607#endif
1608
1609static void __init bootmem_init_nonnuma(void)
1610{
Yinghai Lu95f72d12010-07-12 14:36:09 +10001611 unsigned long top_of_ram = memblock_end_of_DRAM();
1612 unsigned long total_ram = memblock_phys_mem_size();
David S. Miller919ee672008-04-23 05:40:25 -07001613
1614 numadbg("bootmem_init_nonnuma()\n");
1615
1616 printk(KERN_INFO "Top of RAM: 0x%lx, Total RAM: 0x%lx\n",
1617 top_of_ram, total_ram);
1618 printk(KERN_INFO "Memory hole size: %ldMB\n",
1619 (top_of_ram - total_ram) >> 20);
1620
1621 init_node_masks_nonnuma();
Tang Chene7e8de52014-01-21 15:49:26 -08001622 memblock_set_node(0, (phys_addr_t)ULLONG_MAX, &memblock.memory, 0);
David S. Miller919ee672008-04-23 05:40:25 -07001623 allocate_node_data(0);
David S. Miller919ee672008-04-23 05:40:25 -07001624 node_set_online(0);
1625}
1626
David S. Miller919ee672008-04-23 05:40:25 -07001627static unsigned long __init bootmem_init(unsigned long phys_base)
1628{
1629 unsigned long end_pfn;
David S. Miller919ee672008-04-23 05:40:25 -07001630
Yinghai Lu95f72d12010-07-12 14:36:09 +10001631 end_pfn = memblock_end_of_DRAM() >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 max_pfn = max_low_pfn = end_pfn;
David S. Millerd1112012006-03-08 02:16:07 -08001633 min_low_pfn = (phys_base >> PAGE_SHIFT);
1634
David S. Miller919ee672008-04-23 05:40:25 -07001635 if (bootmem_init_numa() < 0)
1636 bootmem_init_nonnuma();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
David S. Miller625d6932012-04-25 13:13:43 -07001638 /* Dump memblock with node info. */
1639 memblock_dump_all();
1640
David S. Miller919ee672008-04-23 05:40:25 -07001641 /* XXX cpu notifier XXX */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
David S. Miller625d6932012-04-25 13:13:43 -07001643 sparse_memory_present_with_active_regions(MAX_NUMNODES);
David S. Millerd1112012006-03-08 02:16:07 -08001644 sparse_init();
1645
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 return end_pfn;
1647}
1648
David S. Miller9cc3a1a2006-02-21 20:51:13 -08001649static struct linux_prom64_registers pall[MAX_BANKS] __initdata;
1650static int pall_ents __initdata;
1651
David S. Miller0dd5b7b2014-09-24 20:56:11 -07001652static unsigned long max_phys_bits = 40;
1653
1654bool kern_addr_valid(unsigned long addr)
1655{
David S. Miller0dd5b7b2014-09-24 20:56:11 -07001656 pgd_t *pgd;
1657 pud_t *pud;
1658 pmd_t *pmd;
1659 pte_t *pte;
1660
David S. Millerbb4e6e82014-09-27 11:05:21 -07001661 if ((long)addr < 0L) {
1662 unsigned long pa = __pa(addr);
1663
bob piccoadfae8a2017-03-10 14:31:19 -05001664 if ((pa >> max_phys_bits) != 0UL)
David S. Millerbb4e6e82014-09-27 11:05:21 -07001665 return false;
1666
1667 return pfn_valid(pa >> PAGE_SHIFT);
1668 }
David S. Miller0dd5b7b2014-09-24 20:56:11 -07001669
1670 if (addr >= (unsigned long) KERNBASE &&
1671 addr < (unsigned long)&_end)
1672 return true;
1673
David S. Miller0dd5b7b2014-09-24 20:56:11 -07001674 pgd = pgd_offset_k(addr);
1675 if (pgd_none(*pgd))
1676 return 0;
1677
1678 pud = pud_offset(pgd, addr);
1679 if (pud_none(*pud))
1680 return 0;
1681
1682 if (pud_large(*pud))
1683 return pfn_valid(pud_pfn(*pud));
1684
1685 pmd = pmd_offset(pud, addr);
1686 if (pmd_none(*pmd))
1687 return 0;
1688
1689 if (pmd_large(*pmd))
1690 return pfn_valid(pmd_pfn(*pmd));
1691
1692 pte = pte_offset_kernel(pmd, addr);
1693 if (pte_none(*pte))
1694 return 0;
1695
1696 return pfn_valid(pte_pfn(*pte));
1697}
1698EXPORT_SYMBOL(kern_addr_valid);
1699
1700static unsigned long __ref kernel_map_hugepud(unsigned long vstart,
1701 unsigned long vend,
1702 pud_t *pud)
1703{
1704 const unsigned long mask16gb = (1UL << 34) - 1UL;
1705 u64 pte_val = vstart;
1706
1707 /* Each PUD is 8GB */
1708 if ((vstart & mask16gb) ||
1709 (vend - vstart <= mask16gb)) {
1710 pte_val ^= kern_linear_pte_xor[2];
1711 pud_val(*pud) = pte_val | _PAGE_PUD_HUGE;
1712
1713 return vstart + PUD_SIZE;
1714 }
1715
1716 pte_val ^= kern_linear_pte_xor[3];
1717 pte_val |= _PAGE_PUD_HUGE;
1718
1719 vend = vstart + mask16gb + 1UL;
1720 while (vstart < vend) {
1721 pud_val(*pud) = pte_val;
1722
1723 pte_val += PUD_SIZE;
1724 vstart += PUD_SIZE;
1725 pud++;
1726 }
1727 return vstart;
1728}
1729
1730static bool kernel_can_map_hugepud(unsigned long vstart, unsigned long vend,
1731 bool guard)
1732{
1733 if (guard && !(vstart & ~PUD_MASK) && (vend - vstart) >= PUD_SIZE)
1734 return true;
1735
1736 return false;
1737}
1738
1739static unsigned long __ref kernel_map_hugepmd(unsigned long vstart,
1740 unsigned long vend,
1741 pmd_t *pmd)
1742{
1743 const unsigned long mask256mb = (1UL << 28) - 1UL;
1744 const unsigned long mask2gb = (1UL << 31) - 1UL;
1745 u64 pte_val = vstart;
1746
1747 /* Each PMD is 8MB */
1748 if ((vstart & mask256mb) ||
1749 (vend - vstart <= mask256mb)) {
1750 pte_val ^= kern_linear_pte_xor[0];
1751 pmd_val(*pmd) = pte_val | _PAGE_PMD_HUGE;
1752
1753 return vstart + PMD_SIZE;
1754 }
1755
1756 if ((vstart & mask2gb) ||
1757 (vend - vstart <= mask2gb)) {
1758 pte_val ^= kern_linear_pte_xor[1];
1759 pte_val |= _PAGE_PMD_HUGE;
1760 vend = vstart + mask256mb + 1UL;
1761 } else {
1762 pte_val ^= kern_linear_pte_xor[2];
1763 pte_val |= _PAGE_PMD_HUGE;
1764 vend = vstart + mask2gb + 1UL;
1765 }
1766
1767 while (vstart < vend) {
1768 pmd_val(*pmd) = pte_val;
1769
1770 pte_val += PMD_SIZE;
1771 vstart += PMD_SIZE;
1772 pmd++;
1773 }
1774
1775 return vstart;
1776}
1777
1778static bool kernel_can_map_hugepmd(unsigned long vstart, unsigned long vend,
1779 bool guard)
1780{
1781 if (guard && !(vstart & ~PMD_MASK) && (vend - vstart) >= PMD_SIZE)
1782 return true;
1783
1784 return false;
1785}
1786
Sam Ravnborg896aef42008-02-24 19:49:52 -08001787static unsigned long __ref kernel_map_range(unsigned long pstart,
David S. Miller0dd5b7b2014-09-24 20:56:11 -07001788 unsigned long pend, pgprot_t prot,
1789 bool use_huge)
David S. Miller56425302005-09-25 16:46:57 -07001790{
1791 unsigned long vstart = PAGE_OFFSET + pstart;
1792 unsigned long vend = PAGE_OFFSET + pend;
1793 unsigned long alloc_bytes = 0UL;
1794
1795 if ((vstart & ~PAGE_MASK) || (vend & ~PAGE_MASK)) {
David S. Miller13edad72005-09-29 17:58:26 -07001796 prom_printf("kernel_map: Unaligned physmem[%lx:%lx]\n",
David S. Miller56425302005-09-25 16:46:57 -07001797 vstart, vend);
1798 prom_halt();
1799 }
1800
1801 while (vstart < vend) {
1802 unsigned long this_end, paddr = __pa(vstart);
1803 pgd_t *pgd = pgd_offset_k(vstart);
1804 pud_t *pud;
1805 pmd_t *pmd;
1806 pte_t *pte;
1807
David S. Millerac55c762014-09-26 21:19:46 -07001808 if (pgd_none(*pgd)) {
1809 pud_t *new;
1810
1811 new = __alloc_bootmem(PAGE_SIZE, PAGE_SIZE, PAGE_SIZE);
1812 alloc_bytes += PAGE_SIZE;
1813 pgd_populate(&init_mm, pgd, new);
1814 }
David S. Miller56425302005-09-25 16:46:57 -07001815 pud = pud_offset(pgd, vstart);
1816 if (pud_none(*pud)) {
1817 pmd_t *new;
1818
David S. Miller0dd5b7b2014-09-24 20:56:11 -07001819 if (kernel_can_map_hugepud(vstart, vend, use_huge)) {
1820 vstart = kernel_map_hugepud(vstart, vend, pud);
1821 continue;
1822 }
David S. Miller56425302005-09-25 16:46:57 -07001823 new = __alloc_bootmem(PAGE_SIZE, PAGE_SIZE, PAGE_SIZE);
1824 alloc_bytes += PAGE_SIZE;
1825 pud_populate(&init_mm, pud, new);
1826 }
1827
1828 pmd = pmd_offset(pud, vstart);
David S. Miller0dd5b7b2014-09-24 20:56:11 -07001829 if (pmd_none(*pmd)) {
David S. Miller56425302005-09-25 16:46:57 -07001830 pte_t *new;
1831
David S. Miller0dd5b7b2014-09-24 20:56:11 -07001832 if (kernel_can_map_hugepmd(vstart, vend, use_huge)) {
1833 vstart = kernel_map_hugepmd(vstart, vend, pmd);
1834 continue;
1835 }
David S. Miller56425302005-09-25 16:46:57 -07001836 new = __alloc_bootmem(PAGE_SIZE, PAGE_SIZE, PAGE_SIZE);
1837 alloc_bytes += PAGE_SIZE;
1838 pmd_populate_kernel(&init_mm, pmd, new);
1839 }
1840
1841 pte = pte_offset_kernel(pmd, vstart);
1842 this_end = (vstart + PMD_SIZE) & PMD_MASK;
1843 if (this_end > vend)
1844 this_end = vend;
1845
1846 while (vstart < this_end) {
1847 pte_val(*pte) = (paddr | pgprot_val(prot));
1848
1849 vstart += PAGE_SIZE;
1850 paddr += PAGE_SIZE;
1851 pte++;
1852 }
1853 }
1854
1855 return alloc_bytes;
1856}
1857
David S. Miller0dd5b7b2014-09-24 20:56:11 -07001858static void __init flush_all_kernel_tsbs(void)
1859{
1860 int i;
1861
1862 for (i = 0; i < KERNEL_TSB_NENTRIES; i++) {
1863 struct tsb *ent = &swapper_tsb[i];
1864
1865 ent->tag = (1UL << TSB_TAG_INVALID_BIT);
1866 }
1867#ifndef CONFIG_DEBUG_PAGEALLOC
1868 for (i = 0; i < KERNEL_TSB4M_NENTRIES; i++) {
1869 struct tsb *ent = &swapper_4m_tsb[i];
1870
1871 ent->tag = (1UL << TSB_TAG_INVALID_BIT);
1872 }
1873#endif
1874}
1875
David S. Miller56425302005-09-25 16:46:57 -07001876extern unsigned int kvmap_linear_patch[1];
David S. Miller9cc3a1a2006-02-21 20:51:13 -08001877
David S. Miller8f3614532007-12-13 06:13:38 -08001878static void __init kernel_physical_mapping_init(void)
1879{
David S. Miller8f3614532007-12-13 06:13:38 -08001880 unsigned long i, mem_alloced = 0UL;
David S. Miller0dd5b7b2014-09-24 20:56:11 -07001881 bool use_huge = true;
David S. Miller8f3614532007-12-13 06:13:38 -08001882
David S. Miller0dd5b7b2014-09-24 20:56:11 -07001883#ifdef CONFIG_DEBUG_PAGEALLOC
1884 use_huge = false;
1885#endif
David S. Miller8f3614532007-12-13 06:13:38 -08001886 for (i = 0; i < pall_ents; i++) {
1887 unsigned long phys_start, phys_end;
1888
1889 phys_start = pall[i].phys_addr;
1890 phys_end = phys_start + pall[i].reg_size;
1891
David S. Miller56425302005-09-25 16:46:57 -07001892 mem_alloced += kernel_map_range(phys_start, phys_end,
David S. Miller0dd5b7b2014-09-24 20:56:11 -07001893 PAGE_KERNEL, use_huge);
David S. Miller56425302005-09-25 16:46:57 -07001894 }
1895
1896 printk("Allocated %ld bytes for kernel page tables.\n",
1897 mem_alloced);
1898
1899 kvmap_linear_patch[0] = 0x01000000; /* nop */
1900 flushi(&kvmap_linear_patch[0]);
1901
David S. Miller0dd5b7b2014-09-24 20:56:11 -07001902 flush_all_kernel_tsbs();
1903
David S. Miller56425302005-09-25 16:46:57 -07001904 __flush_tlb_all();
1905}
1906
David S. Miller9cc3a1a2006-02-21 20:51:13 -08001907#ifdef CONFIG_DEBUG_PAGEALLOC
Joonsoo Kim031bc572014-12-12 16:55:52 -08001908void __kernel_map_pages(struct page *page, int numpages, int enable)
David S. Miller56425302005-09-25 16:46:57 -07001909{
1910 unsigned long phys_start = page_to_pfn(page) << PAGE_SHIFT;
1911 unsigned long phys_end = phys_start + (numpages * PAGE_SIZE);
1912
1913 kernel_map_range(phys_start, phys_end,
David S. Miller0dd5b7b2014-09-24 20:56:11 -07001914 (enable ? PAGE_KERNEL : __pgprot(0)), false);
David S. Miller56425302005-09-25 16:46:57 -07001915
David S. Miller74bf4312006-01-31 18:29:18 -08001916 flush_tsb_kernel_range(PAGE_OFFSET + phys_start,
1917 PAGE_OFFSET + phys_end);
1918
David S. Miller56425302005-09-25 16:46:57 -07001919 /* we should perform an IPI and flush all tlbs,
1920 * but that can deadlock->flush only current cpu.
1921 */
1922 __flush_tlb_kernel_range(PAGE_OFFSET + phys_start,
1923 PAGE_OFFSET + phys_end);
1924}
1925#endif
1926
David S. Miller10147572005-09-28 21:46:43 -07001927unsigned long __init find_ecache_flush_span(unsigned long size)
1928{
David S. Miller13edad72005-09-29 17:58:26 -07001929 int i;
David S. Miller10147572005-09-28 21:46:43 -07001930
David S. Miller13edad72005-09-29 17:58:26 -07001931 for (i = 0; i < pavail_ents; i++) {
1932 if (pavail[i].reg_size >= size)
1933 return pavail[i].phys_addr;
David S. Miller10147572005-09-28 21:46:43 -07001934 }
1935
1936 return ~0UL;
1937}
1938
David S. Millerb2d43832013-09-20 21:50:41 -07001939unsigned long PAGE_OFFSET;
1940EXPORT_SYMBOL(PAGE_OFFSET);
1941
David S. Millerbb4e6e82014-09-27 11:05:21 -07001942unsigned long VMALLOC_END = 0x0000010000000000UL;
1943EXPORT_SYMBOL(VMALLOC_END);
1944
David S. Miller4397bed2014-09-26 21:58:33 -07001945unsigned long sparc64_va_hole_top = 0xfffff80000000000UL;
1946unsigned long sparc64_va_hole_bottom = 0x0000080000000000UL;
1947
David S. Millerb2d43832013-09-20 21:50:41 -07001948static void __init setup_page_offset(void)
1949{
David S. Millerb2d43832013-09-20 21:50:41 -07001950 if (tlb_type == cheetah || tlb_type == cheetah_plus) {
David S. Miller4397bed2014-09-26 21:58:33 -07001951 /* Cheetah/Panther support a full 64-bit virtual
1952 * address, so we can use all that our page tables
1953 * support.
1954 */
1955 sparc64_va_hole_top = 0xfff0000000000000UL;
1956 sparc64_va_hole_bottom = 0x0010000000000000UL;
1957
David S. Millerb2d43832013-09-20 21:50:41 -07001958 max_phys_bits = 42;
1959 } else if (tlb_type == hypervisor) {
1960 switch (sun4v_chip_type) {
1961 case SUN4V_CHIP_NIAGARA1:
1962 case SUN4V_CHIP_NIAGARA2:
David S. Miller4397bed2014-09-26 21:58:33 -07001963 /* T1 and T2 support 48-bit virtual addresses. */
1964 sparc64_va_hole_top = 0xffff800000000000UL;
1965 sparc64_va_hole_bottom = 0x0000800000000000UL;
1966
David S. Millerb2d43832013-09-20 21:50:41 -07001967 max_phys_bits = 39;
1968 break;
1969 case SUN4V_CHIP_NIAGARA3:
David S. Miller4397bed2014-09-26 21:58:33 -07001970 /* T3 supports 48-bit virtual addresses. */
1971 sparc64_va_hole_top = 0xffff800000000000UL;
1972 sparc64_va_hole_bottom = 0x0000800000000000UL;
1973
David S. Millerb2d43832013-09-20 21:50:41 -07001974 max_phys_bits = 43;
1975 break;
1976 case SUN4V_CHIP_NIAGARA4:
1977 case SUN4V_CHIP_NIAGARA5:
1978 case SUN4V_CHIP_SPARC64X:
David S. Miller7c0fa0f2014-09-24 21:49:29 -07001979 case SUN4V_CHIP_SPARC_M6:
David S. Miller4397bed2014-09-26 21:58:33 -07001980 /* T4 and later support 52-bit virtual addresses. */
1981 sparc64_va_hole_top = 0xfff8000000000000UL;
1982 sparc64_va_hole_bottom = 0x0008000000000000UL;
David S. Millerb2d43832013-09-20 21:50:41 -07001983 max_phys_bits = 47;
1984 break;
David S. Miller7c0fa0f2014-09-24 21:49:29 -07001985 case SUN4V_CHIP_SPARC_M7:
Khalid Azizc5b8b5b2016-04-19 11:12:54 -06001986 case SUN4V_CHIP_SPARC_SN:
David S. Miller7c0fa0f2014-09-24 21:49:29 -07001987 /* M7 and later support 52-bit virtual addresses. */
1988 sparc64_va_hole_top = 0xfff8000000000000UL;
1989 sparc64_va_hole_bottom = 0x0008000000000000UL;
1990 max_phys_bits = 49;
1991 break;
Vijay Kumarfdaccf72017-07-28 19:29:32 -06001992 case SUN4V_CHIP_SPARC_M8:
1993 default:
1994 /* M8 and later support 54-bit virtual addresses.
1995 * However, restricting M8 and above VA bits to 53
1996 * as 4-level page table cannot support more than
1997 * 53 VA bits.
1998 */
1999 sparc64_va_hole_top = 0xfff0000000000000UL;
2000 sparc64_va_hole_bottom = 0x0010000000000000UL;
2001 max_phys_bits = 51;
2002 break;
David S. Millerb2d43832013-09-20 21:50:41 -07002003 }
2004 }
2005
2006 if (max_phys_bits > MAX_PHYS_ADDRESS_BITS) {
2007 prom_printf("MAX_PHYS_ADDRESS_BITS is too small, need %lu\n",
2008 max_phys_bits);
2009 prom_halt();
2010 }
2011
David S. Millerbb4e6e82014-09-27 11:05:21 -07002012 PAGE_OFFSET = sparc64_va_hole_top;
2013 VMALLOC_END = ((sparc64_va_hole_bottom >> 1) +
2014 (sparc64_va_hole_bottom >> 2));
David S. Millerb2d43832013-09-20 21:50:41 -07002015
David S. Millerbb4e6e82014-09-27 11:05:21 -07002016 pr_info("MM: PAGE_OFFSET is 0x%016lx (max_phys_bits == %lu)\n",
David S. Millerb2d43832013-09-20 21:50:41 -07002017 PAGE_OFFSET, max_phys_bits);
David S. Millerbb4e6e82014-09-27 11:05:21 -07002018 pr_info("MM: VMALLOC [0x%016lx --> 0x%016lx]\n",
2019 VMALLOC_START, VMALLOC_END);
2020 pr_info("MM: VMEMMAP [0x%016lx --> 0x%016lx]\n",
2021 VMEMMAP_BASE, VMEMMAP_BASE << 1);
David S. Millerb2d43832013-09-20 21:50:41 -07002022}
2023
David S. Miller517af332006-02-01 15:55:21 -08002024static void __init tsb_phys_patch(void)
2025{
David S. Millerd257d5d2006-02-06 23:44:37 -08002026 struct tsb_ldquad_phys_patch_entry *pquad;
David S. Miller517af332006-02-01 15:55:21 -08002027 struct tsb_phys_patch_entry *p;
2028
David S. Millerd257d5d2006-02-06 23:44:37 -08002029 pquad = &__tsb_ldquad_phys_patch;
2030 while (pquad < &__tsb_ldquad_phys_patch_end) {
2031 unsigned long addr = pquad->addr;
2032
2033 if (tlb_type == hypervisor)
2034 *(unsigned int *) addr = pquad->sun4v_insn;
2035 else
2036 *(unsigned int *) addr = pquad->sun4u_insn;
2037 wmb();
2038 __asm__ __volatile__("flush %0"
2039 : /* no outputs */
2040 : "r" (addr));
2041
2042 pquad++;
2043 }
2044
David S. Miller517af332006-02-01 15:55:21 -08002045 p = &__tsb_phys_patch;
2046 while (p < &__tsb_phys_patch_end) {
2047 unsigned long addr = p->addr;
2048
2049 *(unsigned int *) addr = p->insn;
2050 wmb();
2051 __asm__ __volatile__("flush %0"
2052 : /* no outputs */
2053 : "r" (addr));
2054
2055 p++;
2056 }
2057}
2058
David S. Miller490384e2006-02-11 14:41:18 -08002059/* Don't mark as init, we give this to the Hypervisor. */
David S. Millerd1acb422007-03-16 17:20:28 -07002060#ifndef CONFIG_DEBUG_PAGEALLOC
2061#define NUM_KTSB_DESCR 2
2062#else
2063#define NUM_KTSB_DESCR 1
2064#endif
2065static struct hv_tsb_descr ktsb_descr[NUM_KTSB_DESCR];
David S. Miller490384e2006-02-11 14:41:18 -08002066
David S. Miller8c82dc02014-09-17 10:14:56 -07002067/* The swapper TSBs are loaded with a base sequence of:
2068 *
2069 * sethi %uhi(SYMBOL), REG1
2070 * sethi %hi(SYMBOL), REG2
2071 * or REG1, %ulo(SYMBOL), REG1
2072 * or REG2, %lo(SYMBOL), REG2
2073 * sllx REG1, 32, REG1
2074 * or REG1, REG2, REG1
2075 *
2076 * When we use physical addressing for the TSB accesses, we patch the
2077 * first four instructions in the above sequence.
2078 */
2079
David S. Miller9076d0e2011-08-05 00:53:57 -07002080static void patch_one_ktsb_phys(unsigned int *start, unsigned int *end, unsigned long pa)
2081{
David S. Miller8c82dc02014-09-17 10:14:56 -07002082 unsigned long high_bits, low_bits;
2083
2084 high_bits = (pa >> 32) & 0xffffffff;
2085 low_bits = (pa >> 0) & 0xffffffff;
David S. Miller9076d0e2011-08-05 00:53:57 -07002086
2087 while (start < end) {
2088 unsigned int *ia = (unsigned int *)(unsigned long)*start;
2089
David S. Miller8c82dc02014-09-17 10:14:56 -07002090 ia[0] = (ia[0] & ~0x3fffff) | (high_bits >> 10);
David S. Miller9076d0e2011-08-05 00:53:57 -07002091 __asm__ __volatile__("flush %0" : : "r" (ia));
2092
David S. Miller8c82dc02014-09-17 10:14:56 -07002093 ia[1] = (ia[1] & ~0x3fffff) | (low_bits >> 10);
David S. Miller9076d0e2011-08-05 00:53:57 -07002094 __asm__ __volatile__("flush %0" : : "r" (ia + 1));
2095
David S. Miller8c82dc02014-09-17 10:14:56 -07002096 ia[2] = (ia[2] & ~0x1fff) | (high_bits & 0x3ff);
2097 __asm__ __volatile__("flush %0" : : "r" (ia + 2));
2098
2099 ia[3] = (ia[3] & ~0x1fff) | (low_bits & 0x3ff);
2100 __asm__ __volatile__("flush %0" : : "r" (ia + 3));
2101
David S. Miller9076d0e2011-08-05 00:53:57 -07002102 start++;
2103 }
2104}
2105
2106static void ktsb_phys_patch(void)
2107{
2108 extern unsigned int __swapper_tsb_phys_patch;
2109 extern unsigned int __swapper_tsb_phys_patch_end;
David S. Miller9076d0e2011-08-05 00:53:57 -07002110 unsigned long ktsb_pa;
2111
2112 ktsb_pa = kern_base + ((unsigned long)&swapper_tsb[0] - KERNBASE);
2113 patch_one_ktsb_phys(&__swapper_tsb_phys_patch,
2114 &__swapper_tsb_phys_patch_end, ktsb_pa);
2115#ifndef CONFIG_DEBUG_PAGEALLOC
David S. Miller0785a8e2011-08-06 05:26:35 -07002116 {
2117 extern unsigned int __swapper_4m_tsb_phys_patch;
2118 extern unsigned int __swapper_4m_tsb_phys_patch_end;
David S. Miller9076d0e2011-08-05 00:53:57 -07002119 ktsb_pa = (kern_base +
2120 ((unsigned long)&swapper_4m_tsb[0] - KERNBASE));
2121 patch_one_ktsb_phys(&__swapper_4m_tsb_phys_patch,
2122 &__swapper_4m_tsb_phys_patch_end, ktsb_pa);
David S. Miller0785a8e2011-08-06 05:26:35 -07002123 }
David S. Miller9076d0e2011-08-05 00:53:57 -07002124#endif
2125}
2126
David S. Miller490384e2006-02-11 14:41:18 -08002127static void __init sun4v_ktsb_init(void)
2128{
2129 unsigned long ktsb_pa;
2130
David S. Millerd7744a02006-02-21 22:31:11 -08002131 /* First KTSB for PAGE_SIZE mappings. */
David S. Miller490384e2006-02-11 14:41:18 -08002132 ktsb_pa = kern_base + ((unsigned long)&swapper_tsb[0] - KERNBASE);
2133
2134 switch (PAGE_SIZE) {
2135 case 8 * 1024:
2136 default:
2137 ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_8K;
2138 ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_8K;
2139 break;
2140
2141 case 64 * 1024:
2142 ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_64K;
2143 ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_64K;
2144 break;
2145
2146 case 512 * 1024:
2147 ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_512K;
2148 ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_512K;
2149 break;
2150
2151 case 4 * 1024 * 1024:
2152 ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_4MB;
2153 ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_4MB;
2154 break;
Joe Perches6cb79b32011-06-03 14:45:23 +00002155 }
David S. Miller490384e2006-02-11 14:41:18 -08002156
David S. Miller3f19a842006-02-17 12:03:20 -08002157 ktsb_descr[0].assoc = 1;
David S. Miller490384e2006-02-11 14:41:18 -08002158 ktsb_descr[0].num_ttes = KERNEL_TSB_NENTRIES;
2159 ktsb_descr[0].ctx_idx = 0;
2160 ktsb_descr[0].tsb_base = ktsb_pa;
2161 ktsb_descr[0].resv = 0;
2162
David S. Millerd1acb422007-03-16 17:20:28 -07002163#ifndef CONFIG_DEBUG_PAGEALLOC
David S. Miller4f93d212012-09-06 18:13:58 -07002164 /* Second KTSB for 4MB/256MB/2GB/16GB mappings. */
David S. Millerd7744a02006-02-21 22:31:11 -08002165 ktsb_pa = (kern_base +
2166 ((unsigned long)&swapper_4m_tsb[0] - KERNBASE));
2167
2168 ktsb_descr[1].pgsz_idx = HV_PGSZ_IDX_4MB;
David S. Millerc69ad0a2012-09-06 20:35:36 -07002169 ktsb_descr[1].pgsz_mask = ((HV_PGSZ_MASK_4MB |
2170 HV_PGSZ_MASK_256MB |
2171 HV_PGSZ_MASK_2GB |
2172 HV_PGSZ_MASK_16GB) &
2173 cpu_pgsz_mask);
David S. Millerd7744a02006-02-21 22:31:11 -08002174 ktsb_descr[1].assoc = 1;
2175 ktsb_descr[1].num_ttes = KERNEL_TSB4M_NENTRIES;
2176 ktsb_descr[1].ctx_idx = 0;
2177 ktsb_descr[1].tsb_base = ktsb_pa;
2178 ktsb_descr[1].resv = 0;
David S. Millerd1acb422007-03-16 17:20:28 -07002179#endif
David S. Miller490384e2006-02-11 14:41:18 -08002180}
2181
Paul Gortmaker2066aad2013-06-17 15:43:14 -04002182void sun4v_ktsb_register(void)
David S. Miller490384e2006-02-11 14:41:18 -08002183{
David S. Miller7db35f32007-05-29 02:22:14 -07002184 unsigned long pa, ret;
David S. Miller490384e2006-02-11 14:41:18 -08002185
2186 pa = kern_base + ((unsigned long)&ktsb_descr[0] - KERNBASE);
2187
David S. Miller7db35f32007-05-29 02:22:14 -07002188 ret = sun4v_mmu_tsb_ctx0(NUM_KTSB_DESCR, pa);
2189 if (ret != 0) {
2190 prom_printf("hypervisor_mmu_tsb_ctx0[%lx]: "
2191 "errors with %lx\n", pa, ret);
2192 prom_halt();
2193 }
David S. Miller490384e2006-02-11 14:41:18 -08002194}
2195
David S. Millerc69ad0a2012-09-06 20:35:36 -07002196static void __init sun4u_linear_pte_xor_finalize(void)
2197{
2198#ifndef CONFIG_DEBUG_PAGEALLOC
2199 /* This is where we would add Panther support for
2200 * 32MB and 256MB pages.
2201 */
2202#endif
2203}
2204
2205static void __init sun4v_linear_pte_xor_finalize(void)
2206{
Khalid Aziz494e5b62015-05-27 10:00:46 -06002207 unsigned long pagecv_flag;
2208
2209 /* Bit 9 of TTE is no longer CV bit on M7 processor and it instead
2210 * enables MCD error. Do not set bit 9 on M7 processor.
2211 */
2212 switch (sun4v_chip_type) {
2213 case SUN4V_CHIP_SPARC_M7:
Allen Pais7d484ac2017-07-24 11:44:18 +05302214 case SUN4V_CHIP_SPARC_M8:
Khalid Azizc5b8b5b2016-04-19 11:12:54 -06002215 case SUN4V_CHIP_SPARC_SN:
Khalid Aziz494e5b62015-05-27 10:00:46 -06002216 pagecv_flag = 0x00;
2217 break;
2218 default:
2219 pagecv_flag = _PAGE_CV_4V;
2220 break;
2221 }
David S. Millerc69ad0a2012-09-06 20:35:36 -07002222#ifndef CONFIG_DEBUG_PAGEALLOC
2223 if (cpu_pgsz_mask & HV_PGSZ_MASK_256MB) {
2224 kern_linear_pte_xor[1] = (_PAGE_VALID | _PAGE_SZ256MB_4V) ^
David S. Miller922631b2013-09-18 12:00:00 -07002225 PAGE_OFFSET;
Khalid Aziz494e5b62015-05-27 10:00:46 -06002226 kern_linear_pte_xor[1] |= (_PAGE_CP_4V | pagecv_flag |
David S. Millerc69ad0a2012-09-06 20:35:36 -07002227 _PAGE_P_4V | _PAGE_W_4V);
2228 } else {
2229 kern_linear_pte_xor[1] = kern_linear_pte_xor[0];
2230 }
2231
2232 if (cpu_pgsz_mask & HV_PGSZ_MASK_2GB) {
2233 kern_linear_pte_xor[2] = (_PAGE_VALID | _PAGE_SZ2GB_4V) ^
David S. Miller922631b2013-09-18 12:00:00 -07002234 PAGE_OFFSET;
Khalid Aziz494e5b62015-05-27 10:00:46 -06002235 kern_linear_pte_xor[2] |= (_PAGE_CP_4V | pagecv_flag |
David S. Millerc69ad0a2012-09-06 20:35:36 -07002236 _PAGE_P_4V | _PAGE_W_4V);
2237 } else {
2238 kern_linear_pte_xor[2] = kern_linear_pte_xor[1];
2239 }
2240
2241 if (cpu_pgsz_mask & HV_PGSZ_MASK_16GB) {
2242 kern_linear_pte_xor[3] = (_PAGE_VALID | _PAGE_SZ16GB_4V) ^
David S. Miller922631b2013-09-18 12:00:00 -07002243 PAGE_OFFSET;
Khalid Aziz494e5b62015-05-27 10:00:46 -06002244 kern_linear_pte_xor[3] |= (_PAGE_CP_4V | pagecv_flag |
David S. Millerc69ad0a2012-09-06 20:35:36 -07002245 _PAGE_P_4V | _PAGE_W_4V);
2246 } else {
2247 kern_linear_pte_xor[3] = kern_linear_pte_xor[2];
2248 }
2249#endif
2250}
2251
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252/* paging_init() sets up the page tables */
2253
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254static unsigned long last_valid_pfn;
David S. Millerac55c762014-09-26 21:19:46 -07002255
David S. Millerc4bce902006-02-11 21:57:54 -08002256static void sun4u_pgprot_init(void);
2257static void sun4v_pgprot_init(void);
2258
bob picco7c21d532014-09-16 09:29:54 -04002259static phys_addr_t __init available_memory(void)
2260{
2261 phys_addr_t available = 0ULL;
2262 phys_addr_t pa_start, pa_end;
2263 u64 i;
2264
Tony Luckfc6daaf2015-06-24 16:58:09 -07002265 for_each_free_mem_range(i, NUMA_NO_NODE, MEMBLOCK_NONE, &pa_start,
2266 &pa_end, NULL)
bob picco7c21d532014-09-16 09:29:54 -04002267 available = available + (pa_end - pa_start);
2268
2269 return available;
2270}
2271
Khalid Aziz494e5b62015-05-27 10:00:46 -06002272#define _PAGE_CACHE_4U (_PAGE_CP_4U | _PAGE_CV_4U)
2273#define _PAGE_CACHE_4V (_PAGE_CP_4V | _PAGE_CV_4V)
2274#define __DIRTY_BITS_4U (_PAGE_MODIFIED_4U | _PAGE_WRITE_4U | _PAGE_W_4U)
2275#define __DIRTY_BITS_4V (_PAGE_MODIFIED_4V | _PAGE_WRITE_4V | _PAGE_W_4V)
2276#define __ACCESS_BITS_4U (_PAGE_ACCESSED_4U | _PAGE_READ_4U | _PAGE_R)
2277#define __ACCESS_BITS_4V (_PAGE_ACCESSED_4V | _PAGE_READ_4V | _PAGE_R)
2278
bob picco7c21d532014-09-16 09:29:54 -04002279/* We need to exclude reserved regions. This exclusion will include
2280 * vmlinux and initrd. To be more precise the initrd size could be used to
2281 * compute a new lower limit because it is freed later during initialization.
2282 */
2283static void __init reduce_memory(phys_addr_t limit_ram)
2284{
2285 phys_addr_t avail_ram = available_memory();
2286 phys_addr_t pa_start, pa_end;
2287 u64 i;
2288
2289 if (limit_ram >= avail_ram)
2290 return;
2291
Tony Luckfc6daaf2015-06-24 16:58:09 -07002292 for_each_free_mem_range(i, NUMA_NO_NODE, MEMBLOCK_NONE, &pa_start,
2293 &pa_end, NULL) {
bob picco7c21d532014-09-16 09:29:54 -04002294 phys_addr_t region_size = pa_end - pa_start;
2295 phys_addr_t clip_start = pa_start;
2296
2297 avail_ram = avail_ram - region_size;
2298 /* Are we consuming too much? */
2299 if (avail_ram < limit_ram) {
2300 phys_addr_t give_back = limit_ram - avail_ram;
2301
2302 region_size = region_size - give_back;
2303 clip_start = clip_start + give_back;
2304 }
2305
2306 memblock_remove(clip_start, region_size);
2307
2308 if (avail_ram <= limit_ram)
2309 break;
2310 i = 0UL;
2311 }
2312}
2313
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314void __init paging_init(void)
2315{
David S. Miller919ee672008-04-23 05:40:25 -07002316 unsigned long end_pfn, shift, phys_base;
David S. Miller0836a0e2005-09-28 21:38:08 -07002317 unsigned long real_end, i;
2318
David S. Millerb2d43832013-09-20 21:50:41 -07002319 setup_page_offset();
2320
David S. Miller22adb352007-05-26 01:14:43 -07002321 /* These build time checkes make sure that the dcache_dirty_cpu()
2322 * page->flags usage will work.
2323 *
2324 * When a page gets marked as dcache-dirty, we store the
2325 * cpu number starting at bit 32 in the page->flags. Also,
2326 * functions like clear_dcache_dirty_cpu use the cpu mask
2327 * in 13-bit signed-immediate instruction fields.
2328 */
Christoph Lameter9223b4192008-04-28 02:12:48 -07002329
2330 /*
2331 * Page flags must not reach into upper 32 bits that are used
2332 * for the cpu number
2333 */
2334 BUILD_BUG_ON(NR_PAGEFLAGS > 32);
2335
2336 /*
2337 * The bit fields placed in the high range must not reach below
2338 * the 32 bit boundary. Otherwise we cannot place the cpu field
2339 * at the 32 bit boundary.
2340 */
David S. Miller22adb352007-05-26 01:14:43 -07002341 BUILD_BUG_ON(SECTIONS_WIDTH + NODES_WIDTH + ZONES_WIDTH +
Christoph Lameter9223b4192008-04-28 02:12:48 -07002342 ilog2(roundup_pow_of_two(NR_CPUS)) > 32);
2343
David S. Miller22adb352007-05-26 01:14:43 -07002344 BUILD_BUG_ON(NR_CPUS > 4096);
2345
David S. Miller0eef3312014-05-03 22:52:50 -07002346 kern_base = (prom_boot_mapping_phys_low >> ILOG2_4MB) << ILOG2_4MB;
David S. Miller481295f2006-02-07 21:51:08 -08002347 kern_size = (unsigned long)&_end - (unsigned long)KERNBASE;
2348
David S. Millerd7744a02006-02-21 22:31:11 -08002349 /* Invalidate both kernel TSBs. */
David S. Miller8b234272006-02-17 18:01:02 -08002350 memset(swapper_tsb, 0x40, sizeof(swapper_tsb));
David S. Millerd1acb422007-03-16 17:20:28 -07002351#ifndef CONFIG_DEBUG_PAGEALLOC
David S. Millerd7744a02006-02-21 22:31:11 -08002352 memset(swapper_4m_tsb, 0x40, sizeof(swapper_4m_tsb));
David S. Millerd1acb422007-03-16 17:20:28 -07002353#endif
David S. Miller8b234272006-02-17 18:01:02 -08002354
Khalid Aziz494e5b62015-05-27 10:00:46 -06002355 /* TTE.cv bit on sparc v9 occupies the same position as TTE.mcde
2356 * bit on M7 processor. This is a conflicting usage of the same
2357 * bit. Enabling TTE.cv on M7 would turn on Memory Corruption
2358 * Detection error on all pages and this will lead to problems
2359 * later. Kernel does not run with MCD enabled and hence rest
2360 * of the required steps to fully configure memory corruption
2361 * detection are not taken. We need to ensure TTE.mcde is not
2362 * set on M7 processor. Compute the value of cacheability
2363 * flag for use later taking this into consideration.
2364 */
2365 switch (sun4v_chip_type) {
2366 case SUN4V_CHIP_SPARC_M7:
Allen Pais7d484ac2017-07-24 11:44:18 +05302367 case SUN4V_CHIP_SPARC_M8:
Khalid Azizc5b8b5b2016-04-19 11:12:54 -06002368 case SUN4V_CHIP_SPARC_SN:
Khalid Aziz494e5b62015-05-27 10:00:46 -06002369 page_cache4v_flag = _PAGE_CP_4V;
2370 break;
2371 default:
2372 page_cache4v_flag = _PAGE_CACHE_4V;
2373 break;
2374 }
2375
David S. Millerc4bce902006-02-11 21:57:54 -08002376 if (tlb_type == hypervisor)
2377 sun4v_pgprot_init();
2378 else
2379 sun4u_pgprot_init();
2380
David S. Millerd257d5d2006-02-06 23:44:37 -08002381 if (tlb_type == cheetah_plus ||
David S. Miller9076d0e2011-08-05 00:53:57 -07002382 tlb_type == hypervisor) {
David S. Miller517af332006-02-01 15:55:21 -08002383 tsb_phys_patch();
David S. Miller9076d0e2011-08-05 00:53:57 -07002384 ktsb_phys_patch();
2385 }
David S. Miller517af332006-02-01 15:55:21 -08002386
David S. Millerc69ad0a2012-09-06 20:35:36 -07002387 if (tlb_type == hypervisor)
David S. Millerd257d5d2006-02-06 23:44:37 -08002388 sun4v_patch_tlb_handlers();
2389
David S. Millera94a1722008-05-11 21:04:48 -07002390 /* Find available physical memory...
2391 *
2392 * Read it twice in order to work around a bug in openfirmware.
2393 * The call to grab this table itself can cause openfirmware to
2394 * allocate memory, which in turn can take away some space from
2395 * the list of available memory. Reading it twice makes sure
2396 * we really do get the final value.
2397 */
2398 read_obp_translations();
2399 read_obp_memory("reg", &pall[0], &pall_ents);
2400 read_obp_memory("available", &pavail[0], &pavail_ents);
David S. Miller13edad72005-09-29 17:58:26 -07002401 read_obp_memory("available", &pavail[0], &pavail_ents);
David S. Miller0836a0e2005-09-28 21:38:08 -07002402
2403 phys_base = 0xffffffffffffffffUL;
David S. Miller3b2a7e22008-02-13 18:13:20 -08002404 for (i = 0; i < pavail_ents; i++) {
David S. Miller13edad72005-09-29 17:58:26 -07002405 phys_base = min(phys_base, pavail[i].phys_addr);
Yinghai Lu95f72d12010-07-12 14:36:09 +10002406 memblock_add(pavail[i].phys_addr, pavail[i].reg_size);
David S. Miller3b2a7e22008-02-13 18:13:20 -08002407 }
2408
Yinghai Lu95f72d12010-07-12 14:36:09 +10002409 memblock_reserve(kern_base, kern_size);
David S. Miller0836a0e2005-09-28 21:38:08 -07002410
David S. Miller4e82c9a2008-02-13 18:00:03 -08002411 find_ramdisk(phys_base);
2412
bob picco7c21d532014-09-16 09:29:54 -04002413 if (cmdline_memory_size)
2414 reduce_memory(cmdline_memory_size);
David S. Miller25b0c652008-02-13 18:20:14 -08002415
Tejun Heo1aadc052011-12-08 10:22:08 -08002416 memblock_allow_resize();
Yinghai Lu95f72d12010-07-12 14:36:09 +10002417 memblock_dump_all();
David S. Miller3b2a7e22008-02-13 18:13:20 -08002418
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 set_bit(0, mmu_context_bmap);
2420
David S. Miller2bdb3cb2005-09-22 01:08:57 -07002421 shift = kern_base + PAGE_OFFSET - ((unsigned long)KERNBASE);
2422
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 real_end = (unsigned long)_end;
David S. Miller0eef3312014-05-03 22:52:50 -07002424 num_kernel_image_mappings = DIV_ROUND_UP(real_end - KERNBASE, 1 << ILOG2_4MB);
David S. Miller64658742008-03-21 17:01:38 -07002425 printk("Kernel: Using %d locked TLB entries for main kernel image.\n",
2426 num_kernel_image_mappings);
David S. Miller2bdb3cb2005-09-22 01:08:57 -07002427
2428 /* Set kernel pgd to upper alias so physical page computations
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 * work.
2430 */
2431 init_mm.pgd += ((shift) / (sizeof(pgd_t)));
2432
David S. Millerd195b712014-09-27 21:30:57 -07002433 memset(swapper_pg_dir, 0, sizeof(swapper_pg_dir));
David S. Miller0dd5b7b2014-09-24 20:56:11 -07002434
David S. Millerc9c10832005-10-12 12:22:46 -07002435 inherit_prom_mappings();
David S. Miller5085b4a2005-09-22 00:45:41 -07002436
David S. Millera8b900d2006-01-31 18:33:37 -08002437 /* Ok, we can use our TLB miss and window trap handlers safely. */
2438 setup_tba();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439
David S. Millerc9c10832005-10-12 12:22:46 -07002440 __flush_tlb_all();
David S. Miller9ad98c52005-10-05 15:12:00 -07002441
David S. Millerad072002008-02-13 19:21:51 -08002442 prom_build_devicetree();
David S. Millerb696fdc2009-05-26 22:37:25 -07002443 of_populate_present_mask();
David S. Millerb99c6eb2009-06-18 01:44:19 -07002444#ifndef CONFIG_SMP
2445 of_fill_in_cpu_data();
2446#endif
David S. Millerad072002008-02-13 19:21:51 -08002447
David S. Miller890db402009-04-01 03:13:15 -07002448 if (tlb_type == hypervisor) {
David S. Miller4a283332008-02-13 19:22:23 -08002449 sun4v_mdesc_init();
Stephen Rothwell6ac5c612009-06-15 03:06:18 -07002450 mdesc_populate_present_mask(cpu_all_mask);
David S. Millerb99c6eb2009-06-18 01:44:19 -07002451#ifndef CONFIG_SMP
2452 mdesc_fill_in_cpu_data(cpu_all_mask);
2453#endif
David S. Millerce33fdc2012-09-06 19:01:25 -07002454 mdesc_get_page_sizes(cpu_all_mask, &cpu_pgsz_mask);
David S. Millerc69ad0a2012-09-06 20:35:36 -07002455
2456 sun4v_linear_pte_xor_finalize();
2457
2458 sun4v_ktsb_init();
2459 sun4v_ktsb_register();
David S. Millerce33fdc2012-09-06 19:01:25 -07002460 } else {
2461 unsigned long impl, ver;
2462
2463 cpu_pgsz_mask = (HV_PGSZ_MASK_8K | HV_PGSZ_MASK_64K |
2464 HV_PGSZ_MASK_512K | HV_PGSZ_MASK_4MB);
2465
2466 __asm__ __volatile__("rdpr %%ver, %0" : "=r" (ver));
2467 impl = ((ver >> 32) & 0xffff);
2468 if (impl == PANTHER_IMPL)
2469 cpu_pgsz_mask |= (HV_PGSZ_MASK_32MB |
2470 HV_PGSZ_MASK_256MB);
David S. Millerc69ad0a2012-09-06 20:35:36 -07002471
2472 sun4u_linear_pte_xor_finalize();
David S. Miller890db402009-04-01 03:13:15 -07002473 }
David S. Miller4a283332008-02-13 19:22:23 -08002474
David S. Millerc69ad0a2012-09-06 20:35:36 -07002475 /* Flush the TLBs and the 4M TSB so that the updated linear
2476 * pte XOR settings are realized for all mappings.
2477 */
2478 __flush_tlb_all();
2479#ifndef CONFIG_DEBUG_PAGEALLOC
2480 memset(swapper_4m_tsb, 0x40, sizeof(swapper_4m_tsb));
2481#endif
2482 __flush_tlb_all();
2483
David S. Miller2bdb3cb2005-09-22 01:08:57 -07002484 /* Setup bootmem... */
David S. Miller919ee672008-04-23 05:40:25 -07002485 last_valid_pfn = end_pfn = bootmem_init(phys_base);
David S. Millerd1112012006-03-08 02:16:07 -08002486
David S. Miller56425302005-09-25 16:46:57 -07002487 kernel_physical_mapping_init();
David S. Miller56425302005-09-25 16:46:57 -07002488
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 {
David S. Miller919ee672008-04-23 05:40:25 -07002490 unsigned long max_zone_pfns[MAX_NR_ZONES];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491
David S. Miller919ee672008-04-23 05:40:25 -07002492 memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493
David S. Miller919ee672008-04-23 05:40:25 -07002494 max_zone_pfns[ZONE_NORMAL] = end_pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495
David S. Miller919ee672008-04-23 05:40:25 -07002496 free_area_init_nodes(max_zone_pfns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 }
2498
David S. Miller3c62a2d2008-02-17 23:22:50 -08002499 printk("Booting Linux...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500}
2501
Greg Kroah-Hartman7c9503b2012-12-21 14:03:26 -08002502int page_in_phys_avail(unsigned long paddr)
David S. Miller919ee672008-04-23 05:40:25 -07002503{
2504 int i;
2505
2506 paddr &= PAGE_MASK;
2507
2508 for (i = 0; i < pavail_ents; i++) {
2509 unsigned long start, end;
2510
2511 start = pavail[i].phys_addr;
2512 end = start + pavail[i].reg_size;
2513
2514 if (paddr >= start && paddr < end)
2515 return 1;
2516 }
2517 if (paddr >= kern_base && paddr < (kern_base + kern_size))
2518 return 1;
2519#ifdef CONFIG_BLK_DEV_INITRD
2520 if (paddr >= __pa(initrd_start) &&
2521 paddr < __pa(PAGE_ALIGN(initrd_end)))
2522 return 1;
2523#endif
2524
2525 return 0;
2526}
2527
Yinghai Lu961f8fa2012-11-16 19:39:21 -08002528static void __init register_page_bootmem_info(void)
2529{
2530#ifdef CONFIG_NEED_MULTIPLE_NODES
2531 int i;
2532
2533 for_each_online_node(i)
2534 if (NODE_DATA(i)->node_spanned_pages)
2535 register_page_bootmem_info_node(NODE_DATA(i));
2536#endif
2537}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538void __init mem_init(void)
2539{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 high_memory = __va(last_valid_pfn << PAGE_SHIFT);
2541
Yinghai Lu961f8fa2012-11-16 19:39:21 -08002542 register_page_bootmem_info();
Jiang Liu0c988532013-07-03 15:03:24 -07002543 free_all_bootmem();
David S. Miller919ee672008-04-23 05:40:25 -07002544
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 /*
2546 * Set up the zero page, mark it reserved, so that page count
2547 * is not manipulated when freeing the page from user ptes.
2548 */
2549 mem_map_zero = alloc_pages(GFP_KERNEL|__GFP_ZERO, 0);
2550 if (mem_map_zero == NULL) {
2551 prom_printf("paging_init: Cannot alloc zero page.\n");
2552 prom_halt();
2553 }
Jiang Liu70affe42013-05-07 16:18:08 -07002554 mark_page_reserved(mem_map_zero);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555
Jiang Liudceccbe2013-07-03 15:04:14 -07002556 mem_init_print_info(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557
2558 if (tlb_type == cheetah || tlb_type == cheetah_plus)
2559 cheetah_ecache_flush_init();
2560}
2561
David S. Miller898cf0e2005-09-23 11:59:44 -07002562void free_initmem(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563{
2564 unsigned long addr, initend;
David S. Millerf2b60792008-08-14 01:45:41 -07002565 int do_free = 1;
2566
2567 /* If the physical memory maps were trimmed by kernel command
2568 * line options, don't even try freeing this initmem stuff up.
2569 * The kernel image could have been in the trimmed out region
2570 * and if so the freeing below will free invalid page structs.
2571 */
2572 if (cmdline_memory_size)
2573 do_free = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574
2575 /*
2576 * The init section is aligned to 8k in vmlinux.lds. Page align for >8k pagesizes.
2577 */
2578 addr = PAGE_ALIGN((unsigned long)(__init_begin));
2579 initend = (unsigned long)(__init_end) & PAGE_MASK;
2580 for (; addr < initend; addr += PAGE_SIZE) {
2581 unsigned long page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582
2583 page = (addr +
2584 ((unsigned long) __va(kern_base)) -
2585 ((unsigned long) KERNBASE));
Randy Dunlapc9cf5522006-06-27 02:53:52 -07002586 memset((void *)addr, POISON_FREE_INITMEM, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587
Jiang Liu70affe42013-05-07 16:18:08 -07002588 if (do_free)
2589 free_reserved_page(virt_to_page(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 }
2591}
2592
2593#ifdef CONFIG_BLK_DEV_INITRD
2594void free_initrd_mem(unsigned long start, unsigned long end)
2595{
Jiang Liudceccbe2013-07-03 15:04:14 -07002596 free_reserved_area((void *)start, (void *)end, POISON_FREE_INITMEM,
2597 "initrd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598}
2599#endif
David S. Millerc4bce902006-02-11 21:57:54 -08002600
David S. Millerc4bce902006-02-11 21:57:54 -08002601pgprot_t PAGE_KERNEL __read_mostly;
2602EXPORT_SYMBOL(PAGE_KERNEL);
2603
2604pgprot_t PAGE_KERNEL_LOCKED __read_mostly;
2605pgprot_t PAGE_COPY __read_mostly;
David S. Miller0f159522006-02-18 12:43:16 -08002606
2607pgprot_t PAGE_SHARED __read_mostly;
2608EXPORT_SYMBOL(PAGE_SHARED);
2609
David S. Millerc4bce902006-02-11 21:57:54 -08002610unsigned long pg_iobits __read_mostly;
2611
2612unsigned long _PAGE_IE __read_mostly;
David S. Miller987c74f2006-06-25 01:34:43 -07002613EXPORT_SYMBOL(_PAGE_IE);
David S. Millerb2bef442006-02-23 01:55:55 -08002614
David S. Millerc4bce902006-02-11 21:57:54 -08002615unsigned long _PAGE_E __read_mostly;
David S. Millerb2bef442006-02-23 01:55:55 -08002616EXPORT_SYMBOL(_PAGE_E);
2617
David S. Millerc4bce902006-02-11 21:57:54 -08002618unsigned long _PAGE_CACHE __read_mostly;
David S. Millerb2bef442006-02-23 01:55:55 -08002619EXPORT_SYMBOL(_PAGE_CACHE);
David S. Millerc4bce902006-02-11 21:57:54 -08002620
David Miller46644c22007-10-16 01:24:16 -07002621#ifdef CONFIG_SPARSEMEM_VMEMMAP
Johannes Weiner0aad8182013-04-29 15:07:50 -07002622int __meminit vmemmap_populate(unsigned long vstart, unsigned long vend,
2623 int node)
David Miller46644c22007-10-16 01:24:16 -07002624{
David Miller46644c22007-10-16 01:24:16 -07002625 unsigned long pte_base;
2626
2627 pte_base = (_PAGE_VALID | _PAGE_SZ4MB_4U |
2628 _PAGE_CP_4U | _PAGE_CV_4U |
2629 _PAGE_P_4U | _PAGE_W_4U);
2630 if (tlb_type == hypervisor)
2631 pte_base = (_PAGE_VALID | _PAGE_SZ4MB_4V |
Khalid Aziz494e5b62015-05-27 10:00:46 -06002632 page_cache4v_flag | _PAGE_P_4V | _PAGE_W_4V);
David Miller46644c22007-10-16 01:24:16 -07002633
David S. Millerc06240c2014-09-24 21:20:14 -07002634 pte_base |= _PAGE_PMD_HUGE;
David Miller46644c22007-10-16 01:24:16 -07002635
David S. Millerc06240c2014-09-24 21:20:14 -07002636 vstart = vstart & PMD_MASK;
2637 vend = ALIGN(vend, PMD_SIZE);
2638 for (; vstart < vend; vstart += PMD_SIZE) {
2639 pgd_t *pgd = pgd_offset_k(vstart);
2640 unsigned long pte;
2641 pud_t *pud;
2642 pmd_t *pmd;
2643
2644 if (pgd_none(*pgd)) {
2645 pud_t *new = vmemmap_alloc_block(PAGE_SIZE, node);
2646
2647 if (!new)
2648 return -ENOMEM;
2649 pgd_populate(&init_mm, pgd, new);
2650 }
2651
2652 pud = pud_offset(pgd, vstart);
2653 if (pud_none(*pud)) {
2654 pmd_t *new = vmemmap_alloc_block(PAGE_SIZE, node);
2655
2656 if (!new)
2657 return -ENOMEM;
2658 pud_populate(&init_mm, pud, new);
2659 }
2660
2661 pmd = pmd_offset(pud, vstart);
2662
2663 pte = pmd_val(*pmd);
2664 if (!(pte & _PAGE_VALID)) {
2665 void *block = vmemmap_alloc_block(PMD_SIZE, node);
2666
David Miller46644c22007-10-16 01:24:16 -07002667 if (!block)
2668 return -ENOMEM;
2669
David S. Millerc06240c2014-09-24 21:20:14 -07002670 pmd_val(*pmd) = pte_base | __pa(block);
David Miller46644c22007-10-16 01:24:16 -07002671 }
2672 }
David S. Miller2856cc22012-08-15 00:37:29 -07002673
David S. Millerc06240c2014-09-24 21:20:14 -07002674 return 0;
David S. Miller2856cc22012-08-15 00:37:29 -07002675}
Yasuaki Ishimatsu46723bf2013-02-22 16:33:00 -08002676
Johannes Weiner0aad8182013-04-29 15:07:50 -07002677void vmemmap_free(unsigned long start, unsigned long end)
Tang Chen01975182013-02-22 16:33:08 -08002678{
2679}
David Miller46644c22007-10-16 01:24:16 -07002680#endif /* CONFIG_SPARSEMEM_VMEMMAP */
2681
David S. Millerc4bce902006-02-11 21:57:54 -08002682static void prot_init_common(unsigned long page_none,
2683 unsigned long page_shared,
2684 unsigned long page_copy,
2685 unsigned long page_readonly,
2686 unsigned long page_exec_bit)
2687{
2688 PAGE_COPY = __pgprot(page_copy);
David S. Miller0f159522006-02-18 12:43:16 -08002689 PAGE_SHARED = __pgprot(page_shared);
David S. Millerc4bce902006-02-11 21:57:54 -08002690
2691 protection_map[0x0] = __pgprot(page_none);
2692 protection_map[0x1] = __pgprot(page_readonly & ~page_exec_bit);
2693 protection_map[0x2] = __pgprot(page_copy & ~page_exec_bit);
2694 protection_map[0x3] = __pgprot(page_copy & ~page_exec_bit);
2695 protection_map[0x4] = __pgprot(page_readonly);
2696 protection_map[0x5] = __pgprot(page_readonly);
2697 protection_map[0x6] = __pgprot(page_copy);
2698 protection_map[0x7] = __pgprot(page_copy);
2699 protection_map[0x8] = __pgprot(page_none);
2700 protection_map[0x9] = __pgprot(page_readonly & ~page_exec_bit);
2701 protection_map[0xa] = __pgprot(page_shared & ~page_exec_bit);
2702 protection_map[0xb] = __pgprot(page_shared & ~page_exec_bit);
2703 protection_map[0xc] = __pgprot(page_readonly);
2704 protection_map[0xd] = __pgprot(page_readonly);
2705 protection_map[0xe] = __pgprot(page_shared);
2706 protection_map[0xf] = __pgprot(page_shared);
2707}
2708
2709static void __init sun4u_pgprot_init(void)
2710{
2711 unsigned long page_none, page_shared, page_copy, page_readonly;
2712 unsigned long page_exec_bit;
David S. Miller4f93d212012-09-06 18:13:58 -07002713 int i;
David S. Millerc4bce902006-02-11 21:57:54 -08002714
2715 PAGE_KERNEL = __pgprot (_PAGE_PRESENT_4U | _PAGE_VALID |
2716 _PAGE_CACHE_4U | _PAGE_P_4U |
2717 __ACCESS_BITS_4U | __DIRTY_BITS_4U |
2718 _PAGE_EXEC_4U);
2719 PAGE_KERNEL_LOCKED = __pgprot (_PAGE_PRESENT_4U | _PAGE_VALID |
2720 _PAGE_CACHE_4U | _PAGE_P_4U |
2721 __ACCESS_BITS_4U | __DIRTY_BITS_4U |
2722 _PAGE_EXEC_4U | _PAGE_L_4U);
David S. Millerc4bce902006-02-11 21:57:54 -08002723
2724 _PAGE_IE = _PAGE_IE_4U;
2725 _PAGE_E = _PAGE_E_4U;
2726 _PAGE_CACHE = _PAGE_CACHE_4U;
2727
2728 pg_iobits = (_PAGE_VALID | _PAGE_PRESENT_4U | __DIRTY_BITS_4U |
2729 __ACCESS_BITS_4U | _PAGE_E_4U);
2730
David S. Millerd1acb422007-03-16 17:20:28 -07002731#ifdef CONFIG_DEBUG_PAGEALLOC
David S. Miller922631b2013-09-18 12:00:00 -07002732 kern_linear_pte_xor[0] = _PAGE_VALID ^ PAGE_OFFSET;
David S. Millerd1acb422007-03-16 17:20:28 -07002733#else
David S. Miller9cc3a1a2006-02-21 20:51:13 -08002734 kern_linear_pte_xor[0] = (_PAGE_VALID | _PAGE_SZ4MB_4U) ^
David S. Miller922631b2013-09-18 12:00:00 -07002735 PAGE_OFFSET;
David S. Millerd1acb422007-03-16 17:20:28 -07002736#endif
David S. Miller9cc3a1a2006-02-21 20:51:13 -08002737 kern_linear_pte_xor[0] |= (_PAGE_CP_4U | _PAGE_CV_4U |
2738 _PAGE_P_4U | _PAGE_W_4U);
2739
David S. Miller4f93d212012-09-06 18:13:58 -07002740 for (i = 1; i < 4; i++)
2741 kern_linear_pte_xor[i] = kern_linear_pte_xor[0];
David S. Millerc4bce902006-02-11 21:57:54 -08002742
David S. Millerc4bce902006-02-11 21:57:54 -08002743 _PAGE_ALL_SZ_BITS = (_PAGE_SZ4MB_4U | _PAGE_SZ512K_4U |
2744 _PAGE_SZ64K_4U | _PAGE_SZ8K_4U |
2745 _PAGE_SZ32MB_4U | _PAGE_SZ256MB_4U);
2746
2747
2748 page_none = _PAGE_PRESENT_4U | _PAGE_ACCESSED_4U | _PAGE_CACHE_4U;
2749 page_shared = (_PAGE_VALID | _PAGE_PRESENT_4U | _PAGE_CACHE_4U |
2750 __ACCESS_BITS_4U | _PAGE_WRITE_4U | _PAGE_EXEC_4U);
2751 page_copy = (_PAGE_VALID | _PAGE_PRESENT_4U | _PAGE_CACHE_4U |
2752 __ACCESS_BITS_4U | _PAGE_EXEC_4U);
2753 page_readonly = (_PAGE_VALID | _PAGE_PRESENT_4U | _PAGE_CACHE_4U |
2754 __ACCESS_BITS_4U | _PAGE_EXEC_4U);
2755
2756 page_exec_bit = _PAGE_EXEC_4U;
2757
2758 prot_init_common(page_none, page_shared, page_copy, page_readonly,
2759 page_exec_bit);
2760}
2761
2762static void __init sun4v_pgprot_init(void)
2763{
2764 unsigned long page_none, page_shared, page_copy, page_readonly;
2765 unsigned long page_exec_bit;
David S. Miller4f93d212012-09-06 18:13:58 -07002766 int i;
David S. Millerc4bce902006-02-11 21:57:54 -08002767
2768 PAGE_KERNEL = __pgprot (_PAGE_PRESENT_4V | _PAGE_VALID |
Khalid Aziz494e5b62015-05-27 10:00:46 -06002769 page_cache4v_flag | _PAGE_P_4V |
David S. Millerc4bce902006-02-11 21:57:54 -08002770 __ACCESS_BITS_4V | __DIRTY_BITS_4V |
2771 _PAGE_EXEC_4V);
2772 PAGE_KERNEL_LOCKED = PAGE_KERNEL;
David S. Millerc4bce902006-02-11 21:57:54 -08002773
2774 _PAGE_IE = _PAGE_IE_4V;
2775 _PAGE_E = _PAGE_E_4V;
Khalid Aziz494e5b62015-05-27 10:00:46 -06002776 _PAGE_CACHE = page_cache4v_flag;
David S. Millerc4bce902006-02-11 21:57:54 -08002777
David S. Millerd1acb422007-03-16 17:20:28 -07002778#ifdef CONFIG_DEBUG_PAGEALLOC
David S. Miller922631b2013-09-18 12:00:00 -07002779 kern_linear_pte_xor[0] = _PAGE_VALID ^ PAGE_OFFSET;
David S. Millerd1acb422007-03-16 17:20:28 -07002780#else
David S. Miller9cc3a1a2006-02-21 20:51:13 -08002781 kern_linear_pte_xor[0] = (_PAGE_VALID | _PAGE_SZ4MB_4V) ^
David S. Miller922631b2013-09-18 12:00:00 -07002782 PAGE_OFFSET;
David S. Millerd1acb422007-03-16 17:20:28 -07002783#endif
Khalid Aziz494e5b62015-05-27 10:00:46 -06002784 kern_linear_pte_xor[0] |= (page_cache4v_flag | _PAGE_P_4V |
2785 _PAGE_W_4V);
David S. Miller9cc3a1a2006-02-21 20:51:13 -08002786
David S. Millerc69ad0a2012-09-06 20:35:36 -07002787 for (i = 1; i < 4; i++)
2788 kern_linear_pte_xor[i] = kern_linear_pte_xor[0];
David S. Miller4f93d212012-09-06 18:13:58 -07002789
David S. Millerc4bce902006-02-11 21:57:54 -08002790 pg_iobits = (_PAGE_VALID | _PAGE_PRESENT_4V | __DIRTY_BITS_4V |
2791 __ACCESS_BITS_4V | _PAGE_E_4V);
2792
David S. Millerc4bce902006-02-11 21:57:54 -08002793 _PAGE_ALL_SZ_BITS = (_PAGE_SZ16GB_4V | _PAGE_SZ2GB_4V |
2794 _PAGE_SZ256MB_4V | _PAGE_SZ32MB_4V |
2795 _PAGE_SZ4MB_4V | _PAGE_SZ512K_4V |
2796 _PAGE_SZ64K_4V | _PAGE_SZ8K_4V);
2797
Khalid Aziz494e5b62015-05-27 10:00:46 -06002798 page_none = _PAGE_PRESENT_4V | _PAGE_ACCESSED_4V | page_cache4v_flag;
2799 page_shared = (_PAGE_VALID | _PAGE_PRESENT_4V | page_cache4v_flag |
David S. Millerc4bce902006-02-11 21:57:54 -08002800 __ACCESS_BITS_4V | _PAGE_WRITE_4V | _PAGE_EXEC_4V);
Khalid Aziz494e5b62015-05-27 10:00:46 -06002801 page_copy = (_PAGE_VALID | _PAGE_PRESENT_4V | page_cache4v_flag |
David S. Millerc4bce902006-02-11 21:57:54 -08002802 __ACCESS_BITS_4V | _PAGE_EXEC_4V);
Khalid Aziz494e5b62015-05-27 10:00:46 -06002803 page_readonly = (_PAGE_VALID | _PAGE_PRESENT_4V | page_cache4v_flag |
David S. Millerc4bce902006-02-11 21:57:54 -08002804 __ACCESS_BITS_4V | _PAGE_EXEC_4V);
2805
2806 page_exec_bit = _PAGE_EXEC_4V;
2807
2808 prot_init_common(page_none, page_shared, page_copy, page_readonly,
2809 page_exec_bit);
2810}
2811
2812unsigned long pte_sz_bits(unsigned long sz)
2813{
2814 if (tlb_type == hypervisor) {
2815 switch (sz) {
2816 case 8 * 1024:
2817 default:
2818 return _PAGE_SZ8K_4V;
2819 case 64 * 1024:
2820 return _PAGE_SZ64K_4V;
2821 case 512 * 1024:
2822 return _PAGE_SZ512K_4V;
2823 case 4 * 1024 * 1024:
2824 return _PAGE_SZ4MB_4V;
Joe Perches6cb79b32011-06-03 14:45:23 +00002825 }
David S. Millerc4bce902006-02-11 21:57:54 -08002826 } else {
2827 switch (sz) {
2828 case 8 * 1024:
2829 default:
2830 return _PAGE_SZ8K_4U;
2831 case 64 * 1024:
2832 return _PAGE_SZ64K_4U;
2833 case 512 * 1024:
2834 return _PAGE_SZ512K_4U;
2835 case 4 * 1024 * 1024:
2836 return _PAGE_SZ4MB_4U;
Joe Perches6cb79b32011-06-03 14:45:23 +00002837 }
David S. Millerc4bce902006-02-11 21:57:54 -08002838 }
2839}
2840
2841pte_t mk_pte_io(unsigned long page, pgprot_t prot, int space, unsigned long page_size)
2842{
2843 pte_t pte;
David S. Millercf627152006-02-12 21:10:07 -08002844
2845 pte_val(pte) = page | pgprot_val(pgprot_noncached(prot));
David S. Millerc4bce902006-02-11 21:57:54 -08002846 pte_val(pte) |= (((unsigned long)space) << 32);
2847 pte_val(pte) |= pte_sz_bits(page_size);
David S. Millercf627152006-02-12 21:10:07 -08002848
David S. Millerc4bce902006-02-11 21:57:54 -08002849 return pte;
2850}
2851
David S. Millerc4bce902006-02-11 21:57:54 -08002852static unsigned long kern_large_tte(unsigned long paddr)
2853{
2854 unsigned long val;
2855
2856 val = (_PAGE_VALID | _PAGE_SZ4MB_4U |
2857 _PAGE_CP_4U | _PAGE_CV_4U | _PAGE_P_4U |
2858 _PAGE_EXEC_4U | _PAGE_L_4U | _PAGE_W_4U);
2859 if (tlb_type == hypervisor)
2860 val = (_PAGE_VALID | _PAGE_SZ4MB_4V |
Khalid Aziz494e5b62015-05-27 10:00:46 -06002861 page_cache4v_flag | _PAGE_P_4V |
David S. Millerc4bce902006-02-11 21:57:54 -08002862 _PAGE_EXEC_4V | _PAGE_W_4V);
2863
2864 return val | paddr;
2865}
2866
David S. Millerc4bce902006-02-11 21:57:54 -08002867/* If not locked, zap it. */
2868void __flush_tlb_all(void)
2869{
2870 unsigned long pstate;
2871 int i;
2872
2873 __asm__ __volatile__("flushw\n\t"
2874 "rdpr %%pstate, %0\n\t"
2875 "wrpr %0, %1, %%pstate"
2876 : "=r" (pstate)
2877 : "i" (PSTATE_IE));
David S. Miller8f3614532007-12-13 06:13:38 -08002878 if (tlb_type == hypervisor) {
2879 sun4v_mmu_demap_all();
2880 } else if (tlb_type == spitfire) {
David S. Millerc4bce902006-02-11 21:57:54 -08002881 for (i = 0; i < 64; i++) {
2882 /* Spitfire Errata #32 workaround */
2883 /* NOTE: Always runs on spitfire, so no
2884 * cheetah+ page size encodings.
2885 */
2886 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
2887 "flush %%g6"
2888 : /* No outputs */
2889 : "r" (0),
2890 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
2891
2892 if (!(spitfire_get_dtlb_data(i) & _PAGE_L_4U)) {
2893 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
2894 "membar #Sync"
2895 : /* no outputs */
2896 : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
2897 spitfire_put_dtlb_data(i, 0x0UL);
2898 }
2899
2900 /* Spitfire Errata #32 workaround */
2901 /* NOTE: Always runs on spitfire, so no
2902 * cheetah+ page size encodings.
2903 */
2904 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
2905 "flush %%g6"
2906 : /* No outputs */
2907 : "r" (0),
2908 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
2909
2910 if (!(spitfire_get_itlb_data(i) & _PAGE_L_4U)) {
2911 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
2912 "membar #Sync"
2913 : /* no outputs */
2914 : "r" (TLB_TAG_ACCESS), "i" (ASI_IMMU));
2915 spitfire_put_itlb_data(i, 0x0UL);
2916 }
2917 }
2918 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
2919 cheetah_flush_dtlb_all();
2920 cheetah_flush_itlb_all();
2921 }
2922 __asm__ __volatile__("wrpr %0, 0, %%pstate"
2923 : : "r" (pstate));
2924}
David Millerc460bec2012-10-08 16:34:22 -07002925
David Millerc460bec2012-10-08 16:34:22 -07002926pte_t *pte_alloc_one_kernel(struct mm_struct *mm,
2927 unsigned long address)
2928{
Michal Hocko32d6bd92016-06-24 14:48:47 -07002929 struct page *page = alloc_page(GFP_KERNEL | __GFP_NOTRACK | __GFP_ZERO);
David S. Miller37b3a8f2013-09-25 13:48:49 -07002930 pte_t *pte = NULL;
David Millerc460bec2012-10-08 16:34:22 -07002931
David Millerc460bec2012-10-08 16:34:22 -07002932 if (page)
2933 pte = (pte_t *) page_address(page);
2934
2935 return pte;
2936}
2937
2938pgtable_t pte_alloc_one(struct mm_struct *mm,
2939 unsigned long address)
2940{
Michal Hocko32d6bd92016-06-24 14:48:47 -07002941 struct page *page = alloc_page(GFP_KERNEL | __GFP_NOTRACK | __GFP_ZERO);
Kirill A. Shutemov1ae9ae52013-11-14 14:31:42 -08002942 if (!page)
2943 return NULL;
2944 if (!pgtable_page_ctor(page)) {
2945 free_hot_cold_page(page, 0);
2946 return NULL;
David Millerc460bec2012-10-08 16:34:22 -07002947 }
Kirill A. Shutemov1ae9ae52013-11-14 14:31:42 -08002948 return (pte_t *) page_address(page);
David Millerc460bec2012-10-08 16:34:22 -07002949}
2950
2951void pte_free_kernel(struct mm_struct *mm, pte_t *pte)
2952{
David S. Miller37b3a8f2013-09-25 13:48:49 -07002953 free_page((unsigned long)pte);
David Millerc460bec2012-10-08 16:34:22 -07002954}
2955
2956static void __pte_free(pgtable_t pte)
2957{
2958 struct page *page = virt_to_page(pte);
David S. Miller37b3a8f2013-09-25 13:48:49 -07002959
2960 pgtable_page_dtor(page);
2961 __free_page(page);
David Millerc460bec2012-10-08 16:34:22 -07002962}
2963
2964void pte_free(struct mm_struct *mm, pgtable_t pte)
2965{
2966 __pte_free(pte);
2967}
2968
2969void pgtable_free(void *table, bool is_page)
2970{
2971 if (is_page)
2972 __pte_free(table);
2973 else
2974 kmem_cache_free(pgtable_cache, table);
2975}
David Miller9e695d22012-10-08 16:34:29 -07002976
2977#ifdef CONFIG_TRANSPARENT_HUGEPAGE
David Miller9e695d22012-10-08 16:34:29 -07002978void update_mmu_cache_pmd(struct vm_area_struct *vma, unsigned long addr,
2979 pmd_t *pmd)
2980{
2981 unsigned long pte, flags;
2982 struct mm_struct *mm;
2983 pmd_t entry = *pmd;
David Miller9e695d22012-10-08 16:34:29 -07002984
2985 if (!pmd_large(entry) || !pmd_young(entry))
2986 return;
2987
David S. Millera7b94032013-09-26 13:45:15 -07002988 pte = pmd_val(entry);
David Miller9e695d22012-10-08 16:34:29 -07002989
David S. Miller18f38132014-08-04 16:34:01 -07002990 /* Don't insert a non-valid PMD into the TSB, we'll deadlock. */
2991 if (!(pte & _PAGE_VALID))
2992 return;
2993
David S. Miller37b3a8f2013-09-25 13:48:49 -07002994 /* We are fabricating 8MB pages using 4MB real hw pages. */
2995 pte |= (addr & (1UL << REAL_HPAGE_SHIFT));
David Miller9e695d22012-10-08 16:34:29 -07002996
2997 mm = vma->vm_mm;
2998
2999 spin_lock_irqsave(&mm->context.lock, flags);
3000
3001 if (mm->context.tsb_block[MM_TSB_HUGE].tsb != NULL)
David S. Miller37b3a8f2013-09-25 13:48:49 -07003002 __update_mmu_tsb_insert(mm, MM_TSB_HUGE, REAL_HPAGE_SHIFT,
David Miller9e695d22012-10-08 16:34:29 -07003003 addr, pte);
3004
3005 spin_unlock_irqrestore(&mm->context.lock, flags);
3006}
3007#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
3008
3009#if defined(CONFIG_HUGETLB_PAGE) || defined(CONFIG_TRANSPARENT_HUGEPAGE)
3010static void context_reload(void *__data)
3011{
3012 struct mm_struct *mm = __data;
3013
3014 if (mm == current->mm)
3015 load_secondary_context(mm);
3016}
3017
David S. Miller0fbebed2013-02-19 22:34:10 -08003018void hugetlb_setup(struct pt_regs *regs)
David Miller9e695d22012-10-08 16:34:29 -07003019{
David S. Miller0fbebed2013-02-19 22:34:10 -08003020 struct mm_struct *mm = current->mm;
3021 struct tsb_config *tp;
David Miller9e695d22012-10-08 16:34:29 -07003022
David Hildenbrand70ffdb92015-05-11 17:52:11 +02003023 if (faulthandler_disabled() || !mm) {
David S. Miller0fbebed2013-02-19 22:34:10 -08003024 const struct exception_table_entry *entry;
David Miller9e695d22012-10-08 16:34:29 -07003025
David S. Miller0fbebed2013-02-19 22:34:10 -08003026 entry = search_exception_tables(regs->tpc);
3027 if (entry) {
3028 regs->tpc = entry->fixup;
3029 regs->tnpc = regs->tpc + 4;
3030 return;
3031 }
3032 pr_alert("Unexpected HugeTLB setup in atomic context.\n");
3033 die_if_kernel("HugeTSB in atomic", regs);
3034 }
3035
3036 tp = &mm->context.tsb_block[MM_TSB_HUGE];
3037 if (likely(tp->tsb == NULL))
3038 tsb_grow(mm, MM_TSB_HUGE, 0);
3039
David Miller9e695d22012-10-08 16:34:29 -07003040 tsb_context_switch(mm);
3041 smp_tsb_sync(mm);
3042
3043 /* On UltraSPARC-III+ and later, configure the second half of
3044 * the Data-TLB for huge pages.
3045 */
3046 if (tlb_type == cheetah_plus) {
David S. Miller9ea46abe2016-05-25 12:51:20 -07003047 bool need_context_reload = false;
David Miller9e695d22012-10-08 16:34:29 -07003048 unsigned long ctx;
3049
David S. Miller9ea46abe2016-05-25 12:51:20 -07003050 spin_lock_irq(&ctx_alloc_lock);
David Miller9e695d22012-10-08 16:34:29 -07003051 ctx = mm->context.sparc64_ctx_val;
3052 ctx &= ~CTX_PGSZ_MASK;
3053 ctx |= CTX_PGSZ_BASE << CTX_PGSZ0_SHIFT;
3054 ctx |= CTX_PGSZ_HUGE << CTX_PGSZ1_SHIFT;
3055
3056 if (ctx != mm->context.sparc64_ctx_val) {
3057 /* When changing the page size fields, we
3058 * must perform a context flush so that no
3059 * stale entries match. This flush must
3060 * occur with the original context register
3061 * settings.
3062 */
3063 do_flush_tlb_mm(mm);
3064
3065 /* Reload the context register of all processors
3066 * also executing in this address space.
3067 */
3068 mm->context.sparc64_ctx_val = ctx;
David S. Miller9ea46abe2016-05-25 12:51:20 -07003069 need_context_reload = true;
David Miller9e695d22012-10-08 16:34:29 -07003070 }
David S. Miller9ea46abe2016-05-25 12:51:20 -07003071 spin_unlock_irq(&ctx_alloc_lock);
3072
3073 if (need_context_reload)
3074 on_each_cpu(context_reload, mm, 0);
David Miller9e695d22012-10-08 16:34:29 -07003075 }
3076}
3077#endif
bob piccof6d4fb52014-03-03 11:54:42 -05003078
3079static struct resource code_resource = {
3080 .name = "Kernel code",
Toshi Kani35d98e92016-01-26 21:57:22 +01003081 .flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM
bob piccof6d4fb52014-03-03 11:54:42 -05003082};
3083
3084static struct resource data_resource = {
3085 .name = "Kernel data",
Toshi Kani35d98e92016-01-26 21:57:22 +01003086 .flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM
bob piccof6d4fb52014-03-03 11:54:42 -05003087};
3088
3089static struct resource bss_resource = {
3090 .name = "Kernel bss",
Toshi Kani35d98e92016-01-26 21:57:22 +01003091 .flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM
bob piccof6d4fb52014-03-03 11:54:42 -05003092};
3093
3094static inline resource_size_t compute_kern_paddr(void *addr)
3095{
3096 return (resource_size_t) (addr - KERNBASE + kern_base);
3097}
3098
3099static void __init kernel_lds_init(void)
3100{
3101 code_resource.start = compute_kern_paddr(_text);
3102 code_resource.end = compute_kern_paddr(_etext - 1);
3103 data_resource.start = compute_kern_paddr(_etext);
3104 data_resource.end = compute_kern_paddr(_edata - 1);
3105 bss_resource.start = compute_kern_paddr(__bss_start);
3106 bss_resource.end = compute_kern_paddr(_end - 1);
3107}
3108
3109static int __init report_memory(void)
3110{
3111 int i;
3112 struct resource *res;
3113
3114 kernel_lds_init();
3115
3116 for (i = 0; i < pavail_ents; i++) {
3117 res = kzalloc(sizeof(struct resource), GFP_KERNEL);
3118
3119 if (!res) {
3120 pr_warn("Failed to allocate source.\n");
3121 break;
3122 }
3123
3124 res->name = "System RAM";
3125 res->start = pavail[i].phys_addr;
3126 res->end = pavail[i].phys_addr + pavail[i].reg_size - 1;
Toshi Kani35d98e92016-01-26 21:57:22 +01003127 res->flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM;
bob piccof6d4fb52014-03-03 11:54:42 -05003128
3129 if (insert_resource(&iomem_resource, res) < 0) {
3130 pr_warn("Resource insertion failed.\n");
3131 break;
3132 }
3133
3134 insert_resource(res, &code_resource);
3135 insert_resource(res, &data_resource);
3136 insert_resource(res, &bss_resource);
3137 }
3138
3139 return 0;
3140}
David S. Miller3c081582015-03-18 19:15:28 -07003141arch_initcall(report_memory);
David S. Millere9011d02014-08-05 18:57:18 -07003142
David S. Miller4ca9a232014-08-04 20:07:37 -07003143#ifdef CONFIG_SMP
3144#define do_flush_tlb_kernel_range smp_flush_tlb_kernel_range
3145#else
3146#define do_flush_tlb_kernel_range __flush_tlb_kernel_range
3147#endif
3148
3149void flush_tlb_kernel_range(unsigned long start, unsigned long end)
3150{
3151 if (start < HI_OBP_ADDRESS && end > LOW_OBP_ADDRESS) {
3152 if (start < LOW_OBP_ADDRESS) {
3153 flush_tsb_kernel_range(start, LOW_OBP_ADDRESS);
3154 do_flush_tlb_kernel_range(start, LOW_OBP_ADDRESS);
3155 }
3156 if (end > HI_OBP_ADDRESS) {
David S. Miller473ad7f2014-10-04 21:05:14 -07003157 flush_tsb_kernel_range(HI_OBP_ADDRESS, end);
3158 do_flush_tlb_kernel_range(HI_OBP_ADDRESS, end);
David S. Miller4ca9a232014-08-04 20:07:37 -07003159 }
3160 } else {
3161 flush_tsb_kernel_range(start, end);
3162 do_flush_tlb_kernel_range(start, end);
3163 }
3164}