runtime-map.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. #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. #include <asm/setup.h>
  14. static void *efi_runtime_map;
  15. static int nr_efi_runtime_map;
  16. static u32 efi_memdesc_size;
  17. struct efi_runtime_map_entry {
  18. efi_memory_desc_t md;
  19. struct kobject kobj; /* kobject for each entry */
  20. };
  21. static struct efi_runtime_map_entry **map_entries;
  22. struct map_attribute {
  23. struct attribute attr;
  24. ssize_t (*show)(struct efi_runtime_map_entry *entry, char *buf);
  25. };
  26. static inline struct map_attribute *to_map_attr(struct attribute *attr)
  27. {
  28. return container_of(attr, struct map_attribute, attr);
  29. }
  30. static ssize_t type_show(struct efi_runtime_map_entry *entry, char *buf)
  31. {
  32. return snprintf(buf, PAGE_SIZE, "0x%x\n", entry->md.type);
  33. }
  34. #define EFI_RUNTIME_FIELD(var) entry->md.var
  35. #define EFI_RUNTIME_U64_ATTR_SHOW(name) \
  36. static ssize_t name##_show(struct efi_runtime_map_entry *entry, char *buf) \
  37. { \
  38. return snprintf(buf, PAGE_SIZE, "0x%llx\n", EFI_RUNTIME_FIELD(name)); \
  39. }
  40. EFI_RUNTIME_U64_ATTR_SHOW(phys_addr);
  41. EFI_RUNTIME_U64_ATTR_SHOW(virt_addr);
  42. EFI_RUNTIME_U64_ATTR_SHOW(num_pages);
  43. EFI_RUNTIME_U64_ATTR_SHOW(attribute);
  44. static inline struct efi_runtime_map_entry *to_map_entry(struct kobject *kobj)
  45. {
  46. return container_of(kobj, struct efi_runtime_map_entry, kobj);
  47. }
  48. static ssize_t map_attr_show(struct kobject *kobj, struct attribute *attr,
  49. char *buf)
  50. {
  51. struct efi_runtime_map_entry *entry = to_map_entry(kobj);
  52. struct map_attribute *map_attr = to_map_attr(attr);
  53. return map_attr->show(entry, buf);
  54. }
  55. static struct map_attribute map_type_attr = __ATTR_RO(type);
  56. static struct map_attribute map_phys_addr_attr = __ATTR_RO(phys_addr);
  57. static struct map_attribute map_virt_addr_attr = __ATTR_RO(virt_addr);
  58. static struct map_attribute map_num_pages_attr = __ATTR_RO(num_pages);
  59. static struct map_attribute map_attribute_attr = __ATTR_RO(attribute);
  60. /*
  61. * These are default attributes that are added for every memmap entry.
  62. */
  63. static struct attribute *def_attrs[] = {
  64. &map_type_attr.attr,
  65. &map_phys_addr_attr.attr,
  66. &map_virt_addr_attr.attr,
  67. &map_num_pages_attr.attr,
  68. &map_attribute_attr.attr,
  69. NULL
  70. };
  71. static const struct sysfs_ops map_attr_ops = {
  72. .show = map_attr_show,
  73. };
  74. static void map_release(struct kobject *kobj)
  75. {
  76. struct efi_runtime_map_entry *entry;
  77. entry = to_map_entry(kobj);
  78. kfree(entry);
  79. }
  80. static struct kobj_type __refdata map_ktype = {
  81. .sysfs_ops = &map_attr_ops,
  82. .default_attrs = def_attrs,
  83. .release = map_release,
  84. };
  85. static struct kset *map_kset;
  86. static struct efi_runtime_map_entry *
  87. add_sysfs_runtime_map_entry(struct kobject *kobj, int nr)
  88. {
  89. int ret;
  90. struct efi_runtime_map_entry *entry;
  91. if (!map_kset) {
  92. map_kset = kset_create_and_add("runtime-map", NULL, kobj);
  93. if (!map_kset)
  94. return ERR_PTR(-ENOMEM);
  95. }
  96. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  97. if (!entry) {
  98. kset_unregister(map_kset);
  99. return entry;
  100. }
  101. memcpy(&entry->md, efi_runtime_map + nr * efi_memdesc_size,
  102. sizeof(efi_memory_desc_t));
  103. kobject_init(&entry->kobj, &map_ktype);
  104. entry->kobj.kset = map_kset;
  105. ret = kobject_add(&entry->kobj, NULL, "%d", nr);
  106. if (ret) {
  107. kobject_put(&entry->kobj);
  108. kset_unregister(map_kset);
  109. return ERR_PTR(ret);
  110. }
  111. return entry;
  112. }
  113. void efi_runtime_map_setup(void *map, int nr_entries, u32 desc_size)
  114. {
  115. efi_runtime_map = map;
  116. nr_efi_runtime_map = nr_entries;
  117. efi_memdesc_size = desc_size;
  118. }
  119. int __init efi_runtime_map_init(struct kobject *efi_kobj)
  120. {
  121. int i, j, ret = 0;
  122. struct efi_runtime_map_entry *entry;
  123. if (!efi_runtime_map)
  124. return 0;
  125. map_entries = kzalloc(nr_efi_runtime_map * sizeof(entry), GFP_KERNEL);
  126. if (!map_entries) {
  127. ret = -ENOMEM;
  128. goto out;
  129. }
  130. for (i = 0; i < nr_efi_runtime_map; i++) {
  131. entry = add_sysfs_runtime_map_entry(efi_kobj, i);
  132. if (IS_ERR(entry)) {
  133. ret = PTR_ERR(entry);
  134. goto out_add_entry;
  135. }
  136. *(map_entries + i) = entry;
  137. }
  138. return 0;
  139. out_add_entry:
  140. for (j = i - 1; j > 0; j--) {
  141. entry = *(map_entries + j);
  142. kobject_put(&entry->kobj);
  143. }
  144. if (map_kset)
  145. kset_unregister(map_kset);
  146. out:
  147. return ret;
  148. }