Chris Wilson | 9de433a | 2011-06-20 13:53:12 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2011 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 | * Chris Wilson <chris@chris-wilson.co.uk> |
| 25 | * |
| 26 | */ |
| 27 | |
| 28 | #include <unistd.h> |
| 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> |
| 37 | #include <sys/ioctl.h> |
| 38 | #include <sys/mman.h> |
| 39 | #include "drm.h" |
| 40 | #include "i915_drm.h" |
| 41 | #include "drmtest.h" |
| 42 | |
| 43 | #define OBJECT_SIZE (16*1024*1024) |
| 44 | |
Daniel Vetter | 733a1d9 | 2011-09-15 18:47:21 +0200 | [diff] [blame] | 45 | static void set_domain(int fd, uint32_t handle) |
| 46 | { |
Daniel Vetter | 673e6b2 | 2012-01-10 16:05:34 +0100 | [diff] [blame] | 47 | gem_set_domain(fd, handle, I915_GEM_DOMAIN_GTT, I915_GEM_DOMAIN_GTT); |
Daniel Vetter | 733a1d9 | 2011-09-15 18:47:21 +0200 | [diff] [blame] | 48 | } |
| 49 | |
Chris Wilson | 9de433a | 2011-06-20 13:53:12 +0100 | [diff] [blame] | 50 | static void * |
Daniel Vetter | 733a1d9 | 2011-09-15 18:47:21 +0200 | [diff] [blame] | 51 | mmap_bo(int fd, uint32_t handle) |
Chris Wilson | 9de433a | 2011-06-20 13:53:12 +0100 | [diff] [blame] | 52 | { |
Chris Wilson | 9de433a | 2011-06-20 13:53:12 +0100 | [diff] [blame] | 53 | void *ptr; |
Chris Wilson | 9de433a | 2011-06-20 13:53:12 +0100 | [diff] [blame] | 54 | |
Daniel Vetter | 527cad1 | 2012-01-10 18:41:46 +0100 | [diff] [blame] | 55 | ptr = gem_mmap(fd, handle, OBJECT_SIZE, PROT_READ | PROT_WRITE); |
Chris Wilson | 9de433a | 2011-06-20 13:53:12 +0100 | [diff] [blame] | 56 | assert(ptr != MAP_FAILED); |
| 57 | |
Daniel Vetter | 733a1d9 | 2011-09-15 18:47:21 +0200 | [diff] [blame] | 58 | return ptr; |
| 59 | } |
| 60 | |
| 61 | static void * |
| 62 | create_pointer(int fd) |
| 63 | { |
| 64 | uint32_t handle; |
| 65 | void *ptr; |
| 66 | |
| 67 | handle = gem_create(fd, OBJECT_SIZE); |
| 68 | |
| 69 | ptr = mmap_bo(fd, handle); |
| 70 | |
Chris Wilson | 9de433a | 2011-06-20 13:53:12 +0100 | [diff] [blame] | 71 | gem_close(fd, handle); |
| 72 | |
| 73 | return ptr; |
| 74 | } |
| 75 | |
| 76 | static void |
| 77 | test_copy(int fd) |
| 78 | { |
| 79 | void *src, *dst; |
| 80 | |
| 81 | /* copy from a fresh src to fresh dst to force pagefault on both */ |
| 82 | src = create_pointer(fd); |
| 83 | dst = create_pointer(fd); |
| 84 | |
| 85 | memcpy(dst, src, OBJECT_SIZE); |
| 86 | memcpy(src, dst, OBJECT_SIZE); |
| 87 | |
| 88 | munmap(dst, OBJECT_SIZE); |
| 89 | munmap(src, OBJECT_SIZE); |
| 90 | } |
| 91 | |
| 92 | static void |
| 93 | test_write(int fd) |
| 94 | { |
| 95 | void *src; |
| 96 | uint32_t dst; |
| 97 | |
| 98 | /* copy from a fresh src to fresh dst to force pagefault on both */ |
| 99 | src = create_pointer(fd); |
| 100 | dst = gem_create(fd, OBJECT_SIZE); |
| 101 | |
| 102 | gem_write(fd, dst, 0, src, OBJECT_SIZE); |
| 103 | |
| 104 | gem_close(fd, dst); |
| 105 | munmap(src, OBJECT_SIZE); |
| 106 | } |
| 107 | |
| 108 | static void |
Daniel Vetter | 733a1d9 | 2011-09-15 18:47:21 +0200 | [diff] [blame] | 109 | test_write_gtt(int fd) |
| 110 | { |
| 111 | uint32_t dst; |
| 112 | char *dst_gtt; |
| 113 | void *src; |
| 114 | |
| 115 | dst = gem_create(fd, OBJECT_SIZE); |
| 116 | |
| 117 | /* prefault object into gtt */ |
| 118 | dst_gtt = mmap_bo(fd, dst); |
| 119 | set_domain(fd, dst); |
| 120 | memset(dst_gtt, 0, OBJECT_SIZE); |
| 121 | munmap(dst_gtt, OBJECT_SIZE); |
| 122 | |
| 123 | src = create_pointer(fd); |
| 124 | |
| 125 | gem_write(fd, dst, 0, src, OBJECT_SIZE); |
| 126 | |
| 127 | gem_close(fd, dst); |
| 128 | munmap(src, OBJECT_SIZE); |
| 129 | } |
| 130 | |
| 131 | static void |
Chris Wilson | 9de433a | 2011-06-20 13:53:12 +0100 | [diff] [blame] | 132 | test_read(int fd) |
| 133 | { |
| 134 | void *dst; |
| 135 | uint32_t src; |
| 136 | |
| 137 | /* copy from a fresh src to fresh dst to force pagefault on both */ |
| 138 | dst = create_pointer(fd); |
| 139 | src = gem_create(fd, OBJECT_SIZE); |
| 140 | |
| 141 | gem_read(fd, src, 0, dst, OBJECT_SIZE); |
| 142 | |
| 143 | gem_close(fd, src); |
| 144 | munmap(dst, OBJECT_SIZE); |
| 145 | } |
| 146 | |
| 147 | int main(int argc, char **argv) |
| 148 | { |
| 149 | int fd; |
| 150 | |
Daniel Vetter | af228d4 | 2012-11-28 12:40:23 +0100 | [diff] [blame] | 151 | drmtest_subtest_init(argc, argv); |
| 152 | |
Chris Wilson | 9de433a | 2011-06-20 13:53:12 +0100 | [diff] [blame] | 153 | fd = drm_open_any(); |
| 154 | |
Daniel Vetter | af228d4 | 2012-11-28 12:40:23 +0100 | [diff] [blame] | 155 | if (drmtest_run_subtest("copy")) |
| 156 | test_copy(fd); |
| 157 | if (drmtest_run_subtest("read")) |
| 158 | test_read(fd); |
| 159 | if (drmtest_run_subtest("write")) |
| 160 | test_write(fd); |
| 161 | if (drmtest_run_subtest("write-gtt")) |
| 162 | test_write_gtt(fd); |
Chris Wilson | 9de433a | 2011-06-20 13:53:12 +0100 | [diff] [blame] | 163 | |
| 164 | close(fd); |
| 165 | |
| 166 | return 0; |
| 167 | } |