2 * Copyright (C) 2014-2015 The Linux Foundation. All rights reserved.
3 * Copyright (C) 2013 Red Hat
4 * Author: Rob Clark <robdclark@gmail.com>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program. If not, see <http://www.gnu.org/licenses/>.
19 #include <drm/drm_print.h>
23 struct drm_plane base;
28 #define to_mdp5_plane(x) container_of(x, struct mdp5_plane, base)
30 static int mdp5_plane_mode_set(struct drm_plane *plane,
31 struct drm_crtc *crtc, struct drm_framebuffer *fb,
32 struct drm_rect *src, struct drm_rect *dest);
34 static struct mdp5_kms *get_kms(struct drm_plane *plane)
36 struct msm_drm_private *priv = plane->dev->dev_private;
37 return to_mdp5_kms(to_mdp_kms(priv->kms));
40 static bool plane_enabled(struct drm_plane_state *state)
42 return state->visible;
45 static void mdp5_plane_destroy(struct drm_plane *plane)
47 struct mdp5_plane *mdp5_plane = to_mdp5_plane(plane);
49 drm_plane_cleanup(plane);
54 static void mdp5_plane_install_rotation_property(struct drm_device *dev,
55 struct drm_plane *plane)
57 drm_plane_create_rotation_property(plane,
65 /* helper to install properties which are common to planes and crtcs */
66 static void mdp5_plane_install_properties(struct drm_plane *plane,
67 struct drm_mode_object *obj)
69 struct drm_device *dev = plane->dev;
70 struct msm_drm_private *dev_priv = dev->dev_private;
71 struct drm_property *prop;
73 #define INSTALL_PROPERTY(name, NAME, init_val, fnc, ...) do { \
74 prop = dev_priv->plane_property[PLANE_PROP_##NAME]; \
76 prop = drm_property_##fnc(dev, 0, #name, \
80 "Create property %s failed\n", \
84 dev_priv->plane_property[PLANE_PROP_##NAME] = prop; \
86 drm_object_attach_property(&plane->base, prop, init_val); \
89 #define INSTALL_RANGE_PROPERTY(name, NAME, min, max, init_val) \
90 INSTALL_PROPERTY(name, NAME, init_val, \
91 create_range, min, max)
93 #define INSTALL_ENUM_PROPERTY(name, NAME, init_val) \
94 INSTALL_PROPERTY(name, NAME, init_val, \
95 create_enum, name##_prop_enum_list, \
96 ARRAY_SIZE(name##_prop_enum_list))
98 INSTALL_RANGE_PROPERTY(zpos, ZPOS, 1, 255, 1);
100 mdp5_plane_install_rotation_property(dev, plane);
102 #undef INSTALL_RANGE_PROPERTY
103 #undef INSTALL_ENUM_PROPERTY
104 #undef INSTALL_PROPERTY
107 static int mdp5_plane_atomic_set_property(struct drm_plane *plane,
108 struct drm_plane_state *state, struct drm_property *property,
111 struct drm_device *dev = plane->dev;
112 struct mdp5_plane_state *pstate;
113 struct msm_drm_private *dev_priv = dev->dev_private;
116 pstate = to_mdp5_plane_state(state);
118 #define SET_PROPERTY(name, NAME, type) do { \
119 if (dev_priv->plane_property[PLANE_PROP_##NAME] == property) { \
120 pstate->name = (type)val; \
121 DBG("Set property %s %d", #name, (type)val); \
126 SET_PROPERTY(zpos, ZPOS, uint8_t);
128 dev_err(dev->dev, "Invalid property\n");
135 static int mdp5_plane_atomic_get_property(struct drm_plane *plane,
136 const struct drm_plane_state *state,
137 struct drm_property *property, uint64_t *val)
139 struct drm_device *dev = plane->dev;
140 struct mdp5_plane_state *pstate;
141 struct msm_drm_private *dev_priv = dev->dev_private;
144 pstate = to_mdp5_plane_state(state);
146 #define GET_PROPERTY(name, NAME, type) do { \
147 if (dev_priv->plane_property[PLANE_PROP_##NAME] == property) { \
148 *val = pstate->name; \
149 DBG("Get property %s %lld", #name, *val); \
154 GET_PROPERTY(zpos, ZPOS, uint8_t);
156 dev_err(dev->dev, "Invalid property\n");
164 mdp5_plane_atomic_print_state(struct drm_printer *p,
165 const struct drm_plane_state *state)
167 struct mdp5_plane_state *pstate = to_mdp5_plane_state(state);
168 struct mdp5_kms *mdp5_kms = get_kms(state->plane);
170 drm_printf(p, "\thwpipe=%s\n", pstate->hwpipe ?
171 pstate->hwpipe->name : "(null)");
172 if (mdp5_kms->caps & MDP_CAP_SRC_SPLIT)
173 drm_printf(p, "\tright-hwpipe=%s\n",
174 pstate->r_hwpipe ? pstate->r_hwpipe->name :
176 drm_printf(p, "\tpremultiplied=%u\n", pstate->premultiplied);
177 drm_printf(p, "\tzpos=%u\n", pstate->zpos);
178 drm_printf(p, "\talpha=%u\n", pstate->alpha);
179 drm_printf(p, "\tstage=%s\n", stage2name(pstate->stage));
182 static void mdp5_plane_reset(struct drm_plane *plane)
184 struct mdp5_plane_state *mdp5_state;
186 if (plane->state && plane->state->fb)
187 drm_framebuffer_put(plane->state->fb);
189 kfree(to_mdp5_plane_state(plane->state));
190 mdp5_state = kzalloc(sizeof(*mdp5_state), GFP_KERNEL);
192 /* assign default blend parameters */
193 mdp5_state->alpha = 255;
194 mdp5_state->premultiplied = 0;
196 if (plane->type == DRM_PLANE_TYPE_PRIMARY)
197 mdp5_state->zpos = STAGE_BASE;
199 mdp5_state->zpos = STAGE0 + drm_plane_index(plane);
201 mdp5_state->base.plane = plane;
203 plane->state = &mdp5_state->base;
206 static struct drm_plane_state *
207 mdp5_plane_duplicate_state(struct drm_plane *plane)
209 struct mdp5_plane_state *mdp5_state;
211 if (WARN_ON(!plane->state))
214 mdp5_state = kmemdup(to_mdp5_plane_state(plane->state),
215 sizeof(*mdp5_state), GFP_KERNEL);
219 __drm_atomic_helper_plane_duplicate_state(plane, &mdp5_state->base);
221 return &mdp5_state->base;
224 static void mdp5_plane_destroy_state(struct drm_plane *plane,
225 struct drm_plane_state *state)
227 struct mdp5_plane_state *pstate = to_mdp5_plane_state(state);
230 drm_framebuffer_put(state->fb);
235 static const struct drm_plane_funcs mdp5_plane_funcs = {
236 .update_plane = drm_atomic_helper_update_plane,
237 .disable_plane = drm_atomic_helper_disable_plane,
238 .destroy = mdp5_plane_destroy,
239 .atomic_set_property = mdp5_plane_atomic_set_property,
240 .atomic_get_property = mdp5_plane_atomic_get_property,
241 .reset = mdp5_plane_reset,
242 .atomic_duplicate_state = mdp5_plane_duplicate_state,
243 .atomic_destroy_state = mdp5_plane_destroy_state,
244 .atomic_print_state = mdp5_plane_atomic_print_state,
247 static void mdp5_plane_cleanup_fb(struct drm_plane *plane,
248 struct drm_plane_state *old_state)
250 struct mdp5_kms *mdp5_kms = get_kms(plane);
251 struct msm_kms *kms = &mdp5_kms->base.base;
252 struct drm_framebuffer *fb = old_state->fb;
257 DBG("%s: cleanup: FB[%u]", plane->name, fb->base.id);
258 msm_framebuffer_cleanup(fb, kms->aspace);
261 static int mdp5_plane_atomic_check_with_state(struct drm_crtc_state *crtc_state,
262 struct drm_plane_state *state)
264 struct mdp5_plane_state *mdp5_state = to_mdp5_plane_state(state);
265 struct drm_plane *plane = state->plane;
266 struct drm_plane_state *old_state = plane->state;
267 struct mdp5_cfg *config = mdp5_cfg_get_config(get_kms(plane)->cfg);
268 bool new_hwpipe = false;
269 bool need_right_hwpipe = false;
270 uint32_t max_width, max_height;
271 bool out_of_bounds = false;
273 int min_scale, max_scale;
276 DBG("%s: check (%d -> %d)", plane->name,
277 plane_enabled(old_state), plane_enabled(state));
279 max_width = config->hw->lm.max_width << 16;
280 max_height = config->hw->lm.max_height << 16;
282 /* Make sure source dimensions are within bounds. */
283 if (state->src_h > max_height)
284 out_of_bounds = true;
286 if (state->src_w > max_width) {
287 /* If source split is supported, we can go up to 2x
288 * the max LM width, but we'd need to stage another
289 * hwpipe to the right LM. So, the drm_plane would
290 * consist of 2 hwpipes.
292 if (config->hw->mdp.caps & MDP_CAP_SRC_SPLIT &&
293 (state->src_w <= 2 * max_width))
294 need_right_hwpipe = true;
296 out_of_bounds = true;
300 struct drm_rect src = drm_plane_state_src(state);
301 DBG("Invalid source size "DRM_RECT_FP_FMT,
302 DRM_RECT_FP_ARG(&src));
306 min_scale = FRAC_16_16(1, 8);
307 max_scale = FRAC_16_16(8, 1);
309 ret = drm_atomic_helper_check_plane_state(state, crtc_state,
310 min_scale, max_scale,
315 if (plane_enabled(state)) {
316 unsigned int rotation;
317 const struct mdp_format *format;
318 struct mdp5_kms *mdp5_kms = get_kms(plane);
321 format = to_mdp_format(msm_framebuffer_format(state->fb));
322 if (MDP_FORMAT_IS_YUV(format))
323 caps |= MDP_PIPE_CAP_SCALE | MDP_PIPE_CAP_CSC;
325 if (((state->src_w >> 16) != state->crtc_w) ||
326 ((state->src_h >> 16) != state->crtc_h))
327 caps |= MDP_PIPE_CAP_SCALE;
329 rotation = drm_rotation_simplify(state->rotation,
334 if (rotation & DRM_MODE_REFLECT_X)
335 caps |= MDP_PIPE_CAP_HFLIP;
337 if (rotation & DRM_MODE_REFLECT_Y)
338 caps |= MDP_PIPE_CAP_VFLIP;
340 if (plane->type == DRM_PLANE_TYPE_CURSOR)
341 caps |= MDP_PIPE_CAP_CURSOR;
343 /* (re)allocate hw pipe if we don't have one or caps-mismatch: */
344 if (!mdp5_state->hwpipe || (caps & ~mdp5_state->hwpipe->caps))
348 * (re)allocte hw pipe if we're either requesting for 2 hw pipes
349 * or we're switching from 2 hw pipes to 1 hw pipe because the
350 * new src_w can be supported by 1 hw pipe itself.
352 if ((need_right_hwpipe && !mdp5_state->r_hwpipe) ||
353 (!need_right_hwpipe && mdp5_state->r_hwpipe))
357 const struct mdp_format *format =
358 to_mdp_format(msm_framebuffer_format(state->fb));
360 blkcfg = mdp5_smp_calculate(mdp5_kms->smp, format,
361 state->src_w >> 16, false);
363 if (mdp5_state->hwpipe && (mdp5_state->hwpipe->blkcfg != blkcfg))
367 /* (re)assign hwpipe if needed, otherwise keep old one: */
369 /* TODO maybe we want to re-assign hwpipe sometimes
370 * in cases when we no-longer need some caps to make
371 * it available for other planes?
373 struct mdp5_hw_pipe *old_hwpipe = mdp5_state->hwpipe;
374 struct mdp5_hw_pipe *old_right_hwpipe =
375 mdp5_state->r_hwpipe;
376 struct mdp5_hw_pipe *new_hwpipe = NULL;
377 struct mdp5_hw_pipe *new_right_hwpipe = NULL;
379 ret = mdp5_pipe_assign(state->state, plane, caps,
382 &new_right_hwpipe : NULL);
384 DBG("%s: failed to assign hwpipe(s)!",
389 mdp5_state->hwpipe = new_hwpipe;
390 if (need_right_hwpipe)
391 mdp5_state->r_hwpipe = new_right_hwpipe;
394 * set it to NULL so that the driver knows we
395 * don't have a right hwpipe when committing a
398 mdp5_state->r_hwpipe = NULL;
401 mdp5_pipe_release(state->state, old_hwpipe);
402 mdp5_pipe_release(state->state, old_right_hwpipe);
405 mdp5_pipe_release(state->state, mdp5_state->hwpipe);
406 mdp5_pipe_release(state->state, mdp5_state->r_hwpipe);
407 mdp5_state->hwpipe = mdp5_state->r_hwpipe = NULL;
413 static int mdp5_plane_atomic_check(struct drm_plane *plane,
414 struct drm_plane_state *state)
416 struct drm_crtc *crtc;
417 struct drm_crtc_state *crtc_state;
419 crtc = state->crtc ? state->crtc : plane->state->crtc;
423 crtc_state = drm_atomic_get_existing_crtc_state(state->state, crtc);
424 if (WARN_ON(!crtc_state))
427 return mdp5_plane_atomic_check_with_state(crtc_state, state);
430 static void mdp5_plane_atomic_update(struct drm_plane *plane,
431 struct drm_plane_state *old_state)
433 struct drm_plane_state *state = plane->state;
435 DBG("%s: update", plane->name);
437 if (plane_enabled(state)) {
440 ret = mdp5_plane_mode_set(plane,
441 state->crtc, state->fb,
442 &state->src, &state->dst);
443 /* atomic_check should have ensured that this doesn't fail */
448 static int mdp5_plane_atomic_async_check(struct drm_plane *plane,
449 struct drm_plane_state *state)
451 struct mdp5_plane_state *mdp5_state = to_mdp5_plane_state(state);
452 struct drm_crtc_state *crtc_state;
453 int min_scale, max_scale;
456 crtc_state = drm_atomic_get_existing_crtc_state(state->state,
458 if (WARN_ON(!crtc_state))
461 if (!crtc_state->active)
464 mdp5_state = to_mdp5_plane_state(state);
466 /* don't use fast path if we don't have a hwpipe allocated yet */
467 if (!mdp5_state->hwpipe)
470 /* only allow changing of position(crtc x/y or src x/y) in fast path */
471 if (plane->state->crtc != state->crtc ||
472 plane->state->src_w != state->src_w ||
473 plane->state->src_h != state->src_h ||
474 plane->state->crtc_w != state->crtc_w ||
475 plane->state->crtc_h != state->crtc_h ||
477 plane->state->fb != state->fb)
480 min_scale = FRAC_16_16(1, 8);
481 max_scale = FRAC_16_16(8, 1);
483 ret = drm_atomic_helper_check_plane_state(state, crtc_state,
484 min_scale, max_scale,
490 * if the visibility of the plane changes (i.e, if the cursor is
491 * clipped out completely, we can't take the async path because
492 * we need to stage/unstage the plane from the Layer Mixer(s). We
493 * also assign/unassign the hwpipe(s) tied to the plane. We avoid
494 * taking the fast path for both these reasons.
496 if (state->visible != plane->state->visible)
502 static void mdp5_plane_atomic_async_update(struct drm_plane *plane,
503 struct drm_plane_state *new_state)
505 plane->state->src_x = new_state->src_x;
506 plane->state->src_y = new_state->src_y;
507 plane->state->crtc_x = new_state->crtc_x;
508 plane->state->crtc_y = new_state->crtc_y;
510 if (plane_enabled(new_state)) {
511 struct mdp5_ctl *ctl;
512 struct mdp5_pipeline *pipeline =
513 mdp5_crtc_get_pipeline(new_state->crtc);
516 ret = mdp5_plane_mode_set(plane, new_state->crtc, new_state->fb,
517 &new_state->src, &new_state->dst);
520 ctl = mdp5_crtc_get_ctl(new_state->crtc);
522 mdp5_ctl_commit(ctl, pipeline, mdp5_plane_get_flush(plane), true);
525 *to_mdp5_plane_state(plane->state) =
526 *to_mdp5_plane_state(new_state);
529 static const struct drm_plane_helper_funcs mdp5_plane_helper_funcs = {
530 .prepare_fb = msm_atomic_prepare_fb,
531 .cleanup_fb = mdp5_plane_cleanup_fb,
532 .atomic_check = mdp5_plane_atomic_check,
533 .atomic_update = mdp5_plane_atomic_update,
534 .atomic_async_check = mdp5_plane_atomic_async_check,
535 .atomic_async_update = mdp5_plane_atomic_async_update,
538 static void set_scanout_locked(struct mdp5_kms *mdp5_kms,
540 struct drm_framebuffer *fb)
542 struct msm_kms *kms = &mdp5_kms->base.base;
544 mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC_STRIDE_A(pipe),
545 MDP5_PIPE_SRC_STRIDE_A_P0(fb->pitches[0]) |
546 MDP5_PIPE_SRC_STRIDE_A_P1(fb->pitches[1]));
548 mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC_STRIDE_B(pipe),
549 MDP5_PIPE_SRC_STRIDE_B_P2(fb->pitches[2]) |
550 MDP5_PIPE_SRC_STRIDE_B_P3(fb->pitches[3]));
552 mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC0_ADDR(pipe),
553 msm_framebuffer_iova(fb, kms->aspace, 0));
554 mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC1_ADDR(pipe),
555 msm_framebuffer_iova(fb, kms->aspace, 1));
556 mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC2_ADDR(pipe),
557 msm_framebuffer_iova(fb, kms->aspace, 2));
558 mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC3_ADDR(pipe),
559 msm_framebuffer_iova(fb, kms->aspace, 3));
562 /* Note: mdp5_plane->pipe_lock must be locked */
563 static void csc_disable(struct mdp5_kms *mdp5_kms, enum mdp5_pipe pipe)
565 uint32_t value = mdp5_read(mdp5_kms, REG_MDP5_PIPE_OP_MODE(pipe)) &
566 ~MDP5_PIPE_OP_MODE_CSC_1_EN;
568 mdp5_write(mdp5_kms, REG_MDP5_PIPE_OP_MODE(pipe), value);
571 /* Note: mdp5_plane->pipe_lock must be locked */
572 static void csc_enable(struct mdp5_kms *mdp5_kms, enum mdp5_pipe pipe,
575 uint32_t i, mode = 0; /* RGB, no CSC */
581 if ((csc->type == CSC_YUV2RGB) || (CSC_YUV2YUV == csc->type))
582 mode |= MDP5_PIPE_OP_MODE_CSC_SRC_DATA_FORMAT(DATA_FORMAT_YUV);
583 if ((csc->type == CSC_RGB2YUV) || (CSC_YUV2YUV == csc->type))
584 mode |= MDP5_PIPE_OP_MODE_CSC_DST_DATA_FORMAT(DATA_FORMAT_YUV);
585 mode |= MDP5_PIPE_OP_MODE_CSC_1_EN;
586 mdp5_write(mdp5_kms, REG_MDP5_PIPE_OP_MODE(pipe), mode);
588 matrix = csc->matrix;
589 mdp5_write(mdp5_kms, REG_MDP5_PIPE_CSC_1_MATRIX_COEFF_0(pipe),
590 MDP5_PIPE_CSC_1_MATRIX_COEFF_0_COEFF_11(matrix[0]) |
591 MDP5_PIPE_CSC_1_MATRIX_COEFF_0_COEFF_12(matrix[1]));
592 mdp5_write(mdp5_kms, REG_MDP5_PIPE_CSC_1_MATRIX_COEFF_1(pipe),
593 MDP5_PIPE_CSC_1_MATRIX_COEFF_1_COEFF_13(matrix[2]) |
594 MDP5_PIPE_CSC_1_MATRIX_COEFF_1_COEFF_21(matrix[3]));
595 mdp5_write(mdp5_kms, REG_MDP5_PIPE_CSC_1_MATRIX_COEFF_2(pipe),
596 MDP5_PIPE_CSC_1_MATRIX_COEFF_2_COEFF_22(matrix[4]) |
597 MDP5_PIPE_CSC_1_MATRIX_COEFF_2_COEFF_23(matrix[5]));
598 mdp5_write(mdp5_kms, REG_MDP5_PIPE_CSC_1_MATRIX_COEFF_3(pipe),
599 MDP5_PIPE_CSC_1_MATRIX_COEFF_3_COEFF_31(matrix[6]) |
600 MDP5_PIPE_CSC_1_MATRIX_COEFF_3_COEFF_32(matrix[7]));
601 mdp5_write(mdp5_kms, REG_MDP5_PIPE_CSC_1_MATRIX_COEFF_4(pipe),
602 MDP5_PIPE_CSC_1_MATRIX_COEFF_4_COEFF_33(matrix[8]));
604 for (i = 0; i < ARRAY_SIZE(csc->pre_bias); i++) {
605 uint32_t *pre_clamp = csc->pre_clamp;
606 uint32_t *post_clamp = csc->post_clamp;
608 mdp5_write(mdp5_kms, REG_MDP5_PIPE_CSC_1_PRE_CLAMP(pipe, i),
609 MDP5_PIPE_CSC_1_PRE_CLAMP_REG_HIGH(pre_clamp[2*i+1]) |
610 MDP5_PIPE_CSC_1_PRE_CLAMP_REG_LOW(pre_clamp[2*i]));
612 mdp5_write(mdp5_kms, REG_MDP5_PIPE_CSC_1_POST_CLAMP(pipe, i),
613 MDP5_PIPE_CSC_1_POST_CLAMP_REG_HIGH(post_clamp[2*i+1]) |
614 MDP5_PIPE_CSC_1_POST_CLAMP_REG_LOW(post_clamp[2*i]));
616 mdp5_write(mdp5_kms, REG_MDP5_PIPE_CSC_1_PRE_BIAS(pipe, i),
617 MDP5_PIPE_CSC_1_PRE_BIAS_REG_VALUE(csc->pre_bias[i]));
619 mdp5_write(mdp5_kms, REG_MDP5_PIPE_CSC_1_POST_BIAS(pipe, i),
620 MDP5_PIPE_CSC_1_POST_BIAS_REG_VALUE(csc->post_bias[i]));
624 #define PHASE_STEP_SHIFT 21
625 #define DOWN_SCALE_RATIO_MAX 32 /* 2^(26-21) */
627 static int calc_phase_step(uint32_t src, uint32_t dst, uint32_t *out_phase)
631 if (src == 0 || dst == 0)
635 * PHASE_STEP_X/Y is coded on 26 bits (25:0),
636 * where 2^21 represents the unity "1" in fixed-point hardware design.
637 * This leaves 5 bits for the integer part (downscale case):
638 * -> maximum downscale ratio = 0b1_1111 = 31
640 if (src > (dst * DOWN_SCALE_RATIO_MAX))
643 unit = 1 << PHASE_STEP_SHIFT;
644 *out_phase = mult_frac(unit, src, dst);
649 static int calc_scalex_steps(struct drm_plane *plane,
650 uint32_t pixel_format, uint32_t src, uint32_t dest,
651 uint32_t phasex_steps[COMP_MAX])
653 struct mdp5_kms *mdp5_kms = get_kms(plane);
654 struct device *dev = mdp5_kms->dev->dev;
655 uint32_t phasex_step;
659 ret = calc_phase_step(src, dest, &phasex_step);
661 dev_err(dev, "X scaling (%d->%d) failed: %d\n", src, dest, ret);
665 hsub = drm_format_horz_chroma_subsampling(pixel_format);
667 phasex_steps[COMP_0] = phasex_step;
668 phasex_steps[COMP_3] = phasex_step;
669 phasex_steps[COMP_1_2] = phasex_step / hsub;
674 static int calc_scaley_steps(struct drm_plane *plane,
675 uint32_t pixel_format, uint32_t src, uint32_t dest,
676 uint32_t phasey_steps[COMP_MAX])
678 struct mdp5_kms *mdp5_kms = get_kms(plane);
679 struct device *dev = mdp5_kms->dev->dev;
680 uint32_t phasey_step;
684 ret = calc_phase_step(src, dest, &phasey_step);
686 dev_err(dev, "Y scaling (%d->%d) failed: %d\n", src, dest, ret);
690 vsub = drm_format_vert_chroma_subsampling(pixel_format);
692 phasey_steps[COMP_0] = phasey_step;
693 phasey_steps[COMP_3] = phasey_step;
694 phasey_steps[COMP_1_2] = phasey_step / vsub;
699 static uint32_t get_scale_config(const struct mdp_format *format,
700 uint32_t src, uint32_t dst, bool horz)
702 bool scaling = format->is_yuv ? true : (src != dst);
703 uint32_t sub, pix_fmt = format->base.pixel_format;
704 uint32_t ya_filter, uv_filter;
705 bool yuv = format->is_yuv;
711 sub = horz ? drm_format_horz_chroma_subsampling(pix_fmt) :
712 drm_format_vert_chroma_subsampling(pix_fmt);
713 uv_filter = ((src / sub) <= dst) ?
714 SCALE_FILTER_BIL : SCALE_FILTER_PCMN;
716 ya_filter = (src <= dst) ? SCALE_FILTER_BIL : SCALE_FILTER_PCMN;
719 return MDP5_PIPE_SCALE_CONFIG_SCALEX_EN |
720 MDP5_PIPE_SCALE_CONFIG_SCALEX_FILTER_COMP_0(ya_filter) |
721 MDP5_PIPE_SCALE_CONFIG_SCALEX_FILTER_COMP_3(ya_filter) |
722 COND(yuv, MDP5_PIPE_SCALE_CONFIG_SCALEX_FILTER_COMP_1_2(uv_filter));
724 return MDP5_PIPE_SCALE_CONFIG_SCALEY_EN |
725 MDP5_PIPE_SCALE_CONFIG_SCALEY_FILTER_COMP_0(ya_filter) |
726 MDP5_PIPE_SCALE_CONFIG_SCALEY_FILTER_COMP_3(ya_filter) |
727 COND(yuv, MDP5_PIPE_SCALE_CONFIG_SCALEY_FILTER_COMP_1_2(uv_filter));
730 static void calc_pixel_ext(const struct mdp_format *format,
731 uint32_t src, uint32_t dst, uint32_t phase_step[2],
732 int pix_ext_edge1[COMP_MAX], int pix_ext_edge2[COMP_MAX],
735 bool scaling = format->is_yuv ? true : (src != dst);
740 * We assume here that:
741 * 1. PCMN filter is used for downscale
742 * 2. bilinear filter is used for upscale
743 * 3. we are in a single pipe configuration
746 for (i = 0; i < COMP_MAX; i++) {
747 pix_ext_edge1[i] = 0;
748 pix_ext_edge2[i] = scaling ? 1 : 0;
752 static void mdp5_write_pixel_ext(struct mdp5_kms *mdp5_kms, enum mdp5_pipe pipe,
753 const struct mdp_format *format,
754 uint32_t src_w, int pe_left[COMP_MAX], int pe_right[COMP_MAX],
755 uint32_t src_h, int pe_top[COMP_MAX], int pe_bottom[COMP_MAX])
757 uint32_t pix_fmt = format->base.pixel_format;
758 uint32_t lr, tb, req;
761 for (i = 0; i < COMP_MAX; i++) {
762 uint32_t roi_w = src_w;
763 uint32_t roi_h = src_h;
765 if (format->is_yuv && i == COMP_1_2) {
766 roi_w /= drm_format_horz_chroma_subsampling(pix_fmt);
767 roi_h /= drm_format_vert_chroma_subsampling(pix_fmt);
770 lr = (pe_left[i] >= 0) ?
771 MDP5_PIPE_SW_PIX_EXT_LR_LEFT_RPT(pe_left[i]) :
772 MDP5_PIPE_SW_PIX_EXT_LR_LEFT_OVF(pe_left[i]);
774 lr |= (pe_right[i] >= 0) ?
775 MDP5_PIPE_SW_PIX_EXT_LR_RIGHT_RPT(pe_right[i]) :
776 MDP5_PIPE_SW_PIX_EXT_LR_RIGHT_OVF(pe_right[i]);
778 tb = (pe_top[i] >= 0) ?
779 MDP5_PIPE_SW_PIX_EXT_TB_TOP_RPT(pe_top[i]) :
780 MDP5_PIPE_SW_PIX_EXT_TB_TOP_OVF(pe_top[i]);
782 tb |= (pe_bottom[i] >= 0) ?
783 MDP5_PIPE_SW_PIX_EXT_TB_BOTTOM_RPT(pe_bottom[i]) :
784 MDP5_PIPE_SW_PIX_EXT_TB_BOTTOM_OVF(pe_bottom[i]);
786 req = MDP5_PIPE_SW_PIX_EXT_REQ_PIXELS_LEFT_RIGHT(roi_w +
787 pe_left[i] + pe_right[i]);
789 req |= MDP5_PIPE_SW_PIX_EXT_REQ_PIXELS_TOP_BOTTOM(roi_h +
790 pe_top[i] + pe_bottom[i]);
792 mdp5_write(mdp5_kms, REG_MDP5_PIPE_SW_PIX_EXT_LR(pipe, i), lr);
793 mdp5_write(mdp5_kms, REG_MDP5_PIPE_SW_PIX_EXT_TB(pipe, i), tb);
794 mdp5_write(mdp5_kms, REG_MDP5_PIPE_SW_PIX_EXT_REQ_PIXELS(pipe, i), req);
796 DBG("comp-%d (L/R): rpt=%d/%d, ovf=%d/%d, req=%d", i,
797 FIELD(lr, MDP5_PIPE_SW_PIX_EXT_LR_LEFT_RPT),
798 FIELD(lr, MDP5_PIPE_SW_PIX_EXT_LR_RIGHT_RPT),
799 FIELD(lr, MDP5_PIPE_SW_PIX_EXT_LR_LEFT_OVF),
800 FIELD(lr, MDP5_PIPE_SW_PIX_EXT_LR_RIGHT_OVF),
801 FIELD(req, MDP5_PIPE_SW_PIX_EXT_REQ_PIXELS_LEFT_RIGHT));
803 DBG("comp-%d (T/B): rpt=%d/%d, ovf=%d/%d, req=%d", i,
804 FIELD(tb, MDP5_PIPE_SW_PIX_EXT_TB_TOP_RPT),
805 FIELD(tb, MDP5_PIPE_SW_PIX_EXT_TB_BOTTOM_RPT),
806 FIELD(tb, MDP5_PIPE_SW_PIX_EXT_TB_TOP_OVF),
807 FIELD(tb, MDP5_PIPE_SW_PIX_EXT_TB_BOTTOM_OVF),
808 FIELD(req, MDP5_PIPE_SW_PIX_EXT_REQ_PIXELS_TOP_BOTTOM));
816 int bottom[COMP_MAX];
824 static void mdp5_hwpipe_mode_set(struct mdp5_kms *mdp5_kms,
825 struct mdp5_hw_pipe *hwpipe,
826 struct drm_framebuffer *fb,
827 struct phase_step *step,
828 struct pixel_ext *pe,
829 u32 scale_config, u32 hdecm, u32 vdecm,
830 bool hflip, bool vflip,
831 int crtc_x, int crtc_y,
832 unsigned int crtc_w, unsigned int crtc_h,
833 u32 src_img_w, u32 src_img_h,
834 u32 src_x, u32 src_y,
835 u32 src_w, u32 src_h)
837 enum mdp5_pipe pipe = hwpipe->pipe;
838 bool has_pe = hwpipe->caps & MDP_PIPE_CAP_SW_PIX_EXT;
839 const struct mdp_format *format =
840 to_mdp_format(msm_framebuffer_format(fb));
842 mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC_IMG_SIZE(pipe),
843 MDP5_PIPE_SRC_IMG_SIZE_WIDTH(src_img_w) |
844 MDP5_PIPE_SRC_IMG_SIZE_HEIGHT(src_img_h));
846 mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC_SIZE(pipe),
847 MDP5_PIPE_SRC_SIZE_WIDTH(src_w) |
848 MDP5_PIPE_SRC_SIZE_HEIGHT(src_h));
850 mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC_XY(pipe),
851 MDP5_PIPE_SRC_XY_X(src_x) |
852 MDP5_PIPE_SRC_XY_Y(src_y));
854 mdp5_write(mdp5_kms, REG_MDP5_PIPE_OUT_SIZE(pipe),
855 MDP5_PIPE_OUT_SIZE_WIDTH(crtc_w) |
856 MDP5_PIPE_OUT_SIZE_HEIGHT(crtc_h));
858 mdp5_write(mdp5_kms, REG_MDP5_PIPE_OUT_XY(pipe),
859 MDP5_PIPE_OUT_XY_X(crtc_x) |
860 MDP5_PIPE_OUT_XY_Y(crtc_y));
862 mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC_FORMAT(pipe),
863 MDP5_PIPE_SRC_FORMAT_A_BPC(format->bpc_a) |
864 MDP5_PIPE_SRC_FORMAT_R_BPC(format->bpc_r) |
865 MDP5_PIPE_SRC_FORMAT_G_BPC(format->bpc_g) |
866 MDP5_PIPE_SRC_FORMAT_B_BPC(format->bpc_b) |
867 COND(format->alpha_enable, MDP5_PIPE_SRC_FORMAT_ALPHA_ENABLE) |
868 MDP5_PIPE_SRC_FORMAT_CPP(format->cpp - 1) |
869 MDP5_PIPE_SRC_FORMAT_UNPACK_COUNT(format->unpack_count - 1) |
870 COND(format->unpack_tight, MDP5_PIPE_SRC_FORMAT_UNPACK_TIGHT) |
871 MDP5_PIPE_SRC_FORMAT_FETCH_TYPE(format->fetch_type) |
872 MDP5_PIPE_SRC_FORMAT_CHROMA_SAMP(format->chroma_sample));
874 mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC_UNPACK(pipe),
875 MDP5_PIPE_SRC_UNPACK_ELEM0(format->unpack[0]) |
876 MDP5_PIPE_SRC_UNPACK_ELEM1(format->unpack[1]) |
877 MDP5_PIPE_SRC_UNPACK_ELEM2(format->unpack[2]) |
878 MDP5_PIPE_SRC_UNPACK_ELEM3(format->unpack[3]));
880 mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC_OP_MODE(pipe),
881 (hflip ? MDP5_PIPE_SRC_OP_MODE_FLIP_LR : 0) |
882 (vflip ? MDP5_PIPE_SRC_OP_MODE_FLIP_UD : 0) |
883 COND(has_pe, MDP5_PIPE_SRC_OP_MODE_SW_PIX_EXT_OVERRIDE) |
884 MDP5_PIPE_SRC_OP_MODE_BWC(BWC_LOSSLESS));
886 /* not using secure mode: */
887 mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC_ADDR_SW_STATUS(pipe), 0);
889 if (hwpipe->caps & MDP_PIPE_CAP_SW_PIX_EXT)
890 mdp5_write_pixel_ext(mdp5_kms, pipe, format,
891 src_w, pe->left, pe->right,
892 src_h, pe->top, pe->bottom);
894 if (hwpipe->caps & MDP_PIPE_CAP_SCALE) {
895 mdp5_write(mdp5_kms, REG_MDP5_PIPE_SCALE_PHASE_STEP_X(pipe),
897 mdp5_write(mdp5_kms, REG_MDP5_PIPE_SCALE_PHASE_STEP_Y(pipe),
899 mdp5_write(mdp5_kms, REG_MDP5_PIPE_SCALE_CR_PHASE_STEP_X(pipe),
901 mdp5_write(mdp5_kms, REG_MDP5_PIPE_SCALE_CR_PHASE_STEP_Y(pipe),
903 mdp5_write(mdp5_kms, REG_MDP5_PIPE_DECIMATION(pipe),
904 MDP5_PIPE_DECIMATION_VERT(vdecm) |
905 MDP5_PIPE_DECIMATION_HORZ(hdecm));
906 mdp5_write(mdp5_kms, REG_MDP5_PIPE_SCALE_CONFIG(pipe),
910 if (hwpipe->caps & MDP_PIPE_CAP_CSC) {
911 if (MDP_FORMAT_IS_YUV(format))
912 csc_enable(mdp5_kms, pipe,
913 mdp_get_default_csc_cfg(CSC_YUV2RGB));
915 csc_disable(mdp5_kms, pipe);
918 set_scanout_locked(mdp5_kms, pipe, fb);
921 static int mdp5_plane_mode_set(struct drm_plane *plane,
922 struct drm_crtc *crtc, struct drm_framebuffer *fb,
923 struct drm_rect *src, struct drm_rect *dest)
925 struct drm_plane_state *pstate = plane->state;
926 struct mdp5_hw_pipe *hwpipe = to_mdp5_plane_state(pstate)->hwpipe;
927 struct mdp5_kms *mdp5_kms = get_kms(plane);
928 enum mdp5_pipe pipe = hwpipe->pipe;
929 struct mdp5_hw_pipe *right_hwpipe;
930 const struct mdp_format *format;
931 uint32_t nplanes, config = 0;
932 struct phase_step step = { { 0 } };
933 struct pixel_ext pe = { { 0 } };
934 uint32_t hdecm = 0, vdecm = 0;
936 unsigned int rotation;
939 unsigned int crtc_w, crtc_h;
940 uint32_t src_x, src_y;
941 uint32_t src_w, src_h;
942 uint32_t src_img_w, src_img_h;
945 nplanes = fb->format->num_planes;
947 /* bad formats should already be rejected: */
948 if (WARN_ON(nplanes > pipe2nclients(pipe)))
951 format = to_mdp_format(msm_framebuffer_format(fb));
952 pix_format = format->base.pixel_format;
956 src_w = drm_rect_width(src);
957 src_h = drm_rect_height(src);
961 crtc_w = drm_rect_width(dest);
962 crtc_h = drm_rect_height(dest);
964 /* src values are in Q16 fixed point, convert to integer: */
970 src_img_w = min(fb->width, src_w);
971 src_img_h = min(fb->height, src_h);
973 DBG("%s: FB[%u] %u,%u,%u,%u -> CRTC[%u] %d,%d,%u,%u", plane->name,
974 fb->base.id, src_x, src_y, src_w, src_h,
975 crtc->base.id, crtc_x, crtc_y, crtc_w, crtc_h);
977 right_hwpipe = to_mdp5_plane_state(pstate)->r_hwpipe;
980 * if the plane comprises of 2 hw pipes, assume that the width
981 * is split equally across them. The only parameters that varies
982 * between the 2 pipes are src_x and crtc_x
989 ret = calc_scalex_steps(plane, pix_format, src_w, crtc_w, step.x);
993 ret = calc_scaley_steps(plane, pix_format, src_h, crtc_h, step.y);
997 if (hwpipe->caps & MDP_PIPE_CAP_SW_PIX_EXT) {
998 calc_pixel_ext(format, src_w, crtc_w, step.x,
999 pe.left, pe.right, true);
1000 calc_pixel_ext(format, src_h, crtc_h, step.y,
1001 pe.top, pe.bottom, false);
1004 /* TODO calc hdecm, vdecm */
1006 /* SCALE is used to both scale and up-sample chroma components */
1007 config |= get_scale_config(format, src_w, crtc_w, true);
1008 config |= get_scale_config(format, src_h, crtc_h, false);
1009 DBG("scale config = %x", config);
1011 rotation = drm_rotation_simplify(pstate->rotation,
1013 DRM_MODE_REFLECT_X |
1014 DRM_MODE_REFLECT_Y);
1015 hflip = !!(rotation & DRM_MODE_REFLECT_X);
1016 vflip = !!(rotation & DRM_MODE_REFLECT_Y);
1018 mdp5_hwpipe_mode_set(mdp5_kms, hwpipe, fb, &step, &pe,
1019 config, hdecm, vdecm, hflip, vflip,
1020 crtc_x, crtc_y, crtc_w, crtc_h,
1021 src_img_w, src_img_h,
1022 src_x, src_y, src_w, src_h);
1024 mdp5_hwpipe_mode_set(mdp5_kms, right_hwpipe, fb, &step, &pe,
1025 config, hdecm, vdecm, hflip, vflip,
1026 crtc_x + crtc_w, crtc_y, crtc_w, crtc_h,
1027 src_img_w, src_img_h,
1028 src_x + src_w, src_y, src_w, src_h);
1034 * Use this func and the one below only after the atomic state has been
1035 * successfully swapped
1037 enum mdp5_pipe mdp5_plane_pipe(struct drm_plane *plane)
1039 struct mdp5_plane_state *pstate = to_mdp5_plane_state(plane->state);
1041 if (WARN_ON(!pstate->hwpipe))
1044 return pstate->hwpipe->pipe;
1047 enum mdp5_pipe mdp5_plane_right_pipe(struct drm_plane *plane)
1049 struct mdp5_plane_state *pstate = to_mdp5_plane_state(plane->state);
1051 if (!pstate->r_hwpipe)
1054 return pstate->r_hwpipe->pipe;
1057 uint32_t mdp5_plane_get_flush(struct drm_plane *plane)
1059 struct mdp5_plane_state *pstate = to_mdp5_plane_state(plane->state);
1062 if (WARN_ON(!pstate->hwpipe))
1065 mask = pstate->hwpipe->flush_mask;
1067 if (pstate->r_hwpipe)
1068 mask |= pstate->r_hwpipe->flush_mask;
1073 /* initialize plane */
1074 struct drm_plane *mdp5_plane_init(struct drm_device *dev,
1075 enum drm_plane_type type)
1077 struct drm_plane *plane = NULL;
1078 struct mdp5_plane *mdp5_plane;
1081 mdp5_plane = kzalloc(sizeof(*mdp5_plane), GFP_KERNEL);
1087 plane = &mdp5_plane->base;
1089 mdp5_plane->nformats = mdp_get_formats(mdp5_plane->formats,
1090 ARRAY_SIZE(mdp5_plane->formats), false);
1092 ret = drm_universal_plane_init(dev, plane, 0xff, &mdp5_plane_funcs,
1093 mdp5_plane->formats, mdp5_plane->nformats,
1098 drm_plane_helper_add(plane, &mdp5_plane_helper_funcs);
1100 mdp5_plane_install_properties(plane, &plane->base);
1106 mdp5_plane_destroy(plane);
1108 return ERR_PTR(ret);