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