kms_rotation_crc: Remove useless comments
[platform/upstream/intel-gpu-tools.git] / tests / gem_readwrite.c
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 <unistd.h>
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <fcntl.h>
33 #include <inttypes.h>
34 #include <errno.h>
35 #include <sys/stat.h>
36 #include <sys/ioctl.h>
37 #include "drm.h"
38 #include "ioctl_wrappers.h"
39 #include "drmtest.h"
40
41 #define OBJECT_SIZE 16384
42
43 static int
44 do_read(int fd, int handle, void *buf, int offset, int size)
45 {
46         struct drm_i915_gem_pread gem_pread;
47
48         /* Ensure that we don't have any convenient data in buf in case
49          * we fail.
50          */
51         memset(buf, 0xd0, size);
52
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;
58
59         return ioctl(fd, DRM_IOCTL_I915_GEM_PREAD, &gem_pread);
60 }
61
62 static int
63 do_write(int fd, int handle, void *buf, int offset, int size)
64 {
65         struct drm_i915_gem_pwrite gem_pwrite;
66
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;
72
73         return ioctl(fd, DRM_IOCTL_I915_GEM_PWRITE, &gem_pwrite);
74 }
75
76 int fd;
77 uint32_t handle;
78
79 igt_main
80 {
81         uint8_t expected[OBJECT_SIZE];
82         uint8_t buf[OBJECT_SIZE];
83         int ret;
84
85         igt_skip_on_simulation();
86
87         igt_fixture {
88                 fd = drm_open_any();
89
90                 handle = gem_create(fd, OBJECT_SIZE);
91         }
92
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);
96                 igt_assert(ret == 0);
97                 memset(&expected, 0, sizeof(expected));
98                 igt_assert(memcmp(expected, buf, sizeof(expected)) == 0);
99         }
100
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);
105         }
106
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);
117
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);
126
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);
131         }
132
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);
137         }
138
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);
143         }
144
145         igt_fixture
146                 close(fd);
147 }