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