// SPDX-License-Identifier: GPL-2.0-only1/*2* linux/drivers/firmware/memmap.c3* Copyright (C) 2008 SUSE LINUX Products GmbH4* by Bernhard Walle <[email protected]>5*/67#include <linux/string.h>8#include <linux/firmware-map.h>9#include <linux/kernel.h>10#include <linux/module.h>11#include <linux/types.h>12#include <linux/memblock.h>13#include <linux/slab.h>14#include <linux/mm.h>1516/*17* Data types ------------------------------------------------------------------18*/1920/*21* Firmware map entry. Because firmware memory maps are flat and not22* hierarchical, it's ok to organise them in a linked list. No parent23* information is necessary as for the resource tree.24*/25struct firmware_map_entry {26/*27* start and end must be u64 rather than resource_size_t, because e82028* resources can lie at addresses above 4G.29*/30u64 start; /* start of the memory range */31u64 end; /* end of the memory range (incl.) */32const char *type; /* type of the memory range */33struct list_head list; /* entry for the linked list */34struct kobject kobj; /* kobject for each entry */35};3637/*38* Forward declarations --------------------------------------------------------39*/40static ssize_t memmap_attr_show(struct kobject *kobj,41struct attribute *attr, char *buf);42static ssize_t start_show(struct firmware_map_entry *entry, char *buf);43static ssize_t end_show(struct firmware_map_entry *entry, char *buf);44static ssize_t type_show(struct firmware_map_entry *entry, char *buf);4546static struct firmware_map_entry * __meminit47firmware_map_find_entry(u64 start, u64 end, const char *type);4849/*50* Static data -----------------------------------------------------------------51*/5253struct memmap_attribute {54struct attribute attr;55ssize_t (*show)(struct firmware_map_entry *entry, char *buf);56};5758static struct memmap_attribute memmap_start_attr = __ATTR_RO(start);59static struct memmap_attribute memmap_end_attr = __ATTR_RO(end);60static struct memmap_attribute memmap_type_attr = __ATTR_RO(type);6162/*63* These are default attributes that are added for every memmap entry.64*/65static struct attribute *def_attrs[] = {66&memmap_start_attr.attr,67&memmap_end_attr.attr,68&memmap_type_attr.attr,69NULL70};71ATTRIBUTE_GROUPS(def);7273static const struct sysfs_ops memmap_attr_ops = {74.show = memmap_attr_show,75};7677/* Firmware memory map entries. */78static LIST_HEAD(map_entries);79static DEFINE_SPINLOCK(map_entries_lock);8081/*82* For memory hotplug, there is no way to free memory map entries allocated83* by boot mem after the system is up. So when we hot-remove memory whose84* map entry is allocated by bootmem, we need to remember the storage and85* reuse it when the memory is hot-added again.86*/87static LIST_HEAD(map_entries_bootmem);88static DEFINE_SPINLOCK(map_entries_bootmem_lock);899091static inline struct firmware_map_entry *92to_memmap_entry(struct kobject *kobj)93{94return container_of(kobj, struct firmware_map_entry, kobj);95}9697static void __meminit release_firmware_map_entry(struct kobject *kobj)98{99struct firmware_map_entry *entry = to_memmap_entry(kobj);100101if (PageReserved(virt_to_page(entry))) {102/*103* Remember the storage allocated by bootmem, and reuse it when104* the memory is hot-added again. The entry will be added to105* map_entries_bootmem here, and deleted from &map_entries in106* firmware_map_remove_entry().107*/108spin_lock(&map_entries_bootmem_lock);109list_add(&entry->list, &map_entries_bootmem);110spin_unlock(&map_entries_bootmem_lock);111112return;113}114115kfree(entry);116}117118static const struct kobj_type memmap_ktype = {119.release = release_firmware_map_entry,120.sysfs_ops = &memmap_attr_ops,121.default_groups = def_groups,122};123124/*125* Registration functions ------------------------------------------------------126*/127128/**129* firmware_map_add_entry() - Does the real work to add a firmware memmap entry.130* @start: Start of the memory range.131* @end: End of the memory range (exclusive).132* @type: Type of the memory range.133* @entry: Pre-allocated (either kmalloc() or bootmem allocator), uninitialised134* entry.135*136* Common implementation of firmware_map_add() and firmware_map_add_early()137* which expects a pre-allocated struct firmware_map_entry.138*139* Return: 0 always140*/141static int firmware_map_add_entry(u64 start, u64 end,142const char *type,143struct firmware_map_entry *entry)144{145BUG_ON(start > end);146147entry->start = start;148entry->end = end - 1;149entry->type = type;150INIT_LIST_HEAD(&entry->list);151kobject_init(&entry->kobj, &memmap_ktype);152153spin_lock(&map_entries_lock);154list_add_tail(&entry->list, &map_entries);155spin_unlock(&map_entries_lock);156157return 0;158}159160/**161* firmware_map_remove_entry() - Does the real work to remove a firmware162* memmap entry.163* @entry: removed entry.164*165* The caller must hold map_entries_lock, and release it properly.166*/167static inline void firmware_map_remove_entry(struct firmware_map_entry *entry)168{169list_del(&entry->list);170}171172/*173* Add memmap entry on sysfs174*/175static int add_sysfs_fw_map_entry(struct firmware_map_entry *entry)176{177static int map_entries_nr;178static struct kset *mmap_kset;179180if (entry->kobj.state_in_sysfs)181return -EEXIST;182183if (!mmap_kset) {184mmap_kset = kset_create_and_add("memmap", NULL, firmware_kobj);185if (!mmap_kset)186return -ENOMEM;187}188189entry->kobj.kset = mmap_kset;190if (kobject_add(&entry->kobj, NULL, "%d", map_entries_nr++))191kobject_put(&entry->kobj);192193return 0;194}195196/*197* Remove memmap entry on sysfs198*/199static inline void remove_sysfs_fw_map_entry(struct firmware_map_entry *entry)200{201kobject_put(&entry->kobj);202}203204/**205* firmware_map_find_entry_in_list() - Search memmap entry in a given list.206* @start: Start of the memory range.207* @end: End of the memory range (exclusive).208* @type: Type of the memory range.209* @list: In which to find the entry.210*211* This function is to find the memmap entey of a given memory range in a212* given list. The caller must hold map_entries_lock, and must not release213* the lock until the processing of the returned entry has completed.214*215* Return: Pointer to the entry to be found on success, or NULL on failure.216*/217static struct firmware_map_entry * __meminit218firmware_map_find_entry_in_list(u64 start, u64 end, const char *type,219struct list_head *list)220{221struct firmware_map_entry *entry;222223list_for_each_entry(entry, list, list)224if ((entry->start == start) && (entry->end == end) &&225(!strcmp(entry->type, type))) {226return entry;227}228229return NULL;230}231232/**233* firmware_map_find_entry() - Search memmap entry in map_entries.234* @start: Start of the memory range.235* @end: End of the memory range (exclusive).236* @type: Type of the memory range.237*238* This function is to find the memmap entey of a given memory range.239* The caller must hold map_entries_lock, and must not release the lock240* until the processing of the returned entry has completed.241*242* Return: Pointer to the entry to be found on success, or NULL on failure.243*/244static struct firmware_map_entry * __meminit245firmware_map_find_entry(u64 start, u64 end, const char *type)246{247return firmware_map_find_entry_in_list(start, end, type, &map_entries);248}249250/**251* firmware_map_find_entry_bootmem() - Search memmap entry in map_entries_bootmem.252* @start: Start of the memory range.253* @end: End of the memory range (exclusive).254* @type: Type of the memory range.255*256* This function is similar to firmware_map_find_entry except that it find the257* given entry in map_entries_bootmem.258*259* Return: Pointer to the entry to be found on success, or NULL on failure.260*/261static struct firmware_map_entry * __meminit262firmware_map_find_entry_bootmem(u64 start, u64 end, const char *type)263{264return firmware_map_find_entry_in_list(start, end, type,265&map_entries_bootmem);266}267268/**269* firmware_map_add_hotplug() - Adds a firmware mapping entry when we do270* memory hotplug.271* @start: Start of the memory range.272* @end: End of the memory range (exclusive)273* @type: Type of the memory range.274*275* Adds a firmware mapping entry. This function is for memory hotplug, it is276* similar to function firmware_map_add_early(). The only difference is that277* it will create the syfs entry dynamically.278*279* Return: 0 on success, or -ENOMEM if no memory could be allocated.280*/281int __meminit firmware_map_add_hotplug(u64 start, u64 end, const char *type)282{283struct firmware_map_entry *entry;284285entry = firmware_map_find_entry(start, end - 1, type);286if (entry)287return 0;288289entry = firmware_map_find_entry_bootmem(start, end - 1, type);290if (!entry) {291entry = kzalloc(sizeof(struct firmware_map_entry), GFP_ATOMIC);292if (!entry)293return -ENOMEM;294} else {295/* Reuse storage allocated by bootmem. */296spin_lock(&map_entries_bootmem_lock);297list_del(&entry->list);298spin_unlock(&map_entries_bootmem_lock);299300memset(entry, 0, sizeof(*entry));301}302303firmware_map_add_entry(start, end, type, entry);304/* create the memmap entry */305add_sysfs_fw_map_entry(entry);306307return 0;308}309310/**311* firmware_map_add_early() - Adds a firmware mapping entry.312* @start: Start of the memory range.313* @end: End of the memory range.314* @type: Type of the memory range.315*316* Adds a firmware mapping entry. This function uses the bootmem allocator317* for memory allocation.318*319* That function must be called before late_initcall.320*321* Return: 0 on success, or -ENOMEM if no memory could be allocated.322*/323int __init firmware_map_add_early(u64 start, u64 end, const char *type)324{325struct firmware_map_entry *entry;326327entry = memblock_alloc(sizeof(struct firmware_map_entry),328SMP_CACHE_BYTES);329if (WARN_ON(!entry))330return -ENOMEM;331332return firmware_map_add_entry(start, end, type, entry);333}334335/**336* firmware_map_remove() - remove a firmware mapping entry337* @start: Start of the memory range.338* @end: End of the memory range.339* @type: Type of the memory range.340*341* removes a firmware mapping entry.342*343* Return: 0 on success, or -EINVAL if no entry.344*/345int __meminit firmware_map_remove(u64 start, u64 end, const char *type)346{347struct firmware_map_entry *entry;348349spin_lock(&map_entries_lock);350entry = firmware_map_find_entry(start, end - 1, type);351if (!entry) {352spin_unlock(&map_entries_lock);353return -EINVAL;354}355356firmware_map_remove_entry(entry);357spin_unlock(&map_entries_lock);358359/* remove the memmap entry */360remove_sysfs_fw_map_entry(entry);361362return 0;363}364365/*366* Sysfs functions -------------------------------------------------------------367*/368369static ssize_t start_show(struct firmware_map_entry *entry, char *buf)370{371return snprintf(buf, PAGE_SIZE, "0x%llx\n",372(unsigned long long)entry->start);373}374375static ssize_t end_show(struct firmware_map_entry *entry, char *buf)376{377return snprintf(buf, PAGE_SIZE, "0x%llx\n",378(unsigned long long)entry->end);379}380381static ssize_t type_show(struct firmware_map_entry *entry, char *buf)382{383return snprintf(buf, PAGE_SIZE, "%s\n", entry->type);384}385386static inline struct memmap_attribute *to_memmap_attr(struct attribute *attr)387{388return container_of(attr, struct memmap_attribute, attr);389}390391static ssize_t memmap_attr_show(struct kobject *kobj,392struct attribute *attr, char *buf)393{394struct firmware_map_entry *entry = to_memmap_entry(kobj);395struct memmap_attribute *memmap_attr = to_memmap_attr(attr);396397return memmap_attr->show(entry, buf);398}399400/*401* Initialises stuff and adds the entries in the map_entries list to402* sysfs. Important is that firmware_map_add() and firmware_map_add_early()403* must be called before late_initcall. That's just because that function404* is called as late_initcall() function, which means that if you call405* firmware_map_add() or firmware_map_add_early() afterwards, the entries406* are not added to sysfs.407*/408static int __init firmware_memmap_init(void)409{410struct firmware_map_entry *entry;411412list_for_each_entry(entry, &map_entries, list)413add_sysfs_fw_map_entry(entry);414415return 0;416}417late_initcall(firmware_memmap_init);418419420421