1 /* SPDX-License-Identifier: GPL-2.0 */
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 
16 #ifndef __IA_CSS_3A_H
17 #define __IA_CSS_3A_H
18 
19 /* @file
20  * This file contains types used for 3A statistics
21  */
22 
23 #include <type_support.h>
24 #include "ia_css_types.h"
25 #include "ia_css_err.h"
26 #include "system_global.h"
27 
28 enum ia_css_3a_tables {
29 	IA_CSS_S3A_TBL_HI,
30 	IA_CSS_S3A_TBL_LO,
31 	IA_CSS_RGBY_TBL,
32 	IA_CSS_NUM_3A_TABLES
33 };
34 
35 /* Structure that holds 3A statistics in the ISP internal
36  * format. Use ia_css_get_3a_statistics() to translate
37  * this to the format used on the host (3A library).
38  * */
39 struct ia_css_isp_3a_statistics {
40 	union {
41 		struct {
42 			ia_css_ptr s3a_tbl;
43 		} dmem;
44 		struct {
45 			ia_css_ptr s3a_tbl_hi;
46 			ia_css_ptr s3a_tbl_lo;
47 		} vmem;
48 	} data;
49 	struct {
50 		ia_css_ptr rgby_tbl;
51 	} data_hmem;
52 	u32 exp_id;     /** exposure id, to match statistics to a frame,
53 				  see ia_css_event_public.h for more detail. */
54 	u32 isp_config_id;/** Unique ID to track which config was actually applied to a particular frame */
55 	ia_css_ptr data_ptr; /** pointer to base of all data */
56 	u32   size;     /** total size of all data */
57 	u32   dmem_size;
58 	u32   vmem_size; /** both lo and hi have this size */
59 	u32   hmem_size;
60 };
61 
62 #define SIZE_OF_DMEM_STRUCT						\
63 	(SIZE_OF_IA_CSS_PTR)
64 
65 #define SIZE_OF_VMEM_STRUCT						\
66 	(2 * SIZE_OF_IA_CSS_PTR)
67 
68 #define SIZE_OF_DATA_UNION						\
69 	(MAX(SIZE_OF_DMEM_STRUCT, SIZE_OF_VMEM_STRUCT))
70 
71 #define SIZE_OF_DATA_HMEM_STRUCT					\
72 	(SIZE_OF_IA_CSS_PTR)
73 
74 #define SIZE_OF_IA_CSS_ISP_3A_STATISTICS_STRUCT				\
75 	(SIZE_OF_DATA_UNION +						\
76 	 SIZE_OF_DATA_HMEM_STRUCT +					\
77 	 sizeof(uint32_t) +						\
78 	 sizeof(uint32_t) +						\
79 	 SIZE_OF_IA_CSS_PTR +						\
80 	 4 * sizeof(uint32_t))
81 
82 /* Map with host-side pointers to ISP-format statistics.
83  * These pointers can either be copies of ISP data or memory mapped
84  * ISP pointers.
85  * All of the data behind these pointers is allocated contiguously, the
86  * allocated pointer is stored in the data_ptr field. The other fields
87  * point into this one block of data.
88  */
89 struct ia_css_isp_3a_statistics_map {
90 	void                    *data_ptr; /** Pointer to start of memory */
91 	struct ia_css_3a_output *dmem_stats;
92 	u16                *vmem_stats_hi;
93 	u16                *vmem_stats_lo;
94 	struct ia_css_bh_table  *hmem_stats;
95 	u32                 size; /** total size in bytes of data_ptr */
96 	u32                 data_allocated; /** indicate whether data_ptr
97 						    was allocated or not. */
98 };
99 
100 /* @brief Copy and translate 3A statistics from an ISP buffer to a host buffer
101  * @param[out]	host_stats Host buffer.
102  * @param[in]	isp_stats ISP buffer.
103  * @return	error value if temporary memory cannot be allocated
104  *
105  * This copies 3a statistics from an ISP pointer to a host pointer and then
106  * translates some of the statistics, details depend on which ISP binary is
107  * used.
108  * Always use this function, never copy the buffer directly.
109  */
110 int
111 ia_css_get_3a_statistics(struct ia_css_3a_statistics           *host_stats,
112 			 const struct ia_css_isp_3a_statistics *isp_stats);
113 
114 /* @brief Translate 3A statistics from ISP format to host format.
115  * @param[out]	host_stats host-format statistics
116  * @param[in]	isp_stats  ISP-format statistics
117  * @return	None
118  *
119  * This function translates statistics from the internal ISP-format to
120  * the host-format. This function does not include an additional copy
121  * step.
122  * */
123 void
124 ia_css_translate_3a_statistics(
125     struct ia_css_3a_statistics               *host_stats,
126     const struct ia_css_isp_3a_statistics_map *isp_stats);
127 
128 /* Convenience functions for alloc/free of certain datatypes */
129 
130 /* @brief Allocate memory for the 3a statistics on the ISP
131  * @param[in]	grid The grid.
132  * @return		Pointer to the allocated 3a statistics buffer on the ISP
133 */
134 struct ia_css_isp_3a_statistics *
135 ia_css_isp_3a_statistics_allocate(const struct ia_css_3a_grid_info *grid);
136 
137 /* @brief Free the 3a statistics memory on the isp
138  * @param[in]	me Pointer to the 3a statistics buffer on the ISP.
139  * @return		None
140 */
141 void
142 ia_css_isp_3a_statistics_free(struct ia_css_isp_3a_statistics *me);
143 
144 /* @brief Allocate memory for the 3a statistics on the host
145  * @param[in]	grid The grid.
146  * @return		Pointer to the allocated 3a statistics buffer on the host
147 */
148 struct ia_css_3a_statistics *
149 ia_css_3a_statistics_allocate(const struct ia_css_3a_grid_info *grid);
150 
151 /* @brief Free the 3a statistics memory on the host
152  * @param[in]	me Pointer to the 3a statistics buffer on the host.
153  * @return		None
154  */
155 void
156 ia_css_3a_statistics_free(struct ia_css_3a_statistics *me);
157 
158 /* @brief Allocate a 3a statistics map structure
159  * @param[in]	isp_stats pointer to ISP 3a statistis struct
160  * @param[in]	data_ptr  host-side pointer to ISP 3a statistics.
161  * @return		Pointer to the allocated 3a statistics map
162  *
163  * This function allocates the ISP 3a statistics map structure
164  * and uses the data_ptr as base pointer to set the appropriate
165  * pointers to all relevant subsets of the 3a statistics (dmem,
166  * vmem, hmem).
167  * If the data_ptr is NULL, this function will allocate the host-side
168  * memory. This information is stored in the struct and used in the
169  * ia_css_isp_3a_statistics_map_free() function to determine whether
170  * the memory should be freed or not.
171  * Note that this function does not allocate or map any ISP
172  * memory.
173 */
174 struct ia_css_isp_3a_statistics_map *
175 ia_css_isp_3a_statistics_map_allocate(
176     const struct ia_css_isp_3a_statistics *isp_stats,
177     void *data_ptr);
178 
179 /* @brief Free the 3a statistics map
180  * @param[in]	me Pointer to the 3a statistics map
181  * @return		None
182  *
183  * This function frees the map struct. If the data_ptr inside it
184  * was allocated inside ia_css_isp_3a_statistics_map_allocate(), it
185  * will be freed in this function. Otherwise it will not be freed.
186  */
187 void
188 ia_css_isp_3a_statistics_map_free(struct ia_css_isp_3a_statistics_map *me);
189 
190 #endif /* __IA_CSS_3A_H */
191