sh_eth: fix EESIPR values for SH77{34|63}
[linux/fpc-iii.git] / drivers / firmware / efi / memmap.c
blobf03ddecd232b542ce4c7163506b69da0d29ad2c7
1 /*
2 * Common EFI memory map functions.
3 */
5 #define pr_fmt(fmt) "efi: " fmt
7 #include <linux/init.h>
8 #include <linux/kernel.h>
9 #include <linux/efi.h>
10 #include <linux/io.h>
11 #include <asm/early_ioremap.h>
13 /**
14 * __efi_memmap_init - Common code for mapping the EFI memory map
15 * @data: EFI memory map data
16 * @late: Use early or late mapping function?
18 * This function takes care of figuring out which function to use to
19 * map the EFI memory map in efi.memmap based on how far into the boot
20 * we are.
22 * During bootup @late should be %false since we only have access to
23 * the early_memremap*() functions as the vmalloc space isn't setup.
24 * Once the kernel is fully booted we can fallback to the more robust
25 * memremap*() API.
27 * Returns zero on success, a negative error code on failure.
29 static int __init
30 __efi_memmap_init(struct efi_memory_map_data *data, bool late)
32 struct efi_memory_map map;
33 phys_addr_t phys_map;
35 if (efi_enabled(EFI_PARAVIRT))
36 return 0;
38 phys_map = data->phys_map;
40 if (late)
41 map.map = memremap(phys_map, data->size, MEMREMAP_WB);
42 else
43 map.map = early_memremap(phys_map, data->size);
45 if (!map.map) {
46 pr_err("Could not map the memory map!\n");
47 return -ENOMEM;
50 map.phys_map = data->phys_map;
51 map.nr_map = data->size / data->desc_size;
52 map.map_end = map.map + data->size;
54 map.desc_version = data->desc_version;
55 map.desc_size = data->desc_size;
56 map.late = late;
58 set_bit(EFI_MEMMAP, &efi.flags);
60 efi.memmap = map;
62 return 0;
65 /**
66 * efi_memmap_init_early - Map the EFI memory map data structure
67 * @data: EFI memory map data
69 * Use early_memremap() to map the passed in EFI memory map and assign
70 * it to efi.memmap.
72 int __init efi_memmap_init_early(struct efi_memory_map_data *data)
74 /* Cannot go backwards */
75 WARN_ON(efi.memmap.late);
77 return __efi_memmap_init(data, false);
80 void __init efi_memmap_unmap(void)
82 if (!efi.memmap.late) {
83 unsigned long size;
85 size = efi.memmap.desc_size * efi.memmap.nr_map;
86 early_memunmap(efi.memmap.map, size);
87 } else {
88 memunmap(efi.memmap.map);
91 efi.memmap.map = NULL;
92 clear_bit(EFI_MEMMAP, &efi.flags);
95 /**
96 * efi_memmap_init_late - Map efi.memmap with memremap()
97 * @phys_addr: Physical address of the new EFI memory map
98 * @size: Size in bytes of the new EFI memory map
100 * Setup a mapping of the EFI memory map using ioremap_cache(). This
101 * function should only be called once the vmalloc space has been
102 * setup and is therefore not suitable for calling during early EFI
103 * initialise, e.g. in efi_init(). Additionally, it expects
104 * efi_memmap_init_early() to have already been called.
106 * The reason there are two EFI memmap initialisation
107 * (efi_memmap_init_early() and this late version) is because the
108 * early EFI memmap should be explicitly unmapped once EFI
109 * initialisation is complete as the fixmap space used to map the EFI
110 * memmap (via early_memremap()) is a scarce resource.
112 * This late mapping is intended to persist for the duration of
113 * runtime so that things like efi_mem_desc_lookup() and
114 * efi_mem_attributes() always work.
116 * Returns zero on success, a negative error code on failure.
118 int __init efi_memmap_init_late(phys_addr_t addr, unsigned long size)
120 struct efi_memory_map_data data = {
121 .phys_map = addr,
122 .size = size,
125 /* Did we forget to unmap the early EFI memmap? */
126 WARN_ON(efi.memmap.map);
128 /* Were we already called? */
129 WARN_ON(efi.memmap.late);
132 * It makes no sense to allow callers to register different
133 * values for the following fields. Copy them out of the
134 * existing early EFI memmap.
136 data.desc_version = efi.memmap.desc_version;
137 data.desc_size = efi.memmap.desc_size;
139 return __efi_memmap_init(&data, true);
143 * efi_memmap_install - Install a new EFI memory map in efi.memmap
144 * @addr: Physical address of the memory map
145 * @nr_map: Number of entries in the memory map
147 * Unlike efi_memmap_init_*(), this function does not allow the caller
148 * to switch from early to late mappings. It simply uses the existing
149 * mapping function and installs the new memmap.
151 * Returns zero on success, a negative error code on failure.
153 int __init efi_memmap_install(phys_addr_t addr, unsigned int nr_map)
155 struct efi_memory_map_data data;
157 efi_memmap_unmap();
159 data.phys_map = addr;
160 data.size = efi.memmap.desc_size * nr_map;
161 data.desc_version = efi.memmap.desc_version;
162 data.desc_size = efi.memmap.desc_size;
164 return __efi_memmap_init(&data, efi.memmap.late);
168 * efi_memmap_split_count - Count number of additional EFI memmap entries
169 * @md: EFI memory descriptor to split
170 * @range: Address range (start, end) to split around
172 * Returns the number of additional EFI memmap entries required to
173 * accomodate @range.
175 int __init efi_memmap_split_count(efi_memory_desc_t *md, struct range *range)
177 u64 m_start, m_end;
178 u64 start, end;
179 int count = 0;
181 start = md->phys_addr;
182 end = start + (md->num_pages << EFI_PAGE_SHIFT) - 1;
184 /* modifying range */
185 m_start = range->start;
186 m_end = range->end;
188 if (m_start <= start) {
189 /* split into 2 parts */
190 if (start < m_end && m_end < end)
191 count++;
194 if (start < m_start && m_start < end) {
195 /* split into 3 parts */
196 if (m_end < end)
197 count += 2;
198 /* split into 2 parts */
199 if (end <= m_end)
200 count++;
203 return count;
207 * efi_memmap_insert - Insert a memory region in an EFI memmap
208 * @old_memmap: The existing EFI memory map structure
209 * @buf: Address of buffer to store new map
210 * @mem: Memory map entry to insert
212 * It is suggested that you call efi_memmap_split_count() first
213 * to see how large @buf needs to be.
215 void __init efi_memmap_insert(struct efi_memory_map *old_memmap, void *buf,
216 struct efi_mem_range *mem)
218 u64 m_start, m_end, m_attr;
219 efi_memory_desc_t *md;
220 u64 start, end;
221 void *old, *new;
223 /* modifying range */
224 m_start = mem->range.start;
225 m_end = mem->range.end;
226 m_attr = mem->attribute;
229 * The EFI memory map deals with regions in EFI_PAGE_SIZE
230 * units. Ensure that the region described by 'mem' is aligned
231 * correctly.
233 if (!IS_ALIGNED(m_start, EFI_PAGE_SIZE) ||
234 !IS_ALIGNED(m_end + 1, EFI_PAGE_SIZE)) {
235 WARN_ON(1);
236 return;
239 for (old = old_memmap->map, new = buf;
240 old < old_memmap->map_end;
241 old += old_memmap->desc_size, new += old_memmap->desc_size) {
243 /* copy original EFI memory descriptor */
244 memcpy(new, old, old_memmap->desc_size);
245 md = new;
246 start = md->phys_addr;
247 end = md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT) - 1;
249 if (m_start <= start && end <= m_end)
250 md->attribute |= m_attr;
252 if (m_start <= start &&
253 (start < m_end && m_end < end)) {
254 /* first part */
255 md->attribute |= m_attr;
256 md->num_pages = (m_end - md->phys_addr + 1) >>
257 EFI_PAGE_SHIFT;
258 /* latter part */
259 new += old_memmap->desc_size;
260 memcpy(new, old, old_memmap->desc_size);
261 md = new;
262 md->phys_addr = m_end + 1;
263 md->num_pages = (end - md->phys_addr + 1) >>
264 EFI_PAGE_SHIFT;
267 if ((start < m_start && m_start < end) && m_end < end) {
268 /* first part */
269 md->num_pages = (m_start - md->phys_addr) >>
270 EFI_PAGE_SHIFT;
271 /* middle part */
272 new += old_memmap->desc_size;
273 memcpy(new, old, old_memmap->desc_size);
274 md = new;
275 md->attribute |= m_attr;
276 md->phys_addr = m_start;
277 md->num_pages = (m_end - m_start + 1) >>
278 EFI_PAGE_SHIFT;
279 /* last part */
280 new += old_memmap->desc_size;
281 memcpy(new, old, old_memmap->desc_size);
282 md = new;
283 md->phys_addr = m_end + 1;
284 md->num_pages = (end - m_end) >>
285 EFI_PAGE_SHIFT;
288 if ((start < m_start && m_start < end) &&
289 (end <= m_end)) {
290 /* first part */
291 md->num_pages = (m_start - md->phys_addr) >>
292 EFI_PAGE_SHIFT;
293 /* latter part */
294 new += old_memmap->desc_size;
295 memcpy(new, old, old_memmap->desc_size);
296 md = new;
297 md->phys_addr = m_start;
298 md->num_pages = (end - md->phys_addr + 1) >>
299 EFI_PAGE_SHIFT;
300 md->attribute |= m_attr;