2 * Copyright © 2008 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 * Eric Anholt <eric@anholt.net>
36 #include <sys/ioctl.h>
38 #include "ioctl_wrappers.h"
41 #define OBJECT_SIZE 16384
44 do_read(int fd, int handle, void *buf, int offset, int size)
46 struct drm_i915_gem_pread gem_pread;
48 /* Ensure that we don't have any convenient data in buf in case
51 memset(buf, 0xd0, size);
53 memset(&gem_pread, 0, sizeof(gem_pread));
54 gem_pread.handle = handle;
55 gem_pread.data_ptr = (uintptr_t)buf;
56 gem_pread.size = size;
57 gem_pread.offset = offset;
59 return ioctl(fd, DRM_IOCTL_I915_GEM_PREAD, &gem_pread);
63 do_write(int fd, int handle, void *buf, int offset, int size)
65 struct drm_i915_gem_pwrite gem_pwrite;
67 memset(&gem_pwrite, 0, sizeof(gem_pwrite));
68 gem_pwrite.handle = handle;
69 gem_pwrite.data_ptr = (uintptr_t)buf;
70 gem_pwrite.size = size;
71 gem_pwrite.offset = offset;
73 return ioctl(fd, DRM_IOCTL_I915_GEM_PWRITE, &gem_pwrite);
81 uint8_t expected[OBJECT_SIZE];
82 uint8_t buf[OBJECT_SIZE];
85 igt_skip_on_simulation();
90 handle = gem_create(fd, OBJECT_SIZE);
93 igt_subtest("new-obj") {
94 igt_info("Testing contents of newly created object.\n");
95 ret = do_read(fd, handle, buf, 0, OBJECT_SIZE);
97 memset(&expected, 0, sizeof(expected));
98 igt_assert(memcmp(expected, buf, sizeof(expected)) == 0);
101 igt_subtest("beyond-EOB") {
102 igt_info("Testing read beyond end of buffer.\n");
103 ret = do_read(fd, handle, buf, OBJECT_SIZE / 2, OBJECT_SIZE);
104 igt_assert(ret == -1 && errno == EINVAL);
107 igt_subtest("read-write") {
108 igt_info("Testing full write of buffer\n");
109 memset(buf, 0, sizeof(buf));
110 memset(buf + 1024, 0x01, 1024);
111 memset(expected + 1024, 0x01, 1024);
112 ret = do_write(fd, handle, buf, 0, OBJECT_SIZE);
113 igt_assert(ret == 0);
114 ret = do_read(fd, handle, buf, 0, OBJECT_SIZE);
115 igt_assert(ret == 0);
116 igt_assert(memcmp(buf, expected, sizeof(buf)) == 0);
118 igt_info("Testing partial write of buffer\n");
119 memset(buf + 4096, 0x02, 1024);
120 memset(expected + 4096, 0x02, 1024);
121 ret = do_write(fd, handle, buf + 4096, 4096, 1024);
122 igt_assert(ret == 0);
123 ret = do_read(fd, handle, buf, 0, OBJECT_SIZE);
124 igt_assert(ret == 0);
125 igt_assert(memcmp(buf, expected, sizeof(buf)) == 0);
127 igt_info("Testing partial read of buffer\n");
128 ret = do_read(fd, handle, buf, 512, 1024);
129 igt_assert(ret == 0);
130 igt_assert(memcmp(buf, expected + 512, 1024) == 0);
133 igt_subtest("read-bad-handle") {
134 igt_info("Testing read of bad buffer handle\n");
135 ret = do_read(fd, 1234, buf, 0, 1024);
136 igt_assert(ret == -1 && errno == ENOENT);
139 igt_subtest("write-bad-handle") {
140 igt_info("Testing write of bad buffer handle\n");
141 ret = do_write(fd, 1234, buf, 0, 1024);
142 igt_assert(ret == -1 && errno == ENOENT);