memmap.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * linux/drivers/firmware/memmap.c
  3. * Copyright (C) 2008 SUSE LINUX Products GmbH
  4. * by Bernhard Walle <bwalle@suse.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. resource_size_t start; /* start of the memory range */
  32. resource_size_t end; /* end of the memory range (incl.) */
  33. const char *type; /* type of the memory range */
  34. struct list_head list; /* entry for the linked list */
  35. struct kobject kobj; /* kobject for each entry */
  36. };
  37. /*
  38. * Forward declarations --------------------------------------------------------
  39. */
  40. static ssize_t memmap_attr_show(struct kobject *kobj,
  41. struct attribute *attr, char *buf);
  42. static ssize_t start_show(struct firmware_map_entry *entry, char *buf);
  43. static ssize_t end_show(struct firmware_map_entry *entry, char *buf);
  44. static ssize_t type_show(struct firmware_map_entry *entry, char *buf);
  45. /*
  46. * Static data -----------------------------------------------------------------
  47. */
  48. struct memmap_attribute {
  49. struct attribute attr;
  50. ssize_t (*show)(struct firmware_map_entry *entry, char *buf);
  51. };
  52. struct memmap_attribute memmap_start_attr = __ATTR_RO(start);
  53. struct memmap_attribute memmap_end_attr = __ATTR_RO(end);
  54. struct memmap_attribute memmap_type_attr = __ATTR_RO(type);
  55. /*
  56. * These are default attributes that are added for every memmap entry.
  57. */
  58. static struct attribute *def_attrs[] = {
  59. &memmap_start_attr.attr,
  60. &memmap_end_attr.attr,
  61. &memmap_type_attr.attr,
  62. NULL
  63. };
  64. static struct sysfs_ops memmap_attr_ops = {
  65. .show = memmap_attr_show,
  66. };
  67. static struct kobj_type memmap_ktype = {
  68. .sysfs_ops = &memmap_attr_ops,
  69. .default_attrs = def_attrs,
  70. };
  71. /*
  72. * Registration functions ------------------------------------------------------
  73. */
  74. /*
  75. * Firmware memory map entries. No locking is needed because the
  76. * firmware_map_add() and firmware_map_add_early() functions are called
  77. * in firmware initialisation code in one single thread of execution.
  78. */
  79. static LIST_HEAD(map_entries);
  80. /**
  81. * firmware_map_add_entry() - Does the real work to add a firmware memmap entry.
  82. * @start: Start of the memory range.
  83. * @end: End of the memory range (inclusive).
  84. * @type: Type of the memory range.
  85. * @entry: Pre-allocated (either kmalloc() or bootmem allocator), uninitialised
  86. * entry.
  87. *
  88. * Common implementation of firmware_map_add() and firmware_map_add_early()
  89. * which expects a pre-allocated struct firmware_map_entry.
  90. **/
  91. static int firmware_map_add_entry(resource_size_t start, resource_size_t end,
  92. const char *type,
  93. struct firmware_map_entry *entry)
  94. {
  95. BUG_ON(start > end);
  96. entry->start = start;
  97. entry->end = end;
  98. entry->type = type;
  99. INIT_LIST_HEAD(&entry->list);
  100. kobject_init(&entry->kobj, &memmap_ktype);
  101. list_add_tail(&entry->list, &map_entries);
  102. return 0;
  103. }
  104. /**
  105. * firmware_map_add() - Adds a firmware mapping entry.
  106. * @start: Start of the memory range.
  107. * @end: End of the memory range (inclusive).
  108. * @type: Type of the memory range.
  109. *
  110. * This function uses kmalloc() for memory
  111. * allocation. Use firmware_map_add_early() if you want to use the bootmem
  112. * allocator.
  113. *
  114. * That function must be called before late_initcall.
  115. *
  116. * Returns 0 on success, or -ENOMEM if no memory could be allocated.
  117. **/
  118. int firmware_map_add(resource_size_t start, resource_size_t end,
  119. const char *type)
  120. {
  121. struct firmware_map_entry *entry;
  122. entry = kmalloc(sizeof(struct firmware_map_entry), GFP_ATOMIC);
  123. if (!entry)
  124. return -ENOMEM;
  125. return firmware_map_add_entry(start, end, type, entry);
  126. }
  127. /**
  128. * firmware_map_add_early() - Adds a firmware mapping entry.
  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 uses the bootmem allocator
  134. * for memory allocation. Use firmware_map_add() if you want to use kmalloc().
  135. *
  136. * That function must be called before late_initcall.
  137. *
  138. * Returns 0 on success, or -ENOMEM if no memory could be allocated.
  139. **/
  140. int __init firmware_map_add_early(resource_size_t start, resource_size_t end,
  141. const char *type)
  142. {
  143. struct firmware_map_entry *entry;
  144. entry = alloc_bootmem_low(sizeof(struct firmware_map_entry));
  145. if (WARN_ON(!entry))
  146. return -ENOMEM;
  147. return firmware_map_add_entry(start, end, type, entry);
  148. }
  149. /*
  150. * Sysfs functions -------------------------------------------------------------
  151. */
  152. static ssize_t start_show(struct firmware_map_entry *entry, char *buf)
  153. {
  154. return snprintf(buf, PAGE_SIZE, "0x%llx\n",
  155. (unsigned long long)entry->start);
  156. }
  157. static ssize_t end_show(struct firmware_map_entry *entry, char *buf)
  158. {
  159. return snprintf(buf, PAGE_SIZE, "0x%llx\n",
  160. (unsigned long long)entry->end);
  161. }
  162. static ssize_t type_show(struct firmware_map_entry *entry, char *buf)
  163. {
  164. return snprintf(buf, PAGE_SIZE, "%s\n", entry->type);
  165. }
  166. #define to_memmap_attr(_attr) container_of(_attr, struct memmap_attribute, attr)
  167. #define to_memmap_entry(obj) container_of(obj, struct firmware_map_entry, kobj)
  168. static ssize_t memmap_attr_show(struct kobject *kobj,
  169. struct attribute *attr, char *buf)
  170. {
  171. struct firmware_map_entry *entry = to_memmap_entry(kobj);
  172. struct memmap_attribute *memmap_attr = to_memmap_attr(attr);
  173. return memmap_attr->show(entry, buf);
  174. }
  175. /*
  176. * Initialises stuff and adds the entries in the map_entries list to
  177. * sysfs. Important is that firmware_map_add() and firmware_map_add_early()
  178. * must be called before late_initcall. That's just because that function
  179. * is called as late_initcall() function, which means that if you call
  180. * firmware_map_add() or firmware_map_add_early() afterwards, the entries
  181. * are not added to sysfs.
  182. */
  183. static int __init memmap_init(void)
  184. {
  185. int i = 0;
  186. struct firmware_map_entry *entry;
  187. struct kset *memmap_kset;
  188. memmap_kset = kset_create_and_add("memmap", NULL, firmware_kobj);
  189. if (WARN_ON(!memmap_kset))
  190. return -ENOMEM;
  191. list_for_each_entry(entry, &map_entries, list) {
  192. entry->kobj.kset = memmap_kset;
  193. if (kobject_add(&entry->kobj, NULL, "%d", i++))
  194. kobject_put(&entry->kobj);
  195. }
  196. return 0;
  197. }
  198. late_initcall(memmap_init);