1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2014-2015 The Linux Foundation. All rights reserved.
4 * Copyright (C) 2013 Red Hat
5 * Author: Rob Clark <robdclark@gmail.com>
8 #include <drm/drm_damage_helper.h>
9 #include <drm/drm_fourcc.h>
10 #include <drm/drm_print.h>
15 struct drm_plane base
;
20 #define to_mdp5_plane(x) container_of(x, struct mdp5_plane, base)
22 static int mdp5_plane_mode_set(struct drm_plane
*plane
,
23 struct drm_crtc
*crtc
, struct drm_framebuffer
*fb
,
24 struct drm_rect
*src
, struct drm_rect
*dest
);
26 static struct mdp5_kms
*get_kms(struct drm_plane
*plane
)
28 struct msm_drm_private
*priv
= plane
->dev
->dev_private
;
29 return to_mdp5_kms(to_mdp_kms(priv
->kms
));
32 static bool plane_enabled(struct drm_plane_state
*state
)
34 return state
->visible
;
37 static void mdp5_plane_destroy(struct drm_plane
*plane
)
39 struct mdp5_plane
*mdp5_plane
= to_mdp5_plane(plane
);
41 drm_plane_cleanup(plane
);
46 static void mdp5_plane_install_rotation_property(struct drm_device
*dev
,
47 struct drm_plane
*plane
)
49 drm_plane_create_rotation_property(plane
,
57 /* helper to install properties which are common to planes and crtcs */
58 static void mdp5_plane_install_properties(struct drm_plane
*plane
,
59 struct drm_mode_object
*obj
)
61 struct drm_device
*dev
= plane
->dev
;
62 struct msm_drm_private
*dev_priv
= dev
->dev_private
;
63 struct drm_property
*prop
;
65 #define INSTALL_PROPERTY(name, NAME, init_val, fnc, ...) do { \
66 prop = dev_priv->plane_property[PLANE_PROP_##NAME]; \
68 prop = drm_property_##fnc(dev, 0, #name, \
72 "Create property %s failed\n", \
76 dev_priv->plane_property[PLANE_PROP_##NAME] = prop; \
78 drm_object_attach_property(&plane->base, prop, init_val); \
81 #define INSTALL_RANGE_PROPERTY(name, NAME, min, max, init_val) \
82 INSTALL_PROPERTY(name, NAME, init_val, \
83 create_range, min, max)
85 #define INSTALL_ENUM_PROPERTY(name, NAME, init_val) \
86 INSTALL_PROPERTY(name, NAME, init_val, \
87 create_enum, name##_prop_enum_list, \
88 ARRAY_SIZE(name##_prop_enum_list))
90 INSTALL_RANGE_PROPERTY(zpos
, ZPOS
, 1, 255, 1);
92 mdp5_plane_install_rotation_property(dev
, plane
);
94 #undef INSTALL_RANGE_PROPERTY
95 #undef INSTALL_ENUM_PROPERTY
96 #undef INSTALL_PROPERTY
99 static int mdp5_plane_atomic_set_property(struct drm_plane
*plane
,
100 struct drm_plane_state
*state
, struct drm_property
*property
,
103 struct drm_device
*dev
= plane
->dev
;
104 struct mdp5_plane_state
*pstate
;
105 struct msm_drm_private
*dev_priv
= dev
->dev_private
;
108 pstate
= to_mdp5_plane_state(state
);
110 #define SET_PROPERTY(name, NAME, type) do { \
111 if (dev_priv->plane_property[PLANE_PROP_##NAME] == property) { \
112 pstate->name = (type)val; \
113 DBG("Set property %s %d", #name, (type)val); \
118 SET_PROPERTY(zpos
, ZPOS
, uint8_t);
120 DRM_DEV_ERROR(dev
->dev
, "Invalid property\n");
127 static int mdp5_plane_atomic_get_property(struct drm_plane
*plane
,
128 const struct drm_plane_state
*state
,
129 struct drm_property
*property
, uint64_t *val
)
131 struct drm_device
*dev
= plane
->dev
;
132 struct mdp5_plane_state
*pstate
;
133 struct msm_drm_private
*dev_priv
= dev
->dev_private
;
136 pstate
= to_mdp5_plane_state(state
);
138 #define GET_PROPERTY(name, NAME, type) do { \
139 if (dev_priv->plane_property[PLANE_PROP_##NAME] == property) { \
140 *val = pstate->name; \
141 DBG("Get property %s %lld", #name, *val); \
146 GET_PROPERTY(zpos
, ZPOS
, uint8_t);
148 DRM_DEV_ERROR(dev
->dev
, "Invalid property\n");
156 mdp5_plane_atomic_print_state(struct drm_printer
*p
,
157 const struct drm_plane_state
*state
)
159 struct mdp5_plane_state
*pstate
= to_mdp5_plane_state(state
);
160 struct mdp5_kms
*mdp5_kms
= get_kms(state
->plane
);
162 drm_printf(p
, "\thwpipe=%s\n", pstate
->hwpipe
?
163 pstate
->hwpipe
->name
: "(null)");
164 if (mdp5_kms
->caps
& MDP_CAP_SRC_SPLIT
)
165 drm_printf(p
, "\tright-hwpipe=%s\n",
166 pstate
->r_hwpipe
? pstate
->r_hwpipe
->name
:
168 drm_printf(p
, "\tpremultiplied=%u\n", pstate
->premultiplied
);
169 drm_printf(p
, "\tzpos=%u\n", pstate
->zpos
);
170 drm_printf(p
, "\talpha=%u\n", pstate
->alpha
);
171 drm_printf(p
, "\tstage=%s\n", stage2name(pstate
->stage
));
174 static void mdp5_plane_reset(struct drm_plane
*plane
)
176 struct mdp5_plane_state
*mdp5_state
;
178 if (plane
->state
&& plane
->state
->fb
)
179 drm_framebuffer_put(plane
->state
->fb
);
181 kfree(to_mdp5_plane_state(plane
->state
));
182 mdp5_state
= kzalloc(sizeof(*mdp5_state
), GFP_KERNEL
);
184 /* assign default blend parameters */
185 mdp5_state
->alpha
= 255;
186 mdp5_state
->premultiplied
= 0;
188 if (plane
->type
== DRM_PLANE_TYPE_PRIMARY
)
189 mdp5_state
->zpos
= STAGE_BASE
;
191 mdp5_state
->zpos
= STAGE0
+ drm_plane_index(plane
);
193 mdp5_state
->base
.plane
= plane
;
195 plane
->state
= &mdp5_state
->base
;
198 static struct drm_plane_state
*
199 mdp5_plane_duplicate_state(struct drm_plane
*plane
)
201 struct mdp5_plane_state
*mdp5_state
;
203 if (WARN_ON(!plane
->state
))
206 mdp5_state
= kmemdup(to_mdp5_plane_state(plane
->state
),
207 sizeof(*mdp5_state
), GFP_KERNEL
);
211 __drm_atomic_helper_plane_duplicate_state(plane
, &mdp5_state
->base
);
213 return &mdp5_state
->base
;
216 static void mdp5_plane_destroy_state(struct drm_plane
*plane
,
217 struct drm_plane_state
*state
)
219 struct mdp5_plane_state
*pstate
= to_mdp5_plane_state(state
);
222 drm_framebuffer_put(state
->fb
);
227 static const struct drm_plane_funcs mdp5_plane_funcs
= {
228 .update_plane
= drm_atomic_helper_update_plane
,
229 .disable_plane
= drm_atomic_helper_disable_plane
,
230 .destroy
= mdp5_plane_destroy
,
231 .atomic_set_property
= mdp5_plane_atomic_set_property
,
232 .atomic_get_property
= mdp5_plane_atomic_get_property
,
233 .reset
= mdp5_plane_reset
,
234 .atomic_duplicate_state
= mdp5_plane_duplicate_state
,
235 .atomic_destroy_state
= mdp5_plane_destroy_state
,
236 .atomic_print_state
= mdp5_plane_atomic_print_state
,
239 static void mdp5_plane_cleanup_fb(struct drm_plane
*plane
,
240 struct drm_plane_state
*old_state
)
242 struct mdp5_kms
*mdp5_kms
= get_kms(plane
);
243 struct msm_kms
*kms
= &mdp5_kms
->base
.base
;
244 struct drm_framebuffer
*fb
= old_state
->fb
;
249 DBG("%s: cleanup: FB[%u]", plane
->name
, fb
->base
.id
);
250 msm_framebuffer_cleanup(fb
, kms
->aspace
);
253 static int mdp5_plane_atomic_check_with_state(struct drm_crtc_state
*crtc_state
,
254 struct drm_plane_state
*state
)
256 struct mdp5_plane_state
*mdp5_state
= to_mdp5_plane_state(state
);
257 struct drm_plane
*plane
= state
->plane
;
258 struct drm_plane_state
*old_state
= plane
->state
;
259 struct mdp5_cfg
*config
= mdp5_cfg_get_config(get_kms(plane
)->cfg
);
260 bool new_hwpipe
= false;
261 bool need_right_hwpipe
= false;
262 uint32_t max_width
, max_height
;
263 bool out_of_bounds
= false;
265 int min_scale
, max_scale
;
268 DBG("%s: check (%d -> %d)", plane
->name
,
269 plane_enabled(old_state
), plane_enabled(state
));
271 max_width
= config
->hw
->lm
.max_width
<< 16;
272 max_height
= config
->hw
->lm
.max_height
<< 16;
274 /* Make sure source dimensions are within bounds. */
275 if (state
->src_h
> max_height
)
276 out_of_bounds
= true;
278 if (state
->src_w
> max_width
) {
279 /* If source split is supported, we can go up to 2x
280 * the max LM width, but we'd need to stage another
281 * hwpipe to the right LM. So, the drm_plane would
282 * consist of 2 hwpipes.
284 if (config
->hw
->mdp
.caps
& MDP_CAP_SRC_SPLIT
&&
285 (state
->src_w
<= 2 * max_width
))
286 need_right_hwpipe
= true;
288 out_of_bounds
= true;
292 struct drm_rect src
= drm_plane_state_src(state
);
293 DBG("Invalid source size "DRM_RECT_FP_FMT
,
294 DRM_RECT_FP_ARG(&src
));
298 min_scale
= FRAC_16_16(1, 8);
299 max_scale
= FRAC_16_16(8, 1);
301 ret
= drm_atomic_helper_check_plane_state(state
, crtc_state
,
302 min_scale
, max_scale
,
307 if (plane_enabled(state
)) {
308 unsigned int rotation
;
309 const struct mdp_format
*format
;
310 struct mdp5_kms
*mdp5_kms
= get_kms(plane
);
313 format
= to_mdp_format(msm_framebuffer_format(state
->fb
));
314 if (MDP_FORMAT_IS_YUV(format
))
315 caps
|= MDP_PIPE_CAP_SCALE
| MDP_PIPE_CAP_CSC
;
317 if (((state
->src_w
>> 16) != state
->crtc_w
) ||
318 ((state
->src_h
>> 16) != state
->crtc_h
))
319 caps
|= MDP_PIPE_CAP_SCALE
;
321 rotation
= drm_rotation_simplify(state
->rotation
,
326 if (rotation
& DRM_MODE_REFLECT_X
)
327 caps
|= MDP_PIPE_CAP_HFLIP
;
329 if (rotation
& DRM_MODE_REFLECT_Y
)
330 caps
|= MDP_PIPE_CAP_VFLIP
;
332 if (plane
->type
== DRM_PLANE_TYPE_CURSOR
)
333 caps
|= MDP_PIPE_CAP_CURSOR
;
335 /* (re)allocate hw pipe if we don't have one or caps-mismatch: */
336 if (!mdp5_state
->hwpipe
|| (caps
& ~mdp5_state
->hwpipe
->caps
))
340 * (re)allocte hw pipe if we're either requesting for 2 hw pipes
341 * or we're switching from 2 hw pipes to 1 hw pipe because the
342 * new src_w can be supported by 1 hw pipe itself.
344 if ((need_right_hwpipe
&& !mdp5_state
->r_hwpipe
) ||
345 (!need_right_hwpipe
&& mdp5_state
->r_hwpipe
))
349 const struct mdp_format
*format
=
350 to_mdp_format(msm_framebuffer_format(state
->fb
));
352 blkcfg
= mdp5_smp_calculate(mdp5_kms
->smp
, format
,
353 state
->src_w
>> 16, false);
355 if (mdp5_state
->hwpipe
&& (mdp5_state
->hwpipe
->blkcfg
!= blkcfg
))
359 /* (re)assign hwpipe if needed, otherwise keep old one: */
361 /* TODO maybe we want to re-assign hwpipe sometimes
362 * in cases when we no-longer need some caps to make
363 * it available for other planes?
365 struct mdp5_hw_pipe
*old_hwpipe
= mdp5_state
->hwpipe
;
366 struct mdp5_hw_pipe
*old_right_hwpipe
=
367 mdp5_state
->r_hwpipe
;
368 struct mdp5_hw_pipe
*new_hwpipe
= NULL
;
369 struct mdp5_hw_pipe
*new_right_hwpipe
= NULL
;
371 ret
= mdp5_pipe_assign(state
->state
, plane
, caps
,
374 &new_right_hwpipe
: NULL
);
376 DBG("%s: failed to assign hwpipe(s)!",
381 mdp5_state
->hwpipe
= new_hwpipe
;
382 if (need_right_hwpipe
)
383 mdp5_state
->r_hwpipe
= new_right_hwpipe
;
386 * set it to NULL so that the driver knows we
387 * don't have a right hwpipe when committing a
390 mdp5_state
->r_hwpipe
= NULL
;
393 mdp5_pipe_release(state
->state
, old_hwpipe
);
394 mdp5_pipe_release(state
->state
, old_right_hwpipe
);
397 mdp5_pipe_release(state
->state
, mdp5_state
->hwpipe
);
398 mdp5_pipe_release(state
->state
, mdp5_state
->r_hwpipe
);
399 mdp5_state
->hwpipe
= mdp5_state
->r_hwpipe
= NULL
;
405 static int mdp5_plane_atomic_check(struct drm_plane
*plane
,
406 struct drm_plane_state
*state
)
408 struct drm_crtc
*crtc
;
409 struct drm_crtc_state
*crtc_state
;
411 crtc
= state
->crtc
? state
->crtc
: plane
->state
->crtc
;
415 crtc_state
= drm_atomic_get_existing_crtc_state(state
->state
, crtc
);
416 if (WARN_ON(!crtc_state
))
419 return mdp5_plane_atomic_check_with_state(crtc_state
, state
);
422 static void mdp5_plane_atomic_update(struct drm_plane
*plane
,
423 struct drm_plane_state
*old_state
)
425 struct drm_plane_state
*state
= plane
->state
;
427 DBG("%s: update", plane
->name
);
429 if (plane_enabled(state
)) {
432 ret
= mdp5_plane_mode_set(plane
,
433 state
->crtc
, state
->fb
,
434 &state
->src
, &state
->dst
);
435 /* atomic_check should have ensured that this doesn't fail */
440 static int mdp5_plane_atomic_async_check(struct drm_plane
*plane
,
441 struct drm_plane_state
*state
)
443 struct mdp5_plane_state
*mdp5_state
= to_mdp5_plane_state(state
);
444 struct drm_crtc_state
*crtc_state
;
445 int min_scale
, max_scale
;
448 crtc_state
= drm_atomic_get_existing_crtc_state(state
->state
,
450 if (WARN_ON(!crtc_state
))
453 if (!crtc_state
->active
)
456 mdp5_state
= to_mdp5_plane_state(state
);
458 /* don't use fast path if we don't have a hwpipe allocated yet */
459 if (!mdp5_state
->hwpipe
)
462 /* only allow changing of position(crtc x/y or src x/y) in fast path */
463 if (plane
->state
->crtc
!= state
->crtc
||
464 plane
->state
->src_w
!= state
->src_w
||
465 plane
->state
->src_h
!= state
->src_h
||
466 plane
->state
->crtc_w
!= state
->crtc_w
||
467 plane
->state
->crtc_h
!= state
->crtc_h
||
469 plane
->state
->fb
!= state
->fb
)
472 min_scale
= FRAC_16_16(1, 8);
473 max_scale
= FRAC_16_16(8, 1);
475 ret
= drm_atomic_helper_check_plane_state(state
, crtc_state
,
476 min_scale
, max_scale
,
482 * if the visibility of the plane changes (i.e, if the cursor is
483 * clipped out completely, we can't take the async path because
484 * we need to stage/unstage the plane from the Layer Mixer(s). We
485 * also assign/unassign the hwpipe(s) tied to the plane. We avoid
486 * taking the fast path for both these reasons.
488 if (state
->visible
!= plane
->state
->visible
)
494 static void mdp5_plane_atomic_async_update(struct drm_plane
*plane
,
495 struct drm_plane_state
*new_state
)
497 struct drm_framebuffer
*old_fb
= plane
->state
->fb
;
499 plane
->state
->src_x
= new_state
->src_x
;
500 plane
->state
->src_y
= new_state
->src_y
;
501 plane
->state
->crtc_x
= new_state
->crtc_x
;
502 plane
->state
->crtc_y
= new_state
->crtc_y
;
504 if (plane_enabled(new_state
)) {
505 struct mdp5_ctl
*ctl
;
506 struct mdp5_pipeline
*pipeline
=
507 mdp5_crtc_get_pipeline(new_state
->crtc
);
510 ret
= mdp5_plane_mode_set(plane
, new_state
->crtc
, new_state
->fb
,
511 &new_state
->src
, &new_state
->dst
);
514 ctl
= mdp5_crtc_get_ctl(new_state
->crtc
);
516 mdp5_ctl_commit(ctl
, pipeline
, mdp5_plane_get_flush(plane
), true);
519 *to_mdp5_plane_state(plane
->state
) =
520 *to_mdp5_plane_state(new_state
);
522 new_state
->fb
= old_fb
;
525 static const struct drm_plane_helper_funcs mdp5_plane_helper_funcs
= {
526 .prepare_fb
= msm_atomic_prepare_fb
,
527 .cleanup_fb
= mdp5_plane_cleanup_fb
,
528 .atomic_check
= mdp5_plane_atomic_check
,
529 .atomic_update
= mdp5_plane_atomic_update
,
530 .atomic_async_check
= mdp5_plane_atomic_async_check
,
531 .atomic_async_update
= mdp5_plane_atomic_async_update
,
534 static void set_scanout_locked(struct mdp5_kms
*mdp5_kms
,
536 struct drm_framebuffer
*fb
)
538 struct msm_kms
*kms
= &mdp5_kms
->base
.base
;
540 mdp5_write(mdp5_kms
, REG_MDP5_PIPE_SRC_STRIDE_A(pipe
),
541 MDP5_PIPE_SRC_STRIDE_A_P0(fb
->pitches
[0]) |
542 MDP5_PIPE_SRC_STRIDE_A_P1(fb
->pitches
[1]));
544 mdp5_write(mdp5_kms
, REG_MDP5_PIPE_SRC_STRIDE_B(pipe
),
545 MDP5_PIPE_SRC_STRIDE_B_P2(fb
->pitches
[2]) |
546 MDP5_PIPE_SRC_STRIDE_B_P3(fb
->pitches
[3]));
548 mdp5_write(mdp5_kms
, REG_MDP5_PIPE_SRC0_ADDR(pipe
),
549 msm_framebuffer_iova(fb
, kms
->aspace
, 0));
550 mdp5_write(mdp5_kms
, REG_MDP5_PIPE_SRC1_ADDR(pipe
),
551 msm_framebuffer_iova(fb
, kms
->aspace
, 1));
552 mdp5_write(mdp5_kms
, REG_MDP5_PIPE_SRC2_ADDR(pipe
),
553 msm_framebuffer_iova(fb
, kms
->aspace
, 2));
554 mdp5_write(mdp5_kms
, REG_MDP5_PIPE_SRC3_ADDR(pipe
),
555 msm_framebuffer_iova(fb
, kms
->aspace
, 3));
558 /* Note: mdp5_plane->pipe_lock must be locked */
559 static void csc_disable(struct mdp5_kms
*mdp5_kms
, enum mdp5_pipe pipe
)
561 uint32_t value
= mdp5_read(mdp5_kms
, REG_MDP5_PIPE_OP_MODE(pipe
)) &
562 ~MDP5_PIPE_OP_MODE_CSC_1_EN
;
564 mdp5_write(mdp5_kms
, REG_MDP5_PIPE_OP_MODE(pipe
), value
);
567 /* Note: mdp5_plane->pipe_lock must be locked */
568 static void csc_enable(struct mdp5_kms
*mdp5_kms
, enum mdp5_pipe pipe
,
571 uint32_t i
, mode
= 0; /* RGB, no CSC */
577 if ((csc
->type
== CSC_YUV2RGB
) || (CSC_YUV2YUV
== csc
->type
))
578 mode
|= MDP5_PIPE_OP_MODE_CSC_SRC_DATA_FORMAT(DATA_FORMAT_YUV
);
579 if ((csc
->type
== CSC_RGB2YUV
) || (CSC_YUV2YUV
== csc
->type
))
580 mode
|= MDP5_PIPE_OP_MODE_CSC_DST_DATA_FORMAT(DATA_FORMAT_YUV
);
581 mode
|= MDP5_PIPE_OP_MODE_CSC_1_EN
;
582 mdp5_write(mdp5_kms
, REG_MDP5_PIPE_OP_MODE(pipe
), mode
);
584 matrix
= csc
->matrix
;
585 mdp5_write(mdp5_kms
, REG_MDP5_PIPE_CSC_1_MATRIX_COEFF_0(pipe
),
586 MDP5_PIPE_CSC_1_MATRIX_COEFF_0_COEFF_11(matrix
[0]) |
587 MDP5_PIPE_CSC_1_MATRIX_COEFF_0_COEFF_12(matrix
[1]));
588 mdp5_write(mdp5_kms
, REG_MDP5_PIPE_CSC_1_MATRIX_COEFF_1(pipe
),
589 MDP5_PIPE_CSC_1_MATRIX_COEFF_1_COEFF_13(matrix
[2]) |
590 MDP5_PIPE_CSC_1_MATRIX_COEFF_1_COEFF_21(matrix
[3]));
591 mdp5_write(mdp5_kms
, REG_MDP5_PIPE_CSC_1_MATRIX_COEFF_2(pipe
),
592 MDP5_PIPE_CSC_1_MATRIX_COEFF_2_COEFF_22(matrix
[4]) |
593 MDP5_PIPE_CSC_1_MATRIX_COEFF_2_COEFF_23(matrix
[5]));
594 mdp5_write(mdp5_kms
, REG_MDP5_PIPE_CSC_1_MATRIX_COEFF_3(pipe
),
595 MDP5_PIPE_CSC_1_MATRIX_COEFF_3_COEFF_31(matrix
[6]) |
596 MDP5_PIPE_CSC_1_MATRIX_COEFF_3_COEFF_32(matrix
[7]));
597 mdp5_write(mdp5_kms
, REG_MDP5_PIPE_CSC_1_MATRIX_COEFF_4(pipe
),
598 MDP5_PIPE_CSC_1_MATRIX_COEFF_4_COEFF_33(matrix
[8]));
600 for (i
= 0; i
< ARRAY_SIZE(csc
->pre_bias
); i
++) {
601 uint32_t *pre_clamp
= csc
->pre_clamp
;
602 uint32_t *post_clamp
= csc
->post_clamp
;
604 mdp5_write(mdp5_kms
, REG_MDP5_PIPE_CSC_1_PRE_CLAMP(pipe
, i
),
605 MDP5_PIPE_CSC_1_PRE_CLAMP_REG_HIGH(pre_clamp
[2*i
+1]) |
606 MDP5_PIPE_CSC_1_PRE_CLAMP_REG_LOW(pre_clamp
[2*i
]));
608 mdp5_write(mdp5_kms
, REG_MDP5_PIPE_CSC_1_POST_CLAMP(pipe
, i
),
609 MDP5_PIPE_CSC_1_POST_CLAMP_REG_HIGH(post_clamp
[2*i
+1]) |
610 MDP5_PIPE_CSC_1_POST_CLAMP_REG_LOW(post_clamp
[2*i
]));
612 mdp5_write(mdp5_kms
, REG_MDP5_PIPE_CSC_1_PRE_BIAS(pipe
, i
),
613 MDP5_PIPE_CSC_1_PRE_BIAS_REG_VALUE(csc
->pre_bias
[i
]));
615 mdp5_write(mdp5_kms
, REG_MDP5_PIPE_CSC_1_POST_BIAS(pipe
, i
),
616 MDP5_PIPE_CSC_1_POST_BIAS_REG_VALUE(csc
->post_bias
[i
]));
620 #define PHASE_STEP_SHIFT 21
621 #define DOWN_SCALE_RATIO_MAX 32 /* 2^(26-21) */
623 static int calc_phase_step(uint32_t src
, uint32_t dst
, uint32_t *out_phase
)
627 if (src
== 0 || dst
== 0)
631 * PHASE_STEP_X/Y is coded on 26 bits (25:0),
632 * where 2^21 represents the unity "1" in fixed-point hardware design.
633 * This leaves 5 bits for the integer part (downscale case):
634 * -> maximum downscale ratio = 0b1_1111 = 31
636 if (src
> (dst
* DOWN_SCALE_RATIO_MAX
))
639 unit
= 1 << PHASE_STEP_SHIFT
;
640 *out_phase
= mult_frac(unit
, src
, dst
);
645 static int calc_scalex_steps(struct drm_plane
*plane
,
646 uint32_t pixel_format
, uint32_t src
, uint32_t dest
,
647 uint32_t phasex_steps
[COMP_MAX
])
649 const struct drm_format_info
*info
= drm_format_info(pixel_format
);
650 struct mdp5_kms
*mdp5_kms
= get_kms(plane
);
651 struct device
*dev
= mdp5_kms
->dev
->dev
;
652 uint32_t phasex_step
;
655 ret
= calc_phase_step(src
, dest
, &phasex_step
);
657 DRM_DEV_ERROR(dev
, "X scaling (%d->%d) failed: %d\n", src
, dest
, ret
);
661 phasex_steps
[COMP_0
] = phasex_step
;
662 phasex_steps
[COMP_3
] = phasex_step
;
663 phasex_steps
[COMP_1_2
] = phasex_step
/ info
->hsub
;
668 static int calc_scaley_steps(struct drm_plane
*plane
,
669 uint32_t pixel_format
, uint32_t src
, uint32_t dest
,
670 uint32_t phasey_steps
[COMP_MAX
])
672 const struct drm_format_info
*info
= drm_format_info(pixel_format
);
673 struct mdp5_kms
*mdp5_kms
= get_kms(plane
);
674 struct device
*dev
= mdp5_kms
->dev
->dev
;
675 uint32_t phasey_step
;
678 ret
= calc_phase_step(src
, dest
, &phasey_step
);
680 DRM_DEV_ERROR(dev
, "Y scaling (%d->%d) failed: %d\n", src
, dest
, ret
);
684 phasey_steps
[COMP_0
] = phasey_step
;
685 phasey_steps
[COMP_3
] = phasey_step
;
686 phasey_steps
[COMP_1_2
] = phasey_step
/ info
->vsub
;
691 static uint32_t get_scale_config(const struct mdp_format
*format
,
692 uint32_t src
, uint32_t dst
, bool horz
)
694 const struct drm_format_info
*info
= drm_format_info(format
->base
.pixel_format
);
695 bool scaling
= format
->is_yuv
? true : (src
!= dst
);
697 uint32_t ya_filter
, uv_filter
;
698 bool yuv
= format
->is_yuv
;
704 sub
= horz
? info
->hsub
: info
->vsub
;
705 uv_filter
= ((src
/ sub
) <= dst
) ?
706 SCALE_FILTER_BIL
: SCALE_FILTER_PCMN
;
708 ya_filter
= (src
<= dst
) ? SCALE_FILTER_BIL
: SCALE_FILTER_PCMN
;
711 return MDP5_PIPE_SCALE_CONFIG_SCALEX_EN
|
712 MDP5_PIPE_SCALE_CONFIG_SCALEX_FILTER_COMP_0(ya_filter
) |
713 MDP5_PIPE_SCALE_CONFIG_SCALEX_FILTER_COMP_3(ya_filter
) |
714 COND(yuv
, MDP5_PIPE_SCALE_CONFIG_SCALEX_FILTER_COMP_1_2(uv_filter
));
716 return MDP5_PIPE_SCALE_CONFIG_SCALEY_EN
|
717 MDP5_PIPE_SCALE_CONFIG_SCALEY_FILTER_COMP_0(ya_filter
) |
718 MDP5_PIPE_SCALE_CONFIG_SCALEY_FILTER_COMP_3(ya_filter
) |
719 COND(yuv
, MDP5_PIPE_SCALE_CONFIG_SCALEY_FILTER_COMP_1_2(uv_filter
));
722 static void calc_pixel_ext(const struct mdp_format
*format
,
723 uint32_t src
, uint32_t dst
, uint32_t phase_step
[2],
724 int pix_ext_edge1
[COMP_MAX
], int pix_ext_edge2
[COMP_MAX
],
727 bool scaling
= format
->is_yuv
? true : (src
!= dst
);
732 * We assume here that:
733 * 1. PCMN filter is used for downscale
734 * 2. bilinear filter is used for upscale
735 * 3. we are in a single pipe configuration
738 for (i
= 0; i
< COMP_MAX
; i
++) {
739 pix_ext_edge1
[i
] = 0;
740 pix_ext_edge2
[i
] = scaling
? 1 : 0;
744 static void mdp5_write_pixel_ext(struct mdp5_kms
*mdp5_kms
, enum mdp5_pipe pipe
,
745 const struct mdp_format
*format
,
746 uint32_t src_w
, int pe_left
[COMP_MAX
], int pe_right
[COMP_MAX
],
747 uint32_t src_h
, int pe_top
[COMP_MAX
], int pe_bottom
[COMP_MAX
])
749 const struct drm_format_info
*info
= drm_format_info(format
->base
.pixel_format
);
750 uint32_t lr
, tb
, req
;
753 for (i
= 0; i
< COMP_MAX
; i
++) {
754 uint32_t roi_w
= src_w
;
755 uint32_t roi_h
= src_h
;
757 if (format
->is_yuv
&& i
== COMP_1_2
) {
762 lr
= (pe_left
[i
] >= 0) ?
763 MDP5_PIPE_SW_PIX_EXT_LR_LEFT_RPT(pe_left
[i
]) :
764 MDP5_PIPE_SW_PIX_EXT_LR_LEFT_OVF(pe_left
[i
]);
766 lr
|= (pe_right
[i
] >= 0) ?
767 MDP5_PIPE_SW_PIX_EXT_LR_RIGHT_RPT(pe_right
[i
]) :
768 MDP5_PIPE_SW_PIX_EXT_LR_RIGHT_OVF(pe_right
[i
]);
770 tb
= (pe_top
[i
] >= 0) ?
771 MDP5_PIPE_SW_PIX_EXT_TB_TOP_RPT(pe_top
[i
]) :
772 MDP5_PIPE_SW_PIX_EXT_TB_TOP_OVF(pe_top
[i
]);
774 tb
|= (pe_bottom
[i
] >= 0) ?
775 MDP5_PIPE_SW_PIX_EXT_TB_BOTTOM_RPT(pe_bottom
[i
]) :
776 MDP5_PIPE_SW_PIX_EXT_TB_BOTTOM_OVF(pe_bottom
[i
]);
778 req
= MDP5_PIPE_SW_PIX_EXT_REQ_PIXELS_LEFT_RIGHT(roi_w
+
779 pe_left
[i
] + pe_right
[i
]);
781 req
|= MDP5_PIPE_SW_PIX_EXT_REQ_PIXELS_TOP_BOTTOM(roi_h
+
782 pe_top
[i
] + pe_bottom
[i
]);
784 mdp5_write(mdp5_kms
, REG_MDP5_PIPE_SW_PIX_EXT_LR(pipe
, i
), lr
);
785 mdp5_write(mdp5_kms
, REG_MDP5_PIPE_SW_PIX_EXT_TB(pipe
, i
), tb
);
786 mdp5_write(mdp5_kms
, REG_MDP5_PIPE_SW_PIX_EXT_REQ_PIXELS(pipe
, i
), req
);
788 DBG("comp-%d (L/R): rpt=%d/%d, ovf=%d/%d, req=%d", i
,
789 FIELD(lr
, MDP5_PIPE_SW_PIX_EXT_LR_LEFT_RPT
),
790 FIELD(lr
, MDP5_PIPE_SW_PIX_EXT_LR_RIGHT_RPT
),
791 FIELD(lr
, MDP5_PIPE_SW_PIX_EXT_LR_LEFT_OVF
),
792 FIELD(lr
, MDP5_PIPE_SW_PIX_EXT_LR_RIGHT_OVF
),
793 FIELD(req
, MDP5_PIPE_SW_PIX_EXT_REQ_PIXELS_LEFT_RIGHT
));
795 DBG("comp-%d (T/B): rpt=%d/%d, ovf=%d/%d, req=%d", i
,
796 FIELD(tb
, MDP5_PIPE_SW_PIX_EXT_TB_TOP_RPT
),
797 FIELD(tb
, MDP5_PIPE_SW_PIX_EXT_TB_BOTTOM_RPT
),
798 FIELD(tb
, MDP5_PIPE_SW_PIX_EXT_TB_TOP_OVF
),
799 FIELD(tb
, MDP5_PIPE_SW_PIX_EXT_TB_BOTTOM_OVF
),
800 FIELD(req
, MDP5_PIPE_SW_PIX_EXT_REQ_PIXELS_TOP_BOTTOM
));
808 int bottom
[COMP_MAX
];
816 static void mdp5_hwpipe_mode_set(struct mdp5_kms
*mdp5_kms
,
817 struct mdp5_hw_pipe
*hwpipe
,
818 struct drm_framebuffer
*fb
,
819 struct phase_step
*step
,
820 struct pixel_ext
*pe
,
821 u32 scale_config
, u32 hdecm
, u32 vdecm
,
822 bool hflip
, bool vflip
,
823 int crtc_x
, int crtc_y
,
824 unsigned int crtc_w
, unsigned int crtc_h
,
825 u32 src_img_w
, u32 src_img_h
,
826 u32 src_x
, u32 src_y
,
827 u32 src_w
, u32 src_h
)
829 enum mdp5_pipe pipe
= hwpipe
->pipe
;
830 bool has_pe
= hwpipe
->caps
& MDP_PIPE_CAP_SW_PIX_EXT
;
831 const struct mdp_format
*format
=
832 to_mdp_format(msm_framebuffer_format(fb
));
834 mdp5_write(mdp5_kms
, REG_MDP5_PIPE_SRC_IMG_SIZE(pipe
),
835 MDP5_PIPE_SRC_IMG_SIZE_WIDTH(src_img_w
) |
836 MDP5_PIPE_SRC_IMG_SIZE_HEIGHT(src_img_h
));
838 mdp5_write(mdp5_kms
, REG_MDP5_PIPE_SRC_SIZE(pipe
),
839 MDP5_PIPE_SRC_SIZE_WIDTH(src_w
) |
840 MDP5_PIPE_SRC_SIZE_HEIGHT(src_h
));
842 mdp5_write(mdp5_kms
, REG_MDP5_PIPE_SRC_XY(pipe
),
843 MDP5_PIPE_SRC_XY_X(src_x
) |
844 MDP5_PIPE_SRC_XY_Y(src_y
));
846 mdp5_write(mdp5_kms
, REG_MDP5_PIPE_OUT_SIZE(pipe
),
847 MDP5_PIPE_OUT_SIZE_WIDTH(crtc_w
) |
848 MDP5_PIPE_OUT_SIZE_HEIGHT(crtc_h
));
850 mdp5_write(mdp5_kms
, REG_MDP5_PIPE_OUT_XY(pipe
),
851 MDP5_PIPE_OUT_XY_X(crtc_x
) |
852 MDP5_PIPE_OUT_XY_Y(crtc_y
));
854 mdp5_write(mdp5_kms
, REG_MDP5_PIPE_SRC_FORMAT(pipe
),
855 MDP5_PIPE_SRC_FORMAT_A_BPC(format
->bpc_a
) |
856 MDP5_PIPE_SRC_FORMAT_R_BPC(format
->bpc_r
) |
857 MDP5_PIPE_SRC_FORMAT_G_BPC(format
->bpc_g
) |
858 MDP5_PIPE_SRC_FORMAT_B_BPC(format
->bpc_b
) |
859 COND(format
->alpha_enable
, MDP5_PIPE_SRC_FORMAT_ALPHA_ENABLE
) |
860 MDP5_PIPE_SRC_FORMAT_CPP(format
->cpp
- 1) |
861 MDP5_PIPE_SRC_FORMAT_UNPACK_COUNT(format
->unpack_count
- 1) |
862 COND(format
->unpack_tight
, MDP5_PIPE_SRC_FORMAT_UNPACK_TIGHT
) |
863 MDP5_PIPE_SRC_FORMAT_FETCH_TYPE(format
->fetch_type
) |
864 MDP5_PIPE_SRC_FORMAT_CHROMA_SAMP(format
->chroma_sample
));
866 mdp5_write(mdp5_kms
, REG_MDP5_PIPE_SRC_UNPACK(pipe
),
867 MDP5_PIPE_SRC_UNPACK_ELEM0(format
->unpack
[0]) |
868 MDP5_PIPE_SRC_UNPACK_ELEM1(format
->unpack
[1]) |
869 MDP5_PIPE_SRC_UNPACK_ELEM2(format
->unpack
[2]) |
870 MDP5_PIPE_SRC_UNPACK_ELEM3(format
->unpack
[3]));
872 mdp5_write(mdp5_kms
, REG_MDP5_PIPE_SRC_OP_MODE(pipe
),
873 (hflip
? MDP5_PIPE_SRC_OP_MODE_FLIP_LR
: 0) |
874 (vflip
? MDP5_PIPE_SRC_OP_MODE_FLIP_UD
: 0) |
875 COND(has_pe
, MDP5_PIPE_SRC_OP_MODE_SW_PIX_EXT_OVERRIDE
) |
876 MDP5_PIPE_SRC_OP_MODE_BWC(BWC_LOSSLESS
));
878 /* not using secure mode: */
879 mdp5_write(mdp5_kms
, REG_MDP5_PIPE_SRC_ADDR_SW_STATUS(pipe
), 0);
881 if (hwpipe
->caps
& MDP_PIPE_CAP_SW_PIX_EXT
)
882 mdp5_write_pixel_ext(mdp5_kms
, pipe
, format
,
883 src_w
, pe
->left
, pe
->right
,
884 src_h
, pe
->top
, pe
->bottom
);
886 if (hwpipe
->caps
& MDP_PIPE_CAP_SCALE
) {
887 mdp5_write(mdp5_kms
, REG_MDP5_PIPE_SCALE_PHASE_STEP_X(pipe
),
889 mdp5_write(mdp5_kms
, REG_MDP5_PIPE_SCALE_PHASE_STEP_Y(pipe
),
891 mdp5_write(mdp5_kms
, REG_MDP5_PIPE_SCALE_CR_PHASE_STEP_X(pipe
),
893 mdp5_write(mdp5_kms
, REG_MDP5_PIPE_SCALE_CR_PHASE_STEP_Y(pipe
),
895 mdp5_write(mdp5_kms
, REG_MDP5_PIPE_DECIMATION(pipe
),
896 MDP5_PIPE_DECIMATION_VERT(vdecm
) |
897 MDP5_PIPE_DECIMATION_HORZ(hdecm
));
898 mdp5_write(mdp5_kms
, REG_MDP5_PIPE_SCALE_CONFIG(pipe
),
902 if (hwpipe
->caps
& MDP_PIPE_CAP_CSC
) {
903 if (MDP_FORMAT_IS_YUV(format
))
904 csc_enable(mdp5_kms
, pipe
,
905 mdp_get_default_csc_cfg(CSC_YUV2RGB
));
907 csc_disable(mdp5_kms
, pipe
);
910 set_scanout_locked(mdp5_kms
, pipe
, fb
);
913 static int mdp5_plane_mode_set(struct drm_plane
*plane
,
914 struct drm_crtc
*crtc
, struct drm_framebuffer
*fb
,
915 struct drm_rect
*src
, struct drm_rect
*dest
)
917 struct drm_plane_state
*pstate
= plane
->state
;
918 struct mdp5_hw_pipe
*hwpipe
= to_mdp5_plane_state(pstate
)->hwpipe
;
919 struct mdp5_kms
*mdp5_kms
= get_kms(plane
);
920 enum mdp5_pipe pipe
= hwpipe
->pipe
;
921 struct mdp5_hw_pipe
*right_hwpipe
;
922 const struct mdp_format
*format
;
923 uint32_t nplanes
, config
= 0;
924 struct phase_step step
= { { 0 } };
925 struct pixel_ext pe
= { { 0 } };
926 uint32_t hdecm
= 0, vdecm
= 0;
928 unsigned int rotation
;
931 unsigned int crtc_w
, crtc_h
;
932 uint32_t src_x
, src_y
;
933 uint32_t src_w
, src_h
;
934 uint32_t src_img_w
, src_img_h
;
937 nplanes
= fb
->format
->num_planes
;
939 /* bad formats should already be rejected: */
940 if (WARN_ON(nplanes
> pipe2nclients(pipe
)))
943 format
= to_mdp_format(msm_framebuffer_format(fb
));
944 pix_format
= format
->base
.pixel_format
;
948 src_w
= drm_rect_width(src
);
949 src_h
= drm_rect_height(src
);
953 crtc_w
= drm_rect_width(dest
);
954 crtc_h
= drm_rect_height(dest
);
956 /* src values are in Q16 fixed point, convert to integer: */
962 src_img_w
= min(fb
->width
, src_w
);
963 src_img_h
= min(fb
->height
, src_h
);
965 DBG("%s: FB[%u] %u,%u,%u,%u -> CRTC[%u] %d,%d,%u,%u", plane
->name
,
966 fb
->base
.id
, src_x
, src_y
, src_w
, src_h
,
967 crtc
->base
.id
, crtc_x
, crtc_y
, crtc_w
, crtc_h
);
969 right_hwpipe
= to_mdp5_plane_state(pstate
)->r_hwpipe
;
972 * if the plane comprises of 2 hw pipes, assume that the width
973 * is split equally across them. The only parameters that varies
974 * between the 2 pipes are src_x and crtc_x
981 ret
= calc_scalex_steps(plane
, pix_format
, src_w
, crtc_w
, step
.x
);
985 ret
= calc_scaley_steps(plane
, pix_format
, src_h
, crtc_h
, step
.y
);
989 if (hwpipe
->caps
& MDP_PIPE_CAP_SW_PIX_EXT
) {
990 calc_pixel_ext(format
, src_w
, crtc_w
, step
.x
,
991 pe
.left
, pe
.right
, true);
992 calc_pixel_ext(format
, src_h
, crtc_h
, step
.y
,
993 pe
.top
, pe
.bottom
, false);
996 /* TODO calc hdecm, vdecm */
998 /* SCALE is used to both scale and up-sample chroma components */
999 config
|= get_scale_config(format
, src_w
, crtc_w
, true);
1000 config
|= get_scale_config(format
, src_h
, crtc_h
, false);
1001 DBG("scale config = %x", config
);
1003 rotation
= drm_rotation_simplify(pstate
->rotation
,
1005 DRM_MODE_REFLECT_X
|
1006 DRM_MODE_REFLECT_Y
);
1007 hflip
= !!(rotation
& DRM_MODE_REFLECT_X
);
1008 vflip
= !!(rotation
& DRM_MODE_REFLECT_Y
);
1010 mdp5_hwpipe_mode_set(mdp5_kms
, hwpipe
, fb
, &step
, &pe
,
1011 config
, hdecm
, vdecm
, hflip
, vflip
,
1012 crtc_x
, crtc_y
, crtc_w
, crtc_h
,
1013 src_img_w
, src_img_h
,
1014 src_x
, src_y
, src_w
, src_h
);
1016 mdp5_hwpipe_mode_set(mdp5_kms
, right_hwpipe
, fb
, &step
, &pe
,
1017 config
, hdecm
, vdecm
, hflip
, vflip
,
1018 crtc_x
+ crtc_w
, crtc_y
, crtc_w
, crtc_h
,
1019 src_img_w
, src_img_h
,
1020 src_x
+ src_w
, src_y
, src_w
, src_h
);
1026 * Use this func and the one below only after the atomic state has been
1027 * successfully swapped
1029 enum mdp5_pipe
mdp5_plane_pipe(struct drm_plane
*plane
)
1031 struct mdp5_plane_state
*pstate
= to_mdp5_plane_state(plane
->state
);
1033 if (WARN_ON(!pstate
->hwpipe
))
1036 return pstate
->hwpipe
->pipe
;
1039 enum mdp5_pipe
mdp5_plane_right_pipe(struct drm_plane
*plane
)
1041 struct mdp5_plane_state
*pstate
= to_mdp5_plane_state(plane
->state
);
1043 if (!pstate
->r_hwpipe
)
1046 return pstate
->r_hwpipe
->pipe
;
1049 uint32_t mdp5_plane_get_flush(struct drm_plane
*plane
)
1051 struct mdp5_plane_state
*pstate
= to_mdp5_plane_state(plane
->state
);
1054 if (WARN_ON(!pstate
->hwpipe
))
1057 mask
= pstate
->hwpipe
->flush_mask
;
1059 if (pstate
->r_hwpipe
)
1060 mask
|= pstate
->r_hwpipe
->flush_mask
;
1065 /* initialize plane */
1066 struct drm_plane
*mdp5_plane_init(struct drm_device
*dev
,
1067 enum drm_plane_type type
)
1069 struct drm_plane
*plane
= NULL
;
1070 struct mdp5_plane
*mdp5_plane
;
1073 mdp5_plane
= kzalloc(sizeof(*mdp5_plane
), GFP_KERNEL
);
1079 plane
= &mdp5_plane
->base
;
1081 mdp5_plane
->nformats
= mdp_get_formats(mdp5_plane
->formats
,
1082 ARRAY_SIZE(mdp5_plane
->formats
), false);
1084 ret
= drm_universal_plane_init(dev
, plane
, 0xff, &mdp5_plane_funcs
,
1085 mdp5_plane
->formats
, mdp5_plane
->nformats
,
1090 drm_plane_helper_add(plane
, &mdp5_plane_helper_funcs
);
1092 mdp5_plane_install_properties(plane
, &plane
->base
);
1094 drm_plane_enable_fb_damage_clips(plane
);
1100 mdp5_plane_destroy(plane
);
1102 return ERR_PTR(ret
);