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