summaryrefslogtreecommitdiffstats
path: root/common/efi/efi-iomem.c
blob: 4d34328a580289b15aa637ec8143e60c1cd4478b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// SPDX-License-Identifier: GPL-2.0
// Copyright (c) 2019 Ahmad Fatoum, Pengutronix

#define pr_fmt(fmt) "efi-iomem: " fmt

#include <common.h>
#include <init.h>
#include <efi.h>
#include <efi/efi.h>
#include <memory.h>
#include <linux/sizes.h>

static int efi_parse_mmap(struct efi_memory_desc *desc, bool verbose)
{
	struct resource *res;
	u32 flags;
	const char *name;
	char *fullname;
	resource_size_t va_base, va_size;
	int ret = 0;

	va_size = desc->npages * SZ_4K;
	if (!va_size)
		return 0;

	/* XXX At least OVMF doesn't populate ->virt_start and leaves it at zero
	 * for all mapping. Thus assume a 1:1 mapping and ignore virt_start
	 */
	va_base = desc->phys_start;

	switch (desc->type) {
	case EFI_RESERVED_TYPE:
		if (verbose)
			return 0;
		name = "reserved";
		flags = IORESOURCE_MEM | IORESOURCE_DISABLED;
		break;
	case EFI_LOADER_CODE:
		name = "loader code";
		flags = IORESOURCE_MEM | IORESOURCE_READONLY;
		break;
	case EFI_LOADER_DATA:
		name = "loader data";
		flags = IORESOURCE_MEM;
		break;
	case EFI_BOOT_SERVICES_CODE:
		if (!verbose)
			return 0;
		name = "boot services code";
		flags = IORESOURCE_MEM | IORESOURCE_READONLY;
		break;
	case EFI_BOOT_SERVICES_DATA:
		if (!verbose)
			return 0;
		name = "boot services data";
		flags = IORESOURCE_MEM;
		break;
	case EFI_RUNTIME_SERVICES_CODE:
		if (!verbose)
			return 0;
		name = "runtime services code";
		flags = IORESOURCE_MEM | IORESOURCE_READONLY;
		break;
	case EFI_RUNTIME_SERVICES_DATA:
		if (!verbose)
			return 0;
		name = "runtime services data";
		flags = IORESOURCE_MEM;
		break;
	case EFI_CONVENTIONAL_MEMORY:
		if (!verbose)
			return 0;
		name = "conventional memory";
		flags = IORESOURCE_MEM | IORESOURCE_PREFETCH | IORESOURCE_CACHEABLE;
		break;
	case EFI_UNUSABLE_MEMORY:
		if (!verbose)
			return 0;
		name = "unusable";
		flags = IORESOURCE_MEM | IORESOURCE_DISABLED;
		break;
	case EFI_ACPI_RECLAIM_MEMORY:
		if (!verbose)
			return 0;
		name = "ACPI reclaim memory";
		flags = IORESOURCE_MEM | IORESOURCE_READONLY;
		break;
	case EFI_ACPI_MEMORY_NVS:
		if (!verbose)
			return 0;
		name = "ACPI NVS memory";
		flags = IORESOURCE_MEM | IORESOURCE_READONLY;
		break;
	case EFI_MEMORY_MAPPED_IO:
		if (!verbose)
			return 0;
		name = "MMIO";
		flags = IORESOURCE_MEM;
		break;
	case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
		if (!verbose)
			return 0;
		name = "MMIOPORT";
		flags = IORESOURCE_IO;
		break;
	case EFI_PAL_CODE:
		if (!verbose)
			return 0;
		name = "PAL code";
		flags = IORESOURCE_MEM | IORESOURCE_ROM_BIOS_COPY;
		break;
	default:
		if (!(desc->type & (1U << 31))) {
			pr_warn("illegal memory type = %u >= %u\n",
				desc->type, EFI_MAX_MEMORY_TYPE);
			return -EINVAL;
		}

		if (!verbose)
			return 0;

		name = "vendor reserved";
		flags = IORESOURCE_MEM | IORESOURCE_ROM_BIOS_COPY;
	}

	fullname = xasprintf("%s@%llx", name, desc->phys_start);

	pr_debug("%s: (0x%llx+0x%llx)\n", fullname, va_base, va_size);

	res = request_iomem_region(fullname, va_base, va_base + va_size - 1);
	if (IS_ERR(res)) {
		ret = PTR_ERR(res);
		goto out;
	}

	res->flags |= flags;

out:
	free(fullname);
	return ret;
}

static int efi_barebox_populate_mmap(void)
{
	void *desc;
	u8 *mmap_buf = NULL;
	efi_status_t efiret;
	size_t mmap_size;
	size_t mapkey;
	size_t descsz;
	u32 descver;
	int ret = 0;

	mmap_size = sizeof(struct efi_memory_desc);

	do {
		mmap_buf = xrealloc(mmap_buf, mmap_size);
		efiret = BS->get_memory_map(&mmap_size, mmap_buf,
					    &mapkey, &descsz, &descver);
	} while (efiret == EFI_BUFFER_TOO_SMALL);

	if (EFI_ERROR(efiret)) {
		ret = -efi_errno(efiret);
		goto out;
	}

	if (descver != 1) {
		ret = -ENOSYS;
		goto out;
	}

	for (desc = mmap_buf; (u8 *)desc < mmap_buf + mmap_size; desc += descsz)
		efi_parse_mmap(desc, __is_defined(DEBUG));

out:
	free(mmap_buf);
	return ret;
}
mem_initcall(efi_barebox_populate_mmap);