17e1659ccSBen Skeggs /* SPDX-License-Identifier: MIT */
27e1659ccSBen Skeggs #ifndef __NVKM_FALCON_QMGR_H__
37e1659ccSBen Skeggs #define __NVKM_FALCON_QMGR_H__
47e1659ccSBen Skeggs #include <core/falcon.h>
57e1659ccSBen Skeggs #include "msgqueue.h"
67e1659ccSBen Skeggs 
7c80157a2SBen Skeggs #define HDR_SIZE sizeof(struct nv_falcon_msg)
87e1659ccSBen Skeggs #define QUEUE_ALIGNMENT 4
97e1659ccSBen Skeggs /* max size of the messages we can receive */
107e1659ccSBen Skeggs #define MSG_BUF_SIZE 128
117e1659ccSBen Skeggs 
120ae59432SBen Skeggs /**
130ae59432SBen Skeggs  * struct nvkm_msgqueue_seq - keep track of ongoing commands
140ae59432SBen Skeggs  *
150ae59432SBen Skeggs  * Every time a command is sent, a sequence is assigned to it so the
160ae59432SBen Skeggs  * corresponding message can be matched. Upon receiving the message, a callback
170ae59432SBen Skeggs  * can be called and/or a completion signaled.
180ae59432SBen Skeggs  *
190ae59432SBen Skeggs  * @id:		sequence ID
200ae59432SBen Skeggs  * @state:	current state
210ae59432SBen Skeggs  * @callback:	callback to call upon receiving matching message
220ae59432SBen Skeggs  * @completion:	completion to signal after callback is called
230ae59432SBen Skeggs  */
240ae59432SBen Skeggs struct nvkm_msgqueue_seq {
250ae59432SBen Skeggs 	u16 id;
260ae59432SBen Skeggs 	enum {
270ae59432SBen Skeggs 		SEQ_STATE_FREE = 0,
280ae59432SBen Skeggs 		SEQ_STATE_PENDING,
290ae59432SBen Skeggs 		SEQ_STATE_USED,
300ae59432SBen Skeggs 		SEQ_STATE_CANCELLED
310ae59432SBen Skeggs 	} state;
32c80157a2SBen Skeggs 	nvkm_falcon_qmgr_callback callback;
33c80157a2SBen Skeggs 	void *priv;
340ae59432SBen Skeggs 	struct completion *completion;
35c80157a2SBen Skeggs 	int result;
368763955bSBen Skeggs };
378763955bSBen Skeggs 
380ae59432SBen Skeggs /*
390ae59432SBen Skeggs  * We can have an arbitrary number of sequences, but realistically we will
400ae59432SBen Skeggs  * probably not use that much simultaneously.
410ae59432SBen Skeggs  */
420ae59432SBen Skeggs #define NVKM_MSGQUEUE_NUM_SEQUENCES 16
430ae59432SBen Skeggs 
440ae59432SBen Skeggs struct nvkm_falcon_qmgr {
450ae59432SBen Skeggs 	struct nvkm_falcon *falcon;
460ae59432SBen Skeggs 
470ae59432SBen Skeggs 	struct mutex seq_lock;
480ae59432SBen Skeggs 	struct nvkm_msgqueue_seq seq[NVKM_MSGQUEUE_NUM_SEQUENCES];
490ae59432SBen Skeggs 	unsigned long seq_tbl[BITS_TO_LONGS(NVKM_MSGQUEUE_NUM_SEQUENCES)];
500ae59432SBen Skeggs };
510ae59432SBen Skeggs 
520ae59432SBen Skeggs struct nvkm_msgqueue_seq *
530ae59432SBen Skeggs nvkm_falcon_qmgr_seq_acquire(struct nvkm_falcon_qmgr *);
540ae59432SBen Skeggs void nvkm_falcon_qmgr_seq_release(struct nvkm_falcon_qmgr *,
550ae59432SBen Skeggs 				  struct nvkm_msgqueue_seq *);
56acc466abSBen Skeggs 
57acc466abSBen Skeggs #define FLCNQ_PRINTK(t,q,f,a...)                                               \
58acc466abSBen Skeggs        FLCN_PRINTK(t, (q)->qmgr->falcon, "%s: "f, (q)->name, ##a)
59acc466abSBen Skeggs #define FLCNQ_DBG(q,f,a...) FLCNQ_PRINTK(debug, (q), f, ##a)
60acc466abSBen Skeggs #define FLCNQ_ERR(q,f,a...) FLCNQ_PRINTK(error, (q), f, ##a)
617e1659ccSBen Skeggs #endif
62