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 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 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
|
/*
* Copyright © 2016 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.
*
*/
/** @file gem_exec_suspend.c
*
* Exercise executing batches across suspend before checking the results.
*/
#include <fcntl.h>
#include <unistd.h>
#include "i915/gem.h"
#include "i915/gem_create.h"
#include "igt.h"
#include "igt_dummyload.h"
#include "igt_gt.h"
#include "igt_sysfs.h"
/**
* TEST: gem exec suspend
* Description: Exercise simple execbufs runs across various suspend/resume cycles.
* Category: Core
* Mega feature: General Core features
* Sub-category: CMD submission
* Functionality: suspend
* Feature: cmd_submission, suspend
*
* SUBTEST: basic
* Description: Check basic functionality without any suspend/resume cycle.
*
* SUBTEST: basic-S0
* Description: Check with suspend-to-idle target state.
*
* SUBTEST: basic-S3
* Description: Check full cycle of suspend-to-mem.
*
* SUBTEST: basic-S3-devices
* Description: Check with suspend-to-mem with devices only.
*
* SUBTEST: basic-S4
* Description: Check full cycle of suspend-to-disk.
*
* SUBTEST: basic-S4-devices
* Description: Check with suspend-to-disk with devices only.
*
* SUBTEST: cached
* Description: Check without suspend/resume cycle state with cached object.
*
* SUBTEST: cached-S3
* Description: Check suspend-to-mem state with cached object.
*
* SUBTEST: cached-S4
* Description: Check suspend-to-disk state with cached object.
*
* SUBTEST: fixed
* Description: Check without suspend/resume cycle state with fixed object.
*
* SUBTEST: fixed-S3
* Description: Check suspend-to-mem state with fixed object.
*
* SUBTEST: fixed-S4
* Description: Check suspend-to-disk state with fixed object.
*
* SUBTEST: hang-S3
* Description: Check full cycle of suspend-to-mem with a pending GPU hang.
*
* SUBTEST: hang-S4
* Description: Check full cycle of suspend-to-disk with a pending GPU hang.
*
* SUBTEST: power-S0
* Description: Check power consumption during idle state.
*
* SUBTEST: power-S3
* Description: Check power consumption during suspend-to-mem state.
*
* SUBTEST: uncached
* Description: Check without suspend/resume cycle state with uncached object.
*
* SUBTEST: uncached-S3
* Description: Check suspend-to-mem state with uncached object.
*
* SUBTEST: uncached-S4
* Description: Check suspend-to-disk state with uncached object.
*/
IGT_TEST_DESCRIPTION("Exercise simple execbufs runs across various suspend/resume cycles.");
#define NOSLEEP 0
#define IDLE 1
#define SUSPEND_DEVICES 2
#define SUSPEND 3
#define HIBERNATE_DEVICES 4
#define HIBERNATE 5
#define mode(x) ((x) & 0xff)
#define ENGINE_MASK (I915_EXEC_RING_MASK | I915_EXEC_BSD_MASK)
#define UNCACHED (0<<8)
#define CACHED (1<<8)
#define HANG (2<<8)
static void run_test(int fd, const intel_ctx_t *ctx,
unsigned engine, unsigned flags, uint32_t region);
static void check_bo(int fd, uint32_t handle)
{
uint32_t *map;
int i;
igt_debug("Verifying result\n");
map = gem_mmap__cpu(fd, handle, 0, 4096, PROT_READ);
gem_set_domain(fd, handle, I915_GEM_DOMAIN_CPU, 0);
for (i = 0; i < 1024; i++)
igt_assert_eq(map[i], i);
munmap(map, 4096);
}
static void test_all(int fd, const intel_ctx_t *ctx, unsigned flags, uint32_t region)
{
run_test(fd, ctx, ALL_ENGINES, flags & ~0xff, region);
}
static void run_test(int fd, const intel_ctx_t *ctx,
unsigned engine, unsigned flags, uint32_t region)
{
const unsigned int gen = intel_gen(intel_get_drm_devid(fd));
const uint32_t bbe = MI_BATCH_BUFFER_END;
struct drm_i915_gem_exec_object2 obj[2];
struct drm_i915_gem_relocation_entry reloc;
struct drm_i915_gem_execbuffer2 execbuf;
unsigned engines[I915_EXEC_RING_MASK + 1];
unsigned nengine;
igt_spin_t *spin = NULL;
uint64_t ahnd = get_reloc_ahnd(fd, 0);
nengine = 0;
if (engine == ALL_ENGINES) {
const struct intel_execution_engine2 *e;
for_each_ctx_engine(fd, ctx, e) {
if (gem_class_can_store_dword(fd, e->class))
engines[nengine++] = e->flags;
}
} else {
engines[nengine++] = engine;
}
igt_require(nengine);
/* Before suspending, check normal operation */
if (mode(flags) != NOSLEEP)
test_all(fd, ctx, flags, region);
gem_quiescent_gpu(fd);
memset(&execbuf, 0, sizeof(execbuf));
execbuf.buffers_ptr = to_user_pointer(obj);
execbuf.buffer_count = 2;
execbuf.flags = 1 << 11;
if (gem_store_dword_needs_secure(fd))
execbuf.flags |= I915_EXEC_SECURE;
execbuf.rsvd1 = ctx->id;
memset(obj, 0, sizeof(obj));
obj[0].handle = gem_create_in_memory_regions(fd, 4096, region);
if (!gem_has_lmem(fd)) {
if (igt_has_set_caching(intel_get_drm_devid(fd)))
gem_set_caching(fd, obj[0].handle, !!(flags & CACHED));
}
obj[0].flags |= EXEC_OBJECT_WRITE;
obj[1].handle = gem_create_in_memory_regions(fd, 4096, region);
gem_write(fd, obj[1].handle, 0, &bbe, sizeof(bbe));
igt_require(__gem_execbuf(fd, &execbuf) == 0);
gem_close(fd, obj[1].handle);
if (!ahnd) {
memset(&reloc, 0, sizeof(reloc));
reloc.target_handle = obj[0].handle;
reloc.presumed_offset = obj[0].offset;
reloc.offset = sizeof(uint32_t);
if (gen >= 4 && gen < 8)
reloc.offset += sizeof(uint32_t);
reloc.read_domains = I915_GEM_DOMAIN_INSTRUCTION;
reloc.write_domain = I915_GEM_DOMAIN_INSTRUCTION;
obj[1].relocs_ptr = to_user_pointer(&reloc);
obj[1].relocation_count = 1;
} else {
/* ignore first execbuf offset */
obj[0].offset = get_offset(ahnd, obj[0].handle, 4096, 0);
obj[0].flags |= EXEC_OBJECT_PINNED | EXEC_OBJECT_SUPPORTS_48B_ADDRESS;
}
for (int i = 0; i < 1024; i++) {
uint64_t offset;
uint32_t buf[16];
int b;
reloc.delta = i * sizeof(uint32_t);
obj[1].handle = gem_create(fd, 4096);
if (ahnd) {
obj[1].offset = get_offset(ahnd, obj[1].handle, 4096, 0);
obj[1].flags |= EXEC_OBJECT_PINNED | EXEC_OBJECT_SUPPORTS_48B_ADDRESS;
offset = obj[0].offset + reloc.delta;
} else {
offset = reloc.presumed_offset + reloc.delta;
}
b = 0;
buf[b] = MI_STORE_DWORD_IMM_GEN4 | (gen < 6 ? 1 << 22 : 0);
if (gen >= 8) {
buf[++b] = offset;
buf[++b] = offset >> 32;
} else if (gen >= 4) {
buf[++b] = 0;
buf[++b] = offset;
} else {
buf[b] -= 1;
buf[++b] = offset;
}
buf[++b] = i;
buf[++b] = MI_BATCH_BUFFER_END;
gem_write(fd, obj[1].handle,
4096-sizeof(buf), buf, sizeof(buf));
execbuf.flags &= ~ENGINE_MASK;
execbuf.flags |= engines[rand() % nengine];
gem_execbuf(fd, &execbuf);
gem_close(fd, obj[1].handle);
}
if (flags & HANG)
spin = igt_spin_new(fd, .ahnd = ahnd, .engine = engine,
.ctx = ctx);
switch (mode(flags)) {
case NOSLEEP:
break;
case IDLE:
igt_system_suspend_autoresume(SUSPEND_STATE_FREEZE,
SUSPEND_TEST_NONE);
break;
case SUSPEND_DEVICES:
igt_system_suspend_autoresume(SUSPEND_STATE_MEM,
SUSPEND_TEST_DEVICES);
break;
case SUSPEND:
igt_system_suspend_autoresume(SUSPEND_STATE_MEM,
SUSPEND_TEST_NONE);
break;
case HIBERNATE_DEVICES:
igt_system_suspend_autoresume(SUSPEND_STATE_DISK,
SUSPEND_TEST_DEVICES);
break;
case HIBERNATE:
igt_system_suspend_autoresume(SUSPEND_STATE_DISK,
SUSPEND_TEST_NONE);
break;
}
igt_spin_free(fd, spin);
check_bo(fd, obj[0].handle);
gem_close(fd, obj[0].handle);
put_ahnd(ahnd);
gem_quiescent_gpu(fd);
/* After resume, make sure it still works */
if (mode(flags) != NOSLEEP)
test_all(fd, ctx, flags, region);
}
struct battery_sample {
struct timespec tv;
uint64_t charge;
};
static bool get_power(int dir, struct battery_sample *s)
{
return (clock_gettime(CLOCK_REALTIME, &s->tv) == 0 &&
igt_sysfs_scanf(dir, "charge_now", "%"PRIu64, &s->charge) == 1);
}
static double d_charge(const struct battery_sample *after,
const struct battery_sample *before)
{
return (before->charge - after->charge) * 1e-3; /* mWh */
}
static double d_time(const struct battery_sample *after,
const struct battery_sample *before)
{
return ((after->tv.tv_sec - before->tv.tv_sec) +
(after->tv.tv_nsec - before->tv.tv_nsec) * 1e-9); /* s */
}
static void power_test(int i915, const intel_ctx_t *ctx,
unsigned engine, unsigned flags, uint32_t region)
{
struct battery_sample before, after;
char *status;
int dir;
dir = open("/sys/class/power_supply/BAT0", O_RDONLY);
igt_require_f(dir != -1, "/sys/class/power_supply/BAT0 not available\n");
igt_require_f(get_power(dir, &before),
"power test needs reported energy level\n");
status = igt_sysfs_get(dir, "status");
igt_require_f(status && strcmp(status, "Discharging") == 0,
"power test needs to be on battery, not mains, power\n");
free(status);
igt_set_autoresume_delay(5 * 60); /* 5 minutes; longer == more stable */
igt_assert(get_power(dir, &before));
run_test(i915, ctx, engine, flags, region);
igt_assert(get_power(dir, &after));
igt_set_autoresume_delay(0);
igt_info("Power consumed while suspended: %.3fmWh\n",
d_charge(&after, &before));
igt_info("Discharge rate while suspended: %.3fmW\n",
d_charge(&after, &before) * 3600 / d_time(&after, &before));
}
igt_main
{
const struct {
const char *suffix;
unsigned mode;
const char *describe;
} modes[] = {
{ "", NOSLEEP, "without suspend/resume cycle" },
{ "-S3", SUSPEND, "suspend-to-mem" },
{ "-S4", HIBERNATE, "suspend-to-disk" },
{ NULL, 0, "" }
}, *m;
struct test {
const char *name;
unsigned int flags;
void (*fn)(int, const intel_ctx_t *, unsigned, unsigned, uint32_t);
const char *describe;
} *test, tests_all_engines[] = {
{ "basic", NOSLEEP, run_test, "Check basic functionality without any "
"suspend/resume cycle." },
{ "basic-S0", IDLE, run_test, "Check with suspend-to-idle target state." },
{ "basic-S3-devices", SUSPEND_DEVICES, run_test, "Check with suspend-to-mem "
"with devices only." },
{ "basic-S3", SUSPEND, run_test, "Check full cycle of suspend-to-mem." },
{ "basic-S4-devices", HIBERNATE_DEVICES, run_test, "Check with suspend-to-disk "
"with devices only." },
{ "basic-S4", HIBERNATE, run_test, "Check full cycle of suspend-to-disk." },
{ }
}, tests_power_hang[] = {
{ "hang-S3", SUSPEND | HANG, run_test, "Check full cycle of suspend-to-mem with a "
"pending GPU hang." },
{ "hang-S4", HIBERNATE | HANG, run_test, "Check full cycle of suspend-to-disk with "
"a pending GPU hang." },
{ "power-S0", IDLE, power_test, "Check power consumption during idle state." },
{ "power-S3", SUSPEND, power_test, "Check power consumption during "
"suspend-to-mem state." },
{ }
};
const struct intel_execution_engine2 *e;
igt_hang_t hang;
const intel_ctx_t *ctx;
int fd;
char *sub_name;
uint32_t region;
struct drm_i915_query_memory_regions *query_info;
struct igt_collection *set, *regions;
igt_fixture {
fd = drm_open_driver_master(DRIVER_INTEL);
igt_require_gem(fd);
igt_require(gem_can_store_dword(fd, 0));
ctx = intel_ctx_create_all_physical(fd);
igt_fork_hang_detector(fd);
query_info = gem_get_query_memory_regions(fd);
igt_assert(query_info);
set = get_memory_region_set(query_info,
I915_SYSTEM_MEMORY,
I915_DEVICE_MEMORY);
}
#define subtest_for_each_combination(__name, __ctx, __flags, __fn) \
igt_subtest_with_dynamic(__name) { \
for_each_combination(regions, 1, set) { \
sub_name = memregion_dynamic_subtest_name(regions); \
region = igt_collection_get_value(regions, 0); \
igt_dynamic_f("%s", sub_name) \
(__fn)(fd, (__ctx), ALL_ENGINES, (__flags), region); \
free(sub_name); \
} \
}
#define for_each_ctx_engine_combination(__mode) \
for_each_ctx_engine(fd, ctx, e) { \
if (!gem_class_can_store_dword(fd, e->class)) \
continue; \
for_each_combination(regions, 1, set) { \
sub_name = memregion_dynamic_subtest_name(regions); \
region = igt_collection_get_value(regions, 0); \
igt_dynamic_f("%s-%s", e->name, sub_name) \
run_test(fd, ctx, e->flags, (__mode), region); \
free(sub_name); \
} \
}
for (test = tests_all_engines; test->name; test++) {
igt_describe(test->describe);
subtest_for_each_combination(test->name, intel_ctx_0(fd), test->flags, test->fn);
}
for (m = modes; m->suffix; m++) {
igt_describe_f("Check %s state with fixed object.", m->describe);
igt_subtest_with_dynamic_f("fixed%s", m->suffix) {
igt_require(gem_has_lmem(fd));
for_each_ctx_engine_combination(m->mode);
}
igt_describe_f("Check %s state with uncached object.", m->describe);
igt_subtest_with_dynamic_f("uncached%s", m->suffix) {
igt_require(!gem_has_lmem(fd));
for_each_ctx_engine_combination(m->mode | UNCACHED);
}
igt_describe_f("Check %s state with cached object.", m->describe);
igt_subtest_with_dynamic_f("cached%s", m->suffix) {
igt_require(!gem_has_lmem(fd));
for_each_ctx_engine_combination(m->mode | CACHED);
}
}
igt_fixture {
igt_stop_hang_detector();
hang = igt_allow_hang(fd, 0, 0);
}
for (test = tests_power_hang; test->name; test++) {
igt_describe(test->describe);
subtest_for_each_combination(test->name, intel_ctx_0(fd), test->flags, test->fn);
}
igt_fixture {
free(query_info);
igt_collection_destroy(set);
igt_disallow_hang(fd, hang);
intel_ctx_destroy(fd, ctx);
drm_close_driver(fd);
}
}
|