xref: /OK3568_Linux_fs/external/rkwifibt/drivers/infineon/include/bcm_mpool_pub.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Memory pools library, Public interface
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * API Overview
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * This package provides a memory allocation subsystem based on pools of
7*4882a593Smuzhiyun  * homogenous objects.
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * Instrumentation is available for reporting memory utilization both
10*4882a593Smuzhiyun  * on a per-data-structure basis and system wide.
11*4882a593Smuzhiyun  *
12*4882a593Smuzhiyun  * There are two main types defined in this API.
13*4882a593Smuzhiyun  *
14*4882a593Smuzhiyun  *    pool manager: A singleton object that acts as a factory for
15*4882a593Smuzhiyun  *                  pool allocators. It also is used for global
16*4882a593Smuzhiyun  *                  instrumentation, such as reporting all blocks
17*4882a593Smuzhiyun  *                  in use across all data structures. The pool manager
18*4882a593Smuzhiyun  *                  creates and provides individual memory pools
19*4882a593Smuzhiyun  *                  upon request to application code.
20*4882a593Smuzhiyun  *
21*4882a593Smuzhiyun  *    memory pool:  An object for allocating homogenous memory blocks.
22*4882a593Smuzhiyun  *
23*4882a593Smuzhiyun  * Global identifiers in this module use the following prefixes:
24*4882a593Smuzhiyun  *    bcm_mpm_*     Memory pool manager
25*4882a593Smuzhiyun  *    bcm_mp_*      Memory pool
26*4882a593Smuzhiyun  *
27*4882a593Smuzhiyun  * There are two main types of memory pools:
28*4882a593Smuzhiyun  *
29*4882a593Smuzhiyun  *    prealloc: The contiguous memory block of objects can either be supplied
30*4882a593Smuzhiyun  *              by the client or malloc'ed by the memory manager. The objects are
31*4882a593Smuzhiyun  *              allocated out of a block of memory and freed back to the block.
32*4882a593Smuzhiyun  *
33*4882a593Smuzhiyun  *    heap:     The memory pool allocator uses the heap (malloc/free) for memory.
34*4882a593Smuzhiyun  *              In this case, the pool allocator is just providing statistics
35*4882a593Smuzhiyun  *              and instrumentation on top of the heap, without modifying the heap
36*4882a593Smuzhiyun  *              allocation implementation.
37*4882a593Smuzhiyun  *
38*4882a593Smuzhiyun  * Portions of this code are copyright (c) 2021 Cypress Semiconductor Corporation
39*4882a593Smuzhiyun  *
40*4882a593Smuzhiyun  * Copyright (C) 1999-2017, Broadcom Corporation
41*4882a593Smuzhiyun  *
42*4882a593Smuzhiyun  *      Unless you and Broadcom execute a separate written software license
43*4882a593Smuzhiyun  * agreement governing use of this software, this software is licensed to you
44*4882a593Smuzhiyun  * under the terms of the GNU General Public License version 2 (the "GPL"),
45*4882a593Smuzhiyun  * available at http://www.broadcom.com/licenses/GPLv2.php, with the
46*4882a593Smuzhiyun  * following added to such license:
47*4882a593Smuzhiyun  *
48*4882a593Smuzhiyun  *      As a special exception, the copyright holders of this software give you
49*4882a593Smuzhiyun  * permission to link this software with independent modules, and to copy and
50*4882a593Smuzhiyun  * distribute the resulting executable under terms of your choice, provided that
51*4882a593Smuzhiyun  * you also meet, for each linked independent module, the terms and conditions of
52*4882a593Smuzhiyun  * the license of that module.  An independent module is a module which is not
53*4882a593Smuzhiyun  * derived from this software.  The special exception does not apply to any
54*4882a593Smuzhiyun  * modifications of the software.
55*4882a593Smuzhiyun  *
56*4882a593Smuzhiyun  *      Notwithstanding the above, under no circumstances may you combine this
57*4882a593Smuzhiyun  * software in any way with any other Broadcom software provided under a license
58*4882a593Smuzhiyun  * other than the GPL, without Broadcom's express prior written consent.
59*4882a593Smuzhiyun  *
60*4882a593Smuzhiyun  *
61*4882a593Smuzhiyun  * <<Broadcom-WL-IPTag/Open:>>
62*4882a593Smuzhiyun  *
63*4882a593Smuzhiyun  * $Id: bcm_mpool_pub.h 535090 2015-02-17 04:49:01Z $
64*4882a593Smuzhiyun  */
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun #ifndef _BCM_MPOOL_PUB_H
67*4882a593Smuzhiyun #define _BCM_MPOOL_PUB_H 1
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun #include <typedefs.h> /* needed for uint16 */
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun /*
72*4882a593Smuzhiyun **************************************************************************
73*4882a593Smuzhiyun *
74*4882a593Smuzhiyun * Type definitions, handles
75*4882a593Smuzhiyun *
76*4882a593Smuzhiyun **************************************************************************
77*4882a593Smuzhiyun */
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun /* Forward declaration of OSL handle. */
80*4882a593Smuzhiyun struct osl_info;
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun /* Forward declaration of string buffer. */
83*4882a593Smuzhiyun struct bcmstrbuf;
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun /*
86*4882a593Smuzhiyun  * Opaque type definition for the pool manager handle. This object is used for global
87*4882a593Smuzhiyun  * memory pool operations such as obtaining a new pool, deleting a pool, iterating and
88*4882a593Smuzhiyun  * instrumentation/debugging.
89*4882a593Smuzhiyun  */
90*4882a593Smuzhiyun struct bcm_mpm_mgr;
91*4882a593Smuzhiyun typedef struct bcm_mpm_mgr *bcm_mpm_mgr_h;
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun /*
94*4882a593Smuzhiyun  * Opaque type definition for an instance of a pool. This handle is used for allocating
95*4882a593Smuzhiyun  * and freeing memory through the pool, as well as management/instrumentation on this
96*4882a593Smuzhiyun  * specific pool.
97*4882a593Smuzhiyun  */
98*4882a593Smuzhiyun struct bcm_mp_pool;
99*4882a593Smuzhiyun typedef struct bcm_mp_pool *bcm_mp_pool_h;
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun /*
102*4882a593Smuzhiyun  * To make instrumentation more readable, every memory
103*4882a593Smuzhiyun  * pool must have a readable name. Pool names are up to
104*4882a593Smuzhiyun  * 8 bytes including '\0' termination. (7 printable characters.)
105*4882a593Smuzhiyun  */
106*4882a593Smuzhiyun #define BCM_MP_NAMELEN 8
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun /*
109*4882a593Smuzhiyun  * Type definition for pool statistics.
110*4882a593Smuzhiyun  */
111*4882a593Smuzhiyun typedef struct bcm_mp_stats {
112*4882a593Smuzhiyun 	char name[BCM_MP_NAMELEN];  /* Name of this pool. */
113*4882a593Smuzhiyun 	unsigned int objsz;         /* Object size allocated in this pool */
114*4882a593Smuzhiyun 	uint16 nobj;                /* Total number of objects in this pool */
115*4882a593Smuzhiyun 	uint16 num_alloc;           /* Number of objects currently allocated */
116*4882a593Smuzhiyun 	uint16 high_water;          /* Max number of allocated objects. */
117*4882a593Smuzhiyun 	uint16 failed_alloc;        /* Failed allocations. */
118*4882a593Smuzhiyun } bcm_mp_stats_t;
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun /*
121*4882a593Smuzhiyun **************************************************************************
122*4882a593Smuzhiyun *
123*4882a593Smuzhiyun * API Routines on the pool manager.
124*4882a593Smuzhiyun *
125*4882a593Smuzhiyun **************************************************************************
126*4882a593Smuzhiyun */
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun /*
129*4882a593Smuzhiyun  * bcm_mpm_init() - initialize the whole memory pool system.
130*4882a593Smuzhiyun  *
131*4882a593Smuzhiyun  * Parameters:
132*4882a593Smuzhiyun  *    osh:       INPUT  Operating system handle. Needed for heap memory allocation.
133*4882a593Smuzhiyun  *    max_pools: INPUT Maximum number of mempools supported.
134*4882a593Smuzhiyun  *    mgr:       OUTPUT The handle is written with the new pools manager object/handle.
135*4882a593Smuzhiyun  *
136*4882a593Smuzhiyun  * Returns:
137*4882a593Smuzhiyun  *    BCME_OK     Object initialized successfully. May be used.
138*4882a593Smuzhiyun  *    BCME_NOMEM  Initialization failed due to no memory. Object must not be used.
139*4882a593Smuzhiyun  */
140*4882a593Smuzhiyun int bcm_mpm_init(struct osl_info *osh, int max_pools, bcm_mpm_mgr_h *mgrp);
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun /*
143*4882a593Smuzhiyun  * bcm_mpm_deinit() - de-initialize the whole memory pool system.
144*4882a593Smuzhiyun  *
145*4882a593Smuzhiyun  * Parameters:
146*4882a593Smuzhiyun  *    mgr:     INPUT  Pointer to pool manager handle.
147*4882a593Smuzhiyun  *
148*4882a593Smuzhiyun  * Returns:
149*4882a593Smuzhiyun  *    BCME_OK  Memory pool manager successfully de-initialized.
150*4882a593Smuzhiyun  *    other    Indicated error occured during de-initialization.
151*4882a593Smuzhiyun  */
152*4882a593Smuzhiyun int bcm_mpm_deinit(bcm_mpm_mgr_h *mgrp);
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun /*
155*4882a593Smuzhiyun  * bcm_mpm_create_prealloc_pool() - Create a new pool for fixed size objects. The
156*4882a593Smuzhiyun  *                                  pool uses a contiguous block of pre-alloced
157*4882a593Smuzhiyun  *                                  memory. The memory block may either be provided
158*4882a593Smuzhiyun  *                                  by the client or dynamically allocated by the
159*4882a593Smuzhiyun  *                                  pool manager.
160*4882a593Smuzhiyun  *
161*4882a593Smuzhiyun  * Parameters:
162*4882a593Smuzhiyun  *    mgr:      INPUT  The handle to the pool manager
163*4882a593Smuzhiyun  *    obj_sz:   INPUT  Size of objects that will be allocated by the new pool
164*4882a593Smuzhiyun  *                     Must be >= sizeof(void *).
165*4882a593Smuzhiyun  *    nobj:     INPUT  Maximum number of concurrently existing objects to support
166*4882a593Smuzhiyun  *    memstart  INPUT  Pointer to the memory to use, or NULL to malloc()
167*4882a593Smuzhiyun  *    memsize   INPUT  Number of bytes referenced from memstart (for error checking).
168*4882a593Smuzhiyun  *                     Must be 0 if 'memstart' is NULL.
169*4882a593Smuzhiyun  *    poolname  INPUT  For instrumentation, the name of the pool
170*4882a593Smuzhiyun  *    newp:     OUTPUT The handle for the new pool, if creation is successful
171*4882a593Smuzhiyun  *
172*4882a593Smuzhiyun  * Returns:
173*4882a593Smuzhiyun  *    BCME_OK   Pool created ok.
174*4882a593Smuzhiyun  *    other     Pool not created due to indicated error. newpoolp set to NULL.
175*4882a593Smuzhiyun  *
176*4882a593Smuzhiyun  *
177*4882a593Smuzhiyun  */
178*4882a593Smuzhiyun int bcm_mpm_create_prealloc_pool(bcm_mpm_mgr_h mgr,
179*4882a593Smuzhiyun                                  unsigned int obj_sz,
180*4882a593Smuzhiyun                                  int nobj,
181*4882a593Smuzhiyun                                  void *memstart,
182*4882a593Smuzhiyun                                  unsigned int memsize,
183*4882a593Smuzhiyun                                  const char poolname[BCM_MP_NAMELEN],
184*4882a593Smuzhiyun                                  bcm_mp_pool_h *newp);
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun /*
187*4882a593Smuzhiyun  * bcm_mpm_delete_prealloc_pool() - Delete a memory pool. This should only be called after
188*4882a593Smuzhiyun  *                                  all memory objects have been freed back to the pool.
189*4882a593Smuzhiyun  *
190*4882a593Smuzhiyun  * Parameters:
191*4882a593Smuzhiyun  *    mgr:     INPUT The handle to the pools manager
192*4882a593Smuzhiyun  *    pool:    INPUT The handle of the  pool to delete
193*4882a593Smuzhiyun  *
194*4882a593Smuzhiyun  * Returns:
195*4882a593Smuzhiyun  *    BCME_OK   Pool deleted ok.
196*4882a593Smuzhiyun  *    other     Pool not deleted due to indicated error.
197*4882a593Smuzhiyun  *
198*4882a593Smuzhiyun  */
199*4882a593Smuzhiyun int bcm_mpm_delete_prealloc_pool(bcm_mpm_mgr_h mgr, bcm_mp_pool_h *poolp);
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun /*
202*4882a593Smuzhiyun  * bcm_mpm_create_heap_pool() - Create a new pool for fixed size objects. The memory
203*4882a593Smuzhiyun  *                              pool allocator uses the heap (malloc/free) for memory.
204*4882a593Smuzhiyun  *                              In this case, the pool allocator is just providing
205*4882a593Smuzhiyun  *                              statistics and instrumentation on top of the heap,
206*4882a593Smuzhiyun  *                              without modifying the heap allocation implementation.
207*4882a593Smuzhiyun  *
208*4882a593Smuzhiyun  * Parameters:
209*4882a593Smuzhiyun  *    mgr:      INPUT  The handle to the pool manager
210*4882a593Smuzhiyun  *    obj_sz:   INPUT  Size of objects that will be allocated by the new pool
211*4882a593Smuzhiyun  *    poolname  INPUT  For instrumentation, the name of the pool
212*4882a593Smuzhiyun  *    newp:     OUTPUT The handle for the new pool, if creation is successful
213*4882a593Smuzhiyun  *
214*4882a593Smuzhiyun  * Returns:
215*4882a593Smuzhiyun  *    BCME_OK   Pool created ok.
216*4882a593Smuzhiyun  *    other     Pool not created due to indicated error. newpoolp set to NULL.
217*4882a593Smuzhiyun  *
218*4882a593Smuzhiyun  *
219*4882a593Smuzhiyun  */
220*4882a593Smuzhiyun int bcm_mpm_create_heap_pool(bcm_mpm_mgr_h mgr, unsigned int obj_sz,
221*4882a593Smuzhiyun                              const char poolname[BCM_MP_NAMELEN],
222*4882a593Smuzhiyun                              bcm_mp_pool_h *newp);
223*4882a593Smuzhiyun 
224*4882a593Smuzhiyun /*
225*4882a593Smuzhiyun  * bcm_mpm_delete_heap_pool() - Delete a memory pool. This should only be called after
226*4882a593Smuzhiyun  *                              all memory objects have been freed back to the pool.
227*4882a593Smuzhiyun  *
228*4882a593Smuzhiyun  * Parameters:
229*4882a593Smuzhiyun  *    mgr:     INPUT The handle to the pools manager
230*4882a593Smuzhiyun  *    pool:    INPUT The handle of the  pool to delete
231*4882a593Smuzhiyun  *
232*4882a593Smuzhiyun  * Returns:
233*4882a593Smuzhiyun  *    BCME_OK   Pool deleted ok.
234*4882a593Smuzhiyun  *    other     Pool not deleted due to indicated error.
235*4882a593Smuzhiyun  *
236*4882a593Smuzhiyun  */
237*4882a593Smuzhiyun int bcm_mpm_delete_heap_pool(bcm_mpm_mgr_h mgr, bcm_mp_pool_h *poolp);
238*4882a593Smuzhiyun 
239*4882a593Smuzhiyun /*
240*4882a593Smuzhiyun  * bcm_mpm_stats() - Return stats for all pools
241*4882a593Smuzhiyun  *
242*4882a593Smuzhiyun  * Parameters:
243*4882a593Smuzhiyun  *    mgr:         INPUT   The handle to the pools manager
244*4882a593Smuzhiyun  *    stats:       OUTPUT  Array of pool statistics.
245*4882a593Smuzhiyun  *    nentries:    MOD     Max elements in 'stats' array on INPUT. Actual number
246*4882a593Smuzhiyun  *                         of array elements copied to 'stats' on OUTPUT.
247*4882a593Smuzhiyun  *
248*4882a593Smuzhiyun  * Returns:
249*4882a593Smuzhiyun  *    BCME_OK   Ok
250*4882a593Smuzhiyun  *    other     Error getting stats.
251*4882a593Smuzhiyun  *
252*4882a593Smuzhiyun  */
253*4882a593Smuzhiyun int bcm_mpm_stats(bcm_mpm_mgr_h mgr, bcm_mp_stats_t *stats, int *nentries);
254*4882a593Smuzhiyun 
255*4882a593Smuzhiyun /*
256*4882a593Smuzhiyun  * bcm_mpm_dump() - Display statistics on all pools
257*4882a593Smuzhiyun  *
258*4882a593Smuzhiyun  * Parameters:
259*4882a593Smuzhiyun  *    mgr:     INPUT  The handle to the pools manager
260*4882a593Smuzhiyun  *    b:       OUTPUT Output buffer.
261*4882a593Smuzhiyun  *
262*4882a593Smuzhiyun  * Returns:
263*4882a593Smuzhiyun  *    BCME_OK   Ok
264*4882a593Smuzhiyun  *    other     Error during dump.
265*4882a593Smuzhiyun  *
266*4882a593Smuzhiyun  */
267*4882a593Smuzhiyun int bcm_mpm_dump(bcm_mpm_mgr_h mgr, struct bcmstrbuf *b);
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun /*
270*4882a593Smuzhiyun  * bcm_mpm_get_obj_size() - The size of memory objects may need to be padded to
271*4882a593Smuzhiyun  *                          compensate for alignment requirements of the objects.
272*4882a593Smuzhiyun  *                          This function provides the padded object size. If clients
273*4882a593Smuzhiyun  *                          pre-allocate a memory slab for a memory pool, the
274*4882a593Smuzhiyun  *                          padded object size should be used by the client to allocate
275*4882a593Smuzhiyun  *                          the memory slab (in order to provide sufficent space for
276*4882a593Smuzhiyun  *                          the maximum number of objects).
277*4882a593Smuzhiyun  *
278*4882a593Smuzhiyun  * Parameters:
279*4882a593Smuzhiyun  *    mgr:            INPUT   The handle to the pools manager.
280*4882a593Smuzhiyun  *    obj_sz:         INPUT   Input object size.
281*4882a593Smuzhiyun  *    padded_obj_sz:  OUTPUT  Padded object size.
282*4882a593Smuzhiyun  *
283*4882a593Smuzhiyun  * Returns:
284*4882a593Smuzhiyun  *    BCME_OK      Ok
285*4882a593Smuzhiyun  *    BCME_BADARG  Bad arguments.
286*4882a593Smuzhiyun  *
287*4882a593Smuzhiyun  */
288*4882a593Smuzhiyun int bcm_mpm_get_obj_size(bcm_mpm_mgr_h mgr, unsigned int obj_sz, unsigned int *padded_obj_sz);
289*4882a593Smuzhiyun 
290*4882a593Smuzhiyun /*
291*4882a593Smuzhiyun ***************************************************************************
292*4882a593Smuzhiyun *
293*4882a593Smuzhiyun * API Routines on a specific pool.
294*4882a593Smuzhiyun *
295*4882a593Smuzhiyun ***************************************************************************
296*4882a593Smuzhiyun */
297*4882a593Smuzhiyun 
298*4882a593Smuzhiyun /*
299*4882a593Smuzhiyun  * bcm_mp_alloc() - Allocate a memory pool object.
300*4882a593Smuzhiyun  *
301*4882a593Smuzhiyun  * Parameters:
302*4882a593Smuzhiyun  *    pool:    INPUT    The handle to the pool.
303*4882a593Smuzhiyun  *
304*4882a593Smuzhiyun  * Returns:
305*4882a593Smuzhiyun  *    A pointer to the new object. NULL on error.
306*4882a593Smuzhiyun  *
307*4882a593Smuzhiyun  */
308*4882a593Smuzhiyun void* bcm_mp_alloc(bcm_mp_pool_h pool);
309*4882a593Smuzhiyun 
310*4882a593Smuzhiyun /*
311*4882a593Smuzhiyun  * bcm_mp_free() - Free a memory pool object.
312*4882a593Smuzhiyun  *
313*4882a593Smuzhiyun  * Parameters:
314*4882a593Smuzhiyun  *    pool:  INPUT   The handle to the pool.
315*4882a593Smuzhiyun  *    objp:  INPUT   A pointer to the object to free.
316*4882a593Smuzhiyun  *
317*4882a593Smuzhiyun  * Returns:
318*4882a593Smuzhiyun  *    BCME_OK   Ok
319*4882a593Smuzhiyun  *    other     Error during free.
320*4882a593Smuzhiyun  *
321*4882a593Smuzhiyun  */
322*4882a593Smuzhiyun int bcm_mp_free(bcm_mp_pool_h pool, void *objp);
323*4882a593Smuzhiyun 
324*4882a593Smuzhiyun /*
325*4882a593Smuzhiyun  * bcm_mp_stats() - Return stats for this pool
326*4882a593Smuzhiyun  *
327*4882a593Smuzhiyun  * Parameters:
328*4882a593Smuzhiyun  *    pool:     INPUT    The handle to the pool
329*4882a593Smuzhiyun  *    stats:    OUTPUT   Pool statistics
330*4882a593Smuzhiyun  *
331*4882a593Smuzhiyun  * Returns:
332*4882a593Smuzhiyun  *    BCME_OK   Ok
333*4882a593Smuzhiyun  *    other     Error getting statistics.
334*4882a593Smuzhiyun  *
335*4882a593Smuzhiyun  */
336*4882a593Smuzhiyun void bcm_mp_stats(bcm_mp_pool_h pool, bcm_mp_stats_t *stats);
337*4882a593Smuzhiyun 
338*4882a593Smuzhiyun /*
339*4882a593Smuzhiyun  * bcm_mp_dump() - Dump a pool
340*4882a593Smuzhiyun  *
341*4882a593Smuzhiyun  * Parameters:
342*4882a593Smuzhiyun  *    pool:    INPUT    The handle to the pool
343*4882a593Smuzhiyun  *    b        OUTPUT   Output buffer
344*4882a593Smuzhiyun  *
345*4882a593Smuzhiyun  * Returns:
346*4882a593Smuzhiyun  *    BCME_OK   Ok
347*4882a593Smuzhiyun  *    other     Error during dump.
348*4882a593Smuzhiyun  *
349*4882a593Smuzhiyun  */
350*4882a593Smuzhiyun int bcm_mp_dump(bcm_mp_pool_h pool, struct bcmstrbuf *b);
351*4882a593Smuzhiyun 
352*4882a593Smuzhiyun #endif /* _BCM_MPOOL_PUB_H */
353