| 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 "pvr_srv_job_render.h" |
| 25 |
|
| 26 |
#include <assert.h> |
| 27 |
#include <errno.h> |
| 28 |
#include <fcntl.h> |
| 29 |
#include <limits.h> |
| 30 |
#include <stdbool.h> |
| 31 |
#include <stddef.h> |
| 32 |
#include <stdint.h> |
| 33 |
#include <string.h> |
| 34 |
#include <unistd.h> |
| 35 |
#include <vulkan/vulkan.h> |
| 36 |
|
| 37 |
#include "fw-api/pvr_rogue_fwif.h" |
| 38 |
#include "fw-api/pvr_rogue_fwif_rf.h" |
| 39 |
|
| 40 |
#include "pvr_macros.h" |
| 41 |
#include "pvr_srv_bo.h" |
| 42 |
#include "pvr_srv_bridge.h" |
| 43 |
#include "pvr_srv_job_common.h" |
| 44 |
#include "pvr_srv_job_render.h" |
| 45 |
#include "pvr_srv_sync_prim.h" |
| 46 |
#include "pvr_srv_sync.h" |
| 47 |
#include "pvr_srv.h" |
| 48 |
#include "pvr_types.h" |
| 49 |
#include "pvr_winsys.h" |
| 50 |
|
| 51 |
#include "util/compiler.h" |
| 52 |
#include "util/log.h" |
| 53 |
#include "util/macros.h" |
| 54 |
#include "util/os_file.h" |
| 55 |
#include "util/u_math.h" |
| 56 |
#include "vk_alloc.h" |
| 57 |
#include "vk_log.h" |
| 58 |
#include "vk_util.h" |
| 59 |
|
| 60 |
struct pvr_srv_winsys_free_list { |
| 61 |
struct pvr_winsys_free_list base; |
| 62 |
|
| 63 |
void *handle; |
| 64 |
|
| 65 |
struct pvr_srv_winsys_free_list *parent; |
| 66 |
}; |
| 67 |
|
| 68 |
#define to_pvr_srv_winsys_free_list(free_list) \ |
| 69 |
container_of(free_list, struct pvr_srv_winsys_free_list, base) |
| 70 |
|
| 71 |
struct pvr_srv_winsys_rt_dataset { |
| 72 |
struct pvr_winsys_rt_dataset base; |
| 73 |
|
| 74 |
struct { |
| 75 |
void *handle; |
| 76 |
struct pvr_srv_sync_prim *sync_prim; |
| 77 |
} rt_datas[ROGUE_FWIF_NUM_RTDATAS]; |
| 78 |
}; |
| 79 |
|
| 80 |
#define to_pvr_srv_winsys_rt_dataset(rt_dataset) \ |
| 81 |
container_of(rt_dataset, struct pvr_srv_winsys_rt_dataset, base) |
| 82 |
|
| 83 |
struct pvr_srv_winsys_render_ctx { |
| 84 |
struct pvr_winsys_render_ctx base; |
| 85 |
|
| 86 |
/* Handle to kernel context. */ |
| 87 |
void *handle; |
| 88 |
|
| 89 |
int timeline_geom; |
| 90 |
int timeline_frag; |
| 91 |
}; |
| 92 |
|
| 93 |
#define to_pvr_srv_winsys_render_ctx(ctx) \ |
| 94 |
container_of(ctx, struct pvr_srv_winsys_render_ctx, base) |
| 95 |
|
| 96 |
VkResult pvr_srv_winsys_free_list_create( |
| 97 |
struct pvr_winsys *ws, |
| 98 |
struct pvr_winsys_vma *free_list_vma, |
| 99 |
uint32_t initial_num_pages, |
| 100 |
uint32_t max_num_pages, |
| 101 |
uint32_t grow_num_pages, |
| 102 |
uint32_t grow_threshold, |
| 103 |
struct pvr_winsys_free_list *parent_free_list, |
| 104 |
struct pvr_winsys_free_list **const free_list_out) |
| 105 |
{ |
| 106 |
struct pvr_srv_winsys *srv_ws = to_pvr_srv_winsys(ws); |
| 107 |
struct pvr_srv_winsys_bo *srv_free_list_bo = |
| 108 |
to_pvr_srv_winsys_bo(free_list_vma->bo); |
| 109 |
struct pvr_srv_winsys_free_list *srv_free_list; |
| 110 |
void *parent_handle; |
| 111 |
VkResult result; |
| 112 |
|
| 113 |
srv_free_list = vk_zalloc(ws->alloc, |
| 114 |
sizeof(*srv_free_list), |
| 115 |
8, |
| 116 |
VK_SYSTEM_ALLOCATION_SCOPE_DEVICE); |
| 117 |
if (!srv_free_list) |
| 118 |
return vk_error(NULL, VK_ERROR_OUT_OF_HOST_MEMORY); |
| 119 |
|
| 120 |
if (parent_free_list) { |
| 121 |
srv_free_list->parent = to_pvr_srv_winsys_free_list(parent_free_list); |
| 122 |
parent_handle = srv_free_list->parent->handle; |
| 123 |
} else { |
| 124 |
srv_free_list->parent = NULL; |
| 125 |
parent_handle = NULL; |
| 126 |
} |
| 127 |
|
| 128 |
result = pvr_srv_rgx_create_free_list(ws->render_fd, |
| 129 |
srv_ws->server_memctx_data, |
| 130 |
max_num_pages, |
| 131 |
initial_num_pages, |
| 132 |
grow_num_pages, |
| 133 |
grow_threshold, |
| 134 |
parent_handle, |
| 135 |
#if MESA_DEBUG |
| 136 |
PVR_SRV_TRUE /* free_list_check */, |
| 137 |
#else |
| 138 |
PVR_SRV_FALSE /* free_list_check */, |
| 139 |
#endif |
| 140 |
free_list_vma->dev_addr, |
| 141 |
srv_free_list_bo->pmr, |
| 142 |
0 /* pmr_offset */, |
| 143 |
&srv_free_list->handle); |
| 144 |
if (result != VK_SUCCESS) |
| 145 |
goto err_vk_free_srv_free_list; |
| 146 |
|
| 147 |
srv_free_list->base.ws = ws; |
| 148 |
|
| 149 |
*free_list_out = &srv_free_list->base; |
| 150 |
|
| 151 |
return VK_SUCCESS; |
| 152 |
|
| 153 |
err_vk_free_srv_free_list: |
| 154 |
vk_free(ws->alloc, srv_free_list); |
| 155 |
|
| 156 |
return result; |
| 157 |
} |
| 158 |
|
| 159 |
void pvr_srv_winsys_free_list_destroy(struct pvr_winsys_free_list *free_list) |
| 160 |
{ |
| 161 |
struct pvr_srv_winsys *srv_ws = to_pvr_srv_winsys(free_list->ws); |
| 162 |
struct pvr_srv_winsys_free_list *srv_free_list = |
| 163 |
to_pvr_srv_winsys_free_list(free_list); |
| 164 |
|
| 165 |
pvr_srv_rgx_destroy_free_list(srv_ws->base.render_fd, srv_free_list->handle); |
| 166 |
vk_free(srv_ws->base.alloc, srv_free_list); |
| 167 |
} |
| 168 |
|
| 169 |
void pvr_srv_render_target_dataset_destroy( |
| 170 |
struct pvr_winsys_rt_dataset *rt_dataset) |
| 171 |
{ |
| 172 |
struct pvr_srv_winsys *srv_ws = to_pvr_srv_winsys(rt_dataset->ws); |
| 173 |
struct pvr_srv_winsys_rt_dataset *srv_rt_dataset = |
| 174 |
to_pvr_srv_winsys_rt_dataset(rt_dataset); |
| 175 |
|
| 176 |
for (uint32_t i = 0; i < ARRAY_SIZE(srv_rt_dataset->rt_datas); i++) { |
| 177 |
pvr_srv_sync_prim_free(srv_ws, srv_rt_dataset->rt_datas[i].sync_prim); |
| 178 |
|
| 179 |
if (srv_rt_dataset->rt_datas[i].handle) { |
| 180 |
pvr_srv_rgx_destroy_hwrt_dataset(srv_ws->base.render_fd, |
| 181 |
srv_rt_dataset->rt_datas[i].handle); |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
vk_free(srv_ws->base.alloc, srv_rt_dataset); |
| 186 |
} |
| 187 |
|
| 188 |
static void pvr_srv_render_ctx_fw_static_state_init( |
| 189 |
enum pvr_device_arch arch, |
| 190 |
struct pvr_winsys_render_ctx_create_info *create_info, |
| 191 |
struct rogue_fwif_static_rendercontext_state *static_state) |
| 192 |
{ |
| 193 |
/* TODO: handle non-rogue GPUs */ |
| 194 |
assert(arch == PVR_DEVICE_ARCH_ROGUE); |
| 195 |
struct pvr_rogue_winsys_render_ctx_static_state *ws_static_state = |
| 196 |
&create_info->static_state.rogue; |
| 197 |
|
| 198 |
struct rogue_fwif_ta_regs_cswitch *regs = |
| 199 |
&static_state->ctx_switch_geom_regs[0]; |
| 200 |
|
| 201 |
memset(static_state, 0, sizeof(*static_state)); |
| 202 |
|
| 203 |
regs->vdm_context_state_base_addr = ws_static_state->vdm_ctx_state_base_addr; |
| 204 |
regs->ta_context_state_base_addr = ws_static_state->geom_ctx_state_base_addr; |
| 205 |
|
| 206 |
STATIC_ASSERT(ARRAY_SIZE(regs->ta_state) == |
| 207 |
ARRAY_SIZE(ws_static_state->geom_state)); |
| 208 |
for (uint32_t i = 0; i < ARRAY_SIZE(ws_static_state->geom_state); i++) { |
| 209 |
regs->ta_state[i].vdm_context_store_task0 = |
| 210 |
ws_static_state->geom_state[i].vdm_ctx_store_task0; |
| 211 |
regs->ta_state[i].vdm_context_store_task1 = |
| 212 |
ws_static_state->geom_state[i].vdm_ctx_store_task1; |
| 213 |
regs->ta_state[i].vdm_context_store_task2 = |
| 214 |
ws_static_state->geom_state[i].vdm_ctx_store_task2; |
| 215 |
|
| 216 |
regs->ta_state[i].vdm_context_resume_task0 = |
| 217 |
ws_static_state->geom_state[i].vdm_ctx_resume_task0; |
| 218 |
regs->ta_state[i].vdm_context_resume_task1 = |
| 219 |
ws_static_state->geom_state[i].vdm_ctx_resume_task1; |
| 220 |
regs->ta_state[i].vdm_context_resume_task2 = |
| 221 |
ws_static_state->geom_state[i].vdm_ctx_resume_task2; |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
VkResult pvr_srv_winsys_render_ctx_create( |
| 226 |
struct pvr_winsys *ws, |
| 227 |
struct pvr_winsys_render_ctx_create_info *create_info, |
| 228 |
const struct pvr_device_info *dev_info, |
| 229 |
struct pvr_winsys_render_ctx **const ctx_out) |
| 230 |
{ |
| 231 |
struct pvr_srv_winsys *srv_ws = to_pvr_srv_winsys(ws); |
| 232 |
struct rogue_fwif_rf_cmd reset_cmd = { 0 }; |
| 233 |
|
| 234 |
struct rogue_fwif_static_rendercontext_state static_state; |
| 235 |
struct pvr_srv_winsys_render_ctx *srv_ctx; |
| 236 |
const uint32_t call_stack_depth = 1U; |
| 237 |
VkResult result; |
| 238 |
|
| 239 |
srv_ctx = vk_zalloc(ws->alloc, |
| 240 |
sizeof(*srv_ctx), |
| 241 |
8, |
| 242 |
VK_SYSTEM_ALLOCATION_SCOPE_DEVICE); |
| 243 |
if (!srv_ctx) |
| 244 |
return vk_error(NULL, VK_ERROR_OUT_OF_HOST_MEMORY); |
| 245 |
|
| 246 |
result = pvr_srv_create_timeline(ws->render_fd, &srv_ctx->timeline_geom); |
| 247 |
if (result != VK_SUCCESS) |
| 248 |
goto err_free_srv_ctx; |
| 249 |
|
| 250 |
result = pvr_srv_create_timeline(ws->render_fd, &srv_ctx->timeline_frag); |
| 251 |
if (result != VK_SUCCESS) |
| 252 |
goto err_close_timeline_geom; |
| 253 |
|
| 254 |
pvr_srv_render_ctx_fw_static_state_init(dev_info->ident.arch, |
| 255 |
create_info, |
| 256 |
&static_state); |
| 257 |
|
| 258 |
/* TODO: Add support for reset framework. Currently we subtract |
| 259 |
* reset_cmd.regs size from reset_cmd size to only pass empty flags field. |
| 260 |
*/ |
| 261 |
result = pvr_srv_rgx_create_render_context( |
| 262 |
ws->render_fd, |
| 263 |
pvr_srv_from_winsys_priority(create_info->priority), |
| 264 |
create_info->vdm_callstack_addr, |
| 265 |
call_stack_depth, |
| 266 |
sizeof(reset_cmd) - sizeof(reset_cmd.regs), |
| 267 |
(uint8_t *)&reset_cmd, |
| 268 |
srv_ws->server_memctx_data, |
| 269 |
sizeof(static_state), |
| 270 |
(uint8_t *)&static_state, |
| 271 |
0, |
| 272 |
RGX_CONTEXT_FLAG_DISABLESLR, |
| 273 |
0, |
| 274 |
UINT_MAX, |
| 275 |
UINT_MAX, |
| 276 |
&srv_ctx->handle); |
| 277 |
if (result != VK_SUCCESS) |
| 278 |
goto err_close_timeline_frag; |
| 279 |
|
| 280 |
srv_ctx->base.ws = ws; |
| 281 |
|
| 282 |
*ctx_out = &srv_ctx->base; |
| 283 |
|
| 284 |
return VK_SUCCESS; |
| 285 |
|
| 286 |
err_close_timeline_frag: |
| 287 |
close(srv_ctx->timeline_frag); |
| 288 |
|
| 289 |
err_close_timeline_geom: |
| 290 |
close(srv_ctx->timeline_geom); |
| 291 |
|
| 292 |
err_free_srv_ctx: |
| 293 |
vk_free(ws->alloc, srv_ctx); |
| 294 |
|
| 295 |
return vk_error(NULL, VK_ERROR_INITIALIZATION_FAILED); |
| 296 |
} |
| 297 |
|
| 298 |
void pvr_srv_winsys_render_ctx_destroy(struct pvr_winsys_render_ctx *ctx) |
| 299 |
{ |
| 300 |
struct pvr_srv_winsys *srv_ws = to_pvr_srv_winsys(ctx->ws); |
| 301 |
struct pvr_srv_winsys_render_ctx *srv_ctx = |
| 302 |
to_pvr_srv_winsys_render_ctx(ctx); |
| 303 |
|
| 304 |
pvr_srv_rgx_destroy_render_context(srv_ws->base.render_fd, srv_ctx->handle); |
| 305 |
close(srv_ctx->timeline_frag); |
| 306 |
close(srv_ctx->timeline_geom); |
| 307 |
vk_free(srv_ws->base.alloc, srv_ctx); |
| 308 |
} |