1*1fff234dSArd Biesheuvel // SPDX-License-Identifier: GPL-2.0 2*1fff234dSArd Biesheuvel /* 3*1fff234dSArd Biesheuvel * Copyright (C) 2013 Red Hat, Inc., Dave Young <dyoung@redhat.com> 4*1fff234dSArd Biesheuvel */ 5*1fff234dSArd Biesheuvel 6*1fff234dSArd Biesheuvel #include <linux/string.h> 7*1fff234dSArd Biesheuvel #include <linux/kernel.h> 8*1fff234dSArd Biesheuvel #include <linux/module.h> 9*1fff234dSArd Biesheuvel #include <linux/types.h> 10*1fff234dSArd Biesheuvel #include <linux/efi.h> 11*1fff234dSArd Biesheuvel #include <linux/slab.h> 12*1fff234dSArd Biesheuvel 13*1fff234dSArd Biesheuvel #include <asm/efi.h> 14*1fff234dSArd Biesheuvel #include <asm/setup.h> 15*1fff234dSArd Biesheuvel 16*1fff234dSArd Biesheuvel struct efi_runtime_map_entry { 17*1fff234dSArd Biesheuvel efi_memory_desc_t md; 18*1fff234dSArd Biesheuvel struct kobject kobj; /* kobject for each entry */ 19*1fff234dSArd Biesheuvel }; 20*1fff234dSArd Biesheuvel 21*1fff234dSArd Biesheuvel static struct efi_runtime_map_entry **map_entries; 22*1fff234dSArd Biesheuvel 23*1fff234dSArd Biesheuvel struct map_attribute { 24*1fff234dSArd Biesheuvel struct attribute attr; 25*1fff234dSArd Biesheuvel ssize_t (*show)(struct efi_runtime_map_entry *entry, char *buf); 26*1fff234dSArd Biesheuvel }; 27*1fff234dSArd Biesheuvel 28*1fff234dSArd Biesheuvel static inline struct map_attribute *to_map_attr(struct attribute *attr) 29*1fff234dSArd Biesheuvel { 30*1fff234dSArd Biesheuvel return container_of(attr, struct map_attribute, attr); 31*1fff234dSArd Biesheuvel } 32*1fff234dSArd Biesheuvel 33*1fff234dSArd Biesheuvel static ssize_t type_show(struct efi_runtime_map_entry *entry, char *buf) 34*1fff234dSArd Biesheuvel { 35*1fff234dSArd Biesheuvel return snprintf(buf, PAGE_SIZE, "0x%x\n", entry->md.type); 36*1fff234dSArd Biesheuvel } 37*1fff234dSArd Biesheuvel 38*1fff234dSArd Biesheuvel #define EFI_RUNTIME_FIELD(var) entry->md.var 39*1fff234dSArd Biesheuvel 40*1fff234dSArd Biesheuvel #define EFI_RUNTIME_U64_ATTR_SHOW(name) \ 41*1fff234dSArd Biesheuvel static ssize_t name##_show(struct efi_runtime_map_entry *entry, char *buf) \ 42*1fff234dSArd Biesheuvel { \ 43*1fff234dSArd Biesheuvel return snprintf(buf, PAGE_SIZE, "0x%llx\n", EFI_RUNTIME_FIELD(name)); \ 44*1fff234dSArd Biesheuvel } 45*1fff234dSArd Biesheuvel 46*1fff234dSArd Biesheuvel EFI_RUNTIME_U64_ATTR_SHOW(phys_addr); 47*1fff234dSArd Biesheuvel EFI_RUNTIME_U64_ATTR_SHOW(virt_addr); 48*1fff234dSArd Biesheuvel EFI_RUNTIME_U64_ATTR_SHOW(num_pages); 49*1fff234dSArd Biesheuvel EFI_RUNTIME_U64_ATTR_SHOW(attribute); 50*1fff234dSArd Biesheuvel 51*1fff234dSArd Biesheuvel static inline struct efi_runtime_map_entry *to_map_entry(struct kobject *kobj) 52*1fff234dSArd Biesheuvel { 53*1fff234dSArd Biesheuvel return container_of(kobj, struct efi_runtime_map_entry, kobj); 54*1fff234dSArd Biesheuvel } 55*1fff234dSArd Biesheuvel 56*1fff234dSArd Biesheuvel static ssize_t map_attr_show(struct kobject *kobj, struct attribute *attr, 57*1fff234dSArd Biesheuvel char *buf) 58*1fff234dSArd Biesheuvel { 59*1fff234dSArd Biesheuvel struct efi_runtime_map_entry *entry = to_map_entry(kobj); 60*1fff234dSArd Biesheuvel struct map_attribute *map_attr = to_map_attr(attr); 61*1fff234dSArd Biesheuvel 62*1fff234dSArd Biesheuvel return map_attr->show(entry, buf); 63*1fff234dSArd Biesheuvel } 64*1fff234dSArd Biesheuvel 65*1fff234dSArd Biesheuvel static struct map_attribute map_type_attr = __ATTR_RO_MODE(type, 0400); 66*1fff234dSArd Biesheuvel static struct map_attribute map_phys_addr_attr = __ATTR_RO_MODE(phys_addr, 0400); 67*1fff234dSArd Biesheuvel static struct map_attribute map_virt_addr_attr = __ATTR_RO_MODE(virt_addr, 0400); 68*1fff234dSArd Biesheuvel static struct map_attribute map_num_pages_attr = __ATTR_RO_MODE(num_pages, 0400); 69*1fff234dSArd Biesheuvel static struct map_attribute map_attribute_attr = __ATTR_RO_MODE(attribute, 0400); 70*1fff234dSArd Biesheuvel 71*1fff234dSArd Biesheuvel /* 72*1fff234dSArd Biesheuvel * These are default attributes that are added for every memmap entry. 73*1fff234dSArd Biesheuvel */ 74*1fff234dSArd Biesheuvel static struct attribute *def_attrs[] = { 75*1fff234dSArd Biesheuvel &map_type_attr.attr, 76*1fff234dSArd Biesheuvel &map_phys_addr_attr.attr, 77*1fff234dSArd Biesheuvel &map_virt_addr_attr.attr, 78*1fff234dSArd Biesheuvel &map_num_pages_attr.attr, 79*1fff234dSArd Biesheuvel &map_attribute_attr.attr, 80*1fff234dSArd Biesheuvel NULL 81*1fff234dSArd Biesheuvel }; 82*1fff234dSArd Biesheuvel ATTRIBUTE_GROUPS(def); 83*1fff234dSArd Biesheuvel 84*1fff234dSArd Biesheuvel static const struct sysfs_ops map_attr_ops = { 85*1fff234dSArd Biesheuvel .show = map_attr_show, 86*1fff234dSArd Biesheuvel }; 87*1fff234dSArd Biesheuvel 88*1fff234dSArd Biesheuvel static void map_release(struct kobject *kobj) 89*1fff234dSArd Biesheuvel { 90*1fff234dSArd Biesheuvel struct efi_runtime_map_entry *entry; 91*1fff234dSArd Biesheuvel 92*1fff234dSArd Biesheuvel entry = to_map_entry(kobj); 93*1fff234dSArd Biesheuvel kfree(entry); 94*1fff234dSArd Biesheuvel } 95*1fff234dSArd Biesheuvel 96*1fff234dSArd Biesheuvel static struct kobj_type __refdata map_ktype = { 97*1fff234dSArd Biesheuvel .sysfs_ops = &map_attr_ops, 98*1fff234dSArd Biesheuvel .default_groups = def_groups, 99*1fff234dSArd Biesheuvel .release = map_release, 100*1fff234dSArd Biesheuvel }; 101*1fff234dSArd Biesheuvel 102*1fff234dSArd Biesheuvel static struct kset *map_kset; 103*1fff234dSArd Biesheuvel 104*1fff234dSArd Biesheuvel static struct efi_runtime_map_entry * 105*1fff234dSArd Biesheuvel add_sysfs_runtime_map_entry(struct kobject *kobj, int nr, 106*1fff234dSArd Biesheuvel efi_memory_desc_t *md) 107*1fff234dSArd Biesheuvel { 108*1fff234dSArd Biesheuvel int ret; 109*1fff234dSArd Biesheuvel struct efi_runtime_map_entry *entry; 110*1fff234dSArd Biesheuvel 111*1fff234dSArd Biesheuvel if (!map_kset) { 112*1fff234dSArd Biesheuvel map_kset = kset_create_and_add("runtime-map", NULL, kobj); 113*1fff234dSArd Biesheuvel if (!map_kset) 114*1fff234dSArd Biesheuvel return ERR_PTR(-ENOMEM); 115*1fff234dSArd Biesheuvel } 116*1fff234dSArd Biesheuvel 117*1fff234dSArd Biesheuvel entry = kzalloc(sizeof(*entry), GFP_KERNEL); 118*1fff234dSArd Biesheuvel if (!entry) { 119*1fff234dSArd Biesheuvel kset_unregister(map_kset); 120*1fff234dSArd Biesheuvel map_kset = NULL; 121*1fff234dSArd Biesheuvel return ERR_PTR(-ENOMEM); 122*1fff234dSArd Biesheuvel } 123*1fff234dSArd Biesheuvel 124*1fff234dSArd Biesheuvel memcpy(&entry->md, md, sizeof(efi_memory_desc_t)); 125*1fff234dSArd Biesheuvel 126*1fff234dSArd Biesheuvel kobject_init(&entry->kobj, &map_ktype); 127*1fff234dSArd Biesheuvel entry->kobj.kset = map_kset; 128*1fff234dSArd Biesheuvel ret = kobject_add(&entry->kobj, NULL, "%d", nr); 129*1fff234dSArd Biesheuvel if (ret) { 130*1fff234dSArd Biesheuvel kobject_put(&entry->kobj); 131*1fff234dSArd Biesheuvel kset_unregister(map_kset); 132*1fff234dSArd Biesheuvel map_kset = NULL; 133*1fff234dSArd Biesheuvel return ERR_PTR(ret); 134*1fff234dSArd Biesheuvel } 135*1fff234dSArd Biesheuvel 136*1fff234dSArd Biesheuvel return entry; 137*1fff234dSArd Biesheuvel } 138*1fff234dSArd Biesheuvel 139*1fff234dSArd Biesheuvel int efi_get_runtime_map_size(void) 140*1fff234dSArd Biesheuvel { 141*1fff234dSArd Biesheuvel return efi.memmap.nr_map * efi.memmap.desc_size; 142*1fff234dSArd Biesheuvel } 143*1fff234dSArd Biesheuvel 144*1fff234dSArd Biesheuvel int efi_get_runtime_map_desc_size(void) 145*1fff234dSArd Biesheuvel { 146*1fff234dSArd Biesheuvel return efi.memmap.desc_size; 147*1fff234dSArd Biesheuvel } 148*1fff234dSArd Biesheuvel 149*1fff234dSArd Biesheuvel int efi_runtime_map_copy(void *buf, size_t bufsz) 150*1fff234dSArd Biesheuvel { 151*1fff234dSArd Biesheuvel size_t sz = efi_get_runtime_map_size(); 152*1fff234dSArd Biesheuvel 153*1fff234dSArd Biesheuvel if (sz > bufsz) 154*1fff234dSArd Biesheuvel sz = bufsz; 155*1fff234dSArd Biesheuvel 156*1fff234dSArd Biesheuvel memcpy(buf, efi.memmap.map, sz); 157*1fff234dSArd Biesheuvel return 0; 158*1fff234dSArd Biesheuvel } 159*1fff234dSArd Biesheuvel 160*1fff234dSArd Biesheuvel static int __init efi_runtime_map_init(void) 161*1fff234dSArd Biesheuvel { 162*1fff234dSArd Biesheuvel int i, j, ret = 0; 163*1fff234dSArd Biesheuvel struct efi_runtime_map_entry *entry; 164*1fff234dSArd Biesheuvel efi_memory_desc_t *md; 165*1fff234dSArd Biesheuvel 166*1fff234dSArd Biesheuvel if (!efi_enabled(EFI_MEMMAP) || !efi_kobj) 167*1fff234dSArd Biesheuvel return 0; 168*1fff234dSArd Biesheuvel 169*1fff234dSArd Biesheuvel map_entries = kcalloc(efi.memmap.nr_map, sizeof(entry), GFP_KERNEL); 170*1fff234dSArd Biesheuvel if (!map_entries) { 171*1fff234dSArd Biesheuvel ret = -ENOMEM; 172*1fff234dSArd Biesheuvel goto out; 173*1fff234dSArd Biesheuvel } 174*1fff234dSArd Biesheuvel 175*1fff234dSArd Biesheuvel i = 0; 176*1fff234dSArd Biesheuvel for_each_efi_memory_desc(md) { 177*1fff234dSArd Biesheuvel entry = add_sysfs_runtime_map_entry(efi_kobj, i, md); 178*1fff234dSArd Biesheuvel if (IS_ERR(entry)) { 179*1fff234dSArd Biesheuvel ret = PTR_ERR(entry); 180*1fff234dSArd Biesheuvel goto out_add_entry; 181*1fff234dSArd Biesheuvel } 182*1fff234dSArd Biesheuvel *(map_entries + i++) = entry; 183*1fff234dSArd Biesheuvel } 184*1fff234dSArd Biesheuvel 185*1fff234dSArd Biesheuvel return 0; 186*1fff234dSArd Biesheuvel out_add_entry: 187*1fff234dSArd Biesheuvel for (j = i - 1; j >= 0; j--) { 188*1fff234dSArd Biesheuvel entry = *(map_entries + j); 189*1fff234dSArd Biesheuvel kobject_put(&entry->kobj); 190*1fff234dSArd Biesheuvel } 191*1fff234dSArd Biesheuvel out: 192*1fff234dSArd Biesheuvel return ret; 193*1fff234dSArd Biesheuvel } 194*1fff234dSArd Biesheuvel subsys_initcall_sync(efi_runtime_map_init); 195