rcar_du_vsp.c 10.2 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0+
2 3 4 5 6 7 8 9 10 11 12
/*
 * rcar_du_vsp.h  --  R-Car Display Unit VSP-Based Compositor
 *
 * Copyright (C) 2015 Renesas Electronics Corporation
 *
 * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
 */

#include <drm/drm_atomic_helper.h>
#include <drm/drm_crtc.h>
#include <drm/drm_fb_cma_helper.h>
13
#include <drm/drm_fourcc.h>
14
#include <drm/drm_gem_cma_helper.h>
E
Emre Ucan 已提交
15
#include <drm/drm_gem_framebuffer_helper.h>
16
#include <drm/drm_plane_helper.h>
17
#include <drm/drm_vblank.h>
18

19
#include <linux/bitops.h>
20
#include <linux/dma-mapping.h>
21
#include <linux/of_platform.h>
22
#include <linux/scatterlist.h>
23 24 25 26 27 28 29
#include <linux/videodev2.h>

#include <media/vsp1.h>

#include "rcar_du_drv.h"
#include "rcar_du_kms.h"
#include "rcar_du_vsp.h"
30
#include "rcar_du_writeback.h"
31

32
static void rcar_du_vsp_complete(void *private, unsigned int status, u32 crc)
33 34 35
{
	struct rcar_du_crtc *crtc = private;

36 37 38
	if (crtc->vblank_enable)
		drm_crtc_handle_vblank(&crtc->crtc);

39
	if (status & VSP1_DU_STATUS_COMPLETE)
40
		rcar_du_crtc_finish_page_flip(crtc);
41 42
	if (status & VSP1_DU_STATUS_WRITEBACK)
		rcar_du_writeback_complete(crtc);
43 44

	drm_crtc_add_crc_entry(&crtc->crtc, false, 0, &crc);
45 46
}

47 48 49
void rcar_du_vsp_enable(struct rcar_du_crtc *crtc)
{
	const struct drm_display_mode *mode = &crtc->crtc.state->adjusted_mode;
50
	struct rcar_du_device *rcdu = crtc->group->dev;
51 52 53
	struct vsp1_du_lif_config cfg = {
		.width = mode->hdisplay,
		.height = mode->vdisplay,
54
		.interlaced = mode->flags & DRM_MODE_FLAG_INTERLACE,
55 56
		.callback = rcar_du_vsp_complete,
		.callback_data = crtc,
57
	};
58 59
	struct rcar_du_plane_state state = {
		.state = {
60
			.alpha = DRM_BLEND_ALPHA_OPAQUE,
61
			.crtc = &crtc->crtc,
62 63 64 65 66 67 68 69
			.dst.x1 = 0,
			.dst.y1 = 0,
			.dst.x2 = mode->hdisplay,
			.dst.y2 = mode->vdisplay,
			.src.x1 = 0,
			.src.y1 = 0,
			.src.x2 = mode->hdisplay << 16,
			.src.y2 = mode->vdisplay << 16,
70
			.zpos = 0,
71 72 73 74 75 76
		},
		.format = rcar_du_format_info(DRM_FORMAT_ARGB8888),
		.source = RCAR_DU_PLANE_VSPD1,
		.colorkey = 0,
	};

77 78 79 80 81
	if (rcdu->info->gen >= 3)
		state.hwindex = (crtc->index % 2) ? 2 : 0;
	else
		state.hwindex = crtc->index % 2;

82 83
	__rcar_du_plane_setup(crtc->group, &state);

84 85
	/*
	 * Ensure that the plane source configuration takes effect by requesting
86 87 88 89 90 91 92
	 * a restart of the group. See rcar_du_plane_atomic_update() for a more
	 * detailed explanation.
	 *
	 * TODO: Check whether this is still needed on Gen3.
	 */
	crtc->group->need_restart = true;

93
	vsp1_du_setup_lif(crtc->vsp->vsp, crtc->vsp_pipe, &cfg);
94 95 96 97
}

void rcar_du_vsp_disable(struct rcar_du_crtc *crtc)
{
98
	vsp1_du_setup_lif(crtc->vsp->vsp, crtc->vsp_pipe, NULL);
99 100 101 102
}

