1
2
3
4
5
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
19
20
21
22
23
24
25
26struct firmware_map_entry {
27
28
29
30
31 u64 start;
32 u64 end;
33 const char *type;
34 struct list_head list;
35 struct kobject kobj;
36};
37
38
39
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
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
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
78static LIST_HEAD(map_entries);
79static DEFINE_SPINLOCK(map_entries_lock);
80
81
82
83
84
85
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
104
105
106
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
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
162
163
164
165
166
167static inline void firmware_map_remove_entry(struct firmware_map_entry *entry)
168{
169 list_del(&entry->list);
170}
171
172
173
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
198
199static inline void remove_sysfs_fw_map_entry(struct firmware_map_entry *entry)
200{
201 kobject_put(&entry->kobj);
202}
203
204
205
206
207
208
209
210
211
212
213
214
215
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
234
235
236
237
238
239
240
241
242
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
252
253
254
255
256
257
258
259
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
270
271
272
273
274
275
276
277
278
279
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
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
305 add_sysfs_fw_map_entry(entry);
306
307 return 0;
308}
309
310
311
312
313
314
315
316
317
318
319
320
321
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
337
338
339
340
341
342
343
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
360 remove_sysfs_fw_map_entry(entry);
361
362 return 0;
363}
364
365
366
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
402
403
404
405
406
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
419