Linux-libre 4.14.69-gnu
[librecmc/linux-libre.git] / drivers / staging / media / atomisp / pci / atomisp2 / css2400 / runtime / isys / src / ibuf_ctrl_rmgr.c
1 #ifndef ISP2401
2 /*
3  * Support for Intel Camera Imaging ISP subsystem.
4  * Copyright (c) 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 #else
16 /**
17  * Support for Intel Camera Imaging ISP subsystem.
18  * Copyright (c) 2010 - 2015, Intel Corporation.
19  *
20  * This program is free software; you can redistribute it and/or modify it
21  * under the terms and conditions of the GNU General Public License,
22  * version 2, as published by the Free Software Foundation.
23  *
24  * This program is distributed in the hope it will be useful, but WITHOUT
25  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
26  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
27  * more details.
28  */
29 #endif
30
31 #include "system_global.h"
32
33 #ifdef USE_INPUT_SYSTEM_VERSION_2401
34
35 #include "assert_support.h"
36 #include "platform_support.h"
37 #include "ia_css_isys.h"
38 #include "ibuf_ctrl_rmgr.h"
39
40 static ibuf_rsrc_t      ibuf_rsrc;
41
42 static ibuf_handle_t *getHandle(uint16_t index)
43 {
44         ibuf_handle_t *handle = NULL;
45
46         if (index < MAX_IBUF_HANDLES)
47                 handle = &ibuf_rsrc.handles[index];
48         return handle;
49 }
50
51 void ia_css_isys_ibuf_rmgr_init(void)
52 {
53         memset(&ibuf_rsrc, 0, sizeof(ibuf_rsrc));
54         ibuf_rsrc.free_size = MAX_INPUT_BUFFER_SIZE;
55 }
56
57 void ia_css_isys_ibuf_rmgr_uninit(void)
58 {
59         memset(&ibuf_rsrc, 0, sizeof(ibuf_rsrc));
60         ibuf_rsrc.free_size = MAX_INPUT_BUFFER_SIZE;
61 }
62
63 bool ia_css_isys_ibuf_rmgr_acquire(
64         uint32_t        size,
65         uint32_t        *start_addr)
66 {
67         bool retval = false;
68         bool input_buffer_found = false;
69         uint32_t aligned_size;
70         ibuf_handle_t *handle = NULL;
71         uint16_t i;
72
73         assert(start_addr != NULL);
74         assert(size > 0);
75
76         aligned_size = (size + (IBUF_ALIGN - 1)) & ~(IBUF_ALIGN - 1);
77
78         /* Check if there is an available un-used handle with the size
79          * that will fulfill the request.
80          */
81         if (ibuf_rsrc.num_active < ibuf_rsrc.num_allocated) {
82                 for (i = 0; i < ibuf_rsrc.num_allocated; i++) {
83                         handle = getHandle(i);
84                         if (!handle->active) {
85                                 if (handle->size >= aligned_size) {
86                                         handle->active = true;
87                                         input_buffer_found = true;
88                                         ibuf_rsrc.num_active++;
89                                         break;
90                                 }
91                         }
92                 }
93         }
94
95         if (!input_buffer_found) {
96                 /* There were no available handles that fulfilled the
97                  * request. Allocate a new handle with the requested size.
98                  */
99                 if ((ibuf_rsrc.num_allocated < MAX_IBUF_HANDLES) &&
100                     (ibuf_rsrc.free_size >= aligned_size)) {
101                         handle = getHandle(ibuf_rsrc.num_allocated);
102                         handle->start_addr      = ibuf_rsrc.free_start_addr;
103                         handle->size            = aligned_size;
104                         handle->active          = true;
105
106                         ibuf_rsrc.free_start_addr += aligned_size;
107                         ibuf_rsrc.free_size -= aligned_size;
108                         ibuf_rsrc.num_active++;
109                         ibuf_rsrc.num_allocated++;
110
111                         input_buffer_found = true;
112                 }
113         }
114
115         if (input_buffer_found && handle) {
116                 *start_addr = handle->start_addr;
117                 retval = true;
118         }
119
120         return retval;
121 }
122
123 void ia_css_isys_ibuf_rmgr_release(
124         uint32_t        *start_addr)
125 {
126         uint16_t i;
127         ibuf_handle_t *handle = NULL;
128
129         assert(start_addr != NULL);
130
131         for (i = 0; i < ibuf_rsrc.num_allocated; i++) {
132                 handle = getHandle(i);
133                 if (handle->active && handle->start_addr == *start_addr) {
134                         handle->active = false;
135                         ibuf_rsrc.num_active--;
136                         break;
137                 }
138         }
139 }
140 #endif