Linux 4.19.133
[linux/fpc-iii.git] / drivers / firmware / memmap.c
blob5de3ed29282c4aadfc164acb91384744686690ad
1 /*
2 * linux/drivers/firmware/memmap.c
3 * Copyright (C) 2008 SUSE LINUX Products GmbH
4 * by Bernhard Walle <bernhard.walle@gmx.de>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License v2.0 as published by
8 * the Free Software Foundation
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
17 #include <linux/string.h>
18 #include <linux/firmware-map.h>
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/types.h>
22 #include <linux/bootmem.h>
23 #include <linux/slab.h>
24 #include <linux/mm.h>
27 * Data types ------------------------------------------------------------------
31 * Firmware map entry. Because firmware memory maps are flat and not
32 * hierarchical, it's ok to organise them in a linked list. No parent
33 * information is necessary as for the resource tree.
35 struct firmware_map_entry {
37 * start and end must be u64 rather than resource_size_t, because e820
38 * resources can lie at addresses above 4G.
40 u64 start; /* start of the memory range */
41 u64 end; /* end of the memory range (incl.) */
42 const char *type; /* type of the memory range */
43 struct list_head list; /* entry for the linked list */
44 struct kobject kobj; /* kobject for each entry */
48 * Forward declarations --------------------------------------------------------
50 static ssize_t memmap_attr_show(struct kobject *kobj,
51 struct attribute *attr, char *buf);
52 static ssize_t start_show(struct firmware_map_entry *entry, char *buf);
53 static ssize_t end_show(struct firmware_map_entry *entry, char *buf);
54 static ssize_t type_show(struct firmware_map_entry *entry, char *buf);
56 static struct firmware_map_entry * __meminit
57 firmware_map_find_entry(u64 start, u64 end, const char *type);
60 * Static data -----------------------------------------------------------------
63 struct memmap_attribute {
64 struct attribute attr;
65 ssize_t (*show)(struct firmware_map_entry *entry, char *buf);
68 static struct memmap_attribute memmap_start_attr = __ATTR_RO(start);
69 static struct memmap_attribute memmap_end_attr = __ATTR_RO(end);
70 static struct memmap_attribute memmap_type_attr = __ATTR_RO(type);
73 * These are default attributes that are added for every memmap entry.
75 static struct attribute *def_attrs[] = {
76 &memmap_start_attr.attr,
77 &memmap_end_attr.attr,
78 &memmap_type_attr.attr,
79 NULL
82 static const struct sysfs_ops memmap_attr_ops = {
83 .show = memmap_attr_show,
86 /* Firmware memory map entries. */
87 static LIST_HEAD(map_entries);
88 static DEFINE_SPINLOCK(map_entries_lock);
91 * For memory hotplug, there is no way to free memory map entries allocated
92 * by boot mem after the system is up. So when we hot-remove memory whose
93 * map entry is allocated by bootmem, we need to remember the storage and
94 * reuse it when the memory is hot-added again.
96 static LIST_HEAD(map_entries_bootmem);
97 static DEFINE_SPINLOCK(map_entries_bootmem_lock);
100 static inline struct firmware_map_entry *
101 to_memmap_entry(struct kobject *kobj)
103 return container_of(kobj, struct firmware_map_entry, kobj);
106 static void __meminit release_firmware_map_entry(struct kobject *kobj)
108 struct firmware_map_entry *entry = to_memmap_entry(kobj);
110 if (PageReserved(virt_to_page(entry))) {
112 * Remember the storage allocated by bootmem, and reuse it when
113 * the memory is hot-added again. The entry will be added to
114 * map_entries_bootmem here, and deleted from &map_entries in
115 * firmware_map_remove_entry().
117 spin_lock(&map_entries_bootmem_lock);
118 list_add(&entry->list, &map_entries_bootmem);
119 spin_unlock(&map_entries_bootmem_lock);
121 return;
124 kfree(entry);
127 static struct kobj_type __refdata memmap_ktype = {
128 .release = release_firmware_map_entry,
129 .sysfs_ops = &memmap_attr_ops,
130 .default_attrs = def_attrs,
134 * Registration functions ------------------------------------------------------
138 * firmware_map_add_entry() - Does the real work to add a firmware memmap entry.
139 * @start: Start of the memory range.
140 * @end: End of the memory range (exclusive).
141 * @type: Type of the memory range.
142 * @entry: Pre-allocated (either kmalloc() or bootmem allocator), uninitialised
143 * entry.
145 * Common implementation of firmware_map_add() and firmware_map_add_early()
146 * which expects a pre-allocated struct firmware_map_entry.
148 * Return: 0 always
150 static int firmware_map_add_entry(u64 start, u64 end,
151 const char *type,
152 struct firmware_map_entry *entry)
154 BUG_ON(start > end);
156 entry->start = start;
157 entry->end = end - 1;
158 entry->type = type;
159 INIT_LIST_HEAD(&entry->list);
160 kobject_init(&entry->kobj, &memmap_ktype);
162 spin_lock(&map_entries_lock);
163 list_add_tail(&entry->list, &map_entries);
164 spin_unlock(&map_entries_lock);
166 return 0;
170 * firmware_map_remove_entry() - Does the real work to remove a firmware
171 * memmap entry.
172 * @entry: removed entry.
174 * The caller must hold map_entries_lock, and release it properly.
176 static inline void firmware_map_remove_entry(struct firmware_map_entry *entry)
178 list_del(&entry->list);
182 * Add memmap entry on sysfs
184 static int add_sysfs_fw_map_entry(struct firmware_map_entry *entry)
186 static int map_entries_nr;
187 static struct kset *mmap_kset;
189 if (entry->kobj.state_in_sysfs)
190 return -EEXIST;
192 if (!mmap_kset) {
193 mmap_kset = kset_create_and_add("memmap", NULL, firmware_kobj);
194 if (!mmap_kset)
195 return -ENOMEM;
198 entry->kobj.kset = mmap_kset;
199 if (kobject_add(&entry->kobj, NULL, "%d", map_entries_nr++))
200 kobject_put(&entry->kobj);
202 return 0;
206 * Remove memmap entry on sysfs
208 static inline void remove_sysfs_fw_map_entry(struct firmware_map_entry *entry)
210 kobject_put(&entry->kobj);
214 * firmware_map_find_entry_in_list() - Search memmap entry in a given list.
215 * @start: Start of the memory range.
216 * @end: End of the memory range (exclusive).
217 * @type: Type of the memory range.
218 * @list: In which to find the entry.
220 * This function is to find the memmap entey of a given memory range in a
221 * given list. The caller must hold map_entries_lock, and must not release
222 * the lock until the processing of the returned entry has completed.
224 * Return: Pointer to the entry to be found on success, or NULL on failure.
226 static struct firmware_map_entry * __meminit
227 firmware_map_find_entry_in_list(u64 start, u64 end, const char *type,
228 struct list_head *list)
230 struct firmware_map_entry *entry;
232 list_for_each_entry(entry, list, list)
233 if ((entry->start == start) && (entry->end == end) &&
234 (!strcmp(entry->type, type))) {
235 return entry;
238 return NULL;
242 * firmware_map_find_entry() - Search memmap entry in map_entries.
243 * @start: Start of the memory range.
244 * @end: End of the memory range (exclusive).
245 * @type: Type of the memory range.
247 * This function is to find the memmap entey of a given memory range.
248 * The caller must hold map_entries_lock, and must not release the lock
249 * until the processing of the returned entry has completed.
251 * Return: Pointer to the entry to be found on success, or NULL on failure.
253 static struct firmware_map_entry * __meminit
254 firmware_map_find_entry(u64 start, u64 end, const char *type)
256 return firmware_map_find_entry_in_list(start, end, type, &map_entries);
260 * firmware_map_find_entry_bootmem() - Search memmap entry in map_entries_bootmem.
261 * @start: Start of the memory range.
262 * @end: End of the memory range (exclusive).
263 * @type: Type of the memory range.
265 * This function is similar to firmware_map_find_entry except that it find the
266 * given entry in map_entries_bootmem.
268 * Return: Pointer to the entry to be found on success, or NULL on failure.
270 static struct firmware_map_entry * __meminit
271 firmware_map_find_entry_bootmem(u64 start, u64 end, const char *type)
273 return firmware_map_find_entry_in_list(start, end, type,
274 &map_entries_bootmem);
278 * firmware_map_add_hotplug() - Adds a firmware mapping entry when we do
279 * memory hotplug.
280 * @start: Start of the memory range.
281 * @end: End of the memory range (exclusive)
282 * @type: Type of the memory range.
284 * Adds a firmware mapping entry. This function is for memory hotplug, it is
285 * similar to function firmware_map_add_early(). The only difference is that
286 * it will create the syfs entry dynamically.
288 * Return: 0 on success, or -ENOMEM if no memory could be allocated.
290 int __meminit firmware_map_add_hotplug(u64 start, u64 end, const char *type)
292 struct firmware_map_entry *entry;
294 entry = firmware_map_find_entry(start, end - 1, type);
295 if (entry)
296 return 0;
298 entry = firmware_map_find_entry_bootmem(start, end - 1, type);
299 if (!entry) {
300 entry = kzalloc(sizeof(struct firmware_map_entry), GFP_ATOMIC);
301 if (!entry)
302 return -ENOMEM;
303 } else {
304 /* Reuse storage allocated by bootmem. */
305 spin_lock(&map_entries_bootmem_lock);
306 list_del(&entry->list);
307 spin_unlock(&map_entries_bootmem_lock);
309 memset(entry, 0, sizeof(*entry));
312 firmware_map_add_entry(start, end, type, entry);
313 /* create the memmap entry */
314 add_sysfs_fw_map_entry(entry);
316 return 0;
320 * firmware_map_add_early() - Adds a firmware mapping entry.
321 * @start: Start of the memory range.
322 * @end: End of the memory range.
323 * @type: Type of the memory range.
325 * Adds a firmware mapping entry. This function uses the bootmem allocator
326 * for memory allocation.
328 * That function must be called before late_initcall.
330 * Return: 0 on success, or -ENOMEM if no memory could be allocated.
332 int __init firmware_map_add_early(u64 start, u64 end, const char *type)
334 struct firmware_map_entry *entry;
336 entry = memblock_virt_alloc(sizeof(struct firmware_map_entry), 0);
337 if (WARN_ON(!entry))
338 return -ENOMEM;
340 return firmware_map_add_entry(start, end, type, entry);
344 * firmware_map_remove() - remove a firmware mapping entry
345 * @start: Start of the memory range.
346 * @end: End of the memory range.
347 * @type: Type of the memory range.
349 * removes a firmware mapping entry.
351 * Return: 0 on success, or -EINVAL if no entry.
353 int __meminit firmware_map_remove(u64 start, u64 end, const char *type)
355 struct firmware_map_entry *entry;
357 spin_lock(&map_entries_lock);
358 entry = firmware_map_find_entry(start, end - 1, type);
359 if (!entry) {
360 spin_unlock(&map_entries_lock);
361 return -EINVAL;
364 firmware_map_remove_entry(entry);
365 spin_unlock(&map_entries_lock);
367 /* remove the memmap entry */
368 remove_sysfs_fw_map_entry(entry);
370 return 0;
374 * Sysfs functions -------------------------------------------------------------
377 static ssize_t start_show(struct firmware_map_entry *entry, char *buf)
379 return snprintf(buf, PAGE_SIZE, "0x%llx\n",
380 (unsigned long long)entry->start);
383 static ssize_t end_show(struct firmware_map_entry *entry, char *buf)
385 return snprintf(buf, PAGE_SIZE, "0x%llx\n",
386 (unsigned long long)entry->end);
389 static ssize_t type_show(struct firmware_map_entry *entry, char *buf)
391 return snprintf(buf, PAGE_SIZE, "%s\n", entry->type);
394 static inline struct memmap_attribute *to_memmap_attr(struct attribute *attr)
396 return container_of(attr, struct memmap_attribute, attr);
399 static ssize_t memmap_attr_show(struct kobject *kobj,
400 struct attribute *attr, char *buf)
402 struct firmware_map_entry *entry = to_memmap_entry(kobj);
403 struct memmap_attribute *memmap_attr = to_memmap_attr(attr);
405 return memmap_attr->show(entry, buf);
409 * Initialises stuff and adds the entries in the map_entries list to
410 * sysfs. Important is that firmware_map_add() and firmware_map_add_early()
411 * must be called before late_initcall. That's just because that function
412 * is called as late_initcall() function, which means that if you call
413 * firmware_map_add() or firmware_map_add_early() afterwards, the entries
414 * are not added to sysfs.
416 static int __init firmware_memmap_init(void)
418 struct firmware_map_entry *entry;
420 list_for_each_entry(entry, &map_entries, list)
421 add_sysfs_fw_map_entry(entry);
423 return 0;
425 late_initcall(firmware_memmap_init);