hva.h 13 KB

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