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"
46 #include "util_math.h"
47 #include "amdgpu_asic_id.h"
49 #define PTR_TO_UINT(x) ((unsigned)((intptr_t)(x)))
50 #define UINT_TO_PTR(x) ((void *)((intptr_t)(x)))
52 static pthread_mutex_t fd_mutex = PTHREAD_MUTEX_INITIALIZER;
53 static struct util_hash_table *fd_tab;
55 static unsigned handle_hash(void *key)
57 return PTR_TO_UINT(key);
60 static int handle_compare(void *key1, void *key2)
62 return PTR_TO_UINT(key1) != PTR_TO_UINT(key2);
65 static unsigned fd_hash(void *key)
67 int fd = PTR_TO_UINT(key);
68 char *name = drmGetPrimaryDeviceNameFromFd(fd);
75 for (c = name; *c; ++c)
83 static int fd_compare(void *key1, void *key2)
85 int fd1 = PTR_TO_UINT(key1);
86 int fd2 = PTR_TO_UINT(key2);
87 char *name1 = drmGetPrimaryDeviceNameFromFd(fd1);
88 char *name2 = drmGetPrimaryDeviceNameFromFd(fd2);
91 if (name1 == NULL || name2 == NULL) {
97 result = strcmp(name1, name2);
105 * Get the authenticated form fd,
107 * \param fd - \c [in] File descriptor for AMD GPU device
108 * \param auth - \c [out] Pointer to output the fd is authenticated or not
109 * A render node fd, output auth = 0
110 * A legacy fd, get the authenticated for compatibility root
112 * \return 0 on success\n
113 * >0 - AMD specific error code\n
114 * <0 - Negative POSIX Error code
116 static int amdgpu_get_auth(int fd, int *auth)
119 drm_client_t client = {};
121 if (drmGetNodeTypeFromFd(fd) == DRM_NODE_RENDER)
125 r = drmIoctl(fd, DRM_IOCTL_GET_CLIENT, &client);
132 static void amdgpu_device_free_internal(amdgpu_device_handle dev)
134 amdgpu_vamgr_deinit(&dev->vamgr_32);
135 amdgpu_vamgr_deinit(&dev->vamgr);
136 util_hash_table_destroy(dev->bo_flink_names);
137 util_hash_table_destroy(dev->bo_handles);
138 pthread_mutex_destroy(&dev->bo_table_mutex);
139 util_hash_table_remove(fd_tab, UINT_TO_PTR(dev->fd));
141 if ((dev->flink_fd >= 0) && (dev->fd != dev->flink_fd))
142 close(dev->flink_fd);
147 * Assignment between two amdgpu_device pointers with reference counting.
150 * struct amdgpu_device *dst = ... , *src = ...;
153 * // No reference counting. Only use this when you need to move
154 * // a reference from one pointer to another.
156 * amdgpu_device_reference(&dst, src);
157 * // Reference counters are updated. dst is decremented and src is
158 * // incremented. dst is freed if its reference counter is 0.
160 static void amdgpu_device_reference(struct amdgpu_device **dst,
161 struct amdgpu_device *src)
163 if (update_references(&(*dst)->refcount, &src->refcount))
164 amdgpu_device_free_internal(*dst);
168 int amdgpu_device_initialize(int fd,
169 uint32_t *major_version,
170 uint32_t *minor_version,
171 amdgpu_device_handle *device_handle)
173 struct amdgpu_device *dev;
174 drmVersionPtr version;
177 int flag_authexist=0;
178 uint32_t accel_working = 0;
181 *device_handle = NULL;
183 pthread_mutex_lock(&fd_mutex);
185 fd_tab = util_hash_table_create(fd_hash, fd_compare);
186 r = amdgpu_get_auth(fd, &flag_auth);
188 pthread_mutex_unlock(&fd_mutex);
191 dev = util_hash_table_get(fd_tab, UINT_TO_PTR(fd));
193 r = amdgpu_get_auth(dev->fd, &flag_authexist);
195 pthread_mutex_unlock(&fd_mutex);
198 if ((flag_auth) && (!flag_authexist)) {
199 dev->flink_fd = dup(fd);
201 *major_version = dev->major_version;
202 *minor_version = dev->minor_version;
203 amdgpu_device_reference(device_handle, dev);
204 pthread_mutex_unlock(&fd_mutex);
208 dev = calloc(1, sizeof(struct amdgpu_device));
210 pthread_mutex_unlock(&fd_mutex);
217 atomic_set(&dev->refcount, 1);
219 version = drmGetVersion(fd);
220 if (version->version_major != 3) {
221 fprintf(stderr, "%s: DRM version is %d.%d.%d but this driver is "
222 "only compatible with 3.x.x.\n",
224 version->version_major,
225 version->version_minor,
226 version->version_patchlevel);
227 drmFreeVersion(version);
233 dev->flink_fd = dev->fd;
234 dev->major_version = version->version_major;
235 dev->minor_version = version->version_minor;
236 drmFreeVersion(version);
238 dev->bo_flink_names = util_hash_table_create(handle_hash,
240 dev->bo_handles = util_hash_table_create(handle_hash, handle_compare);
241 pthread_mutex_init(&dev->bo_table_mutex, NULL);
243 /* Check if acceleration is working. */
244 r = amdgpu_query_info(dev, AMDGPU_INFO_ACCEL_WORKING, 4, &accel_working);
247 if (!accel_working) {
252 r = amdgpu_query_gpu_info_init(dev);
256 amdgpu_vamgr_init(&dev->vamgr, dev->dev_info.virtual_address_offset,
257 dev->dev_info.virtual_address_max,
258 dev->dev_info.virtual_address_alignment);
260 max = MIN2(dev->dev_info.virtual_address_max, 0xffffffff);
261 start = amdgpu_vamgr_find_va(&dev->vamgr,
262 max - dev->dev_info.virtual_address_offset,
263 dev->dev_info.virtual_address_alignment, 0);
264 if (start > 0xffffffff)
265 goto free_va; /* shouldn't get here */
267 amdgpu_vamgr_init(&dev->vamgr_32, start, max,
268 dev->dev_info.virtual_address_alignment);
270 *major_version = dev->major_version;
271 *minor_version = dev->minor_version;
272 *device_handle = dev;
273 util_hash_table_set(fd_tab, UINT_TO_PTR(dev->fd), dev);
274 pthread_mutex_unlock(&fd_mutex);
280 amdgpu_vamgr_free_va(&dev->vamgr, start,
281 max - dev->dev_info.virtual_address_offset);
282 amdgpu_vamgr_deinit(&dev->vamgr);
288 pthread_mutex_unlock(&fd_mutex);
292 int amdgpu_device_deinitialize(amdgpu_device_handle dev)
294 amdgpu_device_reference(&dev, NULL);
298 const char *amdgpu_get_marketing_name(amdgpu_device_handle dev)
300 const struct amdgpu_asic_id_table_t *t = amdgpu_asic_id_table;
303 if ((t->did == dev->info.asic_id) &&
304 (t->rid == dev->info.pci_rev_id))
305 return t->marketing_name;