b16a1253ac2f5e5c2cd93fe4b28bc3e414b683ec
[mesa.git] / src / gallium / drivers / panfrost / pan_allocate.c
1 /*
2 * © Copyright 2018 Alyssa Rosenzweig
3 * Copyright (C) 2019 Collabora, Ltd.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 */
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <assert.h>
30 #include <panfrost-misc.h>
31 #include <panfrost-job.h>
32 #include "pan_bo.h"
33 #include "pan_context.h"
34
35 /* TODO: What does this actually have to be? */
36 #define ALIGNMENT 128
37
38 /* Transient command stream pooling: command stream uploads try to simply copy
39 * into whereever we left off. If there isn't space, we allocate a new entry
40 * into the pool and copy there */
41
42 struct panfrost_transfer
43 panfrost_allocate_transient(struct panfrost_batch *batch, size_t sz)
44 {
45 /* Pad the size */
46 sz = ALIGN_POT(sz, ALIGNMENT);
47
48 /* Find or create a suitable BO */
49 struct panfrost_bo *bo = NULL;
50
51 unsigned offset = 0;
52
53 bool fits_in_current = (batch->transient_offset + sz) < TRANSIENT_SLAB_SIZE;
54
55 if (likely(batch->transient_bo && fits_in_current)) {
56 /* We can reuse the current BO, so get it */
57 bo = batch->transient_bo;
58
59 /* Use the specified offset */
60 offset = batch->transient_offset;
61 batch->transient_offset = offset + sz;
62 } else {
63 size_t bo_sz = sz < TRANSIENT_SLAB_SIZE ?
64 TRANSIENT_SLAB_SIZE : ALIGN_POT(sz, 4096);
65
66 /* We can't reuse the current BO, but we can create a new one.
67 * We don't know what the BO will be used for, so let's flag it
68 * RW and attach it to both the fragment and vertex/tiler jobs.
69 * TODO: if we want fine grained BO assignment we should pass
70 * flags to this function and keep the read/write,
71 * fragment/vertex+tiler pools separate.
72 */
73 bo = panfrost_batch_create_bo(batch, bo_sz, 0,
74 PAN_BO_ACCESS_PRIVATE |
75 PAN_BO_ACCESS_RW |
76 PAN_BO_ACCESS_VERTEX_TILER |
77 PAN_BO_ACCESS_FRAGMENT);
78
79 if (sz < TRANSIENT_SLAB_SIZE) {
80 batch->transient_bo = bo;
81 batch->transient_offset = offset + sz;
82 }
83 }
84
85 struct panfrost_transfer ret = {
86 .cpu = bo->cpu + offset,
87 .gpu = bo->gpu + offset,
88 };
89
90 return ret;
91
92 }
93
94 mali_ptr
95 panfrost_upload_transient(struct panfrost_batch *batch, const void *data,
96 size_t sz)
97 {
98 struct panfrost_transfer transfer = panfrost_allocate_transient(batch, sz);
99 memcpy(transfer.cpu, data, sz);
100 return transfer.gpu;
101 }