void rcar_du_vsp_atomic_begin(struct rcar_du_crtc *crtc)
{
103
	vsp1_du_atomic_begin(crtc->vsp->vsp, crtc->vsp_pipe);
104 105 106 107
}

void rcar_du_vsp_atomic_flush(struct rcar_du_crtc *crtc)
{
108
	struct vsp1_du_atomic_pipe_config cfg = { { 0, } };
109 110 111 112
	struct rcar_du_crtc_state *state;

	state = to_rcar_crtc_state(crtc->crtc.state);
	cfg.crc = state->crc;
113

114 115
	rcar_du_writeback_setup(crtc, &cfg.writeback);

116
	vsp1_du_atomic_flush(crtc->vsp->vsp, crtc->vsp_pipe, &cfg);
117 118
}

119
static const u32 rcar_du_vsp_formats[] = {
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
	DRM_FORMAT_RGB332,
	DRM_FORMAT_ARGB4444,
	DRM_FORMAT_XRGB4444,
	DRM_FORMAT_ARGB1555,
	DRM_FORMAT_XRGB1555,
	DRM_FORMAT_RGB565,
	DRM_FORMAT_BGR888,
	DRM_FORMAT_RGB888,
	DRM_FORMAT_BGRA8888,
	DRM_FORMAT_BGRX8888,
	DRM_FORMAT_ARGB8888,
	DRM_FORMAT_XRGB8888,
	DRM_FORMAT_UYVY,
	DRM_FORMAT_YUYV,
	DRM_FORMAT_YVYU,
	DRM_FORMAT_NV12,
	DRM_FORMAT_NV21,
	DRM_FORMAT_NV16,
	DRM_FORMAT_NV61,
139 140 141 142 143 144
	DRM_FORMAT_YUV420,
	DRM_FORMAT_YVU420,
	DRM_FORMAT_YUV422,
	DRM_FORMAT_YVU422,
	DRM_FORMAT_YUV444,
	DRM_FORMAT_YVU444,
145 146 147 148 149 150
};

static void rcar_du_vsp_plane_setup(struct rcar_du_vsp_plane *plane)
{
	struct rcar_du_vsp_plane_state *state =
		to_rcar_vsp_plane_state(plane->plane.state);
151
	struct rcar_du_crtc *crtc = to_rcar_crtc(state->state.crtc);
152
	struct drm_framebuffer *fb = plane->plane.state->fb;
153
	const struct rcar_du_format_info *format;
154 155 156
	struct vsp1_du_atomic_config cfg = {
		.pixelformat = 0,
		.pitch = fb->pitches[0],
157
		.alpha = state->state.alpha >> 8,
158
		.zpos = state->state.zpos,
159
	};
160 161
	unsigned int i;

162 163 164 165
	cfg.src.left = state->state.src.x1 >> 16;
	cfg.src.top = state->state.src.y1 >> 16;
	cfg.src.width = drm_rect_width(&state->state.src) >> 16;
	cfg.src.height = drm_rect_height(&state->state.src) >> 16;
166

167 168 169 170
	cfg.dst.left = state->state.dst.x1;
	cfg.dst.top = state->state.dst.y1;
	cfg.dst.width = drm_rect_width(&state->state.dst);
	cfg.dst.height = drm_rect_height(&state->state.dst);
171

172 173 174
	for (i = 0; i < state->format->planes; ++i)
		cfg.mem[i] = sg_dma_address(state->sg_tables[i].sgl)
			   + fb->offsets[i];
175

176 177
	format = rcar_du_format_info(state->format->fourcc);
	cfg.pixelformat = format->v4l2;
178

179 180
	vsp1_du_atomic_update(plane->vsp->vsp, crtc->vsp_pipe,
			      plane->index, &cfg);
181 182
}

183 184
int rcar_du_vsp_map_fb(struct rcar_du_vsp *vsp, struct drm_framebuffer *fb,
		       struct sg_table sg_tables[3])
