blob: bd1d232bb3afbefff11b3ccc34777a80684669a9 [file] [log] [blame]
Eric Anholt8c741ed2008-04-23 13:06:58 -07001/*
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>
Eric Anholt3148c162008-04-29 13:45:43 -070036#include "drm.h"
Eric Anholt26550052008-06-11 14:42:40 -070037#include "i915_drm.h"
Eric Anholt8c741ed2008-04-23 13:06:58 -070038
Eric Anholt3148c162008-04-29 13:45:43 -070039#define OBJECT_SIZE 16384
Eric Anholt8c741ed2008-04-23 13:06:58 -070040
41int do_read(int fd, int handle, void *buf, int offset, int size)
42{
Eric Anholt26550052008-06-11 14:42:40 -070043 struct drm_i915_gem_pread read;
Eric Anholt8c741ed2008-04-23 13:06:58 -070044
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;
Eric Anholteffc6d92008-05-07 16:00:58 -070052 read.data_ptr = (uintptr_t)buf;
Eric Anholt8c741ed2008-04-23 13:06:58 -070053 read.size = size;
54 read.offset = offset;
55
Eric Anholt26550052008-06-11 14:42:40 -070056 return ioctl(fd, DRM_IOCTL_I915_GEM_PREAD, &read);
Eric Anholt8c741ed2008-04-23 13:06:58 -070057}
58
59int do_write(int fd, int handle, void *buf, int offset, int size)
60{
Eric Anholt26550052008-06-11 14:42:40 -070061 struct drm_i915_gem_pwrite write;
Eric Anholt8c741ed2008-04-23 13:06:58 -070062
63 memset(&write, 0, sizeof(write));
64 write.handle = handle;
Eric Anholteffc6d92008-05-07 16:00:58 -070065 write.data_ptr = (uintptr_t)buf;
Eric Anholt8c741ed2008-04-23 13:06:58 -070066 write.size = size;
67 write.offset = offset;
68
Eric Anholt26550052008-06-11 14:42:40 -070069 return ioctl(fd, DRM_IOCTL_I915_GEM_PWRITE, &write);
Eric Anholt8c741ed2008-04-23 13:06:58 -070070}
71
72int main(int argc, char **argv)
73{
74 int fd;
Eric Anholt26550052008-06-11 14:42:40 -070075 struct drm_i915_gem_create create;
Eric Anholt3148c162008-04-29 13:45:43 -070076 uint8_t expected[OBJECT_SIZE];
77 uint8_t buf[OBJECT_SIZE];
Eric Anholt8c741ed2008-04-23 13:06:58 -070078 int ret;
79 int handle;
80
Eric Anholt3148c162008-04-29 13:45:43 -070081 fd = drm_open_any();
Eric Anholt8c741ed2008-04-23 13:06:58 -070082
Eric Anholt6e46a3c2008-05-12 15:42:20 -070083 memset(&create, 0, sizeof(create));
84 create.size = OBJECT_SIZE;
Eric Anholt26550052008-06-11 14:42:40 -070085 ret = ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create);
Eric Anholt8c741ed2008-04-23 13:06:58 -070086 assert(ret == 0);
Eric Anholt6e46a3c2008-05-12 15:42:20 -070087 handle = create.handle;
Eric Anholt8c741ed2008-04-23 13:06:58 -070088
Eric Anholt6e46a3c2008-05-12 15:42:20 -070089 printf("Testing contents of newly created object.\n");
Eric Anholt3148c162008-04-29 13:45:43 -070090 ret = do_read(fd, handle, buf, 0, OBJECT_SIZE);
Eric Anholt8c741ed2008-04-23 13:06:58 -070091 assert(ret == 0);
92 memset(&expected, 0, sizeof(expected));
93 assert(memcmp(expected, buf, sizeof(expected)) == 0);
94
95 printf("Testing read beyond end of buffer.\n");
Eric Anholt3148c162008-04-29 13:45:43 -070096 ret = do_read(fd, handle, buf, OBJECT_SIZE / 2, OBJECT_SIZE);
Eric Anholt1bdf35f2008-07-28 11:24:00 -070097 printf("%d %d\n", ret, errno);
Eric Anholt8c741ed2008-04-23 13:06:58 -070098 assert(ret == -1 && errno == EINVAL);
99
100 printf("Testing full write of buffer\n");
101 memset(buf, 0, sizeof(buf));
102 memset(buf + 1024, 0x01, 1024);
103 memset(expected + 1024, 0x01, 1024);
Eric Anholt3148c162008-04-29 13:45:43 -0700104 ret = do_write(fd, handle, buf, 0, OBJECT_SIZE);
Eric Anholt8c741ed2008-04-23 13:06:58 -0700105 assert(ret == 0);
Eric Anholt3148c162008-04-29 13:45:43 -0700106 ret = do_read(fd, handle, buf, 0, OBJECT_SIZE);
Eric Anholt8c741ed2008-04-23 13:06:58 -0700107 assert(ret == 0);
108 assert(memcmp(buf, expected, sizeof(buf)) == 0);
109
110 printf("Testing partial write of buffer\n");
111 memset(buf + 4096, 0x02, 1024);
112 memset(expected + 4096, 0x02, 1024);
113 ret = do_write(fd, handle, buf + 4096, 4096, 1024);
114 assert(ret == 0);
Eric Anholt3148c162008-04-29 13:45:43 -0700115 ret = do_read(fd, handle, buf, 0, OBJECT_SIZE);
Eric Anholt8c741ed2008-04-23 13:06:58 -0700116 assert(ret == 0);
117 assert(memcmp(buf, expected, sizeof(buf)) == 0);
118
119 printf("Testing partial read of buffer\n");
120 ret = do_read(fd, handle, buf, 512, 1024);
121 assert(ret == 0);
122 assert(memcmp(buf, expected + 512, 1024) == 0);
123
Eric Anholt1bdf35f2008-07-28 11:24:00 -0700124 printf("Testing read of bad buffer handle\n");
125 ret = do_read(fd, 1234, buf, 0, 1024);
126 assert(ret == -1 && errno == EBADF);
127
128 printf("Testing write of bad buffer handle\n");
129 ret = do_write(fd, 1234, buf, 0, 1024);
130 assert(ret == -1 && errno == EBADF);
131
Eric Anholt8c741ed2008-04-23 13:06:58 -0700132 close(fd);
133
134 return 0;
135}