1
2
3
4
5
6
7
8
9
10
11
12
13
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
27
28
29
30
31
32
33
34struct firmware_map_entry {
35
36
37
38
39 u64 start;
40 u64 end;
41 const char *type;
42 struct list_head list;
43 struct kobject kobj;
44};
45
46
47
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
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
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
89
90
91
92
93
94
95
96static LIST_HEAD(map_entries);
97
98
99
100
101
102
103
104
105
106
107
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 - 1;
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
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
149
150
151
152
153
154
155
156
157
158
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
170 add_sysfs_fw_map_entry(entry);
171
172 return 0;
173}
174
175
176
177
178
179
180
181
182
183
184
185
186
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
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
234
235
236
237
238
239
240static int __init firmware_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(firmware_memmap_init);
250
251