OSDN Git Service

amdgpu: cleanup public interface style
[android-x86/external-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
32 #include <sys/stat.h>
33 #include <errno.h>
34 #include <string.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37
38 #include "xf86drm.h"
39 #include "amdgpu_drm.h"
40 #include "amdgpu_internal.h"
41 #include "util_hash_table.h"
42
43 #define PTR_TO_UINT(x) ((unsigned)((intptr_t)(x)))
44 #define UINT_TO_PTR(x) ((void *)((intptr_t)(x)))
45
46 pthread_mutex_t fd_mutex = PTHREAD_MUTEX_INITIALIZER;
47 static struct util_hash_table *fd_tab;
48
49 static unsigned handle_hash(void *key)
50 {
51         return PTR_TO_UINT(key);
52 }
53
54 static int handle_compare(void *key1, void *key2)
55 {
56         return PTR_TO_UINT(key1) != PTR_TO_UINT(key2);
57 }
58
59 static unsigned fd_hash(void *key)
60 {
61         int fd = PTR_TO_UINT(key);
62         char *name = drmGetPrimaryDeviceNameFromFd(fd);
63         unsigned result = 0;
64         char *c;
65
66         if (name == NULL)
67                 return 0;
68
69         for (c = name; *c; ++c)
70                 result += *c;
71
72         free(name);
73
74         return result;
75 }
76
77 static int fd_compare(void *key1, void *key2)
78 {
79         int fd1 = PTR_TO_UINT(key1);
80         int fd2 = PTR_TO_UINT(key2);
81         char *name1 = drmGetPrimaryDeviceNameFromFd(fd1);
82         char *name2 = drmGetPrimaryDeviceNameFromFd(fd2);
83         int result;
84
85         if (name1 == NULL || name2 == NULL) {
86                 free(name1);
87                 free(name2);
88                 return 0;
89         }
90
91         result = strcmp(name1, name2);
92         free(name1);
93         free(name2);
94
95         return result;
96 }
97
98 /**
99 * Get the authenticated form fd,
100 *
101 * \param   fd   - \c [in]  File descriptor for AMD GPU device
102 * \param   auth - \c [out] Pointer to output the fd is authenticated or not
103 *                          A render node fd, output auth = 0
104 *                          A legacy fd, get the authenticated for compatibility root
105 *
106 * \return   0 on success\n
107 *          >0 - AMD specific error code\n
108 *          <0 - Negative POSIX Error code
109 */
110 static int amdgpu_get_auth(int fd, int *auth)
111 {
112         int r = 0;
113         drm_client_t client = {};
114
115         if (drmGetNodeTypeFromFd(fd) == DRM_NODE_RENDER)
116                 *auth = 0;
117         else {
118                 client.idx = 0;
119                 r = drmIoctl(fd, DRM_IOCTL_GET_CLIENT, &client);
120                 if (!r)
121                         *auth = client.auth;
122         }
123         return r;
124 }
125
126 int amdgpu_device_initialize(int fd,
127                              uint32_t *major_version,
128                              uint32_t *minor_version,
129                              amdgpu_device_handle *device_handle)
130 {
131         struct amdgpu_device *dev;
132         drmVersionPtr version;
133         int r;
134         int flag_auth = 0;
135         int flag_authexist=0;
136         uint32_t accel_working = 0;
137
138         *device_handle = NULL;
139
140         pthread_mutex_lock(&fd_mutex);
141         if (!fd_tab)
142                 fd_tab = util_hash_table_create(fd_hash, fd_compare);
143         r = amdgpu_get_auth(fd, &flag_auth);
144         if (r) {
145                 pthread_mutex_unlock(&fd_mutex);
146                 return r;
147         }
148         dev = util_hash_table_get(fd_tab, UINT_TO_PTR(fd));
149         if (dev) {
150                 r = amdgpu_get_auth(dev->fd, &flag_authexist);
151                 if (r) {
152                         pthread_mutex_unlock(&fd_mutex);
153                         return r;
154                 }
155                 if ((flag_auth) && (!flag_authexist)) {
156                         dev->flink_fd = fd;
157                 }
158                 *major_version = dev->major_version;
159                 *minor_version = dev->minor_version;
160                 amdgpu_device_reference(device_handle, dev);
161                 pthread_mutex_unlock(&fd_mutex);
162                 return 0;
163         }
164
165         dev = calloc(1, sizeof(struct amdgpu_device));
166         if (!dev) {
167                 pthread_mutex_unlock(&fd_mutex);
168                 return -ENOMEM;
169         }
170
171         atomic_set(&dev->refcount, 1);
172
173         version = drmGetVersion(fd);
174         if (version->version_major != 3) {
175                 fprintf(stderr, "%s: DRM version is %d.%d.%d but this driver is "
176                         "only compatible with 3.x.x.\n",
177                         __func__,
178                         version->version_major,
179                         version->version_minor,
180                         version->version_patchlevel);
181                 drmFreeVersion(version);
182                 r = -EBADF;
183                 goto cleanup;
184         }
185
186         dev->fd = fd;
187         dev->flink_fd = fd;
188         dev->major_version = version->version_major;
189         dev->minor_version = version->version_minor;
190         drmFreeVersion(version);
191
192         dev->bo_flink_names = util_hash_table_create(handle_hash,
193                                                      handle_compare);
194         dev->bo_handles = util_hash_table_create(handle_hash, handle_compare);
195         pthread_mutex_init(&dev->bo_table_mutex, NULL);
196
197         /* Check if acceleration is working. */
198         r = amdgpu_query_info(dev, AMDGPU_INFO_ACCEL_WORKING, 4, &accel_working);
199         if (r)
200                 goto cleanup;
201         if (!accel_working) {
202                 r = -EBADF;
203                 goto cleanup;
204         }
205
206         r = amdgpu_query_gpu_info_init(dev);
207         if (r)
208                 goto cleanup;
209
210         dev->vamgr = amdgpu_vamgr_get_global(dev);
211
212         *major_version = dev->major_version;
213         *minor_version = dev->minor_version;
214         *device_handle = dev;
215         util_hash_table_set(fd_tab, UINT_TO_PTR(fd), dev);
216         pthread_mutex_unlock(&fd_mutex);
217
218         return 0;
219
220 cleanup:
221         free(dev);
222         pthread_mutex_unlock(&fd_mutex);
223         return r;
224 }
225
226 void amdgpu_device_free_internal(amdgpu_device_handle dev)
227 {
228         amdgpu_vamgr_reference(&dev->vamgr, NULL);
229         util_hash_table_destroy(dev->bo_flink_names);
230         util_hash_table_destroy(dev->bo_handles);
231         pthread_mutex_destroy(&dev->bo_table_mutex);
232         util_hash_table_remove(fd_tab, UINT_TO_PTR(dev->fd));
233         free(dev);
234 }
235
236 int amdgpu_device_deinitialize(amdgpu_device_handle dev)
237 {
238         amdgpu_device_reference(&dev, NULL);
239         return 0;
240 }
241
242 void amdgpu_device_reference(struct amdgpu_device **dst,
243                              struct amdgpu_device *src)
244 {
245         if (update_references(&(*dst)->refcount, &src->refcount))
246                 amdgpu_device_free_internal(*dst);
247         *dst = src;
248 }