3 * Copyright (C) 2013 Google, Inc.
5 * This software is licensed under the terms of the GNU General Public
6 * License version 2, as published by the Free Software Foundation, and
7 * may be copied, distributed, and modified under those terms.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
16 #define pr_fmt(fmt) "ion-test: " fmt
18 #include <linux/dma-buf.h>
19 #include <linux/dma-direction.h>
21 #include <linux/miscdevice.h>
23 #include <linux/module.h>
24 #include <linux/platform_device.h>
25 #include <linux/sched.h>
26 #include <linux/slab.h>
27 #include <linux/uaccess.h>
28 #include <linux/vmalloc.h>
31 #include "../uapi/ion_test.h"
33 #define u64_to_uptr(x) ((void __user *)(unsigned long)(x))
35 struct ion_test_device {
36 struct miscdevice misc;
39 struct ion_test_data {
40 struct dma_buf *dma_buf;
44 static int ion_handle_test_dma(struct device *dev, struct dma_buf *dma_buf,
45 void __user *ptr, size_t offset, size_t size,
49 struct dma_buf_attachment *attach;
50 struct sg_table *table;
51 pgprot_t pgprot = pgprot_writecombine(PAGE_KERNEL);
52 enum dma_data_direction dir = write ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
53 struct sg_page_iter sg_iter;
54 unsigned long offset_page;
56 attach = dma_buf_attach(dma_buf, dev);
58 return PTR_ERR(attach);
60 table = dma_buf_map_attachment(attach, dir);
62 return PTR_ERR(table);
64 offset_page = offset >> PAGE_SHIFT;
67 for_each_sg_page(table->sgl, &sg_iter, table->nents, offset_page) {
68 struct page *page = sg_page_iter_page(&sg_iter);
69 void *vaddr = vmap(&page, 1, VM_MAP, pgprot);
70 size_t to_copy = PAGE_SIZE - offset;
72 to_copy = min(to_copy, size);
79 ret = copy_from_user(vaddr + offset, ptr, to_copy);
81 ret = copy_to_user(ptr, vaddr + offset, to_copy);
96 dma_buf_unmap_attachment(attach, table, dir);
97 dma_buf_detach(dma_buf, attach);
101 static int ion_handle_test_kernel(struct dma_buf *dma_buf, void __user *ptr,
102 size_t offset, size_t size, bool write)
105 unsigned long page_offset = offset >> PAGE_SHIFT;
106 size_t copy_offset = offset % PAGE_SIZE;
107 size_t copy_size = size;
108 enum dma_data_direction dir = write ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
110 if (offset > dma_buf->size || size > dma_buf->size - offset)
113 ret = dma_buf_begin_cpu_access(dma_buf, dir);
117 while (copy_size > 0) {
119 void *vaddr = dma_buf_kmap(dma_buf, page_offset);
124 to_copy = min_t(size_t, PAGE_SIZE - copy_offset, copy_size);
127 ret = copy_from_user(vaddr + copy_offset, ptr, to_copy);
129 ret = copy_to_user(ptr, vaddr + copy_offset, to_copy);
131 dma_buf_kunmap(dma_buf, page_offset, vaddr);
137 copy_size -= to_copy;
143 dma_buf_end_cpu_access(dma_buf, dir);
147 static long ion_test_ioctl(struct file *filp, unsigned int cmd,
150 struct ion_test_data *test_data = filp->private_data;
154 struct ion_test_rw_data test_rw;
157 if (_IOC_SIZE(cmd) > sizeof(data))
160 if (_IOC_DIR(cmd) & _IOC_WRITE)
161 if (copy_from_user(&data, (void __user *)arg, _IOC_SIZE(cmd)))
165 case ION_IOC_TEST_SET_FD:
167 struct dma_buf *dma_buf = NULL;
171 dma_buf = dma_buf_get((int)arg);
173 return PTR_ERR(dma_buf);
175 if (test_data->dma_buf)
176 dma_buf_put(test_data->dma_buf);
177 test_data->dma_buf = dma_buf;
180 case ION_IOC_TEST_DMA_MAPPING:
182 ret = ion_handle_test_dma(test_data->dev, test_data->dma_buf,
183 u64_to_uptr(data.test_rw.ptr),
189 case ION_IOC_TEST_KERNEL_MAPPING:
191 ret = ion_handle_test_kernel(test_data->dma_buf,
192 u64_to_uptr(data.test_rw.ptr),
202 if (_IOC_DIR(cmd) & _IOC_READ) {
203 if (copy_to_user((void __user *)arg, &data, sizeof(data)))
209 static int ion_test_open(struct inode *inode, struct file *file)
211 struct ion_test_data *data;
212 struct miscdevice *miscdev = file->private_data;
214 data = kzalloc(sizeof(*data), GFP_KERNEL);
218 data->dev = miscdev->parent;
220 file->private_data = data;
225 static int ion_test_release(struct inode *inode, struct file *file)
227 struct ion_test_data *data = file->private_data;
234 static const struct file_operations ion_test_fops = {
235 .owner = THIS_MODULE,
236 .unlocked_ioctl = ion_test_ioctl,
237 .compat_ioctl = ion_test_ioctl,
238 .open = ion_test_open,
239 .release = ion_test_release,
242 static int __init ion_test_probe(struct platform_device *pdev)
245 struct ion_test_device *testdev;
247 testdev = devm_kzalloc(&pdev->dev, sizeof(struct ion_test_device),
252 testdev->misc.minor = MISC_DYNAMIC_MINOR;
253 testdev->misc.name = "ion-test";
254 testdev->misc.fops = &ion_test_fops;
255 testdev->misc.parent = &pdev->dev;
256 ret = misc_register(&testdev->misc);
258 pr_err("failed to register misc device.\n");
262 platform_set_drvdata(pdev, testdev);
267 static int ion_test_remove(struct platform_device *pdev)
269 struct ion_test_device *testdev;
271 testdev = platform_get_drvdata(pdev);
275 misc_deregister(&testdev->misc);
279 static struct platform_device *ion_test_pdev;
280 static struct platform_driver ion_test_platform_driver = {
281 .remove = ion_test_remove,
287 static int __init ion_test_init(void)
289 ion_test_pdev = platform_device_register_simple("ion-test",
291 if (IS_ERR(ion_test_pdev))
292 return PTR_ERR(ion_test_pdev);
294 return platform_driver_probe(&ion_test_platform_driver, ion_test_probe);
297 static void __exit ion_test_exit(void)
299 platform_driver_unregister(&ion_test_platform_driver);
300 platform_device_unregister(ion_test_pdev);
303 module_init(ion_test_init);
304 module_exit(ion_test_exit);
305 MODULE_LICENSE("GPL v2");