memmap.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 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. * firmware_map_add() - Adds a firmware mapping entry.
  110. * @start: Start of the memory range.
  111. * @end: End of the memory range (inclusive).
  112. * @type: Type of the memory range.
  113. *
  114. * This function uses kmalloc() for memory
  115. * allocation. Use firmware_map_add_early() if you want to use the bootmem
  116. * allocator.
  117. *
  118. * That function must be called before late_initcall.
  119. *
  120. * Returns 0 on success, or -ENOMEM if no memory could be allocated.
  121. **/
  122. int firmware_map_add(u64 start, u64 end, const char *type)
  123. {
  124. struct firmware_map_entry *entry;
  125. entry = kmalloc(sizeof(struct firmware_map_entry), GFP_ATOMIC);
  126. if (!entry)
  127. return -ENOMEM;
  128. return firmware_map_add_entry(start, end, type, entry);
  129. }
  130. /**
  131. * firmware_map_add_early() - Adds a firmware mapping entry.
  132. * @start: Start of the memory range.
  133. * @end: End of the memory range (inclusive).
  134. * @type: Type of the memory range.
  135. *
  136. * Adds a firmware mapping entry. This function uses the bootmem allocator
  137. * for memory allocation. Use firmware_map_add() if you want to use kmalloc().
  138. *
  139. * That function must be called before late_initcall.
  140. *
  141. * Returns 0 on success, or -ENOMEM if no memory could be allocated.
  142. **/
  143. int __init firmware_map_add_early(u64 start, u64 end, const char *type)
  144. {
  145. struct firmware_map_entry *entry;
  146. entry = alloc_bootmem_low(sizeof(struct firmware_map_entry));
  147. if (WARN_ON(!entry))
  148. return -ENOMEM;
  149. return firmware_map_add_entry(start, end, type, entry);
  150. }
  151. /*
  152. * Sysfs functions -------------------------------------------------------------
  153. */
  154. static ssize_t start_show(struct firmware_map_entry *entry, char *buf)
  155. {
  156. return snprintf(buf, PAGE_SIZE, "0x%llx\n",
  157. (unsigned long long)entry->start);
  158. }
  159. static ssize_t end_show(struct firmware_map_entry *entry, char *buf)
  160. {
  161. return snprintf(buf, PAGE_SIZE, "0x%llx\n",
  162. (unsigned long long)entry->end);
  163. }
  164. static ssize_t type_show(struct firmware_map_entry *entry, char *buf)
  165. {
  166. return snprintf(buf, PAGE_SIZE, "%s\n", entry->type);
  167. }
  168. #define to_memmap_attr(_attr) container_of(_attr, struct memmap_attribute, attr)
  169. #define to_memmap_entry(obj) container_of(obj, struct firmware_map_entry, kobj)
  170. static ssize_t memmap_attr_show(struct kobject *kobj,
  171. struct attribute *attr, char *buf)
  172. {
  173. struct firmware_map_entry *entry = to_memmap_entry(kobj);
  174. struct memmap_attribute *memmap_attr = to_memmap_attr(attr);
  175. return memmap_attr->show(entry, buf);
  176. }
  177. /*
  178. * Initialises stuff and adds the entries in the map_entries list to
  179. * sysfs. Important is that firmware_map_add() and firmware_map_add_early()
  180. * must be called before late_initcall. That's just because that function
  181. * is called as late_initcall() function, which means that if you call
  182. * firmware_map_add() or firmware_map_add_early() afterwards, the entries
  183. * are not added to sysfs.
  184. */
  185. static int __init memmap_init(void)
  186. {
  187. int i = 0;
  188. struct firmware_map_entry *entry;
  189. struct kset *memmap_kset;
  190. memmap_kset = kset_create_and_add("memmap", NULL, firmware_kobj);
  191. if (WARN_ON(!memmap_kset))
  192. return -ENOMEM;
  193. list_for_each_entry(entry, &map_entries, list) {
  194. entry->kobj.kset = memmap_kset;
  195. if (kobject_add(&entry->kobj, NULL, "%d", i++))
  196. kobject_put(&entry->kobj);
  197. }
  198. return 0;
  199. }
  200. late_initcall(memmap_init);