1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (c) 2015 MediaTek Inc. 4 */ 5 6 #include <linux/clk.h> 7 #include <linux/dma-mapping.h> 8 #include <linux/mailbox_controller.h> 9 #include <linux/of.h> 10 #include <linux/pm_runtime.h> 11 #include <linux/soc/mediatek/mtk-cmdq.h> 12 #include <linux/soc/mediatek/mtk-mmsys.h> 13 #include <linux/soc/mediatek/mtk-mutex.h> 14 15 #include <asm/barrier.h> 16 17 #include <drm/drm_atomic.h> 18 #include <drm/drm_atomic_helper.h> 19 #include <drm/drm_print.h> 20 #include <drm/drm_probe_helper.h> 21 #include <drm/drm_vblank.h> 22 23 #include "mtk_crtc.h" 24 #include "mtk_ddp_comp.h" 25 #include "mtk_drm_drv.h" 26 #include "mtk_plane.h" 27 28 /* 29 * struct mtk_crtc - MediaTek specific crtc structure. 30 * @base: crtc object. 31 * @enabled: records whether crtc_enable succeeded 32 * @planes: array of 4 drm_plane structures, one for each overlay plane 33 * @pending_planes: whether any plane has pending changes to be applied 34 * @mmsys_dev: pointer to the mmsys device for configuration registers 35 * @mutex: handle to one of the ten disp_mutex streams 36 * @ddp_comp_nr: number of components in ddp_comp 37 * @ddp_comp: array of pointers the mtk_ddp_comp structures used by this crtc 38 * 39 * TODO: Needs update: this header is missing a bunch of member descriptions. 40 */ 41 struct mtk_crtc { 42 struct drm_crtc base; 43 bool enabled; 44 45 bool pending_needs_vblank; 46 struct drm_pending_vblank_event *event; 47 48 struct drm_plane *planes; 49 unsigned int layer_nr; 50 bool pending_planes; 51 bool pending_async_planes; 52 53 #if IS_REACHABLE(CONFIG_MTK_CMDQ) 54 struct cmdq_client cmdq_client; 55 struct cmdq_pkt cmdq_handle; 56 u32 cmdq_event; 57 u32 cmdq_vblank_cnt; 58 wait_queue_head_t cb_blocking_queue; 59 #endif 60 61 struct device *mmsys_dev; 62 struct device *dma_dev; 63 struct mtk_mutex *mutex; 64 unsigned int ddp_comp_nr; 65 unsigned int num_conn_routes; 66 const struct mtk_drm_route *conn_routes; 67 68 /* lock for display hardware access */ 69 struct mutex hw_lock; 70 bool config_updating; 71 /* lock for config_updating to cmd buffer */ 72 spinlock_t config_lock; 73 74 struct mtk_ddp_comp *ddp_comp[]; 75 }; 76 77 struct mtk_crtc_state { 78 struct drm_crtc_state base; 79 80 bool pending_config; 81 unsigned int pending_width; 82 unsigned int pending_height; 83 unsigned int pending_vrefresh; 84 }; 85 86 static inline struct mtk_crtc *to_mtk_crtc(struct drm_crtc *c) 87 { 88 return container_of(c, struct mtk_crtc, base); 89 } 90 91 static inline struct mtk_crtc_state *to_mtk_crtc_state(struct drm_crtc_state *s) 92 { 93 return container_of(s, struct mtk_crtc_state, base); 94 } 95 96 static void mtk_crtc_finish_page_flip(struct mtk_crtc *mtk_crtc) 97 { 98 struct drm_crtc *crtc = &mtk_crtc->base; 99 unsigned long flags; 100 101 if (mtk_crtc->event) { 102 spin_lock_irqsave(&crtc->dev->event_lock, flags); 103 drm_crtc_send_vblank_event(crtc, mtk_crtc->event); 104 drm_crtc_vblank_put(crtc); 105 mtk_crtc->event = NULL; 106 spin_unlock_irqrestore(&crtc->dev->event_lock, flags); 107 } 108 } 109 110 static void mtk_drm_finish_page_flip(struct mtk_crtc *mtk_crtc) 111 { 112 unsigned long flags; 113 114 drm_crtc_handle_vblank(&mtk_crtc->base); 115 116 #if IS_REACHABLE(CONFIG_MTK_CMDQ) 117 if (mtk_crtc->cmdq_client.chan) 118 return; 119 #endif 120 121 spin_lock_irqsave(&mtk_crtc->config_lock, flags); 122 if (!mtk_crtc->config_updating && mtk_crtc->pending_needs_vblank) { 123 mtk_crtc_finish_page_flip(mtk_crtc); 124 mtk_crtc->pending_needs_vblank = false; 125 } 126 spin_unlock_irqrestore(&mtk_crtc->config_lock, flags); 127 } 128 129 static void mtk_crtc_destroy(struct drm_crtc *crtc) 130 { 131 struct mtk_crtc *mtk_crtc = to_mtk_crtc(crtc); 132 int i; 133 134 mtk_mutex_put(mtk_crtc->mutex); 135 #if IS_REACHABLE(CONFIG_MTK_CMDQ) 136 if (mtk_crtc->cmdq_client.chan) { 137 cmdq_pkt_destroy(&mtk_crtc->cmdq_client, &mtk_crtc->cmdq_handle); 138 mbox_free_channel(mtk_crtc->cmdq_client.chan); 139 mtk_crtc->cmdq_client.chan = NULL; 140 } 141 #endif 142 143 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) { 144 struct mtk_ddp_comp *comp; 145 146 comp = mtk_crtc->ddp_comp[i]; 147 mtk_ddp_comp_unregister_vblank_cb(comp); 148 } 149 150 drm_crtc_cleanup(crtc); 151 } 152 153 static struct drm_crtc_state *mtk_crtc_create_state(struct drm_crtc *crtc) 154 { 155 struct mtk_crtc_state *state; 156 157 state = kzalloc_obj(*state); 158 if (!state) 159 return ERR_PTR(-ENOMEM); 160 161 __drm_atomic_helper_crtc_state_init(&state->base, crtc); 162 163 return &state->base; 164 } 165 166 static struct drm_crtc_state *mtk_crtc_duplicate_state(struct drm_crtc *crtc) 167 { 168 struct mtk_crtc_state *state; 169 170 state = kmalloc_obj(*state); 171 if (!state) 172 return NULL; 173 174 __drm_atomic_helper_crtc_duplicate_state(crtc, &state->base); 175 176 WARN_ON(state->base.crtc != crtc); 177 state->base.crtc = crtc; 178 state->pending_config = false; 179 180 return &state->base; 181 } 182 183 static void mtk_crtc_destroy_state(struct drm_crtc *crtc, 184 struct drm_crtc_state *state) 185 { 186 __drm_atomic_helper_crtc_destroy_state(state); 187 kfree(to_mtk_crtc_state(state)); 188 } 189 190 static enum drm_mode_status 191 mtk_crtc_mode_valid(struct drm_crtc *crtc, const struct drm_display_mode *mode) 192 { 193 struct mtk_crtc *mtk_crtc = to_mtk_crtc(crtc); 194 enum drm_mode_status status = MODE_OK; 195 int i; 196 197 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) { 198 status = mtk_ddp_comp_mode_valid(mtk_crtc->ddp_comp[i], mode); 199 if (status != MODE_OK) 200 break; 201 } 202 return status; 203 } 204 205 static bool mtk_crtc_mode_fixup(struct drm_crtc *crtc, 206 const struct drm_display_mode *mode, 207 struct drm_display_mode *adjusted_mode) 208 { 209 /* Nothing to do here, but this callback is mandatory. */ 210 return true; 211 } 212 213 static void mtk_crtc_mode_set_nofb(struct drm_crtc *crtc) 214 { 215 struct mtk_crtc_state *state = to_mtk_crtc_state(crtc->state); 216 217 state->pending_width = crtc->mode.hdisplay; 218 state->pending_height = crtc->mode.vdisplay; 219 state->pending_vrefresh = drm_mode_vrefresh(&crtc->mode); 220 wmb(); /* Make sure the above parameters are set before update */ 221 state->pending_config = true; 222 } 223 224 static int mtk_crtc_ddp_clk_enable(struct mtk_crtc *mtk_crtc) 225 { 226 struct drm_device *dev = mtk_crtc->base.dev; 227 int ret; 228 int i; 229 230 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) { 231 ret = mtk_ddp_comp_clk_enable(mtk_crtc->ddp_comp[i]); 232 if (ret) { 233 drm_err(dev, "Failed to enable clock %d: %d\n", i, ret); 234 goto err; 235 } 236 } 237 238 return 0; 239 err: 240 while (--i >= 0) 241 mtk_ddp_comp_clk_disable(mtk_crtc->ddp_comp[i]); 242 return ret; 243 } 244 245 static void mtk_crtc_ddp_clk_disable(struct mtk_crtc *mtk_crtc) 246 { 247 int i; 248 249 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) 250 mtk_ddp_comp_clk_disable(mtk_crtc->ddp_comp[i]); 251 } 252 253 static 254 struct mtk_ddp_comp *mtk_ddp_comp_for_plane(struct drm_crtc *crtc, 255 struct drm_plane *plane, 256 unsigned int *local_layer) 257 { 258 struct mtk_crtc *mtk_crtc = to_mtk_crtc(crtc); 259 struct mtk_ddp_comp *comp; 260 int i, count = 0; 261 unsigned int local_index = plane - mtk_crtc->planes; 262 263 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) { 264 comp = mtk_crtc->ddp_comp[i]; 265 if (local_index < (count + mtk_ddp_comp_layer_nr(comp))) { 266 *local_layer = local_index - count; 267 return comp; 268 } 269 count += mtk_ddp_comp_layer_nr(comp); 270 } 271 272 WARN(1, "Failed to find component for plane %d\n", plane->index); 273 return NULL; 274 } 275 276 #if IS_REACHABLE(CONFIG_MTK_CMDQ) 277 static void ddp_cmdq_cb(struct mbox_client *cl, void *mssg) 278 { 279 struct cmdq_cb_data *data = mssg; 280 struct cmdq_client *cmdq_cl = container_of(cl, struct cmdq_client, client); 281 struct mtk_crtc *mtk_crtc = container_of(cmdq_cl, struct mtk_crtc, cmdq_client); 282 struct mtk_crtc_state *state; 283 unsigned int i; 284 unsigned long flags; 285 286 /* release GCE HW usage and start autosuspend */ 287 pm_runtime_mark_last_busy(cmdq_cl->chan->mbox->dev); 288 pm_runtime_put_autosuspend(cmdq_cl->chan->mbox->dev); 289 290 if (data->sta < 0) 291 return; 292 293 state = to_mtk_crtc_state(mtk_crtc->base.state); 294 295 spin_lock_irqsave(&mtk_crtc->config_lock, flags); 296 if (mtk_crtc->config_updating) 297 goto ddp_cmdq_cb_out; 298 299 state->pending_config = false; 300 301 if (mtk_crtc->pending_planes) { 302 for (i = 0; i < mtk_crtc->layer_nr; i++) { 303 struct drm_plane *plane = &mtk_crtc->planes[i]; 304 struct mtk_plane_state *plane_state; 305 306 plane_state = to_mtk_plane_state(plane->state); 307 308 plane_state->pending.config = false; 309 } 310 mtk_crtc->pending_planes = false; 311 } 312 313 if (mtk_crtc->pending_async_planes) { 314 for (i = 0; i < mtk_crtc->layer_nr; i++) { 315 struct drm_plane *plane = &mtk_crtc->planes[i]; 316 struct mtk_plane_state *plane_state; 317 318 plane_state = to_mtk_plane_state(plane->state); 319 320 plane_state->pending.async_config = false; 321 } 322 mtk_crtc->pending_async_planes = false; 323 } 324 325 ddp_cmdq_cb_out: 326 327 if (mtk_crtc->pending_needs_vblank) { 328 mtk_crtc_finish_page_flip(mtk_crtc); 329 mtk_crtc->pending_needs_vblank = false; 330 } 331 332 spin_unlock_irqrestore(&mtk_crtc->config_lock, flags); 333 334 mtk_crtc->cmdq_vblank_cnt = 0; 335 wake_up(&mtk_crtc->cb_blocking_queue); 336 } 337 #endif 338 339 static int mtk_crtc_ddp_hw_init(struct mtk_crtc *mtk_crtc) 340 { 341 struct drm_crtc *crtc = &mtk_crtc->base; 342 struct drm_connector *connector; 343 struct drm_encoder *encoder; 344 struct drm_connector_list_iter conn_iter; 345 struct drm_device *dev = mtk_crtc->base.dev; 346 unsigned int width, height, vrefresh, bpc = MTK_MAX_BPC; 347 int ret; 348 int i; 349 350 if (WARN_ON(!crtc->state)) 351 return -EINVAL; 352 353 width = crtc->state->adjusted_mode.hdisplay; 354 height = crtc->state->adjusted_mode.vdisplay; 355 vrefresh = drm_mode_vrefresh(&crtc->state->adjusted_mode); 356 357 drm_for_each_encoder(encoder, crtc->dev) { 358 if (encoder->crtc != crtc) 359 continue; 360 361 drm_connector_list_iter_begin(crtc->dev, &conn_iter); 362 drm_for_each_connector_iter(connector, &conn_iter) { 363 if (connector->encoder != encoder) 364 continue; 365 if (connector->display_info.bpc != 0 && 366 bpc > connector->display_info.bpc) 367 bpc = connector->display_info.bpc; 368 } 369 drm_connector_list_iter_end(&conn_iter); 370 } 371 372 ret = pm_runtime_resume_and_get(crtc->dev->dev); 373 if (ret < 0) { 374 drm_err(dev, "Failed to enable power domain: %d\n", ret); 375 return ret; 376 } 377 378 ret = mtk_mutex_prepare(mtk_crtc->mutex); 379 if (ret < 0) { 380 drm_err(dev, "Failed to enable mutex clock: %d\n", ret); 381 goto err_pm_runtime_put; 382 } 383 384 ret = mtk_crtc_ddp_clk_enable(mtk_crtc); 385 if (ret < 0) { 386 drm_err(dev, "Failed to enable component clocks: %d\n", ret); 387 goto err_mutex_unprepare; 388 } 389 390 for (i = 0; i < mtk_crtc->ddp_comp_nr - 1; i++) { 391 if (!mtk_ddp_comp_connect(mtk_crtc->ddp_comp[i], mtk_crtc->mmsys_dev, 392 mtk_crtc->ddp_comp[i + 1]->id)) 393 mtk_mmsys_ddp_connect(mtk_crtc->mmsys_dev, 394 mtk_crtc->ddp_comp[i]->id, 395 mtk_crtc->ddp_comp[i + 1]->id); 396 if (!mtk_ddp_comp_add(mtk_crtc->ddp_comp[i], mtk_crtc->mutex)) 397 mtk_mutex_add_comp(mtk_crtc->mutex, 398 mtk_crtc->ddp_comp[i]->id); 399 } 400 if (!mtk_ddp_comp_add(mtk_crtc->ddp_comp[i], mtk_crtc->mutex)) 401 mtk_mutex_add_comp(mtk_crtc->mutex, mtk_crtc->ddp_comp[i]->id); 402 mtk_mutex_enable(mtk_crtc->mutex); 403 404 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) { 405 struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[i]; 406 407 if (i == 1) 408 mtk_ddp_comp_bgclr_in_on(comp); 409 410 mtk_ddp_comp_config(comp, width, height, vrefresh, bpc, NULL); 411 mtk_ddp_comp_start(comp); 412 } 413 414 /* Initially configure all planes */ 415 for (i = 0; i < mtk_crtc->layer_nr; i++) { 416 struct drm_plane *plane = &mtk_crtc->planes[i]; 417 struct mtk_plane_state *plane_state; 418 struct mtk_ddp_comp *comp; 419 unsigned int local_layer; 420 421 plane_state = to_mtk_plane_state(plane->state); 422 423 /* should not enable layer before crtc enabled */ 424 plane_state->pending.enable = false; 425 comp = mtk_ddp_comp_for_plane(crtc, plane, &local_layer); 426 if (comp) 427 mtk_ddp_comp_layer_config(comp, local_layer, 428 plane_state, NULL); 429 } 430 431 return 0; 432 433 err_mutex_unprepare: 434 mtk_mutex_unprepare(mtk_crtc->mutex); 435 err_pm_runtime_put: 436 pm_runtime_put(crtc->dev->dev); 437 return ret; 438 } 439 440 static void mtk_crtc_ddp_hw_fini(struct mtk_crtc *mtk_crtc) 441 { 442 struct drm_device *drm = mtk_crtc->base.dev; 443 struct drm_crtc *crtc = &mtk_crtc->base; 444 unsigned long flags; 445 int i; 446 447 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) { 448 mtk_ddp_comp_stop(mtk_crtc->ddp_comp[i]); 449 if (i == 1) 450 mtk_ddp_comp_bgclr_in_off(mtk_crtc->ddp_comp[i]); 451 } 452 453 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) 454 if (!mtk_ddp_comp_remove(mtk_crtc->ddp_comp[i], mtk_crtc->mutex)) 455 mtk_mutex_remove_comp(mtk_crtc->mutex, 456 mtk_crtc->ddp_comp[i]->id); 457 mtk_mutex_disable(mtk_crtc->mutex); 458 for (i = 0; i < mtk_crtc->ddp_comp_nr - 1; i++) { 459 if (!mtk_ddp_comp_disconnect(mtk_crtc->ddp_comp[i], mtk_crtc->mmsys_dev, 460 mtk_crtc->ddp_comp[i + 1]->id)) 461 mtk_mmsys_ddp_disconnect(mtk_crtc->mmsys_dev, 462 mtk_crtc->ddp_comp[i]->id, 463 mtk_crtc->ddp_comp[i + 1]->id); 464 if (!mtk_ddp_comp_remove(mtk_crtc->ddp_comp[i], mtk_crtc->mutex)) 465 mtk_mutex_remove_comp(mtk_crtc->mutex, 466 mtk_crtc->ddp_comp[i]->id); 467 } 468 if (!mtk_ddp_comp_remove(mtk_crtc->ddp_comp[i], mtk_crtc->mutex)) 469 mtk_mutex_remove_comp(mtk_crtc->mutex, mtk_crtc->ddp_comp[i]->id); 470 mtk_crtc_ddp_clk_disable(mtk_crtc); 471 mtk_mutex_unprepare(mtk_crtc->mutex); 472 473 pm_runtime_put(drm->dev); 474 475 if (crtc->state->event && !crtc->state->active) { 476 spin_lock_irqsave(&crtc->dev->event_lock, flags); 477 drm_crtc_send_vblank_event(crtc, crtc->state->event); 478 crtc->state->event = NULL; 479 spin_unlock_irqrestore(&crtc->dev->event_lock, flags); 480 } 481 } 482 483 static void mtk_crtc_ddp_config(struct drm_crtc *crtc, 484 struct cmdq_pkt *cmdq_handle) 485 { 486 struct mtk_crtc *mtk_crtc = to_mtk_crtc(crtc); 487 struct mtk_crtc_state *state = to_mtk_crtc_state(mtk_crtc->base.state); 488 struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[0]; 489 unsigned int i; 490 unsigned int local_layer; 491 492 /* 493 * TODO: instead of updating the registers here, we should prepare 494 * working registers in atomic_commit and let the hardware command 495 * queue update module registers on vblank. 496 */ 497 if (state->pending_config) { 498 mtk_ddp_comp_config(comp, state->pending_width, 499 state->pending_height, 500 state->pending_vrefresh, 0, 501 cmdq_handle); 502 503 if (!cmdq_handle) 504 state->pending_config = false; 505 } 506 507 if (mtk_crtc->pending_planes) { 508 for (i = 0; i < mtk_crtc->layer_nr; i++) { 509 struct drm_plane *plane = &mtk_crtc->planes[i]; 510 struct mtk_plane_state *plane_state; 511 512 plane_state = to_mtk_plane_state(plane->state); 513 514 if (!plane_state->pending.config) 515 continue; 516 517 comp = mtk_ddp_comp_for_plane(crtc, plane, &local_layer); 518 519 if (comp) 520 mtk_ddp_comp_layer_config(comp, local_layer, 521 plane_state, 522 cmdq_handle); 523 if (!cmdq_handle) 524 plane_state->pending.config = false; 525 } 526 527 if (!cmdq_handle) 528 mtk_crtc->pending_planes = false; 529 } 530 531 if (mtk_crtc->pending_async_planes) { 532 for (i = 0; i < mtk_crtc->layer_nr; i++) { 533 struct drm_plane *plane = &mtk_crtc->planes[i]; 534 struct mtk_plane_state *plane_state; 535 536 plane_state = to_mtk_plane_state(plane->state); 537 538 if (!plane_state->pending.async_config) 539 continue; 540 541 comp = mtk_ddp_comp_for_plane(crtc, plane, &local_layer); 542 543 if (comp) 544 mtk_ddp_comp_layer_config(comp, local_layer, 545 plane_state, 546 cmdq_handle); 547 if (!cmdq_handle) 548 plane_state->pending.async_config = false; 549 } 550 551 if (!cmdq_handle) 552 mtk_crtc->pending_async_planes = false; 553 } 554 } 555 556 static void mtk_crtc_update_config(struct mtk_crtc *mtk_crtc, bool needs_vblank) 557 { 558 #if IS_REACHABLE(CONFIG_MTK_CMDQ) 559 struct cmdq_pkt *cmdq_handle = &mtk_crtc->cmdq_handle; 560 #endif 561 struct drm_crtc *crtc = &mtk_crtc->base; 562 struct mtk_drm_private *priv = crtc->dev->dev_private; 563 unsigned int pending_planes = 0, pending_async_planes = 0; 564 int i; 565 unsigned long flags; 566 567 mutex_lock(&mtk_crtc->hw_lock); 568 569 spin_lock_irqsave(&mtk_crtc->config_lock, flags); 570 mtk_crtc->config_updating = true; 571 spin_unlock_irqrestore(&mtk_crtc->config_lock, flags); 572 573 if (needs_vblank) 574 mtk_crtc->pending_needs_vblank = true; 575 576 for (i = 0; i < mtk_crtc->layer_nr; i++) { 577 struct drm_plane *plane = &mtk_crtc->planes[i]; 578 struct mtk_plane_state *plane_state; 579 580 plane_state = to_mtk_plane_state(plane->state); 581 if (plane_state->pending.dirty) { 582 plane_state->pending.config = true; 583 plane_state->pending.dirty = false; 584 pending_planes |= BIT(i); 585 } else if (plane_state->pending.async_dirty) { 586 plane_state->pending.async_config = true; 587 plane_state->pending.async_dirty = false; 588 pending_async_planes |= BIT(i); 589 } 590 } 591 if (pending_planes) 592 mtk_crtc->pending_planes = true; 593 if (pending_async_planes) 594 mtk_crtc->pending_async_planes = true; 595 596 if (priv->data->shadow_register) { 597 mtk_mutex_acquire(mtk_crtc->mutex); 598 mtk_crtc_ddp_config(crtc, NULL); 599 mtk_mutex_release(mtk_crtc->mutex); 600 } 601 #if IS_REACHABLE(CONFIG_MTK_CMDQ) 602 if (mtk_crtc->cmdq_client.chan) { 603 mbox_flush(mtk_crtc->cmdq_client.chan, 2000); 604 cmdq_handle->cmd_buf_size = 0; 605 cmdq_pkt_clear_event(cmdq_handle, mtk_crtc->cmdq_event); 606 cmdq_pkt_wfe(cmdq_handle, mtk_crtc->cmdq_event, false); 607 mtk_crtc_ddp_config(crtc, cmdq_handle); 608 cmdq_pkt_eoc(cmdq_handle); 609 dma_sync_single_for_device(mtk_crtc->cmdq_client.chan->mbox->dev, 610 cmdq_handle->pa_base, 611 cmdq_handle->cmd_buf_size, 612 DMA_TO_DEVICE); 613 /* 614 * CMDQ command should execute in next 3 vblank. 615 * One vblank interrupt before send message (occasionally) 616 * and one vblank interrupt after cmdq done, 617 * so it's timeout after 3 vblank interrupt. 618 * If it fail to execute in next 3 vblank, timeout happen. 619 */ 620 mtk_crtc->cmdq_vblank_cnt = 3; 621 622 spin_lock_irqsave(&mtk_crtc->config_lock, flags); 623 mtk_crtc->config_updating = false; 624 spin_unlock_irqrestore(&mtk_crtc->config_lock, flags); 625 626 if (pm_runtime_resume_and_get(mtk_crtc->cmdq_client.chan->mbox->dev) < 0) 627 goto update_config_out; 628 629 mbox_send_message(mtk_crtc->cmdq_client.chan, cmdq_handle); 630 mbox_client_txdone(mtk_crtc->cmdq_client.chan, 0); 631 goto update_config_out; 632 } 633 #endif 634 spin_lock_irqsave(&mtk_crtc->config_lock, flags); 635 mtk_crtc->config_updating = false; 636 spin_unlock_irqrestore(&mtk_crtc->config_lock, flags); 637 638 #if IS_REACHABLE(CONFIG_MTK_CMDQ) 639 update_config_out: 640 #endif 641 mutex_unlock(&mtk_crtc->hw_lock); 642 } 643 644 static void mtk_crtc_ddp_irq(void *data) 645 { 646 struct drm_crtc *crtc = data; 647 struct mtk_crtc *mtk_crtc = to_mtk_crtc(crtc); 648 struct mtk_drm_private *priv = crtc->dev->dev_private; 649 650 #if IS_REACHABLE(CONFIG_MTK_CMDQ) 651 struct drm_device *dev = mtk_crtc->base.dev; 652 if (!priv->data->shadow_register && !mtk_crtc->cmdq_client.chan) 653 mtk_crtc_ddp_config(crtc, NULL); 654 else if (mtk_crtc->cmdq_vblank_cnt > 0 && --mtk_crtc->cmdq_vblank_cnt == 0) 655 drm_err(dev, "mtk_crtc %d CMDQ execute command timeout!\n", 656 drm_crtc_index(&mtk_crtc->base)); 657 #else 658 if (!priv->data->shadow_register) 659 mtk_crtc_ddp_config(crtc, NULL); 660 #endif 661 mtk_drm_finish_page_flip(mtk_crtc); 662 } 663 664 static int mtk_crtc_enable_vblank(struct drm_crtc *crtc) 665 { 666 struct mtk_crtc *mtk_crtc = to_mtk_crtc(crtc); 667 struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[0]; 668 669 mtk_ddp_comp_enable_vblank(comp); 670 671 return 0; 672 } 673 674 static void mtk_crtc_disable_vblank(struct drm_crtc *crtc) 675 { 676 struct mtk_crtc *mtk_crtc = to_mtk_crtc(crtc); 677 struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[0]; 678 679 mtk_ddp_comp_disable_vblank(comp); 680 } 681 682 static void mtk_crtc_update_output(struct drm_crtc *crtc, 683 struct drm_atomic_commit *state) 684 { 685 int crtc_index = drm_crtc_index(crtc); 686 int i; 687 struct device *dev; 688 struct drm_crtc_state *crtc_state = state->crtcs[crtc_index].new_state; 689 struct mtk_crtc *mtk_crtc = to_mtk_crtc(crtc); 690 struct mtk_drm_private *priv; 691 unsigned int encoder_mask = crtc_state->encoder_mask; 692 693 if (!crtc_state->connectors_changed) 694 return; 695 696 if (!mtk_crtc->num_conn_routes) 697 return; 698 699 priv = ((struct mtk_drm_private *)crtc->dev->dev_private)->all_drm_private[crtc_index]; 700 dev = priv->dev; 701 702 dev_dbg(dev, "connector change:%d, encoder mask:0x%x for crtc:%d\n", 703 crtc_state->connectors_changed, encoder_mask, crtc_index); 704 705 for (i = 0; i < mtk_crtc->num_conn_routes; i++) { 706 unsigned int comp_id = mtk_crtc->conn_routes[i].route_ddp; 707 struct mtk_ddp_comp *comp = &priv->ddp_comp[comp_id]; 708 709 if (comp->encoder_index >= 0 && 710 (encoder_mask & BIT(comp->encoder_index))) { 711 mtk_crtc->ddp_comp[mtk_crtc->ddp_comp_nr - 1] = comp; 712 dev_dbg(dev, "Add comp_id: %d at path index %d\n", 713 comp->id, mtk_crtc->ddp_comp_nr - 1); 714 break; 715 } 716 } 717 } 718 719 int mtk_crtc_plane_check(struct drm_crtc *crtc, struct drm_plane *plane, 720 struct mtk_plane_state *state) 721 { 722 unsigned int local_layer; 723 struct mtk_ddp_comp *comp; 724 725 comp = mtk_ddp_comp_for_plane(crtc, plane, &local_layer); 726 if (comp) 727 return mtk_ddp_comp_layer_check(comp, local_layer, state); 728 return 0; 729 } 730 731 void mtk_crtc_plane_disable(struct drm_crtc *crtc, struct drm_plane *plane) 732 { 733 #if IS_REACHABLE(CONFIG_MTK_CMDQ) 734 struct mtk_crtc *mtk_crtc = to_mtk_crtc(crtc); 735 struct mtk_plane_state *plane_state = to_mtk_plane_state(plane->state); 736 int i; 737 738 /* no need to wait for disabling the plane by CPU */ 739 if (!mtk_crtc->cmdq_client.chan) 740 return; 741 742 if (!mtk_crtc->enabled) 743 return; 744 745 /* set pending plane state to disabled */ 746 for (i = 0; i < mtk_crtc->layer_nr; i++) { 747 struct drm_plane *mtk_plane = &mtk_crtc->planes[i]; 748 struct mtk_plane_state *mtk_plane_state = to_mtk_plane_state(mtk_plane->state); 749 750 if (mtk_plane->index == plane->index) { 751 memcpy(mtk_plane_state, plane_state, sizeof(*plane_state)); 752 break; 753 } 754 } 755 mtk_crtc_update_config(mtk_crtc, false); 756 757 /* wait for planes to be disabled by CMDQ */ 758 wait_event_timeout(mtk_crtc->cb_blocking_queue, 759 mtk_crtc->cmdq_vblank_cnt == 0, 760 msecs_to_jiffies(500)); 761 #endif 762 } 763 764 void mtk_crtc_async_update(struct drm_crtc *crtc, struct drm_plane *plane, 765 struct drm_atomic_commit *state) 766 { 767 struct mtk_crtc *mtk_crtc = to_mtk_crtc(crtc); 768 769 if (!mtk_crtc->enabled) 770 return; 771 772 mtk_crtc_update_config(mtk_crtc, false); 773 } 774 775 static void mtk_crtc_atomic_enable(struct drm_crtc *crtc, 776 struct drm_atomic_commit *state) 777 { 778 struct mtk_crtc *mtk_crtc = to_mtk_crtc(crtc); 779 struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[0]; 780 struct drm_device *dev = mtk_crtc->base.dev; 781 int ret; 782 783 drm_dbg_driver(dev, "%s %d\n", __func__, crtc->base.id); 784 785 ret = mtk_ddp_comp_power_on(comp); 786 if (ret < 0) { 787 DRM_DEV_ERROR(comp->dev, "Failed to enable power domain: %d\n", ret); 788 return; 789 } 790 791 mtk_crtc_update_output(crtc, state); 792 793 ret = mtk_crtc_ddp_hw_init(mtk_crtc); 794 if (ret) { 795 mtk_ddp_comp_power_off(comp); 796 return; 797 } 798 799 drm_crtc_vblank_on(crtc); 800 mtk_crtc->enabled = true; 801 } 802 803 static void mtk_crtc_atomic_disable(struct drm_crtc *crtc, 804 struct drm_atomic_commit *state) 805 { 806 struct mtk_crtc *mtk_crtc = to_mtk_crtc(crtc); 807 struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[0]; 808 struct drm_device *dev = mtk_crtc->base.dev; 809 int i; 810 811 drm_dbg_driver(dev, "%s %d\n", __func__, crtc->base.id); 812 if (!mtk_crtc->enabled) 813 return; 814 815 /* Set all pending plane state to disabled */ 816 for (i = 0; i < mtk_crtc->layer_nr; i++) { 817 struct drm_plane *plane = &mtk_crtc->planes[i]; 818 struct mtk_plane_state *plane_state; 819 820 plane_state = to_mtk_plane_state(plane->state); 821 plane_state->pending.enable = false; 822 plane_state->pending.config = true; 823 } 824 mtk_crtc->pending_planes = true; 825 826 mtk_crtc_update_config(mtk_crtc, false); 827 #if IS_REACHABLE(CONFIG_MTK_CMDQ) 828 /* Wait for planes to be disabled by cmdq */ 829 if (mtk_crtc->cmdq_client.chan) 830 wait_event_timeout(mtk_crtc->cb_blocking_queue, 831 mtk_crtc->cmdq_vblank_cnt == 0, 832 msecs_to_jiffies(500)); 833 #endif 834 /* Wait for planes to be disabled */ 835 drm_crtc_wait_one_vblank(crtc); 836 837 drm_crtc_vblank_off(crtc); 838 mtk_crtc_ddp_hw_fini(mtk_crtc); 839 mtk_ddp_comp_power_off(comp); 840 841 mtk_crtc->enabled = false; 842 } 843 844 static void mtk_crtc_atomic_begin(struct drm_crtc *crtc, 845 struct drm_atomic_commit *state) 846 { 847 struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, 848 crtc); 849 struct mtk_crtc_state *mtk_crtc_state = to_mtk_crtc_state(crtc_state); 850 struct mtk_crtc *mtk_crtc = to_mtk_crtc(crtc); 851 struct drm_device *dev = mtk_crtc->base.dev; 852 unsigned long flags; 853 854 if (mtk_crtc->event && mtk_crtc_state->base.event) 855 drm_err(dev, "new event while there is still a pending event\n"); 856 857 if (mtk_crtc_state->base.event) { 858 mtk_crtc_state->base.event->pipe = drm_crtc_index(crtc); 859 WARN_ON(drm_crtc_vblank_get(crtc) != 0); 860 861 spin_lock_irqsave(&crtc->dev->event_lock, flags); 862 mtk_crtc->event = mtk_crtc_state->base.event; 863 spin_unlock_irqrestore(&crtc->dev->event_lock, flags); 864 865 mtk_crtc_state->base.event = NULL; 866 } 867 } 868 869 static void mtk_crtc_atomic_flush(struct drm_crtc *crtc, 870 struct drm_atomic_commit *state) 871 { 872 struct mtk_crtc *mtk_crtc = to_mtk_crtc(crtc); 873 int i; 874 875 if (crtc->state->color_mgmt_changed) 876 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) { 877 mtk_ddp_gamma_set(mtk_crtc->ddp_comp[i], crtc->state); 878 mtk_ddp_ctm_set(mtk_crtc->ddp_comp[i], crtc->state); 879 } 880 mtk_crtc_update_config(mtk_crtc, !!mtk_crtc->event); 881 } 882 883 static const struct drm_crtc_funcs mtk_crtc_funcs = { 884 .set_config = drm_atomic_helper_set_config, 885 .page_flip = drm_atomic_helper_page_flip, 886 .destroy = mtk_crtc_destroy, 887 .atomic_create_state = mtk_crtc_create_state, 888 .atomic_duplicate_state = mtk_crtc_duplicate_state, 889 .atomic_destroy_state = mtk_crtc_destroy_state, 890 .enable_vblank = mtk_crtc_enable_vblank, 891 .disable_vblank = mtk_crtc_disable_vblank, 892 }; 893 894 static const struct drm_crtc_helper_funcs mtk_crtc_helper_funcs = { 895 .mode_fixup = mtk_crtc_mode_fixup, 896 .mode_set_nofb = mtk_crtc_mode_set_nofb, 897 .mode_valid = mtk_crtc_mode_valid, 898 .atomic_begin = mtk_crtc_atomic_begin, 899 .atomic_flush = mtk_crtc_atomic_flush, 900 .atomic_enable = mtk_crtc_atomic_enable, 901 .atomic_disable = mtk_crtc_atomic_disable, 902 }; 903 904 static int mtk_crtc_init(struct drm_device *drm, struct mtk_crtc *mtk_crtc, 905 unsigned int pipe) 906 { 907 struct drm_plane *primary = NULL; 908 struct drm_plane *cursor = NULL; 909 int i, ret; 910 911 for (i = 0; i < mtk_crtc->layer_nr; i++) { 912 if (mtk_crtc->planes[i].type == DRM_PLANE_TYPE_PRIMARY) 913 primary = &mtk_crtc->planes[i]; 914 else if (mtk_crtc->planes[i].type == DRM_PLANE_TYPE_CURSOR) 915 cursor = &mtk_crtc->planes[i]; 916 } 917 918 ret = drm_crtc_init_with_planes(drm, &mtk_crtc->base, primary, cursor, 919 &mtk_crtc_funcs, NULL); 920 if (ret) 921 goto err_cleanup_crtc; 922 923 drm_crtc_helper_add(&mtk_crtc->base, &mtk_crtc_helper_funcs); 924 925 return 0; 926 927 err_cleanup_crtc: 928 drm_crtc_cleanup(&mtk_crtc->base); 929 return ret; 930 } 931 932 static int mtk_crtc_num_comp_planes(struct mtk_crtc *mtk_crtc, int comp_idx) 933 { 934 struct mtk_ddp_comp *comp; 935 936 if (comp_idx > 1) 937 return 0; 938 939 comp = mtk_crtc->ddp_comp[comp_idx]; 940 if (!comp->funcs) 941 return 0; 942 943 if (comp_idx == 1 && !comp->funcs->bgclr_in_on) 944 return 0; 945 946 return mtk_ddp_comp_layer_nr(comp); 947 } 948 949 static inline 950 enum drm_plane_type mtk_crtc_plane_type(unsigned int plane_idx, 951 unsigned int num_planes) 952 { 953 if (plane_idx == 0) 954 return DRM_PLANE_TYPE_PRIMARY; 955 else if (plane_idx == (num_planes - 1)) 956 return DRM_PLANE_TYPE_CURSOR; 957 else 958 return DRM_PLANE_TYPE_OVERLAY; 959 960 } 961 962 static int mtk_crtc_init_comp_planes(struct drm_device *drm_dev, 963 struct mtk_crtc *mtk_crtc, 964 int comp_idx, int pipe) 965 { 966 int num_planes = mtk_crtc_num_comp_planes(mtk_crtc, comp_idx); 967 struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[comp_idx]; 968 int i, ret; 969 970 for (i = 0; i < num_planes; i++) { 971 ret = mtk_plane_init(drm_dev, 972 &mtk_crtc->planes[mtk_crtc->layer_nr], 973 BIT(pipe), 974 mtk_crtc_plane_type(mtk_crtc->layer_nr, num_planes), 975 mtk_ddp_comp_supported_rotations(comp), 976 mtk_ddp_comp_get_blend_modes(comp), 977 mtk_ddp_comp_get_formats(comp), 978 mtk_ddp_comp_get_num_formats(comp), 979 mtk_ddp_comp_is_afbc_supported(comp), i); 980 if (ret) 981 return ret; 982 983 mtk_crtc->layer_nr++; 984 } 985 return 0; 986 } 987 988 struct device *mtk_crtc_dma_dev_get(struct drm_crtc *crtc) 989 { 990 struct mtk_crtc *mtk_crtc = NULL; 991 992 if (!crtc) 993 return NULL; 994 995 mtk_crtc = to_mtk_crtc(crtc); 996 if (!mtk_crtc) 997 return NULL; 998 999 return mtk_crtc->dma_dev; 1000 } 1001 1002 int mtk_crtc_create(struct drm_device *drm_dev, const unsigned int *path, 1003 unsigned int path_len, int priv_data_index, 1004 const struct mtk_drm_route *conn_routes, 1005 unsigned int num_conn_routes) 1006 { 1007 struct mtk_drm_private *priv = drm_dev->dev_private; 1008 struct device *dev = drm_dev->dev; 1009 struct mtk_crtc *mtk_crtc; 1010 unsigned int num_comp_planes = 0; 1011 int ret; 1012 int i; 1013 bool has_ctm = false; 1014 uint gamma_lut_size = 0; 1015 struct drm_crtc *tmp; 1016 int crtc_i = 0; 1017 1018 if (!path) 1019 return 0; 1020 1021 priv = priv->all_drm_private[priv_data_index]; 1022 1023 drm_for_each_crtc(tmp, drm_dev) 1024 crtc_i++; 1025 1026 for (i = 0; i < path_len; i++) { 1027 enum mtk_ddp_comp_id comp_id = path[i]; 1028 struct device_node *node; 1029 struct mtk_ddp_comp *comp; 1030 1031 node = priv->comp_node[comp_id]; 1032 comp = &priv->ddp_comp[comp_id]; 1033 1034 /* Not all drm components have a DTS device node, such as ovl_adaptor, 1035 * which is the drm bring up sub driver 1036 */ 1037 if (!node && comp_id != DDP_COMPONENT_DRM_OVL_ADAPTOR) { 1038 dev_info(dev, 1039 "Not creating crtc %d because component %d is disabled or missing\n", 1040 crtc_i, comp_id); 1041 return 0; 1042 } 1043 1044 if (!comp->dev) { 1045 dev_err(dev, "Component %pOF not initialized\n", node); 1046 return -ENODEV; 1047 } 1048 } 1049 1050 mtk_crtc = devm_kzalloc(dev, 1051 struct_size(mtk_crtc, ddp_comp, path_len + (conn_routes ? 1 : 0)), 1052 GFP_KERNEL); 1053 if (!mtk_crtc) 1054 return -ENOMEM; 1055 1056 mtk_crtc->ddp_comp_nr = path_len; 1057 mtk_crtc->mmsys_dev = priv->mmsys_dev; 1058 1059 mtk_crtc->mutex = mtk_mutex_get(priv->mutex_dev); 1060 if (IS_ERR(mtk_crtc->mutex)) { 1061 ret = PTR_ERR(mtk_crtc->mutex); 1062 dev_err(dev, "Failed to get mutex: %d\n", ret); 1063 return ret; 1064 } 1065 1066 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) { 1067 unsigned int comp_id = path[i]; 1068 struct mtk_ddp_comp *comp; 1069 1070 comp = &priv->ddp_comp[comp_id]; 1071 mtk_crtc->ddp_comp[i] = comp; 1072 1073 if (comp->funcs) { 1074 if (comp->funcs->gamma_set && comp->funcs->gamma_get_lut_size) { 1075 unsigned int lut_sz = mtk_ddp_gamma_get_lut_size(comp); 1076 1077 if (lut_sz) 1078 gamma_lut_size = lut_sz; 1079 } 1080 1081 if (comp->funcs->ctm_set) 1082 has_ctm = true; 1083 } 1084 1085 mtk_ddp_comp_register_vblank_cb(comp, mtk_crtc_ddp_irq, 1086 &mtk_crtc->base); 1087 } 1088 1089 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) 1090 num_comp_planes += mtk_crtc_num_comp_planes(mtk_crtc, i); 1091 1092 mtk_crtc->planes = devm_kcalloc(dev, num_comp_planes, 1093 sizeof(struct drm_plane), GFP_KERNEL); 1094 if (!mtk_crtc->planes) 1095 return -ENOMEM; 1096 1097 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) { 1098 ret = mtk_crtc_init_comp_planes(drm_dev, mtk_crtc, i, crtc_i); 1099 if (ret) 1100 return ret; 1101 } 1102 1103 /* 1104 * Default to use the first component as the dma dev. 1105 * In the case of ovl_adaptor sub driver, it needs to use the 1106 * dma_dev_get function to get representative dma dev. 1107 */ 1108 mtk_crtc->dma_dev = mtk_ddp_comp_dma_dev_get(&priv->ddp_comp[path[0]]); 1109 1110 ret = mtk_crtc_init(drm_dev, mtk_crtc, crtc_i); 1111 if (ret < 0) 1112 return ret; 1113 1114 if (gamma_lut_size) 1115 drm_mode_crtc_set_gamma_size(&mtk_crtc->base, gamma_lut_size); 1116 drm_crtc_enable_color_mgmt(&mtk_crtc->base, 0, has_ctm, gamma_lut_size); 1117 mutex_init(&mtk_crtc->hw_lock); 1118 spin_lock_init(&mtk_crtc->config_lock); 1119 1120 #if IS_REACHABLE(CONFIG_MTK_CMDQ) 1121 i = priv->mbox_index++; 1122 mtk_crtc->cmdq_client.client.dev = mtk_crtc->mmsys_dev; 1123 mtk_crtc->cmdq_client.client.tx_block = false; 1124 mtk_crtc->cmdq_client.client.knows_txdone = true; 1125 mtk_crtc->cmdq_client.client.rx_callback = ddp_cmdq_cb; 1126 mtk_crtc->cmdq_client.chan = 1127 mbox_request_channel(&mtk_crtc->cmdq_client.client, i); 1128 if (IS_ERR(mtk_crtc->cmdq_client.chan)) { 1129 dev_dbg(dev, "mtk_crtc %d failed to create mailbox client, writing register by CPU now\n", 1130 drm_crtc_index(&mtk_crtc->base)); 1131 mtk_crtc->cmdq_client.chan = NULL; 1132 } 1133 1134 if (mtk_crtc->cmdq_client.chan) { 1135 ret = of_property_read_u32_index(priv->mutex_node, 1136 "mediatek,gce-events", 1137 i, 1138 &mtk_crtc->cmdq_event); 1139 if (ret) { 1140 dev_dbg(dev, "mtk_crtc %d failed to get mediatek,gce-events property\n", 1141 drm_crtc_index(&mtk_crtc->base)); 1142 mbox_free_channel(mtk_crtc->cmdq_client.chan); 1143 mtk_crtc->cmdq_client.chan = NULL; 1144 } else { 1145 ret = cmdq_pkt_create(&mtk_crtc->cmdq_client, 1146 &mtk_crtc->cmdq_handle, 1147 PAGE_SIZE); 1148 if (ret) { 1149 dev_dbg(dev, "mtk_crtc %d failed to create cmdq packet\n", 1150 drm_crtc_index(&mtk_crtc->base)); 1151 mbox_free_channel(mtk_crtc->cmdq_client.chan); 1152 mtk_crtc->cmdq_client.chan = NULL; 1153 } 1154 } 1155 1156 /* for sending blocking cmd in crtc disable */ 1157 init_waitqueue_head(&mtk_crtc->cb_blocking_queue); 1158 } 1159 #endif 1160 1161 if (conn_routes) { 1162 for (i = 0; i < num_conn_routes; i++) { 1163 unsigned int comp_id = conn_routes[i].route_ddp; 1164 struct device_node *node = priv->comp_node[comp_id]; 1165 struct mtk_ddp_comp *comp = &priv->ddp_comp[comp_id]; 1166 1167 if (!comp->dev) { 1168 dev_dbg(dev, "comp_id:%d, Component %pOF not initialized\n", 1169 comp_id, node); 1170 /* mark encoder_index to -1, if route comp device is not enabled */ 1171 comp->encoder_index = -1; 1172 continue; 1173 } 1174 1175 mtk_ddp_comp_encoder_index_set(&priv->ddp_comp[comp_id]); 1176 } 1177 1178 mtk_crtc->num_conn_routes = num_conn_routes; 1179 mtk_crtc->conn_routes = conn_routes; 1180 1181 /* increase ddp_comp_nr at the end of mtk_crtc_create */ 1182 mtk_crtc->ddp_comp_nr++; 1183 } 1184 1185 return 0; 1186 } 1187