1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2015 Broadcom
7 * DOC: VC4 plane module
9 * Each DRM plane is a layer of pixels being scanned out by the HVS.
11 * At atomic modeset check time, we compute the HVS display element
12 * state that would be necessary for displaying the plane (giving us a
13 * chance to figure out if a plane configuration is invalid), then at
14 * atomic flush time the CRTC will ask us to write our element state
15 * into the region of the HVS that it has allocated for us.
18 #include <drm/drm_atomic.h>
19 #include <drm/drm_atomic_helper.h>
20 #include <drm/drm_atomic_uapi.h>
21 #include <drm/drm_blend.h>
22 #include <drm/drm_drv.h>
23 #include <drm/drm_fb_dma_helper.h>
24 #include <drm/drm_fourcc.h>
25 #include <drm/drm_framebuffer.h>
26 #include <drm/drm_gem_atomic_helper.h>
28 #include "uapi/drm/vc4_drm.h"
33 static const struct hvs_format {
34 u32 drm; /* DRM_FORMAT_* */
35 u32 hvs; /* HVS_FORMAT_* */
41 .drm = DRM_FORMAT_XRGB8888,
42 .hvs = HVS_PIXEL_FORMAT_RGBA8888,
43 .pixel_order = HVS_PIXEL_ORDER_ABGR,
44 .pixel_order_hvs5 = HVS_PIXEL_ORDER_ARGB,
47 .drm = DRM_FORMAT_ARGB8888,
48 .hvs = HVS_PIXEL_FORMAT_RGBA8888,
49 .pixel_order = HVS_PIXEL_ORDER_ABGR,
50 .pixel_order_hvs5 = HVS_PIXEL_ORDER_ARGB,
53 .drm = DRM_FORMAT_ABGR8888,
54 .hvs = HVS_PIXEL_FORMAT_RGBA8888,
55 .pixel_order = HVS_PIXEL_ORDER_ARGB,
56 .pixel_order_hvs5 = HVS_PIXEL_ORDER_ABGR,
59 .drm = DRM_FORMAT_XBGR8888,
60 .hvs = HVS_PIXEL_FORMAT_RGBA8888,
61 .pixel_order = HVS_PIXEL_ORDER_ARGB,
62 .pixel_order_hvs5 = HVS_PIXEL_ORDER_ABGR,
65 .drm = DRM_FORMAT_RGB565,
66 .hvs = HVS_PIXEL_FORMAT_RGB565,
67 .pixel_order = HVS_PIXEL_ORDER_XRGB,
68 .pixel_order_hvs5 = HVS_PIXEL_ORDER_XRGB,
71 .drm = DRM_FORMAT_BGR565,
72 .hvs = HVS_PIXEL_FORMAT_RGB565,
73 .pixel_order = HVS_PIXEL_ORDER_XBGR,
74 .pixel_order_hvs5 = HVS_PIXEL_ORDER_XBGR,
77 .drm = DRM_FORMAT_ARGB1555,
78 .hvs = HVS_PIXEL_FORMAT_RGBA5551,
79 .pixel_order = HVS_PIXEL_ORDER_ABGR,
80 .pixel_order_hvs5 = HVS_PIXEL_ORDER_ARGB,
83 .drm = DRM_FORMAT_XRGB1555,
84 .hvs = HVS_PIXEL_FORMAT_RGBA5551,
85 .pixel_order = HVS_PIXEL_ORDER_ABGR,
86 .pixel_order_hvs5 = HVS_PIXEL_ORDER_ARGB,
89 .drm = DRM_FORMAT_RGB888,
90 .hvs = HVS_PIXEL_FORMAT_RGB888,
91 .pixel_order = HVS_PIXEL_ORDER_XRGB,
92 .pixel_order_hvs5 = HVS_PIXEL_ORDER_XRGB,
95 .drm = DRM_FORMAT_BGR888,
96 .hvs = HVS_PIXEL_FORMAT_RGB888,
97 .pixel_order = HVS_PIXEL_ORDER_XBGR,
98 .pixel_order_hvs5 = HVS_PIXEL_ORDER_XBGR,
101 .drm = DRM_FORMAT_YUV422,
102 .hvs = HVS_PIXEL_FORMAT_YCBCR_YUV422_3PLANE,
103 .pixel_order = HVS_PIXEL_ORDER_XYCBCR,
104 .pixel_order_hvs5 = HVS_PIXEL_ORDER_XYCBCR,
107 .drm = DRM_FORMAT_YVU422,
108 .hvs = HVS_PIXEL_FORMAT_YCBCR_YUV422_3PLANE,
109 .pixel_order = HVS_PIXEL_ORDER_XYCRCB,
110 .pixel_order_hvs5 = HVS_PIXEL_ORDER_XYCRCB,
113 .drm = DRM_FORMAT_YUV444,
114 .hvs = HVS_PIXEL_FORMAT_YCBCR_YUV422_3PLANE,
115 .pixel_order = HVS_PIXEL_ORDER_XYCBCR,
116 .pixel_order_hvs5 = HVS_PIXEL_ORDER_XYCBCR,
119 .drm = DRM_FORMAT_YVU444,
120 .hvs = HVS_PIXEL_FORMAT_YCBCR_YUV422_3PLANE,
121 .pixel_order = HVS_PIXEL_ORDER_XYCRCB,
122 .pixel_order_hvs5 = HVS_PIXEL_ORDER_XYCRCB,
125 .drm = DRM_FORMAT_YUV420,
126 .hvs = HVS_PIXEL_FORMAT_YCBCR_YUV420_3PLANE,
127 .pixel_order = HVS_PIXEL_ORDER_XYCBCR,
128 .pixel_order_hvs5 = HVS_PIXEL_ORDER_XYCBCR,
131 .drm = DRM_FORMAT_YVU420,
132 .hvs = HVS_PIXEL_FORMAT_YCBCR_YUV420_3PLANE,
133 .pixel_order = HVS_PIXEL_ORDER_XYCRCB,
134 .pixel_order_hvs5 = HVS_PIXEL_ORDER_XYCRCB,
137 .drm = DRM_FORMAT_NV12,
138 .hvs = HVS_PIXEL_FORMAT_YCBCR_YUV420_2PLANE,
139 .pixel_order = HVS_PIXEL_ORDER_XYCBCR,
140 .pixel_order_hvs5 = HVS_PIXEL_ORDER_XYCBCR,
143 .drm = DRM_FORMAT_NV21,
144 .hvs = HVS_PIXEL_FORMAT_YCBCR_YUV420_2PLANE,
145 .pixel_order = HVS_PIXEL_ORDER_XYCRCB,
146 .pixel_order_hvs5 = HVS_PIXEL_ORDER_XYCRCB,
149 .drm = DRM_FORMAT_NV16,
150 .hvs = HVS_PIXEL_FORMAT_YCBCR_YUV422_2PLANE,
151 .pixel_order = HVS_PIXEL_ORDER_XYCBCR,
152 .pixel_order_hvs5 = HVS_PIXEL_ORDER_XYCBCR,
155 .drm = DRM_FORMAT_NV61,
156 .hvs = HVS_PIXEL_FORMAT_YCBCR_YUV422_2PLANE,
157 .pixel_order = HVS_PIXEL_ORDER_XYCRCB,
158 .pixel_order_hvs5 = HVS_PIXEL_ORDER_XYCRCB,
161 .drm = DRM_FORMAT_P030,
162 .hvs = HVS_PIXEL_FORMAT_YCBCR_10BIT,
163 .pixel_order_hvs5 = HVS_PIXEL_ORDER_XYCBCR,
167 .drm = DRM_FORMAT_XRGB2101010,
168 .hvs = HVS_PIXEL_FORMAT_RGBA1010102,
169 .pixel_order_hvs5 = HVS_PIXEL_ORDER_ARGB,
173 .drm = DRM_FORMAT_ARGB2101010,
174 .hvs = HVS_PIXEL_FORMAT_RGBA1010102,
175 .pixel_order_hvs5 = HVS_PIXEL_ORDER_ARGB,
179 .drm = DRM_FORMAT_ABGR2101010,
180 .hvs = HVS_PIXEL_FORMAT_RGBA1010102,
181 .pixel_order_hvs5 = HVS_PIXEL_ORDER_ABGR,
185 .drm = DRM_FORMAT_XBGR2101010,
186 .hvs = HVS_PIXEL_FORMAT_RGBA1010102,
187 .pixel_order_hvs5 = HVS_PIXEL_ORDER_ABGR,
191 .drm = DRM_FORMAT_RGB332,
192 .hvs = HVS_PIXEL_FORMAT_RGB332,
193 .pixel_order = HVS_PIXEL_ORDER_ARGB,
194 .pixel_order_hvs5 = HVS_PIXEL_ORDER_ARGB,
197 .drm = DRM_FORMAT_BGR233,
198 .hvs = HVS_PIXEL_FORMAT_RGB332,
199 .pixel_order = HVS_PIXEL_ORDER_ABGR,
200 .pixel_order_hvs5 = HVS_PIXEL_ORDER_ABGR,
203 .drm = DRM_FORMAT_XRGB4444,
204 .hvs = HVS_PIXEL_FORMAT_RGBA4444,
205 .pixel_order = HVS_PIXEL_ORDER_ABGR,
206 .pixel_order_hvs5 = HVS_PIXEL_ORDER_ARGB,
209 .drm = DRM_FORMAT_ARGB4444,
210 .hvs = HVS_PIXEL_FORMAT_RGBA4444,
211 .pixel_order = HVS_PIXEL_ORDER_ABGR,
212 .pixel_order_hvs5 = HVS_PIXEL_ORDER_ARGB,
215 .drm = DRM_FORMAT_XBGR4444,
216 .hvs = HVS_PIXEL_FORMAT_RGBA4444,
217 .pixel_order = HVS_PIXEL_ORDER_ARGB,
218 .pixel_order_hvs5 = HVS_PIXEL_ORDER_ABGR,
221 .drm = DRM_FORMAT_ABGR4444,
222 .hvs = HVS_PIXEL_FORMAT_RGBA4444,
223 .pixel_order = HVS_PIXEL_ORDER_ARGB,
224 .pixel_order_hvs5 = HVS_PIXEL_ORDER_ABGR,
227 .drm = DRM_FORMAT_BGRX4444,
228 .hvs = HVS_PIXEL_FORMAT_RGBA4444,
229 .pixel_order = HVS_PIXEL_ORDER_RGBA,
230 .pixel_order_hvs5 = HVS_PIXEL_ORDER_BGRA,
233 .drm = DRM_FORMAT_BGRA4444,
234 .hvs = HVS_PIXEL_FORMAT_RGBA4444,
235 .pixel_order = HVS_PIXEL_ORDER_RGBA,
236 .pixel_order_hvs5 = HVS_PIXEL_ORDER_BGRA,
239 .drm = DRM_FORMAT_RGBX4444,
240 .hvs = HVS_PIXEL_FORMAT_RGBA4444,
241 .pixel_order = HVS_PIXEL_ORDER_BGRA,
242 .pixel_order_hvs5 = HVS_PIXEL_ORDER_RGBA,
245 .drm = DRM_FORMAT_RGBA4444,
246 .hvs = HVS_PIXEL_FORMAT_RGBA4444,
247 .pixel_order = HVS_PIXEL_ORDER_BGRA,
248 .pixel_order_hvs5 = HVS_PIXEL_ORDER_RGBA,
252 static const struct hvs_format *vc4_get_hvs_format(u32 drm_format)
256 for (i = 0; i < ARRAY_SIZE(hvs_formats); i++) {
257 if (hvs_formats[i].drm == drm_format)
258 return &hvs_formats[i];
264 static enum vc4_scaling_mode vc4_get_scaling_mode(u32 src, u32 dst)
266 if (dst == src >> 16)
267 return VC4_SCALING_NONE;
268 if (3 * dst >= 2 * (src >> 16))
269 return VC4_SCALING_PPF;
271 return VC4_SCALING_TPZ;
274 static bool plane_enabled(struct drm_plane_state *state)
276 return state->fb && !WARN_ON(!state->crtc);
279 struct drm_plane_state *vc4_plane_duplicate_state(struct drm_plane *plane)
281 struct vc4_plane_state *vc4_state;
284 if (WARN_ON(!plane->state))
287 vc4_state = kmemdup(plane->state, sizeof(*vc4_state), GFP_KERNEL);
291 memset(&vc4_state->upm, 0, sizeof(vc4_state->upm));
293 for (i = 0; i < DRM_FORMAT_MAX_PLANES; i++)
294 vc4_state->upm_handle[i] = 0;
296 vc4_state->dlist_initialized = 0;
298 __drm_atomic_helper_plane_duplicate_state(plane, &vc4_state->base);
300 if (vc4_state->dlist) {
301 vc4_state->dlist = kmemdup(vc4_state->dlist,
302 vc4_state->dlist_count * 4,
304 if (!vc4_state->dlist) {
308 vc4_state->dlist_size = vc4_state->dlist_count;
311 return &vc4_state->base;
314 void vc4_plane_destroy_state(struct drm_plane *plane,
315 struct drm_plane_state *state)
317 struct vc4_dev *vc4 = to_vc4_dev(plane->dev);
318 struct vc4_hvs *hvs = vc4->hvs;
319 struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
322 for (i = 0; i < DRM_FORMAT_MAX_PLANES; i++) {
323 unsigned long irqflags;
325 if (!drm_mm_node_allocated(&vc4_state->upm[i]))
328 spin_lock_irqsave(&hvs->mm_lock, irqflags);
329 drm_mm_remove_node(&vc4_state->upm[i]);
330 spin_unlock_irqrestore(&hvs->mm_lock, irqflags);
332 if (vc4_state->upm_handle[i] > 0)
333 ida_free(&hvs->upm_handles, vc4_state->upm_handle[i]);
336 kfree(vc4_state->dlist);
337 __drm_atomic_helper_plane_destroy_state(&vc4_state->base);
341 /* Called during init to allocate the plane's atomic state. */
342 void vc4_plane_reset(struct drm_plane *plane)
344 struct vc4_plane_state *vc4_state;
346 WARN_ON(plane->state);
348 vc4_state = kzalloc(sizeof(*vc4_state), GFP_KERNEL);
352 __drm_atomic_helper_plane_reset(plane, &vc4_state->base);
355 static void vc4_dlist_counter_increment(struct vc4_plane_state *vc4_state)
357 if (vc4_state->dlist_count == vc4_state->dlist_size) {
358 u32 new_size = max(4u, vc4_state->dlist_count * 2);
359 u32 *new_dlist = kmalloc_array(new_size, 4, GFP_KERNEL);
363 memcpy(new_dlist, vc4_state->dlist, vc4_state->dlist_count * 4);
365 kfree(vc4_state->dlist);
366 vc4_state->dlist = new_dlist;
367 vc4_state->dlist_size = new_size;
370 vc4_state->dlist_count++;
373 static void vc4_dlist_write(struct vc4_plane_state *vc4_state, u32 val)
375 unsigned int idx = vc4_state->dlist_count;
377 vc4_dlist_counter_increment(vc4_state);
378 vc4_state->dlist[idx] = val;
381 /* Returns the scl0/scl1 field based on whether the dimensions need to
382 * be up/down/non-scaled.
384 * This is a replication of a table from the spec.
386 static u32 vc4_get_scl_field(struct drm_plane_state *state, int plane)
388 struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
390 switch (vc4_state->x_scaling[plane] << 2 | vc4_state->y_scaling[plane]) {
391 case VC4_SCALING_PPF << 2 | VC4_SCALING_PPF:
392 return SCALER_CTL0_SCL_H_PPF_V_PPF;
393 case VC4_SCALING_TPZ << 2 | VC4_SCALING_PPF:
394 return SCALER_CTL0_SCL_H_TPZ_V_PPF;
395 case VC4_SCALING_PPF << 2 | VC4_SCALING_TPZ:
396 return SCALER_CTL0_SCL_H_PPF_V_TPZ;
397 case VC4_SCALING_TPZ << 2 | VC4_SCALING_TPZ:
398 return SCALER_CTL0_SCL_H_TPZ_V_TPZ;
399 case VC4_SCALING_PPF << 2 | VC4_SCALING_NONE:
400 return SCALER_CTL0_SCL_H_PPF_V_NONE;
401 case VC4_SCALING_NONE << 2 | VC4_SCALING_PPF:
402 return SCALER_CTL0_SCL_H_NONE_V_PPF;
403 case VC4_SCALING_NONE << 2 | VC4_SCALING_TPZ:
404 return SCALER_CTL0_SCL_H_NONE_V_TPZ;
405 case VC4_SCALING_TPZ << 2 | VC4_SCALING_NONE:
406 return SCALER_CTL0_SCL_H_TPZ_V_NONE;
408 case VC4_SCALING_NONE << 2 | VC4_SCALING_NONE:
409 /* The unity case is independently handled by
416 static int vc4_plane_margins_adj(struct drm_plane_state *pstate)
418 struct vc4_plane_state *vc4_pstate = to_vc4_plane_state(pstate);
419 unsigned int left, right, top, bottom, adjhdisplay, adjvdisplay;
420 struct drm_crtc_state *crtc_state;
422 crtc_state = drm_atomic_get_new_crtc_state(pstate->state,
425 vc4_crtc_get_margins(crtc_state, &left, &right, &top, &bottom);
426 if (!left && !right && !top && !bottom)
429 if (left + right >= crtc_state->mode.hdisplay ||
430 top + bottom >= crtc_state->mode.vdisplay)
433 adjhdisplay = crtc_state->mode.hdisplay - (left + right);
434 vc4_pstate->crtc_x = DIV_ROUND_CLOSEST(vc4_pstate->crtc_x *
436 crtc_state->mode.hdisplay);
437 vc4_pstate->crtc_x += left;
438 if (vc4_pstate->crtc_x > crtc_state->mode.hdisplay - right)
439 vc4_pstate->crtc_x = crtc_state->mode.hdisplay - right;
441 adjvdisplay = crtc_state->mode.vdisplay - (top + bottom);
442 vc4_pstate->crtc_y = DIV_ROUND_CLOSEST(vc4_pstate->crtc_y *
444 crtc_state->mode.vdisplay);
445 vc4_pstate->crtc_y += top;
446 if (vc4_pstate->crtc_y > crtc_state->mode.vdisplay - bottom)
447 vc4_pstate->crtc_y = crtc_state->mode.vdisplay - bottom;
449 vc4_pstate->crtc_w = DIV_ROUND_CLOSEST(vc4_pstate->crtc_w *
451 crtc_state->mode.hdisplay);
452 vc4_pstate->crtc_h = DIV_ROUND_CLOSEST(vc4_pstate->crtc_h *
454 crtc_state->mode.vdisplay);
456 if (!vc4_pstate->crtc_w || !vc4_pstate->crtc_h)
462 static int vc4_plane_setup_clipping_and_scaling(struct drm_plane_state *state)
464 struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
465 struct drm_framebuffer *fb = state->fb;
466 int num_planes = fb->format->num_planes;
467 struct drm_crtc_state *crtc_state;
468 u32 h_subsample = fb->format->hsub;
469 u32 v_subsample = fb->format->vsub;
472 crtc_state = drm_atomic_get_existing_crtc_state(state->state,
475 DRM_DEBUG_KMS("Invalid crtc state\n");
479 ret = drm_atomic_helper_check_plane_state(state, crtc_state, 1,
480 INT_MAX, true, true);
484 vc4_state->src_x = state->src.x1;
485 vc4_state->src_y = state->src.y1;
486 vc4_state->src_w[0] = state->src.x2 - vc4_state->src_x;
487 vc4_state->src_h[0] = state->src.y2 - vc4_state->src_y;
489 vc4_state->crtc_x = state->dst.x1;
490 vc4_state->crtc_y = state->dst.y1;
491 vc4_state->crtc_w = state->dst.x2 - state->dst.x1;
492 vc4_state->crtc_h = state->dst.y2 - state->dst.y1;
494 if (!vc4_state->crtc_w)
495 vc4_state->crtc_w = state->crtc->mode.hdisplay;
496 if (!vc4_state->crtc_h)
497 vc4_state->crtc_h = state->crtc->mode.vdisplay;
499 ret = vc4_plane_margins_adj(state);
503 vc4_state->x_scaling[0] = vc4_get_scaling_mode(vc4_state->src_w[0],
505 vc4_state->y_scaling[0] = vc4_get_scaling_mode(vc4_state->src_h[0],
508 vc4_state->is_unity = (vc4_state->x_scaling[0] == VC4_SCALING_NONE &&
509 vc4_state->y_scaling[0] == VC4_SCALING_NONE);
511 if (num_planes > 1) {
512 vc4_state->is_yuv = true;
514 vc4_state->src_w[1] = vc4_state->src_w[0] / h_subsample;
515 vc4_state->src_h[1] = vc4_state->src_h[0] / v_subsample;
517 vc4_state->x_scaling[1] =
518 vc4_get_scaling_mode(vc4_state->src_w[1],
520 vc4_state->y_scaling[1] =
521 vc4_get_scaling_mode(vc4_state->src_h[1],
524 /* YUV conversion requires that horizontal scaling be enabled
525 * on the UV plane even if vc4_get_scaling_mode() returned
526 * VC4_SCALING_NONE (which can happen when the down-scaling
527 * ratio is 0.5). Let's force it to VC4_SCALING_PPF in this
530 if (vc4_state->x_scaling[1] == VC4_SCALING_NONE)
531 vc4_state->x_scaling[1] = VC4_SCALING_PPF;
533 /* Similarly UV needs vertical scaling to be enabled.
534 * Without this a 1:1 scaled YUV422 plane isn't rendered.
536 if (vc4_state->y_scaling[1] == VC4_SCALING_NONE)
537 vc4_state->y_scaling[1] = VC4_SCALING_PPF;
539 vc4_state->is_yuv = false;
540 vc4_state->x_scaling[1] = VC4_SCALING_NONE;
541 vc4_state->y_scaling[1] = VC4_SCALING_NONE;
547 static void vc4_write_tpz(struct vc4_plane_state *vc4_state, u32 src, u32 dst)
549 struct vc4_dev *vc4 = to_vc4_dev(vc4_state->base.plane->dev);
552 WARN_ON_ONCE(vc4->gen > VC4_GEN_6);
556 /* The specs note that while the reciprocal would be defined
557 * as (1<<32)/scale, ~0 is close enough.
561 vc4_dlist_write(vc4_state,
563 * The BCM2712 is lacking BIT(31) compared to
564 * the previous generations, but we don't use
567 VC4_SET_FIELD(scale, SCALER_TPZ0_SCALE) |
568 VC4_SET_FIELD(0, SCALER_TPZ0_IPHASE));
569 vc4_dlist_write(vc4_state,
570 VC4_SET_FIELD(recip, SCALER_TPZ1_RECIP));
573 /* phase magnitude bits */
576 static void vc4_write_ppf(struct vc4_plane_state *vc4_state, u32 src, u32 dst, u32 xy, int channel, int chroma_offset)
578 struct vc4_dev *vc4 = to_vc4_dev(vc4_state->base.plane->dev);
579 u32 scale = src / dst;
583 WARN_ON_ONCE(vc4->gen > VC4_GEN_6);
585 /* Start the phase at 1/2 pixel from the 1st pixel at src_x.
586 1/4 pixel for YUV, plus the offset for chroma siting */
588 /* the phase is relative to scale_src->x, so shift it for display list's x value */
589 offset = (xy & 0x1ffff) >> (16 - PHASE_BITS) >> 1;
590 offset -= chroma_offset >> (17 - PHASE_BITS);
591 offset += -(1 << PHASE_BITS >> 2);
593 /* the phase is relative to scale_src->x, so shift it for display list's x value */
594 offset = (xy & 0xffff) >> (16 - PHASE_BITS);
595 offset += -(1 << PHASE_BITS >> 1);
597 /* This is a kludge to make sure the scaling factors are consitent with YUV's luma scaling.
598 we lose 1bit precision because of this. */
602 /* There may be a also small error introduced by precision of scale.
603 Add half of that as a compromise */
604 offset2 = src - dst * scale;
605 offset2 >>= 16 - PHASE_BITS;
606 phase = offset + (offset2 >> 1);
608 /* Ensure +ve values don't touch the sign bit, then truncate negative values */
609 if (phase >= 1 << PHASE_BITS)
610 phase = (1 << PHASE_BITS) - 1;
612 phase &= SCALER_PPF_IPHASE_MASK;
614 vc4_dlist_write(vc4_state,
616 VC4_SET_FIELD(scale, SCALER_PPF_SCALE) |
618 * The register layout documentation is slightly
619 * different to setup the phase in the BCM2712,
620 * but they seem equivalent.
622 VC4_SET_FIELD(phase, SCALER_PPF_IPHASE));
625 static u32 __vc4_lbm_size(struct drm_plane_state *state)
627 struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
628 struct vc4_dev *vc4 = to_vc4_dev(state->plane->dev);
632 /* LBM is not needed when there's no vertical scaling. */
633 if (vc4_state->y_scaling[0] == VC4_SCALING_NONE &&
634 vc4_state->y_scaling[1] == VC4_SCALING_NONE)
638 * This can be further optimized in the RGB/YUV444 case if the PPF
639 * decimation factor is between 0.5 and 1.0 by using crtc_w.
641 * It's not an issue though, since in that case since src_w[0] is going
642 * to be greater than or equal to crtc_w.
644 if (vc4_state->x_scaling[0] == VC4_SCALING_TPZ)
645 pix_per_line = vc4_state->crtc_w;
647 pix_per_line = vc4_state->src_w[0] >> 16;
649 if (!vc4_state->is_yuv) {
650 if (vc4_state->y_scaling[0] == VC4_SCALING_TPZ)
651 lbm = pix_per_line * 8;
653 /* In special cases, this multiplier might be 12. */
654 lbm = pix_per_line * 16;
657 /* There are cases for this going down to a multiplier
658 * of 2, but according to the firmware source, the
659 * table in the docs is somewhat wrong.
661 lbm = pix_per_line * 16;
664 /* Align it to 64 or 128 (hvs5) bytes */
665 lbm = roundup(lbm, vc4->gen == VC4_GEN_5 ? 128 : 64);
667 /* Each "word" of the LBM memory contains 2 or 4 (hvs5) pixels */
668 lbm /= vc4->gen == VC4_GEN_5 ? 4 : 2;
673 static unsigned int vc4_lbm_words_per_component(const struct drm_plane_state *state,
674 unsigned int channel)
676 const struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
678 switch (vc4_state->y_scaling[channel]) {
679 case VC4_SCALING_PPF:
682 case VC4_SCALING_TPZ:
690 static unsigned int vc4_lbm_components(const struct drm_plane_state *state,
691 unsigned int channel)
693 const struct drm_format_info *info = state->fb->format;
694 const struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
696 if (vc4_state->y_scaling[channel] == VC4_SCALING_NONE)
700 return channel ? 2 : 1;
708 static unsigned int vc4_lbm_channel_size(const struct drm_plane_state *state,
709 unsigned int channel)
711 const struct drm_format_info *info = state->fb->format;
712 const struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
713 unsigned int channels_scaled = 0;
714 unsigned int components, words, wpc;
715 unsigned int width, lines;
718 /* LBM is meant to use the smaller of source or dest width, but there
719 * is a issue with UV scaling that the size required for the second
720 * channel is based on the source width only.
722 if (info->hsub > 1 && channel == 1)
723 width = state->src_w >> 16;
725 width = min(state->src_w >> 16, state->crtc_w);
726 width = round_up(width / info->hsub, 4);
728 wpc = vc4_lbm_words_per_component(state, channel);
732 components = vc4_lbm_components(state, channel);
736 if (state->alpha != DRM_BLEND_ALPHA_OPAQUE)
739 words = width * wpc * components;
741 lines = DIV_ROUND_UP(words, 128 / info->hsub);
743 for (i = 0; i < 2; i++)
744 if (vc4_state->y_scaling[channel] != VC4_SCALING_NONE)
747 if (channels_scaled == 1)
753 static unsigned int __vc6_lbm_size(const struct drm_plane_state *state)
755 const struct drm_format_info *info = state->fb->format;
758 return max(vc4_lbm_channel_size(state, 0),
759 vc4_lbm_channel_size(state, 1));
761 return vc4_lbm_channel_size(state, 0);
764 u32 vc4_lbm_size(struct drm_plane_state *state)
766 struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
767 struct vc4_dev *vc4 = to_vc4_dev(state->plane->dev);
769 /* LBM is not needed when there's no vertical scaling. */
770 if (vc4_state->y_scaling[0] == VC4_SCALING_NONE &&
771 vc4_state->y_scaling[1] == VC4_SCALING_NONE)
774 if (vc4->gen >= VC4_GEN_6)
775 return __vc6_lbm_size(state);
777 return __vc4_lbm_size(state);
780 static size_t vc6_upm_size(const struct drm_plane_state *state,
783 const struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
784 unsigned int stride = state->fb->pitches[plane];
787 * TODO: This only works for raster formats, and is sub-optimal
788 * for buffers with a stride aligned on 32 bytes.
790 unsigned int words_per_line = (stride + 62) / 32;
791 unsigned int fetch_region_size = words_per_line * 32;
792 unsigned int buffer_lines = 2 << vc4_state->upm_buffer_lines;
793 unsigned int buffer_size = fetch_region_size * buffer_lines;
795 return ALIGN(buffer_size, HVS_UBM_WORD_SIZE);
798 static void vc4_write_scaling_parameters(struct drm_plane_state *state,
801 struct vc4_dev *vc4 = to_vc4_dev(state->plane->dev);
802 struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
804 WARN_ON_ONCE(vc4->gen > VC4_GEN_6);
806 /* Ch0 H-PPF Word 0: Scaling Parameters */
807 if (vc4_state->x_scaling[channel] == VC4_SCALING_PPF) {
808 vc4_write_ppf(vc4_state,
809 vc4_state->src_w[channel], vc4_state->crtc_w, vc4_state->src_x, channel,
810 state->chroma_siting_h);
813 /* Ch0 V-PPF Words 0-1: Scaling Parameters, Context */
814 if (vc4_state->y_scaling[channel] == VC4_SCALING_PPF) {
815 vc4_write_ppf(vc4_state,
816 vc4_state->src_h[channel], vc4_state->crtc_h, vc4_state->src_y, channel,
817 state->chroma_siting_v);
818 vc4_dlist_write(vc4_state, 0xc0c0c0c0);
821 /* Ch0 H-TPZ Words 0-1: Scaling Parameters, Recip */
822 if (vc4_state->x_scaling[channel] == VC4_SCALING_TPZ) {
823 vc4_write_tpz(vc4_state,
824 vc4_state->src_w[channel], vc4_state->crtc_w);
827 /* Ch0 V-TPZ Words 0-2: Scaling Parameters, Recip, Context */
828 if (vc4_state->y_scaling[channel] == VC4_SCALING_TPZ) {
829 vc4_write_tpz(vc4_state,
830 vc4_state->src_h[channel], vc4_state->crtc_h);
831 vc4_dlist_write(vc4_state, 0xc0c0c0c0);
835 static void vc4_plane_calc_load(struct drm_plane_state *state)
837 unsigned int hvs_load_shift, vrefresh, i;
838 struct drm_framebuffer *fb = state->fb;
839 struct vc4_plane_state *vc4_state;
840 struct drm_crtc_state *crtc_state;
841 unsigned int vscale_factor;
843 vc4_state = to_vc4_plane_state(state);
844 crtc_state = drm_atomic_get_existing_crtc_state(state->state,
846 vrefresh = drm_mode_vrefresh(&crtc_state->adjusted_mode);
848 /* The HVS is able to process 2 pixels/cycle when scaling the source,
849 * 4 pixels/cycle otherwise.
850 * Alpha blending step seems to be pipelined and it's always operating
851 * at 4 pixels/cycle, so the limiting aspect here seems to be the
853 * HVS load is expressed in clk-cycles/sec (AKA Hz).
855 if (vc4_state->x_scaling[0] != VC4_SCALING_NONE ||
856 vc4_state->x_scaling[1] != VC4_SCALING_NONE ||
857 vc4_state->y_scaling[0] != VC4_SCALING_NONE ||
858 vc4_state->y_scaling[1] != VC4_SCALING_NONE)
863 vc4_state->membus_load = 0;
864 vc4_state->hvs_load = 0;
865 for (i = 0; i < fb->format->num_planes; i++) {
866 /* Even if the bandwidth/plane required for a single frame is
868 * (vc4_state->src_w[i] >> 16) * (vc4_state->src_h[i] >> 16) *
871 * when downscaling, we have to read more pixels per line in
872 * the time frame reserved for a single line, so the bandwidth
873 * demand can be punctually higher. To account for that, we
874 * calculate the down-scaling factor and multiply the plane
875 * load by this number. We're likely over-estimating the read
876 * demand, but that's better than under-estimating it.
878 vscale_factor = DIV_ROUND_UP(vc4_state->src_h[i] >> 16,
880 vc4_state->membus_load += (vc4_state->src_w[i] >> 16) *
881 (vc4_state->src_h[i] >> 16) *
882 vscale_factor * fb->format->cpp[i];
883 vc4_state->hvs_load += vc4_state->crtc_h * vc4_state->crtc_w;
886 vc4_state->hvs_load *= vrefresh;
887 vc4_state->hvs_load >>= hvs_load_shift;
888 vc4_state->membus_load *= vrefresh;
891 static int vc4_plane_allocate_lbm(struct drm_plane_state *state)
893 struct drm_device *drm = state->plane->dev;
894 struct vc4_dev *vc4 = to_vc4_dev(drm);
895 struct drm_plane *plane = state->plane;
896 struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
899 lbm_size = vc4_lbm_size(state);
901 vc4_state->lbm_size = 0;
906 * NOTE: BCM2712 doesn't need to be aligned, since the size
907 * returned by vc4_lbm_size() is in words already.
909 if (vc4->gen == VC4_GEN_5)
910 lbm_size = ALIGN(lbm_size, 64);
911 else if (vc4->gen == VC4_GEN_4)
912 lbm_size = ALIGN(lbm_size, 32);
914 drm_dbg_driver(drm, "[PLANE:%d:%s] LBM Allocation Size: %u\n",
915 plane->base.id, plane->name, lbm_size);
917 if (WARN_ON(!vc4_state->lbm_offset))
920 /* FIXME: Add loop here that ensures that the total LBM assigned in this
921 * state is less than the total lbm size
923 vc4_state->lbm_size = lbm_size;
928 static int vc6_plane_allocate_upm(struct drm_plane_state *state)
930 const struct drm_format_info *info = state->fb->format;
931 struct drm_device *drm = state->plane->dev;
932 struct vc4_dev *vc4 = to_vc4_dev(drm);
933 struct vc4_hvs *hvs = vc4->hvs;
934 struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
938 WARN_ON_ONCE(vc4->gen < VC4_GEN_6);
940 vc4_state->upm_buffer_lines = SCALER6_PTR0_UPM_BUFF_SIZE_2_LINES;
942 for (i = 0; i < info->num_planes; i++) {
943 unsigned long irqflags;
946 upm_size = vc6_upm_size(state, i);
950 spin_lock_irqsave(&hvs->mm_lock, irqflags);
951 ret = drm_mm_insert_node_generic(&hvs->upm_mm,
953 upm_size, HVS_UBM_WORD_SIZE,
955 spin_unlock_irqrestore(&hvs->mm_lock, irqflags);
957 drm_err(drm, "Failed to allocate UPM entry: %d\n", ret);
961 ret = ida_alloc_range(&hvs->upm_handles, 1, 32, GFP_KERNEL);
965 vc4_state->upm_handle[i] = ret;
967 vc4_state->dlist[vc4_state->ptr0_offset[i]] |=
968 VC4_SET_FIELD(vc4_state->upm[i].start / HVS_UBM_WORD_SIZE,
969 SCALER6_PTR0_UPM_BASE) |
970 VC4_SET_FIELD(vc4_state->upm_handle[i] - 1,
971 SCALER6_PTR0_UPM_HANDLE) |
972 VC4_SET_FIELD(vc4_state->upm_buffer_lines,
973 SCALER6_PTR0_UPM_BUFF_SIZE);
980 * The colorspace conversion matrices are held in 3 entries in the dlist.
981 * Create an array of them, with entries for each full and limited mode, and
982 * each supported colorspace.
984 static const u32 colorspace_coeffs[2][DRM_COLOR_ENCODING_MAX][3] = {
989 SCALER_CSC0_ITR_R_601_5,
990 SCALER_CSC1_ITR_R_601_5,
991 SCALER_CSC2_ITR_R_601_5,
994 SCALER_CSC0_ITR_R_709_3,
995 SCALER_CSC1_ITR_R_709_3,
996 SCALER_CSC2_ITR_R_709_3,
999 SCALER_CSC0_ITR_R_2020,
1000 SCALER_CSC1_ITR_R_2020,
1001 SCALER_CSC2_ITR_R_2020,
1007 SCALER_CSC0_JPEG_JFIF,
1008 SCALER_CSC1_JPEG_JFIF,
1009 SCALER_CSC2_JPEG_JFIF,
1012 SCALER_CSC0_ITR_R_709_3_FR,
1013 SCALER_CSC1_ITR_R_709_3_FR,
1014 SCALER_CSC2_ITR_R_709_3_FR,
1017 SCALER_CSC0_ITR_R_2020_FR,
1018 SCALER_CSC1_ITR_R_2020_FR,
1019 SCALER_CSC2_ITR_R_2020_FR,
1024 static u32 vc4_hvs4_get_alpha_blend_mode(struct drm_plane_state *state)
1026 struct drm_device *dev = state->state->dev;
1027 struct vc4_dev *vc4 = to_vc4_dev(dev);
1029 WARN_ON_ONCE(vc4->gen != VC4_GEN_4);
1031 if (!state->fb->format->has_alpha)
1032 return VC4_SET_FIELD(SCALER_POS2_ALPHA_MODE_FIXED,
1033 SCALER_POS2_ALPHA_MODE);
1035 switch (state->pixel_blend_mode) {
1036 case DRM_MODE_BLEND_PIXEL_NONE:
1037 return VC4_SET_FIELD(SCALER_POS2_ALPHA_MODE_FIXED,
1038 SCALER_POS2_ALPHA_MODE);
1040 case DRM_MODE_BLEND_PREMULTI:
1041 return VC4_SET_FIELD(SCALER_POS2_ALPHA_MODE_PIPELINE,
1042 SCALER_POS2_ALPHA_MODE) |
1043 SCALER_POS2_ALPHA_PREMULT;
1044 case DRM_MODE_BLEND_COVERAGE:
1045 return VC4_SET_FIELD(SCALER_POS2_ALPHA_MODE_PIPELINE,
1046 SCALER_POS2_ALPHA_MODE);
1050 static u32 vc4_hvs5_get_alpha_blend_mode(struct drm_plane_state *state)
1052 struct drm_device *dev = state->state->dev;
1053 struct vc4_dev *vc4 = to_vc4_dev(dev);
1055 WARN_ON_ONCE(vc4->gen != VC4_GEN_5 && vc4->gen != VC4_GEN_6);
1057 if (!state->fb->format->has_alpha)
1058 return VC4_SET_FIELD(SCALER5_CTL2_ALPHA_MODE_FIXED,
1059 SCALER5_CTL2_ALPHA_MODE);
1061 switch (state->pixel_blend_mode) {
1062 case DRM_MODE_BLEND_PIXEL_NONE:
1063 return VC4_SET_FIELD(SCALER5_CTL2_ALPHA_MODE_FIXED,
1064 SCALER5_CTL2_ALPHA_MODE);
1066 case DRM_MODE_BLEND_PREMULTI:
1067 return VC4_SET_FIELD(SCALER5_CTL2_ALPHA_MODE_PIPELINE,
1068 SCALER5_CTL2_ALPHA_MODE) |
1069 SCALER5_CTL2_ALPHA_PREMULT;
1070 case DRM_MODE_BLEND_COVERAGE:
1071 return VC4_SET_FIELD(SCALER5_CTL2_ALPHA_MODE_PIPELINE,
1072 SCALER5_CTL2_ALPHA_MODE);
1076 /* Writes out a full display list for an active plane to the plane's
1077 * private dlist state.
1079 static int vc4_plane_mode_set(struct drm_plane *plane,
1080 struct drm_plane_state *state)
1082 struct vc4_dev *vc4 = to_vc4_dev(plane->dev);
1083 struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
1084 struct drm_framebuffer *fb = state->fb;
1085 u32 ctl0_offset = vc4_state->dlist_count;
1086 const struct hvs_format *format = vc4_get_hvs_format(fb->format->format);
1087 u64 base_format_mod = fourcc_mod_broadcom_mod(fb->modifier);
1088 int num_planes = fb->format->num_planes;
1089 u32 h_subsample = fb->format->hsub;
1090 u32 v_subsample = fb->format->vsub;
1091 bool mix_plane_alpha;
1093 u32 scl0, scl1, pitch0;
1094 u32 tiling, src_x, src_y;
1096 u32 hvs_format = format->hvs;
1097 unsigned int rotation;
1098 u32 offsets[3] = { 0 };
1101 if (vc4_state->dlist_initialized)
1104 ret = vc4_plane_setup_clipping_and_scaling(state);
1108 width = vc4_state->src_w[0] >> 16;
1109 height = vc4_state->src_h[0] >> 16;
1111 /* SCL1 is used for Cb/Cr scaling of planar formats. For RGB
1112 * and 4:4:4, scl1 should be set to scl0 so both channels of
1113 * the scaler do the same thing. For YUV, the Y plane needs
1114 * to be put in channel 1 and Cb/Cr in channel 0, so we swap
1115 * the scl fields here.
1117 if (num_planes == 1) {
1118 scl0 = vc4_get_scl_field(state, 0);
1121 scl0 = vc4_get_scl_field(state, 1);
1122 scl1 = vc4_get_scl_field(state, 0);
1125 rotation = drm_rotation_simplify(state->rotation,
1127 DRM_MODE_REFLECT_X |
1128 DRM_MODE_REFLECT_Y);
1130 /* We must point to the last line when Y reflection is enabled. */
1131 src_y = vc4_state->src_y >> 16;
1132 if (rotation & DRM_MODE_REFLECT_Y)
1133 src_y += height - 1;
1135 src_x = vc4_state->src_x >> 16;
1137 switch (base_format_mod) {
1138 case DRM_FORMAT_MOD_LINEAR:
1139 tiling = SCALER_CTL0_TILING_LINEAR;
1140 pitch0 = VC4_SET_FIELD(fb->pitches[0], SCALER_SRC_PITCH);
1142 /* Adjust the base pointer to the first pixel to be scanned
1145 for (i = 0; i < num_planes; i++) {
1146 offsets[i] += src_y / (i ? v_subsample : 1) * fb->pitches[i];
1147 offsets[i] += src_x / (i ? h_subsample : 1) * fb->format->cpp[i];
1152 case DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED: {
1153 u32 tile_size_shift = 12; /* T tiles are 4kb */
1154 /* Whole-tile offsets, mostly for setting the pitch. */
1155 u32 tile_w_shift = fb->format->cpp[0] == 2 ? 6 : 5;
1156 u32 tile_h_shift = 5; /* 16 and 32bpp are 32 pixels high */
1157 u32 tile_w_mask = (1 << tile_w_shift) - 1;
1158 /* The height mask on 32-bit-per-pixel tiles is 63, i.e. twice
1159 * the height (in pixels) of a 4k tile.
1161 u32 tile_h_mask = (2 << tile_h_shift) - 1;
1162 /* For T-tiled, the FB pitch is "how many bytes from one row to
1163 * the next, such that
1165 * pitch * tile_h == tile_size * tiles_per_row
1167 u32 tiles_w = fb->pitches[0] >> (tile_size_shift - tile_h_shift);
1168 u32 tiles_l = src_x >> tile_w_shift;
1169 u32 tiles_r = tiles_w - tiles_l;
1170 u32 tiles_t = src_y >> tile_h_shift;
1171 /* Intra-tile offsets, which modify the base address (the
1172 * SCALER_PITCH0_TILE_Y_OFFSET tells HVS how to walk from that
1175 u32 tile_y = (src_y >> 4) & 1;
1176 u32 subtile_y = (src_y >> 2) & 3;
1177 u32 utile_y = src_y & 3;
1178 u32 x_off = src_x & tile_w_mask;
1179 u32 y_off = src_y & tile_h_mask;
1181 /* When Y reflection is requested we must set the
1182 * SCALER_PITCH0_TILE_LINE_DIR flag to tell HVS that all lines
1183 * after the initial one should be fetched in descending order,
1184 * which makes sense since we start from the last line and go
1186 * Don't know why we need y_off = max_y_off - y_off, but it's
1187 * definitely required (I guess it's also related to the "going
1188 * backward" situation).
1190 if (rotation & DRM_MODE_REFLECT_Y) {
1191 y_off = tile_h_mask - y_off;
1192 pitch0 = SCALER_PITCH0_TILE_LINE_DIR;
1197 tiling = SCALER_CTL0_TILING_256B_OR_T;
1198 pitch0 |= (VC4_SET_FIELD(x_off, SCALER_PITCH0_SINK_PIX) |
1199 VC4_SET_FIELD(y_off, SCALER_PITCH0_TILE_Y_OFFSET) |
1200 VC4_SET_FIELD(tiles_l, SCALER_PITCH0_TILE_WIDTH_L) |
1201 VC4_SET_FIELD(tiles_r, SCALER_PITCH0_TILE_WIDTH_R));
1202 offsets[0] += tiles_t * (tiles_w << tile_size_shift);
1203 offsets[0] += subtile_y << 8;
1204 offsets[0] += utile_y << 4;
1206 /* Rows of tiles alternate left-to-right and right-to-left. */
1208 pitch0 |= SCALER_PITCH0_TILE_INITIAL_LINE_DIR;
1209 offsets[0] += (tiles_w - tiles_l) << tile_size_shift;
1210 offsets[0] -= (1 + !tile_y) << 10;
1212 offsets[0] += tiles_l << tile_size_shift;
1213 offsets[0] += tile_y << 10;
1219 case DRM_FORMAT_MOD_BROADCOM_SAND64:
1220 case DRM_FORMAT_MOD_BROADCOM_SAND128:
1221 case DRM_FORMAT_MOD_BROADCOM_SAND256: {
1222 uint32_t param = fourcc_mod_broadcom_param(fb->modifier);
1224 if (param > SCALER_TILE_HEIGHT_MASK) {
1225 DRM_DEBUG_KMS("SAND height too large (%d)\n",
1230 if (fb->format->format == DRM_FORMAT_P030) {
1231 hvs_format = HVS_PIXEL_FORMAT_YCBCR_10BIT;
1232 tiling = SCALER_CTL0_TILING_128B;
1234 hvs_format = HVS_PIXEL_FORMAT_H264;
1236 switch (base_format_mod) {
1237 case DRM_FORMAT_MOD_BROADCOM_SAND64:
1238 tiling = SCALER_CTL0_TILING_64B;
1240 case DRM_FORMAT_MOD_BROADCOM_SAND128:
1241 tiling = SCALER_CTL0_TILING_128B;
1243 case DRM_FORMAT_MOD_BROADCOM_SAND256:
1244 tiling = SCALER_CTL0_TILING_256B_OR_T;
1251 /* Adjust the base pointer to the first pixel to be scanned
1254 * For P030, y_ptr [31:4] is the 128bit word for the start pixel
1255 * y_ptr [3:0] is the pixel (0-11) contained within that 128bit
1256 * word that should be taken as the first pixel.
1257 * Ditto uv_ptr [31:4] vs [3:0], however [3:0] contains the
1258 * element within the 128bit word, eg for pixel 3 the value
1261 for (i = 0; i < num_planes; i++) {
1262 u32 tile_w, tile, x_off, pix_per_tile;
1264 if (fb->format->format == DRM_FORMAT_P030) {
1266 * Spec says: bits [31:4] of the given address
1267 * should point to the 128-bit word containing
1268 * the desired starting pixel, and bits[3:0]
1269 * should be between 0 and 11, indicating which
1270 * of the 12-pixels in that 128-bit word is the
1271 * first pixel to be used
1273 u32 remaining_pixels = src_x % 96;
1274 u32 aligned = remaining_pixels / 12;
1275 u32 last_bits = remaining_pixels % 12;
1277 x_off = aligned * 16 + last_bits;
1281 switch (base_format_mod) {
1282 case DRM_FORMAT_MOD_BROADCOM_SAND64:
1285 case DRM_FORMAT_MOD_BROADCOM_SAND128:
1288 case DRM_FORMAT_MOD_BROADCOM_SAND256:
1294 pix_per_tile = tile_w / fb->format->cpp[0];
1295 x_off = (src_x % pix_per_tile) /
1296 (i ? h_subsample : 1) *
1300 tile = src_x / pix_per_tile;
1302 offsets[i] += param * tile_w * tile;
1303 offsets[i] += src_y / (i ? v_subsample : 1) * tile_w;
1304 offsets[i] += x_off & ~(i ? 1 : 0);
1307 pitch0 = VC4_SET_FIELD(param, SCALER_TILE_HEIGHT);
1312 DRM_DEBUG_KMS("Unsupported FB tiling flag 0x%16llx",
1313 (long long)fb->modifier);
1317 /* fetch an extra pixel if we don't actually line up with the left edge. */
1318 if ((vc4_state->src_x & 0xffff) && vc4_state->src_x < (state->fb->width << 16))
1321 /* same for the right side */
1322 if (((vc4_state->src_x + vc4_state->src_w[0]) & 0xffff) &&
1323 vc4_state->src_x + vc4_state->src_w[0] < (state->fb->width << 16))
1326 /* now for the top */
1327 if ((vc4_state->src_y & 0xffff) && vc4_state->src_y < (state->fb->height << 16))
1330 /* and the bottom */
1331 if (((vc4_state->src_y + vc4_state->src_h[0]) & 0xffff) &&
1332 vc4_state->src_y + vc4_state->src_h[0] < (state->fb->height << 16))
1335 /* for YUV444 hardware wants double the width, otherwise it doesn't fetch full width of chroma */
1336 if (format->drm == DRM_FORMAT_YUV444 || format->drm == DRM_FORMAT_YVU444)
1339 /* Don't waste cycles mixing with plane alpha if the set alpha
1340 * is opaque or there is no per-pixel alpha information.
1341 * In any case we use the alpha property value as the fixed alpha.
1343 mix_plane_alpha = state->alpha != DRM_BLEND_ALPHA_OPAQUE &&
1344 fb->format->has_alpha;
1346 if (vc4->gen == VC4_GEN_4) {
1348 vc4_dlist_write(vc4_state,
1350 (rotation & DRM_MODE_REFLECT_X ? SCALER_CTL0_HFLIP : 0) |
1351 (rotation & DRM_MODE_REFLECT_Y ? SCALER_CTL0_VFLIP : 0) |
1352 VC4_SET_FIELD(SCALER_CTL0_RGBA_EXPAND_ROUND, SCALER_CTL0_RGBA_EXPAND) |
1353 (format->pixel_order << SCALER_CTL0_ORDER_SHIFT) |
1354 (hvs_format << SCALER_CTL0_PIXEL_FORMAT_SHIFT) |
1355 VC4_SET_FIELD(tiling, SCALER_CTL0_TILING) |
1356 (vc4_state->is_unity ? SCALER_CTL0_UNITY : 0) |
1357 VC4_SET_FIELD(scl0, SCALER_CTL0_SCL0) |
1358 VC4_SET_FIELD(scl1, SCALER_CTL0_SCL1));
1360 /* Position Word 0: Image Positions and Alpha Value */
1361 vc4_state->pos0_offset = vc4_state->dlist_count;
1362 vc4_dlist_write(vc4_state,
1363 VC4_SET_FIELD(state->alpha >> 8, SCALER_POS0_FIXED_ALPHA) |
1364 VC4_SET_FIELD(vc4_state->crtc_x, SCALER_POS0_START_X) |
1365 VC4_SET_FIELD(vc4_state->crtc_y, SCALER_POS0_START_Y));
1367 /* Position Word 1: Scaled Image Dimensions. */
1368 if (!vc4_state->is_unity) {
1369 vc4_dlist_write(vc4_state,
1370 VC4_SET_FIELD(vc4_state->crtc_w,
1371 SCALER_POS1_SCL_WIDTH) |
1372 VC4_SET_FIELD(vc4_state->crtc_h,
1373 SCALER_POS1_SCL_HEIGHT));
1376 /* Position Word 2: Source Image Size, Alpha */
1377 vc4_state->pos2_offset = vc4_state->dlist_count;
1378 vc4_dlist_write(vc4_state,
1379 (mix_plane_alpha ? SCALER_POS2_ALPHA_MIX : 0) |
1380 vc4_hvs4_get_alpha_blend_mode(state) |
1381 VC4_SET_FIELD(width, SCALER_POS2_WIDTH) |
1382 VC4_SET_FIELD(height, SCALER_POS2_HEIGHT));
1384 /* Position Word 3: Context. Written by the HVS. */
1385 vc4_dlist_write(vc4_state, 0xc0c0c0c0);
1389 vc4_dlist_write(vc4_state,
1391 (format->pixel_order_hvs5 << SCALER_CTL0_ORDER_SHIFT) |
1392 (hvs_format << SCALER_CTL0_PIXEL_FORMAT_SHIFT) |
1393 VC4_SET_FIELD(tiling, SCALER_CTL0_TILING) |
1394 (vc4_state->is_unity ?
1395 SCALER5_CTL0_UNITY : 0) |
1396 VC4_SET_FIELD(scl0, SCALER_CTL0_SCL0) |
1397 VC4_SET_FIELD(scl1, SCALER_CTL0_SCL1) |
1398 SCALER5_CTL0_ALPHA_EXPAND |
1399 SCALER5_CTL0_RGB_EXPAND);
1401 /* Position Word 0: Image Positions and Alpha Value */
1402 vc4_state->pos0_offset = vc4_state->dlist_count;
1403 vc4_dlist_write(vc4_state,
1404 (rotation & DRM_MODE_REFLECT_Y ?
1405 SCALER5_POS0_VFLIP : 0) |
1406 VC4_SET_FIELD(vc4_state->crtc_x,
1407 SCALER_POS0_START_X) |
1408 (rotation & DRM_MODE_REFLECT_X ?
1409 SCALER5_POS0_HFLIP : 0) |
1410 VC4_SET_FIELD(vc4_state->crtc_y,
1411 SCALER5_POS0_START_Y)
1414 /* Control Word 2 */
1415 vc4_dlist_write(vc4_state,
1416 VC4_SET_FIELD(state->alpha >> 4,
1417 SCALER5_CTL2_ALPHA) |
1418 vc4_hvs5_get_alpha_blend_mode(state) |
1420 SCALER5_CTL2_ALPHA_MIX : 0)
1423 /* Position Word 1: Scaled Image Dimensions. */
1424 if (!vc4_state->is_unity) {
1425 vc4_dlist_write(vc4_state,
1426 VC4_SET_FIELD(vc4_state->crtc_w,
1427 SCALER5_POS1_SCL_WIDTH) |
1428 VC4_SET_FIELD(vc4_state->crtc_h,
1429 SCALER5_POS1_SCL_HEIGHT));
1432 /* Position Word 2: Source Image Size */
1433 vc4_state->pos2_offset = vc4_state->dlist_count;
1434 vc4_dlist_write(vc4_state,
1435 VC4_SET_FIELD(width, SCALER5_POS2_WIDTH) |
1436 VC4_SET_FIELD(height, SCALER5_POS2_HEIGHT));
1438 /* Position Word 3: Context. Written by the HVS. */
1439 vc4_dlist_write(vc4_state, 0xc0c0c0c0);
1443 /* Pointer Word 0/1/2: RGB / Y / Cb / Cr Pointers
1445 * The pointers may be any byte address.
1447 vc4_state->ptr0_offset[0] = vc4_state->dlist_count;
1449 for (i = 0; i < num_planes; i++) {
1450 dma_addr_t paddr = drm_fb_dma_get_gem_addr(fb, state, i);
1452 vc4_dlist_write(vc4_state, paddr + offsets[i]);
1455 /* Pointer Context Word 0/1/2: Written by the HVS */
1456 for (i = 0; i < num_planes; i++)
1457 vc4_dlist_write(vc4_state, 0xc0c0c0c0);
1460 vc4_dlist_write(vc4_state, pitch0);
1462 /* Pitch word 1/2 */
1463 for (i = 1; i < num_planes; i++) {
1464 if (hvs_format != HVS_PIXEL_FORMAT_H264 &&
1465 hvs_format != HVS_PIXEL_FORMAT_YCBCR_10BIT) {
1466 vc4_dlist_write(vc4_state,
1467 VC4_SET_FIELD(fb->pitches[i],
1470 vc4_dlist_write(vc4_state, pitch0);
1474 /* Colorspace conversion words */
1475 if (vc4_state->is_yuv) {
1476 enum drm_color_encoding color_encoding = state->color_encoding;
1477 enum drm_color_range color_range = state->color_range;
1480 if (color_encoding >= DRM_COLOR_ENCODING_MAX)
1481 color_encoding = DRM_COLOR_YCBCR_BT601;
1482 if (color_range >= DRM_COLOR_RANGE_MAX)
1483 color_range = DRM_COLOR_YCBCR_LIMITED_RANGE;
1485 ccm = colorspace_coeffs[color_range][color_encoding];
1487 vc4_dlist_write(vc4_state, ccm[0]);
1488 vc4_dlist_write(vc4_state, ccm[1]);
1489 vc4_dlist_write(vc4_state, ccm[2]);
1492 vc4_state->lbm_offset = 0;
1494 if (vc4_state->x_scaling[0] != VC4_SCALING_NONE ||
1495 vc4_state->x_scaling[1] != VC4_SCALING_NONE ||
1496 vc4_state->y_scaling[0] != VC4_SCALING_NONE ||
1497 vc4_state->y_scaling[1] != VC4_SCALING_NONE) {
1498 /* Reserve a slot for the LBM Base Address. The real value will
1499 * be set when calling vc4_plane_allocate_lbm().
1501 if (vc4_state->y_scaling[0] != VC4_SCALING_NONE ||
1502 vc4_state->y_scaling[1] != VC4_SCALING_NONE) {
1503 vc4_state->lbm_offset = vc4_state->dlist_count;
1504 vc4_dlist_counter_increment(vc4_state);
1507 if (num_planes > 1) {
1508 /* Emit Cb/Cr as channel 0 and Y as channel
1509 * 1. This matches how we set up scl0/scl1
1512 vc4_write_scaling_parameters(state, 1);
1514 vc4_write_scaling_parameters(state, 0);
1516 /* If any PPF setup was done, then all the kernel
1517 * pointers get uploaded.
1519 if (vc4_state->x_scaling[0] == VC4_SCALING_PPF ||
1520 vc4_state->y_scaling[0] == VC4_SCALING_PPF ||
1521 vc4_state->x_scaling[1] == VC4_SCALING_PPF ||
1522 vc4_state->y_scaling[1] == VC4_SCALING_PPF) {
1523 u32 kernel = VC4_SET_FIELD(vc4->hvs->mitchell_netravali_filter.start,
1524 SCALER_PPF_KERNEL_OFFSET);
1527 vc4_dlist_write(vc4_state, kernel);
1529 vc4_dlist_write(vc4_state, kernel);
1531 vc4_dlist_write(vc4_state, kernel);
1533 vc4_dlist_write(vc4_state, kernel);
1537 vc4_state->dlist[ctl0_offset] |=
1538 VC4_SET_FIELD(vc4_state->dlist_count, SCALER_CTL0_SIZE);
1540 /* crtc_* are already clipped coordinates. */
1541 covers_screen = vc4_state->crtc_x == 0 && vc4_state->crtc_y == 0 &&
1542 vc4_state->crtc_w == state->crtc->mode.hdisplay &&
1543 vc4_state->crtc_h == state->crtc->mode.vdisplay;
1544 /* Background fill might be necessary when the plane has per-pixel
1545 * alpha content or a non-opaque plane alpha and could blend from the
1546 * background or does not cover the entire screen.
1548 vc4_state->needs_bg_fill = fb->format->has_alpha || !covers_screen ||
1549 state->alpha != DRM_BLEND_ALPHA_OPAQUE;
1551 /* Flag the dlist as initialized to avoid checking it twice in case
1552 * the async update check already called vc4_plane_mode_set() and
1553 * decided to fallback to sync update because async update was not
1556 vc4_state->dlist_initialized = 1;
1558 vc4_plane_calc_load(state);
1563 static u32 vc6_plane_get_csc_mode(struct vc4_plane_state *vc4_state)
1565 struct drm_plane_state *state = &vc4_state->base;
1568 if (vc4_state->is_yuv) {
1569 enum drm_color_encoding color_encoding = state->color_encoding;
1570 enum drm_color_range color_range = state->color_range;
1572 ret |= SCALER6_CTL2_CSC_ENABLE;
1574 /* CSC pre-loaded with:
1575 * 0 = BT601 limited range
1576 * 1 = BT709 limited range
1577 * 2 = BT2020 limited range
1578 * 3 = BT601 full range
1579 * 4 = BT709 full range
1580 * 5 = BT2020 full range
1582 if (color_encoding > DRM_COLOR_YCBCR_BT2020)
1583 color_encoding = DRM_COLOR_YCBCR_BT601;
1584 if (color_range > DRM_COLOR_YCBCR_FULL_RANGE)
1585 color_range = DRM_COLOR_YCBCR_LIMITED_RANGE;
1587 ret |= VC4_SET_FIELD(color_encoding + (color_range * 3),
1588 SCALER6_CTL2_BRCM_CFC_CONTROL);
1594 static int vc6_plane_mode_set(struct drm_plane *plane,
1595 struct drm_plane_state *state)
1597 struct drm_device *drm = plane->dev;
1598 struct vc4_dev *vc4 = to_vc4_dev(drm);
1599 struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
1600 struct drm_framebuffer *fb = state->fb;
1601 const struct hvs_format *format = vc4_get_hvs_format(fb->format->format);
1602 u64 base_format_mod = fourcc_mod_broadcom_mod(fb->modifier);
1603 int num_planes = fb->format->num_planes;
1604 u32 h_subsample = fb->format->hsub;
1605 u32 v_subsample = fb->format->vsub;
1606 bool mix_plane_alpha;
1608 u32 scl0, scl1, pitch0;
1609 u32 tiling, src_x, src_y;
1611 u32 hvs_format = format->hvs;
1612 u32 offsets[3] = { 0 };
1613 unsigned int rotation;
1616 if (vc4_state->dlist_initialized)
1619 ret = vc4_plane_setup_clipping_and_scaling(state);
1623 width = vc4_state->src_w[0] >> 16;
1624 height = vc4_state->src_h[0] >> 16;
1626 /* SCL1 is used for Cb/Cr scaling of planar formats. For RGB
1627 * and 4:4:4, scl1 should be set to scl0 so both channels of
1628 * the scaler do the same thing. For YUV, the Y plane needs
1629 * to be put in channel 1 and Cb/Cr in channel 0, so we swap
1630 * the scl fields here.
1632 if (num_planes == 1) {
1633 scl0 = vc4_get_scl_field(state, 0);
1636 scl0 = vc4_get_scl_field(state, 1);
1637 scl1 = vc4_get_scl_field(state, 0);
1640 rotation = drm_rotation_simplify(state->rotation,
1642 DRM_MODE_REFLECT_X |
1643 DRM_MODE_REFLECT_Y);
1645 /* We must point to the last line when Y reflection is enabled. */
1646 src_y = vc4_state->src_y >> 16;
1647 if (rotation & DRM_MODE_REFLECT_Y)
1648 src_y += height - 1;
1650 src_x = vc4_state->src_x >> 16;
1652 switch (base_format_mod) {
1653 case DRM_FORMAT_MOD_LINEAR:
1654 tiling = SCALER6_CTL0_ADDR_MODE_LINEAR;
1656 /* Adjust the base pointer to the first pixel to be scanned
1659 for (i = 0; i < num_planes; i++) {
1660 offsets[i] += src_y / (i ? v_subsample : 1) * fb->pitches[i];
1661 offsets[i] += src_x / (i ? h_subsample : 1) * fb->format->cpp[i];
1666 case DRM_FORMAT_MOD_BROADCOM_SAND128:
1667 case DRM_FORMAT_MOD_BROADCOM_SAND256: {
1668 uint32_t param = fourcc_mod_broadcom_param(fb->modifier);
1669 u32 components_per_word;
1670 u32 starting_offset;
1673 if (param > SCALER_TILE_HEIGHT_MASK) {
1674 DRM_DEBUG_KMS("SAND height too large (%d)\n",
1679 if (fb->format->format == DRM_FORMAT_P030) {
1680 hvs_format = HVS_PIXEL_FORMAT_YCBCR_10BIT;
1681 tiling = SCALER6_CTL0_ADDR_MODE_128B;
1683 hvs_format = HVS_PIXEL_FORMAT_YCBCR_YUV420_2PLANE;
1685 switch (base_format_mod) {
1686 case DRM_FORMAT_MOD_BROADCOM_SAND128:
1687 tiling = SCALER6_CTL0_ADDR_MODE_128B;
1689 case DRM_FORMAT_MOD_BROADCOM_SAND256:
1690 tiling = SCALER6_CTL0_ADDR_MODE_256B;
1697 /* Adjust the base pointer to the first pixel to be scanned
1700 * For P030, y_ptr [31:4] is the 128bit word for the start pixel
1701 * y_ptr [3:0] is the pixel (0-11) contained within that 128bit
1702 * word that should be taken as the first pixel.
1703 * Ditto uv_ptr [31:4] vs [3:0], however [3:0] contains the
1704 * element within the 128bit word, eg for pixel 3 the value
1707 for (i = 0; i < num_planes; i++) {
1708 u32 tile_w, tile, x_off, pix_per_tile;
1710 if (fb->format->format == DRM_FORMAT_P030) {
1712 * Spec says: bits [31:4] of the given address
1713 * should point to the 128-bit word containing
1714 * the desired starting pixel, and bits[3:0]
1715 * should be between 0 and 11, indicating which
1716 * of the 12-pixels in that 128-bit word is the
1717 * first pixel to be used
1719 u32 remaining_pixels = src_x % 96;
1720 u32 aligned = remaining_pixels / 12;
1721 u32 last_bits = remaining_pixels % 12;
1723 x_off = aligned * 16 + last_bits;
1727 switch (base_format_mod) {
1728 case DRM_FORMAT_MOD_BROADCOM_SAND128:
1731 case DRM_FORMAT_MOD_BROADCOM_SAND256:
1737 pix_per_tile = tile_w / fb->format->cpp[0];
1738 x_off = (src_x % pix_per_tile) /
1739 (i ? h_subsample : 1) *
1743 tile = src_x / pix_per_tile;
1745 offsets[i] += param * tile_w * tile;
1746 offsets[i] += src_y / (i ? v_subsample : 1) * tile_w;
1747 offsets[i] += x_off & ~(i ? 1 : 0);
1750 components_per_word = fb->format->format == DRM_FORMAT_P030 ? 24 : 32;
1751 starting_offset = src_x % components_per_word;
1752 fetch_count = (width + starting_offset + components_per_word - 1) /
1753 components_per_word;
1755 pitch0 = VC4_SET_FIELD(param, SCALER6_PTR2_PITCH) |
1756 VC4_SET_FIELD(fetch_count - 1, SCALER6_PTR2_FETCH_COUNT);
1761 DRM_DEBUG_KMS("Unsupported FB tiling flag 0x%16llx",
1762 (long long)fb->modifier);
1766 /* fetch an extra pixel if we don't actually line up with the left edge. */
1767 if ((vc4_state->src_x & 0xffff) && vc4_state->src_x < (state->fb->width << 16))
1770 /* same for the right side */
1771 if (((vc4_state->src_x + vc4_state->src_w[0]) & 0xffff) &&
1772 vc4_state->src_x + vc4_state->src_w[0] < (state->fb->width << 16))
1775 /* now for the top */
1776 if ((vc4_state->src_y & 0xffff) && vc4_state->src_y < (state->fb->height << 16))
1779 /* and the bottom */
1780 if (((vc4_state->src_y + vc4_state->src_h[0]) & 0xffff) &&
1781 vc4_state->src_y + vc4_state->src_h[0] < (state->fb->height << 16))
1784 /* for YUV444 hardware wants double the width, otherwise it doesn't
1785 * fetch full width of chroma
1787 if (format->drm == DRM_FORMAT_YUV444 || format->drm == DRM_FORMAT_YVU444)
1790 /* Don't waste cycles mixing with plane alpha if the set alpha
1791 * is opaque or there is no per-pixel alpha information.
1792 * In any case we use the alpha property value as the fixed alpha.
1794 mix_plane_alpha = state->alpha != DRM_BLEND_ALPHA_OPAQUE &&
1795 fb->format->has_alpha;
1797 /* Control Word 0: Scaling Configuration & Element Validity*/
1798 vc4_dlist_write(vc4_state,
1799 SCALER6_CTL0_VALID |
1800 VC4_SET_FIELD(tiling, SCALER6_CTL0_ADDR_MODE) |
1801 VC4_SET_FIELD(0, SCALER6_CTL0_ALPHA_MASK) |
1802 (vc4_state->is_unity ? SCALER6_CTL0_UNITY : 0) |
1803 VC4_SET_FIELD(format->pixel_order_hvs5, SCALER6_CTL0_ORDERRGBA) |
1804 VC4_SET_FIELD(scl1, SCALER6_CTL0_SCL1_MODE) |
1805 VC4_SET_FIELD(scl0, SCALER6_CTL0_SCL0_MODE) |
1806 VC4_SET_FIELD(hvs_format, SCALER6_CTL0_PIXEL_FORMAT));
1808 /* Position Word 0: Image Position */
1809 vc4_state->pos0_offset = vc4_state->dlist_count;
1810 vc4_dlist_write(vc4_state,
1811 VC4_SET_FIELD(vc4_state->crtc_y, SCALER6_POS0_START_Y) |
1812 (rotation & DRM_MODE_REFLECT_X ? SCALER6_POS0_HFLIP : 0) |
1813 VC4_SET_FIELD(vc4_state->crtc_x, SCALER6_POS0_START_X));
1815 /* Control Word 2: Alpha Value & CSC */
1816 vc4_dlist_write(vc4_state,
1817 vc6_plane_get_csc_mode(vc4_state) |
1818 vc4_hvs5_get_alpha_blend_mode(state) |
1819 (mix_plane_alpha ? SCALER6_CTL2_ALPHA_MIX : 0) |
1820 VC4_SET_FIELD(state->alpha >> 4, SCALER5_CTL2_ALPHA));
1822 /* Position Word 1: Scaled Image Dimensions */
1823 if (!vc4_state->is_unity)
1824 vc4_dlist_write(vc4_state,
1825 VC4_SET_FIELD(vc4_state->crtc_h - 1,
1826 SCALER6_POS1_SCL_LINES) |
1827 VC4_SET_FIELD(vc4_state->crtc_w - 1,
1828 SCALER6_POS1_SCL_WIDTH));
1830 /* Position Word 2: Source Image Size */
1831 vc4_state->pos2_offset = vc4_state->dlist_count;
1832 vc4_dlist_write(vc4_state,
1833 VC4_SET_FIELD(height - 1,
1834 SCALER6_POS2_SRC_LINES) |
1835 VC4_SET_FIELD(width - 1,
1836 SCALER6_POS2_SRC_WIDTH));
1838 /* Position Word 3: Context */
1839 vc4_dlist_write(vc4_state, 0xc0c0c0c0);
1842 * TODO: This only covers Raster Scan Order planes
1844 for (i = 0; i < num_planes; i++) {
1845 dma_addr_t paddr = drm_fb_dma_get_gem_addr(fb, state, i);
1847 paddr += offsets[i];
1849 /* Pointer Word 0 */
1850 vc4_state->ptr0_offset[i] = vc4_state->dlist_count;
1851 vc4_dlist_write(vc4_state,
1852 (rotation & DRM_MODE_REFLECT_Y ? SCALER6_PTR0_VFLIP : 0) |
1854 * The UPM buffer will be allocated in
1855 * vc6_plane_allocate_upm().
1857 VC4_SET_FIELD(upper_32_bits(paddr) & 0xf,
1858 SCALER6_PTR0_UPPER_ADDR));
1860 /* Pointer Word 1 */
1861 vc4_dlist_write(vc4_state, lower_32_bits(paddr));
1863 /* Pointer Word 2 */
1864 if (base_format_mod != DRM_FORMAT_MOD_BROADCOM_SAND128 &&
1865 base_format_mod != DRM_FORMAT_MOD_BROADCOM_SAND256) {
1866 vc4_dlist_write(vc4_state,
1867 VC4_SET_FIELD(fb->pitches[i],
1868 SCALER6_PTR2_PITCH));
1870 vc4_dlist_write(vc4_state, pitch0);
1876 * TODO: We're not using the palette mode
1881 * TODO: It's only relevant if we set the trans_rgb bit in the
1882 * control word 0, and we don't at the moment.
1885 vc4_state->lbm_offset = 0;
1887 if (!vc4_state->is_unity || fb->format->is_yuv) {
1889 * Reserve a slot for the LBM Base Address. The real value will
1890 * be set when calling vc4_plane_allocate_lbm().
1892 if (vc4_state->y_scaling[0] != VC4_SCALING_NONE ||
1893 vc4_state->y_scaling[1] != VC4_SCALING_NONE) {
1894 vc4_state->lbm_offset = vc4_state->dlist_count;
1895 vc4_dlist_counter_increment(vc4_state);
1898 if (vc4_state->x_scaling[0] != VC4_SCALING_NONE ||
1899 vc4_state->x_scaling[1] != VC4_SCALING_NONE ||
1900 vc4_state->y_scaling[0] != VC4_SCALING_NONE ||
1901 vc4_state->y_scaling[1] != VC4_SCALING_NONE) {
1904 * Emit Cb/Cr as channel 0 and Y as channel
1905 * 1. This matches how we set up scl0/scl1
1908 vc4_write_scaling_parameters(state, 1);
1910 vc4_write_scaling_parameters(state, 0);
1914 * If any PPF setup was done, then all the kernel
1915 * pointers get uploaded.
1917 if (vc4_state->x_scaling[0] == VC4_SCALING_PPF ||
1918 vc4_state->y_scaling[0] == VC4_SCALING_PPF ||
1919 vc4_state->x_scaling[1] == VC4_SCALING_PPF ||
1920 vc4_state->y_scaling[1] == VC4_SCALING_PPF) {
1922 VC4_SET_FIELD(vc4->hvs->mitchell_netravali_filter.start,
1923 SCALER_PPF_KERNEL_OFFSET);
1926 vc4_dlist_write(vc4_state, kernel);
1928 vc4_dlist_write(vc4_state, kernel);
1930 vc4_dlist_write(vc4_state, kernel);
1932 vc4_dlist_write(vc4_state, kernel);
1936 vc4_dlist_write(vc4_state, SCALER6_CTL0_END);
1938 vc4_state->dlist[0] |=
1939 VC4_SET_FIELD(vc4_state->dlist_count, SCALER6_CTL0_NEXT);
1941 /* crtc_* are already clipped coordinates. */
1942 covers_screen = vc4_state->crtc_x == 0 && vc4_state->crtc_y == 0 &&
1943 vc4_state->crtc_w == state->crtc->mode.hdisplay &&
1944 vc4_state->crtc_h == state->crtc->mode.vdisplay;
1947 * Background fill might be necessary when the plane has per-pixel
1948 * alpha content or a non-opaque plane alpha and could blend from the
1949 * background or does not cover the entire screen.
1951 vc4_state->needs_bg_fill = fb->format->has_alpha || !covers_screen ||
1952 state->alpha != DRM_BLEND_ALPHA_OPAQUE;
1955 * Flag the dlist as initialized to avoid checking it twice in case
1956 * the async update check already called vc4_plane_mode_set() and
1957 * decided to fallback to sync update because async update was not
1960 vc4_state->dlist_initialized = 1;
1962 vc4_plane_calc_load(state);
1964 drm_dbg_driver(drm, "[PLANE:%d:%s] Computed DLIST size: %u\n",
1965 plane->base.id, plane->name, vc4_state->dlist_count);
1970 /* If a modeset involves changing the setup of a plane, the atomic
1971 * infrastructure will call this to validate a proposed plane setup.
1972 * However, if a plane isn't getting updated, this (and the
1973 * corresponding vc4_plane_atomic_update) won't get called. Thus, we
1974 * compute the dlist here and have all active plane dlists get updated
1975 * in the CRTC's flush.
1977 int vc4_plane_atomic_check(struct drm_plane *plane,
1978 struct drm_atomic_state *state)
1980 struct vc4_dev *vc4 = to_vc4_dev(plane->dev);
1981 struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
1983 struct vc4_plane_state *vc4_state = to_vc4_plane_state(new_plane_state);
1986 vc4_state->dlist_count = 0;
1988 if (!plane_enabled(new_plane_state))
1991 if (vc4->gen >= VC4_GEN_6)
1992 ret = vc6_plane_mode_set(plane, new_plane_state);
1994 ret = vc4_plane_mode_set(plane, new_plane_state);
1998 ret = vc4_plane_allocate_lbm(new_plane_state);
2002 if (vc4->gen >= VC4_GEN_6) {
2003 ret = vc6_plane_allocate_upm(new_plane_state);
2011 static void vc4_plane_atomic_update(struct drm_plane *plane,
2012 struct drm_atomic_state *state)
2014 /* No contents here. Since we don't know where in the CRTC's
2015 * dlist we should be stored, our dlist is uploaded to the
2016 * hardware with vc4_plane_write_dlist() at CRTC atomic_flush
2021 u32 vc4_plane_write_dlist(struct drm_plane *plane, u32 __iomem *dlist)
2023 struct vc4_plane_state *vc4_state = to_vc4_plane_state(plane->state);
2027 if (!drm_dev_enter(plane->dev, &idx))
2030 vc4_state->hw_dlist = dlist;
2032 /* Can't memcpy_toio() because it needs to be 32-bit writes. */
2033 for (i = 0; i < vc4_state->dlist_count; i++)
2034 writel(vc4_state->dlist[i], &dlist[i]);
2039 return vc4_state->dlist_count;
2042 u32 vc4_plane_dlist_size(const struct drm_plane_state *state)
2044 const struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
2046 return vc4_state->dlist_count;
2049 /* Updates the plane to immediately (well, once the FIFO needs
2050 * refilling) scan out from at a new framebuffer.
2052 void vc4_plane_async_set_fb(struct drm_plane *plane, struct drm_framebuffer *fb)
2054 struct vc4_plane_state *vc4_state = to_vc4_plane_state(plane->state);
2055 struct drm_gem_dma_object *bo = drm_fb_dma_get_gem_obj(fb, 0);
2059 if (!drm_dev_enter(plane->dev, &idx))
2062 /* We're skipping the address adjustment for negative origin,
2063 * because this is only called on the primary plane.
2065 WARN_ON_ONCE(plane->state->crtc_x < 0 || plane->state->crtc_y < 0);
2066 addr = bo->dma_addr + fb->offsets[0];
2068 /* Write the new address into the hardware immediately. The
2069 * scanout will start from this address as soon as the FIFO
2070 * needs to refill with pixels.
2072 writel(addr, &vc4_state->hw_dlist[vc4_state->ptr0_offset[0]]);
2074 /* Also update the CPU-side dlist copy, so that any later
2075 * atomic updates that don't do a new modeset on our plane
2076 * also use our updated address.
2078 vc4_state->dlist[vc4_state->ptr0_offset[0]] = addr;
2083 static void vc4_plane_atomic_async_update(struct drm_plane *plane,
2084 struct drm_atomic_state *state)
2086 struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
2088 struct vc4_plane_state *vc4_state, *new_vc4_state;
2091 if (!drm_dev_enter(plane->dev, &idx))
2094 swap(plane->state->fb, new_plane_state->fb);
2095 plane->state->crtc_x = new_plane_state->crtc_x;
2096 plane->state->crtc_y = new_plane_state->crtc_y;
2097 plane->state->crtc_w = new_plane_state->crtc_w;
2098 plane->state->crtc_h = new_plane_state->crtc_h;
2099 plane->state->src_x = new_plane_state->src_x;
2100 plane->state->src_y = new_plane_state->src_y;
2101 plane->state->src_w = new_plane_state->src_w;
2102 plane->state->src_h = new_plane_state->src_h;
2103 plane->state->alpha = new_plane_state->alpha;
2104 plane->state->pixel_blend_mode = new_plane_state->pixel_blend_mode;
2105 plane->state->rotation = new_plane_state->rotation;
2106 plane->state->zpos = new_plane_state->zpos;
2107 plane->state->normalized_zpos = new_plane_state->normalized_zpos;
2108 plane->state->color_encoding = new_plane_state->color_encoding;
2109 plane->state->color_range = new_plane_state->color_range;
2110 plane->state->src = new_plane_state->src;
2111 plane->state->dst = new_plane_state->dst;
2112 plane->state->visible = new_plane_state->visible;
2114 new_vc4_state = to_vc4_plane_state(new_plane_state);
2115 vc4_state = to_vc4_plane_state(plane->state);
2117 vc4_state->crtc_x = new_vc4_state->crtc_x;
2118 vc4_state->crtc_y = new_vc4_state->crtc_y;
2119 vc4_state->crtc_h = new_vc4_state->crtc_h;
2120 vc4_state->crtc_w = new_vc4_state->crtc_w;
2121 vc4_state->src_x = new_vc4_state->src_x;
2122 vc4_state->src_y = new_vc4_state->src_y;
2123 memcpy(vc4_state->src_w, new_vc4_state->src_w,
2124 sizeof(vc4_state->src_w));
2125 memcpy(vc4_state->src_h, new_vc4_state->src_h,
2126 sizeof(vc4_state->src_h));
2127 memcpy(vc4_state->x_scaling, new_vc4_state->x_scaling,
2128 sizeof(vc4_state->x_scaling));
2129 memcpy(vc4_state->y_scaling, new_vc4_state->y_scaling,
2130 sizeof(vc4_state->y_scaling));
2131 vc4_state->is_unity = new_vc4_state->is_unity;
2132 vc4_state->is_yuv = new_vc4_state->is_yuv;
2133 vc4_state->needs_bg_fill = new_vc4_state->needs_bg_fill;
2135 /* Update the current vc4_state pos0, pos2 and ptr0 dlist entries. */
2136 vc4_state->dlist[vc4_state->pos0_offset] =
2137 new_vc4_state->dlist[vc4_state->pos0_offset];
2138 vc4_state->dlist[vc4_state->pos2_offset] =
2139 new_vc4_state->dlist[vc4_state->pos2_offset];
2140 vc4_state->dlist[vc4_state->ptr0_offset[0]] =
2141 new_vc4_state->dlist[vc4_state->ptr0_offset[0]];
2143 /* Note that we can't just call vc4_plane_write_dlist()
2144 * because that would smash the context data that the HVS is
2147 writel(vc4_state->dlist[vc4_state->pos0_offset],
2148 &vc4_state->hw_dlist[vc4_state->pos0_offset]);
2149 writel(vc4_state->dlist[vc4_state->pos2_offset],
2150 &vc4_state->hw_dlist[vc4_state->pos2_offset]);
2151 writel(vc4_state->dlist[vc4_state->ptr0_offset[0]],
2152 &vc4_state->hw_dlist[vc4_state->ptr0_offset[0]]);
2157 static int vc4_plane_atomic_async_check(struct drm_plane *plane,
2158 struct drm_atomic_state *state)
2160 struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
2162 struct vc4_plane_state *old_vc4_state, *new_vc4_state;
2166 ret = vc4_plane_mode_set(plane, new_plane_state);
2170 old_vc4_state = to_vc4_plane_state(plane->state);
2171 new_vc4_state = to_vc4_plane_state(new_plane_state);
2173 if (!new_vc4_state->hw_dlist)
2176 if (old_vc4_state->dlist_count != new_vc4_state->dlist_count ||
2177 old_vc4_state->pos0_offset != new_vc4_state->pos0_offset ||
2178 old_vc4_state->pos2_offset != new_vc4_state->pos2_offset ||
2179 old_vc4_state->ptr0_offset[0] != new_vc4_state->ptr0_offset[0] ||
2180 vc4_lbm_size(plane->state) != vc4_lbm_size(new_plane_state))
2183 /* Only pos0, pos2 and ptr0 DWORDS can be updated in an async update
2184 * if anything else has changed, fallback to a sync update.
2186 for (i = 0; i < new_vc4_state->dlist_count; i++) {
2187 if (i == new_vc4_state->pos0_offset ||
2188 i == new_vc4_state->pos2_offset ||
2189 i == new_vc4_state->ptr0_offset[0] ||
2190 (new_vc4_state->lbm_offset &&
2191 i == new_vc4_state->lbm_offset))
2194 if (new_vc4_state->dlist[i] != old_vc4_state->dlist[i])
2201 static int vc4_prepare_fb(struct drm_plane *plane,
2202 struct drm_plane_state *state)
2209 bo = to_vc4_bo(&drm_fb_dma_get_gem_obj(state->fb, 0)->base);
2211 drm_gem_plane_helper_prepare_fb(plane, state);
2213 if (plane->state->fb == state->fb)
2216 return vc4_bo_inc_usecnt(bo);
2219 static void vc4_cleanup_fb(struct drm_plane *plane,
2220 struct drm_plane_state *state)
2224 if (plane->state->fb == state->fb || !state->fb)
2227 bo = to_vc4_bo(&drm_fb_dma_get_gem_obj(state->fb, 0)->base);
2228 vc4_bo_dec_usecnt(bo);
2231 static const struct drm_plane_helper_funcs vc4_plane_helper_funcs = {
2232 .atomic_check = vc4_plane_atomic_check,
2233 .atomic_update = vc4_plane_atomic_update,
2234 .prepare_fb = vc4_prepare_fb,
2235 .cleanup_fb = vc4_cleanup_fb,
2236 .atomic_async_check = vc4_plane_atomic_async_check,
2237 .atomic_async_update = vc4_plane_atomic_async_update,
2240 static const struct drm_plane_helper_funcs vc5_plane_helper_funcs = {
2241 .atomic_check = vc4_plane_atomic_check,
2242 .atomic_update = vc4_plane_atomic_update,
2243 .atomic_async_check = vc4_plane_atomic_async_check,
2244 .atomic_async_update = vc4_plane_atomic_async_update,
2247 static bool vc4_format_mod_supported(struct drm_plane *plane,
2251 /* Support T_TILING for RGB formats only. */
2253 case DRM_FORMAT_XRGB8888:
2254 case DRM_FORMAT_ARGB8888:
2255 case DRM_FORMAT_ABGR8888:
2256 case DRM_FORMAT_XBGR8888:
2257 case DRM_FORMAT_RGB565:
2258 case DRM_FORMAT_BGR565:
2259 case DRM_FORMAT_ARGB1555:
2260 case DRM_FORMAT_XRGB1555:
2261 switch (fourcc_mod_broadcom_mod(modifier)) {
2262 case DRM_FORMAT_MOD_LINEAR:
2263 case DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED:
2268 case DRM_FORMAT_NV12:
2269 case DRM_FORMAT_NV21:
2270 switch (fourcc_mod_broadcom_mod(modifier)) {
2271 case DRM_FORMAT_MOD_LINEAR:
2272 case DRM_FORMAT_MOD_BROADCOM_SAND64:
2273 case DRM_FORMAT_MOD_BROADCOM_SAND128:
2274 case DRM_FORMAT_MOD_BROADCOM_SAND256:
2279 case DRM_FORMAT_P030:
2280 switch (fourcc_mod_broadcom_mod(modifier)) {
2281 case DRM_FORMAT_MOD_BROADCOM_SAND128:
2286 case DRM_FORMAT_RGBX1010102:
2287 case DRM_FORMAT_BGRX1010102:
2288 case DRM_FORMAT_RGBA1010102:
2289 case DRM_FORMAT_BGRA1010102:
2290 case DRM_FORMAT_XRGB4444:
2291 case DRM_FORMAT_ARGB4444:
2292 case DRM_FORMAT_XBGR4444:
2293 case DRM_FORMAT_ABGR4444:
2294 case DRM_FORMAT_RGBX4444:
2295 case DRM_FORMAT_RGBA4444:
2296 case DRM_FORMAT_BGRX4444:
2297 case DRM_FORMAT_BGRA4444:
2298 case DRM_FORMAT_RGB332:
2299 case DRM_FORMAT_BGR233:
2300 case DRM_FORMAT_YUV422:
2301 case DRM_FORMAT_YVU422:
2302 case DRM_FORMAT_YUV420:
2303 case DRM_FORMAT_YVU420:
2304 case DRM_FORMAT_NV16:
2305 case DRM_FORMAT_NV61:
2307 return (modifier == DRM_FORMAT_MOD_LINEAR);
2311 static const struct drm_plane_funcs vc4_plane_funcs = {
2312 .update_plane = drm_atomic_helper_update_plane,
2313 .disable_plane = drm_atomic_helper_disable_plane,
2314 .reset = vc4_plane_reset,
2315 .atomic_duplicate_state = vc4_plane_duplicate_state,
2316 .atomic_destroy_state = vc4_plane_destroy_state,
2317 .format_mod_supported = vc4_format_mod_supported,
2320 struct drm_plane *vc4_plane_init(struct drm_device *dev,
2321 enum drm_plane_type type,
2322 uint32_t possible_crtcs)
2324 struct vc4_dev *vc4 = to_vc4_dev(dev);
2325 struct drm_plane *plane;
2326 struct vc4_plane *vc4_plane;
2327 u32 formats[ARRAY_SIZE(hvs_formats)];
2328 int num_formats = 0;
2330 static const uint64_t modifiers[] = {
2331 DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED,
2332 DRM_FORMAT_MOD_BROADCOM_SAND128,
2333 DRM_FORMAT_MOD_BROADCOM_SAND64,
2334 DRM_FORMAT_MOD_BROADCOM_SAND256,
2335 DRM_FORMAT_MOD_LINEAR,
2336 DRM_FORMAT_MOD_INVALID
2339 for (i = 0; i < ARRAY_SIZE(hvs_formats); i++) {
2340 if (!hvs_formats[i].hvs5_only || vc4->gen >= VC4_GEN_5) {
2341 formats[num_formats] = hvs_formats[i].drm;
2346 vc4_plane = drmm_universal_plane_alloc(dev, struct vc4_plane, base,
2349 formats, num_formats,
2350 modifiers, type, NULL);
2351 if (IS_ERR(vc4_plane))
2352 return ERR_CAST(vc4_plane);
2353 plane = &vc4_plane->base;
2355 if (vc4->gen >= VC4_GEN_5)
2356 drm_plane_helper_add(plane, &vc5_plane_helper_funcs);
2358 drm_plane_helper_add(plane, &vc4_plane_helper_funcs);
2360 drm_plane_create_alpha_property(plane);
2361 drm_plane_create_blend_mode_property(plane,
2362 BIT(DRM_MODE_BLEND_PIXEL_NONE) |
2363 BIT(DRM_MODE_BLEND_PREMULTI) |
2364 BIT(DRM_MODE_BLEND_COVERAGE));
2365 drm_plane_create_rotation_property(plane, DRM_MODE_ROTATE_0,
2367 DRM_MODE_ROTATE_180 |
2368 DRM_MODE_REFLECT_X |
2369 DRM_MODE_REFLECT_Y);
2371 drm_plane_create_color_properties(plane,
2372 BIT(DRM_COLOR_YCBCR_BT601) |
2373 BIT(DRM_COLOR_YCBCR_BT709) |
2374 BIT(DRM_COLOR_YCBCR_BT2020),
2375 BIT(DRM_COLOR_YCBCR_LIMITED_RANGE) |
2376 BIT(DRM_COLOR_YCBCR_FULL_RANGE),
2377 DRM_COLOR_YCBCR_BT709,
2378 DRM_COLOR_YCBCR_LIMITED_RANGE);
2380 drm_plane_create_chroma_siting_properties(plane, 0, 0);
2382 if (type == DRM_PLANE_TYPE_PRIMARY)
2383 drm_plane_create_zpos_immutable_property(plane, 0);
2388 #define VC4_NUM_OVERLAY_PLANES 16
2390 int vc4_plane_create_additional_planes(struct drm_device *drm)
2392 struct drm_plane *cursor_plane;
2393 struct drm_crtc *crtc;
2396 /* Set up some arbitrary number of planes. We're not limited
2397 * by a set number of physical registers, just the space in
2398 * the HVS (16k) and how small an plane can be (28 bytes).
2399 * However, each plane we set up takes up some memory, and
2400 * increases the cost of looping over planes, which atomic
2401 * modesetting does quite a bit. As a result, we pick a
2402 * modest number of planes to expose, that should hopefully
2403 * still cover any sane usecase.
2405 for (i = 0; i < VC4_NUM_OVERLAY_PLANES; i++) {
2406 struct drm_plane *plane =
2407 vc4_plane_init(drm, DRM_PLANE_TYPE_OVERLAY,
2408 GENMASK(drm->mode_config.num_crtc - 1, 0));
2413 /* Create zpos property. Max of all the overlays + 1 primary +
2414 * 1 cursor plane on a crtc.
2416 drm_plane_create_zpos_property(plane, i + 1, 1,
2417 VC4_NUM_OVERLAY_PLANES + 1);
2420 drm_for_each_crtc(crtc, drm) {
2421 /* Set up the legacy cursor after overlay initialization,
2422 * since the zpos fallback is that planes are rendered by plane
2423 * ID order, and that then puts the cursor on top.
2425 cursor_plane = vc4_plane_init(drm, DRM_PLANE_TYPE_CURSOR,
2426 drm_crtc_mask(crtc));
2427 if (!IS_ERR(cursor_plane)) {
2428 crtc->cursor = cursor_plane;
2430 drm_plane_create_zpos_property(cursor_plane,
2431 VC4_NUM_OVERLAY_PLANES + 1,
2433 VC4_NUM_OVERLAY_PLANES + 1);