freedreno/a5xx: cubemap image fixes
[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 /* This will be read/written by multiple threads without a lock -- you
43 * should take a snapshot and use it to see if you happen to be in the
44 * CL's handles at this position, to make most lookups O(1). It's
45 * volatile to make sure that the compiler doesn't emit multiple loads
46 * from the address, which would make the lookup racy.
47 */
48 volatile uint32_t last_hindex;
49
50 /** Entry in the linked list of buffers freed, by age. */
51 struct list_head time_list;
52 /** Entry in the per-page-count linked list of buffers freed (by age). */
53 struct list_head size_list;
54 /** Approximate second when the bo was freed. */
55 time_t free_time;
56 /**
57 * Whether only our process has a reference to the BO (meaning that
58 * it's safe to reuse it in the BO cache).
59 */
60 bool private;
61 };
62
63 struct vc4_bo *vc4_bo_alloc(struct vc4_screen *screen, uint32_t size,
64 const char *name);
65 struct vc4_bo *vc4_bo_alloc_shader(struct vc4_screen *screen, const void *data,
66 uint32_t size);
67 void vc4_bo_last_unreference(struct vc4_bo *bo);
68 void vc4_bo_last_unreference_locked_timed(struct vc4_bo *bo, time_t time);
69 struct vc4_bo *vc4_bo_open_name(struct vc4_screen *screen, uint32_t name,
70 uint32_t winsys_stride);
71 struct vc4_bo *vc4_bo_open_dmabuf(struct vc4_screen *screen, int fd,
72 uint32_t winsys_stride);
73 bool vc4_bo_flink(struct vc4_bo *bo, uint32_t *name);
74 int vc4_bo_get_dmabuf(struct vc4_bo *bo);
75
76 void vc4_bo_debug_describe(char* buf, const struct vc4_bo *ptr);
77 static inline struct vc4_bo *
78 vc4_bo_reference(struct vc4_bo *bo)
79 {
80 pipe_reference_described(NULL, &bo->reference,
81 (debug_reference_descriptor)
82 vc4_bo_debug_describe);
83 return bo;
84 }
85
86 static inline void
87 vc4_bo_unreference(struct vc4_bo **bo)
88 {
89 struct vc4_screen *screen;
90 if (!*bo)
91 return;
92
93 if ((*bo)->private) {
94 /* Avoid the mutex for private BOs */
95 if (pipe_reference_described(&(*bo)->reference, NULL,
96 (debug_reference_descriptor)
97 vc4_bo_debug_describe)) {
98 vc4_bo_last_unreference(*bo);
99 }
100 } else {
101 screen = (*bo)->screen;
102 mtx_lock(&screen->bo_handles_mutex);
103
104 if (pipe_reference_described(&(*bo)->reference, NULL,
105 (debug_reference_descriptor)
106 vc4_bo_debug_describe)) {
107 util_hash_table_remove(screen->bo_handles,
108 (void *)(uintptr_t)(*bo)->handle);
109 vc4_bo_last_unreference(*bo);
110 }
111
112 mtx_unlock(&screen->bo_handles_mutex);
113 }
114
115 *bo = NULL;
116 }
117
118 static inline void
119 vc4_bo_unreference_locked_timed(struct vc4_bo **bo, time_t time)
120 {
121 if (!*bo)
122 return;
123
124 if (pipe_reference_described(&(*bo)->reference, NULL,
125 (debug_reference_descriptor)
126 vc4_bo_debug_describe)) {
127 vc4_bo_last_unreference_locked_timed(*bo, time);
128 }
129 *bo = NULL;
130 }
131
132 void *
133 vc4_bo_map(struct vc4_bo *bo);
134
135 void *
136 vc4_bo_map_unsynchronized(struct vc4_bo *bo);
137
138 bool
139 vc4_bo_wait(struct vc4_bo *bo, uint64_t timeout_ns, const char *reason);
140
141 bool
142 vc4_wait_seqno(struct vc4_screen *screen, uint64_t seqno, uint64_t timeout_ns,
143 const char *reason);
144
145 void
146 vc4_bo_label(struct vc4_screen *screen, struct vc4_bo *bo, const char *fmt, ...);
147
148 void
149 vc4_bufmgr_destroy(struct pipe_screen *pscreen);
150
151 #endif /* VC4_BUFMGR_H */
152