Path: blob/21.2-virgl/src/panfrost/lib/pan_pool.h
4560 views
/*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 __PAN_POOL_H__25#define __PAN_POOL_H__2627#include <stddef.h>28#include <midgard_pack.h>29#include "pan_bo.h"3031#include "util/u_dynarray.h"3233/* Represents grow-only memory. */3435struct pan_pool {36/* Parent device for allocation */37struct panfrost_device *dev;3839/* Label for created BOs */40const char *label;4142/* BO flags to use in the pool */43unsigned create_flags;4445/* Minimum size for allocated BOs. */46size_t slab_size;47};4849static inline void50pan_pool_init(struct pan_pool *pool, struct panfrost_device *dev,51unsigned create_flags, size_t slab_size, const char *label)52{53pool->dev = dev;54pool->create_flags = create_flags;55pool->slab_size = slab_size;56pool->label = label;57}5859/* Represents a fat pointer for GPU-mapped memory, returned from the transient60* allocator and not used for much else */6162struct panfrost_ptr63pan_pool_alloc_aligned(struct pan_pool *pool, size_t sz, unsigned alignment);6465#define PAN_POOL_ALLOCATOR(pool_subclass, alloc_func) \66struct panfrost_ptr \67pan_pool_alloc_aligned(struct pan_pool *p, size_t sz, unsigned alignment) \68{ \69pool_subclass *pool = container_of(p, pool_subclass, base); \70return alloc_func(pool, sz, alignment); \71}7273static inline mali_ptr74pan_pool_upload_aligned(struct pan_pool *pool, const void *data, size_t sz, unsigned alignment)75{76struct panfrost_ptr transfer = pan_pool_alloc_aligned(pool, sz, alignment);77memcpy(transfer.cpu, data, sz);78return transfer.gpu;79}8081static inline mali_ptr82pan_pool_upload(struct pan_pool *pool, const void *data, size_t sz)83{84return pan_pool_upload_aligned(pool, data, sz, sz);85}8687struct pan_desc_alloc_info {88unsigned size;89unsigned align;90unsigned nelems;91};9293#define PAN_DESC_ARRAY(count, name) \94{ \95.size = MALI_ ## name ## _LENGTH, \96.align = MALI_ ## name ## _ALIGN, \97.nelems = count, \98}99100#define PAN_DESC(name) PAN_DESC_ARRAY(1, name)101102#define PAN_DESC_AGGREGATE(...) \103(struct pan_desc_alloc_info[]) { \104__VA_ARGS__, \105{ 0 }, \106}107108static inline struct panfrost_ptr109pan_pool_alloc_descs(struct pan_pool *pool,110const struct pan_desc_alloc_info *descs)111{112unsigned size = 0;113unsigned align = descs[0].align;114115for (unsigned i = 0; descs[i].size; i++) {116assert(!(size & (descs[i].align - 1)));117size += descs[i].size * descs[i].nelems;118}119120return pan_pool_alloc_aligned(pool, size, align);121}122123#define pan_pool_alloc_desc(pool, name) \124pan_pool_alloc_descs(pool, PAN_DESC_AGGREGATE(PAN_DESC(name)))125126#define pan_pool_alloc_desc_array(pool, count, name) \127pan_pool_alloc_descs(pool, PAN_DESC_AGGREGATE(PAN_DESC_ARRAY(count, name)))128129#define pan_pool_alloc_desc_aggregate(pool, ...) \130pan_pool_alloc_descs(pool, PAN_DESC_AGGREGATE(__VA_ARGS__))131132#endif133134135