1 // SPDX-License-Identifier: GPL-2.0
3 * linux/drivers/efi/runtime-map.c
4 * Copyright (C) 2013 Red Hat, Inc., Dave Young <dyoung@redhat.com>
7 #include <linux/string.h>
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/types.h>
11 #include <linux/efi.h>
12 #include <linux/slab.h>
14 #include <asm/setup.h>
16 struct efi_runtime_map_entry
{
18 struct kobject kobj
; /* kobject for each entry */
21 static struct efi_runtime_map_entry
**map_entries
;
23 struct map_attribute
{
24 struct attribute attr
;
25 ssize_t (*show
)(struct efi_runtime_map_entry
*entry
, char *buf
);
28 static inline struct map_attribute
*to_map_attr(struct attribute
*attr
)
30 return container_of(attr
, struct map_attribute
, attr
);
33 static ssize_t
type_show(struct efi_runtime_map_entry
*entry
, char *buf
)
35 return snprintf(buf
, PAGE_SIZE
, "0x%x\n", entry
->md
.type
);
38 #define EFI_RUNTIME_FIELD(var) entry->md.var
40 #define EFI_RUNTIME_U64_ATTR_SHOW(name) \
41 static ssize_t name##_show(struct efi_runtime_map_entry *entry, char *buf) \
43 return snprintf(buf, PAGE_SIZE, "0x%llx\n", EFI_RUNTIME_FIELD(name)); \
46 EFI_RUNTIME_U64_ATTR_SHOW(phys_addr
);
47 EFI_RUNTIME_U64_ATTR_SHOW(virt_addr
);
48 EFI_RUNTIME_U64_ATTR_SHOW(num_pages
);
49 EFI_RUNTIME_U64_ATTR_SHOW(attribute
);
51 static inline struct efi_runtime_map_entry
*to_map_entry(struct kobject
*kobj
)
53 return container_of(kobj
, struct efi_runtime_map_entry
, kobj
);
56 static ssize_t
map_attr_show(struct kobject
*kobj
, struct attribute
*attr
,
59 struct efi_runtime_map_entry
*entry
= to_map_entry(kobj
);
60 struct map_attribute
*map_attr
= to_map_attr(attr
);
62 return map_attr
->show(entry
, buf
);
65 static struct map_attribute map_type_attr
= __ATTR_RO_MODE(type
, 0400);
66 static struct map_attribute map_phys_addr_attr
= __ATTR_RO_MODE(phys_addr
, 0400);
67 static struct map_attribute map_virt_addr_attr
= __ATTR_RO_MODE(virt_addr
, 0400);
68 static struct map_attribute map_num_pages_attr
= __ATTR_RO_MODE(num_pages
, 0400);
69 static struct map_attribute map_attribute_attr
= __ATTR_RO_MODE(attribute
, 0400);
72 * These are default attributes that are added for every memmap entry.
74 static struct attribute
*def_attrs
[] = {
76 &map_phys_addr_attr
.attr
,
77 &map_virt_addr_attr
.attr
,
78 &map_num_pages_attr
.attr
,
79 &map_attribute_attr
.attr
,
83 static const struct sysfs_ops map_attr_ops
= {
84 .show
= map_attr_show
,
87 static void map_release(struct kobject
*kobj
)
89 struct efi_runtime_map_entry
*entry
;
91 entry
= to_map_entry(kobj
);
95 static struct kobj_type __refdata map_ktype
= {
96 .sysfs_ops
= &map_attr_ops
,
97 .default_attrs
= def_attrs
,
98 .release
= map_release
,
101 static struct kset
*map_kset
;
103 static struct efi_runtime_map_entry
*
104 add_sysfs_runtime_map_entry(struct kobject
*kobj
, int nr
,
105 efi_memory_desc_t
*md
)
108 struct efi_runtime_map_entry
*entry
;
111 map_kset
= kset_create_and_add("runtime-map", NULL
, kobj
);
113 return ERR_PTR(-ENOMEM
);
116 entry
= kzalloc(sizeof(*entry
), GFP_KERNEL
);
118 kset_unregister(map_kset
);
120 return ERR_PTR(-ENOMEM
);
123 memcpy(&entry
->md
, md
, sizeof(efi_memory_desc_t
));
125 kobject_init(&entry
->kobj
, &map_ktype
);
126 entry
->kobj
.kset
= map_kset
;
127 ret
= kobject_add(&entry
->kobj
, NULL
, "%d", nr
);
129 kobject_put(&entry
->kobj
);
130 kset_unregister(map_kset
);
138 int efi_get_runtime_map_size(void)
140 return efi
.memmap
.nr_map
* efi
.memmap
.desc_size
;
143 int efi_get_runtime_map_desc_size(void)
145 return efi
.memmap
.desc_size
;
148 int efi_runtime_map_copy(void *buf
, size_t bufsz
)
150 size_t sz
= efi_get_runtime_map_size();
155 memcpy(buf
, efi
.memmap
.map
, sz
);
159 int __init
efi_runtime_map_init(struct kobject
*efi_kobj
)
162 struct efi_runtime_map_entry
*entry
;
163 efi_memory_desc_t
*md
;
165 if (!efi_enabled(EFI_MEMMAP
))
168 map_entries
= kcalloc(efi
.memmap
.nr_map
, sizeof(entry
), GFP_KERNEL
);
175 for_each_efi_memory_desc(md
) {
176 entry
= add_sysfs_runtime_map_entry(efi_kobj
, i
, md
);
178 ret
= PTR_ERR(entry
);
181 *(map_entries
+ i
++) = entry
;
186 for (j
= i
- 1; j
>= 0; j
--) {
187 entry
= *(map_entries
+ j
);
188 kobject_put(&entry
->kobj
);