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