omap_plane.c 9.7 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
2
/*
3
 * Copyright (C) 2011 Texas Instruments Incorporated - https://www.ti.com/
4 5 6
 * Author: Rob Clark <rob.clark@linaro.org>
 */

7
#include <drm/drm_atomic.h>
8
#include <drm/drm_atomic_helper.h>
9
#include <drm/drm_gem_atomic_helper.h>
10
#include <drm/drm_plane_helper.h>
11

12
#include "omap_dmm_tiler.h"
13
#include "omap_drv.h"
14 15 16 17 18 19 20 21 22

/*
 * plane funcs
 */

#define to_omap_plane(x) container_of(x, struct omap_plane, base)

struct omap_plane {
	struct drm_plane base;
23
	enum omap_plane_id id;
24
	const char *name;
25
};
26

27
static int omap_plane_prepare_fb(struct drm_plane *plane,
28
				 struct drm_plane_state *new_state)
29
{
30 31 32
	if (!new_state->fb)
		return 0;

33 34
	drm_gem_plane_helper_prepare_fb(plane, new_state);

35
	return omap_framebuffer_pin(new_state->fb);
36 37 38
}

static void omap_plane_cleanup_fb(struct drm_plane *plane,
39
				  struct drm_plane_state *old_state)
40
{
41 42
	if (old_state->fb)
		omap_framebuffer_unpin(old_state->fb);
43 44 45
}

static void omap_plane_atomic_update(struct drm_plane *plane,
46
				     struct drm_atomic_state *state)
47
{
T
Tomi Valkeinen 已提交
48
	struct omap_drm_private *priv = plane->dev->dev_private;
49
	struct omap_plane *omap_plane = to_omap_plane(plane);
50 51
	struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
									   plane);
52
	struct omap_overlay_info info;
53
	int ret;
54

55 56
	DBG("%s, crtc=%p fb=%p", omap_plane->name, new_state->crtc,
	    new_state->fb);
57

58
	memset(&info, 0, sizeof(info));
59
	info.rotation_type = OMAP_DSS_ROT_NONE;
60
	info.rotation = DRM_MODE_ROTATE_0;
61 62 63
	info.global_alpha = new_state->alpha >> 8;
	info.zorder = new_state->normalized_zpos;
	if (new_state->pixel_blend_mode == DRM_MODE_BLEND_PREMULTI)
64 65 66
		info.pre_mult_alpha = 1;
	else
		info.pre_mult_alpha = 0;
67 68
	info.color_encoding = new_state->color_encoding;
	info.color_range = new_state->color_range;
69

70
	/* update scanout: */
71
	omap_framebuffer_update_scanout(new_state->fb, new_state, &info);
72

73 74 75 76 77
	DBG("%dx%d -> %dx%d (%d)", info.width, info.height,
			info.out_width, info.out_height,
			info.screen_width);
	DBG("%d,%d %pad %pad", info.pos_x, info.pos_y,
			&info.paddr, &info.p_uv_addr);
78 79

	/* and finally, update omapdss: */
T
Tomi Valkeinen 已提交
80
	ret = dispc_ovl_setup(priv->dispc, omap_plane->id, &info,
81 82
			      omap_crtc_timings(new_state->crtc), false,
			      omap_crtc_channel(new_state->crtc));
83 84 85
	if (ret) {
		dev_err(plane->dev->dev, "Failed to setup plane %s\n",
			omap_plane->name);
T
Tomi Valkeinen 已提交
86
		dispc_ovl_enable(priv->dispc, omap_plane->id, false);
87
		return;
88
	}
89

T
Tomi Valkeinen 已提交
90
	dispc_ovl_enable(priv->dispc, omap_plane->id, true);
91 92 93
}

static void omap_plane_atomic_disable(struct drm_plane *plane,
94
				      struct drm_atomic_state *state)
95
{
96 97
	struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
									   plane);
T
Tomi Valkeinen 已提交
98
	struct omap_drm_private *priv = plane->dev->dev_private;
99
	struct omap_plane *omap_plane = to_omap_plane(plane);
100

101 102
	new_state->rotation = DRM_MODE_ROTATE_0;
	new_state->zpos = plane->type == DRM_PLANE_TYPE_PRIMARY ? 0 : omap_plane->id;
103

T
Tomi Valkeinen 已提交
104
	dispc_ovl_enable(priv->dispc, omap_plane->id, false);
105 106
}

107 108
#define FRAC_16_16(mult, div)    (((mult) << 16) / (div))

109
static int omap_plane_atomic_check(struct drm_plane *plane,
110
				   struct drm_atomic_state *state)
111
{
112 113
	struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
										 plane);
114
	struct drm_crtc_state *crtc_state;
115
	int ret;
116

117
	if (!new_plane_state->fb)
118 119
		return 0;

