ALSA: sound/usb: fix UAC1 regression
[firewire-audio.git] / drivers / firmware / memmap.c
blobd59f7cad2269d817d21e8eebb3044a97c0b8435b
1 /*
2 * linux/drivers/firmware/memmap.c
3 * Copyright (C) 2008 SUSE LINUX Products GmbH
4 * by Bernhard Walle <bernhard.walle@gmx.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 {
35 * start and end must be u64 rather than resource_size_t, because e820
36 * resources can lie at addresses above 4G.
38 u64 start; /* start of the memory range */
39 u64 end; /* end of the memory range (incl.) */
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 */
46 * Forward declarations --------------------------------------------------------
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);
55 * Static data -----------------------------------------------------------------
58 struct memmap_attribute {
59 struct attribute attr;
60 ssize_t (*show)(struct firmware_map_entry *entry, char *buf);
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);
68 * These are default attributes that are added for every memmap entry.
70 static struct attribute *def_attrs[] = {
71 &memmap_start_attr.attr,
72 &memmap_end_attr.attr,
73 &memmap_type_attr.attr,
74 NULL
77 static const struct sysfs_ops memmap_attr_ops = {
78 .show = memmap_attr_show,
81 static struct kobj_type memmap_ktype = {
82 .sysfs_ops = &memmap_attr_ops,
83 .default_attrs = def_attrs,
87 * Registration functions ------------------------------------------------------
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.
95 static LIST_HEAD(map_entries);
97 /**
98 * firmware_map_add_entry() - Does the real work to add a firmware memmap entry.
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.
105 * Common implementation of firmware_map_add() and firmware_map_add_early()
106 * which expects a pre-allocated struct firmware_map_entry.
108 static int firmware_map_add_entry(u64 start, u64 end,
109 const char *type,
110 struct firmware_map_entry *entry)
112 BUG_ON(start > end);
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);
120 list_add_tail(&entry->list, &map_entries);
122 return 0;
126 * Add memmap entry on sysfs
128 static int add_sysfs_fw_map_entry(struct firmware_map_entry *entry)
130 static int map_entries_nr;
131 static struct kset *mmap_kset;
133 if (!mmap_kset) {
134 mmap_kset = kset_create_and_add("memmap", NULL, firmware_kobj);
135 if (!mmap_kset)
136 return -ENOMEM;
139 entry->kobj.kset = mmap_kset;
140 if (kobject_add(&entry->kobj, NULL, "%d", map_entries_nr++))
141 kobject_put(&entry->kobj);
143 return 0;
147 * firmware_map_add_hotplug() - Adds a firmware mapping entry when we do
148 * memory hotplug.
149 * @start: Start of the memory range.
150 * @end: End of the memory range (inclusive).
151 * @type: Type of the memory range.
153 * Adds a firmware mapping entry. This function is for memory hotplug, it is
154 * similar to function firmware_map_add_early(). The only difference is that
155 * it will create the syfs entry dynamically.
157 * Returns 0 on success, or -ENOMEM if no memory could be allocated.
159 int __meminit firmware_map_add_hotplug(u64 start, u64 end, const char *type)
161 struct firmware_map_entry *entry;
163 entry = kzalloc(sizeof(struct firmware_map_entry), GFP_ATOMIC);
164 if (!entry)
165 return -ENOMEM;
167 firmware_map_add_entry(start, end, type, entry);
168 /* create the memmap entry */
169 add_sysfs_fw_map_entry(entry);
171 return 0;
175 * firmware_map_add_early() - Adds a firmware mapping entry.
176 * @start: Start of the memory range.
177 * @end: End of the memory range (inclusive).
178 * @type: Type of the memory range.
180 * Adds a firmware mapping entry. This function uses the bootmem allocator
181 * for memory allocation.
183 * That function must be called before late_initcall.
185 * Returns 0 on success, or -ENOMEM if no memory could be allocated.
187 int __init firmware_map_add_early(u64 start, u64 end, const char *type)
189 struct firmware_map_entry *entry;
191 entry = alloc_bootmem(sizeof(struct firmware_map_entry));
192 if (WARN_ON(!entry))
193 return -ENOMEM;
195 return firmware_map_add_entry(start, end, type, entry);
199 * Sysfs functions -------------------------------------------------------------
202 static ssize_t start_show(struct firmware_map_entry *entry, char *buf)
204 return snprintf(buf, PAGE_SIZE, "0x%llx\n",
205 (unsigned long long)entry->start);
208 static ssize_t end_show(struct firmware_map_entry *entry, char *buf)
210 return snprintf(buf, PAGE_SIZE, "0x%llx\n",
211 (unsigned long long)entry->end);
214 static ssize_t type_show(struct firmware_map_entry *entry, char *buf)
216 return snprintf(buf, PAGE_SIZE, "%s\n", entry->type);
219 #define to_memmap_attr(_attr) container_of(_attr, struct memmap_attribute, attr)
220 #define to_memmap_entry(obj) container_of(obj, struct firmware_map_entry, kobj)
222 static ssize_t memmap_attr_show(struct kobject *kobj,
223 struct attribute *attr, char *buf)
225 struct firmware_map_entry *entry = to_memmap_entry(kobj);
226 struct memmap_attribute *memmap_attr = to_memmap_attr(attr);
228 return memmap_attr->show(entry, buf);
232 * Initialises stuff and adds the entries in the map_entries list to
233 * sysfs. Important is that firmware_map_add() and firmware_map_add_early()
234 * must be called before late_initcall. That's just because that function
235 * is called as late_initcall() function, which means that if you call
236 * firmware_map_add() or firmware_map_add_early() afterwards, the entries
237 * are not added to sysfs.
239 static int __init memmap_init(void)
241 struct firmware_map_entry *entry;
243 list_for_each_entry(entry, &map_entries, list)
244 add_sysfs_fw_map_entry(entry);
246 return 0;
248 late_initcall(memmap_init);