xref: /linux/arch/loongarch/kernel/mem.c (revision b795fb9f)
1628c3bb4SHuacai Chen // SPDX-License-Identifier: GPL-2.0-or-later
2628c3bb4SHuacai Chen /*
3628c3bb4SHuacai Chen  * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
4628c3bb4SHuacai Chen  */
5628c3bb4SHuacai Chen #include <linux/efi.h>
6628c3bb4SHuacai Chen #include <linux/initrd.h>
7628c3bb4SHuacai Chen #include <linux/memblock.h>
8628c3bb4SHuacai Chen 
9628c3bb4SHuacai Chen #include <asm/bootinfo.h>
10628c3bb4SHuacai Chen #include <asm/loongson.h>
11628c3bb4SHuacai Chen #include <asm/sections.h>
12628c3bb4SHuacai Chen 
memblock_init(void)13628c3bb4SHuacai Chen void __init memblock_init(void)
14628c3bb4SHuacai Chen {
15628c3bb4SHuacai Chen 	u32 mem_type;
16628c3bb4SHuacai Chen 	u64 mem_start, mem_end, mem_size;
17628c3bb4SHuacai Chen 	efi_memory_desc_t *md;
18628c3bb4SHuacai Chen 
19628c3bb4SHuacai Chen 	/* Parse memory information */
20628c3bb4SHuacai Chen 	for_each_efi_memory_desc(md) {
21628c3bb4SHuacai Chen 		mem_type = md->type;
22628c3bb4SHuacai Chen 		mem_start = md->phys_addr;
23628c3bb4SHuacai Chen 		mem_size = md->num_pages << EFI_PAGE_SHIFT;
24628c3bb4SHuacai Chen 		mem_end = mem_start + mem_size;
25628c3bb4SHuacai Chen 
26628c3bb4SHuacai Chen 		switch (mem_type) {
27628c3bb4SHuacai Chen 		case EFI_LOADER_CODE:
28628c3bb4SHuacai Chen 		case EFI_LOADER_DATA:
29628c3bb4SHuacai Chen 		case EFI_BOOT_SERVICES_CODE:
30628c3bb4SHuacai Chen 		case EFI_BOOT_SERVICES_DATA:
31628c3bb4SHuacai Chen 		case EFI_PERSISTENT_MEMORY:
32628c3bb4SHuacai Chen 		case EFI_CONVENTIONAL_MEMORY:
33628c3bb4SHuacai Chen 			memblock_add(mem_start, mem_size);
34628c3bb4SHuacai Chen 			if (max_low_pfn < (mem_end >> PAGE_SHIFT))
35628c3bb4SHuacai Chen 				max_low_pfn = mem_end >> PAGE_SHIFT;
36628c3bb4SHuacai Chen 			break;
37628c3bb4SHuacai Chen 		case EFI_PAL_CODE:
38628c3bb4SHuacai Chen 		case EFI_UNUSABLE_MEMORY:
39628c3bb4SHuacai Chen 		case EFI_ACPI_RECLAIM_MEMORY:
40628c3bb4SHuacai Chen 			memblock_add(mem_start, mem_size);
41628c3bb4SHuacai Chen 			fallthrough;
42628c3bb4SHuacai Chen 		case EFI_RESERVED_TYPE:
43628c3bb4SHuacai Chen 		case EFI_RUNTIME_SERVICES_CODE:
44628c3bb4SHuacai Chen 		case EFI_RUNTIME_SERVICES_DATA:
45628c3bb4SHuacai Chen 		case EFI_MEMORY_MAPPED_IO:
46628c3bb4SHuacai Chen 		case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
47628c3bb4SHuacai Chen 			memblock_reserve(mem_start, mem_size);
48628c3bb4SHuacai Chen 			break;
49628c3bb4SHuacai Chen 		}
50628c3bb4SHuacai Chen 	}
51628c3bb4SHuacai Chen 
52628c3bb4SHuacai Chen 	memblock_set_current_limit(PFN_PHYS(max_low_pfn));
53628c3bb4SHuacai Chen 
54628c3bb4SHuacai Chen 	/* Reserve the first 2MB */
55628c3bb4SHuacai Chen 	memblock_reserve(PHYS_OFFSET, 0x200000);
56628c3bb4SHuacai Chen 
57628c3bb4SHuacai Chen 	/* Reserve the kernel text/data/bss */
58628c3bb4SHuacai Chen 	memblock_reserve(__pa_symbol(&_text),
59628c3bb4SHuacai Chen 			 __pa_symbol(&_end) - __pa_symbol(&_text));
60*b795fb9fSHuacai Chen 
61*b795fb9fSHuacai Chen 	memblock_set_node(0, PHYS_ADDR_MAX, &memblock.memory, 0);
62*b795fb9fSHuacai Chen 	memblock_set_node(0, PHYS_ADDR_MAX, &memblock.reserved, 0);
63628c3bb4SHuacai Chen }
64