blob: 5afe72778518f109adaaef3577af2e8080fb6d17 [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
Alex Deucherd38ceaf2015-04-20 16:55:21 -040041
42static u64 amdgpu_get_vis_part_size(struct amdgpu_device *adev,
Chunming Zhou7e5a5472015-04-24 17:37:30 +080043 struct ttm_mem_reg *mem)
Alex Deucherd38ceaf2015-04-20 16:55:21 -040044{
Christian König6681c5e2016-08-12 16:50:12 +020045 if (mem->start << PAGE_SHIFT >= adev->mc.visible_vram_size)
46 return 0;
47
48 return ((mem->start << PAGE_SHIFT) + mem->size) >
49 adev->mc.visible_vram_size ?
50 adev->mc.visible_vram_size - (mem->start << PAGE_SHIFT) :
51 mem->size;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040052}
53
54static void amdgpu_update_memory_usage(struct amdgpu_device *adev,
55 struct ttm_mem_reg *old_mem,
56 struct ttm_mem_reg *new_mem)
57{
58 u64 vis_size;
59 if (!adev)
60 return;
61
62 if (new_mem) {
63 switch (new_mem->mem_type) {
64 case TTM_PL_TT:
65 atomic64_add(new_mem->size, &adev->gtt_usage);
66 break;
67 case TTM_PL_VRAM:
68 atomic64_add(new_mem->size, &adev->vram_usage);
69 vis_size = amdgpu_get_vis_part_size(adev, new_mem);
70 atomic64_add(vis_size, &adev->vram_vis_usage);
71 break;
72 }
73 }
74
75 if (old_mem) {
76 switch (old_mem->mem_type) {
77 case TTM_PL_TT:
78 atomic64_sub(old_mem->size, &adev->gtt_usage);
79 break;
80 case TTM_PL_VRAM:
81 atomic64_sub(old_mem->size, &adev->vram_usage);
82 vis_size = amdgpu_get_vis_part_size(adev, old_mem);
83 atomic64_sub(vis_size, &adev->vram_vis_usage);
84 break;
85 }
86 }
87}
88
89static void amdgpu_ttm_bo_destroy(struct ttm_buffer_object *tbo)
90{
91 struct amdgpu_bo *bo;
92
93 bo = container_of(tbo, struct amdgpu_bo, tbo);
94
95 amdgpu_update_memory_usage(bo->adev, &bo->tbo.mem, NULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -040096
Christian König0ca43b02018-03-09 14:42:54 +010097 if (bo->gem_base.import_attach)
98 drm_prime_gem_destroy(&bo->gem_base, bo->tbo.sg);
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
Christian König765e7fb2016-09-15 15:06:50 +0200208void amdgpu_ttm_placement_from_domain(struct amdgpu_bo *abo, u32 domain)
Chunming Zhou7e5a5472015-04-24 17:37:30 +0800209{
Christian König765e7fb2016-09-15 15:06:50 +0200210 amdgpu_ttm_placement_init(abo->adev, &abo->placement,
211 abo->placements, domain, abo->flags);
Chunming Zhou7e5a5472015-04-24 17:37:30 +0800212}
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;
Chunming Zhou0c4e7fa2016-08-17 11:41:30 +0800357 INIT_LIST_HEAD(&bo->shadow_list);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400358 INIT_LIST_HEAD(&bo->va);
Christian König1ea863f2015-12-18 22:13:12 +0100359 bo->prefered_domains = domain & (AMDGPU_GEM_DOMAIN_VRAM |
360 AMDGPU_GEM_DOMAIN_GTT |
361 AMDGPU_GEM_DOMAIN_CPU |
362 AMDGPU_GEM_DOMAIN_GDS |
363 AMDGPU_GEM_DOMAIN_GWS |
364 AMDGPU_GEM_DOMAIN_OA);
365 bo->allowed_domains = bo->prefered_domains;
366 if (!kernel && bo->allowed_domains == AMDGPU_GEM_DOMAIN_VRAM)
367 bo->allowed_domains |= AMDGPU_GEM_DOMAIN_GTT;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400368
369 bo->flags = flags;
Oded Gabbaya187f172016-01-30 07:59:34 +0200370
371 /* For architectures that don't support WC memory,
372 * mask out the WC flag from the BO
373 */
374 if (!drm_arch_can_wc_memory())
375 bo->flags &= ~AMDGPU_GEM_CREATE_CPU_GTT_USWC;
376
Chunming Zhou7e5a5472015-04-24 17:37:30 +0800377 amdgpu_fill_placement_to_bo(bo, placement);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400378 /* Kernel allocation are uninterruptible */
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400379 r = ttm_bo_init(&adev->mman.bdev, &bo->tbo, size, type,
380 &bo->placement, page_align, !kernel, NULL,
Christian König72d76682015-09-03 17:34:59 +0200381 acc_size, sg, resv, &amdgpu_ttm_bo_destroy);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400382 if (unlikely(r != 0)) {
383 return r;
384 }
Flora Cui4fea83f2016-07-20 14:44:38 +0800385
386 if (flags & AMDGPU_GEM_CREATE_VRAM_CLEARED &&
387 bo->tbo.mem.placement & TTM_PL_FLAG_VRAM) {
388 struct fence *fence;
389
390 if (adev->mman.buffer_funcs_ring == NULL ||
391 !adev->mman.buffer_funcs_ring->ready) {
392 r = -EBUSY;
393 goto fail_free;
394 }
395
396 r = amdgpu_bo_reserve(bo, false);
397 if (unlikely(r != 0))
398 goto fail_free;
399
400 amdgpu_ttm_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_VRAM);
401 r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false);
402 if (unlikely(r != 0))
403 goto fail_unreserve;
404
405 amdgpu_fill_buffer(bo, 0, bo->tbo.resv, &fence);
406 amdgpu_bo_fence(bo, fence, false);
407 amdgpu_bo_unreserve(bo);
408 fence_put(bo->tbo.moving);
409 bo->tbo.moving = fence_get(fence);
410 fence_put(fence);
411 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400412 *bo_ptr = bo;
413
414 trace_amdgpu_bo_create(bo);
415
416 return 0;
Flora Cui4fea83f2016-07-20 14:44:38 +0800417
418fail_unreserve:
419 amdgpu_bo_unreserve(bo);
420fail_free:
421 amdgpu_bo_unref(&bo);
422 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400423}
424
Chunming Zhoue7893c42016-07-26 14:13:21 +0800425static int amdgpu_bo_create_shadow(struct amdgpu_device *adev,
426 unsigned long size, int byte_align,
427 struct amdgpu_bo *bo)
428{
429 struct ttm_placement placement = {0};
430 struct ttm_place placements[AMDGPU_GEM_DOMAIN_MAX + 1];
431 int r;
432
433 if (bo->shadow)
434 return 0;
435
436 bo->flags |= AMDGPU_GEM_CREATE_SHADOW;
437 memset(&placements, 0,
438 (AMDGPU_GEM_DOMAIN_MAX + 1) * sizeof(struct ttm_place));
439
440 amdgpu_ttm_placement_init(adev, &placement,
441 placements, AMDGPU_GEM_DOMAIN_GTT,
442 AMDGPU_GEM_CREATE_CPU_GTT_USWC);
443
444 r = amdgpu_bo_create_restricted(adev, size, byte_align, true,
445 AMDGPU_GEM_DOMAIN_GTT,
446 AMDGPU_GEM_CREATE_CPU_GTT_USWC,
447 NULL, &placement,
448 bo->tbo.resv,
449 &bo->shadow);
Chunming Zhou0c4e7fa2016-08-17 11:41:30 +0800450 if (!r) {
Chunming Zhoue7893c42016-07-26 14:13:21 +0800451 bo->shadow->parent = amdgpu_bo_ref(bo);
Chunming Zhou0c4e7fa2016-08-17 11:41:30 +0800452 mutex_lock(&adev->shadow_list_lock);
453 list_add_tail(&bo->shadow_list, &adev->shadow_list);
454 mutex_unlock(&adev->shadow_list_lock);
455 }
Chunming Zhoue7893c42016-07-26 14:13:21 +0800456
457 return r;
458}
459
Chunming Zhou7e5a5472015-04-24 17:37:30 +0800460int amdgpu_bo_create(struct amdgpu_device *adev,
461 unsigned long size, int byte_align,
462 bool kernel, u32 domain, u64 flags,
Christian König72d76682015-09-03 17:34:59 +0200463 struct sg_table *sg,
464 struct reservation_object *resv,
465 struct amdgpu_bo **bo_ptr)
Chunming Zhou7e5a5472015-04-24 17:37:30 +0800466{
467 struct ttm_placement placement = {0};
468 struct ttm_place placements[AMDGPU_GEM_DOMAIN_MAX + 1];
Chunming Zhoue7893c42016-07-26 14:13:21 +0800469 int r;
Chunming Zhou7e5a5472015-04-24 17:37:30 +0800470
471 memset(&placements, 0,
472 (AMDGPU_GEM_DOMAIN_MAX + 1) * sizeof(struct ttm_place));
473
474 amdgpu_ttm_placement_init(adev, &placement,
475 placements, domain, flags);
476
Chunming Zhoue7893c42016-07-26 14:13:21 +0800477 r = amdgpu_bo_create_restricted(adev, size, byte_align, kernel,
478 domain, flags, sg, &placement,
479 resv, bo_ptr);
480 if (r)
481 return r;
482
Chunming Zhou3ad81f12016-08-05 17:30:17 +0800483 if (amdgpu_need_backup(adev) && (flags & AMDGPU_GEM_CREATE_SHADOW)) {
Chunming Zhoue7893c42016-07-26 14:13:21 +0800484 r = amdgpu_bo_create_shadow(adev, size, byte_align, (*bo_ptr));
485 if (r)
486 amdgpu_bo_unref(bo_ptr);
487 }
488
489 return r;
Chunming Zhou7e5a5472015-04-24 17:37:30 +0800490}
491
Chunming Zhou20f4eff2016-08-04 16:51:18 +0800492int amdgpu_bo_backup_to_shadow(struct amdgpu_device *adev,
493 struct amdgpu_ring *ring,
494 struct amdgpu_bo *bo,
495 struct reservation_object *resv,
496 struct fence **fence,
497 bool direct)
498
499{
500 struct amdgpu_bo *shadow = bo->shadow;
501 uint64_t bo_addr, shadow_addr;
502 int r;
503
504 if (!shadow)
505 return -EINVAL;
506
507 bo_addr = amdgpu_bo_gpu_offset(bo);
508 shadow_addr = amdgpu_bo_gpu_offset(bo->shadow);
509
510 r = reservation_object_reserve_shared(bo->tbo.resv);
511 if (r)
512 goto err;
513
514 r = amdgpu_copy_buffer(ring, bo_addr, shadow_addr,
515 amdgpu_bo_size(bo), resv, fence,
516 direct);
517 if (!r)
518 amdgpu_bo_fence(bo, *fence, true);
519
520err:
521 return r;
522}
523
524int amdgpu_bo_restore_from_shadow(struct amdgpu_device *adev,
525 struct amdgpu_ring *ring,
526 struct amdgpu_bo *bo,
527 struct reservation_object *resv,
528 struct fence **fence,
529 bool direct)
530
531{
532 struct amdgpu_bo *shadow = bo->shadow;
533 uint64_t bo_addr, shadow_addr;
534 int r;
535
536 if (!shadow)
537 return -EINVAL;
538
539 bo_addr = amdgpu_bo_gpu_offset(bo);
540 shadow_addr = amdgpu_bo_gpu_offset(bo->shadow);
541
542 r = reservation_object_reserve_shared(bo->tbo.resv);
543 if (r)
544 goto err;
545
546 r = amdgpu_copy_buffer(ring, shadow_addr, bo_addr,
547 amdgpu_bo_size(bo), resv, fence,
548 direct);
549 if (!r)
550 amdgpu_bo_fence(bo, *fence, true);
551
552err:
553 return r;
554}
555
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400556int amdgpu_bo_kmap(struct amdgpu_bo *bo, void **ptr)
557{
558 bool is_iomem;
Christian König587f3c72016-03-10 16:21:04 +0100559 long r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400560
Christian König271c8122015-05-13 14:30:53 +0200561 if (bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)
562 return -EPERM;
563
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400564 if (bo->kptr) {
565 if (ptr) {
566 *ptr = bo->kptr;
567 }
568 return 0;
569 }
Christian König587f3c72016-03-10 16:21:04 +0100570
571 r = reservation_object_wait_timeout_rcu(bo->tbo.resv, false, false,
572 MAX_SCHEDULE_TIMEOUT);
573 if (r < 0)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400574 return r;
Christian König587f3c72016-03-10 16:21:04 +0100575
576 r = ttm_bo_kmap(&bo->tbo, 0, bo->tbo.num_pages, &bo->kmap);
577 if (r)
578 return r;
579
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400580 bo->kptr = ttm_kmap_obj_virtual(&bo->kmap, &is_iomem);
Christian König587f3c72016-03-10 16:21:04 +0100581 if (ptr)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400582 *ptr = bo->kptr;
Christian König587f3c72016-03-10 16:21:04 +0100583
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400584 return 0;
585}
586
587void amdgpu_bo_kunmap(struct amdgpu_bo *bo)
588{
589 if (bo->kptr == NULL)
590 return;
591 bo->kptr = NULL;
592 ttm_bo_kunmap(&bo->kmap);
593}
594
595struct amdgpu_bo *amdgpu_bo_ref(struct amdgpu_bo *bo)
596{
597 if (bo == NULL)
598 return NULL;
599
600 ttm_bo_reference(&bo->tbo);
601 return bo;
602}
603
604void amdgpu_bo_unref(struct amdgpu_bo **bo)
605{
606 struct ttm_buffer_object *tbo;
607
608 if ((*bo) == NULL)
609 return;
610
611 tbo = &((*bo)->tbo);
612 ttm_bo_unref(&tbo);
613 if (tbo == NULL)
614 *bo = NULL;
615}
616
Chunming Zhou7e5a5472015-04-24 17:37:30 +0800617int amdgpu_bo_pin_restricted(struct amdgpu_bo *bo, u32 domain,
618 u64 min_offset, u64 max_offset,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400619 u64 *gpu_addr)
620{
621 int r, i;
Chunming Zhou7e5a5472015-04-24 17:37:30 +0800622 unsigned fpfn, lpfn;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400623
Christian Königcc325d12016-02-08 11:08:35 +0100624 if (amdgpu_ttm_tt_get_usermm(bo->tbo.ttm))
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400625 return -EPERM;
626
Chunming Zhou7e5a5472015-04-24 17:37:30 +0800627 if (WARN_ON_ONCE(min_offset > max_offset))
628 return -EINVAL;
629
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400630 if (bo->pin_count) {
Flora Cui408778e2016-08-18 12:55:13 +0800631 uint32_t mem_type = bo->tbo.mem.mem_type;
632
633 if (domain != amdgpu_mem_type_to_domain(mem_type))
634 return -EINVAL;
635
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400636 bo->pin_count++;
637 if (gpu_addr)
638 *gpu_addr = amdgpu_bo_gpu_offset(bo);
639
640 if (max_offset != 0) {
Flora Cui27798e02016-08-18 13:18:09 +0800641 u64 domain_start = bo->tbo.bdev->man[mem_type].gpu_offset;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400642 WARN_ON_ONCE(max_offset <
643 (amdgpu_bo_gpu_offset(bo) - domain_start));
644 }
645
646 return 0;
647 }
648 amdgpu_ttm_placement_from_domain(bo, domain);
649 for (i = 0; i < bo->placement.num_placement; i++) {
650 /* force to pin into visible video ram */
651 if ((bo->placements[i].flags & TTM_PL_FLAG_VRAM) &&
Chunming Zhou7e5a5472015-04-24 17:37:30 +0800652 !(bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS) &&
Christian König6681c5e2016-08-12 16:50:12 +0200653 (!max_offset || max_offset >
654 bo->adev->mc.visible_vram_size)) {
Chunming Zhou7e5a5472015-04-24 17:37:30 +0800655 if (WARN_ON_ONCE(min_offset >
656 bo->adev->mc.visible_vram_size))
657 return -EINVAL;
658 fpfn = min_offset >> PAGE_SHIFT;
659 lpfn = bo->adev->mc.visible_vram_size >> PAGE_SHIFT;
660 } else {
661 fpfn = min_offset >> PAGE_SHIFT;
662 lpfn = max_offset >> PAGE_SHIFT;
663 }
664 if (fpfn > bo->placements[i].fpfn)
665 bo->placements[i].fpfn = fpfn;
Christian König78d0e182016-01-19 12:48:14 +0100666 if (!bo->placements[i].lpfn ||
667 (lpfn && lpfn < bo->placements[i].lpfn))
Chunming Zhou7e5a5472015-04-24 17:37:30 +0800668 bo->placements[i].lpfn = lpfn;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400669 bo->placements[i].flags |= TTM_PL_FLAG_NO_EVICT;
670 }
671
672 r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false);
Christian König6681c5e2016-08-12 16:50:12 +0200673 if (unlikely(r)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400674 dev_err(bo->adev->dev, "%p pin failed\n", bo);
Christian König6681c5e2016-08-12 16:50:12 +0200675 goto error;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400676 }
Christian Königbb990bb2016-09-09 16:32:33 +0200677 r = amdgpu_ttm_bind(&bo->tbo, &bo->tbo.mem);
Christian Königc855e252016-09-05 17:00:57 +0200678 if (unlikely(r)) {
679 dev_err(bo->adev->dev, "%p bind failed\n", bo);
680 goto error;
681 }
Christian König6681c5e2016-08-12 16:50:12 +0200682
683 bo->pin_count = 1;
684 if (gpu_addr != NULL)
685 *gpu_addr = amdgpu_bo_gpu_offset(bo);
686 if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
687 bo->adev->vram_pin_size += amdgpu_bo_size(bo);
688 if (bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)
689 bo->adev->invisible_pin_size += amdgpu_bo_size(bo);
Flora Cui32ab75f2016-08-18 13:17:07 +0800690 } else if (domain == AMDGPU_GEM_DOMAIN_GTT) {
Christian König6681c5e2016-08-12 16:50:12 +0200691 bo->adev->gart_pin_size += amdgpu_bo_size(bo);
692 }
693
694error:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400695 return r;
696}
697
698int amdgpu_bo_pin(struct amdgpu_bo *bo, u32 domain, u64 *gpu_addr)
699{
Chunming Zhou7e5a5472015-04-24 17:37:30 +0800700 return amdgpu_bo_pin_restricted(bo, domain, 0, 0, gpu_addr);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400701}
702
703int amdgpu_bo_unpin(struct amdgpu_bo *bo)
704{
705 int r, i;
706
707 if (!bo->pin_count) {
708 dev_warn(bo->adev->dev, "%p unpin not necessary\n", bo);
709 return 0;
710 }
711 bo->pin_count--;
712 if (bo->pin_count)
713 return 0;
714 for (i = 0; i < bo->placement.num_placement; i++) {
715 bo->placements[i].lpfn = 0;
716 bo->placements[i].flags &= ~TTM_PL_FLAG_NO_EVICT;
717 }
718 r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false);
Christian König6681c5e2016-08-12 16:50:12 +0200719 if (unlikely(r)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400720 dev_err(bo->adev->dev, "%p validate failed for unpin\n", bo);
Christian König6681c5e2016-08-12 16:50:12 +0200721 goto error;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400722 }
Christian König6681c5e2016-08-12 16:50:12 +0200723
724 if (bo->tbo.mem.mem_type == TTM_PL_VRAM) {
725 bo->adev->vram_pin_size -= amdgpu_bo_size(bo);
726 if (bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)
727 bo->adev->invisible_pin_size -= amdgpu_bo_size(bo);
Flora Cui441f90e2016-09-09 14:15:30 +0800728 } else if (bo->tbo.mem.mem_type == TTM_PL_TT) {
Christian König6681c5e2016-08-12 16:50:12 +0200729 bo->adev->gart_pin_size -= amdgpu_bo_size(bo);
730 }
731
732error:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400733 return r;
734}
735
736int amdgpu_bo_evict_vram(struct amdgpu_device *adev)
737{
738 /* late 2.6.33 fix IGP hibernate - we need pm ops to do this correct */
Jammy Zhou2f7d10b2015-07-22 11:29:01 +0800739 if (0 && (adev->flags & AMD_IS_APU)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400740 /* Useless to evict on IGP chips */
741 return 0;
742 }
743 return ttm_bo_evict_mm(&adev->mman.bdev, TTM_PL_VRAM);
744}
745
Alex Deucher1f8628c2016-03-31 16:56:22 -0400746static const char *amdgpu_vram_names[] = {
747 "UNKNOWN",
748 "GDDR1",
749 "DDR2",
750 "GDDR3",
751 "GDDR4",
752 "GDDR5",
753 "HBM",
754 "DDR3"
755};
756
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400757int amdgpu_bo_init(struct amdgpu_device *adev)
758{
Dave Airlie7cf321d2016-10-24 15:37:48 +1000759 /* reserve PAT memory space to WC for VRAM */
760 arch_io_reserve_memtype_wc(adev->mc.aper_base,
761 adev->mc.aper_size);
762
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400763 /* Add an MTRR for the VRAM */
764 adev->mc.vram_mtrr = arch_phys_wc_add(adev->mc.aper_base,
765 adev->mc.aper_size);
766 DRM_INFO("Detected VRAM RAM=%lluM, BAR=%lluM\n",
767 adev->mc.mc_vram_size >> 20,
768 (unsigned long long)adev->mc.aper_size >> 20);
Alex Deucher1f8628c2016-03-31 16:56:22 -0400769 DRM_INFO("RAM width %dbits %s\n",
770 adev->mc.vram_width, amdgpu_vram_names[adev->mc.vram_type]);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400771 return amdgpu_ttm_init(adev);
772}
773
774void amdgpu_bo_fini(struct amdgpu_device *adev)
775{
776 amdgpu_ttm_fini(adev);
777 arch_phys_wc_del(adev->mc.vram_mtrr);
Dave Airlie7cf321d2016-10-24 15:37:48 +1000778 arch_io_free_memtype_wc(adev->mc.aper_base, adev->mc.aper_size);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400779}
780
781int amdgpu_bo_fbdev_mmap(struct amdgpu_bo *bo,
782 struct vm_area_struct *vma)
783{
784 return ttm_fbdev_mmap(vma, &bo->tbo);
785}
786
787int amdgpu_bo_set_tiling_flags(struct amdgpu_bo *bo, u64 tiling_flags)
788{
Marek Olšákfbd76d52015-05-14 23:48:26 +0200789 if (AMDGPU_TILING_GET(tiling_flags, TILE_SPLIT) > 6)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400790 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400791
792 bo->tiling_flags = tiling_flags;
793 return 0;
794}
795
796void amdgpu_bo_get_tiling_flags(struct amdgpu_bo *bo, u64 *tiling_flags)
797{
798 lockdep_assert_held(&bo->tbo.resv->lock.base);
799
800 if (tiling_flags)
801 *tiling_flags = bo->tiling_flags;
802}
803
804int amdgpu_bo_set_metadata (struct amdgpu_bo *bo, void *metadata,
805 uint32_t metadata_size, uint64_t flags)
806{
807 void *buffer;
808
809 if (!metadata_size) {
810 if (bo->metadata_size) {
811 kfree(bo->metadata);
Dave Airlie0092d3e2016-05-03 12:44:29 +1000812 bo->metadata = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400813 bo->metadata_size = 0;
814 }
815 return 0;
816 }
817
818 if (metadata == NULL)
819 return -EINVAL;
820
Andrzej Hajda71affda2015-09-21 17:34:39 -0400821 buffer = kmemdup(metadata, metadata_size, GFP_KERNEL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400822 if (buffer == NULL)
823 return -ENOMEM;
824
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400825 kfree(bo->metadata);
826 bo->metadata_flags = flags;
827 bo->metadata = buffer;
828 bo->metadata_size = metadata_size;
829
830 return 0;
831}
832
833int amdgpu_bo_get_metadata(struct amdgpu_bo *bo, void *buffer,
834 size_t buffer_size, uint32_t *metadata_size,
835 uint64_t *flags)
836{
837 if (!buffer && !metadata_size)
838 return -EINVAL;
839
840 if (buffer) {
841 if (buffer_size < bo->metadata_size)
842 return -EINVAL;
843
844 if (bo->metadata_size)
845 memcpy(buffer, bo->metadata, bo->metadata_size);
846 }
847
848 if (metadata_size)
849 *metadata_size = bo->metadata_size;
850 if (flags)
851 *flags = bo->metadata_flags;
852
853 return 0;
854}
855
856void amdgpu_bo_move_notify(struct ttm_buffer_object *bo,
857 struct ttm_mem_reg *new_mem)
858{
Christian König765e7fb2016-09-15 15:06:50 +0200859 struct amdgpu_bo *abo;
David Mao15da3012016-06-07 17:48:52 +0800860 struct ttm_mem_reg *old_mem = &bo->mem;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400861
862 if (!amdgpu_ttm_bo_is_amdgpu_bo(bo))
863 return;
864
Christian König765e7fb2016-09-15 15:06:50 +0200865 abo = container_of(bo, struct amdgpu_bo, tbo);
866 amdgpu_vm_bo_invalidate(abo->adev, abo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400867
868 /* update statistics */
869 if (!new_mem)
870 return;
871
872 /* move_notify is called before move happens */
Christian König765e7fb2016-09-15 15:06:50 +0200873 amdgpu_update_memory_usage(abo->adev, &bo->mem, new_mem);
David Mao15da3012016-06-07 17:48:52 +0800874
Christian König765e7fb2016-09-15 15:06:50 +0200875 trace_amdgpu_ttm_bo_move(abo, new_mem->mem_type, old_mem->mem_type);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400876}
877
878int amdgpu_bo_fault_reserve_notify(struct ttm_buffer_object *bo)
879{
880 struct amdgpu_device *adev;
Christian König5fb19412015-05-21 17:03:46 +0200881 struct amdgpu_bo *abo;
882 unsigned long offset, size, lpfn;
883 int i, r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400884
885 if (!amdgpu_ttm_bo_is_amdgpu_bo(bo))
886 return 0;
Christian König5fb19412015-05-21 17:03:46 +0200887
888 abo = container_of(bo, struct amdgpu_bo, tbo);
889 adev = abo->adev;
890 if (bo->mem.mem_type != TTM_PL_VRAM)
891 return 0;
892
893 size = bo->mem.num_pages << PAGE_SHIFT;
894 offset = bo->mem.start << PAGE_SHIFT;
895 if ((offset + size) <= adev->mc.visible_vram_size)
896 return 0;
897
Michel Dänzer104ece92016-03-28 12:53:02 +0900898 /* Can't move a pinned BO to visible VRAM */
899 if (abo->pin_count > 0)
900 return -EINVAL;
901
Christian König5fb19412015-05-21 17:03:46 +0200902 /* hurrah the memory is not visible ! */
903 amdgpu_ttm_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_VRAM);
904 lpfn = adev->mc.visible_vram_size >> PAGE_SHIFT;
905 for (i = 0; i < abo->placement.num_placement; i++) {
906 /* Force into visible VRAM */
907 if ((abo->placements[i].flags & TTM_PL_FLAG_VRAM) &&
Christian König6681c5e2016-08-12 16:50:12 +0200908 (!abo->placements[i].lpfn ||
909 abo->placements[i].lpfn > lpfn))
Christian König5fb19412015-05-21 17:03:46 +0200910 abo->placements[i].lpfn = lpfn;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400911 }
Christian König5fb19412015-05-21 17:03:46 +0200912 r = ttm_bo_validate(bo, &abo->placement, false, false);
913 if (unlikely(r == -ENOMEM)) {
914 amdgpu_ttm_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_GTT);
915 return ttm_bo_validate(bo, &abo->placement, false, false);
916 } else if (unlikely(r != 0)) {
917 return r;
918 }
919
920 offset = bo->mem.start << PAGE_SHIFT;
921 /* this should never happen */
922 if ((offset + size) > adev->mc.visible_vram_size)
923 return -EINVAL;
924
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400925 return 0;
926}
927
928/**
929 * amdgpu_bo_fence - add fence to buffer object
930 *
931 * @bo: buffer object in question
932 * @fence: fence to add
933 * @shared: true if fence should be added shared
934 *
935 */
Chunming Zhoue40a3112015-08-03 11:38:09 +0800936void amdgpu_bo_fence(struct amdgpu_bo *bo, struct fence *fence,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400937 bool shared)
938{
939 struct reservation_object *resv = bo->tbo.resv;
940
941 if (shared)
Chunming Zhoue40a3112015-08-03 11:38:09 +0800942 reservation_object_add_shared_fence(resv, fence);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400943 else
Chunming Zhoue40a3112015-08-03 11:38:09 +0800944 reservation_object_add_excl_fence(resv, fence);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400945}
Christian Königcdb7e8f2016-07-25 17:56:18 +0200946
947/**
948 * amdgpu_bo_gpu_offset - return GPU offset of bo
949 * @bo: amdgpu object for which we query the offset
950 *
951 * Returns current GPU offset of the object.
952 *
953 * Note: object should either be pinned or reserved when calling this
954 * function, it might be useful to add check for this for debugging.
955 */
956u64 amdgpu_bo_gpu_offset(struct amdgpu_bo *bo)
957{
958 WARN_ON_ONCE(bo->tbo.mem.mem_type == TTM_PL_SYSTEM);
Christian Königc855e252016-09-05 17:00:57 +0200959 WARN_ON_ONCE(bo->tbo.mem.mem_type == TTM_PL_TT &&
960 !amdgpu_ttm_is_bound(bo->tbo.ttm));
Christian Königcdb7e8f2016-07-25 17:56:18 +0200961 WARN_ON_ONCE(!ww_mutex_is_locked(&bo->tbo.resv->lock) &&
962 !bo->pin_count);
Christian König9702d402016-09-07 15:10:44 +0200963 WARN_ON_ONCE(bo->tbo.mem.start == AMDGPU_BO_INVALID_OFFSET);
Christian Königcdb7e8f2016-07-25 17:56:18 +0200964
965 return bo->tbo.offset;
966}