amdgpu_object.c revision 1.1.1.2 1 /* $NetBSD: amdgpu_object.c,v 1.1.1.2 2021/12/18 20:11:10 riastradh Exp $ */
2
3 /*
4 * Copyright 2009 Jerome Glisse.
5 * All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
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 NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * The above copyright notice and this permission notice (including the
24 * next paragraph) shall be included in all copies or substantial portions
25 * of the Software.
26 *
27 */
28 /*
29 * Authors:
30 * Jerome Glisse <glisse (at) freedesktop.org>
31 * Thomas Hellstrom <thomas-at-tungstengraphics-dot-com>
32 * Dave Airlie
33 */
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: amdgpu_object.c,v 1.1.1.2 2021/12/18 20:11:10 riastradh Exp $");
36
37 #include <linux/list.h>
38 #include <linux/slab.h>
39
40 #include <drm/amdgpu_drm.h>
41 #include <drm/drm_cache.h>
42 #include "amdgpu.h"
43 #include "amdgpu_trace.h"
44 #include "amdgpu_amdkfd.h"
45
46 /**
47 * DOC: amdgpu_object
48 *
49 * This defines the interfaces to operate on an &amdgpu_bo buffer object which
50 * represents memory used by driver (VRAM, system memory, etc.). The driver
51 * provides DRM/GEM APIs to userspace. DRM/GEM APIs then use these interfaces
52 * to create/destroy/set buffer object which are then managed by the kernel TTM
53 * memory manager.
54 * The interfaces are also used internally by kernel clients, including gfx,
55 * uvd, etc. for kernel managed allocations used by the GPU.
56 *
57 */
58
59 /**
60 * amdgpu_bo_subtract_pin_size - Remove BO from pin_size accounting
61 *
62 * @bo: &amdgpu_bo buffer object
63 *
64 * This function is called when a BO stops being pinned, and updates the
65 * &amdgpu_device pin_size values accordingly.
66 */
67 static void amdgpu_bo_subtract_pin_size(struct amdgpu_bo *bo)
68 {
69 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
70
71 if (bo->tbo.mem.mem_type == TTM_PL_VRAM) {
72 atomic64_sub(amdgpu_bo_size(bo), &adev->vram_pin_size);
73 atomic64_sub(amdgpu_vram_mgr_bo_visible_size(bo),
74 &adev->visible_pin_size);
75 } else if (bo->tbo.mem.mem_type == TTM_PL_TT) {
76 atomic64_sub(amdgpu_bo_size(bo), &adev->gart_pin_size);
77 }
78 }
79
80 static void amdgpu_bo_destroy(struct ttm_buffer_object *tbo)
81 {
82 struct amdgpu_device *adev = amdgpu_ttm_adev(tbo->bdev);
83 struct amdgpu_bo *bo = ttm_to_amdgpu_bo(tbo);
84
85 if (bo->pin_count > 0)
86 amdgpu_bo_subtract_pin_size(bo);
87
88 amdgpu_bo_kunmap(bo);
89
90 if (bo->tbo.base.import_attach)
91 drm_prime_gem_destroy(&bo->tbo.base, bo->tbo.sg);
92 drm_gem_object_release(&bo->tbo.base);
93 /* in case amdgpu_device_recover_vram got NULL of bo->parent */
94 if (!list_empty(&bo->shadow_list)) {
95 mutex_lock(&adev->shadow_list_lock);
96 list_del_init(&bo->shadow_list);
97 mutex_unlock(&adev->shadow_list_lock);
98 }
99 amdgpu_bo_unref(&bo->parent);
100
101 kfree(bo->metadata);
102 kfree(bo);
103 }
104
105 /**
106 * amdgpu_bo_is_amdgpu_bo - check if the buffer object is an &amdgpu_bo
107 * @bo: buffer object to be checked
108 *
109 * Uses destroy function associated with the object to determine if this is
110 * an &amdgpu_bo.
111 *
112 * Returns:
113 * true if the object belongs to &amdgpu_bo, false if not.
114 */
115 bool amdgpu_bo_is_amdgpu_bo(struct ttm_buffer_object *bo)
116 {
117 if (bo->destroy == &amdgpu_bo_destroy)
118 return true;
119 return false;
120 }
121
122 /**
123 * amdgpu_bo_placement_from_domain - set buffer's placement
124 * @abo: &amdgpu_bo buffer object whose placement is to be set
125 * @domain: requested domain
126 *
127 * Sets buffer's placement according to requested domain and the buffer's
128 * flags.
129 */
130 void amdgpu_bo_placement_from_domain(struct amdgpu_bo *abo, u32 domain)
131 {
132 struct amdgpu_device *adev = amdgpu_ttm_adev(abo->tbo.bdev);
133 struct ttm_placement *placement = &abo->placement;
134 struct ttm_place *places = abo->placements;
135 u64 flags = abo->flags;
136 u32 c = 0;
137
138 if (domain & AMDGPU_GEM_DOMAIN_VRAM) {
139 unsigned visible_pfn = adev->gmc.visible_vram_size >> PAGE_SHIFT;
140
141 places[c].fpfn = 0;
142 places[c].lpfn = 0;
143 places[c].flags = TTM_PL_FLAG_WC | TTM_PL_FLAG_UNCACHED |
144 TTM_PL_FLAG_VRAM;
145
146 if (flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED)
147 places[c].lpfn = visible_pfn;
148 else
149 places[c].flags |= TTM_PL_FLAG_TOPDOWN;
150
151 if (flags & AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS)
152 places[c].flags |= TTM_PL_FLAG_CONTIGUOUS;
153 c++;
154 }
155
156 if (domain & AMDGPU_GEM_DOMAIN_GTT) {
157 places[c].fpfn = 0;
158 places[c].lpfn = 0;
159 places[c].flags = TTM_PL_FLAG_TT;
160 if (flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC)
161 places[c].flags |= TTM_PL_FLAG_WC |
162 TTM_PL_FLAG_UNCACHED;
163 else
164 places[c].flags |= TTM_PL_FLAG_CACHED;
165 c++;
166 }
167
168 if (domain & AMDGPU_GEM_DOMAIN_CPU) {
169 places[c].fpfn = 0;
170 places[c].lpfn = 0;
171 places[c].flags = TTM_PL_FLAG_SYSTEM;
172 if (flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC)
173 places[c].flags |= TTM_PL_FLAG_WC |
174 TTM_PL_FLAG_UNCACHED;
175 else
176 places[c].flags |= TTM_PL_FLAG_CACHED;
177 c++;
178 }
179
180 if (domain & AMDGPU_GEM_DOMAIN_GDS) {
181 places[c].fpfn = 0;
182 places[c].lpfn = 0;
183 places[c].flags = TTM_PL_FLAG_UNCACHED | AMDGPU_PL_FLAG_GDS;
184 c++;
185 }
186
187 if (domain & AMDGPU_GEM_DOMAIN_GWS) {
188 places[c].fpfn = 0;
189 places[c].lpfn = 0;
190 places[c].flags = TTM_PL_FLAG_UNCACHED | AMDGPU_PL_FLAG_GWS;
191 c++;
192 }
193
194 if (domain & AMDGPU_GEM_DOMAIN_OA) {
195 places[c].fpfn = 0;
196 places[c].lpfn = 0;
197 places[c].flags = TTM_PL_FLAG_UNCACHED | AMDGPU_PL_FLAG_OA;
198 c++;
199 }
200
201 if (!c) {
202 places[c].fpfn = 0;
203 places[c].lpfn = 0;
204 places[c].flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM;
205 c++;
206 }
207
208 BUG_ON(c >= AMDGPU_BO_MAX_PLACEMENTS);
209
210 placement->num_placement = c;
211 placement->placement = places;
212
213 placement->num_busy_placement = c;
214 placement->busy_placement = places;
215 }
216
217 /**
218 * amdgpu_bo_create_reserved - create reserved BO for kernel use
219 *
220 * @adev: amdgpu device object
221 * @size: size for the new BO
222 * @align: alignment for the new BO
223 * @domain: where to place it
224 * @bo_ptr: used to initialize BOs in structures
225 * @gpu_addr: GPU addr of the pinned BO
226 * @cpu_addr: optional CPU address mapping
227 *
228 * Allocates and pins a BO for kernel internal use, and returns it still
229 * reserved.
230 *
231 * Note: For bo_ptr new BO is only created if bo_ptr points to NULL.
232 *
233 * Returns:
234 * 0 on success, negative error code otherwise.
235 */
236 int amdgpu_bo_create_reserved(struct amdgpu_device *adev,
237 unsigned long size, int align,
238 u32 domain, struct amdgpu_bo **bo_ptr,
239 u64 *gpu_addr, void **cpu_addr)
240 {
241 struct amdgpu_bo_param bp;
242 bool free = false;
243 int r;
244
245 if (!size) {
246 amdgpu_bo_unref(bo_ptr);
247 return 0;
248 }
249
250 memset(&bp, 0, sizeof(bp));
251 bp.size = size;
252 bp.byte_align = align;
253 bp.domain = domain;
254 bp.flags = cpu_addr ? AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED
255 : AMDGPU_GEM_CREATE_NO_CPU_ACCESS;
256 bp.flags |= AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
257 bp.type = ttm_bo_type_kernel;
258 bp.resv = NULL;
259
260 if (!*bo_ptr) {
261 r = amdgpu_bo_create(adev, &bp, bo_ptr);
262 if (r) {
263 dev_err(adev->dev, "(%d) failed to allocate kernel bo\n",
264 r);
265 return r;
266 }
267 free = true;
268 }
269
270 r = amdgpu_bo_reserve(*bo_ptr, false);
271 if (r) {
272 dev_err(adev->dev, "(%d) failed to reserve kernel bo\n", r);
273 goto error_free;
274 }
275
276 r = amdgpu_bo_pin(*bo_ptr, domain);
277 if (r) {
278 dev_err(adev->dev, "(%d) kernel bo pin failed\n", r);
279 goto error_unreserve;
280 }
281
282 r = amdgpu_ttm_alloc_gart(&(*bo_ptr)->tbo);
283 if (r) {
284 dev_err(adev->dev, "%p bind failed\n", *bo_ptr);
285 goto error_unpin;
286 }
287
288 if (gpu_addr)
289 *gpu_addr = amdgpu_bo_gpu_offset(*bo_ptr);
290
291 if (cpu_addr) {
292 r = amdgpu_bo_kmap(*bo_ptr, cpu_addr);
293 if (r) {
294 dev_err(adev->dev, "(%d) kernel bo map failed\n", r);
295 goto error_unpin;
296 }
297 }
298
299 return 0;
300
301 error_unpin:
302 amdgpu_bo_unpin(*bo_ptr);
303 error_unreserve:
304 amdgpu_bo_unreserve(*bo_ptr);
305
306 error_free:
307 if (free)
308 amdgpu_bo_unref(bo_ptr);
309
310 return r;
311 }
312
313 /**
314 * amdgpu_bo_create_kernel - create BO for kernel use
315 *
316 * @adev: amdgpu device object
317 * @size: size for the new BO
318 * @align: alignment for the new BO
319 * @domain: where to place it
320 * @bo_ptr: used to initialize BOs in structures
321 * @gpu_addr: GPU addr of the pinned BO
322 * @cpu_addr: optional CPU address mapping
323 *
324 * Allocates and pins a BO for kernel internal use.
325 *
326 * Note: For bo_ptr new BO is only created if bo_ptr points to NULL.
327 *
328 * Returns:
329 * 0 on success, negative error code otherwise.
330 */
331 int amdgpu_bo_create_kernel(struct amdgpu_device *adev,
332 unsigned long size, int align,
333 u32 domain, struct amdgpu_bo **bo_ptr,
334 u64 *gpu_addr, void **cpu_addr)
335 {
336 int r;
337
338 r = amdgpu_bo_create_reserved(adev, size, align, domain, bo_ptr,
339 gpu_addr, cpu_addr);
340
341 if (r)
342 return r;
343
344 if (*bo_ptr)
345 amdgpu_bo_unreserve(*bo_ptr);
346
347 return 0;
348 }
349
350 /**
351 * amdgpu_bo_create_kernel_at - create BO for kernel use at specific location
352 *
353 * @adev: amdgpu device object
354 * @offset: offset of the BO
355 * @size: size of the BO
356 * @domain: where to place it
357 * @bo_ptr: used to initialize BOs in structures
358 * @cpu_addr: optional CPU address mapping
359 *
360 * Creates a kernel BO at a specific offset in the address space of the domain.
361 *
362 * Returns:
363 * 0 on success, negative error code otherwise.
364 */
365 int amdgpu_bo_create_kernel_at(struct amdgpu_device *adev,
366 uint64_t offset, uint64_t size, uint32_t domain,
367 struct amdgpu_bo **bo_ptr, void **cpu_addr)
368 {
369 struct ttm_operation_ctx ctx = { false, false };
370 unsigned int i;
371 int r;
372
373 offset &= PAGE_MASK;
374 size = ALIGN(size, PAGE_SIZE);
375
376 r = amdgpu_bo_create_reserved(adev, size, PAGE_SIZE, domain, bo_ptr,
377 NULL, cpu_addr);
378 if (r)
379 return r;
380
381 /*
382 * Remove the original mem node and create a new one at the request
383 * position.
384 */
385 if (cpu_addr)
386 amdgpu_bo_kunmap(*bo_ptr);
387
388 ttm_bo_mem_put(&(*bo_ptr)->tbo, &(*bo_ptr)->tbo.mem);
389
390 for (i = 0; i < (*bo_ptr)->placement.num_placement; ++i) {
391 (*bo_ptr)->placements[i].fpfn = offset >> PAGE_SHIFT;
392 (*bo_ptr)->placements[i].lpfn = (offset + size) >> PAGE_SHIFT;
393 }
394 r = ttm_bo_mem_space(&(*bo_ptr)->tbo, &(*bo_ptr)->placement,
395 &(*bo_ptr)->tbo.mem, &ctx);
396 if (r)
397 goto error;
398
399 if (cpu_addr) {
400 r = amdgpu_bo_kmap(*bo_ptr, cpu_addr);
401 if (r)
402 goto error;
403 }
404
405 amdgpu_bo_unreserve(*bo_ptr);
406 return 0;
407
408 error:
409 amdgpu_bo_unreserve(*bo_ptr);
410 amdgpu_bo_unref(bo_ptr);
411 return r;
412 }
413
414 /**
415 * amdgpu_bo_free_kernel - free BO for kernel use
416 *
417 * @bo: amdgpu BO to free
418 * @gpu_addr: pointer to where the BO's GPU memory space address was stored
419 * @cpu_addr: pointer to where the BO's CPU memory space address was stored
420 *
421 * unmaps and unpin a BO for kernel internal use.
422 */
423 void amdgpu_bo_free_kernel(struct amdgpu_bo **bo, u64 *gpu_addr,
424 void **cpu_addr)
425 {
426 if (*bo == NULL)
427 return;
428
429 if (likely(amdgpu_bo_reserve(*bo, true) == 0)) {
430 if (cpu_addr)
431 amdgpu_bo_kunmap(*bo);
432
433 amdgpu_bo_unpin(*bo);
434 amdgpu_bo_unreserve(*bo);
435 }
436 amdgpu_bo_unref(bo);
437
438 if (gpu_addr)
439 *gpu_addr = 0;
440
441 if (cpu_addr)
442 *cpu_addr = NULL;
443 }
444
445 /* Validate bo size is bit bigger then the request domain */
446 static bool amdgpu_bo_validate_size(struct amdgpu_device *adev,
447 unsigned long size, u32 domain)
448 {
449 struct ttm_mem_type_manager *man = NULL;
450
451 /*
452 * If GTT is part of requested domains the check must succeed to
453 * allow fall back to GTT
454 */
455 if (domain & AMDGPU_GEM_DOMAIN_GTT) {
456 man = &adev->mman.bdev.man[TTM_PL_TT];
457
458 if (size < (man->size << PAGE_SHIFT))
459 return true;
460 else
461 goto fail;
462 }
463
464 if (domain & AMDGPU_GEM_DOMAIN_VRAM) {
465 man = &adev->mman.bdev.man[TTM_PL_VRAM];
466
467 if (size < (man->size << PAGE_SHIFT))
468 return true;
469 else
470 goto fail;
471 }
472
473
474 /* TODO add more domains checks, such as AMDGPU_GEM_DOMAIN_CPU */
475 return true;
476
477 fail:
478 DRM_DEBUG("BO size %lu > total memory in domain: %llu\n", size,
479 man->size << PAGE_SHIFT);
480 return false;
481 }
482
483 bool amdgpu_bo_support_uswc(u64 bo_flags)
484 {
485
486 #ifdef CONFIG_X86_32
487 /* XXX: Write-combined CPU mappings of GTT seem broken on 32-bit
488 * See https://bugs.freedesktop.org/show_bug.cgi?id=84627
489 */
490 return false;
491 #elif defined(CONFIG_X86) && !defined(CONFIG_X86_PAT)
492 /* Don't try to enable write-combining when it can't work, or things
493 * may be slow
494 * See https://bugs.freedesktop.org/show_bug.cgi?id=88758
495 */
496
497 #ifndef CONFIG_COMPILE_TEST
498 #warning Please enable CONFIG_MTRR and CONFIG_X86_PAT for better performance \
499 thanks to write-combining
500 #endif
501
502 if (bo_flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC)
503 DRM_INFO_ONCE("Please enable CONFIG_MTRR and CONFIG_X86_PAT for "
504 "better performance thanks to write-combining\n");
505 return false;
506 #else
507 /* For architectures that don't support WC memory,
508 * mask out the WC flag from the BO
509 */
510 if (!drm_arch_can_wc_memory())
511 return false;
512
513 return true;
514 #endif
515 }
516
517 static int amdgpu_bo_do_create(struct amdgpu_device *adev,
518 struct amdgpu_bo_param *bp,
519 struct amdgpu_bo **bo_ptr)
520 {
521 struct ttm_operation_ctx ctx = {
522 .interruptible = (bp->type != ttm_bo_type_kernel),
523 .no_wait_gpu = bp->no_wait_gpu,
524 .resv = bp->resv,
525 .flags = bp->type != ttm_bo_type_kernel ?
526 TTM_OPT_FLAG_ALLOW_RES_EVICT : 0
527 };
528 struct amdgpu_bo *bo;
529 unsigned long page_align, size = bp->size;
530 size_t acc_size;
531 int r;
532
533 /* Note that GDS/GWS/OA allocates 1 page per byte/resource. */
534 if (bp->domain & (AMDGPU_GEM_DOMAIN_GWS | AMDGPU_GEM_DOMAIN_OA)) {
535 /* GWS and OA don't need any alignment. */
536 page_align = bp->byte_align;
537 size <<= PAGE_SHIFT;
538 } else if (bp->domain & AMDGPU_GEM_DOMAIN_GDS) {
539 /* Both size and alignment must be a multiple of 4. */
540 page_align = ALIGN(bp->byte_align, 4);
541 size = ALIGN(size, 4) << PAGE_SHIFT;
542 } else {
543 /* Memory should be aligned at least to a page size. */
544 page_align = ALIGN(bp->byte_align, PAGE_SIZE) >> PAGE_SHIFT;
545 size = ALIGN(size, PAGE_SIZE);
546 }
547
548 if (!amdgpu_bo_validate_size(adev, size, bp->domain))
549 return -ENOMEM;
550
551 *bo_ptr = NULL;
552
553 acc_size = ttm_bo_dma_acc_size(&adev->mman.bdev, size,
554 sizeof(struct amdgpu_bo));
555
556 bo = kzalloc(sizeof(struct amdgpu_bo), GFP_KERNEL);
557 if (bo == NULL)
558 return -ENOMEM;
559 drm_gem_private_object_init(adev->ddev, &bo->tbo.base, size);
560 INIT_LIST_HEAD(&bo->shadow_list);
561 bo->vm_bo = NULL;
562 bo->preferred_domains = bp->preferred_domain ? bp->preferred_domain :
563 bp->domain;
564 bo->allowed_domains = bo->preferred_domains;
565 if (bp->type != ttm_bo_type_kernel &&
566 bo->allowed_domains == AMDGPU_GEM_DOMAIN_VRAM)
567 bo->allowed_domains |= AMDGPU_GEM_DOMAIN_GTT;
568
569 bo->flags = bp->flags;
570
571 if (!amdgpu_bo_support_uswc(bo->flags))
572 bo->flags &= ~AMDGPU_GEM_CREATE_CPU_GTT_USWC;
573
574 bo->tbo.bdev = &adev->mman.bdev;
575 if (bp->domain & (AMDGPU_GEM_DOMAIN_GWS | AMDGPU_GEM_DOMAIN_OA |
576 AMDGPU_GEM_DOMAIN_GDS))
577 amdgpu_bo_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_CPU);
578 else
579 amdgpu_bo_placement_from_domain(bo, bp->domain);
580 if (bp->type == ttm_bo_type_kernel)
581 bo->tbo.priority = 1;
582
583 r = ttm_bo_init_reserved(&adev->mman.bdev, &bo->tbo, size, bp->type,
584 &bo->placement, page_align, &ctx, acc_size,
585 NULL, bp->resv, &amdgpu_bo_destroy);
586 if (unlikely(r != 0))
587 return r;
588
589 if (!amdgpu_gmc_vram_full_visible(&adev->gmc) &&
590 bo->tbo.mem.mem_type == TTM_PL_VRAM &&
591 bo->tbo.mem.start < adev->gmc.visible_vram_size >> PAGE_SHIFT)
592 amdgpu_cs_report_moved_bytes(adev, ctx.bytes_moved,
593 ctx.bytes_moved);
594 else
595 amdgpu_cs_report_moved_bytes(adev, ctx.bytes_moved, 0);
596
597 if (bp->flags & AMDGPU_GEM_CREATE_VRAM_CLEARED &&
598 bo->tbo.mem.placement & TTM_PL_FLAG_VRAM) {
599 struct dma_fence *fence;
600
601 r = amdgpu_fill_buffer(bo, 0, bo->tbo.base.resv, &fence);
602 if (unlikely(r))
603 goto fail_unreserve;
604
605 amdgpu_bo_fence(bo, fence, false);
606 dma_fence_put(bo->tbo.moving);
607 bo->tbo.moving = dma_fence_get(fence);
608 dma_fence_put(fence);
609 }
610 if (!bp->resv)
611 amdgpu_bo_unreserve(bo);
612 *bo_ptr = bo;
613
614 trace_amdgpu_bo_create(bo);
615
616 /* Treat CPU_ACCESS_REQUIRED only as a hint if given by UMD */
617 if (bp->type == ttm_bo_type_device)
618 bo->flags &= ~AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
619
620 return 0;
621
622 fail_unreserve:
623 if (!bp->resv)
624 dma_resv_unlock(bo->tbo.base.resv);
625 amdgpu_bo_unref(&bo);
626 return r;
627 }
628
629 static int amdgpu_bo_create_shadow(struct amdgpu_device *adev,
630 unsigned long size,
631 struct amdgpu_bo *bo)
632 {
633 struct amdgpu_bo_param bp;
634 int r;
635
636 if (bo->shadow)
637 return 0;
638
639 memset(&bp, 0, sizeof(bp));
640 bp.size = size;
641 bp.domain = AMDGPU_GEM_DOMAIN_GTT;
642 bp.flags = AMDGPU_GEM_CREATE_CPU_GTT_USWC |
643 AMDGPU_GEM_CREATE_SHADOW;
644 bp.type = ttm_bo_type_kernel;
645 bp.resv = bo->tbo.base.resv;
646
647 r = amdgpu_bo_do_create(adev, &bp, &bo->shadow);
648 if (!r) {
649 bo->shadow->parent = amdgpu_bo_ref(bo);
650 mutex_lock(&adev->shadow_list_lock);
651 list_add_tail(&bo->shadow->shadow_list, &adev->shadow_list);
652 mutex_unlock(&adev->shadow_list_lock);
653 }
654
655 return r;
656 }
657
658 /**
659 * amdgpu_bo_create - create an &amdgpu_bo buffer object
660 * @adev: amdgpu device object
661 * @bp: parameters to be used for the buffer object
662 * @bo_ptr: pointer to the buffer object pointer
663 *
664 * Creates an &amdgpu_bo buffer object; and if requested, also creates a
665 * shadow object.
666 * Shadow object is used to backup the original buffer object, and is always
667 * in GTT.
668 *
669 * Returns:
670 * 0 for success or a negative error code on failure.
671 */
672 int amdgpu_bo_create(struct amdgpu_device *adev,
673 struct amdgpu_bo_param *bp,
674 struct amdgpu_bo **bo_ptr)
675 {
676 u64 flags = bp->flags;
677 int r;
678
679 bp->flags = bp->flags & ~AMDGPU_GEM_CREATE_SHADOW;
680 r = amdgpu_bo_do_create(adev, bp, bo_ptr);
681 if (r)
682 return r;
683
684 if ((flags & AMDGPU_GEM_CREATE_SHADOW) && !(adev->flags & AMD_IS_APU)) {
685 if (!bp->resv)
686 WARN_ON(dma_resv_lock((*bo_ptr)->tbo.base.resv,
687 NULL));
688
689 r = amdgpu_bo_create_shadow(adev, bp->size, *bo_ptr);
690
691 if (!bp->resv)
692 dma_resv_unlock((*bo_ptr)->tbo.base.resv);
693
694 if (r)
695 amdgpu_bo_unref(bo_ptr);
696 }
697
698 return r;
699 }
700
701 /**
702 * amdgpu_bo_validate - validate an &amdgpu_bo buffer object
703 * @bo: pointer to the buffer object
704 *
705 * Sets placement according to domain; and changes placement and caching
706 * policy of the buffer object according to the placement.
707 * This is used for validating shadow bos. It calls ttm_bo_validate() to
708 * make sure the buffer is resident where it needs to be.
709 *
710 * Returns:
711 * 0 for success or a negative error code on failure.
712 */
713 int amdgpu_bo_validate(struct amdgpu_bo *bo)
714 {
715 struct ttm_operation_ctx ctx = { false, false };
716 uint32_t domain;
717 int r;
718
719 if (bo->pin_count)
720 return 0;
721
722 domain = bo->preferred_domains;
723
724 retry:
725 amdgpu_bo_placement_from_domain(bo, domain);
726 r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
727 if (unlikely(r == -ENOMEM) && domain != bo->allowed_domains) {
728 domain = bo->allowed_domains;
729 goto retry;
730 }
731
732 return r;
733 }
734
735 /**
736 * amdgpu_bo_restore_shadow - restore an &amdgpu_bo shadow
737 *
738 * @shadow: &amdgpu_bo shadow to be restored
739 * @fence: dma_fence associated with the operation
740 *
741 * Copies a buffer object's shadow content back to the object.
742 * This is used for recovering a buffer from its shadow in case of a gpu
743 * reset where vram context may be lost.
744 *
745 * Returns:
746 * 0 for success or a negative error code on failure.
747 */
748 int amdgpu_bo_restore_shadow(struct amdgpu_bo *shadow, struct dma_fence **fence)
749
750 {
751 struct amdgpu_device *adev = amdgpu_ttm_adev(shadow->tbo.bdev);
752 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
753 uint64_t shadow_addr, parent_addr;
754
755 shadow_addr = amdgpu_bo_gpu_offset(shadow);
756 parent_addr = amdgpu_bo_gpu_offset(shadow->parent);
757
758 return amdgpu_copy_buffer(ring, shadow_addr, parent_addr,
759 amdgpu_bo_size(shadow), NULL, fence,
760 true, false);
761 }
762
763 /**
764 * amdgpu_bo_kmap - map an &amdgpu_bo buffer object
765 * @bo: &amdgpu_bo buffer object to be mapped
766 * @ptr: kernel virtual address to be returned
767 *
768 * Calls ttm_bo_kmap() to set up the kernel virtual mapping; calls
769 * amdgpu_bo_kptr() to get the kernel virtual address.
770 *
771 * Returns:
772 * 0 for success or a negative error code on failure.
773 */
774 int amdgpu_bo_kmap(struct amdgpu_bo *bo, void **ptr)
775 {
776 void *kptr;
777 long r;
778
779 if (bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)
780 return -EPERM;
781
782 kptr = amdgpu_bo_kptr(bo);
783 if (kptr) {
784 if (ptr)
785 *ptr = kptr;
786 return 0;
787 }
788
789 r = dma_resv_wait_timeout_rcu(bo->tbo.base.resv, false, false,
790 MAX_SCHEDULE_TIMEOUT);
791 if (r < 0)
792 return r;
793
794 r = ttm_bo_kmap(&bo->tbo, 0, bo->tbo.num_pages, &bo->kmap);
795 if (r)
796 return r;
797
798 if (ptr)
799 *ptr = amdgpu_bo_kptr(bo);
800
801 return 0;
802 }
803
804 /**
805 * amdgpu_bo_kptr - returns a kernel virtual address of the buffer object
806 * @bo: &amdgpu_bo buffer object
807 *
808 * Calls ttm_kmap_obj_virtual() to get the kernel virtual address
809 *
810 * Returns:
811 * the virtual address of a buffer object area.
812 */
813 void *amdgpu_bo_kptr(struct amdgpu_bo *bo)
814 {
815 bool is_iomem;
816
817 return ttm_kmap_obj_virtual(&bo->kmap, &is_iomem);
818 }
819
820 /**
821 * amdgpu_bo_kunmap - unmap an &amdgpu_bo buffer object
822 * @bo: &amdgpu_bo buffer object to be unmapped
823 *
824 * Unmaps a kernel map set up by amdgpu_bo_kmap().
825 */
826 void amdgpu_bo_kunmap(struct amdgpu_bo *bo)
827 {
828 if (bo->kmap.bo)
829 ttm_bo_kunmap(&bo->kmap);
830 }
831
832 /**
833 * amdgpu_bo_ref - reference an &amdgpu_bo buffer object
834 * @bo: &amdgpu_bo buffer object
835 *
836 * References the contained &ttm_buffer_object.
837 *
838 * Returns:
839 * a refcounted pointer to the &amdgpu_bo buffer object.
840 */
841 struct amdgpu_bo *amdgpu_bo_ref(struct amdgpu_bo *bo)
842 {
843 if (bo == NULL)
844 return NULL;
845
846 ttm_bo_get(&bo->tbo);
847 return bo;
848 }
849
850 /**
851 * amdgpu_bo_unref - unreference an &amdgpu_bo buffer object
852 * @bo: &amdgpu_bo buffer object
853 *
854 * Unreferences the contained &ttm_buffer_object and clear the pointer
855 */
856 void amdgpu_bo_unref(struct amdgpu_bo **bo)
857 {
858 struct ttm_buffer_object *tbo;
859
860 if ((*bo) == NULL)
861 return;
862
863 tbo = &((*bo)->tbo);
864 ttm_bo_put(tbo);
865 *bo = NULL;
866 }
867
868 /**
869 * amdgpu_bo_pin_restricted - pin an &amdgpu_bo buffer object
870 * @bo: &amdgpu_bo buffer object to be pinned
871 * @domain: domain to be pinned to
872 * @min_offset: the start of requested address range
873 * @max_offset: the end of requested address range
874 *
875 * Pins the buffer object according to requested domain and address range. If
876 * the memory is unbound gart memory, binds the pages into gart table. Adjusts
877 * pin_count and pin_size accordingly.
878 *
879 * Pinning means to lock pages in memory along with keeping them at a fixed
880 * offset. It is required when a buffer can not be moved, for example, when
881 * a display buffer is being scanned out.
882 *
883 * Compared with amdgpu_bo_pin(), this function gives more flexibility on
884 * where to pin a buffer if there are specific restrictions on where a buffer
885 * must be located.
886 *
887 * Returns:
888 * 0 for success or a negative error code on failure.
889 */
890 int amdgpu_bo_pin_restricted(struct amdgpu_bo *bo, u32 domain,
891 u64 min_offset, u64 max_offset)
892 {
893 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
894 struct ttm_operation_ctx ctx = { false, false };
895 int r, i;
896
897 if (amdgpu_ttm_tt_get_usermm(bo->tbo.ttm))
898 return -EPERM;
899
900 if (WARN_ON_ONCE(min_offset > max_offset))
901 return -EINVAL;
902
903 /* A shared bo cannot be migrated to VRAM */
904 if (bo->prime_shared_count) {
905 if (domain & AMDGPU_GEM_DOMAIN_GTT)
906 domain = AMDGPU_GEM_DOMAIN_GTT;
907 else
908 return -EINVAL;
909 }
910
911 /* This assumes only APU display buffers are pinned with (VRAM|GTT).
912 * See function amdgpu_display_supported_domains()
913 */
914 domain = amdgpu_bo_get_preferred_pin_domain(adev, domain);
915
916 if (bo->pin_count) {
917 uint32_t mem_type = bo->tbo.mem.mem_type;
918
919 if (!(domain & amdgpu_mem_type_to_domain(mem_type)))
920 return -EINVAL;
921
922 bo->pin_count++;
923
924 if (max_offset != 0) {
925 u64 domain_start = bo->tbo.bdev->man[mem_type].gpu_offset;
926 WARN_ON_ONCE(max_offset <
927 (amdgpu_bo_gpu_offset(bo) - domain_start));
928 }
929
930 return 0;
931 }
932
933 bo->flags |= AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
934 /* force to pin into visible video ram */
935 if (!(bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS))
936 bo->flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
937 amdgpu_bo_placement_from_domain(bo, domain);
938 for (i = 0; i < bo->placement.num_placement; i++) {
939 unsigned fpfn, lpfn;
940
941 fpfn = min_offset >> PAGE_SHIFT;
942 lpfn = max_offset >> PAGE_SHIFT;
943
944 if (fpfn > bo->placements[i].fpfn)
945 bo->placements[i].fpfn = fpfn;
946 if (!bo->placements[i].lpfn ||
947 (lpfn && lpfn < bo->placements[i].lpfn))
948 bo->placements[i].lpfn = lpfn;
949 bo->placements[i].flags |= TTM_PL_FLAG_NO_EVICT;
950 }
951
952 r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
953 if (unlikely(r)) {
954 dev_err(adev->dev, "%p pin failed\n", bo);
955 goto error;
956 }
957
958 bo->pin_count = 1;
959
960 domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
961 if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
962 atomic64_add(amdgpu_bo_size(bo), &adev->vram_pin_size);
963 atomic64_add(amdgpu_vram_mgr_bo_visible_size(bo),
964 &adev->visible_pin_size);
965 } else if (domain == AMDGPU_GEM_DOMAIN_GTT) {
966 atomic64_add(amdgpu_bo_size(bo), &adev->gart_pin_size);
967 }
968
969 error:
970 return r;
971 }
972
973 /**
974 * amdgpu_bo_pin - pin an &amdgpu_bo buffer object
975 * @bo: &amdgpu_bo buffer object to be pinned
976 * @domain: domain to be pinned to
977 *
978 * A simple wrapper to amdgpu_bo_pin_restricted().
979 * Provides a simpler API for buffers that do not have any strict restrictions
980 * on where a buffer must be located.
981 *
982 * Returns:
983 * 0 for success or a negative error code on failure.
984 */
985 int amdgpu_bo_pin(struct amdgpu_bo *bo, u32 domain)
986 {
987 return amdgpu_bo_pin_restricted(bo, domain, 0, 0);
988 }
989
990 /**
991 * amdgpu_bo_unpin - unpin an &amdgpu_bo buffer object
992 * @bo: &amdgpu_bo buffer object to be unpinned
993 *
994 * Decreases the pin_count, and clears the flags if pin_count reaches 0.
995 * Changes placement and pin size accordingly.
996 *
997 * Returns:
998 * 0 for success or a negative error code on failure.
999 */
1000 int amdgpu_bo_unpin(struct amdgpu_bo *bo)
1001 {
1002 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
1003 struct ttm_operation_ctx ctx = { false, false };
1004 int r, i;
1005
1006 if (WARN_ON_ONCE(!bo->pin_count)) {
1007 dev_warn(adev->dev, "%p unpin not necessary\n", bo);
1008 return 0;
1009 }
1010 bo->pin_count--;
1011 if (bo->pin_count)
1012 return 0;
1013
1014 amdgpu_bo_subtract_pin_size(bo);
1015
1016 for (i = 0; i < bo->placement.num_placement; i++) {
1017 bo->placements[i].lpfn = 0;
1018 bo->placements[i].flags &= ~TTM_PL_FLAG_NO_EVICT;
1019 }
1020 r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
1021 if (unlikely(r))
1022 dev_err(adev->dev, "%p validate failed for unpin\n", bo);
1023
1024 return r;
1025 }
1026
1027 /**
1028 * amdgpu_bo_evict_vram - evict VRAM buffers
1029 * @adev: amdgpu device object
1030 *
1031 * Evicts all VRAM buffers on the lru list of the memory type.
1032 * Mainly used for evicting vram at suspend time.
1033 *
1034 * Returns:
1035 * 0 for success or a negative error code on failure.
1036 */
1037 int amdgpu_bo_evict_vram(struct amdgpu_device *adev)
1038 {
1039 /* late 2.6.33 fix IGP hibernate - we need pm ops to do this correct */
1040 #ifndef CONFIG_HIBERNATION
1041 if (adev->flags & AMD_IS_APU) {
1042 /* Useless to evict on IGP chips */
1043 return 0;
1044 }
1045 #endif
1046 return ttm_bo_evict_mm(&adev->mman.bdev, TTM_PL_VRAM);
1047 }
1048
1049 static const char *amdgpu_vram_names[] = {
1050 "UNKNOWN",
1051 "GDDR1",
1052 "DDR2",
1053 "GDDR3",
1054 "GDDR4",
1055 "GDDR5",
1056 "HBM",
1057 "DDR3",
1058 "DDR4",
1059 "GDDR6",
1060 };
1061
1062 /**
1063 * amdgpu_bo_init - initialize memory manager
1064 * @adev: amdgpu device object
1065 *
1066 * Calls amdgpu_ttm_init() to initialize amdgpu memory manager.
1067 *
1068 * Returns:
1069 * 0 for success or a negative error code on failure.
1070 */
1071 int amdgpu_bo_init(struct amdgpu_device *adev)
1072 {
1073 /* reserve PAT memory space to WC for VRAM */
1074 arch_io_reserve_memtype_wc(adev->gmc.aper_base,
1075 adev->gmc.aper_size);
1076
1077 /* Add an MTRR for the VRAM */
1078 adev->gmc.vram_mtrr = arch_phys_wc_add(adev->gmc.aper_base,
1079 adev->gmc.aper_size);
1080 DRM_INFO("Detected VRAM RAM=%lluM, BAR=%lluM\n",
1081 adev->gmc.mc_vram_size >> 20,
1082 (unsigned long long)adev->gmc.aper_size >> 20);
1083 DRM_INFO("RAM width %dbits %s\n",
1084 adev->gmc.vram_width, amdgpu_vram_names[adev->gmc.vram_type]);
1085 return amdgpu_ttm_init(adev);
1086 }
1087
1088 /**
1089 * amdgpu_bo_late_init - late init
1090 * @adev: amdgpu device object
1091 *
1092 * Calls amdgpu_ttm_late_init() to free resources used earlier during
1093 * initialization.
1094 *
1095 * Returns:
1096 * 0 for success or a negative error code on failure.
1097 */
1098 int amdgpu_bo_late_init(struct amdgpu_device *adev)
1099 {
1100 amdgpu_ttm_late_init(adev);
1101
1102 return 0;
1103 }
1104
1105 /**
1106 * amdgpu_bo_fini - tear down memory manager
1107 * @adev: amdgpu device object
1108 *
1109 * Reverses amdgpu_bo_init() to tear down memory manager.
1110 */
1111 void amdgpu_bo_fini(struct amdgpu_device *adev)
1112 {
1113 amdgpu_ttm_fini(adev);
1114 arch_phys_wc_del(adev->gmc.vram_mtrr);
1115 arch_io_free_memtype_wc(adev->gmc.aper_base, adev->gmc.aper_size);
1116 }
1117
1118 /**
1119 * amdgpu_bo_fbdev_mmap - mmap fbdev memory
1120 * @bo: &amdgpu_bo buffer object
1121 * @vma: vma as input from the fbdev mmap method
1122 *
1123 * Calls ttm_fbdev_mmap() to mmap fbdev memory if it is backed by a bo.
1124 *
1125 * Returns:
1126 * 0 for success or a negative error code on failure.
1127 */
1128 int amdgpu_bo_fbdev_mmap(struct amdgpu_bo *bo,
1129 struct vm_area_struct *vma)
1130 {
1131 if (vma->vm_pgoff != 0)
1132 return -EACCES;
1133
1134 return ttm_bo_mmap_obj(vma, &bo->tbo);
1135 }
1136
1137 /**
1138 * amdgpu_bo_set_tiling_flags - set tiling flags
1139 * @bo: &amdgpu_bo buffer object
1140 * @tiling_flags: new flags
1141 *
1142 * Sets buffer object's tiling flags with the new one. Used by GEM ioctl or
1143 * kernel driver to set the tiling flags on a buffer.
1144 *
1145 * Returns:
1146 * 0 for success or a negative error code on failure.
1147 */
1148 int amdgpu_bo_set_tiling_flags(struct amdgpu_bo *bo, u64 tiling_flags)
1149 {
1150 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
1151
1152 if (adev->family <= AMDGPU_FAMILY_CZ &&
1153 AMDGPU_TILING_GET(tiling_flags, TILE_SPLIT) > 6)
1154 return -EINVAL;
1155
1156 bo->tiling_flags = tiling_flags;
1157 return 0;
1158 }
1159
1160 /**
1161 * amdgpu_bo_get_tiling_flags - get tiling flags
1162 * @bo: &amdgpu_bo buffer object
1163 * @tiling_flags: returned flags
1164 *
1165 * Gets buffer object's tiling flags. Used by GEM ioctl or kernel driver to
1166 * set the tiling flags on a buffer.
1167 */
1168 void amdgpu_bo_get_tiling_flags(struct amdgpu_bo *bo, u64 *tiling_flags)
1169 {
1170 dma_resv_assert_held(bo->tbo.base.resv);
1171
1172 if (tiling_flags)
1173 *tiling_flags = bo->tiling_flags;
1174 }
1175
1176 /**
1177 * amdgpu_bo_set_metadata - set metadata
1178 * @bo: &amdgpu_bo buffer object
1179 * @metadata: new metadata
1180 * @metadata_size: size of the new metadata
1181 * @flags: flags of the new metadata
1182 *
1183 * Sets buffer object's metadata, its size and flags.
1184 * Used via GEM ioctl.
1185 *
1186 * Returns:
1187 * 0 for success or a negative error code on failure.
1188 */
1189 int amdgpu_bo_set_metadata (struct amdgpu_bo *bo, void *metadata,
1190 uint32_t metadata_size, uint64_t flags)
1191 {
1192 void *buffer;
1193
1194 if (!metadata_size) {
1195 if (bo->metadata_size) {
1196 kfree(bo->metadata);
1197 bo->metadata = NULL;
1198 bo->metadata_size = 0;
1199 }
1200 return 0;
1201 }
1202
1203 if (metadata == NULL)
1204 return -EINVAL;
1205
1206 buffer = kmemdup(metadata, metadata_size, GFP_KERNEL);
1207 if (buffer == NULL)
1208 return -ENOMEM;
1209
1210 kfree(bo->metadata);
1211 bo->metadata_flags = flags;
1212 bo->metadata = buffer;
1213 bo->metadata_size = metadata_size;
1214
1215 return 0;
1216 }
1217
1218 /**
1219 * amdgpu_bo_get_metadata - get metadata
1220 * @bo: &amdgpu_bo buffer object
1221 * @buffer: returned metadata
1222 * @buffer_size: size of the buffer
1223 * @metadata_size: size of the returned metadata
1224 * @flags: flags of the returned metadata
1225 *
1226 * Gets buffer object's metadata, its size and flags. buffer_size shall not be
1227 * less than metadata_size.
1228 * Used via GEM ioctl.
1229 *
1230 * Returns:
1231 * 0 for success or a negative error code on failure.
1232 */
1233 int amdgpu_bo_get_metadata(struct amdgpu_bo *bo, void *buffer,
1234 size_t buffer_size, uint32_t *metadata_size,
1235 uint64_t *flags)
1236 {
1237 if (!buffer && !metadata_size)
1238 return -EINVAL;
1239
1240 if (buffer) {
1241 if (buffer_size < bo->metadata_size)
1242 return -EINVAL;
1243
1244 if (bo->metadata_size)
1245 memcpy(buffer, bo->metadata, bo->metadata_size);
1246 }
1247
1248 if (metadata_size)
1249 *metadata_size = bo->metadata_size;
1250 if (flags)
1251 *flags = bo->metadata_flags;
1252
1253 return 0;
1254 }
1255
1256 /**
1257 * amdgpu_bo_move_notify - notification about a memory move
1258 * @bo: pointer to a buffer object
1259 * @evict: if this move is evicting the buffer from the graphics address space
1260 * @new_mem: new information of the bufer object
1261 *
1262 * Marks the corresponding &amdgpu_bo buffer object as invalid, also performs
1263 * bookkeeping.
1264 * TTM driver callback which is called when ttm moves a buffer.
1265 */
1266 void amdgpu_bo_move_notify(struct ttm_buffer_object *bo,
1267 bool evict,
1268 struct ttm_mem_reg *new_mem)
1269 {
1270 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
1271 struct amdgpu_bo *abo;
1272 struct ttm_mem_reg *old_mem = &bo->mem;
1273
1274 if (!amdgpu_bo_is_amdgpu_bo(bo))
1275 return;
1276
1277 abo = ttm_to_amdgpu_bo(bo);
1278 amdgpu_vm_bo_invalidate(adev, abo, evict);
1279
1280 amdgpu_bo_kunmap(abo);
1281
1282 /* remember the eviction */
1283 if (evict)
1284 atomic64_inc(&adev->num_evictions);
1285
1286 /* update statistics */
1287 if (!new_mem)
1288 return;
1289
1290 /* move_notify is called before move happens */
1291 trace_amdgpu_bo_move(abo, new_mem->mem_type, old_mem->mem_type);
1292 }
1293
1294 /**
1295 * amdgpu_bo_move_notify - notification about a BO being released
1296 * @bo: pointer to a buffer object
1297 *
1298 * Wipes VRAM buffers whose contents should not be leaked before the
1299 * memory is released.
1300 */
1301 void amdgpu_bo_release_notify(struct ttm_buffer_object *bo)
1302 {
1303 struct dma_fence *fence = NULL;
1304 struct amdgpu_bo *abo;
1305 int r;
1306
1307 if (!amdgpu_bo_is_amdgpu_bo(bo))
1308 return;
1309
1310 abo = ttm_to_amdgpu_bo(bo);
1311
1312 if (abo->kfd_bo)
1313 amdgpu_amdkfd_unreserve_memory_limit(abo);
1314
1315 if (bo->mem.mem_type != TTM_PL_VRAM || !bo->mem.mm_node ||
1316 !(abo->flags & AMDGPU_GEM_CREATE_VRAM_WIPE_ON_RELEASE))
1317 return;
1318
1319 dma_resv_lock(bo->base.resv, NULL);
1320
1321 r = amdgpu_fill_buffer(abo, AMDGPU_POISON, bo->base.resv, &fence);
1322 if (!WARN_ON(r)) {
1323 amdgpu_bo_fence(abo, fence, false);
1324 dma_fence_put(fence);
1325 }
1326
1327 dma_resv_unlock(bo->base.resv);
1328 }
1329
1330 /**
1331 * amdgpu_bo_fault_reserve_notify - notification about a memory fault
1332 * @bo: pointer to a buffer object
1333 *
1334 * Notifies the driver we are taking a fault on this BO and have reserved it,
1335 * also performs bookkeeping.
1336 * TTM driver callback for dealing with vm faults.
1337 *
1338 * Returns:
1339 * 0 for success or a negative error code on failure.
1340 */
1341 int amdgpu_bo_fault_reserve_notify(struct ttm_buffer_object *bo)
1342 {
1343 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
1344 struct ttm_operation_ctx ctx = { false, false };
1345 struct amdgpu_bo *abo;
1346 unsigned long offset, size;
1347 int r;
1348
1349 if (!amdgpu_bo_is_amdgpu_bo(bo))
1350 return 0;
1351
1352 abo = ttm_to_amdgpu_bo(bo);
1353
1354 /* Remember that this BO was accessed by the CPU */
1355 abo->flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
1356
1357 if (bo->mem.mem_type != TTM_PL_VRAM)
1358 return 0;
1359
1360 size = bo->mem.num_pages << PAGE_SHIFT;
1361 offset = bo->mem.start << PAGE_SHIFT;
1362 if ((offset + size) <= adev->gmc.visible_vram_size)
1363 return 0;
1364
1365 /* Can't move a pinned BO to visible VRAM */
1366 if (abo->pin_count > 0)
1367 return -EINVAL;
1368
1369 /* hurrah the memory is not visible ! */
1370 atomic64_inc(&adev->num_vram_cpu_page_faults);
1371 amdgpu_bo_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_VRAM |
1372 AMDGPU_GEM_DOMAIN_GTT);
1373
1374 /* Avoid costly evictions; only set GTT as a busy placement */
1375 abo->placement.num_busy_placement = 1;
1376 abo->placement.busy_placement = &abo->placements[1];
1377
1378 r = ttm_bo_validate(bo, &abo->placement, &ctx);
1379 if (unlikely(r != 0))
1380 return r;
1381
1382 offset = bo->mem.start << PAGE_SHIFT;
1383 /* this should never happen */
1384 if (bo->mem.mem_type == TTM_PL_VRAM &&
1385 (offset + size) > adev->gmc.visible_vram_size)
1386 return -EINVAL;
1387
1388 return 0;
1389 }
1390
1391 /**
1392 * amdgpu_bo_fence - add fence to buffer object
1393 *
1394 * @bo: buffer object in question
1395 * @fence: fence to add
1396 * @shared: true if fence should be added shared
1397 *
1398 */
1399 void amdgpu_bo_fence(struct amdgpu_bo *bo, struct dma_fence *fence,
1400 bool shared)
1401 {
1402 struct dma_resv *resv = bo->tbo.base.resv;
1403
1404 if (shared)
1405 dma_resv_add_shared_fence(resv, fence);
1406 else
1407 dma_resv_add_excl_fence(resv, fence);
1408 }
1409
1410 /**
1411 * amdgpu_sync_wait_resv - Wait for BO reservation fences
1412 *
1413 * @bo: buffer object
1414 * @owner: fence owner
1415 * @intr: Whether the wait is interruptible
1416 *
1417 * Returns:
1418 * 0 on success, errno otherwise.
1419 */
1420 int amdgpu_bo_sync_wait(struct amdgpu_bo *bo, void *owner, bool intr)
1421 {
1422 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
1423 struct amdgpu_sync sync;
1424 int r;
1425
1426 amdgpu_sync_create(&sync);
1427 amdgpu_sync_resv(adev, &sync, bo->tbo.base.resv, owner, false);
1428 r = amdgpu_sync_wait(&sync, intr);
1429 amdgpu_sync_free(&sync);
1430
1431 return r;
1432 }
1433
1434 /**
1435 * amdgpu_bo_gpu_offset - return GPU offset of bo
1436 * @bo: amdgpu object for which we query the offset
1437 *
1438 * Note: object should either be pinned or reserved when calling this
1439 * function, it might be useful to add check for this for debugging.
1440 *
1441 * Returns:
1442 * current GPU offset of the object.
1443 */
1444 u64 amdgpu_bo_gpu_offset(struct amdgpu_bo *bo)
1445 {
1446 WARN_ON_ONCE(bo->tbo.mem.mem_type == TTM_PL_SYSTEM);
1447 WARN_ON_ONCE(!dma_resv_is_locked(bo->tbo.base.resv) &&
1448 !bo->pin_count && bo->tbo.type != ttm_bo_type_kernel);
1449 WARN_ON_ONCE(bo->tbo.mem.start == AMDGPU_BO_INVALID_OFFSET);
1450 WARN_ON_ONCE(bo->tbo.mem.mem_type == TTM_PL_VRAM &&
1451 !(bo->flags & AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS));
1452
1453 return amdgpu_gmc_sign_extend(bo->tbo.offset);
1454 }
1455
1456 /**
1457 * amdgpu_bo_get_preferred_pin_domain - get preferred domain for scanout
1458 * @adev: amdgpu device object
1459 * @domain: allowed :ref:`memory domains <amdgpu_memory_domains>`
1460 *
1461 * Returns:
1462 * Which of the allowed domains is preferred for pinning the BO for scanout.
1463 */
1464 uint32_t amdgpu_bo_get_preferred_pin_domain(struct amdgpu_device *adev,
1465 uint32_t domain)
1466 {
1467 if (domain == (AMDGPU_GEM_DOMAIN_VRAM | AMDGPU_GEM_DOMAIN_GTT)) {
1468 domain = AMDGPU_GEM_DOMAIN_VRAM;
1469 if (adev->gmc.real_vram_size <= AMDGPU_SG_THRESHOLD)
1470 domain = AMDGPU_GEM_DOMAIN_GTT;
1471 }
1472 return domain;
1473 }
1474