xref: /openbmc/linux/drivers/gpu/drm/arm/display/komeda/komeda_pipeline.h (revision b0e55fef624e511e060fa05e4ca96cae6d902f04)
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 #ifndef _KOMEDA_PIPELINE_H_
8 #define _KOMEDA_PIPELINE_H_
9 
10 #include <linux/types.h>
11 #include <drm/drm_atomic.h>
12 #include <drm/drm_atomic_helper.h>
13 #include "malidp_utils.h"
14 
15 #define KOMEDA_MAX_PIPELINES		2
16 #define KOMEDA_PIPELINE_MAX_LAYERS	4
17 #define KOMEDA_PIPELINE_MAX_SCALERS	2
18 #define KOMEDA_COMPONENT_N_INPUTS	5
19 
20 /* pipeline component IDs */
21 enum {
22 	KOMEDA_COMPONENT_LAYER0		= 0,
23 	KOMEDA_COMPONENT_LAYER1		= 1,
24 	KOMEDA_COMPONENT_LAYER2		= 2,
25 	KOMEDA_COMPONENT_LAYER3		= 3,
26 	KOMEDA_COMPONENT_WB_LAYER	= 7, /* write back layer */
27 	KOMEDA_COMPONENT_SCALER0	= 8,
28 	KOMEDA_COMPONENT_SCALER1	= 9,
29 	KOMEDA_COMPONENT_SPLITTER	= 12,
30 	KOMEDA_COMPONENT_MERGER		= 14,
31 	KOMEDA_COMPONENT_COMPIZ0	= 16, /* compositor */
32 	KOMEDA_COMPONENT_COMPIZ1	= 17,
33 	KOMEDA_COMPONENT_IPS0		= 20, /* post image processor */
34 	KOMEDA_COMPONENT_IPS1		= 21,
35 	KOMEDA_COMPONENT_TIMING_CTRLR	= 22, /* timing controller */
36 };
37 
38 #define KOMEDA_PIPELINE_LAYERS		(BIT(KOMEDA_COMPONENT_LAYER0) |\
39 					 BIT(KOMEDA_COMPONENT_LAYER1) |\
40 					 BIT(KOMEDA_COMPONENT_LAYER2) |\
41 					 BIT(KOMEDA_COMPONENT_LAYER3))
42 
43 #define KOMEDA_PIPELINE_SCALERS		(BIT(KOMEDA_COMPONENT_SCALER0) |\
44 					 BIT(KOMEDA_COMPONENT_SCALER1))
45 
46 #define KOMEDA_PIPELINE_COMPIZS		(BIT(KOMEDA_COMPONENT_COMPIZ0) |\
47 					 BIT(KOMEDA_COMPONENT_COMPIZ1))
48 
49 #define KOMEDA_PIPELINE_IMPROCS		(BIT(KOMEDA_COMPONENT_IPS0) |\
50 					 BIT(KOMEDA_COMPONENT_IPS1))
51 struct komeda_component;
52 struct komeda_component_state;
53 
54 /** komeda_component_funcs - component control functions */
55 struct komeda_component_funcs {
56 	/** @validate: optional,
57 	 * component may has special requirements or limitations, this function
58 	 * supply HW the ability to do the further HW specific check.
59 	 */
60 	int (*validate)(struct komeda_component *c,
61 			struct komeda_component_state *state);
62 	/** @update: update is a active update */
63 	void (*update)(struct komeda_component *c,
64 		       struct komeda_component_state *state);
65 	/** @disable: disable component */
66 	void (*disable)(struct komeda_component *c);
67 	/** @dump_register: Optional, dump registers to seq_file */
68 	void (*dump_register)(struct komeda_component *c, struct seq_file *seq);
69 };
70 
71 /**
72  * struct komeda_component
73  *
74  * struct komeda_component describe the data flow capabilities for how to link a
75  * component into the display pipeline.
76  * all specified components are subclass of this structure.
77  */
78 struct komeda_component {
79 	/** @obj: treat component as private obj */
80 	struct drm_private_obj obj;
81 	/** @pipeline: the komeda pipeline this component belongs to */
82 	struct komeda_pipeline *pipeline;
83 	/** @name: component name */
84 	char name[32];
85 	/**
86 	 * @reg:
87 	 * component register base,
88 	 * which is initialized by chip and used by chip only
89 	 */
90 	u32 __iomem *reg;
91 	/** @id: component id */
92 	u32 id;
93 	/**
94 	 * @hw_id: component hw id,
95 	 * which is initialized by chip and used by chip only
96 	 */
97 	u32 hw_id;
98 
99 	/**
100 	 * @max_active_inputs:
101 	 * @max_active_outputs:
102 	 *
103 	 * maximum number of inputs/outputs that can be active at the same time
104 	 * Note:
105 	 * the number isn't the bit number of @supported_inputs or
106 	 * @supported_outputs, but may be less than it, since component may not
107 	 * support enabling all @supported_inputs/outputs at the same time.
108 	 */
109 	u8 max_active_inputs;
110 	/** @max_active_outputs: maximum number of outputs */
111 	u8 max_active_outputs;
112 	/**
113 	 * @supported_inputs:
114 	 * @supported_outputs:
115 	 *
116 	 * bitmask of BIT(component->id) for the supported inputs/outputs,
117 	 * describes the possibilities of how a component is linked into a
118 	 * pipeline.
119 	 */
120 	u32 supported_inputs;
121 	/** @supported_outputs: bitmask of supported output componenet ids */
122 	u32 supported_outputs;
123 
124 	/**
125 	 * @funcs: chip functions to access HW
126 	 */
127 	const struct komeda_component_funcs *funcs;
128 };
129 
130 /**
131  * struct komeda_component_output
132  *
133  * a component has multiple outputs, if want to know where the data
134  * comes from, only know the component is not enough, we still need to know
135  * its output port
136  */
137 struct komeda_component_output {
138 	/** @component: indicate which component the data comes from */
139 	struct komeda_component *component;
140 	/**
141 	 * @output_port:
142 	 * the output port of the &komeda_component_output.component
143 	 */
144 	u8 output_port;
145 };
146 
147 /**
148  * struct komeda_component_state
149  *
150  * component_state is the data flow configuration of the component, and it's
151  * the superclass of all specific component_state like @komeda_layer_state,
152  * @komeda_scaler_state
153  */
154 struct komeda_component_state {
155 	/** @obj: tracking component_state by drm_atomic_state */
156 	struct drm_private_state obj;
157 	/** @component: backpointer to the component */
158 	struct komeda_component *component;
159 	/**
160 	 * @binding_user:
161 	 * currently bound user, the user can be @crtc, @plane or @wb_conn,
162 	 * which is valid decided by @component and @inputs
163 	 *
164 	 * -  Layer: its user always is plane.
165 	 * -  compiz/improc/timing_ctrlr: the user is crtc.
166 	 * -  wb_layer: wb_conn;
167 	 * -  scaler: plane when input is layer, wb_conn if input is compiz.
168 	 */
169 	union {
170 		/** @crtc: backpointer for user crtc */
171 		struct drm_crtc *crtc;
172 		/** @plane: backpointer for user plane */
173 		struct drm_plane *plane;
174 		/** @wb_conn: backpointer for user wb_connector  */
175 		struct drm_connector *wb_conn;
176 		void *binding_user;
177 	};
178 
179 	/**
180 	 * @active_inputs:
181 	 *
182 	 * active_inputs is bitmask of @inputs index
183 	 *
184 	 * -  active_inputs = changed_active_inputs | unchanged_active_inputs
185 	 * -  affected_inputs = old->active_inputs | new->active_inputs;
186 	 * -  disabling_inputs = affected_inputs ^ active_inputs;
187 	 * -  changed_inputs = disabling_inputs | changed_active_inputs;
188 	 *
189 	 * NOTE:
190 	 * changed_inputs doesn't include all active_input but only
191 	 * @changed_active_inputs, and this bitmask can be used in chip
192 	 * level for dirty update.
193 	 */
194 	u16 active_inputs;
195 	/** @changed_active_inputs: bitmask of the changed @active_inputs */
196 	u16 changed_active_inputs;
197 	/** @affected_inputs: bitmask for affected @inputs */
198 	u16 affected_inputs;
199 	/**
200 	 * @inputs:
201 	 *
202 	 * the specific inputs[i] only valid on BIT(i) has been set in
203 	 * @active_inputs, if not the inputs[i] is undefined.
204 	 */
205 	struct komeda_component_output inputs[KOMEDA_COMPONENT_N_INPUTS];
206 };
207 
208 static inline u16 component_disabling_inputs(struct komeda_component_state *st)
209 {
210 	return st->affected_inputs ^ st->active_inputs;
211 }
212 
213 static inline u16 component_changed_inputs(struct komeda_component_state *st)
214 {
215 	return component_disabling_inputs(st) | st->changed_active_inputs;
216 }
217 
218 #define for_each_changed_input(st, i)	\
219 	for ((i) = 0; (i) < (st)->component->max_active_inputs; (i)++)	\
220 		if (has_bit((i), component_changed_inputs(st)))
221 
222 #define to_comp(__c)	(((__c) == NULL) ? NULL : &((__c)->base))
223 #define to_cpos(__c)	((struct komeda_component **)&(__c))
224 
225 struct komeda_layer {
226 	struct komeda_component base;
227 	/* accepted h/v input range before rotation */
228 	struct malidp_range hsize_in, vsize_in;
229 	u32 layer_type; /* RICH, SIMPLE or WB */
230 	u32 line_sz;
231 	u32 yuv_line_sz; /* maximum line size for YUV422 and YUV420 */
232 	u32 supported_rots;
233 	/* komeda supports layer split which splits a whole image to two parts
234 	 * left and right and handle them by two individual layer processors
235 	 * Note: left/right are always according to the final display rect,
236 	 * not the source buffer.
237 	 */
238 	struct komeda_layer *right;
239 };
240 
241 struct komeda_layer_state {
242 	struct komeda_component_state base;
243 	/* layer specific configuration state */
244 	u16 hsize, vsize;
245 	u32 rot;
246 	u16 afbc_crop_l;
247 	u16 afbc_crop_r;
248 	u16 afbc_crop_t;
249 	u16 afbc_crop_b;
250 	dma_addr_t addr[3];
251 };
252 
253 struct komeda_scaler {
254 	struct komeda_component base;
255 	struct malidp_range hsize, vsize;
256 	u32 max_upscaling;
257 	u32 max_downscaling;
258 	u8 scaling_split_overlap; /* split overlap for scaling */
259 	u8 enh_split_overlap; /* split overlap for image enhancement */
260 };
261 
262 struct komeda_scaler_state {
263 	struct komeda_component_state base;
264 	u16 hsize_in, vsize_in;
265 	u16 hsize_out, vsize_out;
266 	u16 total_hsize_in, total_vsize_in;
267 	u16 total_hsize_out; /* total_xxxx are size before split */
268 	u16 left_crop, right_crop;
269 	u8 en_scaling : 1,
270 	   en_alpha : 1, /* enable alpha processing */
271 	   en_img_enhancement : 1,
272 	   en_split : 1,
273 	   right_part : 1; /* right part of split image */
274 };
275 
276 struct komeda_compiz {
277 	struct komeda_component base;
278 	struct malidp_range hsize, vsize;
279 };
280 
281 struct komeda_compiz_input_cfg {
282 	u16 hsize, vsize;
283 	u16 hoffset, voffset;
284 	u8 pixel_blend_mode, layer_alpha;
285 };
286 
287 struct komeda_compiz_state {
288 	struct komeda_component_state base;
289 	/* composition size */
290 	u16 hsize, vsize;
291 	struct komeda_compiz_input_cfg cins[KOMEDA_COMPONENT_N_INPUTS];
292 };
293 
294 struct komeda_merger {
295 	struct komeda_component base;
296 	struct malidp_range hsize_merged;
297 	struct malidp_range vsize_merged;
298 };
299 
300 struct komeda_merger_state {
301 	struct komeda_component_state base;
302 	u16 hsize_merged;
303 	u16 vsize_merged;
304 };
305 
306 struct komeda_splitter {
307 	struct komeda_component base;
308 	struct malidp_range hsize, vsize;
309 };
310 
311 struct komeda_splitter_state {
312 	struct komeda_component_state base;
313 	u16 hsize, vsize;
314 	u16 overlap;
315 };
316 
317 struct komeda_improc {
318 	struct komeda_component base;
319 	u32 supported_color_formats;  /* DRM_RGB/YUV444/YUV420*/
320 	u32 supported_color_depths; /* BIT(8) | BIT(10)*/
321 	u8 supports_degamma : 1;
322 	u8 supports_csc : 1;
323 	u8 supports_gamma : 1;
324 };
325 
326 struct komeda_improc_state {
327 	struct komeda_component_state base;
328 	u8 color_format, color_depth;
329 	u16 hsize, vsize;
330 };
331 
332 /* display timing controller */
333 struct komeda_timing_ctrlr {
334 	struct komeda_component base;
335 	u8 supports_dual_link : 1;
336 };
337 
338 struct komeda_timing_ctrlr_state {
339 	struct komeda_component_state base;
340 };
341 
342 /* Why define A separated structure but not use plane_state directly ?
343  * 1. Komeda supports layer_split which means a plane_state can be split and
344  *    handled by two layers, one layer only handle half of plane image.
345  * 2. Fix up the user properties according to HW's capabilities, like user
346  *    set rotation to R180, but HW only supports REFLECT_X+Y. the rot here is
347  *    after drm_rotation_simplify()
348  */
349 struct komeda_data_flow_cfg {
350 	struct komeda_component_output input;
351 	u16 in_x, in_y, in_w, in_h;
352 	u32 out_x, out_y, out_w, out_h;
353 	u16 total_in_h, total_in_w;
354 	u16 total_out_w;
355 	u16 left_crop, right_crop, overlap;
356 	u32 rot;
357 	int blending_zorder;
358 	u8 pixel_blend_mode, layer_alpha;
359 	u8 en_scaling : 1,
360 	   en_img_enhancement : 1,
361 	   en_split : 1,
362 	   is_yuv : 1,
363 	   right_part : 1; /* right part of display image if split enabled */
364 };
365 
366 struct komeda_pipeline_funcs {
367 	/* check if the aclk (main engine clock) can satisfy the clock
368 	 * requirements of the downscaling that specified by dflow
369 	 */
370 	int (*downscaling_clk_check)(struct komeda_pipeline *pipe,
371 				     struct drm_display_mode *mode,
372 				     unsigned long aclk_rate,
373 				     struct komeda_data_flow_cfg *dflow);
374 	/* dump_register: Optional, dump registers to seq_file */
375 	void (*dump_register)(struct komeda_pipeline *pipe,
376 			      struct seq_file *sf);
377 };
378 
379 /**
380  * struct komeda_pipeline
381  *
382  * Represent a complete display pipeline and hold all functional components.
383  */
384 struct komeda_pipeline {
385 	/** @obj: link pipeline as private obj of drm_atomic_state */
386 	struct drm_private_obj obj;
387 	/** @mdev: the parent komeda_dev */
388 	struct komeda_dev *mdev;
389 	/** @pxlclk: pixel clock */
390 	struct clk *pxlclk;
391 	/** @id: pipeline id */
392 	int id;
393 	/** @avail_comps: available components mask of pipeline */
394 	u32 avail_comps;
395 	/**
396 	 * @standalone_disabled_comps:
397 	 *
398 	 * When disable the pipeline, some components can not be disabled
399 	 * together with others, but need a sparated and standalone disable.
400 	 * The standalone_disabled_comps are the components which need to be
401 	 * disabled standalone, and this concept also introduce concept of
402 	 * two phase.
403 	 * phase 1: for disabling the common components.
404 	 * phase 2: for disabling the standalong_disabled_comps.
405 	 */
406 	u32 standalone_disabled_comps;
407 	/** @n_layers: the number of layer on @layers */
408 	int n_layers;
409 	/** @layers: the pipeline layers */
410 	struct komeda_layer *layers[KOMEDA_PIPELINE_MAX_LAYERS];
411 	/** @n_scalers: the number of scaler on @scalers */
412 	int n_scalers;
413 	/** @scalers: the pipeline scalers */
414 	struct komeda_scaler *scalers[KOMEDA_PIPELINE_MAX_SCALERS];
415 	/** @compiz: compositor */
416 	struct komeda_compiz *compiz;
417 	/** @splitter: for split the compiz output to two half data flows */
418 	struct komeda_splitter *splitter;
419 	/** @merger: merger */
420 	struct komeda_merger *merger;
421 	/** @wb_layer: writeback layer */
422 	struct komeda_layer  *wb_layer;
423 	/** @improc: post image processor */
424 	struct komeda_improc *improc;
425 	/** @ctrlr: timing controller */
426 	struct komeda_timing_ctrlr *ctrlr;
427 	/** @funcs: chip private pipeline functions */
428 	const struct komeda_pipeline_funcs *funcs;
429 
430 	/** @of_node: pipeline dt node */
431 	struct device_node *of_node;
432 	/** @of_output_port: pipeline output port */
433 	struct device_node *of_output_port;
434 	/** @of_output_links: output connector device nodes */
435 	struct device_node *of_output_links[2];
436 	/** @dual_link: true if of_output_links[0] and [1] are both valid */
437 	bool dual_link;
438 };
439 
440 /**
441  * struct komeda_pipeline_state
442  *
443  * NOTE:
444  * Unlike the pipeline, pipeline_state doesn’t gather any component_state
445  * into it. It because all component will be managed by drm_atomic_state.
446  */
447 struct komeda_pipeline_state {
448 	/** @obj: tracking pipeline_state by drm_atomic_state */
449 	struct drm_private_state obj;
450 	/** @pipe: backpointer to the pipeline */
451 	struct komeda_pipeline *pipe;
452 	/** @crtc: currently bound crtc */
453 	struct drm_crtc *crtc;
454 	/**
455 	 * @active_comps:
456 	 *
457 	 * bitmask - BIT(component->id) of active components
458 	 */
459 	u32 active_comps;
460 };
461 
462 #define to_layer(c)	container_of(c, struct komeda_layer, base)
463 #define to_compiz(c)	container_of(c, struct komeda_compiz, base)
464 #define to_scaler(c)	container_of(c, struct komeda_scaler, base)
465 #define to_splitter(c)	container_of(c, struct komeda_splitter, base)
466 #define to_merger(c)	container_of(c, struct komeda_merger, base)
467 #define to_improc(c)	container_of(c, struct komeda_improc, base)
468 #define to_ctrlr(c)	container_of(c, struct komeda_timing_ctrlr, base)
469 
470 #define to_layer_st(c)	container_of(c, struct komeda_layer_state, base)
471 #define to_compiz_st(c)	container_of(c, struct komeda_compiz_state, base)
472 #define to_scaler_st(c)	container_of(c, struct komeda_scaler_state, base)
473 #define to_splitter_st(c) container_of(c, struct komeda_splitter_state, base)
474 #define to_merger_st(c)	container_of(c, struct komeda_merger_state, base)
475 #define to_improc_st(c)	container_of(c, struct komeda_improc_state, base)
476 #define to_ctrlr_st(c)	container_of(c, struct komeda_timing_ctrlr_state, base)
477 
478 #define priv_to_comp_st(o) container_of(o, struct komeda_component_state, obj)
479 #define priv_to_pipe_st(o) container_of(o, struct komeda_pipeline_state, obj)
480 
481 /* pipeline APIs */
482 struct komeda_pipeline *
483 komeda_pipeline_add(struct komeda_dev *mdev, size_t size,
484 		    const struct komeda_pipeline_funcs *funcs);
485 void komeda_pipeline_destroy(struct komeda_dev *mdev,
486 			     struct komeda_pipeline *pipe);
487 struct komeda_pipeline *
488 komeda_pipeline_get_slave(struct komeda_pipeline *master);
489 int komeda_assemble_pipelines(struct komeda_dev *mdev);
490 struct komeda_component *
491 komeda_pipeline_get_component(struct komeda_pipeline *pipe, int id);
492 struct komeda_component *
493 komeda_pipeline_get_first_component(struct komeda_pipeline *pipe,
494 				    u32 comp_mask);
495 
496 void komeda_pipeline_dump_register(struct komeda_pipeline *pipe,
497 				   struct seq_file *sf);
498 
499 /* component APIs */
500 extern __printf(10, 11)
501 struct komeda_component *
502 komeda_component_add(struct komeda_pipeline *pipe,
503 		     size_t comp_sz, u32 id, u32 hw_id,
504 		     const struct komeda_component_funcs *funcs,
505 		     u8 max_active_inputs, u32 supported_inputs,
506 		     u8 max_active_outputs, u32 __iomem *reg,
507 		     const char *name_fmt, ...);
508 
509 void komeda_component_destroy(struct komeda_dev *mdev,
510 			      struct komeda_component *c);
511 
512 static inline struct komeda_component *
513 komeda_component_pickup_output(struct komeda_component *c, u32 avail_comps)
514 {
515 	u32 avail_inputs = c->supported_outputs & (avail_comps);
516 
517 	return komeda_pipeline_get_first_component(c->pipeline, avail_inputs);
518 }
519 
520 struct komeda_plane_state;
521 struct komeda_crtc_state;
522 struct komeda_crtc;
523 
524 void pipeline_composition_size(struct komeda_crtc_state *kcrtc_st,
525 			       u16 *hsize, u16 *vsize);
526 
527 int komeda_build_layer_data_flow(struct komeda_layer *layer,
528 				 struct komeda_plane_state *kplane_st,
529 				 struct komeda_crtc_state *kcrtc_st,
530 				 struct komeda_data_flow_cfg *dflow);
531 int komeda_build_wb_data_flow(struct komeda_layer *wb_layer,
532 			      struct drm_connector_state *conn_st,
533 			      struct komeda_crtc_state *kcrtc_st,
534 			      struct komeda_data_flow_cfg *dflow);
535 int komeda_build_display_data_flow(struct komeda_crtc *kcrtc,
536 				   struct komeda_crtc_state *kcrtc_st);
537 
538 int komeda_build_layer_split_data_flow(struct komeda_layer *left,
539 				       struct komeda_plane_state *kplane_st,
540 				       struct komeda_crtc_state *kcrtc_st,
541 				       struct komeda_data_flow_cfg *dflow);
542 int komeda_build_wb_split_data_flow(struct komeda_layer *wb_layer,
543 				    struct drm_connector_state *conn_st,
544 				    struct komeda_crtc_state *kcrtc_st,
545 				    struct komeda_data_flow_cfg *dflow);
546 
547 int komeda_release_unclaimed_resources(struct komeda_pipeline *pipe,
548 				       struct komeda_crtc_state *kcrtc_st);
549 
550 struct komeda_pipeline_state *
551 komeda_pipeline_get_old_state(struct komeda_pipeline *pipe,
552 			      struct drm_atomic_state *state);
553 bool komeda_pipeline_disable(struct komeda_pipeline *pipe,
554 			     struct drm_atomic_state *old_state);
555 void komeda_pipeline_update(struct komeda_pipeline *pipe,
556 			    struct drm_atomic_state *old_state);
557 
558 void komeda_complete_data_flow_cfg(struct komeda_layer *layer,
559 				   struct komeda_data_flow_cfg *dflow,
560 				   struct drm_framebuffer *fb);
561 
562 #endif /* _KOMEDA_PIPELINE_H_*/
563