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