]>
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 | */ | |
f54d1867 | 28 | #include <linux/dma-fence-array.h> |
a9f87f64 | 29 | #include <linux/interval_tree_generic.h> |
d38ceaf9 AD |
30 | #include <drm/drmP.h> |
31 | #include <drm/amdgpu_drm.h> | |
32 | #include "amdgpu.h" | |
33 | #include "amdgpu_trace.h" | |
34 | ||
35 | /* | |
36 | * GPUVM | |
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; | |
52 | * SI supports 16. | |
53 | */ | |
54 | ||
a9f87f64 CK |
55 | #define START(node) ((node)->start) |
56 | #define LAST(node) ((node)->last) | |
57 | ||
58 | INTERVAL_TREE_DEFINE(struct amdgpu_bo_va_mapping, rb, uint64_t, __subtree_last, | |
59 | START, LAST, static, amdgpu_vm_it) | |
60 | ||
61 | #undef START | |
62 | #undef LAST | |
63 | ||
f4833c4f HK |
64 | /* Local structure. Encapsulate some VM table update parameters to reduce |
65 | * the number of function parameters | |
66 | */ | |
29efc4f5 | 67 | struct amdgpu_pte_update_params { |
27c5f36f CK |
68 | /* amdgpu device we do this update for */ |
69 | struct amdgpu_device *adev; | |
49ac8a24 CK |
70 | /* optional amdgpu_vm we do this update for */ |
71 | struct amdgpu_vm *vm; | |
f4833c4f HK |
72 | /* address where to copy page table entries from */ |
73 | uint64_t src; | |
f4833c4f HK |
74 | /* indirect buffer to fill with commands */ |
75 | struct amdgpu_ib *ib; | |
afef8b8f CK |
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, | |
6b777607 | 79 | uint64_t flags); |
4c7e8855 CZ |
80 | /* indicate update pt or its shadow */ |
81 | bool shadow; | |
f4833c4f HK |
82 | }; |
83 | ||
284710fa CK |
84 | /* Helper to disable partial resident texture feature from a fence callback */ |
85 | struct amdgpu_prt_cb { | |
86 | struct amdgpu_device *adev; | |
87 | struct dma_fence_cb cb; | |
88 | }; | |
89 | ||
d38ceaf9 | 90 | /** |
72a7ec5c | 91 | * amdgpu_vm_num_entries - return the number of entries in a PD/PT |
d38ceaf9 AD |
92 | * |
93 | * @adev: amdgpu_device pointer | |
94 | * | |
72a7ec5c | 95 | * Calculate the number of entries in a page directory or page table. |
d38ceaf9 | 96 | */ |
72a7ec5c CK |
97 | static unsigned amdgpu_vm_num_entries(struct amdgpu_device *adev, |
98 | unsigned level) | |
d38ceaf9 | 99 | { |
72a7ec5c CK |
100 | if (level == 0) |
101 | /* For the root directory */ | |
102 | return adev->vm_manager.max_pfn >> | |
36b32a68 ZJ |
103 | (adev->vm_manager.block_size * |
104 | adev->vm_manager.num_level); | |
72a7ec5c CK |
105 | else if (level == adev->vm_manager.num_level) |
106 | /* For the page tables on the leaves */ | |
36b32a68 | 107 | return AMDGPU_VM_PTE_COUNT(adev); |
72a7ec5c CK |
108 | else |
109 | /* Everything in between */ | |
36b32a68 | 110 | return 1 << adev->vm_manager.block_size; |
d38ceaf9 AD |
111 | } |
112 | ||
113 | /** | |
72a7ec5c | 114 | * amdgpu_vm_bo_size - returns the size of the BOs in bytes |
d38ceaf9 AD |
115 | * |
116 | * @adev: amdgpu_device pointer | |
117 | * | |
72a7ec5c | 118 | * Calculate the size of the BO for a page directory or page table in bytes. |
d38ceaf9 | 119 | */ |
72a7ec5c | 120 | static unsigned amdgpu_vm_bo_size(struct amdgpu_device *adev, unsigned level) |
d38ceaf9 | 121 | { |
72a7ec5c | 122 | return AMDGPU_GPU_PAGE_ALIGN(amdgpu_vm_num_entries(adev, level) * 8); |
d38ceaf9 AD |
123 | } |
124 | ||
125 | /** | |
56467ebf | 126 | * amdgpu_vm_get_pd_bo - add the VM PD to a validation list |
d38ceaf9 AD |
127 | * |
128 | * @vm: vm providing the BOs | |
3c0eea6c | 129 | * @validated: head of validation list |
56467ebf | 130 | * @entry: entry to add |
d38ceaf9 AD |
131 | * |
132 | * Add the page directory to the list of BOs to | |
56467ebf | 133 | * validate for command submission. |
d38ceaf9 | 134 | */ |
56467ebf CK |
135 | void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm, |
136 | struct list_head *validated, | |
137 | struct amdgpu_bo_list_entry *entry) | |
d38ceaf9 | 138 | { |
67003a15 | 139 | entry->robj = vm->root.bo; |
56467ebf | 140 | entry->priority = 0; |
67003a15 | 141 | entry->tv.bo = &entry->robj->tbo; |
56467ebf | 142 | entry->tv.shared = true; |
2f568dbd | 143 | entry->user_pages = NULL; |
56467ebf CK |
144 | list_add(&entry->tv.head, validated); |
145 | } | |
d38ceaf9 | 146 | |
670fecc8 CK |
147 | /** |
148 | * amdgpu_vm_validate_layer - validate a single page table level | |
149 | * | |
150 | * @parent: parent page table level | |
151 | * @validate: callback to do the validation | |
152 | * @param: parameter for the validation callback | |
153 | * | |
154 | * Validate the page table BOs on command submission if neccessary. | |
155 | */ | |
156 | static int amdgpu_vm_validate_level(struct amdgpu_vm_pt *parent, | |
157 | int (*validate)(void *, struct amdgpu_bo *), | |
158 | void *param) | |
159 | { | |
160 | unsigned i; | |
161 | int r; | |
162 | ||
163 | if (!parent->entries) | |
164 | return 0; | |
165 | ||
166 | for (i = 0; i <= parent->last_entry_used; ++i) { | |
167 | struct amdgpu_vm_pt *entry = &parent->entries[i]; | |
168 | ||
169 | if (!entry->bo) | |
170 | continue; | |
171 | ||
172 | r = validate(param, entry->bo); | |
173 | if (r) | |
174 | return r; | |
175 | ||
176 | /* | |
177 | * Recurse into the sub directory. This is harmless because we | |
178 | * have only a maximum of 5 layers. | |
179 | */ | |
180 | r = amdgpu_vm_validate_level(entry, validate, param); | |
181 | if (r) | |
182 | return r; | |
183 | } | |
184 | ||
185 | return r; | |
186 | } | |
187 | ||
56467ebf | 188 | /** |
f7da30d9 | 189 | * amdgpu_vm_validate_pt_bos - validate the page table BOs |
56467ebf | 190 | * |
5a712a87 | 191 | * @adev: amdgpu device pointer |
56467ebf | 192 | * @vm: vm providing the BOs |
f7da30d9 CK |
193 | * @validate: callback to do the validation |
194 | * @param: parameter for the validation callback | |
d38ceaf9 | 195 | * |
f7da30d9 | 196 | * Validate the page table BOs on command submission if neccessary. |
d38ceaf9 | 197 | */ |
f7da30d9 CK |
198 | int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm, |
199 | int (*validate)(void *p, struct amdgpu_bo *bo), | |
200 | void *param) | |
d38ceaf9 | 201 | { |
5a712a87 | 202 | uint64_t num_evictions; |
d38ceaf9 | 203 | |
5a712a87 CK |
204 | /* We only need to validate the page tables |
205 | * if they aren't already valid. | |
206 | */ | |
207 | num_evictions = atomic64_read(&adev->num_evictions); | |
208 | if (num_evictions == vm->last_eviction_counter) | |
f7da30d9 | 209 | return 0; |
5a712a87 | 210 | |
670fecc8 | 211 | return amdgpu_vm_validate_level(&vm->root, validate, param); |
eceb8a15 CK |
212 | } |
213 | ||
214 | /** | |
d711e139 | 215 | * amdgpu_vm_move_level_in_lru - move one level of PT BOs to the LRU tail |
eceb8a15 CK |
216 | * |
217 | * @adev: amdgpu device instance | |
218 | * @vm: vm providing the BOs | |
219 | * | |
220 | * Move the PT BOs to the tail of the LRU. | |
221 | */ | |
d711e139 | 222 | static void amdgpu_vm_move_level_in_lru(struct amdgpu_vm_pt *parent) |
eceb8a15 | 223 | { |
eceb8a15 CK |
224 | unsigned i; |
225 | ||
d711e139 CK |
226 | if (!parent->entries) |
227 | return; | |
eceb8a15 | 228 | |
d711e139 CK |
229 | for (i = 0; i <= parent->last_entry_used; ++i) { |
230 | struct amdgpu_vm_pt *entry = &parent->entries[i]; | |
231 | ||
232 | if (!entry->bo) | |
eceb8a15 CK |
233 | continue; |
234 | ||
d711e139 CK |
235 | ttm_bo_move_to_lru_tail(&entry->bo->tbo); |
236 | amdgpu_vm_move_level_in_lru(entry); | |
eceb8a15 | 237 | } |
d711e139 CK |
238 | } |
239 | ||
240 | /** | |
241 | * amdgpu_vm_move_pt_bos_in_lru - move the PT BOs to the LRU tail | |
242 | * | |
243 | * @adev: amdgpu device instance | |
244 | * @vm: vm providing the BOs | |
245 | * | |
246 | * Move the PT BOs to the tail of the LRU. | |
247 | */ | |
248 | void amdgpu_vm_move_pt_bos_in_lru(struct amdgpu_device *adev, | |
249 | struct amdgpu_vm *vm) | |
250 | { | |
251 | struct ttm_bo_global *glob = adev->mman.bdev.glob; | |
252 | ||
253 | spin_lock(&glob->lru_lock); | |
254 | amdgpu_vm_move_level_in_lru(&vm->root); | |
eceb8a15 | 255 | spin_unlock(&glob->lru_lock); |
d38ceaf9 AD |
256 | } |
257 | ||
f566ceb1 CK |
258 | /** |
259 | * amdgpu_vm_alloc_levels - allocate the PD/PT levels | |
260 | * | |
261 | * @adev: amdgpu_device pointer | |
262 | * @vm: requested vm | |
263 | * @saddr: start of the address range | |
264 | * @eaddr: end of the address range | |
265 | * | |
266 | * Make sure the page directories and page tables are allocated | |
267 | */ | |
268 | static int amdgpu_vm_alloc_levels(struct amdgpu_device *adev, | |
269 | struct amdgpu_vm *vm, | |
270 | struct amdgpu_vm_pt *parent, | |
271 | uint64_t saddr, uint64_t eaddr, | |
272 | unsigned level) | |
273 | { | |
274 | unsigned shift = (adev->vm_manager.num_level - level) * | |
36b32a68 | 275 | adev->vm_manager.block_size; |
f566ceb1 CK |
276 | unsigned pt_idx, from, to; |
277 | int r; | |
278 | ||
279 | if (!parent->entries) { | |
280 | unsigned num_entries = amdgpu_vm_num_entries(adev, level); | |
281 | ||
282 | parent->entries = drm_calloc_large(num_entries, | |
283 | sizeof(struct amdgpu_vm_pt)); | |
284 | if (!parent->entries) | |
285 | return -ENOMEM; | |
286 | memset(parent->entries, 0 , sizeof(struct amdgpu_vm_pt)); | |
287 | } | |
288 | ||
1866bac8 FK |
289 | from = saddr >> shift; |
290 | to = eaddr >> shift; | |
291 | if (from >= amdgpu_vm_num_entries(adev, level) || | |
292 | to >= amdgpu_vm_num_entries(adev, level)) | |
293 | return -EINVAL; | |
f566ceb1 CK |
294 | |
295 | if (to > parent->last_entry_used) | |
296 | parent->last_entry_used = to; | |
297 | ||
298 | ++level; | |
1866bac8 FK |
299 | saddr = saddr & ((1 << shift) - 1); |
300 | eaddr = eaddr & ((1 << shift) - 1); | |
f566ceb1 CK |
301 | |
302 | /* walk over the address space and allocate the page tables */ | |
303 | for (pt_idx = from; pt_idx <= to; ++pt_idx) { | |
304 | struct reservation_object *resv = vm->root.bo->tbo.resv; | |
305 | struct amdgpu_vm_pt *entry = &parent->entries[pt_idx]; | |
306 | struct amdgpu_bo *pt; | |
307 | ||
308 | if (!entry->bo) { | |
309 | r = amdgpu_bo_create(adev, | |
310 | amdgpu_vm_bo_size(adev, level), | |
311 | AMDGPU_GPU_PAGE_SIZE, true, | |
312 | AMDGPU_GEM_DOMAIN_VRAM, | |
313 | AMDGPU_GEM_CREATE_NO_CPU_ACCESS | | |
314 | AMDGPU_GEM_CREATE_SHADOW | | |
315 | AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS | | |
316 | AMDGPU_GEM_CREATE_VRAM_CLEARED, | |
317 | NULL, resv, &pt); | |
318 | if (r) | |
319 | return r; | |
320 | ||
321 | /* Keep a reference to the root directory to avoid | |
322 | * freeing them up in the wrong order. | |
323 | */ | |
324 | pt->parent = amdgpu_bo_ref(vm->root.bo); | |
325 | ||
326 | entry->bo = pt; | |
327 | entry->addr = 0; | |
328 | } | |
329 | ||
330 | if (level < adev->vm_manager.num_level) { | |
1866bac8 FK |
331 | uint64_t sub_saddr = (pt_idx == from) ? saddr : 0; |
332 | uint64_t sub_eaddr = (pt_idx == to) ? eaddr : | |
333 | ((1 << shift) - 1); | |
334 | r = amdgpu_vm_alloc_levels(adev, vm, entry, sub_saddr, | |
335 | sub_eaddr, level); | |
f566ceb1 CK |
336 | if (r) |
337 | return r; | |
338 | } | |
339 | } | |
340 | ||
341 | return 0; | |
342 | } | |
343 | ||
663e4577 CK |
344 | /** |
345 | * amdgpu_vm_alloc_pts - Allocate page tables. | |
346 | * | |
347 | * @adev: amdgpu_device pointer | |
348 | * @vm: VM to allocate page tables for | |
349 | * @saddr: Start address which needs to be allocated | |
350 | * @size: Size from start address we need. | |
351 | * | |
352 | * Make sure the page tables are allocated. | |
353 | */ | |
354 | int amdgpu_vm_alloc_pts(struct amdgpu_device *adev, | |
355 | struct amdgpu_vm *vm, | |
356 | uint64_t saddr, uint64_t size) | |
357 | { | |
22770e5a | 358 | uint64_t last_pfn; |
663e4577 | 359 | uint64_t eaddr; |
663e4577 CK |
360 | |
361 | /* validate the parameters */ | |
362 | if (saddr & AMDGPU_GPU_PAGE_MASK || size & AMDGPU_GPU_PAGE_MASK) | |
363 | return -EINVAL; | |
364 | ||
365 | eaddr = saddr + size - 1; | |
366 | last_pfn = eaddr / AMDGPU_GPU_PAGE_SIZE; | |
367 | if (last_pfn >= adev->vm_manager.max_pfn) { | |
22770e5a | 368 | dev_err(adev->dev, "va above limit (0x%08llX >= 0x%08llX)\n", |
663e4577 CK |
369 | last_pfn, adev->vm_manager.max_pfn); |
370 | return -EINVAL; | |
371 | } | |
372 | ||
373 | saddr /= AMDGPU_GPU_PAGE_SIZE; | |
374 | eaddr /= AMDGPU_GPU_PAGE_SIZE; | |
375 | ||
f566ceb1 | 376 | return amdgpu_vm_alloc_levels(adev, vm, &vm->root, saddr, eaddr, 0); |
663e4577 CK |
377 | } |
378 | ||
641e9400 CK |
379 | /** |
380 | * amdgpu_vm_had_gpu_reset - check if reset occured since last use | |
381 | * | |
382 | * @adev: amdgpu_device pointer | |
383 | * @id: VMID structure | |
384 | * | |
385 | * Check if GPU reset occured since last use of the VMID. | |
386 | */ | |
387 | static bool amdgpu_vm_had_gpu_reset(struct amdgpu_device *adev, | |
388 | struct amdgpu_vm_id *id) | |
192b7dcb CZ |
389 | { |
390 | return id->current_gpu_reset_count != | |
641e9400 | 391 | atomic_read(&adev->gpu_reset_counter); |
192b7dcb CZ |
392 | } |
393 | ||
d38ceaf9 AD |
394 | /** |
395 | * amdgpu_vm_grab_id - allocate the next free VMID | |
396 | * | |
d38ceaf9 | 397 | * @vm: vm to allocate id for |
7f8a5290 CK |
398 | * @ring: ring we want to submit job to |
399 | * @sync: sync object where we add dependencies | |
94dd0a4a | 400 | * @fence: fence protecting ID from reuse |
d38ceaf9 | 401 | * |
7f8a5290 | 402 | * Allocate an id for the vm, adding fences to the sync obj as necessary. |
d38ceaf9 | 403 | */ |
7f8a5290 | 404 | int amdgpu_vm_grab_id(struct amdgpu_vm *vm, struct amdgpu_ring *ring, |
f54d1867 | 405 | struct amdgpu_sync *sync, struct dma_fence *fence, |
fd53be30 | 406 | struct amdgpu_job *job) |
d38ceaf9 | 407 | { |
d38ceaf9 | 408 | struct amdgpu_device *adev = ring->adev; |
2e819849 | 409 | unsigned vmhub = ring->funcs->vmhub; |
7645670d | 410 | struct amdgpu_vm_id_manager *id_mgr = &adev->vm_manager.id_mgr[vmhub]; |
090b767e | 411 | uint64_t fence_context = adev->fence_context + ring->idx; |
f54d1867 | 412 | struct dma_fence *updates = sync->last_vm_update; |
8d76001e | 413 | struct amdgpu_vm_id *id, *idle; |
f54d1867 | 414 | struct dma_fence **fences; |
1fbb2e92 CK |
415 | unsigned i; |
416 | int r = 0; | |
417 | ||
7645670d | 418 | fences = kmalloc_array(sizeof(void *), id_mgr->num_ids, GFP_KERNEL); |
1fbb2e92 CK |
419 | if (!fences) |
420 | return -ENOMEM; | |
d38ceaf9 | 421 | |
7645670d | 422 | mutex_lock(&id_mgr->lock); |
94dd0a4a | 423 | |
36fd7c5c | 424 | /* Check if we have an idle VMID */ |
1fbb2e92 | 425 | i = 0; |
7645670d | 426 | list_for_each_entry(idle, &id_mgr->ids_lru, list) { |
1fbb2e92 CK |
427 | fences[i] = amdgpu_sync_peek_fence(&idle->active, ring); |
428 | if (!fences[i]) | |
36fd7c5c | 429 | break; |
1fbb2e92 | 430 | ++i; |
36fd7c5c CK |
431 | } |
432 | ||
1fbb2e92 | 433 | /* If we can't find a idle VMID to use, wait till one becomes available */ |
7645670d | 434 | if (&idle->list == &id_mgr->ids_lru) { |
1fbb2e92 CK |
435 | u64 fence_context = adev->vm_manager.fence_context + ring->idx; |
436 | unsigned seqno = ++adev->vm_manager.seqno[ring->idx]; | |
f54d1867 | 437 | struct dma_fence_array *array; |
1fbb2e92 CK |
438 | unsigned j; |
439 | ||
440 | for (j = 0; j < i; ++j) | |
f54d1867 | 441 | dma_fence_get(fences[j]); |
1fbb2e92 | 442 | |
f54d1867 | 443 | array = dma_fence_array_create(i, fences, fence_context, |
1fbb2e92 CK |
444 | seqno, true); |
445 | if (!array) { | |
446 | for (j = 0; j < i; ++j) | |
f54d1867 | 447 | dma_fence_put(fences[j]); |
1fbb2e92 CK |
448 | kfree(fences); |
449 | r = -ENOMEM; | |
450 | goto error; | |
451 | } | |
452 | ||
453 | ||
454 | r = amdgpu_sync_fence(ring->adev, sync, &array->base); | |
f54d1867 | 455 | dma_fence_put(&array->base); |
1fbb2e92 CK |
456 | if (r) |
457 | goto error; | |
458 | ||
7645670d | 459 | mutex_unlock(&id_mgr->lock); |
1fbb2e92 CK |
460 | return 0; |
461 | ||
462 | } | |
463 | kfree(fences); | |
464 | ||
87c910d8 | 465 | job->vm_needs_flush = false; |
1fbb2e92 | 466 | /* Check if we can use a VMID already assigned to this VM */ |
7645670d | 467 | list_for_each_entry_reverse(id, &id_mgr->ids_lru, list) { |
f54d1867 | 468 | struct dma_fence *flushed; |
87c910d8 | 469 | bool needs_flush = false; |
1fbb2e92 | 470 | |
1fbb2e92 | 471 | /* Check all the prerequisites to using this VMID */ |
641e9400 | 472 | if (amdgpu_vm_had_gpu_reset(adev, id)) |
6adb0513 | 473 | continue; |
1fbb2e92 CK |
474 | |
475 | if (atomic64_read(&id->owner) != vm->client_id) | |
476 | continue; | |
477 | ||
fd53be30 | 478 | if (job->vm_pd_addr != id->pd_gpu_addr) |
1fbb2e92 CK |
479 | continue; |
480 | ||
87c910d8 CK |
481 | if (!id->last_flush || |
482 | (id->last_flush->context != fence_context && | |
483 | !dma_fence_is_signaled(id->last_flush))) | |
484 | needs_flush = true; | |
1fbb2e92 CK |
485 | |
486 | flushed = id->flushed_updates; | |
87c910d8 CK |
487 | if (updates && (!flushed || dma_fence_is_later(updates, flushed))) |
488 | needs_flush = true; | |
489 | ||
490 | /* Concurrent flushes are only possible starting with Vega10 */ | |
491 | if (adev->asic_type < CHIP_VEGA10 && needs_flush) | |
1fbb2e92 CK |
492 | continue; |
493 | ||
3dab83be CK |
494 | /* Good we can use this VMID. Remember this submission as |
495 | * user of the VMID. | |
496 | */ | |
1fbb2e92 CK |
497 | r = amdgpu_sync_fence(ring->adev, &id->active, fence); |
498 | if (r) | |
499 | goto error; | |
8d76001e | 500 | |
87c910d8 CK |
501 | if (updates && (!flushed || dma_fence_is_later(updates, flushed))) { |
502 | dma_fence_put(id->flushed_updates); | |
503 | id->flushed_updates = dma_fence_get(updates); | |
504 | } | |
8d76001e | 505 | |
87c910d8 CK |
506 | if (needs_flush) |
507 | goto needs_flush; | |
508 | else | |
509 | goto no_flush_needed; | |
8d76001e | 510 | |
4f618e73 | 511 | }; |
8d76001e | 512 | |
1fbb2e92 CK |
513 | /* Still no ID to use? Then use the idle one found earlier */ |
514 | id = idle; | |
8e9fbeb5 | 515 | |
1fbb2e92 CK |
516 | /* Remember this submission as user of the VMID */ |
517 | r = amdgpu_sync_fence(ring->adev, &id->active, fence); | |
832a902f CK |
518 | if (r) |
519 | goto error; | |
94dd0a4a | 520 | |
87c910d8 | 521 | id->pd_gpu_addr = job->vm_pd_addr; |
f54d1867 CW |
522 | dma_fence_put(id->flushed_updates); |
523 | id->flushed_updates = dma_fence_get(updates); | |
b46b8a87 | 524 | id->current_gpu_reset_count = atomic_read(&adev->gpu_reset_counter); |
0ea54b9b | 525 | atomic64_set(&id->owner, vm->client_id); |
d38ceaf9 | 526 | |
87c910d8 CK |
527 | needs_flush: |
528 | job->vm_needs_flush = true; | |
529 | dma_fence_put(id->last_flush); | |
530 | id->last_flush = NULL; | |
531 | ||
532 | no_flush_needed: | |
533 | list_move_tail(&id->list, &id_mgr->ids_lru); | |
534 | ||
7645670d | 535 | job->vm_id = id - id_mgr->ids; |
c5296d14 | 536 | trace_amdgpu_vm_grab_id(vm, ring, job); |
832a902f CK |
537 | |
538 | error: | |
7645670d | 539 | mutex_unlock(&id_mgr->lock); |
a9a78b32 | 540 | return r; |
d38ceaf9 AD |
541 | } |
542 | ||
1e9ef26f CZ |
543 | static void amdgpu_vm_free_reserved_vmid(struct amdgpu_device *adev, |
544 | struct amdgpu_vm *vm, | |
545 | unsigned vmhub) | |
546 | { | |
547 | struct amdgpu_vm_id_manager *id_mgr = &adev->vm_manager.id_mgr[vmhub]; | |
548 | ||
549 | mutex_lock(&id_mgr->lock); | |
550 | if (vm->reserved_vmid[vmhub]) { | |
551 | list_add(&vm->reserved_vmid[vmhub]->list, | |
552 | &id_mgr->ids_lru); | |
553 | vm->reserved_vmid[vmhub] = NULL; | |
c3505770 | 554 | atomic_dec(&id_mgr->reserved_vmid_num); |
1e9ef26f CZ |
555 | } |
556 | mutex_unlock(&id_mgr->lock); | |
557 | } | |
558 | ||
559 | static int amdgpu_vm_alloc_reserved_vmid(struct amdgpu_device *adev, | |
560 | struct amdgpu_vm *vm, | |
561 | unsigned vmhub) | |
562 | { | |
563 | struct amdgpu_vm_id_manager *id_mgr; | |
564 | struct amdgpu_vm_id *idle; | |
565 | int r = 0; | |
566 | ||
567 | id_mgr = &adev->vm_manager.id_mgr[vmhub]; | |
568 | mutex_lock(&id_mgr->lock); | |
569 | if (vm->reserved_vmid[vmhub]) | |
570 | goto unlock; | |
c3505770 CZ |
571 | if (atomic_inc_return(&id_mgr->reserved_vmid_num) > |
572 | AMDGPU_VM_MAX_RESERVED_VMID) { | |
573 | DRM_ERROR("Over limitation of reserved vmid\n"); | |
574 | atomic_dec(&id_mgr->reserved_vmid_num); | |
575 | r = -EINVAL; | |
576 | goto unlock; | |
577 | } | |
1e9ef26f CZ |
578 | /* Select the first entry VMID */ |
579 | idle = list_first_entry(&id_mgr->ids_lru, struct amdgpu_vm_id, list); | |
580 | list_del_init(&idle->list); | |
581 | vm->reserved_vmid[vmhub] = idle; | |
582 | mutex_unlock(&id_mgr->lock); | |
583 | ||
584 | return 0; | |
585 | unlock: | |
586 | mutex_unlock(&id_mgr->lock); | |
587 | return r; | |
588 | } | |
589 | ||
93dcc37d AD |
590 | static bool amdgpu_vm_ring_has_compute_vm_bug(struct amdgpu_ring *ring) |
591 | { | |
592 | struct amdgpu_device *adev = ring->adev; | |
a1255107 | 593 | const struct amdgpu_ip_block *ip_block; |
93dcc37d | 594 | |
21cd942e | 595 | if (ring->funcs->type != AMDGPU_RING_TYPE_COMPUTE) |
93dcc37d AD |
596 | /* only compute rings */ |
597 | return false; | |
598 | ||
599 | ip_block = amdgpu_get_ip_block(adev, AMD_IP_BLOCK_TYPE_GFX); | |
600 | if (!ip_block) | |
601 | return false; | |
602 | ||
a1255107 | 603 | if (ip_block->version->major <= 7) { |
93dcc37d AD |
604 | /* gfx7 has no workaround */ |
605 | return true; | |
a1255107 | 606 | } else if (ip_block->version->major == 8) { |
93dcc37d AD |
607 | if (adev->gfx.mec_fw_version >= 673) |
608 | /* gfx8 is fixed in MEC firmware 673 */ | |
609 | return false; | |
610 | else | |
611 | return true; | |
612 | } | |
613 | return false; | |
614 | } | |
615 | ||
e60f8db5 AX |
616 | static u64 amdgpu_vm_adjust_mc_addr(struct amdgpu_device *adev, u64 mc_addr) |
617 | { | |
618 | u64 addr = mc_addr; | |
619 | ||
f75e237c CK |
620 | if (adev->gart.gart_funcs->adjust_mc_addr) |
621 | addr = adev->gart.gart_funcs->adjust_mc_addr(adev, addr); | |
e60f8db5 AX |
622 | |
623 | return addr; | |
624 | } | |
625 | ||
d38ceaf9 AD |
626 | /** |
627 | * amdgpu_vm_flush - hardware flush the vm | |
628 | * | |
629 | * @ring: ring to use for flush | |
cffadc83 | 630 | * @vm_id: vmid number to use |
4ff37a83 | 631 | * @pd_addr: address of the page directory |
d38ceaf9 | 632 | * |
4ff37a83 | 633 | * Emit a VM flush when it is necessary. |
d38ceaf9 | 634 | */ |
fd53be30 | 635 | int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job) |
d38ceaf9 | 636 | { |
971fe9a9 | 637 | struct amdgpu_device *adev = ring->adev; |
7645670d CK |
638 | unsigned vmhub = ring->funcs->vmhub; |
639 | struct amdgpu_vm_id_manager *id_mgr = &adev->vm_manager.id_mgr[vmhub]; | |
640 | struct amdgpu_vm_id *id = &id_mgr->ids[job->vm_id]; | |
d564a06e | 641 | bool gds_switch_needed = ring->funcs->emit_gds_switch && ( |
fd53be30 CZ |
642 | id->gds_base != job->gds_base || |
643 | id->gds_size != job->gds_size || | |
644 | id->gws_base != job->gws_base || | |
645 | id->gws_size != job->gws_size || | |
646 | id->oa_base != job->oa_base || | |
647 | id->oa_size != job->oa_size); | |
f7d015b9 CK |
648 | bool vm_flush_needed = job->vm_needs_flush || |
649 | amdgpu_vm_ring_has_compute_vm_bug(ring); | |
c0e51931 | 650 | unsigned patch_offset = 0; |
41d9eb2c | 651 | int r; |
d564a06e | 652 | |
f7d015b9 CK |
653 | if (amdgpu_vm_had_gpu_reset(adev, id)) { |
654 | gds_switch_needed = true; | |
655 | vm_flush_needed = true; | |
656 | } | |
971fe9a9 | 657 | |
f7d015b9 CK |
658 | if (!vm_flush_needed && !gds_switch_needed) |
659 | return 0; | |
41d9eb2c | 660 | |
c0e51931 CK |
661 | if (ring->funcs->init_cond_exec) |
662 | patch_offset = amdgpu_ring_init_cond_exec(ring); | |
41d9eb2c | 663 | |
30514dec | 664 | if (ring->funcs->emit_pipeline_sync && !job->need_pipeline_sync) |
c0e51931 | 665 | amdgpu_ring_emit_pipeline_sync(ring); |
3dab83be | 666 | |
f7d015b9 | 667 | if (ring->funcs->emit_vm_flush && vm_flush_needed) { |
c0e51931 CK |
668 | u64 pd_addr = amdgpu_vm_adjust_mc_addr(adev, job->vm_pd_addr); |
669 | struct dma_fence *fence; | |
41d9eb2c | 670 | |
5f1bcf51 | 671 | trace_amdgpu_vm_flush(ring, job->vm_id, pd_addr); |
c0e51931 | 672 | amdgpu_ring_emit_vm_flush(ring, job->vm_id, pd_addr); |
e9d672b2 | 673 | |
c0e51931 CK |
674 | r = amdgpu_fence_emit(ring, &fence); |
675 | if (r) | |
676 | return r; | |
e9d672b2 | 677 | |
7645670d | 678 | mutex_lock(&id_mgr->lock); |
c0e51931 CK |
679 | dma_fence_put(id->last_flush); |
680 | id->last_flush = fence; | |
7645670d | 681 | mutex_unlock(&id_mgr->lock); |
c0e51931 | 682 | } |
e9d672b2 | 683 | |
ca7962d8 | 684 | if (ring->funcs->emit_gds_switch && gds_switch_needed) { |
c0e51931 CK |
685 | id->gds_base = job->gds_base; |
686 | id->gds_size = job->gds_size; | |
687 | id->gws_base = job->gws_base; | |
688 | id->gws_size = job->gws_size; | |
689 | id->oa_base = job->oa_base; | |
690 | id->oa_size = job->oa_size; | |
691 | amdgpu_ring_emit_gds_switch(ring, job->vm_id, job->gds_base, | |
692 | job->gds_size, job->gws_base, | |
693 | job->gws_size, job->oa_base, | |
694 | job->oa_size); | |
695 | } | |
696 | ||
697 | if (ring->funcs->patch_cond_exec) | |
698 | amdgpu_ring_patch_cond_exec(ring, patch_offset); | |
699 | ||
700 | /* the double SWITCH_BUFFER here *cannot* be skipped by COND_EXEC */ | |
701 | if (ring->funcs->emit_switch_buffer) { | |
702 | amdgpu_ring_emit_switch_buffer(ring); | |
703 | amdgpu_ring_emit_switch_buffer(ring); | |
e9d672b2 | 704 | } |
41d9eb2c | 705 | return 0; |
971fe9a9 CK |
706 | } |
707 | ||
708 | /** | |
709 | * amdgpu_vm_reset_id - reset VMID to zero | |
710 | * | |
711 | * @adev: amdgpu device structure | |
712 | * @vm_id: vmid number to use | |
713 | * | |
714 | * Reset saved GDW, GWS and OA to force switch on next flush. | |
715 | */ | |
7645670d CK |
716 | void amdgpu_vm_reset_id(struct amdgpu_device *adev, unsigned vmhub, |
717 | unsigned vmid) | |
971fe9a9 | 718 | { |
7645670d CK |
719 | struct amdgpu_vm_id_manager *id_mgr = &adev->vm_manager.id_mgr[vmhub]; |
720 | struct amdgpu_vm_id *id = &id_mgr->ids[vmid]; | |
bcb1ba35 | 721 | |
32601d48 | 722 | atomic64_set(&id->owner, 0); |
bcb1ba35 CK |
723 | id->gds_base = 0; |
724 | id->gds_size = 0; | |
725 | id->gws_base = 0; | |
726 | id->gws_size = 0; | |
727 | id->oa_base = 0; | |
728 | id->oa_size = 0; | |
d38ceaf9 AD |
729 | } |
730 | ||
32601d48 CK |
731 | /** |
732 | * amdgpu_vm_reset_all_id - reset VMID to zero | |
733 | * | |
734 | * @adev: amdgpu device structure | |
735 | * | |
736 | * Reset VMID to force flush on next use | |
737 | */ | |
738 | void amdgpu_vm_reset_all_ids(struct amdgpu_device *adev) | |
739 | { | |
740 | unsigned i, j; | |
741 | ||
742 | for (i = 0; i < AMDGPU_MAX_VMHUBS; ++i) { | |
743 | struct amdgpu_vm_id_manager *id_mgr = | |
744 | &adev->vm_manager.id_mgr[i]; | |
745 | ||
746 | for (j = 1; j < id_mgr->num_ids; ++j) | |
747 | amdgpu_vm_reset_id(adev, i, j); | |
748 | } | |
749 | } | |
750 | ||
d38ceaf9 AD |
751 | /** |
752 | * amdgpu_vm_bo_find - find the bo_va for a specific vm & bo | |
753 | * | |
754 | * @vm: requested vm | |
755 | * @bo: requested buffer object | |
756 | * | |
8843dbbb | 757 | * Find @bo inside the requested vm. |
d38ceaf9 AD |
758 | * Search inside the @bos vm list for the requested vm |
759 | * Returns the found bo_va or NULL if none is found | |
760 | * | |
761 | * Object has to be reserved! | |
762 | */ | |
763 | struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm, | |
764 | struct amdgpu_bo *bo) | |
765 | { | |
766 | struct amdgpu_bo_va *bo_va; | |
767 | ||
768 | list_for_each_entry(bo_va, &bo->va, bo_list) { | |
769 | if (bo_va->vm == vm) { | |
770 | return bo_va; | |
771 | } | |
772 | } | |
773 | return NULL; | |
774 | } | |
775 | ||
776 | /** | |
afef8b8f | 777 | * amdgpu_vm_do_set_ptes - helper to call the right asic function |
d38ceaf9 | 778 | * |
29efc4f5 | 779 | * @params: see amdgpu_pte_update_params definition |
d38ceaf9 AD |
780 | * @pe: addr of the page entry |
781 | * @addr: dst addr to write into pe | |
782 | * @count: number of page entries to update | |
783 | * @incr: increase next addr by incr bytes | |
784 | * @flags: hw access flags | |
d38ceaf9 AD |
785 | * |
786 | * Traces the parameters and calls the right asic functions | |
787 | * to setup the page table using the DMA. | |
788 | */ | |
afef8b8f CK |
789 | static void amdgpu_vm_do_set_ptes(struct amdgpu_pte_update_params *params, |
790 | uint64_t pe, uint64_t addr, | |
791 | unsigned count, uint32_t incr, | |
6b777607 | 792 | uint64_t flags) |
d38ceaf9 | 793 | { |
ec2f05f0 | 794 | trace_amdgpu_vm_set_ptes(pe, addr, count, incr, flags); |
d38ceaf9 | 795 | |
afef8b8f | 796 | if (count < 3) { |
de9ea7bd CK |
797 | amdgpu_vm_write_pte(params->adev, params->ib, pe, |
798 | addr | flags, count, incr); | |
d38ceaf9 AD |
799 | |
800 | } else { | |
27c5f36f | 801 | amdgpu_vm_set_pte_pde(params->adev, params->ib, pe, addr, |
d38ceaf9 AD |
802 | count, incr, flags); |
803 | } | |
804 | } | |
805 | ||
afef8b8f CK |
806 | /** |
807 | * amdgpu_vm_do_copy_ptes - copy the PTEs from the GART | |
808 | * | |
809 | * @params: see amdgpu_pte_update_params definition | |
810 | * @pe: addr of the page entry | |
811 | * @addr: dst addr to write into pe | |
812 | * @count: number of page entries to update | |
813 | * @incr: increase next addr by incr bytes | |
814 | * @flags: hw access flags | |
815 | * | |
816 | * Traces the parameters and calls the DMA function to copy the PTEs. | |
817 | */ | |
818 | static void amdgpu_vm_do_copy_ptes(struct amdgpu_pte_update_params *params, | |
819 | uint64_t pe, uint64_t addr, | |
820 | unsigned count, uint32_t incr, | |
6b777607 | 821 | uint64_t flags) |
afef8b8f | 822 | { |
ec2f05f0 | 823 | uint64_t src = (params->src + (addr >> 12) * 8); |
afef8b8f | 824 | |
ec2f05f0 CK |
825 | |
826 | trace_amdgpu_vm_copy_ptes(pe, src, count); | |
827 | ||
828 | amdgpu_vm_copy_pte(params->adev, params->ib, pe, src, count); | |
afef8b8f CK |
829 | } |
830 | ||
d38ceaf9 | 831 | /** |
b07c9d2a | 832 | * amdgpu_vm_map_gart - Resolve gart mapping of addr |
d38ceaf9 | 833 | * |
b07c9d2a | 834 | * @pages_addr: optional DMA address to use for lookup |
d38ceaf9 AD |
835 | * @addr: the unmapped addr |
836 | * | |
837 | * Look up the physical address of the page that the pte resolves | |
b07c9d2a | 838 | * to and return the pointer for the page table entry. |
d38ceaf9 | 839 | */ |
de9ea7bd | 840 | static uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t addr) |
d38ceaf9 AD |
841 | { |
842 | uint64_t result; | |
843 | ||
de9ea7bd CK |
844 | /* page table offset */ |
845 | result = pages_addr[addr >> PAGE_SHIFT]; | |
b07c9d2a | 846 | |
de9ea7bd CK |
847 | /* in case cpu page size != gpu page size*/ |
848 | result |= addr & (~PAGE_MASK); | |
d38ceaf9 | 849 | |
b07c9d2a | 850 | result &= 0xFFFFFFFFFFFFF000ULL; |
d38ceaf9 AD |
851 | |
852 | return result; | |
853 | } | |
854 | ||
f8991bab | 855 | /* |
194d2161 | 856 | * amdgpu_vm_update_level - update a single level in the hierarchy |
f8991bab CK |
857 | * |
858 | * @adev: amdgpu_device pointer | |
859 | * @vm: requested vm | |
194d2161 | 860 | * @parent: parent directory |
f8991bab | 861 | * |
194d2161 | 862 | * Makes sure all entries in @parent are up to date. |
f8991bab CK |
863 | * Returns 0 for success, error for failure. |
864 | */ | |
194d2161 CK |
865 | static int amdgpu_vm_update_level(struct amdgpu_device *adev, |
866 | struct amdgpu_vm *vm, | |
867 | struct amdgpu_vm_pt *parent, | |
868 | unsigned level) | |
d38ceaf9 | 869 | { |
f8991bab | 870 | struct amdgpu_bo *shadow; |
2d55e45a | 871 | struct amdgpu_ring *ring; |
f8991bab | 872 | uint64_t pd_addr, shadow_addr; |
194d2161 | 873 | uint32_t incr = amdgpu_vm_bo_size(adev, level + 1); |
f8991bab | 874 | uint64_t last_pde = ~0, last_pt = ~0, last_shadow = ~0; |
d38ceaf9 | 875 | unsigned count = 0, pt_idx, ndw; |
d71518b5 | 876 | struct amdgpu_job *job; |
29efc4f5 | 877 | struct amdgpu_pte_update_params params; |
f54d1867 | 878 | struct dma_fence *fence = NULL; |
d5fc5e82 | 879 | |
d38ceaf9 AD |
880 | int r; |
881 | ||
194d2161 CK |
882 | if (!parent->entries) |
883 | return 0; | |
2d55e45a CK |
884 | ring = container_of(vm->entity.sched, struct amdgpu_ring, sched); |
885 | ||
d38ceaf9 AD |
886 | /* padding, etc. */ |
887 | ndw = 64; | |
888 | ||
889 | /* assume the worst case */ | |
194d2161 | 890 | ndw += parent->last_entry_used * 6; |
d38ceaf9 | 891 | |
194d2161 CK |
892 | pd_addr = amdgpu_bo_gpu_offset(parent->bo); |
893 | ||
894 | shadow = parent->bo->shadow; | |
f8991bab CK |
895 | if (shadow) { |
896 | r = amdgpu_ttm_bind(&shadow->tbo, &shadow->tbo.mem); | |
897 | if (r) | |
898 | return r; | |
899 | shadow_addr = amdgpu_bo_gpu_offset(shadow); | |
900 | ndw *= 2; | |
901 | } else { | |
902 | shadow_addr = 0; | |
903 | } | |
904 | ||
d71518b5 CK |
905 | r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job); |
906 | if (r) | |
d38ceaf9 | 907 | return r; |
d71518b5 | 908 | |
27c5f36f CK |
909 | memset(¶ms, 0, sizeof(params)); |
910 | params.adev = adev; | |
29efc4f5 | 911 | params.ib = &job->ibs[0]; |
d38ceaf9 | 912 | |
194d2161 CK |
913 | /* walk over the address space and update the directory */ |
914 | for (pt_idx = 0; pt_idx <= parent->last_entry_used; ++pt_idx) { | |
915 | struct amdgpu_bo *bo = parent->entries[pt_idx].bo; | |
d38ceaf9 AD |
916 | uint64_t pde, pt; |
917 | ||
918 | if (bo == NULL) | |
919 | continue; | |
920 | ||
0fc8683e | 921 | if (bo->shadow) { |
f8991bab | 922 | struct amdgpu_bo *pt_shadow = bo->shadow; |
0fc8683e | 923 | |
f8991bab CK |
924 | r = amdgpu_ttm_bind(&pt_shadow->tbo, |
925 | &pt_shadow->tbo.mem); | |
0fc8683e CK |
926 | if (r) |
927 | return r; | |
928 | } | |
929 | ||
d38ceaf9 | 930 | pt = amdgpu_bo_gpu_offset(bo); |
194d2161 | 931 | if (parent->entries[pt_idx].addr == pt) |
f8991bab CK |
932 | continue; |
933 | ||
194d2161 | 934 | parent->entries[pt_idx].addr = pt; |
d38ceaf9 AD |
935 | |
936 | pde = pd_addr + pt_idx * 8; | |
937 | if (((last_pde + 8 * count) != pde) || | |
96105e53 CK |
938 | ((last_pt + incr * count) != pt) || |
939 | (count == AMDGPU_VM_MAX_UPDATE_SIZE)) { | |
d38ceaf9 AD |
940 | |
941 | if (count) { | |
e60f8db5 AX |
942 | uint64_t pt_addr = |
943 | amdgpu_vm_adjust_mc_addr(adev, last_pt); | |
944 | ||
f8991bab CK |
945 | if (shadow) |
946 | amdgpu_vm_do_set_ptes(¶ms, | |
947 | last_shadow, | |
e60f8db5 | 948 | pt_addr, count, |
f8991bab CK |
949 | incr, |
950 | AMDGPU_PTE_VALID); | |
951 | ||
afef8b8f | 952 | amdgpu_vm_do_set_ptes(¶ms, last_pde, |
e60f8db5 | 953 | pt_addr, count, incr, |
afef8b8f | 954 | AMDGPU_PTE_VALID); |
d38ceaf9 AD |
955 | } |
956 | ||
957 | count = 1; | |
958 | last_pde = pde; | |
f8991bab | 959 | last_shadow = shadow_addr + pt_idx * 8; |
d38ceaf9 AD |
960 | last_pt = pt; |
961 | } else { | |
962 | ++count; | |
963 | } | |
964 | } | |
965 | ||
f8991bab | 966 | if (count) { |
e60f8db5 AX |
967 | uint64_t pt_addr = amdgpu_vm_adjust_mc_addr(adev, last_pt); |
968 | ||
67003a15 | 969 | if (vm->root.bo->shadow) |
e60f8db5 | 970 | amdgpu_vm_do_set_ptes(¶ms, last_shadow, pt_addr, |
f8991bab CK |
971 | count, incr, AMDGPU_PTE_VALID); |
972 | ||
e60f8db5 | 973 | amdgpu_vm_do_set_ptes(¶ms, last_pde, pt_addr, |
afef8b8f | 974 | count, incr, AMDGPU_PTE_VALID); |
f8991bab | 975 | } |
d38ceaf9 | 976 | |
f8991bab CK |
977 | if (params.ib->length_dw == 0) { |
978 | amdgpu_job_free(job); | |
194d2161 CK |
979 | } else { |
980 | amdgpu_ring_pad_ib(ring, params.ib); | |
981 | amdgpu_sync_resv(adev, &job->sync, parent->bo->tbo.resv, | |
e86f9cee | 982 | AMDGPU_FENCE_OWNER_VM); |
194d2161 CK |
983 | if (shadow) |
984 | amdgpu_sync_resv(adev, &job->sync, shadow->tbo.resv, | |
985 | AMDGPU_FENCE_OWNER_VM); | |
05906dec | 986 | |
194d2161 CK |
987 | WARN_ON(params.ib->length_dw > ndw); |
988 | r = amdgpu_job_submit(job, ring, &vm->entity, | |
989 | AMDGPU_FENCE_OWNER_VM, &fence); | |
990 | if (r) | |
991 | goto error_free; | |
992 | ||
993 | amdgpu_bo_fence(parent->bo, fence, true); | |
994 | dma_fence_put(vm->last_dir_update); | |
995 | vm->last_dir_update = dma_fence_get(fence); | |
996 | dma_fence_put(fence); | |
997 | } | |
998 | /* | |
999 | * Recurse into the subdirectories. This recursion is harmless because | |
1000 | * we only have a maximum of 5 layers. | |
1001 | */ | |
1002 | for (pt_idx = 0; pt_idx <= parent->last_entry_used; ++pt_idx) { | |
1003 | struct amdgpu_vm_pt *entry = &parent->entries[pt_idx]; | |
1004 | ||
1005 | if (!entry->bo) | |
1006 | continue; | |
d5fc5e82 | 1007 | |
194d2161 CK |
1008 | r = amdgpu_vm_update_level(adev, vm, entry, level + 1); |
1009 | if (r) | |
1010 | return r; | |
1011 | } | |
d38ceaf9 AD |
1012 | |
1013 | return 0; | |
d5fc5e82 CZ |
1014 | |
1015 | error_free: | |
d71518b5 | 1016 | amdgpu_job_free(job); |
4af9f07c | 1017 | return r; |
d38ceaf9 AD |
1018 | } |
1019 | ||
194d2161 CK |
1020 | /* |
1021 | * amdgpu_vm_update_directories - make sure that all directories are valid | |
1022 | * | |
1023 | * @adev: amdgpu_device pointer | |
1024 | * @vm: requested vm | |
1025 | * | |
1026 | * Makes sure all directories are up to date. | |
1027 | * Returns 0 for success, error for failure. | |
1028 | */ | |
1029 | int amdgpu_vm_update_directories(struct amdgpu_device *adev, | |
1030 | struct amdgpu_vm *vm) | |
1031 | { | |
1032 | return amdgpu_vm_update_level(adev, vm, &vm->root, 0); | |
1033 | } | |
1034 | ||
4e2cb640 CK |
1035 | /** |
1036 | * amdgpu_vm_find_pt - find the page table for an address | |
1037 | * | |
1038 | * @p: see amdgpu_pte_update_params definition | |
1039 | * @addr: virtual address in question | |
1040 | * | |
1041 | * Find the page table BO for a virtual address, return NULL when none found. | |
1042 | */ | |
1043 | static struct amdgpu_bo *amdgpu_vm_get_pt(struct amdgpu_pte_update_params *p, | |
1044 | uint64_t addr) | |
1045 | { | |
1046 | struct amdgpu_vm_pt *entry = &p->vm->root; | |
1047 | unsigned idx, level = p->adev->vm_manager.num_level; | |
1048 | ||
1049 | while (entry->entries) { | |
36b32a68 | 1050 | idx = addr >> (p->adev->vm_manager.block_size * level--); |
4e2cb640 CK |
1051 | idx %= amdgpu_bo_size(entry->bo) / 8; |
1052 | entry = &entry->entries[idx]; | |
1053 | } | |
1054 | ||
1055 | if (level) | |
1056 | return NULL; | |
1057 | ||
1058 | return entry->bo; | |
1059 | } | |
1060 | ||
d38ceaf9 AD |
1061 | /** |
1062 | * amdgpu_vm_update_ptes - make sure that page tables are valid | |
1063 | * | |
29efc4f5 | 1064 | * @params: see amdgpu_pte_update_params definition |
d38ceaf9 AD |
1065 | * @vm: requested vm |
1066 | * @start: start of GPU address range | |
1067 | * @end: end of GPU address range | |
677131a1 | 1068 | * @dst: destination address to map to, the next dst inside the function |
d38ceaf9 AD |
1069 | * @flags: mapping flags |
1070 | * | |
8843dbbb | 1071 | * Update the page tables in the range @start - @end. |
d38ceaf9 | 1072 | */ |
27c5f36f | 1073 | static void amdgpu_vm_update_ptes(struct amdgpu_pte_update_params *params, |
a1e08d3b | 1074 | uint64_t start, uint64_t end, |
6b777607 | 1075 | uint64_t dst, uint64_t flags) |
d38ceaf9 | 1076 | { |
36b32a68 ZJ |
1077 | struct amdgpu_device *adev = params->adev; |
1078 | const uint64_t mask = AMDGPU_VM_PTE_COUNT(adev) - 1; | |
31f6c1fe | 1079 | |
92696dd5 | 1080 | uint64_t cur_pe_start, cur_nptes, cur_dst; |
677131a1 | 1081 | uint64_t addr; /* next GPU address to be updated */ |
21718497 AX |
1082 | struct amdgpu_bo *pt; |
1083 | unsigned nptes; /* next number of ptes to be updated */ | |
1084 | uint64_t next_pe_start; | |
1085 | ||
1086 | /* initialize the variables */ | |
1087 | addr = start; | |
4e2cb640 | 1088 | pt = amdgpu_vm_get_pt(params, addr); |
1866bac8 FK |
1089 | if (!pt) { |
1090 | pr_err("PT not found, aborting update_ptes\n"); | |
4e2cb640 | 1091 | return; |
1866bac8 | 1092 | } |
4e2cb640 | 1093 | |
4c7e8855 CZ |
1094 | if (params->shadow) { |
1095 | if (!pt->shadow) | |
1096 | return; | |
914b4dce | 1097 | pt = pt->shadow; |
4c7e8855 | 1098 | } |
21718497 AX |
1099 | if ((addr & ~mask) == (end & ~mask)) |
1100 | nptes = end - addr; | |
1101 | else | |
36b32a68 | 1102 | nptes = AMDGPU_VM_PTE_COUNT(adev) - (addr & mask); |
21718497 AX |
1103 | |
1104 | cur_pe_start = amdgpu_bo_gpu_offset(pt); | |
1105 | cur_pe_start += (addr & mask) * 8; | |
92696dd5 | 1106 | cur_nptes = nptes; |
21718497 AX |
1107 | cur_dst = dst; |
1108 | ||
1109 | /* for next ptb*/ | |
1110 | addr += nptes; | |
1111 | dst += nptes * AMDGPU_GPU_PAGE_SIZE; | |
d38ceaf9 AD |
1112 | |
1113 | /* walk over the address space and update the page tables */ | |
21718497 | 1114 | while (addr < end) { |
4e2cb640 | 1115 | pt = amdgpu_vm_get_pt(params, addr); |
1866bac8 FK |
1116 | if (!pt) { |
1117 | pr_err("PT not found, aborting update_ptes\n"); | |
4e2cb640 | 1118 | return; |
1866bac8 | 1119 | } |
4e2cb640 | 1120 | |
4c7e8855 CZ |
1121 | if (params->shadow) { |
1122 | if (!pt->shadow) | |
1123 | return; | |
914b4dce | 1124 | pt = pt->shadow; |
4c7e8855 | 1125 | } |
d38ceaf9 AD |
1126 | |
1127 | if ((addr & ~mask) == (end & ~mask)) | |
1128 | nptes = end - addr; | |
1129 | else | |
36b32a68 | 1130 | nptes = AMDGPU_VM_PTE_COUNT(adev) - (addr & mask); |
d38ceaf9 | 1131 | |
677131a1 AX |
1132 | next_pe_start = amdgpu_bo_gpu_offset(pt); |
1133 | next_pe_start += (addr & mask) * 8; | |
d38ceaf9 | 1134 | |
96105e53 CK |
1135 | if ((cur_pe_start + 8 * cur_nptes) == next_pe_start && |
1136 | ((cur_nptes + nptes) <= AMDGPU_VM_MAX_UPDATE_SIZE)) { | |
3a6f8e0c | 1137 | /* The next ptb is consecutive to current ptb. |
afef8b8f | 1138 | * Don't call the update function now. |
3a6f8e0c AX |
1139 | * Will update two ptbs together in future. |
1140 | */ | |
92696dd5 | 1141 | cur_nptes += nptes; |
3a6f8e0c | 1142 | } else { |
afef8b8f CK |
1143 | params->func(params, cur_pe_start, cur_dst, cur_nptes, |
1144 | AMDGPU_GPU_PAGE_SIZE, flags); | |
d38ceaf9 | 1145 | |
677131a1 | 1146 | cur_pe_start = next_pe_start; |
92696dd5 | 1147 | cur_nptes = nptes; |
677131a1 | 1148 | cur_dst = dst; |
d38ceaf9 AD |
1149 | } |
1150 | ||
21718497 | 1151 | /* for next ptb*/ |
d38ceaf9 AD |
1152 | addr += nptes; |
1153 | dst += nptes * AMDGPU_GPU_PAGE_SIZE; | |
1154 | } | |
1155 | ||
afef8b8f CK |
1156 | params->func(params, cur_pe_start, cur_dst, cur_nptes, |
1157 | AMDGPU_GPU_PAGE_SIZE, flags); | |
92696dd5 CK |
1158 | } |
1159 | ||
1160 | /* | |
1161 | * amdgpu_vm_frag_ptes - add fragment information to PTEs | |
1162 | * | |
1163 | * @params: see amdgpu_pte_update_params definition | |
1164 | * @vm: requested vm | |
1165 | * @start: first PTE to handle | |
1166 | * @end: last PTE to handle | |
1167 | * @dst: addr those PTEs should point to | |
1168 | * @flags: hw mapping flags | |
1169 | */ | |
1170 | static void amdgpu_vm_frag_ptes(struct amdgpu_pte_update_params *params, | |
92696dd5 | 1171 | uint64_t start, uint64_t end, |
6b777607 | 1172 | uint64_t dst, uint64_t flags) |
92696dd5 CK |
1173 | { |
1174 | /** | |
1175 | * The MC L1 TLB supports variable sized pages, based on a fragment | |
1176 | * field in the PTE. When this field is set to a non-zero value, page | |
1177 | * granularity is increased from 4KB to (1 << (12 + frag)). The PTE | |
1178 | * flags are considered valid for all PTEs within the fragment range | |
1179 | * and corresponding mappings are assumed to be physically contiguous. | |
1180 | * | |
1181 | * The L1 TLB can store a single PTE for the whole fragment, | |
1182 | * significantly increasing the space available for translation | |
1183 | * caching. This leads to large improvements in throughput when the | |
1184 | * TLB is under pressure. | |
1185 | * | |
1186 | * The L2 TLB distributes small and large fragments into two | |
1187 | * asymmetric partitions. The large fragment cache is significantly | |
1188 | * larger. Thus, we try to use large fragments wherever possible. | |
1189 | * Userspace can support this by aligning virtual base address and | |
1190 | * allocation size to the fragment size. | |
1191 | */ | |
1192 | ||
8036617e CK |
1193 | /* SI and newer are optimized for 64KB */ |
1194 | uint64_t frag_flags = AMDGPU_PTE_FRAG(AMDGPU_LOG2_PAGES_PER_FRAG); | |
1195 | uint64_t frag_align = 1 << AMDGPU_LOG2_PAGES_PER_FRAG; | |
92696dd5 CK |
1196 | |
1197 | uint64_t frag_start = ALIGN(start, frag_align); | |
1198 | uint64_t frag_end = end & ~(frag_align - 1); | |
1199 | ||
1200 | /* system pages are non continuously */ | |
b7fc2cbd | 1201 | if (params->src || !(flags & AMDGPU_PTE_VALID) || |
92696dd5 CK |
1202 | (frag_start >= frag_end)) { |
1203 | ||
49ac8a24 | 1204 | amdgpu_vm_update_ptes(params, start, end, dst, flags); |
92696dd5 CK |
1205 | return; |
1206 | } | |
1207 | ||
1208 | /* handle the 4K area at the beginning */ | |
1209 | if (start != frag_start) { | |
49ac8a24 | 1210 | amdgpu_vm_update_ptes(params, start, frag_start, |
92696dd5 CK |
1211 | dst, flags); |
1212 | dst += (frag_start - start) * AMDGPU_GPU_PAGE_SIZE; | |
1213 | } | |
1214 | ||
1215 | /* handle the area in the middle */ | |
49ac8a24 | 1216 | amdgpu_vm_update_ptes(params, frag_start, frag_end, dst, |
8036617e | 1217 | flags | frag_flags); |
92696dd5 CK |
1218 | |
1219 | /* handle the 4K area at the end */ | |
1220 | if (frag_end != end) { | |
1221 | dst += (frag_end - frag_start) * AMDGPU_GPU_PAGE_SIZE; | |
49ac8a24 | 1222 | amdgpu_vm_update_ptes(params, frag_end, end, dst, flags); |
92696dd5 | 1223 | } |
d38ceaf9 AD |
1224 | } |
1225 | ||
d38ceaf9 AD |
1226 | /** |
1227 | * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table | |
1228 | * | |
1229 | * @adev: amdgpu_device pointer | |
3cabaa54 | 1230 | * @exclusive: fence we need to sync to |
fa3ab3c7 CK |
1231 | * @src: address where to copy page table entries from |
1232 | * @pages_addr: DMA addresses to use for mapping | |
d38ceaf9 | 1233 | * @vm: requested vm |
a14faa65 CK |
1234 | * @start: start of mapped range |
1235 | * @last: last mapped entry | |
1236 | * @flags: flags for the entries | |
d38ceaf9 | 1237 | * @addr: addr to set the area to |
d38ceaf9 AD |
1238 | * @fence: optional resulting fence |
1239 | * | |
a14faa65 | 1240 | * Fill in the page table entries between @start and @last. |
d38ceaf9 | 1241 | * Returns 0 for success, -EINVAL for failure. |
d38ceaf9 AD |
1242 | */ |
1243 | static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev, | |
f54d1867 | 1244 | struct dma_fence *exclusive, |
fa3ab3c7 CK |
1245 | uint64_t src, |
1246 | dma_addr_t *pages_addr, | |
d38ceaf9 | 1247 | struct amdgpu_vm *vm, |
a14faa65 | 1248 | uint64_t start, uint64_t last, |
6b777607 | 1249 | uint64_t flags, uint64_t addr, |
f54d1867 | 1250 | struct dma_fence **fence) |
d38ceaf9 | 1251 | { |
2d55e45a | 1252 | struct amdgpu_ring *ring; |
a1e08d3b | 1253 | void *owner = AMDGPU_FENCE_OWNER_VM; |
d38ceaf9 | 1254 | unsigned nptes, ncmds, ndw; |
d71518b5 | 1255 | struct amdgpu_job *job; |
29efc4f5 | 1256 | struct amdgpu_pte_update_params params; |
f54d1867 | 1257 | struct dma_fence *f = NULL; |
d38ceaf9 AD |
1258 | int r; |
1259 | ||
afef8b8f CK |
1260 | memset(¶ms, 0, sizeof(params)); |
1261 | params.adev = adev; | |
49ac8a24 | 1262 | params.vm = vm; |
afef8b8f CK |
1263 | params.src = src; |
1264 | ||
2d55e45a | 1265 | ring = container_of(vm->entity.sched, struct amdgpu_ring, sched); |
27c5f36f | 1266 | |
a1e08d3b CK |
1267 | /* sync to everything on unmapping */ |
1268 | if (!(flags & AMDGPU_PTE_VALID)) | |
1269 | owner = AMDGPU_FENCE_OWNER_UNDEFINED; | |
1270 | ||
a14faa65 | 1271 | nptes = last - start + 1; |
d38ceaf9 AD |
1272 | |
1273 | /* | |
1274 | * reserve space for one command every (1 << BLOCK_SIZE) | |
1275 | * entries or 2k dwords (whatever is smaller) | |
1276 | */ | |
36b32a68 | 1277 | ncmds = (nptes >> min(adev->vm_manager.block_size, 11u)) + 1; |
d38ceaf9 AD |
1278 | |
1279 | /* padding, etc. */ | |
1280 | ndw = 64; | |
1281 | ||
b0456f93 | 1282 | if (src) { |
d38ceaf9 AD |
1283 | /* only copy commands needed */ |
1284 | ndw += ncmds * 7; | |
1285 | ||
afef8b8f CK |
1286 | params.func = amdgpu_vm_do_copy_ptes; |
1287 | ||
b0456f93 CK |
1288 | } else if (pages_addr) { |
1289 | /* copy commands needed */ | |
1290 | ndw += ncmds * 7; | |
d38ceaf9 | 1291 | |
b0456f93 | 1292 | /* and also PTEs */ |
d38ceaf9 AD |
1293 | ndw += nptes * 2; |
1294 | ||
afef8b8f CK |
1295 | params.func = amdgpu_vm_do_copy_ptes; |
1296 | ||
d38ceaf9 AD |
1297 | } else { |
1298 | /* set page commands needed */ | |
1299 | ndw += ncmds * 10; | |
1300 | ||
1301 | /* two extra commands for begin/end of fragment */ | |
1302 | ndw += 2 * 10; | |
afef8b8f CK |
1303 | |
1304 | params.func = amdgpu_vm_do_set_ptes; | |
d38ceaf9 AD |
1305 | } |
1306 | ||
d71518b5 CK |
1307 | r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job); |
1308 | if (r) | |
d38ceaf9 | 1309 | return r; |
d71518b5 | 1310 | |
29efc4f5 | 1311 | params.ib = &job->ibs[0]; |
d5fc5e82 | 1312 | |
b0456f93 CK |
1313 | if (!src && pages_addr) { |
1314 | uint64_t *pte; | |
1315 | unsigned i; | |
1316 | ||
1317 | /* Put the PTEs at the end of the IB. */ | |
1318 | i = ndw - nptes * 2; | |
1319 | pte= (uint64_t *)&(job->ibs->ptr[i]); | |
1320 | params.src = job->ibs->gpu_addr + i * 4; | |
1321 | ||
1322 | for (i = 0; i < nptes; ++i) { | |
1323 | pte[i] = amdgpu_vm_map_gart(pages_addr, addr + i * | |
1324 | AMDGPU_GPU_PAGE_SIZE); | |
1325 | pte[i] |= flags; | |
1326 | } | |
d7a4ac66 | 1327 | addr = 0; |
b0456f93 CK |
1328 | } |
1329 | ||
3cabaa54 CK |
1330 | r = amdgpu_sync_fence(adev, &job->sync, exclusive); |
1331 | if (r) | |
1332 | goto error_free; | |
1333 | ||
67003a15 | 1334 | r = amdgpu_sync_resv(adev, &job->sync, vm->root.bo->tbo.resv, |
a1e08d3b CK |
1335 | owner); |
1336 | if (r) | |
1337 | goto error_free; | |
d38ceaf9 | 1338 | |
67003a15 | 1339 | r = reservation_object_reserve_shared(vm->root.bo->tbo.resv); |
a1e08d3b CK |
1340 | if (r) |
1341 | goto error_free; | |
1342 | ||
4c7e8855 | 1343 | params.shadow = true; |
49ac8a24 | 1344 | amdgpu_vm_frag_ptes(¶ms, start, last + 1, addr, flags); |
4c7e8855 | 1345 | params.shadow = false; |
49ac8a24 | 1346 | amdgpu_vm_frag_ptes(¶ms, start, last + 1, addr, flags); |
d38ceaf9 | 1347 | |
29efc4f5 CK |
1348 | amdgpu_ring_pad_ib(ring, params.ib); |
1349 | WARN_ON(params.ib->length_dw > ndw); | |
2bd9ccfa CK |
1350 | r = amdgpu_job_submit(job, ring, &vm->entity, |
1351 | AMDGPU_FENCE_OWNER_VM, &f); | |
4af9f07c CZ |
1352 | if (r) |
1353 | goto error_free; | |
d38ceaf9 | 1354 | |
67003a15 | 1355 | amdgpu_bo_fence(vm->root.bo, f, true); |
284710fa CK |
1356 | dma_fence_put(*fence); |
1357 | *fence = f; | |
d38ceaf9 | 1358 | return 0; |
d5fc5e82 CZ |
1359 | |
1360 | error_free: | |
d71518b5 | 1361 | amdgpu_job_free(job); |
4af9f07c | 1362 | return r; |
d38ceaf9 AD |
1363 | } |
1364 | ||
a14faa65 CK |
1365 | /** |
1366 | * amdgpu_vm_bo_split_mapping - split a mapping into smaller chunks | |
1367 | * | |
1368 | * @adev: amdgpu_device pointer | |
3cabaa54 | 1369 | * @exclusive: fence we need to sync to |
8358dcee CK |
1370 | * @gtt_flags: flags as they are used for GTT |
1371 | * @pages_addr: DMA addresses to use for mapping | |
a14faa65 CK |
1372 | * @vm: requested vm |
1373 | * @mapping: mapped range and flags to use for the update | |
8358dcee | 1374 | * @flags: HW flags for the mapping |
63e0ba40 | 1375 | * @nodes: array of drm_mm_nodes with the MC addresses |
a14faa65 CK |
1376 | * @fence: optional resulting fence |
1377 | * | |
1378 | * Split the mapping into smaller chunks so that each update fits | |
1379 | * into a SDMA IB. | |
1380 | * Returns 0 for success, -EINVAL for failure. | |
1381 | */ | |
1382 | static int amdgpu_vm_bo_split_mapping(struct amdgpu_device *adev, | |
f54d1867 | 1383 | struct dma_fence *exclusive, |
6b777607 | 1384 | uint64_t gtt_flags, |
8358dcee | 1385 | dma_addr_t *pages_addr, |
a14faa65 CK |
1386 | struct amdgpu_vm *vm, |
1387 | struct amdgpu_bo_va_mapping *mapping, | |
6b777607 | 1388 | uint64_t flags, |
63e0ba40 | 1389 | struct drm_mm_node *nodes, |
f54d1867 | 1390 | struct dma_fence **fence) |
a14faa65 | 1391 | { |
a9f87f64 | 1392 | uint64_t pfn, src = 0, start = mapping->start; |
a14faa65 CK |
1393 | int r; |
1394 | ||
1395 | /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here | |
1396 | * but in case of something, we filter the flags in first place | |
1397 | */ | |
1398 | if (!(mapping->flags & AMDGPU_PTE_READABLE)) | |
1399 | flags &= ~AMDGPU_PTE_READABLE; | |
1400 | if (!(mapping->flags & AMDGPU_PTE_WRITEABLE)) | |
1401 | flags &= ~AMDGPU_PTE_WRITEABLE; | |
1402 | ||
15b31c59 AX |
1403 | flags &= ~AMDGPU_PTE_EXECUTABLE; |
1404 | flags |= mapping->flags & AMDGPU_PTE_EXECUTABLE; | |
1405 | ||
b0fd18b0 AX |
1406 | flags &= ~AMDGPU_PTE_MTYPE_MASK; |
1407 | flags |= (mapping->flags & AMDGPU_PTE_MTYPE_MASK); | |
1408 | ||
d0766e98 ZJ |
1409 | if ((mapping->flags & AMDGPU_PTE_PRT) && |
1410 | (adev->asic_type >= CHIP_VEGA10)) { | |
1411 | flags |= AMDGPU_PTE_PRT; | |
1412 | flags &= ~AMDGPU_PTE_VALID; | |
1413 | } | |
1414 | ||
a14faa65 CK |
1415 | trace_amdgpu_vm_bo_update(mapping); |
1416 | ||
63e0ba40 CK |
1417 | pfn = mapping->offset >> PAGE_SHIFT; |
1418 | if (nodes) { | |
1419 | while (pfn >= nodes->size) { | |
1420 | pfn -= nodes->size; | |
1421 | ++nodes; | |
1422 | } | |
fa3ab3c7 | 1423 | } |
a14faa65 | 1424 | |
63e0ba40 CK |
1425 | do { |
1426 | uint64_t max_entries; | |
1427 | uint64_t addr, last; | |
a14faa65 | 1428 | |
63e0ba40 CK |
1429 | if (nodes) { |
1430 | addr = nodes->start << PAGE_SHIFT; | |
1431 | max_entries = (nodes->size - pfn) * | |
1432 | (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE); | |
1433 | } else { | |
1434 | addr = 0; | |
1435 | max_entries = S64_MAX; | |
1436 | } | |
a14faa65 | 1437 | |
63e0ba40 CK |
1438 | if (pages_addr) { |
1439 | if (flags == gtt_flags) | |
1440 | src = adev->gart.table_addr + | |
1441 | (addr >> AMDGPU_GPU_PAGE_SHIFT) * 8; | |
1442 | else | |
1443 | max_entries = min(max_entries, 16ull * 1024ull); | |
1444 | addr = 0; | |
1445 | } else if (flags & AMDGPU_PTE_VALID) { | |
1446 | addr += adev->vm_manager.vram_base_offset; | |
1447 | } | |
1448 | addr += pfn << PAGE_SHIFT; | |
1449 | ||
a9f87f64 | 1450 | last = min((uint64_t)mapping->last, start + max_entries - 1); |
3cabaa54 CK |
1451 | r = amdgpu_vm_bo_update_mapping(adev, exclusive, |
1452 | src, pages_addr, vm, | |
a14faa65 CK |
1453 | start, last, flags, addr, |
1454 | fence); | |
1455 | if (r) | |
1456 | return r; | |
1457 | ||
63e0ba40 CK |
1458 | pfn += last - start + 1; |
1459 | if (nodes && nodes->size == pfn) { | |
1460 | pfn = 0; | |
1461 | ++nodes; | |
1462 | } | |
a14faa65 | 1463 | start = last + 1; |
63e0ba40 | 1464 | |
a9f87f64 | 1465 | } while (unlikely(start != mapping->last + 1)); |
a14faa65 CK |
1466 | |
1467 | return 0; | |
1468 | } | |
1469 | ||
d38ceaf9 AD |
1470 | /** |
1471 | * amdgpu_vm_bo_update - update all BO mappings in the vm page table | |
1472 | * | |
1473 | * @adev: amdgpu_device pointer | |
1474 | * @bo_va: requested BO and VM object | |
99e124f4 | 1475 | * @clear: if true clear the entries |
d38ceaf9 AD |
1476 | * |
1477 | * Fill in the page table entries for @bo_va. | |
1478 | * Returns 0 for success, -EINVAL for failure. | |
d38ceaf9 AD |
1479 | */ |
1480 | int amdgpu_vm_bo_update(struct amdgpu_device *adev, | |
1481 | struct amdgpu_bo_va *bo_va, | |
99e124f4 | 1482 | bool clear) |
d38ceaf9 AD |
1483 | { |
1484 | struct amdgpu_vm *vm = bo_va->vm; | |
1485 | struct amdgpu_bo_va_mapping *mapping; | |
8358dcee | 1486 | dma_addr_t *pages_addr = NULL; |
6b777607 | 1487 | uint64_t gtt_flags, flags; |
99e124f4 | 1488 | struct ttm_mem_reg *mem; |
63e0ba40 | 1489 | struct drm_mm_node *nodes; |
f54d1867 | 1490 | struct dma_fence *exclusive; |
d38ceaf9 AD |
1491 | int r; |
1492 | ||
a5f6b5b1 | 1493 | if (clear || !bo_va->bo) { |
99e124f4 | 1494 | mem = NULL; |
63e0ba40 | 1495 | nodes = NULL; |
99e124f4 CK |
1496 | exclusive = NULL; |
1497 | } else { | |
8358dcee CK |
1498 | struct ttm_dma_tt *ttm; |
1499 | ||
99e124f4 | 1500 | mem = &bo_va->bo->tbo.mem; |
63e0ba40 CK |
1501 | nodes = mem->mm_node; |
1502 | if (mem->mem_type == TTM_PL_TT) { | |
8358dcee CK |
1503 | ttm = container_of(bo_va->bo->tbo.ttm, struct |
1504 | ttm_dma_tt, ttm); | |
1505 | pages_addr = ttm->dma_address; | |
9ab21462 | 1506 | } |
3cabaa54 | 1507 | exclusive = reservation_object_get_excl(bo_va->bo->tbo.resv); |
d38ceaf9 AD |
1508 | } |
1509 | ||
a5f6b5b1 CK |
1510 | if (bo_va->bo) { |
1511 | flags = amdgpu_ttm_tt_pte_flags(adev, bo_va->bo->tbo.ttm, mem); | |
1512 | gtt_flags = (amdgpu_ttm_is_bound(bo_va->bo->tbo.ttm) && | |
1513 | adev == amdgpu_ttm_adev(bo_va->bo->tbo.bdev)) ? | |
1514 | flags : 0; | |
1515 | } else { | |
1516 | flags = 0x0; | |
1517 | gtt_flags = ~0x0; | |
1518 | } | |
d38ceaf9 | 1519 | |
7fc11959 CK |
1520 | spin_lock(&vm->status_lock); |
1521 | if (!list_empty(&bo_va->vm_status)) | |
1522 | list_splice_init(&bo_va->valids, &bo_va->invalids); | |
1523 | spin_unlock(&vm->status_lock); | |
1524 | ||
1525 | list_for_each_entry(mapping, &bo_va->invalids, list) { | |
3cabaa54 CK |
1526 | r = amdgpu_vm_bo_split_mapping(adev, exclusive, |
1527 | gtt_flags, pages_addr, vm, | |
63e0ba40 | 1528 | mapping, flags, nodes, |
8358dcee | 1529 | &bo_va->last_pt_update); |
d38ceaf9 AD |
1530 | if (r) |
1531 | return r; | |
1532 | } | |
1533 | ||
d6c10f6b CK |
1534 | if (trace_amdgpu_vm_bo_mapping_enabled()) { |
1535 | list_for_each_entry(mapping, &bo_va->valids, list) | |
1536 | trace_amdgpu_vm_bo_mapping(mapping); | |
1537 | ||
1538 | list_for_each_entry(mapping, &bo_va->invalids, list) | |
1539 | trace_amdgpu_vm_bo_mapping(mapping); | |
1540 | } | |
1541 | ||
d38ceaf9 | 1542 | spin_lock(&vm->status_lock); |
6d1d0ef7 | 1543 | list_splice_init(&bo_va->invalids, &bo_va->valids); |
d38ceaf9 | 1544 | list_del_init(&bo_va->vm_status); |
99e124f4 | 1545 | if (clear) |
7fc11959 | 1546 | list_add(&bo_va->vm_status, &vm->cleared); |
d38ceaf9 AD |
1547 | spin_unlock(&vm->status_lock); |
1548 | ||
1549 | return 0; | |
1550 | } | |
1551 | ||
284710fa CK |
1552 | /** |
1553 | * amdgpu_vm_update_prt_state - update the global PRT state | |
1554 | */ | |
1555 | static void amdgpu_vm_update_prt_state(struct amdgpu_device *adev) | |
1556 | { | |
1557 | unsigned long flags; | |
1558 | bool enable; | |
1559 | ||
1560 | spin_lock_irqsave(&adev->vm_manager.prt_lock, flags); | |
451bc8eb | 1561 | enable = !!atomic_read(&adev->vm_manager.num_prt_users); |
284710fa CK |
1562 | adev->gart.gart_funcs->set_prt(adev, enable); |
1563 | spin_unlock_irqrestore(&adev->vm_manager.prt_lock, flags); | |
1564 | } | |
1565 | ||
451bc8eb | 1566 | /** |
4388fc2a | 1567 | * amdgpu_vm_prt_get - add a PRT user |
451bc8eb CK |
1568 | */ |
1569 | static void amdgpu_vm_prt_get(struct amdgpu_device *adev) | |
1570 | { | |
4388fc2a CK |
1571 | if (!adev->gart.gart_funcs->set_prt) |
1572 | return; | |
1573 | ||
451bc8eb CK |
1574 | if (atomic_inc_return(&adev->vm_manager.num_prt_users) == 1) |
1575 | amdgpu_vm_update_prt_state(adev); | |
1576 | } | |
1577 | ||
0b15f2fc CK |
1578 | /** |
1579 | * amdgpu_vm_prt_put - drop a PRT user | |
1580 | */ | |
1581 | static void amdgpu_vm_prt_put(struct amdgpu_device *adev) | |
1582 | { | |
451bc8eb | 1583 | if (atomic_dec_return(&adev->vm_manager.num_prt_users) == 0) |
0b15f2fc CK |
1584 | amdgpu_vm_update_prt_state(adev); |
1585 | } | |
1586 | ||
284710fa | 1587 | /** |
451bc8eb | 1588 | * amdgpu_vm_prt_cb - callback for updating the PRT status |
284710fa CK |
1589 | */ |
1590 | static void amdgpu_vm_prt_cb(struct dma_fence *fence, struct dma_fence_cb *_cb) | |
1591 | { | |
1592 | struct amdgpu_prt_cb *cb = container_of(_cb, struct amdgpu_prt_cb, cb); | |
1593 | ||
0b15f2fc | 1594 | amdgpu_vm_prt_put(cb->adev); |
284710fa CK |
1595 | kfree(cb); |
1596 | } | |
1597 | ||
451bc8eb CK |
1598 | /** |
1599 | * amdgpu_vm_add_prt_cb - add callback for updating the PRT status | |
1600 | */ | |
1601 | static void amdgpu_vm_add_prt_cb(struct amdgpu_device *adev, | |
1602 | struct dma_fence *fence) | |
1603 | { | |
4388fc2a | 1604 | struct amdgpu_prt_cb *cb; |
451bc8eb | 1605 | |
4388fc2a CK |
1606 | if (!adev->gart.gart_funcs->set_prt) |
1607 | return; | |
1608 | ||
1609 | cb = kmalloc(sizeof(struct amdgpu_prt_cb), GFP_KERNEL); | |
451bc8eb CK |
1610 | if (!cb) { |
1611 | /* Last resort when we are OOM */ | |
1612 | if (fence) | |
1613 | dma_fence_wait(fence, false); | |
1614 | ||
486a68f5 | 1615 | amdgpu_vm_prt_put(adev); |
451bc8eb CK |
1616 | } else { |
1617 | cb->adev = adev; | |
1618 | if (!fence || dma_fence_add_callback(fence, &cb->cb, | |
1619 | amdgpu_vm_prt_cb)) | |
1620 | amdgpu_vm_prt_cb(fence, &cb->cb); | |
1621 | } | |
1622 | } | |
1623 | ||
284710fa CK |
1624 | /** |
1625 | * amdgpu_vm_free_mapping - free a mapping | |
1626 | * | |
1627 | * @adev: amdgpu_device pointer | |
1628 | * @vm: requested vm | |
1629 | * @mapping: mapping to be freed | |
1630 | * @fence: fence of the unmap operation | |
1631 | * | |
1632 | * Free a mapping and make sure we decrease the PRT usage count if applicable. | |
1633 | */ | |
1634 | static void amdgpu_vm_free_mapping(struct amdgpu_device *adev, | |
1635 | struct amdgpu_vm *vm, | |
1636 | struct amdgpu_bo_va_mapping *mapping, | |
1637 | struct dma_fence *fence) | |
1638 | { | |
451bc8eb CK |
1639 | if (mapping->flags & AMDGPU_PTE_PRT) |
1640 | amdgpu_vm_add_prt_cb(adev, fence); | |
1641 | kfree(mapping); | |
1642 | } | |
284710fa | 1643 | |
451bc8eb CK |
1644 | /** |
1645 | * amdgpu_vm_prt_fini - finish all prt mappings | |
1646 | * | |
1647 | * @adev: amdgpu_device pointer | |
1648 | * @vm: requested vm | |
1649 | * | |
1650 | * Register a cleanup callback to disable PRT support after VM dies. | |
1651 | */ | |
1652 | static void amdgpu_vm_prt_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm) | |
1653 | { | |
67003a15 | 1654 | struct reservation_object *resv = vm->root.bo->tbo.resv; |
451bc8eb CK |
1655 | struct dma_fence *excl, **shared; |
1656 | unsigned i, shared_count; | |
1657 | int r; | |
0b15f2fc | 1658 | |
451bc8eb CK |
1659 | r = reservation_object_get_fences_rcu(resv, &excl, |
1660 | &shared_count, &shared); | |
1661 | if (r) { | |
1662 | /* Not enough memory to grab the fence list, as last resort | |
1663 | * block for all the fences to complete. | |
1664 | */ | |
1665 | reservation_object_wait_timeout_rcu(resv, true, false, | |
1666 | MAX_SCHEDULE_TIMEOUT); | |
1667 | return; | |
284710fa | 1668 | } |
451bc8eb CK |
1669 | |
1670 | /* Add a callback for each fence in the reservation object */ | |
1671 | amdgpu_vm_prt_get(adev); | |
1672 | amdgpu_vm_add_prt_cb(adev, excl); | |
1673 | ||
1674 | for (i = 0; i < shared_count; ++i) { | |
1675 | amdgpu_vm_prt_get(adev); | |
1676 | amdgpu_vm_add_prt_cb(adev, shared[i]); | |
1677 | } | |
1678 | ||
1679 | kfree(shared); | |
284710fa CK |
1680 | } |
1681 | ||
d38ceaf9 AD |
1682 | /** |
1683 | * amdgpu_vm_clear_freed - clear freed BOs in the PT | |
1684 | * | |
1685 | * @adev: amdgpu_device pointer | |
1686 | * @vm: requested vm | |
f3467818 NH |
1687 | * @fence: optional resulting fence (unchanged if no work needed to be done |
1688 | * or if an error occurred) | |
d38ceaf9 AD |
1689 | * |
1690 | * Make sure all freed BOs are cleared in the PT. | |
1691 | * Returns 0 for success. | |
1692 | * | |
1693 | * PTs have to be reserved and mutex must be locked! | |
1694 | */ | |
1695 | int amdgpu_vm_clear_freed(struct amdgpu_device *adev, | |
f3467818 NH |
1696 | struct amdgpu_vm *vm, |
1697 | struct dma_fence **fence) | |
d38ceaf9 AD |
1698 | { |
1699 | struct amdgpu_bo_va_mapping *mapping; | |
f3467818 | 1700 | struct dma_fence *f = NULL; |
d38ceaf9 AD |
1701 | int r; |
1702 | ||
1703 | while (!list_empty(&vm->freed)) { | |
1704 | mapping = list_first_entry(&vm->freed, | |
1705 | struct amdgpu_bo_va_mapping, list); | |
1706 | list_del(&mapping->list); | |
e17841b9 | 1707 | |
fc6aa33d CK |
1708 | r = amdgpu_vm_bo_update_mapping(adev, NULL, 0, NULL, vm, |
1709 | mapping->start, mapping->last, | |
1710 | 0, 0, &f); | |
f3467818 | 1711 | amdgpu_vm_free_mapping(adev, vm, mapping, f); |
284710fa | 1712 | if (r) { |
f3467818 | 1713 | dma_fence_put(f); |
d38ceaf9 | 1714 | return r; |
284710fa | 1715 | } |
f3467818 | 1716 | } |
d38ceaf9 | 1717 | |
f3467818 NH |
1718 | if (fence && f) { |
1719 | dma_fence_put(*fence); | |
1720 | *fence = f; | |
1721 | } else { | |
1722 | dma_fence_put(f); | |
d38ceaf9 | 1723 | } |
f3467818 | 1724 | |
d38ceaf9 AD |
1725 | return 0; |
1726 | ||
1727 | } | |
1728 | ||
1729 | /** | |
1730 | * amdgpu_vm_clear_invalids - clear invalidated BOs in the PT | |
1731 | * | |
1732 | * @adev: amdgpu_device pointer | |
1733 | * @vm: requested vm | |
1734 | * | |
1735 | * Make sure all invalidated BOs are cleared in the PT. | |
1736 | * Returns 0 for success. | |
1737 | * | |
1738 | * PTs have to be reserved and mutex must be locked! | |
1739 | */ | |
1740 | int amdgpu_vm_clear_invalids(struct amdgpu_device *adev, | |
cfe2c978 | 1741 | struct amdgpu_vm *vm, struct amdgpu_sync *sync) |
d38ceaf9 | 1742 | { |
cfe2c978 | 1743 | struct amdgpu_bo_va *bo_va = NULL; |
91e1a520 | 1744 | int r = 0; |
d38ceaf9 AD |
1745 | |
1746 | spin_lock(&vm->status_lock); | |
1747 | while (!list_empty(&vm->invalidated)) { | |
1748 | bo_va = list_first_entry(&vm->invalidated, | |
1749 | struct amdgpu_bo_va, vm_status); | |
1750 | spin_unlock(&vm->status_lock); | |
32b41ac2 | 1751 | |
99e124f4 | 1752 | r = amdgpu_vm_bo_update(adev, bo_va, true); |
d38ceaf9 AD |
1753 | if (r) |
1754 | return r; | |
1755 | ||
1756 | spin_lock(&vm->status_lock); | |
1757 | } | |
1758 | spin_unlock(&vm->status_lock); | |
1759 | ||
cfe2c978 | 1760 | if (bo_va) |
bb1e38a4 | 1761 | r = amdgpu_sync_fence(adev, sync, bo_va->last_pt_update); |
91e1a520 CK |
1762 | |
1763 | return r; | |
d38ceaf9 AD |
1764 | } |
1765 | ||
1766 | /** | |
1767 | * amdgpu_vm_bo_add - add a bo to a specific vm | |
1768 | * | |
1769 | * @adev: amdgpu_device pointer | |
1770 | * @vm: requested vm | |
1771 | * @bo: amdgpu buffer object | |
1772 | * | |
8843dbbb | 1773 | * Add @bo into the requested vm. |
d38ceaf9 AD |
1774 | * Add @bo to the list of bos associated with the vm |
1775 | * Returns newly added bo_va or NULL for failure | |
1776 | * | |
1777 | * Object has to be reserved! | |
1778 | */ | |
1779 | struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev, | |
1780 | struct amdgpu_vm *vm, | |
1781 | struct amdgpu_bo *bo) | |
1782 | { | |
1783 | struct amdgpu_bo_va *bo_va; | |
1784 | ||
1785 | bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL); | |
1786 | if (bo_va == NULL) { | |
1787 | return NULL; | |
1788 | } | |
1789 | bo_va->vm = vm; | |
1790 | bo_va->bo = bo; | |
d38ceaf9 AD |
1791 | bo_va->ref_count = 1; |
1792 | INIT_LIST_HEAD(&bo_va->bo_list); | |
7fc11959 CK |
1793 | INIT_LIST_HEAD(&bo_va->valids); |
1794 | INIT_LIST_HEAD(&bo_va->invalids); | |
d38ceaf9 | 1795 | INIT_LIST_HEAD(&bo_va->vm_status); |
32b41ac2 | 1796 | |
a5f6b5b1 CK |
1797 | if (bo) |
1798 | list_add_tail(&bo_va->bo_list, &bo->va); | |
d38ceaf9 AD |
1799 | |
1800 | return bo_va; | |
1801 | } | |
1802 | ||
1803 | /** | |
1804 | * amdgpu_vm_bo_map - map bo inside a vm | |
1805 | * | |
1806 | * @adev: amdgpu_device pointer | |
1807 | * @bo_va: bo_va to store the address | |
1808 | * @saddr: where to map the BO | |
1809 | * @offset: requested offset in the BO | |
1810 | * @flags: attributes of pages (read/write/valid/etc.) | |
1811 | * | |
1812 | * Add a mapping of the BO at the specefied addr into the VM. | |
1813 | * Returns 0 for success, error for failure. | |
1814 | * | |
49b02b18 | 1815 | * Object has to be reserved and unreserved outside! |
d38ceaf9 AD |
1816 | */ |
1817 | int amdgpu_vm_bo_map(struct amdgpu_device *adev, | |
1818 | struct amdgpu_bo_va *bo_va, | |
1819 | uint64_t saddr, uint64_t offset, | |
268c3001 | 1820 | uint64_t size, uint64_t flags) |
d38ceaf9 | 1821 | { |
a9f87f64 | 1822 | struct amdgpu_bo_va_mapping *mapping, *tmp; |
d38ceaf9 | 1823 | struct amdgpu_vm *vm = bo_va->vm; |
d38ceaf9 | 1824 | uint64_t eaddr; |
d38ceaf9 | 1825 | |
0be52de9 CK |
1826 | /* validate the parameters */ |
1827 | if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK || | |
49b02b18 | 1828 | size == 0 || size & AMDGPU_GPU_PAGE_MASK) |
0be52de9 | 1829 | return -EINVAL; |
0be52de9 | 1830 | |
d38ceaf9 | 1831 | /* make sure object fit at this offset */ |
005ae95e | 1832 | eaddr = saddr + size - 1; |
a5f6b5b1 CK |
1833 | if (saddr >= eaddr || |
1834 | (bo_va->bo && offset + size > amdgpu_bo_size(bo_va->bo))) | |
d38ceaf9 | 1835 | return -EINVAL; |
d38ceaf9 | 1836 | |
d38ceaf9 AD |
1837 | saddr /= AMDGPU_GPU_PAGE_SIZE; |
1838 | eaddr /= AMDGPU_GPU_PAGE_SIZE; | |
1839 | ||
a9f87f64 CK |
1840 | tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr); |
1841 | if (tmp) { | |
d38ceaf9 AD |
1842 | /* bo and tmp overlap, invalid addr */ |
1843 | dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with " | |
a9f87f64 CK |
1844 | "0x%010Lx-0x%010Lx\n", bo_va->bo, saddr, eaddr, |
1845 | tmp->start, tmp->last + 1); | |
663e4577 | 1846 | return -EINVAL; |
d38ceaf9 AD |
1847 | } |
1848 | ||
1849 | mapping = kmalloc(sizeof(*mapping), GFP_KERNEL); | |
663e4577 CK |
1850 | if (!mapping) |
1851 | return -ENOMEM; | |
d38ceaf9 AD |
1852 | |
1853 | INIT_LIST_HEAD(&mapping->list); | |
a9f87f64 CK |
1854 | mapping->start = saddr; |
1855 | mapping->last = eaddr; | |
d38ceaf9 AD |
1856 | mapping->offset = offset; |
1857 | mapping->flags = flags; | |
1858 | ||
7fc11959 | 1859 | list_add(&mapping->list, &bo_va->invalids); |
a9f87f64 | 1860 | amdgpu_vm_it_insert(mapping, &vm->va); |
80f95c57 CK |
1861 | |
1862 | if (flags & AMDGPU_PTE_PRT) | |
1863 | amdgpu_vm_prt_get(adev); | |
1864 | ||
1865 | return 0; | |
1866 | } | |
1867 | ||
1868 | /** | |
1869 | * amdgpu_vm_bo_replace_map - map bo inside a vm, replacing existing mappings | |
1870 | * | |
1871 | * @adev: amdgpu_device pointer | |
1872 | * @bo_va: bo_va to store the address | |
1873 | * @saddr: where to map the BO | |
1874 | * @offset: requested offset in the BO | |
1875 | * @flags: attributes of pages (read/write/valid/etc.) | |
1876 | * | |
1877 | * Add a mapping of the BO at the specefied addr into the VM. Replace existing | |
1878 | * mappings as we do so. | |
1879 | * Returns 0 for success, error for failure. | |
1880 | * | |
1881 | * Object has to be reserved and unreserved outside! | |
1882 | */ | |
1883 | int amdgpu_vm_bo_replace_map(struct amdgpu_device *adev, | |
1884 | struct amdgpu_bo_va *bo_va, | |
1885 | uint64_t saddr, uint64_t offset, | |
1886 | uint64_t size, uint64_t flags) | |
1887 | { | |
1888 | struct amdgpu_bo_va_mapping *mapping; | |
1889 | struct amdgpu_vm *vm = bo_va->vm; | |
1890 | uint64_t eaddr; | |
1891 | int r; | |
1892 | ||
1893 | /* validate the parameters */ | |
1894 | if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK || | |
1895 | size == 0 || size & AMDGPU_GPU_PAGE_MASK) | |
1896 | return -EINVAL; | |
1897 | ||
1898 | /* make sure object fit at this offset */ | |
1899 | eaddr = saddr + size - 1; | |
1900 | if (saddr >= eaddr || | |
1901 | (bo_va->bo && offset + size > amdgpu_bo_size(bo_va->bo))) | |
1902 | return -EINVAL; | |
1903 | ||
1904 | /* Allocate all the needed memory */ | |
1905 | mapping = kmalloc(sizeof(*mapping), GFP_KERNEL); | |
1906 | if (!mapping) | |
1907 | return -ENOMEM; | |
1908 | ||
1909 | r = amdgpu_vm_bo_clear_mappings(adev, bo_va->vm, saddr, size); | |
1910 | if (r) { | |
1911 | kfree(mapping); | |
1912 | return r; | |
1913 | } | |
1914 | ||
1915 | saddr /= AMDGPU_GPU_PAGE_SIZE; | |
1916 | eaddr /= AMDGPU_GPU_PAGE_SIZE; | |
1917 | ||
a9f87f64 CK |
1918 | mapping->start = saddr; |
1919 | mapping->last = eaddr; | |
80f95c57 CK |
1920 | mapping->offset = offset; |
1921 | mapping->flags = flags; | |
1922 | ||
1923 | list_add(&mapping->list, &bo_va->invalids); | |
a9f87f64 | 1924 | amdgpu_vm_it_insert(mapping, &vm->va); |
d38ceaf9 | 1925 | |
4388fc2a CK |
1926 | if (flags & AMDGPU_PTE_PRT) |
1927 | amdgpu_vm_prt_get(adev); | |
1928 | ||
d38ceaf9 | 1929 | return 0; |
d38ceaf9 AD |
1930 | } |
1931 | ||
1932 | /** | |
1933 | * amdgpu_vm_bo_unmap - remove bo mapping from vm | |
1934 | * | |
1935 | * @adev: amdgpu_device pointer | |
1936 | * @bo_va: bo_va to remove the address from | |
1937 | * @saddr: where to the BO is mapped | |
1938 | * | |
1939 | * Remove a mapping of the BO at the specefied addr from the VM. | |
1940 | * Returns 0 for success, error for failure. | |
1941 | * | |
49b02b18 | 1942 | * Object has to be reserved and unreserved outside! |
d38ceaf9 AD |
1943 | */ |
1944 | int amdgpu_vm_bo_unmap(struct amdgpu_device *adev, | |
1945 | struct amdgpu_bo_va *bo_va, | |
1946 | uint64_t saddr) | |
1947 | { | |
1948 | struct amdgpu_bo_va_mapping *mapping; | |
1949 | struct amdgpu_vm *vm = bo_va->vm; | |
7fc11959 | 1950 | bool valid = true; |
d38ceaf9 | 1951 | |
6c7fc503 | 1952 | saddr /= AMDGPU_GPU_PAGE_SIZE; |
32b41ac2 | 1953 | |
7fc11959 | 1954 | list_for_each_entry(mapping, &bo_va->valids, list) { |
a9f87f64 | 1955 | if (mapping->start == saddr) |
d38ceaf9 AD |
1956 | break; |
1957 | } | |
1958 | ||
7fc11959 CK |
1959 | if (&mapping->list == &bo_va->valids) { |
1960 | valid = false; | |
1961 | ||
1962 | list_for_each_entry(mapping, &bo_va->invalids, list) { | |
a9f87f64 | 1963 | if (mapping->start == saddr) |
7fc11959 CK |
1964 | break; |
1965 | } | |
1966 | ||
32b41ac2 | 1967 | if (&mapping->list == &bo_va->invalids) |
7fc11959 | 1968 | return -ENOENT; |
d38ceaf9 | 1969 | } |
32b41ac2 | 1970 | |
d38ceaf9 | 1971 | list_del(&mapping->list); |
a9f87f64 | 1972 | amdgpu_vm_it_remove(mapping, &vm->va); |
93e3e438 | 1973 | trace_amdgpu_vm_bo_unmap(bo_va, mapping); |
d38ceaf9 | 1974 | |
e17841b9 | 1975 | if (valid) |
d38ceaf9 | 1976 | list_add(&mapping->list, &vm->freed); |
e17841b9 | 1977 | else |
284710fa CK |
1978 | amdgpu_vm_free_mapping(adev, vm, mapping, |
1979 | bo_va->last_pt_update); | |
d38ceaf9 AD |
1980 | |
1981 | return 0; | |
1982 | } | |
1983 | ||
dc54d3d1 CK |
1984 | /** |
1985 | * amdgpu_vm_bo_clear_mappings - remove all mappings in a specific range | |
1986 | * | |
1987 | * @adev: amdgpu_device pointer | |
1988 | * @vm: VM structure to use | |
1989 | * @saddr: start of the range | |
1990 | * @size: size of the range | |
1991 | * | |
1992 | * Remove all mappings in a range, split them as appropriate. | |
1993 | * Returns 0 for success, error for failure. | |
1994 | */ | |
1995 | int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev, | |
1996 | struct amdgpu_vm *vm, | |
1997 | uint64_t saddr, uint64_t size) | |
1998 | { | |
1999 | struct amdgpu_bo_va_mapping *before, *after, *tmp, *next; | |
dc54d3d1 CK |
2000 | LIST_HEAD(removed); |
2001 | uint64_t eaddr; | |
2002 | ||
2003 | eaddr = saddr + size - 1; | |
2004 | saddr /= AMDGPU_GPU_PAGE_SIZE; | |
2005 | eaddr /= AMDGPU_GPU_PAGE_SIZE; | |
2006 | ||
2007 | /* Allocate all the needed memory */ | |
2008 | before = kzalloc(sizeof(*before), GFP_KERNEL); | |
2009 | if (!before) | |
2010 | return -ENOMEM; | |
27f6d610 | 2011 | INIT_LIST_HEAD(&before->list); |
dc54d3d1 CK |
2012 | |
2013 | after = kzalloc(sizeof(*after), GFP_KERNEL); | |
2014 | if (!after) { | |
2015 | kfree(before); | |
2016 | return -ENOMEM; | |
2017 | } | |
27f6d610 | 2018 | INIT_LIST_HEAD(&after->list); |
dc54d3d1 CK |
2019 | |
2020 | /* Now gather all removed mappings */ | |
a9f87f64 CK |
2021 | tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr); |
2022 | while (tmp) { | |
dc54d3d1 | 2023 | /* Remember mapping split at the start */ |
a9f87f64 CK |
2024 | if (tmp->start < saddr) { |
2025 | before->start = tmp->start; | |
2026 | before->last = saddr - 1; | |
dc54d3d1 CK |
2027 | before->offset = tmp->offset; |
2028 | before->flags = tmp->flags; | |
2029 | list_add(&before->list, &tmp->list); | |
2030 | } | |
2031 | ||
2032 | /* Remember mapping split at the end */ | |
a9f87f64 CK |
2033 | if (tmp->last > eaddr) { |
2034 | after->start = eaddr + 1; | |
2035 | after->last = tmp->last; | |
dc54d3d1 | 2036 | after->offset = tmp->offset; |
a9f87f64 | 2037 | after->offset += after->start - tmp->start; |
dc54d3d1 CK |
2038 | after->flags = tmp->flags; |
2039 | list_add(&after->list, &tmp->list); | |
2040 | } | |
2041 | ||
2042 | list_del(&tmp->list); | |
2043 | list_add(&tmp->list, &removed); | |
a9f87f64 CK |
2044 | |
2045 | tmp = amdgpu_vm_it_iter_next(tmp, saddr, eaddr); | |
dc54d3d1 CK |
2046 | } |
2047 | ||
2048 | /* And free them up */ | |
2049 | list_for_each_entry_safe(tmp, next, &removed, list) { | |
a9f87f64 | 2050 | amdgpu_vm_it_remove(tmp, &vm->va); |
dc54d3d1 CK |
2051 | list_del(&tmp->list); |
2052 | ||
a9f87f64 CK |
2053 | if (tmp->start < saddr) |
2054 | tmp->start = saddr; | |
2055 | if (tmp->last > eaddr) | |
2056 | tmp->last = eaddr; | |
dc54d3d1 CK |
2057 | |
2058 | list_add(&tmp->list, &vm->freed); | |
2059 | trace_amdgpu_vm_bo_unmap(NULL, tmp); | |
2060 | } | |
2061 | ||
27f6d610 JZ |
2062 | /* Insert partial mapping before the range */ |
2063 | if (!list_empty(&before->list)) { | |
a9f87f64 | 2064 | amdgpu_vm_it_insert(before, &vm->va); |
dc54d3d1 CK |
2065 | if (before->flags & AMDGPU_PTE_PRT) |
2066 | amdgpu_vm_prt_get(adev); | |
2067 | } else { | |
2068 | kfree(before); | |
2069 | } | |
2070 | ||
2071 | /* Insert partial mapping after the range */ | |
27f6d610 | 2072 | if (!list_empty(&after->list)) { |
a9f87f64 | 2073 | amdgpu_vm_it_insert(after, &vm->va); |
dc54d3d1 CK |
2074 | if (after->flags & AMDGPU_PTE_PRT) |
2075 | amdgpu_vm_prt_get(adev); | |
2076 | } else { | |
2077 | kfree(after); | |
2078 | } | |
2079 | ||
2080 | return 0; | |
2081 | } | |
2082 | ||
d38ceaf9 AD |
2083 | /** |
2084 | * amdgpu_vm_bo_rmv - remove a bo to a specific vm | |
2085 | * | |
2086 | * @adev: amdgpu_device pointer | |
2087 | * @bo_va: requested bo_va | |
2088 | * | |
8843dbbb | 2089 | * Remove @bo_va->bo from the requested vm. |
d38ceaf9 AD |
2090 | * |
2091 | * Object have to be reserved! | |
2092 | */ | |
2093 | void amdgpu_vm_bo_rmv(struct amdgpu_device *adev, | |
2094 | struct amdgpu_bo_va *bo_va) | |
2095 | { | |
2096 | struct amdgpu_bo_va_mapping *mapping, *next; | |
2097 | struct amdgpu_vm *vm = bo_va->vm; | |
2098 | ||
2099 | list_del(&bo_va->bo_list); | |
2100 | ||
d38ceaf9 AD |
2101 | spin_lock(&vm->status_lock); |
2102 | list_del(&bo_va->vm_status); | |
2103 | spin_unlock(&vm->status_lock); | |
2104 | ||
7fc11959 | 2105 | list_for_each_entry_safe(mapping, next, &bo_va->valids, list) { |
d38ceaf9 | 2106 | list_del(&mapping->list); |
a9f87f64 | 2107 | amdgpu_vm_it_remove(mapping, &vm->va); |
93e3e438 | 2108 | trace_amdgpu_vm_bo_unmap(bo_va, mapping); |
7fc11959 CK |
2109 | list_add(&mapping->list, &vm->freed); |
2110 | } | |
2111 | list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) { | |
2112 | list_del(&mapping->list); | |
a9f87f64 | 2113 | amdgpu_vm_it_remove(mapping, &vm->va); |
284710fa CK |
2114 | amdgpu_vm_free_mapping(adev, vm, mapping, |
2115 | bo_va->last_pt_update); | |
d38ceaf9 | 2116 | } |
32b41ac2 | 2117 | |
f54d1867 | 2118 | dma_fence_put(bo_va->last_pt_update); |
d38ceaf9 | 2119 | kfree(bo_va); |
d38ceaf9 AD |
2120 | } |
2121 | ||
2122 | /** | |
2123 | * amdgpu_vm_bo_invalidate - mark the bo as invalid | |
2124 | * | |
2125 | * @adev: amdgpu_device pointer | |
2126 | * @vm: requested vm | |
2127 | * @bo: amdgpu buffer object | |
2128 | * | |
8843dbbb | 2129 | * Mark @bo as invalid. |
d38ceaf9 AD |
2130 | */ |
2131 | void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev, | |
2132 | struct amdgpu_bo *bo) | |
2133 | { | |
2134 | struct amdgpu_bo_va *bo_va; | |
2135 | ||
2136 | list_for_each_entry(bo_va, &bo->va, bo_list) { | |
7fc11959 CK |
2137 | spin_lock(&bo_va->vm->status_lock); |
2138 | if (list_empty(&bo_va->vm_status)) | |
d38ceaf9 | 2139 | list_add(&bo_va->vm_status, &bo_va->vm->invalidated); |
7fc11959 | 2140 | spin_unlock(&bo_va->vm->status_lock); |
d38ceaf9 AD |
2141 | } |
2142 | } | |
2143 | ||
bab4fee7 JZ |
2144 | static uint32_t amdgpu_vm_get_block_size(uint64_t vm_size) |
2145 | { | |
2146 | /* Total bits covered by PD + PTs */ | |
2147 | unsigned bits = ilog2(vm_size) + 18; | |
2148 | ||
2149 | /* Make sure the PD is 4K in size up to 8GB address space. | |
2150 | Above that split equal between PD and PTs */ | |
2151 | if (vm_size <= 8) | |
2152 | return (bits - 9); | |
2153 | else | |
2154 | return ((bits + 3) / 2); | |
2155 | } | |
2156 | ||
2157 | /** | |
2158 | * amdgpu_vm_adjust_size - adjust vm size and block size | |
2159 | * | |
2160 | * @adev: amdgpu_device pointer | |
2161 | * @vm_size: the default vm size if it's set auto | |
2162 | */ | |
2163 | void amdgpu_vm_adjust_size(struct amdgpu_device *adev, uint64_t vm_size) | |
2164 | { | |
2165 | /* adjust vm size firstly */ | |
2166 | if (amdgpu_vm_size == -1) | |
2167 | adev->vm_manager.vm_size = vm_size; | |
2168 | else | |
2169 | adev->vm_manager.vm_size = amdgpu_vm_size; | |
2170 | ||
2171 | /* block size depends on vm size */ | |
2172 | if (amdgpu_vm_block_size == -1) | |
2173 | adev->vm_manager.block_size = | |
2174 | amdgpu_vm_get_block_size(adev->vm_manager.vm_size); | |
2175 | else | |
2176 | adev->vm_manager.block_size = amdgpu_vm_block_size; | |
2177 | ||
2178 | DRM_INFO("vm size is %llu GB, block size is %u-bit\n", | |
2179 | adev->vm_manager.vm_size, adev->vm_manager.block_size); | |
2180 | } | |
2181 | ||
d38ceaf9 AD |
2182 | /** |
2183 | * amdgpu_vm_init - initialize a vm instance | |
2184 | * | |
2185 | * @adev: amdgpu_device pointer | |
2186 | * @vm: requested vm | |
2187 | * | |
8843dbbb | 2188 | * Init @vm fields. |
d38ceaf9 AD |
2189 | */ |
2190 | int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm) | |
2191 | { | |
2192 | const unsigned align = min(AMDGPU_VM_PTB_ALIGN_SIZE, | |
36b32a68 | 2193 | AMDGPU_VM_PTE_COUNT(adev) * 8); |
2d55e45a CK |
2194 | unsigned ring_instance; |
2195 | struct amdgpu_ring *ring; | |
2bd9ccfa | 2196 | struct amd_sched_rq *rq; |
36bbf3bf | 2197 | int r, i; |
d38ceaf9 | 2198 | |
d38ceaf9 | 2199 | vm->va = RB_ROOT; |
031e2983 | 2200 | vm->client_id = atomic64_inc_return(&adev->vm_manager.client_counter); |
36bbf3bf CZ |
2201 | for (i = 0; i < AMDGPU_MAX_VMHUBS; i++) |
2202 | vm->reserved_vmid[i] = NULL; | |
d38ceaf9 AD |
2203 | spin_lock_init(&vm->status_lock); |
2204 | INIT_LIST_HEAD(&vm->invalidated); | |
7fc11959 | 2205 | INIT_LIST_HEAD(&vm->cleared); |
d38ceaf9 | 2206 | INIT_LIST_HEAD(&vm->freed); |
20250215 | 2207 | |
2bd9ccfa | 2208 | /* create scheduler entity for page table updates */ |
2d55e45a CK |
2209 | |
2210 | ring_instance = atomic_inc_return(&adev->vm_manager.vm_pte_next_ring); | |
2211 | ring_instance %= adev->vm_manager.vm_pte_num_rings; | |
2212 | ring = adev->vm_manager.vm_pte_rings[ring_instance]; | |
2bd9ccfa CK |
2213 | rq = &ring->sched.sched_rq[AMD_SCHED_PRIORITY_KERNEL]; |
2214 | r = amd_sched_entity_init(&ring->sched, &vm->entity, | |
2215 | rq, amdgpu_sched_jobs); | |
2216 | if (r) | |
f566ceb1 | 2217 | return r; |
2bd9ccfa | 2218 | |
a24960f3 | 2219 | vm->last_dir_update = NULL; |
05906dec | 2220 | |
f566ceb1 | 2221 | r = amdgpu_bo_create(adev, amdgpu_vm_bo_size(adev, 0), align, true, |
857d913d | 2222 | AMDGPU_GEM_DOMAIN_VRAM, |
1baa439f | 2223 | AMDGPU_GEM_CREATE_NO_CPU_ACCESS | |
03f48dd5 | 2224 | AMDGPU_GEM_CREATE_SHADOW | |
617859e0 CK |
2225 | AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS | |
2226 | AMDGPU_GEM_CREATE_VRAM_CLEARED, | |
67003a15 | 2227 | NULL, NULL, &vm->root.bo); |
d38ceaf9 | 2228 | if (r) |
2bd9ccfa CK |
2229 | goto error_free_sched_entity; |
2230 | ||
67003a15 | 2231 | r = amdgpu_bo_reserve(vm->root.bo, false); |
2bd9ccfa | 2232 | if (r) |
67003a15 | 2233 | goto error_free_root; |
2bd9ccfa | 2234 | |
5a712a87 | 2235 | vm->last_eviction_counter = atomic64_read(&adev->num_evictions); |
67003a15 | 2236 | amdgpu_bo_unreserve(vm->root.bo); |
d38ceaf9 AD |
2237 | |
2238 | return 0; | |
2bd9ccfa | 2239 | |
67003a15 CK |
2240 | error_free_root: |
2241 | amdgpu_bo_unref(&vm->root.bo->shadow); | |
2242 | amdgpu_bo_unref(&vm->root.bo); | |
2243 | vm->root.bo = NULL; | |
2bd9ccfa CK |
2244 | |
2245 | error_free_sched_entity: | |
2246 | amd_sched_entity_fini(&ring->sched, &vm->entity); | |
2247 | ||
2248 | return r; | |
d38ceaf9 AD |
2249 | } |
2250 | ||
f566ceb1 CK |
2251 | /** |
2252 | * amdgpu_vm_free_levels - free PD/PT levels | |
2253 | * | |
2254 | * @level: PD/PT starting level to free | |
2255 | * | |
2256 | * Free the page directory or page table level and all sub levels. | |
2257 | */ | |
2258 | static void amdgpu_vm_free_levels(struct amdgpu_vm_pt *level) | |
2259 | { | |
2260 | unsigned i; | |
2261 | ||
2262 | if (level->bo) { | |
2263 | amdgpu_bo_unref(&level->bo->shadow); | |
2264 | amdgpu_bo_unref(&level->bo); | |
2265 | } | |
2266 | ||
2267 | if (level->entries) | |
2268 | for (i = 0; i <= level->last_entry_used; i++) | |
2269 | amdgpu_vm_free_levels(&level->entries[i]); | |
2270 | ||
2271 | drm_free_large(level->entries); | |
2272 | } | |
2273 | ||
d38ceaf9 AD |
2274 | /** |
2275 | * amdgpu_vm_fini - tear down a vm instance | |
2276 | * | |
2277 | * @adev: amdgpu_device pointer | |
2278 | * @vm: requested vm | |
2279 | * | |
8843dbbb | 2280 | * Tear down @vm. |
d38ceaf9 AD |
2281 | * Unbind the VM and remove all bos from the vm bo list |
2282 | */ | |
2283 | void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm) | |
2284 | { | |
2285 | struct amdgpu_bo_va_mapping *mapping, *tmp; | |
4388fc2a | 2286 | bool prt_fini_needed = !!adev->gart.gart_funcs->set_prt; |
36bbf3bf | 2287 | int i; |
d38ceaf9 | 2288 | |
2d55e45a | 2289 | amd_sched_entity_fini(vm->entity.sched, &vm->entity); |
2bd9ccfa | 2290 | |
d38ceaf9 AD |
2291 | if (!RB_EMPTY_ROOT(&vm->va)) { |
2292 | dev_err(adev->dev, "still active bo inside vm\n"); | |
2293 | } | |
a9f87f64 | 2294 | rbtree_postorder_for_each_entry_safe(mapping, tmp, &vm->va, rb) { |
d38ceaf9 | 2295 | list_del(&mapping->list); |
a9f87f64 | 2296 | amdgpu_vm_it_remove(mapping, &vm->va); |
d38ceaf9 AD |
2297 | kfree(mapping); |
2298 | } | |
2299 | list_for_each_entry_safe(mapping, tmp, &vm->freed, list) { | |
4388fc2a | 2300 | if (mapping->flags & AMDGPU_PTE_PRT && prt_fini_needed) { |
451bc8eb | 2301 | amdgpu_vm_prt_fini(adev, vm); |
4388fc2a | 2302 | prt_fini_needed = false; |
451bc8eb | 2303 | } |
284710fa | 2304 | |
d38ceaf9 | 2305 | list_del(&mapping->list); |
451bc8eb | 2306 | amdgpu_vm_free_mapping(adev, vm, mapping, NULL); |
d38ceaf9 AD |
2307 | } |
2308 | ||
f566ceb1 | 2309 | amdgpu_vm_free_levels(&vm->root); |
a24960f3 | 2310 | dma_fence_put(vm->last_dir_update); |
1e9ef26f CZ |
2311 | for (i = 0; i < AMDGPU_MAX_VMHUBS; i++) |
2312 | amdgpu_vm_free_reserved_vmid(adev, vm, i); | |
d38ceaf9 | 2313 | } |
ea89f8c9 | 2314 | |
a9a78b32 CK |
2315 | /** |
2316 | * amdgpu_vm_manager_init - init the VM manager | |
2317 | * | |
2318 | * @adev: amdgpu_device pointer | |
2319 | * | |
2320 | * Initialize the VM manager structures | |
2321 | */ | |
2322 | void amdgpu_vm_manager_init(struct amdgpu_device *adev) | |
2323 | { | |
7645670d CK |
2324 | unsigned i, j; |
2325 | ||
2326 | for (i = 0; i < AMDGPU_MAX_VMHUBS; ++i) { | |
2327 | struct amdgpu_vm_id_manager *id_mgr = | |
2328 | &adev->vm_manager.id_mgr[i]; | |
a9a78b32 | 2329 | |
7645670d CK |
2330 | mutex_init(&id_mgr->lock); |
2331 | INIT_LIST_HEAD(&id_mgr->ids_lru); | |
c3505770 | 2332 | atomic_set(&id_mgr->reserved_vmid_num, 0); |
a9a78b32 | 2333 | |
7645670d CK |
2334 | /* skip over VMID 0, since it is the system VM */ |
2335 | for (j = 1; j < id_mgr->num_ids; ++j) { | |
2336 | amdgpu_vm_reset_id(adev, i, j); | |
2337 | amdgpu_sync_create(&id_mgr->ids[i].active); | |
2338 | list_add_tail(&id_mgr->ids[j].list, &id_mgr->ids_lru); | |
2339 | } | |
971fe9a9 | 2340 | } |
2d55e45a | 2341 | |
f54d1867 CW |
2342 | adev->vm_manager.fence_context = |
2343 | dma_fence_context_alloc(AMDGPU_MAX_RINGS); | |
1fbb2e92 CK |
2344 | for (i = 0; i < AMDGPU_MAX_RINGS; ++i) |
2345 | adev->vm_manager.seqno[i] = 0; | |
2346 | ||
2d55e45a | 2347 | atomic_set(&adev->vm_manager.vm_pte_next_ring, 0); |
b1c8a81f | 2348 | atomic64_set(&adev->vm_manager.client_counter, 0); |
284710fa | 2349 | spin_lock_init(&adev->vm_manager.prt_lock); |
451bc8eb | 2350 | atomic_set(&adev->vm_manager.num_prt_users, 0); |
a9a78b32 CK |
2351 | } |
2352 | ||
ea89f8c9 CK |
2353 | /** |
2354 | * amdgpu_vm_manager_fini - cleanup VM manager | |
2355 | * | |
2356 | * @adev: amdgpu_device pointer | |
2357 | * | |
2358 | * Cleanup the VM manager and free resources. | |
2359 | */ | |
2360 | void amdgpu_vm_manager_fini(struct amdgpu_device *adev) | |
2361 | { | |
7645670d | 2362 | unsigned i, j; |
ea89f8c9 | 2363 | |
7645670d CK |
2364 | for (i = 0; i < AMDGPU_MAX_VMHUBS; ++i) { |
2365 | struct amdgpu_vm_id_manager *id_mgr = | |
2366 | &adev->vm_manager.id_mgr[i]; | |
bcb1ba35 | 2367 | |
7645670d CK |
2368 | mutex_destroy(&id_mgr->lock); |
2369 | for (j = 0; j < AMDGPU_NUM_VM; ++j) { | |
2370 | struct amdgpu_vm_id *id = &id_mgr->ids[j]; | |
2371 | ||
2372 | amdgpu_sync_free(&id->active); | |
2373 | dma_fence_put(id->flushed_updates); | |
2374 | dma_fence_put(id->last_flush); | |
2375 | } | |
bcb1ba35 | 2376 | } |
ea89f8c9 | 2377 | } |
cfbcacf4 CZ |
2378 | |
2379 | int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp) | |
2380 | { | |
2381 | union drm_amdgpu_vm *args = data; | |
1e9ef26f CZ |
2382 | struct amdgpu_device *adev = dev->dev_private; |
2383 | struct amdgpu_fpriv *fpriv = filp->driver_priv; | |
2384 | int r; | |
cfbcacf4 CZ |
2385 | |
2386 | switch (args->in.op) { | |
2387 | case AMDGPU_VM_OP_RESERVE_VMID: | |
1e9ef26f CZ |
2388 | /* current, we only have requirement to reserve vmid from gfxhub */ |
2389 | r = amdgpu_vm_alloc_reserved_vmid(adev, &fpriv->vm, | |
2390 | AMDGPU_GFXHUB); | |
2391 | if (r) | |
2392 | return r; | |
2393 | break; | |
cfbcacf4 | 2394 | case AMDGPU_VM_OP_UNRESERVE_VMID: |
1e9ef26f | 2395 | amdgpu_vm_free_reserved_vmid(adev, &fpriv->vm, AMDGPU_GFXHUB); |
cfbcacf4 CZ |
2396 | break; |
2397 | default: | |
2398 | return -EINVAL; | |
2399 | } | |
2400 | ||
2401 | return 0; | |
2402 | } |