Commit | Line | Data |
---|---|---|
d38ceaf9 AD |
1 | /* |
2 | * Copyright 2008 Advanced Micro Devices, Inc. | |
3 | * Copyright 2008 Red Hat Inc. | |
4 | * Copyright 2009 Jerome Glisse. | |
5 | * | |
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: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
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. | |
23 | * | |
24 | * Authors: Dave Airlie | |
25 | * Alex Deucher | |
26 | * Jerome Glisse | |
27 | */ | |
28 | #include <drm/drmP.h> | |
29 | #include <drm/amdgpu_drm.h> | |
30 | #include "amdgpu.h" | |
31 | #include "amdgpu_trace.h" | |
32 | ||
33 | /* | |
34 | * GPUVM | |
35 | * GPUVM is similar to the legacy gart on older asics, however | |
36 | * rather than there being a single global gart table | |
37 | * for the entire GPU, there are multiple VM page tables active | |
38 | * at any given time. The VM page tables can contain a mix | |
39 | * vram pages and system memory pages and system memory pages | |
40 | * can be mapped as snooped (cached system pages) or unsnooped | |
41 | * (uncached system pages). | |
42 | * Each VM has an ID associated with it and there is a page table | |
43 | * associated with each VMID. When execting a command buffer, | |
44 | * the kernel tells the the ring what VMID to use for that command | |
45 | * buffer. VMIDs are allocated dynamically as commands are submitted. | |
46 | * The userspace drivers maintain their own address space and the kernel | |
47 | * sets up their pages tables accordingly when they submit their | |
48 | * command buffers and a VMID is assigned. | |
49 | * Cayman/Trinity support up to 8 active VMs at any given time; | |
50 | * SI supports 16. | |
51 | */ | |
52 | ||
4ff37a83 CK |
53 | /* Special value that no flush is necessary */ |
54 | #define AMDGPU_VM_NO_FLUSH (~0ll) | |
55 | ||
d38ceaf9 AD |
56 | /** |
57 | * amdgpu_vm_num_pde - return the number of page directory entries | |
58 | * | |
59 | * @adev: amdgpu_device pointer | |
60 | * | |
8843dbbb | 61 | * Calculate the number of page directory entries. |
d38ceaf9 AD |
62 | */ |
63 | static unsigned amdgpu_vm_num_pdes(struct amdgpu_device *adev) | |
64 | { | |
65 | return adev->vm_manager.max_pfn >> amdgpu_vm_block_size; | |
66 | } | |
67 | ||
68 | /** | |
69 | * amdgpu_vm_directory_size - returns the size of the page directory in bytes | |
70 | * | |
71 | * @adev: amdgpu_device pointer | |
72 | * | |
8843dbbb | 73 | * Calculate the size of the page directory in bytes. |
d38ceaf9 AD |
74 | */ |
75 | static unsigned amdgpu_vm_directory_size(struct amdgpu_device *adev) | |
76 | { | |
77 | return AMDGPU_GPU_PAGE_ALIGN(amdgpu_vm_num_pdes(adev) * 8); | |
78 | } | |
79 | ||
80 | /** | |
56467ebf | 81 | * amdgpu_vm_get_pd_bo - add the VM PD to a validation list |
d38ceaf9 AD |
82 | * |
83 | * @vm: vm providing the BOs | |
3c0eea6c | 84 | * @validated: head of validation list |
56467ebf | 85 | * @entry: entry to add |
d38ceaf9 AD |
86 | * |
87 | * Add the page directory to the list of BOs to | |
56467ebf | 88 | * validate for command submission. |
d38ceaf9 | 89 | */ |
56467ebf CK |
90 | void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm, |
91 | struct list_head *validated, | |
92 | struct amdgpu_bo_list_entry *entry) | |
d38ceaf9 | 93 | { |
56467ebf | 94 | entry->robj = vm->page_directory; |
56467ebf CK |
95 | entry->priority = 0; |
96 | entry->tv.bo = &vm->page_directory->tbo; | |
97 | entry->tv.shared = true; | |
2f568dbd | 98 | entry->user_pages = NULL; |
56467ebf CK |
99 | list_add(&entry->tv.head, validated); |
100 | } | |
d38ceaf9 | 101 | |
56467ebf | 102 | /** |
ee1782c3 | 103 | * amdgpu_vm_get_bos - add the vm BOs to a duplicates list |
56467ebf CK |
104 | * |
105 | * @vm: vm providing the BOs | |
3c0eea6c | 106 | * @duplicates: head of duplicates list |
d38ceaf9 | 107 | * |
ee1782c3 CK |
108 | * Add the page directory to the BO duplicates list |
109 | * for command submission. | |
d38ceaf9 | 110 | */ |
ee1782c3 | 111 | void amdgpu_vm_get_pt_bos(struct amdgpu_vm *vm, struct list_head *duplicates) |
d38ceaf9 | 112 | { |
ee1782c3 | 113 | unsigned i; |
d38ceaf9 AD |
114 | |
115 | /* add the vm page table to the list */ | |
ee1782c3 CK |
116 | for (i = 0; i <= vm->max_pde_used; ++i) { |
117 | struct amdgpu_bo_list_entry *entry = &vm->page_tables[i].entry; | |
118 | ||
119 | if (!entry->robj) | |
d38ceaf9 AD |
120 | continue; |
121 | ||
ee1782c3 | 122 | list_add(&entry->tv.head, duplicates); |
d38ceaf9 | 123 | } |
eceb8a15 CK |
124 | |
125 | } | |
126 | ||
127 | /** | |
128 | * amdgpu_vm_move_pt_bos_in_lru - move the PT BOs to the LRU tail | |
129 | * | |
130 | * @adev: amdgpu device instance | |
131 | * @vm: vm providing the BOs | |
132 | * | |
133 | * Move the PT BOs to the tail of the LRU. | |
134 | */ | |
135 | void amdgpu_vm_move_pt_bos_in_lru(struct amdgpu_device *adev, | |
136 | struct amdgpu_vm *vm) | |
137 | { | |
138 | struct ttm_bo_global *glob = adev->mman.bdev.glob; | |
139 | unsigned i; | |
140 | ||
141 | spin_lock(&glob->lru_lock); | |
142 | for (i = 0; i <= vm->max_pde_used; ++i) { | |
143 | struct amdgpu_bo_list_entry *entry = &vm->page_tables[i].entry; | |
144 | ||
145 | if (!entry->robj) | |
146 | continue; | |
147 | ||
148 | ttm_bo_move_to_lru_tail(&entry->robj->tbo); | |
149 | } | |
150 | spin_unlock(&glob->lru_lock); | |
d38ceaf9 AD |
151 | } |
152 | ||
153 | /** | |
154 | * amdgpu_vm_grab_id - allocate the next free VMID | |
155 | * | |
d38ceaf9 | 156 | * @vm: vm to allocate id for |
7f8a5290 CK |
157 | * @ring: ring we want to submit job to |
158 | * @sync: sync object where we add dependencies | |
94dd0a4a | 159 | * @fence: fence protecting ID from reuse |
d38ceaf9 | 160 | * |
7f8a5290 | 161 | * Allocate an id for the vm, adding fences to the sync obj as necessary. |
d38ceaf9 | 162 | */ |
7f8a5290 | 163 | int amdgpu_vm_grab_id(struct amdgpu_vm *vm, struct amdgpu_ring *ring, |
4ff37a83 CK |
164 | struct amdgpu_sync *sync, struct fence *fence, |
165 | unsigned *vm_id, uint64_t *vm_pd_addr) | |
d38ceaf9 | 166 | { |
4ff37a83 | 167 | uint64_t pd_addr = amdgpu_bo_gpu_offset(vm->page_directory); |
d38ceaf9 | 168 | struct amdgpu_device *adev = ring->adev; |
4ff37a83 CK |
169 | struct amdgpu_vm_id *id = &vm->ids[ring->idx]; |
170 | struct fence *updates = sync->last_vm_update; | |
a9a78b32 | 171 | int r; |
d38ceaf9 | 172 | |
94dd0a4a CK |
173 | mutex_lock(&adev->vm_manager.lock); |
174 | ||
d38ceaf9 | 175 | /* check if the id is still valid */ |
4ff37a83 CK |
176 | if (id->mgr_id) { |
177 | struct fence *flushed = id->flushed_updates; | |
178 | bool is_later; | |
1c16c0a7 CK |
179 | long owner; |
180 | ||
4ff37a83 CK |
181 | if (!flushed) |
182 | is_later = true; | |
183 | else if (!updates) | |
184 | is_later = false; | |
185 | else | |
186 | is_later = fence_is_later(updates, flushed); | |
187 | ||
188 | owner = atomic_long_read(&id->mgr_id->owner); | |
189 | if (!is_later && owner == (long)id && | |
190 | pd_addr == id->pd_gpu_addr) { | |
191 | ||
a8bd1bec CK |
192 | r = amdgpu_sync_fence(ring->adev, sync, |
193 | id->mgr_id->active); | |
194 | if (r) { | |
195 | mutex_unlock(&adev->vm_manager.lock); | |
196 | return r; | |
197 | } | |
198 | ||
4ff37a83 CK |
199 | fence_put(id->mgr_id->active); |
200 | id->mgr_id->active = fence_get(fence); | |
201 | ||
202 | list_move_tail(&id->mgr_id->list, | |
203 | &adev->vm_manager.ids_lru); | |
d38ceaf9 | 204 | |
4ff37a83 CK |
205 | *vm_id = id->mgr_id - adev->vm_manager.ids; |
206 | *vm_pd_addr = AMDGPU_VM_NO_FLUSH; | |
22073fe7 CK |
207 | trace_amdgpu_vm_grab_id(vm, ring->idx, *vm_id, |
208 | *vm_pd_addr); | |
d38ceaf9 | 209 | |
94dd0a4a | 210 | mutex_unlock(&adev->vm_manager.lock); |
7f8a5290 | 211 | return 0; |
d38ceaf9 | 212 | } |
d38ceaf9 AD |
213 | } |
214 | ||
4ff37a83 CK |
215 | id->mgr_id = list_first_entry(&adev->vm_manager.ids_lru, |
216 | struct amdgpu_vm_manager_id, | |
217 | list); | |
7f8a5290 | 218 | |
8e9fbeb5 CZ |
219 | if (id->mgr_id->active && !fence_is_signaled(id->mgr_id->active)) { |
220 | struct amdgpu_vm_manager_id *mgr_id, *tmp; | |
221 | struct list_head *head = &adev->vm_manager.ids_lru; | |
222 | list_for_each_entry_safe(mgr_id, tmp, &adev->vm_manager.ids_lru, list) { | |
223 | if (mgr_id->active && fence_is_signaled(mgr_id->active)) { | |
224 | list_move(&mgr_id->list, head); | |
225 | head = &mgr_id->list; | |
226 | } | |
227 | } | |
228 | id->mgr_id = list_first_entry(&adev->vm_manager.ids_lru, | |
229 | struct amdgpu_vm_manager_id, | |
230 | list); | |
231 | } | |
232 | ||
4ff37a83 CK |
233 | r = amdgpu_sync_fence(ring->adev, sync, id->mgr_id->active); |
234 | if (!r) { | |
235 | fence_put(id->mgr_id->active); | |
236 | id->mgr_id->active = fence_get(fence); | |
94dd0a4a | 237 | |
4ff37a83 CK |
238 | fence_put(id->flushed_updates); |
239 | id->flushed_updates = fence_get(updates); | |
94dd0a4a | 240 | |
4ff37a83 | 241 | id->pd_gpu_addr = pd_addr; |
94dd0a4a | 242 | |
4ff37a83 CK |
243 | list_move_tail(&id->mgr_id->list, &adev->vm_manager.ids_lru); |
244 | atomic_long_set(&id->mgr_id->owner, (long)id); | |
245 | ||
246 | *vm_id = id->mgr_id - adev->vm_manager.ids; | |
247 | *vm_pd_addr = pd_addr; | |
22073fe7 | 248 | trace_amdgpu_vm_grab_id(vm, ring->idx, *vm_id, *vm_pd_addr); |
d38ceaf9 AD |
249 | } |
250 | ||
94dd0a4a | 251 | mutex_unlock(&adev->vm_manager.lock); |
a9a78b32 | 252 | return r; |
d38ceaf9 AD |
253 | } |
254 | ||
255 | /** | |
256 | * amdgpu_vm_flush - hardware flush the vm | |
257 | * | |
258 | * @ring: ring to use for flush | |
cffadc83 | 259 | * @vm_id: vmid number to use |
4ff37a83 | 260 | * @pd_addr: address of the page directory |
d38ceaf9 | 261 | * |
4ff37a83 | 262 | * Emit a VM flush when it is necessary. |
d38ceaf9 AD |
263 | */ |
264 | void amdgpu_vm_flush(struct amdgpu_ring *ring, | |
cffadc83 CK |
265 | unsigned vm_id, uint64_t pd_addr, |
266 | uint32_t gds_base, uint32_t gds_size, | |
267 | uint32_t gws_base, uint32_t gws_size, | |
268 | uint32_t oa_base, uint32_t oa_size) | |
d38ceaf9 | 269 | { |
971fe9a9 CK |
270 | struct amdgpu_device *adev = ring->adev; |
271 | struct amdgpu_vm_manager_id *mgr_id = &adev->vm_manager.ids[vm_id]; | |
d564a06e CK |
272 | bool gds_switch_needed = ring->funcs->emit_gds_switch && ( |
273 | mgr_id->gds_base != gds_base || | |
274 | mgr_id->gds_size != gds_size || | |
275 | mgr_id->gws_base != gws_base || | |
276 | mgr_id->gws_size != gws_size || | |
277 | mgr_id->oa_base != oa_base || | |
278 | mgr_id->oa_size != oa_size); | |
279 | ||
280 | if (ring->funcs->emit_pipeline_sync && ( | |
281 | pd_addr != AMDGPU_VM_NO_FLUSH || gds_switch_needed)) | |
282 | amdgpu_ring_emit_pipeline_sync(ring); | |
971fe9a9 | 283 | |
4ff37a83 | 284 | if (pd_addr != AMDGPU_VM_NO_FLUSH) { |
cffadc83 CK |
285 | trace_amdgpu_vm_flush(pd_addr, ring->idx, vm_id); |
286 | amdgpu_ring_emit_vm_flush(ring, vm_id, pd_addr); | |
d38ceaf9 | 287 | } |
cffadc83 | 288 | |
d564a06e | 289 | if (gds_switch_needed) { |
971fe9a9 CK |
290 | mgr_id->gds_base = gds_base; |
291 | mgr_id->gds_size = gds_size; | |
292 | mgr_id->gws_base = gws_base; | |
293 | mgr_id->gws_size = gws_size; | |
294 | mgr_id->oa_base = oa_base; | |
295 | mgr_id->oa_size = oa_size; | |
cffadc83 CK |
296 | amdgpu_ring_emit_gds_switch(ring, vm_id, |
297 | gds_base, gds_size, | |
298 | gws_base, gws_size, | |
299 | oa_base, oa_size); | |
971fe9a9 CK |
300 | } |
301 | } | |
302 | ||
303 | /** | |
304 | * amdgpu_vm_reset_id - reset VMID to zero | |
305 | * | |
306 | * @adev: amdgpu device structure | |
307 | * @vm_id: vmid number to use | |
308 | * | |
309 | * Reset saved GDW, GWS and OA to force switch on next flush. | |
310 | */ | |
311 | void amdgpu_vm_reset_id(struct amdgpu_device *adev, unsigned vm_id) | |
312 | { | |
313 | struct amdgpu_vm_manager_id *mgr_id = &adev->vm_manager.ids[vm_id]; | |
314 | ||
315 | mgr_id->gds_base = 0; | |
316 | mgr_id->gds_size = 0; | |
317 | mgr_id->gws_base = 0; | |
318 | mgr_id->gws_size = 0; | |
319 | mgr_id->oa_base = 0; | |
320 | mgr_id->oa_size = 0; | |
d38ceaf9 AD |
321 | } |
322 | ||
d38ceaf9 AD |
323 | /** |
324 | * amdgpu_vm_bo_find - find the bo_va for a specific vm & bo | |
325 | * | |
326 | * @vm: requested vm | |
327 | * @bo: requested buffer object | |
328 | * | |
8843dbbb | 329 | * Find @bo inside the requested vm. |
d38ceaf9 AD |
330 | * Search inside the @bos vm list for the requested vm |
331 | * Returns the found bo_va or NULL if none is found | |
332 | * | |
333 | * Object has to be reserved! | |
334 | */ | |
335 | struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm, | |
336 | struct amdgpu_bo *bo) | |
337 | { | |
338 | struct amdgpu_bo_va *bo_va; | |
339 | ||
340 | list_for_each_entry(bo_va, &bo->va, bo_list) { | |
341 | if (bo_va->vm == vm) { | |
342 | return bo_va; | |
343 | } | |
344 | } | |
345 | return NULL; | |
346 | } | |
347 | ||
348 | /** | |
349 | * amdgpu_vm_update_pages - helper to call the right asic function | |
350 | * | |
351 | * @adev: amdgpu_device pointer | |
fa3ab3c7 CK |
352 | * @src: address where to copy page table entries from |
353 | * @pages_addr: DMA addresses to use for mapping | |
d38ceaf9 AD |
354 | * @ib: indirect buffer to fill with commands |
355 | * @pe: addr of the page entry | |
356 | * @addr: dst addr to write into pe | |
357 | * @count: number of page entries to update | |
358 | * @incr: increase next addr by incr bytes | |
359 | * @flags: hw access flags | |
d38ceaf9 AD |
360 | * |
361 | * Traces the parameters and calls the right asic functions | |
362 | * to setup the page table using the DMA. | |
363 | */ | |
364 | static void amdgpu_vm_update_pages(struct amdgpu_device *adev, | |
fa3ab3c7 CK |
365 | uint64_t src, |
366 | dma_addr_t *pages_addr, | |
d38ceaf9 AD |
367 | struct amdgpu_ib *ib, |
368 | uint64_t pe, uint64_t addr, | |
369 | unsigned count, uint32_t incr, | |
9ab21462 | 370 | uint32_t flags) |
d38ceaf9 AD |
371 | { |
372 | trace_amdgpu_vm_set_page(pe, addr, count, incr, flags); | |
373 | ||
fa3ab3c7 CK |
374 | if (src) { |
375 | src += (addr >> 12) * 8; | |
d38ceaf9 AD |
376 | amdgpu_vm_copy_pte(adev, ib, pe, src, count); |
377 | ||
fa3ab3c7 | 378 | } else if (pages_addr) { |
b07c9d2a CK |
379 | amdgpu_vm_write_pte(adev, ib, pages_addr, pe, addr, |
380 | count, incr, flags); | |
381 | ||
382 | } else if (count < 3) { | |
383 | amdgpu_vm_write_pte(adev, ib, NULL, pe, addr, | |
384 | count, incr, flags); | |
d38ceaf9 AD |
385 | |
386 | } else { | |
387 | amdgpu_vm_set_pte_pde(adev, ib, pe, addr, | |
388 | count, incr, flags); | |
389 | } | |
390 | } | |
391 | ||
392 | /** | |
393 | * amdgpu_vm_clear_bo - initially clear the page dir/table | |
394 | * | |
395 | * @adev: amdgpu_device pointer | |
396 | * @bo: bo to clear | |
ef9f0a83 CZ |
397 | * |
398 | * need to reserve bo first before calling it. | |
d38ceaf9 AD |
399 | */ |
400 | static int amdgpu_vm_clear_bo(struct amdgpu_device *adev, | |
2bd9ccfa | 401 | struct amdgpu_vm *vm, |
d38ceaf9 AD |
402 | struct amdgpu_bo *bo) |
403 | { | |
2d55e45a | 404 | struct amdgpu_ring *ring; |
4af9f07c | 405 | struct fence *fence = NULL; |
d71518b5 | 406 | struct amdgpu_job *job; |
d38ceaf9 AD |
407 | unsigned entries; |
408 | uint64_t addr; | |
409 | int r; | |
410 | ||
2d55e45a CK |
411 | ring = container_of(vm->entity.sched, struct amdgpu_ring, sched); |
412 | ||
ca952613 | 413 | r = reservation_object_reserve_shared(bo->tbo.resv); |
414 | if (r) | |
415 | return r; | |
416 | ||
d38ceaf9 AD |
417 | r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false); |
418 | if (r) | |
ef9f0a83 | 419 | goto error; |
d38ceaf9 AD |
420 | |
421 | addr = amdgpu_bo_gpu_offset(bo); | |
422 | entries = amdgpu_bo_size(bo) / 8; | |
423 | ||
d71518b5 CK |
424 | r = amdgpu_job_alloc_with_ib(adev, 64, &job); |
425 | if (r) | |
ef9f0a83 | 426 | goto error; |
d38ceaf9 | 427 | |
fa3ab3c7 | 428 | amdgpu_vm_update_pages(adev, 0, NULL, &job->ibs[0], addr, 0, entries, |
d71518b5 CK |
429 | 0, 0); |
430 | amdgpu_ring_pad_ib(ring, &job->ibs[0]); | |
431 | ||
432 | WARN_ON(job->ibs[0].length_dw > 64); | |
2bd9ccfa CK |
433 | r = amdgpu_job_submit(job, ring, &vm->entity, |
434 | AMDGPU_FENCE_OWNER_VM, &fence); | |
d38ceaf9 AD |
435 | if (r) |
436 | goto error_free; | |
437 | ||
d71518b5 | 438 | amdgpu_bo_fence(bo, fence, true); |
281b4223 | 439 | fence_put(fence); |
cadf97b1 | 440 | return 0; |
ef9f0a83 | 441 | |
d38ceaf9 | 442 | error_free: |
d71518b5 | 443 | amdgpu_job_free(job); |
d38ceaf9 | 444 | |
ef9f0a83 | 445 | error: |
d38ceaf9 AD |
446 | return r; |
447 | } | |
448 | ||
449 | /** | |
b07c9d2a | 450 | * amdgpu_vm_map_gart - Resolve gart mapping of addr |
d38ceaf9 | 451 | * |
b07c9d2a | 452 | * @pages_addr: optional DMA address to use for lookup |
d38ceaf9 AD |
453 | * @addr: the unmapped addr |
454 | * | |
455 | * Look up the physical address of the page that the pte resolves | |
b07c9d2a | 456 | * to and return the pointer for the page table entry. |
d38ceaf9 | 457 | */ |
b07c9d2a | 458 | uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t addr) |
d38ceaf9 AD |
459 | { |
460 | uint64_t result; | |
461 | ||
b07c9d2a CK |
462 | if (pages_addr) { |
463 | /* page table offset */ | |
464 | result = pages_addr[addr >> PAGE_SHIFT]; | |
465 | ||
466 | /* in case cpu page size != gpu page size*/ | |
467 | result |= addr & (~PAGE_MASK); | |
468 | ||
469 | } else { | |
470 | /* No mapping required */ | |
471 | result = addr; | |
472 | } | |
d38ceaf9 | 473 | |
b07c9d2a | 474 | result &= 0xFFFFFFFFFFFFF000ULL; |
d38ceaf9 AD |
475 | |
476 | return result; | |
477 | } | |
478 | ||
479 | /** | |
480 | * amdgpu_vm_update_pdes - make sure that page directory is valid | |
481 | * | |
482 | * @adev: amdgpu_device pointer | |
483 | * @vm: requested vm | |
484 | * @start: start of GPU address range | |
485 | * @end: end of GPU address range | |
486 | * | |
487 | * Allocates new page tables if necessary | |
8843dbbb | 488 | * and updates the page directory. |
d38ceaf9 | 489 | * Returns 0 for success, error for failure. |
d38ceaf9 AD |
490 | */ |
491 | int amdgpu_vm_update_page_directory(struct amdgpu_device *adev, | |
492 | struct amdgpu_vm *vm) | |
493 | { | |
2d55e45a | 494 | struct amdgpu_ring *ring; |
d38ceaf9 AD |
495 | struct amdgpu_bo *pd = vm->page_directory; |
496 | uint64_t pd_addr = amdgpu_bo_gpu_offset(pd); | |
497 | uint32_t incr = AMDGPU_VM_PTE_COUNT * 8; | |
498 | uint64_t last_pde = ~0, last_pt = ~0; | |
499 | unsigned count = 0, pt_idx, ndw; | |
d71518b5 | 500 | struct amdgpu_job *job; |
d5fc5e82 | 501 | struct amdgpu_ib *ib; |
4af9f07c | 502 | struct fence *fence = NULL; |
d5fc5e82 | 503 | |
d38ceaf9 AD |
504 | int r; |
505 | ||
2d55e45a CK |
506 | ring = container_of(vm->entity.sched, struct amdgpu_ring, sched); |
507 | ||
d38ceaf9 AD |
508 | /* padding, etc. */ |
509 | ndw = 64; | |
510 | ||
511 | /* assume the worst case */ | |
512 | ndw += vm->max_pde_used * 6; | |
513 | ||
d71518b5 CK |
514 | r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job); |
515 | if (r) | |
d38ceaf9 | 516 | return r; |
d71518b5 CK |
517 | |
518 | ib = &job->ibs[0]; | |
d38ceaf9 AD |
519 | |
520 | /* walk over the address space and update the page directory */ | |
521 | for (pt_idx = 0; pt_idx <= vm->max_pde_used; ++pt_idx) { | |
ee1782c3 | 522 | struct amdgpu_bo *bo = vm->page_tables[pt_idx].entry.robj; |
d38ceaf9 AD |
523 | uint64_t pde, pt; |
524 | ||
525 | if (bo == NULL) | |
526 | continue; | |
527 | ||
528 | pt = amdgpu_bo_gpu_offset(bo); | |
529 | if (vm->page_tables[pt_idx].addr == pt) | |
530 | continue; | |
531 | vm->page_tables[pt_idx].addr = pt; | |
532 | ||
533 | pde = pd_addr + pt_idx * 8; | |
534 | if (((last_pde + 8 * count) != pde) || | |
535 | ((last_pt + incr * count) != pt)) { | |
536 | ||
537 | if (count) { | |
fa3ab3c7 | 538 | amdgpu_vm_update_pages(adev, 0, NULL, ib, |
9ab21462 CK |
539 | last_pde, last_pt, |
540 | count, incr, | |
541 | AMDGPU_PTE_VALID); | |
d38ceaf9 AD |
542 | } |
543 | ||
544 | count = 1; | |
545 | last_pde = pde; | |
546 | last_pt = pt; | |
547 | } else { | |
548 | ++count; | |
549 | } | |
550 | } | |
551 | ||
552 | if (count) | |
fa3ab3c7 | 553 | amdgpu_vm_update_pages(adev, 0, NULL, ib, last_pde, last_pt, |
9ab21462 | 554 | count, incr, AMDGPU_PTE_VALID); |
d38ceaf9 | 555 | |
d5fc5e82 | 556 | if (ib->length_dw != 0) { |
9e5d5309 | 557 | amdgpu_ring_pad_ib(ring, ib); |
e86f9cee CK |
558 | amdgpu_sync_resv(adev, &job->sync, pd->tbo.resv, |
559 | AMDGPU_FENCE_OWNER_VM); | |
d5fc5e82 | 560 | WARN_ON(ib->length_dw > ndw); |
2bd9ccfa CK |
561 | r = amdgpu_job_submit(job, ring, &vm->entity, |
562 | AMDGPU_FENCE_OWNER_VM, &fence); | |
4af9f07c CZ |
563 | if (r) |
564 | goto error_free; | |
05906dec | 565 | |
4af9f07c | 566 | amdgpu_bo_fence(pd, fence, true); |
05906dec BN |
567 | fence_put(vm->page_directory_fence); |
568 | vm->page_directory_fence = fence_get(fence); | |
281b4223 | 569 | fence_put(fence); |
d5fc5e82 | 570 | |
d71518b5 CK |
571 | } else { |
572 | amdgpu_job_free(job); | |
d5fc5e82 | 573 | } |
d38ceaf9 AD |
574 | |
575 | return 0; | |
d5fc5e82 CZ |
576 | |
577 | error_free: | |
d71518b5 | 578 | amdgpu_job_free(job); |
4af9f07c | 579 | return r; |
d38ceaf9 AD |
580 | } |
581 | ||
582 | /** | |
583 | * amdgpu_vm_frag_ptes - add fragment information to PTEs | |
584 | * | |
585 | * @adev: amdgpu_device pointer | |
fa3ab3c7 CK |
586 | * @src: address where to copy page table entries from |
587 | * @pages_addr: DMA addresses to use for mapping | |
d38ceaf9 AD |
588 | * @ib: IB for the update |
589 | * @pe_start: first PTE to handle | |
590 | * @pe_end: last PTE to handle | |
591 | * @addr: addr those PTEs should point to | |
592 | * @flags: hw mapping flags | |
d38ceaf9 AD |
593 | */ |
594 | static void amdgpu_vm_frag_ptes(struct amdgpu_device *adev, | |
fa3ab3c7 CK |
595 | uint64_t src, |
596 | dma_addr_t *pages_addr, | |
d38ceaf9 AD |
597 | struct amdgpu_ib *ib, |
598 | uint64_t pe_start, uint64_t pe_end, | |
9ab21462 | 599 | uint64_t addr, uint32_t flags) |
d38ceaf9 AD |
600 | { |
601 | /** | |
602 | * The MC L1 TLB supports variable sized pages, based on a fragment | |
603 | * field in the PTE. When this field is set to a non-zero value, page | |
604 | * granularity is increased from 4KB to (1 << (12 + frag)). The PTE | |
605 | * flags are considered valid for all PTEs within the fragment range | |
606 | * and corresponding mappings are assumed to be physically contiguous. | |
607 | * | |
608 | * The L1 TLB can store a single PTE for the whole fragment, | |
609 | * significantly increasing the space available for translation | |
610 | * caching. This leads to large improvements in throughput when the | |
611 | * TLB is under pressure. | |
612 | * | |
613 | * The L2 TLB distributes small and large fragments into two | |
614 | * asymmetric partitions. The large fragment cache is significantly | |
615 | * larger. Thus, we try to use large fragments wherever possible. | |
616 | * Userspace can support this by aligning virtual base address and | |
617 | * allocation size to the fragment size. | |
618 | */ | |
619 | ||
620 | /* SI and newer are optimized for 64KB */ | |
621 | uint64_t frag_flags = AMDGPU_PTE_FRAG_64KB; | |
622 | uint64_t frag_align = 0x80; | |
623 | ||
624 | uint64_t frag_start = ALIGN(pe_start, frag_align); | |
625 | uint64_t frag_end = pe_end & ~(frag_align - 1); | |
626 | ||
627 | unsigned count; | |
628 | ||
31f6c1fe CK |
629 | /* Abort early if there isn't anything to do */ |
630 | if (pe_start == pe_end) | |
631 | return; | |
632 | ||
d38ceaf9 | 633 | /* system pages are non continuously */ |
fa3ab3c7 CK |
634 | if (src || pages_addr || !(flags & AMDGPU_PTE_VALID) || |
635 | (frag_start >= frag_end)) { | |
d38ceaf9 AD |
636 | |
637 | count = (pe_end - pe_start) / 8; | |
fa3ab3c7 | 638 | amdgpu_vm_update_pages(adev, src, pages_addr, ib, pe_start, |
9ab21462 CK |
639 | addr, count, AMDGPU_GPU_PAGE_SIZE, |
640 | flags); | |
d38ceaf9 AD |
641 | return; |
642 | } | |
643 | ||
644 | /* handle the 4K area at the beginning */ | |
645 | if (pe_start != frag_start) { | |
646 | count = (frag_start - pe_start) / 8; | |
fa3ab3c7 | 647 | amdgpu_vm_update_pages(adev, 0, NULL, ib, pe_start, addr, |
9ab21462 | 648 | count, AMDGPU_GPU_PAGE_SIZE, flags); |
d38ceaf9 AD |
649 | addr += AMDGPU_GPU_PAGE_SIZE * count; |
650 | } | |
651 | ||
652 | /* handle the area in the middle */ | |
653 | count = (frag_end - frag_start) / 8; | |
fa3ab3c7 | 654 | amdgpu_vm_update_pages(adev, 0, NULL, ib, frag_start, addr, count, |
9ab21462 | 655 | AMDGPU_GPU_PAGE_SIZE, flags | frag_flags); |
d38ceaf9 AD |
656 | |
657 | /* handle the 4K area at the end */ | |
658 | if (frag_end != pe_end) { | |
659 | addr += AMDGPU_GPU_PAGE_SIZE * count; | |
660 | count = (pe_end - frag_end) / 8; | |
fa3ab3c7 | 661 | amdgpu_vm_update_pages(adev, 0, NULL, ib, frag_end, addr, |
9ab21462 | 662 | count, AMDGPU_GPU_PAGE_SIZE, flags); |
d38ceaf9 AD |
663 | } |
664 | } | |
665 | ||
666 | /** | |
667 | * amdgpu_vm_update_ptes - make sure that page tables are valid | |
668 | * | |
669 | * @adev: amdgpu_device pointer | |
fa3ab3c7 CK |
670 | * @src: address where to copy page table entries from |
671 | * @pages_addr: DMA addresses to use for mapping | |
d38ceaf9 AD |
672 | * @vm: requested vm |
673 | * @start: start of GPU address range | |
674 | * @end: end of GPU address range | |
675 | * @dst: destination address to map to | |
676 | * @flags: mapping flags | |
677 | * | |
8843dbbb | 678 | * Update the page tables in the range @start - @end. |
d38ceaf9 | 679 | */ |
a1e08d3b | 680 | static void amdgpu_vm_update_ptes(struct amdgpu_device *adev, |
fa3ab3c7 CK |
681 | uint64_t src, |
682 | dma_addr_t *pages_addr, | |
a1e08d3b CK |
683 | struct amdgpu_vm *vm, |
684 | struct amdgpu_ib *ib, | |
685 | uint64_t start, uint64_t end, | |
686 | uint64_t dst, uint32_t flags) | |
d38ceaf9 | 687 | { |
31f6c1fe CK |
688 | const uint64_t mask = AMDGPU_VM_PTE_COUNT - 1; |
689 | ||
690 | uint64_t last_pe_start = ~0, last_pe_end = ~0, last_dst = ~0; | |
d38ceaf9 AD |
691 | uint64_t addr; |
692 | ||
693 | /* walk over the address space and update the page tables */ | |
694 | for (addr = start; addr < end; ) { | |
695 | uint64_t pt_idx = addr >> amdgpu_vm_block_size; | |
ee1782c3 | 696 | struct amdgpu_bo *pt = vm->page_tables[pt_idx].entry.robj; |
d38ceaf9 | 697 | unsigned nptes; |
31f6c1fe | 698 | uint64_t pe_start; |
d38ceaf9 AD |
699 | |
700 | if ((addr & ~mask) == (end & ~mask)) | |
701 | nptes = end - addr; | |
702 | else | |
703 | nptes = AMDGPU_VM_PTE_COUNT - (addr & mask); | |
704 | ||
31f6c1fe CK |
705 | pe_start = amdgpu_bo_gpu_offset(pt); |
706 | pe_start += (addr & mask) * 8; | |
d38ceaf9 | 707 | |
31f6c1fe | 708 | if (last_pe_end != pe_start) { |
d38ceaf9 | 709 | |
fa3ab3c7 | 710 | amdgpu_vm_frag_ptes(adev, src, pages_addr, ib, |
31f6c1fe CK |
711 | last_pe_start, last_pe_end, |
712 | last_dst, flags); | |
d38ceaf9 | 713 | |
31f6c1fe CK |
714 | last_pe_start = pe_start; |
715 | last_pe_end = pe_start + 8 * nptes; | |
d38ceaf9 AD |
716 | last_dst = dst; |
717 | } else { | |
31f6c1fe | 718 | last_pe_end += 8 * nptes; |
d38ceaf9 AD |
719 | } |
720 | ||
721 | addr += nptes; | |
722 | dst += nptes * AMDGPU_GPU_PAGE_SIZE; | |
723 | } | |
724 | ||
fa3ab3c7 CK |
725 | amdgpu_vm_frag_ptes(adev, src, pages_addr, ib, last_pe_start, |
726 | last_pe_end, last_dst, flags); | |
d38ceaf9 AD |
727 | } |
728 | ||
d38ceaf9 AD |
729 | /** |
730 | * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table | |
731 | * | |
732 | * @adev: amdgpu_device pointer | |
fa3ab3c7 CK |
733 | * @src: address where to copy page table entries from |
734 | * @pages_addr: DMA addresses to use for mapping | |
d38ceaf9 | 735 | * @vm: requested vm |
a14faa65 CK |
736 | * @start: start of mapped range |
737 | * @last: last mapped entry | |
738 | * @flags: flags for the entries | |
d38ceaf9 | 739 | * @addr: addr to set the area to |
d38ceaf9 AD |
740 | * @fence: optional resulting fence |
741 | * | |
a14faa65 | 742 | * Fill in the page table entries between @start and @last. |
d38ceaf9 | 743 | * Returns 0 for success, -EINVAL for failure. |
d38ceaf9 AD |
744 | */ |
745 | static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev, | |
fa3ab3c7 CK |
746 | uint64_t src, |
747 | dma_addr_t *pages_addr, | |
d38ceaf9 | 748 | struct amdgpu_vm *vm, |
a14faa65 CK |
749 | uint64_t start, uint64_t last, |
750 | uint32_t flags, uint64_t addr, | |
751 | struct fence **fence) | |
d38ceaf9 | 752 | { |
2d55e45a | 753 | struct amdgpu_ring *ring; |
a1e08d3b | 754 | void *owner = AMDGPU_FENCE_OWNER_VM; |
d38ceaf9 | 755 | unsigned nptes, ncmds, ndw; |
d71518b5 | 756 | struct amdgpu_job *job; |
d5fc5e82 | 757 | struct amdgpu_ib *ib; |
4af9f07c | 758 | struct fence *f = NULL; |
d38ceaf9 AD |
759 | int r; |
760 | ||
2d55e45a CK |
761 | ring = container_of(vm->entity.sched, struct amdgpu_ring, sched); |
762 | ||
a1e08d3b CK |
763 | /* sync to everything on unmapping */ |
764 | if (!(flags & AMDGPU_PTE_VALID)) | |
765 | owner = AMDGPU_FENCE_OWNER_UNDEFINED; | |
766 | ||
a14faa65 | 767 | nptes = last - start + 1; |
d38ceaf9 AD |
768 | |
769 | /* | |
770 | * reserve space for one command every (1 << BLOCK_SIZE) | |
771 | * entries or 2k dwords (whatever is smaller) | |
772 | */ | |
773 | ncmds = (nptes >> min(amdgpu_vm_block_size, 11)) + 1; | |
774 | ||
775 | /* padding, etc. */ | |
776 | ndw = 64; | |
777 | ||
fa3ab3c7 | 778 | if (src) { |
d38ceaf9 AD |
779 | /* only copy commands needed */ |
780 | ndw += ncmds * 7; | |
781 | ||
fa3ab3c7 | 782 | } else if (pages_addr) { |
d38ceaf9 AD |
783 | /* header for write data commands */ |
784 | ndw += ncmds * 4; | |
785 | ||
786 | /* body of write data command */ | |
787 | ndw += nptes * 2; | |
788 | ||
789 | } else { | |
790 | /* set page commands needed */ | |
791 | ndw += ncmds * 10; | |
792 | ||
793 | /* two extra commands for begin/end of fragment */ | |
794 | ndw += 2 * 10; | |
795 | } | |
796 | ||
d71518b5 CK |
797 | r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job); |
798 | if (r) | |
d38ceaf9 | 799 | return r; |
d71518b5 CK |
800 | |
801 | ib = &job->ibs[0]; | |
d5fc5e82 | 802 | |
e86f9cee | 803 | r = amdgpu_sync_resv(adev, &job->sync, vm->page_directory->tbo.resv, |
a1e08d3b CK |
804 | owner); |
805 | if (r) | |
806 | goto error_free; | |
d38ceaf9 | 807 | |
a1e08d3b CK |
808 | r = reservation_object_reserve_shared(vm->page_directory->tbo.resv); |
809 | if (r) | |
810 | goto error_free; | |
811 | ||
fa3ab3c7 CK |
812 | amdgpu_vm_update_ptes(adev, src, pages_addr, vm, ib, start, |
813 | last + 1, addr, flags); | |
d38ceaf9 | 814 | |
9e5d5309 | 815 | amdgpu_ring_pad_ib(ring, ib); |
d5fc5e82 | 816 | WARN_ON(ib->length_dw > ndw); |
2bd9ccfa CK |
817 | r = amdgpu_job_submit(job, ring, &vm->entity, |
818 | AMDGPU_FENCE_OWNER_VM, &f); | |
4af9f07c CZ |
819 | if (r) |
820 | goto error_free; | |
d38ceaf9 | 821 | |
bf60efd3 | 822 | amdgpu_bo_fence(vm->page_directory, f, true); |
4af9f07c CZ |
823 | if (fence) { |
824 | fence_put(*fence); | |
825 | *fence = fence_get(f); | |
826 | } | |
281b4223 | 827 | fence_put(f); |
d38ceaf9 | 828 | return 0; |
d5fc5e82 CZ |
829 | |
830 | error_free: | |
d71518b5 | 831 | amdgpu_job_free(job); |
4af9f07c | 832 | return r; |
d38ceaf9 AD |
833 | } |
834 | ||
a14faa65 CK |
835 | /** |
836 | * amdgpu_vm_bo_split_mapping - split a mapping into smaller chunks | |
837 | * | |
838 | * @adev: amdgpu_device pointer | |
8358dcee CK |
839 | * @gtt_flags: flags as they are used for GTT |
840 | * @pages_addr: DMA addresses to use for mapping | |
a14faa65 CK |
841 | * @vm: requested vm |
842 | * @mapping: mapped range and flags to use for the update | |
843 | * @addr: addr to set the area to | |
8358dcee | 844 | * @flags: HW flags for the mapping |
a14faa65 CK |
845 | * @fence: optional resulting fence |
846 | * | |
847 | * Split the mapping into smaller chunks so that each update fits | |
848 | * into a SDMA IB. | |
849 | * Returns 0 for success, -EINVAL for failure. | |
850 | */ | |
851 | static int amdgpu_vm_bo_split_mapping(struct amdgpu_device *adev, | |
a14faa65 | 852 | uint32_t gtt_flags, |
8358dcee | 853 | dma_addr_t *pages_addr, |
a14faa65 CK |
854 | struct amdgpu_vm *vm, |
855 | struct amdgpu_bo_va_mapping *mapping, | |
fa3ab3c7 CK |
856 | uint32_t flags, uint64_t addr, |
857 | struct fence **fence) | |
a14faa65 CK |
858 | { |
859 | const uint64_t max_size = 64ULL * 1024ULL * 1024ULL / AMDGPU_GPU_PAGE_SIZE; | |
860 | ||
fa3ab3c7 | 861 | uint64_t src = 0, start = mapping->it.start; |
a14faa65 CK |
862 | int r; |
863 | ||
864 | /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here | |
865 | * but in case of something, we filter the flags in first place | |
866 | */ | |
867 | if (!(mapping->flags & AMDGPU_PTE_READABLE)) | |
868 | flags &= ~AMDGPU_PTE_READABLE; | |
869 | if (!(mapping->flags & AMDGPU_PTE_WRITEABLE)) | |
870 | flags &= ~AMDGPU_PTE_WRITEABLE; | |
871 | ||
872 | trace_amdgpu_vm_bo_update(mapping); | |
873 | ||
8358dcee | 874 | if (pages_addr) { |
fa3ab3c7 CK |
875 | if (flags == gtt_flags) |
876 | src = adev->gart.table_addr + (addr >> 12) * 8; | |
fa3ab3c7 CK |
877 | addr = 0; |
878 | } | |
a14faa65 CK |
879 | addr += mapping->offset; |
880 | ||
8358dcee | 881 | if (!pages_addr || src) |
fa3ab3c7 | 882 | return amdgpu_vm_bo_update_mapping(adev, src, pages_addr, vm, |
a14faa65 CK |
883 | start, mapping->it.last, |
884 | flags, addr, fence); | |
885 | ||
886 | while (start != mapping->it.last + 1) { | |
887 | uint64_t last; | |
888 | ||
fb29b57c | 889 | last = min((uint64_t)mapping->it.last, start + max_size - 1); |
fa3ab3c7 | 890 | r = amdgpu_vm_bo_update_mapping(adev, src, pages_addr, vm, |
a14faa65 CK |
891 | start, last, flags, addr, |
892 | fence); | |
893 | if (r) | |
894 | return r; | |
895 | ||
896 | start = last + 1; | |
fb29b57c | 897 | addr += max_size * AMDGPU_GPU_PAGE_SIZE; |
a14faa65 CK |
898 | } |
899 | ||
900 | return 0; | |
901 | } | |
902 | ||
d38ceaf9 AD |
903 | /** |
904 | * amdgpu_vm_bo_update - update all BO mappings in the vm page table | |
905 | * | |
906 | * @adev: amdgpu_device pointer | |
907 | * @bo_va: requested BO and VM object | |
908 | * @mem: ttm mem | |
909 | * | |
910 | * Fill in the page table entries for @bo_va. | |
911 | * Returns 0 for success, -EINVAL for failure. | |
912 | * | |
913 | * Object have to be reserved and mutex must be locked! | |
914 | */ | |
915 | int amdgpu_vm_bo_update(struct amdgpu_device *adev, | |
916 | struct amdgpu_bo_va *bo_va, | |
917 | struct ttm_mem_reg *mem) | |
918 | { | |
919 | struct amdgpu_vm *vm = bo_va->vm; | |
920 | struct amdgpu_bo_va_mapping *mapping; | |
8358dcee | 921 | dma_addr_t *pages_addr = NULL; |
fa3ab3c7 | 922 | uint32_t gtt_flags, flags; |
d38ceaf9 AD |
923 | uint64_t addr; |
924 | int r; | |
925 | ||
926 | if (mem) { | |
8358dcee CK |
927 | struct ttm_dma_tt *ttm; |
928 | ||
b7d698d7 | 929 | addr = (u64)mem->start << PAGE_SHIFT; |
9ab21462 CK |
930 | switch (mem->mem_type) { |
931 | case TTM_PL_TT: | |
8358dcee CK |
932 | ttm = container_of(bo_va->bo->tbo.ttm, struct |
933 | ttm_dma_tt, ttm); | |
934 | pages_addr = ttm->dma_address; | |
9ab21462 CK |
935 | break; |
936 | ||
937 | case TTM_PL_VRAM: | |
d38ceaf9 | 938 | addr += adev->vm_manager.vram_base_offset; |
9ab21462 CK |
939 | break; |
940 | ||
941 | default: | |
942 | break; | |
943 | } | |
d38ceaf9 AD |
944 | } else { |
945 | addr = 0; | |
946 | } | |
947 | ||
d38ceaf9 | 948 | flags = amdgpu_ttm_tt_pte_flags(adev, bo_va->bo->tbo.ttm, mem); |
fa3ab3c7 | 949 | gtt_flags = (adev == bo_va->bo->adev) ? flags : 0; |
d38ceaf9 | 950 | |
7fc11959 CK |
951 | spin_lock(&vm->status_lock); |
952 | if (!list_empty(&bo_va->vm_status)) | |
953 | list_splice_init(&bo_va->valids, &bo_va->invalids); | |
954 | spin_unlock(&vm->status_lock); | |
955 | ||
956 | list_for_each_entry(mapping, &bo_va->invalids, list) { | |
8358dcee CK |
957 | r = amdgpu_vm_bo_split_mapping(adev, gtt_flags, pages_addr, vm, |
958 | mapping, flags, addr, | |
959 | &bo_va->last_pt_update); | |
d38ceaf9 AD |
960 | if (r) |
961 | return r; | |
962 | } | |
963 | ||
d6c10f6b CK |
964 | if (trace_amdgpu_vm_bo_mapping_enabled()) { |
965 | list_for_each_entry(mapping, &bo_va->valids, list) | |
966 | trace_amdgpu_vm_bo_mapping(mapping); | |
967 | ||
968 | list_for_each_entry(mapping, &bo_va->invalids, list) | |
969 | trace_amdgpu_vm_bo_mapping(mapping); | |
970 | } | |
971 | ||
d38ceaf9 | 972 | spin_lock(&vm->status_lock); |
6d1d0ef7 | 973 | list_splice_init(&bo_va->invalids, &bo_va->valids); |
d38ceaf9 | 974 | list_del_init(&bo_va->vm_status); |
7fc11959 CK |
975 | if (!mem) |
976 | list_add(&bo_va->vm_status, &vm->cleared); | |
d38ceaf9 AD |
977 | spin_unlock(&vm->status_lock); |
978 | ||
979 | return 0; | |
980 | } | |
981 | ||
982 | /** | |
983 | * amdgpu_vm_clear_freed - clear freed BOs in the PT | |
984 | * | |
985 | * @adev: amdgpu_device pointer | |
986 | * @vm: requested vm | |
987 | * | |
988 | * Make sure all freed BOs are cleared in the PT. | |
989 | * Returns 0 for success. | |
990 | * | |
991 | * PTs have to be reserved and mutex must be locked! | |
992 | */ | |
993 | int amdgpu_vm_clear_freed(struct amdgpu_device *adev, | |
994 | struct amdgpu_vm *vm) | |
995 | { | |
996 | struct amdgpu_bo_va_mapping *mapping; | |
997 | int r; | |
998 | ||
999 | while (!list_empty(&vm->freed)) { | |
1000 | mapping = list_first_entry(&vm->freed, | |
1001 | struct amdgpu_bo_va_mapping, list); | |
1002 | list_del(&mapping->list); | |
e17841b9 | 1003 | |
8358dcee | 1004 | r = amdgpu_vm_bo_split_mapping(adev, 0, NULL, vm, mapping, |
fa3ab3c7 | 1005 | 0, 0, NULL); |
d38ceaf9 AD |
1006 | kfree(mapping); |
1007 | if (r) | |
1008 | return r; | |
1009 | ||
1010 | } | |
1011 | return 0; | |
1012 | ||
1013 | } | |
1014 | ||
1015 | /** | |
1016 | * amdgpu_vm_clear_invalids - clear invalidated BOs in the PT | |
1017 | * | |
1018 | * @adev: amdgpu_device pointer | |
1019 | * @vm: requested vm | |
1020 | * | |
1021 | * Make sure all invalidated BOs are cleared in the PT. | |
1022 | * Returns 0 for success. | |
1023 | * | |
1024 | * PTs have to be reserved and mutex must be locked! | |
1025 | */ | |
1026 | int amdgpu_vm_clear_invalids(struct amdgpu_device *adev, | |
cfe2c978 | 1027 | struct amdgpu_vm *vm, struct amdgpu_sync *sync) |
d38ceaf9 | 1028 | { |
cfe2c978 | 1029 | struct amdgpu_bo_va *bo_va = NULL; |
91e1a520 | 1030 | int r = 0; |
d38ceaf9 AD |
1031 | |
1032 | spin_lock(&vm->status_lock); | |
1033 | while (!list_empty(&vm->invalidated)) { | |
1034 | bo_va = list_first_entry(&vm->invalidated, | |
1035 | struct amdgpu_bo_va, vm_status); | |
1036 | spin_unlock(&vm->status_lock); | |
32b41ac2 | 1037 | |
d38ceaf9 AD |
1038 | r = amdgpu_vm_bo_update(adev, bo_va, NULL); |
1039 | if (r) | |
1040 | return r; | |
1041 | ||
1042 | spin_lock(&vm->status_lock); | |
1043 | } | |
1044 | spin_unlock(&vm->status_lock); | |
1045 | ||
cfe2c978 | 1046 | if (bo_va) |
bb1e38a4 | 1047 | r = amdgpu_sync_fence(adev, sync, bo_va->last_pt_update); |
91e1a520 CK |
1048 | |
1049 | return r; | |
d38ceaf9 AD |
1050 | } |
1051 | ||
1052 | /** | |
1053 | * amdgpu_vm_bo_add - add a bo to a specific vm | |
1054 | * | |
1055 | * @adev: amdgpu_device pointer | |
1056 | * @vm: requested vm | |
1057 | * @bo: amdgpu buffer object | |
1058 | * | |
8843dbbb | 1059 | * Add @bo into the requested vm. |
d38ceaf9 AD |
1060 | * Add @bo to the list of bos associated with the vm |
1061 | * Returns newly added bo_va or NULL for failure | |
1062 | * | |
1063 | * Object has to be reserved! | |
1064 | */ | |
1065 | struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev, | |
1066 | struct amdgpu_vm *vm, | |
1067 | struct amdgpu_bo *bo) | |
1068 | { | |
1069 | struct amdgpu_bo_va *bo_va; | |
1070 | ||
1071 | bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL); | |
1072 | if (bo_va == NULL) { | |
1073 | return NULL; | |
1074 | } | |
1075 | bo_va->vm = vm; | |
1076 | bo_va->bo = bo; | |
d38ceaf9 AD |
1077 | bo_va->ref_count = 1; |
1078 | INIT_LIST_HEAD(&bo_va->bo_list); | |
7fc11959 CK |
1079 | INIT_LIST_HEAD(&bo_va->valids); |
1080 | INIT_LIST_HEAD(&bo_va->invalids); | |
d38ceaf9 | 1081 | INIT_LIST_HEAD(&bo_va->vm_status); |
32b41ac2 | 1082 | |
d38ceaf9 | 1083 | list_add_tail(&bo_va->bo_list, &bo->va); |
d38ceaf9 AD |
1084 | |
1085 | return bo_va; | |
1086 | } | |
1087 | ||
1088 | /** | |
1089 | * amdgpu_vm_bo_map - map bo inside a vm | |
1090 | * | |
1091 | * @adev: amdgpu_device pointer | |
1092 | * @bo_va: bo_va to store the address | |
1093 | * @saddr: where to map the BO | |
1094 | * @offset: requested offset in the BO | |
1095 | * @flags: attributes of pages (read/write/valid/etc.) | |
1096 | * | |
1097 | * Add a mapping of the BO at the specefied addr into the VM. | |
1098 | * Returns 0 for success, error for failure. | |
1099 | * | |
49b02b18 | 1100 | * Object has to be reserved and unreserved outside! |
d38ceaf9 AD |
1101 | */ |
1102 | int amdgpu_vm_bo_map(struct amdgpu_device *adev, | |
1103 | struct amdgpu_bo_va *bo_va, | |
1104 | uint64_t saddr, uint64_t offset, | |
1105 | uint64_t size, uint32_t flags) | |
1106 | { | |
1107 | struct amdgpu_bo_va_mapping *mapping; | |
1108 | struct amdgpu_vm *vm = bo_va->vm; | |
1109 | struct interval_tree_node *it; | |
1110 | unsigned last_pfn, pt_idx; | |
1111 | uint64_t eaddr; | |
1112 | int r; | |
1113 | ||
0be52de9 CK |
1114 | /* validate the parameters */ |
1115 | if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK || | |
49b02b18 | 1116 | size == 0 || size & AMDGPU_GPU_PAGE_MASK) |
0be52de9 | 1117 | return -EINVAL; |
0be52de9 | 1118 | |
d38ceaf9 | 1119 | /* make sure object fit at this offset */ |
005ae95e | 1120 | eaddr = saddr + size - 1; |
49b02b18 | 1121 | if ((saddr >= eaddr) || (offset + size > amdgpu_bo_size(bo_va->bo))) |
d38ceaf9 | 1122 | return -EINVAL; |
d38ceaf9 AD |
1123 | |
1124 | last_pfn = eaddr / AMDGPU_GPU_PAGE_SIZE; | |
005ae95e FK |
1125 | if (last_pfn >= adev->vm_manager.max_pfn) { |
1126 | dev_err(adev->dev, "va above limit (0x%08X >= 0x%08X)\n", | |
d38ceaf9 | 1127 | last_pfn, adev->vm_manager.max_pfn); |
d38ceaf9 AD |
1128 | return -EINVAL; |
1129 | } | |
1130 | ||
d38ceaf9 AD |
1131 | saddr /= AMDGPU_GPU_PAGE_SIZE; |
1132 | eaddr /= AMDGPU_GPU_PAGE_SIZE; | |
1133 | ||
005ae95e | 1134 | it = interval_tree_iter_first(&vm->va, saddr, eaddr); |
d38ceaf9 AD |
1135 | if (it) { |
1136 | struct amdgpu_bo_va_mapping *tmp; | |
1137 | tmp = container_of(it, struct amdgpu_bo_va_mapping, it); | |
1138 | /* bo and tmp overlap, invalid addr */ | |
1139 | dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with " | |
1140 | "0x%010lx-0x%010lx\n", bo_va->bo, saddr, eaddr, | |
1141 | tmp->it.start, tmp->it.last + 1); | |
d38ceaf9 | 1142 | r = -EINVAL; |
f48b2659 | 1143 | goto error; |
d38ceaf9 AD |
1144 | } |
1145 | ||
1146 | mapping = kmalloc(sizeof(*mapping), GFP_KERNEL); | |
1147 | if (!mapping) { | |
d38ceaf9 | 1148 | r = -ENOMEM; |
f48b2659 | 1149 | goto error; |
d38ceaf9 AD |
1150 | } |
1151 | ||
1152 | INIT_LIST_HEAD(&mapping->list); | |
1153 | mapping->it.start = saddr; | |
005ae95e | 1154 | mapping->it.last = eaddr; |
d38ceaf9 AD |
1155 | mapping->offset = offset; |
1156 | mapping->flags = flags; | |
1157 | ||
7fc11959 | 1158 | list_add(&mapping->list, &bo_va->invalids); |
d38ceaf9 AD |
1159 | interval_tree_insert(&mapping->it, &vm->va); |
1160 | ||
1161 | /* Make sure the page tables are allocated */ | |
1162 | saddr >>= amdgpu_vm_block_size; | |
1163 | eaddr >>= amdgpu_vm_block_size; | |
1164 | ||
1165 | BUG_ON(eaddr >= amdgpu_vm_num_pdes(adev)); | |
1166 | ||
1167 | if (eaddr > vm->max_pde_used) | |
1168 | vm->max_pde_used = eaddr; | |
1169 | ||
d38ceaf9 AD |
1170 | /* walk over the address space and allocate the page tables */ |
1171 | for (pt_idx = saddr; pt_idx <= eaddr; ++pt_idx) { | |
bf60efd3 | 1172 | struct reservation_object *resv = vm->page_directory->tbo.resv; |
ee1782c3 | 1173 | struct amdgpu_bo_list_entry *entry; |
d38ceaf9 AD |
1174 | struct amdgpu_bo *pt; |
1175 | ||
ee1782c3 CK |
1176 | entry = &vm->page_tables[pt_idx].entry; |
1177 | if (entry->robj) | |
d38ceaf9 AD |
1178 | continue; |
1179 | ||
d38ceaf9 AD |
1180 | r = amdgpu_bo_create(adev, AMDGPU_VM_PTE_COUNT * 8, |
1181 | AMDGPU_GPU_PAGE_SIZE, true, | |
857d913d AD |
1182 | AMDGPU_GEM_DOMAIN_VRAM, |
1183 | AMDGPU_GEM_CREATE_NO_CPU_ACCESS, | |
bf60efd3 | 1184 | NULL, resv, &pt); |
49b02b18 | 1185 | if (r) |
d38ceaf9 | 1186 | goto error_free; |
49b02b18 | 1187 | |
82b9c55b CK |
1188 | /* Keep a reference to the page table to avoid freeing |
1189 | * them up in the wrong order. | |
1190 | */ | |
1191 | pt->parent = amdgpu_bo_ref(vm->page_directory); | |
1192 | ||
2bd9ccfa | 1193 | r = amdgpu_vm_clear_bo(adev, vm, pt); |
d38ceaf9 AD |
1194 | if (r) { |
1195 | amdgpu_bo_unref(&pt); | |
1196 | goto error_free; | |
1197 | } | |
1198 | ||
ee1782c3 | 1199 | entry->robj = pt; |
ee1782c3 CK |
1200 | entry->priority = 0; |
1201 | entry->tv.bo = &entry->robj->tbo; | |
1202 | entry->tv.shared = true; | |
2f568dbd | 1203 | entry->user_pages = NULL; |
d38ceaf9 | 1204 | vm->page_tables[pt_idx].addr = 0; |
d38ceaf9 AD |
1205 | } |
1206 | ||
d38ceaf9 AD |
1207 | return 0; |
1208 | ||
1209 | error_free: | |
d38ceaf9 AD |
1210 | list_del(&mapping->list); |
1211 | interval_tree_remove(&mapping->it, &vm->va); | |
93e3e438 | 1212 | trace_amdgpu_vm_bo_unmap(bo_va, mapping); |
d38ceaf9 AD |
1213 | kfree(mapping); |
1214 | ||
f48b2659 | 1215 | error: |
d38ceaf9 AD |
1216 | return r; |
1217 | } | |
1218 | ||
1219 | /** | |
1220 | * amdgpu_vm_bo_unmap - remove bo mapping from vm | |
1221 | * | |
1222 | * @adev: amdgpu_device pointer | |
1223 | * @bo_va: bo_va to remove the address from | |
1224 | * @saddr: where to the BO is mapped | |
1225 | * | |
1226 | * Remove a mapping of the BO at the specefied addr from the VM. | |
1227 | * Returns 0 for success, error for failure. | |
1228 | * | |
49b02b18 | 1229 | * Object has to be reserved and unreserved outside! |
d38ceaf9 AD |
1230 | */ |
1231 | int amdgpu_vm_bo_unmap(struct amdgpu_device *adev, | |
1232 | struct amdgpu_bo_va *bo_va, | |
1233 | uint64_t saddr) | |
1234 | { | |
1235 | struct amdgpu_bo_va_mapping *mapping; | |
1236 | struct amdgpu_vm *vm = bo_va->vm; | |
7fc11959 | 1237 | bool valid = true; |
d38ceaf9 | 1238 | |
6c7fc503 | 1239 | saddr /= AMDGPU_GPU_PAGE_SIZE; |
32b41ac2 | 1240 | |
7fc11959 | 1241 | list_for_each_entry(mapping, &bo_va->valids, list) { |
d38ceaf9 AD |
1242 | if (mapping->it.start == saddr) |
1243 | break; | |
1244 | } | |
1245 | ||
7fc11959 CK |
1246 | if (&mapping->list == &bo_va->valids) { |
1247 | valid = false; | |
1248 | ||
1249 | list_for_each_entry(mapping, &bo_va->invalids, list) { | |
1250 | if (mapping->it.start == saddr) | |
1251 | break; | |
1252 | } | |
1253 | ||
32b41ac2 | 1254 | if (&mapping->list == &bo_va->invalids) |
7fc11959 | 1255 | return -ENOENT; |
d38ceaf9 | 1256 | } |
32b41ac2 | 1257 | |
d38ceaf9 AD |
1258 | list_del(&mapping->list); |
1259 | interval_tree_remove(&mapping->it, &vm->va); | |
93e3e438 | 1260 | trace_amdgpu_vm_bo_unmap(bo_va, mapping); |
d38ceaf9 | 1261 | |
e17841b9 | 1262 | if (valid) |
d38ceaf9 | 1263 | list_add(&mapping->list, &vm->freed); |
e17841b9 | 1264 | else |
d38ceaf9 | 1265 | kfree(mapping); |
d38ceaf9 AD |
1266 | |
1267 | return 0; | |
1268 | } | |
1269 | ||
1270 | /** | |
1271 | * amdgpu_vm_bo_rmv - remove a bo to a specific vm | |
1272 | * | |
1273 | * @adev: amdgpu_device pointer | |
1274 | * @bo_va: requested bo_va | |
1275 | * | |
8843dbbb | 1276 | * Remove @bo_va->bo from the requested vm. |
d38ceaf9 AD |
1277 | * |
1278 | * Object have to be reserved! | |
1279 | */ | |
1280 | void amdgpu_vm_bo_rmv(struct amdgpu_device *adev, | |
1281 | struct amdgpu_bo_va *bo_va) | |
1282 | { | |
1283 | struct amdgpu_bo_va_mapping *mapping, *next; | |
1284 | struct amdgpu_vm *vm = bo_va->vm; | |
1285 | ||
1286 | list_del(&bo_va->bo_list); | |
1287 | ||
d38ceaf9 AD |
1288 | spin_lock(&vm->status_lock); |
1289 | list_del(&bo_va->vm_status); | |
1290 | spin_unlock(&vm->status_lock); | |
1291 | ||
7fc11959 | 1292 | list_for_each_entry_safe(mapping, next, &bo_va->valids, list) { |
d38ceaf9 AD |
1293 | list_del(&mapping->list); |
1294 | interval_tree_remove(&mapping->it, &vm->va); | |
93e3e438 | 1295 | trace_amdgpu_vm_bo_unmap(bo_va, mapping); |
7fc11959 CK |
1296 | list_add(&mapping->list, &vm->freed); |
1297 | } | |
1298 | list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) { | |
1299 | list_del(&mapping->list); | |
1300 | interval_tree_remove(&mapping->it, &vm->va); | |
1301 | kfree(mapping); | |
d38ceaf9 | 1302 | } |
32b41ac2 | 1303 | |
bb1e38a4 | 1304 | fence_put(bo_va->last_pt_update); |
d38ceaf9 | 1305 | kfree(bo_va); |
d38ceaf9 AD |
1306 | } |
1307 | ||
1308 | /** | |
1309 | * amdgpu_vm_bo_invalidate - mark the bo as invalid | |
1310 | * | |
1311 | * @adev: amdgpu_device pointer | |
1312 | * @vm: requested vm | |
1313 | * @bo: amdgpu buffer object | |
1314 | * | |
8843dbbb | 1315 | * Mark @bo as invalid. |
d38ceaf9 AD |
1316 | */ |
1317 | void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev, | |
1318 | struct amdgpu_bo *bo) | |
1319 | { | |
1320 | struct amdgpu_bo_va *bo_va; | |
1321 | ||
1322 | list_for_each_entry(bo_va, &bo->va, bo_list) { | |
7fc11959 CK |
1323 | spin_lock(&bo_va->vm->status_lock); |
1324 | if (list_empty(&bo_va->vm_status)) | |
d38ceaf9 | 1325 | list_add(&bo_va->vm_status, &bo_va->vm->invalidated); |
7fc11959 | 1326 | spin_unlock(&bo_va->vm->status_lock); |
d38ceaf9 AD |
1327 | } |
1328 | } | |
1329 | ||
1330 | /** | |
1331 | * amdgpu_vm_init - initialize a vm instance | |
1332 | * | |
1333 | * @adev: amdgpu_device pointer | |
1334 | * @vm: requested vm | |
1335 | * | |
8843dbbb | 1336 | * Init @vm fields. |
d38ceaf9 AD |
1337 | */ |
1338 | int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm) | |
1339 | { | |
1340 | const unsigned align = min(AMDGPU_VM_PTB_ALIGN_SIZE, | |
1341 | AMDGPU_VM_PTE_COUNT * 8); | |
9571e1d8 | 1342 | unsigned pd_size, pd_entries; |
2d55e45a CK |
1343 | unsigned ring_instance; |
1344 | struct amdgpu_ring *ring; | |
2bd9ccfa | 1345 | struct amd_sched_rq *rq; |
d38ceaf9 AD |
1346 | int i, r; |
1347 | ||
1348 | for (i = 0; i < AMDGPU_MAX_RINGS; ++i) { | |
4ff37a83 | 1349 | vm->ids[i].mgr_id = NULL; |
d38ceaf9 | 1350 | vm->ids[i].flushed_updates = NULL; |
d38ceaf9 | 1351 | } |
d38ceaf9 AD |
1352 | vm->va = RB_ROOT; |
1353 | spin_lock_init(&vm->status_lock); | |
1354 | INIT_LIST_HEAD(&vm->invalidated); | |
7fc11959 | 1355 | INIT_LIST_HEAD(&vm->cleared); |
d38ceaf9 | 1356 | INIT_LIST_HEAD(&vm->freed); |
20250215 | 1357 | |
d38ceaf9 AD |
1358 | pd_size = amdgpu_vm_directory_size(adev); |
1359 | pd_entries = amdgpu_vm_num_pdes(adev); | |
1360 | ||
1361 | /* allocate page table array */ | |
9571e1d8 | 1362 | vm->page_tables = drm_calloc_large(pd_entries, sizeof(struct amdgpu_vm_pt)); |
d38ceaf9 AD |
1363 | if (vm->page_tables == NULL) { |
1364 | DRM_ERROR("Cannot allocate memory for page table array\n"); | |
1365 | return -ENOMEM; | |
1366 | } | |
1367 | ||
2bd9ccfa | 1368 | /* create scheduler entity for page table updates */ |
2d55e45a CK |
1369 | |
1370 | ring_instance = atomic_inc_return(&adev->vm_manager.vm_pte_next_ring); | |
1371 | ring_instance %= adev->vm_manager.vm_pte_num_rings; | |
1372 | ring = adev->vm_manager.vm_pte_rings[ring_instance]; | |
2bd9ccfa CK |
1373 | rq = &ring->sched.sched_rq[AMD_SCHED_PRIORITY_KERNEL]; |
1374 | r = amd_sched_entity_init(&ring->sched, &vm->entity, | |
1375 | rq, amdgpu_sched_jobs); | |
1376 | if (r) | |
1377 | return r; | |
1378 | ||
05906dec BN |
1379 | vm->page_directory_fence = NULL; |
1380 | ||
d38ceaf9 | 1381 | r = amdgpu_bo_create(adev, pd_size, align, true, |
857d913d AD |
1382 | AMDGPU_GEM_DOMAIN_VRAM, |
1383 | AMDGPU_GEM_CREATE_NO_CPU_ACCESS, | |
72d7668b | 1384 | NULL, NULL, &vm->page_directory); |
d38ceaf9 | 1385 | if (r) |
2bd9ccfa CK |
1386 | goto error_free_sched_entity; |
1387 | ||
ef9f0a83 | 1388 | r = amdgpu_bo_reserve(vm->page_directory, false); |
2bd9ccfa CK |
1389 | if (r) |
1390 | goto error_free_page_directory; | |
1391 | ||
1392 | r = amdgpu_vm_clear_bo(adev, vm, vm->page_directory); | |
ef9f0a83 | 1393 | amdgpu_bo_unreserve(vm->page_directory); |
2bd9ccfa CK |
1394 | if (r) |
1395 | goto error_free_page_directory; | |
d38ceaf9 AD |
1396 | |
1397 | return 0; | |
2bd9ccfa CK |
1398 | |
1399 | error_free_page_directory: | |
1400 | amdgpu_bo_unref(&vm->page_directory); | |
1401 | vm->page_directory = NULL; | |
1402 | ||
1403 | error_free_sched_entity: | |
1404 | amd_sched_entity_fini(&ring->sched, &vm->entity); | |
1405 | ||
1406 | return r; | |
d38ceaf9 AD |
1407 | } |
1408 | ||
1409 | /** | |
1410 | * amdgpu_vm_fini - tear down a vm instance | |
1411 | * | |
1412 | * @adev: amdgpu_device pointer | |
1413 | * @vm: requested vm | |
1414 | * | |
8843dbbb | 1415 | * Tear down @vm. |
d38ceaf9 AD |
1416 | * Unbind the VM and remove all bos from the vm bo list |
1417 | */ | |
1418 | void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm) | |
1419 | { | |
1420 | struct amdgpu_bo_va_mapping *mapping, *tmp; | |
1421 | int i; | |
1422 | ||
2d55e45a | 1423 | amd_sched_entity_fini(vm->entity.sched, &vm->entity); |
2bd9ccfa | 1424 | |
d38ceaf9 AD |
1425 | if (!RB_EMPTY_ROOT(&vm->va)) { |
1426 | dev_err(adev->dev, "still active bo inside vm\n"); | |
1427 | } | |
1428 | rbtree_postorder_for_each_entry_safe(mapping, tmp, &vm->va, it.rb) { | |
1429 | list_del(&mapping->list); | |
1430 | interval_tree_remove(&mapping->it, &vm->va); | |
1431 | kfree(mapping); | |
1432 | } | |
1433 | list_for_each_entry_safe(mapping, tmp, &vm->freed, list) { | |
1434 | list_del(&mapping->list); | |
1435 | kfree(mapping); | |
1436 | } | |
1437 | ||
1438 | for (i = 0; i < amdgpu_vm_num_pdes(adev); i++) | |
ee1782c3 | 1439 | amdgpu_bo_unref(&vm->page_tables[i].entry.robj); |
9571e1d8 | 1440 | drm_free_large(vm->page_tables); |
d38ceaf9 AD |
1441 | |
1442 | amdgpu_bo_unref(&vm->page_directory); | |
05906dec | 1443 | fence_put(vm->page_directory_fence); |
20250215 | 1444 | |
d38ceaf9 | 1445 | for (i = 0; i < AMDGPU_MAX_RINGS; ++i) { |
4ff37a83 | 1446 | struct amdgpu_vm_id *id = &vm->ids[i]; |
1c16c0a7 | 1447 | |
4ff37a83 CK |
1448 | if (id->mgr_id) |
1449 | atomic_long_cmpxchg(&id->mgr_id->owner, | |
1450 | (long)id, 0); | |
1451 | fence_put(id->flushed_updates); | |
d38ceaf9 | 1452 | } |
d38ceaf9 | 1453 | } |
ea89f8c9 | 1454 | |
a9a78b32 CK |
1455 | /** |
1456 | * amdgpu_vm_manager_init - init the VM manager | |
1457 | * | |
1458 | * @adev: amdgpu_device pointer | |
1459 | * | |
1460 | * Initialize the VM manager structures | |
1461 | */ | |
1462 | void amdgpu_vm_manager_init(struct amdgpu_device *adev) | |
1463 | { | |
1464 | unsigned i; | |
1465 | ||
1466 | INIT_LIST_HEAD(&adev->vm_manager.ids_lru); | |
1467 | ||
1468 | /* skip over VMID 0, since it is the system VM */ | |
971fe9a9 CK |
1469 | for (i = 1; i < adev->vm_manager.num_ids; ++i) { |
1470 | amdgpu_vm_reset_id(adev, i); | |
a9a78b32 CK |
1471 | list_add_tail(&adev->vm_manager.ids[i].list, |
1472 | &adev->vm_manager.ids_lru); | |
971fe9a9 | 1473 | } |
2d55e45a CK |
1474 | |
1475 | atomic_set(&adev->vm_manager.vm_pte_next_ring, 0); | |
a9a78b32 CK |
1476 | } |
1477 | ||
ea89f8c9 CK |
1478 | /** |
1479 | * amdgpu_vm_manager_fini - cleanup VM manager | |
1480 | * | |
1481 | * @adev: amdgpu_device pointer | |
1482 | * | |
1483 | * Cleanup the VM manager and free resources. | |
1484 | */ | |
1485 | void amdgpu_vm_manager_fini(struct amdgpu_device *adev) | |
1486 | { | |
1487 | unsigned i; | |
1488 | ||
1489 | for (i = 0; i < AMDGPU_NUM_VM; ++i) | |
1c16c0a7 | 1490 | fence_put(adev->vm_manager.ids[i].active); |
ea89f8c9 | 1491 | } |