Eric Anholt | 895a415 | 2009-03-26 18:47:42 -0700 | [diff] [blame] | 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 | |
Eric Anholt | 0581738 | 2009-03-27 16:04:25 -0700 | [diff] [blame] | 28 | #include <unistd.h> |
Eric Anholt | 895a415 | 2009-03-26 18:47:42 -0700 | [diff] [blame] | 29 | #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 Anholt | 0581738 | 2009-03-27 16:04:25 -0700 | [diff] [blame] | 37 | #include <sys/ioctl.h> |
Eric Anholt | 895a415 | 2009-03-26 18:47:42 -0700 | [diff] [blame] | 38 | #include "drm.h" |
| 39 | #include "i915_drm.h" |
Eric Anholt | 0581738 | 2009-03-27 16:04:25 -0700 | [diff] [blame] | 40 | #include "drmtest.h" |
Eric Anholt | 895a415 | 2009-03-26 18:47:42 -0700 | [diff] [blame] | 41 | |
| 42 | #define OBJECT_SIZE 16384 |
| 43 | |
Eric Anholt | 0581738 | 2009-03-27 16:04:25 -0700 | [diff] [blame] | 44 | static int |
| 45 | do_read(int fd, int handle, void *buf, int offset, int size) |
Eric Anholt | 895a415 | 2009-03-26 18:47:42 -0700 | [diff] [blame] | 46 | { |
Imre Deak | b614b4d | 2012-10-10 16:04:41 +0300 | [diff] [blame] | 47 | struct drm_i915_gem_pread gem_pread; |
Eric Anholt | 895a415 | 2009-03-26 18:47:42 -0700 | [diff] [blame] | 48 | |
| 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 Deak | b614b4d | 2012-10-10 16:04:41 +0300 | [diff] [blame] | 54 | 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 Anholt | 895a415 | 2009-03-26 18:47:42 -0700 | [diff] [blame] | 59 | |
Imre Deak | b614b4d | 2012-10-10 16:04:41 +0300 | [diff] [blame] | 60 | return ioctl(fd, DRM_IOCTL_I915_GEM_PREAD, &gem_pread); |
Eric Anholt | 895a415 | 2009-03-26 18:47:42 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Eric Anholt | 0581738 | 2009-03-27 16:04:25 -0700 | [diff] [blame] | 63 | static int |
| 64 | do_write(int fd, int handle, void *buf, int offset, int size) |
Eric Anholt | 895a415 | 2009-03-26 18:47:42 -0700 | [diff] [blame] | 65 | { |
Imre Deak | b614b4d | 2012-10-10 16:04:41 +0300 | [diff] [blame] | 66 | struct drm_i915_gem_pwrite gem_pwrite; |
Eric Anholt | 895a415 | 2009-03-26 18:47:42 -0700 | [diff] [blame] | 67 | |
Imre Deak | b614b4d | 2012-10-10 16:04:41 +0300 | [diff] [blame] | 68 | 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 Anholt | 895a415 | 2009-03-26 18:47:42 -0700 | [diff] [blame] | 73 | |
Imre Deak | b614b4d | 2012-10-10 16:04:41 +0300 | [diff] [blame] | 74 | return ioctl(fd, DRM_IOCTL_I915_GEM_PWRITE, &gem_pwrite); |
Eric Anholt | 895a415 | 2009-03-26 18:47:42 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | int main(int argc, char **argv) |
| 78 | { |
| 79 | int fd; |
Eric Anholt | 895a415 | 2009-03-26 18:47:42 -0700 | [diff] [blame] | 80 | 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 Vetter | 9dbce09 | 2012-03-24 19:30:29 +0100 | [diff] [blame] | 87 | handle = gem_create(fd, OBJECT_SIZE); |
Eric Anholt | 895a415 | 2009-03-26 18:47:42 -0700 | [diff] [blame] | 88 | |
| 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 Anholt | 895a415 | 2009-03-26 18:47:42 -0700 | [diff] [blame] | 97 | 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 Wilson | c935c60 | 2010-08-25 11:56:59 +0100 | [diff] [blame] | 125 | assert(ret == -1 && errno == ENOENT); |
Eric Anholt | 895a415 | 2009-03-26 18:47:42 -0700 | [diff] [blame] | 126 | |
| 127 | printf("Testing write of bad buffer handle\n"); |
| 128 | ret = do_write(fd, 1234, buf, 0, 1024); |
Chris Wilson | c935c60 | 2010-08-25 11:56:59 +0100 | [diff] [blame] | 129 | assert(ret == -1 && errno == ENOENT); |
Eric Anholt | 895a415 | 2009-03-26 18:47:42 -0700 | [diff] [blame] | 130 | |
| 131 | close(fd); |
| 132 | |
| 133 | return 0; |
| 134 | } |