Linux Audio

Check our new training course

Loading...
v5.9
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * linux/drivers/firmware/memmap.c
  4 *  Copyright (C) 2008 SUSE LINUX Products GmbH
  5 *  by Bernhard Walle <bernhard.walle@gmx.de>
 
 
 
 
 
 
 
 
 
 
  6 */
  7
  8#include <linux/string.h>
  9#include <linux/firmware-map.h>
 10#include <linux/kernel.h>
 11#include <linux/module.h>
 12#include <linux/types.h>
 13#include <linux/memblock.h>
 14#include <linux/slab.h>
 15#include <linux/mm.h>
 16
 17/*
 18 * Data types ------------------------------------------------------------------
 19 */
 20
 21/*
 22 * Firmware map entry. Because firmware memory maps are flat and not
 23 * hierarchical, it's ok to organise them in a linked list. No parent
 24 * information is necessary as for the resource tree.
 25 */
 26struct firmware_map_entry {
 27	/*
 28	 * start and end must be u64 rather than resource_size_t, because e820
 29	 * resources can lie at addresses above 4G.
 30	 */
 31	u64			start;	/* start of the memory range */
 32	u64			end;	/* end of the memory range (incl.) */
 33	const char		*type;	/* type of the memory range */
 34	struct list_head	list;	/* entry for the linked list */
 35	struct kobject		kobj;   /* kobject for each entry */
 36};
 37
 38/*
 39 * Forward declarations --------------------------------------------------------
 40 */
 41static ssize_t memmap_attr_show(struct kobject *kobj,
 42				struct attribute *attr, char *buf);
 43static ssize_t start_show(struct firmware_map_entry *entry, char *buf);
 44static ssize_t end_show(struct firmware_map_entry *entry, char *buf);
 45static ssize_t type_show(struct firmware_map_entry *entry, char *buf);
 46
 47static struct firmware_map_entry * __meminit
 48firmware_map_find_entry(u64 start, u64 end, const char *type);
 49
 50/*
 51 * Static data -----------------------------------------------------------------
 52 */
 53
 54struct memmap_attribute {
 55	struct attribute attr;
 56	ssize_t (*show)(struct firmware_map_entry *entry, char *buf);
 57};
 58
 59static struct memmap_attribute memmap_start_attr = __ATTR_RO(start);
 60static struct memmap_attribute memmap_end_attr   = __ATTR_RO(end);
 61static struct memmap_attribute memmap_type_attr  = __ATTR_RO(type);
 62
 63/*
 64 * These are default attributes that are added for every memmap entry.
 65 */
 66static struct attribute *def_attrs[] = {
 67	&memmap_start_attr.attr,
 68	&memmap_end_attr.attr,
 69	&memmap_type_attr.attr,
 70	NULL
 71};
 72
 73static const struct sysfs_ops memmap_attr_ops = {
 74	.show = memmap_attr_show,
 75};
 76
 77/* Firmware memory map entries. */
 78static LIST_HEAD(map_entries);
 79static DEFINE_SPINLOCK(map_entries_lock);
 80
 81/*
 82 * For memory hotplug, there is no way to free memory map entries allocated
 83 * by boot mem after the system is up. So when we hot-remove memory whose
 84 * map entry is allocated by bootmem, we need to remember the storage and
 85 * reuse it when the memory is hot-added again.
 86 */
 87static LIST_HEAD(map_entries_bootmem);
 88static DEFINE_SPINLOCK(map_entries_bootmem_lock);
 89
 90
 91static inline struct firmware_map_entry *
 92to_memmap_entry(struct kobject *kobj)
 93{
 94	return container_of(kobj, struct firmware_map_entry, kobj);
 95}
 96
 97static void __meminit release_firmware_map_entry(struct kobject *kobj)
 98{
 99	struct firmware_map_entry *entry = to_memmap_entry(kobj);
100
101	if (PageReserved(virt_to_page(entry))) {
102		/*
103		 * Remember the storage allocated by bootmem, and reuse it when
104		 * the memory is hot-added again. The entry will be added to
105		 * map_entries_bootmem here, and deleted from &map_entries in
106		 * firmware_map_remove_entry().
107		 */
108		spin_lock(&map_entries_bootmem_lock);
109		list_add(&entry->list, &map_entries_bootmem);
110		spin_unlock(&map_entries_bootmem_lock);
111
112		return;
113	}
114
115	kfree(entry);
116}
117
118static struct kobj_type __refdata memmap_ktype = {
119	.release	= release_firmware_map_entry,
120	.sysfs_ops	= &memmap_attr_ops,
121	.default_attrs	= def_attrs,
122};
123
124/*
125 * Registration functions ------------------------------------------------------
126 */
127
 
 
 
 
 
 
 
