1 /*
2 * Copyright © 2023 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/mman.h>
25
26 #include "anv_private.h"
27
28 #include "i915/anv_batch_chain.h"
29
30 #include "drm-uapi/i915_drm.h"
31 #include "intel/common/i915/intel_gem.h"
32
33 static int
i915_gem_set_caching(struct anv_device * device,uint32_t gem_handle,uint32_t caching)34 i915_gem_set_caching(struct anv_device *device,
35 uint32_t gem_handle, uint32_t caching)
36 {
37 struct drm_i915_gem_caching gem_caching = {
38 .handle = gem_handle,
39 .caching = caching,
40 };
41
42 return intel_ioctl(device->fd, DRM_IOCTL_I915_GEM_SET_CACHING, &gem_caching);
43 }
44
45 static uint32_t
i915_gem_create(struct anv_device * device,const struct intel_memory_class_instance ** regions,uint16_t num_regions,uint64_t size,enum anv_bo_alloc_flags alloc_flags,uint64_t * actual_size)46 i915_gem_create(struct anv_device *device,
47 const struct intel_memory_class_instance **regions,
48 uint16_t num_regions, uint64_t size,
49 enum anv_bo_alloc_flags alloc_flags,
50 uint64_t *actual_size)
51 {
52 if (unlikely(!device->info->mem.use_class_instance)) {
53 assert(num_regions == 1 &&
54 device->physical->sys.region == regions[0]);
55
56 struct drm_i915_gem_create gem_create = {
57 .size = size,
58 };
59 if (intel_ioctl(device->fd, DRM_IOCTL_I915_GEM_CREATE, &gem_create))
60 return 0;
61
62 if ((alloc_flags & ANV_BO_ALLOC_HOST_CACHED_COHERENT) == ANV_BO_ALLOC_HOST_CACHED_COHERENT) {
63 /* We don't want to change these defaults if it's going to be shared
64 * with another process.
65 */
66 assert(!(alloc_flags & ANV_BO_ALLOC_EXTERNAL));
67
68 /* Regular objects are created I915_CACHING_CACHED on LLC platforms and
69 * I915_CACHING_NONE on non-LLC platforms. For many internal state
70 * objects, we'd rather take the snooping overhead than risk forgetting
71 * a CLFLUSH somewhere. Userptr objects are always created as
72 * I915_CACHING_CACHED, which on non-LLC means snooped so there's no
73 * need to do this there.
74 */
75 if (device->info->has_caching_uapi && !device->info->has_llc)
76 i915_gem_set_caching(device, gem_create.handle, I915_CACHING_CACHED);
77 }
78
79 *actual_size = gem_create.size;
80 return gem_create.handle;
81 }
82
83 struct drm_i915_gem_memory_class_instance i915_regions[2];
84 assert(num_regions <= ARRAY_SIZE(i915_regions));
85
86 for (uint16_t i = 0; i < num_regions; i++) {
87 i915_regions[i].memory_class = regions[i]->klass;
88 i915_regions[i].memory_instance = regions[i]->instance;
89 }
90
91 uint32_t flags = 0;
92 if (alloc_flags & (ANV_BO_ALLOC_MAPPED | ANV_BO_ALLOC_LOCAL_MEM_CPU_VISIBLE) &&
93 !(alloc_flags & ANV_BO_ALLOC_NO_LOCAL_MEM))
94 if (device->physical->vram_non_mappable.size > 0)
95 flags |= I915_GEM_CREATE_EXT_FLAG_NEEDS_CPU_ACCESS;
96
97 struct drm_i915_gem_create_ext_memory_regions ext_regions = {
98 .num_regions = num_regions,
99 .regions = (uintptr_t)i915_regions,
100 };
101 struct drm_i915_gem_create_ext gem_create = {
102 .size = size,
103 .flags = flags,
104 };
105
106 intel_i915_gem_add_ext(&gem_create.extensions,
107 I915_GEM_CREATE_EXT_MEMORY_REGIONS,
108 &ext_regions.base);
109
110 struct drm_i915_gem_create_ext_set_pat set_pat_param = { 0 };
111 if (device->info->has_set_pat_uapi) {
112 /* Set PAT param */
113 set_pat_param.pat_index = anv_device_get_pat_entry(device, alloc_flags)->index;
114 intel_i915_gem_add_ext(&gem_create.extensions,
115 I915_GEM_CREATE_EXT_SET_PAT,
116 &set_pat_param.base);
117 }
118
119 struct drm_i915_gem_create_ext_protected_content protected_param = { 0 };
120 if (alloc_flags & ANV_BO_ALLOC_PROTECTED) {
121 intel_i915_gem_add_ext(&gem_create.extensions,
122 I915_GEM_CREATE_EXT_PROTECTED_CONTENT,
123 &protected_param.base);
124 }
125
126 if (intel_ioctl(device->fd, DRM_IOCTL_I915_GEM_CREATE_EXT, &gem_create))
127 return 0;
128
129 *actual_size = gem_create.size;
130
131 if ((alloc_flags & ANV_BO_ALLOC_HOST_CACHED_COHERENT) == ANV_BO_ALLOC_HOST_CACHED_COHERENT) {
132 /* We don't want to change these defaults if it's going to be shared
133 * with another process.
134 */
135 assert(!(alloc_flags & ANV_BO_ALLOC_EXTERNAL));
136
137 /* Regular objects are created I915_CACHING_CACHED on LLC platforms and
138 * I915_CACHING_NONE on non-LLC platforms. For many internal state
139 * objects, we'd rather take the snooping overhead than risk forgetting
140 * a CLFLUSH somewhere. Userptr objects are always created as
141 * I915_CACHING_CACHED, which on non-LLC means snooped so there's no
142 * need to do this there.
143 */
144 if (device->info->has_caching_uapi && !device->info->has_llc)
145 i915_gem_set_caching(device, gem_create.handle, I915_CACHING_CACHED);
146 }
147
148 return gem_create.handle;
149 }
150
151 static void
i915_gem_close(struct anv_device * device,struct anv_bo * bo)152 i915_gem_close(struct anv_device *device, struct anv_bo *bo)
153 {
154 struct drm_gem_close close = {
155 .handle = bo->gem_handle,
156 };
157
158 intel_ioctl(device->fd, DRM_IOCTL_GEM_CLOSE, &close);
159 }
160
161 static void *
i915_gem_mmap_offset(struct anv_device * device,struct anv_bo * bo,uint64_t size,uint32_t flags)162 i915_gem_mmap_offset(struct anv_device *device, struct anv_bo *bo,
163 uint64_t size, uint32_t flags)
164 {
165 struct drm_i915_gem_mmap_offset gem_mmap = {
166 .handle = bo->gem_handle,
167 .flags = flags,
168 };
169 if (intel_ioctl(device->fd, DRM_IOCTL_I915_GEM_MMAP_OFFSET, &gem_mmap))
170 return MAP_FAILED;
171
172 return mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED,
173 device->fd, gem_mmap.offset);
174 }
175
176 static void *
i915_gem_mmap_legacy(struct anv_device * device,struct anv_bo * bo,uint64_t offset,uint64_t size,uint32_t flags)177 i915_gem_mmap_legacy(struct anv_device *device, struct anv_bo *bo, uint64_t offset,
178 uint64_t size, uint32_t flags)
179 {
180 struct drm_i915_gem_mmap gem_mmap = {
181 .handle = bo->gem_handle,
182 .offset = offset,
183 .size = size,
184 .flags = flags,
185 };
186 if (intel_ioctl(device->fd, DRM_IOCTL_I915_GEM_MMAP, &gem_mmap))
187 return MAP_FAILED;
188
189 return (void *)(uintptr_t) gem_mmap.addr_ptr;
190 }
191
192 static uint32_t
mmap_calc_flags(struct anv_device * device,struct anv_bo * bo)193 mmap_calc_flags(struct anv_device *device, struct anv_bo *bo)
194 {
195 if (device->info->has_local_mem)
196 return I915_MMAP_OFFSET_FIXED;
197
198 uint32_t flags;
199 switch (anv_bo_get_mmap_mode(device, bo)) {
200 case INTEL_DEVICE_INFO_MMAP_MODE_WC:
201 flags = I915_MMAP_WC;
202 break;
203 case INTEL_DEVICE_INFO_MMAP_MODE_UC:
204 unreachable("Missing");
205 default:
206 /* no flags == WB */
207 flags = 0;
208 }
209
210 if (likely(device->physical->info.has_mmap_offset))
211 flags = (flags & I915_MMAP_WC) ? I915_MMAP_OFFSET_WC : I915_MMAP_OFFSET_WB;
212 return flags;
213 }
214
215 static void *
i915_gem_mmap(struct anv_device * device,struct anv_bo * bo,uint64_t offset,uint64_t size)216 i915_gem_mmap(struct anv_device *device, struct anv_bo *bo, uint64_t offset,
217 uint64_t size)
218 {
219 const uint32_t flags = mmap_calc_flags(device, bo);
220
221 if (likely(device->physical->info.has_mmap_offset))
222 return i915_gem_mmap_offset(device, bo, size, flags);
223 return i915_gem_mmap_legacy(device, bo, offset, size, flags);
224 }
225
226 static int
i915_vm_bind(struct anv_device * device,struct anv_sparse_submission * submit)227 i915_vm_bind(struct anv_device *device, struct anv_sparse_submission *submit)
228 {
229 return 0;
230 }
231
232 static int
i915_vm_bind_bo(struct anv_device * device,struct anv_bo * bo)233 i915_vm_bind_bo(struct anv_device *device, struct anv_bo *bo)
234 {
235 return 0;
236 }
237
238 static uint32_t
i915_gem_create_userptr(struct anv_device * device,void * mem,uint64_t size)239 i915_gem_create_userptr(struct anv_device *device, void *mem, uint64_t size)
240 {
241 struct drm_i915_gem_userptr userptr = {
242 .user_ptr = (__u64)((unsigned long) mem),
243 .user_size = size,
244 .flags = 0,
245 };
246
247 if (device->physical->info.has_userptr_probe)
248 userptr.flags |= I915_USERPTR_PROBE;
249
250 int ret = intel_ioctl(device->fd, DRM_IOCTL_I915_GEM_USERPTR, &userptr);
251 if (ret == -1)
252 return 0;
253
254 return userptr.handle;
255 }
256
257 static uint32_t
i915_bo_alloc_flags_to_bo_flags(struct anv_device * device,enum anv_bo_alloc_flags alloc_flags)258 i915_bo_alloc_flags_to_bo_flags(struct anv_device *device,
259 enum anv_bo_alloc_flags alloc_flags)
260 {
261 struct anv_physical_device *pdevice = device->physical;
262
263 uint64_t bo_flags = EXEC_OBJECT_PINNED;
264
265 if (!(alloc_flags & ANV_BO_ALLOC_32BIT_ADDRESS))
266 bo_flags |= EXEC_OBJECT_SUPPORTS_48B_ADDRESS;
267
268 if (((alloc_flags & ANV_BO_ALLOC_CAPTURE) ||
269 INTEL_DEBUG(DEBUG_CAPTURE_ALL)) &&
270 pdevice->has_exec_capture)
271 bo_flags |= EXEC_OBJECT_CAPTURE;
272
273 if (alloc_flags & ANV_BO_ALLOC_IMPLICIT_WRITE) {
274 assert(alloc_flags & ANV_BO_ALLOC_IMPLICIT_SYNC);
275 bo_flags |= EXEC_OBJECT_WRITE;
276 }
277
278 if (!(alloc_flags & ANV_BO_ALLOC_IMPLICIT_SYNC) && pdevice->has_exec_async)
279 bo_flags |= EXEC_OBJECT_ASYNC;
280
281 return bo_flags;
282 }
283
284 const struct anv_kmd_backend *
anv_i915_kmd_backend_get(void)285 anv_i915_kmd_backend_get(void)
286 {
287 static const struct anv_kmd_backend i915_backend = {
288 .gem_create = i915_gem_create,
289 .gem_create_userptr = i915_gem_create_userptr,
290 .gem_close = i915_gem_close,
291 .gem_mmap = i915_gem_mmap,
292 .vm_bind = i915_vm_bind,
293 .vm_bind_bo = i915_vm_bind_bo,
294 .vm_unbind_bo = i915_vm_bind_bo,
295 .execute_simple_batch = i915_execute_simple_batch,
296 .execute_trtt_batch = i915_execute_trtt_batch,
297 .queue_exec_locked = i915_queue_exec_locked,
298 .queue_exec_trace = i915_queue_exec_trace,
299 .bo_alloc_flags_to_bo_flags = i915_bo_alloc_flags_to_bo_flags,
300 };
301 return &i915_backend;
302 }
303