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 #ifndef _DMUB_SRV_H_ 27 #define _DMUB_SRV_H_ 28 29 /** 30 * DOC: DMUB interface and operation 31 * 32 * DMUB is the interface to the display DMCUB microcontroller on DCN hardware. 33 * It delegates hardware initialization and command submission to the 34 * microcontroller. DMUB is the shortname for DMCUB. 35 * 36 * This interface is not thread-safe. Ensure that all access to the interface 37 * is properly synchronized by the caller. 38 * 39 * Initialization and usage of the DMUB service should be done in the 40 * steps given below: 41 * 42 * 1. dmub_srv_create() 43 * 2. dmub_srv_has_hw_support() 44 * 3. dmub_srv_calc_region_info() 45 * 4. dmub_srv_hw_init() 46 * 47 * The call to dmub_srv_create() is required to use the server. 48 * 49 * The calls to dmub_srv_has_hw_support() and dmub_srv_calc_region_info() 50 * are helpers to query cache window size and allocate framebuffer(s) 51 * for the cache windows. 52 * 53 * The call to dmub_srv_hw_init() programs the DMCUB registers to prepare 54 * for command submission. Commands can be queued via dmub_srv_cmd_queue() 55 * and executed via dmub_srv_cmd_execute(). 56 * 57 * If the queue is full the dmub_srv_wait_for_idle() call can be used to 58 * wait until the queue has been cleared. 59 * 60 * Destroying the DMUB service can be done by calling dmub_srv_destroy(). 61 * This does not clear DMUB hardware state, only software state. 62 * 63 * The interface is intended to be standalone and should not depend on any 64 * other component within DAL. 65 */ 66 67 #include "inc/dmub_cmd.h" 68 69 #if defined(__cplusplus) 70 extern "C" { 71 #endif 72 73 /* Forward declarations */ 74 struct dmub_srv; 75 struct dmub_srv_common_regs; 76 77 /* enum dmub_status - return code for dmcub functions */ 78 enum dmub_status { 79 DMUB_STATUS_OK = 0, 80 DMUB_STATUS_NO_CTX, 81 DMUB_STATUS_QUEUE_FULL, 82 DMUB_STATUS_TIMEOUT, 83 DMUB_STATUS_INVALID, 84 }; 85 86 /* enum dmub_asic - dmub asic identifier */ 87 enum dmub_asic { 88 DMUB_ASIC_NONE = 0, 89 DMUB_ASIC_DCN20, 90 DMUB_ASIC_DCN21, 91 #ifdef CONFIG_DRM_AMD_DC_DCN3_0 92 DMUB_ASIC_DCN30, 93 #endif 94 DMUB_ASIC_MAX, 95 }; 96 97 /* enum dmub_window_id - dmub window identifier */ 98 enum dmub_window_id { 99 DMUB_WINDOW_0_INST_CONST = 0, 100 DMUB_WINDOW_1_STACK, 101 DMUB_WINDOW_2_BSS_DATA, 102 DMUB_WINDOW_3_VBIOS, 103 DMUB_WINDOW_4_MAILBOX, 104 DMUB_WINDOW_5_TRACEBUFF, 105 DMUB_WINDOW_6_FW_STATE, 106 DMUB_WINDOW_7_SCRATCH_MEM, 107 DMUB_WINDOW_TOTAL, 108 }; 109 110 /** 111 * struct dmub_region - dmub hw memory region 112 * @base: base address for region, must be 256 byte aligned 113 * @top: top address for region 114 */ 115 struct dmub_region { 116 uint32_t base; 117 uint32_t top; 118 }; 119 120 /** 121 * struct dmub_window - dmub hw cache window 122 * @off: offset to the fb memory in gpu address space 123 * @r: region in uc address space for cache window 124 */ 125 struct dmub_window { 126 union dmub_addr offset; 127 struct dmub_region region; 128 }; 129 130 /** 131 * struct dmub_fb - defines a dmub framebuffer memory region 132 * @cpu_addr: cpu virtual address for the region, NULL if invalid 133 * @gpu_addr: gpu virtual address for the region, NULL if invalid 134 * @size: size of the region in bytes, zero if invalid 135 */ 136 struct dmub_fb { 137 void *cpu_addr; 138 uint64_t gpu_addr; 139 uint32_t size; 140 }; 141 142 /** 143 * struct dmub_srv_region_params - params used for calculating dmub regions 144 * @inst_const_size: size of the fw inst const section 145 * @bss_data_size: size of the fw bss data section 146 * @vbios_size: size of the vbios data 147 * @fw_bss_data: raw firmware bss data section 148 */ 149 struct dmub_srv_region_params { 150 uint32_t inst_const_size; 151 uint32_t bss_data_size; 152 uint32_t vbios_size; 153 const uint8_t *fw_inst_const; 154 const uint8_t *fw_bss_data; 155 bool is_mailbox_in_inbox; 156 }; 157 158 /** 159 * struct dmub_srv_region_info - output region info from the dmub service 160 * @fb_size: required minimum fb size for all regions, aligned to 4096 bytes 161 * @num_regions: number of regions used by the dmub service 162 * @regions: region info 163 * 164 * The regions are aligned such that they can be all placed within the 165 * same framebuffer but they can also be placed into different framebuffers. 166 * 167 * The size of each region can be calculated by the caller: 168 * size = reg.top - reg.base 169 * 170 * Care must be taken when performing custom allocations to ensure that each 171 * region base address is 256 byte aligned. 172 */ 173 struct dmub_srv_region_info { 174 uint32_t fb_size; 175 uint32_t inbox_size; 176 uint8_t num_regions; 177 struct dmub_region regions[DMUB_WINDOW_TOTAL]; 178 }; 179 180 /** 181 * struct dmub_srv_memory_params - parameters used for driver fb setup 182 * @region_info: region info calculated by dmub service 183 * @cpu_fb_addr: base cpu address for the framebuffer 184 * @cpu_inbox_addr: base cpu address for the gart 185 * @gpu_fb_addr: base gpu virtual address for the framebuffer 186 * @gpu_inbox_addr: base gpu virtual address for the gart 187 */ 188 struct dmub_srv_memory_params { 189 const struct dmub_srv_region_info *region_info; 190 void *cpu_fb_addr; 191 void *cpu_inbox_addr; 192 uint64_t gpu_fb_addr; 193 uint64_t gpu_inbox_addr; 194 }; 195 196 /** 197 * struct dmub_srv_fb_info - output fb info from the dmub service 198 * @num_fbs: number of required dmub framebuffers 199 * @fbs: fb data for each region 200 * 201 * Output from the dmub service helper that can be used by the 202 * driver to prepare dmub_fb that can be passed into the dmub 203 * hw init service. 204 * 205 * Assumes that all regions are within the same framebuffer 206 * and have been setup according to the region_info generated 207 * by the dmub service. 208 */ 209 struct dmub_srv_fb_info { 210 uint8_t num_fb; 211 struct dmub_fb fb[DMUB_WINDOW_TOTAL]; 212 }; 213 214 /** 215 * struct dmub_srv_base_funcs - Driver specific base callbacks 216 */ 217 struct dmub_srv_base_funcs { 218 /** 219 * @reg_read: 220 * 221 * Hook for reading a register. 222 * 223 * Return: The 32-bit register value from the given address. 224 */ 225 uint32_t (*reg_read)(void *ctx, uint32_t address); 226 227 /** 228 * @reg_write: 229 * 230 * Hook for writing a value to the register specified by address. 231 */ 232 void (*reg_write)(void *ctx, uint32_t address, uint32_t value); 233 }; 234 235 /** 236 * struct dmub_srv_hw_funcs - hardware sequencer funcs for dmub 237 */ 238 struct dmub_srv_hw_funcs { 239 /* private: internal use only */ 240 241 void (*init)(struct dmub_srv *dmub); 242 243 void (*reset)(struct dmub_srv *dmub); 244 245 void (*reset_release)(struct dmub_srv *dmub); 246 247 void (*backdoor_load)(struct dmub_srv *dmub, 248 const struct dmub_window *cw0, 249 const struct dmub_window *cw1); 250 251 void (*setup_windows)(struct dmub_srv *dmub, 252 const struct dmub_window *cw2, 253 const struct dmub_window *cw3, 254 const struct dmub_window *cw4, 255 const struct dmub_window *cw5, 256 const struct dmub_window *cw6); 257 258 void (*setup_mailbox)(struct dmub_srv *dmub, 259 const struct dmub_region *inbox1); 260 261 uint32_t (*get_inbox1_rptr)(struct dmub_srv *dmub); 262 263 void (*set_inbox1_wptr)(struct dmub_srv *dmub, uint32_t wptr_offset); 264 265 uint32_t (*emul_get_inbox1_rptr)(struct dmub_srv *dmub); 266 267 void (*emul_set_inbox1_wptr)(struct dmub_srv *dmub, uint32_t wptr_offset); 268 269 bool (*is_supported)(struct dmub_srv *dmub); 270 271 bool (*is_hw_init)(struct dmub_srv *dmub); 272 273 bool (*is_phy_init)(struct dmub_srv *dmub); 274 275 bool (*is_auto_load_done)(struct dmub_srv *dmub); 276 277 void (*set_gpint)(struct dmub_srv *dmub, 278 union dmub_gpint_data_register reg); 279 280 bool (*is_gpint_acked)(struct dmub_srv *dmub, 281 union dmub_gpint_data_register reg); 282 283 uint32_t (*get_gpint_response)(struct dmub_srv *dmub); 284 }; 285 286 /** 287 * struct dmub_srv_create_params - params for dmub service creation 288 * @base_funcs: driver supplied base routines 289 * @hw_funcs: optional overrides for hw funcs 290 * @user_ctx: context data for callback funcs 291 * @asic: driver supplied asic 292 * @fw_version: the current firmware version, if any 293 * @is_virtual: false for hw support only 294 */ 295 struct dmub_srv_create_params { 296 struct dmub_srv_base_funcs funcs; 297 struct dmub_srv_hw_funcs *hw_funcs; 298 void *user_ctx; 299 enum dmub_asic asic; 300 uint32_t fw_version; 301 bool is_virtual; 302 }; 303 304 /* 305 * struct dmub_srv_hw_params - params for dmub hardware initialization 306 * @fb: framebuffer info for each region 307 * @fb_base: base of the framebuffer aperture 308 * @fb_offset: offset of the framebuffer aperture 309 * @psp_version: psp version to pass for DMCU init 310 * @load_inst_const: true if DMUB should load inst const fw 311 */ 312 struct dmub_srv_hw_params { 313 struct dmub_fb *fb[DMUB_WINDOW_TOTAL]; 314 uint64_t fb_base; 315 uint64_t fb_offset; 316 uint32_t psp_version; 317 bool load_inst_const; 318 }; 319 320 /** 321 * struct dmub_srv - software state for dmcub 322 * @asic: dmub asic identifier 323 * @user_ctx: user provided context for the dmub_srv 324 * @fw_version: the current firmware version, if any 325 * @is_virtual: false if hardware support only 326 * @fw_state: dmub firmware state pointer 327 */ 328 struct dmub_srv { 329 enum dmub_asic asic; 330 void *user_ctx; 331 uint32_t fw_version; 332 bool is_virtual; 333 struct dmub_fb scratch_mem_fb; 334 volatile const struct dmub_fw_state *fw_state; 335 336 /* private: internal use only */ 337 const struct dmub_srv_common_regs *regs; 338 339 struct dmub_srv_base_funcs funcs; 340 struct dmub_srv_hw_funcs hw_funcs; 341 struct dmub_rb inbox1_rb; 342 343 bool sw_init; 344 bool hw_init; 345 346 uint64_t fb_base; 347 uint64_t fb_offset; 348 uint32_t psp_version; 349 }; 350 351 /** 352 * DMUB firmware version helper macro - useful for checking if the version 353 * of a firmware to know if feature or functionality is supported or present. 354 */ 355 #define DMUB_FW_VERSION(major, minor, revision) \ 356 ((((major) & 0xFF) << 24) | (((minor) & 0xFF) << 16) | (((revision) & 0xFF) << 8)) 357 358 /** 359 * dmub_srv_create() - creates the DMUB service. 360 * @dmub: the dmub service 361 * @params: creation parameters for the service 362 * 363 * Return: 364 * DMUB_STATUS_OK - success 365 * DMUB_STATUS_INVALID - unspecified error 366 */ 367 enum dmub_status dmub_srv_create(struct dmub_srv *dmub, 368 const struct dmub_srv_create_params *params); 369 370 /** 371 * dmub_srv_destroy() - destroys the DMUB service. 372 * @dmub: the dmub service 373 */ 374 void dmub_srv_destroy(struct dmub_srv *dmub); 375 376 /** 377 * dmub_srv_calc_region_info() - retreives region info from the dmub service 378 * @dmub: the dmub service 379 * @params: parameters used to calculate region locations 380 * @info_out: the output region info from dmub 381 * 382 * Calculates the base and top address for all relevant dmub regions 383 * using the parameters given (if any). 384 * 385 * Return: 386 * DMUB_STATUS_OK - success 387 * DMUB_STATUS_INVALID - unspecified error 388 */ 389 enum dmub_status 390 dmub_srv_calc_region_info(struct dmub_srv *dmub, 391 const struct dmub_srv_region_params *params, 392 struct dmub_srv_region_info *out); 393 394 /** 395 * dmub_srv_calc_region_info() - retreives fb info from the dmub service 396 * @dmub: the dmub service 397 * @params: parameters used to calculate fb locations 398 * @info_out: the output fb info from dmub 399 * 400 * Calculates the base and top address for all relevant dmub regions 401 * using the parameters given (if any). 402 * 403 * Return: 404 * DMUB_STATUS_OK - success 405 * DMUB_STATUS_INVALID - unspecified error 406 */ 407 enum dmub_status dmub_srv_calc_mem_info(struct dmub_srv *dmub, 408 const struct dmub_srv_memory_params *params, 409 struct dmub_srv_fb_info *out); 410 411 /** 412 * dmub_srv_has_hw_support() - returns hw support state for dmcub 413 * @dmub: the dmub service 414 * @is_supported: hw support state 415 * 416 * Queries the hardware for DMCUB support and returns the result. 417 * 418 * Can be called before dmub_srv_hw_init(). 419 * 420 * Return: 421 * DMUB_STATUS_OK - success 422 * DMUB_STATUS_INVALID - unspecified error 423 */ 424 enum dmub_status dmub_srv_has_hw_support(struct dmub_srv *dmub, 425 bool *is_supported); 426 427 /** 428 * dmub_srv_is_hw_init() - returns hardware init state 429 * 430 * Return: 431 * DMUB_STATUS_OK - success 432 * DMUB_STATUS_INVALID - unspecified error 433 */ 434 enum dmub_status dmub_srv_is_hw_init(struct dmub_srv *dmub, bool *is_hw_init); 435 436 /** 437 * dmub_srv_hw_init() - initializes the underlying DMUB hardware 438 * @dmub: the dmub service 439 * @params: params for hardware initialization 440 * 441 * Resets the DMUB hardware and performs backdoor loading of the 442 * required cache regions based on the input framebuffer regions. 443 * 444 * Return: 445 * DMUB_STATUS_OK - success 446 * DMUB_STATUS_NO_CTX - dmcub context not initialized 447 * DMUB_STATUS_INVALID - unspecified error 448 */ 449 enum dmub_status dmub_srv_hw_init(struct dmub_srv *dmub, 450 const struct dmub_srv_hw_params *params); 451 452 /** 453 * dmub_srv_hw_reset() - puts the DMUB hardware in reset state if initialized 454 * @dmub: the dmub service 455 * 456 * Before destroying the DMUB service or releasing the backing framebuffer 457 * memory we'll need to put the DMCUB into reset first. 458 * 459 * A subsequent call to dmub_srv_hw_init() will re-enable the DMCUB. 460 * 461 * Return: 462 * DMUB_STATUS_OK - success 463 * DMUB_STATUS_INVALID - unspecified error 464 */ 465 enum dmub_status dmub_srv_hw_reset(struct dmub_srv *dmub); 466 467 /** 468 * dmub_srv_cmd_queue() - queues a command to the DMUB 469 * @dmub: the dmub service 470 * @cmd: the command to queue 471 * 472 * Queues a command to the DMUB service but does not begin execution 473 * immediately. 474 * 475 * Return: 476 * DMUB_STATUS_OK - success 477 * DMUB_STATUS_QUEUE_FULL - no remaining room in queue 478 * DMUB_STATUS_INVALID - unspecified error 479 */ 480 enum dmub_status dmub_srv_cmd_queue(struct dmub_srv *dmub, 481 const union dmub_rb_cmd *cmd); 482 483 /** 484 * dmub_srv_cmd_execute() - Executes a queued sequence to the dmub 485 * @dmub: the dmub service 486 * 487 * Begins execution of queued commands on the dmub. 488 * 489 * Return: 490 * DMUB_STATUS_OK - success 491 * DMUB_STATUS_INVALID - unspecified error 492 */ 493 enum dmub_status dmub_srv_cmd_execute(struct dmub_srv *dmub); 494 495 /** 496 * dmub_srv_wait_for_auto_load() - Waits for firmware auto load to complete 497 * @dmub: the dmub service 498 * @timeout_us: the maximum number of microseconds to wait 499 * 500 * Waits until firmware has been autoloaded by the DMCUB. The maximum 501 * wait time is given in microseconds to prevent spinning forever. 502 * 503 * On ASICs without firmware autoload support this function will return 504 * immediately. 505 * 506 * Return: 507 * DMUB_STATUS_OK - success 508 * DMUB_STATUS_TIMEOUT - wait for phy init timed out 509 * DMUB_STATUS_INVALID - unspecified error 510 */ 511 enum dmub_status dmub_srv_wait_for_auto_load(struct dmub_srv *dmub, 512 uint32_t timeout_us); 513 514 /** 515 * dmub_srv_wait_for_phy_init() - Waits for DMUB PHY init to complete 516 * @dmub: the dmub service 517 * @timeout_us: the maximum number of microseconds to wait 518 * 519 * Waits until the PHY has been initialized by the DMUB. The maximum 520 * wait time is given in microseconds to prevent spinning forever. 521 * 522 * On ASICs without PHY init support this function will return 523 * immediately. 524 * 525 * Return: 526 * DMUB_STATUS_OK - success 527 * DMUB_STATUS_TIMEOUT - wait for phy init timed out 528 * DMUB_STATUS_INVALID - unspecified error 529 */ 530 enum dmub_status dmub_srv_wait_for_phy_init(struct dmub_srv *dmub, 531 uint32_t timeout_us); 532 533 /** 534 * dmub_srv_wait_for_idle() - Waits for the DMUB to be idle 535 * @dmub: the dmub service 536 * @timeout_us: the maximum number of microseconds to wait 537 * 538 * Waits until the DMUB buffer is empty and all commands have 539 * finished processing. The maximum wait time is given in 540 * microseconds to prevent spinning forever. 541 * 542 * Return: 543 * DMUB_STATUS_OK - success 544 * DMUB_STATUS_TIMEOUT - wait for buffer to flush timed out 545 * DMUB_STATUS_INVALID - unspecified error 546 */ 547 enum dmub_status dmub_srv_wait_for_idle(struct dmub_srv *dmub, 548 uint32_t timeout_us); 549 550 /** 551 * dmub_srv_send_gpint_command() - Sends a GPINT based command. 552 * @dmub: the dmub service 553 * @command_code: the command code to send 554 * @param: the command parameter to send 555 * @timeout_us: the maximum number of microseconds to wait 556 * 557 * Sends a command via the general purpose interrupt (GPINT). 558 * Waits for the number of microseconds specified by timeout_us 559 * for the command ACK before returning. 560 * 561 * Can be called after software initialization. 562 * 563 * Return: 564 * DMUB_STATUS_OK - success 565 * DMUB_STATUS_TIMEOUT - wait for ACK timed out 566 * DMUB_STATUS_INVALID - unspecified error 567 */ 568 enum dmub_status 569 dmub_srv_send_gpint_command(struct dmub_srv *dmub, 570 enum dmub_gpint_command command_code, 571 uint16_t param, uint32_t timeout_us); 572 573 /** 574 * dmub_srv_get_gpint_response() - Queries the GPINT response. 575 * @dmub: the dmub service 576 * @response: the response for the last GPINT 577 * 578 * Returns the response code for the last GPINT interrupt. 579 * 580 * Can be called after software initialization. 581 * 582 * Return: 583 * DMUB_STATUS_OK - success 584 * DMUB_STATUS_INVALID - unspecified error 585 */ 586 enum dmub_status dmub_srv_get_gpint_response(struct dmub_srv *dmub, 587 uint32_t *response); 588 589 /** 590 * dmub_flush_buffer_mem() - Read back entire frame buffer region. 591 * This ensures that the write from x86 has been flushed and will not 592 * hang the DMCUB. 593 * @fb: frame buffer to flush 594 * 595 * Can be called after software initialization. 596 */ 597 void dmub_flush_buffer_mem(const struct dmub_fb *fb); 598 599 #if defined(__cplusplus) 600 } 601 #endif 602 603 #endif /* _DMUB_SRV_H_ */ 604