• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 		if ((r == -EACCES) && (errno == EACCES))
70 			printf("\n\nError:%s. "
71 				"Hint:Try to run this test program as root.",
72 				strerror(errno));
73 
74 		return CUE_SINIT_FAILED;
75 	}
76 
77 	req.alloc_size = BUFFER_SIZE;
78 	req.phys_alignment = BUFFER_ALIGN;
79 	req.preferred_heap = AMDGPU_GEM_DOMAIN_GTT;
80 
81 	r = amdgpu_bo_alloc(device_handle, &req, &buf_handle);
82 	if (r)
83 		return CUE_SINIT_FAILED;
84 
85 	r = amdgpu_va_range_alloc(device_handle,
86 				  amdgpu_gpu_va_range_general,
87 				  BUFFER_SIZE, BUFFER_ALIGN, 0,
88 				  &va, &va_handle, 0);
89 	if (r)
90 		goto error_va_alloc;
91 
92 	r = amdgpu_bo_va_op(buf_handle, 0, BUFFER_SIZE, va, 0, AMDGPU_VA_OP_MAP);
93 	if (r)
94 		goto error_va_map;
95 
96 	buffer_handle = buf_handle;
97 	virtual_mc_base_address = va;
98 
99 	return CUE_SUCCESS;
100 
101 error_va_map:
102 	amdgpu_va_range_free(va_handle);
103 
104 error_va_alloc:
105 	amdgpu_bo_free(buf_handle);
106 	return CUE_SINIT_FAILED;
107 }
108 
suite_bo_tests_clean(void)109 int suite_bo_tests_clean(void)
110 {
111 	int r;
112 
113 	r = amdgpu_bo_va_op(buffer_handle, 0, BUFFER_SIZE,
114 			    virtual_mc_base_address, 0,
115 			    AMDGPU_VA_OP_UNMAP);
116 	if (r)
117 		return CUE_SCLEAN_FAILED;
118 
119 	r = amdgpu_va_range_free(va_handle);
120 	if (r)
121 		return CUE_SCLEAN_FAILED;
122 
123 	r = amdgpu_bo_free(buffer_handle);
124 	if (r)
125 		return CUE_SCLEAN_FAILED;
126 
127 	r = amdgpu_device_deinitialize(device_handle);
128 	if (r)
129 		return CUE_SCLEAN_FAILED;
130 
131 	return CUE_SUCCESS;
132 }
133 
amdgpu_bo_export_import_do_type(enum amdgpu_bo_handle_type type)134 static void amdgpu_bo_export_import_do_type(enum amdgpu_bo_handle_type type)
135 {
136 	struct amdgpu_bo_import_result res = {0};
137 	uint32_t shared_handle;
138 	int r;
139 
140 	r = amdgpu_bo_export(buffer_handle, type, &shared_handle);
141 	CU_ASSERT_EQUAL(r, 0);
142 
143 	r = amdgpu_bo_import(device_handle, type, shared_handle, &res);
144 	CU_ASSERT_EQUAL(r, 0);
145 
146 	CU_ASSERT_EQUAL(res.buf_handle, buffer_handle);
147 	CU_ASSERT_EQUAL(res.alloc_size, BUFFER_SIZE);
148 
149 	r = amdgpu_bo_free(res.buf_handle);
150 	CU_ASSERT_EQUAL(r, 0);
151 }
152 
amdgpu_bo_export_import(void)153 static void amdgpu_bo_export_import(void)
154 {
155 	if (open_render_node) {
156 		printf("(DRM render node is used. Skip export/Import test) ");
157 		return;
158 	}
159 
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);
162 }
163 
amdgpu_bo_metadata(void)164 static void amdgpu_bo_metadata(void)
165 {
166 	struct amdgpu_bo_metadata meta = {0};
167 	struct amdgpu_bo_info info = {0};
168 	int r;
169 
170 	meta.size_metadata = 1;
171 	meta.umd_metadata[0] = 0xdeadbeef;
172 
173 	r = amdgpu_bo_set_metadata(buffer_handle, &meta);
174 	CU_ASSERT_EQUAL(r, 0);
175 
176 	r = amdgpu_bo_query_info(buffer_handle, &info);
177 	CU_ASSERT_EQUAL(r, 0);
178 
179 	CU_ASSERT_EQUAL(info.metadata.size_metadata, 1);
180 	CU_ASSERT_EQUAL(info.metadata.umd_metadata[0], 0xdeadbeef);
181 }
182 
amdgpu_bo_map_unmap(void)183 static void amdgpu_bo_map_unmap(void)
184 {
185 	uint32_t *ptr;
186 	int i, r;
187 
188 	r = amdgpu_bo_cpu_map(buffer_handle, (void **)&ptr);
189 	CU_ASSERT_EQUAL(r, 0);
190 	CU_ASSERT_NOT_EQUAL(ptr, NULL);
191 
192 	for (i = 0; i < (BUFFER_SIZE / 4); ++i)
193 		ptr[i] = 0xdeadbeef;
194 
195 	r = amdgpu_bo_cpu_unmap(buffer_handle);
196 	CU_ASSERT_EQUAL(r, 0);
197 }
198