sparc64: Probe cpu page size support more portably.
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / arch / sparc / mm / init_64.c
CommitLineData
b00dc837 1/*
1da177e4
LT
2 * 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
c4bce90e 8#include <linux/module.h>
1da177e4
LT
9#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>
1da177e4
LT
16#include <linux/initrd.h>
17#include <linux/swap.h>
18#include <linux/pagemap.h>
c9cf5528 19#include <linux/poison.h>
1da177e4
LT
20#include <linux/fs.h>
21#include <linux/seq_file.h>
05e14cb3 22#include <linux/kprobes.h>
1ac4f5eb 23#include <linux/cache.h>
13edad7a 24#include <linux/sort.h>
5cbc3073 25#include <linux/percpu.h>
95f72d1e 26#include <linux/memblock.h>
919ee677 27#include <linux/mmzone.h>
5a0e3ad6 28#include <linux/gfp.h>
1da177e4
LT
29
30#include <asm/head.h>
1da177e4
LT
31#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>
517af332 45#include <asm/tsb.h>
481295f9 46#include <asm/hypervisor.h>
372b07bb 47#include <asm/prom.h>
5cbc3073 48#include <asm/mdesc.h>
3d5ae6b6 49#include <asm/cpudata.h>
4f70f7a9 50#include <asm/irq.h>
1da177e4 51
27137e52 52#include "init_64.h"
9cc3a1ac 53
4f93d21d 54unsigned long kern_linear_pte_xor[4] __read_mostly;
9cc3a1ac 55
4f93d21d
DM
56/* A bitmap, two bits for every 256MB of physical memory. These two
57 * bits determine what page size we use for kernel linear
58 * translations. They form an index into kern_linear_pte_xor[]. The
59 * value in the indexed slot is XOR'd with the TLB miss virtual
60 * address to form the resulting TTE. The mapping is:
61 *
62 * 0 ==> 4MB
63 * 1 ==> 256MB
64 * 2 ==> 2GB
65 * 3 ==> 16GB
66 *
67 * All sun4v chips support 256MB pages. Only SPARC-T4 and later
68 * support 2GB pages, and hopefully future cpus will support the 16GB
69 * pages as well. For slots 2 and 3, we encode a 256MB TTE xor there
70 * if these larger page sizes are not supported by the cpu.
71 *
72 * It would be nice to determine this from the machine description
73 * 'cpu' properties, but we need to have this table setup before the
74 * MDESC is initialized.
9cc3a1ac
DM
75 */
76unsigned long kpte_linear_bitmap[KPTE_BITMAP_BYTES / sizeof(unsigned long)];
77
d1acb421 78#ifndef CONFIG_DEBUG_PAGEALLOC
4f93d21d
DM
79/* A special kernel TSB for 4MB, 256MB, 2GB and 16GB linear mappings.
80 * Space is allocated for this right after the trap table in
81 * arch/sparc64/kernel/head.S
2d9e2763
DM
82 */
83extern struct tsb swapper_4m_tsb[KERNEL_TSB4M_NENTRIES];
d1acb421 84#endif
d7744a09 85
ce33fdc5
DM
86static unsigned long cpu_pgsz_mask;
87
13edad7a
DM
88#define MAX_BANKS 32
89
9a2ed5cc
DM
90static struct linux_prom64_registers pavail[MAX_BANKS] __devinitdata;
91static int pavail_ents __devinitdata;
13edad7a
DM
92
93static int cmp_p64(const void *a, const void *b)
94{
95 const struct linux_prom64_registers *x = a, *y = b;
96
97 if (x->phys_addr > y->phys_addr)
98 return 1;
99 if (x->phys_addr < y->phys_addr)
100 return -1;
101 return 0;
102}
103
104static void __init read_obp_memory(const char *property,
105 struct linux_prom64_registers *regs,
106 int *num_ents)
107{
8d125562 108 phandle node = prom_finddevice("/memory");
13edad7a
DM
109 int prop_size = prom_getproplen(node, property);
110 int ents, ret, i;
111
112 ents = prop_size / sizeof(struct linux_prom64_registers);
113 if (ents > MAX_BANKS) {
114 prom_printf("The machine has more %s property entries than "
115 "this kernel can support (%d).\n",
116 property, MAX_BANKS);
117 prom_halt();
118 }
119
120 ret = prom_getproperty(node, property, (char *) regs, prop_size);
121 if (ret == -1) {
122 prom_printf("Couldn't get %s property from /memory.\n");
123 prom_halt();
124 }
125
13edad7a
DM
126 /* Sanitize what we got from the firmware, by page aligning
127 * everything.
128 */
129 for (i = 0; i < ents; i++) {
130 unsigned long base, size;
131
132 base = regs[i].phys_addr;
133 size = regs[i].reg_size;
10147570 134
13edad7a
DM
135 size &= PAGE_MASK;
136 if (base & ~PAGE_MASK) {
137 unsigned long new_base = PAGE_ALIGN(base);
138
139 size -= new_base - base;
140 if ((long) size < 0L)
141 size = 0UL;
142 base = new_base;
143 }
0015d3d6
DM
144 if (size == 0UL) {
145 /* If it is empty, simply get rid of it.
146 * This simplifies the logic of the other
147 * functions that process these arrays.
148 */
149 memmove(&regs[i], &regs[i + 1],
150 (ents - i - 1) * sizeof(regs[0]));
486ad10a 151 i--;
0015d3d6
DM
152 ents--;
153 continue;
486ad10a 154 }
0015d3d6
DM
155 regs[i].phys_addr = base;
156 regs[i].reg_size = size;
486ad10a
DM
157 }
158
159 *num_ents = ents;
160
c9c10830 161 sort(regs, ents, sizeof(struct linux_prom64_registers),
13edad7a
DM
162 cmp_p64, NULL);
163}
1da177e4 164
d8ed1d43
DM
165unsigned long sparc64_valid_addr_bitmap[VALID_ADDR_BITMAP_BYTES /
166 sizeof(unsigned long)];
917c3660 167EXPORT_SYMBOL(sparc64_valid_addr_bitmap);
1da177e4 168
d1112018 169/* Kernel physical address base and size in bytes. */
1ac4f5eb
DM
170unsigned long kern_base __read_mostly;
171unsigned long kern_size __read_mostly;
1da177e4 172
1da177e4
LT
173/* Initial ramdisk setup */
174extern unsigned long sparc_ramdisk_image64;
175extern unsigned int sparc_ramdisk_image;
176extern unsigned int sparc_ramdisk_size;
177
1ac4f5eb 178struct page *mem_map_zero __read_mostly;
35802c0b 179EXPORT_SYMBOL(mem_map_zero);
1da177e4 180
0835ae0f
DM
181unsigned int sparc64_highest_unlocked_tlb_ent __read_mostly;
182
183unsigned long sparc64_kern_pri_context __read_mostly;
184unsigned long sparc64_kern_pri_nuc_bits __read_mostly;
185unsigned long sparc64_kern_sec_context __read_mostly;
186
64658743 187int num_kernel_image_mappings;
1da177e4 188
1da177e4
LT
189#ifdef CONFIG_DEBUG_DCFLUSH
190atomic_t dcpage_flushes = ATOMIC_INIT(0);
191#ifdef CONFIG_SMP
192atomic_t dcpage_flushes_xcall = ATOMIC_INIT(0);
193#endif
194#endif
195
7a591cfe 196inline void flush_dcache_page_impl(struct page *page)
1da177e4 197{
7a591cfe 198 BUG_ON(tlb_type == hypervisor);
1da177e4
LT
199#ifdef CONFIG_DEBUG_DCFLUSH
200 atomic_inc(&dcpage_flushes);
201#endif
202
203#ifdef DCACHE_ALIASING_POSSIBLE
204 __flush_dcache_page(page_address(page),
205 ((tlb_type == spitfire) &&
206 page_mapping(page) != NULL));
207#else
208 if (page_mapping(page) != NULL &&
209 tlb_type == spitfire)
210 __flush_icache_page(__pa(page_address(page)));
211#endif
212}
213
214#define PG_dcache_dirty PG_arch_1
22adb358
DM
215#define PG_dcache_cpu_shift 32UL
216#define PG_dcache_cpu_mask \
217 ((1UL<<ilog2(roundup_pow_of_two(NR_CPUS)))-1UL)
1da177e4
LT
218
219#define dcache_dirty_cpu(page) \
48b0e548 220 (((page)->flags >> PG_dcache_cpu_shift) & PG_dcache_cpu_mask)
1da177e4 221
d979f179 222static inline void set_dcache_dirty(struct page *page, int this_cpu)
1da177e4
LT
223{
224 unsigned long mask = this_cpu;
48b0e548
DM
225 unsigned long non_cpu_bits;
226
227 non_cpu_bits = ~(PG_dcache_cpu_mask << PG_dcache_cpu_shift);
228 mask = (mask << PG_dcache_cpu_shift) | (1UL << PG_dcache_dirty);
229
1da177e4
LT
230 __asm__ __volatile__("1:\n\t"
231 "ldx [%2], %%g7\n\t"
232 "and %%g7, %1, %%g1\n\t"
233 "or %%g1, %0, %%g1\n\t"
234 "casx [%2], %%g7, %%g1\n\t"
235 "cmp %%g7, %%g1\n\t"
236 "bne,pn %%xcc, 1b\n\t"
b445e26c 237 " nop"
1da177e4
LT
238 : /* no outputs */
239 : "r" (mask), "r" (non_cpu_bits), "r" (&page->flags)
240 : "g1", "g7");
241}
242
d979f179 243static inline void clear_dcache_dirty_cpu(struct page *page, unsigned long cpu)
1da177e4
LT
244{
245 unsigned long mask = (1UL << PG_dcache_dirty);
246
247 __asm__ __volatile__("! test_and_clear_dcache_dirty\n"
248 "1:\n\t"
249 "ldx [%2], %%g7\n\t"
48b0e548 250 "srlx %%g7, %4, %%g1\n\t"
1da177e4
LT
251 "and %%g1, %3, %%g1\n\t"
252 "cmp %%g1, %0\n\t"
253 "bne,pn %%icc, 2f\n\t"
254 " andn %%g7, %1, %%g1\n\t"
255 "casx [%2], %%g7, %%g1\n\t"
256 "cmp %%g7, %%g1\n\t"
257 "bne,pn %%xcc, 1b\n\t"
b445e26c 258 " nop\n"
1da177e4
LT
259 "2:"
260 : /* no outputs */
261 : "r" (cpu), "r" (mask), "r" (&page->flags),
48b0e548
DM
262 "i" (PG_dcache_cpu_mask),
263 "i" (PG_dcache_cpu_shift)
1da177e4
LT
264 : "g1", "g7");
265}
266
517af332
DM
267static inline void tsb_insert(struct tsb *ent, unsigned long tag, unsigned long pte)
268{
269 unsigned long tsb_addr = (unsigned long) ent;
270
3b3ab2eb 271 if (tlb_type == cheetah_plus || tlb_type == hypervisor)
517af332
DM
272 tsb_addr = __pa(tsb_addr);
273
274 __tsb_insert(tsb_addr, tag, pte);
275}
276
c4bce90e
DM
277unsigned long _PAGE_ALL_SZ_BITS __read_mostly;
278unsigned long _PAGE_SZBITS __read_mostly;
279
ff9aefbf 280static void flush_dcache(unsigned long pfn)
1da177e4 281{
ff9aefbf 282 struct page *page;
7a591cfe 283
ff9aefbf 284 page = pfn_to_page(pfn);
1a78cedb 285 if (page) {
7a591cfe 286 unsigned long pg_flags;
7a591cfe 287
ff9aefbf
SR
288 pg_flags = page->flags;
289 if (pg_flags & (1UL << PG_dcache_dirty)) {
7a591cfe
DM
290 int cpu = ((pg_flags >> PG_dcache_cpu_shift) &
291 PG_dcache_cpu_mask);
292 int this_cpu = get_cpu();
293
294 /* This is just to optimize away some function calls
295 * in the SMP case.
296 */
297 if (cpu == this_cpu)
298 flush_dcache_page_impl(page);
299 else
300 smp_flush_dcache_page_impl(page, cpu);
301
302 clear_dcache_dirty_cpu(page, cpu);
303
304 put_cpu();
305 }
1da177e4 306 }
ff9aefbf
SR
307}
308
4b3073e1 309void update_mmu_cache(struct vm_area_struct *vma, unsigned long address, pte_t *ptep)
ff9aefbf
SR
310{
311 struct mm_struct *mm;
312 struct tsb *tsb;
313 unsigned long tag, flags;
314 unsigned long tsb_index, tsb_hash_shift;
4b3073e1 315 pte_t pte = *ptep;
ff9aefbf
SR
316
317 if (tlb_type != hypervisor) {
318 unsigned long pfn = pte_pfn(pte);
319
320 if (pfn_valid(pfn))
321 flush_dcache(pfn);
322 }
bd40791e
DM
323
324 mm = vma->vm_mm;
7a1ac526 325
dcc1e8dd
DM
326 tsb_index = MM_TSB_BASE;
327 tsb_hash_shift = PAGE_SHIFT;
328
7a1ac526
DM
329 spin_lock_irqsave(&mm->context.lock, flags);
330
dcc1e8dd
DM
331#ifdef CONFIG_HUGETLB_PAGE
332 if (mm->context.tsb_block[MM_TSB_HUGE].tsb != NULL) {
333 if ((tlb_type == hypervisor &&
334 (pte_val(pte) & _PAGE_SZALL_4V) == _PAGE_SZHUGE_4V) ||
335 (tlb_type != hypervisor &&
336 (pte_val(pte) & _PAGE_SZALL_4U) == _PAGE_SZHUGE_4U)) {
337 tsb_index = MM_TSB_HUGE;
338 tsb_hash_shift = HPAGE_SHIFT;
339 }
340 }
341#endif
342
343 tsb = mm->context.tsb_block[tsb_index].tsb;
344 tsb += ((address >> tsb_hash_shift) &
345 (mm->context.tsb_block[tsb_index].tsb_nentries - 1UL));
74ae9987
DM
346 tag = (address >> 22UL);
347 tsb_insert(tsb, tag, pte_val(pte));
7a1ac526
DM
348
349 spin_unlock_irqrestore(&mm->context.lock, flags);
1da177e4
LT
350}
351
352void flush_dcache_page(struct page *page)
353{
a9546f59
DM
354 struct address_space *mapping;
355 int this_cpu;
1da177e4 356
7a591cfe
DM
357 if (tlb_type == hypervisor)
358 return;
359
a9546f59
DM
360 /* Do not bother with the expensive D-cache flush if it
361 * is merely the zero page. The 'bigcore' testcase in GDB
362 * causes this case to run millions of times.
363 */
364 if (page == ZERO_PAGE(0))
365 return;
366
367 this_cpu = get_cpu();
368
369 mapping = page_mapping(page);
1da177e4 370 if (mapping && !mapping_mapped(mapping)) {
a9546f59 371 int dirty = test_bit(PG_dcache_dirty, &page->flags);
1da177e4 372 if (dirty) {
a9546f59
DM
373 int dirty_cpu = dcache_dirty_cpu(page);
374
1da177e4
LT
375 if (dirty_cpu == this_cpu)
376 goto out;
377 smp_flush_dcache_page_impl(page, dirty_cpu);
378 }
379 set_dcache_dirty(page, this_cpu);
380 } else {
381 /* We could delay the flush for the !page_mapping
382 * case too. But that case is for exec env/arg
383 * pages and those are %99 certainly going to get
384 * faulted into the tlb (and thus flushed) anyways.
385 */
386 flush_dcache_page_impl(page);
387 }
388
389out:
390 put_cpu();
391}
917c3660 392EXPORT_SYMBOL(flush_dcache_page);
1da177e4 393
05e14cb3 394void __kprobes flush_icache_range(unsigned long start, unsigned long end)
1da177e4 395{
a43fe0e7 396 /* Cheetah and Hypervisor platform cpus have coherent I-cache. */
1da177e4
LT
397 if (tlb_type == spitfire) {
398 unsigned long kaddr;
399
a94aa253
DM
400 /* This code only runs on Spitfire cpus so this is
401 * why we can assume _PAGE_PADDR_4U.
402 */
403 for (kaddr = start; kaddr < end; kaddr += PAGE_SIZE) {
404 unsigned long paddr, mask = _PAGE_PADDR_4U;
405
406 if (kaddr >= PAGE_OFFSET)
407 paddr = kaddr & mask;
408 else {
409 pgd_t *pgdp = pgd_offset_k(kaddr);
410 pud_t *pudp = pud_offset(pgdp, kaddr);
411 pmd_t *pmdp = pmd_offset(pudp, kaddr);
412 pte_t *ptep = pte_offset_kernel(pmdp, kaddr);
413
414 paddr = pte_val(*ptep) & mask;
415 }
416 __flush_icache_page(paddr);
417 }
1da177e4
LT
418 }
419}
917c3660 420EXPORT_SYMBOL(flush_icache_range);
1da177e4 421
1da177e4
LT
422void mmu_info(struct seq_file *m)
423{
ce33fdc5
DM
424 static const char *pgsz_strings[] = {
425 "8K", "64K", "512K", "4MB", "32MB",
426 "256MB", "2GB", "16GB",
427 };
428 int i, printed;
429
1da177e4
LT
430 if (tlb_type == cheetah)
431 seq_printf(m, "MMU Type\t: Cheetah\n");
432 else if (tlb_type == cheetah_plus)
433 seq_printf(m, "MMU Type\t: Cheetah+\n");
434 else if (tlb_type == spitfire)
435 seq_printf(m, "MMU Type\t: Spitfire\n");
a43fe0e7
DM
436 else if (tlb_type == hypervisor)
437 seq_printf(m, "MMU Type\t: Hypervisor (sun4v)\n");
1da177e4
LT
438 else
439 seq_printf(m, "MMU Type\t: ???\n");
440
ce33fdc5
DM
441 seq_printf(m, "MMU PGSZs\t: ");
442 printed = 0;
443 for (i = 0; i < ARRAY_SIZE(pgsz_strings); i++) {
444 if (cpu_pgsz_mask & (1UL << i)) {
445 seq_printf(m, "%s%s",
446 printed ? "," : "", pgsz_strings[i]);
447 printed++;
448 }
449 }
450 seq_putc(m, '\n');
451
1da177e4
LT
452#ifdef CONFIG_DEBUG_DCFLUSH
453 seq_printf(m, "DCPageFlushes\t: %d\n",
454 atomic_read(&dcpage_flushes));
455#ifdef CONFIG_SMP
456 seq_printf(m, "DCPageFlushesXC\t: %d\n",
457 atomic_read(&dcpage_flushes_xcall));
458#endif /* CONFIG_SMP */
459#endif /* CONFIG_DEBUG_DCFLUSH */
460}
461
a94aa253
DM
462struct linux_prom_translation prom_trans[512] __read_mostly;
463unsigned int prom_trans_ents __read_mostly;
464
1da177e4
LT
465unsigned long kern_locked_tte_data;
466
c9c10830
DM
467/* The obp translations are saved based on 8k pagesize, since obp can
468 * use a mixture of pagesizes. Misses to the LOW_OBP_ADDRESS ->
74bf4312 469 * HI_OBP_ADDRESS range are handled in ktlb.S.
c9c10830 470 */
5085b4a5
DM
471static inline int in_obp_range(unsigned long vaddr)
472{
473 return (vaddr >= LOW_OBP_ADDRESS &&
474 vaddr < HI_OBP_ADDRESS);
475}
476
c9c10830 477static int cmp_ptrans(const void *a, const void *b)
405599bd 478{
c9c10830 479 const struct linux_prom_translation *x = a, *y = b;
405599bd 480
c9c10830
DM
481 if (x->virt > y->virt)
482 return 1;
483 if (x->virt < y->virt)
484 return -1;
485 return 0;
405599bd
DM
486}
487
c9c10830 488/* Read OBP translations property into 'prom_trans[]'. */
9ad98c5b 489static void __init read_obp_translations(void)
405599bd 490{
c9c10830 491 int n, node, ents, first, last, i;
1da177e4
LT
492
493 node = prom_finddevice("/virtual-memory");
494 n = prom_getproplen(node, "translations");
405599bd 495 if (unlikely(n == 0 || n == -1)) {
b206fc4c 496 prom_printf("prom_mappings: Couldn't get size.\n");
1da177e4
LT
497 prom_halt();
498 }
405599bd
DM
499 if (unlikely(n > sizeof(prom_trans))) {
500 prom_printf("prom_mappings: Size %Zd is too big.\n", n);
1da177e4
LT
501 prom_halt();
502 }
405599bd 503
b206fc4c 504 if ((n = prom_getproperty(node, "translations",
405599bd
DM
505 (char *)&prom_trans[0],
506 sizeof(prom_trans))) == -1) {
b206fc4c 507 prom_printf("prom_mappings: Couldn't get property.\n");
1da177e4
LT
508 prom_halt();
509 }
9ad98c5b 510
b206fc4c 511 n = n / sizeof(struct linux_prom_translation);
9ad98c5b 512
c9c10830
DM
513 ents = n;
514
515 sort(prom_trans, ents, sizeof(struct linux_prom_translation),
516 cmp_ptrans, NULL);
517
518 /* Now kick out all the non-OBP entries. */
519 for (i = 0; i < ents; i++) {
520 if (in_obp_range(prom_trans[i].virt))
521 break;
522 }
523 first = i;
524 for (; i < ents; i++) {
525 if (!in_obp_range(prom_trans[i].virt))
526 break;
527 }
528 last = i;
529
530 for (i = 0; i < (last - first); i++) {
531 struct linux_prom_translation *src = &prom_trans[i + first];
532 struct linux_prom_translation *dest = &prom_trans[i];
533
534 *dest = *src;
535 }
536 for (; i < ents; i++) {
537 struct linux_prom_translation *dest = &prom_trans[i];
538 dest->virt = dest->size = dest->data = 0x0UL;
539 }
540
541 prom_trans_ents = last - first;
542
543 if (tlb_type == spitfire) {
544 /* Clear diag TTE bits. */
545 for (i = 0; i < prom_trans_ents; i++)
546 prom_trans[i].data &= ~0x0003fe0000000000UL;
547 }
f4142cba
DM
548
549 /* Force execute bit on. */
550 for (i = 0; i < prom_trans_ents; i++)
551 prom_trans[i].data |= (tlb_type == hypervisor ?
552 _PAGE_EXEC_4V : _PAGE_EXEC_4U);
405599bd 553}
1da177e4 554
d82ace7d
DM
555static void __init hypervisor_tlb_lock(unsigned long vaddr,
556 unsigned long pte,
557 unsigned long mmu)
558{
7db35f31
DM
559 unsigned long ret = sun4v_mmu_map_perm_addr(vaddr, 0, pte, mmu);
560
561 if (ret != 0) {
12e126ad 562 prom_printf("hypervisor_tlb_lock[%lx:%lx:%lx:%lx]: "
7db35f31 563 "errors with %lx\n", vaddr, 0, pte, mmu, ret);
12e126ad
DM
564 prom_halt();
565 }
d82ace7d
DM
566}
567
c4bce90e
DM
568static unsigned long kern_large_tte(unsigned long paddr);
569
898cf0ec 570static void __init remap_kernel(void)
405599bd
DM
571{
572 unsigned long phys_page, tte_vaddr, tte_data;
64658743 573 int i, tlb_ent = sparc64_highest_locked_tlbent();
405599bd 574
1da177e4 575 tte_vaddr = (unsigned long) KERNBASE;
bff06d55 576 phys_page = (prom_boot_mapping_phys_low >> 22UL) << 22UL;
c4bce90e 577 tte_data = kern_large_tte(phys_page);
1da177e4
LT
578
579 kern_locked_tte_data = tte_data;
580
d82ace7d
DM
581 /* Now lock us into the TLBs via Hypervisor or OBP. */
582 if (tlb_type == hypervisor) {
64658743 583 for (i = 0; i < num_kernel_image_mappings; i++) {
d82ace7d
DM
584 hypervisor_tlb_lock(tte_vaddr, tte_data, HV_MMU_DMMU);
585 hypervisor_tlb_lock(tte_vaddr, tte_data, HV_MMU_IMMU);
64658743
DM
586 tte_vaddr += 0x400000;
587 tte_data += 0x400000;
d82ace7d
DM
588 }
589 } else {
64658743
DM
590 for (i = 0; i < num_kernel_image_mappings; i++) {
591 prom_dtlb_load(tlb_ent - i, tte_data, tte_vaddr);
592 prom_itlb_load(tlb_ent - i, tte_data, tte_vaddr);
593 tte_vaddr += 0x400000;
594 tte_data += 0x400000;
d82ace7d 595 }
64658743 596 sparc64_highest_unlocked_tlb_ent = tlb_ent - i;
1da177e4 597 }
0835ae0f
DM
598 if (tlb_type == cheetah_plus) {
599 sparc64_kern_pri_context = (CTX_CHEETAH_PLUS_CTX0 |
600 CTX_CHEETAH_PLUS_NUC);
601 sparc64_kern_pri_nuc_bits = CTX_CHEETAH_PLUS_NUC;
602 sparc64_kern_sec_context = CTX_CHEETAH_PLUS_CTX0;
603 }
405599bd 604}
1da177e4 605
405599bd 606
c9c10830 607static void __init inherit_prom_mappings(void)
9ad98c5b 608{
405599bd 609 /* Now fixup OBP's idea about where we really are mapped. */
3c62a2d3 610 printk("Remapping the kernel... ");
405599bd 611 remap_kernel();
3c62a2d3 612 printk("done.\n");
1da177e4
LT
613}
614
1da177e4
LT
615void prom_world(int enter)
616{
1da177e4
LT
617 if (!enter)
618 set_fs((mm_segment_t) { get_thread_current_ds() });
619
3487d1d4 620 __asm__ __volatile__("flushw");
1da177e4
LT
621}
622
1da177e4
LT
623void __flush_dcache_range(unsigned long start, unsigned long end)
624{
625 unsigned long va;
626
627 if (tlb_type == spitfire) {
628 int n = 0;
629
630 for (va = start; va < end; va += 32) {
631 spitfire_put_dcache_tag(va & 0x3fe0, 0x0);
632 if (++n >= 512)
633 break;
634 }
a43fe0e7 635 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
1da177e4
LT
636 start = __pa(start);
637 end = __pa(end);
638 for (va = start; va < end; va += 32)
639 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
640 "membar #Sync"
641 : /* no outputs */
642 : "r" (va),
643 "i" (ASI_DCACHE_INVALIDATE));
644 }
645}
917c3660 646EXPORT_SYMBOL(__flush_dcache_range);
1da177e4 647
85f1e1f6
DM
648/* get_new_mmu_context() uses "cache + 1". */
649DEFINE_SPINLOCK(ctx_alloc_lock);
650unsigned long tlb_context_cache = CTX_FIRST_VERSION - 1;
651#define MAX_CTX_NR (1UL << CTX_NR_BITS)
652#define CTX_BMAP_SLOTS BITS_TO_LONGS(MAX_CTX_NR)
653DECLARE_BITMAP(mmu_context_bmap, MAX_CTX_NR);
654
1da177e4
LT
655/* Caller does TLB context flushing on local CPU if necessary.
656 * The caller also ensures that CTX_VALID(mm->context) is false.
657 *
658 * We must be careful about boundary cases so that we never
659 * let the user have CTX 0 (nucleus) or we ever use a CTX
660 * version of zero (and thus NO_CONTEXT would not be caught
661 * by version mis-match tests in mmu_context.h).
a0663a79
DM
662 *
663 * Always invoked with interrupts disabled.
1da177e4
LT
664 */
665void get_new_mmu_context(struct mm_struct *mm)
666{
667 unsigned long ctx, new_ctx;
668 unsigned long orig_pgsz_bits;
a77754b4 669 unsigned long flags;
a0663a79 670 int new_version;
1da177e4 671
a77754b4 672 spin_lock_irqsave(&ctx_alloc_lock, flags);
1da177e4
LT
673 orig_pgsz_bits = (mm->context.sparc64_ctx_val & CTX_PGSZ_MASK);
674 ctx = (tlb_context_cache + 1) & CTX_NR_MASK;
675 new_ctx = find_next_zero_bit(mmu_context_bmap, 1 << CTX_NR_BITS, ctx);
a0663a79 676 new_version = 0;
1da177e4
LT
677 if (new_ctx >= (1 << CTX_NR_BITS)) {
678 new_ctx = find_next_zero_bit(mmu_context_bmap, ctx, 1);
679 if (new_ctx >= ctx) {
680 int i;
681 new_ctx = (tlb_context_cache & CTX_VERSION_MASK) +
682 CTX_FIRST_VERSION;
683 if (new_ctx == 1)
684 new_ctx = CTX_FIRST_VERSION;
685
686 /* Don't call memset, for 16 entries that's just
687 * plain silly...
688 */
689 mmu_context_bmap[0] = 3;
690 mmu_context_bmap[1] = 0;
691 mmu_context_bmap[2] = 0;
692 mmu_context_bmap[3] = 0;
693 for (i = 4; i < CTX_BMAP_SLOTS; i += 4) {
694 mmu_context_bmap[i + 0] = 0;
695 mmu_context_bmap[i + 1] = 0;
696 mmu_context_bmap[i + 2] = 0;
697 mmu_context_bmap[i + 3] = 0;
698 }
a0663a79 699 new_version = 1;
1da177e4
LT
700 goto out;
701 }
702 }
703 mmu_context_bmap[new_ctx>>6] |= (1UL << (new_ctx & 63));
704 new_ctx |= (tlb_context_cache & CTX_VERSION_MASK);
705out:
706 tlb_context_cache = new_ctx;
707 mm->context.sparc64_ctx_val = new_ctx | orig_pgsz_bits;
a77754b4 708 spin_unlock_irqrestore(&ctx_alloc_lock, flags);
a0663a79
DM
709
710 if (unlikely(new_version))
711 smp_new_mmu_context_version();
1da177e4
LT
712}
713
919ee677
DM
714static int numa_enabled = 1;
715static int numa_debug;
716
717static int __init early_numa(char *p)
1da177e4 718{
919ee677
DM
719 if (!p)
720 return 0;
721
722 if (strstr(p, "off"))
723 numa_enabled = 0;
d1112018 724
919ee677
DM
725 if (strstr(p, "debug"))
726 numa_debug = 1;
d1112018 727
919ee677 728 return 0;
d1112018 729}
919ee677
DM
730early_param("numa", early_numa);
731
732#define numadbg(f, a...) \
733do { if (numa_debug) \
734 printk(KERN_INFO f, ## a); \
735} while (0)
d1112018 736
4e82c9a6
DM
737static void __init find_ramdisk(unsigned long phys_base)
738{
739#ifdef CONFIG_BLK_DEV_INITRD
740 if (sparc_ramdisk_image || sparc_ramdisk_image64) {
741 unsigned long ramdisk_image;
742
743 /* Older versions of the bootloader only supported a
744 * 32-bit physical address for the ramdisk image
745 * location, stored at sparc_ramdisk_image. Newer
746 * SILO versions set sparc_ramdisk_image to zero and
747 * provide a full 64-bit physical address at
748 * sparc_ramdisk_image64.
749 */
750 ramdisk_image = sparc_ramdisk_image;
751 if (!ramdisk_image)
752 ramdisk_image = sparc_ramdisk_image64;
753
754 /* Another bootloader quirk. The bootloader normalizes
755 * the physical address to KERNBASE, so we have to
756 * factor that back out and add in the lowest valid
757 * physical page address to get the true physical address.
758 */
759 ramdisk_image -= KERNBASE;
760 ramdisk_image += phys_base;
761
919ee677
DM
762 numadbg("Found ramdisk at physical address 0x%lx, size %u\n",
763 ramdisk_image, sparc_ramdisk_size);
764
4e82c9a6
DM
765 initrd_start = ramdisk_image;
766 initrd_end = ramdisk_image + sparc_ramdisk_size;
3b2a7e23 767
95f72d1e 768 memblock_reserve(initrd_start, sparc_ramdisk_size);
d45100f7
DM
769
770 initrd_start += PAGE_OFFSET;
771 initrd_end += PAGE_OFFSET;
4e82c9a6
DM
772 }
773#endif
774}
775
919ee677
DM
776struct node_mem_mask {
777 unsigned long mask;
778 unsigned long val;
919ee677
DM
779};
780static struct node_mem_mask node_masks[MAX_NUMNODES];
781static int num_node_masks;
782
783int numa_cpu_lookup_table[NR_CPUS];
784cpumask_t numa_cpumask_lookup_table[MAX_NUMNODES];
785
786#ifdef CONFIG_NEED_MULTIPLE_NODES
919ee677
DM
787
788struct mdesc_mblock {
789 u64 base;
790 u64 size;
791 u64 offset; /* RA-to-PA */
792};
793static struct mdesc_mblock *mblocks;
794static int num_mblocks;
795
796static unsigned long ra_to_pa(unsigned long addr)
797{
798 int i;
799
800 for (i = 0; i < num_mblocks; i++) {
801 struct mdesc_mblock *m = &mblocks[i];
802
803 if (addr >= m->base &&
804 addr < (m->base + m->size)) {
805 addr += m->offset;
806 break;
807 }
808 }
809 return addr;
810}
811
812static int find_node(unsigned long addr)
813{
814 int i;
815
816 addr = ra_to_pa(addr);
817 for (i = 0; i < num_node_masks; i++) {
818 struct node_mem_mask *p = &node_masks[i];
819
820 if ((addr & p->mask) == p->val)
821 return i;
822 }
823 return -1;
824}
825
f9b18db3 826static u64 memblock_nid_range(u64 start, u64 end, int *nid)
919ee677
DM
827{
828 *nid = find_node(start);
829 start += PAGE_SIZE;
830 while (start < end) {
831 int n = find_node(start);
832
833 if (n != *nid)
834 break;
835 start += PAGE_SIZE;
836 }
837
c918dcce
DM
838 if (start > end)
839 start = end;
840
919ee677
DM
841 return start;
842}
919ee677
DM
843#endif
844
845/* This must be invoked after performing all of the necessary
2a4814df 846 * memblock_set_node() calls for 'nid'. We need to be able to get
919ee677 847 * correct data from get_pfn_range_for_nid().
f1cfdb55 848 */
919ee677
DM
849static void __init allocate_node_data(int nid)
850{
919ee677 851 struct pglist_data *p;
aa6f0790 852 unsigned long start_pfn, end_pfn;
919ee677 853#ifdef CONFIG_NEED_MULTIPLE_NODES
aa6f0790
PG
854 unsigned long paddr;
855
9d1e2492 856 paddr = memblock_alloc_try_nid(sizeof(struct pglist_data), SMP_CACHE_BYTES, nid);
919ee677
DM
857 if (!paddr) {
858 prom_printf("Cannot allocate pglist_data for nid[%d]\n", nid);
859 prom_halt();
860 }
861 NODE_DATA(nid) = __va(paddr);
862 memset(NODE_DATA(nid), 0, sizeof(struct pglist_data));
863
625d693e 864 NODE_DATA(nid)->node_id = nid;
919ee677
DM
865#endif
866
867 p = NODE_DATA(nid);
868
869 get_pfn_range_for_nid(nid, &start_pfn, &end_pfn);
870 p->node_start_pfn = start_pfn;
871 p->node_spanned_pages = end_pfn - start_pfn;
919ee677
DM
872}
873
874static void init_node_masks_nonnuma(void)
d1112018 875{
1da177e4
LT
876 int i;
877
919ee677 878 numadbg("Initializing tables for non-numa.\n");
6fc5bae7 879
919ee677
DM
880 node_masks[0].mask = node_masks[0].val = 0;
881 num_node_masks = 1;
d1112018 882
919ee677
DM
883 for (i = 0; i < NR_CPUS; i++)
884 numa_cpu_lookup_table[i] = 0;
1da177e4 885
fb1fece5 886 cpumask_setall(&numa_cpumask_lookup_table[0]);
919ee677
DM
887}
888
889#ifdef CONFIG_NEED_MULTIPLE_NODES
890struct pglist_data *node_data[MAX_NUMNODES];
891
892EXPORT_SYMBOL(numa_cpu_lookup_table);
893EXPORT_SYMBOL(numa_cpumask_lookup_table);
894EXPORT_SYMBOL(node_data);
895
896struct mdesc_mlgroup {
897 u64 node;
898 u64 latency;
899 u64 match;
900 u64 mask;
901};
902static struct mdesc_mlgroup *mlgroups;
903static int num_mlgroups;
904
905static int scan_pio_for_cfg_handle(struct mdesc_handle *md, u64 pio,
906 u32 cfg_handle)
907{
908 u64 arc;
909
910 mdesc_for_each_arc(arc, md, pio, MDESC_ARC_TYPE_FWD) {
911 u64 target = mdesc_arc_target(md, arc);
912 const u64 *val;
913
914 val = mdesc_get_property(md, target,
915 "cfg-handle", NULL);
916 if (val && *val == cfg_handle)
917 return 0;
918 }
919 return -ENODEV;
920}
921
922static int scan_arcs_for_cfg_handle(struct mdesc_handle *md, u64 grp,
923 u32 cfg_handle)
924{
925 u64 arc, candidate, best_latency = ~(u64)0;
926
927 candidate = MDESC_NODE_NULL;
928 mdesc_for_each_arc(arc, md, grp, MDESC_ARC_TYPE_FWD) {
929 u64 target = mdesc_arc_target(md, arc);
930 const char *name = mdesc_node_name(md, target);
931 const u64 *val;
932
933 if (strcmp(name, "pio-latency-group"))
934 continue;
935
936 val = mdesc_get_property(md, target, "latency", NULL);
937 if (!val)
938 continue;
939
940 if (*val < best_latency) {
941 candidate = target;
942 best_latency = *val;
943 }
944 }
945
946 if (candidate == MDESC_NODE_NULL)
947 return -ENODEV;
948
949 return scan_pio_for_cfg_handle(md, candidate, cfg_handle);
950}
951
952int of_node_to_nid(struct device_node *dp)
953{
954 const struct linux_prom64_registers *regs;
955 struct mdesc_handle *md;
956 u32 cfg_handle;
957 int count, nid;
958 u64 grp;
959
072bd413
DM
960 /* This is the right thing to do on currently supported
961 * SUN4U NUMA platforms as well, as the PCI controller does
962 * not sit behind any particular memory controller.
963 */
919ee677
DM
964 if (!mlgroups)
965 return -1;
966
967 regs = of_get_property(dp, "reg", NULL);
968 if (!regs)
969 return -1;
970
971 cfg_handle = (regs->phys_addr >> 32UL) & 0x0fffffff;
972
973 md = mdesc_grab();
974
975 count = 0;
976 nid = -1;
977 mdesc_for_each_node_by_name(md, grp, "group") {
978 if (!scan_arcs_for_cfg_handle(md, grp, cfg_handle)) {
979 nid = count;
980 break;
981 }
982 count++;
983 }
984
985 mdesc_release(md);
986
987 return nid;
988}
989
01c45381 990static void __init add_node_ranges(void)
919ee677 991{
08b84798 992 struct memblock_region *reg;
919ee677 993
08b84798
BH
994 for_each_memblock(memory, reg) {
995 unsigned long size = reg->size;
919ee677
DM
996 unsigned long start, end;
997
08b84798 998 start = reg->base;
919ee677
DM
999 end = start + size;
1000 while (start < end) {
1001 unsigned long this_end;
1002 int nid;
1003
35a1f0bd 1004 this_end = memblock_nid_range(start, end, &nid);
919ee677 1005
2a4814df 1006 numadbg("Setting memblock NUMA node nid[%d] "
919ee677
DM
1007 "start[%lx] end[%lx]\n",
1008 nid, start, this_end);
1009
2a4814df 1010 memblock_set_node(start, this_end - start, nid);
919ee677
DM
1011 start = this_end;
1012 }
1013 }
1014}
1015
1016static int __init grab_mlgroups(struct mdesc_handle *md)
1017{
1018 unsigned long paddr;
1019 int count = 0;
1020 u64 node;
1021
1022 mdesc_for_each_node_by_name(md, node, "memory-latency-group")
1023 count++;
1024 if (!count)
1025 return -ENOENT;
1026
95f72d1e 1027 paddr = memblock_alloc(count * sizeof(struct mdesc_mlgroup),
919ee677
DM
1028 SMP_CACHE_BYTES);
1029 if (!paddr)
1030 return -ENOMEM;
1031
1032 mlgroups = __va(paddr);
1033 num_mlgroups = count;
1034
1035 count = 0;
1036 mdesc_for_each_node_by_name(md, node, "memory-latency-group") {
1037 struct mdesc_mlgroup *m = &mlgroups[count++];
1038 const u64 *val;
1039
1040 m->node = node;
1041
1042 val = mdesc_get_property(md, node, "latency", NULL);
1043 m->latency = *val;
1044 val = mdesc_get_property(md, node, "address-match", NULL);
1045 m->match = *val;
1046 val = mdesc_get_property(md, node, "address-mask", NULL);
1047 m->mask = *val;
1048
90181136
SR
1049 numadbg("MLGROUP[%d]: node[%llx] latency[%llx] "
1050 "match[%llx] mask[%llx]\n",
919ee677
DM
1051 count - 1, m->node, m->latency, m->match, m->mask);
1052 }
1053
1054 return 0;
1055}
1056
1057static int __init grab_mblocks(struct mdesc_handle *md)
1058{
1059 unsigned long paddr;
1060 int count = 0;
1061 u64 node;
1062
1063 mdesc_for_each_node_by_name(md, node, "mblock")
1064 count++;
1065 if (!count)
1066 return -ENOENT;
1067
95f72d1e 1068 paddr = memblock_alloc(count * sizeof(struct mdesc_mblock),
919ee677
DM
1069 SMP_CACHE_BYTES);
1070 if (!paddr)
1071 return -ENOMEM;
1072
1073 mblocks = __va(paddr);
1074 num_mblocks = count;
1075
1076 count = 0;
1077 mdesc_for_each_node_by_name(md, node, "mblock") {
1078 struct mdesc_mblock *m = &mblocks[count++];
1079 const u64 *val;
1080
1081 val = mdesc_get_property(md, node, "base", NULL);
1082 m->base = *val;
1083 val = mdesc_get_property(md, node, "size", NULL);
1084 m->size = *val;
1085 val = mdesc_get_property(md, node,
1086 "address-congruence-offset", NULL);
1087 m->offset = *val;
1088
90181136 1089 numadbg("MBLOCK[%d]: base[%llx] size[%llx] offset[%llx]\n",
919ee677
DM
1090 count - 1, m->base, m->size, m->offset);
1091 }
1092
1093 return 0;
1094}
1095
1096static void __init numa_parse_mdesc_group_cpus(struct mdesc_handle *md,
1097 u64 grp, cpumask_t *mask)
1098{
1099 u64 arc;
1100
fb1fece5 1101 cpumask_clear(mask);
919ee677
DM
1102
1103 mdesc_for_each_arc(arc, md, grp, MDESC_ARC_TYPE_BACK) {
1104 u64 target = mdesc_arc_target(md, arc);
1105 const char *name = mdesc_node_name(md, target);
1106 const u64 *id;
1107
1108 if (strcmp(name, "cpu"))
1109 continue;
1110 id = mdesc_get_property(md, target, "id", NULL);
e305cb8f 1111 if (*id < nr_cpu_ids)
fb1fece5 1112 cpumask_set_cpu(*id, mask);
919ee677
DM
1113 }
1114}
1115
1116static struct mdesc_mlgroup * __init find_mlgroup(u64 node)
1117{
1118 int i;
1119
1120 for (i = 0; i < num_mlgroups; i++) {
1121 struct mdesc_mlgroup *m = &mlgroups[i];
1122 if (m->node == node)
1123 return m;
1124 }
1125 return NULL;
1126}
1127
1128static int __init numa_attach_mlgroup(struct mdesc_handle *md, u64 grp,
1129 int index)
1130{
1131 struct mdesc_mlgroup *candidate = NULL;
1132 u64 arc, best_latency = ~(u64)0;
1133 struct node_mem_mask *n;
1134
1135 mdesc_for_each_arc(arc, md, grp, MDESC_ARC_TYPE_FWD) {
1136 u64 target = mdesc_arc_target(md, arc);
1137 struct mdesc_mlgroup *m = find_mlgroup(target);
1138 if (!m)
1139 continue;
1140 if (m->latency < best_latency) {
1141 candidate = m;
1142 best_latency = m->latency;
1143 }
1144 }
1145 if (!candidate)
1146 return -ENOENT;
1147
1148 if (num_node_masks != index) {
1149 printk(KERN_ERR "Inconsistent NUMA state, "
1150 "index[%d] != num_node_masks[%d]\n",
1151 index, num_node_masks);
1152 return -EINVAL;
1153 }
1154
1155 n = &node_masks[num_node_masks++];
1156
1157 n->mask = candidate->mask;
1158 n->val = candidate->match;
1da177e4 1159
90181136 1160 numadbg("NUMA NODE[%d]: mask[%lx] val[%lx] (latency[%llx])\n",
919ee677 1161 index, n->mask, n->val, candidate->latency);
1da177e4 1162
919ee677
DM
1163 return 0;
1164}
1165
1166static int __init numa_parse_mdesc_group(struct mdesc_handle *md, u64 grp,
1167 int index)
1168{
1169 cpumask_t mask;
1170 int cpu;
1171
1172 numa_parse_mdesc_group_cpus(md, grp, &mask);
1173
fb1fece5 1174 for_each_cpu(cpu, &mask)
919ee677 1175 numa_cpu_lookup_table[cpu] = index;
fb1fece5 1176 cpumask_copy(&numa_cpumask_lookup_table[index], &mask);
919ee677
DM
1177
1178 if (numa_debug) {
1179 printk(KERN_INFO "NUMA GROUP[%d]: cpus [ ", index);
fb1fece5 1180 for_each_cpu(cpu, &mask)
919ee677
DM
1181 printk("%d ", cpu);
1182 printk("]\n");
1183 }
1184
1185 return numa_attach_mlgroup(md, grp, index);
1186}
1187
1188static int __init numa_parse_mdesc(void)
1189{
1190 struct mdesc_handle *md = mdesc_grab();
1191 int i, err, count;
1192 u64 node;
1193
1194 node = mdesc_node_by_name(md, MDESC_NODE_NULL, "latency-groups");
1195 if (node == MDESC_NODE_NULL) {
1196 mdesc_release(md);
1197 return -ENOENT;
1198 }
1199
1200 err = grab_mblocks(md);
1201 if (err < 0)
1202 goto out;
1203
1204 err = grab_mlgroups(md);
1205 if (err < 0)
1206 goto out;
1207
1208 count = 0;
1209 mdesc_for_each_node_by_name(md, node, "group") {
1210 err = numa_parse_mdesc_group(md, node, count);
1211 if (err < 0)
1212 break;
1213 count++;
1214 }
1215
1216 add_node_ranges();
1217
1218 for (i = 0; i < num_node_masks; i++) {
1219 allocate_node_data(i);
1220 node_set_online(i);
1221 }
1222
1223 err = 0;
1224out:
1225 mdesc_release(md);
1226 return err;
1227}
1228
072bd413
DM
1229static int __init numa_parse_jbus(void)
1230{
1231 unsigned long cpu, index;
1232
1233 /* NUMA node id is encoded in bits 36 and higher, and there is
1234 * a 1-to-1 mapping from CPU ID to NUMA node ID.
1235 */
1236 index = 0;
1237 for_each_present_cpu(cpu) {
1238 numa_cpu_lookup_table[cpu] = index;
fb1fece5 1239 cpumask_copy(&numa_cpumask_lookup_table[index], cpumask_of(cpu));
072bd413
DM
1240 node_masks[index].mask = ~((1UL << 36UL) - 1UL);
1241 node_masks[index].val = cpu << 36UL;
1242
1243 index++;
1244 }
1245 num_node_masks = index;
1246
1247 add_node_ranges();
1248
1249 for (index = 0; index < num_node_masks; index++) {
1250 allocate_node_data(index);
1251 node_set_online(index);
1252 }
1253
1254 return 0;
1255}
1256
919ee677
DM
1257static int __init numa_parse_sun4u(void)
1258{
072bd413
DM
1259 if (tlb_type == cheetah || tlb_type == cheetah_plus) {
1260 unsigned long ver;
1261
1262 __asm__ ("rdpr %%ver, %0" : "=r" (ver));
1263 if ((ver >> 32UL) == __JALAPENO_ID ||
1264 (ver >> 32UL) == __SERRANO_ID)
1265 return numa_parse_jbus();
1266 }
919ee677
DM
1267 return -1;
1268}
1269
1270static int __init bootmem_init_numa(void)
1271{
1272 int err = -1;
1273
1274 numadbg("bootmem_init_numa()\n");
1275
1276 if (numa_enabled) {
1277 if (tlb_type == hypervisor)
1278 err = numa_parse_mdesc();
1279 else
1280 err = numa_parse_sun4u();
1281 }
1282 return err;
1283}
1284
1285#else
1da177e4 1286
919ee677
DM
1287static int bootmem_init_numa(void)
1288{
1289 return -1;
1290}
1291
1292#endif
1293
1294static void __init bootmem_init_nonnuma(void)
1295{
95f72d1e
YL
1296 unsigned long top_of_ram = memblock_end_of_DRAM();
1297 unsigned long total_ram = memblock_phys_mem_size();
919ee677
DM
1298
1299 numadbg("bootmem_init_nonnuma()\n");
1300
1301 printk(KERN_INFO "Top of RAM: 0x%lx, Total RAM: 0x%lx\n",
1302 top_of_ram, total_ram);
1303 printk(KERN_INFO "Memory hole size: %ldMB\n",
1304 (top_of_ram - total_ram) >> 20);
1305
1306 init_node_masks_nonnuma();
2a4814df 1307 memblock_set_node(0, (phys_addr_t)ULLONG_MAX, 0);
919ee677 1308 allocate_node_data(0);
919ee677
DM
1309 node_set_online(0);
1310}
1311
919ee677
DM
1312static unsigned long __init bootmem_init(unsigned long phys_base)
1313{
1314 unsigned long end_pfn;
919ee677 1315
95f72d1e 1316 end_pfn = memblock_end_of_DRAM() >> PAGE_SHIFT;
919ee677
DM
1317 max_pfn = max_low_pfn = end_pfn;
1318 min_low_pfn = (phys_base >> PAGE_SHIFT);
1319
1320 if (bootmem_init_numa() < 0)
1321 bootmem_init_nonnuma();
1322
625d693e
DM
1323 /* Dump memblock with node info. */
1324 memblock_dump_all();
919ee677 1325
625d693e 1326 /* XXX cpu notifier XXX */
d1112018 1327
625d693e 1328 sparse_memory_present_with_active_regions(MAX_NUMNODES);
d1112018
DM
1329 sparse_init();
1330
1da177e4
LT
1331 return end_pfn;
1332}
1333
9cc3a1ac
DM
1334static struct linux_prom64_registers pall[MAX_BANKS] __initdata;
1335static int pall_ents __initdata;
1336
56425306 1337#ifdef CONFIG_DEBUG_PAGEALLOC
896aef43
SR
1338static unsigned long __ref kernel_map_range(unsigned long pstart,
1339 unsigned long pend, pgprot_t prot)
56425306
DM
1340{
1341 unsigned long vstart = PAGE_OFFSET + pstart;
1342 unsigned long vend = PAGE_OFFSET + pend;
1343 unsigned long alloc_bytes = 0UL;
1344
1345 if ((vstart & ~PAGE_MASK) || (vend & ~PAGE_MASK)) {
13edad7a 1346 prom_printf("kernel_map: Unaligned physmem[%lx:%lx]\n",
56425306
DM
1347 vstart, vend);
1348 prom_halt();
1349 }
1350
1351 while (vstart < vend) {
1352 unsigned long this_end, paddr = __pa(vstart);
1353 pgd_t *pgd = pgd_offset_k(vstart);
1354 pud_t *pud;
1355 pmd_t *pmd;
1356 pte_t *pte;
1357
1358 pud = pud_offset(pgd, vstart);
1359 if (pud_none(*pud)) {
1360 pmd_t *new;
1361
1362 new = __alloc_bootmem(PAGE_SIZE, PAGE_SIZE, PAGE_SIZE);
1363 alloc_bytes += PAGE_SIZE;
1364 pud_populate(&init_mm, pud, new);
1365 }
1366
1367 pmd = pmd_offset(pud, vstart);
1368 if (!pmd_present(*pmd)) {
1369 pte_t *new;
1370
1371 new = __alloc_bootmem(PAGE_SIZE, PAGE_SIZE, PAGE_SIZE);
1372 alloc_bytes += PAGE_SIZE;
1373 pmd_populate_kernel(&init_mm, pmd, new);
1374 }
1375
1376 pte = pte_offset_kernel(pmd, vstart);
1377 this_end = (vstart + PMD_SIZE) & PMD_MASK;
1378 if (this_end > vend)
1379 this_end = vend;
1380
1381 while (vstart < this_end) {
1382 pte_val(*pte) = (paddr | pgprot_val(prot));
1383
1384 vstart += PAGE_SIZE;
1385 paddr += PAGE_SIZE;
1386 pte++;
1387 }
1388 }
1389
1390 return alloc_bytes;
1391}
1392
56425306 1393extern unsigned int kvmap_linear_patch[1];
9cc3a1ac
DM
1394#endif /* CONFIG_DEBUG_PAGEALLOC */
1395
4f93d21d 1396static void __init kpte_set_val(unsigned long index, unsigned long val)
9cc3a1ac 1397{
4f93d21d 1398 unsigned long *ptr = kpte_linear_bitmap;
9cc3a1ac 1399
4f93d21d
DM
1400 val <<= ((index % (BITS_PER_LONG / 2)) * 2);
1401 ptr += (index / (BITS_PER_LONG / 2));
9cc3a1ac 1402
4f93d21d
DM
1403 *ptr |= val;
1404}
f7c00338 1405
4f93d21d
DM
1406static const unsigned long kpte_shift_min = 28; /* 256MB */
1407static const unsigned long kpte_shift_max = 34; /* 16GB */
1408static const unsigned long kpte_shift_incr = 3;
9cc3a1ac 1409
4f93d21d
DM
1410static unsigned long kpte_mark_using_shift(unsigned long start, unsigned long end,
1411 unsigned long shift)
1412{
1413 unsigned long size = (1UL << shift);
1414 unsigned long mask = (size - 1UL);
1415 unsigned long remains = end - start;
1416 unsigned long val;
9cc3a1ac 1417
4f93d21d
DM
1418 if (remains < size || (start & mask))
1419 return start;
9cc3a1ac 1420
4f93d21d
DM
1421 /* VAL maps:
1422 *
1423 * shift 28 --> kern_linear_pte_xor index 1
1424 * shift 31 --> kern_linear_pte_xor index 2
1425 * shift 34 --> kern_linear_pte_xor index 3
1426 */
1427 val = ((shift - kpte_shift_min) / kpte_shift_incr) + 1;
1428
1429 remains &= ~mask;
1430 if (shift != kpte_shift_max)
1431 remains = size;
1432
1433 while (remains) {
1434 unsigned long index = start >> kpte_shift_min;
1435
1436 kpte_set_val(index, val);
1437
1438 start += 1UL << kpte_shift_min;
1439 remains -= 1UL << kpte_shift_min;
1440 }
1441
1442 return start;
1443}
1444
1445static void __init mark_kpte_bitmap(unsigned long start, unsigned long end)
1446{
1447 unsigned long smallest_size, smallest_mask;
1448 unsigned long s;
1449
1450 smallest_size = (1UL << kpte_shift_min);
1451 smallest_mask = (smallest_size - 1UL);
1452
1453 while (start < end) {
1454 unsigned long orig_start = start;
1455
1456 for (s = kpte_shift_max; s >= kpte_shift_min; s -= kpte_shift_incr) {
1457 start = kpte_mark_using_shift(start, end, s);
1458
1459 if (start != orig_start)
1460 break;
9cc3a1ac 1461 }
4f93d21d
DM
1462
1463 if (start == orig_start)
1464 start = (start + smallest_size) & ~smallest_mask;
9cc3a1ac
DM
1465 }
1466}
56425306 1467
8f361453 1468static void __init init_kpte_bitmap(void)
56425306 1469{
9cc3a1ac 1470 unsigned long i;
13edad7a
DM
1471
1472 for (i = 0; i < pall_ents; i++) {
56425306
DM
1473 unsigned long phys_start, phys_end;
1474
13edad7a
DM
1475 phys_start = pall[i].phys_addr;
1476 phys_end = phys_start + pall[i].reg_size;
9cc3a1ac
DM
1477
1478 mark_kpte_bitmap(phys_start, phys_end);
8f361453
DM
1479 }
1480}
9cc3a1ac 1481
8f361453
DM
1482static void __init kernel_physical_mapping_init(void)
1483{
9cc3a1ac 1484#ifdef CONFIG_DEBUG_PAGEALLOC
8f361453
DM
1485 unsigned long i, mem_alloced = 0UL;
1486
1487 for (i = 0; i < pall_ents; i++) {
1488 unsigned long phys_start, phys_end;
1489
1490 phys_start = pall[i].phys_addr;
1491 phys_end = phys_start + pall[i].reg_size;
1492
56425306
DM
1493 mem_alloced += kernel_map_range(phys_start, phys_end,
1494 PAGE_KERNEL);
56425306
DM
1495 }
1496
1497 printk("Allocated %ld bytes for kernel page tables.\n",
1498 mem_alloced);
1499
1500 kvmap_linear_patch[0] = 0x01000000; /* nop */
1501 flushi(&kvmap_linear_patch[0]);
1502
1503 __flush_tlb_all();
9cc3a1ac 1504#endif
56425306
DM
1505}
1506
9cc3a1ac 1507#ifdef CONFIG_DEBUG_PAGEALLOC
56425306
DM
1508void kernel_map_pages(struct page *page, int numpages, int enable)
1509{
1510 unsigned long phys_start = page_to_pfn(page) << PAGE_SHIFT;
1511 unsigned long phys_end = phys_start + (numpages * PAGE_SIZE);
1512
1513 kernel_map_range(phys_start, phys_end,
1514 (enable ? PAGE_KERNEL : __pgprot(0)));
1515
74bf4312
DM
1516 flush_tsb_kernel_range(PAGE_OFFSET + phys_start,
1517 PAGE_OFFSET + phys_end);
1518
56425306
DM
1519 /* we should perform an IPI and flush all tlbs,
1520 * but that can deadlock->flush only current cpu.
1521 */
1522 __flush_tlb_kernel_range(PAGE_OFFSET + phys_start,
1523 PAGE_OFFSET + phys_end);
1524}
1525#endif
1526
10147570
DM
1527unsigned long __init find_ecache_flush_span(unsigned long size)
1528{
0836a0eb
DM
1529 int i;
1530
13edad7a
DM
1531 for (i = 0; i < pavail_ents; i++) {
1532 if (pavail[i].reg_size >= size)
1533 return pavail[i].phys_addr;
0836a0eb
DM
1534 }
1535
13edad7a 1536 return ~0UL;
0836a0eb
DM
1537}
1538
517af332
DM
1539static void __init tsb_phys_patch(void)
1540{
d257d5da 1541 struct tsb_ldquad_phys_patch_entry *pquad;
517af332
DM
1542 struct tsb_phys_patch_entry *p;
1543
d257d5da
DM
1544 pquad = &__tsb_ldquad_phys_patch;
1545 while (pquad < &__tsb_ldquad_phys_patch_end) {
1546 unsigned long addr = pquad->addr;
1547
1548 if (tlb_type == hypervisor)
1549 *(unsigned int *) addr = pquad->sun4v_insn;
1550 else
1551 *(unsigned int *) addr = pquad->sun4u_insn;
1552 wmb();
1553 __asm__ __volatile__("flush %0"
1554 : /* no outputs */
1555 : "r" (addr));
1556
1557 pquad++;
1558 }
1559
517af332
DM
1560 p = &__tsb_phys_patch;
1561 while (p < &__tsb_phys_patch_end) {
1562 unsigned long addr = p->addr;
1563
1564 *(unsigned int *) addr = p->insn;
1565 wmb();
1566 __asm__ __volatile__("flush %0"
1567 : /* no outputs */
1568 : "r" (addr));
1569
1570 p++;
1571 }
1572}
1573
490384e7 1574/* Don't mark as init, we give this to the Hypervisor. */
d1acb421
DM
1575#ifndef CONFIG_DEBUG_PAGEALLOC
1576#define NUM_KTSB_DESCR 2
1577#else
1578#define NUM_KTSB_DESCR 1
1579#endif
1580static struct hv_tsb_descr ktsb_descr[NUM_KTSB_DESCR];
490384e7
DM
1581extern struct tsb swapper_tsb[KERNEL_TSB_NENTRIES];
1582
9076d0e7
DM
1583static void patch_one_ktsb_phys(unsigned int *start, unsigned int *end, unsigned long pa)
1584{
1585 pa >>= KTSB_PHYS_SHIFT;
1586
1587 while (start < end) {
1588 unsigned int *ia = (unsigned int *)(unsigned long)*start;
1589
1590 ia[0] = (ia[0] & ~0x3fffff) | (pa >> 10);
1591 __asm__ __volatile__("flush %0" : : "r" (ia));
1592
1593 ia[1] = (ia[1] & ~0x3ff) | (pa & 0x3ff);
1594 __asm__ __volatile__("flush %0" : : "r" (ia + 1));
1595
1596 start++;
1597 }
1598}
1599
1600static void ktsb_phys_patch(void)
1601{
1602 extern unsigned int __swapper_tsb_phys_patch;
1603 extern unsigned int __swapper_tsb_phys_patch_end;
9076d0e7
DM
1604 unsigned long ktsb_pa;
1605
1606 ktsb_pa = kern_base + ((unsigned long)&swapper_tsb[0] - KERNBASE);
1607 patch_one_ktsb_phys(&__swapper_tsb_phys_patch,
1608 &__swapper_tsb_phys_patch_end, ktsb_pa);
1609#ifndef CONFIG_DEBUG_PAGEALLOC
0785a8e8
DM
1610 {
1611 extern unsigned int __swapper_4m_tsb_phys_patch;
1612 extern unsigned int __swapper_4m_tsb_phys_patch_end;
9076d0e7
DM
1613 ktsb_pa = (kern_base +
1614 ((unsigned long)&swapper_4m_tsb[0] - KERNBASE));
1615 patch_one_ktsb_phys(&__swapper_4m_tsb_phys_patch,
1616 &__swapper_4m_tsb_phys_patch_end, ktsb_pa);
0785a8e8 1617 }
9076d0e7
DM
1618#endif
1619}
1620
490384e7
DM
1621static void __init sun4v_ktsb_init(void)
1622{
1623 unsigned long ktsb_pa;
1624
d7744a09 1625 /* First KTSB for PAGE_SIZE mappings. */
490384e7
DM
1626 ktsb_pa = kern_base + ((unsigned long)&swapper_tsb[0] - KERNBASE);
1627
1628 switch (PAGE_SIZE) {
1629 case 8 * 1024:
1630 default:
1631 ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_8K;
1632 ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_8K;
1633 break;
1634
1635 case 64 * 1024:
1636 ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_64K;
1637 ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_64K;
1638 break;
1639
1640 case 512 * 1024:
1641 ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_512K;
1642 ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_512K;
1643 break;
1644
1645 case 4 * 1024 * 1024:
1646 ktsb_descr[0].pgsz_idx = HV_PGSZ_IDX_4MB;
1647 ktsb_descr[0].pgsz_mask = HV_PGSZ_MASK_4MB;
1648 break;
6cb79b3f 1649 }
490384e7 1650
3f19a84e 1651 ktsb_descr[0].assoc = 1;
490384e7
DM
1652 ktsb_descr[0].num_ttes = KERNEL_TSB_NENTRIES;
1653 ktsb_descr[0].ctx_idx = 0;
1654 ktsb_descr[0].tsb_base = ktsb_pa;
1655 ktsb_descr[0].resv = 0;
1656
d1acb421 1657#ifndef CONFIG_DEBUG_PAGEALLOC
4f93d21d 1658 /* Second KTSB for 4MB/256MB/2GB/16GB mappings. */
d7744a09
DM
1659 ktsb_pa = (kern_base +
1660 ((unsigned long)&swapper_4m_tsb[0] - KERNBASE));
1661
1662 ktsb_descr[1].pgsz_idx = HV_PGSZ_IDX_4MB;
1663 ktsb_descr[1].pgsz_mask = (HV_PGSZ_MASK_4MB |
1664 HV_PGSZ_MASK_256MB);
4f93d21d
DM
1665 if (sun4v_chip_type == SUN4V_CHIP_NIAGARA4)
1666 ktsb_descr[1].pgsz_mask |= HV_PGSZ_MASK_2GB;
d7744a09
DM
1667 ktsb_descr[1].assoc = 1;
1668 ktsb_descr[1].num_ttes = KERNEL_TSB4M_NENTRIES;
1669 ktsb_descr[1].ctx_idx = 0;
1670 ktsb_descr[1].tsb_base = ktsb_pa;
1671 ktsb_descr[1].resv = 0;
d1acb421 1672#endif
490384e7
DM
1673}
1674
1675void __cpuinit sun4v_ktsb_register(void)
1676{
7db35f31 1677 unsigned long pa, ret;
490384e7
DM
1678
1679 pa = kern_base + ((unsigned long)&ktsb_descr[0] - KERNBASE);
1680
7db35f31
DM
1681 ret = sun4v_mmu_tsb_ctx0(NUM_KTSB_DESCR, pa);
1682 if (ret != 0) {
1683 prom_printf("hypervisor_mmu_tsb_ctx0[%lx]: "
1684 "errors with %lx\n", pa, ret);
1685 prom_halt();
1686 }
490384e7
DM
1687}
1688
1da177e4
LT
1689/* paging_init() sets up the page tables */
1690
1da177e4 1691static unsigned long last_valid_pfn;
56425306 1692pgd_t swapper_pg_dir[2048];
1da177e4 1693
c4bce90e
DM
1694static void sun4u_pgprot_init(void);
1695static void sun4v_pgprot_init(void);
1696
1da177e4
LT
1697void __init paging_init(void)
1698{
919ee677 1699 unsigned long end_pfn, shift, phys_base;
0836a0eb 1700 unsigned long real_end, i;
aa6f0790 1701 int node;
0836a0eb 1702
22adb358
DM
1703 /* These build time checkes make sure that the dcache_dirty_cpu()
1704 * page->flags usage will work.
1705 *
1706 * When a page gets marked as dcache-dirty, we store the
1707 * cpu number starting at bit 32 in the page->flags. Also,
1708 * functions like clear_dcache_dirty_cpu use the cpu mask
1709 * in 13-bit signed-immediate instruction fields.
1710 */
9223b419
CL
1711
1712 /*
1713 * Page flags must not reach into upper 32 bits that are used
1714 * for the cpu number
1715 */
1716 BUILD_BUG_ON(NR_PAGEFLAGS > 32);
1717
1718 /*
1719 * The bit fields placed in the high range must not reach below
1720 * the 32 bit boundary. Otherwise we cannot place the cpu field
1721 * at the 32 bit boundary.
1722 */
22adb358 1723 BUILD_BUG_ON(SECTIONS_WIDTH + NODES_WIDTH + ZONES_WIDTH +
9223b419
CL
1724 ilog2(roundup_pow_of_two(NR_CPUS)) > 32);
1725
22adb358
DM
1726 BUILD_BUG_ON(NR_CPUS > 4096);
1727
481295f9
DM
1728 kern_base = (prom_boot_mapping_phys_low >> 22UL) << 22UL;
1729 kern_size = (unsigned long)&_end - (unsigned long)KERNBASE;
1730
d7744a09 1731 /* Invalidate both kernel TSBs. */
8b234274 1732 memset(swapper_tsb, 0x40, sizeof(swapper_tsb));
d1acb421 1733#ifndef CONFIG_DEBUG_PAGEALLOC
d7744a09 1734 memset(swapper_4m_tsb, 0x40, sizeof(swapper_4m_tsb));
d1acb421 1735#endif
8b234274 1736
c4bce90e
DM
1737 if (tlb_type == hypervisor)
1738 sun4v_pgprot_init();
1739 else
1740 sun4u_pgprot_init();
1741
d257d5da 1742 if (tlb_type == cheetah_plus ||
9076d0e7 1743 tlb_type == hypervisor) {
517af332 1744 tsb_phys_patch();
9076d0e7
DM
1745 ktsb_phys_patch();
1746 }
517af332 1747
490384e7 1748 if (tlb_type == hypervisor) {
d257d5da 1749 sun4v_patch_tlb_handlers();
490384e7
DM
1750 sun4v_ktsb_init();
1751 }
d257d5da 1752
a94a172d
DM
1753 /* Find available physical memory...
1754 *
1755 * Read it twice in order to work around a bug in openfirmware.
1756 * The call to grab this table itself can cause openfirmware to
1757 * allocate memory, which in turn can take away some space from
1758 * the list of available memory. Reading it twice makes sure
1759 * we really do get the final value.
1760 */
1761 read_obp_translations();
1762 read_obp_memory("reg", &pall[0], &pall_ents);
1763 read_obp_memory("available", &pavail[0], &pavail_ents);
13edad7a 1764 read_obp_memory("available", &pavail[0], &pavail_ents);
0836a0eb
DM
1765
1766 phys_base = 0xffffffffffffffffUL;
3b2a7e23 1767 for (i = 0; i < pavail_ents; i++) {
13edad7a 1768 phys_base = min(phys_base, pavail[i].phys_addr);
95f72d1e 1769 memblock_add(pavail[i].phys_addr, pavail[i].reg_size);
3b2a7e23
DM
1770 }
1771
95f72d1e 1772 memblock_reserve(kern_base, kern_size);
0836a0eb 1773
4e82c9a6
DM
1774 find_ramdisk(phys_base);
1775
95f72d1e 1776 memblock_enforce_memory_limit(cmdline_memory_size);
25b0c659 1777
1aadc056 1778 memblock_allow_resize();
95f72d1e 1779 memblock_dump_all();
3b2a7e23 1780
1da177e4
LT
1781 set_bit(0, mmu_context_bmap);
1782
2bdb3cb2
DM
1783 shift = kern_base + PAGE_OFFSET - ((unsigned long)KERNBASE);
1784
1da177e4 1785 real_end = (unsigned long)_end;
64658743
DM
1786 num_kernel_image_mappings = DIV_ROUND_UP(real_end - KERNBASE, 1 << 22);
1787 printk("Kernel: Using %d locked TLB entries for main kernel image.\n",
1788 num_kernel_image_mappings);
2bdb3cb2
DM
1789
1790 /* Set kernel pgd to upper alias so physical page computations
1da177e4
LT
1791 * work.
1792 */
1793 init_mm.pgd += ((shift) / (sizeof(pgd_t)));
1794
56425306 1795 memset(swapper_low_pmd_dir, 0, sizeof(swapper_low_pmd_dir));
1da177e4
LT
1796
1797 /* Now can init the kernel/bad page tables. */
1798 pud_set(pud_offset(&swapper_pg_dir[0], 0),
56425306 1799 swapper_low_pmd_dir + (shift / sizeof(pgd_t)));
1da177e4 1800
c9c10830 1801 inherit_prom_mappings();
5085b4a5 1802
8f361453
DM
1803 init_kpte_bitmap();
1804
a8b900d8
DM
1805 /* Ok, we can use our TLB miss and window trap handlers safely. */
1806 setup_tba();
1da177e4 1807
c9c10830 1808 __flush_tlb_all();
9ad98c5b 1809
490384e7
DM
1810 if (tlb_type == hypervisor)
1811 sun4v_ktsb_register();
1812
ad072004 1813 prom_build_devicetree();
b696fdc2 1814 of_populate_present_mask();
b99c6ebe
DM
1815#ifndef CONFIG_SMP
1816 of_fill_in_cpu_data();
1817#endif
ad072004 1818
890db403 1819 if (tlb_type == hypervisor) {
4a283339 1820 sun4v_mdesc_init();
6ac5c610 1821 mdesc_populate_present_mask(cpu_all_mask);
b99c6ebe
DM
1822#ifndef CONFIG_SMP
1823 mdesc_fill_in_cpu_data(cpu_all_mask);
1824#endif
ce33fdc5
DM
1825 mdesc_get_page_sizes(cpu_all_mask, &cpu_pgsz_mask);
1826 } else {
1827 unsigned long impl, ver;
1828
1829 cpu_pgsz_mask = (HV_PGSZ_MASK_8K | HV_PGSZ_MASK_64K |
1830 HV_PGSZ_MASK_512K | HV_PGSZ_MASK_4MB);
1831
1832 __asm__ __volatile__("rdpr %%ver, %0" : "=r" (ver));
1833 impl = ((ver >> 32) & 0xffff);
1834 if (impl == PANTHER_IMPL)
1835 cpu_pgsz_mask |= (HV_PGSZ_MASK_32MB |
1836 HV_PGSZ_MASK_256MB);
890db403 1837 }
4a283339 1838
5ed56f1a
DM
1839 /* Setup bootmem... */
1840 last_valid_pfn = end_pfn = bootmem_init(phys_base);
1841
4f70f7a9
DM
1842 /* Once the OF device tree and MDESC have been setup, we know
1843 * the list of possible cpus. Therefore we can allocate the
1844 * IRQ stacks.
1845 */
1846 for_each_possible_cpu(i) {
aa6f0790 1847 node = cpu_to_node(i);
5ed56f1a
DM
1848
1849 softirq_stack[i] = __alloc_bootmem_node(NODE_DATA(node),
1850 THREAD_SIZE,
1851 THREAD_SIZE, 0);
1852 hardirq_stack[i] = __alloc_bootmem_node(NODE_DATA(node),
1853 THREAD_SIZE,
1854 THREAD_SIZE, 0);
4f70f7a9
DM
1855 }
1856
56425306 1857 kernel_physical_mapping_init();
56425306 1858
1da177e4 1859 {
919ee677 1860 unsigned long max_zone_pfns[MAX_NR_ZONES];
1da177e4 1861
919ee677 1862 memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
1da177e4 1863
919ee677 1864 max_zone_pfns[ZONE_NORMAL] = end_pfn;
1da177e4 1865
919ee677 1866 free_area_init_nodes(max_zone_pfns);
1da177e4
LT
1867 }
1868
3c62a2d3 1869 printk("Booting Linux...\n");
1da177e4
LT
1870}
1871
9a2ed5cc 1872int __devinit page_in_phys_avail(unsigned long paddr)
919ee677
DM
1873{
1874 int i;
1875
1876 paddr &= PAGE_MASK;
1877
1878 for (i = 0; i < pavail_ents; i++) {
1879 unsigned long start, end;
1880
1881 start = pavail[i].phys_addr;
1882 end = start + pavail[i].reg_size;
1883
1884 if (paddr >= start && paddr < end)
1885 return 1;
1886 }
1887 if (paddr >= kern_base && paddr < (kern_base + kern_size))
1888 return 1;
1889#ifdef CONFIG_BLK_DEV_INITRD
1890 if (paddr >= __pa(initrd_start) &&
1891 paddr < __pa(PAGE_ALIGN(initrd_end)))
1892 return 1;
1893#endif
1894
1895 return 0;
1896}
1897
1898static struct linux_prom64_registers pavail_rescan[MAX_BANKS] __initdata;
1899static int pavail_rescan_ents __initdata;
1900
1901/* Certain OBP calls, such as fetching "available" properties, can
1902 * claim physical memory. So, along with initializing the valid
1903 * address bitmap, what we do here is refetch the physical available
1904 * memory list again, and make sure it provides at least as much
1905 * memory as 'pavail' does.
1906 */
d8ed1d43 1907static void __init setup_valid_addr_bitmap_from_pavail(unsigned long *bitmap)
1da177e4 1908{
1da177e4
LT
1909 int i;
1910
13edad7a 1911 read_obp_memory("available", &pavail_rescan[0], &pavail_rescan_ents);
1da177e4 1912
13edad7a 1913 for (i = 0; i < pavail_ents; i++) {
1da177e4
LT
1914 unsigned long old_start, old_end;
1915
13edad7a 1916 old_start = pavail[i].phys_addr;
919ee677 1917 old_end = old_start + pavail[i].reg_size;
1da177e4
LT
1918 while (old_start < old_end) {
1919 int n;
1920
c2a5a46b 1921 for (n = 0; n < pavail_rescan_ents; n++) {
1da177e4
LT
1922 unsigned long new_start, new_end;
1923
13edad7a
DM
1924 new_start = pavail_rescan[n].phys_addr;
1925 new_end = new_start +
1926 pavail_rescan[n].reg_size;
1da177e4
LT
1927
1928 if (new_start <= old_start &&
1929 new_end >= (old_start + PAGE_SIZE)) {
d8ed1d43 1930 set_bit(old_start >> 22, bitmap);
1da177e4
LT
1931 goto do_next_page;
1932 }
1933 }
919ee677
DM
1934
1935 prom_printf("mem_init: Lost memory in pavail\n");
1936 prom_printf("mem_init: OLD start[%lx] size[%lx]\n",
1937 pavail[i].phys_addr,
1938 pavail[i].reg_size);
1939 prom_printf("mem_init: NEW start[%lx] size[%lx]\n",
1940 pavail_rescan[i].phys_addr,
1941 pavail_rescan[i].reg_size);
1942 prom_printf("mem_init: Cannot continue, aborting.\n");
1943 prom_halt();
1da177e4
LT
1944
1945 do_next_page:
1946 old_start += PAGE_SIZE;
1947 }
1948 }
1949}
1950
d8ed1d43
DM
1951static void __init patch_tlb_miss_handler_bitmap(void)
1952{
1953 extern unsigned int valid_addr_bitmap_insn[];
1954 extern unsigned int valid_addr_bitmap_patch[];
1955
1956 valid_addr_bitmap_insn[1] = valid_addr_bitmap_patch[1];
1957 mb();
1958 valid_addr_bitmap_insn[0] = valid_addr_bitmap_patch[0];
1959 flushi(&valid_addr_bitmap_insn[0]);
1960}
1961
1da177e4
LT
1962void __init mem_init(void)
1963{
1964 unsigned long codepages, datapages, initpages;
1965 unsigned long addr, last;
1da177e4
LT
1966
1967 addr = PAGE_OFFSET + kern_base;
1968 last = PAGE_ALIGN(kern_size) + addr;
1969 while (addr < last) {
1970 set_bit(__pa(addr) >> 22, sparc64_valid_addr_bitmap);
1971 addr += PAGE_SIZE;
1972 }
1973
d8ed1d43
DM
1974 setup_valid_addr_bitmap_from_pavail(sparc64_valid_addr_bitmap);
1975 patch_tlb_miss_handler_bitmap();
1da177e4 1976
1da177e4
LT
1977 high_memory = __va(last_valid_pfn << PAGE_SHIFT);
1978
919ee677 1979#ifdef CONFIG_NEED_MULTIPLE_NODES
d8ed1d43
DM
1980 {
1981 int i;
1982 for_each_online_node(i) {
1983 if (NODE_DATA(i)->node_spanned_pages != 0) {
1984 totalram_pages +=
1985 free_all_bootmem_node(NODE_DATA(i));
1986 }
919ee677 1987 }
625d693e 1988 totalram_pages += free_low_memory_core_early(MAX_NUMNODES);
919ee677
DM
1989 }
1990#else
1991 totalram_pages = free_all_bootmem();
1992#endif
1993
f1cfdb55
DM
1994 /* We subtract one to account for the mem_map_zero page
1995 * allocated below.
1996 */
919ee677
DM
1997 totalram_pages -= 1;
1998 num_physpages = totalram_pages;
1da177e4
LT
1999
2000 /*
2001 * Set up the zero page, mark it reserved, so that page count
2002 * is not manipulated when freeing the page from user ptes.
2003 */
2004 mem_map_zero = alloc_pages(GFP_KERNEL|__GFP_ZERO, 0);
2005 if (mem_map_zero == NULL) {
2006 prom_printf("paging_init: Cannot alloc zero page.\n");
2007 prom_halt();
2008 }
2009 SetPageReserved(mem_map_zero);
2010
2011 codepages = (((unsigned long) _etext) - ((unsigned long) _start));
2012 codepages = PAGE_ALIGN(codepages) >> PAGE_SHIFT;
2013 datapages = (((unsigned long) _edata) - ((unsigned long) _etext));
2014 datapages = PAGE_ALIGN(datapages) >> PAGE_SHIFT;
2015 initpages = (((unsigned long) __init_end) - ((unsigned long) __init_begin));
2016 initpages = PAGE_ALIGN(initpages) >> PAGE_SHIFT;
2017
96177299 2018 printk("Memory: %luk available (%ldk kernel code, %ldk data, %ldk init) [%016lx,%016lx]\n",
1da177e4
LT
2019 nr_free_pages() << (PAGE_SHIFT-10),
2020 codepages << (PAGE_SHIFT-10),
2021 datapages << (PAGE_SHIFT-10),
2022 initpages << (PAGE_SHIFT-10),
2023 PAGE_OFFSET, (last_valid_pfn << PAGE_SHIFT));
2024
2025 if (tlb_type == cheetah || tlb_type == cheetah_plus)
2026 cheetah_ecache_flush_init();
2027}
2028
898cf0ec 2029void free_initmem(void)
1da177e4
LT
2030{
2031 unsigned long addr, initend;
f2b60794
DM
2032 int do_free = 1;
2033
2034 /* If the physical memory maps were trimmed by kernel command
2035 * line options, don't even try freeing this initmem stuff up.
2036 * The kernel image could have been in the trimmed out region
2037 * and if so the freeing below will free invalid page structs.
2038 */
2039 if (cmdline_memory_size)
2040 do_free = 0;
1da177e4
LT
2041
2042 /*
2043 * The init section is aligned to 8k in vmlinux.lds. Page align for >8k pagesizes.
2044 */
2045 addr = PAGE_ALIGN((unsigned long)(__init_begin));
2046 initend = (unsigned long)(__init_end) & PAGE_MASK;
2047 for (; addr < initend; addr += PAGE_SIZE) {
2048 unsigned long page;
2049 struct page *p;
2050
2051 page = (addr +
2052 ((unsigned long) __va(kern_base)) -
2053 ((unsigned long) KERNBASE));
c9cf5528 2054 memset((void *)addr, POISON_FREE_INITMEM, PAGE_SIZE);
1da177e4 2055
f2b60794
DM
2056 if (do_free) {
2057 p = virt_to_page(page);
2058
2059 ClearPageReserved(p);
2060 init_page_count(p);
2061 __free_page(p);
2062 num_physpages++;
2063 totalram_pages++;
2064 }
1da177e4
LT
2065 }
2066}
2067
2068#ifdef CONFIG_BLK_DEV_INITRD
2069void free_initrd_mem(unsigned long start, unsigned long end)
2070{
2071 if (start < end)
2072 printk ("Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
2073 for (; start < end; start += PAGE_SIZE) {
2074 struct page *p = virt_to_page(start);
2075
2076 ClearPageReserved(p);
7835e98b 2077 init_page_count(p);
1da177e4
LT
2078 __free_page(p);
2079 num_physpages++;
2080 totalram_pages++;
2081 }
2082}
2083#endif
c4bce90e 2084
c4bce90e
DM
2085#define _PAGE_CACHE_4U (_PAGE_CP_4U | _PAGE_CV_4U)
2086#define _PAGE_CACHE_4V (_PAGE_CP_4V | _PAGE_CV_4V)
2087#define __DIRTY_BITS_4U (_PAGE_MODIFIED_4U | _PAGE_WRITE_4U | _PAGE_W_4U)
2088#define __DIRTY_BITS_4V (_PAGE_MODIFIED_4V | _PAGE_WRITE_4V | _PAGE_W_4V)
2089#define __ACCESS_BITS_4U (_PAGE_ACCESSED_4U | _PAGE_READ_4U | _PAGE_R)
2090#define __ACCESS_BITS_4V (_PAGE_ACCESSED_4V | _PAGE_READ_4V | _PAGE_R)
2091
2092pgprot_t PAGE_KERNEL __read_mostly;
2093EXPORT_SYMBOL(PAGE_KERNEL);
2094
2095pgprot_t PAGE_KERNEL_LOCKED __read_mostly;
2096pgprot_t PAGE_COPY __read_mostly;
0f15952a
DM
2097
2098pgprot_t PAGE_SHARED __read_mostly;
2099EXPORT_SYMBOL(PAGE_SHARED);
2100
c4bce90e
DM
2101unsigned long pg_iobits __read_mostly;
2102
2103unsigned long _PAGE_IE __read_mostly;
987c74fc 2104EXPORT_SYMBOL(_PAGE_IE);
b2bef442 2105
c4bce90e 2106unsigned long _PAGE_E __read_mostly;
b2bef442
DM
2107EXPORT_SYMBOL(_PAGE_E);
2108
c4bce90e 2109unsigned long _PAGE_CACHE __read_mostly;
b2bef442 2110EXPORT_SYMBOL(_PAGE_CACHE);
c4bce90e 2111
46644c24 2112#ifdef CONFIG_SPARSEMEM_VMEMMAP
46644c24
DM
2113unsigned long vmemmap_table[VMEMMAP_SIZE];
2114
2856cc2e
DM
2115static long __meminitdata addr_start, addr_end;
2116static int __meminitdata node_start;
2117
46644c24
DM
2118int __meminit vmemmap_populate(struct page *start, unsigned long nr, int node)
2119{
2120 unsigned long vstart = (unsigned long) start;
2121 unsigned long vend = (unsigned long) (start + nr);
2122 unsigned long phys_start = (vstart - VMEMMAP_BASE);
2123 unsigned long phys_end = (vend - VMEMMAP_BASE);
2124 unsigned long addr = phys_start & VMEMMAP_CHUNK_MASK;
2125 unsigned long end = VMEMMAP_ALIGN(phys_end);
2126 unsigned long pte_base;
2127
2128 pte_base = (_PAGE_VALID | _PAGE_SZ4MB_4U |
2129 _PAGE_CP_4U | _PAGE_CV_4U |
2130 _PAGE_P_4U | _PAGE_W_4U);
2131 if (tlb_type == hypervisor)
2132 pte_base = (_PAGE_VALID | _PAGE_SZ4MB_4V |
2133 _PAGE_CP_4V | _PAGE_CV_4V |
2134 _PAGE_P_4V | _PAGE_W_4V);
2135
2136 for (; addr < end; addr += VMEMMAP_CHUNK) {
2137 unsigned long *vmem_pp =
2138 vmemmap_table + (addr >> VMEMMAP_CHUNK_SHIFT);
2139 void *block;
2140
2141 if (!(*vmem_pp & _PAGE_VALID)) {
2142 block = vmemmap_alloc_block(1UL << 22, node);
2143 if (!block)
2144 return -ENOMEM;
2145
2146 *vmem_pp = pte_base | __pa(block);
2147
2856cc2e
DM
2148 /* check to see if we have contiguous blocks */
2149 if (addr_end != addr || node_start != node) {
2150 if (addr_start)
2151 printk(KERN_DEBUG " [%lx-%lx] on node %d\n",
2152 addr_start, addr_end-1, node_start);
2153 addr_start = addr;
2154 node_start = node;
2155 }
2156 addr_end = addr + VMEMMAP_CHUNK;
46644c24
DM
2157 }
2158 }
2159 return 0;
2160}
2856cc2e
DM
2161
2162void __meminit vmemmap_populate_print_last(void)
2163{
2164 if (addr_start) {
2165 printk(KERN_DEBUG " [%lx-%lx] on node %d\n",
2166 addr_start, addr_end-1, node_start);
2167 addr_start = 0;
2168 addr_end = 0;
2169 node_start = 0;
2170 }
2171}
46644c24
DM
2172#endif /* CONFIG_SPARSEMEM_VMEMMAP */
2173
c4bce90e
DM
2174static void prot_init_common(unsigned long page_none,
2175 unsigned long page_shared,
2176 unsigned long page_copy,
2177 unsigned long page_readonly,
2178 unsigned long page_exec_bit)
2179{
2180 PAGE_COPY = __pgprot(page_copy);
0f15952a 2181 PAGE_SHARED = __pgprot(page_shared);
c4bce90e
DM
2182
2183 protection_map[0x0] = __pgprot(page_none);
2184 protection_map[0x1] = __pgprot(page_readonly & ~page_exec_bit);
2185 protection_map[0x2] = __pgprot(page_copy & ~page_exec_bit);
2186 protection_map[0x3] = __pgprot(page_copy & ~page_exec_bit);
2187 protection_map[0x4] = __pgprot(page_readonly);
2188 protection_map[0x5] = __pgprot(page_readonly);
2189 protection_map[0x6] = __pgprot(page_copy);
2190 protection_map[0x7] = __pgprot(page_copy);
2191 protection_map[0x8] = __pgprot(page_none);
2192 protection_map[0x9] = __pgprot(page_readonly & ~page_exec_bit);
2193 protection_map[0xa] = __pgprot(page_shared & ~page_exec_bit);
2194 protection_map[0xb] = __pgprot(page_shared & ~page_exec_bit);
2195 protection_map[0xc] = __pgprot(page_readonly);
2196 protection_map[0xd] = __pgprot(page_readonly);
2197 protection_map[0xe] = __pgprot(page_shared);
2198 protection_map[0xf] = __pgprot(page_shared);
2199}
2200
2201static void __init sun4u_pgprot_init(void)
2202{
2203 unsigned long page_none, page_shared, page_copy, page_readonly;
2204 unsigned long page_exec_bit;
4f93d21d 2205 int i;
c4bce90e
DM
2206
2207 PAGE_KERNEL = __pgprot (_PAGE_PRESENT_4U | _PAGE_VALID |
2208 _PAGE_CACHE_4U | _PAGE_P_4U |
2209 __ACCESS_BITS_4U | __DIRTY_BITS_4U |
2210 _PAGE_EXEC_4U);
2211 PAGE_KERNEL_LOCKED = __pgprot (_PAGE_PRESENT_4U | _PAGE_VALID |
2212 _PAGE_CACHE_4U | _PAGE_P_4U |
2213 __ACCESS_BITS_4U | __DIRTY_BITS_4U |
2214 _PAGE_EXEC_4U | _PAGE_L_4U);
c4bce90e
DM
2215
2216 _PAGE_IE = _PAGE_IE_4U;
2217 _PAGE_E = _PAGE_E_4U;
2218 _PAGE_CACHE = _PAGE_CACHE_4U;
2219
2220 pg_iobits = (_PAGE_VALID | _PAGE_PRESENT_4U | __DIRTY_BITS_4U |
2221 __ACCESS_BITS_4U | _PAGE_E_4U);
2222
d1acb421
DM
2223#ifdef CONFIG_DEBUG_PAGEALLOC
2224 kern_linear_pte_xor[0] = (_PAGE_VALID | _PAGE_SZBITS_4U) ^
af1ee569 2225 0xfffff80000000000UL;
d1acb421 2226#else
9cc3a1ac 2227 kern_linear_pte_xor[0] = (_PAGE_VALID | _PAGE_SZ4MB_4U) ^
af1ee569 2228 0xfffff80000000000UL;
d1acb421 2229#endif
9cc3a1ac
DM
2230 kern_linear_pte_xor[0] |= (_PAGE_CP_4U | _PAGE_CV_4U |
2231 _PAGE_P_4U | _PAGE_W_4U);
2232
2233 /* XXX Should use 256MB on Panther. XXX */
4f93d21d
DM
2234 for (i = 1; i < 4; i++)
2235 kern_linear_pte_xor[i] = kern_linear_pte_xor[0];
c4bce90e
DM
2236
2237 _PAGE_SZBITS = _PAGE_SZBITS_4U;
2238 _PAGE_ALL_SZ_BITS = (_PAGE_SZ4MB_4U | _PAGE_SZ512K_4U |
2239 _PAGE_SZ64K_4U | _PAGE_SZ8K_4U |
2240 _PAGE_SZ32MB_4U | _PAGE_SZ256MB_4U);
2241
2242
2243 page_none = _PAGE_PRESENT_4U | _PAGE_ACCESSED_4U | _PAGE_CACHE_4U;
2244 page_shared = (_PAGE_VALID | _PAGE_PRESENT_4U | _PAGE_CACHE_4U |
2245 __ACCESS_BITS_4U | _PAGE_WRITE_4U | _PAGE_EXEC_4U);
2246 page_copy = (_PAGE_VALID | _PAGE_PRESENT_4U | _PAGE_CACHE_4U |
2247 __ACCESS_BITS_4U | _PAGE_EXEC_4U);
2248 page_readonly = (_PAGE_VALID | _PAGE_PRESENT_4U | _PAGE_CACHE_4U |
2249 __ACCESS_BITS_4U | _PAGE_EXEC_4U);
2250
2251 page_exec_bit = _PAGE_EXEC_4U;
2252
2253 prot_init_common(page_none, page_shared, page_copy, page_readonly,
2254 page_exec_bit);
2255}
2256
2257static void __init sun4v_pgprot_init(void)
2258{
2259 unsigned long page_none, page_shared, page_copy, page_readonly;
2260 unsigned long page_exec_bit;
4f93d21d 2261 int i;
c4bce90e
DM
2262
2263 PAGE_KERNEL = __pgprot (_PAGE_PRESENT_4V | _PAGE_VALID |
2264 _PAGE_CACHE_4V | _PAGE_P_4V |
2265 __ACCESS_BITS_4V | __DIRTY_BITS_4V |
2266 _PAGE_EXEC_4V);
2267 PAGE_KERNEL_LOCKED = PAGE_KERNEL;
c4bce90e
DM
2268
2269 _PAGE_IE = _PAGE_IE_4V;
2270 _PAGE_E = _PAGE_E_4V;
2271 _PAGE_CACHE = _PAGE_CACHE_4V;
2272
d1acb421
DM
2273#ifdef CONFIG_DEBUG_PAGEALLOC
2274 kern_linear_pte_xor[0] = (_PAGE_VALID | _PAGE_SZBITS_4V) ^
af1ee569 2275 0xfffff80000000000UL;
d1acb421 2276#else
9cc3a1ac 2277 kern_linear_pte_xor[0] = (_PAGE_VALID | _PAGE_SZ4MB_4V) ^
af1ee569 2278 0xfffff80000000000UL;
d1acb421 2279#endif
9cc3a1ac
DM
2280 kern_linear_pte_xor[0] |= (_PAGE_CP_4V | _PAGE_CV_4V |
2281 _PAGE_P_4V | _PAGE_W_4V);
2282
d1acb421
DM
2283#ifdef CONFIG_DEBUG_PAGEALLOC
2284 kern_linear_pte_xor[1] = (_PAGE_VALID | _PAGE_SZBITS_4V) ^
af1ee569 2285 0xfffff80000000000UL;
d1acb421 2286#else
9cc3a1ac 2287 kern_linear_pte_xor[1] = (_PAGE_VALID | _PAGE_SZ256MB_4V) ^
af1ee569 2288 0xfffff80000000000UL;
d1acb421 2289#endif
9cc3a1ac
DM
2290 kern_linear_pte_xor[1] |= (_PAGE_CP_4V | _PAGE_CV_4V |
2291 _PAGE_P_4V | _PAGE_W_4V);
c4bce90e 2292
4f93d21d
DM
2293 i = 2;
2294
2295 if (sun4v_chip_type == SUN4V_CHIP_NIAGARA4) {
2296#ifdef CONFIG_DEBUG_PAGEALLOC
2297 kern_linear_pte_xor[2] = (_PAGE_VALID | _PAGE_SZBITS_4V) ^
2298 0xfffff80000000000UL;
2299#else
2300 kern_linear_pte_xor[2] = (_PAGE_VALID | _PAGE_SZ2GB_4V) ^
2301 0xfffff80000000000UL;
2302#endif
2303 kern_linear_pte_xor[2] |= (_PAGE_CP_4V | _PAGE_CV_4V |
2304 _PAGE_P_4V | _PAGE_W_4V);
2305
2306 i = 3;
2307 }
2308
2309 for (; i < 4; i++)
2310 kern_linear_pte_xor[i] = kern_linear_pte_xor[i - 1];
2311
c4bce90e
DM
2312 pg_iobits = (_PAGE_VALID | _PAGE_PRESENT_4V | __DIRTY_BITS_4V |
2313 __ACCESS_BITS_4V | _PAGE_E_4V);
2314
2315 _PAGE_SZBITS = _PAGE_SZBITS_4V;
2316 _PAGE_ALL_SZ_BITS = (_PAGE_SZ16GB_4V | _PAGE_SZ2GB_4V |
2317 _PAGE_SZ256MB_4V | _PAGE_SZ32MB_4V |
2318 _PAGE_SZ4MB_4V | _PAGE_SZ512K_4V |
2319 _PAGE_SZ64K_4V | _PAGE_SZ8K_4V);
2320
2321 page_none = _PAGE_PRESENT_4V | _PAGE_ACCESSED_4V | _PAGE_CACHE_4V;
2322 page_shared = (_PAGE_VALID | _PAGE_PRESENT_4V | _PAGE_CACHE_4V |
2323 __ACCESS_BITS_4V | _PAGE_WRITE_4V | _PAGE_EXEC_4V);
2324 page_copy = (_PAGE_VALID | _PAGE_PRESENT_4V | _PAGE_CACHE_4V |
2325 __ACCESS_BITS_4V | _PAGE_EXEC_4V);
2326 page_readonly = (_PAGE_VALID | _PAGE_PRESENT_4V | _PAGE_CACHE_4V |
2327 __ACCESS_BITS_4V | _PAGE_EXEC_4V);
2328
2329 page_exec_bit = _PAGE_EXEC_4V;
2330
2331 prot_init_common(page_none, page_shared, page_copy, page_readonly,
2332 page_exec_bit);
2333}
2334
2335unsigned long pte_sz_bits(unsigned long sz)
2336{
2337 if (tlb_type == hypervisor) {
2338 switch (sz) {
2339 case 8 * 1024:
2340 default:
2341 return _PAGE_SZ8K_4V;
2342 case 64 * 1024:
2343 return _PAGE_SZ64K_4V;
2344 case 512 * 1024:
2345 return _PAGE_SZ512K_4V;
2346 case 4 * 1024 * 1024:
2347 return _PAGE_SZ4MB_4V;
6cb79b3f 2348 }
c4bce90e
DM
2349 } else {
2350 switch (sz) {
2351 case 8 * 1024:
2352 default:
2353 return _PAGE_SZ8K_4U;
2354 case 64 * 1024:
2355 return _PAGE_SZ64K_4U;
2356 case 512 * 1024:
2357 return _PAGE_SZ512K_4U;
2358 case 4 * 1024 * 1024:
2359 return _PAGE_SZ4MB_4U;
6cb79b3f 2360 }
c4bce90e
DM
2361 }
2362}
2363
2364pte_t mk_pte_io(unsigned long page, pgprot_t prot, int space, unsigned long page_size)
2365{
2366 pte_t pte;
cf627156
DM
2367
2368 pte_val(pte) = page | pgprot_val(pgprot_noncached(prot));
c4bce90e
DM
2369 pte_val(pte) |= (((unsigned long)space) << 32);
2370 pte_val(pte) |= pte_sz_bits(page_size);
c4bce90e 2371
cf627156 2372 return pte;
c4bce90e
DM
2373}
2374
2375static unsigned long kern_large_tte(unsigned long paddr)
2376{
2377 unsigned long val;
2378
2379 val = (_PAGE_VALID | _PAGE_SZ4MB_4U |
2380 _PAGE_CP_4U | _PAGE_CV_4U | _PAGE_P_4U |
2381 _PAGE_EXEC_4U | _PAGE_L_4U | _PAGE_W_4U);
2382 if (tlb_type == hypervisor)
2383 val = (_PAGE_VALID | _PAGE_SZ4MB_4V |
2384 _PAGE_CP_4V | _PAGE_CV_4V | _PAGE_P_4V |
2385 _PAGE_EXEC_4V | _PAGE_W_4V);
2386
2387 return val | paddr;
2388}
2389
c4bce90e
DM
2390/* If not locked, zap it. */
2391void __flush_tlb_all(void)
2392{
2393 unsigned long pstate;
2394 int i;
2395
2396 __asm__ __volatile__("flushw\n\t"
2397 "rdpr %%pstate, %0\n\t"
2398 "wrpr %0, %1, %%pstate"
2399 : "=r" (pstate)
2400 : "i" (PSTATE_IE));
8f361453
DM
2401 if (tlb_type == hypervisor) {
2402 sun4v_mmu_demap_all();
2403 } else if (tlb_type == spitfire) {
c4bce90e
DM
2404 for (i = 0; i < 64; i++) {
2405 /* Spitfire Errata #32 workaround */
2406 /* NOTE: Always runs on spitfire, so no
2407 * cheetah+ page size encodings.
2408 */
2409 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
2410 "flush %%g6"
2411 : /* No outputs */
2412 : "r" (0),
2413 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
2414
2415 if (!(spitfire_get_dtlb_data(i) & _PAGE_L_4U)) {
2416 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
2417 "membar #Sync"
2418 : /* no outputs */
2419 : "r" (TLB_TAG_ACCESS), "i" (ASI_DMMU));
2420 spitfire_put_dtlb_data(i, 0x0UL);
2421 }
2422
2423 /* Spitfire Errata #32 workaround */
2424 /* NOTE: Always runs on spitfire, so no
2425 * cheetah+ page size encodings.
2426 */
2427 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
2428 "flush %%g6"
2429 : /* No outputs */
2430 : "r" (0),
2431 "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
2432
2433 if (!(spitfire_get_itlb_data(i) & _PAGE_L_4U)) {
2434 __asm__ __volatile__("stxa %%g0, [%0] %1\n\t"
2435 "membar #Sync"
2436 : /* no outputs */
2437 : "r" (TLB_TAG_ACCESS), "i" (ASI_IMMU));
2438 spitfire_put_itlb_data(i, 0x0UL);
2439 }
2440 }
2441 } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
2442 cheetah_flush_dtlb_all();
2443 cheetah_flush_itlb_all();
2444 }
2445 __asm__ __volatile__("wrpr %0, 0, %%pstate"
2446 : : "r" (pstate));
2447}