From patchwork Mon Jun 24 19:49:08 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stultz X-Patchwork-Id: 167648 Delivered-To: patches@linaro.org Received: by 2002:a92:4782:0:0:0:0:0 with SMTP id e2csp4622546ilk; Mon, 24 Jun 2019 12:49:24 -0700 (PDT) X-Received: by 2002:a17:902:bb95:: with SMTP id m21mr152175306pls.154.1561405764613; Mon, 24 Jun 2019 12:49:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1561405764; cv=none; d=google.com; s=arc-20160816; b=xKksPVxpbnDE1Wg0RIPehyavUwqg8U0qJm5KxLhS74+p993eXBGatbCXZ5xmQxjiDv 2jUzVI6S3cEMKYGPfB8ng5prl3opZ4Mlfj/kmYe64rX9mY5r9KJOwBwARlF/aqDWMuBR 8B+NnZLjkMmXxF5Ke18PwABqbdPVWlsJXdDTXv5tOkejC97YSx2Xsghwii168p1NaCiV HwTB9IHd6kAeJcob2XCPqAmvBJBzBns5ACd8Y/UW2A5+oGl/x1o1265D5iZsEejvPYnT y13zm70ZlnnoD+TwCqhiyTjYyY52Zcq5+q9GKa3WWCmnpvEKKIq1f/Jbg5aH9rFUOrD/ MuwQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=2hSI8EbMMFV4OCju8jPVBwC8bC74JNJP0N5AkdXGFG8=; b=P+mGzywLZkdGJi3BN0afZU529PO08hWl/N6q1jTw/c5mjpnaet8VHGpGPsNAbluCNZ 6jhC+3JZEXAUzXg32Prq6dimGxl311fZJkBoystSgLLzu/WWHbo+h1Mzc5AVG+cz8tkU n5+bIFd/fBsNeYSWhc6+LpbB/uurhUe1lOsHR+GjUjE3Fbe4Uejq8yWIyFNdnNHo5DGi yCqObYXRgBZjr4TtBVPxW6QYHfw45UYFTfkzKFgq0knayv7/7ZxuKJG8Ia43Ne0GKlVX tkd17SvIM4ZqNca3CX7SMliiRXXkZmMzCo6giuYd13/3FES1n0xKxQ2IZZjP9vCB/XJ5 LIXA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="HJ8/f17U"; spf=pass (google.com: domain of john.stultz@linaro.org designates 209.85.220.65 as permitted sender) smtp.mailfrom=john.stultz@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from mail-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id h3sor6647003pgc.1.2019.06.24.12.49.24 for (Google Transport Security); Mon, 24 Jun 2019 12:49:24 -0700 (PDT) Received-SPF: pass (google.com: domain of john.stultz@linaro.org designates 209.85.220.65 as permitted sender) client-ip=209.85.220.65; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="HJ8/f17U"; spf=pass (google.com: domain of john.stultz@linaro.org designates 209.85.220.65 as permitted sender) smtp.mailfrom=john.stultz@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=2hSI8EbMMFV4OCju8jPVBwC8bC74JNJP0N5AkdXGFG8=; b=HJ8/f17UiYsYKLq92bi4VUN5Un6UE93aX2ImJYs9C+FtSZnvWN4z2rP0bGOrLWjjkF j/4a8+5HXiMEfk/njOZbvIjxs0VMaqOOqA7Y/aEWC+xGsdhv9NjF36z53pmlgNiyr5gs FV07tGXAkkokHDRyQBOonjNjDiOu/uUaZtEbWKibac3KSx24wPcKT7alDpM1fOC5Id2+ COeIR+AFU4kjVjBwqzTKvFwL9hp1shKRUkKVwDkZ870msBn90gxJfHQmzDbqvjXbOIpz dySZHmX54+/bsEA6l9CcUoPYlCoLRsZ6k5qiJcMNiDJ66FvLIQCPcztxqIXab3OMlGOU viAQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=2hSI8EbMMFV4OCju8jPVBwC8bC74JNJP0N5AkdXGFG8=; b=QgkG1WjTMLTfcNTyY5wOppcnXh+b7UNsjeMv0It/5gVdtRw+lv6c92A62ENNlM8LyI h3U02PL8sbjwlB5JqnR498v60iSBzsQo7idxXv92WTMRBr/escUKJevypHAH82tBs9zm RtjbI6Xa7/CM9OCinEmwZAVXYq6PVlQaYOHiWUgIeyfz1qy272v1SGsVM6oDTvPbdiYn NOqbpO5uFov/KTcBwm9AwtSWWrYckh8RAn20Kgb3gI7JB1M+l6BgqjycMO1N1kw5dAAl OL+YTmOi/BuYHwdR/pn9VzFXSJ87NVhdG73CZQKCaUlHXbQ4GU7uzyLBToIi0ECqB7lY ujVA== X-Gm-Message-State: APjAAAUsGQvSO+1yh3ftAxEo1mGO7Jp4gO3+MVylFKj1TrcdQhy1tIWL 3JfG2Y7stK5JSa5AWMA104pjrjIz X-Google-Smtp-Source: APXvYqz4euNAuhD6FRrwHxg71dXALMoi3gDsa00rimNtHcAF8xHYqr0WyFukBz8MrFTl62EEguvIsQ== X-Received: by 2002:a63:c301:: with SMTP id c1mr34700786pgd.41.1561405763984; Mon, 24 Jun 2019 12:49:23 -0700 (PDT) Return-Path: Received: from localhost.localdomain ([2601:1c2:680:1319:692:26ff:feda:3a81]) by smtp.gmail.com with ESMTPSA id v3sm7957031pfm.188.2019.06.24.12.49.22 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Mon, 24 Jun 2019 12:49:23 -0700 (PDT) From: John Stultz To: lkml Cc: John Stultz , Benjamin Gaignard , Sumit Semwal , Liam Mark , Pratik Patel , Brian Starkey , Vincent Donnefort , Sudipto Paul , "Andrew F . Davis" , Xu YiPing , "Chenfeng (puck)" , butao , "Xiaqing (A)" , Yudongbin , Christoph Hellwig , Chenbo Feng , Alistair Strachan , dri-devel@lists.freedesktop.org Subject: [PATCH v6 5/5] kselftests: Add dma-heap test Date: Mon, 24 Jun 2019 19:49:08 +0000 Message-Id: <20190624194908.121273-6-john.stultz@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190624194908.121273-1-john.stultz@linaro.org> References: <20190624194908.121273-1-john.stultz@linaro.org> Add very trivial allocation and import test for dma-heaps, utilizing the vgem driver as a test importer. A good chunk of this code taken from: tools/testing/selftests/android/ion/ionmap_test.c Originally by Laura Abbott Cc: Benjamin Gaignard Cc: Sumit Semwal Cc: Liam Mark Cc: Pratik Patel Cc: Brian Starkey Cc: Vincent Donnefort Cc: Sudipto Paul Cc: Andrew F. Davis Cc: Xu YiPing Cc: "Chenfeng (puck)" Cc: butao Cc: "Xiaqing (A)" Cc: Yudongbin Cc: Christoph Hellwig Cc: Chenbo Feng Cc: Alistair Strachan Cc: dri-devel@lists.freedesktop.org Reviewed-by: Benjamin Gaignard Signed-off-by: John Stultz Change-Id: Ib98569fdda6378eb086b8092fb5d6bd419b8d431 --- v2: * Switched to use reworked dma-heap apis v3: * Add simple mmap * Utilize dma-buf testdev to test importing v4: * Rework to use vgem * Pass in fd_flags to match interface changes * Skip . and .. dirs v6: * Number of style/cleanups suggested by Brian --- tools/testing/selftests/dmabuf-heaps/Makefile | 9 + .../selftests/dmabuf-heaps/dmabuf-heap.c | 234 ++++++++++++++++++ 2 files changed, 243 insertions(+) create mode 100644 tools/testing/selftests/dmabuf-heaps/Makefile create mode 100644 tools/testing/selftests/dmabuf-heaps/dmabuf-heap.c -- 2.17.1 diff --git a/tools/testing/selftests/dmabuf-heaps/Makefile b/tools/testing/selftests/dmabuf-heaps/Makefile new file mode 100644 index 000000000000..8c4c36e2972d --- /dev/null +++ b/tools/testing/selftests/dmabuf-heaps/Makefile @@ -0,0 +1,9 @@ +# 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 000000000000..1e93b6fbe459 --- /dev/null +++ b/tools/testing/selftests/dmabuf-heaps/dmabuf-heap.c @@ -0,0 +1,234 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + + +#include "../../../../include/uapi/linux/dma-heap.h" + +#define DEVPATH "/dev/dma_heap" + +static int check_vgem(int fd) +{ + drm_version_t version = { 0 }; + char name[5]; + int ret; + + version.name_len = 4; + version.name = name; + + ret = ioctl(fd, DRM_IOCTL_VERSION, &version); + if (ret) + return 0; + + return !strcmp(name, "vgem"); +} + +static int open_vgem(void) +{ + int i, fd; + const char *drmstr = "/dev/dri/card"; + + fd = -1; + for (i = 0; i < 16; i++) { + char name[80]; + + sprintf(name, "%s%u", drmstr, i); + + fd = open(name, O_RDWR); + if (fd < 0) + continue; + + if (!check_vgem(fd)) { + close(fd); + continue; + } else { + break; + } + + } + return fd; +} + +static int import_vgem_fd(int vgem_fd, int dma_buf_fd, uint32_t *handle) +{ + struct drm_prime_handle import_handle = { + .fd = dma_buf_fd, + .flags = 0, + .handle = 0, + }; + int ret; + + ret = ioctl(vgem_fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &import_handle); + if (ret == 0) + *handle = import_handle.handle; + return ret; +} + +static void close_handle(int vgem_fd, uint32_t handle) +{ + struct drm_gem_close close = { + .handle = handle, + }; + + ioctl(vgem_fd, DRM_IOCTL_GEM_CLOSE, &close); +} + + +static 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; +} + +static int dmabuf_heap_alloc(int fd, size_t len, unsigned int flags, int *dmabuf_fd) +{ + struct dma_heap_allocation_data data = { + .len = len, + .fd_flags = O_RDWR | O_CLOEXEC, + .heap_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; +} + +static void dmabuf_sync(int fd, int start_stop) +{ + struct dma_buf_sync sync = { + .flags = start_stop | DMA_BUF_SYNC_RW, + }; + int ret; + + ret = ioctl(fd, DMA_BUF_IOCTL_SYNC, &sync); + if (ret) + printf("sync failed %d\n", errno); +} + +#define ONE_MEG (1024*1024) + +static void do_test(char *heap_name) +{ + int heap_fd = -1, dmabuf_fd = -1, importer_fd = -1; + uint32_t handle = 0; + void *p = NULL; + 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); + if (ret) { + printf("Allocation Failed!\n"); + goto out; + } + /* mmap and write a simple pattern */ + p = mmap(NULL, + ONE_MEG, + PROT_READ | PROT_WRITE, + MAP_SHARED, + dmabuf_fd, + 0); + if (p == MAP_FAILED) { + printf("mmap() failed: %m\n"); + goto out; + } + printf("mmap passed\n"); + + + dmabuf_sync(dmabuf_fd, DMA_BUF_SYNC_START); + + memset(p, 1, ONE_MEG / 2); + memset((char *)p + ONE_MEG / 2, 0, ONE_MEG / 2); + dmabuf_sync(dmabuf_fd, DMA_BUF_SYNC_END); + + importer_fd = open_vgem(); + if (importer_fd < 0) { + ret = importer_fd; + printf("Failed to open vgem\n"); + goto out; + } + + ret = import_vgem_fd(importer_fd, dmabuf_fd, &handle); + if (ret < 0) { + printf("Failed to import buffer\n"); + goto out; + } + printf("import passed\n"); + + dmabuf_sync(dmabuf_fd, DMA_BUF_SYNC_START); + memset(p, 0xff, ONE_MEG); + dmabuf_sync(dmabuf_fd, DMA_BUF_SYNC_END); + printf("syncs passed\n"); + + close_handle(importer_fd, handle); + +out: + if (p) + munmap(p, ONE_MEG); + if (importer_fd >= 0) + close(importer_fd); + 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) { + if (!strncmp(dir->d_name, ".", 2)) + continue; + if (!strncmp(dir->d_name, "..", 3)) + continue; + + do_test(dir->d_name); + } + closedir(d); + + return 0; +}