1 /*
2  * Copyright © 2008 Intel Corporation
3  *
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:
10  *
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
13  * Software.
14  *
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
21  * IN THE SOFTWARE.
22  *
23  * Authors:
24  *    Eric Anholt <eric@anholt.net>
25  *
26  */
27 
28 #include "igt.h"
29 #include <unistd.h>
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <fcntl.h>
34 #include <inttypes.h>
35 #include <errno.h>
36 #include <sys/stat.h>
37 #include <sys/ioctl.h>
38 #include "drm.h"
39 
40 #define OBJECT_SIZE 16384
41 
42 static int
do_read(int fd,int handle,void * buf,int offset,int size)43 do_read(int fd, int handle, void *buf, int offset, int size)
44 {
45 	struct drm_i915_gem_pread gem_pread;
46 
47 	/* Ensure that we don't have any convenient data in buf in case
48 	 * we fail.
49 	 */
50 	memset(buf, 0xd0, size);
51 
52 	memset(&gem_pread, 0, sizeof(gem_pread));
53 	gem_pread.handle = handle;
54 	gem_pread.data_ptr = to_user_pointer(buf);
55 	gem_pread.size = size;
56 	gem_pread.offset = offset;
57 
58 	return ioctl(fd, DRM_IOCTL_I915_GEM_PREAD, &gem_pread);
59 }
60 
61 static int
do_write(int fd,int handle,void * buf,int offset,int size)62 do_write(int fd, int handle, void *buf, int offset, int size)
63 {
64 	struct drm_i915_gem_pwrite gem_pwrite;
65 
66 	memset(&gem_pwrite, 0, sizeof(gem_pwrite));
67 	gem_pwrite.handle = handle;
68 	gem_pwrite.data_ptr = to_user_pointer(buf);
69 	gem_pwrite.size = size;
70 	gem_pwrite.offset = offset;
71 
72 	return ioctl(fd, DRM_IOCTL_I915_GEM_PWRITE, &gem_pwrite);
73 }
74 
75 int fd;
76 uint32_t handle;
77 
78 igt_main
79 {
80 	uint8_t expected[OBJECT_SIZE];
81 	uint8_t buf[OBJECT_SIZE];
82 	int ret;
83 
84 	igt_skip_on_simulation();
85 
86 	igt_fixture {
87 		fd = drm_open_driver(DRIVER_INTEL);
88 
89 		handle = gem_create(fd, OBJECT_SIZE);
90 	}
91 
92 	igt_subtest("new-obj") {
93 		igt_info("Testing contents of newly created object.\n");
94 		ret = do_read(fd, handle, buf, 0, OBJECT_SIZE);
95 		igt_assert(ret == 0);
96 		memset(&expected, 0, sizeof(expected));
97 		igt_assert(memcmp(expected, buf, sizeof(expected)) == 0);
98 	}
99 
100 	igt_subtest("beyond-EOB") {
101 		igt_info("Testing read beyond end of buffer.\n");
102 		ret = do_read(fd, handle, buf, OBJECT_SIZE / 2, OBJECT_SIZE);
103 		igt_assert(ret == -1 && errno == EINVAL);
104 	}
105 
106 	igt_subtest("read-write") {
107 		igt_info("Testing full write of buffer\n");
108 		memset(buf, 0, sizeof(buf));
109 		memset(buf + 1024, 0x01, 1024);
110 		memset(expected + 1024, 0x01, 1024);
111 		ret = do_write(fd, handle, buf, 0, OBJECT_SIZE);
112 		igt_assert(ret == 0);
113 		ret = do_read(fd, handle, buf, 0, OBJECT_SIZE);
114 		igt_assert(ret == 0);
115 		igt_assert(memcmp(buf, expected, sizeof(buf)) == 0);
116 
117 		igt_info("Testing partial write of buffer\n");
118 		memset(buf + 4096, 0x02, 1024);
119 		memset(expected + 4096, 0x02, 1024);
120 		ret = do_write(fd, handle, buf + 4096, 4096, 1024);
121 		igt_assert(ret == 0);
122 		ret = do_read(fd, handle, buf, 0, OBJECT_SIZE);
123 		igt_assert(ret == 0);
124 		igt_assert(memcmp(buf, expected, sizeof(buf)) == 0);
125 
126 		igt_info("Testing partial read of buffer\n");
127 		ret = do_read(fd, handle, buf, 512, 1024);
128 		igt_assert(ret == 0);
129 		igt_assert(memcmp(buf, expected + 512, 1024) == 0);
130 	}
131 
132 	igt_subtest("read-bad-handle") {
133 		igt_info("Testing read of bad buffer handle\n");
134 		ret = do_read(fd, 1234, buf, 0, 1024);
135 		igt_assert(ret == -1 && errno == ENOENT);
136 	}
137 
138 	igt_subtest("write-bad-handle") {
139 		igt_info("Testing write of bad buffer handle\n");
140 		ret = do_write(fd, 1234, buf, 0, 1024);
141 		igt_assert(ret == -1 && errno == ENOENT);
142 	}
143 
144 	igt_fixture
145 		close(fd);
146 }
147