panfrost: Allocate tiler and scratchpad BOs per-batch
[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 bo = panfrost_batch_create_bo(batch, bo_sz, 0);
68
69 if (sz < TRANSIENT_SLAB_SIZE) {
70 batch->transient_bo = bo;
71 batch->transient_offset = offset + sz;
72 }
73 }
74
75 struct panfrost_transfer ret = {
76 .cpu = bo->cpu + offset,
77 .gpu = bo->gpu + offset,
78 };
79
80 return ret;
81
82 }
83
84 mali_ptr
85 panfrost_upload_transient(struct panfrost_batch *batch, const void *data,
86 size_t sz)
87 {
88 struct panfrost_transfer transfer = panfrost_allocate_transient(batch, sz);
89 memcpy(transfer.cpu, data, sz);
90 return transfer.gpu;
91 }