memmap.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. if (firmware_map_find_entry(entry->start, entry->end,
  102. entry->type)) {
  103. spin_lock(&map_entries_bootmem_lock);
  104. list_add(&entry->list, &map_entries_bootmem);
  105. spin_unlock(&map_entries_bootmem_lock);
  106. }
  107. return;
  108. }
  109. kfree(entry);
  110. }
  111. static struct kobj_type __refdata memmap_ktype = {
  112. .release = release_firmware_map_entry,
  113. .sysfs_ops = &memmap_attr_ops,
  114. .default_attrs = def_attrs,
  115. };
  116. /*
  117. * Registration functions ------------------------------------------------------
  118. */
  119. /**
  120. * firmware_map_add_entry() - Does the real work to add a firmware memmap entry.
  121. * @start: Start of the memory range.
  122. * @end: End of the memory range (exclusive).
  123. * @type: Type of the memory range.
  124. * @entry: Pre-allocated (either kmalloc() or bootmem allocator), uninitialised
  125. * entry.
  126. *
  127. * Common implementation of firmware_map_add() and firmware_map_add_early()
  128. * which expects a pre-allocated struct firmware_map_entry.
  129. **/
  130. static int firmware_map_add_entry(u64 start, u64 end,
  131. const char *type,
  132. struct firmware_map_entry *entry)
  133. {
  134. BUG_ON(start > end);
  135. entry->start = start;
  136. entry->end = end - 1;
  137. entry->type = type;
  138. INIT_LIST_HEAD(&entry->list);
  139. kobject_init(&entry->kobj, &memmap_ktype);
  140. spin_lock(&map_entries_lock);
  141. list_add_tail(&entry->list, &map_entries);
  142. spin_unlock(&map_entries_lock);
  143. return 0;
  144. }
  145. /**
  146. * firmware_map_remove_entry() - Does the real work to remove a firmware
  147. * memmap entry.
  148. * @entry: removed entry.
  149. *
  150. * The caller must hold map_entries_lock, and release it properly.
  151. **/
  152. static inline void firmware_map_remove_entry(struct firmware_map_entry *entry)
  153. {
  154. list_del(&entry->list);
  155. }
  156. /*
  157. * Add memmap entry on sysfs
  158. */
  159. static int add_sysfs_fw_map_entry(struct firmware_map_entry *entry)
  160. {
  161. static int map_entries_nr;
  162. static struct kset *mmap_kset;
  163. if (!mmap_kset) {
  164. mmap_kset = kset_create_and_add("memmap", NULL, firmware_kobj);
  165. if (!mmap_kset)
  166. return -ENOMEM;
  167. }
  168. entry->kobj.kset = mmap_kset;
  169. if (kobject_add(&entry->kobj, NULL, "%d", map_entries_nr++))
  170. kobject_put(&entry->kobj);
  171. return 0;
  172. }
  173. /*
  174. * Remove memmap entry on sysfs
  175. */
  176. static inline void remove_sysfs_fw_map_entry(struct firmware_map_entry *entry)
  177. {
  178. kobject_put(&entry->kobj);
  179. }
  180. /*
  181. * firmware_map_find_entry_in_list() - Search memmap entry in a given list.
  182. * @start: Start of the memory range.
  183. * @end: End of the memory range (exclusive).
  184. * @type: Type of the memory range.
  185. * @list: In which to find the entry.
  186. *
  187. * This function is to find the memmap entey of a given memory range in a
  188. * given list. The caller must hold map_entries_lock, and must not release
  189. * the lock until the processing of the returned entry has completed.
  190. *
  191. * Return: Pointer to the entry to be found on success, or NULL on failure.
  192. */
  193. static struct firmware_map_entry * __meminit
  194. firmware_map_find_entry_in_list(u64 start, u64 end, const char *type,
  195. struct list_head *list)
  196. {
  197. struct firmware_map_entry *entry;
  198. list_for_each_entry(entry, list, list)
  199. if ((entry->start == start) && (entry->end == end) &&
  200. (!strcmp(entry->type, type))) {
  201. return entry;
  202. }
  203. return NULL;
  204. }
  205. /*
  206. * firmware_map_find_entry() - Search memmap entry in map_entries.
  207. * @start: Start of the memory range.
  208. * @end: End of the memory range (exclusive).
  209. * @type: Type of the memory range.
  210. *
  211. * This function is to find the memmap entey of a given memory range.
  212. * The caller must hold map_entries_lock, and must not release the lock
  213. * until the processing of the returned entry has completed.
  214. *
  215. * Return: Pointer to the entry to be found on success, or NULL on failure.
  216. */
  217. static struct firmware_map_entry * __meminit
  218. firmware_map_find_entry(u64 start, u64 end, const char *type)
  219. {
  220. return firmware_map_find_entry_in_list(start, end, type, &map_entries);
  221. }
  222. /*
  223. * firmware_map_find_entry_bootmem() - Search memmap entry in map_entries_bootmem.
  224. * @start: Start of the memory range.
  225. * @end: End of the memory range (exclusive).
  226. * @type: Type of the memory range.
  227. *
  228. * This function is similar to firmware_map_find_entry except that it find the
  229. * given entry in map_entries_bootmem.
  230. *
  231. * Return: Pointer to the entry to be found on success, or NULL on failure.
  232. */
  233. static struct firmware_map_entry * __meminit
  234. firmware_map_find_entry_bootmem(u64 start, u64 end, const char *type)
  235. {
  236. return firmware_map_find_entry_in_list(start, end, type,
  237. &map_entries_bootmem);
  238. }
  239. /**
  240. * firmware_map_add_hotplug() - Adds a firmware mapping entry when we do
  241. * memory hotplug.
  242. * @start: Start of the memory range.
  243. * @end: End of the memory range (exclusive)
  244. * @type: Type of the memory range.
  245. *
  246. * Adds a firmware mapping entry. This function is for memory hotplug, it is
  247. * similar to function firmware_map_add_early(). The only difference is that
  248. * it will create the syfs entry dynamically.
  249. *
  250. * Returns 0 on success, or -ENOMEM if no memory could be allocated.
  251. **/
  252. int __meminit firmware_map_add_hotplug(u64 start, u64 end, const char *type)
  253. {
  254. struct firmware_map_entry *entry;
  255. entry = firmware_map_find_entry_bootmem(start, end, type);
  256. if (!entry) {
  257. entry = kzalloc(sizeof(struct firmware_map_entry), GFP_ATOMIC);
  258. if (!entry)
  259. return -ENOMEM;
  260. } else {
  261. /* Reuse storage allocated by bootmem. */
  262. spin_lock(&map_entries_bootmem_lock);
  263. list_del(&entry->list);
  264. spin_unlock(&map_entries_bootmem_lock);
  265. memset(entry, 0, sizeof(*entry));
  266. }
  267. firmware_map_add_entry(start, end, type, entry);
  268. /* create the memmap entry */
  269. add_sysfs_fw_map_entry(entry);
  270. return 0;
  271. }
  272. /**
  273. * firmware_map_add_early() - Adds a firmware mapping entry.
  274. * @start: Start of the memory range.
  275. * @end: End of the memory range.
  276. * @type: Type of the memory range.
  277. *
  278. * Adds a firmware mapping entry. This function uses the bootmem allocator
  279. * for memory allocation.
  280. *
  281. * That function must be called before late_initcall.
  282. *
  283. * Returns 0 on success, or -ENOMEM if no memory could be allocated.
  284. **/
  285. int __init firmware_map_add_early(u64 start, u64 end, const char *type)
  286. {
  287. struct firmware_map_entry *entry;
  288. entry = alloc_bootmem(sizeof(struct firmware_map_entry));
  289. if (WARN_ON(!entry))
  290. return -ENOMEM;
  291. return firmware_map_add_entry(start, end, type, entry);
  292. }
  293. /**
  294. * firmware_map_remove() - remove a firmware mapping entry
  295. * @start: Start of the memory range.
  296. * @end: End of the memory range.
  297. * @type: Type of the memory range.
  298. *
  299. * removes a firmware mapping entry.
  300. *
  301. * Returns 0 on success, or -EINVAL if no entry.
  302. **/
  303. int __meminit firmware_map_remove(u64 start, u64 end, const char *type)
  304. {
  305. struct firmware_map_entry *entry;
  306. spin_lock(&map_entries_lock);
  307. entry = firmware_map_find_entry(start, end - 1, type);
  308. if (!entry) {
  309. spin_unlock(&map_entries_lock);
  310. return -EINVAL;
  311. }
  312. firmware_map_remove_entry(entry);
  313. spin_unlock(&map_entries_lock);
  314. /* remove the memmap entry */
  315. remove_sysfs_fw_map_entry(entry);
  316. return 0;
  317. }
  318. /*
  319. * Sysfs functions -------------------------------------------------------------
  320. */
  321. static ssize_t start_show(struct firmware_map_entry *entry, char *buf)
  322. {
  323. return snprintf(buf, PAGE_SIZE, "0x%llx\n",
  324. (unsigned long long)entry->start);
  325. }
  326. static ssize_t end_show(struct firmware_map_entry *entry, char *buf)
  327. {
  328. return snprintf(buf, PAGE_SIZE, "0x%llx\n",
  329. (unsigned long long)entry->end);
  330. }
  331. static ssize_t type_show(struct firmware_map_entry *entry, char *buf)
  332. {
  333. return snprintf(buf, PAGE_SIZE, "%s\n", entry->type);
  334. }
  335. static inline struct memmap_attribute *to_memmap_attr(struct attribute *attr)
  336. {
  337. return container_of(attr, struct memmap_attribute, attr);
  338. }
  339. static ssize_t memmap_attr_show(struct kobject *kobj,
  340. struct attribute *attr, char *buf)
  341. {
  342. struct firmware_map_entry *entry = to_memmap_entry(kobj);
  343. struct memmap_attribute *memmap_attr = to_memmap_attr(attr);
  344. return memmap_attr->show(entry, buf);
  345. }
  346. /*
  347. * Initialises stuff and adds the entries in the map_entries list to
  348. * sysfs. Important is that firmware_map_add() and firmware_map_add_early()
  349. * must be called before late_initcall. That's just because that function
  350. * is called as late_initcall() function, which means that if you call
  351. * firmware_map_add() or firmware_map_add_early() afterwards, the entries
  352. * are not added to sysfs.
  353. */
  354. static int __init firmware_memmap_init(void)
  355. {
  356. struct firmware_map_entry *entry;
  357. list_for_each_entry(entry, &map_entries, list)
  358. add_sysfs_fw_map_entry(entry);
  359. return 0;
  360. }
  361. late_initcall(firmware_memmap_init);