128/**
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), uninitialised
134 *         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 always
140 */
141static int firmware_map_add_entry(u64 start, u64 end,
142				  const char *type,
143				  struct firmware_map_entry *entry)
144{
145	BUG_ON(start > end);
146
147	entry->start = start;
148	entry->end = end - 1;
149	entry->type = type;
150	INIT_LIST_HEAD(&entry->list);
151	kobject_init(&entry->kobj, &memmap_ktype);
152
153	spin_lock(&map_entries_lock);
154	list_add_tail(&entry->list, &map_entries);
155	spin_unlock(&map_entries_lock);
156
157	return 0;
158}
159
160/**
161 * firmware_map_remove_entry() - Does the real work to remove a firmware
162 * 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{
169	list_del(&entry->list);
170}
171
172/*
173 * Add memmap entry on sysfs
174 */
175static int add_sysfs_fw_map_entry(struct firmware_map_entry *entry)
176{
177	static int map_entries_nr;
178	static struct kset *mmap_kset;
179
180	if (entry->kobj.state_in_sysfs)
181		return -EEXIST;
182
183	if (!mmap_kset) {
184		mmap_kset = kset_create_and_add("memmap", NULL, firmware_kobj);
185		if (!mmap_kset)
186			return -ENOMEM;
187	}
188
189	entry->kobj.kset = mmap_kset;
190	if (kobject_add(&entry->kobj, NULL, "%d", map_entries_nr++))
191		kobject_put(&entry->kobj);
192
193	return 0;
194}
195
196/*
197 * Remove memmap entry on sysfs
198 */
199static inline void remove_sysfs_fw_map_entry(struct firmware_map_entry *entry)
200{
201	kobject_put(&entry->kobj);
202}
203
204/**
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 a
212 * given list. The caller must hold map_entries_lock, and must not release
213 * 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 * __meminit
218firmware_map_find_entry_in_list(u64 start, u64 end, const char *type,
219				struct list_head *list)
220{
221	struct firmware_map_entry *entry;
222
223	list_for_each_entry(entry, list, list)
224		if ((entry->start == start) && (entry->end == end) &&
225		    (!strcmp(entry->type, type))) {
226			return entry;
227		}
228
229	return NULL;
230}
231
232/**
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 lock
240 * 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 * __meminit
245firmware_map_find_entry(u64 start, u64 end, const char *type)
246{
247	return firmware_map_find_entry_in_list(start, end, type, &map_entries);
248}
249
250/**
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 the
257 * 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 * __meminit
262firmware_map_find_entry_bootmem(u64 start, u64 end, const char *type)
263{
264	return firmware_map_find_entry_in_list(start, end, type,
265					       &map_entries_bootmem);
266}
267
268/**
269 * firmware_map_add_hotplug() - Adds a firmware mapping entry when we do
270 * 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 is
276 * similar to function firmware_map_add_early(). The only difference is that
277 * 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{
283	struct firmware_map_entry *entry;
284
285	entry = firmware_map_find_entry(start, end - 1, type);
286	if (entry)
287		return 0;
288
289	entry = firmware_map_find_entry_bootmem(start, end - 1, type);
290	if (!entry) {
291		entry = kzalloc(sizeof(struct firmware_map_entry), GFP_ATOMIC);
292		if (!entry)
293			return -ENOMEM;
294	} else {
295		/* Reuse storage allocated by bootmem. */
296		spin_lock(&map_entries_bootmem_lock);
297		list_del(&entry->list);
298		spin_unlock(&map_entries_bootmem_lock);
299
300		memset(entry, 0, sizeof(*entry));
301	}
302
303	firmware_map_add_entry(start, end, type, entry);
304	/* create the memmap entry */
305	add_sysfs_fw_map_entry(entry);
306
307	return 0;
308}
309
310/**
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 allocator
317 * 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{
325	struct firmware_map_entry *entry;
326
327	entry = memblock_alloc(sizeof(struct firmware_map_entry),
328			       SMP_CACHE_BYTES);
329	if (WARN_ON(!entry))
330		return -ENOMEM;
331
332	return firmware_map_add_entry(start, end, type, entry);
333}
334
335/**
336 * firmware_map_remove() - remove a firmware mapping entry
337 * @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{
347	struct firmware_map_entry *entry;
348
349	spin_lock(&map_entries_lock);
350	entry = firmware_map_find_entry(start, end - 1, type);
351	if (!entry) {
352		spin_unlock(&map_entries_lock);
353		return -EINVAL;
354	}
355
356	firmware_map_remove_entry(entry);
357	spin_unlock(&map_entries_lock);
358
359	/* remove the memmap entry */
360	remove_sysfs_fw_map_entry(entry);
361
362	return 0;
363}
364
365/*
366 * Sysfs functions -------------------------------------------------------------
367 */
368
369static ssize_t start_show(struct firmware_map_entry *entry, char *buf)
370{
371	return snprintf(buf, PAGE_SIZE, "0x%llx\n",
372		(unsigned long long)entry->start);
373}
374
375static ssize_t end_show(struct firmware_map_entry *entry, char *buf)
376{
377	return snprintf(buf, PAGE_SIZE, "0x%llx\n",
378		(unsigned long long)entry->end);
379}
380
381static ssize_t type_show(struct firmware_map_entry *entry, char *buf)
382{
383	return snprintf(buf, PAGE_SIZE, "%s\n", entry->type);
384}
385
386static inline struct memmap_attribute *to_memmap_attr(struct attribute *attr)
387{
388	return container_of(attr, struct memmap_attribute, attr);
389}
390
391static ssize_t memmap_attr_show(struct kobject *kobj,
392				struct attribute *attr, char *buf)
393{
394	struct firmware_map_entry *entry = to_memmap_entry(kobj);
395	struct memmap_attribute *memmap_attr = to_memmap_attr(attr);
396
397	return memmap_attr->show(entry, buf);
398}
399
400/*
401 * Initialises stuff and adds the entries in the map_entries list to
402 * sysfs. Important is that firmware_map_add() and firmware_map_add_early()
403 * must be called before late_initcall. That's just because that function
404 * is called as late_initcall() function, which means that if you call
405 * firmware_map_add() or firmware_map_add_early() afterwards, the entries
406 * are not added to sysfs.
407 */
408static int __init firmware_memmap_init(void)
409{
410	struct firmware_map_entry *entry;
411
412	list_for_each_entry(entry, &map_entries, list)
413		add_sysfs_fw_map_entry(entry);
414
415	return 0;
416}
417late_initcall(firmware_memmap_init);
418
v3.5.6
 
  1/*
  2 * linux/drivers/firmware/memmap.c
  3 *  Copyright (C) 2008 SUSE LINUX Products GmbH
  4 *  by Bernhard Walle <bernhard.walle@gmx.de>
  5 *
  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
  9 *
 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.
 14 *
 15 */
 16
 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
 25/*
 26 * Data types ------------------------------------------------------------------
 27 */
 28
 29/*
 30 * Firmware map entry. Because firmware memory maps are flat and not
 31 * hierarchical, it's ok to organise them in a linked list. No parent
 32 * information is necessary as for the resource tree.
 33 */
 34struct firmware_map_entry {
 35	/*
 36	 * start and end must be u64 rather than resource_size_t, because e820
 37	 * resources can lie at addresses above 4G.
 38	 */
 39	u64			start;	/* start of the memory range */
 40	u64			end;	/* end of the memory range (incl.) */
 41	const char		*type;	/* type of the memory range */
 42	struct list_head	list;	/* entry for the linked list */
 43	struct kobject		kobj;   /* kobject for each entry */
 44};
 45
 46/*
 47 * Forward declarations --------------------------------------------------------
 48 */
 49static ssize_t memmap_attr_show(struct kobject *kobj,
 50				struct attribute *attr, char *buf);
 51static ssize_t start_show(struct firmware_map_entry *entry, char *buf);
 52static ssize_t end_show(struct firmware_map_entry *entry, char *buf);
 53static ssize_t type_show(struct firmware_map_entry *entry, char *buf);
 54
 
 
 
 55/*
 56 * Static data -----------------------------------------------------------------
 57 */
 58
 59struct memmap_attribute {
 60	struct attribute attr;
 61	ssize_t (*show)(struct firmware_map_entry *entry, char *buf);
 62};
 63
 64static struct memmap_attribute memmap_start_attr = __ATTR_RO(start);
 65static struct memmap_attribute memmap_end_attr   = __ATTR_RO(end);
 66static struct memmap_attribute memmap_type_attr  = __ATTR_RO(type);
 67
 68/*
 69 * These are default attributes that are added for every memmap entry.
 70 */
 71static struct attribute *def_attrs[] = {
 72	&memmap_start_attr.attr,
 73	&memmap_end_attr.attr,
 74	&memmap_type_attr.attr,
 75	NULL
 76};
 77
 78static const struct sysfs_ops memmap_attr_ops = {
 79	.show = memmap_attr_show,
 80};
 81
 82static struct kobj_type memmap_ktype = {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 83	.sysfs_ops	= &memmap_attr_ops,
 84	.default_attrs	= def_attrs,
 85};
 86
 87/*
 88 * Registration functions ------------------------------------------------------
 89 */
 90
 91/*
 92 * Firmware memory map entries. No locking is needed because the
 93 * firmware_map_add() and firmware_map_add_early() functions are called
 94 * in firmware initialisation code in one single thread of execution.
 95 */
 96static LIST_HEAD(map_entries);
 97
 98/**
 99 * firmware_map_add_entry() - Does the real work to add a firmware memmap entry.
100 * @start: Start of the memory range.
101 * @end:   End of the memory range (inclusive).
102 * @type:  Type of the memory range.
103 * @entry: Pre-allocated (either kmalloc() or bootmem allocator), uninitialised
104 *         entry.
105 *
106 * Common implementation of firmware_map_add() and firmware_map_add_early()
107 * which expects a pre-allocated struct firmware_map_entry.
108 **/
 
 
109static int firmware_map_add_entry(u64 start, u64 end,
110				  const char *type,
111				  struct firmware_map_entry *entry)
112{
113	BUG_ON(start > end);
114
115	entry->start = start;
116	entry->end = end;
117	entry->type = type;
118	INIT_LIST_HEAD(&entry->list);
119	kobject_init(&entry->kobj, &memmap_ktype);
120
 
121	list_add_tail(&entry->list, &map_entries);
 
122
123	return 0;
124}
125
 
 
 
 
 
 
 
 
 
 
 
 
126/*
127 * Add memmap entry on sysfs
128 */
129static int add_sysfs_fw_map_entry(struct firmware_map_entry *entry)
130{
131	static int map_entries_nr;
132	static struct kset *mmap_kset;
133
 
 
 
134	if (!mmap_kset) {
135		mmap_kset = kset_create_and_add("memmap", NULL, firmware_kobj);
136		if (!mmap_kset)
137			return -ENOMEM;
138	}
139
140	entry->kobj.kset = mmap_kset;
141	if (kobject_add(&entry->kobj, NULL, "%d", map_entries_nr++))
142		kobject_put(&entry->kobj);
143
144	return 0;
145}
146
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
147/**
148 * firmware_map_add_hotplug() - Adds a firmware mapping entry when we do
149 * memory hotplug.
150 * @start: Start of the memory range.
151 * @end:   End of the memory range (inclusive).
152 * @type:  Type of the memory range.
153 *
154 * Adds a firmware mapping entry. This function is for memory hotplug, it is
155 * similar to function firmware_map_add_early(). The only difference is that
156 * it will create the syfs entry dynamically.
157 *
158 * Returns 0 on success, or -ENOMEM if no memory could be allocated.
159 **/
160int __meminit firmware_map_add_hotplug(u64 start, u64 end, const char *type)
161{
162	struct firmware_map_entry *entry;
163
164	entry = kzalloc(sizeof(struct firmware_map_entry), GFP_ATOMIC);
165	if (!entry)
166		return -ENOMEM;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
167
168	firmware_map_add_entry(start, end, type, entry);
169	/* create the memmap entry */
170	add_sysfs_fw_map_entry(entry);
171
172	return 0;
173}
174
175/**
176 * firmware_map_add_early() - Adds a firmware mapping entry.
177 * @start: Start of the memory range.
178 * @end:   End of the memory range (inclusive).
179 * @type:  Type of the memory range.
180 *
181 * Adds a firmware mapping entry. This function uses the bootmem allocator
182 * for memory allocation.
183 *
184 * That function must be called before late_initcall.
185 *
186 * Returns 0 on success, or -ENOMEM if no memory could be allocated.
187 **/
188int __init firmware_map_add_early(u64 start, u64 end, const char *type)
189{
190	struct firmware_map_entry *entry;
191
192	entry = alloc_bootmem(sizeof(struct firmware_map_entry));
 
193	if (WARN_ON(!entry))
194		return -ENOMEM;
195
196	return firmware_map_add_entry(start, end, type, entry);
197}
198
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
199/*
200 * Sysfs functions -------------------------------------------------------------
201 */
202
203static ssize_t start_show(struct firmware_map_entry *entry, char *buf)
204{
205	return snprintf(buf, PAGE_SIZE, "0x%llx\n",
206		(unsigned long long)entry->start);
207}
208
209static ssize_t end_show(struct firmware_map_entry *entry, char *buf)
210{
211	return snprintf(buf, PAGE_SIZE, "0x%llx\n",
212		(unsigned long long)entry->end);
213}
214
215static ssize_t type_show(struct firmware_map_entry *entry, char *buf)
216{
217	return snprintf(buf, PAGE_SIZE, "%s\n", entry->type);
218}
219
220#define to_memmap_attr(_attr) container_of(_attr, struct memmap_attribute, attr)
221#define to_memmap_entry(obj) container_of(obj, struct firmware_map_entry, kobj)
 
 
222
223static ssize_t memmap_attr_show(struct kobject *kobj,
224				struct attribute *attr, char *buf)
225{
226	struct firmware_map_entry *entry = to_memmap_entry(kobj);
227	struct memmap_attribute *memmap_attr = to_memmap_attr(attr);
228
229	return memmap_attr->show(entry, buf);
230}
231
232/*
233 * Initialises stuff and adds the entries in the map_entries list to
234 * sysfs. Important is that firmware_map_add() and firmware_map_add_early()
235 * must be called before late_initcall. That's just because that function
236 * is called as late_initcall() function, which means that if you call
237 * firmware_map_add() or firmware_map_add_early() afterwards, the entries
238 * are not added to sysfs.
239 */
240static int __init memmap_init(void)
241{
242	struct firmware_map_entry *entry;
243
244	list_for_each_entry(entry, &map_entries, list)
245		add_sysfs_fw_map_entry(entry);
246
247	return 0;
248}
249late_initcall(memmap_init);
250