Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris...
[linux/fpc-iii.git] / drivers / firmware / efi / runtime-map.c
blob97cdd16a2169a17e741323cca3adc634c26c966e
1 /*
2 * linux/drivers/efi/runtime-map.c
3 * Copyright (C) 2013 Red Hat, Inc., Dave Young <dyoung@redhat.com>
5 * This file is released under the GPLv2.
6 */
8 #include <linux/string.h>
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/types.h>
12 #include <linux/efi.h>
13 #include <linux/slab.h>
15 #include <asm/setup.h>
17 static void *efi_runtime_map;
18 static int nr_efi_runtime_map;
19 static u32 efi_memdesc_size;
21 struct efi_runtime_map_entry {
22 efi_memory_desc_t md;
23 struct kobject kobj; /* kobject for each entry */
26 static struct efi_runtime_map_entry **map_entries;
28 struct map_attribute {
29 struct attribute attr;
30 ssize_t (*show)(struct efi_runtime_map_entry *entry, char *buf);
33 static inline struct map_attribute *to_map_attr(struct attribute *attr)
35 return container_of(attr, struct map_attribute, attr);
38 static ssize_t type_show(struct efi_runtime_map_entry *entry, char *buf)
40 return snprintf(buf, PAGE_SIZE, "0x%x\n", entry->md.type);
43 #define EFI_RUNTIME_FIELD(var) entry->md.var
45 #define EFI_RUNTIME_U64_ATTR_SHOW(name) \
46 static ssize_t name##_show(struct efi_runtime_map_entry *entry, char *buf) \
47 { \
48 return snprintf(buf, PAGE_SIZE, "0x%llx\n", EFI_RUNTIME_FIELD(name)); \
51 EFI_RUNTIME_U64_ATTR_SHOW(phys_addr);
52 EFI_RUNTIME_U64_ATTR_SHOW(virt_addr);
53 EFI_RUNTIME_U64_ATTR_SHOW(num_pages);
54 EFI_RUNTIME_U64_ATTR_SHOW(attribute);
56 static inline struct efi_runtime_map_entry *to_map_entry(struct kobject *kobj)
58 return container_of(kobj, struct efi_runtime_map_entry, kobj);
61 static ssize_t map_attr_show(struct kobject *kobj, struct attribute *attr,
62 char *buf)
64 struct efi_runtime_map_entry *entry = to_map_entry(kobj);
65 struct map_attribute *map_attr = to_map_attr(attr);
67 return map_attr->show(entry, buf);
70 static struct map_attribute map_type_attr = __ATTR_RO(type);
71 static struct map_attribute map_phys_addr_attr = __ATTR_RO(phys_addr);
72 static struct map_attribute map_virt_addr_attr = __ATTR_RO(virt_addr);
73 static struct map_attribute map_num_pages_attr = __ATTR_RO(num_pages);
74 static struct map_attribute map_attribute_attr = __ATTR_RO(attribute);
77 * These are default attributes that are added for every memmap entry.
79 static struct attribute *def_attrs[] = {
80 &map_type_attr.attr,
81 &map_phys_addr_attr.attr,
82 &map_virt_addr_attr.attr,
83 &map_num_pages_attr.attr,
84 &map_attribute_attr.attr,
85 NULL
88 static const struct sysfs_ops map_attr_ops = {
89 .show = map_attr_show,
92 static void map_release(struct kobject *kobj)
94 struct efi_runtime_map_entry *entry;
96 entry = to_map_entry(kobj);
97 kfree(entry);
100 static struct kobj_type __refdata map_ktype = {
101 .sysfs_ops = &map_attr_ops,
102 .default_attrs = def_attrs,
103 .release = map_release,
106 static struct kset *map_kset;
108 static struct efi_runtime_map_entry *
109 add_sysfs_runtime_map_entry(struct kobject *kobj, int nr)
111 int ret;
112 struct efi_runtime_map_entry *entry;
114 if (!map_kset) {
115 map_kset = kset_create_and_add("runtime-map", NULL, kobj);
116 if (!map_kset)
117 return ERR_PTR(-ENOMEM);
120 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
121 if (!entry) {
122 kset_unregister(map_kset);
123 return entry;
126 memcpy(&entry->md, efi_runtime_map + nr * efi_memdesc_size,
127 sizeof(efi_memory_desc_t));
129 kobject_init(&entry->kobj, &map_ktype);
130 entry->kobj.kset = map_kset;
131 ret = kobject_add(&entry->kobj, NULL, "%d", nr);
132 if (ret) {
133 kobject_put(&entry->kobj);
134 kset_unregister(map_kset);
135 return ERR_PTR(ret);
138 return entry;
141 void efi_runtime_map_setup(void *map, int nr_entries, u32 desc_size)
143 efi_runtime_map = map;
144 nr_efi_runtime_map = nr_entries;
145 efi_memdesc_size = desc_size;
148 int __init efi_runtime_map_init(struct kobject *efi_kobj)
150 int i, j, ret = 0;
151 struct efi_runtime_map_entry *entry;
153 if (!efi_runtime_map)
154 return 0;
156 map_entries = kzalloc(nr_efi_runtime_map * sizeof(entry), GFP_KERNEL);
157 if (!map_entries) {
158 ret = -ENOMEM;
159 goto out;
162 for (i = 0; i < nr_efi_runtime_map; i++) {
163 entry = add_sysfs_runtime_map_entry(efi_kobj, i);
164 if (IS_ERR(entry)) {
165 ret = PTR_ERR(entry);
166 goto out_add_entry;
168 *(map_entries + i) = entry;
171 return 0;
172 out_add_entry:
173 for (j = i - 1; j > 0; j--) {
174 entry = *(map_entries + j);
175 kobject_put(&entry->kobj);
177 if (map_kset)
178 kset_unregister(map_kset);
179 out:
180 return ret;