blob: 9ed22b571a054d83f84bccf3c4fc5ba26e7286dd [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
David S. Millerc4bce902006-02-11 21:57:54 -08008#include <linux/module.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>
David S. Miller5cbc3072007-05-25 15:49:59 -070025#include <linux/percpu.h>
Yinghai Lu95f72d12010-07-12 14:36:09 +100026#include <linux/memblock.h>
David S. Miller919ee672008-04-23 05:40:25 -070027#include <linux/mmzone.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30#include <asm/head.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <asm/page.h>
32#include <asm/pgalloc.h>
33#include <asm/pgtable.h>
34#include <asm/oplib.h>
35#include <asm/iommu.h>
36#include <asm/io.h>
37#include <asm/uaccess.h>
38#include <asm/mmu_context.h>
39#include <asm/tlbflush.h>
40#include <asm/dma.h>
41#include <asm/starfire.h>
42#include <asm/tlb.h>
43#include <asm/spitfire.h>
44#include <asm/sections.h>
David S. Miller517af332006-02-01 15:55:21 -080045#include <asm/tsb.h>
David S. Miller481295f2006-02-07 21:51:08 -080046#include <asm/hypervisor.h>
David S. Miller372b07b2006-06-21 15:35:28 -070047#include <asm/prom.h>
David S. Miller5cbc3072007-05-25 15:49:59 -070048#include <asm/mdesc.h>
David S. Miller3d5ae6b2008-03-25 21:51:40 -070049#include <asm/cpudata.h>
David S. Miller4f70f7a2008-08-12 18:33:56 -070050#include <asm/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Sam Ravnborg27137e52008-11-16 20:08:45 -080052#include "init_64.h"
David S. Miller9cc3a1a2006-02-21 20:51:13 -080053
54unsigned long kern_linear_pte_xor[2] __read_mostly;
55
56/* A bitmap, one bit for every 256MB of physical memory. If the bit
57 * is clear, we should use a 4MB page (via kern_linear_pte_xor[0]) else
58 * if set we should use a 256MB page (via kern_linear_pte_xor[1]).
59 */
60unsigned long kpte_linear_bitmap[KPTE_BITMAP_BYTES / sizeof(unsigned long)];
61
David S. Millerd1acb422007-03-16 17:20:28 -070062#ifndef CONFIG_DEBUG_PAGEALLOC
David S. Miller2d9e2762007-05-29 01:58:31 -070063/* A special kernel TSB for 4MB and 256MB linear mappings.
64 * Space is allocated for this right after the trap table
65 * in arch/sparc64/kernel/head.S
66 */
67extern struct tsb swapper_4m_tsb[KERNEL_TSB4M_NENTRIES];
David S. Millerd1acb422007-03-16 17:20:28 -070068#endif
David S. Millerd7744a02006-02-21 22:31:11 -080069
David S. Miller13edad72005-09-29 17:58:26 -070070#define MAX_BANKS 32
David S. Miller10147572005-09-28 21:46:43 -070071
David S. Miller9a2ed5c2009-04-07 01:03:58 -070072static struct linux_prom64_registers pavail[MAX_BANKS] __devinitdata;
73static int pavail_ents __devinitdata;
David S. Miller10147572005-09-28 21:46:43 -070074
David S. Miller13edad72005-09-29 17:58:26 -070075static int cmp_p64(const void *a, const void *b)
76{
77 const struct linux_prom64_registers *x = a, *y = b;
78
79 if (x->phys_addr > y->phys_addr)
80 return 1;
81 if (x->phys_addr < y->phys_addr)
82 return -1;
83 return 0;
84}
85
86static void __init read_obp_memory(const char *property,
87 struct linux_prom64_registers *regs,
88 int *num_ents)
89{
Andres Salomon8d125562010-10-08 14:18:11 -070090 phandle node = prom_finddevice("/memory");
David S. Miller13edad72005-09-29 17:58:26 -070091 int prop_size = prom_getproplen(node, property);
92 int ents, ret, i;
93
94 ents = prop_size / sizeof(struct linux_prom64_registers);
95 if (ents > MAX_BANKS) {
96 prom_printf("The machine has more %s property entries than "
97 "this kernel can support (%d).\n",
98 property, MAX_BANKS);
99 prom_halt();
100 }
101
102 ret = prom_getproperty(node, property, (char *) regs, prop_size);
103 if (ret == -1) {
104 prom_printf("Couldn't get %s property from /memory.\n");
105 prom_halt();
106 }
107
David S. Miller13edad72005-09-29 17:58:26 -0700108 /* Sanitize what we got from the firmware, by page aligning
109 * everything.
110 */
111 for (i = 0; i < ents; i++) {
112 unsigned long base, size;
113
114 base = regs[i].phys_addr;
115 size = regs[i].reg_size;
116
117 size &= PAGE_MASK;
118 if (base & ~PAGE_MASK) {
119 unsigned long new_base = PAGE_ALIGN(base);
120
121 size -= new_base - base;
122 if ((long) size < 0L)
123 size = 0UL;
124 base = new_base;
125 }
David S. Miller0015d3d2007-03-15 00:06:34 -0700126 if (size == 0UL) {
127 /* If it is empty, simply get rid of it.
128 * This simplifies the logic of the other
129 * functions that process these arrays.
130 */
131 memmove(&regs[i], &regs[i + 1],
132 (ents - i - 1) * sizeof(regs[0]));
133 i--;
134 ents--;
135 continue;
136 }
David S. Miller13edad72005-09-29 17:58:26 -0700137 regs[i].phys_addr = base;
138 regs[i].reg_size = size;
139 }
David S. Miller486ad102006-06-22 00:00:00 -0700140
David S. Miller486ad102006-06-22 00:00:00 -0700141 *num_ents = ents;
142
David S. Millerc9c10832005-10-12 12:22:46 -0700143 sort(regs, ents, sizeof(struct linux_prom64_registers),
David S. Miller13edad72005-09-29 17:58:26 -0700144 cmp_p64, NULL);
145}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
David S. Millerd8ed1d42009-08-25 16:47:46 -0700147unsigned long sparc64_valid_addr_bitmap[VALID_ADDR_BITMAP_BYTES /
148 sizeof(unsigned long)];
Sam Ravnborg917c3662009-01-08 16:58:20 -0800149EXPORT_SYMBOL(sparc64_valid_addr_bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
David S. Millerd1112012006-03-08 02:16:07 -0800151/* Kernel physical address base and size in bytes. */
David S. Miller1ac4f5e2005-09-21 21:49:32 -0700152unsigned long kern_base __read_mostly;
153unsigned long kern_size __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155/* Initial ramdisk setup */
156extern unsigned long sparc_ramdisk_image64;
157extern unsigned int sparc_ramdisk_image;
158extern unsigned int sparc_ramdisk_size;
159
David S. Miller1ac4f5e2005-09-21 21:49:32 -0700160struct page *mem_map_zero __read_mostly;
Aneesh Kumar K.V35802c02008-04-29 08:11:12 -0400161EXPORT_SYMBOL(mem_map_zero);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
David S. Miller0835ae02005-10-04 15:23:20 -0700163unsigned int sparc64_highest_unlocked_tlb_ent __read_mostly;
164
165unsigned long sparc64_kern_pri_context __read_mostly;
166unsigned long sparc64_kern_pri_nuc_bits __read_mostly;
167unsigned long sparc64_kern_sec_context __read_mostly;
168
David S. Miller64658742008-03-21 17:01:38 -0700169int num_kernel_image_mappings;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171#ifdef CONFIG_DEBUG_DCFLUSH
172atomic_t dcpage_flushes = ATOMIC_INIT(0);
173#ifdef CONFIG_SMP
174atomic_t dcpage_flushes_xcall = ATOMIC_INIT(0);
175#endif
176#endif
177
David S. Miller7a591cf2006-02-26 19:44:50 -0800178inline void flush_dcache_page_impl(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
David S. Miller7a591cf2006-02-26 19:44:50 -0800180 BUG_ON(tlb_type == hypervisor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181#ifdef CONFIG_DEBUG_DCFLUSH
182 atomic_inc(&dcpage_flushes);
183#endif
184
185#ifdef DCACHE_ALIASING_POSSIBLE
186 __flush_dcache_page(page_address(page),
187 ((tlb_type == spitfire) &&
188 page_mapping(page) != NULL));
189#else
190 if (page_mapping(page) != NULL &&
191 tlb_type == spitfire)
192 __flush_icache_page(__pa(page_address(page)));
193#endif
194}
195
196#define PG_dcache_dirty PG_arch_1
David S. Miller22adb352007-05-26 01:14:43 -0700197#define PG_dcache_cpu_shift 32UL
198#define PG_dcache_cpu_mask \
199 ((1UL<<ilog2(roundup_pow_of_two(NR_CPUS)))-1UL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201#define dcache_dirty_cpu(page) \
David S. Miller48b0e542005-07-27 16:08:44 -0700202 (((page)->flags >> PG_dcache_cpu_shift) & PG_dcache_cpu_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
David S. Millerd979f172007-10-27 00:13:04 -0700204static inline void set_dcache_dirty(struct page *page, int this_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
206 unsigned long mask = this_cpu;
David S. Miller48b0e542005-07-27 16:08:44 -0700207 unsigned long non_cpu_bits;
208
209 non_cpu_bits = ~(PG_dcache_cpu_mask << PG_dcache_cpu_shift);
210 mask = (mask << PG_dcache_cpu_shift) | (1UL << PG_dcache_dirty);
211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 __asm__ __volatile__("1:\n\t"
213 "ldx [%2], %%g7\n\t"
214 "and %%g7, %1, %%g1\n\t"
215 "or %%g1, %0, %%g1\n\t"
216 "casx [%2], %%g7, %%g1\n\t"
217 "cmp %%g7, %%g1\n\t"
218 "bne,pn %%xcc, 1b\n\t"
David S. Millerb445e262005-06-27 15:42:04 -0700219 " nop"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 : /* no outputs */
221 : "r" (mask), "r" (non_cpu_bits), "r" (&page->flags)
222 : "g1", "g7");
223}
224
David S. Millerd979f172007-10-27 00:13:04 -0700225static inline void clear_dcache_dirty_cpu(struct page *page, unsigned long cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
227 unsigned long mask = (1UL << PG_dcache_dirty);
228
229 __asm__ __volatile__("! test_and_clear_dcache_dirty\n"
230 "1:\n\t"
231 "ldx [%2], %%g7\n\t"
David S. Miller48b0e542005-07-27 16:08:44 -0700232 "srlx %%g7, %4, %%g1\n\t"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 "and %%g1, %3, %%g1\n\t"
234 "cmp %%g1, %0\n\t"
235 "bne,pn %%icc, 2f\n\t"
236 " andn %%g7, %1, %%g1\n\t"
237 "casx [%2], %%g7, %%g1\n\t"
238 "cmp %%g7, %%g1\n\t"
239 "bne,pn %%xcc, 1b\n\t"
David S. Millerb445e262005-06-27 15:42:04 -0700240 " nop\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 "2:"
242 : /* no outputs */
243 : "r" (cpu), "r" (mask), "r" (&page->flags),
David S. Miller48b0e542005-07-27 16:08:44 -0700244 "i" (PG_dcache_cpu_mask),
245 "i" (PG_dcache_cpu_shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 : "g1", "g7");
247}
248
David S. Miller517af332006-02-01 15:55:21 -0800249static inline void tsb_insert(struct tsb *ent, unsigned long tag, unsigned long pte)
250{
251 unsigned long tsb_addr = (unsigned long) ent;
252
David S. Miller3b3ab2e2006-02-17 09:54:42 -0800253 if (tlb_type == cheetah_plus || tlb_type == hypervisor)
David S. Miller517af332006-02-01 15:55:21 -0800254 tsb_addr = __pa(tsb_addr);
255
256 __tsb_insert(tsb_addr, tag, pte);
257}
258
David S. Millerc4bce902006-02-11 21:57:54 -0800259unsigned long _PAGE_ALL_SZ_BITS __read_mostly;
260unsigned long _PAGE_SZBITS __read_mostly;
261
Sam Ravnborgff9aefb2009-01-06 12:51:26 -0800262static void flush_dcache(unsigned long pfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
Sam Ravnborgff9aefb2009-01-06 12:51:26 -0800264 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Sam Ravnborgff9aefb2009-01-06 12:51:26 -0800266 page = pfn_to_page(pfn);
David S. Miller1a78ced2009-10-12 03:20:57 -0700267 if (page) {
David S. Miller7a591cf2006-02-26 19:44:50 -0800268 unsigned long pg_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Sam Ravnborgff9aefb2009-01-06 12:51:26 -0800270 pg_flags = page->flags;
271 if (pg_flags & (1UL << PG_dcache_dirty)) {
David S. Miller7a591cf2006-02-26 19:44:50 -0800272 int cpu = ((pg_flags >> PG_dcache_cpu_shift) &
273 PG_dcache_cpu_mask);
274 int this_cpu = get_cpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
David S. Miller7a591cf2006-02-26 19:44:50 -0800276 /* This is just to optimize away some function calls
277 * in the SMP case.
278 */
279 if (cpu == this_cpu)
280 flush_dcache_page_impl(page);
281 else
282 smp_flush_dcache_page_impl(page, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
David S. Miller7a591cf2006-02-26 19:44:50 -0800284 clear_dcache_dirty_cpu(page, cpu);
285
286 put_cpu();
287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 }
Sam Ravnborgff9aefb2009-01-06 12:51:26 -0800289}
290
Russell King4b3073e2009-12-18 16:40:18 +0000291void update_mmu_cache(struct vm_area_struct *vma, unsigned long address, pte_t *ptep)
Sam Ravnborgff9aefb2009-01-06 12:51:26 -0800292{
293 struct mm_struct *mm;
294 struct tsb *tsb;
295 unsigned long tag, flags;
296 unsigned long tsb_index, tsb_hash_shift;
Russell King4b3073e2009-12-18 16:40:18 +0000297 pte_t pte = *ptep;
Sam Ravnborgff9aefb2009-01-06 12:51:26 -0800298
299 if (tlb_type != hypervisor) {
300 unsigned long pfn = pte_pfn(pte);
301
302 if (pfn_valid(pfn))
303 flush_dcache(pfn);
304 }
David S. Millerbd407912006-01-31 18:31:38 -0800305
306 mm = vma->vm_mm;
David S. Miller7a1ac522006-03-16 02:02:32 -0800307
David S. Millerdcc1e8d2006-03-22 00:49:59 -0800308 tsb_index = MM_TSB_BASE;
309 tsb_hash_shift = PAGE_SHIFT;
310
David S. Millerafa9b042014-08-04 16:34:01 -0700311 /* Don't insert a non-valid PTE into the TSB, we'll deadlock. */
312 if (!(pte_val(pte) & _PAGE_VALID))
313 return;
314
David S. Miller7a1ac522006-03-16 02:02:32 -0800315 spin_lock_irqsave(&mm->context.lock, flags);
316
David S. Millerdcc1e8d2006-03-22 00:49:59 -0800317#ifdef CONFIG_HUGETLB_PAGE
318 if (mm->context.tsb_block[MM_TSB_HUGE].tsb != NULL) {
319 if ((tlb_type == hypervisor &&
320 (pte_val(pte) & _PAGE_SZALL_4V) == _PAGE_SZHUGE_4V) ||
321 (tlb_type != hypervisor &&
322 (pte_val(pte) & _PAGE_SZALL_4U) == _PAGE_SZHUGE_4U)) {
323 tsb_index = MM_TSB_HUGE;
324 tsb_hash_shift = HPAGE_SHIFT;
325 }
326 }
327#endif
328
329 tsb = mm->context.tsb_block[tsb_index].tsb;
330 tsb += ((address >> tsb_hash_shift) &
331 (mm->context.tsb_block[tsb_index].tsb_nentries - 1UL));
David S. Miller74ae9982006-03-05 18:26:24 -0800332 tag = (address >> 22UL);
333 tsb_insert(tsb, tag, pte_val(pte));
David S. Miller7a1ac522006-03-16 02:02:32 -0800334
335 spin_unlock_irqrestore(&mm->context.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336}
337
338void flush_dcache_page(struct page *page)
339{
David S. Millera9546f52005-04-17 18:03:09 -0700340 struct address_space *mapping;
341 int this_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
David S. Miller7a591cf2006-02-26 19:44:50 -0800343 if (tlb_type == hypervisor)
344 return;
345
David S. Millera9546f52005-04-17 18:03:09 -0700346 /* Do not bother with the expensive D-cache flush if it
347 * is merely the zero page. The 'bigcore' testcase in GDB
348 * causes this case to run millions of times.
349 */
350 if (page == ZERO_PAGE(0))
351 return;
352
353 this_cpu = get_cpu();
354
355 mapping = page_mapping(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 if (mapping && !mapping_mapped(mapping)) {
David S. Millera9546f52005-04-17 18:03:09 -0700357 int dirty = test_bit(PG_dcache_dirty, &page->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 if (dirty) {
David S. Millera9546f52005-04-17 18:03:09 -0700359 int dirty_cpu = dcache_dirty_cpu(page);
360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 if (dirty_cpu == this_cpu)
362 goto out;
363 smp_flush_dcache_page_impl(page, dirty_cpu);
364 }
365 set_dcache_dirty(page, this_cpu);
366 } else {
367 /* We could delay the flush for the !page_mapping
368 * case too. But that case is for exec env/arg
369 * pages and those are %99 certainly going to get
370 * faulted into the tlb (and thus flushed) anyways.
371 */
372 flush_dcache_page_impl(page);
373 }
374
375out:
376 put_cpu();
377}
Sam Ravnborg917c3662009-01-08 16:58:20 -0800378EXPORT_SYMBOL(flush_dcache_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Prasanna S Panchamukhi05e14cb2005-09-06 15:19:30 -0700380void __kprobes flush_icache_range(unsigned long start, unsigned long end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
David S. Millera43fe0e2006-02-04 03:10:53 -0800382 /* Cheetah and Hypervisor platform cpus have coherent I-cache. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 if (tlb_type == spitfire) {
384 unsigned long kaddr;
385
David S. Millera94aa252007-03-15 15:50:11 -0700386 /* This code only runs on Spitfire cpus so this is
387 * why we can assume _PAGE_PADDR_4U.
388 */
389 for (kaddr = start; kaddr < end; kaddr += PAGE_SIZE) {
390 unsigned long paddr, mask = _PAGE_PADDR_4U;
391
392 if (kaddr >= PAGE_OFFSET)
393 paddr = kaddr & mask;
394 else {
395 pgd_t *pgdp = pgd_offset_k(kaddr);
396 pud_t *pudp = pud_offset(pgdp, kaddr);
397 pmd_t *pmdp = pmd_offset(pudp, kaddr);
398 pte_t *ptep = pte_offset_kernel(pmdp, kaddr);
399
400 paddr = pte_val(*ptep) & mask;
401 }
402 __flush_icache_page(paddr);
403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 }
405}
Sam Ravnborg917c3662009-01-08 16:58:20 -0800406EXPORT_SYMBOL(flush_icache_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408void mmu_info(struct seq_file *m)
409{
410 if (tlb_type == cheetah)
411 seq_printf(m, "MMU Type\t: Cheetah\n");
412 else if (tlb_type == cheetah_plus)
413 seq_printf(m, "MMU Type\t: Cheetah+\n");
414 else if (tlb_type == spitfire)
415 seq_printf(m, "MMU Type\t: Spitfire\n");
David S. Millera43fe0e2006-02-04 03:10:53 -0800416 else if (tlb_type == hypervisor)
417 seq_printf(m, "MMU Type\t: Hypervisor (sun4v)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 else
419 seq_printf(m, "MMU Type\t: ???\n");
420
421#ifdef CONFIG_DEBUG_DCFLUSH
422 seq_printf(m, "DCPageFlushes\t: %d\n",
423 atomic_read(&dcpage_flushes));
424#ifdef CONFIG_SMP
425 seq_printf(m, "DCPageFlushesXC\t: %d\n",
426 atomic_read(&dcpage_flushes_xcall));
427#endif /* CONFIG_SMP */
428#endif /* CONFIG_DEBUG_DCFLUSH */
429}
430
David S. Millera94aa252007-03-15 15:50:11 -0700431struct linux_prom_translation prom_trans[512] __read_mostly;
432unsigned int prom_trans_ents __read_mostly;
433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434unsigned long kern_locked_tte_data;
435
David S. Miller405599b2005-09-22 00:12:35 -0700436/* The obp translations are saved based on 8k pagesize, since obp can
437 * use a mixture of pagesizes. Misses to the LOW_OBP_ADDRESS ->
David S. Miller74bf4312006-01-31 18:29:18 -0800438 * HI_OBP_ADDRESS range are handled in ktlb.S.
David S. Miller405599b2005-09-22 00:12:35 -0700439 */
David S. Miller5085b4a2005-09-22 00:45:41 -0700440static inline int in_obp_range(unsigned long vaddr)
441{
442 return (vaddr >= LOW_OBP_ADDRESS &&
443 vaddr < HI_OBP_ADDRESS);
444}
445
David S. Millerc9c10832005-10-12 12:22:46 -0700446static int cmp_ptrans(const void *a, const void *b)
David S. Miller405599b2005-09-22 00:12:35 -0700447{
David S. Millerc9c10832005-10-12 12:22:46 -0700448 const struct linux_prom_translation *x = a, *y = b;
David S. Miller405599b2005-09-22 00:12:35 -0700449
David S. Millerc9c10832005-10-12 12:22:46 -0700450 if (x->virt > y->virt)
451 return 1;
452 if (x->virt < y->virt)
453 return -1;
454 return 0;
David S. Miller405599b2005-09-22 00:12:35 -0700455}
456
David S. Millerc9c10832005-10-12 12:22:46 -0700457/* Read OBP translations property into 'prom_trans[]'. */
David S. Miller9ad98c52005-10-05 15:12:00 -0700458static void __init read_obp_translations(void)
David S. Miller405599b2005-09-22 00:12:35 -0700459{
David S. Millerc9c10832005-10-12 12:22:46 -0700460 int n, node, ents, first, last, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462 node = prom_finddevice("/virtual-memory");
463 n = prom_getproplen(node, "translations");
David S. Miller405599b2005-09-22 00:12:35 -0700464 if (unlikely(n == 0 || n == -1)) {
David S. Millerb206fc42005-09-21 22:31:13 -0700465 prom_printf("prom_mappings: Couldn't get size.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 prom_halt();
467 }
David S. Miller405599b2005-09-22 00:12:35 -0700468 if (unlikely(n > sizeof(prom_trans))) {
469 prom_printf("prom_mappings: Size %Zd is too big.\n", n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 prom_halt();
471 }
David S. Miller405599b2005-09-22 00:12:35 -0700472
David S. Millerb206fc42005-09-21 22:31:13 -0700473 if ((n = prom_getproperty(node, "translations",
David S. Miller405599b2005-09-22 00:12:35 -0700474 (char *)&prom_trans[0],
475 sizeof(prom_trans))) == -1) {
David S. Millerb206fc42005-09-21 22:31:13 -0700476 prom_printf("prom_mappings: Couldn't get property.\n");
477 prom_halt();
478 }
David S. Miller9ad98c52005-10-05 15:12:00 -0700479
David S. Millerb206fc42005-09-21 22:31:13 -0700480 n = n / sizeof(struct linux_prom_translation);
David S. Miller9ad98c52005-10-05 15:12:00 -0700481
David S. Millerc9c10832005-10-12 12:22:46 -0700482 ents = n;
483
484 sort(prom_trans, ents, sizeof(struct linux_prom_translation),
485 cmp_ptrans, NULL);
486
487 /* Now kick out all the non-OBP entries. */
488 for (i = 0; i < ents; i++) {
489 if (in_obp_range(prom_trans[i].virt))
490 break;
491 }
492 first = i;
493 for (; i < ents; i++) {
494 if (!in_obp_range(prom_trans[i].virt))
495 break;
496 }
497 last = i;
498
499 for (i = 0; i < (last - first); i++) {
500 struct linux_prom_translation *src = &prom_trans[i + first];
501 struct linux_prom_translation *dest = &prom_trans[i];
502
503 *dest = *src;
504 }
505 for (; i < ents; i++) {
506 struct linux_prom_translation *dest = &prom_trans[i];
507 dest->virt = dest->size = dest->data = 0x0UL;
508 }
509
510 prom_trans_ents = last - first;
511
512 if (tlb_type == spitfire) {
513 /* Clear diag TTE bits. */
514 for (i = 0; i < prom_trans_ents; i++)
515 prom_trans[i].data &= ~0x0003fe0000000000UL;
516 }
David S. Millerf4142cb2011-09-29 12:18:59 -0700517
518 /* Force execute bit on. */
519 for (i = 0; i < prom_trans_ents; i++)
520 prom_trans[i].data |= (tlb_type == hypervisor ?
521 _PAGE_EXEC_4V : _PAGE_EXEC_4U);
David S. Miller405599b2005-09-22 00:12:35 -0700522}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
David S. Millerd82ace72006-02-09 02:52:44 -0800524static void __init hypervisor_tlb_lock(unsigned long vaddr,
525 unsigned long pte,
526 unsigned long mmu)
527{
David S. Miller7db35f32007-05-29 02:22:14 -0700528 unsigned long ret = sun4v_mmu_map_perm_addr(vaddr, 0, pte, mmu);
David S. Millerd82ace72006-02-09 02:52:44 -0800529
David S. Miller7db35f32007-05-29 02:22:14 -0700530 if (ret != 0) {
David S. Miller12e126a2006-02-17 14:40:30 -0800531 prom_printf("hypervisor_tlb_lock[%lx:%lx:%lx:%lx]: "
David S. Miller7db35f32007-05-29 02:22:14 -0700532 "errors with %lx\n", vaddr, 0, pte, mmu, ret);
David S. Miller12e126a2006-02-17 14:40:30 -0800533 prom_halt();
534 }
David S. Millerd82ace72006-02-09 02:52:44 -0800535}
536
David S. Millerc4bce902006-02-11 21:57:54 -0800537static unsigned long kern_large_tte(unsigned long paddr);
538
David S. Miller898cf0e2005-09-23 11:59:44 -0700539static void __init remap_kernel(void)
David S. Miller405599b2005-09-22 00:12:35 -0700540{
541 unsigned long phys_page, tte_vaddr, tte_data;
David S. Miller64658742008-03-21 17:01:38 -0700542 int i, tlb_ent = sparc64_highest_locked_tlbent();
David S. Miller405599b2005-09-22 00:12:35 -0700543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 tte_vaddr = (unsigned long) KERNBASE;
David S. Millerbff06d52005-09-22 20:11:33 -0700545 phys_page = (prom_boot_mapping_phys_low >> 22UL) << 22UL;
David S. Millerc4bce902006-02-11 21:57:54 -0800546 tte_data = kern_large_tte(phys_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
548 kern_locked_tte_data = tte_data;
549
David S. Millerd82ace72006-02-09 02:52:44 -0800550 /* Now lock us into the TLBs via Hypervisor or OBP. */
551 if (tlb_type == hypervisor) {
David S. Miller64658742008-03-21 17:01:38 -0700552 for (i = 0; i < num_kernel_image_mappings; i++) {
David S. Millerd82ace72006-02-09 02:52:44 -0800553 hypervisor_tlb_lock(tte_vaddr, tte_data, HV_MMU_DMMU);
554 hypervisor_tlb_lock(tte_vaddr, tte_data, HV_MMU_IMMU);
David S. Miller64658742008-03-21 17:01:38 -0700555 tte_vaddr += 0x400000;
556 tte_data += 0x400000;
David S. Millerd82ace72006-02-09 02:52:44 -0800557 }
558 } else {
David S. Miller64658742008-03-21 17:01:38 -0700559 for (i = 0; i < num_kernel_image_mappings; i++) {
560 prom_dtlb_load(tlb_ent - i, tte_data, tte_vaddr);
561 prom_itlb_load(tlb_ent - i, tte_data, tte_vaddr);
562 tte_vaddr += 0x400000;
563 tte_data += 0x400000;
David S. Millerd82ace72006-02-09 02:52:44 -0800564 }
David S. Miller64658742008-03-21 17:01:38 -0700565 sparc64_highest_unlocked_tlb_ent = tlb_ent - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 }
David S. Miller0835ae02005-10-04 15:23:20 -0700567 if (tlb_type == cheetah_plus) {
568 sparc64_kern_pri_context = (CTX_CHEETAH_PLUS_CTX0 |
569 CTX_CHEETAH_PLUS_NUC);
570 sparc64_kern_pri_nuc_bits = CTX_CHEETAH_PLUS_NUC;
571 sparc64_kern_sec_context = CTX_CHEETAH_PLUS_CTX0;
572 }
David S. Miller405599b2005-09-22 00:12:35 -0700573}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
David S. Miller405599b2005-09-22 00:12:35 -0700575
David S. Millerc9c10832005-10-12 12:22:46 -0700576static void __init inherit_prom_mappings(void)
David S. Miller9ad98c52005-10-05 15:12:00 -0700577{
David S. Miller405599b2005-09-22 00:12:35 -0700578 /* Now fixup OBP's idea about where we really are mapped. */
David S. Miller3c62a2d2008-02-17 23:22:50 -0800579 printk("Remapping the kernel... ");
David S. Miller405599b2005-09-22 00:12:35 -0700580 remap_kernel();
David S. Miller3c62a2d2008-02-17 23:22:50 -0800581 printk("done.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582}
583
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584void prom_world(int enter)
585{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 if (!enter)
587 set_fs((mm_segment_t) { get_thread_current_ds() });
588
David S. Miller3487d1d2006-01-31 18:33:25 -0800589 __asm__ __volatile__("flushw");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590}
591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592void __flush_dcache_range(unsigned long start, unsigned long end)
593{
594 unsigned long va;
595
596 if (tlb_type == spitfire) {
597 int n = 0;
598
599 for (va = start; va < end; va += 32) {
600 spitfire_put_dcache_tag(va & 0x3fe0, 0x0);
601 if (++n >= 512)
602 break;
603 }
David S. Millera43fe0e2006-02-04 03:10:53 -0800604 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 start = __pa(start);
606 end = __pa(end);
607 for (va = start; va < end; va += 32)
608 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
609 "membar #Sync"
610 : /* no outputs */
611 : "r" (va),
612 "i" (ASI_DCACHE_INVALIDATE));
613 }
614}
Sam Ravnborg917c3662009-01-08 16:58:20 -0800615EXPORT_SYMBOL(__flush_dcache_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
David S. Miller85f1e1f2007-03-15 17:51:26 -0700617/* get_new_mmu_context() uses "cache + 1". */
618DEFINE_SPINLOCK(ctx_alloc_lock);
619unsigned long tlb_context_cache = CTX_FIRST_VERSION - 1;
620#define MAX_CTX_NR (1UL << CTX_NR_BITS)
621#define CTX_BMAP_SLOTS BITS_TO_LONGS(MAX_CTX_NR)
622DECLARE_BITMAP(mmu_context_bmap, MAX_CTX_NR);
623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624/* Caller does TLB context flushing on local CPU if necessary.
625 * The caller also ensures that CTX_VALID(mm->context) is false.
626 *
627 * We must be careful about boundary cases so that we never
628 * let the user have CTX 0 (nucleus) or we ever use a CTX
629 * version of zero (and thus NO_CONTEXT would not be caught
630 * by version mis-match tests in mmu_context.h).
David S. Millera0663a72006-02-23 14:19:28 -0800631 *
632 * Always invoked with interrupts disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 */
634void get_new_mmu_context(struct mm_struct *mm)
635{
636 unsigned long ctx, new_ctx;
637 unsigned long orig_pgsz_bits;
David S. Millera77754b2006-03-06 19:59:50 -0800638 unsigned long flags;
David S. Millera0663a72006-02-23 14:19:28 -0800639 int new_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
David S. Millera77754b2006-03-06 19:59:50 -0800641 spin_lock_irqsave(&ctx_alloc_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 orig_pgsz_bits = (mm->context.sparc64_ctx_val & CTX_PGSZ_MASK);
643 ctx = (tlb_context_cache + 1) & CTX_NR_MASK;
644 new_ctx = find_next_zero_bit(mmu_context_bmap, 1 << CTX_NR_BITS, ctx);
David S. Millera0663a72006-02-23 14:19:28 -0800645 new_version = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 if (new_ctx >= (1 << CTX_NR_BITS)) {
647 new_ctx = find_next_zero_bit(mmu_context_bmap, ctx, 1);
648 if (new_ctx >= ctx) {
649 int i;
650 new_ctx = (tlb_context_cache & CTX_VERSION_MASK) +
651 CTX_FIRST_VERSION;
652 if (new_ctx == 1)
653 new_ctx = CTX_FIRST_VERSION;
654
655 /* Don't call memset, for 16 entries that's just
656 * plain silly...
657 */
658 mmu_context_bmap[0] = 3;
659 mmu_context_bmap[1] = 0;
660 mmu_context_bmap[2] = 0;
661 mmu_context_bmap[3] = 0;
662 for (i = 4; i < CTX_BMAP_SLOTS; i += 4) {
663 mmu_context_bmap[i + 0] = 0;
664 mmu_context_bmap[i + 1] = 0;
665 mmu_context_bmap[i + 2] = 0;
666 mmu_context_bmap[i + 3] = 0;
667 }
David S. Millera0663a72006-02-23 14:19:28 -0800668 new_version = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 goto out;
670 }
671 }
672 mmu_context_bmap[new_ctx>>6] |= (1UL << (new_ctx & 63));
673 new_ctx |= (tlb_context_cache & CTX_VERSION_MASK);
674out:
675 tlb_context_cache = new_ctx;
676 mm->context.sparc64_ctx_val = new_ctx | orig_pgsz_bits;
David S. Millera77754b2006-03-06 19:59:50 -0800677 spin_unlock_irqrestore(&ctx_alloc_lock, flags);
David S. Millera0663a72006-02-23 14:19:28 -0800678
679 if (unlikely(new_version))
680 smp_new_mmu_context_version();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681}
682
David S. Miller919ee672008-04-23 05:40:25 -0700683static int numa_enabled = 1;
684static int numa_debug;
685
686static int __init early_numa(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
David S. Miller919ee672008-04-23 05:40:25 -0700688 if (!p)
689 return 0;
David S. Millerd1112012006-03-08 02:16:07 -0800690
David S. Miller919ee672008-04-23 05:40:25 -0700691 if (strstr(p, "off"))
692 numa_enabled = 0;
David S. Millerd1112012006-03-08 02:16:07 -0800693
David S. Miller919ee672008-04-23 05:40:25 -0700694 if (strstr(p, "debug"))
695 numa_debug = 1;
696
697 return 0;
David S. Millerd1112012006-03-08 02:16:07 -0800698}
David S. Miller919ee672008-04-23 05:40:25 -0700699early_param("numa", early_numa);
700
701#define numadbg(f, a...) \
702do { if (numa_debug) \
703 printk(KERN_INFO f, ## a); \
704} while (0)
David S. Millerd1112012006-03-08 02:16:07 -0800705
David S. Miller4e82c9a2008-02-13 18:00:03 -0800706static void __init find_ramdisk(unsigned long phys_base)
707{
708#ifdef CONFIG_BLK_DEV_INITRD
709 if (sparc_ramdisk_image || sparc_ramdisk_image64) {
710 unsigned long ramdisk_image;
711
712 /* Older versions of the bootloader only supported a
713 * 32-bit physical address for the ramdisk image
714 * location, stored at sparc_ramdisk_image. Newer
715 * SILO versions set sparc_ramdisk_image to zero and
716 * provide a full 64-bit physical address at
717 * sparc_ramdisk_image64.
718 */
719 ramdisk_image = sparc_ramdisk_image;
720 if (!ramdisk_image)
721 ramdisk_image = sparc_ramdisk_image64;
722
723 /* Another bootloader quirk. The bootloader normalizes
724 * the physical address to KERNBASE, so we have to
725 * factor that back out and add in the lowest valid
726 * physical page address to get the true physical address.
727 */
728 ramdisk_image -= KERNBASE;
729 ramdisk_image += phys_base;
730
David S. Miller919ee672008-04-23 05:40:25 -0700731 numadbg("Found ramdisk at physical address 0x%lx, size %u\n",
732 ramdisk_image, sparc_ramdisk_size);
733
David S. Miller4e82c9a2008-02-13 18:00:03 -0800734 initrd_start = ramdisk_image;
735 initrd_end = ramdisk_image + sparc_ramdisk_size;
David S. Miller3b2a7e22008-02-13 18:13:20 -0800736
Yinghai Lu95f72d12010-07-12 14:36:09 +1000737 memblock_reserve(initrd_start, sparc_ramdisk_size);
David S. Millerd45100f2008-05-06 15:19:54 -0700738
739 initrd_start += PAGE_OFFSET;
740 initrd_end += PAGE_OFFSET;
David S. Miller4e82c9a2008-02-13 18:00:03 -0800741 }
742#endif
743}
744
David S. Miller919ee672008-04-23 05:40:25 -0700745struct node_mem_mask {
746 unsigned long mask;
747 unsigned long val;
748 unsigned long bootmem_paddr;
749};
750static struct node_mem_mask node_masks[MAX_NUMNODES];
751static int num_node_masks;
752
753int numa_cpu_lookup_table[NR_CPUS];
754cpumask_t numa_cpumask_lookup_table[MAX_NUMNODES];
755
756#ifdef CONFIG_NEED_MULTIPLE_NODES
David S. Miller919ee672008-04-23 05:40:25 -0700757
758struct mdesc_mblock {
759 u64 base;
760 u64 size;
761 u64 offset; /* RA-to-PA */
762};
763static struct mdesc_mblock *mblocks;
764static int num_mblocks;
765
766static unsigned long ra_to_pa(unsigned long addr)
David S. Millerd1112012006-03-08 02:16:07 -0800767{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 int i;
769
David S. Miller919ee672008-04-23 05:40:25 -0700770 for (i = 0; i < num_mblocks; i++) {
771 struct mdesc_mblock *m = &mblocks[i];
David S. Miller6fc5bae2006-12-28 21:00:23 -0800772
David S. Miller919ee672008-04-23 05:40:25 -0700773 if (addr >= m->base &&
774 addr < (m->base + m->size)) {
775 addr += m->offset;
776 break;
777 }
778 }
779 return addr;
780}
781
782static int find_node(unsigned long addr)
783{
784 int i;
785
786 addr = ra_to_pa(addr);
787 for (i = 0; i < num_node_masks; i++) {
788 struct node_mem_mask *p = &node_masks[i];
789
790 if ((addr & p->mask) == p->val)
791 return i;
792 }
793 return -1;
794}
795
Tejun Heof9b18db2011-07-12 10:46:32 +0200796static u64 memblock_nid_range(u64 start, u64 end, int *nid)
David S. Miller919ee672008-04-23 05:40:25 -0700797{
798 *nid = find_node(start);
799 start += PAGE_SIZE;
800 while (start < end) {
801 int n = find_node(start);
802
803 if (n != *nid)
804 break;
805 start += PAGE_SIZE;
806 }
807
David S. Millerc918dcc2008-08-14 01:41:39 -0700808 if (start > end)
809 start = end;
810
David S. Miller919ee672008-04-23 05:40:25 -0700811 return start;
812}
813#else
Tejun Heof9b18db2011-07-12 10:46:32 +0200814static u64 memblock_nid_range(u64 start, u64 end, int *nid)
David S. Miller919ee672008-04-23 05:40:25 -0700815{
816 *nid = 0;
817 return end;
818}
819#endif
820
821/* This must be invoked after performing all of the necessary
Tejun Heo2a4814d2011-12-08 10:22:08 -0800822 * memblock_set_node() calls for 'nid'. We need to be able to get
David S. Miller919ee672008-04-23 05:40:25 -0700823 * correct data from get_pfn_range_for_nid().
824 */
825static void __init allocate_node_data(int nid)
826{
827 unsigned long paddr, num_pages, start_pfn, end_pfn;
828 struct pglist_data *p;
829
830#ifdef CONFIG_NEED_MULTIPLE_NODES
Benjamin Herrenschmidt9d1e2492010-07-06 15:39:17 -0700831 paddr = memblock_alloc_try_nid(sizeof(struct pglist_data), SMP_CACHE_BYTES, nid);
David S. Miller919ee672008-04-23 05:40:25 -0700832 if (!paddr) {
833 prom_printf("Cannot allocate pglist_data for nid[%d]\n", nid);
834 prom_halt();
835 }
836 NODE_DATA(nid) = __va(paddr);
837 memset(NODE_DATA(nid), 0, sizeof(struct pglist_data));
838
Johannes Weinerb61bfa32008-07-23 21:26:55 -0700839 NODE_DATA(nid)->bdata = &bootmem_node_data[nid];
David S. Miller919ee672008-04-23 05:40:25 -0700840#endif
841
842 p = NODE_DATA(nid);
843
844 get_pfn_range_for_nid(nid, &start_pfn, &end_pfn);
845 p->node_start_pfn = start_pfn;
846 p->node_spanned_pages = end_pfn - start_pfn;
847
848 if (p->node_spanned_pages) {
849 num_pages = bootmem_bootmap_pages(p->node_spanned_pages);
850
Benjamin Herrenschmidt9d1e2492010-07-06 15:39:17 -0700851 paddr = memblock_alloc_try_nid(num_pages << PAGE_SHIFT, PAGE_SIZE, nid);
David S. Miller919ee672008-04-23 05:40:25 -0700852 if (!paddr) {
853 prom_printf("Cannot allocate bootmap for nid[%d]\n",
854 nid);
855 prom_halt();
856 }
857 node_masks[nid].bootmem_paddr = paddr;
858 }
859}
860
861static void init_node_masks_nonnuma(void)
862{
863 int i;
864
865 numadbg("Initializing tables for non-numa.\n");
866
867 node_masks[0].mask = node_masks[0].val = 0;
868 num_node_masks = 1;
869
870 for (i = 0; i < NR_CPUS; i++)
871 numa_cpu_lookup_table[i] = 0;
872
KOSAKI Motohirofb1fece2011-05-16 13:38:07 -0700873 cpumask_setall(&numa_cpumask_lookup_table[0]);
David S. Miller919ee672008-04-23 05:40:25 -0700874}
875
876#ifdef CONFIG_NEED_MULTIPLE_NODES
877struct pglist_data *node_data[MAX_NUMNODES];
878
879EXPORT_SYMBOL(numa_cpu_lookup_table);
880EXPORT_SYMBOL(numa_cpumask_lookup_table);
881EXPORT_SYMBOL(node_data);
882
883struct mdesc_mlgroup {
884 u64 node;
885 u64 latency;
886 u64 match;
887 u64 mask;
888};
889static struct mdesc_mlgroup *mlgroups;
890static int num_mlgroups;
891
892static int scan_pio_for_cfg_handle(struct mdesc_handle *md, u64 pio,
893 u32 cfg_handle)
894{
895 u64 arc;
896
897 mdesc_for_each_arc(arc, md, pio, MDESC_ARC_TYPE_FWD) {
898 u64 target = mdesc_arc_target(md, arc);
899 const u64 *val;
900
901 val = mdesc_get_property(md, target,
902 "cfg-handle", NULL);
903 if (val && *val == cfg_handle)
904 return 0;
905 }
906 return -ENODEV;
907}
908
909static int scan_arcs_for_cfg_handle(struct mdesc_handle *md, u64 grp,
910 u32 cfg_handle)
911{
912 u64 arc, candidate, best_latency = ~(u64)0;
913
914 candidate = MDESC_NODE_NULL;
915 mdesc_for_each_arc(arc, md, grp, MDESC_ARC_TYPE_FWD) {
916 u64 target = mdesc_arc_target(md, arc);
917 const char *name = mdesc_node_name(md, target);
918 const u64 *val;
919
920 if (strcmp(name, "pio-latency-group"))
921 continue;
922
923 val = mdesc_get_property(md, target, "latency", NULL);
924 if (!val)
925 continue;
926
927 if (*val < best_latency) {
928 candidate = target;
929 best_latency = *val;
930 }
931 }
932
933 if (candidate == MDESC_NODE_NULL)
934 return -ENODEV;
935
936 return scan_pio_for_cfg_handle(md, candidate, cfg_handle);
937}
938
939int of_node_to_nid(struct device_node *dp)
940{
941 const struct linux_prom64_registers *regs;
942 struct mdesc_handle *md;
943 u32 cfg_handle;
944 int count, nid;
945 u64 grp;
946
David S. Miller072bd412008-08-18 20:36:17 -0700947 /* This is the right thing to do on currently supported
948 * SUN4U NUMA platforms as well, as the PCI controller does
949 * not sit behind any particular memory controller.
950 */
David S. Miller919ee672008-04-23 05:40:25 -0700951 if (!mlgroups)
952 return -1;
953
954 regs = of_get_property(dp, "reg", NULL);
955 if (!regs)
956 return -1;
957
958 cfg_handle = (regs->phys_addr >> 32UL) & 0x0fffffff;
959
960 md = mdesc_grab();
961
962 count = 0;
963 nid = -1;
964 mdesc_for_each_node_by_name(md, grp, "group") {
965 if (!scan_arcs_for_cfg_handle(md, grp, cfg_handle)) {
966 nid = count;
967 break;
968 }
969 count++;
970 }
971
972 mdesc_release(md);
973
974 return nid;
975}
976
David S. Miller01c45382009-04-07 01:05:22 -0700977static void __init add_node_ranges(void)
David S. Miller919ee672008-04-23 05:40:25 -0700978{
Benjamin Herrenschmidt08b84792010-08-04 13:43:31 +1000979 struct memblock_region *reg;
David S. Miller919ee672008-04-23 05:40:25 -0700980
Benjamin Herrenschmidt08b84792010-08-04 13:43:31 +1000981 for_each_memblock(memory, reg) {
982 unsigned long size = reg->size;
David S. Miller919ee672008-04-23 05:40:25 -0700983 unsigned long start, end;
984
Benjamin Herrenschmidt08b84792010-08-04 13:43:31 +1000985 start = reg->base;
David S. Miller919ee672008-04-23 05:40:25 -0700986 end = start + size;
987 while (start < end) {
988 unsigned long this_end;
989 int nid;
990
Benjamin Herrenschmidt35a1f0b2010-07-06 15:38:58 -0700991 this_end = memblock_nid_range(start, end, &nid);
David S. Miller919ee672008-04-23 05:40:25 -0700992
Tejun Heo2a4814d2011-12-08 10:22:08 -0800993 numadbg("Setting memblock NUMA node nid[%d] "
David S. Miller919ee672008-04-23 05:40:25 -0700994 "start[%lx] end[%lx]\n",
995 nid, start, this_end);
996
Tejun Heo2a4814d2011-12-08 10:22:08 -0800997 memblock_set_node(start, this_end - start, nid);
David S. Miller919ee672008-04-23 05:40:25 -0700998 start = this_end;
999 }
1000 }
1001}
1002
1003static int __init grab_mlgroups(struct mdesc_handle *md)
1004{
1005 unsigned long paddr;
1006 int count = 0;
1007 u64 node;
1008
1009 mdesc_for_each_node_by_name(md, node, "memory-latency-group")
1010 count++;
1011 if (!count)
1012 return -ENOENT;
1013
Yinghai Lu95f72d12010-07-12 14:36:09 +10001014 paddr = memblock_alloc(count * sizeof(struct mdesc_mlgroup),
David S. Miller919ee672008-04-23 05:40:25 -07001015 SMP_CACHE_BYTES);
1016 if (!paddr)
1017 return -ENOMEM;
1018
1019 mlgroups = __va(paddr);
1020 num_mlgroups = count;
1021
1022 count = 0;
1023 mdesc_for_each_node_by_name(md, node, "memory-latency-group") {
1024 struct mdesc_mlgroup *m = &mlgroups[count++];
1025 const u64 *val;
1026
1027 m->node = node;
1028
1029 val = mdesc_get_property(md, node, "latency", NULL);
1030 m->latency = *val;
1031 val = mdesc_get_property(md, node, "address-match", NULL);
1032 m->match = *val;
1033 val = mdesc_get_property(md, node, "address-mask", NULL);
1034 m->mask = *val;
1035
Sam Ravnborg90181132009-01-06 13:19:28 -08001036 numadbg("MLGROUP[%d]: node[%llx] latency[%llx] "
1037 "match[%llx] mask[%llx]\n",
David S. Miller919ee672008-04-23 05:40:25 -07001038 count - 1, m->node, m->latency, m->match, m->mask);
1039 }
1040
1041 return 0;
1042}
1043
1044static int __init grab_mblocks(struct mdesc_handle *md)
1045{
1046 unsigned long paddr;
1047 int count = 0;
1048 u64 node;
1049
1050 mdesc_for_each_node_by_name(md, node, "mblock")
1051 count++;
1052 if (!count)
1053 return -ENOENT;
1054
Yinghai Lu95f72d12010-07-12 14:36:09 +10001055 paddr = memblock_alloc(count * sizeof(struct mdesc_mblock),
David S. Miller919ee672008-04-23 05:40:25 -07001056 SMP_CACHE_BYTES);
1057 if (!paddr)
1058 return -ENOMEM;
1059
1060 mblocks = __va(paddr);
1061 num_mblocks = count;
1062
1063 count = 0;
1064 mdesc_for_each_node_by_name(md, node, "mblock") {
1065 struct mdesc_mblock *m = &mblocks[count++];
1066 const u64 *val;
1067
1068 val = mdesc_get_property(md, node, "base", NULL);
1069 m->base = *val;
1070 val = mdesc_get_property(md, node, "size", NULL);
1071 m->size = *val;
1072 val = mdesc_get_property(md, node,
1073 "address-congruence-offset", NULL);
bob picco88f74a12013-06-11 14:54:51 -04001074
1075 /* The address-congruence-offset property is optional.
1076 * Explicity zero it be identifty this.
1077 */
1078 if (val)
1079 m->offset = *val;
1080 else
1081 m->offset = 0UL;
David S. Miller919ee672008-04-23 05:40:25 -07001082
Sam Ravnborg90181132009-01-06 13:19:28 -08001083 numadbg("MBLOCK[%d]: base[%llx] size[%llx] offset[%llx]\n",
David S. Miller919ee672008-04-23 05:40:25 -07001084 count - 1, m->base, m->size, m->offset);
1085 }
1086
1087 return 0;
1088}
1089
1090static void __init numa_parse_mdesc_group_cpus(struct mdesc_handle *md,
1091 u64 grp, cpumask_t *mask)
1092{
1093 u64 arc;
1094
KOSAKI Motohirofb1fece2011-05-16 13:38:07 -07001095 cpumask_clear(mask);
David S. Miller919ee672008-04-23 05:40:25 -07001096
1097 mdesc_for_each_arc(arc, md, grp, MDESC_ARC_TYPE_BACK) {
1098 u64 target = mdesc_arc_target(md, arc);
1099 const char *name = mdesc_node_name(md, target);
1100 const u64 *id;
1101
1102 if (strcmp(name, "cpu"))
1103 continue;
1104 id = mdesc_get_property(md, target, "id", NULL);
Rusty Russelle305cb82009-03-16 14:40:23 +10301105 if (*id < nr_cpu_ids)
KOSAKI Motohirofb1fece2011-05-16 13:38:07 -07001106 cpumask_set_cpu(*id, mask);
David S. Miller919ee672008-04-23 05:40:25 -07001107 }
1108}
1109
1110static struct mdesc_mlgroup * __init find_mlgroup(u64 node)
1111{
1112 int i;
1113
1114 for (i = 0; i < num_mlgroups; i++) {
1115 struct mdesc_mlgroup *m = &mlgroups[i];
1116 if (m->node == node)
1117 return m;
1118 }
1119 return NULL;
1120}
1121
1122static int __init numa_attach_mlgroup(struct mdesc_handle *md, u64 grp,
1123 int index)
1124{
1125 struct mdesc_mlgroup *candidate = NULL;
1126 u64 arc, best_latency = ~(u64)0;
1127 struct node_mem_mask *n;
1128
1129 mdesc_for_each_arc(arc, md, grp, MDESC_ARC_TYPE_FWD) {
1130 u64 target = mdesc_arc_target(md, arc);
1131 struct mdesc_mlgroup *m = find_mlgroup(target);
1132 if (!m)
1133 continue;
1134 if (m->latency < best_latency) {
1135 candidate = m;
1136 best_latency = m->latency;
1137 }
1138 }
1139 if (!candidate)
1140 return -ENOENT;
1141
1142 if (num_node_masks != index) {
1143 printk(KERN_ERR "Inconsistent NUMA state, "
1144 "index[%d] != num_node_masks[%d]\n",
1145 index, num_node_masks);
1146 return -EINVAL;
1147 }
1148
1149 n = &node_masks[num_node_masks++];
1150
1151 n->mask = candidate->mask;
1152 n->val = candidate->match;
1153
Sam Ravnborg90181132009-01-06 13:19:28 -08001154 numadbg("NUMA NODE[%d]: mask[%lx] val[%lx] (latency[%llx])\n",
David S. Miller919ee672008-04-23 05:40:25 -07001155 index, n->mask, n->val, candidate->latency);
1156
1157 return 0;
1158}
1159
1160static int __init numa_parse_mdesc_group(struct mdesc_handle *md, u64 grp,
1161 int index)
1162{
1163 cpumask_t mask;
1164 int cpu;
1165
1166 numa_parse_mdesc_group_cpus(md, grp, &mask);
1167
KOSAKI Motohirofb1fece2011-05-16 13:38:07 -07001168 for_each_cpu(cpu, &mask)
David S. Miller919ee672008-04-23 05:40:25 -07001169 numa_cpu_lookup_table[cpu] = index;
KOSAKI Motohirofb1fece2011-05-16 13:38:07 -07001170 cpumask_copy(&numa_cpumask_lookup_table[index], &mask);
David S. Miller919ee672008-04-23 05:40:25 -07001171
1172 if (numa_debug) {
1173 printk(KERN_INFO "NUMA GROUP[%d]: cpus [ ", index);
KOSAKI Motohirofb1fece2011-05-16 13:38:07 -07001174 for_each_cpu(cpu, &mask)
David S. Miller919ee672008-04-23 05:40:25 -07001175 printk("%d ", cpu);
1176 printk("]\n");
1177 }
1178
1179 return numa_attach_mlgroup(md, grp, index);
1180}
1181
1182static int __init numa_parse_mdesc(void)
1183{
1184 struct mdesc_handle *md = mdesc_grab();
1185 int i, err, count;
1186 u64 node;
1187
1188 node = mdesc_node_by_name(md, MDESC_NODE_NULL, "latency-groups");
1189 if (node == MDESC_NODE_NULL) {
1190 mdesc_release(md);
1191 return -ENOENT;
1192 }
1193
1194 err = grab_mblocks(md);
1195 if (err < 0)
1196 goto out;
1197
1198 err = grab_mlgroups(md);
1199 if (err < 0)
1200 goto out;
1201
1202 count = 0;
1203 mdesc_for_each_node_by_name(md, node, "group") {
1204 err = numa_parse_mdesc_group(md, node, count);
1205 if (err < 0)
1206 break;
1207 count++;
1208 }
1209
1210 add_node_ranges();
1211
1212 for (i = 0; i < num_node_masks; i++) {
1213 allocate_node_data(i);
1214 node_set_online(i);
1215 }
1216
1217 err = 0;
1218out:
1219 mdesc_release(md);
1220 return err;
1221}
1222
David S. Miller072bd412008-08-18 20:36:17 -07001223static int __init numa_parse_jbus(void)
1224{
1225 unsigned long cpu, index;
1226
1227 /* NUMA node id is encoded in bits 36 and higher, and there is
1228 * a 1-to-1 mapping from CPU ID to NUMA node ID.
1229 */
1230 index = 0;
1231 for_each_present_cpu(cpu) {
1232 numa_cpu_lookup_table[cpu] = index;
KOSAKI Motohirofb1fece2011-05-16 13:38:07 -07001233 cpumask_copy(&numa_cpumask_lookup_table[index], cpumask_of(cpu));
David S. Miller072bd412008-08-18 20:36:17 -07001234 node_masks[index].mask = ~((1UL << 36UL) - 1UL);
1235 node_masks[index].val = cpu << 36UL;
1236
1237 index++;
1238 }
1239 num_node_masks = index;
1240
1241 add_node_ranges();
1242
1243 for (index = 0; index < num_node_masks; index++) {
1244 allocate_node_data(index);
1245 node_set_online(index);
1246 }
1247
1248 return 0;
1249}
1250
David S. Miller919ee672008-04-23 05:40:25 -07001251static int __init numa_parse_sun4u(void)
1252{
David S. Miller072bd412008-08-18 20:36:17 -07001253 if (tlb_type == cheetah || tlb_type == cheetah_plus) {
1254 unsigned long ver;
1255
1256 __asm__ ("rdpr %%ver, %0" : "=r" (ver));
1257 if ((ver >> 32UL) == __JALAPENO_ID ||
1258 (ver >> 32UL) == __SERRANO_ID)
1259 return numa_parse_jbus();
1260 }
David S. Miller919ee672008-04-23 05:40:25 -07001261 return -1;
1262}
1263
1264static int __init bootmem_init_numa(void)
1265{
1266 int err = -1;
1267
1268 numadbg("bootmem_init_numa()\n");
1269
1270 if (numa_enabled) {
1271 if (tlb_type == hypervisor)
1272 err = numa_parse_mdesc();
1273 else
1274 err = numa_parse_sun4u();
1275 }
1276 return err;
1277}
1278
1279#else
1280
1281static int bootmem_init_numa(void)
1282{
1283 return -1;
1284}
1285
1286#endif
1287
1288static void __init bootmem_init_nonnuma(void)
1289{
Yinghai Lu95f72d12010-07-12 14:36:09 +10001290 unsigned long top_of_ram = memblock_end_of_DRAM();
1291 unsigned long total_ram = memblock_phys_mem_size();
David S. Miller919ee672008-04-23 05:40:25 -07001292
1293 numadbg("bootmem_init_nonnuma()\n");
1294
1295 printk(KERN_INFO "Top of RAM: 0x%lx, Total RAM: 0x%lx\n",
1296 top_of_ram, total_ram);
1297 printk(KERN_INFO "Memory hole size: %ldMB\n",
1298 (top_of_ram - total_ram) >> 20);
1299
1300 init_node_masks_nonnuma();
Tejun Heo2a4814d2011-12-08 10:22:08 -08001301 memblock_set_node(0, (phys_addr_t)ULLONG_MAX, 0);
David S. Miller919ee672008-04-23 05:40:25 -07001302 allocate_node_data(0);
David S. Miller919ee672008-04-23 05:40:25 -07001303 node_set_online(0);
1304}
1305
1306static void __init reserve_range_in_node(int nid, unsigned long start,
1307 unsigned long end)
1308{
1309 numadbg(" reserve_range_in_node(nid[%d],start[%lx],end[%lx]\n",
1310 nid, start, end);
1311 while (start < end) {
1312 unsigned long this_end;
1313 int n;
1314
Benjamin Herrenschmidt35a1f0b2010-07-06 15:38:58 -07001315 this_end = memblock_nid_range(start, end, &n);
David S. Miller919ee672008-04-23 05:40:25 -07001316 if (n == nid) {
1317 numadbg(" MATCH reserving range [%lx:%lx]\n",
1318 start, this_end);
1319 reserve_bootmem_node(NODE_DATA(nid), start,
1320 (this_end - start), BOOTMEM_DEFAULT);
1321 } else
1322 numadbg(" NO MATCH, advancing start to %lx\n",
1323 this_end);
1324
1325 start = this_end;
1326 }
1327}
1328
1329static void __init trim_reserved_in_node(int nid)
1330{
Benjamin Herrenschmidt08b84792010-08-04 13:43:31 +10001331 struct memblock_region *reg;
David S. Miller919ee672008-04-23 05:40:25 -07001332
1333 numadbg(" trim_reserved_in_node(%d)\n", nid);
1334
Benjamin Herrenschmidt08b84792010-08-04 13:43:31 +10001335 for_each_memblock(reserved, reg)
1336 reserve_range_in_node(nid, reg->base, reg->base + reg->size);
David S. Miller919ee672008-04-23 05:40:25 -07001337}
1338
1339static void __init bootmem_init_one_node(int nid)
1340{
1341 struct pglist_data *p;
1342
1343 numadbg("bootmem_init_one_node(%d)\n", nid);
1344
1345 p = NODE_DATA(nid);
1346
1347 if (p->node_spanned_pages) {
1348 unsigned long paddr = node_masks[nid].bootmem_paddr;
1349 unsigned long end_pfn;
1350
1351 end_pfn = p->node_start_pfn + p->node_spanned_pages;
1352
1353 numadbg(" init_bootmem_node(%d, %lx, %lx, %lx)\n",
1354 nid, paddr >> PAGE_SHIFT, p->node_start_pfn, end_pfn);
1355
1356 init_bootmem_node(p, paddr >> PAGE_SHIFT,
1357 p->node_start_pfn, end_pfn);
1358
1359 numadbg(" free_bootmem_with_active_regions(%d, %lx)\n",
1360 nid, end_pfn);
1361 free_bootmem_with_active_regions(nid, end_pfn);
1362
1363 trim_reserved_in_node(nid);
1364
1365 numadbg(" sparse_memory_present_with_active_regions(%d)\n",
1366 nid);
1367 sparse_memory_present_with_active_regions(nid);
1368 }
1369}
1370
1371static unsigned long __init bootmem_init(unsigned long phys_base)
1372{
1373 unsigned long end_pfn;
1374 int nid;
1375
Yinghai Lu95f72d12010-07-12 14:36:09 +10001376 end_pfn = memblock_end_of_DRAM() >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 max_pfn = max_low_pfn = end_pfn;
David S. Millerd1112012006-03-08 02:16:07 -08001378 min_low_pfn = (phys_base >> PAGE_SHIFT);
1379
David S. Miller919ee672008-04-23 05:40:25 -07001380 if (bootmem_init_numa() < 0)
1381 bootmem_init_nonnuma();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
David S. Miller919ee672008-04-23 05:40:25 -07001383 /* XXX cpu notifier XXX */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
David S. Miller919ee672008-04-23 05:40:25 -07001385 for_each_online_node(nid)
1386 bootmem_init_one_node(nid);
David S. Millerd1112012006-03-08 02:16:07 -08001387
1388 sparse_init();
1389
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 return end_pfn;
1391}
1392
David S. Miller9cc3a1a2006-02-21 20:51:13 -08001393static struct linux_prom64_registers pall[MAX_BANKS] __initdata;
1394static int pall_ents __initdata;
1395
David S. Miller56425302005-09-25 16:46:57 -07001396#ifdef CONFIG_DEBUG_PAGEALLOC
Sam Ravnborg896aef42008-02-24 19:49:52 -08001397static unsigned long __ref kernel_map_range(unsigned long pstart,
1398 unsigned long pend, pgprot_t prot)
David S. Miller56425302005-09-25 16:46:57 -07001399{
1400 unsigned long vstart = PAGE_OFFSET + pstart;
1401 unsigned long vend = PAGE_OFFSET + pend;
1402 unsigned long alloc_bytes = 0UL;
1403
1404 if ((vstart & ~PAGE_MASK) || (vend & ~PAGE_MASK)) {
David S. Miller13edad72005-09-29 17:58:26 -07001405 prom_printf("kernel_map: Unaligned physmem[%lx:%lx]\n",
David S. Miller56425302005-09-25 16:46:57 -07001406 vstart, vend);
1407 prom_halt();
1408 }
1409
1410 while (vstart < vend) {
1411 unsigned long this_end, paddr = __pa(vstart);
1412 pgd_t *pgd = pgd_offset_k(vstart);
1413 pud_t *pud;
1414 pmd_t *pmd;
1415 pte_t *pte;
1416
1417 pud = pud_offset(pgd, vstart);
1418 if (pud_none(*pud)) {
1419 pmd_t *new;
1420
1421 new = __alloc_bootmem(PAGE_SIZE, PAGE_SIZE, PAGE_SIZE);
1422 alloc_bytes += PAGE_SIZE;
1423 pud_populate(&init_mm, pud, new);
1424 }
1425
1426 pmd = pmd_offset(pud, vstart);
1427 if (!pmd_present(*pmd)) {
1428 pte_t *new;
1429
1430 new = __alloc_bootmem(PAGE_SIZE, PAGE_SIZE, PAGE_SIZE);
1431 alloc_bytes += PAGE_SIZE;
1432 pmd_populate_kernel(&init_mm, pmd, new);
1433 }
1434
1435 pte = pte_offset_kernel(pmd, vstart);
1436 this_end = (vstart + PMD_SIZE) & PMD_MASK;
1437 if (this_end > vend)
1438 this_end = vend;
1439
1440 while (vstart < this_end) {
1441 pte_val(*pte) = (paddr | pgprot_val(prot));
1442
1443 vstart += PAGE_SIZE;
1444 paddr += PAGE_SIZE;
1445 pte++;
1446 }
1447 }
1448
1449 return alloc_bytes;
1450}
1451
David S. Miller56425302005-09-25 16:46:57 -07001452extern unsigned int kvmap_linear_patch[1];
David S. Miller9cc3a1a2006-02-21 20:51:13 -08001453#endif /* CONFIG_DEBUG_PAGEALLOC */
1454
1455static void __init mark_kpte_bitmap(unsigned long start, unsigned long end)
1456{
1457 const unsigned long shift_256MB = 28;
1458 const unsigned long mask_256MB = ((1UL << shift_256MB) - 1UL);
1459 const unsigned long size_256MB = (1UL << shift_256MB);
1460
1461 while (start < end) {
1462 long remains;
1463
David S. Millerf7c00332006-03-05 22:18:50 -08001464 remains = end - start;
1465 if (remains < size_256MB)
1466 break;
1467
David S. Miller9cc3a1a2006-02-21 20:51:13 -08001468 if (start & mask_256MB) {
1469 start = (start + size_256MB) & ~mask_256MB;
1470 continue;
1471 }
1472
David S. Miller9cc3a1a2006-02-21 20:51:13 -08001473 while (remains >= size_256MB) {
1474 unsigned long index = start >> shift_256MB;
1475
1476 __set_bit(index, kpte_linear_bitmap);
1477
1478 start += size_256MB;
1479 remains -= size_256MB;
1480 }
1481 }
1482}
David S. Miller56425302005-09-25 16:46:57 -07001483
David S. Miller8f3614532007-12-13 06:13:38 -08001484static void __init init_kpte_bitmap(void)
David S. Miller56425302005-09-25 16:46:57 -07001485{
David S. Miller9cc3a1a2006-02-21 20:51:13 -08001486 unsigned long i;
David S. Miller13edad72005-09-29 17:58:26 -07001487
1488 for (i = 0; i < pall_ents; i++) {
David S. Miller56425302005-09-25 16:46:57 -07001489 unsigned long phys_start, phys_end;
1490
David S. Miller13edad72005-09-29 17:58:26 -07001491 phys_start = pall[i].phys_addr;
1492 phys_end = phys_start + pall[i].reg_size;
David S. Miller9cc3a1a2006-02-21 20:51:13 -08001493
1494 mark_kpte_bitmap(phys_start, phys_end);
David S. Miller8f3614532007-12-13 06:13:38 -08001495 }
1496}
David S. Miller9cc3a1a2006-02-21 20:51:13 -08001497
David S. Miller8f3614532007-12-13 06:13:38 -08001498static void __init kernel_physical_mapping_init(void)
1499{
David S. Miller9cc3a1a2006-02-21 20:51:13 -08001500#ifdef CONFIG_DEBUG_PAGEALLOC
David S. Miller8f3614532007-12-13 06:13:38 -08001501 unsigned long i, mem_alloced = 0UL;
1502
1503 for (i = 0; i < pall_ents; i++) {
1504 unsigned long phys_start, phys_end;
1505
1506 phys_start = pall[i].phys_addr;
1507 phys_end = phys_start + pall[i].reg_size;
1508
David S. Miller56425302005-09-25 16:46:57 -07001509 mem_alloced += kernel_map_range(phys_start, phys_end,
1510 PAGE_KERNEL);
David S. Miller56425302005-09-25 16:46:57 -07001511 }
1512
1513 printk("Allocated %ld bytes for kernel page tables.\n",
1514 mem_alloced);
1515
1516 kvmap_linear_patch[0] = 0x01000000; /* nop */
1517 flushi(&kvmap_linear_patch[0]);
1518
1519 __flush_tlb_all();
David S. Miller9cc3a1a2006-02-21 20:51:13 -08001520#endif
David S. Miller56425302005-09-25 16:46:57 -07001521}
1522
David S. Miller9cc3a1a2006-02-21 20:51:13 -08001523#ifdef CONFIG_DEBUG_PAGEALLOC
David S. Miller56425302005-09-25 16:46:57 -07001524void kernel_map_pages(struct page *page, int numpages, int enable)
1525{
1526 unsigned long phys_start = page_to_pfn(page) << PAGE_SHIFT;
1527 unsigned long phys_end = phys_start + (numpages * PAGE_SIZE);
1528
1529 kernel_map_range(phys_start, phys_end,
1530 (enable ? PAGE_KERNEL : __pgprot(0)));
1531
David S. Miller74bf4312006-01-31 18:29:18 -08001532 flush_tsb_kernel_range(PAGE_OFFSET + phys_start,
1533 PAGE_OFFSET + phys_end);
1534
David S. Miller56425302005-09-25 16:46:57 -07001535 /* we should perform an IPI and flush all tlbs,
1536 * but that can deadlock->flush only current cpu.
1537 */
1538 __flush_tlb_kernel_range(PAGE_OFFSET + phys_start,
1539 PAGE_OFFSET + phys_end);
1540}
1541#endif
1542
David S. Miller10147572005-09-28 21:46:43 -07001543unsigned long __init find_ecache_flush_span(unsigned long size)
1544{
David S. Miller13edad72005-09-29 17:58:26 -07001545 int i;
David S. Miller10147572005-09-28 21:46:43 -07001546
David S. Miller13edad72005-09-29 17:58:26 -07001547 for (i = 0; i < pavail_ents; i++) {
1548 if (pavail[i].reg_size >= size)
1549 return pavail[i].phys_addr;
David S. Miller10147572005-09-28 21:46:43 -07001550 }
1551
1552 return ~0UL;
1553}
1554
David S. Miller517af332006-02-01 15:55:21 -08001555static void __init tsb_phys_patch(void)
1556{
David S. Millerd257d5d2006-02-06 23:44:37 -08001557 struct tsb_ldquad_phys_patch_entry *pquad;
David S. Miller517af332006-02-01 15:55:21 -08001558 struct tsb_phys_patch_entry *p;
1559
David S. Millerd257d5d2006-02-06 23:44:37 -08001560 pquad = &__tsb_ldquad_phys_patch;
1561 while (pquad < &__tsb_ldquad_phys_patch_end) {
1562 unsigned long addr = pquad->addr;
1563
1564 if (tlb_type == hypervisor)
1565 *(unsigned int *) addr = pquad->sun4v_insn;
1566 else
1567 *(unsigned int *) addr = pquad->sun4u_insn;
1568 wmb();
1569 __asm__ __volatile__("flush %0"
1570 : /* no outputs */
1571 : "r" (addr));
1572
1573 pquad++;
1574 }
1575
David S. Miller517af332006-02-01 15:55:21 -08001576 p = &__tsb_phys_patch;
1577 while (p < &__tsb_phys_patch_end) {
1578 unsigned long addr = p->addr;
1579
1580 *(unsigned int *) addr = p->insn;
1581 wmb();
1582 __asm__ __volatile__("flush %0"
1583 : /* no outputs */
1584 : "r" (addr));
1585
1586 p++;
1587 }
1588}
1589
David S. Miller490384e2006-02-11 14:41:18 -08001590/* Don't mark as init, we give this to the Hypervisor. */
David S. Millerd1acb422007-03-16 17:20:28 -07001591#ifndef CONFIG_DEBUG_PAGEALLOC
1592#define NUM_KTSB_DESCR 2
1593#else
1594#define NUM_KTSB_DESCR 1
1595#endif
1596static struct hv_tsb_descr ktsb_descr[NUM_KTSB_DESCR];
David S. Miller490384e2006-02-11 14:41:18 -08001597extern struct tsb swapper_tsb[KERNEL_TSB_NENTRIES];
1598
David S. Miller9076d0e2011-08-05 00:53:57 -07001599static void patch_one_ktsb_phys(unsigned int *start, unsigned int *end, unsigned long pa)
1600{
1601 pa >>= KTSB_PHYS_SHIFT;
1602
1603 while (start < end) {
1604 unsigned int *ia = (unsigned int *)(unsigned long)*start;
1605
1606 ia[0] = (ia[0] & ~0x3fffff) | (pa >> 10);
1607 __asm__ __volatile__("flush %0" : : "r" (ia));
1608
1609 ia[1] = (ia[1] & ~0x3ff) | (pa & 0x3ff);
1610 __asm__ __volatile__("flush %0" : : "r" (ia + 1));
1611
1612 start++;
1613 }
1614}
1615
1616static void ktsb_phys_patch(void)
1617{
1618 extern unsigned int __swapper_tsb_phys_patch;
1619 extern unsigned int __swapper_tsb_phys_patch_end;
David S. Miller9076d0e2011-08-05 00:53:57 -07001620 unsigned long ktsb_pa;
1621
1622 ktsb_pa = kern_base + ((unsigned long)&swapper_tsb[0] - KERNBASE);
1623 patch_one_ktsb_phys(&__swapper_tsb_phys_patch,
1624 &__swapper_tsb_phys_patch_end, ktsb_pa);
1625#ifndef CONFIG_DEBUG_PAGEALLOC
David S. Miller0785a8e2011-08-06 05:26:35 -07001626 {
1627 extern unsigned int __swapper_4m_tsb_phys_patch;
1628 extern unsigned int __swapper_4m_tsb_phys_patch_end;
David S. Miller9076d0e2011-08-05 00:53:57 -07001629 ktsb_pa = (kern_base +
1630 ((unsigned long)&swapper_4m_tsb[0] - KERNBASE));
1631 patch_one_ktsb_phys(&__swapper_4m_tsb_phys_patch,
1632 &__swapper_4m_tsb_phys_patch_end, ktsb_pa);
David S. Miller0785a8e2011-08-06 05:26:35 -07001633 }
David S. Miller9076d0e2011-08-05 00:53:57 -07001634#endif
1635}
1636
David S. Miller490384e2006-02-11 14:41:18 -08001637static void __init sun4v_ktsb_init(void)
1638{
1639 unsigned long ktsb_pa;
1640
David S. Millerd7744a02006-02-21 22:31:11 -08001641 /* First KTSB for PAGE_SIZE mappings. */
David S. Miller490384e2006-02-11 14:41:18 -08001642 ktsb_pa = kern_base + ((unsigned long)&swapper_tsb[0] - KERNBASE);
1643
1644 switch (PAGE_SIZE) {
1645 case 8 * 1024:
1646 default:
1647 ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_8K;
1648 ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_8K;
1649 break;
1650
1651 case 64 * 1024:
1652 ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_64K;
1653 ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_64K;
1654 break;
1655
1656 case 512 * 1024:
1657 ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_512K;
1658 ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_512K;
1659 break;
1660
1661 case 4 * 1024 * 1024:
1662 ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_4MB;
1663 ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_4MB;
1664 break;
Joe Perches6cb79b32011-06-03 14:45:23 +00001665 }
David S. Miller490384e2006-02-11 14:41:18 -08001666
David S. Miller3f19a842006-02-17 12:03:20 -08001667 ktsb_descr[0].assoc = 1;
David S. Miller490384e2006-02-11 14:41:18 -08001668 ktsb_descr[0].num_ttes = KERNEL_TSB_NENTRIES;
1669 ktsb_descr[0].ctx_idx = 0;
1670 ktsb_descr[0].tsb_base = ktsb_pa;
1671 ktsb_descr[0].resv = 0;
1672
David S. Millerd1acb422007-03-16 17:20:28 -07001673#ifndef CONFIG_DEBUG_PAGEALLOC
David S. Millerd7744a02006-02-21 22:31:11 -08001674 /* Second KTSB for 4MB/256MB mappings. */
1675 ktsb_pa = (kern_base +
1676 ((unsigned long)&swapper_4m_tsb[0] - KERNBASE));
1677
1678 ktsb_descr[1].pgsz_idx = HV_PGSZ_IDX_4MB;
1679 ktsb_descr[1].pgsz_mask = (HV_PGSZ_MASK_4MB |
1680 HV_PGSZ_MASK_256MB);
1681 ktsb_descr[1].assoc = 1;
1682 ktsb_descr[1].num_ttes = KERNEL_TSB4M_NENTRIES;
1683 ktsb_descr[1].ctx_idx = 0;
1684 ktsb_descr[1].tsb_base = ktsb_pa;
1685 ktsb_descr[1].resv = 0;
David S. Millerd1acb422007-03-16 17:20:28 -07001686#endif
David S. Miller490384e2006-02-11 14:41:18 -08001687}
1688
1689void __cpuinit sun4v_ktsb_register(void)
1690{
David S. Miller7db35f32007-05-29 02:22:14 -07001691 unsigned long pa, ret;
David S. Miller490384e2006-02-11 14:41:18 -08001692
1693 pa = kern_base + ((unsigned long)&ktsb_descr[0] - KERNBASE);
1694
David S. Miller7db35f32007-05-29 02:22:14 -07001695 ret = sun4v_mmu_tsb_ctx0(NUM_KTSB_DESCR, pa);
1696 if (ret != 0) {
1697 prom_printf("hypervisor_mmu_tsb_ctx0[%lx]: "
1698 "errors with %lx\n", pa, ret);
1699 prom_halt();
1700 }
David S. Miller490384e2006-02-11 14:41:18 -08001701}
1702
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703/* paging_init() sets up the page tables */
1704
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705static unsigned long last_valid_pfn;
David S. Miller56425302005-09-25 16:46:57 -07001706pgd_t swapper_pg_dir[2048];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
David S. Millerc4bce902006-02-11 21:57:54 -08001708static void sun4u_pgprot_init(void);
1709static void sun4v_pgprot_init(void);
1710
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711void __init paging_init(void)
1712{
David S. Miller919ee672008-04-23 05:40:25 -07001713 unsigned long end_pfn, shift, phys_base;
David S. Miller0836a0e2005-09-28 21:38:08 -07001714 unsigned long real_end, i;
1715
David S. Miller22adb352007-05-26 01:14:43 -07001716 /* These build time checkes make sure that the dcache_dirty_cpu()
1717 * page->flags usage will work.
1718 *
1719 * When a page gets marked as dcache-dirty, we store the
1720 * cpu number starting at bit 32 in the page->flags. Also,
1721 * functions like clear_dcache_dirty_cpu use the cpu mask
1722 * in 13-bit signed-immediate instruction fields.
1723 */
Christoph Lameter9223b412008-04-28 02:12:48 -07001724
1725 /*
1726 * Page flags must not reach into upper 32 bits that are used
1727 * for the cpu number
1728 */
1729 BUILD_BUG_ON(NR_PAGEFLAGS > 32);
1730
1731 /*
1732 * The bit fields placed in the high range must not reach below
1733 * the 32 bit boundary. Otherwise we cannot place the cpu field
1734 * at the 32 bit boundary.
1735 */
David S. Miller22adb352007-05-26 01:14:43 -07001736 BUILD_BUG_ON(SECTIONS_WIDTH + NODES_WIDTH + ZONES_WIDTH +
Christoph Lameter9223b412008-04-28 02:12:48 -07001737 ilog2(roundup_pow_of_two(NR_CPUS)) > 32);
1738
David S. Miller22adb352007-05-26 01:14:43 -07001739 BUILD_BUG_ON(NR_CPUS > 4096);
1740
David S. Miller481295f2006-02-07 21:51:08 -08001741 kern_base = (prom_boot_mapping_phys_low >> 22UL) << 22UL;
1742 kern_size = (unsigned long)&_end - (unsigned long)KERNBASE;
1743
David S. Millerd7744a02006-02-21 22:31:11 -08001744 /* Invalidate both kernel TSBs. */
David S. Miller8b234272006-02-17 18:01:02 -08001745 memset(swapper_tsb, 0x40, sizeof(swapper_tsb));
David S. Millerd1acb422007-03-16 17:20:28 -07001746#ifndef CONFIG_DEBUG_PAGEALLOC
David S. Millerd7744a02006-02-21 22:31:11 -08001747 memset(swapper_4m_tsb, 0x40, sizeof(swapper_4m_tsb));
David S. Millerd1acb422007-03-16 17:20:28 -07001748#endif
David S. Miller8b234272006-02-17 18:01:02 -08001749
David S. Millerc4bce902006-02-11 21:57:54 -08001750 if (tlb_type == hypervisor)
1751 sun4v_pgprot_init();
1752 else
1753 sun4u_pgprot_init();
1754
David S. Millerd257d5d2006-02-06 23:44:37 -08001755 if (tlb_type == cheetah_plus ||
David S. Miller9076d0e2011-08-05 00:53:57 -07001756 tlb_type == hypervisor) {
David S. Miller517af332006-02-01 15:55:21 -08001757 tsb_phys_patch();
David S. Miller9076d0e2011-08-05 00:53:57 -07001758 ktsb_phys_patch();
1759 }
David S. Miller517af332006-02-01 15:55:21 -08001760
David S. Miller490384e2006-02-11 14:41:18 -08001761 if (tlb_type == hypervisor) {
David S. Millerd257d5d2006-02-06 23:44:37 -08001762 sun4v_patch_tlb_handlers();
David S. Miller490384e2006-02-11 14:41:18 -08001763 sun4v_ktsb_init();
1764 }
David S. Millerd257d5d2006-02-06 23:44:37 -08001765
David S. Millera94a1722008-05-11 21:04:48 -07001766 /* Find available physical memory...
1767 *
1768 * Read it twice in order to work around a bug in openfirmware.
1769 * The call to grab this table itself can cause openfirmware to
1770 * allocate memory, which in turn can take away some space from
1771 * the list of available memory. Reading it twice makes sure
1772 * we really do get the final value.
1773 */
1774 read_obp_translations();
1775 read_obp_memory("reg", &pall[0], &pall_ents);
1776 read_obp_memory("available", &pavail[0], &pavail_ents);
David S. Miller13edad72005-09-29 17:58:26 -07001777 read_obp_memory("available", &pavail[0], &pavail_ents);
David S. Miller0836a0e2005-09-28 21:38:08 -07001778
1779 phys_base = 0xffffffffffffffffUL;
David S. Miller3b2a7e22008-02-13 18:13:20 -08001780 for (i = 0; i < pavail_ents; i++) {
David S. Miller13edad72005-09-29 17:58:26 -07001781 phys_base = min(phys_base, pavail[i].phys_addr);
Yinghai Lu95f72d12010-07-12 14:36:09 +10001782 memblock_add(pavail[i].phys_addr, pavail[i].reg_size);
David S. Miller3b2a7e22008-02-13 18:13:20 -08001783 }
1784
Yinghai Lu95f72d12010-07-12 14:36:09 +10001785 memblock_reserve(kern_base, kern_size);
David S. Miller0836a0e2005-09-28 21:38:08 -07001786
David S. Miller4e82c9a2008-02-13 18:00:03 -08001787 find_ramdisk(phys_base);
1788
Yinghai Lu95f72d12010-07-12 14:36:09 +10001789 memblock_enforce_memory_limit(cmdline_memory_size);
David S. Miller25b0c652008-02-13 18:20:14 -08001790
Tejun Heo1aadc052011-12-08 10:22:08 -08001791 memblock_allow_resize();
Yinghai Lu95f72d12010-07-12 14:36:09 +10001792 memblock_dump_all();
David S. Miller3b2a7e22008-02-13 18:13:20 -08001793
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 set_bit(0, mmu_context_bmap);
1795
David S. Miller2bdb3cb2005-09-22 01:08:57 -07001796 shift = kern_base + PAGE_OFFSET - ((unsigned long)KERNBASE);
1797
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 real_end = (unsigned long)_end;
David S. Miller64658742008-03-21 17:01:38 -07001799 num_kernel_image_mappings = DIV_ROUND_UP(real_end - KERNBASE, 1 << 22);
1800 printk("Kernel: Using %d locked TLB entries for main kernel image.\n",
1801 num_kernel_image_mappings);
David S. Miller2bdb3cb2005-09-22 01:08:57 -07001802
1803 /* Set kernel pgd to upper alias so physical page computations
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 * work.
1805 */
1806 init_mm.pgd += ((shift) / (sizeof(pgd_t)));
1807
David S. Miller56425302005-09-25 16:46:57 -07001808 memset(swapper_low_pmd_dir, 0, sizeof(swapper_low_pmd_dir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
1810 /* Now can init the kernel/bad page tables. */
1811 pud_set(pud_offset(&swapper_pg_dir[0], 0),
David S. Miller56425302005-09-25 16:46:57 -07001812 swapper_low_pmd_dir + (shift / sizeof(pgd_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
David S. Millerc9c10832005-10-12 12:22:46 -07001814 inherit_prom_mappings();
David S. Miller5085b4a2005-09-22 00:45:41 -07001815
David S. Miller8f3614532007-12-13 06:13:38 -08001816 init_kpte_bitmap();
1817
David S. Millera8b900d2006-01-31 18:33:37 -08001818 /* Ok, we can use our TLB miss and window trap handlers safely. */
1819 setup_tba();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820
David S. Millerc9c10832005-10-12 12:22:46 -07001821 __flush_tlb_all();
David S. Miller9ad98c52005-10-05 15:12:00 -07001822
David S. Miller490384e2006-02-11 14:41:18 -08001823 if (tlb_type == hypervisor)
1824 sun4v_ktsb_register();
1825
David S. Millerad072002008-02-13 19:21:51 -08001826 prom_build_devicetree();
David S. Millerb696fdc2009-05-26 22:37:25 -07001827 of_populate_present_mask();
David S. Millerb99c6eb2009-06-18 01:44:19 -07001828#ifndef CONFIG_SMP
1829 of_fill_in_cpu_data();
1830#endif
David S. Millerad072002008-02-13 19:21:51 -08001831
David S. Miller890db402009-04-01 03:13:15 -07001832 if (tlb_type == hypervisor) {
David S. Miller4a283332008-02-13 19:22:23 -08001833 sun4v_mdesc_init();
Stephen Rothwell6ac5c612009-06-15 03:06:18 -07001834 mdesc_populate_present_mask(cpu_all_mask);
David S. Millerb99c6eb2009-06-18 01:44:19 -07001835#ifndef CONFIG_SMP
1836 mdesc_fill_in_cpu_data(cpu_all_mask);
1837#endif
David S. Miller890db402009-04-01 03:13:15 -07001838 }
David S. Miller4a283332008-02-13 19:22:23 -08001839
David S. Miller4f70f7a2008-08-12 18:33:56 -07001840 /* Once the OF device tree and MDESC have been setup, we know
1841 * the list of possible cpus. Therefore we can allocate the
1842 * IRQ stacks.
1843 */
1844 for_each_possible_cpu(i) {
1845 /* XXX Use node local allocations... XXX */
Yinghai Lu95f72d12010-07-12 14:36:09 +10001846 softirq_stack[i] = __va(memblock_alloc(THREAD_SIZE, THREAD_SIZE));
1847 hardirq_stack[i] = __va(memblock_alloc(THREAD_SIZE, THREAD_SIZE));
David S. Miller4f70f7a2008-08-12 18:33:56 -07001848 }
1849
David S. Miller2bdb3cb2005-09-22 01:08:57 -07001850 /* Setup bootmem... */
David S. Miller919ee672008-04-23 05:40:25 -07001851 last_valid_pfn = end_pfn = bootmem_init(phys_base);
David S. Millerd1112012006-03-08 02:16:07 -08001852
David S. Miller919ee672008-04-23 05:40:25 -07001853#ifndef CONFIG_NEED_MULTIPLE_NODES
David S. Miller17b0e192006-03-08 15:57:03 -08001854 max_mapnr = last_valid_pfn;
David S. Miller919ee672008-04-23 05:40:25 -07001855#endif
David S. Miller56425302005-09-25 16:46:57 -07001856 kernel_physical_mapping_init();
David S. Miller56425302005-09-25 16:46:57 -07001857
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 {
David S. Miller919ee672008-04-23 05:40:25 -07001859 unsigned long max_zone_pfns[MAX_NR_ZONES];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
David S. Miller919ee672008-04-23 05:40:25 -07001861 memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
David S. Miller919ee672008-04-23 05:40:25 -07001863 max_zone_pfns[ZONE_NORMAL] = end_pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864
David S. Miller919ee672008-04-23 05:40:25 -07001865 free_area_init_nodes(max_zone_pfns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 }
1867
David S. Miller3c62a2d2008-02-17 23:22:50 -08001868 printk("Booting Linux...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869}
1870
David S. Miller9a2ed5c2009-04-07 01:03:58 -07001871int __devinit page_in_phys_avail(unsigned long paddr)
David S. Miller919ee672008-04-23 05:40:25 -07001872{
1873 int i;
1874
1875 paddr &= PAGE_MASK;
1876
1877 for (i = 0; i < pavail_ents; i++) {
1878 unsigned long start, end;
1879
1880 start = pavail[i].phys_addr;
1881 end = start + pavail[i].reg_size;
1882
1883 if (paddr >= start && paddr < end)
1884 return 1;
1885 }
1886 if (paddr >= kern_base && paddr < (kern_base + kern_size))
1887 return 1;
1888#ifdef CONFIG_BLK_DEV_INITRD
1889 if (paddr >= __pa(initrd_start) &&
1890 paddr < __pa(PAGE_ALIGN(initrd_end)))
1891 return 1;
1892#endif
1893
1894 return 0;
1895}
1896
1897static struct linux_prom64_registers pavail_rescan[MAX_BANKS] __initdata;
1898static int pavail_rescan_ents __initdata;
1899
1900/* Certain OBP calls, such as fetching "available" properties, can
1901 * claim physical memory. So, along with initializing the valid
1902 * address bitmap, what we do here is refetch the physical available
1903 * memory list again, and make sure it provides at least as much
1904 * memory as 'pavail' does.
1905 */
David S. Millerd8ed1d42009-08-25 16:47:46 -07001906static void __init setup_valid_addr_bitmap_from_pavail(unsigned long *bitmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 int i;
1909
David S. Miller13edad72005-09-29 17:58:26 -07001910 read_obp_memory("available", &pavail_rescan[0], &pavail_rescan_ents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911
David S. Miller13edad72005-09-29 17:58:26 -07001912 for (i = 0; i < pavail_ents; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 unsigned long old_start, old_end;
1914
David S. Miller13edad72005-09-29 17:58:26 -07001915 old_start = pavail[i].phys_addr;
David S. Miller919ee672008-04-23 05:40:25 -07001916 old_end = old_start + pavail[i].reg_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 while (old_start < old_end) {
1918 int n;
1919
David S. Millerc2a5a462006-06-22 00:01:56 -07001920 for (n = 0; n < pavail_rescan_ents; n++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 unsigned long new_start, new_end;
1922
David S. Miller13edad72005-09-29 17:58:26 -07001923 new_start = pavail_rescan[n].phys_addr;
1924 new_end = new_start +
1925 pavail_rescan[n].reg_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
1927 if (new_start <= old_start &&
1928 new_end >= (old_start + PAGE_SIZE)) {
David S. Millerd8ed1d42009-08-25 16:47:46 -07001929 set_bit(old_start >> 22, bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 goto do_next_page;
1931 }
1932 }
David S. Miller919ee672008-04-23 05:40:25 -07001933
1934 prom_printf("mem_init: Lost memory in pavail\n");
1935 prom_printf("mem_init: OLD start[%lx] size[%lx]\n",
1936 pavail[i].phys_addr,
1937 pavail[i].reg_size);
1938 prom_printf("mem_init: NEW start[%lx] size[%lx]\n",
1939 pavail_rescan[i].phys_addr,
1940 pavail_rescan[i].reg_size);
1941 prom_printf("mem_init: Cannot continue, aborting.\n");
1942 prom_halt();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943
1944 do_next_page:
1945 old_start += PAGE_SIZE;
1946 }
1947 }
1948}
1949
David S. Millerd8ed1d42009-08-25 16:47:46 -07001950static void __init patch_tlb_miss_handler_bitmap(void)
1951{
1952 extern unsigned int valid_addr_bitmap_insn[];
1953 extern unsigned int valid_addr_bitmap_patch[];
1954
1955 valid_addr_bitmap_insn[1] = valid_addr_bitmap_patch[1];
1956 mb();
1957 valid_addr_bitmap_insn[0] = valid_addr_bitmap_patch[0];
1958 flushi(&valid_addr_bitmap_insn[0]);
1959}
1960
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961void __init mem_init(void)
1962{
1963 unsigned long codepages, datapages, initpages;
1964 unsigned long addr, last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
1966 addr = PAGE_OFFSET + kern_base;
1967 last = PAGE_ALIGN(kern_size) + addr;
1968 while (addr < last) {
1969 set_bit(__pa(addr) >> 22, sparc64_valid_addr_bitmap);
1970 addr += PAGE_SIZE;
1971 }
1972
David S. Millerd8ed1d42009-08-25 16:47:46 -07001973 setup_valid_addr_bitmap_from_pavail(sparc64_valid_addr_bitmap);
1974 patch_tlb_miss_handler_bitmap();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 high_memory = __va(last_valid_pfn << PAGE_SHIFT);
1977
David S. Miller919ee672008-04-23 05:40:25 -07001978#ifdef CONFIG_NEED_MULTIPLE_NODES
David S. Millerd8ed1d42009-08-25 16:47:46 -07001979 {
1980 int i;
1981 for_each_online_node(i) {
1982 if (NODE_DATA(i)->node_spanned_pages != 0) {
1983 totalram_pages +=
1984 free_all_bootmem_node(NODE_DATA(i));
1985 }
David S. Miller919ee672008-04-23 05:40:25 -07001986 }
1987 }
1988#else
1989 totalram_pages = free_all_bootmem();
1990#endif
1991
David S. Millerf1cfdb52007-03-15 22:52:18 -07001992 /* We subtract one to account for the mem_map_zero page
1993 * allocated below.
1994 */
David S. Miller919ee672008-04-23 05:40:25 -07001995 totalram_pages -= 1;
1996 num_physpages = totalram_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
1998 /*
1999 * Set up the zero page, mark it reserved, so that page count
2000 * is not manipulated when freeing the page from user ptes.
2001 */
2002 mem_map_zero = alloc_pages(GFP_KERNEL|__GFP_ZERO, 0);
2003 if (mem_map_zero == NULL) {
2004 prom_printf("paging_init: Cannot alloc zero page.\n");
2005 prom_halt();
2006 }
2007 SetPageReserved(mem_map_zero);
2008
2009 codepages = (((unsigned long) _etext) - ((unsigned long) _start));
2010 codepages = PAGE_ALIGN(codepages) >> PAGE_SHIFT;
2011 datapages = (((unsigned long) _edata) - ((unsigned long) _etext));
2012 datapages = PAGE_ALIGN(datapages) >> PAGE_SHIFT;
2013 initpages = (((unsigned long) __init_end) - ((unsigned long) __init_begin));
2014 initpages = PAGE_ALIGN(initpages) >> PAGE_SHIFT;
2015
Christoph Lameter96177292007-02-10 01:43:03 -08002016 printk("Memory: %luk available (%ldk kernel code, %ldk data, %ldk init) [%016lx,%016lx]\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 nr_free_pages() << (PAGE_SHIFT-10),
2018 codepages << (PAGE_SHIFT-10),
2019 datapages << (PAGE_SHIFT-10),
2020 initpages << (PAGE_SHIFT-10),
2021 PAGE_OFFSET, (last_valid_pfn << PAGE_SHIFT));
2022
2023 if (tlb_type == cheetah || tlb_type == cheetah_plus)
2024 cheetah_ecache_flush_init();
2025}
2026
David S. Miller898cf0e2005-09-23 11:59:44 -07002027void free_initmem(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028{
2029 unsigned long addr, initend;
David S. Millerf2b60792008-08-14 01:45:41 -07002030 int do_free = 1;
2031
2032 /* If the physical memory maps were trimmed by kernel command
2033 * line options, don't even try freeing this initmem stuff up.
2034 * The kernel image could have been in the trimmed out region
2035 * and if so the freeing below will free invalid page structs.
2036 */
2037 if (cmdline_memory_size)
2038 do_free = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039
2040 /*
2041 * The init section is aligned to 8k in vmlinux.lds. Page align for >8k pagesizes.
2042 */
2043 addr = PAGE_ALIGN((unsigned long)(__init_begin));
2044 initend = (unsigned long)(__init_end) & PAGE_MASK;
2045 for (; addr < initend; addr += PAGE_SIZE) {
2046 unsigned long page;
2047 struct page *p;
2048
2049 page = (addr +
2050 ((unsigned long) __va(kern_base)) -
2051 ((unsigned long) KERNBASE));
Randy Dunlapc9cf5522006-06-27 02:53:52 -07002052 memset((void *)addr, POISON_FREE_INITMEM, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053
David S. Millerf2b60792008-08-14 01:45:41 -07002054 if (do_free) {
2055 p = virt_to_page(page);
2056
2057 ClearPageReserved(p);
2058 init_page_count(p);
2059 __free_page(p);
2060 num_physpages++;
2061 totalram_pages++;
2062 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 }
2064}
2065
2066#ifdef CONFIG_BLK_DEV_INITRD
2067void free_initrd_mem(unsigned long start, unsigned long end)
2068{
2069 if (start < end)
2070 printk ("Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
2071 for (; start < end; start += PAGE_SIZE) {
2072 struct page *p = virt_to_page(start);
2073
2074 ClearPageReserved(p);
Nick Piggin7835e982006-03-22 00:08:40 -08002075 init_page_count(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 __free_page(p);
2077 num_physpages++;
2078 totalram_pages++;
2079 }
2080}
2081#endif
David S. Millerc4bce902006-02-11 21:57:54 -08002082
David S. Millerc4bce902006-02-11 21:57:54 -08002083#define _PAGE_CACHE_4U (_PAGE_CP_4U | _PAGE_CV_4U)
2084#define _PAGE_CACHE_4V (_PAGE_CP_4V | _PAGE_CV_4V)
2085#define __DIRTY_BITS_4U (_PAGE_MODIFIED_4U | _PAGE_WRITE_4U | _PAGE_W_4U)
2086#define __DIRTY_BITS_4V (_PAGE_MODIFIED_4V | _PAGE_WRITE_4V | _PAGE_W_4V)
2087#define __ACCESS_BITS_4U (_PAGE_ACCESSED_4U | _PAGE_READ_4U | _PAGE_R)
2088#define __ACCESS_BITS_4V (_PAGE_ACCESSED_4V | _PAGE_READ_4V | _PAGE_R)
2089
2090pgprot_t PAGE_KERNEL __read_mostly;
2091EXPORT_SYMBOL(PAGE_KERNEL);
2092
2093pgprot_t PAGE_KERNEL_LOCKED __read_mostly;
2094pgprot_t PAGE_COPY __read_mostly;
David S. Miller0f159522006-02-18 12:43:16 -08002095
2096pgprot_t PAGE_SHARED __read_mostly;
2097EXPORT_SYMBOL(PAGE_SHARED);
2098
David S. Millerc4bce902006-02-11 21:57:54 -08002099unsigned long pg_iobits __read_mostly;
2100
2101unsigned long _PAGE_IE __read_mostly;
David S. Miller987c74f2006-06-25 01:34:43 -07002102EXPORT_SYMBOL(_PAGE_IE);
David S. Millerb2bef442006-02-23 01:55:55 -08002103
David S. Millerc4bce902006-02-11 21:57:54 -08002104unsigned long _PAGE_E __read_mostly;
David S. Millerb2bef442006-02-23 01:55:55 -08002105EXPORT_SYMBOL(_PAGE_E);
2106
David S. Millerc4bce902006-02-11 21:57:54 -08002107unsigned long _PAGE_CACHE __read_mostly;
David S. Millerb2bef442006-02-23 01:55:55 -08002108EXPORT_SYMBOL(_PAGE_CACHE);
David S. Millerc4bce902006-02-11 21:57:54 -08002109
David Miller46644c22007-10-16 01:24:16 -07002110#ifdef CONFIG_SPARSEMEM_VMEMMAP
David Miller46644c22007-10-16 01:24:16 -07002111unsigned long vmemmap_table[VMEMMAP_SIZE];
2112
David S. Miller3ba59542012-08-15 00:37:29 -07002113static long __meminitdata addr_start, addr_end;
2114static int __meminitdata node_start;
2115
David Miller46644c22007-10-16 01:24:16 -07002116int __meminit vmemmap_populate(struct page *start, unsigned long nr, int node)
2117{
2118 unsigned long vstart = (unsigned long) start;
2119 unsigned long vend = (unsigned long) (start + nr);
2120 unsigned long phys_start = (vstart - VMEMMAP_BASE);
2121 unsigned long phys_end = (vend - VMEMMAP_BASE);
2122 unsigned long addr = phys_start & VMEMMAP_CHUNK_MASK;
2123 unsigned long end = VMEMMAP_ALIGN(phys_end);
2124 unsigned long pte_base;
2125
2126 pte_base = (_PAGE_VALID | _PAGE_SZ4MB_4U |
2127 _PAGE_CP_4U | _PAGE_CV_4U |
2128 _PAGE_P_4U | _PAGE_W_4U);
2129 if (tlb_type == hypervisor)
2130 pte_base = (_PAGE_VALID | _PAGE_SZ4MB_4V |
2131 _PAGE_CP_4V | _PAGE_CV_4V |
2132 _PAGE_P_4V | _PAGE_W_4V);
2133
2134 for (; addr < end; addr += VMEMMAP_CHUNK) {
2135 unsigned long *vmem_pp =
2136 vmemmap_table + (addr >> VMEMMAP_CHUNK_SHIFT);
2137 void *block;
2138
2139 if (!(*vmem_pp & _PAGE_VALID)) {
2140 block = vmemmap_alloc_block(1UL << 22, node);
2141 if (!block)
2142 return -ENOMEM;
2143
2144 *vmem_pp = pte_base | __pa(block);
2145
David S. Miller3ba59542012-08-15 00:37:29 -07002146 /* check to see if we have contiguous blocks */
2147 if (addr_end != addr || node_start != node) {
2148 if (addr_start)
2149 printk(KERN_DEBUG " [%lx-%lx] on node %d\n",
2150 addr_start, addr_end-1, node_start);
2151 addr_start = addr;
2152 node_start = node;
2153 }
2154 addr_end = addr + VMEMMAP_CHUNK;
David Miller46644c22007-10-16 01:24:16 -07002155 }
2156 }
2157 return 0;
2158}
David S. Miller3ba59542012-08-15 00:37:29 -07002159
2160void __meminit vmemmap_populate_print_last(void)
2161{
2162 if (addr_start) {
2163 printk(KERN_DEBUG " [%lx-%lx] on node %d\n",
2164 addr_start, addr_end-1, node_start);
2165 addr_start = 0;
2166 addr_end = 0;
2167 node_start = 0;
2168 }
2169}
David Miller46644c22007-10-16 01:24:16 -07002170#endif /* CONFIG_SPARSEMEM_VMEMMAP */
2171
David S. Millerc4bce902006-02-11 21:57:54 -08002172static void prot_init_common(unsigned long page_none,
2173 unsigned long page_shared,
2174 unsigned long page_copy,
2175 unsigned long page_readonly,
2176 unsigned long page_exec_bit)
2177{
2178 PAGE_COPY = __pgprot(page_copy);
David S. Miller0f159522006-02-18 12:43:16 -08002179 PAGE_SHARED = __pgprot(page_shared);
David S. Millerc4bce902006-02-11 21:57:54 -08002180
2181 protection_map[0x0] = __pgprot(page_none);
2182 protection_map[0x1] = __pgprot(page_readonly & ~page_exec_bit);
2183 protection_map[0x2] = __pgprot(page_copy & ~page_exec_bit);
2184 protection_map[0x3] = __pgprot(page_copy & ~page_exec_bit);
2185 protection_map[0x4] = __pgprot(page_readonly);
2186 protection_map[0x5] = __pgprot(page_readonly);
2187 protection_map[0x6] = __pgprot(page_copy);
2188 protection_map[0x7] = __pgprot(page_copy);
2189 protection_map[0x8] = __pgprot(page_none);
2190 protection_map[0x9] = __pgprot(page_readonly & ~page_exec_bit);
2191 protection_map[0xa] = __pgprot(page_shared & ~page_exec_bit);
2192 protection_map[0xb] = __pgprot(page_shared & ~page_exec_bit);
2193 protection_map[0xc] = __pgprot(page_readonly);
2194 protection_map[0xd] = __pgprot(page_readonly);
2195 protection_map[0xe] = __pgprot(page_shared);
2196 protection_map[0xf] = __pgprot(page_shared);
2197}
2198
2199static void __init sun4u_pgprot_init(void)
2200{
2201 unsigned long page_none, page_shared, page_copy, page_readonly;
2202 unsigned long page_exec_bit;
2203
2204 PAGE_KERNEL = __pgprot (_PAGE_PRESENT_4U | _PAGE_VALID |
2205 _PAGE_CACHE_4U | _PAGE_P_4U |
2206 __ACCESS_BITS_4U | __DIRTY_BITS_4U |
2207 _PAGE_EXEC_4U);
2208 PAGE_KERNEL_LOCKED = __pgprot (_PAGE_PRESENT_4U | _PAGE_VALID |
2209 _PAGE_CACHE_4U | _PAGE_P_4U |
2210 __ACCESS_BITS_4U | __DIRTY_BITS_4U |
2211 _PAGE_EXEC_4U | _PAGE_L_4U);
David S. Millerc4bce902006-02-11 21:57:54 -08002212
2213 _PAGE_IE = _PAGE_IE_4U;
2214 _PAGE_E = _PAGE_E_4U;
2215 _PAGE_CACHE = _PAGE_CACHE_4U;
2216
2217 pg_iobits = (_PAGE_VALID | _PAGE_PRESENT_4U | __DIRTY_BITS_4U |
2218 __ACCESS_BITS_4U | _PAGE_E_4U);
2219
David S. Millerd1acb422007-03-16 17:20:28 -07002220#ifdef CONFIG_DEBUG_PAGEALLOC
2221 kern_linear_pte_xor[0] = (_PAGE_VALID | _PAGE_SZBITS_4U) ^
David S. Milleraf1ee562008-09-12 00:19:21 -07002222 0xfffff80000000000UL;
David S. Millerd1acb422007-03-16 17:20:28 -07002223#else
David S. Miller9cc3a1a2006-02-21 20:51:13 -08002224 kern_linear_pte_xor[0] = (_PAGE_VALID | _PAGE_SZ4MB_4U) ^
David S. Milleraf1ee562008-09-12 00:19:21 -07002225 0xfffff80000000000UL;
David S. Millerd1acb422007-03-16 17:20:28 -07002226#endif
David S. Miller9cc3a1a2006-02-21 20:51:13 -08002227 kern_linear_pte_xor[0] |= (_PAGE_CP_4U | _PAGE_CV_4U |
2228 _PAGE_P_4U | _PAGE_W_4U);
2229
2230 /* XXX Should use 256MB on Panther. XXX */
2231 kern_linear_pte_xor[1] = kern_linear_pte_xor[0];
David S. Millerc4bce902006-02-11 21:57:54 -08002232
2233 _PAGE_SZBITS = _PAGE_SZBITS_4U;
2234 _PAGE_ALL_SZ_BITS = (_PAGE_SZ4MB_4U | _PAGE_SZ512K_4U |
2235 _PAGE_SZ64K_4U | _PAGE_SZ8K_4U |
2236 _PAGE_SZ32MB_4U | _PAGE_SZ256MB_4U);
2237
2238
2239 page_none = _PAGE_PRESENT_4U | _PAGE_ACCESSED_4U | _PAGE_CACHE_4U;
2240 page_shared = (_PAGE_VALID | _PAGE_PRESENT_4U | _PAGE_CACHE_4U |
2241 __ACCESS_BITS_4U | _PAGE_WRITE_4U | _PAGE_EXEC_4U);
2242 page_copy = (_PAGE_VALID | _PAGE_PRESENT_4U | _PAGE_CACHE_4U |
2243 __ACCESS_BITS_4U | _PAGE_EXEC_4U);
2244 page_readonly = (_PAGE_VALID | _PAGE_PRESENT_4U | _PAGE_CACHE_4U |
2245 __ACCESS_BITS_4U | _PAGE_EXEC_4U);
2246
2247 page_exec_bit = _PAGE_EXEC_4U;
2248
2249 prot_init_common(page_none, page_shared, page_copy, page_readonly,
2250 page_exec_bit);
2251}
2252
2253static void __init sun4v_pgprot_init(void)
2254{
2255 unsigned long page_none, page_shared, page_copy, page_readonly;
2256 unsigned long page_exec_bit;
2257
2258 PAGE_KERNEL = __pgprot (_PAGE_PRESENT_4V | _PAGE_VALID |
2259 _PAGE_CACHE_4V | _PAGE_P_4V |
2260 __ACCESS_BITS_4V | __DIRTY_BITS_4V |
2261 _PAGE_EXEC_4V);
2262 PAGE_KERNEL_LOCKED = PAGE_KERNEL;
David S. Millerc4bce902006-02-11 21:57:54 -08002263
2264 _PAGE_IE = _PAGE_IE_4V;
2265 _PAGE_E = _PAGE_E_4V;
2266 _PAGE_CACHE = _PAGE_CACHE_4V;
2267
David S. Millerd1acb422007-03-16 17:20:28 -07002268#ifdef CONFIG_DEBUG_PAGEALLOC
2269 kern_linear_pte_xor[0] = (_PAGE_VALID | _PAGE_SZBITS_4V) ^
David S. Milleraf1ee562008-09-12 00:19:21 -07002270 0xfffff80000000000UL;
David S. Millerd1acb422007-03-16 17:20:28 -07002271#else
David S. Miller9cc3a1a2006-02-21 20:51:13 -08002272 kern_linear_pte_xor[0] = (_PAGE_VALID | _PAGE_SZ4MB_4V) ^
David S. Milleraf1ee562008-09-12 00:19:21 -07002273 0xfffff80000000000UL;
David S. Millerd1acb422007-03-16 17:20:28 -07002274#endif
David S. Miller9cc3a1a2006-02-21 20:51:13 -08002275 kern_linear_pte_xor[0] |= (_PAGE_CP_4V | _PAGE_CV_4V |
2276 _PAGE_P_4V | _PAGE_W_4V);
2277
David S. Millerd1acb422007-03-16 17:20:28 -07002278#ifdef CONFIG_DEBUG_PAGEALLOC
2279 kern_linear_pte_xor[1] = (_PAGE_VALID | _PAGE_SZBITS_4V) ^
David S. Milleraf1ee562008-09-12 00:19:21 -07002280 0xfffff80000000000UL;
David S. Millerd1acb422007-03-16 17:20:28 -07002281#else
David S. Miller9cc3a1a2006-02-21 20:51:13 -08002282 kern_linear_pte_xor[1] = (_PAGE_VALID | _PAGE_SZ256MB_4V) ^
David S. Milleraf1ee562008-09-12 00:19:21 -07002283 0xfffff80000000000UL;
David S. Millerd1acb422007-03-16 17:20:28 -07002284#endif
David S. Miller9cc3a1a2006-02-21 20:51:13 -08002285 kern_linear_pte_xor[1] |= (_PAGE_CP_4V | _PAGE_CV_4V |
2286 _PAGE_P_4V | _PAGE_W_4V);
David S. Millerc4bce902006-02-11 21:57:54 -08002287
2288 pg_iobits = (_PAGE_VALID | _PAGE_PRESENT_4V | __DIRTY_BITS_4V |
2289 __ACCESS_BITS_4V | _PAGE_E_4V);
2290
2291 _PAGE_SZBITS = _PAGE_SZBITS_4V;
2292 _PAGE_ALL_SZ_BITS = (_PAGE_SZ16GB_4V | _PAGE_SZ2GB_4V |
2293 _PAGE_SZ256MB_4V | _PAGE_SZ32MB_4V |
2294 _PAGE_SZ4MB_4V | _PAGE_SZ512K_4V |
2295 _PAGE_SZ64K_4V | _PAGE_SZ8K_4V);
2296
2297 page_none = _PAGE_PRESENT_4V | _PAGE_ACCESSED_4V | _PAGE_CACHE_4V;
2298 page_shared = (_PAGE_VALID | _PAGE_PRESENT_4V | _PAGE_CACHE_4V |
2299 __ACCESS_BITS_4V | _PAGE_WRITE_4V | _PAGE_EXEC_4V);
2300 page_copy = (_PAGE_VALID | _PAGE_PRESENT_4V | _PAGE_CACHE_4V |
2301 __ACCESS_BITS_4V | _PAGE_EXEC_4V);
2302 page_readonly = (_PAGE_VALID | _PAGE_PRESENT_4V | _PAGE_CACHE_4V |
2303 __ACCESS_BITS_4V | _PAGE_EXEC_4V);
2304
2305 page_exec_bit = _PAGE_EXEC_4V;
2306
2307 prot_init_common(page_none, page_shared, page_copy, page_readonly,
2308 page_exec_bit);
2309}
2310
2311unsigned long pte_sz_bits(unsigned long sz)
2312{
2313 if (tlb_type == hypervisor) {
2314 switch (sz) {
2315 case 8 * 1024:
2316 default:
2317 return _PAGE_SZ8K_4V;
2318 case 64 * 1024:
2319 return _PAGE_SZ64K_4V;
2320 case 512 * 1024:
2321 return _PAGE_SZ512K_4V;
2322 case 4 * 1024 * 1024:
2323 return _PAGE_SZ4MB_4V;
Joe Perches6cb79b32011-06-03 14:45:23 +00002324 }
David S. Millerc4bce902006-02-11 21:57:54 -08002325 } else {
2326 switch (sz) {
2327 case 8 * 1024:
2328 default:
2329 return _PAGE_SZ8K_4U;
2330 case 64 * 1024:
2331 return _PAGE_SZ64K_4U;
2332 case 512 * 1024:
2333 return _PAGE_SZ512K_4U;
2334 case 4 * 1024 * 1024:
2335 return _PAGE_SZ4MB_4U;
Joe Perches6cb79b32011-06-03 14:45:23 +00002336 }
David S. Millerc4bce902006-02-11 21:57:54 -08002337 }
2338}
2339
2340pte_t mk_pte_io(unsigned long page, pgprot_t prot, int space, unsigned long page_size)
2341{
2342 pte_t pte;
David S. Millercf627152006-02-12 21:10:07 -08002343
2344 pte_val(pte) = page | pgprot_val(pgprot_noncached(prot));
David S. Millerc4bce902006-02-11 21:57:54 -08002345 pte_val(pte) |= (((unsigned long)space) << 32);
2346 pte_val(pte) |= pte_sz_bits(page_size);
David S. Millercf627152006-02-12 21:10:07 -08002347
David S. Millerc4bce902006-02-11 21:57:54 -08002348 return pte;
2349}
2350
David S. Millerc4bce902006-02-11 21:57:54 -08002351static unsigned long kern_large_tte(unsigned long paddr)
2352{
2353 unsigned long val;
2354
2355 val = (_PAGE_VALID | _PAGE_SZ4MB_4U |
2356 _PAGE_CP_4U | _PAGE_CV_4U | _PAGE_P_4U |
2357 _PAGE_EXEC_4U | _PAGE_L_4U | _PAGE_W_4U);
2358 if (tlb_type == hypervisor)
2359 val = (_PAGE_VALID | _PAGE_SZ4MB_4V |
2360 _PAGE_CP_4V | _PAGE_CV_4V | _PAGE_P_4V |
2361 _PAGE_EXEC_4V | _PAGE_W_4V);
2362
2363 return val | paddr;
2364}
2365
David S. Millerc4bce902006-02-11 21:57:54 -08002366/* If not locked, zap it. */
2367void __flush_tlb_all(void)
2368{
2369 unsigned long pstate;
2370 int i;
2371
2372 __asm__ __volatile__("flushw\n\t"
2373 "rdpr %%pstate, %0\n\t"
2374 "wrpr %0, %1, %%pstate"
2375 : "=r" (pstate)
2376 : "i" (PSTATE_IE));
David S. Miller8f3614532007-12-13 06:13:38 -08002377 if (tlb_type == hypervisor) {
2378 sun4v_mmu_demap_all();
2379 } else if (tlb_type == spitfire) {
David S. Millerc4bce902006-02-11 21:57:54 -08002380 for (i = 0; i < 64; i++) {
2381 /* Spitfire Errata #32 workaround */
2382 /* NOTE: Always runs on spitfire, so no
2383 * cheetah+ page size encodings.
2384 */
2385 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
2386 "flush %%g6"
2387 : /* No outputs */
2388 : "r" (0),
2389 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
2390
2391 if (!(spitfire_get_dtlb_data(i) & _PAGE_L_4U)) {
2392 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
2393 "membar #Sync"
2394 : /* no outputs */
2395 : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
2396 spitfire_put_dtlb_data(i, 0x0UL);
2397 }
2398
2399 /* Spitfire Errata #32 workaround */
2400 /* NOTE: Always runs on spitfire, so no
2401 * cheetah+ page size encodings.
2402 */
2403 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
2404 "flush %%g6"
2405 : /* No outputs */
2406 : "r" (0),
2407 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
2408
2409 if (!(spitfire_get_itlb_data(i) & _PAGE_L_4U)) {
2410 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
2411 "membar #Sync"
2412 : /* no outputs */
2413 : "r" (TLB_TAG_ACCESS), "i" (ASI_IMMU));
2414 spitfire_put_itlb_data(i, 0x0UL);
2415 }
2416 }
2417 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
2418 cheetah_flush_dtlb_all();
2419 cheetah_flush_itlb_all();
2420 }
2421 __asm__ __volatile__("wrpr %0, 0, %%pstate"
2422 : : "r" (pstate));
2423}