memmap.c 7.1 KB

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