Received: by 2002:a25:c593:0:0:0:0:0 with SMTP id v141csp2284210ybe; Sat, 7 Sep 2019 12:28:39 -0700 (PDT) X-Google-Smtp-Source: APXvYqw2I6ONn+DFSPusU8orZcenk8zKzkhoxjVz1J/rCxVM7mQ2asZ54DYi3GyyDDXjSeK29Ynd X-Received: by 2002:a17:902:748c:: with SMTP id h12mr16372274pll.58.1567884519572; Sat, 07 Sep 2019 12:28:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1567884519; cv=none; d=google.com; s=arc-20160816; b=QcEEIGg7WgeT/zjl2yDeyiua5xemDwOkOG+NHVgzsLCcS0B+cpP6UnAzdAsIaaUZyz hb1AN6MEZLYiDFp6o2AiT7I1IzXCW7wevfaes7nftJiw0e1d66BprdaGSHExr52NkaEH Z2eKjw52bqQcYTTWypMajl60hVhNgyF7TD7KNfcCUSeXwQlx+HGCF/zBgpN8Ez20T6Hm oxM3Or+NAC4q+hxjYw/tQ97s34Fw5R3zDzDIWkCMVItXY5qc5DTd3wWtOhC/J13kLfTm /amU92d335up+BelsYpYNZtBZOiob4ihgR8a5eh4RVZgMtDO4RPvv2NgeyGe+CqOUQMt QgZg== 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=NyBUASuAu3Awm8j3/xOM+PgdVeP3B+2DGc7L537MPcY=; b=BOLRyEQ9K74y9fSITYl1sVZphYQS9Xe4USDtLZRIzHJ586crksrRwqBSttek1Cg14V W34YgCzT2H2exQcPFvM0Qnu5KA57DdpRapCiMJ6q8AlG+LXtbVcJDHSP0stQndYneYm2 IXZ+6rqlMDYKYr6DytX4q8rSbd1fWNJc93R/buIWln7GpwM+kspkWWkByCjc8ATTJTX+ VA/Kp8C+6i7oKEJMAiWiq71b3WndT6kjGEkQjg1/sosgvl+mEcrAg6xqwcgWhsKhpU9a Lp36V8JU/kLA4oyxMUowJ1ktN5kLGTKk8FRjqRgQyQgBxp9tnEZg1xRrHpZ+GjxLtFK8 sKMw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="GKu/KlzE"; 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 j4si7568684pgq.375.2019.09.07.12.28.24; Sat, 07 Sep 2019 12:28:39 -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="GKu/KlzE"; 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 S2406183AbfIFSrc (ORCPT + 99 others); Fri, 6 Sep 2019 14:47:32 -0400 Received: from mail-pg1-f195.google.com ([209.85.215.195]:40182 "EHLO mail-pg1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2405122AbfIFSr2 (ORCPT ); Fri, 6 Sep 2019 14:47:28 -0400 Received: by mail-pg1-f195.google.com with SMTP id w10so3964427pgj.7 for ; Fri, 06 Sep 2019 11:47:28 -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=NyBUASuAu3Awm8j3/xOM+PgdVeP3B+2DGc7L537MPcY=; b=GKu/KlzEUje+qonPQGhCN6wEzMs6Tk3BiTe2zSuSTl/SKbVz0cawiehkQF3xoUHD3S FQQRf1NHztjrSInJLUY5Hif24eaWKBXoibpwfakX+3ezla1Oj0mgsNoqWYa/g2X5PgjY wbo4W5n+DdjhMpcSeMgZU6An9rTCoY6yV2u1xsMFkCMFa33+OohHtBx68e9ypZmQqWSK diBkQSzGtiUYd0RE0hc8x4rjw3/9JS1jcolyv4Aum3filKukVabwwuiJ+t4HX+BMOwDk l2hYmgjZucIe+mo9mOFPNSyNE5ElcAxCt53bkthPCsVgbT3hswo4Ecvw/NzrrnCAwdsy j0TQ== 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=NyBUASuAu3Awm8j3/xOM+PgdVeP3B+2DGc7L537MPcY=; b=Hen0ACvzeXcjvEW9KT5FrfVNu6p9mXaug7wZl+ICFbTZ4mxsqWxwDrv5thGLvLL1gI JL+SFI6oW+6/Two4AuogL+aDIleVNjM7X//LXp/TpwtqXBAxZmuEGMAyc4Nz+PGNex3k zzlCm/T3wiFvR6Dn+TARX0/idpKf0+Bql5Ota26e2VLMJPhgSfVvjKfhU1ONhIU5sTxw LUWVBSnm6cavcEOULoU5SrQRbMqCSX0R3QikYkDT7CRnJkZpttHQ3poHrHFjC5K344nN 8wlG52JZ2rkFBsGwAXo/wCvAG46TfOPWvD1g1wcNJwOzYagtWElE/Grrb9Gmxaif79jK mzmg== X-Gm-Message-State: APjAAAWJUKIHsBUAdk/JDtuufxkIaU6v+IOiWyS0tjuKpjvWOhUATf7x HQ/CD1miTe4GSeSFUNXf6QUY9PYX0bc= X-Received: by 2002:a62:a513:: with SMTP id v19mr11034618pfm.208.1567795647185; Fri, 06 Sep 2019 11:47:27 -0700 (PDT) Received: from localhost.localdomain ([2601:1c2:680:1319:692:26ff:feda:3a81]) by smtp.gmail.com with ESMTPSA id o35sm4889462pgm.29.2019.09.06.11.47.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 06 Sep 2019 11:47:26 -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 , dri-devel@lists.freedesktop.org Subject: [RESEND][PATCH v8 5/5] kselftests: Add dma-heap test Date: Fri, 6 Sep 2019 18:47:12 +0000 Message-Id: <20190906184712.91980-6-john.stultz@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190906184712.91980-1-john.stultz@linaro.org> References: <20190906184712.91980-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: dri-devel@lists.freedesktop.org Reviewed-by: Benjamin Gaignard 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 --- tools/testing/selftests/dmabuf-heaps/Makefile | 9 + .../selftests/dmabuf-heaps/dmabuf-heap.c | 230 ++++++++++++++++++ 2 files changed, 239 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..e439d6cf3d81 --- /dev/null +++ b/tools/testing/selftests/dmabuf-heaps/dmabuf-heap.c @@ -0,0 +1,230 @@ +// 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) + 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; +} -- 2.17.1