init.c 9.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/*
 * arch/xtensa/mm/init.c
 *
 * Derived from MIPS, PPC.
 *
 * This file is subject to the terms and conditions of the GNU General Public
 * License.  See the file "COPYING" in the main directory of this archive
 * for more details.
 *
 * Copyright (C) 2001 - 2005 Tensilica Inc.
11
 * Copyright (C) 2014 Cadence Design Systems Inc.
12 13 14 15 16 17 18 19 20 21
 *
 * Chris Zankel	<chris@zankel.net>
 * Joe Taylor	<joe@tensilica.com, joetylr@yahoo.com>
 * Marc Gauthier
 * Kevin Chea
 */

#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/bootmem.h>
22
#include <linux/gfp.h>
M
Max Filippov 已提交
23
#include <linux/highmem.h>
24
#include <linux/swap.h>
25 26 27
#include <linux/mman.h>
#include <linux/nodemask.h>
#include <linux/mm.h>
28 29 30

#include <asm/bootparam.h>
#include <asm/page.h>
31
#include <asm/sections.h>
32 33 34 35
#include <asm/sysmem.h>

struct sysmem_info sysmem __initdata;

36 37 38 39 40 41 42 43 44 45 46
static void __init sysmem_dump(void)
{
	unsigned i;

	pr_debug("Sysmem:\n");
	for (i = 0; i < sysmem.nr_banks; ++i)
		pr_debug("  0x%08lx - 0x%08lx (%ldK)\n",
			 sysmem.bank[i].start, sysmem.bank[i].end,
			 (sysmem.bank[i].end - sysmem.bank[i].start) >> 10);
}

47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
/*
 * Find bank with maximal .start such that bank.start <= start
 */
static inline struct meminfo * __init find_bank(unsigned long start)
{
	unsigned i;
	struct meminfo *it = NULL;

	for (i = 0; i < sysmem.nr_banks; ++i)
		if (sysmem.bank[i].start <= start)
			it = sysmem.bank + i;
		else
			break;
	return it;
}

/*
 * Move all memory banks starting at 'from' to a new place at 'to',
 * adjust nr_banks accordingly.
 * Both 'from' and 'to' must be inside the sysmem.bank.
 *
 * Returns: 0 (success), -ENOMEM (not enough space in the sysmem.bank).
 */
static int __init move_banks(struct meminfo *to, struct meminfo *from)
{
	unsigned n = sysmem.nr_banks - (from - sysmem.bank);

	if (to > from && to - from + sysmem.nr_banks > SYSMEM_BANKS_MAX)
		return -ENOMEM;
	if (to != from)
		memmove(to, from, n * sizeof(struct meminfo));
	sysmem.nr_banks += to - from;
	return 0;
}

/*
 * Add new bank to sysmem. Resulting sysmem is the union of bytes of the
 * original sysmem and the new bank.
 *
 * Returns: 0 (success), < 0 (error)
 */
88 89
int __init add_sysmem_bank(unsigned long start, unsigned long end)
{
90 91 92 93 94 95 96 97
	unsigned i;
	struct meminfo *it = NULL;
	unsigned long sz;
	unsigned long bank_sz = 0;

	if (start == end ||
	    (start < end) != (PAGE_ALIGN(start) < (end & PAGE_MASK))) {
		pr_warn("Ignoring small memory bank 0x%08lx size: %ld bytes\n",
98 99 100 101
			start, end - start);
		return -EINVAL;
	}

102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
	start = PAGE_ALIGN(start);
	end &= PAGE_MASK;
	sz = end - start;

	it = find_bank(start);

	if (it)
		bank_sz = it->end - it->start;

	if (it && bank_sz >= start - it->start) {
		if (end - it->start > bank_sz)
			it->end = end;
		else
			return 0;
	} else {
		if (!it)
			it = sysmem.bank;
		else
			++it;

		if (it - sysmem.bank < sysmem.nr_banks &&
		    it->start - start <= sz) {
			it->start = start;
			if (it->end - it->start < sz)
				it->end = end;
			else
				return 0;
		} else {
			if (move_banks(it + 1, it) < 0) {
				pr_warn("Ignoring memory bank 0x%08lx size %ld bytes\n",
					start, end - start);
				return -EINVAL;
			}
			it->start = start;
			it->end = end;
			return 0;
		}
	}
	sz = it->end - it->start;
	for (i = it + 1 - sysmem.bank; i < sysmem.nr_banks; ++i)
		if (sysmem.bank[i].start - it->start <= sz) {
			if (sz < sysmem.bank[i].end - it->start)
				it->end = sysmem.bank[i].end;
		} else {
			break;
		}

	move_banks(it + 1, sysmem.bank + i);
150 151
	return 0;
}
152 153 154 155 156

