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