2 * drivers/gpu/rockchip/rockchip_ion.c
4 * Copyright (C) 2011 Google, Inc.
6 * This software is licensed under the terms of the GNU General Public
7 * License version 2, as published by the Free Software Foundation, and
8 * may be copied, distributed, and modified under those terms.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
17 #include <linux/err.h>
18 #include <linux/platform_device.h>
19 #include <linux/slab.h>
20 #include <linux/rockchip_ion.h>
21 #include <linux/uaccess.h>
22 #include "../ion_priv.h"
23 #include <linux/dma-buf.h>
24 #include <asm-generic/dma-contiguous.h>
28 #include <linux/of_gpio.h>
29 #include <video/of_display_timing.h>
30 #include <linux/of_fdt.h>
33 static struct ion_device *idev;
35 static struct ion_heap **heaps;
37 struct ion_heap_desc {
39 enum ion_heap_type type;
43 extern struct ion_handle *ion_handle_get_by_id(struct ion_client *client,
45 extern int ion_handle_put(struct ion_handle *handle);
47 static struct ion_heap_desc ion_heap_meta[] = {
49 .id = ION_SYSTEM_HEAP_ID,
50 .type = ION_HEAP_TYPE_SYSTEM,
51 .name = ION_SYSTEM_HEAP_NAME,
54 .id = ION_CMA_HEAP_ID,
55 .type = ION_HEAP_TYPE_DMA,
56 .name = ION_CMA_HEAP_NAME,
59 .id = ION_IOMMU_HEAP_ID,
60 .type = ION_HEAP_TYPE_DMA,//ION_HEAP_TYPE_IOMMU,
61 .name = ION_IOMMU_HEAP_NAME,
64 .id = ION_DRM_HEAP_ID,
65 .type = ION_HEAP_TYPE_DMA,
66 .name = ION_DRM_HEAP_NAME,
70 struct device rockchip_ion_cma_dev = {
71 .coherent_dma_mask = DMA_BIT_MASK(32),
72 .init_name = "rockchip_ion_cma",
75 static int rockchip_ion_populate_heap(struct ion_platform_heap *heap)
79 unsigned int len = ARRAY_SIZE(ion_heap_meta);
80 for (i = 0; i < len; ++i) {
81 if (ion_heap_meta[i].id == heap->id) {
82 heap->name = ion_heap_meta[i].name;
83 heap->type = ion_heap_meta[i].type;
84 if(heap->id == ION_CMA_HEAP_ID)
85 heap->priv = &rockchip_ion_cma_dev;
91 pr_err("%s: Unable to populate heap, error: %d", __func__, ret);
95 static int rockchip_ion_get_heap_size(struct device_node *node,
96 struct ion_platform_heap *heap)
101 ret = of_property_read_u32(node, "rockchip,memory-reservation-size", &val);
111 static struct ion_platform_data *rockchip_ion_parse_dt(
114 struct device_node *dt_node = dev->of_node;
115 struct ion_platform_data *pdata = 0;
116 struct device_node *node;
119 uint32_t num_heaps = 0;
122 for_each_child_of_node(dt_node, node)
125 pr_info("%s: num_heaps = %d\n", __func__, num_heaps);
128 return ERR_PTR(-EINVAL);
130 pdata = kzalloc(sizeof(struct ion_platform_data) +
131 num_heaps*sizeof(struct ion_platform_heap), GFP_KERNEL);
133 return ERR_PTR(-ENOMEM);
134 pdata->heaps = (struct ion_platform_heap*)((void*)pdata+sizeof(struct ion_platform_data));
135 pdata->nr = num_heaps;
137 for_each_child_of_node(dt_node, node) {
138 ret = of_property_read_u32(node, "reg", &val);
140 pr_err("%s: Unable to find reg key", __func__);
143 pdata->heaps[idx].id = val;
145 ret = rockchip_ion_populate_heap(&pdata->heaps[idx]);
149 // rockchip_ion_get_heap_align(node, &pdata->heaps[idx]);
150 ret = rockchip_ion_get_heap_size(node, &pdata->heaps[idx]);
154 // rockchip_ion_get_heap_adjacent(node, &pdata->heaps[idx]);
155 pr_info("%d: %d %d %s 0x%p\n", idx, pdata->heaps[idx].type, pdata->heaps[idx].id, pdata->heaps[idx].name, pdata->heaps[idx].priv);
169 // struct "cma" quoted from drivers/base/dma-contiguous.c
171 unsigned long base_pfn;
173 unsigned long *bitmap;
176 // struct "ion_cma_heap" quoted from drivers/staging/android/ion/ion_cma_heap.c
177 struct ion_cma_heap {
178 struct ion_heap heap;
182 static int ion_cma_heap_debug_show(struct ion_heap *heap, struct seq_file *s,
185 struct ion_cma_heap *cma_heap = container_of(heap,
188 struct device *dev = cma_heap->dev;
189 struct cma *cma = dev_get_cma_area(dev);
191 int rows = cma->count/(SZ_1M >> PAGE_SHIFT);
192 phys_addr_t base = __pfn_to_phys(cma->base_pfn);
194 seq_printf(s, "Heap bitmap:\n");
196 for(i = rows - 1; i>= 0; i--){
197 seq_printf(s, "%.4uM@0x%08x: %08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx\n",
199 cma->bitmap[i*8 + 7],
200 cma->bitmap[i*8 + 6],
201 cma->bitmap[i*8 + 5],
202 cma->bitmap[i*8 + 4],
203 cma->bitmap[i*8 + 3],
204 cma->bitmap[i*8 + 2],
205 cma->bitmap[i*8 + 1],
208 seq_printf(s, "Heap size: %luM, Heap base: 0x%08x\n",
209 (cma->count)>>8, base);
216 struct ion_client *rockchip_ion_client_create(const char *name)
218 return ion_client_create(idev, name);
220 EXPORT_SYMBOL(rockchip_ion_client_create);
222 static long rockchip_custom_ioctl (struct ion_client *client, unsigned int cmd,
225 pr_debug("[%s %d] cmd=%X\n", __func__, __LINE__, cmd);
228 case ION_IOC_CLEAN_CACHES:
229 case ION_IOC_INV_CACHES:
230 case ION_IOC_CLEAN_INV_CACHES:
232 case ION_IOC_GET_PHYS:
234 struct ion_phys_data data;
235 struct ion_handle *handle;
238 if (copy_from_user(&data, (void __user *)arg,
239 sizeof(struct ion_phys_data)))
242 handle = ion_handle_get_by_id(client, data.handle);
244 return PTR_ERR(handle);
246 ret = ion_phys(client, handle, &data.phys, (size_t *)&data.size);
247 pr_debug("ret=%d, phys=0x%lX\n", ret, data.phys);
248 ion_handle_put(handle);
251 if (copy_to_user((void __user *)arg, &data, sizeof(struct ion_phys_data)))
255 case ION_IOC_GET_SHARE_ID:
257 struct ion_share_id_data data;
258 struct dma_buf *dmabuf = NULL;
260 if (copy_from_user(&data, (void __user *)arg,
261 sizeof(struct ion_share_id_data)))
264 dmabuf = dma_buf_get(data.fd);
266 return PTR_ERR(dmabuf);
268 data.id = (unsigned int)dmabuf;
269 // dma_buf_put(dmabuf);
271 if (copy_to_user((void __user *)arg, &data, sizeof(struct ion_share_id_data)))
276 case ION_IOC_SHARE_BY_ID:
278 struct ion_share_id_data data;
281 if (copy_from_user(&data, (void __user *)arg,
282 sizeof(struct ion_share_id_data)))
285 fd = dma_buf_fd((struct dma_buf*)data.id, O_CLOEXEC);
291 if (copy_to_user((void __user *)arg, &data, sizeof(struct ion_share_id_data)))
303 static int rockchip_ion_probe(struct platform_device *pdev)
305 struct ion_platform_data *pdata;
306 unsigned int pdata_needs_to_be_freed;
310 if (pdev->dev.of_node) {
311 pdata = rockchip_ion_parse_dt(&pdev->dev);
313 return PTR_ERR(pdata);
315 pdata_needs_to_be_freed = 1;
317 pdata = pdev->dev.platform_data;
318 pdata_needs_to_be_freed = 0;
321 num_heaps = pdata->nr;
322 heaps = kzalloc(sizeof(struct ion_heap *) * num_heaps, GFP_KERNEL);
324 idev = ion_device_create(rockchip_custom_ioctl);
325 if (IS_ERR_OR_NULL(idev)) {
327 return PTR_ERR(idev);
329 /* create the heaps as specified in the board file */
330 for (i = 0; i < num_heaps; i++) {
331 struct ion_platform_heap *heap_data = &pdata->heaps[i];
333 heaps[i] = ion_heap_create(heap_data);
334 if (IS_ERR_OR_NULL(heaps[i])) {
335 err = PTR_ERR(heaps[i]);
339 if (ION_HEAP_TYPE_DMA==heap_data->type)
340 heaps[i]->debug_show = ion_cma_heap_debug_show;
342 ion_device_add_heap(idev, heaps[i]);
344 platform_set_drvdata(pdev, idev);
345 if (pdata_needs_to_be_freed)
348 pr_info("Rockchip ion module is successfully loaded\n");
351 for (i = 0; i < num_heaps; i++) {
353 ion_heap_destroy(heaps[i]);
355 if (pdata_needs_to_be_freed)
361 static int rockchip_ion_remove(struct platform_device *pdev)
363 struct ion_device *idev = platform_get_drvdata(pdev);
366 ion_device_destroy(idev);
367 for (i = 0; i < num_heaps; i++)
368 ion_heap_destroy(heaps[i]);
373 int __init rockchip_ion_find_reserve_mem(unsigned long node, const char *uname,
374 int depth, void *data)
382 if (!of_flat_dt_is_compatible(node, "rockchip,ion-reserve"))
385 prop = of_get_flat_dt_prop(node, "memory-reservation", &len);
386 if (!prop || (len != 2 * sizeof(unsigned long)))
389 base = be32_to_cpu(prop[0]);
390 size = be32_to_cpu(prop[1]);
392 pr_info("%s: reserve cma memory: %x %x\n", __func__, base, size);
394 dma_declare_contiguous(&rockchip_ion_cma_dev, size, base, 0);
400 static const struct of_device_id rockchip_ion_dt_ids[] = {
401 { .compatible = "rockchip,ion", },
405 static struct platform_driver ion_driver = {
406 .probe = rockchip_ion_probe,
407 .remove = rockchip_ion_remove,
409 .name = "ion-rockchip",
410 .owner = THIS_MODULE,
411 .of_match_table = of_match_ptr(rockchip_ion_dt_ids),
415 static int __init ion_init(void)
417 return platform_driver_register(&ion_driver);
420 static void __exit ion_exit(void)
422 platform_driver_unregister(&ion_driver);
425 subsys_initcall(ion_init);
426 module_exit(ion_exit);