1*5113495bSYour Name /* 2*5113495bSYour Name * Copyright (c) 2018-2019 The Linux Foundation. All rights reserved. 3*5113495bSYour Name * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights reserved. 4*5113495bSYour Name * 5*5113495bSYour Name * Permission to use, copy, modify, and/or distribute this software for 6*5113495bSYour Name * any purpose with or without fee is hereby granted, provided that the 7*5113495bSYour Name * above copyright notice and this permission notice appear in all 8*5113495bSYour Name * copies. 9*5113495bSYour Name * 10*5113495bSYour Name * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL 11*5113495bSYour Name * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED 12*5113495bSYour Name * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE 13*5113495bSYour Name * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 14*5113495bSYour Name * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR 15*5113495bSYour Name * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 16*5113495bSYour Name * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 17*5113495bSYour Name * PERFORMANCE OF THIS SOFTWARE. 18*5113495bSYour Name */ 19*5113495bSYour Name 20*5113495bSYour Name /** 21*5113495bSYour Name * DOC: qdf_flex_mem (flexibly sized memory allocator) 22*5113495bSYour Name * QCA driver framework (QDF) flex mem APIs 23*5113495bSYour Name * 24*5113495bSYour Name * A flex memory allocator is a memory pool which not only dynamically expands, 25*5113495bSYour Name * but also dynamically reduces as well. Benefits over full dynamic memory 26*5113495bSYour Name * allocation are amoritized allocation cost, and reduced memory fragmentation. 27*5113495bSYour Name * 28*5113495bSYour Name * The allocator consists of 3 parts: the pool, segments, and items. Items are 29*5113495bSYour Name * the smallest chunks of memory that are handed out via the alloc call, and 30*5113495bSYour Name * are all of a uniform size. Segments are groups of items, representing the 31*5113495bSYour Name * smallest amount of memory that can be dynamically allocated or freed. A pool 32*5113495bSYour Name * is simply a collection of segments. 33*5113495bSYour Name */ 34*5113495bSYour Name 35*5113495bSYour Name #ifndef __QDF_FLEX_MEM_H 36*5113495bSYour Name #define __QDF_FLEX_MEM_H 37*5113495bSYour Name 38*5113495bSYour Name #include "qdf_list.h" 39*5113495bSYour Name #include "qdf_lock.h" 40*5113495bSYour Name 41*5113495bSYour Name #define QDF_FM_BITMAP uint32_t 42*5113495bSYour Name #define QDF_FM_BITMAP_BITS (sizeof(QDF_FM_BITMAP) * 8) 43*5113495bSYour Name 44*5113495bSYour Name /** 45*5113495bSYour Name * struct qdf_flex_mem_pool - a pool of memory segments 46*5113495bSYour Name * @seg_list: the list containing the memory segments 47*5113495bSYour Name * @lock: spinlock for protecting internal data structures 48*5113495bSYour Name * @reduction_limit: the minimum number of segments to keep during reduction 49*5113495bSYour Name * @item_size: the size of the items the pool will allocate 50*5113495bSYour Name */ 51*5113495bSYour Name struct qdf_flex_mem_pool { 52*5113495bSYour Name qdf_list_t seg_list; 53*5113495bSYour Name struct qdf_spinlock lock; 54*5113495bSYour Name uint16_t reduction_limit; 55*5113495bSYour Name uint16_t item_size; 56*5113495bSYour Name }; 57*5113495bSYour Name 58*5113495bSYour Name /** 59*5113495bSYour Name * struct qdf_flex_mem_segment - a memory pool segment 60*5113495bSYour Name * @node: the list node for membership in the memory pool 61*5113495bSYour Name * @dynamic: true if this segment was dynamically allocated 62*5113495bSYour Name * @used_bitmap: bitmap for tracking which items in the segment are in use 63*5113495bSYour Name * @bytes: raw memory for allocating items from 64*5113495bSYour Name */ 65*5113495bSYour Name struct qdf_flex_mem_segment { 66*5113495bSYour Name qdf_list_node_t node; 67*5113495bSYour Name bool dynamic; 68*5113495bSYour Name QDF_FM_BITMAP used_bitmap; 69*5113495bSYour Name uint8_t *bytes; 70*5113495bSYour Name }; 71*5113495bSYour Name 72*5113495bSYour Name /** 73*5113495bSYour Name * DEFINE_QDF_FLEX_MEM_POOL() - define a new flex mem pool with one segment 74*5113495bSYour Name * @name: the name of the pool variable 75*5113495bSYour Name * @size_of_item: size of the items the pool will allocate 76*5113495bSYour Name * @rm_limit: min number of segments to keep during reduction 77*5113495bSYour Name */ 78*5113495bSYour Name #define DEFINE_QDF_FLEX_MEM_POOL(name, size_of_item, rm_limit) \ 79*5113495bSYour Name struct qdf_flex_mem_pool name; \ 80*5113495bSYour Name uint8_t __ ## name ## _head_bytes[QDF_FM_BITMAP_BITS * (size_of_item)];\ 81*5113495bSYour Name struct qdf_flex_mem_segment __ ## name ## _head = { \ 82*5113495bSYour Name .node = QDF_LIST_NODE_INIT_SINGLE( \ 83*5113495bSYour Name QDF_LIST_ANCHOR(name.seg_list)), \ 84*5113495bSYour Name .bytes = __ ## name ## _head_bytes, \ 85*5113495bSYour Name }; \ 86*5113495bSYour Name struct qdf_flex_mem_pool name = { \ 87*5113495bSYour Name .seg_list = QDF_LIST_INIT_SINGLE(__ ## name ## _head.node), \ 88*5113495bSYour Name .reduction_limit = (rm_limit), \ 89*5113495bSYour Name .item_size = (size_of_item), \ 90*5113495bSYour Name } 91*5113495bSYour Name 92*5113495bSYour Name /** 93*5113495bSYour Name * qdf_flex_mem_init() - initialize a qdf_flex_mem_pool 94*5113495bSYour Name * @pool: the pool to initialize 95*5113495bSYour Name * 96*5113495bSYour Name * Return: None 97*5113495bSYour Name */ 98*5113495bSYour Name void qdf_flex_mem_init(struct qdf_flex_mem_pool *pool); 99*5113495bSYour Name 100*5113495bSYour Name /** 101*5113495bSYour Name * qdf_flex_mem_deinit() - deinitialize a qdf_flex_mem_pool 102*5113495bSYour Name * @pool: the pool to deinitialize 103*5113495bSYour Name * 104*5113495bSYour Name * Return: None 105*5113495bSYour Name */ 106*5113495bSYour Name void qdf_flex_mem_deinit(struct qdf_flex_mem_pool *pool); 107*5113495bSYour Name 108*5113495bSYour Name /** 109*5113495bSYour Name * qdf_flex_mem_alloc() - logically allocate memory from the pool 110*5113495bSYour Name * @pool: the pool to allocate from 111*5113495bSYour Name * 112*5113495bSYour Name * This function returns any unused item from any existing segment in the pool. 113*5113495bSYour Name * If there are no unused items in the pool, a new segment is dynamically 114*5113495bSYour Name * allocated to service the request. The size of the allocated memory is the 115*5113495bSYour Name * size originally used to create the pool. 116*5113495bSYour Name * 117*5113495bSYour Name * Return: Point to newly allocated memory, NULL on failure 118*5113495bSYour Name */ 119*5113495bSYour Name void *qdf_flex_mem_alloc(struct qdf_flex_mem_pool *pool); 120*5113495bSYour Name 121*5113495bSYour Name /** 122*5113495bSYour Name * qdf_flex_mem_free() - logically frees @ptr from the pool 123*5113495bSYour Name * @pool: the pool to return the memory to 124*5113495bSYour Name * @ptr: a pointer received via a call to qdf_flex_mem_alloc() 125*5113495bSYour Name * 126*5113495bSYour Name * This function marks the item corresponding to @ptr as unused. If that item 127*5113495bSYour Name * was the last used item in the segment it belongs to, and the segment was 128*5113495bSYour Name * dynamically allocated, the segment will be freed. 129*5113495bSYour Name * 130*5113495bSYour Name * Return: None 131*5113495bSYour Name */ 132*5113495bSYour Name void qdf_flex_mem_free(struct qdf_flex_mem_pool *pool, void *ptr); 133*5113495bSYour Name 134*5113495bSYour Name #endif /* __QDF_FLEX_MEM_H */ 135