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>
25 #include <drm/drm_atomic_uapi.h>
27 #include "uapi/drm/vc4_drm.h"
31 static const struct hvs_format {
32 u32 drm; /* DRM_FORMAT_* */
33 u32 hvs; /* HVS_FORMAT_* */
37 .drm = DRM_FORMAT_XRGB8888, .hvs = HVS_PIXEL_FORMAT_RGBA8888,
38 .pixel_order = HVS_PIXEL_ORDER_ABGR,
41 .drm = DRM_FORMAT_ARGB8888, .hvs = HVS_PIXEL_FORMAT_RGBA8888,
42 .pixel_order = HVS_PIXEL_ORDER_ABGR,
45 .drm = DRM_FORMAT_ABGR8888, .hvs = HVS_PIXEL_FORMAT_RGBA8888,
46 .pixel_order = HVS_PIXEL_ORDER_ARGB,
49 .drm = DRM_FORMAT_XBGR8888, .hvs = HVS_PIXEL_FORMAT_RGBA8888,
50 .pixel_order = HVS_PIXEL_ORDER_ARGB,
53 .drm = DRM_FORMAT_RGB565, .hvs = HVS_PIXEL_FORMAT_RGB565,
54 .pixel_order = HVS_PIXEL_ORDER_XRGB,
57 .drm = DRM_FORMAT_BGR565, .hvs = HVS_PIXEL_FORMAT_RGB565,
58 .pixel_order = HVS_PIXEL_ORDER_XBGR,
61 .drm = DRM_FORMAT_ARGB1555, .hvs = HVS_PIXEL_FORMAT_RGBA5551,
62 .pixel_order = HVS_PIXEL_ORDER_ABGR,
65 .drm = DRM_FORMAT_XRGB1555, .hvs = HVS_PIXEL_FORMAT_RGBA5551,
66 .pixel_order = HVS_PIXEL_ORDER_ABGR,
69 .drm = DRM_FORMAT_RGB888, .hvs = HVS_PIXEL_FORMAT_RGB888,
70 .pixel_order = HVS_PIXEL_ORDER_XRGB,
73 .drm = DRM_FORMAT_BGR888, .hvs = HVS_PIXEL_FORMAT_RGB888,
74 .pixel_order = HVS_PIXEL_ORDER_XBGR,
77 .drm = DRM_FORMAT_YUV422,
78 .hvs = HVS_PIXEL_FORMAT_YCBCR_YUV422_3PLANE,
79 .pixel_order = HVS_PIXEL_ORDER_XYCBCR,
82 .drm = DRM_FORMAT_YVU422,
83 .hvs = HVS_PIXEL_FORMAT_YCBCR_YUV422_3PLANE,
84 .pixel_order = HVS_PIXEL_ORDER_XYCRCB,
87 .drm = DRM_FORMAT_YUV420,
88 .hvs = HVS_PIXEL_FORMAT_YCBCR_YUV420_3PLANE,
89 .pixel_order = HVS_PIXEL_ORDER_XYCBCR,
92 .drm = DRM_FORMAT_YVU420,
93 .hvs = HVS_PIXEL_FORMAT_YCBCR_YUV420_3PLANE,
94 .pixel_order = HVS_PIXEL_ORDER_XYCRCB,
97 .drm = DRM_FORMAT_NV12,
98 .hvs = HVS_PIXEL_FORMAT_YCBCR_YUV420_2PLANE,
99 .pixel_order = HVS_PIXEL_ORDER_XYCBCR,
102 .drm = DRM_FORMAT_NV21,
103 .hvs = HVS_PIXEL_FORMAT_YCBCR_YUV420_2PLANE,
104 .pixel_order = HVS_PIXEL_ORDER_XYCRCB,
107 .drm = DRM_FORMAT_NV16,
108 .hvs = HVS_PIXEL_FORMAT_YCBCR_YUV422_2PLANE,
109 .pixel_order = HVS_PIXEL_ORDER_XYCBCR,
112 .drm = DRM_FORMAT_NV61,
113 .hvs = HVS_PIXEL_FORMAT_YCBCR_YUV422_2PLANE,
114 .pixel_order = HVS_PIXEL_ORDER_XYCRCB,
118 static const struct hvs_format *vc4_get_hvs_format(u32 drm_format)
122 for (i = 0; i < ARRAY_SIZE(hvs_formats); i++) {
123 if (hvs_formats[i].drm == drm_format)
124 return &hvs_formats[i];
130 static enum vc4_scaling_mode vc4_get_scaling_mode(u32 src, u32 dst)
133 return VC4_SCALING_NONE;
134 if (3 * dst >= 2 * src)
135 return VC4_SCALING_PPF;
137 return VC4_SCALING_TPZ;
140 static bool plane_enabled(struct drm_plane_state *state)
142 return state->fb && state->crtc;
145 static struct drm_plane_state *vc4_plane_duplicate_state(struct drm_plane *plane)
147 struct vc4_plane_state *vc4_state;
149 if (WARN_ON(!plane->state))
152 vc4_state = kmemdup(plane->state, sizeof(*vc4_state), GFP_KERNEL);
156 memset(&vc4_state->lbm, 0, sizeof(vc4_state->lbm));
157 vc4_state->dlist_initialized = 0;
159 __drm_atomic_helper_plane_duplicate_state(plane, &vc4_state->base);
161 if (vc4_state->dlist) {
162 vc4_state->dlist = kmemdup(vc4_state->dlist,
163 vc4_state->dlist_count * 4,
165 if (!vc4_state->dlist) {
169 vc4_state->dlist_size = vc4_state->dlist_count;
172 return &vc4_state->base;
175 static void vc4_plane_destroy_state(struct drm_plane *plane,
176 struct drm_plane_state *state)
178 struct vc4_dev *vc4 = to_vc4_dev(plane->dev);
179 struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
181 if (vc4_state->lbm.allocated) {
182 unsigned long irqflags;
184 spin_lock_irqsave(&vc4->hvs->mm_lock, irqflags);
185 drm_mm_remove_node(&vc4_state->lbm);
186 spin_unlock_irqrestore(&vc4->hvs->mm_lock, irqflags);
189 kfree(vc4_state->dlist);
190 __drm_atomic_helper_plane_destroy_state(&vc4_state->base);
194 /* Called during init to allocate the plane's atomic state. */
195 static void vc4_plane_reset(struct drm_plane *plane)
197 struct vc4_plane_state *vc4_state;
199 WARN_ON(plane->state);
201 vc4_state = kzalloc(sizeof(*vc4_state), GFP_KERNEL);
205 __drm_atomic_helper_plane_reset(plane, &vc4_state->base);
208 static void vc4_dlist_write(struct vc4_plane_state *vc4_state, u32 val)
210 if (vc4_state->dlist_count == vc4_state->dlist_size) {
211 u32 new_size = max(4u, vc4_state->dlist_count * 2);
212 u32 *new_dlist = kmalloc_array(new_size, 4, GFP_KERNEL);
216 memcpy(new_dlist, vc4_state->dlist, vc4_state->dlist_count * 4);
218 kfree(vc4_state->dlist);
219 vc4_state->dlist = new_dlist;
220 vc4_state->dlist_size = new_size;
223 vc4_state->dlist[vc4_state->dlist_count++] = val;
226 /* Returns the scl0/scl1 field based on whether the dimensions need to
227 * be up/down/non-scaled.
229 * This is a replication of a table from the spec.
231 static u32 vc4_get_scl_field(struct drm_plane_state *state, int plane)
233 struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
235 switch (vc4_state->x_scaling[plane] << 2 | vc4_state->y_scaling[plane]) {
236 case VC4_SCALING_PPF << 2 | VC4_SCALING_PPF:
237 return SCALER_CTL0_SCL_H_PPF_V_PPF;
238 case VC4_SCALING_TPZ << 2 | VC4_SCALING_PPF:
239 return SCALER_CTL0_SCL_H_TPZ_V_PPF;
240 case VC4_SCALING_PPF << 2 | VC4_SCALING_TPZ:
241 return SCALER_CTL0_SCL_H_PPF_V_TPZ;
242 case VC4_SCALING_TPZ << 2 | VC4_SCALING_TPZ:
243 return SCALER_CTL0_SCL_H_TPZ_V_TPZ;
244 case VC4_SCALING_PPF << 2 | VC4_SCALING_NONE:
245 return SCALER_CTL0_SCL_H_PPF_V_NONE;
246 case VC4_SCALING_NONE << 2 | VC4_SCALING_PPF:
247 return SCALER_CTL0_SCL_H_NONE_V_PPF;
248 case VC4_SCALING_NONE << 2 | VC4_SCALING_TPZ:
249 return SCALER_CTL0_SCL_H_NONE_V_TPZ;
250 case VC4_SCALING_TPZ << 2 | VC4_SCALING_NONE:
251 return SCALER_CTL0_SCL_H_TPZ_V_NONE;
253 case VC4_SCALING_NONE << 2 | VC4_SCALING_NONE:
254 /* The unity case is independently handled by
261 static int vc4_plane_setup_clipping_and_scaling(struct drm_plane_state *state)
263 struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
264 struct drm_framebuffer *fb = state->fb;
265 struct drm_gem_cma_object *bo = drm_fb_cma_get_gem_obj(fb, 0);
266 u32 subpixel_src_mask = (1 << 16) - 1;
267 u32 format = fb->format->format;
268 int num_planes = fb->format->num_planes;
269 struct drm_crtc_state *crtc_state;
270 u32 h_subsample, v_subsample;
273 crtc_state = drm_atomic_get_existing_crtc_state(state->state,
276 DRM_DEBUG_KMS("Invalid crtc state\n");
280 ret = drm_atomic_helper_check_plane_state(state, crtc_state, 1,
281 INT_MAX, true, true);
285 h_subsample = drm_format_horz_chroma_subsampling(format);
286 v_subsample = drm_format_vert_chroma_subsampling(format);
288 for (i = 0; i < num_planes; i++)
289 vc4_state->offsets[i] = bo->paddr + fb->offsets[i];
291 /* We don't support subpixel source positioning for scaling. */
292 if ((state->src.x1 & subpixel_src_mask) ||
293 (state->src.x2 & subpixel_src_mask) ||
294 (state->src.y1 & subpixel_src_mask) ||
295 (state->src.y2 & subpixel_src_mask)) {
299 vc4_state->src_x = state->src.x1 >> 16;
300 vc4_state->src_y = state->src.y1 >> 16;
301 vc4_state->src_w[0] = (state->src.x2 - state->src.x1) >> 16;
302 vc4_state->src_h[0] = (state->src.y2 - state->src.y1) >> 16;
304 vc4_state->crtc_x = state->dst.x1;
305 vc4_state->crtc_y = state->dst.y1;
306 vc4_state->crtc_w = state->dst.x2 - state->dst.x1;
307 vc4_state->crtc_h = state->dst.y2 - state->dst.y1;
309 vc4_state->x_scaling[0] = vc4_get_scaling_mode(vc4_state->src_w[0],
311 vc4_state->y_scaling[0] = vc4_get_scaling_mode(vc4_state->src_h[0],
314 vc4_state->is_unity = (vc4_state->x_scaling[0] == VC4_SCALING_NONE &&
315 vc4_state->y_scaling[0] == VC4_SCALING_NONE);
317 if (num_planes > 1) {
318 vc4_state->is_yuv = true;
320 vc4_state->src_w[1] = vc4_state->src_w[0] / h_subsample;
321 vc4_state->src_h[1] = vc4_state->src_h[0] / v_subsample;
323 vc4_state->x_scaling[1] =
324 vc4_get_scaling_mode(vc4_state->src_w[1],
326 vc4_state->y_scaling[1] =
327 vc4_get_scaling_mode(vc4_state->src_h[1],
330 /* YUV conversion requires that horizontal scaling be enabled
331 * on the UV plane even if vc4_get_scaling_mode() returned
332 * VC4_SCALING_NONE (which can happen when the down-scaling
333 * ratio is 0.5). Let's force it to VC4_SCALING_PPF in this
336 if (vc4_state->x_scaling[1] == VC4_SCALING_NONE)
337 vc4_state->x_scaling[1] = VC4_SCALING_PPF;
339 vc4_state->is_yuv = false;
340 vc4_state->x_scaling[1] = VC4_SCALING_NONE;
341 vc4_state->y_scaling[1] = VC4_SCALING_NONE;
347 static void vc4_write_tpz(struct vc4_plane_state *vc4_state, u32 src, u32 dst)
351 scale = (1 << 16) * src / dst;
353 /* The specs note that while the reciprocal would be defined
354 * as (1<<32)/scale, ~0 is close enough.
358 vc4_dlist_write(vc4_state,
359 VC4_SET_FIELD(scale, SCALER_TPZ0_SCALE) |
360 VC4_SET_FIELD(0, SCALER_TPZ0_IPHASE));
361 vc4_dlist_write(vc4_state,
362 VC4_SET_FIELD(recip, SCALER_TPZ1_RECIP));
365 static void vc4_write_ppf(struct vc4_plane_state *vc4_state, u32 src, u32 dst)
367 u32 scale = (1 << 16) * src / dst;
369 vc4_dlist_write(vc4_state,
371 VC4_SET_FIELD(scale, SCALER_PPF_SCALE) |
372 VC4_SET_FIELD(0, SCALER_PPF_IPHASE));
375 static u32 vc4_lbm_size(struct drm_plane_state *state)
377 struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
378 /* This is the worst case number. One of the two sizes will
379 * be used depending on the scaling configuration.
381 u32 pix_per_line = max(vc4_state->src_w[0], (u32)vc4_state->crtc_w);
384 /* LBM is not needed when there's no vertical scaling. */
385 if (vc4_state->y_scaling[0] == VC4_SCALING_NONE &&
386 vc4_state->y_scaling[1] == VC4_SCALING_NONE)
389 if (!vc4_state->is_yuv) {
390 if (vc4_state->y_scaling[0] == VC4_SCALING_TPZ)
391 lbm = pix_per_line * 8;
393 /* In special cases, this multiplier might be 12. */
394 lbm = pix_per_line * 16;
397 /* There are cases for this going down to a multiplier
398 * of 2, but according to the firmware source, the
399 * table in the docs is somewhat wrong.
401 lbm = pix_per_line * 16;
404 lbm = roundup(lbm, 32);
409 static void vc4_write_scaling_parameters(struct drm_plane_state *state,
412 struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
414 /* Ch0 H-PPF Word 0: Scaling Parameters */
415 if (vc4_state->x_scaling[channel] == VC4_SCALING_PPF) {
416 vc4_write_ppf(vc4_state,
417 vc4_state->src_w[channel], vc4_state->crtc_w);
420 /* Ch0 V-PPF Words 0-1: Scaling Parameters, Context */
421 if (vc4_state->y_scaling[channel] == VC4_SCALING_PPF) {
422 vc4_write_ppf(vc4_state,
423 vc4_state->src_h[channel], vc4_state->crtc_h);
424 vc4_dlist_write(vc4_state, 0xc0c0c0c0);
427 /* Ch0 H-TPZ Words 0-1: Scaling Parameters, Recip */
428 if (vc4_state->x_scaling[channel] == VC4_SCALING_TPZ) {
429 vc4_write_tpz(vc4_state,
430 vc4_state->src_w[channel], vc4_state->crtc_w);
433 /* Ch0 V-TPZ Words 0-2: Scaling Parameters, Recip, Context */
434 if (vc4_state->y_scaling[channel] == VC4_SCALING_TPZ) {
435 vc4_write_tpz(vc4_state,
436 vc4_state->src_h[channel], vc4_state->crtc_h);
437 vc4_dlist_write(vc4_state, 0xc0c0c0c0);
441 static int vc4_plane_allocate_lbm(struct drm_plane_state *state)
443 struct vc4_dev *vc4 = to_vc4_dev(state->plane->dev);
444 struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
445 unsigned long irqflags;
448 lbm_size = vc4_lbm_size(state);
452 if (WARN_ON(!vc4_state->lbm_offset))
455 /* Allocate the LBM memory that the HVS will use for temporary
456 * storage due to our scaling/format conversion.
458 if (!vc4_state->lbm.allocated) {
461 spin_lock_irqsave(&vc4->hvs->mm_lock, irqflags);
462 ret = drm_mm_insert_node_generic(&vc4->hvs->lbm_mm,
465 spin_unlock_irqrestore(&vc4->hvs->mm_lock, irqflags);
470 WARN_ON_ONCE(lbm_size != vc4_state->lbm.size);
473 vc4_state->dlist[vc4_state->lbm_offset] = vc4_state->lbm.start;
478 /* Writes out a full display list for an active plane to the plane's
479 * private dlist state.
481 static int vc4_plane_mode_set(struct drm_plane *plane,
482 struct drm_plane_state *state)
484 struct vc4_dev *vc4 = to_vc4_dev(plane->dev);
485 struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
486 struct drm_framebuffer *fb = state->fb;
487 u32 ctl0_offset = vc4_state->dlist_count;
488 const struct hvs_format *format = vc4_get_hvs_format(fb->format->format);
489 u64 base_format_mod = fourcc_mod_broadcom_mod(fb->modifier);
490 int num_planes = drm_format_num_planes(format->drm);
491 u32 h_subsample, v_subsample;
492 bool mix_plane_alpha;
494 u32 scl0, scl1, pitch0;
496 u32 hvs_format = format->hvs;
499 if (vc4_state->dlist_initialized)
502 ret = vc4_plane_setup_clipping_and_scaling(state);
506 /* SCL1 is used for Cb/Cr scaling of planar formats. For RGB
507 * and 4:4:4, scl1 should be set to scl0 so both channels of
508 * the scaler do the same thing. For YUV, the Y plane needs
509 * to be put in channel 1 and Cb/Cr in channel 0, so we swap
510 * the scl fields here.
512 if (num_planes == 1) {
513 scl0 = vc4_get_scl_field(state, 0);
516 scl0 = vc4_get_scl_field(state, 1);
517 scl1 = vc4_get_scl_field(state, 0);
520 h_subsample = drm_format_horz_chroma_subsampling(format->drm);
521 v_subsample = drm_format_vert_chroma_subsampling(format->drm);
523 switch (base_format_mod) {
524 case DRM_FORMAT_MOD_LINEAR:
525 tiling = SCALER_CTL0_TILING_LINEAR;
526 pitch0 = VC4_SET_FIELD(fb->pitches[0], SCALER_SRC_PITCH);
528 /* Adjust the base pointer to the first pixel to be scanned
531 for (i = 0; i < num_planes; i++) {
532 vc4_state->offsets[i] += vc4_state->src_y /
533 (i ? v_subsample : 1) *
535 vc4_state->offsets[i] += vc4_state->src_x /
536 (i ? h_subsample : 1) *
542 case DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED: {
543 u32 tile_size_shift = 12; /* T tiles are 4kb */
544 /* Whole-tile offsets, mostly for setting the pitch. */
545 u32 tile_w_shift = fb->format->cpp[0] == 2 ? 6 : 5;
546 u32 tile_h_shift = 5; /* 16 and 32bpp are 32 pixels high */
547 u32 tile_w_mask = (1 << tile_w_shift) - 1;
548 /* The height mask on 32-bit-per-pixel tiles is 63, i.e. twice
549 * the height (in pixels) of a 4k tile.
551 u32 tile_h_mask = (2 << tile_h_shift) - 1;
552 /* For T-tiled, the FB pitch is "how many bytes from one row to
553 * the next, such that
555 * pitch * tile_h == tile_size * tiles_per_row
557 u32 tiles_w = fb->pitches[0] >> (tile_size_shift - tile_h_shift);
558 u32 tiles_l = vc4_state->src_x >> tile_w_shift;
559 u32 tiles_r = tiles_w - tiles_l;
560 u32 tiles_t = vc4_state->src_y >> tile_h_shift;
561 /* Intra-tile offsets, which modify the base address (the
562 * SCALER_PITCH0_TILE_Y_OFFSET tells HVS how to walk from that
565 u32 tile_y = (vc4_state->src_y >> 4) & 1;
566 u32 subtile_y = (vc4_state->src_y >> 2) & 3;
567 u32 utile_y = vc4_state->src_y & 3;
568 u32 x_off = vc4_state->src_x & tile_w_mask;
569 u32 y_off = vc4_state->src_y & tile_h_mask;
571 tiling = SCALER_CTL0_TILING_256B_OR_T;
572 pitch0 = (VC4_SET_FIELD(x_off, SCALER_PITCH0_SINK_PIX) |
573 VC4_SET_FIELD(y_off, SCALER_PITCH0_TILE_Y_OFFSET) |
574 VC4_SET_FIELD(tiles_l, SCALER_PITCH0_TILE_WIDTH_L) |
575 VC4_SET_FIELD(tiles_r, SCALER_PITCH0_TILE_WIDTH_R));
576 vc4_state->offsets[0] += tiles_t * (tiles_w << tile_size_shift);
577 vc4_state->offsets[0] += subtile_y << 8;
578 vc4_state->offsets[0] += utile_y << 4;
580 /* Rows of tiles alternate left-to-right and right-to-left. */
582 pitch0 |= SCALER_PITCH0_TILE_INITIAL_LINE_DIR;
583 vc4_state->offsets[0] += (tiles_w - tiles_l) <<
585 vc4_state->offsets[0] -= (1 + !tile_y) << 10;
587 vc4_state->offsets[0] += tiles_l << tile_size_shift;
588 vc4_state->offsets[0] += tile_y << 10;
594 case DRM_FORMAT_MOD_BROADCOM_SAND64:
595 case DRM_FORMAT_MOD_BROADCOM_SAND128:
596 case DRM_FORMAT_MOD_BROADCOM_SAND256: {
597 uint32_t param = fourcc_mod_broadcom_param(fb->modifier);
599 /* Column-based NV12 or RGBA.
601 if (fb->format->num_planes > 1) {
602 if (hvs_format != HVS_PIXEL_FORMAT_YCBCR_YUV420_2PLANE) {
603 DRM_DEBUG_KMS("SAND format only valid for NV12/21");
606 hvs_format = HVS_PIXEL_FORMAT_H264;
608 if (base_format_mod == DRM_FORMAT_MOD_BROADCOM_SAND256) {
609 DRM_DEBUG_KMS("SAND256 format only valid for H.264");
614 switch (base_format_mod) {
615 case DRM_FORMAT_MOD_BROADCOM_SAND64:
616 tiling = SCALER_CTL0_TILING_64B;
618 case DRM_FORMAT_MOD_BROADCOM_SAND128:
619 tiling = SCALER_CTL0_TILING_128B;
621 case DRM_FORMAT_MOD_BROADCOM_SAND256:
622 tiling = SCALER_CTL0_TILING_256B_OR_T;
628 if (param > SCALER_TILE_HEIGHT_MASK) {
629 DRM_DEBUG_KMS("SAND height too large (%d)\n", param);
633 pitch0 = VC4_SET_FIELD(param, SCALER_TILE_HEIGHT);
638 DRM_DEBUG_KMS("Unsupported FB tiling flag 0x%16llx",
639 (long long)fb->modifier);
644 vc4_dlist_write(vc4_state,
646 VC4_SET_FIELD(SCALER_CTL0_RGBA_EXPAND_ROUND, SCALER_CTL0_RGBA_EXPAND) |
647 (format->pixel_order << SCALER_CTL0_ORDER_SHIFT) |
648 (hvs_format << SCALER_CTL0_PIXEL_FORMAT_SHIFT) |
649 VC4_SET_FIELD(tiling, SCALER_CTL0_TILING) |
650 (vc4_state->is_unity ? SCALER_CTL0_UNITY : 0) |
651 VC4_SET_FIELD(scl0, SCALER_CTL0_SCL0) |
652 VC4_SET_FIELD(scl1, SCALER_CTL0_SCL1));
654 /* Position Word 0: Image Positions and Alpha Value */
655 vc4_state->pos0_offset = vc4_state->dlist_count;
656 vc4_dlist_write(vc4_state,
657 VC4_SET_FIELD(state->alpha >> 8, SCALER_POS0_FIXED_ALPHA) |
658 VC4_SET_FIELD(vc4_state->crtc_x, SCALER_POS0_START_X) |
659 VC4_SET_FIELD(vc4_state->crtc_y, SCALER_POS0_START_Y));
661 /* Position Word 1: Scaled Image Dimensions. */
662 if (!vc4_state->is_unity) {
663 vc4_dlist_write(vc4_state,
664 VC4_SET_FIELD(vc4_state->crtc_w,
665 SCALER_POS1_SCL_WIDTH) |
666 VC4_SET_FIELD(vc4_state->crtc_h,
667 SCALER_POS1_SCL_HEIGHT));
670 /* Don't waste cycles mixing with plane alpha if the set alpha
671 * is opaque or there is no per-pixel alpha information.
672 * In any case we use the alpha property value as the fixed alpha.
674 mix_plane_alpha = state->alpha != DRM_BLEND_ALPHA_OPAQUE &&
675 fb->format->has_alpha;
677 /* Position Word 2: Source Image Size, Alpha */
678 vc4_state->pos2_offset = vc4_state->dlist_count;
679 vc4_dlist_write(vc4_state,
680 VC4_SET_FIELD(fb->format->has_alpha ?
681 SCALER_POS2_ALPHA_MODE_PIPELINE :
682 SCALER_POS2_ALPHA_MODE_FIXED,
683 SCALER_POS2_ALPHA_MODE) |
684 (mix_plane_alpha ? SCALER_POS2_ALPHA_MIX : 0) |
685 (fb->format->has_alpha ? SCALER_POS2_ALPHA_PREMULT : 0) |
686 VC4_SET_FIELD(vc4_state->src_w[0], SCALER_POS2_WIDTH) |
687 VC4_SET_FIELD(vc4_state->src_h[0], SCALER_POS2_HEIGHT));
689 /* Position Word 3: Context. Written by the HVS. */
690 vc4_dlist_write(vc4_state, 0xc0c0c0c0);
693 /* Pointer Word 0/1/2: RGB / Y / Cb / Cr Pointers
695 * The pointers may be any byte address.
697 vc4_state->ptr0_offset = vc4_state->dlist_count;
698 for (i = 0; i < num_planes; i++)
699 vc4_dlist_write(vc4_state, vc4_state->offsets[i]);
701 /* Pointer Context Word 0/1/2: Written by the HVS */
702 for (i = 0; i < num_planes; i++)
703 vc4_dlist_write(vc4_state, 0xc0c0c0c0);
706 vc4_dlist_write(vc4_state, pitch0);
709 for (i = 1; i < num_planes; i++) {
710 if (hvs_format != HVS_PIXEL_FORMAT_H264) {
711 vc4_dlist_write(vc4_state,
712 VC4_SET_FIELD(fb->pitches[i],
715 vc4_dlist_write(vc4_state, pitch0);
719 /* Colorspace conversion words */
720 if (vc4_state->is_yuv) {
721 vc4_dlist_write(vc4_state, SCALER_CSC0_ITR_R_601_5);
722 vc4_dlist_write(vc4_state, SCALER_CSC1_ITR_R_601_5);
723 vc4_dlist_write(vc4_state, SCALER_CSC2_ITR_R_601_5);
726 vc4_state->lbm_offset = 0;
728 if (vc4_state->x_scaling[0] != VC4_SCALING_NONE ||
729 vc4_state->x_scaling[1] != VC4_SCALING_NONE ||
730 vc4_state->y_scaling[0] != VC4_SCALING_NONE ||
731 vc4_state->y_scaling[1] != VC4_SCALING_NONE) {
732 /* Reserve a slot for the LBM Base Address. The real value will
733 * be set when calling vc4_plane_allocate_lbm().
735 if (vc4_state->y_scaling[0] != VC4_SCALING_NONE ||
736 vc4_state->y_scaling[1] != VC4_SCALING_NONE)
737 vc4_state->lbm_offset = vc4_state->dlist_count++;
739 if (num_planes > 1) {
740 /* Emit Cb/Cr as channel 0 and Y as channel
741 * 1. This matches how we set up scl0/scl1
744 vc4_write_scaling_parameters(state, 1);
746 vc4_write_scaling_parameters(state, 0);
748 /* If any PPF setup was done, then all the kernel
749 * pointers get uploaded.
751 if (vc4_state->x_scaling[0] == VC4_SCALING_PPF ||
752 vc4_state->y_scaling[0] == VC4_SCALING_PPF ||
753 vc4_state->x_scaling[1] == VC4_SCALING_PPF ||
754 vc4_state->y_scaling[1] == VC4_SCALING_PPF) {
755 u32 kernel = VC4_SET_FIELD(vc4->hvs->mitchell_netravali_filter.start,
756 SCALER_PPF_KERNEL_OFFSET);
759 vc4_dlist_write(vc4_state, kernel);
761 vc4_dlist_write(vc4_state, kernel);
763 vc4_dlist_write(vc4_state, kernel);
765 vc4_dlist_write(vc4_state, kernel);
769 vc4_state->dlist[ctl0_offset] |=
770 VC4_SET_FIELD(vc4_state->dlist_count, SCALER_CTL0_SIZE);
772 /* crtc_* are already clipped coordinates. */
773 covers_screen = vc4_state->crtc_x == 0 && vc4_state->crtc_y == 0 &&
774 vc4_state->crtc_w == state->crtc->mode.hdisplay &&
775 vc4_state->crtc_h == state->crtc->mode.vdisplay;
776 /* Background fill might be necessary when the plane has per-pixel
777 * alpha content or a non-opaque plane alpha and could blend from the
778 * background or does not cover the entire screen.
780 vc4_state->needs_bg_fill = fb->format->has_alpha || !covers_screen ||
781 state->alpha != DRM_BLEND_ALPHA_OPAQUE;
783 /* Flag the dlist as initialized to avoid checking it twice in case
784 * the async update check already called vc4_plane_mode_set() and
785 * decided to fallback to sync update because async update was not
788 vc4_state->dlist_initialized = 1;
793 /* If a modeset involves changing the setup of a plane, the atomic
794 * infrastructure will call this to validate a proposed plane setup.
795 * However, if a plane isn't getting updated, this (and the
796 * corresponding vc4_plane_atomic_update) won't get called. Thus, we
797 * compute the dlist here and have all active plane dlists get updated
798 * in the CRTC's flush.
800 static int vc4_plane_atomic_check(struct drm_plane *plane,
801 struct drm_plane_state *state)
803 struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
806 vc4_state->dlist_count = 0;
808 if (!plane_enabled(state))
811 ret = vc4_plane_mode_set(plane, state);
815 return vc4_plane_allocate_lbm(state);
818 static void vc4_plane_atomic_update(struct drm_plane *plane,
819 struct drm_plane_state *old_state)
821 /* No contents here. Since we don't know where in the CRTC's
822 * dlist we should be stored, our dlist is uploaded to the
823 * hardware with vc4_plane_write_dlist() at CRTC atomic_flush
828 u32 vc4_plane_write_dlist(struct drm_plane *plane, u32 __iomem *dlist)
830 struct vc4_plane_state *vc4_state = to_vc4_plane_state(plane->state);
833 vc4_state->hw_dlist = dlist;
835 /* Can't memcpy_toio() because it needs to be 32-bit writes. */
836 for (i = 0; i < vc4_state->dlist_count; i++)
837 writel(vc4_state->dlist[i], &dlist[i]);
839 return vc4_state->dlist_count;
842 u32 vc4_plane_dlist_size(const struct drm_plane_state *state)
844 const struct vc4_plane_state *vc4_state =
845 container_of(state, typeof(*vc4_state), base);
847 return vc4_state->dlist_count;
850 /* Updates the plane to immediately (well, once the FIFO needs
851 * refilling) scan out from at a new framebuffer.
853 void vc4_plane_async_set_fb(struct drm_plane *plane, struct drm_framebuffer *fb)
855 struct vc4_plane_state *vc4_state = to_vc4_plane_state(plane->state);
856 struct drm_gem_cma_object *bo = drm_fb_cma_get_gem_obj(fb, 0);
859 /* We're skipping the address adjustment for negative origin,
860 * because this is only called on the primary plane.
862 WARN_ON_ONCE(plane->state->crtc_x < 0 || plane->state->crtc_y < 0);
863 addr = bo->paddr + fb->offsets[0];
865 /* Write the new address into the hardware immediately. The
866 * scanout will start from this address as soon as the FIFO
867 * needs to refill with pixels.
869 writel(addr, &vc4_state->hw_dlist[vc4_state->ptr0_offset]);
871 /* Also update the CPU-side dlist copy, so that any later
872 * atomic updates that don't do a new modeset on our plane
873 * also use our updated address.
875 vc4_state->dlist[vc4_state->ptr0_offset] = addr;
878 static void vc4_plane_atomic_async_update(struct drm_plane *plane,
879 struct drm_plane_state *state)
881 struct vc4_plane_state *vc4_state, *new_vc4_state;
883 drm_atomic_set_fb_for_plane(plane->state, state->fb);
884 plane->state->crtc_x = state->crtc_x;
885 plane->state->crtc_y = state->crtc_y;
886 plane->state->crtc_w = state->crtc_w;
887 plane->state->crtc_h = state->crtc_h;
888 plane->state->src_x = state->src_x;
889 plane->state->src_y = state->src_y;
890 plane->state->src_w = state->src_w;
891 plane->state->src_h = state->src_h;
892 plane->state->src_h = state->src_h;
893 plane->state->alpha = state->alpha;
894 plane->state->pixel_blend_mode = state->pixel_blend_mode;
895 plane->state->rotation = state->rotation;
896 plane->state->zpos = state->zpos;
897 plane->state->normalized_zpos = state->normalized_zpos;
898 plane->state->color_encoding = state->color_encoding;
899 plane->state->color_range = state->color_range;
900 plane->state->src = state->src;
901 plane->state->dst = state->dst;
902 plane->state->visible = state->visible;
904 new_vc4_state = to_vc4_plane_state(state);
905 vc4_state = to_vc4_plane_state(plane->state);
907 vc4_state->crtc_x = new_vc4_state->crtc_x;
908 vc4_state->crtc_y = new_vc4_state->crtc_y;
909 vc4_state->crtc_h = new_vc4_state->crtc_h;
910 vc4_state->crtc_w = new_vc4_state->crtc_w;
911 vc4_state->src_x = new_vc4_state->src_x;
912 vc4_state->src_y = new_vc4_state->src_y;
913 memcpy(vc4_state->src_w, new_vc4_state->src_w,
914 sizeof(vc4_state->src_w));
915 memcpy(vc4_state->src_h, new_vc4_state->src_h,
916 sizeof(vc4_state->src_h));
917 memcpy(vc4_state->x_scaling, new_vc4_state->x_scaling,
918 sizeof(vc4_state->x_scaling));
919 memcpy(vc4_state->y_scaling, new_vc4_state->y_scaling,
920 sizeof(vc4_state->y_scaling));
921 vc4_state->is_unity = new_vc4_state->is_unity;
922 vc4_state->is_yuv = new_vc4_state->is_yuv;
923 memcpy(vc4_state->offsets, new_vc4_state->offsets,
924 sizeof(vc4_state->offsets));
925 vc4_state->needs_bg_fill = new_vc4_state->needs_bg_fill;
927 /* Update the current vc4_state pos0, pos2 and ptr0 dlist entries. */
928 vc4_state->dlist[vc4_state->pos0_offset] =
929 new_vc4_state->dlist[vc4_state->pos0_offset];
930 vc4_state->dlist[vc4_state->pos2_offset] =
931 new_vc4_state->dlist[vc4_state->pos2_offset];
932 vc4_state->dlist[vc4_state->ptr0_offset] =
933 new_vc4_state->dlist[vc4_state->ptr0_offset];
935 /* Note that we can't just call vc4_plane_write_dlist()
936 * because that would smash the context data that the HVS is
939 writel(vc4_state->dlist[vc4_state->pos0_offset],
940 &vc4_state->hw_dlist[vc4_state->pos0_offset]);
941 writel(vc4_state->dlist[vc4_state->pos2_offset],
942 &vc4_state->hw_dlist[vc4_state->pos2_offset]);
943 writel(vc4_state->dlist[vc4_state->ptr0_offset],
944 &vc4_state->hw_dlist[vc4_state->ptr0_offset]);
947 static int vc4_plane_atomic_async_check(struct drm_plane *plane,
948 struct drm_plane_state *state)
950 struct vc4_plane_state *old_vc4_state, *new_vc4_state;
954 ret = vc4_plane_mode_set(plane, state);
958 old_vc4_state = to_vc4_plane_state(plane->state);
959 new_vc4_state = to_vc4_plane_state(state);
960 if (old_vc4_state->dlist_count != new_vc4_state->dlist_count ||
961 old_vc4_state->pos0_offset != new_vc4_state->pos0_offset ||
962 old_vc4_state->pos2_offset != new_vc4_state->pos2_offset ||
963 old_vc4_state->ptr0_offset != new_vc4_state->ptr0_offset ||
964 vc4_lbm_size(plane->state) != vc4_lbm_size(state))
967 /* Only pos0, pos2 and ptr0 DWORDS can be updated in an async update
968 * if anything else has changed, fallback to a sync update.
970 for (i = 0; i < new_vc4_state->dlist_count; i++) {
971 if (i == new_vc4_state->pos0_offset ||
972 i == new_vc4_state->pos2_offset ||
973 i == new_vc4_state->ptr0_offset ||
974 (new_vc4_state->lbm_offset &&
975 i == new_vc4_state->lbm_offset))
978 if (new_vc4_state->dlist[i] != old_vc4_state->dlist[i])
985 static int vc4_prepare_fb(struct drm_plane *plane,
986 struct drm_plane_state *state)
989 struct dma_fence *fence;
995 bo = to_vc4_bo(&drm_fb_cma_get_gem_obj(state->fb, 0)->base);
997 fence = reservation_object_get_excl_rcu(bo->resv);
998 drm_atomic_set_fence_for_plane(state, fence);
1000 if (plane->state->fb == state->fb)
1003 ret = vc4_bo_inc_usecnt(bo);
1010 static void vc4_cleanup_fb(struct drm_plane *plane,
1011 struct drm_plane_state *state)
1015 if (plane->state->fb == state->fb || !state->fb)
1018 bo = to_vc4_bo(&drm_fb_cma_get_gem_obj(state->fb, 0)->base);
1019 vc4_bo_dec_usecnt(bo);
1022 static const struct drm_plane_helper_funcs vc4_plane_helper_funcs = {
1023 .atomic_check = vc4_plane_atomic_check,
1024 .atomic_update = vc4_plane_atomic_update,
1025 .prepare_fb = vc4_prepare_fb,
1026 .cleanup_fb = vc4_cleanup_fb,
1027 .atomic_async_check = vc4_plane_atomic_async_check,
1028 .atomic_async_update = vc4_plane_atomic_async_update,
1031 static void vc4_plane_destroy(struct drm_plane *plane)
1033 drm_plane_cleanup(plane);
1036 static bool vc4_format_mod_supported(struct drm_plane *plane,
1040 /* Support T_TILING for RGB formats only. */
1042 case DRM_FORMAT_XRGB8888:
1043 case DRM_FORMAT_ARGB8888:
1044 case DRM_FORMAT_ABGR8888:
1045 case DRM_FORMAT_XBGR8888:
1046 case DRM_FORMAT_RGB565:
1047 case DRM_FORMAT_BGR565:
1048 case DRM_FORMAT_ARGB1555:
1049 case DRM_FORMAT_XRGB1555:
1050 switch (fourcc_mod_broadcom_mod(modifier)) {
1051 case DRM_FORMAT_MOD_LINEAR:
1052 case DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED:
1053 case DRM_FORMAT_MOD_BROADCOM_SAND64:
1054 case DRM_FORMAT_MOD_BROADCOM_SAND128:
1059 case DRM_FORMAT_NV12:
1060 case DRM_FORMAT_NV21:
1061 switch (fourcc_mod_broadcom_mod(modifier)) {
1062 case DRM_FORMAT_MOD_LINEAR:
1063 case DRM_FORMAT_MOD_BROADCOM_SAND64:
1064 case DRM_FORMAT_MOD_BROADCOM_SAND128:
1065 case DRM_FORMAT_MOD_BROADCOM_SAND256:
1070 case DRM_FORMAT_YUV422:
1071 case DRM_FORMAT_YVU422:
1072 case DRM_FORMAT_YUV420:
1073 case DRM_FORMAT_YVU420:
1074 case DRM_FORMAT_NV16:
1075 case DRM_FORMAT_NV61:
1077 return (modifier == DRM_FORMAT_MOD_LINEAR);
1081 static const struct drm_plane_funcs vc4_plane_funcs = {
1082 .update_plane = drm_atomic_helper_update_plane,
1083 .disable_plane = drm_atomic_helper_disable_plane,
1084 .destroy = vc4_plane_destroy,
1085 .set_property = NULL,
1086 .reset = vc4_plane_reset,
1087 .atomic_duplicate_state = vc4_plane_duplicate_state,
1088 .atomic_destroy_state = vc4_plane_destroy_state,
1089 .format_mod_supported = vc4_format_mod_supported,
1092 struct drm_plane *vc4_plane_init(struct drm_device *dev,
1093 enum drm_plane_type type)
1095 struct drm_plane *plane = NULL;
1096 struct vc4_plane *vc4_plane;
1097 u32 formats[ARRAY_SIZE(hvs_formats)];
1100 static const uint64_t modifiers[] = {
1101 DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED,
1102 DRM_FORMAT_MOD_BROADCOM_SAND128,
1103 DRM_FORMAT_MOD_BROADCOM_SAND64,
1104 DRM_FORMAT_MOD_BROADCOM_SAND256,
1105 DRM_FORMAT_MOD_LINEAR,
1106 DRM_FORMAT_MOD_INVALID
1109 vc4_plane = devm_kzalloc(dev->dev, sizeof(*vc4_plane),
1112 return ERR_PTR(-ENOMEM);
1114 for (i = 0; i < ARRAY_SIZE(hvs_formats); i++)
1115 formats[i] = hvs_formats[i].drm;
1117 plane = &vc4_plane->base;
1118 ret = drm_universal_plane_init(dev, plane, 0,
1120 formats, ARRAY_SIZE(formats),
1121 modifiers, type, NULL);
1123 drm_plane_helper_add(plane, &vc4_plane_helper_funcs);
1125 drm_plane_create_alpha_property(plane);