2 * Copyright (C) 2015 Broadcom
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
10 * DOC: VC4 plane module
12 * Each DRM plane is a layer of pixels being scanned out by the HVS.
14 * At atomic modeset check time, we compute the HVS display element
15 * state that would be necessary for displaying the plane (giving us a
16 * chance to figure out if a plane configuration is invalid), then at
17 * atomic flush time the CRTC will ask us to write our element state
18 * into the region of the HVS that it has allocated for us.
21 #include <drm/drm_atomic.h>
22 #include <drm/drm_atomic_helper.h>
23 #include <drm/drm_fb_cma_helper.h>
24 #include <drm/drm_plane_helper.h>
26 #include "uapi/drm/vc4_drm.h"
30 enum vc4_scaling_mode {
36 struct vc4_plane_state {
37 struct drm_plane_state base;
38 /* System memory copy of the display list for this element, computed
39 * at atomic_check time.
42 u32 dlist_size; /* Number of dwords allocated for the display list */
43 u32 dlist_count; /* Number of used dwords in the display list. */
45 /* Offset in the dlist to various words, for pageflip or
52 /* Offset where the plane's dlist was last stored in the
53 * hardware at vc4_crtc_atomic_flush() time.
55 u32 __iomem *hw_dlist;
57 /* Clipped coordinates of the plane on the display. */
58 int crtc_x, crtc_y, crtc_w, crtc_h;
59 /* Clipped area being scanned from in the FB. */
62 u32 src_w[2], src_h[2];
64 /* Scaling selection for the RGB/Y plane and the Cb/Cr planes. */
65 enum vc4_scaling_mode x_scaling[2], y_scaling[2];
69 /* Offset to start scanning out from the start of the plane's
74 /* Our allocation in LBM for temporary storage during scaling. */
75 struct drm_mm_node lbm;
78 static inline struct vc4_plane_state *
79 to_vc4_plane_state(struct drm_plane_state *state)
81 return (struct vc4_plane_state *)state;
84 static const struct hvs_format {
85 u32 drm; /* DRM_FORMAT_* */
86 u32 hvs; /* HVS_FORMAT_* */
92 .drm = DRM_FORMAT_XRGB8888, .hvs = HVS_PIXEL_FORMAT_RGBA8888,
93 .pixel_order = HVS_PIXEL_ORDER_ABGR, .has_alpha = false,
96 .drm = DRM_FORMAT_ARGB8888, .hvs = HVS_PIXEL_FORMAT_RGBA8888,
97 .pixel_order = HVS_PIXEL_ORDER_ABGR, .has_alpha = true,
100 .drm = DRM_FORMAT_ABGR8888, .hvs = HVS_PIXEL_FORMAT_RGBA8888,
101 .pixel_order = HVS_PIXEL_ORDER_ARGB, .has_alpha = true,
104 .drm = DRM_FORMAT_XBGR8888, .hvs = HVS_PIXEL_FORMAT_RGBA8888,
105 .pixel_order = HVS_PIXEL_ORDER_ARGB, .has_alpha = false,
108 .drm = DRM_FORMAT_RGB565, .hvs = HVS_PIXEL_FORMAT_RGB565,
109 .pixel_order = HVS_PIXEL_ORDER_XRGB, .has_alpha = false,
112 .drm = DRM_FORMAT_BGR565, .hvs = HVS_PIXEL_FORMAT_RGB565,
113 .pixel_order = HVS_PIXEL_ORDER_XBGR, .has_alpha = false,
116 .drm = DRM_FORMAT_ARGB1555, .hvs = HVS_PIXEL_FORMAT_RGBA5551,
117 .pixel_order = HVS_PIXEL_ORDER_ABGR, .has_alpha = true,
120 .drm = DRM_FORMAT_XRGB1555, .hvs = HVS_PIXEL_FORMAT_RGBA5551,
121 .pixel_order = HVS_PIXEL_ORDER_ABGR, .has_alpha = false,
124 .drm = DRM_FORMAT_YUV422,
125 .hvs = HVS_PIXEL_FORMAT_YCBCR_YUV422_3PLANE,
128 .drm = DRM_FORMAT_YVU422,
129 .hvs = HVS_PIXEL_FORMAT_YCBCR_YUV422_3PLANE,
133 .drm = DRM_FORMAT_YUV420,
134 .hvs = HVS_PIXEL_FORMAT_YCBCR_YUV420_3PLANE,
137 .drm = DRM_FORMAT_YVU420,
138 .hvs = HVS_PIXEL_FORMAT_YCBCR_YUV420_3PLANE,
142 .drm = DRM_FORMAT_NV12,
143 .hvs = HVS_PIXEL_FORMAT_YCBCR_YUV420_2PLANE,
146 .drm = DRM_FORMAT_NV16,
147 .hvs = HVS_PIXEL_FORMAT_YCBCR_YUV422_2PLANE,
151 static const struct hvs_format *vc4_get_hvs_format(u32 drm_format)
155 for (i = 0; i < ARRAY_SIZE(hvs_formats); i++) {
156 if (hvs_formats[i].drm == drm_format)
157 return &hvs_formats[i];
163 static enum vc4_scaling_mode vc4_get_scaling_mode(u32 src, u32 dst)
166 return VC4_SCALING_PPF;
168 return VC4_SCALING_TPZ;
170 return VC4_SCALING_NONE;
173 static bool plane_enabled(struct drm_plane_state *state)
175 return state->fb && state->crtc;
178 static struct drm_plane_state *vc4_plane_duplicate_state(struct drm_plane *plane)
180 struct vc4_plane_state *vc4_state;
182 if (WARN_ON(!plane->state))
185 vc4_state = kmemdup(plane->state, sizeof(*vc4_state), GFP_KERNEL);
189 memset(&vc4_state->lbm, 0, sizeof(vc4_state->lbm));
191 __drm_atomic_helper_plane_duplicate_state(plane, &vc4_state->base);
193 if (vc4_state->dlist) {
194 vc4_state->dlist = kmemdup(vc4_state->dlist,
195 vc4_state->dlist_count * 4,
197 if (!vc4_state->dlist) {
201 vc4_state->dlist_size = vc4_state->dlist_count;
204 return &vc4_state->base;
207 static void vc4_plane_destroy_state(struct drm_plane *plane,
208 struct drm_plane_state *state)
210 struct vc4_dev *vc4 = to_vc4_dev(plane->dev);
211 struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
213 if (vc4_state->lbm.allocated) {
214 unsigned long irqflags;
216 spin_lock_irqsave(&vc4->hvs->mm_lock, irqflags);
217 drm_mm_remove_node(&vc4_state->lbm);
218 spin_unlock_irqrestore(&vc4->hvs->mm_lock, irqflags);
221 kfree(vc4_state->dlist);
222 __drm_atomic_helper_plane_destroy_state(&vc4_state->base);
226 /* Called during init to allocate the plane's atomic state. */
227 static void vc4_plane_reset(struct drm_plane *plane)
229 struct vc4_plane_state *vc4_state;
231 WARN_ON(plane->state);
233 vc4_state = kzalloc(sizeof(*vc4_state), GFP_KERNEL);
237 plane->state = &vc4_state->base;
238 vc4_state->base.plane = plane;
241 static void vc4_dlist_write(struct vc4_plane_state *vc4_state, u32 val)
243 if (vc4_state->dlist_count == vc4_state->dlist_size) {
244 u32 new_size = max(4u, vc4_state->dlist_count * 2);
245 u32 *new_dlist = kmalloc(new_size * 4, GFP_KERNEL);
249 memcpy(new_dlist, vc4_state->dlist, vc4_state->dlist_count * 4);
251 kfree(vc4_state->dlist);
252 vc4_state->dlist = new_dlist;
253 vc4_state->dlist_size = new_size;
256 vc4_state->dlist[vc4_state->dlist_count++] = val;
259 /* Returns the scl0/scl1 field based on whether the dimensions need to
260 * be up/down/non-scaled.
262 * This is a replication of a table from the spec.
264 static u32 vc4_get_scl_field(struct drm_plane_state *state, int plane)
266 struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
268 switch (vc4_state->x_scaling[plane] << 2 | vc4_state->y_scaling[plane]) {
269 case VC4_SCALING_PPF << 2 | VC4_SCALING_PPF:
270 return SCALER_CTL0_SCL_H_PPF_V_PPF;
271 case VC4_SCALING_TPZ << 2 | VC4_SCALING_PPF:
272 return SCALER_CTL0_SCL_H_TPZ_V_PPF;
273 case VC4_SCALING_PPF << 2 | VC4_SCALING_TPZ:
274 return SCALER_CTL0_SCL_H_PPF_V_TPZ;
275 case VC4_SCALING_TPZ << 2 | VC4_SCALING_TPZ:
276 return SCALER_CTL0_SCL_H_TPZ_V_TPZ;
277 case VC4_SCALING_PPF << 2 | VC4_SCALING_NONE:
278 return SCALER_CTL0_SCL_H_PPF_V_NONE;
279 case VC4_SCALING_NONE << 2 | VC4_SCALING_PPF:
280 return SCALER_CTL0_SCL_H_NONE_V_PPF;
281 case VC4_SCALING_NONE << 2 | VC4_SCALING_TPZ:
282 return SCALER_CTL0_SCL_H_NONE_V_TPZ;
283 case VC4_SCALING_TPZ << 2 | VC4_SCALING_NONE:
284 return SCALER_CTL0_SCL_H_TPZ_V_NONE;
286 case VC4_SCALING_NONE << 2 | VC4_SCALING_NONE:
287 /* The unity case is independently handled by
294 static int vc4_plane_setup_clipping_and_scaling(struct drm_plane_state *state)
296 struct drm_plane *plane = state->plane;
297 struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
298 struct drm_framebuffer *fb = state->fb;
299 struct drm_gem_cma_object *bo = drm_fb_cma_get_gem_obj(fb, 0);
300 u32 subpixel_src_mask = (1 << 16) - 1;
301 u32 format = fb->format->format;
302 int num_planes = fb->format->num_planes;
307 for (i = 0; i < num_planes; i++)
308 vc4_state->offsets[i] = bo->paddr + fb->offsets[i];
310 /* We don't support subpixel source positioning for scaling. */
311 if ((state->src_x & subpixel_src_mask) ||
312 (state->src_y & subpixel_src_mask) ||
313 (state->src_w & subpixel_src_mask) ||
314 (state->src_h & subpixel_src_mask)) {
318 vc4_state->src_x = state->src_x >> 16;
319 vc4_state->src_y = state->src_y >> 16;
320 vc4_state->src_w[0] = state->src_w >> 16;
321 vc4_state->src_h[0] = state->src_h >> 16;
323 vc4_state->crtc_x = state->crtc_x;
324 vc4_state->crtc_y = state->crtc_y;
325 vc4_state->crtc_w = state->crtc_w;
326 vc4_state->crtc_h = state->crtc_h;
328 vc4_state->x_scaling[0] = vc4_get_scaling_mode(vc4_state->src_w[0],
330 vc4_state->y_scaling[0] = vc4_get_scaling_mode(vc4_state->src_h[0],
333 if (num_planes > 1) {
334 vc4_state->is_yuv = true;
336 h_subsample = drm_format_horz_chroma_subsampling(format);
337 v_subsample = drm_format_vert_chroma_subsampling(format);
338 vc4_state->src_w[1] = vc4_state->src_w[0] / h_subsample;
339 vc4_state->src_h[1] = vc4_state->src_h[0] / v_subsample;
341 vc4_state->x_scaling[1] =
342 vc4_get_scaling_mode(vc4_state->src_w[1],
344 vc4_state->y_scaling[1] =
345 vc4_get_scaling_mode(vc4_state->src_h[1],
348 /* YUV conversion requires that scaling be enabled,
349 * even on a plane that's otherwise 1:1. Choose TPZ
352 if (vc4_state->x_scaling[0] == VC4_SCALING_NONE)
353 vc4_state->x_scaling[0] = VC4_SCALING_TPZ;
354 if (vc4_state->y_scaling[0] == VC4_SCALING_NONE)
355 vc4_state->y_scaling[0] = VC4_SCALING_TPZ;
358 vc4_state->is_unity = (vc4_state->x_scaling[0] == VC4_SCALING_NONE &&
359 vc4_state->y_scaling[0] == VC4_SCALING_NONE &&
360 vc4_state->x_scaling[1] == VC4_SCALING_NONE &&
361 vc4_state->y_scaling[1] == VC4_SCALING_NONE);
363 /* No configuring scaling on the cursor plane, since it gets
364 non-vblank-synced updates, and scaling requires requires
365 LBM changes which have to be vblank-synced.
367 if (plane->type == DRM_PLANE_TYPE_CURSOR && !vc4_state->is_unity)
370 /* Clamp the on-screen start x/y to 0. The hardware doesn't
371 * support negative y, and negative x wastes bandwidth.
373 if (vc4_state->crtc_x < 0) {
374 for (i = 0; i < num_planes; i++) {
375 u32 cpp = fb->format->cpp[i];
376 u32 subs = ((i == 0) ? 1 : h_subsample);
378 vc4_state->offsets[i] += (cpp *
379 (-vc4_state->crtc_x) / subs);
381 vc4_state->src_w[0] += vc4_state->crtc_x;
382 vc4_state->src_w[1] += vc4_state->crtc_x / h_subsample;
383 vc4_state->crtc_x = 0;
386 if (vc4_state->crtc_y < 0) {
387 for (i = 0; i < num_planes; i++) {
388 u32 subs = ((i == 0) ? 1 : v_subsample);
390 vc4_state->offsets[i] += (fb->pitches[i] *
391 (-vc4_state->crtc_y) / subs);
393 vc4_state->src_h[0] += vc4_state->crtc_y;
394 vc4_state->src_h[1] += vc4_state->crtc_y / v_subsample;
395 vc4_state->crtc_y = 0;
401 static void vc4_write_tpz(struct vc4_plane_state *vc4_state, u32 src, u32 dst)
405 scale = (1 << 16) * src / dst;
407 /* The specs note that while the reciprocal would be defined
408 * as (1<<32)/scale, ~0 is close enough.
412 vc4_dlist_write(vc4_state,
413 VC4_SET_FIELD(scale, SCALER_TPZ0_SCALE) |
414 VC4_SET_FIELD(0, SCALER_TPZ0_IPHASE));
415 vc4_dlist_write(vc4_state,
416 VC4_SET_FIELD(recip, SCALER_TPZ1_RECIP));
419 static void vc4_write_ppf(struct vc4_plane_state *vc4_state, u32 src, u32 dst)
421 u32 scale = (1 << 16) * src / dst;
423 vc4_dlist_write(vc4_state,
425 VC4_SET_FIELD(scale, SCALER_PPF_SCALE) |
426 VC4_SET_FIELD(0, SCALER_PPF_IPHASE));
429 static u32 vc4_lbm_size(struct drm_plane_state *state)
431 struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
432 /* This is the worst case number. One of the two sizes will
433 * be used depending on the scaling configuration.
435 u32 pix_per_line = max(vc4_state->src_w[0], (u32)vc4_state->crtc_w);
438 if (!vc4_state->is_yuv) {
439 if (vc4_state->is_unity)
441 else if (vc4_state->y_scaling[0] == VC4_SCALING_TPZ)
442 lbm = pix_per_line * 8;
444 /* In special cases, this multiplier might be 12. */
445 lbm = pix_per_line * 16;
448 /* There are cases for this going down to a multiplier
449 * of 2, but according to the firmware source, the
450 * table in the docs is somewhat wrong.
452 lbm = pix_per_line * 16;
455 lbm = roundup(lbm, 32);
460 static void vc4_write_scaling_parameters(struct drm_plane_state *state,
463 struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
465 /* Ch0 H-PPF Word 0: Scaling Parameters */
466 if (vc4_state->x_scaling[channel] == VC4_SCALING_PPF) {
467 vc4_write_ppf(vc4_state,
468 vc4_state->src_w[channel], vc4_state->crtc_w);
471 /* Ch0 V-PPF Words 0-1: Scaling Parameters, Context */
472 if (vc4_state->y_scaling[channel] == VC4_SCALING_PPF) {
473 vc4_write_ppf(vc4_state,
474 vc4_state->src_h[channel], vc4_state->crtc_h);
475 vc4_dlist_write(vc4_state, 0xc0c0c0c0);
478 /* Ch0 H-TPZ Words 0-1: Scaling Parameters, Recip */
479 if (vc4_state->x_scaling[channel] == VC4_SCALING_TPZ) {
480 vc4_write_tpz(vc4_state,
481 vc4_state->src_w[channel], vc4_state->crtc_w);
484 /* Ch0 V-TPZ Words 0-2: Scaling Parameters, Recip, Context */
485 if (vc4_state->y_scaling[channel] == VC4_SCALING_TPZ) {
486 vc4_write_tpz(vc4_state,
487 vc4_state->src_h[channel], vc4_state->crtc_h);
488 vc4_dlist_write(vc4_state, 0xc0c0c0c0);
492 /* Writes out a full display list for an active plane to the plane's
493 * private dlist state.
495 static int vc4_plane_mode_set(struct drm_plane *plane,
496 struct drm_plane_state *state)
498 struct vc4_dev *vc4 = to_vc4_dev(plane->dev);
499 struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
500 struct drm_framebuffer *fb = state->fb;
501 u32 ctl0_offset = vc4_state->dlist_count;
502 const struct hvs_format *format = vc4_get_hvs_format(fb->format->format);
503 int num_planes = drm_format_num_planes(format->drm);
504 u32 scl0, scl1, pitch0;
505 u32 lbm_size, tiling;
506 unsigned long irqflags;
509 ret = vc4_plane_setup_clipping_and_scaling(state);
513 /* Allocate the LBM memory that the HVS will use for temporary
514 * storage due to our scaling/format conversion.
516 lbm_size = vc4_lbm_size(state);
518 if (!vc4_state->lbm.allocated) {
519 spin_lock_irqsave(&vc4->hvs->mm_lock, irqflags);
520 ret = drm_mm_insert_node_generic(&vc4->hvs->lbm_mm,
523 spin_unlock_irqrestore(&vc4->hvs->mm_lock, irqflags);
525 WARN_ON_ONCE(lbm_size != vc4_state->lbm.size);
532 /* SCL1 is used for Cb/Cr scaling of planar formats. For RGB
533 * and 4:4:4, scl1 should be set to scl0 so both channels of
534 * the scaler do the same thing. For YUV, the Y plane needs
535 * to be put in channel 1 and Cb/Cr in channel 0, so we swap
536 * the scl fields here.
538 if (num_planes == 1) {
539 scl0 = vc4_get_scl_field(state, 1);
542 scl0 = vc4_get_scl_field(state, 1);
543 scl1 = vc4_get_scl_field(state, 0);
546 switch (fb->modifier) {
547 case DRM_FORMAT_MOD_LINEAR:
548 tiling = SCALER_CTL0_TILING_LINEAR;
549 pitch0 = VC4_SET_FIELD(fb->pitches[0], SCALER_SRC_PITCH);
552 case DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED: {
553 /* For T-tiled, the FB pitch is "how many bytes from
554 * one row to the next, such that pitch * tile_h ==
555 * tile_size * tiles_per_row."
557 u32 tile_size_shift = 12; /* T tiles are 4kb */
558 u32 tile_h_shift = 5; /* 16 and 32bpp are 32 pixels high */
559 u32 tiles_w = fb->pitches[0] >> (tile_size_shift - tile_h_shift);
561 tiling = SCALER_CTL0_TILING_256B_OR_T;
563 pitch0 = (VC4_SET_FIELD(0, SCALER_PITCH0_TILE_Y_OFFSET) |
564 VC4_SET_FIELD(0, SCALER_PITCH0_TILE_WIDTH_L) |
565 VC4_SET_FIELD(tiles_w, SCALER_PITCH0_TILE_WIDTH_R));
570 DRM_DEBUG_KMS("Unsupported FB tiling flag 0x%16llx",
571 (long long)fb->modifier);
576 vc4_dlist_write(vc4_state,
578 (format->pixel_order << SCALER_CTL0_ORDER_SHIFT) |
579 (format->hvs << SCALER_CTL0_PIXEL_FORMAT_SHIFT) |
580 VC4_SET_FIELD(tiling, SCALER_CTL0_TILING) |
581 (vc4_state->is_unity ? SCALER_CTL0_UNITY : 0) |
582 VC4_SET_FIELD(scl0, SCALER_CTL0_SCL0) |
583 VC4_SET_FIELD(scl1, SCALER_CTL0_SCL1));
585 /* Position Word 0: Image Positions and Alpha Value */
586 vc4_state->pos0_offset = vc4_state->dlist_count;
587 vc4_dlist_write(vc4_state,
588 VC4_SET_FIELD(0xff, SCALER_POS0_FIXED_ALPHA) |
589 VC4_SET_FIELD(vc4_state->crtc_x, SCALER_POS0_START_X) |
590 VC4_SET_FIELD(vc4_state->crtc_y, SCALER_POS0_START_Y));
592 /* Position Word 1: Scaled Image Dimensions. */
593 if (!vc4_state->is_unity) {
594 vc4_dlist_write(vc4_state,
595 VC4_SET_FIELD(vc4_state->crtc_w,
596 SCALER_POS1_SCL_WIDTH) |
597 VC4_SET_FIELD(vc4_state->crtc_h,
598 SCALER_POS1_SCL_HEIGHT));
601 /* Position Word 2: Source Image Size, Alpha Mode */
602 vc4_state->pos2_offset = vc4_state->dlist_count;
603 vc4_dlist_write(vc4_state,
604 VC4_SET_FIELD(format->has_alpha ?
605 SCALER_POS2_ALPHA_MODE_PIPELINE :
606 SCALER_POS2_ALPHA_MODE_FIXED,
607 SCALER_POS2_ALPHA_MODE) |
608 VC4_SET_FIELD(vc4_state->src_w[0], SCALER_POS2_WIDTH) |
609 VC4_SET_FIELD(vc4_state->src_h[0], SCALER_POS2_HEIGHT));
611 /* Position Word 3: Context. Written by the HVS. */
612 vc4_dlist_write(vc4_state, 0xc0c0c0c0);
615 /* Pointer Word 0/1/2: RGB / Y / Cb / Cr Pointers
617 * The pointers may be any byte address.
619 vc4_state->ptr0_offset = vc4_state->dlist_count;
620 if (!format->flip_cbcr) {
621 for (i = 0; i < num_planes; i++)
622 vc4_dlist_write(vc4_state, vc4_state->offsets[i]);
624 WARN_ON_ONCE(num_planes != 3);
625 vc4_dlist_write(vc4_state, vc4_state->offsets[0]);
626 vc4_dlist_write(vc4_state, vc4_state->offsets[2]);
627 vc4_dlist_write(vc4_state, vc4_state->offsets[1]);
630 /* Pointer Context Word 0/1/2: Written by the HVS */
631 for (i = 0; i < num_planes; i++)
632 vc4_dlist_write(vc4_state, 0xc0c0c0c0);
635 vc4_dlist_write(vc4_state, pitch0);
638 for (i = 1; i < num_planes; i++) {
639 vc4_dlist_write(vc4_state,
640 VC4_SET_FIELD(fb->pitches[i], SCALER_SRC_PITCH));
643 /* Colorspace conversion words */
644 if (vc4_state->is_yuv) {
645 vc4_dlist_write(vc4_state, SCALER_CSC0_ITR_R_601_5);
646 vc4_dlist_write(vc4_state, SCALER_CSC1_ITR_R_601_5);
647 vc4_dlist_write(vc4_state, SCALER_CSC2_ITR_R_601_5);
650 if (!vc4_state->is_unity) {
651 /* LBM Base Address. */
652 if (vc4_state->y_scaling[0] != VC4_SCALING_NONE ||
653 vc4_state->y_scaling[1] != VC4_SCALING_NONE) {
654 vc4_dlist_write(vc4_state, vc4_state->lbm.start);
657 if (num_planes > 1) {
658 /* Emit Cb/Cr as channel 0 and Y as channel
659 * 1. This matches how we set up scl0/scl1
662 vc4_write_scaling_parameters(state, 1);
664 vc4_write_scaling_parameters(state, 0);
666 /* If any PPF setup was done, then all the kernel
667 * pointers get uploaded.
669 if (vc4_state->x_scaling[0] == VC4_SCALING_PPF ||
670 vc4_state->y_scaling[0] == VC4_SCALING_PPF ||
671 vc4_state->x_scaling[1] == VC4_SCALING_PPF ||
672 vc4_state->y_scaling[1] == VC4_SCALING_PPF) {
673 u32 kernel = VC4_SET_FIELD(vc4->hvs->mitchell_netravali_filter.start,
674 SCALER_PPF_KERNEL_OFFSET);
677 vc4_dlist_write(vc4_state, kernel);
679 vc4_dlist_write(vc4_state, kernel);
681 vc4_dlist_write(vc4_state, kernel);
683 vc4_dlist_write(vc4_state, kernel);
687 vc4_state->dlist[ctl0_offset] |=
688 VC4_SET_FIELD(vc4_state->dlist_count, SCALER_CTL0_SIZE);
693 /* If a modeset involves changing the setup of a plane, the atomic
694 * infrastructure will call this to validate a proposed plane setup.
695 * However, if a plane isn't getting updated, this (and the
696 * corresponding vc4_plane_atomic_update) won't get called. Thus, we
697 * compute the dlist here and have all active plane dlists get updated
698 * in the CRTC's flush.
700 static int vc4_plane_atomic_check(struct drm_plane *plane,
701 struct drm_plane_state *state)
703 struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
705 vc4_state->dlist_count = 0;
707 if (plane_enabled(state))
708 return vc4_plane_mode_set(plane, state);
713 static void vc4_plane_atomic_update(struct drm_plane *plane,
714 struct drm_plane_state *old_state)
716 /* No contents here. Since we don't know where in the CRTC's
717 * dlist we should be stored, our dlist is uploaded to the
718 * hardware with vc4_plane_write_dlist() at CRTC atomic_flush
723 u32 vc4_plane_write_dlist(struct drm_plane *plane, u32 __iomem *dlist)
725 struct vc4_plane_state *vc4_state = to_vc4_plane_state(plane->state);
728 vc4_state->hw_dlist = dlist;
730 /* Can't memcpy_toio() because it needs to be 32-bit writes. */
731 for (i = 0; i < vc4_state->dlist_count; i++)
732 writel(vc4_state->dlist[i], &dlist[i]);
734 return vc4_state->dlist_count;
737 u32 vc4_plane_dlist_size(const struct drm_plane_state *state)
739 const struct vc4_plane_state *vc4_state =
740 container_of(state, typeof(*vc4_state), base);
742 return vc4_state->dlist_count;
745 /* Updates the plane to immediately (well, once the FIFO needs
746 * refilling) scan out from at a new framebuffer.
748 void vc4_plane_async_set_fb(struct drm_plane *plane, struct drm_framebuffer *fb)
750 struct vc4_plane_state *vc4_state = to_vc4_plane_state(plane->state);
751 struct drm_gem_cma_object *bo = drm_fb_cma_get_gem_obj(fb, 0);
754 /* We're skipping the address adjustment for negative origin,
755 * because this is only called on the primary plane.
757 WARN_ON_ONCE(plane->state->crtc_x < 0 || plane->state->crtc_y < 0);
758 addr = bo->paddr + fb->offsets[0];
760 /* Write the new address into the hardware immediately. The
761 * scanout will start from this address as soon as the FIFO
762 * needs to refill with pixels.
764 writel(addr, &vc4_state->hw_dlist[vc4_state->ptr0_offset]);
766 /* Also update the CPU-side dlist copy, so that any later
767 * atomic updates that don't do a new modeset on our plane
768 * also use our updated address.
770 vc4_state->dlist[vc4_state->ptr0_offset] = addr;
773 static int vc4_prepare_fb(struct drm_plane *plane,
774 struct drm_plane_state *state)
777 struct dma_fence *fence;
780 if ((plane->state->fb == state->fb) || !state->fb)
783 bo = to_vc4_bo(&drm_fb_cma_get_gem_obj(state->fb, 0)->base);
785 ret = vc4_bo_inc_usecnt(bo);
789 fence = reservation_object_get_excl_rcu(bo->resv);
790 drm_atomic_set_fence_for_plane(state, fence);
795 static void vc4_cleanup_fb(struct drm_plane *plane,
796 struct drm_plane_state *state)
800 if (plane->state->fb == state->fb || !state->fb)
803 bo = to_vc4_bo(&drm_fb_cma_get_gem_obj(state->fb, 0)->base);
804 vc4_bo_dec_usecnt(bo);
807 static const struct drm_plane_helper_funcs vc4_plane_helper_funcs = {
808 .atomic_check = vc4_plane_atomic_check,
809 .atomic_update = vc4_plane_atomic_update,
810 .prepare_fb = vc4_prepare_fb,
811 .cleanup_fb = vc4_cleanup_fb,
814 static void vc4_plane_destroy(struct drm_plane *plane)
816 drm_plane_helper_disable(plane);
817 drm_plane_cleanup(plane);
820 /* Implements immediate (non-vblank-synced) updates of the cursor
821 * position, or falls back to the atomic helper otherwise.
824 vc4_update_plane(struct drm_plane *plane,
825 struct drm_crtc *crtc,
826 struct drm_framebuffer *fb,
827 int crtc_x, int crtc_y,
828 unsigned int crtc_w, unsigned int crtc_h,
829 uint32_t src_x, uint32_t src_y,
830 uint32_t src_w, uint32_t src_h,
831 struct drm_modeset_acquire_ctx *ctx)
833 struct drm_plane_state *plane_state;
834 struct vc4_plane_state *vc4_state;
836 if (plane != crtc->cursor)
839 plane_state = plane->state;
840 vc4_state = to_vc4_plane_state(plane_state);
845 /* No configuring new scaling in the fast path. */
846 if (crtc_w != plane_state->crtc_w ||
847 crtc_h != plane_state->crtc_h ||
848 src_w != plane_state->src_w ||
849 src_h != plane_state->src_h) {
853 if (fb != plane_state->fb) {
854 drm_atomic_set_fb_for_plane(plane->state, fb);
855 vc4_plane_async_set_fb(plane, fb);
858 /* Set the cursor's position on the screen. This is the
859 * expected change from the drm_mode_cursor_universal()
862 plane_state->crtc_x = crtc_x;
863 plane_state->crtc_y = crtc_y;
865 /* Allow changing the start position within the cursor BO, if
868 plane_state->src_x = src_x;
869 plane_state->src_y = src_y;
871 /* Update the display list based on the new crtc_x/y. */
872 vc4_plane_atomic_check(plane, plane_state);
874 /* Note that we can't just call vc4_plane_write_dlist()
875 * because that would smash the context data that the HVS is
878 writel(vc4_state->dlist[vc4_state->pos0_offset],
879 &vc4_state->hw_dlist[vc4_state->pos0_offset]);
880 writel(vc4_state->dlist[vc4_state->pos2_offset],
881 &vc4_state->hw_dlist[vc4_state->pos2_offset]);
882 writel(vc4_state->dlist[vc4_state->ptr0_offset],
883 &vc4_state->hw_dlist[vc4_state->ptr0_offset]);
888 return drm_atomic_helper_update_plane(plane, crtc, fb,
896 static const struct drm_plane_funcs vc4_plane_funcs = {
897 .update_plane = vc4_update_plane,
898 .disable_plane = drm_atomic_helper_disable_plane,
899 .destroy = vc4_plane_destroy,
900 .set_property = NULL,
901 .reset = vc4_plane_reset,
902 .atomic_duplicate_state = vc4_plane_duplicate_state,
903 .atomic_destroy_state = vc4_plane_destroy_state,
906 struct drm_plane *vc4_plane_init(struct drm_device *dev,
907 enum drm_plane_type type)
909 struct drm_plane *plane = NULL;
910 struct vc4_plane *vc4_plane;
911 u32 formats[ARRAY_SIZE(hvs_formats)];
916 vc4_plane = devm_kzalloc(dev->dev, sizeof(*vc4_plane),
919 return ERR_PTR(-ENOMEM);
921 for (i = 0; i < ARRAY_SIZE(hvs_formats); i++) {
922 /* Don't allow YUV in cursor planes, since that means
923 * tuning on the scaler, which we don't allow for the
926 if (type != DRM_PLANE_TYPE_CURSOR ||
927 hvs_formats[i].hvs < HVS_PIXEL_FORMAT_YCBCR_YUV420_3PLANE) {
928 formats[num_formats++] = hvs_formats[i].drm;
931 plane = &vc4_plane->base;
932 ret = drm_universal_plane_init(dev, plane, 0,
934 formats, num_formats,
937 drm_plane_helper_add(plane, &vc4_plane_helper_funcs);