2 * Copyright 2008 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
4 * Copyright 2009 Jerome Glisse.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
24 * Authors: Dave Airlie
28 #include <linux/dma-fence-array.h>
29 #include <linux/interval_tree_generic.h>
31 #include <drm/amdgpu_drm.h>
33 #include "amdgpu_trace.h"
37 * GPUVM is similar to the legacy gart on older asics, however
38 * rather than there being a single global gart table
39 * for the entire GPU, there are multiple VM page tables active
40 * at any given time. The VM page tables can contain a mix
41 * vram pages and system memory pages and system memory pages
42 * can be mapped as snooped (cached system pages) or unsnooped
43 * (uncached system pages).
44 * Each VM has an ID associated with it and there is a page table
45 * associated with each VMID. When execting a command buffer,
46 * the kernel tells the the ring what VMID to use for that command
47 * buffer. VMIDs are allocated dynamically as commands are submitted.
48 * The userspace drivers maintain their own address space and the kernel
49 * sets up their pages tables accordingly when they submit their
50 * command buffers and a VMID is assigned.
51 * Cayman/Trinity support up to 8 active VMs at any given time;
55 #define START(node) ((node)->start)
56 #define LAST(node) ((node)->last)
58 INTERVAL_TREE_DEFINE(struct amdgpu_bo_va_mapping
, rb
, uint64_t, __subtree_last
,
59 START
, LAST
, static, amdgpu_vm_it
)
64 /* Local structure. Encapsulate some VM table update parameters to reduce
65 * the number of function parameters
67 struct amdgpu_pte_update_params
{
68 /* amdgpu device we do this update for */
69 struct amdgpu_device
*adev
;
70 /* optional amdgpu_vm we do this update for */
72 /* address where to copy page table entries from */
74 /* indirect buffer to fill with commands */
76 /* Function which actually does the update */
77 void (*func
)(struct amdgpu_pte_update_params
*params
, uint64_t pe
,
78 uint64_t addr
, unsigned count
, uint32_t incr
,
80 /* The next two are used during VM update by CPU
81 * DMA addresses to use for mapping
82 * Kernel pointer of PD/PT BO that needs to be updated
84 dma_addr_t
*pages_addr
;
88 /* Helper to disable partial resident texture feature from a fence callback */
89 struct amdgpu_prt_cb
{
90 struct amdgpu_device
*adev
;
91 struct dma_fence_cb cb
;
95 * amdgpu_vm_num_entries - return the number of entries in a PD/PT
97 * @adev: amdgpu_device pointer
99 * Calculate the number of entries in a page directory or page table.
101 static unsigned amdgpu_vm_num_entries(struct amdgpu_device
*adev
,
105 /* For the root directory */
106 return adev
->vm_manager
.max_pfn
>>
107 (adev
->vm_manager
.block_size
*
108 adev
->vm_manager
.num_level
);
109 else if (level
== adev
->vm_manager
.num_level
)
110 /* For the page tables on the leaves */
111 return AMDGPU_VM_PTE_COUNT(adev
);
113 /* Everything in between */
114 return 1 << adev
->vm_manager
.block_size
;
118 * amdgpu_vm_bo_size - returns the size of the BOs in bytes
120 * @adev: amdgpu_device pointer
122 * Calculate the size of the BO for a page directory or page table in bytes.
124 static unsigned amdgpu_vm_bo_size(struct amdgpu_device
*adev
, unsigned level
)
126 return AMDGPU_GPU_PAGE_ALIGN(amdgpu_vm_num_entries(adev
, level
) * 8);
130 * amdgpu_vm_get_pd_bo - add the VM PD to a validation list
132 * @vm: vm providing the BOs
133 * @validated: head of validation list
134 * @entry: entry to add
136 * Add the page directory to the list of BOs to
137 * validate for command submission.
139 void amdgpu_vm_get_pd_bo(struct amdgpu_vm
*vm
,
140 struct list_head
*validated
,
141 struct amdgpu_bo_list_entry
*entry
)
143 entry
->robj
= vm
->root
.bo
;
145 entry
->tv
.bo
= &entry
->robj
->tbo
;
146 entry
->tv
.shared
= true;
147 entry
->user_pages
= NULL
;
148 list_add(&entry
->tv
.head
, validated
);
152 * amdgpu_vm_validate_layer - validate a single page table level
154 * @parent: parent page table level
155 * @validate: callback to do the validation
156 * @param: parameter for the validation callback
158 * Validate the page table BOs on command submission if neccessary.
160 static int amdgpu_vm_validate_level(struct amdgpu_vm_pt
*parent
,
161 int (*validate
)(void *, struct amdgpu_bo
*),
162 void *param
, bool use_cpu_for_update
)
167 if (use_cpu_for_update
) {
168 r
= amdgpu_bo_kmap(parent
->bo
, NULL
);
173 if (!parent
->entries
)
176 for (i
= 0; i
<= parent
->last_entry_used
; ++i
) {
177 struct amdgpu_vm_pt
*entry
= &parent
->entries
[i
];
182 r
= validate(param
, entry
->bo
);
187 * Recurse into the sub directory. This is harmless because we
188 * have only a maximum of 5 layers.
190 r
= amdgpu_vm_validate_level(entry
, validate
, param
,
200 * amdgpu_vm_validate_pt_bos - validate the page table BOs
202 * @adev: amdgpu device pointer
203 * @vm: vm providing the BOs
204 * @validate: callback to do the validation
205 * @param: parameter for the validation callback
207 * Validate the page table BOs on command submission if neccessary.
209 int amdgpu_vm_validate_pt_bos(struct amdgpu_device
*adev
, struct amdgpu_vm
*vm
,
210 int (*validate
)(void *p
, struct amdgpu_bo
*bo
),
213 uint64_t num_evictions
;
215 /* We only need to validate the page tables
216 * if they aren't already valid.
218 num_evictions
= atomic64_read(&adev
->num_evictions
);
219 if (num_evictions
== vm
->last_eviction_counter
)
222 return amdgpu_vm_validate_level(&vm
->root
, validate
, param
,
223 vm
->use_cpu_for_update
);
227 * amdgpu_vm_move_level_in_lru - move one level of PT BOs to the LRU tail
229 * @adev: amdgpu device instance
230 * @vm: vm providing the BOs
232 * Move the PT BOs to the tail of the LRU.
234 static void amdgpu_vm_move_level_in_lru(struct amdgpu_vm_pt
*parent
)
238 if (!parent
->entries
)
241 for (i
= 0; i
<= parent
->last_entry_used
; ++i
) {
242 struct amdgpu_vm_pt
*entry
= &parent
->entries
[i
];
247 ttm_bo_move_to_lru_tail(&entry
->bo
->tbo
);
248 amdgpu_vm_move_level_in_lru(entry
);
253 * amdgpu_vm_move_pt_bos_in_lru - move the PT BOs to the LRU tail
255 * @adev: amdgpu device instance
256 * @vm: vm providing the BOs
258 * Move the PT BOs to the tail of the LRU.
260 void amdgpu_vm_move_pt_bos_in_lru(struct amdgpu_device
*adev
,
261 struct amdgpu_vm
*vm
)
263 struct ttm_bo_global
*glob
= adev
->mman
.bdev
.glob
;
265 spin_lock(&glob
->lru_lock
);
266 amdgpu_vm_move_level_in_lru(&vm
->root
);
267 spin_unlock(&glob
->lru_lock
);
271 * amdgpu_vm_alloc_levels - allocate the PD/PT levels
273 * @adev: amdgpu_device pointer
275 * @saddr: start of the address range
276 * @eaddr: end of the address range
278 * Make sure the page directories and page tables are allocated
280 static int amdgpu_vm_alloc_levels(struct amdgpu_device
*adev
,
281 struct amdgpu_vm
*vm
,
282 struct amdgpu_vm_pt
*parent
,
283 uint64_t saddr
, uint64_t eaddr
,
286 unsigned shift
= (adev
->vm_manager
.num_level
- level
) *
287 adev
->vm_manager
.block_size
;
288 unsigned pt_idx
, from
, to
;
291 uint64_t init_value
= 0;
293 if (!parent
->entries
) {
294 unsigned num_entries
= amdgpu_vm_num_entries(adev
, level
);
296 parent
->entries
= kvmalloc_array(num_entries
,
297 sizeof(struct amdgpu_vm_pt
),
298 GFP_KERNEL
| __GFP_ZERO
);
299 if (!parent
->entries
)
301 memset(parent
->entries
, 0 , sizeof(struct amdgpu_vm_pt
));
304 from
= saddr
>> shift
;
306 if (from
>= amdgpu_vm_num_entries(adev
, level
) ||
307 to
>= amdgpu_vm_num_entries(adev
, level
))
310 if (to
> parent
->last_entry_used
)
311 parent
->last_entry_used
= to
;
314 saddr
= saddr
& ((1 << shift
) - 1);
315 eaddr
= eaddr
& ((1 << shift
) - 1);
317 flags
= AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS
|
318 AMDGPU_GEM_CREATE_VRAM_CLEARED
;
319 if (vm
->use_cpu_for_update
)
320 flags
|= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED
;
322 flags
|= (AMDGPU_GEM_CREATE_NO_CPU_ACCESS
|
323 AMDGPU_GEM_CREATE_SHADOW
);
325 if (vm
->pte_support_ats
) {
326 init_value
= AMDGPU_PTE_SYSTEM
;
327 if (level
!= adev
->vm_manager
.num_level
- 1)
328 init_value
|= AMDGPU_PDE_PTE
;
331 /* walk over the address space and allocate the page tables */
332 for (pt_idx
= from
; pt_idx
<= to
; ++pt_idx
) {
333 struct reservation_object
*resv
= vm
->root
.bo
->tbo
.resv
;
334 struct amdgpu_vm_pt
*entry
= &parent
->entries
[pt_idx
];
335 struct amdgpu_bo
*pt
;
338 r
= amdgpu_bo_create(adev
,
339 amdgpu_vm_bo_size(adev
, level
),
340 AMDGPU_GPU_PAGE_SIZE
, true,
341 AMDGPU_GEM_DOMAIN_VRAM
,
343 NULL
, resv
, init_value
, &pt
);
347 if (vm
->use_cpu_for_update
) {
348 r
= amdgpu_bo_kmap(pt
, NULL
);
350 amdgpu_bo_unref(&pt
);
355 /* Keep a reference to the root directory to avoid
356 * freeing them up in the wrong order.
358 pt
->parent
= amdgpu_bo_ref(vm
->root
.bo
);
362 entry
->huge_page
= false;
365 if (level
< adev
->vm_manager
.num_level
) {
366 uint64_t sub_saddr
= (pt_idx
== from
) ? saddr
: 0;
367 uint64_t sub_eaddr
= (pt_idx
== to
) ? eaddr
:
369 r
= amdgpu_vm_alloc_levels(adev
, vm
, entry
, sub_saddr
,
380 * amdgpu_vm_alloc_pts - Allocate page tables.
382 * @adev: amdgpu_device pointer
383 * @vm: VM to allocate page tables for
384 * @saddr: Start address which needs to be allocated
385 * @size: Size from start address we need.
387 * Make sure the page tables are allocated.
389 int amdgpu_vm_alloc_pts(struct amdgpu_device
*adev
,
390 struct amdgpu_vm
*vm
,
391 uint64_t saddr
, uint64_t size
)
396 /* validate the parameters */
397 if (saddr
& AMDGPU_GPU_PAGE_MASK
|| size
& AMDGPU_GPU_PAGE_MASK
)
400 eaddr
= saddr
+ size
- 1;
401 last_pfn
= eaddr
/ AMDGPU_GPU_PAGE_SIZE
;
402 if (last_pfn
>= adev
->vm_manager
.max_pfn
) {
403 dev_err(adev
->dev
, "va above limit (0x%08llX >= 0x%08llX)\n",
404 last_pfn
, adev
->vm_manager
.max_pfn
);
408 saddr
/= AMDGPU_GPU_PAGE_SIZE
;
409 eaddr
/= AMDGPU_GPU_PAGE_SIZE
;
411 return amdgpu_vm_alloc_levels(adev
, vm
, &vm
->root
, saddr
, eaddr
, 0);
415 * amdgpu_vm_had_gpu_reset - check if reset occured since last use
417 * @adev: amdgpu_device pointer
418 * @id: VMID structure
420 * Check if GPU reset occured since last use of the VMID.
422 static bool amdgpu_vm_had_gpu_reset(struct amdgpu_device
*adev
,
423 struct amdgpu_vm_id
*id
)
425 return id
->current_gpu_reset_count
!=
426 atomic_read(&adev
->gpu_reset_counter
);
429 static bool amdgpu_vm_reserved_vmid_ready(struct amdgpu_vm
*vm
, unsigned vmhub
)
431 return !!vm
->reserved_vmid
[vmhub
];
434 /* idr_mgr->lock must be held */
435 static int amdgpu_vm_grab_reserved_vmid_locked(struct amdgpu_vm
*vm
,
436 struct amdgpu_ring
*ring
,
437 struct amdgpu_sync
*sync
,
438 struct dma_fence
*fence
,
439 struct amdgpu_job
*job
)
441 struct amdgpu_device
*adev
= ring
->adev
;
442 unsigned vmhub
= ring
->funcs
->vmhub
;
443 uint64_t fence_context
= adev
->fence_context
+ ring
->idx
;
444 struct amdgpu_vm_id
*id
= vm
->reserved_vmid
[vmhub
];
445 struct amdgpu_vm_id_manager
*id_mgr
= &adev
->vm_manager
.id_mgr
[vmhub
];
446 struct dma_fence
*updates
= sync
->last_vm_update
;
448 struct dma_fence
*flushed
, *tmp
;
449 bool needs_flush
= vm
->use_cpu_for_update
;
451 flushed
= id
->flushed_updates
;
452 if ((amdgpu_vm_had_gpu_reset(adev
, id
)) ||
453 (atomic64_read(&id
->owner
) != vm
->client_id
) ||
454 (job
->vm_pd_addr
!= id
->pd_gpu_addr
) ||
455 (updates
&& (!flushed
|| updates
->context
!= flushed
->context
||
456 dma_fence_is_later(updates
, flushed
))) ||
457 (!id
->last_flush
|| (id
->last_flush
->context
!= fence_context
&&
458 !dma_fence_is_signaled(id
->last_flush
)))) {
460 /* to prevent one context starved by another context */
462 tmp
= amdgpu_sync_peek_fence(&id
->active
, ring
);
464 r
= amdgpu_sync_fence(adev
, sync
, tmp
);
469 /* Good we can use this VMID. Remember this submission as
472 r
= amdgpu_sync_fence(ring
->adev
, &id
->active
, fence
);
476 if (updates
&& (!flushed
|| updates
->context
!= flushed
->context
||
477 dma_fence_is_later(updates
, flushed
))) {
478 dma_fence_put(id
->flushed_updates
);
479 id
->flushed_updates
= dma_fence_get(updates
);
481 id
->pd_gpu_addr
= job
->vm_pd_addr
;
482 atomic64_set(&id
->owner
, vm
->client_id
);
483 job
->vm_needs_flush
= needs_flush
;
485 dma_fence_put(id
->last_flush
);
486 id
->last_flush
= NULL
;
488 job
->vm_id
= id
- id_mgr
->ids
;
489 trace_amdgpu_vm_grab_id(vm
, ring
, job
);
495 * amdgpu_vm_grab_id - allocate the next free VMID
497 * @vm: vm to allocate id for
498 * @ring: ring we want to submit job to
499 * @sync: sync object where we add dependencies
500 * @fence: fence protecting ID from reuse
502 * Allocate an id for the vm, adding fences to the sync obj as necessary.
504 int amdgpu_vm_grab_id(struct amdgpu_vm
*vm
, struct amdgpu_ring
*ring
,
505 struct amdgpu_sync
*sync
, struct dma_fence
*fence
,
506 struct amdgpu_job
*job
)
508 struct amdgpu_device
*adev
= ring
->adev
;
509 unsigned vmhub
= ring
->funcs
->vmhub
;
510 struct amdgpu_vm_id_manager
*id_mgr
= &adev
->vm_manager
.id_mgr
[vmhub
];
511 uint64_t fence_context
= adev
->fence_context
+ ring
->idx
;
512 struct dma_fence
*updates
= sync
->last_vm_update
;
513 struct amdgpu_vm_id
*id
, *idle
;
514 struct dma_fence
**fences
;
518 mutex_lock(&id_mgr
->lock
);
519 if (amdgpu_vm_reserved_vmid_ready(vm
, vmhub
)) {
520 r
= amdgpu_vm_grab_reserved_vmid_locked(vm
, ring
, sync
, fence
, job
);
521 mutex_unlock(&id_mgr
->lock
);
524 fences
= kmalloc_array(sizeof(void *), id_mgr
->num_ids
, GFP_KERNEL
);
526 mutex_unlock(&id_mgr
->lock
);
529 /* Check if we have an idle VMID */
531 list_for_each_entry(idle
, &id_mgr
->ids_lru
, list
) {
532 fences
[i
] = amdgpu_sync_peek_fence(&idle
->active
, ring
);
538 /* If we can't find a idle VMID to use, wait till one becomes available */
539 if (&idle
->list
== &id_mgr
->ids_lru
) {
540 u64 fence_context
= adev
->vm_manager
.fence_context
+ ring
->idx
;
541 unsigned seqno
= ++adev
->vm_manager
.seqno
[ring
->idx
];
542 struct dma_fence_array
*array
;
545 for (j
= 0; j
< i
; ++j
)
546 dma_fence_get(fences
[j
]);
548 array
= dma_fence_array_create(i
, fences
, fence_context
,
551 for (j
= 0; j
< i
; ++j
)
552 dma_fence_put(fences
[j
]);
559 r
= amdgpu_sync_fence(ring
->adev
, sync
, &array
->base
);
560 dma_fence_put(&array
->base
);
564 mutex_unlock(&id_mgr
->lock
);
570 job
->vm_needs_flush
= vm
->use_cpu_for_update
;
571 /* Check if we can use a VMID already assigned to this VM */
572 list_for_each_entry_reverse(id
, &id_mgr
->ids_lru
, list
) {
573 struct dma_fence
*flushed
;
574 bool needs_flush
= vm
->use_cpu_for_update
;
576 /* Check all the prerequisites to using this VMID */
577 if (amdgpu_vm_had_gpu_reset(adev
, id
))
580 if (atomic64_read(&id
->owner
) != vm
->client_id
)
583 if (job
->vm_pd_addr
!= id
->pd_gpu_addr
)
586 if (!id
->last_flush
||
587 (id
->last_flush
->context
!= fence_context
&&
588 !dma_fence_is_signaled(id
->last_flush
)))
591 flushed
= id
->flushed_updates
;
592 if (updates
&& (!flushed
|| dma_fence_is_later(updates
, flushed
)))
595 /* Concurrent flushes are only possible starting with Vega10 */
596 if (adev
->asic_type
< CHIP_VEGA10
&& needs_flush
)
599 /* Good we can use this VMID. Remember this submission as
602 r
= amdgpu_sync_fence(ring
->adev
, &id
->active
, fence
);
606 if (updates
&& (!flushed
|| dma_fence_is_later(updates
, flushed
))) {
607 dma_fence_put(id
->flushed_updates
);
608 id
->flushed_updates
= dma_fence_get(updates
);
614 goto no_flush_needed
;
618 /* Still no ID to use? Then use the idle one found earlier */
621 /* Remember this submission as user of the VMID */
622 r
= amdgpu_sync_fence(ring
->adev
, &id
->active
, fence
);
626 id
->pd_gpu_addr
= job
->vm_pd_addr
;
627 dma_fence_put(id
->flushed_updates
);
628 id
->flushed_updates
= dma_fence_get(updates
);
629 atomic64_set(&id
->owner
, vm
->client_id
);
632 job
->vm_needs_flush
= true;
633 dma_fence_put(id
->last_flush
);
634 id
->last_flush
= NULL
;
637 list_move_tail(&id
->list
, &id_mgr
->ids_lru
);
639 job
->vm_id
= id
- id_mgr
->ids
;
640 trace_amdgpu_vm_grab_id(vm
, ring
, job
);
643 mutex_unlock(&id_mgr
->lock
);
647 static void amdgpu_vm_free_reserved_vmid(struct amdgpu_device
*adev
,
648 struct amdgpu_vm
*vm
,
651 struct amdgpu_vm_id_manager
*id_mgr
= &adev
->vm_manager
.id_mgr
[vmhub
];
653 mutex_lock(&id_mgr
->lock
);
654 if (vm
->reserved_vmid
[vmhub
]) {
655 list_add(&vm
->reserved_vmid
[vmhub
]->list
,
657 vm
->reserved_vmid
[vmhub
] = NULL
;
658 atomic_dec(&id_mgr
->reserved_vmid_num
);
660 mutex_unlock(&id_mgr
->lock
);
663 static int amdgpu_vm_alloc_reserved_vmid(struct amdgpu_device
*adev
,
664 struct amdgpu_vm
*vm
,
667 struct amdgpu_vm_id_manager
*id_mgr
;
668 struct amdgpu_vm_id
*idle
;
671 id_mgr
= &adev
->vm_manager
.id_mgr
[vmhub
];
672 mutex_lock(&id_mgr
->lock
);
673 if (vm
->reserved_vmid
[vmhub
])
675 if (atomic_inc_return(&id_mgr
->reserved_vmid_num
) >
676 AMDGPU_VM_MAX_RESERVED_VMID
) {
677 DRM_ERROR("Over limitation of reserved vmid\n");
678 atomic_dec(&id_mgr
->reserved_vmid_num
);
682 /* Select the first entry VMID */
683 idle
= list_first_entry(&id_mgr
->ids_lru
, struct amdgpu_vm_id
, list
);
684 list_del_init(&idle
->list
);
685 vm
->reserved_vmid
[vmhub
] = idle
;
686 mutex_unlock(&id_mgr
->lock
);
690 mutex_unlock(&id_mgr
->lock
);
695 * amdgpu_vm_check_compute_bug - check whether asic has compute vm bug
697 * @adev: amdgpu_device pointer
699 void amdgpu_vm_check_compute_bug(struct amdgpu_device
*adev
)
701 const struct amdgpu_ip_block
*ip_block
;
702 bool has_compute_vm_bug
;
703 struct amdgpu_ring
*ring
;
706 has_compute_vm_bug
= false;
708 ip_block
= amdgpu_get_ip_block(adev
, AMD_IP_BLOCK_TYPE_GFX
);
710 /* Compute has a VM bug for GFX version < 7.
711 Compute has a VM bug for GFX 8 MEC firmware version < 673.*/
712 if (ip_block
->version
->major
<= 7)
713 has_compute_vm_bug
= true;
714 else if (ip_block
->version
->major
== 8)
715 if (adev
->gfx
.mec_fw_version
< 673)
716 has_compute_vm_bug
= true;
719 for (i
= 0; i
< adev
->num_rings
; i
++) {
720 ring
= adev
->rings
[i
];
721 if (ring
->funcs
->type
== AMDGPU_RING_TYPE_COMPUTE
)
722 /* only compute rings */
723 ring
->has_compute_vm_bug
= has_compute_vm_bug
;
725 ring
->has_compute_vm_bug
= false;
729 bool amdgpu_vm_need_pipeline_sync(struct amdgpu_ring
*ring
,
730 struct amdgpu_job
*job
)
732 struct amdgpu_device
*adev
= ring
->adev
;
733 unsigned vmhub
= ring
->funcs
->vmhub
;
734 struct amdgpu_vm_id_manager
*id_mgr
= &adev
->vm_manager
.id_mgr
[vmhub
];
735 struct amdgpu_vm_id
*id
;
736 bool gds_switch_needed
;
737 bool vm_flush_needed
= job
->vm_needs_flush
|| ring
->has_compute_vm_bug
;
741 id
= &id_mgr
->ids
[job
->vm_id
];
742 gds_switch_needed
= ring
->funcs
->emit_gds_switch
&& (
743 id
->gds_base
!= job
->gds_base
||
744 id
->gds_size
!= job
->gds_size
||
745 id
->gws_base
!= job
->gws_base
||
746 id
->gws_size
!= job
->gws_size
||
747 id
->oa_base
!= job
->oa_base
||
748 id
->oa_size
!= job
->oa_size
);
750 if (amdgpu_vm_had_gpu_reset(adev
, id
))
753 return vm_flush_needed
|| gds_switch_needed
;
756 static bool amdgpu_vm_is_large_bar(struct amdgpu_device
*adev
)
758 return (adev
->mc
.real_vram_size
== adev
->mc
.visible_vram_size
);
762 * amdgpu_vm_flush - hardware flush the vm
764 * @ring: ring to use for flush
765 * @vm_id: vmid number to use
766 * @pd_addr: address of the page directory
768 * Emit a VM flush when it is necessary.
770 int amdgpu_vm_flush(struct amdgpu_ring
*ring
, struct amdgpu_job
*job
, bool need_pipe_sync
)
772 struct amdgpu_device
*adev
= ring
->adev
;
773 unsigned vmhub
= ring
->funcs
->vmhub
;
774 struct amdgpu_vm_id_manager
*id_mgr
= &adev
->vm_manager
.id_mgr
[vmhub
];
775 struct amdgpu_vm_id
*id
= &id_mgr
->ids
[job
->vm_id
];
776 bool gds_switch_needed
= ring
->funcs
->emit_gds_switch
&& (
777 id
->gds_base
!= job
->gds_base
||
778 id
->gds_size
!= job
->gds_size
||
779 id
->gws_base
!= job
->gws_base
||
780 id
->gws_size
!= job
->gws_size
||
781 id
->oa_base
!= job
->oa_base
||
782 id
->oa_size
!= job
->oa_size
);
783 bool vm_flush_needed
= job
->vm_needs_flush
;
784 unsigned patch_offset
= 0;
787 if (amdgpu_vm_had_gpu_reset(adev
, id
)) {
788 gds_switch_needed
= true;
789 vm_flush_needed
= true;
792 if (!vm_flush_needed
&& !gds_switch_needed
&& !need_pipe_sync
)
795 if (ring
->funcs
->init_cond_exec
)
796 patch_offset
= amdgpu_ring_init_cond_exec(ring
);
799 amdgpu_ring_emit_pipeline_sync(ring
);
801 if (ring
->funcs
->emit_vm_flush
&& vm_flush_needed
) {
802 struct dma_fence
*fence
;
804 trace_amdgpu_vm_flush(ring
, job
->vm_id
, job
->vm_pd_addr
);
805 amdgpu_ring_emit_vm_flush(ring
, job
->vm_id
, job
->vm_pd_addr
);
807 r
= amdgpu_fence_emit(ring
, &fence
);
811 mutex_lock(&id_mgr
->lock
);
812 dma_fence_put(id
->last_flush
);
813 id
->last_flush
= fence
;
814 id
->current_gpu_reset_count
= atomic_read(&adev
->gpu_reset_counter
);
815 mutex_unlock(&id_mgr
->lock
);
818 if (ring
->funcs
->emit_gds_switch
&& gds_switch_needed
) {
819 id
->gds_base
= job
->gds_base
;
820 id
->gds_size
= job
->gds_size
;
821 id
->gws_base
= job
->gws_base
;
822 id
->gws_size
= job
->gws_size
;
823 id
->oa_base
= job
->oa_base
;
824 id
->oa_size
= job
->oa_size
;
825 amdgpu_ring_emit_gds_switch(ring
, job
->vm_id
, job
->gds_base
,
826 job
->gds_size
, job
->gws_base
,
827 job
->gws_size
, job
->oa_base
,
831 if (ring
->funcs
->patch_cond_exec
)
832 amdgpu_ring_patch_cond_exec(ring
, patch_offset
);
834 /* the double SWITCH_BUFFER here *cannot* be skipped by COND_EXEC */
835 if (ring
->funcs
->emit_switch_buffer
) {
836 amdgpu_ring_emit_switch_buffer(ring
);
837 amdgpu_ring_emit_switch_buffer(ring
);
843 * amdgpu_vm_reset_id - reset VMID to zero
845 * @adev: amdgpu device structure
846 * @vm_id: vmid number to use
848 * Reset saved GDW, GWS and OA to force switch on next flush.
850 void amdgpu_vm_reset_id(struct amdgpu_device
*adev
, unsigned vmhub
,
853 struct amdgpu_vm_id_manager
*id_mgr
= &adev
->vm_manager
.id_mgr
[vmhub
];
854 struct amdgpu_vm_id
*id
= &id_mgr
->ids
[vmid
];
856 atomic64_set(&id
->owner
, 0);
866 * amdgpu_vm_reset_all_id - reset VMID to zero
868 * @adev: amdgpu device structure
870 * Reset VMID to force flush on next use
872 void amdgpu_vm_reset_all_ids(struct amdgpu_device
*adev
)
876 for (i
= 0; i
< AMDGPU_MAX_VMHUBS
; ++i
) {
877 struct amdgpu_vm_id_manager
*id_mgr
=
878 &adev
->vm_manager
.id_mgr
[i
];
880 for (j
= 1; j
< id_mgr
->num_ids
; ++j
)
881 amdgpu_vm_reset_id(adev
, i
, j
);
886 * amdgpu_vm_bo_find - find the bo_va for a specific vm & bo
889 * @bo: requested buffer object
891 * Find @bo inside the requested vm.
892 * Search inside the @bos vm list for the requested vm
893 * Returns the found bo_va or NULL if none is found
895 * Object has to be reserved!
897 struct amdgpu_bo_va
*amdgpu_vm_bo_find(struct amdgpu_vm
*vm
,
898 struct amdgpu_bo
*bo
)
900 struct amdgpu_bo_va
*bo_va
;
902 list_for_each_entry(bo_va
, &bo
->va
, bo_list
) {
903 if (bo_va
->vm
== vm
) {
911 * amdgpu_vm_do_set_ptes - helper to call the right asic function
913 * @params: see amdgpu_pte_update_params definition
914 * @pe: addr of the page entry
915 * @addr: dst addr to write into pe
916 * @count: number of page entries to update
917 * @incr: increase next addr by incr bytes
918 * @flags: hw access flags
920 * Traces the parameters and calls the right asic functions
921 * to setup the page table using the DMA.
923 static void amdgpu_vm_do_set_ptes(struct amdgpu_pte_update_params
*params
,
924 uint64_t pe
, uint64_t addr
,
925 unsigned count
, uint32_t incr
,
928 trace_amdgpu_vm_set_ptes(pe
, addr
, count
, incr
, flags
);
931 amdgpu_vm_write_pte(params
->adev
, params
->ib
, pe
,
932 addr
| flags
, count
, incr
);
935 amdgpu_vm_set_pte_pde(params
->adev
, params
->ib
, pe
, addr
,
941 * amdgpu_vm_do_copy_ptes - copy the PTEs from the GART
943 * @params: see amdgpu_pte_update_params definition
944 * @pe: addr of the page entry
945 * @addr: dst addr to write into pe
946 * @count: number of page entries to update
947 * @incr: increase next addr by incr bytes
948 * @flags: hw access flags
950 * Traces the parameters and calls the DMA function to copy the PTEs.
952 static void amdgpu_vm_do_copy_ptes(struct amdgpu_pte_update_params
*params
,
953 uint64_t pe
, uint64_t addr
,
954 unsigned count
, uint32_t incr
,
957 uint64_t src
= (params
->src
+ (addr
>> 12) * 8);
960 trace_amdgpu_vm_copy_ptes(pe
, src
, count
);
962 amdgpu_vm_copy_pte(params
->adev
, params
->ib
, pe
, src
, count
);
966 * amdgpu_vm_map_gart - Resolve gart mapping of addr
968 * @pages_addr: optional DMA address to use for lookup
969 * @addr: the unmapped addr
971 * Look up the physical address of the page that the pte resolves
972 * to and return the pointer for the page table entry.
974 static uint64_t amdgpu_vm_map_gart(const dma_addr_t
*pages_addr
, uint64_t addr
)
978 /* page table offset */
979 result
= pages_addr
[addr
>> PAGE_SHIFT
];
981 /* in case cpu page size != gpu page size*/
982 result
|= addr
& (~PAGE_MASK
);
984 result
&= 0xFFFFFFFFFFFFF000ULL
;
990 * amdgpu_vm_cpu_set_ptes - helper to update page tables via CPU
992 * @params: see amdgpu_pte_update_params definition
993 * @pe: kmap addr of the page entry
994 * @addr: dst addr to write into pe
995 * @count: number of page entries to update
996 * @incr: increase next addr by incr bytes
997 * @flags: hw access flags
999 * Write count number of PT/PD entries directly.
1001 static void amdgpu_vm_cpu_set_ptes(struct amdgpu_pte_update_params
*params
,
1002 uint64_t pe
, uint64_t addr
,
1003 unsigned count
, uint32_t incr
,
1009 trace_amdgpu_vm_set_ptes(pe
, addr
, count
, incr
, flags
);
1011 for (i
= 0; i
< count
; i
++) {
1012 value
= params
->pages_addr
?
1013 amdgpu_vm_map_gart(params
->pages_addr
, addr
) :
1015 amdgpu_gart_set_pte_pde(params
->adev
, (void *)(uintptr_t)pe
,
1021 static int amdgpu_vm_wait_pd(struct amdgpu_device
*adev
, struct amdgpu_vm
*vm
,
1024 struct amdgpu_sync sync
;
1027 amdgpu_sync_create(&sync
);
1028 amdgpu_sync_resv(adev
, &sync
, vm
->root
.bo
->tbo
.resv
, owner
);
1029 r
= amdgpu_sync_wait(&sync
, true);
1030 amdgpu_sync_free(&sync
);
1036 * amdgpu_vm_update_level - update a single level in the hierarchy
1038 * @adev: amdgpu_device pointer
1040 * @parent: parent directory
1042 * Makes sure all entries in @parent are up to date.
1043 * Returns 0 for success, error for failure.
1045 static int amdgpu_vm_update_level(struct amdgpu_device
*adev
,
1046 struct amdgpu_vm
*vm
,
1047 struct amdgpu_vm_pt
*parent
,
1050 struct amdgpu_bo
*shadow
;
1051 struct amdgpu_ring
*ring
= NULL
;
1052 uint64_t pd_addr
, shadow_addr
= 0;
1053 uint32_t incr
= amdgpu_vm_bo_size(adev
, level
+ 1);
1054 uint64_t last_pde
= ~0, last_pt
= ~0, last_shadow
= ~0;
1055 unsigned count
= 0, pt_idx
, ndw
= 0;
1056 struct amdgpu_job
*job
;
1057 struct amdgpu_pte_update_params params
;
1058 struct dma_fence
*fence
= NULL
;
1062 if (!parent
->entries
)
1065 memset(¶ms
, 0, sizeof(params
));
1067 shadow
= parent
->bo
->shadow
;
1069 if (vm
->use_cpu_for_update
) {
1070 pd_addr
= (unsigned long)amdgpu_bo_kptr(parent
->bo
);
1071 r
= amdgpu_vm_wait_pd(adev
, vm
, AMDGPU_FENCE_OWNER_VM
);
1075 params
.func
= amdgpu_vm_cpu_set_ptes
;
1078 r
= amdgpu_ttm_bind(&shadow
->tbo
, &shadow
->tbo
.mem
);
1082 ring
= container_of(vm
->entity
.sched
, struct amdgpu_ring
,
1088 /* assume the worst case */
1089 ndw
+= parent
->last_entry_used
* 6;
1091 pd_addr
= amdgpu_bo_gpu_offset(parent
->bo
);
1094 shadow_addr
= amdgpu_bo_gpu_offset(shadow
);
1100 r
= amdgpu_job_alloc_with_ib(adev
, ndw
* 4, &job
);
1104 params
.ib
= &job
->ibs
[0];
1105 params
.func
= amdgpu_vm_do_set_ptes
;
1109 /* walk over the address space and update the directory */
1110 for (pt_idx
= 0; pt_idx
<= parent
->last_entry_used
; ++pt_idx
) {
1111 struct amdgpu_bo
*bo
= parent
->entries
[pt_idx
].bo
;
1118 struct amdgpu_bo
*pt_shadow
= bo
->shadow
;
1120 r
= amdgpu_ttm_bind(&pt_shadow
->tbo
,
1121 &pt_shadow
->tbo
.mem
);
1126 pt
= amdgpu_bo_gpu_offset(bo
);
1127 pt
= amdgpu_gart_get_vm_pde(adev
, pt
);
1128 if (parent
->entries
[pt_idx
].addr
== pt
||
1129 parent
->entries
[pt_idx
].huge_page
)
1132 parent
->entries
[pt_idx
].addr
= pt
;
1134 pde
= pd_addr
+ pt_idx
* 8;
1135 if (((last_pde
+ 8 * count
) != pde
) ||
1136 ((last_pt
+ incr
* count
) != pt
) ||
1137 (count
== AMDGPU_VM_MAX_UPDATE_SIZE
)) {
1141 params
.func(¶ms
,
1147 params
.func(¶ms
, last_pde
,
1148 last_pt
, count
, incr
,
1154 last_shadow
= shadow_addr
+ pt_idx
* 8;
1162 if (vm
->root
.bo
->shadow
)
1163 params
.func(¶ms
, last_shadow
, last_pt
,
1164 count
, incr
, AMDGPU_PTE_VALID
);
1166 params
.func(¶ms
, last_pde
, last_pt
,
1167 count
, incr
, AMDGPU_PTE_VALID
);
1170 if (!vm
->use_cpu_for_update
) {
1171 if (params
.ib
->length_dw
== 0) {
1172 amdgpu_job_free(job
);
1174 amdgpu_ring_pad_ib(ring
, params
.ib
);
1175 amdgpu_sync_resv(adev
, &job
->sync
, parent
->bo
->tbo
.resv
,
1176 AMDGPU_FENCE_OWNER_VM
);
1178 amdgpu_sync_resv(adev
, &job
->sync
,
1180 AMDGPU_FENCE_OWNER_VM
);
1182 WARN_ON(params
.ib
->length_dw
> ndw
);
1183 r
= amdgpu_job_submit(job
, ring
, &vm
->entity
,
1184 AMDGPU_FENCE_OWNER_VM
, &fence
);
1188 amdgpu_bo_fence(parent
->bo
, fence
, true);
1189 dma_fence_put(vm
->last_dir_update
);
1190 vm
->last_dir_update
= dma_fence_get(fence
);
1191 dma_fence_put(fence
);
1195 * Recurse into the subdirectories. This recursion is harmless because
1196 * we only have a maximum of 5 layers.
1198 for (pt_idx
= 0; pt_idx
<= parent
->last_entry_used
; ++pt_idx
) {
1199 struct amdgpu_vm_pt
*entry
= &parent
->entries
[pt_idx
];
1204 r
= amdgpu_vm_update_level(adev
, vm
, entry
, level
+ 1);
1212 amdgpu_job_free(job
);
1217 * amdgpu_vm_invalidate_level - mark all PD levels as invalid
1219 * @parent: parent PD
1221 * Mark all PD level as invalid after an error.
1223 static void amdgpu_vm_invalidate_level(struct amdgpu_vm_pt
*parent
)
1228 * Recurse into the subdirectories. This recursion is harmless because
1229 * we only have a maximum of 5 layers.
1231 for (pt_idx
= 0; pt_idx
<= parent
->last_entry_used
; ++pt_idx
) {
1232 struct amdgpu_vm_pt
*entry
= &parent
->entries
[pt_idx
];
1237 entry
->addr
= ~0ULL;
1238 amdgpu_vm_invalidate_level(entry
);
1243 * amdgpu_vm_update_directories - make sure that all directories are valid
1245 * @adev: amdgpu_device pointer
1248 * Makes sure all directories are up to date.
1249 * Returns 0 for success, error for failure.
1251 int amdgpu_vm_update_directories(struct amdgpu_device
*adev
,
1252 struct amdgpu_vm
*vm
)
1256 r
= amdgpu_vm_update_level(adev
, vm
, &vm
->root
, 0);
1258 amdgpu_vm_invalidate_level(&vm
->root
);
1260 if (vm
->use_cpu_for_update
) {
1263 amdgpu_gart_flush_gpu_tlb(adev
, 0);
1270 * amdgpu_vm_find_entry - find the entry for an address
1272 * @p: see amdgpu_pte_update_params definition
1273 * @addr: virtual address in question
1274 * @entry: resulting entry or NULL
1275 * @parent: parent entry
1277 * Find the vm_pt entry and it's parent for the given address.
1279 void amdgpu_vm_get_entry(struct amdgpu_pte_update_params
*p
, uint64_t addr
,
1280 struct amdgpu_vm_pt
**entry
,
1281 struct amdgpu_vm_pt
**parent
)
1283 unsigned idx
, level
= p
->adev
->vm_manager
.num_level
;
1286 *entry
= &p
->vm
->root
;
1287 while ((*entry
)->entries
) {
1288 idx
= addr
>> (p
->adev
->vm_manager
.block_size
* level
--);
1289 idx
%= amdgpu_bo_size((*entry
)->bo
) / 8;
1291 *entry
= &(*entry
)->entries
[idx
];
1299 * amdgpu_vm_handle_huge_pages - handle updating the PD with huge pages
1301 * @p: see amdgpu_pte_update_params definition
1302 * @entry: vm_pt entry to check
1303 * @parent: parent entry
1304 * @nptes: number of PTEs updated with this operation
1305 * @dst: destination address where the PTEs should point to
1306 * @flags: access flags fro the PTEs
1308 * Check if we can update the PD with a huge page.
1310 static int amdgpu_vm_handle_huge_pages(struct amdgpu_pte_update_params
*p
,
1311 struct amdgpu_vm_pt
*entry
,
1312 struct amdgpu_vm_pt
*parent
,
1313 unsigned nptes
, uint64_t dst
,
1316 bool use_cpu_update
= (p
->func
== amdgpu_vm_cpu_set_ptes
);
1317 uint64_t pd_addr
, pde
;
1320 /* In the case of a mixed PT the PDE must point to it*/
1321 if (p
->adev
->asic_type
< CHIP_VEGA10
||
1322 nptes
!= AMDGPU_VM_PTE_COUNT(p
->adev
) ||
1323 p
->func
== amdgpu_vm_do_copy_ptes
||
1324 !(flags
& AMDGPU_PTE_VALID
)) {
1326 dst
= amdgpu_bo_gpu_offset(entry
->bo
);
1327 dst
= amdgpu_gart_get_vm_pde(p
->adev
, dst
);
1328 flags
= AMDGPU_PTE_VALID
;
1330 flags
|= AMDGPU_PDE_PTE
;
1333 if (entry
->addr
== dst
&&
1334 entry
->huge_page
== !!(flags
& AMDGPU_PDE_PTE
))
1338 entry
->huge_page
= !!(flags
& AMDGPU_PDE_PTE
);
1340 if (use_cpu_update
) {
1341 r
= amdgpu_bo_kmap(parent
->bo
, (void *)&pd_addr
);
1345 pde
= pd_addr
+ (entry
- parent
->entries
) * 8;
1346 amdgpu_vm_cpu_set_ptes(p
, pde
, dst
, 1, 0, flags
);
1348 if (parent
->bo
->shadow
) {
1349 pd_addr
= amdgpu_bo_gpu_offset(parent
->bo
->shadow
);
1350 pde
= pd_addr
+ (entry
- parent
->entries
) * 8;
1351 amdgpu_vm_do_set_ptes(p
, pde
, dst
, 1, 0, flags
);
1353 pd_addr
= amdgpu_bo_gpu_offset(parent
->bo
);
1354 pde
= pd_addr
+ (entry
- parent
->entries
) * 8;
1355 amdgpu_vm_do_set_ptes(p
, pde
, dst
, 1, 0, flags
);
1362 * amdgpu_vm_update_ptes - make sure that page tables are valid
1364 * @params: see amdgpu_pte_update_params definition
1366 * @start: start of GPU address range
1367 * @end: end of GPU address range
1368 * @dst: destination address to map to, the next dst inside the function
1369 * @flags: mapping flags
1371 * Update the page tables in the range @start - @end.
1372 * Returns 0 for success, -EINVAL for failure.
1374 static int amdgpu_vm_update_ptes(struct amdgpu_pte_update_params
*params
,
1375 uint64_t start
, uint64_t end
,
1376 uint64_t dst
, uint64_t flags
)
1378 struct amdgpu_device
*adev
= params
->adev
;
1379 const uint64_t mask
= AMDGPU_VM_PTE_COUNT(adev
) - 1;
1381 uint64_t addr
, pe_start
;
1382 struct amdgpu_bo
*pt
;
1384 bool use_cpu_update
= (params
->func
== amdgpu_vm_cpu_set_ptes
);
1387 /* walk over the address space and update the page tables */
1388 for (addr
= start
; addr
< end
; addr
+= nptes
,
1389 dst
+= nptes
* AMDGPU_GPU_PAGE_SIZE
) {
1390 struct amdgpu_vm_pt
*entry
, *parent
;
1392 amdgpu_vm_get_entry(params
, addr
, &entry
, &parent
);
1396 if ((addr
& ~mask
) == (end
& ~mask
))
1399 nptes
= AMDGPU_VM_PTE_COUNT(adev
) - (addr
& mask
);
1401 r
= amdgpu_vm_handle_huge_pages(params
, entry
, parent
,
1406 if (entry
->huge_page
)
1410 if (use_cpu_update
) {
1411 pe_start
= (unsigned long)amdgpu_bo_kptr(pt
);
1414 pe_start
= amdgpu_bo_gpu_offset(pt
->shadow
);
1415 pe_start
+= (addr
& mask
) * 8;
1416 params
->func(params
, pe_start
, dst
, nptes
,
1417 AMDGPU_GPU_PAGE_SIZE
, flags
);
1419 pe_start
= amdgpu_bo_gpu_offset(pt
);
1422 pe_start
+= (addr
& mask
) * 8;
1423 params
->func(params
, pe_start
, dst
, nptes
,
1424 AMDGPU_GPU_PAGE_SIZE
, flags
);
1431 * amdgpu_vm_frag_ptes - add fragment information to PTEs
1433 * @params: see amdgpu_pte_update_params definition
1435 * @start: first PTE to handle
1436 * @end: last PTE to handle
1437 * @dst: addr those PTEs should point to
1438 * @flags: hw mapping flags
1439 * Returns 0 for success, -EINVAL for failure.
1441 static int amdgpu_vm_frag_ptes(struct amdgpu_pte_update_params
*params
,
1442 uint64_t start
, uint64_t end
,
1443 uint64_t dst
, uint64_t flags
)
1448 * The MC L1 TLB supports variable sized pages, based on a fragment
1449 * field in the PTE. When this field is set to a non-zero value, page
1450 * granularity is increased from 4KB to (1 << (12 + frag)). The PTE
1451 * flags are considered valid for all PTEs within the fragment range
1452 * and corresponding mappings are assumed to be physically contiguous.
1454 * The L1 TLB can store a single PTE for the whole fragment,
1455 * significantly increasing the space available for translation
1456 * caching. This leads to large improvements in throughput when the
1457 * TLB is under pressure.
1459 * The L2 TLB distributes small and large fragments into two
1460 * asymmetric partitions. The large fragment cache is significantly
1461 * larger. Thus, we try to use large fragments wherever possible.
1462 * Userspace can support this by aligning virtual base address and
1463 * allocation size to the fragment size.
1466 /* SI and newer are optimized for 64KB */
1467 unsigned pages_per_frag
= AMDGPU_LOG2_PAGES_PER_FRAG(params
->adev
);
1468 uint64_t frag_flags
= AMDGPU_PTE_FRAG(pages_per_frag
);
1469 uint64_t frag_align
= 1 << pages_per_frag
;
1471 uint64_t frag_start
= ALIGN(start
, frag_align
);
1472 uint64_t frag_end
= end
& ~(frag_align
- 1);
1474 /* system pages are non continuously */
1475 if (params
->src
|| !(flags
& AMDGPU_PTE_VALID
) ||
1476 (frag_start
>= frag_end
))
1477 return amdgpu_vm_update_ptes(params
, start
, end
, dst
, flags
);
1479 /* handle the 4K area at the beginning */
1480 if (start
!= frag_start
) {
1481 r
= amdgpu_vm_update_ptes(params
, start
, frag_start
,
1485 dst
+= (frag_start
- start
) * AMDGPU_GPU_PAGE_SIZE
;
1488 /* handle the area in the middle */
1489 r
= amdgpu_vm_update_ptes(params
, frag_start
, frag_end
, dst
,
1490 flags
| frag_flags
);
1494 /* handle the 4K area at the end */
1495 if (frag_end
!= end
) {
1496 dst
+= (frag_end
- frag_start
) * AMDGPU_GPU_PAGE_SIZE
;
1497 r
= amdgpu_vm_update_ptes(params
, frag_end
, end
, dst
, flags
);
1503 * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table
1505 * @adev: amdgpu_device pointer
1506 * @exclusive: fence we need to sync to
1507 * @src: address where to copy page table entries from
1508 * @pages_addr: DMA addresses to use for mapping
1510 * @start: start of mapped range
1511 * @last: last mapped entry
1512 * @flags: flags for the entries
1513 * @addr: addr to set the area to
1514 * @fence: optional resulting fence
1516 * Fill in the page table entries between @start and @last.
1517 * Returns 0 for success, -EINVAL for failure.
1519 static int amdgpu_vm_bo_update_mapping(struct amdgpu_device
*adev
,
1520 struct dma_fence
*exclusive
,
1522 dma_addr_t
*pages_addr
,
1523 struct amdgpu_vm
*vm
,
1524 uint64_t start
, uint64_t last
,
1525 uint64_t flags
, uint64_t addr
,
1526 struct dma_fence
**fence
)
1528 struct amdgpu_ring
*ring
;
1529 void *owner
= AMDGPU_FENCE_OWNER_VM
;
1530 unsigned nptes
, ncmds
, ndw
;
1531 struct amdgpu_job
*job
;
1532 struct amdgpu_pte_update_params params
;
1533 struct dma_fence
*f
= NULL
;
1536 memset(¶ms
, 0, sizeof(params
));
1541 /* sync to everything on unmapping */
1542 if (!(flags
& AMDGPU_PTE_VALID
))
1543 owner
= AMDGPU_FENCE_OWNER_UNDEFINED
;
1545 if (vm
->use_cpu_for_update
) {
1546 /* params.src is used as flag to indicate system Memory */
1550 /* Wait for PT BOs to be free. PTs share the same resv. object
1553 r
= amdgpu_vm_wait_pd(adev
, vm
, owner
);
1557 params
.func
= amdgpu_vm_cpu_set_ptes
;
1558 params
.pages_addr
= pages_addr
;
1559 return amdgpu_vm_frag_ptes(¶ms
, start
, last
+ 1,
1563 ring
= container_of(vm
->entity
.sched
, struct amdgpu_ring
, sched
);
1565 nptes
= last
- start
+ 1;
1568 * reserve space for one command every (1 << BLOCK_SIZE)
1569 * entries or 2k dwords (whatever is smaller)
1571 ncmds
= (nptes
>> min(adev
->vm_manager
.block_size
, 11u)) + 1;
1576 /* one PDE write for each huge page */
1577 ndw
+= ((nptes
>> adev
->vm_manager
.block_size
) + 1) * 6;
1580 /* only copy commands needed */
1583 params
.func
= amdgpu_vm_do_copy_ptes
;
1585 } else if (pages_addr
) {
1586 /* copy commands needed */
1592 params
.func
= amdgpu_vm_do_copy_ptes
;
1595 /* set page commands needed */
1598 /* two extra commands for begin/end of fragment */
1601 params
.func
= amdgpu_vm_do_set_ptes
;
1604 r
= amdgpu_job_alloc_with_ib(adev
, ndw
* 4, &job
);
1608 params
.ib
= &job
->ibs
[0];
1610 if (!src
&& pages_addr
) {
1614 /* Put the PTEs at the end of the IB. */
1615 i
= ndw
- nptes
* 2;
1616 pte
= (uint64_t *)&(job
->ibs
->ptr
[i
]);
1617 params
.src
= job
->ibs
->gpu_addr
+ i
* 4;
1619 for (i
= 0; i
< nptes
; ++i
) {
1620 pte
[i
] = amdgpu_vm_map_gart(pages_addr
, addr
+ i
*
1621 AMDGPU_GPU_PAGE_SIZE
);
1627 r
= amdgpu_sync_fence(adev
, &job
->sync
, exclusive
);
1631 r
= amdgpu_sync_resv(adev
, &job
->sync
, vm
->root
.bo
->tbo
.resv
,
1636 r
= reservation_object_reserve_shared(vm
->root
.bo
->tbo
.resv
);
1640 r
= amdgpu_vm_frag_ptes(¶ms
, start
, last
+ 1, addr
, flags
);
1644 amdgpu_ring_pad_ib(ring
, params
.ib
);
1645 WARN_ON(params
.ib
->length_dw
> ndw
);
1646 r
= amdgpu_job_submit(job
, ring
, &vm
->entity
,
1647 AMDGPU_FENCE_OWNER_VM
, &f
);
1651 amdgpu_bo_fence(vm
->root
.bo
, f
, true);
1652 dma_fence_put(*fence
);
1657 amdgpu_job_free(job
);
1658 amdgpu_vm_invalidate_level(&vm
->root
);
1663 * amdgpu_vm_bo_split_mapping - split a mapping into smaller chunks
1665 * @adev: amdgpu_device pointer
1666 * @exclusive: fence we need to sync to
1667 * @gtt_flags: flags as they are used for GTT
1668 * @pages_addr: DMA addresses to use for mapping
1670 * @mapping: mapped range and flags to use for the update
1671 * @flags: HW flags for the mapping
1672 * @nodes: array of drm_mm_nodes with the MC addresses
1673 * @fence: optional resulting fence
1675 * Split the mapping into smaller chunks so that each update fits
1677 * Returns 0 for success, -EINVAL for failure.
1679 static int amdgpu_vm_bo_split_mapping(struct amdgpu_device
*adev
,
1680 struct dma_fence
*exclusive
,
1682 dma_addr_t
*pages_addr
,
1683 struct amdgpu_vm
*vm
,
1684 struct amdgpu_bo_va_mapping
*mapping
,
1686 struct drm_mm_node
*nodes
,
1687 struct dma_fence
**fence
)
1689 uint64_t pfn
, src
= 0, start
= mapping
->start
;
1692 /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here
1693 * but in case of something, we filter the flags in first place
1695 if (!(mapping
->flags
& AMDGPU_PTE_READABLE
))
1696 flags
&= ~AMDGPU_PTE_READABLE
;
1697 if (!(mapping
->flags
& AMDGPU_PTE_WRITEABLE
))
1698 flags
&= ~AMDGPU_PTE_WRITEABLE
;
1700 flags
&= ~AMDGPU_PTE_EXECUTABLE
;
1701 flags
|= mapping
->flags
& AMDGPU_PTE_EXECUTABLE
;
1703 flags
&= ~AMDGPU_PTE_MTYPE_MASK
;
1704 flags
|= (mapping
->flags
& AMDGPU_PTE_MTYPE_MASK
);
1706 if ((mapping
->flags
& AMDGPU_PTE_PRT
) &&
1707 (adev
->asic_type
>= CHIP_VEGA10
)) {
1708 flags
|= AMDGPU_PTE_PRT
;
1709 flags
&= ~AMDGPU_PTE_VALID
;
1712 trace_amdgpu_vm_bo_update(mapping
);
1714 pfn
= mapping
->offset
>> PAGE_SHIFT
;
1716 while (pfn
>= nodes
->size
) {
1723 uint64_t max_entries
;
1724 uint64_t addr
, last
;
1727 addr
= nodes
->start
<< PAGE_SHIFT
;
1728 max_entries
= (nodes
->size
- pfn
) *
1729 (PAGE_SIZE
/ AMDGPU_GPU_PAGE_SIZE
);
1732 max_entries
= S64_MAX
;
1736 if (flags
== gtt_flags
)
1737 src
= adev
->gart
.table_addr
+
1738 (addr
>> AMDGPU_GPU_PAGE_SHIFT
) * 8;
1740 max_entries
= min(max_entries
, 16ull * 1024ull);
1742 } else if (flags
& AMDGPU_PTE_VALID
) {
1743 addr
+= adev
->vm_manager
.vram_base_offset
;
1745 addr
+= pfn
<< PAGE_SHIFT
;
1747 last
= min((uint64_t)mapping
->last
, start
+ max_entries
- 1);
1748 r
= amdgpu_vm_bo_update_mapping(adev
, exclusive
,
1749 src
, pages_addr
, vm
,
1750 start
, last
, flags
, addr
,
1755 pfn
+= last
- start
+ 1;
1756 if (nodes
&& nodes
->size
== pfn
) {
1762 } while (unlikely(start
!= mapping
->last
+ 1));
1768 * amdgpu_vm_bo_update - update all BO mappings in the vm page table
1770 * @adev: amdgpu_device pointer
1771 * @bo_va: requested BO and VM object
1772 * @clear: if true clear the entries
1774 * Fill in the page table entries for @bo_va.
1775 * Returns 0 for success, -EINVAL for failure.
1777 int amdgpu_vm_bo_update(struct amdgpu_device
*adev
,
1778 struct amdgpu_bo_va
*bo_va
,
1781 struct amdgpu_vm
*vm
= bo_va
->vm
;
1782 struct amdgpu_bo_va_mapping
*mapping
;
1783 dma_addr_t
*pages_addr
= NULL
;
1784 uint64_t gtt_flags
, flags
;
1785 struct ttm_mem_reg
*mem
;
1786 struct drm_mm_node
*nodes
;
1787 struct dma_fence
*exclusive
;
1790 if (clear
|| !bo_va
->bo
) {
1795 struct ttm_dma_tt
*ttm
;
1797 mem
= &bo_va
->bo
->tbo
.mem
;
1798 nodes
= mem
->mm_node
;
1799 if (mem
->mem_type
== TTM_PL_TT
) {
1800 ttm
= container_of(bo_va
->bo
->tbo
.ttm
, struct
1802 pages_addr
= ttm
->dma_address
;
1804 exclusive
= reservation_object_get_excl(bo_va
->bo
->tbo
.resv
);
1808 flags
= amdgpu_ttm_tt_pte_flags(adev
, bo_va
->bo
->tbo
.ttm
, mem
);
1809 gtt_flags
= (amdgpu_ttm_is_bound(bo_va
->bo
->tbo
.ttm
) &&
1810 adev
== amdgpu_ttm_adev(bo_va
->bo
->tbo
.bdev
)) ?
1817 spin_lock(&vm
->status_lock
);
1818 if (!list_empty(&bo_va
->vm_status
))
1819 list_splice_init(&bo_va
->valids
, &bo_va
->invalids
);
1820 spin_unlock(&vm
->status_lock
);
1822 list_for_each_entry(mapping
, &bo_va
->invalids
, list
) {
1823 r
= amdgpu_vm_bo_split_mapping(adev
, exclusive
,
1824 gtt_flags
, pages_addr
, vm
,
1825 mapping
, flags
, nodes
,
1826 &bo_va
->last_pt_update
);
1831 if (trace_amdgpu_vm_bo_mapping_enabled()) {
1832 list_for_each_entry(mapping
, &bo_va
->valids
, list
)
1833 trace_amdgpu_vm_bo_mapping(mapping
);
1835 list_for_each_entry(mapping
, &bo_va
->invalids
, list
)
1836 trace_amdgpu_vm_bo_mapping(mapping
);
1839 spin_lock(&vm
->status_lock
);
1840 list_splice_init(&bo_va
->invalids
, &bo_va
->valids
);
1841 list_del_init(&bo_va
->vm_status
);
1843 list_add(&bo_va
->vm_status
, &vm
->cleared
);
1844 spin_unlock(&vm
->status_lock
);
1846 if (vm
->use_cpu_for_update
) {
1849 amdgpu_gart_flush_gpu_tlb(adev
, 0);
1856 * amdgpu_vm_update_prt_state - update the global PRT state
1858 static void amdgpu_vm_update_prt_state(struct amdgpu_device
*adev
)
1860 unsigned long flags
;
1863 spin_lock_irqsave(&adev
->vm_manager
.prt_lock
, flags
);
1864 enable
= !!atomic_read(&adev
->vm_manager
.num_prt_users
);
1865 adev
->gart
.gart_funcs
->set_prt(adev
, enable
);
1866 spin_unlock_irqrestore(&adev
->vm_manager
.prt_lock
, flags
);
1870 * amdgpu_vm_prt_get - add a PRT user
1872 static void amdgpu_vm_prt_get(struct amdgpu_device
*adev
)
1874 if (!adev
->gart
.gart_funcs
->set_prt
)
1877 if (atomic_inc_return(&adev
->vm_manager
.num_prt_users
) == 1)
1878 amdgpu_vm_update_prt_state(adev
);
1882 * amdgpu_vm_prt_put - drop a PRT user
1884 static void amdgpu_vm_prt_put(struct amdgpu_device
*adev
)
1886 if (atomic_dec_return(&adev
->vm_manager
.num_prt_users
) == 0)
1887 amdgpu_vm_update_prt_state(adev
);
1891 * amdgpu_vm_prt_cb - callback for updating the PRT status
1893 static void amdgpu_vm_prt_cb(struct dma_fence
*fence
, struct dma_fence_cb
*_cb
)
1895 struct amdgpu_prt_cb
*cb
= container_of(_cb
, struct amdgpu_prt_cb
, cb
);
1897 amdgpu_vm_prt_put(cb
->adev
);
1902 * amdgpu_vm_add_prt_cb - add callback for updating the PRT status
1904 static void amdgpu_vm_add_prt_cb(struct amdgpu_device
*adev
,
1905 struct dma_fence
*fence
)
1907 struct amdgpu_prt_cb
*cb
;
1909 if (!adev
->gart
.gart_funcs
->set_prt
)
1912 cb
= kmalloc(sizeof(struct amdgpu_prt_cb
), GFP_KERNEL
);
1914 /* Last resort when we are OOM */
1916 dma_fence_wait(fence
, false);
1918 amdgpu_vm_prt_put(adev
);
1921 if (!fence
|| dma_fence_add_callback(fence
, &cb
->cb
,
1923 amdgpu_vm_prt_cb(fence
, &cb
->cb
);
1928 * amdgpu_vm_free_mapping - free a mapping
1930 * @adev: amdgpu_device pointer
1932 * @mapping: mapping to be freed
1933 * @fence: fence of the unmap operation
1935 * Free a mapping and make sure we decrease the PRT usage count if applicable.
1937 static void amdgpu_vm_free_mapping(struct amdgpu_device
*adev
,
1938 struct amdgpu_vm
*vm
,
1939 struct amdgpu_bo_va_mapping
*mapping
,
1940 struct dma_fence
*fence
)
1942 if (mapping
->flags
& AMDGPU_PTE_PRT
)
1943 amdgpu_vm_add_prt_cb(adev
, fence
);
1948 * amdgpu_vm_prt_fini - finish all prt mappings
1950 * @adev: amdgpu_device pointer
1953 * Register a cleanup callback to disable PRT support after VM dies.
1955 static void amdgpu_vm_prt_fini(struct amdgpu_device
*adev
, struct amdgpu_vm
*vm
)
1957 struct reservation_object
*resv
= vm
->root
.bo
->tbo
.resv
;
1958 struct dma_fence
*excl
, **shared
;
1959 unsigned i
, shared_count
;
1962 r
= reservation_object_get_fences_rcu(resv
, &excl
,
1963 &shared_count
, &shared
);
1965 /* Not enough memory to grab the fence list, as last resort
1966 * block for all the fences to complete.
1968 reservation_object_wait_timeout_rcu(resv
, true, false,
1969 MAX_SCHEDULE_TIMEOUT
);
1973 /* Add a callback for each fence in the reservation object */
1974 amdgpu_vm_prt_get(adev
);
1975 amdgpu_vm_add_prt_cb(adev
, excl
);
1977 for (i
= 0; i
< shared_count
; ++i
) {
1978 amdgpu_vm_prt_get(adev
);
1979 amdgpu_vm_add_prt_cb(adev
, shared
[i
]);
1986 * amdgpu_vm_clear_freed - clear freed BOs in the PT
1988 * @adev: amdgpu_device pointer
1990 * @fence: optional resulting fence (unchanged if no work needed to be done
1991 * or if an error occurred)
1993 * Make sure all freed BOs are cleared in the PT.
1994 * Returns 0 for success.
1996 * PTs have to be reserved and mutex must be locked!
1998 int amdgpu_vm_clear_freed(struct amdgpu_device
*adev
,
1999 struct amdgpu_vm
*vm
,
2000 struct dma_fence
**fence
)
2002 struct amdgpu_bo_va_mapping
*mapping
;
2003 struct dma_fence
*f
= NULL
;
2005 uint64_t init_pte_value
= 0;
2007 while (!list_empty(&vm
->freed
)) {
2008 mapping
= list_first_entry(&vm
->freed
,
2009 struct amdgpu_bo_va_mapping
, list
);
2010 list_del(&mapping
->list
);
2012 if (vm
->pte_support_ats
)
2013 init_pte_value
= AMDGPU_PTE_SYSTEM
;
2015 r
= amdgpu_vm_bo_update_mapping(adev
, NULL
, 0, NULL
, vm
,
2016 mapping
->start
, mapping
->last
,
2017 init_pte_value
, 0, &f
);
2018 amdgpu_vm_free_mapping(adev
, vm
, mapping
, f
);
2026 dma_fence_put(*fence
);
2037 * amdgpu_vm_clear_invalids - clear invalidated BOs in the PT
2039 * @adev: amdgpu_device pointer
2042 * Make sure all invalidated BOs are cleared in the PT.
2043 * Returns 0 for success.
2045 * PTs have to be reserved and mutex must be locked!
2047 int amdgpu_vm_clear_invalids(struct amdgpu_device
*adev
,
2048 struct amdgpu_vm
*vm
, struct amdgpu_sync
*sync
)
2050 struct amdgpu_bo_va
*bo_va
= NULL
;
2053 spin_lock(&vm
->status_lock
);
2054 while (!list_empty(&vm
->invalidated
)) {
2055 bo_va
= list_first_entry(&vm
->invalidated
,
2056 struct amdgpu_bo_va
, vm_status
);
2057 spin_unlock(&vm
->status_lock
);
2059 r
= amdgpu_vm_bo_update(adev
, bo_va
, true);
2063 spin_lock(&vm
->status_lock
);
2065 spin_unlock(&vm
->status_lock
);
2068 r
= amdgpu_sync_fence(adev
, sync
, bo_va
->last_pt_update
);
2074 * amdgpu_vm_bo_add - add a bo to a specific vm
2076 * @adev: amdgpu_device pointer
2078 * @bo: amdgpu buffer object
2080 * Add @bo into the requested vm.
2081 * Add @bo to the list of bos associated with the vm
2082 * Returns newly added bo_va or NULL for failure
2084 * Object has to be reserved!
2086 struct amdgpu_bo_va
*amdgpu_vm_bo_add(struct amdgpu_device
*adev
,
2087 struct amdgpu_vm
*vm
,
2088 struct amdgpu_bo
*bo
)
2090 struct amdgpu_bo_va
*bo_va
;
2092 bo_va
= kzalloc(sizeof(struct amdgpu_bo_va
), GFP_KERNEL
);
2093 if (bo_va
== NULL
) {
2098 bo_va
->ref_count
= 1;
2099 INIT_LIST_HEAD(&bo_va
->bo_list
);
2100 INIT_LIST_HEAD(&bo_va
->valids
);
2101 INIT_LIST_HEAD(&bo_va
->invalids
);
2102 INIT_LIST_HEAD(&bo_va
->vm_status
);
2105 list_add_tail(&bo_va
->bo_list
, &bo
->va
);
2111 * amdgpu_vm_bo_map - map bo inside a vm
2113 * @adev: amdgpu_device pointer
2114 * @bo_va: bo_va to store the address
2115 * @saddr: where to map the BO
2116 * @offset: requested offset in the BO
2117 * @flags: attributes of pages (read/write/valid/etc.)
2119 * Add a mapping of the BO at the specefied addr into the VM.
2120 * Returns 0 for success, error for failure.
2122 * Object has to be reserved and unreserved outside!
2124 int amdgpu_vm_bo_map(struct amdgpu_device
*adev
,
2125 struct amdgpu_bo_va
*bo_va
,
2126 uint64_t saddr
, uint64_t offset
,
2127 uint64_t size
, uint64_t flags
)
2129 struct amdgpu_bo_va_mapping
*mapping
, *tmp
;
2130 struct amdgpu_vm
*vm
= bo_va
->vm
;
2133 /* validate the parameters */
2134 if (saddr
& AMDGPU_GPU_PAGE_MASK
|| offset
& AMDGPU_GPU_PAGE_MASK
||
2135 size
== 0 || size
& AMDGPU_GPU_PAGE_MASK
)
2138 /* make sure object fit at this offset */
2139 eaddr
= saddr
+ size
- 1;
2140 if (saddr
>= eaddr
||
2141 (bo_va
->bo
&& offset
+ size
> amdgpu_bo_size(bo_va
->bo
)))
2144 saddr
/= AMDGPU_GPU_PAGE_SIZE
;
2145 eaddr
/= AMDGPU_GPU_PAGE_SIZE
;
2147 tmp
= amdgpu_vm_it_iter_first(&vm
->va
, saddr
, eaddr
);
2149 /* bo and tmp overlap, invalid addr */
2150 dev_err(adev
->dev
, "bo %p va 0x%010Lx-0x%010Lx conflict with "
2151 "0x%010Lx-0x%010Lx\n", bo_va
->bo
, saddr
, eaddr
,
2152 tmp
->start
, tmp
->last
+ 1);
2156 mapping
= kmalloc(sizeof(*mapping
), GFP_KERNEL
);
2160 INIT_LIST_HEAD(&mapping
->list
);
2161 mapping
->start
= saddr
;
2162 mapping
->last
= eaddr
;
2163 mapping
->offset
= offset
;
2164 mapping
->flags
= flags
;
2166 list_add(&mapping
->list
, &bo_va
->invalids
);
2167 amdgpu_vm_it_insert(mapping
, &vm
->va
);
2169 if (flags
& AMDGPU_PTE_PRT
)
2170 amdgpu_vm_prt_get(adev
);
2176 * amdgpu_vm_bo_replace_map - map bo inside a vm, replacing existing mappings
2178 * @adev: amdgpu_device pointer
2179 * @bo_va: bo_va to store the address
2180 * @saddr: where to map the BO
2181 * @offset: requested offset in the BO
2182 * @flags: attributes of pages (read/write/valid/etc.)
2184 * Add a mapping of the BO at the specefied addr into the VM. Replace existing
2185 * mappings as we do so.
2186 * Returns 0 for success, error for failure.
2188 * Object has to be reserved and unreserved outside!
2190 int amdgpu_vm_bo_replace_map(struct amdgpu_device
*adev
,
2191 struct amdgpu_bo_va
*bo_va
,
2192 uint64_t saddr
, uint64_t offset
,
2193 uint64_t size
, uint64_t flags
)
2195 struct amdgpu_bo_va_mapping
*mapping
;
2196 struct amdgpu_vm
*vm
= bo_va
->vm
;
2200 /* validate the parameters */
2201 if (saddr
& AMDGPU_GPU_PAGE_MASK
|| offset
& AMDGPU_GPU_PAGE_MASK
||
2202 size
== 0 || size
& AMDGPU_GPU_PAGE_MASK
)
2205 /* make sure object fit at this offset */
2206 eaddr
= saddr
+ size
- 1;
2207 if (saddr
>= eaddr
||
2208 (bo_va
->bo
&& offset
+ size
> amdgpu_bo_size(bo_va
->bo
)))
2211 /* Allocate all the needed memory */
2212 mapping
= kmalloc(sizeof(*mapping
), GFP_KERNEL
);
2216 r
= amdgpu_vm_bo_clear_mappings(adev
, bo_va
->vm
, saddr
, size
);
2222 saddr
/= AMDGPU_GPU_PAGE_SIZE
;
2223 eaddr
/= AMDGPU_GPU_PAGE_SIZE
;
2225 mapping
->start
= saddr
;
2226 mapping
->last
= eaddr
;
2227 mapping
->offset
= offset
;
2228 mapping
->flags
= flags
;
2230 list_add(&mapping
->list
, &bo_va
->invalids
);
2231 amdgpu_vm_it_insert(mapping
, &vm
->va
);
2233 if (flags
& AMDGPU_PTE_PRT
)
2234 amdgpu_vm_prt_get(adev
);
2240 * amdgpu_vm_bo_unmap - remove bo mapping from vm
2242 * @adev: amdgpu_device pointer
2243 * @bo_va: bo_va to remove the address from
2244 * @saddr: where to the BO is mapped
2246 * Remove a mapping of the BO at the specefied addr from the VM.
2247 * Returns 0 for success, error for failure.
2249 * Object has to be reserved and unreserved outside!
2251 int amdgpu_vm_bo_unmap(struct amdgpu_device
*adev
,
2252 struct amdgpu_bo_va
*bo_va
,
2255 struct amdgpu_bo_va_mapping
*mapping
;
2256 struct amdgpu_vm
*vm
= bo_va
->vm
;
2259 saddr
/= AMDGPU_GPU_PAGE_SIZE
;
2261 list_for_each_entry(mapping
, &bo_va
->valids
, list
) {
2262 if (mapping
->start
== saddr
)
2266 if (&mapping
->list
== &bo_va
->valids
) {
2269 list_for_each_entry(mapping
, &bo_va
->invalids
, list
) {
2270 if (mapping
->start
== saddr
)
2274 if (&mapping
->list
== &bo_va
->invalids
)
2278 list_del(&mapping
->list
);
2279 amdgpu_vm_it_remove(mapping
, &vm
->va
);
2280 trace_amdgpu_vm_bo_unmap(bo_va
, mapping
);
2283 list_add(&mapping
->list
, &vm
->freed
);
2285 amdgpu_vm_free_mapping(adev
, vm
, mapping
,
2286 bo_va
->last_pt_update
);
2292 * amdgpu_vm_bo_clear_mappings - remove all mappings in a specific range
2294 * @adev: amdgpu_device pointer
2295 * @vm: VM structure to use
2296 * @saddr: start of the range
2297 * @size: size of the range
2299 * Remove all mappings in a range, split them as appropriate.
2300 * Returns 0 for success, error for failure.
2302 int amdgpu_vm_bo_clear_mappings(struct amdgpu_device
*adev
,
2303 struct amdgpu_vm
*vm
,
2304 uint64_t saddr
, uint64_t size
)
2306 struct amdgpu_bo_va_mapping
*before
, *after
, *tmp
, *next
;
2310 eaddr
= saddr
+ size
- 1;
2311 saddr
/= AMDGPU_GPU_PAGE_SIZE
;
2312 eaddr
/= AMDGPU_GPU_PAGE_SIZE
;
2314 /* Allocate all the needed memory */
2315 before
= kzalloc(sizeof(*before
), GFP_KERNEL
);
2318 INIT_LIST_HEAD(&before
->list
);
2320 after
= kzalloc(sizeof(*after
), GFP_KERNEL
);
2325 INIT_LIST_HEAD(&after
->list
);
2327 /* Now gather all removed mappings */
2328 tmp
= amdgpu_vm_it_iter_first(&vm
->va
, saddr
, eaddr
);
2330 /* Remember mapping split at the start */
2331 if (tmp
->start
< saddr
) {
2332 before
->start
= tmp
->start
;
2333 before
->last
= saddr
- 1;
2334 before
->offset
= tmp
->offset
;
2335 before
->flags
= tmp
->flags
;
2336 list_add(&before
->list
, &tmp
->list
);
2339 /* Remember mapping split at the end */
2340 if (tmp
->last
> eaddr
) {
2341 after
->start
= eaddr
+ 1;
2342 after
->last
= tmp
->last
;
2343 after
->offset
= tmp
->offset
;
2344 after
->offset
+= after
->start
- tmp
->start
;
2345 after
->flags
= tmp
->flags
;
2346 list_add(&after
->list
, &tmp
->list
);
2349 list_del(&tmp
->list
);
2350 list_add(&tmp
->list
, &removed
);
2352 tmp
= amdgpu_vm_it_iter_next(tmp
, saddr
, eaddr
);
2355 /* And free them up */
2356 list_for_each_entry_safe(tmp
, next
, &removed
, list
) {
2357 amdgpu_vm_it_remove(tmp
, &vm
->va
);
2358 list_del(&tmp
->list
);
2360 if (tmp
->start
< saddr
)
2362 if (tmp
->last
> eaddr
)
2365 list_add(&tmp
->list
, &vm
->freed
);
2366 trace_amdgpu_vm_bo_unmap(NULL
, tmp
);
2369 /* Insert partial mapping before the range */
2370 if (!list_empty(&before
->list
)) {
2371 amdgpu_vm_it_insert(before
, &vm
->va
);
2372 if (before
->flags
& AMDGPU_PTE_PRT
)
2373 amdgpu_vm_prt_get(adev
);
2378 /* Insert partial mapping after the range */
2379 if (!list_empty(&after
->list
)) {
2380 amdgpu_vm_it_insert(after
, &vm
->va
);
2381 if (after
->flags
& AMDGPU_PTE_PRT
)
2382 amdgpu_vm_prt_get(adev
);
2391 * amdgpu_vm_bo_rmv - remove a bo to a specific vm
2393 * @adev: amdgpu_device pointer
2394 * @bo_va: requested bo_va
2396 * Remove @bo_va->bo from the requested vm.
2398 * Object have to be reserved!
2400 void amdgpu_vm_bo_rmv(struct amdgpu_device
*adev
,
2401 struct amdgpu_bo_va
*bo_va
)
2403 struct amdgpu_bo_va_mapping
*mapping
, *next
;
2404 struct amdgpu_vm
*vm
= bo_va
->vm
;
2406 list_del(&bo_va
->bo_list
);
2408 spin_lock(&vm
->status_lock
);
2409 list_del(&bo_va
->vm_status
);
2410 spin_unlock(&vm
->status_lock
);
2412 list_for_each_entry_safe(mapping
, next
, &bo_va
->valids
, list
) {
2413 list_del(&mapping
->list
);
2414 amdgpu_vm_it_remove(mapping
, &vm
->va
);
2415 trace_amdgpu_vm_bo_unmap(bo_va
, mapping
);
2416 list_add(&mapping
->list
, &vm
->freed
);
2418 list_for_each_entry_safe(mapping
, next
, &bo_va
->invalids
, list
) {
2419 list_del(&mapping
->list
);
2420 amdgpu_vm_it_remove(mapping
, &vm
->va
);
2421 amdgpu_vm_free_mapping(adev
, vm
, mapping
,
2422 bo_va
->last_pt_update
);
2425 dma_fence_put(bo_va
->last_pt_update
);
2430 * amdgpu_vm_bo_invalidate - mark the bo as invalid
2432 * @adev: amdgpu_device pointer
2434 * @bo: amdgpu buffer object
2436 * Mark @bo as invalid.
2438 void amdgpu_vm_bo_invalidate(struct amdgpu_device
*adev
,
2439 struct amdgpu_bo
*bo
)
2441 struct amdgpu_bo_va
*bo_va
;
2443 list_for_each_entry(bo_va
, &bo
->va
, bo_list
) {
2444 spin_lock(&bo_va
->vm
->status_lock
);
2445 if (list_empty(&bo_va
->vm_status
))
2446 list_add(&bo_va
->vm_status
, &bo_va
->vm
->invalidated
);
2447 spin_unlock(&bo_va
->vm
->status_lock
);
2451 static uint32_t amdgpu_vm_get_block_size(uint64_t vm_size
)
2453 /* Total bits covered by PD + PTs */
2454 unsigned bits
= ilog2(vm_size
) + 18;
2456 /* Make sure the PD is 4K in size up to 8GB address space.
2457 Above that split equal between PD and PTs */
2461 return ((bits
+ 3) / 2);
2465 * amdgpu_vm_adjust_size - adjust vm size and block size
2467 * @adev: amdgpu_device pointer
2468 * @vm_size: the default vm size if it's set auto
2470 void amdgpu_vm_adjust_size(struct amdgpu_device
*adev
, uint64_t vm_size
)
2472 /* adjust vm size firstly */
2473 if (amdgpu_vm_size
== -1)
2474 adev
->vm_manager
.vm_size
= vm_size
;
2476 adev
->vm_manager
.vm_size
= amdgpu_vm_size
;
2478 /* block size depends on vm size */
2479 if (amdgpu_vm_block_size
== -1)
2480 adev
->vm_manager
.block_size
=
2481 amdgpu_vm_get_block_size(adev
->vm_manager
.vm_size
);
2483 adev
->vm_manager
.block_size
= amdgpu_vm_block_size
;
2485 DRM_INFO("vm size is %llu GB, block size is %u-bit\n",
2486 adev
->vm_manager
.vm_size
, adev
->vm_manager
.block_size
);
2490 * amdgpu_vm_init - initialize a vm instance
2492 * @adev: amdgpu_device pointer
2494 * @vm_context: Indicates if it GFX or Compute context
2498 int amdgpu_vm_init(struct amdgpu_device
*adev
, struct amdgpu_vm
*vm
,
2501 const unsigned align
= min(AMDGPU_VM_PTB_ALIGN_SIZE
,
2502 AMDGPU_VM_PTE_COUNT(adev
) * 8);
2503 unsigned ring_instance
;
2504 struct amdgpu_ring
*ring
;
2505 struct amd_sched_rq
*rq
;
2508 uint64_t init_pde_value
= 0;
2511 vm
->client_id
= atomic64_inc_return(&adev
->vm_manager
.client_counter
);
2512 for (i
= 0; i
< AMDGPU_MAX_VMHUBS
; i
++)
2513 vm
->reserved_vmid
[i
] = NULL
;
2514 spin_lock_init(&vm
->status_lock
);
2515 INIT_LIST_HEAD(&vm
->invalidated
);
2516 INIT_LIST_HEAD(&vm
->cleared
);
2517 INIT_LIST_HEAD(&vm
->freed
);
2519 /* create scheduler entity for page table updates */
2521 ring_instance
= atomic_inc_return(&adev
->vm_manager
.vm_pte_next_ring
);
2522 ring_instance
%= adev
->vm_manager
.vm_pte_num_rings
;
2523 ring
= adev
->vm_manager
.vm_pte_rings
[ring_instance
];
2524 rq
= &ring
->sched
.sched_rq
[AMD_SCHED_PRIORITY_KERNEL
];
2525 r
= amd_sched_entity_init(&ring
->sched
, &vm
->entity
,
2526 rq
, amdgpu_sched_jobs
);
2530 vm
->pte_support_ats
= false;
2532 if (vm_context
== AMDGPU_VM_CONTEXT_COMPUTE
) {
2533 vm
->use_cpu_for_update
= !!(adev
->vm_manager
.vm_update_mode
&
2534 AMDGPU_VM_USE_CPU_FOR_COMPUTE
);
2536 if (adev
->asic_type
== CHIP_RAVEN
) {
2537 vm
->pte_support_ats
= true;
2538 init_pde_value
= AMDGPU_PTE_SYSTEM
| AMDGPU_PDE_PTE
;
2541 vm
->use_cpu_for_update
= !!(adev
->vm_manager
.vm_update_mode
&
2542 AMDGPU_VM_USE_CPU_FOR_GFX
);
2543 DRM_DEBUG_DRIVER("VM update mode is %s\n",
2544 vm
->use_cpu_for_update
? "CPU" : "SDMA");
2545 WARN_ONCE((vm
->use_cpu_for_update
& !amdgpu_vm_is_large_bar(adev
)),
2546 "CPU update of VM recommended only for large BAR system\n");
2547 vm
->last_dir_update
= NULL
;
2549 flags
= AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS
|
2550 AMDGPU_GEM_CREATE_VRAM_CLEARED
;
2551 if (vm
->use_cpu_for_update
)
2552 flags
|= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED
;
2554 flags
|= (AMDGPU_GEM_CREATE_NO_CPU_ACCESS
|
2555 AMDGPU_GEM_CREATE_SHADOW
);
2557 r
= amdgpu_bo_create(adev
, amdgpu_vm_bo_size(adev
, 0), align
, true,
2558 AMDGPU_GEM_DOMAIN_VRAM
,
2560 NULL
, NULL
, init_pde_value
, &vm
->root
.bo
);
2562 goto error_free_sched_entity
;
2564 r
= amdgpu_bo_reserve(vm
->root
.bo
, false);
2566 goto error_free_root
;
2568 vm
->last_eviction_counter
= atomic64_read(&adev
->num_evictions
);
2570 if (vm
->use_cpu_for_update
) {
2571 r
= amdgpu_bo_kmap(vm
->root
.bo
, NULL
);
2573 goto error_free_root
;
2576 amdgpu_bo_unreserve(vm
->root
.bo
);
2581 amdgpu_bo_unref(&vm
->root
.bo
->shadow
);
2582 amdgpu_bo_unref(&vm
->root
.bo
);
2585 error_free_sched_entity
:
2586 amd_sched_entity_fini(&ring
->sched
, &vm
->entity
);
2592 * amdgpu_vm_free_levels - free PD/PT levels
2594 * @level: PD/PT starting level to free
2596 * Free the page directory or page table level and all sub levels.
2598 static void amdgpu_vm_free_levels(struct amdgpu_vm_pt
*level
)
2603 amdgpu_bo_unref(&level
->bo
->shadow
);
2604 amdgpu_bo_unref(&level
->bo
);
2608 for (i
= 0; i
<= level
->last_entry_used
; i
++)
2609 amdgpu_vm_free_levels(&level
->entries
[i
]);
2611 kvfree(level
->entries
);
2615 * amdgpu_vm_fini - tear down a vm instance
2617 * @adev: amdgpu_device pointer
2621 * Unbind the VM and remove all bos from the vm bo list
2623 void amdgpu_vm_fini(struct amdgpu_device
*adev
, struct amdgpu_vm
*vm
)
2625 struct amdgpu_bo_va_mapping
*mapping
, *tmp
;
2626 bool prt_fini_needed
= !!adev
->gart
.gart_funcs
->set_prt
;
2629 amd_sched_entity_fini(vm
->entity
.sched
, &vm
->entity
);
2631 if (!RB_EMPTY_ROOT(&vm
->va
)) {
2632 dev_err(adev
->dev
, "still active bo inside vm\n");
2634 rbtree_postorder_for_each_entry_safe(mapping
, tmp
, &vm
->va
, rb
) {
2635 list_del(&mapping
->list
);
2636 amdgpu_vm_it_remove(mapping
, &vm
->va
);
2639 list_for_each_entry_safe(mapping
, tmp
, &vm
->freed
, list
) {
2640 if (mapping
->flags
& AMDGPU_PTE_PRT
&& prt_fini_needed
) {
2641 amdgpu_vm_prt_fini(adev
, vm
);
2642 prt_fini_needed
= false;
2645 list_del(&mapping
->list
);
2646 amdgpu_vm_free_mapping(adev
, vm
, mapping
, NULL
);
2649 amdgpu_vm_free_levels(&vm
->root
);
2650 dma_fence_put(vm
->last_dir_update
);
2651 for (i
= 0; i
< AMDGPU_MAX_VMHUBS
; i
++)
2652 amdgpu_vm_free_reserved_vmid(adev
, vm
, i
);
2656 * amdgpu_vm_manager_init - init the VM manager
2658 * @adev: amdgpu_device pointer
2660 * Initialize the VM manager structures
2662 void amdgpu_vm_manager_init(struct amdgpu_device
*adev
)
2666 for (i
= 0; i
< AMDGPU_MAX_VMHUBS
; ++i
) {
2667 struct amdgpu_vm_id_manager
*id_mgr
=
2668 &adev
->vm_manager
.id_mgr
[i
];
2670 mutex_init(&id_mgr
->lock
);
2671 INIT_LIST_HEAD(&id_mgr
->ids_lru
);
2672 atomic_set(&id_mgr
->reserved_vmid_num
, 0);
2674 /* skip over VMID 0, since it is the system VM */
2675 for (j
= 1; j
< id_mgr
->num_ids
; ++j
) {
2676 amdgpu_vm_reset_id(adev
, i
, j
);
2677 amdgpu_sync_create(&id_mgr
->ids
[i
].active
);
2678 list_add_tail(&id_mgr
->ids
[j
].list
, &id_mgr
->ids_lru
);
2682 adev
->vm_manager
.fence_context
=
2683 dma_fence_context_alloc(AMDGPU_MAX_RINGS
);
2684 for (i
= 0; i
< AMDGPU_MAX_RINGS
; ++i
)
2685 adev
->vm_manager
.seqno
[i
] = 0;
2687 atomic_set(&adev
->vm_manager
.vm_pte_next_ring
, 0);
2688 atomic64_set(&adev
->vm_manager
.client_counter
, 0);
2689 spin_lock_init(&adev
->vm_manager
.prt_lock
);
2690 atomic_set(&adev
->vm_manager
.num_prt_users
, 0);
2692 /* If not overridden by the user, by default, only in large BAR systems
2693 * Compute VM tables will be updated by CPU
2695 #ifdef CONFIG_X86_64
2696 if (amdgpu_vm_update_mode
== -1) {
2697 if (amdgpu_vm_is_large_bar(adev
))
2698 adev
->vm_manager
.vm_update_mode
=
2699 AMDGPU_VM_USE_CPU_FOR_COMPUTE
;
2701 adev
->vm_manager
.vm_update_mode
= 0;
2703 adev
->vm_manager
.vm_update_mode
= amdgpu_vm_update_mode
;
2705 adev
->vm_manager
.vm_update_mode
= 0;
2711 * amdgpu_vm_manager_fini - cleanup VM manager
2713 * @adev: amdgpu_device pointer
2715 * Cleanup the VM manager and free resources.
2717 void amdgpu_vm_manager_fini(struct amdgpu_device
*adev
)
2721 for (i
= 0; i
< AMDGPU_MAX_VMHUBS
; ++i
) {
2722 struct amdgpu_vm_id_manager
*id_mgr
=
2723 &adev
->vm_manager
.id_mgr
[i
];
2725 mutex_destroy(&id_mgr
->lock
);
2726 for (j
= 0; j
< AMDGPU_NUM_VM
; ++j
) {
2727 struct amdgpu_vm_id
*id
= &id_mgr
->ids
[j
];
2729 amdgpu_sync_free(&id
->active
);
2730 dma_fence_put(id
->flushed_updates
);
2731 dma_fence_put(id
->last_flush
);
2736 int amdgpu_vm_ioctl(struct drm_device
*dev
, void *data
, struct drm_file
*filp
)
2738 union drm_amdgpu_vm
*args
= data
;
2739 struct amdgpu_device
*adev
= dev
->dev_private
;
2740 struct amdgpu_fpriv
*fpriv
= filp
->driver_priv
;
2743 switch (args
->in
.op
) {
2744 case AMDGPU_VM_OP_RESERVE_VMID
:
2745 /* current, we only have requirement to reserve vmid from gfxhub */
2746 r
= amdgpu_vm_alloc_reserved_vmid(adev
, &fpriv
->vm
,
2751 case AMDGPU_VM_OP_UNRESERVE_VMID
:
2752 amdgpu_vm_free_reserved_vmid(adev
, &fpriv
->vm
, AMDGPU_GFXHUB
);