1 /*
2 * Copyright © 2022 Imagination Technologies Ltd.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * 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 THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24 #include <fcntl.h>
25 #include <limits.h>
26 #include <stddef.h>
27 #include <stdint.h>
28 #include <unistd.h>
29 #include <vulkan/vulkan.h>
30
31 #include "fw-api/pvr_rogue_fwif.h"
32 #include "fw-api/pvr_rogue_fwif_rf.h"
33 #include "pvr_private.h"
34 #include "pvr_srv.h"
35 #include "pvr_srv_bridge.h"
36 #include "pvr_srv_job_common.h"
37 #include "pvr_srv_job_compute.h"
38 #include "pvr_srv_sync.h"
39 #include "pvr_winsys.h"
40 #include "util/libsync.h"
41 #include "util/macros.h"
42 #include "vk_alloc.h"
43 #include "vk_log.h"
44
45 struct pvr_srv_winsys_compute_ctx {
46 struct pvr_winsys_compute_ctx base;
47
48 void *handle;
49
50 int timeline;
51 };
52
53 #define to_pvr_srv_winsys_compute_ctx(ctx) \
54 container_of(ctx, struct pvr_srv_winsys_compute_ctx, base)
55
pvr_srv_winsys_compute_ctx_create(struct pvr_winsys * ws,const struct pvr_winsys_compute_ctx_create_info * create_info,struct pvr_winsys_compute_ctx ** const ctx_out)56 VkResult pvr_srv_winsys_compute_ctx_create(
57 struct pvr_winsys *ws,
58 const struct pvr_winsys_compute_ctx_create_info *create_info,
59 struct pvr_winsys_compute_ctx **const ctx_out)
60 {
61 struct rogue_fwif_static_computecontext_state static_state = {
62 .ctx_switch_regs = {
63 .cdm_context_pds0 = create_info->static_state.cdm_ctx_store_pds0,
64 .cdm_context_pds0_b =
65 create_info->static_state.cdm_ctx_store_pds0_b,
66 .cdm_context_pds1 = create_info->static_state.cdm_ctx_store_pds1,
67
68 .cdm_terminate_pds = create_info->static_state.cdm_ctx_terminate_pds,
69 .cdm_terminate_pds1 =
70 create_info->static_state.cdm_ctx_terminate_pds1,
71
72 .cdm_resume_pds0 = create_info->static_state.cdm_ctx_resume_pds0,
73 .cdm_resume_pds0_b = create_info->static_state.cdm_ctx_resume_pds0_b,
74 },
75 };
76
77 struct rogue_fwif_rf_cmd reset_cmd = { 0 };
78
79 struct pvr_srv_winsys *srv_ws = to_pvr_srv_winsys(ws);
80 struct pvr_srv_winsys_compute_ctx *srv_ctx;
81 VkResult result;
82
83 srv_ctx = vk_alloc(srv_ws->alloc,
84 sizeof(*srv_ctx),
85 8U,
86 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
87 if (!srv_ctx)
88 return vk_error(NULL, VK_ERROR_OUT_OF_HOST_MEMORY);
89
90 result = pvr_srv_create_timeline(srv_ws->render_fd, &srv_ctx->timeline);
91 if (result != VK_SUCCESS)
92 goto err_free_srv_ctx;
93
94 /* TODO: Add support for reset framework. Currently we subtract
95 * reset_cmd.regs size from reset_cmd size to only pass empty flags field.
96 */
97 result = pvr_srv_rgx_create_compute_context(
98 srv_ws->render_fd,
99 pvr_srv_from_winsys_priority(create_info->priority),
100 sizeof(reset_cmd) - sizeof(reset_cmd.regs),
101 (uint8_t *)&reset_cmd,
102 srv_ws->server_memctx_data,
103 sizeof(static_state),
104 (uint8_t *)&static_state,
105 0U,
106 RGX_CONTEXT_FLAG_DISABLESLR,
107 0U,
108 UINT_MAX,
109 &srv_ctx->handle);
110 if (result != VK_SUCCESS)
111 goto err_close_timeline;
112
113 srv_ctx->base.ws = ws;
114
115 *ctx_out = &srv_ctx->base;
116
117 return VK_SUCCESS;
118
119 err_close_timeline:
120 close(srv_ctx->timeline);
121
122 err_free_srv_ctx:
123 vk_free(srv_ws->alloc, srv_ctx);
124
125 return result;
126 }
127
pvr_srv_winsys_compute_ctx_destroy(struct pvr_winsys_compute_ctx * ctx)128 void pvr_srv_winsys_compute_ctx_destroy(struct pvr_winsys_compute_ctx *ctx)
129 {
130 struct pvr_srv_winsys *srv_ws = to_pvr_srv_winsys(ctx->ws);
131 struct pvr_srv_winsys_compute_ctx *srv_ctx =
132 to_pvr_srv_winsys_compute_ctx(ctx);
133
134 pvr_srv_rgx_destroy_compute_context(srv_ws->render_fd, srv_ctx->handle);
135 close(srv_ctx->timeline);
136 vk_free(srv_ws->alloc, srv_ctx);
137 }
138
pvr_srv_compute_cmd_init(const struct pvr_winsys_compute_submit_info * submit_info,struct rogue_fwif_cmd_compute * cmd)139 static void pvr_srv_compute_cmd_init(
140 const struct pvr_winsys_compute_submit_info *submit_info,
141 struct rogue_fwif_cmd_compute *cmd)
142 {
143 struct rogue_fwif_cdm_regs *fw_regs = &cmd->regs;
144
145 memset(cmd, 0, sizeof(*cmd));
146
147 cmd->cmn.frame_num = submit_info->frame_num;
148
149 fw_regs->tpu_border_colour_table = submit_info->regs.tpu_border_colour_table;
150 fw_regs->cdm_item = submit_info->regs.cdm_item;
151 fw_regs->compute_cluster = submit_info->regs.compute_cluster;
152 fw_regs->cdm_ctrl_stream_base = submit_info->regs.cdm_ctrl_stream_base;
153 fw_regs->cdm_context_state_base_addr =
154 submit_info->regs.cdm_ctx_state_base_addr;
155 fw_regs->tpu = submit_info->regs.tpu;
156 fw_regs->cdm_resume_pds1 = submit_info->regs.cdm_resume_pds1;
157
158 if (submit_info->flags & PVR_WINSYS_COMPUTE_FLAG_PREVENT_ALL_OVERLAP)
159 cmd->flags |= ROGUE_FWIF_COMPUTE_FLAG_PREVENT_ALL_OVERLAP;
160
161 if (submit_info->flags & PVR_WINSYS_COMPUTE_FLAG_SINGLE_CORE)
162 cmd->flags |= ROGUE_FWIF_COMPUTE_FLAG_SINGLE_CORE;
163 }
164
pvr_srv_winsys_compute_submit(const struct pvr_winsys_compute_ctx * ctx,const struct pvr_winsys_compute_submit_info * submit_info,struct vk_sync * signal_sync)165 VkResult pvr_srv_winsys_compute_submit(
166 const struct pvr_winsys_compute_ctx *ctx,
167 const struct pvr_winsys_compute_submit_info *submit_info,
168 struct vk_sync *signal_sync)
169 {
170 const struct pvr_srv_winsys_compute_ctx *srv_ctx =
171 to_pvr_srv_winsys_compute_ctx(ctx);
172 const struct pvr_srv_winsys *srv_ws = to_pvr_srv_winsys(ctx->ws);
173 struct rogue_fwif_cmd_compute compute_cmd;
174 struct pvr_srv_sync *srv_signal_sync;
175 VkResult result;
176 int in_fd = -1;
177 int fence;
178
179 pvr_srv_compute_cmd_init(submit_info, &compute_cmd);
180
181 for (uint32_t i = 0U; i < submit_info->wait_count; i++) {
182 struct pvr_srv_sync *srv_wait_sync = to_srv_sync(submit_info->waits[i]);
183 int ret;
184
185 if (!submit_info->waits[i] || srv_wait_sync->fd < 0)
186 continue;
187
188 if (submit_info->stage_flags[i] & PVR_PIPELINE_STAGE_COMPUTE_BIT) {
189 ret = sync_accumulate("", &in_fd, srv_wait_sync->fd);
190 if (ret) {
191 result = vk_error(NULL, VK_ERROR_OUT_OF_HOST_MEMORY);
192 goto err_close_in_fd;
193 }
194
195 submit_info->stage_flags[i] &= ~PVR_PIPELINE_STAGE_COMPUTE_BIT;
196 }
197 }
198
199 do {
200 result = pvr_srv_rgx_kick_compute2(srv_ws->render_fd,
201 srv_ctx->handle,
202 0U,
203 NULL,
204 NULL,
205 NULL,
206 in_fd,
207 srv_ctx->timeline,
208 sizeof(compute_cmd),
209 (uint8_t *)&compute_cmd,
210 submit_info->job_num,
211 0,
212 NULL,
213 NULL,
214 0U,
215 0U,
216 0U,
217 0U,
218 "COMPUTE",
219 &fence);
220 } while (result == VK_NOT_READY);
221
222 if (result != VK_SUCCESS)
223 goto err_close_in_fd;
224
225 if (signal_sync) {
226 srv_signal_sync = to_srv_sync(signal_sync);
227 pvr_srv_set_sync_payload(srv_signal_sync, fence);
228 } else if (fence != -1) {
229 close(fence);
230 }
231
232 return VK_SUCCESS;
233
234 err_close_in_fd:
235 if (in_fd >= 0)
236 close(in_fd);
237
238 return result;
239 }
240