aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/gpu/drm/amd/amdgpu/amdgpu_object.c
diff options
context:
space:
mode:
authorDave Airlie <airlied@redhat.com>2015-06-04 21:14:18 -0400
committerDave Airlie <airlied@redhat.com>2015-06-04 21:14:18 -0400
commita93fe8f8b444499119261914b8870d7f5ac173f8 (patch)
treebe15a66542177a9d863fd61c9e46deaece1134f7 /drivers/gpu/drm/amd/amdgpu/amdgpu_object.c
parentef1316961048988c6d044a298b5842c8288cc35b (diff)
parent3ccec53c294cbec2af44b6b24f70349637c45428 (diff)
Merge branch 'drm-next-4.2-amdgpu' of git://people.freedesktop.org/~agd5f/linux into drm-next
This is the big pull request for amdgpu, the new driver for VI+ AMD asics. I currently supports Tonga, Iceland, and Carrizo and also contains a Kconfig option to build support for CI parts for testing. All major functionality is supported (displays, gfx, compute, dma, video decode/encode, etc.). Power management is working on Carrizo, but is still being worked on for Tonga and Iceland. * 'drm-next-4.2-amdgpu' of git://people.freedesktop.org/~agd5f/linux: (106 commits) drm/amdgpu: only support IBs in the buffer list (v2) drm/amdgpu: add vram_type and vram_bit_width for interface query (v2) drm/amdgpu: add ib_size/start_alignment interface query drm/amdgpu: add me/ce/pfp_feature_version interface query drm/amdgpu add ce_ram_size for interface query drm/amdgpu add max_memory_clock for interface query (v2) drm/amdgpu: add hdp flush for gfx8 compute ring drm/amdgpu: fix no hdp flush for compute ring drm/amdgpu: add HEVC/H.265 UVD support drm/amdgpu: stop loading firmware with pm.mutex locked drm/amdgpu: remove mclk_lock drm/amdgpu: fix description of vm_size module parameter (v2) drm/amdgpu: remove all sh mem register modification in vm flush drm/amdgpu: rename GEM_OP_SET_INITIAL_DOMAIN -> GEM_OP_SET_PLACEMENT drm/amdgpu: fence should be added to shared slot drm/amdgpu: sync fence of clear_invalids (v2) drm/amdgpu: max_pde_used usage should be under protect drm/amdgpu: fix bug of vm_bo_map (v2) drm/amdgpu: implement the allocation range (v3) drm/amdgpu: rename amdgpu_ip_funcs to amd_ip_funcs (v2) ...
Diffstat (limited to 'drivers/gpu/drm/amd/amdgpu/amdgpu_object.c')
-rw-r--r--drivers/gpu/drm/amd/amdgpu/amdgpu_object.c671
1 files changed, 671 insertions, 0 deletions
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_object.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_object.c
new file mode 100644
index 000000000000..7d801e016e31
--- /dev/null
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_object.c
@@ -0,0 +1,671 @@
1/*
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>
36#include "amdgpu.h"
37#include "amdgpu_trace.h"
38
39
40int amdgpu_ttm_init(struct amdgpu_device *adev);
41void amdgpu_ttm_fini(struct amdgpu_device *adev);
42
43static u64 amdgpu_get_vis_part_size(struct amdgpu_device *adev,
44 struct ttm_mem_reg *mem)
45{
46 u64 ret = 0;
47 if (mem->start << PAGE_SHIFT < adev->mc.visible_vram_size) {
48 ret = (u64)((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;
52 }
53 return ret;
54}
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);
98 amdgpu_mn_unregister(bo);
99
100 mutex_lock(&bo->adev->gem.mutex);
101 list_del_init(&bo->list);
102 mutex_unlock(&bo->adev->gem.mutex);
103 drm_gem_object_release(&bo->gem_base);
104 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
115static void amdgpu_ttm_placement_init(struct amdgpu_device *adev,
116 struct ttm_placement *placement,
117 struct ttm_place *placements,
118 u32 domain, u64 flags)
119{
120 u32 c = 0, i;
121
122 placement->placement = placements;
123 placement->busy_placement = placements;
124
125 if (domain & AMDGPU_GEM_DOMAIN_VRAM) {
126 if (flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS &&
127 adev->mc.visible_vram_size < adev->mc.real_vram_size) {
128 placements[c].fpfn =
129 adev->mc.visible_vram_size >> PAGE_SHIFT;
130 placements[c++].flags = TTM_PL_FLAG_WC | TTM_PL_FLAG_UNCACHED |
131 TTM_PL_FLAG_VRAM;
132 }
133 placements[c].fpfn = 0;
134 placements[c++].flags = TTM_PL_FLAG_WC | TTM_PL_FLAG_UNCACHED |
135 TTM_PL_FLAG_VRAM;
136 }
137
138 if (domain & AMDGPU_GEM_DOMAIN_GTT) {
139 if (flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC) {
140 placements[c].fpfn = 0;
141 placements[c++].flags = TTM_PL_FLAG_WC | TTM_PL_FLAG_TT |
142 TTM_PL_FLAG_UNCACHED;
143 } else {
144 placements[c].fpfn = 0;
145 placements[c++].flags = TTM_PL_FLAG_CACHED | TTM_PL_FLAG_TT;
146 }
147 }
148
149 if (domain & AMDGPU_GEM_DOMAIN_CPU) {
150 if (flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC) {
151 placements[c].fpfn = 0;
152 placements[c++].flags = TTM_PL_FLAG_WC | TTM_PL_FLAG_SYSTEM |
153 TTM_PL_FLAG_UNCACHED;
154 } else {
155 placements[c].fpfn = 0;
156 placements[c++].flags = TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM;
157 }
158 }
159
160 if (domain & AMDGPU_GEM_DOMAIN_GDS) {
161 placements[c].fpfn = 0;
162 placements[c++].flags = TTM_PL_FLAG_UNCACHED |
163 AMDGPU_PL_FLAG_GDS;
164 }
165 if (domain & AMDGPU_GEM_DOMAIN_GWS) {
166 placements[c].fpfn = 0;
167 placements[c++].flags = TTM_PL_FLAG_UNCACHED |
168 AMDGPU_PL_FLAG_GWS;
169 }
170 if (domain & AMDGPU_GEM_DOMAIN_OA) {
171 placements[c].fpfn = 0;
172 placements[c++].flags = TTM_PL_FLAG_UNCACHED |
173 AMDGPU_PL_FLAG_OA;
174 }
175
176 if (!c) {
177 placements[c].fpfn = 0;
178 placements[c++].flags = TTM_PL_MASK_CACHING |
179 TTM_PL_FLAG_SYSTEM;
180 }
181 placement->num_placement = c;
182 placement->num_busy_placement = c;
183
184 for (i = 0; i < c; i++) {
185 if ((flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED) &&
186 (placements[i].flags & TTM_PL_FLAG_VRAM) &&
187 !placements[i].fpfn)
188 placements[i].lpfn =
189 adev->mc.visible_vram_size >> PAGE_SHIFT;
190 else
191 placements[i].lpfn = 0;
192 }
193}
194
195void amdgpu_ttm_placement_from_domain(struct amdgpu_bo *rbo, u32 domain)
196{
197 amdgpu_ttm_placement_init(rbo->adev, &rbo->placement,
198 rbo->placements, domain, rbo->flags);
199}
200
201static void amdgpu_fill_placement_to_bo(struct amdgpu_bo *bo,
202 struct ttm_placement *placement)
203{
204 BUG_ON(placement->num_placement > (AMDGPU_GEM_DOMAIN_MAX + 1));
205
206 memcpy(bo->placements, placement->placement,
207 placement->num_placement * sizeof(struct ttm_place));
208 bo->placement.num_placement = placement->num_placement;
209 bo->placement.num_busy_placement = placement->num_busy_placement;
210 bo->placement.placement = bo->placements;
211 bo->placement.busy_placement = bo->placements;
212}
213
214int amdgpu_bo_create_restricted(struct amdgpu_device *adev,
215 unsigned long size, int byte_align,
216 bool kernel, u32 domain, u64 flags,
217 struct sg_table *sg,
218 struct ttm_placement *placement,
219 struct amdgpu_bo **bo_ptr)
220{
221 struct amdgpu_bo *bo;
222 enum ttm_bo_type type;
223 unsigned long page_align;
224 size_t acc_size;
225 int r;
226
227 /* VI has a hw bug where VM PTEs have to be allocated in groups of 8.
228 * do this as a temporary workaround
229 */
230 if (!(domain & (AMDGPU_GEM_DOMAIN_GDS | AMDGPU_GEM_DOMAIN_GWS | AMDGPU_GEM_DOMAIN_OA))) {
231 if (adev->asic_type >= CHIP_TOPAZ) {
232 if (byte_align & 0x7fff)
233 byte_align = ALIGN(byte_align, 0x8000);
234 if (size & 0x7fff)
235 size = ALIGN(size, 0x8000);
236 }
237 }
238
239 page_align = roundup(byte_align, PAGE_SIZE) >> PAGE_SHIFT;
240 size = ALIGN(size, PAGE_SIZE);
241
242 if (kernel) {
243 type = ttm_bo_type_kernel;
244 } else if (sg) {
245 type = ttm_bo_type_sg;
246 } else {
247 type = ttm_bo_type_device;
248 }
249 *bo_ptr = NULL;
250
251 acc_size = ttm_bo_dma_acc_size(&adev->mman.bdev, size,
252 sizeof(struct amdgpu_bo));
253
254 bo = kzalloc(sizeof(struct amdgpu_bo), GFP_KERNEL);
255 if (bo == NULL)
256 return -ENOMEM;
257 r = drm_gem_object_init(adev->ddev, &bo->gem_base, size);
258 if (unlikely(r)) {
259 kfree(bo);
260 return r;
261 }
262 bo->adev = adev;
263 INIT_LIST_HEAD(&bo->list);
264 INIT_LIST_HEAD(&bo->va);
265 bo->initial_domain = domain & (AMDGPU_GEM_DOMAIN_VRAM |
266 AMDGPU_GEM_DOMAIN_GTT |
267 AMDGPU_GEM_DOMAIN_CPU |
268 AMDGPU_GEM_DOMAIN_GDS |
269 AMDGPU_GEM_DOMAIN_GWS |
270 AMDGPU_GEM_DOMAIN_OA);
271
272 bo->flags = flags;
273 amdgpu_fill_placement_to_bo(bo, placement);
274 /* Kernel allocation are uninterruptible */
275 r = ttm_bo_init(&adev->mman.bdev, &bo->tbo, size, type,
276 &bo->placement, page_align, !kernel, NULL,
277 acc_size, sg, NULL, &amdgpu_ttm_bo_destroy);
278 if (unlikely(r != 0)) {
279 return r;
280 }
281 *bo_ptr = bo;
282
283 trace_amdgpu_bo_create(bo);
284
285 return 0;
286}
287
288int amdgpu_bo_create(struct amdgpu_device *adev,
289 unsigned long size, int byte_align,
290 bool kernel, u32 domain, u64 flags,
291 struct sg_table *sg, struct amdgpu_bo **bo_ptr)
292{
293 struct ttm_placement placement = {0};
294 struct ttm_place placements[AMDGPU_GEM_DOMAIN_MAX + 1];
295
296 memset(&placements, 0,
297 (AMDGPU_GEM_DOMAIN_MAX + 1) * sizeof(struct ttm_place));
298
299 amdgpu_ttm_placement_init(adev, &placement,
300 placements, domain, flags);
301
302 return amdgpu_bo_create_restricted(adev, size, byte_align,
303 kernel, domain, flags,
304 sg,
305 &placement,
306 bo_ptr);
307}
308
309int amdgpu_bo_kmap(struct amdgpu_bo *bo, void **ptr)
310{
311 bool is_iomem;
312 int r;
313
314 if (bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)
315 return -EPERM;
316
317 if (bo->kptr) {
318 if (ptr) {
319 *ptr = bo->kptr;
320 }
321 return 0;
322 }
323 r = ttm_bo_kmap(&bo->tbo, 0, bo->tbo.num_pages, &bo->kmap);
324 if (r) {
325 return r;
326 }
327 bo->kptr = ttm_kmap_obj_virtual(&bo->kmap, &is_iomem);
328 if (ptr) {
329 *ptr = bo->kptr;
330 }
331 return 0;
332}
333
334void amdgpu_bo_kunmap(struct amdgpu_bo *bo)
335{
336 if (bo->kptr == NULL)
337 return;
338 bo->kptr = NULL;
339 ttm_bo_kunmap(&bo->kmap);
340}
341
342struct amdgpu_bo *amdgpu_bo_ref(struct amdgpu_bo *bo)
343{
344 if (bo == NULL)
345 return NULL;
346
347 ttm_bo_reference(&bo->tbo);
348 return bo;
349}
350
351void amdgpu_bo_unref(struct amdgpu_bo **bo)
352{
353 struct ttm_buffer_object *tbo;
354
355 if ((*bo) == NULL)
356 return;
357
358 tbo = &((*bo)->tbo);
359 ttm_bo_unref(&tbo);
360 if (tbo == NULL)
361 *bo = NULL;
362}
363
364int amdgpu_bo_pin_restricted(struct amdgpu_bo *bo, u32 domain,
365 u64 min_offset, u64 max_offset,
366 u64 *gpu_addr)
367{
368 int r, i;
369 unsigned fpfn, lpfn;
370
371 if (amdgpu_ttm_tt_has_userptr(bo->tbo.ttm))
372 return -EPERM;
373
374 if (WARN_ON_ONCE(min_offset > max_offset))
375 return -EINVAL;
376
377 if (bo->pin_count) {
378 bo->pin_count++;
379 if (gpu_addr)
380 *gpu_addr = amdgpu_bo_gpu_offset(bo);
381
382 if (max_offset != 0) {
383 u64 domain_start;
384 if (domain == AMDGPU_GEM_DOMAIN_VRAM)
385 domain_start = bo->adev->mc.vram_start;
386 else
387 domain_start = bo->adev->mc.gtt_start;
388 WARN_ON_ONCE(max_offset <
389 (amdgpu_bo_gpu_offset(bo) - domain_start));
390 }
391
392 return 0;
393 }
394 amdgpu_ttm_placement_from_domain(bo, domain);
395 for (i = 0; i < bo->placement.num_placement; i++) {
396 /* force to pin into visible video ram */
397 if ((bo->placements[i].flags & TTM_PL_FLAG_VRAM) &&
398 !(bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS) &&
399 (!max_offset || max_offset > bo->adev->mc.visible_vram_size)) {
400 if (WARN_ON_ONCE(min_offset >
401 bo->adev->mc.visible_vram_size))
402 return -EINVAL;
403 fpfn = min_offset >> PAGE_SHIFT;
404 lpfn = bo->adev->mc.visible_vram_size >> PAGE_SHIFT;
405 } else {
406 fpfn = min_offset >> PAGE_SHIFT;
407 lpfn = max_offset >> PAGE_SHIFT;
408 }
409 if (fpfn > bo->placements[i].fpfn)
410 bo->placements[i].fpfn = fpfn;
411 if (lpfn && lpfn < bo->placements[i].lpfn)
412 bo->placements[i].lpfn = lpfn;
413 bo->placements[i].flags |= TTM_PL_FLAG_NO_EVICT;
414 }
415
416 r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false);
417 if (likely(r == 0)) {
418 bo->pin_count = 1;
419 if (gpu_addr != NULL)
420 *gpu_addr = amdgpu_bo_gpu_offset(bo);
421 if (domain == AMDGPU_GEM_DOMAIN_VRAM)
422 bo->adev->vram_pin_size += amdgpu_bo_size(bo);
423 else
424 bo->adev->gart_pin_size += amdgpu_bo_size(bo);
425 } else {
426 dev_err(bo->adev->dev, "%p pin failed\n", bo);
427 }
428 return r;
429}
430
431int amdgpu_bo_pin(struct amdgpu_bo *bo, u32 domain, u64 *gpu_addr)
432{
433 return amdgpu_bo_pin_restricted(bo, domain, 0, 0, gpu_addr);
434}
435
436int amdgpu_bo_unpin(struct amdgpu_bo *bo)
437{
438 int r, i;
439
440 if (!bo->pin_count) {
441 dev_warn(bo->adev->dev, "%p unpin not necessary\n", bo);
442 return 0;
443 }
444 bo->pin_count--;
445 if (bo->pin_count)
446 return 0;
447 for (i = 0; i < bo->placement.num_placement; i++) {
448 bo->placements[i].lpfn = 0;
449 bo->placements[i].flags &= ~TTM_PL_FLAG_NO_EVICT;
450 }
451 r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false);
452 if (likely(r == 0)) {
453 if (bo->tbo.mem.mem_type == TTM_PL_VRAM)
454 bo->adev->vram_pin_size -= amdgpu_bo_size(bo);
455 else
456 bo->adev->gart_pin_size -= amdgpu_bo_size(bo);
457 } else {
458 dev_err(bo->adev->dev, "%p validate failed for unpin\n", bo);
459 }
460 return r;
461}
462
463int amdgpu_bo_evict_vram(struct amdgpu_device *adev)
464{
465 /* late 2.6.33 fix IGP hibernate - we need pm ops to do this correct */
466 if (0 && (adev->flags & AMDGPU_IS_APU)) {
467 /* Useless to evict on IGP chips */
468 return 0;
469 }
470 return ttm_bo_evict_mm(&adev->mman.bdev, TTM_PL_VRAM);
471}
472
473void amdgpu_bo_force_delete(struct amdgpu_device *adev)
474{
475 struct amdgpu_bo *bo, *n;
476
477 if (list_empty(&adev->gem.objects)) {
478 return;
479 }
480 dev_err(adev->dev, "Userspace still has active objects !\n");
481 list_for_each_entry_safe(bo, n, &adev->gem.objects, list) {
482 mutex_lock(&adev->ddev->struct_mutex);
483 dev_err(adev->dev, "%p %p %lu %lu force free\n",
484 &bo->gem_base, bo, (unsigned long)bo->gem_base.size,
485 *((unsigned long *)&bo->gem_base.refcount));
486 mutex_lock(&bo->adev->gem.mutex);
487 list_del_init(&bo->list);
488 mutex_unlock(&bo->adev->gem.mutex);
489 /* this should unref the ttm bo */
490 drm_gem_object_unreference(&bo->gem_base);
491 mutex_unlock(&adev->ddev->struct_mutex);
492 }
493}
494
495int amdgpu_bo_init(struct amdgpu_device *adev)
496{
497 /* Add an MTRR for the VRAM */
498 adev->mc.vram_mtrr = arch_phys_wc_add(adev->mc.aper_base,
499 adev->mc.aper_size);
500 DRM_INFO("Detected VRAM RAM=%lluM, BAR=%lluM\n",
501 adev->mc.mc_vram_size >> 20,
502 (unsigned long long)adev->mc.aper_size >> 20);
503 DRM_INFO("RAM width %dbits DDR\n",
504 adev->mc.vram_width);
505 return amdgpu_ttm_init(adev);
506}
507
508void amdgpu_bo_fini(struct amdgpu_device *adev)
509{
510 amdgpu_ttm_fini(adev);
511 arch_phys_wc_del(adev->mc.vram_mtrr);
512}
513
514int amdgpu_bo_fbdev_mmap(struct amdgpu_bo *bo,
515 struct vm_area_struct *vma)
516{
517 return ttm_fbdev_mmap(vma, &bo->tbo);
518}
519
520int amdgpu_bo_set_tiling_flags(struct amdgpu_bo *bo, u64 tiling_flags)
521{
522 if (AMDGPU_TILING_GET(tiling_flags, TILE_SPLIT) > 6)
523 return -EINVAL;
524
525 bo->tiling_flags = tiling_flags;
526 return 0;
527}
528
529void amdgpu_bo_get_tiling_flags(struct amdgpu_bo *bo, u64 *tiling_flags)
530{
531 lockdep_assert_held(&bo->tbo.resv->lock.base);
532
533 if (tiling_flags)
534 *tiling_flags = bo->tiling_flags;
535}
536
537int amdgpu_bo_set_metadata (struct amdgpu_bo *bo, void *metadata,
538 uint32_t metadata_size, uint64_t flags)
539{
540 void *buffer;
541
542 if (!metadata_size) {
543 if (bo->metadata_size) {
544 kfree(bo->metadata);
545 bo->metadata_size = 0;
546 }
547 return 0;
548 }
549
550 if (metadata == NULL)
551 return -EINVAL;
552
553 buffer = kzalloc(metadata_size, GFP_KERNEL);
554 if (buffer == NULL)
555 return -ENOMEM;
556
557 memcpy(buffer, metadata, metadata_size);
558
559 kfree(bo->metadata);
560 bo->metadata_flags = flags;
561 bo->metadata = buffer;
562 bo->metadata_size = metadata_size;
563
564 return 0;
565}
566
567int amdgpu_bo_get_metadata(struct amdgpu_bo *bo, void *buffer,
568 size_t buffer_size, uint32_t *metadata_size,
569 uint64_t *flags)
570{
571 if (!buffer && !metadata_size)
572 return -EINVAL;
573
574 if (buffer) {
575 if (buffer_size < bo->metadata_size)
576 return -EINVAL;
577
578 if (bo->metadata_size)
579 memcpy(buffer, bo->metadata, bo->metadata_size);
580 }
581
582 if (metadata_size)
583 *metadata_size = bo->metadata_size;
584 if (flags)
585 *flags = bo->metadata_flags;
586
587 return 0;
588}
589
590void amdgpu_bo_move_notify(struct ttm_buffer_object *bo,
591 struct ttm_mem_reg *new_mem)
592{
593 struct amdgpu_bo *rbo;
594
595 if (!amdgpu_ttm_bo_is_amdgpu_bo(bo))
596 return;
597
598 rbo = container_of(bo, struct amdgpu_bo, tbo);
599 amdgpu_vm_bo_invalidate(rbo->adev, rbo);
600
601 /* update statistics */
602 if (!new_mem)
603 return;
604
605 /* move_notify is called before move happens */
606 amdgpu_update_memory_usage(rbo->adev, &bo->mem, new_mem);
607}
608
609int amdgpu_bo_fault_reserve_notify(struct ttm_buffer_object *bo)
610{
611 struct amdgpu_device *adev;
612 struct amdgpu_bo *abo;
613 unsigned long offset, size, lpfn;
614 int i, r;
615
616 if (!amdgpu_ttm_bo_is_amdgpu_bo(bo))
617 return 0;
618
619 abo = container_of(bo, struct amdgpu_bo, tbo);
620 adev = abo->adev;
621 if (bo->mem.mem_type != TTM_PL_VRAM)
622 return 0;
623
624 size = bo->mem.num_pages << PAGE_SHIFT;
625 offset = bo->mem.start << PAGE_SHIFT;
626 if ((offset + size) <= adev->mc.visible_vram_size)
627 return 0;
628
629 /* hurrah the memory is not visible ! */
630 amdgpu_ttm_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_VRAM);
631 lpfn = adev->mc.visible_vram_size >> PAGE_SHIFT;
632 for (i = 0; i < abo->placement.num_placement; i++) {
633 /* Force into visible VRAM */
634 if ((abo->placements[i].flags & TTM_PL_FLAG_VRAM) &&
635 (!abo->placements[i].lpfn || abo->placements[i].lpfn > lpfn))
636 abo->placements[i].lpfn = lpfn;
637 }
638 r = ttm_bo_validate(bo, &abo->placement, false, false);
639 if (unlikely(r == -ENOMEM)) {
640 amdgpu_ttm_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_GTT);
641 return ttm_bo_validate(bo, &abo->placement, false, false);
642 } else if (unlikely(r != 0)) {
643 return r;
644 }
645
646 offset = bo->mem.start << PAGE_SHIFT;
647 /* this should never happen */
648 if ((offset + size) > adev->mc.visible_vram_size)
649 return -EINVAL;
650
651 return 0;
652}
653
654/**
655 * amdgpu_bo_fence - add fence to buffer object
656 *
657 * @bo: buffer object in question
658 * @fence: fence to add
659 * @shared: true if fence should be added shared
660 *
661 */
662void amdgpu_bo_fence(struct amdgpu_bo *bo, struct amdgpu_fence *fence,
663 bool shared)
664{
665 struct reservation_object *resv = bo->tbo.resv;
666
667 if (shared)
668 reservation_object_add_shared_fence(resv, &fence->base);
669 else
670 reservation_object_add_excl_fence(resv, &fence->base);
671}