• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * Coda multi-standard codec IP
4  *
5  * Copyright (C) 2012 Vista Silicon S.L.
6  *    Javier Martin, <javier.martin@vista-silicon.com>
7  *    Xavier Duret
8  * Copyright (C) 2012-2014 Philipp Zabel, Pengutronix
9  */
10 
11 #ifndef __CODA_H__
12 #define __CODA_H__
13 
14 #include <linux/debugfs.h>
15 #include <linux/idr.h>
16 #include <linux/irqreturn.h>
17 #include <linux/mutex.h>
18 #include <linux/kfifo.h>
19 #include <linux/videodev2.h>
20 
21 #include <media/v4l2-ctrls.h>
22 #include <media/v4l2-device.h>
23 #include <media/v4l2-fh.h>
24 #include <media/videobuf2-v4l2.h>
25 
26 #include "coda_regs.h"
27 
28 #define CODA_MAX_FRAMEBUFFERS	19
29 #define FMO_SLICE_SAVE_BUF_SIZE	(32)
30 
31 enum {
32 	V4L2_M2M_SRC = 0,
33 	V4L2_M2M_DST = 1,
34 };
35 
36 enum coda_inst_type {
37 	CODA_INST_ENCODER,
38 	CODA_INST_DECODER,
39 };
40 
41 enum coda_product {
42 	CODA_DX6 = 0xf001,
43 	CODA_HX4 = 0xf00a,
44 	CODA_7541 = 0xf012,
45 	CODA_960 = 0xf020,
46 };
47 
48 struct coda_video_device;
49 
50 struct coda_devtype {
51 	char			*firmware[3];
52 	enum coda_product	product;
53 	const struct coda_codec	*codecs;
54 	unsigned int		num_codecs;
55 	const struct coda_video_device **vdevs;
56 	unsigned int		num_vdevs;
57 	size_t			workbuf_size;
58 	size_t			tempbuf_size;
59 	size_t			iram_size;
60 };
61 
62 struct coda_aux_buf {
63 	void			*vaddr;
64 	dma_addr_t		paddr;
65 	u32			size;
66 	struct debugfs_blob_wrapper blob;
67 	struct dentry		*dentry;
68 };
69 
70 struct coda_dev {
71 	struct v4l2_device	v4l2_dev;
72 	struct video_device	vfd[6];
73 	struct device		*dev;
74 	const struct coda_devtype *devtype;
75 	int			firmware;
76 	struct vdoa_data	*vdoa;
77 
78 	void __iomem		*regs_base;
79 	struct clk		*clk_per;
80 	struct clk		*clk_ahb;
81 	struct reset_control	*rstc;
82 
83 	struct coda_aux_buf	codebuf;
84 	struct coda_aux_buf	tempbuf;
85 	struct coda_aux_buf	workbuf;
86 	struct gen_pool		*iram_pool;
87 	struct coda_aux_buf	iram;
88 
89 	struct mutex		dev_mutex;
90 	struct mutex		coda_mutex;
91 	struct workqueue_struct	*workqueue;
92 	struct v4l2_m2m_dev	*m2m_dev;
93 	struct ida		ida;
94 	struct dentry		*debugfs_root;
95 };
96 
97 struct coda_codec {
98 	u32 mode;
99 	u32 src_fourcc;
100 	u32 dst_fourcc;
101 	u32 max_w;
102 	u32 max_h;
103 };
104 
105 struct coda_huff_tab;
106 
107 struct coda_params {
108 	u8			rot_mode;
109 	u8			h264_intra_qp;
110 	u8			h264_inter_qp;
111 	u8			h264_min_qp;
112 	u8			h264_max_qp;
113 	u8			h264_disable_deblocking_filter_idc;
114 	s8			h264_slice_alpha_c0_offset_div2;
115 	s8			h264_slice_beta_offset_div2;
116 	bool			h264_constrained_intra_pred_flag;
117 	s8			h264_chroma_qp_index_offset;
118 	u8			h264_profile_idc;
119 	u8			h264_level_idc;
120 	u8			mpeg2_profile_idc;
121 	u8			mpeg2_level_idc;
122 	u8			mpeg4_intra_qp;
123 	u8			mpeg4_inter_qp;
124 	u8			gop_size;
125 	int			intra_refresh;
126 	enum v4l2_jpeg_chroma_subsampling jpeg_chroma_subsampling;
127 	u8			jpeg_quality;
128 	u8			jpeg_restart_interval;
129 	u8			*jpeg_qmat_tab[3];
130 	int			jpeg_qmat_index[3];
131 	int			jpeg_huff_dc_index[3];
132 	int			jpeg_huff_ac_index[3];
133 	u32			*jpeg_huff_data;
134 	struct coda_huff_tab	*jpeg_huff_tab;
135 	int			codec_mode;
136 	int			codec_mode_aux;
137 	enum v4l2_mpeg_video_multi_slice_mode slice_mode;
138 	u32			framerate;
139 	u16			bitrate;
140 	u16			vbv_delay;
141 	u32			vbv_size;
142 	u32			slice_max_bits;
143 	u32			slice_max_mb;
144 	bool			force_ipicture;
145 	bool			gop_size_changed;
146 	bool			bitrate_changed;
147 	bool			framerate_changed;
148 	bool			h264_intra_qp_changed;
149 	bool			intra_refresh_changed;
150 	bool			slice_mode_changed;
151 	bool			frame_rc_enable;
152 	bool			mb_rc_enable;
153 };
154 
155 struct coda_buffer_meta {
156 	struct list_head	list;
157 	u32			sequence;
158 	struct v4l2_timecode	timecode;
159 	u64			timestamp;
160 	unsigned int		start;
161 	unsigned int		end;
162 	bool			last;
163 };
164 
165 /* Per-queue, driver-specific private data */
166 struct coda_q_data {
167 	unsigned int		width;
168 	unsigned int		height;
169 	unsigned int		bytesperline;
170 	unsigned int		sizeimage;
171 	unsigned int		fourcc;
172 	struct v4l2_rect	rect;
173 };
174 
175 struct coda_iram_info {
176 	u32		axi_sram_use;
177 	phys_addr_t	buf_bit_use;
178 	phys_addr_t	buf_ip_ac_dc_use;
179 	phys_addr_t	buf_dbk_y_use;
180 	phys_addr_t	buf_dbk_c_use;
181 	phys_addr_t	buf_ovl_use;
182 	phys_addr_t	buf_btp_use;
183 	phys_addr_t	search_ram_paddr;
184 	int		search_ram_size;
185 	int		remaining;
186 	phys_addr_t	next_paddr;
187 };
188 
189 #define GDI_LINEAR_FRAME_MAP 0
190 #define GDI_TILED_FRAME_MB_RASTER_MAP 1
191 
192 struct coda_ctx;
193 
194 struct coda_context_ops {
195 	int (*queue_init)(void *priv, struct vb2_queue *src_vq,
196 			  struct vb2_queue *dst_vq);
197 	int (*reqbufs)(struct coda_ctx *ctx, struct v4l2_requestbuffers *rb);
198 	int (*start_streaming)(struct coda_ctx *ctx);
199 	int (*prepare_run)(struct coda_ctx *ctx);
200 	void (*finish_run)(struct coda_ctx *ctx);
201 	void (*run_timeout)(struct coda_ctx *ctx);
202 	void (*seq_init_work)(struct work_struct *work);
203 	void (*seq_end_work)(struct work_struct *work);
204 	void (*release)(struct coda_ctx *ctx);
205 };
206 
207 struct coda_internal_frame {
208 	struct coda_aux_buf		buf;
209 	struct coda_buffer_meta		meta;
210 	u32				type;
211 	u32				error;
212 };
213 
214 struct coda_ctx {
215 	struct coda_dev			*dev;
216 	struct mutex			buffer_mutex;
217 	struct work_struct		pic_run_work;
218 	struct work_struct		seq_init_work;
219 	struct work_struct		seq_end_work;
220 	struct completion		completion;
221 	const struct coda_video_device	*cvd;
222 	const struct coda_context_ops	*ops;
223 	int				aborting;
224 	int				initialized;
225 	int				streamon_out;
226 	int				streamon_cap;
227 	u32				qsequence;
228 	u32				osequence;
229 	u32				sequence_offset;
230 	struct coda_q_data		q_data[2];
231 	enum coda_inst_type		inst_type;
232 	const struct coda_codec		*codec;
233 	enum v4l2_colorspace		colorspace;
234 	enum v4l2_xfer_func		xfer_func;
235 	enum v4l2_ycbcr_encoding	ycbcr_enc;
236 	enum v4l2_quantization		quantization;
237 	struct coda_params		params;
238 	struct v4l2_ctrl_handler	ctrls;
239 	struct v4l2_ctrl		*h264_profile_ctrl;
240 	struct v4l2_ctrl		*h264_level_ctrl;
241 	struct v4l2_ctrl		*mpeg2_profile_ctrl;
242 	struct v4l2_ctrl		*mpeg2_level_ctrl;
243 	struct v4l2_ctrl		*mpeg4_profile_ctrl;
244 	struct v4l2_ctrl		*mpeg4_level_ctrl;
245 	struct v4l2_fh			fh;
246 	int				gopcounter;
247 	int				runcounter;
248 	int				jpeg_ecs_offset;
249 	char				vpu_header[3][64];
250 	int				vpu_header_size[3];
251 	struct kfifo			bitstream_fifo;
252 	struct mutex			bitstream_mutex;
253 	struct coda_aux_buf		bitstream;
254 	bool				hold;
255 	struct coda_aux_buf		parabuf;
256 	struct coda_aux_buf		psbuf;
257 	struct coda_aux_buf		slicebuf;
258 	struct coda_internal_frame	internal_frames[CODA_MAX_FRAMEBUFFERS];
259 	struct list_head		buffer_meta_list;
260 	spinlock_t			buffer_meta_lock;
261 	int				num_metas;
262 	struct coda_aux_buf		workbuf;
263 	int				num_internal_frames;
264 	int				idx;
265 	int				reg_idx;
266 	struct coda_iram_info		iram_info;
267 	int				tiled_map_type;
268 	u32				bit_stream_param;
269 	u32				frm_dis_flg;
270 	u32				frame_mem_ctrl;
271 	u32				para_change;
272 	int				display_idx;
273 	struct dentry			*debugfs_entry;
274 	bool				use_bit;
275 	bool				use_vdoa;
276 	struct vdoa_ctx			*vdoa;
277 	/*
278 	 * wakeup mutex used to serialize encoder stop command and finish_run,
279 	 * ensures that finish_run always either flags the last returned buffer
280 	 * or wakes up the capture queue to signal EOS afterwards.
281 	 */
282 	struct mutex			wakeup_mutex;
283 };
284 
285 extern int coda_debug;
286 
287 #define coda_dbg(level, ctx, fmt, arg...)				\
288 	do {								\
289 		if (coda_debug >= (level))				\
290 			v4l2_dbg((level), coda_debug, &(ctx)->dev->v4l2_dev, \
291 			 "%u: " fmt, (ctx)->idx, ##arg);		\
292 	} while (0)
293 
294 void coda_write(struct coda_dev *dev, u32 data, u32 reg);
295 unsigned int coda_read(struct coda_dev *dev, u32 reg);
296 void coda_write_base(struct coda_ctx *ctx, struct coda_q_data *q_data,
297 		     struct vb2_v4l2_buffer *buf, unsigned int reg_y);
298 
299 int coda_alloc_aux_buf(struct coda_dev *dev, struct coda_aux_buf *buf,
300 		       size_t size, const char *name, struct dentry *parent);
301 void coda_free_aux_buf(struct coda_dev *dev, struct coda_aux_buf *buf);
302 
303 int coda_encoder_queue_init(void *priv, struct vb2_queue *src_vq,
304 			    struct vb2_queue *dst_vq);
305 int coda_decoder_queue_init(void *priv, struct vb2_queue *src_vq,
306 			    struct vb2_queue *dst_vq);
307 
308 int coda_hw_reset(struct coda_ctx *ctx);
309 
310 void coda_fill_bitstream(struct coda_ctx *ctx, struct list_head *buffer_list);
311 
312 void coda_set_gdi_regs(struct coda_ctx *ctx);
313 
get_q_data(struct coda_ctx * ctx,enum v4l2_buf_type type)314 static inline struct coda_q_data *get_q_data(struct coda_ctx *ctx,
315 					     enum v4l2_buf_type type)
316 {
317 	switch (type) {
318 	case V4L2_BUF_TYPE_VIDEO_OUTPUT:
319 		return &(ctx->q_data[V4L2_M2M_SRC]);
320 	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
321 		return &(ctx->q_data[V4L2_M2M_DST]);
322 	default:
323 		return NULL;
324 	}
325 }
326 
327 const char *coda_product_name(int product);
328 
329 int coda_check_firmware(struct coda_dev *dev);
330 
coda_get_bitstream_payload(struct coda_ctx * ctx)331 static inline unsigned int coda_get_bitstream_payload(struct coda_ctx *ctx)
332 {
333 	return kfifo_len(&ctx->bitstream_fifo);
334 }
335 
336 /*
337  * The bitstream prefetcher needs to read at least 2 256 byte periods past
338  * the desired bitstream position for all data to reach the decoder.
339  */
coda_bitstream_can_fetch_past(struct coda_ctx * ctx,unsigned int pos)340 static inline bool coda_bitstream_can_fetch_past(struct coda_ctx *ctx,
341 						 unsigned int pos)
342 {
343 	return (int)(ctx->bitstream_fifo.kfifo.in - ALIGN(pos, 256)) > 512;
344 }
345 
346 bool coda_bitstream_can_fetch_past(struct coda_ctx *ctx, unsigned int pos);
347 int coda_bitstream_flush(struct coda_ctx *ctx);
348 
349 void coda_bit_stream_end_flag(struct coda_ctx *ctx);
350 
351 void coda_m2m_buf_done(struct coda_ctx *ctx, struct vb2_v4l2_buffer *buf,
352 		       enum vb2_buffer_state state);
353 
354 int coda_h264_filler_nal(int size, char *p);
355 int coda_h264_padding(int size, char *p);
356 int coda_h264_profile(int profile_idc);
357 int coda_h264_level(int level_idc);
358 int coda_sps_parse_profile(struct coda_ctx *ctx, struct vb2_buffer *vb);
359 int coda_h264_sps_fixup(struct coda_ctx *ctx, int width, int height, char *buf,
360 			int *size, int max_size);
361 
362 int coda_mpeg2_profile(int profile_idc);
363 int coda_mpeg2_level(int level_idc);
364 u32 coda_mpeg2_parse_headers(struct coda_ctx *ctx, u8 *buf, u32 size);
365 int coda_mpeg4_profile(int profile_idc);
366 int coda_mpeg4_level(int level_idc);
367 u32 coda_mpeg4_parse_headers(struct coda_ctx *ctx, u8 *buf, u32 size);
368 
369 void coda_update_profile_level_ctrls(struct coda_ctx *ctx, u8 profile_idc,
370 				     u8 level_idc);
371 
372 bool coda_jpeg_check_buffer(struct coda_ctx *ctx, struct vb2_buffer *vb);
373 int coda_jpeg_decode_header(struct coda_ctx *ctx, struct vb2_buffer *vb);
374 int coda_jpeg_write_tables(struct coda_ctx *ctx);
375 void coda_set_jpeg_compression_quality(struct coda_ctx *ctx, int quality);
376 
377 extern const struct coda_context_ops coda_bit_encode_ops;
378 extern const struct coda_context_ops coda_bit_decode_ops;
379 extern const struct coda_context_ops coda9_jpeg_encode_ops;
380 extern const struct coda_context_ops coda9_jpeg_decode_ops;
381 
382 irqreturn_t coda_irq_handler(int irq, void *data);
383 irqreturn_t coda9_jpeg_irq_handler(int irq, void *data);
384 
385 #endif /* __CODA_H__ */
386