xref: /openbmc/linux/drivers/media/platform/st/sti/hva/hva.h (revision 09de5cd2)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Copyright (C) STMicroelectronics SA 2015
4  * Authors: Yannick Fertre <yannick.fertre@st.com>
5  *          Hugues Fruchet <hugues.fruchet@st.com>
6  */
7 
8 #ifndef HVA_H
9 #define HVA_H
10 
11 #include <media/v4l2-ctrls.h>
12 #include <media/v4l2-device.h>
13 #include <media/videobuf2-v4l2.h>
14 #include <media/v4l2-mem2mem.h>
15 
16 #define fh_to_ctx(f)    (container_of(f, struct hva_ctx, fh))
17 
18 #define hva_to_dev(h)   (h->dev)
19 
20 #define ctx_to_dev(c)   (c->hva_dev->dev)
21 
22 #define ctx_to_hdev(c)  (c->hva_dev)
23 
24 #define HVA_NAME	"st-hva"
25 #define HVA_PREFIX	"[---:----]"
26 
27 extern const struct hva_enc nv12h264enc;
28 extern const struct hva_enc nv21h264enc;
29 
30 /**
31  * struct hva_frameinfo - information about hva frame
32  *
33  * @pixelformat:    fourcc code for uncompressed video format
34  * @width:          width of frame
35  * @height:         height of frame
36  * @aligned_width:  width of frame (with encoder alignment constraint)
37  * @aligned_height: height of frame (with encoder alignment constraint)
38  * @size:           maximum size in bytes required for data
39 */
40 struct hva_frameinfo {
41 	u32	pixelformat;
42 	u32	width;
43 	u32	height;
44 	u32	aligned_width;
45 	u32	aligned_height;
46 	u32	size;
47 };
48 
49 /**
50  * struct hva_streaminfo - information about hva stream
51  *
52  * @streamformat: fourcc code of compressed video format (H.264...)
53  * @width:        width of stream
54  * @height:       height of stream
55  * @profile:      profile string
56  * @level:        level string
57  */
58 struct hva_streaminfo {
59 	u32	streamformat;
60 	u32	width;
61 	u32	height;
62 	u8	profile[32];
63 	u8	level[32];
64 };
65 
66 /**
67  * struct hva_controls - hva controls set
68  *
69  * @time_per_frame: time per frame in seconds
70  * @bitrate_mode:   bitrate mode (constant bitrate or variable bitrate)
71  * @gop_size:       groupe of picture size
72  * @bitrate:        bitrate (in bps)
73  * @aspect:         video aspect
74  * @profile:        H.264 profile
75  * @level:          H.264 level
76  * @entropy_mode:   H.264 entropy mode (CABAC or CVLC)
77  * @cpb_size:       coded picture buffer size (in kB)
78  * @dct8x8:         transform mode 8x8 enable
79  * @qpmin:          minimum quantizer
80  * @qpmax:          maximum quantizer
81  * @vui_sar:        pixel aspect ratio enable
82  * @vui_sar_idc:    pixel aspect ratio identifier
83  * @sei_fp:         sei frame packing arrangement enable
84  * @sei_fp_type:    sei frame packing arrangement type
85  */
86 struct hva_controls {
87 	struct v4l2_fract					time_per_frame;
88 	enum v4l2_mpeg_video_bitrate_mode			bitrate_mode;
89 	u32							gop_size;
90 	u32							bitrate;
91 	enum v4l2_mpeg_video_aspect				aspect;
92 	enum v4l2_mpeg_video_h264_profile			profile;
93 	enum v4l2_mpeg_video_h264_level				level;
94 	enum v4l2_mpeg_video_h264_entropy_mode			entropy_mode;
95 	u32							cpb_size;
96 	bool							dct8x8;
97 	u32							qpmin;
98 	u32							qpmax;
99 	bool							vui_sar;
100 	enum v4l2_mpeg_video_h264_vui_sar_idc			vui_sar_idc;
101 	bool							sei_fp;
102 	enum v4l2_mpeg_video_h264_sei_fp_arrangement_type	sei_fp_type;
103 };
104 
105 /**
106  * struct hva_frame - hva frame buffer (output)
107  *
108  * @vbuf:     video buffer information for V4L2
109  * @list:     V4L2 m2m list that the frame belongs to
110  * @info:     frame information (width, height, format, alignment...)
111  * @paddr:    physical address (for hardware)
112  * @vaddr:    virtual address (kernel can read/write)
113  * @prepared: true if vaddr/paddr are resolved
114  */
115 struct hva_frame {
116 	struct vb2_v4l2_buffer	vbuf;
117 	struct list_head	list;
118 	struct hva_frameinfo	info;
119 	dma_addr_t		paddr;
120 	void			*vaddr;
121 	bool			prepared;
122 };
123 
124 /*
125  * to_hva_frame() - cast struct vb2_v4l2_buffer * to struct hva_frame *
126  */
127 #define to_hva_frame(vb) \
128 	container_of(vb, struct hva_frame, vbuf)
129 
130 /**
131  * struct hva_stream - hva stream buffer (capture)
132  *
133  * @vbuf:       video buffer information for V4L2
134  * @list:       V4L2 m2m list that the frame belongs to
135  * @paddr:      physical address (for hardware)
136  * @vaddr:      virtual address (kernel can read/write)
137  * @prepared:   true if vaddr/paddr are resolved
138  * @size:       size of the buffer in bytes
139  * @bytesused:  number of bytes occupied by data in the buffer
140  */
141 struct hva_stream {
142 	struct vb2_v4l2_buffer	vbuf;
143 	struct list_head	list;
144 	dma_addr_t		paddr;
145 	void			*vaddr;
146 	bool			prepared;
147 	unsigned int		size;
148 	unsigned int		bytesused;
149 };
150 
151 /*
152  * to_hva_stream() - cast struct vb2_v4l2_buffer * to struct hva_stream *
153  */
154 #define to_hva_stream(vb) \
155 	container_of(vb, struct hva_stream, vbuf)
156 
157 #ifdef CONFIG_VIDEO_STI_HVA_DEBUGFS
158 /**
159  * struct hva_ctx_dbg - instance context debug info
160  *
161  * @debugfs_entry:      debugfs entry
162  * @is_valid_period:    true if the sequence is valid for performance
163  * @begin:              start time of last HW task
164  * @total_duration:     total HW processing durations in 0.1ms
165  * @cnt_duration:       number of HW processings
166  * @min_duration:       minimum HW processing duration in 0.1ms
167  * @max_duration:       maximum HW processing duration in 0.1ms
168  * @avg_duration:       average HW processing duration in 0.1ms
169  * @max_fps:            maximum frames encoded per second (in 0.1Hz)
170  * @total_period:       total encoding periods in 0.1ms
171  * @cnt_period:         number of periods
172  * @min_period:         minimum encoding period in 0.1ms
173  * @max_period:         maximum encoding period in 0.1ms
174  * @avg_period:         average encoding period in 0.1ms
175  * @total_stream_size:  total number of encoded bytes
176  * @avg_fps:            average frames encoded per second (in 0.1Hz)
177  * @window_duration:    duration of the sampling window in 0.1ms
178  * @cnt_window:         number of samples in the window
179  * @window_stream_size: number of encoded bytes upon the sampling window
180  * @last_bitrate:       bitrate upon the last sampling window
181  * @min_bitrate:        minimum bitrate in kbps
182  * @max_bitrate:        maximum bitrate in kbps
183  * @avg_bitrate:        average bitrate in kbps
184  */
185 struct hva_ctx_dbg {
186 	struct dentry	*debugfs_entry;
187 	bool		is_valid_period;
188 	ktime_t		begin;
189 	u32		total_duration;
190 	u32		cnt_duration;
191 	u32		min_duration;
192 	u32		max_duration;
193 	u32		avg_duration;
194 	u32		max_fps;
195 	u32		total_period;
196 	u32		cnt_period;
197 	u32		min_period;
198 	u32		max_period;
199 	u32		avg_period;
200 	u32		total_stream_size;
201 	u32		avg_fps;
202 	u32		window_duration;
203 	u32		cnt_window;
204 	u32		window_stream_size;
205 	u32		last_bitrate;
206 	u32		min_bitrate;
207 	u32		max_bitrate;
208 	u32		avg_bitrate;
209 };
210 #endif
211 
212 struct hva_dev;
213 struct hva_enc;
214 
215 /**
216  * struct hva_ctx - context of hva instance
217  *
218  * @hva_dev:         the device that this instance is associated with
219  * @fh:              V4L2 file handle
220  * @ctrl_handler:    V4L2 controls handler
221  * @ctrls:           hva controls set
222  * @id:              instance identifier
223  * @aborting:        true if current job aborted
224  * @name:            instance name (debug purpose)
225  * @run_work:        encode work
226  * @lock:            mutex used to lock access of this context
227  * @flags:           validity of streaminfo and frameinfo fields
228  * @frame_num:       frame number
229  * @stream_num:      stream number
230  * @max_stream_size: maximum size in bytes required for stream data
231  * @colorspace:      colorspace identifier
232  * @xfer_func:       transfer function identifier
233  * @ycbcr_enc:       Y'CbCr encoding identifier
234  * @quantization:    quantization identifier
235  * @streaminfo:      stream properties
236  * @frameinfo:       frame properties
237  * @enc:             current encoder
238  * @priv:            private codec data for this instance, allocated
239  *                   by encoder @open time
240  * @hw_err:          true if hardware error detected
241  * @encoded_frames:  number of encoded frames
242  * @sys_errors:      number of system errors (memory, resource, pm...)
243  * @encode_errors:   number of encoding errors (hw/driver errors)
244  * @frame_errors:    number of frame errors (format, size, header...)
245  * @dbg:             context debug info
246  */
247 struct hva_ctx {
248 	struct hva_dev			*hva_dev;
249 	struct v4l2_fh			fh;
250 	struct v4l2_ctrl_handler	ctrl_handler;
251 	struct hva_controls		ctrls;
252 	u8				id;
253 	bool				aborting;
254 	char				name[100];
255 	struct work_struct		run_work;
256 	/* mutex protecting this data structure */
257 	struct mutex			lock;
258 	u32				flags;
259 	u32				frame_num;
260 	u32				stream_num;
261 	u32				max_stream_size;
262 	enum v4l2_colorspace		colorspace;
263 	enum v4l2_xfer_func		xfer_func;
264 	enum v4l2_ycbcr_encoding	ycbcr_enc;
265 	enum v4l2_quantization		quantization;
266 	struct hva_streaminfo		streaminfo;
267 	struct hva_frameinfo		frameinfo;
268 	struct hva_enc			*enc;
269 	void				*priv;
270 	bool				hw_err;
271 	u32				encoded_frames;
272 	u32				sys_errors;
273 	u32				encode_errors;
274 	u32				frame_errors;
275 #ifdef CONFIG_VIDEO_STI_HVA_DEBUGFS
276 	struct hva_ctx_dbg		dbg;
277 #endif
278 };
279 
280 #define HVA_FLAG_STREAMINFO	0x0001
281 #define HVA_FLAG_FRAMEINFO	0x0002
282 
283 #ifdef CONFIG_VIDEO_STI_HVA_DEBUGFS
284 /**
285  * struct hva_dev_dbg - device debug info
286  *
287  * @debugfs_entry: debugfs entry
288  * @last_ctx:      debug information about last running instance context
289  */
290 struct hva_dev_dbg {
291 	struct dentry	*debugfs_entry;
292 	struct hva_ctx	last_ctx;
293 };
294 #endif
295 
296 #define HVA_MAX_INSTANCES	16
297 #define HVA_MAX_ENCODERS	10
298 #define HVA_MAX_FORMATS		HVA_MAX_ENCODERS
299 
300 /**
301  * struct hva_dev - abstraction for hva entity
302  *
303  * @v4l2_dev:            V4L2 device
304  * @vdev:                video device
305  * @pdev:                platform device
306  * @dev:                 device
307  * @lock:                mutex used for critical sections & V4L2 ops
308  *                       serialization
309  * @m2m_dev:             memory-to-memory V4L2 device information
310  * @instances:           opened instances
311  * @nb_of_instances:     number of opened instances
312  * @instance_id:         rolling counter identifying an instance (debug purpose)
313  * @regs:                register io memory access
314  * @esram_addr:          esram address
315  * @esram_size:          esram size
316  * @clk:                 hva clock
317  * @irq_its:             status interruption
318  * @irq_err:             error interruption
319  * @work_queue:          work queue to handle the encode jobs
320  * @protect_mutex:       mutex used to lock access of hardware
321  * @interrupt:           completion interrupt
322  * @ip_version:          IP hardware version
323  * @encoders:            registered encoders
324  * @nb_of_encoders:      number of registered encoders
325  * @pixelformats:        supported uncompressed video formats
326  * @nb_of_pixelformats:  number of supported umcompressed video formats
327  * @streamformats:       supported compressed video formats
328  * @nb_of_streamformats: number of supported compressed video formats
329  * @sfl_reg:             status fifo level register value
330  * @sts_reg:             status register value
331  * @lmi_err_reg:         local memory interface error register value
332  * @emi_err_reg:         external memory interface error register value
333  * @hec_mif_err_reg:     HEC memory interface error register value
334  * @dbg:                 device debug info
335  */
336 struct hva_dev {
337 	struct v4l2_device	v4l2_dev;
338 	struct video_device	*vdev;
339 	struct platform_device	*pdev;
340 	struct device		*dev;
341 	/* mutex protecting vb2_queue structure */
342 	struct mutex		lock;
343 	struct v4l2_m2m_dev	*m2m_dev;
344 	struct hva_ctx		*instances[HVA_MAX_INSTANCES];
345 	unsigned int		nb_of_instances;
346 	unsigned int		instance_id;
347 	void __iomem		*regs;
348 	u32			esram_addr;
349 	u32			esram_size;
350 	struct clk		*clk;
351 	int			irq_its;
352 	int			irq_err;
353 	struct workqueue_struct *work_queue;
354 	/* mutex protecting hardware access */
355 	struct mutex		protect_mutex;
356 	struct completion	interrupt;
357 	unsigned long int	ip_version;
358 	const struct hva_enc	*encoders[HVA_MAX_ENCODERS];
359 	u32			nb_of_encoders;
360 	u32			pixelformats[HVA_MAX_FORMATS];
361 	u32			nb_of_pixelformats;
362 	u32			streamformats[HVA_MAX_FORMATS];
363 	u32			nb_of_streamformats;
364 	u32			sfl_reg;
365 	u32			sts_reg;
366 	u32			lmi_err_reg;
367 	u32			emi_err_reg;
368 	u32			hec_mif_err_reg;
369 #ifdef CONFIG_VIDEO_STI_HVA_DEBUGFS
370 	struct hva_dev_dbg	dbg;
371 #endif
372 };
373 
374 /**
375  * struct hva_enc - hva encoder
376  *
377  * @name:         encoder name
378  * @streamformat: fourcc code for compressed video format (H.264...)
379  * @pixelformat:  fourcc code for uncompressed video format
380  * @max_width:    maximum width of frame for this encoder
381  * @max_height:   maximum height of frame for this encoder
382  * @open:         open encoder
383  * @close:        close encoder
384  * @encode:       encode a frame (struct hva_frame) in a stream
385  *                (struct hva_stream)
386  */
387 
388 struct hva_enc {
389 	const char	*name;
390 	u32		streamformat;
391 	u32		pixelformat;
392 	u32		max_width;
393 	u32		max_height;
394 	int		(*open)(struct hva_ctx *ctx);
395 	int		(*close)(struct hva_ctx *ctx);
396 	int		(*encode)(struct hva_ctx *ctx, struct hva_frame *frame,
397 				  struct hva_stream *stream);
398 };
399 
400 #ifdef CONFIG_VIDEO_STI_HVA_DEBUGFS
401 void hva_debugfs_create(struct hva_dev *hva);
402 void hva_debugfs_remove(struct hva_dev *hva);
403 void hva_dbg_ctx_create(struct hva_ctx *ctx);
404 void hva_dbg_ctx_remove(struct hva_ctx *ctx);
405 void hva_dbg_perf_begin(struct hva_ctx *ctx);
406 void hva_dbg_perf_end(struct hva_ctx *ctx, struct hva_stream *stream);
407 #endif
408 
409 #endif /* HVA_H */
410