1 /*
2  * Copyright © 2019 Raspberry Pi
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 "v3dv_private.h"
25 #include "broadcom/cle/v3dx_pack.h"
26 
27 void
v3dv_cl_init(struct v3dv_job * job,struct v3dv_cl * cl)28 v3dv_cl_init(struct v3dv_job *job, struct v3dv_cl *cl)
29 {
30    cl->base = NULL;
31    cl->next = cl->base;
32    cl->bo = NULL;
33    cl->size = 0;
34    cl->job = job;
35    list_inithead(&cl->bo_list);
36 }
37 
38 void
v3dv_cl_destroy(struct v3dv_cl * cl)39 v3dv_cl_destroy(struct v3dv_cl *cl)
40 {
41    list_for_each_entry_safe(struct v3dv_bo, bo, &cl->bo_list, list_link) {
42       assert(cl->job);
43       list_del(&bo->list_link);
44       v3dv_bo_free(cl->job->device, bo);
45    }
46 
47    /* Leave the CL in a reset state to catch use after destroy instances */
48    v3dv_cl_init(NULL, cl);
49 }
50 
51 static bool
cl_alloc_bo(struct v3dv_cl * cl,uint32_t space,bool use_branch)52 cl_alloc_bo(struct v3dv_cl *cl, uint32_t space, bool use_branch)
53 {
54    struct v3dv_bo *bo = v3dv_bo_alloc(cl->job->device, space, "CL", true);
55    if (!bo) {
56       fprintf(stderr, "failed to allocate memory for command list\n");
57       v3dv_flag_oom(NULL, cl->job);
58       return false;
59    }
60 
61    list_addtail(&bo->list_link, &cl->bo_list);
62 
63    bool ok = v3dv_bo_map(cl->job->device, bo, bo->size);
64    if (!ok) {
65       fprintf(stderr, "failed to map command list buffer\n");
66       v3dv_flag_oom(NULL, cl->job);
67       return false;
68    }
69 
70    /* Chain to the new BO from the old one if requested */
71    if (use_branch && cl->bo) {
72       cl_emit(cl, BRANCH, branch) {
73          branch.address = v3dv_cl_address(bo, 0);
74       }
75    }
76 
77    v3dv_job_add_bo(cl->job, bo);
78 
79    cl->bo = bo;
80    cl->base = cl->bo->map;
81    cl->size = cl->bo->size;
82    cl->next = cl->base;
83 
84    return true;
85 }
86 
87 uint32_t
v3dv_cl_ensure_space(struct v3dv_cl * cl,uint32_t space,uint32_t alignment)88 v3dv_cl_ensure_space(struct v3dv_cl *cl, uint32_t space, uint32_t alignment)
89 {
90    uint32_t offset = align(v3dv_cl_offset(cl), alignment);
91 
92    if (offset + space <= cl->size) {
93       cl->next = cl->base + offset;
94       return offset;
95    }
96 
97    cl_alloc_bo(cl, space, false);
98    return 0;
99 }
100 
101 void
v3dv_cl_ensure_space_with_branch(struct v3dv_cl * cl,uint32_t space)102 v3dv_cl_ensure_space_with_branch(struct v3dv_cl *cl, uint32_t space)
103 {
104    if (v3dv_cl_offset(cl) + space + cl_packet_length(BRANCH) <= cl->size)
105       return;
106 
107    cl_alloc_bo(cl, space, true);
108 }
109