gem_ctx_param.c 7.7 KB
Newer Older
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
/*
 * Copyright © 2015 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:
 *    Daniel Vetter <daniel.vetter@ffwll.ch>
 */

27 28 29
#include <fcntl.h>
#include <limits.h>

30
#include "igt.h"
31 32 33

IGT_TEST_DESCRIPTION("Basic test for context set/get param input validation.");

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
#define BIT(x) (1ul << (x))

#define NEW_CTX	BIT(0)
#define USER BIT(1)

static int reopen_driver(int fd)
{
	char path[256];

	snprintf(path, sizeof(path), "/proc/self/fd/%d", fd);
	fd = open(path, O_RDWR);
	igt_assert_lte(0, fd);

	return fd;
}

static void set_priority(int i915)
{
	static const int64_t test_values[] = {
		/* Test space too big, pick significant values */
		INT_MIN,

		I915_CONTEXT_MIN_USER_PRIORITY - 1,
		I915_CONTEXT_MIN_USER_PRIORITY,
		I915_CONTEXT_MIN_USER_PRIORITY + 1,

		I915_CONTEXT_DEFAULT_PRIORITY - 1,
		I915_CONTEXT_DEFAULT_PRIORITY,
		I915_CONTEXT_DEFAULT_PRIORITY + 1,

		I915_CONTEXT_MAX_USER_PRIORITY - 1,
		I915_CONTEXT_MAX_USER_PRIORITY,
		I915_CONTEXT_MAX_USER_PRIORITY + 1,

		INT_MAX
	};
	unsigned int size;
	int64_t *values;

	igt_require(getuid() == 0);

	size = ARRAY_SIZE(test_values);
	values = malloc(sizeof(test_values) * 8);
	igt_assert(values);

	for (unsigned i = 0; i < size; i++) {
		values[i + 0*size] = test_values[i];
		values[i + 1*size] = test_values[i] | (uint64_t)1 << 32;
		values[i + 2*size] = test_values[i] | (uint64_t)rand() << 32;
		values[i + 3*size] = test_values[i] ^ rand();
		values[i + 4*size] = rand() % (I915_CONTEXT_MAX_USER_PRIORITY - I915_CONTEXT_MIN_USER_PRIORITY) + I915_CONTEXT_MIN_USER_PRIORITY;
		values[i + 5*size] = rand();
		values[i + 6*size] = rand() | (uint64_t)rand() << 32;
		values[i + 7*size] = (uint64_t)test_values[i] << 32;
	}
	size *= 8;

	igt_permute_array(values, size, igt_exchange_int64);

	igt_fork(flags, NEW_CTX | USER) {
		int fd = reopen_driver(i915);
		struct drm_i915_gem_context_param arg = {
			.param = I915_CONTEXT_PARAM_PRIORITY,
			.ctx_id = flags & NEW_CTX ? gem_context_create(fd) : 0,
		};
		int64_t old_prio;

		if (flags & USER) {
			igt_debug("Dropping root privilege\n");
			igt_drop_root();
		}

		gem_context_get_param(fd, &arg);
		old_prio = arg.value;

		for (unsigned i = 0; i < size; i++) {
			int64_t prio = values[i];
			int expected = 0;
			int err;

			arg.value = prio;

			if (flags & USER &&
			    prio > I915_CONTEXT_DEFAULT_PRIORITY)
				expected = -EPERM;

			if (prio < I915_CONTEXT_MIN_USER_PRIORITY ||
			    prio > I915_CONTEXT_MAX_USER_PRIORITY)
				expected = -EINVAL;

			err =__gem_context_set_param(fd, &arg);
			igt_assert_f(err == expected,
				     "Priority requested %" PRId64 " with flags %x, expected result %d, returned %d\n",
				     prio, flags, expected, err);

			gem_context_get_param(fd, &arg);
			if (!err)
				old_prio = prio;
			igt_assert_eq(arg.value, old_prio);
		}

		arg.value = 0;
		gem_context_set_param(fd, &arg);

		if (flags & NEW_CTX)
			gem_context_destroy(fd, arg.ctx_id);
	}

	igt_waitchildren();
	free(values);
}

