blob: 75db62cbe468df2324d395a2a33446c0a81cf172 [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{
Eric Anholt21af94c2015-10-20 16:06:57 +0100263 struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800264 struct drm_framebuffer *fb = state->fb;
Eric Anholtfc040232015-12-30 12:25:44 -0800265 struct drm_gem_cma_object *bo = drm_fb_cma_get_gem_obj(fb, 0);
Eric Anholt21af94c2015-10-20 16:06:57 +0100266 u32 subpixel_src_mask = (1 << 16) - 1;
Ville Syrjälä438b74a2016-12-14 23:32:55 +0200267 u32 format = fb->format->format;
Ville Syrjäläbcb0b462016-12-14 23:30:22 +0200268 int num_planes = fb->format->num_planes;
Boris Brezillon58a6a36f2018-08-03 11:22:29 +0200269 struct drm_crtc_state *crtc_state;
270 u32 h_subsample, v_subsample;
271 int i, ret;
272
273 crtc_state = drm_atomic_get_existing_crtc_state(state->state,
274 state->crtc);
275 if (!crtc_state) {
276 DRM_DEBUG_KMS("Invalid crtc state\n");
277 return -EINVAL;
278 }
279
Boris Brezillon5dc416d2018-11-30 10:02:53 +0100280 ret = drm_atomic_helper_check_plane_state(state, crtc_state, 1,
281 INT_MAX, true, true);
Boris Brezillon58a6a36f2018-08-03 11:22:29 +0200282 if (ret)
283 return ret;
284
285 h_subsample = drm_format_horz_chroma_subsampling(format);
286 v_subsample = drm_format_vert_chroma_subsampling(format);
Eric Anholt5c679992015-12-28 14:34:44 -0800287
Eric Anholtfc040232015-12-30 12:25:44 -0800288 for (i = 0; i < num_planes; i++)
289 vc4_state->offsets[i] = bo->paddr + fb->offsets[i];
Eric Anholt5c679992015-12-28 14:34:44 -0800290
Eric Anholt21af94c2015-10-20 16:06:57 +0100291 /* We don't support subpixel source positioning for scaling. */
Boris Brezillon58a6a36f2018-08-03 11:22:29 +0200292 if ((state->src.x1 & subpixel_src_mask) ||
293 (state->src.x2 & subpixel_src_mask) ||
294 (state->src.y1 & subpixel_src_mask) ||
295 (state->src.y2 & subpixel_src_mask)) {
Eric Anholtbf893ac2015-10-23 10:36:27 +0100296 return -EINVAL;
297 }
298
Boris Brezillon58a6a36f2018-08-03 11:22:29 +0200299 vc4_state->src_x = state->src.x1 >> 16;
300 vc4_state->src_y = state->src.y1 >> 16;
301 vc4_state->src_w[0] = (state->src.x2 - state->src.x1) >> 16;
302 vc4_state->src_h[0] = (state->src.y2 - state->src.y1) >> 16;
Eric Anholtf863e352015-12-28 14:45:25 -0800303
Boris Brezillon58a6a36f2018-08-03 11:22:29 +0200304 vc4_state->crtc_x = state->dst.x1;
305 vc4_state->crtc_y = state->dst.y1;
306 vc4_state->crtc_w = state->dst.x2 - state->dst.x1;
307 vc4_state->crtc_h = state->dst.y2 - state->dst.y1;
Eric Anholtf863e352015-12-28 14:45:25 -0800308
Eric Anholtfc040232015-12-30 12:25:44 -0800309 vc4_state->x_scaling[0] = vc4_get_scaling_mode(vc4_state->src_w[0],
310 vc4_state->crtc_w);
311 vc4_state->y_scaling[0] = vc4_get_scaling_mode(vc4_state->src_h[0],
312 vc4_state->crtc_h);
313
Boris Brezillon658d8cb2018-07-25 14:29:07 +0200314 vc4_state->is_unity = (vc4_state->x_scaling[0] == VC4_SCALING_NONE &&
315 vc4_state->y_scaling[0] == VC4_SCALING_NONE);
316
Eric Anholtfc040232015-12-30 12:25:44 -0800317 if (num_planes > 1) {
318 vc4_state->is_yuv = true;
319
Eric Anholtfc040232015-12-30 12:25:44 -0800320 vc4_state->src_w[1] = vc4_state->src_w[0] / h_subsample;
321 vc4_state->src_h[1] = vc4_state->src_h[0] / v_subsample;
322
323 vc4_state->x_scaling[1] =
324 vc4_get_scaling_mode(vc4_state->src_w[1],
325 vc4_state->crtc_w);
326 vc4_state->y_scaling[1] =
327 vc4_get_scaling_mode(vc4_state->src_h[1],
328 vc4_state->crtc_h);
329
Boris Brezillon05600542018-11-09 11:26:32 +0100330 /* YUV conversion requires that horizontal scaling be enabled
331 * on the UV plane even if vc4_get_scaling_mode() returned
332 * VC4_SCALING_NONE (which can happen when the down-scaling
333 * ratio is 0.5). Let's force it to VC4_SCALING_PPF in this
334 * case.
Eric Anholtfc040232015-12-30 12:25:44 -0800335 */
Boris Brezillon05600542018-11-09 11:26:32 +0100336 if (vc4_state->x_scaling[1] == VC4_SCALING_NONE)
337 vc4_state->x_scaling[1] = VC4_SCALING_PPF;
Boris Brezillona6a00912018-07-24 15:36:01 +0200338 } else {
Boris Brezillon2b02a052018-10-09 15:24:46 +0200339 vc4_state->is_yuv = false;
Boris Brezillona6a00912018-07-24 15:36:01 +0200340 vc4_state->x_scaling[1] = VC4_SCALING_NONE;
341 vc4_state->y_scaling[1] = VC4_SCALING_NONE;
Eric Anholtfc040232015-12-30 12:25:44 -0800342 }
343
Eric Anholt5c679992015-12-28 14:34:44 -0800344 return 0;
345}
346
Eric Anholt21af94c2015-10-20 16:06:57 +0100347static void vc4_write_tpz(struct vc4_plane_state *vc4_state, u32 src, u32 dst)
348{
349 u32 scale, recip;
350
351 scale = (1 << 16) * src / dst;
352
353 /* The specs note that while the reciprocal would be defined
354 * as (1<<32)/scale, ~0 is close enough.
355 */
356 recip = ~0 / scale;
357
358 vc4_dlist_write(vc4_state,
359 VC4_SET_FIELD(scale, SCALER_TPZ0_SCALE) |
360 VC4_SET_FIELD(0, SCALER_TPZ0_IPHASE));
361 vc4_dlist_write(vc4_state,
362 VC4_SET_FIELD(recip, SCALER_TPZ1_RECIP));
363}
364
365static void vc4_write_ppf(struct vc4_plane_state *vc4_state, u32 src, u32 dst)
366{
367 u32 scale = (1 << 16) * src / dst;
368
369 vc4_dlist_write(vc4_state,
370 SCALER_PPF_AGC |
371 VC4_SET_FIELD(scale, SCALER_PPF_SCALE) |
372 VC4_SET_FIELD(0, SCALER_PPF_IPHASE));
373}
374
375static u32 vc4_lbm_size(struct drm_plane_state *state)
376{
377 struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
378 /* This is the worst case number. One of the two sizes will
379 * be used depending on the scaling configuration.
380 */
Eric Anholtfc040232015-12-30 12:25:44 -0800381 u32 pix_per_line = max(vc4_state->src_w[0], (u32)vc4_state->crtc_w);
Eric Anholt21af94c2015-10-20 16:06:57 +0100382 u32 lbm;
383
Boris Brezillonb2e554d2018-11-30 10:02:49 +0100384 /* LBM is not needed when there's no vertical scaling. */
385 if (vc4_state->y_scaling[0] == VC4_SCALING_NONE &&
386 vc4_state->y_scaling[1] == VC4_SCALING_NONE)
387 return 0;
388
Eric Anholtfc040232015-12-30 12:25:44 -0800389 if (!vc4_state->is_yuv) {
Boris Brezillonb2e554d2018-11-30 10:02:49 +0100390 if (vc4_state->y_scaling[0] == VC4_SCALING_TPZ)
Eric Anholtfc040232015-12-30 12:25:44 -0800391 lbm = pix_per_line * 8;
392 else {
393 /* In special cases, this multiplier might be 12. */
394 lbm = pix_per_line * 16;
395 }
396 } else {
397 /* There are cases for this going down to a multiplier
398 * of 2, but according to the firmware source, the
399 * table in the docs is somewhat wrong.
400 */
Eric Anholt21af94c2015-10-20 16:06:57 +0100401 lbm = pix_per_line * 16;
402 }
403
404 lbm = roundup(lbm, 32);
405
406 return lbm;
407}
408
Eric Anholtfc040232015-12-30 12:25:44 -0800409static void vc4_write_scaling_parameters(struct drm_plane_state *state,
410 int channel)
Eric Anholt21af94c2015-10-20 16:06:57 +0100411{
412 struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
413
414 /* Ch0 H-PPF Word 0: Scaling Parameters */
Eric Anholtfc040232015-12-30 12:25:44 -0800415 if (vc4_state->x_scaling[channel] == VC4_SCALING_PPF) {
Eric Anholt21af94c2015-10-20 16:06:57 +0100416 vc4_write_ppf(vc4_state,
Eric Anholtfc040232015-12-30 12:25:44 -0800417 vc4_state->src_w[channel], vc4_state->crtc_w);
Eric Anholt21af94c2015-10-20 16:06:57 +0100418 }
419
420 /* Ch0 V-PPF Words 0-1: Scaling Parameters, Context */
Eric Anholtfc040232015-12-30 12:25:44 -0800421 if (vc4_state->y_scaling[channel] == VC4_SCALING_PPF) {
Eric Anholt21af94c2015-10-20 16:06:57 +0100422 vc4_write_ppf(vc4_state,
Eric Anholtfc040232015-12-30 12:25:44 -0800423 vc4_state->src_h[channel], vc4_state->crtc_h);
Eric Anholt21af94c2015-10-20 16:06:57 +0100424 vc4_dlist_write(vc4_state, 0xc0c0c0c0);
425 }
426
427 /* Ch0 H-TPZ Words 0-1: Scaling Parameters, Recip */
Eric Anholtfc040232015-12-30 12:25:44 -0800428 if (vc4_state->x_scaling[channel] == VC4_SCALING_TPZ) {
Eric Anholt21af94c2015-10-20 16:06:57 +0100429 vc4_write_tpz(vc4_state,
Eric Anholtfc040232015-12-30 12:25:44 -0800430 vc4_state->src_w[channel], vc4_state->crtc_w);
Eric Anholt21af94c2015-10-20 16:06:57 +0100431 }
432
433 /* Ch0 V-TPZ Words 0-2: Scaling Parameters, Recip, Context */
Eric Anholtfc040232015-12-30 12:25:44 -0800434 if (vc4_state->y_scaling[channel] == VC4_SCALING_TPZ) {
Eric Anholt21af94c2015-10-20 16:06:57 +0100435 vc4_write_tpz(vc4_state,
Eric Anholtfc040232015-12-30 12:25:44 -0800436 vc4_state->src_h[channel], vc4_state->crtc_h);
Eric Anholt21af94c2015-10-20 16:06:57 +0100437 vc4_dlist_write(vc4_state, 0xc0c0c0c0);
438 }
439}
Eric Anholt5c679992015-12-28 14:34:44 -0800440
Boris Brezillon0a038c12018-11-30 10:02:50 +0100441static int vc4_plane_allocate_lbm(struct drm_plane_state *state)
442{
443 struct vc4_dev *vc4 = to_vc4_dev(state->plane->dev);
444 struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
445 unsigned long irqflags;
446 u32 lbm_size;
447
448 lbm_size = vc4_lbm_size(state);
449 if (!lbm_size)
450 return 0;
451
452 if (WARN_ON(!vc4_state->lbm_offset))
453 return -EINVAL;
454
455 /* Allocate the LBM memory that the HVS will use for temporary
456 * storage due to our scaling/format conversion.
457 */
458 if (!vc4_state->lbm.allocated) {
459 int ret;
460
461 spin_lock_irqsave(&vc4->hvs->mm_lock, irqflags);
462 ret = drm_mm_insert_node_generic(&vc4->hvs->lbm_mm,
463 &vc4_state->lbm,
464 lbm_size, 32, 0, 0);
465 spin_unlock_irqrestore(&vc4->hvs->mm_lock, irqflags);
466
467 if (ret)
468 return ret;
469 } else {
470 WARN_ON_ONCE(lbm_size != vc4_state->lbm.size);
471 }
472
473 vc4_state->dlist[vc4_state->lbm_offset] = vc4_state->lbm.start;
474
475 return 0;
476}
477
Eric Anholt5c679992015-12-28 14:34:44 -0800478/* Writes out a full display list for an active plane to the plane's
479 * private dlist state.
480 */
481static int vc4_plane_mode_set(struct drm_plane *plane,
482 struct drm_plane_state *state)
483{
Eric Anholt21af94c2015-10-20 16:06:57 +0100484 struct vc4_dev *vc4 = to_vc4_dev(plane->dev);
Eric Anholt5c679992015-12-28 14:34:44 -0800485 struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
486 struct drm_framebuffer *fb = state->fb;
Eric Anholt5c679992015-12-28 14:34:44 -0800487 u32 ctl0_offset = vc4_state->dlist_count;
Ville Syrjälä438b74a2016-12-14 23:32:55 +0200488 const struct hvs_format *format = vc4_get_hvs_format(fb->format->format);
Dave Stevensone065a8d2018-03-16 15:04:35 -0700489 u64 base_format_mod = fourcc_mod_broadcom_mod(fb->modifier);
Eric Anholtfc040232015-12-30 12:25:44 -0800490 int num_planes = drm_format_num_planes(format->drm);
Boris Brezillona65511b12018-08-03 11:22:30 +0200491 u32 h_subsample, v_subsample;
Stefan Schake22445f02018-04-20 17:09:54 -0700492 bool mix_plane_alpha;
Stefan Schake3d67b682018-03-09 01:53:35 +0100493 bool covers_screen;
Eric Anholt98830d912017-06-07 17:13:35 -0700494 u32 scl0, scl1, pitch0;
Boris Brezillon0a038c12018-11-30 10:02:50 +0100495 u32 tiling;
Dave Stevensone065a8d2018-03-16 15:04:35 -0700496 u32 hvs_format = format->hvs;
Eric Anholtfc040232015-12-30 12:25:44 -0800497 int ret, i;
Eric Anholt5c679992015-12-28 14:34:44 -0800498
Boris Brezillon8d938442018-11-30 10:02:51 +0100499 if (vc4_state->dlist_initialized)
500 return 0;
501
Eric Anholt5c679992015-12-28 14:34:44 -0800502 ret = vc4_plane_setup_clipping_and_scaling(state);
503 if (ret)
504 return ret;
505
Eric Anholtfc040232015-12-30 12:25:44 -0800506 /* SCL1 is used for Cb/Cr scaling of planar formats. For RGB
507 * and 4:4:4, scl1 should be set to scl0 so both channels of
508 * the scaler do the same thing. For YUV, the Y plane needs
509 * to be put in channel 1 and Cb/Cr in channel 0, so we swap
510 * the scl fields here.
511 */
512 if (num_planes == 1) {
Boris Brezillon9a0e9802018-05-07 14:13:03 +0200513 scl0 = vc4_get_scl_field(state, 0);
Eric Anholtfc040232015-12-30 12:25:44 -0800514 scl1 = scl0;
515 } else {
516 scl0 = vc4_get_scl_field(state, 1);
517 scl1 = vc4_get_scl_field(state, 0);
518 }
Eric Anholt21af94c2015-10-20 16:06:57 +0100519
Boris Brezillona65511b12018-08-03 11:22:30 +0200520 h_subsample = drm_format_horz_chroma_subsampling(format->drm);
521 v_subsample = drm_format_vert_chroma_subsampling(format->drm);
522
Dave Stevensone065a8d2018-03-16 15:04:35 -0700523 switch (base_format_mod) {
Eric Anholt98830d912017-06-07 17:13:35 -0700524 case DRM_FORMAT_MOD_LINEAR:
525 tiling = SCALER_CTL0_TILING_LINEAR;
526 pitch0 = VC4_SET_FIELD(fb->pitches[0], SCALER_SRC_PITCH);
Boris Brezillona65511b12018-08-03 11:22:30 +0200527
528 /* Adjust the base pointer to the first pixel to be scanned
529 * out.
530 */
531 for (i = 0; i < num_planes; i++) {
532 vc4_state->offsets[i] += vc4_state->src_y /
533 (i ? v_subsample : 1) *
534 fb->pitches[i];
535 vc4_state->offsets[i] += vc4_state->src_x /
536 (i ? h_subsample : 1) *
537 fb->format->cpp[i];
538 }
Boris Brezillon3e407412018-08-03 11:22:31 +0200539
Eric Anholt98830d912017-06-07 17:13:35 -0700540 break;
Eric Anholt652badb2017-09-27 12:32:09 -0700541
542 case DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED: {
Eric Anholt652badb2017-09-27 12:32:09 -0700543 u32 tile_size_shift = 12; /* T tiles are 4kb */
Boris Brezillon3e407412018-08-03 11:22:31 +0200544 /* Whole-tile offsets, mostly for setting the pitch. */
545 u32 tile_w_shift = fb->format->cpp[0] == 2 ? 6 : 5;
Eric Anholt652badb2017-09-27 12:32:09 -0700546 u32 tile_h_shift = 5; /* 16 and 32bpp are 32 pixels high */
Boris Brezillon3e407412018-08-03 11:22:31 +0200547 u32 tile_w_mask = (1 << tile_w_shift) - 1;
548 /* The height mask on 32-bit-per-pixel tiles is 63, i.e. twice
549 * the height (in pixels) of a 4k tile.
550 */
551 u32 tile_h_mask = (2 << tile_h_shift) - 1;
552 /* For T-tiled, the FB pitch is "how many bytes from one row to
553 * the next, such that
554 *
555 * pitch * tile_h == tile_size * tiles_per_row
556 */
Eric Anholt652badb2017-09-27 12:32:09 -0700557 u32 tiles_w = fb->pitches[0] >> (tile_size_shift - tile_h_shift);
Boris Brezillon3e407412018-08-03 11:22:31 +0200558 u32 tiles_l = vc4_state->src_x >> tile_w_shift;
559 u32 tiles_r = tiles_w - tiles_l;
560 u32 tiles_t = vc4_state->src_y >> tile_h_shift;
561 /* Intra-tile offsets, which modify the base address (the
562 * SCALER_PITCH0_TILE_Y_OFFSET tells HVS how to walk from that
563 * base address).
564 */
565 u32 tile_y = (vc4_state->src_y >> 4) & 1;
566 u32 subtile_y = (vc4_state->src_y >> 2) & 3;
567 u32 utile_y = vc4_state->src_y & 3;
568 u32 x_off = vc4_state->src_x & tile_w_mask;
569 u32 y_off = vc4_state->src_y & tile_h_mask;
Eric Anholt652badb2017-09-27 12:32:09 -0700570
Eric Anholt98830d912017-06-07 17:13:35 -0700571 tiling = SCALER_CTL0_TILING_256B_OR_T;
Boris Brezillon3e407412018-08-03 11:22:31 +0200572 pitch0 = (VC4_SET_FIELD(x_off, SCALER_PITCH0_SINK_PIX) |
573 VC4_SET_FIELD(y_off, SCALER_PITCH0_TILE_Y_OFFSET) |
574 VC4_SET_FIELD(tiles_l, SCALER_PITCH0_TILE_WIDTH_L) |
575 VC4_SET_FIELD(tiles_r, SCALER_PITCH0_TILE_WIDTH_R));
576 vc4_state->offsets[0] += tiles_t * (tiles_w << tile_size_shift);
577 vc4_state->offsets[0] += subtile_y << 8;
578 vc4_state->offsets[0] += utile_y << 4;
Eric Anholt98830d912017-06-07 17:13:35 -0700579
Boris Brezillon3e407412018-08-03 11:22:31 +0200580 /* Rows of tiles alternate left-to-right and right-to-left. */
581 if (tiles_t & 1) {
582 pitch0 |= SCALER_PITCH0_TILE_INITIAL_LINE_DIR;
583 vc4_state->offsets[0] += (tiles_w - tiles_l) <<
584 tile_size_shift;
585 vc4_state->offsets[0] -= (1 + !tile_y) << 10;
586 } else {
587 vc4_state->offsets[0] += tiles_l << tile_size_shift;
588 vc4_state->offsets[0] += tile_y << 10;
589 }
590
Eric Anholt98830d912017-06-07 17:13:35 -0700591 break;
Eric Anholt652badb2017-09-27 12:32:09 -0700592 }
593
Dave Stevensone065a8d2018-03-16 15:04:35 -0700594 case DRM_FORMAT_MOD_BROADCOM_SAND64:
595 case DRM_FORMAT_MOD_BROADCOM_SAND128:
596 case DRM_FORMAT_MOD_BROADCOM_SAND256: {
597 uint32_t param = fourcc_mod_broadcom_param(fb->modifier);
598
599 /* Column-based NV12 or RGBA.
600 */
601 if (fb->format->num_planes > 1) {
602 if (hvs_format != HVS_PIXEL_FORMAT_YCBCR_YUV420_2PLANE) {
603 DRM_DEBUG_KMS("SAND format only valid for NV12/21");
604 return -EINVAL;
605 }
606 hvs_format = HVS_PIXEL_FORMAT_H264;
607 } else {
608 if (base_format_mod == DRM_FORMAT_MOD_BROADCOM_SAND256) {
609 DRM_DEBUG_KMS("SAND256 format only valid for H.264");
610 return -EINVAL;
611 }
612 }
613
614 switch (base_format_mod) {
615 case DRM_FORMAT_MOD_BROADCOM_SAND64:
616 tiling = SCALER_CTL0_TILING_64B;
617 break;
618 case DRM_FORMAT_MOD_BROADCOM_SAND128:
619 tiling = SCALER_CTL0_TILING_128B;
620 break;
621 case DRM_FORMAT_MOD_BROADCOM_SAND256:
622 tiling = SCALER_CTL0_TILING_256B_OR_T;
623 break;
624 default:
625 break;
626 }
627
628 if (param > SCALER_TILE_HEIGHT_MASK) {
629 DRM_DEBUG_KMS("SAND height too large (%d)\n", param);
630 return -EINVAL;
631 }
632
633 pitch0 = VC4_SET_FIELD(param, SCALER_TILE_HEIGHT);
634 break;
635 }
636
Eric Anholt98830d912017-06-07 17:13:35 -0700637 default:
638 DRM_DEBUG_KMS("Unsupported FB tiling flag 0x%16llx",
639 (long long)fb->modifier);
640 return -EINVAL;
641 }
642
Eric Anholt21af94c2015-10-20 16:06:57 +0100643 /* Control word */
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800644 vc4_dlist_write(vc4_state,
645 SCALER_CTL0_VALID |
Maxime Ripard3257ec72018-05-17 15:37:59 +0200646 VC4_SET_FIELD(SCALER_CTL0_RGBA_EXPAND_ROUND, SCALER_CTL0_RGBA_EXPAND) |
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800647 (format->pixel_order << SCALER_CTL0_ORDER_SHIFT) |
Dave Stevensone065a8d2018-03-16 15:04:35 -0700648 (hvs_format << SCALER_CTL0_PIXEL_FORMAT_SHIFT) |
Eric Anholt98830d912017-06-07 17:13:35 -0700649 VC4_SET_FIELD(tiling, SCALER_CTL0_TILING) |
Eric Anholt21af94c2015-10-20 16:06:57 +0100650 (vc4_state->is_unity ? SCALER_CTL0_UNITY : 0) |
Eric Anholtfc040232015-12-30 12:25:44 -0800651 VC4_SET_FIELD(scl0, SCALER_CTL0_SCL0) |
652 VC4_SET_FIELD(scl1, SCALER_CTL0_SCL1));
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800653
654 /* Position Word 0: Image Positions and Alpha Value */
Eric Anholt6674a9042015-12-30 11:50:22 -0800655 vc4_state->pos0_offset = vc4_state->dlist_count;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800656 vc4_dlist_write(vc4_state,
Stefan Schake22445f02018-04-20 17:09:54 -0700657 VC4_SET_FIELD(state->alpha >> 8, SCALER_POS0_FIXED_ALPHA) |
Eric Anholt5c679992015-12-28 14:34:44 -0800658 VC4_SET_FIELD(vc4_state->crtc_x, SCALER_POS0_START_X) |
659 VC4_SET_FIELD(vc4_state->crtc_y, SCALER_POS0_START_Y));
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800660
Eric Anholt21af94c2015-10-20 16:06:57 +0100661 /* Position Word 1: Scaled Image Dimensions. */
662 if (!vc4_state->is_unity) {
663 vc4_dlist_write(vc4_state,
664 VC4_SET_FIELD(vc4_state->crtc_w,
665 SCALER_POS1_SCL_WIDTH) |
666 VC4_SET_FIELD(vc4_state->crtc_h,
667 SCALER_POS1_SCL_HEIGHT));
668 }
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800669
Stefan Schake22445f02018-04-20 17:09:54 -0700670 /* Don't waste cycles mixing with plane alpha if the set alpha
671 * is opaque or there is no per-pixel alpha information.
672 * In any case we use the alpha property value as the fixed alpha.
673 */
674 mix_plane_alpha = state->alpha != DRM_BLEND_ALPHA_OPAQUE &&
675 fb->format->has_alpha;
676
Stefan Schake05202c22018-03-09 01:53:34 +0100677 /* Position Word 2: Source Image Size, Alpha */
Eric Anholt6674a9042015-12-30 11:50:22 -0800678 vc4_state->pos2_offset = vc4_state->dlist_count;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800679 vc4_dlist_write(vc4_state,
Maxime Ripard124e5da2017-12-22 15:31:27 +0100680 VC4_SET_FIELD(fb->format->has_alpha ?
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800681 SCALER_POS2_ALPHA_MODE_PIPELINE :
682 SCALER_POS2_ALPHA_MODE_FIXED,
683 SCALER_POS2_ALPHA_MODE) |
Stefan Schake22445f02018-04-20 17:09:54 -0700684 (mix_plane_alpha ? SCALER_POS2_ALPHA_MIX : 0) |
Stefan Schake05202c22018-03-09 01:53:34 +0100685 (fb->format->has_alpha ? SCALER_POS2_ALPHA_PREMULT : 0) |
Eric Anholtfc040232015-12-30 12:25:44 -0800686 VC4_SET_FIELD(vc4_state->src_w[0], SCALER_POS2_WIDTH) |
687 VC4_SET_FIELD(vc4_state->src_h[0], SCALER_POS2_HEIGHT));
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800688
689 /* Position Word 3: Context. Written by the HVS. */
690 vc4_dlist_write(vc4_state, 0xc0c0c0c0);
691
Eric Anholtfc040232015-12-30 12:25:44 -0800692
693 /* Pointer Word 0/1/2: RGB / Y / Cb / Cr Pointers
694 *
695 * The pointers may be any byte address.
696 */
Eric Anholt6674a9042015-12-30 11:50:22 -0800697 vc4_state->ptr0_offset = vc4_state->dlist_count;
Dave Stevenson090cb0c2017-11-16 14:22:30 +0000698 for (i = 0; i < num_planes; i++)
699 vc4_dlist_write(vc4_state, vc4_state->offsets[i]);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800700
Eric Anholtfc040232015-12-30 12:25:44 -0800701 /* Pointer Context Word 0/1/2: Written by the HVS */
702 for (i = 0; i < num_planes; i++)
703 vc4_dlist_write(vc4_state, 0xc0c0c0c0);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800704
Eric Anholt98830d912017-06-07 17:13:35 -0700705 /* Pitch word 0 */
706 vc4_dlist_write(vc4_state, pitch0);
707
708 /* Pitch word 1/2 */
709 for (i = 1; i < num_planes; i++) {
Dave Stevensone065a8d2018-03-16 15:04:35 -0700710 if (hvs_format != HVS_PIXEL_FORMAT_H264) {
711 vc4_dlist_write(vc4_state,
712 VC4_SET_FIELD(fb->pitches[i],
713 SCALER_SRC_PITCH));
714 } else {
715 vc4_dlist_write(vc4_state, pitch0);
716 }
Eric Anholtfc040232015-12-30 12:25:44 -0800717 }
718
719 /* Colorspace conversion words */
720 if (vc4_state->is_yuv) {
721 vc4_dlist_write(vc4_state, SCALER_CSC0_ITR_R_601_5);
722 vc4_dlist_write(vc4_state, SCALER_CSC1_ITR_R_601_5);
723 vc4_dlist_write(vc4_state, SCALER_CSC2_ITR_R_601_5);
724 }
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800725
Boris Brezillon0a038c12018-11-30 10:02:50 +0100726 vc4_state->lbm_offset = 0;
727
Boris Brezillon658d8cb2018-07-25 14:29:07 +0200728 if (vc4_state->x_scaling[0] != VC4_SCALING_NONE ||
729 vc4_state->x_scaling[1] != VC4_SCALING_NONE ||
730 vc4_state->y_scaling[0] != VC4_SCALING_NONE ||
731 vc4_state->y_scaling[1] != VC4_SCALING_NONE) {
Boris Brezillon0a038c12018-11-30 10:02:50 +0100732 /* Reserve a slot for the LBM Base Address. The real value will
733 * be set when calling vc4_plane_allocate_lbm().
734 */
Eric Anholtfc040232015-12-30 12:25:44 -0800735 if (vc4_state->y_scaling[0] != VC4_SCALING_NONE ||
Boris Brezillon0a038c12018-11-30 10:02:50 +0100736 vc4_state->y_scaling[1] != VC4_SCALING_NONE)
737 vc4_state->lbm_offset = vc4_state->dlist_count++;
Eric Anholt21af94c2015-10-20 16:06:57 +0100738
Eric Anholtfc040232015-12-30 12:25:44 -0800739 if (num_planes > 1) {
740 /* Emit Cb/Cr as channel 0 and Y as channel
741 * 1. This matches how we set up scl0/scl1
742 * above.
743 */
744 vc4_write_scaling_parameters(state, 1);
745 }
746 vc4_write_scaling_parameters(state, 0);
Eric Anholt21af94c2015-10-20 16:06:57 +0100747
748 /* If any PPF setup was done, then all the kernel
749 * pointers get uploaded.
750 */
Eric Anholtfc040232015-12-30 12:25:44 -0800751 if (vc4_state->x_scaling[0] == VC4_SCALING_PPF ||
752 vc4_state->y_scaling[0] == VC4_SCALING_PPF ||
753 vc4_state->x_scaling[1] == VC4_SCALING_PPF ||
754 vc4_state->y_scaling[1] == VC4_SCALING_PPF) {
Eric Anholt21af94c2015-10-20 16:06:57 +0100755 u32 kernel = VC4_SET_FIELD(vc4->hvs->mitchell_netravali_filter.start,
756 SCALER_PPF_KERNEL_OFFSET);
757
758 /* HPPF plane 0 */
759 vc4_dlist_write(vc4_state, kernel);
760 /* VPPF plane 0 */
761 vc4_dlist_write(vc4_state, kernel);
762 /* HPPF plane 1 */
763 vc4_dlist_write(vc4_state, kernel);
764 /* VPPF plane 1 */
765 vc4_dlist_write(vc4_state, kernel);
766 }
767 }
768
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800769 vc4_state->dlist[ctl0_offset] |=
770 VC4_SET_FIELD(vc4_state->dlist_count, SCALER_CTL0_SIZE);
771
Stefan Schake3d67b682018-03-09 01:53:35 +0100772 /* crtc_* are already clipped coordinates. */
773 covers_screen = vc4_state->crtc_x == 0 && vc4_state->crtc_y == 0 &&
774 vc4_state->crtc_w == state->crtc->mode.hdisplay &&
775 vc4_state->crtc_h == state->crtc->mode.vdisplay;
776 /* Background fill might be necessary when the plane has per-pixel
Stefan Schake22445f02018-04-20 17:09:54 -0700777 * alpha content or a non-opaque plane alpha and could blend from the
778 * background or does not cover the entire screen.
Stefan Schake3d67b682018-03-09 01:53:35 +0100779 */
Stefan Schake22445f02018-04-20 17:09:54 -0700780 vc4_state->needs_bg_fill = fb->format->has_alpha || !covers_screen ||
781 state->alpha != DRM_BLEND_ALPHA_OPAQUE;
Stefan Schake3d67b682018-03-09 01:53:35 +0100782
Boris Brezillon8d938442018-11-30 10:02:51 +0100783 /* Flag the dlist as initialized to avoid checking it twice in case
784 * the async update check already called vc4_plane_mode_set() and
785 * decided to fallback to sync update because async update was not
786 * possible.
787 */
788 vc4_state->dlist_initialized = 1;
789
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800790 return 0;
791}
792
793/* If a modeset involves changing the setup of a plane, the atomic
794 * infrastructure will call this to validate a proposed plane setup.
795 * However, if a plane isn't getting updated, this (and the
796 * corresponding vc4_plane_atomic_update) won't get called. Thus, we
797 * compute the dlist here and have all active plane dlists get updated
798 * in the CRTC's flush.
799 */
800static int vc4_plane_atomic_check(struct drm_plane *plane,
801 struct drm_plane_state *state)
802{
803 struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
Boris Brezillon0a038c12018-11-30 10:02:50 +0100804 int ret;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800805
806 vc4_state->dlist_count = 0;
807
Boris Brezillon0a038c12018-11-30 10:02:50 +0100808 if (!plane_enabled(state))
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800809 return 0;
Boris Brezillon0a038c12018-11-30 10:02:50 +0100810
811 ret = vc4_plane_mode_set(plane, state);
812 if (ret)
813 return ret;
814
815 return vc4_plane_allocate_lbm(state);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800816}
817
818static void vc4_plane_atomic_update(struct drm_plane *plane,
819 struct drm_plane_state *old_state)
820{
821 /* No contents here. Since we don't know where in the CRTC's
822 * dlist we should be stored, our dlist is uploaded to the
823 * hardware with vc4_plane_write_dlist() at CRTC atomic_flush
824 * time.
825 */
826}
827
828u32 vc4_plane_write_dlist(struct drm_plane *plane, u32 __iomem *dlist)
829{
830 struct vc4_plane_state *vc4_state = to_vc4_plane_state(plane->state);
831 int i;
832
Eric Anholtb501bac2015-11-30 12:34:01 -0800833 vc4_state->hw_dlist = dlist;
834
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800835 /* Can't memcpy_toio() because it needs to be 32-bit writes. */
836 for (i = 0; i < vc4_state->dlist_count; i++)
837 writel(vc4_state->dlist[i], &dlist[i]);
838
839 return vc4_state->dlist_count;
840}
841
Daniel Vetter2f196b72016-06-02 16:21:44 +0200842u32 vc4_plane_dlist_size(const struct drm_plane_state *state)
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800843{
Daniel Vetter2f196b72016-06-02 16:21:44 +0200844 const struct vc4_plane_state *vc4_state =
845 container_of(state, typeof(*vc4_state), base);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800846
847 return vc4_state->dlist_count;
848}
849
Eric Anholtb501bac2015-11-30 12:34:01 -0800850/* Updates the plane to immediately (well, once the FIFO needs
851 * refilling) scan out from at a new framebuffer.
852 */
853void vc4_plane_async_set_fb(struct drm_plane *plane, struct drm_framebuffer *fb)
854{
855 struct vc4_plane_state *vc4_state = to_vc4_plane_state(plane->state);
856 struct drm_gem_cma_object *bo = drm_fb_cma_get_gem_obj(fb, 0);
857 uint32_t addr;
858
859 /* We're skipping the address adjustment for negative origin,
860 * because this is only called on the primary plane.
861 */
862 WARN_ON_ONCE(plane->state->crtc_x < 0 || plane->state->crtc_y < 0);
863 addr = bo->paddr + fb->offsets[0];
864
865 /* Write the new address into the hardware immediately. The
866 * scanout will start from this address as soon as the FIFO
867 * needs to refill with pixels.
868 */
Eric Anholt6674a9042015-12-30 11:50:22 -0800869 writel(addr, &vc4_state->hw_dlist[vc4_state->ptr0_offset]);
Eric Anholtb501bac2015-11-30 12:34:01 -0800870
871 /* Also update the CPU-side dlist copy, so that any later
872 * atomic updates that don't do a new modeset on our plane
873 * also use our updated address.
874 */
Eric Anholt6674a9042015-12-30 11:50:22 -0800875 vc4_state->dlist[vc4_state->ptr0_offset] = addr;
Eric Anholtb501bac2015-11-30 12:34:01 -0800876}
877
Gustavo Padovan539c3202018-03-30 10:54:45 +0200878static void vc4_plane_atomic_async_update(struct drm_plane *plane,
879 struct drm_plane_state *state)
880{
Boris Brezillon5a439112018-11-15 11:58:51 +0100881 struct vc4_plane_state *vc4_state, *new_vc4_state;
Gustavo Padovan539c3202018-03-30 10:54:45 +0200882
Boris Brezillon1d4118c2018-11-30 10:02:52 +0100883 drm_atomic_set_fb_for_plane(plane->state, state->fb);
Gustavo Padovan539c3202018-03-30 10:54:45 +0200884 plane->state->crtc_x = state->crtc_x;
885 plane->state->crtc_y = state->crtc_y;
Boris Brezillon1d4118c2018-11-30 10:02:52 +0100886 plane->state->crtc_w = state->crtc_w;
887 plane->state->crtc_h = state->crtc_h;
Gustavo Padovan539c3202018-03-30 10:54:45 +0200888 plane->state->src_x = state->src_x;
889 plane->state->src_y = state->src_y;
Boris Brezillon1d4118c2018-11-30 10:02:52 +0100890 plane->state->src_w = state->src_w;
891 plane->state->src_h = state->src_h;
892 plane->state->src_h = state->src_h;
893 plane->state->alpha = state->alpha;
894 plane->state->pixel_blend_mode = state->pixel_blend_mode;
895 plane->state->rotation = state->rotation;
896 plane->state->zpos = state->zpos;
897 plane->state->normalized_zpos = state->normalized_zpos;
898 plane->state->color_encoding = state->color_encoding;
899 plane->state->color_range = state->color_range;
900 plane->state->src = state->src;
901 plane->state->dst = state->dst;
902 plane->state->visible = state->visible;
Boris Brezillon5a439112018-11-15 11:58:51 +0100903
904 new_vc4_state = to_vc4_plane_state(state);
905 vc4_state = to_vc4_plane_state(plane->state);
906
Boris Brezillon1d4118c2018-11-30 10:02:52 +0100907 vc4_state->crtc_x = new_vc4_state->crtc_x;
908 vc4_state->crtc_y = new_vc4_state->crtc_y;
909 vc4_state->crtc_h = new_vc4_state->crtc_h;
910 vc4_state->crtc_w = new_vc4_state->crtc_w;
911 vc4_state->src_x = new_vc4_state->src_x;
912 vc4_state->src_y = new_vc4_state->src_y;
913 memcpy(vc4_state->src_w, new_vc4_state->src_w,
914 sizeof(vc4_state->src_w));
915 memcpy(vc4_state->src_h, new_vc4_state->src_h,
916 sizeof(vc4_state->src_h));
917 memcpy(vc4_state->x_scaling, new_vc4_state->x_scaling,
918 sizeof(vc4_state->x_scaling));
919 memcpy(vc4_state->y_scaling, new_vc4_state->y_scaling,
920 sizeof(vc4_state->y_scaling));
921 vc4_state->is_unity = new_vc4_state->is_unity;
922 vc4_state->is_yuv = new_vc4_state->is_yuv;
923 memcpy(vc4_state->offsets, new_vc4_state->offsets,
924 sizeof(vc4_state->offsets));
925 vc4_state->needs_bg_fill = new_vc4_state->needs_bg_fill;
926
Boris Brezillon5a439112018-11-15 11:58:51 +0100927 /* Update the current vc4_state pos0, pos2 and ptr0 dlist entries. */
928 vc4_state->dlist[vc4_state->pos0_offset] =
929 new_vc4_state->dlist[vc4_state->pos0_offset];
930 vc4_state->dlist[vc4_state->pos2_offset] =
931 new_vc4_state->dlist[vc4_state->pos2_offset];
932 vc4_state->dlist[vc4_state->ptr0_offset] =
933 new_vc4_state->dlist[vc4_state->ptr0_offset];
Gustavo Padovan539c3202018-03-30 10:54:45 +0200934
935 /* Note that we can't just call vc4_plane_write_dlist()
936 * because that would smash the context data that the HVS is
937 * currently using.
938 */
939 writel(vc4_state->dlist[vc4_state->pos0_offset],
940 &vc4_state->hw_dlist[vc4_state->pos0_offset]);
941 writel(vc4_state->dlist[vc4_state->pos2_offset],
942 &vc4_state->hw_dlist[vc4_state->pos2_offset]);
943 writel(vc4_state->dlist[vc4_state->ptr0_offset],
944 &vc4_state->hw_dlist[vc4_state->ptr0_offset]);
945}
946
947static int vc4_plane_atomic_async_check(struct drm_plane *plane,
948 struct drm_plane_state *state)
949{
Boris Brezillon1d4118c2018-11-30 10:02:52 +0100950 struct vc4_plane_state *old_vc4_state, *new_vc4_state;
951 int ret;
952 u32 i;
953
954 ret = vc4_plane_mode_set(plane, state);
955 if (ret)
956 return ret;
957
958 old_vc4_state = to_vc4_plane_state(plane->state);
959 new_vc4_state = to_vc4_plane_state(state);
960 if (old_vc4_state->dlist_count != new_vc4_state->dlist_count ||
961 old_vc4_state->pos0_offset != new_vc4_state->pos0_offset ||
962 old_vc4_state->pos2_offset != new_vc4_state->pos2_offset ||
963 old_vc4_state->ptr0_offset != new_vc4_state->ptr0_offset ||
964 vc4_lbm_size(plane->state) != vc4_lbm_size(state))
Gustavo Padovan539c3202018-03-30 10:54:45 +0200965 return -EINVAL;
966
Boris Brezillon1d4118c2018-11-30 10:02:52 +0100967 /* Only pos0, pos2 and ptr0 DWORDS can be updated in an async update
968 * if anything else has changed, fallback to a sync update.
969 */
970 for (i = 0; i < new_vc4_state->dlist_count; i++) {
971 if (i == new_vc4_state->pos0_offset ||
972 i == new_vc4_state->pos2_offset ||
973 i == new_vc4_state->ptr0_offset ||
974 (new_vc4_state->lbm_offset &&
975 i == new_vc4_state->lbm_offset))
976 continue;
977
978 if (new_vc4_state->dlist[i] != old_vc4_state->dlist[i])
979 return -EINVAL;
980 }
981
Gustavo Padovan539c3202018-03-30 10:54:45 +0200982 return 0;
983}
984
Eric Anholt334dbd62017-06-21 11:49:59 -0700985static int vc4_prepare_fb(struct drm_plane *plane,
986 struct drm_plane_state *state)
987{
988 struct vc4_bo *bo;
989 struct dma_fence *fence;
Boris Brezillonb9f19252017-10-19 14:57:48 +0200990 int ret;
Eric Anholt334dbd62017-06-21 11:49:59 -0700991
Daniel Vetter2227a7a2018-04-05 17:44:48 +0200992 if (!state->fb)
Eric Anholt334dbd62017-06-21 11:49:59 -0700993 return 0;
994
995 bo = to_vc4_bo(&drm_fb_cma_get_gem_obj(state->fb, 0)->base);
Boris Brezillonb9f19252017-10-19 14:57:48 +0200996
Daniel Vetter2227a7a2018-04-05 17:44:48 +0200997 fence = reservation_object_get_excl_rcu(bo->resv);
998 drm_atomic_set_fence_for_plane(state, fence);
999
1000 if (plane->state->fb == state->fb)
1001 return 0;
1002
Boris Brezillonb9f19252017-10-19 14:57:48 +02001003 ret = vc4_bo_inc_usecnt(bo);
1004 if (ret)
1005 return ret;
1006
Eric Anholt334dbd62017-06-21 11:49:59 -07001007 return 0;
1008}
1009
Boris Brezillonb9f19252017-10-19 14:57:48 +02001010static void vc4_cleanup_fb(struct drm_plane *plane,
1011 struct drm_plane_state *state)
1012{
1013 struct vc4_bo *bo;
1014
1015 if (plane->state->fb == state->fb || !state->fb)
1016 return;
1017
1018 bo = to_vc4_bo(&drm_fb_cma_get_gem_obj(state->fb, 0)->base);
1019 vc4_bo_dec_usecnt(bo);
1020}
1021
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001022static const struct drm_plane_helper_funcs vc4_plane_helper_funcs = {
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001023 .atomic_check = vc4_plane_atomic_check,
1024 .atomic_update = vc4_plane_atomic_update,
Eric Anholt334dbd62017-06-21 11:49:59 -07001025 .prepare_fb = vc4_prepare_fb,
Boris Brezillonb9f19252017-10-19 14:57:48 +02001026 .cleanup_fb = vc4_cleanup_fb,
Gustavo Padovan539c3202018-03-30 10:54:45 +02001027 .atomic_async_check = vc4_plane_atomic_async_check,
1028 .atomic_async_update = vc4_plane_atomic_async_update,
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001029};
1030
1031static void vc4_plane_destroy(struct drm_plane *plane)
1032{
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001033 drm_plane_cleanup(plane);
1034}
1035
Daniel Stone423ad7b2017-08-08 17:44:48 +01001036static bool vc4_format_mod_supported(struct drm_plane *plane,
1037 uint32_t format,
1038 uint64_t modifier)
1039{
1040 /* Support T_TILING for RGB formats only. */
1041 switch (format) {
1042 case DRM_FORMAT_XRGB8888:
1043 case DRM_FORMAT_ARGB8888:
1044 case DRM_FORMAT_ABGR8888:
1045 case DRM_FORMAT_XBGR8888:
1046 case DRM_FORMAT_RGB565:
1047 case DRM_FORMAT_BGR565:
1048 case DRM_FORMAT_ARGB1555:
1049 case DRM_FORMAT_XRGB1555:
Dave Stevensone065a8d2018-03-16 15:04:35 -07001050 switch (fourcc_mod_broadcom_mod(modifier)) {
1051 case DRM_FORMAT_MOD_LINEAR:
1052 case DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED:
1053 case DRM_FORMAT_MOD_BROADCOM_SAND64:
1054 case DRM_FORMAT_MOD_BROADCOM_SAND128:
1055 return true;
1056 default:
1057 return false;
1058 }
1059 case DRM_FORMAT_NV12:
1060 case DRM_FORMAT_NV21:
1061 switch (fourcc_mod_broadcom_mod(modifier)) {
1062 case DRM_FORMAT_MOD_LINEAR:
1063 case DRM_FORMAT_MOD_BROADCOM_SAND64:
1064 case DRM_FORMAT_MOD_BROADCOM_SAND128:
1065 case DRM_FORMAT_MOD_BROADCOM_SAND256:
1066 return true;
1067 default:
1068 return false;
1069 }
Daniel Stone423ad7b2017-08-08 17:44:48 +01001070 case DRM_FORMAT_YUV422:
1071 case DRM_FORMAT_YVU422:
1072 case DRM_FORMAT_YUV420:
1073 case DRM_FORMAT_YVU420:
Daniel Stone423ad7b2017-08-08 17:44:48 +01001074 case DRM_FORMAT_NV16:
Eric Anholt1e871d62018-03-16 15:04:34 -07001075 case DRM_FORMAT_NV61:
Daniel Stone423ad7b2017-08-08 17:44:48 +01001076 default:
1077 return (modifier == DRM_FORMAT_MOD_LINEAR);
1078 }
1079}
1080
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001081static const struct drm_plane_funcs vc4_plane_funcs = {
Gustavo Padovan539c3202018-03-30 10:54:45 +02001082 .update_plane = drm_atomic_helper_update_plane,
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001083 .disable_plane = drm_atomic_helper_disable_plane,
1084 .destroy = vc4_plane_destroy,
1085 .set_property = NULL,
1086 .reset = vc4_plane_reset,
1087 .atomic_duplicate_state = vc4_plane_duplicate_state,
1088 .atomic_destroy_state = vc4_plane_destroy_state,
Daniel Stone423ad7b2017-08-08 17:44:48 +01001089 .format_mod_supported = vc4_format_mod_supported,
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001090};
1091
1092struct drm_plane *vc4_plane_init(struct drm_device *dev,
1093 enum drm_plane_type type)
1094{
1095 struct drm_plane *plane = NULL;
1096 struct vc4_plane *vc4_plane;
1097 u32 formats[ARRAY_SIZE(hvs_formats)];
1098 int ret = 0;
1099 unsigned i;
Daniel Stone423ad7b2017-08-08 17:44:48 +01001100 static const uint64_t modifiers[] = {
1101 DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED,
Dave Stevensone065a8d2018-03-16 15:04:35 -07001102 DRM_FORMAT_MOD_BROADCOM_SAND128,
1103 DRM_FORMAT_MOD_BROADCOM_SAND64,
1104 DRM_FORMAT_MOD_BROADCOM_SAND256,
Daniel Stone423ad7b2017-08-08 17:44:48 +01001105 DRM_FORMAT_MOD_LINEAR,
1106 DRM_FORMAT_MOD_INVALID
1107 };
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001108
1109 vc4_plane = devm_kzalloc(dev->dev, sizeof(*vc4_plane),
1110 GFP_KERNEL);
Colin Ian King7b347342017-03-16 18:54:18 +00001111 if (!vc4_plane)
1112 return ERR_PTR(-ENOMEM);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001113
Boris Brezillon2c2853f2018-11-30 10:02:54 +01001114 for (i = 0; i < ARRAY_SIZE(hvs_formats); i++)
1115 formats[i] = hvs_formats[i].drm;
1116
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001117 plane = &vc4_plane->base;
Andrzej Pietrasiewicz49d29a02017-02-01 10:35:08 +01001118 ret = drm_universal_plane_init(dev, plane, 0,
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001119 &vc4_plane_funcs,
Boris Brezillon2c2853f2018-11-30 10:02:54 +01001120 formats, ARRAY_SIZE(formats),
Daniel Stone423ad7b2017-08-08 17:44:48 +01001121 modifiers, type, NULL);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001122
1123 drm_plane_helper_add(plane, &vc4_plane_helper_funcs);
1124
Stefan Schake22445f02018-04-20 17:09:54 -07001125 drm_plane_create_alpha_property(plane);
1126
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001127 return plane;
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001128}