1 /*
2 * Copyright © 2015 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is 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
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include "anv_private.h"
25 #include "anv_measure.h"
26 #include "wsi_common.h"
27 #include "vk_util.h"
28
29 static PFN_vkVoidFunction
anv_wsi_proc_addr(VkPhysicalDevice physicalDevice,const char * pName)30 anv_wsi_proc_addr(VkPhysicalDevice physicalDevice, const char *pName)
31 {
32 ANV_FROM_HANDLE(anv_physical_device, pdevice, physicalDevice);
33 return vk_instance_get_proc_addr_unchecked(&pdevice->instance->vk, pName);
34 }
35
36 static void
anv_wsi_signal_semaphore_for_memory(VkDevice _device,VkSemaphore _semaphore,VkDeviceMemory _memory)37 anv_wsi_signal_semaphore_for_memory(VkDevice _device,
38 VkSemaphore _semaphore,
39 VkDeviceMemory _memory)
40 {
41 ANV_FROM_HANDLE(anv_device, device, _device);
42 ANV_FROM_HANDLE(anv_semaphore, semaphore, _semaphore);
43 ANV_FROM_HANDLE(anv_device_memory, memory, _memory);
44
45 /* Put a BO semaphore with the image BO in the temporary. For BO binary
46 * semaphores, we always set EXEC_OBJECT_WRITE so this creates a WaR
47 * hazard with the display engine's read to ensure that no one writes to
48 * the image before the read is complete.
49 */
50 anv_semaphore_reset_temporary(device, semaphore);
51
52 struct anv_semaphore_impl *impl = &semaphore->temporary;
53 impl->type = ANV_SEMAPHORE_TYPE_WSI_BO;
54 impl->bo = anv_bo_ref(memory->bo);
55 }
56
57 static void
anv_wsi_signal_fence_for_memory(VkDevice _device,VkFence _fence,VkDeviceMemory _memory)58 anv_wsi_signal_fence_for_memory(VkDevice _device,
59 VkFence _fence,
60 VkDeviceMemory _memory)
61 {
62 ANV_FROM_HANDLE(anv_device, device, _device);
63 ANV_FROM_HANDLE(anv_fence, fence, _fence);
64 ANV_FROM_HANDLE(anv_device_memory, memory, _memory);
65
66 /* Put a BO fence with the image BO in the temporary. For BO fences, we
67 * always just wait until the BO isn't busy and reads from the BO should
68 * count as busy.
69 */
70 anv_fence_reset_temporary(device, fence);
71
72 struct anv_fence_impl *impl = &fence->temporary;
73 impl->type = ANV_FENCE_TYPE_WSI_BO;
74 impl->bo.bo = anv_bo_ref(memory->bo);
75 impl->bo.state = ANV_BO_FENCE_STATE_SUBMITTED;
76 }
77
78 VkResult
anv_init_wsi(struct anv_physical_device * physical_device)79 anv_init_wsi(struct anv_physical_device *physical_device)
80 {
81 VkResult result;
82
83 result = wsi_device_init(&physical_device->wsi_device,
84 anv_physical_device_to_handle(physical_device),
85 anv_wsi_proc_addr,
86 &physical_device->instance->vk.alloc,
87 physical_device->master_fd,
88 &physical_device->instance->dri_options,
89 false);
90 if (result != VK_SUCCESS)
91 return result;
92
93 physical_device->wsi_device.supports_modifiers = true;
94 physical_device->wsi_device.signal_semaphore_for_memory =
95 anv_wsi_signal_semaphore_for_memory;
96 physical_device->wsi_device.signal_fence_for_memory =
97 anv_wsi_signal_fence_for_memory;
98
99 physical_device->vk.wsi_device = &physical_device->wsi_device;
100
101 return VK_SUCCESS;
102 }
103
104 void
anv_finish_wsi(struct anv_physical_device * physical_device)105 anv_finish_wsi(struct anv_physical_device *physical_device)
106 {
107 physical_device->vk.wsi_device = NULL;
108 wsi_device_finish(&physical_device->wsi_device,
109 &physical_device->instance->vk.alloc);
110 }
111
anv_QueuePresentKHR(VkQueue _queue,const VkPresentInfoKHR * pPresentInfo)112 VkResult anv_QueuePresentKHR(
113 VkQueue _queue,
114 const VkPresentInfoKHR* pPresentInfo)
115 {
116 ANV_FROM_HANDLE(anv_queue, queue, _queue);
117 struct anv_device *device = queue->device;
118
119 if (device->debug_frame_desc) {
120 device->debug_frame_desc->frame_id++;
121 if (!device->info.has_llc) {
122 intel_clflush_range(device->debug_frame_desc,
123 sizeof(*device->debug_frame_desc));
124 }
125 }
126
127 if (device->has_thread_submit &&
128 pPresentInfo->waitSemaphoreCount > 0) {
129 /* Make sure all of the dependency semaphores have materialized when
130 * using a threaded submission.
131 */
132 VK_MULTIALLOC(ma);
133 VK_MULTIALLOC_DECL(&ma, uint64_t, values,
134 pPresentInfo->waitSemaphoreCount);
135 VK_MULTIALLOC_DECL(&ma, uint32_t, syncobjs,
136 pPresentInfo->waitSemaphoreCount);
137
138 if (!vk_multialloc_alloc(&ma, &device->vk.alloc,
139 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND))
140 return vk_error(queue, VK_ERROR_OUT_OF_HOST_MEMORY);
141
142 uint32_t wait_count = 0;
143 for (uint32_t i = 0; i < pPresentInfo->waitSemaphoreCount; i++) {
144 ANV_FROM_HANDLE(anv_semaphore, semaphore, pPresentInfo->pWaitSemaphores[i]);
145 struct anv_semaphore_impl *impl =
146 semaphore->temporary.type != ANV_SEMAPHORE_TYPE_NONE ?
147 &semaphore->temporary : &semaphore->permanent;
148
149 if (impl->type == ANV_SEMAPHORE_TYPE_DUMMY)
150 continue;
151 assert(impl->type == ANV_SEMAPHORE_TYPE_DRM_SYNCOBJ);
152 syncobjs[wait_count] = impl->syncobj;
153 values[wait_count] = 0;
154 wait_count++;
155 }
156
157 int ret = 0;
158 if (wait_count > 0) {
159 ret =
160 anv_gem_syncobj_timeline_wait(device,
161 syncobjs, values, wait_count,
162 anv_get_absolute_timeout(INT64_MAX),
163 true /* wait_all */,
164 true /* wait_materialize */);
165 }
166
167 vk_free(&device->vk.alloc, values);
168
169 if (ret)
170 return vk_error(queue, VK_ERROR_DEVICE_LOST);
171 }
172
173 VkResult result = wsi_common_queue_present(&device->physical->wsi_device,
174 anv_device_to_handle(queue->device),
175 _queue, 0,
176 pPresentInfo);
177
178 for (uint32_t i = 0; i < pPresentInfo->waitSemaphoreCount; i++) {
179 ANV_FROM_HANDLE(anv_semaphore, semaphore, pPresentInfo->pWaitSemaphores[i]);
180 /* From the Vulkan 1.0.53 spec:
181 *
182 * "If the import is temporary, the implementation must restore the
183 * semaphore to its prior permanent state after submitting the next
184 * semaphore wait operation."
185 */
186 anv_semaphore_reset_temporary(queue->device, semaphore);
187 }
188
189 return result;
190 }
191