1 /* SPDX-License-Identifier: GPL-2.0+ */ 2 /* 3 * Copyright (C) 2018 BayLibre, SAS 4 * Author: Maxime Jourdan <mjourdan@baylibre.com> 5 */ 6 7 #ifndef __MESON_VDEC_CORE_H_ 8 #define __MESON_VDEC_CORE_H_ 9 10 #include <linux/irqreturn.h> 11 #include <linux/regmap.h> 12 #include <linux/list.h> 13 #include <media/videobuf2-v4l2.h> 14 #include <media/v4l2-ctrls.h> 15 #include <media/v4l2-device.h> 16 #include <linux/soc/amlogic/meson-canvas.h> 17 18 #include "vdec_platform.h" 19 20 /* 32 buffers in 3-plane YUV420 */ 21 #define MAX_CANVAS (32 * 3) 22 23 struct amvdec_buffer { 24 struct list_head list; 25 struct vb2_buffer *vb; 26 }; 27 28 /** 29 * struct amvdec_timestamp - stores a src timestamp along with a VIFIFO offset 30 * 31 * @list: used to make lists out of this struct 32 * @tc: timecode from the v4l2 buffer 33 * @ts: timestamp from the VB2 buffer 34 * @offset: offset in the VIFIFO where the associated packet was written 35 * @flags: flags from the v4l2 buffer 36 * @used_count: times this timestamp was checked for a match with a dst buffer 37 */ 38 struct amvdec_timestamp { 39 struct list_head list; 40 struct v4l2_timecode tc; 41 u64 ts; 42 u32 offset; 43 u32 flags; 44 u32 used_count; 45 }; 46 47 struct amvdec_session; 48 49 /** 50 * struct amvdec_core - device parameters, singleton 51 * 52 * @dos_base: DOS memory base address 53 * @esparser_base: PARSER memory base address 54 * @regmap_ao: regmap for the AO bus 55 * @dev: core device 56 * @dev_dec: decoder device 57 * @platform: platform-specific data 58 * @canvas: canvas provider reference 59 * @dos_parser_clk: DOS_PARSER clock 60 * @dos_clk: DOS clock 61 * @vdec_1_clk: VDEC_1 clock 62 * @vdec_hevc_clk: VDEC_HEVC clock 63 * @vdec_hevcf_clk: VDEC_HEVCF clock 64 * @esparser_reset: RESET for the PARSER 65 * @vdev_dec: video device for the decoder 66 * @v4l2_dev: v4l2 device 67 * @cur_sess: current decoding session 68 * @lock: video device lock 69 */ 70 struct amvdec_core { 71 void __iomem *dos_base; 72 void __iomem *esparser_base; 73 struct regmap *regmap_ao; 74 75 struct device *dev; 76 struct device *dev_dec; 77 const struct vdec_platform *platform; 78 79 struct meson_canvas *canvas; 80 81 struct clk *dos_parser_clk; 82 struct clk *dos_clk; 83 struct clk *vdec_1_clk; 84 struct clk *vdec_hevc_clk; 85 struct clk *vdec_hevcf_clk; 86 87 struct reset_control *esparser_reset; 88 89 struct video_device *vdev_dec; 90 struct v4l2_device v4l2_dev; 91 92 struct amvdec_session *cur_sess; 93 struct mutex lock; 94 }; 95 96 /** 97 * struct amvdec_ops - vdec operations 98 * 99 * @start: mandatory call when the vdec needs to initialize 100 * @stop: mandatory call when the vdec needs to stop 101 * @conf_esparser: mandatory call to let the vdec configure the ESPARSER 102 * @vififo_level: mandatory call to get the current amount of data 103 * in the VIFIFO 104 */ 105 struct amvdec_ops { 106 int (*start)(struct amvdec_session *sess); 107 int (*stop)(struct amvdec_session *sess); 108 void (*conf_esparser)(struct amvdec_session *sess); 109 u32 (*vififo_level)(struct amvdec_session *sess); 110 }; 111 112 /** 113 * struct amvdec_codec_ops - codec operations 114 * 115 * @start: mandatory call when the codec needs to initialize 116 * @stop: mandatory call when the codec needs to stop 117 * @load_extended_firmware: optional call to load additional firmware bits 118 * @num_pending_bufs: optional call to get the number of dst buffers on hold 119 * @can_recycle: optional call to know if the codec is ready to recycle 120 * a dst buffer 121 * @recycle: optional call to tell the codec to recycle a dst buffer. Must go 122 * in pair with @can_recycle 123 * @drain: optional call if the codec has a custom way of draining 124 * @resume: optional call to resume after a resolution change 125 * @eos_sequence: optional call to get an end sequence to send to esparser 126 * for flush. Mutually exclusive with @drain. 127 * @isr: mandatory call when the ISR triggers 128 * @threaded_isr: mandatory call for the threaded ISR 129 */ 130 struct amvdec_codec_ops { 131 int (*start)(struct amvdec_session *sess); 132 int (*stop)(struct amvdec_session *sess); 133 int (*load_extended_firmware)(struct amvdec_session *sess, 134 const u8 *data, u32 len); 135 u32 (*num_pending_bufs)(struct amvdec_session *sess); 136 int (*can_recycle)(struct amvdec_core *core); 137 void (*recycle)(struct amvdec_core *core, u32 buf_idx); 138 void (*drain)(struct amvdec_session *sess); 139 void (*resume)(struct amvdec_session *sess); 140 const u8 * (*eos_sequence)(u32 *len); 141 irqreturn_t (*isr)(struct amvdec_session *sess); 142 irqreturn_t (*threaded_isr)(struct amvdec_session *sess); 143 }; 144 145 /** 146 * struct amvdec_format - describes one of the OUTPUT (src) format supported 147 * 148 * @pixfmt: V4L2 pixel format 149 * @min_buffers: minimum amount of CAPTURE (dst) buffers 150 * @max_buffers: maximum amount of CAPTURE (dst) buffers 151 * @max_width: maximum picture width supported 152 * @max_height: maximum picture height supported 153 * @flags: enum flags associated with this pixfmt 154 * @vdec_ops: the VDEC operations that support this format 155 * @codec_ops: the codec operations that support this format 156 * @firmware_path: Path to the firmware that supports this format 157 * @pixfmts_cap: list of CAPTURE pixel formats available with pixfmt 158 */ 159 struct amvdec_format { 160 u32 pixfmt; 161 u32 min_buffers; 162 u32 max_buffers; 163 u32 max_width; 164 u32 max_height; 165 u32 flags; 166 167 struct amvdec_ops *vdec_ops; 168 struct amvdec_codec_ops *codec_ops; 169 170 char *firmware_path; 171 u32 pixfmts_cap[4]; 172 }; 173 174 enum amvdec_status { 175 STATUS_STOPPED, 176 STATUS_INIT, 177 STATUS_RUNNING, 178 STATUS_NEEDS_RESUME, 179 }; 180 181 /** 182 * struct amvdec_session - decoding session parameters 183 * 184 * @core: reference to the vdec core struct 185 * @fh: v4l2 file handle 186 * @m2m_dev: v4l2 m2m device 187 * @m2m_ctx: v4l2 m2m context 188 * @ctrl_handler: V4L2 control handler 189 * @ctrl_min_buf_capture: V4L2 control V4L2_CID_MIN_BUFFERS_FOR_CAPTURE 190 * @lock: cap & out queues lock 191 * @fmt_out: vdec pixel format for the OUTPUT queue 192 * @pixfmt_cap: V4L2 pixel format for the CAPTURE queue 193 * @src_buffer_size: size in bytes of the OUTPUT buffers' only plane 194 * @width: current picture width 195 * @height: current picture height 196 * @colorspace: current colorspace 197 * @ycbcr_enc: current ycbcr_enc 198 * @quantization: current quantization 199 * @xfer_func: current transfer function 200 * @pixelaspect: Pixel Aspect Ratio reported by the decoder 201 * @esparser_queued_bufs: number of buffers currently queued into ESPARSER 202 * @esparser_queue_work: work struct for the ESPARSER to process src buffers 203 * @streamon_cap: stream on flag for capture queue 204 * @streamon_out: stream on flag for output queue 205 * @sequence_cap: capture sequence counter 206 * @sequence_out: output sequence counter 207 * @should_stop: flag set if userspace signaled EOS via command 208 * or empty buffer 209 * @keyframe_found: flag set once a keyframe has been parsed 210 * @num_dst_bufs: number of destination buffers 211 * @changed_format: the format changed 212 * @canvas_alloc: array of all the canvas IDs allocated 213 * @canvas_num: number of canvas IDs allocated 214 * @vififo_vaddr: virtual address for the VIFIFO 215 * @vififo_paddr: physical address for the VIFIFO 216 * @vififo_size: size of the VIFIFO dma alloc 217 * @bufs_recycle: list of buffers that need to be recycled 218 * @bufs_recycle_lock: lock for the bufs_recycle list 219 * @recycle_thread: task struct for the recycling thread 220 * @timestamps: chronological list of src timestamps 221 * @ts_spinlock: spinlock for the timestamps list 222 * @last_irq_jiffies: tracks last time the vdec triggered an IRQ 223 * @last_offset: tracks last offset of vififo 224 * @wrap_count: number of times the vififo wrapped around 225 * @fw_idx_to_vb2_idx: firmware buffer index to vb2 buffer index 226 * @status: current decoding status 227 * @priv: codec private data 228 */ 229 struct amvdec_session { 230 struct amvdec_core *core; 231 232 struct v4l2_fh fh; 233 struct v4l2_m2m_dev *m2m_dev; 234 struct v4l2_m2m_ctx *m2m_ctx; 235 struct v4l2_ctrl_handler ctrl_handler; 236 struct v4l2_ctrl *ctrl_min_buf_capture; 237 struct mutex lock; 238 239 const struct amvdec_format *fmt_out; 240 u32 pixfmt_cap; 241 u32 src_buffer_size; 242 243 u32 width; 244 u32 height; 245 u32 colorspace; 246 u8 ycbcr_enc; 247 u8 quantization; 248 u8 xfer_func; 249 250 struct v4l2_fract pixelaspect; 251 252 atomic_t esparser_queued_bufs; 253 struct work_struct esparser_queue_work; 254 255 unsigned int streamon_cap, streamon_out; 256 unsigned int sequence_cap, sequence_out; 257 unsigned int should_stop; 258 unsigned int keyframe_found; 259 unsigned int num_dst_bufs; 260 unsigned int changed_format; 261 262 u8 canvas_alloc[MAX_CANVAS]; 263 u32 canvas_num; 264 265 void *vififo_vaddr; 266 dma_addr_t vififo_paddr; 267 u32 vififo_size; 268 269 struct list_head bufs_recycle; 270 struct mutex bufs_recycle_lock; /* bufs_recycle list lock */ 271 struct task_struct *recycle_thread; 272 273 struct list_head timestamps; 274 spinlock_t ts_spinlock; /* timestamp list lock */ 275 276 u64 last_irq_jiffies; 277 u32 last_offset; 278 u32 wrap_count; 279 u32 fw_idx_to_vb2_idx[32]; 280 281 enum amvdec_status status; 282 void *priv; 283 }; 284 285 u32 amvdec_get_output_size(struct amvdec_session *sess); 286 287 #endif 288