120 121
	/* crtc should only be NULL when disabling (i.e., !new_plane_state->fb) */
	if (WARN_ON(!new_plane_state->crtc))
122 123
		return 0;

124
	crtc_state = drm_atomic_get_existing_crtc_state(state,
125
							new_plane_state->crtc);
126 127 128
	/* we should have a crtc state if the plane is attached to a crtc */
	if (WARN_ON(!crtc_state))
		return 0;
129

130 131 132
	if (!crtc_state->enable)
		return 0;

133 134 135 136 137 138 139 140 141 142 143 144
	/*
	 * Note: these are just sanity checks to filter out totally bad scaling
	 * factors. The real limits must be calculated case by case, and
	 * unfortunately we currently do those checks only at the commit
	 * phase in dispc.
	 */
	ret = drm_atomic_helper_check_plane_state(new_plane_state, crtc_state,
						  FRAC_16_16(1, 8), FRAC_16_16(8, 1),
						  true, true);
	if (ret)
		return ret;

145
	if (new_plane_state->crtc_x < 0 || new_plane_state->crtc_y < 0)
146 147
		return -EINVAL;

148
	if (new_plane_state->crtc_x + new_plane_state->crtc_w > crtc_state->adjusted_mode.hdisplay)
149 150
		return -EINVAL;

151
	if (new_plane_state->crtc_y + new_plane_state->crtc_h > crtc_state->adjusted_mode.vdisplay)
152 153
		return -EINVAL;

154 155
	if (new_plane_state->rotation != DRM_MODE_ROTATE_0 &&
	    !omap_framebuffer_supports_rotation(new_plane_state->fb))
156
		return -EINVAL;
157

158 159 160
	return 0;
}

161 162 163
static const struct drm_plane_helper_funcs omap_plane_helper_funcs = {
	.prepare_fb = omap_plane_prepare_fb,
	.cleanup_fb = omap_plane_cleanup_fb,
164
	.atomic_check = omap_plane_atomic_check,
165 166 167 168
	.atomic_update = omap_plane_atomic_update,
	.atomic_disable = omap_plane_atomic_disable,
};

169 170 171
static void omap_plane_destroy(struct drm_plane *plane)
{
	struct omap_plane *omap_plane = to_omap_plane(plane);
172 173 174

	DBG("%s", omap_plane->name);

175
	drm_plane_cleanup(plane);
176

177 178 179
	kfree(omap_plane);
}

180 181 182 183 184 185 186
/* helper to install properties which are common to planes and crtcs */
void omap_plane_install_properties(struct drm_plane *plane,
		struct drm_mode_object *obj)
{
	struct drm_device *dev = plane->dev;
	struct omap_drm_private *priv = dev->dev_private;

187
	if (priv->has_dmm) {
188 189
		if (!plane->rotation_property)
			drm_plane_create_rotation_property(plane,
190 191 192 193
							   DRM_MODE_ROTATE_0,
							   DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_90 |
							   DRM_MODE_ROTATE_180 | DRM_MODE_ROTATE_270 |
							   DRM_MODE_REFLECT_X | DRM_MODE_REFLECT_Y);
194 195 196 197

		/* Attach the rotation property also to the crtc object */
		if (plane->rotation_property && obj != &plane->base)
			drm_object_attach_property(obj, plane->rotation_property,
198
						   DRM_MODE_ROTATE_0);
199
	}
200

201
	drm_object_attach_property(obj, priv->zorder_prop, 0);
202 203
}

204 205 206 207
static void omap_plane_reset(struct drm_plane *plane)
{
	struct omap_plane *omap_plane = to_omap_plane(plane);

208 209
	drm_atomic_helper_plane_reset(plane);
	if (!plane->state)
210 211 212
		return;

	/*
213
	 * Set the zpos default depending on whether we are a primary or overlay
214 215
	 * plane.
	 */
216 217
	plane->state->zpos = plane->type == DRM_PLANE_TYPE_PRIMARY
			   ? 0 : omap_plane->id;
218 219
	plane->state->color_encoding = DRM_COLOR_YCBCR_BT601;
	plane->state->color_range = DRM_COLOR_YCBCR_FULL_RANGE;
220 221
}

222 223 224
static int omap_plane_atomic_set_property(struct drm_plane *plane,
					  struct drm_plane_state *state,
					  struct drm_property *property,
225
					  u64 val)
226 227 228
{
	struct omap_drm_private *priv = plane->dev->dev_private;

229
	if (property == priv->zorder_prop)
230
		state->zpos = val;
231
	else
232
		return -EINVAL;
233

234 235
	return 0;
}
236

237 238 239
static int omap_plane_atomic_get_property(struct drm_plane *plane,
					  const struct drm_plane_state *state,
					  struct drm_property *property,
240
					  u64 *val)
241 242 243 244
{
	struct omap_drm_private *priv = plane->dev->dev_private;

	if (property == priv->zorder_prop)
245
		*val = state->zpos;
246 247
	else
		return -EINVAL;
248

249
	return 0;
250 251
}

