1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Support for Intel Camera Imaging ISP subsystem.
4  * Copyright (c) 2015, Intel Corporation.
5  */
6 
7 #ifndef __IA_CSS_DVS_H
8 #define __IA_CSS_DVS_H
9 
10 /* @file
11  * This file contains types for DVS statistics
12  */
13 
14 #include <linux/build_bug.h>
15 
16 #include <type_support.h>
17 #include "ia_css_types.h"
18 #include "ia_css_err.h"
19 #include "ia_css_stream_public.h"
20 
21 enum dvs_statistics_type {
22 	DVS_STATISTICS,
23 	DVS2_STATISTICS,
24 	SKC_DVS_STATISTICS
25 };
26 
27 /* Structure that holds DVS statistics in the ISP internal
28  * format. Use ia_css_get_dvs_statistics() to translate
29  * this to the format used on the host (DVS engine).
30  * */
31 struct ia_css_isp_dvs_statistics {
32 	ia_css_ptr hor_proj;
33 	ia_css_ptr ver_proj;
34 	u32   hor_size;
35 	u32   ver_size;
36 	u32   exp_id;   /** see ia_css_event_public.h for more detail */
37 	ia_css_ptr data_ptr; /* base pointer containing all memory */
38 	u32   size;     /* size of allocated memory in data_ptr */
39 };
40 
41 /* Structure that holds SKC DVS statistics in the ISP internal
42  * format. Use ia_css_dvs_statistics_get() to translate this to
43  * the format used on the host.
44  * */
45 struct ia_css_isp_skc_dvs_statistics;
46 
47 #define SIZE_OF_IA_CSS_ISP_DVS_STATISTICS_STRUCT			\
48 	((3 * SIZE_OF_IA_CSS_PTR) +					\
49 	 (4 * sizeof(uint32_t)))
50 
51 static_assert(sizeof(struct ia_css_isp_dvs_statistics) == SIZE_OF_IA_CSS_ISP_DVS_STATISTICS_STRUCT);
52 
53 /* Map with host-side pointers to ISP-format statistics.
54  * These pointers can either be copies of ISP data or memory mapped
55  * ISP pointers.
56  * All of the data behind these pointers is allocatd contiguously, the
57  * allocated pointer is stored in the data_ptr field. The other fields
58  * point into this one block of data.
59  */
60 struct ia_css_isp_dvs_statistics_map {
61 	void    *data_ptr;
62 	s32 *hor_proj;
63 	s32 *ver_proj;
64 	u32 size;		 /* total size in bytes */
65 	u32 data_allocated; /* indicate whether data was allocated */
66 };
67 
68 union ia_css_dvs_statistics_isp {
69 	struct ia_css_isp_dvs_statistics *p_dvs_statistics_isp;
70 	struct ia_css_isp_skc_dvs_statistics *p_skc_dvs_statistics_isp;
71 };
72 
73 union ia_css_dvs_statistics_host {
74 	struct ia_css_dvs_statistics *p_dvs_statistics_host;
75 	struct ia_css_dvs2_statistics *p_dvs2_statistics_host;
76 	struct ia_css_skc_dvs_statistics *p_skc_dvs_statistics_host;
77 };
78 
79 /* @brief Copy DVS statistics from an ISP buffer to a host buffer.
80  * @param[in]	host_stats Host buffer
81  * @param[in]	isp_stats ISP buffer
82  * @return	error value if temporary memory cannot be allocated
83  *
84  * This may include a translation step as well depending
85  * on the ISP version.
86  * Always use this function, never copy the buffer directly.
87  * Note that this function uses the mem_load function from the CSS
88  * environment struct.
89  * In certain environments this may be slow. In those cases it is
90  * advised to map the ISP memory into a host-side pointer and use
91  * the ia_css_translate_dvs_statistics() function instead.
92  */
93 int
94 ia_css_get_dvs_statistics(struct ia_css_dvs_statistics *host_stats,
95 			  const struct ia_css_isp_dvs_statistics *isp_stats);
96 
97 /* @brief Translate DVS statistics from ISP format to host format
98  * @param[in]	host_stats Host buffer
99  * @param[in]	isp_stats ISP buffer
100  * @return	None
101  *
102  * This function translates the dvs statistics from the ISP-internal
103  * format to the format used by the DVS library on the CPU.
104  * This function takes a host-side pointer as input. This can either
105  * point to a copy of the data or be a memory mapped pointer to the
106  * ISP memory pages.
107  */
108 void
109 ia_css_translate_dvs_statistics(
110     struct ia_css_dvs_statistics *host_stats,
111     const struct ia_css_isp_dvs_statistics_map *isp_stats);
112 
113 /* @brief Copy DVS 2.0 statistics from an ISP buffer to a host buffer.
114  * @param[in]	host_stats Host buffer
115  * @param[in]	isp_stats ISP buffer
116  * @return	error value if temporary memory cannot be allocated
117  *
118  * This may include a translation step as well depending
119  * on the ISP version.
120  * Always use this function, never copy the buffer directly.
121  * Note that this function uses the mem_load function from the CSS
122  * environment struct.
123  * In certain environments this may be slow. In those cases it is
124  * advised to map the ISP memory into a host-side pointer and use
125  * the ia_css_translate_dvs2_statistics() function instead.
126  */
127 int
128 ia_css_get_dvs2_statistics(struct ia_css_dvs2_statistics *host_stats,
129 			   const struct ia_css_isp_dvs_statistics *isp_stats);
130 
131 /* @brief Translate DVS2 statistics from ISP format to host format
132  * @param[in]	host_stats Host buffer
133  * @param[in]	isp_stats ISP buffer
134  * @return		None
135  *
136  * This function translates the dvs2 statistics from the ISP-internal
137  * format to the format used by the DVS2 library on the CPU.
138  * This function takes a host-side pointer as input. This can either
139  * point to a copy of the data or be a memory mapped pointer to the
140  * ISP memory pages.
141  */
142 void
143 ia_css_translate_dvs2_statistics(
144     struct ia_css_dvs2_statistics	   *host_stats,
145     const struct ia_css_isp_dvs_statistics_map *isp_stats);
146 
147 /* @brief Copy DVS statistics from an ISP buffer to a host buffer.
148  * @param[in] type - DVS statistics type
149  * @param[in] host_stats Host buffer
150  * @param[in] isp_stats ISP buffer
151  * @return None
152  */
153 void
154 ia_css_dvs_statistics_get(enum dvs_statistics_type type,
155 			  union ia_css_dvs_statistics_host  *host_stats,
156 			  const union ia_css_dvs_statistics_isp *isp_stats);
157 
158 /* @brief Allocate the DVS statistics memory on the ISP
159  * @param[in]	grid The grid.
160  * @return	Pointer to the allocated DVS statistics buffer on the ISP
161 */
162 struct ia_css_isp_dvs_statistics *
163 ia_css_isp_dvs_statistics_allocate(const struct ia_css_dvs_grid_info *grid);
164 
165 /* @brief Free the DVS statistics memory on the ISP
166  * @param[in]	me Pointer to the DVS statistics buffer on the ISP.
167  * @return	None
168 */
169 void
170 ia_css_isp_dvs_statistics_free(struct ia_css_isp_dvs_statistics *me);
171 
172 /* @brief Allocate the DVS 2.0 statistics memory
173  * @param[in]	grid The grid.
174  * @return	Pointer to the allocated DVS statistics buffer on the ISP
175 */
176 struct ia_css_isp_dvs_statistics *
177 ia_css_isp_dvs2_statistics_allocate(const struct ia_css_dvs_grid_info *grid);
178 
179 /* @brief Free the DVS 2.0 statistics memory
180  * @param[in]	me Pointer to the DVS statistics buffer on the ISP.
181  * @return	None
182 */
183 void
184 ia_css_isp_dvs2_statistics_free(struct ia_css_isp_dvs_statistics *me);
185 
186 /* @brief Allocate the DVS statistics memory on the host
187  * @param[in]	grid The grid.
188  * @return	Pointer to the allocated DVS statistics buffer on the host
189 */
190 struct ia_css_dvs_statistics *
191 ia_css_dvs_statistics_allocate(const struct ia_css_dvs_grid_info *grid);
192 
193 /* @brief Free the DVS statistics memory on the host
194  * @param[in]	me Pointer to the DVS statistics buffer on the host.
195  * @return	None
196 */
197 void
198 ia_css_dvs_statistics_free(struct ia_css_dvs_statistics *me);
199 
200 /* @brief Allocate the DVS coefficients memory
201  * @param[in]	grid The grid.
202  * @return	Pointer to the allocated DVS coefficients buffer
203 */
204 struct ia_css_dvs_coefficients *
205 ia_css_dvs_coefficients_allocate(const struct ia_css_dvs_grid_info *grid);
206 
207 /* @brief Free the DVS coefficients memory
208  * @param[in]	me Pointer to the DVS coefficients buffer.
209  * @return	None
210  */
211 void
212 ia_css_dvs_coefficients_free(struct ia_css_dvs_coefficients *me);
213 
214 /* @brief Allocate the DVS 2.0 statistics memory on the host
215  * @param[in]	grid The grid.
216  * @return	Pointer to the allocated DVS 2.0 statistics buffer on the host
217  */
218 struct ia_css_dvs2_statistics *
219 ia_css_dvs2_statistics_allocate(const struct ia_css_dvs_grid_info *grid);
220 
221 /* @brief Free the DVS 2.0 statistics memory
222  * @param[in]	me Pointer to the DVS 2.0 statistics buffer on the host.
223  * @return	None
224 */
225 void
226 ia_css_dvs2_statistics_free(struct ia_css_dvs2_statistics *me);
227 
228 /* @brief Allocate the DVS 2.0 coefficients memory
229  * @param[in]	grid The grid.
230  * @return	Pointer to the allocated DVS 2.0 coefficients buffer
231 */
232 struct ia_css_dvs2_coefficients *
233 ia_css_dvs2_coefficients_allocate(const struct ia_css_dvs_grid_info *grid);
234 
235 /* @brief Free the DVS 2.0 coefficients memory
236  * @param[in]	me Pointer to the DVS 2.0 coefficients buffer.
237  * @return	None
238 */
239 void
240 ia_css_dvs2_coefficients_free(struct ia_css_dvs2_coefficients *me);
241 
242 /* @brief Allocate the DVS 2.0 6-axis config memory
243  * @param[in]	stream The stream.
244  * @return	Pointer to the allocated DVS 6axis configuration buffer
245 */
246 struct ia_css_dvs_6axis_config *
247 ia_css_dvs2_6axis_config_allocate(const struct ia_css_stream *stream);
248 
249 /* @brief Free the DVS 2.0 6-axis config memory
250  * @param[in]	dvs_6axis_config Pointer to the DVS 6axis configuration buffer
251  * @return	None
252  */
253 void
254 ia_css_dvs2_6axis_config_free(struct ia_css_dvs_6axis_config *dvs_6axis_config);
255 
256 /* @brief Allocate a dvs statistics map structure
257  * @param[in]	isp_stats pointer to ISP dvs statistis struct
258  * @param[in]	data_ptr  host-side pointer to ISP dvs statistics.
259  * @return	Pointer to the allocated dvs statistics map
260  *
261  * This function allocates the ISP dvs statistics map structure
262  * and uses the data_ptr as base pointer to set the appropriate
263  * pointers to all relevant subsets of the dvs statistics (dmem,
264  * vmem, hmem).
265  * If the data_ptr is NULL, this function will allocate the host-side
266  * memory. This information is stored in the struct and used in the
267  * ia_css_isp_dvs_statistics_map_free() function to determine whether
268  * the memory should be freed or not.
269  * Note that this function does not allocate or map any ISP
270  * memory.
271 */
272 struct ia_css_isp_dvs_statistics_map *
273 ia_css_isp_dvs_statistics_map_allocate(
274     const struct ia_css_isp_dvs_statistics *isp_stats,
275     void *data_ptr);
276 
277 /* @brief Free the dvs statistics map
278  * @param[in]	me Pointer to the dvs statistics map
279  * @return	None
280  *
281  * This function frees the map struct. If the data_ptr inside it
282  * was allocated inside ia_css_isp_dvs_statistics_map_allocate(), it
283  * will be freed in this function. Otherwise it will not be freed.
284  */
285 void
286 ia_css_isp_dvs_statistics_map_free(struct ia_css_isp_dvs_statistics_map *me);
287 
288 /* @brief Allocate memory for the SKC DVS statistics on the ISP
289  * @return		Pointer to the allocated ACC DVS statistics buffer on the ISP
290 */
291 struct ia_css_isp_skc_dvs_statistics *ia_css_skc_dvs_statistics_allocate(void);
292 
293 #endif /*  __IA_CSS_DVS_H */
294