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