/*
 * mem_reserve(start, end, must_exist)
 *
 * Reserve some memory from the memory pool.
157 158
 * If must_exist is set and a part of the region being reserved does not exist
 * memory map is not altered.
159 160 161 162 163 164 165
 *
 * Parameters:
 *  start	Start of region,
 *  end		End of region,
 *  must_exist	Must exist in memory pool.
 *
 * Returns:
166 167
 *  0 (success)
 *  < 0 (error)
168 169 170 171
 */

int __init mem_reserve(unsigned long start, unsigned long end, int must_exist)
{
172 173 174 175
	struct meminfo *it;
	struct meminfo *rm = NULL;
	unsigned long sz;
	unsigned long bank_sz = 0;
176 177 178

	start = start & PAGE_MASK;
	end = PAGE_ALIGN(end);
179 180 181
	sz = end - start;
	if (!sz)
		return -EINVAL;
182

183 184 185 186
	it = find_bank(start);

	if (it)
		bank_sz = it->end - it->start;
187

188 189 190 191
	if ((!it || end - it->start > bank_sz) && must_exist) {
		pr_warn("mem_reserve: [0x%0lx, 0x%0lx) not in any region!\n",
			start, end);
		return -EINVAL;
192 193
	}

194
	if (it && start - it->start <= bank_sz) {
195 196 197 198 199 200 201 202 203 204 205 206 207
		if (start == it->start) {
			if (end - it->start < bank_sz) {
				it->start = end;
				return 0;
			} else {
				rm = it;
			}
		} else {
			it->end = start;
			if (end - it->start < bank_sz)
				return add_sysmem_bank(end,
						       it->start + bank_sz);
			++it;
208
		}
209
	}
210

211 212
	if (!it)
		it = sysmem.bank;
213

214 215 216 217 218 219 220 221 222
	for (; it < sysmem.bank + sysmem.nr_banks; ++it) {
		if (it->end - start <= sz) {
			if (!rm)
				rm = it;
		} else {
			if (it->start - start < sz)
				it->start = end;
			break;
		}
223
	}
224 225 226 227 228

	if (rm)
		move_banks(rm, it);

	return 0;
229 230 231 232
}


/*
233
 * Initialize the bootmem system and give it all low memory we have available.
234 235 236 237 238 239 240 241
 */

void __init bootmem_init(void)
{
	unsigned long pfn;
	unsigned long bootmap_start, bootmap_size;
	int i;

242 243 244 245 246 247 248 249 250 251 252
	/* Reserve all memory below PLATFORM_DEFAULT_MEM_START, as memory
	 * accounting doesn't work for pages below that address.
	 *
	 * If PLATFORM_DEFAULT_MEM_START is zero reserve page at address 0:
	 * successfull allocations should never return NULL.
	 */
	if (PLATFORM_DEFAULT_MEM_START)
		mem_reserve(0, PLATFORM_DEFAULT_MEM_START, 0);
	else
		mem_reserve(0, 1, 0);

253
	sysmem_dump();
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
	max_low_pfn = max_pfn = 0;
	min_low_pfn = ~0;

	for (i=0; i < sysmem.nr_banks; i++) {
		pfn = PAGE_ALIGN(sysmem.bank[i].start) >> PAGE_SHIFT;
		if (pfn < min_low_pfn)
			min_low_pfn = pfn;
		pfn = PAGE_ALIGN(sysmem.bank[i].end - 1) >> PAGE_SHIFT;
		if (pfn > max_pfn)
			max_pfn = pfn;
	}

	if (min_low_pfn > max_pfn)
		panic("No memory found!\n");

269
	max_low_pfn = min(max_pfn, MAX_LOW_PFN);
270 271 272

	/* Find an area to use for the bootmem bitmap. */

273 274
	bootmap_size = bootmem_bootmap_pages(max_low_pfn - min_low_pfn);
	bootmap_size <<= PAGE_SHIFT;
275 276 277 278 279 280 281 282 283 284 285 286 287 288
	bootmap_start = ~0;

	for (i=0; i<sysmem.nr_banks; i++)
		if (sysmem.bank[i].end - sysmem.bank[i].start >= bootmap_size) {
			bootmap_start = sysmem.bank[i].start;
			break;
		}

	if (bootmap_start == ~0UL)
		panic("Cannot find %ld bytes for bootmap\n", bootmap_size);

	/* Reserve the bootmem bitmap area */

	mem_reserve(bootmap_start, bootmap_start + bootmap_size, 1);
289
	bootmap_size = init_bootmem_node(NODE_DATA(0),
290
					 bootmap_start >> PAGE_SHIFT,
291
					 min_low_pfn,
292 293 294 295
					 max_low_pfn);

	/* Add all remaining memory pieces into the bootmem map */

296 297 298 299 300 301 302 303
	for (i = 0; i < sysmem.nr_banks; i++) {
		if (sysmem.bank[i].start >> PAGE_SHIFT < max_low_pfn) {
			unsigned long end = min(max_low_pfn << PAGE_SHIFT,
						sysmem.bank[i].end);
			free_bootmem(sysmem.bank[i].start,
				     end - sysmem.bank[i].start);
		}
	}
304 305 306 307

}


