1 /*
2  * Copyright 2019 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 
26 #include "dc.h"
27 #include "dc_dmub_srv.h"
28 #include "../dmub/dmub_srv.h"
29 #include "dm_helpers.h"
30 
31 #define CTX dc_dmub_srv->ctx
32 #define DC_LOGGER CTX->logger
33 
34 static void dc_dmub_srv_construct(struct dc_dmub_srv *dc_srv, struct dc *dc,
35 				  struct dmub_srv *dmub)
36 {
37 	dc_srv->dmub = dmub;
38 	dc_srv->ctx = dc->ctx;
39 }
40 
41 struct dc_dmub_srv *dc_dmub_srv_create(struct dc *dc, struct dmub_srv *dmub)
42 {
43 	struct dc_dmub_srv *dc_srv =
44 		kzalloc(sizeof(struct dc_dmub_srv), GFP_KERNEL);
45 
46 	if (dc_srv == NULL) {
47 		BREAK_TO_DEBUGGER();
48 		return NULL;
49 	}
50 
51 	dc_dmub_srv_construct(dc_srv, dc, dmub);
52 
53 	return dc_srv;
54 }
55 
56 void dc_dmub_srv_destroy(struct dc_dmub_srv **dmub_srv)
57 {
58 	if (*dmub_srv) {
59 		kfree(*dmub_srv);
60 		*dmub_srv = NULL;
61 	}
62 }
63 
64 void dc_dmub_srv_cmd_queue(struct dc_dmub_srv *dc_dmub_srv,
65 			   union dmub_rb_cmd *cmd)
66 {
67 	struct dmub_srv *dmub = dc_dmub_srv->dmub;
68 	struct dc_context *dc_ctx = dc_dmub_srv->ctx;
69 	enum dmub_status status;
70 
71 	status = dmub_srv_cmd_queue(dmub, cmd);
72 	if (status == DMUB_STATUS_OK)
73 		return;
74 
75 	if (status != DMUB_STATUS_QUEUE_FULL)
76 		goto error;
77 
78 	/* Execute and wait for queue to become empty again. */
79 	dc_dmub_srv_cmd_execute(dc_dmub_srv);
80 	dc_dmub_srv_wait_idle(dc_dmub_srv);
81 
82 	/* Requeue the command. */
83 	status = dmub_srv_cmd_queue(dmub, cmd);
84 	if (status == DMUB_STATUS_OK)
85 		return;
86 
87 error:
88 	DC_ERROR("Error queuing DMUB command: status=%d\n", status);
89 	dc_dmub_srv_log_diagnostic_data(dc_dmub_srv);
90 }
91 
92 void dc_dmub_srv_cmd_execute(struct dc_dmub_srv *dc_dmub_srv)
93 {
94 	struct dmub_srv *dmub = dc_dmub_srv->dmub;
95 	struct dc_context *dc_ctx = dc_dmub_srv->ctx;
96 	enum dmub_status status;
97 
98 	status = dmub_srv_cmd_execute(dmub);
99 	if (status != DMUB_STATUS_OK) {
100 		DC_ERROR("Error starting DMUB execution: status=%d\n", status);
101 		dc_dmub_srv_log_diagnostic_data(dc_dmub_srv);
102 	}
103 }
104 
105 void dc_dmub_srv_wait_idle(struct dc_dmub_srv *dc_dmub_srv)
106 {
107 	struct dmub_srv *dmub = dc_dmub_srv->dmub;
108 	struct dc_context *dc_ctx = dc_dmub_srv->ctx;
109 	enum dmub_status status;
110 
111 	status = dmub_srv_wait_for_idle(dmub, 100000);
112 	if (status != DMUB_STATUS_OK) {
113 		DC_ERROR("Error waiting for DMUB idle: status=%d\n", status);
114 		dc_dmub_srv_log_diagnostic_data(dc_dmub_srv);
115 	}
116 }
117 
118 void dc_dmub_srv_send_inbox0_cmd(struct dc_dmub_srv *dmub_srv,
119 		union dmub_inbox0_data_register data)
120 {
121 	struct dmub_srv *dmub = dmub_srv->dmub;
122 	if (dmub->hw_funcs.send_inbox0_cmd)
123 		dmub->hw_funcs.send_inbox0_cmd(dmub, data);
124 	// TODO: Add wait command -- poll register for ACK
125 }
126 
127 bool dc_dmub_srv_cmd_with_reply_data(struct dc_dmub_srv *dc_dmub_srv, union dmub_rb_cmd *cmd)
128 {
129 	struct dmub_srv *dmub;
130 	enum dmub_status status;
131 
132 	if (!dc_dmub_srv || !dc_dmub_srv->dmub)
133 		return false;
134 
135 	dmub = dc_dmub_srv->dmub;
136 
137 	status = dmub_srv_cmd_with_reply_data(dmub, cmd);
138 	if (status != DMUB_STATUS_OK) {
139 		DC_LOG_DEBUG("No reply for DMUB command: status=%d\n", status);
140 		return false;
141 	}
142 
143 	return true;
144 }
145 
146 void dc_dmub_srv_wait_phy_init(struct dc_dmub_srv *dc_dmub_srv)
147 {
148 	struct dmub_srv *dmub = dc_dmub_srv->dmub;
149 	struct dc_context *dc_ctx = dc_dmub_srv->ctx;
150 	enum dmub_status status;
151 
152 	for (;;) {
153 		/* Wait up to a second for PHY init. */
154 		status = dmub_srv_wait_for_phy_init(dmub, 1000000);
155 		if (status == DMUB_STATUS_OK)
156 			/* Initialization OK */
157 			break;
158 
159 		DC_ERROR("DMCUB PHY init failed: status=%d\n", status);
160 		ASSERT(0);
161 
162 		if (status != DMUB_STATUS_TIMEOUT)
163 			/*
164 			 * Server likely initialized or we don't have
165 			 * DMCUB HW support - this won't end.
166 			 */
167 			break;
168 
169 		/* Continue spinning so we don't hang the ASIC. */
170 	}
171 }
172 
173 bool dc_dmub_srv_notify_stream_mask(struct dc_dmub_srv *dc_dmub_srv,
174 				    unsigned int stream_mask)
175 {
176 	struct dmub_srv *dmub;
177 	const uint32_t timeout = 30;
178 
179 	if (!dc_dmub_srv || !dc_dmub_srv->dmub)
180 		return false;
181 
182 	dmub = dc_dmub_srv->dmub;
183 
184 	return dmub_srv_send_gpint_command(
185 		       dmub, DMUB_GPINT__IDLE_OPT_NOTIFY_STREAM_MASK,
186 		       stream_mask, timeout) == DMUB_STATUS_OK;
187 }
188 
189 bool dc_dmub_srv_is_restore_required(struct dc_dmub_srv *dc_dmub_srv)
190 {
191 	struct dmub_srv *dmub;
192 	struct dc_context *dc_ctx;
193 	union dmub_fw_boot_status boot_status;
194 	enum dmub_status status;
195 
196 	if (!dc_dmub_srv || !dc_dmub_srv->dmub)
197 		return false;
198 
199 	dmub = dc_dmub_srv->dmub;
200 	dc_ctx = dc_dmub_srv->ctx;
201 
202 	status = dmub_srv_get_fw_boot_status(dmub, &boot_status);
203 	if (status != DMUB_STATUS_OK) {
204 		DC_ERROR("Error querying DMUB boot status: error=%d\n", status);
205 		return false;
206 	}
207 
208 	return boot_status.bits.restore_required;
209 }
210 
211 bool dc_dmub_srv_get_dmub_outbox0_msg(const struct dc *dc, struct dmcub_trace_buf_entry *entry)
212 {
213 	struct dmub_srv *dmub = dc->ctx->dmub_srv->dmub;
214 	return dmub_srv_get_outbox0_msg(dmub, entry);
215 }
216 
217 void dc_dmub_trace_event_control(struct dc *dc, bool enable)
218 {
219 	dm_helpers_dmub_outbox_interrupt_control(dc->ctx, enable);
220 }
221 
222 bool dc_dmub_srv_get_diagnostic_data(struct dc_dmub_srv *dc_dmub_srv, struct dmub_diagnostic_data *diag_data)
223 {
224 	if (!dc_dmub_srv || !dc_dmub_srv->dmub || !diag_data)
225 		return false;
226 	return dmub_srv_get_diagnostic_data(dc_dmub_srv->dmub, diag_data);
227 }
228 
229 void dc_dmub_srv_log_diagnostic_data(struct dc_dmub_srv *dc_dmub_srv)
230 {
231 	struct dmub_diagnostic_data diag_data = {0};
232 
233 	if (!dc_dmub_srv || !dc_dmub_srv->dmub) {
234 		DC_LOG_ERROR("%s: invalid parameters.", __func__);
235 		return;
236 	}
237 
238 	if (!dc_dmub_srv_get_diagnostic_data(dc_dmub_srv, &diag_data)) {
239 		DC_LOG_ERROR("%s: dc_dmub_srv_get_diagnostic_data failed.", __func__);
240 		return;
241 	}
242 
243 	DC_LOG_DEBUG(
244 		"DMCUB STATE\n"
245 		"    dmcub_version      : %08x\n"
246 		"    scratch  [0]       : %08x\n"
247 		"    scratch  [1]       : %08x\n"
248 		"    scratch  [2]       : %08x\n"
249 		"    scratch  [3]       : %08x\n"
250 		"    scratch  [4]       : %08x\n"
251 		"    scratch  [5]       : %08x\n"
252 		"    scratch  [6]       : %08x\n"
253 		"    scratch  [7]       : %08x\n"
254 		"    scratch  [8]       : %08x\n"
255 		"    scratch  [9]       : %08x\n"
256 		"    scratch [10]       : %08x\n"
257 		"    scratch [11]       : %08x\n"
258 		"    scratch [12]       : %08x\n"
259 		"    scratch [13]       : %08x\n"
260 		"    scratch [14]       : %08x\n"
261 		"    scratch [15]       : %08x\n"
262 		"    pc                 : %08x\n"
263 		"    unk_fault_addr     : %08x\n"
264 		"    inst_fault_addr    : %08x\n"
265 		"    data_fault_addr    : %08x\n"
266 		"    inbox1_rptr        : %08x\n"
267 		"    inbox1_wptr        : %08x\n"
268 		"    inbox1_size        : %08x\n"
269 		"    inbox0_rptr        : %08x\n"
270 		"    inbox0_wptr        : %08x\n"
271 		"    inbox0_size        : %08x\n"
272 		"    is_enabled         : %d\n"
273 		"    is_soft_reset      : %d\n"
274 		"    is_secure_reset    : %d\n"
275 		"    is_traceport_en    : %d\n"
276 		"    is_cw0_en          : %d\n"
277 		"    is_cw6_en          : %d\n",
278 		diag_data.dmcub_version,
279 		diag_data.scratch[0],
280 		diag_data.scratch[1],
281 		diag_data.scratch[2],
282 		diag_data.scratch[3],
283 		diag_data.scratch[4],
284 		diag_data.scratch[5],
285 		diag_data.scratch[6],
286 		diag_data.scratch[7],
287 		diag_data.scratch[8],
288 		diag_data.scratch[9],
289 		diag_data.scratch[10],
290 		diag_data.scratch[11],
291 		diag_data.scratch[12],
292 		diag_data.scratch[13],
293 		diag_data.scratch[14],
294 		diag_data.scratch[15],
295 		diag_data.pc,
296 		diag_data.undefined_address_fault_addr,
297 		diag_data.inst_fetch_fault_addr,
298 		diag_data.data_write_fault_addr,
299 		diag_data.inbox1_rptr,
300 		diag_data.inbox1_wptr,
301 		diag_data.inbox1_size,
302 		diag_data.inbox0_rptr,
303 		diag_data.inbox0_wptr,
304 		diag_data.inbox0_size,
305 		diag_data.is_dmcub_enabled,
306 		diag_data.is_dmcub_soft_reset,
307 		diag_data.is_dmcub_secure_reset,
308 		diag_data.is_traceport_en,
309 		diag_data.is_cw0_enabled,
310 		diag_data.is_cw6_enabled);
311 }
312