1d722e3fbSopenharmony_ci/* 2d722e3fbSopenharmony_ci * Copyright 2014 Advanced Micro Devices, Inc. 3d722e3fbSopenharmony_ci * 4d722e3fbSopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 5d722e3fbSopenharmony_ci * copy of this software and associated documentation files (the "Software"), 6d722e3fbSopenharmony_ci * to deal in the Software without restriction, including without limitation 7d722e3fbSopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8d722e3fbSopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the 9d722e3fbSopenharmony_ci * Software is furnished to do so, subject to the following conditions: 10d722e3fbSopenharmony_ci * 11d722e3fbSopenharmony_ci * The above copyright notice and this permission notice shall be included in 12d722e3fbSopenharmony_ci * all copies or substantial portions of the Software. 13d722e3fbSopenharmony_ci * 14d722e3fbSopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15d722e3fbSopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16d722e3fbSopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17d722e3fbSopenharmony_ci * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 18d722e3fbSopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 19d722e3fbSopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 20d722e3fbSopenharmony_ci * OTHER DEALINGS IN THE SOFTWARE. 21d722e3fbSopenharmony_ci * 22d722e3fbSopenharmony_ci*/ 23d722e3fbSopenharmony_ci 24d722e3fbSopenharmony_ci#include <stdio.h> 25d722e3fbSopenharmony_ci 26d722e3fbSopenharmony_ci#include "CUnit/Basic.h" 27d722e3fbSopenharmony_ci 28d722e3fbSopenharmony_ci#include "amdgpu_test.h" 29d722e3fbSopenharmony_ci#include "amdgpu_drm.h" 30d722e3fbSopenharmony_ci#include "amdgpu_internal.h" 31d722e3fbSopenharmony_ci 32d722e3fbSopenharmony_ci#define BUFFER_SIZE (4*1024) 33d722e3fbSopenharmony_ci#define BUFFER_ALIGN (4*1024) 34d722e3fbSopenharmony_ci 35d722e3fbSopenharmony_cistatic amdgpu_device_handle device_handle; 36d722e3fbSopenharmony_cistatic uint32_t major_version; 37d722e3fbSopenharmony_cistatic uint32_t minor_version; 38d722e3fbSopenharmony_ci 39d722e3fbSopenharmony_cistatic amdgpu_bo_handle buffer_handle; 40d722e3fbSopenharmony_cistatic uint64_t virtual_mc_base_address; 41d722e3fbSopenharmony_cistatic amdgpu_va_handle va_handle; 42d722e3fbSopenharmony_ci 43d722e3fbSopenharmony_cistatic void amdgpu_bo_export_import(void); 44d722e3fbSopenharmony_cistatic void amdgpu_bo_metadata(void); 45d722e3fbSopenharmony_cistatic void amdgpu_bo_map_unmap(void); 46d722e3fbSopenharmony_cistatic void amdgpu_memory_alloc(void); 47d722e3fbSopenharmony_cistatic void amdgpu_mem_fail_alloc(void); 48d722e3fbSopenharmony_cistatic void amdgpu_bo_find_by_cpu_mapping(void); 49d722e3fbSopenharmony_ci 50d722e3fbSopenharmony_ciCU_TestInfo bo_tests[] = { 51d722e3fbSopenharmony_ci { "Export/Import", amdgpu_bo_export_import }, 52d722e3fbSopenharmony_ci { "Metadata", amdgpu_bo_metadata }, 53d722e3fbSopenharmony_ci { "CPU map/unmap", amdgpu_bo_map_unmap }, 54d722e3fbSopenharmony_ci { "Memory alloc Test", amdgpu_memory_alloc }, 55d722e3fbSopenharmony_ci { "Memory fail alloc Test", amdgpu_mem_fail_alloc }, 56d722e3fbSopenharmony_ci { "Find bo by CPU mapping", amdgpu_bo_find_by_cpu_mapping }, 57d722e3fbSopenharmony_ci CU_TEST_INFO_NULL, 58d722e3fbSopenharmony_ci}; 59d722e3fbSopenharmony_ci 60d722e3fbSopenharmony_ciint suite_bo_tests_init(void) 61d722e3fbSopenharmony_ci{ 62d722e3fbSopenharmony_ci struct amdgpu_bo_alloc_request req = {0}; 63d722e3fbSopenharmony_ci amdgpu_bo_handle buf_handle; 64d722e3fbSopenharmony_ci uint64_t va; 65d722e3fbSopenharmony_ci int r; 66d722e3fbSopenharmony_ci 67d722e3fbSopenharmony_ci r = amdgpu_device_initialize(drm_amdgpu[0], &major_version, 68d722e3fbSopenharmony_ci &minor_version, &device_handle); 69d722e3fbSopenharmony_ci if (r) { 70d722e3fbSopenharmony_ci if ((r == -EACCES) && (errno == EACCES)) 71d722e3fbSopenharmony_ci printf("\n\nError:%s. " 72d722e3fbSopenharmony_ci "Hint:Try to run this test program as root.", 73d722e3fbSopenharmony_ci strerror(errno)); 74d722e3fbSopenharmony_ci 75d722e3fbSopenharmony_ci return CUE_SINIT_FAILED; 76d722e3fbSopenharmony_ci } 77d722e3fbSopenharmony_ci 78d722e3fbSopenharmony_ci req.alloc_size = BUFFER_SIZE; 79d722e3fbSopenharmony_ci req.phys_alignment = BUFFER_ALIGN; 80d722e3fbSopenharmony_ci req.preferred_heap = AMDGPU_GEM_DOMAIN_GTT; 81d722e3fbSopenharmony_ci 82d722e3fbSopenharmony_ci r = amdgpu_bo_alloc(device_handle, &req, &buf_handle); 83d722e3fbSopenharmony_ci if (r) 84d722e3fbSopenharmony_ci return CUE_SINIT_FAILED; 85d722e3fbSopenharmony_ci 86d722e3fbSopenharmony_ci r = amdgpu_va_range_alloc(device_handle, 87d722e3fbSopenharmony_ci amdgpu_gpu_va_range_general, 88d722e3fbSopenharmony_ci BUFFER_SIZE, BUFFER_ALIGN, 0, 89d722e3fbSopenharmony_ci &va, &va_handle, 0); 90d722e3fbSopenharmony_ci if (r) 91d722e3fbSopenharmony_ci goto error_va_alloc; 92d722e3fbSopenharmony_ci 93d722e3fbSopenharmony_ci r = amdgpu_bo_va_op(buf_handle, 0, BUFFER_SIZE, va, 0, AMDGPU_VA_OP_MAP); 94d722e3fbSopenharmony_ci if (r) 95d722e3fbSopenharmony_ci goto error_va_map; 96d722e3fbSopenharmony_ci 97d722e3fbSopenharmony_ci buffer_handle = buf_handle; 98d722e3fbSopenharmony_ci virtual_mc_base_address = va; 99d722e3fbSopenharmony_ci 100d722e3fbSopenharmony_ci return CUE_SUCCESS; 101d722e3fbSopenharmony_ci 102d722e3fbSopenharmony_cierror_va_map: 103d722e3fbSopenharmony_ci amdgpu_va_range_free(va_handle); 104d722e3fbSopenharmony_ci 105d722e3fbSopenharmony_cierror_va_alloc: 106d722e3fbSopenharmony_ci amdgpu_bo_free(buf_handle); 107d722e3fbSopenharmony_ci return CUE_SINIT_FAILED; 108d722e3fbSopenharmony_ci} 109d722e3fbSopenharmony_ci 110d722e3fbSopenharmony_ciint suite_bo_tests_clean(void) 111d722e3fbSopenharmony_ci{ 112d722e3fbSopenharmony_ci int r; 113d722e3fbSopenharmony_ci 114d722e3fbSopenharmony_ci r = amdgpu_bo_va_op(buffer_handle, 0, BUFFER_SIZE, 115d722e3fbSopenharmony_ci virtual_mc_base_address, 0, 116d722e3fbSopenharmony_ci AMDGPU_VA_OP_UNMAP); 117d722e3fbSopenharmony_ci if (r) 118d722e3fbSopenharmony_ci return CUE_SCLEAN_FAILED; 119d722e3fbSopenharmony_ci 120d722e3fbSopenharmony_ci r = amdgpu_va_range_free(va_handle); 121d722e3fbSopenharmony_ci if (r) 122d722e3fbSopenharmony_ci return CUE_SCLEAN_FAILED; 123d722e3fbSopenharmony_ci 124d722e3fbSopenharmony_ci r = amdgpu_bo_free(buffer_handle); 125d722e3fbSopenharmony_ci if (r) 126d722e3fbSopenharmony_ci return CUE_SCLEAN_FAILED; 127d722e3fbSopenharmony_ci 128d722e3fbSopenharmony_ci r = amdgpu_device_deinitialize(device_handle); 129d722e3fbSopenharmony_ci if (r) 130d722e3fbSopenharmony_ci return CUE_SCLEAN_FAILED; 131d722e3fbSopenharmony_ci 132d722e3fbSopenharmony_ci return CUE_SUCCESS; 133d722e3fbSopenharmony_ci} 134d722e3fbSopenharmony_ci 135d722e3fbSopenharmony_cistatic void amdgpu_bo_export_import_do_type(enum amdgpu_bo_handle_type type) 136d722e3fbSopenharmony_ci{ 137d722e3fbSopenharmony_ci struct amdgpu_bo_import_result res = {0}; 138d722e3fbSopenharmony_ci uint32_t shared_handle; 139d722e3fbSopenharmony_ci int r; 140d722e3fbSopenharmony_ci 141d722e3fbSopenharmony_ci r = amdgpu_bo_export(buffer_handle, type, &shared_handle); 142d722e3fbSopenharmony_ci CU_ASSERT_EQUAL(r, 0); 143d722e3fbSopenharmony_ci 144d722e3fbSopenharmony_ci r = amdgpu_bo_import(device_handle, type, shared_handle, &res); 145d722e3fbSopenharmony_ci CU_ASSERT_EQUAL(r, 0); 146d722e3fbSopenharmony_ci 147d722e3fbSopenharmony_ci CU_ASSERT_EQUAL(res.buf_handle, buffer_handle); 148d722e3fbSopenharmony_ci CU_ASSERT_EQUAL(res.alloc_size, BUFFER_SIZE); 149d722e3fbSopenharmony_ci 150d722e3fbSopenharmony_ci r = amdgpu_bo_free(res.buf_handle); 151d722e3fbSopenharmony_ci CU_ASSERT_EQUAL(r, 0); 152d722e3fbSopenharmony_ci} 153d722e3fbSopenharmony_ci 154d722e3fbSopenharmony_cistatic void amdgpu_bo_export_import(void) 155d722e3fbSopenharmony_ci{ 156d722e3fbSopenharmony_ci if (open_render_node) { 157d722e3fbSopenharmony_ci printf("(DRM render node is used. Skip export/Import test) "); 158d722e3fbSopenharmony_ci return; 159d722e3fbSopenharmony_ci } 160d722e3fbSopenharmony_ci 161d722e3fbSopenharmony_ci amdgpu_bo_export_import_do_type(amdgpu_bo_handle_type_gem_flink_name); 162d722e3fbSopenharmony_ci amdgpu_bo_export_import_do_type(amdgpu_bo_handle_type_dma_buf_fd); 163d722e3fbSopenharmony_ci} 164d722e3fbSopenharmony_ci 165d722e3fbSopenharmony_cistatic void amdgpu_bo_metadata(void) 166d722e3fbSopenharmony_ci{ 167d722e3fbSopenharmony_ci struct amdgpu_bo_metadata meta = {0}; 168d722e3fbSopenharmony_ci struct amdgpu_bo_info info = {0}; 169d722e3fbSopenharmony_ci int r; 170d722e3fbSopenharmony_ci 171d722e3fbSopenharmony_ci meta.size_metadata = 4; 172d722e3fbSopenharmony_ci meta.umd_metadata[0] = 0xdeadbeef; 173d722e3fbSopenharmony_ci 174d722e3fbSopenharmony_ci r = amdgpu_bo_set_metadata(buffer_handle, &meta); 175d722e3fbSopenharmony_ci CU_ASSERT_EQUAL(r, 0); 176d722e3fbSopenharmony_ci 177d722e3fbSopenharmony_ci r = amdgpu_bo_query_info(buffer_handle, &info); 178d722e3fbSopenharmony_ci CU_ASSERT_EQUAL(r, 0); 179d722e3fbSopenharmony_ci 180d722e3fbSopenharmony_ci CU_ASSERT_EQUAL(info.metadata.size_metadata, 4); 181d722e3fbSopenharmony_ci CU_ASSERT_EQUAL(info.metadata.umd_metadata[0], 0xdeadbeef); 182d722e3fbSopenharmony_ci} 183d722e3fbSopenharmony_ci 184d722e3fbSopenharmony_cistatic void amdgpu_bo_map_unmap(void) 185d722e3fbSopenharmony_ci{ 186d722e3fbSopenharmony_ci uint32_t *ptr; 187d722e3fbSopenharmony_ci int i, r; 188d722e3fbSopenharmony_ci 189d722e3fbSopenharmony_ci r = amdgpu_bo_cpu_map(buffer_handle, (void **)&ptr); 190d722e3fbSopenharmony_ci CU_ASSERT_EQUAL(r, 0); 191d722e3fbSopenharmony_ci CU_ASSERT_NOT_EQUAL(ptr, NULL); 192d722e3fbSopenharmony_ci 193d722e3fbSopenharmony_ci for (i = 0; i < (BUFFER_SIZE / 4); ++i) 194d722e3fbSopenharmony_ci ptr[i] = 0xdeadbeef; 195d722e3fbSopenharmony_ci 196d722e3fbSopenharmony_ci r = amdgpu_bo_cpu_unmap(buffer_handle); 197d722e3fbSopenharmony_ci CU_ASSERT_EQUAL(r, 0); 198d722e3fbSopenharmony_ci} 199d722e3fbSopenharmony_ci 200d722e3fbSopenharmony_cistatic void amdgpu_memory_alloc(void) 201d722e3fbSopenharmony_ci{ 202d722e3fbSopenharmony_ci amdgpu_bo_handle bo; 203d722e3fbSopenharmony_ci amdgpu_va_handle va_handle; 204d722e3fbSopenharmony_ci uint64_t bo_mc; 205d722e3fbSopenharmony_ci int r; 206d722e3fbSopenharmony_ci 207d722e3fbSopenharmony_ci /* Test visible VRAM */ 208d722e3fbSopenharmony_ci bo = gpu_mem_alloc(device_handle, 209d722e3fbSopenharmony_ci 4096, 4096, 210d722e3fbSopenharmony_ci AMDGPU_GEM_DOMAIN_VRAM, 211d722e3fbSopenharmony_ci AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED, 212d722e3fbSopenharmony_ci &bo_mc, &va_handle); 213d722e3fbSopenharmony_ci 214d722e3fbSopenharmony_ci r = gpu_mem_free(bo, va_handle, bo_mc, 4096); 215d722e3fbSopenharmony_ci CU_ASSERT_EQUAL(r, 0); 216d722e3fbSopenharmony_ci 217d722e3fbSopenharmony_ci /* Test invisible VRAM */ 218d722e3fbSopenharmony_ci bo = gpu_mem_alloc(device_handle, 219d722e3fbSopenharmony_ci 4096, 4096, 220d722e3fbSopenharmony_ci AMDGPU_GEM_DOMAIN_VRAM, 221d722e3fbSopenharmony_ci AMDGPU_GEM_CREATE_NO_CPU_ACCESS, 222d722e3fbSopenharmony_ci &bo_mc, &va_handle); 223d722e3fbSopenharmony_ci 224d722e3fbSopenharmony_ci r = gpu_mem_free(bo, va_handle, bo_mc, 4096); 225d722e3fbSopenharmony_ci CU_ASSERT_EQUAL(r, 0); 226d722e3fbSopenharmony_ci 227d722e3fbSopenharmony_ci /* Test GART Cacheable */ 228d722e3fbSopenharmony_ci bo = gpu_mem_alloc(device_handle, 229d722e3fbSopenharmony_ci 4096, 4096, 230d722e3fbSopenharmony_ci AMDGPU_GEM_DOMAIN_GTT, 231d722e3fbSopenharmony_ci 0, &bo_mc, &va_handle); 232d722e3fbSopenharmony_ci 233d722e3fbSopenharmony_ci r = gpu_mem_free(bo, va_handle, bo_mc, 4096); 234d722e3fbSopenharmony_ci CU_ASSERT_EQUAL(r, 0); 235d722e3fbSopenharmony_ci 236d722e3fbSopenharmony_ci /* Test GART USWC */ 237d722e3fbSopenharmony_ci bo = gpu_mem_alloc(device_handle, 238d722e3fbSopenharmony_ci 4096, 4096, 239d722e3fbSopenharmony_ci AMDGPU_GEM_DOMAIN_GTT, 240d722e3fbSopenharmony_ci AMDGPU_GEM_CREATE_CPU_GTT_USWC, 241d722e3fbSopenharmony_ci &bo_mc, &va_handle); 242d722e3fbSopenharmony_ci 243d722e3fbSopenharmony_ci r = gpu_mem_free(bo, va_handle, bo_mc, 4096); 244d722e3fbSopenharmony_ci CU_ASSERT_EQUAL(r, 0); 245d722e3fbSopenharmony_ci 246d722e3fbSopenharmony_ci /* Test GDS */ 247d722e3fbSopenharmony_ci bo = gpu_mem_alloc(device_handle, 1024, 0, 248d722e3fbSopenharmony_ci AMDGPU_GEM_DOMAIN_GDS, 0, 249d722e3fbSopenharmony_ci NULL, NULL); 250d722e3fbSopenharmony_ci r = gpu_mem_free(bo, NULL, 0, 4096); 251d722e3fbSopenharmony_ci CU_ASSERT_EQUAL(r, 0); 252d722e3fbSopenharmony_ci 253d722e3fbSopenharmony_ci /* Test GWS */ 254d722e3fbSopenharmony_ci bo = gpu_mem_alloc(device_handle, 1, 0, 255d722e3fbSopenharmony_ci AMDGPU_GEM_DOMAIN_GWS, 0, 256d722e3fbSopenharmony_ci NULL, NULL); 257d722e3fbSopenharmony_ci r = gpu_mem_free(bo, NULL, 0, 4096); 258d722e3fbSopenharmony_ci CU_ASSERT_EQUAL(r, 0); 259d722e3fbSopenharmony_ci 260d722e3fbSopenharmony_ci /* Test OA */ 261d722e3fbSopenharmony_ci bo = gpu_mem_alloc(device_handle, 1, 0, 262d722e3fbSopenharmony_ci AMDGPU_GEM_DOMAIN_OA, 0, 263d722e3fbSopenharmony_ci NULL, NULL); 264d722e3fbSopenharmony_ci r = gpu_mem_free(bo, NULL, 0, 4096); 265d722e3fbSopenharmony_ci CU_ASSERT_EQUAL(r, 0); 266d722e3fbSopenharmony_ci} 267d722e3fbSopenharmony_ci 268d722e3fbSopenharmony_cistatic void amdgpu_mem_fail_alloc(void) 269d722e3fbSopenharmony_ci{ 270d722e3fbSopenharmony_ci int r; 271d722e3fbSopenharmony_ci struct amdgpu_bo_alloc_request req = {0}; 272d722e3fbSopenharmony_ci amdgpu_bo_handle buf_handle; 273d722e3fbSopenharmony_ci 274d722e3fbSopenharmony_ci /* Test impossible mem allocation, 1TB */ 275d722e3fbSopenharmony_ci req.alloc_size = 0xE8D4A51000; 276d722e3fbSopenharmony_ci req.phys_alignment = 4096; 277d722e3fbSopenharmony_ci req.preferred_heap = AMDGPU_GEM_DOMAIN_VRAM; 278d722e3fbSopenharmony_ci req.flags = AMDGPU_GEM_CREATE_NO_CPU_ACCESS; 279d722e3fbSopenharmony_ci 280d722e3fbSopenharmony_ci r = amdgpu_bo_alloc(device_handle, &req, &buf_handle); 281d722e3fbSopenharmony_ci CU_ASSERT_EQUAL(r, -ENOMEM); 282d722e3fbSopenharmony_ci 283d722e3fbSopenharmony_ci if (!r) { 284d722e3fbSopenharmony_ci r = amdgpu_bo_free(buf_handle); 285d722e3fbSopenharmony_ci CU_ASSERT_EQUAL(r, 0); 286d722e3fbSopenharmony_ci } 287d722e3fbSopenharmony_ci} 288d722e3fbSopenharmony_ci 289d722e3fbSopenharmony_cistatic void amdgpu_bo_find_by_cpu_mapping(void) 290d722e3fbSopenharmony_ci{ 291d722e3fbSopenharmony_ci amdgpu_bo_handle bo_handle, find_bo_handle; 292d722e3fbSopenharmony_ci amdgpu_va_handle va_handle; 293d722e3fbSopenharmony_ci void *bo_cpu; 294d722e3fbSopenharmony_ci uint64_t bo_mc_address; 295d722e3fbSopenharmony_ci uint64_t offset; 296d722e3fbSopenharmony_ci int r; 297d722e3fbSopenharmony_ci 298d722e3fbSopenharmony_ci r = amdgpu_bo_alloc_and_map(device_handle, 4096, 4096, 299d722e3fbSopenharmony_ci AMDGPU_GEM_DOMAIN_GTT, 0, 300d722e3fbSopenharmony_ci &bo_handle, &bo_cpu, 301d722e3fbSopenharmony_ci &bo_mc_address, &va_handle); 302d722e3fbSopenharmony_ci CU_ASSERT_EQUAL(r, 0); 303d722e3fbSopenharmony_ci 304d722e3fbSopenharmony_ci r = amdgpu_find_bo_by_cpu_mapping(device_handle, 305d722e3fbSopenharmony_ci bo_cpu, 306d722e3fbSopenharmony_ci 4096, 307d722e3fbSopenharmony_ci &find_bo_handle, 308d722e3fbSopenharmony_ci &offset); 309d722e3fbSopenharmony_ci CU_ASSERT_EQUAL(r, 0); 310d722e3fbSopenharmony_ci CU_ASSERT_EQUAL(offset, 0); 311d722e3fbSopenharmony_ci CU_ASSERT_EQUAL(bo_handle->handle, find_bo_handle->handle); 312d722e3fbSopenharmony_ci 313d722e3fbSopenharmony_ci atomic_dec(&find_bo_handle->refcount, 1); 314d722e3fbSopenharmony_ci r = amdgpu_bo_unmap_and_free(bo_handle, va_handle, 315d722e3fbSopenharmony_ci bo_mc_address, 4096); 316d722e3fbSopenharmony_ci CU_ASSERT_EQUAL(r, 0); 317d722e3fbSopenharmony_ci} 318