blob: 8f04857fbe1b54623c0932d88526db79fb08fef9 [file] [log] [blame]
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001/*
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 */
Christian König1fbb2e92016-06-01 10:47:36 +020028#include <linux/fence-array.h>
Alex Deucherd38ceaf2015-04-20 16:55:21 -040029#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
Christian König4ff37a82016-02-26 16:18:26 +010054/* Special value that no flush is necessary */
55#define AMDGPU_VM_NO_FLUSH (~0ll)
56
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -040057/* Local structure. Encapsulate some VM table update parameters to reduce
58 * the number of function parameters
59 */
60struct amdgpu_vm_update_params {
61 /* address where to copy page table entries from */
62 uint64_t src;
63 /* DMA addresses to use for mapping */
64 dma_addr_t *pages_addr;
65 /* indirect buffer to fill with commands */
66 struct amdgpu_ib *ib;
67};
68
Alex Deucherd38ceaf2015-04-20 16:55:21 -040069/**
70 * amdgpu_vm_num_pde - return the number of page directory entries
71 *
72 * @adev: amdgpu_device pointer
73 *
Christian König8843dbb2016-01-26 12:17:11 +010074 * Calculate the number of page directory entries.
Alex Deucherd38ceaf2015-04-20 16:55:21 -040075 */
76static unsigned amdgpu_vm_num_pdes(struct amdgpu_device *adev)
77{
78 return adev->vm_manager.max_pfn >> amdgpu_vm_block_size;
79}
80
81/**
82 * amdgpu_vm_directory_size - returns the size of the page directory in bytes
83 *
84 * @adev: amdgpu_device pointer
85 *
Christian König8843dbb2016-01-26 12:17:11 +010086 * Calculate the size of the page directory in bytes.
Alex Deucherd38ceaf2015-04-20 16:55:21 -040087 */
88static unsigned amdgpu_vm_directory_size(struct amdgpu_device *adev)
89{
90 return AMDGPU_GPU_PAGE_ALIGN(amdgpu_vm_num_pdes(adev) * 8);
91}
92
93/**
Christian König56467eb2015-12-11 15:16:32 +010094 * amdgpu_vm_get_pd_bo - add the VM PD to a validation list
Alex Deucherd38ceaf2015-04-20 16:55:21 -040095 *
96 * @vm: vm providing the BOs
Christian König3c0eea62015-12-11 14:39:05 +010097 * @validated: head of validation list
Christian König56467eb2015-12-11 15:16:32 +010098 * @entry: entry to add
Alex Deucherd38ceaf2015-04-20 16:55:21 -040099 *
100 * Add the page directory to the list of BOs to
Christian König56467eb2015-12-11 15:16:32 +0100101 * validate for command submission.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400102 */
Christian König56467eb2015-12-11 15:16:32 +0100103void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm,
104 struct list_head *validated,
105 struct amdgpu_bo_list_entry *entry)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400106{
Christian König56467eb2015-12-11 15:16:32 +0100107 entry->robj = vm->page_directory;
Christian König56467eb2015-12-11 15:16:32 +0100108 entry->priority = 0;
109 entry->tv.bo = &vm->page_directory->tbo;
110 entry->tv.shared = true;
Christian König2f568db2016-02-23 12:36:59 +0100111 entry->user_pages = NULL;
Christian König56467eb2015-12-11 15:16:32 +0100112 list_add(&entry->tv.head, validated);
113}
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400114
Christian König56467eb2015-12-11 15:16:32 +0100115/**
Christian Königee1782c2015-12-11 21:01:23 +0100116 * amdgpu_vm_get_bos - add the vm BOs to a duplicates list
Christian König56467eb2015-12-11 15:16:32 +0100117 *
118 * @vm: vm providing the BOs
Christian König3c0eea62015-12-11 14:39:05 +0100119 * @duplicates: head of duplicates list
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400120 *
Christian Königee1782c2015-12-11 21:01:23 +0100121 * Add the page directory to the BO duplicates list
122 * for command submission.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400123 */
Christian Königee1782c2015-12-11 21:01:23 +0100124void amdgpu_vm_get_pt_bos(struct amdgpu_vm *vm, struct list_head *duplicates)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400125{
Christian Königee1782c2015-12-11 21:01:23 +0100126 unsigned i;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400127
128 /* add the vm page table to the list */
Christian Königee1782c2015-12-11 21:01:23 +0100129 for (i = 0; i <= vm->max_pde_used; ++i) {
130 struct amdgpu_bo_list_entry *entry = &vm->page_tables[i].entry;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400131
Christian Königee1782c2015-12-11 21:01:23 +0100132 if (!entry->robj)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400133 continue;
134
Christian Königee1782c2015-12-11 21:01:23 +0100135 list_add(&entry->tv.head, duplicates);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400136 }
Christian Königeceb8a12016-01-11 15:35:21 +0100137
138}
139
140/**
141 * amdgpu_vm_move_pt_bos_in_lru - move the PT BOs to the LRU tail
142 *
143 * @adev: amdgpu device instance
144 * @vm: vm providing the BOs
145 *
146 * Move the PT BOs to the tail of the LRU.
147 */
148void amdgpu_vm_move_pt_bos_in_lru(struct amdgpu_device *adev,
149 struct amdgpu_vm *vm)
150{
151 struct ttm_bo_global *glob = adev->mman.bdev.glob;
152 unsigned i;
153
154 spin_lock(&glob->lru_lock);
155 for (i = 0; i <= vm->max_pde_used; ++i) {
156 struct amdgpu_bo_list_entry *entry = &vm->page_tables[i].entry;
157
158 if (!entry->robj)
159 continue;
160
161 ttm_bo_move_to_lru_tail(&entry->robj->tbo);
162 }
163 spin_unlock(&glob->lru_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400164}
165
166/**
167 * amdgpu_vm_grab_id - allocate the next free VMID
168 *
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400169 * @vm: vm to allocate id for
Christian König7f8a5292015-07-20 16:09:40 +0200170 * @ring: ring we want to submit job to
171 * @sync: sync object where we add dependencies
Christian König94dd0a42016-01-18 17:01:42 +0100172 * @fence: fence protecting ID from reuse
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400173 *
Christian König7f8a5292015-07-20 16:09:40 +0200174 * Allocate an id for the vm, adding fences to the sync obj as necessary.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400175 */
Christian König7f8a5292015-07-20 16:09:40 +0200176int amdgpu_vm_grab_id(struct amdgpu_vm *vm, struct amdgpu_ring *ring,
Christian König4ff37a82016-02-26 16:18:26 +0100177 struct amdgpu_sync *sync, struct fence *fence,
178 unsigned *vm_id, uint64_t *vm_pd_addr)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400179{
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400180 struct amdgpu_device *adev = ring->adev;
Christian König4ff37a82016-02-26 16:18:26 +0100181 struct fence *updates = sync->last_vm_update;
Christian König8d76001e2016-05-23 16:00:32 +0200182 struct amdgpu_vm_id *id, *idle;
Christian König1fbb2e92016-06-01 10:47:36 +0200183 struct fence **fences;
184 unsigned i;
185 int r = 0;
186
187 fences = kmalloc_array(sizeof(void *), adev->vm_manager.num_ids,
188 GFP_KERNEL);
189 if (!fences)
190 return -ENOMEM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400191
Christian König94dd0a42016-01-18 17:01:42 +0100192 mutex_lock(&adev->vm_manager.lock);
193
Christian König36fd7c52016-05-23 15:30:08 +0200194 /* Check if we have an idle VMID */
Christian König1fbb2e92016-06-01 10:47:36 +0200195 i = 0;
Christian König8d76001e2016-05-23 16:00:32 +0200196 list_for_each_entry(idle, &adev->vm_manager.ids_lru, list) {
Christian König1fbb2e92016-06-01 10:47:36 +0200197 fences[i] = amdgpu_sync_peek_fence(&idle->active, ring);
198 if (!fences[i])
Christian König36fd7c52016-05-23 15:30:08 +0200199 break;
Christian König1fbb2e92016-06-01 10:47:36 +0200200 ++i;
Christian König36fd7c52016-05-23 15:30:08 +0200201 }
Christian Königbcb1ba32016-03-08 15:40:11 +0100202
Christian König1fbb2e92016-06-01 10:47:36 +0200203 /* If we can't find a idle VMID to use, wait till one becomes available */
Christian König8d76001e2016-05-23 16:00:32 +0200204 if (&idle->list == &adev->vm_manager.ids_lru) {
Christian König1fbb2e92016-06-01 10:47:36 +0200205 u64 fence_context = adev->vm_manager.fence_context + ring->idx;
206 unsigned seqno = ++adev->vm_manager.seqno[ring->idx];
207 struct fence_array *array;
208 unsigned j;
Christian König8d76001e2016-05-23 16:00:32 +0200209
Christian König1fbb2e92016-06-01 10:47:36 +0200210 for (j = 0; j < i; ++j)
211 fence_get(fences[j]);
Christian König8d76001e2016-05-23 16:00:32 +0200212
Christian König1fbb2e92016-06-01 10:47:36 +0200213 array = fence_array_create(i, fences, fence_context,
214 seqno, true);
215 if (!array) {
216 for (j = 0; j < i; ++j)
217 fence_put(fences[j]);
218 kfree(fences);
219 r = -ENOMEM;
220 goto error;
221 }
Christian König8d76001e2016-05-23 16:00:32 +0200222
Christian König8d76001e2016-05-23 16:00:32 +0200223
Christian König1fbb2e92016-06-01 10:47:36 +0200224 r = amdgpu_sync_fence(ring->adev, sync, &array->base);
225 fence_put(&array->base);
226 if (r)
227 goto error;
Christian König8d76001e2016-05-23 16:00:32 +0200228
Christian König1fbb2e92016-06-01 10:47:36 +0200229 mutex_unlock(&adev->vm_manager.lock);
230 return 0;
Christian König8d76001e2016-05-23 16:00:32 +0200231
Christian König1fbb2e92016-06-01 10:47:36 +0200232 }
233 kfree(fences);
Christian König8d76001e2016-05-23 16:00:32 +0200234
Christian König1fbb2e92016-06-01 10:47:36 +0200235 /* Check if we can use a VMID already assigned to this VM */
236 i = ring->idx;
237 do {
238 struct fence *flushed;
Christian König3dab83b2016-06-01 13:31:17 +0200239 bool same_ring = ring->idx == i;
Christian König8d76001e2016-05-23 16:00:32 +0200240
Christian König1fbb2e92016-06-01 10:47:36 +0200241 id = vm->ids[i++];
242 if (i == AMDGPU_MAX_RINGS)
243 i = 0;
244
245 /* Check all the prerequisites to using this VMID */
246 if (!id)
247 continue;
248
249 if (atomic64_read(&id->owner) != vm->client_id)
250 continue;
251
Christian König281d1442016-06-15 13:44:04 +0200252 if (*vm_pd_addr != id->pd_gpu_addr)
Christian König1fbb2e92016-06-01 10:47:36 +0200253 continue;
254
Christian König3dab83b2016-06-01 13:31:17 +0200255 if (!same_ring &&
Christian König1fbb2e92016-06-01 10:47:36 +0200256 (!id->last_flush || !fence_is_signaled(id->last_flush)))
257 continue;
258
259 flushed = id->flushed_updates;
260 if (updates &&
261 (!flushed || fence_is_later(updates, flushed)))
262 continue;
263
Christian König3dab83b2016-06-01 13:31:17 +0200264 /* Good we can use this VMID. Remember this submission as
265 * user of the VMID.
266 */
Christian König1fbb2e92016-06-01 10:47:36 +0200267 r = amdgpu_sync_fence(ring->adev, &id->active, fence);
268 if (r)
269 goto error;
Christian König8d76001e2016-05-23 16:00:32 +0200270
Christian König1fbb2e92016-06-01 10:47:36 +0200271 list_move_tail(&id->list, &adev->vm_manager.ids_lru);
272 vm->ids[ring->idx] = id;
Christian König8d76001e2016-05-23 16:00:32 +0200273
Christian König1fbb2e92016-06-01 10:47:36 +0200274 *vm_id = id - adev->vm_manager.ids;
275 *vm_pd_addr = AMDGPU_VM_NO_FLUSH;
276 trace_amdgpu_vm_grab_id(vm, ring->idx, *vm_id, *vm_pd_addr);
Christian König8d76001e2016-05-23 16:00:32 +0200277
Christian König1fbb2e92016-06-01 10:47:36 +0200278 mutex_unlock(&adev->vm_manager.lock);
279 return 0;
Christian König8d76001e2016-05-23 16:00:32 +0200280
Christian König1fbb2e92016-06-01 10:47:36 +0200281 } while (i != ring->idx);
Chunming Zhou8e9fbeb2016-03-17 11:41:37 +0800282
Christian König1fbb2e92016-06-01 10:47:36 +0200283 /* Still no ID to use? Then use the idle one found earlier */
284 id = idle;
285
286 /* Remember this submission as user of the VMID */
287 r = amdgpu_sync_fence(ring->adev, &id->active, fence);
Christian König832a9022016-02-15 12:33:02 +0100288 if (r)
289 goto error;
Christian König4ff37a82016-02-26 16:18:26 +0100290
Christian König832a9022016-02-15 12:33:02 +0100291 fence_put(id->first);
292 id->first = fence_get(fence);
Christian König4ff37a82016-02-26 16:18:26 +0100293
Christian König41d9eb22016-03-01 16:46:18 +0100294 fence_put(id->last_flush);
295 id->last_flush = NULL;
296
Christian König832a9022016-02-15 12:33:02 +0100297 fence_put(id->flushed_updates);
298 id->flushed_updates = fence_get(updates);
Christian König4ff37a82016-02-26 16:18:26 +0100299
Christian König281d1442016-06-15 13:44:04 +0200300 id->pd_gpu_addr = *vm_pd_addr;
Christian König4ff37a82016-02-26 16:18:26 +0100301
Christian König832a9022016-02-15 12:33:02 +0100302 list_move_tail(&id->list, &adev->vm_manager.ids_lru);
Christian König0ea54b92016-05-04 10:20:01 +0200303 atomic64_set(&id->owner, vm->client_id);
Christian König832a9022016-02-15 12:33:02 +0100304 vm->ids[ring->idx] = id;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400305
Christian König832a9022016-02-15 12:33:02 +0100306 *vm_id = id - adev->vm_manager.ids;
Christian König832a9022016-02-15 12:33:02 +0100307 trace_amdgpu_vm_grab_id(vm, ring->idx, *vm_id, *vm_pd_addr);
308
309error:
Christian König94dd0a42016-01-18 17:01:42 +0100310 mutex_unlock(&adev->vm_manager.lock);
Christian Königa9a78b32016-01-21 10:19:11 +0100311 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400312}
313
Alex Deucher93dcc372016-06-17 17:05:15 -0400314static bool amdgpu_vm_ring_has_compute_vm_bug(struct amdgpu_ring *ring)
315{
316 struct amdgpu_device *adev = ring->adev;
317 const struct amdgpu_ip_block_version *ip_block;
318
319 if (ring->type != AMDGPU_RING_TYPE_COMPUTE)
320 /* only compute rings */
321 return false;
322
323 ip_block = amdgpu_get_ip_block(adev, AMD_IP_BLOCK_TYPE_GFX);
324 if (!ip_block)
325 return false;
326
327 if (ip_block->major <= 7) {
328 /* gfx7 has no workaround */
329 return true;
330 } else if (ip_block->major == 8) {
331 if (adev->gfx.mec_fw_version >= 673)
332 /* gfx8 is fixed in MEC firmware 673 */
333 return false;
334 else
335 return true;
336 }
337 return false;
338}
339
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400340/**
341 * amdgpu_vm_flush - hardware flush the vm
342 *
343 * @ring: ring to use for flush
Christian Königcffadc82016-03-01 13:34:49 +0100344 * @vm_id: vmid number to use
Christian König4ff37a82016-02-26 16:18:26 +0100345 * @pd_addr: address of the page directory
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400346 *
Christian König4ff37a82016-02-26 16:18:26 +0100347 * Emit a VM flush when it is necessary.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400348 */
Christian König41d9eb22016-03-01 16:46:18 +0100349int amdgpu_vm_flush(struct amdgpu_ring *ring,
350 unsigned vm_id, uint64_t pd_addr,
351 uint32_t gds_base, uint32_t gds_size,
352 uint32_t gws_base, uint32_t gws_size,
353 uint32_t oa_base, uint32_t oa_size)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400354{
Christian König971fe9a92016-03-01 15:09:25 +0100355 struct amdgpu_device *adev = ring->adev;
Christian Königbcb1ba32016-03-08 15:40:11 +0100356 struct amdgpu_vm_id *id = &adev->vm_manager.ids[vm_id];
Christian Königd564a062016-03-01 15:51:53 +0100357 bool gds_switch_needed = ring->funcs->emit_gds_switch && (
Christian Königbcb1ba32016-03-08 15:40:11 +0100358 id->gds_base != gds_base ||
359 id->gds_size != gds_size ||
360 id->gws_base != gws_base ||
361 id->gws_size != gws_size ||
362 id->oa_base != oa_base ||
363 id->oa_size != oa_size);
Christian König41d9eb22016-03-01 16:46:18 +0100364 int r;
Christian Königd564a062016-03-01 15:51:53 +0100365
366 if (ring->funcs->emit_pipeline_sync && (
Chunming Zhoufe707662016-04-27 18:07:41 +0800367 pd_addr != AMDGPU_VM_NO_FLUSH || gds_switch_needed ||
Alex Deucher93dcc372016-06-17 17:05:15 -0400368 amdgpu_vm_ring_has_compute_vm_bug(ring)))
Christian Königd564a062016-03-01 15:51:53 +0100369 amdgpu_ring_emit_pipeline_sync(ring);
Christian König971fe9a92016-03-01 15:09:25 +0100370
Monk Liuc5637832016-04-19 20:11:32 +0800371 if (ring->funcs->emit_vm_flush &&
372 pd_addr != AMDGPU_VM_NO_FLUSH) {
Christian König41d9eb22016-03-01 16:46:18 +0100373 struct fence *fence;
374
Christian Königcffadc82016-03-01 13:34:49 +0100375 trace_amdgpu_vm_flush(pd_addr, ring->idx, vm_id);
376 amdgpu_ring_emit_vm_flush(ring, vm_id, pd_addr);
Christian König41d9eb22016-03-01 16:46:18 +0100377
Christian König3dab83b2016-06-01 13:31:17 +0200378 r = amdgpu_fence_emit(ring, &fence);
379 if (r)
380 return r;
381
Christian König41d9eb22016-03-01 16:46:18 +0100382 mutex_lock(&adev->vm_manager.lock);
Christian König3dab83b2016-06-01 13:31:17 +0200383 fence_put(id->last_flush);
384 id->last_flush = fence;
Christian König41d9eb22016-03-01 16:46:18 +0100385 mutex_unlock(&adev->vm_manager.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400386 }
Christian Königcffadc82016-03-01 13:34:49 +0100387
Christian Königd564a062016-03-01 15:51:53 +0100388 if (gds_switch_needed) {
Christian Königbcb1ba32016-03-08 15:40:11 +0100389 id->gds_base = gds_base;
390 id->gds_size = gds_size;
391 id->gws_base = gws_base;
392 id->gws_size = gws_size;
393 id->oa_base = oa_base;
394 id->oa_size = oa_size;
Christian Königcffadc82016-03-01 13:34:49 +0100395 amdgpu_ring_emit_gds_switch(ring, vm_id,
396 gds_base, gds_size,
397 gws_base, gws_size,
398 oa_base, oa_size);
Christian König971fe9a92016-03-01 15:09:25 +0100399 }
Christian König41d9eb22016-03-01 16:46:18 +0100400
401 return 0;
Christian König971fe9a92016-03-01 15:09:25 +0100402}
403
404/**
405 * amdgpu_vm_reset_id - reset VMID to zero
406 *
407 * @adev: amdgpu device structure
408 * @vm_id: vmid number to use
409 *
410 * Reset saved GDW, GWS and OA to force switch on next flush.
411 */
412void amdgpu_vm_reset_id(struct amdgpu_device *adev, unsigned vm_id)
413{
Christian Königbcb1ba32016-03-08 15:40:11 +0100414 struct amdgpu_vm_id *id = &adev->vm_manager.ids[vm_id];
Christian König971fe9a92016-03-01 15:09:25 +0100415
Christian Königbcb1ba32016-03-08 15:40:11 +0100416 id->gds_base = 0;
417 id->gds_size = 0;
418 id->gws_base = 0;
419 id->gws_size = 0;
420 id->oa_base = 0;
421 id->oa_size = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400422}
423
424/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400425 * amdgpu_vm_bo_find - find the bo_va for a specific vm & bo
426 *
427 * @vm: requested vm
428 * @bo: requested buffer object
429 *
Christian König8843dbb2016-01-26 12:17:11 +0100430 * Find @bo inside the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400431 * Search inside the @bos vm list for the requested vm
432 * Returns the found bo_va or NULL if none is found
433 *
434 * Object has to be reserved!
435 */
436struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
437 struct amdgpu_bo *bo)
438{
439 struct amdgpu_bo_va *bo_va;
440
441 list_for_each_entry(bo_va, &bo->va, bo_list) {
442 if (bo_va->vm == vm) {
443 return bo_va;
444 }
445 }
446 return NULL;
447}
448
449/**
450 * amdgpu_vm_update_pages - helper to call the right asic function
451 *
452 * @adev: amdgpu_device pointer
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400453 * @vm_update_params: see amdgpu_vm_update_params definition
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400454 * @pe: addr of the page entry
455 * @addr: dst addr to write into pe
456 * @count: number of page entries to update
457 * @incr: increase next addr by incr bytes
458 * @flags: hw access flags
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400459 *
460 * Traces the parameters and calls the right asic functions
461 * to setup the page table using the DMA.
462 */
463static void amdgpu_vm_update_pages(struct amdgpu_device *adev,
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400464 struct amdgpu_vm_update_params
465 *vm_update_params,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400466 uint64_t pe, uint64_t addr,
467 unsigned count, uint32_t incr,
Christian König9ab21462015-11-30 14:19:26 +0100468 uint32_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400469{
470 trace_amdgpu_vm_set_page(pe, addr, count, incr, flags);
471
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400472 if (vm_update_params->src) {
473 amdgpu_vm_copy_pte(adev, vm_update_params->ib,
474 pe, (vm_update_params->src + (addr >> 12) * 8), count);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400475
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400476 } else if (vm_update_params->pages_addr) {
477 amdgpu_vm_write_pte(adev, vm_update_params->ib,
478 vm_update_params->pages_addr,
479 pe, addr, count, incr, flags);
Christian Königb07c9d22015-11-30 13:26:07 +0100480
481 } else if (count < 3) {
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400482 amdgpu_vm_write_pte(adev, vm_update_params->ib, NULL, pe, addr,
Christian Königb07c9d22015-11-30 13:26:07 +0100483 count, incr, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400484
485 } else {
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400486 amdgpu_vm_set_pte_pde(adev, vm_update_params->ib, pe, addr,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400487 count, incr, flags);
488 }
489}
490
491/**
492 * amdgpu_vm_clear_bo - initially clear the page dir/table
493 *
494 * @adev: amdgpu_device pointer
495 * @bo: bo to clear
Chunming Zhouef9f0a82015-11-13 13:43:22 +0800496 *
497 * need to reserve bo first before calling it.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400498 */
499static int amdgpu_vm_clear_bo(struct amdgpu_device *adev,
Christian König2bd9ccf2016-02-01 12:53:58 +0100500 struct amdgpu_vm *vm,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400501 struct amdgpu_bo *bo)
502{
Christian König2d55e452016-02-08 17:37:38 +0100503 struct amdgpu_ring *ring;
Chunming Zhou4af9f072015-08-03 12:57:31 +0800504 struct fence *fence = NULL;
Christian Königd71518b2016-02-01 12:20:25 +0100505 struct amdgpu_job *job;
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400506 struct amdgpu_vm_update_params vm_update_params;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400507 unsigned entries;
508 uint64_t addr;
509 int r;
510
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400511 memset(&vm_update_params, 0, sizeof(vm_update_params));
Christian König2d55e452016-02-08 17:37:38 +0100512 ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
513
monk.liuca952612015-05-25 14:44:05 +0800514 r = reservation_object_reserve_shared(bo->tbo.resv);
515 if (r)
516 return r;
517
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400518 r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
519 if (r)
Chunming Zhouef9f0a82015-11-13 13:43:22 +0800520 goto error;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400521
522 addr = amdgpu_bo_gpu_offset(bo);
523 entries = amdgpu_bo_size(bo) / 8;
524
Christian Königd71518b2016-02-01 12:20:25 +0100525 r = amdgpu_job_alloc_with_ib(adev, 64, &job);
526 if (r)
Chunming Zhouef9f0a82015-11-13 13:43:22 +0800527 goto error;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400528
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400529 vm_update_params.ib = &job->ibs[0];
530 amdgpu_vm_update_pages(adev, &vm_update_params, addr, 0, entries,
Christian Königd71518b2016-02-01 12:20:25 +0100531 0, 0);
532 amdgpu_ring_pad_ib(ring, &job->ibs[0]);
533
534 WARN_ON(job->ibs[0].length_dw > 64);
Christian König2bd9ccf2016-02-01 12:53:58 +0100535 r = amdgpu_job_submit(job, ring, &vm->entity,
536 AMDGPU_FENCE_OWNER_VM, &fence);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400537 if (r)
538 goto error_free;
539
Christian Königd71518b2016-02-01 12:20:25 +0100540 amdgpu_bo_fence(bo, fence, true);
Chunming Zhou281b4222015-08-12 12:58:31 +0800541 fence_put(fence);
Chunming Zhoucadf97b2016-01-15 11:25:00 +0800542 return 0;
Chunming Zhouef9f0a82015-11-13 13:43:22 +0800543
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400544error_free:
Christian Königd71518b2016-02-01 12:20:25 +0100545 amdgpu_job_free(job);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400546
Chunming Zhouef9f0a82015-11-13 13:43:22 +0800547error:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400548 return r;
549}
550
551/**
Christian Königb07c9d22015-11-30 13:26:07 +0100552 * amdgpu_vm_map_gart - Resolve gart mapping of addr
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400553 *
Christian Königb07c9d22015-11-30 13:26:07 +0100554 * @pages_addr: optional DMA address to use for lookup
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400555 * @addr: the unmapped addr
556 *
557 * Look up the physical address of the page that the pte resolves
Christian Königb07c9d22015-11-30 13:26:07 +0100558 * to and return the pointer for the page table entry.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400559 */
Christian Königb07c9d22015-11-30 13:26:07 +0100560uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t addr)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400561{
562 uint64_t result;
563
Christian Königb07c9d22015-11-30 13:26:07 +0100564 if (pages_addr) {
565 /* page table offset */
566 result = pages_addr[addr >> PAGE_SHIFT];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400567
Christian Königb07c9d22015-11-30 13:26:07 +0100568 /* in case cpu page size != gpu page size*/
569 result |= addr & (~PAGE_MASK);
570
571 } else {
572 /* No mapping required */
573 result = addr;
574 }
575
576 result &= 0xFFFFFFFFFFFFF000ULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400577
578 return result;
579}
580
581/**
582 * amdgpu_vm_update_pdes - make sure that page directory is valid
583 *
584 * @adev: amdgpu_device pointer
585 * @vm: requested vm
586 * @start: start of GPU address range
587 * @end: end of GPU address range
588 *
589 * Allocates new page tables if necessary
Christian König8843dbb2016-01-26 12:17:11 +0100590 * and updates the page directory.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400591 * Returns 0 for success, error for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400592 */
593int amdgpu_vm_update_page_directory(struct amdgpu_device *adev,
594 struct amdgpu_vm *vm)
595{
Christian König2d55e452016-02-08 17:37:38 +0100596 struct amdgpu_ring *ring;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400597 struct amdgpu_bo *pd = vm->page_directory;
598 uint64_t pd_addr = amdgpu_bo_gpu_offset(pd);
599 uint32_t incr = AMDGPU_VM_PTE_COUNT * 8;
600 uint64_t last_pde = ~0, last_pt = ~0;
601 unsigned count = 0, pt_idx, ndw;
Christian Königd71518b2016-02-01 12:20:25 +0100602 struct amdgpu_job *job;
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400603 struct amdgpu_vm_update_params vm_update_params;
Chunming Zhou4af9f072015-08-03 12:57:31 +0800604 struct fence *fence = NULL;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800605
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400606 int r;
607
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400608 memset(&vm_update_params, 0, sizeof(vm_update_params));
Christian König2d55e452016-02-08 17:37:38 +0100609 ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
610
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400611 /* padding, etc. */
612 ndw = 64;
613
614 /* assume the worst case */
615 ndw += vm->max_pde_used * 6;
616
Christian Königd71518b2016-02-01 12:20:25 +0100617 r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
618 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400619 return r;
Christian Königd71518b2016-02-01 12:20:25 +0100620
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400621 vm_update_params.ib = &job->ibs[0];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400622
623 /* walk over the address space and update the page directory */
624 for (pt_idx = 0; pt_idx <= vm->max_pde_used; ++pt_idx) {
Christian Königee1782c2015-12-11 21:01:23 +0100625 struct amdgpu_bo *bo = vm->page_tables[pt_idx].entry.robj;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400626 uint64_t pde, pt;
627
628 if (bo == NULL)
629 continue;
630
631 pt = amdgpu_bo_gpu_offset(bo);
632 if (vm->page_tables[pt_idx].addr == pt)
633 continue;
634 vm->page_tables[pt_idx].addr = pt;
635
636 pde = pd_addr + pt_idx * 8;
637 if (((last_pde + 8 * count) != pde) ||
638 ((last_pt + incr * count) != pt)) {
639
640 if (count) {
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400641 amdgpu_vm_update_pages(adev, &vm_update_params,
Christian König9ab21462015-11-30 14:19:26 +0100642 last_pde, last_pt,
643 count, incr,
644 AMDGPU_PTE_VALID);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400645 }
646
647 count = 1;
648 last_pde = pde;
649 last_pt = pt;
650 } else {
651 ++count;
652 }
653 }
654
655 if (count)
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400656 amdgpu_vm_update_pages(adev, &vm_update_params,
657 last_pde, last_pt,
658 count, incr, AMDGPU_PTE_VALID);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400659
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400660 if (vm_update_params.ib->length_dw != 0) {
661 amdgpu_ring_pad_ib(ring, vm_update_params.ib);
Christian Könige86f9ce2016-02-08 12:13:05 +0100662 amdgpu_sync_resv(adev, &job->sync, pd->tbo.resv,
663 AMDGPU_FENCE_OWNER_VM);
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400664 WARN_ON(vm_update_params.ib->length_dw > ndw);
Christian König2bd9ccf2016-02-01 12:53:58 +0100665 r = amdgpu_job_submit(job, ring, &vm->entity,
666 AMDGPU_FENCE_OWNER_VM, &fence);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800667 if (r)
668 goto error_free;
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +0200669
Chunming Zhou4af9f072015-08-03 12:57:31 +0800670 amdgpu_bo_fence(pd, fence, true);
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +0200671 fence_put(vm->page_directory_fence);
672 vm->page_directory_fence = fence_get(fence);
Chunming Zhou281b4222015-08-12 12:58:31 +0800673 fence_put(fence);
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800674
Christian Königd71518b2016-02-01 12:20:25 +0100675 } else {
676 amdgpu_job_free(job);
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800677 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400678
679 return 0;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800680
681error_free:
Christian Königd71518b2016-02-01 12:20:25 +0100682 amdgpu_job_free(job);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800683 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400684}
685
686/**
687 * amdgpu_vm_frag_ptes - add fragment information to PTEs
688 *
689 * @adev: amdgpu_device pointer
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400690 * @vm_update_params: see amdgpu_vm_update_params definition
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400691 * @pe_start: first PTE to handle
692 * @pe_end: last PTE to handle
693 * @addr: addr those PTEs should point to
694 * @flags: hw mapping flags
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400695 */
696static void amdgpu_vm_frag_ptes(struct amdgpu_device *adev,
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400697 struct amdgpu_vm_update_params
698 *vm_update_params,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400699 uint64_t pe_start, uint64_t pe_end,
Christian König9ab21462015-11-30 14:19:26 +0100700 uint64_t addr, uint32_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400701{
702 /**
703 * The MC L1 TLB supports variable sized pages, based on a fragment
704 * field in the PTE. When this field is set to a non-zero value, page
705 * granularity is increased from 4KB to (1 << (12 + frag)). The PTE
706 * flags are considered valid for all PTEs within the fragment range
707 * and corresponding mappings are assumed to be physically contiguous.
708 *
709 * The L1 TLB can store a single PTE for the whole fragment,
710 * significantly increasing the space available for translation
711 * caching. This leads to large improvements in throughput when the
712 * TLB is under pressure.
713 *
714 * The L2 TLB distributes small and large fragments into two
715 * asymmetric partitions. The large fragment cache is significantly
716 * larger. Thus, we try to use large fragments wherever possible.
717 * Userspace can support this by aligning virtual base address and
718 * allocation size to the fragment size.
719 */
720
721 /* SI and newer are optimized for 64KB */
722 uint64_t frag_flags = AMDGPU_PTE_FRAG_64KB;
723 uint64_t frag_align = 0x80;
724
725 uint64_t frag_start = ALIGN(pe_start, frag_align);
726 uint64_t frag_end = pe_end & ~(frag_align - 1);
727
728 unsigned count;
729
Christian König31f6c1f2016-01-26 12:37:49 +0100730 /* Abort early if there isn't anything to do */
731 if (pe_start == pe_end)
732 return;
733
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400734 /* system pages are non continuously */
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400735 if (vm_update_params->src || vm_update_params->pages_addr ||
736 !(flags & AMDGPU_PTE_VALID) || (frag_start >= frag_end)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400737
738 count = (pe_end - pe_start) / 8;
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400739 amdgpu_vm_update_pages(adev, vm_update_params, pe_start,
Christian König9ab21462015-11-30 14:19:26 +0100740 addr, count, AMDGPU_GPU_PAGE_SIZE,
741 flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400742 return;
743 }
744
745 /* handle the 4K area at the beginning */
746 if (pe_start != frag_start) {
747 count = (frag_start - pe_start) / 8;
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400748 amdgpu_vm_update_pages(adev, vm_update_params, pe_start, addr,
Christian König9ab21462015-11-30 14:19:26 +0100749 count, AMDGPU_GPU_PAGE_SIZE, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400750 addr += AMDGPU_GPU_PAGE_SIZE * count;
751 }
752
753 /* handle the area in the middle */
754 count = (frag_end - frag_start) / 8;
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400755 amdgpu_vm_update_pages(adev, vm_update_params, frag_start, addr, count,
Christian König9ab21462015-11-30 14:19:26 +0100756 AMDGPU_GPU_PAGE_SIZE, flags | frag_flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400757
758 /* handle the 4K area at the end */
759 if (frag_end != pe_end) {
760 addr += AMDGPU_GPU_PAGE_SIZE * count;
761 count = (pe_end - frag_end) / 8;
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400762 amdgpu_vm_update_pages(adev, vm_update_params, frag_end, addr,
Christian König9ab21462015-11-30 14:19:26 +0100763 count, AMDGPU_GPU_PAGE_SIZE, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400764 }
765}
766
767/**
768 * amdgpu_vm_update_ptes - make sure that page tables are valid
769 *
770 * @adev: amdgpu_device pointer
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400771 * @vm_update_params: see amdgpu_vm_update_params definition
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400772 * @vm: requested vm
773 * @start: start of GPU address range
774 * @end: end of GPU address range
Alex Xie677131a2016-06-06 18:13:26 -0400775 * @dst: destination address to map to, the next dst inside the function
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400776 * @flags: mapping flags
777 *
Christian König8843dbb2016-01-26 12:17:11 +0100778 * Update the page tables in the range @start - @end.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400779 */
Christian Königa1e08d32016-01-26 11:40:46 +0100780static void amdgpu_vm_update_ptes(struct amdgpu_device *adev,
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400781 struct amdgpu_vm_update_params
782 *vm_update_params,
Christian Königa1e08d32016-01-26 11:40:46 +0100783 struct amdgpu_vm *vm,
Christian Königa1e08d32016-01-26 11:40:46 +0100784 uint64_t start, uint64_t end,
785 uint64_t dst, uint32_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400786{
Christian König31f6c1f2016-01-26 12:37:49 +0100787 const uint64_t mask = AMDGPU_VM_PTE_COUNT - 1;
788
Alex Xie21718492016-06-06 18:21:09 -0400789 uint64_t cur_pe_start, cur_pe_end, cur_dst;
Alex Xie677131a2016-06-06 18:13:26 -0400790 uint64_t addr; /* next GPU address to be updated */
Alex Xie21718492016-06-06 18:21:09 -0400791 uint64_t pt_idx;
792 struct amdgpu_bo *pt;
793 unsigned nptes; /* next number of ptes to be updated */
794 uint64_t next_pe_start;
795
796 /* initialize the variables */
797 addr = start;
798 pt_idx = addr >> amdgpu_vm_block_size;
799 pt = vm->page_tables[pt_idx].entry.robj;
800
801 if ((addr & ~mask) == (end & ~mask))
802 nptes = end - addr;
803 else
804 nptes = AMDGPU_VM_PTE_COUNT - (addr & mask);
805
806 cur_pe_start = amdgpu_bo_gpu_offset(pt);
807 cur_pe_start += (addr & mask) * 8;
808 cur_pe_end = cur_pe_start + 8 * nptes;
809 cur_dst = dst;
810
811 /* for next ptb*/
812 addr += nptes;
813 dst += nptes * AMDGPU_GPU_PAGE_SIZE;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400814
815 /* walk over the address space and update the page tables */
Alex Xie21718492016-06-06 18:21:09 -0400816 while (addr < end) {
817 pt_idx = addr >> amdgpu_vm_block_size;
818 pt = vm->page_tables[pt_idx].entry.robj;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400819
820 if ((addr & ~mask) == (end & ~mask))
821 nptes = end - addr;
822 else
823 nptes = AMDGPU_VM_PTE_COUNT - (addr & mask);
824
Alex Xie677131a2016-06-06 18:13:26 -0400825 next_pe_start = amdgpu_bo_gpu_offset(pt);
826 next_pe_start += (addr & mask) * 8;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400827
Alex Xie3a6f8e02016-06-06 18:14:57 -0400828 if (cur_pe_end == next_pe_start) {
829 /* The next ptb is consecutive to current ptb.
830 * Don't call amdgpu_vm_frag_ptes now.
831 * Will update two ptbs together in future.
832 */
833 cur_pe_end += 8 * nptes;
834 } else {
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400835 amdgpu_vm_frag_ptes(adev, vm_update_params,
Alex Xie677131a2016-06-06 18:13:26 -0400836 cur_pe_start, cur_pe_end,
837 cur_dst, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400838
Alex Xie677131a2016-06-06 18:13:26 -0400839 cur_pe_start = next_pe_start;
840 cur_pe_end = next_pe_start + 8 * nptes;
841 cur_dst = dst;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400842 }
843
Alex Xie21718492016-06-06 18:21:09 -0400844 /* for next ptb*/
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400845 addr += nptes;
846 dst += nptes * AMDGPU_GPU_PAGE_SIZE;
847 }
848
Alex Xie677131a2016-06-06 18:13:26 -0400849 amdgpu_vm_frag_ptes(adev, vm_update_params, cur_pe_start,
850 cur_pe_end, cur_dst, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400851}
852
853/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400854 * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table
855 *
856 * @adev: amdgpu_device pointer
Christian König3cabaa52016-06-06 10:17:58 +0200857 * @exclusive: fence we need to sync to
Christian Königfa3ab3c2016-03-18 21:00:35 +0100858 * @src: address where to copy page table entries from
859 * @pages_addr: DMA addresses to use for mapping
Christian Königa14faa62016-01-25 14:27:31 +0100860 * @vm: requested vm
861 * @start: start of mapped range
862 * @last: last mapped entry
863 * @flags: flags for the entries
864 * @addr: addr to set the area to
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400865 * @fence: optional resulting fence
866 *
Christian Königa14faa62016-01-25 14:27:31 +0100867 * Fill in the page table entries between @start and @last.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400868 * Returns 0 for success, -EINVAL for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400869 */
870static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
Christian König3cabaa52016-06-06 10:17:58 +0200871 struct fence *exclusive,
Christian Königfa3ab3c2016-03-18 21:00:35 +0100872 uint64_t src,
873 dma_addr_t *pages_addr,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400874 struct amdgpu_vm *vm,
Christian Königa14faa62016-01-25 14:27:31 +0100875 uint64_t start, uint64_t last,
876 uint32_t flags, uint64_t addr,
877 struct fence **fence)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400878{
Christian König2d55e452016-02-08 17:37:38 +0100879 struct amdgpu_ring *ring;
Christian Königa1e08d32016-01-26 11:40:46 +0100880 void *owner = AMDGPU_FENCE_OWNER_VM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400881 unsigned nptes, ncmds, ndw;
Christian Königd71518b2016-02-01 12:20:25 +0100882 struct amdgpu_job *job;
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400883 struct amdgpu_vm_update_params vm_update_params;
Chunming Zhou4af9f072015-08-03 12:57:31 +0800884 struct fence *f = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400885 int r;
886
Christian König2d55e452016-02-08 17:37:38 +0100887 ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400888 memset(&vm_update_params, 0, sizeof(vm_update_params));
889 vm_update_params.src = src;
890 vm_update_params.pages_addr = pages_addr;
Christian König2d55e452016-02-08 17:37:38 +0100891
Christian Königa1e08d32016-01-26 11:40:46 +0100892 /* sync to everything on unmapping */
893 if (!(flags & AMDGPU_PTE_VALID))
894 owner = AMDGPU_FENCE_OWNER_UNDEFINED;
895
Christian Königa14faa62016-01-25 14:27:31 +0100896 nptes = last - start + 1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400897
898 /*
899 * reserve space for one command every (1 << BLOCK_SIZE)
900 * entries or 2k dwords (whatever is smaller)
901 */
902 ncmds = (nptes >> min(amdgpu_vm_block_size, 11)) + 1;
903
904 /* padding, etc. */
905 ndw = 64;
906
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400907 if (vm_update_params.src) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400908 /* only copy commands needed */
909 ndw += ncmds * 7;
910
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400911 } else if (vm_update_params.pages_addr) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400912 /* header for write data commands */
913 ndw += ncmds * 4;
914
915 /* body of write data command */
916 ndw += nptes * 2;
917
918 } else {
919 /* set page commands needed */
920 ndw += ncmds * 10;
921
922 /* two extra commands for begin/end of fragment */
923 ndw += 2 * 10;
924 }
925
Christian Königd71518b2016-02-01 12:20:25 +0100926 r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
927 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400928 return r;
Christian Königd71518b2016-02-01 12:20:25 +0100929
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400930 vm_update_params.ib = &job->ibs[0];
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800931
Christian König3cabaa52016-06-06 10:17:58 +0200932 r = amdgpu_sync_fence(adev, &job->sync, exclusive);
933 if (r)
934 goto error_free;
935
Christian Könige86f9ce2016-02-08 12:13:05 +0100936 r = amdgpu_sync_resv(adev, &job->sync, vm->page_directory->tbo.resv,
Christian Königa1e08d32016-01-26 11:40:46 +0100937 owner);
938 if (r)
939 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400940
Christian Königa1e08d32016-01-26 11:40:46 +0100941 r = reservation_object_reserve_shared(vm->page_directory->tbo.resv);
942 if (r)
943 goto error_free;
944
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400945 amdgpu_vm_update_ptes(adev, &vm_update_params, vm, start,
Christian Königfa3ab3c2016-03-18 21:00:35 +0100946 last + 1, addr, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400947
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -0400948 amdgpu_ring_pad_ib(ring, vm_update_params.ib);
949 WARN_ON(vm_update_params.ib->length_dw > ndw);
Christian König2bd9ccf2016-02-01 12:53:58 +0100950 r = amdgpu_job_submit(job, ring, &vm->entity,
951 AMDGPU_FENCE_OWNER_VM, &f);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800952 if (r)
953 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400954
Christian Königbf60efd2015-09-04 10:47:56 +0200955 amdgpu_bo_fence(vm->page_directory, f, true);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800956 if (fence) {
957 fence_put(*fence);
958 *fence = fence_get(f);
959 }
Chunming Zhou281b4222015-08-12 12:58:31 +0800960 fence_put(f);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400961 return 0;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800962
963error_free:
Christian Königd71518b2016-02-01 12:20:25 +0100964 amdgpu_job_free(job);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800965 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400966}
967
968/**
Christian Königa14faa62016-01-25 14:27:31 +0100969 * amdgpu_vm_bo_split_mapping - split a mapping into smaller chunks
970 *
971 * @adev: amdgpu_device pointer
Christian König3cabaa52016-06-06 10:17:58 +0200972 * @exclusive: fence we need to sync to
Christian König8358dce2016-03-30 10:50:25 +0200973 * @gtt_flags: flags as they are used for GTT
974 * @pages_addr: DMA addresses to use for mapping
Christian Königa14faa62016-01-25 14:27:31 +0100975 * @vm: requested vm
976 * @mapping: mapped range and flags to use for the update
977 * @addr: addr to set the area to
Christian König8358dce2016-03-30 10:50:25 +0200978 * @flags: HW flags for the mapping
Christian Königa14faa62016-01-25 14:27:31 +0100979 * @fence: optional resulting fence
980 *
981 * Split the mapping into smaller chunks so that each update fits
982 * into a SDMA IB.
983 * Returns 0 for success, -EINVAL for failure.
984 */
985static int amdgpu_vm_bo_split_mapping(struct amdgpu_device *adev,
Christian König3cabaa52016-06-06 10:17:58 +0200986 struct fence *exclusive,
Christian Königa14faa62016-01-25 14:27:31 +0100987 uint32_t gtt_flags,
Christian König8358dce2016-03-30 10:50:25 +0200988 dma_addr_t *pages_addr,
Christian Königa14faa62016-01-25 14:27:31 +0100989 struct amdgpu_vm *vm,
990 struct amdgpu_bo_va_mapping *mapping,
Christian Königfa3ab3c2016-03-18 21:00:35 +0100991 uint32_t flags, uint64_t addr,
992 struct fence **fence)
Christian Königa14faa62016-01-25 14:27:31 +0100993{
994 const uint64_t max_size = 64ULL * 1024ULL * 1024ULL / AMDGPU_GPU_PAGE_SIZE;
995
Christian Königfa3ab3c2016-03-18 21:00:35 +0100996 uint64_t src = 0, start = mapping->it.start;
Christian Königa14faa62016-01-25 14:27:31 +0100997 int r;
998
999 /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here
1000 * but in case of something, we filter the flags in first place
1001 */
1002 if (!(mapping->flags & AMDGPU_PTE_READABLE))
1003 flags &= ~AMDGPU_PTE_READABLE;
1004 if (!(mapping->flags & AMDGPU_PTE_WRITEABLE))
1005 flags &= ~AMDGPU_PTE_WRITEABLE;
1006
1007 trace_amdgpu_vm_bo_update(mapping);
1008
Christian König8358dce2016-03-30 10:50:25 +02001009 if (pages_addr) {
Christian Königfa3ab3c2016-03-18 21:00:35 +01001010 if (flags == gtt_flags)
1011 src = adev->gart.table_addr + (addr >> 12) * 8;
Christian Königfa3ab3c2016-03-18 21:00:35 +01001012 addr = 0;
1013 }
Christian Königa14faa62016-01-25 14:27:31 +01001014 addr += mapping->offset;
1015
Christian König8358dce2016-03-30 10:50:25 +02001016 if (!pages_addr || src)
Christian König3cabaa52016-06-06 10:17:58 +02001017 return amdgpu_vm_bo_update_mapping(adev, exclusive,
1018 src, pages_addr, vm,
Christian Königa14faa62016-01-25 14:27:31 +01001019 start, mapping->it.last,
1020 flags, addr, fence);
1021
1022 while (start != mapping->it.last + 1) {
1023 uint64_t last;
1024
Felix Kuehlingfb29b572016-03-03 19:13:20 -05001025 last = min((uint64_t)mapping->it.last, start + max_size - 1);
Christian König3cabaa52016-06-06 10:17:58 +02001026 r = amdgpu_vm_bo_update_mapping(adev, exclusive,
1027 src, pages_addr, vm,
Christian Königa14faa62016-01-25 14:27:31 +01001028 start, last, flags, addr,
1029 fence);
1030 if (r)
1031 return r;
1032
1033 start = last + 1;
Felix Kuehlingfb29b572016-03-03 19:13:20 -05001034 addr += max_size * AMDGPU_GPU_PAGE_SIZE;
Christian Königa14faa62016-01-25 14:27:31 +01001035 }
1036
1037 return 0;
1038}
1039
1040/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001041 * amdgpu_vm_bo_update - update all BO mappings in the vm page table
1042 *
1043 * @adev: amdgpu_device pointer
1044 * @bo_va: requested BO and VM object
1045 * @mem: ttm mem
1046 *
1047 * Fill in the page table entries for @bo_va.
1048 * Returns 0 for success, -EINVAL for failure.
1049 *
1050 * Object have to be reserved and mutex must be locked!
1051 */
1052int amdgpu_vm_bo_update(struct amdgpu_device *adev,
1053 struct amdgpu_bo_va *bo_va,
1054 struct ttm_mem_reg *mem)
1055{
1056 struct amdgpu_vm *vm = bo_va->vm;
1057 struct amdgpu_bo_va_mapping *mapping;
Christian König8358dce2016-03-30 10:50:25 +02001058 dma_addr_t *pages_addr = NULL;
Christian Königfa3ab3c2016-03-18 21:00:35 +01001059 uint32_t gtt_flags, flags;
Christian König3cabaa52016-06-06 10:17:58 +02001060 struct fence *exclusive;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001061 uint64_t addr;
1062 int r;
1063
1064 if (mem) {
Christian König8358dce2016-03-30 10:50:25 +02001065 struct ttm_dma_tt *ttm;
1066
Christian Königb7d698d2015-09-07 12:32:09 +02001067 addr = (u64)mem->start << PAGE_SHIFT;
Christian König9ab21462015-11-30 14:19:26 +01001068 switch (mem->mem_type) {
1069 case TTM_PL_TT:
Christian König8358dce2016-03-30 10:50:25 +02001070 ttm = container_of(bo_va->bo->tbo.ttm, struct
1071 ttm_dma_tt, ttm);
1072 pages_addr = ttm->dma_address;
Christian König9ab21462015-11-30 14:19:26 +01001073 break;
1074
1075 case TTM_PL_VRAM:
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001076 addr += adev->vm_manager.vram_base_offset;
Christian König9ab21462015-11-30 14:19:26 +01001077 break;
1078
1079 default:
1080 break;
1081 }
Christian König3cabaa52016-06-06 10:17:58 +02001082
1083 exclusive = reservation_object_get_excl(bo_va->bo->tbo.resv);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001084 } else {
1085 addr = 0;
Christian König3cabaa52016-06-06 10:17:58 +02001086 exclusive = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001087 }
1088
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001089 flags = amdgpu_ttm_tt_pte_flags(adev, bo_va->bo->tbo.ttm, mem);
Christian Königfa3ab3c2016-03-18 21:00:35 +01001090 gtt_flags = (adev == bo_va->bo->adev) ? flags : 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001091
Christian König7fc11952015-07-30 11:53:42 +02001092 spin_lock(&vm->status_lock);
1093 if (!list_empty(&bo_va->vm_status))
1094 list_splice_init(&bo_va->valids, &bo_va->invalids);
1095 spin_unlock(&vm->status_lock);
1096
1097 list_for_each_entry(mapping, &bo_va->invalids, list) {
Christian König3cabaa52016-06-06 10:17:58 +02001098 r = amdgpu_vm_bo_split_mapping(adev, exclusive,
1099 gtt_flags, pages_addr, vm,
Christian König8358dce2016-03-30 10:50:25 +02001100 mapping, flags, addr,
1101 &bo_va->last_pt_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001102 if (r)
1103 return r;
1104 }
1105
Christian Königd6c10f62015-09-28 12:00:23 +02001106 if (trace_amdgpu_vm_bo_mapping_enabled()) {
1107 list_for_each_entry(mapping, &bo_va->valids, list)
1108 trace_amdgpu_vm_bo_mapping(mapping);
1109
1110 list_for_each_entry(mapping, &bo_va->invalids, list)
1111 trace_amdgpu_vm_bo_mapping(mapping);
1112 }
1113
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001114 spin_lock(&vm->status_lock);
monk.liu6d1d0ef2015-08-14 13:36:41 +08001115 list_splice_init(&bo_va->invalids, &bo_va->valids);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001116 list_del_init(&bo_va->vm_status);
Christian König7fc11952015-07-30 11:53:42 +02001117 if (!mem)
1118 list_add(&bo_va->vm_status, &vm->cleared);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001119 spin_unlock(&vm->status_lock);
1120
1121 return 0;
1122}
1123
1124/**
1125 * amdgpu_vm_clear_freed - clear freed BOs in the PT
1126 *
1127 * @adev: amdgpu_device pointer
1128 * @vm: requested vm
1129 *
1130 * Make sure all freed BOs are cleared in the PT.
1131 * Returns 0 for success.
1132 *
1133 * PTs have to be reserved and mutex must be locked!
1134 */
1135int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
1136 struct amdgpu_vm *vm)
1137{
1138 struct amdgpu_bo_va_mapping *mapping;
1139 int r;
1140
1141 while (!list_empty(&vm->freed)) {
1142 mapping = list_first_entry(&vm->freed,
1143 struct amdgpu_bo_va_mapping, list);
1144 list_del(&mapping->list);
Christian Könige17841b2016-03-08 17:52:01 +01001145
Christian König3cabaa52016-06-06 10:17:58 +02001146 r = amdgpu_vm_bo_split_mapping(adev, NULL, 0, NULL, vm, mapping,
Christian Königfa3ab3c2016-03-18 21:00:35 +01001147 0, 0, NULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001148 kfree(mapping);
1149 if (r)
1150 return r;
1151
1152 }
1153 return 0;
1154
1155}
1156
1157/**
1158 * amdgpu_vm_clear_invalids - clear invalidated BOs in the PT
1159 *
1160 * @adev: amdgpu_device pointer
1161 * @vm: requested vm
1162 *
1163 * Make sure all invalidated BOs are cleared in the PT.
1164 * Returns 0 for success.
1165 *
1166 * PTs have to be reserved and mutex must be locked!
1167 */
1168int amdgpu_vm_clear_invalids(struct amdgpu_device *adev,
monk.liucfe2c972015-05-26 15:01:54 +08001169 struct amdgpu_vm *vm, struct amdgpu_sync *sync)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001170{
monk.liucfe2c972015-05-26 15:01:54 +08001171 struct amdgpu_bo_va *bo_va = NULL;
Christian König91e1a522015-07-06 22:06:40 +02001172 int r = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001173
1174 spin_lock(&vm->status_lock);
1175 while (!list_empty(&vm->invalidated)) {
1176 bo_va = list_first_entry(&vm->invalidated,
1177 struct amdgpu_bo_va, vm_status);
1178 spin_unlock(&vm->status_lock);
Christian König32b41ac2016-03-08 18:03:27 +01001179
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001180 r = amdgpu_vm_bo_update(adev, bo_va, NULL);
1181 if (r)
1182 return r;
1183
1184 spin_lock(&vm->status_lock);
1185 }
1186 spin_unlock(&vm->status_lock);
1187
monk.liucfe2c972015-05-26 15:01:54 +08001188 if (bo_va)
Chunming Zhoubb1e38a42015-08-03 18:19:38 +08001189 r = amdgpu_sync_fence(adev, sync, bo_va->last_pt_update);
Christian König91e1a522015-07-06 22:06:40 +02001190
1191 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001192}
1193
1194/**
1195 * amdgpu_vm_bo_add - add a bo to a specific vm
1196 *
1197 * @adev: amdgpu_device pointer
1198 * @vm: requested vm
1199 * @bo: amdgpu buffer object
1200 *
Christian König8843dbb2016-01-26 12:17:11 +01001201 * Add @bo into the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001202 * Add @bo to the list of bos associated with the vm
1203 * Returns newly added bo_va or NULL for failure
1204 *
1205 * Object has to be reserved!
1206 */
1207struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
1208 struct amdgpu_vm *vm,
1209 struct amdgpu_bo *bo)
1210{
1211 struct amdgpu_bo_va *bo_va;
1212
1213 bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL);
1214 if (bo_va == NULL) {
1215 return NULL;
1216 }
1217 bo_va->vm = vm;
1218 bo_va->bo = bo;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001219 bo_va->ref_count = 1;
1220 INIT_LIST_HEAD(&bo_va->bo_list);
Christian König7fc11952015-07-30 11:53:42 +02001221 INIT_LIST_HEAD(&bo_va->valids);
1222 INIT_LIST_HEAD(&bo_va->invalids);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001223 INIT_LIST_HEAD(&bo_va->vm_status);
Christian König32b41ac2016-03-08 18:03:27 +01001224
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001225 list_add_tail(&bo_va->bo_list, &bo->va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001226
1227 return bo_va;
1228}
1229
1230/**
1231 * amdgpu_vm_bo_map - map bo inside a vm
1232 *
1233 * @adev: amdgpu_device pointer
1234 * @bo_va: bo_va to store the address
1235 * @saddr: where to map the BO
1236 * @offset: requested offset in the BO
1237 * @flags: attributes of pages (read/write/valid/etc.)
1238 *
1239 * Add a mapping of the BO at the specefied addr into the VM.
1240 * Returns 0 for success, error for failure.
1241 *
Chunming Zhou49b02b12015-11-13 14:18:38 +08001242 * Object has to be reserved and unreserved outside!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001243 */
1244int amdgpu_vm_bo_map(struct amdgpu_device *adev,
1245 struct amdgpu_bo_va *bo_va,
1246 uint64_t saddr, uint64_t offset,
1247 uint64_t size, uint32_t flags)
1248{
1249 struct amdgpu_bo_va_mapping *mapping;
1250 struct amdgpu_vm *vm = bo_va->vm;
1251 struct interval_tree_node *it;
1252 unsigned last_pfn, pt_idx;
1253 uint64_t eaddr;
1254 int r;
1255
Christian König0be52de2015-05-18 14:37:27 +02001256 /* validate the parameters */
1257 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
Chunming Zhou49b02b12015-11-13 14:18:38 +08001258 size == 0 || size & AMDGPU_GPU_PAGE_MASK)
Christian König0be52de2015-05-18 14:37:27 +02001259 return -EINVAL;
Christian König0be52de2015-05-18 14:37:27 +02001260
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001261 /* make sure object fit at this offset */
Felix Kuehling005ae952015-11-23 17:43:48 -05001262 eaddr = saddr + size - 1;
Chunming Zhou49b02b12015-11-13 14:18:38 +08001263 if ((saddr >= eaddr) || (offset + size > amdgpu_bo_size(bo_va->bo)))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001264 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001265
1266 last_pfn = eaddr / AMDGPU_GPU_PAGE_SIZE;
Felix Kuehling005ae952015-11-23 17:43:48 -05001267 if (last_pfn >= adev->vm_manager.max_pfn) {
1268 dev_err(adev->dev, "va above limit (0x%08X >= 0x%08X)\n",
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001269 last_pfn, adev->vm_manager.max_pfn);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001270 return -EINVAL;
1271 }
1272
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001273 saddr /= AMDGPU_GPU_PAGE_SIZE;
1274 eaddr /= AMDGPU_GPU_PAGE_SIZE;
1275
Felix Kuehling005ae952015-11-23 17:43:48 -05001276 it = interval_tree_iter_first(&vm->va, saddr, eaddr);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001277 if (it) {
1278 struct amdgpu_bo_va_mapping *tmp;
1279 tmp = container_of(it, struct amdgpu_bo_va_mapping, it);
1280 /* bo and tmp overlap, invalid addr */
1281 dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with "
1282 "0x%010lx-0x%010lx\n", bo_va->bo, saddr, eaddr,
1283 tmp->it.start, tmp->it.last + 1);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001284 r = -EINVAL;
Chunming Zhouf48b2652015-10-16 14:06:19 +08001285 goto error;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001286 }
1287
1288 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
1289 if (!mapping) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001290 r = -ENOMEM;
Chunming Zhouf48b2652015-10-16 14:06:19 +08001291 goto error;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001292 }
1293
1294 INIT_LIST_HEAD(&mapping->list);
1295 mapping->it.start = saddr;
Felix Kuehling005ae952015-11-23 17:43:48 -05001296 mapping->it.last = eaddr;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001297 mapping->offset = offset;
1298 mapping->flags = flags;
1299
Christian König7fc11952015-07-30 11:53:42 +02001300 list_add(&mapping->list, &bo_va->invalids);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001301 interval_tree_insert(&mapping->it, &vm->va);
1302
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001303 /* Make sure the page tables are allocated */
1304 saddr >>= amdgpu_vm_block_size;
1305 eaddr >>= amdgpu_vm_block_size;
1306
1307 BUG_ON(eaddr >= amdgpu_vm_num_pdes(adev));
1308
1309 if (eaddr > vm->max_pde_used)
1310 vm->max_pde_used = eaddr;
1311
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001312 /* walk over the address space and allocate the page tables */
1313 for (pt_idx = saddr; pt_idx <= eaddr; ++pt_idx) {
Christian Königbf60efd2015-09-04 10:47:56 +02001314 struct reservation_object *resv = vm->page_directory->tbo.resv;
Christian Königee1782c2015-12-11 21:01:23 +01001315 struct amdgpu_bo_list_entry *entry;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001316 struct amdgpu_bo *pt;
1317
Christian Königee1782c2015-12-11 21:01:23 +01001318 entry = &vm->page_tables[pt_idx].entry;
1319 if (entry->robj)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001320 continue;
1321
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001322 r = amdgpu_bo_create(adev, AMDGPU_VM_PTE_COUNT * 8,
1323 AMDGPU_GPU_PAGE_SIZE, true,
Alex Deucher857d9132015-08-27 00:14:16 -04001324 AMDGPU_GEM_DOMAIN_VRAM,
1325 AMDGPU_GEM_CREATE_NO_CPU_ACCESS,
Christian Königbf60efd2015-09-04 10:47:56 +02001326 NULL, resv, &pt);
Chunming Zhou49b02b12015-11-13 14:18:38 +08001327 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001328 goto error_free;
Chunming Zhou49b02b12015-11-13 14:18:38 +08001329
Christian König82b9c552015-11-27 16:49:00 +01001330 /* Keep a reference to the page table to avoid freeing
1331 * them up in the wrong order.
1332 */
1333 pt->parent = amdgpu_bo_ref(vm->page_directory);
1334
Christian König2bd9ccf2016-02-01 12:53:58 +01001335 r = amdgpu_vm_clear_bo(adev, vm, pt);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001336 if (r) {
1337 amdgpu_bo_unref(&pt);
1338 goto error_free;
1339 }
1340
Christian Königee1782c2015-12-11 21:01:23 +01001341 entry->robj = pt;
Christian Königee1782c2015-12-11 21:01:23 +01001342 entry->priority = 0;
1343 entry->tv.bo = &entry->robj->tbo;
1344 entry->tv.shared = true;
Christian König2f568db2016-02-23 12:36:59 +01001345 entry->user_pages = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001346 vm->page_tables[pt_idx].addr = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001347 }
1348
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001349 return 0;
1350
1351error_free:
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001352 list_del(&mapping->list);
1353 interval_tree_remove(&mapping->it, &vm->va);
Christian König93e3e432015-06-09 16:58:33 +02001354 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001355 kfree(mapping);
1356
Chunming Zhouf48b2652015-10-16 14:06:19 +08001357error:
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001358 return r;
1359}
1360
1361/**
1362 * amdgpu_vm_bo_unmap - remove bo mapping from vm
1363 *
1364 * @adev: amdgpu_device pointer
1365 * @bo_va: bo_va to remove the address from
1366 * @saddr: where to the BO is mapped
1367 *
1368 * Remove a mapping of the BO at the specefied addr from the VM.
1369 * Returns 0 for success, error for failure.
1370 *
Chunming Zhou49b02b12015-11-13 14:18:38 +08001371 * Object has to be reserved and unreserved outside!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001372 */
1373int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
1374 struct amdgpu_bo_va *bo_va,
1375 uint64_t saddr)
1376{
1377 struct amdgpu_bo_va_mapping *mapping;
1378 struct amdgpu_vm *vm = bo_va->vm;
Christian König7fc11952015-07-30 11:53:42 +02001379 bool valid = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001380
Christian König6c7fc502015-06-05 20:56:17 +02001381 saddr /= AMDGPU_GPU_PAGE_SIZE;
Christian König32b41ac2016-03-08 18:03:27 +01001382
Christian König7fc11952015-07-30 11:53:42 +02001383 list_for_each_entry(mapping, &bo_va->valids, list) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001384 if (mapping->it.start == saddr)
1385 break;
1386 }
1387
Christian König7fc11952015-07-30 11:53:42 +02001388 if (&mapping->list == &bo_va->valids) {
1389 valid = false;
1390
1391 list_for_each_entry(mapping, &bo_va->invalids, list) {
1392 if (mapping->it.start == saddr)
1393 break;
1394 }
1395
Christian König32b41ac2016-03-08 18:03:27 +01001396 if (&mapping->list == &bo_va->invalids)
Christian König7fc11952015-07-30 11:53:42 +02001397 return -ENOENT;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001398 }
Christian König32b41ac2016-03-08 18:03:27 +01001399
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001400 list_del(&mapping->list);
1401 interval_tree_remove(&mapping->it, &vm->va);
Christian König93e3e432015-06-09 16:58:33 +02001402 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001403
Christian Könige17841b2016-03-08 17:52:01 +01001404 if (valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001405 list_add(&mapping->list, &vm->freed);
Christian Könige17841b2016-03-08 17:52:01 +01001406 else
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001407 kfree(mapping);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001408
1409 return 0;
1410}
1411
1412/**
1413 * amdgpu_vm_bo_rmv - remove a bo to a specific vm
1414 *
1415 * @adev: amdgpu_device pointer
1416 * @bo_va: requested bo_va
1417 *
Christian König8843dbb2016-01-26 12:17:11 +01001418 * Remove @bo_va->bo from the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001419 *
1420 * Object have to be reserved!
1421 */
1422void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
1423 struct amdgpu_bo_va *bo_va)
1424{
1425 struct amdgpu_bo_va_mapping *mapping, *next;
1426 struct amdgpu_vm *vm = bo_va->vm;
1427
1428 list_del(&bo_va->bo_list);
1429
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001430 spin_lock(&vm->status_lock);
1431 list_del(&bo_va->vm_status);
1432 spin_unlock(&vm->status_lock);
1433
Christian König7fc11952015-07-30 11:53:42 +02001434 list_for_each_entry_safe(mapping, next, &bo_va->valids, list) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001435 list_del(&mapping->list);
1436 interval_tree_remove(&mapping->it, &vm->va);
Christian König93e3e432015-06-09 16:58:33 +02001437 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Christian König7fc11952015-07-30 11:53:42 +02001438 list_add(&mapping->list, &vm->freed);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001439 }
Christian König7fc11952015-07-30 11:53:42 +02001440 list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) {
1441 list_del(&mapping->list);
1442 interval_tree_remove(&mapping->it, &vm->va);
1443 kfree(mapping);
1444 }
Christian König32b41ac2016-03-08 18:03:27 +01001445
Chunming Zhoubb1e38a42015-08-03 18:19:38 +08001446 fence_put(bo_va->last_pt_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001447 kfree(bo_va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001448}
1449
1450/**
1451 * amdgpu_vm_bo_invalidate - mark the bo as invalid
1452 *
1453 * @adev: amdgpu_device pointer
1454 * @vm: requested vm
1455 * @bo: amdgpu buffer object
1456 *
Christian König8843dbb2016-01-26 12:17:11 +01001457 * Mark @bo as invalid.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001458 */
1459void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
1460 struct amdgpu_bo *bo)
1461{
1462 struct amdgpu_bo_va *bo_va;
1463
1464 list_for_each_entry(bo_va, &bo->va, bo_list) {
Christian König7fc11952015-07-30 11:53:42 +02001465 spin_lock(&bo_va->vm->status_lock);
1466 if (list_empty(&bo_va->vm_status))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001467 list_add(&bo_va->vm_status, &bo_va->vm->invalidated);
Christian König7fc11952015-07-30 11:53:42 +02001468 spin_unlock(&bo_va->vm->status_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001469 }
1470}
1471
1472/**
1473 * amdgpu_vm_init - initialize a vm instance
1474 *
1475 * @adev: amdgpu_device pointer
1476 * @vm: requested vm
1477 *
Christian König8843dbb2016-01-26 12:17:11 +01001478 * Init @vm fields.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001479 */
1480int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1481{
1482 const unsigned align = min(AMDGPU_VM_PTB_ALIGN_SIZE,
1483 AMDGPU_VM_PTE_COUNT * 8);
Michel Dänzer9571e1d2016-01-19 17:59:46 +09001484 unsigned pd_size, pd_entries;
Christian König2d55e452016-02-08 17:37:38 +01001485 unsigned ring_instance;
1486 struct amdgpu_ring *ring;
Christian König2bd9ccf2016-02-01 12:53:58 +01001487 struct amd_sched_rq *rq;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001488 int i, r;
1489
Christian Königbcb1ba32016-03-08 15:40:11 +01001490 for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
1491 vm->ids[i] = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001492 vm->va = RB_ROOT;
Chunming Zhou031e2982016-04-25 10:19:13 +08001493 vm->client_id = atomic64_inc_return(&adev->vm_manager.client_counter);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001494 spin_lock_init(&vm->status_lock);
1495 INIT_LIST_HEAD(&vm->invalidated);
Christian König7fc11952015-07-30 11:53:42 +02001496 INIT_LIST_HEAD(&vm->cleared);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001497 INIT_LIST_HEAD(&vm->freed);
Christian König20250212016-03-08 17:58:35 +01001498
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001499 pd_size = amdgpu_vm_directory_size(adev);
1500 pd_entries = amdgpu_vm_num_pdes(adev);
1501
1502 /* allocate page table array */
Michel Dänzer9571e1d2016-01-19 17:59:46 +09001503 vm->page_tables = drm_calloc_large(pd_entries, sizeof(struct amdgpu_vm_pt));
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001504 if (vm->page_tables == NULL) {
1505 DRM_ERROR("Cannot allocate memory for page table array\n");
1506 return -ENOMEM;
1507 }
1508
Christian König2bd9ccf2016-02-01 12:53:58 +01001509 /* create scheduler entity for page table updates */
Christian König2d55e452016-02-08 17:37:38 +01001510
1511 ring_instance = atomic_inc_return(&adev->vm_manager.vm_pte_next_ring);
1512 ring_instance %= adev->vm_manager.vm_pte_num_rings;
1513 ring = adev->vm_manager.vm_pte_rings[ring_instance];
Christian König2bd9ccf2016-02-01 12:53:58 +01001514 rq = &ring->sched.sched_rq[AMD_SCHED_PRIORITY_KERNEL];
1515 r = amd_sched_entity_init(&ring->sched, &vm->entity,
1516 rq, amdgpu_sched_jobs);
1517 if (r)
1518 return r;
1519
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +02001520 vm->page_directory_fence = NULL;
1521
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001522 r = amdgpu_bo_create(adev, pd_size, align, true,
Alex Deucher857d9132015-08-27 00:14:16 -04001523 AMDGPU_GEM_DOMAIN_VRAM,
1524 AMDGPU_GEM_CREATE_NO_CPU_ACCESS,
Christian König72d76682015-09-03 17:34:59 +02001525 NULL, NULL, &vm->page_directory);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001526 if (r)
Christian König2bd9ccf2016-02-01 12:53:58 +01001527 goto error_free_sched_entity;
1528
Chunming Zhouef9f0a82015-11-13 13:43:22 +08001529 r = amdgpu_bo_reserve(vm->page_directory, false);
Christian König2bd9ccf2016-02-01 12:53:58 +01001530 if (r)
1531 goto error_free_page_directory;
1532
1533 r = amdgpu_vm_clear_bo(adev, vm, vm->page_directory);
Chunming Zhouef9f0a82015-11-13 13:43:22 +08001534 amdgpu_bo_unreserve(vm->page_directory);
Christian König2bd9ccf2016-02-01 12:53:58 +01001535 if (r)
1536 goto error_free_page_directory;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001537
1538 return 0;
Christian König2bd9ccf2016-02-01 12:53:58 +01001539
1540error_free_page_directory:
1541 amdgpu_bo_unref(&vm->page_directory);
1542 vm->page_directory = NULL;
1543
1544error_free_sched_entity:
1545 amd_sched_entity_fini(&ring->sched, &vm->entity);
1546
1547 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001548}
1549
1550/**
1551 * amdgpu_vm_fini - tear down a vm instance
1552 *
1553 * @adev: amdgpu_device pointer
1554 * @vm: requested vm
1555 *
Christian König8843dbb2016-01-26 12:17:11 +01001556 * Tear down @vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001557 * Unbind the VM and remove all bos from the vm bo list
1558 */
1559void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1560{
1561 struct amdgpu_bo_va_mapping *mapping, *tmp;
1562 int i;
1563
Christian König2d55e452016-02-08 17:37:38 +01001564 amd_sched_entity_fini(vm->entity.sched, &vm->entity);
Christian König2bd9ccf2016-02-01 12:53:58 +01001565
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001566 if (!RB_EMPTY_ROOT(&vm->va)) {
1567 dev_err(adev->dev, "still active bo inside vm\n");
1568 }
1569 rbtree_postorder_for_each_entry_safe(mapping, tmp, &vm->va, it.rb) {
1570 list_del(&mapping->list);
1571 interval_tree_remove(&mapping->it, &vm->va);
1572 kfree(mapping);
1573 }
1574 list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
1575 list_del(&mapping->list);
1576 kfree(mapping);
1577 }
1578
1579 for (i = 0; i < amdgpu_vm_num_pdes(adev); i++)
Christian Königee1782c2015-12-11 21:01:23 +01001580 amdgpu_bo_unref(&vm->page_tables[i].entry.robj);
Michel Dänzer9571e1d2016-01-19 17:59:46 +09001581 drm_free_large(vm->page_tables);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001582
1583 amdgpu_bo_unref(&vm->page_directory);
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +02001584 fence_put(vm->page_directory_fence);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001585}
Christian Königea89f8c2015-11-15 20:52:06 +01001586
1587/**
Christian Königa9a78b32016-01-21 10:19:11 +01001588 * amdgpu_vm_manager_init - init the VM manager
1589 *
1590 * @adev: amdgpu_device pointer
1591 *
1592 * Initialize the VM manager structures
1593 */
1594void amdgpu_vm_manager_init(struct amdgpu_device *adev)
1595{
1596 unsigned i;
1597
1598 INIT_LIST_HEAD(&adev->vm_manager.ids_lru);
1599
1600 /* skip over VMID 0, since it is the system VM */
Christian König971fe9a92016-03-01 15:09:25 +01001601 for (i = 1; i < adev->vm_manager.num_ids; ++i) {
1602 amdgpu_vm_reset_id(adev, i);
Christian König832a9022016-02-15 12:33:02 +01001603 amdgpu_sync_create(&adev->vm_manager.ids[i].active);
Christian Königa9a78b32016-01-21 10:19:11 +01001604 list_add_tail(&adev->vm_manager.ids[i].list,
1605 &adev->vm_manager.ids_lru);
Christian König971fe9a92016-03-01 15:09:25 +01001606 }
Christian König2d55e452016-02-08 17:37:38 +01001607
Christian König1fbb2e92016-06-01 10:47:36 +02001608 adev->vm_manager.fence_context = fence_context_alloc(AMDGPU_MAX_RINGS);
1609 for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
1610 adev->vm_manager.seqno[i] = 0;
1611
Christian König2d55e452016-02-08 17:37:38 +01001612 atomic_set(&adev->vm_manager.vm_pte_next_ring, 0);
Christian Königb1c8a812016-05-04 10:34:03 +02001613 atomic64_set(&adev->vm_manager.client_counter, 0);
Christian Königa9a78b32016-01-21 10:19:11 +01001614}
1615
1616/**
Christian Königea89f8c2015-11-15 20:52:06 +01001617 * amdgpu_vm_manager_fini - cleanup VM manager
1618 *
1619 * @adev: amdgpu_device pointer
1620 *
1621 * Cleanup the VM manager and free resources.
1622 */
1623void amdgpu_vm_manager_fini(struct amdgpu_device *adev)
1624{
1625 unsigned i;
1626
Christian Königbcb1ba32016-03-08 15:40:11 +01001627 for (i = 0; i < AMDGPU_NUM_VM; ++i) {
1628 struct amdgpu_vm_id *id = &adev->vm_manager.ids[i];
1629
Christian König832a9022016-02-15 12:33:02 +01001630 fence_put(adev->vm_manager.ids[i].first);
1631 amdgpu_sync_free(&adev->vm_manager.ids[i].active);
Christian Königbcb1ba32016-03-08 15:40:11 +01001632 fence_put(id->flushed_updates);
1633 }
Christian Königea89f8c2015-11-15 20:52:06 +01001634}