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