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 <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <assert.h>
32 #include <fcntl.h>
33 #include <inttypes.h>
34 #include <errno.h>
35 #include <sys/stat.h>
36 #include "drm.h"
37 #include "i915_drm.h"
38 
39 #define OBJECT_SIZE 16384
40 
do_read(int fd,int handle,void * buf,int offset,int size)41 int do_read(int fd, int handle, void *buf, int offset, int size)
42 {
43 	struct drm_i915_gem_pread read;
44 
45 	/* Ensure that we don't have any convenient data in buf in case
46 	 * we fail.
47 	 */
48 	memset(buf, 0xd0, size);
49 
50 	memset(&read, 0, sizeof(read));
51 	read.handle = handle;
52 	read.data_ptr = (uintptr_t)buf;
53 	read.size = size;
54 	read.offset = offset;
55 
56 	return ioctl(fd, DRM_IOCTL_I915_GEM_PREAD, &read);
57 }
58 
do_write(int fd,int handle,void * buf,int offset,int size)59 int do_write(int fd, int handle, void *buf, int offset, int size)
60 {
61 	struct drm_i915_gem_pwrite write;
62 
63 	memset(&write, 0, sizeof(write));
64 	write.handle = handle;
65 	write.data_ptr = (uintptr_t)buf;
66 	write.size = size;
67 	write.offset = offset;
68 
69 	return ioctl(fd, DRM_IOCTL_I915_GEM_PWRITE, &write);
70 }
71 
main(int argc,char ** argv)72 int main(int argc, char **argv)
73 {
74 	int fd;
75 	struct drm_i915_gem_create create;
76 	uint8_t expected[OBJECT_SIZE];
77 	uint8_t buf[OBJECT_SIZE];
78 	int ret;
79 	int handle;
80 
81 	fd = drm_open_matching("8086:*", 0);
82 	if (fd < 0) {
83 		fprintf(stderr, "failed to open intel drm device, skipping\n");
84 		return 0;
85 	}
86 
87 	memset(&create, 0, sizeof(create));
88 	create.size = OBJECT_SIZE;
89 	ret = ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create);
90 	assert(ret == 0);
91 	handle = create.handle;
92 
93 	printf("Testing contents of newly created object.\n");
94 	ret = do_read(fd, handle, buf, 0, OBJECT_SIZE);
95 	assert(ret == 0);
96 	memset(&expected, 0, sizeof(expected));
97 	assert(memcmp(expected, buf, sizeof(expected)) == 0);
98 
99 	printf("Testing read beyond end of buffer.\n");
100 	ret = do_read(fd, handle, buf, OBJECT_SIZE / 2, OBJECT_SIZE);
101 	printf("%d %d\n", ret, errno);
102 	assert(ret == -1 && errno == EINVAL);
103 
104 	printf("Testing full write of buffer\n");
105 	memset(buf, 0, sizeof(buf));
106 	memset(buf + 1024, 0x01, 1024);
107 	memset(expected + 1024, 0x01, 1024);
108 	ret = do_write(fd, handle, buf, 0, OBJECT_SIZE);
109 	assert(ret == 0);
110 	ret = do_read(fd, handle, buf, 0, OBJECT_SIZE);
111 	assert(ret == 0);
112 	assert(memcmp(buf, expected, sizeof(buf)) == 0);
113 
114 	printf("Testing partial write of buffer\n");
115 	memset(buf + 4096, 0x02, 1024);
116 	memset(expected + 4096, 0x02, 1024);
117 	ret = do_write(fd, handle, buf + 4096, 4096, 1024);
118 	assert(ret == 0);
119 	ret = do_read(fd, handle, buf, 0, OBJECT_SIZE);
120 	assert(ret == 0);
121 	assert(memcmp(buf, expected, sizeof(buf)) == 0);
122 
123 	printf("Testing partial read of buffer\n");
124 	ret = do_read(fd, handle, buf, 512, 1024);
125 	assert(ret == 0);
126 	assert(memcmp(buf, expected + 512, 1024) == 0);
127 
128 	printf("Testing read of bad buffer handle\n");
129 	ret = do_read(fd, 1234, buf, 0, 1024);
130 	assert(ret == -1 && errno == ENOENT);
131 
132 	printf("Testing write of bad buffer handle\n");
133 	ret = do_write(fd, 1234, buf, 0, 1024);
134 	assert(ret == -1 && errno == ENOENT);
135 
136 	close(fd);
137 
138 	return 0;
139 }
140