1 /*
2  * Copyright 2022 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * Authors: AMD
23  *
24  */
25 #include "link_dp_trace.h"
26 #include "link/protocols/link_dpcd.h"
27 #include "link.h"
28 
29 void dp_trace_init(struct dc_link *link)
30 {
31 	memset(&link->dp_trace, 0, sizeof(link->dp_trace));
32 	link->dp_trace.is_initialized = true;
33 }
34 
35 void dp_trace_reset(struct dc_link *link)
36 {
37 	memset(&link->dp_trace, 0, sizeof(link->dp_trace));
38 }
39 
40 bool dc_dp_trace_is_initialized(struct dc_link *link)
41 {
42 	return link->dp_trace.is_initialized;
43 }
44 
45 void dp_trace_detect_lt_init(struct dc_link *link)
46 {
47 	memset(&link->dp_trace.detect_lt_trace, 0, sizeof(link->dp_trace.detect_lt_trace));
48 }
49 
50 void dp_trace_commit_lt_init(struct dc_link *link)
51 {
52 	memset(&link->dp_trace.commit_lt_trace, 0, sizeof(link->dp_trace.commit_lt_trace));
53 }
54 
55 void dp_trace_link_loss_increment(struct dc_link *link)
56 {
57 	link->dp_trace.link_loss_count++;
58 }
59 
60 void dp_trace_lt_fail_count_update(struct dc_link *link,
61 		unsigned int fail_count,
62 		bool in_detection)
63 {
64 	if (in_detection)
65 		link->dp_trace.detect_lt_trace.counts.fail = fail_count;
66 	else
67 		link->dp_trace.commit_lt_trace.counts.fail = fail_count;
68 }
69 
70 void dp_trace_lt_total_count_increment(struct dc_link *link,
71 		bool in_detection)
72 {
73 	if (in_detection)
74 		link->dp_trace.detect_lt_trace.counts.total++;
75 	else
76 		link->dp_trace.commit_lt_trace.counts.total++;
77 }
78 
79 void dc_dp_trace_set_is_logged_flag(struct dc_link *link,
80 		bool in_detection,
81 		bool is_logged)
82 {
83 	if (in_detection)
84 		link->dp_trace.detect_lt_trace.is_logged = is_logged;
85 	else
86 		link->dp_trace.commit_lt_trace.is_logged = is_logged;
87 }
88 
89 bool dc_dp_trace_is_logged(struct dc_link *link,
90 		bool in_detection)
91 {
92 	if (in_detection)
93 		return link->dp_trace.detect_lt_trace.is_logged;
94 	else
95 		return link->dp_trace.commit_lt_trace.is_logged;
96 }
97 
98 void dp_trace_lt_result_update(struct dc_link *link,
99 		enum link_training_result result,
100 		bool in_detection)
101 {
102 	if (in_detection)
103 		link->dp_trace.detect_lt_trace.result = result;
104 	else
105 		link->dp_trace.commit_lt_trace.result = result;
106 }
107 
108 void dp_trace_set_lt_start_timestamp(struct dc_link *link,
109 		bool in_detection)
110 {
111 	if (in_detection)
112 		link->dp_trace.detect_lt_trace.timestamps.start = dm_get_timestamp(link->dc->ctx);
113 	else
114 		link->dp_trace.commit_lt_trace.timestamps.start = dm_get_timestamp(link->dc->ctx);
115 }
116 
117 void dp_trace_set_lt_end_timestamp(struct dc_link *link,
118 		bool in_detection)
119 {
120 	if (in_detection)
121 		link->dp_trace.detect_lt_trace.timestamps.end = dm_get_timestamp(link->dc->ctx);
122 	else
123 		link->dp_trace.commit_lt_trace.timestamps.end = dm_get_timestamp(link->dc->ctx);
124 }
125 
126 unsigned long long dc_dp_trace_get_lt_end_timestamp(struct dc_link *link,
127 		bool in_detection)
128 {
129 	if (in_detection)
130 		return link->dp_trace.detect_lt_trace.timestamps.end;
131 	else
132 		return link->dp_trace.commit_lt_trace.timestamps.end;
133 }
134 
135 struct dp_trace_lt_counts *dc_dp_trace_get_lt_counts(struct dc_link *link,
136 		bool in_detection)
137 {
138 	if (in_detection)
139 		return &link->dp_trace.detect_lt_trace.counts;
140 	else
141 		return &link->dp_trace.commit_lt_trace.counts;
142 }
143 
144 unsigned int dc_dp_trace_get_link_loss_count(struct dc_link *link)
145 {
146 	return link->dp_trace.link_loss_count;
147 }
148 
149 void link_dp_trace_set_edp_power_timestamp(struct dc_link *link,
150 		bool power_up)
151 {
152 	if (!power_up)
153 		/*save driver power off time stamp*/
154 		link->dp_trace.edp_trace_power_timestamps.poweroff = dm_get_timestamp(link->dc->ctx);
155 	else
156 		link->dp_trace.edp_trace_power_timestamps.poweron = dm_get_timestamp(link->dc->ctx);
157 }
158 
159 uint64_t link_dp_trace_get_edp_poweron_timestamp(struct dc_link *link)
160 {
161 	return link->dp_trace.edp_trace_power_timestamps.poweron;
162 }
163 
164 uint64_t link_dp_trace_get_edp_poweroff_timestamp(struct dc_link *link)
165 {
166 	return link->dp_trace.edp_trace_power_timestamps.poweroff;
167 }
168 
169 void link_dp_source_sequence_trace(struct dc_link *link, uint8_t dp_test_mode)
170 {
171 	if (link != NULL && link->dc->debug.enable_driver_sequence_debug)
172 		core_link_write_dpcd(link, DP_SOURCE_SEQUENCE,
173 					&dp_test_mode, sizeof(dp_test_mode));
174 }
175