1/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * Copyright 2018-2020 Broadcom.
4 */
5
6#ifndef BCM_VK_MSG_H
7#define BCM_VK_MSG_H
8
9#include <uapi/linux/misc/bcm_vk.h>
10#include "bcm_vk_sg.h"
11
12/* Single message queue control structure */
13struct bcm_vk_msgq {
14	u16 type;	/* queue type */
15	u16 num;	/* queue number */
16	u32 start;	/* offset in BAR1 where the queue memory starts */
17
18	u32 rd_idx; /* read idx */
19	u32 wr_idx; /* write idx */
20
21	u32 size;	/*
22			 * size, which is in number of 16byte blocks,
23			 * to align with the message data structure.
24			 */
25	u32 nxt;	/*
26			 * nxt offset to the next msg queue struct.
27			 * This is to provide flexibity for alignment purposes.
28			 */
29
30/* Least significant 16 bits in below field hold doorbell register offset */
31#define DB_SHIFT 16
32
33	u32 db_offset; /* queue doorbell register offset in BAR0 */
34
35	u32 rsvd;
36};
37
38/*
39 * Structure to record static info from the msgq sync.  We keep local copy
40 * for some of these variables for both performance + checking purpose.
41 */
42struct bcm_vk_sync_qinfo {
43	void __iomem *q_start;
44	u32 q_size;
45	u32 q_mask;
46	u32 q_low;
47	u32 q_db_offset;
48};
49
50#define VK_MSGQ_MAX_NR 4 /* Maximum number of message queues */
51
52/*
53 * message block - basic unit in the message where a message's size is always
54 *		   N x sizeof(basic_block)
55 */
56struct vk_msg_blk {
57	u8 function_id;
58#define VK_FID_TRANS_BUF	5
59#define VK_FID_SHUTDOWN		8
60#define VK_FID_INIT		9
61	u8 size; /* size of the message in number of vk_msg_blk's */
62	u16 trans_id; /* transport id, queue & msg_id */
63	u32 context_id;
64#define VK_NEW_CTX		0
65	u32 cmd;
66#define VK_CMD_PLANES_MASK	0x000f /* number of planes to up/download */
67#define VK_CMD_UPLOAD		0x0400 /* memory transfer to vk */
68#define VK_CMD_DOWNLOAD		0x0500 /* memory transfer from vk */
69#define VK_CMD_MASK		0x0f00 /* command mask */
70	u32 arg;
71};
72
73/* vk_msg_blk is 16 bytes fixed */
74#define VK_MSGQ_BLK_SIZE   (sizeof(struct vk_msg_blk))
75/* shift for fast division of basic msg blk size */
76#define VK_MSGQ_BLK_SZ_SHIFT 4
77
78/* use msg_id 0 for any simplex host2vk communication */
79#define VK_SIMPLEX_MSG_ID 0
80
81/* context per session opening of sysfs */
82struct bcm_vk_ctx {
83	struct list_head node; /* use for linkage in Hash Table */
84	unsigned int idx;
85	bool in_use;
86	pid_t pid;
87	u32 hash_idx;
88	u32 q_num; /* queue number used by the stream */
89	struct miscdevice *miscdev;
90	atomic_t pend_cnt; /* number of items pending to be read from host */
91	atomic_t dma_cnt; /* any dma transaction outstanding */
92	wait_queue_head_t rd_wq;
93};
94
95/* pid hash table entry */
96struct bcm_vk_ht_entry {
97	struct list_head head;
98};
99
100#define VK_DMA_MAX_ADDRS 4 /* Max 4 DMA Addresses */
101/* structure for house keeping a single work entry */
102struct bcm_vk_wkent {
103	struct list_head node; /* for linking purpose */
104	struct bcm_vk_ctx *ctx;
105
106	/* Store up to 4 dma pointers */
107	struct bcm_vk_dma dma[VK_DMA_MAX_ADDRS];
108
109	u32 to_h_blks; /* response */
110	struct vk_msg_blk *to_h_msg;
111
112	/*
113	 * put the to_v_msg at the end so that we could simply append to_v msg
114	 * to the end of the allocated block
115	 */
116	u32 usr_msg_id;
117	u32 to_v_blks;
118	u32 seq_num;
119	struct vk_msg_blk to_v_msg[] __counted_by(to_v_blks);
120};
121
122/* queue stats counters */
123struct bcm_vk_qs_cnts {
124	u32 cnt; /* general counter, used to limit output */
125	u32 acc_sum;
126	u32 max_occ; /* max during a sampling period */
127	u32 max_abs; /* the abs max since reset */
128};
129
130/* control channel structure for either to_v or to_h communication */
131struct bcm_vk_msg_chan {
132	u32 q_nr;
133	/* Mutex to access msgq */
134	struct mutex msgq_mutex;
135	/* pointing to BAR locations */
136	struct bcm_vk_msgq __iomem *msgq[VK_MSGQ_MAX_NR];
137	/* Spinlock to access pending queue */
138	spinlock_t pendq_lock;
139	/* for temporary storing pending items, one for each queue */
140	struct list_head pendq[VK_MSGQ_MAX_NR];
141	/* static queue info from the sync */
142	struct bcm_vk_sync_qinfo sync_qinfo[VK_MSGQ_MAX_NR];
143};
144
145/* totol number of message q allowed by the driver */
146#define VK_MSGQ_PER_CHAN_MAX	3
147#define VK_MSGQ_NUM_DEFAULT	(VK_MSGQ_PER_CHAN_MAX - 1)
148
149/* total number of supported ctx, 32 ctx each for 5 components */
150#define VK_CMPT_CTX_MAX		(32 * 5)
151
152/* hash table defines to store the opened FDs */
153#define VK_PID_HT_SHIFT_BIT	7 /* 128 */
154#define VK_PID_HT_SZ		BIT(VK_PID_HT_SHIFT_BIT)
155
156/* The following are offsets of DDR info provided by the vk card */
157#define VK_BAR0_SEG_SIZE	(4 * SZ_1K) /* segment size for BAR0 */
158
159/* shutdown types supported */
160#define VK_SHUTDOWN_PID		1
161#define VK_SHUTDOWN_GRACEFUL	2
162
163#endif
164