101e04c3fSmrg/*
201e04c3fSmrg * Copyright © 2014-2017 Broadcom
301e04c3fSmrg *
401e04c3fSmrg * Permission is hereby granted, free of charge, to any person obtaining a
501e04c3fSmrg * copy of this software and associated documentation files (the "Software"),
601e04c3fSmrg * to deal in the Software without restriction, including without limitation
701e04c3fSmrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
801e04c3fSmrg * and/or sell copies of the Software, and to permit persons to whom the
901e04c3fSmrg * Software is furnished to do so, subject to the following conditions:
1001e04c3fSmrg *
1101e04c3fSmrg * The above copyright notice and this permission notice (including the next
1201e04c3fSmrg * paragraph) shall be included in all copies or substantial portions of the
1301e04c3fSmrg * Software.
1401e04c3fSmrg *
1501e04c3fSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1601e04c3fSmrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1701e04c3fSmrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
1801e04c3fSmrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1901e04c3fSmrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
2001e04c3fSmrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
2101e04c3fSmrg * IN THE SOFTWARE.
2201e04c3fSmrg */
2301e04c3fSmrg
2401e04c3fSmrg#include "util/u_math.h"
2501e04c3fSmrg#include "util/ralloc.h"
2601e04c3fSmrg#include "v3d_context.h"
277ec681f3Smrg/* We don't expect that the packets we use in this file change across across
287ec681f3Smrg * hw versions, so we just explicitly set the V3D_VERSION and include
297ec681f3Smrg * v3dx_pack here
307ec681f3Smrg */
3101e04c3fSmrg#define V3D_VERSION 33
3201e04c3fSmrg#include "broadcom/common/v3d_macros.h"
3301e04c3fSmrg#include "broadcom/cle/v3dx_pack.h"
3401e04c3fSmrg
3501e04c3fSmrgvoid
3601e04c3fSmrgv3d_init_cl(struct v3d_job *job, struct v3d_cl *cl)
3701e04c3fSmrg{
3801e04c3fSmrg        cl->base = NULL;
3901e04c3fSmrg        cl->next = cl->base;
4001e04c3fSmrg        cl->size = 0;
4101e04c3fSmrg        cl->job = job;
4201e04c3fSmrg}
4301e04c3fSmrg
4401e04c3fSmrguint32_t
4501e04c3fSmrgv3d_cl_ensure_space(struct v3d_cl *cl, uint32_t space, uint32_t alignment)
4601e04c3fSmrg{
4701e04c3fSmrg        uint32_t offset = align(cl_offset(cl), alignment);
4801e04c3fSmrg
4901e04c3fSmrg        if (offset + space <= cl->size) {
5001e04c3fSmrg                cl->next = cl->base + offset;
5101e04c3fSmrg                return offset;
5201e04c3fSmrg        }
5301e04c3fSmrg
5401e04c3fSmrg        v3d_bo_unreference(&cl->bo);
5501e04c3fSmrg        cl->bo = v3d_bo_alloc(cl->job->v3d->screen, align(space, 4096), "CL");
5601e04c3fSmrg        cl->base = v3d_bo_map(cl->bo);
5701e04c3fSmrg        cl->size = cl->bo->size;
5801e04c3fSmrg        cl->next = cl->base;
5901e04c3fSmrg
6001e04c3fSmrg        return 0;
6101e04c3fSmrg}
6201e04c3fSmrg
6301e04c3fSmrgvoid
6401e04c3fSmrgv3d_cl_ensure_space_with_branch(struct v3d_cl *cl, uint32_t space)
6501e04c3fSmrg{
6601e04c3fSmrg        if (cl_offset(cl) + space + cl_packet_length(BRANCH) <= cl->size)
6701e04c3fSmrg                return;
6801e04c3fSmrg
697ec681f3Smrg        struct v3d_bo *new_bo = v3d_bo_alloc(cl->job->v3d->screen, space, "CL");
7001e04c3fSmrg        assert(space <= new_bo->size);
7101e04c3fSmrg
7201e04c3fSmrg        /* Chain to the new BO from the old one. */
7301e04c3fSmrg        if (cl->bo) {
7401e04c3fSmrg                cl_emit(cl, BRANCH, branch) {
7501e04c3fSmrg                        branch.address = cl_address(new_bo, 0);
7601e04c3fSmrg                }
7701e04c3fSmrg                v3d_bo_unreference(&cl->bo);
7801e04c3fSmrg        } else {
7901e04c3fSmrg                /* Root the first RCL/BCL BO in the job. */
807ec681f3Smrg                v3d_job_add_bo(cl->job, new_bo);
8101e04c3fSmrg        }
8201e04c3fSmrg
8301e04c3fSmrg        cl->bo = new_bo;
8401e04c3fSmrg        cl->base = v3d_bo_map(cl->bo);
8501e04c3fSmrg        cl->size = cl->bo->size;
8601e04c3fSmrg        cl->next = cl->base;
8701e04c3fSmrg}
8801e04c3fSmrg
8901e04c3fSmrgvoid
9001e04c3fSmrgv3d_destroy_cl(struct v3d_cl *cl)
9101e04c3fSmrg{
9201e04c3fSmrg        v3d_bo_unreference(&cl->bo);
9301e04c3fSmrg}
94