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 42
32 #include "broadcom/common/v3d_macros.h"
33 #include "broadcom/cle/v3dx_pack.h"
34
35 void
v3d_init_cl(struct v3d_job * job,struct v3d_cl * cl)36 v3d_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
44 uint32_t
v3d_cl_ensure_space(struct v3d_cl * cl,uint32_t space,uint32_t alignment)45 v3d_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 struct v3d_device_info *devinfo = &cl->job->v3d->screen->devinfo;
54 v3d_bo_unreference(&cl->bo);
55 cl->bo = v3d_bo_alloc(cl->job->v3d->screen,
56 align(space, devinfo->cle_buffer_min_size),
57 "CL");
58 cl->base = v3d_bo_map(cl->bo);
59 cl->size = cl->bo->size;
60 cl->next = cl->base;
61
62 return 0;
63 }
64
65 void
v3d_cl_ensure_space_with_branch(struct v3d_cl * cl,uint32_t space)66 v3d_cl_ensure_space_with_branch(struct v3d_cl *cl, uint32_t space)
67 {
68 if (cl_offset(cl) + space <= cl->size)
69 return;
70
71 /* The last V3D_CLE_READAHEAD bytes of the buffer are unusable, so we
72 * need to take them into account when allocating a new BO for the
73 * CL. We have to be sure that we have room for a BRANCH packet so we
74 * can always chain a next BO if needed. We will need to increase
75 * cl->size by the packet length before calling cl_summit to use this
76 * reserved space.
77 */
78 struct v3d_device_info *devinfo = &cl->job->v3d->screen->devinfo;
79 uint32_t unusable_size = devinfo->cle_readahead + cl_packet_length(BRANCH);
80 struct v3d_bo *new_bo = v3d_bo_alloc(cl->job->v3d->screen,
81 align(space + unusable_size,
82 devinfo->cle_buffer_min_size),
83 "CL");
84 assert(space + unusable_size <= new_bo->size);
85
86 /* Chain to the new BO from the old one. */
87 if (cl->bo) {
88 cl->size += cl_packet_length(BRANCH);
89 assert(cl->size + devinfo->cle_readahead <= cl->bo->size);
90 cl_emit(cl, BRANCH, branch) {
91 branch.address = cl_address(new_bo, 0);
92 }
93 v3d_bo_unreference(&cl->bo);
94 } else {
95 /* Root the first RCL/BCL BO in the job. */
96 v3d_job_add_bo(cl->job, new_bo);
97 }
98
99 cl->bo = new_bo;
100 cl->base = v3d_bo_map(cl->bo);
101 /* Take only into account the usable size of the BO to guarantee that
102 * we never write in the last bytes of the CL buffer because of the
103 * readahead of the CLE
104 */
105 cl->size = cl->bo->size - unusable_size;
106 cl->next = cl->base;
107 }
108
109 void
v3d_destroy_cl(struct v3d_cl * cl)110 v3d_destroy_cl(struct v3d_cl *cl)
111 {
112 v3d_bo_unreference(&cl->bo);
113 }
114