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.
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 struct efi_runtime_map_entry
{
19 struct kobject kobj
; /* kobject for each entry */
22 static struct efi_runtime_map_entry
**map_entries
;
24 struct map_attribute
{
25 struct attribute attr
;
26 ssize_t (*show
)(struct efi_runtime_map_entry
*entry
, char *buf
);
29 static inline struct map_attribute
*to_map_attr(struct attribute
*attr
)
31 return container_of(attr
, struct map_attribute
, attr
);
34 static ssize_t
type_show(struct efi_runtime_map_entry
*entry
, char *buf
)
36 return snprintf(buf
, PAGE_SIZE
, "0x%x\n", entry
->md
.type
);
39 #define EFI_RUNTIME_FIELD(var) entry->md.var
41 #define EFI_RUNTIME_U64_ATTR_SHOW(name) \
42 static ssize_t name##_show(struct efi_runtime_map_entry *entry, char *buf) \
44 return snprintf(buf, PAGE_SIZE, "0x%llx\n", EFI_RUNTIME_FIELD(name)); \
47 EFI_RUNTIME_U64_ATTR_SHOW(phys_addr
);
48 EFI_RUNTIME_U64_ATTR_SHOW(virt_addr
);
49 EFI_RUNTIME_U64_ATTR_SHOW(num_pages
);
50 EFI_RUNTIME_U64_ATTR_SHOW(attribute
);
52 static inline struct efi_runtime_map_entry
*to_map_entry(struct kobject
*kobj
)
54 return container_of(kobj
, struct efi_runtime_map_entry
, kobj
);
57 static ssize_t
map_attr_show(struct kobject
*kobj
, struct attribute
*attr
,
60 struct efi_runtime_map_entry
*entry
= to_map_entry(kobj
);
61 struct map_attribute
*map_attr
= to_map_attr(attr
);
63 return map_attr
->show(entry
, buf
);
66 static struct map_attribute map_type_attr
= __ATTR_RO_MODE(type
, 0400);
67 static struct map_attribute map_phys_addr_attr
= __ATTR_RO_MODE(phys_addr
, 0400);
68 static struct map_attribute map_virt_addr_attr
= __ATTR_RO_MODE(virt_addr
, 0400);
69 static struct map_attribute map_num_pages_attr
= __ATTR_RO_MODE(num_pages
, 0400);
70 static struct map_attribute map_attribute_attr
= __ATTR_RO_MODE(attribute
, 0400);
73 * These are default attributes that are added for every memmap entry.
75 static struct attribute
*def_attrs
[] = {
77 &map_phys_addr_attr
.attr
,
78 &map_virt_addr_attr
.attr
,
79 &map_num_pages_attr
.attr
,
80 &map_attribute_attr
.attr
,
84 static const struct sysfs_ops map_attr_ops
= {
85 .show
= map_attr_show
,
88 static void map_release(struct kobject
*kobj
)
90 struct efi_runtime_map_entry
*entry
;
92 entry
= to_map_entry(kobj
);
96 static struct kobj_type __refdata map_ktype
= {
97 .sysfs_ops
= &map_attr_ops
,
98 .default_attrs
= def_attrs
,
99 .release
= map_release
,
102 static struct kset
*map_kset
;
104 static struct efi_runtime_map_entry
*
105 add_sysfs_runtime_map_entry(struct kobject
*kobj
, int nr
,
106 efi_memory_desc_t
*md
)
109 struct efi_runtime_map_entry
*entry
;
112 map_kset
= kset_create_and_add("runtime-map", NULL
, kobj
);
114 return ERR_PTR(-ENOMEM
);
117 entry
= kzalloc(sizeof(*entry
), GFP_KERNEL
);
119 kset_unregister(map_kset
);
121 return ERR_PTR(-ENOMEM
);
124 memcpy(&entry
->md
, md
, sizeof(efi_memory_desc_t
));
126 kobject_init(&entry
->kobj
, &map_ktype
);
127 entry
->kobj
.kset
= map_kset
;
128 ret
= kobject_add(&entry
->kobj
, NULL
, "%d", nr
);
130 kobject_put(&entry
->kobj
);
131 kset_unregister(map_kset
);
139 int efi_get_runtime_map_size(void)
141 return efi
.memmap
.nr_map
* efi
.memmap
.desc_size
;
144 int efi_get_runtime_map_desc_size(void)
146 return efi
.memmap
.desc_size
;
149 int efi_runtime_map_copy(void *buf
, size_t bufsz
)
151 size_t sz
= efi_get_runtime_map_size();
156 memcpy(buf
, efi
.memmap
.map
, sz
);
160 int __init
efi_runtime_map_init(struct kobject
*efi_kobj
)
163 struct efi_runtime_map_entry
*entry
;
164 efi_memory_desc_t
*md
;
166 if (!efi_enabled(EFI_MEMMAP
))
169 map_entries
= kzalloc(efi
.memmap
.nr_map
* sizeof(entry
), GFP_KERNEL
);
176 for_each_efi_memory_desc(md
) {
177 entry
= add_sysfs_runtime_map_entry(efi_kobj
, i
, md
);
179 ret
= PTR_ERR(entry
);
182 *(map_entries
+ i
++) = entry
;
187 for (j
= i
- 1; j
>= 0; j
--) {
188 entry
= *(map_entries
+ j
);
189 kobject_put(&entry
->kobj
);