1 /* 2 * Copyright (C) STMicroelectronics SA 2015 3 * Authors: Yannick Fertre <yannick.fertre@st.com> 4 * Hugues Fruchet <hugues.fruchet@st.com> 5 * License terms: GNU General Public License (GPL), version 2 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_PREFIX "[---:----]" 25 26 extern const struct hva_enc nv12h264enc; 27 extern const struct hva_enc nv21h264enc; 28 29 /** 30 * struct hva_frameinfo - information about hva frame 31 * 32 * @pixelformat: fourcc code for uncompressed video format 33 * @width: width of frame 34 * @height: height of frame 35 * @aligned_width: width of frame (with encoder alignment constraint) 36 * @aligned_height: height of frame (with encoder alignment constraint) 37 * @size: maximum size in bytes required for data 38 */ 39 struct hva_frameinfo { 40 u32 pixelformat; 41 u32 width; 42 u32 height; 43 u32 aligned_width; 44 u32 aligned_height; 45 u32 size; 46 }; 47 48 /** 49 * struct hva_streaminfo - information about hva stream 50 * 51 * @streamformat: fourcc code of compressed video format (H.264...) 52 * @width: width of stream 53 * @height: height of stream 54 * @profile: profile string 55 * @level: level string 56 */ 57 struct hva_streaminfo { 58 u32 streamformat; 59 u32 width; 60 u32 height; 61 u8 profile[32]; 62 u8 level[32]; 63 }; 64 65 /** 66 * struct hva_controls - hva controls set 67 * 68 * @time_per_frame: time per frame in seconds 69 * @bitrate_mode: bitrate mode (constant bitrate or variable bitrate) 70 * @gop_size: groupe of picture size 71 * @bitrate: bitrate (in bps) 72 * @aspect: video aspect 73 * @profile: H.264 profile 74 * @level: H.264 level 75 * @entropy_mode: H.264 entropy mode (CABAC or CVLC) 76 * @cpb_size: coded picture buffer size (in kB) 77 * @dct8x8: transform mode 8x8 enable 78 * @qpmin: minimum quantizer 79 * @qpmax: maximum quantizer 80 * @vui_sar: pixel aspect ratio enable 81 * @vui_sar_idc: pixel aspect ratio identifier 82 * @sei_fp: sei frame packing arrangement enable 83 * @sei_fp_type: sei frame packing arrangement type 84 */ 85 struct hva_controls { 86 struct v4l2_fract time_per_frame; 87 enum v4l2_mpeg_video_bitrate_mode bitrate_mode; 88 u32 gop_size; 89 u32 bitrate; 90 enum v4l2_mpeg_video_aspect aspect; 91 enum v4l2_mpeg_video_h264_profile profile; 92 enum v4l2_mpeg_video_h264_level level; 93 enum v4l2_mpeg_video_h264_entropy_mode entropy_mode; 94 u32 cpb_size; 95 bool dct8x8; 96 u32 qpmin; 97 u32 qpmax; 98 bool vui_sar; 99 enum v4l2_mpeg_video_h264_vui_sar_idc vui_sar_idc; 100 bool sei_fp; 101 enum v4l2_mpeg_video_h264_sei_fp_arrangement_type sei_fp_type; 102 }; 103 104 /** 105 * struct hva_frame - hva frame buffer (output) 106 * 107 * @vbuf: video buffer information for V4L2 108 * @list: V4L2 m2m list that the frame belongs to 109 * @info: frame information (width, height, format, alignment...) 110 * @paddr: physical address (for hardware) 111 * @vaddr: virtual address (kernel can read/write) 112 * @prepared: true if vaddr/paddr are resolved 113 */ 114 struct hva_frame { 115 struct vb2_v4l2_buffer vbuf; 116 struct list_head list; 117 struct hva_frameinfo info; 118 dma_addr_t paddr; 119 void *vaddr; 120 bool prepared; 121 }; 122 123 /* 124 * to_hva_frame() - cast struct vb2_v4l2_buffer * to struct hva_frame * 125 */ 126 #define to_hva_frame(vb) \ 127 container_of(vb, struct hva_frame, vbuf) 128 129 /** 130 * struct hva_stream - hva stream buffer (capture) 131 * 132 * @v4l2: video buffer information for V4L2 133 * @list: V4L2 m2m list that the frame belongs to 134 * @paddr: physical address (for hardware) 135 * @vaddr: virtual address (kernel can read/write) 136 * @prepared: true if vaddr/paddr are resolved 137 * @size: size of the buffer in bytes 138 * @bytesused: number of bytes occupied by data in the buffer 139 */ 140 struct hva_stream { 141 struct vb2_v4l2_buffer vbuf; 142 struct list_head list; 143 dma_addr_t paddr; 144 void *vaddr; 145 bool prepared; 146 unsigned int size; 147 unsigned int bytesused; 148 }; 149 150 /* 151 * to_hva_stream() - cast struct vb2_v4l2_buffer * to struct hva_stream * 152 */ 153 #define to_hva_stream(vb) \ 154 container_of(vb, struct hva_stream, vbuf) 155 156 struct hva_dev; 157 struct hva_enc; 158 159 /** 160 * struct hva_ctx - context of hva instance 161 * 162 * @hva_dev: the device that this instance is associated with 163 * @fh: V4L2 file handle 164 * @ctrl_handler: V4L2 controls handler 165 * @ctrls: hva controls set 166 * @id: instance identifier 167 * @aborting: true if current job aborted 168 * @name: instance name (debug purpose) 169 * @run_work: encode work 170 * @lock: mutex used to lock access of this context 171 * @flags: validity of streaminfo and frameinfo fields 172 * @frame_num: frame number 173 * @stream_num: stream number 174 * @max_stream_size: maximum size in bytes required for stream data 175 * @colorspace: colorspace identifier 176 * @xfer_func: transfer function identifier 177 * @ycbcr_enc: Y'CbCr encoding identifier 178 * @quantization: quantization identifier 179 * @streaminfo: stream properties 180 * @frameinfo: frame properties 181 * @enc: current encoder 182 * @priv: private codec data for this instance, allocated 183 * by encoder @open time 184 * @hw_err: true if hardware error detected 185 */ 186 struct hva_ctx { 187 struct hva_dev *hva_dev; 188 struct v4l2_fh fh; 189 struct v4l2_ctrl_handler ctrl_handler; 190 struct hva_controls ctrls; 191 u8 id; 192 bool aborting; 193 char name[100]; 194 struct work_struct run_work; 195 /* mutex protecting this data structure */ 196 struct mutex lock; 197 u32 flags; 198 u32 frame_num; 199 u32 stream_num; 200 u32 max_stream_size; 201 enum v4l2_colorspace colorspace; 202 enum v4l2_xfer_func xfer_func; 203 enum v4l2_ycbcr_encoding ycbcr_enc; 204 enum v4l2_quantization quantization; 205 struct hva_streaminfo streaminfo; 206 struct hva_frameinfo frameinfo; 207 struct hva_enc *enc; 208 void *priv; 209 bool hw_err; 210 }; 211 212 #define HVA_FLAG_STREAMINFO 0x0001 213 #define HVA_FLAG_FRAMEINFO 0x0002 214 215 #define HVA_MAX_INSTANCES 16 216 #define HVA_MAX_ENCODERS 10 217 #define HVA_MAX_FORMATS HVA_MAX_ENCODERS 218 219 /** 220 * struct hva_dev - abstraction for hva entity 221 * 222 * @v4l2_dev: V4L2 device 223 * @vdev: video device 224 * @pdev: platform device 225 * @dev: device 226 * @lock: mutex used for critical sections & V4L2 ops 227 * serialization 228 * @m2m_dev: memory-to-memory V4L2 device information 229 * @instances: opened instances 230 * @nb_of_instances: number of opened instances 231 * @instance_id: rolling counter identifying an instance (debug purpose) 232 * @regs: register io memory access 233 * @esram_addr: esram address 234 * @esram_size: esram size 235 * @clk: hva clock 236 * @irq_its: status interruption 237 * @irq_err: error interruption 238 * @work_queue: work queue to handle the encode jobs 239 * @protect_mutex: mutex used to lock access of hardware 240 * @interrupt: completion interrupt 241 * @ip_version: IP hardware version 242 * @encoders: registered encoders 243 * @nb_of_encoders: number of registered encoders 244 * @pixelformats: supported uncompressed video formats 245 * @nb_of_pixelformats: number of supported umcompressed video formats 246 * @streamformats: supported compressed video formats 247 * @nb_of_streamformats: number of supported compressed video formats 248 * @sfl_reg: status fifo level register value 249 * @sts_reg: status register value 250 * @lmi_err_reg: local memory interface error register value 251 * @emi_err_reg: external memory interface error register value 252 * @hec_mif_err_reg: HEC memory interface error register value 253 */ 254 struct hva_dev { 255 struct v4l2_device v4l2_dev; 256 struct video_device *vdev; 257 struct platform_device *pdev; 258 struct device *dev; 259 /* mutex protecting vb2_queue structure */ 260 struct mutex lock; 261 struct v4l2_m2m_dev *m2m_dev; 262 struct hva_ctx *instances[HVA_MAX_INSTANCES]; 263 unsigned int nb_of_instances; 264 unsigned int instance_id; 265 void __iomem *regs; 266 u32 esram_addr; 267 u32 esram_size; 268 struct clk *clk; 269 int irq_its; 270 int irq_err; 271 struct workqueue_struct *work_queue; 272 /* mutex protecting hardware access */ 273 struct mutex protect_mutex; 274 struct completion interrupt; 275 unsigned long int ip_version; 276 const struct hva_enc *encoders[HVA_MAX_ENCODERS]; 277 u32 nb_of_encoders; 278 u32 pixelformats[HVA_MAX_FORMATS]; 279 u32 nb_of_pixelformats; 280 u32 streamformats[HVA_MAX_FORMATS]; 281 u32 nb_of_streamformats; 282 u32 sfl_reg; 283 u32 sts_reg; 284 u32 lmi_err_reg; 285 u32 emi_err_reg; 286 u32 hec_mif_err_reg; 287 }; 288 289 /** 290 * struct hva_enc - hva encoder 291 * 292 * @name: encoder name 293 * @streamformat: fourcc code for compressed video format (H.264...) 294 * @pixelformat: fourcc code for uncompressed video format 295 * @max_width: maximum width of frame for this encoder 296 * @max_height: maximum height of frame for this encoder 297 * @open: open encoder 298 * @close: close encoder 299 * @encode: encode a frame (struct hva_frame) in a stream 300 * (struct hva_stream) 301 */ 302 303 struct hva_enc { 304 const char *name; 305 u32 streamformat; 306 u32 pixelformat; 307 u32 max_width; 308 u32 max_height; 309 int (*open)(struct hva_ctx *ctx); 310 int (*close)(struct hva_ctx *ctx); 311 int (*encode)(struct hva_ctx *ctx, struct hva_frame *frame, 312 struct hva_stream *stream); 313 }; 314 315 #endif /* HVA_H */ 316