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 23struct 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 */ 38struct 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 47struct 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 * @esparser_reset: RESET for the PARSER 64 * @vdec_dec: video device for the decoder 65 * @v4l2_dev: v4l2 device 66 * @cur_sess: current decoding session 67 */ 68struct amvdec_core { 69 void __iomem *dos_base; 70 void __iomem *esparser_base; 71 struct regmap *regmap_ao; 72 73 struct device *dev; 74 struct device *dev_dec; 75 const struct vdec_platform *platform; 76 77 struct meson_canvas *canvas; 78 79 struct clk *dos_parser_clk; 80 struct clk *dos_clk; 81 struct clk *vdec_1_clk; 82 struct clk *vdec_hevc_clk; 83 struct clk *vdec_hevcf_clk; 84 85 struct reset_control *esparser_reset; 86 87 struct video_device *vdev_dec; 88 struct v4l2_device v4l2_dev; 89 90 struct amvdec_session *cur_sess; 91 struct mutex lock; /* video device lock */ 92}; 93 94/** 95 * struct amvdec_ops - vdec operations 96 * 97 * @start: mandatory call when the vdec needs to initialize 98 * @stop: mandatory call when the vdec needs to stop 99 * @conf_esparser: mandatory call to let the vdec configure the ESPARSER 100 * @vififo_level: mandatory call to get the current amount of data 101 * in the VIFIFO 102 * @use_offsets: mandatory call. Returns 1 if the VDEC supports vififo offsets 103 */ 104struct amvdec_ops { 105 int (*start)(struct amvdec_session *sess); 106 int (*stop)(struct amvdec_session *sess); 107 void (*conf_esparser)(struct amvdec_session *sess); 108 u32 (*vififo_level)(struct amvdec_session *sess); 109}; 110 111/** 112 * struct amvdec_codec_ops - codec operations 113 * 114 * @start: mandatory call when the codec needs to initialize 115 * @stop: mandatory call when the codec needs to stop 116 * @load_extended_firmware: optional call to load additional firmware bits 117 * @num_pending_bufs: optional call to get the number of dst buffers on hold 118 * @can_recycle: optional call to know if the codec is ready to recycle 119 * a dst buffer 120 * @recycle: optional call to tell the codec to recycle a dst buffer. Must go 121 * in pair with @can_recycle 122 * @drain: optional call if the codec has a custom way of draining 123 * @eos_sequence: optional call to get an end sequence to send to esparser 124 * for flush. Mutually exclusive with @drain. 125 * @isr: mandatory call when the ISR triggers 126 * @threaded_isr: mandatory call for the threaded ISR 127 */ 128struct amvdec_codec_ops { 129 int (*start)(struct amvdec_session *sess); 130 int (*stop)(struct amvdec_session *sess); 131 int (*load_extended_firmware)(struct amvdec_session *sess, 132 const u8 *data, u32 len); 133 u32 (*num_pending_bufs)(struct amvdec_session *sess); 134 int (*can_recycle)(struct amvdec_core *core); 135 void (*recycle)(struct amvdec_core *core, u32 buf_idx); 136 void (*drain)(struct amvdec_session *sess); 137 void (*resume)(struct amvdec_session *sess); 138 const u8 * (*eos_sequence)(u32 *len); 139 irqreturn_t (*isr)(struct amvdec_session *sess); 140 irqreturn_t (*threaded_isr)(struct amvdec_session *sess); 141}; 142 143/** 144 * struct amvdec_format - describes one of the OUTPUT (src) format supported 145 * 146 * @pixfmt: V4L2 pixel format 147 * @min_buffers: minimum amount of CAPTURE (dst) buffers 148 * @max_buffers: maximum amount of CAPTURE (dst) buffers 149 * @max_width: maximum picture width supported 150 * @max_height: maximum picture height supported 151 * @flags: enum flags associated with this pixfmt 152 * @vdec_ops: the VDEC operations that support this format 153 * @codec_ops: the codec operations that support this format 154 * @firmware_path: Path to the firmware that supports this format 155 * @pixfmts_cap: list of CAPTURE pixel formats available with pixfmt 156 */ 157struct amvdec_format { 158 u32 pixfmt; 159 u32 min_buffers; 160 u32 max_buffers; 161 u32 max_width; 162 u32 max_height; 163 u32 flags; 164 165 struct amvdec_ops *vdec_ops; 166 struct amvdec_codec_ops *codec_ops; 167 168 char *firmware_path; 169 u32 pixfmts_cap[4]; 170}; 171 172enum amvdec_status { 173 STATUS_STOPPED, 174 STATUS_INIT, 175 STATUS_RUNNING, 176 STATUS_NEEDS_RESUME, 177}; 178 179/** 180 * struct amvdec_session - decoding session parameters 181 * 182 * @core: reference to the vdec core struct 183 * @fh: v4l2 file handle 184 * @m2m_dev: v4l2 m2m device 185 * @m2m_ctx: v4l2 m2m context 186 * @ctrl_handler: V4L2 control handler 187 * @ctrl_min_buf_capture: V4L2 control V4L2_CID_MIN_BUFFERS_FOR_CAPTURE 188 * @fmt_out: vdec pixel format for the OUTPUT queue 189 * @pixfmt_cap: V4L2 pixel format for the CAPTURE queue 190 * @src_buffer_size: size in bytes of the OUTPUT buffers' only plane 191 * @width: current picture width 192 * @height: current picture height 193 * @colorspace: current colorspace 194 * @ycbcr_enc: current ycbcr_enc 195 * @quantization: current quantization 196 * @xfer_func: current transfer function 197 * @pixelaspect: Pixel Aspect Ratio reported by the decoder 198 * @esparser_queued_bufs: number of buffers currently queued into ESPARSER 199 * @esparser_queue_work: work struct for the ESPARSER to process src buffers 200 * @streamon_cap: stream on flag for capture queue 201 * @streamon_out: stream on flag for output queue 202 * @sequence_cap: capture sequence counter 203 * @should_stop: flag set if userspace signaled EOS via command 204 * or empty buffer 205 * @keyframe_found: flag set once a keyframe has been parsed 206 * @canvas_alloc: array of all the canvas IDs allocated 207 * @canvas_num: number of canvas IDs allocated 208 * @vififo_vaddr: virtual address for the VIFIFO 209 * @vififo_paddr: physical address for the VIFIFO 210 * @vififo_size: size of the VIFIFO dma alloc 211 * @bufs_recycle: list of buffers that need to be recycled 212 * @bufs_recycle_lock: lock for the bufs_recycle list 213 * @recycle_thread: task struct for the recycling thread 214 * @timestamps: chronological list of src timestamps 215 * @ts_spinlock: spinlock for the timestamps list 216 * @last_irq_jiffies: tracks last time the vdec triggered an IRQ 217 * @status: current decoding status 218 * @priv: codec private data 219 */ 220struct amvdec_session { 221 struct amvdec_core *core; 222 223 struct v4l2_fh fh; 224 struct v4l2_m2m_dev *m2m_dev; 225 struct v4l2_m2m_ctx *m2m_ctx; 226 struct v4l2_ctrl_handler ctrl_handler; 227 struct v4l2_ctrl *ctrl_min_buf_capture; 228 struct mutex lock; /* cap & out queues lock */ 229 230 const struct amvdec_format *fmt_out; 231 u32 pixfmt_cap; 232 u32 src_buffer_size; 233 234 u32 width; 235 u32 height; 236 u32 colorspace; 237 u8 ycbcr_enc; 238 u8 quantization; 239 u8 xfer_func; 240 241 struct v4l2_fract pixelaspect; 242 243 atomic_t esparser_queued_bufs; 244 struct work_struct esparser_queue_work; 245 246 unsigned int streamon_cap, streamon_out; 247 unsigned int sequence_cap, sequence_out; 248 unsigned int should_stop; 249 unsigned int keyframe_found; 250 unsigned int num_dst_bufs; 251 unsigned int changed_format; 252 253 u8 canvas_alloc[MAX_CANVAS]; 254 u32 canvas_num; 255 256 void *vififo_vaddr; 257 dma_addr_t vififo_paddr; 258 u32 vififo_size; 259 260 struct list_head bufs_recycle; 261 struct mutex bufs_recycle_lock; /* bufs_recycle list lock */ 262 struct task_struct *recycle_thread; 263 264 struct list_head timestamps; 265 spinlock_t ts_spinlock; /* timestamp list lock */ 266 267 u64 last_irq_jiffies; 268 u32 last_offset; 269 u32 wrap_count; 270 u32 fw_idx_to_vb2_idx[32]; 271 272 enum amvdec_status status; 273 void *priv; 274}; 275 276u32 amvdec_get_output_size(struct amvdec_session *sess); 277 278#endif 279