1 /* $Id: init.c,v 1.96 2000/11/30 08:51:50 anton Exp $
2 * linux/arch/sparc/mm/init.c
4 * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
5 * Copyright (C) 1995 Eddie C. Dost (ecd@skynet.be)
6 * Copyright (C) 1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
7 * Copyright (C) 2000 Anton Blanchard (anton@linuxcare.com)
10 #include <linux/config.h>
11 #include <linux/signal.h>
12 #include <linux/sched.h>
13 #include <linux/kernel.h>
14 #include <linux/errno.h>
15 #include <linux/string.h>
16 #include <linux/types.h>
17 #include <linux/ptrace.h>
18 #include <linux/mman.h>
20 #include <linux/swap.h>
21 #include <linux/swapctl.h>
22 #ifdef CONFIG_BLK_DEV_INITRD
23 #include <linux/blk.h>
25 #include <linux/init.h>
26 #include <linux/highmem.h>
27 #include <linux/bootmem.h>
29 #include <asm/system.h>
30 #include <asm/segment.h>
31 #include <asm/vac-ops.h>
33 #include <asm/pgtable.h>
34 #include <asm/vaddrs.h>
36 unsigned long *sparc_valid_addr_bitmap
;
38 unsigned long phys_base
;
40 unsigned long page_kernel
;
42 struct sparc_phys_banks sp_banks
[SPARC_PHYS_BANKS
];
43 unsigned long sparc_unmapped_base
;
45 struct pgtable_cache_struct pgt_quicklists
;
47 /* References to section boundaries */
48 extern char __init_begin
, __init_end
, _start
, _end
, etext
, edata
;
50 /* Initial ramdisk setup */
51 extern unsigned int sparc_ramdisk_image
;
52 extern unsigned int sparc_ramdisk_size
;
54 unsigned long highstart_pfn
, highend_pfn
;
55 unsigned long totalram_pages
;
56 static unsigned long totalhigh_pages
;
59 * BAD_PAGE is the page that is used for page faults when linux
60 * is out-of-memory. Older versions of linux just did a
61 * do_exit(), but using this instead means there is less risk
62 * for a process dying in kernel mode, possibly leaving an inode
65 * BAD_PAGETABLE is the accompanying page-table: it is initialized
66 * to point to BAD_PAGE entries.
68 * ZERO_PAGE is a special page that is used for zero-initialized
71 pte_t
*__bad_pagetable(void)
73 memset((void *) &empty_bad_page_table
, 0, PAGE_SIZE
);
74 return (pte_t
*) &empty_bad_page_table
;
77 pte_t
__bad_page(void)
79 memset((void *) &empty_bad_page
, 0, PAGE_SIZE
);
80 return pte_mkdirty(mk_pte_phys((unsigned long)__pa(&empty_bad_page
) + phys_base
,
87 #define kmap_get_fixed_pte(vaddr) \
88 pte_offset(pmd_offset(pgd_offset_k(vaddr), (vaddr)), (vaddr))
90 void __init
kmap_init(void)
92 /* cache the first kmap pte */
93 kmap_pte
= kmap_get_fixed_pte(FIX_KMAP_BEGIN
);
94 kmap_prot
= __pgprot(SRMMU_ET_PTE
| SRMMU_PRIV
| SRMMU_CACHE
);
99 printk("Mem-info:\n");
101 printk("Free swap: %6dkB\n",
102 nr_swap_pages
<< (PAGE_SHIFT
-10));
103 printk("%ld pages of RAM\n", totalram_pages
);
104 printk("%d free pages\n", nr_free_pages());
105 printk("%ld pages in page table cache\n",pgtable_cache_size
);
107 if (sparc_cpu_model
== sun4m
|| sparc_cpu_model
== sun4d
)
108 printk("%ld entries in page dir cache\n",pgd_cache_size
);
113 extern pgprot_t protection_map
[16];
115 void __init
sparc_context_init(int numctx
)
119 ctx_list_pool
= __alloc_bootmem(numctx
* sizeof(struct ctx_list
), SMP_CACHE_BYTES
, 0UL);
121 for(ctx
= 0; ctx
< numctx
; ctx
++) {
122 struct ctx_list
*clist
;
124 clist
= (ctx_list_pool
+ ctx
);
125 clist
->ctx_number
= ctx
;
128 ctx_free
.next
= ctx_free
.prev
= &ctx_free
;
129 ctx_used
.next
= ctx_used
.prev
= &ctx_used
;
130 for(ctx
= 0; ctx
< numctx
; ctx
++)
131 add_to_free_ctxlist(ctx_list_pool
+ ctx
);
134 #define DEBUG_BOOTMEM
136 extern unsigned long cmdline_memory_size
;
137 extern unsigned long last_valid_pfn
;
139 void __init
bootmem_init(void)
141 unsigned long bootmap_size
, start_pfn
, max_pfn
;
142 unsigned long end_of_phys_memory
= 0UL;
143 unsigned long bootmap_pfn
;
146 /* XXX It is a bit ambiguous here, whether we should
147 * XXX treat the user specified mem=xxx as total wanted
148 * XXX physical memory, or as a limit to the upper
149 * XXX physical address we allow. For now it is the
153 prom_printf("bootmem_init: Scan sp_banks, ");
155 for (i
= 0; sp_banks
[i
].num_bytes
!= 0; i
++) {
156 end_of_phys_memory
= sp_banks
[i
].base_addr
+
157 sp_banks
[i
].num_bytes
;
158 if (cmdline_memory_size
) {
159 if (end_of_phys_memory
> cmdline_memory_size
) {
160 if (cmdline_memory_size
< sp_banks
[i
].base_addr
) {
162 sp_banks
[i
-1].base_addr
+
163 sp_banks
[i
-1].num_bytes
;
164 sp_banks
[i
].base_addr
= 0xdeadbeef;
165 sp_banks
[i
].num_bytes
= 0;
167 sp_banks
[i
].num_bytes
-=
168 (end_of_phys_memory
-
169 cmdline_memory_size
);
170 end_of_phys_memory
= cmdline_memory_size
;
171 sp_banks
[++i
].base_addr
= 0xdeadbeef;
172 sp_banks
[i
].num_bytes
= 0;
179 /* Start with page aligned address of last symbol in kernel
182 start_pfn
= (unsigned long)__pa(PAGE_ALIGN((unsigned long) &_end
));
184 /* Adjust up to the physical address where the kernel begins. */
185 start_pfn
+= phys_base
;
187 /* Now shift down to get the real physical page frame number. */
188 start_pfn
>>= PAGE_SHIFT
;
190 bootmap_pfn
= start_pfn
;
192 max_pfn
= end_of_phys_memory
>> PAGE_SHIFT
;
194 max_low_pfn
= max_pfn
;
195 highstart_pfn
= highend_pfn
= max_pfn
;
197 if (max_low_pfn
> (SRMMU_MAXMEM
>> PAGE_SHIFT
)) {
198 highstart_pfn
= max_low_pfn
= (SRMMU_MAXMEM
>> PAGE_SHIFT
);
199 printk(KERN_NOTICE
"%ldMB HIGHMEM available.\n",
200 (highend_pfn
- highstart_pfn
) >> (20-PAGE_SHIFT
));
203 #ifdef CONFIG_BLK_DEV_INITRD
204 /* Now have to check initial ramdisk, so that bootmap does not overwrite it */
205 if (sparc_ramdisk_image
) {
206 if (sparc_ramdisk_image
>= (unsigned long)&_end
- 2 * PAGE_SIZE
)
207 sparc_ramdisk_image
-= KERNBASE
;
208 initrd_start
= sparc_ramdisk_image
+ phys_base
;
209 initrd_end
= initrd_start
+ sparc_ramdisk_size
;
210 if (initrd_end
> end_of_phys_memory
) {
211 printk(KERN_CRIT
"initrd extends beyond end of memory "
212 "(0x%016lx > 0x%016lx)\ndisabling initrd\n",
213 initrd_end
, end_of_phys_memory
);
217 if (initrd_start
>= (start_pfn
<< PAGE_SHIFT
) &&
218 initrd_start
< (start_pfn
<< PAGE_SHIFT
) + 2 * PAGE_SIZE
)
219 bootmap_pfn
= PAGE_ALIGN (initrd_end
) >> PAGE_SHIFT
;
223 /* Initialize the boot-time allocator. */
225 prom_printf("init_bootmem(spfn[%lx],bpfn[%lx],mlpfn[%lx])\n",
226 start_pfn
, bootmap_pfn
, max_low_pfn
);
228 bootmap_size
= init_bootmem(bootmap_pfn
, max_low_pfn
);
230 /* Now register the available physical memory with the
233 for (i
= 0; sp_banks
[i
].num_bytes
!= 0; i
++) {
234 unsigned long curr_pfn
, last_pfn
, size
;
236 curr_pfn
= sp_banks
[i
].base_addr
>> PAGE_SHIFT
;
237 if (curr_pfn
>= max_low_pfn
)
240 last_pfn
= (sp_banks
[i
].base_addr
+ sp_banks
[i
].num_bytes
) >> PAGE_SHIFT
;
241 if (last_pfn
> max_low_pfn
)
242 last_pfn
= max_low_pfn
;
245 * .. finally, did all the rounding and playing
246 * around just make the area go away?
248 if (last_pfn
<= curr_pfn
)
251 size
= (last_pfn
- curr_pfn
) << PAGE_SHIFT
;
254 prom_printf("free_bootmem: base[%lx] size[%lx]\n",
255 sp_banks
[i
].base_addr
,
258 free_bootmem(sp_banks
[i
].base_addr
,
262 /* Reserve the kernel text/data/bss, the bootmem bitmap and initrd. */
264 #ifdef CONFIG_BLK_DEV_INITRD
266 prom_printf("reserve_bootmem: base[%lx] size[%lx]\n",
267 initrd_start
, initrd_end
- initrd_start
);
269 prom_printf("reserve_bootmem: base[%lx] size[%lx]\n",
270 phys_base
, (start_pfn
<< PAGE_SHIFT
) - phys_base
);
271 prom_printf("reserve_bootmem: base[%lx] size[%lx]\n",
272 (bootmap_pfn
<< PAGE_SHIFT
), bootmap_size
);
274 #ifdef CONFIG_BLK_DEV_INITRD
276 reserve_bootmem(initrd_start
, initrd_end
- initrd_start
);
277 initrd_start
+= PAGE_OFFSET
;
278 initrd_end
+= PAGE_OFFSET
;
281 reserve_bootmem(phys_base
, (start_pfn
<< PAGE_SHIFT
) - phys_base
);
282 reserve_bootmem((bootmap_pfn
<< PAGE_SHIFT
), bootmap_size
);
284 last_valid_pfn
= max_pfn
;
288 * paging_init() sets up the page tables: We call the MMU specific
289 * init routine based upon the Sun model type on the Sparc.
292 extern void sun4c_paging_init(void);
293 extern void srmmu_paging_init(void);
294 extern void device_scan(void);
296 unsigned long last_valid_pfn
;
298 void __init
paging_init(void)
300 switch(sparc_cpu_model
) {
305 sparc_unmapped_base
= 0xe0000000;
306 BTFIXUPSET_SETHI(sparc_unmapped_base
, 0xe0000000);
311 sparc_unmapped_base
= 0x50000000;
312 BTFIXUPSET_SETHI(sparc_unmapped_base
, 0x50000000);
315 prom_printf("paging_init: Cannot init paging on this Sparc\n");
316 prom_printf("paging_init: sparc_cpu_model = %d\n", sparc_cpu_model
);
317 prom_printf("paging_init: Halting...\n");
321 /* Initialize the protection map with non-constant, MMU dependent values. */
322 protection_map
[0] = PAGE_NONE
;
323 protection_map
[1] = PAGE_READONLY
;
324 protection_map
[2] = PAGE_COPY
;
325 protection_map
[3] = PAGE_COPY
;
326 protection_map
[4] = PAGE_READONLY
;
327 protection_map
[5] = PAGE_READONLY
;
328 protection_map
[6] = PAGE_COPY
;
329 protection_map
[7] = PAGE_COPY
;
330 protection_map
[8] = PAGE_NONE
;
331 protection_map
[9] = PAGE_READONLY
;
332 protection_map
[10] = PAGE_SHARED
;
333 protection_map
[11] = PAGE_SHARED
;
334 protection_map
[12] = PAGE_READONLY
;
335 protection_map
[13] = PAGE_READONLY
;
336 protection_map
[14] = PAGE_SHARED
;
337 protection_map
[15] = PAGE_SHARED
;
342 struct cache_palias
*sparc_aliases
;
344 static void __init
taint_real_pages(void)
348 for (i
= 0; sp_banks
[i
].num_bytes
; i
++) {
349 unsigned long start
, end
;
351 start
= sp_banks
[i
].base_addr
;
352 end
= start
+ sp_banks
[i
].num_bytes
;
354 while (start
< end
) {
355 set_bit (start
>> 20,
356 sparc_valid_addr_bitmap
);
362 void __init
free_mem_map_range(struct page
*first
, struct page
*last
)
364 first
= (struct page
*) PAGE_ALIGN((unsigned long)first
);
365 last
= (struct page
*) ((unsigned long)last
& PAGE_MASK
);
367 prom_printf("[%p,%p] ", first
, last
);
369 while (first
< last
) {
370 ClearPageReserved(virt_to_page(first
));
371 set_page_count(virt_to_page(first
), 1);
372 free_page((unsigned long)first
);
376 first
= (struct page
*)((unsigned long)first
+ PAGE_SIZE
);
380 /* Walk through holes in sp_banks regions, if the mem_map array
381 * areas representing those holes consume a page or more, free
382 * up such pages. This helps a lot on machines where physical
383 * ram is configured such that it begins at some hugh value.
385 * The sp_banks array is sorted by base address.
387 void __init
free_unused_mem_map(void)
392 prom_printf("free_unused_mem_map: ");
394 for (i
= 0; sp_banks
[i
].num_bytes
; i
++) {
396 struct page
*first
, *last
;
399 last
= &mem_map
[sp_banks
[i
].base_addr
>> PAGE_SHIFT
];
400 free_mem_map_range(first
, last
);
402 struct page
*first
, *last
;
403 unsigned long prev_end
;
405 prev_end
= sp_banks
[i
-1].base_addr
+
406 sp_banks
[i
-1].num_bytes
;
407 prev_end
= PAGE_ALIGN(prev_end
);
408 first
= &mem_map
[prev_end
>> PAGE_SHIFT
];
409 last
= &mem_map
[sp_banks
[i
].base_addr
>> PAGE_SHIFT
];
411 free_mem_map_range(first
, last
);
413 if (!sp_banks
[i
+1].num_bytes
) {
414 prev_end
= sp_banks
[i
].base_addr
+
415 sp_banks
[i
].num_bytes
;
416 first
= &mem_map
[prev_end
>> PAGE_SHIFT
];
417 last
= &mem_map
[last_valid_pfn
];
418 free_mem_map_range(first
, last
);
427 void map_high_region(unsigned long start_pfn
, unsigned long end_pfn
)
432 printk("mapping high region %08lx - %08lx\n", start_pfn
, end_pfn
);
435 for (tmp
= start_pfn
; tmp
< end_pfn
; tmp
++) {
436 struct page
*page
= mem_map
+ tmp
;
438 ClearPageReserved(page
);
439 set_bit(PG_highmem
, &page
->flags
);
440 atomic_set(&page
->count
, 1);
446 void __init
mem_init(void)
452 #ifdef CONFIG_BLK_DEV_INITRD
453 unsigned long addr
, last
;
456 highmem_start_page
= mem_map
+ highstart_pfn
;
458 /* Saves us work later. */
459 memset((void *)&empty_zero_page
, 0, PAGE_SIZE
);
461 i
= last_valid_pfn
>> (8 + 5);
464 sparc_valid_addr_bitmap
= (unsigned long *)
465 __alloc_bootmem(i
<< 2, SMP_CACHE_BYTES
, 0UL);
467 if (sparc_valid_addr_bitmap
== NULL
) {
468 prom_printf("mem_init: Cannot alloc valid_addr_bitmap.\n");
471 memset(sparc_valid_addr_bitmap
, 0, i
<< 2);
475 max_mapnr
= last_valid_pfn
;
476 high_memory
= __va(max_low_pfn
<< PAGE_SHIFT
);
479 prom_printf("mem_init: Calling free_all_bootmem().\n");
481 num_physpages
= totalram_pages
= free_all_bootmem();
484 free_unused_mem_map();
487 for (i
= 0; sp_banks
[i
].num_bytes
!= 0; i
++) {
488 unsigned long start_pfn
= sp_banks
[i
].base_addr
>> PAGE_SHIFT
;
489 unsigned long end_pfn
= (sp_banks
[i
].base_addr
+ sp_banks
[i
].num_bytes
) >> PAGE_SHIFT
;
491 if (end_pfn
<= highstart_pfn
)
494 if (start_pfn
< highstart_pfn
)
495 start_pfn
= highstart_pfn
;
497 map_high_region(start_pfn
, end_pfn
);
500 totalram_pages
+= totalhigh_pages
;
502 codepages
= (((unsigned long) &etext
) - ((unsigned long)&_start
));
503 codepages
= PAGE_ALIGN(codepages
) >> PAGE_SHIFT
;
504 datapages
= (((unsigned long) &edata
) - ((unsigned long)&etext
));
505 datapages
= PAGE_ALIGN(datapages
) >> PAGE_SHIFT
;
506 initpages
= (((unsigned long) &__init_end
) - ((unsigned long) &__init_begin
));
507 initpages
= PAGE_ALIGN(initpages
) >> PAGE_SHIFT
;
509 printk("Memory: %dk available (%dk kernel code, %dk data, %dk init, %ldk highmem) [%08lx,%08lx]\n",
510 nr_free_pages() << (PAGE_SHIFT
-10),
511 codepages
<< (PAGE_SHIFT
-10),
512 datapages
<< (PAGE_SHIFT
-10),
513 initpages
<< (PAGE_SHIFT
-10),
514 totalhigh_pages
<< (PAGE_SHIFT
-10),
515 (unsigned long)PAGE_OFFSET
, (last_valid_pfn
<< PAGE_SHIFT
));
517 /* NOTE NOTE NOTE NOTE
518 * Please keep track of things and make sure this
519 * always matches the code in mm/page_alloc.c -DaveM
521 i
= nr_free_pages() >> 7;
527 freepages
.low
= i
<< 1;
528 freepages
.high
= freepages
.low
+ i
;
531 void free_initmem (void)
535 addr
= (unsigned long)(&__init_begin
);
536 for (; addr
< (unsigned long)(&__init_end
); addr
+= PAGE_SIZE
) {
540 page
= addr
+ phys_base
;
541 p
= virt_to_page(page
);
543 ClearPageReserved(p
);
544 set_page_count(p
, 1);
549 printk ("Freeing unused kernel memory: %dk freed\n", (&__init_end
- &__init_begin
) >> 10);
552 #ifdef CONFIG_BLK_DEV_INITRD
553 void free_initrd_mem(unsigned long start
, unsigned long end
)
556 printk ("Freeing initrd memory: %ldk freed\n", (end
- start
) >> 10);
557 for (; start
< end
; start
+= PAGE_SIZE
) {
558 struct page
*p
= virt_to_page(start
);
560 ClearPageReserved(p
);
561 set_page_count(p
, 1);
568 void si_meminfo(struct sysinfo
*val
)
570 val
->totalram
= totalram_pages
;
572 val
->freeram
= nr_free_pages();
573 val
->bufferram
= atomic_read(&buffermem_pages
);
574 val
->totalhigh
= totalhigh_pages
;
575 val
->freehigh
= nr_free_highpages();
577 val
->mem_unit
= PAGE_SIZE
;
580 void flush_page_to_ram(struct page
*page
)
582 unsigned long vaddr
= (unsigned long)page_address(page
);
585 __flush_page_to_ram(vaddr
);