memmap.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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 <[email protected]>
  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. ATTRIBUTE_GROUPS(def);
  64. static const struct sysfs_ops memmap_attr_ops = {
  65. .show = memmap_attr_show,
  66. };
  67. /* Firmware memory map entries. */
  68. static LIST_HEAD(map_entries);
  69. static DEFINE_SPINLOCK(map_entries_lock);
  70. /*
  71. * For memory hotplug, there is no way to free memory map entries allocated
  72. * by boot mem after the system is up. So when we hot-remove memory whose
  73. * map entry is allocated by bootmem, we need to remember the storage and
  74. * reuse it when the memory is hot-added again.
  75. */
  76. static LIST_HEAD(map_entries_bootmem);
  77. static DEFINE_SPINLOCK(map_entries_bootmem_lock);
  78. static inline struct firmware_map_entry *
  79. to_memmap_entry(struct kobject *kobj)
  80. {
  81. return container_of(kobj, struct firmware_map_entry, kobj);
  82. }
  83. static void __meminit release_firmware_map_entry(struct kobject *kobj)
  84. {
  85. struct firmware_map_entry *entry = to_memmap_entry(kobj);
  86. if (PageReserved(virt_to_page(entry))) {
  87. /*
  88. * Remember the storage allocated by bootmem, and reuse it when
  89. * the memory is hot-added again. The entry will be added to
  90. * map_entries_bootmem here, and deleted from &map_entries in
  91. * firmware_map_remove_entry().
  92. */
  93. spin_lock(&map_entries_bootmem_lock);
  94. list_add(&entry->list, &map_entries_bootmem);
  95. spin_unlock(&map_entries_bootmem_lock);
  96. return;
  97. }
  98. kfree(entry);
  99. }
  100. static struct kobj_type __refdata memmap_ktype = {
  101. .release = release_firmware_map_entry,
  102. .sysfs_ops = &memmap_attr_ops,
  103. .default_groups = def_groups,
  104. };
  105. /*
  106. * Registration functions ------------------------------------------------------
  107. */
  108. /**
  109. * firmware_map_add_entry() - Does the real work to add a firmware memmap entry.
  110. * @start: Start of the memory range.
  111. * @end: End of the memory range (exclusive).
  112. * @type: Type of the memory range.
  113. * @entry: Pre-allocated (either kmalloc() or bootmem allocator), uninitialised
  114. * entry.
  115. *
  116. * Common implementation of firmware_map_add() and firmware_map_add_early()
  117. * which expects a pre-allocated struct firmware_map_entry.
  118. *
  119. * Return: 0 always
  120. */
  121. static int firmware_map_add_entry(u64 start, u64 end,
  122. const char *type,
  123. struct firmware_map_entry *entry)
  124. {
  125. BUG_ON(start > end);
  126. entry->start = start;
  127. entry->end = end - 1;
  128. entry->type = type;
  129. INIT_LIST_HEAD(&entry->list);
  130. kobject_init(&entry->kobj, &memmap_ktype);
  131. spin_lock(&map_entries_lock);
  132. list_add_tail(&entry->list, &map_entries);
  133. spin_unlock(&map_entries_lock);
  134. return 0;
  135. }
  136. /**
  137. * firmware_map_remove_entry() - Does the real work to remove a firmware
  138. * memmap entry.
  139. * @entry: removed entry.
  140. *
  141. * The caller must hold map_entries_lock, and release it properly.
  142. */
  143. static inline void firmware_map_remove_entry(struct firmware_map_entry *entry)
  144. {
  145. list_del(&entry->list);
  146. }
  147. /*
  148. * Add memmap entry on sysfs
  149. */
  150. static int add_sysfs_fw_map_entry(struct firmware_map_entry *entry)
  151. {
  152. static int map_entries_nr;
  153. static struct kset *mmap_kset;
  154. if (entry->kobj.state_in_sysfs)
  155. return -EEXIST;
  156. if (!mmap_kset) {
  157. mmap_kset = kset_create_and_add("memmap", NULL, firmware_kobj);
  158. if (!mmap_kset)
  159. return -ENOMEM;
  160. }
  161. entry->kobj.kset = mmap_kset;
  162. if (kobject_add(&entry->kobj, NULL, "%d", map_entries_nr++))
  163. kobject_put(&entry->kobj);
  164. return 0;
  165. }
  166. /*
  167. * Remove memmap entry on sysfs
  168. */
  169. static inline void remove_sysfs_fw_map_entry(struct firmware_map_entry *entry)
  170. {
  171. kobject_put(&entry->kobj);
  172. }
  173. /**
  174. * firmware_map_find_entry_in_list() - Search memmap entry in a given list.
  175. * @start: Start of the memory range.
  176. * @end: End of the memory range (exclusive).
  177. * @type: Type of the memory range.
  178. * @list: In which to find the entry.
  179. *
  180. * This function is to find the memmap entey of a given memory range in a
  181. * given list. The caller must hold map_entries_lock, and must not release
  182. * the lock until the processing of the returned entry has completed.
  183. *
  184. * Return: Pointer to the entry to be found on success, or NULL on failure.
  185. */
  186. static struct firmware_map_entry * __meminit
  187. firmware_map_find_entry_in_list(u64 start, u64 end, const char *type,
  188. struct list_head *list)
  189. {
  190. struct firmware_map_entry *entry;
  191. list_for_each_entry(entry, list, list)
  192. if ((entry->start == start) && (entry->end == end) &&
  193. (!strcmp(entry->type, type))) {
  194. return entry;
  195. }
  196. return NULL;
  197. }
  198. /**
  199. * firmware_map_find_entry() - Search memmap entry in map_entries.
  200. * @start: Start of the memory range.
  201. * @end: End of the memory range (exclusive).
  202. * @type: Type of the memory range.
  203. *
  204. * This function is to find the memmap entey of a given memory range.
  205. * The caller must hold map_entries_lock, and must not release the lock
  206. * until the processing of the returned entry has completed.
  207. *
  208. * Return: Pointer to the entry to be found on success, or NULL on failure.
  209. */
  210. static struct firmware_map_entry * __meminit
  211. firmware_map_find_entry(u64 start, u64 end, const char *type)
  212. {
  213. return firmware_map_find_entry_in_list(start, end, type, &map_entries);
  214. }
  215. /**
  216. * firmware_map_find_entry_bootmem() - Search memmap entry in map_entries_bootmem.
  217. * @start: Start of the memory range.
  218. * @end: End of the memory range (exclusive).
  219. * @type: Type of the memory range.
  220. *
  221. * This function is similar to firmware_map_find_entry except that it find the
  222. * given entry in map_entries_bootmem.
  223. *
  224. * Return: Pointer to the entry to be found on success, or NULL on failure.
  225. */
  226. static struct firmware_map_entry * __meminit
  227. firmware_map_find_entry_bootmem(u64 start, u64 end, const char *type)
  228. {
  229. return firmware_map_find_entry_in_list(start, end, type,
  230. &map_entries_bootmem);
  231. }
  232. /**
  233. * firmware_map_add_hotplug() - Adds a firmware mapping entry when we do
  234. * memory hotplug.
  235. * @start: Start of the memory range.
  236. * @end: End of the memory range (exclusive)
  237. * @type: Type of the memory range.
  238. *
  239. * Adds a firmware mapping entry. This function is for memory hotplug, it is
  240. * similar to function firmware_map_add_early(). The only difference is that
  241. * it will create the syfs entry dynamically.
  242. *
  243. * Return: 0 on success, or -ENOMEM if no memory could be allocated.
  244. */
  245. int __meminit firmware_map_add_hotplug(u64 start, u64 end, const char *type)
  246. {
  247. struct firmware_map_entry *entry;
  248. entry = firmware_map_find_entry(start, end - 1, type);
  249. if (entry)
  250. return 0;
  251. entry = firmware_map_find_entry_bootmem(start, end - 1, type);
  252. if (!entry) {
  253. entry = kzalloc(sizeof(struct firmware_map_entry), GFP_ATOMIC);
  254. if (!entry)
  255. return -ENOMEM;
  256. } else {
  257. /* Reuse storage allocated by bootmem. */
  258. spin_lock(&map_entries_bootmem_lock);
  259. list_del(&entry->list);
  260. spin_unlock(&map_entries_bootmem_lock);
  261. memset(entry, 0, sizeof(*entry));
  262. }
  263. firmware_map_add_entry(start, end, type, entry);
  264. /* create the memmap entry */
  265. add_sysfs_fw_map_entry(entry);
  266. return 0;
  267. }
  268. /**
  269. * firmware_map_add_early() - Adds a firmware mapping entry.
  270. * @start: Start of the memory range.
  271. * @end: End of the memory range.
  272. * @type: Type of the memory range.
  273. *
  274. * Adds a firmware mapping entry. This function uses the bootmem allocator
  275. * for memory allocation.
  276. *
  277. * That function must be called before late_initcall.
  278. *
  279. * Return: 0 on success, or -ENOMEM if no memory could be allocated.
  280. */
  281. int __init firmware_map_add_early(u64 start, u64 end, const char *type)
  282. {
  283. struct firmware_map_entry *entry;
  284. entry = memblock_alloc(sizeof(struct firmware_map_entry),
  285. SMP_CACHE_BYTES);
  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. * Return: 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);