s5p_mfc_common.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. /*
  2. * Samsung S5P Multi Format Codec v 5.0
  3. *
  4. * This file contains definitions of enums and structs used by the codec
  5. * driver.
  6. *
  7. * Copyright (C) 2011 Samsung Electronics Co., Ltd.
  8. * Kamil Debski, <k.debski@samsung.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by the
  12. * Free Software Foundation; either version 2 of the
  13. * License, or (at your option) any later version
  14. */
  15. #ifndef S5P_MFC_COMMON_H_
  16. #define S5P_MFC_COMMON_H_
  17. #include "regs-mfc.h"
  18. #include <linux/platform_device.h>
  19. #include <linux/videodev2.h>
  20. #include <media/v4l2-ctrls.h>
  21. #include <media/v4l2-device.h>
  22. #include <media/v4l2-ioctl.h>
  23. #include <media/videobuf2-core.h>
  24. /* Definitions related to MFC memory */
  25. /* Offset base used to differentiate between CAPTURE and OUTPUT
  26. * while mmaping */
  27. #define DST_QUEUE_OFF_BASE (TASK_SIZE / 2)
  28. /* Offset used by the hardware to store addresses */
  29. #define MFC_OFFSET_SHIFT 11
  30. #define FIRMWARE_ALIGN 0x20000 /* 128KB */
  31. #define MFC_H264_CTX_BUF_SIZE 0x96000 /* 600KB per H264 instance */
  32. #define MFC_CTX_BUF_SIZE 0x2800 /* 10KB per instance */
  33. #define DESC_BUF_SIZE 0x20000 /* 128KB for DESC buffer */
  34. #define SHARED_BUF_SIZE 0x2000 /* 8KB for shared buffer */
  35. #define DEF_CPB_SIZE 0x40000 /* 512KB */
  36. #define MFC_BANK1_ALLOC_CTX 0
  37. #define MFC_BANK2_ALLOC_CTX 1
  38. #define MFC_BANK1_ALIGN_ORDER 13
  39. #define MFC_BANK2_ALIGN_ORDER 13
  40. #define MFC_BASE_ALIGN_ORDER 17
  41. #include <media/videobuf2-dma-contig.h>
  42. static inline dma_addr_t s5p_mfc_mem_cookie(void *a, void *b)
  43. {
  44. /* Same functionality as the vb2_dma_contig_plane_paddr */
  45. dma_addr_t *paddr = vb2_dma_contig_memops.cookie(b);
  46. return *paddr;
  47. }
  48. /* MFC definitions */
  49. #define MFC_MAX_EXTRA_DPB 5
  50. #define MFC_MAX_BUFFERS 32
  51. #define MFC_NUM_CONTEXTS 4
  52. /* Interrupt timeout */
  53. #define MFC_INT_TIMEOUT 2000
  54. /* Busy wait timeout */
  55. #define MFC_BW_TIMEOUT 500
  56. /* Watchdog interval */
  57. #define MFC_WATCHDOG_INTERVAL 1000
  58. /* After how many executions watchdog should assume lock up */
  59. #define MFC_WATCHDOG_CNT 10
  60. #define MFC_NO_INSTANCE_SET -1
  61. #define MFC_ENC_CAP_PLANE_COUNT 1
  62. #define MFC_ENC_OUT_PLANE_COUNT 2
  63. #define STUFF_BYTE 4
  64. #define MFC_MAX_CTRLS 64
  65. #define mfc_read(dev, offset) readl(dev->regs_base + (offset))
  66. #define mfc_write(dev, data, offset) writel((data), dev->regs_base + \
  67. (offset))
  68. /**
  69. * enum s5p_mfc_fmt_type - type of the pixelformat
  70. */
  71. enum s5p_mfc_fmt_type {
  72. MFC_FMT_DEC,
  73. MFC_FMT_ENC,
  74. MFC_FMT_RAW,
  75. };
  76. /**
  77. * enum s5p_mfc_node_type - The type of an MFC device node.
  78. */
  79. enum s5p_mfc_node_type {
  80. MFCNODE_INVALID = -1,
  81. MFCNODE_DECODER = 0,
  82. MFCNODE_ENCODER = 1,
  83. };
  84. /**
  85. * enum s5p_mfc_inst_type - The type of an MFC instance.
  86. */
  87. enum s5p_mfc_inst_type {
  88. MFCINST_INVALID,
  89. MFCINST_DECODER,
  90. MFCINST_ENCODER,
  91. };
  92. /**
  93. * enum s5p_mfc_inst_state - The state of an MFC instance.
  94. */
  95. enum s5p_mfc_inst_state {
  96. MFCINST_FREE = 0,
  97. MFCINST_INIT = 100,
  98. MFCINST_GOT_INST,
  99. MFCINST_HEAD_PARSED,
  100. MFCINST_BUFS_SET,
  101. MFCINST_RUNNING,
  102. MFCINST_FINISHING,
  103. MFCINST_FINISHED,
  104. MFCINST_RETURN_INST,
  105. MFCINST_ERROR,
  106. MFCINST_ABORT,
  107. MFCINST_RES_CHANGE_INIT,
  108. MFCINST_RES_CHANGE_FLUSH,
  109. MFCINST_RES_CHANGE_END,
  110. };
  111. /**
  112. * enum s5p_mfc_queue_state - The state of buffer queue.
  113. */
  114. enum s5p_mfc_queue_state {
  115. QUEUE_FREE,
  116. QUEUE_BUFS_REQUESTED,
  117. QUEUE_BUFS_QUERIED,
  118. QUEUE_BUFS_MMAPED,
  119. };
  120. /**
  121. * enum s5p_mfc_decode_arg - type of frame decoding
  122. */
  123. enum s5p_mfc_decode_arg {
  124. MFC_DEC_FRAME,
  125. MFC_DEC_LAST_FRAME,
  126. MFC_DEC_RES_CHANGE,
  127. };
  128. struct s5p_mfc_ctx;
  129. /**
  130. * struct s5p_mfc_buf - MFC buffer
  131. */
  132. struct s5p_mfc_buf {
  133. struct list_head list;
  134. struct vb2_buffer *b;
  135. union {
  136. struct {
  137. size_t luma;
  138. size_t chroma;
  139. } raw;
  140. size_t stream;
  141. } cookie;
  142. int used;
  143. };
  144. /**
  145. * struct s5p_mfc_pm - power management data structure
  146. */
  147. struct s5p_mfc_pm {
  148. struct clk *clock;
  149. struct clk *clock_gate;
  150. atomic_t power;
  151. struct device *device;
  152. };
  153. /**
  154. * struct s5p_mfc_dev - The struct containing driver internal parameters.
  155. *
  156. * @v4l2_dev: v4l2_device
  157. * @vfd_dec: video device for decoding
  158. * @vfd_enc: video device for encoding
  159. * @plat_dev: platform device
  160. * @mem_dev_l: child device of the left memory bank (0)
  161. * @mem_dev_r: child device of the right memory bank (1)
  162. * @regs_base: base address of the MFC hw registers
  163. * @irq: irq resource
  164. * @mfc_mem: MFC registers memory resource
  165. * @dec_ctrl_handler: control framework handler for decoding
  166. * @enc_ctrl_handler: control framework handler for encoding
  167. * @pm: power management control
  168. * @num_inst: couter of active MFC instances
  169. * @irqlock: lock for operations on videobuf2 queues
  170. * @condlock: lock for changing/checking if a context is ready to be
  171. * processed
  172. * @mfc_mutex: lock for video_device
  173. * @int_cond: variable used by the waitqueue
  174. * @int_type: type of last interrupt
  175. * @int_err: error number for last interrupt
  176. * @queue: waitqueue for waiting for completion of device commands
  177. * @fw_size: size of firmware
  178. * @bank1: address of the beggining of bank 1 memory
  179. * @bank2: address of the beggining of bank 2 memory
  180. * @hw_lock: used for hardware locking
  181. * @ctx: array of driver contexts
  182. * @curr_ctx: number of the currently running context
  183. * @ctx_work_bits: used to mark which contexts are waiting for hardware
  184. * @watchdog_cnt: counter for the watchdog
  185. * @watchdog_workqueue: workqueue for the watchdog
  186. * @watchdog_work: worker for the watchdog
  187. * @alloc_ctx: videobuf2 allocator contexts for two memory banks
  188. * @enter_suspend: flag set when entering suspend
  189. *
  190. */
  191. struct s5p_mfc_dev {
  192. struct v4l2_device v4l2_dev;
  193. struct video_device *vfd_dec;
  194. struct video_device *vfd_enc;
  195. struct platform_device *plat_dev;
  196. struct device *mem_dev_l;
  197. struct device *mem_dev_r;
  198. void __iomem *regs_base;
  199. int irq;
  200. struct resource *mfc_mem;
  201. struct v4l2_ctrl_handler dec_ctrl_handler;
  202. struct v4l2_ctrl_handler enc_ctrl_handler;
  203. struct s5p_mfc_pm pm;
  204. int num_inst;
  205. spinlock_t irqlock; /* lock when operating on videobuf2 queues */
  206. spinlock_t condlock; /* lock when changing/checking if a context is
  207. ready to be processed */
  208. struct mutex mfc_mutex; /* video_device lock */
  209. int int_cond;
  210. int int_type;
  211. unsigned int int_err;
  212. wait_queue_head_t queue;
  213. size_t fw_size;
  214. size_t bank1;
  215. size_t bank2;
  216. unsigned long hw_lock;
  217. struct s5p_mfc_ctx *ctx[MFC_NUM_CONTEXTS];
  218. int curr_ctx;
  219. unsigned long ctx_work_bits;
  220. atomic_t watchdog_cnt;
  221. struct timer_list watchdog_timer;
  222. struct workqueue_struct *watchdog_workqueue;
  223. struct work_struct watchdog_work;
  224. void *alloc_ctx[2];
  225. unsigned long enter_suspend;
  226. };
  227. /**
  228. * struct s5p_mfc_h264_enc_params - encoding parameters for h264
  229. */
  230. struct s5p_mfc_h264_enc_params {
  231. enum v4l2_mpeg_video_h264_profile profile;
  232. enum v4l2_mpeg_video_h264_loop_filter_mode loop_filter_mode;
  233. s8 loop_filter_alpha;
  234. s8 loop_filter_beta;
  235. enum v4l2_mpeg_video_h264_entropy_mode entropy_mode;
  236. u8 max_ref_pic;
  237. u8 num_ref_pic_4p;
  238. int _8x8_transform;
  239. int rc_mb;
  240. int rc_mb_dark;
  241. int rc_mb_smooth;
  242. int rc_mb_static;
  243. int rc_mb_activity;
  244. int vui_sar;
  245. u8 vui_sar_idc;
  246. u16 vui_ext_sar_width;
  247. u16 vui_ext_sar_height;
  248. int open_gop;
  249. u16 open_gop_size;
  250. u8 rc_frame_qp;
  251. u8 rc_min_qp;
  252. u8 rc_max_qp;
  253. u8 rc_p_frame_qp;
  254. u8 rc_b_frame_qp;
  255. enum v4l2_mpeg_video_h264_level level_v4l2;
  256. int level;
  257. u16 cpb_size;
  258. };
  259. /**
  260. * struct s5p_mfc_mpeg4_enc_params - encoding parameters for h263 and mpeg4
  261. */
  262. struct s5p_mfc_mpeg4_enc_params {
  263. /* MPEG4 Only */
  264. enum v4l2_mpeg_video_mpeg4_profile profile;
  265. int quarter_pixel;
  266. /* Common for MPEG4, H263 */
  267. u16 vop_time_res;
  268. u16 vop_frm_delta;
  269. u8 rc_frame_qp;
  270. u8 rc_min_qp;
  271. u8 rc_max_qp;
  272. u8 rc_p_frame_qp;
  273. u8 rc_b_frame_qp;
  274. enum v4l2_mpeg_video_mpeg4_level level_v4l2;
  275. int level;
  276. };
  277. /**
  278. * struct s5p_mfc_enc_params - general encoding parameters
  279. */
  280. struct s5p_mfc_enc_params {
  281. u16 width;
  282. u16 height;
  283. u16 gop_size;
  284. enum v4l2_mpeg_video_multi_slice_mode slice_mode;
  285. u16 slice_mb;
  286. u32 slice_bit;
  287. u16 intra_refresh_mb;
  288. int pad;
  289. u8 pad_luma;
  290. u8 pad_cb;
  291. u8 pad_cr;
  292. int rc_frame;
  293. u32 rc_bitrate;
  294. u16 rc_reaction_coeff;
  295. u16 vbv_size;
  296. enum v4l2_mpeg_video_header_mode seq_hdr_mode;
  297. enum v4l2_mpeg_mfc51_video_frame_skip_mode frame_skip_mode;
  298. int fixed_target_bit;
  299. u8 num_b_frame;
  300. u32 rc_framerate_num;
  301. u32 rc_framerate_denom;
  302. int interlace;
  303. union {
  304. struct s5p_mfc_h264_enc_params h264;
  305. struct s5p_mfc_mpeg4_enc_params mpeg4;
  306. } codec;
  307. };
  308. /**
  309. * struct s5p_mfc_codec_ops - codec ops, used by encoding
  310. */
  311. struct s5p_mfc_codec_ops {
  312. /* initialization routines */
  313. int (*pre_seq_start) (struct s5p_mfc_ctx *ctx);
  314. int (*post_seq_start) (struct s5p_mfc_ctx *ctx);
  315. /* execution routines */
  316. int (*pre_frame_start) (struct s5p_mfc_ctx *ctx);
  317. int (*post_frame_start) (struct s5p_mfc_ctx *ctx);
  318. };
  319. #define call_cop(c, op, args...) \
  320. (((c)->c_ops->op) ? \
  321. ((c)->c_ops->op(args)) : 0)
  322. /**
  323. * struct s5p_mfc_ctx - This struct contains the instance context
  324. *
  325. * @dev: pointer to the s5p_mfc_dev of the device
  326. * @fh: struct v4l2_fh
  327. * @num: number of the context that this structure describes
  328. * @int_cond: variable used by the waitqueue
  329. * @int_type: type of the last interrupt
  330. * @int_err: error number received from MFC hw in the interrupt
  331. * @queue: waitqueue that can be used to wait for this context to
  332. * finish
  333. * @src_fmt: source pixelformat information
  334. * @dst_fmt: destination pixelformat information
  335. * @vq_src: vb2 queue for source buffers
  336. * @vq_dst: vb2 queue for destination buffers
  337. * @src_queue: driver internal queue for source buffers
  338. * @dst_queue: driver internal queue for destination buffers
  339. * @src_queue_cnt: number of buffers queued on the source internal queue
  340. * @dst_queue_cnt: number of buffers queued on the dest internal queue
  341. * @type: type of the instance - decoder or encoder
  342. * @state: state of the context
  343. * @inst_no: number of hw instance associated with the context
  344. * @img_width: width of the image that is decoded or encoded
  345. * @img_height: height of the image that is decoded or encoded
  346. * @buf_width: width of the buffer for processed image
  347. * @buf_height: height of the buffer for processed image
  348. * @luma_size: size of a luma plane
  349. * @chroma_size: size of a chroma plane
  350. * @mv_size: size of a motion vectors buffer
  351. * @consumed_stream: number of bytes that have been used so far from the
  352. * decoding buffer
  353. * @dpb_flush_flag: flag used to indicate that a DPB buffers are being
  354. * flushed
  355. * @bank1_buf: handle to memory allocated for temporary buffers from
  356. * memory bank 1
  357. * @bank1_phys: address of the temporary buffers from memory bank 1
  358. * @bank1_size: size of the memory allocated for temporary buffers from
  359. * memory bank 1
  360. * @bank2_buf: handle to memory allocated for temporary buffers from
  361. * memory bank 2
  362. * @bank2_phys: address of the temporary buffers from memory bank 2
  363. * @bank2_size: size of the memory allocated for temporary buffers from
  364. * memory bank 2
  365. * @capture_state: state of the capture buffers queue
  366. * @output_state: state of the output buffers queue
  367. * @src_bufs: information on allocated source buffers
  368. * @dst_bufs: information on allocated destination buffers
  369. * @sequence: counter for the sequence number for v4l2
  370. * @dec_dst_flag: flags for buffers queued in the hardware
  371. * @dec_src_buf_size: size of the buffer for source buffers in decoding
  372. * @codec_mode: number of codec mode used by MFC hw
  373. * @slice_interface: slice interface flag
  374. * @loop_filter_mpeg4: loop filter for MPEG4 flag
  375. * @display_delay: value of the display delay for H264
  376. * @display_delay_enable: display delay for H264 enable flag
  377. * @after_packed_pb: flag used to track buffer when stream is in
  378. * Packed PB format
  379. * @dpb_count: count of the DPB buffers required by MFC hw
  380. * @total_dpb_count: count of DPB buffers with additional buffers
  381. * requested by the application
  382. * @ctx_buf: handle to the memory associated with this context
  383. * @ctx_phys: address of the memory associated with this context
  384. * @ctx_size: size of the memory associated with this context
  385. * @desc_buf: description buffer for decoding handle
  386. * @desc_phys: description buffer for decoding address
  387. * @shm_alloc: handle for the shared memory buffer
  388. * @shm: virtual address for the shared memory buffer
  389. * @shm_ofs: address offset for shared memory
  390. * @enc_params: encoding parameters for MFC
  391. * @enc_dst_buf_size: size of the buffers for encoder output
  392. * @frame_type: used to force the type of the next encoded frame
  393. * @ref_queue: list of the reference buffers for encoding
  394. * @ref_queue_cnt: number of the buffers in the reference list
  395. * @c_ops: ops for encoding
  396. * @ctrls: array of controls, used when adding controls to the
  397. * v4l2 control framework
  398. * @ctrl_handler: handler for v4l2 framework
  399. */
  400. struct s5p_mfc_ctx {
  401. struct s5p_mfc_dev *dev;
  402. struct v4l2_fh fh;
  403. int num;
  404. int int_cond;
  405. int int_type;
  406. unsigned int int_err;
  407. wait_queue_head_t queue;
  408. struct s5p_mfc_fmt *src_fmt;
  409. struct s5p_mfc_fmt *dst_fmt;
  410. struct vb2_queue vq_src;
  411. struct vb2_queue vq_dst;
  412. struct list_head src_queue;
  413. struct list_head dst_queue;
  414. unsigned int src_queue_cnt;
  415. unsigned int dst_queue_cnt;
  416. enum s5p_mfc_inst_type type;
  417. enum s5p_mfc_inst_state state;
  418. int inst_no;
  419. /* Image parameters */
  420. int img_width;
  421. int img_height;
  422. int buf_width;
  423. int buf_height;
  424. int luma_size;
  425. int chroma_size;
  426. int mv_size;
  427. unsigned long consumed_stream;
  428. unsigned int dpb_flush_flag;
  429. /* Buffers */
  430. void *bank1_buf;
  431. size_t bank1_phys;
  432. size_t bank1_size;
  433. void *bank2_buf;
  434. size_t bank2_phys;
  435. size_t bank2_size;
  436. enum s5p_mfc_queue_state capture_state;
  437. enum s5p_mfc_queue_state output_state;
  438. struct s5p_mfc_buf src_bufs[MFC_MAX_BUFFERS];
  439. int src_bufs_cnt;
  440. struct s5p_mfc_buf dst_bufs[MFC_MAX_BUFFERS];
  441. int dst_bufs_cnt;
  442. unsigned int sequence;
  443. unsigned long dec_dst_flag;
  444. size_t dec_src_buf_size;
  445. /* Control values */
  446. int codec_mode;
  447. int slice_interface;
  448. int loop_filter_mpeg4;
  449. int display_delay;
  450. int display_delay_enable;
  451. int after_packed_pb;
  452. int dpb_count;
  453. int total_dpb_count;
  454. /* Buffers */
  455. void *ctx_buf;
  456. size_t ctx_phys;
  457. size_t ctx_ofs;
  458. size_t ctx_size;
  459. void *desc_buf;
  460. size_t desc_phys;
  461. void *shm_alloc;
  462. void *shm;
  463. size_t shm_ofs;
  464. struct s5p_mfc_enc_params enc_params;
  465. size_t enc_dst_buf_size;
  466. enum v4l2_mpeg_mfc51_video_force_frame_type force_frame_type;
  467. struct list_head ref_queue;
  468. unsigned int ref_queue_cnt;
  469. struct s5p_mfc_codec_ops *c_ops;
  470. struct v4l2_ctrl *ctrls[MFC_MAX_CTRLS];
  471. struct v4l2_ctrl_handler ctrl_handler;
  472. };
  473. /*
  474. * struct s5p_mfc_fmt - structure used to store information about pixelformats
  475. * used by the MFC
  476. */
  477. struct s5p_mfc_fmt {
  478. char *name;
  479. u32 fourcc;
  480. u32 codec_mode;
  481. enum s5p_mfc_fmt_type type;
  482. u32 num_planes;
  483. };
  484. /**
  485. * struct mfc_control - structure used to store information about MFC controls
  486. * it is used to initialize the control framework.
  487. */
  488. struct mfc_control {
  489. __u32 id;
  490. enum v4l2_ctrl_type type;
  491. __u8 name[32]; /* Whatever */
  492. __s32 minimum; /* Note signedness */
  493. __s32 maximum;
  494. __s32 step;
  495. __u32 menu_skip_mask;
  496. __s32 default_value;
  497. __u32 flags;
  498. __u32 reserved[2];
  499. __u8 is_volatile;
  500. };
  501. #define fh_to_ctx(__fh) container_of(__fh, struct s5p_mfc_ctx, fh)
  502. #define ctrl_to_ctx(__ctrl) \
  503. container_of((__ctrl)->handler, struct s5p_mfc_ctx, ctrl_handler)
  504. #endif /* S5P_MFC_COMMON_H_ */