1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Copyright (C) STMicroelectronics SA 2015
4  * Author: Hugues Fruchet <hugues.fruchet@st.com> for STMicroelectronics.
5  */
6 
7 #ifndef DELTA_H
8 #define DELTA_H
9 
10 #include <linux/rpmsg.h>
11 #include <media/v4l2-device.h>
12 #include <media/v4l2-mem2mem.h>
13 
14 #include "delta-cfg.h"
15 
16 /*
17  * enum delta_state - state of decoding instance
18  *
19  *@DELTA_STATE_WF_FORMAT:
20  *	Wait for compressed format to be set by V4L2 client in order
21  *	to know what is the relevant decoder to open.
22  *
23  *@DELTA_STATE_WF_STREAMINFO:
24  *	Wait for stream information to be available (bitstream
25  *	header parsing is done).
26  *
27  *@DELTA_STATE_READY:
28  *	Decoding instance is ready to decode compressed access unit.
29  *
30  *@DELTA_STATE_WF_EOS:
31  *	Decoding instance is waiting for EOS (End Of Stream) completion.
32  *
33  *@DELTA_STATE_EOS:
34  *	EOS (End Of Stream) is completed (signaled to user). Decoding instance
35  *	should then be closed.
36  */
37 enum delta_state {
38 	DELTA_STATE_WF_FORMAT,
39 	DELTA_STATE_WF_STREAMINFO,
40 	DELTA_STATE_READY,
41 	DELTA_STATE_WF_EOS,
42 	DELTA_STATE_EOS
43 };
44 
45 /*
46  * struct delta_streaminfo - information about stream to decode
47  *
48  * @flags:		validity of fields (crop, pixelaspect, other)
49  * @width:		width of video stream
50  * @height:		height ""
51  * @streamformat:	fourcc compressed format of video (MJPEG, MPEG2, ...)
52  * @dpb:		number of frames needed to decode a single frame
53  *			(h264 dpb, up to 16)
54  * @crop:		cropping window inside decoded frame (1920x1080@0,0
55  *			inside 1920x1088 frame for ex.)
56  * @pixelaspect:	pixel aspect ratio of video (4/3, 5/4)
57  * @field:		interlaced or not
58  * @profile:		profile string
59  * @level:		level string
60  * @other:		other string information from codec
61  * @colorspace:		colorspace identifier
62  * @xfer_func:		transfer function identifier
63  * @ycbcr_enc:		Y'CbCr encoding identifier
64  * @quantization:	quantization identifier
65  */
66 struct delta_streaminfo {
67 	u32 flags;
68 	u32 streamformat;
69 	u32 width;
70 	u32 height;
71 	u32 dpb;
72 	struct v4l2_rect crop;
73 	struct v4l2_fract pixelaspect;
74 	enum v4l2_field field;
75 	u8 profile[32];
76 	u8 level[32];
77 	u8 other[32];
78 	enum v4l2_colorspace colorspace;
79 	enum v4l2_xfer_func xfer_func;
80 	enum v4l2_ycbcr_encoding ycbcr_enc;
81 	enum v4l2_quantization quantization;
82 };
83 
84 #define DELTA_STREAMINFO_FLAG_CROP		0x0001
85 #define DELTA_STREAMINFO_FLAG_PIXELASPECT	0x0002
86 #define DELTA_STREAMINFO_FLAG_OTHER		0x0004
87 
88 /*
89  * struct delta_au - access unit structure.
90  *
91  * @vbuf:	video buffer information for V4L2
92  * @list:	V4L2 m2m list that the frame belongs to
93  * @prepared:	if set vaddr/paddr are resolved
94  * @vaddr:	virtual address (kernel can read/write)
95  * @paddr:	physical address (for hardware)
96  * @flags:	access unit type (V4L2_BUF_FLAG_KEYFRAME/PFRAME/BFRAME)
97  * @dts:	decoding timestamp of this access unit
98  */
99 struct delta_au {
100 	struct vb2_v4l2_buffer vbuf;	/* keep first */
101 	struct list_head list;	/* keep second */
102 
103 	bool prepared;
104 	u32 size;
105 	void *vaddr;
106 	dma_addr_t paddr;
107 	u32 flags;
108 	u64 dts;
109 };
110 
111 /*
112  * struct delta_frameinfo - information about decoded frame
113  *
114  * @flags:		validity of fields (crop, pixelaspect)
115  * @pixelformat:	fourcc code for uncompressed video format
116  * @width:		width of frame
117  * @height:		height of frame
118  * @aligned_width:	width of frame (with encoder or decoder alignment
119  *			constraint)
120  * @aligned_height:	height of frame (with encoder or decoder alignment
121  *			constraint)
122  * @size:		maximum size in bytes required for data
123  * @crop:		cropping window inside frame (1920x1080@0,0
124  *			inside 1920x1088 frame for ex.)
125  * @pixelaspect:	pixel aspect ratio of video (4/3, 5/4)
126  * @field:		interlaced mode
127  * @colorspace:		colorspace identifier
128  * @xfer_func:		transfer function identifier
129  * @ycbcr_enc:		Y'CbCr encoding identifier
130  * @quantization:	quantization identifier
131  */
132 struct delta_frameinfo {
133 	u32 flags;
134 	u32 pixelformat;
135 	u32 width;
136 	u32 height;
137 	u32 aligned_width;
138 	u32 aligned_height;
139 	u32 size;
140 	struct v4l2_rect crop;
141 	struct v4l2_fract pixelaspect;
142 	enum v4l2_field field;
143 	enum v4l2_colorspace colorspace;
144 	enum v4l2_xfer_func xfer_func;
145 	enum v4l2_ycbcr_encoding ycbcr_enc;
146 	enum v4l2_quantization quantization;
147 };
148 
149 #define DELTA_FRAMEINFO_FLAG_CROP		0x0001
150 #define DELTA_FRAMEINFO_FLAG_PIXELASPECT	0x0002
151 
152 /*
153  * struct delta_frame - frame structure.
154  *
155  * @vbuf:	video buffer information for V4L2
156  * @list:	V4L2 m2m list that the frame belongs to
157  * @info:	frame information (width, height, format, alignment...)
158  * @prepared:	if set pix/vaddr/paddr are resolved
159  * @index:	frame index, aligned on V4L2 wow
160  * @vaddr:	virtual address (kernel can read/write)
161  * @paddr:	physical address (for hardware)
162  * @state:	frame state for frame lifecycle tracking
163  *		(DELTA_FRAME_FREE/DEC/OUT/REC/...)
164  * @flags:	frame type (V4L2_BUF_FLAG_KEYFRAME/PFRAME/BFRAME)
165  * @dts:	decoding timestamp of this frame
166  * @field:	field order for interlaced frame
167  */
168 struct delta_frame {
169 	struct vb2_v4l2_buffer vbuf;	/* keep first */
170 	struct list_head list;	/* keep second */
171 
172 	struct delta_frameinfo info;
173 	bool prepared;
174 	u32 index;
175 	void *vaddr;
176 	dma_addr_t paddr;
177 	u32 state;
178 	u32 flags;
179 	u64 dts;
180 	enum v4l2_field field;
181 };
182 
183 /* frame state for frame lifecycle tracking */
184 #define DELTA_FRAME_FREE	0x00 /* is free and can be used for decoding */
185 #define DELTA_FRAME_REF		0x01 /* is a reference frame */
186 #define DELTA_FRAME_BSY		0x02 /* is owned by decoder and busy */
187 #define DELTA_FRAME_DEC		0x04 /* contains decoded content */
188 #define DELTA_FRAME_OUT		0x08 /* has been given to user */
189 #define DELTA_FRAME_RDY		0x10 /* is ready but still held by decoder */
190 #define DELTA_FRAME_M2M		0x20 /* is owned by mem2mem framework */
191 
192 /*
193  * struct delta_dts - decoding timestamp.
194  *
195  * @list:	list to chain timestamps
196  * @val:	timestamp in microseconds
197  */
198 struct delta_dts {
199 	struct list_head list;
200 	u64 val;
201 };
202 
203 struct delta_buf {
204 	u32 size;
205 	void *vaddr;
206 	dma_addr_t paddr;
207 	const char *name;
208 	unsigned long attrs;
209 };
210 
211 struct delta_ipc_ctx {
212 	int cb_err;
213 	u32 copro_hdl;
214 	struct completion done;
215 	struct delta_buf ipc_buf_struct;
216 	struct delta_buf *ipc_buf;
217 };
218 
219 struct delta_ipc_param {
220 	u32 size;
221 	void *data;
222 };
223 
224 struct delta_ctx;
225 
226 /*
227  * struct delta_dec - decoder structure.
228  *
229  * @name:		name of this decoder
230  * @streamformat:	input stream format that this decoder support
231  * @pixelformat:	pixel format of decoded frame that this decoder support
232  * @max_width:		(optional) maximum width that can decode this decoder
233  *			if not set, maximum width is DELTA_MAX_WIDTH
234  * @max_height:		(optional) maximum height that can decode this decoder
235  *			if not set, maximum height is DELTA_MAX_HEIGHT
236  * @pm:			(optional) if set, decoder will manage power on its own
237  * @open:		open this decoder
238  * @close:		close this decoder
239  * @setup_frame:	setup frame to be used by decoder, see below
240  * @get_streaminfo:	get stream related infos, see below
241  * @get_frameinfo:	get decoded frame related infos, see below
242  * @set_frameinfo:	(optional) set decoded frame related infos, see below
243  * @setup_frame:	setup frame to be used by decoder, see below
244  * @decode:		decode a single access unit, see below
245  * @get_frame:		get the next decoded frame available, see below
246  * @recycle:		recycle the given frame, see below
247  * @flush:		(optional) flush decoder, see below
248  * @drain:		(optional) drain decoder, see below
249  */
250 struct delta_dec {
251 	const char *name;
252 	u32 streamformat;
253 	u32 pixelformat;
254 	u32 max_width;
255 	u32 max_height;
256 	bool pm;
257 
258 	/*
259 	 * decoder ops
260 	 */
261 	int (*open)(struct delta_ctx *ctx);
262 	int (*close)(struct delta_ctx *ctx);
263 
264 	/*
265 	 * setup_frame() - setup frame to be used by decoder
266 	 * @ctx:	(in) instance
267 	 * @frame:	(in) frame to use
268 	 *  @frame.index	(in) identifier of frame
269 	 *  @frame.vaddr	(in) virtual address (kernel can read/write)
270 	 *  @frame.paddr	(in) physical address (for hardware)
271 	 *
272 	 * Frame is to be allocated by caller, then given
273 	 * to decoder through this call.
274 	 * Several frames must be given to decoder (dpb),
275 	 * each frame is identified using its index.
276 	 */
277 	int (*setup_frame)(struct delta_ctx *ctx, struct delta_frame *frame);
278 
279 	/*
280 	 * get_streaminfo() - get stream related infos
281 	 * @ctx:	(in) instance
282 	 * @streaminfo:	(out) width, height, dpb,...
283 	 *
284 	 * Precondition: stream header must have been successfully
285 	 * parsed to have this call successful & @streaminfo valid.
286 	 * Header parsing must be done using decode(), giving
287 	 * explicitly header access unit or first access unit of bitstream.
288 	 * If no valid header is found, get_streaminfo will return -ENODATA,
289 	 * in this case the next bitstream access unit must be decoded till
290 	 * get_streaminfo becomes successful.
291 	 */
292 	int (*get_streaminfo)(struct delta_ctx *ctx,
293 			      struct delta_streaminfo *streaminfo);
294 
295 	/*
296 	 * get_frameinfo() - get decoded frame related infos
297 	 * @ctx:	(in) instance
298 	 * @frameinfo:	(out) width, height, alignment, crop, ...
299 	 *
300 	 * Precondition: get_streaminfo() must be successful
301 	 */
302 	int (*get_frameinfo)(struct delta_ctx *ctx,
303 			     struct delta_frameinfo *frameinfo);
304 
305 	/*
306 	 * set_frameinfo() - set decoded frame related infos
307 	 * @ctx:	(in) instance
308 	 * @frameinfo:	(out) width, height, alignment, crop, ...
309 	 *
310 	 * Optional.
311 	 * Typically used to negotiate with decoder the output
312 	 * frame if decoder can do post-processing.
313 	 */
314 	int (*set_frameinfo)(struct delta_ctx *ctx,
315 			     struct delta_frameinfo *frameinfo);
316 
317 	/*
318 	 * decode() - decode a single access unit
319 	 * @ctx:	(in) instance
320 	 * @au:		(in/out) access unit
321 	 *  @au.size	(in) size of au to decode
322 	 *  @au.vaddr	(in) virtual address (kernel can read/write)
323 	 *  @au.paddr	(in) physical address (for hardware)
324 	 *  @au.flags	(out) au type (V4L2_BUF_FLAG_KEYFRAME/
325 	 *			PFRAME/BFRAME)
326 	 *
327 	 * Decode the access unit given. Decode is synchronous;
328 	 * access unit memory is no more needed after this call.
329 	 * After this call, none, one or several frames could
330 	 * have been decoded, which can be retrieved using
331 	 * get_frame().
332 	 */
333 	int (*decode)(struct delta_ctx *ctx, struct delta_au *au);
334 
335 	/*
336 	 * get_frame() - get the next decoded frame available
337 	 * @ctx:	(in) instance
338 	 * @frame:	(out) frame with decoded data:
339 	 *  @frame.index	(out) identifier of frame
340 	 *  @frame.field	(out) field order for interlaced frame
341 	 *  @frame.state	(out) frame state for frame lifecycle tracking
342 	 *  @frame.flags	(out) frame type (V4L2_BUF_FLAG_KEYFRAME/
343 	 *			PFRAME/BFRAME)
344 	 *
345 	 * Get the next available decoded frame.
346 	 * If no frame is available, -ENODATA is returned.
347 	 * If a frame is available, frame structure is filled with
348 	 * relevant data, frame.index identifying this exact frame.
349 	 * When this frame is no more needed by upper layers,
350 	 * recycle() must be called giving this frame identifier.
351 	 */
352 	int (*get_frame)(struct delta_ctx *ctx, struct delta_frame **frame);
353 
354 	/*
355 	 * recycle() - recycle the given frame
356 	 * @ctx:	(in) instance
357 	 * @frame:	(in) frame to recycle:
358 	 *  @frame.index	(in) identifier of frame
359 	 *
360 	 * recycle() is to be called by user when the decoded frame
361 	 * is no more needed (composition/display done).
362 	 * This frame will then be reused by decoder to proceed
363 	 * with next frame decoding.
364 	 * If not enough frames have been provided through setup_frame(),
365 	 * or recycle() is not called fast enough, the decoder can run out
366 	 * of available frames to proceed with decoding (starvation).
367 	 * This case is guarded by wq_recycle wait queue which ensures that
368 	 * decoder is called only if at least one frame is available.
369 	 */
370 	int (*recycle)(struct delta_ctx *ctx, struct delta_frame *frame);
371 
372 	/*
373 	 * flush() - flush decoder
374 	 * @ctx:	(in) instance
375 	 *
376 	 * Optional.
377 	 * Reset decoder context and discard all internal buffers.
378 	 * This allows implementation of seek, which leads to discontinuity
379 	 * of input bitstream that decoder must know to restart its internal
380 	 * decoding logic.
381 	 */
382 	int (*flush)(struct delta_ctx *ctx);
383 
384 	/*
385 	 * drain() - drain decoder
386 	 * @ctx:	(in) instance
387 	 *
388 	 * Optional.
389 	 * Mark decoder pending frames (decoded but not yet output) as ready
390 	 * so that they can be output to client at EOS (End Of Stream).
391 	 * get_frame() is to be called in a loop right after drain() to
392 	 * get all those pending frames.
393 	 */
394 	int (*drain)(struct delta_ctx *ctx);
395 };
396 
397 struct delta_dev;
398 
399 /*
400  * struct delta_ctx - instance structure.
401  *
402  * @flags:		validity of fields (streaminfo)
403  * @fh:			V4L2 file handle
404  * @dev:		device context
405  * @dec:		selected decoder context for this instance
406  * @ipc_ctx:		context of IPC communication with firmware
407  * @state:		instance state
408  * @frame_num:		frame number
409  * @au_num:		access unit number
410  * @max_au_size:	max size of an access unit
411  * @streaminfo:		stream information (width, height, dpb, interlacing...)
412  * @frameinfo:		frame information (width, height, format, alignment...)
413  * @nb_of_frames:	number of frames available for decoding
414  * @frames:		array of decoding frames to keep track of frame
415  *			state and manage frame recycling
416  * @decoded_frames:	nb of decoded frames from opening
417  * @output_frames:	nb of output frames from opening
418  * @dropped_frames:	nb of frames dropped (ie access unit not parsed
419  *			or frame decoded but not output)
420  * @stream_errors:	nb of stream errors (corrupted, not supported, ...)
421  * @decode_errors:	nb of decode errors (firmware error)
422  * @sys_errors:		nb of system errors (memory, ipc, ...)
423  * @dts:		FIFO of decoding timestamp.
424  *			output frames are timestamped with incoming access
425  *			unit timestamps using this fifo.
426  * @name:		string naming this instance (debug purpose)
427  * @run_work:		decoding work
428  * @lock:		lock for decoding work serialization
429  * @aborting:		true if current job aborted
430  * @priv:		private decoder context for this instance, allocated
431  *			by decoder @open time.
432  */
433 struct delta_ctx {
434 	u32 flags;
435 	struct v4l2_fh fh;
436 	struct delta_dev *dev;
437 	const struct delta_dec *dec;
438 	struct delta_ipc_ctx ipc_ctx;
439 
440 	enum delta_state state;
441 	u32 frame_num;
442 	u32 au_num;
443 	size_t max_au_size;
444 	struct delta_streaminfo streaminfo;
445 	struct delta_frameinfo frameinfo;
446 	u32 nb_of_frames;
447 	struct delta_frame *frames[DELTA_MAX_FRAMES];
448 	u32 decoded_frames;
449 	u32 output_frames;
450 	u32 dropped_frames;
451 	u32 stream_errors;
452 	u32 decode_errors;
453 	u32 sys_errors;
454 	struct list_head dts;
455 	char name[100];
456 	struct work_struct run_work;
457 	struct mutex lock;
458 	bool aborting;
459 	void *priv;
460 };
461 
462 #define DELTA_FLAG_STREAMINFO 0x0001
463 #define DELTA_FLAG_FRAMEINFO 0x0002
464 
465 #define DELTA_MAX_FORMATS  DELTA_MAX_DECODERS
466 
467 /*
468  * struct delta_dev - device struct, 1 per probe (so single one for
469  * all platform life)
470  *
471  * @v4l2_dev:		v4l2 device
472  * @vdev:		v4l2 video device
473  * @pdev:		platform device
474  * @dev:		device
475  * @m2m_dev:		memory-to-memory V4L2 device
476  * @lock:		device lock, for crit section & V4L2 ops serialization.
477  * @clk_delta:		delta main clock
478  * @clk_st231:		st231 coprocessor main clock
479  * @clk_flash_promip:	flash promip clock
480  * @decoders:		list of registered decoders
481  * @nb_of_decoders:	nb of registered decoders
482  * @pixelformats:	supported uncompressed video formats
483  * @nb_of_pixelformats:	number of supported umcompressed video formats
484  * @streamformats:	supported compressed video formats
485  * @nb_of_streamformats:number of supported compressed video formats
486  * @instance_id:	rolling counter identifying an instance (debug purpose)
487  * @work_queue:		decoding job work queue
488  * @rpmsg_driver:	rpmsg IPC driver
489  * @rpmsg_device:	rpmsg IPC device
490  */
491 struct delta_dev {
492 	struct v4l2_device v4l2_dev;
493 	struct video_device *vdev;
494 	struct platform_device *pdev;
495 	struct device *dev;
496 	struct v4l2_m2m_dev *m2m_dev;
497 	struct mutex lock;
498 	struct clk *clk_delta;
499 	struct clk *clk_st231;
500 	struct clk *clk_flash_promip;
501 	const struct delta_dec *decoders[DELTA_MAX_DECODERS];
502 	u32 nb_of_decoders;
503 	u32 pixelformats[DELTA_MAX_FORMATS];
504 	u32 nb_of_pixelformats;
505 	u32 streamformats[DELTA_MAX_FORMATS];
506 	u32 nb_of_streamformats;
507 	u8 instance_id;
508 	struct workqueue_struct *work_queue;
509 	struct rpmsg_driver rpmsg_driver;
510 	struct rpmsg_device *rpmsg_device;
511 };
512 
frame_type_str(u32 flags)513 static inline char *frame_type_str(u32 flags)
514 {
515 	if (flags & V4L2_BUF_FLAG_KEYFRAME)
516 		return "I";
517 	if (flags & V4L2_BUF_FLAG_PFRAME)
518 		return "P";
519 	if (flags & V4L2_BUF_FLAG_BFRAME)
520 		return "B";
521 	if (flags & V4L2_BUF_FLAG_LAST)
522 		return "EOS";
523 	return "?";
524 }
525 
frame_field_str(enum v4l2_field field)526 static inline char *frame_field_str(enum v4l2_field field)
527 {
528 	if (field == V4L2_FIELD_NONE)
529 		return "-";
530 	if (field == V4L2_FIELD_TOP)
531 		return "T";
532 	if (field == V4L2_FIELD_BOTTOM)
533 		return "B";
534 	if (field == V4L2_FIELD_INTERLACED)
535 		return "I";
536 	if (field == V4L2_FIELD_INTERLACED_TB)
537 		return "TB";
538 	if (field == V4L2_FIELD_INTERLACED_BT)
539 		return "BT";
540 	return "?";
541 }
542 
frame_state_str(u32 state,char * str,unsigned int len)543 static inline char *frame_state_str(u32 state, char *str, unsigned int len)
544 {
545 	snprintf(str, len, "%s %s %s %s %s %s",
546 		 (state & DELTA_FRAME_REF)  ? "ref" : "   ",
547 		 (state & DELTA_FRAME_BSY)  ? "bsy" : "   ",
548 		 (state & DELTA_FRAME_DEC)  ? "dec" : "   ",
549 		 (state & DELTA_FRAME_OUT)  ? "out" : "   ",
550 		 (state & DELTA_FRAME_M2M)  ? "m2m" : "   ",
551 		 (state & DELTA_FRAME_RDY)  ? "rdy" : "   ");
552 	return str;
553 }
554 
555 int delta_get_frameinfo_default(struct delta_ctx *ctx,
556 				struct delta_frameinfo *frameinfo);
557 int delta_recycle_default(struct delta_ctx *pctx,
558 			  struct delta_frame *frame);
559 
560 int delta_get_free_frame(struct delta_ctx *ctx,
561 			 struct delta_frame **pframe);
562 
563 int delta_get_sync(struct delta_ctx *ctx);
564 void delta_put_autosuspend(struct delta_ctx *ctx);
565 
566 #endif /* DELTA_H */
567