memmap.c 12 KB

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