2 * linux/arch/arm/mm/init.c
4 * Copyright (C) 1995-2002 Russell King
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
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/smp.h>
22 #include <linux/init.h>
23 #include <linux/initrd.h>
24 #include <linux/bootmem.h>
25 #include <linux/blk.h>
27 #include <asm/segment.h>
28 #include <asm/mach-types.h>
29 #include <asm/pgalloc.h>
31 #include <asm/hardware.h>
32 #include <asm/setup.h>
35 //#include <asm/arch.h>
38 #define TABLE_SIZE PTRS_PER_PTE * sizeof(pte_t))
40 struct mmu_gather mmu_gathers
[NR_CPUS
];
42 extern pgd_t swapper_pg_dir
[PTRS_PER_PGD
];
43 extern char _stext
, _text
, _etext
, _end
, __init_begin
, __init_end
;
44 extern unsigned long phys_initrd_start
;
45 extern unsigned long phys_initrd_size
;
48 * The sole use of this is to pass memory configuration
49 * data from paging_init to mem_init.
51 static struct meminfo meminfo __initdata
= { 0, };
54 * empty_zero_page is a special page that is used for
55 * zero-initialized data and COW.
57 struct page
*empty_zero_page
;
61 int free
= 0, total
= 0, reserved
= 0;
62 int shared
= 0, cached
= 0, slab
= 0;
63 struct page
*page
, *end
;
65 printk("Mem-info:\n");
67 printk("Free swap: %6dkB\n",nr_swap_pages
<<(PAGE_SHIFT
-10));
70 page
= NODE_MEM_MAP(0);
71 end
= page
+ NODE_DATA(0)->node_spanned_pages
;
75 if (PageReserved(page
))
77 else if (PageSwapCache(page
))
79 else if (PageSlab(page
))
81 else if (!page_count(page
))
84 shared
+= atomic_read(&page
->count
) - 1;
88 printk("%d pages of RAM\n", total
);
89 printk("%d free pages\n", free
);
90 printk("%d reserved pages\n", reserved
);
91 printk("%d slab pages\n", slab
);
92 printk("%d pages shared\n", shared
);
93 printk("%d pages swap cached\n", cached
);
102 #define PFN_DOWN(x) ((x) >> PAGE_SHIFT)
103 #define PFN_UP(x) (PAGE_ALIGN(x) >> PAGE_SHIFT)
104 #define PFN_SIZE(x) ((x) >> PAGE_SHIFT)
105 #define PFN_RANGE(s,e) PFN_SIZE(PAGE_ALIGN((unsigned long)(e)) - \
106 (((unsigned long)(s)) & PAGE_MASK))
109 * FIXME: We really want to avoid allocating the bootmap bitmap
110 * over the top of the initrd. Hopefully, this is located towards
111 * the start of a bank, so if we allocate the bootmap bitmap at
112 * the end, we won't clash.
114 static unsigned int __init
115 find_bootmap_pfn(struct meminfo
*mi
, unsigned int bootmap_pages
)
117 unsigned int start_pfn
, bootmap_pfn
;
118 unsigned int start
, end
;
120 start_pfn
= PFN_UP((unsigned long)&_end
);
123 /* ARM26 machines only have one node */
124 if (mi
->bank
->node
!= 0)
127 start
= PFN_UP(mi
->bank
->start
);
128 end
= PFN_DOWN(mi
->bank
->size
+ mi
->bank
->start
);
130 if (start
< start_pfn
)
136 if (end
- start
>= bootmap_pages
)
145 * Scan the memory info structure and pull out:
146 * - the end of memory
147 * - the number of nodes
148 * - the pfn range of each node
149 * - the number of bootmem bitmap pages
152 find_memend_and_nodes(struct meminfo
*mi
, struct node_info
*np
)
154 unsigned int memend_pfn
= 0;
157 np
->bootmap_pages
= 0;
159 if (mi
->bank
->size
== 0) {
164 * Get the start and end pfns for this bank
166 np
->start
= PFN_UP(mi
->bank
->start
);
167 np
->end
= PFN_DOWN(mi
->bank
->start
+ mi
->bank
->size
);
169 if (memend_pfn
< np
->end
)
170 memend_pfn
= np
->end
;
173 * Calculate the number of pages we require to
174 * store the bootmem bitmaps.
176 np
->bootmap_pages
= bootmem_bootmap_pages(np
->end
- np
->start
);
179 * This doesn't seem to be used by the Linux memory
180 * manager any more. If we can get rid of it, we
181 * also get rid of some of the stuff above as well.
183 max_low_pfn
= memend_pfn
- PFN_DOWN(PHYS_OFFSET
);
184 max_pfn
= memend_pfn
- PFN_DOWN(PHYS_OFFSET
);
185 mi
->end
= memend_pfn
<< PAGE_SHIFT
;
190 * Reserve the various regions of node 0
192 static __init
void reserve_node_zero(unsigned int bootmap_pfn
, unsigned int bootmap_pages
)
194 pg_data_t
*pgdat
= NODE_DATA(0);
197 * Register the kernel text and data with bootmem.
198 * Note that this can only be in node 0.
200 reserve_bootmem_node(pgdat
, __pa(&_stext
), &_end
- &_stext
);
203 * And don't forget to reserve the allocator bitmap,
204 * which will be freed later.
206 reserve_bootmem_node(pgdat
, bootmap_pfn
<< PAGE_SHIFT
,
207 bootmap_pages
<< PAGE_SHIFT
);
210 * These should likewise go elsewhere. They pre-reserve
211 * the screen memory region at the start of main system
214 reserve_bootmem_node(pgdat
, 0x02000000, 0x00080000);
216 #ifdef CONFIG_BLK_DEV_INITRD
217 initrd_start
= phys_initrd_start
;
218 initrd_end
= initrd_start
+ phys_initrd_size
;
220 /* Achimedes machines only have one node, so initrd is in node 0 */
221 reserve_bootmem_node(pgdat
, __pa(initrd_start
),
222 initrd_end
- initrd_start
);
229 * Initialise the bootmem allocator for all nodes. This is called
230 * early during the architecture specific initialisation.
232 void __init
bootmem_init(struct meminfo
*mi
)
234 struct node_info node_info
;
235 unsigned int bootmap_pfn
;
237 find_memend_and_nodes(mi
, &node_info
);
239 bootmap_pfn
= find_bootmap_pfn(mi
, node_info
.bootmap_pages
);
242 * Note that node 0 must always have some pages.
244 if (node_info
.end
== 0)
248 * Initialise the bootmem allocator.
250 init_bootmem_node(NODE_DATA(node
), bootmap_pfn
, node_info
.start
, node_info
.end
);
253 * Register all available RAM in this node with the bootmem allocator.
255 free_bootmem_node(NODE_DATA(node
), mi
->bank
->start
, mi
->bank
->size
);
258 * Reserve ram for stuff like initrd, video, kernel, etc.
261 reserve_node_zero(bootmap_pfn
, node_info
.bootmap_pages
);
266 * paging_init() sets up the page tables, initialises the zone memory
267 * maps, and sets up the zero page, bad page and bad page tables.
269 void __init
paging_init(struct meminfo
*mi
)
272 unsigned long zone_size
[MAX_NR_ZONES
];
273 unsigned long zhole_size
[MAX_NR_ZONES
];
274 struct bootmem_data
*bdata
;
278 memcpy(&meminfo
, mi
, sizeof(meminfo
));
281 * allocate the zero page. Note that we count on this going ok.
283 zero_page
= alloc_bootmem_low_pages(PAGE_SIZE
);
286 * initialise the page tables.
292 * initialise the zones in node 0 (archimedes have only 1 node)
295 for (i
= 0; i
< MAX_NR_ZONES
; i
++) {
300 pgdat
= NODE_DATA(0);
301 bdata
= pgdat
->bdata
;
303 zone_size
[0] = bdata
->node_low_pfn
-
304 (bdata
->node_boot_start
>> PAGE_SHIFT
);
308 free_area_init_node(0, pgdat
, 0, zone_size
,
309 bdata
->node_boot_start
>> PAGE_SHIFT
, 0);
311 mem_map
= contig_page_data
.node_mem_map
;
314 * finish off the bad pages once
315 * the mem_map is initialised
317 memzero(zero_page
, PAGE_SIZE
);
318 empty_zero_page
= virt_to_page(zero_page
);
321 static inline void free_area(unsigned long addr
, unsigned long end
, char *s
)
323 unsigned int size
= (end
- addr
) >> 10;
325 for (; addr
< end
; addr
+= PAGE_SIZE
) {
326 struct page
*page
= virt_to_page(addr
);
327 ClearPageReserved(page
);
328 set_page_count(page
, 1);
334 printk(KERN_INFO
"Freeing %s memory: %dK\n", s
, size
);
338 * mem_init() marks the free areas in the mem_map and tells us how much
339 * memory is free. This is done after various parts of the system have
340 * claimed their memory after the kernel image.
342 void __init
mem_init(void)
344 unsigned int codepages
, datapages
, initpages
;
345 pg_data_t
*pgdat
= NODE_DATA(0);
346 extern int sysctl_overcommit_memory
;
348 datapages
= &_end
- &_etext
;
349 codepages
= &_etext
- &_text
;
350 initpages
= &__init_end
- &__init_begin
;
352 high_memory
= (void *)__va(meminfo
.end
);
353 max_mapnr
= virt_to_page(high_memory
) - mem_map
;
355 /* this will put all unused low memory onto the freelists */
356 if (pgdat
->node_spanned_pages
!= 0)
357 totalram_pages
+= free_all_bootmem_node(pgdat
);
359 printk(KERN_INFO
"Memory:");
361 num_physpages
= meminfo
.bank
[0].size
>> PAGE_SHIFT
;
363 printk(" = %luMB total\n", num_physpages
>> (20 - PAGE_SHIFT
));
364 printk(KERN_NOTICE
"Memory: %luKB available (%dK code, "
365 "%dK data, %dK init)\n",
366 (unsigned long) nr_free_pages() << (PAGE_SHIFT
-10),
367 codepages
>> 10, datapages
>> 10, initpages
>> 10);
369 * Turn on overcommit on tiny machines
371 if (PAGE_SIZE
>= 16384 && num_physpages
<= 128) {
372 sysctl_overcommit_memory
= 1;
373 printk("Turning on overcommit\n");
377 void free_initmem(void)
379 free_area((unsigned long)(&__init_begin
),
380 (unsigned long)(&__init_end
),
384 #ifdef CONFIG_BLK_DEV_INITRD
386 static int keep_initrd
;
388 void free_initrd_mem(unsigned long start
, unsigned long end
)
391 free_area(start
, end
, "initrd");
394 static int __init
keepinitrd_setup(char *__unused
)
400 __setup("keepinitrd", keepinitrd_setup
);