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 <sys/ioctl.h>
25 #include <sys/types.h>
26 #include <sys/mman.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <unistd.h>
30 #include <fcntl.h>
31
32 #include "anv_private.h"
33 #include "common/intel_gem.h"
34
35 #include "i915/anv_gem.h"
36
37 /**
38 * On error, \a timeout_ns holds the remaining time.
39 */
40 int
anv_gem_wait(struct anv_device * device,uint32_t gem_handle,int64_t * timeout_ns)41 anv_gem_wait(struct anv_device *device, uint32_t gem_handle, int64_t *timeout_ns)
42 {
43 switch (device->info->kmd_type) {
44 case INTEL_KMD_TYPE_I915:
45 return anv_i915_gem_wait(device, gem_handle, timeout_ns);
46 case INTEL_KMD_TYPE_XE:
47 return -1;
48 default:
49 unreachable("missing");
50 return -1;
51 }
52 }
53
54 /** Return -1 on error. */
55 int
anv_gem_get_tiling(struct anv_device * device,uint32_t gem_handle)56 anv_gem_get_tiling(struct anv_device *device, uint32_t gem_handle)
57 {
58 switch (device->info->kmd_type) {
59 case INTEL_KMD_TYPE_I915:
60 return anv_i915_gem_get_tiling(device, gem_handle);
61 case INTEL_KMD_TYPE_XE:
62 return -1;
63 default:
64 unreachable("missing");
65 return -1;
66 }
67 }
68
69 int
anv_gem_set_tiling(struct anv_device * device,uint32_t gem_handle,uint32_t stride,uint32_t tiling)70 anv_gem_set_tiling(struct anv_device *device,
71 uint32_t gem_handle, uint32_t stride, uint32_t tiling)
72 {
73 switch (device->info->kmd_type) {
74 case INTEL_KMD_TYPE_I915:
75 return anv_i915_gem_set_tiling(device, gem_handle, stride, tiling);
76 case INTEL_KMD_TYPE_XE:
77 return 0;
78 default:
79 unreachable("missing");
80 return -1;
81 }
82 }
83
84 int
anv_gem_handle_to_fd(struct anv_device * device,uint32_t gem_handle)85 anv_gem_handle_to_fd(struct anv_device *device, uint32_t gem_handle)
86 {
87 struct drm_prime_handle args = {
88 .handle = gem_handle,
89 .flags = DRM_CLOEXEC | DRM_RDWR,
90 };
91
92 int ret = intel_ioctl(device->fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &args);
93 if (ret == -1)
94 return -1;
95
96 return args.fd;
97 }
98
99 uint32_t
anv_gem_fd_to_handle(struct anv_device * device,int fd)100 anv_gem_fd_to_handle(struct anv_device *device, int fd)
101 {
102 struct drm_prime_handle args = {
103 .fd = fd,
104 };
105
106 int ret = intel_ioctl(device->fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &args);
107 if (ret == -1)
108 return 0;
109
110 return args.handle;
111 }
112
113 VkResult
anv_gem_import_bo_alloc_flags_to_bo_flags(struct anv_device * device,struct anv_bo * bo,enum anv_bo_alloc_flags alloc_flags,uint32_t * bo_flags)114 anv_gem_import_bo_alloc_flags_to_bo_flags(struct anv_device *device,
115 struct anv_bo *bo,
116 enum anv_bo_alloc_flags alloc_flags,
117 uint32_t *bo_flags)
118 {
119 switch (device->info->kmd_type) {
120 case INTEL_KMD_TYPE_I915:
121 return anv_i915_gem_import_bo_alloc_flags_to_bo_flags(device, bo,
122 alloc_flags,
123 bo_flags);
124 case INTEL_KMD_TYPE_XE:
125 *bo_flags = device->kmd_backend->bo_alloc_flags_to_bo_flags(device, alloc_flags);
126 return VK_SUCCESS;
127 default:
128 unreachable("missing");
129 return VK_ERROR_UNKNOWN;
130 }
131 }
132
anv_stub_kmd_backend_get(void)133 const struct anv_kmd_backend *anv_stub_kmd_backend_get(void)
134 {
135 return NULL;
136 }
137