aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Deucher2015-04-20 11:04:22 -0500
committerAlex Deucher2015-08-05 12:47:48 -0500
commit09361395363805b5892d48d7bc10cf717e4d2927 (patch)
treec81192a93843d2f9af16df2b9ec71a66579314d5 /amdgpu/amdgpu_device.c
parenta3c89dda5b519f6b217bebe97f6795b820931b78 (diff)
downloadexternal-libdrm-09361395363805b5892d48d7bc10cf717e4d2927.tar.gz
external-libdrm-09361395363805b5892d48d7bc10cf717e4d2927.tar.xz
external-libdrm-09361395363805b5892d48d7bc10cf717e4d2927.zip
drm: add libdrm_amdgpu (v7)
This is the new ioctl wrapper used by the new admgpu driver. It's primarily used by xf86-video-amdgpu and mesa. v2: fix amdgpu_drm.h install v3: Integrate some of the sugestions from Emil: clean up Makefile.am, configure.ac capitalize header guards fix _FILE_OFFSET_BITS with config.h use drm_mmap/drm_munmap Remove unused ARRAY_SIZE macro use shared list implementation use shared math implementation use drmGetNodeTypeFromFd helper v4: remove unused tiling defines v5: include amdgpu.h in Makefile.am v6: update amdgpu_drm.h v7: libdrm.h -> libdrm_macros.h Signed-off-by: Alex Deucher <alexander.deucher@amd.com>
Diffstat (limited to 'amdgpu/amdgpu_device.c')
-rw-r--r--amdgpu/amdgpu_device.c241
1 files changed, 241 insertions, 0 deletions
diff --git a/amdgpu/amdgpu_device.c b/amdgpu/amdgpu_device.c
new file mode 100644
index 00000000..c610fd38
--- /dev/null
+++ b/amdgpu/amdgpu_device.c
@@ -0,0 +1,241 @@
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#define RENDERNODE_MINOR_MASK 0xff7f
46
47pthread_mutex_t fd_mutex = PTHREAD_MUTEX_INITIALIZER;
48static struct util_hash_table *fd_tab;
49
50static unsigned handle_hash(void *key)
51{
52 return PTR_TO_UINT(key);
53}
54
55static int handle_compare(void *key1, void *key2)
56{
57 return PTR_TO_UINT(key1) != PTR_TO_UINT(key2);
58}
59
60static unsigned fd_hash(void *key)
61{
62 int fd = PTR_TO_UINT(key);
63 struct stat stat;
64 fstat(fd, &stat);
65
66 if (!S_ISCHR(stat.st_mode))
67 return stat.st_dev ^ stat.st_ino;
68 else
69 return stat.st_dev ^ (stat.st_rdev & RENDERNODE_MINOR_MASK);
70}
71
72static int fd_compare(void *key1, void *key2)
73{
74 int fd1 = PTR_TO_UINT(key1);
75 int fd2 = PTR_TO_UINT(key2);
76 struct stat stat1, stat2;
77 fstat(fd1, &stat1);
78 fstat(fd2, &stat2);
79
80 if (!S_ISCHR(stat1.st_mode) || !S_ISCHR(stat2.st_mode))
81 return stat1.st_dev != stat2.st_dev ||
82 stat1.st_ino != stat2.st_ino;
83 else
84 return major(stat1.st_rdev) != major(stat2.st_rdev) ||
85 (minor(stat1.st_rdev) & RENDERNODE_MINOR_MASK) !=
86 (minor(stat2.st_rdev) & RENDERNODE_MINOR_MASK);
87}
88
89/**
90* Get the authenticated form fd,
91*
92* \param fd - \c [in] File descriptor for AMD GPU device
93* \param auth - \c [out] Pointer to output the fd is authenticated or not
94* A render node fd, output auth = 0
95* A legacy fd, get the authenticated for compatibility root
96*
97* \return 0 on success\n
98* >0 - AMD specific error code\n
99* <0 - Negative POSIX Error code
100*/
101static int amdgpu_get_auth(int fd, int *auth)
102{
103 int r = 0;
104 drm_client_t client;
105
106 if (drmGetNodeTypeFromFd(fd) == DRM_NODE_RENDER)
107 *auth = 0;
108 else {
109 client.idx = 0;
110 r = drmIoctl(fd, DRM_IOCTL_GET_CLIENT, &client);
111 if (!r)
112 *auth = client.auth;
113 }
114 return r;
115}
116
117int amdgpu_device_initialize(int fd,
118 uint32_t *major_version,
119 uint32_t *minor_version,
120 amdgpu_device_handle *device_handle)
121{
122 struct amdgpu_device *dev;
123 drmVersionPtr version;
124 int r;
125 int flag_auth = 0;
126 int flag_authexist=0;
127 uint32_t accel_working;
128
129 *device_handle = NULL;
130
131 pthread_mutex_lock(&fd_mutex);
132 if (!fd_tab)
133 fd_tab = util_hash_table_create(fd_hash, fd_compare);
134 r = amdgpu_get_auth(fd, &flag_auth);
135 if (r) {
136 pthread_mutex_unlock(&fd_mutex);
137 return r;
138 }
139 dev = util_hash_table_get(fd_tab, UINT_TO_PTR(fd));
140 if (dev) {
141 r = amdgpu_get_auth(dev->fd, &flag_authexist);
142 if (r) {
143 pthread_mutex_unlock(&fd_mutex);
144 return r;
145 }
146 if ((flag_auth) && (!flag_authexist)) {
147 dev->flink_fd = fd;
148 }
149 *major_version = dev->major_version;
150 *minor_version = dev->minor_version;
151 amdgpu_device_reference(device_handle, dev);
152 pthread_mutex_unlock(&fd_mutex);
153 return 0;
154 }
155
156 dev = calloc(1, sizeof(struct amdgpu_device));
157 if (!dev) {
158 pthread_mutex_unlock(&fd_mutex);
159 return -ENOMEM;
160 }
161
162 atomic_set(&dev->refcount, 1);
163
164 version = drmGetVersion(fd);
165 if (version->version_major != 3) {
166 fprintf(stderr, "%s: DRM version is %d.%d.%d but this driver is "
167 "only compatible with 3.x.x.\n",
168 __func__,
169 version->version_major,
170 version->version_minor,
171 version->version_patchlevel);
172 drmFreeVersion(version);
173 r = -EBADF;
174 goto cleanup;
175 }
176
177 dev->fd = fd;
178 dev->flink_fd = fd;
179 dev->major_version = version->version_major;
180 dev->minor_version = version->version_minor;
181 drmFreeVersion(version);
182
183 dev->bo_flink_names = util_hash_table_create(handle_hash,
184 handle_compare);
185 dev->bo_handles = util_hash_table_create(handle_hash, handle_compare);
186 dev->bo_vas = util_hash_table_create(handle_hash, handle_compare);
187 pthread_mutex_init(&dev->bo_table_mutex, NULL);
188
189 /* Check if acceleration is working. */
190 r = amdgpu_query_info(dev, AMDGPU_INFO_ACCEL_WORKING, 4, &accel_working);
191 if (r)
192 goto cleanup;
193 if (!accel_working) {
194 r = -EBADF;
195 goto cleanup;
196 }
197
198 r = amdgpu_query_gpu_info_init(dev);
199 if (r)
200 goto cleanup;
201
202 amdgpu_vamgr_init(dev);
203
204 *major_version = dev->major_version;
205 *minor_version = dev->minor_version;
206 *device_handle = dev;
207 util_hash_table_set(fd_tab, UINT_TO_PTR(fd), dev);
208 pthread_mutex_unlock(&fd_mutex);
209
210 return 0;
211
212cleanup:
213 free(dev);
214 pthread_mutex_unlock(&fd_mutex);
215 return r;
216}
217
218void amdgpu_device_free_internal(amdgpu_device_handle dev)
219{
220 util_hash_table_destroy(dev->bo_flink_names);
221 util_hash_table_destroy(dev->bo_handles);
222 util_hash_table_destroy(dev->bo_vas);
223 pthread_mutex_destroy(&dev->bo_table_mutex);
224 pthread_mutex_destroy(&(dev->vamgr.bo_va_mutex));
225 util_hash_table_remove(fd_tab, UINT_TO_PTR(dev->fd));
226 free(dev);
227}
228
229int amdgpu_device_deinitialize(amdgpu_device_handle dev)
230{
231 amdgpu_device_reference(&dev, NULL);
232 return 0;
233}
234
235void amdgpu_device_reference(struct amdgpu_device **dst,
236 struct amdgpu_device *src)
237{
238 if (update_references(&(*dst)->refcount, &src->refcount))
239 amdgpu_device_free_internal(*dst);
240 *dst = src;
241}