of_reserved_mem.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  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/ioport.h>
  14. #include <linux/libfdt.h>
  15. #include <linux/of.h>
  16. #include <linux/of_fdt.h>
  17. #include <linux/of_platform.h>
  18. #include <linux/mm.h>
  19. #include <linux/sizes.h>
  20. #include <linux/of_reserved_mem.h>
  21. #include <linux/sort.h>
  22. #include <linux/slab.h>
  23. #include <linux/memblock.h>
  24. #include <linux/kmemleak.h>
  25. #include <linux/cma.h>
  26. #include <linux/dma-map-ops.h>
  27. #include "of_private.h"
  28. static struct reserved_mem reserved_mem_array[MAX_RESERVED_REGIONS] __initdata;
  29. static struct reserved_mem *reserved_mem __refdata = reserved_mem_array;
  30. static int total_reserved_mem_cnt = MAX_RESERVED_REGIONS;
  31. static int reserved_mem_count;
  32. static int __init early_init_dt_alloc_reserved_memory_arch(phys_addr_t size,
  33. phys_addr_t align, phys_addr_t start, phys_addr_t end, bool nomap,
  34. phys_addr_t *res_base)
  35. {
  36. phys_addr_t base;
  37. int err = 0;
  38. end = !end ? MEMBLOCK_ALLOC_ANYWHERE : end;
  39. align = !align ? SMP_CACHE_BYTES : align;
  40. base = memblock_phys_alloc_range(size, align, start, end);
  41. if (!base)
  42. return -ENOMEM;
  43. *res_base = base;
  44. if (nomap) {
  45. err = memblock_mark_nomap(base, size);
  46. if (err)
  47. memblock_phys_free(base, size);
  48. }
  49. if (!err)
  50. kmemleak_ignore_phys(base);
  51. return err;
  52. }
  53. /*
  54. * alloc_reserved_mem_array() - allocate memory for the reserved_mem
  55. * array using memblock
  56. *
  57. * This function is used to allocate memory for the reserved_mem
  58. * array according to the total number of reserved memory regions
  59. * defined in the DT.
  60. * After the new array is allocated, the information stored in
  61. * the initial static array is copied over to this new array and
  62. * the new array is used from this point on.
  63. */
  64. static void __init alloc_reserved_mem_array(void)
  65. {
  66. struct reserved_mem *new_array;
  67. size_t alloc_size, copy_size, memset_size;
  68. alloc_size = array_size(total_reserved_mem_cnt, sizeof(*new_array));
  69. if (alloc_size == SIZE_MAX) {
  70. pr_err("Failed to allocate memory for reserved_mem array with err: %d", -EOVERFLOW);
  71. return;
  72. }
  73. new_array = memblock_alloc(alloc_size, SMP_CACHE_BYTES);
  74. if (!new_array) {
  75. pr_err("Failed to allocate memory for reserved_mem array with err: %d", -ENOMEM);
  76. return;
  77. }
  78. copy_size = array_size(reserved_mem_count, sizeof(*new_array));
  79. if (copy_size == SIZE_MAX) {
  80. memblock_free(new_array, alloc_size);
  81. total_reserved_mem_cnt = MAX_RESERVED_REGIONS;
  82. pr_err("Failed to allocate memory for reserved_mem array with err: %d", -EOVERFLOW);
  83. return;
  84. }
  85. memset_size = alloc_size - copy_size;
  86. memcpy(new_array, reserved_mem, copy_size);
  87. memset(new_array + reserved_mem_count, 0, memset_size);
  88. reserved_mem = new_array;
  89. }
  90. static void __init fdt_init_reserved_mem_node(struct reserved_mem *rmem);
  91. /*
  92. * fdt_reserved_mem_save_node() - save fdt node for second pass initialization
  93. */
  94. static void __init fdt_reserved_mem_save_node(unsigned long node, const char *uname,
  95. phys_addr_t base, phys_addr_t size)
  96. {
  97. struct reserved_mem *rmem = &reserved_mem[reserved_mem_count];
  98. if (reserved_mem_count == total_reserved_mem_cnt) {
  99. pr_err("not enough space for all defined regions.\n");
  100. return;
  101. }
  102. rmem->fdt_node = node;
  103. rmem->name = uname;
  104. rmem->base = base;
  105. rmem->size = size;
  106. /* Call the region specific initialization function */
  107. fdt_init_reserved_mem_node(rmem);
  108. reserved_mem_count++;
  109. }
  110. static int __init early_init_dt_reserve_memory(phys_addr_t base,
  111. phys_addr_t size, bool nomap)
  112. {
  113. if (nomap) {
  114. /*
  115. * If the memory is already reserved (by another region), we
  116. * should not allow it to be marked nomap, but don't worry
  117. * if the region isn't memory as it won't be mapped.
  118. */
  119. if (memblock_overlaps_region(&memblock.memory, base, size) &&
  120. memblock_is_region_reserved(base, size))
  121. return -EBUSY;
  122. return memblock_mark_nomap(base, size);
  123. }
  124. return memblock_reserve(base, size);
  125. }
  126. /*
  127. * __reserved_mem_reserve_reg() - reserve all memory described in 'reg' property
  128. */
  129. static int __init __reserved_mem_reserve_reg(unsigned long node,
  130. const char *uname)
  131. {
  132. phys_addr_t base, size;
  133. int i, len;
  134. const __be32 *prop;
  135. bool nomap, default_cma;
  136. prop = of_flat_dt_get_addr_size_prop(node, "reg", &len);
  137. if (!prop)
  138. return -ENOENT;
  139. nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL;
  140. default_cma = of_get_flat_dt_prop(node, "linux,cma-default", NULL);
  141. if (default_cma && cma_skip_dt_default_reserved_mem()) {
  142. pr_err("Skipping dt linux,cma-default for \"cma=\" kernel param.\n");
  143. return -EINVAL;
  144. }
  145. for (i = 0; i < len; i++) {
  146. u64 b, s;
  147. of_flat_dt_read_addr_size(prop, i, &b, &s);
  148. base = b;
  149. size = s;
  150. if (size && early_init_dt_reserve_memory(base, size, nomap) == 0) {
  151. /* Architecture specific contiguous memory fixup. */
  152. if (of_flat_dt_is_compatible(node, "shared-dma-pool") &&
  153. of_get_flat_dt_prop(node, "reusable", NULL))
  154. dma_contiguous_early_fixup(base, size);
  155. pr_debug("Reserved memory: reserved region for node '%s': base %pa, size %lu MiB\n",
  156. uname, &base, (unsigned long)(size / SZ_1M));
  157. } else {
  158. pr_err("Reserved memory: failed to reserve memory for node '%s': base %pa, size %lu MiB\n",
  159. uname, &base, (unsigned long)(size / SZ_1M));
  160. }
  161. }
  162. return 0;
  163. }
  164. /*
  165. * __reserved_mem_check_root() - check if #size-cells, #address-cells provided
  166. * in /reserved-memory matches the values supported by the current implementation,
  167. * also check if ranges property has been provided
  168. */
  169. static int __init __reserved_mem_check_root(unsigned long node)
  170. {
  171. const __be32 *prop;
  172. prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
  173. if (!prop || be32_to_cpup(prop) != dt_root_size_cells)
  174. return -EINVAL;
  175. prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
  176. if (!prop || be32_to_cpup(prop) != dt_root_addr_cells)
  177. return -EINVAL;
  178. prop = of_get_flat_dt_prop(node, "ranges", NULL);
  179. if (!prop)
  180. return -EINVAL;
  181. return 0;
  182. }
  183. static void __init __rmem_check_for_overlap(void);
  184. /**
  185. * fdt_scan_reserved_mem_reg_nodes() - Store info for the "reg" defined
  186. * reserved memory regions.
  187. *
  188. * This function is used to scan through the DT and store the
  189. * information for the reserved memory regions that are defined using
  190. * the "reg" property. The region node number, name, base address, and
  191. * size are all stored in the reserved_mem array by calling the
  192. * fdt_reserved_mem_save_node() function.
  193. */
  194. void __init fdt_scan_reserved_mem_reg_nodes(void)
  195. {
  196. const void *fdt = initial_boot_params;
  197. phys_addr_t base, size;
  198. int node, child;
  199. if (!fdt)
  200. return;
  201. node = fdt_path_offset(fdt, "/reserved-memory");
  202. if (node < 0) {
  203. pr_info("Reserved memory: No reserved-memory node in the DT\n");
  204. return;
  205. }
  206. /* Attempt dynamic allocation of a new reserved_mem array */
  207. alloc_reserved_mem_array();
  208. if (__reserved_mem_check_root(node)) {
  209. pr_err("Reserved memory: unsupported node format, ignoring\n");
  210. return;
  211. }
  212. fdt_for_each_subnode(child, fdt, node) {
  213. const char *uname;
  214. bool default_cma = of_get_flat_dt_prop(child, "linux,cma-default", NULL);
  215. u64 b, s;
  216. if (!of_fdt_device_is_available(fdt, child))
  217. continue;
  218. if (default_cma && cma_skip_dt_default_reserved_mem())
  219. continue;
  220. if (!of_flat_dt_get_addr_size(child, "reg", &b, &s))
  221. continue;
  222. base = b;
  223. size = s;
  224. if (size) {
  225. uname = fdt_get_name(fdt, child, NULL);
  226. fdt_reserved_mem_save_node(child, uname, base, size);
  227. }
  228. }
  229. /* check for overlapping reserved regions */
  230. __rmem_check_for_overlap();
  231. }
  232. static int __init __reserved_mem_alloc_size(unsigned long node, const char *uname);
  233. /*
  234. * fdt_scan_reserved_mem() - scan a single FDT node for reserved memory
  235. */
  236. int __init fdt_scan_reserved_mem(void)
  237. {
  238. int node, child;
  239. int dynamic_nodes_cnt = 0, count = 0;
  240. int dynamic_nodes[MAX_RESERVED_REGIONS];
  241. const void *fdt = initial_boot_params;
  242. node = fdt_path_offset(fdt, "/reserved-memory");
  243. if (node < 0)
  244. return -ENODEV;
  245. if (__reserved_mem_check_root(node) != 0) {
  246. pr_err("Reserved memory: unsupported node format, ignoring\n");
  247. return -EINVAL;
  248. }
  249. fdt_for_each_subnode(child, fdt, node) {
  250. const char *uname;
  251. int err;
  252. if (!of_fdt_device_is_available(fdt, child))
  253. continue;
  254. uname = fdt_get_name(fdt, child, NULL);
  255. err = __reserved_mem_reserve_reg(child, uname);
  256. if (!err)
  257. count++;
  258. /*
  259. * Save the nodes for the dynamically-placed regions
  260. * into an array which will be used for allocation right
  261. * after all the statically-placed regions are reserved
  262. * or marked as no-map. This is done to avoid dynamically
  263. * allocating from one of the statically-placed regions.
  264. */
  265. if (err == -ENOENT && of_get_flat_dt_prop(child, "size", NULL)) {
  266. dynamic_nodes[dynamic_nodes_cnt] = child;
  267. dynamic_nodes_cnt++;
  268. }
  269. }
  270. for (int i = 0; i < dynamic_nodes_cnt; i++) {
  271. const char *uname;
  272. int err;
  273. child = dynamic_nodes[i];
  274. uname = fdt_get_name(fdt, child, NULL);
  275. err = __reserved_mem_alloc_size(child, uname);
  276. if (!err)
  277. count++;
  278. }
  279. total_reserved_mem_cnt = count;
  280. return 0;
  281. }
  282. /*
  283. * __reserved_mem_alloc_in_range() - allocate reserved memory described with
  284. * 'alloc-ranges'. Choose bottom-up/top-down depending on nearby existing
  285. * reserved regions to keep the reserved memory contiguous if possible.
  286. */
  287. static int __init __reserved_mem_alloc_in_range(phys_addr_t size,
  288. phys_addr_t align, phys_addr_t start, phys_addr_t end, bool nomap,
  289. phys_addr_t *res_base)
  290. {
  291. bool prev_bottom_up = memblock_bottom_up();
  292. bool bottom_up = false, top_down = false;
  293. int ret, i;
  294. for (i = 0; i < reserved_mem_count; i++) {
  295. struct reserved_mem *rmem = &reserved_mem[i];
  296. /* Skip regions that were not reserved yet */
  297. if (rmem->size == 0)
  298. continue;
  299. /*
  300. * If range starts next to an existing reservation, use bottom-up:
  301. * |....RRRR................RRRRRRRR..............|
  302. * --RRRR------
  303. */
  304. if (start >= rmem->base && start <= (rmem->base + rmem->size))
  305. bottom_up = true;
  306. /*
  307. * If range ends next to an existing reservation, use top-down:
  308. * |....RRRR................RRRRRRRR..............|
  309. * -------RRRR-----
  310. */
  311. if (end >= rmem->base && end <= (rmem->base + rmem->size))
  312. top_down = true;
  313. }
  314. /* Change setting only if either bottom-up or top-down was selected */
  315. if (bottom_up != top_down)
  316. memblock_set_bottom_up(bottom_up);
  317. ret = early_init_dt_alloc_reserved_memory_arch(size, align,
  318. start, end, nomap, res_base);
  319. /* Restore old setting if needed */
  320. if (bottom_up != top_down)
  321. memblock_set_bottom_up(prev_bottom_up);
  322. return ret;
  323. }
  324. /*
  325. * __reserved_mem_alloc_size() - allocate reserved memory described by
  326. * 'size', 'alignment' and 'alloc-ranges' properties.
  327. */
  328. static int __init __reserved_mem_alloc_size(unsigned long node, const char *uname)
  329. {
  330. phys_addr_t start = 0, end = 0;
  331. phys_addr_t base = 0, align = 0, size;
  332. int i, len;
  333. const __be32 *prop;
  334. bool nomap, default_cma;
  335. int ret;
  336. prop = of_get_flat_dt_prop(node, "size", &len);
  337. if (!prop)
  338. return -EINVAL;
  339. if (len != dt_root_size_cells * sizeof(__be32)) {
  340. pr_err("invalid size property in '%s' node.\n", uname);
  341. return -EINVAL;
  342. }
  343. size = dt_mem_next_cell(dt_root_size_cells, &prop);
  344. prop = of_get_flat_dt_prop(node, "alignment", &len);
  345. if (prop) {
  346. if (len != dt_root_addr_cells * sizeof(__be32)) {
  347. pr_err("invalid alignment property in '%s' node.\n",
  348. uname);
  349. return -EINVAL;
  350. }
  351. align = dt_mem_next_cell(dt_root_addr_cells, &prop);
  352. }
  353. nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL;
  354. default_cma = of_get_flat_dt_prop(node, "linux,cma-default", NULL);
  355. if (default_cma && cma_skip_dt_default_reserved_mem()) {
  356. pr_err("Skipping dt linux,cma-default for \"cma=\" kernel param.\n");
  357. return -EINVAL;
  358. }
  359. /* Need adjust the alignment to satisfy the CMA requirement */
  360. if (IS_ENABLED(CONFIG_CMA)
  361. && of_flat_dt_is_compatible(node, "shared-dma-pool")
  362. && of_get_flat_dt_prop(node, "reusable", NULL)
  363. && !nomap)
  364. align = max_t(phys_addr_t, align, CMA_MIN_ALIGNMENT_BYTES);
  365. prop = of_flat_dt_get_addr_size_prop(node, "alloc-ranges", &len);
  366. if (prop) {
  367. for (i = 0; i < len; i++) {
  368. u64 b, s;
  369. of_flat_dt_read_addr_size(prop, i, &b, &s);
  370. start = b;
  371. end = b + s;
  372. base = 0;
  373. ret = __reserved_mem_alloc_in_range(size, align,
  374. start, end, nomap, &base);
  375. if (ret == 0) {
  376. pr_debug("allocated memory for '%s' node: base %pa, size %lu MiB\n",
  377. uname, &base,
  378. (unsigned long)(size / SZ_1M));
  379. break;
  380. }
  381. }
  382. } else {
  383. ret = early_init_dt_alloc_reserved_memory_arch(size, align,
  384. 0, 0, nomap, &base);
  385. if (ret == 0)
  386. pr_debug("allocated memory for '%s' node: base %pa, size %lu MiB\n",
  387. uname, &base, (unsigned long)(size / SZ_1M));
  388. }
  389. if (base == 0) {
  390. pr_err("failed to allocate memory for node '%s': size %lu MiB\n",
  391. uname, (unsigned long)(size / SZ_1M));
  392. return -ENOMEM;
  393. }
  394. /* Architecture specific contiguous memory fixup. */
  395. if (of_flat_dt_is_compatible(node, "shared-dma-pool") &&
  396. of_get_flat_dt_prop(node, "reusable", NULL))
  397. dma_contiguous_early_fixup(base, size);
  398. /* Save region in the reserved_mem array */
  399. fdt_reserved_mem_save_node(node, uname, base, size);
  400. return 0;
  401. }
  402. static const struct of_device_id __rmem_of_table_sentinel
  403. __used __section("__reservedmem_of_table_end");
  404. /*
  405. * __reserved_mem_init_node() - call region specific reserved memory init code
  406. */
  407. static int __init __reserved_mem_init_node(struct reserved_mem *rmem)
  408. {
  409. extern const struct of_device_id __reservedmem_of_table[];
  410. const struct of_device_id *i;
  411. int ret = -ENOENT;
  412. for (i = __reservedmem_of_table; i < &__rmem_of_table_sentinel; i++) {
  413. reservedmem_of_init_fn initfn = i->data;
  414. const char *compat = i->compatible;
  415. if (!of_flat_dt_is_compatible(rmem->fdt_node, compat))
  416. continue;
  417. ret = initfn(rmem);
  418. if (ret == 0) {
  419. pr_info("initialized node %s, compatible id %s\n",
  420. rmem->name, compat);
  421. break;
  422. }
  423. }
  424. return ret;
  425. }
  426. static int __init __rmem_cmp(const void *a, const void *b)
  427. {
  428. const struct reserved_mem *ra = a, *rb = b;
  429. if (ra->base < rb->base)
  430. return -1;
  431. if (ra->base > rb->base)
  432. return 1;
  433. /*
  434. * Put the dynamic allocations (address == 0, size == 0) before static
  435. * allocations at address 0x0 so that overlap detection works
  436. * correctly.
  437. */
  438. if (ra->size < rb->size)
  439. return -1;
  440. if (ra->size > rb->size)
  441. return 1;
  442. if (ra->fdt_node < rb->fdt_node)
  443. return -1;
  444. if (ra->fdt_node > rb->fdt_node)
  445. return 1;
  446. return 0;
  447. }
  448. static void __init __rmem_check_for_overlap(void)
  449. {
  450. int i;
  451. if (reserved_mem_count < 2)
  452. return;
  453. sort(reserved_mem, reserved_mem_count, sizeof(reserved_mem[0]),
  454. __rmem_cmp, NULL);
  455. for (i = 0; i < reserved_mem_count - 1; i++) {
  456. struct reserved_mem *this, *next;
  457. this = &reserved_mem[i];
  458. next = &reserved_mem[i + 1];
  459. if (this->base + this->size > next->base) {
  460. phys_addr_t this_end, next_end;
  461. this_end = this->base + this->size;
  462. next_end = next->base + next->size;
  463. pr_err("OVERLAP DETECTED!\n%s (%pa--%pa) overlaps with %s (%pa--%pa)\n",
  464. this->name, &this->base, &this_end,
  465. next->name, &next->base, &next_end);
  466. }
  467. }
  468. }
  469. /**
  470. * fdt_init_reserved_mem_node() - Initialize a reserved memory region
  471. * @rmem: reserved_mem struct of the memory region to be initialized.
  472. *
  473. * This function is used to call the region specific initialization
  474. * function for a reserved memory region.
  475. */
  476. static void __init fdt_init_reserved_mem_node(struct reserved_mem *rmem)
  477. {
  478. unsigned long node = rmem->fdt_node;
  479. int err = 0;
  480. bool nomap;
  481. nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL;
  482. err = __reserved_mem_init_node(rmem);
  483. if (err != 0 && err != -ENOENT) {
  484. pr_info("node %s compatible matching fail\n", rmem->name);
  485. if (nomap)
  486. memblock_clear_nomap(rmem->base, rmem->size);
  487. else
  488. memblock_phys_free(rmem->base, rmem->size);
  489. } else {
  490. phys_addr_t end = rmem->base + rmem->size - 1;
  491. bool reusable =
  492. (of_get_flat_dt_prop(node, "reusable", NULL)) != NULL;
  493. pr_info("%pa..%pa (%lu KiB) %s %s %s\n",
  494. &rmem->base, &end, (unsigned long)(rmem->size / SZ_1K),
  495. nomap ? "nomap" : "map",
  496. reusable ? "reusable" : "non-reusable",
  497. rmem->name ? rmem->name : "unknown");
  498. }
  499. }
  500. struct rmem_assigned_device {
  501. struct device *dev;
  502. struct reserved_mem *rmem;
  503. struct list_head list;
  504. };
  505. static LIST_HEAD(of_rmem_assigned_device_list);
  506. static DEFINE_MUTEX(of_rmem_assigned_device_mutex);
  507. /**
  508. * of_reserved_mem_device_init_by_idx() - assign reserved memory region to
  509. * given device
  510. * @dev: Pointer to the device to configure
  511. * @np: Pointer to the device_node with 'reserved-memory' property
  512. * @idx: Index of selected region
  513. *
  514. * This function assigns respective DMA-mapping operations based on reserved
  515. * memory region specified by 'memory-region' property in @np node to the @dev
  516. * device. When driver needs to use more than one reserved memory region, it
  517. * should allocate child devices and initialize regions by name for each of
  518. * child device.
  519. *
  520. * Returns error code or zero on success.
  521. */
  522. int of_reserved_mem_device_init_by_idx(struct device *dev,
  523. struct device_node *np, int idx)
  524. {
  525. struct rmem_assigned_device *rd;
  526. struct device_node *target;
  527. struct reserved_mem *rmem;
  528. int ret;
  529. if (!np || !dev)
  530. return -EINVAL;
  531. target = of_parse_phandle(np, "memory-region", idx);
  532. if (!target)
  533. return -ENODEV;
  534. if (!of_device_is_available(target)) {
  535. of_node_put(target);
  536. return 0;
  537. }
  538. rmem = of_reserved_mem_lookup(target);
  539. of_node_put(target);
  540. if (!rmem || !rmem->ops || !rmem->ops->device_init)
  541. return -EINVAL;
  542. rd = kmalloc_obj(struct rmem_assigned_device);
  543. if (!rd)
  544. return -ENOMEM;
  545. ret = rmem->ops->device_init(rmem, dev);
  546. if (ret == 0) {
  547. rd->dev = dev;
  548. rd->rmem = rmem;
  549. mutex_lock(&of_rmem_assigned_device_mutex);
  550. list_add(&rd->list, &of_rmem_assigned_device_list);
  551. mutex_unlock(&of_rmem_assigned_device_mutex);
  552. dev_info(dev, "assigned reserved memory node %s\n", rmem->name);
  553. } else {
  554. kfree(rd);
  555. }
  556. return ret;
  557. }
  558. EXPORT_SYMBOL_GPL(of_reserved_mem_device_init_by_idx);
  559. /**
  560. * of_reserved_mem_device_init_by_name() - assign named reserved memory region
  561. * to given device
  562. * @dev: pointer to the device to configure
  563. * @np: pointer to the device node with 'memory-region' property
  564. * @name: name of the selected memory region
  565. *
  566. * Returns: 0 on success or a negative error-code on failure.
  567. */
  568. int of_reserved_mem_device_init_by_name(struct device *dev,
  569. struct device_node *np,
  570. const char *name)
  571. {
  572. int idx = of_property_match_string(np, "memory-region-names", name);
  573. return of_reserved_mem_device_init_by_idx(dev, np, idx);
  574. }
  575. EXPORT_SYMBOL_GPL(of_reserved_mem_device_init_by_name);
  576. /**
  577. * of_reserved_mem_device_release() - release reserved memory device structures
  578. * @dev: Pointer to the device to deconfigure
  579. *
  580. * This function releases structures allocated for memory region handling for
  581. * the given device.
  582. */
  583. void of_reserved_mem_device_release(struct device *dev)
  584. {
  585. struct rmem_assigned_device *rd, *tmp;
  586. LIST_HEAD(release_list);
  587. mutex_lock(&of_rmem_assigned_device_mutex);
  588. list_for_each_entry_safe(rd, tmp, &of_rmem_assigned_device_list, list) {
  589. if (rd->dev == dev)
  590. list_move_tail(&rd->list, &release_list);
  591. }
  592. mutex_unlock(&of_rmem_assigned_device_mutex);
  593. list_for_each_entry_safe(rd, tmp, &release_list, list) {
  594. if (rd->rmem && rd->rmem->ops && rd->rmem->ops->device_release)
  595. rd->rmem->ops->device_release(rd->rmem, dev);
  596. kfree(rd);
  597. }
  598. }
  599. EXPORT_SYMBOL_GPL(of_reserved_mem_device_release);
  600. /**
  601. * of_reserved_mem_lookup() - acquire reserved_mem from a device node
  602. * @np: node pointer of the desired reserved-memory region
  603. *
  604. * This function allows drivers to acquire a reference to the reserved_mem
  605. * struct based on a device node handle.
  606. *
  607. * Returns a reserved_mem reference, or NULL on error.
  608. */
  609. struct reserved_mem *of_reserved_mem_lookup(struct device_node *np)
  610. {
  611. const char *name;
  612. int i;
  613. if (!np->full_name)
  614. return NULL;
  615. name = kbasename(np->full_name);
  616. for (i = 0; i < reserved_mem_count; i++)
  617. if (!strcmp(reserved_mem[i].name, name))
  618. return &reserved_mem[i];
  619. return NULL;
  620. }
  621. EXPORT_SYMBOL_GPL(of_reserved_mem_lookup);
  622. /**
  623. * of_reserved_mem_region_to_resource() - Get a reserved memory region as a resource
  624. * @np: node containing 'memory-region' property
  625. * @idx: index of 'memory-region' property to lookup
  626. * @res: Pointer to a struct resource to fill in with reserved region
  627. *
  628. * This function allows drivers to lookup a node's 'memory-region' property
  629. * entries by index and return a struct resource for the entry.
  630. *
  631. * Returns 0 on success with @res filled in. Returns -ENODEV if 'memory-region'
  632. * is missing or unavailable, -EINVAL for any other error.
  633. */
  634. int of_reserved_mem_region_to_resource(const struct device_node *np,
  635. unsigned int idx, struct resource *res)
  636. {
  637. struct reserved_mem *rmem;
  638. if (!np)
  639. return -EINVAL;
  640. struct device_node *target __free(device_node) = of_parse_phandle(np, "memory-region", idx);
  641. if (!target || !of_device_is_available(target))
  642. return -ENODEV;
  643. rmem = of_reserved_mem_lookup(target);
  644. if (!rmem)
  645. return -EINVAL;
  646. resource_set_range(res, rmem->base, rmem->size);
  647. res->flags = IORESOURCE_MEM;
  648. res->name = rmem->name;
  649. return 0;
  650. }
  651. EXPORT_SYMBOL_GPL(of_reserved_mem_region_to_resource);
  652. /**
  653. * of_reserved_mem_region_to_resource_byname() - Get a reserved memory region as a resource
  654. * @np: node containing 'memory-region' property
  655. * @name: name of 'memory-region' property entry to lookup
  656. * @res: Pointer to a struct resource to fill in with reserved region
  657. *
  658. * This function allows drivers to lookup a node's 'memory-region' property
  659. * entries by name and return a struct resource for the entry.
  660. *
  661. * Returns 0 on success with @res filled in, or a negative error-code on
  662. * failure.
  663. */
  664. int of_reserved_mem_region_to_resource_byname(const struct device_node *np,
  665. const char *name,
  666. struct resource *res)
  667. {
  668. int idx;
  669. if (!name)
  670. return -EINVAL;
  671. idx = of_property_match_string(np, "memory-region-names", name);
  672. if (idx < 0)
  673. return idx;
  674. return of_reserved_mem_region_to_resource(np, idx, res);
  675. }
  676. EXPORT_SYMBOL_GPL(of_reserved_mem_region_to_resource_byname);
  677. /**
  678. * of_reserved_mem_region_count() - Return the number of 'memory-region' entries
  679. * @np: node containing 'memory-region' property
  680. *
  681. * This function allows drivers to retrieve the number of entries for a node's
  682. * 'memory-region' property.
  683. *
  684. * Returns the number of entries on success, or negative error code on a
  685. * malformed property.
  686. */
  687. int of_reserved_mem_region_count(const struct device_node *np)
  688. {
  689. return of_count_phandle_with_args(np, "memory-region", NULL);
  690. }
  691. EXPORT_SYMBOL_GPL(of_reserved_mem_region_count);