runtime-map.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. map_kset = NULL;
  100. return ERR_PTR(-ENOMEM);
  101. }
  102. memcpy(&entry->md, efi_runtime_map + nr * efi_memdesc_size,
  103. sizeof(efi_memory_desc_t));
  104. kobject_init(&entry->kobj, &map_ktype);
  105. entry->kobj.kset = map_kset;
  106. ret = kobject_add(&entry->kobj, NULL, "%d", nr);
  107. if (ret) {
  108. kobject_put(&entry->kobj);
  109. kset_unregister(map_kset);
  110. map_kset = NULL;
  111. return ERR_PTR(ret);
  112. }
  113. return entry;
  114. }
  115. int efi_get_runtime_map_size(void)
  116. {
  117. return nr_efi_runtime_map * efi_memdesc_size;
  118. }
  119. int efi_get_runtime_map_desc_size(void)
  120. {
  121. return efi_memdesc_size;
  122. }
  123. int efi_runtime_map_copy(void *buf, size_t bufsz)
  124. {
  125. size_t sz = efi_get_runtime_map_size();
  126. if (sz > bufsz)
  127. sz = bufsz;
  128. memcpy(buf, efi_runtime_map, sz);
  129. return 0;
  130. }
  131. void efi_runtime_map_setup(void *map, int nr_entries, u32 desc_size)
  132. {
  133. efi_runtime_map = map;
  134. nr_efi_runtime_map = nr_entries;
  135. efi_memdesc_size = desc_size;
  136. }
  137. int __init efi_runtime_map_init(struct kobject *efi_kobj)
  138. {
  139. int i, j, ret = 0;
  140. struct efi_runtime_map_entry *entry;
  141. if (!efi_runtime_map)
  142. return 0;
  143. map_entries = kzalloc(nr_efi_runtime_map * sizeof(entry), GFP_KERNEL);
  144. if (!map_entries) {
  145. ret = -ENOMEM;
  146. goto out;
  147. }
  148. for (i = 0; i < nr_efi_runtime_map; i++) {
  149. entry = add_sysfs_runtime_map_entry(efi_kobj, i);
  150. if (IS_ERR(entry)) {
  151. ret = PTR_ERR(entry);
  152. goto out_add_entry;
  153. }
  154. *(map_entries + i) = entry;
  155. }
  156. return 0;
  157. out_add_entry:
  158. for (j = i - 1; j >= 0; j--) {
  159. entry = *(map_entries + j);
  160. kobject_put(&entry->kobj);
  161. }
  162. out:
  163. return ret;
  164. }