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_helper_disable(plane);
50 drm_plane_cleanup(plane);
55 static void mdp5_plane_install_rotation_property(struct drm_device *dev,
56 struct drm_plane *plane)
58 drm_plane_create_rotation_property(plane,
66 /* helper to install properties which are common to planes and crtcs */
67 static void mdp5_plane_install_properties(struct drm_plane *plane,
68 struct drm_mode_object *obj)
70 struct drm_device *dev = plane->dev;
71 struct msm_drm_private *dev_priv = dev->dev_private;
72 struct drm_property *prop;
74 #define INSTALL_PROPERTY(name, NAME, init_val, fnc, ...) do { \
75 prop = dev_priv->plane_property[PLANE_PROP_##NAME]; \
77 prop = drm_property_##fnc(dev, 0, #name, \
81 "Create property %s failed\n", \
85 dev_priv->plane_property[PLANE_PROP_##NAME] = prop; \
87 drm_object_attach_property(&plane->base, prop, init_val); \
90 #define INSTALL_RANGE_PROPERTY(name, NAME, min, max, init_val) \
91 INSTALL_PROPERTY(name, NAME, init_val, \
92 create_range, min, max)
94 #define INSTALL_ENUM_PROPERTY(name, NAME, init_val) \
95 INSTALL_PROPERTY(name, NAME, init_val, \
96 create_enum, name##_prop_enum_list, \
97 ARRAY_SIZE(name##_prop_enum_list))
99 INSTALL_RANGE_PROPERTY(zpos, ZPOS, 1, 255, 1);
101 mdp5_plane_install_rotation_property(dev, plane);
103 #undef INSTALL_RANGE_PROPERTY
104 #undef INSTALL_ENUM_PROPERTY
105 #undef INSTALL_PROPERTY
108 static int mdp5_plane_atomic_set_property(struct drm_plane *plane,
109 struct drm_plane_state *state, struct drm_property *property,
112 struct drm_device *dev = plane->dev;
113 struct mdp5_plane_state *pstate;
114 struct msm_drm_private *dev_priv = dev->dev_private;
117 pstate = to_mdp5_plane_state(state);
119 #define SET_PROPERTY(name, NAME, type) do { \
120 if (dev_priv->plane_property[PLANE_PROP_##NAME] == property) { \
121 pstate->name = (type)val; \
122 DBG("Set property %s %d", #name, (type)val); \
127 SET_PROPERTY(zpos, ZPOS, uint8_t);
129 dev_err(dev->dev, "Invalid property\n");
136 static int mdp5_plane_atomic_get_property(struct drm_plane *plane,
137 const struct drm_plane_state *state,
138 struct drm_property *property, uint64_t *val)
140 struct drm_device *dev = plane->dev;
141 struct mdp5_plane_state *pstate;
142 struct msm_drm_private *dev_priv = dev->dev_private;
145 pstate = to_mdp5_plane_state(state);
147 #define GET_PROPERTY(name, NAME, type) do { \
148 if (dev_priv->plane_property[PLANE_PROP_##NAME] == property) { \
149 *val = pstate->name; \
150 DBG("Get property %s %lld", #name, *val); \
155 GET_PROPERTY(zpos, ZPOS, uint8_t);
157 dev_err(dev->dev, "Invalid property\n");
165 mdp5_plane_atomic_print_state(struct drm_printer *p,
166 const struct drm_plane_state *state)
168 struct mdp5_plane_state *pstate = to_mdp5_plane_state(state);
169 struct mdp5_kms *mdp5_kms = get_kms(state->plane);
171 drm_printf(p, "\thwpipe=%s\n", pstate->hwpipe ?
172 pstate->hwpipe->name : "(null)");
173 if (mdp5_kms->caps & MDP_CAP_SRC_SPLIT)
174 drm_printf(p, "\tright-hwpipe=%s\n",
175 pstate->r_hwpipe ? pstate->r_hwpipe->name :
177 drm_printf(p, "\tpremultiplied=%u\n", pstate->premultiplied);
178 drm_printf(p, "\tzpos=%u\n", pstate->zpos);
179 drm_printf(p, "\talpha=%u\n", pstate->alpha);
180 drm_printf(p, "\tstage=%s\n", stage2name(pstate->stage));
183 static void mdp5_plane_reset(struct drm_plane *plane)
185 struct mdp5_plane_state *mdp5_state;
187 if (plane->state && plane->state->fb)
188 drm_framebuffer_unreference(plane->state->fb);
190 kfree(to_mdp5_plane_state(plane->state));
191 mdp5_state = kzalloc(sizeof(*mdp5_state), GFP_KERNEL);
193 /* assign default blend parameters */
194 mdp5_state->alpha = 255;
195 mdp5_state->premultiplied = 0;
197 if (plane->type == DRM_PLANE_TYPE_PRIMARY)
198 mdp5_state->zpos = STAGE_BASE;
200 mdp5_state->zpos = STAGE0 + drm_plane_index(plane);
202 mdp5_state->base.plane = plane;
204 plane->state = &mdp5_state->base;
207 static struct drm_plane_state *
208 mdp5_plane_duplicate_state(struct drm_plane *plane)
210 struct mdp5_plane_state *mdp5_state;
212 if (WARN_ON(!plane->state))
215 mdp5_state = kmemdup(to_mdp5_plane_state(plane->state),
216 sizeof(*mdp5_state), GFP_KERNEL);
220 __drm_atomic_helper_plane_duplicate_state(plane, &mdp5_state->base);
222 return &mdp5_state->base;
225 static void mdp5_plane_destroy_state(struct drm_plane *plane,
226 struct drm_plane_state *state)
228 struct mdp5_plane_state *pstate = to_mdp5_plane_state(state);
231 drm_framebuffer_unreference(state->fb);
236 static const struct drm_plane_funcs mdp5_plane_funcs = {
237 .update_plane = drm_atomic_helper_update_plane,
238 .disable_plane = drm_atomic_helper_disable_plane,
239 .destroy = mdp5_plane_destroy,
240 .atomic_set_property = mdp5_plane_atomic_set_property,
241 .atomic_get_property = mdp5_plane_atomic_get_property,
242 .reset = mdp5_plane_reset,
243 .atomic_duplicate_state = mdp5_plane_duplicate_state,
244 .atomic_destroy_state = mdp5_plane_destroy_state,
245 .atomic_print_state = mdp5_plane_atomic_print_state,
248 static int mdp5_plane_prepare_fb(struct drm_plane *plane,
249 struct drm_plane_state *new_state)
251 struct mdp5_kms *mdp5_kms = get_kms(plane);
252 struct msm_kms *kms = &mdp5_kms->base.base;
253 struct drm_framebuffer *fb = new_state->fb;
258 DBG("%s: prepare: FB[%u]", plane->name, fb->base.id);
259 return msm_framebuffer_prepare(fb, kms->aspace);
262 static void mdp5_plane_cleanup_fb(struct drm_plane *plane,
263 struct drm_plane_state *old_state)
265 struct mdp5_kms *mdp5_kms = get_kms(plane);
266 struct msm_kms *kms = &mdp5_kms->base.base;
267 struct drm_framebuffer *fb = old_state->fb;
272 DBG("%s: cleanup: FB[%u]", plane->name, fb->base.id);
273 msm_framebuffer_cleanup(fb, kms->aspace);
276 #define FRAC_16_16(mult, div) (((mult) << 16) / (div))
277 static int mdp5_plane_atomic_check_with_state(struct drm_crtc_state *crtc_state,
278 struct drm_plane_state *state)
280 struct mdp5_plane_state *mdp5_state = to_mdp5_plane_state(state);
281 struct drm_plane *plane = state->plane;
282 struct drm_plane_state *old_state = plane->state;
283 struct mdp5_cfg *config = mdp5_cfg_get_config(get_kms(plane)->cfg);
284 bool new_hwpipe = false;
285 bool need_right_hwpipe = false;
286 uint32_t max_width, max_height;
287 bool out_of_bounds = false;
289 struct drm_rect clip;
290 int min_scale, max_scale;
293 DBG("%s: check (%d -> %d)", plane->name,
294 plane_enabled(old_state), plane_enabled(state));
296 max_width = config->hw->lm.max_width << 16;
297 max_height = config->hw->lm.max_height << 16;
299 /* Make sure source dimensions are within bounds. */
300 if (state->src_h > max_height)
301 out_of_bounds = true;
303 if (state->src_w > max_width) {
304 /* If source split is supported, we can go up to 2x
305 * the max LM width, but we'd need to stage another
306 * hwpipe to the right LM. So, the drm_plane would
307 * consist of 2 hwpipes.
309 if (config->hw->mdp.caps & MDP_CAP_SRC_SPLIT &&
310 (state->src_w <= 2 * max_width))
311 need_right_hwpipe = true;
313 out_of_bounds = true;
317 struct drm_rect src = drm_plane_state_src(state);
318 DBG("Invalid source size "DRM_RECT_FP_FMT,
319 DRM_RECT_FP_ARG(&src));
325 clip.x2 = crtc_state->adjusted_mode.hdisplay;
326 clip.y2 = crtc_state->adjusted_mode.vdisplay;
327 min_scale = FRAC_16_16(1, 8);
328 max_scale = FRAC_16_16(8, 1);
330 ret = drm_atomic_helper_check_plane_state(state, crtc_state, &clip,
331 min_scale, max_scale,
336 if (plane_enabled(state)) {
337 unsigned int rotation;
338 const struct mdp_format *format;
339 struct mdp5_kms *mdp5_kms = get_kms(plane);
342 format = to_mdp_format(msm_framebuffer_format(state->fb));
343 if (MDP_FORMAT_IS_YUV(format))
344 caps |= MDP_PIPE_CAP_SCALE | MDP_PIPE_CAP_CSC;
346 if (((state->src_w >> 16) != state->crtc_w) ||
347 ((state->src_h >> 16) != state->crtc_h))
348 caps |= MDP_PIPE_CAP_SCALE;
350 rotation = drm_rotation_simplify(state->rotation,
355 if (rotation & DRM_MODE_REFLECT_X)
356 caps |= MDP_PIPE_CAP_HFLIP;
358 if (rotation & DRM_MODE_REFLECT_Y)
359 caps |= MDP_PIPE_CAP_VFLIP;
361 if (plane->type == DRM_PLANE_TYPE_CURSOR)
362 caps |= MDP_PIPE_CAP_CURSOR;
364 /* (re)allocate hw pipe if we don't have one or caps-mismatch: */
365 if (!mdp5_state->hwpipe || (caps & ~mdp5_state->hwpipe->caps))
369 * (re)allocte hw pipe if we're either requesting for 2 hw pipes
370 * or we're switching from 2 hw pipes to 1 hw pipe because the
371 * new src_w can be supported by 1 hw pipe itself.
373 if ((need_right_hwpipe && !mdp5_state->r_hwpipe) ||
374 (!need_right_hwpipe && mdp5_state->r_hwpipe))
378 const struct mdp_format *format =
379 to_mdp_format(msm_framebuffer_format(state->fb));
381 blkcfg = mdp5_smp_calculate(mdp5_kms->smp, format,
382 state->src_w >> 16, false);
384 if (mdp5_state->hwpipe && (mdp5_state->hwpipe->blkcfg != blkcfg))
388 /* (re)assign hwpipe if needed, otherwise keep old one: */
390 /* TODO maybe we want to re-assign hwpipe sometimes
391 * in cases when we no-longer need some caps to make
392 * it available for other planes?
394 struct mdp5_hw_pipe *old_hwpipe = mdp5_state->hwpipe;
395 struct mdp5_hw_pipe *old_right_hwpipe =
396 mdp5_state->r_hwpipe;
397 struct mdp5_hw_pipe *new_hwpipe = NULL;
398 struct mdp5_hw_pipe *new_right_hwpipe = NULL;
400 ret = mdp5_pipe_assign(state->state, plane, caps,
403 &new_right_hwpipe : NULL);
405 DBG("%s: failed to assign hwpipe(s)!",
410 mdp5_state->hwpipe = new_hwpipe;
411 if (need_right_hwpipe)
412 mdp5_state->r_hwpipe = new_right_hwpipe;
415 * set it to NULL so that the driver knows we
416 * don't have a right hwpipe when committing a
419 mdp5_state->r_hwpipe = NULL;
422 mdp5_pipe_release(state->state, old_hwpipe);
423 mdp5_pipe_release(state->state, old_right_hwpipe);
426 mdp5_pipe_release(state->state, mdp5_state->hwpipe);
427 mdp5_pipe_release(state->state, mdp5_state->r_hwpipe);
428 mdp5_state->hwpipe = mdp5_state->r_hwpipe = NULL;
434 static int mdp5_plane_atomic_check(struct drm_plane *plane,
435 struct drm_plane_state *state)
437 struct drm_crtc *crtc;
438 struct drm_crtc_state *crtc_state;
440 crtc = state->crtc ? state->crtc : plane->state->crtc;
444 crtc_state = drm_atomic_get_existing_crtc_state(state->state, crtc);
445 if (WARN_ON(!crtc_state))
448 return mdp5_plane_atomic_check_with_state(crtc_state, state);
451 static void mdp5_plane_atomic_update(struct drm_plane *plane,
452 struct drm_plane_state *old_state)
454 struct drm_plane_state *state = plane->state;
456 DBG("%s: update", plane->name);
458 if (plane_enabled(state)) {
461 ret = mdp5_plane_mode_set(plane,
462 state->crtc, state->fb,
463 &state->src, &state->dst);
464 /* atomic_check should have ensured that this doesn't fail */
469 static int mdp5_plane_atomic_async_check(struct drm_plane *plane,
470 struct drm_plane_state *state)
472 struct mdp5_plane_state *mdp5_state = to_mdp5_plane_state(state);
473 struct drm_crtc_state *crtc_state;
474 struct drm_rect clip;
475 int min_scale, max_scale;
478 crtc_state = drm_atomic_get_existing_crtc_state(state->state,
480 if (WARN_ON(!crtc_state))
483 if (!crtc_state->active)
486 mdp5_state = to_mdp5_plane_state(state);
488 /* don't use fast path if we don't have a hwpipe allocated yet */
489 if (!mdp5_state->hwpipe)
492 /* only allow changing of position(crtc x/y or src x/y) in fast path */
493 if (plane->state->crtc != state->crtc ||
494 plane->state->src_w != state->src_w ||
495 plane->state->src_h != state->src_h ||
496 plane->state->crtc_w != state->crtc_w ||
497 plane->state->crtc_h != state->crtc_h ||
499 plane->state->fb != state->fb)
504 clip.x2 = crtc_state->adjusted_mode.hdisplay;
505 clip.y2 = crtc_state->adjusted_mode.vdisplay;
506 min_scale = FRAC_16_16(1, 8);
507 max_scale = FRAC_16_16(8, 1);
509 ret = drm_atomic_helper_check_plane_state(state, crtc_state, &clip,
510 min_scale, max_scale,
516 * if the visibility of the plane changes (i.e, if the cursor is
517 * clipped out completely, we can't take the async path because
518 * we need to stage/unstage the plane from the Layer Mixer(s). We
519 * also assign/unassign the hwpipe(s) tied to the plane. We avoid
520 * taking the fast path for both these reasons.
522 if (state->visible != plane->state->visible)
528 static void mdp5_plane_atomic_async_update(struct drm_plane *plane,
529 struct drm_plane_state *new_state)
531 plane->state->src_x = new_state->src_x;
532 plane->state->src_y = new_state->src_y;
533 plane->state->crtc_x = new_state->crtc_x;
534 plane->state->crtc_y = new_state->crtc_y;
536 if (plane_enabled(new_state)) {
537 struct mdp5_ctl *ctl;
538 struct mdp5_pipeline *pipeline =
539 mdp5_crtc_get_pipeline(plane->crtc);
542 ret = mdp5_plane_mode_set(plane, new_state->crtc, new_state->fb,
543 &new_state->src, &new_state->dst);
546 ctl = mdp5_crtc_get_ctl(new_state->crtc);
548 mdp5_ctl_commit(ctl, pipeline, mdp5_plane_get_flush(plane));
551 *to_mdp5_plane_state(plane->state) =
552 *to_mdp5_plane_state(new_state);
555 static const struct drm_plane_helper_funcs mdp5_plane_helper_funcs = {
556 .prepare_fb = mdp5_plane_prepare_fb,
557 .cleanup_fb = mdp5_plane_cleanup_fb,
558 .atomic_check = mdp5_plane_atomic_check,
559 .atomic_update = mdp5_plane_atomic_update,
560 .atomic_async_check = mdp5_plane_atomic_async_check,
561 .atomic_async_update = mdp5_plane_atomic_async_update,
564 static void set_scanout_locked(struct mdp5_kms *mdp5_kms,
566 struct drm_framebuffer *fb)
568 struct msm_kms *kms = &mdp5_kms->base.base;
570 mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC_STRIDE_A(pipe),
571 MDP5_PIPE_SRC_STRIDE_A_P0(fb->pitches[0]) |
572 MDP5_PIPE_SRC_STRIDE_A_P1(fb->pitches[1]));
574 mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC_STRIDE_B(pipe),
575 MDP5_PIPE_SRC_STRIDE_B_P2(fb->pitches[2]) |
576 MDP5_PIPE_SRC_STRIDE_B_P3(fb->pitches[3]));
578 mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC0_ADDR(pipe),
579 msm_framebuffer_iova(fb, kms->aspace, 0));
580 mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC1_ADDR(pipe),
581 msm_framebuffer_iova(fb, kms->aspace, 1));
582 mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC2_ADDR(pipe),
583 msm_framebuffer_iova(fb, kms->aspace, 2));
584 mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC3_ADDR(pipe),
585 msm_framebuffer_iova(fb, kms->aspace, 3));
588 /* Note: mdp5_plane->pipe_lock must be locked */
589 static void csc_disable(struct mdp5_kms *mdp5_kms, enum mdp5_pipe pipe)
591 uint32_t value = mdp5_read(mdp5_kms, REG_MDP5_PIPE_OP_MODE(pipe)) &
592 ~MDP5_PIPE_OP_MODE_CSC_1_EN;
594 mdp5_write(mdp5_kms, REG_MDP5_PIPE_OP_MODE(pipe), value);
597 /* Note: mdp5_plane->pipe_lock must be locked */
598 static void csc_enable(struct mdp5_kms *mdp5_kms, enum mdp5_pipe pipe,
601 uint32_t i, mode = 0; /* RGB, no CSC */
607 if ((csc->type == CSC_YUV2RGB) || (CSC_YUV2YUV == csc->type))
608 mode |= MDP5_PIPE_OP_MODE_CSC_SRC_DATA_FORMAT(DATA_FORMAT_YUV);
609 if ((csc->type == CSC_RGB2YUV) || (CSC_YUV2YUV == csc->type))
610 mode |= MDP5_PIPE_OP_MODE_CSC_DST_DATA_FORMAT(DATA_FORMAT_YUV);
611 mode |= MDP5_PIPE_OP_MODE_CSC_1_EN;
612 mdp5_write(mdp5_kms, REG_MDP5_PIPE_OP_MODE(pipe), mode);
614 matrix = csc->matrix;
615 mdp5_write(mdp5_kms, REG_MDP5_PIPE_CSC_1_MATRIX_COEFF_0(pipe),
616 MDP5_PIPE_CSC_1_MATRIX_COEFF_0_COEFF_11(matrix[0]) |
617 MDP5_PIPE_CSC_1_MATRIX_COEFF_0_COEFF_12(matrix[1]));
618 mdp5_write(mdp5_kms, REG_MDP5_PIPE_CSC_1_MATRIX_COEFF_1(pipe),
619 MDP5_PIPE_CSC_1_MATRIX_COEFF_1_COEFF_13(matrix[2]) |
620 MDP5_PIPE_CSC_1_MATRIX_COEFF_1_COEFF_21(matrix[3]));
621 mdp5_write(mdp5_kms, REG_MDP5_PIPE_CSC_1_MATRIX_COEFF_2(pipe),
622 MDP5_PIPE_CSC_1_MATRIX_COEFF_2_COEFF_22(matrix[4]) |
623 MDP5_PIPE_CSC_1_MATRIX_COEFF_2_COEFF_23(matrix[5]));
624 mdp5_write(mdp5_kms, REG_MDP5_PIPE_CSC_1_MATRIX_COEFF_3(pipe),
625 MDP5_PIPE_CSC_1_MATRIX_COEFF_3_COEFF_31(matrix[6]) |
626 MDP5_PIPE_CSC_1_MATRIX_COEFF_3_COEFF_32(matrix[7]));
627 mdp5_write(mdp5_kms, REG_MDP5_PIPE_CSC_1_MATRIX_COEFF_4(pipe),
628 MDP5_PIPE_CSC_1_MATRIX_COEFF_4_COEFF_33(matrix[8]));
630 for (i = 0; i < ARRAY_SIZE(csc->pre_bias); i++) {
631 uint32_t *pre_clamp = csc->pre_clamp;
632 uint32_t *post_clamp = csc->post_clamp;
634 mdp5_write(mdp5_kms, REG_MDP5_PIPE_CSC_1_PRE_CLAMP(pipe, i),
635 MDP5_PIPE_CSC_1_PRE_CLAMP_REG_HIGH(pre_clamp[2*i+1]) |
636 MDP5_PIPE_CSC_1_PRE_CLAMP_REG_LOW(pre_clamp[2*i]));
638 mdp5_write(mdp5_kms, REG_MDP5_PIPE_CSC_1_POST_CLAMP(pipe, i),
639 MDP5_PIPE_CSC_1_POST_CLAMP_REG_HIGH(post_clamp[2*i+1]) |
640 MDP5_PIPE_CSC_1_POST_CLAMP_REG_LOW(post_clamp[2*i]));
642 mdp5_write(mdp5_kms, REG_MDP5_PIPE_CSC_1_PRE_BIAS(pipe, i),
643 MDP5_PIPE_CSC_1_PRE_BIAS_REG_VALUE(csc->pre_bias[i]));
645 mdp5_write(mdp5_kms, REG_MDP5_PIPE_CSC_1_POST_BIAS(pipe, i),
646 MDP5_PIPE_CSC_1_POST_BIAS_REG_VALUE(csc->post_bias[i]));
650 #define PHASE_STEP_SHIFT 21
651 #define DOWN_SCALE_RATIO_MAX 32 /* 2^(26-21) */
653 static int calc_phase_step(uint32_t src, uint32_t dst, uint32_t *out_phase)
657 if (src == 0 || dst == 0)
661 * PHASE_STEP_X/Y is coded on 26 bits (25:0),
662 * where 2^21 represents the unity "1" in fixed-point hardware design.
663 * This leaves 5 bits for the integer part (downscale case):
664 * -> maximum downscale ratio = 0b1_1111 = 31
666 if (src > (dst * DOWN_SCALE_RATIO_MAX))
669 unit = 1 << PHASE_STEP_SHIFT;
670 *out_phase = mult_frac(unit, src, dst);
675 static int calc_scalex_steps(struct drm_plane *plane,
676 uint32_t pixel_format, uint32_t src, uint32_t dest,
677 uint32_t phasex_steps[COMP_MAX])
679 struct mdp5_kms *mdp5_kms = get_kms(plane);
680 struct device *dev = mdp5_kms->dev->dev;
681 uint32_t phasex_step;
685 ret = calc_phase_step(src, dest, &phasex_step);
687 dev_err(dev, "X scaling (%d->%d) failed: %d\n", src, dest, ret);
691 hsub = drm_format_horz_chroma_subsampling(pixel_format);
693 phasex_steps[COMP_0] = phasex_step;
694 phasex_steps[COMP_3] = phasex_step;
695 phasex_steps[COMP_1_2] = phasex_step / hsub;
700 static int calc_scaley_steps(struct drm_plane *plane,
701 uint32_t pixel_format, uint32_t src, uint32_t dest,
702 uint32_t phasey_steps[COMP_MAX])
704 struct mdp5_kms *mdp5_kms = get_kms(plane);
705 struct device *dev = mdp5_kms->dev->dev;
706 uint32_t phasey_step;
710 ret = calc_phase_step(src, dest, &phasey_step);
712 dev_err(dev, "Y scaling (%d->%d) failed: %d\n", src, dest, ret);
716 vsub = drm_format_vert_chroma_subsampling(pixel_format);
718 phasey_steps[COMP_0] = phasey_step;
719 phasey_steps[COMP_3] = phasey_step;
720 phasey_steps[COMP_1_2] = phasey_step / vsub;
725 static uint32_t get_scale_config(const struct mdp_format *format,
726 uint32_t src, uint32_t dst, bool horz)
728 bool scaling = format->is_yuv ? true : (src != dst);
729 uint32_t sub, pix_fmt = format->base.pixel_format;
730 uint32_t ya_filter, uv_filter;
731 bool yuv = format->is_yuv;
737 sub = horz ? drm_format_horz_chroma_subsampling(pix_fmt) :
738 drm_format_vert_chroma_subsampling(pix_fmt);
739 uv_filter = ((src / sub) <= dst) ?
740 SCALE_FILTER_BIL : SCALE_FILTER_PCMN;
742 ya_filter = (src <= dst) ? SCALE_FILTER_BIL : SCALE_FILTER_PCMN;
745 return MDP5_PIPE_SCALE_CONFIG_SCALEX_EN |
746 MDP5_PIPE_SCALE_CONFIG_SCALEX_FILTER_COMP_0(ya_filter) |
747 MDP5_PIPE_SCALE_CONFIG_SCALEX_FILTER_COMP_3(ya_filter) |
748 COND(yuv, MDP5_PIPE_SCALE_CONFIG_SCALEX_FILTER_COMP_1_2(uv_filter));
750 return MDP5_PIPE_SCALE_CONFIG_SCALEY_EN |
751 MDP5_PIPE_SCALE_CONFIG_SCALEY_FILTER_COMP_0(ya_filter) |
752 MDP5_PIPE_SCALE_CONFIG_SCALEY_FILTER_COMP_3(ya_filter) |
753 COND(yuv, MDP5_PIPE_SCALE_CONFIG_SCALEY_FILTER_COMP_1_2(uv_filter));
756 static void calc_pixel_ext(const struct mdp_format *format,
757 uint32_t src, uint32_t dst, uint32_t phase_step[2],
758 int pix_ext_edge1[COMP_MAX], int pix_ext_edge2[COMP_MAX],
761 bool scaling = format->is_yuv ? true : (src != dst);
766 * We assume here that:
767 * 1. PCMN filter is used for downscale
768 * 2. bilinear filter is used for upscale
769 * 3. we are in a single pipe configuration
772 for (i = 0; i < COMP_MAX; i++) {
773 pix_ext_edge1[i] = 0;
774 pix_ext_edge2[i] = scaling ? 1 : 0;
778 static void mdp5_write_pixel_ext(struct mdp5_kms *mdp5_kms, enum mdp5_pipe pipe,
779 const struct mdp_format *format,
780 uint32_t src_w, int pe_left[COMP_MAX], int pe_right[COMP_MAX],
781 uint32_t src_h, int pe_top[COMP_MAX], int pe_bottom[COMP_MAX])
783 uint32_t pix_fmt = format->base.pixel_format;
784 uint32_t lr, tb, req;
787 for (i = 0; i < COMP_MAX; i++) {
788 uint32_t roi_w = src_w;
789 uint32_t roi_h = src_h;
791 if (format->is_yuv && i == COMP_1_2) {
792 roi_w /= drm_format_horz_chroma_subsampling(pix_fmt);
793 roi_h /= drm_format_vert_chroma_subsampling(pix_fmt);
796 lr = (pe_left[i] >= 0) ?
797 MDP5_PIPE_SW_PIX_EXT_LR_LEFT_RPT(pe_left[i]) :
798 MDP5_PIPE_SW_PIX_EXT_LR_LEFT_OVF(pe_left[i]);
800 lr |= (pe_right[i] >= 0) ?
801 MDP5_PIPE_SW_PIX_EXT_LR_RIGHT_RPT(pe_right[i]) :
802 MDP5_PIPE_SW_PIX_EXT_LR_RIGHT_OVF(pe_right[i]);
804 tb = (pe_top[i] >= 0) ?
805 MDP5_PIPE_SW_PIX_EXT_TB_TOP_RPT(pe_top[i]) :
806 MDP5_PIPE_SW_PIX_EXT_TB_TOP_OVF(pe_top[i]);
808 tb |= (pe_bottom[i] >= 0) ?
809 MDP5_PIPE_SW_PIX_EXT_TB_BOTTOM_RPT(pe_bottom[i]) :
810 MDP5_PIPE_SW_PIX_EXT_TB_BOTTOM_OVF(pe_bottom[i]);
812 req = MDP5_PIPE_SW_PIX_EXT_REQ_PIXELS_LEFT_RIGHT(roi_w +
813 pe_left[i] + pe_right[i]);
815 req |= MDP5_PIPE_SW_PIX_EXT_REQ_PIXELS_TOP_BOTTOM(roi_h +
816 pe_top[i] + pe_bottom[i]);
818 mdp5_write(mdp5_kms, REG_MDP5_PIPE_SW_PIX_EXT_LR(pipe, i), lr);
819 mdp5_write(mdp5_kms, REG_MDP5_PIPE_SW_PIX_EXT_TB(pipe, i), tb);
820 mdp5_write(mdp5_kms, REG_MDP5_PIPE_SW_PIX_EXT_REQ_PIXELS(pipe, i), req);
822 DBG("comp-%d (L/R): rpt=%d/%d, ovf=%d/%d, req=%d", i,
823 FIELD(lr, MDP5_PIPE_SW_PIX_EXT_LR_LEFT_RPT),
824 FIELD(lr, MDP5_PIPE_SW_PIX_EXT_LR_RIGHT_RPT),
825 FIELD(lr, MDP5_PIPE_SW_PIX_EXT_LR_LEFT_OVF),
826 FIELD(lr, MDP5_PIPE_SW_PIX_EXT_LR_RIGHT_OVF),
827 FIELD(req, MDP5_PIPE_SW_PIX_EXT_REQ_PIXELS_LEFT_RIGHT));
829 DBG("comp-%d (T/B): rpt=%d/%d, ovf=%d/%d, req=%d", i,
830 FIELD(tb, MDP5_PIPE_SW_PIX_EXT_TB_TOP_RPT),
831 FIELD(tb, MDP5_PIPE_SW_PIX_EXT_TB_BOTTOM_RPT),
832 FIELD(tb, MDP5_PIPE_SW_PIX_EXT_TB_TOP_OVF),
833 FIELD(tb, MDP5_PIPE_SW_PIX_EXT_TB_BOTTOM_OVF),
834 FIELD(req, MDP5_PIPE_SW_PIX_EXT_REQ_PIXELS_TOP_BOTTOM));
842 int bottom[COMP_MAX];
850 static void mdp5_hwpipe_mode_set(struct mdp5_kms *mdp5_kms,
851 struct mdp5_hw_pipe *hwpipe,
852 struct drm_framebuffer *fb,
853 struct phase_step *step,
854 struct pixel_ext *pe,
855 u32 scale_config, u32 hdecm, u32 vdecm,
856 bool hflip, bool vflip,
857 int crtc_x, int crtc_y,
858 unsigned int crtc_w, unsigned int crtc_h,
859 u32 src_img_w, u32 src_img_h,
860 u32 src_x, u32 src_y,
861 u32 src_w, u32 src_h)
863 enum mdp5_pipe pipe = hwpipe->pipe;
864 bool has_pe = hwpipe->caps & MDP_PIPE_CAP_SW_PIX_EXT;
865 const struct mdp_format *format =
866 to_mdp_format(msm_framebuffer_format(fb));
868 mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC_IMG_SIZE(pipe),
869 MDP5_PIPE_SRC_IMG_SIZE_WIDTH(src_img_w) |
870 MDP5_PIPE_SRC_IMG_SIZE_HEIGHT(src_img_h));
872 mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC_SIZE(pipe),
873 MDP5_PIPE_SRC_SIZE_WIDTH(src_w) |
874 MDP5_PIPE_SRC_SIZE_HEIGHT(src_h));
876 mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC_XY(pipe),
877 MDP5_PIPE_SRC_XY_X(src_x) |
878 MDP5_PIPE_SRC_XY_Y(src_y));
880 mdp5_write(mdp5_kms, REG_MDP5_PIPE_OUT_SIZE(pipe),
881 MDP5_PIPE_OUT_SIZE_WIDTH(crtc_w) |
882 MDP5_PIPE_OUT_SIZE_HEIGHT(crtc_h));
884 mdp5_write(mdp5_kms, REG_MDP5_PIPE_OUT_XY(pipe),
885 MDP5_PIPE_OUT_XY_X(crtc_x) |
886 MDP5_PIPE_OUT_XY_Y(crtc_y));
888 mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC_FORMAT(pipe),
889 MDP5_PIPE_SRC_FORMAT_A_BPC(format->bpc_a) |
890 MDP5_PIPE_SRC_FORMAT_R_BPC(format->bpc_r) |
891 MDP5_PIPE_SRC_FORMAT_G_BPC(format->bpc_g) |
892 MDP5_PIPE_SRC_FORMAT_B_BPC(format->bpc_b) |
893 COND(format->alpha_enable, MDP5_PIPE_SRC_FORMAT_ALPHA_ENABLE) |
894 MDP5_PIPE_SRC_FORMAT_CPP(format->cpp - 1) |
895 MDP5_PIPE_SRC_FORMAT_UNPACK_COUNT(format->unpack_count - 1) |
896 COND(format->unpack_tight, MDP5_PIPE_SRC_FORMAT_UNPACK_TIGHT) |
897 MDP5_PIPE_SRC_FORMAT_FETCH_TYPE(format->fetch_type) |
898 MDP5_PIPE_SRC_FORMAT_CHROMA_SAMP(format->chroma_sample));
900 mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC_UNPACK(pipe),
901 MDP5_PIPE_SRC_UNPACK_ELEM0(format->unpack[0]) |
902 MDP5_PIPE_SRC_UNPACK_ELEM1(format->unpack[1]) |
903 MDP5_PIPE_SRC_UNPACK_ELEM2(format->unpack[2]) |
904 MDP5_PIPE_SRC_UNPACK_ELEM3(format->unpack[3]));
906 mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC_OP_MODE(pipe),
907 (hflip ? MDP5_PIPE_SRC_OP_MODE_FLIP_LR : 0) |
908 (vflip ? MDP5_PIPE_SRC_OP_MODE_FLIP_UD : 0) |
909 COND(has_pe, MDP5_PIPE_SRC_OP_MODE_SW_PIX_EXT_OVERRIDE) |
910 MDP5_PIPE_SRC_OP_MODE_BWC(BWC_LOSSLESS));
912 /* not using secure mode: */
913 mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC_ADDR_SW_STATUS(pipe), 0);
915 if (hwpipe->caps & MDP_PIPE_CAP_SW_PIX_EXT)
916 mdp5_write_pixel_ext(mdp5_kms, pipe, format,
917 src_w, pe->left, pe->right,
918 src_h, pe->top, pe->bottom);
920 if (hwpipe->caps & MDP_PIPE_CAP_SCALE) {
921 mdp5_write(mdp5_kms, REG_MDP5_PIPE_SCALE_PHASE_STEP_X(pipe),
923 mdp5_write(mdp5_kms, REG_MDP5_PIPE_SCALE_PHASE_STEP_Y(pipe),
925 mdp5_write(mdp5_kms, REG_MDP5_PIPE_SCALE_CR_PHASE_STEP_X(pipe),
927 mdp5_write(mdp5_kms, REG_MDP5_PIPE_SCALE_CR_PHASE_STEP_Y(pipe),
929 mdp5_write(mdp5_kms, REG_MDP5_PIPE_DECIMATION(pipe),
930 MDP5_PIPE_DECIMATION_VERT(vdecm) |
931 MDP5_PIPE_DECIMATION_HORZ(hdecm));
932 mdp5_write(mdp5_kms, REG_MDP5_PIPE_SCALE_CONFIG(pipe),
936 if (hwpipe->caps & MDP_PIPE_CAP_CSC) {
937 if (MDP_FORMAT_IS_YUV(format))
938 csc_enable(mdp5_kms, pipe,
939 mdp_get_default_csc_cfg(CSC_YUV2RGB));
941 csc_disable(mdp5_kms, pipe);
944 set_scanout_locked(mdp5_kms, pipe, fb);
947 static int mdp5_plane_mode_set(struct drm_plane *plane,
948 struct drm_crtc *crtc, struct drm_framebuffer *fb,
949 struct drm_rect *src, struct drm_rect *dest)
951 struct drm_plane_state *pstate = plane->state;
952 struct mdp5_hw_pipe *hwpipe = to_mdp5_plane_state(pstate)->hwpipe;
953 struct mdp5_kms *mdp5_kms = get_kms(plane);
954 enum mdp5_pipe pipe = hwpipe->pipe;
955 struct mdp5_hw_pipe *right_hwpipe;
956 const struct mdp_format *format;
957 uint32_t nplanes, config = 0;
958 struct phase_step step = { { 0 } };
959 struct pixel_ext pe = { { 0 } };
960 uint32_t hdecm = 0, vdecm = 0;
962 unsigned int rotation;
965 unsigned int crtc_w, crtc_h;
966 uint32_t src_x, src_y;
967 uint32_t src_w, src_h;
968 uint32_t src_img_w, src_img_h;
973 nplanes = fb->format->num_planes;
975 /* bad formats should already be rejected: */
976 if (WARN_ON(nplanes > pipe2nclients(pipe)))
979 format = to_mdp_format(msm_framebuffer_format(fb));
980 pix_format = format->base.pixel_format;
984 src_w = drm_rect_width(src);
985 src_h = drm_rect_height(src);
989 crtc_w = drm_rect_width(dest);
990 crtc_h = drm_rect_height(dest);
992 /* src values are in Q16 fixed point, convert to integer: */
998 src_img_w = min(fb->width, src_w);
999 src_img_h = min(fb->height, src_h);
1001 DBG("%s: FB[%u] %u,%u,%u,%u -> CRTC[%u] %d,%d,%u,%u", plane->name,
1002 fb->base.id, src_x, src_y, src_w, src_h,
1003 crtc->base.id, crtc_x, crtc_y, crtc_w, crtc_h);
1005 right_hwpipe = to_mdp5_plane_state(pstate)->r_hwpipe;
1008 * if the plane comprises of 2 hw pipes, assume that the width
1009 * is split equally across them. The only parameters that varies
1010 * between the 2 pipes are src_x and crtc_x
1016 crtc_x_r = crtc_x + crtc_w;
1017 src_x_r = src_x + src_w;
1020 ret = calc_scalex_steps(plane, pix_format, src_w, crtc_w, step.x);
1024 ret = calc_scaley_steps(plane, pix_format, src_h, crtc_h, step.y);
1028 if (hwpipe->caps & MDP_PIPE_CAP_SW_PIX_EXT) {
1029 calc_pixel_ext(format, src_w, crtc_w, step.x,
1030 pe.left, pe.right, true);
1031 calc_pixel_ext(format, src_h, crtc_h, step.y,
1032 pe.top, pe.bottom, false);
1035 /* TODO calc hdecm, vdecm */
1037 /* SCALE is used to both scale and up-sample chroma components */
1038 config |= get_scale_config(format, src_w, crtc_w, true);
1039 config |= get_scale_config(format, src_h, crtc_h, false);
1040 DBG("scale config = %x", config);
1042 rotation = drm_rotation_simplify(pstate->rotation,
1044 DRM_MODE_REFLECT_X |
1045 DRM_MODE_REFLECT_Y);
1046 hflip = !!(rotation & DRM_MODE_REFLECT_X);
1047 vflip = !!(rotation & DRM_MODE_REFLECT_Y);
1049 mdp5_hwpipe_mode_set(mdp5_kms, hwpipe, fb, &step, &pe,
1050 config, hdecm, vdecm, hflip, vflip,
1051 crtc_x, crtc_y, crtc_w, crtc_h,
1052 src_img_w, src_img_h,
1053 src_x, src_y, src_w, src_h);
1055 mdp5_hwpipe_mode_set(mdp5_kms, right_hwpipe, fb, &step, &pe,
1056 config, hdecm, vdecm, hflip, vflip,
1057 crtc_x_r, crtc_y, crtc_w, crtc_h,
1058 src_img_w, src_img_h,
1059 src_x_r, src_y, src_w, src_h);
1067 * Use this func and the one below only after the atomic state has been
1068 * successfully swapped
1070 enum mdp5_pipe mdp5_plane_pipe(struct drm_plane *plane)
1072 struct mdp5_plane_state *pstate = to_mdp5_plane_state(plane->state);
1074 if (WARN_ON(!pstate->hwpipe))
1077 return pstate->hwpipe->pipe;
1080 enum mdp5_pipe mdp5_plane_right_pipe(struct drm_plane *plane)
1082 struct mdp5_plane_state *pstate = to_mdp5_plane_state(plane->state);
1084 if (!pstate->r_hwpipe)
1087 return pstate->r_hwpipe->pipe;
1090 uint32_t mdp5_plane_get_flush(struct drm_plane *plane)
1092 struct mdp5_plane_state *pstate = to_mdp5_plane_state(plane->state);
1095 if (WARN_ON(!pstate->hwpipe))
1098 mask = pstate->hwpipe->flush_mask;
1100 if (pstate->r_hwpipe)
1101 mask |= pstate->r_hwpipe->flush_mask;
1106 /* initialize plane */
1107 struct drm_plane *mdp5_plane_init(struct drm_device *dev,
1108 enum drm_plane_type type)
1110 struct drm_plane *plane = NULL;
1111 struct mdp5_plane *mdp5_plane;
1114 mdp5_plane = kzalloc(sizeof(*mdp5_plane), GFP_KERNEL);
1120 plane = &mdp5_plane->base;
1122 mdp5_plane->nformats = mdp_get_formats(mdp5_plane->formats,
1123 ARRAY_SIZE(mdp5_plane->formats), false);
1125 ret = drm_universal_plane_init(dev, plane, 0xff, &mdp5_plane_funcs,
1126 mdp5_plane->formats, mdp5_plane->nformats,
1131 drm_plane_helper_add(plane, &mdp5_plane_helper_funcs);
1133 mdp5_plane_install_properties(plane, &plane->base);
1139 mdp5_plane_destroy(plane);
1141 return ERR_PTR(ret);