Path: blob/21.2-virgl/src/gallium/drivers/panfrost/pan_mempool.c
4570 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 "pan_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* panfrost_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 *43panfrost_pool_alloc_backing(struct panfrost_pool *pool, size_t bo_sz)44{45/* We don't know what the BO will be used for, so let's flag it46* RW and attach it to both the fragment and vertex/tiler jobs.47* TODO: if we want fine grained BO assignment we should pass48* flags to this function and keep the read/write,49* fragment/vertex+tiler pools separate.50*/51struct panfrost_bo *bo = panfrost_bo_create(pool->base.dev, bo_sz,52pool->base.create_flags, pool->base.label);5354if (pool->owned)55util_dynarray_append(&pool->bos, struct panfrost_bo *, bo);56else57panfrost_bo_unreference(pool->transient_bo);5859pool->transient_bo = bo;60pool->transient_offset = 0;6162return bo;63}6465void66panfrost_pool_init(struct panfrost_pool *pool, void *memctx,67struct panfrost_device *dev,68unsigned create_flags, size_t slab_size, const char *label,69bool prealloc, bool owned)70{71memset(pool, 0, sizeof(*pool));72pan_pool_init(&pool->base, dev, create_flags, slab_size, label);73pool->owned = owned;7475if (owned)76util_dynarray_init(&pool->bos, memctx);7778if (prealloc)79panfrost_pool_alloc_backing(pool, pool->base.slab_size);80}8182void83panfrost_pool_cleanup(struct panfrost_pool *pool)84{85if (!pool->owned) {86panfrost_bo_unreference(pool->transient_bo);87return;88}8990util_dynarray_foreach(&pool->bos, struct panfrost_bo *, bo)91panfrost_bo_unreference(*bo);9293util_dynarray_fini(&pool->bos);94}9596void97panfrost_pool_get_bo_handles(struct panfrost_pool *pool, uint32_t *handles)98{99assert(pool->owned && "pool does not track BOs in unowned mode");100101unsigned idx = 0;102util_dynarray_foreach(&pool->bos, struct panfrost_bo *, bo) {103assert((*bo)->gem_handle > 0);104handles[idx++] = (*bo)->gem_handle;105106/* Update the BO access flags so that panfrost_bo_wait() knows107* about all pending accesses.108* We only keep the READ/WRITE info since this is all the BO109* wait logic cares about.110* We also preserve existing flags as this batch might not111* be the first one to access the BO.112*/113(*bo)->gpu_access |= PAN_BO_ACCESS_RW;114}115}116117static struct panfrost_ptr118panfrost_pool_alloc_aligned(struct panfrost_pool *pool, size_t sz, unsigned alignment)119{120assert(alignment == util_next_power_of_two(alignment));121122/* Find or create a suitable BO */123struct panfrost_bo *bo = pool->transient_bo;124unsigned offset = ALIGN_POT(pool->transient_offset, alignment);125126/* If we don't fit, allocate a new backing */127if (unlikely(bo == NULL || (offset + sz) >= pool->base.slab_size)) {128bo = panfrost_pool_alloc_backing(pool,129ALIGN_POT(MAX2(pool->base.slab_size, sz), 4096));130offset = 0;131}132133pool->transient_offset = offset + sz;134135struct panfrost_ptr ret = {136.cpu = bo->ptr.cpu + offset,137.gpu = bo->ptr.gpu + offset,138};139140return ret;141}142PAN_POOL_ALLOCATOR(struct panfrost_pool, panfrost_pool_alloc_aligned)143144145