1848b8605Smrg/*
2848b8605Smrg * Copyright © 2014 Broadcom
3848b8605Smrg *
4848b8605Smrg * Permission is hereby granted, free of charge, to any person obtaining a
5848b8605Smrg * copy of this software and associated documentation files (the "Software"),
6848b8605Smrg * to deal in the Software without restriction, including without limitation
7848b8605Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8848b8605Smrg * and/or sell copies of the Software, and to permit persons to whom the
9848b8605Smrg * Software is furnished to do so, subject to the following conditions:
10848b8605Smrg *
11848b8605Smrg * The above copyright notice and this permission notice (including the next
12848b8605Smrg * paragraph) shall be included in all copies or substantial portions of the
13848b8605Smrg * Software.
14848b8605Smrg *
15848b8605Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16848b8605Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17848b8605Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18848b8605Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19848b8605Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20848b8605Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21848b8605Smrg * IN THE SOFTWARE.
22848b8605Smrg */
23848b8605Smrg
24848b8605Smrg#include "util/u_math.h"
25b8e80941Smrg#include "util/ralloc.h"
26848b8605Smrg#include "vc4_context.h"
27848b8605Smrg
28848b8605Smrgvoid
29b8e80941Smrgvc4_init_cl(struct vc4_job *job, struct vc4_cl *cl)
30848b8605Smrg{
31b8e80941Smrg        cl->base = rzalloc_size(job, 1); /* TODO: don't use rzalloc */
32b8e80941Smrg        cl->next = cl->base;
33b8e80941Smrg        cl->size = 0;
34b8e80941Smrg        cl->job = job;
35848b8605Smrg}
36848b8605Smrg
37848b8605Smrgvoid
38b8e80941Smrgcl_ensure_space(struct vc4_cl *cl, uint32_t space)
39848b8605Smrg{
40b8e80941Smrg        uint32_t offset = cl_offset(cl);
41b8e80941Smrg
42b8e80941Smrg        if (offset + space <= cl->size)
43b8e80941Smrg                return;
44848b8605Smrg
45b8e80941Smrg        uint32_t size = MAX2(cl->size + space, cl->size * 2);
46b8e80941Smrg
47b8e80941Smrg        cl->base = reralloc(ralloc_parent(cl->base), cl->base, uint8_t, size);
48b8e80941Smrg        cl->size = size;
49848b8605Smrg        cl->next = cl->base + offset;
50848b8605Smrg}
51848b8605Smrg
52848b8605Smrgvoid
53848b8605Smrgvc4_reset_cl(struct vc4_cl *cl)
54848b8605Smrg{
55848b8605Smrg        assert(cl->reloc_count == 0);
56848b8605Smrg        cl->next = cl->base;
57848b8605Smrg}
58848b8605Smrg
59848b8605Smrguint32_t
60b8e80941Smrgvc4_gem_hindex(struct vc4_job *job, struct vc4_bo *bo)
61848b8605Smrg{
62848b8605Smrg        uint32_t hindex;
63b8e80941Smrg        uint32_t *current_handles = job->bo_handles.base;
64b8e80941Smrg        uint32_t cl_hindex_count = cl_offset(&job->bo_handles) / 4;
65b8e80941Smrg        uint32_t last_hindex = bo->last_hindex; /* volatile read! */
66b8e80941Smrg
67b8e80941Smrg        if (last_hindex < cl_hindex_count &&
68b8e80941Smrg            current_handles[last_hindex] == bo->handle) {
69b8e80941Smrg                return last_hindex;
70b8e80941Smrg        }
71848b8605Smrg
72b8e80941Smrg        for (hindex = 0; hindex < cl_hindex_count; hindex++) {
73b8e80941Smrg                if (current_handles[hindex] == bo->handle) {
74b8e80941Smrg                        bo->last_hindex = hindex;
75848b8605Smrg                        return hindex;
76b8e80941Smrg                }
77848b8605Smrg        }
78848b8605Smrg
79b8e80941Smrg        struct vc4_cl_out *out;
80b8e80941Smrg
81b8e80941Smrg        out = cl_start(&job->bo_handles);
82b8e80941Smrg        cl_u32(&out, bo->handle);
83b8e80941Smrg        cl_end(&job->bo_handles, out);
84b8e80941Smrg
85b8e80941Smrg        out = cl_start(&job->bo_pointers);
86b8e80941Smrg        cl_ptr(&out, vc4_bo_reference(bo));
87b8e80941Smrg        cl_end(&job->bo_pointers, out);
88b8e80941Smrg
89b8e80941Smrg        job->bo_space += bo->size;
90848b8605Smrg
91b8e80941Smrg        bo->last_hindex = hindex;
92848b8605Smrg        return hindex;
93848b8605Smrg}
94