memmap.c 12 KB

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