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 "membar #StoreLoad | #StoreStore\n\t"
140 "bne,pn %%xcc, 1b\n\t"
143 : "r" (mask
), "r" (non_cpu_bits
), "r" (&page
->flags
)
147 static __inline__
void clear_dcache_dirty_cpu(struct page
*page
, unsigned long cpu
)
149 unsigned long mask
= (1UL << PG_dcache_dirty
);
151 __asm__
__volatile__("! test_and_clear_dcache_dirty\n"
154 "srlx %%g7, 24, %%g1\n\t"
155 "and %%g1, %3, %%g1\n\t"
157 "bne,pn %%icc, 2f\n\t"
158 " andn %%g7, %1, %%g1\n\t"
159 "casx [%2], %%g7, %%g1\n\t"
161 "membar #StoreLoad | #StoreStore\n\t"
162 "bne,pn %%xcc, 1b\n\t"
166 : "r" (cpu
), "r" (mask
), "r" (&page
->flags
),
171 extern void __update_mmu_cache(unsigned long mmu_context_hw
, unsigned long address
, pte_t pte
, int code
);
173 void update_mmu_cache(struct vm_area_struct
*vma
, unsigned long address
, pte_t pte
)
177 unsigned long pg_flags
;
180 if (pfn_valid(pfn
) &&
181 (page
= pfn_to_page(pfn
), page_mapping(page
)) &&
182 ((pg_flags
= page
->flags
) & (1UL << PG_dcache_dirty
))) {
183 int cpu
= ((pg_flags
>> 24) & (NR_CPUS
- 1UL));
184 int this_cpu
= get_cpu();
186 /* This is just to optimize away some function calls
190 flush_dcache_page_impl(page
);
192 smp_flush_dcache_page_impl(page
, cpu
);
194 clear_dcache_dirty_cpu(page
, cpu
);
199 if (get_thread_fault_code())
200 __update_mmu_cache(CTX_NRBITS(vma
->vm_mm
->context
),
201 address
, pte
, get_thread_fault_code());
204 void flush_dcache_page(struct page
*page
)
206 struct address_space
*mapping
;
209 /* Do not bother with the expensive D-cache flush if it
210 * is merely the zero page. The 'bigcore' testcase in GDB
211 * causes this case to run millions of times.
213 if (page
== ZERO_PAGE(0))
216 this_cpu
= get_cpu();
218 mapping
= page_mapping(page
);
219 if (mapping
&& !mapping_mapped(mapping
)) {
220 int dirty
= test_bit(PG_dcache_dirty
, &page
->flags
);
222 int dirty_cpu
= dcache_dirty_cpu(page
);
224 if (dirty_cpu
== this_cpu
)
226 smp_flush_dcache_page_impl(page
, dirty_cpu
);
228 set_dcache_dirty(page
, this_cpu
);
230 /* We could delay the flush for the !page_mapping
231 * case too. But that case is for exec env/arg
232 * pages and those are %99 certainly going to get
233 * faulted into the tlb (and thus flushed) anyways.
235 flush_dcache_page_impl(page
);
242 void flush_icache_range(unsigned long start
, unsigned long end
)
244 /* Cheetah has coherent I-cache. */
245 if (tlb_type
== spitfire
) {
248 for (kaddr
= start
; kaddr
< end
; kaddr
+= PAGE_SIZE
)
249 __flush_icache_page(__get_phys(kaddr
));
253 unsigned long page_to_pfn(struct page
*page
)
255 return (unsigned long) ((page
- mem_map
) + pfn_base
);
258 struct page
*pfn_to_page(unsigned long pfn
)
260 return (mem_map
+ (pfn
- pfn_base
));
265 printk("Mem-info:\n");
267 printk("Free swap: %6ldkB\n",
268 nr_swap_pages
<< (PAGE_SHIFT
-10));
269 printk("%ld pages of RAM\n", num_physpages
);
270 printk("%d free pages\n", nr_free_pages());
271 printk("%d pages in page table cache\n",pgtable_cache_size
);
274 void mmu_info(struct seq_file
*m
)
276 if (tlb_type
== cheetah
)
277 seq_printf(m
, "MMU Type\t: Cheetah\n");
278 else if (tlb_type
== cheetah_plus
)
279 seq_printf(m
, "MMU Type\t: Cheetah+\n");
280 else if (tlb_type
== spitfire
)
281 seq_printf(m
, "MMU Type\t: Spitfire\n");
283 seq_printf(m
, "MMU Type\t: ???\n");
285 #ifdef CONFIG_DEBUG_DCFLUSH
286 seq_printf(m
, "DCPageFlushes\t: %d\n",
287 atomic_read(&dcpage_flushes
));
289 seq_printf(m
, "DCPageFlushesXC\t: %d\n",
290 atomic_read(&dcpage_flushes_xcall
));
291 #endif /* CONFIG_SMP */
292 #endif /* CONFIG_DEBUG_DCFLUSH */
295 struct linux_prom_translation
{
301 extern unsigned long prom_boot_page
;
302 extern void prom_remap(unsigned long physpage
, unsigned long virtpage
, int mmu_ihandle
);
303 extern int prom_get_mmu_ihandle(void);
304 extern void register_prom_callbacks(void);
306 /* Exported for SMP bootup purposes. */
307 unsigned long kern_locked_tte_data
;
309 void __init
early_pgtable_allocfail(char *type
)
311 prom_printf("inherit_prom_mappings: Cannot alloc kernel %s.\n", type
);
315 #define BASE_PAGE_SIZE 8192
316 static pmd_t
*prompmd
;
319 * Translate PROM's mapping we capture at boot time into physical address.
320 * The second parameter is only set from prom_callback() invocations.
322 unsigned long prom_virt_to_phys(unsigned long promva
, int *error
)
324 pmd_t
*pmdp
= prompmd
+ ((promva
>> 23) & 0x7ff);
328 if (pmd_none(*pmdp
)) {
333 ptep
= (pte_t
*)__pmd_page(*pmdp
) + ((promva
>> 13) & 0x3ff);
334 if (!pte_present(*ptep
)) {
341 return(pte_val(*ptep
));
343 base
= pte_val(*ptep
) & _PAGE_PADDR
;
344 return(base
+ (promva
& (BASE_PAGE_SIZE
- 1)));
347 static void inherit_prom_mappings(void)
349 struct linux_prom_translation
*trans
;
350 unsigned long phys_page
, tte_vaddr
, tte_data
;
351 void (*remap_func
)(unsigned long, unsigned long, int);
355 extern unsigned int obp_iaddr_patch
[2], obp_daddr_patch
[2];
357 node
= prom_finddevice("/virtual-memory");
358 n
= prom_getproplen(node
, "translations");
359 if (n
== 0 || n
== -1) {
360 prom_printf("Couldn't get translation property\n");
363 n
+= 5 * sizeof(struct linux_prom_translation
);
364 for (tsz
= 1; tsz
< n
; tsz
<<= 1)
366 trans
= __alloc_bootmem(tsz
, SMP_CACHE_BYTES
, bootmap_base
);
368 prom_printf("inherit_prom_mappings: Cannot alloc translations.\n");
371 memset(trans
, 0, tsz
);
373 if ((n
= prom_getproperty(node
, "translations", (char *)trans
, tsz
)) == -1) {
374 prom_printf("Couldn't get translation property\n");
377 n
= n
/ sizeof(*trans
);
380 * The obp translations are saved based on 8k pagesize, since obp can
381 * use a mixture of pagesizes. Misses to the 0xf0000000 - 0x100000000,
382 * ie obp range, are handled in entry.S and do not use the vpte scheme
383 * (see rant in inherit_locked_prom_mappings()).
385 #define OBP_PMD_SIZE 2048
386 prompmd
= __alloc_bootmem(OBP_PMD_SIZE
, OBP_PMD_SIZE
, bootmap_base
);
388 early_pgtable_allocfail("pmd");
389 memset(prompmd
, 0, OBP_PMD_SIZE
);
390 for (i
= 0; i
< n
; i
++) {
393 if (trans
[i
].virt
>= LOW_OBP_ADDRESS
&& trans
[i
].virt
< HI_OBP_ADDRESS
) {
394 for (vaddr
= trans
[i
].virt
;
395 ((vaddr
< trans
[i
].virt
+ trans
[i
].size
) &&
396 (vaddr
< HI_OBP_ADDRESS
));
397 vaddr
+= BASE_PAGE_SIZE
) {
400 pmdp
= prompmd
+ ((vaddr
>> 23) & 0x7ff);
401 if (pmd_none(*pmdp
)) {
402 ptep
= __alloc_bootmem(BASE_PAGE_SIZE
,
406 early_pgtable_allocfail("pte");
407 memset(ptep
, 0, BASE_PAGE_SIZE
);
410 ptep
= (pte_t
*)__pmd_page(*pmdp
) +
411 ((vaddr
>> 13) & 0x3ff);
415 /* Clear diag TTE bits. */
416 if (tlb_type
== spitfire
)
417 val
&= ~0x0003fe0000000000UL
;
419 set_pte_at(&init_mm
, vaddr
,
420 ptep
, __pte(val
| _PAGE_MODIFIED
));
421 trans
[i
].data
+= BASE_PAGE_SIZE
;
425 phys_page
= __pa(prompmd
);
426 obp_iaddr_patch
[0] |= (phys_page
>> 10);
427 obp_iaddr_patch
[1] |= (phys_page
& 0x3ff);
428 flushi((long)&obp_iaddr_patch
[0]);
429 obp_daddr_patch
[0] |= (phys_page
>> 10);
430 obp_daddr_patch
[1] |= (phys_page
& 0x3ff);
431 flushi((long)&obp_daddr_patch
[0]);
433 /* Now fixup OBP's idea about where we really are mapped. */
434 prom_printf("Remapping the kernel... ");
436 /* Spitfire Errata #32 workaround */
437 /* NOTE: Using plain zero for the context value is
438 * correct here, we are not using the Linux trap
439 * tables yet so we should not use the special
440 * UltraSPARC-III+ page size encodings yet.
442 __asm__
__volatile__("stxa %0, [%1] %2\n\t"
445 : "r" (0), "r" (PRIMARY_CONTEXT
), "i" (ASI_DMMU
));
450 phys_page
= spitfire_get_dtlb_data(sparc64_highest_locked_tlbent());
455 phys_page
= cheetah_get_litlb_data(sparc64_highest_locked_tlbent());
459 phys_page
&= _PAGE_PADDR
;
460 phys_page
+= ((unsigned long)&prom_boot_page
-
461 (unsigned long)KERNBASE
);
463 if (tlb_type
== spitfire
) {
464 /* Lock this into i/d tlb entry 59 */
465 __asm__
__volatile__(
466 "stxa %%g0, [%2] %3\n\t"
467 "stxa %0, [%1] %4\n\t"
470 "stxa %%g0, [%2] %5\n\t"
471 "stxa %0, [%1] %6\n\t"
474 : : "r" (phys_page
| _PAGE_VALID
| _PAGE_SZ8K
| _PAGE_CP
|
475 _PAGE_CV
| _PAGE_P
| _PAGE_L
| _PAGE_W
),
476 "r" (59 << 3), "r" (TLB_TAG_ACCESS
),
477 "i" (ASI_DMMU
), "i" (ASI_DTLB_DATA_ACCESS
),
478 "i" (ASI_IMMU
), "i" (ASI_ITLB_DATA_ACCESS
)
480 } else if (tlb_type
== cheetah
|| tlb_type
== cheetah_plus
) {
481 /* Lock this into i/d tlb-0 entry 11 */
482 __asm__
__volatile__(
483 "stxa %%g0, [%2] %3\n\t"
484 "stxa %0, [%1] %4\n\t"
487 "stxa %%g0, [%2] %5\n\t"
488 "stxa %0, [%1] %6\n\t"
491 : : "r" (phys_page
| _PAGE_VALID
| _PAGE_SZ8K
| _PAGE_CP
|
492 _PAGE_CV
| _PAGE_P
| _PAGE_L
| _PAGE_W
),
493 "r" ((0 << 16) | (11 << 3)), "r" (TLB_TAG_ACCESS
),
494 "i" (ASI_DMMU
), "i" (ASI_DTLB_DATA_ACCESS
),
495 "i" (ASI_IMMU
), "i" (ASI_ITLB_DATA_ACCESS
)
498 /* Implement me :-) */
502 tte_vaddr
= (unsigned long) KERNBASE
;
504 /* Spitfire Errata #32 workaround */
505 /* NOTE: Using plain zero for the context value is
506 * correct here, we are not using the Linux trap
507 * tables yet so we should not use the special
508 * UltraSPARC-III+ page size encodings yet.
510 __asm__
__volatile__("stxa %0, [%1] %2\n\t"
514 "r" (PRIMARY_CONTEXT
), "i" (ASI_DMMU
));
516 if (tlb_type
== spitfire
)
517 tte_data
= spitfire_get_dtlb_data(sparc64_highest_locked_tlbent());
519 tte_data
= cheetah_get_ldtlb_data(sparc64_highest_locked_tlbent());
521 kern_locked_tte_data
= tte_data
;
523 remap_func
= (void *) ((unsigned long) &prom_remap
-
524 (unsigned long) &prom_boot_page
);
527 /* Spitfire Errata #32 workaround */
528 /* NOTE: Using plain zero for the context value is
529 * correct here, we are not using the Linux trap
530 * tables yet so we should not use the special
531 * UltraSPARC-III+ page size encodings yet.
533 __asm__
__volatile__("stxa %0, [%1] %2\n\t"
537 "r" (PRIMARY_CONTEXT
), "i" (ASI_DMMU
));
539 remap_func((tlb_type
== spitfire
?
540 (spitfire_get_dtlb_data(sparc64_highest_locked_tlbent()) & _PAGE_PADDR
) :
541 (cheetah_get_litlb_data(sparc64_highest_locked_tlbent()) & _PAGE_PADDR
)),
542 (unsigned long) KERNBASE
,
543 prom_get_mmu_ihandle());
546 remap_func(((tte_data
+ 0x400000) & _PAGE_PADDR
),
547 (unsigned long) KERNBASE
+ 0x400000, prom_get_mmu_ihandle());
549 /* Flush out that temporary mapping. */
550 spitfire_flush_dtlb_nucleus_page(0x0);
551 spitfire_flush_itlb_nucleus_page(0x0);
553 /* Now lock us back into the TLBs via OBP. */
554 prom_dtlb_load(sparc64_highest_locked_tlbent(), tte_data
, tte_vaddr
);
555 prom_itlb_load(sparc64_highest_locked_tlbent(), tte_data
, tte_vaddr
);
557 prom_dtlb_load(sparc64_highest_locked_tlbent()-1, tte_data
+ 0x400000,
558 tte_vaddr
+ 0x400000);
559 prom_itlb_load(sparc64_highest_locked_tlbent()-1, tte_data
+ 0x400000,
560 tte_vaddr
+ 0x400000);
563 /* Re-read translations property. */
564 if ((n
= prom_getproperty(node
, "translations", (char *)trans
, tsz
)) == -1) {
565 prom_printf("Couldn't get translation property\n");
568 n
= n
/ sizeof(*trans
);
570 for (i
= 0; i
< n
; i
++) {
571 unsigned long vaddr
= trans
[i
].virt
;
572 unsigned long size
= trans
[i
].size
;
574 if (vaddr
< 0xf0000000UL
) {
575 unsigned long avoid_start
= (unsigned long) KERNBASE
;
576 unsigned long avoid_end
= avoid_start
+ (4 * 1024 * 1024);
579 avoid_end
+= (4 * 1024 * 1024);
580 if (vaddr
< avoid_start
) {
581 unsigned long top
= vaddr
+ size
;
583 if (top
> avoid_start
)
585 prom_unmap(top
- vaddr
, vaddr
);
587 if ((vaddr
+ size
) > avoid_end
) {
588 unsigned long bottom
= vaddr
;
590 if (bottom
< avoid_end
)
592 prom_unmap((vaddr
+ size
) - bottom
, bottom
);
597 prom_printf("done.\n");
599 register_prom_callbacks();
602 /* The OBP specifications for sun4u mark 0xfffffffc00000000 and
603 * upwards as reserved for use by the firmware (I wonder if this
604 * will be the same on Cheetah...). We use this virtual address
605 * range for the VPTE table mappings of the nucleus so we need
606 * to zap them when we enter the PROM. -DaveM
608 static void __flush_nucleus_vptes(void)
610 unsigned long prom_reserved_base
= 0xfffffffc00000000UL
;
613 /* Only DTLB must be checked for VPTE entries. */
614 if (tlb_type
== spitfire
) {
615 for (i
= 0; i
< 63; i
++) {
618 /* Spitfire Errata #32 workaround */
619 /* NOTE: Always runs on spitfire, so no cheetah+
620 * page size encodings.
622 __asm__
__volatile__("stxa %0, [%1] %2\n\t"
626 "r" (PRIMARY_CONTEXT
), "i" (ASI_DMMU
));
628 tag
= spitfire_get_dtlb_tag(i
);
629 if (((tag
& ~(PAGE_MASK
)) == 0) &&
630 ((tag
& (PAGE_MASK
)) >= prom_reserved_base
)) {
631 __asm__
__volatile__("stxa %%g0, [%0] %1\n\t"
634 : "r" (TLB_TAG_ACCESS
), "i" (ASI_DMMU
));
635 spitfire_put_dtlb_data(i
, 0x0UL
);
638 } else if (tlb_type
== cheetah
|| tlb_type
== cheetah_plus
) {
639 for (i
= 0; i
< 512; i
++) {
640 unsigned long tag
= cheetah_get_dtlb_tag(i
, 2);
642 if ((tag
& ~PAGE_MASK
) == 0 &&
643 (tag
& PAGE_MASK
) >= prom_reserved_base
) {
644 __asm__
__volatile__("stxa %%g0, [%0] %1\n\t"
647 : "r" (TLB_TAG_ACCESS
), "i" (ASI_DMMU
));
648 cheetah_put_dtlb_data(i
, 0x0UL
, 2);
651 if (tlb_type
!= cheetah_plus
)
654 tag
= cheetah_get_dtlb_tag(i
, 3);
656 if ((tag
& ~PAGE_MASK
) == 0 &&
657 (tag
& PAGE_MASK
) >= prom_reserved_base
) {
658 __asm__
__volatile__("stxa %%g0, [%0] %1\n\t"
661 : "r" (TLB_TAG_ACCESS
), "i" (ASI_DMMU
));
662 cheetah_put_dtlb_data(i
, 0x0UL
, 3);
666 /* Implement me :-) */
671 static int prom_ditlb_set
;
672 struct prom_tlb_entry
{
674 unsigned long tlb_tag
;
675 unsigned long tlb_data
;
677 struct prom_tlb_entry prom_itlb
[16], prom_dtlb
[16];
679 void prom_world(int enter
)
681 unsigned long pstate
;
685 set_fs((mm_segment_t
) { get_thread_current_ds() });
690 /* Make sure the following runs atomically. */
691 __asm__
__volatile__("flushw\n\t"
692 "rdpr %%pstate, %0\n\t"
693 "wrpr %0, %1, %%pstate"
698 /* Kick out nucleus VPTEs. */
699 __flush_nucleus_vptes();
701 /* Install PROM world. */
702 for (i
= 0; i
< 16; i
++) {
703 if (prom_dtlb
[i
].tlb_ent
!= -1) {
704 __asm__
__volatile__("stxa %0, [%1] %2\n\t"
706 : : "r" (prom_dtlb
[i
].tlb_tag
), "r" (TLB_TAG_ACCESS
),
708 if (tlb_type
== spitfire
)
709 spitfire_put_dtlb_data(prom_dtlb
[i
].tlb_ent
,
710 prom_dtlb
[i
].tlb_data
);
711 else if (tlb_type
== cheetah
|| tlb_type
== cheetah_plus
)
712 cheetah_put_ldtlb_data(prom_dtlb
[i
].tlb_ent
,
713 prom_dtlb
[i
].tlb_data
);
715 if (prom_itlb
[i
].tlb_ent
!= -1) {
716 __asm__
__volatile__("stxa %0, [%1] %2\n\t"
718 : : "r" (prom_itlb
[i
].tlb_tag
),
719 "r" (TLB_TAG_ACCESS
),
721 if (tlb_type
== spitfire
)
722 spitfire_put_itlb_data(prom_itlb
[i
].tlb_ent
,
723 prom_itlb
[i
].tlb_data
);
724 else if (tlb_type
== cheetah
|| tlb_type
== cheetah_plus
)
725 cheetah_put_litlb_data(prom_itlb
[i
].tlb_ent
,
726 prom_itlb
[i
].tlb_data
);
730 for (i
= 0; i
< 16; i
++) {
731 if (prom_dtlb
[i
].tlb_ent
!= -1) {
732 __asm__
__volatile__("stxa %%g0, [%0] %1\n\t"
734 : : "r" (TLB_TAG_ACCESS
), "i" (ASI_DMMU
));
735 if (tlb_type
== spitfire
)
736 spitfire_put_dtlb_data(prom_dtlb
[i
].tlb_ent
, 0x0UL
);
738 cheetah_put_ldtlb_data(prom_dtlb
[i
].tlb_ent
, 0x0UL
);
740 if (prom_itlb
[i
].tlb_ent
!= -1) {
741 __asm__
__volatile__("stxa %%g0, [%0] %1\n\t"
743 : : "r" (TLB_TAG_ACCESS
),
745 if (tlb_type
== spitfire
)
746 spitfire_put_itlb_data(prom_itlb
[i
].tlb_ent
, 0x0UL
);
748 cheetah_put_litlb_data(prom_itlb
[i
].tlb_ent
, 0x0UL
);
752 __asm__
__volatile__("wrpr %0, 0, %%pstate"
756 void inherit_locked_prom_mappings(int save_p
)
762 /* Fucking losing PROM has more mappings in the TLB, but
763 * it (conveniently) fails to mention any of these in the
764 * translations property. The only ones that matter are
765 * the locked PROM tlb entries, so we impose the following
766 * irrecovable rule on the PROM, it is allowed 8 locked
767 * entries in the ITLB and 8 in the DTLB.
769 * Supposedly the upper 16GB of the address space is
770 * reserved for OBP, BUT I WISH THIS WAS DOCUMENTED
771 * SOMEWHERE!!!!!!!!!!!!!!!!! Furthermore the entire interface
772 * used between the client program and the firmware on sun5
773 * systems to coordinate mmu mappings is also COMPLETELY
774 * UNDOCUMENTED!!!!!! Thanks S(t)un!
777 for (i
= 0; i
< 16; i
++) {
778 prom_itlb
[i
].tlb_ent
= -1;
779 prom_dtlb
[i
].tlb_ent
= -1;
782 if (tlb_type
== spitfire
) {
783 int high
= SPITFIRE_HIGHEST_LOCKED_TLBENT
- bigkernel
;
784 for (i
= 0; i
< high
; i
++) {
787 /* Spitfire Errata #32 workaround */
788 /* NOTE: Always runs on spitfire, so no cheetah+
789 * page size encodings.
791 __asm__
__volatile__("stxa %0, [%1] %2\n\t"
795 "r" (PRIMARY_CONTEXT
), "i" (ASI_DMMU
));
797 data
= spitfire_get_dtlb_data(i
);
798 if ((data
& (_PAGE_L
|_PAGE_VALID
)) == (_PAGE_L
|_PAGE_VALID
)) {
801 /* Spitfire Errata #32 workaround */
802 /* NOTE: Always runs on spitfire, so no
803 * cheetah+ page size encodings.
805 __asm__
__volatile__("stxa %0, [%1] %2\n\t"
809 "r" (PRIMARY_CONTEXT
), "i" (ASI_DMMU
));
811 tag
= spitfire_get_dtlb_tag(i
);
813 prom_dtlb
[dtlb_seen
].tlb_ent
= i
;
814 prom_dtlb
[dtlb_seen
].tlb_tag
= tag
;
815 prom_dtlb
[dtlb_seen
].tlb_data
= data
;
817 __asm__
__volatile__("stxa %%g0, [%0] %1\n\t"
819 : : "r" (TLB_TAG_ACCESS
), "i" (ASI_DMMU
));
820 spitfire_put_dtlb_data(i
, 0x0UL
);
828 for (i
= 0; i
< high
; i
++) {
831 /* Spitfire Errata #32 workaround */
832 /* NOTE: Always runs on spitfire, so no
833 * cheetah+ page size encodings.
835 __asm__
__volatile__("stxa %0, [%1] %2\n\t"
839 "r" (PRIMARY_CONTEXT
), "i" (ASI_DMMU
));
841 data
= spitfire_get_itlb_data(i
);
842 if ((data
& (_PAGE_L
|_PAGE_VALID
)) == (_PAGE_L
|_PAGE_VALID
)) {
845 /* Spitfire Errata #32 workaround */
846 /* NOTE: Always runs on spitfire, so no
847 * cheetah+ page size encodings.
849 __asm__
__volatile__("stxa %0, [%1] %2\n\t"
853 "r" (PRIMARY_CONTEXT
), "i" (ASI_DMMU
));
855 tag
= spitfire_get_itlb_tag(i
);
857 prom_itlb
[itlb_seen
].tlb_ent
= i
;
858 prom_itlb
[itlb_seen
].tlb_tag
= tag
;
859 prom_itlb
[itlb_seen
].tlb_data
= data
;
861 __asm__
__volatile__("stxa %%g0, [%0] %1\n\t"
863 : : "r" (TLB_TAG_ACCESS
), "i" (ASI_IMMU
));
864 spitfire_put_itlb_data(i
, 0x0UL
);
871 } else if (tlb_type
== cheetah
|| tlb_type
== cheetah_plus
) {
872 int high
= CHEETAH_HIGHEST_LOCKED_TLBENT
- bigkernel
;
874 for (i
= 0; i
< high
; i
++) {
877 data
= cheetah_get_ldtlb_data(i
);
878 if ((data
& (_PAGE_L
|_PAGE_VALID
)) == (_PAGE_L
|_PAGE_VALID
)) {
881 tag
= cheetah_get_ldtlb_tag(i
);
883 prom_dtlb
[dtlb_seen
].tlb_ent
= i
;
884 prom_dtlb
[dtlb_seen
].tlb_tag
= tag
;
885 prom_dtlb
[dtlb_seen
].tlb_data
= data
;
887 __asm__
__volatile__("stxa %%g0, [%0] %1\n\t"
889 : : "r" (TLB_TAG_ACCESS
), "i" (ASI_DMMU
));
890 cheetah_put_ldtlb_data(i
, 0x0UL
);
898 for (i
= 0; i
< high
; i
++) {
901 data
= cheetah_get_litlb_data(i
);
902 if ((data
& (_PAGE_L
|_PAGE_VALID
)) == (_PAGE_L
|_PAGE_VALID
)) {
905 tag
= cheetah_get_litlb_tag(i
);
907 prom_itlb
[itlb_seen
].tlb_ent
= i
;
908 prom_itlb
[itlb_seen
].tlb_tag
= tag
;
909 prom_itlb
[itlb_seen
].tlb_data
= data
;
911 __asm__
__volatile__("stxa %%g0, [%0] %1\n\t"
913 : : "r" (TLB_TAG_ACCESS
), "i" (ASI_IMMU
));
914 cheetah_put_litlb_data(i
, 0x0UL
);
922 /* Implement me :-) */
929 /* Give PROM back his world, done during reboots... */
930 void prom_reload_locked(void)
934 for (i
= 0; i
< 16; i
++) {
935 if (prom_dtlb
[i
].tlb_ent
!= -1) {
936 __asm__
__volatile__("stxa %0, [%1] %2\n\t"
938 : : "r" (prom_dtlb
[i
].tlb_tag
), "r" (TLB_TAG_ACCESS
),
940 if (tlb_type
== spitfire
)
941 spitfire_put_dtlb_data(prom_dtlb
[i
].tlb_ent
,
942 prom_dtlb
[i
].tlb_data
);
943 else if (tlb_type
== cheetah
|| tlb_type
== cheetah_plus
)
944 cheetah_put_ldtlb_data(prom_dtlb
[i
].tlb_ent
,
945 prom_dtlb
[i
].tlb_data
);
948 if (prom_itlb
[i
].tlb_ent
!= -1) {
949 __asm__
__volatile__("stxa %0, [%1] %2\n\t"
951 : : "r" (prom_itlb
[i
].tlb_tag
),
952 "r" (TLB_TAG_ACCESS
),
954 if (tlb_type
== spitfire
)
955 spitfire_put_itlb_data(prom_itlb
[i
].tlb_ent
,
956 prom_itlb
[i
].tlb_data
);
958 cheetah_put_litlb_data(prom_itlb
[i
].tlb_ent
,
959 prom_itlb
[i
].tlb_data
);
964 #ifdef DCACHE_ALIASING_POSSIBLE
965 void __flush_dcache_range(unsigned long start
, unsigned long end
)
969 if (tlb_type
== spitfire
) {
972 for (va
= start
; va
< end
; va
+= 32) {
973 spitfire_put_dcache_tag(va
& 0x3fe0, 0x0);
980 for (va
= start
; va
< end
; va
+= 32)
981 __asm__
__volatile__("stxa %%g0, [%0] %1\n\t"
985 "i" (ASI_DCACHE_INVALIDATE
));
988 #endif /* DCACHE_ALIASING_POSSIBLE */
990 /* If not locked, zap it. */
991 void __flush_tlb_all(void)
993 unsigned long pstate
;
996 __asm__
__volatile__("flushw\n\t"
997 "rdpr %%pstate, %0\n\t"
998 "wrpr %0, %1, %%pstate"
1001 if (tlb_type
== spitfire
) {
1002 for (i
= 0; i
< 64; i
++) {
1003 /* Spitfire Errata #32 workaround */
1004 /* NOTE: Always runs on spitfire, so no
1005 * cheetah+ page size encodings.
1007 __asm__
__volatile__("stxa %0, [%1] %2\n\t"
1011 "r" (PRIMARY_CONTEXT
), "i" (ASI_DMMU
));
1013 if (!(spitfire_get_dtlb_data(i
) & _PAGE_L
)) {
1014 __asm__
__volatile__("stxa %%g0, [%0] %1\n\t"
1017 : "r" (TLB_TAG_ACCESS
), "i" (ASI_DMMU
));
1018 spitfire_put_dtlb_data(i
, 0x0UL
);
1021 /* Spitfire Errata #32 workaround */
1022 /* NOTE: Always runs on spitfire, so no
1023 * cheetah+ page size encodings.
1025 __asm__
__volatile__("stxa %0, [%1] %2\n\t"
1029 "r" (PRIMARY_CONTEXT
), "i" (ASI_DMMU
));
1031 if (!(spitfire_get_itlb_data(i
) & _PAGE_L
)) {
1032 __asm__
__volatile__("stxa %%g0, [%0] %1\n\t"
1035 : "r" (TLB_TAG_ACCESS
), "i" (ASI_IMMU
));
1036 spitfire_put_itlb_data(i
, 0x0UL
);
1039 } else if (tlb_type
== cheetah
|| tlb_type
== cheetah_plus
) {
1040 cheetah_flush_dtlb_all();
1041 cheetah_flush_itlb_all();
1043 __asm__
__volatile__("wrpr %0, 0, %%pstate"
1047 /* Caller does TLB context flushing on local CPU if necessary.
1048 * The caller also ensures that CTX_VALID(mm->context) is false.
1050 * We must be careful about boundary cases so that we never
1051 * let the user have CTX 0 (nucleus) or we ever use a CTX
1052 * version of zero (and thus NO_CONTEXT would not be caught
1053 * by version mis-match tests in mmu_context.h).
1055 void get_new_mmu_context(struct mm_struct
*mm
)
1057 unsigned long ctx
, new_ctx
;
1058 unsigned long orig_pgsz_bits
;
1061 spin_lock(&ctx_alloc_lock
);
1062 orig_pgsz_bits
= (mm
->context
.sparc64_ctx_val
& CTX_PGSZ_MASK
);
1063 ctx
= (tlb_context_cache
+ 1) & CTX_NR_MASK
;
1064 new_ctx
= find_next_zero_bit(mmu_context_bmap
, 1 << CTX_NR_BITS
, ctx
);
1065 if (new_ctx
>= (1 << CTX_NR_BITS
)) {
1066 new_ctx
= find_next_zero_bit(mmu_context_bmap
, ctx
, 1);
1067 if (new_ctx
>= ctx
) {
1069 new_ctx
= (tlb_context_cache
& CTX_VERSION_MASK
) +
1072 new_ctx
= CTX_FIRST_VERSION
;
1074 /* Don't call memset, for 16 entries that's just
1077 mmu_context_bmap
[0] = 3;
1078 mmu_context_bmap
[1] = 0;
1079 mmu_context_bmap
[2] = 0;
1080 mmu_context_bmap
[3] = 0;
1081 for (i
= 4; i
< CTX_BMAP_SLOTS
; i
+= 4) {
1082 mmu_context_bmap
[i
+ 0] = 0;
1083 mmu_context_bmap
[i
+ 1] = 0;
1084 mmu_context_bmap
[i
+ 2] = 0;
1085 mmu_context_bmap
[i
+ 3] = 0;
1090 mmu_context_bmap
[new_ctx
>>6] |= (1UL << (new_ctx
& 63));
1091 new_ctx
|= (tlb_context_cache
& CTX_VERSION_MASK
);
1093 tlb_context_cache
= new_ctx
;
1094 mm
->context
.sparc64_ctx_val
= new_ctx
| orig_pgsz_bits
;
1095 spin_unlock(&ctx_alloc_lock
);
1099 struct pgtable_cache_struct pgt_quicklists
;
1102 /* OK, we have to color these pages. The page tables are accessed
1103 * by non-Dcache enabled mapping in the VPTE area by the dtlb_backend.S
1104 * code, as well as by PAGE_OFFSET range direct-mapped addresses by
1105 * other parts of the kernel. By coloring, we make sure that the tlbmiss
1106 * fast handlers do not get data from old/garbage dcache lines that
1107 * correspond to an old/stale virtual address (user/kernel) that
1108 * previously mapped the pagetable page while accessing vpte range
1109 * addresses. The idea is that if the vpte color and PAGE_OFFSET range
1110 * color is the same, then when the kernel initializes the pagetable
1111 * using the later address range, accesses with the first address
1112 * range will see the newly initialized data rather than the garbage.
1114 #ifdef DCACHE_ALIASING_POSSIBLE
1115 #define DC_ALIAS_SHIFT 1
1117 #define DC_ALIAS_SHIFT 0
1119 pte_t
*pte_alloc_one_kernel(struct mm_struct
*mm
, unsigned long address
)
1122 unsigned long color
;
1125 pte_t
*ptep
= pte_alloc_one_fast(mm
, address
);
1131 color
= VPTE_COLOR(address
);
1132 page
= alloc_pages(GFP_KERNEL
|__GFP_REPEAT
, DC_ALIAS_SHIFT
);
1134 unsigned long *to_free
;
1135 unsigned long paddr
;
1138 #ifdef DCACHE_ALIASING_POSSIBLE
1139 set_page_count(page
, 1);
1140 ClearPageCompound(page
);
1142 set_page_count((page
+ 1), 1);
1143 ClearPageCompound(page
+ 1);
1145 paddr
= (unsigned long) page_address(page
);
1146 memset((char *)paddr
, 0, (PAGE_SIZE
<< DC_ALIAS_SHIFT
));
1149 pte
= (pte_t
*) paddr
;
1150 to_free
= (unsigned long *) (paddr
+ PAGE_SIZE
);
1152 pte
= (pte_t
*) (paddr
+ PAGE_SIZE
);
1153 to_free
= (unsigned long *) paddr
;
1156 #ifdef DCACHE_ALIASING_POSSIBLE
1157 /* Now free the other one up, adjust cache size. */
1159 *to_free
= (unsigned long) pte_quicklist
[color
^ 0x1];
1160 pte_quicklist
[color
^ 0x1] = to_free
;
1161 pgtable_cache_size
++;
1170 void sparc_ultra_dump_itlb(void)
1174 if (tlb_type
== spitfire
) {
1175 printk ("Contents of itlb: ");
1176 for (slot
= 0; slot
< 14; slot
++) printk (" ");
1177 printk ("%2x:%016lx,%016lx\n",
1179 spitfire_get_itlb_tag(0), spitfire_get_itlb_data(0));
1180 for (slot
= 1; slot
< 64; slot
+=3) {
1181 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx %2x:%016lx,%016lx\n",
1183 spitfire_get_itlb_tag(slot
), spitfire_get_itlb_data(slot
),
1185 spitfire_get_itlb_tag(slot
+1), spitfire_get_itlb_data(slot
+1),
1187 spitfire_get_itlb_tag(slot
+2), spitfire_get_itlb_data(slot
+2));
1189 } else if (tlb_type
== cheetah
|| tlb_type
== cheetah_plus
) {
1190 printk ("Contents of itlb0:\n");
1191 for (slot
= 0; slot
< 16; slot
+=2) {
1192 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
1194 cheetah_get_litlb_tag(slot
), cheetah_get_litlb_data(slot
),
1196 cheetah_get_litlb_tag(slot
+1), cheetah_get_litlb_data(slot
+1));
1198 printk ("Contents of itlb2:\n");
1199 for (slot
= 0; slot
< 128; slot
+=2) {
1200 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
1202 cheetah_get_itlb_tag(slot
), cheetah_get_itlb_data(slot
),
1204 cheetah_get_itlb_tag(slot
+1), cheetah_get_itlb_data(slot
+1));
1209 void sparc_ultra_dump_dtlb(void)
1213 if (tlb_type
== spitfire
) {
1214 printk ("Contents of dtlb: ");
1215 for (slot
= 0; slot
< 14; slot
++) printk (" ");
1216 printk ("%2x:%016lx,%016lx\n", 0,
1217 spitfire_get_dtlb_tag(0), spitfire_get_dtlb_data(0));
1218 for (slot
= 1; slot
< 64; slot
+=3) {
1219 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx %2x:%016lx,%016lx\n",
1221 spitfire_get_dtlb_tag(slot
), spitfire_get_dtlb_data(slot
),
1223 spitfire_get_dtlb_tag(slot
+1), spitfire_get_dtlb_data(slot
+1),
1225 spitfire_get_dtlb_tag(slot
+2), spitfire_get_dtlb_data(slot
+2));
1227 } else if (tlb_type
== cheetah
|| tlb_type
== cheetah_plus
) {
1228 printk ("Contents of dtlb0:\n");
1229 for (slot
= 0; slot
< 16; slot
+=2) {
1230 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
1232 cheetah_get_ldtlb_tag(slot
), cheetah_get_ldtlb_data(slot
),
1234 cheetah_get_ldtlb_tag(slot
+1), cheetah_get_ldtlb_data(slot
+1));
1236 printk ("Contents of dtlb2:\n");
1237 for (slot
= 0; slot
< 512; slot
+=2) {
1238 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
1240 cheetah_get_dtlb_tag(slot
, 2), cheetah_get_dtlb_data(slot
, 2),
1242 cheetah_get_dtlb_tag(slot
+1, 2), cheetah_get_dtlb_data(slot
+1, 2));
1244 if (tlb_type
== cheetah_plus
) {
1245 printk ("Contents of dtlb3:\n");
1246 for (slot
= 0; slot
< 512; slot
+=2) {
1247 printk ("%2x:%016lx,%016lx %2x:%016lx,%016lx\n",
1249 cheetah_get_dtlb_tag(slot
, 3), cheetah_get_dtlb_data(slot
, 3),
1251 cheetah_get_dtlb_tag(slot
+1, 3), cheetah_get_dtlb_data(slot
+1, 3));
1257 extern unsigned long cmdline_memory_size
;
1259 unsigned long __init
bootmem_init(unsigned long *pages_avail
)
1261 unsigned long bootmap_size
, start_pfn
, end_pfn
;
1262 unsigned long end_of_phys_memory
= 0UL;
1263 unsigned long bootmap_pfn
, bytes_avail
, size
;
1266 #ifdef CONFIG_DEBUG_BOOTMEM
1267 prom_printf("bootmem_init: Scan sp_banks, ");
1271 for (i
= 0; sp_banks
[i
].num_bytes
!= 0; i
++) {
1272 end_of_phys_memory
= sp_banks
[i
].base_addr
+
1273 sp_banks
[i
].num_bytes
;
1274 bytes_avail
+= sp_banks
[i
].num_bytes
;
1275 if (cmdline_memory_size
) {
1276 if (bytes_avail
> cmdline_memory_size
) {
1277 unsigned long slack
= bytes_avail
- cmdline_memory_size
;
1279 bytes_avail
-= slack
;
1280 end_of_phys_memory
-= slack
;
1282 sp_banks
[i
].num_bytes
-= slack
;
1283 if (sp_banks
[i
].num_bytes
== 0) {
1284 sp_banks
[i
].base_addr
= 0xdeadbeef;
1286 sp_banks
[i
+1].num_bytes
= 0;
1287 sp_banks
[i
+1].base_addr
= 0xdeadbeef;
1294 *pages_avail
= bytes_avail
>> PAGE_SHIFT
;
1296 /* Start with page aligned address of last symbol in kernel
1297 * image. The kernel is hard mapped below PAGE_OFFSET in a
1298 * 4MB locked TLB translation.
1300 start_pfn
= PAGE_ALIGN(kern_base
+ kern_size
) >> PAGE_SHIFT
;
1302 bootmap_pfn
= start_pfn
;
1304 end_pfn
= end_of_phys_memory
>> PAGE_SHIFT
;
1306 #ifdef CONFIG_BLK_DEV_INITRD
1307 /* Now have to check initial ramdisk, so that bootmap does not overwrite it */
1308 if (sparc_ramdisk_image
|| sparc_ramdisk_image64
) {
1309 unsigned long ramdisk_image
= sparc_ramdisk_image
?
1310 sparc_ramdisk_image
: sparc_ramdisk_image64
;
1311 if (ramdisk_image
>= (unsigned long)_end
- 2 * PAGE_SIZE
)
1312 ramdisk_image
-= KERNBASE
;
1313 initrd_start
= ramdisk_image
+ phys_base
;
1314 initrd_end
= initrd_start
+ sparc_ramdisk_size
;
1315 if (initrd_end
> end_of_phys_memory
) {
1316 printk(KERN_CRIT
"initrd extends beyond end of memory "
1317 "(0x%016lx > 0x%016lx)\ndisabling initrd\n",
1318 initrd_end
, end_of_phys_memory
);
1322 if (initrd_start
>= (start_pfn
<< PAGE_SHIFT
) &&
1323 initrd_start
< (start_pfn
<< PAGE_SHIFT
) + 2 * PAGE_SIZE
)
1324 bootmap_pfn
= PAGE_ALIGN (initrd_end
) >> PAGE_SHIFT
;
1328 /* Initialize the boot-time allocator. */
1329 max_pfn
= max_low_pfn
= end_pfn
;
1330 min_low_pfn
= pfn_base
;
1332 #ifdef CONFIG_DEBUG_BOOTMEM
1333 prom_printf("init_bootmem(min[%lx], bootmap[%lx], max[%lx])\n",
1334 min_low_pfn
, bootmap_pfn
, max_low_pfn
);
1336 bootmap_size
= init_bootmem_node(NODE_DATA(0), bootmap_pfn
, pfn_base
, end_pfn
);
1338 bootmap_base
= bootmap_pfn
<< PAGE_SHIFT
;
1340 /* Now register the available physical memory with the
1343 for (i
= 0; sp_banks
[i
].num_bytes
!= 0; i
++) {
1344 #ifdef CONFIG_DEBUG_BOOTMEM
1345 prom_printf("free_bootmem(sp_banks:%d): base[%lx] size[%lx]\n",
1346 i
, sp_banks
[i
].base_addr
, sp_banks
[i
].num_bytes
);
1348 free_bootmem(sp_banks
[i
].base_addr
, sp_banks
[i
].num_bytes
);
1351 #ifdef CONFIG_BLK_DEV_INITRD
1353 size
= initrd_end
- initrd_start
;
1355 /* Resert the initrd image area. */
1356 #ifdef CONFIG_DEBUG_BOOTMEM
1357 prom_printf("reserve_bootmem(initrd): base[%llx] size[%lx]\n",
1358 initrd_start
, initrd_end
);
1360 reserve_bootmem(initrd_start
, size
);
1361 *pages_avail
-= PAGE_ALIGN(size
) >> PAGE_SHIFT
;
1363 initrd_start
+= PAGE_OFFSET
;
1364 initrd_end
+= PAGE_OFFSET
;
1367 /* Reserve the kernel text/data/bss. */
1368 #ifdef CONFIG_DEBUG_BOOTMEM
1369 prom_printf("reserve_bootmem(kernel): base[%lx] size[%lx]\n", kern_base
, kern_size
);
1371 reserve_bootmem(kern_base
, kern_size
);
1372 *pages_avail
-= PAGE_ALIGN(kern_size
) >> PAGE_SHIFT
;
1374 /* Reserve the bootmem map. We do not account for it
1375 * in pages_avail because we will release that memory
1376 * in free_all_bootmem.
1378 size
= bootmap_size
;
1379 #ifdef CONFIG_DEBUG_BOOTMEM
1380 prom_printf("reserve_bootmem(bootmap): base[%lx] size[%lx]\n",
1381 (bootmap_pfn
<< PAGE_SHIFT
), size
);
1383 reserve_bootmem((bootmap_pfn
<< PAGE_SHIFT
), size
);
1384 *pages_avail
-= PAGE_ALIGN(size
) >> PAGE_SHIFT
;
1389 /* paging_init() sets up the page tables */
1391 extern void cheetah_ecache_flush_init(void);
1393 static unsigned long last_valid_pfn
;
1395 void __init
paging_init(void)
1397 extern pmd_t swapper_pmd_dir
[1024];
1398 extern unsigned int sparc64_vpte_patchme1
[1];
1399 extern unsigned int sparc64_vpte_patchme2
[1];
1400 unsigned long alias_base
= kern_base
+ PAGE_OFFSET
;
1401 unsigned long second_alias_page
= 0;
1402 unsigned long pt
, flags
, end_pfn
, pages_avail
;
1403 unsigned long shift
= alias_base
- ((unsigned long)KERNBASE
);
1404 unsigned long real_end
;
1406 set_bit(0, mmu_context_bmap
);
1408 real_end
= (unsigned long)_end
;
1409 if ((real_end
> ((unsigned long)KERNBASE
+ 0x400000)))
1411 #ifdef CONFIG_BLK_DEV_INITRD
1412 if (sparc_ramdisk_image
|| sparc_ramdisk_image64
)
1413 real_end
= (PAGE_ALIGN(real_end
) + PAGE_ALIGN(sparc_ramdisk_size
));
1416 /* We assume physical memory starts at some 4mb multiple,
1417 * if this were not true we wouldn't boot up to this point
1420 pt
= kern_base
| _PAGE_VALID
| _PAGE_SZ4MB
;
1421 pt
|= _PAGE_CP
| _PAGE_CV
| _PAGE_P
| _PAGE_L
| _PAGE_W
;
1422 local_irq_save(flags
);
1423 if (tlb_type
== spitfire
) {
1424 __asm__
__volatile__(
1425 " stxa %1, [%0] %3\n"
1426 " stxa %2, [%5] %4\n"
1433 : "r" (TLB_TAG_ACCESS
), "r" (alias_base
), "r" (pt
),
1434 "i" (ASI_DMMU
), "i" (ASI_DTLB_DATA_ACCESS
), "r" (61 << 3)
1436 if (real_end
>= KERNBASE
+ 0x340000) {
1437 second_alias_page
= alias_base
+ 0x400000;
1438 __asm__
__volatile__(
1439 " stxa %1, [%0] %3\n"
1440 " stxa %2, [%5] %4\n"
1447 : "r" (TLB_TAG_ACCESS
), "r" (second_alias_page
), "r" (pt
+ 0x400000),
1448 "i" (ASI_DMMU
), "i" (ASI_DTLB_DATA_ACCESS
), "r" (60 << 3)
1451 } else if (tlb_type
== cheetah
|| tlb_type
== cheetah_plus
) {
1452 __asm__
__volatile__(
1453 " stxa %1, [%0] %3\n"
1454 " stxa %2, [%5] %4\n"
1461 : "r" (TLB_TAG_ACCESS
), "r" (alias_base
), "r" (pt
),
1462 "i" (ASI_DMMU
), "i" (ASI_DTLB_DATA_ACCESS
), "r" ((0<<16) | (13<<3))
1464 if (real_end
>= KERNBASE
+ 0x340000) {
1465 second_alias_page
= alias_base
+ 0x400000;
1466 __asm__
__volatile__(
1467 " stxa %1, [%0] %3\n"
1468 " stxa %2, [%5] %4\n"
1475 : "r" (TLB_TAG_ACCESS
), "r" (second_alias_page
), "r" (pt
+ 0x400000),
1476 "i" (ASI_DMMU
), "i" (ASI_DTLB_DATA_ACCESS
), "r" ((0<<16) | (12<<3))
1480 local_irq_restore(flags
);
1482 /* Now set kernel pgd to upper alias so physical page computations
1485 init_mm
.pgd
+= ((shift
) / (sizeof(pgd_t
)));
1487 memset(swapper_pmd_dir
, 0, sizeof(swapper_pmd_dir
));
1489 /* Now can init the kernel/bad page tables. */
1490 pud_set(pud_offset(&swapper_pg_dir
[0], 0),
1491 swapper_pmd_dir
+ (shift
/ sizeof(pgd_t
)));
1493 sparc64_vpte_patchme1
[0] |=
1494 (((unsigned long)pgd_val(init_mm
.pgd
[0])) >> 10);
1495 sparc64_vpte_patchme2
[0] |=
1496 (((unsigned long)pgd_val(init_mm
.pgd
[0])) & 0x3ff);
1497 flushi((long)&sparc64_vpte_patchme1
[0]);
1499 /* Setup bootmem... */
1501 last_valid_pfn
= end_pfn
= bootmem_init(&pages_avail
);
1503 /* Inherit non-locked OBP mappings. */
1504 inherit_prom_mappings();
1506 /* Ok, we can use our TLB miss and window trap handlers safely.
1507 * We need to do a quick peek here to see if we are on StarFire
1508 * or not, so setup_tba can setup the IRQ globals correctly (it
1509 * needs to get the hard smp processor id correctly).
1512 extern void setup_tba(int);
1513 setup_tba(this_is_starfire
);
1516 inherit_locked_prom_mappings(1);
1518 /* We only created DTLB mapping of this stuff. */
1519 spitfire_flush_dtlb_nucleus_page(alias_base
);
1520 if (second_alias_page
)
1521 spitfire_flush_dtlb_nucleus_page(second_alias_page
);
1526 unsigned long zones_size
[MAX_NR_ZONES
];
1527 unsigned long zholes_size
[MAX_NR_ZONES
];
1528 unsigned long npages
;
1531 for (znum
= 0; znum
< MAX_NR_ZONES
; znum
++)
1532 zones_size
[znum
] = zholes_size
[znum
] = 0;
1534 npages
= end_pfn
- pfn_base
;
1535 zones_size
[ZONE_DMA
] = npages
;
1536 zholes_size
[ZONE_DMA
] = npages
- pages_avail
;
1538 free_area_init_node(0, &contig_page_data
, zones_size
,
1539 phys_base
>> PAGE_SHIFT
, zholes_size
);
1545 /* Ok, it seems that the prom can allocate some more memory chunks
1546 * as a side effect of some prom calls we perform during the
1547 * boot sequence. My most likely theory is that it is from the
1548 * prom_set_traptable() call, and OBP is allocating a scratchpad
1549 * for saving client program register state etc.
1551 static void __init
sort_memlist(struct linux_mlist_p1275
*thislist
)
1555 unsigned long tmpaddr
, tmpsize
;
1556 unsigned long lowest
;
1558 for (i
= 0; thislist
[i
].theres_more
!= 0; i
++) {
1559 lowest
= thislist
[i
].start_adr
;
1560 for (mitr
= i
+1; thislist
[mitr
-1].theres_more
!= 0; mitr
++)
1561 if (thislist
[mitr
].start_adr
< lowest
) {
1562 lowest
= thislist
[mitr
].start_adr
;
1565 if (lowest
== thislist
[i
].start_adr
)
1567 tmpaddr
= thislist
[swapi
].start_adr
;
1568 tmpsize
= thislist
[swapi
].num_bytes
;
1569 for (mitr
= swapi
; mitr
> i
; mitr
--) {
1570 thislist
[mitr
].start_adr
= thislist
[mitr
-1].start_adr
;
1571 thislist
[mitr
].num_bytes
= thislist
[mitr
-1].num_bytes
;
1573 thislist
[i
].start_adr
= tmpaddr
;
1574 thislist
[i
].num_bytes
= tmpsize
;
1578 void __init
rescan_sp_banks(void)
1580 struct linux_prom64_registers memlist
[64];
1581 struct linux_mlist_p1275 avail
[64], *mlist
;
1582 unsigned long bytes
, base_paddr
;
1583 int num_regs
, node
= prom_finddevice("/memory");
1586 num_regs
= prom_getproperty(node
, "available",
1587 (char *) memlist
, sizeof(memlist
));
1588 num_regs
= (num_regs
/ sizeof(struct linux_prom64_registers
));
1589 for (i
= 0; i
< num_regs
; i
++) {
1590 avail
[i
].start_adr
= memlist
[i
].phys_addr
;
1591 avail
[i
].num_bytes
= memlist
[i
].reg_size
;
1592 avail
[i
].theres_more
= &avail
[i
+ 1];
1594 avail
[i
- 1].theres_more
= NULL
;
1595 sort_memlist(avail
);
1599 bytes
= mlist
->num_bytes
;
1600 base_paddr
= mlist
->start_adr
;
1602 sp_banks
[0].base_addr
= base_paddr
;
1603 sp_banks
[0].num_bytes
= bytes
;
1605 while (mlist
->theres_more
!= NULL
){
1607 mlist
= mlist
->theres_more
;
1608 bytes
= mlist
->num_bytes
;
1609 if (i
>= SPARC_PHYS_BANKS
-1) {
1610 printk ("The machine has more banks than "
1611 "this kernel can support\n"
1612 "Increase the SPARC_PHYS_BANKS "
1613 "setting (currently %d)\n",
1615 i
= SPARC_PHYS_BANKS
-1;
1619 sp_banks
[i
].base_addr
= mlist
->start_adr
;
1620 sp_banks
[i
].num_bytes
= mlist
->num_bytes
;
1624 sp_banks
[i
].base_addr
= 0xdeadbeefbeefdeadUL
;
1625 sp_banks
[i
].num_bytes
= 0;
1627 for (i
= 0; sp_banks
[i
].num_bytes
!= 0; i
++)
1628 sp_banks
[i
].num_bytes
&= PAGE_MASK
;
1631 static void __init
taint_real_pages(void)
1633 struct sparc_phys_banks saved_sp_banks
[SPARC_PHYS_BANKS
];
1636 for (i
= 0; i
< SPARC_PHYS_BANKS
; i
++) {
1637 saved_sp_banks
[i
].base_addr
=
1638 sp_banks
[i
].base_addr
;
1639 saved_sp_banks
[i
].num_bytes
=
1640 sp_banks
[i
].num_bytes
;
1645 /* Find changes discovered in the sp_bank rescan and
1646 * reserve the lost portions in the bootmem maps.
1648 for (i
= 0; saved_sp_banks
[i
].num_bytes
; i
++) {
1649 unsigned long old_start
, old_end
;
1651 old_start
= saved_sp_banks
[i
].base_addr
;
1652 old_end
= old_start
+
1653 saved_sp_banks
[i
].num_bytes
;
1654 while (old_start
< old_end
) {
1657 for (n
= 0; sp_banks
[n
].num_bytes
; n
++) {
1658 unsigned long new_start
, new_end
;
1660 new_start
= sp_banks
[n
].base_addr
;
1661 new_end
= new_start
+ sp_banks
[n
].num_bytes
;
1663 if (new_start
<= old_start
&&
1664 new_end
>= (old_start
+ PAGE_SIZE
)) {
1665 set_bit (old_start
>> 22,
1666 sparc64_valid_addr_bitmap
);
1670 reserve_bootmem(old_start
, PAGE_SIZE
);
1673 old_start
+= PAGE_SIZE
;
1678 void __init
mem_init(void)
1680 unsigned long codepages
, datapages
, initpages
;
1681 unsigned long addr
, last
;
1684 i
= last_valid_pfn
>> ((22 - PAGE_SHIFT
) + 6);
1686 sparc64_valid_addr_bitmap
= (unsigned long *)
1687 __alloc_bootmem(i
<< 3, SMP_CACHE_BYTES
, bootmap_base
);
1688 if (sparc64_valid_addr_bitmap
== NULL
) {
1689 prom_printf("mem_init: Cannot alloc valid_addr_bitmap.\n");
1692 memset(sparc64_valid_addr_bitmap
, 0, i
<< 3);
1694 addr
= PAGE_OFFSET
+ kern_base
;
1695 last
= PAGE_ALIGN(kern_size
) + addr
;
1696 while (addr
< last
) {
1697 set_bit(__pa(addr
) >> 22, sparc64_valid_addr_bitmap
);
1703 max_mapnr
= last_valid_pfn
- pfn_base
;
1704 high_memory
= __va(last_valid_pfn
<< PAGE_SHIFT
);
1706 #ifdef CONFIG_DEBUG_BOOTMEM
1707 prom_printf("mem_init: Calling free_all_bootmem().\n");
1709 totalram_pages
= num_physpages
= free_all_bootmem() - 1;
1712 * Set up the zero page, mark it reserved, so that page count
1713 * is not manipulated when freeing the page from user ptes.
1715 mem_map_zero
= alloc_pages(GFP_KERNEL
|__GFP_ZERO
, 0);
1716 if (mem_map_zero
== NULL
) {
1717 prom_printf("paging_init: Cannot alloc zero page.\n");
1720 SetPageReserved(mem_map_zero
);
1722 codepages
= (((unsigned long) _etext
) - ((unsigned long) _start
));
1723 codepages
= PAGE_ALIGN(codepages
) >> PAGE_SHIFT
;
1724 datapages
= (((unsigned long) _edata
) - ((unsigned long) _etext
));
1725 datapages
= PAGE_ALIGN(datapages
) >> PAGE_SHIFT
;
1726 initpages
= (((unsigned long) __init_end
) - ((unsigned long) __init_begin
));
1727 initpages
= PAGE_ALIGN(initpages
) >> PAGE_SHIFT
;
1729 printk("Memory: %uk available (%ldk kernel code, %ldk data, %ldk init) [%016lx,%016lx]\n",
1730 nr_free_pages() << (PAGE_SHIFT
-10),
1731 codepages
<< (PAGE_SHIFT
-10),
1732 datapages
<< (PAGE_SHIFT
-10),
1733 initpages
<< (PAGE_SHIFT
-10),
1734 PAGE_OFFSET
, (last_valid_pfn
<< PAGE_SHIFT
));
1736 if (tlb_type
== cheetah
|| tlb_type
== cheetah_plus
)
1737 cheetah_ecache_flush_init();
1740 void free_initmem (void)
1742 unsigned long addr
, initend
;
1745 * The init section is aligned to 8k in vmlinux.lds. Page align for >8k pagesizes.
1747 addr
= PAGE_ALIGN((unsigned long)(__init_begin
));
1748 initend
= (unsigned long)(__init_end
) & PAGE_MASK
;
1749 for (; addr
< initend
; addr
+= PAGE_SIZE
) {
1754 ((unsigned long) __va(kern_base
)) -
1755 ((unsigned long) KERNBASE
));
1756 memset((void *)addr
, 0xcc, PAGE_SIZE
);
1757 p
= virt_to_page(page
);
1759 ClearPageReserved(p
);
1760 set_page_count(p
, 1);
1767 #ifdef CONFIG_BLK_DEV_INITRD
1768 void free_initrd_mem(unsigned long start
, unsigned long end
)
1771 printk ("Freeing initrd memory: %ldk freed\n", (end
- start
) >> 10);
1772 for (; start
< end
; start
+= PAGE_SIZE
) {
1773 struct page
*p
= virt_to_page(start
);
1775 ClearPageReserved(p
);
1776 set_page_count(p
, 1);