blob: 06f24322e7c31bcfbb9dc867909ebe234968accb [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
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -040054/* Local structure. Encapsulate some VM table update parameters to reduce
55 * the number of function parameters
56 */
Christian König29efc4f2016-08-04 14:52:50 +020057struct amdgpu_pte_update_params {
Christian König27c5f362016-08-04 15:02:49 +020058 /* amdgpu device we do this update for */
59 struct amdgpu_device *adev;
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -040060 /* address where to copy page table entries from */
61 uint64_t src;
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -040062 /* indirect buffer to fill with commands */
63 struct amdgpu_ib *ib;
Christian Königafef8b82016-08-12 13:29:18 +020064 /* Function which actually does the update */
65 void (*func)(struct amdgpu_pte_update_params *params, uint64_t pe,
66 uint64_t addr, unsigned count, uint32_t incr,
67 uint32_t flags);
Chunming Zhou4c7e8852016-08-15 11:46:21 +080068 /* indicate update pt or its shadow */
69 bool shadow;
Harish Kasiviswanathanf4833c42016-04-21 10:40:18 -040070};
71
Alex Deucherd38ceaf2015-04-20 16:55:21 -040072/**
73 * amdgpu_vm_num_pde - return the number of page directory entries
74 *
75 * @adev: amdgpu_device pointer
76 *
Christian König8843dbb2016-01-26 12:17:11 +010077 * Calculate the number of page directory entries.
Alex Deucherd38ceaf2015-04-20 16:55:21 -040078 */
79static unsigned amdgpu_vm_num_pdes(struct amdgpu_device *adev)
80{
81 return adev->vm_manager.max_pfn >> amdgpu_vm_block_size;
82}
83
84/**
85 * amdgpu_vm_directory_size - returns the size of the page directory in bytes
86 *
87 * @adev: amdgpu_device pointer
88 *
Christian König8843dbb2016-01-26 12:17:11 +010089 * Calculate the size of the page directory in bytes.
Alex Deucherd38ceaf2015-04-20 16:55:21 -040090 */
91static unsigned amdgpu_vm_directory_size(struct amdgpu_device *adev)
92{
93 return AMDGPU_GPU_PAGE_ALIGN(amdgpu_vm_num_pdes(adev) * 8);
94}
95
96/**
Christian König56467eb2015-12-11 15:16:32 +010097 * amdgpu_vm_get_pd_bo - add the VM PD to a validation list
Alex Deucherd38ceaf2015-04-20 16:55:21 -040098 *
99 * @vm: vm providing the BOs
Christian König3c0eea62015-12-11 14:39:05 +0100100 * @validated: head of validation list
Christian König56467eb2015-12-11 15:16:32 +0100101 * @entry: entry to add
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400102 *
103 * Add the page directory to the list of BOs to
Christian König56467eb2015-12-11 15:16:32 +0100104 * validate for command submission.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400105 */
Christian König56467eb2015-12-11 15:16:32 +0100106void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm,
107 struct list_head *validated,
108 struct amdgpu_bo_list_entry *entry)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400109{
Christian König56467eb2015-12-11 15:16:32 +0100110 entry->robj = vm->page_directory;
Christian König56467eb2015-12-11 15:16:32 +0100111 entry->priority = 0;
112 entry->tv.bo = &vm->page_directory->tbo;
113 entry->tv.shared = true;
Christian König2f568db2016-02-23 12:36:59 +0100114 entry->user_pages = NULL;
Christian König56467eb2015-12-11 15:16:32 +0100115 list_add(&entry->tv.head, validated);
116}
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400117
Christian König56467eb2015-12-11 15:16:32 +0100118/**
Christian Königee1782c2015-12-11 21:01:23 +0100119 * amdgpu_vm_get_bos - add the vm BOs to a duplicates list
Christian König56467eb2015-12-11 15:16:32 +0100120 *
Christian König5a712a82016-06-21 16:28:15 +0200121 * @adev: amdgpu device pointer
Christian König56467eb2015-12-11 15:16:32 +0100122 * @vm: vm providing the BOs
Christian König3c0eea62015-12-11 14:39:05 +0100123 * @duplicates: head of duplicates list
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400124 *
Christian Königee1782c2015-12-11 21:01:23 +0100125 * Add the page directory to the BO duplicates list
126 * for command submission.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400127 */
Christian König5a712a82016-06-21 16:28:15 +0200128void amdgpu_vm_get_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
129 struct list_head *duplicates)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400130{
Christian König5a712a82016-06-21 16:28:15 +0200131 uint64_t num_evictions;
Christian Königee1782c2015-12-11 21:01:23 +0100132 unsigned i;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400133
Christian König5a712a82016-06-21 16:28:15 +0200134 /* We only need to validate the page tables
135 * if they aren't already valid.
136 */
137 num_evictions = atomic64_read(&adev->num_evictions);
138 if (num_evictions == vm->last_eviction_counter)
139 return;
140
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400141 /* add the vm page table to the list */
Christian Königee1782c2015-12-11 21:01:23 +0100142 for (i = 0; i <= vm->max_pde_used; ++i) {
143 struct amdgpu_bo_list_entry *entry = &vm->page_tables[i].entry;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400144
Christian Königee1782c2015-12-11 21:01:23 +0100145 if (!entry->robj)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400146 continue;
147
Christian Königee1782c2015-12-11 21:01:23 +0100148 list_add(&entry->tv.head, duplicates);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400149 }
Christian Königeceb8a12016-01-11 15:35:21 +0100150
151}
152
153/**
154 * amdgpu_vm_move_pt_bos_in_lru - move the PT BOs to the LRU tail
155 *
156 * @adev: amdgpu device instance
157 * @vm: vm providing the BOs
158 *
159 * Move the PT BOs to the tail of the LRU.
160 */
161void amdgpu_vm_move_pt_bos_in_lru(struct amdgpu_device *adev,
162 struct amdgpu_vm *vm)
163{
164 struct ttm_bo_global *glob = adev->mman.bdev.glob;
165 unsigned i;
166
167 spin_lock(&glob->lru_lock);
168 for (i = 0; i <= vm->max_pde_used; ++i) {
169 struct amdgpu_bo_list_entry *entry = &vm->page_tables[i].entry;
170
171 if (!entry->robj)
172 continue;
173
174 ttm_bo_move_to_lru_tail(&entry->robj->tbo);
175 }
176 spin_unlock(&glob->lru_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400177}
178
Chunming Zhou192b7dc2016-06-29 14:01:15 +0800179static bool amdgpu_vm_is_gpu_reset(struct amdgpu_device *adev,
180 struct amdgpu_vm_id *id)
181{
182 return id->current_gpu_reset_count !=
183 atomic_read(&adev->gpu_reset_counter) ? true : false;
184}
185
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400186/**
187 * amdgpu_vm_grab_id - allocate the next free VMID
188 *
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400189 * @vm: vm to allocate id for
Christian König7f8a5292015-07-20 16:09:40 +0200190 * @ring: ring we want to submit job to
191 * @sync: sync object where we add dependencies
Christian König94dd0a42016-01-18 17:01:42 +0100192 * @fence: fence protecting ID from reuse
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400193 *
Christian König7f8a5292015-07-20 16:09:40 +0200194 * Allocate an id for the vm, adding fences to the sync obj as necessary.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400195 */
Christian König7f8a5292015-07-20 16:09:40 +0200196int amdgpu_vm_grab_id(struct amdgpu_vm *vm, struct amdgpu_ring *ring,
Christian König4ff37a82016-02-26 16:18:26 +0100197 struct amdgpu_sync *sync, struct fence *fence,
Chunming Zhoufd53be32016-07-01 17:59:01 +0800198 struct amdgpu_job *job)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400199{
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400200 struct amdgpu_device *adev = ring->adev;
Christian König090b7672016-07-08 10:21:02 +0200201 uint64_t fence_context = adev->fence_context + ring->idx;
Christian König4ff37a82016-02-26 16:18:26 +0100202 struct fence *updates = sync->last_vm_update;
Christian König8d76001e2016-05-23 16:00:32 +0200203 struct amdgpu_vm_id *id, *idle;
Christian König1fbb2e92016-06-01 10:47:36 +0200204 struct fence **fences;
205 unsigned i;
206 int r = 0;
207
208 fences = kmalloc_array(sizeof(void *), adev->vm_manager.num_ids,
209 GFP_KERNEL);
210 if (!fences)
211 return -ENOMEM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400212
Christian König94dd0a42016-01-18 17:01:42 +0100213 mutex_lock(&adev->vm_manager.lock);
214
Christian König36fd7c52016-05-23 15:30:08 +0200215 /* Check if we have an idle VMID */
Christian König1fbb2e92016-06-01 10:47:36 +0200216 i = 0;
Christian König8d76001e2016-05-23 16:00:32 +0200217 list_for_each_entry(idle, &adev->vm_manager.ids_lru, list) {
Christian König1fbb2e92016-06-01 10:47:36 +0200218 fences[i] = amdgpu_sync_peek_fence(&idle->active, ring);
219 if (!fences[i])
Christian König36fd7c52016-05-23 15:30:08 +0200220 break;
Christian König1fbb2e92016-06-01 10:47:36 +0200221 ++i;
Christian König36fd7c52016-05-23 15:30:08 +0200222 }
Christian Königbcb1ba32016-03-08 15:40:11 +0100223
Christian König1fbb2e92016-06-01 10:47:36 +0200224 /* If we can't find a idle VMID to use, wait till one becomes available */
Christian König8d76001e2016-05-23 16:00:32 +0200225 if (&idle->list == &adev->vm_manager.ids_lru) {
Christian König1fbb2e92016-06-01 10:47:36 +0200226 u64 fence_context = adev->vm_manager.fence_context + ring->idx;
227 unsigned seqno = ++adev->vm_manager.seqno[ring->idx];
228 struct fence_array *array;
229 unsigned j;
Christian König8d76001e2016-05-23 16:00:32 +0200230
Christian König1fbb2e92016-06-01 10:47:36 +0200231 for (j = 0; j < i; ++j)
232 fence_get(fences[j]);
Christian König8d76001e2016-05-23 16:00:32 +0200233
Christian König1fbb2e92016-06-01 10:47:36 +0200234 array = fence_array_create(i, fences, fence_context,
235 seqno, true);
236 if (!array) {
237 for (j = 0; j < i; ++j)
238 fence_put(fences[j]);
239 kfree(fences);
240 r = -ENOMEM;
241 goto error;
242 }
Christian König8d76001e2016-05-23 16:00:32 +0200243
Christian König8d76001e2016-05-23 16:00:32 +0200244
Christian König1fbb2e92016-06-01 10:47:36 +0200245 r = amdgpu_sync_fence(ring->adev, sync, &array->base);
246 fence_put(&array->base);
247 if (r)
248 goto error;
Christian König8d76001e2016-05-23 16:00:32 +0200249
Christian König1fbb2e92016-06-01 10:47:36 +0200250 mutex_unlock(&adev->vm_manager.lock);
251 return 0;
Christian König8d76001e2016-05-23 16:00:32 +0200252
Christian König1fbb2e92016-06-01 10:47:36 +0200253 }
254 kfree(fences);
Christian König8d76001e2016-05-23 16:00:32 +0200255
Chunming Zhoufd53be32016-07-01 17:59:01 +0800256 job->vm_needs_flush = true;
Christian König1fbb2e92016-06-01 10:47:36 +0200257 /* Check if we can use a VMID already assigned to this VM */
258 i = ring->idx;
259 do {
260 struct fence *flushed;
Christian König8d76001e2016-05-23 16:00:32 +0200261
Christian König1fbb2e92016-06-01 10:47:36 +0200262 id = vm->ids[i++];
263 if (i == AMDGPU_MAX_RINGS)
264 i = 0;
265
266 /* Check all the prerequisites to using this VMID */
267 if (!id)
268 continue;
Chunming Zhou192b7dc2016-06-29 14:01:15 +0800269 if (amdgpu_vm_is_gpu_reset(adev, id))
Chunming Zhou6adb0512016-06-27 17:06:01 +0800270 continue;
Christian König1fbb2e92016-06-01 10:47:36 +0200271
272 if (atomic64_read(&id->owner) != vm->client_id)
273 continue;
274
Chunming Zhoufd53be32016-07-01 17:59:01 +0800275 if (job->vm_pd_addr != id->pd_gpu_addr)
Christian König1fbb2e92016-06-01 10:47:36 +0200276 continue;
277
Christian König090b7672016-07-08 10:21:02 +0200278 if (!id->last_flush)
279 continue;
280
281 if (id->last_flush->context != fence_context &&
282 !fence_is_signaled(id->last_flush))
Christian König1fbb2e92016-06-01 10:47:36 +0200283 continue;
284
285 flushed = id->flushed_updates;
286 if (updates &&
287 (!flushed || fence_is_later(updates, flushed)))
288 continue;
289
Christian König3dab83b2016-06-01 13:31:17 +0200290 /* Good we can use this VMID. Remember this submission as
291 * user of the VMID.
292 */
Christian König1fbb2e92016-06-01 10:47:36 +0200293 r = amdgpu_sync_fence(ring->adev, &id->active, fence);
294 if (r)
295 goto error;
Christian König8d76001e2016-05-23 16:00:32 +0200296
Chunming Zhou6adb0512016-06-27 17:06:01 +0800297 id->current_gpu_reset_count = atomic_read(&adev->gpu_reset_counter);
Christian König1fbb2e92016-06-01 10:47:36 +0200298 list_move_tail(&id->list, &adev->vm_manager.ids_lru);
299 vm->ids[ring->idx] = id;
Christian König8d76001e2016-05-23 16:00:32 +0200300
Chunming Zhoufd53be32016-07-01 17:59:01 +0800301 job->vm_id = id - adev->vm_manager.ids;
302 job->vm_needs_flush = false;
Christian König0c0fdf12016-07-08 10:48:24 +0200303 trace_amdgpu_vm_grab_id(vm, ring->idx, job);
Christian König8d76001e2016-05-23 16:00:32 +0200304
Christian König1fbb2e92016-06-01 10:47:36 +0200305 mutex_unlock(&adev->vm_manager.lock);
306 return 0;
Christian König8d76001e2016-05-23 16:00:32 +0200307
Christian König1fbb2e92016-06-01 10:47:36 +0200308 } while (i != ring->idx);
Chunming Zhou8e9fbeb2016-03-17 11:41:37 +0800309
Christian König1fbb2e92016-06-01 10:47:36 +0200310 /* Still no ID to use? Then use the idle one found earlier */
311 id = idle;
312
313 /* Remember this submission as user of the VMID */
314 r = amdgpu_sync_fence(ring->adev, &id->active, fence);
Christian König832a9022016-02-15 12:33:02 +0100315 if (r)
316 goto error;
Christian König4ff37a82016-02-26 16:18:26 +0100317
Christian König832a9022016-02-15 12:33:02 +0100318 fence_put(id->first);
319 id->first = fence_get(fence);
Christian König4ff37a82016-02-26 16:18:26 +0100320
Christian König41d9eb22016-03-01 16:46:18 +0100321 fence_put(id->last_flush);
322 id->last_flush = NULL;
323
Christian König832a9022016-02-15 12:33:02 +0100324 fence_put(id->flushed_updates);
325 id->flushed_updates = fence_get(updates);
Christian König4ff37a82016-02-26 16:18:26 +0100326
Chunming Zhoufd53be32016-07-01 17:59:01 +0800327 id->pd_gpu_addr = job->vm_pd_addr;
Chunming Zhoub46b8a82016-06-27 17:04:23 +0800328 id->current_gpu_reset_count = atomic_read(&adev->gpu_reset_counter);
Christian König832a9022016-02-15 12:33:02 +0100329 list_move_tail(&id->list, &adev->vm_manager.ids_lru);
Christian König0ea54b92016-05-04 10:20:01 +0200330 atomic64_set(&id->owner, vm->client_id);
Christian König832a9022016-02-15 12:33:02 +0100331 vm->ids[ring->idx] = id;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400332
Chunming Zhoufd53be32016-07-01 17:59:01 +0800333 job->vm_id = id - adev->vm_manager.ids;
Christian König0c0fdf12016-07-08 10:48:24 +0200334 trace_amdgpu_vm_grab_id(vm, ring->idx, job);
Christian König832a9022016-02-15 12:33:02 +0100335
336error:
Christian König94dd0a42016-01-18 17:01:42 +0100337 mutex_unlock(&adev->vm_manager.lock);
Christian Königa9a78b32016-01-21 10:19:11 +0100338 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400339}
340
Alex Deucher93dcc372016-06-17 17:05:15 -0400341static bool amdgpu_vm_ring_has_compute_vm_bug(struct amdgpu_ring *ring)
342{
343 struct amdgpu_device *adev = ring->adev;
344 const struct amdgpu_ip_block_version *ip_block;
345
346 if (ring->type != AMDGPU_RING_TYPE_COMPUTE)
347 /* only compute rings */
348 return false;
349
350 ip_block = amdgpu_get_ip_block(adev, AMD_IP_BLOCK_TYPE_GFX);
351 if (!ip_block)
352 return false;
353
354 if (ip_block->major <= 7) {
355 /* gfx7 has no workaround */
356 return true;
357 } else if (ip_block->major == 8) {
358 if (adev->gfx.mec_fw_version >= 673)
359 /* gfx8 is fixed in MEC firmware 673 */
360 return false;
361 else
362 return true;
363 }
364 return false;
365}
366
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400367/**
368 * amdgpu_vm_flush - hardware flush the vm
369 *
370 * @ring: ring to use for flush
Christian Königcffadc82016-03-01 13:34:49 +0100371 * @vm_id: vmid number to use
Christian König4ff37a82016-02-26 16:18:26 +0100372 * @pd_addr: address of the page directory
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400373 *
Christian König4ff37a82016-02-26 16:18:26 +0100374 * Emit a VM flush when it is necessary.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400375 */
Chunming Zhoufd53be32016-07-01 17:59:01 +0800376int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400377{
Christian König971fe9a92016-03-01 15:09:25 +0100378 struct amdgpu_device *adev = ring->adev;
Chunming Zhoufd53be32016-07-01 17:59:01 +0800379 struct amdgpu_vm_id *id = &adev->vm_manager.ids[job->vm_id];
Christian Königd564a062016-03-01 15:51:53 +0100380 bool gds_switch_needed = ring->funcs->emit_gds_switch && (
Chunming Zhoufd53be32016-07-01 17:59:01 +0800381 id->gds_base != job->gds_base ||
382 id->gds_size != job->gds_size ||
383 id->gws_base != job->gws_base ||
384 id->gws_size != job->gws_size ||
385 id->oa_base != job->oa_base ||
386 id->oa_size != job->oa_size);
Christian König41d9eb22016-03-01 16:46:18 +0100387 int r;
Christian Königd564a062016-03-01 15:51:53 +0100388
389 if (ring->funcs->emit_pipeline_sync && (
Chunming Zhoufd53be32016-07-01 17:59:01 +0800390 job->vm_needs_flush || gds_switch_needed ||
Alex Deucher93dcc372016-06-17 17:05:15 -0400391 amdgpu_vm_ring_has_compute_vm_bug(ring)))
Christian Königd564a062016-03-01 15:51:53 +0100392 amdgpu_ring_emit_pipeline_sync(ring);
Christian König971fe9a92016-03-01 15:09:25 +0100393
Chunming Zhouaa1c8902016-06-30 13:56:02 +0800394 if (ring->funcs->emit_vm_flush && (job->vm_needs_flush ||
395 amdgpu_vm_is_gpu_reset(adev, id))) {
Christian König41d9eb22016-03-01 16:46:18 +0100396 struct fence *fence;
397
Chunming Zhoufd53be32016-07-01 17:59:01 +0800398 trace_amdgpu_vm_flush(job->vm_pd_addr, ring->idx, job->vm_id);
399 amdgpu_ring_emit_vm_flush(ring, job->vm_id, job->vm_pd_addr);
Christian König41d9eb22016-03-01 16:46:18 +0100400
Christian König3dab83b2016-06-01 13:31:17 +0200401 r = amdgpu_fence_emit(ring, &fence);
402 if (r)
403 return r;
404
Christian König41d9eb22016-03-01 16:46:18 +0100405 mutex_lock(&adev->vm_manager.lock);
Christian König3dab83b2016-06-01 13:31:17 +0200406 fence_put(id->last_flush);
407 id->last_flush = fence;
Christian König41d9eb22016-03-01 16:46:18 +0100408 mutex_unlock(&adev->vm_manager.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400409 }
Christian Königcffadc82016-03-01 13:34:49 +0100410
Christian Königd564a062016-03-01 15:51:53 +0100411 if (gds_switch_needed) {
Chunming Zhoufd53be32016-07-01 17:59:01 +0800412 id->gds_base = job->gds_base;
413 id->gds_size = job->gds_size;
414 id->gws_base = job->gws_base;
415 id->gws_size = job->gws_size;
416 id->oa_base = job->oa_base;
417 id->oa_size = job->oa_size;
418 amdgpu_ring_emit_gds_switch(ring, job->vm_id,
419 job->gds_base, job->gds_size,
420 job->gws_base, job->gws_size,
421 job->oa_base, job->oa_size);
Christian König971fe9a92016-03-01 15:09:25 +0100422 }
Christian König41d9eb22016-03-01 16:46:18 +0100423
424 return 0;
Christian König971fe9a92016-03-01 15:09:25 +0100425}
426
427/**
428 * amdgpu_vm_reset_id - reset VMID to zero
429 *
430 * @adev: amdgpu device structure
431 * @vm_id: vmid number to use
432 *
433 * Reset saved GDW, GWS and OA to force switch on next flush.
434 */
435void amdgpu_vm_reset_id(struct amdgpu_device *adev, unsigned vm_id)
436{
Christian Königbcb1ba32016-03-08 15:40:11 +0100437 struct amdgpu_vm_id *id = &adev->vm_manager.ids[vm_id];
Christian König971fe9a92016-03-01 15:09:25 +0100438
Christian Königbcb1ba32016-03-08 15:40:11 +0100439 id->gds_base = 0;
440 id->gds_size = 0;
441 id->gws_base = 0;
442 id->gws_size = 0;
443 id->oa_base = 0;
444 id->oa_size = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400445}
446
447/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400448 * amdgpu_vm_bo_find - find the bo_va for a specific vm & bo
449 *
450 * @vm: requested vm
451 * @bo: requested buffer object
452 *
Christian König8843dbb2016-01-26 12:17:11 +0100453 * Find @bo inside the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400454 * Search inside the @bos vm list for the requested vm
455 * Returns the found bo_va or NULL if none is found
456 *
457 * Object has to be reserved!
458 */
459struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
460 struct amdgpu_bo *bo)
461{
462 struct amdgpu_bo_va *bo_va;
463
464 list_for_each_entry(bo_va, &bo->va, bo_list) {
465 if (bo_va->vm == vm) {
466 return bo_va;
467 }
468 }
469 return NULL;
470}
471
472/**
Christian Königafef8b82016-08-12 13:29:18 +0200473 * amdgpu_vm_do_set_ptes - helper to call the right asic function
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400474 *
Christian König29efc4f2016-08-04 14:52:50 +0200475 * @params: see amdgpu_pte_update_params definition
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400476 * @pe: addr of the page entry
477 * @addr: dst addr to write into pe
478 * @count: number of page entries to update
479 * @incr: increase next addr by incr bytes
480 * @flags: hw access flags
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400481 *
482 * Traces the parameters and calls the right asic functions
483 * to setup the page table using the DMA.
484 */
Christian Königafef8b82016-08-12 13:29:18 +0200485static void amdgpu_vm_do_set_ptes(struct amdgpu_pte_update_params *params,
486 uint64_t pe, uint64_t addr,
487 unsigned count, uint32_t incr,
488 uint32_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400489{
Christian Königec2f05f2016-09-25 16:11:52 +0200490 trace_amdgpu_vm_set_ptes(pe, addr, count, incr, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400491
Christian Königafef8b82016-08-12 13:29:18 +0200492 if (count < 3) {
Christian Königde9ea7b2016-08-12 11:33:30 +0200493 amdgpu_vm_write_pte(params->adev, params->ib, pe,
494 addr | flags, count, incr);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400495
496 } else {
Christian König27c5f362016-08-04 15:02:49 +0200497 amdgpu_vm_set_pte_pde(params->adev, params->ib, pe, addr,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400498 count, incr, flags);
499 }
500}
501
502/**
Christian Königafef8b82016-08-12 13:29:18 +0200503 * amdgpu_vm_do_copy_ptes - copy the PTEs from the GART
504 *
505 * @params: see amdgpu_pte_update_params definition
506 * @pe: addr of the page entry
507 * @addr: dst addr to write into pe
508 * @count: number of page entries to update
509 * @incr: increase next addr by incr bytes
510 * @flags: hw access flags
511 *
512 * Traces the parameters and calls the DMA function to copy the PTEs.
513 */
514static void amdgpu_vm_do_copy_ptes(struct amdgpu_pte_update_params *params,
515 uint64_t pe, uint64_t addr,
516 unsigned count, uint32_t incr,
517 uint32_t flags)
518{
Christian Königec2f05f2016-09-25 16:11:52 +0200519 uint64_t src = (params->src + (addr >> 12) * 8);
Christian Königafef8b82016-08-12 13:29:18 +0200520
Christian Königec2f05f2016-09-25 16:11:52 +0200521
522 trace_amdgpu_vm_copy_ptes(pe, src, count);
523
524 amdgpu_vm_copy_pte(params->adev, params->ib, pe, src, count);
Christian Königafef8b82016-08-12 13:29:18 +0200525}
526
527/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400528 * amdgpu_vm_clear_bo - initially clear the page dir/table
529 *
530 * @adev: amdgpu_device pointer
531 * @bo: bo to clear
Chunming Zhouef9f0a82015-11-13 13:43:22 +0800532 *
533 * need to reserve bo first before calling it.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400534 */
535static int amdgpu_vm_clear_bo(struct amdgpu_device *adev,
Christian König2bd9ccf2016-02-01 12:53:58 +0100536 struct amdgpu_vm *vm,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400537 struct amdgpu_bo *bo)
538{
Christian König2d55e452016-02-08 17:37:38 +0100539 struct amdgpu_ring *ring;
Chunming Zhou4af9f072015-08-03 12:57:31 +0800540 struct fence *fence = NULL;
Christian Königd71518b2016-02-01 12:20:25 +0100541 struct amdgpu_job *job;
Christian König29efc4f2016-08-04 14:52:50 +0200542 struct amdgpu_pte_update_params params;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400543 unsigned entries;
544 uint64_t addr;
545 int r;
546
Christian König2d55e452016-02-08 17:37:38 +0100547 ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
548
monk.liuca952612015-05-25 14:44:05 +0800549 r = reservation_object_reserve_shared(bo->tbo.resv);
550 if (r)
551 return r;
552
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400553 r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
554 if (r)
Chunming Zhouef9f0a82015-11-13 13:43:22 +0800555 goto error;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400556
Christian König0fc86832016-09-16 11:46:23 +0200557 r = amdgpu_ttm_bind(&bo->tbo, &bo->tbo.mem);
558 if (r)
559 goto error;
560
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400561 addr = amdgpu_bo_gpu_offset(bo);
562 entries = amdgpu_bo_size(bo) / 8;
563
Christian Königd71518b2016-02-01 12:20:25 +0100564 r = amdgpu_job_alloc_with_ib(adev, 64, &job);
565 if (r)
Chunming Zhouef9f0a82015-11-13 13:43:22 +0800566 goto error;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400567
Christian König27c5f362016-08-04 15:02:49 +0200568 memset(&params, 0, sizeof(params));
569 params.adev = adev;
Christian König29efc4f2016-08-04 14:52:50 +0200570 params.ib = &job->ibs[0];
Christian Königafef8b82016-08-12 13:29:18 +0200571 amdgpu_vm_do_set_ptes(&params, addr, 0, entries, 0, 0);
Christian Königd71518b2016-02-01 12:20:25 +0100572 amdgpu_ring_pad_ib(ring, &job->ibs[0]);
573
574 WARN_ON(job->ibs[0].length_dw > 64);
Christian König2bd9ccf2016-02-01 12:53:58 +0100575 r = amdgpu_job_submit(job, ring, &vm->entity,
576 AMDGPU_FENCE_OWNER_VM, &fence);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400577 if (r)
578 goto error_free;
579
Christian Königd71518b2016-02-01 12:20:25 +0100580 amdgpu_bo_fence(bo, fence, true);
Chunming Zhou281b4222015-08-12 12:58:31 +0800581 fence_put(fence);
Chunming Zhoucadf97b2016-01-15 11:25:00 +0800582 return 0;
Chunming Zhouef9f0a82015-11-13 13:43:22 +0800583
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400584error_free:
Christian Königd71518b2016-02-01 12:20:25 +0100585 amdgpu_job_free(job);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400586
Chunming Zhouef9f0a82015-11-13 13:43:22 +0800587error:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400588 return r;
589}
590
591/**
Christian Königb07c9d22015-11-30 13:26:07 +0100592 * amdgpu_vm_map_gart - Resolve gart mapping of addr
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400593 *
Christian Königb07c9d22015-11-30 13:26:07 +0100594 * @pages_addr: optional DMA address to use for lookup
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400595 * @addr: the unmapped addr
596 *
597 * Look up the physical address of the page that the pte resolves
Christian Königb07c9d22015-11-30 13:26:07 +0100598 * to and return the pointer for the page table entry.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400599 */
Christian Königde9ea7b2016-08-12 11:33:30 +0200600static uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t addr)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400601{
602 uint64_t result;
603
Christian Königde9ea7b2016-08-12 11:33:30 +0200604 /* page table offset */
605 result = pages_addr[addr >> PAGE_SHIFT];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400606
Christian Königde9ea7b2016-08-12 11:33:30 +0200607 /* in case cpu page size != gpu page size*/
608 result |= addr & (~PAGE_MASK);
Christian Königb07c9d22015-11-30 13:26:07 +0100609
610 result &= 0xFFFFFFFFFFFFF000ULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400611
612 return result;
613}
614
Chunming Zhou6557e3d2016-08-15 11:36:54 +0800615static int amdgpu_vm_update_pd_or_shadow(struct amdgpu_device *adev,
616 struct amdgpu_vm *vm,
617 bool shadow)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400618{
Christian König2d55e452016-02-08 17:37:38 +0100619 struct amdgpu_ring *ring;
Chunming Zhou6557e3d2016-08-15 11:36:54 +0800620 struct amdgpu_bo *pd = shadow ? vm->page_directory->shadow :
621 vm->page_directory;
622 uint64_t pd_addr;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400623 uint32_t incr = AMDGPU_VM_PTE_COUNT * 8;
624 uint64_t last_pde = ~0, last_pt = ~0;
625 unsigned count = 0, pt_idx, ndw;
Christian Königd71518b2016-02-01 12:20:25 +0100626 struct amdgpu_job *job;
Christian König29efc4f2016-08-04 14:52:50 +0200627 struct amdgpu_pte_update_params params;
Chunming Zhou4af9f072015-08-03 12:57:31 +0800628 struct fence *fence = NULL;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800629
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400630 int r;
631
Chunming Zhou6557e3d2016-08-15 11:36:54 +0800632 if (!pd)
633 return 0;
Christian König0fc86832016-09-16 11:46:23 +0200634
635 r = amdgpu_ttm_bind(&pd->tbo, &pd->tbo.mem);
636 if (r)
637 return r;
638
Chunming Zhou6557e3d2016-08-15 11:36:54 +0800639 pd_addr = amdgpu_bo_gpu_offset(pd);
Christian König2d55e452016-02-08 17:37:38 +0100640 ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
641
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400642 /* padding, etc. */
643 ndw = 64;
644
645 /* assume the worst case */
646 ndw += vm->max_pde_used * 6;
647
Christian Königd71518b2016-02-01 12:20:25 +0100648 r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
649 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400650 return r;
Christian Königd71518b2016-02-01 12:20:25 +0100651
Christian König27c5f362016-08-04 15:02:49 +0200652 memset(&params, 0, sizeof(params));
653 params.adev = adev;
Christian König29efc4f2016-08-04 14:52:50 +0200654 params.ib = &job->ibs[0];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400655
656 /* walk over the address space and update the page directory */
657 for (pt_idx = 0; pt_idx <= vm->max_pde_used; ++pt_idx) {
Christian Königee1782c2015-12-11 21:01:23 +0100658 struct amdgpu_bo *bo = vm->page_tables[pt_idx].entry.robj;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400659 uint64_t pde, pt;
660
661 if (bo == NULL)
662 continue;
663
Christian König0fc86832016-09-16 11:46:23 +0200664 if (bo->shadow) {
665 struct amdgpu_bo *shadow = bo->shadow;
666
667 r = amdgpu_ttm_bind(&shadow->tbo, &shadow->tbo.mem);
668 if (r)
669 return r;
670 }
671
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400672 pt = amdgpu_bo_gpu_offset(bo);
Chunming Zhou6557e3d2016-08-15 11:36:54 +0800673 if (!shadow) {
674 if (vm->page_tables[pt_idx].addr == pt)
675 continue;
676 vm->page_tables[pt_idx].addr = pt;
677 } else {
678 if (vm->page_tables[pt_idx].shadow_addr == pt)
679 continue;
680 vm->page_tables[pt_idx].shadow_addr = pt;
681 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400682
683 pde = pd_addr + pt_idx * 8;
684 if (((last_pde + 8 * count) != pde) ||
Christian König96105e52016-08-12 12:59:59 +0200685 ((last_pt + incr * count) != pt) ||
686 (count == AMDGPU_VM_MAX_UPDATE_SIZE)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400687
688 if (count) {
Christian Königafef8b82016-08-12 13:29:18 +0200689 amdgpu_vm_do_set_ptes(&params, last_pde,
690 last_pt, count, incr,
691 AMDGPU_PTE_VALID);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400692 }
693
694 count = 1;
695 last_pde = pde;
696 last_pt = pt;
697 } else {
698 ++count;
699 }
700 }
701
702 if (count)
Christian Königafef8b82016-08-12 13:29:18 +0200703 amdgpu_vm_do_set_ptes(&params, last_pde, last_pt,
704 count, incr, AMDGPU_PTE_VALID);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400705
Christian König29efc4f2016-08-04 14:52:50 +0200706 if (params.ib->length_dw != 0) {
707 amdgpu_ring_pad_ib(ring, params.ib);
Christian Könige86f9ce2016-02-08 12:13:05 +0100708 amdgpu_sync_resv(adev, &job->sync, pd->tbo.resv,
709 AMDGPU_FENCE_OWNER_VM);
Christian König29efc4f2016-08-04 14:52:50 +0200710 WARN_ON(params.ib->length_dw > ndw);
Christian König2bd9ccf2016-02-01 12:53:58 +0100711 r = amdgpu_job_submit(job, ring, &vm->entity,
712 AMDGPU_FENCE_OWNER_VM, &fence);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800713 if (r)
714 goto error_free;
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +0200715
Chunming Zhou4af9f072015-08-03 12:57:31 +0800716 amdgpu_bo_fence(pd, fence, true);
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +0200717 fence_put(vm->page_directory_fence);
718 vm->page_directory_fence = fence_get(fence);
Chunming Zhou281b4222015-08-12 12:58:31 +0800719 fence_put(fence);
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800720
Christian Königd71518b2016-02-01 12:20:25 +0100721 } else {
722 amdgpu_job_free(job);
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800723 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400724
725 return 0;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +0800726
727error_free:
Christian Königd71518b2016-02-01 12:20:25 +0100728 amdgpu_job_free(job);
Chunming Zhou4af9f072015-08-03 12:57:31 +0800729 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400730}
731
Chunming Zhou6557e3d2016-08-15 11:36:54 +0800732/*
733 * amdgpu_vm_update_pdes - make sure that page directory is valid
734 *
735 * @adev: amdgpu_device pointer
736 * @vm: requested vm
737 * @start: start of GPU address range
738 * @end: end of GPU address range
739 *
740 * Allocates new page tables if necessary
741 * and updates the page directory.
742 * Returns 0 for success, error for failure.
743 */
744int amdgpu_vm_update_page_directory(struct amdgpu_device *adev,
745 struct amdgpu_vm *vm)
746{
747 int r;
748
749 r = amdgpu_vm_update_pd_or_shadow(adev, vm, true);
750 if (r)
751 return r;
752 return amdgpu_vm_update_pd_or_shadow(adev, vm, false);
753}
754
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400755/**
Christian König92696dd2016-08-05 13:56:35 +0200756 * amdgpu_vm_update_ptes - make sure that page tables are valid
757 *
758 * @params: see amdgpu_pte_update_params definition
759 * @vm: requested vm
760 * @start: start of GPU address range
761 * @end: end of GPU address range
762 * @dst: destination address to map to, the next dst inside the function
763 * @flags: mapping flags
764 *
765 * Update the page tables in the range @start - @end.
766 */
767static void amdgpu_vm_update_ptes(struct amdgpu_pte_update_params *params,
768 struct amdgpu_vm *vm,
769 uint64_t start, uint64_t end,
770 uint64_t dst, uint32_t flags)
771{
772 const uint64_t mask = AMDGPU_VM_PTE_COUNT - 1;
773
774 uint64_t cur_pe_start, cur_nptes, cur_dst;
775 uint64_t addr; /* next GPU address to be updated */
776 uint64_t pt_idx;
777 struct amdgpu_bo *pt;
778 unsigned nptes; /* next number of ptes to be updated */
779 uint64_t next_pe_start;
780
781 /* initialize the variables */
782 addr = start;
783 pt_idx = addr >> amdgpu_vm_block_size;
784 pt = vm->page_tables[pt_idx].entry.robj;
Chunming Zhou4c7e8852016-08-15 11:46:21 +0800785 if (params->shadow) {
786 if (!pt->shadow)
787 return;
788 pt = vm->page_tables[pt_idx].entry.robj->shadow;
789 }
Christian König92696dd2016-08-05 13:56:35 +0200790 if ((addr & ~mask) == (end & ~mask))
791 nptes = end - addr;
792 else
793 nptes = AMDGPU_VM_PTE_COUNT - (addr & mask);
794
795 cur_pe_start = amdgpu_bo_gpu_offset(pt);
796 cur_pe_start += (addr & mask) * 8;
797 cur_nptes = nptes;
798 cur_dst = dst;
799
800 /* for next ptb*/
801 addr += nptes;
802 dst += nptes * AMDGPU_GPU_PAGE_SIZE;
803
804 /* walk over the address space and update the page tables */
805 while (addr < end) {
806 pt_idx = addr >> amdgpu_vm_block_size;
807 pt = vm->page_tables[pt_idx].entry.robj;
Chunming Zhou4c7e8852016-08-15 11:46:21 +0800808 if (params->shadow) {
809 if (!pt->shadow)
810 return;
811 pt = vm->page_tables[pt_idx].entry.robj->shadow;
812 }
Christian König92696dd2016-08-05 13:56:35 +0200813
814 if ((addr & ~mask) == (end & ~mask))
815 nptes = end - addr;
816 else
817 nptes = AMDGPU_VM_PTE_COUNT - (addr & mask);
818
819 next_pe_start = amdgpu_bo_gpu_offset(pt);
820 next_pe_start += (addr & mask) * 8;
821
Christian König96105e52016-08-12 12:59:59 +0200822 if ((cur_pe_start + 8 * cur_nptes) == next_pe_start &&
823 ((cur_nptes + nptes) <= AMDGPU_VM_MAX_UPDATE_SIZE)) {
Christian König92696dd2016-08-05 13:56:35 +0200824 /* The next ptb is consecutive to current ptb.
Christian Königafef8b82016-08-12 13:29:18 +0200825 * Don't call the update function now.
Christian König92696dd2016-08-05 13:56:35 +0200826 * Will update two ptbs together in future.
827 */
828 cur_nptes += nptes;
829 } else {
Christian Königafef8b82016-08-12 13:29:18 +0200830 params->func(params, cur_pe_start, cur_dst, cur_nptes,
831 AMDGPU_GPU_PAGE_SIZE, flags);
Christian König92696dd2016-08-05 13:56:35 +0200832
833 cur_pe_start = next_pe_start;
834 cur_nptes = nptes;
835 cur_dst = dst;
836 }
837
838 /* for next ptb*/
839 addr += nptes;
840 dst += nptes * AMDGPU_GPU_PAGE_SIZE;
841 }
842
Christian Königafef8b82016-08-12 13:29:18 +0200843 params->func(params, cur_pe_start, cur_dst, cur_nptes,
844 AMDGPU_GPU_PAGE_SIZE, flags);
Christian König92696dd2016-08-05 13:56:35 +0200845}
846
847/*
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400848 * amdgpu_vm_frag_ptes - add fragment information to PTEs
849 *
Christian König29efc4f2016-08-04 14:52:50 +0200850 * @params: see amdgpu_pte_update_params definition
Christian König92696dd2016-08-05 13:56:35 +0200851 * @vm: requested vm
852 * @start: first PTE to handle
853 * @end: last PTE to handle
854 * @dst: addr those PTEs should point to
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400855 * @flags: hw mapping flags
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400856 */
Christian König27c5f362016-08-04 15:02:49 +0200857static void amdgpu_vm_frag_ptes(struct amdgpu_pte_update_params *params,
Christian König92696dd2016-08-05 13:56:35 +0200858 struct amdgpu_vm *vm,
859 uint64_t start, uint64_t end,
860 uint64_t dst, uint32_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400861{
862 /**
863 * The MC L1 TLB supports variable sized pages, based on a fragment
864 * field in the PTE. When this field is set to a non-zero value, page
865 * granularity is increased from 4KB to (1 << (12 + frag)). The PTE
866 * flags are considered valid for all PTEs within the fragment range
867 * and corresponding mappings are assumed to be physically contiguous.
868 *
869 * The L1 TLB can store a single PTE for the whole fragment,
870 * significantly increasing the space available for translation
871 * caching. This leads to large improvements in throughput when the
872 * TLB is under pressure.
873 *
874 * The L2 TLB distributes small and large fragments into two
875 * asymmetric partitions. The large fragment cache is significantly
876 * larger. Thus, we try to use large fragments wherever possible.
877 * Userspace can support this by aligning virtual base address and
878 * allocation size to the fragment size.
879 */
880
Christian König80366172016-10-04 13:39:43 +0200881 /* SI and newer are optimized for 64KB */
882 uint64_t frag_flags = AMDGPU_PTE_FRAG(AMDGPU_LOG2_PAGES_PER_FRAG);
883 uint64_t frag_align = 1 << AMDGPU_LOG2_PAGES_PER_FRAG;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400884
Christian König92696dd2016-08-05 13:56:35 +0200885 uint64_t frag_start = ALIGN(start, frag_align);
886 uint64_t frag_end = end & ~(frag_align - 1);
Christian König31f6c1f2016-01-26 12:37:49 +0100887
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400888 /* system pages are non continuously */
Christian Königb7fc2cb2016-08-11 16:44:15 +0200889 if (params->src || !(flags & AMDGPU_PTE_VALID) ||
Christian König92696dd2016-08-05 13:56:35 +0200890 (frag_start >= frag_end)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400891
Christian König92696dd2016-08-05 13:56:35 +0200892 amdgpu_vm_update_ptes(params, vm, start, end, dst, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400893 return;
894 }
895
896 /* handle the 4K area at the beginning */
Christian König92696dd2016-08-05 13:56:35 +0200897 if (start != frag_start) {
898 amdgpu_vm_update_ptes(params, vm, start, frag_start,
899 dst, flags);
900 dst += (frag_start - start) * AMDGPU_GPU_PAGE_SIZE;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400901 }
902
903 /* handle the area in the middle */
Christian König92696dd2016-08-05 13:56:35 +0200904 amdgpu_vm_update_ptes(params, vm, frag_start, frag_end, dst,
Christian König80366172016-10-04 13:39:43 +0200905 flags | frag_flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400906
907 /* handle the 4K area at the end */
Christian König92696dd2016-08-05 13:56:35 +0200908 if (frag_end != end) {
909 dst += (frag_end - frag_start) * AMDGPU_GPU_PAGE_SIZE;
910 amdgpu_vm_update_ptes(params, vm, frag_end, end, dst, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400911 }
912}
913
914/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400915 * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table
916 *
917 * @adev: amdgpu_device pointer
Christian König3cabaa52016-06-06 10:17:58 +0200918 * @exclusive: fence we need to sync to
Christian Königfa3ab3c2016-03-18 21:00:35 +0100919 * @src: address where to copy page table entries from
920 * @pages_addr: DMA addresses to use for mapping
Christian Königa14faa62016-01-25 14:27:31 +0100921 * @vm: requested vm
922 * @start: start of mapped range
923 * @last: last mapped entry
924 * @flags: flags for the entries
925 * @addr: addr to set the area to
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400926 * @fence: optional resulting fence
927 *
Christian Königa14faa62016-01-25 14:27:31 +0100928 * Fill in the page table entries between @start and @last.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400929 * Returns 0 for success, -EINVAL for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400930 */
931static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
Christian König3cabaa52016-06-06 10:17:58 +0200932 struct fence *exclusive,
Christian Königfa3ab3c2016-03-18 21:00:35 +0100933 uint64_t src,
934 dma_addr_t *pages_addr,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400935 struct amdgpu_vm *vm,
Christian Königa14faa62016-01-25 14:27:31 +0100936 uint64_t start, uint64_t last,
937 uint32_t flags, uint64_t addr,
938 struct fence **fence)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400939{
Christian König2d55e452016-02-08 17:37:38 +0100940 struct amdgpu_ring *ring;
Christian Königa1e08d32016-01-26 11:40:46 +0100941 void *owner = AMDGPU_FENCE_OWNER_VM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400942 unsigned nptes, ncmds, ndw;
Christian Königd71518b2016-02-01 12:20:25 +0100943 struct amdgpu_job *job;
Christian König29efc4f2016-08-04 14:52:50 +0200944 struct amdgpu_pte_update_params params;
Chunming Zhou4af9f072015-08-03 12:57:31 +0800945 struct fence *f = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400946 int r;
947
Christian Königafef8b82016-08-12 13:29:18 +0200948 memset(&params, 0, sizeof(params));
949 params.adev = adev;
950 params.src = src;
951
Christian König2d55e452016-02-08 17:37:38 +0100952 ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
Christian König27c5f362016-08-04 15:02:49 +0200953
Christian König29efc4f2016-08-04 14:52:50 +0200954 memset(&params, 0, sizeof(params));
Christian König27c5f362016-08-04 15:02:49 +0200955 params.adev = adev;
Christian König29efc4f2016-08-04 14:52:50 +0200956 params.src = src;
Christian König2d55e452016-02-08 17:37:38 +0100957
Christian Königa1e08d32016-01-26 11:40:46 +0100958 /* sync to everything on unmapping */
959 if (!(flags & AMDGPU_PTE_VALID))
960 owner = AMDGPU_FENCE_OWNER_UNDEFINED;
961
Christian Königa14faa62016-01-25 14:27:31 +0100962 nptes = last - start + 1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400963
964 /*
965 * reserve space for one command every (1 << BLOCK_SIZE)
966 * entries or 2k dwords (whatever is smaller)
967 */
968 ncmds = (nptes >> min(amdgpu_vm_block_size, 11)) + 1;
969
970 /* padding, etc. */
971 ndw = 64;
972
Christian Königb0456f92016-08-11 14:06:54 +0200973 if (src) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400974 /* only copy commands needed */
975 ndw += ncmds * 7;
976
Christian Königafef8b82016-08-12 13:29:18 +0200977 params.func = amdgpu_vm_do_copy_ptes;
978
Christian Königb0456f92016-08-11 14:06:54 +0200979 } else if (pages_addr) {
980 /* copy commands needed */
981 ndw += ncmds * 7;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400982
Christian Königb0456f92016-08-11 14:06:54 +0200983 /* and also PTEs */
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400984 ndw += nptes * 2;
985
Christian Königafef8b82016-08-12 13:29:18 +0200986 params.func = amdgpu_vm_do_copy_ptes;
987
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400988 } else {
989 /* set page commands needed */
990 ndw += ncmds * 10;
991
992 /* two extra commands for begin/end of fragment */
993 ndw += 2 * 10;
Christian Königafef8b82016-08-12 13:29:18 +0200994
995 params.func = amdgpu_vm_do_set_ptes;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400996 }
997
Christian Königd71518b2016-02-01 12:20:25 +0100998 r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
999 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001000 return r;
Christian Königd71518b2016-02-01 12:20:25 +01001001
Christian König29efc4f2016-08-04 14:52:50 +02001002 params.ib = &job->ibs[0];
Chunming Zhoud5fc5e82015-07-21 16:52:10 +08001003
Christian Königb0456f92016-08-11 14:06:54 +02001004 if (!src && pages_addr) {
1005 uint64_t *pte;
1006 unsigned i;
1007
1008 /* Put the PTEs at the end of the IB. */
1009 i = ndw - nptes * 2;
1010 pte= (uint64_t *)&(job->ibs->ptr[i]);
1011 params.src = job->ibs->gpu_addr + i * 4;
1012
1013 for (i = 0; i < nptes; ++i) {
1014 pte[i] = amdgpu_vm_map_gart(pages_addr, addr + i *
1015 AMDGPU_GPU_PAGE_SIZE);
1016 pte[i] |= flags;
1017 }
Christian Königd7a4ac62016-09-25 11:54:00 +02001018 addr = 0;
Christian Königb0456f92016-08-11 14:06:54 +02001019 }
1020
Christian König3cabaa52016-06-06 10:17:58 +02001021 r = amdgpu_sync_fence(adev, &job->sync, exclusive);
1022 if (r)
1023 goto error_free;
1024
Christian Könige86f9ce2016-02-08 12:13:05 +01001025 r = amdgpu_sync_resv(adev, &job->sync, vm->page_directory->tbo.resv,
Christian Königa1e08d32016-01-26 11:40:46 +01001026 owner);
1027 if (r)
1028 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001029
Christian Königa1e08d32016-01-26 11:40:46 +01001030 r = reservation_object_reserve_shared(vm->page_directory->tbo.resv);
1031 if (r)
1032 goto error_free;
1033
Chunming Zhou4c7e8852016-08-15 11:46:21 +08001034 params.shadow = true;
1035 amdgpu_vm_frag_ptes(&params, vm, start, last + 1, addr, flags);
1036 params.shadow = false;
Christian König92696dd2016-08-05 13:56:35 +02001037 amdgpu_vm_frag_ptes(&params, vm, start, last + 1, addr, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001038
Christian König29efc4f2016-08-04 14:52:50 +02001039 amdgpu_ring_pad_ib(ring, params.ib);
1040 WARN_ON(params.ib->length_dw > ndw);
Christian König2bd9ccf2016-02-01 12:53:58 +01001041 r = amdgpu_job_submit(job, ring, &vm->entity,
1042 AMDGPU_FENCE_OWNER_VM, &f);
Chunming Zhou4af9f072015-08-03 12:57:31 +08001043 if (r)
1044 goto error_free;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001045
Christian Königbf60efd2015-09-04 10:47:56 +02001046 amdgpu_bo_fence(vm->page_directory, f, true);
Chunming Zhou4af9f072015-08-03 12:57:31 +08001047 if (fence) {
1048 fence_put(*fence);
1049 *fence = fence_get(f);
1050 }
Chunming Zhou281b4222015-08-12 12:58:31 +08001051 fence_put(f);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001052 return 0;
Chunming Zhoud5fc5e82015-07-21 16:52:10 +08001053
1054error_free:
Christian Königd71518b2016-02-01 12:20:25 +01001055 amdgpu_job_free(job);
Chunming Zhou4af9f072015-08-03 12:57:31 +08001056 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001057}
1058
1059/**
Christian Königa14faa62016-01-25 14:27:31 +01001060 * amdgpu_vm_bo_split_mapping - split a mapping into smaller chunks
1061 *
1062 * @adev: amdgpu_device pointer
Christian König3cabaa52016-06-06 10:17:58 +02001063 * @exclusive: fence we need to sync to
Christian König8358dce2016-03-30 10:50:25 +02001064 * @gtt_flags: flags as they are used for GTT
1065 * @pages_addr: DMA addresses to use for mapping
Christian Königa14faa62016-01-25 14:27:31 +01001066 * @vm: requested vm
1067 * @mapping: mapped range and flags to use for the update
1068 * @addr: addr to set the area to
Christian König8358dce2016-03-30 10:50:25 +02001069 * @flags: HW flags for the mapping
Christian Königa14faa62016-01-25 14:27:31 +01001070 * @fence: optional resulting fence
1071 *
1072 * Split the mapping into smaller chunks so that each update fits
1073 * into a SDMA IB.
1074 * Returns 0 for success, -EINVAL for failure.
1075 */
1076static int amdgpu_vm_bo_split_mapping(struct amdgpu_device *adev,
Christian König3cabaa52016-06-06 10:17:58 +02001077 struct fence *exclusive,
Christian Königa14faa62016-01-25 14:27:31 +01001078 uint32_t gtt_flags,
Christian König8358dce2016-03-30 10:50:25 +02001079 dma_addr_t *pages_addr,
Christian Königa14faa62016-01-25 14:27:31 +01001080 struct amdgpu_vm *vm,
1081 struct amdgpu_bo_va_mapping *mapping,
Christian Königfa3ab3c2016-03-18 21:00:35 +01001082 uint32_t flags, uint64_t addr,
1083 struct fence **fence)
Christian Königa14faa62016-01-25 14:27:31 +01001084{
1085 const uint64_t max_size = 64ULL * 1024ULL * 1024ULL / AMDGPU_GPU_PAGE_SIZE;
1086
Christian Königfa3ab3c2016-03-18 21:00:35 +01001087 uint64_t src = 0, start = mapping->it.start;
Christian Königa14faa62016-01-25 14:27:31 +01001088 int r;
1089
1090 /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here
1091 * but in case of something, we filter the flags in first place
1092 */
1093 if (!(mapping->flags & AMDGPU_PTE_READABLE))
1094 flags &= ~AMDGPU_PTE_READABLE;
1095 if (!(mapping->flags & AMDGPU_PTE_WRITEABLE))
1096 flags &= ~AMDGPU_PTE_WRITEABLE;
1097
1098 trace_amdgpu_vm_bo_update(mapping);
1099
Christian König8358dce2016-03-30 10:50:25 +02001100 if (pages_addr) {
Christian Königfa3ab3c2016-03-18 21:00:35 +01001101 if (flags == gtt_flags)
1102 src = adev->gart.table_addr + (addr >> 12) * 8;
Christian Königfa3ab3c2016-03-18 21:00:35 +01001103 addr = 0;
1104 }
Christian Königa14faa62016-01-25 14:27:31 +01001105 addr += mapping->offset;
1106
Christian König8358dce2016-03-30 10:50:25 +02001107 if (!pages_addr || src)
Christian König3cabaa52016-06-06 10:17:58 +02001108 return amdgpu_vm_bo_update_mapping(adev, exclusive,
1109 src, pages_addr, vm,
Christian Königa14faa62016-01-25 14:27:31 +01001110 start, mapping->it.last,
1111 flags, addr, fence);
1112
1113 while (start != mapping->it.last + 1) {
1114 uint64_t last;
1115
Felix Kuehlingfb29b572016-03-03 19:13:20 -05001116 last = min((uint64_t)mapping->it.last, start + max_size - 1);
Christian König3cabaa52016-06-06 10:17:58 +02001117 r = amdgpu_vm_bo_update_mapping(adev, exclusive,
1118 src, pages_addr, vm,
Christian Königa14faa62016-01-25 14:27:31 +01001119 start, last, flags, addr,
1120 fence);
1121 if (r)
1122 return r;
1123
1124 start = last + 1;
Felix Kuehlingfb29b572016-03-03 19:13:20 -05001125 addr += max_size * AMDGPU_GPU_PAGE_SIZE;
Christian Königa14faa62016-01-25 14:27:31 +01001126 }
1127
1128 return 0;
1129}
1130
1131/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001132 * amdgpu_vm_bo_update - update all BO mappings in the vm page table
1133 *
1134 * @adev: amdgpu_device pointer
1135 * @bo_va: requested BO and VM object
Christian König99e124f2016-08-16 14:43:17 +02001136 * @clear: if true clear the entries
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001137 *
1138 * Fill in the page table entries for @bo_va.
1139 * Returns 0 for success, -EINVAL for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001140 */
1141int amdgpu_vm_bo_update(struct amdgpu_device *adev,
1142 struct amdgpu_bo_va *bo_va,
Christian König99e124f2016-08-16 14:43:17 +02001143 bool clear)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001144{
1145 struct amdgpu_vm *vm = bo_va->vm;
1146 struct amdgpu_bo_va_mapping *mapping;
Christian König8358dce2016-03-30 10:50:25 +02001147 dma_addr_t *pages_addr = NULL;
Christian Königfa3ab3c2016-03-18 21:00:35 +01001148 uint32_t gtt_flags, flags;
Christian König99e124f2016-08-16 14:43:17 +02001149 struct ttm_mem_reg *mem;
Christian König3cabaa52016-06-06 10:17:58 +02001150 struct fence *exclusive;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001151 uint64_t addr;
1152 int r;
1153
Christian König99e124f2016-08-16 14:43:17 +02001154 if (clear) {
1155 mem = NULL;
1156 addr = 0;
1157 exclusive = NULL;
1158 } else {
Christian König8358dce2016-03-30 10:50:25 +02001159 struct ttm_dma_tt *ttm;
1160
Christian König99e124f2016-08-16 14:43:17 +02001161 mem = &bo_va->bo->tbo.mem;
Christian Königb7d698d2015-09-07 12:32:09 +02001162 addr = (u64)mem->start << PAGE_SHIFT;
Christian König9ab21462015-11-30 14:19:26 +01001163 switch (mem->mem_type) {
1164 case TTM_PL_TT:
Christian König8358dce2016-03-30 10:50:25 +02001165 ttm = container_of(bo_va->bo->tbo.ttm, struct
1166 ttm_dma_tt, ttm);
1167 pages_addr = ttm->dma_address;
Christian König9ab21462015-11-30 14:19:26 +01001168 break;
1169
1170 case TTM_PL_VRAM:
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001171 addr += adev->vm_manager.vram_base_offset;
Christian König9ab21462015-11-30 14:19:26 +01001172 break;
1173
1174 default:
1175 break;
1176 }
Christian König3cabaa52016-06-06 10:17:58 +02001177
1178 exclusive = reservation_object_get_excl(bo_va->bo->tbo.resv);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001179 }
1180
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001181 flags = amdgpu_ttm_tt_pte_flags(adev, bo_va->bo->tbo.ttm, mem);
Christian Königc855e252016-09-05 17:00:57 +02001182 gtt_flags = (amdgpu_ttm_is_bound(bo_va->bo->tbo.ttm) &&
1183 adev == bo_va->bo->adev) ? flags : 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001184
Christian König7fc11952015-07-30 11:53:42 +02001185 spin_lock(&vm->status_lock);
1186 if (!list_empty(&bo_va->vm_status))
1187 list_splice_init(&bo_va->valids, &bo_va->invalids);
1188 spin_unlock(&vm->status_lock);
1189
1190 list_for_each_entry(mapping, &bo_va->invalids, list) {
Christian König3cabaa52016-06-06 10:17:58 +02001191 r = amdgpu_vm_bo_split_mapping(adev, exclusive,
1192 gtt_flags, pages_addr, vm,
Christian König8358dce2016-03-30 10:50:25 +02001193 mapping, flags, addr,
1194 &bo_va->last_pt_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001195 if (r)
1196 return r;
1197 }
1198
Christian Königd6c10f62015-09-28 12:00:23 +02001199 if (trace_amdgpu_vm_bo_mapping_enabled()) {
1200 list_for_each_entry(mapping, &bo_va->valids, list)
1201 trace_amdgpu_vm_bo_mapping(mapping);
1202
1203 list_for_each_entry(mapping, &bo_va->invalids, list)
1204 trace_amdgpu_vm_bo_mapping(mapping);
1205 }
1206
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001207 spin_lock(&vm->status_lock);
monk.liu6d1d0ef2015-08-14 13:36:41 +08001208 list_splice_init(&bo_va->invalids, &bo_va->valids);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001209 list_del_init(&bo_va->vm_status);
Christian König99e124f2016-08-16 14:43:17 +02001210 if (clear)
Christian König7fc11952015-07-30 11:53:42 +02001211 list_add(&bo_va->vm_status, &vm->cleared);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001212 spin_unlock(&vm->status_lock);
1213
1214 return 0;
1215}
1216
1217/**
1218 * amdgpu_vm_clear_freed - clear freed BOs in the PT
1219 *
1220 * @adev: amdgpu_device pointer
1221 * @vm: requested vm
1222 *
1223 * Make sure all freed BOs are cleared in the PT.
1224 * Returns 0 for success.
1225 *
1226 * PTs have to be reserved and mutex must be locked!
1227 */
1228int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
1229 struct amdgpu_vm *vm)
1230{
1231 struct amdgpu_bo_va_mapping *mapping;
1232 int r;
1233
1234 while (!list_empty(&vm->freed)) {
1235 mapping = list_first_entry(&vm->freed,
1236 struct amdgpu_bo_va_mapping, list);
1237 list_del(&mapping->list);
Christian Könige17841b2016-03-08 17:52:01 +01001238
Christian König3cabaa52016-06-06 10:17:58 +02001239 r = amdgpu_vm_bo_split_mapping(adev, NULL, 0, NULL, vm, mapping,
Christian Königfa3ab3c2016-03-18 21:00:35 +01001240 0, 0, NULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001241 kfree(mapping);
1242 if (r)
1243 return r;
1244
1245 }
1246 return 0;
1247
1248}
1249
1250/**
1251 * amdgpu_vm_clear_invalids - clear invalidated BOs in the PT
1252 *
1253 * @adev: amdgpu_device pointer
1254 * @vm: requested vm
1255 *
1256 * Make sure all invalidated BOs are cleared in the PT.
1257 * Returns 0 for success.
1258 *
1259 * PTs have to be reserved and mutex must be locked!
1260 */
1261int amdgpu_vm_clear_invalids(struct amdgpu_device *adev,
monk.liucfe2c972015-05-26 15:01:54 +08001262 struct amdgpu_vm *vm, struct amdgpu_sync *sync)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001263{
monk.liucfe2c972015-05-26 15:01:54 +08001264 struct amdgpu_bo_va *bo_va = NULL;
Christian König91e1a522015-07-06 22:06:40 +02001265 int r = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001266
1267 spin_lock(&vm->status_lock);
1268 while (!list_empty(&vm->invalidated)) {
1269 bo_va = list_first_entry(&vm->invalidated,
1270 struct amdgpu_bo_va, vm_status);
1271 spin_unlock(&vm->status_lock);
Christian König32b41ac2016-03-08 18:03:27 +01001272
Christian König99e124f2016-08-16 14:43:17 +02001273 r = amdgpu_vm_bo_update(adev, bo_va, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001274 if (r)
1275 return r;
1276
1277 spin_lock(&vm->status_lock);
1278 }
1279 spin_unlock(&vm->status_lock);
1280
monk.liucfe2c972015-05-26 15:01:54 +08001281 if (bo_va)
Chunming Zhoubb1e38a42015-08-03 18:19:38 +08001282 r = amdgpu_sync_fence(adev, sync, bo_va->last_pt_update);
Christian König91e1a522015-07-06 22:06:40 +02001283
1284 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001285}
1286
1287/**
1288 * amdgpu_vm_bo_add - add a bo to a specific vm
1289 *
1290 * @adev: amdgpu_device pointer
1291 * @vm: requested vm
1292 * @bo: amdgpu buffer object
1293 *
Christian König8843dbb2016-01-26 12:17:11 +01001294 * Add @bo into the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001295 * Add @bo to the list of bos associated with the vm
1296 * Returns newly added bo_va or NULL for failure
1297 *
1298 * Object has to be reserved!
1299 */
1300struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
1301 struct amdgpu_vm *vm,
1302 struct amdgpu_bo *bo)
1303{
1304 struct amdgpu_bo_va *bo_va;
1305
1306 bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL);
1307 if (bo_va == NULL) {
1308 return NULL;
1309 }
1310 bo_va->vm = vm;
1311 bo_va->bo = bo;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001312 bo_va->ref_count = 1;
1313 INIT_LIST_HEAD(&bo_va->bo_list);
Christian König7fc11952015-07-30 11:53:42 +02001314 INIT_LIST_HEAD(&bo_va->valids);
1315 INIT_LIST_HEAD(&bo_va->invalids);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001316 INIT_LIST_HEAD(&bo_va->vm_status);
Christian König32b41ac2016-03-08 18:03:27 +01001317
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001318 list_add_tail(&bo_va->bo_list, &bo->va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001319
1320 return bo_va;
1321}
1322
1323/**
1324 * amdgpu_vm_bo_map - map bo inside a vm
1325 *
1326 * @adev: amdgpu_device pointer
1327 * @bo_va: bo_va to store the address
1328 * @saddr: where to map the BO
1329 * @offset: requested offset in the BO
1330 * @flags: attributes of pages (read/write/valid/etc.)
1331 *
1332 * Add a mapping of the BO at the specefied addr into the VM.
1333 * Returns 0 for success, error for failure.
1334 *
Chunming Zhou49b02b12015-11-13 14:18:38 +08001335 * Object has to be reserved and unreserved outside!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001336 */
1337int amdgpu_vm_bo_map(struct amdgpu_device *adev,
1338 struct amdgpu_bo_va *bo_va,
1339 uint64_t saddr, uint64_t offset,
1340 uint64_t size, uint32_t flags)
1341{
1342 struct amdgpu_bo_va_mapping *mapping;
1343 struct amdgpu_vm *vm = bo_va->vm;
1344 struct interval_tree_node *it;
1345 unsigned last_pfn, pt_idx;
1346 uint64_t eaddr;
1347 int r;
1348
Christian König0be52de2015-05-18 14:37:27 +02001349 /* validate the parameters */
1350 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
Chunming Zhou49b02b12015-11-13 14:18:38 +08001351 size == 0 || size & AMDGPU_GPU_PAGE_MASK)
Christian König0be52de2015-05-18 14:37:27 +02001352 return -EINVAL;
Christian König0be52de2015-05-18 14:37:27 +02001353
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001354 /* make sure object fit at this offset */
Felix Kuehling005ae952015-11-23 17:43:48 -05001355 eaddr = saddr + size - 1;
Chunming Zhou49b02b12015-11-13 14:18:38 +08001356 if ((saddr >= eaddr) || (offset + size > amdgpu_bo_size(bo_va->bo)))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001357 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001358
1359 last_pfn = eaddr / AMDGPU_GPU_PAGE_SIZE;
Felix Kuehling005ae952015-11-23 17:43:48 -05001360 if (last_pfn >= adev->vm_manager.max_pfn) {
1361 dev_err(adev->dev, "va above limit (0x%08X >= 0x%08X)\n",
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001362 last_pfn, adev->vm_manager.max_pfn);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001363 return -EINVAL;
1364 }
1365
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001366 saddr /= AMDGPU_GPU_PAGE_SIZE;
1367 eaddr /= AMDGPU_GPU_PAGE_SIZE;
1368
Felix Kuehling005ae952015-11-23 17:43:48 -05001369 it = interval_tree_iter_first(&vm->va, saddr, eaddr);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001370 if (it) {
1371 struct amdgpu_bo_va_mapping *tmp;
1372 tmp = container_of(it, struct amdgpu_bo_va_mapping, it);
1373 /* bo and tmp overlap, invalid addr */
1374 dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with "
1375 "0x%010lx-0x%010lx\n", bo_va->bo, saddr, eaddr,
1376 tmp->it.start, tmp->it.last + 1);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001377 r = -EINVAL;
Chunming Zhouf48b2652015-10-16 14:06:19 +08001378 goto error;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001379 }
1380
1381 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
1382 if (!mapping) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001383 r = -ENOMEM;
Chunming Zhouf48b2652015-10-16 14:06:19 +08001384 goto error;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001385 }
1386
1387 INIT_LIST_HEAD(&mapping->list);
1388 mapping->it.start = saddr;
Felix Kuehling005ae952015-11-23 17:43:48 -05001389 mapping->it.last = eaddr;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001390 mapping->offset = offset;
1391 mapping->flags = flags;
1392
Christian König7fc11952015-07-30 11:53:42 +02001393 list_add(&mapping->list, &bo_va->invalids);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001394 interval_tree_insert(&mapping->it, &vm->va);
1395
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001396 /* Make sure the page tables are allocated */
1397 saddr >>= amdgpu_vm_block_size;
1398 eaddr >>= amdgpu_vm_block_size;
1399
1400 BUG_ON(eaddr >= amdgpu_vm_num_pdes(adev));
1401
1402 if (eaddr > vm->max_pde_used)
1403 vm->max_pde_used = eaddr;
1404
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001405 /* walk over the address space and allocate the page tables */
1406 for (pt_idx = saddr; pt_idx <= eaddr; ++pt_idx) {
Christian Königbf60efd2015-09-04 10:47:56 +02001407 struct reservation_object *resv = vm->page_directory->tbo.resv;
Christian Königee1782c2015-12-11 21:01:23 +01001408 struct amdgpu_bo_list_entry *entry;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001409 struct amdgpu_bo *pt;
1410
Christian Königee1782c2015-12-11 21:01:23 +01001411 entry = &vm->page_tables[pt_idx].entry;
1412 if (entry->robj)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001413 continue;
1414
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001415 r = amdgpu_bo_create(adev, AMDGPU_VM_PTE_COUNT * 8,
1416 AMDGPU_GPU_PAGE_SIZE, true,
Alex Deucher857d9132015-08-27 00:14:16 -04001417 AMDGPU_GEM_DOMAIN_VRAM,
Chunming Zhou1baa4392016-08-04 13:59:32 +08001418 AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
1419 AMDGPU_GEM_CREATE_SHADOW,
Christian Königbf60efd2015-09-04 10:47:56 +02001420 NULL, resv, &pt);
Chunming Zhou49b02b12015-11-13 14:18:38 +08001421 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001422 goto error_free;
Chunming Zhou49b02b12015-11-13 14:18:38 +08001423
Christian König82b9c552015-11-27 16:49:00 +01001424 /* Keep a reference to the page table to avoid freeing
1425 * them up in the wrong order.
1426 */
1427 pt->parent = amdgpu_bo_ref(vm->page_directory);
1428
Christian König2bd9ccf2016-02-01 12:53:58 +01001429 r = amdgpu_vm_clear_bo(adev, vm, pt);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001430 if (r) {
Christian König2698f622016-09-16 13:06:09 +02001431 amdgpu_bo_unref(&pt->shadow);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001432 amdgpu_bo_unref(&pt);
1433 goto error_free;
1434 }
1435
Christian König2befa602016-09-16 14:07:46 +02001436 if (pt->shadow) {
1437 r = amdgpu_vm_clear_bo(adev, vm, pt->shadow);
1438 if (r) {
1439 amdgpu_bo_unref(&pt->shadow);
1440 amdgpu_bo_unref(&pt);
1441 goto error_free;
1442 }
1443 }
1444
Christian Königee1782c2015-12-11 21:01:23 +01001445 entry->robj = pt;
Christian Königee1782c2015-12-11 21:01:23 +01001446 entry->priority = 0;
1447 entry->tv.bo = &entry->robj->tbo;
1448 entry->tv.shared = true;
Christian König2f568db2016-02-23 12:36:59 +01001449 entry->user_pages = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001450 vm->page_tables[pt_idx].addr = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001451 }
1452
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001453 return 0;
1454
1455error_free:
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001456 list_del(&mapping->list);
1457 interval_tree_remove(&mapping->it, &vm->va);
Christian König93e3e432015-06-09 16:58:33 +02001458 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001459 kfree(mapping);
1460
Chunming Zhouf48b2652015-10-16 14:06:19 +08001461error:
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001462 return r;
1463}
1464
1465/**
1466 * amdgpu_vm_bo_unmap - remove bo mapping from vm
1467 *
1468 * @adev: amdgpu_device pointer
1469 * @bo_va: bo_va to remove the address from
1470 * @saddr: where to the BO is mapped
1471 *
1472 * Remove a mapping of the BO at the specefied addr from the VM.
1473 * Returns 0 for success, error for failure.
1474 *
Chunming Zhou49b02b12015-11-13 14:18:38 +08001475 * Object has to be reserved and unreserved outside!
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001476 */
1477int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
1478 struct amdgpu_bo_va *bo_va,
1479 uint64_t saddr)
1480{
1481 struct amdgpu_bo_va_mapping *mapping;
1482 struct amdgpu_vm *vm = bo_va->vm;
Christian König7fc11952015-07-30 11:53:42 +02001483 bool valid = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001484
Christian König6c7fc502015-06-05 20:56:17 +02001485 saddr /= AMDGPU_GPU_PAGE_SIZE;
Christian König32b41ac2016-03-08 18:03:27 +01001486
Christian König7fc11952015-07-30 11:53:42 +02001487 list_for_each_entry(mapping, &bo_va->valids, list) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001488 if (mapping->it.start == saddr)
1489 break;
1490 }
1491
Christian König7fc11952015-07-30 11:53:42 +02001492 if (&mapping->list == &bo_va->valids) {
1493 valid = false;
1494
1495 list_for_each_entry(mapping, &bo_va->invalids, list) {
1496 if (mapping->it.start == saddr)
1497 break;
1498 }
1499
Christian König32b41ac2016-03-08 18:03:27 +01001500 if (&mapping->list == &bo_va->invalids)
Christian König7fc11952015-07-30 11:53:42 +02001501 return -ENOENT;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001502 }
Christian König32b41ac2016-03-08 18:03:27 +01001503
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001504 list_del(&mapping->list);
1505 interval_tree_remove(&mapping->it, &vm->va);
Christian König93e3e432015-06-09 16:58:33 +02001506 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001507
Christian Könige17841b2016-03-08 17:52:01 +01001508 if (valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001509 list_add(&mapping->list, &vm->freed);
Christian Könige17841b2016-03-08 17:52:01 +01001510 else
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001511 kfree(mapping);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001512
1513 return 0;
1514}
1515
1516/**
1517 * amdgpu_vm_bo_rmv - remove a bo to a specific vm
1518 *
1519 * @adev: amdgpu_device pointer
1520 * @bo_va: requested bo_va
1521 *
Christian König8843dbb2016-01-26 12:17:11 +01001522 * Remove @bo_va->bo from the requested vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001523 *
1524 * Object have to be reserved!
1525 */
1526void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
1527 struct amdgpu_bo_va *bo_va)
1528{
1529 struct amdgpu_bo_va_mapping *mapping, *next;
1530 struct amdgpu_vm *vm = bo_va->vm;
1531
1532 list_del(&bo_va->bo_list);
1533
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001534 spin_lock(&vm->status_lock);
1535 list_del(&bo_va->vm_status);
1536 spin_unlock(&vm->status_lock);
1537
Christian König7fc11952015-07-30 11:53:42 +02001538 list_for_each_entry_safe(mapping, next, &bo_va->valids, list) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001539 list_del(&mapping->list);
1540 interval_tree_remove(&mapping->it, &vm->va);
Christian König93e3e432015-06-09 16:58:33 +02001541 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
Christian König7fc11952015-07-30 11:53:42 +02001542 list_add(&mapping->list, &vm->freed);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001543 }
Christian König7fc11952015-07-30 11:53:42 +02001544 list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) {
1545 list_del(&mapping->list);
1546 interval_tree_remove(&mapping->it, &vm->va);
1547 kfree(mapping);
1548 }
Christian König32b41ac2016-03-08 18:03:27 +01001549
Chunming Zhoubb1e38a42015-08-03 18:19:38 +08001550 fence_put(bo_va->last_pt_update);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001551 kfree(bo_va);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001552}
1553
1554/**
1555 * amdgpu_vm_bo_invalidate - mark the bo as invalid
1556 *
1557 * @adev: amdgpu_device pointer
1558 * @vm: requested vm
1559 * @bo: amdgpu buffer object
1560 *
Christian König8843dbb2016-01-26 12:17:11 +01001561 * Mark @bo as invalid.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001562 */
1563void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
1564 struct amdgpu_bo *bo)
1565{
1566 struct amdgpu_bo_va *bo_va;
1567
1568 list_for_each_entry(bo_va, &bo->va, bo_list) {
Christian König7fc11952015-07-30 11:53:42 +02001569 spin_lock(&bo_va->vm->status_lock);
1570 if (list_empty(&bo_va->vm_status))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001571 list_add(&bo_va->vm_status, &bo_va->vm->invalidated);
Christian König7fc11952015-07-30 11:53:42 +02001572 spin_unlock(&bo_va->vm->status_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001573 }
1574}
1575
1576/**
1577 * amdgpu_vm_init - initialize a vm instance
1578 *
1579 * @adev: amdgpu_device pointer
1580 * @vm: requested vm
1581 *
Christian König8843dbb2016-01-26 12:17:11 +01001582 * Init @vm fields.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001583 */
1584int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1585{
1586 const unsigned align = min(AMDGPU_VM_PTB_ALIGN_SIZE,
1587 AMDGPU_VM_PTE_COUNT * 8);
Michel Dänzer9571e1d2016-01-19 17:59:46 +09001588 unsigned pd_size, pd_entries;
Christian König2d55e452016-02-08 17:37:38 +01001589 unsigned ring_instance;
1590 struct amdgpu_ring *ring;
Christian König2bd9ccf2016-02-01 12:53:58 +01001591 struct amd_sched_rq *rq;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001592 int i, r;
1593
Christian Königbcb1ba32016-03-08 15:40:11 +01001594 for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
1595 vm->ids[i] = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001596 vm->va = RB_ROOT;
Chunming Zhou031e2982016-04-25 10:19:13 +08001597 vm->client_id = atomic64_inc_return(&adev->vm_manager.client_counter);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001598 spin_lock_init(&vm->status_lock);
1599 INIT_LIST_HEAD(&vm->invalidated);
Christian König7fc11952015-07-30 11:53:42 +02001600 INIT_LIST_HEAD(&vm->cleared);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001601 INIT_LIST_HEAD(&vm->freed);
Christian König20250212016-03-08 17:58:35 +01001602
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001603 pd_size = amdgpu_vm_directory_size(adev);
1604 pd_entries = amdgpu_vm_num_pdes(adev);
1605
1606 /* allocate page table array */
Michel Dänzer9571e1d2016-01-19 17:59:46 +09001607 vm->page_tables = drm_calloc_large(pd_entries, sizeof(struct amdgpu_vm_pt));
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001608 if (vm->page_tables == NULL) {
1609 DRM_ERROR("Cannot allocate memory for page table array\n");
1610 return -ENOMEM;
1611 }
1612
Christian König2bd9ccf2016-02-01 12:53:58 +01001613 /* create scheduler entity for page table updates */
Christian König2d55e452016-02-08 17:37:38 +01001614
1615 ring_instance = atomic_inc_return(&adev->vm_manager.vm_pte_next_ring);
1616 ring_instance %= adev->vm_manager.vm_pte_num_rings;
1617 ring = adev->vm_manager.vm_pte_rings[ring_instance];
Christian König2bd9ccf2016-02-01 12:53:58 +01001618 rq = &ring->sched.sched_rq[AMD_SCHED_PRIORITY_KERNEL];
1619 r = amd_sched_entity_init(&ring->sched, &vm->entity,
1620 rq, amdgpu_sched_jobs);
1621 if (r)
Chunming Zhou64827ad2016-07-28 17:20:32 +08001622 goto err;
Christian König2bd9ccf2016-02-01 12:53:58 +01001623
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +02001624 vm->page_directory_fence = NULL;
1625
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001626 r = amdgpu_bo_create(adev, pd_size, align, true,
Alex Deucher857d9132015-08-27 00:14:16 -04001627 AMDGPU_GEM_DOMAIN_VRAM,
Chunming Zhou1baa4392016-08-04 13:59:32 +08001628 AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
1629 AMDGPU_GEM_CREATE_SHADOW,
Christian König72d76682015-09-03 17:34:59 +02001630 NULL, NULL, &vm->page_directory);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001631 if (r)
Christian König2bd9ccf2016-02-01 12:53:58 +01001632 goto error_free_sched_entity;
1633
Chunming Zhouef9f0a82015-11-13 13:43:22 +08001634 r = amdgpu_bo_reserve(vm->page_directory, false);
Christian König2bd9ccf2016-02-01 12:53:58 +01001635 if (r)
1636 goto error_free_page_directory;
1637
1638 r = amdgpu_vm_clear_bo(adev, vm, vm->page_directory);
Christian König2bd9ccf2016-02-01 12:53:58 +01001639 if (r)
Christian König2a82ec212016-09-16 13:11:45 +02001640 goto error_unreserve;
1641
Christian König2befa602016-09-16 14:07:46 +02001642 if (vm->page_directory->shadow) {
1643 r = amdgpu_vm_clear_bo(adev, vm, vm->page_directory->shadow);
1644 if (r)
1645 goto error_unreserve;
1646 }
1647
Christian König5a712a82016-06-21 16:28:15 +02001648 vm->last_eviction_counter = atomic64_read(&adev->num_evictions);
Christian König2a82ec212016-09-16 13:11:45 +02001649 amdgpu_bo_unreserve(vm->page_directory);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001650
1651 return 0;
Christian König2bd9ccf2016-02-01 12:53:58 +01001652
Christian König2a82ec212016-09-16 13:11:45 +02001653error_unreserve:
1654 amdgpu_bo_unreserve(vm->page_directory);
1655
Christian König2bd9ccf2016-02-01 12:53:58 +01001656error_free_page_directory:
Christian König2698f622016-09-16 13:06:09 +02001657 amdgpu_bo_unref(&vm->page_directory->shadow);
Christian König2bd9ccf2016-02-01 12:53:58 +01001658 amdgpu_bo_unref(&vm->page_directory);
1659 vm->page_directory = NULL;
1660
1661error_free_sched_entity:
1662 amd_sched_entity_fini(&ring->sched, &vm->entity);
1663
Chunming Zhou64827ad2016-07-28 17:20:32 +08001664err:
1665 drm_free_large(vm->page_tables);
1666
Christian König2bd9ccf2016-02-01 12:53:58 +01001667 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001668}
1669
1670/**
1671 * amdgpu_vm_fini - tear down a vm instance
1672 *
1673 * @adev: amdgpu_device pointer
1674 * @vm: requested vm
1675 *
Christian König8843dbb2016-01-26 12:17:11 +01001676 * Tear down @vm.
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001677 * Unbind the VM and remove all bos from the vm bo list
1678 */
1679void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1680{
1681 struct amdgpu_bo_va_mapping *mapping, *tmp;
1682 int i;
1683
Christian König2d55e452016-02-08 17:37:38 +01001684 amd_sched_entity_fini(vm->entity.sched, &vm->entity);
Christian König2bd9ccf2016-02-01 12:53:58 +01001685
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001686 if (!RB_EMPTY_ROOT(&vm->va)) {
1687 dev_err(adev->dev, "still active bo inside vm\n");
1688 }
1689 rbtree_postorder_for_each_entry_safe(mapping, tmp, &vm->va, it.rb) {
1690 list_del(&mapping->list);
1691 interval_tree_remove(&mapping->it, &vm->va);
1692 kfree(mapping);
1693 }
1694 list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
1695 list_del(&mapping->list);
1696 kfree(mapping);
1697 }
1698
Chunming Zhou1baa4392016-08-04 13:59:32 +08001699 for (i = 0; i < amdgpu_vm_num_pdes(adev); i++) {
Christian König2698f622016-09-16 13:06:09 +02001700 struct amdgpu_bo *pt = vm->page_tables[i].entry.robj;
1701
1702 if (!pt)
1703 continue;
1704
1705 amdgpu_bo_unref(&pt->shadow);
1706 amdgpu_bo_unref(&pt);
Chunming Zhou1baa4392016-08-04 13:59:32 +08001707 }
Michel Dänzer9571e1d2016-01-19 17:59:46 +09001708 drm_free_large(vm->page_tables);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001709
Christian König2698f622016-09-16 13:06:09 +02001710 amdgpu_bo_unref(&vm->page_directory->shadow);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001711 amdgpu_bo_unref(&vm->page_directory);
Bas Nieuwenhuizen05906de2015-08-14 20:08:40 +02001712 fence_put(vm->page_directory_fence);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001713}
Christian Königea89f8c2015-11-15 20:52:06 +01001714
1715/**
Christian Königa9a78b32016-01-21 10:19:11 +01001716 * amdgpu_vm_manager_init - init the VM manager
1717 *
1718 * @adev: amdgpu_device pointer
1719 *
1720 * Initialize the VM manager structures
1721 */
1722void amdgpu_vm_manager_init(struct amdgpu_device *adev)
1723{
1724 unsigned i;
1725
1726 INIT_LIST_HEAD(&adev->vm_manager.ids_lru);
1727
1728 /* skip over VMID 0, since it is the system VM */
Christian König971fe9a92016-03-01 15:09:25 +01001729 for (i = 1; i < adev->vm_manager.num_ids; ++i) {
1730 amdgpu_vm_reset_id(adev, i);
Christian König832a9022016-02-15 12:33:02 +01001731 amdgpu_sync_create(&adev->vm_manager.ids[i].active);
Christian Königa9a78b32016-01-21 10:19:11 +01001732 list_add_tail(&adev->vm_manager.ids[i].list,
1733 &adev->vm_manager.ids_lru);
Christian König971fe9a92016-03-01 15:09:25 +01001734 }
Christian König2d55e452016-02-08 17:37:38 +01001735
Christian König1fbb2e92016-06-01 10:47:36 +02001736 adev->vm_manager.fence_context = fence_context_alloc(AMDGPU_MAX_RINGS);
1737 for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
1738 adev->vm_manager.seqno[i] = 0;
1739
Christian König2d55e452016-02-08 17:37:38 +01001740 atomic_set(&adev->vm_manager.vm_pte_next_ring, 0);
Christian Königb1c8a812016-05-04 10:34:03 +02001741 atomic64_set(&adev->vm_manager.client_counter, 0);
Christian Königa9a78b32016-01-21 10:19:11 +01001742}
1743
1744/**
Christian Königea89f8c2015-11-15 20:52:06 +01001745 * amdgpu_vm_manager_fini - cleanup VM manager
1746 *
1747 * @adev: amdgpu_device pointer
1748 *
1749 * Cleanup the VM manager and free resources.
1750 */
1751void amdgpu_vm_manager_fini(struct amdgpu_device *adev)
1752{
1753 unsigned i;
1754
Christian Königbcb1ba32016-03-08 15:40:11 +01001755 for (i = 0; i < AMDGPU_NUM_VM; ++i) {
1756 struct amdgpu_vm_id *id = &adev->vm_manager.ids[i];
1757
Christian König832a9022016-02-15 12:33:02 +01001758 fence_put(adev->vm_manager.ids[i].first);
1759 amdgpu_sync_free(&adev->vm_manager.ids[i].active);
Christian Königbcb1ba32016-03-08 15:40:11 +01001760 fence_put(id->flushed_updates);
1761 }
Christian Königea89f8c2015-11-15 20:52:06 +01001762}