18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved.
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
58c2ecf20Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
68c2ecf20Sopenharmony_ci * to deal in the Software without restriction, including without limitation
78c2ecf20Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
88c2ecf20Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
98c2ecf20Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * The above copyright notice and this permission notice shall be included in
128c2ecf20Sopenharmony_ci * all copies or substantial portions of the Software.
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
158c2ecf20Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
168c2ecf20Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
178c2ecf20Sopenharmony_ci * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
188c2ecf20Sopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
198c2ecf20Sopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
208c2ecf20Sopenharmony_ci * OTHER DEALINGS IN THE SOFTWARE.
218c2ecf20Sopenharmony_ci *
228c2ecf20Sopenharmony_ci */
238c2ecf20Sopenharmony_ci#include "qmgr.h"
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_cistatic void
268c2ecf20Sopenharmony_cinvkm_falcon_msgq_open(struct nvkm_falcon_msgq *msgq)
278c2ecf20Sopenharmony_ci{
288c2ecf20Sopenharmony_ci	mutex_lock(&msgq->mutex);
298c2ecf20Sopenharmony_ci	msgq->position = nvkm_falcon_rd32(msgq->qmgr->falcon, msgq->tail_reg);
308c2ecf20Sopenharmony_ci}
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_cistatic void
338c2ecf20Sopenharmony_cinvkm_falcon_msgq_close(struct nvkm_falcon_msgq *msgq, bool commit)
348c2ecf20Sopenharmony_ci{
358c2ecf20Sopenharmony_ci	struct nvkm_falcon *falcon = msgq->qmgr->falcon;
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci	if (commit)
388c2ecf20Sopenharmony_ci		nvkm_falcon_wr32(falcon, msgq->tail_reg, msgq->position);
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci	mutex_unlock(&msgq->mutex);
418c2ecf20Sopenharmony_ci}
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_cistatic bool
448c2ecf20Sopenharmony_cinvkm_falcon_msgq_empty(struct nvkm_falcon_msgq *msgq)
458c2ecf20Sopenharmony_ci{
468c2ecf20Sopenharmony_ci	u32 head = nvkm_falcon_rd32(msgq->qmgr->falcon, msgq->head_reg);
478c2ecf20Sopenharmony_ci	u32 tail = nvkm_falcon_rd32(msgq->qmgr->falcon, msgq->tail_reg);
488c2ecf20Sopenharmony_ci	return head == tail;
498c2ecf20Sopenharmony_ci}
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_cistatic int
528c2ecf20Sopenharmony_cinvkm_falcon_msgq_pop(struct nvkm_falcon_msgq *msgq, void *data, u32 size)
538c2ecf20Sopenharmony_ci{
548c2ecf20Sopenharmony_ci	struct nvkm_falcon *falcon = msgq->qmgr->falcon;
558c2ecf20Sopenharmony_ci	u32 head, tail, available;
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	head = nvkm_falcon_rd32(falcon, msgq->head_reg);
588c2ecf20Sopenharmony_ci	/* has the buffer looped? */
598c2ecf20Sopenharmony_ci	if (head < msgq->position)
608c2ecf20Sopenharmony_ci		msgq->position = msgq->offset;
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci	tail = msgq->position;
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	available = head - tail;
658c2ecf20Sopenharmony_ci	if (size > available) {
668c2ecf20Sopenharmony_ci		FLCNQ_ERR(msgq, "requested %d bytes, but only %d available",
678c2ecf20Sopenharmony_ci			  size, available);
688c2ecf20Sopenharmony_ci		return -EINVAL;
698c2ecf20Sopenharmony_ci	}
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci	nvkm_falcon_read_dmem(falcon, tail, size, 0, data);
728c2ecf20Sopenharmony_ci	msgq->position += ALIGN(size, QUEUE_ALIGNMENT);
738c2ecf20Sopenharmony_ci	return 0;
748c2ecf20Sopenharmony_ci}
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_cistatic int
778c2ecf20Sopenharmony_cinvkm_falcon_msgq_read(struct nvkm_falcon_msgq *msgq, struct nvfw_falcon_msg *hdr)
788c2ecf20Sopenharmony_ci{
798c2ecf20Sopenharmony_ci	int ret = 0;
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	nvkm_falcon_msgq_open(msgq);
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	if (nvkm_falcon_msgq_empty(msgq))
848c2ecf20Sopenharmony_ci		goto close;
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci	ret = nvkm_falcon_msgq_pop(msgq, hdr, HDR_SIZE);
878c2ecf20Sopenharmony_ci	if (ret) {
888c2ecf20Sopenharmony_ci		FLCNQ_ERR(msgq, "failed to read message header");
898c2ecf20Sopenharmony_ci		goto close;
908c2ecf20Sopenharmony_ci	}
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	if (hdr->size > MSG_BUF_SIZE) {
938c2ecf20Sopenharmony_ci		FLCNQ_ERR(msgq, "message too big, %d bytes", hdr->size);
948c2ecf20Sopenharmony_ci		ret = -ENOSPC;
958c2ecf20Sopenharmony_ci		goto close;
968c2ecf20Sopenharmony_ci	}
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci	if (hdr->size > HDR_SIZE) {
998c2ecf20Sopenharmony_ci		u32 read_size = hdr->size - HDR_SIZE;
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci		ret = nvkm_falcon_msgq_pop(msgq, (hdr + 1), read_size);
1028c2ecf20Sopenharmony_ci		if (ret) {
1038c2ecf20Sopenharmony_ci			FLCNQ_ERR(msgq, "failed to read message data");
1048c2ecf20Sopenharmony_ci			goto close;
1058c2ecf20Sopenharmony_ci		}
1068c2ecf20Sopenharmony_ci	}
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	ret = 1;
1098c2ecf20Sopenharmony_ciclose:
1108c2ecf20Sopenharmony_ci	nvkm_falcon_msgq_close(msgq, (ret >= 0));
1118c2ecf20Sopenharmony_ci	return ret;
1128c2ecf20Sopenharmony_ci}
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_cistatic int
1158c2ecf20Sopenharmony_cinvkm_falcon_msgq_exec(struct nvkm_falcon_msgq *msgq, struct nvfw_falcon_msg *hdr)
1168c2ecf20Sopenharmony_ci{
1178c2ecf20Sopenharmony_ci	struct nvkm_falcon_qmgr_seq *seq;
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	seq = &msgq->qmgr->seq.id[hdr->seq_id];
1208c2ecf20Sopenharmony_ci	if (seq->state != SEQ_STATE_USED && seq->state != SEQ_STATE_CANCELLED) {
1218c2ecf20Sopenharmony_ci		FLCNQ_ERR(msgq, "message for unknown sequence %08x", seq->id);
1228c2ecf20Sopenharmony_ci		return -EINVAL;
1238c2ecf20Sopenharmony_ci	}
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	if (seq->state == SEQ_STATE_USED) {
1268c2ecf20Sopenharmony_ci		if (seq->callback)
1278c2ecf20Sopenharmony_ci			seq->result = seq->callback(seq->priv, hdr);
1288c2ecf20Sopenharmony_ci	}
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci	if (seq->async) {
1318c2ecf20Sopenharmony_ci		nvkm_falcon_qmgr_seq_release(msgq->qmgr, seq);
1328c2ecf20Sopenharmony_ci		return 0;
1338c2ecf20Sopenharmony_ci	}
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	complete_all(&seq->done);
1368c2ecf20Sopenharmony_ci	return 0;
1378c2ecf20Sopenharmony_ci}
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_civoid
1408c2ecf20Sopenharmony_cinvkm_falcon_msgq_recv(struct nvkm_falcon_msgq *msgq)
1418c2ecf20Sopenharmony_ci{
1428c2ecf20Sopenharmony_ci	/*
1438c2ecf20Sopenharmony_ci	 * We are invoked from a worker thread, so normally we have plenty of
1448c2ecf20Sopenharmony_ci	 * stack space to work with.
1458c2ecf20Sopenharmony_ci	 */
1468c2ecf20Sopenharmony_ci	u8 msg_buffer[MSG_BUF_SIZE];
1478c2ecf20Sopenharmony_ci	struct nvfw_falcon_msg *hdr = (void *)msg_buffer;
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	while (nvkm_falcon_msgq_read(msgq, hdr) > 0)
1508c2ecf20Sopenharmony_ci		nvkm_falcon_msgq_exec(msgq, hdr);
1518c2ecf20Sopenharmony_ci}
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ciint
1548c2ecf20Sopenharmony_cinvkm_falcon_msgq_recv_initmsg(struct nvkm_falcon_msgq *msgq,
1558c2ecf20Sopenharmony_ci			      void *data, u32 size)
1568c2ecf20Sopenharmony_ci{
1578c2ecf20Sopenharmony_ci	struct nvkm_falcon *falcon = msgq->qmgr->falcon;
1588c2ecf20Sopenharmony_ci	struct nvfw_falcon_msg *hdr = data;
1598c2ecf20Sopenharmony_ci	int ret;
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci	msgq->head_reg = falcon->func->msgq.head;
1628c2ecf20Sopenharmony_ci	msgq->tail_reg = falcon->func->msgq.tail;
1638c2ecf20Sopenharmony_ci	msgq->offset = nvkm_falcon_rd32(falcon, falcon->func->msgq.tail);
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	nvkm_falcon_msgq_open(msgq);
1668c2ecf20Sopenharmony_ci	ret = nvkm_falcon_msgq_pop(msgq, data, size);
1678c2ecf20Sopenharmony_ci	if (ret == 0 && hdr->size != size) {
1688c2ecf20Sopenharmony_ci		FLCN_ERR(falcon, "unexpected init message size %d vs %d",
1698c2ecf20Sopenharmony_ci			 hdr->size, size);
1708c2ecf20Sopenharmony_ci		ret = -EINVAL;
1718c2ecf20Sopenharmony_ci	}
1728c2ecf20Sopenharmony_ci	nvkm_falcon_msgq_close(msgq, ret == 0);
1738c2ecf20Sopenharmony_ci	return ret;
1748c2ecf20Sopenharmony_ci}
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_civoid
1778c2ecf20Sopenharmony_cinvkm_falcon_msgq_init(struct nvkm_falcon_msgq *msgq,
1788c2ecf20Sopenharmony_ci		      u32 index, u32 offset, u32 size)
1798c2ecf20Sopenharmony_ci{
1808c2ecf20Sopenharmony_ci	const struct nvkm_falcon_func *func = msgq->qmgr->falcon->func;
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci	msgq->head_reg = func->msgq.head + index * func->msgq.stride;
1838c2ecf20Sopenharmony_ci	msgq->tail_reg = func->msgq.tail + index * func->msgq.stride;
1848c2ecf20Sopenharmony_ci	msgq->offset = offset;
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci	FLCNQ_DBG(msgq, "initialised @ index %d offset 0x%08x size 0x%08x",
1878c2ecf20Sopenharmony_ci		  index, msgq->offset, size);
1888c2ecf20Sopenharmony_ci}
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_civoid
1918c2ecf20Sopenharmony_cinvkm_falcon_msgq_del(struct nvkm_falcon_msgq **pmsgq)
1928c2ecf20Sopenharmony_ci{
1938c2ecf20Sopenharmony_ci	struct nvkm_falcon_msgq *msgq = *pmsgq;
1948c2ecf20Sopenharmony_ci	if (msgq) {
1958c2ecf20Sopenharmony_ci		kfree(*pmsgq);
1968c2ecf20Sopenharmony_ci		*pmsgq = NULL;
1978c2ecf20Sopenharmony_ci	}
1988c2ecf20Sopenharmony_ci}
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ciint
2018c2ecf20Sopenharmony_cinvkm_falcon_msgq_new(struct nvkm_falcon_qmgr *qmgr, const char *name,
2028c2ecf20Sopenharmony_ci		     struct nvkm_falcon_msgq **pmsgq)
2038c2ecf20Sopenharmony_ci{
2048c2ecf20Sopenharmony_ci	struct nvkm_falcon_msgq *msgq = *pmsgq;
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci	if (!(msgq = *pmsgq = kzalloc(sizeof(*msgq), GFP_KERNEL)))
2078c2ecf20Sopenharmony_ci		return -ENOMEM;
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci	msgq->qmgr = qmgr;
2108c2ecf20Sopenharmony_ci	msgq->name = name;
2118c2ecf20Sopenharmony_ci	mutex_init(&msgq->mutex);
2128c2ecf20Sopenharmony_ci	return 0;
2138c2ecf20Sopenharmony_ci}
214