summaryrefslogtreecommitdiff
path: root/tests/gem_exec_nop.c
blob: c8fff2be11dd07d3ace154d0fefba502f968bf02 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*
 * Copyright © 2011 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *
 * Authors:
 *    Chris Wilson <chris@chris-wilson.co.uk>
 *
 */

#include <unistd.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <inttypes.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <time.h>
#include "drm.h"
#include "ioctl_wrappers.h"
#include "drmtest.h"
#include "intel_io.h"
#include "igt_stats.h"

#define LOCAL_I915_EXEC_NO_RELOC (1<<11)
#define LOCAL_I915_EXEC_HANDLE_LUT (1<<12)

#define LOCAL_I915_EXEC_VEBOX (4<<0)

const uint32_t batch[2] = {MI_BATCH_BUFFER_END};
int device;

static int sysfs_read(const char *name)
{
	char buf[4096];
	struct stat st;
	int sysfd;
	int len;

	if (fstat(device, &st))
		return -1;

	sprintf(buf, "/sys/class/drm/card%d/%s",
		(int)(st.st_rdev & 0x7f), name);
	sysfd = open(buf, O_RDONLY);
	if (sysfd < 0)
		return -1;

	len = read(sysfd, buf, sizeof(buf)-1);
	close(sysfd);
	if (len < 0)
		return -1;

	buf[len] = '\0';
	return atoi(buf);
}

static int sysfs_write(const char *name, int value)
{
	char buf[4096];
	struct stat st;
	int sysfd;
	int len;

	if (fstat(device, &st))
		return -1;

	sprintf(buf, "/sys/class/drm/card%d/%s",
		(int)(st.st_rdev & 0x7f), name);
	sysfd = open(buf, O_WRONLY);
	if (sysfd < 0)
		return -1;

	len = sprintf(buf, "%d", value);
	len = write(sysfd, buf, len);
	close(sysfd);

	if (len < 0)
		return len;

	return 0;
}

static uint64_t elapsed(const struct timespec *start,
		const struct timespec *end,
		int loop)
{
	return (1000000000ULL*(end->tv_sec - start->tv_sec) + (end->tv_nsec - start->tv_nsec))/loop;
}

static void loop(int fd, uint32_t handle, unsigned ring_id, const char *ring_name)
{
	struct drm_i915_gem_execbuffer2 execbuf;
	struct drm_i915_gem_exec_object2 gem_exec[1];
	int count;

	gem_require_ring(fd, ring_id);
	igt_debug("RPS frequency range [%d, %d]\n",
		  sysfs_read("gt_min_freq_mhz"),
		  sysfs_read("gt_max_freq_mhz"));

	memset(&gem_exec, 0, sizeof(gem_exec));
	gem_exec[0].handle = handle;

	memset(&execbuf, 0, sizeof(execbuf));
	execbuf.buffers_ptr = (uintptr_t)gem_exec;
	execbuf.buffer_count = 1;
	execbuf.flags = ring_id;
	execbuf.flags |= LOCAL_I915_EXEC_HANDLE_LUT;
	execbuf.flags |= LOCAL_I915_EXEC_NO_RELOC;
	if (drmIoctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &execbuf)) {
		execbuf.flags = ring_id;
		do_ioctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &execbuf);
	}
	gem_sync(fd, handle);

	for (count = 1; count <= SLOW_QUICK(1<<17, 1<<4); count <<= 1) {
		const int reps = 13;
		igt_stats_t stats;
		int n;

		igt_stats_init_with_size(&stats, reps);

		for (n = 0; n < reps; n++) {
			struct timespec start, end;
			int loops = count;
			sleep(1); /* wait for the hw to go back to sleep */
			clock_gettime(CLOCK_MONOTONIC, &start);
			while (loops--)
				do_ioctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &execbuf);
			gem_sync(fd, handle);
			clock_gettime(CLOCK_MONOTONIC, &end);
			igt_stats_push(&stats, elapsed(&start, &end, count));
		}

		igt_info("Time to exec x %d:		%7.3fµs (ring=%s)\n",
			 count, igt_stats_get_trimean(&stats)/1000, ring_name);
		fflush(stdout);

		igt_stats_fini(&stats);
	}
}

static void set_auto_freq(void)
{
	int min = sysfs_read("gt_RPn_freq_mhz");
	int max = sysfs_read("gt_RP0_freq_mhz");
	if (max <= min)
		return;

	igt_debug("Setting min to %dMHz, and max to %dMHz\n", min, max);
	sysfs_write("gt_min_freq_mhz", min);
	sysfs_write("gt_max_freq_mhz", max);
}

static void set_min_freq(void)
{
	int min = sysfs_read("gt_RPn_freq_mhz");
	igt_require(min > 0);
	igt_debug("Setting min/max to %dMHz\n", min);
	(void)sysfs_write("gt_idle_freq_mhz", min);
	(void)sysfs_write("gt_boost_freq_mhz", min);
	igt_require(sysfs_write("gt_min_freq_mhz", min) == 0 &&
		    sysfs_write("gt_max_freq_mhz", min) == 0);
}

static void set_max_freq(void)
{
	int max = sysfs_read("gt_RP0_freq_mhz");
	igt_require(max > 0);
	igt_debug("Setting min/max to %dMHz\n", max);
	(void)sysfs_write("gt_idle_freq_mhz", max);
	(void)sysfs_write("gt_boost_freq_mhz", max);
	igt_require(sysfs_write("gt_max_freq_mhz", max) == 0 &&
		    sysfs_write("gt_min_freq_mhz", max) == 0);
}

igt_main
{
	const struct {
		const char *suffix;
		void (*func)(void);
	} rps[] = {
		{ "", set_auto_freq },
		{ "-min", set_min_freq },
		{ "-max", set_max_freq },
		{ NULL, NULL },
	}, *r;
	int min = -1, max = -1, boost = -1, idle = -1;
	uint32_t handle = 0;

	igt_fixture {
		device = drm_open_any();

		min = sysfs_read("gt_min_freq_mhz");
		max = sysfs_read("gt_max_freq_mhz");
		boost = sysfs_read("gt_boost_freq_mhz");
		idle = sysfs_read("gt_idle_freq_mhz");

		handle = gem_create(device, 4096);
		gem_write(device, handle, 0, batch, sizeof(batch));
	}

	for (r = rps; r->suffix; r++) {
		igt_fixture r->func();

		igt_subtest_f("render%s", r->suffix)
			loop(device, handle, I915_EXEC_RENDER, "render");

		igt_subtest_f("bsd%s", r->suffix)
			loop(device, handle, I915_EXEC_BSD, "bsd");

		igt_subtest_f("blt%s", r->suffix)
			loop(device, handle, I915_EXEC_BLT, "blt");

		igt_subtest_f("vebox%s", r->suffix)
			loop(device, handle, LOCAL_I915_EXEC_VEBOX, "vebox");
	}

	igt_fixture {
		gem_close(device, handle);

		if (min > 0)
			sysfs_write("gt_min_freq_mhz", min);
		if (max > 0)
			sysfs_write("gt_max_freq_mhz", max);
		if (boost > 0)
			sysfs_write("gt_boost_freq_mhz", boost);
		if (idle > 0)
			sysfs_write("gt_idle_freq_mhz", idle);
		close(device);
	}
}