of_reserved_mem.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Device tree based initialization code for reserved memory.
  4. *
  5. * Copyright (c) 2013, 2015 The Linux Foundation. All Rights Reserved.
  6. * Copyright (c) 2013,2014 Samsung Electronics Co., Ltd.
  7. * http://www.samsung.com
  8. * Author: Marek Szyprowski <m.szyprowski@samsung.com>
  9. * Author: Josh Cartwright <joshc@codeaurora.org>
  10. */
  11. #define pr_fmt(fmt) "OF: reserved mem: " fmt
  12. #include <linux/err.h>
  13. #include <linux/of.h>
  14. #include <linux/of_fdt.h>
  15. #include <linux/of_platform.h>
  16. #include <linux/mm.h>
  17. #include <linux/sizes.h>
  18. #include <linux/of_reserved_mem.h>
  19. #include <linux/sort.h>
  20. #include <linux/slab.h>
  21. #define MAX_RESERVED_REGIONS 32
  22. static struct reserved_mem reserved_mem[MAX_RESERVED_REGIONS];
  23. static int reserved_mem_count;
  24. #if defined(CONFIG_HAVE_MEMBLOCK)
  25. #include <linux/memblock.h>
  26. int __init __weak early_init_dt_alloc_reserved_memory_arch(phys_addr_t size,
  27. phys_addr_t align, phys_addr_t start, phys_addr_t end, bool nomap,
  28. phys_addr_t *res_base)
  29. {
  30. phys_addr_t base;
  31. /*
  32. * We use __memblock_alloc_base() because memblock_alloc_base()
  33. * panic()s on allocation failure.
  34. */
  35. end = !end ? MEMBLOCK_ALLOC_ANYWHERE : end;
  36. base = __memblock_alloc_base(size, align, end);
  37. if (!base)
  38. return -ENOMEM;
  39. /*
  40. * Check if the allocated region fits in to start..end window
  41. */
  42. if (base < start) {
  43. memblock_free(base, size);
  44. return -ENOMEM;
  45. }
  46. *res_base = base;
  47. if (nomap)
  48. return memblock_remove(base, size);
  49. return 0;
  50. }
  51. #else
  52. int __init __weak early_init_dt_alloc_reserved_memory_arch(phys_addr_t size,
  53. phys_addr_t align, phys_addr_t start, phys_addr_t end, bool nomap,
  54. phys_addr_t *res_base)
  55. {
  56. pr_err("Reserved memory not supported, ignoring region 0x%llx%s\n",
  57. size, nomap ? " (nomap)" : "");
  58. return -ENOSYS;
  59. }
  60. #endif
  61. /**
  62. * res_mem_save_node() - save fdt node for second pass initialization
  63. */
  64. void __init fdt_reserved_mem_save_node(unsigned long node, const char *uname,
  65. phys_addr_t base, phys_addr_t size)
  66. {
  67. struct reserved_mem *rmem = &reserved_mem[reserved_mem_count];
  68. if (reserved_mem_count == ARRAY_SIZE(reserved_mem)) {
  69. pr_err("not enough space all defined regions.\n");
  70. return;
  71. }
  72. rmem->fdt_node = node;
  73. rmem->name = uname;
  74. rmem->base = base;
  75. rmem->size = size;
  76. reserved_mem_count++;
  77. return;
  78. }
  79. /**
  80. * res_mem_alloc_size() - allocate reserved memory described by 'size', 'align'
  81. * and 'alloc-ranges' properties
  82. */
  83. static int __init __reserved_mem_alloc_size(unsigned long node,
  84. const char *uname, phys_addr_t *res_base, phys_addr_t *res_size)
  85. {
  86. int t_len = (dt_root_addr_cells + dt_root_size_cells) * sizeof(__be32);
  87. phys_addr_t start = 0, end = 0;
  88. phys_addr_t base = 0, align = 0, size;
  89. int len;
  90. const __be32 *prop;
  91. int nomap;
  92. int ret;
  93. prop = of_get_flat_dt_prop(node, "size", &len);
  94. if (!prop)
  95. return -EINVAL;
  96. if (len != dt_root_size_cells * sizeof(__be32)) {
  97. pr_err("invalid size property in '%s' node.\n", uname);
  98. return -EINVAL;
  99. }
  100. size = dt_mem_next_cell(dt_root_size_cells, &prop);
  101. nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL;
  102. prop = of_get_flat_dt_prop(node, "alignment", &len);
  103. if (prop) {
  104. if (len != dt_root_addr_cells * sizeof(__be32)) {
  105. pr_err("invalid alignment property in '%s' node.\n",
  106. uname);
  107. return -EINVAL;
  108. }
  109. align = dt_mem_next_cell(dt_root_addr_cells, &prop);
  110. }
  111. /* Need adjust the alignment to satisfy the CMA requirement */
  112. if (IS_ENABLED(CONFIG_CMA)
  113. && of_flat_dt_is_compatible(node, "shared-dma-pool")
  114. && of_get_flat_dt_prop(node, "reusable", NULL)
  115. && !of_get_flat_dt_prop(node, "no-map", NULL)) {
  116. unsigned long order =
  117. max_t(unsigned long, MAX_ORDER - 1, pageblock_order);
  118. align = max(align, (phys_addr_t)PAGE_SIZE << order);
  119. }
  120. prop = of_get_flat_dt_prop(node, "alloc-ranges", &len);
  121. if (prop) {
  122. if (len % t_len != 0) {
  123. pr_err("invalid alloc-ranges property in '%s', skipping node.\n",
  124. uname);
  125. return -EINVAL;
  126. }
  127. base = 0;
  128. while (len > 0) {
  129. start = dt_mem_next_cell(dt_root_addr_cells, &prop);
  130. end = start + dt_mem_next_cell(dt_root_size_cells,
  131. &prop);
  132. ret = early_init_dt_alloc_reserved_memory_arch(size,
  133. align, start, end, nomap, &base);
  134. if (ret == 0) {
  135. pr_debug("allocated memory for '%s' node: base %pa, size %ld MiB\n",
  136. uname, &base,
  137. (unsigned long)size / SZ_1M);
  138. break;
  139. }
  140. len -= t_len;
  141. }
  142. } else {
  143. ret = early_init_dt_alloc_reserved_memory_arch(size, align,
  144. 0, 0, nomap, &base);
  145. if (ret == 0)
  146. pr_debug("allocated memory for '%s' node: base %pa, size %ld MiB\n",
  147. uname, &base, (unsigned long)size / SZ_1M);
  148. }
  149. if (base == 0) {
  150. pr_info("failed to allocate memory for node '%s'\n", uname);
  151. return -ENOMEM;
  152. }
  153. *res_base = base;
  154. *res_size = size;
  155. return 0;
  156. }
  157. static const struct of_device_id __rmem_of_table_sentinel
  158. __used __section(__reservedmem_of_table_end);
  159. /**
  160. * res_mem_init_node() - call region specific reserved memory init code
  161. */
  162. static int __init __reserved_mem_init_node(struct reserved_mem *rmem)
  163. {
  164. extern const struct of_device_id __reservedmem_of_table[];
  165. const struct of_device_id *i;
  166. for (i = __reservedmem_of_table; i < &__rmem_of_table_sentinel; i++) {
  167. reservedmem_of_init_fn initfn = i->data;
  168. const char *compat = i->compatible;
  169. if (!of_flat_dt_is_compatible(rmem->fdt_node, compat))
  170. continue;
  171. if (initfn(rmem) == 0) {
  172. pr_info("initialized node %s, compatible id %s\n",
  173. rmem->name, compat);
  174. return 0;
  175. }
  176. }
  177. return -ENOENT;
  178. }
  179. static int __init __rmem_cmp(const void *a, const void *b)
  180. {
  181. const struct reserved_mem *ra = a, *rb = b;
  182. if (ra->base < rb->base)
  183. return -1;
  184. if (ra->base > rb->base)
  185. return 1;
  186. return 0;
  187. }
  188. static void __init __rmem_check_for_overlap(void)
  189. {
  190. int i;
  191. if (reserved_mem_count < 2)
  192. return;
  193. sort(reserved_mem, reserved_mem_count, sizeof(reserved_mem[0]),
  194. __rmem_cmp, NULL);
  195. for (i = 0; i < reserved_mem_count - 1; i++) {
  196. struct reserved_mem *this, *next;
  197. this = &reserved_mem[i];
  198. next = &reserved_mem[i + 1];
  199. if (!(this->base && next->base))
  200. continue;
  201. if (this->base + this->size > next->base) {
  202. phys_addr_t this_end, next_end;
  203. this_end = this->base + this->size;
  204. next_end = next->base + next->size;
  205. pr_err("OVERLAP DETECTED!\n%s (%pa--%pa) overlaps with %s (%pa--%pa)\n",
  206. this->name, &this->base, &this_end,
  207. next->name, &next->base, &next_end);
  208. }
  209. }
  210. }
  211. /**
  212. * fdt_init_reserved_mem - allocate and init all saved reserved memory regions
  213. */
  214. void __init fdt_init_reserved_mem(void)
  215. {
  216. int i;
  217. /* check for overlapping reserved regions */
  218. __rmem_check_for_overlap();
  219. for (i = 0; i < reserved_mem_count; i++) {
  220. struct reserved_mem *rmem = &reserved_mem[i];
  221. unsigned long node = rmem->fdt_node;
  222. int len;
  223. const __be32 *prop;
  224. int err = 0;
  225. prop = of_get_flat_dt_prop(node, "phandle", &len);
  226. if (!prop)
  227. prop = of_get_flat_dt_prop(node, "linux,phandle", &len);
  228. if (prop)
  229. rmem->phandle = of_read_number(prop, len/4);
  230. if (rmem->size == 0)
  231. err = __reserved_mem_alloc_size(node, rmem->name,
  232. &rmem->base, &rmem->size);
  233. if (err == 0)
  234. __reserved_mem_init_node(rmem);
  235. }
  236. }
  237. static inline struct reserved_mem *__find_rmem(struct device_node *node)
  238. {
  239. unsigned int i;
  240. if (!node->phandle)
  241. return NULL;
  242. for (i = 0; i < reserved_mem_count; i++)
  243. if (reserved_mem[i].phandle == node->phandle)
  244. return &reserved_mem[i];
  245. return NULL;
  246. }
  247. struct rmem_assigned_device {
  248. struct device *dev;
  249. struct reserved_mem *rmem;
  250. struct list_head list;
  251. };
  252. static LIST_HEAD(of_rmem_assigned_device_list);
  253. static DEFINE_MUTEX(of_rmem_assigned_device_mutex);
  254. /**
  255. * of_reserved_mem_device_init_by_idx() - assign reserved memory region to
  256. * given device
  257. * @dev: Pointer to the device to configure
  258. * @np: Pointer to the device_node with 'reserved-memory' property
  259. * @idx: Index of selected region
  260. *
  261. * This function assigns respective DMA-mapping operations based on reserved
  262. * memory region specified by 'memory-region' property in @np node to the @dev
  263. * device. When driver needs to use more than one reserved memory region, it
  264. * should allocate child devices and initialize regions by name for each of
  265. * child device.
  266. *
  267. * Returns error code or zero on success.
  268. */
  269. int of_reserved_mem_device_init_by_idx(struct device *dev,
  270. struct device_node *np, int idx)
  271. {
  272. struct rmem_assigned_device *rd;
  273. struct device_node *target;
  274. struct reserved_mem *rmem;
  275. int ret;
  276. if (!np || !dev)
  277. return -EINVAL;
  278. target = of_parse_phandle(np, "memory-region", idx);
  279. if (!target)
  280. return -ENODEV;
  281. rmem = __find_rmem(target);
  282. of_node_put(target);
  283. if (!rmem || !rmem->ops || !rmem->ops->device_init)
  284. return -EINVAL;
  285. rd = kmalloc(sizeof(struct rmem_assigned_device), GFP_KERNEL);
  286. if (!rd)
  287. return -ENOMEM;
  288. ret = rmem->ops->device_init(rmem, dev);
  289. if (ret == 0) {
  290. rd->dev = dev;
  291. rd->rmem = rmem;
  292. mutex_lock(&of_rmem_assigned_device_mutex);
  293. list_add(&rd->list, &of_rmem_assigned_device_list);
  294. mutex_unlock(&of_rmem_assigned_device_mutex);
  295. /* ensure that dma_ops is set for virtual devices
  296. * using reserved memory
  297. */
  298. of_dma_configure(dev, np, true);
  299. dev_info(dev, "assigned reserved memory node %s\n", rmem->name);
  300. } else {
  301. kfree(rd);
  302. }
  303. return ret;
  304. }
  305. EXPORT_SYMBOL_GPL(of_reserved_mem_device_init_by_idx);
  306. /**
  307. * of_reserved_mem_device_release() - release reserved memory device structures
  308. * @dev: Pointer to the device to deconfigure
  309. *
  310. * This function releases structures allocated for memory region handling for
  311. * the given device.
  312. */
  313. void of_reserved_mem_device_release(struct device *dev)
  314. {
  315. struct rmem_assigned_device *rd;
  316. struct reserved_mem *rmem = NULL;
  317. mutex_lock(&of_rmem_assigned_device_mutex);
  318. list_for_each_entry(rd, &of_rmem_assigned_device_list, list) {
  319. if (rd->dev == dev) {
  320. rmem = rd->rmem;
  321. list_del(&rd->list);
  322. kfree(rd);
  323. break;
  324. }
  325. }
  326. mutex_unlock(&of_rmem_assigned_device_mutex);
  327. if (!rmem || !rmem->ops || !rmem->ops->device_release)
  328. return;
  329. rmem->ops->device_release(rmem, dev);
  330. }
  331. EXPORT_SYMBOL_GPL(of_reserved_mem_device_release);
  332. /**
  333. * of_reserved_mem_lookup() - acquire reserved_mem from a device node
  334. * @np: node pointer of the desired reserved-memory region
  335. *
  336. * This function allows drivers to acquire a reference to the reserved_mem
  337. * struct based on a device node handle.
  338. *
  339. * Returns a reserved_mem reference, or NULL on error.
  340. */
  341. struct reserved_mem *of_reserved_mem_lookup(struct device_node *np)
  342. {
  343. const char *name;
  344. int i;
  345. if (!np->full_name)
  346. return NULL;
  347. name = kbasename(np->full_name);
  348. for (i = 0; i < reserved_mem_count; i++)
  349. if (!strcmp(reserved_mem[i].name, name))
  350. return &reserved_mem[i];
  351. return NULL;
  352. }
  353. EXPORT_SYMBOL_GPL(of_reserved_mem_lookup);