1 /*
2  * Copyright © 2016 Red Hat
3  * based on intel anv code:
4  * Copyright © 2015 Intel Corporation
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the next
14  * paragraph) shall be included in all copies or substantial portions of the
15  * Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23  * IN THE SOFTWARE.
24  */
25 
26 #include "util/macros.h"
27 #include "radv_debug.h"
28 #include "radv_meta.h"
29 #include "radv_private.h"
30 #include "vk_fence.h"
31 #include "vk_semaphore.h"
32 #include "vk_util.h"
33 #include "wsi_common.h"
34 
35 static VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
radv_wsi_proc_addr(VkPhysicalDevice physicalDevice,const char * pName)36 radv_wsi_proc_addr(VkPhysicalDevice physicalDevice, const char *pName)
37 {
38    RADV_FROM_HANDLE(radv_physical_device, pdevice, physicalDevice);
39    return vk_instance_get_proc_addr_unchecked(&pdevice->instance->vk, pName);
40 }
41 
42 static void
radv_wsi_set_memory_ownership(VkDevice _device,VkDeviceMemory _mem,VkBool32 ownership)43 radv_wsi_set_memory_ownership(VkDevice _device, VkDeviceMemory _mem, VkBool32 ownership)
44 {
45    RADV_FROM_HANDLE(radv_device, device, _device);
46    RADV_FROM_HANDLE(radv_device_memory, mem, _mem);
47 
48    if (device->use_global_bo_list) {
49       device->ws->buffer_make_resident(device->ws, mem->bo, ownership);
50    }
51 }
52 
53 static VkQueue
radv_wsi_get_prime_blit_queue(VkDevice _device)54 radv_wsi_get_prime_blit_queue(VkDevice _device)
55 {
56    RADV_FROM_HANDLE(radv_device, device, _device);
57 
58    if (device->private_sdma_queue != VK_NULL_HANDLE)
59       return vk_queue_to_handle(&device->private_sdma_queue->vk);
60 
61    if (device->physical_device->rad_info.chip_class >= GFX9 &&
62        !(device->physical_device->instance->debug_flags & RADV_DEBUG_NO_DMA_BLIT)) {
63 
64       device->physical_device->vk_queue_to_radv[device->physical_device->num_queues++] = RADV_QUEUE_TRANSFER;
65       const VkDeviceQueueCreateInfo queue_create = {
66          .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
67          .queueFamilyIndex = device->physical_device->num_queues - 1,
68          .queueCount = 1,
69       };
70 
71       device->private_sdma_queue = vk_zalloc(&device->vk.alloc, sizeof(struct radv_queue), 8,
72                                              VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
73 
74       VkResult result = radv_queue_init(device, device->private_sdma_queue, 0, &queue_create, NULL);
75       if (result == VK_SUCCESS) {
76          /* Remove the queue from our queue list because it'll be cleared manually
77           * in radv_DestroyDevice.
78           */
79          list_delinit(&device->private_sdma_queue->vk.link);
80          return vk_queue_to_handle(&device->private_sdma_queue->vk);
81       } else {
82          vk_free(&device->vk.alloc, device->private_sdma_queue);
83          device->private_sdma_queue = VK_NULL_HANDLE;
84       }
85    }
86    return VK_NULL_HANDLE;
87 }
88 
89 VkResult
radv_init_wsi(struct radv_physical_device * physical_device)90 radv_init_wsi(struct radv_physical_device *physical_device)
91 {
92    VkResult result =
93       wsi_device_init(&physical_device->wsi_device, radv_physical_device_to_handle(physical_device),
94                       radv_wsi_proc_addr, &physical_device->instance->vk.alloc,
95                       physical_device->master_fd, &physical_device->instance->dri_options, false);
96    if (result != VK_SUCCESS)
97       return result;
98 
99    physical_device->wsi_device.supports_modifiers = physical_device->rad_info.chip_class >= GFX9;
100    physical_device->wsi_device.set_memory_ownership = radv_wsi_set_memory_ownership;
101    physical_device->wsi_device.get_buffer_blit_queue = radv_wsi_get_prime_blit_queue;
102    physical_device->wsi_device.signal_semaphore_with_memory = true;
103    physical_device->wsi_device.signal_fence_with_memory = true;
104 
105    wsi_device_setup_syncobj_fd(&physical_device->wsi_device, physical_device->local_fd);
106 
107    physical_device->vk.wsi_device = &physical_device->wsi_device;
108 
109    return VK_SUCCESS;
110 }
111 
112 void
radv_finish_wsi(struct radv_physical_device * physical_device)113 radv_finish_wsi(struct radv_physical_device *physical_device)
114 {
115    physical_device->vk.wsi_device = NULL;
116    wsi_device_finish(&physical_device->wsi_device, &physical_device->instance->vk.alloc);
117 }
118 
119 VKAPI_ATTR VkResult VKAPI_CALL
radv_QueuePresentKHR(VkQueue _queue,const VkPresentInfoKHR * pPresentInfo)120 radv_QueuePresentKHR(VkQueue _queue, const VkPresentInfoKHR *pPresentInfo)
121 {
122    RADV_FROM_HANDLE(radv_queue, queue, _queue);
123    return wsi_common_queue_present(&queue->device->physical_device->wsi_device,
124                                    radv_device_to_handle(queue->device), _queue,
125                                    queue->vk.queue_family_index, pPresentInfo);
126 }
127