3 * Andrew Andrianov <andrew@ncrmnt.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/init.h>
13 #include <linux/platform_device.h>
14 #include <linux/slab.h>
16 #include <linux/of_platform.h>
17 #include <linux/of_address.h>
18 #include <linux/clk.h>
19 #include <linux/dma-mapping.h>
20 #include <linux/cma.h>
21 #include <linux/dma-contiguous.h>
23 #include <linux/of_reserved_mem.h>
28 static int ion_parse_dt_heap_common(struct device_node *heap_node,
29 struct ion_platform_heap *heap,
30 struct ion_of_heap *compatible)
34 for (i = 0; compatible[i].name; i++) {
35 if (of_device_is_compatible(heap_node, compatible[i].compat))
39 if (!compatible[i].name)
42 heap->id = compatible[i].heap_id;
43 heap->type = compatible[i].type;
44 heap->name = compatible[i].name;
45 heap->align = compatible[i].align;
47 /* Some kind of callback function pointer? */
49 pr_info("%s: id %d type %d name %s align %lx\n", __func__,
50 heap->id, heap->type, heap->name, heap->align);
54 static int ion_setup_heap_common(struct platform_device *parent,
55 struct device_node *heap_node,
56 struct ion_platform_heap *heap)
61 case ION_HEAP_TYPE_CARVEOUT:
62 case ION_HEAP_TYPE_CHUNK:
63 if (heap->base && heap->size)
66 ret = of_reserved_mem_device_init(heap->priv);
75 struct ion_platform_data *ion_parse_dt(struct platform_device *pdev,
76 struct ion_of_heap *compatible)
79 const struct device_node *dt_node = pdev->dev.of_node;
80 struct device_node *node;
81 struct ion_platform_heap *heaps;
82 struct ion_platform_data *data;
85 num_heaps = of_get_available_child_count(dt_node);
88 return ERR_PTR(-EINVAL);
90 heaps = devm_kzalloc(&pdev->dev,
91 sizeof(struct ion_platform_heap) * num_heaps,
94 return ERR_PTR(-ENOMEM);
96 data = devm_kzalloc(&pdev->dev, sizeof(struct ion_platform_data),
99 return ERR_PTR(-ENOMEM);
101 for_each_available_child_of_node(dt_node, node) {
102 struct platform_device *heap_pdev;
104 ret = ion_parse_dt_heap_common(node, &heaps[i], compatible);
108 heap_pdev = of_platform_device_create(node, heaps[i].name,
111 return ERR_PTR(-ENOMEM);
112 heap_pdev->dev.platform_data = &heaps[i];
114 heaps[i].priv = &heap_pdev->dev;
116 ret = ion_setup_heap_common(pdev, node, &heaps[i]);
123 data->nr = num_heaps;
129 of_device_unregister(to_platform_device(heaps[i].priv));
134 void ion_destroy_platform_data(struct ion_platform_data *data)
138 for (i = 0; i < data->nr; i++)
139 if (data->heaps[i].priv)
140 of_device_unregister(to_platform_device(
141 data->heaps[i].priv));
144 #ifdef CONFIG_OF_RESERVED_MEM
145 #include <linux/of.h>
146 #include <linux/of_fdt.h>
147 #include <linux/of_reserved_mem.h>
149 static int rmem_ion_device_init(struct reserved_mem *rmem, struct device *dev)
151 struct platform_device *pdev = to_platform_device(dev);
152 struct ion_platform_heap *heap = pdev->dev.platform_data;
154 heap->base = rmem->base;
155 heap->base = rmem->size;
156 pr_debug("%s: heap %s base %pa size %pa dev %p\n", __func__,
157 heap->name, &rmem->base, &rmem->size, dev);
161 static void rmem_ion_device_release(struct reserved_mem *rmem,
167 static const struct reserved_mem_ops rmem_dma_ops = {
168 .device_init = rmem_ion_device_init,
169 .device_release = rmem_ion_device_release,
172 static int __init rmem_ion_setup(struct reserved_mem *rmem)
174 phys_addr_t size = rmem->size;
178 pr_info("Ion memory setup at %pa size %pa MiB\n",
180 rmem->ops = &rmem_dma_ops;
184 RESERVEDMEM_OF_DECLARE(ion, "ion-region", rmem_ion_setup);