xref: /openbmc/linux/arch/xtensa/mm/init.c (revision 9ba067f9)
13f65ce4dSChris Zankel /*
23f65ce4dSChris Zankel  * arch/xtensa/mm/init.c
33f65ce4dSChris Zankel  *
43f65ce4dSChris Zankel  * Derived from MIPS, PPC.
53f65ce4dSChris Zankel  *
63f65ce4dSChris Zankel  * This file is subject to the terms and conditions of the GNU General Public
73f65ce4dSChris Zankel  * License.  See the file "COPYING" in the main directory of this archive
83f65ce4dSChris Zankel  * for more details.
93f65ce4dSChris Zankel  *
103f65ce4dSChris Zankel  * Copyright (C) 2001 - 2005 Tensilica Inc.
113f65ce4dSChris Zankel  *
123f65ce4dSChris Zankel  * Chris Zankel	<chris@zankel.net>
133f65ce4dSChris Zankel  * Joe Taylor	<joe@tensilica.com, joetylr@yahoo.com>
143f65ce4dSChris Zankel  * Marc Gauthier
153f65ce4dSChris Zankel  * Kevin Chea
163f65ce4dSChris Zankel  */
173f65ce4dSChris Zankel 
183f65ce4dSChris Zankel #include <linux/kernel.h>
193f65ce4dSChris Zankel #include <linux/errno.h>
203f65ce4dSChris Zankel #include <linux/bootmem.h>
215a0e3ad6STejun Heo #include <linux/gfp.h>
223f65ce4dSChris Zankel #include <linux/swap.h>
236656920bSChris Zankel #include <linux/mman.h>
246656920bSChris Zankel #include <linux/nodemask.h>
256656920bSChris Zankel #include <linux/mm.h>
263f65ce4dSChris Zankel 
273f65ce4dSChris Zankel #include <asm/bootparam.h>
283f65ce4dSChris Zankel #include <asm/page.h>
29f022d0faSGeert Uytterhoeven #include <asm/sections.h>
309ba067f9SMax Filippov #include <asm/sysmem.h>
319ba067f9SMax Filippov 
329ba067f9SMax Filippov struct sysmem_info sysmem __initdata;
339ba067f9SMax Filippov 
349ba067f9SMax Filippov int __init add_sysmem_bank(unsigned long start, unsigned long end)
359ba067f9SMax Filippov {
369ba067f9SMax Filippov 	if (sysmem.nr_banks >= SYSMEM_BANKS_MAX) {
379ba067f9SMax Filippov 		pr_warn("Ignoring memory bank 0x%08lx size %ldKB\n",
389ba067f9SMax Filippov 			start, end - start);
399ba067f9SMax Filippov 		return -EINVAL;
409ba067f9SMax Filippov 	}
419ba067f9SMax Filippov 	sysmem.bank[sysmem.nr_banks].start = PAGE_ALIGN(start);
429ba067f9SMax Filippov 	sysmem.bank[sysmem.nr_banks].end   = end & PAGE_MASK;
439ba067f9SMax Filippov 	sysmem.nr_banks++;
449ba067f9SMax Filippov 
459ba067f9SMax Filippov 	return 0;
469ba067f9SMax Filippov }
473f65ce4dSChris Zankel 
483f65ce4dSChris Zankel /*
493f65ce4dSChris Zankel  * mem_reserve(start, end, must_exist)
503f65ce4dSChris Zankel  *
513f65ce4dSChris Zankel  * Reserve some memory from the memory pool.
523f65ce4dSChris Zankel  *
533f65ce4dSChris Zankel  * Parameters:
543f65ce4dSChris Zankel  *  start	Start of region,
553f65ce4dSChris Zankel  *  end		End of region,
563f65ce4dSChris Zankel  *  must_exist	Must exist in memory pool.
573f65ce4dSChris Zankel  *
583f65ce4dSChris Zankel  * Returns:
593f65ce4dSChris Zankel  *  0 (memory area couldn't be mapped)
603f65ce4dSChris Zankel  * -1 (success)
613f65ce4dSChris Zankel  */
623f65ce4dSChris Zankel 
633f65ce4dSChris Zankel int __init mem_reserve(unsigned long start, unsigned long end, int must_exist)
643f65ce4dSChris Zankel {
653f65ce4dSChris Zankel 	int i;
663f65ce4dSChris Zankel 
673f65ce4dSChris Zankel 	if (start == end)
683f65ce4dSChris Zankel 		return 0;
693f65ce4dSChris Zankel 
703f65ce4dSChris Zankel 	start = start & PAGE_MASK;
713f65ce4dSChris Zankel 	end = PAGE_ALIGN(end);
723f65ce4dSChris Zankel 
733f65ce4dSChris Zankel 	for (i = 0; i < sysmem.nr_banks; i++)
743f65ce4dSChris Zankel 		if (start < sysmem.bank[i].end
753f65ce4dSChris Zankel 		    && end >= sysmem.bank[i].start)
763f65ce4dSChris Zankel 			break;
773f65ce4dSChris Zankel 
783f65ce4dSChris Zankel 	if (i == sysmem.nr_banks) {
793f65ce4dSChris Zankel 		if (must_exist)
803f65ce4dSChris Zankel 			printk (KERN_WARNING "mem_reserve: [0x%0lx, 0x%0lx) "
813f65ce4dSChris Zankel 				"not in any region!\n", start, end);
823f65ce4dSChris Zankel 		return 0;
833f65ce4dSChris Zankel 	}
843f65ce4dSChris Zankel 
853f65ce4dSChris Zankel 	if (start > sysmem.bank[i].start) {
863f65ce4dSChris Zankel 		if (end < sysmem.bank[i].end) {
873f65ce4dSChris Zankel 			/* split entry */
883f65ce4dSChris Zankel 			if (sysmem.nr_banks >= SYSMEM_BANKS_MAX)
893f65ce4dSChris Zankel 				panic("meminfo overflow\n");
903f65ce4dSChris Zankel 			sysmem.bank[sysmem.nr_banks].start = end;
913f65ce4dSChris Zankel 			sysmem.bank[sysmem.nr_banks].end = sysmem.bank[i].end;
923f65ce4dSChris Zankel 			sysmem.nr_banks++;
933f65ce4dSChris Zankel 		}
943f65ce4dSChris Zankel 		sysmem.bank[i].end = start;
95c4c4594bSChris Zankel 
96c4c4594bSChris Zankel 	} else if (end < sysmem.bank[i].end) {
973f65ce4dSChris Zankel 		sysmem.bank[i].start = end;
98c4c4594bSChris Zankel 
99c4c4594bSChris Zankel 	} else {
1003f65ce4dSChris Zankel 		/* remove entry */
1013f65ce4dSChris Zankel 		sysmem.nr_banks--;
1023f65ce4dSChris Zankel 		sysmem.bank[i].start = sysmem.bank[sysmem.nr_banks].start;
1033f65ce4dSChris Zankel 		sysmem.bank[i].end   = sysmem.bank[sysmem.nr_banks].end;
1043f65ce4dSChris Zankel 	}
1053f65ce4dSChris Zankel 	return -1;
1063f65ce4dSChris Zankel }
1073f65ce4dSChris Zankel 
1083f65ce4dSChris Zankel 
1093f65ce4dSChris Zankel /*
110e9d6dca5SMax Filippov  * Initialize the bootmem system and give it all low memory we have available.
1113f65ce4dSChris Zankel  */
1123f65ce4dSChris Zankel 
1133f65ce4dSChris Zankel void __init bootmem_init(void)
1143f65ce4dSChris Zankel {
1153f65ce4dSChris Zankel 	unsigned long pfn;
1163f65ce4dSChris Zankel 	unsigned long bootmap_start, bootmap_size;
1173f65ce4dSChris Zankel 	int i;
1183f65ce4dSChris Zankel 
1193f65ce4dSChris Zankel 	max_low_pfn = max_pfn = 0;
1203f65ce4dSChris Zankel 	min_low_pfn = ~0;
1213f65ce4dSChris Zankel 
1223f65ce4dSChris Zankel 	for (i=0; i < sysmem.nr_banks; i++) {
1233f65ce4dSChris Zankel 		pfn = PAGE_ALIGN(sysmem.bank[i].start) >> PAGE_SHIFT;
1243f65ce4dSChris Zankel 		if (pfn < min_low_pfn)
1253f65ce4dSChris Zankel 			min_low_pfn = pfn;
1263f65ce4dSChris Zankel 		pfn = PAGE_ALIGN(sysmem.bank[i].end - 1) >> PAGE_SHIFT;
1273f65ce4dSChris Zankel 		if (pfn > max_pfn)
1283f65ce4dSChris Zankel 			max_pfn = pfn;
1293f65ce4dSChris Zankel 	}
1303f65ce4dSChris Zankel 
1313f65ce4dSChris Zankel 	if (min_low_pfn > max_pfn)
1323f65ce4dSChris Zankel 		panic("No memory found!\n");
1333f65ce4dSChris Zankel 
134173d6681SChris Zankel 	max_low_pfn = max_pfn < MAX_MEM_PFN >> PAGE_SHIFT ?
135173d6681SChris Zankel 		max_pfn : MAX_MEM_PFN >> PAGE_SHIFT;
1363f65ce4dSChris Zankel 
1373f65ce4dSChris Zankel 	/* Find an area to use for the bootmem bitmap. */
1383f65ce4dSChris Zankel 
139264da9f7SJohannes Weiner 	bootmap_size = bootmem_bootmap_pages(max_low_pfn - min_low_pfn);
140264da9f7SJohannes Weiner 	bootmap_size <<= PAGE_SHIFT;
1413f65ce4dSChris Zankel 	bootmap_start = ~0;
1423f65ce4dSChris Zankel 
1433f65ce4dSChris Zankel 	for (i=0; i<sysmem.nr_banks; i++)
1443f65ce4dSChris Zankel 		if (sysmem.bank[i].end - sysmem.bank[i].start >= bootmap_size) {
1453f65ce4dSChris Zankel 			bootmap_start = sysmem.bank[i].start;
1463f65ce4dSChris Zankel 			break;
1473f65ce4dSChris Zankel 		}
1483f65ce4dSChris Zankel 
1493f65ce4dSChris Zankel 	if (bootmap_start == ~0UL)
1503f65ce4dSChris Zankel 		panic("Cannot find %ld bytes for bootmap\n", bootmap_size);
1513f65ce4dSChris Zankel 
1523f65ce4dSChris Zankel 	/* Reserve the bootmem bitmap area */
1533f65ce4dSChris Zankel 
1543f65ce4dSChris Zankel 	mem_reserve(bootmap_start, bootmap_start + bootmap_size, 1);
1550bef42e5SJohannes Weiner 	bootmap_size = init_bootmem_node(NODE_DATA(0),
1563f65ce4dSChris Zankel 					 bootmap_start >> PAGE_SHIFT,
1570bef42e5SJohannes Weiner 					 min_low_pfn,
1583f65ce4dSChris Zankel 					 max_low_pfn);
1593f65ce4dSChris Zankel 
1603f65ce4dSChris Zankel 	/* Add all remaining memory pieces into the bootmem map */
1613f65ce4dSChris Zankel 
162e9d6dca5SMax Filippov 	for (i = 0; i < sysmem.nr_banks; i++) {
163e9d6dca5SMax Filippov 		if (sysmem.bank[i].start >> PAGE_SHIFT < max_low_pfn) {
164e9d6dca5SMax Filippov 			unsigned long end = min(max_low_pfn << PAGE_SHIFT,
165e9d6dca5SMax Filippov 						sysmem.bank[i].end);
1663f65ce4dSChris Zankel 			free_bootmem(sysmem.bank[i].start,
167e9d6dca5SMax Filippov 				     end - sysmem.bank[i].start);
168e9d6dca5SMax Filippov 		}
169e9d6dca5SMax Filippov 	}
1703f65ce4dSChris Zankel 
1713f65ce4dSChris Zankel }
1723f65ce4dSChris Zankel 
1733f65ce4dSChris Zankel 
174e5083a63SJohannes Weiner void __init zones_init(void)
1753f65ce4dSChris Zankel {
1763f65ce4dSChris Zankel 	unsigned long zones_size[MAX_NR_ZONES];
1773f65ce4dSChris Zankel 	int i;
1783f65ce4dSChris Zankel 
1793f65ce4dSChris Zankel 	/* All pages are DMA-able, so we put them all in the DMA zone. */
1803f65ce4dSChris Zankel 
181c947a585SJohannes Weiner 	zones_size[ZONE_DMA] = max_low_pfn - ARCH_PFN_OFFSET;
1823f65ce4dSChris Zankel 	for (i = 1; i < MAX_NR_ZONES; i++)
1833f65ce4dSChris Zankel 		zones_size[i] = 0;
1843f65ce4dSChris Zankel 
1853f65ce4dSChris Zankel #ifdef CONFIG_HIGHMEM
1863f65ce4dSChris Zankel 	zones_size[ZONE_HIGHMEM] = max_pfn - max_low_pfn;
1873f65ce4dSChris Zankel #endif
1883f65ce4dSChris Zankel 
189c947a585SJohannes Weiner 	free_area_init_node(0, zones_size, ARCH_PFN_OFFSET, NULL);
1903f65ce4dSChris Zankel }
1913f65ce4dSChris Zankel 
1923f65ce4dSChris Zankel /*
1933f65ce4dSChris Zankel  * Initialize memory pages.
1943f65ce4dSChris Zankel  */
1953f65ce4dSChris Zankel 
1963f65ce4dSChris Zankel void __init mem_init(void)
1973f65ce4dSChris Zankel {
198808c2c37SJiang Liu 	max_mapnr = max_low_pfn - ARCH_PFN_OFFSET;
199c947a585SJohannes Weiner 	high_memory = (void *) __va(max_low_pfn << PAGE_SHIFT);
2003f65ce4dSChris Zankel 
201288a60cfSChris Zankel #ifdef CONFIG_HIGHMEM
2023f65ce4dSChris Zankel #error HIGHGMEM not implemented in init.c
2033f65ce4dSChris Zankel #endif
2043f65ce4dSChris Zankel 
2050c988534SJiang Liu 	free_all_bootmem();
2063f65ce4dSChris Zankel 
207808c2c37SJiang Liu 	mem_init_print_info(NULL);
2083f65ce4dSChris Zankel }
2093f65ce4dSChris Zankel 
2103f65ce4dSChris Zankel #ifdef CONFIG_BLK_DEV_INITRD
2113f65ce4dSChris Zankel extern int initrd_is_mapped;
2123f65ce4dSChris Zankel 
2133f65ce4dSChris Zankel void free_initrd_mem(unsigned long start, unsigned long end)
2143f65ce4dSChris Zankel {
2157acb2c2eSJiang Liu 	if (initrd_is_mapped)
216dbe67df4SJiang Liu 		free_reserved_area((void *)start, (void *)end, -1, "initrd");
2173f65ce4dSChris Zankel }
2183f65ce4dSChris Zankel #endif
2193f65ce4dSChris Zankel 
2203f65ce4dSChris Zankel void free_initmem(void)
2213f65ce4dSChris Zankel {
222dbe67df4SJiang Liu 	free_initmem_default(-1);
2233f65ce4dSChris Zankel }
224