| 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 <errno.h> |
| 25 |
#include <stddef.h> |
| 26 |
#include <stdint.h> |
| 27 |
#include <vulkan/vulkan.h> |
| 28 |
#include <xf86drm.h> |
| 29 |
|
| 30 |
#include "drm-uapi/pvr_drm.h" |
| 31 |
#include "pvr_drm.h" |
| 32 |
#include "pvr_drm_job_common.h" |
| 33 |
#include "pvr_drm_job_transfer.h" |
| 34 |
#include "pvr_winsys.h" |
| 35 |
#include "pvr_winsys_helper.h" |
| 36 |
#include "util/macros.h" |
| 37 |
#include "vk_alloc.h" |
| 38 |
#include "vk_drm_syncobj.h" |
| 39 |
#include "vk_log.h" |
| 40 |
|
| 41 |
struct pvr_drm_winsys_transfer_ctx { |
| 42 |
struct pvr_winsys_transfer_ctx base; |
| 43 |
|
| 44 |
uint32_t handle; |
| 45 |
}; |
| 46 |
|
| 47 |
#define to_pvr_drm_winsys_transfer_ctx(ctx) \ |
| 48 |
container_of(ctx, struct pvr_drm_winsys_transfer_ctx, base) |
| 49 |
|
| 50 |
VkResult pvr_drm_winsys_transfer_ctx_create( |
| 51 |
struct pvr_winsys *ws, |
| 52 |
const struct pvr_winsys_transfer_ctx_create_info *create_info, |
| 53 |
struct pvr_winsys_transfer_ctx **const ctx_out) |
| 54 |
{ |
| 55 |
struct pvr_drm_winsys *drm_ws = to_pvr_drm_winsys(ws); |
| 56 |
struct drm_pvr_ioctl_create_context_args ctx_args = { |
| 57 |
.type = DRM_PVR_CTX_TYPE_TRANSFER_FRAG, |
| 58 |
.priority = pvr_drm_from_winsys_priority(create_info->priority), |
| 59 |
.vm_context_handle = drm_ws->vm_context, |
| 60 |
}; |
| 61 |
|
| 62 |
struct pvr_drm_winsys_transfer_ctx *drm_ctx; |
| 63 |
VkResult result; |
| 64 |
|
| 65 |
drm_ctx = vk_zalloc(ws->alloc, |
| 66 |
sizeof(*drm_ctx), |
| 67 |
8, |
| 68 |
VK_SYSTEM_ALLOCATION_SCOPE_DEVICE); |
| 69 |
if (!drm_ctx) { |
| 70 |
result = vk_error(NULL, VK_ERROR_OUT_OF_HOST_MEMORY); |
| 71 |
goto err_out; |
| 72 |
} |
| 73 |
|
| 74 |
result = pvr_ioctlf(ws->render_fd, |
| 75 |
DRM_IOCTL_PVR_CREATE_CONTEXT, |
| 76 |
&ctx_args, |
| 77 |
VK_ERROR_INITIALIZATION_FAILED, |
| 78 |
"Failed to create transfer context"); |
| 79 |
if (result) |
| 80 |
goto err_free_ctx; |
| 81 |
|
| 82 |
drm_ctx->base.ws = ws; |
| 83 |
drm_ctx->handle = ctx_args.handle; |
| 84 |
|
| 85 |
*ctx_out = &drm_ctx->base; |
| 86 |
|
| 87 |
return VK_SUCCESS; |
| 88 |
|
| 89 |
err_free_ctx: |
| 90 |
vk_free(ws->alloc, drm_ctx); |
| 91 |
|
| 92 |
err_out: |
| 93 |
return result; |
| 94 |
} |
| 95 |
|
| 96 |
void pvr_drm_winsys_transfer_ctx_destroy(struct pvr_winsys_transfer_ctx *ctx) |
| 97 |
{ |
| 98 |
struct pvr_drm_winsys *drm_ws = to_pvr_drm_winsys(ctx->ws); |
| 99 |
struct pvr_drm_winsys_transfer_ctx *drm_ctx = |
| 100 |
to_pvr_drm_winsys_transfer_ctx(ctx); |
| 101 |
struct drm_pvr_ioctl_destroy_context_args args = { |
| 102 |
.handle = drm_ctx->handle, |
| 103 |
}; |
| 104 |
|
| 105 |
pvr_ioctlf(drm_ws->base.render_fd, |
| 106 |
DRM_IOCTL_PVR_DESTROY_CONTEXT, |
| 107 |
&args, |
| 108 |
VK_ERROR_UNKNOWN, |
| 109 |
"Error destroying transfer context"); |
| 110 |
|
| 111 |
vk_free(drm_ws->base.alloc, drm_ctx); |
| 112 |
} |
| 113 |
|
| 114 |
static uint32_t pvr_winsys_transfer_flags_to_drm( |
| 115 |
const struct pvr_winsys_transfer_cmd_flags *ws_flags) |
| 116 |
{ |
| 117 |
uint32_t flags = 0U; |
| 118 |
|
| 119 |
if (ws_flags->use_single_core) |
| 120 |
flags |= DRM_PVR_SUBMIT_JOB_TRANSFER_CMD_SINGLE_CORE; |
| 121 |
|
| 122 |
return flags; |
| 123 |
} |
| 124 |
|
| 125 |
VkResult pvr_drm_winsys_transfer_submit( |
| 126 |
const struct pvr_winsys_transfer_ctx *ctx, |
| 127 |
const struct pvr_winsys_transfer_submit_info *submit_info, |
| 128 |
UNUSED const struct pvr_device_info *const dev_info, |
| 129 |
struct vk_sync *signal_sync) |
| 130 |
{ |
| 131 |
const struct pvr_drm_winsys *drm_ws = to_pvr_drm_winsys(ctx->ws); |
| 132 |
const struct pvr_drm_winsys_transfer_ctx *drm_ctx = |
| 133 |
to_pvr_drm_winsys_transfer_ctx(ctx); |
| 134 |
|
| 135 |
struct drm_pvr_sync_op sync_ops[2]; |
| 136 |
|
| 137 |
struct drm_pvr_job job_args = { |
| 138 |
.type = DRM_PVR_JOB_TYPE_TRANSFER_FRAG, |
| 139 |
.cmd_stream = (__u64)&submit_info->cmds[0].fw_stream[0], |
| 140 |
.cmd_stream_len = submit_info->cmds[0].fw_stream_len, |
| 141 |
.flags = pvr_winsys_transfer_flags_to_drm(&submit_info->cmds[0].flags), |
| 142 |
.context_handle = drm_ctx->handle, |
| 143 |
.sync_ops = DRM_PVR_OBJ_ARRAY(0, sync_ops), |
| 144 |
}; |
| 145 |
|
| 146 |
struct drm_pvr_ioctl_submit_jobs_args args = { |
| 147 |
.jobs = DRM_PVR_OBJ_ARRAY(1, &job_args), |
| 148 |
}; |
| 149 |
|
| 150 |
assert(submit_info->cmd_count == 1); |
| 151 |
|
| 152 |
if (submit_info->wait) { |
| 153 |
struct vk_sync *sync = submit_info->wait; |
| 154 |
|
| 155 |
assert(!(sync->flags & VK_SYNC_IS_TIMELINE)); |
| 156 |
sync_ops[job_args.sync_ops.count++] = (struct drm_pvr_sync_op){ |
| 157 |
.handle = vk_sync_as_drm_syncobj(sync)->syncobj, |
| 158 |
.flags = DRM_PVR_SYNC_OP_FLAG_WAIT | |
| 159 |
DRM_PVR_SYNC_OP_FLAG_HANDLE_TYPE_SYNCOBJ, |
| 160 |
.value = 0, |
| 161 |
}; |
| 162 |
} |
| 163 |
|
| 164 |
if (signal_sync) { |
| 165 |
assert(!(signal_sync->flags & VK_SYNC_IS_TIMELINE)); |
| 166 |
sync_ops[job_args.sync_ops.count++] = (struct drm_pvr_sync_op){ |
| 167 |
.handle = vk_sync_as_drm_syncobj(signal_sync)->syncobj, |
| 168 |
.flags = DRM_PVR_SYNC_OP_FLAG_SIGNAL | |
| 169 |
DRM_PVR_SYNC_OP_FLAG_HANDLE_TYPE_SYNCOBJ, |
| 170 |
.value = 0, |
| 171 |
}; |
| 172 |
} |
| 173 |
|
| 174 |
/* Returns VK_ERROR_OUT_OF_DEVICE_MEMORY to match pvrsrv. */ |
| 175 |
return pvr_ioctlf(drm_ws->base.render_fd, |
| 176 |
DRM_IOCTL_PVR_SUBMIT_JOBS, |
| 177 |
&args, |
| 178 |
VK_ERROR_OUT_OF_DEVICE_MEMORY, |
| 179 |
"Failed to submit transfer job"); |
| 180 |
} |