[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <alpine.DEB.2.10.1903131320560.5023@lmark-linux.qualcomm.com>
Date: Wed, 13 Mar 2019 13:23:32 -0700 (PDT)
From: Liam Mark <lmark@...eaurora.org>
To: John Stultz <john.stultz@...aro.org>
cc: lkml <linux-kernel@...r.kernel.org>,
Laura Abbott <labbott@...hat.com>,
Benjamin Gaignard <benjamin.gaignard@...aro.org>,
Greg KH <gregkh@...uxfoundation.org>,
Sumit Semwal <sumit.semwal@...aro.org>,
Brian Starkey <Brian.Starkey@....com>,
"Andrew F . Davis" <afd@...com>, Chenbo Feng <fengc@...gle.com>,
Alistair Strachan <astrachan@...gle.com>,
dri-devel@...ts.freedesktop.org
Subject: Re: [RFC][PATCH 5/5 v2] kselftests: Add dma-heap test
On Tue, 5 Mar 2019, John Stultz wrote:
> Add very trivial allocation test for dma-heaps.
>
> TODO: Need to actually do some validation on
> the returned dma-buf.
>
> Cc: Laura Abbott <labbott@...hat.com>
> Cc: Benjamin Gaignard <benjamin.gaignard@...aro.org>
> Cc: Greg KH <gregkh@...uxfoundation.org>
> Cc: Sumit Semwal <sumit.semwal@...aro.org>
> Cc: Liam Mark <lmark@...eaurora.org>
> Cc: Brian Starkey <Brian.Starkey@....com>
> Cc: Andrew F. Davis <afd@...com>
> Cc: Chenbo Feng <fengc@...gle.com>
> Cc: Alistair Strachan <astrachan@...gle.com>
> Cc: dri-devel@...ts.freedesktop.org
> Signed-off-by: John Stultz <john.stultz@...aro.org>
> ---
> v2: Switched to use reworked dma-heap apis
> ---
> tools/testing/selftests/dmabuf-heaps/Makefile | 11 +++
> tools/testing/selftests/dmabuf-heaps/dmabuf-heap.c | 96 ++++++++++++++++++++++
> 2 files changed, 107 insertions(+)
> create mode 100644 tools/testing/selftests/dmabuf-heaps/Makefile
> create mode 100644 tools/testing/selftests/dmabuf-heaps/dmabuf-heap.c
>
> diff --git a/tools/testing/selftests/dmabuf-heaps/Makefile b/tools/testing/selftests/dmabuf-heaps/Makefile
> new file mode 100644
> index 0000000..c414ad3
> --- /dev/null
> +++ b/tools/testing/selftests/dmabuf-heaps/Makefile
> @@ -0,0 +1,11 @@
> +# SPDX-License-Identifier: GPL-2.0
> +CFLAGS += -static -O3 -Wl,-no-as-needed -Wall
> +#LDLIBS += -lrt -lpthread -lm
> +
> +# these are all "safe" tests that don't modify
> +# system time or require escalated privileges
> +TEST_GEN_PROGS = dmabuf-heap
> +
> +
> +include ../lib.mk
> +
> diff --git a/tools/testing/selftests/dmabuf-heaps/dmabuf-heap.c b/tools/testing/selftests/dmabuf-heaps/dmabuf-heap.c
> new file mode 100644
> index 0000000..06837a4
> --- /dev/null
> +++ b/tools/testing/selftests/dmabuf-heaps/dmabuf-heap.c
> @@ -0,0 +1,96 @@
> +// SPDX-License-Identifier: GPL-2.0
> +
> +#include <dirent.h>
> +#include <errno.h>
> +#include <fcntl.h>
> +#include <stdio.h>
> +#include <string.h>
> +#include <unistd.h>
> +#include <sys/ioctl.h>
> +#include <sys/mman.h>
> +#include <sys/types.h>
> +
> +#include "../../../../include/uapi/linux/dma-heap.h"
> +
> +#define DEVPATH "/dev/dma_heap"
> +
> +int dmabuf_heap_open(char *name)
> +{
> + int ret, fd;
> + char buf[256];
> +
> + ret = sprintf(buf, "%s/%s", DEVPATH, name);
> + if (ret < 0) {
> + printf("sprintf failed!\n");
> + return ret;
> + }
> +
> + fd = open(buf, O_RDWR);
> + if (fd < 0)
> + printf("open %s failed!\n", buf);
> + return fd;
> +}
> +
> +int dmabuf_heap_alloc(int fd, size_t len, unsigned int flags, int *dmabuf_fd)
> +{
> + struct dma_heap_allocation_data data = {
> + .len = len,
> + .flags = flags,
> + };
> + int ret;
> +
> + if (dmabuf_fd == NULL)
> + return -EINVAL;
> +
> + ret = ioctl(fd, DMA_HEAP_IOC_ALLOC, &data);
> + if (ret < 0)
> + return ret;
> + *dmabuf_fd = (int)data.fd;
> + return ret;
> +}
> +
> +#define ONE_MEG (1024*1024)
> +
> +void do_test(char *heap_name)
> +{
> + int heap_fd = -1, dmabuf_fd = -1;
> + int ret;
> +
> + printf("Testing heap: %s\n", heap_name);
> +
> + heap_fd = dmabuf_heap_open(heap_name);
> + if (heap_fd < 0)
> + return;
> +
> + printf("Allocating 1 MEG\n");
> + ret = dmabuf_heap_alloc(heap_fd, ONE_MEG, 0, &dmabuf_fd);
Just be aware that some CMA heaps may already have all their memory
allocated by the client, so you may see intermittent failures depending on
when you run the test and failrues may
be more common when run on certain platform.
> + if (ret)
> + goto out;
> +
> + /* DO SOMETHING WITH THE DMABUF HERE? */
> +
> +out:
> + if (dmabuf_fd >= 0)
> + close(dmabuf_fd);
> + if (heap_fd >= 0)
> + close(heap_fd);
> +}
> +
> +
> +int main(void)
> +{
> + DIR *d;
> + struct dirent *dir;
> +
> + d = opendir(DEVPATH);
> + if (!d) {
> + printf("No %s directory?\n", DEVPATH);
> + return -1;
> + }
> +
> + while ((dir = readdir(d)) != NULL)
> + do_test(dir->d_name);
> +
> +
> + return 0;
> +}
> --
> 2.7.4
>
>
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum,
a Linux Foundation Collaborative Project
Powered by blists - more mailing lists