4 #include "pipe/p_context.h"
5 #include "pipe/p_state.h"
6 #include "util/u_format.h"
7 #include "util/u_memory.h"
8 #include "util/u_inlines.h"
9 #include "util/u_hash_table.h"
10 #include "os/os_thread.h"
12 #include "nouveau_drm_public.h"
14 #include "nouveau/nouveau_winsys.h"
15 #include "nouveau/nouveau_screen.h"
17 #include <nvif/class.h>
18 #include <nvif/cl0080.h>
20 static struct util_hash_table
*fd_tab
= NULL
;
22 static mtx_t nouveau_screen_mutex
= _MTX_INITIALIZER_NP
;
24 bool nouveau_drm_screen_unref(struct nouveau_screen
*screen
)
27 if (screen
->refcount
== -1)
30 pipe_mutex_lock(nouveau_screen_mutex
);
31 ret
= --screen
->refcount
;
34 util_hash_table_remove(fd_tab
, intptr_to_pointer(screen
->drm
->fd
));
35 pipe_mutex_unlock(nouveau_screen_mutex
);
39 static unsigned hash_fd(void *key
)
41 int fd
= pointer_to_intptr(key
);
45 return stat
.st_dev
^ stat
.st_ino
^ stat
.st_rdev
;
48 static int compare_fd(void *key1
, void *key2
)
50 int fd1
= pointer_to_intptr(key1
);
51 int fd2
= pointer_to_intptr(key2
);
52 struct stat stat1
, stat2
;
56 return stat1
.st_dev
!= stat2
.st_dev
||
57 stat1
.st_ino
!= stat2
.st_ino
||
58 stat1
.st_rdev
!= stat2
.st_rdev
;
61 PUBLIC
struct pipe_screen
*
62 nouveau_drm_screen_create(int fd
)
64 struct nouveau_drm
*drm
= NULL
;
65 struct nouveau_device
*dev
= NULL
;
66 struct nouveau_screen
*(*init
)(struct nouveau_device
*);
67 struct nouveau_screen
*screen
= NULL
;
70 pipe_mutex_lock(nouveau_screen_mutex
);
72 fd_tab
= util_hash_table_create(hash_fd
, compare_fd
);
74 pipe_mutex_unlock(nouveau_screen_mutex
);
79 screen
= util_hash_table_get(fd_tab
, intptr_to_pointer(fd
));
82 pipe_mutex_unlock(nouveau_screen_mutex
);
86 /* Since the screen re-use is based on the device node and not the fd,
87 * create a copy of the fd to be owned by the device. Otherwise a
88 * scenario could occur where two screens are created, and the first
89 * one is shut down, along with the fd being closed. The second
90 * (identical) screen would now have a reference to the closed fd. We
91 * avoid this by duplicating the original fd. Note that
92 * nouveau_device_wrap does not close the fd in case of a device
95 dupfd
= fcntl(fd
, F_DUPFD_CLOEXEC
, 3);
97 ret
= nouveau_drm_new(dupfd
, &drm
);
101 ret
= nouveau_device_new(&drm
->client
, NV_DEVICE
,
102 &(struct nv_device_v0
) {
104 }, sizeof(struct nv_device_v0
), &dev
);
108 switch (dev
->chipset
& ~0xf) {
112 init
= nv30_screen_create
;
118 init
= nv50_screen_create
;
128 init
= nvc0_screen_create
;
131 debug_printf("%s: unknown chipset nv%02x\n", __func__
,
137 if (!screen
|| !screen
->base
.context_create
)
140 /* Use dupfd in hash table, to avoid errors if the original fd gets
141 * closed by its owner. The hash key needs to live at least as long as
144 util_hash_table_set(fd_tab
, intptr_to_pointer(dupfd
), screen
);
145 screen
->refcount
= 1;
146 pipe_mutex_unlock(nouveau_screen_mutex
);
147 return &screen
->base
;
151 screen
->base
.destroy(&screen
->base
);
153 nouveau_device_del(&dev
);
154 nouveau_drm_del(&drm
);
157 pipe_mutex_unlock(nouveau_screen_mutex
);