1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Support for Intel Camera Imaging ISP subsystem.
4 * Copyright (c) 2010 - 2015, Intel Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13 * more details.
14 */
15
16#include "system_global.h"
17
18
19#include "assert_support.h"
20#include "platform_support.h"
21#include "ia_css_isys.h"
22#include "bitop_support.h"
23#include "isys_dma_rmgr.h"
24
25static isys_dma_rsrc_t isys_dma_rsrc[N_ISYS2401_DMA_ID];
26
27void ia_css_isys_dma_channel_rmgr_init(void)
28{
29	memset(&isys_dma_rsrc, 0, sizeof(isys_dma_rsrc_t));
30}
31
32void ia_css_isys_dma_channel_rmgr_uninit(void)
33{
34	memset(&isys_dma_rsrc, 0, sizeof(isys_dma_rsrc_t));
35}
36
37bool ia_css_isys_dma_channel_rmgr_acquire(
38    isys2401_dma_ID_t	dma_id,
39    isys2401_dma_channel	*channel)
40{
41	bool retval = false;
42	isys2401_dma_channel	i;
43	isys2401_dma_channel	max_dma_channel;
44	isys_dma_rsrc_t		*cur_rsrc = NULL;
45
46	assert(dma_id < N_ISYS2401_DMA_ID);
47	assert(channel);
48
49	max_dma_channel = N_ISYS2401_DMA_CHANNEL_PROCS[dma_id];
50	cur_rsrc = &isys_dma_rsrc[dma_id];
51
52	if (cur_rsrc->num_active < max_dma_channel) {
53		for (i = ISYS2401_DMA_CHANNEL_0; i < N_ISYS2401_DMA_CHANNEL; i++) {
54			if (bitop_getbit(cur_rsrc->active_table, i) == 0) {
55				bitop_setbit(cur_rsrc->active_table, i);
56				*channel = i;
57				cur_rsrc->num_active++;
58				retval = true;
59				break;
60			}
61		}
62	}
63
64	return retval;
65}
66
67void ia_css_isys_dma_channel_rmgr_release(
68    isys2401_dma_ID_t	dma_id,
69    isys2401_dma_channel	*channel)
70{
71	isys2401_dma_channel	max_dma_channel;
72	isys_dma_rsrc_t		*cur_rsrc = NULL;
73
74	assert(dma_id < N_ISYS2401_DMA_ID);
75	assert(channel);
76
77	max_dma_channel = N_ISYS2401_DMA_CHANNEL_PROCS[dma_id];
78	cur_rsrc = &isys_dma_rsrc[dma_id];
79
80	if ((*channel < max_dma_channel) && (cur_rsrc->num_active > 0)) {
81		if (bitop_getbit(cur_rsrc->active_table, *channel) == 1) {
82			bitop_clearbit(cur_rsrc->active_table, *channel);
83			cur_rsrc->num_active--;
84		}
85	}
86}
87