Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Common EFI memory map functions.
4 */
5
6#define pr_fmt(fmt) "efi: " fmt
7
8#include <linux/init.h>
9#include <linux/kernel.h>
10#include <linux/efi.h>
11#include <linux/io.h>
12#include <linux/memblock.h>
13#include <linux/slab.h>
14
15#include <asm/early_ioremap.h>
16#include <asm/efi.h>
17
18#ifndef __efi_memmap_free
19#define __efi_memmap_free(phys, size, flags) do { } while (0)
20#endif
21
22/**
23 * __efi_memmap_init - Common code for mapping the EFI memory map
24 * @data: EFI memory map data
25 *
26 * This function takes care of figuring out which function to use to
27 * map the EFI memory map in efi.memmap based on how far into the boot
28 * we are.
29 *
30 * During bootup EFI_MEMMAP_LATE in data->flags should be clear since we
31 * only have access to the early_memremap*() functions as the vmalloc
32 * space isn't setup. Once the kernel is fully booted we can fallback
33 * to the more robust memremap*() API.
34 *
35 * Returns zero on success, a negative error code on failure.
36 */
37int __init __efi_memmap_init(struct efi_memory_map_data *data)
38{
39 struct efi_memory_map map;
40 phys_addr_t phys_map;
41
42 phys_map = data->phys_map;
43
44 if (data->flags & EFI_MEMMAP_LATE)
45 map.map = memremap(phys_map, data->size, MEMREMAP_WB);
46 else
47 map.map = early_memremap(phys_map, data->size);
48
49 if (!map.map) {
50 pr_err("Could not map the memory map!\n");
51 return -ENOMEM;
52 }
53
54 if (efi.memmap.flags & (EFI_MEMMAP_MEMBLOCK | EFI_MEMMAP_SLAB))
55 __efi_memmap_free(efi.memmap.phys_map,
56 efi.memmap.desc_size * efi.memmap.nr_map,
57 efi.memmap.flags);
58
59 map.phys_map = data->phys_map;
60 map.nr_map = data->size / data->desc_size;
61 map.map_end = map.map + data->size;
62
63 map.desc_version = data->desc_version;
64 map.desc_size = data->desc_size;
65 map.flags = data->flags;
66
67 set_bit(EFI_MEMMAP, &efi.flags);
68
69 efi.memmap = map;
70
71 return 0;
72}
73
74/**
75 * efi_memmap_init_early - Map the EFI memory map data structure
76 * @data: EFI memory map data
77 *
78 * Use early_memremap() to map the passed in EFI memory map and assign
79 * it to efi.memmap.
80 */
81int __init efi_memmap_init_early(struct efi_memory_map_data *data)
82{
83 /* Cannot go backwards */
84 WARN_ON(efi.memmap.flags & EFI_MEMMAP_LATE);
85
86 data->flags = 0;
87 return __efi_memmap_init(data);
88}
89
90void __init efi_memmap_unmap(void)
91{
92 if (!efi_enabled(EFI_MEMMAP))
93 return;
94
95 if (!(efi.memmap.flags & EFI_MEMMAP_LATE)) {
96 unsigned long size;
97
98 size = efi.memmap.desc_size * efi.memmap.nr_map;
99 early_memunmap(efi.memmap.map, size);
100 } else {
101 memunmap(efi.memmap.map);
102 }
103
104 efi.memmap.map = NULL;
105 clear_bit(EFI_MEMMAP, &efi.flags);
106}
107
108/**
109 * efi_memmap_init_late - Map efi.memmap with memremap()
110 * @phys_addr: Physical address of the new EFI memory map
111 * @size: Size in bytes of the new EFI memory map
112 *
113 * Setup a mapping of the EFI memory map using ioremap_cache(). This
114 * function should only be called once the vmalloc space has been
115 * setup and is therefore not suitable for calling during early EFI
116 * initialise, e.g. in efi_init(). Additionally, it expects
117 * efi_memmap_init_early() to have already been called.
118 *
119 * The reason there are two EFI memmap initialisation
120 * (efi_memmap_init_early() and this late version) is because the
121 * early EFI memmap should be explicitly unmapped once EFI
122 * initialisation is complete as the fixmap space used to map the EFI
123 * memmap (via early_memremap()) is a scarce resource.
124 *
125 * This late mapping is intended to persist for the duration of
126 * runtime so that things like efi_mem_desc_lookup() and
127 * efi_mem_attributes() always work.
128 *
129 * Returns zero on success, a negative error code on failure.
130 */
131int __init efi_memmap_init_late(phys_addr_t addr, unsigned long size)
132{
133 struct efi_memory_map_data data = {
134 .phys_map = addr,
135 .size = size,
136 .flags = EFI_MEMMAP_LATE,
137 };
138
139 /* Did we forget to unmap the early EFI memmap? */
140 WARN_ON(efi.memmap.map);
141
142 /* Were we already called? */
143 WARN_ON(efi.memmap.flags & EFI_MEMMAP_LATE);
144
145 /*
146 * It makes no sense to allow callers to register different
147 * values for the following fields. Copy them out of the
148 * existing early EFI memmap.
149 */
150 data.desc_version = efi.memmap.desc_version;
151 data.desc_size = efi.memmap.desc_size;
152
153 return __efi_memmap_init(&data);
154}
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Common EFI memory map functions.
4 */
5
6#define pr_fmt(fmt) "efi: " fmt
7
8#include <linux/init.h>
9#include <linux/kernel.h>
10#include <linux/efi.h>
11#include <linux/io.h>
12#include <linux/memblock.h>
13#include <linux/slab.h>
14
15#include <asm/early_ioremap.h>
16#include <asm/efi.h>
17
18/**
19 * __efi_memmap_init - Common code for mapping the EFI memory map
20 * @data: EFI memory map data
21 *
22 * This function takes care of figuring out which function to use to
23 * map the EFI memory map in efi.memmap based on how far into the boot
24 * we are.
25 *
26 * During bootup EFI_MEMMAP_LATE in data->flags should be clear since we
27 * only have access to the early_memremap*() functions as the vmalloc
28 * space isn't setup. Once the kernel is fully booted we can fallback
29 * to the more robust memremap*() API.
30 *
31 * Returns: zero on success, a negative error code on failure.
32 */
33int __init __efi_memmap_init(struct efi_memory_map_data *data)
34{
35 struct efi_memory_map map;
36 phys_addr_t phys_map;
37
38 phys_map = data->phys_map;
39
40 if (data->flags & EFI_MEMMAP_LATE)
41 map.map = memremap(phys_map, data->size, MEMREMAP_WB);
42 else
43 map.map = early_memremap(phys_map, data->size);
44
45 if (!map.map) {
46 pr_err("Could not map the memory map!\n");
47 return -ENOMEM;
48 }
49
50 map.phys_map = data->phys_map;
51 map.nr_map = data->size / data->desc_size;
52 map.map_end = map.map + data->size;
53
54 map.desc_version = data->desc_version;
55 map.desc_size = data->desc_size;
56 map.flags = data->flags;
57
58 set_bit(EFI_MEMMAP, &efi.flags);
59
60 efi.memmap = map;
61
62 return 0;
63}
64
65/**
66 * efi_memmap_init_early - Map the EFI memory map data structure
67 * @data: EFI memory map data
68 *
69 * Use early_memremap() to map the passed in EFI memory map and assign
70 * it to efi.memmap.
71 *
72 * Returns: zero on success, a negative error code on failure.
73 */
74int __init efi_memmap_init_early(struct efi_memory_map_data *data)
75{
76 /* Cannot go backwards */
77 WARN_ON(efi.memmap.flags & EFI_MEMMAP_LATE);
78
79 data->flags = 0;
80 return __efi_memmap_init(data);
81}
82
83void __init efi_memmap_unmap(void)
84{
85 if (!efi_enabled(EFI_MEMMAP))
86 return;
87
88 if (!(efi.memmap.flags & EFI_MEMMAP_LATE)) {
89 unsigned long size;
90
91 size = efi.memmap.desc_size * efi.memmap.nr_map;
92 early_memunmap(efi.memmap.map, size);
93 } else {
94 memunmap(efi.memmap.map);
95 }
96
97 efi.memmap.map = NULL;
98 clear_bit(EFI_MEMMAP, &efi.flags);
99}
100
101/**
102 * efi_memmap_init_late - Map efi.memmap with memremap()
103 * @addr: Physical address of the new EFI memory map
104 * @size: Size in bytes of the new EFI memory map
105 *
106 * Setup a mapping of the EFI memory map using ioremap_cache(). This
107 * function should only be called once the vmalloc space has been
108 * setup and is therefore not suitable for calling during early EFI
109 * initialise, e.g. in efi_init(). Additionally, it expects
110 * efi_memmap_init_early() to have already been called.
111 *
112 * The reason there are two EFI memmap initialisation
113 * (efi_memmap_init_early() and this late version) is because the
114 * early EFI memmap should be explicitly unmapped once EFI
115 * initialisation is complete as the fixmap space used to map the EFI
116 * memmap (via early_memremap()) is a scarce resource.
117 *
118 * This late mapping is intended to persist for the duration of
119 * runtime so that things like efi_mem_desc_lookup() and
120 * efi_mem_attributes() always work.
121 *
122 * Returns: zero on success, a negative error code on failure.
123 */
124int __init efi_memmap_init_late(phys_addr_t addr, unsigned long size)
125{
126 struct efi_memory_map_data data = {
127 .phys_map = addr,
128 .size = size,
129 .flags = EFI_MEMMAP_LATE,
130 };
131
132 /* Did we forget to unmap the early EFI memmap? */
133 WARN_ON(efi.memmap.map);
134
135 /* Were we already called? */
136 WARN_ON(efi.memmap.flags & EFI_MEMMAP_LATE);
137
138 /*
139 * It makes no sense to allow callers to register different
140 * values for the following fields. Copy them out of the
141 * existing early EFI memmap.
142 */
143 data.desc_version = efi.memmap.desc_version;
144 data.desc_size = efi.memmap.desc_size;
145
146 return __efi_memmap_init(&data);
147}