]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/gpu/drm/amd/amdgpu/amdgpu_vm_sdma.c
drm/amdgpu: move VM table mapping into the backend as well
[linux.git] / drivers / gpu / drm / amd / amdgpu / amdgpu_vm_sdma.c
1 /*
2  * Copyright 2019 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  */
22
23 #include "amdgpu_vm.h"
24 #include "amdgpu_job.h"
25 #include "amdgpu_object.h"
26 #include "amdgpu_trace.h"
27
28 #define AMDGPU_VM_SDMA_MIN_NUM_DW       256u
29 #define AMDGPU_VM_SDMA_MAX_NUM_DW       (16u * 1024u)
30
31 /**
32  * amdgpu_vm_sdma_map_table - make sure new PDs/PTs are GTT mapped
33  *
34  * @table: newly allocated or validated PD/PT
35  */
36 static int amdgpu_vm_sdma_map_table(struct amdgpu_bo *table)
37 {
38         int r;
39
40         r = amdgpu_ttm_alloc_gart(&table->tbo);
41         if (r)
42                 return r;
43
44         if (table->shadow)
45                 r = amdgpu_ttm_alloc_gart(&table->shadow->tbo);
46
47         return r;
48 }
49
50 /**
51  * amdgpu_vm_sdma_prepare - prepare SDMA command submission
52  *
53  * @p: see amdgpu_vm_update_params definition
54  * @owner: owner we need to sync to
55  * @exclusive: exclusive move fence we need to sync to
56  *
57  * Returns:
58  * Negativ errno, 0 for success.
59  */
60 static int amdgpu_vm_sdma_prepare(struct amdgpu_vm_update_params *p,
61                                   void *owner, struct dma_fence *exclusive)
62 {
63         struct amdgpu_bo *root = p->vm->root.base.bo;
64         unsigned int ndw = AMDGPU_VM_SDMA_MIN_NUM_DW;
65         int r;
66
67         r = amdgpu_job_alloc_with_ib(p->adev, ndw * 4, &p->job);
68         if (r)
69                 return r;
70
71         r = amdgpu_sync_fence(p->adev, &p->job->sync, exclusive, false);
72         if (r)
73                 return r;
74
75         r = amdgpu_sync_resv(p->adev, &p->job->sync, root->tbo.resv,
76                              owner, false);
77         if (r)
78                 return r;
79
80         p->num_dw_left = ndw;
81         p->ib = &p->job->ibs[0];
82         return 0;
83 }
84
85 /**
86  * amdgpu_vm_sdma_commit - commit SDMA command submission
87  *
88  * @p: see amdgpu_vm_update_params definition
89  * @fence: resulting fence
90  *
91  * Returns:
92  * Negativ errno, 0 for success.
93  */
94 static int amdgpu_vm_sdma_commit(struct amdgpu_vm_update_params *p,
95                                  struct dma_fence **fence)
96 {
97         struct amdgpu_bo *root = p->vm->root.base.bo;
98         struct amdgpu_ring *ring;
99         struct dma_fence *f;
100         int r;
101
102         ring = container_of(p->vm->entity.rq->sched, struct amdgpu_ring, sched);
103
104         WARN_ON(p->ib->length_dw == 0);
105         amdgpu_ring_pad_ib(ring, p->ib);
106         WARN_ON(p->ib->length_dw > p->num_dw_left);
107         r = amdgpu_job_submit(p->job, &p->vm->entity,
108                               AMDGPU_FENCE_OWNER_VM, &f);
109         if (r)
110                 goto error;
111
112         amdgpu_bo_fence(root, f, true);
113         if (fence)
114                 swap(*fence, f);
115         dma_fence_put(f);
116         return 0;
117
118 error:
119         amdgpu_job_free(p->job);
120         return r;
121 }
122
123
124 /**
125  * amdgpu_vm_sdma_copy_ptes - copy the PTEs from mapping
126  *
127  * @p: see amdgpu_vm_update_params definition
128  * @bo: PD/PT to update
129  * @pe: addr of the page entry
130  * @count: number of page entries to copy
131  *
132  * Traces the parameters and calls the DMA function to copy the PTEs.
133  */
134 static void amdgpu_vm_sdma_copy_ptes(struct amdgpu_vm_update_params *p,
135                                      struct amdgpu_bo *bo, uint64_t pe,
136                                      unsigned count)
137 {
138         uint64_t src = p->ib->gpu_addr;
139
140         src += p->num_dw_left * 4;
141
142         pe += amdgpu_bo_gpu_offset(bo);
143         trace_amdgpu_vm_copy_ptes(pe, src, count);
144
145         amdgpu_vm_copy_pte(p->adev, p->ib, pe, src, count);
146 }
147
148 /**
149  * amdgpu_vm_sdma_set_ptes - helper to call the right asic function
150  *
151  * @p: see amdgpu_vm_update_params definition
152  * @bo: PD/PT to update
153  * @pe: addr of the page entry
154  * @addr: dst addr to write into pe
155  * @count: number of page entries to update
156  * @incr: increase next addr by incr bytes
157  * @flags: hw access flags
158  *
159  * Traces the parameters and calls the right asic functions
160  * to setup the page table using the DMA.
161  */
162 static void amdgpu_vm_sdma_set_ptes(struct amdgpu_vm_update_params *p,
163                                     struct amdgpu_bo *bo, uint64_t pe,
164                                     uint64_t addr, unsigned count,
165                                     uint32_t incr, uint64_t flags)
166 {
167         pe += amdgpu_bo_gpu_offset(bo);
168         trace_amdgpu_vm_set_ptes(pe, addr, count, incr, flags);
169         if (count < 3) {
170                 amdgpu_vm_write_pte(p->adev, p->ib, pe, addr | flags,
171                                     count, incr);
172         } else {
173                 amdgpu_vm_set_pte_pde(p->adev, p->ib, pe, addr,
174                                       count, incr, flags);
175         }
176 }
177
178 /**
179  * amdgpu_vm_sdma_update - execute VM update
180  *
181  * @p: see amdgpu_vm_update_params definition
182  * @bo: PD/PT to update
183  * @pe: addr of the page entry
184  * @addr: dst addr to write into pe
185  * @count: number of page entries to update
186  * @incr: increase next addr by incr bytes
187  * @flags: hw access flags
188  *
189  * Reserve space in the IB, setup mapping buffer on demand and write commands to
190  * the IB.
191  */
192 static int amdgpu_vm_sdma_update(struct amdgpu_vm_update_params *p,
193                                  struct amdgpu_bo *bo, uint64_t pe,
194                                  uint64_t addr, unsigned count, uint32_t incr,
195                                  uint64_t flags)
196 {
197         unsigned int i, ndw, nptes;
198         uint64_t *pte;
199         int r;
200
201         do {
202                 ndw = p->num_dw_left;
203                 ndw -= p->ib->length_dw;
204
205                 if (ndw < 32) {
206                         r = amdgpu_vm_sdma_commit(p, NULL);
207                         if (r)
208                                 return r;
209
210                         /* estimate how many dw we need */
211                         ndw = 32;
212                         if (p->pages_addr)
213                                 ndw += count * 2;
214                         ndw = max(ndw, AMDGPU_VM_SDMA_MIN_NUM_DW);
215                         ndw = min(ndw, AMDGPU_VM_SDMA_MAX_NUM_DW);
216
217                         r = amdgpu_job_alloc_with_ib(p->adev, ndw * 4, &p->job);
218                         if (r)
219                                 return r;
220
221                         p->num_dw_left = ndw;
222                         p->ib = &p->job->ibs[0];
223                 }
224
225                 if (!p->pages_addr) {
226                         /* set page commands needed */
227                         if (bo->shadow)
228                                 amdgpu_vm_sdma_set_ptes(p, bo->shadow, pe, addr,
229                                                         count, incr, flags);
230                         amdgpu_vm_sdma_set_ptes(p, bo, pe, addr, count,
231                                                 incr, flags);
232                         return 0;
233                 }
234
235                 /* copy commands needed */
236                 ndw -= p->adev->vm_manager.vm_pte_funcs->copy_pte_num_dw *
237                         (bo->shadow ? 2 : 1);
238
239                 /* for padding */
240                 ndw -= 7;
241
242                 nptes = min(count, ndw / 2);
243
244                 /* Put the PTEs at the end of the IB. */
245                 p->num_dw_left -= nptes * 2;
246                 pte = (uint64_t *)&(p->ib->ptr[p->num_dw_left]);
247                 for (i = 0; i < nptes; ++i, addr += incr) {
248                         pte[i] = amdgpu_vm_map_gart(p->pages_addr, addr);
249                         pte[i] |= flags;
250                 }
251
252                 if (bo->shadow)
253                         amdgpu_vm_sdma_copy_ptes(p, bo->shadow, pe, nptes);
254                 amdgpu_vm_sdma_copy_ptes(p, bo, pe, nptes);
255
256                 pe += nptes * 8;
257                 count -= nptes;
258         } while (count);
259
260         return 0;
261 }
262
263 const struct amdgpu_vm_update_funcs amdgpu_vm_sdma_funcs = {
264         .map_table = amdgpu_vm_sdma_map_table,
265         .prepare = amdgpu_vm_sdma_prepare,
266         .update = amdgpu_vm_sdma_update,
267         .commit = amdgpu_vm_sdma_commit
268 };