162306a36Sopenharmony_ci/* SPDX-License-Identifier: MIT */
262306a36Sopenharmony_ci#ifndef __NVKM_FALCON_QMGR_H__
362306a36Sopenharmony_ci#define __NVKM_FALCON_QMGR_H__
462306a36Sopenharmony_ci#include <core/falcon.h>
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci#define HDR_SIZE sizeof(struct nvfw_falcon_msg)
762306a36Sopenharmony_ci#define QUEUE_ALIGNMENT 4
862306a36Sopenharmony_ci/* max size of the messages we can receive */
962306a36Sopenharmony_ci#define MSG_BUF_SIZE 128
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci/**
1262306a36Sopenharmony_ci * struct nvkm_falcon_qmgr_seq - keep track of ongoing commands
1362306a36Sopenharmony_ci *
1462306a36Sopenharmony_ci * Every time a command is sent, a sequence is assigned to it so the
1562306a36Sopenharmony_ci * corresponding message can be matched. Upon receiving the message, a callback
1662306a36Sopenharmony_ci * can be called and/or a completion signaled.
1762306a36Sopenharmony_ci *
1862306a36Sopenharmony_ci * @id:		sequence ID
1962306a36Sopenharmony_ci * @state:	current state
2062306a36Sopenharmony_ci * @callback:	callback to call upon receiving matching message
2162306a36Sopenharmony_ci * @completion:	completion to signal after callback is called
2262306a36Sopenharmony_ci */
2362306a36Sopenharmony_cistruct nvkm_falcon_qmgr_seq {
2462306a36Sopenharmony_ci	u16 id;
2562306a36Sopenharmony_ci	enum {
2662306a36Sopenharmony_ci		SEQ_STATE_FREE = 0,
2762306a36Sopenharmony_ci		SEQ_STATE_PENDING,
2862306a36Sopenharmony_ci		SEQ_STATE_USED,
2962306a36Sopenharmony_ci		SEQ_STATE_CANCELLED
3062306a36Sopenharmony_ci	} state;
3162306a36Sopenharmony_ci	bool async;
3262306a36Sopenharmony_ci	nvkm_falcon_qmgr_callback callback;
3362306a36Sopenharmony_ci	void *priv;
3462306a36Sopenharmony_ci	struct completion done;
3562306a36Sopenharmony_ci	int result;
3662306a36Sopenharmony_ci};
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ci/*
3962306a36Sopenharmony_ci * We can have an arbitrary number of sequences, but realistically we will
4062306a36Sopenharmony_ci * probably not use that much simultaneously.
4162306a36Sopenharmony_ci */
4262306a36Sopenharmony_ci#define NVKM_FALCON_QMGR_SEQ_NUM 16
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_cistruct nvkm_falcon_qmgr {
4562306a36Sopenharmony_ci	struct nvkm_falcon *falcon;
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ci	struct {
4862306a36Sopenharmony_ci		struct mutex mutex;
4962306a36Sopenharmony_ci		struct nvkm_falcon_qmgr_seq id[NVKM_FALCON_QMGR_SEQ_NUM];
5062306a36Sopenharmony_ci		unsigned long tbl[BITS_TO_LONGS(NVKM_FALCON_QMGR_SEQ_NUM)];
5162306a36Sopenharmony_ci	} seq;
5262306a36Sopenharmony_ci};
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_cistruct nvkm_falcon_qmgr_seq *
5562306a36Sopenharmony_cinvkm_falcon_qmgr_seq_acquire(struct nvkm_falcon_qmgr *);
5662306a36Sopenharmony_civoid nvkm_falcon_qmgr_seq_release(struct nvkm_falcon_qmgr *,
5762306a36Sopenharmony_ci				  struct nvkm_falcon_qmgr_seq *);
5862306a36Sopenharmony_ci
5962306a36Sopenharmony_cistruct nvkm_falcon_cmdq {
6062306a36Sopenharmony_ci	struct nvkm_falcon_qmgr *qmgr;
6162306a36Sopenharmony_ci	const char *name;
6262306a36Sopenharmony_ci	struct mutex mutex;
6362306a36Sopenharmony_ci	struct completion ready;
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ci	u32 head_reg;
6662306a36Sopenharmony_ci	u32 tail_reg;
6762306a36Sopenharmony_ci	u32 offset;
6862306a36Sopenharmony_ci	u32 size;
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_ci	u32 position;
7162306a36Sopenharmony_ci};
7262306a36Sopenharmony_ci
7362306a36Sopenharmony_cistruct nvkm_falcon_msgq {
7462306a36Sopenharmony_ci	struct nvkm_falcon_qmgr *qmgr;
7562306a36Sopenharmony_ci	const char *name;
7662306a36Sopenharmony_ci	spinlock_t lock;
7762306a36Sopenharmony_ci
7862306a36Sopenharmony_ci	u32 head_reg;
7962306a36Sopenharmony_ci	u32 tail_reg;
8062306a36Sopenharmony_ci	u32 offset;
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_ci	u32 position;
8362306a36Sopenharmony_ci};
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_ci#define FLCNQ_PRINTK(q,l,p,f,a...) FLCN_PRINTK((q)->qmgr->falcon, l, p, "%s: "f, (q)->name, ##a)
8662306a36Sopenharmony_ci#define FLCNQ_DBG(q,f,a...) FLCNQ_PRINTK((q), DEBUG, info, f, ##a)
8762306a36Sopenharmony_ci#define FLCNQ_ERR(q,f,a...) FLCNQ_PRINTK((q), ERROR, err, f, ##a)
8862306a36Sopenharmony_ci#endif
89