xref: /linux/drivers/gpu/drm/sun4i/sun4i_layer.c (revision 48dea9a700c8728cc31a1dd44588b97578de86ee)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2015 Free Electrons
4  * Copyright (C) 2015 NextThing Co
5  *
6  * Maxime Ripard <maxime.ripard@free-electrons.com>
7  */
8 
9 #include <drm/drm_atomic_helper.h>
10 #include <drm/drm_gem_framebuffer_helper.h>
11 #include <drm/drm_plane_helper.h>
12 
13 #include "sun4i_backend.h"
14 #include "sun4i_frontend.h"
15 #include "sun4i_layer.h"
16 #include "sunxi_engine.h"
17 
18 static void sun4i_backend_layer_reset(struct drm_plane *plane)
19 {
20 	struct sun4i_layer *layer = plane_to_sun4i_layer(plane);
21 	struct sun4i_layer_state *state;
22 
23 	if (plane->state) {
24 		state = state_to_sun4i_layer_state(plane->state);
25 
26 		__drm_atomic_helper_plane_destroy_state(&state->state);
27 
28 		kfree(state);
29 		plane->state = NULL;
30 	}
31 
32 	state = kzalloc(sizeof(*state), GFP_KERNEL);
33 	if (state) {
34 		__drm_atomic_helper_plane_reset(plane, &state->state);
35 		plane->state->zpos = layer->id;
36 	}
37 }
38 
39 static struct drm_plane_state *
40 sun4i_backend_layer_duplicate_state(struct drm_plane *plane)
41 {
42 	struct sun4i_layer_state *orig = state_to_sun4i_layer_state(plane->state);
43 	struct sun4i_layer_state *copy;
44 
45 	copy = kzalloc(sizeof(*copy), GFP_KERNEL);
46 	if (!copy)
47 		return NULL;
48 
49 	__drm_atomic_helper_plane_duplicate_state(plane, &copy->state);
50 	copy->uses_frontend = orig->uses_frontend;
51 
52 	return &copy->state;
53 }
54 
55 static void sun4i_backend_layer_destroy_state(struct drm_plane *plane,
56 					      struct drm_plane_state *state)
57 {
58 	struct sun4i_layer_state *s_state = state_to_sun4i_layer_state(state);
59 
60 	__drm_atomic_helper_plane_destroy_state(state);
61 
62 	kfree(s_state);
63 }
64 
65 static void sun4i_backend_layer_atomic_disable(struct drm_plane *plane,
66 					       struct drm_plane_state *old_state)
67 {
68 	struct sun4i_layer_state *layer_state = state_to_sun4i_layer_state(old_state);
69 	struct sun4i_layer *layer = plane_to_sun4i_layer(plane);
70 	struct sun4i_backend *backend = layer->backend;
71 
72 	sun4i_backend_layer_enable(backend, layer->id, false);
73 
74 	if (layer_state->uses_frontend) {
75 		unsigned long flags;
76 
77 		spin_lock_irqsave(&backend->frontend_lock, flags);
78 		backend->frontend_teardown = true;
79 		spin_unlock_irqrestore(&backend->frontend_lock, flags);
80 	}
81 }
82 
83 static void sun4i_backend_layer_atomic_update(struct drm_plane *plane,
84 					      struct drm_plane_state *old_state)
85 {
86 	struct sun4i_layer_state *layer_state = state_to_sun4i_layer_state(plane->state);
87 	struct sun4i_layer *layer = plane_to_sun4i_layer(plane);
88 	struct sun4i_backend *backend = layer->backend;
89 	struct sun4i_frontend *frontend = backend->frontend;
90 
91 	sun4i_backend_cleanup_layer(backend, layer->id);
92 
93 	if (layer_state->uses_frontend) {
94 		sun4i_frontend_init(frontend);
95 		sun4i_frontend_update_coord(frontend, plane);
96 		sun4i_frontend_update_buffer(frontend, plane);
97 		sun4i_frontend_update_formats(frontend, plane,
98 					      DRM_FORMAT_XRGB8888);
99 		sun4i_backend_update_layer_frontend(backend, layer->id,
100 						    DRM_FORMAT_XRGB8888);
101 		sun4i_frontend_enable(frontend);
102 	} else {
103 		sun4i_backend_update_layer_formats(backend, layer->id, plane);
104 		sun4i_backend_update_layer_buffer(backend, layer->id, plane);
105 	}
106 
107 	sun4i_backend_update_layer_coord(backend, layer->id, plane);
108 	sun4i_backend_update_layer_zpos(backend, layer->id, plane);
109 	sun4i_backend_layer_enable(backend, layer->id, true);
110 }
111 
112 static bool sun4i_layer_format_mod_supported(struct drm_plane *plane,
113 					     uint32_t format, uint64_t modifier)
114 {
115 	struct sun4i_layer *layer = plane_to_sun4i_layer(plane);
116 
117 	if (IS_ERR_OR_NULL(layer->backend->frontend))
118 		sun4i_backend_format_is_supported(format, modifier);
119 
120 	return sun4i_backend_format_is_supported(format, modifier) ||
121 	       sun4i_frontend_format_is_supported(format, modifier);
122 }
123 
124 static const struct drm_plane_helper_funcs sun4i_backend_layer_helper_funcs = {
125 	.prepare_fb	= drm_gem_fb_prepare_fb,
126 	.atomic_disable	= sun4i_backend_layer_atomic_disable,
127 	.atomic_update	= sun4i_backend_layer_atomic_update,
128 };
129 
130 static const struct drm_plane_funcs sun4i_backend_layer_funcs = {
131 	.atomic_destroy_state	= sun4i_backend_layer_destroy_state,
132 	.atomic_duplicate_state	= sun4i_backend_layer_duplicate_state,
133 	.destroy		= drm_plane_cleanup,
134 	.disable_plane		= drm_atomic_helper_disable_plane,
135 	.reset			= sun4i_backend_layer_reset,
136 	.update_plane		= drm_atomic_helper_update_plane,
137 	.format_mod_supported	= sun4i_layer_format_mod_supported,
138 };
139 
140 static const uint32_t sun4i_layer_formats[] = {
141 	DRM_FORMAT_ARGB8888,
142 	DRM_FORMAT_ARGB4444,
143 	DRM_FORMAT_ARGB1555,
144 	DRM_FORMAT_BGRX8888,
145 	DRM_FORMAT_RGBA5551,
146 	DRM_FORMAT_RGBA4444,
147 	DRM_FORMAT_RGB888,
148 	DRM_FORMAT_RGB565,
149 	DRM_FORMAT_NV12,
150 	DRM_FORMAT_NV16,
151 	DRM_FORMAT_NV21,
152 	DRM_FORMAT_NV61,
153 	DRM_FORMAT_UYVY,
154 	DRM_FORMAT_VYUY,
155 	DRM_FORMAT_XRGB8888,
156 	DRM_FORMAT_YUV411,
157 	DRM_FORMAT_YUV420,
158 	DRM_FORMAT_YUV422,
159 	DRM_FORMAT_YUV444,
160 	DRM_FORMAT_YUYV,
161 	DRM_FORMAT_YVU411,
162 	DRM_FORMAT_YVU420,
163 	DRM_FORMAT_YVU422,
164 	DRM_FORMAT_YVU444,
165 	DRM_FORMAT_YVYU,
166 };
167 
168 static const uint32_t sun4i_backend_layer_formats[] = {
169 	DRM_FORMAT_ARGB8888,
170 	DRM_FORMAT_ARGB4444,
171 	DRM_FORMAT_ARGB1555,
172 	DRM_FORMAT_RGBA5551,
173 	DRM_FORMAT_RGBA4444,
174 	DRM_FORMAT_RGB888,
175 	DRM_FORMAT_RGB565,
176 	DRM_FORMAT_UYVY,
177 	DRM_FORMAT_VYUY,
178 	DRM_FORMAT_XRGB8888,
179 	DRM_FORMAT_YUYV,
180 	DRM_FORMAT_YVYU,
181 };
182 
183 static const uint64_t sun4i_layer_modifiers[] = {
184 	DRM_FORMAT_MOD_LINEAR,
185 	DRM_FORMAT_MOD_ALLWINNER_TILED,
186 	DRM_FORMAT_MOD_INVALID
187 };
188 
189 static struct sun4i_layer *sun4i_layer_init_one(struct drm_device *drm,
190 						struct sun4i_backend *backend,
191 						enum drm_plane_type type)
192 {
193 	const uint64_t *modifiers = sun4i_layer_modifiers;
194 	const uint32_t *formats = sun4i_layer_formats;
195 	unsigned int formats_len = ARRAY_SIZE(sun4i_layer_formats);
196 	struct sun4i_layer *layer;
197 	int ret;
198 
199 	layer = devm_kzalloc(drm->dev, sizeof(*layer), GFP_KERNEL);
200 	if (!layer)
201 		return ERR_PTR(-ENOMEM);
202 
203 	layer->backend = backend;
204 
205 	if (IS_ERR_OR_NULL(backend->frontend)) {
206 		formats = sun4i_backend_layer_formats;
207 		formats_len = ARRAY_SIZE(sun4i_backend_layer_formats);
208 		modifiers = NULL;
209 	}
210 
211 	/* possible crtcs are set later */
212 	ret = drm_universal_plane_init(drm, &layer->plane, 0,
213 				       &sun4i_backend_layer_funcs,
214 				       formats, formats_len,
215 				       modifiers, type, NULL);
216 	if (ret) {
217 		dev_err(drm->dev, "Couldn't initialize layer\n");
218 		return ERR_PTR(ret);
219 	}
220 
221 	drm_plane_helper_add(&layer->plane,
222 			     &sun4i_backend_layer_helper_funcs);
223 
224 	drm_plane_create_alpha_property(&layer->plane);
225 	drm_plane_create_zpos_property(&layer->plane, 0, 0,
226 				       SUN4I_BACKEND_NUM_LAYERS - 1);
227 
228 	return layer;
229 }
230 
231 struct drm_plane **sun4i_layers_init(struct drm_device *drm,
232 				     struct sunxi_engine *engine)
233 {
234 	struct drm_plane **planes;
235 	struct sun4i_backend *backend = engine_to_sun4i_backend(engine);
236 	int i;
237 
238 	/* We need to have a sentinel at the need, hence the overallocation */
239 	planes = devm_kcalloc(drm->dev, SUN4I_BACKEND_NUM_LAYERS + 1,
240 			      sizeof(*planes), GFP_KERNEL);
241 	if (!planes)
242 		return ERR_PTR(-ENOMEM);
243 
244 	for (i = 0; i < SUN4I_BACKEND_NUM_LAYERS; i++) {
245 		enum drm_plane_type type = i ? DRM_PLANE_TYPE_OVERLAY : DRM_PLANE_TYPE_PRIMARY;
246 		struct sun4i_layer *layer;
247 
248 		layer = sun4i_layer_init_one(drm, backend, type);
249 		if (IS_ERR(layer)) {
250 			dev_err(drm->dev, "Couldn't initialize %s plane\n",
251 				i ? "overlay" : "primary");
252 			return ERR_CAST(layer);
253 		}
254 
255 		layer->id = i;
256 		planes[i] = &layer->plane;
257 	}
258 
259 	return planes;
260 }
261