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