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 VC5_BUFMGR_H
25 #define VC5_BUFMGR_H
26
27 #include <stdint.h>
28 #include "util/u_hash_table.h"
29 #include "util/u_inlines.h"
30 #include "util/list.h"
31 #include "vc5_screen.h"
32
33 struct vc5_context;
34
35 struct vc5_bo {
36 struct pipe_reference reference;
37 struct vc5_screen *screen;
38 void *map;
39 const char *name;
40 uint32_t handle;
41 uint32_t size;
42
43 /* Address of the BO in our page tables. */
44 uint32_t offset;
45
46 /** Entry in the linked list of buffers freed, by age. */
47 struct list_head time_list;
48 /** Entry in the per-page-count linked list of buffers freed (by age). */
49 struct list_head size_list;
50 /** Approximate second when the bo was freed. */
51 time_t free_time;
52 /**
53 * Whether only our process has a reference to the BO (meaning that
54 * it's safe to reuse it in the BO cache).
55 */
56 bool private;
57 };
58
59 struct vc5_bo *vc5_bo_alloc(struct vc5_screen *screen, uint32_t size,
60 const char *name);
61 void vc5_bo_last_unreference(struct vc5_bo *bo);
62 void vc5_bo_last_unreference_locked_timed(struct vc5_bo *bo, time_t time);
63 struct vc5_bo *vc5_bo_open_name(struct vc5_screen *screen, uint32_t name,
64 uint32_t winsys_stride);
65 struct vc5_bo *vc5_bo_open_dmabuf(struct vc5_screen *screen, int fd,
66 uint32_t winsys_stride);
67 bool vc5_bo_flink(struct vc5_bo *bo, uint32_t *name);
68 int vc5_bo_get_dmabuf(struct vc5_bo *bo);
69
70 static inline void
vc5_bo_set_reference(struct vc5_bo ** old_bo,struct vc5_bo * new_bo)71 vc5_bo_set_reference(struct vc5_bo **old_bo, struct vc5_bo *new_bo)
72 {
73 if (pipe_reference(&(*old_bo)->reference, &new_bo->reference))
74 vc5_bo_last_unreference(*old_bo);
75 *old_bo = new_bo;
76 }
77
78 static inline struct vc5_bo *
vc5_bo_reference(struct vc5_bo * bo)79 vc5_bo_reference(struct vc5_bo *bo)
80 {
81 pipe_reference(NULL, &bo->reference);
82 return bo;
83 }
84
85 static inline void
vc5_bo_unreference(struct vc5_bo ** bo)86 vc5_bo_unreference(struct vc5_bo **bo)
87 {
88 struct vc5_screen *screen;
89 if (!*bo)
90 return;
91
92 if ((*bo)->private) {
93 /* Avoid the mutex for private BOs */
94 if (pipe_reference(&(*bo)->reference, NULL))
95 vc5_bo_last_unreference(*bo);
96 } else {
97 screen = (*bo)->screen;
98 mtx_lock(&screen->bo_handles_mutex);
99
100 if (pipe_reference(&(*bo)->reference, NULL)) {
101 util_hash_table_remove(screen->bo_handles,
102 (void *)(uintptr_t)(*bo)->handle);
103 vc5_bo_last_unreference(*bo);
104 }
105
106 mtx_unlock(&screen->bo_handles_mutex);
107 }
108
109 *bo = NULL;
110 }
111
112 static inline void
vc5_bo_unreference_locked_timed(struct vc5_bo ** bo,time_t time)113 vc5_bo_unreference_locked_timed(struct vc5_bo **bo, time_t time)
114 {
115 if (!*bo)
116 return;
117
118 if (pipe_reference(&(*bo)->reference, NULL))
119 vc5_bo_last_unreference_locked_timed(*bo, time);
120 *bo = NULL;
121 }
122
123 void *
124 vc5_bo_map(struct vc5_bo *bo);
125
126 void *
127 vc5_bo_map_unsynchronized(struct vc5_bo *bo);
128
129 bool
130 vc5_bo_wait(struct vc5_bo *bo, uint64_t timeout_ns, const char *reason);
131
132 bool
133 vc5_wait_seqno(struct vc5_screen *screen, uint64_t seqno, uint64_t timeout_ns,
134 const char *reason);
135
136 void
137 vc5_bufmgr_destroy(struct pipe_screen *pscreen);
138
139 #endif /* VC5_BUFMGR_H */
140
141