blob: 831e1959e64d35b7898671edc7cd153fd62329ea [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>
25
Boris Brezillonb9f19252017-10-19 14:57:48 +020026#include "uapi/drm/vc4_drm.h"
Eric Anholtc8b75bc2015-03-02 13:01:12 -080027#include "vc4_drv.h"
28#include "vc4_regs.h"
Eric Anholtc8b75bc2015-03-02 13:01:12 -080029
Eric Anholt21af94c2015-10-20 16:06:57 +010030enum vc4_scaling_mode {
31 VC4_SCALING_NONE,
32 VC4_SCALING_TPZ,
33 VC4_SCALING_PPF,
34};
35
Eric Anholtc8b75bc2015-03-02 13:01:12 -080036struct vc4_plane_state {
37 struct drm_plane_state base;
Eric Anholtf427fb12015-12-28 14:14:09 -080038 /* System memory copy of the display list for this element, computed
39 * at atomic_check time.
40 */
Eric Anholtc8b75bc2015-03-02 13:01:12 -080041 u32 *dlist;
Eric Anholtf427fb12015-12-28 14:14:09 -080042 u32 dlist_size; /* Number of dwords allocated for the display list */
Eric Anholtc8b75bc2015-03-02 13:01:12 -080043 u32 dlist_count; /* Number of used dwords in the display list. */
Eric Anholtb501bac2015-11-30 12:34:01 -080044
Eric Anholt6674a902015-12-30 11:50:22 -080045 /* Offset in the dlist to various words, for pageflip or
46 * cursor updates.
47 */
48 u32 pos0_offset;
49 u32 pos2_offset;
50 u32 ptr0_offset;
Eric Anholtb501bac2015-11-30 12:34:01 -080051
52 /* Offset where the plane's dlist was last stored in the
Eric Anholtf427fb12015-12-28 14:14:09 -080053 * hardware at vc4_crtc_atomic_flush() time.
54 */
Eric Anholt17eac752015-12-28 14:14:57 -080055 u32 __iomem *hw_dlist;
Eric Anholt5c679992015-12-28 14:34:44 -080056
57 /* Clipped coordinates of the plane on the display. */
58 int crtc_x, crtc_y, crtc_w, crtc_h;
Eric Anholt21af94c2015-10-20 16:06:57 +010059 /* Clipped area being scanned from in the FB. */
Eric Anholtfc040232015-12-30 12:25:44 -080060 u32 src_x, src_y;
Eric Anholt21af94c2015-10-20 16:06:57 +010061
Eric Anholtfc040232015-12-30 12:25:44 -080062 u32 src_w[2], src_h[2];
63
64 /* Scaling selection for the RGB/Y plane and the Cb/Cr planes. */
65 enum vc4_scaling_mode x_scaling[2], y_scaling[2];
Eric Anholt21af94c2015-10-20 16:06:57 +010066 bool is_unity;
Eric Anholtfc040232015-12-30 12:25:44 -080067 bool is_yuv;
Eric Anholt5c679992015-12-28 14:34:44 -080068
69 /* Offset to start scanning out from the start of the plane's
70 * BO.
71 */
Eric Anholtfc040232015-12-30 12:25:44 -080072 u32 offsets[3];
Eric Anholt21af94c2015-10-20 16:06:57 +010073
74 /* Our allocation in LBM for temporary storage during scaling. */
75 struct drm_mm_node lbm;
Eric Anholtc8b75bc2015-03-02 13:01:12 -080076};
77
78static inline struct vc4_plane_state *
79to_vc4_plane_state(struct drm_plane_state *state)
80{
81 return (struct vc4_plane_state *)state;
82}
83
84static const struct hvs_format {
85 u32 drm; /* DRM_FORMAT_* */
86 u32 hvs; /* HVS_FORMAT_* */
87 u32 pixel_order;
Eric Anholtc8b75bc2015-03-02 13:01:12 -080088} hvs_formats[] = {
89 {
90 .drm = DRM_FORMAT_XRGB8888, .hvs = HVS_PIXEL_FORMAT_RGBA8888,
Maxime Ripard124e5da2017-12-22 15:31:27 +010091 .pixel_order = HVS_PIXEL_ORDER_ABGR,
Eric Anholtc8b75bc2015-03-02 13:01:12 -080092 },
93 {
94 .drm = DRM_FORMAT_ARGB8888, .hvs = HVS_PIXEL_FORMAT_RGBA8888,
Maxime Ripard124e5da2017-12-22 15:31:27 +010095 .pixel_order = HVS_PIXEL_ORDER_ABGR,
Eric Anholtc8b75bc2015-03-02 13:01:12 -080096 },
Eric Anholtfe4cd842015-10-20 13:59:15 +010097 {
Rob Herring93977762016-06-09 16:19:25 -050098 .drm = DRM_FORMAT_ABGR8888, .hvs = HVS_PIXEL_FORMAT_RGBA8888,
Maxime Ripard124e5da2017-12-22 15:31:27 +010099 .pixel_order = HVS_PIXEL_ORDER_ARGB,
Rob Herring93977762016-06-09 16:19:25 -0500100 },
101 {
102 .drm = DRM_FORMAT_XBGR8888, .hvs = HVS_PIXEL_FORMAT_RGBA8888,
Maxime Ripard124e5da2017-12-22 15:31:27 +0100103 .pixel_order = HVS_PIXEL_ORDER_ARGB,
Rob Herring93977762016-06-09 16:19:25 -0500104 },
105 {
Eric Anholtfe4cd842015-10-20 13:59:15 +0100106 .drm = DRM_FORMAT_RGB565, .hvs = HVS_PIXEL_FORMAT_RGB565,
Maxime Ripard124e5da2017-12-22 15:31:27 +0100107 .pixel_order = HVS_PIXEL_ORDER_XRGB,
Eric Anholtfe4cd842015-10-20 13:59:15 +0100108 },
109 {
110 .drm = DRM_FORMAT_BGR565, .hvs = HVS_PIXEL_FORMAT_RGB565,
Maxime Ripard124e5da2017-12-22 15:31:27 +0100111 .pixel_order = HVS_PIXEL_ORDER_XBGR,
Eric Anholtfe4cd842015-10-20 13:59:15 +0100112 },
113 {
114 .drm = DRM_FORMAT_ARGB1555, .hvs = HVS_PIXEL_FORMAT_RGBA5551,
Maxime Ripard124e5da2017-12-22 15:31:27 +0100115 .pixel_order = HVS_PIXEL_ORDER_ABGR,
Eric Anholtfe4cd842015-10-20 13:59:15 +0100116 },
117 {
118 .drm = DRM_FORMAT_XRGB1555, .hvs = HVS_PIXEL_FORMAT_RGBA5551,
Maxime Ripard124e5da2017-12-22 15:31:27 +0100119 .pixel_order = HVS_PIXEL_ORDER_ABGR,
Eric Anholtfe4cd842015-10-20 13:59:15 +0100120 },
Eric Anholtfc040232015-12-30 12:25:44 -0800121 {
Dave Stevenson88f81562017-11-16 14:22:29 +0000122 .drm = DRM_FORMAT_RGB888, .hvs = HVS_PIXEL_FORMAT_RGB888,
Maxime Ripard124e5da2017-12-22 15:31:27 +0100123 .pixel_order = HVS_PIXEL_ORDER_XRGB,
Dave Stevenson88f81562017-11-16 14:22:29 +0000124 },
125 {
126 .drm = DRM_FORMAT_BGR888, .hvs = HVS_PIXEL_FORMAT_RGB888,
Maxime Ripard124e5da2017-12-22 15:31:27 +0100127 .pixel_order = HVS_PIXEL_ORDER_XBGR,
Dave Stevenson88f81562017-11-16 14:22:29 +0000128 },
129 {
Eric Anholtfc040232015-12-30 12:25:44 -0800130 .drm = DRM_FORMAT_YUV422,
131 .hvs = HVS_PIXEL_FORMAT_YCBCR_YUV422_3PLANE,
Dave Stevenson090cb0c2017-11-16 14:22:30 +0000132 .pixel_order = HVS_PIXEL_ORDER_XYCBCR,
Eric Anholtfc040232015-12-30 12:25:44 -0800133 },
134 {
135 .drm = DRM_FORMAT_YVU422,
136 .hvs = HVS_PIXEL_FORMAT_YCBCR_YUV422_3PLANE,
Dave Stevenson090cb0c2017-11-16 14:22:30 +0000137 .pixel_order = HVS_PIXEL_ORDER_XYCRCB,
Eric Anholtfc040232015-12-30 12:25:44 -0800138 },
139 {
140 .drm = DRM_FORMAT_YUV420,
141 .hvs = HVS_PIXEL_FORMAT_YCBCR_YUV420_3PLANE,
Dave Stevenson090cb0c2017-11-16 14:22:30 +0000142 .pixel_order = HVS_PIXEL_ORDER_XYCBCR,
Eric Anholtfc040232015-12-30 12:25:44 -0800143 },
144 {
145 .drm = DRM_FORMAT_YVU420,
146 .hvs = HVS_PIXEL_FORMAT_YCBCR_YUV420_3PLANE,
Dave Stevenson090cb0c2017-11-16 14:22:30 +0000147 .pixel_order = HVS_PIXEL_ORDER_XYCRCB,
Eric Anholtfc040232015-12-30 12:25:44 -0800148 },
149 {
150 .drm = DRM_FORMAT_NV12,
151 .hvs = HVS_PIXEL_FORMAT_YCBCR_YUV420_2PLANE,
Dave Stevenson090cb0c2017-11-16 14:22:30 +0000152 .pixel_order = HVS_PIXEL_ORDER_XYCBCR,
Eric Anholtfc040232015-12-30 12:25:44 -0800153 },
154 {
Dave Stevensoncb20dd12017-11-16 14:22:31 +0000155 .drm = DRM_FORMAT_NV21,
156 .hvs = HVS_PIXEL_FORMAT_YCBCR_YUV420_2PLANE,
157 .pixel_order = HVS_PIXEL_ORDER_XYCRCB,
158 },
159 {
Eric Anholtfc040232015-12-30 12:25:44 -0800160 .drm = DRM_FORMAT_NV16,
161 .hvs = HVS_PIXEL_FORMAT_YCBCR_YUV422_2PLANE,
Dave Stevenson090cb0c2017-11-16 14:22:30 +0000162 .pixel_order = HVS_PIXEL_ORDER_XYCBCR,
Eric Anholtfc040232015-12-30 12:25:44 -0800163 },
Dave Stevensoncb20dd12017-11-16 14:22:31 +0000164 {
165 .drm = DRM_FORMAT_NV61,
166 .hvs = HVS_PIXEL_FORMAT_YCBCR_YUV422_2PLANE,
167 .pixel_order = HVS_PIXEL_ORDER_XYCRCB,
168 },
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800169};
170
171static const struct hvs_format *vc4_get_hvs_format(u32 drm_format)
172{
173 unsigned i;
174
175 for (i = 0; i < ARRAY_SIZE(hvs_formats); i++) {
176 if (hvs_formats[i].drm == drm_format)
177 return &hvs_formats[i];
178 }
179
180 return NULL;
181}
182
Eric Anholt21af94c2015-10-20 16:06:57 +0100183static enum vc4_scaling_mode vc4_get_scaling_mode(u32 src, u32 dst)
184{
185 if (dst > src)
186 return VC4_SCALING_PPF;
187 else if (dst < src)
188 return VC4_SCALING_TPZ;
189 else
190 return VC4_SCALING_NONE;
191}
192
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800193static bool plane_enabled(struct drm_plane_state *state)
194{
195 return state->fb && state->crtc;
196}
197
kbuild test robot91276ae2015-10-22 11:12:26 +0800198static struct drm_plane_state *vc4_plane_duplicate_state(struct drm_plane *plane)
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800199{
200 struct vc4_plane_state *vc4_state;
201
202 if (WARN_ON(!plane->state))
203 return NULL;
204
205 vc4_state = kmemdup(plane->state, sizeof(*vc4_state), GFP_KERNEL);
206 if (!vc4_state)
207 return NULL;
208
Eric Anholt21af94c2015-10-20 16:06:57 +0100209 memset(&vc4_state->lbm, 0, sizeof(vc4_state->lbm));
210
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800211 __drm_atomic_helper_plane_duplicate_state(plane, &vc4_state->base);
212
213 if (vc4_state->dlist) {
214 vc4_state->dlist = kmemdup(vc4_state->dlist,
215 vc4_state->dlist_count * 4,
216 GFP_KERNEL);
217 if (!vc4_state->dlist) {
218 kfree(vc4_state);
219 return NULL;
220 }
221 vc4_state->dlist_size = vc4_state->dlist_count;
222 }
223
224 return &vc4_state->base;
225}
226
kbuild test robot91276ae2015-10-22 11:12:26 +0800227static void vc4_plane_destroy_state(struct drm_plane *plane,
228 struct drm_plane_state *state)
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800229{
Eric Anholt21af94c2015-10-20 16:06:57 +0100230 struct vc4_dev *vc4 = to_vc4_dev(plane->dev);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800231 struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
232
Eric Anholt21af94c2015-10-20 16:06:57 +0100233 if (vc4_state->lbm.allocated) {
234 unsigned long irqflags;
235
236 spin_lock_irqsave(&vc4->hvs->mm_lock, irqflags);
237 drm_mm_remove_node(&vc4_state->lbm);
238 spin_unlock_irqrestore(&vc4->hvs->mm_lock, irqflags);
239 }
240
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800241 kfree(vc4_state->dlist);
Daniel Vetter2f701692016-05-09 16:34:10 +0200242 __drm_atomic_helper_plane_destroy_state(&vc4_state->base);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800243 kfree(state);
244}
245
246/* Called during init to allocate the plane's atomic state. */
kbuild test robot91276ae2015-10-22 11:12:26 +0800247static void vc4_plane_reset(struct drm_plane *plane)
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800248{
249 struct vc4_plane_state *vc4_state;
250
251 WARN_ON(plane->state);
252
253 vc4_state = kzalloc(sizeof(*vc4_state), GFP_KERNEL);
254 if (!vc4_state)
255 return;
256
257 plane->state = &vc4_state->base;
258 vc4_state->base.plane = plane;
259}
260
261static void vc4_dlist_write(struct vc4_plane_state *vc4_state, u32 val)
262{
263 if (vc4_state->dlist_count == vc4_state->dlist_size) {
264 u32 new_size = max(4u, vc4_state->dlist_count * 2);
265 u32 *new_dlist = kmalloc(new_size * 4, GFP_KERNEL);
266
267 if (!new_dlist)
268 return;
269 memcpy(new_dlist, vc4_state->dlist, vc4_state->dlist_count * 4);
270
271 kfree(vc4_state->dlist);
272 vc4_state->dlist = new_dlist;
273 vc4_state->dlist_size = new_size;
274 }
275
276 vc4_state->dlist[vc4_state->dlist_count++] = val;
277}
278
Eric Anholt21af94c2015-10-20 16:06:57 +0100279/* Returns the scl0/scl1 field based on whether the dimensions need to
280 * be up/down/non-scaled.
281 *
282 * This is a replication of a table from the spec.
283 */
Eric Anholtfc040232015-12-30 12:25:44 -0800284static u32 vc4_get_scl_field(struct drm_plane_state *state, int plane)
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800285{
286 struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
Eric Anholt21af94c2015-10-20 16:06:57 +0100287
Eric Anholtfc040232015-12-30 12:25:44 -0800288 switch (vc4_state->x_scaling[plane] << 2 | vc4_state->y_scaling[plane]) {
Eric Anholt21af94c2015-10-20 16:06:57 +0100289 case VC4_SCALING_PPF << 2 | VC4_SCALING_PPF:
290 return SCALER_CTL0_SCL_H_PPF_V_PPF;
291 case VC4_SCALING_TPZ << 2 | VC4_SCALING_PPF:
292 return SCALER_CTL0_SCL_H_TPZ_V_PPF;
293 case VC4_SCALING_PPF << 2 | VC4_SCALING_TPZ:
294 return SCALER_CTL0_SCL_H_PPF_V_TPZ;
295 case VC4_SCALING_TPZ << 2 | VC4_SCALING_TPZ:
296 return SCALER_CTL0_SCL_H_TPZ_V_TPZ;
297 case VC4_SCALING_PPF << 2 | VC4_SCALING_NONE:
298 return SCALER_CTL0_SCL_H_PPF_V_NONE;
299 case VC4_SCALING_NONE << 2 | VC4_SCALING_PPF:
300 return SCALER_CTL0_SCL_H_NONE_V_PPF;
301 case VC4_SCALING_NONE << 2 | VC4_SCALING_TPZ:
302 return SCALER_CTL0_SCL_H_NONE_V_TPZ;
303 case VC4_SCALING_TPZ << 2 | VC4_SCALING_NONE:
304 return SCALER_CTL0_SCL_H_TPZ_V_NONE;
305 default:
306 case VC4_SCALING_NONE << 2 | VC4_SCALING_NONE:
307 /* The unity case is independently handled by
308 * SCALER_CTL0_UNITY.
309 */
310 return 0;
311 }
312}
313
314static int vc4_plane_setup_clipping_and_scaling(struct drm_plane_state *state)
315{
316 struct drm_plane *plane = state->plane;
317 struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800318 struct drm_framebuffer *fb = state->fb;
Eric Anholtfc040232015-12-30 12:25:44 -0800319 struct drm_gem_cma_object *bo = drm_fb_cma_get_gem_obj(fb, 0);
Eric Anholt21af94c2015-10-20 16:06:57 +0100320 u32 subpixel_src_mask = (1 << 16) - 1;
Ville Syrjälä438b74a2016-12-14 23:32:55 +0200321 u32 format = fb->format->format;
Ville Syrjäläbcb0b462016-12-14 23:30:22 +0200322 int num_planes = fb->format->num_planes;
Eric Anholtfc040232015-12-30 12:25:44 -0800323 u32 h_subsample = 1;
324 u32 v_subsample = 1;
325 int i;
Eric Anholt5c679992015-12-28 14:34:44 -0800326
Eric Anholtfc040232015-12-30 12:25:44 -0800327 for (i = 0; i < num_planes; i++)
328 vc4_state->offsets[i] = bo->paddr + fb->offsets[i];
Eric Anholt5c679992015-12-28 14:34:44 -0800329
Eric Anholt21af94c2015-10-20 16:06:57 +0100330 /* We don't support subpixel source positioning for scaling. */
331 if ((state->src_x & subpixel_src_mask) ||
332 (state->src_y & subpixel_src_mask) ||
333 (state->src_w & subpixel_src_mask) ||
334 (state->src_h & subpixel_src_mask)) {
Eric Anholtbf893ac2015-10-23 10:36:27 +0100335 return -EINVAL;
336 }
337
Eric Anholt21af94c2015-10-20 16:06:57 +0100338 vc4_state->src_x = state->src_x >> 16;
339 vc4_state->src_y = state->src_y >> 16;
Eric Anholtfc040232015-12-30 12:25:44 -0800340 vc4_state->src_w[0] = state->src_w >> 16;
341 vc4_state->src_h[0] = state->src_h >> 16;
Eric Anholtf863e352015-12-28 14:45:25 -0800342
343 vc4_state->crtc_x = state->crtc_x;
344 vc4_state->crtc_y = state->crtc_y;
345 vc4_state->crtc_w = state->crtc_w;
346 vc4_state->crtc_h = state->crtc_h;
347
Eric Anholtfc040232015-12-30 12:25:44 -0800348 vc4_state->x_scaling[0] = vc4_get_scaling_mode(vc4_state->src_w[0],
349 vc4_state->crtc_w);
350 vc4_state->y_scaling[0] = vc4_get_scaling_mode(vc4_state->src_h[0],
351 vc4_state->crtc_h);
352
353 if (num_planes > 1) {
354 vc4_state->is_yuv = true;
355
356 h_subsample = drm_format_horz_chroma_subsampling(format);
357 v_subsample = drm_format_vert_chroma_subsampling(format);
358 vc4_state->src_w[1] = vc4_state->src_w[0] / h_subsample;
359 vc4_state->src_h[1] = vc4_state->src_h[0] / v_subsample;
360
361 vc4_state->x_scaling[1] =
362 vc4_get_scaling_mode(vc4_state->src_w[1],
363 vc4_state->crtc_w);
364 vc4_state->y_scaling[1] =
365 vc4_get_scaling_mode(vc4_state->src_h[1],
366 vc4_state->crtc_h);
367
368 /* YUV conversion requires that scaling be enabled,
369 * even on a plane that's otherwise 1:1. Choose TPZ
370 * for simplicity.
371 */
372 if (vc4_state->x_scaling[0] == VC4_SCALING_NONE)
373 vc4_state->x_scaling[0] = VC4_SCALING_TPZ;
374 if (vc4_state->y_scaling[0] == VC4_SCALING_NONE)
375 vc4_state->y_scaling[0] = VC4_SCALING_TPZ;
376 }
377
378 vc4_state->is_unity = (vc4_state->x_scaling[0] == VC4_SCALING_NONE &&
379 vc4_state->y_scaling[0] == VC4_SCALING_NONE &&
380 vc4_state->x_scaling[1] == VC4_SCALING_NONE &&
381 vc4_state->y_scaling[1] == VC4_SCALING_NONE);
Eric Anholt21af94c2015-10-20 16:06:57 +0100382
383 /* No configuring scaling on the cursor plane, since it gets
384 non-vblank-synced updates, and scaling requires requires
385 LBM changes which have to be vblank-synced.
386 */
387 if (plane->type == DRM_PLANE_TYPE_CURSOR && !vc4_state->is_unity)
388 return -EINVAL;
389
390 /* Clamp the on-screen start x/y to 0. The hardware doesn't
391 * support negative y, and negative x wastes bandwidth.
392 */
Eric Anholt5c679992015-12-28 14:34:44 -0800393 if (vc4_state->crtc_x < 0) {
Eric Anholtfc040232015-12-30 12:25:44 -0800394 for (i = 0; i < num_planes; i++) {
Ville Syrjälä353c8592016-12-14 23:30:57 +0200395 u32 cpp = fb->format->cpp[i];
Eric Anholtfc040232015-12-30 12:25:44 -0800396 u32 subs = ((i == 0) ? 1 : h_subsample);
397
398 vc4_state->offsets[i] += (cpp *
399 (-vc4_state->crtc_x) / subs);
400 }
401 vc4_state->src_w[0] += vc4_state->crtc_x;
402 vc4_state->src_w[1] += vc4_state->crtc_x / h_subsample;
Eric Anholt5c679992015-12-28 14:34:44 -0800403 vc4_state->crtc_x = 0;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800404 }
405
Eric Anholt5c679992015-12-28 14:34:44 -0800406 if (vc4_state->crtc_y < 0) {
Eric Anholtfc040232015-12-30 12:25:44 -0800407 for (i = 0; i < num_planes; i++) {
408 u32 subs = ((i == 0) ? 1 : v_subsample);
409
410 vc4_state->offsets[i] += (fb->pitches[i] *
411 (-vc4_state->crtc_y) / subs);
412 }
413 vc4_state->src_h[0] += vc4_state->crtc_y;
414 vc4_state->src_h[1] += vc4_state->crtc_y / v_subsample;
Eric Anholt5c679992015-12-28 14:34:44 -0800415 vc4_state->crtc_y = 0;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800416 }
417
Eric Anholt5c679992015-12-28 14:34:44 -0800418 return 0;
419}
420
Eric Anholt21af94c2015-10-20 16:06:57 +0100421static void vc4_write_tpz(struct vc4_plane_state *vc4_state, u32 src, u32 dst)
422{
423 u32 scale, recip;
424
425 scale = (1 << 16) * src / dst;
426
427 /* The specs note that while the reciprocal would be defined
428 * as (1<<32)/scale, ~0 is close enough.
429 */
430 recip = ~0 / scale;
431
432 vc4_dlist_write(vc4_state,
433 VC4_SET_FIELD(scale, SCALER_TPZ0_SCALE) |
434 VC4_SET_FIELD(0, SCALER_TPZ0_IPHASE));
435 vc4_dlist_write(vc4_state,
436 VC4_SET_FIELD(recip, SCALER_TPZ1_RECIP));
437}
438
439static void vc4_write_ppf(struct vc4_plane_state *vc4_state, u32 src, u32 dst)
440{
441 u32 scale = (1 << 16) * src / dst;
442
443 vc4_dlist_write(vc4_state,
444 SCALER_PPF_AGC |
445 VC4_SET_FIELD(scale, SCALER_PPF_SCALE) |
446 VC4_SET_FIELD(0, SCALER_PPF_IPHASE));
447}
448
449static u32 vc4_lbm_size(struct drm_plane_state *state)
450{
451 struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
452 /* This is the worst case number. One of the two sizes will
453 * be used depending on the scaling configuration.
454 */
Eric Anholtfc040232015-12-30 12:25:44 -0800455 u32 pix_per_line = max(vc4_state->src_w[0], (u32)vc4_state->crtc_w);
Eric Anholt21af94c2015-10-20 16:06:57 +0100456 u32 lbm;
457
Eric Anholtfc040232015-12-30 12:25:44 -0800458 if (!vc4_state->is_yuv) {
459 if (vc4_state->is_unity)
460 return 0;
461 else if (vc4_state->y_scaling[0] == VC4_SCALING_TPZ)
462 lbm = pix_per_line * 8;
463 else {
464 /* In special cases, this multiplier might be 12. */
465 lbm = pix_per_line * 16;
466 }
467 } else {
468 /* There are cases for this going down to a multiplier
469 * of 2, but according to the firmware source, the
470 * table in the docs is somewhat wrong.
471 */
Eric Anholt21af94c2015-10-20 16:06:57 +0100472 lbm = pix_per_line * 16;
473 }
474
475 lbm = roundup(lbm, 32);
476
477 return lbm;
478}
479
Eric Anholtfc040232015-12-30 12:25:44 -0800480static void vc4_write_scaling_parameters(struct drm_plane_state *state,
481 int channel)
Eric Anholt21af94c2015-10-20 16:06:57 +0100482{
483 struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
484
485 /* Ch0 H-PPF Word 0: Scaling Parameters */
Eric Anholtfc040232015-12-30 12:25:44 -0800486 if (vc4_state->x_scaling[channel] == VC4_SCALING_PPF) {
Eric Anholt21af94c2015-10-20 16:06:57 +0100487 vc4_write_ppf(vc4_state,
Eric Anholtfc040232015-12-30 12:25:44 -0800488 vc4_state->src_w[channel], vc4_state->crtc_w);
Eric Anholt21af94c2015-10-20 16:06:57 +0100489 }
490
491 /* Ch0 V-PPF Words 0-1: Scaling Parameters, Context */
Eric Anholtfc040232015-12-30 12:25:44 -0800492 if (vc4_state->y_scaling[channel] == VC4_SCALING_PPF) {
Eric Anholt21af94c2015-10-20 16:06:57 +0100493 vc4_write_ppf(vc4_state,
Eric Anholtfc040232015-12-30 12:25:44 -0800494 vc4_state->src_h[channel], vc4_state->crtc_h);
Eric Anholt21af94c2015-10-20 16:06:57 +0100495 vc4_dlist_write(vc4_state, 0xc0c0c0c0);
496 }
497
498 /* Ch0 H-TPZ Words 0-1: Scaling Parameters, Recip */
Eric Anholtfc040232015-12-30 12:25:44 -0800499 if (vc4_state->x_scaling[channel] == VC4_SCALING_TPZ) {
Eric Anholt21af94c2015-10-20 16:06:57 +0100500 vc4_write_tpz(vc4_state,
Eric Anholtfc040232015-12-30 12:25:44 -0800501 vc4_state->src_w[channel], vc4_state->crtc_w);
Eric Anholt21af94c2015-10-20 16:06:57 +0100502 }
503
504 /* Ch0 V-TPZ Words 0-2: Scaling Parameters, Recip, Context */
Eric Anholtfc040232015-12-30 12:25:44 -0800505 if (vc4_state->y_scaling[channel] == VC4_SCALING_TPZ) {
Eric Anholt21af94c2015-10-20 16:06:57 +0100506 vc4_write_tpz(vc4_state,
Eric Anholtfc040232015-12-30 12:25:44 -0800507 vc4_state->src_h[channel], vc4_state->crtc_h);
Eric Anholt21af94c2015-10-20 16:06:57 +0100508 vc4_dlist_write(vc4_state, 0xc0c0c0c0);
509 }
510}
Eric Anholt5c679992015-12-28 14:34:44 -0800511
512/* Writes out a full display list for an active plane to the plane's
513 * private dlist state.
514 */
515static int vc4_plane_mode_set(struct drm_plane *plane,
516 struct drm_plane_state *state)
517{
Eric Anholt21af94c2015-10-20 16:06:57 +0100518 struct vc4_dev *vc4 = to_vc4_dev(plane->dev);
Eric Anholt5c679992015-12-28 14:34:44 -0800519 struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
520 struct drm_framebuffer *fb = state->fb;
Eric Anholt5c679992015-12-28 14:34:44 -0800521 u32 ctl0_offset = vc4_state->dlist_count;
Ville Syrjälä438b74a2016-12-14 23:32:55 +0200522 const struct hvs_format *format = vc4_get_hvs_format(fb->format->format);
Eric Anholtfc040232015-12-30 12:25:44 -0800523 int num_planes = drm_format_num_planes(format->drm);
Eric Anholt98830d912017-06-07 17:13:35 -0700524 u32 scl0, scl1, pitch0;
525 u32 lbm_size, tiling;
Eric Anholt21af94c2015-10-20 16:06:57 +0100526 unsigned long irqflags;
Eric Anholtfc040232015-12-30 12:25:44 -0800527 int ret, i;
Eric Anholt5c679992015-12-28 14:34:44 -0800528
529 ret = vc4_plane_setup_clipping_and_scaling(state);
530 if (ret)
531 return ret;
532
Eric Anholt21af94c2015-10-20 16:06:57 +0100533 /* Allocate the LBM memory that the HVS will use for temporary
534 * storage due to our scaling/format conversion.
535 */
536 lbm_size = vc4_lbm_size(state);
537 if (lbm_size) {
538 if (!vc4_state->lbm.allocated) {
539 spin_lock_irqsave(&vc4->hvs->mm_lock, irqflags);
Chris Wilson4e64e552017-02-02 21:04:38 +0000540 ret = drm_mm_insert_node_generic(&vc4->hvs->lbm_mm,
541 &vc4_state->lbm,
542 lbm_size, 32, 0, 0);
Eric Anholt21af94c2015-10-20 16:06:57 +0100543 spin_unlock_irqrestore(&vc4->hvs->mm_lock, irqflags);
544 } else {
545 WARN_ON_ONCE(lbm_size != vc4_state->lbm.size);
546 }
547 }
548
549 if (ret)
550 return ret;
551
Eric Anholtfc040232015-12-30 12:25:44 -0800552 /* SCL1 is used for Cb/Cr scaling of planar formats. For RGB
553 * and 4:4:4, scl1 should be set to scl0 so both channels of
554 * the scaler do the same thing. For YUV, the Y plane needs
555 * to be put in channel 1 and Cb/Cr in channel 0, so we swap
556 * the scl fields here.
557 */
558 if (num_planes == 1) {
559 scl0 = vc4_get_scl_field(state, 1);
560 scl1 = scl0;
561 } else {
562 scl0 = vc4_get_scl_field(state, 1);
563 scl1 = vc4_get_scl_field(state, 0);
564 }
Eric Anholt21af94c2015-10-20 16:06:57 +0100565
Eric Anholt98830d912017-06-07 17:13:35 -0700566 switch (fb->modifier) {
567 case DRM_FORMAT_MOD_LINEAR:
568 tiling = SCALER_CTL0_TILING_LINEAR;
569 pitch0 = VC4_SET_FIELD(fb->pitches[0], SCALER_SRC_PITCH);
570 break;
Eric Anholt652badb2017-09-27 12:32:09 -0700571
572 case DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED: {
573 /* For T-tiled, the FB pitch is "how many bytes from
574 * one row to the next, such that pitch * tile_h ==
575 * tile_size * tiles_per_row."
576 */
577 u32 tile_size_shift = 12; /* T tiles are 4kb */
578 u32 tile_h_shift = 5; /* 16 and 32bpp are 32 pixels high */
579 u32 tiles_w = fb->pitches[0] >> (tile_size_shift - tile_h_shift);
580
Eric Anholt98830d912017-06-07 17:13:35 -0700581 tiling = SCALER_CTL0_TILING_256B_OR_T;
582
Eric Anholt652badb2017-09-27 12:32:09 -0700583 pitch0 = (VC4_SET_FIELD(0, SCALER_PITCH0_TILE_Y_OFFSET) |
584 VC4_SET_FIELD(0, SCALER_PITCH0_TILE_WIDTH_L) |
585 VC4_SET_FIELD(tiles_w, SCALER_PITCH0_TILE_WIDTH_R));
Eric Anholt98830d912017-06-07 17:13:35 -0700586 break;
Eric Anholt652badb2017-09-27 12:32:09 -0700587 }
588
Eric Anholt98830d912017-06-07 17:13:35 -0700589 default:
590 DRM_DEBUG_KMS("Unsupported FB tiling flag 0x%16llx",
591 (long long)fb->modifier);
592 return -EINVAL;
593 }
594
Eric Anholt21af94c2015-10-20 16:06:57 +0100595 /* Control word */
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800596 vc4_dlist_write(vc4_state,
597 SCALER_CTL0_VALID |
598 (format->pixel_order << SCALER_CTL0_ORDER_SHIFT) |
599 (format->hvs << SCALER_CTL0_PIXEL_FORMAT_SHIFT) |
Eric Anholt98830d912017-06-07 17:13:35 -0700600 VC4_SET_FIELD(tiling, SCALER_CTL0_TILING) |
Eric Anholt21af94c2015-10-20 16:06:57 +0100601 (vc4_state->is_unity ? SCALER_CTL0_UNITY : 0) |
Eric Anholtfc040232015-12-30 12:25:44 -0800602 VC4_SET_FIELD(scl0, SCALER_CTL0_SCL0) |
603 VC4_SET_FIELD(scl1, SCALER_CTL0_SCL1));
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800604
605 /* Position Word 0: Image Positions and Alpha Value */
Eric Anholt6674a902015-12-30 11:50:22 -0800606 vc4_state->pos0_offset = vc4_state->dlist_count;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800607 vc4_dlist_write(vc4_state,
608 VC4_SET_FIELD(0xff, SCALER_POS0_FIXED_ALPHA) |
Eric Anholt5c679992015-12-28 14:34:44 -0800609 VC4_SET_FIELD(vc4_state->crtc_x, SCALER_POS0_START_X) |
610 VC4_SET_FIELD(vc4_state->crtc_y, SCALER_POS0_START_Y));
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800611
Eric Anholt21af94c2015-10-20 16:06:57 +0100612 /* Position Word 1: Scaled Image Dimensions. */
613 if (!vc4_state->is_unity) {
614 vc4_dlist_write(vc4_state,
615 VC4_SET_FIELD(vc4_state->crtc_w,
616 SCALER_POS1_SCL_WIDTH) |
617 VC4_SET_FIELD(vc4_state->crtc_h,
618 SCALER_POS1_SCL_HEIGHT));
619 }
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800620
Stefan Schake05202c22018-03-09 01:53:34 +0100621 /* Position Word 2: Source Image Size, Alpha */
Eric Anholt6674a902015-12-30 11:50:22 -0800622 vc4_state->pos2_offset = vc4_state->dlist_count;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800623 vc4_dlist_write(vc4_state,
Maxime Ripard124e5da2017-12-22 15:31:27 +0100624 VC4_SET_FIELD(fb->format->has_alpha ?
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800625 SCALER_POS2_ALPHA_MODE_PIPELINE :
626 SCALER_POS2_ALPHA_MODE_FIXED,
627 SCALER_POS2_ALPHA_MODE) |
Stefan Schake05202c22018-03-09 01:53:34 +0100628 (fb->format->has_alpha ? SCALER_POS2_ALPHA_PREMULT : 0) |
Eric Anholtfc040232015-12-30 12:25:44 -0800629 VC4_SET_FIELD(vc4_state->src_w[0], SCALER_POS2_WIDTH) |
630 VC4_SET_FIELD(vc4_state->src_h[0], SCALER_POS2_HEIGHT));
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800631
632 /* Position Word 3: Context. Written by the HVS. */
633 vc4_dlist_write(vc4_state, 0xc0c0c0c0);
634
Eric Anholtfc040232015-12-30 12:25:44 -0800635
636 /* Pointer Word 0/1/2: RGB / Y / Cb / Cr Pointers
637 *
638 * The pointers may be any byte address.
639 */
Eric Anholt6674a902015-12-30 11:50:22 -0800640 vc4_state->ptr0_offset = vc4_state->dlist_count;
Dave Stevenson090cb0c2017-11-16 14:22:30 +0000641 for (i = 0; i < num_planes; i++)
642 vc4_dlist_write(vc4_state, vc4_state->offsets[i]);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800643
Eric Anholtfc040232015-12-30 12:25:44 -0800644 /* Pointer Context Word 0/1/2: Written by the HVS */
645 for (i = 0; i < num_planes; i++)
646 vc4_dlist_write(vc4_state, 0xc0c0c0c0);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800647
Eric Anholt98830d912017-06-07 17:13:35 -0700648 /* Pitch word 0 */
649 vc4_dlist_write(vc4_state, pitch0);
650
651 /* Pitch word 1/2 */
652 for (i = 1; i < num_planes; i++) {
Eric Anholtfc040232015-12-30 12:25:44 -0800653 vc4_dlist_write(vc4_state,
654 VC4_SET_FIELD(fb->pitches[i], SCALER_SRC_PITCH));
655 }
656
657 /* Colorspace conversion words */
658 if (vc4_state->is_yuv) {
659 vc4_dlist_write(vc4_state, SCALER_CSC0_ITR_R_601_5);
660 vc4_dlist_write(vc4_state, SCALER_CSC1_ITR_R_601_5);
661 vc4_dlist_write(vc4_state, SCALER_CSC2_ITR_R_601_5);
662 }
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800663
Eric Anholt21af94c2015-10-20 16:06:57 +0100664 if (!vc4_state->is_unity) {
665 /* LBM Base Address. */
Eric Anholtfc040232015-12-30 12:25:44 -0800666 if (vc4_state->y_scaling[0] != VC4_SCALING_NONE ||
667 vc4_state->y_scaling[1] != VC4_SCALING_NONE) {
Eric Anholt21af94c2015-10-20 16:06:57 +0100668 vc4_dlist_write(vc4_state, vc4_state->lbm.start);
Eric Anholtfc040232015-12-30 12:25:44 -0800669 }
Eric Anholt21af94c2015-10-20 16:06:57 +0100670
Eric Anholtfc040232015-12-30 12:25:44 -0800671 if (num_planes > 1) {
672 /* Emit Cb/Cr as channel 0 and Y as channel
673 * 1. This matches how we set up scl0/scl1
674 * above.
675 */
676 vc4_write_scaling_parameters(state, 1);
677 }
678 vc4_write_scaling_parameters(state, 0);
Eric Anholt21af94c2015-10-20 16:06:57 +0100679
680 /* If any PPF setup was done, then all the kernel
681 * pointers get uploaded.
682 */
Eric Anholtfc040232015-12-30 12:25:44 -0800683 if (vc4_state->x_scaling[0] == VC4_SCALING_PPF ||
684 vc4_state->y_scaling[0] == VC4_SCALING_PPF ||
685 vc4_state->x_scaling[1] == VC4_SCALING_PPF ||
686 vc4_state->y_scaling[1] == VC4_SCALING_PPF) {
Eric Anholt21af94c2015-10-20 16:06:57 +0100687 u32 kernel = VC4_SET_FIELD(vc4->hvs->mitchell_netravali_filter.start,
688 SCALER_PPF_KERNEL_OFFSET);
689
690 /* HPPF plane 0 */
691 vc4_dlist_write(vc4_state, kernel);
692 /* VPPF plane 0 */
693 vc4_dlist_write(vc4_state, kernel);
694 /* HPPF plane 1 */
695 vc4_dlist_write(vc4_state, kernel);
696 /* VPPF plane 1 */
697 vc4_dlist_write(vc4_state, kernel);
698 }
699 }
700
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800701 vc4_state->dlist[ctl0_offset] |=
702 VC4_SET_FIELD(vc4_state->dlist_count, SCALER_CTL0_SIZE);
703
704 return 0;
705}
706
707/* If a modeset involves changing the setup of a plane, the atomic
708 * infrastructure will call this to validate a proposed plane setup.
709 * However, if a plane isn't getting updated, this (and the
710 * corresponding vc4_plane_atomic_update) won't get called. Thus, we
711 * compute the dlist here and have all active plane dlists get updated
712 * in the CRTC's flush.
713 */
714static int vc4_plane_atomic_check(struct drm_plane *plane,
715 struct drm_plane_state *state)
716{
717 struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
718
719 vc4_state->dlist_count = 0;
720
721 if (plane_enabled(state))
722 return vc4_plane_mode_set(plane, state);
723 else
724 return 0;
725}
726
727static void vc4_plane_atomic_update(struct drm_plane *plane,
728 struct drm_plane_state *old_state)
729{
730 /* No contents here. Since we don't know where in the CRTC's
731 * dlist we should be stored, our dlist is uploaded to the
732 * hardware with vc4_plane_write_dlist() at CRTC atomic_flush
733 * time.
734 */
735}
736
737u32 vc4_plane_write_dlist(struct drm_plane *plane, u32 __iomem *dlist)
738{
739 struct vc4_plane_state *vc4_state = to_vc4_plane_state(plane->state);
740 int i;
741
Eric Anholtb501bac2015-11-30 12:34:01 -0800742 vc4_state->hw_dlist = dlist;
743
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800744 /* Can't memcpy_toio() because it needs to be 32-bit writes. */
745 for (i = 0; i < vc4_state->dlist_count; i++)
746 writel(vc4_state->dlist[i], &dlist[i]);
747
748 return vc4_state->dlist_count;
749}
750
Daniel Vetter2f196b72016-06-02 16:21:44 +0200751u32 vc4_plane_dlist_size(const struct drm_plane_state *state)
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800752{
Daniel Vetter2f196b72016-06-02 16:21:44 +0200753 const struct vc4_plane_state *vc4_state =
754 container_of(state, typeof(*vc4_state), base);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800755
756 return vc4_state->dlist_count;
757}
758
Eric Anholtb501bac2015-11-30 12:34:01 -0800759/* Updates the plane to immediately (well, once the FIFO needs
760 * refilling) scan out from at a new framebuffer.
761 */
762void vc4_plane_async_set_fb(struct drm_plane *plane, struct drm_framebuffer *fb)
763{
764 struct vc4_plane_state *vc4_state = to_vc4_plane_state(plane->state);
765 struct drm_gem_cma_object *bo = drm_fb_cma_get_gem_obj(fb, 0);
766 uint32_t addr;
767
768 /* We're skipping the address adjustment for negative origin,
769 * because this is only called on the primary plane.
770 */
771 WARN_ON_ONCE(plane->state->crtc_x < 0 || plane->state->crtc_y < 0);
772 addr = bo->paddr + fb->offsets[0];
773
774 /* Write the new address into the hardware immediately. The
775 * scanout will start from this address as soon as the FIFO
776 * needs to refill with pixels.
777 */
Eric Anholt6674a902015-12-30 11:50:22 -0800778 writel(addr, &vc4_state->hw_dlist[vc4_state->ptr0_offset]);
Eric Anholtb501bac2015-11-30 12:34:01 -0800779
780 /* Also update the CPU-side dlist copy, so that any later
781 * atomic updates that don't do a new modeset on our plane
782 * also use our updated address.
783 */
Eric Anholt6674a902015-12-30 11:50:22 -0800784 vc4_state->dlist[vc4_state->ptr0_offset] = addr;
Eric Anholtb501bac2015-11-30 12:34:01 -0800785}
786
Eric Anholt334dbd62017-06-21 11:49:59 -0700787static int vc4_prepare_fb(struct drm_plane *plane,
788 struct drm_plane_state *state)
789{
790 struct vc4_bo *bo;
791 struct dma_fence *fence;
Boris Brezillonb9f19252017-10-19 14:57:48 +0200792 int ret;
Eric Anholt334dbd62017-06-21 11:49:59 -0700793
794 if ((plane->state->fb == state->fb) || !state->fb)
795 return 0;
796
797 bo = to_vc4_bo(&drm_fb_cma_get_gem_obj(state->fb, 0)->base);
Boris Brezillonb9f19252017-10-19 14:57:48 +0200798
799 ret = vc4_bo_inc_usecnt(bo);
800 if (ret)
801 return ret;
802
Eric Anholt334dbd62017-06-21 11:49:59 -0700803 fence = reservation_object_get_excl_rcu(bo->resv);
804 drm_atomic_set_fence_for_plane(state, fence);
805
806 return 0;
807}
808
Boris Brezillonb9f19252017-10-19 14:57:48 +0200809static void vc4_cleanup_fb(struct drm_plane *plane,
810 struct drm_plane_state *state)
811{
812 struct vc4_bo *bo;
813
814 if (plane->state->fb == state->fb || !state->fb)
815 return;
816
817 bo = to_vc4_bo(&drm_fb_cma_get_gem_obj(state->fb, 0)->base);
818 vc4_bo_dec_usecnt(bo);
819}
820
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800821static const struct drm_plane_helper_funcs vc4_plane_helper_funcs = {
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800822 .atomic_check = vc4_plane_atomic_check,
823 .atomic_update = vc4_plane_atomic_update,
Eric Anholt334dbd62017-06-21 11:49:59 -0700824 .prepare_fb = vc4_prepare_fb,
Boris Brezillonb9f19252017-10-19 14:57:48 +0200825 .cleanup_fb = vc4_cleanup_fb,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800826};
827
828static void vc4_plane_destroy(struct drm_plane *plane)
829{
830 drm_plane_helper_disable(plane);
831 drm_plane_cleanup(plane);
832}
833
Eric Anholt6674a902015-12-30 11:50:22 -0800834/* Implements immediate (non-vblank-synced) updates of the cursor
835 * position, or falls back to the atomic helper otherwise.
836 */
837static int
838vc4_update_plane(struct drm_plane *plane,
839 struct drm_crtc *crtc,
840 struct drm_framebuffer *fb,
841 int crtc_x, int crtc_y,
842 unsigned int crtc_w, unsigned int crtc_h,
843 uint32_t src_x, uint32_t src_y,
Daniel Vetter34a2ab52017-03-22 22:50:41 +0100844 uint32_t src_w, uint32_t src_h,
845 struct drm_modeset_acquire_ctx *ctx)
Eric Anholt6674a902015-12-30 11:50:22 -0800846{
847 struct drm_plane_state *plane_state;
848 struct vc4_plane_state *vc4_state;
849
850 if (plane != crtc->cursor)
851 goto out;
852
853 plane_state = plane->state;
854 vc4_state = to_vc4_plane_state(plane_state);
855
856 if (!plane_state)
857 goto out;
858
Eric Anholt6674a902015-12-30 11:50:22 -0800859 /* No configuring new scaling in the fast path. */
860 if (crtc_w != plane_state->crtc_w ||
861 crtc_h != plane_state->crtc_h ||
862 src_w != plane_state->src_w ||
863 src_h != plane_state->src_h) {
864 goto out;
865 }
866
Michael Zoran6d24c1c2017-02-23 17:54:31 -0800867 if (fb != plane_state->fb) {
868 drm_atomic_set_fb_for_plane(plane->state, fb);
869 vc4_plane_async_set_fb(plane, fb);
870 }
871
Eric Anholt6674a902015-12-30 11:50:22 -0800872 /* Set the cursor's position on the screen. This is the
873 * expected change from the drm_mode_cursor_universal()
874 * helper.
875 */
876 plane_state->crtc_x = crtc_x;
877 plane_state->crtc_y = crtc_y;
878
879 /* Allow changing the start position within the cursor BO, if
880 * that matters.
881 */
882 plane_state->src_x = src_x;
883 plane_state->src_y = src_y;
884
885 /* Update the display list based on the new crtc_x/y. */
886 vc4_plane_atomic_check(plane, plane_state);
887
888 /* Note that we can't just call vc4_plane_write_dlist()
889 * because that would smash the context data that the HVS is
890 * currently using.
891 */
892 writel(vc4_state->dlist[vc4_state->pos0_offset],
893 &vc4_state->hw_dlist[vc4_state->pos0_offset]);
894 writel(vc4_state->dlist[vc4_state->pos2_offset],
895 &vc4_state->hw_dlist[vc4_state->pos2_offset]);
896 writel(vc4_state->dlist[vc4_state->ptr0_offset],
897 &vc4_state->hw_dlist[vc4_state->ptr0_offset]);
898
899 return 0;
900
901out:
902 return drm_atomic_helper_update_plane(plane, crtc, fb,
903 crtc_x, crtc_y,
904 crtc_w, crtc_h,
905 src_x, src_y,
Daniel Vetter34a2ab52017-03-22 22:50:41 +0100906 src_w, src_h,
907 ctx);
Eric Anholt6674a902015-12-30 11:50:22 -0800908}
909
Daniel Stone423ad7b2017-08-08 17:44:48 +0100910static bool vc4_format_mod_supported(struct drm_plane *plane,
911 uint32_t format,
912 uint64_t modifier)
913{
914 /* Support T_TILING for RGB formats only. */
915 switch (format) {
916 case DRM_FORMAT_XRGB8888:
917 case DRM_FORMAT_ARGB8888:
918 case DRM_FORMAT_ABGR8888:
919 case DRM_FORMAT_XBGR8888:
920 case DRM_FORMAT_RGB565:
921 case DRM_FORMAT_BGR565:
922 case DRM_FORMAT_ARGB1555:
923 case DRM_FORMAT_XRGB1555:
924 return true;
925 case DRM_FORMAT_YUV422:
926 case DRM_FORMAT_YVU422:
927 case DRM_FORMAT_YUV420:
928 case DRM_FORMAT_YVU420:
929 case DRM_FORMAT_NV12:
930 case DRM_FORMAT_NV16:
931 default:
932 return (modifier == DRM_FORMAT_MOD_LINEAR);
933 }
934}
935
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800936static const struct drm_plane_funcs vc4_plane_funcs = {
Eric Anholt6674a902015-12-30 11:50:22 -0800937 .update_plane = vc4_update_plane,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800938 .disable_plane = drm_atomic_helper_disable_plane,
939 .destroy = vc4_plane_destroy,
940 .set_property = NULL,
941 .reset = vc4_plane_reset,
942 .atomic_duplicate_state = vc4_plane_duplicate_state,
943 .atomic_destroy_state = vc4_plane_destroy_state,
Daniel Stone423ad7b2017-08-08 17:44:48 +0100944 .format_mod_supported = vc4_format_mod_supported,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800945};
946
947struct drm_plane *vc4_plane_init(struct drm_device *dev,
948 enum drm_plane_type type)
949{
950 struct drm_plane *plane = NULL;
951 struct vc4_plane *vc4_plane;
952 u32 formats[ARRAY_SIZE(hvs_formats)];
Eric Anholtfc040232015-12-30 12:25:44 -0800953 u32 num_formats = 0;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800954 int ret = 0;
955 unsigned i;
Daniel Stone423ad7b2017-08-08 17:44:48 +0100956 static const uint64_t modifiers[] = {
957 DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED,
958 DRM_FORMAT_MOD_LINEAR,
959 DRM_FORMAT_MOD_INVALID
960 };
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800961
962 vc4_plane = devm_kzalloc(dev->dev, sizeof(*vc4_plane),
963 GFP_KERNEL);
Colin Ian King7b347342017-03-16 18:54:18 +0000964 if (!vc4_plane)
965 return ERR_PTR(-ENOMEM);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800966
Eric Anholtfc040232015-12-30 12:25:44 -0800967 for (i = 0; i < ARRAY_SIZE(hvs_formats); i++) {
968 /* Don't allow YUV in cursor planes, since that means
969 * tuning on the scaler, which we don't allow for the
970 * cursor.
971 */
972 if (type != DRM_PLANE_TYPE_CURSOR ||
973 hvs_formats[i].hvs < HVS_PIXEL_FORMAT_YCBCR_YUV420_3PLANE) {
974 formats[num_formats++] = hvs_formats[i].drm;
975 }
976 }
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800977 plane = &vc4_plane->base;
Andrzej Pietrasiewicz49d29a02017-02-01 10:35:08 +0100978 ret = drm_universal_plane_init(dev, plane, 0,
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800979 &vc4_plane_funcs,
Eric Anholtfc040232015-12-30 12:25:44 -0800980 formats, num_formats,
Daniel Stone423ad7b2017-08-08 17:44:48 +0100981 modifiers, type, NULL);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800982
983 drm_plane_helper_add(plane, &vc4_plane_helper_funcs);
984
985 return plane;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800986}