1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * (C) COPYRIGHT 2018 ARM Limited. All rights reserved. 4 * Author: James.Qian.Wang <james.qian.wang@arm.com> 5 * 6 */ 7 #include <drm/drm_atomic.h> 8 #include <drm/drm_atomic_helper.h> 9 #include <drm/drm_blend.h> 10 #include <drm/drm_plane_helper.h> 11 #include <drm/drm_print.h> 12 #include "komeda_dev.h" 13 #include "komeda_kms.h" 14 #include "komeda_framebuffer.h" 15 16 static int 17 komeda_plane_init_data_flow(struct drm_plane_state *st, 18 struct komeda_crtc_state *kcrtc_st, 19 struct komeda_data_flow_cfg *dflow) 20 { 21 struct komeda_plane *kplane = to_kplane(st->plane); 22 struct drm_framebuffer *fb = st->fb; 23 const struct komeda_format_caps *caps = to_kfb(fb)->format_caps; 24 struct komeda_pipeline *pipe = kplane->layer->base.pipeline; 25 26 memset(dflow, 0, sizeof(*dflow)); 27 28 dflow->blending_zorder = st->normalized_zpos; 29 if (pipe == to_kcrtc(st->crtc)->master) 30 dflow->blending_zorder -= kcrtc_st->max_slave_zorder; 31 if (dflow->blending_zorder < 0) { 32 DRM_DEBUG_ATOMIC("%s zorder:%d < max_slave_zorder: %d.\n", 33 st->plane->name, st->normalized_zpos, 34 kcrtc_st->max_slave_zorder); 35 return -EINVAL; 36 } 37 38 dflow->pixel_blend_mode = st->pixel_blend_mode; 39 dflow->layer_alpha = st->alpha >> 8; 40 41 dflow->out_x = st->crtc_x; 42 dflow->out_y = st->crtc_y; 43 dflow->out_w = st->crtc_w; 44 dflow->out_h = st->crtc_h; 45 46 dflow->in_x = st->src_x >> 16; 47 dflow->in_y = st->src_y >> 16; 48 dflow->in_w = st->src_w >> 16; 49 dflow->in_h = st->src_h >> 16; 50 51 dflow->rot = drm_rotation_simplify(st->rotation, caps->supported_rots); 52 if (!has_bits(dflow->rot, caps->supported_rots)) { 53 DRM_DEBUG_ATOMIC("rotation(0x%x) isn't supported by %p4cc with modifier: 0x%llx.\n", 54 dflow->rot, &caps->fourcc, fb->modifier); 55 return -EINVAL; 56 } 57 58 komeda_complete_data_flow_cfg(kplane->layer, dflow, fb); 59 60 return 0; 61 } 62 63 /** 64 * komeda_plane_atomic_check - build input data flow 65 * @plane: DRM plane 66 * @state: the plane state object 67 * 68 * RETURNS: 69 * Zero for success or -errno 70 */ 71 static int 72 komeda_plane_atomic_check(struct drm_plane *plane, 73 struct drm_atomic_state *state) 74 { 75 struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state, 76 plane); 77 struct komeda_plane *kplane = to_kplane(plane); 78 struct komeda_plane_state *kplane_st = to_kplane_st(new_plane_state); 79 struct komeda_layer *layer = kplane->layer; 80 struct drm_crtc_state *crtc_st; 81 struct komeda_crtc_state *kcrtc_st; 82 struct komeda_data_flow_cfg dflow; 83 int err; 84 85 if (!new_plane_state->crtc || !new_plane_state->fb) 86 return 0; 87 88 crtc_st = drm_atomic_get_crtc_state(state, 89 new_plane_state->crtc); 90 if (IS_ERR(crtc_st) || !crtc_st->enable) { 91 DRM_DEBUG_ATOMIC("Cannot update plane on a disabled CRTC.\n"); 92 return -EINVAL; 93 } 94 95 /* crtc is inactive, skip the resource assignment */ 96 if (!crtc_st->active) 97 return 0; 98 99 kcrtc_st = to_kcrtc_st(crtc_st); 100 101 err = komeda_plane_init_data_flow(new_plane_state, kcrtc_st, &dflow); 102 if (err) 103 return err; 104 105 if (dflow.en_split) 106 err = komeda_build_layer_split_data_flow(layer, 107 kplane_st, kcrtc_st, &dflow); 108 else 109 err = komeda_build_layer_data_flow(layer, 110 kplane_st, kcrtc_st, &dflow); 111 112 return err; 113 } 114 115 /* plane doesn't represent a real HW, so there is no HW update for plane. 116 * komeda handles all the HW update in crtc->atomic_flush 117 */ 118 static void 119 komeda_plane_atomic_update(struct drm_plane *plane, 120 struct drm_atomic_state *state) 121 { 122 } 123 124 static const struct drm_plane_helper_funcs komeda_plane_helper_funcs = { 125 .atomic_check = komeda_plane_atomic_check, 126 .atomic_update = komeda_plane_atomic_update, 127 }; 128 129 static void komeda_plane_destroy(struct drm_plane *plane) 130 { 131 drm_plane_cleanup(plane); 132 133 kfree(to_kplane(plane)); 134 } 135 136 static void komeda_plane_reset(struct drm_plane *plane) 137 { 138 struct komeda_plane_state *state; 139 140 if (plane->state) 141 __drm_atomic_helper_plane_destroy_state(plane->state); 142 143 kfree(plane->state); 144 plane->state = NULL; 145 146 state = kzalloc(sizeof(*state), GFP_KERNEL); 147 if (state) 148 __drm_atomic_helper_plane_reset(plane, &state->base); 149 } 150 151 static struct drm_plane_state * 152 komeda_plane_atomic_duplicate_state(struct drm_plane *plane) 153 { 154 struct komeda_plane_state *new; 155 156 if (WARN_ON(!plane->state)) 157 return NULL; 158 159 new = kzalloc(sizeof(*new), GFP_KERNEL); 160 if (!new) 161 return NULL; 162 163 __drm_atomic_helper_plane_duplicate_state(plane, &new->base); 164 165 return &new->base; 166 } 167 168 static void 169 komeda_plane_atomic_destroy_state(struct drm_plane *plane, 170 struct drm_plane_state *state) 171 { 172 __drm_atomic_helper_plane_destroy_state(state); 173 kfree(to_kplane_st(state)); 174 } 175 176 static bool 177 komeda_plane_format_mod_supported(struct drm_plane *plane, 178 u32 format, u64 modifier) 179 { 180 struct komeda_dev *mdev = plane->dev->dev_private; 181 struct komeda_plane *kplane = to_kplane(plane); 182 u32 layer_type = kplane->layer->layer_type; 183 184 return komeda_format_mod_supported(&mdev->fmt_tbl, layer_type, 185 format, modifier, 0); 186 } 187 188 static const struct drm_plane_funcs komeda_plane_funcs = { 189 .update_plane = drm_atomic_helper_update_plane, 190 .disable_plane = drm_atomic_helper_disable_plane, 191 .destroy = komeda_plane_destroy, 192 .reset = komeda_plane_reset, 193 .atomic_duplicate_state = komeda_plane_atomic_duplicate_state, 194 .atomic_destroy_state = komeda_plane_atomic_destroy_state, 195 .format_mod_supported = komeda_plane_format_mod_supported, 196 }; 197 198 /* for komeda, which is pipeline can be share between crtcs */ 199 static u32 get_possible_crtcs(struct komeda_kms_dev *kms, 200 struct komeda_pipeline *pipe) 201 { 202 struct komeda_crtc *crtc; 203 u32 possible_crtcs = 0; 204 int i; 205 206 for (i = 0; i < kms->n_crtcs; i++) { 207 crtc = &kms->crtcs[i]; 208 209 if ((pipe == crtc->master) || (pipe == crtc->slave)) 210 possible_crtcs |= BIT(i); 211 } 212 213 return possible_crtcs; 214 } 215 216 static void 217 komeda_set_crtc_plane_mask(struct komeda_kms_dev *kms, 218 struct komeda_pipeline *pipe, 219 struct drm_plane *plane) 220 { 221 struct komeda_crtc *kcrtc; 222 int i; 223 224 for (i = 0; i < kms->n_crtcs; i++) { 225 kcrtc = &kms->crtcs[i]; 226 227 if (pipe == kcrtc->slave) 228 kcrtc->slave_planes |= BIT(drm_plane_index(plane)); 229 } 230 } 231 232 /* use Layer0 as primary */ 233 static u32 get_plane_type(struct komeda_kms_dev *kms, 234 struct komeda_component *c) 235 { 236 bool is_primary = (c->id == KOMEDA_COMPONENT_LAYER0); 237 238 return is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY; 239 } 240 241 static int komeda_plane_add(struct komeda_kms_dev *kms, 242 struct komeda_layer *layer) 243 { 244 struct komeda_dev *mdev = kms->base.dev_private; 245 struct komeda_component *c = &layer->base; 246 struct komeda_plane *kplane; 247 struct drm_plane *plane; 248 u32 *formats, n_formats = 0; 249 int err; 250 251 kplane = kzalloc(sizeof(*kplane), GFP_KERNEL); 252 if (!kplane) 253 return -ENOMEM; 254 255 plane = &kplane->base; 256 kplane->layer = layer; 257 258 formats = komeda_get_layer_fourcc_list(&mdev->fmt_tbl, 259 layer->layer_type, &n_formats); 260 if (!formats) { 261 kfree(kplane); 262 return -ENOMEM; 263 } 264 265 err = drm_universal_plane_init(&kms->base, plane, 266 get_possible_crtcs(kms, c->pipeline), 267 &komeda_plane_funcs, 268 formats, n_formats, komeda_supported_modifiers, 269 get_plane_type(kms, c), 270 "%s", c->name); 271 272 komeda_put_fourcc_list(formats); 273 274 if (err) { 275 kfree(kplane); 276 return err; 277 } 278 279 drm_plane_helper_add(plane, &komeda_plane_helper_funcs); 280 281 err = drm_plane_create_rotation_property(plane, DRM_MODE_ROTATE_0, 282 layer->supported_rots); 283 if (err) 284 goto cleanup; 285 286 err = drm_plane_create_alpha_property(plane); 287 if (err) 288 goto cleanup; 289 290 err = drm_plane_create_blend_mode_property(plane, 291 BIT(DRM_MODE_BLEND_PIXEL_NONE) | 292 BIT(DRM_MODE_BLEND_PREMULTI) | 293 BIT(DRM_MODE_BLEND_COVERAGE)); 294 if (err) 295 goto cleanup; 296 297 err = drm_plane_create_color_properties(plane, 298 BIT(DRM_COLOR_YCBCR_BT601) | 299 BIT(DRM_COLOR_YCBCR_BT709) | 300 BIT(DRM_COLOR_YCBCR_BT2020), 301 BIT(DRM_COLOR_YCBCR_LIMITED_RANGE) | 302 BIT(DRM_COLOR_YCBCR_FULL_RANGE), 303 DRM_COLOR_YCBCR_BT601, 304 DRM_COLOR_YCBCR_LIMITED_RANGE); 305 if (err) 306 goto cleanup; 307 308 err = drm_plane_create_zpos_property(plane, layer->base.id, 0, 8); 309 if (err) 310 goto cleanup; 311 312 komeda_set_crtc_plane_mask(kms, c->pipeline, plane); 313 314 return 0; 315 cleanup: 316 komeda_plane_destroy(plane); 317 return err; 318 } 319 320 int komeda_kms_add_planes(struct komeda_kms_dev *kms, struct komeda_dev *mdev) 321 { 322 struct komeda_pipeline *pipe; 323 int i, j, err; 324 325 for (i = 0; i < mdev->n_pipelines; i++) { 326 pipe = mdev->pipelines[i]; 327 328 for (j = 0; j < pipe->n_layers; j++) { 329 err = komeda_plane_add(kms, pipe->layers[j]); 330 if (err) 331 return err; 332 } 333 } 334 335 return 0; 336 } 337