Received: by 10.223.185.116 with SMTP id b49csp1221059wrg; Wed, 21 Feb 2018 14:27:22 -0800 (PST) X-Google-Smtp-Source: AH8x227V6iNh16h7Hzsu3FozItzbJQf0rWxh96yrOHkF34lP7ZfMc4Dmy7MktcpHm3qWTziB9lfr X-Received: by 2002:a17:902:2b84:: with SMTP id l4-v6mr4522272plb.338.1519252042570; Wed, 21 Feb 2018 14:27:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519252042; cv=none; d=google.com; s=arc-20160816; b=bYElr/VYnsWrCibXq9j64flwgT1TvYKSDwZJpcZm47tNZJe33fgK9DY+/obFkSkDPS vjMHLnJ+Hi3vPPfW5Ype3eB6hfcS9h2KMY5h9DJ0gnsaw7Xgg2SDtR9SuKrirrXb7K9n SbuOWl2bGYlVEhyxyA7k3zjDCjBfFqN308qeprxUglOqPITY2r9MM4HLzT8W7bOPFesi pG8wZzW9gU2v792uOZMxEd8AOqcdefgMCdnfPcjl9aIdopaqnaFI3Bc2cZntLbn+cQ8R VeuBfP4V5XlWhplcVSH/BG3KxVRTuROWV4PX7rjCinNvc+yyrsMpKz3oM4BeNO/v3mQd m6Gw== 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:arc-authentication-results; bh=04oIE8c+Sl03vKWO255FWDgDp8/5CiuLbf1JdMVneuw=; b=tL/bwgiJQ5HfeAgidEByxqJyQ2WfE/gnQRzjK/KCdNRmpZblY1PM+cDgDbQpWwhH9T dSA6znELHeKOTnQTqlbCuZFIYHrG70tNJKpg0nOUWB+21A7Yikpqx3snkOracuzQkob6 HWPeMkuwtFYtR85gJPFZVi/tqIFiy4G+pMAYdpOyhWqCO7PTL8Z8ydqXIycYUWFG8PrC f9b9FZAfYT4QHuVFYCTzupSsDo+ybWtqk7nW0fBw+wl2ou834jnIj0AmUrXgO0E+L7Wg HF91EZ2hCAUxu22xuR8M4WOFeBzgznt58aNnQY+qTg9USKj7C2w5h4gP+zfxo6GHQUkg 638Q== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id i62si3680948pfe.364.2018.02.21.14.27.07; Wed, 21 Feb 2018 14:27:22 -0800 (PST) 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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751485AbeBUW0L (ORCPT + 99 others); Wed, 21 Feb 2018 17:26:11 -0500 Received: from mail-oi0-f67.google.com ([209.85.218.67]:34416 "EHLO mail-oi0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751447AbeBUWZ4 (ORCPT ); Wed, 21 Feb 2018 17:25:56 -0500 Received: by mail-oi0-f67.google.com with SMTP id c83so2332262oib.1 for ; Wed, 21 Feb 2018 14:25:56 -0800 (PST) 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=04oIE8c+Sl03vKWO255FWDgDp8/5CiuLbf1JdMVneuw=; b=BMKRDwxB+FEAHnEsJYPkSfoFhe/zgVV/zLdVBCb9y/wPkw4zqeyvOA+59qPfjx0/0Q /9m33E56AGUBgVzZ3L989qiOurPGEHlepPQP2n849XyKjcBugOlNPQwgtcnbvGh5D7eQ 8IZMZax+0zIpsSt9UOtlB0KyBwh8H/vDn317egkmY588NTbqjjDNXcEftkp7KckLVeSn N+SeKW6+R1YEyXzxfnKSr5PTSsvmGUJdRHVGwDfjpJJyScOZXg3hOhZhaOKzhQZYZjfg 4Qca7Dgip3NguWdtPi8LPXd8RnrmvBy546m86+PwrsblR08dVX1hFXOjJq83wNaO3Zyx 7DnA== X-Gm-Message-State: APf1xPAFf8zKe8ohd32QKpHpGpgRKLlcSCIp9rPm5zwkNZ2IiKI0N34i y63BcxvRetzmomwdH+ytC7UH+g== X-Received: by 10.202.84.132 with SMTP id i126mr3023463oib.295.1519251955659; Wed, 21 Feb 2018 14:25:55 -0800 (PST) Received: from labbott-redhat.redhat.com ([2601:602:9802:a8dc::f21a]) by smtp.gmail.com with ESMTPSA id s62sm6317717oia.37.2018.02.21.14.25.53 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 21 Feb 2018 14:25:54 -0800 (PST) From: Laura Abbott To: Sumit Semwal , Daniel Vetter Cc: Laura Abbott , devel@driverdev.osuosl.org, linux-kernel@vger.kernel.org, Todd Kjos , dri-devel@lists.freedesktop.org, Chris Wilson , Liam Mark , Shuah Khan , linux-kselftest@vger.kernel.org Subject: [PATCHv2 2/2] selftests: ion: Add simple test with the vgem driver Date: Wed, 21 Feb 2018 14:25:43 -0800 Message-Id: <20180221222543.13633-3-labbott@redhat.com> X-Mailer: git-send-email 2.14.3 In-Reply-To: <20180221222543.13633-1-labbott@redhat.com> References: <20180221222543.13633-1-labbott@redhat.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Ion is designed to be a framework used by other clients who perform operations on the buffer. Use the DRM vgem client as a simple consumer. In conjunction with the dma-buf sync ioctls, this tests the full attach/map path for the system heap. Reviewed-by: Daniel Vetter Signed-off-by: Laura Abbott --- v2: Introduce drmIoctl wrapper per suggestion of Daniel Vetter to better match how DRM ioctls actually work. --- tools/testing/selftests/android/ion/Makefile | 3 +- tools/testing/selftests/android/ion/config | 1 + tools/testing/selftests/android/ion/ionmap_test.c | 149 ++++++++++++++++++++++ 3 files changed, 152 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/android/ion/ionmap_test.c diff --git a/tools/testing/selftests/android/ion/Makefile b/tools/testing/selftests/android/ion/Makefile index 96e0c448b39d..d23b6d537d8b 100644 --- a/tools/testing/selftests/android/ion/Makefile +++ b/tools/testing/selftests/android/ion/Makefile @@ -2,7 +2,7 @@ INCLUDEDIR := -I. -I../../../../../drivers/staging/android/uapi/ CFLAGS := $(CFLAGS) $(INCLUDEDIR) -Wall -O2 -g -TEST_GEN_FILES := ionapp_export ionapp_import +TEST_GEN_FILES := ionapp_export ionapp_import ionmap_test all: $(TEST_GEN_FILES) @@ -14,3 +14,4 @@ include ../../lib.mk $(OUTPUT)/ionapp_export: ionapp_export.c ipcsocket.c ionutils.c $(OUTPUT)/ionapp_import: ionapp_import.c ipcsocket.c ionutils.c +$(OUTPUT)/ionmap_test: ionmap_test.c ionutils.c diff --git a/tools/testing/selftests/android/ion/config b/tools/testing/selftests/android/ion/config index 19db6ca9aa2b..b4ad748a9dd9 100644 --- a/tools/testing/selftests/android/ion/config +++ b/tools/testing/selftests/android/ion/config @@ -2,3 +2,4 @@ CONFIG_ANDROID=y CONFIG_STAGING=y CONFIG_ION=y CONFIG_ION_SYSTEM_HEAP=y +CONFIG_DRM_VGEM=y diff --git a/tools/testing/selftests/android/ion/ionmap_test.c b/tools/testing/selftests/android/ion/ionmap_test.c new file mode 100644 index 000000000000..58ecdc3511d2 --- /dev/null +++ b/tools/testing/selftests/android/ion/ionmap_test.c @@ -0,0 +1,149 @@ +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include + +#include + +#include "ion.h" +#include "ionutils.h" + +/* Local copy of drmIoctl to match the expected behavior */ +static int drmIoctl(int fd, unsigned long request, void *arg) +{ + int ret; + + do { + ret = ioctl(fd, request, arg); + } while (ret == -1 && (errno == EINTR || errno == EAGAIN)); +} + +int check_vgem(int fd) +{ + drm_version_t version = { 0 }; + char name[5]; + int ret; + + version.name_len = 4; + version.name = name; + + ret = drmIoctl(fd, DRM_IOCTL_VERSION, &version); + if (ret) + return 1; + + return strcmp(name, "vgem"); +} + +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; +} + +int import_vgem_fd(int vgem_fd, int dma_buf_fd, uint32_t *handle) +{ + struct drm_prime_handle import_handle = { 0 }; + int ret; + + import_handle.fd = dma_buf_fd; + import_handle.flags = 0; + import_handle.handle = 0; + + ret = drmIoctl(vgem_fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &import_handle); + if (ret == 0) + *handle = import_handle.handle; + return ret; +} + +void close_handle(int vgem_fd, uint32_t handle) +{ + struct drm_gem_close close = { 0 }; + + close.handle = handle; + drmIoctl(vgem_fd, DRM_IOCTL_GEM_CLOSE, &close); +} + +int main() +{ + int ret, vgem_fd; + struct ion_buffer_info info; + uint32_t handle = 0; + struct dma_buf_sync sync = { 0 }; + + info.heap_type = ION_HEAP_TYPE_SYSTEM; + info.heap_size = 4096; + info.flag_type = ION_FLAG_CACHED; + + ret = ion_export_buffer_fd(&info); + if (ret < 0) { + printf("ion buffer alloc failed\n"); + return -1; + } + + vgem_fd = open_vgem(); + if (vgem_fd < 0) { + ret = vgem_fd; + printf("Failed to open vgem\n"); + goto out_ion; + } + + ret = import_vgem_fd(vgem_fd, info.buffd, &handle); + + if (ret < 0) { + printf("Failed to import buffer\n"); + goto out_vgem; + } + + /* + * While not strictly drm, the drmIoctl properly handles restarting + */ + sync.flags = DMA_BUF_SYNC_START | DMA_BUF_SYNC_RW; + ret = drmIoctl(info.buffd, DMA_BUF_IOCTL_SYNC, &sync); + if (ret) + printf("sync start failed %d\n", errno); + + memset(info.buffer, 0xff, 4096); + + sync.flags = DMA_BUF_SYNC_END | DMA_BUF_SYNC_RW; + ret = drmIoctl(info.buffd, DMA_BUF_IOCTL_SYNC, &sync); + if (ret) + printf("sync end failed %d\n", errno); + + close_handle(vgem_fd, handle); + ret = 0; + +out_vgem: + close(vgem_fd); +out_ion: + ion_close_buffer_fd(&info); + printf("done.\n"); + return ret; +} -- 2.14.3