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_cistruct nvkm_falcon_qmgr_seq *
268c2ecf20Sopenharmony_cinvkm_falcon_qmgr_seq_acquire(struct nvkm_falcon_qmgr *qmgr)
278c2ecf20Sopenharmony_ci{
288c2ecf20Sopenharmony_ci	const struct nvkm_subdev *subdev = qmgr->falcon->owner;
298c2ecf20Sopenharmony_ci	struct nvkm_falcon_qmgr_seq *seq;
308c2ecf20Sopenharmony_ci	u32 index;
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci	mutex_lock(&qmgr->seq.mutex);
338c2ecf20Sopenharmony_ci	index = find_first_zero_bit(qmgr->seq.tbl, NVKM_FALCON_QMGR_SEQ_NUM);
348c2ecf20Sopenharmony_ci	if (index >= NVKM_FALCON_QMGR_SEQ_NUM) {
358c2ecf20Sopenharmony_ci		nvkm_error(subdev, "no free sequence available\n");
368c2ecf20Sopenharmony_ci		mutex_unlock(&qmgr->seq.mutex);
378c2ecf20Sopenharmony_ci		return ERR_PTR(-EAGAIN);
388c2ecf20Sopenharmony_ci	}
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci	set_bit(index, qmgr->seq.tbl);
418c2ecf20Sopenharmony_ci	mutex_unlock(&qmgr->seq.mutex);
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci	seq = &qmgr->seq.id[index];
448c2ecf20Sopenharmony_ci	seq->state = SEQ_STATE_PENDING;
458c2ecf20Sopenharmony_ci	return seq;
468c2ecf20Sopenharmony_ci}
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_civoid
498c2ecf20Sopenharmony_cinvkm_falcon_qmgr_seq_release(struct nvkm_falcon_qmgr *qmgr,
508c2ecf20Sopenharmony_ci			     struct nvkm_falcon_qmgr_seq *seq)
518c2ecf20Sopenharmony_ci{
528c2ecf20Sopenharmony_ci	/* no need to acquire seq.mutex since clear_bit is atomic */
538c2ecf20Sopenharmony_ci	seq->state = SEQ_STATE_FREE;
548c2ecf20Sopenharmony_ci	seq->callback = NULL;
558c2ecf20Sopenharmony_ci	reinit_completion(&seq->done);
568c2ecf20Sopenharmony_ci	clear_bit(seq->id, qmgr->seq.tbl);
578c2ecf20Sopenharmony_ci}
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_civoid
608c2ecf20Sopenharmony_cinvkm_falcon_qmgr_del(struct nvkm_falcon_qmgr **pqmgr)
618c2ecf20Sopenharmony_ci{
628c2ecf20Sopenharmony_ci	struct nvkm_falcon_qmgr *qmgr = *pqmgr;
638c2ecf20Sopenharmony_ci	if (qmgr) {
648c2ecf20Sopenharmony_ci		kfree(*pqmgr);
658c2ecf20Sopenharmony_ci		*pqmgr = NULL;
668c2ecf20Sopenharmony_ci	}
678c2ecf20Sopenharmony_ci}
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ciint
708c2ecf20Sopenharmony_cinvkm_falcon_qmgr_new(struct nvkm_falcon *falcon,
718c2ecf20Sopenharmony_ci		     struct nvkm_falcon_qmgr **pqmgr)
728c2ecf20Sopenharmony_ci{
738c2ecf20Sopenharmony_ci	struct nvkm_falcon_qmgr *qmgr;
748c2ecf20Sopenharmony_ci	int i;
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci	if (!(qmgr = *pqmgr = kzalloc(sizeof(*qmgr), GFP_KERNEL)))
778c2ecf20Sopenharmony_ci		return -ENOMEM;
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci	qmgr->falcon = falcon;
808c2ecf20Sopenharmony_ci	mutex_init(&qmgr->seq.mutex);
818c2ecf20Sopenharmony_ci	for (i = 0; i < NVKM_FALCON_QMGR_SEQ_NUM; i++) {
828c2ecf20Sopenharmony_ci		qmgr->seq.id[i].id = i;
838c2ecf20Sopenharmony_ci		init_completion(&qmgr->seq.id[i].done);
848c2ecf20Sopenharmony_ci	}
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci	return 0;
878c2ecf20Sopenharmony_ci}
88