Linux Audio

Check our new training course

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