1 /*
2 * Copyright © 2018 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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
21 * IN THE SOFTWARE.
22 */
23
24 #include "igt_psr.h"
25 #include "igt_sysfs.h"
26 #include <errno.h>
27
psr_active_check(int debugfs_fd,enum psr_mode mode)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 igt_debugfs_simple_read(debugfs_fd, "i915_edp_psr_status", buf,
34 sizeof(buf));
35
36 return strstr(buf, state);
37 }
38
psr_active_state_get(enum psr_mode mode)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 * For PSR1, we wait until PSR is active. We wait until DEEP_SLEEP for PSR2.
46 */
psr_wait_entry(int debugfs_fd,enum psr_mode mode)47 bool psr_wait_entry(int debugfs_fd, enum psr_mode mode)
48 {
49 return igt_wait(psr_active_check(debugfs_fd, mode), 500, 20);
50 }
51
psr_wait_update(int debugfs_fd,enum psr_mode mode)52 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
psr_long_wait_update(int debugfs_fd,enum psr_mode mode)57 bool psr_long_wait_update(int debugfs_fd, enum psr_mode mode)
58 {
59 return igt_wait(!psr_active_check(debugfs_fd, mode), 500, 10);
60 }
61
psr_write(int debugfs_fd,const char * buf)62 static ssize_t psr_write(int debugfs_fd, const char *buf)
63 {
64 return igt_sysfs_write(debugfs_fd, "i915_edp_psr_debug", buf,
65 strlen(buf));
66 }
67
has_psr_debugfs(int debugfs_fd)68 static int has_psr_debugfs(int debugfs_fd)
69 {
70 int ret;
71
72 /*
73 * Check if new PSR debugfs api is usable by writing an invalid value.
74 * Legacy mode will return OK here, debugfs api will return -EINVAL.
75 * -ENODEV is returned when PSR is unavailable.
76 */
77 ret = psr_write(debugfs_fd, "0xf");
78 if (ret == -EINVAL)
79 return 0;
80 else if (ret < 0)
81 return ret;
82
83 /* legacy debugfs api, we enabled irqs by writing, disable them. */
84 psr_write(debugfs_fd, "0");
85 return -EINVAL;
86 }
87
psr_modparam_set(int val)88 static bool psr_modparam_set(int val)
89 {
90 static int oldval = -1;
91
92 igt_set_module_param_int("enable_psr", val);
93
94 if (val == oldval)
95 return false;
96
97 oldval = val;
98 return true;
99 }
100
101 static int psr_restore_debugfs_fd = -1;
102
restore_psr_debugfs(int sig)103 static void restore_psr_debugfs(int sig)
104 {
105 psr_write(psr_restore_debugfs_fd, "0");
106 }
107
psr_set(int debugfs_fd,int mode)108 static bool psr_set(int debugfs_fd, int mode)
109 {
110 int ret;
111
112 ret = has_psr_debugfs(debugfs_fd);
113 if (ret == -ENODEV) {
114 igt_skip("PSR not available\n");
115 return false;
116 }
117
118 if (ret == -EINVAL) {
119 /*
120 * We can not control what PSR version is going to be enabled
121 * by setting enable_psr parameter, when unmatched the PSR
122 * version enabled and the PSR version of the test, it will
123 * fail in the first psr_wait_entry() of the test.
124 */
125 ret = psr_modparam_set(mode >= PSR_MODE_1);
126 } else {
127 const char *debug_val;
128
129 switch (mode) {
130 case PSR_MODE_1:
131 debug_val = "0x3";
132 break;
133 case PSR_MODE_2:
134 debug_val = "0x2";
135 break;
136 default:
137 /* Disables PSR */
138 debug_val = "0x1";
139 }
140
141 ret = psr_write(debugfs_fd, debug_val);
142 igt_assert(ret > 0);
143 }
144
145 /* Restore original value on exit */
146 if (psr_restore_debugfs_fd == -1) {
147 psr_restore_debugfs_fd = dup(debugfs_fd);
148 igt_assert(psr_restore_debugfs_fd >= 0);
149 igt_install_exit_handler(restore_psr_debugfs);
150 }
151
152 return ret;
153 }
154
psr_enable(int debugfs_fd,enum psr_mode mode)155 bool psr_enable(int debugfs_fd, enum psr_mode mode)
156 {
157 return psr_set(debugfs_fd, mode);
158 }
159
psr_disable(int debugfs_fd)160 bool psr_disable(int debugfs_fd)
161 {
162 /* Any mode different than PSR_MODE_1/2 will disable PSR */
163 return psr_set(debugfs_fd, -1);
164 }
165
psr_sink_support(int debugfs_fd,enum psr_mode mode)166 bool psr_sink_support(int debugfs_fd, enum psr_mode mode)
167 {
168 char buf[PSR_STATUS_MAX_LEN];
169 int ret;
170
171 ret = igt_debugfs_simple_read(debugfs_fd, "i915_edp_psr_status", buf,
172 sizeof(buf));
173 if (ret < 1)
174 return false;
175
176 if (mode == PSR_MODE_1)
177 return strstr(buf, "Sink_Support: yes\n") ||
178 strstr(buf, "Sink support: yes");
179 else
180 /*
181 * i915 requires PSR version 0x03 that is PSR2 + SU with
182 * Y-coordinate to support PSR2
183 */
184 return strstr(buf, "Sink support: yes [0x03]");
185 }
186
187 #define PSR2_SU_BLOCK_STR_LOOKUP "PSR2 SU blocks:\n0\t"
188
189 static bool
psr2_read_last_num_su_blocks_val(int debugfs_fd,uint16_t * num_su_blocks)190 psr2_read_last_num_su_blocks_val(int debugfs_fd, uint16_t *num_su_blocks)
191 {
192 char buf[PSR_STATUS_MAX_LEN];
193 char *str;
194 int ret;
195
196 ret = igt_debugfs_simple_read(debugfs_fd, "i915_edp_psr_status", buf,
197 sizeof(buf));
198 if (ret < 0)
199 return false;
200
201 str = strstr(buf, PSR2_SU_BLOCK_STR_LOOKUP);
202 if (!str)
203 return false;
204
205 str = &str[strlen(PSR2_SU_BLOCK_STR_LOOKUP)];
206 *num_su_blocks = (uint16_t)strtol(str, NULL, 10);
207
208 return true;
209 }
210
psr2_wait_su(int debugfs_fd,uint16_t * num_su_blocks)211 bool psr2_wait_su(int debugfs_fd, uint16_t *num_su_blocks)
212 {
213 return igt_wait(psr2_read_last_num_su_blocks_val(debugfs_fd, num_su_blocks), 40, 1);
214 }
215