Eric Anholt | 2287786 | 2008-04-23 14:52:30 -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 | |
| 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 Anholt | 3148c16 | 2008-04-29 13:45:43 -0700 | [diff] [blame] | 36 | #include "drm.h" |
Eric Anholt | 2655005 | 2008-06-11 14:42:40 -0700 | [diff] [blame] | 37 | #include "i915_drm.h" |
Eric Anholt | 2287786 | 2008-04-23 14:52:30 -0700 | [diff] [blame] | 38 | |
Eric Anholt | 3148c16 | 2008-04-29 13:45:43 -0700 | [diff] [blame] | 39 | #define OBJECT_SIZE 16384 |
Eric Anholt | 2287786 | 2008-04-23 14:52:30 -0700 | [diff] [blame] | 40 | |
| 41 | int do_read(int fd, int handle, void *buf, int offset, int size) |
| 42 | { |
Eric Anholt | 2655005 | 2008-06-11 14:42:40 -0700 | [diff] [blame] | 43 | struct drm_i915_gem_pread read; |
Eric Anholt | 2287786 | 2008-04-23 14:52:30 -0700 | [diff] [blame] | 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; |
Eric Anholt | effc6d9 | 2008-05-07 16:00:58 -0700 | [diff] [blame] | 52 | read.data_ptr = (uintptr_t)buf; |
Eric Anholt | 2287786 | 2008-04-23 14:52:30 -0700 | [diff] [blame] | 53 | read.size = size; |
| 54 | read.offset = offset; |
| 55 | |
Eric Anholt | 2655005 | 2008-06-11 14:42:40 -0700 | [diff] [blame] | 56 | return ioctl(fd, DRM_IOCTL_I915_GEM_PREAD, &read); |
Eric Anholt | 2287786 | 2008-04-23 14:52:30 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | int do_write(int fd, int handle, void *buf, int offset, int size) |
| 60 | { |
Eric Anholt | 2655005 | 2008-06-11 14:42:40 -0700 | [diff] [blame] | 61 | struct drm_i915_gem_pwrite write; |
Eric Anholt | 2287786 | 2008-04-23 14:52:30 -0700 | [diff] [blame] | 62 | |
| 63 | memset(&write, 0, sizeof(write)); |
| 64 | write.handle = handle; |
Eric Anholt | effc6d9 | 2008-05-07 16:00:58 -0700 | [diff] [blame] | 65 | write.data_ptr = (uintptr_t)buf; |
Eric Anholt | 2287786 | 2008-04-23 14:52:30 -0700 | [diff] [blame] | 66 | write.size = size; |
| 67 | write.offset = offset; |
| 68 | |
Eric Anholt | 2655005 | 2008-06-11 14:42:40 -0700 | [diff] [blame] | 69 | return ioctl(fd, DRM_IOCTL_I915_GEM_PWRITE, &write); |
Eric Anholt | 2287786 | 2008-04-23 14:52:30 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | int main(int argc, char **argv) |
| 73 | { |
| 74 | int fd; |
Eric Anholt | 2655005 | 2008-06-11 14:42:40 -0700 | [diff] [blame] | 75 | struct drm_i915_gem_create create; |
| 76 | struct drm_i915_gem_mmap mmap; |
Eric Anholt | 6e46a3c | 2008-05-12 15:42:20 -0700 | [diff] [blame] | 77 | struct drm_gem_close unref; |
Eric Anholt | 3148c16 | 2008-04-29 13:45:43 -0700 | [diff] [blame] | 78 | uint8_t expected[OBJECT_SIZE]; |
| 79 | uint8_t buf[OBJECT_SIZE]; |
Eric Anholt | effc6d9 | 2008-05-07 16:00:58 -0700 | [diff] [blame] | 80 | uint8_t *addr; |
Eric Anholt | 2287786 | 2008-04-23 14:52:30 -0700 | [diff] [blame] | 81 | int ret; |
| 82 | int handle; |
| 83 | |
Kristian Høgsberg | e9d6116 | 2009-04-06 17:13:01 -0400 | [diff] [blame] | 84 | fd = drm_open_matching("8086:*", 0); |
| 85 | if (fd < 0) { |
| 86 | fprintf(stderr, "failed to open intel drm device, skipping\n"); |
| 87 | return 0; |
| 88 | } |
Eric Anholt | 2287786 | 2008-04-23 14:52:30 -0700 | [diff] [blame] | 89 | |
| 90 | memset(&mmap, 0, sizeof(mmap)); |
| 91 | mmap.handle = 0x10101010; |
| 92 | mmap.offset = 0; |
| 93 | mmap.size = 4096; |
| 94 | printf("Testing mmaping of bad object.\n"); |
Eric Anholt | 2655005 | 2008-06-11 14:42:40 -0700 | [diff] [blame] | 95 | ret = ioctl(fd, DRM_IOCTL_I915_GEM_MMAP, &mmap); |
Eric Anholt | 1bdf35f | 2008-07-28 11:24:00 -0700 | [diff] [blame] | 96 | assert(ret == -1 && errno == EBADF); |
Eric Anholt | 2287786 | 2008-04-23 14:52:30 -0700 | [diff] [blame] | 97 | |
Eric Anholt | 6e46a3c | 2008-05-12 15:42:20 -0700 | [diff] [blame] | 98 | memset(&create, 0, sizeof(create)); |
| 99 | create.size = OBJECT_SIZE; |
Eric Anholt | 2655005 | 2008-06-11 14:42:40 -0700 | [diff] [blame] | 100 | ret = ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create); |
Eric Anholt | 2287786 | 2008-04-23 14:52:30 -0700 | [diff] [blame] | 101 | assert(ret == 0); |
Eric Anholt | 6e46a3c | 2008-05-12 15:42:20 -0700 | [diff] [blame] | 102 | handle = create.handle; |
Eric Anholt | 2287786 | 2008-04-23 14:52:30 -0700 | [diff] [blame] | 103 | |
Eric Anholt | 6e46a3c | 2008-05-12 15:42:20 -0700 | [diff] [blame] | 104 | printf("Testing mmaping of newly created object.\n"); |
Eric Anholt | 2287786 | 2008-04-23 14:52:30 -0700 | [diff] [blame] | 105 | mmap.handle = handle; |
| 106 | mmap.offset = 0; |
Eric Anholt | 3148c16 | 2008-04-29 13:45:43 -0700 | [diff] [blame] | 107 | mmap.size = OBJECT_SIZE; |
Eric Anholt | 2655005 | 2008-06-11 14:42:40 -0700 | [diff] [blame] | 108 | ret = ioctl(fd, DRM_IOCTL_I915_GEM_MMAP, &mmap); |
Eric Anholt | 2287786 | 2008-04-23 14:52:30 -0700 | [diff] [blame] | 109 | assert(ret == 0); |
Eric Anholt | effc6d9 | 2008-05-07 16:00:58 -0700 | [diff] [blame] | 110 | addr = (uint8_t *)(uintptr_t)mmap.addr_ptr; |
Eric Anholt | 2287786 | 2008-04-23 14:52:30 -0700 | [diff] [blame] | 111 | |
Eric Anholt | 6e46a3c | 2008-05-12 15:42:20 -0700 | [diff] [blame] | 112 | printf("Testing contents of newly created object.\n"); |
Eric Anholt | 2287786 | 2008-04-23 14:52:30 -0700 | [diff] [blame] | 113 | memset(expected, 0, sizeof(expected)); |
Eric Anholt | effc6d9 | 2008-05-07 16:00:58 -0700 | [diff] [blame] | 114 | assert(memcmp(addr, expected, sizeof(expected)) == 0); |
Eric Anholt | 2287786 | 2008-04-23 14:52:30 -0700 | [diff] [blame] | 115 | |
| 116 | printf("Testing coherency of writes and mmap reads.\n"); |
| 117 | memset(buf, 0, sizeof(buf)); |
| 118 | memset(buf + 1024, 0x01, 1024); |
| 119 | memset(expected + 1024, 0x01, 1024); |
Eric Anholt | 3148c16 | 2008-04-29 13:45:43 -0700 | [diff] [blame] | 120 | ret = do_write(fd, handle, buf, 0, OBJECT_SIZE); |
Eric Anholt | 2287786 | 2008-04-23 14:52:30 -0700 | [diff] [blame] | 121 | assert(ret == 0); |
Eric Anholt | effc6d9 | 2008-05-07 16:00:58 -0700 | [diff] [blame] | 122 | assert(memcmp(buf, addr, sizeof(buf)) == 0); |
Eric Anholt | 2287786 | 2008-04-23 14:52:30 -0700 | [diff] [blame] | 123 | |
Eric Anholt | 6e46a3c | 2008-05-12 15:42:20 -0700 | [diff] [blame] | 124 | printf("Testing that mapping stays after close\n"); |
Eric Anholt | 2287786 | 2008-04-23 14:52:30 -0700 | [diff] [blame] | 125 | unref.handle = handle; |
Eric Anholt | 6e46a3c | 2008-05-12 15:42:20 -0700 | [diff] [blame] | 126 | ret = ioctl(fd, DRM_IOCTL_GEM_CLOSE, &unref); |
Eric Anholt | 2287786 | 2008-04-23 14:52:30 -0700 | [diff] [blame] | 127 | assert(ret == 0); |
Eric Anholt | effc6d9 | 2008-05-07 16:00:58 -0700 | [diff] [blame] | 128 | assert(memcmp(buf, addr, sizeof(buf)) == 0); |
Eric Anholt | 2287786 | 2008-04-23 14:52:30 -0700 | [diff] [blame] | 129 | |
| 130 | printf("Testing unmapping\n"); |
Eric Anholt | effc6d9 | 2008-05-07 16:00:58 -0700 | [diff] [blame] | 131 | munmap(addr, OBJECT_SIZE); |
Eric Anholt | 2287786 | 2008-04-23 14:52:30 -0700 | [diff] [blame] | 132 | |
| 133 | close(fd); |
| 134 | |
| 135 | return 0; |
| 136 | } |