From 149e945ad45b650fd468b6ff14919e38586f5555 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jos=C3=A9=20Roberto=20de=20Souza?= Date: Thu, 9 Feb 2023 08:44:04 -0800 Subject: [PATCH] anv: Implement Xe functions to create and destroy VM MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Also using the vm_id to create gem buffers. Signed-off-by: José Roberto de Souza Reviewed-by: Lionel Landwerlin Part-of: --- src/intel/vulkan/anv_device.c | 37 ++++++++++++++++++++++++--- src/intel/vulkan/anv_kmd_backend.c | 7 ++++++ src/intel/vulkan/anv_kmd_backend.h | 3 +++ src/intel/vulkan/anv_private.h | 5 +++- src/intel/vulkan/meson.build | 2 ++ src/intel/vulkan/xe/anv_device.c | 47 +++++++++++++++++++++++++++++++++++ src/intel/vulkan/xe/anv_device.h | 32 ++++++++++++++++++++++++ src/intel/vulkan/xe/anv_kmd_backend.c | 2 +- 8 files changed, 130 insertions(+), 5 deletions(-) create mode 100644 src/intel/vulkan/xe/anv_device.c create mode 100644 src/intel/vulkan/xe/anv_device.h diff --git a/src/intel/vulkan/anv_device.c b/src/intel/vulkan/anv_device.c index 9db9343..a875c31 100644 --- a/src/intel/vulkan/anv_device.c +++ b/src/intel/vulkan/anv_device.c @@ -59,6 +59,7 @@ #include "perf/intel_perf.h" #include "i915/anv_device.h" +#include "xe/anv_device.h" #include "genxml/gen7_pack.h" #include "genxml/genX_bits.h" @@ -3163,6 +3164,36 @@ static struct intel_mapped_pinned_buffer_alloc aux_map_allocator = { .free = intel_aux_map_buffer_free, }; +static VkResult +anv_device_setup_context_or_vm(struct anv_device *device, + const VkDeviceCreateInfo *pCreateInfo, + const uint32_t num_queues) +{ + switch (anv_kmd_type_get(device)) { + case INTEL_KMD_TYPE_I915: + return anv_i915_device_setup_context(device, pCreateInfo, num_queues); + case INTEL_KMD_TYPE_XE: + return anv_xe_device_setup_vm(device); + default: + unreachable("Missing"); + return VK_ERROR_UNKNOWN; + } +} + +static bool +anv_device_destroy_context_or_vm(struct anv_device *device) +{ + switch (anv_kmd_type_get(device)) { + case INTEL_KMD_TYPE_I915: + return intel_gem_destroy_context(device->fd, device->context_id); + case INTEL_KMD_TYPE_XE: + return anv_xe_device_destroy_vm(device); + default: + unreachable("Missing"); + return false; + } +} + VkResult anv_CreateDevice( VkPhysicalDevice physicalDevice, const VkDeviceCreateInfo* pCreateInfo, @@ -3259,7 +3290,7 @@ VkResult anv_CreateDevice( for (uint32_t i = 0; i < pCreateInfo->queueCreateInfoCount; i++) num_queues += pCreateInfo->pQueueCreateInfos[i].queueCount; - result = anv_i915_device_setup_context(device, pCreateInfo, num_queues); + result = anv_device_setup_context_or_vm(device, pCreateInfo, num_queues); if (result != VK_SUCCESS) goto fail_fd; @@ -3636,7 +3667,7 @@ VkResult anv_CreateDevice( anv_queue_finish(&device->queues[i]); vk_free(&device->vk.alloc, device->queues); fail_context_id: - intel_gem_destroy_context(device->fd, device->context_id); + anv_device_destroy_context_or_vm(device); fail_fd: close(device->fd); fail_device: @@ -3728,7 +3759,7 @@ void anv_DestroyDevice( anv_queue_finish(&device->queues[i]); vk_free(&device->vk.alloc, device->queues); - intel_gem_destroy_context(device->fd, device->context_id); + anv_device_destroy_context_or_vm(device); if (INTEL_DEBUG(DEBUG_BATCH)) { for (unsigned i = 0; i < pdevice->queue.family_count; i++) diff --git a/src/intel/vulkan/anv_kmd_backend.c b/src/intel/vulkan/anv_kmd_backend.c index 57f49de..f72d87d 100644 --- a/src/intel/vulkan/anv_kmd_backend.c +++ b/src/intel/vulkan/anv_kmd_backend.c @@ -24,6 +24,7 @@ #include #include "anv_kmd_backend.h" +#include "anv_private.h" const struct anv_kmd_backend * anv_kmd_backend_get(enum intel_kmd_type type) @@ -39,3 +40,9 @@ anv_kmd_backend_get(enum intel_kmd_type type) return NULL; } } + +inline enum intel_kmd_type +anv_kmd_type_get(struct anv_device *device) +{ + return device->info->kmd_type; +} diff --git a/src/intel/vulkan/anv_kmd_backend.h b/src/intel/vulkan/anv_kmd_backend.h index 2f86ba7..97479f1 100644 --- a/src/intel/vulkan/anv_kmd_backend.h +++ b/src/intel/vulkan/anv_kmd_backend.h @@ -68,6 +68,9 @@ struct anv_kmd_backend { const struct anv_kmd_backend *anv_kmd_backend_get(enum intel_kmd_type type); +enum intel_kmd_type +anv_kmd_type_get(struct anv_device *device); + /* Internal functions, should only be called by anv_kmd_backend_get() */ const struct anv_kmd_backend *anv_i915_kmd_backend_get(void); const struct anv_kmd_backend *anv_xe_kmd_backend_get(void); diff --git a/src/intel/vulkan/anv_private.h b/src/intel/vulkan/anv_private.h index 71ba1de..278e384 100644 --- a/src/intel/vulkan/anv_private.h +++ b/src/intel/vulkan/anv_private.h @@ -1127,7 +1127,10 @@ struct anv_device { const struct intel_device_info * info; const struct anv_kmd_backend * kmd_backend; struct isl_device isl_dev; - uint32_t context_id; + union { + uint32_t context_id; /* i915 */ + uint32_t vm_id; /* Xe */ + }; int fd; pthread_mutex_t vma_mutex; diff --git a/src/intel/vulkan/meson.build b/src/intel/vulkan/meson.build index d3a7af4..ce87c99 100644 --- a/src/intel/vulkan/meson.build +++ b/src/intel/vulkan/meson.build @@ -138,6 +138,8 @@ libanv_files = files( 'layers/anv_doom64.c', 'layers/anv_hitman3.c', 'xe/anv_kmd_backend.c', + 'xe/anv_device.c', + 'xe/anv_device.h', 'anv_allocator.c', 'anv_android.h', 'anv_batch_chain.c', diff --git a/src/intel/vulkan/xe/anv_device.c b/src/intel/vulkan/xe/anv_device.c new file mode 100644 index 0000000..8b85c39 --- /dev/null +++ b/src/intel/vulkan/xe/anv_device.c @@ -0,0 +1,47 @@ +/* + * Copyright © 2023 Intel Corporation + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +#include "xe/anv_device.h" +#include "anv_private.h" + +#include "drm-uapi/xe_drm.h" + +bool anv_xe_device_destroy_vm(struct anv_device *device) +{ + struct drm_xe_vm_destroy destroy = { + .vm_id = device->vm_id, + }; + return intel_ioctl(device->fd, DRM_IOCTL_XE_VM_DESTROY, &destroy) == 0; +} + +VkResult anv_xe_device_setup_vm(struct anv_device *device) +{ + struct drm_xe_vm_create create = { + .flags = DRM_XE_VM_CREATE_SCRATCH_PAGE, + }; + if (intel_ioctl(device->fd, DRM_IOCTL_XE_VM_CREATE, &create) != 0) + return vk_errorf(device, VK_ERROR_INITIALIZATION_FAILED, + "vm creation failed"); + + device->vm_id = create.vm_id; + return VK_SUCCESS; +} diff --git a/src/intel/vulkan/xe/anv_device.h b/src/intel/vulkan/xe/anv_device.h new file mode 100644 index 0000000..03d222a --- /dev/null +++ b/src/intel/vulkan/xe/anv_device.h @@ -0,0 +1,32 @@ +/* + * Copyright © 2023 Intel Corporation + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +#pragma once + +#include + +#include "vulkan/vulkan_core.h" + +struct anv_device; + +bool anv_xe_device_destroy_vm(struct anv_device *device); +VkResult anv_xe_device_setup_vm(struct anv_device *device); diff --git a/src/intel/vulkan/xe/anv_kmd_backend.c b/src/intel/vulkan/xe/anv_kmd_backend.c index 21a9a3b..44661cf 100644 --- a/src/intel/vulkan/xe/anv_kmd_backend.c +++ b/src/intel/vulkan/xe/anv_kmd_backend.c @@ -32,7 +32,7 @@ xe_gem_create(struct anv_device *device, enum anv_bo_alloc_flags alloc_flags) { struct drm_xe_gem_create gem_create = { - .vm_id = 0,/* TODO: create VM */ + .vm_id = device->vm_id, .size = size, }; for (uint16_t i = 0; i < regions_count; i++) -- 2.7.4