4 #include "pipe/p_context.h"
5 #include "pipe/p_state.h"
6 #include "util/format/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 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 mtx_lock(&nouveau_screen_mutex
);
31 ret
= --screen
->refcount
;
34 _mesa_hash_table_remove_key(fd_tab
, intptr_to_pointer(screen
->drm
->fd
));
35 mtx_unlock(&nouveau_screen_mutex
);
39 PUBLIC
struct pipe_screen
*
40 nouveau_drm_screen_create(int fd
)
42 struct nouveau_drm
*drm
= NULL
;
43 struct nouveau_device
*dev
= NULL
;
44 struct nouveau_screen
*(*init
)(struct nouveau_device
*);
45 struct nouveau_screen
*screen
= NULL
;
48 mtx_lock(&nouveau_screen_mutex
);
50 fd_tab
= util_hash_table_create_fd_keys();
52 mtx_unlock(&nouveau_screen_mutex
);
57 screen
= util_hash_table_get(fd_tab
, intptr_to_pointer(fd
));
60 mtx_unlock(&nouveau_screen_mutex
);
64 /* Since the screen re-use is based on the device node and not the fd,
65 * create a copy of the fd to be owned by the device. Otherwise a
66 * scenario could occur where two screens are created, and the first
67 * one is shut down, along with the fd being closed. The second
68 * (identical) screen would now have a reference to the closed fd. We
69 * avoid this by duplicating the original fd. Note that
70 * nouveau_device_wrap does not close the fd in case of a device
73 dupfd
= fcntl(fd
, F_DUPFD_CLOEXEC
, 3);
75 ret
= nouveau_drm_new(dupfd
, &drm
);
79 ret
= nouveau_device_new(&drm
->client
, NV_DEVICE
,
80 &(struct nv_device_v0
) {
82 }, sizeof(struct nv_device_v0
), &dev
);
86 switch (dev
->chipset
& ~0xf) {
90 init
= nv30_screen_create
;
96 init
= nv50_screen_create
;
106 init
= nvc0_screen_create
;
109 debug_printf("%s: unknown chipset nv%02x\n", __func__
,
115 if (!screen
|| !screen
->base
.context_create
)
118 /* Use dupfd in hash table, to avoid errors if the original fd gets
119 * closed by its owner. The hash key needs to live at least as long as
122 _mesa_hash_table_insert(fd_tab
, intptr_to_pointer(dupfd
), screen
);
123 screen
->refcount
= 1;
124 mtx_unlock(&nouveau_screen_mutex
);
125 return &screen
->base
;
129 screen
->base
.destroy(&screen
->base
);
131 nouveau_device_del(&dev
);
132 nouveau_drm_del(&drm
);
135 mtx_unlock(&nouveau_screen_mutex
);