blob: 7fd22bd01f3070bdf6488f6fb8b41e89462a7ed2 [file] [log] [blame]
Chris Wilsonf62abaf2014-01-10 13:42:55 +00001/*
2 * Copyright © 2014 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 <fcntl.h>
34#include <inttypes.h>
35#include <errno.h>
36#include <sys/stat.h>
37#include <sys/ioctl.h>
38#include <sys/time.h>
39#include <sys/wait.h>
40
41#include "drm.h"
Daniel Vettere49ceb82014-03-22 21:07:37 +010042#include "ioctl_wrappers.h"
Chris Wilsonf62abaf2014-01-10 13:42:55 +000043#include "drmtest.h"
Daniel Vettere49ceb82014-03-22 21:07:37 +010044#include "intel_chipset.h"
Chris Wilsonf62abaf2014-01-10 13:42:55 +000045
Chris Wilsonf62abaf2014-01-10 13:42:55 +000046static const uint32_t canary = 0xdeadbeef;
47
Rodrigo Vivicde058a2014-03-18 11:18:56 -030048typedef struct data {
49 int fd;
50 int devid;
51 int intel_gen;
52} data_t;
53
Chris Wilsonf62abaf2014-01-10 13:42:55 +000054static double elapsed(const struct timeval *start,
55 const struct timeval *end)
56{
57 return 1e6*(end->tv_sec - start->tv_sec) + (end->tv_usec - start->tv_usec);
58}
59
Rodrigo Vivicde058a2014-03-18 11:18:56 -030060static void busy(data_t *data, uint32_t handle, int size, int loops)
Chris Wilsonf62abaf2014-01-10 13:42:55 +000061{
62 struct drm_i915_gem_relocation_entry reloc[20];
63 struct drm_i915_gem_exec_object2 gem_exec[2];
64 struct drm_i915_gem_execbuffer2 execbuf;
65 struct drm_i915_gem_pwrite gem_pwrite;
66 struct drm_i915_gem_create create;
Rodrigo Vivicde058a2014-03-18 11:18:56 -030067 uint32_t buf[170], *b;
Chris Wilsonf62abaf2014-01-10 13:42:55 +000068 int i;
69
70 memset(reloc, 0, sizeof(reloc));
71 memset(gem_exec, 0, sizeof(gem_exec));
72 memset(&execbuf, 0, sizeof(execbuf));
73
74 b = buf;
75 for (i = 0; i < 20; i++) {
Rodrigo Vivicde058a2014-03-18 11:18:56 -030076 *b++ = XY_COLOR_BLT_CMD_NOLEN |
77 ((data->intel_gen >= 8) ? 5 : 4) |
Rodrigo Vivid8164352014-03-18 11:18:55 -030078 COLOR_BLT_WRITE_ALPHA | XY_COLOR_BLT_WRITE_RGB;
Chris Wilsonf62abaf2014-01-10 13:42:55 +000079 *b++ = 0xf0 << 16 | 1 << 25 | 1 << 24 | 4096;
Rodrigo Vivid8164352014-03-18 11:18:55 -030080 *b++ = 0;
81 *b++ = size >> 12 << 16 | 1024;
Chris Wilsonf62abaf2014-01-10 13:42:55 +000082 reloc[i].offset = (b - buf) * sizeof(uint32_t);
83 reloc[i].target_handle = handle;
84 reloc[i].read_domains = I915_GEM_DOMAIN_RENDER;
85 reloc[i].write_domain = I915_GEM_DOMAIN_RENDER;
86 *b++ = 0;
Rodrigo Vivicde058a2014-03-18 11:18:56 -030087 if (data->intel_gen >= 8)
88 *b++ = 0;
Chris Wilsonf62abaf2014-01-10 13:42:55 +000089 *b++ = canary;
90 }
91 *b++ = MI_BATCH_BUFFER_END;
Rodrigo Vivicde058a2014-03-18 11:18:56 -030092 if ((b - buf) & 1)
93 *b++ = 0;
Chris Wilsonf62abaf2014-01-10 13:42:55 +000094
95 gem_exec[0].handle = handle;
96 gem_exec[0].flags = EXEC_OBJECT_NEEDS_FENCE;
97
98 create.handle = 0;
99 create.size = 4096;
Rodrigo Vivicde058a2014-03-18 11:18:56 -0300100 drmIoctl(data->fd, DRM_IOCTL_I915_GEM_CREATE, &create);
Chris Wilsonf62abaf2014-01-10 13:42:55 +0000101 gem_exec[1].handle = create.handle;
102 gem_exec[1].relocation_count = 20;
103 gem_exec[1].relocs_ptr = (uintptr_t)reloc;
104
105 execbuf.buffers_ptr = (uintptr_t)gem_exec;
106 execbuf.buffer_count = 2;
Rodrigo Vivicde058a2014-03-18 11:18:56 -0300107 execbuf.batch_len = (b - buf) * sizeof(buf[0]);
Chris Wilsonf62abaf2014-01-10 13:42:55 +0000108 execbuf.flags = 1 << 11;
Rodrigo Vivicde058a2014-03-18 11:18:56 -0300109 if (HAS_BLT_RING(data->devid))
Chris Wilsonf62abaf2014-01-10 13:42:55 +0000110 execbuf.flags |= I915_EXEC_BLT;
111
112 gem_pwrite.handle = gem_exec[1].handle;
113 gem_pwrite.offset = 0;
Rodrigo Vivicde058a2014-03-18 11:18:56 -0300114 gem_pwrite.size = execbuf.batch_len;
Chris Wilsonf62abaf2014-01-10 13:42:55 +0000115 gem_pwrite.data_ptr = (uintptr_t)buf;
Rodrigo Vivicde058a2014-03-18 11:18:56 -0300116 if (drmIoctl(data->fd, DRM_IOCTL_I915_GEM_PWRITE, &gem_pwrite) == 0) {
Chris Wilsonf62abaf2014-01-10 13:42:55 +0000117 while (loops--)
Rodrigo Vivicde058a2014-03-18 11:18:56 -0300118 drmIoctl(data->fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &execbuf);
Chris Wilsonf62abaf2014-01-10 13:42:55 +0000119 }
120
Rodrigo Vivicde058a2014-03-18 11:18:56 -0300121 drmIoctl(data->fd, DRM_IOCTL_GEM_CLOSE, &create.handle);
Chris Wilsonf62abaf2014-01-10 13:42:55 +0000122}
123
Rodrigo Vivicde058a2014-03-18 11:18:56 -0300124static void run(data_t *data, int child)
Chris Wilsonf62abaf2014-01-10 13:42:55 +0000125{
126 const int size = 4096 * (256 + child * child);
127 const int tiling = child % 2;
128 const int write = child % 2;
Rodrigo Vivicde058a2014-03-18 11:18:56 -0300129 uint32_t handle = gem_create(data->fd, size);
Chris Wilsonf62abaf2014-01-10 13:42:55 +0000130 uint32_t *ptr;
131 uint32_t x;
132
133 igt_assert(handle);
134
135 if (tiling != I915_TILING_NONE)
Rodrigo Vivicde058a2014-03-18 11:18:56 -0300136 gem_set_tiling(data->fd, handle, tiling, 4096);
Chris Wilsonf62abaf2014-01-10 13:42:55 +0000137
138 /* load up the unfaulted bo */
Rodrigo Vivicde058a2014-03-18 11:18:56 -0300139 busy(data, handle, size, 100);
Chris Wilsonf62abaf2014-01-10 13:42:55 +0000140
141 /* Note that we ignore the API and rely on the implict
142 * set-to-gtt-domain within the fault handler.
143 */
144 if (write) {
Rodrigo Vivicde058a2014-03-18 11:18:56 -0300145 ptr = gem_mmap(data->fd, handle, size, PROT_READ | PROT_WRITE);
Chris Wilsonf62abaf2014-01-10 13:42:55 +0000146 ptr[rand() % (size / 4)] = canary;
147 } else
Rodrigo Vivicde058a2014-03-18 11:18:56 -0300148 ptr = gem_mmap(data->fd, handle, size, PROT_READ);
Chris Wilsonf62abaf2014-01-10 13:42:55 +0000149 x = ptr[rand() % (size / 4)];
150 munmap(ptr, size);
151
152 igt_assert(x == canary);
Chris Wilson5e278c82014-01-15 10:40:40 +0000153 exit(0);
Chris Wilsonf62abaf2014-01-10 13:42:55 +0000154}
155
Daniel Vetter8fa20662014-01-15 23:59:52 +0100156igt_simple_main
Chris Wilsonf62abaf2014-01-10 13:42:55 +0000157{
158 struct timeval start, end;
159 pid_t children[64];
Rodrigo Vivicde058a2014-03-18 11:18:56 -0300160 data_t data = {};
Chris Wilsonf62abaf2014-01-10 13:42:55 +0000161 int n;
162
Daniel Vetter58633cf2014-02-12 00:05:57 +0100163 /* check for an intel gpu before goint nuts. */
164 int fd = drm_open_any();
165 close(fd);
166
Chris Wilsonf62abaf2014-01-10 13:42:55 +0000167 igt_skip_on_simulation();
168
Rodrigo Vivicde058a2014-03-18 11:18:56 -0300169 data.fd = drm_open_any();
170 data.devid = intel_get_drm_devid(data.fd);
171 data.intel_gen = intel_gen(data.devid);
172
Chris Wilsonf62abaf2014-01-10 13:42:55 +0000173 gettimeofday(&start, NULL);
174 for (n = 0; n < ARRAY_SIZE(children); n++) {
175 switch ((children[n] = fork())) {
176 case -1: igt_assert(0);
Rodrigo Vivicde058a2014-03-18 11:18:56 -0300177 case 0: run(&data, n); break;
Chris Wilsonf62abaf2014-01-10 13:42:55 +0000178 default: break;
179 }
180 }
181
182 for (n = 0; n < ARRAY_SIZE(children); n++) {
183 int status = -1;
184 while (waitpid(children[n], &status, 0) == -1 &&
185 errno == -EINTR)
186 ;
187 igt_assert(status == 0);
188 }
189 gettimeofday(&end, NULL);
Chris Wilson84af2b92014-01-10 16:09:57 +0000190 printf("Time to execute %lu children: %7.3fms\n",
191 ARRAY_SIZE(children), elapsed(&start, &end) / 1000);
Chris Wilsonf62abaf2014-01-10 13:42:55 +0000192}