2 * Copyright 2014 Advanced Micro Devices, Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
30 #include "CUnit/Basic.h"
32 #include "amdgpu_test.h"
33 #include "amdgpu_drm.h"
35 #define BUFFER_SIZE (4*1024)
36 #define BUFFER_ALIGN (4*1024)
38 static amdgpu_device_handle device_handle;
39 static uint32_t major_version;
40 static uint32_t minor_version;
42 static amdgpu_bo_handle buffer_handle;
43 static uint64_t virtual_mc_base_address;
44 static amdgpu_va_handle va_handle;
46 static void amdgpu_bo_export_import(void);
47 static void amdgpu_bo_metadata(void);
48 static void amdgpu_bo_map_unmap(void);
50 CU_TestInfo bo_tests[] = {
51 { "Export/Import", amdgpu_bo_export_import },
53 { "Metadata", amdgpu_bo_metadata },
55 { "CPU map/unmap", amdgpu_bo_map_unmap },
59 int suite_bo_tests_init(void)
61 struct amdgpu_bo_alloc_request req = {0};
62 amdgpu_bo_handle buf_handle;
66 r = amdgpu_device_initialize(drm_amdgpu[0], &major_version,
67 &minor_version, &device_handle);
69 if ((r == -EACCES) && (errno == EACCES))
70 printf("\n\nError:%s. "
71 "Hint:Try to run this test program as root.",
74 return CUE_SINIT_FAILED;
77 req.alloc_size = BUFFER_SIZE;
78 req.phys_alignment = BUFFER_ALIGN;
79 req.preferred_heap = AMDGPU_GEM_DOMAIN_GTT;
81 r = amdgpu_bo_alloc(device_handle, &req, &buf_handle);
83 return CUE_SINIT_FAILED;
85 r = amdgpu_va_range_alloc(device_handle,
86 amdgpu_gpu_va_range_general,
87 BUFFER_SIZE, BUFFER_ALIGN, 0,
92 r = amdgpu_bo_va_op(buf_handle, 0, BUFFER_SIZE, va, 0, AMDGPU_VA_OP_MAP);
96 buffer_handle = buf_handle;
97 virtual_mc_base_address = va;
102 amdgpu_va_range_free(va_handle);
105 amdgpu_bo_free(buf_handle);
106 return CUE_SINIT_FAILED;
109 int suite_bo_tests_clean(void)
113 r = amdgpu_bo_va_op(buffer_handle, 0, BUFFER_SIZE,
114 virtual_mc_base_address, 0,
117 return CUE_SCLEAN_FAILED;
119 r = amdgpu_va_range_free(va_handle);
121 return CUE_SCLEAN_FAILED;
123 r = amdgpu_bo_free(buffer_handle);
125 return CUE_SCLEAN_FAILED;
127 r = amdgpu_device_deinitialize(device_handle);
129 return CUE_SCLEAN_FAILED;
134 static void amdgpu_bo_export_import_do_type(enum amdgpu_bo_handle_type type)
136 struct amdgpu_bo_import_result res = {0};
137 uint32_t shared_handle;
140 r = amdgpu_bo_export(buffer_handle, type, &shared_handle);
141 CU_ASSERT_EQUAL(r, 0);
143 r = amdgpu_bo_import(device_handle, type, shared_handle, &res);
144 CU_ASSERT_EQUAL(r, 0);
146 CU_ASSERT_EQUAL(res.buf_handle, buffer_handle);
147 CU_ASSERT_EQUAL(res.alloc_size, BUFFER_SIZE);
149 r = amdgpu_bo_free(res.buf_handle);
150 CU_ASSERT_EQUAL(r, 0);
153 static void amdgpu_bo_export_import(void)
155 if (open_render_node) {
156 printf("(DRM render node is used. Skip export/Import test) ");
160 amdgpu_bo_export_import_do_type(amdgpu_bo_handle_type_gem_flink_name);
161 amdgpu_bo_export_import_do_type(amdgpu_bo_handle_type_dma_buf_fd);
164 static void amdgpu_bo_metadata(void)
166 struct amdgpu_bo_metadata meta = {0};
167 struct amdgpu_bo_info info = {0};
170 meta.size_metadata = 1;
171 meta.umd_metadata[0] = 0xdeadbeef;
173 r = amdgpu_bo_set_metadata(buffer_handle, &meta);
174 CU_ASSERT_EQUAL(r, 0);
176 r = amdgpu_bo_query_info(buffer_handle, &info);
177 CU_ASSERT_EQUAL(r, 0);
179 CU_ASSERT_EQUAL(info.metadata.size_metadata, 1);
180 CU_ASSERT_EQUAL(info.metadata.umd_metadata[0], 0xdeadbeef);
183 static void amdgpu_bo_map_unmap(void)
188 r = amdgpu_bo_cpu_map(buffer_handle, (void **)&ptr);
189 CU_ASSERT_EQUAL(r, 0);
190 CU_ASSERT_NOT_EQUAL(ptr, NULL);
192 for (i = 0; i < (BUFFER_SIZE / 4); ++i)
195 r = amdgpu_bo_cpu_unmap(buffer_handle);
196 CU_ASSERT_EQUAL(r, 0);