blob: d3d69e30123614d59840c1ba882153c68a3a5062 [file] [log] [blame]
Eric Anholt895a4152009-03-26 18:47:42 -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
Eric Anholt05817382009-03-27 16:04:25 -070028#include <unistd.h>
Eric Anholt895a4152009-03-26 18:47:42 -070029#include <stdlib.h>
30#include <stdio.h>
31#include <string.h>
32#include <assert.h>
33#include <fcntl.h>
34#include <inttypes.h>
35#include <errno.h>
36#include <sys/stat.h>
Eric Anholt05817382009-03-27 16:04:25 -070037#include <sys/ioctl.h>
Eric Anholt895a4152009-03-26 18:47:42 -070038#include "drm.h"
39#include "i915_drm.h"
Eric Anholt05817382009-03-27 16:04:25 -070040#include "drmtest.h"
Eric Anholt895a4152009-03-26 18:47:42 -070041
42#define OBJECT_SIZE 16384
43
Eric Anholt05817382009-03-27 16:04:25 -070044static int
45do_read(int fd, int handle, void *buf, int offset, int size)
Eric Anholt895a4152009-03-26 18:47:42 -070046{
Imre Deakb614b4d2012-10-10 16:04:41 +030047 struct drm_i915_gem_pread gem_pread;
Eric Anholt895a4152009-03-26 18:47:42 -070048
49 /* Ensure that we don't have any convenient data in buf in case
50 * we fail.
51 */
52 memset(buf, 0xd0, size);
53
Imre Deakb614b4d2012-10-10 16:04:41 +030054 memset(&gem_pread, 0, sizeof(gem_pread));
55 gem_pread.handle = handle;
56 gem_pread.data_ptr = (uintptr_t)buf;
57 gem_pread.size = size;
58 gem_pread.offset = offset;
Eric Anholt895a4152009-03-26 18:47:42 -070059
Imre Deakb614b4d2012-10-10 16:04:41 +030060 return ioctl(fd, DRM_IOCTL_I915_GEM_PREAD, &gem_pread);
Eric Anholt895a4152009-03-26 18:47:42 -070061}
62
Eric Anholt05817382009-03-27 16:04:25 -070063static int
64do_write(int fd, int handle, void *buf, int offset, int size)
Eric Anholt895a4152009-03-26 18:47:42 -070065{
Imre Deakb614b4d2012-10-10 16:04:41 +030066 struct drm_i915_gem_pwrite gem_pwrite;
Eric Anholt895a4152009-03-26 18:47:42 -070067
Imre Deakb614b4d2012-10-10 16:04:41 +030068 memset(&gem_pwrite, 0, sizeof(gem_pwrite));
69 gem_pwrite.handle = handle;
70 gem_pwrite.data_ptr = (uintptr_t)buf;
71 gem_pwrite.size = size;
72 gem_pwrite.offset = offset;
Eric Anholt895a4152009-03-26 18:47:42 -070073
Imre Deakb614b4d2012-10-10 16:04:41 +030074 return ioctl(fd, DRM_IOCTL_I915_GEM_PWRITE, &gem_pwrite);
Eric Anholt895a4152009-03-26 18:47:42 -070075}
76
77int main(int argc, char **argv)
78{
79 int fd;
Eric Anholt895a4152009-03-26 18:47:42 -070080 uint8_t expected[OBJECT_SIZE];
81 uint8_t buf[OBJECT_SIZE];
82 int ret;
83 int handle;
84
85 fd = drm_open_any();
86
Daniel Vetter9dbce092012-03-24 19:30:29 +010087 handle = gem_create(fd, OBJECT_SIZE);
Eric Anholt895a4152009-03-26 18:47:42 -070088
89 printf("Testing contents of newly created object.\n");
90 ret = do_read(fd, handle, buf, 0, OBJECT_SIZE);
91 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");
96 ret = do_read(fd, handle, buf, OBJECT_SIZE / 2, OBJECT_SIZE);
Eric Anholt895a4152009-03-26 18:47:42 -070097 assert(ret == -1 && errno == EINVAL);
98
99 printf("Testing full write of buffer\n");
100 memset(buf, 0, sizeof(buf));
101 memset(buf + 1024, 0x01, 1024);
102 memset(expected + 1024, 0x01, 1024);
103 ret = do_write(fd, handle, buf, 0, OBJECT_SIZE);
104 assert(ret == 0);
105 ret = do_read(fd, handle, buf, 0, OBJECT_SIZE);
106 assert(ret == 0);
107 assert(memcmp(buf, expected, sizeof(buf)) == 0);
108
109 printf("Testing partial write of buffer\n");
110 memset(buf + 4096, 0x02, 1024);
111 memset(expected + 4096, 0x02, 1024);
112 ret = do_write(fd, handle, buf + 4096, 4096, 1024);
113 assert(ret == 0);
114 ret = do_read(fd, handle, buf, 0, OBJECT_SIZE);
115 assert(ret == 0);
116 assert(memcmp(buf, expected, sizeof(buf)) == 0);
117
118 printf("Testing partial read of buffer\n");
119 ret = do_read(fd, handle, buf, 512, 1024);
120 assert(ret == 0);
121 assert(memcmp(buf, expected + 512, 1024) == 0);
122
123 printf("Testing read of bad buffer handle\n");
124 ret = do_read(fd, 1234, buf, 0, 1024);
Chris Wilsonc935c602010-08-25 11:56:59 +0100125 assert(ret == -1 && errno == ENOENT);
Eric Anholt895a4152009-03-26 18:47:42 -0700126
127 printf("Testing write of bad buffer handle\n");
128 ret = do_write(fd, 1234, buf, 0, 1024);
Chris Wilsonc935c602010-08-25 11:56:59 +0100129 assert(ret == -1 && errno == ENOENT);
Eric Anholt895a4152009-03-26 18:47:42 -0700130
131 close(fd);
132
133 return 0;
134}