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