J
Johannes Weiner 已提交
308
void __init zones_init(void)
309 310
{
	/* All pages are DMA-able, so we put them all in the DMA zone. */
M
Max Filippov 已提交
311 312
	unsigned long zones_size[MAX_NR_ZONES] = {
		[ZONE_DMA] = max_low_pfn - ARCH_PFN_OFFSET,
313
#ifdef CONFIG_HIGHMEM
M
Max Filippov 已提交
314
		[ZONE_HIGHMEM] = max_pfn - max_low_pfn,
315
#endif
M
Max Filippov 已提交
316
	};
317
	free_area_init_node(0, zones_size, ARCH_PFN_OFFSET, NULL);
318 319 320 321 322 323 324 325
}

/*
 * Initialize memory pages.
 */

void __init mem_init(void)
{
326
#ifdef CONFIG_HIGHMEM
M
Max Filippov 已提交
327 328 329 330 331
	unsigned long tmp;

	reset_all_zones_managed_pages();
	for (tmp = max_low_pfn; tmp < max_pfn; tmp++)
		free_highmem_page(pfn_to_page(tmp));
332 333
#endif

M
Max Filippov 已提交
334 335 336
	max_mapnr = max_pfn - ARCH_PFN_OFFSET;
	high_memory = (void *)__va(max_low_pfn << PAGE_SHIFT);

337
	free_all_bootmem();
338

339
	mem_init_print_info(NULL);
M
Max Filippov 已提交
340 341 342 343 344
	pr_info("virtual kernel memory layout:\n"
#ifdef CONFIG_HIGHMEM
		"    pkmap   : 0x%08lx - 0x%08lx  (%5lu kB)\n"
		"    fixmap  : 0x%08lx - 0x%08lx  (%5lu kB)\n"
#endif
345
#ifdef CONFIG_MMU
346
		"    vmalloc : 0x%08lx - 0x%08lx  (%5lu MB)\n"
347 348
#endif
		"    lowmem  : 0x%08lx - 0x%08lx  (%5lu MB)\n",
M
Max Filippov 已提交
349 350 351 352 353 354
#ifdef CONFIG_HIGHMEM
		PKMAP_BASE, PKMAP_BASE + LAST_PKMAP * PAGE_SIZE,
		(LAST_PKMAP*PAGE_SIZE) >> 10,
		FIXADDR_START, FIXADDR_TOP,
		(FIXADDR_TOP - FIXADDR_START) >> 10,
#endif
355
#ifdef CONFIG_MMU
M
Max Filippov 已提交
356 357 358 359
		VMALLOC_START, VMALLOC_END,
		(VMALLOC_END - VMALLOC_START) >> 20,
		PAGE_OFFSET, PAGE_OFFSET +
		(max_low_pfn - min_low_pfn) * PAGE_SIZE,
360 361 362
#else
		min_low_pfn * PAGE_SIZE, max_low_pfn * PAGE_SIZE,
#endif
M
Max Filippov 已提交
363
		((max_low_pfn - min_low_pfn) * PAGE_SIZE) >> 20);
364 365 366 367 368 369 370
}

#ifdef CONFIG_BLK_DEV_INITRD
extern int initrd_is_mapped;

void free_initrd_mem(unsigned long start, unsigned long end)
{
371
	if (initrd_is_mapped)
372
		free_reserved_area((void *)start, (void *)end, -1, "initrd");
373 374 375 376 377
}
#endif

void free_initmem(void)
{
378
	free_initmem_default(-1);
379
}
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429

static void __init parse_memmap_one(char *p)
{
	char *oldp;
	unsigned long start_at, mem_size;

	if (!p)
		return;

	oldp = p;
	mem_size = memparse(p, &p);
	if (p == oldp)
		return;

	switch (*p) {
	case '@':
		start_at = memparse(p + 1, &p);
		add_sysmem_bank(start_at, start_at + mem_size);
		break;

	case '$':
		start_at = memparse(p + 1, &p);
		mem_reserve(start_at, start_at + mem_size, 0);
		break;

	case 0:
		mem_reserve(mem_size, 0, 0);
		break;

	default:
		pr_warn("Unrecognized memmap syntax: %s\n", p);
		break;
	}
}

static int __init parse_memmap_opt(char *str)
{
	while (str) {
		char *k = strchr(str, ',');

		if (k)
			*k++ = 0;

		parse_memmap_one(str);
		str = k;
	}

	return 0;
}
early_param("memmap", parse_memmap_opt);