1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Copyright (c) 2021 MediaTek Inc.
4  * Author: Yunfei Dong <yunfei.dong@mediatek.com>
5  */
6 
7 #ifndef _VDEC_MSG_QUEUE_H_
8 #define _VDEC_MSG_QUEUE_H_
9 
10 #include <linux/sched.h>
11 #include <linux/semaphore.h>
12 #include <linux/slab.h>
13 #include <media/videobuf2-v4l2.h>
14 
15 #include "mtk_vcodec_util.h"
16 
17 #define NUM_BUFFER_COUNT 3
18 
19 struct vdec_lat_buf;
20 struct mtk_vcodec_ctx;
21 struct mtk_vcodec_dev;
22 typedef int (*core_decode_cb_t)(struct vdec_lat_buf *lat_buf);
23 
24 /**
25  * struct vdec_msg_queue_ctx - represents a queue for buffers ready to be processed
26  * @ready_to_use: ready used queue used to signalize when get a job queue
27  * @ready_queue: list of ready lat buffer queues
28  * @ready_lock: spin lock to protect the lat buffer usage
29  * @ready_num: number of buffers ready to be processed
30  * @hardware_index: hardware id that this queue is used for
31  */
32 struct vdec_msg_queue_ctx {
33 	wait_queue_head_t ready_to_use;
34 	struct list_head ready_queue;
35 	/* protect lat buffer */
36 	spinlock_t ready_lock;
37 	int ready_num;
38 	int hardware_index;
39 };
40 
41 /**
42  * struct vdec_lat_buf - lat buffer message used to store lat info for core decode
43  * @wdma_err_addr: wdma error address used for lat hardware
44  * @slice_bc_addr: slice bc address used for lat hardware
45  * @ts_info: need to set timestamp from output to capture
46  * @src_buf_req: output buffer media request object
47  *
48  * @private_data: shared information used to lat and core hardware
49  * @ctx: mtk vcodec context information
50  * @core_decode: different codec use different decode callback function
51  * @lat_list: add lat buffer to lat head list
52  * @core_list: add lat buffer to core head list
53  */
54 struct vdec_lat_buf {
55 	struct mtk_vcodec_mem wdma_err_addr;
56 	struct mtk_vcodec_mem slice_bc_addr;
57 	struct vb2_v4l2_buffer ts_info;
58 	struct media_request *src_buf_req;
59 
60 	void *private_data;
61 	struct mtk_vcodec_ctx *ctx;
62 	core_decode_cb_t core_decode;
63 	struct list_head lat_list;
64 	struct list_head core_list;
65 };
66 
67 /**
68  * struct vdec_msg_queue - used to store lat buffer message
69  * @lat_buf: lat buffer used to store lat buffer information
70  * @wdma_addr: wdma address used for ube
71  * @wdma_rptr_addr: ube read point
72  * @wdma_wptr_addr: ube write point
73  * @core_work: core hardware work
74  * @lat_ctx: used to store lat buffer list
75  */
76 struct vdec_msg_queue {
77 	struct vdec_lat_buf lat_buf[NUM_BUFFER_COUNT];
78 
79 	struct mtk_vcodec_mem wdma_addr;
80 	u64 wdma_rptr_addr;
81 	u64 wdma_wptr_addr;
82 
83 	struct work_struct core_work;
84 	struct vdec_msg_queue_ctx lat_ctx;
85 };
86 
87 /**
88  * vdec_msg_queue_init - init lat buffer information.
89  * @msg_queue: used to store the lat buffer information
90  * @ctx: v4l2 ctx
91  * @core_decode: core decode callback for each codec
92  * @private_size: the private data size used to share with core
93  *
94  * Return: returns 0 if init successfully, or fail.
95  */
96 int vdec_msg_queue_init(struct vdec_msg_queue *msg_queue,
97 			struct mtk_vcodec_ctx *ctx, core_decode_cb_t core_decode,
98 			int private_size);
99 
100 /**
101  * vdec_msg_queue_init_ctx - used to init msg queue context information.
102  * @ctx: message queue context
103  * @hardware_index: hardware index
104  */
105 void vdec_msg_queue_init_ctx(struct vdec_msg_queue_ctx *ctx, int hardware_index);
106 
107 /**
108  * vdec_msg_queue_qbuf - enqueue lat buffer to queue list.
109  * @ctx: message queue context
110  * @buf: current lat buffer
111  *
112  * Return: returns 0 if qbuf successfully, or fail.
113  */
114 int vdec_msg_queue_qbuf(struct vdec_msg_queue_ctx *ctx, struct vdec_lat_buf *buf);
115 
116 /**
117  * vdec_msg_queue_dqbuf - dequeue lat buffer from queue list.
118  * @ctx: message queue context
119  *
120  * Return: returns not null if dq successfully, or fail.
121  */
122 struct vdec_lat_buf *vdec_msg_queue_dqbuf(struct vdec_msg_queue_ctx *ctx);
123 
124 /**
125  * vdec_msg_queue_update_ube_rptr - used to updata the ube read point.
126  * @msg_queue: used to store the lat buffer information
127  * @ube_rptr: current ube read point
128  */
129 void vdec_msg_queue_update_ube_rptr(struct vdec_msg_queue *msg_queue, uint64_t ube_rptr);
130 
131 /**
132  * vdec_msg_queue_update_ube_wptr - used to updata the ube write point.
133  * @msg_queue: used to store the lat buffer information
134  * @ube_wptr: current ube write point
135  */
136 void vdec_msg_queue_update_ube_wptr(struct vdec_msg_queue *msg_queue, uint64_t ube_wptr);
137 
138 /**
139  * vdec_msg_queue_wait_lat_buf_full - used to check whether all lat buffer
140  *                                    in lat list.
141  * @msg_queue: used to store the lat buffer information
142  *
143  * Return: returns true if successfully, or fail.
144  */
145 bool vdec_msg_queue_wait_lat_buf_full(struct vdec_msg_queue *msg_queue);
146 
147 /**
148  * vdec_msg_queue_deinit - deinit lat buffer information.
149  * @msg_queue: used to store the lat buffer information
150  * @ctx: v4l2 ctx
151  */
152 void vdec_msg_queue_deinit(struct vdec_msg_queue *msg_queue,
153 			   struct mtk_vcodec_ctx *ctx);
154 
155 #endif
156