xref: /openbmc/linux/drivers/firmware/memmap.c (revision f54dfdf7)
1d9523678SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
269ac9cd6SBernhard Walle /*
369ac9cd6SBernhard Walle  * linux/drivers/firmware/memmap.c
469ac9cd6SBernhard Walle  *  Copyright (C) 2008 SUSE LINUX Products GmbH
597bef7ddSBernhard Walle  *  by Bernhard Walle <bernhard.walle@gmx.de>
669ac9cd6SBernhard Walle  */
769ac9cd6SBernhard Walle 
869ac9cd6SBernhard Walle #include <linux/string.h>
969ac9cd6SBernhard Walle #include <linux/firmware-map.h>
1069ac9cd6SBernhard Walle #include <linux/kernel.h>
1169ac9cd6SBernhard Walle #include <linux/module.h>
1269ac9cd6SBernhard Walle #include <linux/types.h>
1357c8a661SMike Rapoport #include <linux/memblock.h>
145a0e3ad6STejun Heo #include <linux/slab.h>
1546c66c4bSYasuaki Ishimatsu #include <linux/mm.h>
1669ac9cd6SBernhard Walle 
1769ac9cd6SBernhard Walle /*
1869ac9cd6SBernhard Walle  * Data types ------------------------------------------------------------------
1969ac9cd6SBernhard Walle  */
2069ac9cd6SBernhard Walle 
2169ac9cd6SBernhard Walle /*
2269ac9cd6SBernhard Walle  * Firmware map entry. Because firmware memory maps are flat and not
2369ac9cd6SBernhard Walle  * hierarchical, it's ok to organise them in a linked list. No parent
2469ac9cd6SBernhard Walle  * information is necessary as for the resource tree.
2569ac9cd6SBernhard Walle  */
2669ac9cd6SBernhard Walle struct firmware_map_entry {
273b0fde0fSYinghai Lu 	/*
283b0fde0fSYinghai Lu 	 * start and end must be u64 rather than resource_size_t, because e820
293b0fde0fSYinghai Lu 	 * resources can lie at addresses above 4G.
303b0fde0fSYinghai Lu 	 */
313b0fde0fSYinghai Lu 	u64			start;	/* start of the memory range */
323b0fde0fSYinghai Lu 	u64			end;	/* end of the memory range (incl.) */
3369ac9cd6SBernhard Walle 	const char		*type;	/* type of the memory range */
3469ac9cd6SBernhard Walle 	struct list_head	list;	/* entry for the linked list */
3569ac9cd6SBernhard Walle 	struct kobject		kobj;   /* kobject for each entry */
3669ac9cd6SBernhard Walle };
3769ac9cd6SBernhard Walle 
3869ac9cd6SBernhard Walle /*
3969ac9cd6SBernhard Walle  * Forward declarations --------------------------------------------------------
4069ac9cd6SBernhard Walle  */
4169ac9cd6SBernhard Walle static ssize_t memmap_attr_show(struct kobject *kobj,
4269ac9cd6SBernhard Walle 				struct attribute *attr, char *buf);
4369ac9cd6SBernhard Walle static ssize_t start_show(struct firmware_map_entry *entry, char *buf);
4469ac9cd6SBernhard Walle static ssize_t end_show(struct firmware_map_entry *entry, char *buf);
4569ac9cd6SBernhard Walle static ssize_t type_show(struct firmware_map_entry *entry, char *buf);
4669ac9cd6SBernhard Walle 
4746c66c4bSYasuaki Ishimatsu static struct firmware_map_entry * __meminit
4846c66c4bSYasuaki Ishimatsu firmware_map_find_entry(u64 start, u64 end, const char *type);
4946c66c4bSYasuaki Ishimatsu 
5069ac9cd6SBernhard Walle /*
5169ac9cd6SBernhard Walle  * Static data -----------------------------------------------------------------
5269ac9cd6SBernhard Walle  */
5369ac9cd6SBernhard Walle 
5469ac9cd6SBernhard Walle struct memmap_attribute {
5569ac9cd6SBernhard Walle 	struct attribute attr;
5669ac9cd6SBernhard Walle 	ssize_t (*show)(struct firmware_map_entry *entry, char *buf);
5769ac9cd6SBernhard Walle };
5869ac9cd6SBernhard Walle 
59da2bdf9aSRoel Kluin static struct memmap_attribute memmap_start_attr = __ATTR_RO(start);
60da2bdf9aSRoel Kluin static struct memmap_attribute memmap_end_attr   = __ATTR_RO(end);
61da2bdf9aSRoel Kluin static struct memmap_attribute memmap_type_attr  = __ATTR_RO(type);
6269ac9cd6SBernhard Walle 
6369ac9cd6SBernhard Walle /*
6469ac9cd6SBernhard Walle  * These are default attributes that are added for every memmap entry.
6569ac9cd6SBernhard Walle  */
6669ac9cd6SBernhard Walle static struct attribute *def_attrs[] = {
6769ac9cd6SBernhard Walle 	&memmap_start_attr.attr,
6869ac9cd6SBernhard Walle 	&memmap_end_attr.attr,
6969ac9cd6SBernhard Walle 	&memmap_type_attr.attr,
7069ac9cd6SBernhard Walle 	NULL
7169ac9cd6SBernhard Walle };
72*f54dfdf7SGreg Kroah-Hartman ATTRIBUTE_GROUPS(def);
7369ac9cd6SBernhard Walle 
7452cf25d0SEmese Revfy static const struct sysfs_ops memmap_attr_ops = {
7569ac9cd6SBernhard Walle 	.show = memmap_attr_show,
7669ac9cd6SBernhard Walle };
7769ac9cd6SBernhard Walle 
7846c66c4bSYasuaki Ishimatsu /* Firmware memory map entries. */
7946c66c4bSYasuaki Ishimatsu static LIST_HEAD(map_entries);
8046c66c4bSYasuaki Ishimatsu static DEFINE_SPINLOCK(map_entries_lock);
8146c66c4bSYasuaki Ishimatsu 
8246c66c4bSYasuaki Ishimatsu /*
8346c66c4bSYasuaki Ishimatsu  * For memory hotplug, there is no way to free memory map entries allocated
8446c66c4bSYasuaki Ishimatsu  * by boot mem after the system is up. So when we hot-remove memory whose
8546c66c4bSYasuaki Ishimatsu  * map entry is allocated by bootmem, we need to remember the storage and
8646c66c4bSYasuaki Ishimatsu  * reuse it when the memory is hot-added again.
8746c66c4bSYasuaki Ishimatsu  */
8846c66c4bSYasuaki Ishimatsu static LIST_HEAD(map_entries_bootmem);
8946c66c4bSYasuaki Ishimatsu static DEFINE_SPINLOCK(map_entries_bootmem_lock);
9046c66c4bSYasuaki Ishimatsu 
9146c66c4bSYasuaki Ishimatsu 
9246c66c4bSYasuaki Ishimatsu static inline struct firmware_map_entry *
to_memmap_entry(struct kobject * kobj)9346c66c4bSYasuaki Ishimatsu to_memmap_entry(struct kobject *kobj)
9446c66c4bSYasuaki Ishimatsu {
9546c66c4bSYasuaki Ishimatsu 	return container_of(kobj, struct firmware_map_entry, kobj);
9646c66c4bSYasuaki Ishimatsu }
9746c66c4bSYasuaki Ishimatsu 
release_firmware_map_entry(struct kobject * kobj)9846c66c4bSYasuaki Ishimatsu static void __meminit release_firmware_map_entry(struct kobject *kobj)
9946c66c4bSYasuaki Ishimatsu {
10046c66c4bSYasuaki Ishimatsu 	struct firmware_map_entry *entry = to_memmap_entry(kobj);
10146c66c4bSYasuaki Ishimatsu 
10246c66c4bSYasuaki Ishimatsu 	if (PageReserved(virt_to_page(entry))) {
10346c66c4bSYasuaki Ishimatsu 		/*
10446c66c4bSYasuaki Ishimatsu 		 * Remember the storage allocated by bootmem, and reuse it when
10546c66c4bSYasuaki Ishimatsu 		 * the memory is hot-added again. The entry will be added to
10646c66c4bSYasuaki Ishimatsu 		 * map_entries_bootmem here, and deleted from &map_entries in
10746c66c4bSYasuaki Ishimatsu 		 * firmware_map_remove_entry().
10846c66c4bSYasuaki Ishimatsu 		 */
10946c66c4bSYasuaki Ishimatsu 		spin_lock(&map_entries_bootmem_lock);
11046c66c4bSYasuaki Ishimatsu 		list_add(&entry->list, &map_entries_bootmem);
11146c66c4bSYasuaki Ishimatsu 		spin_unlock(&map_entries_bootmem_lock);
11246c66c4bSYasuaki Ishimatsu 
11346c66c4bSYasuaki Ishimatsu 		return;
11446c66c4bSYasuaki Ishimatsu 	}
11546c66c4bSYasuaki Ishimatsu 
11646c66c4bSYasuaki Ishimatsu 	kfree(entry);
11746c66c4bSYasuaki Ishimatsu }
11846c66c4bSYasuaki Ishimatsu 
11946c66c4bSYasuaki Ishimatsu static struct kobj_type __refdata memmap_ktype = {
12046c66c4bSYasuaki Ishimatsu 	.release	= release_firmware_map_entry,
12169ac9cd6SBernhard Walle 	.sysfs_ops	= &memmap_attr_ops,
122*f54dfdf7SGreg Kroah-Hartman 	.default_groups	= def_groups,
12369ac9cd6SBernhard Walle };
12469ac9cd6SBernhard Walle 
12569ac9cd6SBernhard Walle /*
12669ac9cd6SBernhard Walle  * Registration functions ------------------------------------------------------
12769ac9cd6SBernhard Walle  */
12869ac9cd6SBernhard Walle 
12969ac9cd6SBernhard Walle /**
13031bad924SBernhard Walle  * firmware_map_add_entry() - Does the real work to add a firmware memmap entry.
13169ac9cd6SBernhard Walle  * @start: Start of the memory range.
1324ed940d4SYasuaki Ishimatsu  * @end:   End of the memory range (exclusive).
13369ac9cd6SBernhard Walle  * @type:  Type of the memory range.
13469ac9cd6SBernhard Walle  * @entry: Pre-allocated (either kmalloc() or bootmem allocator), uninitialised
13569ac9cd6SBernhard Walle  *         entry.
13631bad924SBernhard Walle  *
13731bad924SBernhard Walle  * Common implementation of firmware_map_add() and firmware_map_add_early()
13831bad924SBernhard Walle  * which expects a pre-allocated struct firmware_map_entry.
139cbdc2810SMichal Simek  *
140cbdc2810SMichal Simek  * Return: 0 always
141cbdc2810SMichal Simek  */
firmware_map_add_entry(u64 start,u64 end,const char * type,struct firmware_map_entry * entry)1423b0fde0fSYinghai Lu static int firmware_map_add_entry(u64 start, u64 end,
14369ac9cd6SBernhard Walle 				  const char *type,
14469ac9cd6SBernhard Walle 				  struct firmware_map_entry *entry)
14569ac9cd6SBernhard Walle {
14669ac9cd6SBernhard Walle 	BUG_ON(start > end);
14769ac9cd6SBernhard Walle 
14869ac9cd6SBernhard Walle 	entry->start = start;
1494ed940d4SYasuaki Ishimatsu 	entry->end = end - 1;
15069ac9cd6SBernhard Walle 	entry->type = type;
15169ac9cd6SBernhard Walle 	INIT_LIST_HEAD(&entry->list);
15269ac9cd6SBernhard Walle 	kobject_init(&entry->kobj, &memmap_ktype);
15369ac9cd6SBernhard Walle 
15446c66c4bSYasuaki Ishimatsu 	spin_lock(&map_entries_lock);
15569ac9cd6SBernhard Walle 	list_add_tail(&entry->list, &map_entries);
15646c66c4bSYasuaki Ishimatsu 	spin_unlock(&map_entries_lock);
15769ac9cd6SBernhard Walle 
15869ac9cd6SBernhard Walle 	return 0;
15969ac9cd6SBernhard Walle }
16069ac9cd6SBernhard Walle 
16146c66c4bSYasuaki Ishimatsu /**
16246c66c4bSYasuaki Ishimatsu  * firmware_map_remove_entry() - Does the real work to remove a firmware
16346c66c4bSYasuaki Ishimatsu  * memmap entry.
16446c66c4bSYasuaki Ishimatsu  * @entry: removed entry.
16546c66c4bSYasuaki Ishimatsu  *
16646c66c4bSYasuaki Ishimatsu  * The caller must hold map_entries_lock, and release it properly.
167cbdc2810SMichal Simek  */
firmware_map_remove_entry(struct firmware_map_entry * entry)16846c66c4bSYasuaki Ishimatsu static inline void firmware_map_remove_entry(struct firmware_map_entry *entry)
16946c66c4bSYasuaki Ishimatsu {
17046c66c4bSYasuaki Ishimatsu 	list_del(&entry->list);
17146c66c4bSYasuaki Ishimatsu }
17246c66c4bSYasuaki Ishimatsu 
173d96ae530Sakpm@linux-foundation.org /*
174d96ae530Sakpm@linux-foundation.org  * Add memmap entry on sysfs
175d96ae530Sakpm@linux-foundation.org  */
add_sysfs_fw_map_entry(struct firmware_map_entry * entry)176d96ae530Sakpm@linux-foundation.org static int add_sysfs_fw_map_entry(struct firmware_map_entry *entry)
177d96ae530Sakpm@linux-foundation.org {
178d96ae530Sakpm@linux-foundation.org 	static int map_entries_nr;
179d96ae530Sakpm@linux-foundation.org 	static struct kset *mmap_kset;
180d96ae530Sakpm@linux-foundation.org 
18122880ebeSYasuaki Ishimatsu 	if (entry->kobj.state_in_sysfs)
18222880ebeSYasuaki Ishimatsu 		return -EEXIST;
18322880ebeSYasuaki Ishimatsu 
184d96ae530Sakpm@linux-foundation.org 	if (!mmap_kset) {
185d96ae530Sakpm@linux-foundation.org 		mmap_kset = kset_create_and_add("memmap", NULL, firmware_kobj);
186d96ae530Sakpm@linux-foundation.org 		if (!mmap_kset)
187d96ae530Sakpm@linux-foundation.org 			return -ENOMEM;
188d96ae530Sakpm@linux-foundation.org 	}
189d96ae530Sakpm@linux-foundation.org 
190d96ae530Sakpm@linux-foundation.org 	entry->kobj.kset = mmap_kset;
191d96ae530Sakpm@linux-foundation.org 	if (kobject_add(&entry->kobj, NULL, "%d", map_entries_nr++))
192d96ae530Sakpm@linux-foundation.org 		kobject_put(&entry->kobj);
193d96ae530Sakpm@linux-foundation.org 
194d96ae530Sakpm@linux-foundation.org 	return 0;
195d96ae530Sakpm@linux-foundation.org }
196d96ae530Sakpm@linux-foundation.org 
19746c66c4bSYasuaki Ishimatsu /*
19846c66c4bSYasuaki Ishimatsu  * Remove memmap entry on sysfs
19946c66c4bSYasuaki Ishimatsu  */
remove_sysfs_fw_map_entry(struct firmware_map_entry * entry)20046c66c4bSYasuaki Ishimatsu static inline void remove_sysfs_fw_map_entry(struct firmware_map_entry *entry)
20146c66c4bSYasuaki Ishimatsu {
20246c66c4bSYasuaki Ishimatsu 	kobject_put(&entry->kobj);
20346c66c4bSYasuaki Ishimatsu }
20446c66c4bSYasuaki Ishimatsu 
205cbdc2810SMichal Simek /**
20646c66c4bSYasuaki Ishimatsu  * firmware_map_find_entry_in_list() - Search memmap entry in a given list.
20746c66c4bSYasuaki Ishimatsu  * @start: Start of the memory range.
20846c66c4bSYasuaki Ishimatsu  * @end:   End of the memory range (exclusive).
20946c66c4bSYasuaki Ishimatsu  * @type:  Type of the memory range.
21046c66c4bSYasuaki Ishimatsu  * @list:  In which to find the entry.
21146c66c4bSYasuaki Ishimatsu  *
21246c66c4bSYasuaki Ishimatsu  * This function is to find the memmap entey of a given memory range in a
21346c66c4bSYasuaki Ishimatsu  * given list. The caller must hold map_entries_lock, and must not release
21446c66c4bSYasuaki Ishimatsu  * the lock until the processing of the returned entry has completed.
21546c66c4bSYasuaki Ishimatsu  *
21646c66c4bSYasuaki Ishimatsu  * Return: Pointer to the entry to be found on success, or NULL on failure.
21746c66c4bSYasuaki Ishimatsu  */
21846c66c4bSYasuaki Ishimatsu static struct firmware_map_entry * __meminit
firmware_map_find_entry_in_list(u64 start,u64 end,const char * type,struct list_head * list)21946c66c4bSYasuaki Ishimatsu firmware_map_find_entry_in_list(u64 start, u64 end, const char *type,
22046c66c4bSYasuaki Ishimatsu 				struct list_head *list)
22146c66c4bSYasuaki Ishimatsu {
22246c66c4bSYasuaki Ishimatsu 	struct firmware_map_entry *entry;
22346c66c4bSYasuaki Ishimatsu 
22446c66c4bSYasuaki Ishimatsu 	list_for_each_entry(entry, list, list)
22546c66c4bSYasuaki Ishimatsu 		if ((entry->start == start) && (entry->end == end) &&
22646c66c4bSYasuaki Ishimatsu 		    (!strcmp(entry->type, type))) {
22746c66c4bSYasuaki Ishimatsu 			return entry;
22846c66c4bSYasuaki Ishimatsu 		}
22946c66c4bSYasuaki Ishimatsu 
23046c66c4bSYasuaki Ishimatsu 	return NULL;
23146c66c4bSYasuaki Ishimatsu }
23246c66c4bSYasuaki Ishimatsu 
233cbdc2810SMichal Simek /**
23446c66c4bSYasuaki Ishimatsu  * firmware_map_find_entry() - Search memmap entry in map_entries.
23546c66c4bSYasuaki Ishimatsu  * @start: Start of the memory range.
23646c66c4bSYasuaki Ishimatsu  * @end:   End of the memory range (exclusive).
23746c66c4bSYasuaki Ishimatsu  * @type:  Type of the memory range.
23846c66c4bSYasuaki Ishimatsu  *
23946c66c4bSYasuaki Ishimatsu  * This function is to find the memmap entey of a given memory range.
24046c66c4bSYasuaki Ishimatsu  * The caller must hold map_entries_lock, and must not release the lock
24146c66c4bSYasuaki Ishimatsu  * until the processing of the returned entry has completed.
24246c66c4bSYasuaki Ishimatsu  *
24346c66c4bSYasuaki Ishimatsu  * Return: Pointer to the entry to be found on success, or NULL on failure.
24446c66c4bSYasuaki Ishimatsu  */
24546c66c4bSYasuaki Ishimatsu static struct firmware_map_entry * __meminit
firmware_map_find_entry(u64 start,u64 end,const char * type)24646c66c4bSYasuaki Ishimatsu firmware_map_find_entry(u64 start, u64 end, const char *type)
24746c66c4bSYasuaki Ishimatsu {
24846c66c4bSYasuaki Ishimatsu 	return firmware_map_find_entry_in_list(start, end, type, &map_entries);
24946c66c4bSYasuaki Ishimatsu }
25046c66c4bSYasuaki Ishimatsu 
251cbdc2810SMichal Simek /**
25246c66c4bSYasuaki Ishimatsu  * firmware_map_find_entry_bootmem() - Search memmap entry in map_entries_bootmem.
25346c66c4bSYasuaki Ishimatsu  * @start: Start of the memory range.
25446c66c4bSYasuaki Ishimatsu  * @end:   End of the memory range (exclusive).
25546c66c4bSYasuaki Ishimatsu  * @type:  Type of the memory range.
25646c66c4bSYasuaki Ishimatsu  *
25746c66c4bSYasuaki Ishimatsu  * This function is similar to firmware_map_find_entry except that it find the
25846c66c4bSYasuaki Ishimatsu  * given entry in map_entries_bootmem.
25946c66c4bSYasuaki Ishimatsu  *
26046c66c4bSYasuaki Ishimatsu  * Return: Pointer to the entry to be found on success, or NULL on failure.
26146c66c4bSYasuaki Ishimatsu  */
26246c66c4bSYasuaki Ishimatsu static struct firmware_map_entry * __meminit
firmware_map_find_entry_bootmem(u64 start,u64 end,const char * type)26346c66c4bSYasuaki Ishimatsu firmware_map_find_entry_bootmem(u64 start, u64 end, const char *type)
26446c66c4bSYasuaki Ishimatsu {
26546c66c4bSYasuaki Ishimatsu 	return firmware_map_find_entry_in_list(start, end, type,
26646c66c4bSYasuaki Ishimatsu 					       &map_entries_bootmem);
26746c66c4bSYasuaki Ishimatsu }
26846c66c4bSYasuaki Ishimatsu 
26931bad924SBernhard Walle /**
270d96ae530Sakpm@linux-foundation.org  * firmware_map_add_hotplug() - Adds a firmware mapping entry when we do
271d96ae530Sakpm@linux-foundation.org  * memory hotplug.
27231bad924SBernhard Walle  * @start: Start of the memory range.
2734ed940d4SYasuaki Ishimatsu  * @end:   End of the memory range (exclusive)
27431bad924SBernhard Walle  * @type:  Type of the memory range.
27531bad924SBernhard Walle  *
276d96ae530Sakpm@linux-foundation.org  * Adds a firmware mapping entry. This function is for memory hotplug, it is
277d96ae530Sakpm@linux-foundation.org  * similar to function firmware_map_add_early(). The only difference is that
278d96ae530Sakpm@linux-foundation.org  * it will create the syfs entry dynamically.
27931bad924SBernhard Walle  *
280cbdc2810SMichal Simek  * Return: 0 on success, or -ENOMEM if no memory could be allocated.
281cbdc2810SMichal Simek  */
firmware_map_add_hotplug(u64 start,u64 end,const char * type)282d96ae530Sakpm@linux-foundation.org int __meminit firmware_map_add_hotplug(u64 start, u64 end, const char *type)
28369ac9cd6SBernhard Walle {
28469ac9cd6SBernhard Walle 	struct firmware_map_entry *entry;
28569ac9cd6SBernhard Walle 
286f0093edeSYasuaki Ishimatsu 	entry = firmware_map_find_entry(start, end - 1, type);
287f0093edeSYasuaki Ishimatsu 	if (entry)
288f0093edeSYasuaki Ishimatsu 		return 0;
289f0093edeSYasuaki Ishimatsu 
29049c8b24dSYasuaki Ishimatsu 	entry = firmware_map_find_entry_bootmem(start, end - 1, type);
29146c66c4bSYasuaki Ishimatsu 	if (!entry) {
292d96ae530Sakpm@linux-foundation.org 		entry = kzalloc(sizeof(struct firmware_map_entry), GFP_ATOMIC);
29369ac9cd6SBernhard Walle 		if (!entry)
29469ac9cd6SBernhard Walle 			return -ENOMEM;
29546c66c4bSYasuaki Ishimatsu 	} else {
29646c66c4bSYasuaki Ishimatsu 		/* Reuse storage allocated by bootmem. */
29746c66c4bSYasuaki Ishimatsu 		spin_lock(&map_entries_bootmem_lock);
29846c66c4bSYasuaki Ishimatsu 		list_del(&entry->list);
29946c66c4bSYasuaki Ishimatsu 		spin_unlock(&map_entries_bootmem_lock);
30046c66c4bSYasuaki Ishimatsu 
30146c66c4bSYasuaki Ishimatsu 		memset(entry, 0, sizeof(*entry));
30246c66c4bSYasuaki Ishimatsu 	}
30369ac9cd6SBernhard Walle 
304d96ae530Sakpm@linux-foundation.org 	firmware_map_add_entry(start, end, type, entry);
305d96ae530Sakpm@linux-foundation.org 	/* create the memmap entry */
306d96ae530Sakpm@linux-foundation.org 	add_sysfs_fw_map_entry(entry);
307d96ae530Sakpm@linux-foundation.org 
308d96ae530Sakpm@linux-foundation.org 	return 0;
30969ac9cd6SBernhard Walle }
31069ac9cd6SBernhard Walle 
31131bad924SBernhard Walle /**
31231bad924SBernhard Walle  * firmware_map_add_early() - Adds a firmware mapping entry.
31331bad924SBernhard Walle  * @start: Start of the memory range.
3144ed940d4SYasuaki Ishimatsu  * @end:   End of the memory range.
31531bad924SBernhard Walle  * @type:  Type of the memory range.
31631bad924SBernhard Walle  *
31731bad924SBernhard Walle  * Adds a firmware mapping entry. This function uses the bootmem allocator
318d96ae530Sakpm@linux-foundation.org  * for memory allocation.
31931bad924SBernhard Walle  *
32031bad924SBernhard Walle  * That function must be called before late_initcall.
32131bad924SBernhard Walle  *
322cbdc2810SMichal Simek  * Return: 0 on success, or -ENOMEM if no memory could be allocated.
323cbdc2810SMichal Simek  */
firmware_map_add_early(u64 start,u64 end,const char * type)3243b0fde0fSYinghai Lu int __init firmware_map_add_early(u64 start, u64 end, const char *type)
32569ac9cd6SBernhard Walle {
32669ac9cd6SBernhard Walle 	struct firmware_map_entry *entry;
32769ac9cd6SBernhard Walle 
32826fb3daeSMike Rapoport 	entry = memblock_alloc(sizeof(struct firmware_map_entry),
3297e1c4e27SMike Rapoport 			       SMP_CACHE_BYTES);
33031bad924SBernhard Walle 	if (WARN_ON(!entry))
33169ac9cd6SBernhard Walle 		return -ENOMEM;
33269ac9cd6SBernhard Walle 
33369ac9cd6SBernhard Walle 	return firmware_map_add_entry(start, end, type, entry);
33469ac9cd6SBernhard Walle }
33569ac9cd6SBernhard Walle 
33646c66c4bSYasuaki Ishimatsu /**
33746c66c4bSYasuaki Ishimatsu  * firmware_map_remove() - remove a firmware mapping entry
33846c66c4bSYasuaki Ishimatsu  * @start: Start of the memory range.
33946c66c4bSYasuaki Ishimatsu  * @end:   End of the memory range.
34046c66c4bSYasuaki Ishimatsu  * @type:  Type of the memory range.
34146c66c4bSYasuaki Ishimatsu  *
34246c66c4bSYasuaki Ishimatsu  * removes a firmware mapping entry.
34346c66c4bSYasuaki Ishimatsu  *
344cbdc2810SMichal Simek  * Return: 0 on success, or -EINVAL if no entry.
345cbdc2810SMichal Simek  */
firmware_map_remove(u64 start,u64 end,const char * type)34646c66c4bSYasuaki Ishimatsu int __meminit firmware_map_remove(u64 start, u64 end, const char *type)
34746c66c4bSYasuaki Ishimatsu {
34846c66c4bSYasuaki Ishimatsu 	struct firmware_map_entry *entry;
34946c66c4bSYasuaki Ishimatsu 
35046c66c4bSYasuaki Ishimatsu 	spin_lock(&map_entries_lock);
35146c66c4bSYasuaki Ishimatsu 	entry = firmware_map_find_entry(start, end - 1, type);
35246c66c4bSYasuaki Ishimatsu 	if (!entry) {
35346c66c4bSYasuaki Ishimatsu 		spin_unlock(&map_entries_lock);
35446c66c4bSYasuaki Ishimatsu 		return -EINVAL;
35546c66c4bSYasuaki Ishimatsu 	}
35646c66c4bSYasuaki Ishimatsu 
35746c66c4bSYasuaki Ishimatsu 	firmware_map_remove_entry(entry);
35846c66c4bSYasuaki Ishimatsu 	spin_unlock(&map_entries_lock);
35946c66c4bSYasuaki Ishimatsu 
36046c66c4bSYasuaki Ishimatsu 	/* remove the memmap entry */
36146c66c4bSYasuaki Ishimatsu 	remove_sysfs_fw_map_entry(entry);
36246c66c4bSYasuaki Ishimatsu 
36346c66c4bSYasuaki Ishimatsu 	return 0;
36446c66c4bSYasuaki Ishimatsu }
36546c66c4bSYasuaki Ishimatsu 
36669ac9cd6SBernhard Walle /*
36769ac9cd6SBernhard Walle  * Sysfs functions -------------------------------------------------------------
36869ac9cd6SBernhard Walle  */
36969ac9cd6SBernhard Walle 
start_show(struct firmware_map_entry * entry,char * buf)37069ac9cd6SBernhard Walle static ssize_t start_show(struct firmware_map_entry *entry, char *buf)
37169ac9cd6SBernhard Walle {
37278681ac0SRandy Dunlap 	return snprintf(buf, PAGE_SIZE, "0x%llx\n",
37378681ac0SRandy Dunlap 		(unsigned long long)entry->start);
37469ac9cd6SBernhard Walle }
37569ac9cd6SBernhard Walle 
end_show(struct firmware_map_entry * entry,char * buf)37669ac9cd6SBernhard Walle static ssize_t end_show(struct firmware_map_entry *entry, char *buf)
37769ac9cd6SBernhard Walle {
37878681ac0SRandy Dunlap 	return snprintf(buf, PAGE_SIZE, "0x%llx\n",
37978681ac0SRandy Dunlap 		(unsigned long long)entry->end);
38069ac9cd6SBernhard Walle }
38169ac9cd6SBernhard Walle 
type_show(struct firmware_map_entry * entry,char * buf)38269ac9cd6SBernhard Walle static ssize_t type_show(struct firmware_map_entry *entry, char *buf)
38369ac9cd6SBernhard Walle {
38469ac9cd6SBernhard Walle 	return snprintf(buf, PAGE_SIZE, "%s\n", entry->type);
38569ac9cd6SBernhard Walle }
38669ac9cd6SBernhard Walle 
to_memmap_attr(struct attribute * attr)38746c66c4bSYasuaki Ishimatsu static inline struct memmap_attribute *to_memmap_attr(struct attribute *attr)
38846c66c4bSYasuaki Ishimatsu {
38946c66c4bSYasuaki Ishimatsu 	return container_of(attr, struct memmap_attribute, attr);
39046c66c4bSYasuaki Ishimatsu }
39169ac9cd6SBernhard Walle 
memmap_attr_show(struct kobject * kobj,struct attribute * attr,char * buf)39269ac9cd6SBernhard Walle static ssize_t memmap_attr_show(struct kobject *kobj,
39369ac9cd6SBernhard Walle 				struct attribute *attr, char *buf)
39469ac9cd6SBernhard Walle {
39569ac9cd6SBernhard Walle 	struct firmware_map_entry *entry = to_memmap_entry(kobj);
39669ac9cd6SBernhard Walle 	struct memmap_attribute *memmap_attr = to_memmap_attr(attr);
39769ac9cd6SBernhard Walle 
39869ac9cd6SBernhard Walle 	return memmap_attr->show(entry, buf);
39969ac9cd6SBernhard Walle }
40069ac9cd6SBernhard Walle 
40169ac9cd6SBernhard Walle /*
40269ac9cd6SBernhard Walle  * Initialises stuff and adds the entries in the map_entries list to
40369ac9cd6SBernhard Walle  * sysfs. Important is that firmware_map_add() and firmware_map_add_early()
40431bad924SBernhard Walle  * must be called before late_initcall. That's just because that function
40531bad924SBernhard Walle  * is called as late_initcall() function, which means that if you call
40631bad924SBernhard Walle  * firmware_map_add() or firmware_map_add_early() afterwards, the entries
40731bad924SBernhard Walle  * are not added to sysfs.
40869ac9cd6SBernhard Walle  */
firmware_memmap_init(void)409bac71696SFengguang Wu static int __init firmware_memmap_init(void)
41069ac9cd6SBernhard Walle {
41169ac9cd6SBernhard Walle 	struct firmware_map_entry *entry;
41269ac9cd6SBernhard Walle 
413d96ae530Sakpm@linux-foundation.org 	list_for_each_entry(entry, &map_entries, list)
414d96ae530Sakpm@linux-foundation.org 		add_sysfs_fw_map_entry(entry);
41569ac9cd6SBernhard Walle 
41669ac9cd6SBernhard Walle 	return 0;
41769ac9cd6SBernhard Walle }
418bac71696SFengguang Wu late_initcall(firmware_memmap_init);
41969ac9cd6SBernhard Walle 
420