Received: by 2002:a25:d7c1:0:0:0:0:0 with SMTP id o184csp1307718ybg; Fri, 18 Oct 2019 15:37:03 -0700 (PDT) X-Google-Smtp-Source: APXvYqwK9cp+XHUUJviIaSAVt64QBvBnthIaiRQJOzIGOy8hzX46sZIx1GYcvkJX3z2xD10ttwCX X-Received: by 2002:aa7:c6d5:: with SMTP id b21mr12378551eds.37.1571438222986; Fri, 18 Oct 2019 15:37:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1571438222; cv=none; d=google.com; s=arc-20160816; b=v/DicdgDK3MYLOFQ5sEgY7oZ2YhBMN2Ct0550eBQR+uuLdGvou5URSYVpTmHMspWzY /WHfPZBPy2fAKKU5obySSl7obQzA6R4JUMxJ/JJ/BFdmy1/spu7XFkFCHBe2m2K6UDce kwhTWne4ERO/J/n3UJ9h1mWoVVtlT5AU5yHM/M9zJKsL9JFQEoBk4LsObkmKTlmHNzWF N616h/cGWOyBg/t0yD877iwtpJeto9gOYGSK4q2BZuOZ2lKi1YH92WkIfkWFP2eP2ZoE YJ2HGLe14z6dyqItnvnz1TOMpwMR05V8StMju190YsjxHLQvjGmwDAqywKxB3iUOgl0N LRxQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature; bh=p2Be0C4qL+ABPGADFRkpPnXO4Go7F19pYM8Im08Sq8E=; b=GcV5O7KQJVNXJUUj8mSUMlKKu6NFJ6ckifOIcPV+e8BadFXeQdGi3y+xYK7FFn09s9 OSz5vLNbhlEVhaZ0BIO8e7wg4AcDCP/rgNcgJo1Cg+QRWHFQvNCTR12yrh5tPzl7vZpV SN/AbYhbUr2eKMfFX8O3XqoF6R0keNed8Lm7Oj3u/wEv5YwI1DvsMFB4QDzURvbzW1HH xUhMxhDeTokzS/UdFsY766WzRev/z5U4SmUcgjL3qzq9COK1d+GqnjhxsVvwPQPUzECb lP1ayoVnDk+kik3FZM5UUt/tNd+SkXbgZMZ87T81qVagjcDwIILiHHAU/pz5Fd19lv5y nhbw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=imZxwwpK; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v12si4970766edf.212.2019.10.18.15.36.40; Fri, 18 Oct 2019 15:37:02 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=imZxwwpK; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2391386AbfJRFXk (ORCPT + 99 others); Fri, 18 Oct 2019 01:23:40 -0400 Received: from mail-pl1-f196.google.com ([209.85.214.196]:42442 "EHLO mail-pl1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2438917AbfJRFXh (ORCPT ); Fri, 18 Oct 2019 01:23:37 -0400 Received: by mail-pl1-f196.google.com with SMTP id g9so960060plj.9 for ; Thu, 17 Oct 2019 22:23:36 -0700 (PDT) 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=p2Be0C4qL+ABPGADFRkpPnXO4Go7F19pYM8Im08Sq8E=; b=imZxwwpKk8QNbI06J7n1TQBJkTHSKJ5siWiyWyEV3yKZ+ING8ynCwmmxiR4/UoYS+K 9f2v7O1AFdf93o215390acCXMXmf3NBfNWRLlzbkRKXLPldcoK2hdUGhtwWVZIuwdfNa AGpDAwu9oyi017InTrI7+qzqsLEFOdWiJNvQGTlIroXBc5c1bdp0O5TpIx20n5S1lgfo VTE0mYL04LGN5CSGm9k5bcgVUrCmV6iyu23Gaspn2JnAICBOmII8MVuNaItu4XoaPIsG /fTqlKuTls+xXleyn0GpeYpU3TyVT4hzIDtKPrqUiUFrV2wmHH5ZQv2yitRtlHjM1Zr1 Ai7w== 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=p2Be0C4qL+ABPGADFRkpPnXO4Go7F19pYM8Im08Sq8E=; b=Xs9pYZzyGaguF/5BOrV7K962/t1SNej5HRN1Srsmaq1UOKy2/B+VJi0BNt/iIJbrl9 PV5GK9K+VJSrB345TZIaVbv8m1ueIY2D6ZiySyd3EpWln0FtGo1qI6tfDDGm4Gi/tqz1 yvibUz3C+UubjSd7Pk2+AoGsG+FFBY5xcB6Qnzlnx5q/XMfngAW3N9gHTx5txYMo0Jfn F4d6wZkAAehttNV720BuvZk1pZtMo+0SV2vy48k/lMP+cRbsnVULuwQre/gCLpbU+1KY 1Y0O4QV+pvkhAbdD1yeP7Ln9JXDOLs2ybn4DpF8LF10jg+arXNLDwkUfgQD4ddYtRWHw yKiA== X-Gm-Message-State: APjAAAVjpN/mfEpDIK3ujgYASVtfF6uvtTLpKBZMeB5vqOZHfez7g3lq yCyTyg+QKsUXFCpb2hiyGHJFlSsnJpA= X-Received: by 2002:a17:902:9a01:: with SMTP id v1mr8035340plp.132.1571376215601; Thu, 17 Oct 2019 22:23:35 -0700 (PDT) Received: from localhost.localdomain ([2601:1c2:680:1319:692:26ff:feda:3a81]) by smtp.gmail.com with ESMTPSA id g202sm4729336pfb.155.2019.10.17.22.23.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Oct 2019 22:23:34 -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" , Christoph Hellwig , Chenbo Feng , Alistair Strachan , Hridya Valsaraju , Hillf Danton , dri-devel@lists.freedesktop.org Subject: [PATCH v12 5/5] kselftests: Add dma-heap test Date: Fri, 18 Oct 2019 05:23:23 +0000 Message-Id: <20191018052323.21659-6-john.stultz@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20191018052323.21659-1-john.stultz@linaro.org> References: <20191018052323.21659-1-john.stultz@linaro.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.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: Christoph Hellwig Cc: Chenbo Feng Cc: Alistair Strachan Cc: Hridya Valsaraju Cc: Hillf Danton Cc: dri-devel@lists.freedesktop.org Reviewed-by: Benjamin Gaignard Reviewed-by: Brian Starkey Acked-by: Laura Abbott Tested-by: Ayan Kumar Halder Signed-off-by: John Stultz --- 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 v7: * Whitespace fixup for checkpatch v8: * More checkpatch whitespace fixups v9: * Better handling error returns out to main, suggested by Brian Starkey * Switch to using snprintf, suggested by Brian --- tools/testing/selftests/dmabuf-heaps/Makefile | 9 + .../selftests/dmabuf-heaps/dmabuf-heap.c | 238 ++++++++++++++++++ 2 files changed, 247 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 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..b36dd9f35c19 --- /dev/null +++ b/tools/testing/selftests/dmabuf-heaps/dmabuf-heap.c @@ -0,0 +1,238 @@ +// 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]; + + snprintf(name, 80, "%s%u", drmstr, i); + + fd = open(name, O_RDWR); + if (fd < 0) + continue; + + if (!check_vgem(fd)) { + close(fd); + fd = -1; + 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 = snprintf(buf, 256, "%s/%s", DEVPATH, name); + if (ret < 0) { + printf("snprintf 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) + 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 int 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"); + ret = -1; + 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"); + ret = -1; + 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); + ret = 0; + +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); + + return ret; +} + +int main(void) +{ + DIR *d; + struct dirent *dir; + int ret = -1; + + 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; + + ret = do_test(dir->d_name); + if (ret) + break; + } + closedir(d); + + return ret; +} -- 2.17.1