blob: 428aa00025e4875637afaa51b1b1e6ebba2799b8 [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
Alex Deucherd38ceaf2015-04-20 16:55:21 -040097 drm_gem_object_release(&bo->gem_base);
Christian König82b9c552015-11-27 16:49:00 +010098 amdgpu_bo_unref(&bo->parent);
Chunming Zhou0c4e7fa2016-08-17 11:41:30 +080099 if (!list_empty(&bo->shadow_list)) {
100 mutex_lock(&bo->adev->shadow_list_lock);
101 list_del_init(&bo->shadow_list);
102 mutex_unlock(&bo->adev->shadow_list_lock);
103 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400104 kfree(bo->metadata);
105 kfree(bo);
106}
107
108bool amdgpu_ttm_bo_is_amdgpu_bo(struct ttm_buffer_object *bo)
109{
110 if (bo->destroy == &amdgpu_ttm_bo_destroy)
111 return true;
112 return false;
113}
114
Chunming Zhou7e5a5472015-04-24 17:37:30 +0800115static void amdgpu_ttm_placement_init(struct amdgpu_device *adev,
116 struct ttm_placement *placement,
Christian Königfaceaf62016-08-15 14:06:50 +0200117 struct ttm_place *places,
Chunming Zhou7e5a5472015-04-24 17:37:30 +0800118 u32 domain, u64 flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400119{
Christian König6369f6f2016-08-15 14:08:54 +0200120 u32 c = 0;
Chunming Zhou7e5a5472015-04-24 17:37:30 +0800121
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400122 if (domain & AMDGPU_GEM_DOMAIN_VRAM) {
Christian Königfaceaf62016-08-15 14:06:50 +0200123 unsigned visible_pfn = adev->mc.visible_vram_size >> PAGE_SHIFT;
124
Chunming Zhou7e5a5472015-04-24 17:37:30 +0800125 if (flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS &&
Christian König6369f6f2016-08-15 14:08:54 +0200126 !(flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED) &&
Christian Königfaceaf62016-08-15 14:06:50 +0200127 adev->mc.visible_vram_size < adev->mc.real_vram_size) {
128 places[c].fpfn = visible_pfn;
Christian König6369f6f2016-08-15 14:08:54 +0200129 places[c].lpfn = 0;
Christian Königfaceaf62016-08-15 14:06:50 +0200130 places[c].flags = TTM_PL_FLAG_WC |
Christian König6681c5e2016-08-12 16:50:12 +0200131 TTM_PL_FLAG_UNCACHED | TTM_PL_FLAG_VRAM |
132 TTM_PL_FLAG_TOPDOWN;
Christian Königfaceaf62016-08-15 14:06:50 +0200133 c++;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400134 }
Christian Königfaceaf62016-08-15 14:06:50 +0200135
136 places[c].fpfn = 0;
137 places[c].lpfn = 0;
138 places[c].flags = TTM_PL_FLAG_WC | TTM_PL_FLAG_UNCACHED |
Chunming Zhou7e5a5472015-04-24 17:37:30 +0800139 TTM_PL_FLAG_VRAM;
Christian Königfaceaf62016-08-15 14:06:50 +0200140 if (flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED)
141 places[c].lpfn = visible_pfn;
142 else
143 places[c].flags |= TTM_PL_FLAG_TOPDOWN;
144 c++;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400145 }
146
147 if (domain & AMDGPU_GEM_DOMAIN_GTT) {
Christian Königfaceaf62016-08-15 14:06:50 +0200148 places[c].fpfn = 0;
149 places[c].lpfn = 0;
150 places[c].flags = TTM_PL_FLAG_TT;
151 if (flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC)
152 places[c].flags |= TTM_PL_FLAG_WC |
153 TTM_PL_FLAG_UNCACHED;
154 else
155 places[c].flags |= TTM_PL_FLAG_CACHED;
156 c++;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400157 }
158
159 if (domain & AMDGPU_GEM_DOMAIN_CPU) {
Christian Königfaceaf62016-08-15 14:06:50 +0200160 places[c].fpfn = 0;
161 places[c].lpfn = 0;
162 places[c].flags = TTM_PL_FLAG_SYSTEM;
163 if (flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC)
164 places[c].flags |= TTM_PL_FLAG_WC |
165 TTM_PL_FLAG_UNCACHED;
166 else
167 places[c].flags |= TTM_PL_FLAG_CACHED;
168 c++;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400169 }
170
171 if (domain & AMDGPU_GEM_DOMAIN_GDS) {
Christian Königfaceaf62016-08-15 14:06:50 +0200172 places[c].fpfn = 0;
173 places[c].lpfn = 0;
174 places[c].flags = TTM_PL_FLAG_UNCACHED | AMDGPU_PL_FLAG_GDS;
175 c++;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400176 }
Christian Königfaceaf62016-08-15 14:06:50 +0200177
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400178 if (domain & AMDGPU_GEM_DOMAIN_GWS) {
Christian Königfaceaf62016-08-15 14:06:50 +0200179 places[c].fpfn = 0;
180 places[c].lpfn = 0;
181 places[c].flags = TTM_PL_FLAG_UNCACHED | AMDGPU_PL_FLAG_GWS;
182 c++;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400183 }
Christian Königfaceaf62016-08-15 14:06:50 +0200184
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400185 if (domain & AMDGPU_GEM_DOMAIN_OA) {
Christian Königfaceaf62016-08-15 14:06:50 +0200186 places[c].fpfn = 0;
187 places[c].lpfn = 0;
188 places[c].flags = TTM_PL_FLAG_UNCACHED | AMDGPU_PL_FLAG_OA;
189 c++;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400190 }
191
192 if (!c) {
Christian Königfaceaf62016-08-15 14:06:50 +0200193 places[c].fpfn = 0;
194 places[c].lpfn = 0;
195 places[c].flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM;
196 c++;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400197 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400198
Christian Königfaceaf62016-08-15 14:06:50 +0200199 placement->num_placement = c;
200 placement->placement = places;
201
202 placement->num_busy_placement = c;
203 placement->busy_placement = places;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400204}
205
Chunming Zhou7e5a5472015-04-24 17:37:30 +0800206void amdgpu_ttm_placement_from_domain(struct amdgpu_bo *rbo, u32 domain)
207{
208 amdgpu_ttm_placement_init(rbo->adev, &rbo->placement,
209 rbo->placements, domain, rbo->flags);
210}
211
212static void amdgpu_fill_placement_to_bo(struct amdgpu_bo *bo,
213 struct ttm_placement *placement)
214{
215 BUG_ON(placement->num_placement > (AMDGPU_GEM_DOMAIN_MAX + 1));
216
217 memcpy(bo->placements, placement->placement,
218 placement->num_placement * sizeof(struct ttm_place));
219 bo->placement.num_placement = placement->num_placement;
220 bo->placement.num_busy_placement = placement->num_busy_placement;
221 bo->placement.placement = bo->placements;
222 bo->placement.busy_placement = bo->placements;
223}
224
Christian König7c204882015-12-14 13:18:01 +0100225/**
226 * amdgpu_bo_create_kernel - create BO for kernel use
227 *
228 * @adev: amdgpu device object
229 * @size: size for the new BO
230 * @align: alignment for the new BO
231 * @domain: where to place it
232 * @bo_ptr: resulting BO
233 * @gpu_addr: GPU addr of the pinned BO
234 * @cpu_addr: optional CPU address mapping
235 *
236 * Allocates and pins a BO for kernel internal use.
237 *
238 * Returns 0 on success, negative error code otherwise.
239 */
240int amdgpu_bo_create_kernel(struct amdgpu_device *adev,
241 unsigned long size, int align,
242 u32 domain, struct amdgpu_bo **bo_ptr,
243 u64 *gpu_addr, void **cpu_addr)
244{
245 int r;
246
247 r = amdgpu_bo_create(adev, size, align, true, domain,
248 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED,
249 NULL, NULL, bo_ptr);
250 if (r) {
251 dev_err(adev->dev, "(%d) failed to allocate kernel bo\n", r);
252 return r;
253 }
254
255 r = amdgpu_bo_reserve(*bo_ptr, false);
256 if (r) {
257 dev_err(adev->dev, "(%d) failed to reserve kernel bo\n", r);
258 goto error_free;
259 }
260
261 r = amdgpu_bo_pin(*bo_ptr, domain, gpu_addr);
262 if (r) {
263 dev_err(adev->dev, "(%d) kernel bo pin failed\n", r);
264 goto error_unreserve;
265 }
266
267 if (cpu_addr) {
268 r = amdgpu_bo_kmap(*bo_ptr, cpu_addr);
269 if (r) {
270 dev_err(adev->dev, "(%d) kernel bo map failed\n", r);
271 goto error_unreserve;
272 }
273 }
274
275 amdgpu_bo_unreserve(*bo_ptr);
276
277 return 0;
278
279error_unreserve:
280 amdgpu_bo_unreserve(*bo_ptr);
281
282error_free:
283 amdgpu_bo_unref(bo_ptr);
284
285 return r;
286}
287
Junwei Zhangaa1d5622016-09-08 10:13:32 +0800288/**
289 * amdgpu_bo_free_kernel - free BO for kernel use
290 *
291 * @bo: amdgpu BO to free
292 *
293 * unmaps and unpin a BO for kernel internal use.
294 */
295void amdgpu_bo_free_kernel(struct amdgpu_bo **bo, u64 *gpu_addr,
296 void **cpu_addr)
297{
298 if (*bo == NULL)
299 return;
300
301 if (likely(amdgpu_bo_reserve(*bo, false) == 0)) {
302 if (cpu_addr)
303 amdgpu_bo_kunmap(*bo);
304
305 amdgpu_bo_unpin(*bo);
306 amdgpu_bo_unreserve(*bo);
307 }
308 amdgpu_bo_unref(bo);
309
310 if (gpu_addr)
311 *gpu_addr = 0;
312
313 if (cpu_addr)
314 *cpu_addr = NULL;
315}
316
Chunming Zhou7e5a5472015-04-24 17:37:30 +0800317int amdgpu_bo_create_restricted(struct amdgpu_device *adev,
318 unsigned long size, int byte_align,
319 bool kernel, u32 domain, u64 flags,
320 struct sg_table *sg,
321 struct ttm_placement *placement,
Christian König72d76682015-09-03 17:34:59 +0200322 struct reservation_object *resv,
Chunming Zhou7e5a5472015-04-24 17:37:30 +0800323 struct amdgpu_bo **bo_ptr)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400324{
325 struct amdgpu_bo *bo;
326 enum ttm_bo_type type;
327 unsigned long page_align;
328 size_t acc_size;
329 int r;
330
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400331 page_align = roundup(byte_align, PAGE_SIZE) >> PAGE_SHIFT;
332 size = ALIGN(size, PAGE_SIZE);
333
334 if (kernel) {
335 type = ttm_bo_type_kernel;
336 } else if (sg) {
337 type = ttm_bo_type_sg;
338 } else {
339 type = ttm_bo_type_device;
340 }
341 *bo_ptr = NULL;
342
343 acc_size = ttm_bo_dma_acc_size(&adev->mman.bdev, size,
344 sizeof(struct amdgpu_bo));
345
346 bo = kzalloc(sizeof(struct amdgpu_bo), GFP_KERNEL);
347 if (bo == NULL)
348 return -ENOMEM;
349 r = drm_gem_object_init(adev->ddev, &bo->gem_base, size);
350 if (unlikely(r)) {
351 kfree(bo);
352 return r;
353 }
354 bo->adev = adev;
355 INIT_LIST_HEAD(&bo->list);
Chunming Zhou0c4e7fa2016-08-17 11:41:30 +0800356 INIT_LIST_HEAD(&bo->shadow_list);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400357 INIT_LIST_HEAD(&bo->va);
Christian König1ea863f2015-12-18 22:13:12 +0100358 bo->prefered_domains = domain & (AMDGPU_GEM_DOMAIN_VRAM |
359 AMDGPU_GEM_DOMAIN_GTT |
360 AMDGPU_GEM_DOMAIN_CPU |
361 AMDGPU_GEM_DOMAIN_GDS |
362 AMDGPU_GEM_DOMAIN_GWS |
363 AMDGPU_GEM_DOMAIN_OA);
364 bo->allowed_domains = bo->prefered_domains;
365 if (!kernel && bo->allowed_domains == AMDGPU_GEM_DOMAIN_VRAM)
366 bo->allowed_domains |= AMDGPU_GEM_DOMAIN_GTT;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400367
368 bo->flags = flags;
Oded Gabbaya187f172016-01-30 07:59:34 +0200369
370 /* For architectures that don't support WC memory,
371 * mask out the WC flag from the BO
372 */
373 if (!drm_arch_can_wc_memory())
374 bo->flags &= ~AMDGPU_GEM_CREATE_CPU_GTT_USWC;
375
Chunming Zhou7e5a5472015-04-24 17:37:30 +0800376 amdgpu_fill_placement_to_bo(bo, placement);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400377 /* Kernel allocation are uninterruptible */
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400378 r = ttm_bo_init(&adev->mman.bdev, &bo->tbo, size, type,
379 &bo->placement, page_align, !kernel, NULL,
Christian König72d76682015-09-03 17:34:59 +0200380 acc_size, sg, resv, &amdgpu_ttm_bo_destroy);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400381 if (unlikely(r != 0)) {
382 return r;
383 }
Flora Cui4fea83f2016-07-20 14:44:38 +0800384
385 if (flags & AMDGPU_GEM_CREATE_VRAM_CLEARED &&
386 bo->tbo.mem.placement & TTM_PL_FLAG_VRAM) {
387 struct fence *fence;
388
389 if (adev->mman.buffer_funcs_ring == NULL ||
390 !adev->mman.buffer_funcs_ring->ready) {
391 r = -EBUSY;
392 goto fail_free;
393 }
394
395 r = amdgpu_bo_reserve(bo, false);
396 if (unlikely(r != 0))
397 goto fail_free;
398
399 amdgpu_ttm_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_VRAM);
400 r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false);
401 if (unlikely(r != 0))
402 goto fail_unreserve;
403
404 amdgpu_fill_buffer(bo, 0, bo->tbo.resv, &fence);
405 amdgpu_bo_fence(bo, fence, false);
406 amdgpu_bo_unreserve(bo);
407 fence_put(bo->tbo.moving);
408 bo->tbo.moving = fence_get(fence);
409 fence_put(fence);
410 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400411 *bo_ptr = bo;
412
413 trace_amdgpu_bo_create(bo);
414
415 return 0;
Flora Cui4fea83f2016-07-20 14:44:38 +0800416
417fail_unreserve:
418 amdgpu_bo_unreserve(bo);
419fail_free:
420 amdgpu_bo_unref(&bo);
421 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400422}
423
Chunming Zhoue7893c42016-07-26 14:13:21 +0800424static int amdgpu_bo_create_shadow(struct amdgpu_device *adev,
425 unsigned long size, int byte_align,
426 struct amdgpu_bo *bo)
427{
428 struct ttm_placement placement = {0};
429 struct ttm_place placements[AMDGPU_GEM_DOMAIN_MAX + 1];
430 int r;
431
432 if (bo->shadow)
433 return 0;
434
435 bo->flags |= AMDGPU_GEM_CREATE_SHADOW;
436 memset(&placements, 0,
437 (AMDGPU_GEM_DOMAIN_MAX + 1) * sizeof(struct ttm_place));
438
439 amdgpu_ttm_placement_init(adev, &placement,
440 placements, AMDGPU_GEM_DOMAIN_GTT,
441 AMDGPU_GEM_CREATE_CPU_GTT_USWC);
442
443 r = amdgpu_bo_create_restricted(adev, size, byte_align, true,
444 AMDGPU_GEM_DOMAIN_GTT,
445 AMDGPU_GEM_CREATE_CPU_GTT_USWC,
446 NULL, &placement,
447 bo->tbo.resv,
448 &bo->shadow);
Chunming Zhou0c4e7fa2016-08-17 11:41:30 +0800449 if (!r) {
Chunming Zhoue7893c42016-07-26 14:13:21 +0800450 bo->shadow->parent = amdgpu_bo_ref(bo);
Chunming Zhou0c4e7fa2016-08-17 11:41:30 +0800451 mutex_lock(&adev->shadow_list_lock);
452 list_add_tail(&bo->shadow_list, &adev->shadow_list);
453 mutex_unlock(&adev->shadow_list_lock);
454 }
Chunming Zhoue7893c42016-07-26 14:13:21 +0800455
456 return r;
457}
458
Chunming Zhou7e5a5472015-04-24 17:37:30 +0800459int amdgpu_bo_create(struct amdgpu_device *adev,
460 unsigned long size, int byte_align,
461 bool kernel, u32 domain, u64 flags,
Christian König72d76682015-09-03 17:34:59 +0200462 struct sg_table *sg,
463 struct reservation_object *resv,
464 struct amdgpu_bo **bo_ptr)
Chunming Zhou7e5a5472015-04-24 17:37:30 +0800465{
466 struct ttm_placement placement = {0};
467 struct ttm_place placements[AMDGPU_GEM_DOMAIN_MAX + 1];
Chunming Zhoue7893c42016-07-26 14:13:21 +0800468 int r;
Chunming Zhou7e5a5472015-04-24 17:37:30 +0800469
470 memset(&placements, 0,
471 (AMDGPU_GEM_DOMAIN_MAX + 1) * sizeof(struct ttm_place));
472
473 amdgpu_ttm_placement_init(adev, &placement,
474 placements, domain, flags);
475
Chunming Zhoue7893c42016-07-26 14:13:21 +0800476 r = amdgpu_bo_create_restricted(adev, size, byte_align, kernel,
477 domain, flags, sg, &placement,
478 resv, bo_ptr);
479 if (r)
480 return r;
481
Chunming Zhou3ad81f12016-08-05 17:30:17 +0800482 if (amdgpu_need_backup(adev) && (flags & AMDGPU_GEM_CREATE_SHADOW)) {
Chunming Zhoue7893c42016-07-26 14:13:21 +0800483 r = amdgpu_bo_create_shadow(adev, size, byte_align, (*bo_ptr));
484 if (r)
485 amdgpu_bo_unref(bo_ptr);
486 }
487
488 return r;
Chunming Zhou7e5a5472015-04-24 17:37:30 +0800489}
490
Chunming Zhou20f4eff2016-08-04 16:51:18 +0800491int amdgpu_bo_backup_to_shadow(struct amdgpu_device *adev,
492 struct amdgpu_ring *ring,
493 struct amdgpu_bo *bo,
494 struct reservation_object *resv,
495 struct fence **fence,
496 bool direct)
497
498{
499 struct amdgpu_bo *shadow = bo->shadow;
500 uint64_t bo_addr, shadow_addr;
501 int r;
502
503 if (!shadow)
504 return -EINVAL;
505
506 bo_addr = amdgpu_bo_gpu_offset(bo);
507 shadow_addr = amdgpu_bo_gpu_offset(bo->shadow);
508
509 r = reservation_object_reserve_shared(bo->tbo.resv);
510 if (r)
511 goto err;
512
513 r = amdgpu_copy_buffer(ring, bo_addr, shadow_addr,
514 amdgpu_bo_size(bo), resv, fence,
515 direct);
516 if (!r)
517 amdgpu_bo_fence(bo, *fence, true);
518
519err:
520 return r;
521}
522
523int amdgpu_bo_restore_from_shadow(struct amdgpu_device *adev,
524 struct amdgpu_ring *ring,
525 struct amdgpu_bo *bo,
526 struct reservation_object *resv,
527 struct fence **fence,
528 bool direct)
529
530{
531 struct amdgpu_bo *shadow = bo->shadow;
532 uint64_t bo_addr, shadow_addr;
533 int r;
534
535 if (!shadow)
536 return -EINVAL;
537
538 bo_addr = amdgpu_bo_gpu_offset(bo);
539 shadow_addr = amdgpu_bo_gpu_offset(bo->shadow);
540
541 r = reservation_object_reserve_shared(bo->tbo.resv);
542 if (r)
543 goto err;
544
545 r = amdgpu_copy_buffer(ring, shadow_addr, bo_addr,
546 amdgpu_bo_size(bo), resv, fence,
547 direct);
548 if (!r)
549 amdgpu_bo_fence(bo, *fence, true);
550
551err:
552 return r;
553}
554
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400555int amdgpu_bo_kmap(struct amdgpu_bo *bo, void **ptr)
556{
557 bool is_iomem;
Christian König587f3c72016-03-10 16:21:04 +0100558 long r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400559
Christian König271c8122015-05-13 14:30:53 +0200560 if (bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)
561 return -EPERM;
562
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400563 if (bo->kptr) {
564 if (ptr) {
565 *ptr = bo->kptr;
566 }
567 return 0;
568 }
Christian König587f3c72016-03-10 16:21:04 +0100569
570 r = reservation_object_wait_timeout_rcu(bo->tbo.resv, false, false,
571 MAX_SCHEDULE_TIMEOUT);
572 if (r < 0)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400573 return r;
Christian König587f3c72016-03-10 16:21:04 +0100574
575 r = ttm_bo_kmap(&bo->tbo, 0, bo->tbo.num_pages, &bo->kmap);
576 if (r)
577 return r;
578
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400579 bo->kptr = ttm_kmap_obj_virtual(&bo->kmap, &is_iomem);
Christian König587f3c72016-03-10 16:21:04 +0100580 if (ptr)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400581 *ptr = bo->kptr;
Christian König587f3c72016-03-10 16:21:04 +0100582
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400583 return 0;
584}
585
586void amdgpu_bo_kunmap(struct amdgpu_bo *bo)
587{
588 if (bo->kptr == NULL)
589 return;
590 bo->kptr = NULL;
591 ttm_bo_kunmap(&bo->kmap);
592}
593
594struct amdgpu_bo *amdgpu_bo_ref(struct amdgpu_bo *bo)
595{
596 if (bo == NULL)
597 return NULL;
598
599 ttm_bo_reference(&bo->tbo);
600 return bo;
601}
602
603void amdgpu_bo_unref(struct amdgpu_bo **bo)
604{
605 struct ttm_buffer_object *tbo;
606
607 if ((*bo) == NULL)
608 return;
609
610 tbo = &((*bo)->tbo);
611 ttm_bo_unref(&tbo);
612 if (tbo == NULL)
613 *bo = NULL;
614}
615
Chunming Zhou7e5a5472015-04-24 17:37:30 +0800616int amdgpu_bo_pin_restricted(struct amdgpu_bo *bo, u32 domain,
617 u64 min_offset, u64 max_offset,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400618 u64 *gpu_addr)
619{
620 int r, i;
Chunming Zhou7e5a5472015-04-24 17:37:30 +0800621 unsigned fpfn, lpfn;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400622
Christian Königcc325d12016-02-08 11:08:35 +0100623 if (amdgpu_ttm_tt_get_usermm(bo->tbo.ttm))
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400624 return -EPERM;
625
Chunming Zhou7e5a5472015-04-24 17:37:30 +0800626 if (WARN_ON_ONCE(min_offset > max_offset))
627 return -EINVAL;
628
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400629 if (bo->pin_count) {
Flora Cui408778e2016-08-18 12:55:13 +0800630 uint32_t mem_type = bo->tbo.mem.mem_type;
631
632 if (domain != amdgpu_mem_type_to_domain(mem_type))
633 return -EINVAL;
634
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400635 bo->pin_count++;
636 if (gpu_addr)
637 *gpu_addr = amdgpu_bo_gpu_offset(bo);
638
639 if (max_offset != 0) {
Flora Cui27798e02016-08-18 13:18:09 +0800640 u64 domain_start = bo->tbo.bdev->man[mem_type].gpu_offset;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400641 WARN_ON_ONCE(max_offset <
642 (amdgpu_bo_gpu_offset(bo) - domain_start));
643 }
644
645 return 0;
646 }
647 amdgpu_ttm_placement_from_domain(bo, domain);
648 for (i = 0; i < bo->placement.num_placement; i++) {
649 /* force to pin into visible video ram */
650 if ((bo->placements[i].flags & TTM_PL_FLAG_VRAM) &&
Chunming Zhou7e5a5472015-04-24 17:37:30 +0800651 !(bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS) &&
Christian König6681c5e2016-08-12 16:50:12 +0200652 (!max_offset || max_offset >
653 bo->adev->mc.visible_vram_size)) {
Chunming Zhou7e5a5472015-04-24 17:37:30 +0800654 if (WARN_ON_ONCE(min_offset >
655 bo->adev->mc.visible_vram_size))
656 return -EINVAL;
657 fpfn = min_offset >> PAGE_SHIFT;
658 lpfn = bo->adev->mc.visible_vram_size >> PAGE_SHIFT;
659 } else {
660 fpfn = min_offset >> PAGE_SHIFT;
661 lpfn = max_offset >> PAGE_SHIFT;
662 }
663 if (fpfn > bo->placements[i].fpfn)
664 bo->placements[i].fpfn = fpfn;
Christian König78d0e182016-01-19 12:48:14 +0100665 if (!bo->placements[i].lpfn ||
666 (lpfn && lpfn < bo->placements[i].lpfn))
Chunming Zhou7e5a5472015-04-24 17:37:30 +0800667 bo->placements[i].lpfn = lpfn;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400668 bo->placements[i].flags |= TTM_PL_FLAG_NO_EVICT;
669 }
670
671 r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false);
Christian König6681c5e2016-08-12 16:50:12 +0200672 if (unlikely(r)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400673 dev_err(bo->adev->dev, "%p pin failed\n", bo);
Christian König6681c5e2016-08-12 16:50:12 +0200674 goto error;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400675 }
Christian Königc855e252016-09-05 17:00:57 +0200676 r = amdgpu_ttm_bind(bo->tbo.ttm, &bo->tbo.mem);
677 if (unlikely(r)) {
678 dev_err(bo->adev->dev, "%p bind failed\n", bo);
679 goto error;
680 }
Christian König6681c5e2016-08-12 16:50:12 +0200681
682 bo->pin_count = 1;
683 if (gpu_addr != NULL)
684 *gpu_addr = amdgpu_bo_gpu_offset(bo);
685 if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
686 bo->adev->vram_pin_size += amdgpu_bo_size(bo);
687 if (bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)
688 bo->adev->invisible_pin_size += amdgpu_bo_size(bo);
Flora Cui32ab75f2016-08-18 13:17:07 +0800689 } else if (domain == AMDGPU_GEM_DOMAIN_GTT) {
Christian König6681c5e2016-08-12 16:50:12 +0200690 bo->adev->gart_pin_size += amdgpu_bo_size(bo);
691 }
692
693error:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400694 return r;
695}
696
697int amdgpu_bo_pin(struct amdgpu_bo *bo, u32 domain, u64 *gpu_addr)
698{
Chunming Zhou7e5a5472015-04-24 17:37:30 +0800699 return amdgpu_bo_pin_restricted(bo, domain, 0, 0, gpu_addr);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400700}
701
702int amdgpu_bo_unpin(struct amdgpu_bo *bo)
703{
704 int r, i;
705
706 if (!bo->pin_count) {
707 dev_warn(bo->adev->dev, "%p unpin not necessary\n", bo);
708 return 0;
709 }
710 bo->pin_count--;
711 if (bo->pin_count)
712 return 0;
713 for (i = 0; i < bo->placement.num_placement; i++) {
714 bo->placements[i].lpfn = 0;
715 bo->placements[i].flags &= ~TTM_PL_FLAG_NO_EVICT;
716 }
717 r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false);
Christian König6681c5e2016-08-12 16:50:12 +0200718 if (unlikely(r)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400719 dev_err(bo->adev->dev, "%p validate failed for unpin\n", bo);
Christian König6681c5e2016-08-12 16:50:12 +0200720 goto error;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400721 }
Christian König6681c5e2016-08-12 16:50:12 +0200722
723 if (bo->tbo.mem.mem_type == TTM_PL_VRAM) {
724 bo->adev->vram_pin_size -= amdgpu_bo_size(bo);
725 if (bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)
726 bo->adev->invisible_pin_size -= amdgpu_bo_size(bo);
Flora Cui441f90e2016-09-09 14:15:30 +0800727 } else if (bo->tbo.mem.mem_type == TTM_PL_TT) {
Christian König6681c5e2016-08-12 16:50:12 +0200728 bo->adev->gart_pin_size -= amdgpu_bo_size(bo);
729 }
730
731error:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400732 return r;
733}
734
735int amdgpu_bo_evict_vram(struct amdgpu_device *adev)
736{
737 /* late 2.6.33 fix IGP hibernate - we need pm ops to do this correct */
Jammy Zhou2f7d10b2015-07-22 11:29:01 +0800738 if (0 && (adev->flags & AMD_IS_APU)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400739 /* Useless to evict on IGP chips */
740 return 0;
741 }
742 return ttm_bo_evict_mm(&adev->mman.bdev, TTM_PL_VRAM);
743}
744
Alex Deucher1f8628c2016-03-31 16:56:22 -0400745static const char *amdgpu_vram_names[] = {
746 "UNKNOWN",
747 "GDDR1",
748 "DDR2",
749 "GDDR3",
750 "GDDR4",
751 "GDDR5",
752 "HBM",
753 "DDR3"
754};
755
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400756int amdgpu_bo_init(struct amdgpu_device *adev)
757{
758 /* Add an MTRR for the VRAM */
759 adev->mc.vram_mtrr = arch_phys_wc_add(adev->mc.aper_base,
760 adev->mc.aper_size);
761 DRM_INFO("Detected VRAM RAM=%lluM, BAR=%lluM\n",
762 adev->mc.mc_vram_size >> 20,
763 (unsigned long long)adev->mc.aper_size >> 20);
Alex Deucher1f8628c2016-03-31 16:56:22 -0400764 DRM_INFO("RAM width %dbits %s\n",
765 adev->mc.vram_width, amdgpu_vram_names[adev->mc.vram_type]);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400766 return amdgpu_ttm_init(adev);
767}
768
769void amdgpu_bo_fini(struct amdgpu_device *adev)
770{
771 amdgpu_ttm_fini(adev);
772 arch_phys_wc_del(adev->mc.vram_mtrr);
773}
774
775int amdgpu_bo_fbdev_mmap(struct amdgpu_bo *bo,
776 struct vm_area_struct *vma)
777{
778 return ttm_fbdev_mmap(vma, &bo->tbo);
779}
780
781int amdgpu_bo_set_tiling_flags(struct amdgpu_bo *bo, u64 tiling_flags)
782{
Marek Olšákfbd76d52015-05-14 23:48:26 +0200783 if (AMDGPU_TILING_GET(tiling_flags, TILE_SPLIT) > 6)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400784 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400785
786 bo->tiling_flags = tiling_flags;
787 return 0;
788}
789
790void amdgpu_bo_get_tiling_flags(struct amdgpu_bo *bo, u64 *tiling_flags)
791{
792 lockdep_assert_held(&bo->tbo.resv->lock.base);
793
794 if (tiling_flags)
795 *tiling_flags = bo->tiling_flags;
796}
797
798int amdgpu_bo_set_metadata (struct amdgpu_bo *bo, void *metadata,
799 uint32_t metadata_size, uint64_t flags)
800{
801 void *buffer;
802
803 if (!metadata_size) {
804 if (bo->metadata_size) {
805 kfree(bo->metadata);
Dave Airlie0092d3e2016-05-03 12:44:29 +1000806 bo->metadata = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400807 bo->metadata_size = 0;
808 }
809 return 0;
810 }
811
812 if (metadata == NULL)
813 return -EINVAL;
814
Andrzej Hajda71affda2015-09-21 17:34:39 -0400815 buffer = kmemdup(metadata, metadata_size, GFP_KERNEL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400816 if (buffer == NULL)
817 return -ENOMEM;
818
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400819 kfree(bo->metadata);
820 bo->metadata_flags = flags;
821 bo->metadata = buffer;
822 bo->metadata_size = metadata_size;
823
824 return 0;
825}
826
827int amdgpu_bo_get_metadata(struct amdgpu_bo *bo, void *buffer,
828 size_t buffer_size, uint32_t *metadata_size,
829 uint64_t *flags)
830{
831 if (!buffer && !metadata_size)
832 return -EINVAL;
833
834 if (buffer) {
835 if (buffer_size < bo->metadata_size)
836 return -EINVAL;
837
838 if (bo->metadata_size)
839 memcpy(buffer, bo->metadata, bo->metadata_size);
840 }
841
842 if (metadata_size)
843 *metadata_size = bo->metadata_size;
844 if (flags)
845 *flags = bo->metadata_flags;
846
847 return 0;
848}
849
850void amdgpu_bo_move_notify(struct ttm_buffer_object *bo,
851 struct ttm_mem_reg *new_mem)
852{
853 struct amdgpu_bo *rbo;
David Mao15da3012016-06-07 17:48:52 +0800854 struct ttm_mem_reg *old_mem = &bo->mem;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400855
856 if (!amdgpu_ttm_bo_is_amdgpu_bo(bo))
857 return;
858
859 rbo = container_of(bo, struct amdgpu_bo, tbo);
860 amdgpu_vm_bo_invalidate(rbo->adev, rbo);
861
862 /* update statistics */
863 if (!new_mem)
864 return;
865
866 /* move_notify is called before move happens */
867 amdgpu_update_memory_usage(rbo->adev, &bo->mem, new_mem);
David Mao15da3012016-06-07 17:48:52 +0800868
869 trace_amdgpu_ttm_bo_move(rbo, new_mem->mem_type, old_mem->mem_type);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400870}
871
872int amdgpu_bo_fault_reserve_notify(struct ttm_buffer_object *bo)
873{
874 struct amdgpu_device *adev;
Christian König5fb19412015-05-21 17:03:46 +0200875 struct amdgpu_bo *abo;
876 unsigned long offset, size, lpfn;
877 int i, r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400878
879 if (!amdgpu_ttm_bo_is_amdgpu_bo(bo))
880 return 0;
Christian König5fb19412015-05-21 17:03:46 +0200881
882 abo = container_of(bo, struct amdgpu_bo, tbo);
883 adev = abo->adev;
884 if (bo->mem.mem_type != TTM_PL_VRAM)
885 return 0;
886
887 size = bo->mem.num_pages << PAGE_SHIFT;
888 offset = bo->mem.start << PAGE_SHIFT;
889 if ((offset + size) <= adev->mc.visible_vram_size)
890 return 0;
891
Michel Dänzer104ece92016-03-28 12:53:02 +0900892 /* Can't move a pinned BO to visible VRAM */
893 if (abo->pin_count > 0)
894 return -EINVAL;
895
Christian König5fb19412015-05-21 17:03:46 +0200896 /* hurrah the memory is not visible ! */
897 amdgpu_ttm_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_VRAM);
898 lpfn = adev->mc.visible_vram_size >> PAGE_SHIFT;
899 for (i = 0; i < abo->placement.num_placement; i++) {
900 /* Force into visible VRAM */
901 if ((abo->placements[i].flags & TTM_PL_FLAG_VRAM) &&
Christian König6681c5e2016-08-12 16:50:12 +0200902 (!abo->placements[i].lpfn ||
903 abo->placements[i].lpfn > lpfn))
Christian König5fb19412015-05-21 17:03:46 +0200904 abo->placements[i].lpfn = lpfn;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400905 }
Christian König5fb19412015-05-21 17:03:46 +0200906 r = ttm_bo_validate(bo, &abo->placement, false, false);
907 if (unlikely(r == -ENOMEM)) {
908 amdgpu_ttm_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_GTT);
909 return ttm_bo_validate(bo, &abo->placement, false, false);
910 } else if (unlikely(r != 0)) {
911 return r;
912 }
913
914 offset = bo->mem.start << PAGE_SHIFT;
915 /* this should never happen */
916 if ((offset + size) > adev->mc.visible_vram_size)
917 return -EINVAL;
918
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400919 return 0;
920}
921
922/**
923 * amdgpu_bo_fence - add fence to buffer object
924 *
925 * @bo: buffer object in question
926 * @fence: fence to add
927 * @shared: true if fence should be added shared
928 *
929 */
Chunming Zhoue40a3112015-08-03 11:38:09 +0800930void amdgpu_bo_fence(struct amdgpu_bo *bo, struct fence *fence,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400931 bool shared)
932{
933 struct reservation_object *resv = bo->tbo.resv;
934
935 if (shared)
Chunming Zhoue40a3112015-08-03 11:38:09 +0800936 reservation_object_add_shared_fence(resv, fence);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400937 else
Chunming Zhoue40a3112015-08-03 11:38:09 +0800938 reservation_object_add_excl_fence(resv, fence);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400939}
Christian Königcdb7e8f2016-07-25 17:56:18 +0200940
941/**
942 * amdgpu_bo_gpu_offset - return GPU offset of bo
943 * @bo: amdgpu object for which we query the offset
944 *
945 * Returns current GPU offset of the object.
946 *
947 * Note: object should either be pinned or reserved when calling this
948 * function, it might be useful to add check for this for debugging.
949 */
950u64 amdgpu_bo_gpu_offset(struct amdgpu_bo *bo)
951{
952 WARN_ON_ONCE(bo->tbo.mem.mem_type == TTM_PL_SYSTEM);
Christian Königc855e252016-09-05 17:00:57 +0200953 WARN_ON_ONCE(bo->tbo.mem.mem_type == TTM_PL_TT &&
954 !amdgpu_ttm_is_bound(bo->tbo.ttm));
Christian Königcdb7e8f2016-07-25 17:56:18 +0200955 WARN_ON_ONCE(!ww_mutex_is_locked(&bo->tbo.resv->lock) &&
956 !bo->pin_count);
Christian König9702d402016-09-07 15:10:44 +0200957 WARN_ON_ONCE(bo->tbo.mem.start == AMDGPU_BO_INVALID_OFFSET);
Christian Königcdb7e8f2016-07-25 17:56:18 +0200958
959 return bo->tbo.offset;
960}