xref: /linux/drivers/staging/media/atomisp/pci/runtime/isys/src/isys_stream2mmio_rmgr.c (revision 42874e4eb35bdfc54f8514685e50434098ba4f6c)
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_stream2mmio_rmgr.h"
24 
25 static isys_stream2mmio_rsrc_t	isys_stream2mmio_rsrc[N_STREAM2MMIO_ID];
26 
27 void ia_css_isys_stream2mmio_sid_rmgr_init(void)
28 {
29 	memset(isys_stream2mmio_rsrc, 0, sizeof(isys_stream2mmio_rsrc));
30 }
31 
32 void ia_css_isys_stream2mmio_sid_rmgr_uninit(void)
33 {
34 	memset(isys_stream2mmio_rsrc, 0, sizeof(isys_stream2mmio_rsrc));
35 }
36 
37 bool ia_css_isys_stream2mmio_sid_rmgr_acquire(
38     stream2mmio_ID_t	stream2mmio,
39     stream2mmio_sid_ID_t	*sid)
40 {
41 	bool retval = false;
42 	stream2mmio_sid_ID_t max_sid;
43 	isys_stream2mmio_rsrc_t *cur_rsrc = NULL;
44 	stream2mmio_sid_ID_t	i;
45 
46 	assert(stream2mmio < N_STREAM2MMIO_ID);
47 	assert(sid);
48 
49 	if ((stream2mmio < N_STREAM2MMIO_ID) && (sid)) {
50 		max_sid = N_STREAM2MMIO_SID_PROCS[stream2mmio];
51 		cur_rsrc = &isys_stream2mmio_rsrc[stream2mmio];
52 
53 		if (cur_rsrc->num_active < max_sid) {
54 			for (i = STREAM2MMIO_SID0_ID; i < max_sid; i++) {
55 				if (bitop_getbit(cur_rsrc->active_table, i) == 0) {
56 					bitop_setbit(cur_rsrc->active_table, i);
57 					*sid = i;
58 					cur_rsrc->num_active++;
59 					retval = true;
60 					break;
61 				}
62 			}
63 		}
64 	}
65 	return retval;
66 }
67 
68 void ia_css_isys_stream2mmio_sid_rmgr_release(
69     stream2mmio_ID_t	stream2mmio,
70     stream2mmio_sid_ID_t	*sid)
71 {
72 	stream2mmio_sid_ID_t max_sid;
73 	isys_stream2mmio_rsrc_t *cur_rsrc = NULL;
74 
75 	assert(stream2mmio < N_STREAM2MMIO_ID);
76 	assert(sid);
77 
78 	if ((stream2mmio < N_STREAM2MMIO_ID) && (sid)) {
79 		max_sid = N_STREAM2MMIO_SID_PROCS[stream2mmio];
80 		cur_rsrc = &isys_stream2mmio_rsrc[stream2mmio];
81 		if ((*sid < max_sid) && (cur_rsrc->num_active > 0)) {
82 			if (bitop_getbit(cur_rsrc->active_table, *sid) == 1) {
83 				bitop_clearbit(cur_rsrc->active_table, *sid);
84 				cur_rsrc->num_active--;
85 			}
86 		}
87 	}
88 }
89