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/pm_runtime.h> 10 #include <linux/soc/mediatek/mtk-cmdq.h> 11 #include <linux/soc/mediatek/mtk-mmsys.h> 12 #include <linux/soc/mediatek/mtk-mutex.h> 13 14 #include <asm/barrier.h> 15 #include <soc/mediatek/smi.h> 16 17 #include <drm/drm_atomic.h> 18 #include <drm/drm_atomic_helper.h> 19 #include <drm/drm_plane_helper.h> 20 #include <drm/drm_probe_helper.h> 21 #include <drm/drm_vblank.h> 22 23 #include "mtk_drm_drv.h" 24 #include "mtk_drm_crtc.h" 25 #include "mtk_drm_ddp_comp.h" 26 #include "mtk_drm_gem.h" 27 #include "mtk_drm_plane.h" 28 29 /* 30 * struct mtk_drm_crtc - MediaTek specific crtc structure. 31 * @base: crtc object. 32 * @enabled: records whether crtc_enable succeeded 33 * @planes: array of 4 drm_plane structures, one for each overlay plane 34 * @pending_planes: whether any plane has pending changes to be applied 35 * @mmsys_dev: pointer to the mmsys device for configuration registers 36 * @mutex: handle to one of the ten disp_mutex streams 37 * @ddp_comp_nr: number of components in ddp_comp 38 * @ddp_comp: array of pointers the mtk_ddp_comp structures used by this crtc 39 * 40 * TODO: Needs update: this header is missing a bunch of member descriptions. 41 */ 42 struct mtk_drm_crtc { 43 struct drm_crtc base; 44 bool enabled; 45 46 bool pending_needs_vblank; 47 struct drm_pending_vblank_event *event; 48 49 struct drm_plane *planes; 50 unsigned int layer_nr; 51 bool pending_planes; 52 bool pending_async_planes; 53 54 #if IS_REACHABLE(CONFIG_MTK_CMDQ) 55 struct cmdq_client cmdq_client; 56 struct cmdq_pkt cmdq_handle; 57 u32 cmdq_event; 58 u32 cmdq_vblank_cnt; 59 wait_queue_head_t cb_blocking_queue; 60 #endif 61 62 struct device *mmsys_dev; 63 struct mtk_mutex *mutex; 64 unsigned int ddp_comp_nr; 65 struct mtk_ddp_comp **ddp_comp; 66 67 /* lock for display hardware access */ 68 struct mutex hw_lock; 69 bool config_updating; 70 }; 71 72 struct mtk_crtc_state { 73 struct drm_crtc_state base; 74 75 bool pending_config; 76 unsigned int pending_width; 77 unsigned int pending_height; 78 unsigned int pending_vrefresh; 79 }; 80 81 static inline struct mtk_drm_crtc *to_mtk_crtc(struct drm_crtc *c) 82 { 83 return container_of(c, struct mtk_drm_crtc, base); 84 } 85 86 static inline struct mtk_crtc_state *to_mtk_crtc_state(struct drm_crtc_state *s) 87 { 88 return container_of(s, struct mtk_crtc_state, base); 89 } 90 91 static void mtk_drm_crtc_finish_page_flip(struct mtk_drm_crtc *mtk_crtc) 92 { 93 struct drm_crtc *crtc = &mtk_crtc->base; 94 unsigned long flags; 95 96 spin_lock_irqsave(&crtc->dev->event_lock, flags); 97 drm_crtc_send_vblank_event(crtc, mtk_crtc->event); 98 drm_crtc_vblank_put(crtc); 99 mtk_crtc->event = NULL; 100 spin_unlock_irqrestore(&crtc->dev->event_lock, flags); 101 } 102 103 static void mtk_drm_finish_page_flip(struct mtk_drm_crtc *mtk_crtc) 104 { 105 drm_crtc_handle_vblank(&mtk_crtc->base); 106 if (!mtk_crtc->config_updating && mtk_crtc->pending_needs_vblank) { 107 mtk_drm_crtc_finish_page_flip(mtk_crtc); 108 mtk_crtc->pending_needs_vblank = false; 109 } 110 } 111 112 #if IS_REACHABLE(CONFIG_MTK_CMDQ) 113 static int mtk_drm_cmdq_pkt_create(struct cmdq_client *client, struct cmdq_pkt *pkt, 114 size_t size) 115 { 116 struct device *dev; 117 dma_addr_t dma_addr; 118 119 pkt->va_base = kzalloc(size, GFP_KERNEL); 120 if (!pkt->va_base) { 121 kfree(pkt); 122 return -ENOMEM; 123 } 124 pkt->buf_size = size; 125 pkt->cl = (void *)client; 126 127 dev = client->chan->mbox->dev; 128 dma_addr = dma_map_single(dev, pkt->va_base, pkt->buf_size, 129 DMA_TO_DEVICE); 130 if (dma_mapping_error(dev, dma_addr)) { 131 dev_err(dev, "dma map failed, size=%u\n", (u32)(u64)size); 132 kfree(pkt->va_base); 133 kfree(pkt); 134 return -ENOMEM; 135 } 136 137 pkt->pa_base = dma_addr; 138 139 return 0; 140 } 141 142 static void mtk_drm_cmdq_pkt_destroy(struct cmdq_pkt *pkt) 143 { 144 struct cmdq_client *client = (struct cmdq_client *)pkt->cl; 145 146 dma_unmap_single(client->chan->mbox->dev, pkt->pa_base, pkt->buf_size, 147 DMA_TO_DEVICE); 148 kfree(pkt->va_base); 149 kfree(pkt); 150 } 151 #endif 152 153 static void mtk_drm_crtc_destroy(struct drm_crtc *crtc) 154 { 155 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc); 156 157 mtk_mutex_put(mtk_crtc->mutex); 158 #if IS_REACHABLE(CONFIG_MTK_CMDQ) 159 mtk_drm_cmdq_pkt_destroy(&mtk_crtc->cmdq_handle); 160 161 if (mtk_crtc->cmdq_client.chan) { 162 mbox_free_channel(mtk_crtc->cmdq_client.chan); 163 mtk_crtc->cmdq_client.chan = NULL; 164 } 165 #endif 166 drm_crtc_cleanup(crtc); 167 } 168 169 static void mtk_drm_crtc_reset(struct drm_crtc *crtc) 170 { 171 struct mtk_crtc_state *state; 172 173 if (crtc->state) 174 __drm_atomic_helper_crtc_destroy_state(crtc->state); 175 176 kfree(to_mtk_crtc_state(crtc->state)); 177 crtc->state = NULL; 178 179 state = kzalloc(sizeof(*state), GFP_KERNEL); 180 if (state) 181 __drm_atomic_helper_crtc_reset(crtc, &state->base); 182 } 183 184 static struct drm_crtc_state *mtk_drm_crtc_duplicate_state(struct drm_crtc *crtc) 185 { 186 struct mtk_crtc_state *state; 187 188 state = kzalloc(sizeof(*state), GFP_KERNEL); 189 if (!state) 190 return NULL; 191 192 __drm_atomic_helper_crtc_duplicate_state(crtc, &state->base); 193 194 WARN_ON(state->base.crtc != crtc); 195 state->base.crtc = crtc; 196 197 return &state->base; 198 } 199 200 static void mtk_drm_crtc_destroy_state(struct drm_crtc *crtc, 201 struct drm_crtc_state *state) 202 { 203 __drm_atomic_helper_crtc_destroy_state(state); 204 kfree(to_mtk_crtc_state(state)); 205 } 206 207 static bool mtk_drm_crtc_mode_fixup(struct drm_crtc *crtc, 208 const struct drm_display_mode *mode, 209 struct drm_display_mode *adjusted_mode) 210 { 211 /* Nothing to do here, but this callback is mandatory. */ 212 return true; 213 } 214 215 static void mtk_drm_crtc_mode_set_nofb(struct drm_crtc *crtc) 216 { 217 struct mtk_crtc_state *state = to_mtk_crtc_state(crtc->state); 218 219 state->pending_width = crtc->mode.hdisplay; 220 state->pending_height = crtc->mode.vdisplay; 221 state->pending_vrefresh = drm_mode_vrefresh(&crtc->mode); 222 wmb(); /* Make sure the above parameters are set before update */ 223 state->pending_config = true; 224 } 225 226 static int mtk_crtc_ddp_clk_enable(struct mtk_drm_crtc *mtk_crtc) 227 { 228 int ret; 229 int i; 230 231 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) { 232 ret = mtk_ddp_comp_clk_enable(mtk_crtc->ddp_comp[i]); 233 if (ret) { 234 DRM_ERROR("Failed to enable clock %d: %d\n", i, ret); 235 goto err; 236 } 237 } 238 239 return 0; 240 err: 241 while (--i >= 0) 242 mtk_ddp_comp_clk_disable(mtk_crtc->ddp_comp[i]); 243 return ret; 244 } 245 246 static void mtk_crtc_ddp_clk_disable(struct mtk_drm_crtc *mtk_crtc) 247 { 248 int i; 249 250 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) 251 mtk_ddp_comp_clk_disable(mtk_crtc->ddp_comp[i]); 252 } 253 254 static 255 struct mtk_ddp_comp *mtk_drm_ddp_comp_for_plane(struct drm_crtc *crtc, 256 struct drm_plane *plane, 257 unsigned int *local_layer) 258 { 259 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc); 260 struct mtk_ddp_comp *comp; 261 int i, count = 0; 262 unsigned int local_index = plane - mtk_crtc->planes; 263 264 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) { 265 comp = mtk_crtc->ddp_comp[i]; 266 if (local_index < (count + mtk_ddp_comp_layer_nr(comp))) { 267 *local_layer = local_index - count; 268 return comp; 269 } 270 count += mtk_ddp_comp_layer_nr(comp); 271 } 272 273 WARN(1, "Failed to find component for plane %d\n", plane->index); 274 return NULL; 275 } 276 277 #if IS_REACHABLE(CONFIG_MTK_CMDQ) 278 static void ddp_cmdq_cb(struct mbox_client *cl, void *mssg) 279 { 280 struct cmdq_cb_data *data = mssg; 281 struct cmdq_client *cmdq_cl = container_of(cl, struct cmdq_client, client); 282 struct mtk_drm_crtc *mtk_crtc = container_of(cmdq_cl, struct mtk_drm_crtc, cmdq_client); 283 struct mtk_crtc_state *state; 284 unsigned int i; 285 286 if (data->sta < 0) 287 return; 288 289 state = to_mtk_crtc_state(mtk_crtc->base.state); 290 291 state->pending_config = false; 292 293 if (mtk_crtc->pending_planes) { 294 for (i = 0; i < mtk_crtc->layer_nr; i++) { 295 struct drm_plane *plane = &mtk_crtc->planes[i]; 296 struct mtk_plane_state *plane_state; 297 298 plane_state = to_mtk_plane_state(plane->state); 299 300 plane_state->pending.config = false; 301 } 302 mtk_crtc->pending_planes = false; 303 } 304 305 if (mtk_crtc->pending_async_planes) { 306 for (i = 0; i < mtk_crtc->layer_nr; i++) { 307 struct drm_plane *plane = &mtk_crtc->planes[i]; 308 struct mtk_plane_state *plane_state; 309 310 plane_state = to_mtk_plane_state(plane->state); 311 312 plane_state->pending.async_config = false; 313 } 314 mtk_crtc->pending_async_planes = false; 315 } 316 317 mtk_crtc->cmdq_vblank_cnt = 0; 318 wake_up(&mtk_crtc->cb_blocking_queue); 319 } 320 #endif 321 322 static int mtk_crtc_ddp_hw_init(struct mtk_drm_crtc *mtk_crtc) 323 { 324 struct drm_crtc *crtc = &mtk_crtc->base; 325 struct drm_connector *connector; 326 struct drm_encoder *encoder; 327 struct drm_connector_list_iter conn_iter; 328 unsigned int width, height, vrefresh, bpc = MTK_MAX_BPC; 329 int ret; 330 int i; 331 332 if (WARN_ON(!crtc->state)) 333 return -EINVAL; 334 335 width = crtc->state->adjusted_mode.hdisplay; 336 height = crtc->state->adjusted_mode.vdisplay; 337 vrefresh = drm_mode_vrefresh(&crtc->state->adjusted_mode); 338 339 drm_for_each_encoder(encoder, crtc->dev) { 340 if (encoder->crtc != crtc) 341 continue; 342 343 drm_connector_list_iter_begin(crtc->dev, &conn_iter); 344 drm_for_each_connector_iter(connector, &conn_iter) { 345 if (connector->encoder != encoder) 346 continue; 347 if (connector->display_info.bpc != 0 && 348 bpc > connector->display_info.bpc) 349 bpc = connector->display_info.bpc; 350 } 351 drm_connector_list_iter_end(&conn_iter); 352 } 353 354 ret = pm_runtime_resume_and_get(crtc->dev->dev); 355 if (ret < 0) { 356 DRM_ERROR("Failed to enable power domain: %d\n", ret); 357 return ret; 358 } 359 360 ret = mtk_mutex_prepare(mtk_crtc->mutex); 361 if (ret < 0) { 362 DRM_ERROR("Failed to enable mutex clock: %d\n", ret); 363 goto err_pm_runtime_put; 364 } 365 366 ret = mtk_crtc_ddp_clk_enable(mtk_crtc); 367 if (ret < 0) { 368 DRM_ERROR("Failed to enable component clocks: %d\n", ret); 369 goto err_mutex_unprepare; 370 } 371 372 for (i = 0; i < mtk_crtc->ddp_comp_nr - 1; i++) { 373 mtk_mmsys_ddp_connect(mtk_crtc->mmsys_dev, 374 mtk_crtc->ddp_comp[i]->id, 375 mtk_crtc->ddp_comp[i + 1]->id); 376 mtk_mutex_add_comp(mtk_crtc->mutex, 377 mtk_crtc->ddp_comp[i]->id); 378 } 379 mtk_mutex_add_comp(mtk_crtc->mutex, mtk_crtc->ddp_comp[i]->id); 380 mtk_mutex_enable(mtk_crtc->mutex); 381 382 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) { 383 struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[i]; 384 385 if (i == 1) 386 mtk_ddp_comp_bgclr_in_on(comp); 387 388 mtk_ddp_comp_config(comp, width, height, vrefresh, bpc, NULL); 389 mtk_ddp_comp_start(comp); 390 } 391 392 /* Initially configure all planes */ 393 for (i = 0; i < mtk_crtc->layer_nr; i++) { 394 struct drm_plane *plane = &mtk_crtc->planes[i]; 395 struct mtk_plane_state *plane_state; 396 struct mtk_ddp_comp *comp; 397 unsigned int local_layer; 398 399 plane_state = to_mtk_plane_state(plane->state); 400 comp = mtk_drm_ddp_comp_for_plane(crtc, plane, &local_layer); 401 if (comp) 402 mtk_ddp_comp_layer_config(comp, local_layer, 403 plane_state, NULL); 404 } 405 406 return 0; 407 408 err_mutex_unprepare: 409 mtk_mutex_unprepare(mtk_crtc->mutex); 410 err_pm_runtime_put: 411 pm_runtime_put(crtc->dev->dev); 412 return ret; 413 } 414 415 static void mtk_crtc_ddp_hw_fini(struct mtk_drm_crtc *mtk_crtc) 416 { 417 struct drm_device *drm = mtk_crtc->base.dev; 418 struct drm_crtc *crtc = &mtk_crtc->base; 419 int i; 420 421 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) { 422 mtk_ddp_comp_stop(mtk_crtc->ddp_comp[i]); 423 if (i == 1) 424 mtk_ddp_comp_bgclr_in_off(mtk_crtc->ddp_comp[i]); 425 } 426 427 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) 428 mtk_mutex_remove_comp(mtk_crtc->mutex, 429 mtk_crtc->ddp_comp[i]->id); 430 mtk_mutex_disable(mtk_crtc->mutex); 431 for (i = 0; i < mtk_crtc->ddp_comp_nr - 1; i++) { 432 mtk_mmsys_ddp_disconnect(mtk_crtc->mmsys_dev, 433 mtk_crtc->ddp_comp[i]->id, 434 mtk_crtc->ddp_comp[i + 1]->id); 435 mtk_mutex_remove_comp(mtk_crtc->mutex, 436 mtk_crtc->ddp_comp[i]->id); 437 } 438 mtk_mutex_remove_comp(mtk_crtc->mutex, mtk_crtc->ddp_comp[i]->id); 439 mtk_crtc_ddp_clk_disable(mtk_crtc); 440 mtk_mutex_unprepare(mtk_crtc->mutex); 441 442 pm_runtime_put(drm->dev); 443 444 if (crtc->state->event && !crtc->state->active) { 445 spin_lock_irq(&crtc->dev->event_lock); 446 drm_crtc_send_vblank_event(crtc, crtc->state->event); 447 crtc->state->event = NULL; 448 spin_unlock_irq(&crtc->dev->event_lock); 449 } 450 } 451 452 static void mtk_crtc_ddp_config(struct drm_crtc *crtc, 453 struct cmdq_pkt *cmdq_handle) 454 { 455 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc); 456 struct mtk_crtc_state *state = to_mtk_crtc_state(mtk_crtc->base.state); 457 struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[0]; 458 unsigned int i; 459 unsigned int local_layer; 460 461 /* 462 * TODO: instead of updating the registers here, we should prepare 463 * working registers in atomic_commit and let the hardware command 464 * queue update module registers on vblank. 465 */ 466 if (state->pending_config) { 467 mtk_ddp_comp_config(comp, state->pending_width, 468 state->pending_height, 469 state->pending_vrefresh, 0, 470 cmdq_handle); 471 472 if (!cmdq_handle) 473 state->pending_config = false; 474 } 475 476 if (mtk_crtc->pending_planes) { 477 for (i = 0; i < mtk_crtc->layer_nr; i++) { 478 struct drm_plane *plane = &mtk_crtc->planes[i]; 479 struct mtk_plane_state *plane_state; 480 481 plane_state = to_mtk_plane_state(plane->state); 482 483 if (!plane_state->pending.config) 484 continue; 485 486 comp = mtk_drm_ddp_comp_for_plane(crtc, plane, 487 &local_layer); 488 489 if (comp) 490 mtk_ddp_comp_layer_config(comp, local_layer, 491 plane_state, 492 cmdq_handle); 493 if (!cmdq_handle) 494 plane_state->pending.config = false; 495 } 496 497 if (!cmdq_handle) 498 mtk_crtc->pending_planes = false; 499 } 500 501 if (mtk_crtc->pending_async_planes) { 502 for (i = 0; i < mtk_crtc->layer_nr; i++) { 503 struct drm_plane *plane = &mtk_crtc->planes[i]; 504 struct mtk_plane_state *plane_state; 505 506 plane_state = to_mtk_plane_state(plane->state); 507 508 if (!plane_state->pending.async_config) 509 continue; 510 511 comp = mtk_drm_ddp_comp_for_plane(crtc, plane, 512 &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.async_config = false; 520 } 521 522 if (!cmdq_handle) 523 mtk_crtc->pending_async_planes = false; 524 } 525 } 526 527 static void mtk_drm_crtc_update_config(struct mtk_drm_crtc *mtk_crtc, 528 bool needs_vblank) 529 { 530 #if IS_REACHABLE(CONFIG_MTK_CMDQ) 531 struct cmdq_pkt *cmdq_handle = &mtk_crtc->cmdq_handle; 532 #endif 533 struct drm_crtc *crtc = &mtk_crtc->base; 534 struct mtk_drm_private *priv = crtc->dev->dev_private; 535 unsigned int pending_planes = 0, pending_async_planes = 0; 536 int i; 537 538 mutex_lock(&mtk_crtc->hw_lock); 539 mtk_crtc->config_updating = true; 540 if (needs_vblank) 541 mtk_crtc->pending_needs_vblank = true; 542 543 for (i = 0; i < mtk_crtc->layer_nr; i++) { 544 struct drm_plane *plane = &mtk_crtc->planes[i]; 545 struct mtk_plane_state *plane_state; 546 547 plane_state = to_mtk_plane_state(plane->state); 548 if (plane_state->pending.dirty) { 549 plane_state->pending.config = true; 550 plane_state->pending.dirty = false; 551 pending_planes |= BIT(i); 552 } else if (plane_state->pending.async_dirty) { 553 plane_state->pending.async_config = true; 554 plane_state->pending.async_dirty = false; 555 pending_async_planes |= BIT(i); 556 } 557 } 558 if (pending_planes) 559 mtk_crtc->pending_planes = true; 560 if (pending_async_planes) 561 mtk_crtc->pending_async_planes = true; 562 563 if (priv->data->shadow_register) { 564 mtk_mutex_acquire(mtk_crtc->mutex); 565 mtk_crtc_ddp_config(crtc, NULL); 566 mtk_mutex_release(mtk_crtc->mutex); 567 } 568 #if IS_REACHABLE(CONFIG_MTK_CMDQ) 569 if (mtk_crtc->cmdq_client.chan) { 570 mbox_flush(mtk_crtc->cmdq_client.chan, 2000); 571 cmdq_handle->cmd_buf_size = 0; 572 cmdq_pkt_clear_event(cmdq_handle, mtk_crtc->cmdq_event); 573 cmdq_pkt_wfe(cmdq_handle, mtk_crtc->cmdq_event, false); 574 mtk_crtc_ddp_config(crtc, cmdq_handle); 575 cmdq_pkt_finalize(cmdq_handle); 576 dma_sync_single_for_device(mtk_crtc->cmdq_client.chan->mbox->dev, 577 cmdq_handle->pa_base, 578 cmdq_handle->cmd_buf_size, 579 DMA_TO_DEVICE); 580 /* 581 * CMDQ command should execute in next 3 vblank. 582 * One vblank interrupt before send message (occasionally) 583 * and one vblank interrupt after cmdq done, 584 * so it's timeout after 3 vblank interrupt. 585 * If it fail to execute in next 3 vblank, timeout happen. 586 */ 587 mtk_crtc->cmdq_vblank_cnt = 3; 588 589 mbox_send_message(mtk_crtc->cmdq_client.chan, cmdq_handle); 590 mbox_client_txdone(mtk_crtc->cmdq_client.chan, 0); 591 } 592 #endif 593 mtk_crtc->config_updating = false; 594 mutex_unlock(&mtk_crtc->hw_lock); 595 } 596 597 static void mtk_crtc_ddp_irq(void *data) 598 { 599 struct drm_crtc *crtc = data; 600 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc); 601 struct mtk_drm_private *priv = crtc->dev->dev_private; 602 603 #if IS_REACHABLE(CONFIG_MTK_CMDQ) 604 if (!priv->data->shadow_register && !mtk_crtc->cmdq_client.chan) 605 mtk_crtc_ddp_config(crtc, NULL); 606 else if (mtk_crtc->cmdq_vblank_cnt > 0 && --mtk_crtc->cmdq_vblank_cnt == 0) 607 DRM_ERROR("mtk_crtc %d CMDQ execute command timeout!\n", 608 drm_crtc_index(&mtk_crtc->base)); 609 #else 610 if (!priv->data->shadow_register) 611 mtk_crtc_ddp_config(crtc, NULL); 612 #endif 613 mtk_drm_finish_page_flip(mtk_crtc); 614 } 615 616 static int mtk_drm_crtc_enable_vblank(struct drm_crtc *crtc) 617 { 618 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc); 619 struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[0]; 620 621 mtk_ddp_comp_enable_vblank(comp, mtk_crtc_ddp_irq, &mtk_crtc->base); 622 623 return 0; 624 } 625 626 static void mtk_drm_crtc_disable_vblank(struct drm_crtc *crtc) 627 { 628 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc); 629 struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[0]; 630 631 mtk_ddp_comp_disable_vblank(comp); 632 } 633 634 int mtk_drm_crtc_plane_check(struct drm_crtc *crtc, struct drm_plane *plane, 635 struct mtk_plane_state *state) 636 { 637 unsigned int local_layer; 638 struct mtk_ddp_comp *comp; 639 640 comp = mtk_drm_ddp_comp_for_plane(crtc, plane, &local_layer); 641 if (comp) 642 return mtk_ddp_comp_layer_check(comp, local_layer, state); 643 return 0; 644 } 645 646 void mtk_drm_crtc_async_update(struct drm_crtc *crtc, struct drm_plane *plane, 647 struct drm_atomic_state *state) 648 { 649 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc); 650 651 if (!mtk_crtc->enabled) 652 return; 653 654 mtk_drm_crtc_update_config(mtk_crtc, false); 655 } 656 657 static void mtk_drm_crtc_atomic_enable(struct drm_crtc *crtc, 658 struct drm_atomic_state *state) 659 { 660 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc); 661 struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[0]; 662 int ret; 663 664 DRM_DEBUG_DRIVER("%s %d\n", __func__, crtc->base.id); 665 666 ret = mtk_smi_larb_get(comp->larb_dev); 667 if (ret) { 668 DRM_ERROR("Failed to get larb: %d\n", ret); 669 return; 670 } 671 672 ret = mtk_crtc_ddp_hw_init(mtk_crtc); 673 if (ret) { 674 mtk_smi_larb_put(comp->larb_dev); 675 return; 676 } 677 678 drm_crtc_vblank_on(crtc); 679 mtk_crtc->enabled = true; 680 } 681 682 static void mtk_drm_crtc_atomic_disable(struct drm_crtc *crtc, 683 struct drm_atomic_state *state) 684 { 685 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc); 686 struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[0]; 687 int i; 688 689 DRM_DEBUG_DRIVER("%s %d\n", __func__, crtc->base.id); 690 if (!mtk_crtc->enabled) 691 return; 692 693 /* Set all pending plane state to disabled */ 694 for (i = 0; i < mtk_crtc->layer_nr; i++) { 695 struct drm_plane *plane = &mtk_crtc->planes[i]; 696 struct mtk_plane_state *plane_state; 697 698 plane_state = to_mtk_plane_state(plane->state); 699 plane_state->pending.enable = false; 700 plane_state->pending.config = true; 701 } 702 mtk_crtc->pending_planes = true; 703 704 mtk_drm_crtc_update_config(mtk_crtc, false); 705 #if IS_REACHABLE(CONFIG_MTK_CMDQ) 706 /* Wait for planes to be disabled by cmdq */ 707 if (mtk_crtc->cmdq_client.chan) 708 wait_event_timeout(mtk_crtc->cb_blocking_queue, 709 mtk_crtc->cmdq_vblank_cnt == 0, 710 msecs_to_jiffies(500)); 711 #endif 712 /* Wait for planes to be disabled */ 713 drm_crtc_wait_one_vblank(crtc); 714 715 drm_crtc_vblank_off(crtc); 716 mtk_crtc_ddp_hw_fini(mtk_crtc); 717 mtk_smi_larb_put(comp->larb_dev); 718 719 mtk_crtc->enabled = false; 720 } 721 722 static void mtk_drm_crtc_atomic_begin(struct drm_crtc *crtc, 723 struct drm_atomic_state *state) 724 { 725 struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, 726 crtc); 727 struct mtk_crtc_state *mtk_crtc_state = to_mtk_crtc_state(crtc_state); 728 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc); 729 730 if (mtk_crtc->event && mtk_crtc_state->base.event) 731 DRM_ERROR("new event while there is still a pending event\n"); 732 733 if (mtk_crtc_state->base.event) { 734 mtk_crtc_state->base.event->pipe = drm_crtc_index(crtc); 735 WARN_ON(drm_crtc_vblank_get(crtc) != 0); 736 mtk_crtc->event = mtk_crtc_state->base.event; 737 mtk_crtc_state->base.event = NULL; 738 } 739 } 740 741 static void mtk_drm_crtc_atomic_flush(struct drm_crtc *crtc, 742 struct drm_atomic_state *state) 743 { 744 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc); 745 int i; 746 747 if (crtc->state->color_mgmt_changed) 748 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) { 749 mtk_ddp_gamma_set(mtk_crtc->ddp_comp[i], crtc->state); 750 mtk_ddp_ctm_set(mtk_crtc->ddp_comp[i], crtc->state); 751 } 752 mtk_drm_crtc_update_config(mtk_crtc, !!mtk_crtc->event); 753 } 754 755 static const struct drm_crtc_funcs mtk_crtc_funcs = { 756 .set_config = drm_atomic_helper_set_config, 757 .page_flip = drm_atomic_helper_page_flip, 758 .destroy = mtk_drm_crtc_destroy, 759 .reset = mtk_drm_crtc_reset, 760 .atomic_duplicate_state = mtk_drm_crtc_duplicate_state, 761 .atomic_destroy_state = mtk_drm_crtc_destroy_state, 762 .enable_vblank = mtk_drm_crtc_enable_vblank, 763 .disable_vblank = mtk_drm_crtc_disable_vblank, 764 }; 765 766 static const struct drm_crtc_helper_funcs mtk_crtc_helper_funcs = { 767 .mode_fixup = mtk_drm_crtc_mode_fixup, 768 .mode_set_nofb = mtk_drm_crtc_mode_set_nofb, 769 .atomic_begin = mtk_drm_crtc_atomic_begin, 770 .atomic_flush = mtk_drm_crtc_atomic_flush, 771 .atomic_enable = mtk_drm_crtc_atomic_enable, 772 .atomic_disable = mtk_drm_crtc_atomic_disable, 773 }; 774 775 static int mtk_drm_crtc_init(struct drm_device *drm, 776 struct mtk_drm_crtc *mtk_crtc, 777 unsigned int pipe) 778 { 779 struct drm_plane *primary = NULL; 780 struct drm_plane *cursor = NULL; 781 int i, ret; 782 783 for (i = 0; i < mtk_crtc->layer_nr; i++) { 784 if (mtk_crtc->planes[i].type == DRM_PLANE_TYPE_PRIMARY) 785 primary = &mtk_crtc->planes[i]; 786 else if (mtk_crtc->planes[i].type == DRM_PLANE_TYPE_CURSOR) 787 cursor = &mtk_crtc->planes[i]; 788 } 789 790 ret = drm_crtc_init_with_planes(drm, &mtk_crtc->base, primary, cursor, 791 &mtk_crtc_funcs, NULL); 792 if (ret) 793 goto err_cleanup_crtc; 794 795 drm_crtc_helper_add(&mtk_crtc->base, &mtk_crtc_helper_funcs); 796 797 return 0; 798 799 err_cleanup_crtc: 800 drm_crtc_cleanup(&mtk_crtc->base); 801 return ret; 802 } 803 804 static int mtk_drm_crtc_num_comp_planes(struct mtk_drm_crtc *mtk_crtc, 805 int comp_idx) 806 { 807 struct mtk_ddp_comp *comp; 808 809 if (comp_idx > 1) 810 return 0; 811 812 comp = mtk_crtc->ddp_comp[comp_idx]; 813 if (!comp->funcs) 814 return 0; 815 816 if (comp_idx == 1 && !comp->funcs->bgclr_in_on) 817 return 0; 818 819 return mtk_ddp_comp_layer_nr(comp); 820 } 821 822 static inline 823 enum drm_plane_type mtk_drm_crtc_plane_type(unsigned int plane_idx, 824 unsigned int num_planes) 825 { 826 if (plane_idx == 0) 827 return DRM_PLANE_TYPE_PRIMARY; 828 else if (plane_idx == (num_planes - 1)) 829 return DRM_PLANE_TYPE_CURSOR; 830 else 831 return DRM_PLANE_TYPE_OVERLAY; 832 833 } 834 835 static int mtk_drm_crtc_init_comp_planes(struct drm_device *drm_dev, 836 struct mtk_drm_crtc *mtk_crtc, 837 int comp_idx, int pipe) 838 { 839 int num_planes = mtk_drm_crtc_num_comp_planes(mtk_crtc, comp_idx); 840 struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[comp_idx]; 841 int i, ret; 842 843 for (i = 0; i < num_planes; i++) { 844 ret = mtk_plane_init(drm_dev, 845 &mtk_crtc->planes[mtk_crtc->layer_nr], 846 BIT(pipe), 847 mtk_drm_crtc_plane_type(mtk_crtc->layer_nr, 848 num_planes), 849 mtk_ddp_comp_supported_rotations(comp)); 850 if (ret) 851 return ret; 852 853 mtk_crtc->layer_nr++; 854 } 855 return 0; 856 } 857 858 int mtk_drm_crtc_create(struct drm_device *drm_dev, 859 const enum mtk_ddp_comp_id *path, unsigned int path_len) 860 { 861 struct mtk_drm_private *priv = drm_dev->dev_private; 862 struct device *dev = drm_dev->dev; 863 struct mtk_drm_crtc *mtk_crtc; 864 unsigned int num_comp_planes = 0; 865 int pipe = priv->num_pipes; 866 int ret; 867 int i; 868 bool has_ctm = false; 869 uint gamma_lut_size = 0; 870 871 if (!path) 872 return 0; 873 874 for (i = 0; i < path_len; i++) { 875 enum mtk_ddp_comp_id comp_id = path[i]; 876 struct device_node *node; 877 struct mtk_ddp_comp *comp; 878 879 node = priv->comp_node[comp_id]; 880 comp = &priv->ddp_comp[comp_id]; 881 882 if (!node) { 883 dev_info(dev, 884 "Not creating crtc %d because component %d is disabled or missing\n", 885 pipe, comp_id); 886 return 0; 887 } 888 889 if (!comp->dev) { 890 dev_err(dev, "Component %pOF not initialized\n", node); 891 return -ENODEV; 892 } 893 } 894 895 mtk_crtc = devm_kzalloc(dev, sizeof(*mtk_crtc), GFP_KERNEL); 896 if (!mtk_crtc) 897 return -ENOMEM; 898 899 mtk_crtc->mmsys_dev = priv->mmsys_dev; 900 mtk_crtc->ddp_comp_nr = path_len; 901 mtk_crtc->ddp_comp = devm_kmalloc_array(dev, mtk_crtc->ddp_comp_nr, 902 sizeof(*mtk_crtc->ddp_comp), 903 GFP_KERNEL); 904 if (!mtk_crtc->ddp_comp) 905 return -ENOMEM; 906 907 mtk_crtc->mutex = mtk_mutex_get(priv->mutex_dev); 908 if (IS_ERR(mtk_crtc->mutex)) { 909 ret = PTR_ERR(mtk_crtc->mutex); 910 dev_err(dev, "Failed to get mutex: %d\n", ret); 911 return ret; 912 } 913 914 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) { 915 enum mtk_ddp_comp_id comp_id = path[i]; 916 struct mtk_ddp_comp *comp; 917 918 comp = &priv->ddp_comp[comp_id]; 919 mtk_crtc->ddp_comp[i] = comp; 920 921 if (comp->funcs) { 922 if (comp->funcs->gamma_set) 923 gamma_lut_size = MTK_LUT_SIZE; 924 925 if (comp->funcs->ctm_set) 926 has_ctm = true; 927 } 928 } 929 930 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) 931 num_comp_planes += mtk_drm_crtc_num_comp_planes(mtk_crtc, i); 932 933 mtk_crtc->planes = devm_kcalloc(dev, num_comp_planes, 934 sizeof(struct drm_plane), GFP_KERNEL); 935 936 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) { 937 ret = mtk_drm_crtc_init_comp_planes(drm_dev, mtk_crtc, i, 938 pipe); 939 if (ret) 940 return ret; 941 } 942 943 ret = mtk_drm_crtc_init(drm_dev, mtk_crtc, pipe); 944 if (ret < 0) 945 return ret; 946 947 if (gamma_lut_size) 948 drm_mode_crtc_set_gamma_size(&mtk_crtc->base, gamma_lut_size); 949 drm_crtc_enable_color_mgmt(&mtk_crtc->base, 0, has_ctm, gamma_lut_size); 950 priv->num_pipes++; 951 mutex_init(&mtk_crtc->hw_lock); 952 953 #if IS_REACHABLE(CONFIG_MTK_CMDQ) 954 mtk_crtc->cmdq_client.client.dev = mtk_crtc->mmsys_dev; 955 mtk_crtc->cmdq_client.client.tx_block = false; 956 mtk_crtc->cmdq_client.client.knows_txdone = true; 957 mtk_crtc->cmdq_client.client.rx_callback = ddp_cmdq_cb; 958 mtk_crtc->cmdq_client.chan = 959 mbox_request_channel(&mtk_crtc->cmdq_client.client, 960 drm_crtc_index(&mtk_crtc->base)); 961 if (IS_ERR(mtk_crtc->cmdq_client.chan)) { 962 dev_dbg(dev, "mtk_crtc %d failed to create mailbox client, writing register by CPU now\n", 963 drm_crtc_index(&mtk_crtc->base)); 964 mtk_crtc->cmdq_client.chan = NULL; 965 } 966 967 if (mtk_crtc->cmdq_client.chan) { 968 ret = of_property_read_u32_index(priv->mutex_node, 969 "mediatek,gce-events", 970 drm_crtc_index(&mtk_crtc->base), 971 &mtk_crtc->cmdq_event); 972 if (ret) { 973 dev_dbg(dev, "mtk_crtc %d failed to get mediatek,gce-events property\n", 974 drm_crtc_index(&mtk_crtc->base)); 975 mbox_free_channel(mtk_crtc->cmdq_client.chan); 976 mtk_crtc->cmdq_client.chan = NULL; 977 } else { 978 ret = mtk_drm_cmdq_pkt_create(&mtk_crtc->cmdq_client, 979 &mtk_crtc->cmdq_handle, 980 PAGE_SIZE); 981 if (ret) { 982 dev_dbg(dev, "mtk_crtc %d failed to create cmdq packet\n", 983 drm_crtc_index(&mtk_crtc->base)); 984 mbox_free_channel(mtk_crtc->cmdq_client.chan); 985 mtk_crtc->cmdq_client.chan = NULL; 986 } 987 } 988 989 /* for sending blocking cmd in crtc disable */ 990 init_waitqueue_head(&mtk_crtc->cb_blocking_queue); 991 } 992 #endif 993 return 0; 994 } 995