17ec681f3Smrg/*
27ec681f3Smrg * © Copyright 2017-2018 Alyssa Rosenzweig
37ec681f3Smrg *
47ec681f3Smrg * Permission is hereby granted, free of charge, to any person obtaining a
57ec681f3Smrg * copy of this software and associated documentation files (the "Software"),
67ec681f3Smrg * to deal in the Software without restriction, including without limitation
77ec681f3Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
87ec681f3Smrg * and/or sell copies of the Software, and to permit persons to whom the
97ec681f3Smrg * Software is furnished to do so, subject to the following conditions:
107ec681f3Smrg *
117ec681f3Smrg * The above copyright notice and this permission notice (including the next
127ec681f3Smrg * paragraph) shall be included in all copies or substantial portions of the
137ec681f3Smrg * Software.
147ec681f3Smrg *
157ec681f3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
167ec681f3Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
177ec681f3Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
187ec681f3Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
197ec681f3Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
207ec681f3Smrg * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
217ec681f3Smrg * SOFTWARE.
227ec681f3Smrg *
237ec681f3Smrg */
247ec681f3Smrg
257ec681f3Smrg#ifndef __PAN_POOL_H__
267ec681f3Smrg#define __PAN_POOL_H__
277ec681f3Smrg
287ec681f3Smrg#include <stddef.h>
297ec681f3Smrg#include <genxml/gen_macros.h>
307ec681f3Smrg#include "pan_bo.h"
317ec681f3Smrg
327ec681f3Smrg#include "util/u_dynarray.h"
337ec681f3Smrg
347ec681f3Smrg/* Represents grow-only memory. */
357ec681f3Smrg
367ec681f3Smrgstruct pan_pool {
377ec681f3Smrg        /* Parent device for allocation */
387ec681f3Smrg        struct panfrost_device *dev;
397ec681f3Smrg
407ec681f3Smrg        /* Label for created BOs */
417ec681f3Smrg        const char *label;
427ec681f3Smrg
437ec681f3Smrg        /* BO flags to use in the pool */
447ec681f3Smrg        unsigned create_flags;
457ec681f3Smrg
467ec681f3Smrg        /* Minimum size for allocated BOs. */
477ec681f3Smrg        size_t slab_size;
487ec681f3Smrg};
497ec681f3Smrg
507ec681f3Smrgstatic inline void
517ec681f3Smrgpan_pool_init(struct pan_pool *pool, struct panfrost_device *dev,
527ec681f3Smrg              unsigned create_flags, size_t slab_size, const char *label)
537ec681f3Smrg{
547ec681f3Smrg        pool->dev = dev;
557ec681f3Smrg        pool->create_flags = create_flags;
567ec681f3Smrg        pool->slab_size = slab_size;
577ec681f3Smrg        pool->label = label;
587ec681f3Smrg}
597ec681f3Smrg
607ec681f3Smrg/* Represents a fat pointer for GPU-mapped memory, returned from the transient
617ec681f3Smrg * allocator and not used for much else */
627ec681f3Smrg
637ec681f3Smrgstruct panfrost_ptr
647ec681f3Smrgpan_pool_alloc_aligned(struct pan_pool *pool, size_t sz, unsigned alignment);
657ec681f3Smrg
667ec681f3Smrg#define PAN_POOL_ALLOCATOR(pool_subclass, alloc_func) \
677ec681f3Smrgstruct panfrost_ptr \
687ec681f3Smrgpan_pool_alloc_aligned(struct pan_pool *p, size_t sz, unsigned alignment) \
697ec681f3Smrg{ \
707ec681f3Smrg        pool_subclass *pool = container_of(p, pool_subclass, base); \
717ec681f3Smrg        return alloc_func(pool, sz, alignment); \
727ec681f3Smrg}
737ec681f3Smrg
747ec681f3Smrgstatic inline mali_ptr
757ec681f3Smrgpan_pool_upload_aligned(struct pan_pool *pool, const void *data, size_t sz, unsigned alignment)
767ec681f3Smrg{
777ec681f3Smrg        struct panfrost_ptr transfer = pan_pool_alloc_aligned(pool, sz, alignment);
787ec681f3Smrg        memcpy(transfer.cpu, data, sz);
797ec681f3Smrg        return transfer.gpu;
807ec681f3Smrg}
817ec681f3Smrg
827ec681f3Smrgstatic inline mali_ptr
837ec681f3Smrgpan_pool_upload(struct pan_pool *pool, const void *data, size_t sz)
847ec681f3Smrg{
857ec681f3Smrg        return pan_pool_upload_aligned(pool, data, sz, sz);
867ec681f3Smrg}
877ec681f3Smrg
887ec681f3Smrgstruct pan_desc_alloc_info {
897ec681f3Smrg        unsigned size;
907ec681f3Smrg        unsigned align;
917ec681f3Smrg        unsigned nelems;
927ec681f3Smrg};
937ec681f3Smrg
947ec681f3Smrg#define PAN_DESC_ARRAY(count, name) \
957ec681f3Smrg        { \
967ec681f3Smrg                .size = pan_size(name), \
977ec681f3Smrg                .align = pan_alignment(name), \
987ec681f3Smrg                .nelems = count, \
997ec681f3Smrg        }
1007ec681f3Smrg
1017ec681f3Smrg#define PAN_DESC(name) PAN_DESC_ARRAY(1, name)
1027ec681f3Smrg
1037ec681f3Smrg#define PAN_DESC_AGGREGATE(...) \
1047ec681f3Smrg        (struct pan_desc_alloc_info[]) { \
1057ec681f3Smrg                __VA_ARGS__, \
1067ec681f3Smrg                { 0 }, \
1077ec681f3Smrg        }
1087ec681f3Smrg
1097ec681f3Smrgstatic inline struct panfrost_ptr
1107ec681f3Smrgpan_pool_alloc_descs(struct pan_pool *pool,
1117ec681f3Smrg                     const struct pan_desc_alloc_info *descs)
1127ec681f3Smrg{
1137ec681f3Smrg        unsigned size = 0;
1147ec681f3Smrg        unsigned align = descs[0].align;
1157ec681f3Smrg
1167ec681f3Smrg        for (unsigned i = 0; descs[i].size; i++) {
1177ec681f3Smrg                assert(!(size & (descs[i].align - 1)));
1187ec681f3Smrg                size += descs[i].size * descs[i].nelems;
1197ec681f3Smrg        }
1207ec681f3Smrg
1217ec681f3Smrg        return pan_pool_alloc_aligned(pool, size, align);
1227ec681f3Smrg}
1237ec681f3Smrg
1247ec681f3Smrg#define pan_pool_alloc_desc(pool, name) \
1257ec681f3Smrg        pan_pool_alloc_descs(pool, PAN_DESC_AGGREGATE(PAN_DESC(name)))
1267ec681f3Smrg
1277ec681f3Smrg#define pan_pool_alloc_desc_array(pool, count, name) \
1287ec681f3Smrg        pan_pool_alloc_descs(pool, PAN_DESC_AGGREGATE(PAN_DESC_ARRAY(count, name)))
1297ec681f3Smrg
1307ec681f3Smrg#define pan_pool_alloc_desc_aggregate(pool, ...) \
1317ec681f3Smrg        pan_pool_alloc_descs(pool, PAN_DESC_AGGREGATE(__VA_ARGS__))
1327ec681f3Smrg
1337ec681f3Smrg#endif
134