amdgpu: hide the final internal functions from global namespace
[platform/upstream/libdrm.git] / amdgpu / amdgpu_device.c
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 #ifdef HAVE_CONFIG_H
32 #include "config.h"
33 #endif
34
35 #include <sys/stat.h>
36 #include <errno.h>
37 #include <string.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <unistd.h>
41
42 #include "xf86drm.h"
43 #include "amdgpu_drm.h"
44 #include "amdgpu_internal.h"
45 #include "util_hash_table.h"
46
47 #define PTR_TO_UINT(x) ((unsigned)((intptr_t)(x)))
48 #define UINT_TO_PTR(x) ((void *)((intptr_t)(x)))
49
50 static pthread_mutex_t fd_mutex = PTHREAD_MUTEX_INITIALIZER;
51 static struct util_hash_table *fd_tab;
52
53 static unsigned handle_hash(void *key)
54 {
55         return PTR_TO_UINT(key);
56 }
57
58 static int handle_compare(void *key1, void *key2)
59 {
60         return PTR_TO_UINT(key1) != PTR_TO_UINT(key2);
61 }
62
63 static unsigned fd_hash(void *key)
64 {
65         int fd = PTR_TO_UINT(key);
66         char *name = drmGetPrimaryDeviceNameFromFd(fd);
67         unsigned result = 0;
68         char *c;
69
70         if (name == NULL)
71                 return 0;
72
73         for (c = name; *c; ++c)
74                 result += *c;
75
76         free(name);
77
78         return result;
79 }
80
81 static int fd_compare(void *key1, void *key2)
82 {
83         int fd1 = PTR_TO_UINT(key1);
84         int fd2 = PTR_TO_UINT(key2);
85         char *name1 = drmGetPrimaryDeviceNameFromFd(fd1);
86         char *name2 = drmGetPrimaryDeviceNameFromFd(fd2);
87         int result;
88
89         if (name1 == NULL || name2 == NULL) {
90                 free(name1);
91                 free(name2);
92                 return 0;
93         }
94
95         result = strcmp(name1, name2);
96         free(name1);
97         free(name2);
98
99         return result;
100 }
101
102 /**
103 * Get the authenticated form fd,
104 *
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
109 *
110 * \return   0 on success\n
111 *          >0 - AMD specific error code\n
112 *          <0 - Negative POSIX Error code
113 */
114 static int amdgpu_get_auth(int fd, int *auth)
115 {
116         int r = 0;
117         drm_client_t client = {};
118
119         if (drmGetNodeTypeFromFd(fd) == DRM_NODE_RENDER)
120                 *auth = 0;
121         else {
122                 client.idx = 0;
123                 r = drmIoctl(fd, DRM_IOCTL_GET_CLIENT, &client);
124                 if (!r)
125                         *auth = client.auth;
126         }
127         return r;
128 }
129
130 static void amdgpu_device_free_internal(amdgpu_device_handle dev)
131 {
132         amdgpu_vamgr_reference(&dev->vamgr, NULL);
133         util_hash_table_destroy(dev->bo_flink_names);
134         util_hash_table_destroy(dev->bo_handles);
135         pthread_mutex_destroy(&dev->bo_table_mutex);
136         util_hash_table_remove(fd_tab, UINT_TO_PTR(dev->fd));
137         close(dev->fd);
138         if ((dev->flink_fd >= 0) && (dev->fd != dev->flink_fd))
139                 close(dev->flink_fd);
140         free(dev);
141 }
142
143 /**
144  * Assignment between two amdgpu_device pointers with reference counting.
145  *
146  * Usage:
147  *    struct amdgpu_device *dst = ... , *src = ...;
148  *
149  *    dst = src;
150  *    // No reference counting. Only use this when you need to move
151  *    // a reference from one pointer to another.
152  *
153  *    amdgpu_device_reference(&dst, src);
154  *    // Reference counters are updated. dst is decremented and src is
155  *    // incremented. dst is freed if its reference counter is 0.
156  */
157 static void amdgpu_device_reference(struct amdgpu_device **dst,
158                              struct amdgpu_device *src)
159 {
160         if (update_references(&(*dst)->refcount, &src->refcount))
161                 amdgpu_device_free_internal(*dst);
162         *dst = src;
163 }
164
165 int amdgpu_device_initialize(int fd,
166                              uint32_t *major_version,
167                              uint32_t *minor_version,
168                              amdgpu_device_handle *device_handle)
169 {
170         struct amdgpu_device *dev;
171         drmVersionPtr version;
172         int r;
173         int flag_auth = 0;
174         int flag_authexist=0;
175         uint32_t accel_working = 0;
176
177         *device_handle = NULL;
178
179         pthread_mutex_lock(&fd_mutex);
180         if (!fd_tab)
181                 fd_tab = util_hash_table_create(fd_hash, fd_compare);
182         r = amdgpu_get_auth(fd, &flag_auth);
183         if (r) {
184                 pthread_mutex_unlock(&fd_mutex);
185                 return r;
186         }
187         dev = util_hash_table_get(fd_tab, UINT_TO_PTR(fd));
188         if (dev) {
189                 r = amdgpu_get_auth(dev->fd, &flag_authexist);
190                 if (r) {
191                         pthread_mutex_unlock(&fd_mutex);
192                         return r;
193                 }
194                 if ((flag_auth) && (!flag_authexist)) {
195                         dev->flink_fd = dup(fd);
196                 }
197                 *major_version = dev->major_version;
198                 *minor_version = dev->minor_version;
199                 amdgpu_device_reference(device_handle, dev);
200                 pthread_mutex_unlock(&fd_mutex);
201                 return 0;
202         }
203
204         dev = calloc(1, sizeof(struct amdgpu_device));
205         if (!dev) {
206                 pthread_mutex_unlock(&fd_mutex);
207                 return -ENOMEM;
208         }
209
210         dev->fd = -1;
211         dev->flink_fd = -1;
212
213         atomic_set(&dev->refcount, 1);
214
215         version = drmGetVersion(fd);
216         if (version->version_major != 3) {
217                 fprintf(stderr, "%s: DRM version is %d.%d.%d but this driver is "
218                         "only compatible with 3.x.x.\n",
219                         __func__,
220                         version->version_major,
221                         version->version_minor,
222                         version->version_patchlevel);
223                 drmFreeVersion(version);
224                 r = -EBADF;
225                 goto cleanup;
226         }
227
228         dev->fd = dup(fd);
229         dev->flink_fd = dev->fd;
230         dev->major_version = version->version_major;
231         dev->minor_version = version->version_minor;
232         drmFreeVersion(version);
233
234         dev->bo_flink_names = util_hash_table_create(handle_hash,
235                                                      handle_compare);
236         dev->bo_handles = util_hash_table_create(handle_hash, handle_compare);
237         pthread_mutex_init(&dev->bo_table_mutex, NULL);
238
239         /* Check if acceleration is working. */
240         r = amdgpu_query_info(dev, AMDGPU_INFO_ACCEL_WORKING, 4, &accel_working);
241         if (r)
242                 goto cleanup;
243         if (!accel_working) {
244                 r = -EBADF;
245                 goto cleanup;
246         }
247
248         r = amdgpu_query_gpu_info_init(dev);
249         if (r)
250                 goto cleanup;
251
252         dev->vamgr = amdgpu_vamgr_get_global(dev);
253
254         *major_version = dev->major_version;
255         *minor_version = dev->minor_version;
256         *device_handle = dev;
257         util_hash_table_set(fd_tab, UINT_TO_PTR(dev->fd), dev);
258         pthread_mutex_unlock(&fd_mutex);
259
260         return 0;
261
262 cleanup:
263         if (dev->fd >= 0)
264                 close(dev->fd);
265         free(dev);
266         pthread_mutex_unlock(&fd_mutex);
267         return r;
268 }
269
270 int amdgpu_device_deinitialize(amdgpu_device_handle dev)
271 {
272         amdgpu_device_reference(&dev, NULL);
273         return 0;
274 }