1 /*
2  * Copyright 2014 Advanced Micro Devices, 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 */
23 
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27 
28 #include <stdio.h>
29 
30 #include "CUnit/Basic.h"
31 
32 #include "amdgpu_test.h"
33 #include "amdgpu_drm.h"
34 
35 #define BUFFER_SIZE (4*1024)
36 #define BUFFER_ALIGN (4*1024)
37 
38 static amdgpu_device_handle device_handle;
39 static uint32_t major_version;
40 static uint32_t minor_version;
41 
42 static amdgpu_bo_handle buffer_handle;
43 static uint64_t virtual_mc_base_address;
44 static amdgpu_va_handle va_handle;
45 
46 static void amdgpu_bo_export_import(void);
47 static void amdgpu_bo_metadata(void);
48 static void amdgpu_bo_map_unmap(void);
49 
50 CU_TestInfo bo_tests[] = {
51 	{ "Export/Import",  amdgpu_bo_export_import },
52 #if 0
53 	{ "Metadata",  amdgpu_bo_metadata },
54 #endif
55 	{ "CPU map/unmap",  amdgpu_bo_map_unmap },
56 	CU_TEST_INFO_NULL,
57 };
58 
suite_bo_tests_init(void)59 int suite_bo_tests_init(void)
60 {
61 	struct amdgpu_bo_alloc_request req = {0};
62 	amdgpu_bo_handle buf_handle;
63 	uint64_t va;
64 	int r;
65 
66 	r = amdgpu_device_initialize(drm_amdgpu[0], &major_version,
67 				  &minor_version, &device_handle);
68 	if (r)
69 		return CUE_SINIT_FAILED;
70 
71 	req.alloc_size = BUFFER_SIZE;
72 	req.phys_alignment = BUFFER_ALIGN;
73 	req.preferred_heap = AMDGPU_GEM_DOMAIN_GTT;
74 
75 	r = amdgpu_bo_alloc(device_handle, &req, &buf_handle);
76 	if (r)
77 		return CUE_SINIT_FAILED;
78 
79 	r = amdgpu_va_range_alloc(device_handle,
80 				  amdgpu_gpu_va_range_general,
81 				  BUFFER_SIZE, BUFFER_ALIGN, 0,
82 				  &va, &va_handle, 0);
83 	if (r)
84 		goto error_va_alloc;
85 
86 	r = amdgpu_bo_va_op(buf_handle, 0, BUFFER_SIZE, va, 0, AMDGPU_VA_OP_MAP);
87 	if (r)
88 		goto error_va_map;
89 
90 	buffer_handle = buf_handle;
91 	virtual_mc_base_address = va;
92 
93 	return CUE_SUCCESS;
94 
95 error_va_map:
96 	amdgpu_va_range_free(va_handle);
97 
98 error_va_alloc:
99 	amdgpu_bo_free(buf_handle);
100 	return CUE_SINIT_FAILED;
101 }
102 
suite_bo_tests_clean(void)103 int suite_bo_tests_clean(void)
104 {
105 	int r;
106 
107 	r = amdgpu_bo_va_op(buffer_handle, 0, BUFFER_SIZE,
108 			    virtual_mc_base_address, 0,
109 			    AMDGPU_VA_OP_UNMAP);
110 	if (r)
111 		return CUE_SCLEAN_FAILED;
112 
113 	r = amdgpu_va_range_free(va_handle);
114 	if (r)
115 		return CUE_SCLEAN_FAILED;
116 
117 	r = amdgpu_bo_free(buffer_handle);
118 	if (r)
119 		return CUE_SCLEAN_FAILED;
120 
121 	r = amdgpu_device_deinitialize(device_handle);
122 	if (r)
123 		return CUE_SCLEAN_FAILED;
124 
125 	return CUE_SUCCESS;
126 }
127 
amdgpu_bo_export_import_do_type(enum amdgpu_bo_handle_type type)128 static void amdgpu_bo_export_import_do_type(enum amdgpu_bo_handle_type type)
129 {
130 	struct amdgpu_bo_import_result res = {0};
131 	uint32_t shared_handle;
132 	int r;
133 
134 	r = amdgpu_bo_export(buffer_handle, type, &shared_handle);
135 	CU_ASSERT_EQUAL(r, 0);
136 
137 	r = amdgpu_bo_import(device_handle, type, shared_handle, &res);
138 	CU_ASSERT_EQUAL(r, 0);
139 
140 	CU_ASSERT_EQUAL(res.buf_handle, buffer_handle);
141 	CU_ASSERT_EQUAL(res.alloc_size, BUFFER_SIZE);
142 
143 	r = amdgpu_bo_free(res.buf_handle);
144 	CU_ASSERT_EQUAL(r, 0);
145 }
146 
amdgpu_bo_export_import(void)147 static void amdgpu_bo_export_import(void)
148 {
149 	amdgpu_bo_export_import_do_type(amdgpu_bo_handle_type_gem_flink_name);
150 	amdgpu_bo_export_import_do_type(amdgpu_bo_handle_type_dma_buf_fd);
151 }
152 
amdgpu_bo_metadata(void)153 static void amdgpu_bo_metadata(void)
154 {
155 	struct amdgpu_bo_metadata meta = {0};
156 	struct amdgpu_bo_info info = {0};
157 	int r;
158 
159 	meta.size_metadata = 1;
160 	meta.umd_metadata[0] = 0xdeadbeef;
161 
162 	r = amdgpu_bo_set_metadata(buffer_handle, &meta);
163 	CU_ASSERT_EQUAL(r, 0);
164 
165 	r = amdgpu_bo_query_info(buffer_handle, &info);
166 	CU_ASSERT_EQUAL(r, 0);
167 
168 	CU_ASSERT_EQUAL(info.metadata.size_metadata, 1);
169 	CU_ASSERT_EQUAL(info.metadata.umd_metadata[0], 0xdeadbeef);
170 }
171 
amdgpu_bo_map_unmap(void)172 static void amdgpu_bo_map_unmap(void)
173 {
174 	uint32_t *ptr;
175 	int i, r;
176 
177 	r = amdgpu_bo_cpu_map(buffer_handle, (void **)&ptr);
178 	CU_ASSERT_EQUAL(r, 0);
179 	CU_ASSERT_NOT_EQUAL(ptr, NULL);
180 
181 	for (i = 0; i < (BUFFER_SIZE / 4); ++i)
182 		ptr[i] = 0xdeadbeef;
183 
184 	r = amdgpu_bo_cpu_unmap(buffer_handle);
185 	CU_ASSERT_EQUAL(r, 0);
186 }
187