1 /*
2 * Copyright (c) 2012 Rob Clark <robdclark@gmail.com>
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 #include <string.h>
32
33 #include "util/rb_tree.h"
34 #include "buffers.h"
35
36 struct buffer {
37 struct rb_node node;
38 void *hostptr;
39 unsigned int len;
40 uint64_t gpuaddr;
41
42 /* for 'once' mode, for buffers containing cmdstream keep track per offset
43 * into buffer of which modes it has already been dumped;
44 */
45 struct {
46 unsigned offset;
47 unsigned dumped_mask;
48 } offsets[256];
49 unsigned noffsets;
50 };
51
52 static struct rb_tree buffers;
53
54 static int
buffer_insert_cmp(const struct rb_node * n1,const struct rb_node * n2)55 buffer_insert_cmp(const struct rb_node *n1, const struct rb_node *n2)
56 {
57 const struct buffer *buf1 = (const struct buffer *)n1;
58 const struct buffer *buf2 = (const struct buffer *)n2;
59 /* Note that gpuaddr comparisions can overflow an int: */
60 if (buf1->gpuaddr > buf2->gpuaddr)
61 return 1;
62 else if (buf1->gpuaddr < buf2->gpuaddr)
63 return -1;
64 return 0;
65 }
66
67 static int
buffer_search_cmp(const struct rb_node * node,const void * addrptr)68 buffer_search_cmp(const struct rb_node *node, const void *addrptr)
69 {
70 const struct buffer *buf = (const struct buffer *)node;
71 uint64_t gpuaddr = *(uint64_t *)addrptr;
72 if (buf->gpuaddr + buf->len <= gpuaddr)
73 return -1;
74 else if (buf->gpuaddr > gpuaddr)
75 return 1;
76 return 0;
77 }
78
79 static struct buffer *
get_buffer(uint64_t gpuaddr)80 get_buffer(uint64_t gpuaddr)
81 {
82 if (gpuaddr == 0)
83 return NULL;
84 return (struct buffer *)rb_tree_search(&buffers, &gpuaddr,
85 buffer_search_cmp);
86 }
87
88 static int
buffer_contains_hostptr(struct buffer * buf,void * hostptr)89 buffer_contains_hostptr(struct buffer *buf, void *hostptr)
90 {
91 return (buf->hostptr <= hostptr) && (hostptr < (buf->hostptr + buf->len));
92 }
93
94 uint64_t
gpuaddr(void * hostptr)95 gpuaddr(void *hostptr)
96 {
97 rb_tree_foreach (struct buffer, buf, &buffers, node) {
98 if (buffer_contains_hostptr(buf, hostptr))
99 return buf->gpuaddr + (hostptr - buf->hostptr);
100 }
101 return 0;
102 }
103
104 uint64_t
gpubaseaddr(uint64_t gpuaddr)105 gpubaseaddr(uint64_t gpuaddr)
106 {
107 struct buffer *buf = get_buffer(gpuaddr);
108 if (buf)
109 return buf->gpuaddr;
110 else
111 return 0;
112 }
113
114 void *
hostptr(uint64_t gpuaddr)115 hostptr(uint64_t gpuaddr)
116 {
117 struct buffer *buf = get_buffer(gpuaddr);
118 if (buf)
119 return buf->hostptr + (gpuaddr - buf->gpuaddr);
120 else
121 return 0;
122 }
123
124 unsigned
hostlen(uint64_t gpuaddr)125 hostlen(uint64_t gpuaddr)
126 {
127 struct buffer *buf = get_buffer(gpuaddr);
128 if (buf)
129 return buf->len + buf->gpuaddr - gpuaddr;
130 else
131 return 0;
132 }
133
134 bool
has_dumped(uint64_t gpuaddr,unsigned enable_mask)135 has_dumped(uint64_t gpuaddr, unsigned enable_mask)
136 {
137 if (!gpuaddr)
138 return false;
139
140 struct buffer *b = get_buffer(gpuaddr);
141 if (!b)
142 return false;
143
144 assert(gpuaddr >= b->gpuaddr);
145 unsigned offset = gpuaddr - b->gpuaddr;
146
147 unsigned n = 0;
148 while (n < b->noffsets) {
149 if (offset == b->offsets[n].offset)
150 break;
151 n++;
152 }
153
154 /* if needed, allocate a new offset entry: */
155 if (n == b->noffsets) {
156 b->noffsets++;
157 assert(b->noffsets < ARRAY_SIZE(b->offsets));
158 b->offsets[n].dumped_mask = 0;
159 b->offsets[n].offset = offset;
160 }
161
162 if ((b->offsets[n].dumped_mask & enable_mask) == enable_mask)
163 return true;
164
165 b->offsets[n].dumped_mask |= enable_mask;
166
167 return false;
168 }
169
170 void
reset_buffers(void)171 reset_buffers(void)
172 {
173 rb_tree_foreach_safe (struct buffer, buf, &buffers, node) {
174 rb_tree_remove(&buffers, &buf->node);
175 free(buf->hostptr);
176 free(buf);
177 }
178 }
179
180 /**
181 * Record buffer contents, takes ownership of hostptr (freed in
182 * reset_buffers())
183 */
184 void
add_buffer(uint64_t gpuaddr,unsigned int len,void * hostptr)185 add_buffer(uint64_t gpuaddr, unsigned int len, void *hostptr)
186 {
187 struct buffer *buf = get_buffer(gpuaddr);
188
189 if (!buf) {
190 buf = calloc(sizeof(struct buffer), 1);
191 buf->gpuaddr = gpuaddr;
192 rb_tree_insert(&buffers, &buf->node, buffer_insert_cmp);
193 }
194
195 /* We can end up in scenarios where we capture parts of a buffer that
196 * has been suballocated from twice, once as a dumped buffer and once
197 * as a cmd.. possibly the kernel should get more clever about this,
198 * but we need to tolerate it:
199 */
200 if (buf->gpuaddr != gpuaddr) {
201 assert(gpuaddr > buf->gpuaddr);
202 assert((gpuaddr + len) <= (buf->gpuaddr + buf->len));
203
204 void *ptr = ((uint8_t *)buf->hostptr) + (gpuaddr - buf->gpuaddr);
205 assert(!memcmp(ptr, hostptr, len));
206
207 return;
208 }
209
210 buf->hostptr = hostptr;
211 buf->len = len;
212 }
213