9ece57db81f307511dfc78f6520d7a452b40ad2d
[AGL/meta-agl-devel.git] / meta-egvirt / recipes-kernel / kernel-module-virtio-video / files / virtio_video.h
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /* Common header for virtio video driver.
3  *
4  * Copyright 2020 OpenSynergy GmbH.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #ifndef _VIRTIO_VIDEO_H
21 #define _VIRTIO_VIDEO_H
22
23 #include <linux/virtio.h>
24 #include <linux/virtio_ids.h>
25 #include <linux/virtio_config.h>
26 #include <linux/virtio_video.h>
27 #include <linux/list.h>
28 #include <linux/completion.h>
29 #include <media/v4l2-device.h>
30 #include <media/v4l2-mem2mem.h>
31 #include <media/v4l2-ctrls.h>
32 #include <media/videobuf2-dma-sg.h>
33 #include <media/videobuf2-dma-contig.h>
34
35 #define DRIVER_NAME "virtio-video"
36
37 #define VIRTIO_ID_VIDEO_DEC 31
38 #define VIRTIO_ID_VIDEO_ENC 30
39 #define VIRTIO_ID_VIDEO_CAM 100
40
41 #define MIN_BUFS_MIN 0
42 #define MIN_BUFS_MAX VIDEO_MAX_FRAME
43 #define MIN_BUFS_STEP 1
44 #define MIN_BUFS_DEF 1
45
46 struct video_format_frame {
47         struct virtio_video_format_frame frame;
48         struct virtio_video_format_range *frame_rates;
49 };
50
51 struct video_format {
52         struct list_head formats_list_entry;
53         struct virtio_video_format_desc desc;
54         struct video_format_frame *frames;
55 };
56
57 struct video_control_fmt_data {
58         uint32_t min;
59         uint32_t max;
60         uint32_t num;
61         uint32_t skip_mask;
62         uint32_t *entries;
63 };
64
65 struct video_control_format {
66         struct list_head controls_list_entry;
67         uint32_t format;
68         struct video_control_fmt_data *profile;
69         struct video_control_fmt_data *level;
70 };
71
72 struct video_plane_format {
73         uint32_t plane_size;
74         uint32_t stride;
75 };
76
77 struct video_format_info {
78         uint32_t fourcc_format;
79         uint32_t frame_rate;
80         uint32_t frame_width;
81         uint32_t frame_height;
82         uint32_t min_buffers;
83         uint32_t max_buffers;
84         struct virtio_video_crop crop;
85         uint32_t num_planes;
86         struct video_plane_format plane_format[VIRTIO_VIDEO_MAX_PLANES];
87 };
88
89 struct video_control_info {
90         uint32_t profile;
91         uint32_t level;
92         uint32_t bitrate;
93 };
94
95 struct virtio_video_device;
96 struct virtio_video_vbuffer;
97
98 typedef void (*virtio_video_resp_cb)(struct virtio_video_device *vvd,
99                                      struct virtio_video_vbuffer *vbuf);
100
101 struct virtio_video_vbuffer {
102         char *buf;
103         int size;
104         uint32_t id;
105
106         void *data_buf;
107         uint32_t data_size;
108
109         char *resp_buf;
110         int resp_size;
111
112         void *priv;
113         virtio_video_resp_cb resp_cb;
114
115         bool is_sync;
116         struct completion reclaimed;
117
118         struct list_head pending_list_entry;
119 };
120
121 struct virtio_video_cmd_queue {
122         struct virtqueue *vq;
123         bool ready;
124         spinlock_t qlock;
125         wait_queue_head_t reclaim_queue;
126 };
127
128 struct virtio_video_event_queue {
129         struct virtqueue *vq;
130         bool ready;
131         struct work_struct work;
132 };
133
134 enum video_stream_state {
135         STREAM_STATE_IDLE = 0,
136         STREAM_STATE_INIT,
137         STREAM_STATE_DYNAMIC_RES_CHANGE, /* specific to decoder */
138         STREAM_STATE_RUNNING,
139         STREAM_STATE_DRAIN,
140         STREAM_STATE_STOPPED,
141         STREAM_STATE_RESET, /* specific to encoder */
142         STREAM_STATE_ERROR,
143 };
144
145 struct virtio_video_stream {
146         uint32_t stream_id;
147         atomic_t state;
148         struct video_device *video_dev;
149         struct v4l2_fh fh;
150         struct mutex vq_mutex;
151         struct v4l2_ctrl_handler ctrl_handler;
152         struct video_format_info in_info;
153         struct video_format_info out_info;
154         struct video_control_info control;
155         struct video_format_frame *current_frame;
156 };
157
158 struct virtio_video_device {
159         struct virtio_device *vdev;
160         struct virtio_video_cmd_queue commandq;
161         struct virtio_video_event_queue eventq;
162         wait_queue_head_t wq;
163
164         struct kmem_cache *vbufs;
165         struct virtio_video_event *evts;
166
167         struct idr resource_idr;
168         spinlock_t resource_idr_lock;
169         struct idr stream_idr;
170         spinlock_t stream_idr_lock;
171
172         uint32_t max_caps_len;
173         uint32_t max_resp_len;
174
175         bool has_iommu;
176         bool supp_non_contig;
177
178         int debug;
179         int use_dma_mem;
180
181         struct v4l2_device v4l2_dev;
182         struct video_device video_dev;
183         struct mutex video_dev_mutex;
184
185         bool is_m2m_dev;
186         struct v4l2_m2m_dev *m2m_dev;
187
188         /* non-m2m queue (camera) */
189         struct vb2_queue vb2_output_queue;
190         struct list_head pending_buf_list;
191         spinlock_t pending_buf_list_lock;
192
193         uint32_t vbufs_sent;
194         struct list_head pending_vbuf_list;
195
196         /* device_busy - to block multiple opens for non-m2m (camera) */
197         bool device_busy;
198
199         /* vid_dev_nr - try register starting at video device number */
200         int vid_dev_nr;
201
202         /* is_mplane_cam - camera has multiplanar capabilities (default true) */
203         bool is_mplane_cam;
204
205         /* VIRTIO_VIDEO_FUNC_ */
206         uint32_t type;
207
208         uint32_t num_input_fmts;
209         struct list_head input_fmt_list;
210
211         uint32_t num_output_fmts;
212         struct list_head output_fmt_list;
213
214         struct list_head controls_fmt_list;
215         struct virtio_video_device_ops *ops;
216 };
217
218 struct virtio_video_device_ops {
219         int (*init_ctrls)(struct virtio_video_stream *stream);
220         int (*init_queues)(void *priv, struct vb2_queue *src_vq,
221                            struct vb2_queue *dst_vq);
222         void* (*get_fmt_list)(struct virtio_video_device *vvd);
223 };
224
225 struct virtio_video_buffer {
226         struct v4l2_m2m_buffer v4l2_m2m_vb;
227         uint32_t resource_id;
228         bool queued;
229         struct list_head list;
230 };
231
232 static inline gfp_t
233 virtio_video_gfp_flags(struct virtio_video_device *vvd)
234 {
235         if (vvd->use_dma_mem)
236                 return GFP_DMA;
237         else
238                 return 0;
239 }
240
241 static inline const struct vb2_mem_ops *
242 virtio_video_mem_ops(struct virtio_video_device *vvd)
243 {
244         if (vvd->supp_non_contig)
245                 return &vb2_dma_sg_memops;
246         else
247                 return &vb2_dma_contig_memops;
248 }
249
250 static inline struct virtio_video_device *
251 to_virtio_vd(struct video_device *video_dev)
252 {
253         return container_of(video_dev, struct virtio_video_device,
254                          video_dev);
255 }
256
257 static inline struct virtio_video_stream *file2stream(struct file *file)
258 {
259         return container_of(file->private_data, struct virtio_video_stream, fh);
260 }
261
262 static inline struct virtio_video_stream *ctrl2stream(struct v4l2_ctrl *ctrl)
263 {
264         return container_of(ctrl->handler, struct virtio_video_stream,
265                             ctrl_handler);
266 }
267
268 static inline struct virtio_video_buffer *to_virtio_vb(struct vb2_buffer *vb)
269 {
270         struct vb2_v4l2_buffer *v4l2_vb = to_vb2_v4l2_buffer(vb);
271
272         return container_of(v4l2_vb, struct virtio_video_buffer,
273                             v4l2_m2m_vb.vb);
274 }
275
276 static inline enum virtio_video_queue_type
277 to_virtio_queue_type(enum v4l2_buf_type type)
278 {
279         if (V4L2_TYPE_IS_OUTPUT(type))
280                 return VIRTIO_VIDEO_QUEUE_TYPE_INPUT;
281         else
282                 return VIRTIO_VIDEO_QUEUE_TYPE_OUTPUT;
283 }
284
285 static inline bool within_range(uint32_t min, uint32_t val, uint32_t max)
286 {
287         return ((min <= val) && (val <= max));
288 }
289
290 static inline bool needs_alignment(uint32_t val, uint32_t a)
291 {
292         if (a == 0 || IS_ALIGNED(val, a))
293                 return false;
294
295         return true;
296 }
297
298 enum video_stream_state virtio_video_state(struct virtio_video_stream *stream);
299 void virtio_video_state_reset(struct virtio_video_stream *stream);
300 void virtio_video_state_update(struct virtio_video_stream *stream,
301                                enum video_stream_state new_state);
302
303 int virtio_video_alloc_vbufs(struct virtio_video_device *vvd);
304 void virtio_video_free_vbufs(struct virtio_video_device *vvd);
305 int virtio_video_alloc_events(struct virtio_video_device *vvd);
306
307 int virtio_video_device_init(struct virtio_video_device *vvd);
308 void virtio_video_device_deinit(struct virtio_video_device *vvd);
309
310 int virtio_video_dec_init(struct virtio_video_device *vvd);
311 int virtio_video_enc_init(struct virtio_video_device *vvd);
312 int virtio_video_cam_init(struct virtio_video_device *vvd);
313
314 void virtio_video_stream_id_get(struct virtio_video_device *vvd,
315                                 struct virtio_video_stream *stream,
316                                 uint32_t *id);
317 void virtio_video_stream_id_put(struct virtio_video_device *vvd, uint32_t id);
318 void virtio_video_resource_id_get(struct virtio_video_device *vvd,
319                                   uint32_t *id);
320 void virtio_video_resource_id_put(struct virtio_video_device *vvd, uint32_t id);
321
322 int virtio_video_cmd_stream_create(struct virtio_video_device *vvd,
323                                    uint32_t stream_id,
324                                    enum virtio_video_format format,
325                                    const char *tag);
326 int virtio_video_cmd_stream_destroy(struct virtio_video_device *vvd,
327                                     uint32_t stream_id);
328 int virtio_video_cmd_stream_drain(struct virtio_video_device *vvd,
329                                   uint32_t stream_id);
330 int virtio_video_cmd_resource_attach(struct virtio_video_device *vvd,
331                                      uint32_t stream_id, uint32_t resource_id,
332                                      enum virtio_video_queue_type queue_type,
333                                      void *buf, size_t buf_size);
334 int virtio_video_cmd_resource_queue(struct virtio_video_device *vvd,
335                                     uint32_t stream_id,
336                                     struct virtio_video_buffer *virtio_vb,
337                                     uint32_t data_size[], uint8_t num_data_size,
338                                     enum virtio_video_queue_type queue_type);
339 int virtio_video_cmd_queue_detach_resources(struct virtio_video_device *vvd,
340                                 struct virtio_video_stream *stream,
341                                 enum virtio_video_queue_type queue_type);
342 int virtio_video_cmd_queue_clear(struct virtio_video_device *vvd,
343                                  struct virtio_video_stream *stream,
344                                  enum virtio_video_queue_type queue_type);
345 int virtio_video_cmd_query_capability(struct virtio_video_device *vvd,
346                                       void *resp_buf, size_t resp_size,
347                                       enum virtio_video_queue_type queue_type);
348 int virtio_video_query_control_profile(struct virtio_video_device *vvd,
349                                        void *resp_buf, size_t resp_size,
350                                        enum virtio_video_format format);
351 int virtio_video_query_control_level(struct virtio_video_device *vvd,
352                                      void *resp_buf, size_t resp_size,
353                                      enum virtio_video_format format);
354 int virtio_video_cmd_set_params(struct virtio_video_device *vvd,
355                                 struct virtio_video_stream *stream,
356                                 struct video_format_info *format_info,
357                                 enum virtio_video_queue_type queue_type);
358 int virtio_video_cmd_get_params(struct virtio_video_device *vvd,
359                                 struct virtio_video_stream *stream,
360                                 enum virtio_video_queue_type queue_type);
361 int virtio_video_cmd_set_control(struct virtio_video_device *vvd,
362                                  uint32_t stream_id,
363                                  enum virtio_video_control_type control,
364                                  uint32_t value);
365 int virtio_video_cmd_get_control(struct virtio_video_device *vvd,
366                                  struct virtio_video_stream *stream,
367                                  enum virtio_video_control_type control);
368
369 void virtio_video_queue_res_chg_event(struct virtio_video_stream *stream);
370 void virtio_video_queue_eos_event(struct virtio_video_stream *stream);
371 void virtio_video_handle_error(struct virtio_video_stream *stream);
372 int virtio_video_queue_release_buffers(struct virtio_video_stream *stream,
373                                        enum virtio_video_queue_type queue_type);
374
375 void virtio_video_cmd_cb(struct virtqueue *vq);
376 void virtio_video_event_cb(struct virtqueue *vq);
377 void virtio_video_process_events(struct work_struct *work);
378
379 void virtio_video_buf_done(struct virtio_video_buffer *virtio_vb,
380                            uint32_t flags, uint64_t timestamp,
381                            uint32_t data_sizes[]);
382 int virtio_video_buf_plane_init(uint32_t idx,uint32_t resource_id,
383                                 struct virtio_video_device *vvd,
384                                 struct virtio_video_stream *stream,
385                                 struct vb2_buffer *vb);
386 int virtio_video_queue_setup(struct vb2_queue *vq, unsigned int *num_buffers,
387                              unsigned int *num_planes, unsigned int sizes[],
388                              struct device *alloc_devs[]);
389 int virtio_video_buf_init(struct vb2_buffer *vb);
390 void virtio_video_buf_cleanup(struct vb2_buffer *vb);
391 void virtio_video_buf_queue(struct vb2_buffer *vb);
392 int virtio_video_qbuf(struct file *file, void *priv,
393                       struct v4l2_buffer *buf);
394 int virtio_video_dqbuf(struct file *file, void *priv,
395                        struct v4l2_buffer *buf);
396 int virtio_video_querycap(struct file *file, void *fh,
397                           struct v4l2_capability *cap);
398 int virtio_video_enum_framesizes(struct file *file, void *fh,
399                                  struct v4l2_frmsizeenum *f);
400 int virtio_video_enum_framemintervals(struct file *file, void *fh,
401                                       struct v4l2_frmivalenum *f);
402 int virtio_video_g_fmt(struct file *file, void *fh, struct v4l2_format *f);
403 int virtio_video_s_fmt(struct file *file, void *fh, struct v4l2_format *f);
404 int virtio_video_try_fmt(struct virtio_video_stream *stream,
405                          struct v4l2_format *f);
406 int virtio_video_reqbufs(struct file *file, void *priv,
407                         struct v4l2_requestbuffers *rb);
408 int virtio_video_subscribe_event(struct v4l2_fh *fh,
409                                  const struct v4l2_event_subscription *sub);
410
411 void virtio_video_free_caps_list(struct list_head *caps_list);
412 int virtio_video_parse_virtio_capabilities(struct virtio_video_device *vvd,
413                                            void *input_buf, void *output_buf);
414 void virtio_video_clean_capability(struct virtio_video_device *vvd);
415 int virtio_video_parse_virtio_control(struct virtio_video_device *vvd);
416 void virtio_video_clean_control(struct virtio_video_device *vvd);
417
418 uint32_t virtio_video_format_to_v4l2(uint32_t format);
419 uint32_t virtio_video_control_to_v4l2(uint32_t control);
420 uint32_t virtio_video_profile_to_v4l2(uint32_t profile);
421 uint32_t virtio_video_level_to_v4l2(uint32_t level);
422 uint32_t virtio_video_v4l2_format_to_virtio(uint32_t v4l2_format);
423 uint32_t virtio_video_v4l2_control_to_virtio(uint32_t v4l2_control);
424 uint32_t virtio_video_v4l2_profile_to_virtio(uint32_t v4l2_profile);
425 uint32_t virtio_video_v4l2_level_to_virtio(uint32_t v4l2_level);
426
427 struct video_format *virtio_video_find_video_format(struct list_head *fmts_list,
428                                                     uint32_t fourcc);
429 void virtio_video_format_from_info(struct video_format_info *info,
430                                    struct v4l2_pix_format_mplane *pix_mp);
431 void virtio_video_format_fill_default_info(struct video_format_info *dst_info,
432                                           struct video_format_info *src_info);
433 void virtio_video_pix_fmt_sp2mp(const struct v4l2_pix_format *pix,
434                                 struct v4l2_pix_format_mplane *pix_mp);
435 void virtio_video_pix_fmt_mp2sp(const struct v4l2_pix_format_mplane *pix_mp,
436                                 struct v4l2_pix_format *pix);
437
438 int virtio_video_g_selection(struct file *file, void *fh,
439                              struct v4l2_selection *sel);
440
441 int virtio_video_stream_get_params(struct virtio_video_device *vvd,
442                                    struct virtio_video_stream *stream);
443 int virtio_video_stream_get_controls(struct virtio_video_device *vvd,
444                                      struct virtio_video_stream *stream);
445
446 #endif /* _VIRTIO_VIDEO_H */