/*1* © Copyright 2017-2018 Alyssa Rosenzweig2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,19* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE20* SOFTWARE.21*22*/2324#ifndef __AGX_POOL_H__25#define __AGX_POOL_H__2627#include <stddef.h>28#include "asahi/lib/agx_pack.h"29#include "agx_bo.h"3031#include "util/u_dynarray.h"3233/* Represents a pool of memory that can only grow, used to allocate objects34* with the same lifetime as the pool itself. In OpenGL, a pool is owned by the35* batch for transient structures. In Vulkan, it may be owned by e.g. the36* command pool */3738struct agx_pool {39/* Parent device for allocation */40struct agx_device *dev;4142/* BOs allocated by this pool */43struct util_dynarray bos;4445/* Current transient BO */46struct agx_bo *transient_bo;4748/* Within the topmost transient BO, how much has been used? */49unsigned transient_offset;5051/* BO flags to use in the pool */52unsigned create_flags;53};5455void56agx_pool_init(struct agx_pool *pool, struct agx_device *dev,57unsigned create_flags, bool prealloc);5859void60agx_pool_cleanup(struct agx_pool *pool);6162static inline unsigned63agx_pool_num_bos(struct agx_pool *pool)64{65return util_dynarray_num_elements(&pool->bos, struct agx_bo *);66}6768void69agx_pool_get_bo_handles(struct agx_pool *pool, uint32_t *handles);7071/* Represents a fat pointer for GPU-mapped memory, returned from the transient72* allocator and not used for much else */7374struct agx_ptr75agx_pool_alloc_aligned(struct agx_pool *pool, size_t sz, unsigned alignment);7677uint64_t78agx_pool_upload(struct agx_pool *pool, const void *data, size_t sz);7980uint64_t81agx_pool_upload_aligned(struct agx_pool *pool, const void *data, size_t sz, unsigned alignment);8283struct agx_desc_alloc_info {84unsigned size;85unsigned align;86unsigned nelems;87};8889#define AGX_DESC_ARRAY(count, name) \90{ \91.size = MALI_ ## name ## _LENGTH, \92.align = MALI_ ## name ## _ALIGN, \93.nelems = count, \94}9596#define AGX_DESC(name) AGX_DESC_ARRAY(1, name)9798#define AGX_DESC_AGGREGATE(...) \99(struct agx_desc_alloc_info[]) { \100__VA_ARGS__, \101{ 0 }, \102}103104static inline struct agx_ptr105agx_pool_alloc_descs(struct agx_pool *pool,106const struct agx_desc_alloc_info *descs)107{108unsigned size = 0;109unsigned align = descs[0].align;110111for (unsigned i = 0; descs[i].size; i++) {112assert(!(size & (descs[i].align - 1)));113size += descs[i].size * descs[i].nelems;114}115116return agx_pool_alloc_aligned(pool, size, align);117}118119#define agx_pool_alloc_desc(pool, name) \120agx_pool_alloc_descs(pool, AGX_DESC_AGGREGATE(AGX_DESC(name)))121122#define agx_pool_alloc_desc_array(pool, count, name) \123agx_pool_alloc_descs(pool, AGX_DESC_AGGREGATE(AGX_DESC_ARRAY(count, name)))124125#define agx_pool_alloc_desc_aggregate(pool, ...) \126agx_pool_alloc_descs(pool, AGX_DESC_AGGREGATE(__VA_ARGS__))127128#endif129130131