185 186 187 188 189
{
	struct rcar_du_device *rcdu = vsp->dev;
	unsigned int i;
	int ret;

190 191 192
	for (i = 0; i < fb->format->num_planes; ++i) {
		struct drm_gem_cma_object *gem = drm_fb_cma_get_gem_obj(fb, i);
		struct sg_table *sgt = &sg_tables[i];
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210

		ret = dma_get_sgtable(rcdu->dev, sgt, gem->vaddr, gem->paddr,
				      gem->base.size);
		if (ret)
			goto fail;

		ret = vsp1_du_map_sg(vsp->vsp, sgt);
		if (!ret) {
			sg_free_table(sgt);
			ret = -ENOMEM;
			goto fail;
		}
	}

	return 0;

fail:
	while (i--) {
211
		struct sg_table *sgt = &sg_tables[i];
212 213 214 215 216 217 218 219

		vsp1_du_unmap_sg(vsp->vsp, sgt);
		sg_free_table(sgt);
	}

	return ret;
}

220 221
static int rcar_du_vsp_plane_prepare_fb(struct drm_plane *plane,
					struct drm_plane_state *state)
222 223 224
{
	struct rcar_du_vsp_plane_state *rstate = to_rcar_vsp_plane_state(state);
	struct rcar_du_vsp *vsp = to_rcar_vsp_plane(plane)->vsp;
225
	int ret;
226

227 228 229 230
	/*
	 * There's no need to prepare (and unprepare) the framebuffer when the
	 * plane is not visible, as it will not be displayed.
	 */
231
	if (!state->visible)
232 233 234 235 236 237 238 239
		return 0;

	ret = rcar_du_vsp_map_fb(vsp, state->fb, rstate->sg_tables);
	if (ret < 0)
		return ret;

	return drm_gem_fb_prepare_fb(plane, state);
}
240

241 242 243 244 245 246 247
void rcar_du_vsp_unmap_fb(struct rcar_du_vsp *vsp, struct drm_framebuffer *fb,
			  struct sg_table sg_tables[3])
{
	unsigned int i;

	for (i = 0; i < fb->format->num_planes; ++i) {
		struct sg_table *sgt = &sg_tables[i];
248 249 250 251 252 253

		vsp1_du_unmap_sg(vsp->vsp, sgt);
		sg_free_table(sgt);
	}
}

254 255 256 257 258 259 260 261 262 263 264 265
static void rcar_du_vsp_plane_cleanup_fb(struct drm_plane *plane,
					 struct drm_plane_state *state)
{
	struct rcar_du_vsp_plane_state *rstate = to_rcar_vsp_plane_state(state);
	struct rcar_du_vsp *vsp = to_rcar_vsp_plane(plane)->vsp;

	if (!state->visible)
		return;

	rcar_du_vsp_unmap_fb(vsp, state->fb, rstate->sg_tables);
}

266 267 268 269 270
static int rcar_du_vsp_plane_atomic_check(struct drm_plane *plane,
					  struct drm_plane_state *state)
{
	struct rcar_du_vsp_plane_state *rstate = to_rcar_vsp_plane_state(state);

271
	return __rcar_du_plane_atomic_check(plane, state, &rstate->format);
272 273 274 275 276 277
}

static void rcar_du_vsp_plane_atomic_update(struct drm_plane *plane,
					struct drm_plane_state *old_state)
{
	struct rcar_du_vsp_plane *rplane = to_rcar_vsp_plane(plane);
278
	struct rcar_du_crtc *crtc = to_rcar_crtc(old_state->crtc);
279

280
	if (plane->state->visible)
281 282
		rcar_du_vsp_plane_setup(rplane);
	else
283 284
		vsp1_du_atomic_update(rplane->vsp->vsp, crtc->vsp_pipe,
				      rplane->index, NULL);
285 286 287
}

static const struct drm_plane_helper_funcs rcar_du_vsp_plane_helper_funcs = {
288 289
	.prepare_fb = rcar_du_vsp_plane_prepare_fb,
	.cleanup_fb = rcar_du_vsp_plane_cleanup_fb,
290 291 292 293 294 295 296 297 298 299 300 301
	.atomic_check = rcar_du_vsp_plane_atomic_check,
	.atomic_update = rcar_du_vsp_plane_atomic_update,
};

