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