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