diff options
author | Alex Xie | 2017-01-24 16:29:50 -0600 |
---|---|---|
committer | Alex Deucher | 2017-01-27 10:53:26 -0600 |
commit | 5e0f7c5c65ca6714cd7352d72303ec2cbec35cb5 (patch) | |
tree | f9a6eac3b42907850c8e9cb8d618f19daf205e78 | |
parent | 8ef7e5b7026d06a803f0bf75fe323c14f142db3a (diff) | |
download | external-libdrm-5e0f7c5c65ca6714cd7352d72303ec2cbec35cb5.tar.gz external-libdrm-5e0f7c5c65ca6714cd7352d72303ec2cbec35cb5.tar.xz external-libdrm-5e0f7c5c65ca6714cd7352d72303ec2cbec35cb5.zip |
amdgpu: verify the tested device
Verify the vender ID and driver name.
Open all AMDGPU devices.
Provide an option to open render node.
Tested as root: PASS
Tested as non-privileged user:
All tests failed as expected
v2: Return value in the ene of function amdgpu_open_devices.
Check the return value of amdgpu_open_devices.
amdgpu_test is not for USB device for the time being.
Get the name of node from function drmGetDevices2.
Drop the legacy drmAvailable() from the test.
Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
Signed-off-by: Alex Xie <AlexBin.Xie@amd.com>
Signed-off-by: Alex Deucher <alexander.deucher@amd.com>
-rw-r--r-- | tests/amdgpu/amdgpu_test.c | 145 |
1 files changed, 115 insertions, 30 deletions
diff --git a/tests/amdgpu/amdgpu_test.c b/tests/amdgpu/amdgpu_test.c index 71f357c6..d2b00d4f 100644 --- a/tests/amdgpu/amdgpu_test.c +++ b/tests/amdgpu/amdgpu_test.c | |||
@@ -115,6 +115,111 @@ static const char usage[] = "Usage: %s [-hl] [<-s <suite id>> [-t <test id>]]\n" | |||
115 | /** Specified options strings for getopt */ | 115 | /** Specified options strings for getopt */ |
116 | static const char options[] = "hls:t:"; | 116 | static const char options[] = "hls:t:"; |
117 | 117 | ||
118 | /* Open AMD devices. | ||
119 | * Return the number of AMD device openned. | ||
120 | */ | ||
121 | static int amdgpu_open_devices(int open_render_node) | ||
122 | { | ||
123 | drmDevicePtr devices[MAX_CARDS_SUPPORTED]; | ||
124 | int ret; | ||
125 | int i; | ||
126 | int drm_node; | ||
127 | int amd_index = 0; | ||
128 | int drm_count; | ||
129 | int fd; | ||
130 | drmVersionPtr version; | ||
131 | |||
132 | drm_count = drmGetDevices2(0, devices, MAX_CARDS_SUPPORTED); | ||
133 | |||
134 | if (drm_count < 0) { | ||
135 | fprintf(stderr, | ||
136 | "drmGetDevices2() returned an error %d\n", | ||
137 | drm_count); | ||
138 | return 0; | ||
139 | } | ||
140 | |||
141 | for (i = 0; i < drm_count; i++) { | ||
142 | /* If this is not PCI device, skip*/ | ||
143 | if (devices[i]->bustype != DRM_BUS_PCI) | ||
144 | continue; | ||
145 | |||
146 | /* If this is not AMD GPU vender ID, skip*/ | ||
147 | if (devices[i]->deviceinfo.pci->vendor_id != 0x1002) | ||
148 | continue; | ||
149 | |||
150 | if (open_render_node) | ||
151 | drm_node = DRM_NODE_RENDER; | ||
152 | else | ||
153 | drm_node = DRM_NODE_PRIMARY; | ||
154 | |||
155 | fd = -1; | ||
156 | if (devices[i]->available_nodes & 1 << drm_node) | ||
157 | fd = open( | ||
158 | devices[i]->nodes[drm_node], | ||
159 | O_RDWR | O_CLOEXEC); | ||
160 | |||
161 | /* This node is not available. */ | ||
162 | if (fd < 0) continue; | ||
163 | |||
164 | version = drmGetVersion(fd); | ||
165 | if (!version) { | ||
166 | fprintf(stderr, | ||
167 | "Warning: Cannot get version for %s." | ||
168 | "Error is %s\n", | ||
169 | devices[i]->nodes[drm_node], | ||
170 | strerror(errno)); | ||
171 | close(fd); | ||
172 | continue; | ||
173 | } | ||
174 | |||
175 | if (strcmp(version->name, "amdgpu")) { | ||
176 | /* This is not AMDGPU driver, skip.*/ | ||
177 | drmFreeVersion(version); | ||
178 | close(fd); | ||
179 | continue; | ||
180 | } | ||
181 | |||
182 | drmFreeVersion(version); | ||
183 | |||
184 | drm_amdgpu[amd_index] = fd; | ||
185 | amd_index++; | ||
186 | } | ||
187 | |||
188 | drmFreeDevices(devices, drm_count); | ||
189 | return amd_index; | ||
190 | } | ||
191 | |||
192 | /* Close AMD devices. | ||
193 | */ | ||
194 | static void amdgpu_close_devices() | ||
195 | { | ||
196 | int i; | ||
197 | for (i = 0; i < MAX_CARDS_SUPPORTED; i++) | ||
198 | if (drm_amdgpu[i] >=0) | ||
199 | close(drm_amdgpu[i]); | ||
200 | } | ||
201 | |||
202 | /* Print AMD devices information */ | ||
203 | static void amdgpu_print_devices() | ||
204 | { | ||
205 | int i; | ||
206 | for (i = 0; i < MAX_CARDS_SUPPORTED; i++) | ||
207 | if (drm_amdgpu[i] >=0) { | ||
208 | /** Display version of DRM driver */ | ||
209 | drmVersionPtr retval = drmGetVersion(drm_amdgpu[0]); | ||
210 | |||
211 | if (retval == NULL) { | ||
212 | perror("Cannot get version for AMDGPU device"); | ||
213 | exit(EXIT_FAILURE); | ||
214 | } | ||
215 | |||
216 | printf("AMDGPU device #%d: " | ||
217 | "Name: [%s] : Date [%s] : Description [%s]\n", | ||
218 | i, retval->name, retval->date, retval->desc); | ||
219 | drmFreeVersion(retval); | ||
220 | } | ||
221 | } | ||
222 | |||
118 | /* The main() function for setting up and running the tests. | 223 | /* The main() function for setting up and running the tests. |
119 | * Returns a CUE_SUCCESS on successful running, another | 224 | * Returns a CUE_SUCCESS on successful running, another |
120 | * CUnit error code on failure. | 225 | * CUnit error code on failure. |
@@ -128,14 +233,6 @@ int main(int argc, char **argv) | |||
128 | CU_pSuite pSuite = NULL; | 233 | CU_pSuite pSuite = NULL; |
129 | CU_pTest pTest = NULL; | 234 | CU_pTest pTest = NULL; |
130 | 235 | ||
131 | int aval = drmAvailable(); | ||
132 | |||
133 | if (aval == 0) { | ||
134 | fprintf(stderr, "DRM driver is not available\n"); | ||
135 | exit(EXIT_FAILURE); | ||
136 | } | ||
137 | |||
138 | |||
139 | for (i = 0; i < MAX_CARDS_SUPPORTED; i++) | 236 | for (i = 0; i < MAX_CARDS_SUPPORTED; i++) |
140 | drm_amdgpu[i] = -1; | 237 | drm_amdgpu[i] = -1; |
141 | 238 | ||
@@ -163,35 +260,23 @@ int main(int argc, char **argv) | |||
163 | } | 260 | } |
164 | } | 261 | } |
165 | 262 | ||
166 | /* Try to open all possible radeon connections | 263 | if (amdgpu_open_devices(0) <= 0) { |
167 | * Right now: Open only the 0. | 264 | perror("Cannot open AMDGPU device"); |
168 | */ | ||
169 | printf("Try to open the card 0..\n"); | ||
170 | drm_amdgpu[0] = open("/dev/dri/card0", O_RDWR | O_CLOEXEC); | ||
171 | |||
172 | if (drm_amdgpu[0] < 0) { | ||
173 | perror("Cannot open /dev/dri/card0\n"); | ||
174 | exit(EXIT_FAILURE); | 265 | exit(EXIT_FAILURE); |
175 | } | 266 | } |
176 | 267 | ||
177 | /** Display version of DRM driver */ | 268 | if (drm_amdgpu[0] < 0) { |
178 | drmVersionPtr retval = drmGetVersion(drm_amdgpu[0]); | 269 | perror("Cannot open AMDGPU device"); |
179 | |||
180 | if (retval == NULL) { | ||
181 | perror("Could not get information about DRM driver"); | ||
182 | exit(EXIT_FAILURE); | 270 | exit(EXIT_FAILURE); |
183 | } | 271 | } |
184 | 272 | ||
185 | printf("DRM Driver: Name: [%s] : Date [%s] : Description [%s]\n", | 273 | amdgpu_print_devices(); |
186 | retval->name, retval->date, retval->desc); | ||
187 | |||
188 | drmFreeVersion(retval); | ||
189 | 274 | ||
190 | /* Initialize test suites to run */ | 275 | /* Initialize test suites to run */ |
191 | 276 | ||
192 | /* initialize the CUnit test registry */ | 277 | /* initialize the CUnit test registry */ |
193 | if (CUE_SUCCESS != CU_initialize_registry()) { | 278 | if (CUE_SUCCESS != CU_initialize_registry()) { |
194 | close(drm_amdgpu[0]); | 279 | amdgpu_close_devices(); |
195 | return CU_get_error(); | 280 | return CU_get_error(); |
196 | } | 281 | } |
197 | 282 | ||
@@ -200,7 +285,7 @@ int main(int argc, char **argv) | |||
200 | fprintf(stderr, "suite registration failed - %s\n", | 285 | fprintf(stderr, "suite registration failed - %s\n", |
201 | CU_get_error_msg()); | 286 | CU_get_error_msg()); |
202 | CU_cleanup_registry(); | 287 | CU_cleanup_registry(); |
203 | close(drm_amdgpu[0]); | 288 | amdgpu_close_devices(); |
204 | exit(EXIT_FAILURE); | 289 | exit(EXIT_FAILURE); |
205 | } | 290 | } |
206 | 291 | ||
@@ -222,7 +307,7 @@ int main(int argc, char **argv) | |||
222 | fprintf(stderr, "Invalid test id: %d\n", | 307 | fprintf(stderr, "Invalid test id: %d\n", |
223 | test_id); | 308 | test_id); |
224 | CU_cleanup_registry(); | 309 | CU_cleanup_registry(); |
225 | close(drm_amdgpu[0]); | 310 | amdgpu_close_devices(); |
226 | exit(EXIT_FAILURE); | 311 | exit(EXIT_FAILURE); |
227 | } | 312 | } |
228 | } else | 313 | } else |
@@ -231,13 +316,13 @@ int main(int argc, char **argv) | |||
231 | fprintf(stderr, "Invalid suite id : %d\n", | 316 | fprintf(stderr, "Invalid suite id : %d\n", |
232 | suite_id); | 317 | suite_id); |
233 | CU_cleanup_registry(); | 318 | CU_cleanup_registry(); |
234 | close(drm_amdgpu[0]); | 319 | amdgpu_close_devices(); |
235 | exit(EXIT_FAILURE); | 320 | exit(EXIT_FAILURE); |
236 | } | 321 | } |
237 | } else | 322 | } else |
238 | CU_basic_run_tests(); | 323 | CU_basic_run_tests(); |
239 | 324 | ||
240 | CU_cleanup_registry(); | 325 | CU_cleanup_registry(); |
241 | close(drm_amdgpu[0]); | 326 | amdgpu_close_devices(); |
242 | return CU_get_error(); | 327 | return CU_get_error(); |
243 | } | 328 | } |