diff --git a/src/imagination/vulkan/meson.build b/src/imagination/vulkan/meson.build index 41a5a507b4b..820aed62ab7 100644 --- a/src/imagination/vulkan/meson.build +++ b/src/imagination/vulkan/meson.build @@ -173,6 +173,7 @@ common_per_arch_files += files( pvr_files += files( 'pvr_g5p_formats.c', 'pvr_g5p_image.c', + 'pvr_g5p_reuse.c', 'pvr_g5p_nir.c', 'pvr_g5p_shader_dump.c', 'pvr_errlog.c', diff --git a/src/imagination/vulkan/pvr_arch_cmd_buffer.c b/src/imagination/vulkan/pvr_arch_cmd_buffer.c index bc6efa3cdff..79571934561 100644 --- a/src/imagination/vulkan/pvr_arch_cmd_buffer.c +++ b/src/imagination/vulkan/pvr_arch_cmd_buffer.c @@ -21,6 +21,7 @@ * SOFTWARE. */ +#include "pvr_g5p_reuse.h" #include "pvr_cmd_buffer.h" #include @@ -192,6 +193,17 @@ struct pvr_renderpass_hwsetup_render *pvr_arch_pass_info_get_hw_render( return &render_pass_info->pass->hw_setup->renders[idx]; } +/* G5P: a primary command buffer's control stream blocks go back to the + * device's cache once its submissions have completed (pvr_g5p_reuse.h). A + * secondary's are linked from primaries this buffer does not know of: freed. */ +static void pvr_cmd_buffer_retire_csb(struct pvr_cmd_buffer *cmd_buffer, struct pvr_csb *csb) +{ + if (cmd_buffer->device->g5p_reuse && cmd_buffer->vk.level == VK_COMMAND_BUFFER_LEVEL_PRIMARY) + pvr_g5p_csb_retire(cmd_buffer->device, csb, cmd_buffer->g5p_retire); + else + pvr_csb_finish(csb); +} + static void pvr_cmd_buffer_free_sub_cmd(struct pvr_cmd_buffer *cmd_buffer, struct pvr_sub_cmd *sub_cmd) { @@ -213,7 +225,7 @@ static void pvr_cmd_buffer_free_sub_cmd(struct pvr_cmd_buffer *cmd_buffer, pvr_bo_free(cmd_buffer->device, *bo); util_dynarray_fini(&sub_cmd->gfx.g9p_view_bos); util_dynarray_fini(&sub_cmd->gfx.unbound_deferred_clears); - pvr_csb_finish(&sub_cmd->gfx.control_stream); + pvr_cmd_buffer_retire_csb(cmd_buffer, &sub_cmd->gfx.control_stream); pvr_bo_free(cmd_buffer->device, sub_cmd->gfx.terminate_ctrl_stream); pvr_bo_free(cmd_buffer->device, sub_cmd->gfx.multiview_ctrl_stream); pvr_bo_suballoc_free(sub_cmd->gfx.depth_bias_bo); @@ -234,7 +246,7 @@ static void pvr_cmd_buffer_free_sub_cmd(struct pvr_cmd_buffer *cmd_buffer, free(l->line); free(l); } - pvr_csb_finish(&sub_cmd->compute.control_stream); + pvr_cmd_buffer_retire_csb(cmd_buffer, &sub_cmd->compute.control_stream); break; case PVR_SUB_CMD_TYPE_TRANSFER: @@ -285,6 +297,8 @@ static void pvr_cmd_buffer_free_resources(struct pvr_cmd_buffer *cmd_buffer) util_dynarray_fini(&cmd_buffer->state.query_indices); pvr_cmd_buffer_free_sub_cmds(cmd_buffer); + /* the blocks recorded from now on belong to no submission yet */ + memset(cmd_buffer->g5p_retire, 0, sizeof(cmd_buffer->g5p_retire)); memset(cmd_buffer->g5p_upload, 0, sizeof(cmd_buffer->g5p_upload)); if (cmd_buffer->g5p_views) { diff --git a/src/imagination/vulkan/pvr_arch_csb.c b/src/imagination/vulkan/pvr_arch_csb.c index 66edec7622b..8cd00d9ec48 100644 --- a/src/imagination/vulkan/pvr_arch_csb.c +++ b/src/imagination/vulkan/pvr_arch_csb.c @@ -29,6 +29,7 @@ #include "util/list.h" #include "util/u_dynarray.h" #include "vk_log.h" +#include "pvr_g5p_reuse.h" /** * \brief Adds VDMCTRL_STREAM_LINK/CDMCTRL_STREAM_LINK dwords into the control @@ -121,12 +122,16 @@ static bool pvr_csb_buffer_extend(struct pvr_csb *csb) STATIC_ASSERT(ROGUE_VDMCTRL_GUARD_SIZE_DEFAULT == ROGUE_CDMCTRL_GUARD_SIZE_DEFAULT); - result = pvr_bo_alloc(csb->device, - csb->device->heaps.general_heap, - PVR_CMD_BUFFER_CSB_BO_SIZE, - cache_line_size, - PVR_BO_ALLOC_FLAG_CPU_MAPPED, - &pvr_bo); + /* G5P: a block retired by an earlier command buffer whose jobs have + * completed, zeroed (pvr_g5p_reuse.h); else a new one */ + pvr_g5p_csb_block_alloc(csb->device, &pvr_bo); + result = pvr_bo ? VK_SUCCESS + : pvr_bo_alloc(csb->device, + csb->device->heaps.general_heap, + PVR_CMD_BUFFER_CSB_BO_SIZE, + cache_line_size, + PVR_BO_ALLOC_FLAG_CPU_MAPPED, + &pvr_bo); if (result != VK_SUCCESS) { vk_error(csb->device, result); csb->status = result; diff --git a/src/imagination/vulkan/pvr_arch_device.c b/src/imagination/vulkan/pvr_arch_device.c index a6597ba27cb..5c80ef5f957 100644 --- a/src/imagination/vulkan/pvr_arch_device.c +++ b/src/imagination/vulkan/pvr_arch_device.c @@ -10,6 +10,7 @@ * SPDX-License-Identifier: MIT */ +#include "pvr_g5p_reuse.h" #include "pvr_bo_log.h" #include "pvr_device.h" @@ -901,6 +902,8 @@ VkResult PVR_PER_ARCH(create_device)(struct pvr_physical_device *pdevice, device, device->suballoc_usc.default_size); + pvr_g5p_reuse_init(device); + if (p_atomic_inc_return(&instance->active_device_count) > PVR_SECONDARY_DEVICE_THRESHOLD) { initial_free_list_size = PVR_SECONDARY_DEVICE_FREE_LIST_INITAL_SIZE; @@ -1079,6 +1082,7 @@ VkResult PVR_PER_ARCH(create_device)(struct pvr_physical_device *pdevice, pvr_bo_log_summary(device, "destroy"); pvr_bo_log_device_finish(device); } + pvr_g5p_reuse_finish(device); pvr_bo_suballocator_fini(&device->suballoc_cmd_usc); pvr_bo_suballocator_fini(&device->suballoc_cmd_pds); pvr_bo_suballocator_fini(&device->suballoc_cmd_general); @@ -1158,6 +1162,7 @@ void PVR_PER_ARCH(destroy_device)(struct pvr_device *device, pvr_bo_log_summary(device, "destroy"); pvr_bo_log_device_finish(device); } + pvr_g5p_reuse_finish(device); pvr_bo_suballocator_fini(&device->suballoc_cmd_usc); pvr_bo_suballocator_fini(&device->suballoc_cmd_pds); pvr_bo_suballocator_fini(&device->suballoc_cmd_general); diff --git a/src/imagination/vulkan/pvr_arch_g5p_compute.c b/src/imagination/vulkan/pvr_arch_g5p_compute.c index 3da2046451b..c07843e4ef4 100644 --- a/src/imagination/vulkan/pvr_arch_g5p_compute.c +++ b/src/imagination/vulkan/pvr_arch_g5p_compute.c @@ -1099,7 +1099,7 @@ PVR_PER_ARCH(g5p_compute_job_submit)(struct pvr_compute_ctx *ctx, * run on the device. */ if (cs->pvr_bo != list_first_entry(&cs->pvr_bo_list, struct pvr_bo, link)) return vk_errorf(d, VK_ERROR_OUT_OF_HOST_MEMORY, "G5P: chained CDM stream not implemented yet"); - struct pvr_winsys_compute_submit_info info = { .wait = wait, + struct pvr_winsys_compute_submit_info info = { .stamp = sub->g5p_stamp, .wait = wait, .g5p_log = sub->g5p_log_first, .g5p_log_count = sub->g5p_log_count, .frame_num = d->global_queue_present_count, .job_num = d->global_cmd_buffer_submit_count }; info.fw_stream_len = diff --git a/src/imagination/vulkan/pvr_arch_job_compute.c b/src/imagination/vulkan/pvr_arch_job_compute.c index 92b2fa2ac79..43d09444f80 100644 --- a/src/imagination/vulkan/pvr_arch_job_compute.c +++ b/src/imagination/vulkan/pvr_arch_job_compute.c @@ -206,6 +206,7 @@ static void pvr_compute_job_ws_submit_info_init( submit_info->job_num = device->global_cmd_buffer_submit_count; submit_info->wait = wait; + submit_info->stamp = sub_cmd->g5p_stamp; pvr_submit_info_stream_init(ctx, sub_cmd, submit_info); pvr_submit_info_ext_stream_init(ctx, submit_info); diff --git a/src/imagination/vulkan/pvr_arch_job_render.c b/src/imagination/vulkan/pvr_arch_job_render.c index f03122f9774..872c303a8a6 100644 --- a/src/imagination/vulkan/pvr_arch_job_render.c +++ b/src/imagination/vulkan/pvr_arch_job_render.c @@ -1565,6 +1565,7 @@ static void pvr_render_job_ws_submit_info_init( submit_info->frame_num = ctx->device->global_queue_present_count; submit_info->job_num = ctx->device->global_cmd_buffer_submit_count; + submit_info->stamp = job->g5p_stamp; pvr_render_job_ws_geometry_state_init(ctx, job, diff --git a/src/imagination/vulkan/pvr_arch_queue.c b/src/imagination/vulkan/pvr_arch_queue.c index fc264dcd2d1..df030490336 100644 --- a/src/imagination/vulkan/pvr_arch_queue.c +++ b/src/imagination/vulkan/pvr_arch_queue.c @@ -32,6 +32,7 @@ #include "util/os_time.h" #include "pvr_bo_log.h" #include "pvr_g9p.h" +#include "pvr_g5p_reuse.h" #include "pvr_queue.h" #include "pvr_g5p_layout.h" #include "pvr_rt_dataset.h" @@ -160,6 +161,10 @@ static VkResult pvr_queue_init(struct pvr_device *device, if (result != VK_SUCCESS) goto err_query_ctx_destroy; + result = pvr_g5p_queue_stamp_init(device, queue, index_in_family); + if (result != VK_SUCCESS) + goto err_gfx_ctx_destroy; + queue->device = device; queue->gfx_ctx = gfx_ctx; queue->compute_ctx = compute_ctx; @@ -170,6 +175,9 @@ static VkResult pvr_queue_init(struct pvr_device *device, return VK_SUCCESS; +err_gfx_ctx_destroy: + pvr_arch_render_ctx_destroy(gfx_ctx); + err_query_ctx_destroy: pvr_arch_compute_ctx_destroy(query_ctx); @@ -240,6 +248,7 @@ static void pvr_queue_finish(struct pvr_queue *queue) pvr_arch_compute_ctx_destroy(queue->compute_ctx); if (queue->transfer_ctx) pvr_arch_transfer_ctx_destroy(queue->transfer_ctx); + pvr_g5p_queue_stamp_finish(queue->device, queue); vk_queue_finish(&queue->vk); } @@ -258,15 +267,12 @@ static void pvr_update_job_syncs(struct pvr_device *device, enum pvr_job_type submitted_job_type) { if (queue->next_job_wait_sync[submitted_job_type]) { - vk_sync_destroy(&device->vk, - queue->next_job_wait_sync[submitted_job_type]); + pvr_g5p_sync_put(device, queue->next_job_wait_sync[submitted_job_type]); queue->next_job_wait_sync[submitted_job_type] = NULL; } - if (queue->last_job_signal_sync[submitted_job_type]) { - vk_sync_destroy(&device->vk, - queue->last_job_signal_sync[submitted_job_type]); - } + if (queue->last_job_signal_sync[submitted_job_type]) + pvr_g5p_sync_put(device, queue->last_job_signal_sync[submitted_job_type]); queue->g5p_src_done[submitted_job_type] = false; queue->last_job_signal_sync[submitted_job_type] = new_signal_sync; @@ -285,13 +291,13 @@ g5p_merge_syncs(struct pvr_device *device, struct vk_sync *a, struct vk_sync *b, waits[n++] = (struct vk_sync_wait){ .sync = a, .stage_mask = ~(VkPipelineStageFlags2)0 }; if (b) waits[n++] = (struct vk_sync_wait){ .sync = b, .stage_mask = ~(VkPipelineStageFlags2)0 }; - result = vk_sync_create(&device->vk, &device->pdevice->sync_type, 0U, 0UL, out); + result = pvr_g5p_sync_get(device, out); if (result != VK_SUCCESS) return result; const struct vk_sync_signal signal = { .sync = *out, .stage_mask = ~(VkPipelineStageFlags2)0 }; result = device->ws->ops->null_job_submit(device->ws, waits, n, (struct vk_sync_signal *)&signal); if (result != VK_SUCCESS) { - vk_sync_destroy(&device->vk, *out); + pvr_g5p_sync_put(device, *out); *out = NULL; } return result; @@ -314,20 +320,12 @@ pvr_process_graphics_cmd_for_view(struct pvr_device *device, (view_index + job->ds.base_array_layer)); job->view_state.view_index = view_index; - result = vk_sync_create(&device->vk, - &device->pdevice->sync_type, - 0U, - 0UL, - &geom_signal_sync); + result = pvr_g5p_sync_get(device, &geom_signal_sync); if (result != VK_SUCCESS) return result; if (job->run_frag) { - result = vk_sync_create(&device->vk, - &device->pdevice->sync_type, - 0U, - 0UL, - &frag_signal_sync); + result = pvr_g5p_sync_get(device, &frag_signal_sync); if (result != VK_SUCCESS) goto err_destroy_geom_sync; } @@ -372,6 +370,8 @@ pvr_process_graphics_cmd_for_view(struct pvr_device *device, /* First submit must not touch fragment work. */ job->geometry_terminate = false; job->run_frag = false; + /* the second submit's stamp covers this one (same geometry queue) */ + job->g5p_stamp = (struct pvr_winsys_stamp){ 0 }; result = pvr_arch_render_job_submit( queue->gfx_ctx, @@ -435,6 +435,7 @@ pvr_process_graphics_cmd_for_view(struct pvr_device *device, goto err_destroy_frag_sync; frag_wait = g5p_frag_wait; } + job->g5p_stamp = pvr_g5p_queue_stamp_next(queue); result = pvr_arch_render_job_submit(queue->gfx_ctx, &sub_cmd->job, @@ -442,8 +443,10 @@ pvr_process_graphics_cmd_for_view(struct pvr_device *device, frag_wait, geom_signal_sync, frag_signal_sync); + if (result == VK_SUCCESS) + pvr_g5p_queue_stamp_issued(queue, job->g5p_stamp); if (g5p_frag_wait) - vk_sync_destroy(&device->vk, g5p_frag_wait); + pvr_g5p_sync_put(device, g5p_frag_wait); if (original_ctrl_stream_addr.addr > 0) job->ctrl_stream_addr = original_ctrl_stream_addr; @@ -633,9 +636,9 @@ pvr_process_graphics_cmd_for_view(struct pvr_device *device, err_destroy_frag_sync: if (frag_signal_sync) - vk_sync_destroy(&device->vk, frag_signal_sync); + pvr_g5p_sync_put(device, frag_signal_sync); err_destroy_geom_sync: - vk_sync_destroy(&device->vk, geom_signal_sync); + pvr_g5p_sync_put(device, geom_signal_sync); return result; } @@ -715,23 +718,21 @@ static VkResult pvr_process_compute_cmd(struct pvr_device *device, struct vk_sync *sync; VkResult result; - result = vk_sync_create(&device->vk, - &device->pdevice->sync_type, - 0U, - 0UL, - &sync); + result = pvr_g5p_sync_get(device, &sync); if (result != VK_SUCCESS) return result; + sub_cmd->g5p_stamp = pvr_g5p_queue_stamp_next(queue); result = pvr_arch_compute_job_submit( queue->compute_ctx, sub_cmd, queue->next_job_wait_sync[PVR_JOB_TYPE_COMPUTE], sync); if (result != VK_SUCCESS) { - vk_sync_destroy(&device->vk, sync); + pvr_g5p_sync_put(device, sync); return result; } + pvr_g5p_queue_stamp_issued(queue, sub_cmd->g5p_stamp); /* G9P_SYNC: wait for each compute job too */ if (PVR_HAS_FEATURE(&device->pdevice->dev_info, apple_g9p) && getenv("G9P_SYNC") && @@ -753,11 +754,7 @@ static VkResult pvr_process_transfer_cmds(struct pvr_device *device, struct vk_sync *sync; VkResult result; - result = vk_sync_create(&device->vk, - &device->pdevice->sync_type, - 0U, - 0UL, - &sync); + result = pvr_g5p_sync_get(device, &sync); if (result != VK_SUCCESS) return result; @@ -801,7 +798,7 @@ static VkResult pvr_process_transfer_cmds(struct pvr_device *device, queue->next_job_wait_sync[PVR_JOB_TYPE_TRANSFER], sync); if (result != VK_SUCCESS) { - vk_sync_destroy(&device->vk, sync); + pvr_g5p_sync_put(device, sync); return result; } @@ -900,23 +897,21 @@ static VkResult pvr_process_query_cmd(struct pvr_device *device, * commands? */ - result = vk_sync_create(&device->vk, - &device->pdevice->sync_type, - 0U, - 0UL, - &sync); + result = pvr_g5p_sync_get(device, &sync); if (result != VK_SUCCESS) return result; + sub_cmd->g5p_stamp = pvr_g5p_queue_stamp_next(queue); result = pvr_arch_compute_job_submit(queue->query_ctx, sub_cmd, queue->next_job_wait_sync[PVR_JOB_TYPE_QUERY], sync); if (result != VK_SUCCESS) { - vk_sync_destroy(&device->vk, sync); + pvr_g5p_sync_put(device, sync); return result; } + pvr_g5p_queue_stamp_issued(queue, sub_cmd->g5p_stamp); pvr_update_job_syncs(device, queue, sync, PVR_JOB_TYPE_QUERY); @@ -971,11 +966,7 @@ pvr_process_event_cmd_barrier(struct pvr_device *device, struct vk_sync_signal signal; struct vk_sync *signal_sync; - result = vk_sync_create(&device->vk, - &device->pdevice->sync_type, - 0U, - 0UL, - &signal_sync); + result = pvr_g5p_sync_get(device, &signal_sync); if (result != VK_SUCCESS) return result; @@ -998,12 +989,12 @@ pvr_process_event_cmd_barrier(struct pvr_device *device, wait_count, &signal); if (result != VK_SUCCESS) { - vk_sync_destroy(&device->vk, signal_sync); + pvr_g5p_sync_put(device, signal_sync); return result; } if (queue->next_job_wait_sync[stage]) - vk_sync_destroy(&device->vk, queue->next_job_wait_sync[stage]); + pvr_g5p_sync_put(device, queue->next_job_wait_sync[stage]); queue->next_job_wait_sync[stage] = signal_sync; } @@ -1151,11 +1142,7 @@ pvr_process_event_cmd_wait(struct pvr_device *device, assert(wait_count <= (sub_cmd->count + 1)); - result = vk_sync_create(&device->vk, - &device->pdevice->sync_type, - 0U, - 0UL, - &signal_sync); + result = pvr_g5p_sync_get(device, &signal_sync); if (result != VK_SUCCESS) goto err_free_waits; @@ -1170,12 +1157,12 @@ pvr_process_event_cmd_wait(struct pvr_device *device, wait_count, &signal); if (result != VK_SUCCESS) { - vk_sync_destroy(&device->vk, signal.sync); + pvr_g5p_sync_put(device, signal.sync); goto err_free_waits; } if (queue->next_job_wait_sync[stage]) - vk_sync_destroy(&device->vk, queue->next_job_wait_sync[stage]); + pvr_g5p_sync_put(device, queue->next_job_wait_sync[stage]); queue->next_job_wait_sync[stage] = signal.sync; } @@ -1633,11 +1620,7 @@ static VkResult pvr_process_queue_waits(struct pvr_queue *queue, }; } - result = vk_sync_create(&device->vk, - &device->pdevice->sync_type, - 0U, - 0UL, - &queue->next_job_wait_sync[i]); + result = pvr_g5p_sync_get(device, &queue->next_job_wait_sync[i]); if (result != VK_SUCCESS) { queue->next_job_wait_sync[i] = prev_wait; goto err_free_waits; @@ -1656,7 +1639,7 @@ static VkResult pvr_process_queue_waits(struct pvr_queue *queue, if (result != VK_SUCCESS) goto err_free_waits; if (prev_wait) - vk_sync_destroy(&device->vk, prev_wait); + pvr_g5p_sync_put(device, prev_wait); } STACK_ARRAY_FINISH(stage_waits); @@ -1702,11 +1685,14 @@ static VkResult pvr_driver_queue_submit_jobs(struct vk_queue *queue, struct pvr_suspended_data suspended_data = {0}; for (uint32_t i = 0U; i < submit->command_buffer_count; i++) { - result = pvr_process_cmd_buffer( - device, - driver_queue, - container_of(submit->command_buffers[i], struct pvr_cmd_buffer, vk), - &suspended_data); + struct pvr_cmd_buffer *cmd_buffer = + container_of(submit->command_buffers[i], struct pvr_cmd_buffer, vk); + + result = pvr_process_cmd_buffer(device, driver_queue, cmd_buffer, &suspended_data); + /* G5P reuse: its blocks are free again once this queue's stamp passes + * the last point issued so far (jobs submitted before a failure too) */ + if (driver_queue->g5p_stamp_syncobj) + cmd_buffer->g5p_retire[driver_queue->g5p_index] = driver_queue->g5p_stamp_issued; if (result != VK_SUCCESS) return result; } diff --git a/src/imagination/vulkan/pvr_cmd_buffer.h b/src/imagination/vulkan/pvr_cmd_buffer.h index 622d85ea9b9..9e89558a1de 100644 --- a/src/imagination/vulkan/pvr_cmd_buffer.h +++ b/src/imagination/vulkan/pvr_cmd_buffer.h @@ -255,6 +255,9 @@ struct pvr_sub_cmd_gfx { }; struct pvr_sub_cmd_compute { + /* G5P submit reuse: what its job signals (pvr_g5p_reuse.h) */ + struct pvr_winsys_stamp g5p_stamp; + /* Control stream builder object. */ struct pvr_csb control_stream; @@ -686,6 +689,10 @@ struct pvr_cmd_buffer { struct pvr_suballoc_bo *block; uint32_t used; } g5p_upload[2]; + /* G5P submit reuse: per queue, the stamp point after which this buffer's + * last submission there has completed; 0: not submitted there since its + * last reset (pvr_g5p_reuse.h) */ + uint64_t g5p_retire[PVR_MAX_QUEUES]; struct linear_ctx *g5p_views; struct list_head sub_cmds; diff --git a/src/imagination/vulkan/pvr_device.c b/src/imagination/vulkan/pvr_device.c index a92172609c2..48cdc1a94ed 100644 --- a/src/imagination/vulkan/pvr_device.c +++ b/src/imagination/vulkan/pvr_device.c @@ -634,20 +634,35 @@ VkResult pvr_bind_memory(struct pvr_device *device, assert(offset % alignment == 0); assert(offset < mem->bo->size); - result = device->ws->ops->heap_alloc(device->heaps.general_heap, - virt_size, - alignment, - &vma); - if (result != VK_SUCCESS) { - mesa_loge("pvr: no GPU address space for %" PRIu64 " bytes (alignment %" PRIu64 ")", - (uint64_t)virt_size, (uint64_t)alignment); - goto err_out; - } + if (mem->vk.import_handle_type && device->ws->ops->vma_map_imported) { + result = device->ws->ops->vma_map_imported(device->heaps.general_heap, + mem->bo, + offset, + size, + alignment, + &vma, + &dev_addr); + if (result != VK_SUCCESS) { + mesa_loge("pvr: mapping imported %" PRIu64 " bytes into the GPU failed (%d)", + (uint64_t)size, result); + goto err_out; + } + } else { + result = device->ws->ops->heap_alloc(device->heaps.general_heap, + virt_size, + alignment, + &vma); + if (result != VK_SUCCESS) { + mesa_loge("pvr: no GPU address space for %" PRIu64 " bytes (alignment %" PRIu64 ")", + (uint64_t)virt_size, (uint64_t)alignment); + goto err_out; + } - result = device->ws->ops->vma_map(vma, mem->bo, offset, size, &dev_addr); - if (result != VK_SUCCESS) { - mesa_loge("pvr: mapping %" PRIu64 " bytes into the GPU failed (%d)", (uint64_t)size, result); - goto err_free_vma; + result = device->ws->ops->vma_map(vma, mem->bo, offset, size, &dev_addr); + if (result != VK_SUCCESS) { + mesa_loge("pvr: mapping %" PRIu64 " bytes into the GPU failed (%d)", (uint64_t)size, result); + goto err_free_vma; + } } *dev_addr_out = dev_addr; diff --git a/src/imagination/vulkan/pvr_device.h b/src/imagination/vulkan/pvr_device.h index d28e7d32198..5742750a6e8 100644 --- a/src/imagination/vulkan/pvr_device.h +++ b/src/imagination/vulkan/pvr_device.h @@ -179,6 +179,9 @@ struct pvr_device { struct pvr_suballocator suballoc_cmd_pds; struct pvr_suballocator suballoc_cmd_usc; + /* G5P submit reuse (pvr_g5p_reuse.h); NULL when off */ + struct pvr_g5p_reuse *g5p_reuse; + struct { struct pvr_pds_upload pds; struct pvr_suballoc_bo *usc; diff --git a/src/imagination/vulkan/pvr_g5p_nir.c b/src/imagination/vulkan/pvr_g5p_nir.c index a7a350a9287..5e56879e67a 100644 --- a/src/imagination/vulkan/pvr_g5p_nir.c +++ b/src/imagination/vulkan/pvr_g5p_nir.c @@ -679,6 +679,7 @@ g5p_nir(const uint32_t *spv, size_t words, mesa_shader_stage stage, const char * util_call_once(&once, g5p_init_options); struct spirv_capabilities caps = { .Shader = true, .Float16 = true, .Int16 = true, .Int8 = true, .Int64 = true, .Float64 = true, .TransformFeedback = true, + .FloatControls2 = true, /* user clip distances: the TA user planes (pvr_g5p_clip_distance_probe()) */ .ClipDistance = true }; struct spirv_to_nir_options opts = { diff --git a/src/imagination/vulkan/pvr_g5p_reuse.c b/src/imagination/vulkan/pvr_g5p_reuse.c new file mode 100644 index 00000000000..b91fc6dc17b --- /dev/null +++ b/src/imagination/vulkan/pvr_g5p_reuse.c @@ -0,0 +1,345 @@ +/* SPDX-License-Identifier: MIT */ +/* + * Apple's A9 GPU (G5P): submit-time reuse (pvr_g5p_reuse.h). + */ + +#include "pvr_g5p_reuse.h" + +#include +#include +#include + +#include "pvr_bo.h" +#include "pvr_csb.h" +#include "pvr_device.h" +#include "pvr_physical_device.h" +#include "pvr_queue.h" +#include "util/u_debug.h" +#include "vk_alloc.h" +#include "vk_drm_syncobj.h" +#include "vk_sync.h" + +/* The command stream blocks retired by one command buffer, pending until + * every queue's stamp timeline has reached its point. */ +struct pvr_g5p_retired { + struct list_head link; + struct list_head bos; + uint32_t count; + uint64_t points[PVR_MAX_QUEUES]; +}; + +void pvr_g5p_reuse_init(struct pvr_device *device) +{ + const struct pvr_winsys_ops *ops = device->ws->ops; + struct pvr_g5p_reuse *r; + + device->g5p_reuse = NULL; + if (!PVR_HAS_FEATURE(&device->pdevice->dev_info, apple_g5p) || !pvr_g5p_reuse_option()) + return; + if (!ops->stamp_create || !ops->stamp_destroy || !ops->stamp_query || !ops->syncs_reset || + !vk_sync_type_is_drm_syncobj(&device->pdevice->sync_type)) + return; + + r = vk_zalloc(&device->vk.alloc, sizeof(*r), 8, VK_SYSTEM_ALLOCATION_SCOPE_DEVICE); + if (!r) + return; /* reuse is an optimisation: without it, the plain paths */ + + simple_mtx_init(&r->mtx, mtx_plain); + simple_mtx_init(&r->sync_mtx, mtx_plain); + list_inithead(&r->free_blocks); + list_inithead(&r->pending); + r->stats = debug_get_bool_option("PVR_G5P_REUSE_STATS", false); + device->g5p_reuse = r; +} + +void pvr_g5p_reuse_finish(struct pvr_device *device) +{ + struct pvr_g5p_reuse *r = device->g5p_reuse; + + if (!r) + return; + + /* The queues are gone (pvr_arch_queues_destroy()): nothing runs any more. */ + if (r->stats) + fprintf(stderr, + "pvr: g5p reuse: blocks reused %" PRIu64 " fresh %" PRIu64 " queries %" PRIu64 + " freed %" PRIu64 " (free %u pending %u); syncs reused %" PRIu64 " fresh %" PRIu64 + " resets %" PRIu64 "\n", + r->block_reused, r->block_fresh, r->block_queries, r->block_freed, r->free_count, + r->pending_count, r->sync_reused, r->sync_fresh, r->sync_resets); + + list_for_each_entry_safe (struct pvr_g5p_retired, ret, &r->pending, link) { + list_for_each_entry_safe (struct pvr_bo, bo, &ret->bos, link) { + list_del(&bo->link); + pvr_bo_free(device, bo); + } + list_del(&ret->link); + vk_free(&device->vk.alloc, ret); + } + list_for_each_entry_safe (struct pvr_bo, bo, &r->free_blocks, link) { + list_del(&bo->link); + pvr_bo_free(device, bo); + } + for (uint32_t i = 0; i < r->nclean; i++) + vk_sync_destroy(&device->vk, r->clean_syncs[i]); + for (uint32_t i = 0; i < r->ndirty; i++) + vk_sync_destroy(&device->vk, r->dirty_syncs[i]); + + simple_mtx_destroy(&r->sync_mtx); + simple_mtx_destroy(&r->mtx); + vk_free(&device->vk.alloc, r); + device->g5p_reuse = NULL; +} + +VkResult pvr_g5p_queue_stamp_init(struct pvr_device *device, struct pvr_queue *queue, uint32_t index) +{ + struct pvr_g5p_reuse *r = device->g5p_reuse; + VkResult result; + + queue->g5p_index = index; + queue->g5p_stamp_syncobj = 0; + queue->g5p_stamp_issued = 0; + if (!r) + return VK_SUCCESS; + + assert(index < PVR_MAX_QUEUES); + /* A queue without a stamp would leave its command buffers' blocks looking + * unsubmitted: no queue runs without one while reuse is on. */ + result = device->ws->ops->stamp_create(device->ws, &queue->g5p_stamp_syncobj); + if (result != VK_SUCCESS) + return result; + + simple_mtx_lock(&r->mtx); + r->stamp_syncobj[index] = queue->g5p_stamp_syncobj; + r->completed[index] = 0; + simple_mtx_unlock(&r->mtx); + return VK_SUCCESS; +} + +void pvr_g5p_queue_stamp_finish(struct pvr_device *device, struct pvr_queue *queue) +{ + struct pvr_g5p_reuse *r = device->g5p_reuse; + + if (!r || !queue->g5p_stamp_syncobj) + return; + simple_mtx_lock(&r->mtx); + r->stamp_syncobj[queue->g5p_index] = 0; + simple_mtx_unlock(&r->mtx); + device->ws->ops->stamp_destroy(device->ws, queue->g5p_stamp_syncobj); + queue->g5p_stamp_syncobj = 0; +} + +struct pvr_winsys_stamp pvr_g5p_queue_stamp_next(const struct pvr_queue *queue) +{ + if (!queue->g5p_stamp_syncobj) + return (struct pvr_winsys_stamp){ 0 }; + return (struct pvr_winsys_stamp){ + .syncobj = queue->g5p_stamp_syncobj, + .point = queue->g5p_stamp_issued + 1, + }; +} + +void pvr_g5p_queue_stamp_issued(struct pvr_queue *queue, struct pvr_winsys_stamp stamp) +{ + if (stamp.syncobj) { + assert(stamp.point == queue->g5p_stamp_issued + 1); + queue->g5p_stamp_issued = stamp.point; + } +} + +static bool g5p_points_reached(const struct pvr_g5p_reuse *r, const uint64_t points[PVR_MAX_QUEUES]) +{ + for (uint32_t q = 0; q < PVR_MAX_QUEUES; q++) { + if (points[q] > r->completed[q]) + return false; + } + return true; +} + +/* Free blocks past the cap (the oldest first) onto `out`, to be freed after + * the lock: pending blocks stay whatever their count. */ +static void g5p_trim_locked(struct pvr_g5p_reuse *r, struct list_head *out) +{ + while (r->free_count && r->free_count + r->pending_count > PVR_G5P_REUSE_BLOCK_CAP) { + struct pvr_bo *bo = list_first_entry(&r->free_blocks, struct pvr_bo, link); + + list_del(&bo->link); + list_addtail(&bo->link, out); + r->free_count--; + r->block_freed++; + } +} + +/* Read every queue's stamp timeline (one ioctl) and free the pending blocks + * whose points were all reached. */ +static void g5p_collect_locked(struct pvr_device *device, struct pvr_g5p_reuse *r) +{ + uint32_t syncobjs[PVR_MAX_QUEUES], index[PVR_MAX_QUEUES]; + uint64_t points[PVR_MAX_QUEUES]; + uint32_t n = 0; + + for (uint32_t q = 0; q < PVR_MAX_QUEUES; q++) { + if (r->stamp_syncobj[q]) { + syncobjs[n] = r->stamp_syncobj[q]; + index[n++] = q; + } + } + if (!n || device->ws->ops->stamp_query(device->ws, syncobjs, points, n) != VK_SUCCESS) + return; + r->block_queries++; + for (uint32_t i = 0; i < n; i++) + r->completed[index[i]] = MAX2(r->completed[index[i]], points[i]); + + list_for_each_entry_safe (struct pvr_g5p_retired, ret, &r->pending, link) { + if (!g5p_points_reached(r, ret->points)) + continue; + list_splicetail(&ret->bos, &r->free_blocks); + r->free_count += ret->count; + r->pending_count -= ret->count; + list_del(&ret->link); + vk_free(&device->vk.alloc, ret); + } +} + +static void g5p_free_blocks(struct pvr_device *device, struct list_head *bos) +{ + list_for_each_entry_safe (struct pvr_bo, bo, bos, link) { + list_del(&bo->link); + pvr_bo_free(device, bo); + } +} + +VkResult pvr_g5p_csb_block_alloc(struct pvr_device *device, struct pvr_bo **bo_out) +{ + struct pvr_g5p_reuse *r = device->g5p_reuse; + struct pvr_bo *bo = NULL; + struct list_head extra; + + *bo_out = NULL; + if (!r) + return VK_SUCCESS; + + list_inithead(&extra); + simple_mtx_lock(&r->mtx); + /* Ask the kernel (one ioctl) only when there is something to learn: no + * free block, some pending. A fresh block joins the cache when it is + * retired, so the cache grows to what the GPU holds in flight. */ + if (list_is_empty(&r->free_blocks) && r->pending_count) { + g5p_collect_locked(device, r); + g5p_trim_locked(r, &extra); + } + if (!list_is_empty(&r->free_blocks)) { + bo = list_first_entry(&r->free_blocks, struct pvr_bo, link); + list_del(&bo->link); + r->free_count--; + r->block_reused++; + } else { + r->block_fresh++; + } + simple_mtx_unlock(&r->mtx); + g5p_free_blocks(device, &extra); + + if (bo) { + /* what a new BO holds: the VDM reads ahead of the stream's end */ + memset(bo->bo->map, 0, bo->bo->size); + *bo_out = bo; + } + return VK_SUCCESS; +} + +void pvr_g5p_csb_retire(struct pvr_device *device, struct pvr_csb *csb, + const uint64_t retire[PVR_MAX_QUEUES]) +{ + struct pvr_g5p_reuse *r = device->g5p_reuse; + struct pvr_g5p_retired *ret = NULL; + struct list_head extra; + bool moved = false; + uint32_t count; + + if (!r || csb->stream_type == PVR_CMD_STREAM_TYPE_GRAPHICS_DEFERRED || + csb->stream_type == PVR_CMD_STREAM_TYPE_INVALID || list_is_empty(&csb->pvr_bo_list)) { + pvr_csb_finish(csb); + return; + } + + count = list_length(&csb->pvr_bo_list); + list_inithead(&extra); + simple_mtx_lock(&r->mtx); + if (g5p_points_reached(r, retire)) { + /* never submitted, or already known complete */ + list_splicetail(&csb->pvr_bo_list, &r->free_blocks); + r->free_count += count; + moved = true; + } else { + ret = vk_alloc(&device->vk.alloc, sizeof(*ret), 8, VK_SYSTEM_ALLOCATION_SCOPE_DEVICE); + if (ret) { + list_inithead(&ret->bos); + list_splicetail(&csb->pvr_bo_list, &ret->bos); + ret->count = count; + memcpy(ret->points, retire, sizeof(ret->points)); + list_addtail(&ret->link, &r->pending); + r->pending_count += count; + moved = true; + } + } + g5p_trim_locked(r, &extra); + simple_mtx_unlock(&r->mtx); + g5p_free_blocks(device, &extra); + + /* the blocks moved (or, without memory for the record, are freed as + * before): finish an empty stream */ + if (moved) + list_inithead(&csb->pvr_bo_list); + pvr_csb_finish(csb); +} + +VkResult pvr_g5p_sync_get(struct pvr_device *device, struct vk_sync **sync_out) +{ + struct pvr_g5p_reuse *r = device->g5p_reuse; + + if (r) { + struct vk_sync *sync = NULL; + + simple_mtx_lock(&r->sync_mtx); + /* reset in batches: below one, a new sync joins the pool instead */ + if (!r->nclean && r->ndirty >= PVR_G5P_REUSE_SYNC_BATCH && + device->ws->ops->syncs_reset(device->ws, r->dirty_syncs, r->ndirty) == VK_SUCCESS) { + memcpy(r->clean_syncs, r->dirty_syncs, r->ndirty * sizeof(r->dirty_syncs[0])); + r->nclean = r->ndirty; + r->ndirty = 0; + r->sync_resets++; + } + if (r->nclean) { + sync = r->clean_syncs[--r->nclean]; + r->sync_reused++; + } else { + r->sync_fresh++; + } + simple_mtx_unlock(&r->sync_mtx); + if (sync) { + *sync_out = sync; + return VK_SUCCESS; + } + } + return vk_sync_create(&device->vk, &device->pdevice->sync_type, 0U, 0UL, sync_out); +} + +void pvr_g5p_sync_put(struct pvr_device *device, struct vk_sync *sync) +{ + struct pvr_g5p_reuse *r = device->g5p_reuse; + + if (!sync) + return; + if (r) { + assert(sync->type == &device->pdevice->sync_type && !(sync->flags & VK_SYNC_IS_TIMELINE)); + simple_mtx_lock(&r->sync_mtx); + if (r->ndirty < PVR_G5P_REUSE_SYNC_CAP) { + r->dirty_syncs[r->ndirty++] = sync; + sync = NULL; + } + simple_mtx_unlock(&r->sync_mtx); + if (!sync) + return; + } + vk_sync_destroy(&device->vk, sync); +} diff --git a/src/imagination/vulkan/pvr_g5p_reuse.h b/src/imagination/vulkan/pvr_g5p_reuse.h new file mode 100644 index 00000000000..ad24ca65b8a --- /dev/null +++ b/src/imagination/vulkan/pvr_g5p_reuse.h @@ -0,0 +1,110 @@ +/* SPDX-License-Identifier: MIT */ +/* + * Apple's A9 GPU (G5P): submit-time reuse instead of per-submit kernel work. + * + * What a vkQueueSubmit used to create and destroy every time: + * - its command stream blocks: one 4 KiB BO each (pvr_csb_buffer_extend()), + * created, CPU-mapped and GPU-mapped while recording, unmapped and closed + * at the command buffer's reset (7 kernel calls per block). On the A9 every + * GPU unmap also moves the MMU tag of every loaded VM slot, so the next job + * of every client starts with its TLB dropped; + * - its job syncobjs (create + destroy per job and per merged wait), and the + * null job's merge syncobj. + * + * Apple (iOS 16 IOGPU.framework): command buffer storage comes from a + * device-wide pool (IOGPUMetalCommandBufferStoragePoolCreateStorage) and goes + * back to it from the completion handler, + * -[IOGPUMetalCommandBuffer didCompleteWithStartTime:endTime:error:] -> + * IOGPUMetalCommandBufferStorageDealloc -> ...StorageReset + + * ...StoragePoolReturnStorage: the memory is reused only once the GPU is done + * with it, never at the app's say. + * + * Here: + * - Stamps. Each queue owns one timeline syncobj. Every GPU job it submits + * (render, compute, query) also signals the next point of it, as one more + * sync op of the same SUBMIT_JOBS ioctl. A queue's points only grow, so the + * timeline's value is the point up to which all its jobs have completed. + * A command buffer records, per queue, the last point its jobs signal. + * - Command stream blocks. A primary command buffer's blocks, when it is reset + * or freed, go to the device's pending list with those points. They become + * free for reuse only after a query (one ioctl for all queues) shows every + * point reached; a never-submitted buffer's blocks are free at once. With no + * free block, a fresh one is allocated: reuse never waits. A reused block + * is zeroed, so the GPU sees exactly what a new BO holds. Secondary command + * buffers (linked from primaries) keep the plain free. + * - Syncs. The queue's job syncs are returned to a pool and reset together + * (one ioctl) before reuse, back to the state vk_sync_create() gives. + * + * PVR_G5P_REUSE=0 turns all of it off (pvr_g5p_reuse_option()). + */ + +#ifndef PVR_G5P_REUSE_H +#define PVR_G5P_REUSE_H + +#include +#include +#include + +#include "pvr_limits.h" +#include "util/list.h" +#include "util/simple_mtx.h" +#include "winsys/pvr_winsys.h" + +struct pvr_bo; +struct pvr_csb; +struct pvr_device; +struct pvr_queue; +struct vk_sync; + +/* free + pending command stream blocks kept at most (pending ones are kept + * whatever the count: the GPU may still read them) */ +#define PVR_G5P_REUSE_BLOCK_CAP 512 +#define PVR_G5P_REUSE_SYNC_CAP 64 +/* syncs reset together, at least */ +#define PVR_G5P_REUSE_SYNC_BATCH 32 + +struct pvr_g5p_reuse { + simple_mtx_t mtx; /* blocks and stamps */ + struct list_head free_blocks; /* struct pvr_bo, by link */ + struct list_head pending; /* struct pvr_g5p_retired */ + uint32_t free_count, pending_count; + /* each queue's stamp timeline (0: none) and its last queried value */ + uint32_t stamp_syncobj[PVR_MAX_QUEUES]; + uint64_t completed[PVR_MAX_QUEUES]; + + simple_mtx_t sync_mtx; + struct vk_sync *clean_syncs[PVR_G5P_REUSE_SYNC_CAP]; + struct vk_sync *dirty_syncs[PVR_G5P_REUSE_SYNC_CAP]; + uint32_t nclean, ndirty; + + /* PVR_G5P_REUSE_STATS=1: printed at device destruction */ + bool stats; + uint64_t block_reused, block_fresh, block_queries, block_freed; + uint64_t sync_reused, sync_fresh, sync_resets; +}; + +void pvr_g5p_reuse_init(struct pvr_device *device); +void pvr_g5p_reuse_finish(struct pvr_device *device); + +/* stamps */ +VkResult pvr_g5p_queue_stamp_init(struct pvr_device *device, struct pvr_queue *queue, uint32_t index); +void pvr_g5p_queue_stamp_finish(struct pvr_device *device, struct pvr_queue *queue); +/* the stamp the queue's next GPU job signals ({0} without reuse); after the + * job was submitted, pvr_g5p_queue_stamp_issued() makes it the queue's last */ +struct pvr_winsys_stamp pvr_g5p_queue_stamp_next(const struct pvr_queue *queue); +void pvr_g5p_queue_stamp_issued(struct pvr_queue *queue, struct pvr_winsys_stamp stamp); + +/* command stream blocks */ +VkResult pvr_g5p_csb_block_alloc(struct pvr_device *device, struct pvr_bo **bo_out); +/* Gives up a command buffer's control stream: its blocks go pending until + * every point of retire[] (per queue, 0: not submitted there) is reached. + * Leaves the csb finished, as pvr_csb_finish() does. */ +void pvr_g5p_csb_retire(struct pvr_device *device, struct pvr_csb *csb, + const uint64_t retire[PVR_MAX_QUEUES]); + +/* syncs: as vk_sync_create(device's sync type, binary, 0) / vk_sync_destroy() + * for syncs no one else holds */ +VkResult pvr_g5p_sync_get(struct pvr_device *device, struct vk_sync **sync_out); +void pvr_g5p_sync_put(struct pvr_device *device, struct vk_sync *sync); + +#endif /* PVR_G5P_REUSE_H */ diff --git a/src/imagination/vulkan/pvr_job_render.h b/src/imagination/vulkan/pvr_job_render.h index 85636aad7b8..7ea26811b1c 100644 --- a/src/imagination/vulkan/pvr_job_render.h +++ b/src/imagination/vulkan/pvr_job_render.h @@ -71,6 +71,9 @@ struct pvr_rt_mtile_info { * (although it doesn't subclass). */ struct pvr_render_job { + /* G5P submit reuse: what this submission's last job signals (pvr_g5p_reuse.h) */ + struct pvr_winsys_stamp g5p_stamp; + struct { bool run_frag : 1; bool geometry_terminate : 1; diff --git a/src/imagination/vulkan/pvr_queue.h b/src/imagination/vulkan/pvr_queue.h index 0064a4a221f..7e3ab8cb633 100644 --- a/src/imagination/vulkan/pvr_queue.h +++ b/src/imagination/vulkan/pvr_queue.h @@ -40,6 +40,12 @@ struct pvr_queue { struct vk_sync *next_job_wait_sync[PVR_JOB_TYPE_MAX]; /* G5P: last_job_signal_sync[i] is known complete (zero-timeout wait succeeded); cleared whenever it is replaced */ bool g5p_src_done[PVR_JOB_TYPE_MAX]; + + /* G5P submit reuse (pvr_g5p_reuse.h): this queue's index, its stamp + * timeline (0: none) and the last point its jobs signal */ + uint32_t g5p_index; + uint32_t g5p_stamp_syncobj; + uint64_t g5p_stamp_issued; }; VK_DEFINE_HANDLE_CASTS(pvr_queue, vk.base, VkQueue, VK_OBJECT_TYPE_QUEUE) diff --git a/src/imagination/vulkan/winsys/powervr/pvr_drm.c b/src/imagination/vulkan/winsys/powervr/pvr_drm.c index e6f809d98b0..15fabe4f1d8 100644 --- a/src/imagination/vulkan/winsys/powervr/pvr_drm.c +++ b/src/imagination/vulkan/winsys/powervr/pvr_drm.c @@ -22,9 +22,10 @@ */ #include +#include +#include #include #include -#include #include #include @@ -124,6 +125,98 @@ static bool pvr_drm_winsys_is_lost(struct pvr_winsys *ws) return p_atomic_read(&drm_ws->lost); } +/* G5P submit reuse: a queue's stamp timeline (pvr_g5p_reuse.h) */ +static VkResult pvr_drm_stamp_create(struct pvr_winsys *base, uint32_t *syncobj_out) +{ + if (drmSyncobjCreate(base->render_fd, 0, syncobj_out)) + return vk_errorf(NULL, VK_ERROR_OUT_OF_HOST_MEMORY, "stamp syncobj: %s", strerror(errno)); + return VK_SUCCESS; +} + +static void pvr_drm_stamp_destroy(struct pvr_winsys *base, uint32_t syncobj) +{ + drmSyncobjDestroy(base->render_fd, syncobj); +} + +static VkResult pvr_drm_stamp_query(struct pvr_winsys *base, const uint32_t *syncobjs, + uint64_t *points, uint32_t count) +{ + /* flags 0: the last point up to which the timeline has signalled */ + if (drmSyncobjQuery(base->render_fd, (uint32_t *)syncobjs, points, count)) + return vk_errorf(NULL, VK_ERROR_DEVICE_LOST, "stamp query: %s", strerror(errno)); + return VK_SUCCESS; +} + +static VkResult pvr_drm_syncs_reset(struct pvr_winsys *base, struct vk_sync *const *syncs, + uint32_t count) +{ + struct pvr_drm_winsys *ws = to_pvr_drm_winsys(base); + uint32_t handles[64]; + uint32_t n = 0; + + for (uint32_t i = 0; i < count; i++) { + struct vk_drm_syncobj *sync = vk_sync_as_drm_syncobj(syncs[i]); + assert(sync && !(syncs[i]->flags & VK_SYNC_IS_TIMELINE)); + /* the tag names the job a fence error belongs to: a fresh sync has none */ + if (ws->base.job_sync_size && syncs[i]->type->size == ws->base.job_sync_size) + ((struct pvr_drm_job_sync *)syncs[i])->job_type = NULL; + handles[n++] = sync->syncobj; + if (n == ARRAY_SIZE(handles) || i + 1 == count) { + if (drmSyncobjReset(base->render_fd, handles, n)) + return vk_errorf(NULL, VK_ERROR_OUT_OF_HOST_MEMORY, "syncobj reset: %s", strerror(errno)); + n = 0; + } + } + return VK_SUCCESS; +} + +/* A null job's merge syncobj: a reset one kept from an earlier null job if + * there is one (4 or more dirty ones are reset together first), else a new + * one. + * Without G5P reuse: always a new one, as before. */ +uint32_t pvr_drm_tmp_syncobj_get(struct pvr_drm_winsys *ws, int *err_out) +{ + uint32_t syncobj = 0; + + *err_out = 0; + if (ws->g5p_reuse) { + simple_mtx_lock(&ws->tmp_syncobjs.lock); + /* reset in batches of 4 or more: below that, a new one joins */ + if (!ws->tmp_syncobjs.nclean && ws->tmp_syncobjs.ndirty >= 4 && + !drmSyncobjReset(ws->base.render_fd, ws->tmp_syncobjs.dirty, ws->tmp_syncobjs.ndirty)) { + memcpy(ws->tmp_syncobjs.clean, ws->tmp_syncobjs.dirty, + ws->tmp_syncobjs.ndirty * sizeof(uint32_t)); + ws->tmp_syncobjs.nclean = ws->tmp_syncobjs.ndirty; + ws->tmp_syncobjs.ndirty = 0; + } + if (ws->tmp_syncobjs.nclean) + syncobj = ws->tmp_syncobjs.clean[--ws->tmp_syncobjs.nclean]; + simple_mtx_unlock(&ws->tmp_syncobjs.lock); + if (syncobj) + return syncobj; + } + if (drmSyncobjCreate(ws->base.render_fd, 0, &syncobj)) { + *err_out = errno; + return 0; + } + return syncobj; +} + +void pvr_drm_tmp_syncobj_put(struct pvr_drm_winsys *ws, uint32_t syncobj) +{ + if (ws->g5p_reuse) { + simple_mtx_lock(&ws->tmp_syncobjs.lock); + if (ws->tmp_syncobjs.ndirty < ARRAY_SIZE(ws->tmp_syncobjs.dirty)) { + ws->tmp_syncobjs.dirty[ws->tmp_syncobjs.ndirty++] = syncobj; + syncobj = 0; + } + simple_mtx_unlock(&ws->tmp_syncobjs.lock); + if (!syncobj) + return; + } + drmSyncobjDestroy(ws->base.render_fd, syncobj); +} + static VkResult pvr_drm_check_job_fences(struct pvr_winsys *base, uint32_t count, const struct vk_sync_wait *waits) @@ -198,6 +291,8 @@ static void pvr_drm_winsys_destroy(struct pvr_winsys *ws) if (drm_ws->query_only) goto out; + pvr_drm_import_cache_finish(drm_ws); + pvr_winsys_helper_free_static_memory(drm_ws->general_vma, drm_ws->pds_vma, drm_ws->usc_vma); @@ -213,6 +308,12 @@ static void pvr_drm_winsys_destroy(struct pvr_winsys *ws) util_sparse_array_finish(&drm_ws->bo_map); u_rwlock_destroy(&drm_ws->dmabuf_bo_lock); + for (uint32_t i = 0; i < drm_ws->tmp_syncobjs.nclean; i++) + drmSyncobjDestroy(ws->render_fd, drm_ws->tmp_syncobjs.clean[i]); + for (uint32_t i = 0; i < drm_ws->tmp_syncobjs.ndirty; i++) + drmSyncobjDestroy(ws->render_fd, drm_ws->tmp_syncobjs.dirty[i]); + simple_mtx_destroy(&drm_ws->tmp_syncobjs.lock); + simple_mtx_destroy(&drm_ws->import_cache.lock); vk_free(ws->alloc, drm_ws); } @@ -532,6 +633,7 @@ static const struct pvr_winsys_ops drm_winsys_ops = { .heap_free = pvr_drm_winsys_heap_free, .vma_map = pvr_drm_winsys_vma_map, .vma_unmap = pvr_drm_winsys_vma_unmap, + .vma_map_imported = pvr_drm_winsys_vma_map_imported, .vma_map_mirror = pvr_drm_winsys_vma_map_mirror, .free_list_create = pvr_drm_winsys_free_list_create, .free_list_destroy = pvr_drm_winsys_free_list_destroy, @@ -547,6 +649,10 @@ static const struct pvr_winsys_ops drm_winsys_ops = { .transfer_ctx_destroy = pvr_drm_winsys_transfer_ctx_destroy, .transfer_submit = pvr_drm_winsys_transfer_submit, .null_job_submit = pvr_drm_winsys_null_job_submit, + .stamp_create = pvr_drm_stamp_create, + .stamp_destroy = pvr_drm_stamp_destroy, + .stamp_query = pvr_drm_stamp_query, + .syncs_reset = pvr_drm_syncs_reset, }; struct pvr_static_data_area_description { @@ -857,6 +963,10 @@ VkResult pvr_drm_winsys_create(const int render_fd, util_sparse_array_init(&drm_ws->bo_map, sizeof(struct pvr_drm_winsys_bo), 512); + simple_mtx_init(&drm_ws->tmp_syncobjs.lock, mtx_plain); + drm_ws->import_cache.enabled = debug_get_bool_option("PVR_IMPORT_CACHE", true); + simple_mtx_init(&drm_ws->import_cache.lock, mtx_plain); + list_inithead(&drm_ws->import_cache.lru); result = pvr_drm_get_gpu_info(drm_ws, &gpu_info); if (result != VK_SUCCESS) @@ -871,10 +981,12 @@ VkResult pvr_drm_winsys_create(const int render_fd, PVR_HAS_FEATURE(&info, apple_g9p); drm_ws->base.job_sync_size = drm_ws->job_fence_errors ? sizeof(struct pvr_drm_job_sync) : 0; - /* The A8's physical-device winsys answers device queries only: no VM context, heaps or static memory, so - * a process holds one VM (its VkDevice's) instead of two. powervr's A8 variant gives every VM the hardware - * object window, the zero and data pages and a user slot, and a client that never submits needs none. */ - drm_ws->query_only = query_only && PVR_HAS_FEATURE(&info, apple_g4p); + drm_ws->g5p_reuse = drm_ws->is_g5p && pvr_g5p_reuse_option(); + /* The physical-device winsys on A8 through A10 only answers device + * queries. The VkDevice's winsys owns the VM, heaps and static memory. + */ + drm_ws->query_only = query_only && (drm_ws->is_g5p || + PVR_HAS_FEATURE(&info, apple_g9p)); } if (drm_ws->query_only) { *ws_out = &drm_ws->base; @@ -933,6 +1045,8 @@ VkResult pvr_drm_winsys_create(const int render_fd, VK_ERROR_UNKNOWN); err_util_sparse_array_finish_bo_map: + simple_mtx_destroy(&drm_ws->import_cache.lock); + simple_mtx_destroy(&drm_ws->tmp_syncobjs.lock); util_sparse_array_finish(&drm_ws->bo_map); u_rwlock_destroy(&drm_ws->dmabuf_bo_lock); diff --git a/src/imagination/vulkan/winsys/powervr/pvr_drm.h b/src/imagination/vulkan/winsys/powervr/pvr_drm.h index 956c4232fcd..0e1c58aab49 100644 --- a/src/imagination/vulkan/winsys/powervr/pvr_drm.h +++ b/src/imagination/vulkan/winsys/powervr/pvr_drm.h @@ -30,8 +30,10 @@ #include "pvr_winsys.h" #include "vk_drm_syncobj.h" +#include "util/list.h" #include "util/macros.h" #include "util/rwlock.h" +#include "util/simple_mtx.h" #include "util/sparse_array.h" /* Identify job out-fences without inspecting ordinary poll waits. */ @@ -51,7 +53,7 @@ struct pvr_drm_winsys { uint64_t bvnc; bool is_g5p; - /* pvr_drm_winsys_create(query_only) on the A8: device queries only, no VM context */ + /* The physical-device winsys only answers device queries. */ bool query_only; uint64_t caps; int madvise_warning; @@ -104,8 +106,35 @@ struct pvr_drm_winsys { struct pvr_winsys_vma *general_vma; uint32_t vm_context; + + /* G5P submit reuse (PVR_G5P_REUSE, default on): a null job's merge + * syncobj is reset and kept instead of created and destroyed per job + * (pvr_drm_job_null.c). The dirty ones are reset together. + */ + bool g5p_reuse; + struct { + simple_mtx_t lock; + uint32_t clean[16], dirty[16]; + uint32_t nclean, ndirty; + } tmp_syncobjs; + + /* Cached whole-BO GPU VM mappings for imported buffers (PVR_IMPORT_CACHE, + * default on). Keyed by GEM handle via drm_bo->import_vma; active imports + * keep the mapping alive and recently released imports stay in the LRU so + * rotating surface pools avoid per-frame VM_MAP/VM_UNMAP TLB invalidations. + */ + struct { + bool enabled; + simple_mtx_t lock; + struct list_head lru; + uint32_t lru_count; + uint64_t lru_bytes; + } import_cache; }; +uint32_t pvr_drm_tmp_syncobj_get(struct pvr_drm_winsys *ws, int *err_out); +void pvr_drm_tmp_syncobj_put(struct pvr_drm_winsys *ws, uint32_t syncobj); + #define PVR_DRM_CAP_RETRY_ENOMEM DRM_PVR_CAP_RETRY_ENOMEM #define PVR_DRM_CAP_BO_MADVISE DRM_PVR_CAP_BO_MADVISE int pvr_drm_ioctl_retry(struct pvr_drm_winsys *ws, unsigned long request, void *args); diff --git a/src/imagination/vulkan/winsys/powervr/pvr_drm_bo.c b/src/imagination/vulkan/winsys/powervr/pvr_drm_bo.c index f63c5e7a138..6fed42d2684 100644 --- a/src/imagination/vulkan/winsys/powervr/pvr_drm_bo.c +++ b/src/imagination/vulkan/winsys/powervr/pvr_drm_bo.c @@ -182,6 +182,7 @@ static void pvr_drm_buffer_release(struct pvr_drm_winsys_bo *drm_bo) * since otherwise there is a chance the application creates another BO in * a different thread and gets the same array entry, causing a race. */ + assert(!drm_bo->import_vma); memset(drm_bo, 0, sizeof(*drm_bo)); g9p_bolog('-', handle, 0); @@ -495,25 +496,31 @@ VkResult pvr_drm_heap_alloc_carveout(struct pvr_winsys_heap *const heap, return result; } +static bool pvr_drm_import_cache_flush_lru(struct pvr_drm_winsys *drm_ws); + VkResult pvr_drm_winsys_heap_alloc(struct pvr_winsys_heap *heap, uint64_t size, uint64_t alignment, struct pvr_winsys_vma **const vma_out) { - const struct pvr_drm_winsys *const drm_ws = to_pvr_drm_winsys(heap->ws); + struct pvr_drm_winsys *const drm_ws = to_pvr_drm_winsys(heap->ws); struct pvr_drm_winsys_vma *drm_vma; VkResult result; - drm_vma = vk_alloc(drm_ws->base.alloc, - sizeof(*drm_vma), - 8, - VK_SYSTEM_ALLOCATION_SCOPE_DEVICE); + drm_vma = vk_zalloc(drm_ws->base.alloc, + sizeof(*drm_vma), + 8, + VK_SYSTEM_ALLOCATION_SCOPE_DEVICE); if (!drm_vma) { result = vk_error(NULL, VK_ERROR_OUT_OF_HOST_MEMORY); goto err_out; } result = pvr_winsys_helper_heap_alloc(heap, size, alignment, &drm_vma->base); + if (result != VK_SUCCESS && heap == &drm_ws->general_heap.base && + pvr_drm_import_cache_flush_lru(drm_ws)) { + result = pvr_winsys_helper_heap_alloc(heap, size, alignment, &drm_vma->base); + } if (result != VK_SUCCESS) goto err_free_vma; @@ -537,6 +544,12 @@ void pvr_drm_winsys_heap_free(struct pvr_winsys_vma *vma) /* A vma with an existing device mapping should not be freed. */ assert(!drm_vma->base.bo); + if (drm_vma->is_import_view) { + assert(!drm_vma->import_vma); + vk_free(drm_ws->base.alloc, drm_vma); + return; + } + /* Check if we are dealing with carveout address range. */ if (vma->dev_addr.addr >= carveout_addr && vma->dev_addr.addr < @@ -642,8 +655,26 @@ VkResult pvr_drm_winsys_vma_map_mirror(struct pvr_winsys_vma *vma, struct pvr_wi return VK_SUCCESS; } +static void pvr_drm_import_vma_put(struct pvr_drm_winsys *drm_ws, + struct pvr_drm_import_vma *entry); + void pvr_drm_winsys_vma_unmap(struct pvr_winsys_vma *vma) { + struct pvr_drm_winsys_vma *const drm_vma = to_pvr_drm_winsys_vma(vma); + + /* Address should be mapped. */ + assert(vma->bo); + + if (drm_vma->import_vma) { + struct pvr_drm_winsys *const drm_ws = to_pvr_drm_winsys(vma->bo->ws); + + assert(!vma->mirrored); + pvr_drm_import_vma_put(drm_ws, drm_vma->import_vma); + drm_vma->import_vma = NULL; + vma->bo = NULL; + return; + } + if (vma->mirrored) { /* two mappings of the buffer (pvr_drm_winsys_vma_map_mirror()): the second one first, then the first * through the ordinary path */ @@ -667,9 +698,6 @@ void pvr_drm_winsys_vma_unmap(struct pvr_winsys_vma *vma) .size = vma->mapped_size, }; - /* Address should be mapped. */ - assert(vma->bo); - if (p_atomic_read(&drm_ws->lost)) { if (drmIoctl(drm_ws->base.render_fd, DRM_IOCTL_PVR_VM_UNMAP, &args) && p_atomic_cmpxchg(&drm_ws->unmap_loss_reported, 0, 1) == 0) { @@ -690,3 +718,332 @@ void pvr_drm_winsys_vma_unmap(struct pvr_winsys_vma *vma) vma->bo = NULL; } + +#define PVR_DRM_IMPORT_CACHE_MAX_LRU_ENTRIES 32U +#define PVR_DRM_IMPORT_CACHE_MAX_LRU_BYTES (64ULL * 1024 * 1024) + +struct pvr_drm_import_vma { + struct list_head lru_link; + struct pvr_drm_winsys_bo *drm_bo; + struct pvr_winsys_vma *backing_vma; + uint32_t use_count; + bool in_lru; +}; + +static void +pvr_drm_import_vma_destroy(struct pvr_drm_winsys *drm_ws, + struct pvr_drm_import_vma *entry) +{ + assert(entry->use_count == 0); + assert(!entry->in_lru); + + pvr_drm_winsys_vma_unmap(entry->backing_vma); + pvr_drm_winsys_heap_free(entry->backing_vma); + vk_free(drm_ws->base.alloc, entry); +} + +static void +pvr_drm_import_vma_put(struct pvr_drm_winsys *drm_ws, + struct pvr_drm_import_vma *entry) +{ + struct list_head to_evict; + list_inithead(&to_evict); + + simple_mtx_lock(&drm_ws->import_cache.lock); + + assert(entry->use_count > 0); + entry->use_count--; + + if (entry->use_count == 0) { + assert(!entry->in_lru); + if (entry->backing_vma->mapped_size > PVR_DRM_IMPORT_CACHE_MAX_LRU_BYTES) { + assert(entry->drm_bo->import_vma == entry); + entry->drm_bo->import_vma = NULL; + list_addtail(&entry->lru_link, &to_evict); + } else { + list_add(&entry->lru_link, &drm_ws->import_cache.lru); + entry->in_lru = true; + drm_ws->import_cache.lru_count++; + drm_ws->import_cache.lru_bytes += entry->backing_vma->mapped_size; + + while (drm_ws->import_cache.lru_count > + PVR_DRM_IMPORT_CACHE_MAX_LRU_ENTRIES || + drm_ws->import_cache.lru_bytes > + PVR_DRM_IMPORT_CACHE_MAX_LRU_BYTES) { + assert(!list_is_empty(&drm_ws->import_cache.lru)); + struct pvr_drm_import_vma *victim = + list_last_entry(&drm_ws->import_cache.lru, + struct pvr_drm_import_vma, + lru_link); + assert(victim->use_count == 0); + assert(victim->in_lru); + assert(victim->drm_bo->import_vma == victim); + + list_del(&victim->lru_link); + victim->in_lru = false; + drm_ws->import_cache.lru_count--; + drm_ws->import_cache.lru_bytes -= victim->backing_vma->mapped_size; + victim->drm_bo->import_vma = NULL; + list_addtail(&victim->lru_link, &to_evict); + } + } + } + + simple_mtx_unlock(&drm_ws->import_cache.lock); + + list_for_each_entry_safe (struct pvr_drm_import_vma, victim, &to_evict, lru_link) { + list_del(&victim->lru_link); + pvr_drm_import_vma_destroy(drm_ws, victim); + } +} + +static bool +pvr_drm_import_cache_flush_lru(struct pvr_drm_winsys *drm_ws) +{ + struct list_head to_evict; + list_inithead(&to_evict); + + simple_mtx_lock(&drm_ws->import_cache.lock); + list_for_each_entry_safe (struct pvr_drm_import_vma, entry, + &drm_ws->import_cache.lru, lru_link) { + assert(entry->use_count == 0); + assert(entry->in_lru); + assert(entry->drm_bo->import_vma == entry); + + list_del(&entry->lru_link); + entry->in_lru = false; + drm_ws->import_cache.lru_count--; + drm_ws->import_cache.lru_bytes -= entry->backing_vma->mapped_size; + entry->drm_bo->import_vma = NULL; + list_addtail(&entry->lru_link, &to_evict); + } + assert(drm_ws->import_cache.lru_count == 0); + assert(drm_ws->import_cache.lru_bytes == 0); + simple_mtx_unlock(&drm_ws->import_cache.lock); + + bool evicted = !list_is_empty(&to_evict); + list_for_each_entry_safe (struct pvr_drm_import_vma, victim, &to_evict, lru_link) { + list_del(&victim->lru_link); + pvr_drm_import_vma_destroy(drm_ws, victim); + } + + return evicted; +} + +void pvr_drm_import_cache_finish(struct pvr_drm_winsys *drm_ws) +{ + pvr_drm_import_cache_flush_lru(drm_ws); +} + +static VkResult +pvr_drm_vma_alloc_and_map(struct pvr_winsys_heap *heap, + struct pvr_winsys_bo *bo, + uint64_t offset, + uint64_t size, + uint64_t alignment, + struct pvr_winsys_vma **const vma_out, + pvr_dev_addr_t *const dev_addr_out) +{ + const uint64_t virt_size = size + (offset & (heap->page_size - 1)); + struct pvr_winsys_vma *vma; + VkResult result; + + result = pvr_drm_winsys_heap_alloc(heap, virt_size, alignment, &vma); + if (result != VK_SUCCESS) + return result; + + result = pvr_drm_winsys_vma_map(vma, bo, offset, size, dev_addr_out); + if (result != VK_SUCCESS) { + pvr_drm_winsys_heap_free(vma); + return result; + } + + *vma_out = vma; + return VK_SUCCESS; +} + +static bool +pvr_drm_import_vma_matches(const struct pvr_drm_import_vma *entry, + const struct pvr_winsys_heap *heap, + uint64_t offset, + uint64_t phys_page_offset, + uint64_t aligned_virt_size, + uint64_t alignment) +{ + const struct pvr_winsys_vma *backing = entry->backing_vma; + + if (backing->heap != heap) + return false; + if (phys_page_offset > backing->mapped_size || + aligned_virt_size > backing->mapped_size - phys_page_offset) + return false; + if (alignment > heap->page_size && + ((backing->dev_addr.addr + offset) & (alignment - 1)) != 0) + return false; + + return true; +} + +VkResult pvr_drm_winsys_vma_map_imported(struct pvr_winsys_heap *heap, + struct pvr_winsys_bo *bo, + uint64_t offset, + uint64_t size, + uint64_t alignment, + struct pvr_winsys_vma **const vma_out, + pvr_dev_addr_t *const dev_addr_out) +{ + struct pvr_drm_winsys_bo *const drm_bo = to_pvr_drm_winsys_bo(bo); + struct pvr_drm_winsys *const drm_ws = to_pvr_drm_winsys(bo->ws); + const uint64_t page_size = heap->page_size; + + if (!drm_ws->import_cache.enabled || heap != &drm_ws->general_heap.base || + size == 0) { + return pvr_drm_vma_alloc_and_map(heap, bo, offset, size, alignment, + vma_out, dev_addr_out); + } + + if (offset > bo->size || size > bo->size - offset) + return vk_error(NULL, VK_ERROR_MEMORY_MAP_FAILED); + + const uint64_t virt_offset = offset & (page_size - 1); + const uint64_t phys_page_offset = offset - virt_offset; + const uint64_t aligned_virt_size = ALIGN_POT(virt_offset + size, page_size); + const uint64_t map_size = bo->size & ~(page_size - 1); + + if (ALIGN_POT(offset + size, page_size) > bo->size || map_size == 0) + return vk_error(NULL, VK_ERROR_MEMORY_MAP_FAILED); + + struct pvr_drm_winsys_vma *drm_vma = + vk_zalloc(drm_ws->base.alloc, + sizeof(*drm_vma), + 8, + VK_SYSTEM_ALLOCATION_SCOPE_DEVICE); + if (!drm_vma) + return vk_error(NULL, VK_ERROR_OUT_OF_HOST_MEMORY); + + struct pvr_drm_import_vma *entry = NULL; + bool fallback = false; + + simple_mtx_lock(&drm_ws->import_cache.lock); + if (drm_bo->import_vma) { + if (pvr_drm_import_vma_matches(drm_bo->import_vma, heap, offset, + phys_page_offset, aligned_virt_size, + alignment)) { + entry = drm_bo->import_vma; + if (entry->use_count == 0) { + assert(entry->in_lru); + list_del(&entry->lru_link); + entry->in_lru = false; + drm_ws->import_cache.lru_count--; + drm_ws->import_cache.lru_bytes -= entry->backing_vma->mapped_size; + } + entry->use_count++; + } else { + fallback = true; + } + } + simple_mtx_unlock(&drm_ws->import_cache.lock); + + if (fallback) { + vk_free(drm_ws->base.alloc, drm_vma); + return pvr_drm_vma_alloc_and_map(heap, bo, offset, size, alignment, + vma_out, dev_addr_out); + } + + if (!entry) { + struct pvr_drm_import_vma *new_entry = + vk_zalloc(drm_ws->base.alloc, + sizeof(*new_entry), + 8, + VK_SYSTEM_ALLOCATION_SCOPE_DEVICE); + if (!new_entry) { + vk_free(drm_ws->base.alloc, drm_vma); + return vk_error(NULL, VK_ERROR_OUT_OF_HOST_MEMORY); + } + + VkResult result = pvr_drm_winsys_heap_alloc(heap, map_size, page_size, + &new_entry->backing_vma); + if (result != VK_SUCCESS) { + vk_free(drm_ws->base.alloc, new_entry); + vk_free(drm_ws->base.alloc, drm_vma); + return result; + } + + result = pvr_drm_winsys_vma_map(new_entry->backing_vma, bo, 0, map_size, + NULL); + if (result != VK_SUCCESS && pvr_drm_import_cache_flush_lru(drm_ws)) { + result = pvr_drm_winsys_vma_map(new_entry->backing_vma, bo, 0, + map_size, NULL); + } + if (result != VK_SUCCESS) { + pvr_drm_winsys_heap_free(new_entry->backing_vma); + vk_free(drm_ws->base.alloc, new_entry); + vk_free(drm_ws->base.alloc, drm_vma); + return result; + } + + new_entry->drm_bo = drm_bo; + + if (!pvr_drm_import_vma_matches(new_entry, heap, offset, phys_page_offset, + aligned_virt_size, alignment)) { + pvr_drm_import_vma_destroy(drm_ws, new_entry); + vk_free(drm_ws->base.alloc, drm_vma); + return pvr_drm_vma_alloc_and_map(heap, bo, offset, size, alignment, + vma_out, dev_addr_out); + } + + struct pvr_drm_import_vma *discard = NULL; + + simple_mtx_lock(&drm_ws->import_cache.lock); + if (!drm_bo->import_vma) { + new_entry->use_count = 1; + drm_bo->import_vma = new_entry; + entry = new_entry; + } else if (pvr_drm_import_vma_matches(drm_bo->import_vma, heap, offset, + phys_page_offset, aligned_virt_size, + alignment)) { + entry = drm_bo->import_vma; + if (entry->use_count == 0) { + assert(entry->in_lru); + list_del(&entry->lru_link); + entry->in_lru = false; + drm_ws->import_cache.lru_count--; + drm_ws->import_cache.lru_bytes -= entry->backing_vma->mapped_size; + } + entry->use_count++; + discard = new_entry; + } else { + discard = new_entry; + fallback = true; + } + simple_mtx_unlock(&drm_ws->import_cache.lock); + + if (discard) + pvr_drm_import_vma_destroy(drm_ws, discard); + + if (fallback) { + vk_free(drm_ws->base.alloc, drm_vma); + return pvr_drm_vma_alloc_and_map(heap, bo, offset, size, alignment, + vma_out, dev_addr_out); + } + } + + const uint64_t view_mapped_size = + entry->backing_vma->mapped_size - phys_page_offset; + + drm_vma->base.heap = heap; + drm_vma->base.bo = bo; + drm_vma->base.bo_offset = offset; + drm_vma->base.dev_addr = + PVR_DEV_ADDR_OFFSET(entry->backing_vma->dev_addr, phys_page_offset); + drm_vma->base.size = view_mapped_size; + drm_vma->base.mapped_size = view_mapped_size; + drm_vma->import_vma = entry; + drm_vma->is_import_view = true; + + if (dev_addr_out) + *dev_addr_out = PVR_DEV_ADDR_OFFSET(drm_vma->base.dev_addr, virt_offset); + + *vma_out = &drm_vma->base; + return VK_SUCCESS; +} diff --git a/src/imagination/vulkan/winsys/powervr/pvr_drm_bo.h b/src/imagination/vulkan/winsys/powervr/pvr_drm_bo.h index 323ce6e4d04..a3ab85a7441 100644 --- a/src/imagination/vulkan/winsys/powervr/pvr_drm_bo.h +++ b/src/imagination/vulkan/winsys/powervr/pvr_drm_bo.h @@ -36,6 +36,8 @@ struct definitions *******************************************/ +struct pvr_drm_import_vma; + struct pvr_drm_winsys_bo { struct pvr_winsys_bo base; uint32_t ref_count; @@ -44,10 +46,20 @@ struct pvr_drm_winsys_bo { uint8_t count_kind; uint32_t handle; uint64_t flags; + + /* Cached whole-BO GPU VM mapping for imported buffers (protected by + * drm_ws->import_cache.lock). While non-NULL, the entry holds a ref on this + * BO so the kernel keeps the GEM handle stably bound to the same dma-buf. + */ + struct pvr_drm_import_vma *import_vma; }; struct pvr_drm_winsys_vma { struct pvr_winsys_vma base; + /* Non-NULL while bound to a cached imported-BO mapping. */ + struct pvr_drm_import_vma *import_vma; + /* True for view VMAs returned by vma_map_imported() (do not heap_free). */ + bool is_import_view; }; /******************************************* @@ -91,6 +103,14 @@ VkResult pvr_drm_winsys_vma_map(struct pvr_winsys_vma *vma, uint64_t size, pvr_dev_addr_t *dev_addr_out); void pvr_drm_winsys_vma_unmap(struct pvr_winsys_vma *vma); +VkResult pvr_drm_winsys_vma_map_imported(struct pvr_winsys_heap *heap, + struct pvr_winsys_bo *bo, + uint64_t offset, + uint64_t size, + uint64_t alignment, + struct pvr_winsys_vma **vma_out, + pvr_dev_addr_t *dev_addr_out); +void pvr_drm_import_cache_finish(struct pvr_drm_winsys *drm_ws); VkResult pvr_drm_winsys_vma_map_mirror(struct pvr_winsys_vma *vma, struct pvr_winsys_bo *bo); /******************************************* diff --git a/src/imagination/vulkan/winsys/powervr/pvr_drm_job_compute.c b/src/imagination/vulkan/winsys/powervr/pvr_drm_job_compute.c index 98990b7cf18..ba03ebc7725 100644 --- a/src/imagination/vulkan/winsys/powervr/pvr_drm_job_compute.c +++ b/src/imagination/vulkan/winsys/powervr/pvr_drm_job_compute.c @@ -181,7 +181,7 @@ VkResult pvr_drm_winsys_compute_submit( struct pvr_drm_winsys_compute_ctx *drm_ctx = to_pvr_drm_winsys_compute_ctx((struct pvr_winsys_compute_ctx *)ctx); - struct drm_pvr_sync_op sync_ops[2]; + struct drm_pvr_sync_op sync_ops[3]; /* wait, signal, stamp */ struct drm_pvr_job job_args = { .type = DRM_PVR_JOB_TYPE_COMPUTE, .context_handle = drm_ctx->handle, @@ -219,6 +219,15 @@ VkResult pvr_drm_winsys_compute_submit( }; } + if (submit_info->stamp.syncobj) { + assert(submit_info->stamp.point); + sync_ops[job_args.sync_ops.count++] = (struct drm_pvr_sync_op){ + .handle = submit_info->stamp.syncobj, + .flags = DRM_PVR_SYNC_OP_FLAG_SIGNAL | DRM_PVR_SYNC_OP_FLAG_HANDLE_TYPE_TIMELINE_SYNCOBJ, + .value = submit_info->stamp.point, + }; + } + /* Returns VK_ERROR_OUT_OF_DEVICE_MEMORY to match pvrsrv. */ const VkResult result = pvr_drm_submit_jobs(drm_ws, &args, "compute"); if (result == VK_SUCCESS) diff --git a/src/imagination/vulkan/winsys/powervr/pvr_drm_job_null.c b/src/imagination/vulkan/winsys/powervr/pvr_drm_job_null.c index d4b70229850..3791968a7c9 100644 --- a/src/imagination/vulkan/winsys/powervr/pvr_drm_job_null.c +++ b/src/imagination/vulkan/winsys/powervr/pvr_drm_job_null.c @@ -71,9 +71,16 @@ VkResult pvr_drm_winsys_null_job_submit(struct pvr_winsys *ws, return VK_SUCCESS; } - ret = drmSyncobjCreate(drm_ws->base.render_fd, - wait_count == 0 ? DRM_SYNCOBJ_CREATE_SIGNALED : 0, - &tmp_syncobj); + if (wait_count == 0) { + ret = drmSyncobjCreate(drm_ws->base.render_fd, DRM_SYNCOBJ_CREATE_SIGNALED, &tmp_syncobj); + } else { + /* G5P reuse: a kept merge syncobj, reset (no fence, point 0) */ + int err; + + tmp_syncobj = pvr_drm_tmp_syncobj_get((struct pvr_drm_winsys *)drm_ws, &err); + ret = tmp_syncobj ? 0 : -1; + errno = err; + } if (ret) { return vk_errorf(NULL, VK_ERROR_OUT_OF_DEVICE_MEMORY, @@ -122,6 +129,9 @@ VkResult pvr_drm_winsys_null_job_submit(struct pvr_winsys *ws, } out_destroy_tmp_syncobj: - drmSyncobjDestroy(drm_ws->base.render_fd, tmp_syncobj); + if (wait_count == 0) + drmSyncobjDestroy(drm_ws->base.render_fd, tmp_syncobj); + else + pvr_drm_tmp_syncobj_put((struct pvr_drm_winsys *)drm_ws, tmp_syncobj); return result; } diff --git a/src/imagination/vulkan/winsys/powervr/pvr_drm_job_render.c b/src/imagination/vulkan/winsys/powervr/pvr_drm_job_render.c index ddf43186e42..dd360bc10f0 100644 --- a/src/imagination/vulkan/winsys/powervr/pvr_drm_job_render.c +++ b/src/imagination/vulkan/winsys/powervr/pvr_drm_job_render.c @@ -486,7 +486,8 @@ VkResult pvr_drm_winsys_render_submit( const struct pvr_drm_winsys_rt_dataset *drm_rt_dataset = to_pvr_drm_winsys_rt_dataset(submit_info->rt_dataset); - struct drm_pvr_sync_op geom_sync_ops[2], pr_sync_ops[1], frag_sync_ops[3]; + /* one more each for a stamp (pvr_winsys_stamp) */ + struct drm_pvr_sync_op geom_sync_ops[3], pr_sync_ops[2], frag_sync_ops[4]; unsigned num_geom_syncs = 0, num_pr_syncs = 0, num_frag_syncs = 0; uint32_t geom_to_pr_syncobj; @@ -619,6 +620,25 @@ VkResult pvr_drm_winsys_render_submit( } } + /* The stamp: signalled by the submission's last job, so its point is + * reached only when every job of this submission (and, as the timeline's + * points only grow, of every earlier stamped one) has completed. */ + if (submit_info->stamp.syncobj) { + const struct drm_pvr_sync_op stamp = { + .handle = submit_info->stamp.syncobj, + .flags = DRM_PVR_SYNC_OP_FLAG_SIGNAL | DRM_PVR_SYNC_OP_FLAG_HANDLE_TYPE_TIMELINE_SYNCOBJ, + .value = submit_info->stamp.point, + }; + + assert(submit_info->stamp.point); + if (submit_info->has_fragment_job) + frag_sync_ops[num_frag_syncs++] = stamp; + else if (pr_state->fw_stream_len) + pr_sync_ops[num_pr_syncs++] = stamp; + else + geom_sync_ops[num_geom_syncs++] = stamp; + } + jobs_args[0].sync_ops.count = num_geom_syncs; /* the fragment job follows the partial-render job, or the geometry job without one */ if (pr_state->fw_stream_len) diff --git a/src/imagination/vulkan/winsys/pvr_winsys.h b/src/imagination/vulkan/winsys/pvr_winsys.h index 79dac01e211..9804626fedc 100644 --- a/src/imagination/vulkan/winsys/pvr_winsys.h +++ b/src/imagination/vulkan/winsys/pvr_winsys.h @@ -39,6 +39,7 @@ #include "pvr_rogue_fw.h" #include "pvr_types.h" #include "util/macros.h" +#include "util/u_debug.h" #include "util/vma.h" #include "vk_sync.h" #include "vk_sync_timeline.h" @@ -300,7 +301,26 @@ struct pvr_winsys_transfer_submit_info { struct pvr_g5p_dispatch_log; +/* G5P: a queue's completion stamp. A job that carries one also signals point + * `point` of the timeline syncobj `syncobj` when it completes, in the same + * submission (no extra ioctl). The points of a queue only grow, so the + * timeline's value is the last point up to which every stamped job of that + * queue has completed (pvr_g5p_reuse.h). syncobj 0: no stamp. */ +struct pvr_winsys_stamp { + uint32_t syncobj; + uint64_t point; +}; + +/* PVR_G5P_REUSE=0 turns G5P submit reuse off (the stamps, the command stream + * block cache, the sync pool and the null job's kept merge syncobj), for a + * same-boot A/B. Read at winsys and device creation only. */ +static inline bool pvr_g5p_reuse_option(void) +{ + return debug_get_bool_option("PVR_G5P_REUSE", true); +} + struct pvr_winsys_compute_submit_info { + struct pvr_winsys_stamp stamp; const struct pvr_g5p_dispatch_log *g5p_log; unsigned g5p_log_count; uint32_t frame_num; @@ -322,6 +342,8 @@ struct pvr_winsys_compute_submit_info { }; struct pvr_winsys_render_submit_info { + /* signalled by the submission's last job (fragment if any, else geometry) */ + struct pvr_winsys_stamp stamp; struct pvr_winsys_rt_dataset *rt_dataset; uint8_t rt_data_idx; @@ -414,6 +436,16 @@ struct pvr_winsys_ops { uint64_t size, pvr_dev_addr_t *dev_addr_out); void (*vma_unmap)(struct pvr_winsys_vma *vma); + /* Optional: allocate/reuse a cached GPU VM mapping for an imported BO + * (keyed by GEM handle, reference-counted with a small LRU). Released via + * vma_unmap() + heap_free() like an ordinary VMA. NULL where unsupported. */ + VkResult (*vma_map_imported)(struct pvr_winsys_heap *heap, + struct pvr_winsys_bo *bo, + uint64_t offset, + uint64_t size, + uint64_t alignment, + struct pvr_winsys_vma **vma_out, + pvr_dev_addr_t *dev_addr_out); /* The whole of bo mapped twice back to back, at vma->dev_addr and vma->dev_addr + bo->size (the vma holds * at least 2 * bo->size): the A8's region-header ring, which AGXG4P maps that way (kext 0x847d738, resources * created with flag 0x200000) so a frame's slice can run past the ring's end. Two VM_MAPs of one buffer; @@ -479,6 +511,18 @@ struct pvr_winsys_ops { struct vk_sync_wait *waits, uint32_t wait_count, struct vk_sync_signal *signal_sync); + + /* Optional (G5P submit reuse, pvr_g5p_reuse.h); NULL where unsupported. */ + /* a timeline syncobj for a queue's stamps, at point 0 */ + VkResult (*stamp_create)(struct pvr_winsys *ws, uint32_t *syncobj_out); + void (*stamp_destroy)(struct pvr_winsys *ws, uint32_t syncobj); + /* the completed point of each of count stamp timelines, one ioctl */ + VkResult (*stamp_query)(struct pvr_winsys *ws, const uint32_t *syncobjs, uint64_t *points, + uint32_t count); + /* Return count binary job syncs (of the device's sync type, from this + * winsys) to their initial state, as vk_sync_create(..., 0) leaves them: no + * fence, no job tag. One ioctl for all of them. */ + VkResult (*syncs_reset)(struct pvr_winsys *ws, struct vk_sync *const *syncs, uint32_t count); }; struct pvr_winsys {