1 /* $Id: init.c,v 1.209 2002/02/09 19:49:31 davem Exp $
2 * arch/sparc64/mm/init.c
4 * Copyright (C) 1996-1999 David S. Miller (davem@caip.rutgers.edu)
5 * Copyright (C) 1997-1999 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
8 #include <linux/config.h>
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>
15 #include <linux/hugetlb.h>
16 #include <linux/slab.h>
17 #include <linux/initrd.h>
18 #include <linux/swap.h>
19 #include <linux/pagemap.h>
21 #include <linux/seq_file.h>
24 #include <asm/system.h>
26 #include <asm/pgalloc.h>
27 #include <asm/pgtable.h>
28 #include <asm/oplib.h>
29 #include <asm/iommu.h>
31 #include <asm/uaccess.h>
32 #include <asm/mmu_context.h>
33 #include <asm/tlbflush.h>
35 #include <asm/starfire.h>
37 #include <asm/spitfire.h>
38 #include <asm/sections.h>
40 extern void device_scan(void);
42 struct sparc_phys_banks sp_banks
[SPARC_PHYS_BANKS
];
44 unsigned long *sparc64_valid_addr_bitmap
;
46 /* Ugly, but necessary... -DaveM */
47 unsigned long phys_base
;
48 unsigned long kern_base
;
49 unsigned long kern_size
;
50 unsigned long pfn_base
;
52 /* This is even uglier. We have a problem where the kernel may not be
53 * located at phys_base. However, initial __alloc_bootmem() calls need to
54 * be adjusted to be within the 4-8Megs that the kernel is mapped to, else
55 * those page mappings wont work. Things are ok after inherit_prom_mappings
56 * is called though. Dave says he'll clean this up some other time.
59 static unsigned long bootmap_base
;
61 /* get_new_mmu_context() uses "cache + 1". */
62 DEFINE_SPINLOCK(ctx_alloc_lock
);
63 unsigned long tlb_context_cache
= CTX_FIRST_VERSION
- 1;
64 #define CTX_BMAP_SLOTS (1UL << (CTX_NR_BITS - 6))
65 unsigned long mmu_context_bmap
[CTX_BMAP_SLOTS
];
67 /* References to special section boundaries */
68 extern char _start
[], _end
[];
70 /* Initial ramdisk setup */
71 extern unsigned long sparc_ramdisk_image64
;
72 extern unsigned int sparc_ramdisk_image
;
73 extern unsigned int sparc_ramdisk_size
;
75 struct page
*mem_map_zero
;
79 /* XXX Tune this... */
80 #define PGT_CACHE_LOW 25
81 #define PGT_CACHE_HIGH 50
83 void check_pgt_cache(void)
86 if (pgtable_cache_size
> PGT_CACHE_HIGH
) {
89 free_pgd_slow(get_pgd_fast());
91 free_pte_slow(pte_alloc_one_fast(NULL
, 0));
93 free_pte_slow(pte_alloc_one_fast(NULL
, 1 << (PAGE_SHIFT
+ 10)));
94 } while (pgtable_cache_size
> PGT_CACHE_LOW
);
99 #ifdef CONFIG_DEBUG_DCFLUSH
100 atomic_t dcpage_flushes
= ATOMIC_INIT(0);
102 atomic_t dcpage_flushes_xcall
= ATOMIC_INIT(0);
106 __inline__
void flush_dcache_page_impl(struct page
*page
)
108 #ifdef CONFIG_DEBUG_DCFLUSH
109 atomic_inc(&dcpage_flushes
);
112 #ifdef DCACHE_ALIASING_POSSIBLE
113 __flush_dcache_page(page_address(page
),
114 ((tlb_type
== spitfire
) &&
115 page_mapping(page
) != NULL
));
117 if (page_mapping(page
) != NULL
&&
118 tlb_type
== spitfire
)
119 __flush_icache_page(__pa(page_address(page
)));
123 #define PG_dcache_dirty PG_arch_1
125 #define dcache_dirty_cpu(page) \
126 (((page)->flags >> 24) & (NR_CPUS - 1UL))
128 static __inline__
void set_dcache_dirty(struct page
*page
, int this_cpu
)
130 unsigned long mask
= this_cpu
;
131 unsigned long non_cpu_bits
= ~((NR_CPUS
- 1UL) << 24UL);
132 mask
= (mask
<< 24) | (1UL << PG_dcache_dirty
);
133 __asm__
__volatile__("1:\n\t"
135 "and %%g7, %1, %%g1\n\t"
136 "or %%g1, %0, %%g1\n\t"
137 "casx [%2], %%g7, %%g1\n\t"
139 "bne,pn %%xcc, 1b\n\t"
140 " membar #StoreLoad | #StoreStore"
142 : "r" (mask
), "r" (non_cpu_bits
), "r" (&page
->flags
)
146 static __inline__
void clear_dcache_dirty_cpu(struct page
*page
, unsigned long cpu
)
148 unsigned long mask
= (1UL << PG_dcache_dirty
);
150 __asm__
__volatile__("! test_and_clear_dcache_dirty\n"
153 "srlx %%g7, 24, %%g1\n\t"
154 "and %%g1, %3, %%g1\n\t"
156 "bne,pn %%icc, 2f\n\t"
157 " andn %%g7, %1, %%g1\n\t"
158 "casx [%2], %%g7, %%g1\n\t"
160 "bne,pn %%xcc, 1b\n\t"
161 " membar #StoreLoad | #StoreStore\n"
164 : "r" (cpu
), "r" (mask
), "r" (&page
->flags
),
169 extern void __update_mmu_cache(unsigned long mmu_context_hw
, unsigned long address
, pte_t pte
, int code
);
171 void update_mmu_cache(struct vm_area_struct
*vma
, unsigned long address
, pte_t pte
)
175 unsigned long pg_flags
;
178 if (pfn_valid(pfn
) &&
179 (page
= pfn_to_page(pfn
), page_mapping(page
)) &&
180 ((pg_flags
= page
->flags
) & (1UL << PG_dcache_dirty
))) {
181 int cpu
= ((pg_flags
>> 24) & (NR_CPUS
- 1UL));
182 int this_cpu
= get_cpu();
184 /* This is just to optimize away some function calls
188 flush_dcache_page_impl(page
);
190 smp_flush_dcache_page_impl(page
, cpu
);
192 clear_dcache_dirty_cpu(page
, cpu
);
197 if (get_thread_fault_code())
198 __update_mmu_cache(CTX_NRBITS(vma
->vm_mm
->context
),
199 address
, pte
, get_thread_fault_code());
202 void flush_dcache_page(struct page
*page
)
204 struct address_space
*mapping
;
207 /* Do not bother with the expensive D-cache flush if it
208 * is merely the zero page. The 'bigcore' testcase in GDB
209 * causes this case to run millions of times.
211 if (page
== ZERO_PAGE(0))
214 this_cpu
= get_cpu();
216 mapping
= page_mapping(page
);
217 if (mapping
&& !mapping_mapped(mapping
)) {
218 int dirty
= test_bit(PG_dcache_dirty
, &page
->flags
);
220 int dirty_cpu
= dcache_dirty_cpu(page
);
222 if (dirty_cpu
== this_cpu
)
224 smp_flush_dcache_page_impl(page
, dirty_cpu
);
226 set_dcache_dirty(page
, this_cpu
);
228 /* We could delay the flush for the !page_mapping
229 * case too. But that case is for exec env/arg
230 * pages and those are %99 certainly going to get
231 * faulted into the tlb (and thus flushed) anyways.
233 flush_dcache_page_impl(page
);
240 void flush_icache_range(unsigned long start
, unsigned long end
)
242 /* Cheetah has coherent I-cache. */
243 if (tlb_type
== spitfire
) {
246 for (kaddr
= start
; kaddr
< end
; kaddr
+= PAGE_SIZE
)
247 __flush_icache_page(__get_phys(kaddr
));
251 unsigned long page_to_pfn(struct page
*page
)
253 return (unsigned long) ((page
- mem_map
) + pfn_base
);
256 struct page
*pfn_to_page(unsigned long pfn
)
258 return (mem_map
+ (pfn
- pfn_base
));
263 printk("Mem-info:\n");
265 printk("Free swap: %6ldkB\n",
266 nr_swap_pages
<< (PAGE_SHIFT
-10));
267 printk("%ld pages of RAM\n", num_physpages
);
268 printk("%d free pages\n", nr_free_pages());
269 printk("%d pages in page table cache\n",pgtable_cache_size
);
272 void mmu_info(struct seq_file
*m
)
274 if (tlb_type
== cheetah
)
275 seq_printf(m
, "MMU Type\t: Cheetah\n");
276 else if (tlb_type
== cheetah_plus
)
277 seq_printf(m
, "MMU Type\t: Cheetah+\n");
278 else if (tlb_type
== spitfire
)
279 seq_printf(m
, "MMU Type\t: Spitfire\n");
281 seq_printf(m
, "MMU Type\t: ???\n");
283 #ifdef CONFIG_DEBUG_DCFLUSH
284 seq_printf(m
, "DCPageFlushes\t: %d\n",
285 atomic_read(&dcpage_flushes
));
287 seq_printf(m
, "DCPageFlushesXC\t: %d\n",
288 atomic_read(&dcpage_flushes_xcall
));
289 #endif /* CONFIG_SMP */
290 #endif /* CONFIG_DEBUG_DCFLUSH */
293 struct linux_prom_translation
{
299 extern unsigned long prom_boot_page
;
300 extern void prom_remap(unsigned long physpage
, unsigned long virtpage
, int mmu_ihandle
);
301 extern int prom_get_mmu_ihandle(void);
302 extern void register_prom_callbacks(void);
304 /* Exported for SMP bootup purposes. */
305 unsigned long kern_locked_tte_data
;
307 void __init
early_pgtable_allocfail(char *type
)
309 prom_printf("inherit_prom_mappings: Cannot alloc kernel %s.\n", type
);
313 #define BASE_PAGE_SIZE 8192
314 static pmd_t
*prompmd
;
317 * Translate PROM's mapping we capture at boot time into physical address.
318 * The second parameter is only set from prom_callback() invocations.
320 unsigned long prom_virt_to_phys(unsigned long promva
, int *error
)
322 pmd_t
*pmdp
= prompmd
+ ((promva
>> 23) & 0x7ff);
326 if (pmd_none(*pmdp
)) {
331 ptep
= (pte_t
*)__pmd_page(*pmdp
) + ((promva
>> 13) & 0x3ff);
332 if (!pte_present(*ptep
)) {
339 return(pte_val(*ptep
));
341 base
= pte_val(*ptep
) & _PAGE_PADDR
;
342 return(base
+ (promva
& (BASE_PAGE_SIZE
- 1)));
345 static void inherit_prom_mappings(void)
347 struct linux_prom_translation
*trans
;
348 unsigned long phys_page
, tte_vaddr
, tte_data
;
349 void (*remap_func
)(unsigned long, unsigned long, int);
353 extern unsigned int obp_iaddr_patch
[2], obp_daddr_patch
[2];
355 node
= prom_finddevice("/virtual-memory");
356 n
= prom_getproplen(node
, "translations");
357 if (n
== 0 || n
== -1) {
358 prom_printf("Couldn't get translation property\n");
361 n
+= 5 * sizeof(struct linux_prom_translation
);
362 for (tsz
= 1; tsz
< n
; tsz
<<= 1)
364 trans
= __alloc_bootmem(tsz
, SMP_CACHE_BYTES
, bootmap_base
);
366 prom_printf("inherit_prom_mappings: Cannot alloc translations.\n");
369 memset(trans
, 0, tsz
);
371 if ((n
= prom_getproperty(node
, "translations", (char *)trans
, tsz
)) == -1) {
372 prom_printf("Couldn't get translation property\n");
375 n
= n
/ sizeof(*trans
);
378 * The obp translations are saved based on 8k pagesize, since obp can
379 * use a mixture of pagesizes. Misses to the 0xf0000000 - 0x100000000,
380 * ie obp range, are handled in entry.S and do not use the vpte scheme
381 * (see rant in inherit_locked_prom_mappings()).
383 #define OBP_PMD_SIZE 2048
384 prompmd
= __alloc_bootmem(OBP_PMD_SIZE
, OBP_PMD_SIZE
, bootmap_base
);
386 early_pgtable_allocfail("pmd");
387 memset(prompmd
, 0, OBP_PMD_SIZE
);
388 for (i
= 0; i
< n
; i
++) {
391 if (trans
[i
].virt
>= LOW_OBP_ADDRESS
&& trans
[i
].virt
< HI_OBP_ADDRESS
) {
392 for (vaddr
= trans
[i
].virt
;
393 ((vaddr
< trans
[i
].virt
+ trans
[i
].size
) &&
394 (vaddr
< HI_OBP_ADDRESS
));
395 vaddr
+= BASE_PAGE_SIZE
) {
398 pmdp
= prompmd
+ ((vaddr
>> 23) & 0x7ff);
399 if (pmd_none(*pmdp
)) {
400 ptep
= __alloc_bootmem(BASE_PAGE_SIZE
,
404 early_pgtable_allocfail("pte");
405 memset(ptep
, 0, BASE_PAGE_SIZE
);
408 ptep
= (pte_t
*)__pmd_page(*pmdp
) +
409 ((vaddr
>> 13) & 0x3ff);
413 /* Clear diag TTE bits. */
414 if (tlb_type
== spitfire
)
415 val
&= ~0x0003fe0000000000UL
;
417 set_pte_at(&init_mm
, vaddr
,
418 ptep
, __pte(val
| _PAGE_MODIFIED
));
419 trans
[i
].data
+= BASE_PAGE_SIZE
;
423 phys_page
= __pa(prompmd
);
424 obp_iaddr_patch
[0] |= (phys_page
>> 10);
425 obp_iaddr_patch
[1] |= (phys_page
& 0x3ff);
426 flushi((long)&obp_iaddr_patch
[0]);
427 obp_daddr_patch
[0] |= (phys_page
>> 10);
428 obp_daddr_patch
[1] |= (phys_page
& 0x3ff);
429 flushi((long)&obp_daddr_patch
[0]);
431 /* Now fixup OBP's idea about where we really are mapped. */
432 prom_printf("Remapping the kernel... ");
434 /* Spitfire Errata #32 workaround */
435 /* NOTE: Using plain zero for the context value is
436 * correct here, we are not using the Linux trap
437 * tables yet so we should not use the special
438 * UltraSPARC-III+ page size encodings yet.
440 __asm__
__volatile__("stxa %0, [%1] %2\n\t"
443 : "r" (0), "r" (PRIMARY_CONTEXT
), "i" (ASI_DMMU
));
448 phys_page
= spitfire_get_dtlb_data(sparc64_highest_locked_tlbent());
453 phys_page
= cheetah_get_litlb_data(sparc64_highest_locked_tlbent());
457 phys_page
&= _PAGE_PADDR
;
458 phys_page
+= ((unsigned long)&prom_boot_page
-
459 (unsigned long)KERNBASE
);
461 if (tlb_type
== spitfire
) {
462 /* Lock this into i/d tlb entry 59 */
463 __asm__
__volatile__(
464 "stxa %%g0, [%2] %3\n\t"
465 "stxa %0, [%1] %4\n\t"
468 "stxa %%g0, [%2] %5\n\t"
469 "stxa %0, [%1] %6\n\t"
472 : : "r" (phys_page
| _PAGE_VALID
| _PAGE_SZ8K
| _PAGE_CP
|
473 _PAGE_CV
| _PAGE_P
| _PAGE_L
| _PAGE_W
),
474 "r" (59 << 3), "r" (TLB_TAG_ACCESS
),
475 "i" (ASI_DMMU
), "i" (ASI_DTLB_DATA_ACCESS
),
476 "i" (ASI_IMMU
), "i" (ASI_ITLB_DATA_ACCESS
)
478 } else if (tlb_type
== cheetah
|| tlb_type
== cheetah_plus
) {
479 /* Lock this into i/d tlb-0 entry 11 */
480 __asm__
__volatile__(
481 "stxa %%g0, [%2] %3\n\t"
482 "stxa %0, [%1] %4\n\t"
485 "stxa %%g0, [%2] %5\n\t"
486 "stxa %0, [%1] %6\n\t"
489 : : "r" (phys_page
| _PAGE_VALID
| _PAGE_SZ8K
| _PAGE_CP
|
490 _PAGE_CV
| _PAGE_P
| _PAGE_L
| _PAGE_W
),
491 "r" ((0 << 16) | (11 << 3)), "r" (TLB_TAG_ACCESS
),
492 "i" (ASI_DMMU
), "i" (ASI_DTLB_DATA_ACCESS
),
493 "i" (ASI_IMMU
), "i" (ASI_ITLB_DATA_ACCESS
)
496 /* Implement me :-) */
500 tte_vaddr
= (unsigned long) KERNBASE
;
502 /* Spitfire Errata #32 workaround */
503 /* NOTE: Using plain zero for the context value is
504 * correct here, we are not using the Linux trap
505 * tables yet so we should not use the special
506 * UltraSPARC-III+ page size encodings yet.
508 __asm__
__volatile__("stxa %0, [%1] %2\n\t"
512 "r" (PRIMARY_CONTEXT
), "i" (ASI_DMMU
));
514 if (tlb_type
== spitfire
)
515 tte_data
= spitfire_get_dtlb_data(sparc64_highest_locked_tlbent());
517 tte_data
= cheetah_get_ldtlb_data(sparc64_highest_locked_tlbent());
519 kern_locked_tte_data
= tte_data
;
521 remap_func
= (void *) ((unsigned long) &prom_remap
-
522 (unsigned long) &prom_boot_page
);
525 /* Spitfire Errata #32 workaround */
526 /* NOTE: Using plain zero for the context value is
527 * correct here, we are not using the Linux trap
528 * tables yet so we should not use the special
529 * UltraSPARC-III+ page size encodings yet.
531 __asm__
__volatile__("stxa %0, [%1] %2\n\t"
535 "r" (PRIMARY_CONTEXT
), "i" (ASI_DMMU
));
537 remap_func((tlb_type
== spitfire
?
538 (spitfire_get_dtlb_data(sparc64_highest_locked_tlbent()) & _PAGE_PADDR
) :
539 (cheetah_get_litlb_data(sparc64_highest_locked_tlbent()) & _PAGE_PADDR
)),
540 (unsigned long) KERNBASE
,
541 prom_get_mmu_ihandle());
544 remap_func(((tte_data
+ 0x400000) & _PAGE_PADDR
),
545 (unsigned long) KERNBASE
+ 0x400000, prom_get_mmu_ihandle());
547 /* Flush out that temporary mapping. */
548 spitfire_flush_dtlb_nucleus_page(0x0);
549 spitfire_flush_itlb_nucleus_page(0x0);
551 /* Now lock us back into the TLBs via OBP. */
552 prom_dtlb_load(sparc64_highest_locked_tlbent(), tte_data
, tte_vaddr
);
553 prom_itlb_load(sparc64_highest_locked_tlbent(), tte_data
, tte_vaddr
);
555 prom_dtlb_load(sparc64_highest_locked_tlbent()-1, tte_data
+ 0x400000,
556 tte_vaddr
+ 0x400000);
557 prom_itlb_load(sparc64_highest_locked_tlbent()-1, tte_data
+ 0x400000,
558 tte_vaddr
+ 0x400000);
561 /* Re-read translations property. */
562 if ((n
= prom_getproperty(node
, "translations", (char *)trans
, tsz
)) == -1) {
563 prom_printf("Couldn't get translation property\n");
566 n
= n
/ sizeof(*trans
);
568 for (i
= 0; i
< n
; i
++) {
569 unsigned long vaddr
= trans
[i
].virt
;
570 unsigned long size
= trans
[i
].size
;
572 if (vaddr
< 0xf0000000UL
) {
573 unsigned long avoid_start
= (unsigned long) KERNBASE
;
574 unsigned long avoid_end
= avoid_start
+ (4 * 1024 * 1024);
577 avoid_end
+= (4 * 1024 * 1024);
578 if (vaddr
< avoid_start
) {
579 unsigned long top
= vaddr
+ size
;
581 if (top
> avoid_start
)
583 prom_unmap(top
- vaddr
, vaddr
);
585 if ((vaddr
+ size
) > avoid_end
) {
586 unsigned long bottom
= vaddr
;
588 if (bottom
< avoid_end
)
590 prom_unmap((vaddr
+ size
) - bottom
, bottom
);
595 prom_printf("done.\n");
597 register_prom_callbacks();
600 /* The OBP specifications for sun4u mark 0xfffffffc00000000 and
601 * upwards as reserved for use by the firmware (I wonder if this
602 * will be the same on Cheetah...). We use this virtual address
603 * range for the VPTE table mappings of the nucleus so we need
604 * to zap them when we enter the PROM. -DaveM
606 static void __flush_nucleus_vptes(void)
608 unsigned long prom_reserved_base
= 0xfffffffc00000000UL
;
611 /* Only DTLB must be checked for VPTE entries. */
612 if (tlb_type
== spitfire
) {
613 for (i
= 0; i
< 63; i
++) {
616 /* Spitfire Errata #32 workaround */
617 /* NOTE: Always runs on spitfire, so no cheetah+
618 * page size encodings.
620 __asm__
__volatile__("stxa %0, [%1] %2\n\t"
624 "r" (PRIMARY_CONTEXT
), "i" (ASI_DMMU
));
626 tag
= spitfire_get_dtlb_tag(i
);
627 if (((tag
& ~(PAGE_MASK
)) == 0) &&
628 ((tag
& (PAGE_MASK
)) >= prom_reserved_base
)) {
629 __asm__
__volatile__("stxa %%g0, [%0] %1\n\t"
632 : "r" (TLB_TAG_ACCESS
), "i" (ASI_DMMU
));
633 spitfire_put_dtlb_data(i
, 0x0UL
);
636 } else if (tlb_type
== cheetah
|| tlb_type
== cheetah_plus
) {
637 for (i
= 0; i
< 512; i
++) {
638 unsigned long tag
= cheetah_get_dtlb_tag(i
, 2);
640 if ((tag
& ~PAGE_MASK
) == 0 &&
641 (tag
& PAGE_MASK
) >= prom_reserved_base
) {
642 __asm__
__volatile__("stxa %%g0, [%0] %1\n\t"
645 : "r" (TLB_TAG_ACCESS
), "i" (ASI_DMMU
));
646 cheetah_put_dtlb_data(i
, 0x0UL
, 2);
649 if (tlb_type
!= cheetah_plus
)
652 tag
= cheetah_get_dtlb_tag(i
, 3);
654 if ((tag
& ~PAGE_MASK
) == 0 &&
655 (tag
& PAGE_MASK
) >= prom_reserved_base
) {
656 __asm__
__volatile__("stxa %%g0, [%0] %1\n\t"
659 : "r" (TLB_TAG_ACCESS
), "i" (ASI_DMMU
));
660 cheetah_put_dtlb_data(i
, 0x0UL
, 3);
664 /* Implement me :-) */
669 static int prom_ditlb_set
;
670 struct prom_tlb_entry
{
672 unsigned long tlb_tag
;
673 unsigned long tlb_data
;
675 struct prom_tlb_entry prom_itlb
[16], prom_dtlb
[16];
677 void prom_world(int enter
)
679 unsigned long pstate
;
683 set_fs((mm_segment_t
) { get_thread_current_ds() });
688 /* Make sure the following runs atomically. */
689 __asm__
__volatile__("flushw\n\t"
690 "rdpr %%pstate, %0\n\t"
691 "wrpr %0, %1, %%pstate"
696 /* Kick out nucleus VPTEs. */
697 __flush_nucleus_vptes();
699 /* Install PROM world. */
700 for (i
= 0; i
< 16; i
++) {
701 if (prom_dtlb
[i
].tlb_ent
!= -1) {
702 __asm__
__volatile__("stxa %0, [%1] %2\n\t"
704 : : "r" (prom_dtlb
[i
].tlb_tag
), "r" (TLB_TAG_ACCESS
),
706 if (tlb_type
== spitfire
)
707 spitfire_put_dtlb_data(prom_dtlb
[i
].tlb_ent
,
708 prom_dtlb
[i
].tlb_data
);
709 else if (tlb_type
== cheetah
|| tlb_type
== cheetah_plus
)
710 cheetah_put_ldtlb_data(prom_dtlb
[i
].tlb_ent
,
711 prom_dtlb
[i
].tlb_data
);
713 if (prom_itlb
[i
].tlb_ent
!= -1) {
714 __asm__
__volatile__("stxa %0, [%1] %2\n\t"
716 : : "r" (prom_itlb
[i
].tlb_tag
),
717 "r" (TLB_TAG_ACCESS
),
719 if (tlb_type
== spitfire
)
720 spitfire_put_itlb_data(prom_itlb
[i
].tlb_ent
,
721 prom_itlb
[i
].tlb_data
);
722 else if (tlb_type
== cheetah
|| tlb_type
== cheetah_plus
)
723 cheetah_put_litlb_data(prom_itlb
[i
].tlb_ent
,
724 prom_itlb
[i
].tlb_data
);
728 for (i
= 0; i
< 16; i
++) {
729 if (prom_dtlb
[i
].tlb_ent
!= -1) {
730 __asm__
__volatile__("stxa %%g0, [%0] %1\n\t"
732 : : "r" (TLB_TAG_ACCESS
), "i" (ASI_DMMU
));
733 if (tlb_type
== spitfire
)
734 spitfire_put_dtlb_data(prom_dtlb
[i
].tlb_ent
, 0x0UL
);
736 cheetah_put_ldtlb_data(prom_dtlb
[i
].tlb_ent
, 0x0UL
);
738 if (prom_itlb
[i
].tlb_ent
!= -1) {
739 __asm__
__volatile__("stxa %%g0, [%0] %1\n\t"
741 : : "r" (TLB_TAG_ACCESS
),
743 if (tlb_type
== spitfire
)
744 spitfire_put_itlb_data(prom_itlb
[i
].tlb_ent
, 0x0UL
);
746 cheetah_put_litlb_data(prom_itlb
[i
].tlb_ent
, 0x0UL
);
750 __asm__
__volatile__("wrpr %0, 0, %%pstate"
754 void inherit_locked_prom_mappings(int save_p
)
760 /* Fucking losing PROM has more mappings in the TLB, but
761 * it (conveniently) fails to mention any of these in the
762 * translations property. The only ones that matter are
763 * the locked PROM tlb entries, so we impose the following
764 * irrecovable rule on the PROM, it is allowed 8 locked
765 * entries in the ITLB and 8 in the DTLB.
767 * Supposedly the upper 16GB of the address space is
768 * reserved for OBP, BUT I WISH THIS WAS DOCUMENTED
769 * SOMEWHERE!!!!!!!!!!!!!!!!! Furthermore the entire interface
770 * used between the client program and the firmware on sun5
771 * systems to coordinate mmu mappings is also COMPLETELY
772 * UNDOCUMENTED!!!!!! Thanks S(t)un!
775 for (i
= 0; i
< 16; i
++) {
776 prom_itlb
[i
].tlb_ent
= -1;
777 prom_dtlb
[i
].tlb_ent
= -1;
780 if (tlb_type
== spitfire
) {
781 int high
= SPITFIRE_HIGHEST_LOCKED_TLBENT
- bigkernel
;
782 for (i
= 0; i
< high
; i
++) {
785 /* Spitfire Errata #32 workaround */
786 /* NOTE: Always runs on spitfire, so no cheetah+
787 * page size encodings.
789 __asm__
__volatile__("stxa %0, [%1] %2\n\t"
793 "r" (PRIMARY_CONTEXT
), "i" (ASI_DMMU
));
795 data
= spitfire_get_dtlb_data(i
);
796 if ((data
& (_PAGE_L
|_PAGE_VALID
)) == (_PAGE_L
|_PAGE_VALID
)) {
799 /* Spitfire Errata #32 workaround */
800 /* NOTE: Always runs on spitfire, so no
801 * cheetah+ page size encodings.
803 __asm__
__volatile__("stxa %0, [%1] %2\n\t"
807 "r" (PRIMARY_CONTEXT
), "i" (ASI_DMMU
));
809 tag
= spitfire_get_dtlb_tag(i
);
811 prom_dtlb
[dtlb_seen
].tlb_ent
= i
;
812 prom_dtlb
[dtlb_seen
].tlb_tag
= tag
;
813 prom_dtlb
[dtlb_seen
].tlb_data
= data
;
815 __asm__
__volatile__("stxa %%g0, [%0] %1\n\t"
817 : : "r" (TLB_TAG_ACCESS
), "i" (ASI_DMMU
));
818 spitfire_put_dtlb_data(i
, 0x0UL
);
826 for (i
= 0; i
< high
; i
++) {
829 /* Spitfire Errata #32 workaround */
830 /* NOTE: Always runs on spitfire, so no
831 * cheetah+ page size encodings.
833 __asm__
__volatile__("stxa %0, [%1] %2\n\t"
837 "r" (PRIMARY_CONTEXT
), "i" (ASI_DMMU
));
839 data
= spitfire_get_itlb_data(i
);
840 if ((data
& (_PAGE_L
|_PAGE_VALID
)) == (_PAGE_L
|_PAGE_VALID
)) {
843 /* Spitfire Errata #32 workaround */
844 /* NOTE: Always runs on spitfire, so no
845 * cheetah+ page size encodings.
847 __asm__
__volatile__("stxa %0, [%1] %2\n\t"
851 "r" (PRIMARY_CONTEXT
), "i" (ASI_DMMU
));
853 tag
= spitfire_get_itlb_tag(i
);
855 prom_itlb
[itlb_seen
].tlb_ent
= i
;
856 prom_itlb
[itlb_seen
].tlb_tag
= tag
;
857 prom_itlb
[itlb_seen
].tlb_data
= data
;
859 __asm__
__volatile__("stxa %%g0, [%0] %1\n\t"
861 : : "r" (TLB_TAG_ACCESS
), "i" (ASI_IMMU
));
862 spitfire_put_itlb_data(i
, 0x0UL
);
869 } else if (tlb_type
== cheetah
|| tlb_type
== cheetah_plus
) {
870 int high
= CHEETAH_HIGHEST_LOCKED_TLBENT
- bigkernel
;
872 for (i
= 0; i
< high
; i
++) {
875 data
= cheetah_get_ldtlb_data(i
);
876 if ((data
& (_PAGE_L
|_PAGE_VALID
)) == (_PAGE_L
|_PAGE_VALID
)) {
879 tag
= cheetah_get_ldtlb_tag(i
);
881 prom_dtlb
[dtlb_seen
].tlb_ent
= i
;
882 prom_dtlb
[dtlb_seen
].tlb_tag
= tag
;
883 prom_dtlb
[dtlb_seen
].tlb_data
= data
;
885 __asm__
__volatile__("stxa %%g0, [%0] %1\n\t"
887 : : "r" (TLB_TAG_ACCESS
), "i" (ASI_DMMU
));
888 cheetah_put_ldtlb_data(i
, 0x0UL
);
896 for (i
= 0; i
< high
; i
++) {
899 data
= cheetah_get_litlb_data(i
);
900 if ((data
& (_PAGE_L
|_PAGE_VALID
)) == (_PAGE_L
|_PAGE_VALID
)) {
903 tag
= cheetah_get_litlb_tag(i
);
905 prom_itlb
[itlb_seen
].tlb_ent
= i
;
906 prom_itlb
[itlb_seen
].tlb_tag
= tag
;
907 prom_itlb
[itlb_seen
].tlb_data
= data
;
909 __asm__
__volatile__("stxa %%g0, [%0] %1\n\t"
911 : : "r" (TLB_TAG_ACCESS
), "i" (ASI_IMMU
));
912 cheetah_put_litlb_data(i
, 0x0UL
);
920 /* Implement me :-) */
927 /* Give PROM back his world, done during reboots... */
928 void prom_reload_locked(void)
932 for (i
= 0; i
< 16; i
++) {
933 if (prom_dtlb
[i
].tlb_ent
!= -1) {
934 __asm__
__volatile__("stxa %0, [%1] %2\n\t"
936 : : "r" (prom_dtlb
[i
].tlb_tag
), "r" (TLB_TAG_ACCESS
),
938 if (tlb_type
== spitfire
)
939 spitfire_put_dtlb_data(prom_dtlb
[i
].tlb_ent
,
940 prom_dtlb
[i
].tlb_data
);
941 else if (tlb_type
== cheetah
|| tlb_type
== cheetah_plus
)
942 cheetah_put_ldtlb_data(prom_dtlb
[i
].tlb_ent
,
943 prom_dtlb
[i
].tlb_data
);
946 if (prom_itlb
[i
].tlb_ent
!= -1) {
947 __asm__
__volatile__("stxa %0, [%1] %2\n\t"
949 : : "r" (prom_itlb
[i
].tlb_tag
),
950 "r" (TLB_TAG_ACCESS
),
952 if (tlb_type
== spitfire
)
953 spitfire_put_itlb_data(prom_itlb
[i
].tlb_ent
,
954 prom_itlb
[i
].tlb_data
);
956 cheetah_put_litlb_data(prom_itlb
[i
].tlb_ent
,
957 prom_itlb
[i
].tlb_data
);
962 #ifdef DCACHE_ALIASING_POSSIBLE
963 void __flush_dcache_range(unsigned long start
, unsigned long end
)
967 if (tlb_type
== spitfire
) {
970 for (va
= start
; va
< end
; va
+= 32) {
971 spitfire_put_dcache_tag(va
& 0x3fe0, 0x0);
978 for (va
= start
; va
< end
; va
+= 32)
979 __asm__
__volatile__("stxa %%g0, [%0] %1\n\t"
983 "i" (ASI_DCACHE_INVALIDATE
));
986 #endif /* DCACHE_ALIASING_POSSIBLE */
988 /* If not locked, zap it. */
989 void __flush_tlb_all(void)
991 unsigned long pstate
;
994 __asm__
__volatile__("flushw\n\t"
995 "rdpr %%pstate, %0\n\t"
996 "wrpr %0, %1, %%pstate"
999 if (tlb_type
== spitfire
) {
1000 for (i
= 0; i
< 64; i
++) {
1001 /* Spitfire Errata #32 workaround */
1002 /* NOTE: Always runs on spitfire, so no
1003 * cheetah+ page size encodings.
1005 __asm__
__volatile__("stxa %0, [%1] %2\n\t"
1009 "r" (PRIMARY_CONTEXT
), "i" (ASI_DMMU
));
1011 if (!(spitfire_get_dtlb_data(i
) & _PAGE_L
)) {
1012 __asm__
__volatile__("stxa %%g0, [%0] %1\n\t"
1015 : "r" (TLB_TAG_ACCESS
), "i" (ASI_DMMU
));
1016 spitfire_put_dtlb_data(i
, 0x0UL
);
1019 /* Spitfire Errata #32 workaround */
1020 /* NOTE: Always runs on spitfire, so no
1021 * cheetah+ page size encodings.
1023 __asm__
__volatile__("stxa %0, [%1] %2\n\t"
1027 "r" (PRIMARY_CONTEXT
), "i" (ASI_DMMU
));
1029 if (!(spitfire_get_itlb_data(i
) & _PAGE_L
)) {
1030 __asm__
__volatile__("stxa %%g0, [%0] %1\n\t"
1033 : "r" (TLB_TAG_ACCESS
), "i" (ASI_IMMU
));
1034 spitfire_put_itlb_data(i
, 0x0UL
);
1037 } else if (tlb_type
== cheetah
|| tlb_type
== cheetah_plus
) {
1038 cheetah_flush_dtlb_all();
1039 cheetah_flush_itlb_all();
1041 __asm__
__volatile__("wrpr %0, 0, %%pstate"
1045 /* Caller does TLB context flushing on local CPU if necessary.
1046 * The caller also ensures that CTX_VALID(mm->context) is false.
1048 * We must be careful about boundary cases so that we never
1049 * let the user have CTX 0 (nucleus) or we ever use a CTX
1050 * version of zero (and thus NO_CONTEXT would not be caught
1051 * by version mis-match tests in mmu_context.h).
1053 void get_new_mmu_context(struct mm_struct
*mm
)
1055 unsigned long ctx
, new_ctx
;
1056 unsigned long orig_pgsz_bits
;
1059 spin_lock(&ctx_alloc_lock
);
1060 orig_pgsz_bits
= (mm
->context
.sparc64_ctx_val
& CTX_PGSZ_MASK
);
1061 ctx
= (tlb_context_cache
+ 1) & CTX_NR_MASK
;
1062 new_ctx
= find_next_zero_bit(mmu_context_bmap
, 1 << CTX_NR_BITS
, ctx
);
1063 if (new_ctx
>= (1 << CTX_NR_BITS
)) {
1064 new_ctx
= find_next_zero_bit(mmu_context_bmap
, ctx
, 1);
1065 if (new_ctx
>= ctx
) {
1067 new_ctx
= (tlb_context_cache
& CTX_VERSION_MASK
) +
1070 new_ctx
= CTX_FIRST_VERSION
;
1072 /* Don't call memset, for 16 entries that's just
1075 mmu_context_bmap
[0] = 3;
1076 mmu_context_bmap
[1] = 0;
1077 mmu_context_bmap
[2] = 0;
1078 mmu_context_bmap
[3] = 0;
1079 for (i
= 4; i
< CTX_BMAP_SLOTS
; i
+= 4) {
1080 mmu_context_bmap
[i
+ 0] = 0;
1081 mmu_context_bmap
[i
+ 1] = 0;
1082 mmu_context_bmap
[i
+ 2] = 0;
1083 mmu_context_bmap
[i
+ 3] = 0;
1088 mmu_context_bmap
[new_ctx
>>6] |= (1UL << (new_ctx
& 63));
1089 new_ctx
|= (tlb_context_cache
& CTX_VERSION_MASK
);
1091 tlb_context_cache
= new_ctx
;
1092 mm
->context
.sparc64_ctx_val
= new_ctx
| orig_pgsz_bits
;
1093 spin_unlock(&ctx_alloc_lock
);
1097 struct pgtable_cache_struct pgt_quicklists
;
1100 /* OK, we have to color these pages. The page tables are accessed
1101 * by non-Dcache enabled mapping in the VPTE area by the dtlb_backend.S
1102 * code, as well as by PAGE_OFFSET range direct-mapped addresses by
1103 * other parts of the kernel. By coloring, we make sure that the tlbmiss
1104 * fast handlers do not get data from old/garbage dcache lines that
1105 * correspond to an old/stale virtual address (user/kernel) that
1106 * previously mapped the pagetable page while accessing vpte range
1107 * addresses. The idea is that if the vpte color and PAGE_OFFSET range
1108 * color is the same, then when the kernel initializes the pagetable
1109 * using the later address range, accesses with the first address
1110 * range will see the newly initialized data rather than the garbage.
1112 #ifdef DCACHE_ALIASING_POSSIBLE
1113 #define DC_ALIAS_SHIFT 1
1115 #define DC_ALIAS_SHIFT 0
1117 pte_t
*pte_alloc_one_kernel(struct mm_struct
*mm
, unsigned long address
)
1120 unsigned long color
;
1123 pte_t
*ptep
= pte_alloc_one_fast(mm
, address
);
1129 color
= VPTE_COLOR(address
);
1130 page
= alloc_pages(GFP_KERNEL
|__GFP_REPEAT
, DC_ALIAS_SHIFT
);
1132 unsigned long *to_free
;
1133 unsigned long paddr
;
1136 #ifdef DCACHE_ALIASING_POSSIBLE
1137 set_page_count(page
, 1);
1138 ClearPageCompound(page
);
1140 set_page_count((page
+ 1), 1);
1141 ClearPageCompound(page
+ 1);
1143 paddr
= (unsigned long) page_address(page
);
1144 memset((char *)paddr
, 0, (PAGE_SIZE
<< DC_ALIAS_SHIFT
));
1147 pte
= (pte_t
*) paddr
;
1148 to_free
= (unsigned long *) (paddr
+ PAGE_SIZE
);
1150 pte
= (pte_t
*) (paddr
+ PAGE_SIZE
);
1151 to_free
= (unsigned long *) paddr
;
1154 #ifdef DCACHE_ALIASING_POSSIBLE
1155 /* Now free the other one up, adjust cache size. */
1157 *to_free
= (unsigned long) pte_quicklist
[color
^ 0x1];
1158 pte_quicklist
[color
^ 0x1] = to_free
;
1159 pgtable_cache_size
++;
1168 void sparc_ultra_dump_itlb(void)
1172 if (tlb_type
== spitfire
) {
1173 printk ("Contents of itlb: ");
1174 for (slot
= 0; slot
< 14; slot
++) printk (" ");
1175 printk ("%2x:%016lx,%016lx\n",
1177 spitfire_get_itlb_tag(0), spitfire_get_itlb_data(0));
1178 for (slot
= 1; slot
< 64; slot
+=3) {
1179 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx %2x:%016lx,%016lx\n",
1181 spitfire_get_itlb_tag(slot
), spitfire_get_itlb_data(slot
),
1183 spitfire_get_itlb_tag(slot
+1), spitfire_get_itlb_data(slot
+1),
1185 spitfire_get_itlb_tag(slot
+2), spitfire_get_itlb_data(slot
+2));
1187 } else if (tlb_type
== cheetah
|| tlb_type
== cheetah_plus
) {
1188 printk ("Contents of itlb0:\n");
1189 for (slot
= 0; slot
< 16; slot
+=2) {
1190 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
1192 cheetah_get_litlb_tag(slot
), cheetah_get_litlb_data(slot
),
1194 cheetah_get_litlb_tag(slot
+1), cheetah_get_litlb_data(slot
+1));
1196 printk ("Contents of itlb2:\n");
1197 for (slot
= 0; slot
< 128; slot
+=2) {
1198 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
1200 cheetah_get_itlb_tag(slot
), cheetah_get_itlb_data(slot
),
1202 cheetah_get_itlb_tag(slot
+1), cheetah_get_itlb_data(slot
+1));
1207 void sparc_ultra_dump_dtlb(void)
1211 if (tlb_type
== spitfire
) {
1212 printk ("Contents of dtlb: ");
1213 for (slot
= 0; slot
< 14; slot
++) printk (" ");
1214 printk ("%2x:%016lx,%016lx\n", 0,
1215 spitfire_get_dtlb_tag(0), spitfire_get_dtlb_data(0));
1216 for (slot
= 1; slot
< 64; slot
+=3) {
1217 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx %2x:%016lx,%016lx\n",
1219 spitfire_get_dtlb_tag(slot
), spitfire_get_dtlb_data(slot
),
1221 spitfire_get_dtlb_tag(slot
+1), spitfire_get_dtlb_data(slot
+1),
1223 spitfire_get_dtlb_tag(slot
+2), spitfire_get_dtlb_data(slot
+2));
1225 } else if (tlb_type
== cheetah
|| tlb_type
== cheetah_plus
) {
1226 printk ("Contents of dtlb0:\n");
1227 for (slot
= 0; slot
< 16; slot
+=2) {
1228 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
1230 cheetah_get_ldtlb_tag(slot
), cheetah_get_ldtlb_data(slot
),
1232 cheetah_get_ldtlb_tag(slot
+1), cheetah_get_ldtlb_data(slot
+1));
1234 printk ("Contents of dtlb2:\n");
1235 for (slot
= 0; slot
< 512; slot
+=2) {
1236 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
1238 cheetah_get_dtlb_tag(slot
, 2), cheetah_get_dtlb_data(slot
, 2),
1240 cheetah_get_dtlb_tag(slot
+1, 2), cheetah_get_dtlb_data(slot
+1, 2));
1242 if (tlb_type
== cheetah_plus
) {
1243 printk ("Contents of dtlb3:\n");
1244 for (slot
= 0; slot
< 512; slot
+=2) {
1245 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
1247 cheetah_get_dtlb_tag(slot
, 3), cheetah_get_dtlb_data(slot
, 3),
1249 cheetah_get_dtlb_tag(slot
+1, 3), cheetah_get_dtlb_data(slot
+1, 3));
1255 extern unsigned long cmdline_memory_size
;
1257 unsigned long __init
bootmem_init(unsigned long *pages_avail
)
1259 unsigned long bootmap_size
, start_pfn
, end_pfn
;
1260 unsigned long end_of_phys_memory
= 0UL;
1261 unsigned long bootmap_pfn
, bytes_avail
, size
;
1264 #ifdef CONFIG_DEBUG_BOOTMEM
1265 prom_printf("bootmem_init: Scan sp_banks, ");
1269 for (i
= 0; sp_banks
[i
].num_bytes
!= 0; i
++) {
1270 end_of_phys_memory
= sp_banks
[i
].base_addr
+
1271 sp_banks
[i
].num_bytes
;
1272 bytes_avail
+= sp_banks
[i
].num_bytes
;
1273 if (cmdline_memory_size
) {
1274 if (bytes_avail
> cmdline_memory_size
) {
1275 unsigned long slack
= bytes_avail
- cmdline_memory_size
;
1277 bytes_avail
-= slack
;
1278 end_of_phys_memory
-= slack
;
1280 sp_banks
[i
].num_bytes
-= slack
;
1281 if (sp_banks
[i
].num_bytes
== 0) {
1282 sp_banks
[i
].base_addr
= 0xdeadbeef;
1284 sp_banks
[i
+1].num_bytes
= 0;
1285 sp_banks
[i
+1].base_addr
= 0xdeadbeef;
1292 *pages_avail
= bytes_avail
>> PAGE_SHIFT
;
1294 /* Start with page aligned address of last symbol in kernel
1295 * image. The kernel is hard mapped below PAGE_OFFSET in a
1296 * 4MB locked TLB translation.
1298 start_pfn
= PAGE_ALIGN(kern_base
+ kern_size
) >> PAGE_SHIFT
;
1300 bootmap_pfn
= start_pfn
;
1302 end_pfn
= end_of_phys_memory
>> PAGE_SHIFT
;
1304 #ifdef CONFIG_BLK_DEV_INITRD
1305 /* Now have to check initial ramdisk, so that bootmap does not overwrite it */
1306 if (sparc_ramdisk_image
|| sparc_ramdisk_image64
) {
1307 unsigned long ramdisk_image
= sparc_ramdisk_image
?
1308 sparc_ramdisk_image
: sparc_ramdisk_image64
;
1309 if (ramdisk_image
>= (unsigned long)_end
- 2 * PAGE_SIZE
)
1310 ramdisk_image
-= KERNBASE
;
1311 initrd_start
= ramdisk_image
+ phys_base
;
1312 initrd_end
= initrd_start
+ sparc_ramdisk_size
;
1313 if (initrd_end
> end_of_phys_memory
) {
1314 printk(KERN_CRIT
"initrd extends beyond end of memory "
1315 "(0x%016lx > 0x%016lx)\ndisabling initrd\n",
1316 initrd_end
, end_of_phys_memory
);
1320 if (initrd_start
>= (start_pfn
<< PAGE_SHIFT
) &&
1321 initrd_start
< (start_pfn
<< PAGE_SHIFT
) + 2 * PAGE_SIZE
)
1322 bootmap_pfn
= PAGE_ALIGN (initrd_end
) >> PAGE_SHIFT
;
1326 /* Initialize the boot-time allocator. */
1327 max_pfn
= max_low_pfn
= end_pfn
;
1328 min_low_pfn
= pfn_base
;
1330 #ifdef CONFIG_DEBUG_BOOTMEM
1331 prom_printf("init_bootmem(min[%lx], bootmap[%lx], max[%lx])\n",
1332 min_low_pfn
, bootmap_pfn
, max_low_pfn
);
1334 bootmap_size
= init_bootmem_node(NODE_DATA(0), bootmap_pfn
, pfn_base
, end_pfn
);
1336 bootmap_base
= bootmap_pfn
<< PAGE_SHIFT
;
1338 /* Now register the available physical memory with the
1341 for (i
= 0; sp_banks
[i
].num_bytes
!= 0; i
++) {
1342 #ifdef CONFIG_DEBUG_BOOTMEM
1343 prom_printf("free_bootmem(sp_banks:%d): base[%lx] size[%lx]\n",
1344 i
, sp_banks
[i
].base_addr
, sp_banks
[i
].num_bytes
);
1346 free_bootmem(sp_banks
[i
].base_addr
, sp_banks
[i
].num_bytes
);
1349 #ifdef CONFIG_BLK_DEV_INITRD
1351 size
= initrd_end
- initrd_start
;
1353 /* Resert the initrd image area. */
1354 #ifdef CONFIG_DEBUG_BOOTMEM
1355 prom_printf("reserve_bootmem(initrd): base[%llx] size[%lx]\n",
1356 initrd_start
, initrd_end
);
1358 reserve_bootmem(initrd_start
, size
);
1359 *pages_avail
-= PAGE_ALIGN(size
) >> PAGE_SHIFT
;
1361 initrd_start
+= PAGE_OFFSET
;
1362 initrd_end
+= PAGE_OFFSET
;
1365 /* Reserve the kernel text/data/bss. */
1366 #ifdef CONFIG_DEBUG_BOOTMEM
1367 prom_printf("reserve_bootmem(kernel): base[%lx] size[%lx]\n", kern_base
, kern_size
);
1369 reserve_bootmem(kern_base
, kern_size
);
1370 *pages_avail
-= PAGE_ALIGN(kern_size
) >> PAGE_SHIFT
;
1372 /* Reserve the bootmem map. We do not account for it
1373 * in pages_avail because we will release that memory
1374 * in free_all_bootmem.
1376 size
= bootmap_size
;
1377 #ifdef CONFIG_DEBUG_BOOTMEM
1378 prom_printf("reserve_bootmem(bootmap): base[%lx] size[%lx]\n",
1379 (bootmap_pfn
<< PAGE_SHIFT
), size
);
1381 reserve_bootmem((bootmap_pfn
<< PAGE_SHIFT
), size
);
1382 *pages_avail
-= PAGE_ALIGN(size
) >> PAGE_SHIFT
;
1387 /* paging_init() sets up the page tables */
1389 extern void cheetah_ecache_flush_init(void);
1391 static unsigned long last_valid_pfn
;
1393 void __init
paging_init(void)
1395 extern pmd_t swapper_pmd_dir
[1024];
1396 extern unsigned int sparc64_vpte_patchme1
[1];
1397 extern unsigned int sparc64_vpte_patchme2
[1];
1398 unsigned long alias_base
= kern_base
+ PAGE_OFFSET
;
1399 unsigned long second_alias_page
= 0;
1400 unsigned long pt
, flags
, end_pfn
, pages_avail
;
1401 unsigned long shift
= alias_base
- ((unsigned long)KERNBASE
);
1402 unsigned long real_end
;
1404 set_bit(0, mmu_context_bmap
);
1406 real_end
= (unsigned long)_end
;
1407 if ((real_end
> ((unsigned long)KERNBASE
+ 0x400000)))
1409 #ifdef CONFIG_BLK_DEV_INITRD
1410 if (sparc_ramdisk_image
|| sparc_ramdisk_image64
)
1411 real_end
= (PAGE_ALIGN(real_end
) + PAGE_ALIGN(sparc_ramdisk_size
));
1414 /* We assume physical memory starts at some 4mb multiple,
1415 * if this were not true we wouldn't boot up to this point
1418 pt
= kern_base
| _PAGE_VALID
| _PAGE_SZ4MB
;
1419 pt
|= _PAGE_CP
| _PAGE_CV
| _PAGE_P
| _PAGE_L
| _PAGE_W
;
1420 local_irq_save(flags
);
1421 if (tlb_type
== spitfire
) {
1422 __asm__
__volatile__(
1423 " stxa %1, [%0] %3\n"
1424 " stxa %2, [%5] %4\n"
1431 : "r" (TLB_TAG_ACCESS
), "r" (alias_base
), "r" (pt
),
1432 "i" (ASI_DMMU
), "i" (ASI_DTLB_DATA_ACCESS
), "r" (61 << 3)
1434 if (real_end
>= KERNBASE
+ 0x340000) {
1435 second_alias_page
= alias_base
+ 0x400000;
1436 __asm__
__volatile__(
1437 " stxa %1, [%0] %3\n"
1438 " stxa %2, [%5] %4\n"
1445 : "r" (TLB_TAG_ACCESS
), "r" (second_alias_page
), "r" (pt
+ 0x400000),
1446 "i" (ASI_DMMU
), "i" (ASI_DTLB_DATA_ACCESS
), "r" (60 << 3)
1449 } else if (tlb_type
== cheetah
|| tlb_type
== cheetah_plus
) {
1450 __asm__
__volatile__(
1451 " stxa %1, [%0] %3\n"
1452 " stxa %2, [%5] %4\n"
1459 : "r" (TLB_TAG_ACCESS
), "r" (alias_base
), "r" (pt
),
1460 "i" (ASI_DMMU
), "i" (ASI_DTLB_DATA_ACCESS
), "r" ((0<<16) | (13<<3))
1462 if (real_end
>= KERNBASE
+ 0x340000) {
1463 second_alias_page
= alias_base
+ 0x400000;
1464 __asm__
__volatile__(
1465 " stxa %1, [%0] %3\n"
1466 " stxa %2, [%5] %4\n"
1473 : "r" (TLB_TAG_ACCESS
), "r" (second_alias_page
), "r" (pt
+ 0x400000),
1474 "i" (ASI_DMMU
), "i" (ASI_DTLB_DATA_ACCESS
), "r" ((0<<16) | (12<<3))
1478 local_irq_restore(flags
);
1480 /* Now set kernel pgd to upper alias so physical page computations
1483 init_mm
.pgd
+= ((shift
) / (sizeof(pgd_t
)));
1485 memset(swapper_pmd_dir
, 0, sizeof(swapper_pmd_dir
));
1487 /* Now can init the kernel/bad page tables. */
1488 pud_set(pud_offset(&swapper_pg_dir
[0], 0),
1489 swapper_pmd_dir
+ (shift
/ sizeof(pgd_t
)));
1491 sparc64_vpte_patchme1
[0] |=
1492 (((unsigned long)pgd_val(init_mm
.pgd
[0])) >> 10);
1493 sparc64_vpte_patchme2
[0] |=
1494 (((unsigned long)pgd_val(init_mm
.pgd
[0])) & 0x3ff);
1495 flushi((long)&sparc64_vpte_patchme1
[0]);
1497 /* Setup bootmem... */
1499 last_valid_pfn
= end_pfn
= bootmem_init(&pages_avail
);
1501 /* Inherit non-locked OBP mappings. */
1502 inherit_prom_mappings();
1504 /* Ok, we can use our TLB miss and window trap handlers safely.
1505 * We need to do a quick peek here to see if we are on StarFire
1506 * or not, so setup_tba can setup the IRQ globals correctly (it
1507 * needs to get the hard smp processor id correctly).
1510 extern void setup_tba(int);
1511 setup_tba(this_is_starfire
);
1514 inherit_locked_prom_mappings(1);
1516 /* We only created DTLB mapping of this stuff. */
1517 spitfire_flush_dtlb_nucleus_page(alias_base
);
1518 if (second_alias_page
)
1519 spitfire_flush_dtlb_nucleus_page(second_alias_page
);
1524 unsigned long zones_size
[MAX_NR_ZONES
];
1525 unsigned long zholes_size
[MAX_NR_ZONES
];
1526 unsigned long npages
;
1529 for (znum
= 0; znum
< MAX_NR_ZONES
; znum
++)
1530 zones_size
[znum
] = zholes_size
[znum
] = 0;
1532 npages
= end_pfn
- pfn_base
;
1533 zones_size
[ZONE_DMA
] = npages
;
1534 zholes_size
[ZONE_DMA
] = npages
- pages_avail
;
1536 free_area_init_node(0, &contig_page_data
, zones_size
,
1537 phys_base
>> PAGE_SHIFT
, zholes_size
);
1543 /* Ok, it seems that the prom can allocate some more memory chunks
1544 * as a side effect of some prom calls we perform during the
1545 * boot sequence. My most likely theory is that it is from the
1546 * prom_set_traptable() call, and OBP is allocating a scratchpad
1547 * for saving client program register state etc.
1549 static void __init
sort_memlist(struct linux_mlist_p1275
*thislist
)
1553 unsigned long tmpaddr
, tmpsize
;
1554 unsigned long lowest
;
1556 for (i
= 0; thislist
[i
].theres_more
!= 0; i
++) {
1557 lowest
= thislist
[i
].start_adr
;
1558 for (mitr
= i
+1; thislist
[mitr
-1].theres_more
!= 0; mitr
++)
1559 if (thislist
[mitr
].start_adr
< lowest
) {
1560 lowest
= thislist
[mitr
].start_adr
;
1563 if (lowest
== thislist
[i
].start_adr
)
1565 tmpaddr
= thislist
[swapi
].start_adr
;
1566 tmpsize
= thislist
[swapi
].num_bytes
;
1567 for (mitr
= swapi
; mitr
> i
; mitr
--) {
1568 thislist
[mitr
].start_adr
= thislist
[mitr
-1].start_adr
;
1569 thislist
[mitr
].num_bytes
= thislist
[mitr
-1].num_bytes
;
1571 thislist
[i
].start_adr
= tmpaddr
;
1572 thislist
[i
].num_bytes
= tmpsize
;
1576 void __init
rescan_sp_banks(void)
1578 struct linux_prom64_registers memlist
[64];
1579 struct linux_mlist_p1275 avail
[64], *mlist
;
1580 unsigned long bytes
, base_paddr
;
1581 int num_regs
, node
= prom_finddevice("/memory");
1584 num_regs
= prom_getproperty(node
, "available",
1585 (char *) memlist
, sizeof(memlist
));
1586 num_regs
= (num_regs
/ sizeof(struct linux_prom64_registers
));
1587 for (i
= 0; i
< num_regs
; i
++) {
1588 avail
[i
].start_adr
= memlist
[i
].phys_addr
;
1589 avail
[i
].num_bytes
= memlist
[i
].reg_size
;
1590 avail
[i
].theres_more
= &avail
[i
+ 1];
1592 avail
[i
- 1].theres_more
= NULL
;
1593 sort_memlist(avail
);
1597 bytes
= mlist
->num_bytes
;
1598 base_paddr
= mlist
->start_adr
;
1600 sp_banks
[0].base_addr
= base_paddr
;
1601 sp_banks
[0].num_bytes
= bytes
;
1603 while (mlist
->theres_more
!= NULL
){
1605 mlist
= mlist
->theres_more
;
1606 bytes
= mlist
->num_bytes
;
1607 if (i
>= SPARC_PHYS_BANKS
-1) {
1608 printk ("The machine has more banks than "
1609 "this kernel can support\n"
1610 "Increase the SPARC_PHYS_BANKS "
1611 "setting (currently %d)\n",
1613 i
= SPARC_PHYS_BANKS
-1;
1617 sp_banks
[i
].base_addr
= mlist
->start_adr
;
1618 sp_banks
[i
].num_bytes
= mlist
->num_bytes
;
1622 sp_banks
[i
].base_addr
= 0xdeadbeefbeefdeadUL
;
1623 sp_banks
[i
].num_bytes
= 0;
1625 for (i
= 0; sp_banks
[i
].num_bytes
!= 0; i
++)
1626 sp_banks
[i
].num_bytes
&= PAGE_MASK
;
1629 static void __init
taint_real_pages(void)
1631 struct sparc_phys_banks saved_sp_banks
[SPARC_PHYS_BANKS
];
1634 for (i
= 0; i
< SPARC_PHYS_BANKS
; i
++) {
1635 saved_sp_banks
[i
].base_addr
=
1636 sp_banks
[i
].base_addr
;
1637 saved_sp_banks
[i
].num_bytes
=
1638 sp_banks
[i
].num_bytes
;
1643 /* Find changes discovered in the sp_bank rescan and
1644 * reserve the lost portions in the bootmem maps.
1646 for (i
= 0; saved_sp_banks
[i
].num_bytes
; i
++) {
1647 unsigned long old_start
, old_end
;
1649 old_start
= saved_sp_banks
[i
].base_addr
;
1650 old_end
= old_start
+
1651 saved_sp_banks
[i
].num_bytes
;
1652 while (old_start
< old_end
) {
1655 for (n
= 0; sp_banks
[n
].num_bytes
; n
++) {
1656 unsigned long new_start
, new_end
;
1658 new_start
= sp_banks
[n
].base_addr
;
1659 new_end
= new_start
+ sp_banks
[n
].num_bytes
;
1661 if (new_start
<= old_start
&&
1662 new_end
>= (old_start
+ PAGE_SIZE
)) {
1663 set_bit (old_start
>> 22,
1664 sparc64_valid_addr_bitmap
);
1668 reserve_bootmem(old_start
, PAGE_SIZE
);
1671 old_start
+= PAGE_SIZE
;
1676 void __init
mem_init(void)
1678 unsigned long codepages
, datapages
, initpages
;
1679 unsigned long addr
, last
;
1682 i
= last_valid_pfn
>> ((22 - PAGE_SHIFT
) + 6);
1684 sparc64_valid_addr_bitmap
= (unsigned long *)
1685 __alloc_bootmem(i
<< 3, SMP_CACHE_BYTES
, bootmap_base
);
1686 if (sparc64_valid_addr_bitmap
== NULL
) {
1687 prom_printf("mem_init: Cannot alloc valid_addr_bitmap.\n");
1690 memset(sparc64_valid_addr_bitmap
, 0, i
<< 3);
1692 addr
= PAGE_OFFSET
+ kern_base
;
1693 last
= PAGE_ALIGN(kern_size
) + addr
;
1694 while (addr
< last
) {
1695 set_bit(__pa(addr
) >> 22, sparc64_valid_addr_bitmap
);
1701 max_mapnr
= last_valid_pfn
- pfn_base
;
1702 high_memory
= __va(last_valid_pfn
<< PAGE_SHIFT
);
1704 #ifdef CONFIG_DEBUG_BOOTMEM
1705 prom_printf("mem_init: Calling free_all_bootmem().\n");
1707 totalram_pages
= num_physpages
= free_all_bootmem() - 1;
1710 * Set up the zero page, mark it reserved, so that page count
1711 * is not manipulated when freeing the page from user ptes.
1713 mem_map_zero
= alloc_pages(GFP_KERNEL
|__GFP_ZERO
, 0);
1714 if (mem_map_zero
== NULL
) {
1715 prom_printf("paging_init: Cannot alloc zero page.\n");
1718 SetPageReserved(mem_map_zero
);
1720 codepages
= (((unsigned long) _etext
) - ((unsigned long) _start
));
1721 codepages
= PAGE_ALIGN(codepages
) >> PAGE_SHIFT
;
1722 datapages
= (((unsigned long) _edata
) - ((unsigned long) _etext
));
1723 datapages
= PAGE_ALIGN(datapages
) >> PAGE_SHIFT
;
1724 initpages
= (((unsigned long) __init_end
) - ((unsigned long) __init_begin
));
1725 initpages
= PAGE_ALIGN(initpages
) >> PAGE_SHIFT
;
1727 printk("Memory: %uk available (%ldk kernel code, %ldk data, %ldk init) [%016lx,%016lx]\n",
1728 nr_free_pages() << (PAGE_SHIFT
-10),
1729 codepages
<< (PAGE_SHIFT
-10),
1730 datapages
<< (PAGE_SHIFT
-10),
1731 initpages
<< (PAGE_SHIFT
-10),
1732 PAGE_OFFSET
, (last_valid_pfn
<< PAGE_SHIFT
));
1734 if (tlb_type
== cheetah
|| tlb_type
== cheetah_plus
)
1735 cheetah_ecache_flush_init();
1738 void free_initmem (void)
1740 unsigned long addr
, initend
;
1743 * The init section is aligned to 8k in vmlinux.lds. Page align for >8k pagesizes.
1745 addr
= PAGE_ALIGN((unsigned long)(__init_begin
));
1746 initend
= (unsigned long)(__init_end
) & PAGE_MASK
;
1747 for (; addr
< initend
; addr
+= PAGE_SIZE
) {
1752 ((unsigned long) __va(kern_base
)) -
1753 ((unsigned long) KERNBASE
));
1754 memset((void *)addr
, 0xcc, PAGE_SIZE
);
1755 p
= virt_to_page(page
);
1757 ClearPageReserved(p
);
1758 set_page_count(p
, 1);
1765 #ifdef CONFIG_BLK_DEV_INITRD
1766 void free_initrd_mem(unsigned long start
, unsigned long end
)
1769 printk ("Freeing initrd memory: %ldk freed\n", (end
- start
) >> 10);
1770 for (; start
< end
; start
+= PAGE_SIZE
) {
1771 struct page
*p
= virt_to_page(start
);
1773 ClearPageReserved(p
);
1774 set_page_count(p
, 1);