1af69d88dSmrg/*
2af69d88dSmrg * Copyright © 2014 Broadcom
3af69d88dSmrg *
4af69d88dSmrg * Permission is hereby granted, free of charge, to any person obtaining a
5af69d88dSmrg * copy of this software and associated documentation files (the "Software"),
6af69d88dSmrg * to deal in the Software without restriction, including without limitation
7af69d88dSmrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8af69d88dSmrg * and/or sell copies of the Software, and to permit persons to whom the
9af69d88dSmrg * Software is furnished to do so, subject to the following conditions:
10af69d88dSmrg *
11af69d88dSmrg * The above copyright notice and this permission notice (including the next
12af69d88dSmrg * paragraph) shall be included in all copies or substantial portions of the
13af69d88dSmrg * Software.
14af69d88dSmrg *
15af69d88dSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16af69d88dSmrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17af69d88dSmrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18af69d88dSmrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19af69d88dSmrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20af69d88dSmrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21af69d88dSmrg * IN THE SOFTWARE.
22af69d88dSmrg */
23af69d88dSmrg
24af69d88dSmrg#include "util/u_math.h"
2501e04c3fSmrg#include "util/ralloc.h"
26af69d88dSmrg#include "vc4_context.h"
27af69d88dSmrg
28af69d88dSmrgvoid
2901e04c3fSmrgvc4_init_cl(struct vc4_job *job, struct vc4_cl *cl)
30af69d88dSmrg{
3101e04c3fSmrg        cl->base = rzalloc_size(job, 1); /* TODO: don't use rzalloc */
3201e04c3fSmrg        cl->next = cl->base;
3301e04c3fSmrg        cl->size = 0;
3401e04c3fSmrg        cl->job = job;
35af69d88dSmrg}
36af69d88dSmrg
37af69d88dSmrgvoid
3801e04c3fSmrgcl_ensure_space(struct vc4_cl *cl, uint32_t space)
39af69d88dSmrg{
4001e04c3fSmrg        uint32_t offset = cl_offset(cl);
4101e04c3fSmrg
4201e04c3fSmrg        if (offset + space <= cl->size)
4301e04c3fSmrg                return;
44af69d88dSmrg
4501e04c3fSmrg        uint32_t size = MAX2(cl->size + space, cl->size * 2);
4601e04c3fSmrg
4701e04c3fSmrg        cl->base = reralloc(ralloc_parent(cl->base), cl->base, uint8_t, size);
4801e04c3fSmrg        cl->size = size;
49af69d88dSmrg        cl->next = cl->base + offset;
50af69d88dSmrg}
51af69d88dSmrg
52af69d88dSmrgvoid
53af69d88dSmrgvc4_reset_cl(struct vc4_cl *cl)
54af69d88dSmrg{
55af69d88dSmrg        assert(cl->reloc_count == 0);
56af69d88dSmrg        cl->next = cl->base;
57af69d88dSmrg}
58af69d88dSmrg
59af69d88dSmrguint32_t
6001e04c3fSmrgvc4_gem_hindex(struct vc4_job *job, struct vc4_bo *bo)
61af69d88dSmrg{
62af69d88dSmrg        uint32_t hindex;
6301e04c3fSmrg        uint32_t *current_handles = job->bo_handles.base;
6401e04c3fSmrg        uint32_t cl_hindex_count = cl_offset(&job->bo_handles) / 4;
6501e04c3fSmrg        uint32_t last_hindex = bo->last_hindex; /* volatile read! */
6601e04c3fSmrg
6701e04c3fSmrg        if (last_hindex < cl_hindex_count &&
6801e04c3fSmrg            current_handles[last_hindex] == bo->handle) {
6901e04c3fSmrg                return last_hindex;
7001e04c3fSmrg        }
71af69d88dSmrg
7201e04c3fSmrg        for (hindex = 0; hindex < cl_hindex_count; hindex++) {
7301e04c3fSmrg                if (current_handles[hindex] == bo->handle) {
7401e04c3fSmrg                        bo->last_hindex = hindex;
75af69d88dSmrg                        return hindex;
7601e04c3fSmrg                }
77af69d88dSmrg        }
78af69d88dSmrg
7901e04c3fSmrg        struct vc4_cl_out *out;
8001e04c3fSmrg
8101e04c3fSmrg        out = cl_start(&job->bo_handles);
8201e04c3fSmrg        cl_u32(&out, bo->handle);
8301e04c3fSmrg        cl_end(&job->bo_handles, out);
8401e04c3fSmrg
8501e04c3fSmrg        out = cl_start(&job->bo_pointers);
8601e04c3fSmrg        cl_ptr(&out, vc4_bo_reference(bo));
8701e04c3fSmrg        cl_end(&job->bo_pointers, out);
8801e04c3fSmrg
8901e04c3fSmrg        job->bo_space += bo->size;
90af69d88dSmrg
9101e04c3fSmrg        bo->last_hindex = hindex;
92af69d88dSmrg        return hindex;
93af69d88dSmrg}
94