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#define NUM_BUFFER_COUNT 3
16
17struct vdec_lat_buf;
18struct mtk_vcodec_dec_ctx;
19struct mtk_vcodec_dec_dev;
20typedef int (*core_decode_cb_t)(struct vdec_lat_buf *lat_buf);
21
22/**
23 * enum core_ctx_status - Context decode status for core hardwre.
24 * @CONTEXT_LIST_EMPTY: No buffer queued on core hardware(must always be 0)
25 * @CONTEXT_LIST_QUEUED: Buffer queued to core work list
26 * @CONTEXT_LIST_DEC_DONE: context decode done
27 */
28enum core_ctx_status {
29	CONTEXT_LIST_EMPTY = 0,
30	CONTEXT_LIST_QUEUED,
31	CONTEXT_LIST_DEC_DONE,
32};
33
34/**
35 * struct vdec_msg_queue_ctx - represents a queue for buffers ready to be processed
36 * @ready_to_use: ready used queue used to signalize when get a job queue
37 * @ready_queue: list of ready lat buffer queues
38 * @ready_lock: spin lock to protect the lat buffer usage
39 * @ready_num: number of buffers ready to be processed
40 * @hardware_index: hardware id that this queue is used for
41 */
42struct vdec_msg_queue_ctx {
43	wait_queue_head_t ready_to_use;
44	struct list_head ready_queue;
45	/* protect lat buffer */
46	spinlock_t ready_lock;
47	int ready_num;
48	int hardware_index;
49};
50
51/**
52 * struct vdec_lat_buf - lat buffer message used to store lat info for core decode
53 * @wdma_err_addr: wdma error address used for lat hardware
54 * @slice_bc_addr: slice bc address used for lat hardware
55 * @rd_mv_addr:	mv addr for av1 lat hardware output, core hardware input
56 * @tile_addr:	tile buffer for av1 core input
57 * @ts_info: need to set timestamp from output to capture
58 * @src_buf_req: output buffer media request object
59 *
60 * @private_data: shared information used to lat and core hardware
61 * @ctx: mtk vcodec context information
62 * @core_decode: different codec use different decode callback function
63 * @lat_list: add lat buffer to lat head list
64 * @core_list: add lat buffer to core head list
65 *
66 * @is_last_frame: meaning this buffer is the last frame
67 */
68struct vdec_lat_buf {
69	struct mtk_vcodec_mem wdma_err_addr;
70	struct mtk_vcodec_mem slice_bc_addr;
71	struct mtk_vcodec_mem rd_mv_addr;
72	struct mtk_vcodec_mem tile_addr;
73	struct vb2_v4l2_buffer ts_info;
74	struct media_request *src_buf_req;
75
76	void *private_data;
77	struct mtk_vcodec_dec_ctx *ctx;
78	core_decode_cb_t core_decode;
79	struct list_head lat_list;
80	struct list_head core_list;
81
82	bool is_last_frame;
83};
84
85/**
86 * struct vdec_msg_queue - used to store lat buffer message
87 * @lat_buf: lat buffer used to store lat buffer information
88 * @wdma_addr: wdma address used for ube
89 * @wdma_rptr_addr: ube read point
90 * @wdma_wptr_addr: ube write point
91 * @core_work: core hardware work
92 * @lat_ctx: used to store lat buffer list
93 * @core_ctx: used to store core buffer list
94 *
95 * @lat_list_cnt: used to record each instance lat list count
96 * @core_list_cnt: used to record each instance core list count
97 * @flush_done: core flush done status
98 * @empty_lat_buf: the last lat buf used to flush decode
99 * @core_dec_done: core work queue decode done event
100 * @status: current context decode status for core hardware
101 * @ctx: mtk vcodec context information
102 */
103struct vdec_msg_queue {
104	struct vdec_lat_buf lat_buf[NUM_BUFFER_COUNT];
105
106	struct mtk_vcodec_mem wdma_addr;
107	u64 wdma_rptr_addr;
108	u64 wdma_wptr_addr;
109
110	struct work_struct core_work;
111	struct vdec_msg_queue_ctx lat_ctx;
112	struct vdec_msg_queue_ctx core_ctx;
113
114	atomic_t lat_list_cnt;
115	atomic_t core_list_cnt;
116	bool flush_done;
117	struct vdec_lat_buf empty_lat_buf;
118	wait_queue_head_t core_dec_done;
119	int status;
120	struct mtk_vcodec_dec_ctx *ctx;
121};
122
123/**
124 * vdec_msg_queue_init - init lat buffer information.
125 * @msg_queue: used to store the lat buffer information
126 * @ctx: v4l2 ctx
127 * @core_decode: core decode callback for each codec
128 * @private_size: the private data size used to share with core
129 *
130 * Return: returns 0 if init successfully, or fail.
131 */
132int vdec_msg_queue_init(struct vdec_msg_queue *msg_queue,
133			struct mtk_vcodec_dec_ctx *ctx, core_decode_cb_t core_decode,
134			int private_size);
135
136/**
137 * vdec_msg_queue_init_ctx - used to init msg queue context information.
138 * @ctx: message queue context
139 * @hardware_index: hardware index
140 */
141void vdec_msg_queue_init_ctx(struct vdec_msg_queue_ctx *ctx, int hardware_index);
142
143/**
144 * vdec_msg_queue_qbuf - enqueue lat buffer to queue list.
145 * @ctx: message queue context
146 * @buf: current lat buffer
147 *
148 * Return: returns 0 if qbuf successfully, or fail.
149 */
150int vdec_msg_queue_qbuf(struct vdec_msg_queue_ctx *ctx, struct vdec_lat_buf *buf);
151
152/**
153 * vdec_msg_queue_dqbuf - dequeue lat buffer from queue list.
154 * @ctx: message queue context
155 *
156 * Return: returns not null if dq successfully, or fail.
157 */
158struct vdec_lat_buf *vdec_msg_queue_dqbuf(struct vdec_msg_queue_ctx *ctx);
159
160/**
161 * vdec_msg_queue_update_ube_rptr - used to updata the ube read point.
162 * @msg_queue: used to store the lat buffer information
163 * @ube_rptr: current ube read point
164 */
165void vdec_msg_queue_update_ube_rptr(struct vdec_msg_queue *msg_queue, uint64_t ube_rptr);
166
167/**
168 * vdec_msg_queue_update_ube_wptr - used to updata the ube write point.
169 * @msg_queue: used to store the lat buffer information
170 * @ube_wptr: current ube write point
171 */
172void vdec_msg_queue_update_ube_wptr(struct vdec_msg_queue *msg_queue, uint64_t ube_wptr);
173
174/**
175 * vdec_msg_queue_wait_lat_buf_full - used to check whether all lat buffer
176 *                                    in lat list.
177 * @msg_queue: used to store the lat buffer information
178 *
179 * Return: returns true if successfully, or fail.
180 */
181bool vdec_msg_queue_wait_lat_buf_full(struct vdec_msg_queue *msg_queue);
182
183/**
184 * vdec_msg_queue_deinit - deinit lat buffer information.
185 * @msg_queue: used to store the lat buffer information
186 * @ctx: v4l2 ctx
187 */
188void vdec_msg_queue_deinit(struct vdec_msg_queue *msg_queue,
189			   struct mtk_vcodec_dec_ctx *ctx);
190
191#endif
192