From patchwork Thu Oct 10 18:44:58 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stultz X-Patchwork-Id: 175813 Delivered-To: patches@linaro.org Received: by 2002:a92:7e96:0:0:0:0:0 with SMTP id q22csp2682570ill; Thu, 10 Oct 2019 11:45:14 -0700 (PDT) X-Received: by 2002:aa7:99c7:: with SMTP id v7mr11769125pfi.219.1570733113974; Thu, 10 Oct 2019 11:45:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1570733113; cv=none; d=google.com; s=arc-20160816; b=0izL8k0lQNlE8PdNN0YAeAC/ekVIw8NGVWcewWhjo2eE3G0SRP8RBQXMn3B3ynGOtu f5SvhIJeU6xGmY9aHKLhSAimdwPbIMQsnUAstNxXIEaTgOcU61J2yLPd2LcesziGlGzD sZHMMnk7jfhHbmxrG0pQCYVCYQ4l+b/UvKCfU14oDHTASqyEgegvZSCYKt69g5WridhY r3D4NeGY06td4hAcmhKSU+j1ABmwd5ZvWTQ5pcolbBcKTYHdBXOKVmbOh8VjrH2uz93c 75jryrG//w8U7soA9OdMuce4bCEctgMaB/pPPSnkW491kgT8t+TKotUHUIg9ge5npYNS kLpA== 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=p2Be0C4qL+ABPGADFRkpPnXO4Go7F19pYM8Im08Sq8E=; b=ePLTjMxdFbPf6QwUyVYeLlXwFgay6f5cu8D0K4oM78N6q8J2KNX7UxMRiwbuuww5aH 36v8VUD/D8Im5LEL0Ug2KKXlhlWxAtTs6CbsVgGB0G5eyu93kVdDL4CyHwwkFC9eaKMm GNQwUgHD+6aMSVToUU/QnkAaXY3RzYesi19/5TonPckA1VBT9Nb0IObIK34uIeOf9XIu n/XxBDaAr2f81Xp2kPYGJjr/JB0B+vW8Df2Y2fg9rvddOtH/6GxpsYzfAok9u1WTqz5T HHm/eswX7CG7dA+esstmCAyhm7JlG6KwaDGhKuXUm+QHcAIUuj0KN7/nnVGOdEqzCj7S MH1Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=tYcFtZWv; 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 u5sor8780371plz.40.2019.10.10.11.45.13 for (Google Transport Security); Thu, 10 Oct 2019 11:45:13 -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=tYcFtZWv; 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=p2Be0C4qL+ABPGADFRkpPnXO4Go7F19pYM8Im08Sq8E=; b=tYcFtZWv2yx4RwC6Nn/054maUImFg0FD0qzNZBVZPT+wtUbceF898c1iGeFO9jygRo 86x5wuxVeccJT/rExh9Ou731JVVyu3pLIbC4zfF6r/L5D91OLj+FofzQXVEW+b/uCTQN 8D1hpnH1uTCY6ADyGM/0eqYLb88Hfym304jVMk51oOM0Rz32mUj3X4t0r0Blc5hhMetF yDJIfxKQJS5TmOxI6H7Bbd4g+00rswj21ajFciAL4KjYh7GHSlT88t9YbYUY23AxQiBF 2t/9SiE1JNezB8hzl1VypWo34eoWmQyj43ne3rSfNhDMXDYNpTXofcVmyX9I2c1u2SET nJGQ== 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=L5LJOjy7NK19wO4ZGE53ZD78Bg1o8RcRuIiJtGZkxTbSGp/NE8cqs9JdY4LPhejvj7 7X2HY8EEi4nBKcC0xiv3Xedf/U8kzqoiM/RRdSyT5XR2doMPiTMgqSx/Sl+V6ufr7Qph XXKHdDJAXsfuCJN9a5KfFS9HpsCP4VgK9Me9cBMUwSYj3+hv573jupxCIW65OfUVZlZJ kvffBSS6DVJGVAPaPlRK571y3EL9Bkpa7AprFRlACUdE1eRiywnPdELasH/bESWZSIOI 2ce8c6vcgnaS6S7PWcflhWV0Zk7C2nSq3Y2gOghnBKQBbXgXf79tOAW81GvjiWDUBl5f zaDg== X-Gm-Message-State: APjAAAVo3vAQUH4EuqEbkQzkCKilnZDuz5SIo/Ikoc3YvrtG9Zmi0hS8 Ii2Xo8M67UXWRxL4YMMmCJ1U68F0BV4kiQ== X-Google-Smtp-Source: APXvYqxkJChSbMhA9CgcF4eVG1wRhxJAj55AsIEz2RRlPARXBwPAwnxiJzK2GDcXJA1z/IKVMcNYxg== X-Received: by 2002:a17:90b:316:: with SMTP id ay22mr12567097pjb.6.1570733113520; Thu, 10 Oct 2019 11:45:13 -0700 (PDT) Return-Path: Received: from localhost.localdomain ([2601:1c2:680:1319:692:26ff:feda:3a81]) by smtp.gmail.com with ESMTPSA id w65sm7541732pfb.106.2019.10.10.11.45.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Oct 2019 11:45:12 -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 v11 5/5] kselftests: Add dma-heap test Date: Thu, 10 Oct 2019 18:44:58 +0000 Message-Id: <20191010184458.51039-6-john.stultz@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20191010184458.51039-1-john.stultz@linaro.org> References: <20191010184458.51039-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: 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 -- 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..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; +}