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