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 "util/u_pointer.h"
11 #include "os/os_thread.h"
13 #include "nouveau_drm_public.h"
15 #include "nouveau/nouveau_winsys.h"
16 #include "nouveau/nouveau_screen.h"
18 #include <nvif/class.h>
19 #include <nvif/cl0080.h>
21 static struct hash_table
*fd_tab
= NULL
;
23 static mtx_t nouveau_screen_mutex
= _MTX_INITIALIZER_NP
;
25 bool nouveau_drm_screen_unref(struct nouveau_screen
*screen
)
28 if (screen
->refcount
== -1)
31 mtx_lock(&nouveau_screen_mutex
);
32 ret
= --screen
->refcount
;
35 _mesa_hash_table_remove_key(fd_tab
, intptr_to_pointer(screen
->drm
->fd
));
36 mtx_unlock(&nouveau_screen_mutex
);
40 PUBLIC
struct pipe_screen
*
41 nouveau_drm_screen_create(int fd
)
43 struct nouveau_drm
*drm
= NULL
;
44 struct nouveau_device
*dev
= NULL
;
45 struct nouveau_screen
*(*init
)(struct nouveau_device
*);
46 struct nouveau_screen
*screen
= NULL
;
49 mtx_lock(&nouveau_screen_mutex
);
51 fd_tab
= util_hash_table_create_fd_keys();
53 mtx_unlock(&nouveau_screen_mutex
);
58 screen
= util_hash_table_get(fd_tab
, intptr_to_pointer(fd
));
61 mtx_unlock(&nouveau_screen_mutex
);
65 /* Since the screen re-use is based on the device node and not the fd,
66 * create a copy of the fd to be owned by the device. Otherwise a
67 * scenario could occur where two screens are created, and the first
68 * one is shut down, along with the fd being closed. The second
69 * (identical) screen would now have a reference to the closed fd. We
70 * avoid this by duplicating the original fd. Note that
71 * nouveau_device_wrap does not close the fd in case of a device
74 dupfd
= fcntl(fd
, F_DUPFD_CLOEXEC
, 3);
76 ret
= nouveau_drm_new(dupfd
, &drm
);
80 ret
= nouveau_device_new(&drm
->client
, NV_DEVICE
,
81 &(struct nv_device_v0
) {
83 }, sizeof(struct nv_device_v0
), &dev
);
87 switch (dev
->chipset
& ~0xf) {
91 init
= nv30_screen_create
;
97 init
= nv50_screen_create
;
107 init
= nvc0_screen_create
;
110 debug_printf("%s: unknown chipset nv%02x\n", __func__
,
116 if (!screen
|| !screen
->base
.context_create
)
119 /* Use dupfd in hash table, to avoid errors if the original fd gets
120 * closed by its owner. The hash key needs to live at least as long as
123 _mesa_hash_table_insert(fd_tab
, intptr_to_pointer(dupfd
), screen
);
124 screen
->refcount
= 1;
125 mtx_unlock(&nouveau_screen_mutex
);
126 return &screen
->base
;
130 screen
->base
.destroy(&screen
->base
);
132 nouveau_device_del(&dev
);
133 nouveau_drm_del(&drm
);
136 mtx_unlock(&nouveau_screen_mutex
);