1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2014-2018 The Linux Foundation. All rights reserved.
4 * Copyright (C) 2013 Red Hat
5 * Author: Rob Clark <robdclark@gmail.com>
8 #define pr_fmt(fmt) "[drm:%s:%d] " fmt, __func__, __LINE__
9 #include <linux/sort.h>
10 #include <linux/debugfs.h>
11 #include <linux/ktime.h>
12 #include <linux/bits.h>
14 #include <drm/drm_atomic.h>
15 #include <drm/drm_crtc.h>
16 #include <drm/drm_flip_work.h>
17 #include <drm/drm_mode.h>
18 #include <drm/drm_probe_helper.h>
19 #include <drm/drm_rect.h>
20 #include <drm/drm_vblank.h>
23 #include "dpu_hw_lm.h"
24 #include "dpu_hw_ctl.h"
25 #include "dpu_hw_dspp.h"
27 #include "dpu_plane.h"
28 #include "dpu_encoder.h"
30 #include "dpu_core_perf.h"
31 #include "dpu_trace.h"
33 #define DPU_DRM_BLEND_OP_NOT_DEFINED 0
34 #define DPU_DRM_BLEND_OP_OPAQUE 1
35 #define DPU_DRM_BLEND_OP_PREMULTIPLIED 2
36 #define DPU_DRM_BLEND_OP_COVERAGE 3
37 #define DPU_DRM_BLEND_OP_MAX 4
39 /* layer mixer index on dpu_crtc */
43 /* timeout in ms waiting for frame done */
44 #define DPU_CRTC_FRAME_DONE_TIMEOUT_MS 60
46 #define CONVERT_S3_15(val) \
47 (((((u64)val) & ~BIT_ULL(63)) >> 17) & GENMASK_ULL(17, 0))
49 static struct dpu_kms *_dpu_crtc_get_kms(struct drm_crtc *crtc)
51 struct msm_drm_private *priv = crtc->dev->dev_private;
53 return to_dpu_kms(priv->kms);
56 static void dpu_crtc_destroy(struct drm_crtc *crtc)
58 struct dpu_crtc *dpu_crtc = to_dpu_crtc(crtc);
65 drm_crtc_cleanup(crtc);
69 static void _dpu_crtc_setup_blend_cfg(struct dpu_crtc_mixer *mixer,
70 struct dpu_plane_state *pstate, struct dpu_format *format)
72 struct dpu_hw_mixer *lm = mixer->hw_lm;
74 struct drm_format_name_buf format_name;
76 /* default to opaque blending */
77 blend_op = DPU_BLEND_FG_ALPHA_FG_CONST |
78 DPU_BLEND_BG_ALPHA_BG_CONST;
80 if (format->alpha_enable) {
81 /* coverage blending */
82 blend_op = DPU_BLEND_FG_ALPHA_FG_PIXEL |
83 DPU_BLEND_BG_ALPHA_FG_PIXEL |
84 DPU_BLEND_BG_INV_ALPHA;
87 lm->ops.setup_blend_config(lm, pstate->stage,
90 DPU_DEBUG("format:%s, alpha_en:%u blend_op:0x%x\n",
91 drm_get_format_name(format->base.pixel_format, &format_name),
92 format->alpha_enable, blend_op);
95 static void _dpu_crtc_program_lm_output_roi(struct drm_crtc *crtc)
97 struct dpu_crtc_state *crtc_state;
98 int lm_idx, lm_horiz_position;
100 crtc_state = to_dpu_crtc_state(crtc->state);
102 lm_horiz_position = 0;
103 for (lm_idx = 0; lm_idx < crtc_state->num_mixers; lm_idx++) {
104 const struct drm_rect *lm_roi = &crtc_state->lm_bounds[lm_idx];
105 struct dpu_hw_mixer *hw_lm = crtc_state->mixers[lm_idx].hw_lm;
106 struct dpu_hw_mixer_cfg cfg;
108 if (!lm_roi || !drm_rect_visible(lm_roi))
111 cfg.out_width = drm_rect_width(lm_roi);
112 cfg.out_height = drm_rect_height(lm_roi);
113 cfg.right_mixer = lm_horiz_position++;
115 hw_lm->ops.setup_mixer_out(hw_lm, &cfg);
119 static void _dpu_crtc_blend_setup_mixer(struct drm_crtc *crtc,
120 struct dpu_crtc *dpu_crtc, struct dpu_crtc_mixer *mixer)
122 struct drm_plane *plane;
123 struct drm_framebuffer *fb;
124 struct drm_plane_state *state;
125 struct dpu_crtc_state *cstate = to_dpu_crtc_state(crtc->state);
126 struct dpu_plane_state *pstate = NULL;
127 struct dpu_format *format;
128 struct dpu_hw_ctl *ctl = mixer->lm_ctl;
129 struct dpu_hw_stage_cfg *stage_cfg = &dpu_crtc->stage_cfg;
132 uint32_t stage_idx, lm_idx;
133 int zpos_cnt[DPU_STAGE_MAX + 1] = { 0 };
134 bool bg_alpha_enable = false;
136 drm_atomic_crtc_for_each_plane(plane, crtc) {
137 state = plane->state;
141 pstate = to_dpu_plane_state(state);
144 dpu_plane_get_ctl_flush(plane, ctl, &flush_mask);
146 DPU_DEBUG("crtc %d stage:%d - plane %d sspp %d fb %d\n",
150 dpu_plane_pipe(plane) - SSPP_VIG0,
151 state->fb ? state->fb->base.id : -1);
153 format = to_dpu_format(msm_framebuffer_format(pstate->base.fb));
155 if (pstate->stage == DPU_STAGE_BASE && format->alpha_enable)
156 bg_alpha_enable = true;
158 stage_idx = zpos_cnt[pstate->stage]++;
159 stage_cfg->stage[pstate->stage][stage_idx] =
160 dpu_plane_pipe(plane);
161 stage_cfg->multirect_index[pstate->stage][stage_idx] =
162 pstate->multirect_index;
164 trace_dpu_crtc_setup_mixer(DRMID(crtc), DRMID(plane),
165 state, pstate, stage_idx,
166 dpu_plane_pipe(plane) - SSPP_VIG0,
167 format->base.pixel_format,
168 fb ? fb->modifier : 0);
170 /* blend config update */
171 for (lm_idx = 0; lm_idx < cstate->num_mixers; lm_idx++) {
172 _dpu_crtc_setup_blend_cfg(mixer + lm_idx,
175 mixer[lm_idx].flush_mask |= flush_mask;
177 if (bg_alpha_enable && !format->alpha_enable)
178 mixer[lm_idx].mixer_op_mode = 0;
180 mixer[lm_idx].mixer_op_mode |=
185 _dpu_crtc_program_lm_output_roi(crtc);
189 * _dpu_crtc_blend_setup - configure crtc mixers
190 * @crtc: Pointer to drm crtc structure
192 static void _dpu_crtc_blend_setup(struct drm_crtc *crtc)
194 struct dpu_crtc *dpu_crtc = to_dpu_crtc(crtc);
195 struct dpu_crtc_state *cstate = to_dpu_crtc_state(crtc->state);
196 struct dpu_crtc_mixer *mixer = cstate->mixers;
197 struct dpu_hw_ctl *ctl;
198 struct dpu_hw_mixer *lm;
201 DPU_DEBUG("%s\n", dpu_crtc->name);
203 for (i = 0; i < cstate->num_mixers; i++) {
204 mixer[i].mixer_op_mode = 0;
205 mixer[i].flush_mask = 0;
206 if (mixer[i].lm_ctl->ops.clear_all_blendstages)
207 mixer[i].lm_ctl->ops.clear_all_blendstages(
211 /* initialize stage cfg */
212 memset(&dpu_crtc->stage_cfg, 0, sizeof(struct dpu_hw_stage_cfg));
214 _dpu_crtc_blend_setup_mixer(crtc, dpu_crtc, mixer);
216 for (i = 0; i < cstate->num_mixers; i++) {
217 ctl = mixer[i].lm_ctl;
220 lm->ops.setup_alpha_out(lm, mixer[i].mixer_op_mode);
222 mixer[i].flush_mask |= ctl->ops.get_bitmask_mixer(ctl,
223 mixer[i].hw_lm->idx);
225 /* stage config flush mask */
226 ctl->ops.update_pending_flush(ctl, mixer[i].flush_mask);
228 DPU_DEBUG("lm %d, op_mode 0x%X, ctl %d, flush mask 0x%x\n",
229 mixer[i].hw_lm->idx - LM_0,
230 mixer[i].mixer_op_mode,
232 mixer[i].flush_mask);
234 ctl->ops.setup_blendstage(ctl, mixer[i].hw_lm->idx,
235 &dpu_crtc->stage_cfg);
240 * _dpu_crtc_complete_flip - signal pending page_flip events
241 * Any pending vblank events are added to the vblank_event_list
242 * so that the next vblank interrupt shall signal them.
243 * However PAGE_FLIP events are not handled through the vblank_event_list.
244 * This API signals any pending PAGE_FLIP events requested through
245 * DRM_IOCTL_MODE_PAGE_FLIP and are cached in the dpu_crtc->event.
246 * @crtc: Pointer to drm crtc structure
248 static void _dpu_crtc_complete_flip(struct drm_crtc *crtc)
250 struct dpu_crtc *dpu_crtc = to_dpu_crtc(crtc);
251 struct drm_device *dev = crtc->dev;
254 spin_lock_irqsave(&dev->event_lock, flags);
255 if (dpu_crtc->event) {
256 DRM_DEBUG_VBL("%s: send event: %pK\n", dpu_crtc->name,
258 trace_dpu_crtc_complete_flip(DRMID(crtc));
259 drm_crtc_send_vblank_event(crtc, dpu_crtc->event);
260 dpu_crtc->event = NULL;
262 spin_unlock_irqrestore(&dev->event_lock, flags);
265 enum dpu_intf_mode dpu_crtc_get_intf_mode(struct drm_crtc *crtc)
267 struct drm_encoder *encoder;
270 * TODO: This function is called from dpu debugfs and as part of atomic
271 * check. When called from debugfs, the crtc->mutex must be held to
272 * read crtc->state. However reading crtc->state from atomic check isn't
273 * allowed (unless you have a good reason, a big comment, and a deep
274 * understanding of how the atomic/modeset locks work (<- and this is
275 * probably not possible)). So we'll keep the WARN_ON here for now, but
276 * really we need to figure out a better way to track our operating mode
278 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
280 /* TODO: Returns the first INTF_MODE, could there be multiple values? */
281 drm_for_each_encoder_mask(encoder, crtc->dev, crtc->state->encoder_mask)
282 return dpu_encoder_get_intf_mode(encoder);
284 return INTF_MODE_NONE;
287 void dpu_crtc_vblank_callback(struct drm_crtc *crtc)
289 struct dpu_crtc *dpu_crtc = to_dpu_crtc(crtc);
291 /* keep statistics on vblank callback - with auto reset via debugfs */
292 if (ktime_compare(dpu_crtc->vblank_cb_time, ktime_set(0, 0)) == 0)
293 dpu_crtc->vblank_cb_time = ktime_get();
295 dpu_crtc->vblank_cb_count++;
296 drm_crtc_handle_vblank(crtc);
297 trace_dpu_crtc_vblank_cb(DRMID(crtc));
300 static void dpu_crtc_frame_event_work(struct kthread_work *work)
302 struct dpu_crtc_frame_event *fevent = container_of(work,
303 struct dpu_crtc_frame_event, work);
304 struct drm_crtc *crtc = fevent->crtc;
305 struct dpu_crtc *dpu_crtc = to_dpu_crtc(crtc);
307 bool frame_done = false;
309 DPU_ATRACE_BEGIN("crtc_frame_event");
311 DRM_DEBUG_KMS("crtc%d event:%u ts:%lld\n", crtc->base.id, fevent->event,
312 ktime_to_ns(fevent->ts));
314 if (fevent->event & (DPU_ENCODER_FRAME_EVENT_DONE
315 | DPU_ENCODER_FRAME_EVENT_ERROR
316 | DPU_ENCODER_FRAME_EVENT_PANEL_DEAD)) {
318 if (atomic_read(&dpu_crtc->frame_pending) < 1) {
319 /* ignore vblank when not pending */
320 } else if (atomic_dec_return(&dpu_crtc->frame_pending) == 0) {
321 /* release bandwidth and other resources */
322 trace_dpu_crtc_frame_event_done(DRMID(crtc),
324 dpu_core_perf_crtc_release_bw(crtc);
326 trace_dpu_crtc_frame_event_more_pending(DRMID(crtc),
330 if (fevent->event & DPU_ENCODER_FRAME_EVENT_DONE)
331 dpu_core_perf_crtc_update(crtc, 0, false);
333 if (fevent->event & (DPU_ENCODER_FRAME_EVENT_DONE
334 | DPU_ENCODER_FRAME_EVENT_ERROR))
338 if (fevent->event & DPU_ENCODER_FRAME_EVENT_PANEL_DEAD)
339 DPU_ERROR("crtc%d ts:%lld received panel dead event\n",
340 crtc->base.id, ktime_to_ns(fevent->ts));
343 complete_all(&dpu_crtc->frame_done_comp);
345 spin_lock_irqsave(&dpu_crtc->spin_lock, flags);
346 list_add_tail(&fevent->list, &dpu_crtc->frame_event_list);
347 spin_unlock_irqrestore(&dpu_crtc->spin_lock, flags);
348 DPU_ATRACE_END("crtc_frame_event");
352 * dpu_crtc_frame_event_cb - crtc frame event callback API. CRTC module
353 * registers this API to encoder for all frame event callbacks like
354 * frame_error, frame_done, idle_timeout, etc. Encoder may call different events
355 * from different context - IRQ, user thread, commit_thread, etc. Each event
356 * should be carefully reviewed and should be processed in proper task context
357 * to avoid schedulin delay or properly manage the irq context's bottom half
360 static void dpu_crtc_frame_event_cb(void *data, u32 event)
362 struct drm_crtc *crtc = (struct drm_crtc *)data;
363 struct dpu_crtc *dpu_crtc;
364 struct msm_drm_private *priv;
365 struct dpu_crtc_frame_event *fevent;
369 /* Nothing to do on idle event */
370 if (event & DPU_ENCODER_FRAME_EVENT_IDLE)
373 dpu_crtc = to_dpu_crtc(crtc);
374 priv = crtc->dev->dev_private;
375 crtc_id = drm_crtc_index(crtc);
377 trace_dpu_crtc_frame_event_cb(DRMID(crtc), event);
379 spin_lock_irqsave(&dpu_crtc->spin_lock, flags);
380 fevent = list_first_entry_or_null(&dpu_crtc->frame_event_list,
381 struct dpu_crtc_frame_event, list);
383 list_del_init(&fevent->list);
384 spin_unlock_irqrestore(&dpu_crtc->spin_lock, flags);
387 DRM_ERROR_RATELIMITED("crtc%d event %d overflow\n", crtc->base.id, event);
391 fevent->event = event;
393 fevent->ts = ktime_get();
394 kthread_queue_work(priv->event_thread[crtc_id].worker, &fevent->work);
397 void dpu_crtc_complete_commit(struct drm_crtc *crtc)
399 trace_dpu_crtc_complete_commit(DRMID(crtc));
400 _dpu_crtc_complete_flip(crtc);
403 static void _dpu_crtc_setup_lm_bounds(struct drm_crtc *crtc,
404 struct drm_crtc_state *state)
406 struct dpu_crtc_state *cstate = to_dpu_crtc_state(state);
407 struct drm_display_mode *adj_mode = &state->adjusted_mode;
408 u32 crtc_split_width = adj_mode->hdisplay / cstate->num_mixers;
411 for (i = 0; i < cstate->num_mixers; i++) {
412 struct drm_rect *r = &cstate->lm_bounds[i];
413 r->x1 = crtc_split_width * i;
415 r->x2 = r->x1 + crtc_split_width;
416 r->y2 = adj_mode->vdisplay;
418 trace_dpu_crtc_setup_lm_bounds(DRMID(crtc), i, r);
422 static void _dpu_crtc_get_pcc_coeff(struct drm_crtc_state *state,
423 struct dpu_hw_pcc_cfg *cfg)
425 struct drm_color_ctm *ctm;
427 memset(cfg, 0, sizeof(struct dpu_hw_pcc_cfg));
429 ctm = (struct drm_color_ctm *)state->ctm->data;
434 cfg->r.r = CONVERT_S3_15(ctm->matrix[0]);
435 cfg->g.r = CONVERT_S3_15(ctm->matrix[1]);
436 cfg->b.r = CONVERT_S3_15(ctm->matrix[2]);
438 cfg->r.g = CONVERT_S3_15(ctm->matrix[3]);
439 cfg->g.g = CONVERT_S3_15(ctm->matrix[4]);
440 cfg->b.g = CONVERT_S3_15(ctm->matrix[5]);
442 cfg->r.b = CONVERT_S3_15(ctm->matrix[6]);
443 cfg->g.b = CONVERT_S3_15(ctm->matrix[7]);
444 cfg->b.b = CONVERT_S3_15(ctm->matrix[8]);
447 static void _dpu_crtc_setup_cp_blocks(struct drm_crtc *crtc)
449 struct drm_crtc_state *state = crtc->state;
450 struct dpu_crtc_state *cstate = to_dpu_crtc_state(crtc->state);
451 struct dpu_crtc_mixer *mixer = cstate->mixers;
452 struct dpu_hw_pcc_cfg cfg;
453 struct dpu_hw_ctl *ctl;
454 struct dpu_hw_dspp *dspp;
458 if (!state->color_mgmt_changed)
461 for (i = 0; i < cstate->num_mixers; i++) {
462 ctl = mixer[i].lm_ctl;
463 dspp = mixer[i].hw_dspp;
465 if (!dspp || !dspp->ops.setup_pcc)
469 dspp->ops.setup_pcc(dspp, NULL);
471 _dpu_crtc_get_pcc_coeff(state, &cfg);
472 dspp->ops.setup_pcc(dspp, &cfg);
475 mixer[i].flush_mask |= ctl->ops.get_bitmask_dspp(ctl,
476 mixer[i].hw_dspp->idx);
478 /* stage config flush mask */
479 ctl->ops.update_pending_flush(ctl, mixer[i].flush_mask);
481 DPU_DEBUG("lm %d, ctl %d, flush mask 0x%x\n",
482 mixer[i].hw_lm->idx - DSPP_0,
484 mixer[i].flush_mask);
488 static void dpu_crtc_atomic_begin(struct drm_crtc *crtc,
489 struct drm_crtc_state *old_state)
491 struct dpu_crtc_state *cstate = to_dpu_crtc_state(crtc->state);
492 struct drm_encoder *encoder;
494 if (!crtc->state->enable) {
495 DPU_DEBUG("crtc%d -> enable %d, skip atomic_begin\n",
496 crtc->base.id, crtc->state->enable);
500 DPU_DEBUG("crtc%d\n", crtc->base.id);
502 _dpu_crtc_setup_lm_bounds(crtc, crtc->state);
504 /* encoder will trigger pending mask now */
505 drm_for_each_encoder_mask(encoder, crtc->dev, crtc->state->encoder_mask)
506 dpu_encoder_trigger_kickoff_pending(encoder);
509 * If no mixers have been allocated in dpu_crtc_atomic_check(),
510 * it means we are trying to flush a CRTC whose state is disabled:
511 * nothing else needs to be done.
513 if (unlikely(!cstate->num_mixers))
516 _dpu_crtc_blend_setup(crtc);
518 _dpu_crtc_setup_cp_blocks(crtc);
521 * PP_DONE irq is only used by command mode for now.
522 * It is better to request pending before FLUSH and START trigger
523 * to make sure no pp_done irq missed.
524 * This is safe because no pp_done will happen before SW trigger
529 static void dpu_crtc_atomic_flush(struct drm_crtc *crtc,
530 struct drm_crtc_state *old_crtc_state)
532 struct dpu_crtc *dpu_crtc;
533 struct drm_device *dev;
534 struct drm_plane *plane;
535 struct msm_drm_private *priv;
537 struct dpu_crtc_state *cstate;
539 if (!crtc->state->enable) {
540 DPU_DEBUG("crtc%d -> enable %d, skip atomic_flush\n",
541 crtc->base.id, crtc->state->enable);
545 DPU_DEBUG("crtc%d\n", crtc->base.id);
547 dpu_crtc = to_dpu_crtc(crtc);
548 cstate = to_dpu_crtc_state(crtc->state);
550 priv = dev->dev_private;
552 if (crtc->index >= ARRAY_SIZE(priv->event_thread)) {
553 DPU_ERROR("invalid crtc index[%d]\n", crtc->index);
557 WARN_ON(dpu_crtc->event);
558 spin_lock_irqsave(&dev->event_lock, flags);
559 dpu_crtc->event = crtc->state->event;
560 crtc->state->event = NULL;
561 spin_unlock_irqrestore(&dev->event_lock, flags);
564 * If no mixers has been allocated in dpu_crtc_atomic_check(),
565 * it means we are trying to flush a CRTC whose state is disabled:
566 * nothing else needs to be done.
568 if (unlikely(!cstate->num_mixers))
572 * For planes without commit update, drm framework will not add
573 * those planes to current state since hardware update is not
574 * required. However, if those planes were power collapsed since
575 * last commit cycle, driver has to restore the hardware state
576 * of those planes explicitly here prior to plane flush.
578 drm_atomic_crtc_for_each_plane(plane, crtc)
579 dpu_plane_restore(plane);
581 /* update performance setting before crtc kickoff */
582 dpu_core_perf_crtc_update(crtc, 1, false);
585 * Final plane updates: Give each plane a chance to complete all
586 * required writes/flushing before crtc's "flush
587 * everything" call below.
589 drm_atomic_crtc_for_each_plane(plane, crtc) {
590 if (dpu_crtc->smmu_state.transition_error)
591 dpu_plane_set_error(plane, true);
592 dpu_plane_flush(plane);
595 /* Kickoff will be scheduled by outer layer */
599 * dpu_crtc_destroy_state - state destroy hook
601 * @state: CRTC state object to release
603 static void dpu_crtc_destroy_state(struct drm_crtc *crtc,
604 struct drm_crtc_state *state)
606 struct dpu_crtc_state *cstate = to_dpu_crtc_state(state);
608 DPU_DEBUG("crtc%d\n", crtc->base.id);
610 __drm_atomic_helper_crtc_destroy_state(state);
615 static int _dpu_crtc_wait_for_frame_done(struct drm_crtc *crtc)
617 struct dpu_crtc *dpu_crtc = to_dpu_crtc(crtc);
620 if (!atomic_read(&dpu_crtc->frame_pending)) {
621 DPU_DEBUG("no frames pending\n");
625 DPU_ATRACE_BEGIN("frame done completion wait");
626 ret = wait_for_completion_timeout(&dpu_crtc->frame_done_comp,
627 msecs_to_jiffies(DPU_CRTC_FRAME_DONE_TIMEOUT_MS));
629 DRM_ERROR("frame done wait timed out, ret:%d\n", ret);
632 DPU_ATRACE_END("frame done completion wait");
637 void dpu_crtc_commit_kickoff(struct drm_crtc *crtc)
639 struct drm_encoder *encoder;
640 struct dpu_crtc *dpu_crtc = to_dpu_crtc(crtc);
641 struct dpu_kms *dpu_kms = _dpu_crtc_get_kms(crtc);
642 struct dpu_crtc_state *cstate = to_dpu_crtc_state(crtc->state);
645 * If no mixers has been allocated in dpu_crtc_atomic_check(),
646 * it means we are trying to start a CRTC whose state is disabled:
647 * nothing else needs to be done.
649 if (unlikely(!cstate->num_mixers))
652 DPU_ATRACE_BEGIN("crtc_commit");
655 * Encoder will flush/start now, unless it has a tx pending. If so, it
656 * may delay and flush at an irq event (e.g. ppdone)
658 drm_for_each_encoder_mask(encoder, crtc->dev,
659 crtc->state->encoder_mask)
660 dpu_encoder_prepare_for_kickoff(encoder);
662 if (atomic_inc_return(&dpu_crtc->frame_pending) == 1) {
663 /* acquire bandwidth and other resources */
664 DPU_DEBUG("crtc%d first commit\n", crtc->base.id);
666 DPU_DEBUG("crtc%d commit\n", crtc->base.id);
668 dpu_crtc->play_count++;
670 dpu_vbif_clear_errors(dpu_kms);
672 drm_for_each_encoder_mask(encoder, crtc->dev, crtc->state->encoder_mask)
673 dpu_encoder_kickoff(encoder);
675 reinit_completion(&dpu_crtc->frame_done_comp);
676 DPU_ATRACE_END("crtc_commit");
679 static void dpu_crtc_reset(struct drm_crtc *crtc)
681 struct dpu_crtc_state *cstate = kzalloc(sizeof(*cstate), GFP_KERNEL);
684 dpu_crtc_destroy_state(crtc, crtc->state);
686 __drm_atomic_helper_crtc_reset(crtc, &cstate->base);
690 * dpu_crtc_duplicate_state - state duplicate hook
691 * @crtc: Pointer to drm crtc structure
693 static struct drm_crtc_state *dpu_crtc_duplicate_state(struct drm_crtc *crtc)
695 struct dpu_crtc_state *cstate, *old_cstate = to_dpu_crtc_state(crtc->state);
697 cstate = kmemdup(old_cstate, sizeof(*old_cstate), GFP_KERNEL);
699 DPU_ERROR("failed to allocate state\n");
703 /* duplicate base helper */
704 __drm_atomic_helper_crtc_duplicate_state(crtc, &cstate->base);
706 return &cstate->base;
709 static void dpu_crtc_disable(struct drm_crtc *crtc,
710 struct drm_atomic_state *state)
712 struct drm_crtc_state *old_crtc_state = drm_atomic_get_old_crtc_state(state,
714 struct dpu_crtc *dpu_crtc = to_dpu_crtc(crtc);
715 struct dpu_crtc_state *cstate = to_dpu_crtc_state(crtc->state);
716 struct drm_encoder *encoder;
718 bool release_bandwidth = false;
720 DRM_DEBUG_KMS("crtc%d\n", crtc->base.id);
722 /* Disable/save vblank irq handling */
723 drm_crtc_vblank_off(crtc);
725 drm_for_each_encoder_mask(encoder, crtc->dev,
726 old_crtc_state->encoder_mask) {
727 /* in video mode, we hold an extra bandwidth reference
728 * as we cannot drop bandwidth at frame-done if any
729 * crtc is being used in video mode.
731 if (dpu_encoder_get_intf_mode(encoder) == INTF_MODE_VIDEO)
732 release_bandwidth = true;
733 dpu_encoder_assign_crtc(encoder, NULL);
736 /* wait for frame_event_done completion */
737 if (_dpu_crtc_wait_for_frame_done(crtc))
738 DPU_ERROR("crtc%d wait for frame done failed;frame_pending%d\n",
740 atomic_read(&dpu_crtc->frame_pending));
742 trace_dpu_crtc_disable(DRMID(crtc), false, dpu_crtc);
743 dpu_crtc->enabled = false;
745 if (atomic_read(&dpu_crtc->frame_pending)) {
746 trace_dpu_crtc_disable_frame_pending(DRMID(crtc),
747 atomic_read(&dpu_crtc->frame_pending));
748 if (release_bandwidth)
749 dpu_core_perf_crtc_release_bw(crtc);
750 atomic_set(&dpu_crtc->frame_pending, 0);
753 dpu_core_perf_crtc_update(crtc, 0, true);
755 drm_for_each_encoder_mask(encoder, crtc->dev, crtc->state->encoder_mask)
756 dpu_encoder_register_frame_event_callback(encoder, NULL, NULL);
758 memset(cstate->mixers, 0, sizeof(cstate->mixers));
759 cstate->num_mixers = 0;
761 /* disable clk & bw control until clk & bw properties are set */
762 cstate->bw_control = false;
763 cstate->bw_split_vote = false;
765 if (crtc->state->event && !crtc->state->active) {
766 spin_lock_irqsave(&crtc->dev->event_lock, flags);
767 drm_crtc_send_vblank_event(crtc, crtc->state->event);
768 crtc->state->event = NULL;
769 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
772 pm_runtime_put_sync(crtc->dev->dev);
775 static void dpu_crtc_enable(struct drm_crtc *crtc,
776 struct drm_atomic_state *state)
778 struct dpu_crtc *dpu_crtc = to_dpu_crtc(crtc);
779 struct drm_encoder *encoder;
780 bool request_bandwidth = false;
782 pm_runtime_get_sync(crtc->dev->dev);
784 DRM_DEBUG_KMS("crtc%d\n", crtc->base.id);
786 drm_for_each_encoder_mask(encoder, crtc->dev, crtc->state->encoder_mask) {
787 /* in video mode, we hold an extra bandwidth reference
788 * as we cannot drop bandwidth at frame-done if any
789 * crtc is being used in video mode.
791 if (dpu_encoder_get_intf_mode(encoder) == INTF_MODE_VIDEO)
792 request_bandwidth = true;
793 dpu_encoder_register_frame_event_callback(encoder,
794 dpu_crtc_frame_event_cb, (void *)crtc);
797 if (request_bandwidth)
798 atomic_inc(&_dpu_crtc_get_kms(crtc)->bandwidth_ref);
800 trace_dpu_crtc_enable(DRMID(crtc), true, dpu_crtc);
801 dpu_crtc->enabled = true;
803 drm_for_each_encoder_mask(encoder, crtc->dev, crtc->state->encoder_mask)
804 dpu_encoder_assign_crtc(encoder, crtc);
806 /* Enable/restore vblank irq handling */
807 drm_crtc_vblank_on(crtc);
811 struct dpu_plane_state *dpu_pstate;
812 const struct drm_plane_state *drm_pstate;
817 static int dpu_crtc_atomic_check(struct drm_crtc *crtc,
818 struct drm_crtc_state *state)
820 struct dpu_crtc *dpu_crtc = to_dpu_crtc(crtc);
821 struct dpu_crtc_state *cstate = to_dpu_crtc_state(state);
822 struct plane_state *pstates;
824 const struct drm_plane_state *pstate;
825 struct drm_plane *plane;
826 struct drm_display_mode *mode;
828 int cnt = 0, rc = 0, mixer_width = 0, i, z_pos;
830 struct dpu_multirect_plane_states multirect_plane[DPU_STAGE_MAX * 2];
831 int multirect_count = 0;
832 const struct drm_plane_state *pipe_staged[SSPP_MAX];
833 int left_zpos_cnt = 0, right_zpos_cnt = 0;
834 struct drm_rect crtc_rect = { 0 };
836 pstates = kzalloc(sizeof(*pstates) * DPU_STAGE_MAX * 4, GFP_KERNEL);
838 if (!state->enable || !state->active) {
839 DPU_DEBUG("crtc%d -> enable %d, active %d, skip atomic_check\n",
840 crtc->base.id, state->enable, state->active);
844 mode = &state->adjusted_mode;
845 DPU_DEBUG("%s: check", dpu_crtc->name);
847 /* force a full mode set if active state changed */
848 if (state->active_changed)
849 state->mode_changed = true;
851 memset(pipe_staged, 0, sizeof(pipe_staged));
853 if (cstate->num_mixers) {
854 mixer_width = mode->hdisplay / cstate->num_mixers;
856 _dpu_crtc_setup_lm_bounds(crtc, state);
859 crtc_rect.x2 = mode->hdisplay;
860 crtc_rect.y2 = mode->vdisplay;
862 /* get plane state for all drm planes associated with crtc state */
863 drm_atomic_crtc_state_for_each_plane_state(plane, pstate, state) {
864 struct drm_rect dst, clip = crtc_rect;
866 if (IS_ERR_OR_NULL(pstate)) {
867 rc = PTR_ERR(pstate);
868 DPU_ERROR("%s: failed to get plane%d state, %d\n",
869 dpu_crtc->name, plane->base.id, rc);
872 if (cnt >= DPU_STAGE_MAX * 4)
875 pstates[cnt].dpu_pstate = to_dpu_plane_state(pstate);
876 pstates[cnt].drm_pstate = pstate;
877 pstates[cnt].stage = pstate->normalized_zpos;
878 pstates[cnt].pipe_id = dpu_plane_pipe(plane);
880 if (pipe_staged[pstates[cnt].pipe_id]) {
881 multirect_plane[multirect_count].r0 =
882 pipe_staged[pstates[cnt].pipe_id];
883 multirect_plane[multirect_count].r1 = pstate;
886 pipe_staged[pstates[cnt].pipe_id] = NULL;
888 pipe_staged[pstates[cnt].pipe_id] = pstate;
893 dst = drm_plane_state_dest(pstate);
894 if (!drm_rect_intersect(&clip, &dst)) {
895 DPU_ERROR("invalid vertical/horizontal destination\n");
896 DPU_ERROR("display: " DRM_RECT_FMT " plane: "
897 DRM_RECT_FMT "\n", DRM_RECT_ARG(&crtc_rect),
904 for (i = 1; i < SSPP_MAX; i++) {
905 if (pipe_staged[i]) {
906 dpu_plane_clear_multirect(pipe_staged[i]);
908 if (is_dpu_plane_virtual(pipe_staged[i]->plane)) {
910 "r1 only virt plane:%d not supported\n",
911 pipe_staged[i]->plane->base.id);
919 for (i = 0; i < cnt; i++) {
920 /* reset counts at every new blend stage */
921 if (pstates[i].stage != z_pos) {
924 z_pos = pstates[i].stage;
927 /* verify z_pos setting before using it */
928 if (z_pos >= DPU_STAGE_MAX - DPU_STAGE_0) {
929 DPU_ERROR("> %d plane stages assigned\n",
930 DPU_STAGE_MAX - DPU_STAGE_0);
933 } else if (pstates[i].drm_pstate->crtc_x < mixer_width) {
934 if (left_zpos_cnt == 2) {
935 DPU_ERROR("> 2 planes @ stage %d on left\n",
943 if (right_zpos_cnt == 2) {
944 DPU_ERROR("> 2 planes @ stage %d on right\n",
952 pstates[i].dpu_pstate->stage = z_pos + DPU_STAGE_0;
953 DPU_DEBUG("%s: zpos %d", dpu_crtc->name, z_pos);
956 for (i = 0; i < multirect_count; i++) {
957 if (dpu_plane_validate_multirect_v2(&multirect_plane[i])) {
959 "multirect validation failed for planes (%d - %d)\n",
960 multirect_plane[i].r0->plane->base.id,
961 multirect_plane[i].r1->plane->base.id);
967 atomic_inc(&_dpu_crtc_get_kms(crtc)->bandwidth_ref);
969 rc = dpu_core_perf_crtc_check(crtc, state);
971 DPU_ERROR("crtc%d failed performance check %d\n",
976 /* validate source split:
977 * use pstates sorted by stage to check planes on same stage
978 * we assume that all pipes are in source split so its valid to compare
979 * without taking into account left/right mixer placement
981 for (i = 1; i < cnt; i++) {
982 struct plane_state *prv_pstate, *cur_pstate;
983 struct drm_rect left_rect, right_rect;
984 int32_t left_pid, right_pid;
987 prv_pstate = &pstates[i - 1];
988 cur_pstate = &pstates[i];
989 if (prv_pstate->stage != cur_pstate->stage)
992 stage = cur_pstate->stage;
994 left_pid = prv_pstate->dpu_pstate->base.plane->base.id;
995 left_rect = drm_plane_state_dest(prv_pstate->drm_pstate);
997 right_pid = cur_pstate->dpu_pstate->base.plane->base.id;
998 right_rect = drm_plane_state_dest(cur_pstate->drm_pstate);
1000 if (right_rect.x1 < left_rect.x1) {
1001 swap(left_pid, right_pid);
1002 swap(left_rect, right_rect);
1006 * - planes are enumerated in pipe-priority order such that
1007 * planes with lower drm_id must be left-most in a shared
1008 * blend-stage when using source split.
1009 * - planes in source split must be contiguous in width
1010 * - planes in source split must have same dest yoff and height
1012 if (right_pid < left_pid) {
1014 "invalid src split cfg. priority mismatch. stage: %d left: %d right: %d\n",
1015 stage, left_pid, right_pid);
1018 } else if (right_rect.x1 != drm_rect_width(&left_rect)) {
1019 DPU_ERROR("non-contiguous coordinates for src split. "
1020 "stage: %d left: " DRM_RECT_FMT " right: "
1021 DRM_RECT_FMT "\n", stage,
1022 DRM_RECT_ARG(&left_rect),
1023 DRM_RECT_ARG(&right_rect));
1026 } else if (left_rect.y1 != right_rect.y1 ||
1027 drm_rect_height(&left_rect) != drm_rect_height(&right_rect)) {
1028 DPU_ERROR("source split at stage: %d. invalid "
1029 "yoff/height: left: " DRM_RECT_FMT " right: "
1030 DRM_RECT_FMT "\n", stage,
1031 DRM_RECT_ARG(&left_rect),
1032 DRM_RECT_ARG(&right_rect));
1043 int dpu_crtc_vblank(struct drm_crtc *crtc, bool en)
1045 struct dpu_crtc *dpu_crtc = to_dpu_crtc(crtc);
1046 struct drm_encoder *enc;
1048 trace_dpu_crtc_vblank(DRMID(&dpu_crtc->base), en, dpu_crtc);
1051 * Normally we would iterate through encoder_mask in crtc state to find
1052 * attached encoders. In this case, we might be disabling vblank _after_
1053 * encoder_mask has been cleared.
1055 * Instead, we "assign" a crtc to the encoder in enable and clear it in
1056 * disable (which is also after encoder_mask is cleared). So instead of
1057 * using encoder mask, we'll ask the encoder to toggle itself iff it's
1058 * currently assigned to our crtc.
1060 * Note also that this function cannot be called while crtc is disabled
1061 * since we use drm_crtc_vblank_on/off. So we don't need to worry
1062 * about the assigned crtcs being inconsistent with the current state
1063 * (which means no need to worry about modeset locks).
1065 list_for_each_entry(enc, &crtc->dev->mode_config.encoder_list, head) {
1066 trace_dpu_crtc_vblank_enable(DRMID(crtc), DRMID(enc), en,
1069 dpu_encoder_toggle_vblank_for_crtc(enc, crtc, en);
1075 #ifdef CONFIG_DEBUG_FS
1076 static int _dpu_debugfs_status_show(struct seq_file *s, void *data)
1078 struct dpu_crtc *dpu_crtc;
1079 struct dpu_plane_state *pstate = NULL;
1080 struct dpu_crtc_mixer *m;
1082 struct drm_crtc *crtc;
1083 struct drm_plane *plane;
1084 struct drm_display_mode *mode;
1085 struct drm_framebuffer *fb;
1086 struct drm_plane_state *state;
1087 struct dpu_crtc_state *cstate;
1091 dpu_crtc = s->private;
1092 crtc = &dpu_crtc->base;
1094 drm_modeset_lock_all(crtc->dev);
1095 cstate = to_dpu_crtc_state(crtc->state);
1097 mode = &crtc->state->adjusted_mode;
1098 out_width = mode->hdisplay / cstate->num_mixers;
1100 seq_printf(s, "crtc:%d width:%d height:%d\n", crtc->base.id,
1101 mode->hdisplay, mode->vdisplay);
1105 for (i = 0; i < cstate->num_mixers; ++i) {
1106 m = &cstate->mixers[i];
1107 seq_printf(s, "\tmixer:%d ctl:%d width:%d height:%d\n",
1108 m->hw_lm->idx - LM_0, m->lm_ctl->idx - CTL_0,
1109 out_width, mode->vdisplay);
1114 drm_atomic_crtc_for_each_plane(plane, crtc) {
1115 pstate = to_dpu_plane_state(plane->state);
1116 state = plane->state;
1118 if (!pstate || !state)
1121 seq_printf(s, "\tplane:%u stage:%d\n", plane->base.id,
1124 if (plane->state->fb) {
1125 fb = plane->state->fb;
1127 seq_printf(s, "\tfb:%d image format:%4.4s wxh:%ux%u ",
1128 fb->base.id, (char *) &fb->format->format,
1129 fb->width, fb->height);
1130 for (i = 0; i < ARRAY_SIZE(fb->format->cpp); ++i)
1131 seq_printf(s, "cpp[%d]:%u ",
1132 i, fb->format->cpp[i]);
1133 seq_puts(s, "\n\t");
1135 seq_printf(s, "modifier:%8llu ", fb->modifier);
1139 for (i = 0; i < ARRAY_SIZE(fb->pitches); i++)
1140 seq_printf(s, "pitches[%d]:%8u ", i,
1145 for (i = 0; i < ARRAY_SIZE(fb->offsets); i++)
1146 seq_printf(s, "offsets[%d]:%8u ", i,
1151 seq_printf(s, "\tsrc_x:%4d src_y:%4d src_w:%4d src_h:%4d\n",
1152 state->src_x, state->src_y, state->src_w, state->src_h);
1154 seq_printf(s, "\tdst x:%4d dst_y:%4d dst_w:%4d dst_h:%4d\n",
1155 state->crtc_x, state->crtc_y, state->crtc_w,
1157 seq_printf(s, "\tmultirect: mode: %d index: %d\n",
1158 pstate->multirect_mode, pstate->multirect_index);
1162 if (dpu_crtc->vblank_cb_count) {
1163 ktime_t diff = ktime_sub(ktime_get(), dpu_crtc->vblank_cb_time);
1164 s64 diff_ms = ktime_to_ms(diff);
1165 s64 fps = diff_ms ? div_s64(
1166 dpu_crtc->vblank_cb_count * 1000, diff_ms) : 0;
1169 "vblank fps:%lld count:%u total:%llums total_framecount:%llu\n",
1170 fps, dpu_crtc->vblank_cb_count,
1171 ktime_to_ms(diff), dpu_crtc->play_count);
1173 /* reset time & count for next measurement */
1174 dpu_crtc->vblank_cb_count = 0;
1175 dpu_crtc->vblank_cb_time = ktime_set(0, 0);
1178 drm_modeset_unlock_all(crtc->dev);
1183 DEFINE_SHOW_ATTRIBUTE(_dpu_debugfs_status);
1185 static int dpu_crtc_debugfs_state_show(struct seq_file *s, void *v)
1187 struct drm_crtc *crtc = (struct drm_crtc *) s->private;
1188 struct dpu_crtc *dpu_crtc = to_dpu_crtc(crtc);
1190 seq_printf(s, "client type: %d\n", dpu_crtc_get_client_type(crtc));
1191 seq_printf(s, "intf_mode: %d\n", dpu_crtc_get_intf_mode(crtc));
1192 seq_printf(s, "core_clk_rate: %llu\n",
1193 dpu_crtc->cur_perf.core_clk_rate);
1194 seq_printf(s, "bw_ctl: %llu\n", dpu_crtc->cur_perf.bw_ctl);
1195 seq_printf(s, "max_per_pipe_ib: %llu\n",
1196 dpu_crtc->cur_perf.max_per_pipe_ib);
1200 DEFINE_SHOW_ATTRIBUTE(dpu_crtc_debugfs_state);
1202 static int _dpu_crtc_init_debugfs(struct drm_crtc *crtc)
1204 struct dpu_crtc *dpu_crtc = to_dpu_crtc(crtc);
1206 dpu_crtc->debugfs_root = debugfs_create_dir(dpu_crtc->name,
1207 crtc->dev->primary->debugfs_root);
1209 debugfs_create_file("status", 0400,
1210 dpu_crtc->debugfs_root,
1211 dpu_crtc, &_dpu_debugfs_status_fops);
1212 debugfs_create_file("state", 0600,
1213 dpu_crtc->debugfs_root,
1215 &dpu_crtc_debugfs_state_fops);
1220 static int _dpu_crtc_init_debugfs(struct drm_crtc *crtc)
1224 #endif /* CONFIG_DEBUG_FS */
1226 static int dpu_crtc_late_register(struct drm_crtc *crtc)
1228 return _dpu_crtc_init_debugfs(crtc);
1231 static void dpu_crtc_early_unregister(struct drm_crtc *crtc)
1233 struct dpu_crtc *dpu_crtc = to_dpu_crtc(crtc);
1235 debugfs_remove_recursive(dpu_crtc->debugfs_root);
1238 static const struct drm_crtc_funcs dpu_crtc_funcs = {
1239 .set_config = drm_atomic_helper_set_config,
1240 .destroy = dpu_crtc_destroy,
1241 .page_flip = drm_atomic_helper_page_flip,
1242 .reset = dpu_crtc_reset,
1243 .atomic_duplicate_state = dpu_crtc_duplicate_state,
1244 .atomic_destroy_state = dpu_crtc_destroy_state,
1245 .late_register = dpu_crtc_late_register,
1246 .early_unregister = dpu_crtc_early_unregister,
1247 .enable_vblank = msm_crtc_enable_vblank,
1248 .disable_vblank = msm_crtc_disable_vblank,
1251 static const struct drm_crtc_helper_funcs dpu_crtc_helper_funcs = {
1252 .atomic_disable = dpu_crtc_disable,
1253 .atomic_enable = dpu_crtc_enable,
1254 .atomic_check = dpu_crtc_atomic_check,
1255 .atomic_begin = dpu_crtc_atomic_begin,
1256 .atomic_flush = dpu_crtc_atomic_flush,
1259 /* initialize crtc */
1260 struct drm_crtc *dpu_crtc_init(struct drm_device *dev, struct drm_plane *plane,
1261 struct drm_plane *cursor)
1263 struct drm_crtc *crtc = NULL;
1264 struct dpu_crtc *dpu_crtc = NULL;
1267 dpu_crtc = kzalloc(sizeof(*dpu_crtc), GFP_KERNEL);
1269 return ERR_PTR(-ENOMEM);
1271 crtc = &dpu_crtc->base;
1274 spin_lock_init(&dpu_crtc->spin_lock);
1275 atomic_set(&dpu_crtc->frame_pending, 0);
1277 init_completion(&dpu_crtc->frame_done_comp);
1279 INIT_LIST_HEAD(&dpu_crtc->frame_event_list);
1281 for (i = 0; i < ARRAY_SIZE(dpu_crtc->frame_events); i++) {
1282 INIT_LIST_HEAD(&dpu_crtc->frame_events[i].list);
1283 list_add(&dpu_crtc->frame_events[i].list,
1284 &dpu_crtc->frame_event_list);
1285 kthread_init_work(&dpu_crtc->frame_events[i].work,
1286 dpu_crtc_frame_event_work);
1289 drm_crtc_init_with_planes(dev, crtc, plane, cursor, &dpu_crtc_funcs,
1292 drm_crtc_helper_add(crtc, &dpu_crtc_helper_funcs);
1294 drm_crtc_enable_color_mgmt(crtc, 0, true, 0);
1296 /* save user friendly CRTC name for later */
1297 snprintf(dpu_crtc->name, DPU_CRTC_NAME_SIZE, "crtc%u", crtc->base.id);
1299 /* initialize event handling */
1300 spin_lock_init(&dpu_crtc->event_lock);
1302 DPU_DEBUG("%s: successfully initialized crtc\n", dpu_crtc->name);