1b8e80941Smrg/*
2b8e80941Smrg * © Copyright 2017-2018 Alyssa Rosenzweig
3b8e80941Smrg *
4b8e80941Smrg * Permission is hereby granted, free of charge, to any person obtaining a
5b8e80941Smrg * copy of this software and associated documentation files (the "Software"),
6b8e80941Smrg * to deal in the Software without restriction, including without limitation
7b8e80941Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8b8e80941Smrg * and/or sell copies of the Software, and to permit persons to whom the
9b8e80941Smrg * Software is furnished to do so, subject to the following conditions:
10b8e80941Smrg *
11b8e80941Smrg * The above copyright notice and this permission notice (including the next
12b8e80941Smrg * paragraph) shall be included in all copies or substantial portions of the
13b8e80941Smrg * Software.
14b8e80941Smrg *
15b8e80941Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16b8e80941Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17b8e80941Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18b8e80941Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19b8e80941Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20b8e80941Smrg * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21b8e80941Smrg * SOFTWARE.
22b8e80941Smrg *
23b8e80941Smrg */
24b8e80941Smrg
25b8e80941Smrg#ifndef __PAN_ALLOCATE_H__
26b8e80941Smrg#define __PAN_ALLOCATE_H__
27b8e80941Smrg
28b8e80941Smrg#include <unistd.h>
29b8e80941Smrg#include <sys/mman.h>
30b8e80941Smrg#include <stdbool.h>
31b8e80941Smrg#include "pipebuffer/pb_slab.h"
32b8e80941Smrg
33b8e80941Smrg#include <panfrost-misc.h>
34b8e80941Smrg
35b8e80941Smrgstruct panfrost_context;
36b8e80941Smrg
37b8e80941Smrg/* Texture memory */
38b8e80941Smrg
39b8e80941Smrg#define HEAP_TEXTURE 0
40b8e80941Smrg
41b8e80941Smrg/* Single-frame (transient) command stream memory, done at the block scale
42b8e80941Smrg * rather than the individual cmdstream alllocation scale. We use pb_alloc for
43b8e80941Smrg * pooling, but we have to implement our own logic atop the API for performance
44b8e80941Smrg * reasons when considering many low-latency tiny heterogenous allocations */
45b8e80941Smrg
46b8e80941Smrg#define HEAP_TRANSIENT 1
47b8e80941Smrg
48b8e80941Smrg/* Multi-frame descriptor memory (replaces what used to be
49b8e80941Smrg * cmdstream_persistent), for long-living small allocations */
50b8e80941Smrg
51b8e80941Smrg#define HEAP_DESCRIPTOR 2
52b8e80941Smrg
53b8e80941Smrg/* Represents a fat pointer for GPU-mapped memory, returned from the transient
54b8e80941Smrg * allocator and not used for much else */
55b8e80941Smrg
56b8e80941Smrgstruct panfrost_transfer {
57b8e80941Smrg        uint8_t *cpu;
58b8e80941Smrg        mali_ptr gpu;
59b8e80941Smrg};
60b8e80941Smrg
61b8e80941Smrgstruct panfrost_memory {
62b8e80941Smrg        /* Subclassing slab object */
63b8e80941Smrg        struct pb_slab slab;
64b8e80941Smrg
65b8e80941Smrg        /* Backing for the slab in memory */
66b8e80941Smrg        uint8_t *cpu;
67b8e80941Smrg        mali_ptr gpu;
68b8e80941Smrg        int stack_bottom;
69b8e80941Smrg        size_t size;
70b8e80941Smrg        int gem_handle;
71b8e80941Smrg};
72b8e80941Smrg
73b8e80941Smrg/* Slab entry sizes range from 2^min to 2^max. In this case, we range from 1k
74b8e80941Smrg * to 16MB. Numbers are kind of arbitrary but these seem to work alright in
75b8e80941Smrg * practice. */
76b8e80941Smrg
77b8e80941Smrg#define MIN_SLAB_ENTRY_SIZE (10)
78b8e80941Smrg#define MAX_SLAB_ENTRY_SIZE (24)
79b8e80941Smrg
80b8e80941Smrgstruct panfrost_memory_entry {
81b8e80941Smrg        /* Subclass */
82b8e80941Smrg        struct pb_slab_entry base;
83b8e80941Smrg
84b8e80941Smrg        /* Have we been freed? */
85b8e80941Smrg        bool freed;
86b8e80941Smrg
87b8e80941Smrg        /* Offset into the slab of the entry */
88b8e80941Smrg        off_t offset;
89b8e80941Smrg};
90b8e80941Smrg
91b8e80941Smrg/* Functions for replay */
92b8e80941Smrgmali_ptr pandev_upload(int cheating_offset, int *stack_bottom, mali_ptr base, void *base_map, const void *data, size_t sz, bool no_pad);
93b8e80941Smrgmali_ptr pandev_upload_sequential(mali_ptr base, void *base_map, const void *data, size_t sz);
94b8e80941Smrg
95b8e80941Smrg/* Functions for the actual Galliumish driver */
96b8e80941Smrgmali_ptr panfrost_upload(struct panfrost_memory *mem, const void *data, size_t sz, bool no_pad);
97b8e80941Smrgmali_ptr panfrost_upload_sequential(struct panfrost_memory *mem, const void *data, size_t sz);
98b8e80941Smrg
99b8e80941Smrgstruct panfrost_transfer
100b8e80941Smrgpanfrost_allocate_transient(struct panfrost_context *ctx, size_t sz);
101b8e80941Smrg
102b8e80941Smrgmali_ptr
103b8e80941Smrgpanfrost_upload_transient(struct panfrost_context *ctx, const void *data, size_t sz);
104b8e80941Smrg
105b8e80941Smrgvoid *
106b8e80941Smrgpanfrost_allocate_transfer(struct panfrost_memory *mem, size_t sz, mali_ptr *gpu);
107b8e80941Smrg
108b8e80941Smrgstatic inline mali_ptr
109b8e80941Smrgpanfrost_reserve(struct panfrost_memory *mem, size_t sz)
110b8e80941Smrg{
111b8e80941Smrg        mem->stack_bottom += sz;
112b8e80941Smrg        return mem->gpu + (mem->stack_bottom - sz);
113b8e80941Smrg}
114b8e80941Smrg
115b8e80941Smrgstruct panfrost_transfer
116b8e80941Smrgpanfrost_allocate_chunk(struct panfrost_context *ctx, size_t size, unsigned heap_id);
117b8e80941Smrg
118b8e80941Smrg#include <math.h>
119b8e80941Smrg#define inff INFINITY
120b8e80941Smrg
121b8e80941Smrg#define R(...) #__VA_ARGS__
122b8e80941Smrg#define ALIGN(x, y) (((x) + ((y) - 1)) & ~((y) - 1))
123b8e80941Smrg
124b8e80941Smrg#endif /* __PAN_ALLOCATE_H__ */
125