Commit | Line | Data |
---|---|---|
69ac9cd6 BW |
1 | /* |
2 | * linux/drivers/firmware/memmap.c | |
3 | * Copyright (C) 2008 SUSE LINUX Products GmbH | |
97bef7dd | 4 | * by Bernhard Walle <bernhard.walle@gmx.de> |
69ac9cd6 BW |
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> | |
5a0e3ad6 | 23 | #include <linux/slab.h> |
46c66c4b | 24 | #include <linux/mm.h> |
69ac9cd6 BW |
25 | |
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 | */ | |
35 | struct firmware_map_entry { | |
3b0fde0f YL |
36 | /* |
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.) */ | |
69ac9cd6 BW |
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 */ | |
45 | }; | |
46 | ||
47 | /* | |
48 | * Forward declarations -------------------------------------------------------- | |
49 | */ | |
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); | |
55 | ||
46c66c4b YI |
56 | static struct firmware_map_entry * __meminit |
57 | firmware_map_find_entry(u64 start, u64 end, const char *type); | |
58 | ||
69ac9cd6 BW |
59 | /* |
60 | * Static data ----------------------------------------------------------------- | |
61 | */ | |
62 | ||
63 | struct memmap_attribute { | |
64 | struct attribute attr; | |
65 | ssize_t (*show)(struct firmware_map_entry *entry, char *buf); | |
66 | }; | |
67 | ||
da2bdf9a RK |
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); | |
69ac9cd6 BW |
71 | |
72 | /* | |
73 | * These are default attributes that are added for every memmap entry. | |
74 | */ | |
75 | static struct attribute *def_attrs[] = { | |
76 | &memmap_start_attr.attr, | |
77 | &memmap_end_attr.attr, | |
78 | &memmap_type_attr.attr, | |
79 | NULL | |
80 | }; | |
81 | ||
52cf25d0 | 82 | static const struct sysfs_ops memmap_attr_ops = { |
69ac9cd6 BW |
83 | .show = memmap_attr_show, |
84 | }; | |
85 | ||
46c66c4b YI |
86 | /* Firmware memory map entries. */ |
87 | static LIST_HEAD(map_entries); | |
88 | static 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 | */ | |
96 | static LIST_HEAD(map_entries_bootmem); | |
97 | static DEFINE_SPINLOCK(map_entries_bootmem_lock); | |
98 | ||
99 | ||
100 | static inline struct firmware_map_entry * | |
101 | to_memmap_entry(struct kobject *kobj) | |
102 | { | |
103 | return container_of(kobj, struct firmware_map_entry, kobj); | |
104 | } | |
105 | ||
106 | static 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 | */ | |
117 | if (firmware_map_find_entry(entry->start, entry->end, | |
118 | entry->type)) { | |
119 | spin_lock(&map_entries_bootmem_lock); | |
120 | list_add(&entry->list, &map_entries_bootmem); | |
121 | spin_unlock(&map_entries_bootmem_lock); | |
122 | } | |
123 | ||
124 | return; | |
125 | } | |
126 | ||
127 | kfree(entry); | |
128 | } | |
129 | ||
130 | static struct kobj_type __refdata memmap_ktype = { | |
131 | .release = release_firmware_map_entry, | |
69ac9cd6 BW |
132 | .sysfs_ops = &memmap_attr_ops, |
133 | .default_attrs = def_attrs, | |
134 | }; | |
135 | ||
136 | /* | |
137 | * Registration functions ------------------------------------------------------ | |
138 | */ | |
139 | ||
69ac9cd6 | 140 | /** |
31bad924 | 141 | * firmware_map_add_entry() - Does the real work to add a firmware memmap entry. |
69ac9cd6 | 142 | * @start: Start of the memory range. |
4ed940d4 | 143 | * @end: End of the memory range (exclusive). |
69ac9cd6 BW |
144 | * @type: Type of the memory range. |
145 | * @entry: Pre-allocated (either kmalloc() or bootmem allocator), uninitialised | |
146 | * entry. | |
31bad924 BW |
147 | * |
148 | * Common implementation of firmware_map_add() and firmware_map_add_early() | |
149 | * which expects a pre-allocated struct firmware_map_entry. | |
150 | **/ | |
3b0fde0f | 151 | static int firmware_map_add_entry(u64 start, u64 end, |
69ac9cd6 BW |
152 | const char *type, |
153 | struct firmware_map_entry *entry) | |
154 | { | |
155 | BUG_ON(start > end); | |
156 | ||
157 | entry->start = start; | |
4ed940d4 | 158 | entry->end = end - 1; |
69ac9cd6 BW |
159 | entry->type = type; |
160 | INIT_LIST_HEAD(&entry->list); | |
161 | kobject_init(&entry->kobj, &memmap_ktype); | |
162 | ||
46c66c4b | 163 | spin_lock(&map_entries_lock); |
69ac9cd6 | 164 | list_add_tail(&entry->list, &map_entries); |
46c66c4b | 165 | spin_unlock(&map_entries_lock); |
69ac9cd6 BW |
166 | |
167 | return 0; | |
168 | } | |
169 | ||
46c66c4b YI |
170 | /** |
171 | * firmware_map_remove_entry() - Does the real work to remove a firmware | |
172 | * memmap entry. | |
173 | * @entry: removed entry. | |
174 | * | |
175 | * The caller must hold map_entries_lock, and release it properly. | |
176 | **/ | |
177 | static inline void firmware_map_remove_entry(struct firmware_map_entry *entry) | |
178 | { | |
179 | list_del(&entry->list); | |
180 | } | |
181 | ||
d96ae530 | 182 | /* |
183 | * Add memmap entry on sysfs | |
184 | */ | |
185 | static int add_sysfs_fw_map_entry(struct firmware_map_entry *entry) | |
186 | { | |
187 | static int map_entries_nr; | |
188 | static struct kset *mmap_kset; | |
189 | ||
190 | 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 | ||
46c66c4b YI |
203 | /* |
204 | * Remove memmap entry on sysfs | |
205 | */ | |
206 | static 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 | */ | |
224 | static struct firmware_map_entry * __meminit | |
225 | firmware_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 | */ | |
251 | static struct firmware_map_entry * __meminit | |
252 | firmware_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 | */ | |
268 | static struct firmware_map_entry * __meminit | |
269 | firmware_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 | ||
31bad924 | 275 | /** |
d96ae530 | 276 | * firmware_map_add_hotplug() - Adds a firmware mapping entry when we do |
277 | * memory hotplug. | |
31bad924 | 278 | * @start: Start of the memory range. |
4ed940d4 | 279 | * @end: End of the memory range (exclusive) |
31bad924 BW |
280 | * @type: Type of the memory range. |
281 | * | |
d96ae530 | 282 | * 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. | |
31bad924 BW |
285 | * |
286 | * Returns 0 on success, or -ENOMEM if no memory could be allocated. | |
287 | **/ | |
d96ae530 | 288 | int __meminit firmware_map_add_hotplug(u64 start, u64 end, const char *type) |
69ac9cd6 BW |
289 | { |
290 | struct firmware_map_entry *entry; | |
291 | ||
46c66c4b YI |
292 | entry = firmware_map_find_entry_bootmem(start, end, type); |
293 | if (!entry) { | |
294 | entry = kzalloc(sizeof(struct firmware_map_entry), GFP_ATOMIC); | |
295 | if (!entry) | |
296 | return -ENOMEM; | |
297 | } else { | |
298 | /* Reuse storage allocated by bootmem. */ | |
299 | spin_lock(&map_entries_bootmem_lock); | |
300 | list_del(&entry->list); | |
301 | spin_unlock(&map_entries_bootmem_lock); | |
302 | ||
303 | memset(entry, 0, sizeof(*entry)); | |
304 | } | |
69ac9cd6 | 305 | |
d96ae530 | 306 | firmware_map_add_entry(start, end, type, entry); |
307 | /* create the memmap entry */ | |
308 | add_sysfs_fw_map_entry(entry); | |
309 | ||
310 | return 0; | |
69ac9cd6 BW |
311 | } |
312 | ||
31bad924 BW |
313 | /** |
314 | * firmware_map_add_early() - Adds a firmware mapping entry. | |
315 | * @start: Start of the memory range. | |
4ed940d4 | 316 | * @end: End of the memory range. |
31bad924 BW |
317 | * @type: Type of the memory range. |
318 | * | |
319 | * Adds a firmware mapping entry. This function uses the bootmem allocator | |
d96ae530 | 320 | * for memory allocation. |
31bad924 BW |
321 | * |
322 | * That function must be called before late_initcall. | |
323 | * | |
324 | * Returns 0 on success, or -ENOMEM if no memory could be allocated. | |
325 | **/ | |
3b0fde0f | 326 | int __init firmware_map_add_early(u64 start, u64 end, const char *type) |
69ac9cd6 BW |
327 | { |
328 | struct firmware_map_entry *entry; | |
329 | ||
3c1596ef | 330 | entry = alloc_bootmem(sizeof(struct firmware_map_entry)); |
31bad924 | 331 | if (WARN_ON(!entry)) |
69ac9cd6 BW |
332 | return -ENOMEM; |
333 | ||
334 | return firmware_map_add_entry(start, end, type, entry); | |
335 | } | |
336 | ||
46c66c4b YI |
337 | /** |
338 | * firmware_map_remove() - remove a firmware mapping entry | |
339 | * @start: Start of the memory range. | |
340 | * @end: End of the memory range. | |
341 | * @type: Type of the memory range. | |
342 | * | |
343 | * removes a firmware mapping entry. | |
344 | * | |
345 | * Returns 0 on success, or -EINVAL if no entry. | |
346 | **/ | |
347 | int __meminit firmware_map_remove(u64 start, u64 end, const char *type) | |
348 | { | |
349 | struct firmware_map_entry *entry; | |
350 | ||
351 | spin_lock(&map_entries_lock); | |
352 | entry = firmware_map_find_entry(start, end - 1, type); | |
353 | if (!entry) { | |
354 | spin_unlock(&map_entries_lock); | |
355 | return -EINVAL; | |
356 | } | |
357 | ||
358 | firmware_map_remove_entry(entry); | |
359 | spin_unlock(&map_entries_lock); | |
360 | ||
361 | /* remove the memmap entry */ | |
362 | remove_sysfs_fw_map_entry(entry); | |
363 | ||
364 | return 0; | |
365 | } | |
366 | ||
69ac9cd6 BW |
367 | /* |
368 | * Sysfs functions ------------------------------------------------------------- | |
369 | */ | |
370 | ||
371 | static ssize_t start_show(struct firmware_map_entry *entry, char *buf) | |
372 | { | |
78681ac0 RD |
373 | return snprintf(buf, PAGE_SIZE, "0x%llx\n", |
374 | (unsigned long long)entry->start); | |
69ac9cd6 BW |
375 | } |
376 | ||
377 | static ssize_t end_show(struct firmware_map_entry *entry, char *buf) | |
378 | { | |
78681ac0 RD |
379 | return snprintf(buf, PAGE_SIZE, "0x%llx\n", |
380 | (unsigned long long)entry->end); | |
69ac9cd6 BW |
381 | } |
382 | ||
383 | static ssize_t type_show(struct firmware_map_entry *entry, char *buf) | |
384 | { | |
385 | return snprintf(buf, PAGE_SIZE, "%s\n", entry->type); | |
386 | } | |
387 | ||
46c66c4b YI |
388 | static inline struct memmap_attribute *to_memmap_attr(struct attribute *attr) |
389 | { | |
390 | return container_of(attr, struct memmap_attribute, attr); | |
391 | } | |
69ac9cd6 BW |
392 | |
393 | static ssize_t memmap_attr_show(struct kobject *kobj, | |
394 | struct attribute *attr, char *buf) | |
395 | { | |
396 | struct firmware_map_entry *entry = to_memmap_entry(kobj); | |
397 | struct memmap_attribute *memmap_attr = to_memmap_attr(attr); | |
398 | ||
399 | return memmap_attr->show(entry, buf); | |
400 | } | |
401 | ||
402 | /* | |
403 | * Initialises stuff and adds the entries in the map_entries list to | |
404 | * sysfs. Important is that firmware_map_add() and firmware_map_add_early() | |
31bad924 BW |
405 | * must be called before late_initcall. That's just because that function |
406 | * is called as late_initcall() function, which means that if you call | |
407 | * firmware_map_add() or firmware_map_add_early() afterwards, the entries | |
408 | * are not added to sysfs. | |
69ac9cd6 | 409 | */ |
bac71696 | 410 | static int __init firmware_memmap_init(void) |
69ac9cd6 | 411 | { |
69ac9cd6 | 412 | struct firmware_map_entry *entry; |
69ac9cd6 | 413 | |
d96ae530 | 414 | list_for_each_entry(entry, &map_entries, list) |
415 | add_sysfs_fw_map_entry(entry); | |
69ac9cd6 BW |
416 | |
417 | return 0; | |
418 | } | |
bac71696 | 419 | late_initcall(firmware_memmap_init); |
69ac9cd6 | 420 |