Path: blob/21.2-virgl/src/panfrost/vulkan/panvk_mempool.c
4560 views
/*1* © Copyright 2018 Alyssa Rosenzweig2* Copyright (C) 2019 Collabora, Ltd.3*4* Permission is hereby granted, free of charge, to any person obtaining a5* copy of this software and associated documentation files (the "Software"),6* to deal in the Software without restriction, including without limitation7* the rights to use, copy, modify, merge, publish, distribute, sublicense,8* and/or sell copies of the Software, and to permit persons to whom the9* Software is furnished to do so, subject to the following conditions:10*11* The above copyright notice and this permission notice (including the next12* paragraph) shall be included in all copies or substantial portions of the13* Software.14*15* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR16* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL18* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER19* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,20* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE21* SOFTWARE.22*23*/2425#include "pan_device.h"26#include "panvk_mempool.h"2728/* Knockoff u_upload_mgr. Uploads wherever we left off, allocating new entries29* when needed.30*31* In "owned" mode, a single parent owns the entire pool, and the pool owns all32* created BOs. All BOs are tracked and addable as33* panvk_pool_get_bo_handles. Freeing occurs at the level of an entire pool.34* This is useful for streaming uploads, where the batch owns the pool.35*36* In "unowned" mode, the pool is freestanding. It does not track created BOs37* or hold references. Instead, the consumer must manage the created BOs. This38* is more flexible, enabling non-transient CSO state or shader code to be39* packed with conservative lifetime handling.40*/4142static struct panfrost_bo *43panvk_pool_alloc_backing(struct panvk_pool *pool, size_t bo_sz)44{45/* If there's a free BO in our BO pool, let's pick it. */46if (pool->bo_pool &&47util_dynarray_num_elements(&pool->bo_pool->free_bos, struct panfrost_bo *))48return util_dynarray_pop(&pool->bo_pool->free_bos, struct panfrost_bo *);4950/* We don't know what the BO will be used for, so let's flag it51* RW and attach it to both the fragment and vertex/tiler jobs.52* TODO: if we want fine grained BO assignment we should pass53* flags to this function and keep the read/write,54* fragment/vertex+tiler pools separate.55*/56struct panfrost_bo *bo = panfrost_bo_create(pool->base.dev, bo_sz,57pool->base.create_flags,58pool->base.label);5960util_dynarray_append(&pool->bos, struct panfrost_bo *, bo);61pool->transient_bo = bo;62pool->transient_offset = 0;6364return bo;65}6667static struct panfrost_ptr68panvk_pool_alloc_aligned(struct panvk_pool *pool, size_t sz, unsigned alignment)69{70assert(alignment == util_next_power_of_two(alignment));7172/* Find or create a suitable BO */73struct panfrost_bo *bo = pool->transient_bo;74unsigned offset = ALIGN_POT(pool->transient_offset, alignment);7576/* If we don't fit, allocate a new backing */77if (unlikely(bo == NULL || (offset + sz) >= pool->base.slab_size)) {78bo = panvk_pool_alloc_backing(pool,79ALIGN_POT(MAX2(pool->base.slab_size, sz),804096));81offset = 0;82}8384pool->transient_offset = offset + sz;8586struct panfrost_ptr ret = {87.cpu = bo->ptr.cpu + offset,88.gpu = bo->ptr.gpu + offset,89};9091return ret;92}93PAN_POOL_ALLOCATOR(struct panvk_pool, panvk_pool_alloc_aligned)9495void96panvk_pool_init(struct panvk_pool *pool,97struct panfrost_device *dev, struct panvk_bo_pool *bo_pool,98unsigned create_flags, size_t slab_size, const char *label,99bool prealloc)100{101memset(pool, 0, sizeof(*pool));102pan_pool_init(&pool->base, dev, create_flags, slab_size, label);103pool->bo_pool = bo_pool;104105util_dynarray_init(&pool->bos, NULL);106107if (prealloc)108panvk_pool_alloc_backing(pool, pool->base.slab_size);109}110111void112panvk_pool_reset(struct panvk_pool *pool)113{114if (pool->bo_pool) {115unsigned num_bos = panvk_pool_num_bos(pool);116void *ptr = util_dynarray_grow(&pool->bo_pool->free_bos,117struct panfrost_bo *, num_bos);118memcpy(ptr, util_dynarray_begin(&pool->bos),119num_bos * sizeof(struct panfrost_bo *));120} else {121util_dynarray_foreach(&pool->bos, struct panfrost_bo *, bo)122panfrost_bo_unreference(*bo);123}124125util_dynarray_clear(&pool->bos);126}127128void129panvk_pool_cleanup(struct panvk_pool *pool)130{131panvk_pool_reset(pool);132util_dynarray_fini(&pool->bos);133}134135void136panvk_pool_get_bo_handles(struct panvk_pool *pool, uint32_t *handles)137{138unsigned idx = 0;139util_dynarray_foreach(&pool->bos, struct panfrost_bo *, bo) {140assert((*bo)->gem_handle > 0);141handles[idx++] = (*bo)->gem_handle;142}143}144145146