1 // SPDX-License-Identifier: GPL-2.0
3 * (C) COPYRIGHT 2018 ARM Limited. All rights reserved.
4 * Author: James.Qian.Wang <james.qian.wang@arm.com>
8 #include <drm/drm_print.h>
10 #include "komeda_dev.h"
11 #include "komeda_kms.h"
12 #include "komeda_pipeline.h"
13 #include "komeda_framebuffer.h"
15 static inline bool is_switching_user(void *old, void *new)
23 static struct komeda_pipeline_state *
24 komeda_pipeline_get_state(struct komeda_pipeline *pipe,
25 struct drm_atomic_state *state)
27 struct drm_private_state *priv_st;
29 priv_st = drm_atomic_get_private_obj_state(state, &pipe->obj);
31 return ERR_CAST(priv_st);
33 return priv_to_pipe_st(priv_st);
36 struct komeda_pipeline_state *
37 komeda_pipeline_get_old_state(struct komeda_pipeline *pipe,
38 struct drm_atomic_state *state)
40 struct drm_private_state *priv_st;
42 priv_st = drm_atomic_get_old_private_obj_state(state, &pipe->obj);
44 return priv_to_pipe_st(priv_st);
48 static struct komeda_pipeline_state *
49 komeda_pipeline_get_new_state(struct komeda_pipeline *pipe,
50 struct drm_atomic_state *state)
52 struct drm_private_state *priv_st;
54 priv_st = drm_atomic_get_new_private_obj_state(state, &pipe->obj);
56 return priv_to_pipe_st(priv_st);
60 /* Assign pipeline for crtc */
61 static struct komeda_pipeline_state *
62 komeda_pipeline_get_state_and_set_crtc(struct komeda_pipeline *pipe,
63 struct drm_atomic_state *state,
64 struct drm_crtc *crtc)
66 struct komeda_pipeline_state *st;
68 st = komeda_pipeline_get_state(pipe, state);
72 if (is_switching_user(crtc, st->crtc)) {
73 DRM_DEBUG_ATOMIC("CRTC%d required pipeline%d is busy.\n",
74 drm_crtc_index(crtc), pipe->id);
75 return ERR_PTR(-EBUSY);
78 /* pipeline only can be disabled when the it is free or unused */
79 if (!crtc && st->active_comps) {
80 DRM_DEBUG_ATOMIC("Disabling a busy pipeline:%d.\n", pipe->id);
81 return ERR_PTR(-EBUSY);
87 struct komeda_crtc_state *kcrtc_st;
89 kcrtc_st = to_kcrtc_st(drm_atomic_get_new_crtc_state(state,
92 kcrtc_st->active_pipes |= BIT(pipe->id);
93 kcrtc_st->affected_pipes |= BIT(pipe->id);
98 static struct komeda_component_state *
99 komeda_component_get_state(struct komeda_component *c,
100 struct drm_atomic_state *state)
102 struct drm_private_state *priv_st;
104 WARN_ON(!drm_modeset_is_locked(&c->pipeline->obj.lock));
106 priv_st = drm_atomic_get_private_obj_state(state, &c->obj);
108 return ERR_CAST(priv_st);
110 return priv_to_comp_st(priv_st);
113 static struct komeda_component_state *
114 komeda_component_get_old_state(struct komeda_component *c,
115 struct drm_atomic_state *state)
117 struct drm_private_state *priv_st;
119 priv_st = drm_atomic_get_old_private_obj_state(state, &c->obj);
121 return priv_to_comp_st(priv_st);
126 * komeda_component_get_state_and_set_user()
128 * @c: component to get state and set user
129 * @state: global atomic state
130 * @user: direct user, the binding user
131 * @crtc: the CRTC user, the big boss :)
133 * This function accepts two users:
134 * - The direct user: can be plane/crtc/wb_connector depends on component
135 * - The big boss (CRTC)
136 * CRTC is the big boss (the final user), because all component resources
137 * eventually will be assigned to CRTC, like the layer will be binding to
138 * kms_plane, but kms plane will be binding to a CRTC eventually.
140 * The big boss (CRTC) is for pipeline assignment, since &komeda_component isn't
141 * independent and can be assigned to CRTC freely, but belongs to a specific
142 * pipeline, only pipeline can be shared between crtc, and pipeline as a whole
143 * (include all the internal components) assigned to a specific CRTC.
145 * So when set a user to komeda_component, need first to check the status of
146 * component->pipeline to see if the pipeline is available on this specific
147 * CRTC. if the pipeline is busy (assigned to another CRTC), even the required
148 * component is free, the component still cannot be assigned to the direct user.
150 static struct komeda_component_state *
151 komeda_component_get_state_and_set_user(struct komeda_component *c,
152 struct drm_atomic_state *state,
154 struct drm_crtc *crtc)
156 struct komeda_pipeline_state *pipe_st;
157 struct komeda_component_state *st;
159 /* First check if the pipeline is available */
160 pipe_st = komeda_pipeline_get_state_and_set_crtc(c->pipeline,
163 return ERR_CAST(pipe_st);
165 st = komeda_component_get_state(c, state);
169 /* check if the component has been occupied */
170 if (is_switching_user(user, st->binding_user)) {
171 DRM_DEBUG_ATOMIC("required %s is busy.\n", c->name);
172 return ERR_PTR(-EBUSY);
175 st->binding_user = user;
176 /* mark the component as active if user is valid */
177 if (st->binding_user)
178 pipe_st->active_comps |= BIT(c->id);
184 komeda_component_add_input(struct komeda_component_state *state,
185 struct komeda_component_output *input,
188 struct komeda_component *c = state->component;
190 WARN_ON((idx < 0 || idx >= c->max_active_inputs));
192 /* since the inputs[i] is only valid when it is active. So if a input[i]
193 * is a newly enabled input which switches from disable to enable, then
194 * the old inputs[i] is undefined (NOT zeroed), we can not rely on
195 * memcmp, but directly mark it changed
197 if (!has_bit(idx, state->affected_inputs) ||
198 memcmp(&state->inputs[idx], input, sizeof(*input))) {
199 memcpy(&state->inputs[idx], input, sizeof(*input));
200 state->changed_active_inputs |= BIT(idx);
202 state->active_inputs |= BIT(idx);
203 state->affected_inputs |= BIT(idx);
207 komeda_component_check_input(struct komeda_component_state *state,
208 struct komeda_component_output *input,
211 struct komeda_component *c = state->component;
213 if ((idx < 0) || (idx >= c->max_active_inputs)) {
214 DRM_DEBUG_ATOMIC("%s invalid input id: %d.\n", c->name, idx);
218 if (has_bit(idx, state->active_inputs)) {
219 DRM_DEBUG_ATOMIC("%s required input_id: %d has been occupied already.\n",
228 komeda_component_set_output(struct komeda_component_output *output,
229 struct komeda_component *comp,
232 output->component = comp;
233 output->output_port = output_port;
237 komeda_component_validate_private(struct komeda_component *c,
238 struct komeda_component_state *st)
242 if (!c->funcs->validate)
245 err = c->funcs->validate(c, st);
247 DRM_DEBUG_ATOMIC("%s validate private failed.\n", c->name);
253 komeda_layer_check_cfg(struct komeda_layer *layer,
254 struct komeda_plane_state *kplane_st,
255 struct komeda_data_flow_cfg *dflow)
257 if (!in_range(&layer->hsize_in, dflow->in_w)) {
258 DRM_DEBUG_ATOMIC("src_w: %d is out of range.\n", dflow->in_w);
262 if (!in_range(&layer->vsize_in, dflow->in_h)) {
263 DRM_DEBUG_ATOMIC("src_h: %d is out of range.\n", dflow->in_h);
271 komeda_layer_validate(struct komeda_layer *layer,
272 struct komeda_plane_state *kplane_st,
273 struct komeda_data_flow_cfg *dflow)
275 struct drm_plane_state *plane_st = &kplane_st->base;
276 struct drm_framebuffer *fb = plane_st->fb;
277 struct komeda_fb *kfb = to_kfb(fb);
278 struct komeda_component_state *c_st;
279 struct komeda_layer_state *st;
282 err = komeda_layer_check_cfg(layer, kplane_st, dflow);
286 c_st = komeda_component_get_state_and_set_user(&layer->base,
287 plane_st->state, plane_st->plane, plane_st->crtc);
289 return PTR_ERR(c_st);
291 st = to_layer_st(c_st);
293 st->rot = dflow->rot;
294 st->hsize = kfb->aligned_w;
295 st->vsize = kfb->aligned_h;
297 for (i = 0; i < fb->format->num_planes; i++)
298 st->addr[i] = komeda_fb_get_pixel_addr(kfb, dflow->in_x,
301 err = komeda_component_validate_private(&layer->base, c_st);
305 /* update the data flow for the next stage */
306 komeda_component_set_output(&dflow->input, &layer->base, 0);
311 static void pipeline_composition_size(struct komeda_crtc_state *kcrtc_st,
312 u16 *hsize, u16 *vsize)
314 struct drm_display_mode *m = &kcrtc_st->base.adjusted_mode;
317 *hsize = m->hdisplay;
319 *vsize = m->vdisplay;
323 komeda_compiz_set_input(struct komeda_compiz *compiz,
324 struct komeda_crtc_state *kcrtc_st,
325 struct komeda_data_flow_cfg *dflow)
327 struct drm_atomic_state *drm_st = kcrtc_st->base.state;
328 struct komeda_component_state *c_st, *old_st;
329 struct komeda_compiz_input_cfg *cin;
330 u16 compiz_w, compiz_h;
331 int idx = dflow->blending_zorder;
333 pipeline_composition_size(kcrtc_st, &compiz_w, &compiz_h);
334 /* check display rect */
335 if ((dflow->out_x + dflow->out_w > compiz_w) ||
336 (dflow->out_y + dflow->out_h > compiz_h) ||
337 dflow->out_w == 0 || dflow->out_h == 0) {
338 DRM_DEBUG_ATOMIC("invalid disp rect [x=%d, y=%d, w=%d, h=%d]\n",
339 dflow->out_x, dflow->out_y,
340 dflow->out_w, dflow->out_h);
344 c_st = komeda_component_get_state_and_set_user(&compiz->base, drm_st,
345 kcrtc_st->base.crtc, kcrtc_st->base.crtc);
347 return PTR_ERR(c_st);
349 if (komeda_component_check_input(c_st, &dflow->input, idx))
352 cin = &(to_compiz_st(c_st)->cins[idx]);
354 cin->hsize = dflow->out_w;
355 cin->vsize = dflow->out_h;
356 cin->hoffset = dflow->out_x;
357 cin->voffset = dflow->out_y;
358 cin->pixel_blend_mode = dflow->pixel_blend_mode;
359 cin->layer_alpha = dflow->layer_alpha;
361 old_st = komeda_component_get_old_state(&compiz->base, drm_st);
364 /* compare with old to check if this input has been changed */
365 if (memcmp(&(to_compiz_st(old_st)->cins[idx]), cin, sizeof(*cin)))
366 c_st->changed_active_inputs |= BIT(idx);
368 komeda_component_add_input(c_st, &dflow->input, idx);
374 komeda_compiz_validate(struct komeda_compiz *compiz,
375 struct komeda_crtc_state *state,
376 struct komeda_data_flow_cfg *dflow)
378 struct komeda_component_state *c_st;
379 struct komeda_compiz_state *st;
381 c_st = komeda_component_get_state_and_set_user(&compiz->base,
382 state->base.state, state->base.crtc, state->base.crtc);
384 return PTR_ERR(c_st);
386 st = to_compiz_st(c_st);
388 pipeline_composition_size(state, &st->hsize, &st->vsize);
390 komeda_component_set_output(&dflow->input, &compiz->base, 0);
392 /* compiz output dflow will be fed to the next pipeline stage, prepare
393 * the data flow configuration for the next stage
396 dflow->in_w = st->hsize;
397 dflow->in_h = st->vsize;
398 dflow->out_w = dflow->in_w;
399 dflow->out_h = dflow->in_h;
400 /* the output data of compiz doesn't have alpha, it only can be
401 * used as bottom layer when blend it with master layers
403 dflow->pixel_blend_mode = DRM_MODE_BLEND_PIXEL_NONE;
404 dflow->layer_alpha = 0xFF;
405 dflow->blending_zorder = 0;
412 komeda_improc_validate(struct komeda_improc *improc,
413 struct komeda_crtc_state *kcrtc_st,
414 struct komeda_data_flow_cfg *dflow)
416 struct drm_crtc *crtc = kcrtc_st->base.crtc;
417 struct komeda_component_state *c_st;
418 struct komeda_improc_state *st;
420 c_st = komeda_component_get_state_and_set_user(&improc->base,
421 kcrtc_st->base.state, crtc, crtc);
423 return PTR_ERR(c_st);
425 st = to_improc_st(c_st);
427 st->hsize = dflow->in_w;
428 st->vsize = dflow->in_h;
430 komeda_component_add_input(&st->base, &dflow->input, 0);
431 komeda_component_set_output(&dflow->input, &improc->base, 0);
437 komeda_timing_ctrlr_validate(struct komeda_timing_ctrlr *ctrlr,
438 struct komeda_crtc_state *kcrtc_st,
439 struct komeda_data_flow_cfg *dflow)
441 struct drm_crtc *crtc = kcrtc_st->base.crtc;
442 struct komeda_timing_ctrlr_state *st;
443 struct komeda_component_state *c_st;
445 c_st = komeda_component_get_state_and_set_user(&ctrlr->base,
446 kcrtc_st->base.state, crtc, crtc);
448 return PTR_ERR(c_st);
450 st = to_ctrlr_st(c_st);
452 komeda_component_add_input(&st->base, &dflow->input, 0);
453 komeda_component_set_output(&dflow->input, &ctrlr->base, 0);
458 int komeda_build_layer_data_flow(struct komeda_layer *layer,
459 struct komeda_plane_state *kplane_st,
460 struct komeda_crtc_state *kcrtc_st,
461 struct komeda_data_flow_cfg *dflow)
463 struct drm_plane *plane = kplane_st->base.plane;
464 struct komeda_pipeline *pipe = layer->base.pipeline;
467 DRM_DEBUG_ATOMIC("%s handling [PLANE:%d:%s]: src[x/y:%d/%d, w/h:%d/%d] disp[x/y:%d/%d, w/h:%d/%d]",
468 layer->base.name, plane->base.id, plane->name,
469 dflow->in_x, dflow->in_y, dflow->in_w, dflow->in_h,
470 dflow->out_x, dflow->out_y, dflow->out_w, dflow->out_h);
472 err = komeda_layer_validate(layer, kplane_st, dflow);
476 err = komeda_compiz_set_input(pipe->compiz, kcrtc_st, dflow);
481 /* build display output data flow, the data path is:
482 * compiz -> improc -> timing_ctrlr
484 int komeda_build_display_data_flow(struct komeda_crtc *kcrtc,
485 struct komeda_crtc_state *kcrtc_st)
487 struct komeda_pipeline *master = kcrtc->master;
488 struct komeda_data_flow_cfg m_dflow; /* master data flow */
491 memset(&m_dflow, 0, sizeof(m_dflow));
493 err = komeda_compiz_validate(master->compiz, kcrtc_st, &m_dflow);
497 err = komeda_improc_validate(master->improc, kcrtc_st, &m_dflow);
501 err = komeda_timing_ctrlr_validate(master->ctrlr, kcrtc_st, &m_dflow);
509 komeda_pipeline_unbound_components(struct komeda_pipeline *pipe,
510 struct komeda_pipeline_state *new)
512 struct drm_atomic_state *drm_st = new->obj.state;
513 struct komeda_pipeline_state *old = priv_to_pipe_st(pipe->obj.state);
514 struct komeda_component_state *c_st;
515 struct komeda_component *c;
516 u32 disabling_comps, id;
520 disabling_comps = (~new->active_comps) & old->active_comps;
522 /* unbound all disabling component */
523 dp_for_each_set_bit(id, disabling_comps) {
524 c = komeda_pipeline_get_component(pipe, id);
525 c_st = komeda_component_get_state_and_set_user(c,
526 drm_st, NULL, new->crtc);
527 WARN_ON(IS_ERR(c_st));
531 /* release unclaimed pipeline resource */
532 int komeda_release_unclaimed_resources(struct komeda_pipeline *pipe,
533 struct komeda_crtc_state *kcrtc_st)
535 struct drm_atomic_state *drm_st = kcrtc_st->base.state;
536 struct komeda_pipeline_state *st;
538 /* ignore the pipeline which is not affected */
539 if (!pipe || !has_bit(pipe->id, kcrtc_st->affected_pipes))
542 if (has_bit(pipe->id, kcrtc_st->active_pipes))
543 st = komeda_pipeline_get_new_state(pipe, drm_st);
545 st = komeda_pipeline_get_state_and_set_crtc(pipe, drm_st, NULL);
547 if (WARN_ON(IS_ERR_OR_NULL(st)))
550 komeda_pipeline_unbound_components(pipe, st);
555 void komeda_pipeline_disable(struct komeda_pipeline *pipe,
556 struct drm_atomic_state *old_state)
558 struct komeda_pipeline_state *old;
559 struct komeda_component *c;
560 struct komeda_component_state *c_st;
561 u32 id, disabling_comps = 0;
563 old = komeda_pipeline_get_old_state(pipe, old_state);
565 disabling_comps = old->active_comps;
566 DRM_DEBUG_ATOMIC("PIPE%d: disabling_comps: 0x%x.\n",
567 pipe->id, disabling_comps);
569 dp_for_each_set_bit(id, disabling_comps) {
570 c = komeda_pipeline_get_component(pipe, id);
571 c_st = priv_to_comp_st(c->obj.state);
574 * If we disabled a component then all active_inputs should be
575 * put in the list of changed_active_inputs, so they get
577 * This usually happens during a modeset when the pipeline is
578 * first disabled and then the actual state gets committed
581 c_st->changed_active_inputs |= c_st->active_inputs;
583 c->funcs->disable(c);
587 void komeda_pipeline_update(struct komeda_pipeline *pipe,
588 struct drm_atomic_state *old_state)
590 struct komeda_pipeline_state *new = priv_to_pipe_st(pipe->obj.state);
591 struct komeda_pipeline_state *old;
592 struct komeda_component *c;
593 u32 id, changed_comps = 0;
595 old = komeda_pipeline_get_old_state(pipe, old_state);
597 changed_comps = new->active_comps | old->active_comps;
599 DRM_DEBUG_ATOMIC("PIPE%d: active_comps: 0x%x, changed: 0x%x.\n",
600 pipe->id, new->active_comps, changed_comps);
602 dp_for_each_set_bit(id, changed_comps) {
603 c = komeda_pipeline_get_component(pipe, id);
605 if (new->active_comps & BIT(c->id))
606 c->funcs->update(c, priv_to_comp_st(c->obj.state));
608 c->funcs->disable(c);