Debian Codegraph @

amdgpu/amdgpu_device.c

raw
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 /**
25 * \file amdgpu_device.c
26 *
27 * Implementation of functions for AMD GPU device
28 *
29 */
30
31 #include <sys/stat.h>
32 #include <errno.h>
33 #include <string.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37 #include <fcntl.h>
38
39 #include "xf86drm.h"
40 #include "amdgpu_drm.h"
41 #include "amdgpu_internal.h"
42 #include "util_math.h"
43
44 #define PTR_TO_UINT(x) ((unsigned)((intptr_t)(x)))
45
46 static pthread_mutex_t dev_mutex = PTHREAD_MUTEX_INITIALIZER;
47 static amdgpu_device_handle dev_list;
48
49 static int fd_compare(int fd1, int fd2)
50 {
51 char *name1 = drmGetPrimaryDeviceNameFromFd(fd1);
52 char *name2 = drmGetPrimaryDeviceNameFromFd(fd2);
53 int result;
54
55 if (name1 == NULL || name2 == NULL) {
56 free(name1);
57 free(name2);
58 return 0;
59 }
60
61 result = strcmp(name1, name2);
62 free(name1);
63 free(name2);
64
65 return result;
66 }
67
68 /**
69 * Get the authenticated form fd,
70 *
71 * \param fd - \c [in] File descriptor for AMD GPU device
72 * \param auth - \c [out] Pointer to output the fd is authenticated or not
73 * A render node fd, output auth = 0
74 * A legacy fd, get the authenticated for compatibility root
75 *
76 * \return 0 on success\n
77 * >0 - AMD specific error code\n
78 * <0 - Negative POSIX Error code
79 */
80 static int amdgpu_get_auth(int fd, int *auth)
81 {
82 int r = 0;
83 drm_client_t client = {};
84
85 if (drmGetNodeTypeFromFd(fd) == DRM_NODE_RENDER)
86 *auth = 0;
87 else {
88 client.idx = 0;
89 r = drmIoctl(fd, DRM_IOCTL_GET_CLIENT, &client);
90 if (!r)
91 *auth = client.auth;
92 }
93 return r;
94 }
95
96 static void amdgpu_device_free_internal(amdgpu_device_handle dev)
97 {
98 /* Remove dev from dev_list, if it was added there. */
99 if (dev == dev_list) {
100 dev_list = dev->next;
101 } else {
102 for (amdgpu_device_handle node = dev_list; node; node = node->next) {
103 if (node->next == dev) {
104 node->next = dev->next;
105 break;
106 }
107 }
108 }
109
110 close(dev->fd);
111 if ((dev->flink_fd >= 0) && (dev->fd != dev->flink_fd))
112 close(dev->flink_fd);
113
114 amdgpu_vamgr_deinit(&dev->va_mgr.vamgr_32);
115 amdgpu_vamgr_deinit(&dev->va_mgr.vamgr_low);
116 amdgpu_vamgr_deinit(&dev->va_mgr.vamgr_high_32);
117 amdgpu_vamgr_deinit(&dev->va_mgr.vamgr_high);
118 handle_table_fini(&dev->bo_handles);
119 handle_table_fini(&dev->bo_flink_names);
120 pthread_mutex_destroy(&dev->bo_table_mutex);
121 free(dev->marketing_name);
122 free(dev);
123 }
124
125 /**
126 * Assignment between two amdgpu_device pointers with reference counting.
127 *
128 * Usage:
129 * struct amdgpu_device *dst = ... , *src = ...;
130 *
131 * dst = src;
132 * // No reference counting. Only use this when you need to move
133 * // a reference from one pointer to another.
134 *
135 * amdgpu_device_reference(&dst, src);
136 * // Reference counters are updated. dst is decremented and src is
137 * // incremented. dst is freed if its reference counter is 0.
138 */
139 static void amdgpu_device_reference(struct amdgpu_device **dst,
140 struct amdgpu_device *src)
141 {
142 if (update_references(&(*dst)->refcount, &src->refcount))
143 amdgpu_device_free_internal(*dst);
144 *dst = src;
145 }
146
147 static int amdgpu_query_gfx_level_major(amdgpu_device_handle dev,
148 uint8_t *gfx_ip_version_major)
149 {
150 struct drm_amdgpu_info_hw_ip ip_info;
151 uint32_t gfx_ip_count = 0;
152 int r;
153
154 *gfx_ip_version_major = 0;
155
156 r = amdgpu_query_hw_ip_count(dev, AMDGPU_HW_IP_GFX, &gfx_ip_count);
157 if (r)
158 return r;
159
160 /* No graphics support. */
161 if (gfx_ip_count == 0)
162 return 0;
163
164 memset(&ip_info, 0, sizeof(ip_info));
165
166 r = amdgpu_query_hw_ip_info(dev, AMDGPU_HW_IP_GFX, 0, &ip_info);
167 if (r)
168 return r;
169
170 /* GFX6-8 don't set ip_discovery_version. */
171 if (dev->minor_version >= 48 && ip_info.ip_discovery_version) {
172 *gfx_ip_version_major = (ip_info.ip_discovery_version >> 16) & 0xff;
173 } else {
174 *gfx_ip_version_major = ip_info.hw_ip_version_major;
175 }
176
177 return r;
178 }
179
180 static int _amdgpu_device_initialize(int fd,
181 uint32_t *major_version,
182 uint32_t *minor_version,
183 amdgpu_device_handle *device_handle,
184 bool deduplicate_device)
185 {
186 struct amdgpu_device *dev = NULL;
187 uint8_t gfx_ip_version_major = 0;
188 drmVersionPtr version;
189 int r;
190 int flag_auth = 0;
191 int flag_authexist=0;
192 uint32_t accel_working = 0;
193 uint32_t va_mgr_flags = 0;
194
195 *device_handle = NULL;
196
197 pthread_mutex_lock(&dev_mutex);
198
199 r = amdgpu_get_auth(fd, &flag_auth);
200 if (r) {
201 fprintf(stderr, "%s: amdgpu_get_auth (1) failed (%i)\n",
202 __func__, r);
203 pthread_mutex_unlock(&dev_mutex);
204 return r;
205 }
206
207 if (deduplicate_device)
208 for (dev = dev_list; dev; dev = dev->next)
209 if (fd_compare(dev->fd, fd) == 0)
210 break;
211
212 if (dev) {
213 r = amdgpu_get_auth(dev->fd, &flag_authexist);
214 if (r) {
215 fprintf(stderr, "%s: amdgpu_get_auth (2) failed (%i)\n",
216 __func__, r);
217 pthread_mutex_unlock(&dev_mutex);
218 return r;
219 }
220 if ((flag_auth) && (!flag_authexist)) {
221 dev->flink_fd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
222 }
223 *major_version = dev->major_version;
224 *minor_version = dev->minor_version;
225 amdgpu_device_reference(device_handle, dev);
226 pthread_mutex_unlock(&dev_mutex);
227 return 0;
228 }
229
230 dev = calloc(1, sizeof(struct amdgpu_device));
231 if (!dev) {
232 fprintf(stderr, "%s: calloc failed\n", __func__);
233 pthread_mutex_unlock(&dev_mutex);
234 return -ENOMEM;
235 }
236
237 dev->fd = -1;
238 dev->flink_fd = -1;
239
240 atomic_set(&dev->refcount, 1);
241
242 version = drmGetVersion(fd);
243 if (version->version_major != 3) {
244 fprintf(stderr, "%s: DRM version is %d.%d.%d but this driver is "
245 "only compatible with 3.x.x.\n",
246 __func__,
247 version->version_major,
248 version->version_minor,
249 version->version_patchlevel);
250 drmFreeVersion(version);
251 r = -EBADF;
252 goto cleanup;
253 }
254
255 dev->fd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
256 dev->flink_fd = dev->fd;
257 dev->major_version = version->version_major;
258 dev->minor_version = version->version_minor;
259 drmFreeVersion(version);
260
261 pthread_mutex_init(&dev->bo_table_mutex, NULL);
262
263 /* Check if acceleration is working. */
264 r = amdgpu_query_info(dev, AMDGPU_INFO_ACCEL_WORKING, 4, &accel_working);
265 if (r) {
266 fprintf(stderr, "%s: amdgpu_query_info(ACCEL_WORKING) failed (%i)\n",
267 __func__, r);
268 goto cleanup;
269 }
270 if (!accel_working) {
271 fprintf(stderr, "%s: AMDGPU_INFO_ACCEL_WORKING = 0\n", __func__);
272 r = -EBADF;
273 goto cleanup;
274 }
275
276 r = amdgpu_query_gpu_info_init(dev);
277 if (r) {
278 fprintf(stderr, "%s: amdgpu_query_gpu_info_init failed\n", __func__);
279 goto cleanup;
280 }
281
282 r = amdgpu_query_gfx_level_major(dev, &gfx_ip_version_major);
283 if (!r) {
284 /* Split the HIGH addr space for GFX6-GFX12, except GFX9 to
285 * implement a workaround for SMEM loads with NULL PRT pages.
286 * This is silently ignored if querying the GFX level failed.
287 */
288 if (gfx_ip_version_major >= 6 && gfx_ip_version_major <= 12 &&
289 gfx_ip_version_major != 9) {
290 va_mgr_flags |= AMDGPU_VA_MGR_RESERVE_HALF_VA_FOR_PRT;
291 }
292 }
293
294 amdgpu_va_manager_init2(&dev->va_mgr,
295 dev->dev_info.virtual_address_offset,
296 dev->dev_info.virtual_address_max,
297 dev->dev_info.high_va_offset,
298 dev->dev_info.high_va_max,
299 dev->dev_info.virtual_address_alignment,
300 va_mgr_flags);
301
302 amdgpu_parse_asic_ids(dev);
303
304 *major_version = dev->major_version;
305 *minor_version = dev->minor_version;
306 *device_handle = dev;
307 if (deduplicate_device) {
308 dev->next = dev_list;
309 dev_list = dev;
310 }
311 pthread_mutex_unlock(&dev_mutex);
312
313 return 0;
314
315 cleanup:
316 if (dev->fd >= 0)
317 close(dev->fd);
318 free(dev);
319 pthread_mutex_unlock(&dev_mutex);
320 return r;
321 }
322
323 drm_public int amdgpu_device_initialize(int fd,
324 uint32_t *major_version,
325 uint32_t *minor_version,
326 amdgpu_device_handle *device_handle)
327 {
328 return _amdgpu_device_initialize(fd, major_version, minor_version, device_handle, true);
329 }
330
331 drm_public int amdgpu_device_initialize2(int fd, bool deduplicate_device,
332 uint32_t *major_version,
333 uint32_t *minor_version,
334 amdgpu_device_handle *device_handle)
335 {
336 return _amdgpu_device_initialize(fd, major_version, minor_version, device_handle, deduplicate_device);
337 }
338
339 drm_public int amdgpu_device_deinitialize(amdgpu_device_handle dev)
340 {
341 pthread_mutex_lock(&dev_mutex);
342 amdgpu_device_reference(&dev, NULL);
343 pthread_mutex_unlock(&dev_mutex);
344 return 0;
345 }
346
347 drm_public int amdgpu_device_get_fd(amdgpu_device_handle device_handle)
348 {
349 return device_handle->fd;
350 }
351
352 drm_public const char *amdgpu_get_marketing_name(amdgpu_device_handle dev)
353 {
354 if (dev->marketing_name)
355 return dev->marketing_name;
356 else
357 return "AMD Radeon Graphics";
358 }
359
360 drm_public int amdgpu_query_sw_info(amdgpu_device_handle dev,
361 enum amdgpu_sw_info info,
362 void *value)
363 {
364 uint32_t *val32 = (uint32_t*)value;
365
366 switch (info) {
367 case amdgpu_sw_info_address32_hi:
368 if (dev->va_mgr.vamgr_high_32.va_max)
369 *val32 = (dev->va_mgr.vamgr_high_32.va_max - 1) >> 32;
370 else
371 *val32 = (dev->va_mgr.vamgr_32.va_max - 1) >> 32;
372 return 0;
373 case amdgpu_sw_info_address_prt_wa_control_bit:
374 *val32 = dev->va_mgr.address_prt_wa_control_bit;
375 return 0;
376 }
377 return -EINVAL;
378 }