| 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 <stdbool.h> |
| 26 |
#include <stddef.h> |
| 27 |
#include <stdint.h> |
| 28 |
#include <string.h> |
| 29 |
#include <vulkan/vulkan.h> |
| 30 |
#include <xf86drm.h> |
| 31 |
|
| 32 |
#include "drm-uapi/pvr_drm.h" |
| 33 |
#include "pvr_drm.h" |
| 34 |
#include "pvr_drm_job_common.h" |
| 35 |
#include "pvr_drm_job_compute.h" |
| 36 |
#include "pvr_winsys.h" |
| 37 |
#include "pvr_winsys_helper.h" |
| 38 |
#include "util/macros.h" |
| 39 |
#include "vk_alloc.h" |
| 40 |
#include "vk_drm_syncobj.h" |
| 41 |
#include "vk_log.h" |
| 42 |
|
| 43 |
struct pvr_drm_winsys_compute_ctx { |
| 44 |
struct pvr_winsys_compute_ctx base; |
| 45 |
|
| 46 |
/* Handle to kernel context. */ |
| 47 |
uint32_t handle; |
| 48 |
}; |
| 49 |
|
| 50 |
#define to_pvr_drm_winsys_compute_ctx(ctx) \ |
| 51 |
container_of(ctx, struct pvr_drm_winsys_compute_ctx, base) |
| 52 |
|
| 53 |
static void pvr_drm_compute_ctx_static_state_init( |
| 54 |
enum pvr_device_arch arch, |
| 55 |
const struct pvr_winsys_compute_ctx_create_info *create_info, |
| 56 |
uint8_t *stream_ptr_start, |
| 57 |
uint32_t *stream_len_ptr) |
| 58 |
{ |
| 59 |
/* TODO: handle non-rogue GPUs */ |
| 60 |
assert(arch == PVR_DEVICE_ARCH_ROGUE); |
| 61 |
const struct pvr_rogue_winsys_compute_ctx_static_state *ws_static_state = |
| 62 |
&create_info->static_state.rogue; |
| 63 |
|
| 64 |
uint64_t *stream_ptr = (uint64_t *)stream_ptr_start; |
| 65 |
|
| 66 |
/* Leave space for stream header. */ |
| 67 |
stream_ptr++; |
| 68 |
|
| 69 |
*stream_ptr++ = ws_static_state->cdm_ctx_store_pds0; |
| 70 |
*stream_ptr++ = ws_static_state->cdm_ctx_store_pds1; |
| 71 |
*stream_ptr++ = ws_static_state->cdm_ctx_terminate_pds; |
| 72 |
*stream_ptr++ = ws_static_state->cdm_ctx_terminate_pds1; |
| 73 |
*stream_ptr++ = ws_static_state->cdm_ctx_resume_pds0; |
| 74 |
*stream_ptr++ = ws_static_state->cdm_ctx_store_pds0_b; |
| 75 |
*stream_ptr++ = ws_static_state->cdm_ctx_resume_pds0_b; |
| 76 |
|
| 77 |
*stream_len_ptr = ((uint8_t *)stream_ptr - stream_ptr_start); |
| 78 |
|
| 79 |
*((uint64_t *)stream_ptr_start) = *stream_len_ptr; |
| 80 |
} |
| 81 |
|
| 82 |
VkResult pvr_drm_winsys_compute_ctx_create( |
| 83 |
struct pvr_winsys *ws, |
| 84 |
const struct pvr_winsys_compute_ctx_create_info *create_info, |
| 85 |
const struct pvr_device_info *dev_info, |
| 86 |
struct pvr_winsys_compute_ctx **const ctx_out) |
| 87 |
{ |
| 88 |
uint8_t static_ctx_state_fw_stream[64]; |
| 89 |
struct pvr_drm_winsys *drm_ws = to_pvr_drm_winsys(ws); |
| 90 |
struct drm_pvr_ioctl_create_context_args ctx_args = { |
| 91 |
.type = DRM_PVR_CTX_TYPE_COMPUTE, |
| 92 |
.priority = pvr_drm_from_winsys_priority(create_info->priority), |
| 93 |
.static_context_state = (__u64)static_ctx_state_fw_stream, |
| 94 |
.static_context_state_len = (__u32)sizeof(static_ctx_state_fw_stream), |
| 95 |
.vm_context_handle = drm_ws->vm_context, |
| 96 |
}; |
| 97 |
|
| 98 |
struct pvr_drm_winsys_compute_ctx *drm_ctx; |
| 99 |
VkResult result; |
| 100 |
|
| 101 |
drm_ctx = vk_alloc(ws->alloc, |
| 102 |
sizeof(*drm_ctx), |
| 103 |
8, |
| 104 |
VK_SYSTEM_ALLOCATION_SCOPE_DEVICE); |
| 105 |
if (!drm_ctx) { |
| 106 |
result = vk_error(NULL, VK_ERROR_OUT_OF_HOST_MEMORY); |
| 107 |
goto err_out; |
| 108 |
} |
| 109 |
|
| 110 |
pvr_drm_compute_ctx_static_state_init(dev_info->ident.arch, |
| 111 |
create_info, |
| 112 |
static_ctx_state_fw_stream, |
| 113 |
&ctx_args.static_context_state_len); |
| 114 |
|
| 115 |
result = pvr_ioctlf(ws->render_fd, |
| 116 |
DRM_IOCTL_PVR_CREATE_CONTEXT, |
| 117 |
&ctx_args, |
| 118 |
VK_ERROR_INITIALIZATION_FAILED, |
| 119 |
"Failed to create compute context"); |
| 120 |
if (result != VK_SUCCESS) |
| 121 |
goto err_free_ctx; |
| 122 |
|
| 123 |
drm_ctx->base.ws = ws; |
| 124 |
drm_ctx->handle = ctx_args.handle; |
| 125 |
|
| 126 |
*ctx_out = &drm_ctx->base; |
| 127 |
|
| 128 |
return VK_SUCCESS; |
| 129 |
|
| 130 |
err_free_ctx: |
| 131 |
vk_free(ws->alloc, drm_ctx); |
| 132 |
|
| 133 |
err_out: |
| 134 |
return result; |
| 135 |
} |
| 136 |
|
| 137 |
void pvr_drm_winsys_compute_ctx_destroy(struct pvr_winsys_compute_ctx *ctx) |
| 138 |
{ |
| 139 |
struct pvr_drm_winsys *drm_ws = to_pvr_drm_winsys(ctx->ws); |
| 140 |
struct pvr_drm_winsys_compute_ctx *drm_ctx = |
| 141 |
to_pvr_drm_winsys_compute_ctx(ctx); |
| 142 |
struct drm_pvr_ioctl_destroy_context_args args = { |
| 143 |
.handle = drm_ctx->handle, |
| 144 |
}; |
| 145 |
|
| 146 |
pvr_ioctlf(drm_ws->base.render_fd, |
| 147 |
DRM_IOCTL_PVR_DESTROY_CONTEXT, |
| 148 |
&args, |
| 149 |
VK_ERROR_UNKNOWN, |
| 150 |
"Error destroying compute context"); |
| 151 |
|
| 152 |
vk_free(drm_ws->base.alloc, drm_ctx); |
| 153 |
} |
| 154 |
|
| 155 |
static uint32_t pvr_winsys_compute_flags_to_drm( |
| 156 |
const struct pvr_winsys_compute_submit_flags *const ws_flags) |
| 157 |
{ |
| 158 |
uint32_t flags = 0U; |
| 159 |
|
| 160 |
if (ws_flags->prevent_all_overlap) |
| 161 |
flags |= DRM_PVR_SUBMIT_JOB_COMPUTE_CMD_PREVENT_ALL_OVERLAP; |
| 162 |
|
| 163 |
if (ws_flags->use_single_core) |
| 164 |
flags |= DRM_PVR_SUBMIT_JOB_COMPUTE_CMD_SINGLE_CORE; |
| 165 |
|
| 166 |
return flags; |
| 167 |
} |
| 168 |
|
| 169 |
VkResult pvr_drm_winsys_compute_submit( |
| 170 |
const struct pvr_winsys_compute_ctx *ctx, |
| 171 |
const struct pvr_winsys_compute_submit_info *submit_info, |
| 172 |
UNUSED const struct pvr_device_info *dev_info, |
| 173 |
struct vk_sync *signal_sync) |
| 174 |
{ |
| 175 |
const struct pvr_drm_winsys *drm_ws = to_pvr_drm_winsys(ctx->ws); |
| 176 |
const struct pvr_drm_winsys_compute_ctx *drm_ctx = |
| 177 |
to_pvr_drm_winsys_compute_ctx(ctx); |
| 178 |
|
| 179 |
struct drm_pvr_sync_op sync_ops[2]; |
| 180 |
struct drm_pvr_job job_args = { |
| 181 |
.type = DRM_PVR_JOB_TYPE_COMPUTE, |
| 182 |
.context_handle = drm_ctx->handle, |
| 183 |
.cmd_stream = (__u64)&submit_info->fw_stream[0], |
| 184 |
.cmd_stream_len = submit_info->fw_stream_len, |
| 185 |
/* bo_handles is unused and zeroed. */ |
| 186 |
/* num_bo_handles is unused and zeroed. */ |
| 187 |
.flags = pvr_winsys_compute_flags_to_drm(&submit_info->flags), |
| 188 |
.sync_ops = DRM_PVR_OBJ_ARRAY(0, sync_ops), |
| 189 |
}; |
| 190 |
|
| 191 |
struct drm_pvr_ioctl_submit_jobs_args args = { |
| 192 |
.jobs = DRM_PVR_OBJ_ARRAY(1, &job_args), |
| 193 |
}; |
| 194 |
|
| 195 |
if (submit_info->wait) { |
| 196 |
struct vk_sync *sync = submit_info->wait; |
| 197 |
|
| 198 |
assert(!(sync->flags & VK_SYNC_IS_TIMELINE)); |
| 199 |
sync_ops[job_args.sync_ops.count++] = (struct drm_pvr_sync_op){ |
| 200 |
.handle = vk_sync_as_drm_syncobj(sync)->syncobj, |
| 201 |
.flags = DRM_PVR_SYNC_OP_FLAG_WAIT | |
| 202 |
DRM_PVR_SYNC_OP_FLAG_HANDLE_TYPE_SYNCOBJ, |
| 203 |
.value = 0, |
| 204 |
}; |
| 205 |
} |
| 206 |
|
| 207 |
if (signal_sync) { |
| 208 |
assert(!(signal_sync->flags & VK_SYNC_IS_TIMELINE)); |
| 209 |
sync_ops[job_args.sync_ops.count++] = (struct drm_pvr_sync_op){ |
| 210 |
.handle = vk_sync_as_drm_syncobj(signal_sync)->syncobj, |
| 211 |
.flags = DRM_PVR_SYNC_OP_FLAG_SIGNAL | |
| 212 |
DRM_PVR_SYNC_OP_FLAG_HANDLE_TYPE_SYNCOBJ, |
| 213 |
.value = 0, |
| 214 |
}; |
| 215 |
} |
| 216 |
|
| 217 |
/* Returns VK_ERROR_OUT_OF_DEVICE_MEMORY to match pvrsrv. */ |
| 218 |
return pvr_ioctlf(drm_ws->base.render_fd, |
| 219 |
DRM_IOCTL_PVR_SUBMIT_JOBS, |
| 220 |
&args, |
| 221 |
VK_ERROR_OUT_OF_DEVICE_MEMORY, |
| 222 |
"Failed to submit compute job"); |
| 223 |
} |