blob: 283abd7d1e9b6819f6f4b4336645b787e996918b [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 Vetter72fdb402018-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 Brezillon58a6a362018-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 Brezillon58a6a362018-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 Brezillon58a6a362018-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 Brezillon58a6a362018-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 Brezillon58a6a362018-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 Brezillon7cd3cf32018-12-07 09:36:06 +0100495 u32 tiling, src_y;
Dave Stevensone065a8d2018-03-16 15:04:35 -0700496 u32 hvs_format = format->hvs;
Boris Brezillon7cd3cf32018-12-07 09:36:06 +0100497 unsigned int rotation;
Eric Anholtfc040232015-12-30 12:25:44 -0800498 int ret, i;
Eric Anholt5c679992015-12-28 14:34:44 -0800499
Boris Brezillon8d938442018-11-30 10:02:51 +0100500 if (vc4_state->dlist_initialized)
501 return 0;
502
Eric Anholt5c679992015-12-28 14:34:44 -0800503 ret = vc4_plane_setup_clipping_and_scaling(state);
504 if (ret)
505 return ret;
506
Eric Anholtfc040232015-12-30 12:25:44 -0800507 /* SCL1 is used for Cb/Cr scaling of planar formats. For RGB
508 * and 4:4:4, scl1 should be set to scl0 so both channels of
509 * the scaler do the same thing. For YUV, the Y plane needs
510 * to be put in channel 1 and Cb/Cr in channel 0, so we swap
511 * the scl fields here.
512 */
513 if (num_planes == 1) {
Boris Brezillon9a0e9802018-05-07 14:13:03 +0200514 scl0 = vc4_get_scl_field(state, 0);
Eric Anholtfc040232015-12-30 12:25:44 -0800515 scl1 = scl0;
516 } else {
517 scl0 = vc4_get_scl_field(state, 1);
518 scl1 = vc4_get_scl_field(state, 0);
519 }
Eric Anholt21af94c2015-10-20 16:06:57 +0100520
Boris Brezillona65511b12018-08-03 11:22:30 +0200521 h_subsample = drm_format_horz_chroma_subsampling(format->drm);
522 v_subsample = drm_format_vert_chroma_subsampling(format->drm);
523
Boris Brezillon7cd3cf32018-12-07 09:36:06 +0100524 rotation = drm_rotation_simplify(state->rotation,
525 DRM_MODE_ROTATE_0 |
526 DRM_MODE_REFLECT_X |
527 DRM_MODE_REFLECT_Y);
528
529 /* We must point to the last line when Y reflection is enabled. */
530 src_y = vc4_state->src_y;
531 if (rotation & DRM_MODE_REFLECT_Y)
532 src_y += vc4_state->src_h[0] - 1;
533
Dave Stevensone065a8d2018-03-16 15:04:35 -0700534 switch (base_format_mod) {
Eric Anholt98830d912017-06-07 17:13:35 -0700535 case DRM_FORMAT_MOD_LINEAR:
536 tiling = SCALER_CTL0_TILING_LINEAR;
537 pitch0 = VC4_SET_FIELD(fb->pitches[0], SCALER_SRC_PITCH);
Boris Brezillona65511b12018-08-03 11:22:30 +0200538
539 /* Adjust the base pointer to the first pixel to be scanned
540 * out.
541 */
542 for (i = 0; i < num_planes; i++) {
Boris Brezillon7cd3cf32018-12-07 09:36:06 +0100543 vc4_state->offsets[i] += src_y /
Boris Brezillona65511b12018-08-03 11:22:30 +0200544 (i ? v_subsample : 1) *
545 fb->pitches[i];
Boris Brezillon7cd3cf32018-12-07 09:36:06 +0100546
Boris Brezillona65511b12018-08-03 11:22:30 +0200547 vc4_state->offsets[i] += vc4_state->src_x /
548 (i ? h_subsample : 1) *
549 fb->format->cpp[i];
550 }
Boris Brezillon3e407412018-08-03 11:22:31 +0200551
Eric Anholt98830d912017-06-07 17:13:35 -0700552 break;
Eric Anholt652badb2017-09-27 12:32:09 -0700553
554 case DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED: {
Eric Anholt652badb2017-09-27 12:32:09 -0700555 u32 tile_size_shift = 12; /* T tiles are 4kb */
Boris Brezillon3e407412018-08-03 11:22:31 +0200556 /* Whole-tile offsets, mostly for setting the pitch. */
557 u32 tile_w_shift = fb->format->cpp[0] == 2 ? 6 : 5;
Eric Anholt652badb2017-09-27 12:32:09 -0700558 u32 tile_h_shift = 5; /* 16 and 32bpp are 32 pixels high */
Boris Brezillon3e407412018-08-03 11:22:31 +0200559 u32 tile_w_mask = (1 << tile_w_shift) - 1;
560 /* The height mask on 32-bit-per-pixel tiles is 63, i.e. twice
561 * the height (in pixels) of a 4k tile.
562 */
563 u32 tile_h_mask = (2 << tile_h_shift) - 1;
564 /* For T-tiled, the FB pitch is "how many bytes from one row to
565 * the next, such that
566 *
567 * pitch * tile_h == tile_size * tiles_per_row
568 */
Eric Anholt652badb2017-09-27 12:32:09 -0700569 u32 tiles_w = fb->pitches[0] >> (tile_size_shift - tile_h_shift);
Boris Brezillon3e407412018-08-03 11:22:31 +0200570 u32 tiles_l = vc4_state->src_x >> tile_w_shift;
571 u32 tiles_r = tiles_w - tiles_l;
Boris Brezillon7cd3cf32018-12-07 09:36:06 +0100572 u32 tiles_t = src_y >> tile_h_shift;
Boris Brezillon3e407412018-08-03 11:22:31 +0200573 /* Intra-tile offsets, which modify the base address (the
574 * SCALER_PITCH0_TILE_Y_OFFSET tells HVS how to walk from that
575 * base address).
576 */
Boris Brezillon7cd3cf32018-12-07 09:36:06 +0100577 u32 tile_y = (src_y >> 4) & 1;
578 u32 subtile_y = (src_y >> 2) & 3;
579 u32 utile_y = src_y & 3;
Boris Brezillon3e407412018-08-03 11:22:31 +0200580 u32 x_off = vc4_state->src_x & tile_w_mask;
Boris Brezillon7cd3cf32018-12-07 09:36:06 +0100581 u32 y_off = src_y & tile_h_mask;
582
583 /* When Y reflection is requested we must set the
584 * SCALER_PITCH0_TILE_LINE_DIR flag to tell HVS that all lines
585 * after the initial one should be fetched in descending order,
586 * which makes sense since we start from the last line and go
587 * backward.
588 * Don't know why we need y_off = max_y_off - y_off, but it's
589 * definitely required (I guess it's also related to the "going
590 * backward" situation).
591 */
592 if (rotation & DRM_MODE_REFLECT_Y) {
593 y_off = tile_h_mask - y_off;
594 pitch0 = SCALER_PITCH0_TILE_LINE_DIR;
595 } else {
596 pitch0 = 0;
597 }
Eric Anholt652badb2017-09-27 12:32:09 -0700598
Eric Anholt98830d912017-06-07 17:13:35 -0700599 tiling = SCALER_CTL0_TILING_256B_OR_T;
Boris Brezillon7cd3cf32018-12-07 09:36:06 +0100600 pitch0 |= (VC4_SET_FIELD(x_off, SCALER_PITCH0_SINK_PIX) |
601 VC4_SET_FIELD(y_off, SCALER_PITCH0_TILE_Y_OFFSET) |
602 VC4_SET_FIELD(tiles_l, SCALER_PITCH0_TILE_WIDTH_L) |
603 VC4_SET_FIELD(tiles_r, SCALER_PITCH0_TILE_WIDTH_R));
Boris Brezillon3e407412018-08-03 11:22:31 +0200604 vc4_state->offsets[0] += tiles_t * (tiles_w << tile_size_shift);
605 vc4_state->offsets[0] += subtile_y << 8;
606 vc4_state->offsets[0] += utile_y << 4;
Eric Anholt98830d912017-06-07 17:13:35 -0700607
Boris Brezillon3e407412018-08-03 11:22:31 +0200608 /* Rows of tiles alternate left-to-right and right-to-left. */
609 if (tiles_t & 1) {
610 pitch0 |= SCALER_PITCH0_TILE_INITIAL_LINE_DIR;
611 vc4_state->offsets[0] += (tiles_w - tiles_l) <<
612 tile_size_shift;
613 vc4_state->offsets[0] -= (1 + !tile_y) << 10;
614 } else {
615 vc4_state->offsets[0] += tiles_l << tile_size_shift;
616 vc4_state->offsets[0] += tile_y << 10;
617 }
618
Eric Anholt98830d912017-06-07 17:13:35 -0700619 break;
Eric Anholt652badb2017-09-27 12:32:09 -0700620 }
621
Dave Stevensone065a8d2018-03-16 15:04:35 -0700622 case DRM_FORMAT_MOD_BROADCOM_SAND64:
623 case DRM_FORMAT_MOD_BROADCOM_SAND128:
624 case DRM_FORMAT_MOD_BROADCOM_SAND256: {
625 uint32_t param = fourcc_mod_broadcom_param(fb->modifier);
Boris Brezillon8e75d582018-12-07 09:36:05 +0100626 u32 tile_w, tile, x_off, pix_per_tile;
Dave Stevensone065a8d2018-03-16 15:04:35 -0700627
628 /* Column-based NV12 or RGBA.
629 */
630 if (fb->format->num_planes > 1) {
631 if (hvs_format != HVS_PIXEL_FORMAT_YCBCR_YUV420_2PLANE) {
632 DRM_DEBUG_KMS("SAND format only valid for NV12/21");
633 return -EINVAL;
634 }
635 hvs_format = HVS_PIXEL_FORMAT_H264;
636 } else {
637 if (base_format_mod == DRM_FORMAT_MOD_BROADCOM_SAND256) {
638 DRM_DEBUG_KMS("SAND256 format only valid for H.264");
639 return -EINVAL;
640 }
641 }
642
643 switch (base_format_mod) {
644 case DRM_FORMAT_MOD_BROADCOM_SAND64:
645 tiling = SCALER_CTL0_TILING_64B;
Boris Brezillon8e75d582018-12-07 09:36:05 +0100646 tile_w = 64;
Dave Stevensone065a8d2018-03-16 15:04:35 -0700647 break;
648 case DRM_FORMAT_MOD_BROADCOM_SAND128:
649 tiling = SCALER_CTL0_TILING_128B;
Boris Brezillon8e75d582018-12-07 09:36:05 +0100650 tile_w = 128;
Dave Stevensone065a8d2018-03-16 15:04:35 -0700651 break;
652 case DRM_FORMAT_MOD_BROADCOM_SAND256:
653 tiling = SCALER_CTL0_TILING_256B_OR_T;
Boris Brezillon8e75d582018-12-07 09:36:05 +0100654 tile_w = 256;
Dave Stevensone065a8d2018-03-16 15:04:35 -0700655 break;
656 default:
657 break;
658 }
659
660 if (param > SCALER_TILE_HEIGHT_MASK) {
661 DRM_DEBUG_KMS("SAND height too large (%d)\n", param);
662 return -EINVAL;
663 }
664
Boris Brezillon8e75d582018-12-07 09:36:05 +0100665 pix_per_tile = tile_w / fb->format->cpp[0];
666 tile = vc4_state->src_x / pix_per_tile;
667 x_off = vc4_state->src_x % pix_per_tile;
668
669 /* Adjust the base pointer to the first pixel to be scanned
670 * out.
671 */
672 for (i = 0; i < num_planes; i++) {
673 vc4_state->offsets[i] += param * tile_w * tile;
Boris Brezillon7cd3cf32018-12-07 09:36:06 +0100674 vc4_state->offsets[i] += src_y /
Boris Brezillon8e75d582018-12-07 09:36:05 +0100675 (i ? v_subsample : 1) *
676 tile_w;
677 vc4_state->offsets[i] += x_off /
678 (i ? h_subsample : 1) *
679 fb->format->cpp[i];
680 }
681
Dave Stevensone065a8d2018-03-16 15:04:35 -0700682 pitch0 = VC4_SET_FIELD(param, SCALER_TILE_HEIGHT);
683 break;
684 }
685
Eric Anholt98830d912017-06-07 17:13:35 -0700686 default:
687 DRM_DEBUG_KMS("Unsupported FB tiling flag 0x%16llx",
688 (long long)fb->modifier);
689 return -EINVAL;
690 }
691
Eric Anholt21af94c2015-10-20 16:06:57 +0100692 /* Control word */
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800693 vc4_dlist_write(vc4_state,
694 SCALER_CTL0_VALID |
Boris Brezillon7cd3cf32018-12-07 09:36:06 +0100695 (rotation & DRM_MODE_REFLECT_X ? SCALER_CTL0_HFLIP : 0) |
696 (rotation & DRM_MODE_REFLECT_Y ? SCALER_CTL0_VFLIP : 0) |
Maxime Ripard3257ec72018-05-17 15:37:59 +0200697 VC4_SET_FIELD(SCALER_CTL0_RGBA_EXPAND_ROUND, SCALER_CTL0_RGBA_EXPAND) |
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800698 (format->pixel_order << SCALER_CTL0_ORDER_SHIFT) |
Dave Stevensone065a8d2018-03-16 15:04:35 -0700699 (hvs_format << SCALER_CTL0_PIXEL_FORMAT_SHIFT) |
Eric Anholt98830d912017-06-07 17:13:35 -0700700 VC4_SET_FIELD(tiling, SCALER_CTL0_TILING) |
Eric Anholt21af94c2015-10-20 16:06:57 +0100701 (vc4_state->is_unity ? SCALER_CTL0_UNITY : 0) |
Eric Anholtfc040232015-12-30 12:25:44 -0800702 VC4_SET_FIELD(scl0, SCALER_CTL0_SCL0) |
703 VC4_SET_FIELD(scl1, SCALER_CTL0_SCL1));
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800704
705 /* Position Word 0: Image Positions and Alpha Value */
Eric Anholt6674a902015-12-30 11:50:22 -0800706 vc4_state->pos0_offset = vc4_state->dlist_count;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800707 vc4_dlist_write(vc4_state,
Stefan Schake22445f02018-04-20 17:09:54 -0700708 VC4_SET_FIELD(state->alpha >> 8, SCALER_POS0_FIXED_ALPHA) |
Eric Anholt5c679992015-12-28 14:34:44 -0800709 VC4_SET_FIELD(vc4_state->crtc_x, SCALER_POS0_START_X) |
710 VC4_SET_FIELD(vc4_state->crtc_y, SCALER_POS0_START_Y));
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800711
Eric Anholt21af94c2015-10-20 16:06:57 +0100712 /* Position Word 1: Scaled Image Dimensions. */
713 if (!vc4_state->is_unity) {
714 vc4_dlist_write(vc4_state,
715 VC4_SET_FIELD(vc4_state->crtc_w,
716 SCALER_POS1_SCL_WIDTH) |
717 VC4_SET_FIELD(vc4_state->crtc_h,
718 SCALER_POS1_SCL_HEIGHT));
719 }
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800720
Stefan Schake22445f02018-04-20 17:09:54 -0700721 /* Don't waste cycles mixing with plane alpha if the set alpha
722 * is opaque or there is no per-pixel alpha information.
723 * In any case we use the alpha property value as the fixed alpha.
724 */
725 mix_plane_alpha = state->alpha != DRM_BLEND_ALPHA_OPAQUE &&
726 fb->format->has_alpha;
727
Stefan Schake05202c22018-03-09 01:53:34 +0100728 /* Position Word 2: Source Image Size, Alpha */
Eric Anholt6674a902015-12-30 11:50:22 -0800729 vc4_state->pos2_offset = vc4_state->dlist_count;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800730 vc4_dlist_write(vc4_state,
Maxime Ripard124e5da2017-12-22 15:31:27 +0100731 VC4_SET_FIELD(fb->format->has_alpha ?
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800732 SCALER_POS2_ALPHA_MODE_PIPELINE :
733 SCALER_POS2_ALPHA_MODE_FIXED,
734 SCALER_POS2_ALPHA_MODE) |
Stefan Schake22445f02018-04-20 17:09:54 -0700735 (mix_plane_alpha ? SCALER_POS2_ALPHA_MIX : 0) |
Stefan Schake05202c22018-03-09 01:53:34 +0100736 (fb->format->has_alpha ? SCALER_POS2_ALPHA_PREMULT : 0) |
Eric Anholtfc040232015-12-30 12:25:44 -0800737 VC4_SET_FIELD(vc4_state->src_w[0], SCALER_POS2_WIDTH) |
738 VC4_SET_FIELD(vc4_state->src_h[0], SCALER_POS2_HEIGHT));
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800739
740 /* Position Word 3: Context. Written by the HVS. */
741 vc4_dlist_write(vc4_state, 0xc0c0c0c0);
742
Eric Anholtfc040232015-12-30 12:25:44 -0800743
744 /* Pointer Word 0/1/2: RGB / Y / Cb / Cr Pointers
745 *
746 * The pointers may be any byte address.
747 */
Eric Anholt6674a902015-12-30 11:50:22 -0800748 vc4_state->ptr0_offset = vc4_state->dlist_count;
Dave Stevenson090cb0c2017-11-16 14:22:30 +0000749 for (i = 0; i < num_planes; i++)
750 vc4_dlist_write(vc4_state, vc4_state->offsets[i]);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800751
Eric Anholtfc040232015-12-30 12:25:44 -0800752 /* Pointer Context Word 0/1/2: Written by the HVS */
753 for (i = 0; i < num_planes; i++)
754 vc4_dlist_write(vc4_state, 0xc0c0c0c0);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800755
Eric Anholt98830d912017-06-07 17:13:35 -0700756 /* Pitch word 0 */
757 vc4_dlist_write(vc4_state, pitch0);
758
759 /* Pitch word 1/2 */
760 for (i = 1; i < num_planes; i++) {
Dave Stevensone065a8d2018-03-16 15:04:35 -0700761 if (hvs_format != HVS_PIXEL_FORMAT_H264) {
762 vc4_dlist_write(vc4_state,
763 VC4_SET_FIELD(fb->pitches[i],
764 SCALER_SRC_PITCH));
765 } else {
766 vc4_dlist_write(vc4_state, pitch0);
767 }
Eric Anholtfc040232015-12-30 12:25:44 -0800768 }
769
770 /* Colorspace conversion words */
771 if (vc4_state->is_yuv) {
772 vc4_dlist_write(vc4_state, SCALER_CSC0_ITR_R_601_5);
773 vc4_dlist_write(vc4_state, SCALER_CSC1_ITR_R_601_5);
774 vc4_dlist_write(vc4_state, SCALER_CSC2_ITR_R_601_5);
775 }
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800776
Boris Brezillon0a038c12018-11-30 10:02:50 +0100777 vc4_state->lbm_offset = 0;
778
Boris Brezillon658d8cb2018-07-25 14:29:07 +0200779 if (vc4_state->x_scaling[0] != VC4_SCALING_NONE ||
780 vc4_state->x_scaling[1] != VC4_SCALING_NONE ||
781 vc4_state->y_scaling[0] != VC4_SCALING_NONE ||
782 vc4_state->y_scaling[1] != VC4_SCALING_NONE) {
Boris Brezillon0a038c12018-11-30 10:02:50 +0100783 /* Reserve a slot for the LBM Base Address. The real value will
784 * be set when calling vc4_plane_allocate_lbm().
785 */
Eric Anholtfc040232015-12-30 12:25:44 -0800786 if (vc4_state->y_scaling[0] != VC4_SCALING_NONE ||
Boris Brezillon0a038c12018-11-30 10:02:50 +0100787 vc4_state->y_scaling[1] != VC4_SCALING_NONE)
788 vc4_state->lbm_offset = vc4_state->dlist_count++;
Eric Anholt21af94c2015-10-20 16:06:57 +0100789
Eric Anholtfc040232015-12-30 12:25:44 -0800790 if (num_planes > 1) {
791 /* Emit Cb/Cr as channel 0 and Y as channel
792 * 1. This matches how we set up scl0/scl1
793 * above.
794 */
795 vc4_write_scaling_parameters(state, 1);
796 }
797 vc4_write_scaling_parameters(state, 0);
Eric Anholt21af94c2015-10-20 16:06:57 +0100798
799 /* If any PPF setup was done, then all the kernel
800 * pointers get uploaded.
801 */
Eric Anholtfc040232015-12-30 12:25:44 -0800802 if (vc4_state->x_scaling[0] == VC4_SCALING_PPF ||
803 vc4_state->y_scaling[0] == VC4_SCALING_PPF ||
804 vc4_state->x_scaling[1] == VC4_SCALING_PPF ||
805 vc4_state->y_scaling[1] == VC4_SCALING_PPF) {
Eric Anholt21af94c2015-10-20 16:06:57 +0100806 u32 kernel = VC4_SET_FIELD(vc4->hvs->mitchell_netravali_filter.start,
807 SCALER_PPF_KERNEL_OFFSET);
808
809 /* HPPF plane 0 */
810 vc4_dlist_write(vc4_state, kernel);
811 /* VPPF plane 0 */
812 vc4_dlist_write(vc4_state, kernel);
813 /* HPPF plane 1 */
814 vc4_dlist_write(vc4_state, kernel);
815 /* VPPF plane 1 */
816 vc4_dlist_write(vc4_state, kernel);
817 }
818 }
819
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800820 vc4_state->dlist[ctl0_offset] |=
821 VC4_SET_FIELD(vc4_state->dlist_count, SCALER_CTL0_SIZE);
822
Stefan Schake3d67b682018-03-09 01:53:35 +0100823 /* crtc_* are already clipped coordinates. */
824 covers_screen = vc4_state->crtc_x == 0 && vc4_state->crtc_y == 0 &&
825 vc4_state->crtc_w == state->crtc->mode.hdisplay &&
826 vc4_state->crtc_h == state->crtc->mode.vdisplay;
827 /* Background fill might be necessary when the plane has per-pixel
Stefan Schake22445f02018-04-20 17:09:54 -0700828 * alpha content or a non-opaque plane alpha and could blend from the
829 * background or does not cover the entire screen.
Stefan Schake3d67b682018-03-09 01:53:35 +0100830 */
Stefan Schake22445f02018-04-20 17:09:54 -0700831 vc4_state->needs_bg_fill = fb->format->has_alpha || !covers_screen ||
832 state->alpha != DRM_BLEND_ALPHA_OPAQUE;
Stefan Schake3d67b682018-03-09 01:53:35 +0100833
Boris Brezillon8d938442018-11-30 10:02:51 +0100834 /* Flag the dlist as initialized to avoid checking it twice in case
835 * the async update check already called vc4_plane_mode_set() and
836 * decided to fallback to sync update because async update was not
837 * possible.
838 */
839 vc4_state->dlist_initialized = 1;
840
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800841 return 0;
842}
843
844/* If a modeset involves changing the setup of a plane, the atomic
845 * infrastructure will call this to validate a proposed plane setup.
846 * However, if a plane isn't getting updated, this (and the
847 * corresponding vc4_plane_atomic_update) won't get called. Thus, we
848 * compute the dlist here and have all active plane dlists get updated
849 * in the CRTC's flush.
850 */
851static int vc4_plane_atomic_check(struct drm_plane *plane,
852 struct drm_plane_state *state)
853{
854 struct vc4_plane_state *vc4_state = to_vc4_plane_state(state);
Boris Brezillon0a038c12018-11-30 10:02:50 +0100855 int ret;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800856
857 vc4_state->dlist_count = 0;
858
Boris Brezillon0a038c12018-11-30 10:02:50 +0100859 if (!plane_enabled(state))
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800860 return 0;
Boris Brezillon0a038c12018-11-30 10:02:50 +0100861
862 ret = vc4_plane_mode_set(plane, state);
863 if (ret)
864 return ret;
865
866 return vc4_plane_allocate_lbm(state);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800867}
868
869static void vc4_plane_atomic_update(struct drm_plane *plane,
870 struct drm_plane_state *old_state)
871{
872 /* No contents here. Since we don't know where in the CRTC's
873 * dlist we should be stored, our dlist is uploaded to the
874 * hardware with vc4_plane_write_dlist() at CRTC atomic_flush
875 * time.
876 */
877}
878
879u32 vc4_plane_write_dlist(struct drm_plane *plane, u32 __iomem *dlist)
880{
881 struct vc4_plane_state *vc4_state = to_vc4_plane_state(plane->state);
882 int i;
883
Eric Anholtb501bac2015-11-30 12:34:01 -0800884 vc4_state->hw_dlist = dlist;
885
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800886 /* Can't memcpy_toio() because it needs to be 32-bit writes. */
887 for (i = 0; i < vc4_state->dlist_count; i++)
888 writel(vc4_state->dlist[i], &dlist[i]);
889
890 return vc4_state->dlist_count;
891}
892
Daniel Vetter2f196b72016-06-02 16:21:44 +0200893u32 vc4_plane_dlist_size(const struct drm_plane_state *state)
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800894{
Daniel Vetter2f196b72016-06-02 16:21:44 +0200895 const struct vc4_plane_state *vc4_state =
896 container_of(state, typeof(*vc4_state), base);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800897
898 return vc4_state->dlist_count;
899}
900
Eric Anholtb501bac2015-11-30 12:34:01 -0800901/* Updates the plane to immediately (well, once the FIFO needs
902 * refilling) scan out from at a new framebuffer.
903 */
904void vc4_plane_async_set_fb(struct drm_plane *plane, struct drm_framebuffer *fb)
905{
906 struct vc4_plane_state *vc4_state = to_vc4_plane_state(plane->state);
907 struct drm_gem_cma_object *bo = drm_fb_cma_get_gem_obj(fb, 0);
908 uint32_t addr;
909
910 /* We're skipping the address adjustment for negative origin,
911 * because this is only called on the primary plane.
912 */
913 WARN_ON_ONCE(plane->state->crtc_x < 0 || plane->state->crtc_y < 0);
914 addr = bo->paddr + fb->offsets[0];
915
916 /* Write the new address into the hardware immediately. The
917 * scanout will start from this address as soon as the FIFO
918 * needs to refill with pixels.
919 */
Eric Anholt6674a902015-12-30 11:50:22 -0800920 writel(addr, &vc4_state->hw_dlist[vc4_state->ptr0_offset]);
Eric Anholtb501bac2015-11-30 12:34:01 -0800921
922 /* Also update the CPU-side dlist copy, so that any later
923 * atomic updates that don't do a new modeset on our plane
924 * also use our updated address.
925 */
Eric Anholt6674a902015-12-30 11:50:22 -0800926 vc4_state->dlist[vc4_state->ptr0_offset] = addr;
Eric Anholtb501bac2015-11-30 12:34:01 -0800927}
928
Gustavo Padovan539c3202018-03-30 10:54:45 +0200929static void vc4_plane_atomic_async_update(struct drm_plane *plane,
930 struct drm_plane_state *state)
931{
Boris Brezillon5a439112018-11-15 11:58:51 +0100932 struct vc4_plane_state *vc4_state, *new_vc4_state;
Gustavo Padovan539c3202018-03-30 10:54:45 +0200933
Boris Brezillon1d4118c2018-11-30 10:02:52 +0100934 drm_atomic_set_fb_for_plane(plane->state, state->fb);
Gustavo Padovan539c3202018-03-30 10:54:45 +0200935 plane->state->crtc_x = state->crtc_x;
936 plane->state->crtc_y = state->crtc_y;
Boris Brezillon1d4118c2018-11-30 10:02:52 +0100937 plane->state->crtc_w = state->crtc_w;
938 plane->state->crtc_h = state->crtc_h;
Gustavo Padovan539c3202018-03-30 10:54:45 +0200939 plane->state->src_x = state->src_x;
940 plane->state->src_y = state->src_y;
Boris Brezillon1d4118c2018-11-30 10:02:52 +0100941 plane->state->src_w = state->src_w;
942 plane->state->src_h = state->src_h;
943 plane->state->src_h = state->src_h;
944 plane->state->alpha = state->alpha;
945 plane->state->pixel_blend_mode = state->pixel_blend_mode;
946 plane->state->rotation = state->rotation;
947 plane->state->zpos = state->zpos;
948 plane->state->normalized_zpos = state->normalized_zpos;
949 plane->state->color_encoding = state->color_encoding;
950 plane->state->color_range = state->color_range;
951 plane->state->src = state->src;
952 plane->state->dst = state->dst;
953 plane->state->visible = state->visible;
Boris Brezillon5a439112018-11-15 11:58:51 +0100954
955 new_vc4_state = to_vc4_plane_state(state);
956 vc4_state = to_vc4_plane_state(plane->state);
957
Boris Brezillon1d4118c2018-11-30 10:02:52 +0100958 vc4_state->crtc_x = new_vc4_state->crtc_x;
959 vc4_state->crtc_y = new_vc4_state->crtc_y;
960 vc4_state->crtc_h = new_vc4_state->crtc_h;
961 vc4_state->crtc_w = new_vc4_state->crtc_w;
962 vc4_state->src_x = new_vc4_state->src_x;
963 vc4_state->src_y = new_vc4_state->src_y;
964 memcpy(vc4_state->src_w, new_vc4_state->src_w,
965 sizeof(vc4_state->src_w));
966 memcpy(vc4_state->src_h, new_vc4_state->src_h,
967 sizeof(vc4_state->src_h));
968 memcpy(vc4_state->x_scaling, new_vc4_state->x_scaling,
969 sizeof(vc4_state->x_scaling));
970 memcpy(vc4_state->y_scaling, new_vc4_state->y_scaling,
971 sizeof(vc4_state->y_scaling));
972 vc4_state->is_unity = new_vc4_state->is_unity;
973 vc4_state->is_yuv = new_vc4_state->is_yuv;
974 memcpy(vc4_state->offsets, new_vc4_state->offsets,
975 sizeof(vc4_state->offsets));
976 vc4_state->needs_bg_fill = new_vc4_state->needs_bg_fill;
977
Boris Brezillon5a439112018-11-15 11:58:51 +0100978 /* Update the current vc4_state pos0, pos2 and ptr0 dlist entries. */
979 vc4_state->dlist[vc4_state->pos0_offset] =
980 new_vc4_state->dlist[vc4_state->pos0_offset];
981 vc4_state->dlist[vc4_state->pos2_offset] =
982 new_vc4_state->dlist[vc4_state->pos2_offset];
983 vc4_state->dlist[vc4_state->ptr0_offset] =
984 new_vc4_state->dlist[vc4_state->ptr0_offset];
Gustavo Padovan539c3202018-03-30 10:54:45 +0200985
986 /* Note that we can't just call vc4_plane_write_dlist()
987 * because that would smash the context data that the HVS is
988 * currently using.
989 */
990 writel(vc4_state->dlist[vc4_state->pos0_offset],
991 &vc4_state->hw_dlist[vc4_state->pos0_offset]);
992 writel(vc4_state->dlist[vc4_state->pos2_offset],
993 &vc4_state->hw_dlist[vc4_state->pos2_offset]);
994 writel(vc4_state->dlist[vc4_state->ptr0_offset],
995 &vc4_state->hw_dlist[vc4_state->ptr0_offset]);
996}
997
998static int vc4_plane_atomic_async_check(struct drm_plane *plane,
999 struct drm_plane_state *state)
1000{
Boris Brezillon1d4118c2018-11-30 10:02:52 +01001001 struct vc4_plane_state *old_vc4_state, *new_vc4_state;
1002 int ret;
1003 u32 i;
1004
1005 ret = vc4_plane_mode_set(plane, state);
1006 if (ret)
1007 return ret;
1008
1009 old_vc4_state = to_vc4_plane_state(plane->state);
1010 new_vc4_state = to_vc4_plane_state(state);
1011 if (old_vc4_state->dlist_count != new_vc4_state->dlist_count ||
1012 old_vc4_state->pos0_offset != new_vc4_state->pos0_offset ||
1013 old_vc4_state->pos2_offset != new_vc4_state->pos2_offset ||
1014 old_vc4_state->ptr0_offset != new_vc4_state->ptr0_offset ||
1015 vc4_lbm_size(plane->state) != vc4_lbm_size(state))
Gustavo Padovan539c3202018-03-30 10:54:45 +02001016 return -EINVAL;
1017
Boris Brezillon1d4118c2018-11-30 10:02:52 +01001018 /* Only pos0, pos2 and ptr0 DWORDS can be updated in an async update
1019 * if anything else has changed, fallback to a sync update.
1020 */
1021 for (i = 0; i < new_vc4_state->dlist_count; i++) {
1022 if (i == new_vc4_state->pos0_offset ||
1023 i == new_vc4_state->pos2_offset ||
1024 i == new_vc4_state->ptr0_offset ||
1025 (new_vc4_state->lbm_offset &&
1026 i == new_vc4_state->lbm_offset))
1027 continue;
1028
1029 if (new_vc4_state->dlist[i] != old_vc4_state->dlist[i])
1030 return -EINVAL;
1031 }
1032
Gustavo Padovan539c3202018-03-30 10:54:45 +02001033 return 0;
1034}
1035
Eric Anholt334dbd62017-06-21 11:49:59 -07001036static int vc4_prepare_fb(struct drm_plane *plane,
1037 struct drm_plane_state *state)
1038{
1039 struct vc4_bo *bo;
1040 struct dma_fence *fence;
Boris Brezillonb9f19252017-10-19 14:57:48 +02001041 int ret;
Eric Anholt334dbd62017-06-21 11:49:59 -07001042
Daniel Vetter2227a7a2018-04-05 17:44:48 +02001043 if (!state->fb)
Eric Anholt334dbd62017-06-21 11:49:59 -07001044 return 0;
1045
1046 bo = to_vc4_bo(&drm_fb_cma_get_gem_obj(state->fb, 0)->base);
Boris Brezillonb9f19252017-10-19 14:57:48 +02001047
Daniel Vetter2227a7a2018-04-05 17:44:48 +02001048 fence = reservation_object_get_excl_rcu(bo->resv);
1049 drm_atomic_set_fence_for_plane(state, fence);
1050
1051 if (plane->state->fb == state->fb)
1052 return 0;
1053
Boris Brezillonb9f19252017-10-19 14:57:48 +02001054 ret = vc4_bo_inc_usecnt(bo);
1055 if (ret)
1056 return ret;
1057
Eric Anholt334dbd62017-06-21 11:49:59 -07001058 return 0;
1059}
1060
Boris Brezillonb9f19252017-10-19 14:57:48 +02001061static void vc4_cleanup_fb(struct drm_plane *plane,
1062 struct drm_plane_state *state)
1063{
1064 struct vc4_bo *bo;
1065
1066 if (plane->state->fb == state->fb || !state->fb)
1067 return;
1068
1069 bo = to_vc4_bo(&drm_fb_cma_get_gem_obj(state->fb, 0)->base);
1070 vc4_bo_dec_usecnt(bo);
1071}
1072
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001073static const struct drm_plane_helper_funcs vc4_plane_helper_funcs = {
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001074 .atomic_check = vc4_plane_atomic_check,
1075 .atomic_update = vc4_plane_atomic_update,
Eric Anholt334dbd62017-06-21 11:49:59 -07001076 .prepare_fb = vc4_prepare_fb,
Boris Brezillonb9f19252017-10-19 14:57:48 +02001077 .cleanup_fb = vc4_cleanup_fb,
Gustavo Padovan539c3202018-03-30 10:54:45 +02001078 .atomic_async_check = vc4_plane_atomic_async_check,
1079 .atomic_async_update = vc4_plane_atomic_async_update,
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001080};
1081
1082static void vc4_plane_destroy(struct drm_plane *plane)
1083{
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001084 drm_plane_cleanup(plane);
1085}
1086
Daniel Stone423ad7b2017-08-08 17:44:48 +01001087static bool vc4_format_mod_supported(struct drm_plane *plane,
1088 uint32_t format,
1089 uint64_t modifier)
1090{
1091 /* Support T_TILING for RGB formats only. */
1092 switch (format) {
1093 case DRM_FORMAT_XRGB8888:
1094 case DRM_FORMAT_ARGB8888:
1095 case DRM_FORMAT_ABGR8888:
1096 case DRM_FORMAT_XBGR8888:
1097 case DRM_FORMAT_RGB565:
1098 case DRM_FORMAT_BGR565:
1099 case DRM_FORMAT_ARGB1555:
1100 case DRM_FORMAT_XRGB1555:
Dave Stevensone065a8d2018-03-16 15:04:35 -07001101 switch (fourcc_mod_broadcom_mod(modifier)) {
1102 case DRM_FORMAT_MOD_LINEAR:
1103 case DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED:
1104 case DRM_FORMAT_MOD_BROADCOM_SAND64:
1105 case DRM_FORMAT_MOD_BROADCOM_SAND128:
1106 return true;
1107 default:
1108 return false;
1109 }
1110 case DRM_FORMAT_NV12:
1111 case DRM_FORMAT_NV21:
1112 switch (fourcc_mod_broadcom_mod(modifier)) {
1113 case DRM_FORMAT_MOD_LINEAR:
1114 case DRM_FORMAT_MOD_BROADCOM_SAND64:
1115 case DRM_FORMAT_MOD_BROADCOM_SAND128:
1116 case DRM_FORMAT_MOD_BROADCOM_SAND256:
1117 return true;
1118 default:
1119 return false;
1120 }
Daniel Stone423ad7b2017-08-08 17:44:48 +01001121 case DRM_FORMAT_YUV422:
1122 case DRM_FORMAT_YVU422:
1123 case DRM_FORMAT_YUV420:
1124 case DRM_FORMAT_YVU420:
Daniel Stone423ad7b2017-08-08 17:44:48 +01001125 case DRM_FORMAT_NV16:
Eric Anholt1e871d62018-03-16 15:04:34 -07001126 case DRM_FORMAT_NV61:
Daniel Stone423ad7b2017-08-08 17:44:48 +01001127 default:
1128 return (modifier == DRM_FORMAT_MOD_LINEAR);
1129 }
1130}
1131
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001132static const struct drm_plane_funcs vc4_plane_funcs = {
Gustavo Padovan539c3202018-03-30 10:54:45 +02001133 .update_plane = drm_atomic_helper_update_plane,
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001134 .disable_plane = drm_atomic_helper_disable_plane,
1135 .destroy = vc4_plane_destroy,
1136 .set_property = NULL,
1137 .reset = vc4_plane_reset,
1138 .atomic_duplicate_state = vc4_plane_duplicate_state,
1139 .atomic_destroy_state = vc4_plane_destroy_state,
Daniel Stone423ad7b2017-08-08 17:44:48 +01001140 .format_mod_supported = vc4_format_mod_supported,
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001141};
1142
1143struct drm_plane *vc4_plane_init(struct drm_device *dev,
1144 enum drm_plane_type type)
1145{
1146 struct drm_plane *plane = NULL;
1147 struct vc4_plane *vc4_plane;
1148 u32 formats[ARRAY_SIZE(hvs_formats)];
1149 int ret = 0;
1150 unsigned i;
Daniel Stone423ad7b2017-08-08 17:44:48 +01001151 static const uint64_t modifiers[] = {
1152 DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED,
Dave Stevensone065a8d2018-03-16 15:04:35 -07001153 DRM_FORMAT_MOD_BROADCOM_SAND128,
1154 DRM_FORMAT_MOD_BROADCOM_SAND64,
1155 DRM_FORMAT_MOD_BROADCOM_SAND256,
Daniel Stone423ad7b2017-08-08 17:44:48 +01001156 DRM_FORMAT_MOD_LINEAR,
1157 DRM_FORMAT_MOD_INVALID
1158 };
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001159
1160 vc4_plane = devm_kzalloc(dev->dev, sizeof(*vc4_plane),
1161 GFP_KERNEL);
Colin Ian King7b347342017-03-16 18:54:18 +00001162 if (!vc4_plane)
1163 return ERR_PTR(-ENOMEM);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001164
Boris Brezillon2c2853f2018-11-30 10:02:54 +01001165 for (i = 0; i < ARRAY_SIZE(hvs_formats); i++)
1166 formats[i] = hvs_formats[i].drm;
1167
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001168 plane = &vc4_plane->base;
Andrzej Pietrasiewicz49d29a02017-02-01 10:35:08 +01001169 ret = drm_universal_plane_init(dev, plane, 0,
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001170 &vc4_plane_funcs,
Boris Brezillon2c2853f2018-11-30 10:02:54 +01001171 formats, ARRAY_SIZE(formats),
Daniel Stone423ad7b2017-08-08 17:44:48 +01001172 modifiers, type, NULL);
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001173
1174 drm_plane_helper_add(plane, &vc4_plane_helper_funcs);
1175
Stefan Schake22445f02018-04-20 17:09:54 -07001176 drm_plane_create_alpha_property(plane);
Boris Brezillon7cd3cf32018-12-07 09:36:06 +01001177 drm_plane_create_rotation_property(plane, DRM_MODE_ROTATE_0,
1178 DRM_MODE_ROTATE_0 |
1179 DRM_MODE_ROTATE_180 |
1180 DRM_MODE_REFLECT_X |
1181 DRM_MODE_REFLECT_Y);
Stefan Schake22445f02018-04-20 17:09:54 -07001182
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001183 return plane;
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001184}