162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved.
362306a36Sopenharmony_ci *
462306a36Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
562306a36Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
662306a36Sopenharmony_ci * to deal in the Software without restriction, including without limitation
762306a36Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
862306a36Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
962306a36Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
1062306a36Sopenharmony_ci *
1162306a36Sopenharmony_ci * The above copyright notice and this permission notice shall be included in
1262306a36Sopenharmony_ci * all copies or substantial portions of the Software.
1362306a36Sopenharmony_ci *
1462306a36Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1562306a36Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1662306a36Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
1762306a36Sopenharmony_ci * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
1862306a36Sopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
1962306a36Sopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
2062306a36Sopenharmony_ci * OTHER DEALINGS IN THE SOFTWARE.
2162306a36Sopenharmony_ci *
2262306a36Sopenharmony_ci */
2362306a36Sopenharmony_ci#include "qmgr.h"
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_cistruct nvkm_falcon_qmgr_seq *
2662306a36Sopenharmony_cinvkm_falcon_qmgr_seq_acquire(struct nvkm_falcon_qmgr *qmgr)
2762306a36Sopenharmony_ci{
2862306a36Sopenharmony_ci	const struct nvkm_subdev *subdev = qmgr->falcon->owner;
2962306a36Sopenharmony_ci	struct nvkm_falcon_qmgr_seq *seq;
3062306a36Sopenharmony_ci	u32 index;
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_ci	mutex_lock(&qmgr->seq.mutex);
3362306a36Sopenharmony_ci	index = find_first_zero_bit(qmgr->seq.tbl, NVKM_FALCON_QMGR_SEQ_NUM);
3462306a36Sopenharmony_ci	if (index >= NVKM_FALCON_QMGR_SEQ_NUM) {
3562306a36Sopenharmony_ci		nvkm_error(subdev, "no free sequence available\n");
3662306a36Sopenharmony_ci		mutex_unlock(&qmgr->seq.mutex);
3762306a36Sopenharmony_ci		return ERR_PTR(-EAGAIN);
3862306a36Sopenharmony_ci	}
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_ci	set_bit(index, qmgr->seq.tbl);
4162306a36Sopenharmony_ci	mutex_unlock(&qmgr->seq.mutex);
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_ci	seq = &qmgr->seq.id[index];
4462306a36Sopenharmony_ci	seq->state = SEQ_STATE_PENDING;
4562306a36Sopenharmony_ci	return seq;
4662306a36Sopenharmony_ci}
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_civoid
4962306a36Sopenharmony_cinvkm_falcon_qmgr_seq_release(struct nvkm_falcon_qmgr *qmgr,
5062306a36Sopenharmony_ci			     struct nvkm_falcon_qmgr_seq *seq)
5162306a36Sopenharmony_ci{
5262306a36Sopenharmony_ci	/* no need to acquire seq.mutex since clear_bit is atomic */
5362306a36Sopenharmony_ci	seq->state = SEQ_STATE_FREE;
5462306a36Sopenharmony_ci	seq->callback = NULL;
5562306a36Sopenharmony_ci	reinit_completion(&seq->done);
5662306a36Sopenharmony_ci	clear_bit(seq->id, qmgr->seq.tbl);
5762306a36Sopenharmony_ci}
5862306a36Sopenharmony_ci
5962306a36Sopenharmony_civoid
6062306a36Sopenharmony_cinvkm_falcon_qmgr_del(struct nvkm_falcon_qmgr **pqmgr)
6162306a36Sopenharmony_ci{
6262306a36Sopenharmony_ci	struct nvkm_falcon_qmgr *qmgr = *pqmgr;
6362306a36Sopenharmony_ci	if (qmgr) {
6462306a36Sopenharmony_ci		kfree(*pqmgr);
6562306a36Sopenharmony_ci		*pqmgr = NULL;
6662306a36Sopenharmony_ci	}
6762306a36Sopenharmony_ci}
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_ciint
7062306a36Sopenharmony_cinvkm_falcon_qmgr_new(struct nvkm_falcon *falcon,
7162306a36Sopenharmony_ci		     struct nvkm_falcon_qmgr **pqmgr)
7262306a36Sopenharmony_ci{
7362306a36Sopenharmony_ci	struct nvkm_falcon_qmgr *qmgr;
7462306a36Sopenharmony_ci	int i;
7562306a36Sopenharmony_ci
7662306a36Sopenharmony_ci	if (!(qmgr = *pqmgr = kzalloc(sizeof(*qmgr), GFP_KERNEL)))
7762306a36Sopenharmony_ci		return -ENOMEM;
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_ci	qmgr->falcon = falcon;
8062306a36Sopenharmony_ci	mutex_init(&qmgr->seq.mutex);
8162306a36Sopenharmony_ci	for (i = 0; i < NVKM_FALCON_QMGR_SEQ_NUM; i++) {
8262306a36Sopenharmony_ci		qmgr->seq.id[i].id = i;
8362306a36Sopenharmony_ci		init_completion(&qmgr->seq.id[i].done);
8462306a36Sopenharmony_ci	}
8562306a36Sopenharmony_ci
8662306a36Sopenharmony_ci	return 0;
8762306a36Sopenharmony_ci}
88