xref: /linux/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_debugfs.c (revision 43b9f0f18693c7f7b75613f3aeae25fa2b4e2f76)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright 2018 Advanced Micro Devices, Inc.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the 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 COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21  * OTHER DEALINGS IN THE SOFTWARE.
22  *
23  * Authors: AMD
24  *
25  */
26 
27 #include <linux/string_helpers.h>
28 #include <linux/uaccess.h>
29 #include <media/cec-notifier.h>
30 
31 #include "dc.h"
32 #include "amdgpu.h"
33 #include "amdgpu_dm.h"
34 #include "amdgpu_dm_debugfs.h"
35 #include "amdgpu_dm_replay.h"
36 #include "amdgpu_dm_psr.h"
37 #include "dm_helpers.h"
38 #include "dmub/dmub_srv.h"
39 #include "resource.h"
40 #include "dsc.h"
41 #include "link_hwss.h"
42 #include "dc/dc_dmub_srv.h"
43 #include "link/protocols/link_dp_capability.h"
44 #include "inc/hw/dchubbub.h"
45 
46 #ifdef CONFIG_DRM_AMD_SECURE_DISPLAY
47 #include "amdgpu_dm_psr.h"
48 #endif
49 
50 #define MULTIPLIER_TO_LR 270000
51 struct dmub_debugfs_trace_header {
52 	uint32_t entry_count;
53 	uint32_t reserved[3];
54 };
55 
56 struct dmub_debugfs_trace_entry {
57 	uint32_t trace_code;
58 	uint32_t tick_count;
59 	uint32_t param0;
60 	uint32_t param1;
61 };
62 
63 static const char *const mst_progress_status[] = {
64 	"probe",
65 	"remote_edid",
66 	"allocate_new_payload",
67 	"clear_allocated_payload",
68 };
69 
70 /* parse_write_buffer_into_params - Helper function to parse debugfs write buffer into an array
71  *
72  * Function takes in attributes passed to debugfs write entry
73  * and writes into param array.
74  * The user passes max_param_num to identify maximum number of
75  * parameters that could be parsed.
76  *
77  */
78 static int parse_write_buffer_into_params(char *wr_buf, uint32_t wr_buf_size,
79 					  long *param, const char __user *buf,
80 					  int max_param_num,
81 					  uint8_t *param_nums)
82 {
83 	char *wr_buf_ptr = NULL;
84 	uint32_t wr_buf_count = 0;
85 	int r;
86 	char *sub_str = NULL;
87 	const char delimiter[3] = {' ', '\n', '\0'};
88 	uint8_t param_index = 0;
89 
90 	*param_nums = 0;
91 
92 	wr_buf_ptr = wr_buf;
93 
94 	/* r is bytes not be copied */
95 	if (copy_from_user(wr_buf_ptr, buf, wr_buf_size)) {
96 		DRM_DEBUG_DRIVER("user data could not be read successfully\n");
97 		return -EFAULT;
98 	}
99 
100 	/* check number of parameters. isspace could not differ space and\n */
101 	while ((*wr_buf_ptr != 0xa) && (wr_buf_count < wr_buf_size)) {
102 		/* skip space*/
103 		while (isspace(*wr_buf_ptr) && (wr_buf_count < wr_buf_size)) {
104 			wr_buf_ptr++;
105 			wr_buf_count++;
106 			}
107 
108 		if (wr_buf_count == wr_buf_size)
109 			break;
110 
111 		/* skip non-space*/
112 		while ((!isspace(*wr_buf_ptr)) && (wr_buf_count < wr_buf_size)) {
113 			wr_buf_ptr++;
114 			wr_buf_count++;
115 		}
116 
117 		(*param_nums)++;
118 
119 		if (wr_buf_count == wr_buf_size)
120 			break;
121 	}
122 
123 	if (*param_nums > max_param_num)
124 		*param_nums = max_param_num;
125 
126 	wr_buf_ptr = wr_buf; /* reset buf pointer */
127 	wr_buf_count = 0; /* number of char already checked */
128 
129 	while (isspace(*wr_buf_ptr) && (wr_buf_count < wr_buf_size)) {
130 		wr_buf_ptr++;
131 		wr_buf_count++;
132 	}
133 
134 	while (param_index < *param_nums) {
135 		/* after strsep, wr_buf_ptr will be moved to after space */
136 		sub_str = strsep(&wr_buf_ptr, delimiter);
137 
138 		r = kstrtol(sub_str, 16, &(param[param_index]));
139 
140 		if (r)
141 			DRM_DEBUG_DRIVER("string to int convert error code: %d\n", r);
142 
143 		param_index++;
144 	}
145 
146 	return 0;
147 }
148 
149 /* function description
150  * get/ set DP configuration: lane_count, link_rate, spread_spectrum
151  *
152  * valid lane count value: 1, 2, 4
153  * valid link rate value:
154  * 06h = 1.62Gbps per lane
155  * 0Ah = 2.7Gbps per lane
156  * 0Ch = 3.24Gbps per lane
157  * 14h = 5.4Gbps per lane
158  * 1Eh = 8.1Gbps per lane
159  *
160  * debugfs is located at /sys/kernel/debug/dri/0/DP-x/link_settings
161  *
162  * --- to get dp configuration
163  *
164  * cat /sys/kernel/debug/dri/0/DP-x/link_settings
165  *
166  * It will list current, verified, reported, preferred dp configuration.
167  * current -- for current video mode
168  * verified --- maximum configuration which pass link training
169  * reported --- DP rx report caps (DPCD register offset 0, 1 2)
170  * preferred --- user force settings
171  *
172  * --- set (or force) dp configuration
173  *
174  * echo <lane_count>  <link_rate> > link_settings
175  *
176  * for example, to force to  2 lane, 2.7GHz,
177  * echo 4 0xa > /sys/kernel/debug/dri/0/DP-x/link_settings
178  *
179  * spread_spectrum could not be changed dynamically.
180  *
181  * in case invalid lane count, link rate are force, no hw programming will be
182  * done. please check link settings after force operation to see if HW get
183  * programming.
184  *
185  * cat /sys/kernel/debug/dri/0/DP-x/link_settings
186  *
187  * check current and preferred settings.
188  *
189  */
190 static ssize_t dp_link_settings_read(struct file *f, char __user *buf,
191 				 size_t size, loff_t *pos)
192 {
193 	struct amdgpu_dm_connector *connector = file_inode(f)->i_private;
194 	struct dc_link *link = connector->dc_link;
195 	char *rd_buf = NULL;
196 	char *rd_buf_ptr = NULL;
197 	const uint32_t rd_buf_size = 100;
198 	uint32_t result = 0;
199 	int r;
200 
201 	if (*pos & 3 || size & 3)
202 		return -EINVAL;
203 
204 	rd_buf = kcalloc(rd_buf_size, sizeof(char), GFP_KERNEL);
205 	if (!rd_buf)
206 		return 0;
207 
208 	rd_buf_ptr = rd_buf;
209 
210 	rd_buf_ptr += scnprintf(rd_buf_ptr, rd_buf_size - (rd_buf_ptr - rd_buf),
211 			"Current:  %d  0x%x  %d  ",
212 			link->cur_link_settings.lane_count,
213 			link->cur_link_settings.link_rate,
214 			link->cur_link_settings.link_spread);
215 
216 	rd_buf_ptr += scnprintf(rd_buf_ptr, rd_buf_size - (rd_buf_ptr - rd_buf),
217 			"Verified:  %d  0x%x  %d  ",
218 			link->verified_link_cap.lane_count,
219 			link->verified_link_cap.link_rate,
220 			link->verified_link_cap.link_spread);
221 
222 	rd_buf_ptr += scnprintf(rd_buf_ptr, rd_buf_size - (rd_buf_ptr - rd_buf),
223 			"Reported:  %d  0x%x  %d  ",
224 			link->reported_link_cap.lane_count,
225 			link->reported_link_cap.link_rate,
226 			link->reported_link_cap.link_spread);
227 
228 	rd_buf_ptr += scnprintf(rd_buf_ptr, rd_buf_size - (rd_buf_ptr - rd_buf),
229 			"Preferred:  %d  0x%x  %d\n",
230 			link->preferred_link_setting.lane_count,
231 			link->preferred_link_setting.link_rate,
232 			link->preferred_link_setting.link_spread);
233 
234 	while (size) {
235 		if (*pos >= rd_buf_size)
236 			break;
237 
238 		r = put_user(*(rd_buf + result), buf);
239 		if (r) {
240 			kfree(rd_buf);
241 			return r; /* r = -EFAULT */
242 		}
243 
244 		buf += 1;
245 		size -= 1;
246 		*pos += 1;
247 		result += 1;
248 	}
249 
250 	kfree(rd_buf);
251 	return result;
252 }
253 
254 static ssize_t dp_link_settings_write(struct file *f, const char __user *buf,
255 				 size_t size, loff_t *pos)
256 {
257 	struct amdgpu_dm_connector *connector = file_inode(f)->i_private;
258 	struct dc_link *link = connector->dc_link;
259 	struct amdgpu_device *adev = drm_to_adev(connector->base.dev);
260 	struct dc *dc = (struct dc *)link->dc;
261 	struct dc_link_settings prefer_link_settings = {0};
262 	char *wr_buf = NULL;
263 	const uint32_t wr_buf_size = 40;
264 	/* 0: lane_count; 1: link_rate */
265 	int max_param_num = 2;
266 	uint8_t param_nums = 0;
267 	long param[2];
268 	bool valid_input = true;
269 
270 	if (size == 0)
271 		return -EINVAL;
272 
273 	wr_buf = kcalloc(wr_buf_size, sizeof(char), GFP_KERNEL);
274 	if (!wr_buf)
275 		return -ENOSPC;
276 
277 	if (parse_write_buffer_into_params(wr_buf, wr_buf_size,
278 					   (long *)param, buf,
279 					   max_param_num,
280 					   &param_nums)) {
281 		kfree(wr_buf);
282 		return -EINVAL;
283 	}
284 
285 	if (param_nums <= 0) {
286 		kfree(wr_buf);
287 		DRM_DEBUG_DRIVER("user data not be read\n");
288 		return -EINVAL;
289 	}
290 
291 	switch (param[0]) {
292 	case LANE_COUNT_ONE:
293 	case LANE_COUNT_TWO:
294 	case LANE_COUNT_FOUR:
295 		break;
296 	default:
297 		valid_input = false;
298 		break;
299 	}
300 
301 	switch (param[1]) {
302 	case LINK_RATE_LOW:
303 	case LINK_RATE_RATE_2:
304 	case LINK_RATE_RATE_3:
305 	case LINK_RATE_HIGH:
306 	case LINK_RATE_RBR2:
307 	case LINK_RATE_RATE_6:
308 	case LINK_RATE_HIGH2:
309 	case LINK_RATE_HIGH3:
310 	case LINK_RATE_UHBR10:
311 	case LINK_RATE_UHBR13_5:
312 	case LINK_RATE_UHBR20:
313 		break;
314 	default:
315 		valid_input = false;
316 		break;
317 	}
318 
319 	if (!valid_input) {
320 		kfree(wr_buf);
321 		DRM_DEBUG_DRIVER("Invalid Input value No HW will be programmed\n");
322 		mutex_lock(&adev->dm.dc_lock);
323 		dc_link_set_preferred_training_settings(dc, NULL, NULL, link, false);
324 		mutex_unlock(&adev->dm.dc_lock);
325 		return size;
326 	}
327 
328 	/* save user force lane_count, link_rate to preferred settings
329 	 * spread spectrum will not be changed
330 	 */
331 	prefer_link_settings.link_spread = link->cur_link_settings.link_spread;
332 	prefer_link_settings.use_link_rate_set = false;
333 	prefer_link_settings.lane_count = param[0];
334 	prefer_link_settings.link_rate = param[1];
335 
336 	mutex_lock(&adev->dm.dc_lock);
337 	dc_link_set_preferred_training_settings(dc, &prefer_link_settings, NULL, link, false);
338 	mutex_unlock(&adev->dm.dc_lock);
339 
340 	kfree(wr_buf);
341 	return size;
342 }
343 
344 static bool dp_mst_is_end_device(struct amdgpu_dm_connector *aconnector)
345 {
346 	bool is_end_device = false;
347 	struct drm_dp_mst_topology_mgr *mgr = NULL;
348 	struct drm_dp_mst_port *port = NULL;
349 
350 	if (aconnector->mst_root && aconnector->mst_root->mst_mgr.mst_state) {
351 		mgr = &aconnector->mst_root->mst_mgr;
352 		port = aconnector->mst_output_port;
353 
354 		drm_modeset_lock(&mgr->base.lock, NULL);
355 		if (port->pdt == DP_PEER_DEVICE_SST_SINK ||
356 			port->pdt == DP_PEER_DEVICE_DP_LEGACY_CONV)
357 			is_end_device = true;
358 		drm_modeset_unlock(&mgr->base.lock);
359 	}
360 
361 	return is_end_device;
362 }
363 
364 /* Change MST link setting
365  *
366  * valid lane count value: 1, 2, 4
367  * valid link rate value:
368  * 06h = 1.62Gbps per lane
369  * 0Ah = 2.7Gbps per lane
370  * 0Ch = 3.24Gbps per lane
371  * 14h = 5.4Gbps per lane
372  * 1Eh = 8.1Gbps per lane
373  * 3E8h = 10.0Gbps per lane
374  * 546h = 13.5Gbps per lane
375  * 7D0h = 20.0Gbps per lane
376  *
377  * debugfs is located at /sys/kernel/debug/dri/0/DP-x/mst_link_settings
378  *
379  * for example, to force to  2 lane, 10.0GHz,
380  * echo 2 0x3e8 > /sys/kernel/debug/dri/0/DP-x/mst_link_settings
381  *
382  * Valid input will trigger hotplug event to get new link setting applied
383  * Invalid input will trigger training setting reset
384  *
385  * The usage can be referred to link_settings entry
386  *
387  */
388 static ssize_t dp_mst_link_setting(struct file *f, const char __user *buf,
389 				 size_t size, loff_t *pos)
390 {
391 	struct amdgpu_dm_connector *aconnector = file_inode(f)->i_private;
392 	struct dc_link *link = aconnector->dc_link;
393 	struct amdgpu_device *adev = drm_to_adev(aconnector->base.dev);
394 	struct dc *dc = (struct dc *)link->dc;
395 	struct dc_link_settings prefer_link_settings = {0};
396 	char *wr_buf = NULL;
397 	const uint32_t wr_buf_size = 40;
398 	/* 0: lane_count; 1: link_rate */
399 	int max_param_num = 2;
400 	uint8_t param_nums = 0;
401 	long param[2];
402 	bool valid_input = true;
403 
404 	if (!dp_mst_is_end_device(aconnector))
405 		return -EINVAL;
406 
407 	if (size == 0)
408 		return -EINVAL;
409 
410 	wr_buf = kcalloc(wr_buf_size, sizeof(char), GFP_KERNEL);
411 	if (!wr_buf)
412 		return -ENOSPC;
413 
414 	if (parse_write_buffer_into_params(wr_buf, wr_buf_size,
415 					   (long *)param, buf,
416 					   max_param_num,
417 					   &param_nums)) {
418 		kfree(wr_buf);
419 		return -EINVAL;
420 	}
421 
422 	if (param_nums <= 0) {
423 		kfree(wr_buf);
424 		DRM_DEBUG_DRIVER("user data not be read\n");
425 		return -EINVAL;
426 	}
427 
428 	switch (param[0]) {
429 	case LANE_COUNT_ONE:
430 	case LANE_COUNT_TWO:
431 	case LANE_COUNT_FOUR:
432 		break;
433 	default:
434 		valid_input = false;
435 		break;
436 	}
437 
438 	switch (param[1]) {
439 	case LINK_RATE_LOW:
440 	case LINK_RATE_HIGH:
441 	case LINK_RATE_RBR2:
442 	case LINK_RATE_HIGH2:
443 	case LINK_RATE_HIGH3:
444 	case LINK_RATE_UHBR10:
445 	case LINK_RATE_UHBR13_5:
446 	case LINK_RATE_UHBR20:
447 		break;
448 	default:
449 		valid_input = false;
450 		break;
451 	}
452 
453 	if (!valid_input) {
454 		kfree(wr_buf);
455 		DRM_DEBUG_DRIVER("Invalid Input value No HW will be programmed\n");
456 		mutex_lock(&adev->dm.dc_lock);
457 		dc_link_set_preferred_training_settings(dc, NULL, NULL, link, false);
458 		mutex_unlock(&adev->dm.dc_lock);
459 		return -EINVAL;
460 	}
461 
462 	/* save user force lane_count, link_rate to preferred settings
463 	 * spread spectrum will not be changed
464 	 */
465 	prefer_link_settings.link_spread = link->cur_link_settings.link_spread;
466 	prefer_link_settings.use_link_rate_set = false;
467 	prefer_link_settings.lane_count = param[0];
468 	prefer_link_settings.link_rate = param[1];
469 
470 	/* skip immediate retrain, and train to new link setting after hotplug event triggered */
471 	mutex_lock(&adev->dm.dc_lock);
472 	dc_link_set_preferred_training_settings(dc, &prefer_link_settings, NULL, link, true);
473 	mutex_unlock(&adev->dm.dc_lock);
474 
475 	mutex_lock(&aconnector->base.dev->mode_config.mutex);
476 	aconnector->base.force = DRM_FORCE_OFF;
477 	mutex_unlock(&aconnector->base.dev->mode_config.mutex);
478 	drm_kms_helper_hotplug_event(aconnector->base.dev);
479 
480 	msleep(100);
481 
482 	mutex_lock(&aconnector->base.dev->mode_config.mutex);
483 	aconnector->base.force = DRM_FORCE_UNSPECIFIED;
484 	mutex_unlock(&aconnector->base.dev->mode_config.mutex);
485 	drm_kms_helper_hotplug_event(aconnector->base.dev);
486 
487 	kfree(wr_buf);
488 	return size;
489 }
490 
491 /* function: get current DP PHY settings: voltage swing, pre-emphasis,
492  * post-cursor2 (defined by VESA DP specification)
493  *
494  * valid values
495  * voltage swing: 0,1,2,3
496  * pre-emphasis : 0,1,2,3
497  * post cursor2 : 0,1,2,3
498  *
499  *
500  * how to use this debugfs
501  *
502  * debugfs is located at /sys/kernel/debug/dri/0/DP-x
503  *
504  * there will be directories, like DP-1, DP-2,DP-3, etc. for DP display
505  *
506  * To figure out which DP-x is the display for DP to be check,
507  * cd DP-x
508  * ls -ll
509  * There should be debugfs file, like link_settings, phy_settings.
510  * cat link_settings
511  * from lane_count, link_rate to figure which DP-x is for display to be worked
512  * on
513  *
514  * To get current DP PHY settings,
515  * cat phy_settings
516  *
517  * To change DP PHY settings,
518  * echo <voltage_swing> <pre-emphasis> <post_cursor2> > phy_settings
519  * for examle, to change voltage swing to 2, pre-emphasis to 3, post_cursor2 to
520  * 0,
521  * echo 2 3 0 > phy_settings
522  *
523  * To check if change be applied, get current phy settings by
524  * cat phy_settings
525  *
526  * In case invalid values are set by user, like
527  * echo 1 4 0 > phy_settings
528  *
529  * HW will NOT be programmed by these settings.
530  * cat phy_settings will show the previous valid settings.
531  */
532 static ssize_t dp_phy_settings_read(struct file *f, char __user *buf,
533 				 size_t size, loff_t *pos)
534 {
535 	struct amdgpu_dm_connector *connector = file_inode(f)->i_private;
536 	struct dc_link *link = connector->dc_link;
537 	char *rd_buf = NULL;
538 	const uint32_t rd_buf_size = 20;
539 	uint32_t result = 0;
540 	int r;
541 
542 	if (*pos & 3 || size & 3)
543 		return -EINVAL;
544 
545 	rd_buf = kcalloc(rd_buf_size, sizeof(char), GFP_KERNEL);
546 	if (!rd_buf)
547 		return -EINVAL;
548 
549 	snprintf(rd_buf, rd_buf_size, "  %d  %d  %d\n",
550 			link->cur_lane_setting[0].VOLTAGE_SWING,
551 			link->cur_lane_setting[0].PRE_EMPHASIS,
552 			link->cur_lane_setting[0].POST_CURSOR2);
553 
554 	while (size) {
555 		if (*pos >= rd_buf_size)
556 			break;
557 
558 		r = put_user((*(rd_buf + result)), buf);
559 		if (r) {
560 			kfree(rd_buf);
561 			return r; /* r = -EFAULT */
562 		}
563 
564 		buf += 1;
565 		size -= 1;
566 		*pos += 1;
567 		result += 1;
568 	}
569 
570 	kfree(rd_buf);
571 	return result;
572 }
573 
574 static int dp_lttpr_status_show(struct seq_file *m, void *unused)
575 {
576 	struct drm_connector *connector = m->private;
577 	struct amdgpu_dm_connector *aconnector =
578 		to_amdgpu_dm_connector(connector);
579 	struct dc_lttpr_caps caps = aconnector->dc_link->dpcd_caps.lttpr_caps;
580 
581 	if (connector->status != connector_status_connected)
582 		return -ENODEV;
583 
584 	seq_printf(m, "phy repeater count: %u (raw: 0x%x)\n",
585 		   dp_parse_lttpr_repeater_count(caps.phy_repeater_cnt),
586 		   caps.phy_repeater_cnt);
587 
588 	seq_puts(m, "phy repeater mode: ");
589 
590 	switch (caps.mode) {
591 	case DP_PHY_REPEATER_MODE_TRANSPARENT:
592 		seq_puts(m, "transparent");
593 		break;
594 	case DP_PHY_REPEATER_MODE_NON_TRANSPARENT:
595 		seq_puts(m, "non-transparent");
596 		break;
597 	case 0x00:
598 		seq_puts(m, "non lttpr");
599 		break;
600 	default:
601 		seq_printf(m, "read error (raw: 0x%x)", caps.mode);
602 		break;
603 	}
604 
605 	seq_putc(m, '\n');
606 	return 0;
607 }
608 
609 static ssize_t dp_phy_settings_write(struct file *f, const char __user *buf,
610 				 size_t size, loff_t *pos)
611 {
612 	struct amdgpu_dm_connector *connector = file_inode(f)->i_private;
613 	struct dc_link *link = connector->dc_link;
614 	struct dc *dc = (struct dc *)link->dc;
615 	char *wr_buf = NULL;
616 	uint32_t wr_buf_size = 40;
617 	long param[3];
618 	bool use_prefer_link_setting;
619 	struct link_training_settings link_lane_settings = {0};
620 	int max_param_num = 3;
621 	uint8_t param_nums = 0;
622 	int r = 0;
623 
624 
625 	if (size == 0)
626 		return -EINVAL;
627 
628 	wr_buf = kcalloc(wr_buf_size, sizeof(char), GFP_KERNEL);
629 	if (!wr_buf)
630 		return -ENOSPC;
631 
632 	if (parse_write_buffer_into_params(wr_buf, wr_buf_size,
633 					   (long *)param, buf,
634 					   max_param_num,
635 					   &param_nums)) {
636 		kfree(wr_buf);
637 		return -EINVAL;
638 	}
639 
640 	if (param_nums <= 0) {
641 		kfree(wr_buf);
642 		DRM_DEBUG_DRIVER("user data not be read\n");
643 		return -EINVAL;
644 	}
645 
646 	if ((param[0] > VOLTAGE_SWING_MAX_LEVEL) ||
647 			(param[1] > PRE_EMPHASIS_MAX_LEVEL) ||
648 			(param[2] > POST_CURSOR2_MAX_LEVEL)) {
649 		kfree(wr_buf);
650 		DRM_DEBUG_DRIVER("Invalid Input No HW will be programmed\n");
651 		return size;
652 	}
653 
654 	/* get link settings: lane count, link rate */
655 	use_prefer_link_setting =
656 		((link->preferred_link_setting.link_rate != LINK_RATE_UNKNOWN) &&
657 		(link->test_pattern_enabled));
658 
659 	memset(&link_lane_settings, 0, sizeof(link_lane_settings));
660 
661 	if (use_prefer_link_setting) {
662 		link_lane_settings.link_settings.lane_count =
663 				link->preferred_link_setting.lane_count;
664 		link_lane_settings.link_settings.link_rate =
665 				link->preferred_link_setting.link_rate;
666 		link_lane_settings.link_settings.link_spread =
667 				link->preferred_link_setting.link_spread;
668 	} else {
669 		link_lane_settings.link_settings.lane_count =
670 				link->cur_link_settings.lane_count;
671 		link_lane_settings.link_settings.link_rate =
672 				link->cur_link_settings.link_rate;
673 		link_lane_settings.link_settings.link_spread =
674 				link->cur_link_settings.link_spread;
675 	}
676 
677 	/* apply phy settings from user */
678 	for (r = 0; r < link_lane_settings.link_settings.lane_count; r++) {
679 		link_lane_settings.hw_lane_settings[r].VOLTAGE_SWING =
680 				(enum dc_voltage_swing) (param[0]);
681 		link_lane_settings.hw_lane_settings[r].PRE_EMPHASIS =
682 				(enum dc_pre_emphasis) (param[1]);
683 		link_lane_settings.hw_lane_settings[r].POST_CURSOR2 =
684 				(enum dc_post_cursor2) (param[2]);
685 	}
686 
687 	/* program ASIC registers and DPCD registers */
688 	dc_link_set_drive_settings(dc, &link_lane_settings, link);
689 
690 	kfree(wr_buf);
691 	return size;
692 }
693 
694 /* function description
695  *
696  * set PHY layer or Link layer test pattern
697  * PHY test pattern is used for PHY SI check.
698  * Link layer test will not affect PHY SI.
699  *
700  * Reset Test Pattern:
701  * 0 = DP_TEST_PATTERN_VIDEO_MODE
702  *
703  * PHY test pattern supported:
704  * 1 = DP_TEST_PATTERN_D102
705  * 2 = DP_TEST_PATTERN_SYMBOL_ERROR
706  * 3 = DP_TEST_PATTERN_PRBS7
707  * 4 = DP_TEST_PATTERN_80BIT_CUSTOM
708  * 5 = DP_TEST_PATTERN_CP2520_1
709  * 6 = DP_TEST_PATTERN_CP2520_2 = DP_TEST_PATTERN_HBR2_COMPLIANCE_EYE
710  * 7 = DP_TEST_PATTERN_CP2520_3
711  *
712  * DP PHY Link Training Patterns
713  * 8 = DP_TEST_PATTERN_TRAINING_PATTERN1
714  * 9 = DP_TEST_PATTERN_TRAINING_PATTERN2
715  * a = DP_TEST_PATTERN_TRAINING_PATTERN3
716  * b = DP_TEST_PATTERN_TRAINING_PATTERN4
717  *
718  * DP Link Layer Test pattern
719  * c = DP_TEST_PATTERN_COLOR_SQUARES
720  * d = DP_TEST_PATTERN_COLOR_SQUARES_CEA
721  * e = DP_TEST_PATTERN_VERTICAL_BARS
722  * f = DP_TEST_PATTERN_HORIZONTAL_BARS
723  * 10= DP_TEST_PATTERN_COLOR_RAMP
724  *
725  * debugfs phy_test_pattern is located at /syskernel/debug/dri/0/DP-x
726  *
727  * --- set test pattern
728  * echo <test pattern #> > test_pattern
729  *
730  * If test pattern # is not supported, NO HW programming will be done.
731  * for DP_TEST_PATTERN_80BIT_CUSTOM, it needs extra 10 bytes of data
732  * for the user pattern. input 10 bytes data are separated by space
733  *
734  * echo 0x4 0x11 0x22 0x33 0x44 0x55 0x66 0x77 0x88 0x99 0xaa > test_pattern
735  *
736  * --- reset test pattern
737  * echo 0 > test_pattern
738  *
739  * --- HPD detection is disabled when set PHY test pattern
740  *
741  * when PHY test pattern (pattern # within [1,7]) is set, HPD pin of HW ASIC
742  * is disable. User could unplug DP display from DP connected and plug scope to
743  * check test pattern PHY SI.
744  * If there is need unplug scope and plug DP display back, do steps below:
745  * echo 0 > phy_test_pattern
746  * unplug scope
747  * plug DP display.
748  *
749  * "echo 0 > phy_test_pattern" will re-enable HPD pin again so that video sw
750  * driver could detect "unplug scope" and "plug DP display"
751  */
752 static ssize_t dp_phy_test_pattern_debugfs_write(struct file *f, const char __user *buf,
753 				 size_t size, loff_t *pos)
754 {
755 	struct amdgpu_dm_connector *connector = file_inode(f)->i_private;
756 	struct dc_link *link = connector->dc_link;
757 	char *wr_buf = NULL;
758 	uint32_t wr_buf_size = 100;
759 	long param[11] = {0x0};
760 	int max_param_num = 11;
761 	enum dp_test_pattern test_pattern = DP_TEST_PATTERN_UNSUPPORTED;
762 	bool disable_hpd = false;
763 	bool supports_hpd = link->irq_source_hpd != DC_IRQ_SOURCE_INVALID;
764 	bool valid_test_pattern = false;
765 	uint8_t param_nums = 0;
766 	/* init with default 80bit custom pattern */
767 	uint8_t custom_pattern[10] = {
768 			0x1f, 0x7c, 0xf0, 0xc1, 0x07,
769 			0x1f, 0x7c, 0xf0, 0xc1, 0x07
770 			};
771 	struct dc_link_settings prefer_link_settings = {LANE_COUNT_UNKNOWN,
772 			LINK_RATE_UNKNOWN, LINK_SPREAD_DISABLED};
773 	struct dc_link_settings cur_link_settings = {LANE_COUNT_UNKNOWN,
774 			LINK_RATE_UNKNOWN, LINK_SPREAD_DISABLED};
775 	struct link_training_settings link_training_settings = {0};
776 	int i;
777 
778 	if (size == 0)
779 		return -EINVAL;
780 
781 	wr_buf = kcalloc(wr_buf_size, sizeof(char), GFP_KERNEL);
782 	if (!wr_buf)
783 		return -ENOSPC;
784 
785 	if (parse_write_buffer_into_params(wr_buf, wr_buf_size,
786 					   (long *)param, buf,
787 					   max_param_num,
788 					   &param_nums)) {
789 		kfree(wr_buf);
790 		return -EINVAL;
791 	}
792 
793 	if (param_nums <= 0) {
794 		kfree(wr_buf);
795 		DRM_DEBUG_DRIVER("user data not be read\n");
796 		return -EINVAL;
797 	}
798 
799 
800 	test_pattern = param[0];
801 
802 	switch (test_pattern) {
803 	case DP_TEST_PATTERN_VIDEO_MODE:
804 	case DP_TEST_PATTERN_COLOR_SQUARES:
805 	case DP_TEST_PATTERN_COLOR_SQUARES_CEA:
806 	case DP_TEST_PATTERN_VERTICAL_BARS:
807 	case DP_TEST_PATTERN_HORIZONTAL_BARS:
808 	case DP_TEST_PATTERN_COLOR_RAMP:
809 		valid_test_pattern = true;
810 		break;
811 
812 	case DP_TEST_PATTERN_D102:
813 	case DP_TEST_PATTERN_SYMBOL_ERROR:
814 	case DP_TEST_PATTERN_PRBS7:
815 	case DP_TEST_PATTERN_80BIT_CUSTOM:
816 	case DP_TEST_PATTERN_HBR2_COMPLIANCE_EYE:
817 	case DP_TEST_PATTERN_TRAINING_PATTERN4:
818 		disable_hpd = true;
819 		valid_test_pattern = true;
820 		break;
821 
822 	default:
823 		valid_test_pattern = false;
824 		test_pattern = DP_TEST_PATTERN_UNSUPPORTED;
825 		break;
826 	}
827 
828 	if (!valid_test_pattern) {
829 		kfree(wr_buf);
830 		DRM_DEBUG_DRIVER("Invalid Test Pattern Parameters\n");
831 		return size;
832 	}
833 
834 	if (test_pattern == DP_TEST_PATTERN_80BIT_CUSTOM) {
835 		for (i = 0; i < 10; i++) {
836 			if ((uint8_t) param[i + 1] != 0x0)
837 				break;
838 		}
839 
840 		if (i < 10) {
841 			/* not use default value */
842 			for (i = 0; i < 10; i++)
843 				custom_pattern[i] = (uint8_t) param[i + 1];
844 		}
845 	}
846 
847 	/* Usage: set DP physical test pattern using debugfs with normal DP
848 	 * panel. Then plug out DP panel and connect a scope to measure
849 	 * For normal video mode and test pattern generated from CRCT,
850 	 * they are visibile to user. So do not disable HPD.
851 	 * Video Mode is also set to clear the test pattern, so enable HPD
852 	 * because it might have been disabled after a test pattern was set.
853 	 * AUX depends on HPD * sequence dependent, do not move!
854 	 */
855 	if (supports_hpd && !disable_hpd)
856 		dc_link_enable_hpd(link);
857 
858 	prefer_link_settings.lane_count = link->verified_link_cap.lane_count;
859 	prefer_link_settings.link_rate = link->verified_link_cap.link_rate;
860 	prefer_link_settings.link_spread = link->verified_link_cap.link_spread;
861 
862 	cur_link_settings.lane_count = link->cur_link_settings.lane_count;
863 	cur_link_settings.link_rate = link->cur_link_settings.link_rate;
864 	cur_link_settings.link_spread = link->cur_link_settings.link_spread;
865 
866 	link_training_settings.link_settings = cur_link_settings;
867 
868 
869 	if (test_pattern != DP_TEST_PATTERN_VIDEO_MODE) {
870 		if (prefer_link_settings.lane_count != LANE_COUNT_UNKNOWN &&
871 			prefer_link_settings.link_rate !=  LINK_RATE_UNKNOWN &&
872 			(prefer_link_settings.lane_count != cur_link_settings.lane_count ||
873 			prefer_link_settings.link_rate != cur_link_settings.link_rate))
874 			link_training_settings.link_settings = prefer_link_settings;
875 	}
876 
877 	for (i = 0; i < (unsigned int)(link_training_settings.link_settings.lane_count); i++)
878 		link_training_settings.hw_lane_settings[i] = link->cur_lane_setting[i];
879 
880 	dc_link_dp_set_test_pattern(
881 		link,
882 		test_pattern,
883 		DP_TEST_PATTERN_COLOR_SPACE_RGB,
884 		&link_training_settings,
885 		custom_pattern,
886 		10);
887 
888 	/* Usage: Set DP physical test pattern using AMDDP with normal DP panel
889 	 * Then plug out DP panel and connect a scope to measure DP PHY signal.
890 	 * Need disable interrupt to avoid SW driver disable DP output. This is
891 	 * done after the test pattern is set.
892 	 */
893 	if (valid_test_pattern && supports_hpd && disable_hpd)
894 		dc_link_disable_hpd(link);
895 
896 	kfree(wr_buf);
897 
898 	return size;
899 }
900 
901 /*
902  * Returns the DMCUB tracebuffer contents.
903  * Example usage: cat /sys/kernel/debug/dri/0/amdgpu_dm_dmub_tracebuffer
904  */
905 static int dmub_tracebuffer_show(struct seq_file *m, void *data)
906 {
907 	struct amdgpu_device *adev = m->private;
908 	struct dmub_srv_fb_info *fb_info = adev->dm.dmub_fb_info;
909 	struct dmub_fw_meta_info *fw_meta_info = NULL;
910 	struct dmub_debugfs_trace_entry *entries;
911 	uint8_t *tbuf_base;
912 	uint32_t tbuf_size, max_entries, num_entries, first_entry, i;
913 
914 	if (!fb_info)
915 		return 0;
916 
917 	tbuf_base = (uint8_t *)fb_info->fb[DMUB_WINDOW_5_TRACEBUFF].cpu_addr;
918 	if (!tbuf_base)
919 		return 0;
920 
921 	if (adev->dm.dmub_srv)
922 		fw_meta_info = &adev->dm.dmub_srv->meta_info;
923 
924 	tbuf_size = fw_meta_info ? fw_meta_info->trace_buffer_size :
925 				   DMUB_TRACE_BUFFER_SIZE;
926 	max_entries = (tbuf_size - sizeof(struct dmub_debugfs_trace_header)) /
927 		      sizeof(struct dmub_debugfs_trace_entry);
928 
929 	num_entries =
930 		((struct dmub_debugfs_trace_header *)tbuf_base)->entry_count;
931 
932 	/* DMCUB tracebuffer is a ring. If it rolled over, print a hint that
933 	 * entries are being overwritten.
934 	 */
935 	if (num_entries > max_entries)
936 		seq_printf(m, "...\n");
937 
938 	first_entry = num_entries % max_entries;
939 	num_entries = min(num_entries, max_entries);
940 
941 	entries = (struct dmub_debugfs_trace_entry
942 			   *)(tbuf_base +
943 			      sizeof(struct dmub_debugfs_trace_header));
944 
945 	/* To print entries chronologically, start from the first entry till the
946 	 * top of buffer, then from base of buffer to first entry.
947 	 */
948 	for (i = first_entry; i < num_entries; ++i) {
949 		struct dmub_debugfs_trace_entry *entry = &entries[i];
950 
951 		seq_printf(m,
952 			   "trace_code=%u tick_count=%u param0=%u param1=%u\n",
953 			   entry->trace_code, entry->tick_count, entry->param0,
954 			   entry->param1);
955 	}
956 	for (i = 0; i < first_entry; ++i) {
957 		struct dmub_debugfs_trace_entry *entry = &entries[i];
958 
959 		seq_printf(m,
960 			   "trace_code=%u tick_count=%u param0=%u param1=%u\n",
961 			   entry->trace_code, entry->tick_count, entry->param0,
962 			   entry->param1);
963 	}
964 
965 	return 0;
966 }
967 
968 /*
969  * Returns the DMCUB firmware state contents.
970  * Example usage: cat /sys/kernel/debug/dri/0/amdgpu_dm_dmub_fw_state
971  */
972 static int dmub_fw_state_show(struct seq_file *m, void *data)
973 {
974 	struct amdgpu_device *adev = m->private;
975 	struct dmub_srv_fb_info *fb_info = adev->dm.dmub_fb_info;
976 	uint8_t *state_base;
977 	uint32_t state_size;
978 
979 	if (!fb_info)
980 		return 0;
981 
982 	state_base = (uint8_t *)fb_info->fb[DMUB_WINDOW_6_FW_STATE].cpu_addr;
983 	if (!state_base)
984 		return 0;
985 
986 	state_size = fb_info->fb[DMUB_WINDOW_6_FW_STATE].size;
987 
988 	return seq_write(m, state_base, state_size);
989 }
990 
991 /* replay_capability_show() - show eDP panel replay capability
992  *
993  * The read function: replay_capability_show
994  * Shows if sink and driver has Replay capability or not.
995  *
996  *	cat /sys/kernel/debug/dri/0/eDP-X/replay_capability
997  *
998  * Expected output:
999  * "Sink support: no\n" - if panel doesn't support Replay
1000  * "Sink support: yes\n" - if panel supports Replay
1001  * "Driver support: no\n" - if driver doesn't support Replay
1002  * "Driver support: yes\n" - if driver supports Replay
1003  */
1004 static int replay_capability_show(struct seq_file *m, void *data)
1005 {
1006 	struct drm_connector *connector = m->private;
1007 	struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
1008 	struct dc_link *link = aconnector->dc_link;
1009 	bool sink_support_replay = false;
1010 	bool driver_support_replay = false;
1011 
1012 	if (!link)
1013 		return -ENODEV;
1014 
1015 	if (link->type == dc_connection_none)
1016 		return -ENODEV;
1017 
1018 	if (!(link->connector_signal & SIGNAL_TYPE_EDP))
1019 		return -ENODEV;
1020 
1021 	/* If Replay is already set to support, skip the checks */
1022 	if (link->replay_settings.config.replay_supported) {
1023 		sink_support_replay = true;
1024 		driver_support_replay = true;
1025 	} else if ((amdgpu_dc_debug_mask & DC_DISABLE_REPLAY)) {
1026 		sink_support_replay = amdgpu_dm_link_supports_replay(link, aconnector);
1027 	} else {
1028 		struct dc *dc = link->ctx->dc;
1029 
1030 		sink_support_replay = amdgpu_dm_link_supports_replay(link, aconnector);
1031 		if (dc->ctx->dmub_srv && dc->ctx->dmub_srv->dmub)
1032 			driver_support_replay =
1033 				(bool)dc->ctx->dmub_srv->dmub->feature_caps.replay_supported;
1034 	}
1035 
1036 	seq_printf(m, "Sink support: %s\n", str_yes_no(sink_support_replay));
1037 	seq_printf(m, "Driver support: %s\n", str_yes_no(driver_support_replay));
1038 	seq_printf(m, "Config support: %s\n", str_yes_no(link->replay_settings.config.replay_supported));
1039 
1040 	return 0;
1041 }
1042 
1043 /* psr_capability_show() - show eDP panel PSR capability
1044  *
1045  * The read function: sink_psr_capability_show
1046  * Shows if sink has PSR capability or not.
1047  * If yes - the PSR version is appended
1048  *
1049  *	cat /sys/kernel/debug/dri/0/eDP-X/psr_capability
1050  *
1051  * Expected output:
1052  * "Sink support: no\n" - if panel doesn't support PSR
1053  * "Sink support: yes [0x01]\n" - if panel supports PSR1
1054  * "Driver support: no\n" - if driver doesn't support PSR
1055  * "Driver support: yes [0x01]\n" - if driver supports PSR1
1056  */
1057 static int psr_capability_show(struct seq_file *m, void *data)
1058 {
1059 	struct drm_connector *connector = m->private;
1060 	struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
1061 	struct dc_link *link = aconnector->dc_link;
1062 
1063 	if (!link)
1064 		return -ENODEV;
1065 
1066 	if (link->type == dc_connection_none)
1067 		return -ENODEV;
1068 
1069 	if (!(link->connector_signal & SIGNAL_TYPE_EDP))
1070 		return -ENODEV;
1071 
1072 	seq_printf(m, "Sink support: %s", str_yes_no(link->dpcd_caps.psr_info.psr_version != 0));
1073 	if (link->dpcd_caps.psr_info.psr_version)
1074 		seq_printf(m, " [0x%02x]", link->dpcd_caps.psr_info.psr_version);
1075 	seq_puts(m, "\n");
1076 
1077 	seq_printf(m, "Driver support: %s", str_yes_no(link->psr_settings.psr_feature_enabled));
1078 	if (link->psr_settings.psr_version)
1079 		seq_printf(m, " [0x%02x]", link->psr_settings.psr_version);
1080 	seq_putc(m, '\n');
1081 
1082 	return 0;
1083 }
1084 
1085 /*
1086  * Returns the current bpc for the crtc.
1087  * Example usage: cat /sys/kernel/debug/dri/0/crtc-0/amdgpu_current_bpc
1088  */
1089 static int amdgpu_current_bpc_show(struct seq_file *m, void *data)
1090 {
1091 	struct drm_crtc *crtc = m->private;
1092 	struct drm_device *dev = crtc->dev;
1093 	struct dm_crtc_state *dm_crtc_state = NULL;
1094 	int res = -ENODEV;
1095 	unsigned int bpc;
1096 
1097 	mutex_lock(&dev->mode_config.mutex);
1098 	drm_modeset_lock(&crtc->mutex, NULL);
1099 	if (crtc->state == NULL)
1100 		goto unlock;
1101 
1102 	dm_crtc_state = to_dm_crtc_state(crtc->state);
1103 	if (dm_crtc_state->stream == NULL)
1104 		goto unlock;
1105 
1106 	switch (dm_crtc_state->stream->timing.display_color_depth) {
1107 	case COLOR_DEPTH_666:
1108 		bpc = 6;
1109 		break;
1110 	case COLOR_DEPTH_888:
1111 		bpc = 8;
1112 		break;
1113 	case COLOR_DEPTH_101010:
1114 		bpc = 10;
1115 		break;
1116 	case COLOR_DEPTH_121212:
1117 		bpc = 12;
1118 		break;
1119 	case COLOR_DEPTH_161616:
1120 		bpc = 16;
1121 		break;
1122 	default:
1123 		goto unlock;
1124 	}
1125 
1126 	seq_printf(m, "Current: %u\n", bpc);
1127 	res = 0;
1128 
1129 unlock:
1130 	drm_modeset_unlock(&crtc->mutex);
1131 	mutex_unlock(&dev->mode_config.mutex);
1132 
1133 	return res;
1134 }
1135 DEFINE_SHOW_ATTRIBUTE(amdgpu_current_bpc);
1136 
1137 /*
1138  * Returns the current colorspace for the crtc.
1139  * Example usage: cat /sys/kernel/debug/dri/0/crtc-0/amdgpu_current_colorspace
1140  */
1141 static int amdgpu_current_colorspace_show(struct seq_file *m, void *data)
1142 {
1143 	struct drm_crtc *crtc = m->private;
1144 	struct drm_device *dev = crtc->dev;
1145 	struct dm_crtc_state *dm_crtc_state = NULL;
1146 	int res = -ENODEV;
1147 
1148 	mutex_lock(&dev->mode_config.mutex);
1149 	drm_modeset_lock(&crtc->mutex, NULL);
1150 	if (crtc->state == NULL)
1151 		goto unlock;
1152 
1153 	dm_crtc_state = to_dm_crtc_state(crtc->state);
1154 	if (dm_crtc_state->stream == NULL)
1155 		goto unlock;
1156 
1157 	switch (dm_crtc_state->stream->output_color_space) {
1158 	case COLOR_SPACE_SRGB:
1159 		seq_puts(m, "sRGB");
1160 		break;
1161 	case COLOR_SPACE_YCBCR601:
1162 	case COLOR_SPACE_YCBCR601_LIMITED:
1163 		seq_puts(m, "BT601_YCC");
1164 		break;
1165 	case COLOR_SPACE_YCBCR709:
1166 	case COLOR_SPACE_YCBCR709_LIMITED:
1167 		seq_puts(m, "BT709_YCC");
1168 		break;
1169 	case COLOR_SPACE_ADOBERGB:
1170 		seq_puts(m, "opRGB");
1171 		break;
1172 	case COLOR_SPACE_2020_RGB_FULLRANGE:
1173 		seq_puts(m, "BT2020_RGB");
1174 		break;
1175 	case COLOR_SPACE_2020_YCBCR_LIMITED:
1176 		seq_puts(m, "BT2020_YCC");
1177 		break;
1178 	default:
1179 		goto unlock;
1180 	}
1181 	res = 0;
1182 
1183 unlock:
1184 	drm_modeset_unlock(&crtc->mutex);
1185 	mutex_unlock(&dev->mode_config.mutex);
1186 
1187 	return res;
1188 }
1189 DEFINE_SHOW_ATTRIBUTE(amdgpu_current_colorspace);
1190 
1191 
1192 /*
1193  * Example usage:
1194  * Disable dsc passthrough, i.e.,: have dsc decoding at converver, not external RX
1195  *   echo 1 /sys/kernel/debug/dri/0/DP-1/dsc_disable_passthrough
1196  * Enable dsc passthrough, i.e.,: have dsc passthrough to external RX
1197  *   echo 0 /sys/kernel/debug/dri/0/DP-1/dsc_disable_passthrough
1198  */
1199 static ssize_t dp_dsc_passthrough_set(struct file *f, const char __user *buf,
1200 				 size_t size, loff_t *pos)
1201 {
1202 	struct amdgpu_dm_connector *aconnector = file_inode(f)->i_private;
1203 	char *wr_buf = NULL;
1204 	uint32_t wr_buf_size = 42;
1205 	int max_param_num = 1;
1206 	long param;
1207 	uint8_t param_nums = 0;
1208 
1209 	if (size == 0)
1210 		return -EINVAL;
1211 
1212 	wr_buf = kcalloc(wr_buf_size, sizeof(char), GFP_KERNEL);
1213 
1214 	if (!wr_buf) {
1215 		DRM_DEBUG_DRIVER("no memory to allocate write buffer\n");
1216 		return -ENOSPC;
1217 	}
1218 
1219 	if (parse_write_buffer_into_params(wr_buf, wr_buf_size,
1220 					   &param, buf,
1221 					   max_param_num,
1222 					   &param_nums)) {
1223 		kfree(wr_buf);
1224 		return -EINVAL;
1225 	}
1226 
1227 	aconnector->dsc_settings.dsc_force_disable_passthrough = param;
1228 
1229 	kfree(wr_buf);
1230 	return 0;
1231 }
1232 
1233 /*
1234  * Returns the HDCP capability of the Display (1.4 for now).
1235  *
1236  * NOTE* Not all HDMI displays report their HDCP caps even when they are capable.
1237  * Since its rare for a display to not be HDCP 1.4 capable, we set HDMI as always capable.
1238  *
1239  * Example usage: cat /sys/kernel/debug/dri/0/DP-1/hdcp_sink_capability
1240  *		or cat /sys/kernel/debug/dri/0/HDMI-A-1/hdcp_sink_capability
1241  */
1242 static int hdcp_sink_capability_show(struct seq_file *m, void *data)
1243 {
1244 	struct drm_connector *connector = m->private;
1245 	struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
1246 	bool hdcp_cap, hdcp2_cap;
1247 
1248 	if (connector->status != connector_status_connected)
1249 		return -ENODEV;
1250 
1251 	seq_printf(m, "%s:%d HDCP version: ", connector->name, connector->base.id);
1252 
1253 	hdcp_cap = dc_link_is_hdcp14(aconnector->dc_link, aconnector->dc_sink->sink_signal);
1254 	hdcp2_cap = dc_link_is_hdcp22(aconnector->dc_link, aconnector->dc_sink->sink_signal);
1255 
1256 
1257 	if (hdcp_cap)
1258 		seq_printf(m, "%s ", "HDCP1.4");
1259 	if (hdcp2_cap)
1260 		seq_printf(m, "%s ", "HDCP2.2");
1261 
1262 	if (!hdcp_cap && !hdcp2_cap)
1263 		seq_printf(m, "%s ", "None");
1264 
1265 	seq_putc(m, '\n');
1266 
1267 	return 0;
1268 }
1269 
1270 /*
1271  * Returns whether the connected display is internal and not hotpluggable.
1272  * Example usage: cat /sys/kernel/debug/dri/0/DP-1/internal_display
1273  */
1274 static int internal_display_show(struct seq_file *m, void *data)
1275 {
1276 	struct drm_connector *connector = m->private;
1277 	struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
1278 	struct dc_link *link = aconnector->dc_link;
1279 
1280 	seq_printf(m, "Internal: %u\n", link->is_internal_display);
1281 
1282 	return 0;
1283 }
1284 
1285 /*
1286  * Returns the maximum source viewport width (in pixels) this ASIC can
1287  * downscale from. A value of 0 means there is no such limit.
1288  * Example usage: cat /sys/kernel/debug/dri/0/eDP-1/max_downscale_src_width
1289  */
1290 static int max_downscale_src_width_show(struct seq_file *m, void *unused)
1291 {
1292 	struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(m->private);
1293 	struct dc_link *link = aconnector->dc_link;
1294 
1295 	if (!link)
1296 		return -ENODEV;
1297 
1298 	seq_printf(m, "%d\n", link->dc->debug.max_downscale_src_width);
1299 
1300 	return 0;
1301 }
1302 
1303 /*
1304  * Returns the number of segments used if ODM Combine mode is enabled.
1305  * Example usage: cat /sys/kernel/debug/dri/0/DP-1/odm_combine_segments
1306  */
1307 static int odm_combine_segments_show(struct seq_file *m, void *unused)
1308 {
1309 	struct drm_connector *connector = m->private;
1310 	struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
1311 	struct dc_link *link = aconnector->dc_link;
1312 	struct pipe_ctx *pipe_ctx = NULL;
1313 	int i, segments = -EOPNOTSUPP;
1314 
1315 	for (i = 0; i < MAX_PIPES; i++) {
1316 		pipe_ctx = &link->dc->current_state->res_ctx.pipe_ctx[i];
1317 		if (pipe_ctx->stream &&
1318 		    pipe_ctx->stream->link == link)
1319 			break;
1320 	}
1321 
1322 	if (connector->status != connector_status_connected)
1323 		return -ENODEV;
1324 
1325 	if (pipe_ctx && pipe_ctx->stream_res.tg &&
1326 	    pipe_ctx->stream_res.tg->funcs->get_odm_combine_segments)
1327 		pipe_ctx->stream_res.tg->funcs->get_odm_combine_segments(pipe_ctx->stream_res.tg, &segments);
1328 
1329 	seq_printf(m, "%d\n", segments);
1330 	return 0;
1331 }
1332 
1333 /* function description
1334  *
1335  * generic SDP message access for testing
1336  *
1337  * debugfs sdp_message is located at /syskernel/debug/dri/0/DP-x
1338  *
1339  * SDP header
1340  * Hb0 : Secondary-Data Packet ID
1341  * Hb1 : Secondary-Data Packet type
1342  * Hb2 : Secondary-Data-packet-specific header, Byte 0
1343  * Hb3 : Secondary-Data-packet-specific header, Byte 1
1344  *
1345  * for using custom sdp message: input 4 bytes SDP header and 32 bytes raw data
1346  */
1347 static ssize_t dp_sdp_message_debugfs_write(struct file *f, const char __user *buf,
1348 				 size_t size, loff_t *pos)
1349 {
1350 	int r;
1351 	uint8_t data[36] = {0};
1352 	struct amdgpu_dm_connector *connector = file_inode(f)->i_private;
1353 	struct dm_crtc_state *acrtc_state;
1354 	uint32_t write_size = 36;
1355 
1356 	if (connector->base.status != connector_status_connected)
1357 		return -ENODEV;
1358 
1359 	if (size == 0)
1360 		return 0;
1361 
1362 	if (!connector->base.state || !connector->base.state->crtc)
1363 		return -ENODEV;
1364 
1365 	acrtc_state = to_dm_crtc_state(connector->base.state->crtc->state);
1366 
1367 	write_size = min_t(size_t, size, sizeof(data));
1368 
1369 	r = copy_from_user(data, buf, write_size);
1370 
1371 	write_size -= r;
1372 
1373 	dc_stream_send_dp_sdp(acrtc_state->stream, data, write_size);
1374 
1375 	return write_size;
1376 }
1377 
1378 /* function: Read link's DSC & FEC capabilities
1379  *
1380  *
1381  * Access it with the following command (you need to specify
1382  * connector like DP-1):
1383  *
1384  *	cat /sys/kernel/debug/dri/0/DP-X/dp_dsc_fec_support
1385  *
1386  */
1387 static int dp_dsc_fec_support_show(struct seq_file *m, void *data)
1388 {
1389 	struct drm_connector *connector = m->private;
1390 	struct drm_modeset_acquire_ctx ctx;
1391 	struct drm_device *dev = connector->dev;
1392 	struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
1393 	int ret = 0;
1394 	bool try_again = false;
1395 	bool is_fec_supported = false;
1396 	bool is_dsc_supported = false;
1397 	struct dpcd_caps dpcd_caps;
1398 
1399 	drm_modeset_acquire_init(&ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE);
1400 	do {
1401 		try_again = false;
1402 		ret = drm_modeset_lock(&dev->mode_config.connection_mutex, &ctx);
1403 		if (ret) {
1404 			if (ret == -EDEADLK) {
1405 				ret = drm_modeset_backoff(&ctx);
1406 				if (!ret) {
1407 					try_again = true;
1408 					continue;
1409 				}
1410 			}
1411 			break;
1412 		}
1413 		if (connector->status != connector_status_connected) {
1414 			ret = -ENODEV;
1415 			break;
1416 		}
1417 		dpcd_caps = aconnector->dc_link->dpcd_caps;
1418 		if (aconnector->mst_output_port) {
1419 			/* aconnector sets dsc_aux during get_modes call
1420 			 * if MST connector has it means it can either
1421 			 * enable DSC on the sink device or on MST branch
1422 			 * its connected to.
1423 			 */
1424 			if (aconnector->dsc_aux) {
1425 				is_fec_supported = true;
1426 				is_dsc_supported = true;
1427 			}
1428 		} else {
1429 			is_fec_supported = dpcd_caps.fec_cap.raw & 0x1;
1430 			is_dsc_supported = dpcd_caps.dsc_caps.dsc_basic_caps.raw[0] & 0x1;
1431 		}
1432 	} while (try_again);
1433 
1434 	drm_modeset_drop_locks(&ctx);
1435 	drm_modeset_acquire_fini(&ctx);
1436 
1437 	seq_printf(m, "FEC_Sink_Support: %s\n", str_yes_no(is_fec_supported));
1438 	seq_printf(m, "DSC_Sink_Support: %s\n", str_yes_no(is_dsc_supported));
1439 
1440 	return ret;
1441 }
1442 
1443 /* function: Trigger virtual HPD redetection on connector
1444  *
1445  * This function will perform link rediscovery, link disable
1446  * and enable, and dm connector state update.
1447  *
1448  * Retrigger HPD on an existing connector by echoing 1 into
1449  * its respectful "trigger_hotplug" debugfs entry:
1450  *
1451  *	echo 1 > /sys/kernel/debug/dri/0/DP-X/trigger_hotplug
1452  *
1453  * This function can perform HPD unplug:
1454  *
1455  *	echo 0 > /sys/kernel/debug/dri/0/DP-X/trigger_hotplug
1456  *
1457  */
1458 static ssize_t trigger_hotplug(struct file *f, const char __user *buf,
1459 							size_t size, loff_t *pos)
1460 {
1461 	struct amdgpu_dm_connector *aconnector = file_inode(f)->i_private;
1462 	struct drm_connector *connector = &aconnector->base;
1463 	struct dc_link *link = NULL;
1464 	struct drm_device *dev = connector->dev;
1465 	struct amdgpu_device *adev = drm_to_adev(dev);
1466 	enum dc_connection_type new_connection_type = dc_connection_none;
1467 	char *wr_buf = NULL;
1468 	uint32_t wr_buf_size = 42;
1469 	int max_param_num = 1;
1470 	long param[1] = {0};
1471 	uint8_t param_nums = 0;
1472 	bool ret = false;
1473 
1474 	if (!aconnector->dc_link)
1475 		return -EINVAL;
1476 
1477 	if (size == 0)
1478 		return -EINVAL;
1479 
1480 	wr_buf = kcalloc(wr_buf_size, sizeof(char), GFP_KERNEL);
1481 
1482 	if (!wr_buf) {
1483 		DRM_DEBUG_DRIVER("no memory to allocate write buffer\n");
1484 		return -ENOSPC;
1485 	}
1486 
1487 	if (parse_write_buffer_into_params(wr_buf, wr_buf_size,
1488 						(long *)param, buf,
1489 						max_param_num,
1490 						&param_nums)) {
1491 		kfree(wr_buf);
1492 		return -EINVAL;
1493 	}
1494 
1495 	kfree(wr_buf);
1496 
1497 	if (param_nums <= 0) {
1498 		DRM_DEBUG_DRIVER("user data not be read\n");
1499 		return -EINVAL;
1500 	}
1501 
1502 	mutex_lock(&aconnector->hpd_lock);
1503 
1504 	/* Don't support for mst end device*/
1505 	if (aconnector->mst_root) {
1506 		mutex_unlock(&aconnector->hpd_lock);
1507 		return -EINVAL;
1508 	}
1509 
1510 	if (param[0] == 1) {
1511 
1512 		if (!dc_link_detect_connection_type(aconnector->dc_link, &new_connection_type) &&
1513 			new_connection_type != dc_connection_none)
1514 			goto unlock;
1515 
1516 		mutex_lock(&adev->dm.dc_lock);
1517 		ret = dc_link_detect(aconnector->dc_link, DETECT_REASON_HPD);
1518 		mutex_unlock(&adev->dm.dc_lock);
1519 
1520 		if (!ret)
1521 			goto unlock;
1522 
1523 		amdgpu_dm_update_connector_after_detect(aconnector);
1524 
1525 		drm_modeset_lock_all(dev);
1526 		dm_restore_drm_connector_state(dev, connector);
1527 		drm_modeset_unlock_all(dev);
1528 
1529 		drm_kms_helper_connector_hotplug_event(connector);
1530 	} else if (param[0] == 0) {
1531 		if (!aconnector->dc_link)
1532 			goto unlock;
1533 
1534 		link = aconnector->dc_link;
1535 
1536 		if (link->local_sink) {
1537 			dc_sink_release(link->local_sink);
1538 			link->local_sink = NULL;
1539 		}
1540 
1541 		link->dpcd_sink_count = 0;
1542 		link->type = dc_connection_none;
1543 		link->dongle_max_pix_clk = 0;
1544 
1545 		amdgpu_dm_update_connector_after_detect(aconnector);
1546 
1547 		/* If the aconnector is the root node in mst topology */
1548 		if (aconnector->mst_mgr.mst_state == true)
1549 			dc_link_reset_cur_dp_mst_topology(link);
1550 
1551 		drm_modeset_lock_all(dev);
1552 		dm_restore_drm_connector_state(dev, connector);
1553 		drm_modeset_unlock_all(dev);
1554 
1555 		drm_kms_helper_connector_hotplug_event(connector);
1556 	}
1557 
1558 unlock:
1559 	mutex_unlock(&aconnector->hpd_lock);
1560 
1561 	return size;
1562 }
1563 
1564 /* function: read DSC status on the connector
1565  *
1566  * The read function: dp_dsc_clock_en_read
1567  * returns current status of DSC clock on the connector.
1568  * The return is a boolean flag: 1 or 0.
1569  *
1570  * Access it with the following command (you need to specify
1571  * connector like DP-1):
1572  *
1573  *	cat /sys/kernel/debug/dri/0/DP-X/dsc_clock_en
1574  *
1575  * Expected output:
1576  * 1 - means that DSC is currently enabled
1577  * 0 - means that DSC is disabled
1578  */
1579 static ssize_t dp_dsc_clock_en_read(struct file *f, char __user *buf,
1580 				    size_t size, loff_t *pos)
1581 {
1582 	char *rd_buf = NULL;
1583 	struct amdgpu_dm_connector *aconnector = file_inode(f)->i_private;
1584 	struct display_stream_compressor *dsc;
1585 	struct dcn_dsc_state dsc_state = {0};
1586 	const uint32_t rd_buf_size = 10;
1587 	struct pipe_ctx *pipe_ctx;
1588 	ssize_t result = 0;
1589 	int i, r, str_len = 10;
1590 
1591 	rd_buf = kcalloc(rd_buf_size, sizeof(char), GFP_KERNEL);
1592 
1593 	if (!rd_buf)
1594 		return -ENOMEM;
1595 
1596 	for (i = 0; i < MAX_PIPES; i++) {
1597 		pipe_ctx = &aconnector->dc_link->dc->current_state->res_ctx.pipe_ctx[i];
1598 		if (pipe_ctx->stream &&
1599 		    pipe_ctx->stream->link == aconnector->dc_link &&
1600 		    pipe_ctx->stream->sink &&
1601 		    pipe_ctx->stream->sink == aconnector->dc_sink)
1602 			break;
1603 	}
1604 
1605 	dsc = pipe_ctx->stream_res.dsc;
1606 	if (dsc)
1607 		dsc->funcs->dsc_read_state(dsc, &dsc_state);
1608 
1609 	snprintf(rd_buf, str_len,
1610 		"%d\n",
1611 		dsc_state.dsc_clock_en);
1612 
1613 	while (size) {
1614 		if (*pos >= rd_buf_size)
1615 			break;
1616 
1617 		r = put_user(*(rd_buf + result), buf);
1618 		if (r) {
1619 			kfree(rd_buf);
1620 			return r; /* r = -EFAULT */
1621 		}
1622 
1623 		buf += 1;
1624 		size -= 1;
1625 		*pos += 1;
1626 		result += 1;
1627 	}
1628 
1629 	kfree(rd_buf);
1630 	return result;
1631 }
1632 
1633 /* function: write force DSC on the connector
1634  *
1635  * The write function: dp_dsc_clock_en_write
1636  * enables to force DSC on the connector.
1637  * User can write to either force enable or force disable DSC
1638  * on the next modeset or set it to driver default
1639  *
1640  * Accepted inputs:
1641  * 0 - default DSC enablement policy
1642  * 1 - force enable DSC on the connector
1643  * 2 - force disable DSC on the connector (might cause fail in atomic_check)
1644  *
1645  * Writing DSC settings is done with the following command:
1646  * - To force enable DSC (you need to specify
1647  * connector like DP-1):
1648  *
1649  *	echo 0x1 > /sys/kernel/debug/dri/0/DP-X/dsc_clock_en
1650  *
1651  * - To return to default state set the flag to zero and
1652  * let driver deal with DSC automatically
1653  * (you need to specify connector like DP-1):
1654  *
1655  *	echo 0x0 > /sys/kernel/debug/dri/0/DP-X/dsc_clock_en
1656  *
1657  */
1658 static ssize_t dp_dsc_clock_en_write(struct file *f, const char __user *buf,
1659 				     size_t size, loff_t *pos)
1660 {
1661 	struct amdgpu_dm_connector *aconnector = file_inode(f)->i_private;
1662 	struct drm_connector *connector = &aconnector->base;
1663 	struct drm_device *dev = connector->dev;
1664 	struct drm_crtc *crtc = NULL;
1665 	struct dm_crtc_state *dm_crtc_state = NULL;
1666 	struct pipe_ctx *pipe_ctx;
1667 	int i;
1668 	char *wr_buf = NULL;
1669 	uint32_t wr_buf_size = 42;
1670 	int max_param_num = 1;
1671 	long param[1] = {0};
1672 	uint8_t param_nums = 0;
1673 
1674 	if (size == 0)
1675 		return -EINVAL;
1676 
1677 	wr_buf = kcalloc(wr_buf_size, sizeof(char), GFP_KERNEL);
1678 
1679 	if (!wr_buf) {
1680 		DRM_DEBUG_DRIVER("no memory to allocate write buffer\n");
1681 		return -ENOSPC;
1682 	}
1683 
1684 	if (parse_write_buffer_into_params(wr_buf, wr_buf_size,
1685 					    (long *)param, buf,
1686 					    max_param_num,
1687 					    &param_nums)) {
1688 		kfree(wr_buf);
1689 		return -EINVAL;
1690 	}
1691 
1692 	if (param_nums <= 0) {
1693 		DRM_DEBUG_DRIVER("user data not be read\n");
1694 		kfree(wr_buf);
1695 		return -EINVAL;
1696 	}
1697 
1698 	for (i = 0; i < MAX_PIPES; i++) {
1699 		pipe_ctx = &aconnector->dc_link->dc->current_state->res_ctx.pipe_ctx[i];
1700 		if (pipe_ctx->stream &&
1701 		    pipe_ctx->stream->link == aconnector->dc_link &&
1702 		    pipe_ctx->stream->sink &&
1703 		    pipe_ctx->stream->sink == aconnector->dc_sink)
1704 			break;
1705 	}
1706 
1707 	if (!pipe_ctx->stream)
1708 		goto done;
1709 
1710 	// Get CRTC state
1711 	mutex_lock(&dev->mode_config.mutex);
1712 	drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
1713 
1714 	if (connector->state == NULL)
1715 		goto unlock;
1716 
1717 	crtc = connector->state->crtc;
1718 	if (crtc == NULL)
1719 		goto unlock;
1720 
1721 	drm_modeset_lock(&crtc->mutex, NULL);
1722 	if (crtc->state == NULL)
1723 		goto unlock;
1724 
1725 	dm_crtc_state = to_dm_crtc_state(crtc->state);
1726 	if (dm_crtc_state->stream == NULL)
1727 		goto unlock;
1728 
1729 	if (param[0] == 1)
1730 		aconnector->dsc_settings.dsc_force_enable = DSC_CLK_FORCE_ENABLE;
1731 	else if (param[0] == 2)
1732 		aconnector->dsc_settings.dsc_force_enable = DSC_CLK_FORCE_DISABLE;
1733 	else
1734 		aconnector->dsc_settings.dsc_force_enable = DSC_CLK_FORCE_DEFAULT;
1735 
1736 	dm_crtc_state->dsc_force_changed = true;
1737 
1738 unlock:
1739 	if (crtc)
1740 		drm_modeset_unlock(&crtc->mutex);
1741 	drm_modeset_unlock(&dev->mode_config.connection_mutex);
1742 	mutex_unlock(&dev->mode_config.mutex);
1743 
1744 done:
1745 	kfree(wr_buf);
1746 	return size;
1747 }
1748 
1749 /* function: read DSC slice width parameter on the connector
1750  *
1751  * The read function: dp_dsc_slice_width_read
1752  * returns dsc slice width used in the current configuration
1753  * The return is an integer: 0 or other positive number
1754  *
1755  * Access the status with the following command:
1756  *
1757  *	cat /sys/kernel/debug/dri/0/DP-X/dsc_slice_width
1758  *
1759  * 0 - means that DSC is disabled
1760  *
1761  * Any other number more than zero represents the
1762  * slice width currently used by DSC in pixels
1763  *
1764  */
1765 static ssize_t dp_dsc_slice_width_read(struct file *f, char __user *buf,
1766 				    size_t size, loff_t *pos)
1767 {
1768 	char *rd_buf = NULL;
1769 	struct amdgpu_dm_connector *aconnector = file_inode(f)->i_private;
1770 	struct display_stream_compressor *dsc;
1771 	struct dcn_dsc_state dsc_state = {0};
1772 	const uint32_t rd_buf_size = 100;
1773 	struct pipe_ctx *pipe_ctx;
1774 	ssize_t result = 0;
1775 	int i, r, str_len = 30;
1776 
1777 	rd_buf = kcalloc(rd_buf_size, sizeof(char), GFP_KERNEL);
1778 
1779 	if (!rd_buf)
1780 		return -ENOMEM;
1781 
1782 	for (i = 0; i < MAX_PIPES; i++) {
1783 		pipe_ctx = &aconnector->dc_link->dc->current_state->res_ctx.pipe_ctx[i];
1784 		if (pipe_ctx->stream &&
1785 		    pipe_ctx->stream->link == aconnector->dc_link &&
1786 		    pipe_ctx->stream->sink &&
1787 		    pipe_ctx->stream->sink == aconnector->dc_sink)
1788 			break;
1789 	}
1790 
1791 	dsc = pipe_ctx->stream_res.dsc;
1792 	if (dsc)
1793 		dsc->funcs->dsc_read_state(dsc, &dsc_state);
1794 
1795 	snprintf(rd_buf, str_len,
1796 		"%d\n",
1797 		dsc_state.dsc_slice_width);
1798 
1799 	while (size) {
1800 		if (*pos >= rd_buf_size)
1801 			break;
1802 
1803 		r = put_user(*(rd_buf + result), buf);
1804 		if (r) {
1805 			kfree(rd_buf);
1806 			return r; /* r = -EFAULT */
1807 		}
1808 
1809 		buf += 1;
1810 		size -= 1;
1811 		*pos += 1;
1812 		result += 1;
1813 	}
1814 
1815 	kfree(rd_buf);
1816 	return result;
1817 }
1818 
1819 /* function: write DSC slice width parameter
1820  *
1821  * The write function: dp_dsc_slice_width_write
1822  * overwrites automatically generated DSC configuration
1823  * of slice width.
1824  *
1825  * The user has to write the slice width divisible by the
1826  * picture width.
1827  *
1828  * Also the user has to write width in hexidecimal
1829  * rather than in decimal.
1830  *
1831  * Writing DSC settings is done with the following command:
1832  * - To force overwrite slice width: (example sets to 1920 pixels)
1833  *
1834  *	echo 0x780 > /sys/kernel/debug/dri/0/DP-X/dsc_slice_width
1835  *
1836  *  - To stop overwriting and let driver find the optimal size,
1837  * set the width to zero:
1838  *
1839  *	echo 0x0 > /sys/kernel/debug/dri/0/DP-X/dsc_slice_width
1840  *
1841  */
1842 static ssize_t dp_dsc_slice_width_write(struct file *f, const char __user *buf,
1843 				     size_t size, loff_t *pos)
1844 {
1845 	struct amdgpu_dm_connector *aconnector = file_inode(f)->i_private;
1846 	struct pipe_ctx *pipe_ctx;
1847 	struct drm_connector *connector = &aconnector->base;
1848 	struct drm_device *dev = connector->dev;
1849 	struct drm_crtc *crtc = NULL;
1850 	struct dm_crtc_state *dm_crtc_state = NULL;
1851 	int i;
1852 	char *wr_buf = NULL;
1853 	uint32_t wr_buf_size = 42;
1854 	int max_param_num = 1;
1855 	long param[1] = {0};
1856 	uint8_t param_nums = 0;
1857 
1858 	if (size == 0)
1859 		return -EINVAL;
1860 
1861 	wr_buf = kcalloc(wr_buf_size, sizeof(char), GFP_KERNEL);
1862 
1863 	if (!wr_buf) {
1864 		DRM_DEBUG_DRIVER("no memory to allocate write buffer\n");
1865 		return -ENOSPC;
1866 	}
1867 
1868 	if (parse_write_buffer_into_params(wr_buf, wr_buf_size,
1869 					    (long *)param, buf,
1870 					    max_param_num,
1871 					    &param_nums)) {
1872 		kfree(wr_buf);
1873 		return -EINVAL;
1874 	}
1875 
1876 	if (param_nums <= 0) {
1877 		DRM_DEBUG_DRIVER("user data not be read\n");
1878 		kfree(wr_buf);
1879 		return -EINVAL;
1880 	}
1881 
1882 	for (i = 0; i < MAX_PIPES; i++) {
1883 		pipe_ctx = &aconnector->dc_link->dc->current_state->res_ctx.pipe_ctx[i];
1884 		if (pipe_ctx->stream &&
1885 		    pipe_ctx->stream->link == aconnector->dc_link &&
1886 		    pipe_ctx->stream->sink &&
1887 		    pipe_ctx->stream->sink == aconnector->dc_sink)
1888 			break;
1889 	}
1890 
1891 	if (!pipe_ctx->stream)
1892 		goto done;
1893 
1894 	// Safely get CRTC state
1895 	mutex_lock(&dev->mode_config.mutex);
1896 	drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
1897 
1898 	if (connector->state == NULL)
1899 		goto unlock;
1900 
1901 	crtc = connector->state->crtc;
1902 	if (crtc == NULL)
1903 		goto unlock;
1904 
1905 	drm_modeset_lock(&crtc->mutex, NULL);
1906 	if (crtc->state == NULL)
1907 		goto unlock;
1908 
1909 	dm_crtc_state = to_dm_crtc_state(crtc->state);
1910 	if (dm_crtc_state->stream == NULL)
1911 		goto unlock;
1912 
1913 	if (param[0] > 0)
1914 		aconnector->dsc_settings.dsc_num_slices_h = DIV_ROUND_UP(
1915 					pipe_ctx->stream->timing.h_addressable,
1916 					param[0]);
1917 	else
1918 		aconnector->dsc_settings.dsc_num_slices_h = 0;
1919 
1920 	dm_crtc_state->dsc_force_changed = true;
1921 
1922 unlock:
1923 	if (crtc)
1924 		drm_modeset_unlock(&crtc->mutex);
1925 	drm_modeset_unlock(&dev->mode_config.connection_mutex);
1926 	mutex_unlock(&dev->mode_config.mutex);
1927 
1928 done:
1929 	kfree(wr_buf);
1930 	return size;
1931 }
1932 
1933 /* function: read DSC slice height parameter on the connector
1934  *
1935  * The read function: dp_dsc_slice_height_read
1936  * returns dsc slice height used in the current configuration
1937  * The return is an integer: 0 or other positive number
1938  *
1939  * Access the status with the following command:
1940  *
1941  *	cat /sys/kernel/debug/dri/0/DP-X/dsc_slice_height
1942  *
1943  * 0 - means that DSC is disabled
1944  *
1945  * Any other number more than zero represents the
1946  * slice height currently used by DSC in pixels
1947  *
1948  */
1949 static ssize_t dp_dsc_slice_height_read(struct file *f, char __user *buf,
1950 				    size_t size, loff_t *pos)
1951 {
1952 	char *rd_buf = NULL;
1953 	struct amdgpu_dm_connector *aconnector = file_inode(f)->i_private;
1954 	struct display_stream_compressor *dsc;
1955 	struct dcn_dsc_state dsc_state = {0};
1956 	const uint32_t rd_buf_size = 100;
1957 	struct pipe_ctx *pipe_ctx;
1958 	ssize_t result = 0;
1959 	int i, r, str_len = 30;
1960 
1961 	rd_buf = kcalloc(rd_buf_size, sizeof(char), GFP_KERNEL);
1962 
1963 	if (!rd_buf)
1964 		return -ENOMEM;
1965 
1966 	for (i = 0; i < MAX_PIPES; i++) {
1967 		pipe_ctx = &aconnector->dc_link->dc->current_state->res_ctx.pipe_ctx[i];
1968 		if (pipe_ctx->stream &&
1969 		    pipe_ctx->stream->link == aconnector->dc_link &&
1970 		    pipe_ctx->stream->sink &&
1971 		    pipe_ctx->stream->sink == aconnector->dc_sink)
1972 			break;
1973 	}
1974 
1975 	dsc = pipe_ctx->stream_res.dsc;
1976 	if (dsc)
1977 		dsc->funcs->dsc_read_state(dsc, &dsc_state);
1978 
1979 	snprintf(rd_buf, str_len,
1980 		"%d\n",
1981 		dsc_state.dsc_slice_height);
1982 
1983 	while (size) {
1984 		if (*pos >= rd_buf_size)
1985 			break;
1986 
1987 		r = put_user(*(rd_buf + result), buf);
1988 		if (r) {
1989 			kfree(rd_buf);
1990 			return r; /* r = -EFAULT */
1991 		}
1992 
1993 		buf += 1;
1994 		size -= 1;
1995 		*pos += 1;
1996 		result += 1;
1997 	}
1998 
1999 	kfree(rd_buf);
2000 	return result;
2001 }
2002 
2003 /* function: write DSC slice height parameter
2004  *
2005  * The write function: dp_dsc_slice_height_write
2006  * overwrites automatically generated DSC configuration
2007  * of slice height.
2008  *
2009  * The user has to write the slice height divisible by the
2010  * picture height.
2011  *
2012  * Also the user has to write height in hexidecimal
2013  * rather than in decimal.
2014  *
2015  * Writing DSC settings is done with the following command:
2016  * - To force overwrite slice height (example sets to 128 pixels):
2017  *
2018  *	echo 0x80 > /sys/kernel/debug/dri/0/DP-X/dsc_slice_height
2019  *
2020  *  - To stop overwriting and let driver find the optimal size,
2021  * set the height to zero:
2022  *
2023  *	echo 0x0 > /sys/kernel/debug/dri/0/DP-X/dsc_slice_height
2024  *
2025  */
2026 static ssize_t dp_dsc_slice_height_write(struct file *f, const char __user *buf,
2027 				     size_t size, loff_t *pos)
2028 {
2029 	struct amdgpu_dm_connector *aconnector = file_inode(f)->i_private;
2030 	struct drm_connector *connector = &aconnector->base;
2031 	struct drm_device *dev = connector->dev;
2032 	struct drm_crtc *crtc = NULL;
2033 	struct dm_crtc_state *dm_crtc_state = NULL;
2034 	struct pipe_ctx *pipe_ctx;
2035 	int i;
2036 	char *wr_buf = NULL;
2037 	uint32_t wr_buf_size = 42;
2038 	int max_param_num = 1;
2039 	uint8_t param_nums = 0;
2040 	long param[1] = {0};
2041 
2042 	if (size == 0)
2043 		return -EINVAL;
2044 
2045 	wr_buf = kcalloc(wr_buf_size, sizeof(char), GFP_KERNEL);
2046 
2047 	if (!wr_buf) {
2048 		DRM_DEBUG_DRIVER("no memory to allocate write buffer\n");
2049 		return -ENOSPC;
2050 	}
2051 
2052 	if (parse_write_buffer_into_params(wr_buf, wr_buf_size,
2053 					    (long *)param, buf,
2054 					    max_param_num,
2055 					    &param_nums)) {
2056 		kfree(wr_buf);
2057 		return -EINVAL;
2058 	}
2059 
2060 	if (param_nums <= 0) {
2061 		DRM_DEBUG_DRIVER("user data not be read\n");
2062 		kfree(wr_buf);
2063 		return -EINVAL;
2064 	}
2065 
2066 	for (i = 0; i < MAX_PIPES; i++) {
2067 		pipe_ctx = &aconnector->dc_link->dc->current_state->res_ctx.pipe_ctx[i];
2068 		if (pipe_ctx->stream &&
2069 		    pipe_ctx->stream->link == aconnector->dc_link &&
2070 		    pipe_ctx->stream->sink &&
2071 		    pipe_ctx->stream->sink == aconnector->dc_sink)
2072 			break;
2073 	}
2074 
2075 	if (!pipe_ctx->stream)
2076 		goto done;
2077 
2078 	// Get CRTC state
2079 	mutex_lock(&dev->mode_config.mutex);
2080 	drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
2081 
2082 	if (connector->state == NULL)
2083 		goto unlock;
2084 
2085 	crtc = connector->state->crtc;
2086 	if (crtc == NULL)
2087 		goto unlock;
2088 
2089 	drm_modeset_lock(&crtc->mutex, NULL);
2090 	if (crtc->state == NULL)
2091 		goto unlock;
2092 
2093 	dm_crtc_state = to_dm_crtc_state(crtc->state);
2094 	if (dm_crtc_state->stream == NULL)
2095 		goto unlock;
2096 
2097 	if (param[0] > 0)
2098 		aconnector->dsc_settings.dsc_num_slices_v = DIV_ROUND_UP(
2099 					pipe_ctx->stream->timing.v_addressable,
2100 					param[0]);
2101 	else
2102 		aconnector->dsc_settings.dsc_num_slices_v = 0;
2103 
2104 	dm_crtc_state->dsc_force_changed = true;
2105 
2106 unlock:
2107 	if (crtc)
2108 		drm_modeset_unlock(&crtc->mutex);
2109 	drm_modeset_unlock(&dev->mode_config.connection_mutex);
2110 	mutex_unlock(&dev->mode_config.mutex);
2111 
2112 done:
2113 	kfree(wr_buf);
2114 	return size;
2115 }
2116 
2117 /* function: read DSC target rate on the connector in bits per pixel
2118  *
2119  * The read function: dp_dsc_bits_per_pixel_read
2120  * returns target rate of compression in bits per pixel
2121  * The return is an integer: 0 or other positive integer
2122  *
2123  * Access it with the following command:
2124  *
2125  *	cat /sys/kernel/debug/dri/0/DP-X/dsc_bits_per_pixel
2126  *
2127  *  0 - means that DSC is disabled
2128  */
2129 static ssize_t dp_dsc_bits_per_pixel_read(struct file *f, char __user *buf,
2130 				    size_t size, loff_t *pos)
2131 {
2132 	char *rd_buf = NULL;
2133 	struct amdgpu_dm_connector *aconnector = file_inode(f)->i_private;
2134 	struct display_stream_compressor *dsc;
2135 	struct dcn_dsc_state dsc_state = {0};
2136 	const uint32_t rd_buf_size = 100;
2137 	struct pipe_ctx *pipe_ctx;
2138 	ssize_t result = 0;
2139 	int i, r, str_len = 30;
2140 
2141 	rd_buf = kcalloc(rd_buf_size, sizeof(char), GFP_KERNEL);
2142 
2143 	if (!rd_buf)
2144 		return -ENOMEM;
2145 
2146 	for (i = 0; i < MAX_PIPES; i++) {
2147 		pipe_ctx = &aconnector->dc_link->dc->current_state->res_ctx.pipe_ctx[i];
2148 		if (pipe_ctx->stream &&
2149 		    pipe_ctx->stream->link == aconnector->dc_link &&
2150 		    pipe_ctx->stream->sink &&
2151 		    pipe_ctx->stream->sink == aconnector->dc_sink)
2152 			break;
2153 	}
2154 
2155 	dsc = pipe_ctx->stream_res.dsc;
2156 	if (dsc)
2157 		dsc->funcs->dsc_read_state(dsc, &dsc_state);
2158 
2159 	snprintf(rd_buf, str_len,
2160 		"%d\n",
2161 		dsc_state.dsc_bits_per_pixel);
2162 
2163 	while (size) {
2164 		if (*pos >= rd_buf_size)
2165 			break;
2166 
2167 		r = put_user(*(rd_buf + result), buf);
2168 		if (r) {
2169 			kfree(rd_buf);
2170 			return r; /* r = -EFAULT */
2171 		}
2172 
2173 		buf += 1;
2174 		size -= 1;
2175 		*pos += 1;
2176 		result += 1;
2177 	}
2178 
2179 	kfree(rd_buf);
2180 	return result;
2181 }
2182 
2183 /* function: write DSC target rate in bits per pixel
2184  *
2185  * The write function: dp_dsc_bits_per_pixel_write
2186  * overwrites automatically generated DSC configuration
2187  * of DSC target bit rate.
2188  *
2189  * Also the user has to write bpp in hexidecimal
2190  * rather than in decimal.
2191  *
2192  * Writing DSC settings is done with the following command:
2193  * - To force overwrite rate (example sets to 256 bpp x 1/16):
2194  *
2195  *	echo 0x100 > /sys/kernel/debug/dri/0/DP-X/dsc_bits_per_pixel
2196  *
2197  *  - To stop overwriting and let driver find the optimal rate,
2198  * set the rate to zero:
2199  *
2200  *	echo 0x0 > /sys/kernel/debug/dri/0/DP-X/dsc_bits_per_pixel
2201  *
2202  */
2203 static ssize_t dp_dsc_bits_per_pixel_write(struct file *f, const char __user *buf,
2204 				     size_t size, loff_t *pos)
2205 {
2206 	struct amdgpu_dm_connector *aconnector = file_inode(f)->i_private;
2207 	struct drm_connector *connector = &aconnector->base;
2208 	struct drm_device *dev = connector->dev;
2209 	struct drm_crtc *crtc = NULL;
2210 	struct dm_crtc_state *dm_crtc_state = NULL;
2211 	struct pipe_ctx *pipe_ctx;
2212 	int i;
2213 	char *wr_buf = NULL;
2214 	uint32_t wr_buf_size = 42;
2215 	int max_param_num = 1;
2216 	uint8_t param_nums = 0;
2217 	long param[1] = {0};
2218 
2219 	if (size == 0)
2220 		return -EINVAL;
2221 
2222 	wr_buf = kcalloc(wr_buf_size, sizeof(char), GFP_KERNEL);
2223 
2224 	if (!wr_buf) {
2225 		DRM_DEBUG_DRIVER("no memory to allocate write buffer\n");
2226 		return -ENOSPC;
2227 	}
2228 
2229 	if (parse_write_buffer_into_params(wr_buf, wr_buf_size,
2230 					    (long *)param, buf,
2231 					    max_param_num,
2232 					    &param_nums)) {
2233 		kfree(wr_buf);
2234 		return -EINVAL;
2235 	}
2236 
2237 	if (param_nums <= 0) {
2238 		DRM_DEBUG_DRIVER("user data not be read\n");
2239 		kfree(wr_buf);
2240 		return -EINVAL;
2241 	}
2242 
2243 	for (i = 0; i < MAX_PIPES; i++) {
2244 		pipe_ctx = &aconnector->dc_link->dc->current_state->res_ctx.pipe_ctx[i];
2245 		if (pipe_ctx->stream &&
2246 		    pipe_ctx->stream->link == aconnector->dc_link &&
2247 		    pipe_ctx->stream->sink &&
2248 		    pipe_ctx->stream->sink == aconnector->dc_sink)
2249 			break;
2250 	}
2251 
2252 	if (!pipe_ctx->stream)
2253 		goto done;
2254 
2255 	// Get CRTC state
2256 	mutex_lock(&dev->mode_config.mutex);
2257 	drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
2258 
2259 	if (connector->state == NULL)
2260 		goto unlock;
2261 
2262 	crtc = connector->state->crtc;
2263 	if (crtc == NULL)
2264 		goto unlock;
2265 
2266 	drm_modeset_lock(&crtc->mutex, NULL);
2267 	if (crtc->state == NULL)
2268 		goto unlock;
2269 
2270 	dm_crtc_state = to_dm_crtc_state(crtc->state);
2271 	if (dm_crtc_state->stream == NULL)
2272 		goto unlock;
2273 
2274 	aconnector->dsc_settings.dsc_bits_per_pixel = param[0];
2275 
2276 	dm_crtc_state->dsc_force_changed = true;
2277 
2278 unlock:
2279 	if (crtc)
2280 		drm_modeset_unlock(&crtc->mutex);
2281 	drm_modeset_unlock(&dev->mode_config.connection_mutex);
2282 	mutex_unlock(&dev->mode_config.mutex);
2283 
2284 done:
2285 	kfree(wr_buf);
2286 	return size;
2287 }
2288 
2289 /* function: read DSC picture width parameter on the connector
2290  *
2291  * The read function: dp_dsc_pic_width_read
2292  * returns dsc picture width used in the current configuration
2293  * It is the same as h_addressable of the current
2294  * display's timing
2295  * The return is an integer: 0 or other positive integer
2296  * If 0 then DSC is disabled.
2297  *
2298  * Access it with the following command:
2299  *
2300  *	cat /sys/kernel/debug/dri/0/DP-X/dsc_pic_width
2301  *
2302  * 0 - means that DSC is disabled
2303  */
2304 static ssize_t dp_dsc_pic_width_read(struct file *f, char __user *buf,
2305 				    size_t size, loff_t *pos)
2306 {
2307 	char *rd_buf = NULL;
2308 	struct amdgpu_dm_connector *aconnector = file_inode(f)->i_private;
2309 	struct display_stream_compressor *dsc;
2310 	struct dcn_dsc_state dsc_state = {0};
2311 	const uint32_t rd_buf_size = 100;
2312 	struct pipe_ctx *pipe_ctx;
2313 	ssize_t result = 0;
2314 	int i, r, str_len = 30;
2315 
2316 	rd_buf = kcalloc(rd_buf_size, sizeof(char), GFP_KERNEL);
2317 
2318 	if (!rd_buf)
2319 		return -ENOMEM;
2320 
2321 	for (i = 0; i < MAX_PIPES; i++) {
2322 		pipe_ctx = &aconnector->dc_link->dc->current_state->res_ctx.pipe_ctx[i];
2323 		if (pipe_ctx->stream &&
2324 		    pipe_ctx->stream->link == aconnector->dc_link &&
2325 		    pipe_ctx->stream->sink &&
2326 		    pipe_ctx->stream->sink == aconnector->dc_sink)
2327 			break;
2328 	}
2329 
2330 	dsc = pipe_ctx->stream_res.dsc;
2331 	if (dsc)
2332 		dsc->funcs->dsc_read_state(dsc, &dsc_state);
2333 
2334 	snprintf(rd_buf, str_len,
2335 		"%d\n",
2336 		dsc_state.dsc_pic_width);
2337 
2338 	while (size) {
2339 		if (*pos >= rd_buf_size)
2340 			break;
2341 
2342 		r = put_user(*(rd_buf + result), buf);
2343 		if (r) {
2344 			kfree(rd_buf);
2345 			return r; /* r = -EFAULT */
2346 		}
2347 
2348 		buf += 1;
2349 		size -= 1;
2350 		*pos += 1;
2351 		result += 1;
2352 	}
2353 
2354 	kfree(rd_buf);
2355 	return result;
2356 }
2357 
2358 static ssize_t dp_dsc_pic_height_read(struct file *f, char __user *buf,
2359 				    size_t size, loff_t *pos)
2360 {
2361 	char *rd_buf = NULL;
2362 	struct amdgpu_dm_connector *aconnector = file_inode(f)->i_private;
2363 	struct display_stream_compressor *dsc;
2364 	struct dcn_dsc_state dsc_state = {0};
2365 	const uint32_t rd_buf_size = 100;
2366 	struct pipe_ctx *pipe_ctx;
2367 	ssize_t result = 0;
2368 	int i, r, str_len = 30;
2369 
2370 	rd_buf = kcalloc(rd_buf_size, sizeof(char), GFP_KERNEL);
2371 
2372 	if (!rd_buf)
2373 		return -ENOMEM;
2374 
2375 	for (i = 0; i < MAX_PIPES; i++) {
2376 		pipe_ctx = &aconnector->dc_link->dc->current_state->res_ctx.pipe_ctx[i];
2377 		if (pipe_ctx->stream &&
2378 		    pipe_ctx->stream->link == aconnector->dc_link &&
2379 		    pipe_ctx->stream->sink &&
2380 		    pipe_ctx->stream->sink == aconnector->dc_sink)
2381 			break;
2382 	}
2383 
2384 	dsc = pipe_ctx->stream_res.dsc;
2385 	if (dsc)
2386 		dsc->funcs->dsc_read_state(dsc, &dsc_state);
2387 
2388 	snprintf(rd_buf, str_len,
2389 		"%d\n",
2390 		dsc_state.dsc_pic_height);
2391 
2392 	while (size) {
2393 		if (*pos >= rd_buf_size)
2394 			break;
2395 
2396 		r = put_user(*(rd_buf + result), buf);
2397 		if (r) {
2398 			kfree(rd_buf);
2399 			return r; /* r = -EFAULT */
2400 		}
2401 
2402 		buf += 1;
2403 		size -= 1;
2404 		*pos += 1;
2405 		result += 1;
2406 	}
2407 
2408 	kfree(rd_buf);
2409 	return result;
2410 }
2411 
2412 /* function: read DSC chunk size parameter on the connector
2413  *
2414  * The read function: dp_dsc_chunk_size_read
2415  * returns dsc chunk size set in the current configuration
2416  * The value is calculated automatically by DSC code
2417  * and depends on slice parameters and bpp target rate
2418  * The return is an integer: 0 or other positive integer
2419  * If 0 then DSC is disabled.
2420  *
2421  * Access it with the following command:
2422  *
2423  *	cat /sys/kernel/debug/dri/0/DP-X/dsc_chunk_size
2424  *
2425  * 0 - means that DSC is disabled
2426  */
2427 static ssize_t dp_dsc_chunk_size_read(struct file *f, char __user *buf,
2428 				    size_t size, loff_t *pos)
2429 {
2430 	char *rd_buf = NULL;
2431 	struct amdgpu_dm_connector *aconnector = file_inode(f)->i_private;
2432 	struct display_stream_compressor *dsc;
2433 	struct dcn_dsc_state dsc_state = {0};
2434 	const uint32_t rd_buf_size = 100;
2435 	struct pipe_ctx *pipe_ctx;
2436 	ssize_t result = 0;
2437 	int i, r, str_len = 30;
2438 
2439 	rd_buf = kcalloc(rd_buf_size, sizeof(char), GFP_KERNEL);
2440 
2441 	if (!rd_buf)
2442 		return -ENOMEM;
2443 
2444 	for (i = 0; i < MAX_PIPES; i++) {
2445 		pipe_ctx = &aconnector->dc_link->dc->current_state->res_ctx.pipe_ctx[i];
2446 		if (pipe_ctx->stream &&
2447 		    pipe_ctx->stream->link == aconnector->dc_link &&
2448 		    pipe_ctx->stream->sink &&
2449 		    pipe_ctx->stream->sink == aconnector->dc_sink)
2450 			break;
2451 	}
2452 
2453 	dsc = pipe_ctx->stream_res.dsc;
2454 	if (dsc)
2455 		dsc->funcs->dsc_read_state(dsc, &dsc_state);
2456 
2457 	snprintf(rd_buf, str_len,
2458 		"%d\n",
2459 		dsc_state.dsc_chunk_size);
2460 
2461 	while (size) {
2462 		if (*pos >= rd_buf_size)
2463 			break;
2464 
2465 		r = put_user(*(rd_buf + result), buf);
2466 		if (r) {
2467 			kfree(rd_buf);
2468 			return r; /* r = -EFAULT */
2469 		}
2470 
2471 		buf += 1;
2472 		size -= 1;
2473 		*pos += 1;
2474 		result += 1;
2475 	}
2476 
2477 	kfree(rd_buf);
2478 	return result;
2479 }
2480 
2481 /* function: read DSC slice bpg offset on the connector
2482  *
2483  * The read function: dp_dsc_slice_bpg_offset_read
2484  * returns dsc bpg slice offset set in the current configuration
2485  * The value is calculated automatically by DSC code
2486  * and depends on slice parameters and bpp target rate
2487  * The return is an integer: 0 or other positive integer
2488  * If 0 then DSC is disabled.
2489  *
2490  * Access it with the following command:
2491  *
2492  *	cat /sys/kernel/debug/dri/0/DP-X/dsc_slice_bpg_offset
2493  *
2494  * 0 - means that DSC is disabled
2495  */
2496 static ssize_t dp_dsc_slice_bpg_offset_read(struct file *f, char __user *buf,
2497 				    size_t size, loff_t *pos)
2498 {
2499 	char *rd_buf = NULL;
2500 	struct amdgpu_dm_connector *aconnector = file_inode(f)->i_private;
2501 	struct display_stream_compressor *dsc;
2502 	struct dcn_dsc_state dsc_state = {0};
2503 	const uint32_t rd_buf_size = 100;
2504 	struct pipe_ctx *pipe_ctx;
2505 	ssize_t result = 0;
2506 	int i, r, str_len = 30;
2507 
2508 	rd_buf = kcalloc(rd_buf_size, sizeof(char), GFP_KERNEL);
2509 
2510 	if (!rd_buf)
2511 		return -ENOMEM;
2512 
2513 	for (i = 0; i < MAX_PIPES; i++) {
2514 		pipe_ctx = &aconnector->dc_link->dc->current_state->res_ctx.pipe_ctx[i];
2515 		if (pipe_ctx->stream &&
2516 		    pipe_ctx->stream->link == aconnector->dc_link &&
2517 		    pipe_ctx->stream->sink &&
2518 		    pipe_ctx->stream->sink == aconnector->dc_sink)
2519 			break;
2520 	}
2521 
2522 	dsc = pipe_ctx->stream_res.dsc;
2523 	if (dsc)
2524 		dsc->funcs->dsc_read_state(dsc, &dsc_state);
2525 
2526 	snprintf(rd_buf, str_len,
2527 		"%d\n",
2528 		dsc_state.dsc_slice_bpg_offset);
2529 
2530 	while (size) {
2531 		if (*pos >= rd_buf_size)
2532 			break;
2533 
2534 		r = put_user(*(rd_buf + result), buf);
2535 		if (r) {
2536 			kfree(rd_buf);
2537 			return r; /* r = -EFAULT */
2538 		}
2539 
2540 		buf += 1;
2541 		size -= 1;
2542 		*pos += 1;
2543 		result += 1;
2544 	}
2545 
2546 	kfree(rd_buf);
2547 	return result;
2548 }
2549 
2550 
2551 /*
2552  * function description: Read max_requested_bpc property from the connector
2553  *
2554  * Access it with the following command:
2555  *
2556  *	cat /sys/kernel/debug/dri/0/DP-X/max_bpc
2557  *
2558  */
2559 static ssize_t dp_max_bpc_read(struct file *f, char __user *buf,
2560 		size_t size, loff_t *pos)
2561 {
2562 	struct amdgpu_dm_connector *aconnector = file_inode(f)->i_private;
2563 	struct drm_connector *connector = &aconnector->base;
2564 	struct drm_device *dev = connector->dev;
2565 	struct dm_connector_state *state;
2566 	ssize_t result = 0;
2567 	char *rd_buf = NULL;
2568 	char *rd_buf_ptr = NULL;
2569 	const uint32_t rd_buf_size = 10;
2570 	int r;
2571 
2572 	rd_buf = kcalloc(rd_buf_size, sizeof(char), GFP_KERNEL);
2573 
2574 	if (!rd_buf)
2575 		return -ENOMEM;
2576 
2577 	mutex_lock(&dev->mode_config.mutex);
2578 	drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
2579 
2580 	if (connector->state == NULL)
2581 		goto unlock;
2582 
2583 	state = to_dm_connector_state(connector->state);
2584 
2585 	rd_buf_ptr = rd_buf;
2586 	snprintf(rd_buf_ptr, rd_buf_size,
2587 		"%u\n",
2588 		state->base.max_requested_bpc);
2589 
2590 	while (size) {
2591 		if (*pos >= rd_buf_size)
2592 			break;
2593 
2594 		r = put_user(*(rd_buf + result), buf);
2595 		if (r) {
2596 			result = r; /* r = -EFAULT */
2597 			goto unlock;
2598 		}
2599 		buf += 1;
2600 		size -= 1;
2601 		*pos += 1;
2602 		result += 1;
2603 	}
2604 unlock:
2605 	drm_modeset_unlock(&dev->mode_config.connection_mutex);
2606 	mutex_unlock(&dev->mode_config.mutex);
2607 	kfree(rd_buf);
2608 	return result;
2609 }
2610 
2611 
2612 /*
2613  * function description: Set max_requested_bpc property on the connector
2614  *
2615  * This function will not force the input BPC on connector, it will only
2616  * change the max value. This is equivalent to setting max_bpc through
2617  * xrandr.
2618  *
2619  * The BPC value written must be >= 6 and <= 16. Values outside of this
2620  * range will result in errors.
2621  *
2622  * BPC values:
2623  *	0x6 - 6 BPC
2624  *	0x8 - 8 BPC
2625  *	0xa - 10 BPC
2626  *	0xc - 12 BPC
2627  *	0x10 - 16 BPC
2628  *
2629  * Write the max_bpc in the following way:
2630  *
2631  * echo 0x6 > /sys/kernel/debug/dri/0/DP-X/max_bpc
2632  *
2633  */
2634 static ssize_t dp_max_bpc_write(struct file *f, const char __user *buf,
2635 				     size_t size, loff_t *pos)
2636 {
2637 	struct amdgpu_dm_connector *aconnector = file_inode(f)->i_private;
2638 	struct drm_connector *connector = &aconnector->base;
2639 	struct dm_connector_state *state;
2640 	struct drm_device *dev = connector->dev;
2641 	char *wr_buf = NULL;
2642 	uint32_t wr_buf_size = 42;
2643 	int max_param_num = 1;
2644 	long param[1] = {0};
2645 	uint8_t param_nums = 0;
2646 
2647 	if (size == 0)
2648 		return -EINVAL;
2649 
2650 	wr_buf = kcalloc(wr_buf_size, sizeof(char), GFP_KERNEL);
2651 
2652 	if (!wr_buf) {
2653 		DRM_DEBUG_DRIVER("no memory to allocate write buffer\n");
2654 		return -ENOSPC;
2655 	}
2656 
2657 	if (parse_write_buffer_into_params(wr_buf, wr_buf_size,
2658 					   (long *)param, buf,
2659 					   max_param_num,
2660 					   &param_nums)) {
2661 		kfree(wr_buf);
2662 		return -EINVAL;
2663 	}
2664 
2665 	if (param_nums <= 0) {
2666 		DRM_DEBUG_DRIVER("user data not be read\n");
2667 		kfree(wr_buf);
2668 		return -EINVAL;
2669 	}
2670 
2671 	if (param[0] < 6 || param[0] > 16) {
2672 		DRM_DEBUG_DRIVER("bad max_bpc value\n");
2673 		kfree(wr_buf);
2674 		return -EINVAL;
2675 	}
2676 
2677 	mutex_lock(&dev->mode_config.mutex);
2678 	drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
2679 
2680 	if (connector->state == NULL)
2681 		goto unlock;
2682 
2683 	state = to_dm_connector_state(connector->state);
2684 	state->base.max_requested_bpc = param[0];
2685 unlock:
2686 	drm_modeset_unlock(&dev->mode_config.connection_mutex);
2687 	mutex_unlock(&dev->mode_config.mutex);
2688 
2689 	kfree(wr_buf);
2690 	return size;
2691 }
2692 
2693 /*
2694  * IPS status.  Read only.
2695  *
2696  * Example usage: cat /sys/kernel/debug/dri/0/amdgpu_dm_ips_status
2697  */
2698 static int ips_status_show(struct seq_file *m, void *unused)
2699 {
2700 	struct amdgpu_device *adev = m->private;
2701 	struct dc *dc = adev->dm.dc;
2702 	struct dc_dmub_srv *dc_dmub_srv;
2703 
2704 	seq_printf(m, "IPS config: %d\n", dc->config.disable_ips);
2705 	seq_printf(m, "Idle optimization: %d\n", dc->idle_optimizations_allowed);
2706 
2707 	if (adev->dm.idle_workqueue) {
2708 		seq_printf(m, "Idle workqueue - enabled: %d\n", adev->dm.idle_workqueue->enable);
2709 		seq_printf(m, "Idle workqueue - running: %d\n", adev->dm.idle_workqueue->running);
2710 	}
2711 
2712 	dc_dmub_srv = dc->ctx->dmub_srv;
2713 	if (dc_dmub_srv && dc_dmub_srv->dmub) {
2714 		uint32_t rcg_count, ips1_count, ips2_count;
2715 		volatile const struct dmub_shared_state_ips_fw *ips_fw =
2716 			&dc_dmub_srv->dmub->shared_state[DMUB_SHARED_SHARE_FEATURE__IPS_FW].data.ips_fw;
2717 		rcg_count = ips_fw->rcg_entry_count;
2718 		ips1_count = ips_fw->ips1_entry_count;
2719 		ips2_count = ips_fw->ips2_entry_count;
2720 		seq_printf(m, "entry counts: rcg=%u ips1=%u ips2=%u\n",
2721 			   rcg_count,
2722 			   ips1_count,
2723 			   ips2_count);
2724 		rcg_count = ips_fw->rcg_exit_count;
2725 		ips1_count = ips_fw->ips1_exit_count;
2726 		ips2_count = ips_fw->ips2_exit_count;
2727 		seq_printf(m, "exit counts: rcg=%u ips1=%u ips2=%u\n",
2728 			   rcg_count,
2729 			   ips1_count,
2730 			   ips2_count);
2731 	}
2732 	return 0;
2733 }
2734 
2735 /*
2736  * IPS residency information from DMUB service. Read only.
2737  *
2738  * For time-window (segment) measurement:
2739  *	1) echo 1 > /sys/kernel/debug/dri/0/amdgpu_dm_ips_residency_cntl
2740  *	2) sleep <seconds>
2741  *	3) echo 0 > /sys/kernel/debug/dri/0/amdgpu_dm_ips_residency_cntl
2742  *	4) cat /sys/kernel/debug/dri/0/amdgpu_dm_ips_residency
2743  */
2744 static int ips_residency_show(struct seq_file *m, void *unused)
2745 {
2746 	struct amdgpu_device *adev = m->private;
2747 	struct dc *dc = adev->dm.dc;
2748 	uint8_t panel_inst = 0;
2749 	enum ips_residency_mode mode;
2750 	struct dmub_ips_residency_info info;
2751 
2752 	mutex_lock(&adev->dm.dc_lock);
2753 
2754 	mode = IPS_RESIDENCY__IPS1_RCG;
2755 	if (!dc_dmub_srv_ips_query_residency_info(dc->ctx, panel_inst, &info, mode)) {
2756 		seq_printf(m, "ISP query failed\n");
2757 	} else {
2758 		unsigned int pct, frac;
2759 		pct = info.residency_millipercent / 1000;
2760 		frac = info.residency_millipercent % 1000;
2761 
2762 		seq_printf(m, "IPS residency: %u.%03u%% \n", pct, frac);
2763 		seq_printf(m, "    entry_counter: %u\n", info.entry_counter);
2764 		seq_printf(m, "    total_time_us: %llu\n",
2765 			(unsigned long long)info.total_time_us);
2766 		seq_printf(m, "    total_inactive_time_us: %llu\n",
2767 			(unsigned long long)info.total_inactive_time_us);
2768 	}
2769 	mutex_unlock(&adev->dm.dc_lock);
2770 	return 0;
2771 }
2772 
2773 static int ips_residency_cntl_get(void *data, u64 *val)
2774 {
2775 	*val = 0;
2776 	return 0;
2777 }
2778 
2779 static int ips_residency_cntl_set(void *data, u64 val)
2780 {
2781 	struct amdgpu_device *adev = data;
2782 	struct dc *dc = adev->dm.dc;
2783 	uint8_t panel_inst = 0;
2784 	int ret = 0;
2785 
2786 	mutex_lock(&adev->dm.dc_lock);
2787 	if (!dc_dmub_srv_ips_residency_cntl(dc->ctx, panel_inst, !!val))
2788 		ret = -EIO;
2789 	mutex_unlock(&adev->dm.dc_lock);
2790 
2791 	return ret;
2792 }
2793 
2794 /*
2795  * Backlight at this moment.  Read only.
2796  * As written to display, taking ABM and backlight lut into account.
2797  * Ranges from 0x0 to 0x10000 (= 100% PWM)
2798  *
2799  * Example usage: cat /sys/kernel/debug/dri/0/eDP-1/current_backlight
2800  */
2801 static int current_backlight_show(struct seq_file *m, void *unused)
2802 {
2803 	struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(m->private);
2804 	struct dc_link *link = aconnector->dc_link;
2805 	unsigned int backlight;
2806 
2807 	backlight = dc_link_get_backlight_level(link);
2808 	seq_printf(m, "0x%x\n", backlight);
2809 
2810 	return 0;
2811 }
2812 
2813 /*
2814  * Backlight value that is being approached.  Read only.
2815  * As written to display, taking ABM and backlight lut into account.
2816  * Ranges from 0x0 to 0x10000 (= 100% PWM)
2817  *
2818  * Example usage: cat /sys/kernel/debug/dri/0/eDP-1/target_backlight
2819  */
2820 static int target_backlight_show(struct seq_file *m, void *unused)
2821 {
2822 	struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(m->private);
2823 	struct dc_link *link = aconnector->dc_link;
2824 	unsigned int backlight;
2825 
2826 	backlight = dc_link_get_target_backlight_pwm(link);
2827 	seq_printf(m, "0x%x\n", backlight);
2828 
2829 	return 0;
2830 }
2831 
2832 /*
2833  * function description: Determine if the connector is mst connector
2834  *
2835  * This function helps to determine whether a connector is a mst connector.
2836  * - "root" stands for the root connector of the topology
2837  * - "branch" stands for branch device of the topology
2838  * - "end" stands for leaf node connector of the topology
2839  * - "no" stands for the connector is not a device of a mst topology
2840  * Access it with the following command:
2841  *
2842  *	cat /sys/kernel/debug/dri/0/DP-X/is_mst_connector
2843  *
2844  */
2845 static int dp_is_mst_connector_show(struct seq_file *m, void *unused)
2846 {
2847 	struct drm_connector *connector = m->private;
2848 	struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
2849 	struct drm_dp_mst_topology_mgr *mgr = NULL;
2850 	struct drm_dp_mst_port *port = NULL;
2851 	char *role = NULL;
2852 
2853 	mutex_lock(&aconnector->hpd_lock);
2854 
2855 	if (aconnector->mst_mgr.mst_state) {
2856 		role = "root";
2857 	} else if (aconnector->mst_root &&
2858 		aconnector->mst_root->mst_mgr.mst_state) {
2859 
2860 		role = "end";
2861 
2862 		mgr = &aconnector->mst_root->mst_mgr;
2863 		port = aconnector->mst_output_port;
2864 
2865 		drm_modeset_lock(&mgr->base.lock, NULL);
2866 		if (port->pdt == DP_PEER_DEVICE_MST_BRANCHING &&
2867 			port->mcs)
2868 			role = "branch";
2869 		drm_modeset_unlock(&mgr->base.lock);
2870 
2871 	} else {
2872 		role = "no";
2873 	}
2874 
2875 	seq_printf(m, "%s\n", role);
2876 
2877 	mutex_unlock(&aconnector->hpd_lock);
2878 
2879 	return 0;
2880 }
2881 
2882 /*
2883  * function description: Read out the mst progress status
2884  *
2885  * This function helps to determine the mst progress status of
2886  * a mst connector.
2887  *
2888  * Access it with the following command:
2889  *
2890  *	cat /sys/kernel/debug/dri/0/DP-X/mst_progress_status
2891  *
2892  */
2893 static int dp_mst_progress_status_show(struct seq_file *m, void *unused)
2894 {
2895 	struct drm_connector *connector = m->private;
2896 	struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
2897 	struct amdgpu_device *adev = drm_to_adev(connector->dev);
2898 	int i;
2899 
2900 	mutex_lock(&aconnector->hpd_lock);
2901 	mutex_lock(&adev->dm.dc_lock);
2902 
2903 	if (aconnector->mst_status == MST_STATUS_DEFAULT) {
2904 		seq_puts(m, "disabled\n");
2905 	} else {
2906 		for (i = 0; i < sizeof(mst_progress_status)/sizeof(char *); i++)
2907 			seq_printf(m, "%s:%s\n",
2908 				mst_progress_status[i],
2909 				aconnector->mst_status & BIT(i) ? "done" : "not_done");
2910 	}
2911 
2912 	mutex_unlock(&adev->dm.dc_lock);
2913 	mutex_unlock(&aconnector->hpd_lock);
2914 
2915 	return 0;
2916 }
2917 
2918 /*
2919  * Reports whether the connected display is a USB4 DPIA tunneled display
2920  * Example usage: cat /sys/kernel/debug/dri/0/DP-8/is_dpia_link
2921  */
2922 static int is_dpia_link_show(struct seq_file *m, void *data)
2923 {
2924 	struct drm_connector *connector = m->private;
2925 	struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
2926 	struct dc_link *link = aconnector->dc_link;
2927 
2928 	if (connector->status != connector_status_connected)
2929 		return -ENODEV;
2930 
2931 	seq_printf(m, "%s\n", (link->ep_type == DISPLAY_ENDPOINT_USB4_DPIA) ? "yes" :
2932 				(link->ep_type == DISPLAY_ENDPOINT_PHY) ? "no" : "unknown");
2933 
2934 	return 0;
2935 }
2936 
2937 /**
2938  * hdmi_cec_state_show - Read out the HDMI-CEC feature status
2939  * @m: sequence file.
2940  * @data: unused.
2941  *
2942  * Return 0 on success
2943  */
2944 static int hdmi_cec_state_show(struct seq_file *m, void *data)
2945 {
2946 	struct drm_connector *connector = m->private;
2947 	struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector);
2948 
2949 	seq_printf(m, "%s:%d\n", connector->name, connector->base.id);
2950 	seq_printf(m, "HDMI-CEC status: %d\n", aconnector->notifier ? 1 : 0);
2951 
2952 	return 0;
2953 }
2954 
2955 /**
2956  * hdmi_cec_state_write - Enable/Disable HDMI-CEC feature from driver side
2957  * @f: file structure.
2958  * @buf: userspace buffer. set to '1' to enable; '0' to disable cec feature.
2959  * @size: size of buffer from userpsace.
2960  * @pos: unused.
2961  *
2962  * Return size on success, error code on failure
2963  */
2964 static ssize_t hdmi_cec_state_write(struct file *f, const char __user *buf,
2965 				    size_t size, loff_t *pos)
2966 {
2967 	int ret;
2968 	bool enable;
2969 	struct amdgpu_dm_connector *aconnector = file_inode(f)->i_private;
2970 	struct drm_device *ddev = aconnector->base.dev;
2971 
2972 	if (size == 0)
2973 		return -EINVAL;
2974 
2975 	ret = kstrtobool_from_user(buf, size, &enable);
2976 	if (ret) {
2977 		drm_dbg_driver(ddev, "invalid user data !\n");
2978 		return ret;
2979 	}
2980 
2981 	if (enable) {
2982 		if (aconnector->notifier)
2983 			return -EINVAL;
2984 		ret = amdgpu_dm_initialize_hdmi_connector(aconnector);
2985 		if (ret)
2986 			return ret;
2987 		amdgpu_dm_hdmi_cec_set_edid(aconnector);
2988 	} else {
2989 		if (!aconnector->notifier)
2990 			return -EINVAL;
2991 		cec_notifier_conn_unregister(aconnector->notifier);
2992 		aconnector->notifier = NULL;
2993 	}
2994 
2995 	return size;
2996 }
2997 
2998 /**
2999  * hdmi_automation_enable - Enable/Disable HDMI automation feature
3000  * @f: file structure.
3001  * @buf: userspace buffer. set to '1' to enable; '0' to disable automation feature.
3002  * @size: size of buffer from userpsace.
3003  * @pos: unused.
3004  *
3005  * Return size on success, error code on failure
3006  */
3007 static ssize_t hdmi_automation_enable(struct file *f, const char __user *buf,
3008 	size_t size, loff_t *pos)
3009 {
3010 	struct amdgpu_dm_connector *aconnector = file_inode(f)->i_private;
3011 	char *wr_buf = NULL;
3012 	const uint32_t wr_buf_size = 40;
3013 	int max_param_num = 1;
3014 	uint8_t param_nums = 0;
3015 	long param[2];
3016 	bool hdmi_comp_auto;
3017 
3018 	if (size == 0)
3019 		return -EINVAL;
3020 
3021 	wr_buf = kcalloc(wr_buf_size, sizeof(char), GFP_KERNEL);
3022 	if (!wr_buf)
3023 		return -ENOSPC;
3024 
3025 	if (parse_write_buffer_into_params(wr_buf, wr_buf_size,
3026 					   (long *)param, buf,
3027 					   max_param_num,
3028 					   &param_nums)) {
3029 		kfree(wr_buf);
3030 		return -EINVAL;
3031 	}
3032 
3033 	if (param_nums <= 0) {
3034 		kfree(wr_buf);
3035 		DRM_DEBUG_DRIVER("user data not be read\n");
3036 		return -EINVAL;
3037 	}
3038 
3039 	switch (param[0]) {
3040 	case 0:
3041 		hdmi_comp_auto = false;
3042 		break;
3043 	case 1:
3044 	default:
3045 		hdmi_comp_auto = true;
3046 		break;
3047 	}
3048 
3049 	/* Persist setting across sink re-detection/hotplug. */
3050 	aconnector->hdmi_comp_auto = hdmi_comp_auto;
3051 
3052 	kfree(wr_buf);
3053 	return size;
3054 }
3055 
3056 DEFINE_SHOW_ATTRIBUTE(dp_dsc_fec_support);
3057 DEFINE_SHOW_ATTRIBUTE(dmub_fw_state);
3058 DEFINE_SHOW_ATTRIBUTE(dmub_tracebuffer);
3059 DEFINE_SHOW_ATTRIBUTE(dp_lttpr_status);
3060 DEFINE_SHOW_ATTRIBUTE(hdcp_sink_capability);
3061 DEFINE_SHOW_ATTRIBUTE(internal_display);
3062 DEFINE_SHOW_ATTRIBUTE(max_downscale_src_width);
3063 DEFINE_SHOW_ATTRIBUTE(odm_combine_segments);
3064 DEFINE_SHOW_ATTRIBUTE(replay_capability);
3065 DEFINE_SHOW_ATTRIBUTE(psr_capability);
3066 DEFINE_SHOW_ATTRIBUTE(dp_is_mst_connector);
3067 DEFINE_SHOW_ATTRIBUTE(dp_mst_progress_status);
3068 DEFINE_SHOW_ATTRIBUTE(is_dpia_link);
3069 DEFINE_SHOW_STORE_ATTRIBUTE(hdmi_cec_state);
3070 
3071 static const struct file_operations dp_dsc_clock_en_debugfs_fops = {
3072 	.owner = THIS_MODULE,
3073 	.read = dp_dsc_clock_en_read,
3074 	.write = dp_dsc_clock_en_write,
3075 	.llseek = default_llseek
3076 };
3077 
3078 static const struct file_operations dp_dsc_slice_width_debugfs_fops = {
3079 	.owner = THIS_MODULE,
3080 	.read = dp_dsc_slice_width_read,
3081 	.write = dp_dsc_slice_width_write,
3082 	.llseek = default_llseek
3083 };
3084 
3085 static const struct file_operations dp_dsc_slice_height_debugfs_fops = {
3086 	.owner = THIS_MODULE,
3087 	.read = dp_dsc_slice_height_read,
3088 	.write = dp_dsc_slice_height_write,
3089 	.llseek = default_llseek
3090 };
3091 
3092 static const struct file_operations dp_dsc_bits_per_pixel_debugfs_fops = {
3093 	.owner = THIS_MODULE,
3094 	.read = dp_dsc_bits_per_pixel_read,
3095 	.write = dp_dsc_bits_per_pixel_write,
3096 	.llseek = default_llseek
3097 };
3098 
3099 static const struct file_operations dp_dsc_pic_width_debugfs_fops = {
3100 	.owner = THIS_MODULE,
3101 	.read = dp_dsc_pic_width_read,
3102 	.llseek = default_llseek
3103 };
3104 
3105 static const struct file_operations dp_dsc_pic_height_debugfs_fops = {
3106 	.owner = THIS_MODULE,
3107 	.read = dp_dsc_pic_height_read,
3108 	.llseek = default_llseek
3109 };
3110 
3111 static const struct file_operations dp_dsc_chunk_size_debugfs_fops = {
3112 	.owner = THIS_MODULE,
3113 	.read = dp_dsc_chunk_size_read,
3114 	.llseek = default_llseek
3115 };
3116 
3117 static const struct file_operations dp_dsc_slice_bpg_offset_debugfs_fops = {
3118 	.owner = THIS_MODULE,
3119 	.read = dp_dsc_slice_bpg_offset_read,
3120 	.llseek = default_llseek
3121 };
3122 
3123 static const struct file_operations trigger_hotplug_debugfs_fops = {
3124 	.owner = THIS_MODULE,
3125 	.write = trigger_hotplug,
3126 	.llseek = default_llseek
3127 };
3128 
3129 static const struct file_operations dp_link_settings_debugfs_fops = {
3130 	.owner = THIS_MODULE,
3131 	.read = dp_link_settings_read,
3132 	.write = dp_link_settings_write,
3133 	.llseek = default_llseek
3134 };
3135 
3136 static const struct file_operations dp_phy_settings_debugfs_fop = {
3137 	.owner = THIS_MODULE,
3138 	.read = dp_phy_settings_read,
3139 	.write = dp_phy_settings_write,
3140 	.llseek = default_llseek
3141 };
3142 
3143 static const struct file_operations dp_phy_test_pattern_fops = {
3144 	.owner = THIS_MODULE,
3145 	.write = dp_phy_test_pattern_debugfs_write,
3146 	.llseek = default_llseek
3147 };
3148 
3149 static const struct file_operations sdp_message_fops = {
3150 	.owner = THIS_MODULE,
3151 	.write = dp_sdp_message_debugfs_write,
3152 	.llseek = default_llseek
3153 };
3154 
3155 static const struct file_operations dp_max_bpc_debugfs_fops = {
3156 	.owner = THIS_MODULE,
3157 	.read = dp_max_bpc_read,
3158 	.write = dp_max_bpc_write,
3159 	.llseek = default_llseek
3160 };
3161 
3162 static const struct file_operations dp_dsc_disable_passthrough_debugfs_fops = {
3163 	.owner = THIS_MODULE,
3164 	.write = dp_dsc_passthrough_set,
3165 	.llseek = default_llseek
3166 };
3167 
3168 static const struct file_operations dp_mst_link_settings_debugfs_fops = {
3169 	.owner = THIS_MODULE,
3170 	.write = dp_mst_link_setting,
3171 	.llseek = default_llseek
3172 };
3173 
3174 static const struct file_operations hdmi_automation_debugfs_fops = {
3175 	.owner = THIS_MODULE,
3176 	.write = hdmi_automation_enable,
3177 	.llseek = default_llseek
3178 };
3179 
3180 static const struct {
3181 	char *name;
3182 	const struct file_operations *fops;
3183 } dp_debugfs_entries[] = {
3184 		{"link_settings", &dp_link_settings_debugfs_fops},
3185 		{"phy_settings", &dp_phy_settings_debugfs_fop},
3186 		{"lttpr_status", &dp_lttpr_status_fops},
3187 		{"test_pattern", &dp_phy_test_pattern_fops},
3188 		{"hdcp_sink_capability", &hdcp_sink_capability_fops},
3189 		{"sdp_message", &sdp_message_fops},
3190 		{"dsc_clock_en", &dp_dsc_clock_en_debugfs_fops},
3191 		{"dsc_slice_width", &dp_dsc_slice_width_debugfs_fops},
3192 		{"dsc_slice_height", &dp_dsc_slice_height_debugfs_fops},
3193 		{"dsc_bits_per_pixel", &dp_dsc_bits_per_pixel_debugfs_fops},
3194 		{"dsc_pic_width", &dp_dsc_pic_width_debugfs_fops},
3195 		{"dsc_pic_height", &dp_dsc_pic_height_debugfs_fops},
3196 		{"dsc_chunk_size", &dp_dsc_chunk_size_debugfs_fops},
3197 		{"dsc_slice_bpg", &dp_dsc_slice_bpg_offset_debugfs_fops},
3198 		{"dp_dsc_fec_support", &dp_dsc_fec_support_fops},
3199 		{"max_bpc", &dp_max_bpc_debugfs_fops},
3200 		{"dsc_disable_passthrough", &dp_dsc_disable_passthrough_debugfs_fops},
3201 		{"is_mst_connector", &dp_is_mst_connector_fops},
3202 		{"mst_progress_status", &dp_mst_progress_status_fops},
3203 		{"is_dpia_link", &is_dpia_link_fops},
3204 		{"mst_link_settings", &dp_mst_link_settings_debugfs_fops}
3205 };
3206 
3207 static const struct {
3208 	char *name;
3209 	const struct file_operations *fops;
3210 } hdmi_debugfs_entries[] = {
3211 		{"hdcp_sink_capability", &hdcp_sink_capability_fops},
3212 		{"hdmi_cec_state", &hdmi_cec_state_fops},
3213 		{"hdmi_automation", &hdmi_automation_debugfs_fops}
3214 };
3215 
3216 /*
3217  * Force a specific pixel encoding for the given connector, overriding the
3218  * encoding that stream validation would otherwise pick. The value is an
3219  * enum dc_pixel_encoding:
3220  *
3221  *   0 - PIXEL_ENCODING_UNDEFINED (no override, default)
3222  *   1 - PIXEL_ENCODING_RGB
3223  *   2 - PIXEL_ENCODING_YCBCR422
3224  *   3 - PIXEL_ENCODING_YCBCR444
3225  *   4 - PIXEL_ENCODING_YCBCR420
3226  */
3227 static int force_yuv_pixel_format_set(void *data, u64 val)
3228 {
3229 	struct amdgpu_dm_connector *connector = data;
3230 
3231 	if (val >= PIXEL_ENCODING_COUNT)
3232 		return -EINVAL;
3233 
3234 	connector->force_yuv_pixel_format = (uint8_t)val;
3235 
3236 	return 0;
3237 }
3238 
3239 /*
3240  * Read back the pixel encoding currently forced on the given connector.
3241  */
3242 static int force_yuv_pixel_format_get(void *data, u64 *val)
3243 {
3244 	struct amdgpu_dm_connector *connector = data;
3245 
3246 	*val = connector->force_yuv_pixel_format;
3247 
3248 	return 0;
3249 }
3250 
3251 DEFINE_DEBUGFS_ATTRIBUTE(force_yuv_pixel_format_fops, force_yuv_pixel_format_get,
3252 			 force_yuv_pixel_format_set, "%llu\n");
3253 
3254 /*
3255  *  Read Replay state
3256  */
3257 static int replay_get_state(void *data, u64 *val)
3258 {
3259 	struct amdgpu_dm_connector *connector = data;
3260 	struct dc_link *link = connector->dc_link;
3261 	struct amdgpu_device *adev = drm_to_adev(connector->base.dev);
3262 	struct dc *dc = adev->dm.dc;
3263 	uint64_t state = REPLAY_STATE_INVALID;
3264 	bool reallow_idle = false;
3265 
3266 	mutex_lock(&adev->dm.dc_lock);
3267 
3268 	if (dc->idle_optimizations_allowed) {
3269 		dc_allow_idle_optimizations(dc, false);
3270 		reallow_idle = true;
3271 	}
3272 
3273 	dc_link_get_replay_state(link, &state);
3274 
3275 	if (reallow_idle)
3276 		dc_allow_idle_optimizations(dc, true);
3277 
3278 	mutex_unlock(&adev->dm.dc_lock);
3279 
3280 	*val = state;
3281 
3282 	return 0;
3283 }
3284 
3285 /*
3286  *  Start / Stop capture Replay residency
3287  */
3288 static int replay_set_residency(void *data, u64 val)
3289 {
3290 	struct amdgpu_dm_connector *connector = data;
3291 	struct dc_link *link = connector->dc_link;
3292 	struct amdgpu_device *adev = drm_to_adev(connector->base.dev);
3293 	struct dc *dc = adev->dm.dc;
3294 	bool is_start = (val != 0);
3295 	u32 residency = 0;
3296 	bool reallow_idle = false;
3297 
3298 	mutex_lock(&adev->dm.dc_lock);
3299 
3300 	if (dc->idle_optimizations_allowed) {
3301 		dc_allow_idle_optimizations(dc, false);
3302 		reallow_idle = true;
3303 	}
3304 
3305 	link->dc->link_srv->edp_replay_residency(link, &residency, is_start, PR_RESIDENCY_MODE_PHY);
3306 
3307 	if (reallow_idle)
3308 		dc_allow_idle_optimizations(dc, true);
3309 
3310 	mutex_unlock(&adev->dm.dc_lock);
3311 
3312 	return 0;
3313 }
3314 
3315 /*
3316  *  Read Replay residency
3317  */
3318 static int replay_get_residency(void *data, u64 *val)
3319 {
3320 	struct amdgpu_dm_connector *connector = data;
3321 	struct dc_link *link = connector->dc_link;
3322 	struct amdgpu_device *adev = drm_to_adev(connector->base.dev);
3323 	struct dc *dc = adev->dm.dc;
3324 	u32 residency = 0;
3325 	bool reallow_idle = false;
3326 
3327 	mutex_lock(&adev->dm.dc_lock);
3328 
3329 	if (dc->idle_optimizations_allowed) {
3330 		dc_allow_idle_optimizations(dc, false);
3331 		reallow_idle = true;
3332 	}
3333 
3334 	link->dc->link_srv->edp_replay_residency(link, &residency, false, PR_RESIDENCY_MODE_PHY);
3335 
3336 	if (reallow_idle)
3337 		dc_allow_idle_optimizations(dc, true);
3338 
3339 	mutex_unlock(&adev->dm.dc_lock);
3340 
3341 	*val = (u64)residency;
3342 
3343 	return 0;
3344 }
3345 
3346 /*
3347  *  Read PSR state
3348  */
3349 static int psr_get(void *data, u64 *val)
3350 {
3351 	struct amdgpu_dm_connector *connector = data;
3352 	struct dc_link *link = connector->dc_link;
3353 	struct amdgpu_device *adev = drm_to_adev(connector->base.dev);
3354 	struct dc *dc = adev->dm.dc;
3355 	enum dc_psr_state state = PSR_STATE0;
3356 	bool reallow_idle = false;
3357 
3358 	mutex_lock(&adev->dm.dc_lock);
3359 
3360 	if (dc->idle_optimizations_allowed) {
3361 		dc_allow_idle_optimizations(dc, false);
3362 		reallow_idle = true;
3363 	}
3364 
3365 	dc_link_get_psr_state(link, &state);
3366 
3367 	if (reallow_idle)
3368 		dc_allow_idle_optimizations(dc, true);
3369 
3370 	mutex_unlock(&adev->dm.dc_lock);
3371 
3372 	*val = state;
3373 
3374 	return 0;
3375 }
3376 
3377 /*
3378  *  Read PSR state residency
3379  */
3380 static int psr_read_residency(void *data, u64 *val)
3381 {
3382 	struct amdgpu_dm_connector *connector = data;
3383 	struct dc_link *link = connector->dc_link;
3384 	struct amdgpu_device *adev = drm_to_adev(connector->base.dev);
3385 	struct dc *dc = adev->dm.dc;
3386 	u32 residency = 0;
3387 	bool reallow_idle = false;
3388 
3389 	mutex_lock(&adev->dm.dc_lock);
3390 
3391 	if (dc->idle_optimizations_allowed) {
3392 		dc_allow_idle_optimizations(dc, false);
3393 		reallow_idle = true;
3394 	}
3395 
3396 	link->dc->link_srv->edp_get_psr_residency(link, &residency, PSR_RESIDENCY_MODE_PHY);
3397 
3398 	if (reallow_idle)
3399 		dc_allow_idle_optimizations(dc, true);
3400 
3401 	mutex_unlock(&adev->dm.dc_lock);
3402 
3403 	*val = (u64)residency;
3404 
3405 	return 0;
3406 }
3407 
3408 /* read allow_edp_hotplug_detection */
3409 static int allow_edp_hotplug_detection_get(void *data, u64 *val)
3410 {
3411 	struct amdgpu_dm_connector *aconnector = data;
3412 	struct drm_connector *connector = &aconnector->base;
3413 	struct drm_device *dev = connector->dev;
3414 	struct amdgpu_device *adev = drm_to_adev(dev);
3415 
3416 	*val = adev->dm.dc->config.allow_edp_hotplug_detection;
3417 
3418 	return 0;
3419 }
3420 
3421 /* set allow_edp_hotplug_detection */
3422 static int allow_edp_hotplug_detection_set(void *data, u64 val)
3423 {
3424 	struct amdgpu_dm_connector *aconnector = data;
3425 	struct drm_connector *connector = &aconnector->base;
3426 	struct drm_device *dev = connector->dev;
3427 	struct amdgpu_device *adev = drm_to_adev(dev);
3428 
3429 	adev->dm.dc->config.allow_edp_hotplug_detection = (uint32_t) val;
3430 
3431 	return 0;
3432 }
3433 
3434 /* check if kernel disallow eDP enter psr state
3435  * cat /sys/kernel/debug/dri/0/eDP-X/disallow_edp_enter_psr
3436  * 0: allow edp enter psr; 1: disallow
3437  */
3438 static int disallow_edp_enter_psr_get(void *data, u64 *val)
3439 {
3440 	struct amdgpu_dm_connector *aconnector = data;
3441 
3442 	*val = (u64) aconnector->disallow_edp_enter_psr;
3443 	return 0;
3444 }
3445 
3446 /* set kernel disallow eDP enter psr state
3447  * echo 0x0 /sys/kernel/debug/dri/0/eDP-X/disallow_edp_enter_psr
3448  * 0: allow edp enter psr; 1: disallow
3449  *
3450  * usage: test app read crc from PSR eDP rx.
3451  *
3452  * during kernel boot up, kernel write dpcd 0x170 = 5.
3453  * this notify eDP rx psr enable and let rx check crc.
3454  * rx fw will start checking crc for rx internal logic.
3455  * crc read count within dpcd 0x246 is not updated and
3456  * value is 0. when eDP tx driver wants to read rx crc
3457  * from dpcd 0x246, 0x270, read count 0 lead tx driver
3458  * timeout.
3459  *
3460  * to avoid this, we add this debugfs to let test app to disbable
3461  * rx crc checking for rx internal logic. then test app can read
3462  * non-zero crc read count.
3463  *
3464  * expected app sequence is as below:
3465  * 1. disable eDP PHY and notify eDP rx with dpcd 0x600 = 2.
3466  * 2. echo 0x1 /sys/kernel/debug/dri/0/eDP-X/disallow_edp_enter_psr
3467  * 3. enable eDP PHY and notify eDP rx with dpcd 0x600 = 1 but
3468  *    without dpcd 0x170 = 5.
3469  * 4. read crc from rx dpcd 0x270, 0x246, etc.
3470  * 5. echo 0x0 /sys/kernel/debug/dri/0/eDP-X/disallow_edp_enter_psr.
3471  *    this will let eDP back to normal with psr setup dpcd 0x170 = 5.
3472  */
3473 static int disallow_edp_enter_psr_set(void *data, u64 val)
3474 {
3475 	struct amdgpu_dm_connector *aconnector = data;
3476 	struct dc_link *link = aconnector->dc_link;
3477 
3478 	aconnector->disallow_edp_enter_psr = (val != 0);
3479 
3480 	/* eDP PSR enable / disable is happened during mode change in power module.
3481 	 * Only psr_settings.psr_version is used to decide whether PSR is enabled or not.
3482 	 * So here we only update psr_version based on debugfs setting.
3483 	 * If disallow_edp_enter_psr is true, set psr_version to unsupported;
3484 	 * if disallow_edp_enter_psr is false, set psr_version based on sink capability.
3485 	 */
3486 	if (aconnector->disallow_edp_enter_psr)
3487 		link->psr_settings.psr_version = DC_PSR_VERSION_UNSUPPORTED;
3488 	else if (aconnector->psr_caps.psr_version == 1)
3489 		link->psr_settings.psr_version = DC_PSR_VERSION_1;
3490 	else if (aconnector->psr_caps.psr_version == 2)
3491 		link->psr_settings.psr_version = DC_PSR_VERSION_SU_1;
3492 	return 0;
3493 }
3494 
3495 
3496 /* check if kernel disallow eDP enter replay state
3497  * cat /sys/kernel/debug/dri/0/eDP-X/disallow_edp_enter_replay
3498  * 0: allow edp enter replay; 1: disallow
3499  */
3500 static int disallow_edp_enter_replay_get(void *data, u64 *val)
3501 {
3502 	struct amdgpu_dm_connector *aconnector = data;
3503 
3504 	*val = (u64) aconnector->disallow_edp_enter_replay;
3505 	return 0;
3506 }
3507 
3508 /* set kernel disallow eDP enter replay state
3509  * echo 0x0 /sys/kernel/debug/dri/0/eDP-X/disallow_edp_enter_replay
3510  * 0: allow edp enter replay; 1: disallow
3511  *
3512  * usage: test app read crc from PSR eDP rx.
3513  *
3514  * during kernel boot up, kernel write dpcd 0x37b to
3515  * notify eDP rx replay enable.
3516  * rx fw will start checking crc for rx internal logic.
3517  * crc read count within dpcd 0x246 is not updated and
3518  * value is 0. when eDP tx driver wants to read rx crc
3519  * from dpcd 0x246, 0x270, read count 0 lead tx driver
3520  * timeout.
3521  *
3522  * to avoid this, we add this debugfs to let test app to disbable
3523  * rx replay. then test app can read non-zero crc read count.
3524  *
3525  * expected app sequence is as below:
3526  * 1. disable eDP PHY and notify eDP rx with dpcd 0x600 = 2.
3527  * 2. echo 0x1 /sys/kernel/debug/dri/0/eDP-X/disallow_edp_enter_replay
3528  * 3. enable eDP PHY and notify eDP rx with dpcd 0x600 = 1 but
3529  *    without programming dpcd 0x37b.
3530  * 4. read crc from rx dpcd 0x270, 0x246, etc.
3531  * 5. echo 0x0 /sys/kernel/debug/dri/0/eDP-X/disallow_edp_enter_replay.
3532  *    this will let eDP back to normal with replay setup dpcd 0x37b.
3533  */
3534 static int disallow_edp_enter_replay_set(void *data, u64 val)
3535 {
3536 	struct amdgpu_dm_connector *aconnector = data;
3537 	struct dc_link *link = aconnector->dc_link;
3538 
3539 	aconnector->disallow_edp_enter_replay = (val != 0);
3540 
3541 	/* eDP replay enable / disable is happened during mode change in power module.
3542 	 * Only replay_settings.config.replay_supported is used to decide whether
3543 	 * replay is enabled or not. So here we only update replay_supported based on
3544 	 * debugfs setting.
3545 	 * If disallow_edp_enter_replay is true, set replay_supported to false.
3546 	 * if disallow_edp_enter_replay is false, set replay_supported back based on
3547 	 * sink replay capability.
3548 	 */
3549 	if (aconnector->disallow_edp_enter_replay)
3550 		link->replay_settings.config.replay_supported = false;
3551 	else
3552 		link->replay_settings.config.replay_supported =
3553 			link->replay_settings.config.replay_cap_support;
3554 	return 0;
3555 }
3556 
3557 
3558 static int dmub_trace_mask_set(void *data, u64 val)
3559 {
3560 	struct amdgpu_device *adev = data;
3561 	struct dmub_srv *srv = adev->dm.dc->ctx->dmub_srv->dmub;
3562 	enum dmub_gpint_command cmd;
3563 	u64 mask = 0xffff;
3564 	u8 shift = 0;
3565 	u32 res;
3566 	int i;
3567 
3568 	if (!srv->fw_version)
3569 		return -EINVAL;
3570 
3571 	for (i = 0;  i < 4; i++) {
3572 		res = (val & mask) >> shift;
3573 
3574 		switch (i) {
3575 		case 0:
3576 			cmd = DMUB_GPINT__SET_TRACE_BUFFER_MASK_WORD0;
3577 			break;
3578 		case 1:
3579 			cmd = DMUB_GPINT__SET_TRACE_BUFFER_MASK_WORD1;
3580 			break;
3581 		case 2:
3582 			cmd = DMUB_GPINT__SET_TRACE_BUFFER_MASK_WORD2;
3583 			break;
3584 		case 3:
3585 			cmd = DMUB_GPINT__SET_TRACE_BUFFER_MASK_WORD3;
3586 			break;
3587 		}
3588 
3589 		if (!dc_wake_and_execute_gpint(adev->dm.dc->ctx, cmd, res, NULL, DM_DMUB_WAIT_TYPE_WAIT))
3590 			return -EIO;
3591 
3592 		usleep_range(100, 1000);
3593 
3594 		mask <<= 16;
3595 		shift += 16;
3596 	}
3597 
3598 	return 0;
3599 }
3600 
3601 static int dmub_trace_mask_show(void *data, u64 *val)
3602 {
3603 	enum dmub_gpint_command cmd = DMUB_GPINT__GET_TRACE_BUFFER_MASK_WORD0;
3604 	struct amdgpu_device *adev = data;
3605 	struct dmub_srv *srv = adev->dm.dc->ctx->dmub_srv->dmub;
3606 	u8 shift = 0;
3607 	u64 raw = 0;
3608 	u64 res = 0;
3609 	int i = 0;
3610 
3611 	if (!srv->fw_version)
3612 		return -EINVAL;
3613 
3614 	while (i < 4) {
3615 		uint32_t response;
3616 
3617 		if (!dc_wake_and_execute_gpint(adev->dm.dc->ctx, cmd, 0, &response, DM_DMUB_WAIT_TYPE_WAIT_WITH_REPLY))
3618 			return -EIO;
3619 
3620 		raw = response;
3621 		usleep_range(100, 1000);
3622 
3623 		cmd++;
3624 		res |= (raw << shift);
3625 		shift += 16;
3626 		i++;
3627 	}
3628 
3629 	*val = res;
3630 
3631 	return 0;
3632 }
3633 
3634 DEFINE_DEBUGFS_ATTRIBUTE(dmub_trace_mask_fops, dmub_trace_mask_show,
3635 			 dmub_trace_mask_set, "0x%llx\n");
3636 
3637 /*
3638  * Set dmcub trace event IRQ enable or disable.
3639  * Usage to enable dmcub trace event IRQ: echo 1 > /sys/kernel/debug/dri/0/amdgpu_dm_dmcub_trace_event_en
3640  * Usage to disable dmcub trace event IRQ: echo 0 > /sys/kernel/debug/dri/0/amdgpu_dm_dmcub_trace_event_en
3641  */
3642 static int dmcub_trace_event_state_set(void *data, u64 val)
3643 {
3644 	struct amdgpu_device *adev = data;
3645 
3646 	if (val == 1 || val == 0) {
3647 		dc_dmub_trace_event_control(adev->dm.dc, val);
3648 		adev->dm.dmcub_trace_event_en = (bool)val;
3649 	} else
3650 		return 0;
3651 
3652 	return 0;
3653 }
3654 
3655 /*
3656  * The interface doesn't need get function, so it will return the
3657  * value of zero
3658  * Usage: cat /sys/kernel/debug/dri/0/amdgpu_dm_dmcub_trace_event_en
3659  */
3660 static int dmcub_trace_event_state_get(void *data, u64 *val)
3661 {
3662 	struct amdgpu_device *adev = data;
3663 
3664 	*val = adev->dm.dmcub_trace_event_en;
3665 	return 0;
3666 }
3667 
3668 DEFINE_DEBUGFS_ATTRIBUTE(dmcub_trace_event_state_fops, dmcub_trace_event_state_get,
3669 			 dmcub_trace_event_state_set, "%llu\n");
3670 
3671 DEFINE_DEBUGFS_ATTRIBUTE(replay_state_fops, replay_get_state, NULL, "%llu\n");
3672 DEFINE_DEBUGFS_ATTRIBUTE(replay_residency_fops, replay_get_residency, replay_set_residency,
3673 			 "%llu\n");
3674 DEFINE_DEBUGFS_ATTRIBUTE(psr_fops, psr_get, NULL, "%llu\n");
3675 DEFINE_DEBUGFS_ATTRIBUTE(psr_residency_fops, psr_read_residency, NULL,
3676 			 "%llu\n");
3677 
3678 DEFINE_DEBUGFS_ATTRIBUTE(allow_edp_hotplug_detection_fops,
3679 			allow_edp_hotplug_detection_get,
3680 			allow_edp_hotplug_detection_set, "%llu\n");
3681 
3682 DEFINE_DEBUGFS_ATTRIBUTE(disallow_edp_enter_psr_fops,
3683 			disallow_edp_enter_psr_get,
3684 			disallow_edp_enter_psr_set, "%llu\n");
3685 
3686 DEFINE_DEBUGFS_ATTRIBUTE(disallow_edp_enter_replay_fops,
3687 			disallow_edp_enter_replay_get,
3688 			disallow_edp_enter_replay_set, "%llu\n");
3689 
3690 DEFINE_DEBUGFS_ATTRIBUTE(ips_residency_cntl_fops, ips_residency_cntl_get,
3691 			   ips_residency_cntl_set, "%llu\n");
3692 
3693 DEFINE_SHOW_ATTRIBUTE(current_backlight);
3694 DEFINE_SHOW_ATTRIBUTE(target_backlight);
3695 
3696 /*
3697  * CACP ACE curve area. Read only.
3698  * Returns the area under the ACE PWL curve computed by DMUB firmware on
3699  * eDP panels that support CACP. The value is in DMU-internal units (PWL
3700  * area); it is only meaningful for relative comparison between CACP
3701  * levels on the same panel. Level 0 returns the linear baseline (largest).
3702  *
3703  * Example usage: cat /sys/kernel/debug/dri/0/eDP-1/cacp_ace_curve_area
3704  */
3705 static int cacp_ace_curve_area_show(struct seq_file *m, void *unused)
3706 {
3707 	struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(m->private);
3708 	struct dc_link *link = aconnector->dc_link;
3709 	struct amdgpu_device *adev = drm_to_adev(aconnector->base.dev);
3710 	struct dc *dc;
3711 	union dmub_rb_cmd cmd;
3712 	unsigned int panel_inst = 0;
3713 	bool reallow_idle = false;
3714 	int ret = 0;
3715 
3716 	if (!link || link->type == dc_connection_none)
3717 		return -ENODEV;
3718 
3719 	if (!(link->connector_signal & SIGNAL_TYPE_EDP))
3720 		return -ENODEV;
3721 
3722 	if (!link->panel_config.cacp.cacp_supported)
3723 		return -EOPNOTSUPP;
3724 
3725 	dc = link->ctx->dc;
3726 
3727 	if (!dc_get_edp_link_panel_inst(dc, link, &panel_inst))
3728 		return -EINVAL;
3729 
3730 	memset(&cmd, 0, sizeof(cmd));
3731 	cmd.cacp_get_ace_curve_area.header.type = DMUB_CMD__CACP;
3732 	cmd.cacp_get_ace_curve_area.header.sub_type = DMUB_CMD__CACP_GET_ACE_CURVE_AREA;
3733 	cmd.cacp_get_ace_curve_area.header.payload_bytes =
3734 		sizeof(cmd.cacp_get_ace_curve_area) -
3735 		sizeof(cmd.cacp_get_ace_curve_area.header);
3736 	cmd.cacp_get_ace_curve_area.data.in.panel_inst = panel_inst;
3737 
3738 	mutex_lock(&adev->dm.dc_lock);
3739 
3740 	if (dc->idle_optimizations_allowed) {
3741 		dc_allow_idle_optimizations(dc, false);
3742 		reallow_idle = true;
3743 	}
3744 
3745 	if (!dc_wake_and_execute_dmub_cmd(dc->ctx, &cmd,
3746 					  DM_DMUB_WAIT_TYPE_WAIT_WITH_REPLY) ||
3747 	    cmd.cacp_get_ace_curve_area.header.ret_status)
3748 		ret = -EIO;
3749 	else
3750 		seq_printf(m, "%u\n", cmd.cacp_get_ace_curve_area.data.out.area);
3751 
3752 	if (reallow_idle)
3753 		dc_allow_idle_optimizations(dc, true);
3754 
3755 	mutex_unlock(&adev->dm.dc_lock);
3756 
3757 	return ret;
3758 }
3759 DEFINE_SHOW_ATTRIBUTE(cacp_ace_curve_area);
3760 
3761 DEFINE_SHOW_ATTRIBUTE(ips_status);
3762 DEFINE_SHOW_ATTRIBUTE(ips_residency);
3763 
3764 static const struct {
3765 	char *name;
3766 	const struct file_operations *fops;
3767 } connector_debugfs_entries[] = {
3768 		{"force_yuv_pixel_format", &force_yuv_pixel_format_fops},
3769 		{"trigger_hotplug", &trigger_hotplug_debugfs_fops},
3770 		{"internal_display", &internal_display_fops},
3771 		{"max_downscale_src_width", &max_downscale_src_width_fops},
3772 		{"odm_combine_segments", &odm_combine_segments_fops}
3773 };
3774 
3775 /*
3776  * Returns supported customized link rates by this eDP panel.
3777  * Example usage: cat /sys/kernel/debug/dri/0/eDP-x/ilr_setting
3778  */
3779 static int edp_ilr_show(struct seq_file *m, void *unused)
3780 {
3781 	struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(m->private);
3782 	struct dc_link *link = aconnector->dc_link;
3783 	uint8_t supported_link_rates[16];
3784 	uint32_t link_rate_in_khz;
3785 	uint32_t entry = 0;
3786 	uint8_t dpcd_rev;
3787 
3788 	memset(supported_link_rates, 0, sizeof(supported_link_rates));
3789 	dm_helpers_dp_read_dpcd(link->ctx, link, DP_SUPPORTED_LINK_RATES,
3790 		supported_link_rates, sizeof(supported_link_rates));
3791 
3792 	dpcd_rev = link->dpcd_caps.dpcd_rev.raw;
3793 
3794 	if (dpcd_rev >= DP_DPCD_REV_13 &&
3795 		(supported_link_rates[entry+1] != 0 || supported_link_rates[entry] != 0)) {
3796 
3797 		for (entry = 0; entry < 16; entry += 2) {
3798 			link_rate_in_khz = (supported_link_rates[entry+1] * 0x100 +
3799 										supported_link_rates[entry]) * 200;
3800 			seq_printf(m, "[%d] %d kHz\n", entry/2, link_rate_in_khz);
3801 		}
3802 	} else {
3803 		seq_puts(m, "ILR is not supported by this eDP panel.\n");
3804 	}
3805 
3806 	return 0;
3807 }
3808 
3809 /*
3810  * Set supported customized link rate to eDP panel.
3811  *
3812  * echo <lane_count>  <link_rate option> > ilr_setting
3813  *
3814  * for example, supported ILR : [0] 1620000 kHz [1] 2160000 kHz [2] 2430000 kHz ...
3815  * echo 4 1 > /sys/kernel/debug/dri/0/eDP-x/ilr_setting
3816  * to set 4 lanes and 2.16 GHz
3817  */
3818 static ssize_t edp_ilr_write(struct file *f, const char __user *buf,
3819 				 size_t size, loff_t *pos)
3820 {
3821 	struct amdgpu_dm_connector *connector = file_inode(f)->i_private;
3822 	struct dc_link *link = connector->dc_link;
3823 	struct amdgpu_device *adev = drm_to_adev(connector->base.dev);
3824 	struct dc *dc = (struct dc *)link->dc;
3825 	struct dc_link_settings prefer_link_settings;
3826 	char *wr_buf = NULL;
3827 	const uint32_t wr_buf_size = 40;
3828 	/* 0: lane_count; 1: link_rate */
3829 	int max_param_num = 2;
3830 	uint8_t param_nums = 0;
3831 	long param[2];
3832 	bool valid_input = true;
3833 	uint8_t supported_link_rates[16] = {0};
3834 	uint32_t entry = 0;
3835 	uint32_t link_rate_in_khz = 0;
3836 	uint8_t dpcd_rev = 0;
3837 
3838 	if (size == 0)
3839 		return -EINVAL;
3840 
3841 	wr_buf = kcalloc(wr_buf_size, sizeof(char), GFP_KERNEL);
3842 	if (!wr_buf)
3843 		return -ENOMEM;
3844 
3845 	if (parse_write_buffer_into_params(wr_buf, wr_buf_size,
3846 					   (long *)param, buf,
3847 					   max_param_num,
3848 					   &param_nums)) {
3849 		kfree(wr_buf);
3850 		return -EINVAL;
3851 	}
3852 
3853 	if (param_nums <= 0) {
3854 		kfree(wr_buf);
3855 		return -EINVAL;
3856 	}
3857 
3858 	switch (param[0]) {
3859 	case LANE_COUNT_ONE:
3860 	case LANE_COUNT_TWO:
3861 	case LANE_COUNT_FOUR:
3862 		break;
3863 	default:
3864 		valid_input = false;
3865 		break;
3866 	}
3867 
3868 	if (param[1] >= link->dpcd_caps.edp_supported_link_rates_count)
3869 		valid_input = false;
3870 
3871 	if (!valid_input) {
3872 		kfree(wr_buf);
3873 		DRM_DEBUG_DRIVER("Invalid Input value. No HW will be programmed\n");
3874 		prefer_link_settings.use_link_rate_set = false;
3875 		mutex_lock(&adev->dm.dc_lock);
3876 		dc_link_set_preferred_training_settings(dc, NULL, NULL, link, false);
3877 		mutex_unlock(&adev->dm.dc_lock);
3878 		return size;
3879 	}
3880 
3881 	if (!dm_helpers_dp_read_dpcd(link->ctx, link, DP_SUPPORTED_LINK_RATES,
3882 		supported_link_rates, sizeof(supported_link_rates)))
3883 		return -EINVAL;
3884 
3885 	dpcd_rev = link->dpcd_caps.dpcd_rev.raw;
3886 	if (dpcd_rev < DP_DPCD_REV_13 ||
3887 		(supported_link_rates[entry + 1] == 0 && supported_link_rates[entry] == 0)) {
3888 		return size;
3889 	}
3890 
3891 	entry = param[1] * 2;
3892 	link_rate_in_khz = (supported_link_rates[entry + 1] * 0x100 +
3893 						supported_link_rates[entry]) * 200;
3894 
3895 	/* save user force lane_count, link_rate to preferred settings
3896 	 * spread spectrum will not be changed
3897 	 */
3898 	prefer_link_settings.link_spread = link->cur_link_settings.link_spread;
3899 	prefer_link_settings.lane_count = param[0];
3900 	prefer_link_settings.use_link_rate_set = true;
3901 	prefer_link_settings.link_rate_set = param[1];
3902 	prefer_link_settings.link_rate = link_rate_in_khz / MULTIPLIER_TO_LR;
3903 
3904 	mutex_lock(&adev->dm.dc_lock);
3905 	dc_link_set_preferred_training_settings(dc, &prefer_link_settings,
3906 						NULL, link, false);
3907 	mutex_unlock(&adev->dm.dc_lock);
3908 
3909 	kfree(wr_buf);
3910 	return size;
3911 }
3912 
3913 static int edp_ilr_open(struct inode *inode, struct file *file)
3914 {
3915 	return single_open(file, edp_ilr_show, inode->i_private);
3916 }
3917 
3918 static const struct file_operations edp_ilr_debugfs_fops = {
3919 	.owner = THIS_MODULE,
3920 	.open = edp_ilr_open,
3921 	.read = seq_read,
3922 	.llseek = seq_lseek,
3923 	.release = single_release,
3924 	.write = edp_ilr_write
3925 };
3926 
3927 void connector_debugfs_init(struct amdgpu_dm_connector *connector)
3928 {
3929 	int i;
3930 	struct dentry *dir = connector->base.debugfs_entry;
3931 
3932 	if (connector->base.connector_type == DRM_MODE_CONNECTOR_DisplayPort ||
3933 	    connector->base.connector_type == DRM_MODE_CONNECTOR_eDP) {
3934 		for (i = 0; i < ARRAY_SIZE(dp_debugfs_entries); i++) {
3935 			debugfs_create_file(dp_debugfs_entries[i].name,
3936 					    0644, dir, connector,
3937 					    dp_debugfs_entries[i].fops);
3938 		}
3939 	}
3940 	if (connector->base.connector_type == DRM_MODE_CONNECTOR_eDP) {
3941 		debugfs_create_file("replay_capability", 0444, dir, connector,
3942 					&replay_capability_fops);
3943 		debugfs_create_file("replay_state", 0444, dir, connector, &replay_state_fops);
3944 		debugfs_create_file_unsafe("replay_residency", 0444, dir,
3945 					   connector, &replay_residency_fops);
3946 		debugfs_create_file_unsafe("psr_capability", 0444, dir, connector, &psr_capability_fops);
3947 		debugfs_create_file_unsafe("psr_state", 0444, dir, connector, &psr_fops);
3948 		debugfs_create_file_unsafe("psr_residency", 0444, dir,
3949 					   connector, &psr_residency_fops);
3950 		debugfs_create_file("amdgpu_current_backlight_pwm", 0444, dir, connector,
3951 				    &current_backlight_fops);
3952 		debugfs_create_file("amdgpu_target_backlight_pwm", 0444, dir, connector,
3953 				    &target_backlight_fops);
3954 		debugfs_create_file("ilr_setting", 0644, dir, connector,
3955 					&edp_ilr_debugfs_fops);
3956 		debugfs_create_file("allow_edp_hotplug_detection", 0644, dir, connector,
3957 					&allow_edp_hotplug_detection_fops);
3958 		debugfs_create_file("disallow_edp_enter_psr", 0644, dir, connector,
3959 					&disallow_edp_enter_psr_fops);
3960 		debugfs_create_file("disallow_edp_enter_replay", 0644, dir, connector,
3961 					&disallow_edp_enter_replay_fops);
3962 		debugfs_create_file("cacp_ace_curve_area", 0444, dir, connector,
3963 				    &cacp_ace_curve_area_fops);
3964 	}
3965 
3966 	for (i = 0; i < ARRAY_SIZE(connector_debugfs_entries); i++) {
3967 		debugfs_create_file(connector_debugfs_entries[i].name,
3968 				    0644, dir, connector,
3969 				    connector_debugfs_entries[i].fops);
3970 	}
3971 
3972 	if (connector->base.connector_type == DRM_MODE_CONNECTOR_HDMIA) {
3973 		for (i = 0; i < ARRAY_SIZE(hdmi_debugfs_entries); i++) {
3974 			debugfs_create_file(hdmi_debugfs_entries[i].name,
3975 					    0644, dir, connector,
3976 					    hdmi_debugfs_entries[i].fops);
3977 		}
3978 	}
3979 }
3980 
3981 #ifdef CONFIG_DRM_AMD_SECURE_DISPLAY
3982 /*
3983  * Set crc window coordinate x start
3984  */
3985 static int crc_win_x_start_set(void *data, u64 val)
3986 {
3987 	struct drm_crtc *crtc = data;
3988 	struct drm_device *drm_dev = crtc->dev;
3989 	struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
3990 
3991 	spin_lock_irq(&drm_dev->event_lock);
3992 	acrtc->dm_irq_params.window_param[0].x_start = (uint16_t) val;
3993 	acrtc->dm_irq_params.window_param[0].update_win = false;
3994 	spin_unlock_irq(&drm_dev->event_lock);
3995 
3996 	return 0;
3997 }
3998 
3999 /*
4000  * Get crc window coordinate x start
4001  */
4002 static int crc_win_x_start_get(void *data, u64 *val)
4003 {
4004 	struct drm_crtc *crtc = data;
4005 	struct drm_device *drm_dev = crtc->dev;
4006 	struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
4007 
4008 	spin_lock_irq(&drm_dev->event_lock);
4009 	*val = acrtc->dm_irq_params.window_param[0].x_start;
4010 	spin_unlock_irq(&drm_dev->event_lock);
4011 
4012 	return 0;
4013 }
4014 
4015 DEFINE_DEBUGFS_ATTRIBUTE(crc_win_x_start_fops, crc_win_x_start_get,
4016 			 crc_win_x_start_set, "%llu\n");
4017 
4018 
4019 /*
4020  * Set crc window coordinate y start
4021  */
4022 static int crc_win_y_start_set(void *data, u64 val)
4023 {
4024 	struct drm_crtc *crtc = data;
4025 	struct drm_device *drm_dev = crtc->dev;
4026 	struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
4027 
4028 	spin_lock_irq(&drm_dev->event_lock);
4029 	acrtc->dm_irq_params.window_param[0].y_start = (uint16_t) val;
4030 	acrtc->dm_irq_params.window_param[0].update_win = false;
4031 	spin_unlock_irq(&drm_dev->event_lock);
4032 
4033 	return 0;
4034 }
4035 
4036 /*
4037  * Get crc window coordinate y start
4038  */
4039 static int crc_win_y_start_get(void *data, u64 *val)
4040 {
4041 	struct drm_crtc *crtc = data;
4042 	struct drm_device *drm_dev = crtc->dev;
4043 	struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
4044 
4045 	spin_lock_irq(&drm_dev->event_lock);
4046 	*val = acrtc->dm_irq_params.window_param[0].y_start;
4047 	spin_unlock_irq(&drm_dev->event_lock);
4048 
4049 	return 0;
4050 }
4051 
4052 DEFINE_DEBUGFS_ATTRIBUTE(crc_win_y_start_fops, crc_win_y_start_get,
4053 			 crc_win_y_start_set, "%llu\n");
4054 
4055 /*
4056  * Set crc window coordinate x end
4057  */
4058 static int crc_win_x_end_set(void *data, u64 val)
4059 {
4060 	struct drm_crtc *crtc = data;
4061 	struct drm_device *drm_dev = crtc->dev;
4062 	struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
4063 
4064 	spin_lock_irq(&drm_dev->event_lock);
4065 	acrtc->dm_irq_params.window_param[0].x_end = (uint16_t) val;
4066 	acrtc->dm_irq_params.window_param[0].update_win = false;
4067 	spin_unlock_irq(&drm_dev->event_lock);
4068 
4069 	return 0;
4070 }
4071 
4072 /*
4073  * Get crc window coordinate x end
4074  */
4075 static int crc_win_x_end_get(void *data, u64 *val)
4076 {
4077 	struct drm_crtc *crtc = data;
4078 	struct drm_device *drm_dev = crtc->dev;
4079 	struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
4080 
4081 	spin_lock_irq(&drm_dev->event_lock);
4082 	*val = acrtc->dm_irq_params.window_param[0].x_end;
4083 	spin_unlock_irq(&drm_dev->event_lock);
4084 
4085 	return 0;
4086 }
4087 
4088 DEFINE_DEBUGFS_ATTRIBUTE(crc_win_x_end_fops, crc_win_x_end_get,
4089 			 crc_win_x_end_set, "%llu\n");
4090 
4091 /*
4092  * Set crc window coordinate y end
4093  */
4094 static int crc_win_y_end_set(void *data, u64 val)
4095 {
4096 	struct drm_crtc *crtc = data;
4097 	struct drm_device *drm_dev = crtc->dev;
4098 	struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
4099 
4100 	spin_lock_irq(&drm_dev->event_lock);
4101 	acrtc->dm_irq_params.window_param[0].y_end = (uint16_t) val;
4102 	acrtc->dm_irq_params.window_param[0].update_win = false;
4103 	spin_unlock_irq(&drm_dev->event_lock);
4104 
4105 	return 0;
4106 }
4107 
4108 /*
4109  * Get crc window coordinate y end
4110  */
4111 static int crc_win_y_end_get(void *data, u64 *val)
4112 {
4113 	struct drm_crtc *crtc = data;
4114 	struct drm_device *drm_dev = crtc->dev;
4115 	struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
4116 
4117 	spin_lock_irq(&drm_dev->event_lock);
4118 	*val = acrtc->dm_irq_params.window_param[0].y_end;
4119 	spin_unlock_irq(&drm_dev->event_lock);
4120 
4121 	return 0;
4122 }
4123 
4124 DEFINE_DEBUGFS_ATTRIBUTE(crc_win_y_end_fops, crc_win_y_end_get,
4125 			 crc_win_y_end_set, "%llu\n");
4126 /*
4127  * Trigger to commit crc window
4128  */
4129 static int crc_win_update_set(void *data, u64 val)
4130 {
4131 	struct drm_crtc *crtc = data;
4132 	struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
4133 	struct amdgpu_device *adev = drm_to_adev(crtc->dev);
4134 
4135 	if (val) {
4136 		mutex_lock(&adev->dm.dc_lock);
4137 		/* PSR Replay may write to OTG CRC window control register,
4138 		 * so inactive it before starting secure_display by sending disable event.
4139 		 */
4140 		amdgpu_dm_psr_set_event(&adev->dm, acrtc->dm_irq_params.stream, true,
4141 			psr_event_crc_window_active, true);
4142 		amdgpu_dm_replay_set_event(&adev->dm, acrtc->dm_irq_params.stream, true,
4143 			replay_event_crc_window_active, true);
4144 
4145 		spin_lock_irq(&adev_to_drm(adev)->event_lock);
4146 		acrtc->dm_irq_params.window_param[0].enable = true;
4147 		acrtc->dm_irq_params.window_param[0].update_win = true;
4148 		acrtc->dm_irq_params.window_param[0].skip_frame_cnt = 0;
4149 		acrtc->dm_irq_params.crc_window_activated = true;
4150 		spin_unlock_irq(&adev_to_drm(adev)->event_lock);
4151 		mutex_unlock(&adev->dm.dc_lock);
4152 	} else {
4153 		/* Clear disable events to allow PSR/Replay to active */
4154 		mutex_lock(&adev->dm.dc_lock);
4155 		amdgpu_dm_psr_set_event(&adev->dm, acrtc->dm_irq_params.stream, false,
4156 			psr_event_crc_window_active, false);
4157 		amdgpu_dm_replay_set_event(&adev->dm, acrtc->dm_irq_params.stream, false,
4158 			replay_event_crc_window_active, false);
4159 		mutex_unlock(&adev->dm.dc_lock);
4160 	}
4161 	return 0;
4162 }
4163 
4164 /*
4165  * Get crc window update flag
4166  */
4167 static int crc_win_update_get(void *data, u64 *val)
4168 {
4169 	*val = 0;
4170 	return 0;
4171 }
4172 
4173 DEFINE_DEBUGFS_ATTRIBUTE(crc_win_update_fops, crc_win_update_get,
4174 			 crc_win_update_set, "%llu\n");
4175 
4176 /*
4177  * Trigger to set crc polynomial mode
4178  * 0: 16-bit CRC, 1: 32-bit CRC
4179  * only accepts 0 or 1 for supported hwip versions
4180  */
4181 static int crc_poly_mode_set(void *data, u64 val)
4182 {
4183 	struct drm_crtc *crtc = data;
4184 	struct amdgpu_crtc *acrtc;
4185 	struct amdgpu_device *adev = drm_to_adev(crtc->dev);
4186 
4187 	if ((amdgpu_ip_version(adev, DCE_HWIP, 0) >= IP_VERSION(3, 6, 0)) &&
4188 		(amdgpu_ip_version(adev, DCE_HWIP, 0) != IP_VERSION(4, 0, 1)) &&
4189 		(val < 2)) {
4190 		acrtc = to_amdgpu_crtc(crtc);
4191 		mutex_lock(&adev->dm.dc_lock);
4192 		spin_lock_irq(&adev_to_drm(adev)->event_lock);
4193 		acrtc->dm_irq_params.crc_poly_mode = val;
4194 		spin_unlock_irq(&adev_to_drm(adev)->event_lock);
4195 		mutex_unlock(&adev->dm.dc_lock);
4196 	}
4197 
4198 	return 0;
4199 }
4200 
4201 /*
4202  * Get crc polynomial mode (0: 16-bit CRC, 1: 32-bit CRC)
4203  */
4204 static int crc_poly_mode_get(void *data, u64 *val)
4205 {
4206 	struct drm_crtc *crtc = data;
4207 	struct drm_device *drm_dev = crtc->dev;
4208 	struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
4209 
4210 	spin_lock_irq(&drm_dev->event_lock);
4211 	*val = acrtc->dm_irq_params.crc_poly_mode;
4212 	spin_unlock_irq(&drm_dev->event_lock);
4213 
4214 	return 0;
4215 }
4216 
4217 DEFINE_DEBUGFS_ATTRIBUTE(crc_poly_mode_fops, crc_poly_mode_get,
4218 			 crc_poly_mode_set, "%llu\n");
4219 #endif
4220 void crtc_debugfs_init(struct drm_crtc *crtc)
4221 {
4222 #ifdef CONFIG_DRM_AMD_SECURE_DISPLAY
4223 	struct dentry *dir = debugfs_lookup("crc", crtc->debugfs_entry);
4224 
4225 	if (!dir)
4226 		return;
4227 
4228 	debugfs_create_file_unsafe("crc_win_x_start", 0644, dir, crtc,
4229 				   &crc_win_x_start_fops);
4230 	debugfs_create_file_unsafe("crc_win_y_start", 0644, dir, crtc,
4231 				   &crc_win_y_start_fops);
4232 	debugfs_create_file_unsafe("crc_win_x_end", 0644, dir, crtc,
4233 				   &crc_win_x_end_fops);
4234 	debugfs_create_file_unsafe("crc_win_y_end", 0644, dir, crtc,
4235 				   &crc_win_y_end_fops);
4236 	debugfs_create_file_unsafe("crc_win_update", 0644, dir, crtc,
4237 				   &crc_win_update_fops);
4238 	debugfs_create_file_unsafe("crc_poly_mode", 0644, dir, crtc,
4239 				   &crc_poly_mode_fops);
4240 	dput(dir);
4241 #endif
4242 	debugfs_create_file("amdgpu_current_bpc", 0644, crtc->debugfs_entry,
4243 			    crtc, &amdgpu_current_bpc_fops);
4244 	debugfs_create_file("amdgpu_current_colorspace", 0644, crtc->debugfs_entry,
4245 			    crtc, &amdgpu_current_colorspace_fops);
4246 }
4247 
4248 /*
4249  * Writes DTN log state to the user supplied buffer.
4250  * Example usage: cat /sys/kernel/debug/dri/0/amdgpu_dm_dtn_log
4251  */
4252 static ssize_t dtn_log_read(
4253 	struct file *f,
4254 	char __user *buf,
4255 	size_t size,
4256 	loff_t *pos)
4257 {
4258 	struct amdgpu_device *adev = file_inode(f)->i_private;
4259 	struct dc *dc = adev->dm.dc;
4260 	struct dc_log_buffer_ctx log_ctx = { 0 };
4261 	ssize_t result = 0;
4262 
4263 	if (!buf || !size)
4264 		return -EINVAL;
4265 
4266 	if (!dc->hwss.log_hw_state)
4267 		return 0;
4268 
4269 	dc->hwss.log_hw_state(dc, &log_ctx);
4270 
4271 	if (*pos < log_ctx.pos) {
4272 		size_t to_copy = log_ctx.pos - *pos;
4273 
4274 		to_copy = min(to_copy, size);
4275 
4276 		if (!copy_to_user(buf, log_ctx.buf + *pos, to_copy)) {
4277 			*pos += to_copy;
4278 			result = to_copy;
4279 		}
4280 	}
4281 
4282 	kfree(log_ctx.buf);
4283 
4284 	return result;
4285 }
4286 
4287 /*
4288  * Writes DTN log state to dmesg when triggered via a write.
4289  * Example usage: echo 1 > /sys/kernel/debug/dri/0/amdgpu_dm_dtn_log
4290  */
4291 static ssize_t dtn_log_write(
4292 	struct file *f,
4293 	const char __user *buf,
4294 	size_t size,
4295 	loff_t *pos)
4296 {
4297 	struct amdgpu_device *adev = file_inode(f)->i_private;
4298 	struct dc *dc = adev->dm.dc;
4299 
4300 	/* Write triggers log output via dmesg. */
4301 	if (size == 0)
4302 		return 0;
4303 
4304 	if (dc->hwss.log_hw_state)
4305 		dc->hwss.log_hw_state(dc, NULL);
4306 
4307 	return size;
4308 }
4309 
4310 static int mst_topo_show(struct seq_file *m, void *unused)
4311 {
4312 	struct amdgpu_device *adev = (struct amdgpu_device *)m->private;
4313 	struct drm_device *dev = adev_to_drm(adev);
4314 	struct drm_connector *connector;
4315 	struct drm_connector_list_iter conn_iter;
4316 	struct amdgpu_dm_connector *aconnector;
4317 
4318 	drm_connector_list_iter_begin(dev, &conn_iter);
4319 	drm_for_each_connector_iter(connector, &conn_iter) {
4320 		if (connector->connector_type != DRM_MODE_CONNECTOR_DisplayPort)
4321 			continue;
4322 
4323 		aconnector = to_amdgpu_dm_connector(connector);
4324 
4325 		/* Ensure we're only dumping the topology of a root mst node */
4326 		if (!aconnector->mst_mgr.mst_state)
4327 			continue;
4328 
4329 		seq_printf(m, "\nMST topology for connector %d\n", aconnector->connector_id);
4330 		drm_dp_mst_dump_topology(m, &aconnector->mst_mgr);
4331 	}
4332 	drm_connector_list_iter_end(&conn_iter);
4333 
4334 	return 0;
4335 }
4336 
4337 /*
4338  * Sets trigger hpd for MST topologies.
4339  * All connected connectors will be rediscovered and re started as needed if val of 1 is sent.
4340  * All topologies will be disconnected if val of 0 is set .
4341  * Usage to enable topologies: echo 1 > /sys/kernel/debug/dri/0/amdgpu_dm_trigger_hpd_mst
4342  * Usage to disable topologies: echo 0 > /sys/kernel/debug/dri/0/amdgpu_dm_trigger_hpd_mst
4343  */
4344 static int trigger_hpd_mst_set(void *data, u64 val)
4345 {
4346 	struct amdgpu_device *adev = data;
4347 	struct drm_device *dev = adev_to_drm(adev);
4348 	struct drm_connector_list_iter iter;
4349 	struct amdgpu_dm_connector *aconnector;
4350 	struct drm_connector *connector;
4351 	struct dc_link *link = NULL;
4352 	int ret;
4353 
4354 	if (val == 1) {
4355 		drm_connector_list_iter_begin(dev, &iter);
4356 		drm_for_each_connector_iter(connector, &iter) {
4357 			aconnector = to_amdgpu_dm_connector(connector);
4358 			if (aconnector->dc_link->type == dc_connection_mst_branch &&
4359 			    aconnector->mst_mgr.aux) {
4360 				mutex_lock(&adev->dm.dc_lock);
4361 				ret = dc_link_detect(aconnector->dc_link, DETECT_REASON_HPD);
4362 				mutex_unlock(&adev->dm.dc_lock);
4363 
4364 				if (!ret)
4365 					DRM_ERROR("DM_MST: Failed to detect dc link!");
4366 
4367 				ret = drm_dp_mst_topology_mgr_set_mst(&aconnector->mst_mgr, true);
4368 				if (ret < 0)
4369 					DRM_ERROR("DM_MST: Failed to set the device into MST mode!");
4370 			}
4371 		}
4372 	} else if (val == 0) {
4373 		drm_connector_list_iter_begin(dev, &iter);
4374 		drm_for_each_connector_iter(connector, &iter) {
4375 			aconnector = to_amdgpu_dm_connector(connector);
4376 			if (!aconnector->dc_link)
4377 				continue;
4378 
4379 			if (!aconnector->mst_root)
4380 				continue;
4381 
4382 			link = aconnector->dc_link;
4383 			dc_link_dp_receiver_power_ctrl(link, false);
4384 			drm_dp_mst_topology_mgr_set_mst(&aconnector->mst_root->mst_mgr, false);
4385 			link->mst_stream_alloc_table.stream_count = 0;
4386 			memset(link->mst_stream_alloc_table.stream_allocations, 0,
4387 					sizeof(link->mst_stream_alloc_table.stream_allocations));
4388 		}
4389 	} else {
4390 		return 0;
4391 	}
4392 	drm_kms_helper_hotplug_event(dev);
4393 
4394 	return 0;
4395 }
4396 
4397 /*
4398  * The interface doesn't need get function, so it will return the
4399  * value of zero
4400  * Usage: cat /sys/kernel/debug/dri/0/amdgpu_dm_trigger_hpd_mst
4401  */
4402 static int trigger_hpd_mst_get(void *data, u64 *val)
4403 {
4404 	*val = 0;
4405 	return 0;
4406 }
4407 
4408 DEFINE_DEBUGFS_ATTRIBUTE(trigger_hpd_mst_ops, trigger_hpd_mst_get,
4409 			 trigger_hpd_mst_set, "%llu\n");
4410 
4411 
4412 /*
4413  * Sets the force_timing_sync debug option from the given string.
4414  * All connected displays will be force synchronized immediately.
4415  * Usage: echo 1 > /sys/kernel/debug/dri/0/amdgpu_dm_force_timing_sync
4416  */
4417 static int force_timing_sync_set(void *data, u64 val)
4418 {
4419 	struct amdgpu_device *adev = data;
4420 
4421 	adev->dm.force_timing_sync = (bool)val;
4422 
4423 	amdgpu_dm_trigger_timing_sync(adev_to_drm(adev));
4424 
4425 	return 0;
4426 }
4427 
4428 /*
4429  * Gets the force_timing_sync debug option value into the given buffer.
4430  * Usage: cat /sys/kernel/debug/dri/0/amdgpu_dm_force_timing_sync
4431  */
4432 static int force_timing_sync_get(void *data, u64 *val)
4433 {
4434 	struct amdgpu_device *adev = data;
4435 
4436 	*val = adev->dm.force_timing_sync;
4437 
4438 	return 0;
4439 }
4440 
4441 DEFINE_DEBUGFS_ATTRIBUTE(force_timing_sync_ops, force_timing_sync_get,
4442 			 force_timing_sync_set, "%llu\n");
4443 
4444 
4445 /*
4446  * Disables all HPD and HPD RX interrupt handling in the
4447  * driver when set to 1. Default is 0.
4448  */
4449 static int disable_hpd_set(void *data, u64 val)
4450 {
4451 	struct amdgpu_device *adev = data;
4452 
4453 	adev->dm.disable_hpd_irq = (bool)val;
4454 
4455 	return 0;
4456 }
4457 
4458 
4459 /*
4460  * Returns 1 if HPD and HPRX interrupt handling is disabled,
4461  * 0 otherwise.
4462  */
4463 static int disable_hpd_get(void *data, u64 *val)
4464 {
4465 	struct amdgpu_device *adev = data;
4466 
4467 	*val = adev->dm.disable_hpd_irq;
4468 
4469 	return 0;
4470 }
4471 
4472 DEFINE_DEBUGFS_ATTRIBUTE(disable_hpd_ops, disable_hpd_get,
4473 			 disable_hpd_set, "%llu\n");
4474 
4475 /*
4476  * Prints hardware capabilities. These are used for IGT testing.
4477  */
4478 static int capabilities_show(struct seq_file *m, void *unused)
4479 {
4480 	struct amdgpu_device *adev = (struct amdgpu_device *)m->private;
4481 	struct dc *dc = adev->dm.dc;
4482 	bool mall_supported = dc->caps.mall_size_total;
4483 	bool subvp_supported = dc->caps.subvp_fw_processing_delay_us;
4484 	unsigned int mall_in_use = false;
4485 	unsigned int subvp_in_use = false;
4486 
4487 	struct hubbub *hubbub = dc->res_pool->hubbub;
4488 
4489 	if (hubbub && hubbub->funcs->get_mall_en)
4490 		hubbub->funcs->get_mall_en(hubbub, &mall_in_use);
4491 
4492 	if (dc->cap_funcs.get_subvp_en)
4493 		subvp_in_use = dc->cap_funcs.get_subvp_en(dc, dc->current_state);
4494 
4495 	seq_printf(m, "mall supported: %s, enabled: %s\n",
4496 			   mall_supported ? "yes" : "no", mall_in_use ? "yes" : "no");
4497 	seq_printf(m, "sub-viewport supported: %s, enabled: %s\n",
4498 			   subvp_supported ? "yes" : "no", subvp_in_use ? "yes" : "no");
4499 
4500 	return 0;
4501 }
4502 
4503 DEFINE_SHOW_ATTRIBUTE(capabilities);
4504 
4505 /*
4506  * Temporary w/a to force sst sequence in M42D DP2 mst receiver
4507  * Example usage: echo 1 > /sys/kernel/debug/dri/0/amdgpu_dm_dp_set_mst_en_for_sst
4508  */
4509 static int dp_force_sst_set(void *data, u64 val)
4510 {
4511 	struct amdgpu_device *adev = data;
4512 
4513 	adev->dm.dc->debug.set_mst_en_for_sst = val;
4514 
4515 	return 0;
4516 }
4517 
4518 static int dp_force_sst_get(void *data, u64 *val)
4519 {
4520 	struct amdgpu_device *adev = data;
4521 
4522 	*val = adev->dm.dc->debug.set_mst_en_for_sst;
4523 
4524 	return 0;
4525 }
4526 DEFINE_DEBUGFS_ATTRIBUTE(dp_set_mst_en_for_sst_ops, dp_force_sst_get,
4527 			 dp_force_sst_set, "%llu\n");
4528 
4529 /*
4530  * Force DP2 sequence without VESA certified cable.
4531  * Example usage: echo 1 > /sys/kernel/debug/dri/0/amdgpu_dm_dp_ignore_cable_id
4532  */
4533 static int dp_ignore_cable_id_set(void *data, u64 val)
4534 {
4535 	struct amdgpu_device *adev = data;
4536 
4537 	adev->dm.dc->debug.ignore_cable_id = val;
4538 
4539 	return 0;
4540 }
4541 
4542 static int dp_ignore_cable_id_get(void *data, u64 *val)
4543 {
4544 	struct amdgpu_device *adev = data;
4545 
4546 	*val = adev->dm.dc->debug.ignore_cable_id;
4547 
4548 	return 0;
4549 }
4550 DEFINE_DEBUGFS_ATTRIBUTE(dp_ignore_cable_id_ops, dp_ignore_cable_id_get,
4551 			 dp_ignore_cable_id_set, "%llu\n");
4552 
4553 /*
4554  * Sets the DC visual confirm debug option from the given string.
4555  * Example usage: echo 1 > /sys/kernel/debug/dri/0/amdgpu_visual_confirm
4556  */
4557 static int visual_confirm_set(void *data, u64 val)
4558 {
4559 	struct amdgpu_device *adev = data;
4560 
4561 	adev->dm.dc->debug.visual_confirm = (enum visual_confirm)val;
4562 
4563 	return 0;
4564 }
4565 
4566 /*
4567  * Reads the DC visual confirm debug option value into the given buffer.
4568  * Example usage: cat /sys/kernel/debug/dri/0/amdgpu_dm_visual_confirm
4569  */
4570 static int visual_confirm_get(void *data, u64 *val)
4571 {
4572 	struct amdgpu_device *adev = data;
4573 
4574 	*val = adev->dm.dc->debug.visual_confirm;
4575 
4576 	return 0;
4577 }
4578 
4579 DEFINE_SHOW_ATTRIBUTE(mst_topo);
4580 DEFINE_DEBUGFS_ATTRIBUTE(visual_confirm_fops, visual_confirm_get,
4581 			 visual_confirm_set, "%llu\n");
4582 
4583 
4584 /*
4585  * Sets the DC skip_detection_link_training debug option from the given string.
4586  * Example usage: echo 1 > /sys/kernel/debug/dri/0/amdgpu_skip_detection_link_training
4587  */
4588 static int skip_detection_link_training_set(void *data, u64 val)
4589 {
4590 	struct amdgpu_device *adev = data;
4591 
4592 	if (val == 0)
4593 		adev->dm.dc->debug.skip_detection_link_training = false;
4594 	else
4595 		adev->dm.dc->debug.skip_detection_link_training = true;
4596 
4597 	return 0;
4598 }
4599 
4600 /*
4601  * Reads the DC skip_detection_link_training debug option value into the given buffer.
4602  * Example usage: cat /sys/kernel/debug/dri/0/amdgpu_dm_skip_detection_link_training
4603  */
4604 static int skip_detection_link_training_get(void *data, u64 *val)
4605 {
4606 	struct amdgpu_device *adev = data;
4607 
4608 	*val = adev->dm.dc->debug.skip_detection_link_training;
4609 
4610 	return 0;
4611 }
4612 
4613 DEFINE_DEBUGFS_ATTRIBUTE(skip_detection_link_training_fops,
4614 			 skip_detection_link_training_get,
4615 			 skip_detection_link_training_set, "%llu\n");
4616 
4617 /*
4618  * Dumps the DCC_EN bit for each pipe.
4619  * Example usage: cat /sys/kernel/debug/dri/0/amdgpu_dm_dcc_en
4620  */
4621 static ssize_t dcc_en_bits_read(
4622 	struct file *f,
4623 	char __user *buf,
4624 	size_t size,
4625 	loff_t *pos)
4626 {
4627 	struct amdgpu_device *adev = file_inode(f)->i_private;
4628 	struct dc *dc = adev->dm.dc;
4629 	char *rd_buf = NULL;
4630 	const uint32_t rd_buf_size = 32;
4631 	uint32_t result = 0;
4632 	int offset = 0;
4633 	int num_pipes = dc->res_pool->pipe_count;
4634 	int *dcc_en_bits;
4635 	int i, r;
4636 
4637 	dcc_en_bits = kzalloc_objs(int, num_pipes);
4638 	if (!dcc_en_bits)
4639 		return -ENOMEM;
4640 
4641 	if (!dc->hwss.get_dcc_en_bits) {
4642 		kfree(dcc_en_bits);
4643 		return 0;
4644 	}
4645 
4646 	dc->hwss.get_dcc_en_bits(dc, dcc_en_bits);
4647 
4648 	rd_buf = kcalloc(rd_buf_size, sizeof(char), GFP_KERNEL);
4649 	if (!rd_buf) {
4650 		kfree(dcc_en_bits);
4651 		return -ENOMEM;
4652 	}
4653 
4654 	for (i = 0; i < num_pipes; i++)
4655 		offset += snprintf(rd_buf + offset, rd_buf_size - offset,
4656 				   "%d  ", dcc_en_bits[i]);
4657 	rd_buf[strlen(rd_buf)] = '\n';
4658 
4659 	kfree(dcc_en_bits);
4660 
4661 	while (size) {
4662 		if (*pos >= rd_buf_size)
4663 			break;
4664 		r = put_user(*(rd_buf + result), buf);
4665 		if (r) {
4666 			kfree(rd_buf);
4667 			return r; /* r = -EFAULT */
4668 		}
4669 		buf += 1;
4670 		size -= 1;
4671 		*pos += 1;
4672 		result += 1;
4673 	}
4674 
4675 	kfree(rd_buf);
4676 	return result;
4677 }
4678 
4679 void dtn_debugfs_init(struct amdgpu_device *adev)
4680 {
4681 	static const struct file_operations dtn_log_fops = {
4682 		.owner = THIS_MODULE,
4683 		.read = dtn_log_read,
4684 		.write = dtn_log_write,
4685 		.llseek = default_llseek
4686 	};
4687 	static const struct file_operations dcc_en_bits_fops = {
4688 		.owner = THIS_MODULE,
4689 		.read = dcc_en_bits_read,
4690 		.llseek = default_llseek
4691 	};
4692 
4693 	struct drm_minor *minor = adev_to_drm(adev)->primary;
4694 	struct dentry *root = minor->debugfs_root;
4695 
4696 	debugfs_create_file("amdgpu_mst_topology", 0444, root,
4697 			    adev, &mst_topo_fops);
4698 	debugfs_create_file("amdgpu_dm_capabilities", 0444, root,
4699 			    adev, &capabilities_fops);
4700 	debugfs_create_file("amdgpu_dm_dtn_log", 0644, root, adev,
4701 			    &dtn_log_fops);
4702 	debugfs_create_file("amdgpu_dm_dp_set_mst_en_for_sst", 0644, root, adev,
4703 				&dp_set_mst_en_for_sst_ops);
4704 	debugfs_create_file("amdgpu_dm_dp_ignore_cable_id", 0644, root, adev,
4705 				&dp_ignore_cable_id_ops);
4706 
4707 	debugfs_create_file_unsafe("amdgpu_dm_visual_confirm", 0644, root, adev,
4708 				   &visual_confirm_fops);
4709 
4710 	debugfs_create_file_unsafe("amdgpu_dm_skip_detection_link_training", 0644, root, adev,
4711 				   &skip_detection_link_training_fops);
4712 
4713 	debugfs_create_file_unsafe("amdgpu_dm_dmub_tracebuffer", 0644, root,
4714 				   adev, &dmub_tracebuffer_fops);
4715 
4716 	debugfs_create_file_unsafe("amdgpu_dm_dmub_fw_state", 0644, root,
4717 				   adev, &dmub_fw_state_fops);
4718 
4719 	debugfs_create_file_unsafe("amdgpu_dm_force_timing_sync", 0644, root,
4720 				   adev, &force_timing_sync_ops);
4721 
4722 	debugfs_create_file_unsafe("amdgpu_dm_dmub_trace_mask", 0644, root,
4723 				   adev, &dmub_trace_mask_fops);
4724 
4725 	debugfs_create_file_unsafe("amdgpu_dm_dmcub_trace_event_en", 0644, root,
4726 				   adev, &dmcub_trace_event_state_fops);
4727 
4728 	debugfs_create_file_unsafe("amdgpu_dm_trigger_hpd_mst", 0644, root,
4729 				   adev, &trigger_hpd_mst_ops);
4730 
4731 	debugfs_create_file_unsafe("amdgpu_dm_dcc_en", 0644, root, adev,
4732 				   &dcc_en_bits_fops);
4733 
4734 	debugfs_create_file_unsafe("amdgpu_dm_disable_hpd", 0644, root, adev,
4735 				   &disable_hpd_ops);
4736 
4737 	if (adev->dm.dc->caps.ips_support) {
4738 		debugfs_create_file_unsafe("amdgpu_dm_ips_status", 0644, root, adev,
4739 					   &ips_status_fops);
4740 
4741 		debugfs_create_file_unsafe("amdgpu_dm_ips_residency_cntl", 0644, root, adev,
4742 					   &ips_residency_cntl_fops);
4743 
4744 		debugfs_create_file_unsafe("amdgpu_dm_ips_residency", 0644, root, adev,
4745 					   &ips_residency_fops);
4746 	}
4747 }
4748