diff options
author | Sumit Semwal <sumit.semwal@linaro.org> | 2014-11-24 11:54:11 +0530 |
---|---|---|
committer | Sumit Semwal <sumit.semwal@linaro.org> | 2014-11-24 12:12:05 +0530 |
commit | 3139eba1edc1017e472af97ea0f53e1b1de7f95e (patch) | |
tree | 52cc0fe609b27d5fb80a334166b867b071192e91 | |
parent | 8a0d63917ec9917f8c6284c5faf643ae4b9efaa6 (diff) |
cenalloc: simple test casecenalloc_wip
Add simple test case to cenalloc.
Signed-off-by: Benjamin Gaignard <benjamin.gaignard@linaro.org>
[benjiG: helped with making the test case work.]
Signed-off-by: Sumit Semwal <sumit.semwal@linaro.org>
-rw-r--r-- | drivers/cenalloc/Makefile | 2 | ||||
-rw-r--r-- | drivers/cenalloc/cenalloc_test.c | 169 |
2 files changed, 170 insertions, 1 deletions
diff --git a/drivers/cenalloc/Makefile b/drivers/cenalloc/Makefile index 2f69b61c0215..940d7c953a33 100644 --- a/drivers/cenalloc/Makefile +++ b/drivers/cenalloc/Makefile @@ -1,3 +1,3 @@ # Makefile for the cenalloc helper -obj-y += cenalloc.o cenalloc_system_contig.o +obj-y += cenalloc.o cenalloc_system_contig.o cenalloc_test.o diff --git a/drivers/cenalloc/cenalloc_test.c b/drivers/cenalloc/cenalloc_test.c new file mode 100644 index 000000000000..6738965ca9ab --- /dev/null +++ b/drivers/cenalloc/cenalloc_test.c @@ -0,0 +1,169 @@ +/* + * Simple Test code for constraint-aware allocation helper framework + * for devices using dma-buf + * + * drivers/cenalloc/cenalloc_test.c + * + * Copyright (C) 2014 Linaro, Inc + * + * This software is licensed under the terms of the GNU General Public + * License version 2, as published by the Free Software Foundation, and + * may be copied, distributed, and modified under those terms. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + */ + +#include <linux/err.h> +#include <linux/miscdevice.h> +#include <linux/module.h> +#include <linux/slab.h> +#include <linux/init.h> +#include <linux/bootmem.h> +#include <linux/memblock.h> +#include <linux/sizes.h> +#include <linux/io.h> +#include "cenalloc.h" +#include "cenalloc_priv.h" + +static struct miscdevice dev1 = { + .minor = MISC_DYNAMIC_MINOR, + .name = "cenalloc-test1", + .parent = NULL, +}; + +static struct miscdevice dev2 = { + .minor = MISC_DYNAMIC_MINOR, + .name = "cenalloc-test2", + .parent = NULL, +}; + +static int __init cenalloc_test_init(void) +{ + int err; + struct dma_buf *dmabuf; + struct dma_buf_attachment *attach1, *attach2; + struct sg_table *table1, *table2; + + err = misc_register(&dev1); + if (err) { + pr_err("cenalloc-test1: failed to register misc device.\n"); + return err; + } + dev1.this_device->dma_parms = devm_kzalloc(dev1.this_device, + sizeof(*dev1.this_device->dma_parms), + GFP_KERNEL); + + err = misc_register(&dev2); + if (err) { + pr_err("cenalloc-test2: failed to register misc device.\n"); + return err; + } + dev2.this_device->dma_parms = devm_kzalloc(dev2.this_device, + sizeof(*dev2.this_device->dma_parms), + GFP_KERNEL); + +/* The following is the sequence to follow for dma-buf functions; this is + * for reference only; will add it in relevant sections in the test code. + * + * 1. create the dmabuf via cenalloc_buffer_create;*/ + dmabuf = cenalloc_buffer_create(2048, 0, 0); + + if (IS_ERR(dmabuf)) { + printk (KERN_ERR "create buffer failed\n"); + return -EINVAL; + } + + /* 2. set constraints for the attaching devices:*/ + dev1.this_device->dma_parms->access_constraints_mask = + CENALLOC_ALLOCATOR_SYSTEM_MASK | + CENALLOC_ALLOCATOR_SYSTEM_CONTIG_MASK; + + dev2.this_device->dma_parms->access_constraints_mask = + CENALLOC_ALLOCATOR_SYSTEM_MASK; + +/* 3. For each attaching device, call dma_buf_attach(); + * NOTE: Do remember that dma_buf_map_attachment() *MUST NOT* be called + * before all attaching devices have attached themselves to the dmabuf. + * + * Thanks to the constraints mask, each dma_buf_attach() call will check + * against already attached devices, to see if the constraints are + * compatible or not; if not, attach() would return an error. + * + * Also, the dma_buf_fd() and dma_buf_get() would need to be called for + * each buffer shared via userspace, before calling dma_buf_attach() on it. + */ + attach1 = dma_buf_attach(dmabuf, dev1.this_device); + if (IS_ERR_OR_NULL(attach1)) { + pr_err("Couldn't attach device with constraint_mask: %0lx\n", + dev1.this_device->dma_parms->access_constraints_mask); + pr_err("Current constraint_mask for dma-buf: %0lx\n", + dmabuf->access_constraints_mask); + goto err1; + } + + attach2 = dma_buf_attach(dmabuf, dev2.this_device); + if (IS_ERR_OR_NULL(attach2)) { + pr_err("Couldn't attach device with constraint_mask: %0lx\n", + dev2.this_device->dma_parms->access_constraints_mask); + pr_err("Current constraint_mask for dma-buf: %0lx\n", + dmabuf->access_constraints_mask); + goto err2; + } + + pr_info(" Successfully Attached both devices \n"); + +/* 4. Now, follow the dma_buf* operations sequence as desired; eg + * NOTE: the very first call to dma_buf_map_attachment() will allocate + * the backing storage, by finding the right allocator for the constraint; + * subsequent calls would just return the associated sg_table. + */ + table1 = dma_buf_map_attachment(attach1, DMA_BIDIRECTIONAL); + if (IS_ERR(table1)) { + pr_err("Couldn't map table1\n"); + goto err3; + } +/* do something with the sg_table;*/ + + dma_buf_unmap_attachment(attach1, table1, DMA_BIDIRECTIONAL); + + + table2 = dma_buf_map_attachment(attach2, DMA_BIDIRECTIONAL); + if (IS_ERR(table2)) { + pr_err("Couldn't map table2\n"); + goto err3; + } +/* do something with the sg_table;*/ + + dma_buf_unmap_attachment(attach2, table2, DMA_BIDIRECTIONAL); + +/* 5. If you're done with the use of the buffer; then for each importer, do + */ +err3: + dma_buf_detach(dmabuf, attach2); + +err2: + dma_buf_detach(dmabuf, attach1); +err1: + dma_buf_put(dmabuf); + + return 0; +} + + +device_initcall(cenalloc_test_init); + +static void __exit cenalloc_test_exit(void) +{ + misc_deregister(&dev1); + misc_deregister(&dev2); +} +__exitcall(cenalloc_test_exit); + + +MODULE_AUTHOR("Sumit Semwal <sumit.semwal@linaro.org>"); +MODULE_DESCRIPTION("Constraint Aware Central Allocation Helper Test"); +MODULE_LICENSE("GPL"); |