2 * Copyright (C) 2007-2008 Michal Simek <monstr@monstr.eu>
3 * Copyright (C) 2006 Atmark Techno, Inc.
5 * This file is subject to the terms and conditions of the GNU General Public
6 * License. See the file "COPYING" in the main directory of this archive
10 #include <linux/bootmem.h>
11 #include <linux/init.h>
12 #include <linux/kernel.h>
13 #include <linux/lmb.h>
14 #include <linux/mm.h> /* mem_init */
15 #include <linux/initrd.h>
16 #include <linux/pagemap.h>
17 #include <linux/pfn.h>
18 #include <linux/slab.h>
19 #include <linux/swap.h>
22 #include <asm/mmu_context.h>
23 #include <asm/pgalloc.h>
24 #include <asm/sections.h>
27 /* Use for MMU and noMMU because of PCI generic code */
31 unsigned int __page_offset
;
32 EXPORT_SYMBOL(__page_offset
);
35 DEFINE_PER_CPU(struct mmu_gather
, mmu_gathers
);
37 static int init_bootmem_done
;
38 #endif /* CONFIG_MMU */
43 * Initialize the bootmem system and give it all the memory we
46 unsigned long memory_start
;
47 EXPORT_SYMBOL(memory_start
);
48 unsigned long memory_end
; /* due to mm/nommu.c */
49 unsigned long memory_size
;
50 EXPORT_SYMBOL(memory_size
);
53 * paging_init() sets up the page tables - in fact we've already done this.
55 static void __init
paging_init(void)
57 unsigned long zones_size
[MAX_NR_ZONES
];
59 /* Clean every zones */
60 memset(zones_size
, 0, sizeof(zones_size
));
63 * old: we can DMA to/from any address.put all page into ZONE_DMA
64 * We use only ZONE_NORMAL
66 zones_size
[ZONE_NORMAL
] = max_mapnr
;
68 free_area_init(zones_size
);
71 void __init
setup_memory(void)
74 unsigned long map_size
;
76 u32 kernel_align_start
, kernel_align_size
;
78 /* Find main memory where is the kernel */
79 for (i
= 0; i
< lmb
.memory
.cnt
; i
++) {
80 memory_start
= (u32
) lmb
.memory
.region
[i
].base
;
81 memory_end
= (u32
) lmb
.memory
.region
[i
].base
82 + (u32
) lmb
.memory
.region
[i
].size
;
83 if ((memory_start
<= (u32
)_text
) &&
84 ((u32
)_text
<= memory_end
)) {
85 memory_size
= memory_end
- memory_start
;
86 PAGE_OFFSET
= memory_start
;
87 printk(KERN_INFO
"%s: Main mem: 0x%x-0x%x, "
88 "size 0x%08x\n", __func__
, (u32
) memory_start
,
89 (u32
) memory_end
, (u32
) memory_size
);
94 if (!memory_start
|| !memory_end
) {
95 panic("%s: Missing memory setting 0x%08x-0x%08x\n",
96 __func__
, (u32
) memory_start
, (u32
) memory_end
);
99 /* reservation of region where is the kernel */
100 kernel_align_start
= PAGE_DOWN((u32
)_text
);
101 /* ALIGN can be remove because _end in vmlinux.lds.S is align */
102 kernel_align_size
= PAGE_UP((u32
)klimit
) - kernel_align_start
;
103 lmb_reserve(kernel_align_start
, kernel_align_size
);
104 printk(KERN_INFO
"%s: kernel addr=0x%08x-0x%08x size=0x%08x\n",
105 __func__
, kernel_align_start
, kernel_align_start
106 + kernel_align_size
, kernel_align_size
);
111 * start: base phys address of kernel - page align
112 * end: base phys address of kernel - page align
114 * min_low_pfn - the first page (mm/bootmem.c - node_boot_start)
116 * max_mapnr - the first unused page (mm/bootmem.c - node_low_pfn)
117 * num_physpages - number of all pages
120 /* memory start is from the kernel end (aligned) to higher addr */
121 min_low_pfn
= memory_start
>> PAGE_SHIFT
; /* minimum for allocation */
122 /* RAM is assumed contiguous */
123 num_physpages
= max_mapnr
= memory_size
>> PAGE_SHIFT
;
124 max_pfn
= max_low_pfn
= memory_end
>> PAGE_SHIFT
;
126 printk(KERN_INFO
"%s: max_mapnr: %#lx\n", __func__
, max_mapnr
);
127 printk(KERN_INFO
"%s: min_low_pfn: %#lx\n", __func__
, min_low_pfn
);
128 printk(KERN_INFO
"%s: max_low_pfn: %#lx\n", __func__
, max_low_pfn
);
131 * Find an area to use for the bootmem bitmap.
132 * We look for the first area which is at least
133 * 128kB in length (128kB is enough for a bitmap
134 * for 4GB of memory, using 4kB pages), plus 1 page
135 * (in case the address isn't page-aligned).
138 map_size
= init_bootmem_node(NODE_DATA(0), PFN_UP(TOPHYS((u32
)klimit
)),
139 min_low_pfn
, max_low_pfn
);
141 map_size
= init_bootmem_node(&contig_page_data
,
142 PFN_UP(TOPHYS((u32
)klimit
)), min_low_pfn
, max_low_pfn
);
144 lmb_reserve(PFN_UP(TOPHYS((u32
)klimit
)) << PAGE_SHIFT
, map_size
);
146 /* free bootmem is whole main memory */
147 free_bootmem(memory_start
, memory_size
);
149 /* reserve allocate blocks */
150 for (i
= 0; i
< lmb
.reserved
.cnt
; i
++) {
151 pr_debug("reserved %d - 0x%08x-0x%08x\n", i
,
152 (u32
) lmb
.reserved
.region
[i
].base
,
153 (u32
) lmb_size_bytes(&lmb
.reserved
, i
));
154 reserve_bootmem(lmb
.reserved
.region
[i
].base
,
155 lmb_size_bytes(&lmb
.reserved
, i
) - 1, BOOTMEM_DEFAULT
);
158 init_bootmem_done
= 1;
163 void free_init_pages(char *what
, unsigned long begin
, unsigned long end
)
167 for (addr
= begin
; addr
< end
; addr
+= PAGE_SIZE
) {
168 ClearPageReserved(virt_to_page(addr
));
169 init_page_count(virt_to_page(addr
));
173 printk(KERN_INFO
"Freeing %s: %ldk freed\n", what
, (end
- begin
) >> 10);
176 #ifdef CONFIG_BLK_DEV_INITRD
177 void free_initrd_mem(unsigned long start
, unsigned long end
)
180 for (; start
< end
; start
+= PAGE_SIZE
) {
181 ClearPageReserved(virt_to_page(start
));
182 init_page_count(virt_to_page(start
));
187 printk(KERN_NOTICE
"Freeing initrd memory: %dk freed\n",
188 (int)(pages
* (PAGE_SIZE
/ 1024)));
192 void free_initmem(void)
194 free_init_pages("unused kernel memory",
195 (unsigned long)(&__init_begin
),
196 (unsigned long)(&__init_end
));
199 void __init
mem_init(void)
201 high_memory
= (void *)__va(memory_end
);
202 /* this will put all memory onto the freelists */
203 totalram_pages
+= free_all_bootmem();
205 printk(KERN_INFO
"Memory: %luk/%luk available\n",
206 nr_free_pages() << (PAGE_SHIFT
-10),
207 num_physpages
<< (PAGE_SHIFT
-10));
212 int page_is_ram(unsigned long pfn
)
214 return __range_ok(pfn
, 0);
217 int page_is_ram(unsigned long pfn
)
219 return pfn
< max_low_pfn
;
223 * Check for command-line options that affect what MMU_init will do.
225 static void mm_cmdline_setup(void)
227 unsigned long maxmem
= 0;
230 /* Look for mem= option on command line */
231 p
= strstr(cmd_line
, "mem=");
234 maxmem
= memparse(p
, &p
);
235 if (maxmem
&& memory_size
> maxmem
) {
236 memory_size
= maxmem
;
237 memory_end
= memory_start
+ memory_size
;
238 lmb
.memory
.region
[0].size
= memory_size
;
244 * MMU_init_hw does the chip-specific initialization of the MMU hardware.
246 static void __init
mmu_init_hw(void)
249 * The Zone Protection Register (ZPR) defines how protection will
250 * be applied to every page which is a member of a given zone. At
251 * present, we utilize only two of the zones.
252 * The zone index bits (of ZSEL) in the PTE are used for software
253 * indicators, except the LSB. For user access, zone 1 is used,
254 * for kernel access, zone 0 is used. We set all but zone 1
255 * to zero, allowing only kernel access as indicated in the PTE.
256 * For zone 1, we set a 01 binary (a value of 10 will not work)
257 * to allow user access as indicated in the PTE. This also allows
258 * kernel access as indicated in the PTE.
260 __asm__
__volatile__ ("ori r11, r0, 0x10000000;" \
266 * MMU_init sets up the basic memory mappings for the kernel,
267 * including both RAM and possibly some I/O regions,
268 * and sets up the page tables and the MMU hardware ready to go.
271 /* called from head.S */
272 asmlinkage
void __init
mmu_init(void)
274 unsigned int kstart
, ksize
;
276 if (!lmb
.reserved
.cnt
) {
277 printk(KERN_EMERG
"Error memory count\n");
278 machine_restart(NULL
);
281 if ((u32
) lmb
.memory
.region
[0].size
< 0x1000000) {
282 printk(KERN_EMERG
"Memory must be greater than 16MB\n");
283 machine_restart(NULL
);
285 /* Find main memory where the kernel is */
286 memory_start
= (u32
) lmb
.memory
.region
[0].base
;
287 memory_end
= (u32
) lmb
.memory
.region
[0].base
+
288 (u32
) lmb
.memory
.region
[0].size
;
289 memory_size
= memory_end
- memory_start
;
291 mm_cmdline_setup(); /* FIXME parse args from command line - not used */
294 * Map out the kernel text/data/bss from the available physical
297 kstart
= __pa(CONFIG_KERNEL_START
); /* kernel start */
299 ksize
= PAGE_ALIGN(((u32
)_end
- (u32
)CONFIG_KERNEL_START
));
300 lmb_reserve(kstart
, ksize
);
302 #if defined(CONFIG_BLK_DEV_INITRD)
303 /* Remove the init RAM disk from the available memory. */
304 /* if (initrd_start) {
305 mem_pieces_remove(&phys_avail, __pa(initrd_start),
306 initrd_end - initrd_start, 1);
308 #endif /* CONFIG_BLK_DEV_INITRD */
310 /* Initialize the MMU hardware */
313 /* Map in all of RAM starting at CONFIG_KERNEL_START */
316 #ifdef HIGHMEM_START_BOOL
317 ioremap_base
= HIGHMEM_START
;
319 ioremap_base
= 0xfe000000UL
; /* for now, could be 0xfffff000 */
320 #endif /* CONFIG_HIGHMEM */
321 ioremap_bot
= ioremap_base
;
323 /* Initialize the context management stuff */
327 /* This is only called until mem_init is done. */
328 void __init
*early_get_page(void)
331 if (init_bootmem_done
) {
332 p
= alloc_bootmem_pages(PAGE_SIZE
);
335 * Mem start + 32MB -> here is limit
336 * because of mem mapping from head.S
338 p
= __va(lmb_alloc_base(PAGE_SIZE
, PAGE_SIZE
,
339 memory_start
+ 0x2000000));
344 #endif /* CONFIG_MMU */
346 void * __init_refok
alloc_maybe_bootmem(size_t size
, gfp_t mask
)
349 return kmalloc(size
, mask
);
351 return alloc_bootmem(size
);
354 void * __init_refok
zalloc_maybe_bootmem(size_t size
, gfp_t mask
)
359 p
= kzalloc(size
, mask
);
361 p
= alloc_bootmem(size
);
This page took 0.040623 seconds and 6 git commands to generate.