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 /**
13a15d8f58SBen Skeggs  * struct nvkm_falcon_qmgr_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  */
24a15d8f58SBen Skeggs struct nvkm_falcon_qmgr_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;
328e90a98dSBen Skeggs 	bool async;
33c80157a2SBen Skeggs 	nvkm_falcon_qmgr_callback callback;
34c80157a2SBen Skeggs 	void *priv;
358e90a98dSBen Skeggs 	struct completion done;
36c80157a2SBen Skeggs 	int result;
378763955bSBen Skeggs };
388763955bSBen Skeggs 
390ae59432SBen Skeggs /*
400ae59432SBen Skeggs  * We can have an arbitrary number of sequences, but realistically we will
410ae59432SBen Skeggs  * probably not use that much simultaneously.
420ae59432SBen Skeggs  */
43a15d8f58SBen Skeggs #define NVKM_FALCON_QMGR_SEQ_NUM 16
440ae59432SBen Skeggs 
450ae59432SBen Skeggs struct nvkm_falcon_qmgr {
460ae59432SBen Skeggs 	struct nvkm_falcon *falcon;
470ae59432SBen Skeggs 
48a15d8f58SBen Skeggs 	struct {
49a15d8f58SBen Skeggs 		struct mutex mutex;
50a15d8f58SBen Skeggs 		struct nvkm_falcon_qmgr_seq id[NVKM_FALCON_QMGR_SEQ_NUM];
51a15d8f58SBen Skeggs 		unsigned long tbl[BITS_TO_LONGS(NVKM_FALCON_QMGR_SEQ_NUM)];
52a15d8f58SBen Skeggs 	} seq;
530ae59432SBen Skeggs };
540ae59432SBen Skeggs 
55a15d8f58SBen Skeggs struct nvkm_falcon_qmgr_seq *
560ae59432SBen Skeggs nvkm_falcon_qmgr_seq_acquire(struct nvkm_falcon_qmgr *);
570ae59432SBen Skeggs void nvkm_falcon_qmgr_seq_release(struct nvkm_falcon_qmgr *,
58a15d8f58SBen Skeggs 				  struct nvkm_falcon_qmgr_seq *);
59acc466abSBen Skeggs 
60acc466abSBen Skeggs #define FLCNQ_PRINTK(t,q,f,a...)                                               \
61acc466abSBen Skeggs        FLCN_PRINTK(t, (q)->qmgr->falcon, "%s: "f, (q)->name, ##a)
62acc466abSBen Skeggs #define FLCNQ_DBG(q,f,a...) FLCNQ_PRINTK(debug, (q), f, ##a)
63acc466abSBen Skeggs #define FLCNQ_ERR(q,f,a...) FLCNQ_PRINTK(error, (q), f, ##a)
647e1659ccSBen Skeggs #endif
65