blob: cc016c615c19ab50d0818330423c75313c17c8b5 [file] [log] [blame]
Bernhard Walle69ac9cd2008-06-27 13:12:54 +02001/*
2 * linux/drivers/firmware/memmap.c
3 * Copyright (C) 2008 SUSE LINUX Products GmbH
Bernhard Walle97bef7d2009-02-18 14:48:40 -08004 * by Bernhard Walle <bernhard.walle@gmx.de>
Bernhard Walle69ac9cd2008-06-27 13:12:54 +02005 *
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Yasuaki Ishimatsu46c66c42013-02-22 16:32:56 -080024#include <linux/mm.h>
Bernhard Walle69ac9cd2008-06-27 13:12:54 +020025
26/*
27 * Data types ------------------------------------------------------------------
28 */
29
30/*
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.
34 */
35struct firmware_map_entry {
Yinghai Lu3b0fde02009-06-16 15:31:16 -070036 /*
37 * start and end must be u64 rather than resource_size_t, because e820
38 * resources can lie at addresses above 4G.
39 */
40 u64 start; /* start of the memory range */
41 u64 end; /* end of the memory range (incl.) */
Bernhard Walle69ac9cd2008-06-27 13:12:54 +020042 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 */
45};
46
47/*
48 * Forward declarations --------------------------------------------------------
49 */
50static ssize_t memmap_attr_show(struct kobject *kobj,
51 struct attribute *attr, char *buf);
52static ssize_t start_show(struct firmware_map_entry *entry, char *buf);
53static ssize_t end_show(struct firmware_map_entry *entry, char *buf);
54static ssize_t type_show(struct firmware_map_entry *entry, char *buf);
55
Yasuaki Ishimatsu46c66c42013-02-22 16:32:56 -080056static struct firmware_map_entry * __meminit
57firmware_map_find_entry(u64 start, u64 end, const char *type);
58
Bernhard Walle69ac9cd2008-06-27 13:12:54 +020059/*
60 * Static data -----------------------------------------------------------------
61 */
62
63struct memmap_attribute {
64 struct attribute attr;
65 ssize_t (*show)(struct firmware_map_entry *entry, char *buf);
66};
67
Roel Kluinda2bdf92009-01-07 18:09:15 -080068static struct memmap_attribute memmap_start_attr = __ATTR_RO(start);
69static struct memmap_attribute memmap_end_attr = __ATTR_RO(end);
70static struct memmap_attribute memmap_type_attr = __ATTR_RO(type);
Bernhard Walle69ac9cd2008-06-27 13:12:54 +020071
72/*
73 * These are default attributes that are added for every memmap entry.
74 */
75static struct attribute *def_attrs[] = {
76 &memmap_start_attr.attr,
77 &memmap_end_attr.attr,
78 &memmap_type_attr.attr,
79 NULL
80};
81
Emese Revfy52cf25d2010-01-19 02:58:23 +010082static const struct sysfs_ops memmap_attr_ops = {
Bernhard Walle69ac9cd2008-06-27 13:12:54 +020083 .show = memmap_attr_show,
84};
85
Yasuaki Ishimatsu46c66c42013-02-22 16:32:56 -080086/* Firmware memory map entries. */
87static LIST_HEAD(map_entries);
88static DEFINE_SPINLOCK(map_entries_lock);
89
90/*
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.
95 */
96static LIST_HEAD(map_entries_bootmem);
97static DEFINE_SPINLOCK(map_entries_bootmem_lock);
98
99
100static inline struct firmware_map_entry *
101to_memmap_entry(struct kobject *kobj)
102{
103 return container_of(kobj, struct firmware_map_entry, kobj);
104}
105
106static void __meminit release_firmware_map_entry(struct kobject *kobj)
107{
108 struct firmware_map_entry *entry = to_memmap_entry(kobj);
109
110 if (PageReserved(virt_to_page(entry))) {
111 /*
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().
116 */
Yasuaki Ishimatsu7a6f93b2013-04-29 15:08:39 -0700117 spin_lock(&map_entries_bootmem_lock);
118 list_add(&entry->list, &map_entries_bootmem);
119 spin_unlock(&map_entries_bootmem_lock);
Yasuaki Ishimatsu46c66c42013-02-22 16:32:56 -0800120
121 return;
122 }
123
124 kfree(entry);
125}
126
127static struct kobj_type __refdata memmap_ktype = {
128 .release = release_firmware_map_entry,
Bernhard Walle69ac9cd2008-06-27 13:12:54 +0200129 .sysfs_ops = &memmap_attr_ops,
130 .default_attrs = def_attrs,
131};
132
133/*
134 * Registration functions ------------------------------------------------------
135 */
136
Bernhard Walle69ac9cd2008-06-27 13:12:54 +0200137/**
Bernhard Walle31bad922008-08-12 15:09:14 -0700138 * firmware_map_add_entry() - Does the real work to add a firmware memmap entry.
Bernhard Walle69ac9cd2008-06-27 13:12:54 +0200139 * @start: Start of the memory range.
Yasuaki Ishimatsu4ed940d2012-07-30 14:41:13 -0700140 * @end: End of the memory range (exclusive).
Bernhard Walle69ac9cd2008-06-27 13:12:54 +0200141 * @type: Type of the memory range.
142 * @entry: Pre-allocated (either kmalloc() or bootmem allocator), uninitialised
143 * entry.
Bernhard Walle31bad922008-08-12 15:09:14 -0700144 *
145 * Common implementation of firmware_map_add() and firmware_map_add_early()
146 * which expects a pre-allocated struct firmware_map_entry.
147 **/
Yinghai Lu3b0fde02009-06-16 15:31:16 -0700148static int firmware_map_add_entry(u64 start, u64 end,
Bernhard Walle69ac9cd2008-06-27 13:12:54 +0200149 const char *type,
150 struct firmware_map_entry *entry)
151{
152 BUG_ON(start > end);
153
154 entry->start = start;
Yasuaki Ishimatsu4ed940d2012-07-30 14:41:13 -0700155 entry->end = end - 1;
Bernhard Walle69ac9cd2008-06-27 13:12:54 +0200156 entry->type = type;
157 INIT_LIST_HEAD(&entry->list);
158 kobject_init(&entry->kobj, &memmap_ktype);
159
Yasuaki Ishimatsu46c66c42013-02-22 16:32:56 -0800160 spin_lock(&map_entries_lock);
Bernhard Walle69ac9cd2008-06-27 13:12:54 +0200161 list_add_tail(&entry->list, &map_entries);
Yasuaki Ishimatsu46c66c42013-02-22 16:32:56 -0800162 spin_unlock(&map_entries_lock);
Bernhard Walle69ac9cd2008-06-27 13:12:54 +0200163
164 return 0;
165}
166
Yasuaki Ishimatsu46c66c42013-02-22 16:32:56 -0800167/**
168 * firmware_map_remove_entry() - Does the real work to remove a firmware
169 * memmap entry.
170 * @entry: removed entry.
171 *
172 * The caller must hold map_entries_lock, and release it properly.
173 **/
174static inline void firmware_map_remove_entry(struct firmware_map_entry *entry)
175{
176 list_del(&entry->list);
177}
178
akpm@linux-foundation.orgd96ae532010-03-05 13:41:58 -0800179/*
180 * Add memmap entry on sysfs
181 */
182static int add_sysfs_fw_map_entry(struct firmware_map_entry *entry)
183{
184 static int map_entries_nr;
185 static struct kset *mmap_kset;
186
Yasuaki Ishimatsu22880eb2014-10-09 15:29:07 -0700187 if (entry->kobj.state_in_sysfs)
188 return -EEXIST;
189
akpm@linux-foundation.orgd96ae532010-03-05 13:41:58 -0800190 if (!mmap_kset) {
191 mmap_kset = kset_create_and_add("memmap", NULL, firmware_kobj);
192 if (!mmap_kset)
193 return -ENOMEM;
194 }
195
196 entry->kobj.kset = mmap_kset;
197 if (kobject_add(&entry->kobj, NULL, "%d", map_entries_nr++))
198 kobject_put(&entry->kobj);
199
200 return 0;
201}
202
Yasuaki Ishimatsu46c66c42013-02-22 16:32:56 -0800203/*
204 * Remove memmap entry on sysfs
205 */
206static inline void remove_sysfs_fw_map_entry(struct firmware_map_entry *entry)
207{
208 kobject_put(&entry->kobj);
209}
210
211/*
212 * firmware_map_find_entry_in_list() - Search memmap entry in a given list.
213 * @start: Start of the memory range.
214 * @end: End of the memory range (exclusive).
215 * @type: Type of the memory range.
216 * @list: In which to find the entry.
217 *
218 * This function is to find the memmap entey of a given memory range in a
219 * given list. The caller must hold map_entries_lock, and must not release
220 * the lock until the processing of the returned entry has completed.
221 *
222 * Return: Pointer to the entry to be found on success, or NULL on failure.
223 */
224static struct firmware_map_entry * __meminit
225firmware_map_find_entry_in_list(u64 start, u64 end, const char *type,
226 struct list_head *list)
227{
228 struct firmware_map_entry *entry;
229
230 list_for_each_entry(entry, list, list)
231 if ((entry->start == start) && (entry->end == end) &&
232 (!strcmp(entry->type, type))) {
233 return entry;
234 }
235
236 return NULL;
237}
238
239/*
240 * firmware_map_find_entry() - Search memmap entry in map_entries.
241 * @start: Start of the memory range.
242 * @end: End of the memory range (exclusive).
243 * @type: Type of the memory range.
244 *
245 * This function is to find the memmap entey of a given memory range.
246 * The caller must hold map_entries_lock, and must not release the lock
247 * until the processing of the returned entry has completed.
248 *
249 * Return: Pointer to the entry to be found on success, or NULL on failure.
250 */
251static struct firmware_map_entry * __meminit
252firmware_map_find_entry(u64 start, u64 end, const char *type)
253{
254 return firmware_map_find_entry_in_list(start, end, type, &map_entries);
255}
256
257/*
258 * firmware_map_find_entry_bootmem() - Search memmap entry in map_entries_bootmem.
259 * @start: Start of the memory range.
260 * @end: End of the memory range (exclusive).
261 * @type: Type of the memory range.
262 *
263 * This function is similar to firmware_map_find_entry except that it find the
264 * given entry in map_entries_bootmem.
265 *
266 * Return: Pointer to the entry to be found on success, or NULL on failure.
267 */
268static struct firmware_map_entry * __meminit
269firmware_map_find_entry_bootmem(u64 start, u64 end, const char *type)
270{
271 return firmware_map_find_entry_in_list(start, end, type,
272 &map_entries_bootmem);
273}
274
Bernhard Walle31bad922008-08-12 15:09:14 -0700275/**
akpm@linux-foundation.orgd96ae532010-03-05 13:41:58 -0800276 * firmware_map_add_hotplug() - Adds a firmware mapping entry when we do
277 * memory hotplug.
Bernhard Walle31bad922008-08-12 15:09:14 -0700278 * @start: Start of the memory range.
Yasuaki Ishimatsu4ed940d2012-07-30 14:41:13 -0700279 * @end: End of the memory range (exclusive)
Bernhard Walle31bad922008-08-12 15:09:14 -0700280 * @type: Type of the memory range.
281 *
akpm@linux-foundation.orgd96ae532010-03-05 13:41:58 -0800282 * Adds a firmware mapping entry. This function is for memory hotplug, it is
283 * similar to function firmware_map_add_early(). The only difference is that
284 * it will create the syfs entry dynamically.
Bernhard Walle31bad922008-08-12 15:09:14 -0700285 *
286 * Returns 0 on success, or -ENOMEM if no memory could be allocated.
287 **/
akpm@linux-foundation.orgd96ae532010-03-05 13:41:58 -0800288int __meminit firmware_map_add_hotplug(u64 start, u64 end, const char *type)
Bernhard Walle69ac9cd2008-06-27 13:12:54 +0200289{
290 struct firmware_map_entry *entry;
291
Yasuaki Ishimatsuf0093ed2014-08-06 16:07:03 -0700292 entry = firmware_map_find_entry(start, end - 1, type);
293 if (entry)
294 return 0;
295
Yasuaki Ishimatsu49c8b242014-08-06 16:07:00 -0700296 entry = firmware_map_find_entry_bootmem(start, end - 1, type);
Yasuaki Ishimatsu46c66c42013-02-22 16:32:56 -0800297 if (!entry) {
298 entry = kzalloc(sizeof(struct firmware_map_entry), GFP_ATOMIC);
299 if (!entry)
300 return -ENOMEM;
301 } else {
302 /* Reuse storage allocated by bootmem. */
303 spin_lock(&map_entries_bootmem_lock);
304 list_del(&entry->list);
305 spin_unlock(&map_entries_bootmem_lock);
306
307 memset(entry, 0, sizeof(*entry));
308 }
Bernhard Walle69ac9cd2008-06-27 13:12:54 +0200309
akpm@linux-foundation.orgd96ae532010-03-05 13:41:58 -0800310 firmware_map_add_entry(start, end, type, entry);
311 /* create the memmap entry */
312 add_sysfs_fw_map_entry(entry);
313
314 return 0;
Bernhard Walle69ac9cd2008-06-27 13:12:54 +0200315}
316
Bernhard Walle31bad922008-08-12 15:09:14 -0700317/**
318 * firmware_map_add_early() - Adds a firmware mapping entry.
319 * @start: Start of the memory range.
Yasuaki Ishimatsu4ed940d2012-07-30 14:41:13 -0700320 * @end: End of the memory range.
Bernhard Walle31bad922008-08-12 15:09:14 -0700321 * @type: Type of the memory range.
322 *
323 * Adds a firmware mapping entry. This function uses the bootmem allocator
akpm@linux-foundation.orgd96ae532010-03-05 13:41:58 -0800324 * for memory allocation.
Bernhard Walle31bad922008-08-12 15:09:14 -0700325 *
326 * That function must be called before late_initcall.
327 *
328 * Returns 0 on success, or -ENOMEM if no memory could be allocated.
329 **/
Yinghai Lu3b0fde02009-06-16 15:31:16 -0700330int __init firmware_map_add_early(u64 start, u64 end, const char *type)
Bernhard Walle69ac9cd2008-06-27 13:12:54 +0200331{
332 struct firmware_map_entry *entry;
333
Santosh Shilimkar4fc0bc52014-01-21 15:50:45 -0800334 entry = memblock_virt_alloc(sizeof(struct firmware_map_entry), 0);
Bernhard Walle31bad922008-08-12 15:09:14 -0700335 if (WARN_ON(!entry))
Bernhard Walle69ac9cd2008-06-27 13:12:54 +0200336 return -ENOMEM;
337
338 return firmware_map_add_entry(start, end, type, entry);
339}
340
Yasuaki Ishimatsu46c66c42013-02-22 16:32:56 -0800341/**
342 * firmware_map_remove() - remove a firmware mapping entry
343 * @start: Start of the memory range.
344 * @end: End of the memory range.
345 * @type: Type of the memory range.
346 *
347 * removes a firmware mapping entry.
348 *
349 * Returns 0 on success, or -EINVAL if no entry.
350 **/
351int __meminit firmware_map_remove(u64 start, u64 end, const char *type)
352{
353 struct firmware_map_entry *entry;
354
355 spin_lock(&map_entries_lock);
356 entry = firmware_map_find_entry(start, end - 1, type);
357 if (!entry) {
358 spin_unlock(&map_entries_lock);
359 return -EINVAL;
360 }
361
362 firmware_map_remove_entry(entry);
363 spin_unlock(&map_entries_lock);
364
365 /* remove the memmap entry */
366 remove_sysfs_fw_map_entry(entry);
367
368 return 0;
369}
370
Bernhard Walle69ac9cd2008-06-27 13:12:54 +0200371/*
372 * Sysfs functions -------------------------------------------------------------
373 */
374
375static ssize_t start_show(struct firmware_map_entry *entry, char *buf)
376{
Randy Dunlap78681ac2008-07-26 15:22:28 -0700377 return snprintf(buf, PAGE_SIZE, "0x%llx\n",
378 (unsigned long long)entry->start);
Bernhard Walle69ac9cd2008-06-27 13:12:54 +0200379}
380
381static ssize_t end_show(struct firmware_map_entry *entry, char *buf)
382{
Randy Dunlap78681ac2008-07-26 15:22:28 -0700383 return snprintf(buf, PAGE_SIZE, "0x%llx\n",
384 (unsigned long long)entry->end);
Bernhard Walle69ac9cd2008-06-27 13:12:54 +0200385}
386
387static ssize_t type_show(struct firmware_map_entry *entry, char *buf)
388{
389 return snprintf(buf, PAGE_SIZE, "%s\n", entry->type);
390}
391
Yasuaki Ishimatsu46c66c42013-02-22 16:32:56 -0800392static inline struct memmap_attribute *to_memmap_attr(struct attribute *attr)
393{
394 return container_of(attr, struct memmap_attribute, attr);
395}
Bernhard Walle69ac9cd2008-06-27 13:12:54 +0200396
397static ssize_t memmap_attr_show(struct kobject *kobj,
398 struct attribute *attr, char *buf)
399{
400 struct firmware_map_entry *entry = to_memmap_entry(kobj);
401 struct memmap_attribute *memmap_attr = to_memmap_attr(attr);
402
403 return memmap_attr->show(entry, buf);
404}
405
406/*
407 * Initialises stuff and adds the entries in the map_entries list to
408 * sysfs. Important is that firmware_map_add() and firmware_map_add_early()
Bernhard Walle31bad922008-08-12 15:09:14 -0700409 * must be called before late_initcall. That's just because that function
410 * is called as late_initcall() function, which means that if you call
411 * firmware_map_add() or firmware_map_add_early() afterwards, the entries
412 * are not added to sysfs.
Bernhard Walle69ac9cd2008-06-27 13:12:54 +0200413 */
Fengguang Wubac71692012-10-19 13:56:55 -0700414static int __init firmware_memmap_init(void)
Bernhard Walle69ac9cd2008-06-27 13:12:54 +0200415{
Bernhard Walle69ac9cd2008-06-27 13:12:54 +0200416 struct firmware_map_entry *entry;
Bernhard Walle69ac9cd2008-06-27 13:12:54 +0200417
akpm@linux-foundation.orgd96ae532010-03-05 13:41:58 -0800418 list_for_each_entry(entry, &map_entries, list)
419 add_sysfs_fw_map_entry(entry);
Bernhard Walle69ac9cd2008-06-27 13:12:54 +0200420
421 return 0;
422}
Fengguang Wubac71692012-10-19 13:56:55 -0700423late_initcall(firmware_memmap_init);
Bernhard Walle69ac9cd2008-06-27 13:12:54 +0200424