1/*
2 * Copyright © 2014-2017 Broadcom
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24#include "util/u_math.h"
25#include "util/ralloc.h"
26#include "v3d_context.h"
27/* We don't expect that the packets we use in this file change across across
28 * hw versions, so we just explicitly set the V3D_VERSION and include
29 * v3dx_pack here
30 */
31#define V3D_VERSION 33
32#include "broadcom/common/v3d_macros.h"
33#include "broadcom/cle/v3dx_pack.h"
34
35void
36v3d_init_cl(struct v3d_job *job, struct v3d_cl *cl)
37{
38        cl->base = NULL;
39        cl->next = cl->base;
40        cl->size = 0;
41        cl->job = job;
42}
43
44uint32_t
45v3d_cl_ensure_space(struct v3d_cl *cl, uint32_t space, uint32_t alignment)
46{
47        uint32_t offset = align(cl_offset(cl), alignment);
48
49        if (offset + space <= cl->size) {
50                cl->next = cl->base + offset;
51                return offset;
52        }
53
54        v3d_bo_unreference(&cl->bo);
55        cl->bo = v3d_bo_alloc(cl->job->v3d->screen, align(space, 4096), "CL");
56        cl->base = v3d_bo_map(cl->bo);
57        cl->size = cl->bo->size;
58        cl->next = cl->base;
59
60        return 0;
61}
62
63void
64v3d_cl_ensure_space_with_branch(struct v3d_cl *cl, uint32_t space)
65{
66        if (cl_offset(cl) + space + cl_packet_length(BRANCH) <= cl->size)
67                return;
68
69        struct v3d_bo *new_bo = v3d_bo_alloc(cl->job->v3d->screen, space, "CL");
70        assert(space <= new_bo->size);
71
72        /* Chain to the new BO from the old one. */
73        if (cl->bo) {
74                cl_emit(cl, BRANCH, branch) {
75                        branch.address = cl_address(new_bo, 0);
76                }
77                v3d_bo_unreference(&cl->bo);
78        } else {
79                /* Root the first RCL/BCL BO in the job. */
80                v3d_job_add_bo(cl->job, new_bo);
81        }
82
83        cl->bo = new_bo;
84        cl->base = v3d_bo_map(cl->bo);
85        cl->size = cl->bo->size;
86        cl->next = cl->base;
87}
88
89void
90v3d_destroy_cl(struct v3d_cl *cl)
91{
92        v3d_bo_unreference(&cl->bo);
93}
94