146 147
igt_main
{
148
	struct drm_i915_gem_context_param arg;
149 150
	int fd;
	uint32_t ctx;
151

152
	memset(&arg, 0, sizeof(arg));
153 154

	igt_fixture {
155
		fd = drm_open_driver_render(DRIVER_INTEL);
156 157

		gem_require_contexts(fd);
158 159
		ctx = gem_context_create(fd);

160
		arg.param = I915_CONTEXT_PARAM_BAN_PERIOD;
161

162 163 164 165 166
		/* XXX start to enforce ban period returning -EINVAL when
		 * transition has been done */
		if (__gem_context_get_param(fd, &arg) == -EINVAL)
			arg.param = I915_CONTEXT_PARAM_BANNABLE;
	}
167

168
	igt_subtest("basic") {
169
		arg.ctx_id = ctx;
170 171
		gem_context_get_param(fd, &arg);
		gem_context_set_param(fd, &arg);
172 173 174
	}

	igt_subtest("basic-default") {
175
		arg.ctx_id = 0;
176 177
		gem_context_get_param(fd, &arg);
		gem_context_set_param(fd, &arg);
178 179 180
	}

	igt_subtest("invalid-ctx-get") {
181
		arg.ctx_id = 2;
182
		igt_assert_eq(__gem_context_get_param(fd, &arg), -ENOENT);
183 184 185
	}

	igt_subtest("invalid-ctx-set") {
186
		arg.ctx_id = ctx;
187
		gem_context_get_param(fd, &arg);
188
		arg.ctx_id = 2;
189
		igt_assert_eq(__gem_context_set_param(fd, &arg), -ENOENT);
190 191 192
	}

	igt_subtest("invalid-size-get") {
193
		arg.ctx_id = ctx;
194 195 196
		arg.size = 8;
		gem_context_get_param(fd, &arg);
		igt_assert(arg.size == 0);
197 198 199
	}

	igt_subtest("invalid-size-set") {
200
		arg.ctx_id = ctx;
201 202 203 204
		gem_context_get_param(fd, &arg);
		arg.size = 8;
		igt_assert_eq(__gem_context_set_param(fd, &arg), -EINVAL);
		arg.size = 0;
205 206
	}

207 208 209 210
	igt_subtest("non-root-set") {
		igt_fork(child, 1) {
			igt_drop_root();

211
			arg.ctx_id = ctx;
212 213 214
			gem_context_get_param(fd, &arg);
			arg.value--;
			igt_assert_eq(__gem_context_set_param(fd, &arg), -EPERM);
215 216 217
		}

		igt_waitchildren();
218 219
	}

220
	igt_subtest("root-set") {
221
		arg.ctx_id = ctx;
222 223 224
		gem_context_get_param(fd, &arg);
		arg.value--;
		gem_context_set_param(fd, &arg);
225 226
	}

227
	arg.param = I915_CONTEXT_PARAM_NO_ZEROMAP;
228

229
	igt_subtest("non-root-set-no-zeromap") {
230 231 232
		igt_fork(child, 1) {
			igt_drop_root();

233
			arg.ctx_id = ctx;
234 235 236
			gem_context_get_param(fd, &arg);
			arg.value--;
			gem_context_set_param(fd, &arg);
237 238 239 240 241
		}

		igt_waitchildren();
	}

242
	igt_subtest("root-set-no-zeromap-enabled") {
243
		arg.ctx_id = ctx;
244 245 246
		gem_context_get_param(fd, &arg);
		arg.value = 1;
		gem_context_set_param(fd, &arg);
247 248 249
	}

	igt_subtest("root-set-no-zeromap-disabled") {
250
		arg.ctx_id = ctx;
251 252 253
		gem_context_get_param(fd, &arg);
		arg.value = 0;
		gem_context_set_param(fd, &arg);
254 255
	}

256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
	arg.param = I915_CONTEXT_PARAM_PRIORITY;

	igt_subtest("set-priority-not-supported") {
		igt_require(!gem_scheduler_has_ctx_priority(fd));

		arg.ctx_id = ctx;
		arg.size = 0;

		igt_assert_eq(__gem_context_set_param(fd, &arg), -ENODEV);
	}

	igt_subtest_group {
		igt_fixture {
			igt_require(gem_scheduler_has_ctx_priority(fd));
		}

		igt_subtest("get-priority-new-ctx") {
			struct drm_i915_gem_context_param local_arg = arg;
			uint32_t local_ctx = gem_context_create(fd);

			local_arg.ctx_id = local_ctx;

			gem_context_get_param(fd, &local_arg);
			igt_assert_eq(local_arg.value, I915_CONTEXT_DEFAULT_PRIORITY);

			gem_context_destroy(fd, local_ctx);
		}

		igt_subtest("set-priority-invalid-size") {
			struct drm_i915_gem_context_param local_arg = arg;
			local_arg.ctx_id = ctx;
			local_arg.value = 0;
			local_arg.size = ~0;

			igt_assert_eq(__gem_context_set_param(fd, &local_arg), -EINVAL);
		}

		igt_subtest("set-priority-range")
			set_priority(fd);
	}

297 298
	/* I915_CONTEXT_PARAM_SSEU tests are located in gem_ctx_sseu.c */

299 300
	/* NOTE: This testcase intentionally tests for the next free parameter
	 * to catch ABI extensions. Don't "fix" this testcase without adding all
301 302
	 * the tests for the new param first.
	 */
303
	arg.param = I915_CONTEXT_PARAM_SSEU + 1;
304 305

	igt_subtest("invalid-param-get") {
306
		arg.ctx_id = ctx;
307
		igt_assert_eq(__gem_context_get_param(fd, &arg), -EINVAL);
308 309 310
	}

	igt_subtest("invalid-param-set") {
311
		arg.ctx_id = ctx;
312
		igt_assert_eq(__gem_context_set_param(fd, &arg), -EINVAL);
313 314
	}

315 316 317
	igt_fixture
		close(fd);
}