blob: a2dea26320fb9f50213ee2c13ed8033259fa8dba [file] [log] [blame]
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001/*
2 * Copyright (C) 2015 Broadcom
3 *
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.
7 */
8
9/**
10 * DOC: VC4 plane module
11 *
12 * Each DRM plane is a layer of pixels being scanned out by the HVS.
13 *
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.
19 */
20
Masahiro Yamadab7e8e252017-05-18 13:29:38 +090021#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>
Daniel Vetter72fdb40c2018-09-05 15:57:11 +020025#include <drm/drm_atomic_uapi.h>
Masahiro Yamadab7e8e252017-05-18 13:29:38 +090026
Boris Brezillonb9f19252017-10-19 14:57:48 +020027#include "uapi/drm/vc4_drm.h"
Eric Anholtc8b75bc2015-03-02 13:01:12 -080028#include "vc4_drv.h"
29#include "vc4_regs.h"
Eric Anholtc8b75bc2015-03-02 13:01:12 -080030
Eric Anholtc8b75bc2015-03-02 13:01:12 -080031static const struct hvs_format {
32 u32 drm; /* DRM_FORMAT_* */
33 u32 hvs; /* HVS_FORMAT_* */
34 u32 pixel_order;
Eric Anholtc8b75bc2015-03-02 13:01:12 -080035} hvs_formats[] = {
36 {
37 .drm = DRM_FORMAT_XRGB8888, .hvs = HVS_PIXEL_FORMAT_RGBA8888,
Maxime Ripard124e5da2017-12-22 15:31:27 +010038 .pixel_order = HVS_PIXEL_ORDER_ABGR,
Eric Anholtc8b75bc2015-03-02 13:01:12 -080039 },
40 {
41 .drm = DRM_FORMAT_ARGB8888, .hvs = HVS_PIXEL_FORMAT_RGBA8888,
Maxime Ripard124e5da2017-12-22 15:31:27 +010042 .pixel_order = HVS_PIXEL_ORDER_ABGR,
Eric Anholtc8b75bc2015-03-02 13:01:12 -080043 },
Eric Anholtfe4cd842015-10-20 13:59:15 +010044 {
Rob Herring93977762016-06-09 16:19:25 -050045 .drm = DRM_FORMAT_ABGR8888, .hvs = HVS_PIXEL_FORMAT_RGBA8888,
Maxime Ripard124e5da2017-12-22 15:31:27 +010046 .pixel_order = HVS_PIXEL_ORDER_ARGB,
Rob Herring93977762016-06-09 16:19:25 -050047 },
48 {
49 .drm = DRM_FORMAT_XBGR8888, .hvs = HVS_PIXEL_FORMAT_RGBA8888,
Maxime Ripard124e5da2017-12-22 15:31:27 +010050 .pixel_order = HVS_PIXEL_ORDER_ARGB,
Rob Herring93977762016-06-09 16:19:25 -050051 },
52 {
Eric Anholtfe4cd842015-10-20 13:59:15 +010053 .drm = DRM_FORMAT_RGB565, .hvs = HVS_PIXEL_FORMAT_RGB565,
Maxime Ripard124e5da2017-12-22 15:31:27 +010054 .pixel_order = HVS_PIXEL_ORDER_XRGB,
Eric Anholtfe4cd842015-10-20 13:59:15 +010055 },
56 {
57 .drm = DRM_FORMAT_BGR565, .hvs = HVS_PIXEL_FORMAT_RGB565,
Maxime Ripard124e5da2017-12-22 15:31:27 +010058 .pixel_order = HVS_PIXEL_ORDER_XBGR,
Eric Anholtfe4cd842015-10-20 13:59:15 +010059 },
60 {
61 .drm = DRM_FORMAT_ARGB1555, .hvs = HVS_PIXEL_FORMAT_RGBA5551,
Maxime Ripard124e5da2017-12-22 15:31:27 +010062 .pixel_order = HVS_PIXEL_ORDER_ABGR,
Eric Anholtfe4cd842015-10-20 13:59:15 +010063 },
64 {
65 .drm = DRM_FORMAT_XRGB1555, .hvs = HVS_PIXEL_FORMAT_RGBA5551,
Maxime Ripard124e5da2017-12-22 15:31:27 +010066 .pixel_order = HVS_PIXEL_ORDER_ABGR,
Eric Anholtfe4cd842015-10-20 13:59:15 +010067 },
Eric Anholtfc040232015-12-30 12:25:44 -080068 {
Dave Stevenson88f81562017-11-16 14:22:29 +000069 .drm = DRM_FORMAT_RGB888, .hvs = HVS_PIXEL_FORMAT_RGB888,
Maxime Ripard124e5da2017-12-22 15:31:27 +010070 .pixel_order = HVS_PIXEL_ORDER_XRGB,
Dave Stevenson88f81562017-11-16 14:22:29 +000071 },
72 {
73 .drm = DRM_FORMAT_BGR888, .hvs = HVS_PIXEL_FORMAT_RGB888,
Maxime Ripard124e5da2017-12-22 15:31:27 +010074 .pixel_order = HVS_PIXEL_ORDER_XBGR,
Dave Stevenson88f81562017-11-16 14:22:29 +000075 },
76 {
Eric Anholtfc040232015-12-30 12:25:44 -080077 .drm = DRM_FORMAT_YUV422,
78 .hvs = HVS_PIXEL_FORMAT_YCBCR_YUV422_3PLANE,
Dave Stevenson090cb0c2017-11-16 14:22:30 +000079 .pixel_order = HVS_PIXEL_ORDER_XYCBCR,
Eric Anholtfc040232015-12-30 12:25:44 -080080 },
81 {
82 .drm = DRM_FORMAT_YVU422,
83 .hvs = HVS_PIXEL_FORMAT_YCBCR_YUV422_3PLANE,
Dave Stevenson090cb0c2017-11-16 14:22:30 +000084 .pixel_order = HVS_PIXEL_ORDER_XYCRCB,
Eric Anholtfc040232015-12-30 12:25:44 -080085 },
86 {
87 .drm = DRM_FORMAT_YUV420,
88 .hvs = HVS_PIXEL_FORMAT_YCBCR_YUV420_3PLANE,
Dave Stevenson090cb0c2017-11-16 14:22:30 +000089 .pixel_order = HVS_PIXEL_ORDER_XYCBCR,
Eric Anholtfc040232015-12-30 12:25:44 -080090 },
91 {
92 .drm = DRM_FORMAT_YVU420,
93 .hvs = HVS_PIXEL_FORMAT_YCBCR_YUV420_3PLANE,
Dave Stevenson090cb0c2017-11-16 14:22:30 +000094 .pixel_order = HVS_PIXEL_ORDER_XYCRCB,
Eric Anholtfc040232015-12-30 12:25:44 -080095 },
96 {
97 .drm = DRM_FORMAT_NV12,
98 .hvs = HVS_PIXEL_FORMAT_YCBCR_YUV420_2PLANE,
Dave Stevenson090cb0c2017-11-16 14:22:30 +000099 .pixel_order = HVS_PIXEL_ORDER_XYCBCR,
Eric Anholtfc040232015-12-30 12:25:44 -0800100 },
101 {
Dave Stevensoncb20dd12017-11-16 14:22:31 +0000102 .drm = DRM_FORMAT_NV21,
103 .hvs = HVS_PIXEL_FORMAT_YCBCR_YUV420_2PLANE,
104 .pixel_order = HVS_PIXEL_ORDER_XYCRCB,
105 },
106 {
Eric Anholtfc040232015-12-30 12:25:44 -0800107 .drm = DRM_FORMAT_NV16,
108 .hvs = HVS_PIXEL_FORMAT_YCBCR_YUV422_2PLANE,
Dave Stevenson090cb0c2017-11-16 14:22:30 +0000109 .pixel_order = HVS_PIXEL_ORDER_XYCBCR,
Eric Anholtfc040232015-12-30 12:25:44 -0800110 },
Dave Stevensoncb20dd12017-11-16 14:22:31 +0000111 {
112 .drm = DRM_FORMAT_NV61,
113 .hvs = HVS_PIXEL_FORMAT_YCBCR_YUV422_2PLANE,
114 .pixel_order = HVS_PIXEL_ORDER_XYCRCB,
115 },
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800116};
117
118static const struct hvs_format *vc4_get_hvs_format(u32 drm_format)
119{
120 unsigned i;
121
122 for (i = 0; i < ARRAY_SIZE(hvs_formats); i++) {
123 if (hvs_formats[i].drm == drm_format)
124 return &hvs_formats[i];
125 }
126
127 return NULL;
128}
129
Eric Anholt21af94c2015-10-20 16:06:57 +0100130static enum vc4_scaling_mode vc4_get_scaling_mode(u32 src, u32 dst)
131{
Boris Brezilloneb8dd3a2018-11-09 11:26:33 +0100132 if (dst == src)
Eric Anholt21af94c2015-10-20 16:06:57 +0100133 return VC4_SCALING_NONE;
Boris Brezilloneb8dd3a2018-11-09 11:26:33 +0100134 if (3 * dst >= 2 * src)
135 return VC4_SCALING_PPF;
136 else
137 return VC4_SCALING_TPZ;
Eric Anholt21af94c2015-10-20 16:06:57 +0100138}
139
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800140static bool plane_enabled(struct drm_plane_state *state)
141{
142 return state->fb && state->crtc;
143}
144
kbuild test robot91276ae2015-10-22 11:12:26 +0800145static struct drm_plane_state *vc4_plane_duplicate_state(struct drm_plane *plane)
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800146{
147 struct vc4_plane_state *vc4_state;
148
149 if (WARN_ON(!plane->state))
150 return NULL;
151
152 vc4_state = kmemdup(plane->state, sizeof(*vc4_state), GFP_KERNEL);
153 if (!vc4_state)
154 return NULL;
155
Eric Anholt21af94c2015-10-20 16:06:57 +0100156 memset(&vc4_state->lbm, 0, sizeof(vc4_state->lbm));
Boris Brezillon8d938442018-11-30 10:02:51 +0100157 vc4_state->dlist_initialized = 0;
Eric Anholt21af94c2015-10-20 16:06:57 +0100158
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800159 __drm_atomic_helper_plane_duplicate_state(plane, &vc4_state->base);
160
161 if (vc4_state->dlist) {
162 vc4_state->dlist = kmemdup(vc4_state->dlist,
163 vc4_state->dlist_count * 4,
164 GFP_KERNEL);
165 if (!vc4_state->dlist) {
166 kfree(vc4_state);
167 return NULL;
168 }
169 vc4_state->dlist_size = vc4_state->dlist_count;
170 }
171
172 return &vc4_state->base;
173}
174
kbuild test robot91276ae2015-10-22 11:12:26 +0800175static void vc4_plane_destroy_state(struct drm_plane *plane,
176 struct drm_plane_state *state)
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800177{
Eric Anholt21af94c2015-10-20 16:06:57 +0100178 struct vc4_dev *vc4 = to_vc4_dev(plane->dev);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800179 struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
180
Eric Anholt21af94c2015-10-20 16:06:57 +0100181 if (vc4_state->lbm.allocated) {
182 unsigned long irqflags;
183
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);
187 }
188
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800189 kfree(vc4_state->dlist);
Daniel Vetter2f701692016-05-09 16:34:10 +0200190 __drm_atomic_helper_plane_destroy_state(&vc4_state->base);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800191 kfree(state);
192}
193
194/* Called during init to allocate the plane's atomic state. */
kbuild test robot91276ae2015-10-22 11:12:26 +0800195static void vc4_plane_reset(struct drm_plane *plane)
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800196{
197 struct vc4_plane_state *vc4_state;
198
199 WARN_ON(plane->state);
200
201 vc4_state = kzalloc(sizeof(*vc4_state), GFP_KERNEL);
202 if (!vc4_state)
203 return;
204
Alexandru Gheorghe42da6332018-08-04 17:15:29 +0100205 __drm_atomic_helper_plane_reset(plane, &vc4_state->base);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800206}
207
208static void vc4_dlist_write(struct vc4_plane_state *vc4_state, u32 val)
209{
210 if (vc4_state->dlist_count == vc4_state->dlist_size) {
211 u32 new_size = max(4u, vc4_state->dlist_count * 2);
Kees Cook6da2ec52018-06-12 13:55:00 -0700212 u32 *new_dlist = kmalloc_array(new_size, 4, GFP_KERNEL);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800213
214 if (!new_dlist)
215 return;
216 memcpy(new_dlist, vc4_state->dlist, vc4_state->dlist_count * 4);
217
218 kfree(vc4_state->dlist);
219 vc4_state->dlist = new_dlist;
220 vc4_state->dlist_size = new_size;
221 }
222
223 vc4_state->dlist[vc4_state->dlist_count++] = val;
224}
225
Eric Anholt21af94c2015-10-20 16:06:57 +0100226/* Returns the scl0/scl1 field based on whether the dimensions need to
227 * be up/down/non-scaled.
228 *
229 * This is a replication of a table from the spec.
230 */
Eric Anholtfc040232015-12-30 12:25:44 -0800231static u32 vc4_get_scl_field(struct drm_plane_state *state, int plane)
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800232{
233 struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
Eric Anholt21af94c2015-10-20 16:06:57 +0100234
Eric Anholtfc040232015-12-30 12:25:44 -0800235 switch (vc4_state->x_scaling[plane] << 2 | vc4_state->y_scaling[plane]) {
Eric Anholt21af94c2015-10-20 16:06:57 +0100236 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;
252 default:
253 case VC4_SCALING_NONE << 2 | VC4_SCALING_NONE:
254 /* The unity case is independently handled by
255 * SCALER_CTL0_UNITY.
256 */
257 return 0;
258 }
259}
260
261static int vc4_plane_setup_clipping_and_scaling(struct drm_plane_state *state)
262{
263 struct drm_plane *plane = state->plane;
264 struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800265 struct drm_framebuffer *fb = state->fb;
Eric Anholtfc040232015-12-30 12:25:44 -0800266 struct drm_gem_cma_object *bo = drm_fb_cma_get_gem_obj(fb, 0);
Eric Anholt21af94c2015-10-20 16:06:57 +0100267 u32 subpixel_src_mask = (1 << 16) - 1;
Ville Syrjälä438b74a2016-12-14 23:32:55 +0200268 u32 format = fb->format->format;
Ville Syrjäläbcb0b462016-12-14 23:30:22 +0200269 int num_planes = fb->format->num_planes;
Boris Brezillon58a6a36f2018-08-03 11:22:29 +0200270 int min_scale = 1, max_scale = INT_MAX;
271 struct drm_crtc_state *crtc_state;
272 u32 h_subsample, v_subsample;
273 int i, ret;
274
275 crtc_state = drm_atomic_get_existing_crtc_state(state->state,
276 state->crtc);
277 if (!crtc_state) {
278 DRM_DEBUG_KMS("Invalid crtc state\n");
279 return -EINVAL;
280 }
281
282 /* No configuring scaling on the cursor plane, since it gets
283 * non-vblank-synced updates, and scaling requires LBM changes which
284 * have to be vblank-synced.
285 */
286 if (plane->type == DRM_PLANE_TYPE_CURSOR) {
287 min_scale = DRM_PLANE_HELPER_NO_SCALING;
288 max_scale = DRM_PLANE_HELPER_NO_SCALING;
289 } else {
290 min_scale = 1;
291 max_scale = INT_MAX;
292 }
293
294 ret = drm_atomic_helper_check_plane_state(state, crtc_state,
295 min_scale, max_scale,
296 true, true);
297 if (ret)
298 return ret;
299
300 h_subsample = drm_format_horz_chroma_subsampling(format);
301 v_subsample = drm_format_vert_chroma_subsampling(format);
Eric Anholt5c679992015-12-28 14:34:44 -0800302
Eric Anholtfc040232015-12-30 12:25:44 -0800303 for (i = 0; i < num_planes; i++)
304 vc4_state->offsets[i] = bo->paddr + fb->offsets[i];
Eric Anholt5c679992015-12-28 14:34:44 -0800305
Eric Anholt21af94c2015-10-20 16:06:57 +0100306 /* We don't support subpixel source positioning for scaling. */
Boris Brezillon58a6a36f2018-08-03 11:22:29 +0200307 if ((state->src.x1 & subpixel_src_mask) ||
308 (state->src.x2 & subpixel_src_mask) ||
309 (state->src.y1 & subpixel_src_mask) ||
310 (state->src.y2 & subpixel_src_mask)) {
Eric Anholtbf893ac2015-10-23 10:36:27 +0100311 return -EINVAL;
312 }
313
Boris Brezillon58a6a36f2018-08-03 11:22:29 +0200314 vc4_state->src_x = state->src.x1 >> 16;
315 vc4_state->src_y = state->src.y1 >> 16;
316 vc4_state->src_w[0] = (state->src.x2 - state->src.x1) >> 16;
317 vc4_state->src_h[0] = (state->src.y2 - state->src.y1) >> 16;
Eric Anholtf863e352015-12-28 14:45:25 -0800318
Boris Brezillon58a6a36f2018-08-03 11:22:29 +0200319 vc4_state->crtc_x = state->dst.x1;
320 vc4_state->crtc_y = state->dst.y1;
321 vc4_state->crtc_w = state->dst.x2 - state->dst.x1;
322 vc4_state->crtc_h = state->dst.y2 - state->dst.y1;
Eric Anholtf863e352015-12-28 14:45:25 -0800323
Eric Anholtfc040232015-12-30 12:25:44 -0800324 vc4_state->x_scaling[0] = vc4_get_scaling_mode(vc4_state->src_w[0],
325 vc4_state->crtc_w);
326 vc4_state->y_scaling[0] = vc4_get_scaling_mode(vc4_state->src_h[0],
327 vc4_state->crtc_h);
328
Boris Brezillon658d8cb2018-07-25 14:29:07 +0200329 vc4_state->is_unity = (vc4_state->x_scaling[0] == VC4_SCALING_NONE &&
330 vc4_state->y_scaling[0] == VC4_SCALING_NONE);
331
Eric Anholtfc040232015-12-30 12:25:44 -0800332 if (num_planes > 1) {
333 vc4_state->is_yuv = true;
334
Eric Anholtfc040232015-12-30 12:25:44 -0800335 vc4_state->src_w[1] = vc4_state->src_w[0] / h_subsample;
336 vc4_state->src_h[1] = vc4_state->src_h[0] / v_subsample;
337
338 vc4_state->x_scaling[1] =
339 vc4_get_scaling_mode(vc4_state->src_w[1],
340 vc4_state->crtc_w);
341 vc4_state->y_scaling[1] =
342 vc4_get_scaling_mode(vc4_state->src_h[1],
343 vc4_state->crtc_h);
344
Boris Brezillon05600542018-11-09 11:26:32 +0100345 /* YUV conversion requires that horizontal scaling be enabled
346 * on the UV plane even if vc4_get_scaling_mode() returned
347 * VC4_SCALING_NONE (which can happen when the down-scaling
348 * ratio is 0.5). Let's force it to VC4_SCALING_PPF in this
349 * case.
Eric Anholtfc040232015-12-30 12:25:44 -0800350 */
Boris Brezillon05600542018-11-09 11:26:32 +0100351 if (vc4_state->x_scaling[1] == VC4_SCALING_NONE)
352 vc4_state->x_scaling[1] = VC4_SCALING_PPF;
Boris Brezillona6a00912018-07-24 15:36:01 +0200353 } else {
Boris Brezillon2b02a052018-10-09 15:24:46 +0200354 vc4_state->is_yuv = false;
Boris Brezillona6a00912018-07-24 15:36:01 +0200355 vc4_state->x_scaling[1] = VC4_SCALING_NONE;
356 vc4_state->y_scaling[1] = VC4_SCALING_NONE;
Eric Anholtfc040232015-12-30 12:25:44 -0800357 }
358
Eric Anholt5c679992015-12-28 14:34:44 -0800359 return 0;
360}
361
Eric Anholt21af94c2015-10-20 16:06:57 +0100362static void vc4_write_tpz(struct vc4_plane_state *vc4_state, u32 src, u32 dst)
363{
364 u32 scale, recip;
365
366 scale = (1 << 16) * src / dst;
367
368 /* The specs note that while the reciprocal would be defined
369 * as (1<<32)/scale, ~0 is close enough.
370 */
371 recip = ~0 / scale;
372
373 vc4_dlist_write(vc4_state,
374 VC4_SET_FIELD(scale, SCALER_TPZ0_SCALE) |
375 VC4_SET_FIELD(0, SCALER_TPZ0_IPHASE));
376 vc4_dlist_write(vc4_state,
377 VC4_SET_FIELD(recip, SCALER_TPZ1_RECIP));
378}
379
380static void vc4_write_ppf(struct vc4_plane_state *vc4_state, u32 src, u32 dst)
381{
382 u32 scale = (1 << 16) * src / dst;
383
384 vc4_dlist_write(vc4_state,
385 SCALER_PPF_AGC |
386 VC4_SET_FIELD(scale, SCALER_PPF_SCALE) |
387 VC4_SET_FIELD(0, SCALER_PPF_IPHASE));
388}
389
390static u32 vc4_lbm_size(struct drm_plane_state *state)
391{
392 struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
393 /* This is the worst case number. One of the two sizes will
394 * be used depending on the scaling configuration.
395 */
Eric Anholtfc040232015-12-30 12:25:44 -0800396 u32 pix_per_line = max(vc4_state->src_w[0], (u32)vc4_state->crtc_w);
Eric Anholt21af94c2015-10-20 16:06:57 +0100397 u32 lbm;
398
Boris Brezillonb2e554d2018-11-30 10:02:49 +0100399 /* LBM is not needed when there's no vertical scaling. */
400 if (vc4_state->y_scaling[0] == VC4_SCALING_NONE &&
401 vc4_state->y_scaling[1] == VC4_SCALING_NONE)
402 return 0;
403
Eric Anholtfc040232015-12-30 12:25:44 -0800404 if (!vc4_state->is_yuv) {
Boris Brezillonb2e554d2018-11-30 10:02:49 +0100405 if (vc4_state->y_scaling[0] == VC4_SCALING_TPZ)
Eric Anholtfc040232015-12-30 12:25:44 -0800406 lbm = pix_per_line * 8;
407 else {
408 /* In special cases, this multiplier might be 12. */
409 lbm = pix_per_line * 16;
410 }
411 } else {
412 /* There are cases for this going down to a multiplier
413 * of 2, but according to the firmware source, the
414 * table in the docs is somewhat wrong.
415 */
Eric Anholt21af94c2015-10-20 16:06:57 +0100416 lbm = pix_per_line * 16;
417 }
418
419 lbm = roundup(lbm, 32);
420
421 return lbm;
422}
423
Eric Anholtfc040232015-12-30 12:25:44 -0800424static void vc4_write_scaling_parameters(struct drm_plane_state *state,
425 int channel)
Eric Anholt21af94c2015-10-20 16:06:57 +0100426{
427 struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
428
429 /* Ch0 H-PPF Word 0: Scaling Parameters */
Eric Anholtfc040232015-12-30 12:25:44 -0800430 if (vc4_state->x_scaling[channel] == VC4_SCALING_PPF) {
Eric Anholt21af94c2015-10-20 16:06:57 +0100431 vc4_write_ppf(vc4_state,
Eric Anholtfc040232015-12-30 12:25:44 -0800432 vc4_state->src_w[channel], vc4_state->crtc_w);
Eric Anholt21af94c2015-10-20 16:06:57 +0100433 }
434
435 /* Ch0 V-PPF Words 0-1: Scaling Parameters, Context */
Eric Anholtfc040232015-12-30 12:25:44 -0800436 if (vc4_state->y_scaling[channel] == VC4_SCALING_PPF) {
Eric Anholt21af94c2015-10-20 16:06:57 +0100437 vc4_write_ppf(vc4_state,
Eric Anholtfc040232015-12-30 12:25:44 -0800438 vc4_state->src_h[channel], vc4_state->crtc_h);
Eric Anholt21af94c2015-10-20 16:06:57 +0100439 vc4_dlist_write(vc4_state, 0xc0c0c0c0);
440 }
441
442 /* Ch0 H-TPZ Words 0-1: Scaling Parameters, Recip */
Eric Anholtfc040232015-12-30 12:25:44 -0800443 if (vc4_state->x_scaling[channel] == VC4_SCALING_TPZ) {
Eric Anholt21af94c2015-10-20 16:06:57 +0100444 vc4_write_tpz(vc4_state,
Eric Anholtfc040232015-12-30 12:25:44 -0800445 vc4_state->src_w[channel], vc4_state->crtc_w);
Eric Anholt21af94c2015-10-20 16:06:57 +0100446 }
447
448 /* Ch0 V-TPZ Words 0-2: Scaling Parameters, Recip, Context */
Eric Anholtfc040232015-12-30 12:25:44 -0800449 if (vc4_state->y_scaling[channel] == VC4_SCALING_TPZ) {
Eric Anholt21af94c2015-10-20 16:06:57 +0100450 vc4_write_tpz(vc4_state,
Eric Anholtfc040232015-12-30 12:25:44 -0800451 vc4_state->src_h[channel], vc4_state->crtc_h);
Eric Anholt21af94c2015-10-20 16:06:57 +0100452 vc4_dlist_write(vc4_state, 0xc0c0c0c0);
453 }
454}
Eric Anholt5c679992015-12-28 14:34:44 -0800455
Boris Brezillon0a038c12018-11-30 10:02:50 +0100456static int vc4_plane_allocate_lbm(struct drm_plane_state *state)
457{
458 struct vc4_dev *vc4 = to_vc4_dev(state->plane->dev);
459 struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
460 unsigned long irqflags;
461 u32 lbm_size;
462
463 lbm_size = vc4_lbm_size(state);
464 if (!lbm_size)
465 return 0;
466
467 if (WARN_ON(!vc4_state->lbm_offset))
468 return -EINVAL;
469
470 /* Allocate the LBM memory that the HVS will use for temporary
471 * storage due to our scaling/format conversion.
472 */
473 if (!vc4_state->lbm.allocated) {
474 int ret;
475
476 spin_lock_irqsave(&vc4->hvs->mm_lock, irqflags);
477 ret = drm_mm_insert_node_generic(&vc4->hvs->lbm_mm,
478 &vc4_state->lbm,
479 lbm_size, 32, 0, 0);
480 spin_unlock_irqrestore(&vc4->hvs->mm_lock, irqflags);
481
482 if (ret)
483 return ret;
484 } else {
485 WARN_ON_ONCE(lbm_size != vc4_state->lbm.size);
486 }
487
488 vc4_state->dlist[vc4_state->lbm_offset] = vc4_state->lbm.start;
489
490 return 0;
491}
492
Eric Anholt5c679992015-12-28 14:34:44 -0800493/* Writes out a full display list for an active plane to the plane's
494 * private dlist state.
495 */
496static int vc4_plane_mode_set(struct drm_plane *plane,
497 struct drm_plane_state *state)
498{
Eric Anholt21af94c2015-10-20 16:06:57 +0100499 struct vc4_dev *vc4 = to_vc4_dev(plane->dev);
Eric Anholt5c679992015-12-28 14:34:44 -0800500 struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
501 struct drm_framebuffer *fb = state->fb;
Eric Anholt5c679992015-12-28 14:34:44 -0800502 u32 ctl0_offset = vc4_state->dlist_count;
Ville Syrjälä438b74a2016-12-14 23:32:55 +0200503 const struct hvs_format *format = vc4_get_hvs_format(fb->format->format);
Dave Stevensone065a8d2018-03-16 15:04:35 -0700504 u64 base_format_mod = fourcc_mod_broadcom_mod(fb->modifier);
Eric Anholtfc040232015-12-30 12:25:44 -0800505 int num_planes = drm_format_num_planes(format->drm);
Boris Brezillona65511b12018-08-03 11:22:30 +0200506 u32 h_subsample, v_subsample;
Stefan Schake22445f02018-04-20 17:09:54 -0700507 bool mix_plane_alpha;
Stefan Schake3d67b682018-03-09 01:53:35 +0100508 bool covers_screen;
Eric Anholt98830d912017-06-07 17:13:35 -0700509 u32 scl0, scl1, pitch0;
Boris Brezillon0a038c12018-11-30 10:02:50 +0100510 u32 tiling;
Dave Stevensone065a8d2018-03-16 15:04:35 -0700511 u32 hvs_format = format->hvs;
Eric Anholtfc040232015-12-30 12:25:44 -0800512 int ret, i;
Eric Anholt5c679992015-12-28 14:34:44 -0800513
Boris Brezillon8d938442018-11-30 10:02:51 +0100514 if (vc4_state->dlist_initialized)
515 return 0;
516
Eric Anholt5c679992015-12-28 14:34:44 -0800517 ret = vc4_plane_setup_clipping_and_scaling(state);
518 if (ret)
519 return ret;
520
Eric Anholtfc040232015-12-30 12:25:44 -0800521 /* SCL1 is used for Cb/Cr scaling of planar formats. For RGB
522 * and 4:4:4, scl1 should be set to scl0 so both channels of
523 * the scaler do the same thing. For YUV, the Y plane needs
524 * to be put in channel 1 and Cb/Cr in channel 0, so we swap
525 * the scl fields here.
526 */
527 if (num_planes == 1) {
Boris Brezillon9a0e9802018-05-07 14:13:03 +0200528 scl0 = vc4_get_scl_field(state, 0);
Eric Anholtfc040232015-12-30 12:25:44 -0800529 scl1 = scl0;
530 } else {
531 scl0 = vc4_get_scl_field(state, 1);
532 scl1 = vc4_get_scl_field(state, 0);
533 }
Eric Anholt21af94c2015-10-20 16:06:57 +0100534
Boris Brezillona65511b12018-08-03 11:22:30 +0200535 h_subsample = drm_format_horz_chroma_subsampling(format->drm);
536 v_subsample = drm_format_vert_chroma_subsampling(format->drm);
537
Dave Stevensone065a8d2018-03-16 15:04:35 -0700538 switch (base_format_mod) {
Eric Anholt98830d912017-06-07 17:13:35 -0700539 case DRM_FORMAT_MOD_LINEAR:
540 tiling = SCALER_CTL0_TILING_LINEAR;
541 pitch0 = VC4_SET_FIELD(fb->pitches[0], SCALER_SRC_PITCH);
Boris Brezillona65511b12018-08-03 11:22:30 +0200542
543 /* Adjust the base pointer to the first pixel to be scanned
544 * out.
545 */
546 for (i = 0; i < num_planes; i++) {
547 vc4_state->offsets[i] += vc4_state->src_y /
548 (i ? v_subsample : 1) *
549 fb->pitches[i];
550 vc4_state->offsets[i] += vc4_state->src_x /
551 (i ? h_subsample : 1) *
552 fb->format->cpp[i];
553 }
Boris Brezillon3e407412018-08-03 11:22:31 +0200554
Eric Anholt98830d912017-06-07 17:13:35 -0700555 break;
Eric Anholt652badb2017-09-27 12:32:09 -0700556
557 case DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED: {
Eric Anholt652badb2017-09-27 12:32:09 -0700558 u32 tile_size_shift = 12; /* T tiles are 4kb */
Boris Brezillon3e407412018-08-03 11:22:31 +0200559 /* Whole-tile offsets, mostly for setting the pitch. */
560 u32 tile_w_shift = fb->format->cpp[0] == 2 ? 6 : 5;
Eric Anholt652badb2017-09-27 12:32:09 -0700561 u32 tile_h_shift = 5; /* 16 and 32bpp are 32 pixels high */
Boris Brezillon3e407412018-08-03 11:22:31 +0200562 u32 tile_w_mask = (1 << tile_w_shift) - 1;
563 /* The height mask on 32-bit-per-pixel tiles is 63, i.e. twice
564 * the height (in pixels) of a 4k tile.
565 */
566 u32 tile_h_mask = (2 << tile_h_shift) - 1;
567 /* For T-tiled, the FB pitch is "how many bytes from one row to
568 * the next, such that
569 *
570 * pitch * tile_h == tile_size * tiles_per_row
571 */
Eric Anholt652badb2017-09-27 12:32:09 -0700572 u32 tiles_w = fb->pitches[0] >> (tile_size_shift - tile_h_shift);
Boris Brezillon3e407412018-08-03 11:22:31 +0200573 u32 tiles_l = vc4_state->src_x >> tile_w_shift;
574 u32 tiles_r = tiles_w - tiles_l;
575 u32 tiles_t = vc4_state->src_y >> tile_h_shift;
576 /* Intra-tile offsets, which modify the base address (the
577 * SCALER_PITCH0_TILE_Y_OFFSET tells HVS how to walk from that
578 * base address).
579 */
580 u32 tile_y = (vc4_state->src_y >> 4) & 1;
581 u32 subtile_y = (vc4_state->src_y >> 2) & 3;
582 u32 utile_y = vc4_state->src_y & 3;
583 u32 x_off = vc4_state->src_x & tile_w_mask;
584 u32 y_off = vc4_state->src_y & tile_h_mask;
Eric Anholt652badb2017-09-27 12:32:09 -0700585
Eric Anholt98830d912017-06-07 17:13:35 -0700586 tiling = SCALER_CTL0_TILING_256B_OR_T;
Boris Brezillon3e407412018-08-03 11:22:31 +0200587 pitch0 = (VC4_SET_FIELD(x_off, SCALER_PITCH0_SINK_PIX) |
588 VC4_SET_FIELD(y_off, SCALER_PITCH0_TILE_Y_OFFSET) |
589 VC4_SET_FIELD(tiles_l, SCALER_PITCH0_TILE_WIDTH_L) |
590 VC4_SET_FIELD(tiles_r, SCALER_PITCH0_TILE_WIDTH_R));
591 vc4_state->offsets[0] += tiles_t * (tiles_w << tile_size_shift);
592 vc4_state->offsets[0] += subtile_y << 8;
593 vc4_state->offsets[0] += utile_y << 4;
Eric Anholt98830d912017-06-07 17:13:35 -0700594
Boris Brezillon3e407412018-08-03 11:22:31 +0200595 /* Rows of tiles alternate left-to-right and right-to-left. */
596 if (tiles_t & 1) {
597 pitch0 |= SCALER_PITCH0_TILE_INITIAL_LINE_DIR;
598 vc4_state->offsets[0] += (tiles_w - tiles_l) <<
599 tile_size_shift;
600 vc4_state->offsets[0] -= (1 + !tile_y) << 10;
601 } else {
602 vc4_state->offsets[0] += tiles_l << tile_size_shift;
603 vc4_state->offsets[0] += tile_y << 10;
604 }
605
Eric Anholt98830d912017-06-07 17:13:35 -0700606 break;
Eric Anholt652badb2017-09-27 12:32:09 -0700607 }
608
Dave Stevensone065a8d2018-03-16 15:04:35 -0700609 case DRM_FORMAT_MOD_BROADCOM_SAND64:
610 case DRM_FORMAT_MOD_BROADCOM_SAND128:
611 case DRM_FORMAT_MOD_BROADCOM_SAND256: {
612 uint32_t param = fourcc_mod_broadcom_param(fb->modifier);
613
614 /* Column-based NV12 or RGBA.
615 */
616 if (fb->format->num_planes > 1) {
617 if (hvs_format != HVS_PIXEL_FORMAT_YCBCR_YUV420_2PLANE) {
618 DRM_DEBUG_KMS("SAND format only valid for NV12/21");
619 return -EINVAL;
620 }
621 hvs_format = HVS_PIXEL_FORMAT_H264;
622 } else {
623 if (base_format_mod == DRM_FORMAT_MOD_BROADCOM_SAND256) {
624 DRM_DEBUG_KMS("SAND256 format only valid for H.264");
625 return -EINVAL;
626 }
627 }
628
629 switch (base_format_mod) {
630 case DRM_FORMAT_MOD_BROADCOM_SAND64:
631 tiling = SCALER_CTL0_TILING_64B;
632 break;
633 case DRM_FORMAT_MOD_BROADCOM_SAND128:
634 tiling = SCALER_CTL0_TILING_128B;
635 break;
636 case DRM_FORMAT_MOD_BROADCOM_SAND256:
637 tiling = SCALER_CTL0_TILING_256B_OR_T;
638 break;
639 default:
640 break;
641 }
642
643 if (param > SCALER_TILE_HEIGHT_MASK) {
644 DRM_DEBUG_KMS("SAND height too large (%d)\n", param);
645 return -EINVAL;
646 }
647
648 pitch0 = VC4_SET_FIELD(param, SCALER_TILE_HEIGHT);
649 break;
650 }
651
Eric Anholt98830d912017-06-07 17:13:35 -0700652 default:
653 DRM_DEBUG_KMS("Unsupported FB tiling flag 0x%16llx",
654 (long long)fb->modifier);
655 return -EINVAL;
656 }
657
Eric Anholt21af94c2015-10-20 16:06:57 +0100658 /* Control word */
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800659 vc4_dlist_write(vc4_state,
660 SCALER_CTL0_VALID |
Maxime Ripard3257ec72018-05-17 15:37:59 +0200661 VC4_SET_FIELD(SCALER_CTL0_RGBA_EXPAND_ROUND, SCALER_CTL0_RGBA_EXPAND) |
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800662 (format->pixel_order << SCALER_CTL0_ORDER_SHIFT) |
Dave Stevensone065a8d2018-03-16 15:04:35 -0700663 (hvs_format << SCALER_CTL0_PIXEL_FORMAT_SHIFT) |
Eric Anholt98830d912017-06-07 17:13:35 -0700664 VC4_SET_FIELD(tiling, SCALER_CTL0_TILING) |
Eric Anholt21af94c2015-10-20 16:06:57 +0100665 (vc4_state->is_unity ? SCALER_CTL0_UNITY : 0) |
Eric Anholtfc040232015-12-30 12:25:44 -0800666 VC4_SET_FIELD(scl0, SCALER_CTL0_SCL0) |
667 VC4_SET_FIELD(scl1, SCALER_CTL0_SCL1));
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800668
669 /* Position Word 0: Image Positions and Alpha Value */
Eric Anholt6674a9042015-12-30 11:50:22 -0800670 vc4_state->pos0_offset = vc4_state->dlist_count;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800671 vc4_dlist_write(vc4_state,
Stefan Schake22445f02018-04-20 17:09:54 -0700672 VC4_SET_FIELD(state->alpha >> 8, SCALER_POS0_FIXED_ALPHA) |
Eric Anholt5c679992015-12-28 14:34:44 -0800673 VC4_SET_FIELD(vc4_state->crtc_x, SCALER_POS0_START_X) |
674 VC4_SET_FIELD(vc4_state->crtc_y, SCALER_POS0_START_Y));
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800675
Eric Anholt21af94c2015-10-20 16:06:57 +0100676 /* Position Word 1: Scaled Image Dimensions. */
677 if (!vc4_state->is_unity) {
678 vc4_dlist_write(vc4_state,
679 VC4_SET_FIELD(vc4_state->crtc_w,
680 SCALER_POS1_SCL_WIDTH) |
681 VC4_SET_FIELD(vc4_state->crtc_h,
682 SCALER_POS1_SCL_HEIGHT));
683 }
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800684
Stefan Schake22445f02018-04-20 17:09:54 -0700685 /* Don't waste cycles mixing with plane alpha if the set alpha
686 * is opaque or there is no per-pixel alpha information.
687 * In any case we use the alpha property value as the fixed alpha.
688 */
689 mix_plane_alpha = state->alpha != DRM_BLEND_ALPHA_OPAQUE &&
690 fb->format->has_alpha;
691
Stefan Schake05202c22018-03-09 01:53:34 +0100692 /* Position Word 2: Source Image Size, Alpha */
Eric Anholt6674a9042015-12-30 11:50:22 -0800693 vc4_state->pos2_offset = vc4_state->dlist_count;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800694 vc4_dlist_write(vc4_state,
Maxime Ripard124e5da2017-12-22 15:31:27 +0100695 VC4_SET_FIELD(fb->format->has_alpha ?
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800696 SCALER_POS2_ALPHA_MODE_PIPELINE :
697 SCALER_POS2_ALPHA_MODE_FIXED,
698 SCALER_POS2_ALPHA_MODE) |
Stefan Schake22445f02018-04-20 17:09:54 -0700699 (mix_plane_alpha ? SCALER_POS2_ALPHA_MIX : 0) |
Stefan Schake05202c22018-03-09 01:53:34 +0100700 (fb->format->has_alpha ? SCALER_POS2_ALPHA_PREMULT : 0) |
Eric Anholtfc040232015-12-30 12:25:44 -0800701 VC4_SET_FIELD(vc4_state->src_w[0], SCALER_POS2_WIDTH) |
702 VC4_SET_FIELD(vc4_state->src_h[0], SCALER_POS2_HEIGHT));
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800703
704 /* Position Word 3: Context. Written by the HVS. */
705 vc4_dlist_write(vc4_state, 0xc0c0c0c0);
706
Eric Anholtfc040232015-12-30 12:25:44 -0800707
708 /* Pointer Word 0/1/2: RGB / Y / Cb / Cr Pointers
709 *
710 * The pointers may be any byte address.
711 */
Eric Anholt6674a9042015-12-30 11:50:22 -0800712 vc4_state->ptr0_offset = vc4_state->dlist_count;
Dave Stevenson090cb0c2017-11-16 14:22:30 +0000713 for (i = 0; i < num_planes; i++)
714 vc4_dlist_write(vc4_state, vc4_state->offsets[i]);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800715
Eric Anholtfc040232015-12-30 12:25:44 -0800716 /* Pointer Context Word 0/1/2: Written by the HVS */
717 for (i = 0; i < num_planes; i++)
718 vc4_dlist_write(vc4_state, 0xc0c0c0c0);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800719
Eric Anholt98830d912017-06-07 17:13:35 -0700720 /* Pitch word 0 */
721 vc4_dlist_write(vc4_state, pitch0);
722
723 /* Pitch word 1/2 */
724 for (i = 1; i < num_planes; i++) {
Dave Stevensone065a8d2018-03-16 15:04:35 -0700725 if (hvs_format != HVS_PIXEL_FORMAT_H264) {
726 vc4_dlist_write(vc4_state,
727 VC4_SET_FIELD(fb->pitches[i],
728 SCALER_SRC_PITCH));
729 } else {
730 vc4_dlist_write(vc4_state, pitch0);
731 }
Eric Anholtfc040232015-12-30 12:25:44 -0800732 }
733
734 /* Colorspace conversion words */
735 if (vc4_state->is_yuv) {
736 vc4_dlist_write(vc4_state, SCALER_CSC0_ITR_R_601_5);
737 vc4_dlist_write(vc4_state, SCALER_CSC1_ITR_R_601_5);
738 vc4_dlist_write(vc4_state, SCALER_CSC2_ITR_R_601_5);
739 }
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800740
Boris Brezillon0a038c12018-11-30 10:02:50 +0100741 vc4_state->lbm_offset = 0;
742
Boris Brezillon658d8cb2018-07-25 14:29:07 +0200743 if (vc4_state->x_scaling[0] != VC4_SCALING_NONE ||
744 vc4_state->x_scaling[1] != VC4_SCALING_NONE ||
745 vc4_state->y_scaling[0] != VC4_SCALING_NONE ||
746 vc4_state->y_scaling[1] != VC4_SCALING_NONE) {
Boris Brezillon0a038c12018-11-30 10:02:50 +0100747 /* Reserve a slot for the LBM Base Address. The real value will
748 * be set when calling vc4_plane_allocate_lbm().
749 */
Eric Anholtfc040232015-12-30 12:25:44 -0800750 if (vc4_state->y_scaling[0] != VC4_SCALING_NONE ||
Boris Brezillon0a038c12018-11-30 10:02:50 +0100751 vc4_state->y_scaling[1] != VC4_SCALING_NONE)
752 vc4_state->lbm_offset = vc4_state->dlist_count++;
Eric Anholt21af94c2015-10-20 16:06:57 +0100753
Eric Anholtfc040232015-12-30 12:25:44 -0800754 if (num_planes > 1) {
755 /* Emit Cb/Cr as channel 0 and Y as channel
756 * 1. This matches how we set up scl0/scl1
757 * above.
758 */
759 vc4_write_scaling_parameters(state, 1);
760 }
761 vc4_write_scaling_parameters(state, 0);
Eric Anholt21af94c2015-10-20 16:06:57 +0100762
763 /* If any PPF setup was done, then all the kernel
764 * pointers get uploaded.
765 */
Eric Anholtfc040232015-12-30 12:25:44 -0800766 if (vc4_state->x_scaling[0] == VC4_SCALING_PPF ||
767 vc4_state->y_scaling[0] == VC4_SCALING_PPF ||
768 vc4_state->x_scaling[1] == VC4_SCALING_PPF ||
769 vc4_state->y_scaling[1] == VC4_SCALING_PPF) {
Eric Anholt21af94c2015-10-20 16:06:57 +0100770 u32 kernel = VC4_SET_FIELD(vc4->hvs->mitchell_netravali_filter.start,
771 SCALER_PPF_KERNEL_OFFSET);
772
773 /* HPPF plane 0 */
774 vc4_dlist_write(vc4_state, kernel);
775 /* VPPF plane 0 */
776 vc4_dlist_write(vc4_state, kernel);
777 /* HPPF plane 1 */
778 vc4_dlist_write(vc4_state, kernel);
779 /* VPPF plane 1 */
780 vc4_dlist_write(vc4_state, kernel);
781 }
782 }
783
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800784 vc4_state->dlist[ctl0_offset] |=
785 VC4_SET_FIELD(vc4_state->dlist_count, SCALER_CTL0_SIZE);
786
Stefan Schake3d67b682018-03-09 01:53:35 +0100787 /* crtc_* are already clipped coordinates. */
788 covers_screen = vc4_state->crtc_x == 0 && vc4_state->crtc_y == 0 &&
789 vc4_state->crtc_w == state->crtc->mode.hdisplay &&
790 vc4_state->crtc_h == state->crtc->mode.vdisplay;
791 /* Background fill might be necessary when the plane has per-pixel
Stefan Schake22445f02018-04-20 17:09:54 -0700792 * alpha content or a non-opaque plane alpha and could blend from the
793 * background or does not cover the entire screen.
Stefan Schake3d67b682018-03-09 01:53:35 +0100794 */
Stefan Schake22445f02018-04-20 17:09:54 -0700795 vc4_state->needs_bg_fill = fb->format->has_alpha || !covers_screen ||
796 state->alpha != DRM_BLEND_ALPHA_OPAQUE;
Stefan Schake3d67b682018-03-09 01:53:35 +0100797
Boris Brezillon8d938442018-11-30 10:02:51 +0100798 /* Flag the dlist as initialized to avoid checking it twice in case
799 * the async update check already called vc4_plane_mode_set() and
800 * decided to fallback to sync update because async update was not
801 * possible.
802 */
803 vc4_state->dlist_initialized = 1;
804
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800805 return 0;
806}
807
808/* If a modeset involves changing the setup of a plane, the atomic
809 * infrastructure will call this to validate a proposed plane setup.
810 * However, if a plane isn't getting updated, this (and the
811 * corresponding vc4_plane_atomic_update) won't get called. Thus, we
812 * compute the dlist here and have all active plane dlists get updated
813 * in the CRTC's flush.
814 */
815static int vc4_plane_atomic_check(struct drm_plane *plane,
816 struct drm_plane_state *state)
817{
818 struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
Boris Brezillon0a038c12018-11-30 10:02:50 +0100819 int ret;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800820
821 vc4_state->dlist_count = 0;
822
Boris Brezillon0a038c12018-11-30 10:02:50 +0100823 if (!plane_enabled(state))
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800824 return 0;
Boris Brezillon0a038c12018-11-30 10:02:50 +0100825
826 ret = vc4_plane_mode_set(plane, state);
827 if (ret)
828 return ret;
829
830 return vc4_plane_allocate_lbm(state);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800831}
832
833static void vc4_plane_atomic_update(struct drm_plane *plane,
834 struct drm_plane_state *old_state)
835{
836 /* No contents here. Since we don't know where in the CRTC's
837 * dlist we should be stored, our dlist is uploaded to the
838 * hardware with vc4_plane_write_dlist() at CRTC atomic_flush
839 * time.
840 */
841}
842
843u32 vc4_plane_write_dlist(struct drm_plane *plane, u32 __iomem *dlist)
844{
845 struct vc4_plane_state *vc4_state = to_vc4_plane_state(plane->state);
846 int i;
847
Eric Anholtb501bac2015-11-30 12:34:01 -0800848 vc4_state->hw_dlist = dlist;
849
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800850 /* Can't memcpy_toio() because it needs to be 32-bit writes. */
851 for (i = 0; i < vc4_state->dlist_count; i++)
852 writel(vc4_state->dlist[i], &dlist[i]);
853
854 return vc4_state->dlist_count;
855}
856
Daniel Vetter2f196b72016-06-02 16:21:44 +0200857u32 vc4_plane_dlist_size(const struct drm_plane_state *state)
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800858{
Daniel Vetter2f196b72016-06-02 16:21:44 +0200859 const struct vc4_plane_state *vc4_state =
860 container_of(state, typeof(*vc4_state), base);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800861
862 return vc4_state->dlist_count;
863}
864
Eric Anholtb501bac2015-11-30 12:34:01 -0800865/* Updates the plane to immediately (well, once the FIFO needs
866 * refilling) scan out from at a new framebuffer.
867 */
868void vc4_plane_async_set_fb(struct drm_plane *plane, struct drm_framebuffer *fb)
869{
870 struct vc4_plane_state *vc4_state = to_vc4_plane_state(plane->state);
871 struct drm_gem_cma_object *bo = drm_fb_cma_get_gem_obj(fb, 0);
872 uint32_t addr;
873
874 /* We're skipping the address adjustment for negative origin,
875 * because this is only called on the primary plane.
876 */
877 WARN_ON_ONCE(plane->state->crtc_x < 0 || plane->state->crtc_y < 0);
878 addr = bo->paddr + fb->offsets[0];
879
880 /* Write the new address into the hardware immediately. The
881 * scanout will start from this address as soon as the FIFO
882 * needs to refill with pixels.
883 */
Eric Anholt6674a9042015-12-30 11:50:22 -0800884 writel(addr, &vc4_state->hw_dlist[vc4_state->ptr0_offset]);
Eric Anholtb501bac2015-11-30 12:34:01 -0800885
886 /* Also update the CPU-side dlist copy, so that any later
887 * atomic updates that don't do a new modeset on our plane
888 * also use our updated address.
889 */
Eric Anholt6674a9042015-12-30 11:50:22 -0800890 vc4_state->dlist[vc4_state->ptr0_offset] = addr;
Eric Anholtb501bac2015-11-30 12:34:01 -0800891}
892
Gustavo Padovan539c3202018-03-30 10:54:45 +0200893static void vc4_plane_atomic_async_update(struct drm_plane *plane,
894 struct drm_plane_state *state)
895{
Boris Brezillon5a439112018-11-15 11:58:51 +0100896 struct vc4_plane_state *vc4_state, *new_vc4_state;
Gustavo Padovan539c3202018-03-30 10:54:45 +0200897
Boris Brezillon1d4118c2018-11-30 10:02:52 +0100898 drm_atomic_set_fb_for_plane(plane->state, state->fb);
Gustavo Padovan539c3202018-03-30 10:54:45 +0200899 plane->state->crtc_x = state->crtc_x;
900 plane->state->crtc_y = state->crtc_y;
Boris Brezillon1d4118c2018-11-30 10:02:52 +0100901 plane->state->crtc_w = state->crtc_w;
902 plane->state->crtc_h = state->crtc_h;
Gustavo Padovan539c3202018-03-30 10:54:45 +0200903 plane->state->src_x = state->src_x;
904 plane->state->src_y = state->src_y;
Boris Brezillon1d4118c2018-11-30 10:02:52 +0100905 plane->state->src_w = state->src_w;
906 plane->state->src_h = state->src_h;
907 plane->state->src_h = state->src_h;
908 plane->state->alpha = state->alpha;
909 plane->state->pixel_blend_mode = state->pixel_blend_mode;
910 plane->state->rotation = state->rotation;
911 plane->state->zpos = state->zpos;
912 plane->state->normalized_zpos = state->normalized_zpos;
913 plane->state->color_encoding = state->color_encoding;
914 plane->state->color_range = state->color_range;
915 plane->state->src = state->src;
916 plane->state->dst = state->dst;
917 plane->state->visible = state->visible;
Boris Brezillon5a439112018-11-15 11:58:51 +0100918
919 new_vc4_state = to_vc4_plane_state(state);
920 vc4_state = to_vc4_plane_state(plane->state);
921
Boris Brezillon1d4118c2018-11-30 10:02:52 +0100922 vc4_state->crtc_x = new_vc4_state->crtc_x;
923 vc4_state->crtc_y = new_vc4_state->crtc_y;
924 vc4_state->crtc_h = new_vc4_state->crtc_h;
925 vc4_state->crtc_w = new_vc4_state->crtc_w;
926 vc4_state->src_x = new_vc4_state->src_x;
927 vc4_state->src_y = new_vc4_state->src_y;
928 memcpy(vc4_state->src_w, new_vc4_state->src_w,
929 sizeof(vc4_state->src_w));
930 memcpy(vc4_state->src_h, new_vc4_state->src_h,
931 sizeof(vc4_state->src_h));
932 memcpy(vc4_state->x_scaling, new_vc4_state->x_scaling,
933 sizeof(vc4_state->x_scaling));
934 memcpy(vc4_state->y_scaling, new_vc4_state->y_scaling,
935 sizeof(vc4_state->y_scaling));
936 vc4_state->is_unity = new_vc4_state->is_unity;
937 vc4_state->is_yuv = new_vc4_state->is_yuv;
938 memcpy(vc4_state->offsets, new_vc4_state->offsets,
939 sizeof(vc4_state->offsets));
940 vc4_state->needs_bg_fill = new_vc4_state->needs_bg_fill;
941
Boris Brezillon5a439112018-11-15 11:58:51 +0100942 /* Update the current vc4_state pos0, pos2 and ptr0 dlist entries. */
943 vc4_state->dlist[vc4_state->pos0_offset] =
944 new_vc4_state->dlist[vc4_state->pos0_offset];
945 vc4_state->dlist[vc4_state->pos2_offset] =
946 new_vc4_state->dlist[vc4_state->pos2_offset];
947 vc4_state->dlist[vc4_state->ptr0_offset] =
948 new_vc4_state->dlist[vc4_state->ptr0_offset];
Gustavo Padovan539c3202018-03-30 10:54:45 +0200949
950 /* Note that we can't just call vc4_plane_write_dlist()
951 * because that would smash the context data that the HVS is
952 * currently using.
953 */
954 writel(vc4_state->dlist[vc4_state->pos0_offset],
955 &vc4_state->hw_dlist[vc4_state->pos0_offset]);
956 writel(vc4_state->dlist[vc4_state->pos2_offset],
957 &vc4_state->hw_dlist[vc4_state->pos2_offset]);
958 writel(vc4_state->dlist[vc4_state->ptr0_offset],
959 &vc4_state->hw_dlist[vc4_state->ptr0_offset]);
960}
961
962static int vc4_plane_atomic_async_check(struct drm_plane *plane,
963 struct drm_plane_state *state)
964{
Boris Brezillon1d4118c2018-11-30 10:02:52 +0100965 struct vc4_plane_state *old_vc4_state, *new_vc4_state;
966 int ret;
967 u32 i;
968
969 ret = vc4_plane_mode_set(plane, state);
970 if (ret)
971 return ret;
972
973 old_vc4_state = to_vc4_plane_state(plane->state);
974 new_vc4_state = to_vc4_plane_state(state);
975 if (old_vc4_state->dlist_count != new_vc4_state->dlist_count ||
976 old_vc4_state->pos0_offset != new_vc4_state->pos0_offset ||
977 old_vc4_state->pos2_offset != new_vc4_state->pos2_offset ||
978 old_vc4_state->ptr0_offset != new_vc4_state->ptr0_offset ||
979 vc4_lbm_size(plane->state) != vc4_lbm_size(state))
Gustavo Padovan539c3202018-03-30 10:54:45 +0200980 return -EINVAL;
981
Boris Brezillon1d4118c2018-11-30 10:02:52 +0100982 /* Only pos0, pos2 and ptr0 DWORDS can be updated in an async update
983 * if anything else has changed, fallback to a sync update.
984 */
985 for (i = 0; i < new_vc4_state->dlist_count; i++) {
986 if (i == new_vc4_state->pos0_offset ||
987 i == new_vc4_state->pos2_offset ||
988 i == new_vc4_state->ptr0_offset ||
989 (new_vc4_state->lbm_offset &&
990 i == new_vc4_state->lbm_offset))
991 continue;
992
993 if (new_vc4_state->dlist[i] != old_vc4_state->dlist[i])
994 return -EINVAL;
995 }
996
Gustavo Padovan539c3202018-03-30 10:54:45 +0200997 return 0;
998}
999
Eric Anholt334dbd62017-06-21 11:49:59 -07001000static int vc4_prepare_fb(struct drm_plane *plane,
1001 struct drm_plane_state *state)
1002{
1003 struct vc4_bo *bo;
1004 struct dma_fence *fence;
Boris Brezillonb9f19252017-10-19 14:57:48 +02001005 int ret;
Eric Anholt334dbd62017-06-21 11:49:59 -07001006
Daniel Vetter2227a7a2018-04-05 17:44:48 +02001007 if (!state->fb)
Eric Anholt334dbd62017-06-21 11:49:59 -07001008 return 0;
1009
1010 bo = to_vc4_bo(&drm_fb_cma_get_gem_obj(state->fb, 0)->base);
Boris Brezillonb9f19252017-10-19 14:57:48 +02001011
Daniel Vetter2227a7a2018-04-05 17:44:48 +02001012 fence = reservation_object_get_excl_rcu(bo->resv);
1013 drm_atomic_set_fence_for_plane(state, fence);
1014
1015 if (plane->state->fb == state->fb)
1016 return 0;
1017
Boris Brezillonb9f19252017-10-19 14:57:48 +02001018 ret = vc4_bo_inc_usecnt(bo);
1019 if (ret)
1020 return ret;
1021
Eric Anholt334dbd62017-06-21 11:49:59 -07001022 return 0;
1023}
1024
Boris Brezillonb9f19252017-10-19 14:57:48 +02001025static void vc4_cleanup_fb(struct drm_plane *plane,
1026 struct drm_plane_state *state)
1027{
1028 struct vc4_bo *bo;
1029
1030 if (plane->state->fb == state->fb || !state->fb)
1031 return;
1032
1033 bo = to_vc4_bo(&drm_fb_cma_get_gem_obj(state->fb, 0)->base);
1034 vc4_bo_dec_usecnt(bo);
1035}
1036
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001037static const struct drm_plane_helper_funcs vc4_plane_helper_funcs = {
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001038 .atomic_check = vc4_plane_atomic_check,
1039 .atomic_update = vc4_plane_atomic_update,
Eric Anholt334dbd62017-06-21 11:49:59 -07001040 .prepare_fb = vc4_prepare_fb,
Boris Brezillonb9f19252017-10-19 14:57:48 +02001041 .cleanup_fb = vc4_cleanup_fb,
Gustavo Padovan539c3202018-03-30 10:54:45 +02001042 .atomic_async_check = vc4_plane_atomic_async_check,
1043 .atomic_async_update = vc4_plane_atomic_async_update,
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001044};
1045
1046static void vc4_plane_destroy(struct drm_plane *plane)
1047{
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001048 drm_plane_cleanup(plane);
1049}
1050
Daniel Stone423ad7b2017-08-08 17:44:48 +01001051static bool vc4_format_mod_supported(struct drm_plane *plane,
1052 uint32_t format,
1053 uint64_t modifier)
1054{
1055 /* Support T_TILING for RGB formats only. */
1056 switch (format) {
1057 case DRM_FORMAT_XRGB8888:
1058 case DRM_FORMAT_ARGB8888:
1059 case DRM_FORMAT_ABGR8888:
1060 case DRM_FORMAT_XBGR8888:
1061 case DRM_FORMAT_RGB565:
1062 case DRM_FORMAT_BGR565:
1063 case DRM_FORMAT_ARGB1555:
1064 case DRM_FORMAT_XRGB1555:
Dave Stevensone065a8d2018-03-16 15:04:35 -07001065 switch (fourcc_mod_broadcom_mod(modifier)) {
1066 case DRM_FORMAT_MOD_LINEAR:
1067 case DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED:
1068 case DRM_FORMAT_MOD_BROADCOM_SAND64:
1069 case DRM_FORMAT_MOD_BROADCOM_SAND128:
1070 return true;
1071 default:
1072 return false;
1073 }
1074 case DRM_FORMAT_NV12:
1075 case DRM_FORMAT_NV21:
1076 switch (fourcc_mod_broadcom_mod(modifier)) {
1077 case DRM_FORMAT_MOD_LINEAR:
1078 case DRM_FORMAT_MOD_BROADCOM_SAND64:
1079 case DRM_FORMAT_MOD_BROADCOM_SAND128:
1080 case DRM_FORMAT_MOD_BROADCOM_SAND256:
1081 return true;
1082 default:
1083 return false;
1084 }
Daniel Stone423ad7b2017-08-08 17:44:48 +01001085 case DRM_FORMAT_YUV422:
1086 case DRM_FORMAT_YVU422:
1087 case DRM_FORMAT_YUV420:
1088 case DRM_FORMAT_YVU420:
Daniel Stone423ad7b2017-08-08 17:44:48 +01001089 case DRM_FORMAT_NV16:
Eric Anholt1e871d62018-03-16 15:04:34 -07001090 case DRM_FORMAT_NV61:
Daniel Stone423ad7b2017-08-08 17:44:48 +01001091 default:
1092 return (modifier == DRM_FORMAT_MOD_LINEAR);
1093 }
1094}
1095
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001096static const struct drm_plane_funcs vc4_plane_funcs = {
Gustavo Padovan539c3202018-03-30 10:54:45 +02001097 .update_plane = drm_atomic_helper_update_plane,
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001098 .disable_plane = drm_atomic_helper_disable_plane,
1099 .destroy = vc4_plane_destroy,
1100 .set_property = NULL,
1101 .reset = vc4_plane_reset,
1102 .atomic_duplicate_state = vc4_plane_duplicate_state,
1103 .atomic_destroy_state = vc4_plane_destroy_state,
Daniel Stone423ad7b2017-08-08 17:44:48 +01001104 .format_mod_supported = vc4_format_mod_supported,
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001105};
1106
1107struct drm_plane *vc4_plane_init(struct drm_device *dev,
1108 enum drm_plane_type type)
1109{
1110 struct drm_plane *plane = NULL;
1111 struct vc4_plane *vc4_plane;
1112 u32 formats[ARRAY_SIZE(hvs_formats)];
Eric Anholtfc040232015-12-30 12:25:44 -08001113 u32 num_formats = 0;
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001114 int ret = 0;
1115 unsigned i;
Daniel Stone423ad7b2017-08-08 17:44:48 +01001116 static const uint64_t modifiers[] = {
1117 DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED,
Dave Stevensone065a8d2018-03-16 15:04:35 -07001118 DRM_FORMAT_MOD_BROADCOM_SAND128,
1119 DRM_FORMAT_MOD_BROADCOM_SAND64,
1120 DRM_FORMAT_MOD_BROADCOM_SAND256,
Daniel Stone423ad7b2017-08-08 17:44:48 +01001121 DRM_FORMAT_MOD_LINEAR,
1122 DRM_FORMAT_MOD_INVALID
1123 };
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001124
1125 vc4_plane = devm_kzalloc(dev->dev, sizeof(*vc4_plane),
1126 GFP_KERNEL);
Colin Ian King7b347342017-03-16 18:54:18 +00001127 if (!vc4_plane)
1128 return ERR_PTR(-ENOMEM);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001129
Eric Anholtfc040232015-12-30 12:25:44 -08001130 for (i = 0; i < ARRAY_SIZE(hvs_formats); i++) {
1131 /* Don't allow YUV in cursor planes, since that means
1132 * tuning on the scaler, which we don't allow for the
1133 * cursor.
1134 */
1135 if (type != DRM_PLANE_TYPE_CURSOR ||
1136 hvs_formats[i].hvs < HVS_PIXEL_FORMAT_YCBCR_YUV420_3PLANE) {
1137 formats[num_formats++] = hvs_formats[i].drm;
1138 }
1139 }
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001140 plane = &vc4_plane->base;
Andrzej Pietrasiewicz49d29a02017-02-01 10:35:08 +01001141 ret = drm_universal_plane_init(dev, plane, 0,
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001142 &vc4_plane_funcs,
Eric Anholtfc040232015-12-30 12:25:44 -08001143 formats, num_formats,
Daniel Stone423ad7b2017-08-08 17:44:48 +01001144 modifiers, type, NULL);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001145
1146 drm_plane_helper_add(plane, &vc4_plane_helper_funcs);
1147
Stefan Schake22445f02018-04-20 17:09:54 -07001148 drm_plane_create_alpha_property(plane);
1149
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001150 return plane;
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001151}