blob: 5a6216c9c0073972ea99546e96177e479d5dc1bc [file] [log] [blame]
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001/*
2 * Copyright 2009 Jerome Glisse.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sub license, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
16 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
17 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
18 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
19 * USE OR OTHER DEALINGS IN THE SOFTWARE.
20 *
21 * The above copyright notice and this permission notice (including the
22 * next paragraph) shall be included in all copies or substantial portions
23 * of the Software.
24 *
25 */
26/*
27 * Authors:
28 * Jerome Glisse <glisse@freedesktop.org>
29 * Thomas Hellstrom <thomas-at-tungstengraphics-dot-com>
30 * Dave Airlie
31 */
32#include <linux/list.h>
33#include <linux/slab.h>
34#include <drm/drmP.h>
35#include <drm/amdgpu_drm.h>
Oded Gabbaya187f172016-01-30 07:59:34 +020036#include <drm/drm_cache.h>
Alex Deucherd38ceaf2015-04-20 16:55:21 -040037#include "amdgpu.h"
38#include "amdgpu_trace.h"
39
40
41int amdgpu_ttm_init(struct amdgpu_device *adev);
42void amdgpu_ttm_fini(struct amdgpu_device *adev);
43
44static u64 amdgpu_get_vis_part_size(struct amdgpu_device *adev,
Chunming Zhou7e5a5472015-04-24 17:37:30 +080045 struct ttm_mem_reg *mem)
Alex Deucherd38ceaf2015-04-20 16:55:21 -040046{
Christian König6681c5e2016-08-12 16:50:12 +020047 if (mem->start << PAGE_SHIFT >= adev->mc.visible_vram_size)
48 return 0;
49
50 return ((mem->start << PAGE_SHIFT) + mem->size) >
51 adev->mc.visible_vram_size ?
52 adev->mc.visible_vram_size - (mem->start << PAGE_SHIFT) :
53 mem->size;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040054}
55
56static void amdgpu_update_memory_usage(struct amdgpu_device *adev,
57 struct ttm_mem_reg *old_mem,
58 struct ttm_mem_reg *new_mem)
59{
60 u64 vis_size;
61 if (!adev)
62 return;
63
64 if (new_mem) {
65 switch (new_mem->mem_type) {
66 case TTM_PL_TT:
67 atomic64_add(new_mem->size, &adev->gtt_usage);
68 break;
69 case TTM_PL_VRAM:
70 atomic64_add(new_mem->size, &adev->vram_usage);
71 vis_size = amdgpu_get_vis_part_size(adev, new_mem);
72 atomic64_add(vis_size, &adev->vram_vis_usage);
73 break;
74 }
75 }
76
77 if (old_mem) {
78 switch (old_mem->mem_type) {
79 case TTM_PL_TT:
80 atomic64_sub(old_mem->size, &adev->gtt_usage);
81 break;
82 case TTM_PL_VRAM:
83 atomic64_sub(old_mem->size, &adev->vram_usage);
84 vis_size = amdgpu_get_vis_part_size(adev, old_mem);
85 atomic64_sub(vis_size, &adev->vram_vis_usage);
86 break;
87 }
88 }
89}
90
91static void amdgpu_ttm_bo_destroy(struct ttm_buffer_object *tbo)
92{
93 struct amdgpu_bo *bo;
94
95 bo = container_of(tbo, struct amdgpu_bo, tbo);
96
97 amdgpu_update_memory_usage(bo->adev, &bo->tbo.mem, NULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -040098
Alex Deucherd38ceaf2015-04-20 16:55:21 -040099 drm_gem_object_release(&bo->gem_base);
Christian König82b9c552015-11-27 16:49:00 +0100100 amdgpu_bo_unref(&bo->parent);
Chunming Zhou0c4e7fa2016-08-17 11:41:30 +0800101 if (!list_empty(&bo->shadow_list)) {
102 mutex_lock(&bo->adev->shadow_list_lock);
103 list_del_init(&bo->shadow_list);
104 mutex_unlock(&bo->adev->shadow_list_lock);
105 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400106 kfree(bo->metadata);
107 kfree(bo);
108}
109
110bool amdgpu_ttm_bo_is_amdgpu_bo(struct ttm_buffer_object *bo)
111{
112 if (bo->destroy == &amdgpu_ttm_bo_destroy)
113 return true;
114 return false;
115}
116
Chunming Zhou7e5a5472015-04-24 17:37:30 +0800117static void amdgpu_ttm_placement_init(struct amdgpu_device *adev,
118 struct ttm_placement *placement,
Christian Königfaceaf62016-08-15 14:06:50 +0200119 struct ttm_place *places,
Chunming Zhou7e5a5472015-04-24 17:37:30 +0800120 u32 domain, u64 flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400121{
Christian König6369f6f2016-08-15 14:08:54 +0200122 u32 c = 0;
Chunming Zhou7e5a5472015-04-24 17:37:30 +0800123
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400124 if (domain & AMDGPU_GEM_DOMAIN_VRAM) {
Christian Königfaceaf62016-08-15 14:06:50 +0200125 unsigned visible_pfn = adev->mc.visible_vram_size >> PAGE_SHIFT;
126
Chunming Zhou7e5a5472015-04-24 17:37:30 +0800127 if (flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS &&
Christian König6369f6f2016-08-15 14:08:54 +0200128 !(flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED) &&
Christian Königfaceaf62016-08-15 14:06:50 +0200129 adev->mc.visible_vram_size < adev->mc.real_vram_size) {
130 places[c].fpfn = visible_pfn;
Christian König6369f6f2016-08-15 14:08:54 +0200131 places[c].lpfn = 0;
Christian Königfaceaf62016-08-15 14:06:50 +0200132 places[c].flags = TTM_PL_FLAG_WC |
Christian König6681c5e2016-08-12 16:50:12 +0200133 TTM_PL_FLAG_UNCACHED | TTM_PL_FLAG_VRAM |
134 TTM_PL_FLAG_TOPDOWN;
Christian Königfaceaf62016-08-15 14:06:50 +0200135 c++;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400136 }
Christian Königfaceaf62016-08-15 14:06:50 +0200137
138 places[c].fpfn = 0;
139 places[c].lpfn = 0;
140 places[c].flags = TTM_PL_FLAG_WC | TTM_PL_FLAG_UNCACHED |
Chunming Zhou7e5a5472015-04-24 17:37:30 +0800141 TTM_PL_FLAG_VRAM;
Christian Königfaceaf62016-08-15 14:06:50 +0200142 if (flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED)
143 places[c].lpfn = visible_pfn;
144 else
145 places[c].flags |= TTM_PL_FLAG_TOPDOWN;
146 c++;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400147 }
148
149 if (domain & AMDGPU_GEM_DOMAIN_GTT) {
Christian Königfaceaf62016-08-15 14:06:50 +0200150 places[c].fpfn = 0;
151 places[c].lpfn = 0;
152 places[c].flags = TTM_PL_FLAG_TT;
153 if (flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC)
154 places[c].flags |= TTM_PL_FLAG_WC |
155 TTM_PL_FLAG_UNCACHED;
156 else
157 places[c].flags |= TTM_PL_FLAG_CACHED;
158 c++;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400159 }
160
161 if (domain & AMDGPU_GEM_DOMAIN_CPU) {
Christian Königfaceaf62016-08-15 14:06:50 +0200162 places[c].fpfn = 0;
163 places[c].lpfn = 0;
164 places[c].flags = TTM_PL_FLAG_SYSTEM;
165 if (flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC)
166 places[c].flags |= TTM_PL_FLAG_WC |
167 TTM_PL_FLAG_UNCACHED;
168 else
169 places[c].flags |= TTM_PL_FLAG_CACHED;
170 c++;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400171 }
172
173 if (domain & AMDGPU_GEM_DOMAIN_GDS) {
Christian Königfaceaf62016-08-15 14:06:50 +0200174 places[c].fpfn = 0;
175 places[c].lpfn = 0;
176 places[c].flags = TTM_PL_FLAG_UNCACHED | AMDGPU_PL_FLAG_GDS;
177 c++;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400178 }
Christian Königfaceaf62016-08-15 14:06:50 +0200179
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400180 if (domain & AMDGPU_GEM_DOMAIN_GWS) {
Christian Königfaceaf62016-08-15 14:06:50 +0200181 places[c].fpfn = 0;
182 places[c].lpfn = 0;
183 places[c].flags = TTM_PL_FLAG_UNCACHED | AMDGPU_PL_FLAG_GWS;
184 c++;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400185 }
Christian Königfaceaf62016-08-15 14:06:50 +0200186
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400187 if (domain & AMDGPU_GEM_DOMAIN_OA) {
Christian Königfaceaf62016-08-15 14:06:50 +0200188 places[c].fpfn = 0;
189 places[c].lpfn = 0;
190 places[c].flags = TTM_PL_FLAG_UNCACHED | AMDGPU_PL_FLAG_OA;
191 c++;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400192 }
193
194 if (!c) {
Christian Königfaceaf62016-08-15 14:06:50 +0200195 places[c].fpfn = 0;
196 places[c].lpfn = 0;
197 places[c].flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM;
198 c++;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400199 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400200
Christian Königfaceaf62016-08-15 14:06:50 +0200201 placement->num_placement = c;
202 placement->placement = places;
203
204 placement->num_busy_placement = c;
205 placement->busy_placement = places;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400206}
207
Chunming Zhou7e5a5472015-04-24 17:37:30 +0800208void amdgpu_ttm_placement_from_domain(struct amdgpu_bo *rbo, u32 domain)
209{
210 amdgpu_ttm_placement_init(rbo->adev, &rbo->placement,
211 rbo->placements, domain, rbo->flags);
212}
213
214static void amdgpu_fill_placement_to_bo(struct amdgpu_bo *bo,
215 struct ttm_placement *placement)
216{
217 BUG_ON(placement->num_placement > (AMDGPU_GEM_DOMAIN_MAX + 1));
218
219 memcpy(bo->placements, placement->placement,
220 placement->num_placement * sizeof(struct ttm_place));
221 bo->placement.num_placement = placement->num_placement;
222 bo->placement.num_busy_placement = placement->num_busy_placement;
223 bo->placement.placement = bo->placements;
224 bo->placement.busy_placement = bo->placements;
225}
226
Christian König7c204882015-12-14 13:18:01 +0100227/**
228 * amdgpu_bo_create_kernel - create BO for kernel use
229 *
230 * @adev: amdgpu device object
231 * @size: size for the new BO
232 * @align: alignment for the new BO
233 * @domain: where to place it
234 * @bo_ptr: resulting BO
235 * @gpu_addr: GPU addr of the pinned BO
236 * @cpu_addr: optional CPU address mapping
237 *
238 * Allocates and pins a BO for kernel internal use.
239 *
240 * Returns 0 on success, negative error code otherwise.
241 */
242int amdgpu_bo_create_kernel(struct amdgpu_device *adev,
243 unsigned long size, int align,
244 u32 domain, struct amdgpu_bo **bo_ptr,
245 u64 *gpu_addr, void **cpu_addr)
246{
247 int r;
248
249 r = amdgpu_bo_create(adev, size, align, true, domain,
250 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED,
251 NULL, NULL, bo_ptr);
252 if (r) {
253 dev_err(adev->dev, "(%d) failed to allocate kernel bo\n", r);
254 return r;
255 }
256
257 r = amdgpu_bo_reserve(*bo_ptr, false);
258 if (r) {
259 dev_err(adev->dev, "(%d) failed to reserve kernel bo\n", r);
260 goto error_free;
261 }
262
263 r = amdgpu_bo_pin(*bo_ptr, domain, gpu_addr);
264 if (r) {
265 dev_err(adev->dev, "(%d) kernel bo pin failed\n", r);
266 goto error_unreserve;
267 }
268
269 if (cpu_addr) {
270 r = amdgpu_bo_kmap(*bo_ptr, cpu_addr);
271 if (r) {
272 dev_err(adev->dev, "(%d) kernel bo map failed\n", r);
273 goto error_unreserve;
274 }
275 }
276
277 amdgpu_bo_unreserve(*bo_ptr);
278
279 return 0;
280
281error_unreserve:
282 amdgpu_bo_unreserve(*bo_ptr);
283
284error_free:
285 amdgpu_bo_unref(bo_ptr);
286
287 return r;
288}
289
Junwei Zhangaa1d5622016-09-08 10:13:32 +0800290/**
291 * amdgpu_bo_free_kernel - free BO for kernel use
292 *
293 * @bo: amdgpu BO to free
294 *
295 * unmaps and unpin a BO for kernel internal use.
296 */
297void amdgpu_bo_free_kernel(struct amdgpu_bo **bo, u64 *gpu_addr,
298 void **cpu_addr)
299{
300 if (*bo == NULL)
301 return;
302
303 if (likely(amdgpu_bo_reserve(*bo, false) == 0)) {
304 if (cpu_addr)
305 amdgpu_bo_kunmap(*bo);
306
307 amdgpu_bo_unpin(*bo);
308 amdgpu_bo_unreserve(*bo);
309 }
310 amdgpu_bo_unref(bo);
311
312 if (gpu_addr)
313 *gpu_addr = 0;
314
315 if (cpu_addr)
316 *cpu_addr = NULL;
317}
318
Chunming Zhou7e5a5472015-04-24 17:37:30 +0800319int amdgpu_bo_create_restricted(struct amdgpu_device *adev,
320 unsigned long size, int byte_align,
321 bool kernel, u32 domain, u64 flags,
322 struct sg_table *sg,
323 struct ttm_placement *placement,
Christian König72d76682015-09-03 17:34:59 +0200324 struct reservation_object *resv,
Chunming Zhou7e5a5472015-04-24 17:37:30 +0800325 struct amdgpu_bo **bo_ptr)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400326{
327 struct amdgpu_bo *bo;
328 enum ttm_bo_type type;
329 unsigned long page_align;
330 size_t acc_size;
331 int r;
332
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400333 page_align = roundup(byte_align, PAGE_SIZE) >> PAGE_SHIFT;
334 size = ALIGN(size, PAGE_SIZE);
335
336 if (kernel) {
337 type = ttm_bo_type_kernel;
338 } else if (sg) {
339 type = ttm_bo_type_sg;
340 } else {
341 type = ttm_bo_type_device;
342 }
343 *bo_ptr = NULL;
344
345 acc_size = ttm_bo_dma_acc_size(&adev->mman.bdev, size,
346 sizeof(struct amdgpu_bo));
347
348 bo = kzalloc(sizeof(struct amdgpu_bo), GFP_KERNEL);
349 if (bo == NULL)
350 return -ENOMEM;
351 r = drm_gem_object_init(adev->ddev, &bo->gem_base, size);
352 if (unlikely(r)) {
353 kfree(bo);
354 return r;
355 }
356 bo->adev = adev;
357 INIT_LIST_HEAD(&bo->list);
Chunming Zhou0c4e7fa2016-08-17 11:41:30 +0800358 INIT_LIST_HEAD(&bo->shadow_list);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400359 INIT_LIST_HEAD(&bo->va);
Christian König1ea863f2015-12-18 22:13:12 +0100360 bo->prefered_domains = domain & (AMDGPU_GEM_DOMAIN_VRAM |
361 AMDGPU_GEM_DOMAIN_GTT |
362 AMDGPU_GEM_DOMAIN_CPU |
363 AMDGPU_GEM_DOMAIN_GDS |
364 AMDGPU_GEM_DOMAIN_GWS |
365 AMDGPU_GEM_DOMAIN_OA);
366 bo->allowed_domains = bo->prefered_domains;
367 if (!kernel && bo->allowed_domains == AMDGPU_GEM_DOMAIN_VRAM)
368 bo->allowed_domains |= AMDGPU_GEM_DOMAIN_GTT;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400369
370 bo->flags = flags;
Oded Gabbaya187f172016-01-30 07:59:34 +0200371
372 /* For architectures that don't support WC memory,
373 * mask out the WC flag from the BO
374 */
375 if (!drm_arch_can_wc_memory())
376 bo->flags &= ~AMDGPU_GEM_CREATE_CPU_GTT_USWC;
377
Chunming Zhou7e5a5472015-04-24 17:37:30 +0800378 amdgpu_fill_placement_to_bo(bo, placement);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400379 /* Kernel allocation are uninterruptible */
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400380 r = ttm_bo_init(&adev->mman.bdev, &bo->tbo, size, type,
381 &bo->placement, page_align, !kernel, NULL,
Christian König72d76682015-09-03 17:34:59 +0200382 acc_size, sg, resv, &amdgpu_ttm_bo_destroy);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400383 if (unlikely(r != 0)) {
384 return r;
385 }
Flora Cui4fea83f2016-07-20 14:44:38 +0800386
387 if (flags & AMDGPU_GEM_CREATE_VRAM_CLEARED &&
388 bo->tbo.mem.placement & TTM_PL_FLAG_VRAM) {
389 struct fence *fence;
390
391 if (adev->mman.buffer_funcs_ring == NULL ||
392 !adev->mman.buffer_funcs_ring->ready) {
393 r = -EBUSY;
394 goto fail_free;
395 }
396
397 r = amdgpu_bo_reserve(bo, false);
398 if (unlikely(r != 0))
399 goto fail_free;
400
401 amdgpu_ttm_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_VRAM);
402 r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false);
403 if (unlikely(r != 0))
404 goto fail_unreserve;
405
406 amdgpu_fill_buffer(bo, 0, bo->tbo.resv, &fence);
407 amdgpu_bo_fence(bo, fence, false);
408 amdgpu_bo_unreserve(bo);
409 fence_put(bo->tbo.moving);
410 bo->tbo.moving = fence_get(fence);
411 fence_put(fence);
412 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400413 *bo_ptr = bo;
414
415 trace_amdgpu_bo_create(bo);
416
417 return 0;
Flora Cui4fea83f2016-07-20 14:44:38 +0800418
419fail_unreserve:
420 amdgpu_bo_unreserve(bo);
421fail_free:
422 amdgpu_bo_unref(&bo);
423 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400424}
425
Chunming Zhoue7893c42016-07-26 14:13:21 +0800426static int amdgpu_bo_create_shadow(struct amdgpu_device *adev,
427 unsigned long size, int byte_align,
428 struct amdgpu_bo *bo)
429{
430 struct ttm_placement placement = {0};
431 struct ttm_place placements[AMDGPU_GEM_DOMAIN_MAX + 1];
432 int r;
433
434 if (bo->shadow)
435 return 0;
436
437 bo->flags |= AMDGPU_GEM_CREATE_SHADOW;
438 memset(&placements, 0,
439 (AMDGPU_GEM_DOMAIN_MAX + 1) * sizeof(struct ttm_place));
440
441 amdgpu_ttm_placement_init(adev, &placement,
442 placements, AMDGPU_GEM_DOMAIN_GTT,
443 AMDGPU_GEM_CREATE_CPU_GTT_USWC);
444
445 r = amdgpu_bo_create_restricted(adev, size, byte_align, true,
446 AMDGPU_GEM_DOMAIN_GTT,
447 AMDGPU_GEM_CREATE_CPU_GTT_USWC,
448 NULL, &placement,
449 bo->tbo.resv,
450 &bo->shadow);
Chunming Zhou0c4e7fa2016-08-17 11:41:30 +0800451 if (!r) {
Chunming Zhoue7893c42016-07-26 14:13:21 +0800452 bo->shadow->parent = amdgpu_bo_ref(bo);
Chunming Zhou0c4e7fa2016-08-17 11:41:30 +0800453 mutex_lock(&adev->shadow_list_lock);
454 list_add_tail(&bo->shadow_list, &adev->shadow_list);
455 mutex_unlock(&adev->shadow_list_lock);
456 }
Chunming Zhoue7893c42016-07-26 14:13:21 +0800457
458 return r;
459}
460
Chunming Zhou7e5a5472015-04-24 17:37:30 +0800461int amdgpu_bo_create(struct amdgpu_device *adev,
462 unsigned long size, int byte_align,
463 bool kernel, u32 domain, u64 flags,
Christian König72d76682015-09-03 17:34:59 +0200464 struct sg_table *sg,
465 struct reservation_object *resv,
466 struct amdgpu_bo **bo_ptr)
Chunming Zhou7e5a5472015-04-24 17:37:30 +0800467{
468 struct ttm_placement placement = {0};
469 struct ttm_place placements[AMDGPU_GEM_DOMAIN_MAX + 1];
Chunming Zhoue7893c42016-07-26 14:13:21 +0800470 int r;
Chunming Zhou7e5a5472015-04-24 17:37:30 +0800471
472 memset(&placements, 0,
473 (AMDGPU_GEM_DOMAIN_MAX + 1) * sizeof(struct ttm_place));
474
475 amdgpu_ttm_placement_init(adev, &placement,
476 placements, domain, flags);
477
Chunming Zhoue7893c42016-07-26 14:13:21 +0800478 r = amdgpu_bo_create_restricted(adev, size, byte_align, kernel,
479 domain, flags, sg, &placement,
480 resv, bo_ptr);
481 if (r)
482 return r;
483
Chunming Zhou3ad81f12016-08-05 17:30:17 +0800484 if (amdgpu_need_backup(adev) && (flags & AMDGPU_GEM_CREATE_SHADOW)) {
Chunming Zhoue7893c42016-07-26 14:13:21 +0800485 r = amdgpu_bo_create_shadow(adev, size, byte_align, (*bo_ptr));
486 if (r)
487 amdgpu_bo_unref(bo_ptr);
488 }
489
490 return r;
Chunming Zhou7e5a5472015-04-24 17:37:30 +0800491}
492
Chunming Zhou20f4eff2016-08-04 16:51:18 +0800493int amdgpu_bo_backup_to_shadow(struct amdgpu_device *adev,
494 struct amdgpu_ring *ring,
495 struct amdgpu_bo *bo,
496 struct reservation_object *resv,
497 struct fence **fence,
498 bool direct)
499
500{
501 struct amdgpu_bo *shadow = bo->shadow;
502 uint64_t bo_addr, shadow_addr;
503 int r;
504
505 if (!shadow)
506 return -EINVAL;
507
508 bo_addr = amdgpu_bo_gpu_offset(bo);
509 shadow_addr = amdgpu_bo_gpu_offset(bo->shadow);
510
511 r = reservation_object_reserve_shared(bo->tbo.resv);
512 if (r)
513 goto err;
514
515 r = amdgpu_copy_buffer(ring, bo_addr, shadow_addr,
516 amdgpu_bo_size(bo), resv, fence,
517 direct);
518 if (!r)
519 amdgpu_bo_fence(bo, *fence, true);
520
521err:
522 return r;
523}
524
525int amdgpu_bo_restore_from_shadow(struct amdgpu_device *adev,
526 struct amdgpu_ring *ring,
527 struct amdgpu_bo *bo,
528 struct reservation_object *resv,
529 struct fence **fence,
530 bool direct)
531
532{
533 struct amdgpu_bo *shadow = bo->shadow;
534 uint64_t bo_addr, shadow_addr;
535 int r;
536
537 if (!shadow)
538 return -EINVAL;
539
540 bo_addr = amdgpu_bo_gpu_offset(bo);
541 shadow_addr = amdgpu_bo_gpu_offset(bo->shadow);
542
543 r = reservation_object_reserve_shared(bo->tbo.resv);
544 if (r)
545 goto err;
546
547 r = amdgpu_copy_buffer(ring, shadow_addr, bo_addr,
548 amdgpu_bo_size(bo), resv, fence,
549 direct);
550 if (!r)
551 amdgpu_bo_fence(bo, *fence, true);
552
553err:
554 return r;
555}
556
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400557int amdgpu_bo_kmap(struct amdgpu_bo *bo, void **ptr)
558{
559 bool is_iomem;
Christian König587f3c72016-03-10 16:21:04 +0100560 long r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400561
Christian König271c8122015-05-13 14:30:53 +0200562 if (bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)
563 return -EPERM;
564
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400565 if (bo->kptr) {
566 if (ptr) {
567 *ptr = bo->kptr;
568 }
569 return 0;
570 }
Christian König587f3c72016-03-10 16:21:04 +0100571
572 r = reservation_object_wait_timeout_rcu(bo->tbo.resv, false, false,
573 MAX_SCHEDULE_TIMEOUT);
574 if (r < 0)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400575 return r;
Christian König587f3c72016-03-10 16:21:04 +0100576
577 r = ttm_bo_kmap(&bo->tbo, 0, bo->tbo.num_pages, &bo->kmap);
578 if (r)
579 return r;
580
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400581 bo->kptr = ttm_kmap_obj_virtual(&bo->kmap, &is_iomem);
Christian König587f3c72016-03-10 16:21:04 +0100582 if (ptr)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400583 *ptr = bo->kptr;
Christian König587f3c72016-03-10 16:21:04 +0100584
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400585 return 0;
586}
587
588void amdgpu_bo_kunmap(struct amdgpu_bo *bo)
589{
590 if (bo->kptr == NULL)
591 return;
592 bo->kptr = NULL;
593 ttm_bo_kunmap(&bo->kmap);
594}
595
596struct amdgpu_bo *amdgpu_bo_ref(struct amdgpu_bo *bo)
597{
598 if (bo == NULL)
599 return NULL;
600
601 ttm_bo_reference(&bo->tbo);
602 return bo;
603}
604
605void amdgpu_bo_unref(struct amdgpu_bo **bo)
606{
607 struct ttm_buffer_object *tbo;
608
609 if ((*bo) == NULL)
610 return;
611
612 tbo = &((*bo)->tbo);
613 ttm_bo_unref(&tbo);
614 if (tbo == NULL)
615 *bo = NULL;
616}
617
Chunming Zhou7e5a5472015-04-24 17:37:30 +0800618int amdgpu_bo_pin_restricted(struct amdgpu_bo *bo, u32 domain,
619 u64 min_offset, u64 max_offset,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400620 u64 *gpu_addr)
621{
622 int r, i;
Chunming Zhou7e5a5472015-04-24 17:37:30 +0800623 unsigned fpfn, lpfn;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400624
Christian Königcc325d12016-02-08 11:08:35 +0100625 if (amdgpu_ttm_tt_get_usermm(bo->tbo.ttm))
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400626 return -EPERM;
627
Chunming Zhou7e5a5472015-04-24 17:37:30 +0800628 if (WARN_ON_ONCE(min_offset > max_offset))
629 return -EINVAL;
630
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400631 if (bo->pin_count) {
Flora Cui408778e2016-08-18 12:55:13 +0800632 uint32_t mem_type = bo->tbo.mem.mem_type;
633
634 if (domain != amdgpu_mem_type_to_domain(mem_type))
635 return -EINVAL;
636
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400637 bo->pin_count++;
638 if (gpu_addr)
639 *gpu_addr = amdgpu_bo_gpu_offset(bo);
640
641 if (max_offset != 0) {
Flora Cui27798e02016-08-18 13:18:09 +0800642 u64 domain_start = bo->tbo.bdev->man[mem_type].gpu_offset;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400643 WARN_ON_ONCE(max_offset <
644 (amdgpu_bo_gpu_offset(bo) - domain_start));
645 }
646
647 return 0;
648 }
649 amdgpu_ttm_placement_from_domain(bo, domain);
650 for (i = 0; i < bo->placement.num_placement; i++) {
651 /* force to pin into visible video ram */
652 if ((bo->placements[i].flags & TTM_PL_FLAG_VRAM) &&
Chunming Zhou7e5a5472015-04-24 17:37:30 +0800653 !(bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS) &&
Christian König6681c5e2016-08-12 16:50:12 +0200654 (!max_offset || max_offset >
655 bo->adev->mc.visible_vram_size)) {
Chunming Zhou7e5a5472015-04-24 17:37:30 +0800656 if (WARN_ON_ONCE(min_offset >
657 bo->adev->mc.visible_vram_size))
658 return -EINVAL;
659 fpfn = min_offset >> PAGE_SHIFT;
660 lpfn = bo->adev->mc.visible_vram_size >> PAGE_SHIFT;
661 } else {
662 fpfn = min_offset >> PAGE_SHIFT;
663 lpfn = max_offset >> PAGE_SHIFT;
664 }
665 if (fpfn > bo->placements[i].fpfn)
666 bo->placements[i].fpfn = fpfn;
Christian König78d0e182016-01-19 12:48:14 +0100667 if (!bo->placements[i].lpfn ||
668 (lpfn && lpfn < bo->placements[i].lpfn))
Chunming Zhou7e5a5472015-04-24 17:37:30 +0800669 bo->placements[i].lpfn = lpfn;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400670 bo->placements[i].flags |= TTM_PL_FLAG_NO_EVICT;
671 }
672
673 r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false);
Christian König6681c5e2016-08-12 16:50:12 +0200674 if (unlikely(r)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400675 dev_err(bo->adev->dev, "%p pin failed\n", bo);
Christian König6681c5e2016-08-12 16:50:12 +0200676 goto error;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400677 }
Christian Königc855e252016-09-05 17:00:57 +0200678 r = amdgpu_ttm_bind(bo->tbo.ttm, &bo->tbo.mem);
679 if (unlikely(r)) {
680 dev_err(bo->adev->dev, "%p bind failed\n", bo);
681 goto error;
682 }
Christian König6681c5e2016-08-12 16:50:12 +0200683
684 bo->pin_count = 1;
685 if (gpu_addr != NULL)
686 *gpu_addr = amdgpu_bo_gpu_offset(bo);
687 if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
688 bo->adev->vram_pin_size += amdgpu_bo_size(bo);
689 if (bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)
690 bo->adev->invisible_pin_size += amdgpu_bo_size(bo);
Flora Cui32ab75f2016-08-18 13:17:07 +0800691 } else if (domain == AMDGPU_GEM_DOMAIN_GTT) {
Christian König6681c5e2016-08-12 16:50:12 +0200692 bo->adev->gart_pin_size += amdgpu_bo_size(bo);
693 }
694
695error:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400696 return r;
697}
698
699int amdgpu_bo_pin(struct amdgpu_bo *bo, u32 domain, u64 *gpu_addr)
700{
Chunming Zhou7e5a5472015-04-24 17:37:30 +0800701 return amdgpu_bo_pin_restricted(bo, domain, 0, 0, gpu_addr);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400702}
703
704int amdgpu_bo_unpin(struct amdgpu_bo *bo)
705{
706 int r, i;
707
708 if (!bo->pin_count) {
709 dev_warn(bo->adev->dev, "%p unpin not necessary\n", bo);
710 return 0;
711 }
712 bo->pin_count--;
713 if (bo->pin_count)
714 return 0;
715 for (i = 0; i < bo->placement.num_placement; i++) {
716 bo->placements[i].lpfn = 0;
717 bo->placements[i].flags &= ~TTM_PL_FLAG_NO_EVICT;
718 }
719 r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false);
Christian König6681c5e2016-08-12 16:50:12 +0200720 if (unlikely(r)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400721 dev_err(bo->adev->dev, "%p validate failed for unpin\n", bo);
Christian König6681c5e2016-08-12 16:50:12 +0200722 goto error;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400723 }
Christian König6681c5e2016-08-12 16:50:12 +0200724
725 if (bo->tbo.mem.mem_type == TTM_PL_VRAM) {
726 bo->adev->vram_pin_size -= amdgpu_bo_size(bo);
727 if (bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)
728 bo->adev->invisible_pin_size -= amdgpu_bo_size(bo);
729 } else {
730 bo->adev->gart_pin_size -= amdgpu_bo_size(bo);
731 }
732
733error:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400734 return r;
735}
736
737int amdgpu_bo_evict_vram(struct amdgpu_device *adev)
738{
739 /* late 2.6.33 fix IGP hibernate - we need pm ops to do this correct */
Jammy Zhou2f7d10b2015-07-22 11:29:01 +0800740 if (0 && (adev->flags & AMD_IS_APU)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400741 /* Useless to evict on IGP chips */
742 return 0;
743 }
744 return ttm_bo_evict_mm(&adev->mman.bdev, TTM_PL_VRAM);
745}
746
Alex Deucher1f8628c2016-03-31 16:56:22 -0400747static const char *amdgpu_vram_names[] = {
748 "UNKNOWN",
749 "GDDR1",
750 "DDR2",
751 "GDDR3",
752 "GDDR4",
753 "GDDR5",
754 "HBM",
755 "DDR3"
756};
757
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400758int amdgpu_bo_init(struct amdgpu_device *adev)
759{
760 /* Add an MTRR for the VRAM */
761 adev->mc.vram_mtrr = arch_phys_wc_add(adev->mc.aper_base,
762 adev->mc.aper_size);
763 DRM_INFO("Detected VRAM RAM=%lluM, BAR=%lluM\n",
764 adev->mc.mc_vram_size >> 20,
765 (unsigned long long)adev->mc.aper_size >> 20);
Alex Deucher1f8628c2016-03-31 16:56:22 -0400766 DRM_INFO("RAM width %dbits %s\n",
767 adev->mc.vram_width, amdgpu_vram_names[adev->mc.vram_type]);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400768 return amdgpu_ttm_init(adev);
769}
770
771void amdgpu_bo_fini(struct amdgpu_device *adev)
772{
773 amdgpu_ttm_fini(adev);
774 arch_phys_wc_del(adev->mc.vram_mtrr);
775}
776
777int amdgpu_bo_fbdev_mmap(struct amdgpu_bo *bo,
778 struct vm_area_struct *vma)
779{
780 return ttm_fbdev_mmap(vma, &bo->tbo);
781}
782
783int amdgpu_bo_set_tiling_flags(struct amdgpu_bo *bo, u64 tiling_flags)
784{
Marek Olšákfbd76d52015-05-14 23:48:26 +0200785 if (AMDGPU_TILING_GET(tiling_flags, TILE_SPLIT) > 6)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400786 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400787
788 bo->tiling_flags = tiling_flags;
789 return 0;
790}
791
792void amdgpu_bo_get_tiling_flags(struct amdgpu_bo *bo, u64 *tiling_flags)
793{
794 lockdep_assert_held(&bo->tbo.resv->lock.base);
795
796 if (tiling_flags)
797 *tiling_flags = bo->tiling_flags;
798}
799
800int amdgpu_bo_set_metadata (struct amdgpu_bo *bo, void *metadata,
801 uint32_t metadata_size, uint64_t flags)
802{
803 void *buffer;
804
805 if (!metadata_size) {
806 if (bo->metadata_size) {
807 kfree(bo->metadata);
Dave Airlie0092d3e2016-05-03 12:44:29 +1000808 bo->metadata = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400809 bo->metadata_size = 0;
810 }
811 return 0;
812 }
813
814 if (metadata == NULL)
815 return -EINVAL;
816
Andrzej Hajda71affda2015-09-21 17:34:39 -0400817 buffer = kmemdup(metadata, metadata_size, GFP_KERNEL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400818 if (buffer == NULL)
819 return -ENOMEM;
820
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400821 kfree(bo->metadata);
822 bo->metadata_flags = flags;
823 bo->metadata = buffer;
824 bo->metadata_size = metadata_size;
825
826 return 0;
827}
828
829int amdgpu_bo_get_metadata(struct amdgpu_bo *bo, void *buffer,
830 size_t buffer_size, uint32_t *metadata_size,
831 uint64_t *flags)
832{
833 if (!buffer && !metadata_size)
834 return -EINVAL;
835
836 if (buffer) {
837 if (buffer_size < bo->metadata_size)
838 return -EINVAL;
839
840 if (bo->metadata_size)
841 memcpy(buffer, bo->metadata, bo->metadata_size);
842 }
843
844 if (metadata_size)
845 *metadata_size = bo->metadata_size;
846 if (flags)
847 *flags = bo->metadata_flags;
848
849 return 0;
850}
851
852void amdgpu_bo_move_notify(struct ttm_buffer_object *bo,
853 struct ttm_mem_reg *new_mem)
854{
855 struct amdgpu_bo *rbo;
David Mao15da3012016-06-07 17:48:52 +0800856 struct ttm_mem_reg *old_mem = &bo->mem;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400857
858 if (!amdgpu_ttm_bo_is_amdgpu_bo(bo))
859 return;
860
861 rbo = container_of(bo, struct amdgpu_bo, tbo);
862 amdgpu_vm_bo_invalidate(rbo->adev, rbo);
863
864 /* update statistics */
865 if (!new_mem)
866 return;
867
868 /* move_notify is called before move happens */
869 amdgpu_update_memory_usage(rbo->adev, &bo->mem, new_mem);
David Mao15da3012016-06-07 17:48:52 +0800870
871 trace_amdgpu_ttm_bo_move(rbo, new_mem->mem_type, old_mem->mem_type);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400872}
873
874int amdgpu_bo_fault_reserve_notify(struct ttm_buffer_object *bo)
875{
876 struct amdgpu_device *adev;
Christian König5fb19412015-05-21 17:03:46 +0200877 struct amdgpu_bo *abo;
878 unsigned long offset, size, lpfn;
879 int i, r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400880
881 if (!amdgpu_ttm_bo_is_amdgpu_bo(bo))
882 return 0;
Christian König5fb19412015-05-21 17:03:46 +0200883
884 abo = container_of(bo, struct amdgpu_bo, tbo);
885 adev = abo->adev;
886 if (bo->mem.mem_type != TTM_PL_VRAM)
887 return 0;
888
889 size = bo->mem.num_pages << PAGE_SHIFT;
890 offset = bo->mem.start << PAGE_SHIFT;
891 if ((offset + size) <= adev->mc.visible_vram_size)
892 return 0;
893
Michel Dänzer104ece92016-03-28 12:53:02 +0900894 /* Can't move a pinned BO to visible VRAM */
895 if (abo->pin_count > 0)
896 return -EINVAL;
897
Christian König5fb19412015-05-21 17:03:46 +0200898 /* hurrah the memory is not visible ! */
899 amdgpu_ttm_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_VRAM);
900 lpfn = adev->mc.visible_vram_size >> PAGE_SHIFT;
901 for (i = 0; i < abo->placement.num_placement; i++) {
902 /* Force into visible VRAM */
903 if ((abo->placements[i].flags & TTM_PL_FLAG_VRAM) &&
Christian König6681c5e2016-08-12 16:50:12 +0200904 (!abo->placements[i].lpfn ||
905 abo->placements[i].lpfn > lpfn))
Christian König5fb19412015-05-21 17:03:46 +0200906 abo->placements[i].lpfn = lpfn;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400907 }
Christian König5fb19412015-05-21 17:03:46 +0200908 r = ttm_bo_validate(bo, &abo->placement, false, false);
909 if (unlikely(r == -ENOMEM)) {
910 amdgpu_ttm_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_GTT);
911 return ttm_bo_validate(bo, &abo->placement, false, false);
912 } else if (unlikely(r != 0)) {
913 return r;
914 }
915
916 offset = bo->mem.start << PAGE_SHIFT;
917 /* this should never happen */
918 if ((offset + size) > adev->mc.visible_vram_size)
919 return -EINVAL;
920
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400921 return 0;
922}
923
924/**
925 * amdgpu_bo_fence - add fence to buffer object
926 *
927 * @bo: buffer object in question
928 * @fence: fence to add
929 * @shared: true if fence should be added shared
930 *
931 */
Chunming Zhoue40a3112015-08-03 11:38:09 +0800932void amdgpu_bo_fence(struct amdgpu_bo *bo, struct fence *fence,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400933 bool shared)
934{
935 struct reservation_object *resv = bo->tbo.resv;
936
937 if (shared)
Chunming Zhoue40a3112015-08-03 11:38:09 +0800938 reservation_object_add_shared_fence(resv, fence);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400939 else
Chunming Zhoue40a3112015-08-03 11:38:09 +0800940 reservation_object_add_excl_fence(resv, fence);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400941}
Christian Königcdb7e8f2016-07-25 17:56:18 +0200942
943/**
944 * amdgpu_bo_gpu_offset - return GPU offset of bo
945 * @bo: amdgpu object for which we query the offset
946 *
947 * Returns current GPU offset of the object.
948 *
949 * Note: object should either be pinned or reserved when calling this
950 * function, it might be useful to add check for this for debugging.
951 */
952u64 amdgpu_bo_gpu_offset(struct amdgpu_bo *bo)
953{
954 WARN_ON_ONCE(bo->tbo.mem.mem_type == TTM_PL_SYSTEM);
Christian Königc855e252016-09-05 17:00:57 +0200955 WARN_ON_ONCE(bo->tbo.mem.mem_type == TTM_PL_TT &&
956 !amdgpu_ttm_is_bound(bo->tbo.ttm));
Christian Königcdb7e8f2016-07-25 17:56:18 +0200957 WARN_ON_ONCE(!ww_mutex_is_locked(&bo->tbo.resv->lock) &&
958 !bo->pin_count);
959
960 return bo->tbo.offset;
961}