xref: /dpdk/drivers/net/bnxt/tf_core/tf_sram_mgr.h (revision 97435d7906d7706e39e5c3dfefa5e09d7de7f733)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2019-2023 Broadcom
3  * All rights reserved.
4  */
5 
6 #ifndef _TF_SRAM_MGR_H_
7 #define _TF_SRAM_MGR_H_
8 
9 #include <string.h>
10 #include <stdint.h>
11 #include <stdio.h>
12 #include <unistd.h>
13 #include <stdlib.h>
14 #include <ctype.h>
15 #include <limits.h>
16 #include <errno.h>
17 #include "tf_core.h"
18 #include "tf_rm.h"
19 
20 /* When special access registers are used to access the SRAM, stats can be
21  * automatically cleared on read by the hardware.  This requires additional
22  * support to be added in the firmware to use these registers for statistics.
23  * The support entails using the special access registers to read the stats.
24  * These are stored in bank 3 currently but may move depending upon the
25  * policy defined in tf_device_p58.h
26  */
27 #define STATS_CLEAR_ON_READ_SUPPORT 0
28 
29 #define TF_SRAM_MGR_BLOCK_SZ_BYTES 128
30 #define TF_SRAM_MGR_MIN_SLICE_BYTES 8
31 
32 /**
33  * TF slice size.
34  *
35  * A slice is part of a 128B row
36  *
37  * Each slice is a multiple of 8B
38  */
39 enum tf_sram_slice_size {
40 	TF_SRAM_SLICE_SIZE_8B,		/**< 8 byte SRAM slice */
41 	TF_SRAM_SLICE_SIZE_16B,		/**< 16 byte SRAM slice */
42 	TF_SRAM_SLICE_SIZE_32B,		/**< 32 byte SRAM slice */
43 	TF_SRAM_SLICE_SIZE_64B,		/**< 64 byte SRAM slice */
44 	TF_SRAM_SLICE_SIZE_128B,	/**< 128 byte SRAM slice */
45 	TF_SRAM_SLICE_SIZE_MAX		/**< slice limit */
46 };
47 
48 /** Initialize the SRAM slice manager
49  *
50  *  The SRAM slice manager manages slices within 128B rows. Slices are of size
51  *  tf_sram_slice_size.  This function provides a handle to the SRAM manager
52  *  data.
53  *
54  *  SRAM manager data may dynamically allocate data upon initialization if
55  *  running on the host.
56  *
57  * [in/out] sram_handle
58  *   Pointer to SRAM handle
59  *
60  * Returns
61  *   - (0) if successful
62  *   - (-EINVAL) on failure
63  *
64  * Returns the handle for the SRAM slice manager
65  */
66 int tf_sram_mgr_bind(void **sram_handle);
67 
68 /** Uninitialize the SRAM slice manager
69  *
70  * Frees any dynamically allocated data structures for SRAM slice management.
71  *
72  * [in] sram_handle
73  *   Pointer to SRAM handle
74  *
75  * Returns
76  *   - (0) if successful
77  *   - (-EINVAL) on failure
78  */
79 int tf_sram_mgr_unbind(void *sram_handle);
80 
81 /**
82  * tf_sram_mgr_alloc_parms parameter definition
83  */
84 struct tf_sram_mgr_alloc_parms {
85 	/**
86 	 * [in] dir
87 	 */
88 	enum tf_dir dir;
89 	/**
90 	 * [in] bank
91 	 *
92 	 *  the SRAM bank to allocate from
93 	 */
94 	enum tf_sram_bank_id bank_id;
95 	/**
96 	 * [in] slice_size
97 	 *
98 	 *  the slice size to allocate
99 	 */
100 	enum tf_sram_slice_size slice_size;
101 	/**
102 	 * [in/out] sram_slice
103 	 *
104 	 *  A pointer to be filled with an 8B sram slice offset
105 	 */
106 	uint16_t *sram_offset;
107 	/**
108 	 * [in] RM DB Handle required for RM allocation
109 	 */
110 	void *rm_db;
111 	/**
112 	 * [in] tf table type
113 	 */
114 	enum tf_tbl_type tbl_type;
115 };
116 
117 /**
118  * Allocate an SRAM Slice
119  *
120  * Allocate an SRAM slice from the indicated bank.  If successful an 8B SRAM
121  * offset will be returned.  Slices are variable sized.  This may result in
122  * a row being allocated from the RM SRAM bank pool if required.
123  *
124  * [in] sram_handle
125  *   Pointer to SRAM handle
126  *
127  * [in] parms
128  *   Pointer to the SRAM alloc parameters
129  *
130  * Returns
131  *   - (0) if successful
132  *   - (-EINVAL) on failure
133  *
134  */
135 int tf_sram_mgr_alloc(void *sram_handle,
136 		      struct tf_sram_mgr_alloc_parms *parms);
137 /**
138  * tf_sram_mgr_free_parms parameter definition
139  */
140 struct tf_sram_mgr_free_parms {
141 	/**
142 	 * [in] dir
143 	 */
144 	enum tf_dir dir;
145 	/**
146 	 * [in] bank
147 	 *
148 	 *  the SRAM bank to free to
149 	 */
150 	enum tf_sram_bank_id bank_id;
151 	/**
152 	 * [in] slice_size
153 	 *
154 	 *  the slice size to be returned
155 	 */
156 	enum tf_sram_slice_size slice_size;
157 	/**
158 	 * [in] sram_offset
159 	 *
160 	 *  the SRAM slice offset (8B) to be returned
161 	 */
162 	uint16_t sram_offset;
163 	/**
164 	 * [in] RM DB Handle required for RM free
165 	 */
166 	void *rm_db;
167 	/**
168 	 * [in] tf table type
169 	 */
170 	enum tf_tbl_type tbl_type;
171 #if (STATS_CLEAR_ON_READ_SUPPORT == 0)
172 	/**
173 	 * [in] tfp
174 	 *
175 	 * A pointer to the tf handle
176 	 */
177 	void *tfp;
178 #endif
179 };
180 
181 /**
182  * Free an SRAM Slice
183  *
184  * Free an SRAM slice to the indicated bank.  This may result in a 128B row
185  * being returned to the RM SRAM bank pool.
186  *
187  * [in] sram_handle
188  *   Pointer to SRAM handle
189  *
190  * [in] parms
191  *   Pointer to the SRAM free parameters
192  *
193  * Returns
194  *   - (0) if successful
195  *   - (-EINVAL) on failure
196  *
197  */
198 int tf_sram_mgr_free(void *sram_handle,
199 		     struct tf_sram_mgr_free_parms *parms);
200 
201 /**
202  * tf_sram_mgr_dump_parms parameter definition
203  */
204 struct tf_sram_mgr_dump_parms {
205 	/**
206 	 * [in] dir
207 	 */
208 	enum tf_dir dir;
209 	/**
210 	 * [in] bank
211 	 *
212 	 *  the SRAM bank to dump
213 	 */
214 	enum tf_sram_bank_id bank_id;
215 	/**
216 	 * [in] slice_size
217 	 *
218 	 *  the slice size list to be dumped
219 	 */
220 	enum tf_sram_slice_size slice_size;
221 };
222 
223 /**
224  * Dump a slice list
225  *
226  * Dump the slice list given the SRAM bank and the slice size
227  *
228  * [in] sram_handle
229  *   Pointer to SRAM handle
230  *
231  * [in] parms
232  *   Pointer to the SRAM free parameters
233  *
234  * Returns
235  *   - (0) if successful
236  *   - (-EINVAL) on failure
237  *
238  */
239 int tf_sram_mgr_dump(void *sram_handle,
240 		     struct tf_sram_mgr_dump_parms *parms);
241 
242 /**
243  * tf_sram_mgr_is_allocated_parms parameter definition
244  */
245 struct tf_sram_mgr_is_allocated_parms {
246 	/**
247 	 * [in] dir
248 	 */
249 	enum tf_dir dir;
250 	/**
251 	 * [in] bank
252 	 *
253 	 *  the SRAM bank to allocate from
254 	 */
255 	enum tf_sram_bank_id bank_id;
256 	/**
257 	 * [in] slice_size
258 	 *
259 	 *  the slice size which was allocated
260 	 */
261 	enum tf_sram_slice_size slice_size;
262 	/**
263 	 * [in] sram_offset
264 	 *
265 	 *  The sram slice offset to validate
266 	 */
267 	uint16_t sram_offset;
268 	/**
269 	 * [in/out] is_allocated
270 	 *
271 	 *  Pointer passed in to be filled with indication of allocation
272 	 */
273 	bool *is_allocated;
274 };
275 
276 /**
277  * Validate an SRAM Slice is allocated
278  *
279  * Validate whether the SRAM slice is allocated
280  *
281  * [in] sram_handle
282  *   Pointer to SRAM handle
283  *
284  * [in] parms
285  *   Pointer to the SRAM alloc parameters
286  *
287  * Returns
288  *   - (0) if successful
289  *   - (-EINVAL) on failure
290  *
291  */
292 int tf_sram_mgr_is_allocated(void *sram_handle,
293 			     struct tf_sram_mgr_is_allocated_parms *parms);
294 
295 /**
296  * Given the slice size, return a char string
297  */
298 const char
299 *tf_sram_slice_2_str(enum tf_sram_slice_size slice_size);
300 
301 /**
302  * Given the bank_id, return a char string
303  */
304 const char
305 *tf_sram_bank_2_str(enum tf_sram_bank_id bank_id);
306 #endif /* _TF_SRAM_MGR_H_ */
307