Bernhard Walle | 69ac9cd | 2008-06-27 13:12:54 +0200 | [diff] [blame] | 1 | /* |
| 2 | * linux/drivers/firmware/memmap.c |
| 3 | * Copyright (C) 2008 SUSE LINUX Products GmbH |
Bernhard Walle | 97bef7d | 2009-02-18 14:48:40 -0800 | [diff] [blame^] | 4 | * by Bernhard Walle <bernhard.walle@gmx.de> |
Bernhard Walle | 69ac9cd | 2008-06-27 13:12:54 +0200 | [diff] [blame] | 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> |
| 23 | |
| 24 | /* |
| 25 | * Data types ------------------------------------------------------------------ |
| 26 | */ |
| 27 | |
| 28 | /* |
| 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. |
| 32 | */ |
| 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 */ |
| 39 | }; |
| 40 | |
| 41 | /* |
| 42 | * Forward declarations -------------------------------------------------------- |
| 43 | */ |
| 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); |
| 49 | |
| 50 | /* |
| 51 | * Static data ----------------------------------------------------------------- |
| 52 | */ |
| 53 | |
| 54 | struct memmap_attribute { |
| 55 | struct attribute attr; |
| 56 | ssize_t (*show)(struct firmware_map_entry *entry, char *buf); |
| 57 | }; |
| 58 | |
Roel Kluin | da2bdf9 | 2009-01-07 18:09:15 -0800 | [diff] [blame] | 59 | static struct memmap_attribute memmap_start_attr = __ATTR_RO(start); |
| 60 | static struct memmap_attribute memmap_end_attr = __ATTR_RO(end); |
| 61 | static struct memmap_attribute memmap_type_attr = __ATTR_RO(type); |
Bernhard Walle | 69ac9cd | 2008-06-27 13:12:54 +0200 | [diff] [blame] | 62 | |
| 63 | /* |
| 64 | * These are default attributes that are added for every memmap entry. |
| 65 | */ |
| 66 | static struct attribute *def_attrs[] = { |
| 67 | &memmap_start_attr.attr, |
| 68 | &memmap_end_attr.attr, |
| 69 | &memmap_type_attr.attr, |
| 70 | NULL |
| 71 | }; |
| 72 | |
| 73 | static struct sysfs_ops memmap_attr_ops = { |
| 74 | .show = memmap_attr_show, |
| 75 | }; |
| 76 | |
| 77 | static struct kobj_type memmap_ktype = { |
| 78 | .sysfs_ops = &memmap_attr_ops, |
| 79 | .default_attrs = def_attrs, |
| 80 | }; |
| 81 | |
| 82 | /* |
| 83 | * Registration functions ------------------------------------------------------ |
| 84 | */ |
| 85 | |
| 86 | /* |
Bernhard Walle | 31bad92 | 2008-08-12 15:09:14 -0700 | [diff] [blame] | 87 | * Firmware memory map entries. No locking is needed because the |
| 88 | * firmware_map_add() and firmware_map_add_early() functions are called |
| 89 | * in firmware initialisation code in one single thread of execution. |
Bernhard Walle | 69ac9cd | 2008-06-27 13:12:54 +0200 | [diff] [blame] | 90 | */ |
| 91 | static LIST_HEAD(map_entries); |
| 92 | |
| 93 | /** |
Bernhard Walle | 31bad92 | 2008-08-12 15:09:14 -0700 | [diff] [blame] | 94 | * firmware_map_add_entry() - Does the real work to add a firmware memmap entry. |
Bernhard Walle | 69ac9cd | 2008-06-27 13:12:54 +0200 | [diff] [blame] | 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 |
| 99 | * entry. |
Bernhard Walle | 31bad92 | 2008-08-12 15:09:14 -0700 | [diff] [blame] | 100 | * |
| 101 | * Common implementation of firmware_map_add() and firmware_map_add_early() |
| 102 | * which expects a pre-allocated struct firmware_map_entry. |
| 103 | **/ |
Bernhard Walle | 69ac9cd | 2008-06-27 13:12:54 +0200 | [diff] [blame] | 104 | static int firmware_map_add_entry(resource_size_t start, resource_size_t end, |
| 105 | const char *type, |
| 106 | struct firmware_map_entry *entry) |
| 107 | { |
| 108 | BUG_ON(start > end); |
| 109 | |
| 110 | entry->start = start; |
| 111 | entry->end = end; |
| 112 | entry->type = type; |
| 113 | INIT_LIST_HEAD(&entry->list); |
| 114 | kobject_init(&entry->kobj, &memmap_ktype); |
| 115 | |
| 116 | list_add_tail(&entry->list, &map_entries); |
| 117 | |
| 118 | return 0; |
| 119 | } |
| 120 | |
Bernhard Walle | 31bad92 | 2008-08-12 15:09:14 -0700 | [diff] [blame] | 121 | /** |
| 122 | * firmware_map_add() - Adds a firmware mapping entry. |
| 123 | * @start: Start of the memory range. |
| 124 | * @end: End of the memory range (inclusive). |
| 125 | * @type: Type of the memory range. |
| 126 | * |
| 127 | * This function uses kmalloc() for memory |
| 128 | * allocation. Use firmware_map_add_early() if you want to use the bootmem |
| 129 | * allocator. |
| 130 | * |
| 131 | * That function must be called before late_initcall. |
| 132 | * |
| 133 | * Returns 0 on success, or -ENOMEM if no memory could be allocated. |
| 134 | **/ |
Bernhard Walle | 69ac9cd | 2008-06-27 13:12:54 +0200 | [diff] [blame] | 135 | int firmware_map_add(resource_size_t start, resource_size_t end, |
| 136 | const char *type) |
| 137 | { |
| 138 | struct firmware_map_entry *entry; |
| 139 | |
| 140 | entry = kmalloc(sizeof(struct firmware_map_entry), GFP_ATOMIC); |
Bernhard Walle | 69ac9cd | 2008-06-27 13:12:54 +0200 | [diff] [blame] | 141 | if (!entry) |
| 142 | return -ENOMEM; |
| 143 | |
| 144 | return firmware_map_add_entry(start, end, type, entry); |
| 145 | } |
| 146 | |
Bernhard Walle | 31bad92 | 2008-08-12 15:09:14 -0700 | [diff] [blame] | 147 | /** |
| 148 | * firmware_map_add_early() - Adds a firmware mapping entry. |
| 149 | * @start: Start of the memory range. |
| 150 | * @end: End of the memory range (inclusive). |
| 151 | * @type: Type of the memory range. |
| 152 | * |
| 153 | * Adds a firmware mapping entry. This function uses the bootmem allocator |
| 154 | * for memory allocation. Use firmware_map_add() if you want to use kmalloc(). |
| 155 | * |
| 156 | * That function must be called before late_initcall. |
| 157 | * |
| 158 | * Returns 0 on success, or -ENOMEM if no memory could be allocated. |
| 159 | **/ |
Bernhard Walle | 69ac9cd | 2008-06-27 13:12:54 +0200 | [diff] [blame] | 160 | int __init firmware_map_add_early(resource_size_t start, resource_size_t end, |
| 161 | const char *type) |
| 162 | { |
| 163 | struct firmware_map_entry *entry; |
| 164 | |
| 165 | entry = alloc_bootmem_low(sizeof(struct firmware_map_entry)); |
Bernhard Walle | 31bad92 | 2008-08-12 15:09:14 -0700 | [diff] [blame] | 166 | if (WARN_ON(!entry)) |
Bernhard Walle | 69ac9cd | 2008-06-27 13:12:54 +0200 | [diff] [blame] | 167 | return -ENOMEM; |
| 168 | |
| 169 | return firmware_map_add_entry(start, end, type, entry); |
| 170 | } |
| 171 | |
| 172 | /* |
| 173 | * Sysfs functions ------------------------------------------------------------- |
| 174 | */ |
| 175 | |
| 176 | static ssize_t start_show(struct firmware_map_entry *entry, char *buf) |
| 177 | { |
Randy Dunlap | 78681ac | 2008-07-26 15:22:28 -0700 | [diff] [blame] | 178 | return snprintf(buf, PAGE_SIZE, "0x%llx\n", |
| 179 | (unsigned long long)entry->start); |
Bernhard Walle | 69ac9cd | 2008-06-27 13:12:54 +0200 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | static ssize_t end_show(struct firmware_map_entry *entry, char *buf) |
| 183 | { |
Randy Dunlap | 78681ac | 2008-07-26 15:22:28 -0700 | [diff] [blame] | 184 | return snprintf(buf, PAGE_SIZE, "0x%llx\n", |
| 185 | (unsigned long long)entry->end); |
Bernhard Walle | 69ac9cd | 2008-06-27 13:12:54 +0200 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | static ssize_t type_show(struct firmware_map_entry *entry, char *buf) |
| 189 | { |
| 190 | return snprintf(buf, PAGE_SIZE, "%s\n", entry->type); |
| 191 | } |
| 192 | |
| 193 | #define to_memmap_attr(_attr) container_of(_attr, struct memmap_attribute, attr) |
| 194 | #define to_memmap_entry(obj) container_of(obj, struct firmware_map_entry, kobj) |
| 195 | |
| 196 | static ssize_t memmap_attr_show(struct kobject *kobj, |
| 197 | struct attribute *attr, char *buf) |
| 198 | { |
| 199 | struct firmware_map_entry *entry = to_memmap_entry(kobj); |
| 200 | struct memmap_attribute *memmap_attr = to_memmap_attr(attr); |
| 201 | |
| 202 | return memmap_attr->show(entry, buf); |
| 203 | } |
| 204 | |
| 205 | /* |
| 206 | * Initialises stuff and adds the entries in the map_entries list to |
| 207 | * sysfs. Important is that firmware_map_add() and firmware_map_add_early() |
Bernhard Walle | 31bad92 | 2008-08-12 15:09:14 -0700 | [diff] [blame] | 208 | * must be called before late_initcall. That's just because that function |
| 209 | * is called as late_initcall() function, which means that if you call |
| 210 | * firmware_map_add() or firmware_map_add_early() afterwards, the entries |
| 211 | * are not added to sysfs. |
Bernhard Walle | 69ac9cd | 2008-06-27 13:12:54 +0200 | [diff] [blame] | 212 | */ |
| 213 | static int __init memmap_init(void) |
| 214 | { |
| 215 | int i = 0; |
| 216 | struct firmware_map_entry *entry; |
| 217 | struct kset *memmap_kset; |
| 218 | |
| 219 | memmap_kset = kset_create_and_add("memmap", NULL, firmware_kobj); |
Bernhard Walle | 31bad92 | 2008-08-12 15:09:14 -0700 | [diff] [blame] | 220 | if (WARN_ON(!memmap_kset)) |
Bernhard Walle | 69ac9cd | 2008-06-27 13:12:54 +0200 | [diff] [blame] | 221 | return -ENOMEM; |
| 222 | |
| 223 | list_for_each_entry(entry, &map_entries, list) { |
| 224 | entry->kobj.kset = memmap_kset; |
Bernhard Walle | 31bad92 | 2008-08-12 15:09:14 -0700 | [diff] [blame] | 225 | if (kobject_add(&entry->kobj, NULL, "%d", i++)) |
| 226 | kobject_put(&entry->kobj); |
Bernhard Walle | 69ac9cd | 2008-06-27 13:12:54 +0200 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | return 0; |
| 230 | } |
| 231 | late_initcall(memmap_init); |
| 232 | |