1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include "pipe/p_context.h"
#include "pipe/p_state.h"
#include "util/format/u_format.h"
#include "util/os_file.h"
#include "util/u_memory.h"
#include "util/u_inlines.h"
#include "util/u_hash_table.h"
#include "util/u_pointer.h"
#include "os/os_thread.h"
#include "nouveau_drm_public.h"
#include "nouveau/nouveau_winsys.h"
#include "nouveau/nouveau_screen.h"
#include <nvif/class.h>
#include <nvif/cl0080.h>
static struct hash_table *fd_tab = NULL;
static mtx_t nouveau_screen_mutex = _MTX_INITIALIZER_NP;
bool nouveau_drm_screen_unref(struct nouveau_screen *screen)
{
int ret;
if (screen->refcount == -1)
return true;
mtx_lock(&nouveau_screen_mutex);
ret = --screen->refcount;
assert(ret >= 0);
if (ret == 0)
_mesa_hash_table_remove_key(fd_tab, intptr_to_pointer(screen->drm->fd));
mtx_unlock(&nouveau_screen_mutex);
return ret == 0;
}
PUBLIC struct pipe_screen *
nouveau_drm_screen_create(int fd)
{
struct nouveau_drm *drm = NULL;
struct nouveau_device *dev = NULL;
struct nouveau_screen *(*init)(struct nouveau_device *);
struct nouveau_screen *screen = NULL;
int ret, dupfd;
mtx_lock(&nouveau_screen_mutex);
if (!fd_tab) {
fd_tab = util_hash_table_create_fd_keys();
if (!fd_tab) {
mtx_unlock(&nouveau_screen_mutex);
return NULL;
}
}
screen = util_hash_table_get(fd_tab, intptr_to_pointer(fd));
if (screen) {
screen->refcount++;
mtx_unlock(&nouveau_screen_mutex);
return &screen->base;
}
/* Since the screen re-use is based on the device node and not the fd,
* create a copy of the fd to be owned by the device. Otherwise a
* scenario could occur where two screens are created, and the first
* one is shut down, along with the fd being closed. The second
* (identical) screen would now have a reference to the closed fd. We
* avoid this by duplicating the original fd. Note that
* nouveau_device_wrap does not close the fd in case of a device
* creation error.
*/
dupfd = os_dupfd_cloexec(fd);
ret = nouveau_drm_new(dupfd, &drm);
if (ret)
goto err;
ret = nouveau_device_new(&drm->client, NV_DEVICE,
&(struct nv_device_v0) {
.device = ~0ULL,
}, sizeof(struct nv_device_v0), &dev);
if (ret)
goto err;
switch (dev->chipset & ~0xf) {
case 0x30:
case 0x40:
case 0x60:
init = nv30_screen_create;
break;
case 0x50:
case 0x80:
case 0x90:
case 0xa0:
init = nv50_screen_create;
break;
case 0xc0:
case 0xd0:
case 0xe0:
case 0xf0:
case 0x100:
case 0x110:
case 0x120:
case 0x130:
case 0x140:
case 0x160:
init = nvc0_screen_create;
break;
default:
debug_printf("%s: unknown chipset nv%02x\n", __func__,
dev->chipset);
goto err;
}
screen = init(dev);
if (!screen || !screen->base.context_create)
goto err;
/* Use dupfd in hash table, to avoid errors if the original fd gets
* closed by its owner. The hash key needs to live at least as long as
* the screen.
*/
_mesa_hash_table_insert(fd_tab, intptr_to_pointer(dupfd), screen);
screen->refcount = 1;
mtx_unlock(&nouveau_screen_mutex);
return &screen->base;
err:
if (screen) {
screen->base.destroy(&screen->base);
} else {
nouveau_device_del(&dev);
nouveau_drm_del(&drm);
close(dupfd);
}
mtx_unlock(&nouveau_screen_mutex);
return NULL;
}
|