ttm_bo_vm.c revision 1.27 1 /* $NetBSD: ttm_bo_vm.c,v 1.27 2024/06/23 00:49:20 riastradh Exp $ */
2
3 /*-
4 * Copyright (c) 2014 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Taylor R. Campbell.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /**************************************************************************
33 *
34 * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
35 * All Rights Reserved.
36 *
37 * Permission is hereby granted, free of charge, to any person obtaining a
38 * copy of this software and associated documentation files (the
39 * "Software"), to deal in the Software without restriction, including
40 * without limitation the rights to use, copy, modify, merge, publish,
41 * distribute, sub license, and/or sell copies of the Software, and to
42 * permit persons to whom the Software is furnished to do so, subject to
43 * the following conditions:
44 *
45 * The above copyright notice and this permission notice (including the
46 * next paragraph) shall be included in all copies or substantial portions
47 * of the Software.
48 *
49 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
50 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
51 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
52 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
53 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
54 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
55 * USE OR OTHER DEALINGS IN THE SOFTWARE.
56 *
57 **************************************************************************/
58 /*
59 * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
60 */
61
62 #include <sys/cdefs.h>
63 __KERNEL_RCSID(0, "$NetBSD: ttm_bo_vm.c,v 1.27 2024/06/23 00:49:20 riastradh Exp $");
64
65 #include <sys/types.h>
66
67 #include <uvm/uvm.h>
68 #include <uvm/uvm_extern.h>
69 #include <uvm/uvm_fault.h>
70
71 #include <linux/bitops.h>
72
73 #include <drm/drm_vma_manager.h>
74
75 #include <ttm/ttm_bo_driver.h>
76
77 static int ttm_bo_uvm_lookup(struct ttm_bo_device *, unsigned long,
78 unsigned long, struct ttm_buffer_object **);
79
80 void
81 ttm_bo_uvm_reference(struct uvm_object *uobj)
82 {
83 struct ttm_buffer_object *const bo = container_of(uobj,
84 struct ttm_buffer_object, uvmobj);
85
86 (void)ttm_bo_get(bo);
87 }
88
89 void
90 ttm_bo_uvm_detach(struct uvm_object *uobj)
91 {
92 struct ttm_buffer_object *bo = container_of(uobj,
93 struct ttm_buffer_object, uvmobj);
94
95 ttm_bo_put(bo);
96 }
97
98 static int
99 ttm_bo_vm_fault_idle(struct ttm_buffer_object *bo, struct uvm_faultinfo *vmf)
100 {
101 int err, ret = 0;
102
103 if (__predict_true(!bo->moving))
104 goto out_unlock;
105
106 /*
107 * Quick non-stalling check for idle.
108 */
109 if (dma_fence_is_signaled(bo->moving))
110 goto out_clear;
111
112 /*
113 * If possible, avoid waiting for GPU with mmap_sem
114 * held.
115 */
116 if (1) { /* always retriable in NetBSD */
117 ret = ERESTART;
118
119 ttm_bo_get(bo);
120 uvmfault_unlockall(vmf, vmf->entry->aref.ar_amap, NULL);
121 (void) dma_fence_wait(bo->moving, true);
122 dma_resv_unlock(bo->base.resv);
123 ttm_bo_put(bo);
124 goto out_unlock;
125 }
126
127 /*
128 * Ordinary wait.
129 */
130 err = dma_fence_wait(bo->moving, true);
131 if (__predict_false(err != 0)) {
132 ret = (err != -ERESTARTSYS) ? EINVAL/*SIGBUS*/ :
133 0/*retry access in userland*/;
134 goto out_unlock;
135 }
136
137 out_clear:
138 dma_fence_put(bo->moving);
139 bo->moving = NULL;
140
141 out_unlock:
142 return ret;
143 }
144
145 static int
146 ttm_bo_vm_reserve(struct ttm_buffer_object *bo, struct uvm_faultinfo *vmf)
147 {
148
149 /*
150 * Work around locking order reversal in fault / nopfn
151 * between mmap_sem and bo_reserve: Perform a trylock operation
152 * for reserve, and if it fails, retry the fault after waiting
153 * for the buffer to become unreserved.
154 */
155 if (__predict_false(!dma_resv_trylock(bo->base.resv))) {
156 ttm_bo_get(bo);
157 uvmfault_unlockall(vmf, vmf->entry->aref.ar_amap, NULL);
158 if (!dma_resv_lock_interruptible(bo->base.resv, NULL))
159 dma_resv_unlock(bo->base.resv);
160 ttm_bo_put(bo);
161 return ERESTART;
162 }
163
164 return 0;
165 }
166
167 static int
168 ttm_bo_uvm_fault_reserved(struct uvm_faultinfo *vmf, vaddr_t vaddr,
169 struct vm_page **pps, int npages, int centeridx, vm_prot_t access_type,
170 int flags)
171 {
172 struct uvm_object *const uobj = vmf->entry->object.uvm_obj;
173 struct ttm_buffer_object *const bo = container_of(uobj,
174 struct ttm_buffer_object, uvmobj);
175 struct ttm_bo_device *const bdev = bo->bdev;
176 struct ttm_mem_type_manager *man =
177 &bdev->man[bo->mem.mem_type];
178 union {
179 bus_addr_t base;
180 struct ttm_tt *ttm;
181 } u;
182 size_t size __diagused;
183 voff_t uoffset; /* offset in bytes into bo */
184 unsigned startpage; /* offset in pages into bo */
185 unsigned i;
186 vm_prot_t vm_prot = vmf->entry->protection; /* VM_PROT_* */
187 pgprot_t prot = vm_prot; /* VM_PROT_* | PMAP_* cacheability flags */
188 int err, ret;
189
190 /*
191 * Refuse to fault imported pages. This should be handled
192 * (if at all) by redirecting mmap to the exporter.
193 */
194 if (bo->ttm && (bo->ttm->page_flags & TTM_PAGE_FLAG_SG))
195 return EINVAL; /* SIGBUS */
196
197 if (bdev->driver->fault_reserve_notify) {
198 struct dma_fence *moving = dma_fence_get(bo->moving);
199
200 err = bdev->driver->fault_reserve_notify(bo);
201 switch (err) {
202 case 0:
203 break;
204 case -EBUSY:
205 case -ERESTARTSYS:
206 return 0; /* retry access in userland */
207 default:
208 return EINVAL; /* SIGBUS */
209 }
210
211 if (bo->moving != moving) {
212 spin_lock(&ttm_bo_glob.lru_lock);
213 ttm_bo_move_to_lru_tail(bo, NULL);
214 spin_unlock(&ttm_bo_glob.lru_lock);
215 }
216 dma_fence_put(moving);
217 }
218
219 /*
220 * Wait for buffer data in transit, due to a pipelined
221 * move.
222 */
223 ret = ttm_bo_vm_fault_idle(bo, vmf);
224 if (__predict_false(ret != 0))
225 return ret;
226
227 err = ttm_mem_io_lock(man, true);
228 if (__predict_false(err != 0))
229 return 0; /* retry access in userland */
230 err = ttm_mem_io_reserve_vm(bo);
231 if (__predict_false(err != 0)) {
232 ret = EINVAL; /* SIGBUS */
233 goto out_io_unlock;
234 }
235
236 prot = ttm_io_prot(bo->mem.placement, prot);
237 if (!bo->mem.bus.is_iomem) {
238 struct ttm_operation_ctx ctx = {
239 .interruptible = false,
240 .no_wait_gpu = false,
241 .flags = TTM_OPT_FLAG_FORCE_ALLOC
242
243 };
244
245 u.ttm = bo->ttm;
246 size = (size_t)bo->ttm->num_pages << PAGE_SHIFT;
247 if (ttm_tt_populate(bo->ttm, &ctx)) {
248 ret = ENOMEM;
249 goto out_io_unlock;
250 }
251 } else {
252 u.base = (bo->mem.bus.base + bo->mem.bus.offset);
253 size = bo->mem.bus.size;
254 }
255
256 KASSERT(vmf->entry->start <= vaddr);
257 KASSERT((vmf->entry->offset & (PAGE_SIZE - 1)) == 0);
258 KASSERT(vmf->entry->offset <= size);
259 KASSERT((vaddr - vmf->entry->start) <= (size - vmf->entry->offset));
260 KASSERTMSG(((size_t)npages << PAGE_SHIFT <=
261 ((size - vmf->entry->offset) - (vaddr - vmf->entry->start))),
262 "vaddr=%jx npages=%d bo=%p is_iomem=%d size=%zu"
263 " start=%jx offset=%jx",
264 (uintmax_t)vaddr, npages, bo, (int)bo->mem.bus.is_iomem, size,
265 (uintmax_t)vmf->entry->start, (uintmax_t)vmf->entry->offset);
266 uoffset = (vmf->entry->offset + (vaddr - vmf->entry->start));
267 startpage = (uoffset >> PAGE_SHIFT);
268 for (i = 0; i < npages; i++) {
269 paddr_t paddr;
270
271 if ((flags & PGO_ALLPAGES) == 0 && i != centeridx)
272 continue;
273 if (pps[i] == PGO_DONTCARE)
274 continue;
275 if (!bo->mem.bus.is_iomem) {
276 paddr = page_to_phys(u.ttm->pages[startpage + i]);
277 } else if (bdev->driver->io_mem_pfn) {
278 paddr = (paddr_t)(*bdev->driver->io_mem_pfn)(bo,
279 startpage + i) << PAGE_SHIFT;
280 } else {
281 const paddr_t cookie = bus_space_mmap(bdev->memt,
282 u.base, (off_t)(startpage + i) << PAGE_SHIFT,
283 vm_prot, 0);
284
285 paddr = pmap_phys_address(cookie);
286 #if 0 /* XXX Why no PMAP_* flags added here? */
287 mmapflags = pmap_mmap_flags(cookie);
288 #endif
289 }
290 ret = pmap_enter(vmf->orig_map->pmap, vaddr + i*PAGE_SIZE,
291 paddr, vm_prot, PMAP_CANFAIL | prot);
292 if (ret) {
293 KASSERT(ret != ERESTART);
294 break;
295 }
296 }
297 pmap_update(vmf->orig_map->pmap);
298 ret = 0; /* retry access in userland */
299 out_io_unlock:
300 ttm_mem_io_unlock(man);
301 KASSERT(ret != ERESTART);
302 return ret;
303 }
304
305 int
306 ttm_bo_uvm_fault(struct uvm_faultinfo *vmf, vaddr_t vaddr,
307 struct vm_page **pps, int npages, int centeridx, vm_prot_t access_type,
308 int flags)
309 {
310 struct uvm_object *const uobj = vmf->entry->object.uvm_obj;
311 struct ttm_buffer_object *const bo = container_of(uobj,
312 struct ttm_buffer_object, uvmobj);
313 int ret;
314
315 /* Thanks, uvm, but we don't need this lock. */
316 rw_exit(uobj->vmobjlock);
317
318 /* Copy-on-write mappings make no sense for the graphics aperture. */
319 if (UVM_ET_ISCOPYONWRITE(vmf->entry)) {
320 ret = EINVAL; /* SIGBUS */
321 goto out;
322 }
323
324 ret = ttm_bo_vm_reserve(bo, vmf);
325 if (ret) {
326 /* ttm_bo_vm_reserve already unlocked on ERESTART */
327 KASSERTMSG(ret == ERESTART, "ret=%d", ret);
328 return ret;
329 }
330
331 ret = ttm_bo_uvm_fault_reserved(vmf, vaddr, pps, npages, centeridx,
332 access_type, flags);
333 if (ret == ERESTART) /* already unlocked on ERESTART */
334 return ret;
335
336 dma_resv_unlock(bo->base.resv);
337
338 out: uvmfault_unlockall(vmf, vmf->entry->aref.ar_amap, NULL);
339 return ret;
340 }
341
342 int
343 ttm_bo_mmap_object(struct ttm_bo_device *bdev, off_t offset, size_t size,
344 vm_prot_t prot, struct uvm_object **uobjp, voff_t *uoffsetp,
345 struct file *file)
346 {
347 const unsigned long startpage = (offset >> PAGE_SHIFT);
348 const unsigned long npages = (size >> PAGE_SHIFT);
349 struct ttm_buffer_object *bo;
350 int ret;
351
352 KASSERT(0 == (offset & (PAGE_SIZE - 1)));
353 KASSERT(0 == (size & (PAGE_SIZE - 1)));
354
355 ret = ttm_bo_uvm_lookup(bdev, startpage, npages, &bo);
356 if (ret)
357 goto fail0;
358 KASSERTMSG((drm_vma_node_start(&bo->base.vma_node) <= startpage),
359 "mapping npages=0x%jx @ pfn=0x%jx"
360 " from vma npages=0x%jx @ pfn=0x%jx",
361 (uintmax_t)npages,
362 (uintmax_t)startpage,
363 (uintmax_t)drm_vma_node_size(&bo->base.vma_node),
364 (uintmax_t)drm_vma_node_start(&bo->base.vma_node));
365 KASSERTMSG((npages <= drm_vma_node_size(&bo->base.vma_node)),
366 "mapping npages=0x%jx @ pfn=0x%jx"
367 " from vma npages=0x%jx @ pfn=0x%jx",
368 (uintmax_t)npages,
369 (uintmax_t)startpage,
370 (uintmax_t)drm_vma_node_size(&bo->base.vma_node),
371 (uintmax_t)drm_vma_node_start(&bo->base.vma_node));
372 KASSERTMSG(((startpage - drm_vma_node_start(&bo->base.vma_node))
373 <= (drm_vma_node_size(&bo->base.vma_node) - npages)),
374 "mapping npages=0x%jx @ pfn=0x%jx"
375 " from vma npages=0x%jx @ pfn=0x%jx",
376 (uintmax_t)npages,
377 (uintmax_t)startpage,
378 (uintmax_t)drm_vma_node_size(&bo->base.vma_node),
379 (uintmax_t)drm_vma_node_start(&bo->base.vma_node));
380
381 /* XXX Just assert this? */
382 if (__predict_false(bdev->driver->verify_access == NULL)) {
383 ret = -EPERM;
384 goto fail1;
385 }
386 ret = (*bdev->driver->verify_access)(bo, file);
387 if (ret)
388 goto fail1;
389
390 /* Success! */
391 *uobjp = &bo->uvmobj;
392 *uoffsetp = (offset -
393 ((off_t)drm_vma_node_start(&bo->base.vma_node) << PAGE_SHIFT));
394 return 0;
395
396 fail1: ttm_bo_put(bo);
397 fail0: KASSERT(ret);
398 return ret;
399 }
400
401 static int
402 ttm_bo_uvm_lookup(struct ttm_bo_device *bdev, unsigned long startpage,
403 unsigned long npages, struct ttm_buffer_object **bop)
404 {
405 struct ttm_buffer_object *bo = NULL;
406 struct drm_vma_offset_node *node;
407
408 drm_vma_offset_lock_lookup(bdev->vma_manager);
409 node = drm_vma_offset_lookup_locked(bdev->vma_manager, startpage,
410 npages);
411 if (node != NULL) {
412 bo = container_of(node, struct ttm_buffer_object,
413 base.vma_node);
414 if (!kref_get_unless_zero(&bo->kref))
415 bo = NULL;
416 }
417 drm_vma_offset_unlock_lookup(bdev->vma_manager);
418
419 if (bo == NULL)
420 return -ENOENT;
421
422 *bop = bo;
423 return 0;
424 }
425