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 "../inc/dmub_srv.h" 27 #include "dmub_dcn20.h" 28 #include "dmub_dcn21.h" 29 #include "dmub_fw_meta.h" 30 #include "os_types.h" 31 /* 32 * Note: the DMUB service is standalone. No additional headers should be 33 * added below or above this line unless they reside within the DMUB 34 * folder. 35 */ 36 37 /* Alignment for framebuffer memory. */ 38 #define DMUB_FB_ALIGNMENT (1024 * 1024) 39 40 /* Stack size. */ 41 #define DMUB_STACK_SIZE (128 * 1024) 42 43 /* Context size. */ 44 #define DMUB_CONTEXT_SIZE (512 * 1024) 45 46 /* Mailbox size */ 47 #define DMUB_MAILBOX_SIZE (DMUB_RB_SIZE) 48 49 /* Default state size if meta is absent. */ 50 #define DMUB_FW_STATE_SIZE (1024) 51 52 /* Default tracebuffer size if meta is absent. */ 53 #define DMUB_TRACE_BUFFER_SIZE (1024) 54 55 /* Number of windows in use. */ 56 #define DMUB_NUM_WINDOWS (DMUB_WINDOW_6_FW_STATE + 1) 57 /* Base addresses. */ 58 59 #define DMUB_CW0_BASE (0x60000000) 60 #define DMUB_CW1_BASE (0x61000000) 61 #define DMUB_CW3_BASE (0x63000000) 62 #define DMUB_CW5_BASE (0x65000000) 63 #define DMUB_CW6_BASE (0x66000000) 64 65 static inline uint32_t dmub_align(uint32_t val, uint32_t factor) 66 { 67 return (val + factor - 1) / factor * factor; 68 } 69 70 static void dmub_flush_buffer_mem(const struct dmub_fb *fb) 71 { 72 const uint8_t *base = (const uint8_t *)fb->cpu_addr; 73 uint8_t buf[64]; 74 uint32_t pos, end; 75 76 /** 77 * Read 64-byte chunks since we don't want to store a 78 * large temporary buffer for this purpose. 79 */ 80 end = fb->size / sizeof(buf) * sizeof(buf); 81 82 for (pos = 0; pos < end; pos += sizeof(buf)) 83 dmub_memcpy(buf, base + pos, sizeof(buf)); 84 85 /* Read anything leftover into the buffer. */ 86 if (end < fb->size) 87 dmub_memcpy(buf, base + pos, fb->size - end); 88 } 89 90 static const struct dmub_fw_meta_info * 91 dmub_get_fw_meta_info(const uint8_t *fw_bss_data, uint32_t fw_bss_data_size) 92 { 93 const union dmub_fw_meta *meta; 94 95 if (fw_bss_data == NULL) 96 return NULL; 97 98 if (fw_bss_data_size < sizeof(union dmub_fw_meta) + DMUB_FW_META_OFFSET) 99 return NULL; 100 101 meta = (const union dmub_fw_meta *)(fw_bss_data + fw_bss_data_size - 102 DMUB_FW_META_OFFSET - 103 sizeof(union dmub_fw_meta)); 104 105 if (meta->info.magic_value != DMUB_FW_META_MAGIC) 106 return NULL; 107 108 return &meta->info; 109 } 110 111 static bool dmub_srv_hw_setup(struct dmub_srv *dmub, enum dmub_asic asic) 112 { 113 struct dmub_srv_hw_funcs *funcs = &dmub->hw_funcs; 114 115 switch (asic) { 116 case DMUB_ASIC_DCN20: 117 case DMUB_ASIC_DCN21: 118 dmub->regs = &dmub_srv_dcn20_regs; 119 120 funcs->reset = dmub_dcn20_reset; 121 funcs->reset_release = dmub_dcn20_reset_release; 122 funcs->backdoor_load = dmub_dcn20_backdoor_load; 123 funcs->setup_windows = dmub_dcn20_setup_windows; 124 funcs->setup_mailbox = dmub_dcn20_setup_mailbox; 125 funcs->get_inbox1_rptr = dmub_dcn20_get_inbox1_rptr; 126 funcs->set_inbox1_wptr = dmub_dcn20_set_inbox1_wptr; 127 funcs->is_supported = dmub_dcn20_is_supported; 128 funcs->is_hw_init = dmub_dcn20_is_hw_init; 129 funcs->set_gpint = dmub_dcn20_set_gpint; 130 funcs->is_gpint_acked = dmub_dcn20_is_gpint_acked; 131 funcs->get_gpint_response = dmub_dcn20_get_gpint_response; 132 133 if (asic == DMUB_ASIC_DCN21) { 134 dmub->regs = &dmub_srv_dcn21_regs; 135 136 funcs->is_auto_load_done = dmub_dcn21_is_auto_load_done; 137 funcs->is_phy_init = dmub_dcn21_is_phy_init; 138 } 139 break; 140 141 default: 142 return false; 143 } 144 145 return true; 146 } 147 148 enum dmub_status dmub_srv_create(struct dmub_srv *dmub, 149 const struct dmub_srv_create_params *params) 150 { 151 enum dmub_status status = DMUB_STATUS_OK; 152 153 dmub_memset(dmub, 0, sizeof(*dmub)); 154 155 dmub->funcs = params->funcs; 156 dmub->user_ctx = params->user_ctx; 157 dmub->asic = params->asic; 158 dmub->is_virtual = params->is_virtual; 159 160 /* Setup asic dependent hardware funcs. */ 161 if (!dmub_srv_hw_setup(dmub, params->asic)) { 162 status = DMUB_STATUS_INVALID; 163 goto cleanup; 164 } 165 166 /* Override (some) hardware funcs based on user params. */ 167 if (params->hw_funcs) { 168 if (params->hw_funcs->get_inbox1_rptr) 169 dmub->hw_funcs.get_inbox1_rptr = 170 params->hw_funcs->get_inbox1_rptr; 171 172 if (params->hw_funcs->set_inbox1_wptr) 173 dmub->hw_funcs.set_inbox1_wptr = 174 params->hw_funcs->set_inbox1_wptr; 175 176 if (params->hw_funcs->is_supported) 177 dmub->hw_funcs.is_supported = 178 params->hw_funcs->is_supported; 179 } 180 181 /* Sanity checks for required hw func pointers. */ 182 if (!dmub->hw_funcs.get_inbox1_rptr || 183 !dmub->hw_funcs.set_inbox1_wptr) { 184 status = DMUB_STATUS_INVALID; 185 goto cleanup; 186 } 187 188 cleanup: 189 if (status == DMUB_STATUS_OK) 190 dmub->sw_init = true; 191 else 192 dmub_srv_destroy(dmub); 193 194 return status; 195 } 196 197 void dmub_srv_destroy(struct dmub_srv *dmub) 198 { 199 dmub_memset(dmub, 0, sizeof(*dmub)); 200 } 201 202 enum dmub_status 203 dmub_srv_calc_region_info(struct dmub_srv *dmub, 204 const struct dmub_srv_region_params *params, 205 struct dmub_srv_region_info *out) 206 { 207 struct dmub_region *inst = &out->regions[DMUB_WINDOW_0_INST_CONST]; 208 struct dmub_region *stack = &out->regions[DMUB_WINDOW_1_STACK]; 209 struct dmub_region *data = &out->regions[DMUB_WINDOW_2_BSS_DATA]; 210 struct dmub_region *bios = &out->regions[DMUB_WINDOW_3_VBIOS]; 211 struct dmub_region *mail = &out->regions[DMUB_WINDOW_4_MAILBOX]; 212 struct dmub_region *trace_buff = &out->regions[DMUB_WINDOW_5_TRACEBUFF]; 213 struct dmub_region *fw_state = &out->regions[DMUB_WINDOW_6_FW_STATE]; 214 const struct dmub_fw_meta_info *fw_info; 215 uint32_t fw_state_size = DMUB_FW_STATE_SIZE; 216 uint32_t trace_buffer_size = DMUB_TRACE_BUFFER_SIZE; 217 218 if (!dmub->sw_init) 219 return DMUB_STATUS_INVALID; 220 221 memset(out, 0, sizeof(*out)); 222 223 out->num_regions = DMUB_NUM_WINDOWS; 224 225 inst->base = 0x0; 226 inst->top = inst->base + params->inst_const_size; 227 228 data->base = dmub_align(inst->top, 256); 229 data->top = data->base + params->bss_data_size; 230 231 /* 232 * All cache windows below should be aligned to the size 233 * of the DMCUB cache line, 64 bytes. 234 */ 235 236 stack->base = dmub_align(data->top, 256); 237 stack->top = stack->base + DMUB_STACK_SIZE + DMUB_CONTEXT_SIZE; 238 239 bios->base = dmub_align(stack->top, 256); 240 bios->top = bios->base + params->vbios_size; 241 242 mail->base = dmub_align(bios->top, 256); 243 mail->top = mail->base + DMUB_MAILBOX_SIZE; 244 245 fw_info = dmub_get_fw_meta_info(params->fw_bss_data, 246 params->bss_data_size); 247 248 if (fw_info) { 249 fw_state_size = fw_info->fw_region_size; 250 trace_buffer_size = fw_info->trace_buffer_size; 251 } 252 253 trace_buff->base = dmub_align(mail->top, 256); 254 trace_buff->top = trace_buff->base + dmub_align(trace_buffer_size, 64); 255 256 fw_state->base = dmub_align(trace_buff->top, 256); 257 fw_state->top = fw_state->base + dmub_align(fw_state_size, 64); 258 259 out->fb_size = dmub_align(fw_state->top, 4096); 260 261 return DMUB_STATUS_OK; 262 } 263 264 enum dmub_status dmub_srv_calc_fb_info(struct dmub_srv *dmub, 265 const struct dmub_srv_fb_params *params, 266 struct dmub_srv_fb_info *out) 267 { 268 uint8_t *cpu_base; 269 uint64_t gpu_base; 270 uint32_t i; 271 272 if (!dmub->sw_init) 273 return DMUB_STATUS_INVALID; 274 275 memset(out, 0, sizeof(*out)); 276 277 if (params->region_info->num_regions != DMUB_NUM_WINDOWS) 278 return DMUB_STATUS_INVALID; 279 280 cpu_base = (uint8_t *)params->cpu_addr; 281 gpu_base = params->gpu_addr; 282 283 for (i = 0; i < DMUB_NUM_WINDOWS; ++i) { 284 const struct dmub_region *reg = 285 ¶ms->region_info->regions[i]; 286 287 out->fb[i].cpu_addr = cpu_base + reg->base; 288 out->fb[i].gpu_addr = gpu_base + reg->base; 289 out->fb[i].size = reg->top - reg->base; 290 } 291 292 out->num_fb = DMUB_NUM_WINDOWS; 293 294 return DMUB_STATUS_OK; 295 } 296 297 enum dmub_status dmub_srv_has_hw_support(struct dmub_srv *dmub, 298 bool *is_supported) 299 { 300 *is_supported = false; 301 302 if (!dmub->sw_init) 303 return DMUB_STATUS_INVALID; 304 305 if (dmub->hw_funcs.is_supported) 306 *is_supported = dmub->hw_funcs.is_supported(dmub); 307 308 return DMUB_STATUS_OK; 309 } 310 311 enum dmub_status dmub_srv_is_hw_init(struct dmub_srv *dmub, bool *is_hw_init) 312 { 313 *is_hw_init = false; 314 315 if (!dmub->sw_init) 316 return DMUB_STATUS_INVALID; 317 318 if (!dmub->hw_init) 319 return DMUB_STATUS_OK; 320 321 if (dmub->hw_funcs.is_hw_init) 322 *is_hw_init = dmub->hw_funcs.is_hw_init(dmub); 323 324 return DMUB_STATUS_OK; 325 } 326 327 enum dmub_status dmub_srv_hw_init(struct dmub_srv *dmub, 328 const struct dmub_srv_hw_params *params) 329 { 330 struct dmub_fb *inst_fb = params->fb[DMUB_WINDOW_0_INST_CONST]; 331 struct dmub_fb *stack_fb = params->fb[DMUB_WINDOW_1_STACK]; 332 struct dmub_fb *data_fb = params->fb[DMUB_WINDOW_2_BSS_DATA]; 333 struct dmub_fb *bios_fb = params->fb[DMUB_WINDOW_3_VBIOS]; 334 struct dmub_fb *mail_fb = params->fb[DMUB_WINDOW_4_MAILBOX]; 335 struct dmub_fb *tracebuff_fb = params->fb[DMUB_WINDOW_5_TRACEBUFF]; 336 struct dmub_fb *fw_state_fb = params->fb[DMUB_WINDOW_6_FW_STATE]; 337 338 struct dmub_rb_init_params rb_params; 339 struct dmub_window cw0, cw1, cw2, cw3, cw4, cw5, cw6; 340 struct dmub_region inbox1; 341 342 if (!dmub->sw_init) 343 return DMUB_STATUS_INVALID; 344 345 dmub->fb_base = params->fb_base; 346 dmub->fb_offset = params->fb_offset; 347 dmub->psp_version = params->psp_version; 348 349 if (inst_fb && data_fb) { 350 cw0.offset.quad_part = inst_fb->gpu_addr; 351 cw0.region.base = DMUB_CW0_BASE; 352 cw0.region.top = cw0.region.base + inst_fb->size - 1; 353 354 cw1.offset.quad_part = stack_fb->gpu_addr; 355 cw1.region.base = DMUB_CW1_BASE; 356 cw1.region.top = cw1.region.base + stack_fb->size - 1; 357 358 /** 359 * Read back all the instruction memory so we don't hang the 360 * DMCUB when backdoor loading if the write from x86 hasn't been 361 * flushed yet. This only occurs in backdoor loading. 362 */ 363 dmub_flush_buffer_mem(inst_fb); 364 365 if (params->load_inst_const && dmub->hw_funcs.backdoor_load) 366 dmub->hw_funcs.backdoor_load(dmub, &cw0, &cw1); 367 } 368 369 if (dmub->hw_funcs.reset) 370 dmub->hw_funcs.reset(dmub); 371 372 if (inst_fb && data_fb && bios_fb && mail_fb && tracebuff_fb && 373 fw_state_fb) { 374 cw2.offset.quad_part = data_fb->gpu_addr; 375 cw2.region.base = DMUB_CW0_BASE + inst_fb->size; 376 cw2.region.top = cw2.region.base + data_fb->size; 377 378 cw3.offset.quad_part = bios_fb->gpu_addr; 379 cw3.region.base = DMUB_CW3_BASE; 380 cw3.region.top = cw3.region.base + bios_fb->size; 381 382 cw4.offset.quad_part = mail_fb->gpu_addr; 383 cw4.region.base = cw3.region.top + 1; 384 cw4.region.top = cw4.region.base + mail_fb->size; 385 386 inbox1.base = cw4.region.base; 387 inbox1.top = cw4.region.top; 388 389 cw5.offset.quad_part = tracebuff_fb->gpu_addr; 390 cw5.region.base = DMUB_CW5_BASE; 391 cw5.region.top = cw5.region.base + tracebuff_fb->size; 392 393 cw6.offset.quad_part = fw_state_fb->gpu_addr; 394 cw6.region.base = DMUB_CW6_BASE; 395 cw6.region.top = cw6.region.base + fw_state_fb->size; 396 397 dmub->fw_state = fw_state_fb->cpu_addr; 398 399 if (dmub->hw_funcs.setup_windows) 400 dmub->hw_funcs.setup_windows(dmub, &cw2, &cw3, &cw4, 401 &cw5, &cw6); 402 403 if (dmub->hw_funcs.setup_mailbox) 404 dmub->hw_funcs.setup_mailbox(dmub, &inbox1); 405 } 406 407 if (mail_fb) { 408 dmub_memset(&rb_params, 0, sizeof(rb_params)); 409 rb_params.ctx = dmub; 410 rb_params.base_address = mail_fb->cpu_addr; 411 rb_params.capacity = DMUB_RB_SIZE; 412 413 dmub_rb_init(&dmub->inbox1_rb, &rb_params); 414 } 415 416 if (dmub->hw_funcs.reset_release) 417 dmub->hw_funcs.reset_release(dmub); 418 419 dmub->hw_init = true; 420 421 return DMUB_STATUS_OK; 422 } 423 424 enum dmub_status dmub_srv_hw_reset(struct dmub_srv *dmub) 425 { 426 if (!dmub->sw_init) 427 return DMUB_STATUS_INVALID; 428 429 if (dmub->hw_init == false) 430 return DMUB_STATUS_OK; 431 432 if (dmub->hw_funcs.reset) 433 dmub->hw_funcs.reset(dmub); 434 435 dmub->hw_init = false; 436 437 return DMUB_STATUS_OK; 438 } 439 440 enum dmub_status dmub_srv_cmd_queue(struct dmub_srv *dmub, 441 const struct dmub_cmd_header *cmd) 442 { 443 if (!dmub->hw_init) 444 return DMUB_STATUS_INVALID; 445 446 if (dmub_rb_push_front(&dmub->inbox1_rb, cmd)) 447 return DMUB_STATUS_OK; 448 449 return DMUB_STATUS_QUEUE_FULL; 450 } 451 452 enum dmub_status dmub_srv_cmd_execute(struct dmub_srv *dmub) 453 { 454 if (!dmub->hw_init) 455 return DMUB_STATUS_INVALID; 456 457 /** 458 * Read back all the queued commands to ensure that they've 459 * been flushed to framebuffer memory. Otherwise DMCUB might 460 * read back stale, fully invalid or partially invalid data. 461 */ 462 dmub_rb_flush_pending(&dmub->inbox1_rb); 463 464 dmub->hw_funcs.set_inbox1_wptr(dmub, dmub->inbox1_rb.wrpt); 465 return DMUB_STATUS_OK; 466 } 467 468 enum dmub_status dmub_srv_wait_for_auto_load(struct dmub_srv *dmub, 469 uint32_t timeout_us) 470 { 471 uint32_t i; 472 473 if (!dmub->hw_init) 474 return DMUB_STATUS_INVALID; 475 476 if (!dmub->hw_funcs.is_auto_load_done) 477 return DMUB_STATUS_OK; 478 479 for (i = 0; i <= timeout_us; i += 100) { 480 if (dmub->hw_funcs.is_auto_load_done(dmub)) 481 return DMUB_STATUS_OK; 482 483 udelay(100); 484 } 485 486 return DMUB_STATUS_TIMEOUT; 487 } 488 489 enum dmub_status dmub_srv_wait_for_phy_init(struct dmub_srv *dmub, 490 uint32_t timeout_us) 491 { 492 uint32_t i = 0; 493 494 if (!dmub->hw_init) 495 return DMUB_STATUS_INVALID; 496 497 if (!dmub->hw_funcs.is_phy_init) 498 return DMUB_STATUS_OK; 499 500 for (i = 0; i <= timeout_us; i += 10) { 501 if (dmub->hw_funcs.is_phy_init(dmub)) 502 return DMUB_STATUS_OK; 503 504 udelay(10); 505 } 506 507 return DMUB_STATUS_TIMEOUT; 508 } 509 510 enum dmub_status dmub_srv_wait_for_idle(struct dmub_srv *dmub, 511 uint32_t timeout_us) 512 { 513 uint32_t i; 514 515 if (!dmub->hw_init) 516 return DMUB_STATUS_INVALID; 517 518 for (i = 0; i <= timeout_us; ++i) { 519 dmub->inbox1_rb.rptr = dmub->hw_funcs.get_inbox1_rptr(dmub); 520 if (dmub_rb_empty(&dmub->inbox1_rb)) 521 return DMUB_STATUS_OK; 522 523 udelay(1); 524 } 525 526 return DMUB_STATUS_TIMEOUT; 527 } 528 529 enum dmub_status 530 dmub_srv_send_gpint_command(struct dmub_srv *dmub, 531 enum dmub_gpint_command command_code, 532 uint16_t param, uint32_t timeout_us) 533 { 534 union dmub_gpint_data_register reg; 535 uint32_t i; 536 537 if (!dmub->sw_init) 538 return DMUB_STATUS_INVALID; 539 540 if (!dmub->hw_funcs.set_gpint) 541 return DMUB_STATUS_INVALID; 542 543 if (!dmub->hw_funcs.is_gpint_acked) 544 return DMUB_STATUS_INVALID; 545 546 reg.bits.status = 1; 547 reg.bits.command_code = command_code; 548 reg.bits.param = param; 549 550 dmub->hw_funcs.set_gpint(dmub, reg); 551 552 for (i = 0; i < timeout_us; ++i) { 553 if (dmub->hw_funcs.is_gpint_acked(dmub, reg)) 554 return DMUB_STATUS_OK; 555 } 556 557 return DMUB_STATUS_TIMEOUT; 558 } 559 560 enum dmub_status dmub_srv_get_gpint_response(struct dmub_srv *dmub, 561 uint32_t *response) 562 { 563 *response = 0; 564 565 if (!dmub->sw_init) 566 return DMUB_STATUS_INVALID; 567 568 if (!dmub->hw_funcs.get_gpint_response) 569 return DMUB_STATUS_INVALID; 570 571 *response = dmub->hw_funcs.get_gpint_response(dmub); 572 573 return DMUB_STATUS_OK; 574 } 575