of_reserved_mem.c 10 KB

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