Chris Wilson | 07d59b3 | 2011-01-20 22:10:10 +0000 | [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 <stdint.h> |
| 31 | #include <stdio.h> |
| 32 | #include <string.h> |
| 33 | #include <assert.h> |
| 34 | #include <fcntl.h> |
| 35 | #include <inttypes.h> |
| 36 | #include <errno.h> |
| 37 | #include <sys/stat.h> |
| 38 | #include <sys/ioctl.h> |
| 39 | #include <sys/mman.h> |
| 40 | #include <sys/time.h> |
| 41 | #include "drm.h" |
| 42 | #include "i915_drm.h" |
| 43 | #include "drmtest.h" |
| 44 | |
| 45 | #define MI_BATCH_BUFFER_END (0xA<<23) |
| 46 | |
| 47 | static uint32_t gem_create(int fd, int size) |
| 48 | { |
| 49 | struct drm_i915_gem_create create; |
| 50 | |
| 51 | create.handle = 0; |
| 52 | create.size = size; |
| 53 | (void)drmIoctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create); |
| 54 | |
| 55 | return create.handle; |
| 56 | } |
| 57 | |
| 58 | static int gem_write(int fd, |
| 59 | uint32_t handle, uint32_t offset, |
| 60 | const void *src, int length) |
| 61 | { |
| 62 | struct drm_i915_gem_pwrite pwrite; |
| 63 | |
| 64 | pwrite.handle = handle; |
| 65 | pwrite.offset = offset; |
| 66 | pwrite.size = length; |
| 67 | pwrite.data_ptr = (uintptr_t)src; |
| 68 | return drmIoctl(fd, DRM_IOCTL_I915_GEM_PWRITE, &pwrite); |
| 69 | } |
| 70 | |
| 71 | static void gem_close(int fd, uint32_t handle) |
| 72 | { |
| 73 | struct drm_gem_close close; |
| 74 | |
| 75 | close.handle = handle; |
| 76 | (void)drmIoctl(fd, DRM_IOCTL_GEM_CLOSE, &close); |
| 77 | } |
| 78 | |
| 79 | static void gem_sync(int fd, uint32_t handle) |
| 80 | { |
| 81 | struct drm_i915_gem_set_domain set_domain; |
| 82 | |
| 83 | set_domain.handle = handle; |
| 84 | set_domain.read_domains = I915_GEM_DOMAIN_GTT; |
| 85 | set_domain.write_domain = I915_GEM_DOMAIN_GTT; |
| 86 | |
| 87 | drmIoctl(fd, DRM_IOCTL_I915_GEM_SET_DOMAIN, &set_domain); |
| 88 | } |
| 89 | |
| 90 | static double elapsed(const struct timeval *start, |
| 91 | const struct timeval *end, |
| 92 | int loop) |
| 93 | { |
| 94 | return (1e6*(end->tv_sec - start->tv_sec) + (end->tv_usec - start->tv_usec))/loop; |
| 95 | } |
| 96 | |
| 97 | static void exec(int fd, uint32_t handle, int loops) |
| 98 | { |
| 99 | struct drm_i915_gem_execbuffer2 execbuf; |
| 100 | struct drm_i915_gem_exec_object2 exec[1]; |
| 101 | int ret; |
| 102 | |
| 103 | exec[0].handle = handle; |
| 104 | exec[0].relocation_count = 0; |
| 105 | exec[0].relocs_ptr = 0; |
| 106 | exec[0].alignment = 0; |
| 107 | exec[0].offset = 0; |
| 108 | exec[0].flags = 0; |
| 109 | exec[0].rsvd1 = 0; |
| 110 | exec[0].rsvd2 = 0; |
| 111 | |
| 112 | execbuf.buffers_ptr = (uintptr_t)exec; |
| 113 | execbuf.buffer_count = 1; |
| 114 | execbuf.batch_start_offset = 0; |
| 115 | execbuf.batch_len = 8; |
| 116 | execbuf.cliprects_ptr = 0; |
| 117 | execbuf.num_cliprects = 0; |
| 118 | execbuf.DR1 = 0; |
| 119 | execbuf.DR4 = 0; |
| 120 | execbuf.flags = 0; |
| 121 | execbuf.rsvd1 = 0; |
| 122 | execbuf.rsvd2 = 0; |
| 123 | |
| 124 | while (loops--) { |
| 125 | ret = drmIoctl(fd, |
| 126 | DRM_IOCTL_I915_GEM_EXECBUFFER2, |
| 127 | &execbuf); |
| 128 | } |
| 129 | gem_sync(fd, handle); |
| 130 | } |
| 131 | |
| 132 | int main(int argc, char **argv) |
| 133 | { |
| 134 | uint32_t batch[2] = {MI_BATCH_BUFFER_END}; |
| 135 | uint32_t handle; |
| 136 | int count; |
| 137 | int fd; |
| 138 | |
| 139 | fd = drm_open_any(); |
| 140 | |
| 141 | handle = gem_create(fd, 4096); |
| 142 | gem_write(fd, handle, 0, batch, sizeof(batch)); |
| 143 | |
| 144 | for (count = 1; count <= 1<<17; count <<= 1) { |
| 145 | struct timeval start, end; |
| 146 | |
| 147 | gettimeofday(&start, NULL); |
| 148 | exec(fd, handle, count); |
| 149 | gettimeofday(&end, NULL); |
| 150 | printf("Time to exec x %d: %7.3fµs\n", |
| 151 | count, elapsed(&start, &end, count)); |
| 152 | fflush(stdout); |
| 153 | } |
| 154 | gem_close(fd, handle); |
| 155 | |
| 156 | close(fd); |
| 157 | |
| 158 | return 0; |
| 159 | } |