2 * linux/drivers/firmware/memmap.c
3 * Copyright (C) 2008 SUSE LINUX Products GmbH
4 * by Bernhard Walle <bwalle@suse.de>
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
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.
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>
25 * Data types ------------------------------------------------------------------
29 * Firmware map entry. Because firmware memory maps are flat and not
30 * hierarchical, it's ok to organise them in a linked list. No parent
31 * information is necessary as for the resource tree.
33 struct firmware_map_entry
{
34 resource_size_t start
; /* start of the memory range */
35 resource_size_t end
; /* end of the memory range (incl.) */
36 const char *type
; /* type of the memory range */
37 struct list_head list
; /* entry for the linked list */
38 struct kobject kobj
; /* kobject for each entry */
42 * Forward declarations --------------------------------------------------------
44 static ssize_t
memmap_attr_show(struct kobject
*kobj
,
45 struct attribute
*attr
, char *buf
);
46 static ssize_t
start_show(struct firmware_map_entry
*entry
, char *buf
);
47 static ssize_t
end_show(struct firmware_map_entry
*entry
, char *buf
);
48 static ssize_t
type_show(struct firmware_map_entry
*entry
, char *buf
);
51 * Static data -----------------------------------------------------------------
54 struct memmap_attribute
{
55 struct attribute attr
;
56 ssize_t (*show
)(struct firmware_map_entry
*entry
, char *buf
);
59 struct memmap_attribute memmap_start_attr
= __ATTR_RO(start
);
60 struct memmap_attribute memmap_end_attr
= __ATTR_RO(end
);
61 struct memmap_attribute memmap_type_attr
= __ATTR_RO(type
);
64 * These are default attributes that are added for every memmap entry.
66 static struct attribute
*def_attrs
[] = {
67 &memmap_start_attr
.attr
,
68 &memmap_end_attr
.attr
,
69 &memmap_type_attr
.attr
,
73 static struct sysfs_ops memmap_attr_ops
= {
74 .show
= memmap_attr_show
,
77 static struct kobj_type memmap_ktype
= {
78 .sysfs_ops
= &memmap_attr_ops
,
79 .default_attrs
= def_attrs
,
83 * Registration functions ------------------------------------------------------
87 * Firmware memory map entries
89 static LIST_HEAD(map_entries
);
92 * Common implementation of firmware_map_add() and firmware_map_add_early()
93 * which expects a pre-allocated struct firmware_map_entry.
95 * @start: Start of the memory range.
96 * @end: End of the memory range (inclusive).
97 * @type: Type of the memory range.
98 * @entry: Pre-allocated (either kmalloc() or bootmem allocator), uninitialised
101 static int firmware_map_add_entry(resource_size_t start
, resource_size_t end
,
103 struct firmware_map_entry
*entry
)
107 entry
->start
= start
;
110 INIT_LIST_HEAD(&entry
->list
);
111 kobject_init(&entry
->kobj
, &memmap_ktype
);
113 list_add_tail(&entry
->list
, &map_entries
);
119 * See <linux/firmware-map.h> for documentation.
121 int firmware_map_add(resource_size_t start
, resource_size_t end
,
124 struct firmware_map_entry
*entry
;
126 entry
= kmalloc(sizeof(struct firmware_map_entry
), GFP_ATOMIC
);
131 return firmware_map_add_entry(start
, end
, type
, entry
);
135 * See <linux/firmware-map.h> for documentation.
137 int __init
firmware_map_add_early(resource_size_t start
, resource_size_t end
,
140 struct firmware_map_entry
*entry
;
142 entry
= alloc_bootmem_low(sizeof(struct firmware_map_entry
));
147 return firmware_map_add_entry(start
, end
, type
, entry
);
151 * Sysfs functions -------------------------------------------------------------
154 static ssize_t
start_show(struct firmware_map_entry
*entry
, char *buf
)
156 return snprintf(buf
, PAGE_SIZE
, "0x%llx\n",
157 (unsigned long long)entry
->start
);
160 static ssize_t
end_show(struct firmware_map_entry
*entry
, char *buf
)
162 return snprintf(buf
, PAGE_SIZE
, "0x%llx\n",
163 (unsigned long long)entry
->end
);
166 static ssize_t
type_show(struct firmware_map_entry
*entry
, char *buf
)
168 return snprintf(buf
, PAGE_SIZE
, "%s\n", entry
->type
);
171 #define to_memmap_attr(_attr) container_of(_attr, struct memmap_attribute, attr)
172 #define to_memmap_entry(obj) container_of(obj, struct firmware_map_entry, kobj)
174 static ssize_t
memmap_attr_show(struct kobject
*kobj
,
175 struct attribute
*attr
, char *buf
)
177 struct firmware_map_entry
*entry
= to_memmap_entry(kobj
);
178 struct memmap_attribute
*memmap_attr
= to_memmap_attr(attr
);
180 return memmap_attr
->show(entry
, buf
);
184 * Initialises stuff and adds the entries in the map_entries list to
185 * sysfs. Important is that firmware_map_add() and firmware_map_add_early()
186 * must be called before late_initcall.
188 static int __init
memmap_init(void)
191 struct firmware_map_entry
*entry
;
192 struct kset
*memmap_kset
;
194 memmap_kset
= kset_create_and_add("memmap", NULL
, firmware_kobj
);
195 WARN_ON(!memmap_kset
);
199 list_for_each_entry(entry
, &map_entries
, list
) {
200 entry
->kobj
.kset
= memmap_kset
;
201 kobject_add(&entry
->kobj
, NULL
, "%d", i
++);
206 late_initcall(memmap_init
);