static struct drm_plane_state *
rcar_du_vsp_plane_atomic_duplicate_state(struct drm_plane *plane)
{
	struct rcar_du_vsp_plane_state *copy;

	if (WARN_ON(!plane->state))
		return NULL;

302
	copy = kzalloc(sizeof(*copy), GFP_KERNEL);
303 304 305 306 307 308 309 310 311 312 313
	if (copy == NULL)
		return NULL;

	__drm_atomic_helper_plane_duplicate_state(plane, &copy->state);

	return &copy->state;
}

static void rcar_du_vsp_plane_atomic_destroy_state(struct drm_plane *plane,
						   struct drm_plane_state *state)
{
314
	__drm_atomic_helper_plane_destroy_state(state);
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
	kfree(to_rcar_vsp_plane_state(state));
}

static void rcar_du_vsp_plane_reset(struct drm_plane *plane)
{
	struct rcar_du_vsp_plane_state *state;

	if (plane->state) {
		rcar_du_vsp_plane_atomic_destroy_state(plane, plane->state);
		plane->state = NULL;
	}

	state = kzalloc(sizeof(*state), GFP_KERNEL);
	if (state == NULL)
		return;

331
	__drm_atomic_helper_plane_reset(plane, &state->state);
332
	state->state.zpos = plane->type == DRM_PLANE_TYPE_PRIMARY ? 0 : 1;
333 334 335 336 337 338 339 340 341 342 343
}

static const struct drm_plane_funcs rcar_du_vsp_plane_funcs = {
	.update_plane = drm_atomic_helper_update_plane,
	.disable_plane = drm_atomic_helper_disable_plane,
	.reset = rcar_du_vsp_plane_reset,
	.destroy = drm_plane_cleanup,
	.atomic_duplicate_state = rcar_du_vsp_plane_atomic_duplicate_state,
	.atomic_destroy_state = rcar_du_vsp_plane_atomic_destroy_state,
};

344 345
int rcar_du_vsp_init(struct rcar_du_vsp *vsp, struct device_node *np,
		     unsigned int crtcs)
346 347 348
{
	struct rcar_du_device *rcdu = vsp->dev;
	struct platform_device *pdev;
349
	unsigned int num_crtcs = hweight32(crtcs);
350 351 352 353 354 355 356 357 358 359 360 361 362 363
	unsigned int i;
	int ret;

	/* Find the VSP device and initialize it. */
	pdev = of_find_device_by_node(np);
	if (!pdev)
		return -ENXIO;

	vsp->vsp = &pdev->dev;

	ret = vsp1_du_init(vsp->vsp);
	if (ret < 0)
		return ret;

364 365
	 /*
	  * The VSP2D (Gen3) has 5 RPFs, but the VSP1D (Gen2) is limited to
366
	  * 4 RPFs.
367
	  */
368
	vsp->num_planes = rcdu->info->gen >= 3 ? 5 : 4;
369 370 371 372 373 374 375

	vsp->planes = devm_kcalloc(rcdu->dev, vsp->num_planes,
				   sizeof(*vsp->planes), GFP_KERNEL);
	if (!vsp->planes)
		return -ENOMEM;

	for (i = 0; i < vsp->num_planes; ++i) {
376 377 378
		enum drm_plane_type type = i < num_crtcs
					 ? DRM_PLANE_TYPE_PRIMARY
					 : DRM_PLANE_TYPE_OVERLAY;
379 380 381 382 383
		struct rcar_du_vsp_plane *plane = &vsp->planes[i];

		plane->vsp = vsp;
		plane->index = i;

384
		ret = drm_universal_plane_init(rcdu->ddev, &plane->plane, crtcs,
385
					       &rcar_du_vsp_plane_funcs,
386 387
					       rcar_du_vsp_formats,
					       ARRAY_SIZE(rcar_du_vsp_formats),
388
					       NULL, type, NULL);
389 390 391 392 393 394 395 396 397
		if (ret < 0)
			return ret;

		drm_plane_helper_add(&plane->plane,
				     &rcar_du_vsp_plane_helper_funcs);

		if (type == DRM_PLANE_TYPE_PRIMARY)
			continue;

398
		drm_plane_create_alpha_property(&plane->plane);
399 400
		drm_plane_create_zpos_property(&plane->plane, 1, 1,
					       vsp->num_planes - 1);
401 402 403 404
	}

	return 0;
}