2 * Copyright © 2013 Intel Corporation
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:
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
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
24 * Paulo Zanoni <paulo.r.zanoni@intel.com>
36 #include <sys/ioctl.h>
38 #include <sys/types.h>
40 #include <linux/i2c.h>
41 #include <linux/i2c-dev.h>
46 #include "intel_batchbuffer.h"
48 #include "intel_chipset.h"
49 #include "ioctl_wrappers.h"
52 #include "igt_debugfs.h"
54 #define MSR_PC8_RES 0x630
55 #define MSR_PC9_RES 0x631
56 #define MSR_PC10_RES 0x632
58 #define MAX_CONNECTORS 32
59 #define MAX_ENCODERS 32
62 #define POWER_DIR "/sys/devices/pci0000:00/0000:00:02.0/power"
78 #define WAIT_PC8_RES 2
82 int drm_fd, msr_fd, pm_status_fd, pc8_status_fd;
83 bool has_runtime_pm, has_pc8;
84 struct mode_set_data ms_data;
85 struct scanout_fb *fbs = NULL;
87 /* Stuff used when creating FBs and mode setting. */
88 struct mode_set_data {
90 drmModeConnectorPtr connectors[MAX_CONNECTORS];
91 drmModePropertyBlobPtr edids[MAX_CONNECTORS];
96 /* Stuff we query at different times so we can compare. */
99 drmModeEncoderPtr encoders[MAX_ENCODERS];
100 drmModeConnectorPtr connectors[MAX_CONNECTORS];
101 drmModeCrtcPtr crtcs[MAX_CRTCS];
102 drmModePropertyBlobPtr edids[MAX_CONNECTORS];
105 /* During the stress tests we want to be as fast as possible, so use pre-created
106 * FBs instead of creating them again and again. */
111 struct scanout_fb *next;
114 /* If the read fails, then the machine doesn't support PC8+ residencies. */
115 static bool supports_pc8_plus_residencies(void)
120 rc = pread(msr_fd, &val, sizeof(uint64_t), MSR_PC8_RES);
121 if (rc != sizeof(val))
123 rc = pread(msr_fd, &val, sizeof(uint64_t), MSR_PC9_RES);
124 if (rc != sizeof(val))
126 rc = pread(msr_fd, &val, sizeof(uint64_t), MSR_PC10_RES);
127 if (rc != sizeof(val))
133 static uint64_t get_residency(uint32_t type)
138 rc = pread(msr_fd, &ret, sizeof(uint64_t), type);
139 igt_assert(rc == sizeof(ret));
144 static bool pc8_plus_residency_changed(unsigned int timeout_sec)
147 uint64_t res_pc8, res_pc9, res_pc10;
148 int to_sleep = 100 * 1000;
150 res_pc8 = get_residency(MSR_PC8_RES);
151 res_pc9 = get_residency(MSR_PC9_RES);
152 res_pc10 = get_residency(MSR_PC10_RES);
154 for (i = 0; i < timeout_sec * 1000 * 1000; i += to_sleep) {
155 if (res_pc8 != get_residency(MSR_PC8_RES) ||
156 res_pc9 != get_residency(MSR_PC9_RES) ||
157 res_pc10 != get_residency(MSR_PC10_RES)) {
166 static enum pc8_status get_pc8_status(void)
169 char buf[150]; /* The whole file has less than 100 chars. */
171 lseek(pc8_status_fd, 0, SEEK_SET);
172 n_read = read(pc8_status_fd, buf, ARRAY_SIZE(buf));
173 igt_assert(n_read >= 0);
176 if (strstr(buf, "\nEnabled: yes\n"))
182 static bool wait_for_pc8_status(enum pc8_status status)
185 int hundred_ms = 100 * 1000, ten_s = 10 * 1000 * 1000;
187 for (i = 0; i < ten_s; i += hundred_ms) {
188 if (get_pc8_status() == status)
197 static bool wait_for_suspended(void)
199 if (has_pc8 && !has_runtime_pm)
200 return wait_for_pc8_status(PC8_ENABLED);
202 return igt_wait_for_pm_status(IGT_RUNTIME_PM_STATUS_SUSPENDED);
205 static bool wait_for_active(void)
207 if (has_pc8 && !has_runtime_pm)
208 return wait_for_pc8_status(PC8_DISABLED);
210 return igt_wait_for_pm_status(IGT_RUNTIME_PM_STATUS_ACTIVE);
213 static void disable_all_screens_dpms(struct mode_set_data *data)
217 for (i = 0; i < data->res->count_connectors; i++) {
218 drmModeConnectorPtr c = data->connectors[i];
220 kmstest_set_connector_dpms(drm_fd, c, DRM_MODE_DPMS_OFF);
224 static void disable_all_screens(struct mode_set_data *data)
228 for (i = 0; i < data->res->count_crtcs; i++) {
229 rc = drmModeSetCrtc(drm_fd, data->res->crtcs[i], -1, 0, 0,
235 static struct scanout_fb *create_fb(struct mode_set_data *data, int width,
238 struct scanout_fb *fb_info;
242 fb_info = malloc(sizeof(struct scanout_fb));
245 fb_info->handle = igt_create_fb(drm_fd, width, height,
248 fb_info->width = width;
249 fb_info->height = height;
250 fb_info->next = NULL;
252 cr = igt_get_cairo_ctx(drm_fd, &fb);
253 igt_paint_test_pattern(cr, width, height);
259 static uint32_t get_fb(struct mode_set_data *data, int width, int height)
261 struct scanout_fb *fb;
264 fbs = create_fb(data, width, height);
268 for (fb = fbs; fb != NULL; fb = fb->next) {
269 if (fb->width == width && fb->height == height)
273 fb->next = create_fb(data, width, height);
274 return fb->next->handle;
280 static bool enable_one_screen_with_type(struct mode_set_data *data,
281 enum screen_type type)
283 uint32_t crtc_id = 0, buffer_id = 0, connector_id = 0;
284 drmModeModeInfoPtr mode = NULL;
287 for (i = 0; i < data->res->count_connectors; i++) {
288 drmModeConnectorPtr c = data->connectors[i];
290 if (type == SCREEN_TYPE_LPSP &&
291 c->connector_type != DRM_MODE_CONNECTOR_eDP)
294 if (type == SCREEN_TYPE_NON_LPSP &&
295 c->connector_type == DRM_MODE_CONNECTOR_eDP)
298 if (c->connection == DRM_MODE_CONNECTED && c->count_modes) {
299 connector_id = c->connector_id;
305 if (connector_id == 0)
308 crtc_id = data->res->crtcs[0];
309 buffer_id = get_fb(data, mode->hdisplay, mode->vdisplay);
312 igt_assert(buffer_id);
313 igt_assert(connector_id);
316 rc = drmModeSetCrtc(drm_fd, crtc_id, buffer_id, 0, 0, &connector_id,
323 static void enable_one_screen(struct mode_set_data *data)
325 /* SKIP if there are no connected screens. */
326 igt_require(enable_one_screen_with_type(data, SCREEN_TYPE_ANY));
329 static drmModePropertyBlobPtr get_connector_edid(drmModeConnectorPtr connector,
333 drmModeObjectPropertiesPtr props;
334 drmModePropertyBlobPtr ret = NULL;
336 props = drmModeObjectGetProperties(drm_fd, connector->connector_id,
337 DRM_MODE_OBJECT_CONNECTOR);
339 for (i = 0; i < props->count_props; i++) {
340 drmModePropertyPtr prop = drmModeGetProperty(drm_fd,
343 if (strcmp(prop->name, "EDID") == 0) {
344 igt_assert(prop->flags & DRM_MODE_PROP_BLOB);
345 igt_assert(prop->count_blobs == 0);
346 ret = drmModeGetPropertyBlob(drm_fd,
347 props->prop_values[i]);
350 drmModeFreeProperty(prop);
353 drmModeFreeObjectProperties(props);
357 static void init_mode_set_data(struct mode_set_data *data)
361 data->res = drmModeGetResources(drm_fd);
362 igt_assert(data->res);
363 igt_assert(data->res->count_connectors <= MAX_CONNECTORS);
365 for (i = 0; i < data->res->count_connectors; i++) {
366 data->connectors[i] = drmModeGetConnector(drm_fd,
367 data->res->connectors[i]);
368 data->edids[i] = get_connector_edid(data->connectors[i], i);
371 data->devid = intel_get_drm_devid(drm_fd);
373 igt_set_vt_graphics_mode();
376 static void fini_mode_set_data(struct mode_set_data *data)
380 for (i = 0; i < data->res->count_connectors; i++) {
381 drmModeFreeConnector(data->connectors[i]);
382 drmModeFreePropertyBlob(data->edids[i]);
384 drmModeFreeResources(data->res);
387 static void get_drm_info(struct compare_data *data)
391 data->res = drmModeGetResources(drm_fd);
392 igt_assert(data->res);
394 igt_assert(data->res->count_connectors <= MAX_CONNECTORS);
395 igt_assert(data->res->count_encoders <= MAX_ENCODERS);
396 igt_assert(data->res->count_crtcs <= MAX_CRTCS);
398 for (i = 0; i < data->res->count_connectors; i++) {
399 data->connectors[i] = drmModeGetConnector(drm_fd,
400 data->res->connectors[i]);
401 data->edids[i] = get_connector_edid(data->connectors[i], i);
403 for (i = 0; i < data->res->count_encoders; i++)
404 data->encoders[i] = drmModeGetEncoder(drm_fd,
405 data->res->encoders[i]);
406 for (i = 0; i < data->res->count_crtcs; i++)
407 data->crtcs[i] = drmModeGetCrtc(drm_fd, data->res->crtcs[i]);
410 static void free_drm_info(struct compare_data *data)
414 for (i = 0; i < data->res->count_connectors; i++) {
415 drmModeFreeConnector(data->connectors[i]);
416 drmModeFreePropertyBlob(data->edids[i]);
418 for (i = 0; i < data->res->count_encoders; i++)
419 drmModeFreeEncoder(data->encoders[i]);
420 for (i = 0; i < data->res->count_crtcs; i++)
421 drmModeFreeCrtc(data->crtcs[i]);
423 drmModeFreeResources(data->res);
426 #define COMPARE(d1, d2, data) igt_assert(d1->data == d2->data)
427 #define COMPARE_ARRAY(d1, d2, size, data) do { \
428 for (i = 0; i < size; i++) \
429 igt_assert(d1->data[i] == d2->data[i]); \
432 static void assert_drm_resources_equal(struct compare_data *d1,
433 struct compare_data *d2)
435 COMPARE(d1, d2, res->count_connectors);
436 COMPARE(d1, d2, res->count_encoders);
437 COMPARE(d1, d2, res->count_crtcs);
438 COMPARE(d1, d2, res->min_width);
439 COMPARE(d1, d2, res->max_width);
440 COMPARE(d1, d2, res->min_height);
441 COMPARE(d1, d2, res->max_height);
444 static void assert_modes_equal(drmModeModeInfoPtr m1, drmModeModeInfoPtr m2)
446 COMPARE(m1, m2, clock);
447 COMPARE(m1, m2, hdisplay);
448 COMPARE(m1, m2, hsync_start);
449 COMPARE(m1, m2, hsync_end);
450 COMPARE(m1, m2, htotal);
451 COMPARE(m1, m2, hskew);
452 COMPARE(m1, m2, vdisplay);
453 COMPARE(m1, m2, vsync_start);
454 COMPARE(m1, m2, vsync_end);
455 COMPARE(m1, m2, vtotal);
456 COMPARE(m1, m2, vscan);
457 COMPARE(m1, m2, vrefresh);
458 COMPARE(m1, m2, flags);
459 COMPARE(m1, m2, type);
460 igt_assert(strcmp(m1->name, m2->name) == 0);
463 static void assert_drm_connectors_equal(drmModeConnectorPtr c1,
464 drmModeConnectorPtr c2)
468 COMPARE(c1, c2, connector_id);
469 COMPARE(c1, c2, connector_type);
470 COMPARE(c1, c2, connector_type_id);
471 COMPARE(c1, c2, mmWidth);
472 COMPARE(c1, c2, mmHeight);
473 COMPARE(c1, c2, count_modes);
474 COMPARE(c1, c2, count_props);
475 COMPARE(c1, c2, count_encoders);
476 COMPARE_ARRAY(c1, c2, c1->count_props, props);
477 COMPARE_ARRAY(c1, c2, c1->count_encoders, encoders);
479 for (i = 0; i < c1->count_modes; i++)
480 assert_modes_equal(&c1->modes[0], &c2->modes[0]);
483 static void assert_drm_encoders_equal(drmModeEncoderPtr e1,
484 drmModeEncoderPtr e2)
486 COMPARE(e1, e2, encoder_id);
487 COMPARE(e1, e2, encoder_type);
488 COMPARE(e1, e2, possible_crtcs);
489 COMPARE(e1, e2, possible_clones);
492 static void assert_drm_crtcs_equal(drmModeCrtcPtr c1, drmModeCrtcPtr c2)
494 COMPARE(c1, c2, crtc_id);
497 static void assert_drm_edids_equal(drmModePropertyBlobPtr e1,
498 drmModePropertyBlobPtr e2)
502 igt_assert(e1 && e2);
505 COMPARE(e1, e2, length);
507 igt_assert(memcmp(e1->data, e2->data, e1->length) == 0);
510 static void assert_drm_infos_equal(struct compare_data *d1,
511 struct compare_data *d2)
515 assert_drm_resources_equal(d1, d2);
517 for (i = 0; i < d1->res->count_connectors; i++) {
518 assert_drm_connectors_equal(d1->connectors[i],
520 assert_drm_edids_equal(d1->edids[i], d2->edids[i]);
523 for (i = 0; i < d1->res->count_encoders; i++)
524 assert_drm_encoders_equal(d1->encoders[i], d2->encoders[i]);
526 for (i = 0; i < d1->res->count_crtcs; i++)
527 assert_drm_crtcs_equal(d1->crtcs[i], d2->crtcs[i]);
530 /* We could check the checksum too, but just the header is probably enough. */
531 static bool edid_is_valid(const unsigned char *edid)
533 char edid_header[] = {
534 0x0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0,
537 return (memcmp(edid, edid_header, sizeof(edid_header)) == 0);
540 static int count_drm_valid_edids(struct mode_set_data *data)
544 for (i = 0; i < data->res->count_connectors; i++)
545 if (data->edids[i] && edid_is_valid(data->edids[i]->data))
550 static bool i2c_edid_is_valid(int fd)
553 unsigned char edid[128] = {};
554 struct i2c_msg msgs[] = {
560 }, { /* Now read the EDID. */
567 struct i2c_rdwr_ioctl_data msgset = {
572 rc = ioctl(fd, I2C_RDWR, &msgset);
573 return (rc >= 0) ? edid_is_valid(edid) : false;
576 static int count_i2c_valid_edids(void)
581 struct dirent *dirent;
584 dir = opendir("/dev/");
587 while ((dirent = readdir(dir))) {
588 if (strncmp(dirent->d_name, "i2c-", 4) == 0) {
589 snprintf(full_name, 32, "/dev/%s", dirent->d_name);
590 fd = open(full_name, O_RDWR);
591 igt_assert(fd != -1);
592 if (i2c_edid_is_valid(fd))
603 static void test_i2c(struct mode_set_data *data)
605 int i2c_edids = count_i2c_valid_edids();
606 int drm_edids = count_drm_valid_edids(data);
608 igt_assert_cmpint(i2c_edids, ==, drm_edids);
611 static void setup_pc8(void)
615 /* Only Haswell supports the PC8 feature. */
616 if (!IS_HASWELL(ms_data.devid) && !IS_BROADWELL(ms_data.devid))
619 /* Make sure our Kernel supports MSR and the module is loaded. */
620 igt_assert(system("modprobe -q msr > /dev/null 2>&1") != -1);
622 msr_fd = open("/dev/cpu/0/msr", O_RDONLY);
623 igt_assert_f(msr_fd >= 0,
624 "Can't open /dev/cpu/0/msr.\n");
626 /* Non-ULT machines don't support PC8+. */
627 if (!supports_pc8_plus_residencies())
630 pc8_status_fd = open("/sys/kernel/debug/dri/0/i915_pc8_status",
632 igt_assert_f(pc8_status_fd >= 0,
633 "Can't open /sys/kernel/debug/dri/0/i915_pc8_status");
638 /* If we want to actually reach PC8+ states, we need to properly configure all
639 * the devices on the system to allow this. This function will try to setup the
640 * things we know we need, but won't scream in case anything fails: we don't
641 * know which devices are present on your machine, so we can't really expect
642 * anything, just try to help with the more common problems. */
643 static void setup_non_graphics_runtime_pm(void)
648 /* Disk runtime PM policies. */
649 file_name = malloc(PATH_MAX);
652 snprintf(file_name, PATH_MAX,
653 "/sys/class/scsi_host/host%d/link_power_management_policy",
656 fd = open(file_name, O_WRONLY);
660 write(fd, "min_power\n", 10);
665 /* Audio runtime PM policies. */
666 fd = open("/sys/module/snd_hda_intel/parameters/power_save", O_WRONLY);
671 fd = open("/sys/bus/pci/devices/0000:00:03.0/power/control", O_WRONLY);
673 write(fd, "auto\n", 5);
678 static void setup_environment(void)
680 drm_fd = drm_open_any();
681 igt_assert(drm_fd >= 0);
683 igt_require_f(drmSetMaster(drm_fd) == 0, "Can't become DRM master, "
684 "please check if no other DRM client is running.\n");
686 init_mode_set_data(&ms_data);
688 setup_non_graphics_runtime_pm();
690 has_runtime_pm = igt_setup_runtime_pm();
693 igt_info("Runtime PM support: %d\n", has_runtime_pm);
694 igt_info("PC8 residency support: %d\n", has_pc8);
696 igt_require(has_runtime_pm);
699 static void teardown_environment(void)
701 struct scanout_fb *fb, *fb_next;
710 fini_mode_set_data(&ms_data);
714 close(pc8_status_fd);
717 static void basic_subtest(void)
719 disable_all_screens(&ms_data);
720 igt_assert(wait_for_suspended());
722 enable_one_screen(&ms_data);
723 igt_assert(wait_for_active());
726 static void pc8_residency_subtest(void)
728 igt_require(has_pc8);
730 /* Make sure PC8+ residencies move! */
731 disable_all_screens(&ms_data);
732 igt_assert_f(pc8_plus_residency_changed(120),
733 "Machine is not reaching PC8+ states, please check its "
736 /* Make sure PC8+ residencies stop! */
737 enable_one_screen(&ms_data);
738 igt_assert_f(!pc8_plus_residency_changed(10),
739 "PC8+ residency didn't stop with screen enabled.\n");
742 static void modeset_subtest(enum screen_type type, int rounds, int wait_flags)
746 if (wait_flags & WAIT_PC8_RES)
747 igt_require(has_pc8);
749 for (i = 0; i < rounds; i++) {
750 if (wait_flags & USE_DPMS)
751 disable_all_screens_dpms(&ms_data);
753 disable_all_screens(&ms_data);
755 if (wait_flags & WAIT_STATUS)
756 igt_assert(wait_for_suspended());
757 if (wait_flags & WAIT_PC8_RES)
758 igt_assert(pc8_plus_residency_changed(120));
759 if (wait_flags & WAIT_EXTRA)
762 /* If we skip this line it's because the type of screen we want
763 * is not connected. */
764 igt_require(enable_one_screen_with_type(&ms_data, type));
765 if (wait_flags & WAIT_STATUS)
766 igt_assert(wait_for_active());
767 if (wait_flags & WAIT_PC8_RES)
768 igt_assert(!pc8_plus_residency_changed(5));
769 if (wait_flags & WAIT_EXTRA)
774 /* Test of the DRM resources reported by the IOCTLs are still the same. This
775 * ensures we still see the monitors with the same eyes. We get the EDIDs and
776 * compare them, which ensures we use DP AUX or GMBUS depending on what's
778 static void drm_resources_equal_subtest(void)
780 struct compare_data pre_suspend, during_suspend, post_suspend;
782 enable_one_screen(&ms_data);
783 igt_assert(wait_for_active());
784 get_drm_info(&pre_suspend);
785 igt_assert(wait_for_active());
787 disable_all_screens(&ms_data);
788 igt_assert(wait_for_suspended());
789 get_drm_info(&during_suspend);
790 igt_assert(wait_for_suspended());
792 enable_one_screen(&ms_data);
793 igt_assert(wait_for_active());
794 get_drm_info(&post_suspend);
795 igt_assert(wait_for_active());
797 assert_drm_infos_equal(&pre_suspend, &during_suspend);
798 assert_drm_infos_equal(&pre_suspend, &post_suspend);
800 free_drm_info(&pre_suspend);
801 free_drm_info(&during_suspend);
802 free_drm_info(&post_suspend);
805 static void i2c_subtest_check_environment(void)
807 int i2c_dev_files = 0;
809 struct dirent *dirent;
811 /* Make sure the /dev/i2c-* files exist. */
812 igt_assert(system("modprobe -q i2c-dev > /dev/null 2>&1") != -1);
814 dev_dir = opendir("/dev");
816 while ((dirent = readdir(dev_dir))) {
817 if (strncmp(dirent->d_name, "i2c-", 4) == 0)
821 igt_require(i2c_dev_files);
824 /* Try to use raw I2C, which also needs interrupts. */
825 static void i2c_subtest(void)
827 i2c_subtest_check_environment();
829 enable_one_screen(&ms_data);
830 igt_assert(wait_for_active());
832 disable_all_screens(&ms_data);
833 igt_assert(wait_for_suspended());
835 igt_assert(wait_for_suspended());
837 enable_one_screen(&ms_data);
840 static void read_full_file(const char *name)
845 igt_assert_f(wait_for_suspended(), "File: %s\n", name);
847 fd = open(name, O_RDONLY);
852 rc = read(fd, buf, ARRAY_SIZE(buf));
853 } while (rc == ARRAY_SIZE(buf));
858 igt_assert_f(wait_for_suspended(), "File: %s\n", name);
861 static void read_files_from_dir(const char *name, int level)
864 struct dirent *dirent;
871 full_name = malloc(PATH_MAX);
873 igt_assert(level < 128);
875 while ((dirent = readdir(dir))) {
876 struct stat stat_buf;
878 if (strcmp(dirent->d_name, ".") == 0)
880 if (strcmp(dirent->d_name, "..") == 0)
883 snprintf(full_name, PATH_MAX, "%s/%s", name, dirent->d_name);
885 rc = lstat(full_name, &stat_buf);
888 if (S_ISDIR(stat_buf.st_mode))
889 read_files_from_dir(full_name, level + 1);
891 if (S_ISREG(stat_buf.st_mode))
892 read_full_file(full_name);
899 /* This test will probably pass, with a small chance of hanging the machine in
900 * case of bugs. Many of the bugs exercised by this patch just result in dmesg
901 * errors, so a "pass" here should be confirmed by a check on dmesg. */
902 static void debugfs_read_subtest(void)
904 const char *path = "/sys/kernel/debug/dri/0";
908 igt_require_f(dir, "Can't open the debugfs directory\n");
911 disable_all_screens(&ms_data);
912 igt_assert(wait_for_suspended());
914 read_files_from_dir(path, 0);
917 /* Read the comment on debugfs_read_subtest(). */
918 static void sysfs_read_subtest(void)
920 const char *path = "/sys/devices/pci0000:00/0000:00:02.0";
924 igt_require_f(dir, "Can't open the sysfs directory\n");
927 disable_all_screens(&ms_data);
928 igt_assert(wait_for_suspended());
930 read_files_from_dir(path, 0);
933 /* Make sure we don't suspend when we have the i915_forcewake_user file open. */
934 static void debugfs_forcewake_user_subtest(void)
938 igt_require(intel_gen(ms_data.devid) >= 6);
940 disable_all_screens(&ms_data);
941 igt_assert(wait_for_suspended());
943 fd = igt_open_forcewake_handle();
944 igt_require(fd >= 0);
946 if (has_runtime_pm) {
947 igt_assert(wait_for_active());
949 igt_assert(wait_for_active());
951 igt_assert(wait_for_suspended());
957 igt_assert(wait_for_suspended());
960 static void gem_mmap_subtest(bool gtt_mmap)
967 /* Create, map and set data while the device is active. */
968 enable_one_screen(&ms_data);
969 igt_assert(wait_for_active());
971 handle = gem_create(drm_fd, buf_size);
974 gem_buf = gem_mmap__gtt(drm_fd, handle, buf_size,
975 PROT_READ | PROT_WRITE);
977 gem_buf = gem_mmap__cpu(drm_fd, handle, buf_size, 0);
980 for (i = 0; i < buf_size; i++)
981 gem_buf[i] = i & 0xFF;
983 for (i = 0; i < buf_size; i++)
984 igt_assert(gem_buf[i] == (i & 0xFF));
986 /* Now suspend, read and modify. */
987 disable_all_screens(&ms_data);
988 igt_assert(wait_for_suspended());
990 for (i = 0; i < buf_size; i++)
991 igt_assert(gem_buf[i] == (i & 0xFF));
992 igt_assert(wait_for_suspended());
994 for (i = 0; i < buf_size; i++)
995 gem_buf[i] = (~i & 0xFF);
996 igt_assert(wait_for_suspended());
998 /* Now resume and see if it's still there. */
999 enable_one_screen(&ms_data);
1000 igt_assert(wait_for_active());
1001 for (i = 0; i < buf_size; i++)
1002 igt_assert(gem_buf[i] == (~i & 0xFF));
1004 igt_assert(munmap(gem_buf, buf_size) == 0);
1006 /* Now the opposite: suspend, and try to create the mmap while
1008 disable_all_screens(&ms_data);
1009 igt_assert(wait_for_suspended());
1012 gem_buf = gem_mmap__gtt(drm_fd, handle, buf_size,
1013 PROT_READ | PROT_WRITE);
1015 gem_buf = gem_mmap__cpu(drm_fd, handle, buf_size, 0);
1017 igt_assert(wait_for_suspended());
1019 for (i = 0; i < buf_size; i++)
1020 gem_buf[i] = i & 0xFF;
1022 for (i = 0; i < buf_size; i++)
1023 igt_assert(gem_buf[i] == (i & 0xFF));
1025 igt_assert(wait_for_suspended());
1027 /* Resume and check if it's still there. */
1028 enable_one_screen(&ms_data);
1029 igt_assert(wait_for_active());
1030 for (i = 0; i < buf_size; i++)
1031 igt_assert(gem_buf[i] == (i & 0xFF));
1033 igt_assert(munmap(gem_buf, buf_size) == 0);
1034 gem_close(drm_fd, handle);
1037 static void gem_pread_subtest(void)
1041 int buf_size = 8192;
1042 uint8_t *cpu_buf, *read_buf;
1044 cpu_buf = malloc(buf_size);
1045 read_buf = malloc(buf_size);
1046 igt_assert(cpu_buf);
1047 igt_assert(read_buf);
1048 memset(cpu_buf, 0, buf_size);
1049 memset(read_buf, 0, buf_size);
1051 /* Create and set data while the device is active. */
1052 enable_one_screen(&ms_data);
1053 igt_assert(wait_for_active());
1055 handle = gem_create(drm_fd, buf_size);
1057 for (i = 0; i < buf_size; i++)
1058 cpu_buf[i] = i & 0xFF;
1060 gem_write(drm_fd, handle, 0, cpu_buf, buf_size);
1062 gem_read(drm_fd, handle, 0, read_buf, buf_size);
1064 for (i = 0; i < buf_size; i++)
1065 igt_assert(cpu_buf[i] == read_buf[i]);
1067 /* Now suspend, read and modify. */
1068 disable_all_screens(&ms_data);
1069 igt_assert(wait_for_suspended());
1071 memset(read_buf, 0, buf_size);
1072 gem_read(drm_fd, handle, 0, read_buf, buf_size);
1074 for (i = 0; i < buf_size; i++)
1075 igt_assert(cpu_buf[i] == read_buf[i]);
1076 igt_assert(wait_for_suspended());
1078 for (i = 0; i < buf_size; i++)
1079 cpu_buf[i] = (~i & 0xFF);
1080 gem_write(drm_fd, handle, 0, cpu_buf, buf_size);
1081 igt_assert(wait_for_suspended());
1083 /* Now resume and see if it's still there. */
1084 enable_one_screen(&ms_data);
1085 igt_assert(wait_for_active());
1087 memset(read_buf, 0, buf_size);
1088 gem_read(drm_fd, handle, 0, read_buf, buf_size);
1090 for (i = 0; i < buf_size; i++)
1091 igt_assert(cpu_buf[i] == read_buf[i]);
1093 gem_close(drm_fd, handle);
1099 /* Paints a square of color $color, size $width x $height, at position $x x $y
1100 * of $dst_handle, which contains pitch $pitch. */
1101 static void submit_blt_cmd(uint32_t dst_handle, uint16_t x, uint16_t y,
1102 uint16_t width, uint16_t height, uint32_t pitch,
1103 uint32_t color, uint32_t *presumed_dst_offset)
1106 uint32_t batch_handle;
1107 int batch_size = 8 * sizeof(uint32_t);
1108 uint32_t batch_buf[batch_size];
1109 struct drm_i915_gem_execbuffer2 execbuf = {};
1110 struct drm_i915_gem_exec_object2 objs[2] = {{}, {}};
1111 struct drm_i915_gem_relocation_entry relocs[1] = {{}};
1112 struct drm_i915_gem_wait gem_wait;
1116 if (intel_gen(ms_data.devid) >= 8)
1117 batch_buf[i++] = XY_COLOR_BLT_CMD_NOLEN |
1118 XY_COLOR_BLT_WRITE_ALPHA |
1119 XY_COLOR_BLT_WRITE_RGB | 0x5;
1121 batch_buf[i++] = XY_COLOR_BLT_CMD_NOLEN |
1122 XY_COLOR_BLT_WRITE_ALPHA |
1123 XY_COLOR_BLT_WRITE_RGB | 0x4;
1124 batch_buf[i++] = (3 << 24) | (0xF0 << 16) | (pitch);
1125 batch_buf[i++] = (y << 16) | x;
1126 batch_buf[i++] = ((y + height) << 16) | (x + width);
1128 batch_buf[i++] = *presumed_dst_offset;
1129 if (intel_gen(ms_data.devid) >= 8)
1131 batch_buf[i++] = color;
1133 batch_buf[i++] = MI_BATCH_BUFFER_END;
1134 if (intel_gen(ms_data.devid) < 8)
1135 batch_buf[i++] = MI_NOOP;
1137 igt_assert(i * sizeof(uint32_t) == batch_size);
1139 batch_handle = gem_create(drm_fd, batch_size);
1140 gem_write(drm_fd, batch_handle, 0, batch_buf, batch_size);
1142 relocs[0].target_handle = dst_handle;
1143 relocs[0].delta = 0;
1144 relocs[0].offset = reloc_pos * sizeof(uint32_t);
1145 relocs[0].presumed_offset = *presumed_dst_offset;
1146 relocs[0].read_domains = 0;
1147 relocs[0].write_domain = I915_GEM_DOMAIN_RENDER;
1149 objs[0].handle = dst_handle;
1150 objs[0].alignment = 64;
1152 objs[1].handle = batch_handle;
1153 objs[1].relocation_count = 1;
1154 objs[1].relocs_ptr = (uintptr_t)relocs;
1156 execbuf.buffers_ptr = (uintptr_t)objs;
1157 execbuf.buffer_count = 2;
1158 execbuf.batch_len = batch_size;
1159 execbuf.flags = I915_EXEC_BLT;
1160 i915_execbuffer2_set_context_id(execbuf, 0);
1162 do_ioctl(drm_fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &execbuf);
1164 *presumed_dst_offset = relocs[0].presumed_offset;
1167 gem_wait.timeout_ns = 10000000000LL; /* 10s */
1169 gem_wait.bo_handle = batch_handle;
1170 do_ioctl(drm_fd, DRM_IOCTL_I915_GEM_WAIT, &gem_wait);
1172 gem_wait.bo_handle = dst_handle;
1173 do_ioctl(drm_fd, DRM_IOCTL_I915_GEM_WAIT, &gem_wait);
1175 gem_close(drm_fd, batch_handle);
1178 /* Make sure we can submit a batch buffer and verify its result. */
1179 static void gem_execbuf_subtest(void)
1184 int pitch = 128 * bpp;
1185 int dst_size = 128 * 128 * bpp; /* 128x128 square */
1187 uint32_t presumed_offset = 0;
1188 int sq_x = 5, sq_y = 10, sq_w = 15, sq_h = 20;
1191 /* Create and set data while the device is active. */
1192 enable_one_screen(&ms_data);
1193 igt_assert(wait_for_active());
1195 handle = gem_create(drm_fd, dst_size);
1197 cpu_buf = malloc(dst_size);
1198 igt_assert(cpu_buf);
1199 memset(cpu_buf, 0, dst_size);
1200 gem_write(drm_fd, handle, 0, cpu_buf, dst_size);
1202 /* Now suspend and try it. */
1203 disable_all_screens(&ms_data);
1204 igt_assert(wait_for_suspended());
1207 submit_blt_cmd(handle, sq_x, sq_y, sq_w, sq_h, pitch, color,
1209 igt_assert(wait_for_suspended());
1211 gem_read(drm_fd, handle, 0, cpu_buf, dst_size);
1212 igt_assert(wait_for_suspended());
1213 for (y = 0; y < 128; y++) {
1214 for (x = 0; x < 128; x++) {
1215 uint32_t px = cpu_buf[y * 128 + x];
1217 if (y >= sq_y && y < (sq_y + sq_h) &&
1218 x >= sq_x && x < (sq_x + sq_w))
1219 igt_assert(px == color);
1221 igt_assert(px == 0);
1225 /* Now resume and check for it again. */
1226 enable_one_screen(&ms_data);
1227 igt_assert(wait_for_active());
1229 memset(cpu_buf, 0, dst_size);
1230 gem_read(drm_fd, handle, 0, cpu_buf, dst_size);
1231 for (y = 0; y < 128; y++) {
1232 for (x = 0; x < 128; x++) {
1233 uint32_t px = cpu_buf[y * 128 + x];
1235 if (y >= sq_y && y < (sq_y + sq_h) &&
1236 x >= sq_x && x < (sq_x + sq_w))
1237 igt_assert(px == color);
1239 igt_assert(px == 0);
1243 /* Now we'll do the opposite: do the blt while active, then read while
1244 * suspended. We use the same spot, but a different color. As a bonus,
1245 * we're testing the presumed_offset from the previous command. */
1247 submit_blt_cmd(handle, sq_x, sq_y, sq_w, sq_h, pitch, color,
1250 disable_all_screens(&ms_data);
1251 igt_assert(wait_for_suspended());
1253 memset(cpu_buf, 0, dst_size);
1254 gem_read(drm_fd, handle, 0, cpu_buf, dst_size);
1255 for (y = 0; y < 128; y++) {
1256 for (x = 0; x < 128; x++) {
1257 uint32_t px = cpu_buf[y * 128 + x];
1259 if (y >= sq_y && y < (sq_y + sq_h) &&
1260 x >= sq_x && x < (sq_x + sq_w))
1261 igt_assert(px == color);
1263 igt_assert(px == 0);
1267 gem_close(drm_fd, handle);
1272 /* Assuming execbuf already works, let's see what happens when we force many
1273 * suspend/resume cycles with commands. */
1274 static void gem_execbuf_stress_subtest(int rounds, int wait_flags)
1277 int batch_size = 4 * sizeof(uint32_t);
1278 uint32_t batch_buf[batch_size];
1280 struct drm_i915_gem_execbuffer2 execbuf = {};
1281 struct drm_i915_gem_exec_object2 objs[1] = {{}};
1283 if (wait_flags & WAIT_PC8_RES)
1284 igt_require(has_pc8);
1287 batch_buf[i++] = MI_NOOP;
1288 batch_buf[i++] = MI_NOOP;
1289 batch_buf[i++] = MI_BATCH_BUFFER_END;
1290 batch_buf[i++] = MI_NOOP;
1291 igt_assert(i * sizeof(uint32_t) == batch_size);
1293 disable_all_screens(&ms_data);
1294 igt_assert(wait_for_suspended());
1296 handle = gem_create(drm_fd, batch_size);
1297 gem_write(drm_fd, handle, 0, batch_buf, batch_size);
1299 objs[0].handle = handle;
1301 execbuf.buffers_ptr = (uintptr_t)objs;
1302 execbuf.buffer_count = 1;
1303 execbuf.batch_len = batch_size;
1304 execbuf.flags = I915_EXEC_RENDER;
1305 i915_execbuffer2_set_context_id(execbuf, 0);
1307 for (i = 0; i < rounds; i++) {
1308 do_ioctl(drm_fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &execbuf);
1310 if (wait_flags & WAIT_STATUS)
1311 igt_assert(wait_for_suspended());
1312 if (wait_flags & WAIT_PC8_RES)
1313 igt_assert(pc8_plus_residency_changed(120));
1314 if (wait_flags & WAIT_EXTRA)
1318 gem_close(drm_fd, handle);
1321 /* When this test was written, it triggered WARNs and DRM_ERRORs on dmesg. */
1322 static void gem_idle_subtest(void)
1324 disable_all_screens(&ms_data);
1325 igt_assert(wait_for_suspended());
1329 gem_quiescent_gpu(drm_fd);
1332 /* This also triggered WARNs on dmesg at some point. */
1333 static void reg_read_ioctl_subtest(void)
1335 struct drm_i915_reg_read rr = {
1336 .offset = 0x2358, /* render ring timestamp */
1339 disable_all_screens(&ms_data);
1340 igt_assert(wait_for_suspended());
1342 do_ioctl(drm_fd, DRM_IOCTL_I915_REG_READ, &rr);
1344 igt_assert(wait_for_suspended());
1347 static bool device_in_pci_d3(void)
1349 struct pci_device *pci_dev;
1353 pci_dev = intel_get_pci_device();
1355 rc = pci_device_cfg_read_u16(pci_dev, &val, 0xd4);
1356 igt_assert(rc == 0);
1358 return (val & 0x3) == 0x3;
1361 static void pci_d3_state_subtest(void)
1363 igt_require(has_runtime_pm);
1365 disable_all_screens(&ms_data);
1366 igt_assert(wait_for_suspended());
1368 igt_assert(device_in_pci_d3());
1370 enable_one_screen(&ms_data);
1371 igt_assert(wait_for_active());
1373 igt_assert(!device_in_pci_d3());
1376 static void stay_subtest(void)
1378 disable_all_screens(&ms_data);
1379 igt_assert(wait_for_suspended());
1385 static void system_suspend_subtest(void)
1387 disable_all_screens(&ms_data);
1388 igt_assert(wait_for_suspended());
1389 igt_system_suspend_autoresume();
1390 igt_assert(wait_for_suspended());
1393 /* Enable a screen, activate DPMS, then do a modeset. At some point our driver
1394 * produced WARNs on this case. */
1395 static void dpms_mode_unset_subtest(enum screen_type type)
1397 disable_all_screens(&ms_data);
1398 igt_assert(wait_for_suspended());
1400 igt_require(enable_one_screen_with_type(&ms_data, type));
1401 igt_assert(wait_for_active());
1403 disable_all_screens_dpms(&ms_data);
1404 igt_assert(wait_for_suspended());
1406 disable_all_screens(&ms_data);
1407 igt_assert(wait_for_suspended());
1413 static int opt_handler(int opt, int opt_index)
1429 int main(int argc, char *argv[])
1431 const char *help_str =
1432 " --quick\t\tMake the stress-tests not stressful, for quick regression testing.\n"
1433 " --stay\t\tDisable all screen and try to go into runtime pm. Useful for debugging.";
1434 static struct option long_options[] = {
1435 {"quick", 0, 0, 'q'},
1436 {"stay", 0, 0, 's'},
1440 igt_subtest_init_parse_opts(argc, argv, "", long_options,
1441 help_str, opt_handler);
1443 /* Skip instead of failing in case the machine is not prepared to reach
1444 * PC8+. We don't want bug reports from cases where the machine is just
1445 * not properly configured. */
1447 setup_environment();
1453 /* Essential things */
1456 igt_subtest("drm-resources-equal")
1457 drm_resources_equal_subtest();
1458 igt_subtest("pci-d3-state")
1459 pci_d3_state_subtest();
1462 igt_subtest("modeset-lpsp")
1463 modeset_subtest(SCREEN_TYPE_LPSP, 1, WAIT_STATUS);
1464 igt_subtest("modeset-non-lpsp")
1465 modeset_subtest(SCREEN_TYPE_NON_LPSP, 1, WAIT_STATUS);
1466 igt_subtest("dpms-lpsp")
1467 modeset_subtest(SCREEN_TYPE_LPSP, 1, WAIT_STATUS | USE_DPMS);
1468 igt_subtest("dpms-non-lpsp")
1469 modeset_subtest(SCREEN_TYPE_NON_LPSP, 1, WAIT_STATUS | USE_DPMS);
1472 igt_subtest("gem-mmap-cpu")
1473 gem_mmap_subtest(false);
1474 igt_subtest("gem-mmap-gtt")
1475 gem_mmap_subtest(true);
1476 igt_subtest("gem-pread")
1477 gem_pread_subtest();
1478 igt_subtest("gem-execbuf")
1479 gem_execbuf_subtest();
1480 igt_subtest("gem-idle")
1484 igt_subtest("reg-read-ioctl")
1485 reg_read_ioctl_subtest();
1488 igt_subtest("pc8-residency")
1489 pc8_residency_subtest();
1490 igt_subtest("debugfs-read")
1491 debugfs_read_subtest();
1492 igt_subtest("debugfs-forcewake-user")
1493 debugfs_forcewake_user_subtest();
1494 igt_subtest("sysfs-read")
1495 sysfs_read_subtest();
1496 igt_subtest("dpms-mode-unset-lpsp")
1497 dpms_mode_unset_subtest(SCREEN_TYPE_LPSP);
1498 igt_subtest("dpms-mode-unset-non-lpsp")
1499 dpms_mode_unset_subtest(SCREEN_TYPE_NON_LPSP);
1501 /* Modeset stress */
1502 igt_subtest("modeset-lpsp-stress")
1503 modeset_subtest(SCREEN_TYPE_LPSP, rounds, WAIT_STATUS);
1504 igt_subtest("modeset-non-lpsp-stress")
1505 modeset_subtest(SCREEN_TYPE_NON_LPSP, rounds, WAIT_STATUS);
1506 igt_subtest("modeset-lpsp-stress-no-wait")
1507 modeset_subtest(SCREEN_TYPE_LPSP, rounds, DONT_WAIT);
1508 igt_subtest("modeset-non-lpsp-stress-no-wait")
1509 modeset_subtest(SCREEN_TYPE_NON_LPSP, rounds, DONT_WAIT);
1510 igt_subtest("modeset-pc8-residency-stress")
1511 modeset_subtest(SCREEN_TYPE_ANY, rounds, WAIT_PC8_RES);
1512 igt_subtest("modeset-stress-extra-wait")
1513 modeset_subtest(SCREEN_TYPE_ANY, rounds,
1514 WAIT_STATUS | WAIT_EXTRA);
1516 /* System suspend */
1517 igt_subtest("system-suspend")
1518 system_suspend_subtest();
1521 igt_subtest("gem-execbuf-stress")
1522 gem_execbuf_stress_subtest(rounds, WAIT_STATUS);
1523 igt_subtest("gem-execbuf-stress-pc8")
1524 gem_execbuf_stress_subtest(rounds, WAIT_PC8_RES);
1525 igt_subtest("gem-execbuf-stress-extra-wait")
1526 gem_execbuf_stress_subtest(rounds, WAIT_STATUS | WAIT_EXTRA);
1529 teardown_environment();