1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (c) 2014-2018, The Linux Foundation. All rights reserved. 4 * Copyright (C) 2013 Red Hat 5 * Author: Rob Clark <robdclark@gmail.com> 6 */ 7 8 #define pr_fmt(fmt) "[drm:%s:%d] " fmt, __func__, __LINE__ 9 #include <linux/debugfs.h> 10 #include <linux/kthread.h> 11 #include <linux/seq_file.h> 12 13 #include <drm/drm_crtc.h> 14 #include <drm/drm_file.h> 15 #include <drm/drm_probe_helper.h> 16 17 #include "msm_drv.h" 18 #include "dpu_kms.h" 19 #include "dpu_hwio.h" 20 #include "dpu_hw_catalog.h" 21 #include "dpu_hw_intf.h" 22 #include "dpu_hw_ctl.h" 23 #include "dpu_hw_dspp.h" 24 #include "dpu_formats.h" 25 #include "dpu_encoder_phys.h" 26 #include "dpu_crtc.h" 27 #include "dpu_trace.h" 28 #include "dpu_core_irq.h" 29 30 #define DPU_DEBUG_ENC(e, fmt, ...) DPU_DEBUG("enc%d " fmt,\ 31 (e) ? (e)->base.base.id : -1, ##__VA_ARGS__) 32 33 #define DPU_ERROR_ENC(e, fmt, ...) DPU_ERROR("enc%d " fmt,\ 34 (e) ? (e)->base.base.id : -1, ##__VA_ARGS__) 35 36 #define DPU_DEBUG_PHYS(p, fmt, ...) DPU_DEBUG("enc%d intf%d pp%d " fmt,\ 37 (p) ? (p)->parent->base.id : -1, \ 38 (p) ? (p)->intf_idx - INTF_0 : -1, \ 39 (p) ? ((p)->hw_pp ? (p)->hw_pp->idx - PINGPONG_0 : -1) : -1, \ 40 ##__VA_ARGS__) 41 42 #define DPU_ERROR_PHYS(p, fmt, ...) DPU_ERROR("enc%d intf%d pp%d " fmt,\ 43 (p) ? (p)->parent->base.id : -1, \ 44 (p) ? (p)->intf_idx - INTF_0 : -1, \ 45 (p) ? ((p)->hw_pp ? (p)->hw_pp->idx - PINGPONG_0 : -1) : -1, \ 46 ##__VA_ARGS__) 47 48 /* 49 * Two to anticipate panels that can do cmd/vid dynamic switching 50 * plan is to create all possible physical encoder types, and switch between 51 * them at runtime 52 */ 53 #define NUM_PHYS_ENCODER_TYPES 2 54 55 #define MAX_PHYS_ENCODERS_PER_VIRTUAL \ 56 (MAX_H_TILES_PER_DISPLAY * NUM_PHYS_ENCODER_TYPES) 57 58 #define MAX_CHANNELS_PER_ENC 2 59 60 #define IDLE_SHORT_TIMEOUT 1 61 62 #define MAX_HDISPLAY_SPLIT 1080 63 64 /* timeout in frames waiting for frame done */ 65 #define DPU_ENCODER_FRAME_DONE_TIMEOUT_FRAMES 5 66 67 /** 68 * enum dpu_enc_rc_events - events for resource control state machine 69 * @DPU_ENC_RC_EVENT_KICKOFF: 70 * This event happens at NORMAL priority. 71 * Event that signals the start of the transfer. When this event is 72 * received, enable MDP/DSI core clocks. Regardless of the previous 73 * state, the resource should be in ON state at the end of this event. 74 * @DPU_ENC_RC_EVENT_FRAME_DONE: 75 * This event happens at INTERRUPT level. 76 * Event signals the end of the data transfer after the PP FRAME_DONE 77 * event. At the end of this event, a delayed work is scheduled to go to 78 * IDLE_PC state after IDLE_TIMEOUT time. 79 * @DPU_ENC_RC_EVENT_PRE_STOP: 80 * This event happens at NORMAL priority. 81 * This event, when received during the ON state, leave the RC STATE 82 * in the PRE_OFF state. It should be followed by the STOP event as 83 * part of encoder disable. 84 * If received during IDLE or OFF states, it will do nothing. 85 * @DPU_ENC_RC_EVENT_STOP: 86 * This event happens at NORMAL priority. 87 * When this event is received, disable all the MDP/DSI core clocks, and 88 * disable IRQs. It should be called from the PRE_OFF or IDLE states. 89 * IDLE is expected when IDLE_PC has run, and PRE_OFF did nothing. 90 * PRE_OFF is expected when PRE_STOP was executed during the ON state. 91 * Resource state should be in OFF at the end of the event. 92 * @DPU_ENC_RC_EVENT_ENTER_IDLE: 93 * This event happens at NORMAL priority from a work item. 94 * Event signals that there were no frame updates for IDLE_TIMEOUT time. 95 * This would disable MDP/DSI core clocks and change the resource state 96 * to IDLE. 97 */ 98 enum dpu_enc_rc_events { 99 DPU_ENC_RC_EVENT_KICKOFF = 1, 100 DPU_ENC_RC_EVENT_FRAME_DONE, 101 DPU_ENC_RC_EVENT_PRE_STOP, 102 DPU_ENC_RC_EVENT_STOP, 103 DPU_ENC_RC_EVENT_ENTER_IDLE 104 }; 105 106 /* 107 * enum dpu_enc_rc_states - states that the resource control maintains 108 * @DPU_ENC_RC_STATE_OFF: Resource is in OFF state 109 * @DPU_ENC_RC_STATE_PRE_OFF: Resource is transitioning to OFF state 110 * @DPU_ENC_RC_STATE_ON: Resource is in ON state 111 * @DPU_ENC_RC_STATE_MODESET: Resource is in modeset state 112 * @DPU_ENC_RC_STATE_IDLE: Resource is in IDLE state 113 */ 114 enum dpu_enc_rc_states { 115 DPU_ENC_RC_STATE_OFF, 116 DPU_ENC_RC_STATE_PRE_OFF, 117 DPU_ENC_RC_STATE_ON, 118 DPU_ENC_RC_STATE_IDLE 119 }; 120 121 /** 122 * struct dpu_encoder_virt - virtual encoder. Container of one or more physical 123 * encoders. Virtual encoder manages one "logical" display. Physical 124 * encoders manage one intf block, tied to a specific panel/sub-panel. 125 * Virtual encoder defers as much as possible to the physical encoders. 126 * Virtual encoder registers itself with the DRM Framework as the encoder. 127 * @base: drm_encoder base class for registration with DRM 128 * @enc_spinlock: Virtual-Encoder-Wide Spin Lock for IRQ purposes 129 * @bus_scaling_client: Client handle to the bus scaling interface 130 * @enabled: True if the encoder is active, protected by enc_lock 131 * @num_phys_encs: Actual number of physical encoders contained. 132 * @phys_encs: Container of physical encoders managed. 133 * @cur_master: Pointer to the current master in this mode. Optimization 134 * Only valid after enable. Cleared as disable. 135 * @cur_slave: As above but for the slave encoder. 136 * @hw_pp: Handle to the pingpong blocks used for the display. No. 137 * pingpong blocks can be different than num_phys_encs. 138 * @intfs_swapped: Whether or not the phys_enc interfaces have been swapped 139 * for partial update right-only cases, such as pingpong 140 * split where virtual pingpong does not generate IRQs 141 * @crtc: Pointer to the currently assigned crtc. Normally you 142 * would use crtc->state->encoder_mask to determine the 143 * link between encoder/crtc. However in this case we need 144 * to track crtc in the disable() hook which is called 145 * _after_ encoder_mask is cleared. 146 * @crtc_kickoff_cb: Callback into CRTC that will flush & start 147 * all CTL paths 148 * @crtc_kickoff_cb_data: Opaque user data given to crtc_kickoff_cb 149 * @debugfs_root: Debug file system root file node 150 * @enc_lock: Lock around physical encoder 151 * create/destroy/enable/disable 152 * @frame_busy_mask: Bitmask tracking which phys_enc we are still 153 * busy processing current command. 154 * Bit0 = phys_encs[0] etc. 155 * @crtc_frame_event_cb: callback handler for frame event 156 * @crtc_frame_event_cb_data: callback handler private data 157 * @frame_done_timeout_ms: frame done timeout in ms 158 * @frame_done_timer: watchdog timer for frame done event 159 * @vsync_event_timer: vsync timer 160 * @disp_info: local copy of msm_display_info struct 161 * @idle_pc_supported: indicate if idle power collaps is supported 162 * @rc_lock: resource control mutex lock to protect 163 * virt encoder over various state changes 164 * @rc_state: resource controller state 165 * @delayed_off_work: delayed worker to schedule disabling of 166 * clks and resources after IDLE_TIMEOUT time. 167 * @vsync_event_work: worker to handle vsync event for autorefresh 168 * @topology: topology of the display 169 * @idle_timeout: idle timeout duration in milliseconds 170 */ 171 struct dpu_encoder_virt { 172 struct drm_encoder base; 173 spinlock_t enc_spinlock; 174 uint32_t bus_scaling_client; 175 176 bool enabled; 177 178 unsigned int num_phys_encs; 179 struct dpu_encoder_phys *phys_encs[MAX_PHYS_ENCODERS_PER_VIRTUAL]; 180 struct dpu_encoder_phys *cur_master; 181 struct dpu_encoder_phys *cur_slave; 182 struct dpu_hw_pingpong *hw_pp[MAX_CHANNELS_PER_ENC]; 183 184 bool intfs_swapped; 185 186 struct drm_crtc *crtc; 187 188 struct dentry *debugfs_root; 189 struct mutex enc_lock; 190 DECLARE_BITMAP(frame_busy_mask, MAX_PHYS_ENCODERS_PER_VIRTUAL); 191 void (*crtc_frame_event_cb)(void *, u32 event); 192 void *crtc_frame_event_cb_data; 193 194 atomic_t frame_done_timeout_ms; 195 struct timer_list frame_done_timer; 196 struct timer_list vsync_event_timer; 197 198 struct msm_display_info disp_info; 199 200 bool idle_pc_supported; 201 struct mutex rc_lock; 202 enum dpu_enc_rc_states rc_state; 203 struct delayed_work delayed_off_work; 204 struct kthread_work vsync_event_work; 205 struct msm_display_topology topology; 206 207 u32 idle_timeout; 208 }; 209 210 #define to_dpu_encoder_virt(x) container_of(x, struct dpu_encoder_virt, base) 211 212 static u32 dither_matrix[DITHER_MATRIX_SZ] = { 213 15, 7, 13, 5, 3, 11, 1, 9, 12, 4, 14, 6, 0, 8, 2, 10 214 }; 215 216 static void _dpu_encoder_setup_dither(struct dpu_hw_pingpong *hw_pp, unsigned bpc) 217 { 218 struct dpu_hw_dither_cfg dither_cfg = { 0 }; 219 220 if (!hw_pp->ops.setup_dither) 221 return; 222 223 switch (bpc) { 224 case 6: 225 dither_cfg.c0_bitdepth = 6; 226 dither_cfg.c1_bitdepth = 6; 227 dither_cfg.c2_bitdepth = 6; 228 dither_cfg.c3_bitdepth = 6; 229 dither_cfg.temporal_en = 0; 230 break; 231 default: 232 hw_pp->ops.setup_dither(hw_pp, NULL); 233 return; 234 } 235 236 memcpy(&dither_cfg.matrix, dither_matrix, 237 sizeof(u32) * DITHER_MATRIX_SZ); 238 239 hw_pp->ops.setup_dither(hw_pp, &dither_cfg); 240 } 241 242 void dpu_encoder_helper_report_irq_timeout(struct dpu_encoder_phys *phys_enc, 243 enum dpu_intr_idx intr_idx) 244 { 245 DRM_ERROR("irq timeout id=%u, intf=%d, pp=%d, intr=%d\n", 246 DRMID(phys_enc->parent), phys_enc->intf_idx - INTF_0, 247 phys_enc->hw_pp->idx - PINGPONG_0, intr_idx); 248 249 if (phys_enc->parent_ops->handle_frame_done) 250 phys_enc->parent_ops->handle_frame_done( 251 phys_enc->parent, phys_enc, 252 DPU_ENCODER_FRAME_EVENT_ERROR); 253 } 254 255 static int dpu_encoder_helper_wait_event_timeout(int32_t drm_id, 256 int32_t hw_id, struct dpu_encoder_wait_info *info); 257 258 int dpu_encoder_helper_wait_for_irq(struct dpu_encoder_phys *phys_enc, 259 enum dpu_intr_idx intr_idx, 260 struct dpu_encoder_wait_info *wait_info) 261 { 262 struct dpu_encoder_irq *irq; 263 u32 irq_status; 264 int ret; 265 266 if (!wait_info || intr_idx >= INTR_IDX_MAX) { 267 DPU_ERROR("invalid params\n"); 268 return -EINVAL; 269 } 270 irq = &phys_enc->irq[intr_idx]; 271 272 /* note: do master / slave checking outside */ 273 274 /* return EWOULDBLOCK since we know the wait isn't necessary */ 275 if (phys_enc->enable_state == DPU_ENC_DISABLED) { 276 DRM_ERROR("encoder is disabled id=%u, intr=%d, hw=%d, irq=%d", 277 DRMID(phys_enc->parent), intr_idx, irq->hw_idx, 278 irq->irq_idx); 279 return -EWOULDBLOCK; 280 } 281 282 if (irq->irq_idx < 0) { 283 DRM_DEBUG_KMS("skip irq wait id=%u, intr=%d, hw=%d, irq=%s", 284 DRMID(phys_enc->parent), intr_idx, irq->hw_idx, 285 irq->name); 286 return 0; 287 } 288 289 DRM_DEBUG_KMS("id=%u, intr=%d, hw=%d, irq=%d, pp=%d, pending_cnt=%d", 290 DRMID(phys_enc->parent), intr_idx, irq->hw_idx, 291 irq->irq_idx, phys_enc->hw_pp->idx - PINGPONG_0, 292 atomic_read(wait_info->atomic_cnt)); 293 294 ret = dpu_encoder_helper_wait_event_timeout( 295 DRMID(phys_enc->parent), 296 irq->hw_idx, 297 wait_info); 298 299 if (ret <= 0) { 300 irq_status = dpu_core_irq_read(phys_enc->dpu_kms, 301 irq->irq_idx, true); 302 if (irq_status) { 303 unsigned long flags; 304 305 DRM_DEBUG_KMS("irq not triggered id=%u, intr=%d, " 306 "hw=%d, irq=%d, pp=%d, atomic_cnt=%d", 307 DRMID(phys_enc->parent), intr_idx, 308 irq->hw_idx, irq->irq_idx, 309 phys_enc->hw_pp->idx - PINGPONG_0, 310 atomic_read(wait_info->atomic_cnt)); 311 local_irq_save(flags); 312 irq->cb.func(phys_enc, irq->irq_idx); 313 local_irq_restore(flags); 314 ret = 0; 315 } else { 316 ret = -ETIMEDOUT; 317 DRM_DEBUG_KMS("irq timeout id=%u, intr=%d, " 318 "hw=%d, irq=%d, pp=%d, atomic_cnt=%d", 319 DRMID(phys_enc->parent), intr_idx, 320 irq->hw_idx, irq->irq_idx, 321 phys_enc->hw_pp->idx - PINGPONG_0, 322 atomic_read(wait_info->atomic_cnt)); 323 } 324 } else { 325 ret = 0; 326 trace_dpu_enc_irq_wait_success(DRMID(phys_enc->parent), 327 intr_idx, irq->hw_idx, irq->irq_idx, 328 phys_enc->hw_pp->idx - PINGPONG_0, 329 atomic_read(wait_info->atomic_cnt)); 330 } 331 332 return ret; 333 } 334 335 int dpu_encoder_helper_register_irq(struct dpu_encoder_phys *phys_enc, 336 enum dpu_intr_idx intr_idx) 337 { 338 struct dpu_encoder_irq *irq; 339 int ret = 0; 340 341 if (intr_idx >= INTR_IDX_MAX) { 342 DPU_ERROR("invalid params\n"); 343 return -EINVAL; 344 } 345 irq = &phys_enc->irq[intr_idx]; 346 347 if (irq->irq_idx >= 0) { 348 DPU_DEBUG_PHYS(phys_enc, 349 "skipping already registered irq %s type %d\n", 350 irq->name, irq->intr_type); 351 return 0; 352 } 353 354 irq->irq_idx = dpu_core_irq_idx_lookup(phys_enc->dpu_kms, 355 irq->intr_type, irq->hw_idx); 356 if (irq->irq_idx < 0) { 357 DPU_ERROR_PHYS(phys_enc, 358 "failed to lookup IRQ index for %s type:%d\n", 359 irq->name, irq->intr_type); 360 return -EINVAL; 361 } 362 363 ret = dpu_core_irq_register_callback(phys_enc->dpu_kms, irq->irq_idx, 364 &irq->cb); 365 if (ret) { 366 DPU_ERROR_PHYS(phys_enc, 367 "failed to register IRQ callback for %s\n", 368 irq->name); 369 irq->irq_idx = -EINVAL; 370 return ret; 371 } 372 373 ret = dpu_core_irq_enable(phys_enc->dpu_kms, &irq->irq_idx, 1); 374 if (ret) { 375 DRM_ERROR("enable failed id=%u, intr=%d, hw=%d, irq=%d", 376 DRMID(phys_enc->parent), intr_idx, irq->hw_idx, 377 irq->irq_idx); 378 dpu_core_irq_unregister_callback(phys_enc->dpu_kms, 379 irq->irq_idx, &irq->cb); 380 irq->irq_idx = -EINVAL; 381 return ret; 382 } 383 384 trace_dpu_enc_irq_register_success(DRMID(phys_enc->parent), intr_idx, 385 irq->hw_idx, irq->irq_idx); 386 387 return ret; 388 } 389 390 int dpu_encoder_helper_unregister_irq(struct dpu_encoder_phys *phys_enc, 391 enum dpu_intr_idx intr_idx) 392 { 393 struct dpu_encoder_irq *irq; 394 int ret; 395 396 irq = &phys_enc->irq[intr_idx]; 397 398 /* silently skip irqs that weren't registered */ 399 if (irq->irq_idx < 0) { 400 DRM_ERROR("duplicate unregister id=%u, intr=%d, hw=%d, irq=%d", 401 DRMID(phys_enc->parent), intr_idx, irq->hw_idx, 402 irq->irq_idx); 403 return 0; 404 } 405 406 ret = dpu_core_irq_disable(phys_enc->dpu_kms, &irq->irq_idx, 1); 407 if (ret) { 408 DRM_ERROR("disable failed id=%u, intr=%d, hw=%d, irq=%d ret=%d", 409 DRMID(phys_enc->parent), intr_idx, irq->hw_idx, 410 irq->irq_idx, ret); 411 } 412 413 ret = dpu_core_irq_unregister_callback(phys_enc->dpu_kms, irq->irq_idx, 414 &irq->cb); 415 if (ret) { 416 DRM_ERROR("unreg cb fail id=%u, intr=%d, hw=%d, irq=%d ret=%d", 417 DRMID(phys_enc->parent), intr_idx, irq->hw_idx, 418 irq->irq_idx, ret); 419 } 420 421 trace_dpu_enc_irq_unregister_success(DRMID(phys_enc->parent), intr_idx, 422 irq->hw_idx, irq->irq_idx); 423 424 irq->irq_idx = -EINVAL; 425 426 return 0; 427 } 428 429 int dpu_encoder_get_frame_count(struct drm_encoder *drm_enc) 430 { 431 struct dpu_encoder_virt *dpu_enc; 432 struct dpu_encoder_phys *phys; 433 int framecount = 0; 434 435 dpu_enc = to_dpu_encoder_virt(drm_enc); 436 phys = dpu_enc ? dpu_enc->cur_master : NULL; 437 438 if (phys && phys->ops.get_frame_count) 439 framecount = phys->ops.get_frame_count(phys); 440 441 return framecount; 442 } 443 444 int dpu_encoder_get_linecount(struct drm_encoder *drm_enc) 445 { 446 struct dpu_encoder_virt *dpu_enc; 447 struct dpu_encoder_phys *phys; 448 int linecount = 0; 449 450 dpu_enc = to_dpu_encoder_virt(drm_enc); 451 phys = dpu_enc ? dpu_enc->cur_master : NULL; 452 453 if (phys && phys->ops.get_line_count) 454 linecount = phys->ops.get_line_count(phys); 455 456 return linecount; 457 } 458 459 void dpu_encoder_get_hw_resources(struct drm_encoder *drm_enc, 460 struct dpu_encoder_hw_resources *hw_res) 461 { 462 struct dpu_encoder_virt *dpu_enc = NULL; 463 int i = 0; 464 465 dpu_enc = to_dpu_encoder_virt(drm_enc); 466 DPU_DEBUG_ENC(dpu_enc, "\n"); 467 468 /* Query resources used by phys encs, expected to be without overlap */ 469 memset(hw_res, 0, sizeof(*hw_res)); 470 471 for (i = 0; i < dpu_enc->num_phys_encs; i++) { 472 struct dpu_encoder_phys *phys = dpu_enc->phys_encs[i]; 473 474 if (phys->ops.get_hw_resources) 475 phys->ops.get_hw_resources(phys, hw_res); 476 } 477 } 478 479 static void dpu_encoder_destroy(struct drm_encoder *drm_enc) 480 { 481 struct dpu_encoder_virt *dpu_enc = NULL; 482 int i = 0; 483 484 if (!drm_enc) { 485 DPU_ERROR("invalid encoder\n"); 486 return; 487 } 488 489 dpu_enc = to_dpu_encoder_virt(drm_enc); 490 DPU_DEBUG_ENC(dpu_enc, "\n"); 491 492 mutex_lock(&dpu_enc->enc_lock); 493 494 for (i = 0; i < dpu_enc->num_phys_encs; i++) { 495 struct dpu_encoder_phys *phys = dpu_enc->phys_encs[i]; 496 497 if (phys->ops.destroy) { 498 phys->ops.destroy(phys); 499 --dpu_enc->num_phys_encs; 500 dpu_enc->phys_encs[i] = NULL; 501 } 502 } 503 504 if (dpu_enc->num_phys_encs) 505 DPU_ERROR_ENC(dpu_enc, "expected 0 num_phys_encs not %d\n", 506 dpu_enc->num_phys_encs); 507 dpu_enc->num_phys_encs = 0; 508 mutex_unlock(&dpu_enc->enc_lock); 509 510 drm_encoder_cleanup(drm_enc); 511 mutex_destroy(&dpu_enc->enc_lock); 512 } 513 514 void dpu_encoder_helper_split_config( 515 struct dpu_encoder_phys *phys_enc, 516 enum dpu_intf interface) 517 { 518 struct dpu_encoder_virt *dpu_enc; 519 struct split_pipe_cfg cfg = { 0 }; 520 struct dpu_hw_mdp *hw_mdptop; 521 struct msm_display_info *disp_info; 522 523 if (!phys_enc->hw_mdptop || !phys_enc->parent) { 524 DPU_ERROR("invalid arg(s), encoder %d\n", phys_enc != NULL); 525 return; 526 } 527 528 dpu_enc = to_dpu_encoder_virt(phys_enc->parent); 529 hw_mdptop = phys_enc->hw_mdptop; 530 disp_info = &dpu_enc->disp_info; 531 532 if (disp_info->intf_type != DRM_MODE_ENCODER_DSI) 533 return; 534 535 /** 536 * disable split modes since encoder will be operating in as the only 537 * encoder, either for the entire use case in the case of, for example, 538 * single DSI, or for this frame in the case of left/right only partial 539 * update. 540 */ 541 if (phys_enc->split_role == ENC_ROLE_SOLO) { 542 if (hw_mdptop->ops.setup_split_pipe) 543 hw_mdptop->ops.setup_split_pipe(hw_mdptop, &cfg); 544 return; 545 } 546 547 cfg.en = true; 548 cfg.mode = phys_enc->intf_mode; 549 cfg.intf = interface; 550 551 if (cfg.en && phys_enc->ops.needs_single_flush && 552 phys_enc->ops.needs_single_flush(phys_enc)) 553 cfg.split_flush_en = true; 554 555 if (phys_enc->split_role == ENC_ROLE_MASTER) { 556 DPU_DEBUG_ENC(dpu_enc, "enable %d\n", cfg.en); 557 558 if (hw_mdptop->ops.setup_split_pipe) 559 hw_mdptop->ops.setup_split_pipe(hw_mdptop, &cfg); 560 } 561 } 562 563 static struct msm_display_topology dpu_encoder_get_topology( 564 struct dpu_encoder_virt *dpu_enc, 565 struct dpu_kms *dpu_kms, 566 struct drm_display_mode *mode) 567 { 568 struct msm_display_topology topology = {0}; 569 int i, intf_count = 0; 570 571 for (i = 0; i < MAX_PHYS_ENCODERS_PER_VIRTUAL; i++) 572 if (dpu_enc->phys_encs[i]) 573 intf_count++; 574 575 /* Datapath topology selection 576 * 577 * Dual display 578 * 2 LM, 2 INTF ( Split display using 2 interfaces) 579 * 580 * Single display 581 * 1 LM, 1 INTF 582 * 2 LM, 1 INTF (stream merge to support high resolution interfaces) 583 * 584 * Adding color blocks only to primary interface if available in 585 * sufficient number 586 */ 587 if (intf_count == 2) 588 topology.num_lm = 2; 589 else if (!dpu_kms->catalog->caps->has_3d_merge) 590 topology.num_lm = 1; 591 else 592 topology.num_lm = (mode->hdisplay > MAX_HDISPLAY_SPLIT) ? 2 : 1; 593 594 if (dpu_enc->disp_info.intf_type == DRM_MODE_ENCODER_DSI) { 595 if (dpu_kms->catalog->dspp && 596 (dpu_kms->catalog->dspp_count >= topology.num_lm)) 597 topology.num_dspp = topology.num_lm; 598 } 599 600 topology.num_enc = 0; 601 topology.num_intf = intf_count; 602 603 return topology; 604 } 605 static int dpu_encoder_virt_atomic_check( 606 struct drm_encoder *drm_enc, 607 struct drm_crtc_state *crtc_state, 608 struct drm_connector_state *conn_state) 609 { 610 struct dpu_encoder_virt *dpu_enc; 611 struct msm_drm_private *priv; 612 struct dpu_kms *dpu_kms; 613 const struct drm_display_mode *mode; 614 struct drm_display_mode *adj_mode; 615 struct msm_display_topology topology; 616 struct dpu_global_state *global_state; 617 int i = 0; 618 int ret = 0; 619 620 if (!drm_enc || !crtc_state || !conn_state) { 621 DPU_ERROR("invalid arg(s), drm_enc %d, crtc/conn state %d/%d\n", 622 drm_enc != NULL, crtc_state != NULL, conn_state != NULL); 623 return -EINVAL; 624 } 625 626 dpu_enc = to_dpu_encoder_virt(drm_enc); 627 DPU_DEBUG_ENC(dpu_enc, "\n"); 628 629 priv = drm_enc->dev->dev_private; 630 dpu_kms = to_dpu_kms(priv->kms); 631 mode = &crtc_state->mode; 632 adj_mode = &crtc_state->adjusted_mode; 633 global_state = dpu_kms_get_global_state(crtc_state->state); 634 if (IS_ERR(global_state)) 635 return PTR_ERR(global_state); 636 637 trace_dpu_enc_atomic_check(DRMID(drm_enc)); 638 639 /* perform atomic check on the first physical encoder (master) */ 640 for (i = 0; i < dpu_enc->num_phys_encs; i++) { 641 struct dpu_encoder_phys *phys = dpu_enc->phys_encs[i]; 642 643 if (phys->ops.atomic_check) 644 ret = phys->ops.atomic_check(phys, crtc_state, 645 conn_state); 646 else if (phys->ops.mode_fixup) 647 if (!phys->ops.mode_fixup(phys, mode, adj_mode)) 648 ret = -EINVAL; 649 650 if (ret) { 651 DPU_ERROR_ENC(dpu_enc, 652 "mode unsupported, phys idx %d\n", i); 653 break; 654 } 655 } 656 657 topology = dpu_encoder_get_topology(dpu_enc, dpu_kms, adj_mode); 658 659 /* Reserve dynamic resources now. */ 660 if (!ret) { 661 /* 662 * Release and Allocate resources on every modeset 663 * Dont allocate when active is false. 664 */ 665 if (drm_atomic_crtc_needs_modeset(crtc_state)) { 666 dpu_rm_release(global_state, drm_enc); 667 668 if (!crtc_state->active_changed || crtc_state->active) 669 ret = dpu_rm_reserve(&dpu_kms->rm, global_state, 670 drm_enc, crtc_state, topology); 671 } 672 } 673 674 trace_dpu_enc_atomic_check_flags(DRMID(drm_enc), adj_mode->flags); 675 676 return ret; 677 } 678 679 static void _dpu_encoder_update_vsync_source(struct dpu_encoder_virt *dpu_enc, 680 struct msm_display_info *disp_info) 681 { 682 struct dpu_vsync_source_cfg vsync_cfg = { 0 }; 683 struct msm_drm_private *priv; 684 struct dpu_kms *dpu_kms; 685 struct dpu_hw_mdp *hw_mdptop; 686 struct drm_encoder *drm_enc; 687 int i; 688 689 if (!dpu_enc || !disp_info) { 690 DPU_ERROR("invalid param dpu_enc:%d or disp_info:%d\n", 691 dpu_enc != NULL, disp_info != NULL); 692 return; 693 } else if (dpu_enc->num_phys_encs > ARRAY_SIZE(dpu_enc->hw_pp)) { 694 DPU_ERROR("invalid num phys enc %d/%d\n", 695 dpu_enc->num_phys_encs, 696 (int) ARRAY_SIZE(dpu_enc->hw_pp)); 697 return; 698 } 699 700 drm_enc = &dpu_enc->base; 701 /* this pointers are checked in virt_enable_helper */ 702 priv = drm_enc->dev->dev_private; 703 704 dpu_kms = to_dpu_kms(priv->kms); 705 hw_mdptop = dpu_kms->hw_mdp; 706 if (!hw_mdptop) { 707 DPU_ERROR("invalid mdptop\n"); 708 return; 709 } 710 711 if (hw_mdptop->ops.setup_vsync_source && 712 disp_info->capabilities & MSM_DISPLAY_CAP_CMD_MODE) { 713 for (i = 0; i < dpu_enc->num_phys_encs; i++) 714 vsync_cfg.ppnumber[i] = dpu_enc->hw_pp[i]->idx; 715 716 vsync_cfg.pp_count = dpu_enc->num_phys_encs; 717 if (disp_info->is_te_using_watchdog_timer) 718 vsync_cfg.vsync_source = DPU_VSYNC_SOURCE_WD_TIMER_0; 719 else 720 vsync_cfg.vsync_source = DPU_VSYNC0_SOURCE_GPIO; 721 722 hw_mdptop->ops.setup_vsync_source(hw_mdptop, &vsync_cfg); 723 } 724 } 725 726 static void _dpu_encoder_irq_control(struct drm_encoder *drm_enc, bool enable) 727 { 728 struct dpu_encoder_virt *dpu_enc; 729 int i; 730 731 if (!drm_enc) { 732 DPU_ERROR("invalid encoder\n"); 733 return; 734 } 735 736 dpu_enc = to_dpu_encoder_virt(drm_enc); 737 738 DPU_DEBUG_ENC(dpu_enc, "enable:%d\n", enable); 739 for (i = 0; i < dpu_enc->num_phys_encs; i++) { 740 struct dpu_encoder_phys *phys = dpu_enc->phys_encs[i]; 741 742 if (phys->ops.irq_control) 743 phys->ops.irq_control(phys, enable); 744 } 745 746 } 747 748 static void _dpu_encoder_resource_control_helper(struct drm_encoder *drm_enc, 749 bool enable) 750 { 751 struct msm_drm_private *priv; 752 struct dpu_kms *dpu_kms; 753 struct dpu_encoder_virt *dpu_enc; 754 755 dpu_enc = to_dpu_encoder_virt(drm_enc); 756 priv = drm_enc->dev->dev_private; 757 dpu_kms = to_dpu_kms(priv->kms); 758 759 trace_dpu_enc_rc_helper(DRMID(drm_enc), enable); 760 761 if (!dpu_enc->cur_master) { 762 DPU_ERROR("encoder master not set\n"); 763 return; 764 } 765 766 if (enable) { 767 /* enable DPU core clks */ 768 pm_runtime_get_sync(&dpu_kms->pdev->dev); 769 770 /* enable all the irq */ 771 _dpu_encoder_irq_control(drm_enc, true); 772 773 } else { 774 /* disable all the irq */ 775 _dpu_encoder_irq_control(drm_enc, false); 776 777 /* disable DPU core clks */ 778 pm_runtime_put_sync(&dpu_kms->pdev->dev); 779 } 780 781 } 782 783 static int dpu_encoder_resource_control(struct drm_encoder *drm_enc, 784 u32 sw_event) 785 { 786 struct dpu_encoder_virt *dpu_enc; 787 struct msm_drm_private *priv; 788 bool is_vid_mode = false; 789 790 if (!drm_enc || !drm_enc->dev || !drm_enc->crtc) { 791 DPU_ERROR("invalid parameters\n"); 792 return -EINVAL; 793 } 794 dpu_enc = to_dpu_encoder_virt(drm_enc); 795 priv = drm_enc->dev->dev_private; 796 is_vid_mode = dpu_enc->disp_info.capabilities & 797 MSM_DISPLAY_CAP_VID_MODE; 798 799 /* 800 * when idle_pc is not supported, process only KICKOFF, STOP and MODESET 801 * events and return early for other events (ie wb display). 802 */ 803 if (!dpu_enc->idle_pc_supported && 804 (sw_event != DPU_ENC_RC_EVENT_KICKOFF && 805 sw_event != DPU_ENC_RC_EVENT_STOP && 806 sw_event != DPU_ENC_RC_EVENT_PRE_STOP)) 807 return 0; 808 809 trace_dpu_enc_rc(DRMID(drm_enc), sw_event, dpu_enc->idle_pc_supported, 810 dpu_enc->rc_state, "begin"); 811 812 switch (sw_event) { 813 case DPU_ENC_RC_EVENT_KICKOFF: 814 /* cancel delayed off work, if any */ 815 if (cancel_delayed_work_sync(&dpu_enc->delayed_off_work)) 816 DPU_DEBUG_ENC(dpu_enc, "sw_event:%d, work cancelled\n", 817 sw_event); 818 819 mutex_lock(&dpu_enc->rc_lock); 820 821 /* return if the resource control is already in ON state */ 822 if (dpu_enc->rc_state == DPU_ENC_RC_STATE_ON) { 823 DRM_DEBUG_KMS("id;%u, sw_event:%d, rc in ON state\n", 824 DRMID(drm_enc), sw_event); 825 mutex_unlock(&dpu_enc->rc_lock); 826 return 0; 827 } else if (dpu_enc->rc_state != DPU_ENC_RC_STATE_OFF && 828 dpu_enc->rc_state != DPU_ENC_RC_STATE_IDLE) { 829 DRM_DEBUG_KMS("id;%u, sw_event:%d, rc in state %d\n", 830 DRMID(drm_enc), sw_event, 831 dpu_enc->rc_state); 832 mutex_unlock(&dpu_enc->rc_lock); 833 return -EINVAL; 834 } 835 836 if (is_vid_mode && dpu_enc->rc_state == DPU_ENC_RC_STATE_IDLE) 837 _dpu_encoder_irq_control(drm_enc, true); 838 else 839 _dpu_encoder_resource_control_helper(drm_enc, true); 840 841 dpu_enc->rc_state = DPU_ENC_RC_STATE_ON; 842 843 trace_dpu_enc_rc(DRMID(drm_enc), sw_event, 844 dpu_enc->idle_pc_supported, dpu_enc->rc_state, 845 "kickoff"); 846 847 mutex_unlock(&dpu_enc->rc_lock); 848 break; 849 850 case DPU_ENC_RC_EVENT_FRAME_DONE: 851 /* 852 * mutex lock is not used as this event happens at interrupt 853 * context. And locking is not required as, the other events 854 * like KICKOFF and STOP does a wait-for-idle before executing 855 * the resource_control 856 */ 857 if (dpu_enc->rc_state != DPU_ENC_RC_STATE_ON) { 858 DRM_DEBUG_KMS("id:%d, sw_event:%d,rc:%d-unexpected\n", 859 DRMID(drm_enc), sw_event, 860 dpu_enc->rc_state); 861 return -EINVAL; 862 } 863 864 /* 865 * schedule off work item only when there are no 866 * frames pending 867 */ 868 if (dpu_crtc_frame_pending(drm_enc->crtc) > 1) { 869 DRM_DEBUG_KMS("id:%d skip schedule work\n", 870 DRMID(drm_enc)); 871 return 0; 872 } 873 874 queue_delayed_work(priv->wq, &dpu_enc->delayed_off_work, 875 msecs_to_jiffies(dpu_enc->idle_timeout)); 876 877 trace_dpu_enc_rc(DRMID(drm_enc), sw_event, 878 dpu_enc->idle_pc_supported, dpu_enc->rc_state, 879 "frame done"); 880 break; 881 882 case DPU_ENC_RC_EVENT_PRE_STOP: 883 /* cancel delayed off work, if any */ 884 if (cancel_delayed_work_sync(&dpu_enc->delayed_off_work)) 885 DPU_DEBUG_ENC(dpu_enc, "sw_event:%d, work cancelled\n", 886 sw_event); 887 888 mutex_lock(&dpu_enc->rc_lock); 889 890 if (is_vid_mode && 891 dpu_enc->rc_state == DPU_ENC_RC_STATE_IDLE) { 892 _dpu_encoder_irq_control(drm_enc, true); 893 } 894 /* skip if is already OFF or IDLE, resources are off already */ 895 else if (dpu_enc->rc_state == DPU_ENC_RC_STATE_OFF || 896 dpu_enc->rc_state == DPU_ENC_RC_STATE_IDLE) { 897 DRM_DEBUG_KMS("id:%u, sw_event:%d, rc in %d state\n", 898 DRMID(drm_enc), sw_event, 899 dpu_enc->rc_state); 900 mutex_unlock(&dpu_enc->rc_lock); 901 return 0; 902 } 903 904 dpu_enc->rc_state = DPU_ENC_RC_STATE_PRE_OFF; 905 906 trace_dpu_enc_rc(DRMID(drm_enc), sw_event, 907 dpu_enc->idle_pc_supported, dpu_enc->rc_state, 908 "pre stop"); 909 910 mutex_unlock(&dpu_enc->rc_lock); 911 break; 912 913 case DPU_ENC_RC_EVENT_STOP: 914 mutex_lock(&dpu_enc->rc_lock); 915 916 /* return if the resource control is already in OFF state */ 917 if (dpu_enc->rc_state == DPU_ENC_RC_STATE_OFF) { 918 DRM_DEBUG_KMS("id: %u, sw_event:%d, rc in OFF state\n", 919 DRMID(drm_enc), sw_event); 920 mutex_unlock(&dpu_enc->rc_lock); 921 return 0; 922 } else if (dpu_enc->rc_state == DPU_ENC_RC_STATE_ON) { 923 DRM_ERROR("id: %u, sw_event:%d, rc in state %d\n", 924 DRMID(drm_enc), sw_event, dpu_enc->rc_state); 925 mutex_unlock(&dpu_enc->rc_lock); 926 return -EINVAL; 927 } 928 929 /** 930 * expect to arrive here only if in either idle state or pre-off 931 * and in IDLE state the resources are already disabled 932 */ 933 if (dpu_enc->rc_state == DPU_ENC_RC_STATE_PRE_OFF) 934 _dpu_encoder_resource_control_helper(drm_enc, false); 935 936 dpu_enc->rc_state = DPU_ENC_RC_STATE_OFF; 937 938 trace_dpu_enc_rc(DRMID(drm_enc), sw_event, 939 dpu_enc->idle_pc_supported, dpu_enc->rc_state, 940 "stop"); 941 942 mutex_unlock(&dpu_enc->rc_lock); 943 break; 944 945 case DPU_ENC_RC_EVENT_ENTER_IDLE: 946 mutex_lock(&dpu_enc->rc_lock); 947 948 if (dpu_enc->rc_state != DPU_ENC_RC_STATE_ON) { 949 DRM_ERROR("id: %u, sw_event:%d, rc:%d !ON state\n", 950 DRMID(drm_enc), sw_event, dpu_enc->rc_state); 951 mutex_unlock(&dpu_enc->rc_lock); 952 return 0; 953 } 954 955 /* 956 * if we are in ON but a frame was just kicked off, 957 * ignore the IDLE event, it's probably a stale timer event 958 */ 959 if (dpu_enc->frame_busy_mask[0]) { 960 DRM_ERROR("id:%u, sw_event:%d, rc:%d frame pending\n", 961 DRMID(drm_enc), sw_event, dpu_enc->rc_state); 962 mutex_unlock(&dpu_enc->rc_lock); 963 return 0; 964 } 965 966 if (is_vid_mode) 967 _dpu_encoder_irq_control(drm_enc, false); 968 else 969 _dpu_encoder_resource_control_helper(drm_enc, false); 970 971 dpu_enc->rc_state = DPU_ENC_RC_STATE_IDLE; 972 973 trace_dpu_enc_rc(DRMID(drm_enc), sw_event, 974 dpu_enc->idle_pc_supported, dpu_enc->rc_state, 975 "idle"); 976 977 mutex_unlock(&dpu_enc->rc_lock); 978 break; 979 980 default: 981 DRM_ERROR("id:%u, unexpected sw_event: %d\n", DRMID(drm_enc), 982 sw_event); 983 trace_dpu_enc_rc(DRMID(drm_enc), sw_event, 984 dpu_enc->idle_pc_supported, dpu_enc->rc_state, 985 "error"); 986 break; 987 } 988 989 trace_dpu_enc_rc(DRMID(drm_enc), sw_event, 990 dpu_enc->idle_pc_supported, dpu_enc->rc_state, 991 "end"); 992 return 0; 993 } 994 995 static void dpu_encoder_virt_mode_set(struct drm_encoder *drm_enc, 996 struct drm_display_mode *mode, 997 struct drm_display_mode *adj_mode) 998 { 999 struct dpu_encoder_virt *dpu_enc; 1000 struct msm_drm_private *priv; 1001 struct dpu_kms *dpu_kms; 1002 struct list_head *connector_list; 1003 struct drm_connector *conn = NULL, *conn_iter; 1004 struct drm_crtc *drm_crtc; 1005 struct dpu_crtc_state *cstate; 1006 struct dpu_global_state *global_state; 1007 struct dpu_hw_blk *hw_pp[MAX_CHANNELS_PER_ENC]; 1008 struct dpu_hw_blk *hw_ctl[MAX_CHANNELS_PER_ENC]; 1009 struct dpu_hw_blk *hw_lm[MAX_CHANNELS_PER_ENC]; 1010 struct dpu_hw_blk *hw_dspp[MAX_CHANNELS_PER_ENC] = { NULL }; 1011 int num_lm, num_ctl, num_pp; 1012 int i, j; 1013 1014 if (!drm_enc) { 1015 DPU_ERROR("invalid encoder\n"); 1016 return; 1017 } 1018 1019 dpu_enc = to_dpu_encoder_virt(drm_enc); 1020 DPU_DEBUG_ENC(dpu_enc, "\n"); 1021 1022 priv = drm_enc->dev->dev_private; 1023 dpu_kms = to_dpu_kms(priv->kms); 1024 connector_list = &dpu_kms->dev->mode_config.connector_list; 1025 1026 global_state = dpu_kms_get_existing_global_state(dpu_kms); 1027 if (IS_ERR_OR_NULL(global_state)) { 1028 DPU_ERROR("Failed to get global state"); 1029 return; 1030 } 1031 1032 trace_dpu_enc_mode_set(DRMID(drm_enc)); 1033 1034 if (drm_enc->encoder_type == DRM_MODE_ENCODER_TMDS && priv->dp) 1035 msm_dp_display_mode_set(priv->dp, drm_enc, mode, adj_mode); 1036 1037 list_for_each_entry(conn_iter, connector_list, head) 1038 if (conn_iter->encoder == drm_enc) 1039 conn = conn_iter; 1040 1041 if (!conn) { 1042 DPU_ERROR_ENC(dpu_enc, "failed to find attached connector\n"); 1043 return; 1044 } else if (!conn->state) { 1045 DPU_ERROR_ENC(dpu_enc, "invalid connector state\n"); 1046 return; 1047 } 1048 1049 drm_for_each_crtc(drm_crtc, drm_enc->dev) 1050 if (drm_crtc->state->encoder_mask & drm_encoder_mask(drm_enc)) 1051 break; 1052 1053 /* Query resource that have been reserved in atomic check step. */ 1054 num_pp = dpu_rm_get_assigned_resources(&dpu_kms->rm, global_state, 1055 drm_enc->base.id, DPU_HW_BLK_PINGPONG, hw_pp, 1056 ARRAY_SIZE(hw_pp)); 1057 num_ctl = dpu_rm_get_assigned_resources(&dpu_kms->rm, global_state, 1058 drm_enc->base.id, DPU_HW_BLK_CTL, hw_ctl, ARRAY_SIZE(hw_ctl)); 1059 num_lm = dpu_rm_get_assigned_resources(&dpu_kms->rm, global_state, 1060 drm_enc->base.id, DPU_HW_BLK_LM, hw_lm, ARRAY_SIZE(hw_lm)); 1061 dpu_rm_get_assigned_resources(&dpu_kms->rm, global_state, 1062 drm_enc->base.id, DPU_HW_BLK_DSPP, hw_dspp, 1063 ARRAY_SIZE(hw_dspp)); 1064 1065 for (i = 0; i < MAX_CHANNELS_PER_ENC; i++) 1066 dpu_enc->hw_pp[i] = i < num_pp ? to_dpu_hw_pingpong(hw_pp[i]) 1067 : NULL; 1068 1069 cstate = to_dpu_crtc_state(drm_crtc->state); 1070 1071 for (i = 0; i < num_lm; i++) { 1072 int ctl_idx = (i < num_ctl) ? i : (num_ctl-1); 1073 1074 cstate->mixers[i].hw_lm = to_dpu_hw_mixer(hw_lm[i]); 1075 cstate->mixers[i].lm_ctl = to_dpu_hw_ctl(hw_ctl[ctl_idx]); 1076 cstate->mixers[i].hw_dspp = to_dpu_hw_dspp(hw_dspp[i]); 1077 } 1078 1079 cstate->num_mixers = num_lm; 1080 1081 for (i = 0; i < dpu_enc->num_phys_encs; i++) { 1082 int num_blk; 1083 struct dpu_hw_blk *hw_blk[MAX_CHANNELS_PER_ENC]; 1084 struct dpu_encoder_phys *phys = dpu_enc->phys_encs[i]; 1085 1086 if (!dpu_enc->hw_pp[i]) { 1087 DPU_ERROR_ENC(dpu_enc, 1088 "no pp block assigned at idx: %d\n", i); 1089 return; 1090 } 1091 1092 if (!hw_ctl[i]) { 1093 DPU_ERROR_ENC(dpu_enc, 1094 "no ctl block assigned at idx: %d\n", i); 1095 return; 1096 } 1097 1098 phys->hw_pp = dpu_enc->hw_pp[i]; 1099 phys->hw_ctl = to_dpu_hw_ctl(hw_ctl[i]); 1100 1101 num_blk = dpu_rm_get_assigned_resources(&dpu_kms->rm, 1102 global_state, drm_enc->base.id, DPU_HW_BLK_INTF, 1103 hw_blk, ARRAY_SIZE(hw_blk)); 1104 for (j = 0; j < num_blk; j++) { 1105 struct dpu_hw_intf *hw_intf; 1106 1107 hw_intf = to_dpu_hw_intf(hw_blk[i]); 1108 if (hw_intf->idx == phys->intf_idx) 1109 phys->hw_intf = hw_intf; 1110 } 1111 1112 if (!phys->hw_intf) { 1113 DPU_ERROR_ENC(dpu_enc, 1114 "no intf block assigned at idx: %d\n", i); 1115 return; 1116 } 1117 1118 phys->connector = conn->state->connector; 1119 if (phys->ops.mode_set) 1120 phys->ops.mode_set(phys, mode, adj_mode); 1121 } 1122 } 1123 1124 static void _dpu_encoder_virt_enable_helper(struct drm_encoder *drm_enc) 1125 { 1126 struct dpu_encoder_virt *dpu_enc = NULL; 1127 int i; 1128 1129 if (!drm_enc || !drm_enc->dev) { 1130 DPU_ERROR("invalid parameters\n"); 1131 return; 1132 } 1133 1134 dpu_enc = to_dpu_encoder_virt(drm_enc); 1135 if (!dpu_enc || !dpu_enc->cur_master) { 1136 DPU_ERROR("invalid dpu encoder/master\n"); 1137 return; 1138 } 1139 1140 1141 if (dpu_enc->disp_info.intf_type == DRM_MODE_CONNECTOR_DisplayPort && 1142 dpu_enc->cur_master->hw_mdptop && 1143 dpu_enc->cur_master->hw_mdptop->ops.intf_audio_select) 1144 dpu_enc->cur_master->hw_mdptop->ops.intf_audio_select( 1145 dpu_enc->cur_master->hw_mdptop); 1146 1147 _dpu_encoder_update_vsync_source(dpu_enc, &dpu_enc->disp_info); 1148 1149 if (dpu_enc->disp_info.intf_type == DRM_MODE_ENCODER_DSI && 1150 !WARN_ON(dpu_enc->num_phys_encs == 0)) { 1151 unsigned bpc = dpu_enc->phys_encs[0]->connector->display_info.bpc; 1152 for (i = 0; i < MAX_CHANNELS_PER_ENC; i++) { 1153 if (!dpu_enc->hw_pp[i]) 1154 continue; 1155 _dpu_encoder_setup_dither(dpu_enc->hw_pp[i], bpc); 1156 } 1157 } 1158 } 1159 1160 void dpu_encoder_virt_runtime_resume(struct drm_encoder *drm_enc) 1161 { 1162 struct dpu_encoder_virt *dpu_enc = to_dpu_encoder_virt(drm_enc); 1163 1164 mutex_lock(&dpu_enc->enc_lock); 1165 1166 if (!dpu_enc->enabled) 1167 goto out; 1168 1169 if (dpu_enc->cur_slave && dpu_enc->cur_slave->ops.restore) 1170 dpu_enc->cur_slave->ops.restore(dpu_enc->cur_slave); 1171 if (dpu_enc->cur_master && dpu_enc->cur_master->ops.restore) 1172 dpu_enc->cur_master->ops.restore(dpu_enc->cur_master); 1173 1174 _dpu_encoder_virt_enable_helper(drm_enc); 1175 1176 out: 1177 mutex_unlock(&dpu_enc->enc_lock); 1178 } 1179 1180 static void dpu_encoder_virt_enable(struct drm_encoder *drm_enc) 1181 { 1182 struct dpu_encoder_virt *dpu_enc = NULL; 1183 int ret = 0; 1184 struct msm_drm_private *priv; 1185 struct drm_display_mode *cur_mode = NULL; 1186 1187 if (!drm_enc) { 1188 DPU_ERROR("invalid encoder\n"); 1189 return; 1190 } 1191 dpu_enc = to_dpu_encoder_virt(drm_enc); 1192 1193 mutex_lock(&dpu_enc->enc_lock); 1194 cur_mode = &dpu_enc->base.crtc->state->adjusted_mode; 1195 priv = drm_enc->dev->dev_private; 1196 1197 trace_dpu_enc_enable(DRMID(drm_enc), cur_mode->hdisplay, 1198 cur_mode->vdisplay); 1199 1200 /* always enable slave encoder before master */ 1201 if (dpu_enc->cur_slave && dpu_enc->cur_slave->ops.enable) 1202 dpu_enc->cur_slave->ops.enable(dpu_enc->cur_slave); 1203 1204 if (dpu_enc->cur_master && dpu_enc->cur_master->ops.enable) 1205 dpu_enc->cur_master->ops.enable(dpu_enc->cur_master); 1206 1207 ret = dpu_encoder_resource_control(drm_enc, DPU_ENC_RC_EVENT_KICKOFF); 1208 if (ret) { 1209 DPU_ERROR_ENC(dpu_enc, "dpu resource control failed: %d\n", 1210 ret); 1211 goto out; 1212 } 1213 1214 _dpu_encoder_virt_enable_helper(drm_enc); 1215 1216 if (drm_enc->encoder_type == DRM_MODE_ENCODER_TMDS && priv->dp) { 1217 ret = msm_dp_display_enable(priv->dp, 1218 drm_enc); 1219 if (ret) { 1220 DPU_ERROR_ENC(dpu_enc, "dp display enable failed: %d\n", 1221 ret); 1222 goto out; 1223 } 1224 } 1225 dpu_enc->enabled = true; 1226 1227 out: 1228 mutex_unlock(&dpu_enc->enc_lock); 1229 } 1230 1231 static void dpu_encoder_virt_disable(struct drm_encoder *drm_enc) 1232 { 1233 struct dpu_encoder_virt *dpu_enc = NULL; 1234 struct msm_drm_private *priv; 1235 int i = 0; 1236 1237 if (!drm_enc) { 1238 DPU_ERROR("invalid encoder\n"); 1239 return; 1240 } else if (!drm_enc->dev) { 1241 DPU_ERROR("invalid dev\n"); 1242 return; 1243 } 1244 1245 dpu_enc = to_dpu_encoder_virt(drm_enc); 1246 DPU_DEBUG_ENC(dpu_enc, "\n"); 1247 1248 mutex_lock(&dpu_enc->enc_lock); 1249 dpu_enc->enabled = false; 1250 1251 priv = drm_enc->dev->dev_private; 1252 1253 trace_dpu_enc_disable(DRMID(drm_enc)); 1254 1255 /* wait for idle */ 1256 dpu_encoder_wait_for_event(drm_enc, MSM_ENC_TX_COMPLETE); 1257 1258 if (drm_enc->encoder_type == DRM_MODE_ENCODER_TMDS && priv->dp) { 1259 if (msm_dp_display_pre_disable(priv->dp, drm_enc)) 1260 DPU_ERROR_ENC(dpu_enc, "dp display push idle failed\n"); 1261 } 1262 1263 dpu_encoder_resource_control(drm_enc, DPU_ENC_RC_EVENT_PRE_STOP); 1264 1265 for (i = 0; i < dpu_enc->num_phys_encs; i++) { 1266 struct dpu_encoder_phys *phys = dpu_enc->phys_encs[i]; 1267 1268 if (phys->ops.disable) 1269 phys->ops.disable(phys); 1270 } 1271 1272 1273 /* after phys waits for frame-done, should be no more frames pending */ 1274 if (atomic_xchg(&dpu_enc->frame_done_timeout_ms, 0)) { 1275 DPU_ERROR("enc%d timeout pending\n", drm_enc->base.id); 1276 del_timer_sync(&dpu_enc->frame_done_timer); 1277 } 1278 1279 dpu_encoder_resource_control(drm_enc, DPU_ENC_RC_EVENT_STOP); 1280 1281 for (i = 0; i < dpu_enc->num_phys_encs; i++) { 1282 dpu_enc->phys_encs[i]->connector = NULL; 1283 } 1284 1285 DPU_DEBUG_ENC(dpu_enc, "encoder disabled\n"); 1286 1287 if (drm_enc->encoder_type == DRM_MODE_ENCODER_TMDS && priv->dp) { 1288 if (msm_dp_display_disable(priv->dp, drm_enc)) 1289 DPU_ERROR_ENC(dpu_enc, "dp display disable failed\n"); 1290 } 1291 1292 mutex_unlock(&dpu_enc->enc_lock); 1293 } 1294 1295 static enum dpu_intf dpu_encoder_get_intf(struct dpu_mdss_cfg *catalog, 1296 enum dpu_intf_type type, u32 controller_id) 1297 { 1298 int i = 0; 1299 1300 for (i = 0; i < catalog->intf_count; i++) { 1301 if (catalog->intf[i].type == type 1302 && catalog->intf[i].controller_id == controller_id) { 1303 return catalog->intf[i].id; 1304 } 1305 } 1306 1307 return INTF_MAX; 1308 } 1309 1310 static void dpu_encoder_vblank_callback(struct drm_encoder *drm_enc, 1311 struct dpu_encoder_phys *phy_enc) 1312 { 1313 struct dpu_encoder_virt *dpu_enc = NULL; 1314 unsigned long lock_flags; 1315 1316 if (!drm_enc || !phy_enc) 1317 return; 1318 1319 DPU_ATRACE_BEGIN("encoder_vblank_callback"); 1320 dpu_enc = to_dpu_encoder_virt(drm_enc); 1321 1322 spin_lock_irqsave(&dpu_enc->enc_spinlock, lock_flags); 1323 if (dpu_enc->crtc) 1324 dpu_crtc_vblank_callback(dpu_enc->crtc); 1325 spin_unlock_irqrestore(&dpu_enc->enc_spinlock, lock_flags); 1326 1327 atomic_inc(&phy_enc->vsync_cnt); 1328 DPU_ATRACE_END("encoder_vblank_callback"); 1329 } 1330 1331 static void dpu_encoder_underrun_callback(struct drm_encoder *drm_enc, 1332 struct dpu_encoder_phys *phy_enc) 1333 { 1334 if (!phy_enc) 1335 return; 1336 1337 DPU_ATRACE_BEGIN("encoder_underrun_callback"); 1338 atomic_inc(&phy_enc->underrun_cnt); 1339 trace_dpu_enc_underrun_cb(DRMID(drm_enc), 1340 atomic_read(&phy_enc->underrun_cnt)); 1341 DPU_ATRACE_END("encoder_underrun_callback"); 1342 } 1343 1344 void dpu_encoder_assign_crtc(struct drm_encoder *drm_enc, struct drm_crtc *crtc) 1345 { 1346 struct dpu_encoder_virt *dpu_enc = to_dpu_encoder_virt(drm_enc); 1347 unsigned long lock_flags; 1348 1349 spin_lock_irqsave(&dpu_enc->enc_spinlock, lock_flags); 1350 /* crtc should always be cleared before re-assigning */ 1351 WARN_ON(crtc && dpu_enc->crtc); 1352 dpu_enc->crtc = crtc; 1353 spin_unlock_irqrestore(&dpu_enc->enc_spinlock, lock_flags); 1354 } 1355 1356 void dpu_encoder_toggle_vblank_for_crtc(struct drm_encoder *drm_enc, 1357 struct drm_crtc *crtc, bool enable) 1358 { 1359 struct dpu_encoder_virt *dpu_enc = to_dpu_encoder_virt(drm_enc); 1360 unsigned long lock_flags; 1361 int i; 1362 1363 trace_dpu_enc_vblank_cb(DRMID(drm_enc), enable); 1364 1365 spin_lock_irqsave(&dpu_enc->enc_spinlock, lock_flags); 1366 if (dpu_enc->crtc != crtc) { 1367 spin_unlock_irqrestore(&dpu_enc->enc_spinlock, lock_flags); 1368 return; 1369 } 1370 spin_unlock_irqrestore(&dpu_enc->enc_spinlock, lock_flags); 1371 1372 for (i = 0; i < dpu_enc->num_phys_encs; i++) { 1373 struct dpu_encoder_phys *phys = dpu_enc->phys_encs[i]; 1374 1375 if (phys->ops.control_vblank_irq) 1376 phys->ops.control_vblank_irq(phys, enable); 1377 } 1378 } 1379 1380 void dpu_encoder_register_frame_event_callback(struct drm_encoder *drm_enc, 1381 void (*frame_event_cb)(void *, u32 event), 1382 void *frame_event_cb_data) 1383 { 1384 struct dpu_encoder_virt *dpu_enc = to_dpu_encoder_virt(drm_enc); 1385 unsigned long lock_flags; 1386 bool enable; 1387 1388 enable = frame_event_cb ? true : false; 1389 1390 if (!drm_enc) { 1391 DPU_ERROR("invalid encoder\n"); 1392 return; 1393 } 1394 trace_dpu_enc_frame_event_cb(DRMID(drm_enc), enable); 1395 1396 spin_lock_irqsave(&dpu_enc->enc_spinlock, lock_flags); 1397 dpu_enc->crtc_frame_event_cb = frame_event_cb; 1398 dpu_enc->crtc_frame_event_cb_data = frame_event_cb_data; 1399 spin_unlock_irqrestore(&dpu_enc->enc_spinlock, lock_flags); 1400 } 1401 1402 static void dpu_encoder_frame_done_callback( 1403 struct drm_encoder *drm_enc, 1404 struct dpu_encoder_phys *ready_phys, u32 event) 1405 { 1406 struct dpu_encoder_virt *dpu_enc = to_dpu_encoder_virt(drm_enc); 1407 unsigned int i; 1408 1409 if (event & (DPU_ENCODER_FRAME_EVENT_DONE 1410 | DPU_ENCODER_FRAME_EVENT_ERROR 1411 | DPU_ENCODER_FRAME_EVENT_PANEL_DEAD)) { 1412 1413 if (!dpu_enc->frame_busy_mask[0]) { 1414 /** 1415 * suppress frame_done without waiter, 1416 * likely autorefresh 1417 */ 1418 trace_dpu_enc_frame_done_cb_not_busy(DRMID(drm_enc), 1419 event, ready_phys->intf_idx); 1420 return; 1421 } 1422 1423 /* One of the physical encoders has become idle */ 1424 for (i = 0; i < dpu_enc->num_phys_encs; i++) { 1425 if (dpu_enc->phys_encs[i] == ready_phys) { 1426 trace_dpu_enc_frame_done_cb(DRMID(drm_enc), i, 1427 dpu_enc->frame_busy_mask[0]); 1428 clear_bit(i, dpu_enc->frame_busy_mask); 1429 } 1430 } 1431 1432 if (!dpu_enc->frame_busy_mask[0]) { 1433 atomic_set(&dpu_enc->frame_done_timeout_ms, 0); 1434 del_timer(&dpu_enc->frame_done_timer); 1435 1436 dpu_encoder_resource_control(drm_enc, 1437 DPU_ENC_RC_EVENT_FRAME_DONE); 1438 1439 if (dpu_enc->crtc_frame_event_cb) 1440 dpu_enc->crtc_frame_event_cb( 1441 dpu_enc->crtc_frame_event_cb_data, 1442 event); 1443 } 1444 } else { 1445 if (dpu_enc->crtc_frame_event_cb) 1446 dpu_enc->crtc_frame_event_cb( 1447 dpu_enc->crtc_frame_event_cb_data, event); 1448 } 1449 } 1450 1451 static void dpu_encoder_off_work(struct work_struct *work) 1452 { 1453 struct dpu_encoder_virt *dpu_enc = container_of(work, 1454 struct dpu_encoder_virt, delayed_off_work.work); 1455 1456 if (!dpu_enc) { 1457 DPU_ERROR("invalid dpu encoder\n"); 1458 return; 1459 } 1460 1461 dpu_encoder_resource_control(&dpu_enc->base, 1462 DPU_ENC_RC_EVENT_ENTER_IDLE); 1463 1464 dpu_encoder_frame_done_callback(&dpu_enc->base, NULL, 1465 DPU_ENCODER_FRAME_EVENT_IDLE); 1466 } 1467 1468 /** 1469 * _dpu_encoder_trigger_flush - trigger flush for a physical encoder 1470 * @drm_enc: Pointer to drm encoder structure 1471 * @phys: Pointer to physical encoder structure 1472 * @extra_flush_bits: Additional bit mask to include in flush trigger 1473 */ 1474 static void _dpu_encoder_trigger_flush(struct drm_encoder *drm_enc, 1475 struct dpu_encoder_phys *phys, uint32_t extra_flush_bits) 1476 { 1477 struct dpu_hw_ctl *ctl; 1478 int pending_kickoff_cnt; 1479 u32 ret = UINT_MAX; 1480 1481 if (!phys->hw_pp) { 1482 DPU_ERROR("invalid pingpong hw\n"); 1483 return; 1484 } 1485 1486 ctl = phys->hw_ctl; 1487 if (!ctl->ops.trigger_flush) { 1488 DPU_ERROR("missing trigger cb\n"); 1489 return; 1490 } 1491 1492 pending_kickoff_cnt = dpu_encoder_phys_inc_pending(phys); 1493 1494 if (extra_flush_bits && ctl->ops.update_pending_flush) 1495 ctl->ops.update_pending_flush(ctl, extra_flush_bits); 1496 1497 ctl->ops.trigger_flush(ctl); 1498 1499 if (ctl->ops.get_pending_flush) 1500 ret = ctl->ops.get_pending_flush(ctl); 1501 1502 trace_dpu_enc_trigger_flush(DRMID(drm_enc), phys->intf_idx, 1503 pending_kickoff_cnt, ctl->idx, 1504 extra_flush_bits, ret); 1505 } 1506 1507 /** 1508 * _dpu_encoder_trigger_start - trigger start for a physical encoder 1509 * @phys: Pointer to physical encoder structure 1510 */ 1511 static void _dpu_encoder_trigger_start(struct dpu_encoder_phys *phys) 1512 { 1513 if (!phys) { 1514 DPU_ERROR("invalid argument(s)\n"); 1515 return; 1516 } 1517 1518 if (!phys->hw_pp) { 1519 DPU_ERROR("invalid pingpong hw\n"); 1520 return; 1521 } 1522 1523 if (phys->ops.trigger_start && phys->enable_state != DPU_ENC_DISABLED) 1524 phys->ops.trigger_start(phys); 1525 } 1526 1527 void dpu_encoder_helper_trigger_start(struct dpu_encoder_phys *phys_enc) 1528 { 1529 struct dpu_hw_ctl *ctl; 1530 1531 ctl = phys_enc->hw_ctl; 1532 if (ctl->ops.trigger_start) { 1533 ctl->ops.trigger_start(ctl); 1534 trace_dpu_enc_trigger_start(DRMID(phys_enc->parent), ctl->idx); 1535 } 1536 } 1537 1538 static int dpu_encoder_helper_wait_event_timeout( 1539 int32_t drm_id, 1540 int32_t hw_id, 1541 struct dpu_encoder_wait_info *info) 1542 { 1543 int rc = 0; 1544 s64 expected_time = ktime_to_ms(ktime_get()) + info->timeout_ms; 1545 s64 jiffies = msecs_to_jiffies(info->timeout_ms); 1546 s64 time; 1547 1548 do { 1549 rc = wait_event_timeout(*(info->wq), 1550 atomic_read(info->atomic_cnt) == 0, jiffies); 1551 time = ktime_to_ms(ktime_get()); 1552 1553 trace_dpu_enc_wait_event_timeout(drm_id, hw_id, rc, time, 1554 expected_time, 1555 atomic_read(info->atomic_cnt)); 1556 /* If we timed out, counter is valid and time is less, wait again */ 1557 } while (atomic_read(info->atomic_cnt) && (rc == 0) && 1558 (time < expected_time)); 1559 1560 return rc; 1561 } 1562 1563 static void dpu_encoder_helper_hw_reset(struct dpu_encoder_phys *phys_enc) 1564 { 1565 struct dpu_encoder_virt *dpu_enc; 1566 struct dpu_hw_ctl *ctl; 1567 int rc; 1568 1569 dpu_enc = to_dpu_encoder_virt(phys_enc->parent); 1570 ctl = phys_enc->hw_ctl; 1571 1572 if (!ctl->ops.reset) 1573 return; 1574 1575 DRM_DEBUG_KMS("id:%u ctl %d reset\n", DRMID(phys_enc->parent), 1576 ctl->idx); 1577 1578 rc = ctl->ops.reset(ctl); 1579 if (rc) 1580 DPU_ERROR_ENC(dpu_enc, "ctl %d reset failure\n", ctl->idx); 1581 1582 phys_enc->enable_state = DPU_ENC_ENABLED; 1583 } 1584 1585 /** 1586 * _dpu_encoder_kickoff_phys - handle physical encoder kickoff 1587 * Iterate through the physical encoders and perform consolidated flush 1588 * and/or control start triggering as needed. This is done in the virtual 1589 * encoder rather than the individual physical ones in order to handle 1590 * use cases that require visibility into multiple physical encoders at 1591 * a time. 1592 * @dpu_enc: Pointer to virtual encoder structure 1593 */ 1594 static void _dpu_encoder_kickoff_phys(struct dpu_encoder_virt *dpu_enc) 1595 { 1596 struct dpu_hw_ctl *ctl; 1597 uint32_t i, pending_flush; 1598 unsigned long lock_flags; 1599 1600 pending_flush = 0x0; 1601 1602 /* update pending counts and trigger kickoff ctl flush atomically */ 1603 spin_lock_irqsave(&dpu_enc->enc_spinlock, lock_flags); 1604 1605 /* don't perform flush/start operations for slave encoders */ 1606 for (i = 0; i < dpu_enc->num_phys_encs; i++) { 1607 struct dpu_encoder_phys *phys = dpu_enc->phys_encs[i]; 1608 1609 if (phys->enable_state == DPU_ENC_DISABLED) 1610 continue; 1611 1612 ctl = phys->hw_ctl; 1613 1614 /* 1615 * This is cleared in frame_done worker, which isn't invoked 1616 * for async commits. So don't set this for async, since it'll 1617 * roll over to the next commit. 1618 */ 1619 if (phys->split_role != ENC_ROLE_SLAVE) 1620 set_bit(i, dpu_enc->frame_busy_mask); 1621 1622 if (!phys->ops.needs_single_flush || 1623 !phys->ops.needs_single_flush(phys)) 1624 _dpu_encoder_trigger_flush(&dpu_enc->base, phys, 0x0); 1625 else if (ctl->ops.get_pending_flush) 1626 pending_flush |= ctl->ops.get_pending_flush(ctl); 1627 } 1628 1629 /* for split flush, combine pending flush masks and send to master */ 1630 if (pending_flush && dpu_enc->cur_master) { 1631 _dpu_encoder_trigger_flush( 1632 &dpu_enc->base, 1633 dpu_enc->cur_master, 1634 pending_flush); 1635 } 1636 1637 _dpu_encoder_trigger_start(dpu_enc->cur_master); 1638 1639 spin_unlock_irqrestore(&dpu_enc->enc_spinlock, lock_flags); 1640 } 1641 1642 void dpu_encoder_trigger_kickoff_pending(struct drm_encoder *drm_enc) 1643 { 1644 struct dpu_encoder_virt *dpu_enc; 1645 struct dpu_encoder_phys *phys; 1646 unsigned int i; 1647 struct dpu_hw_ctl *ctl; 1648 struct msm_display_info *disp_info; 1649 1650 if (!drm_enc) { 1651 DPU_ERROR("invalid encoder\n"); 1652 return; 1653 } 1654 dpu_enc = to_dpu_encoder_virt(drm_enc); 1655 disp_info = &dpu_enc->disp_info; 1656 1657 for (i = 0; i < dpu_enc->num_phys_encs; i++) { 1658 phys = dpu_enc->phys_encs[i]; 1659 1660 ctl = phys->hw_ctl; 1661 if (ctl->ops.clear_pending_flush) 1662 ctl->ops.clear_pending_flush(ctl); 1663 1664 /* update only for command mode primary ctl */ 1665 if ((phys == dpu_enc->cur_master) && 1666 (disp_info->capabilities & MSM_DISPLAY_CAP_CMD_MODE) 1667 && ctl->ops.trigger_pending) 1668 ctl->ops.trigger_pending(ctl); 1669 } 1670 } 1671 1672 static u32 _dpu_encoder_calculate_linetime(struct dpu_encoder_virt *dpu_enc, 1673 struct drm_display_mode *mode) 1674 { 1675 u64 pclk_rate; 1676 u32 pclk_period; 1677 u32 line_time; 1678 1679 /* 1680 * For linetime calculation, only operate on master encoder. 1681 */ 1682 if (!dpu_enc->cur_master) 1683 return 0; 1684 1685 if (!dpu_enc->cur_master->ops.get_line_count) { 1686 DPU_ERROR("get_line_count function not defined\n"); 1687 return 0; 1688 } 1689 1690 pclk_rate = mode->clock; /* pixel clock in kHz */ 1691 if (pclk_rate == 0) { 1692 DPU_ERROR("pclk is 0, cannot calculate line time\n"); 1693 return 0; 1694 } 1695 1696 pclk_period = DIV_ROUND_UP_ULL(1000000000ull, pclk_rate); 1697 if (pclk_period == 0) { 1698 DPU_ERROR("pclk period is 0\n"); 1699 return 0; 1700 } 1701 1702 /* 1703 * Line time calculation based on Pixel clock and HTOTAL. 1704 * Final unit is in ns. 1705 */ 1706 line_time = (pclk_period * mode->htotal) / 1000; 1707 if (line_time == 0) { 1708 DPU_ERROR("line time calculation is 0\n"); 1709 return 0; 1710 } 1711 1712 DPU_DEBUG_ENC(dpu_enc, 1713 "clk_rate=%lldkHz, clk_period=%d, linetime=%dns\n", 1714 pclk_rate, pclk_period, line_time); 1715 1716 return line_time; 1717 } 1718 1719 int dpu_encoder_vsync_time(struct drm_encoder *drm_enc, ktime_t *wakeup_time) 1720 { 1721 struct drm_display_mode *mode; 1722 struct dpu_encoder_virt *dpu_enc; 1723 u32 cur_line; 1724 u32 line_time; 1725 u32 vtotal, time_to_vsync; 1726 ktime_t cur_time; 1727 1728 dpu_enc = to_dpu_encoder_virt(drm_enc); 1729 1730 if (!drm_enc->crtc || !drm_enc->crtc->state) { 1731 DPU_ERROR("crtc/crtc state object is NULL\n"); 1732 return -EINVAL; 1733 } 1734 mode = &drm_enc->crtc->state->adjusted_mode; 1735 1736 line_time = _dpu_encoder_calculate_linetime(dpu_enc, mode); 1737 if (!line_time) 1738 return -EINVAL; 1739 1740 cur_line = dpu_enc->cur_master->ops.get_line_count(dpu_enc->cur_master); 1741 1742 vtotal = mode->vtotal; 1743 if (cur_line >= vtotal) 1744 time_to_vsync = line_time * vtotal; 1745 else 1746 time_to_vsync = line_time * (vtotal - cur_line); 1747 1748 if (time_to_vsync == 0) { 1749 DPU_ERROR("time to vsync should not be zero, vtotal=%d\n", 1750 vtotal); 1751 return -EINVAL; 1752 } 1753 1754 cur_time = ktime_get(); 1755 *wakeup_time = ktime_add_ns(cur_time, time_to_vsync); 1756 1757 DPU_DEBUG_ENC(dpu_enc, 1758 "cur_line=%u vtotal=%u time_to_vsync=%u, cur_time=%lld, wakeup_time=%lld\n", 1759 cur_line, vtotal, time_to_vsync, 1760 ktime_to_ms(cur_time), 1761 ktime_to_ms(*wakeup_time)); 1762 return 0; 1763 } 1764 1765 static void dpu_encoder_vsync_event_handler(struct timer_list *t) 1766 { 1767 struct dpu_encoder_virt *dpu_enc = from_timer(dpu_enc, t, 1768 vsync_event_timer); 1769 struct drm_encoder *drm_enc = &dpu_enc->base; 1770 struct msm_drm_private *priv; 1771 struct msm_drm_thread *event_thread; 1772 1773 if (!drm_enc->dev || !drm_enc->crtc) { 1774 DPU_ERROR("invalid parameters\n"); 1775 return; 1776 } 1777 1778 priv = drm_enc->dev->dev_private; 1779 1780 if (drm_enc->crtc->index >= ARRAY_SIZE(priv->event_thread)) { 1781 DPU_ERROR("invalid crtc index\n"); 1782 return; 1783 } 1784 event_thread = &priv->event_thread[drm_enc->crtc->index]; 1785 if (!event_thread) { 1786 DPU_ERROR("event_thread not found for crtc:%d\n", 1787 drm_enc->crtc->index); 1788 return; 1789 } 1790 1791 del_timer(&dpu_enc->vsync_event_timer); 1792 } 1793 1794 static void dpu_encoder_vsync_event_work_handler(struct kthread_work *work) 1795 { 1796 struct dpu_encoder_virt *dpu_enc = container_of(work, 1797 struct dpu_encoder_virt, vsync_event_work); 1798 ktime_t wakeup_time; 1799 1800 if (!dpu_enc) { 1801 DPU_ERROR("invalid dpu encoder\n"); 1802 return; 1803 } 1804 1805 if (dpu_encoder_vsync_time(&dpu_enc->base, &wakeup_time)) 1806 return; 1807 1808 trace_dpu_enc_vsync_event_work(DRMID(&dpu_enc->base), wakeup_time); 1809 mod_timer(&dpu_enc->vsync_event_timer, 1810 nsecs_to_jiffies(ktime_to_ns(wakeup_time))); 1811 } 1812 1813 void dpu_encoder_prepare_for_kickoff(struct drm_encoder *drm_enc) 1814 { 1815 struct dpu_encoder_virt *dpu_enc; 1816 struct dpu_encoder_phys *phys; 1817 bool needs_hw_reset = false; 1818 unsigned int i; 1819 1820 dpu_enc = to_dpu_encoder_virt(drm_enc); 1821 1822 trace_dpu_enc_prepare_kickoff(DRMID(drm_enc)); 1823 1824 /* prepare for next kickoff, may include waiting on previous kickoff */ 1825 DPU_ATRACE_BEGIN("enc_prepare_for_kickoff"); 1826 for (i = 0; i < dpu_enc->num_phys_encs; i++) { 1827 phys = dpu_enc->phys_encs[i]; 1828 if (phys->ops.prepare_for_kickoff) 1829 phys->ops.prepare_for_kickoff(phys); 1830 if (phys->enable_state == DPU_ENC_ERR_NEEDS_HW_RESET) 1831 needs_hw_reset = true; 1832 } 1833 DPU_ATRACE_END("enc_prepare_for_kickoff"); 1834 1835 dpu_encoder_resource_control(drm_enc, DPU_ENC_RC_EVENT_KICKOFF); 1836 1837 /* if any phys needs reset, reset all phys, in-order */ 1838 if (needs_hw_reset) { 1839 trace_dpu_enc_prepare_kickoff_reset(DRMID(drm_enc)); 1840 for (i = 0; i < dpu_enc->num_phys_encs; i++) { 1841 dpu_encoder_helper_hw_reset(dpu_enc->phys_encs[i]); 1842 } 1843 } 1844 } 1845 1846 void dpu_encoder_kickoff(struct drm_encoder *drm_enc) 1847 { 1848 struct dpu_encoder_virt *dpu_enc; 1849 struct dpu_encoder_phys *phys; 1850 ktime_t wakeup_time; 1851 unsigned long timeout_ms; 1852 unsigned int i; 1853 1854 DPU_ATRACE_BEGIN("encoder_kickoff"); 1855 dpu_enc = to_dpu_encoder_virt(drm_enc); 1856 1857 trace_dpu_enc_kickoff(DRMID(drm_enc)); 1858 1859 timeout_ms = DPU_ENCODER_FRAME_DONE_TIMEOUT_FRAMES * 1000 / 1860 drm_mode_vrefresh(&drm_enc->crtc->state->adjusted_mode); 1861 1862 atomic_set(&dpu_enc->frame_done_timeout_ms, timeout_ms); 1863 mod_timer(&dpu_enc->frame_done_timer, 1864 jiffies + msecs_to_jiffies(timeout_ms)); 1865 1866 /* All phys encs are ready to go, trigger the kickoff */ 1867 _dpu_encoder_kickoff_phys(dpu_enc); 1868 1869 /* allow phys encs to handle any post-kickoff business */ 1870 for (i = 0; i < dpu_enc->num_phys_encs; i++) { 1871 phys = dpu_enc->phys_encs[i]; 1872 if (phys->ops.handle_post_kickoff) 1873 phys->ops.handle_post_kickoff(phys); 1874 } 1875 1876 if (dpu_enc->disp_info.intf_type == DRM_MODE_ENCODER_DSI && 1877 !dpu_encoder_vsync_time(drm_enc, &wakeup_time)) { 1878 trace_dpu_enc_early_kickoff(DRMID(drm_enc), 1879 ktime_to_ms(wakeup_time)); 1880 mod_timer(&dpu_enc->vsync_event_timer, 1881 nsecs_to_jiffies(ktime_to_ns(wakeup_time))); 1882 } 1883 1884 DPU_ATRACE_END("encoder_kickoff"); 1885 } 1886 1887 void dpu_encoder_prepare_commit(struct drm_encoder *drm_enc) 1888 { 1889 struct dpu_encoder_virt *dpu_enc; 1890 struct dpu_encoder_phys *phys; 1891 int i; 1892 1893 if (!drm_enc) { 1894 DPU_ERROR("invalid encoder\n"); 1895 return; 1896 } 1897 dpu_enc = to_dpu_encoder_virt(drm_enc); 1898 1899 for (i = 0; i < dpu_enc->num_phys_encs; i++) { 1900 phys = dpu_enc->phys_encs[i]; 1901 if (phys->ops.prepare_commit) 1902 phys->ops.prepare_commit(phys); 1903 } 1904 } 1905 1906 #ifdef CONFIG_DEBUG_FS 1907 static int _dpu_encoder_status_show(struct seq_file *s, void *data) 1908 { 1909 struct dpu_encoder_virt *dpu_enc = s->private; 1910 int i; 1911 1912 mutex_lock(&dpu_enc->enc_lock); 1913 for (i = 0; i < dpu_enc->num_phys_encs; i++) { 1914 struct dpu_encoder_phys *phys = dpu_enc->phys_encs[i]; 1915 1916 seq_printf(s, "intf:%d vsync:%8d underrun:%8d ", 1917 phys->intf_idx - INTF_0, 1918 atomic_read(&phys->vsync_cnt), 1919 atomic_read(&phys->underrun_cnt)); 1920 1921 switch (phys->intf_mode) { 1922 case INTF_MODE_VIDEO: 1923 seq_puts(s, "mode: video\n"); 1924 break; 1925 case INTF_MODE_CMD: 1926 seq_puts(s, "mode: command\n"); 1927 break; 1928 default: 1929 seq_puts(s, "mode: ???\n"); 1930 break; 1931 } 1932 } 1933 mutex_unlock(&dpu_enc->enc_lock); 1934 1935 return 0; 1936 } 1937 1938 DEFINE_SHOW_ATTRIBUTE(_dpu_encoder_status); 1939 1940 static int _dpu_encoder_init_debugfs(struct drm_encoder *drm_enc) 1941 { 1942 struct dpu_encoder_virt *dpu_enc = to_dpu_encoder_virt(drm_enc); 1943 int i; 1944 1945 char name[DPU_NAME_SIZE]; 1946 1947 if (!drm_enc->dev) { 1948 DPU_ERROR("invalid encoder or kms\n"); 1949 return -EINVAL; 1950 } 1951 1952 snprintf(name, DPU_NAME_SIZE, "encoder%u", drm_enc->base.id); 1953 1954 /* create overall sub-directory for the encoder */ 1955 dpu_enc->debugfs_root = debugfs_create_dir(name, 1956 drm_enc->dev->primary->debugfs_root); 1957 1958 /* don't error check these */ 1959 debugfs_create_file("status", 0600, 1960 dpu_enc->debugfs_root, dpu_enc, &_dpu_encoder_status_fops); 1961 1962 for (i = 0; i < dpu_enc->num_phys_encs; i++) 1963 if (dpu_enc->phys_encs[i]->ops.late_register) 1964 dpu_enc->phys_encs[i]->ops.late_register( 1965 dpu_enc->phys_encs[i], 1966 dpu_enc->debugfs_root); 1967 1968 return 0; 1969 } 1970 #else 1971 static int _dpu_encoder_init_debugfs(struct drm_encoder *drm_enc) 1972 { 1973 return 0; 1974 } 1975 #endif 1976 1977 static int dpu_encoder_late_register(struct drm_encoder *encoder) 1978 { 1979 return _dpu_encoder_init_debugfs(encoder); 1980 } 1981 1982 static void dpu_encoder_early_unregister(struct drm_encoder *encoder) 1983 { 1984 struct dpu_encoder_virt *dpu_enc = to_dpu_encoder_virt(encoder); 1985 1986 debugfs_remove_recursive(dpu_enc->debugfs_root); 1987 } 1988 1989 static int dpu_encoder_virt_add_phys_encs( 1990 u32 display_caps, 1991 struct dpu_encoder_virt *dpu_enc, 1992 struct dpu_enc_phys_init_params *params) 1993 { 1994 struct dpu_encoder_phys *enc = NULL; 1995 1996 DPU_DEBUG_ENC(dpu_enc, "\n"); 1997 1998 /* 1999 * We may create up to NUM_PHYS_ENCODER_TYPES physical encoder types 2000 * in this function, check up-front. 2001 */ 2002 if (dpu_enc->num_phys_encs + NUM_PHYS_ENCODER_TYPES >= 2003 ARRAY_SIZE(dpu_enc->phys_encs)) { 2004 DPU_ERROR_ENC(dpu_enc, "too many physical encoders %d\n", 2005 dpu_enc->num_phys_encs); 2006 return -EINVAL; 2007 } 2008 2009 if (display_caps & MSM_DISPLAY_CAP_VID_MODE) { 2010 enc = dpu_encoder_phys_vid_init(params); 2011 2012 if (IS_ERR_OR_NULL(enc)) { 2013 DPU_ERROR_ENC(dpu_enc, "failed to init vid enc: %ld\n", 2014 PTR_ERR(enc)); 2015 return enc == NULL ? -EINVAL : PTR_ERR(enc); 2016 } 2017 2018 dpu_enc->phys_encs[dpu_enc->num_phys_encs] = enc; 2019 ++dpu_enc->num_phys_encs; 2020 } 2021 2022 if (display_caps & MSM_DISPLAY_CAP_CMD_MODE) { 2023 enc = dpu_encoder_phys_cmd_init(params); 2024 2025 if (IS_ERR_OR_NULL(enc)) { 2026 DPU_ERROR_ENC(dpu_enc, "failed to init cmd enc: %ld\n", 2027 PTR_ERR(enc)); 2028 return enc == NULL ? -EINVAL : PTR_ERR(enc); 2029 } 2030 2031 dpu_enc->phys_encs[dpu_enc->num_phys_encs] = enc; 2032 ++dpu_enc->num_phys_encs; 2033 } 2034 2035 if (params->split_role == ENC_ROLE_SLAVE) 2036 dpu_enc->cur_slave = enc; 2037 else 2038 dpu_enc->cur_master = enc; 2039 2040 return 0; 2041 } 2042 2043 static const struct dpu_encoder_virt_ops dpu_encoder_parent_ops = { 2044 .handle_vblank_virt = dpu_encoder_vblank_callback, 2045 .handle_underrun_virt = dpu_encoder_underrun_callback, 2046 .handle_frame_done = dpu_encoder_frame_done_callback, 2047 }; 2048 2049 static int dpu_encoder_setup_display(struct dpu_encoder_virt *dpu_enc, 2050 struct dpu_kms *dpu_kms, 2051 struct msm_display_info *disp_info) 2052 { 2053 int ret = 0; 2054 int i = 0; 2055 enum dpu_intf_type intf_type = INTF_NONE; 2056 struct dpu_enc_phys_init_params phys_params; 2057 2058 if (!dpu_enc) { 2059 DPU_ERROR("invalid arg(s), enc %d\n", dpu_enc != NULL); 2060 return -EINVAL; 2061 } 2062 2063 dpu_enc->cur_master = NULL; 2064 2065 memset(&phys_params, 0, sizeof(phys_params)); 2066 phys_params.dpu_kms = dpu_kms; 2067 phys_params.parent = &dpu_enc->base; 2068 phys_params.parent_ops = &dpu_encoder_parent_ops; 2069 phys_params.enc_spinlock = &dpu_enc->enc_spinlock; 2070 2071 DPU_DEBUG("\n"); 2072 2073 switch (disp_info->intf_type) { 2074 case DRM_MODE_ENCODER_DSI: 2075 intf_type = INTF_DSI; 2076 break; 2077 case DRM_MODE_ENCODER_TMDS: 2078 intf_type = INTF_DP; 2079 break; 2080 } 2081 2082 WARN_ON(disp_info->num_of_h_tiles < 1); 2083 2084 DPU_DEBUG("dsi_info->num_of_h_tiles %d\n", disp_info->num_of_h_tiles); 2085 2086 if ((disp_info->capabilities & MSM_DISPLAY_CAP_CMD_MODE) || 2087 (disp_info->capabilities & MSM_DISPLAY_CAP_VID_MODE)) 2088 dpu_enc->idle_pc_supported = 2089 dpu_kms->catalog->caps->has_idle_pc; 2090 2091 mutex_lock(&dpu_enc->enc_lock); 2092 for (i = 0; i < disp_info->num_of_h_tiles && !ret; i++) { 2093 /* 2094 * Left-most tile is at index 0, content is controller id 2095 * h_tile_instance_ids[2] = {0, 1}; DSI0 = left, DSI1 = right 2096 * h_tile_instance_ids[2] = {1, 0}; DSI1 = left, DSI0 = right 2097 */ 2098 u32 controller_id = disp_info->h_tile_instance[i]; 2099 2100 if (disp_info->num_of_h_tiles > 1) { 2101 if (i == 0) 2102 phys_params.split_role = ENC_ROLE_MASTER; 2103 else 2104 phys_params.split_role = ENC_ROLE_SLAVE; 2105 } else { 2106 phys_params.split_role = ENC_ROLE_SOLO; 2107 } 2108 2109 DPU_DEBUG("h_tile_instance %d = %d, split_role %d\n", 2110 i, controller_id, phys_params.split_role); 2111 2112 phys_params.intf_idx = dpu_encoder_get_intf(dpu_kms->catalog, 2113 intf_type, 2114 controller_id); 2115 if (phys_params.intf_idx == INTF_MAX) { 2116 DPU_ERROR_ENC(dpu_enc, "could not get intf: type %d, id %d\n", 2117 intf_type, controller_id); 2118 ret = -EINVAL; 2119 } 2120 2121 if (!ret) { 2122 ret = dpu_encoder_virt_add_phys_encs(disp_info->capabilities, 2123 dpu_enc, 2124 &phys_params); 2125 if (ret) 2126 DPU_ERROR_ENC(dpu_enc, "failed to add phys encs\n"); 2127 } 2128 } 2129 2130 for (i = 0; i < dpu_enc->num_phys_encs; i++) { 2131 struct dpu_encoder_phys *phys = dpu_enc->phys_encs[i]; 2132 atomic_set(&phys->vsync_cnt, 0); 2133 atomic_set(&phys->underrun_cnt, 0); 2134 } 2135 mutex_unlock(&dpu_enc->enc_lock); 2136 2137 return ret; 2138 } 2139 2140 static void dpu_encoder_frame_done_timeout(struct timer_list *t) 2141 { 2142 struct dpu_encoder_virt *dpu_enc = from_timer(dpu_enc, t, 2143 frame_done_timer); 2144 struct drm_encoder *drm_enc = &dpu_enc->base; 2145 u32 event; 2146 2147 if (!drm_enc->dev) { 2148 DPU_ERROR("invalid parameters\n"); 2149 return; 2150 } 2151 2152 if (!dpu_enc->frame_busy_mask[0] || !dpu_enc->crtc_frame_event_cb) { 2153 DRM_DEBUG_KMS("id:%u invalid timeout frame_busy_mask=%lu\n", 2154 DRMID(drm_enc), dpu_enc->frame_busy_mask[0]); 2155 return; 2156 } else if (!atomic_xchg(&dpu_enc->frame_done_timeout_ms, 0)) { 2157 DRM_DEBUG_KMS("id:%u invalid timeout\n", DRMID(drm_enc)); 2158 return; 2159 } 2160 2161 DPU_ERROR_ENC(dpu_enc, "frame done timeout\n"); 2162 2163 event = DPU_ENCODER_FRAME_EVENT_ERROR; 2164 trace_dpu_enc_frame_done_timeout(DRMID(drm_enc), event); 2165 dpu_enc->crtc_frame_event_cb(dpu_enc->crtc_frame_event_cb_data, event); 2166 } 2167 2168 static const struct drm_encoder_helper_funcs dpu_encoder_helper_funcs = { 2169 .mode_set = dpu_encoder_virt_mode_set, 2170 .disable = dpu_encoder_virt_disable, 2171 .enable = dpu_kms_encoder_enable, 2172 .atomic_check = dpu_encoder_virt_atomic_check, 2173 2174 /* This is called by dpu_kms_encoder_enable */ 2175 .commit = dpu_encoder_virt_enable, 2176 }; 2177 2178 static const struct drm_encoder_funcs dpu_encoder_funcs = { 2179 .destroy = dpu_encoder_destroy, 2180 .late_register = dpu_encoder_late_register, 2181 .early_unregister = dpu_encoder_early_unregister, 2182 }; 2183 2184 int dpu_encoder_setup(struct drm_device *dev, struct drm_encoder *enc, 2185 struct msm_display_info *disp_info) 2186 { 2187 struct msm_drm_private *priv = dev->dev_private; 2188 struct dpu_kms *dpu_kms = to_dpu_kms(priv->kms); 2189 struct drm_encoder *drm_enc = NULL; 2190 struct dpu_encoder_virt *dpu_enc = NULL; 2191 int ret = 0; 2192 2193 dpu_enc = to_dpu_encoder_virt(enc); 2194 2195 ret = dpu_encoder_setup_display(dpu_enc, dpu_kms, disp_info); 2196 if (ret) 2197 goto fail; 2198 2199 atomic_set(&dpu_enc->frame_done_timeout_ms, 0); 2200 timer_setup(&dpu_enc->frame_done_timer, 2201 dpu_encoder_frame_done_timeout, 0); 2202 2203 if (disp_info->intf_type == DRM_MODE_ENCODER_DSI) 2204 timer_setup(&dpu_enc->vsync_event_timer, 2205 dpu_encoder_vsync_event_handler, 2206 0); 2207 2208 2209 INIT_DELAYED_WORK(&dpu_enc->delayed_off_work, 2210 dpu_encoder_off_work); 2211 dpu_enc->idle_timeout = IDLE_TIMEOUT; 2212 2213 kthread_init_work(&dpu_enc->vsync_event_work, 2214 dpu_encoder_vsync_event_work_handler); 2215 2216 memcpy(&dpu_enc->disp_info, disp_info, sizeof(*disp_info)); 2217 2218 DPU_DEBUG_ENC(dpu_enc, "created\n"); 2219 2220 return ret; 2221 2222 fail: 2223 DPU_ERROR("failed to create encoder\n"); 2224 if (drm_enc) 2225 dpu_encoder_destroy(drm_enc); 2226 2227 return ret; 2228 2229 2230 } 2231 2232 struct drm_encoder *dpu_encoder_init(struct drm_device *dev, 2233 int drm_enc_mode) 2234 { 2235 struct dpu_encoder_virt *dpu_enc = NULL; 2236 int rc = 0; 2237 2238 dpu_enc = devm_kzalloc(dev->dev, sizeof(*dpu_enc), GFP_KERNEL); 2239 if (!dpu_enc) 2240 return ERR_PTR(-ENOMEM); 2241 2242 rc = drm_encoder_init(dev, &dpu_enc->base, &dpu_encoder_funcs, 2243 drm_enc_mode, NULL); 2244 if (rc) { 2245 devm_kfree(dev->dev, dpu_enc); 2246 return ERR_PTR(rc); 2247 } 2248 2249 drm_encoder_helper_add(&dpu_enc->base, &dpu_encoder_helper_funcs); 2250 2251 spin_lock_init(&dpu_enc->enc_spinlock); 2252 dpu_enc->enabled = false; 2253 mutex_init(&dpu_enc->enc_lock); 2254 mutex_init(&dpu_enc->rc_lock); 2255 2256 return &dpu_enc->base; 2257 } 2258 2259 int dpu_encoder_wait_for_event(struct drm_encoder *drm_enc, 2260 enum msm_event_wait event) 2261 { 2262 int (*fn_wait)(struct dpu_encoder_phys *phys_enc) = NULL; 2263 struct dpu_encoder_virt *dpu_enc = NULL; 2264 int i, ret = 0; 2265 2266 if (!drm_enc) { 2267 DPU_ERROR("invalid encoder\n"); 2268 return -EINVAL; 2269 } 2270 dpu_enc = to_dpu_encoder_virt(drm_enc); 2271 DPU_DEBUG_ENC(dpu_enc, "\n"); 2272 2273 for (i = 0; i < dpu_enc->num_phys_encs; i++) { 2274 struct dpu_encoder_phys *phys = dpu_enc->phys_encs[i]; 2275 2276 switch (event) { 2277 case MSM_ENC_COMMIT_DONE: 2278 fn_wait = phys->ops.wait_for_commit_done; 2279 break; 2280 case MSM_ENC_TX_COMPLETE: 2281 fn_wait = phys->ops.wait_for_tx_complete; 2282 break; 2283 case MSM_ENC_VBLANK: 2284 fn_wait = phys->ops.wait_for_vblank; 2285 break; 2286 default: 2287 DPU_ERROR_ENC(dpu_enc, "unknown wait event %d\n", 2288 event); 2289 return -EINVAL; 2290 } 2291 2292 if (fn_wait) { 2293 DPU_ATRACE_BEGIN("wait_for_completion_event"); 2294 ret = fn_wait(phys); 2295 DPU_ATRACE_END("wait_for_completion_event"); 2296 if (ret) 2297 return ret; 2298 } 2299 } 2300 2301 return ret; 2302 } 2303 2304 enum dpu_intf_mode dpu_encoder_get_intf_mode(struct drm_encoder *encoder) 2305 { 2306 struct dpu_encoder_virt *dpu_enc = NULL; 2307 2308 if (!encoder) { 2309 DPU_ERROR("invalid encoder\n"); 2310 return INTF_MODE_NONE; 2311 } 2312 dpu_enc = to_dpu_encoder_virt(encoder); 2313 2314 if (dpu_enc->cur_master) 2315 return dpu_enc->cur_master->intf_mode; 2316 2317 if (dpu_enc->num_phys_encs) 2318 return dpu_enc->phys_encs[0]->intf_mode; 2319 2320 return INTF_MODE_NONE; 2321 } 2322