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