OSDN Git Service

freedreno: add support for msm drm
[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 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <unistd.h>
32
33 #include "freedreno_drmif.h"
34 #include "freedreno_priv.h"
35
36 static pthread_mutex_t table_lock = PTHREAD_MUTEX_INITIALIZER;
37 static void * dev_table;
38
39 struct fd_device * kgsl_device_new(int fd);
40 struct fd_device * msm_device_new(int fd);
41
42 static struct fd_device * fd_device_new_impl(int fd)
43 {
44         struct fd_device *dev;
45         drmVersionPtr version;
46
47         /* figure out if we are kgsl or msm drm driver: */
48         version = drmGetVersion(fd);
49         if (!version) {
50                 ERROR_MSG("cannot get version: %s", strerror(errno));
51                 return NULL;
52         }
53
54         if (!strcmp(version->name, "kgsl")) {
55                 DEBUG_MSG("kgsl DRM device");
56                 dev = kgsl_device_new(fd);
57         } else if (!strcmp(version->name, "msm")) {
58                 DEBUG_MSG("msm DRM device");
59                 dev = msm_device_new(fd);
60         } else {
61                 ERROR_MSG("unknown device: %s", version->name);
62                 dev = NULL;
63         }
64
65         if (!dev)
66                 return NULL;
67
68         atomic_set(&dev->refcnt, 1);
69         dev->fd = fd;
70         dev->handle_table = drmHashCreate();
71         dev->name_table = drmHashCreate();
72
73         return dev;
74 }
75
76 struct fd_device * fd_device_new(int fd)
77 {
78         struct fd_device *dev = NULL;
79         int key = fd;
80
81         pthread_mutex_lock(&table_lock);
82
83         if (!dev_table)
84                 dev_table = drmHashCreate();
85
86         if (drmHashLookup(dev_table, key, (void **)&dev)) {
87                 dev = fd_device_new_impl(fd);
88                 if (dev)
89                         drmHashInsert(dev_table, key, dev);
90         } else {
91                 dev = fd_device_ref(dev);
92         }
93
94         pthread_mutex_unlock(&table_lock);
95
96         return dev;
97 }
98
99 struct fd_device * fd_device_ref(struct fd_device *dev)
100 {
101         atomic_inc(&dev->refcnt);
102         return dev;
103 }
104
105 void fd_device_del(struct fd_device *dev)
106 {
107         if (!atomic_dec_and_test(&dev->refcnt))
108                 return;
109         pthread_mutex_lock(&table_lock);
110         drmHashDestroy(dev->handle_table);
111         drmHashDestroy(dev->name_table);
112         drmHashDelete(dev_table, dev->fd);
113         pthread_mutex_unlock(&table_lock);
114         dev->funcs->destroy(dev);
115 }