memmap.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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
  76. */
  77. static LIST_HEAD(map_entries);
  78. /**
  79. * Common implementation of firmware_map_add() and firmware_map_add_early()
  80. * which expects a pre-allocated struct firmware_map_entry.
  81. *
  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. static int firmware_map_add_entry(resource_size_t start, resource_size_t end,
  89. const char *type,
  90. struct firmware_map_entry *entry)
  91. {
  92. BUG_ON(start > end);
  93. entry->start = start;
  94. entry->end = end;
  95. entry->type = type;
  96. INIT_LIST_HEAD(&entry->list);
  97. kobject_init(&entry->kobj, &memmap_ktype);
  98. list_add_tail(&entry->list, &map_entries);
  99. return 0;
  100. }
  101. /*
  102. * See <linux/firmware-map.h> for documentation.
  103. */
  104. int firmware_map_add(resource_size_t start, resource_size_t end,
  105. const char *type)
  106. {
  107. struct firmware_map_entry *entry;
  108. entry = kmalloc(sizeof(struct firmware_map_entry), GFP_ATOMIC);
  109. WARN_ON(!entry);
  110. if (!entry)
  111. return -ENOMEM;
  112. return firmware_map_add_entry(start, end, type, entry);
  113. }
  114. /*
  115. * See <linux/firmware-map.h> for documentation.
  116. */
  117. int __init firmware_map_add_early(resource_size_t start, resource_size_t end,
  118. const char *type)
  119. {
  120. struct firmware_map_entry *entry;
  121. entry = alloc_bootmem_low(sizeof(struct firmware_map_entry));
  122. WARN_ON(!entry);
  123. if (!entry)
  124. return -ENOMEM;
  125. return firmware_map_add_entry(start, end, type, entry);
  126. }
  127. /*
  128. * Sysfs functions -------------------------------------------------------------
  129. */
  130. static ssize_t start_show(struct firmware_map_entry *entry, char *buf)
  131. {
  132. return snprintf(buf, PAGE_SIZE, "0x%llx\n", entry->start);
  133. }
  134. static ssize_t end_show(struct firmware_map_entry *entry, char *buf)
  135. {
  136. return snprintf(buf, PAGE_SIZE, "0x%llx\n", entry->end);
  137. }
  138. static ssize_t type_show(struct firmware_map_entry *entry, char *buf)
  139. {
  140. return snprintf(buf, PAGE_SIZE, "%s\n", entry->type);
  141. }
  142. #define to_memmap_attr(_attr) container_of(_attr, struct memmap_attribute, attr)
  143. #define to_memmap_entry(obj) container_of(obj, struct firmware_map_entry, kobj)
  144. static ssize_t memmap_attr_show(struct kobject *kobj,
  145. struct attribute *attr, char *buf)
  146. {
  147. struct firmware_map_entry *entry = to_memmap_entry(kobj);
  148. struct memmap_attribute *memmap_attr = to_memmap_attr(attr);
  149. return memmap_attr->show(entry, buf);
  150. }
  151. /*
  152. * Initialises stuff and adds the entries in the map_entries list to
  153. * sysfs. Important is that firmware_map_add() and firmware_map_add_early()
  154. * must be called before late_initcall.
  155. */
  156. static int __init memmap_init(void)
  157. {
  158. int i = 0;
  159. struct firmware_map_entry *entry;
  160. struct kset *memmap_kset;
  161. memmap_kset = kset_create_and_add("memmap", NULL, firmware_kobj);
  162. WARN_ON(!memmap_kset);
  163. if (!memmap_kset)
  164. return -ENOMEM;
  165. list_for_each_entry(entry, &map_entries, list) {
  166. entry->kobj.kset = memmap_kset;
  167. kobject_add(&entry->kobj, NULL, "%d", i++);
  168. }
  169. return 0;
  170. }
  171. late_initcall(memmap_init);