vc4: add hash table look-up for exported dmabufs
[mesa.git] / src / gallium / drivers / vc4 / vc4_bufmgr.h
1 /*
2 * Copyright © 2014 Broadcom
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #ifndef VC4_BUFMGR_H
25 #define VC4_BUFMGR_H
26
27 #include <stdint.h>
28 #include "util/u_hash_table.h"
29 #include "util/u_inlines.h"
30 #include "vc4_qir.h"
31
32 struct vc4_context;
33
34 struct vc4_bo {
35 struct pipe_reference reference;
36 struct vc4_screen *screen;
37 void *map;
38 const char *name;
39 uint32_t handle;
40 uint32_t size;
41
42 #ifdef USE_VC4_SIMULATOR
43 void *simulator_winsys_map;
44 uint32_t simulator_winsys_stride;
45 #endif
46
47 /** Entry in the linked list of buffers freed, by age. */
48 struct list_head time_list;
49 /** Entry in the per-page-count linked list of buffers freed (by age). */
50 struct list_head size_list;
51 /** Approximate second when the bo was freed. */
52 time_t free_time;
53 /**
54 * Whether only our process has a reference to the BO (meaning that
55 * it's safe to reuse it in the BO cache).
56 */
57 bool private;
58 };
59
60 struct vc4_bo *vc4_bo_alloc(struct vc4_screen *screen, uint32_t size,
61 const char *name);
62 struct vc4_bo *vc4_bo_alloc_shader(struct vc4_screen *screen, const void *data,
63 uint32_t size);
64 void vc4_bo_last_unreference(struct vc4_bo *bo);
65 void vc4_bo_last_unreference_locked_timed(struct vc4_bo *bo, time_t time);
66 struct vc4_bo *vc4_bo_open_name(struct vc4_screen *screen, uint32_t name,
67 uint32_t winsys_stride);
68 struct vc4_bo *vc4_bo_open_dmabuf(struct vc4_screen *screen, int fd,
69 uint32_t winsys_stride);
70 bool vc4_bo_flink(struct vc4_bo *bo, uint32_t *name);
71 int vc4_bo_get_dmabuf(struct vc4_bo *bo);
72
73 static inline void
74 vc4_bo_set_reference(struct vc4_bo **old_bo, struct vc4_bo *new_bo)
75 {
76 if (pipe_reference(&(*old_bo)->reference, &new_bo->reference))
77 vc4_bo_last_unreference(*old_bo);
78 *old_bo = new_bo;
79 }
80
81 static inline struct vc4_bo *
82 vc4_bo_reference(struct vc4_bo *bo)
83 {
84 pipe_reference(NULL, &bo->reference);
85 return bo;
86 }
87
88 static inline void
89 vc4_bo_unreference(struct vc4_bo **bo)
90 {
91 struct vc4_screen *screen;
92 if (!*bo)
93 return;
94
95 if ((*bo)->private) {
96 /* Avoid the mutex for private BOs */
97 if (pipe_reference(&(*bo)->reference, NULL))
98 vc4_bo_last_unreference(*bo);
99 } else {
100 screen = (*bo)->screen;
101 pipe_mutex_lock(screen->bo_handles_mutex);
102
103 if (pipe_reference(&(*bo)->reference, NULL)) {
104 util_hash_table_remove(screen->bo_handles,
105 (void *)(uintptr_t)(*bo)->handle);
106 vc4_bo_last_unreference(*bo);
107 }
108
109 pipe_mutex_unlock(screen->bo_handles_mutex);
110 }
111
112 *bo = NULL;
113 }
114
115 static inline void
116 vc4_bo_unreference_locked_timed(struct vc4_bo **bo, time_t time)
117 {
118 if (!*bo)
119 return;
120
121 if (pipe_reference(&(*bo)->reference, NULL))
122 vc4_bo_last_unreference_locked_timed(*bo, time);
123 *bo = NULL;
124 }
125
126 void *
127 vc4_bo_map(struct vc4_bo *bo);
128
129 void *
130 vc4_bo_map_unsynchronized(struct vc4_bo *bo);
131
132 bool
133 vc4_bo_wait(struct vc4_bo *bo, uint64_t timeout_ns, const char *reason);
134
135 bool
136 vc4_wait_seqno(struct vc4_screen *screen, uint64_t seqno, uint64_t timeout_ns,
137 const char *reason);
138
139 void
140 vc4_bufmgr_destroy(struct pipe_screen *pscreen);
141
142 #endif /* VC4_BUFMGR_H */
143