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