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.
25 * \file amdgpu_device.c
27 * Implementation of functions for AMD GPU device
43 #include "amdgpu_drm.h"
44 #include "amdgpu_internal.h"
45 #include "util_hash_table.h"
47 #define PTR_TO_UINT(x) ((unsigned)((intptr_t)(x)))
48 #define UINT_TO_PTR(x) ((void *)((intptr_t)(x)))
50 pthread_mutex_t fd_mutex = PTHREAD_MUTEX_INITIALIZER;
51 static struct util_hash_table *fd_tab;
53 static unsigned handle_hash(void *key)
55 return PTR_TO_UINT(key);
58 static int handle_compare(void *key1, void *key2)
60 return PTR_TO_UINT(key1) != PTR_TO_UINT(key2);
63 static unsigned fd_hash(void *key)
65 int fd = PTR_TO_UINT(key);
66 char *name = drmGetPrimaryDeviceNameFromFd(fd);
73 for (c = name; *c; ++c)
81 static int fd_compare(void *key1, void *key2)
83 int fd1 = PTR_TO_UINT(key1);
84 int fd2 = PTR_TO_UINT(key2);
85 char *name1 = drmGetPrimaryDeviceNameFromFd(fd1);
86 char *name2 = drmGetPrimaryDeviceNameFromFd(fd2);
89 if (name1 == NULL || name2 == NULL) {
95 result = strcmp(name1, name2);
103 * Get the authenticated form fd,
105 * \param fd - \c [in] File descriptor for AMD GPU device
106 * \param auth - \c [out] Pointer to output the fd is authenticated or not
107 * A render node fd, output auth = 0
108 * A legacy fd, get the authenticated for compatibility root
110 * \return 0 on success\n
111 * >0 - AMD specific error code\n
112 * <0 - Negative POSIX Error code
114 static int amdgpu_get_auth(int fd, int *auth)
117 drm_client_t client = {};
119 if (drmGetNodeTypeFromFd(fd) == DRM_NODE_RENDER)
123 r = drmIoctl(fd, DRM_IOCTL_GET_CLIENT, &client);
130 int amdgpu_device_initialize(int fd,
131 uint32_t *major_version,
132 uint32_t *minor_version,
133 amdgpu_device_handle *device_handle)
135 struct amdgpu_device *dev;
136 drmVersionPtr version;
139 int flag_authexist=0;
140 uint32_t accel_working = 0;
142 *device_handle = NULL;
144 pthread_mutex_lock(&fd_mutex);
146 fd_tab = util_hash_table_create(fd_hash, fd_compare);
147 r = amdgpu_get_auth(fd, &flag_auth);
149 pthread_mutex_unlock(&fd_mutex);
152 dev = util_hash_table_get(fd_tab, UINT_TO_PTR(fd));
154 r = amdgpu_get_auth(dev->fd, &flag_authexist);
156 pthread_mutex_unlock(&fd_mutex);
159 if ((flag_auth) && (!flag_authexist)) {
160 dev->flink_fd = dup(fd);
162 *major_version = dev->major_version;
163 *minor_version = dev->minor_version;
164 amdgpu_device_reference(device_handle, dev);
165 pthread_mutex_unlock(&fd_mutex);
169 dev = calloc(1, sizeof(struct amdgpu_device));
171 pthread_mutex_unlock(&fd_mutex);
178 atomic_set(&dev->refcount, 1);
180 version = drmGetVersion(fd);
181 if (version->version_major != 3) {
182 fprintf(stderr, "%s: DRM version is %d.%d.%d but this driver is "
183 "only compatible with 3.x.x.\n",
185 version->version_major,
186 version->version_minor,
187 version->version_patchlevel);
188 drmFreeVersion(version);
194 dev->flink_fd = dev->fd;
195 dev->major_version = version->version_major;
196 dev->minor_version = version->version_minor;
197 drmFreeVersion(version);
199 dev->bo_flink_names = util_hash_table_create(handle_hash,
201 dev->bo_handles = util_hash_table_create(handle_hash, handle_compare);
202 pthread_mutex_init(&dev->bo_table_mutex, NULL);
204 /* Check if acceleration is working. */
205 r = amdgpu_query_info(dev, AMDGPU_INFO_ACCEL_WORKING, 4, &accel_working);
208 if (!accel_working) {
213 r = amdgpu_query_gpu_info_init(dev);
217 dev->vamgr = amdgpu_vamgr_get_global(dev);
219 *major_version = dev->major_version;
220 *minor_version = dev->minor_version;
221 *device_handle = dev;
222 util_hash_table_set(fd_tab, UINT_TO_PTR(dev->fd), dev);
223 pthread_mutex_unlock(&fd_mutex);
231 pthread_mutex_unlock(&fd_mutex);
235 void amdgpu_device_free_internal(amdgpu_device_handle dev)
237 amdgpu_vamgr_reference(&dev->vamgr, NULL);
238 util_hash_table_destroy(dev->bo_flink_names);
239 util_hash_table_destroy(dev->bo_handles);
240 pthread_mutex_destroy(&dev->bo_table_mutex);
241 util_hash_table_remove(fd_tab, UINT_TO_PTR(dev->fd));
243 if ((dev->flink_fd >= 0) && (dev->fd != dev->flink_fd))
244 close(dev->flink_fd);
248 int amdgpu_device_deinitialize(amdgpu_device_handle dev)
250 amdgpu_device_reference(&dev, NULL);
254 void amdgpu_device_reference(struct amdgpu_device **dst,
255 struct amdgpu_device *src)
257 if (update_references(&(*dst)->refcount, &src->refcount))
258 amdgpu_device_free_internal(*dst);