igt_psr.c 4.55 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
/*
 * Copyright © 2018 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.
 */

24 25 26
#include "igt_psr.h"
#include "igt_sysfs.h"
#include <errno.h>
27

28
static bool psr_active_check(int debugfs_fd, enum psr_mode mode)
29
{
30
	char buf[PSR_STATUS_MAX_LEN];
31
	const char *state = mode == PSR_MODE_1 ? "SRDENT" : "DEEP_SLEEP";
32

33 34
	igt_debugfs_simple_read(debugfs_fd, "i915_edp_psr_status", buf,
				sizeof(buf));
35

36
	return strstr(buf, state);
37 38
}

39
static inline const char *psr_active_state_get(enum psr_mode mode)
40
{
41
	return mode == PSR_MODE_1 ? "SRDENT" : "DEEP_SLEEP";
42
}
43

44 45 46 47 48 49 50 51 52
/*
 * For PSR1, we wait until PSR is active. We wait until DEEP_SLEEP for PSR2.
 */
bool psr_wait_entry(int debugfs_fd, enum psr_mode mode)
{
	return igt_wait(psr_active_check(debugfs_fd, mode), 500, 20);
}

bool psr_wait_update(int debugfs_fd, enum psr_mode mode)
53
{
54
	return igt_wait(!psr_active_check(debugfs_fd, mode), 40, 10);
55 56
}

57
static ssize_t psr_write(int debugfs_fd, const char *buf)
58
{
59 60
	return igt_sysfs_write(debugfs_fd, "i915_edp_psr_debug", buf,
			       strlen(buf));
61 62
}

63
static int has_psr_debugfs(int debugfs_fd)
64 65 66 67 68 69 70 71
{
	int ret;

	/*
	 * Check if new PSR debugfs api is usable by writing an invalid value.
	 * Legacy mode will return OK here, debugfs api will return -EINVAL.
	 * -ENODEV is returned when PSR is unavailable.
	 */
72
	ret = psr_write(debugfs_fd, "0xf");
73 74 75 76 77 78
	if (ret == -EINVAL)
		return 0;
	else if (ret < 0)
		return ret;

	/* legacy debugfs api, we enabled irqs by writing, disable them. */
79
	psr_write(debugfs_fd, "0");
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
	return -EINVAL;
}

static bool psr_modparam_set(int val)
{
	static int oldval = -1;

	igt_set_module_param_int("enable_psr", val);

	if (val == oldval)
		return false;

	oldval = val;
	return true;
}

static int psr_restore_debugfs_fd = -1;

static void restore_psr_debugfs(int sig)
{
	psr_write(psr_restore_debugfs_fd, "0");
}

103
static bool psr_set(int debugfs_fd, enum psr_mode mode)
104 105 106
{
	int ret;

107
	ret = has_psr_debugfs(debugfs_fd);
108
	if (ret == -ENODEV) {
109
		igt_skip("PSR not available\n");
110 111 112 113
		return false;
	}

	if (ret == -EINVAL) {
114 115 116 117 118 119 120
		/*
		 * We can not control what PSR version is going to be enabled
		 * by setting enable_psr parameter, when unmatched the PSR
		 * version enabled and the PSR version of the test, it will
		 * fail in the first psr_wait_entry() of the test.
		 */
		ret = psr_modparam_set(mode <= PSR_MODE_2);
121
	} else {
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
		const char *debug_val;

		switch (mode) {
		case PSR_MODE_1:
			debug_val = "0x3";
			break;
		case PSR_MODE_2:
			debug_val = "0x2";
			break;
		default:
			/* Disables PSR */
			debug_val = "0x1";
		}

		ret = psr_write(debugfs_fd, debug_val);
137 138 139 140 141
		igt_assert(ret > 0);
	}

	/* Restore original value on exit */
	if (psr_restore_debugfs_fd == -1) {
142
		psr_restore_debugfs_fd = dup(debugfs_fd);
143 144 145 146 147 148 149
		igt_assert(psr_restore_debugfs_fd >= 0);
		igt_install_exit_handler(restore_psr_debugfs);
	}

	return ret;
}

150
bool psr_enable(int debugfs_fd, enum psr_mode mode)
151
{
152
	return psr_set(debugfs_fd, mode);
153 154
}

155
bool psr_disable(int debugfs_fd)
156
{
157 158
	/* Any mode different than PSR_MODE_1/2 will disable PSR */
	return psr_set(debugfs_fd, PSR_MODE_2 + 1);
159
}
160

161
bool psr_sink_support(int debugfs_fd, enum psr_mode mode)
162 163 164 165 166 167
{
	char buf[PSR_STATUS_MAX_LEN];
	int ret;

	ret = igt_debugfs_simple_read(debugfs_fd, "i915_edp_psr_status", buf,
				      sizeof(buf));
168 169 170 171 172 173 174 175 176 177 178 179
	if (ret < 1)
		return false;

	if (mode == PSR_MODE_1)
		return strstr(buf, "Sink_Support: yes\n") ||
		       strstr(buf, "Sink support: yes");
	else
		/*
		 * i915 requires PSR version 0x03 that is PSR2 + SU with
		 * Y-coordinate to support PSR2
		 */
		return strstr(buf, "Sink support: yes [0x03]");
180
}