memmap.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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. #include <linux/mm.h>
  24. /*
  25. * Data types ------------------------------------------------------------------
  26. */
  27. /*
  28. * Firmware map entry. Because firmware memory maps are flat and not
  29. * hierarchical, it's ok to organise them in a linked list. No parent
  30. * information is necessary as for the resource tree.
  31. */
  32. struct firmware_map_entry {
  33. /*
  34. * start and end must be u64 rather than resource_size_t, because e820
  35. * resources can lie at addresses above 4G.
  36. */
  37. u64 start; /* start of the memory range */
  38. u64 end; /* end of the memory range (incl.) */
  39. const char *type; /* type of the memory range */
  40. struct list_head list; /* entry for the linked list */
  41. struct kobject kobj; /* kobject for each entry */
  42. };
  43. /*
  44. * Forward declarations --------------------------------------------------------
  45. */
  46. static ssize_t memmap_attr_show(struct kobject *kobj,
  47. struct attribute *attr, char *buf);
  48. static ssize_t start_show(struct firmware_map_entry *entry, char *buf);
  49. static ssize_t end_show(struct firmware_map_entry *entry, char *buf);
  50. static ssize_t type_show(struct firmware_map_entry *entry, char *buf);
  51. static struct firmware_map_entry * __meminit
  52. firmware_map_find_entry(u64 start, u64 end, const char *type);
  53. /*
  54. * Static data -----------------------------------------------------------------
  55. */
  56. struct memmap_attribute {
  57. struct attribute attr;
  58. ssize_t (*show)(struct firmware_map_entry *entry, char *buf);
  59. };
  60. static struct memmap_attribute memmap_start_attr = __ATTR_RO(start);
  61. static struct memmap_attribute memmap_end_attr = __ATTR_RO(end);
  62. static struct memmap_attribute memmap_type_attr = __ATTR_RO(type);
  63. /*
  64. * These are default attributes that are added for every memmap entry.
  65. */
  66. static struct attribute *def_attrs[] = {
  67. &memmap_start_attr.attr,
  68. &memmap_end_attr.attr,
  69. &memmap_type_attr.attr,
  70. NULL
  71. };
  72. static const struct sysfs_ops memmap_attr_ops = {
  73. .show = memmap_attr_show,
  74. };
  75. /* Firmware memory map entries. */
  76. static LIST_HEAD(map_entries);
  77. static DEFINE_SPINLOCK(map_entries_lock);
  78. /*
  79. * For memory hotplug, there is no way to free memory map entries allocated
  80. * by boot mem after the system is up. So when we hot-remove memory whose
  81. * map entry is allocated by bootmem, we need to remember the storage and
  82. * reuse it when the memory is hot-added again.
  83. */
  84. static LIST_HEAD(map_entries_bootmem);
  85. static DEFINE_SPINLOCK(map_entries_bootmem_lock);
  86. static inline struct firmware_map_entry *
  87. to_memmap_entry(struct kobject *kobj)
  88. {
  89. return container_of(kobj, struct firmware_map_entry, kobj);
  90. }
  91. static void __meminit release_firmware_map_entry(struct kobject *kobj)
  92. {
  93. struct firmware_map_entry *entry = to_memmap_entry(kobj);
  94. if (PageReserved(virt_to_page(entry))) {
  95. /*
  96. * Remember the storage allocated by bootmem, and reuse it when
  97. * the memory is hot-added again. The entry will be added to
  98. * map_entries_bootmem here, and deleted from &map_entries in
  99. * firmware_map_remove_entry().
  100. */
  101. spin_lock(&map_entries_bootmem_lock);
  102. list_add(&entry->list, &map_entries_bootmem);
  103. spin_unlock(&map_entries_bootmem_lock);
  104. return;
  105. }
  106. kfree(entry);
  107. }
  108. static struct kobj_type __refdata memmap_ktype = {
  109. .release = release_firmware_map_entry,
  110. .sysfs_ops = &memmap_attr_ops,
  111. .default_attrs = def_attrs,
  112. };
  113. /*
  114. * Registration functions ------------------------------------------------------
  115. */
  116. /**
  117. * firmware_map_add_entry() - Does the real work to add a firmware memmap entry.
  118. * @start: Start of the memory range.
  119. * @end: End of the memory range (exclusive).
  120. * @type: Type of the memory range.
  121. * @entry: Pre-allocated (either kmalloc() or bootmem allocator), uninitialised
  122. * entry.
  123. *
  124. * Common implementation of firmware_map_add() and firmware_map_add_early()
  125. * which expects a pre-allocated struct firmware_map_entry.
  126. **/
  127. static int firmware_map_add_entry(u64 start, u64 end,
  128. const char *type,
  129. struct firmware_map_entry *entry)
  130. {
  131. BUG_ON(start > end);
  132. entry->start = start;
  133. entry->end = end - 1;
  134. entry->type = type;
  135. INIT_LIST_HEAD(&entry->list);
  136. kobject_init(&entry->kobj, &memmap_ktype);
  137. spin_lock(&map_entries_lock);
  138. list_add_tail(&entry->list, &map_entries);
  139. spin_unlock(&map_entries_lock);
  140. return 0;
  141. }
  142. /**
  143. * firmware_map_remove_entry() - Does the real work to remove a firmware
  144. * memmap entry.
  145. * @entry: removed entry.
  146. *
  147. * The caller must hold map_entries_lock, and release it properly.
  148. **/
  149. static inline void firmware_map_remove_entry(struct firmware_map_entry *entry)
  150. {
  151. list_del(&entry->list);
  152. }
  153. /*
  154. * Add memmap entry on sysfs
  155. */
  156. static int add_sysfs_fw_map_entry(struct firmware_map_entry *entry)
  157. {
  158. static int map_entries_nr;
  159. static struct kset *mmap_kset;
  160. if (!mmap_kset) {
  161. mmap_kset = kset_create_and_add("memmap", NULL, firmware_kobj);
  162. if (!mmap_kset)
  163. return -ENOMEM;
  164. }
  165. entry->kobj.kset = mmap_kset;
  166. if (kobject_add(&entry->kobj, NULL, "%d", map_entries_nr++))
  167. kobject_put(&entry->kobj);
  168. return 0;
  169. }
  170. /*
  171. * Remove memmap entry on sysfs
  172. */
  173. static inline void remove_sysfs_fw_map_entry(struct firmware_map_entry *entry)
  174. {
  175. kobject_put(&entry->kobj);
  176. }
  177. /*
  178. * firmware_map_find_entry_in_list() - Search memmap entry in a given list.
  179. * @start: Start of the memory range.
  180. * @end: End of the memory range (exclusive).
  181. * @type: Type of the memory range.
  182. * @list: In which to find the entry.
  183. *
  184. * This function is to find the memmap entey of a given memory range in a
  185. * given list. The caller must hold map_entries_lock, and must not release
  186. * the lock until the processing of the returned entry has completed.
  187. *
  188. * Return: Pointer to the entry to be found on success, or NULL on failure.
  189. */
  190. static struct firmware_map_entry * __meminit
  191. firmware_map_find_entry_in_list(u64 start, u64 end, const char *type,
  192. struct list_head *list)
  193. {
  194. struct firmware_map_entry *entry;
  195. list_for_each_entry(entry, list, list)
  196. if ((entry->start == start) && (entry->end == end) &&
  197. (!strcmp(entry->type, type))) {
  198. return entry;
  199. }
  200. return NULL;
  201. }
  202. /*
  203. * firmware_map_find_entry() - Search memmap entry in map_entries.
  204. * @start: Start of the memory range.
  205. * @end: End of the memory range (exclusive).
  206. * @type: Type of the memory range.
  207. *
  208. * This function is to find the memmap entey of a given memory range.
  209. * The caller must hold map_entries_lock, and must not release the lock
  210. * until the processing of the returned entry has completed.
  211. *
  212. * Return: Pointer to the entry to be found on success, or NULL on failure.
  213. */
  214. static struct firmware_map_entry * __meminit
  215. firmware_map_find_entry(u64 start, u64 end, const char *type)
  216. {
  217. return firmware_map_find_entry_in_list(start, end, type, &map_entries);
  218. }
  219. /*
  220. * firmware_map_find_entry_bootmem() - Search memmap entry in map_entries_bootmem.
  221. * @start: Start of the memory range.
  222. * @end: End of the memory range (exclusive).
  223. * @type: Type of the memory range.
  224. *
  225. * This function is similar to firmware_map_find_entry except that it find the
  226. * given entry in map_entries_bootmem.
  227. *
  228. * Return: Pointer to the entry to be found on success, or NULL on failure.
  229. */
  230. static struct firmware_map_entry * __meminit
  231. firmware_map_find_entry_bootmem(u64 start, u64 end, const char *type)
  232. {
  233. return firmware_map_find_entry_in_list(start, end, type,
  234. &map_entries_bootmem);
  235. }
  236. /**
  237. * firmware_map_add_hotplug() - Adds a firmware mapping entry when we do
  238. * memory hotplug.
  239. * @start: Start of the memory range.
  240. * @end: End of the memory range (exclusive)
  241. * @type: Type of the memory range.
  242. *
  243. * Adds a firmware mapping entry. This function is for memory hotplug, it is
  244. * similar to function firmware_map_add_early(). The only difference is that
  245. * it will create the syfs entry dynamically.
  246. *
  247. * Returns 0 on success, or -ENOMEM if no memory could be allocated.
  248. **/
  249. int __meminit firmware_map_add_hotplug(u64 start, u64 end, const char *type)
  250. {
  251. struct firmware_map_entry *entry;
  252. entry = firmware_map_find_entry_bootmem(start, end, type);
  253. if (!entry) {
  254. entry = kzalloc(sizeof(struct firmware_map_entry), GFP_ATOMIC);
  255. if (!entry)
  256. return -ENOMEM;
  257. } else {
  258. /* Reuse storage allocated by bootmem. */
  259. spin_lock(&map_entries_bootmem_lock);
  260. list_del(&entry->list);
  261. spin_unlock(&map_entries_bootmem_lock);
  262. memset(entry, 0, sizeof(*entry));
  263. }
  264. firmware_map_add_entry(start, end, type, entry);
  265. /* create the memmap entry */
  266. add_sysfs_fw_map_entry(entry);
  267. return 0;
  268. }
  269. /**
  270. * firmware_map_add_early() - Adds a firmware mapping entry.
  271. * @start: Start of the memory range.
  272. * @end: End of the memory range.
  273. * @type: Type of the memory range.
  274. *
  275. * Adds a firmware mapping entry. This function uses the bootmem allocator
  276. * for memory allocation.
  277. *
  278. * That function must be called before late_initcall.
  279. *
  280. * Returns 0 on success, or -ENOMEM if no memory could be allocated.
  281. **/
  282. int __init firmware_map_add_early(u64 start, u64 end, const char *type)
  283. {
  284. struct firmware_map_entry *entry;
  285. entry = alloc_bootmem(sizeof(struct firmware_map_entry));
  286. if (WARN_ON(!entry))
  287. return -ENOMEM;
  288. return firmware_map_add_entry(start, end, type, entry);
  289. }
  290. /**
  291. * firmware_map_remove() - remove a firmware mapping entry
  292. * @start: Start of the memory range.
  293. * @end: End of the memory range.
  294. * @type: Type of the memory range.
  295. *
  296. * removes a firmware mapping entry.
  297. *
  298. * Returns 0 on success, or -EINVAL if no entry.
  299. **/
  300. int __meminit firmware_map_remove(u64 start, u64 end, const char *type)
  301. {
  302. struct firmware_map_entry *entry;
  303. spin_lock(&map_entries_lock);
  304. entry = firmware_map_find_entry(start, end - 1, type);
  305. if (!entry) {
  306. spin_unlock(&map_entries_lock);
  307. return -EINVAL;
  308. }
  309. firmware_map_remove_entry(entry);
  310. spin_unlock(&map_entries_lock);
  311. /* remove the memmap entry */
  312. remove_sysfs_fw_map_entry(entry);
  313. return 0;
  314. }
  315. /*
  316. * Sysfs functions -------------------------------------------------------------
  317. */
  318. static ssize_t start_show(struct firmware_map_entry *entry, char *buf)
  319. {
  320. return snprintf(buf, PAGE_SIZE, "0x%llx\n",
  321. (unsigned long long)entry->start);
  322. }
  323. static ssize_t end_show(struct firmware_map_entry *entry, char *buf)
  324. {
  325. return snprintf(buf, PAGE_SIZE, "0x%llx\n",
  326. (unsigned long long)entry->end);
  327. }
  328. static ssize_t type_show(struct firmware_map_entry *entry, char *buf)
  329. {
  330. return snprintf(buf, PAGE_SIZE, "%s\n", entry->type);
  331. }
  332. static inline struct memmap_attribute *to_memmap_attr(struct attribute *attr)
  333. {
  334. return container_of(attr, struct memmap_attribute, attr);
  335. }
  336. static ssize_t memmap_attr_show(struct kobject *kobj,
  337. struct attribute *attr, char *buf)
  338. {
  339. struct firmware_map_entry *entry = to_memmap_entry(kobj);
  340. struct memmap_attribute *memmap_attr = to_memmap_attr(attr);
  341. return memmap_attr->show(entry, buf);
  342. }
  343. /*
  344. * Initialises stuff and adds the entries in the map_entries list to
  345. * sysfs. Important is that firmware_map_add() and firmware_map_add_early()
  346. * must be called before late_initcall. That's just because that function
  347. * is called as late_initcall() function, which means that if you call
  348. * firmware_map_add() or firmware_map_add_early() afterwards, the entries
  349. * are not added to sysfs.
  350. */
  351. static int __init firmware_memmap_init(void)
  352. {
  353. struct firmware_map_entry *entry;
  354. list_for_each_entry(entry, &map_entries, list)
  355. add_sysfs_fw_map_entry(entry);
  356. return 0;
  357. }
  358. late_initcall(firmware_memmap_init);