OSDN Git Service

freedreno: fix double-free on exit
[android-x86/external-libdrm.git] / freedreno / freedreno_device.c
1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
2
3 /*
4  * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the next
14  * paragraph) shall be included in all copies or substantial portions of the
15  * Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  *
25  * Authors:
26  *    Rob Clark <robclark@freedesktop.org>
27  */
28
29 #ifdef HAVE_CONFIG_H
30 # include <config.h>
31 #endif
32
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <unistd.h>
36
37 #include "freedreno_drmif.h"
38 #include "freedreno_priv.h"
39
40 static pthread_mutex_t table_lock = PTHREAD_MUTEX_INITIALIZER;
41
42 struct fd_device * kgsl_device_new(int fd);
43 struct fd_device * msm_device_new(int fd);
44
45 struct fd_device * fd_device_new(int fd)
46 {
47         struct fd_device *dev;
48         drmVersionPtr version;
49
50         /* figure out if we are kgsl or msm drm driver: */
51         version = drmGetVersion(fd);
52         if (!version) {
53                 ERROR_MSG("cannot get version: %s", strerror(errno));
54                 return NULL;
55         }
56
57         if (!strcmp(version->name, "msm")) {
58                 DEBUG_MSG("msm DRM device");
59                 if (version->version_major != 1) {
60                         ERROR_MSG("unsupported version: %u.%u.%u", version->version_major,
61                                 version->version_minor, version->version_patchlevel);
62                         dev = NULL;
63                         goto out;
64                 }
65
66                 dev = msm_device_new(fd);
67                 dev->version = version->version_minor;
68 #ifdef HAVE_FREEDRENO_KGSL
69         } else if (!strcmp(version->name, "kgsl")) {
70                 DEBUG_MSG("kgsl DRM device");
71                 dev = kgsl_device_new(fd);
72 #endif
73         } else {
74                 ERROR_MSG("unknown device: %s", version->name);
75                 dev = NULL;
76         }
77
78 out:
79         drmFreeVersion(version);
80
81         if (!dev)
82                 return NULL;
83
84         atomic_set(&dev->refcnt, 1);
85         dev->fd = fd;
86         dev->handle_table = drmHashCreate();
87         dev->name_table = drmHashCreate();
88         fd_bo_cache_init(&dev->bo_cache, FALSE);
89
90         return dev;
91 }
92
93 /* like fd_device_new() but creates it's own private dup() of the fd
94  * which is close()d when the device is finalized.
95  */
96 struct fd_device * fd_device_new_dup(int fd)
97 {
98         int dup_fd = dup(fd);
99         struct fd_device *dev = fd_device_new(dup_fd);
100         if (dev)
101                 dev->closefd = 1;
102         else
103                 close(dup_fd);
104         return dev;
105 }
106
107 struct fd_device * fd_device_ref(struct fd_device *dev)
108 {
109         atomic_inc(&dev->refcnt);
110         return dev;
111 }
112
113 static void fd_device_del_impl(struct fd_device *dev)
114 {
115         int close_fd = dev->closefd ? dev->fd : -1;
116         fd_bo_cache_cleanup(&dev->bo_cache, 0);
117         drmHashDestroy(dev->handle_table);
118         drmHashDestroy(dev->name_table);
119         dev->funcs->destroy(dev);
120         if (close_fd >= 0)
121                 close(close_fd);
122 }
123
124 drm_private void fd_device_del_locked(struct fd_device *dev)
125 {
126         if (!atomic_dec_and_test(&dev->refcnt))
127                 return;
128         fd_device_del_impl(dev);
129 }
130
131 void fd_device_del(struct fd_device *dev)
132 {
133         if (!atomic_dec_and_test(&dev->refcnt))
134                 return;
135         pthread_mutex_lock(&table_lock);
136         fd_device_del_impl(dev);
137         pthread_mutex_unlock(&table_lock);
138 }
139
140 int fd_device_fd(struct fd_device *dev)
141 {
142         return dev->fd;
143 }
144
145 enum fd_version fd_device_version(struct fd_device *dev)
146 {
147         return dev->version;
148 }