blob: 3bd0d513bddbde7f95275be6e3ae94bd26729c6f [file] [log] [blame]
Adrian Bunk88278ca2008-05-19 16:53:02 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#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>
Robert Reiff538f3d2008-07-14 22:57:29 -070025#include <linux/pagemap.h>
Robert Reif1aa03652008-09-03 16:29:42 -070026#include <linux/poison.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Sam Ravnborg86ed40b2008-12-27 00:35:12 -080029#include <asm/sections.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#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 */
Sam Ravnborgd1917232014-04-21 21:39:28 +020034#include <asm/setup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <asm/tlb.h>
David S. Miller942a6bd2006-06-23 15:53:31 -070036#include <asm/prom.h>
Konrad Eisele0fd7ef12009-08-17 00:13:31 +000037#include <asm/leon.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Sam Ravnborge8c29c82014-04-21 21:39:19 +020039#include "mm_32.h"
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041unsigned long *sparc_valid_addr_bitmap;
Sam Ravnborg6943f3d2009-01-08 16:58:05 -080042EXPORT_SYMBOL(sparc_valid_addr_bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44unsigned long phys_base;
Sam Ravnborg6943f3d2009-01-08 16:58:05 -080045EXPORT_SYMBOL(phys_base);
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047unsigned long pfn_base;
Sam Ravnborg6943f3d2009-01-08 16:58:05 -080048EXPORT_SYMBOL(pfn_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Linus Torvalds1da177e2005-04-16 15:20:36 -070050struct sparc_phys_banks sp_banks[SPARC_PHYS_BANKS+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Linus Torvalds1da177e2005-04-16 15:20:36 -070052/* Initial ramdisk setup */
53extern unsigned int sparc_ramdisk_image;
54extern unsigned int sparc_ramdisk_size;
55
56unsigned long highstart_pfn, highend_pfn;
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058unsigned long last_valid_pfn;
59
60unsigned 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
Adrian Bunk50215d62008-06-05 11:41:51 -070081static unsigned long calc_max_low_pfn(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
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
103unsigned 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) {
David S. Millerefb89632007-08-20 15:52:18 -0700159 if (sparc_ramdisk_image >= (unsigned long)&_end - 2 * PAGE_SIZE)
160 sparc_ramdisk_image -= KERNBASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 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;
Bernhard Walle72a7fe32008-02-07 00:15:17 -0800212 reserve_bootmem(initrd_start, size, BOOTMEM_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 *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;
Bernhard Walle72a7fe32008-02-07 00:15:17 -0800221 reserve_bootmem(phys_base, size, BOOTMEM_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 *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;
Bernhard Walle72a7fe32008-02-07 00:15:17 -0800229 reserve_bootmem((bootmap_pfn << PAGE_SHIFT), size, BOOTMEM_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 *pages_avail -= PAGE_ALIGN(size) >> PAGE_SHIFT;
231
232 return max_pfn;
233}
234
235/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 * 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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240void __init paging_init(void)
241{
Sam Ravnborgcc52aea2012-07-26 11:02:22 +0000242 srmmu_paging_init();
David S. Miller942a6bd2006-06-23 15:53:31 -0700243 prom_build_devicetree();
David S. Miller890db402009-04-01 03:13:15 -0700244 of_fill_in_cpu_data();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 device_scan();
246}
247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248static 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
Adrian Bunk50215d62008-06-05 11:41:51 -0700265static void map_high_region(unsigned long start_pfn, unsigned long end_pfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
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
Jiang Liu32e1a102013-04-29 15:07:10 -0700273 for (tmp = start_pfn; tmp < end_pfn; tmp++)
274 free_highmem_page(pfn_to_page(tmp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275}
276
277void __init mem_init(void)
278{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 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. */
Orlando Ariasdeba8042017-05-16 15:34:00 -0400293 memset((void *)empty_zero_page, 0, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
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);
Jiang Liu0c988532013-07-03 15:03:24 -0700310 free_all_bootmem();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 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
Jiang Liudceccbe2013-07-03 15:04:14 -0700325 mem_init_print_info(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326}
327
328void free_initmem (void)
329{
Jiang Liudceccbe2013-07-03 15:04:14 -0700330 free_initmem_default(POISON_FREE_INITMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331}
332
333#ifdef CONFIG_BLK_DEV_INITRD
334void free_initrd_mem(unsigned long start, unsigned long end)
335{
Jiang Liudceccbe2013-07-03 15:04:14 -0700336 free_reserved_area((void *)start, (void *)end, POISON_FREE_INITMEM,
337 "initrd");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338}
339#endif
340
341void 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}
Sam Ravnborg6943f3d2009-01-08 16:58:05 -0800348EXPORT_SYMBOL(sparc_flush_page_to_ram);