From patchwork Mon Aug 14 20:18:32 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Chris Wilson X-Patchwork-Id: 9900049 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 09431602CA for ; Mon, 14 Aug 2017 20:19:15 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C9C2E2832B for ; Mon, 14 Aug 2017 20:19:13 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id BE483283C9; Mon, 14 Aug 2017 20:19:13 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.2 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 61BB92832B for ; Mon, 14 Aug 2017 20:19:13 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id EFC466E222; Mon, 14 Aug 2017 20:19:12 +0000 (UTC) X-Original-To: intel-gfx@lists.freedesktop.org Delivered-To: intel-gfx@lists.freedesktop.org Received: from fireflyinternet.com (mail.fireflyinternet.com [109.228.58.192]) by gabe.freedesktop.org (Postfix) with ESMTPS id 1256E6E225 for ; Mon, 14 Aug 2017 20:19:11 +0000 (UTC) X-Default-Received-SPF: pass (skip=forwardok (res=PASS)) x-ip-name=78.156.65.138; Received: from haswell.alporthouse.com (unverified [78.156.65.138]) by fireflyinternet.com (Firefly Internet (M1)) with ESMTP id 8258363-1500050 for multiple; Mon, 14 Aug 2017 21:19:05 +0100 Received: by haswell.alporthouse.com (sSMTP sendmail emulation); Mon, 14 Aug 2017 21:19:06 +0100 From: Chris Wilson To: intel-gfx@lists.freedesktop.org Date: Mon, 14 Aug 2017 21:18:32 +0100 Message-Id: <20170814201848.28216-8-chris@chris-wilson.co.uk> X-Mailer: git-send-email 2.13.3 In-Reply-To: <20170814201848.28216-1-chris@chris-wilson.co.uk> References: <20170814201848.28216-1-chris@chris-wilson.co.uk> MIME-Version: 1.0 X-Originating-IP: 78.156.65.138 X-Country: code=GB country="United Kingdom" ip=78.156.65.138 Subject: [Intel-gfx] [PATCH igt 08/24] tests/prime_rw: Add basic tests for reading/writing to a dmabuf X-BeenThere: intel-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Intel graphics driver community testing & development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: intel-gfx-bounces@lists.freedesktop.org Sender: "Intel-gfx" X-Virus-Scanned: ClamAV using ClamSMTP The idea is to implement read(dmabuf) and write(dambuf) so provide some tests. Signed-off-by: Chris Wilson --- tests/Makefile.sources | 1 + tests/prime_rw.c | 179 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 180 insertions(+) create mode 100644 tests/prime_rw.c diff --git a/tests/Makefile.sources b/tests/Makefile.sources index bb013c78..7a99cafc 100644 --- a/tests/Makefile.sources +++ b/tests/Makefile.sources @@ -225,6 +225,7 @@ TESTS_progs = \ prime_mmap \ prime_mmap_coherency \ prime_mmap_kms \ + prime_rw \ prime_self_import \ prime_udl \ prime_vgem \ diff --git a/tests/prime_rw.c b/tests/prime_rw.c new file mode 100644 index 00000000..65abec5d --- /dev/null +++ b/tests/prime_rw.c @@ -0,0 +1,179 @@ +/* + * Copyright © 2017 Intel Corporation + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +/* + * Testcase: Check whether read()/write()ing dma-buf works + */ +#define _GNU_SOURCE +#include +#include +#include + +#include "drmtest.h" +#include "ioctl_wrappers.h" + +static int create_dmabuf(int fd, int size) +{ + uint32_t handle; + int dmabuf; + + handle = gem_create(fd, size); + dmabuf = prime_handle_to_fd_for_mmap(fd, handle); + gem_close(fd, handle); + + return dmabuf; +} + +static bool has_dma_buf_read(int fd) +{ + int dmabuf; + int ret; + + dmabuf = create_dmabuf(fd, 4096); + ret = read(dmabuf, NULL, 0); + close(dmabuf); + + errno = 0; + return ret == 0; +} + +static bool has_dma_buf_write(int fd) +{ + int dmabuf; + int ret; + + dmabuf = create_dmabuf(fd, 4096); + ret = write(dmabuf, NULL, 0); + close(dmabuf); + + errno = 0; + return ret == 0; +} + +static void test_read(int fd) +{ + uint32_t buf[4096]; + uint32_t *ptr; + int i, len, offset; + int dmabuf; + + dmabuf = create_dmabuf(fd, sizeof(buf)); + igt_assert(dmabuf != -1); + + ptr = mmap(NULL, sizeof(buf), PROT_WRITE, MAP_SHARED, dmabuf, 0); + igt_assert(ptr != MAP_FAILED); + + prime_sync_start(dmabuf, true); + for (i = 0; i < ARRAY_SIZE(buf); i++) + ptr[i] = i; + prime_sync_end(dmabuf, true); + + munmap(ptr, sizeof(buf)); + + prime_sync_start(dmabuf, false); + for (len = 1; len <= 4096; len <<= 1) { + const int sz = len * sizeof(buf[0]); + + offset = len/2; + lseek(dmabuf, offset * sizeof(buf[0]), SEEK_SET); + for (; offset + len <= 4096; offset += len) { + igt_assert_eq(read(dmabuf, buf, sz), sz); + for (i = 0; i < len; i++) + igt_assert_eq(buf[i], offset + i); + } + igt_assert_eq(read(dmabuf, buf, sz), + (4096 - offset) * sizeof(buf[0])); + igt_assert_eq(read(dmabuf, buf, sz), 0); + } + prime_sync_end(dmabuf, false); + + close(dmabuf); +} + +static void test_write(int fd) +{ + uint32_t buf[4096]; + uint32_t *ptr; + int i, len, offset; + int dmabuf; + + dmabuf = create_dmabuf(fd, sizeof(buf)); + igt_assert(dmabuf != -1); + + for (i = 0; i < ARRAY_SIZE(buf); i++) + buf[i] = i; + + ptr = mmap(NULL, sizeof(buf), PROT_READ, MAP_SHARED, dmabuf, 0); + igt_assert(ptr != MAP_FAILED); + + prime_sync_start(dmabuf, true); + for (len = 1; len <= 4096; len <<= 1) { + const int sz = len * sizeof(buf[0]); + + offset = len/2; + lseek(dmabuf, offset * sizeof(buf[0]), SEEK_SET); + for (; offset + len <= 4096; offset += len) { + igt_assert_eq(write(dmabuf, buf, sz), sz); + for (i = 0; i < len; i++) + igt_assert_eq(ptr[i + offset], i); + } + igt_assert_eq(write(dmabuf, buf, sz), + (4096 - offset) * sizeof(buf[0])); + igt_assert_eq(write(dmabuf, buf, sz), 0); + } + prime_sync_end(dmabuf, true); + + munmap(ptr, sizeof(buf)); + + close(dmabuf); +} + +igt_main +{ + int fd = -1; + + igt_fixture { + fd = drm_open_driver(DRIVER_INTEL); + } + + igt_subtest_group { + igt_fixture { + igt_require((has_dma_buf_read(fd))); + } + + igt_subtest("basic-read") + test_read(fd); + } + + igt_subtest_group { + igt_fixture { + igt_require((has_dma_buf_write(fd))); + } + + igt_subtest("basic-write") + test_write(fd); + } + + igt_fixture + close(fd); +}