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_clear_inbox0_ack(struct dc_dmub_srv *dmub_srv) 119 { 120 struct dmub_srv *dmub = dmub_srv->dmub; 121 struct dc_context *dc_ctx = dmub_srv->ctx; 122 enum dmub_status status = DMUB_STATUS_OK; 123 124 status = dmub_srv_clear_inbox0_ack(dmub); 125 if (status != DMUB_STATUS_OK) { 126 DC_ERROR("Error clearing INBOX0 ack: status=%d\n", status); 127 dc_dmub_srv_log_diagnostic_data(dmub_srv); 128 } 129 } 130 131 void dc_dmub_srv_wait_for_inbox0_ack(struct dc_dmub_srv *dmub_srv) 132 { 133 struct dmub_srv *dmub = dmub_srv->dmub; 134 struct dc_context *dc_ctx = dmub_srv->ctx; 135 enum dmub_status status = DMUB_STATUS_OK; 136 137 status = dmub_srv_wait_for_inbox0_ack(dmub, 100000); 138 if (status != DMUB_STATUS_OK) { 139 DC_ERROR("Error waiting for INBOX0 HW Lock Ack\n"); 140 dc_dmub_srv_log_diagnostic_data(dmub_srv); 141 } 142 } 143 144 void dc_dmub_srv_send_inbox0_cmd(struct dc_dmub_srv *dmub_srv, 145 union dmub_inbox0_data_register data) 146 { 147 struct dmub_srv *dmub = dmub_srv->dmub; 148 struct dc_context *dc_ctx = dmub_srv->ctx; 149 enum dmub_status status = DMUB_STATUS_OK; 150 151 status = dmub_srv_send_inbox0_cmd(dmub, data); 152 if (status != DMUB_STATUS_OK) { 153 DC_ERROR("Error sending INBOX0 cmd\n"); 154 dc_dmub_srv_log_diagnostic_data(dmub_srv); 155 } 156 } 157 158 bool dc_dmub_srv_cmd_with_reply_data(struct dc_dmub_srv *dc_dmub_srv, union dmub_rb_cmd *cmd) 159 { 160 struct dmub_srv *dmub; 161 enum dmub_status status; 162 163 if (!dc_dmub_srv || !dc_dmub_srv->dmub) 164 return false; 165 166 dmub = dc_dmub_srv->dmub; 167 168 status = dmub_srv_cmd_with_reply_data(dmub, cmd); 169 if (status != DMUB_STATUS_OK) { 170 DC_LOG_DEBUG("No reply for DMUB command: status=%d\n", status); 171 return false; 172 } 173 174 return true; 175 } 176 177 void dc_dmub_srv_wait_phy_init(struct dc_dmub_srv *dc_dmub_srv) 178 { 179 struct dmub_srv *dmub = dc_dmub_srv->dmub; 180 struct dc_context *dc_ctx = dc_dmub_srv->ctx; 181 enum dmub_status status; 182 183 for (;;) { 184 /* Wait up to a second for PHY init. */ 185 status = dmub_srv_wait_for_phy_init(dmub, 1000000); 186 if (status == DMUB_STATUS_OK) 187 /* Initialization OK */ 188 break; 189 190 DC_ERROR("DMCUB PHY init failed: status=%d\n", status); 191 ASSERT(0); 192 193 if (status != DMUB_STATUS_TIMEOUT) 194 /* 195 * Server likely initialized or we don't have 196 * DMCUB HW support - this won't end. 197 */ 198 break; 199 200 /* Continue spinning so we don't hang the ASIC. */ 201 } 202 } 203 204 bool dc_dmub_srv_notify_stream_mask(struct dc_dmub_srv *dc_dmub_srv, 205 unsigned int stream_mask) 206 { 207 struct dmub_srv *dmub; 208 const uint32_t timeout = 30; 209 210 if (!dc_dmub_srv || !dc_dmub_srv->dmub) 211 return false; 212 213 dmub = dc_dmub_srv->dmub; 214 215 return dmub_srv_send_gpint_command( 216 dmub, DMUB_GPINT__IDLE_OPT_NOTIFY_STREAM_MASK, 217 stream_mask, timeout) == DMUB_STATUS_OK; 218 } 219 220 bool dc_dmub_srv_is_restore_required(struct dc_dmub_srv *dc_dmub_srv) 221 { 222 struct dmub_srv *dmub; 223 struct dc_context *dc_ctx; 224 union dmub_fw_boot_status boot_status; 225 enum dmub_status status; 226 227 if (!dc_dmub_srv || !dc_dmub_srv->dmub) 228 return false; 229 230 dmub = dc_dmub_srv->dmub; 231 dc_ctx = dc_dmub_srv->ctx; 232 233 status = dmub_srv_get_fw_boot_status(dmub, &boot_status); 234 if (status != DMUB_STATUS_OK) { 235 DC_ERROR("Error querying DMUB boot status: error=%d\n", status); 236 return false; 237 } 238 239 return boot_status.bits.restore_required; 240 } 241 242 bool dc_dmub_srv_get_dmub_outbox0_msg(const struct dc *dc, struct dmcub_trace_buf_entry *entry) 243 { 244 struct dmub_srv *dmub = dc->ctx->dmub_srv->dmub; 245 return dmub_srv_get_outbox0_msg(dmub, entry); 246 } 247 248 void dc_dmub_trace_event_control(struct dc *dc, bool enable) 249 { 250 dm_helpers_dmub_outbox_interrupt_control(dc->ctx, enable); 251 } 252 253 void dc_dmub_srv_query_caps_cmd(struct dmub_srv *dmub) 254 { 255 union dmub_rb_cmd cmd = { 0 }; 256 enum dmub_status status; 257 258 if (!dmub) { 259 return; 260 } 261 262 memset(&cmd, 0, sizeof(cmd)); 263 264 /* Prepare fw command */ 265 cmd.query_feature_caps.header.type = DMUB_CMD__QUERY_FEATURE_CAPS; 266 cmd.query_feature_caps.header.sub_type = 0; 267 cmd.query_feature_caps.header.ret_status = 1; 268 cmd.query_feature_caps.header.payload_bytes = sizeof(struct dmub_cmd_query_feature_caps_data); 269 270 /* Send command to fw */ 271 status = dmub_srv_cmd_with_reply_data(dmub, &cmd); 272 273 ASSERT(status == DMUB_STATUS_OK); 274 275 /* If command was processed, copy feature caps to dmub srv */ 276 if (status == DMUB_STATUS_OK && 277 cmd.query_feature_caps.header.ret_status == 0) { 278 memcpy(&dmub->feature_caps, 279 &cmd.query_feature_caps.query_feature_caps_data, 280 sizeof(struct dmub_feature_caps)); 281 } 282 } 283 284 bool dc_dmub_srv_get_diagnostic_data(struct dc_dmub_srv *dc_dmub_srv, struct dmub_diagnostic_data *diag_data) 285 { 286 if (!dc_dmub_srv || !dc_dmub_srv->dmub || !diag_data) 287 return false; 288 return dmub_srv_get_diagnostic_data(dc_dmub_srv->dmub, diag_data); 289 } 290 291 void dc_dmub_srv_log_diagnostic_data(struct dc_dmub_srv *dc_dmub_srv) 292 { 293 struct dmub_diagnostic_data diag_data = {0}; 294 295 if (!dc_dmub_srv || !dc_dmub_srv->dmub) { 296 DC_LOG_ERROR("%s: invalid parameters.", __func__); 297 return; 298 } 299 300 if (!dc_dmub_srv_get_diagnostic_data(dc_dmub_srv, &diag_data)) { 301 DC_LOG_ERROR("%s: dc_dmub_srv_get_diagnostic_data failed.", __func__); 302 return; 303 } 304 305 DC_LOG_DEBUG( 306 "DMCUB STATE\n" 307 " dmcub_version : %08x\n" 308 " scratch [0] : %08x\n" 309 " scratch [1] : %08x\n" 310 " scratch [2] : %08x\n" 311 " scratch [3] : %08x\n" 312 " scratch [4] : %08x\n" 313 " scratch [5] : %08x\n" 314 " scratch [6] : %08x\n" 315 " scratch [7] : %08x\n" 316 " scratch [8] : %08x\n" 317 " scratch [9] : %08x\n" 318 " scratch [10] : %08x\n" 319 " scratch [11] : %08x\n" 320 " scratch [12] : %08x\n" 321 " scratch [13] : %08x\n" 322 " scratch [14] : %08x\n" 323 " scratch [15] : %08x\n" 324 " pc : %08x\n" 325 " unk_fault_addr : %08x\n" 326 " inst_fault_addr : %08x\n" 327 " data_fault_addr : %08x\n" 328 " inbox1_rptr : %08x\n" 329 " inbox1_wptr : %08x\n" 330 " inbox1_size : %08x\n" 331 " inbox0_rptr : %08x\n" 332 " inbox0_wptr : %08x\n" 333 " inbox0_size : %08x\n" 334 " is_enabled : %d\n" 335 " is_soft_reset : %d\n" 336 " is_secure_reset : %d\n" 337 " is_traceport_en : %d\n" 338 " is_cw0_en : %d\n" 339 " is_cw6_en : %d\n", 340 diag_data.dmcub_version, 341 diag_data.scratch[0], 342 diag_data.scratch[1], 343 diag_data.scratch[2], 344 diag_data.scratch[3], 345 diag_data.scratch[4], 346 diag_data.scratch[5], 347 diag_data.scratch[6], 348 diag_data.scratch[7], 349 diag_data.scratch[8], 350 diag_data.scratch[9], 351 diag_data.scratch[10], 352 diag_data.scratch[11], 353 diag_data.scratch[12], 354 diag_data.scratch[13], 355 diag_data.scratch[14], 356 diag_data.scratch[15], 357 diag_data.pc, 358 diag_data.undefined_address_fault_addr, 359 diag_data.inst_fetch_fault_addr, 360 diag_data.data_write_fault_addr, 361 diag_data.inbox1_rptr, 362 diag_data.inbox1_wptr, 363 diag_data.inbox1_size, 364 diag_data.inbox0_rptr, 365 diag_data.inbox0_wptr, 366 diag_data.inbox0_size, 367 diag_data.is_dmcub_enabled, 368 diag_data.is_dmcub_soft_reset, 369 diag_data.is_dmcub_secure_reset, 370 diag_data.is_traceport_en, 371 diag_data.is_cw0_enabled, 372 diag_data.is_cw6_enabled); 373 } 374