Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2009 VMware, Inc. |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | * copy of this software and associated documentation files (the "Software"), |
| 6 | * to deal in the Software without restriction, including without limitation |
| 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | * Software is furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice shall be included in |
| 12 | * all copies or substantial portions of the Software. |
| 13 | * |
| 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 17 | * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 18 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 19 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 20 | * OTHER DEALINGS IN THE SOFTWARE. |
| 21 | * |
| 22 | * Authors: Michel Dänzer |
| 23 | */ |
| 24 | #include <drm/drmP.h> |
| 25 | #include <drm/amdgpu_drm.h> |
| 26 | #include "amdgpu.h" |
| 27 | #include "amdgpu_uvd.h" |
| 28 | #include "amdgpu_vce.h" |
| 29 | |
| 30 | /* Test BO GTT->VRAM and VRAM->GTT GPU copies across the whole GTT aperture */ |
| 31 | static void amdgpu_do_test_moves(struct amdgpu_device *adev) |
| 32 | { |
| 33 | struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring; |
| 34 | struct amdgpu_bo *vram_obj = NULL; |
| 35 | struct amdgpu_bo **gtt_obj = NULL; |
Chunming Zhou | 3216c6b | 2018-04-16 18:27:50 +0800 | [diff] [blame] | 36 | struct amdgpu_bo_param bp; |
Christian König | 6f02a69 | 2017-07-07 11:56:59 +0200 | [diff] [blame] | 37 | uint64_t gart_addr, vram_addr; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 38 | unsigned n, size; |
| 39 | int i, r; |
| 40 | |
| 41 | size = 1024 * 1024; |
| 42 | |
| 43 | /* Number of tests = |
| 44 | * (Total GTT - IB pool - writeback page - ring buffers) / test size |
| 45 | */ |
Christian König | 770d13b | 2018-01-12 14:52:22 +0100 | [diff] [blame] | 46 | n = adev->gmc.gart_size - AMDGPU_IB_POOL_SIZE*64*1024; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 47 | for (i = 0; i < AMDGPU_MAX_RINGS; ++i) |
| 48 | if (adev->rings[i]) |
| 49 | n -= adev->rings[i]->ring_size; |
| 50 | if (adev->wb.wb_obj) |
| 51 | n -= AMDGPU_GPU_PAGE_SIZE; |
| 52 | if (adev->irq.ih.ring_obj) |
| 53 | n -= adev->irq.ih.ring_size; |
| 54 | n /= size; |
| 55 | |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 56 | gtt_obj = kcalloc(n, sizeof(*gtt_obj), GFP_KERNEL); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 57 | if (!gtt_obj) { |
| 58 | DRM_ERROR("Failed to allocate %d pointers\n", n); |
| 59 | r = 1; |
| 60 | goto out_cleanup; |
| 61 | } |
Chunming Zhou | 3216c6b | 2018-04-16 18:27:50 +0800 | [diff] [blame] | 62 | memset(&bp, 0, sizeof(bp)); |
| 63 | bp.size = size; |
| 64 | bp.byte_align = PAGE_SIZE; |
| 65 | bp.domain = AMDGPU_GEM_DOMAIN_VRAM; |
| 66 | bp.flags = 0; |
| 67 | bp.type = ttm_bo_type_kernel; |
| 68 | bp.resv = NULL; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 69 | |
Chunming Zhou | 3216c6b | 2018-04-16 18:27:50 +0800 | [diff] [blame] | 70 | r = amdgpu_bo_create(adev, &bp, &vram_obj); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 71 | if (r) { |
| 72 | DRM_ERROR("Failed to create VRAM object\n"); |
| 73 | goto out_cleanup; |
| 74 | } |
| 75 | r = amdgpu_bo_reserve(vram_obj, false); |
| 76 | if (unlikely(r != 0)) |
| 77 | goto out_unref; |
| 78 | r = amdgpu_bo_pin(vram_obj, AMDGPU_GEM_DOMAIN_VRAM, &vram_addr); |
| 79 | if (r) { |
| 80 | DRM_ERROR("Failed to pin VRAM object\n"); |
| 81 | goto out_unres; |
| 82 | } |
| 83 | for (i = 0; i < n; i++) { |
| 84 | void *gtt_map, *vram_map; |
Christian König | 6f02a69 | 2017-07-07 11:56:59 +0200 | [diff] [blame] | 85 | void **gart_start, **gart_end; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 86 | void **vram_start, **vram_end; |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 87 | struct dma_fence *fence = NULL; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 88 | |
Chunming Zhou | 3216c6b | 2018-04-16 18:27:50 +0800 | [diff] [blame] | 89 | bp.domain = AMDGPU_GEM_DOMAIN_GTT; |
| 90 | r = amdgpu_bo_create(adev, &bp, gtt_obj + i); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 91 | if (r) { |
| 92 | DRM_ERROR("Failed to create GTT object %d\n", i); |
| 93 | goto out_lclean; |
| 94 | } |
| 95 | |
| 96 | r = amdgpu_bo_reserve(gtt_obj[i], false); |
| 97 | if (unlikely(r != 0)) |
| 98 | goto out_lclean_unref; |
Christian König | 6f02a69 | 2017-07-07 11:56:59 +0200 | [diff] [blame] | 99 | r = amdgpu_bo_pin(gtt_obj[i], AMDGPU_GEM_DOMAIN_GTT, &gart_addr); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 100 | if (r) { |
| 101 | DRM_ERROR("Failed to pin GTT object %d\n", i); |
| 102 | goto out_lclean_unres; |
| 103 | } |
| 104 | |
| 105 | r = amdgpu_bo_kmap(gtt_obj[i], >t_map); |
| 106 | if (r) { |
| 107 | DRM_ERROR("Failed to map GTT object %d\n", i); |
| 108 | goto out_lclean_unpin; |
| 109 | } |
| 110 | |
Christian König | 6f02a69 | 2017-07-07 11:56:59 +0200 | [diff] [blame] | 111 | for (gart_start = gtt_map, gart_end = gtt_map + size; |
| 112 | gart_start < gart_end; |
| 113 | gart_start++) |
| 114 | *gart_start = gart_start; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 115 | |
| 116 | amdgpu_bo_kunmap(gtt_obj[i]); |
| 117 | |
Christian König | 6f02a69 | 2017-07-07 11:56:59 +0200 | [diff] [blame] | 118 | r = amdgpu_copy_buffer(ring, gart_addr, vram_addr, |
Christian König | fc9c8f5 | 2017-06-29 11:46:15 +0200 | [diff] [blame] | 119 | size, NULL, &fence, false, false); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 120 | |
| 121 | if (r) { |
| 122 | DRM_ERROR("Failed GTT->VRAM copy %d\n", i); |
| 123 | goto out_lclean_unpin; |
| 124 | } |
| 125 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 126 | r = dma_fence_wait(fence, false); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 127 | if (r) { |
| 128 | DRM_ERROR("Failed to wait for GTT->VRAM fence %d\n", i); |
| 129 | goto out_lclean_unpin; |
| 130 | } |
| 131 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 132 | dma_fence_put(fence); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 133 | |
| 134 | r = amdgpu_bo_kmap(vram_obj, &vram_map); |
| 135 | if (r) { |
| 136 | DRM_ERROR("Failed to map VRAM object after copy %d\n", i); |
| 137 | goto out_lclean_unpin; |
| 138 | } |
| 139 | |
Christian König | 6f02a69 | 2017-07-07 11:56:59 +0200 | [diff] [blame] | 140 | for (gart_start = gtt_map, gart_end = gtt_map + size, |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 141 | vram_start = vram_map, vram_end = vram_map + size; |
| 142 | vram_start < vram_end; |
Christian König | 6f02a69 | 2017-07-07 11:56:59 +0200 | [diff] [blame] | 143 | gart_start++, vram_start++) { |
| 144 | if (*vram_start != gart_start) { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 145 | DRM_ERROR("Incorrect GTT->VRAM copy %d: Got 0x%p, " |
| 146 | "expected 0x%p (GTT/VRAM offset " |
| 147 | "0x%16llx/0x%16llx)\n", |
Christian König | 6f02a69 | 2017-07-07 11:56:59 +0200 | [diff] [blame] | 148 | i, *vram_start, gart_start, |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 149 | (unsigned long long) |
Christian König | 770d13b | 2018-01-12 14:52:22 +0100 | [diff] [blame] | 150 | (gart_addr - adev->gmc.gart_start + |
Christian König | 6f02a69 | 2017-07-07 11:56:59 +0200 | [diff] [blame] | 151 | (void*)gart_start - gtt_map), |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 152 | (unsigned long long) |
Christian König | 770d13b | 2018-01-12 14:52:22 +0100 | [diff] [blame] | 153 | (vram_addr - adev->gmc.vram_start + |
Christian König | 6f02a69 | 2017-07-07 11:56:59 +0200 | [diff] [blame] | 154 | (void*)gart_start - gtt_map)); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 155 | amdgpu_bo_kunmap(vram_obj); |
| 156 | goto out_lclean_unpin; |
| 157 | } |
| 158 | *vram_start = vram_start; |
| 159 | } |
| 160 | |
| 161 | amdgpu_bo_kunmap(vram_obj); |
| 162 | |
Christian König | 6f02a69 | 2017-07-07 11:56:59 +0200 | [diff] [blame] | 163 | r = amdgpu_copy_buffer(ring, vram_addr, gart_addr, |
Christian König | fc9c8f5 | 2017-06-29 11:46:15 +0200 | [diff] [blame] | 164 | size, NULL, &fence, false, false); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 165 | |
| 166 | if (r) { |
| 167 | DRM_ERROR("Failed VRAM->GTT copy %d\n", i); |
| 168 | goto out_lclean_unpin; |
| 169 | } |
| 170 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 171 | r = dma_fence_wait(fence, false); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 172 | if (r) { |
| 173 | DRM_ERROR("Failed to wait for VRAM->GTT fence %d\n", i); |
| 174 | goto out_lclean_unpin; |
| 175 | } |
| 176 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 177 | dma_fence_put(fence); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 178 | |
| 179 | r = amdgpu_bo_kmap(gtt_obj[i], >t_map); |
| 180 | if (r) { |
| 181 | DRM_ERROR("Failed to map GTT object after copy %d\n", i); |
| 182 | goto out_lclean_unpin; |
| 183 | } |
| 184 | |
Christian König | 6f02a69 | 2017-07-07 11:56:59 +0200 | [diff] [blame] | 185 | for (gart_start = gtt_map, gart_end = gtt_map + size, |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 186 | vram_start = vram_map, vram_end = vram_map + size; |
Christian König | 6f02a69 | 2017-07-07 11:56:59 +0200 | [diff] [blame] | 187 | gart_start < gart_end; |
| 188 | gart_start++, vram_start++) { |
| 189 | if (*gart_start != vram_start) { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 190 | DRM_ERROR("Incorrect VRAM->GTT copy %d: Got 0x%p, " |
| 191 | "expected 0x%p (VRAM/GTT offset " |
| 192 | "0x%16llx/0x%16llx)\n", |
Christian König | 6f02a69 | 2017-07-07 11:56:59 +0200 | [diff] [blame] | 193 | i, *gart_start, vram_start, |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 194 | (unsigned long long) |
Christian König | 770d13b | 2018-01-12 14:52:22 +0100 | [diff] [blame] | 195 | (vram_addr - adev->gmc.vram_start + |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 196 | (void*)vram_start - vram_map), |
| 197 | (unsigned long long) |
Christian König | 770d13b | 2018-01-12 14:52:22 +0100 | [diff] [blame] | 198 | (gart_addr - adev->gmc.gart_start + |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 199 | (void*)vram_start - vram_map)); |
| 200 | amdgpu_bo_kunmap(gtt_obj[i]); |
| 201 | goto out_lclean_unpin; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | amdgpu_bo_kunmap(gtt_obj[i]); |
| 206 | |
| 207 | DRM_INFO("Tested GTT->VRAM and VRAM->GTT copy for GTT offset 0x%llx\n", |
Christian König | 770d13b | 2018-01-12 14:52:22 +0100 | [diff] [blame] | 208 | gart_addr - adev->gmc.gart_start); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 209 | continue; |
| 210 | |
| 211 | out_lclean_unpin: |
| 212 | amdgpu_bo_unpin(gtt_obj[i]); |
| 213 | out_lclean_unres: |
| 214 | amdgpu_bo_unreserve(gtt_obj[i]); |
| 215 | out_lclean_unref: |
| 216 | amdgpu_bo_unref(>t_obj[i]); |
| 217 | out_lclean: |
| 218 | for (--i; i >= 0; --i) { |
| 219 | amdgpu_bo_unpin(gtt_obj[i]); |
| 220 | amdgpu_bo_unreserve(gtt_obj[i]); |
| 221 | amdgpu_bo_unref(>t_obj[i]); |
| 222 | } |
| 223 | if (fence) |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 224 | dma_fence_put(fence); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 225 | break; |
| 226 | } |
| 227 | |
| 228 | amdgpu_bo_unpin(vram_obj); |
| 229 | out_unres: |
| 230 | amdgpu_bo_unreserve(vram_obj); |
| 231 | out_unref: |
| 232 | amdgpu_bo_unref(&vram_obj); |
| 233 | out_cleanup: |
| 234 | kfree(gtt_obj); |
| 235 | if (r) { |
Joe Perches | 7ca8529 | 2017-02-28 04:55:52 -0800 | [diff] [blame] | 236 | pr_warn("Error while testing BO move\n"); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 237 | } |
| 238 | } |
| 239 | |
| 240 | void amdgpu_test_moves(struct amdgpu_device *adev) |
| 241 | { |
| 242 | if (adev->mman.buffer_funcs) |
| 243 | amdgpu_do_test_moves(adev); |
| 244 | } |