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