Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/freedreno/decode/buffers.c
4565 views
1
/*
2
* Copyright (c) 2012 Rob Clark <[email protected]>
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 FROM,
20
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
* SOFTWARE.
22
*/
23
24
/*
25
* Helper lib to track gpu buffers contents/address, and map between gpu and
26
* host address while decoding cmdstream/crashdumps
27
*/
28
29
#include <assert.h>
30
#include <stdlib.h>
31
32
#include "util/rb_tree.h"
33
#include "buffers.h"
34
35
struct buffer {
36
struct rb_node node;
37
void *hostptr;
38
unsigned int len;
39
uint64_t gpuaddr;
40
41
/* for 'once' mode, for buffers containing cmdstream keep track per offset
42
* into buffer of which modes it has already been dumped;
43
*/
44
struct {
45
unsigned offset;
46
unsigned dumped_mask;
47
} offsets[64];
48
unsigned noffsets;
49
};
50
51
static struct rb_tree buffers;
52
53
static int
54
buffer_insert_cmp(const struct rb_node *n1, const struct rb_node *n2)
55
{
56
const struct buffer *buf1 = (const struct buffer *)n1;
57
const struct buffer *buf2 = (const struct buffer *)n2;
58
return buf1->gpuaddr - buf2->gpuaddr;
59
}
60
61
static int
62
buffer_search_cmp(const struct rb_node *node, const void *addrptr)
63
{
64
const struct buffer *buf = (const struct buffer *)node;
65
uint64_t gpuaddr = *(uint64_t *)addrptr;
66
if (buf->gpuaddr + buf->len <= gpuaddr)
67
return -1;
68
else if (buf->gpuaddr > gpuaddr)
69
return 1;
70
return 0;
71
}
72
73
static struct buffer *
74
get_buffer(uint64_t gpuaddr)
75
{
76
if (gpuaddr == 0)
77
return NULL;
78
return (struct buffer *)rb_tree_search(&buffers, &gpuaddr,
79
buffer_search_cmp);
80
}
81
82
static int
83
buffer_contains_hostptr(struct buffer *buf, void *hostptr)
84
{
85
return (buf->hostptr <= hostptr) && (hostptr < (buf->hostptr + buf->len));
86
}
87
88
uint64_t
89
gpuaddr(void *hostptr)
90
{
91
rb_tree_foreach(struct buffer, buf, &buffers, node)
92
{
93
if (buffer_contains_hostptr(buf, hostptr))
94
return buf->gpuaddr + (hostptr - buf->hostptr);
95
}
96
return 0;
97
}
98
99
uint64_t
100
gpubaseaddr(uint64_t gpuaddr)
101
{
102
struct buffer *buf = get_buffer(gpuaddr);
103
if (buf)
104
return buf->gpuaddr;
105
else
106
return 0;
107
}
108
109
void *
110
hostptr(uint64_t gpuaddr)
111
{
112
struct buffer *buf = get_buffer(gpuaddr);
113
if (buf)
114
return buf->hostptr + (gpuaddr - buf->gpuaddr);
115
else
116
return 0;
117
}
118
119
unsigned
120
hostlen(uint64_t gpuaddr)
121
{
122
struct buffer *buf = get_buffer(gpuaddr);
123
if (buf)
124
return buf->len + buf->gpuaddr - gpuaddr;
125
else
126
return 0;
127
}
128
129
bool
130
has_dumped(uint64_t gpuaddr, unsigned enable_mask)
131
{
132
if (!gpuaddr)
133
return false;
134
135
struct buffer *b = get_buffer(gpuaddr);
136
if (!b)
137
return false;
138
139
assert(gpuaddr >= b->gpuaddr);
140
unsigned offset = gpuaddr - b->gpuaddr;
141
142
unsigned n = 0;
143
while (n < b->noffsets) {
144
if (offset == b->offsets[n].offset)
145
break;
146
n++;
147
}
148
149
/* if needed, allocate a new offset entry: */
150
if (n == b->noffsets) {
151
b->noffsets++;
152
assert(b->noffsets < ARRAY_SIZE(b->offsets));
153
b->offsets[n].dumped_mask = 0;
154
b->offsets[n].offset = offset;
155
}
156
157
if ((b->offsets[n].dumped_mask & enable_mask) == enable_mask)
158
return true;
159
160
b->offsets[n].dumped_mask |= enable_mask;
161
162
return false;
163
}
164
165
void
166
reset_buffers(void)
167
{
168
rb_tree_foreach_safe(struct buffer, buf, &buffers, node)
169
{
170
rb_tree_remove(&buffers, &buf->node);
171
free(buf->hostptr);
172
free(buf);
173
}
174
}
175
176
/**
177
* Record buffer contents, takes ownership of hostptr (freed in
178
* reset_buffers())
179
*/
180
void
181
add_buffer(uint64_t gpuaddr, unsigned int len, void *hostptr)
182
{
183
struct buffer *buf = get_buffer(gpuaddr);
184
185
if (!buf) {
186
buf = calloc(sizeof(struct buffer), 1);
187
buf->gpuaddr = gpuaddr;
188
rb_tree_insert(&buffers, &buf->node, buffer_insert_cmp);
189
}
190
191
assert(buf->gpuaddr == gpuaddr);
192
193
buf->hostptr = hostptr;
194
buf->len = len;
195
}
196
197