memmap.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * linux/drivers/firmware/memmap.c
  3. * Copyright (C) 2008 SUSE LINUX Products GmbH
  4. * by Bernhard Walle <bernhard.walle@gmx.de>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License v2.0 as published by
  8. * the Free Software Foundation
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. */
  16. #include <linux/string.h>
  17. #include <linux/firmware-map.h>
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/types.h>
  21. #include <linux/bootmem.h>
  22. #include <linux/slab.h>
  23. /*
  24. * Data types ------------------------------------------------------------------
  25. */
  26. /*
  27. * Firmware map entry. Because firmware memory maps are flat and not
  28. * hierarchical, it's ok to organise them in a linked list. No parent
  29. * information is necessary as for the resource tree.
  30. */
  31. struct firmware_map_entry {
  32. /*
  33. * start and end must be u64 rather than resource_size_t, because e820
  34. * resources can lie at addresses above 4G.
  35. */
  36. u64 start; /* start of the memory range */
  37. u64 end; /* end of the memory range (incl.) */
  38. const char *type; /* type of the memory range */
  39. struct list_head list; /* entry for the linked list */
  40. struct kobject kobj; /* kobject for each entry */
  41. };
  42. /*
  43. * Forward declarations --------------------------------------------------------
  44. */
  45. static ssize_t memmap_attr_show(struct kobject *kobj,
  46. struct attribute *attr, char *buf);
  47. static ssize_t start_show(struct firmware_map_entry *entry, char *buf);
  48. static ssize_t end_show(struct firmware_map_entry *entry, char *buf);
  49. static ssize_t type_show(struct firmware_map_entry *entry, char *buf);
  50. /*
  51. * Static data -----------------------------------------------------------------
  52. */
  53. struct memmap_attribute {
  54. struct attribute attr;
  55. ssize_t (*show)(struct firmware_map_entry *entry, char *buf);
  56. };
  57. static struct memmap_attribute memmap_start_attr = __ATTR_RO(start);
  58. static struct memmap_attribute memmap_end_attr = __ATTR_RO(end);
  59. static struct memmap_attribute memmap_type_attr = __ATTR_RO(type);
  60. /*
  61. * These are default attributes that are added for every memmap entry.
  62. */
  63. static struct attribute *def_attrs[] = {
  64. &memmap_start_attr.attr,
  65. &memmap_end_attr.attr,
  66. &memmap_type_attr.attr,
  67. NULL
  68. };
  69. static const struct sysfs_ops memmap_attr_ops = {
  70. .show = memmap_attr_show,
  71. };
  72. static struct kobj_type memmap_ktype = {
  73. .sysfs_ops = &memmap_attr_ops,
  74. .default_attrs = def_attrs,
  75. };
  76. /*
  77. * Registration functions ------------------------------------------------------
  78. */
  79. /*
  80. * Firmware memory map entries. No locking is needed because the
  81. * firmware_map_add() and firmware_map_add_early() functions are called
  82. * in firmware initialisation code in one single thread of execution.
  83. */
  84. static LIST_HEAD(map_entries);
  85. /**
  86. * firmware_map_add_entry() - Does the real work to add a firmware memmap entry.
  87. * @start: Start of the memory range.
  88. * @end: End of the memory range (inclusive).
  89. * @type: Type of the memory range.
  90. * @entry: Pre-allocated (either kmalloc() or bootmem allocator), uninitialised
  91. * entry.
  92. *
  93. * Common implementation of firmware_map_add() and firmware_map_add_early()
  94. * which expects a pre-allocated struct firmware_map_entry.
  95. **/
  96. static int firmware_map_add_entry(u64 start, u64 end,
  97. const char *type,
  98. struct firmware_map_entry *entry)
  99. {
  100. BUG_ON(start > end);
  101. entry->start = start;
  102. entry->end = end;
  103. entry->type = type;
  104. INIT_LIST_HEAD(&entry->list);
  105. kobject_init(&entry->kobj, &memmap_ktype);
  106. list_add_tail(&entry->list, &map_entries);
  107. return 0;
  108. }
  109. /*
  110. * Add memmap entry on sysfs
  111. */
  112. static int add_sysfs_fw_map_entry(struct firmware_map_entry *entry)
  113. {
  114. static int map_entries_nr;
  115. static struct kset *mmap_kset;
  116. if (!mmap_kset) {
  117. mmap_kset = kset_create_and_add("memmap", NULL, firmware_kobj);
  118. if (!mmap_kset)
  119. return -ENOMEM;
  120. }
  121. entry->kobj.kset = mmap_kset;
  122. if (kobject_add(&entry->kobj, NULL, "%d", map_entries_nr++))
  123. kobject_put(&entry->kobj);
  124. return 0;
  125. }
  126. /**
  127. * firmware_map_add_hotplug() - Adds a firmware mapping entry when we do
  128. * memory hotplug.
  129. * @start: Start of the memory range.
  130. * @end: End of the memory range (inclusive).
  131. * @type: Type of the memory range.
  132. *
  133. * Adds a firmware mapping entry. This function is for memory hotplug, it is
  134. * similar to function firmware_map_add_early(). The only difference is that
  135. * it will create the syfs entry dynamically.
  136. *
  137. * Returns 0 on success, or -ENOMEM if no memory could be allocated.
  138. **/
  139. int __meminit firmware_map_add_hotplug(u64 start, u64 end, const char *type)
  140. {
  141. struct firmware_map_entry *entry;
  142. entry = kzalloc(sizeof(struct firmware_map_entry), GFP_ATOMIC);
  143. if (!entry)
  144. return -ENOMEM;
  145. firmware_map_add_entry(start, end, type, entry);
  146. /* create the memmap entry */
  147. add_sysfs_fw_map_entry(entry);
  148. return 0;
  149. }
  150. /**
  151. * firmware_map_add_early() - Adds a firmware mapping entry.
  152. * @start: Start of the memory range.
  153. * @end: End of the memory range (inclusive).
  154. * @type: Type of the memory range.
  155. *
  156. * Adds a firmware mapping entry. This function uses the bootmem allocator
  157. * for memory allocation.
  158. *
  159. * That function must be called before late_initcall.
  160. *
  161. * Returns 0 on success, or -ENOMEM if no memory could be allocated.
  162. **/
  163. int __init firmware_map_add_early(u64 start, u64 end, const char *type)
  164. {
  165. struct firmware_map_entry *entry;
  166. entry = alloc_bootmem(sizeof(struct firmware_map_entry));
  167. if (WARN_ON(!entry))
  168. return -ENOMEM;
  169. return firmware_map_add_entry(start, end, type, entry);
  170. }
  171. /*
  172. * Sysfs functions -------------------------------------------------------------
  173. */
  174. static ssize_t start_show(struct firmware_map_entry *entry, char *buf)
  175. {
  176. return snprintf(buf, PAGE_SIZE, "0x%llx\n",
  177. (unsigned long long)entry->start);
  178. }
  179. static ssize_t end_show(struct firmware_map_entry *entry, char *buf)
  180. {
  181. return snprintf(buf, PAGE_SIZE, "0x%llx\n",
  182. (unsigned long long)entry->end);
  183. }
  184. static ssize_t type_show(struct firmware_map_entry *entry, char *buf)
  185. {
  186. return snprintf(buf, PAGE_SIZE, "%s\n", entry->type);
  187. }
  188. #define to_memmap_attr(_attr) container_of(_attr, struct memmap_attribute, attr)
  189. #define to_memmap_entry(obj) container_of(obj, struct firmware_map_entry, kobj)
  190. static ssize_t memmap_attr_show(struct kobject *kobj,
  191. struct attribute *attr, char *buf)
  192. {
  193. struct firmware_map_entry *entry = to_memmap_entry(kobj);
  194. struct memmap_attribute *memmap_attr = to_memmap_attr(attr);
  195. return memmap_attr->show(entry, buf);
  196. }
  197. /*
  198. * Initialises stuff and adds the entries in the map_entries list to
  199. * sysfs. Important is that firmware_map_add() and firmware_map_add_early()
  200. * must be called before late_initcall. That's just because that function
  201. * is called as late_initcall() function, which means that if you call
  202. * firmware_map_add() or firmware_map_add_early() afterwards, the entries
  203. * are not added to sysfs.
  204. */
  205. static int __init memmap_init(void)
  206. {
  207. struct firmware_map_entry *entry;
  208. list_for_each_entry(entry, &map_entries, list)
  209. add_sysfs_fw_map_entry(entry);
  210. return 0;
  211. }
  212. late_initcall(memmap_init);