252
static const struct drm_plane_funcs omap_plane_funcs = {
253 254
	.update_plane = drm_atomic_helper_update_plane,
	.disable_plane = drm_atomic_helper_disable_plane,
255
	.reset = omap_plane_reset,
256
	.destroy = omap_plane_destroy,
257 258
	.atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
	.atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
259 260
	.atomic_set_property = omap_plane_atomic_set_property,
	.atomic_get_property = omap_plane_atomic_get_property,
261 262
};

263 264 265 266
static bool omap_plane_supports_yuv(struct drm_plane *plane)
{
	struct omap_drm_private *priv = plane->dev->dev_private;
	struct omap_plane *omap_plane = to_omap_plane(plane);
T
Tomi Valkeinen 已提交
267
	const u32 *formats = dispc_ovl_get_color_modes(priv->dispc, omap_plane->id);
268 269 270 271 272 273 274 275 276 277 278
	u32 i;

	for (i = 0; formats[i]; i++)
		if (formats[i] == DRM_FORMAT_YUYV ||
		    formats[i] == DRM_FORMAT_UYVY ||
		    formats[i] == DRM_FORMAT_NV12)
			return true;

	return false;
}

279
static const char *plane_id_to_name[] = {
280 281 282 283
	[OMAP_DSS_GFX] = "gfx",
	[OMAP_DSS_VIDEO1] = "vid1",
	[OMAP_DSS_VIDEO2] = "vid2",
	[OMAP_DSS_VIDEO3] = "vid3",
284 285
};

286 287 288 289 290 291 292
static const enum omap_plane_id plane_idx_to_id[] = {
	OMAP_DSS_GFX,
	OMAP_DSS_VIDEO1,
	OMAP_DSS_VIDEO2,
	OMAP_DSS_VIDEO3,
};

293 294
/* initialize plane */
struct drm_plane *omap_plane_init(struct drm_device *dev,
295
		int idx, enum drm_plane_type type,
296
		u32 possible_crtcs)
297
{
T
Tomi Valkeinen 已提交
298
	struct omap_drm_private *priv = dev->dev_private;
T
Tomi Valkeinen 已提交
299
	unsigned int num_planes = dispc_get_num_ovls(priv->dispc);
300
	struct drm_plane *plane;
301
	struct omap_plane *omap_plane;
302
	enum omap_plane_id id;
303
	int ret;
304 305
	u32 nformats;
	const u32 *formats;
306

307 308 309 310 311 312
	if (WARN_ON(idx >= ARRAY_SIZE(plane_idx_to_id)))
		return ERR_PTR(-EINVAL);

	id = plane_idx_to_id[idx];

	DBG("%s: type=%d", plane_id_to_name[id], type);
313

314
	omap_plane = kzalloc(sizeof(*omap_plane), GFP_KERNEL);
315
	if (!omap_plane)
316
		return ERR_PTR(-ENOMEM);
317

T
Tomi Valkeinen 已提交
318
	formats = dispc_ovl_get_color_modes(priv->dispc, id);
319 320
	for (nformats = 0; formats[nformats]; ++nformats)
		;
321
	omap_plane->id = id;
322
	omap_plane->name = plane_id_to_name[id];
323

324 325
	plane = &omap_plane->base;

326
	ret = drm_universal_plane_init(dev, plane, possible_crtcs,
327
				       &omap_plane_funcs, formats,
328
				       nformats, NULL, type, NULL);
329 330
	if (ret < 0)
		goto error;
331

332 333
	drm_plane_helper_add(plane, &omap_plane_helper_funcs);

334
	omap_plane_install_properties(plane, &plane->base);
335
	drm_plane_create_zpos_property(plane, 0, 0, num_planes - 1);
336 337 338
	drm_plane_create_alpha_property(plane);
	drm_plane_create_blend_mode_property(plane, BIT(DRM_MODE_BLEND_PREMULTI) |
					     BIT(DRM_MODE_BLEND_COVERAGE));
339

340 341 342 343 344 345 346 347 348
	if (omap_plane_supports_yuv(plane))
		drm_plane_create_color_properties(plane,
						  BIT(DRM_COLOR_YCBCR_BT601) |
						  BIT(DRM_COLOR_YCBCR_BT709),
						  BIT(DRM_COLOR_YCBCR_FULL_RANGE) |
						  BIT(DRM_COLOR_YCBCR_LIMITED_RANGE),
						  DRM_COLOR_YCBCR_BT601,
						  DRM_COLOR_YCBCR_FULL_RANGE);

349
	return plane;
350 351

error:
352 353 354
	dev_err(dev->dev, "%s(): could not create plane: %s\n",
		__func__, plane_id_to_name[id]);

355 356
	kfree(omap_plane);
	return NULL;
357
}