xref: /openbmc/linux/arch/sparc/mm/init_32.c (revision a36954f5)
1 /*
2  *  linux/arch/sparc/mm/init.c
3  *
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@samba.org)
8  */
9 
10 #include <linux/module.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>
19 #include <linux/mm.h>
20 #include <linux/swap.h>
21 #include <linux/initrd.h>
22 #include <linux/init.h>
23 #include <linux/highmem.h>
24 #include <linux/bootmem.h>
25 #include <linux/pagemap.h>
26 #include <linux/poison.h>
27 #include <linux/gfp.h>
28 
29 #include <asm/sections.h>
30 #include <asm/page.h>
31 #include <asm/pgtable.h>
32 #include <asm/vaddrs.h>
33 #include <asm/pgalloc.h>	/* bug in asm-generic/tlb.h: check_pgt_cache */
34 #include <asm/setup.h>
35 #include <asm/tlb.h>
36 #include <asm/prom.h>
37 #include <asm/leon.h>
38 
39 #include "mm_32.h"
40 
41 unsigned long *sparc_valid_addr_bitmap;
42 EXPORT_SYMBOL(sparc_valid_addr_bitmap);
43 
44 unsigned long phys_base;
45 EXPORT_SYMBOL(phys_base);
46 
47 unsigned long pfn_base;
48 EXPORT_SYMBOL(pfn_base);
49 
50 struct sparc_phys_banks sp_banks[SPARC_PHYS_BANKS+1];
51 
52 /* Initial ramdisk setup */
53 extern unsigned int sparc_ramdisk_image;
54 extern unsigned int sparc_ramdisk_size;
55 
56 unsigned long highstart_pfn, highend_pfn;
57 
58 unsigned long last_valid_pfn;
59 
60 unsigned long calc_highpages(void)
61 {
62 	int i;
63 	int nr = 0;
64 
65 	for (i = 0; sp_banks[i].num_bytes != 0; i++) {
66 		unsigned long start_pfn = sp_banks[i].base_addr >> PAGE_SHIFT;
67 		unsigned long end_pfn = (sp_banks[i].base_addr + sp_banks[i].num_bytes) >> PAGE_SHIFT;
68 
69 		if (end_pfn <= max_low_pfn)
70 			continue;
71 
72 		if (start_pfn < max_low_pfn)
73 			start_pfn = max_low_pfn;
74 
75 		nr += end_pfn - start_pfn;
76 	}
77 
78 	return nr;
79 }
80 
81 static unsigned long calc_max_low_pfn(void)
82 {
83 	int i;
84 	unsigned long tmp = pfn_base + (SRMMU_MAXMEM >> PAGE_SHIFT);
85 	unsigned long curr_pfn, last_pfn;
86 
87 	last_pfn = (sp_banks[0].base_addr + sp_banks[0].num_bytes) >> PAGE_SHIFT;
88 	for (i = 1; sp_banks[i].num_bytes != 0; i++) {
89 		curr_pfn = sp_banks[i].base_addr >> PAGE_SHIFT;
90 
91 		if (curr_pfn >= tmp) {
92 			if (last_pfn < tmp)
93 				tmp = last_pfn;
94 			break;
95 		}
96 
97 		last_pfn = (sp_banks[i].base_addr + sp_banks[i].num_bytes) >> PAGE_SHIFT;
98 	}
99 
100 	return tmp;
101 }
102 
103 unsigned long __init bootmem_init(unsigned long *pages_avail)
104 {
105 	unsigned long bootmap_size, start_pfn;
106 	unsigned long end_of_phys_memory = 0UL;
107 	unsigned long bootmap_pfn, bytes_avail, size;
108 	int i;
109 
110 	bytes_avail = 0UL;
111 	for (i = 0; sp_banks[i].num_bytes != 0; i++) {
112 		end_of_phys_memory = sp_banks[i].base_addr +
113 			sp_banks[i].num_bytes;
114 		bytes_avail += sp_banks[i].num_bytes;
115 		if (cmdline_memory_size) {
116 			if (bytes_avail > cmdline_memory_size) {
117 				unsigned long slack = bytes_avail - cmdline_memory_size;
118 
119 				bytes_avail -= slack;
120 				end_of_phys_memory -= slack;
121 
122 				sp_banks[i].num_bytes -= slack;
123 				if (sp_banks[i].num_bytes == 0) {
124 					sp_banks[i].base_addr = 0xdeadbeef;
125 				} else {
126 					sp_banks[i+1].num_bytes = 0;
127 					sp_banks[i+1].base_addr = 0xdeadbeef;
128 				}
129 				break;
130 			}
131 		}
132 	}
133 
134 	/* Start with page aligned address of last symbol in kernel
135 	 * image.
136 	 */
137 	start_pfn  = (unsigned long)__pa(PAGE_ALIGN((unsigned long) &_end));
138 
139 	/* Now shift down to get the real physical page frame number. */
140 	start_pfn >>= PAGE_SHIFT;
141 
142 	bootmap_pfn = start_pfn;
143 
144 	max_pfn = end_of_phys_memory >> PAGE_SHIFT;
145 
146 	max_low_pfn = max_pfn;
147 	highstart_pfn = highend_pfn = max_pfn;
148 
149 	if (max_low_pfn > pfn_base + (SRMMU_MAXMEM >> PAGE_SHIFT)) {
150 		highstart_pfn = pfn_base + (SRMMU_MAXMEM >> PAGE_SHIFT);
151 		max_low_pfn = calc_max_low_pfn();
152 		printk(KERN_NOTICE "%ldMB HIGHMEM available.\n",
153 		    calc_highpages() >> (20 - PAGE_SHIFT));
154 	}
155 
156 #ifdef CONFIG_BLK_DEV_INITRD
157 	/* Now have to check initial ramdisk, so that bootmap does not overwrite it */
158 	if (sparc_ramdisk_image) {
159 		if (sparc_ramdisk_image >= (unsigned long)&_end - 2 * PAGE_SIZE)
160 			sparc_ramdisk_image -= KERNBASE;
161 		initrd_start = sparc_ramdisk_image + phys_base;
162 		initrd_end = initrd_start + sparc_ramdisk_size;
163 		if (initrd_end > end_of_phys_memory) {
164 			printk(KERN_CRIT "initrd extends beyond end of memory "
165 		                 	 "(0x%016lx > 0x%016lx)\ndisabling initrd\n",
166 			       initrd_end, end_of_phys_memory);
167 			initrd_start = 0;
168 		}
169 		if (initrd_start) {
170 			if (initrd_start >= (start_pfn << PAGE_SHIFT) &&
171 			    initrd_start < (start_pfn << PAGE_SHIFT) + 2 * PAGE_SIZE)
172 				bootmap_pfn = PAGE_ALIGN (initrd_end) >> PAGE_SHIFT;
173 		}
174 	}
175 #endif
176 	/* Initialize the boot-time allocator. */
177 	bootmap_size = init_bootmem_node(NODE_DATA(0), bootmap_pfn, pfn_base,
178 					 max_low_pfn);
179 
180 	/* Now register the available physical memory with the
181 	 * allocator.
182 	 */
183 	*pages_avail = 0;
184 	for (i = 0; sp_banks[i].num_bytes != 0; i++) {
185 		unsigned long curr_pfn, last_pfn;
186 
187 		curr_pfn = sp_banks[i].base_addr >> PAGE_SHIFT;
188 		if (curr_pfn >= max_low_pfn)
189 			break;
190 
191 		last_pfn = (sp_banks[i].base_addr + sp_banks[i].num_bytes) >> PAGE_SHIFT;
192 		if (last_pfn > max_low_pfn)
193 			last_pfn = max_low_pfn;
194 
195 		/*
196 		 * .. finally, did all the rounding and playing
197 		 * around just make the area go away?
198 		 */
199 		if (last_pfn <= curr_pfn)
200 			continue;
201 
202 		size = (last_pfn - curr_pfn) << PAGE_SHIFT;
203 		*pages_avail += last_pfn - curr_pfn;
204 
205 		free_bootmem(sp_banks[i].base_addr, size);
206 	}
207 
208 #ifdef CONFIG_BLK_DEV_INITRD
209 	if (initrd_start) {
210 		/* Reserve the initrd image area. */
211 		size = initrd_end - initrd_start;
212 		reserve_bootmem(initrd_start, size, BOOTMEM_DEFAULT);
213 		*pages_avail -= PAGE_ALIGN(size) >> PAGE_SHIFT;
214 
215 		initrd_start = (initrd_start - phys_base) + PAGE_OFFSET;
216 		initrd_end = (initrd_end - phys_base) + PAGE_OFFSET;
217 	}
218 #endif
219 	/* Reserve the kernel text/data/bss. */
220 	size = (start_pfn << PAGE_SHIFT) - phys_base;
221 	reserve_bootmem(phys_base, size, BOOTMEM_DEFAULT);
222 	*pages_avail -= PAGE_ALIGN(size) >> PAGE_SHIFT;
223 
224 	/* Reserve the bootmem map.   We do not account for it
225 	 * in pages_avail because we will release that memory
226 	 * in free_all_bootmem.
227 	 */
228 	size = bootmap_size;
229 	reserve_bootmem((bootmap_pfn << PAGE_SHIFT), size, BOOTMEM_DEFAULT);
230 	*pages_avail -= PAGE_ALIGN(size) >> PAGE_SHIFT;
231 
232 	return max_pfn;
233 }
234 
235 /*
236  * paging_init() sets up the page tables: We call the MMU specific
237  * init routine based upon the Sun model type on the Sparc.
238  *
239  */
240 void __init paging_init(void)
241 {
242 	srmmu_paging_init();
243 	prom_build_devicetree();
244 	of_fill_in_cpu_data();
245 	device_scan();
246 }
247 
248 static void __init taint_real_pages(void)
249 {
250 	int i;
251 
252 	for (i = 0; sp_banks[i].num_bytes; i++) {
253 		unsigned long start, end;
254 
255 		start = sp_banks[i].base_addr;
256 		end = start + sp_banks[i].num_bytes;
257 
258 		while (start < end) {
259 			set_bit(start >> 20, sparc_valid_addr_bitmap);
260 			start += PAGE_SIZE;
261 		}
262 	}
263 }
264 
265 static void map_high_region(unsigned long start_pfn, unsigned long end_pfn)
266 {
267 	unsigned long tmp;
268 
269 #ifdef CONFIG_DEBUG_HIGHMEM
270 	printk("mapping high region %08lx - %08lx\n", start_pfn, end_pfn);
271 #endif
272 
273 	for (tmp = start_pfn; tmp < end_pfn; tmp++)
274 		free_highmem_page(pfn_to_page(tmp));
275 }
276 
277 void __init mem_init(void)
278 {
279 	int i;
280 
281 	if (PKMAP_BASE+LAST_PKMAP*PAGE_SIZE >= FIXADDR_START) {
282 		prom_printf("BUG: fixmap and pkmap areas overlap\n");
283 		prom_printf("pkbase: 0x%lx pkend: 0x%lx fixstart 0x%lx\n",
284 		       PKMAP_BASE,
285 		       (unsigned long)PKMAP_BASE+LAST_PKMAP*PAGE_SIZE,
286 		       FIXADDR_START);
287 		prom_printf("Please mail sparclinux@vger.kernel.org.\n");
288 		prom_halt();
289 	}
290 
291 
292 	/* Saves us work later. */
293 	memset((void *)empty_zero_page, 0, PAGE_SIZE);
294 
295 	i = last_valid_pfn >> ((20 - PAGE_SHIFT) + 5);
296 	i += 1;
297 	sparc_valid_addr_bitmap = (unsigned long *)
298 		__alloc_bootmem(i << 2, SMP_CACHE_BYTES, 0UL);
299 
300 	if (sparc_valid_addr_bitmap == NULL) {
301 		prom_printf("mem_init: Cannot alloc valid_addr_bitmap.\n");
302 		prom_halt();
303 	}
304 	memset(sparc_valid_addr_bitmap, 0, i << 2);
305 
306 	taint_real_pages();
307 
308 	max_mapnr = last_valid_pfn - pfn_base;
309 	high_memory = __va(max_low_pfn << PAGE_SHIFT);
310 	free_all_bootmem();
311 
312 	for (i = 0; sp_banks[i].num_bytes != 0; i++) {
313 		unsigned long start_pfn = sp_banks[i].base_addr >> PAGE_SHIFT;
314 		unsigned long end_pfn = (sp_banks[i].base_addr + sp_banks[i].num_bytes) >> PAGE_SHIFT;
315 
316 		if (end_pfn <= highstart_pfn)
317 			continue;
318 
319 		if (start_pfn < highstart_pfn)
320 			start_pfn = highstart_pfn;
321 
322 		map_high_region(start_pfn, end_pfn);
323 	}
324 
325 	mem_init_print_info(NULL);
326 }
327 
328 void free_initmem (void)
329 {
330 	free_initmem_default(POISON_FREE_INITMEM);
331 }
332 
333 #ifdef CONFIG_BLK_DEV_INITRD
334 void free_initrd_mem(unsigned long start, unsigned long end)
335 {
336 	free_reserved_area((void *)start, (void *)end, POISON_FREE_INITMEM,
337 			   "initrd");
338 }
339 #endif
340 
341 void sparc_flush_page_to_ram(struct page *page)
342 {
343 	unsigned long vaddr = (unsigned long)page_address(page);
344 
345 	if (vaddr)
346 		__flush_page_to_ram(vaddr);
347 }
348 EXPORT_SYMBOL(sparc_flush_page_to_ram);
349