igt_psr.c 3.56 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(int debugfs_fd, bool check_active)
29 30
{
	bool active;
31
	char buf[PSR_STATUS_MAX_LEN];
32

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

36
	active = strstr(buf, "SRDENT") || strstr(buf, "DEEP_SLEEP");
37 38 39
	return check_active ? active : !active;
}

40
bool psr_wait_entry(int debugfs_fd)
41
{
42
	return igt_wait(psr_active(debugfs_fd, true), 500, 20);
43
}
44

45 46 47 48 49
bool psr_wait_exit(int debugfs_fd)
{
	return igt_wait(psr_active(debugfs_fd, false), 40, 10);
}

50
static ssize_t psr_write(int debugfs_fd, const char *buf)
51
{
52 53
	return igt_sysfs_write(debugfs_fd, "i915_edp_psr_debug", buf,
			       strlen(buf));
54 55
}

56
static int has_psr_debugfs(int debugfs_fd)
57 58 59 60 61 62 63 64
{
	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.
	 */
65
	ret = psr_write(debugfs_fd, "0xf");
66 67 68 69 70 71
	if (ret == -EINVAL)
		return 0;
	else if (ret < 0)
		return ret;

	/* legacy debugfs api, we enabled irqs by writing, disable them. */
72
	psr_write(debugfs_fd, "0");
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
	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");
}

96
static bool psr_set(int debugfs_fd, bool enable)
97 98 99
{
	int ret;

100
	ret = has_psr_debugfs(debugfs_fd);
101 102 103 104 105 106 107 108
	if (ret == -ENODEV) {
		igt_skip_on_f(enable, "PSR not available\n");
		return false;
	}

	if (ret == -EINVAL) {
		ret = psr_modparam_set(enable);
	} else {
109
		ret = psr_write(debugfs_fd, enable ? "0x3" : "0x1");
110 111 112 113 114
		igt_assert(ret > 0);
	}

	/* Restore original value on exit */
	if (psr_restore_debugfs_fd == -1) {
115
		psr_restore_debugfs_fd = dup(debugfs_fd);
116 117 118 119 120 121 122
		igt_assert(psr_restore_debugfs_fd >= 0);
		igt_install_exit_handler(restore_psr_debugfs);
	}

	return ret;
}

123
bool psr_enable(int debugfs_fd)
124
{
125
	return psr_set(debugfs_fd, true);
126 127
}

128
bool psr_disable(int debugfs_fd)
129
{
130
	return psr_set(debugfs_fd, false);
131
}
132 133 134 135 136 137 138 139

bool psr_sink_support(int debugfs_fd)
{
	char buf[PSR_STATUS_MAX_LEN];
	int ret;

	ret = igt_debugfs_simple_read(debugfs_fd, "i915_edp_psr_status", buf,
				      sizeof(buf));
140 141
	return ret > 0 && (strstr(buf, "Sink_Support: yes\n") ||
			   strstr(buf, "Sink support: yes"));
142
}