410 lines · c
1/* SPDX-License-Identifier: GPL-2.0 */2/*3 * Copyright (C) STMicroelectronics SA 20154 * Authors: Yannick Fertre <yannick.fertre@st.com>5 * Hugues Fruchet <hugues.fruchet@st.com>6 */7 8#ifndef HVA_H9#define HVA_H10 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 27extern const struct hva_enc nv12h264enc;28extern const struct hva_enc nv21h264enc;29 30/**31 * struct hva_frameinfo - information about hva frame32 *33 * @pixelformat: fourcc code for uncompressed video format34 * @width: width of frame35 * @height: height of frame36 * @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 data39*/40struct 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 stream51 *52 * @streamformat: fourcc code of compressed video format (H.264...)53 * @width: width of stream54 * @height: height of stream55 * @profile: profile string56 * @level: level string57 */58struct 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 set68 *69 * @time_per_frame: time per frame in seconds70 * @bitrate_mode: bitrate mode (constant bitrate or variable bitrate)71 * @gop_size: groupe of picture size72 * @bitrate: bitrate (in bps)73 * @aspect: video aspect74 * @profile: H.264 profile75 * @level: H.264 level76 * @entropy_mode: H.264 entropy mode (CABAC or CVLC)77 * @cpb_size: coded picture buffer size (in kB)78 * @dct8x8: transform mode 8x8 enable79 * @qpmin: minimum quantizer80 * @qpmax: maximum quantizer81 * @vui_sar: pixel aspect ratio enable82 * @vui_sar_idc: pixel aspect ratio identifier83 * @sei_fp: sei frame packing arrangement enable84 * @sei_fp_type: sei frame packing arrangement type85 */86struct 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 V4L2109 * @list: V4L2 m2m list that the frame belongs to110 * @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 resolved114 */115struct 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 V4L2134 * @list: V4L2 m2m list that the frame belongs to135 * @paddr: physical address (for hardware)136 * @vaddr: virtual address (kernel can read/write)137 * @prepared: true if vaddr/paddr are resolved138 * @size: size of the buffer in bytes139 * @bytesused: number of bytes occupied by data in the buffer140 */141struct 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_DEBUGFS158/**159 * struct hva_ctx_dbg - instance context debug info160 *161 * @debugfs_entry: debugfs entry162 * @is_valid_period: true if the sequence is valid for performance163 * @begin: start time of last HW task164 * @total_duration: total HW processing durations in 0.1ms165 * @cnt_duration: number of HW processings166 * @min_duration: minimum HW processing duration in 0.1ms167 * @max_duration: maximum HW processing duration in 0.1ms168 * @avg_duration: average HW processing duration in 0.1ms169 * @max_fps: maximum frames encoded per second (in 0.1Hz)170 * @total_period: total encoding periods in 0.1ms171 * @cnt_period: number of periods172 * @min_period: minimum encoding period in 0.1ms173 * @max_period: maximum encoding period in 0.1ms174 * @avg_period: average encoding period in 0.1ms175 * @total_stream_size: total number of encoded bytes176 * @avg_fps: average frames encoded per second (in 0.1Hz)177 * @window_duration: duration of the sampling window in 0.1ms178 * @cnt_window: number of samples in the window179 * @window_stream_size: number of encoded bytes upon the sampling window180 * @last_bitrate: bitrate upon the last sampling window181 * @min_bitrate: minimum bitrate in kbps182 * @max_bitrate: maximum bitrate in kbps183 * @avg_bitrate: average bitrate in kbps184 */185struct 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#endif211 212struct hva_dev;213struct hva_enc;214 215/**216 * struct hva_ctx - context of hva instance217 *218 * @hva_dev: the device that this instance is associated with219 * @fh: V4L2 file handle220 * @ctrl_handler: V4L2 controls handler221 * @ctrls: hva controls set222 * @id: instance identifier223 * @aborting: true if current job aborted224 * @name: instance name (debug purpose)225 * @run_work: encode work226 * @lock: mutex used to lock access of this context227 * @flags: validity of streaminfo and frameinfo fields228 * @frame_num: frame number229 * @stream_num: stream number230 * @max_stream_size: maximum size in bytes required for stream data231 * @colorspace: colorspace identifier232 * @xfer_func: transfer function identifier233 * @ycbcr_enc: Y'CbCr encoding identifier234 * @quantization: quantization identifier235 * @streaminfo: stream properties236 * @frameinfo: frame properties237 * @enc: current encoder238 * @priv: private codec data for this instance, allocated239 * by encoder @open time240 * @hw_err: true if hardware error detected241 * @encoded_frames: number of encoded frames242 * @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 info246 */247struct 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_DEBUGFS276 struct hva_ctx_dbg dbg;277#endif278};279 280#define HVA_FLAG_STREAMINFO 0x0001281#define HVA_FLAG_FRAMEINFO 0x0002282 283#ifdef CONFIG_VIDEO_STI_HVA_DEBUGFS284/**285 * struct hva_dev_dbg - device debug info286 *287 * @debugfs_entry: debugfs entry288 * @last_ctx: debug information about last running instance context289 */290struct hva_dev_dbg {291 struct dentry *debugfs_entry;292 struct hva_ctx last_ctx;293};294#endif295 296#define HVA_MAX_INSTANCES 16297#define HVA_MAX_ENCODERS 10298#define HVA_MAX_FORMATS HVA_MAX_ENCODERS299 300/**301 * struct hva_dev - abstraction for hva entity302 *303 * @v4l2_dev: V4L2 device304 * @vdev: video device305 * @pdev: platform device306 * @dev: device307 * @lock: mutex used for critical sections & V4L2 ops308 * serialization309 * @m2m_dev: memory-to-memory V4L2 device information310 * @instances: opened instances311 * @nb_of_instances: number of opened instances312 * @instance_id: rolling counter identifying an instance (debug purpose)313 * @regs: register io memory access314 * @esram_addr: esram address315 * @esram_size: esram size316 * @clk: hva clock317 * @irq_its: status interruption318 * @irq_err: error interruption319 * @work_queue: work queue to handle the encode jobs320 * @protect_mutex: mutex used to lock access of hardware321 * @interrupt: completion interrupt322 * @ip_version: IP hardware version323 * @encoders: registered encoders324 * @nb_of_encoders: number of registered encoders325 * @pixelformats: supported uncompressed video formats326 * @nb_of_pixelformats: number of supported umcompressed video formats327 * @streamformats: supported compressed video formats328 * @nb_of_streamformats: number of supported compressed video formats329 * @sfl_reg: status fifo level register value330 * @sts_reg: status register value331 * @lmi_err_reg: local memory interface error register value332 * @emi_err_reg: external memory interface error register value333 * @hec_mif_err_reg: HEC memory interface error register value334 * @dbg: device debug info335 */336struct 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_DEBUGFS370 struct hva_dev_dbg dbg;371#endif372};373 374/**375 * struct hva_enc - hva encoder376 *377 * @name: encoder name378 * @streamformat: fourcc code for compressed video format (H.264...)379 * @pixelformat: fourcc code for uncompressed video format380 * @max_width: maximum width of frame for this encoder381 * @max_height: maximum height of frame for this encoder382 * @open: open encoder383 * @close: close encoder384 * @encode: encode a frame (struct hva_frame) in a stream385 * (struct hva_stream)386 */387 388struct 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_DEBUGFS401void hva_debugfs_create(struct hva_dev *hva);402void hva_debugfs_remove(struct hva_dev *hva);403void hva_dbg_ctx_create(struct hva_ctx *ctx);404void hva_dbg_ctx_remove(struct hva_ctx *ctx);405void hva_dbg_perf_begin(struct hva_ctx *ctx);406void hva_dbg_perf_end(struct hva_ctx *ctx, struct hva_stream *stream);407#endif408 409#endif /* HVA_H */410