17ec681f3Smrg/*
27ec681f3Smrg * Copyright (c) 2012 Rob Clark <robdclark@gmail.com>
37ec681f3Smrg *
47ec681f3Smrg * Permission is hereby granted, free of charge, to any person obtaining a
57ec681f3Smrg * copy of this software and associated documentation files (the "Software"),
67ec681f3Smrg * to deal in the Software without restriction, including without limitation
77ec681f3Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
87ec681f3Smrg * and/or sell copies of the Software, and to permit persons to whom the
97ec681f3Smrg * Software is furnished to do so, subject to the following conditions:
107ec681f3Smrg *
117ec681f3Smrg * The above copyright notice and this permission notice (including the next
127ec681f3Smrg * paragraph) shall be included in all copies or substantial portions of the
137ec681f3Smrg * Software.
147ec681f3Smrg *
157ec681f3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
167ec681f3Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
177ec681f3Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
187ec681f3Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
197ec681f3Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
207ec681f3Smrg * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
217ec681f3Smrg * SOFTWARE.
227ec681f3Smrg */
237ec681f3Smrg
247ec681f3Smrg/*
257ec681f3Smrg * Helper lib to track gpu buffers contents/address, and map between gpu and
267ec681f3Smrg * host address while decoding cmdstream/crashdumps
277ec681f3Smrg */
287ec681f3Smrg
297ec681f3Smrg#include <assert.h>
307ec681f3Smrg#include <stdlib.h>
317ec681f3Smrg
327ec681f3Smrg#include "util/rb_tree.h"
337ec681f3Smrg#include "buffers.h"
347ec681f3Smrg
357ec681f3Smrgstruct buffer {
367ec681f3Smrg   struct rb_node node;
377ec681f3Smrg   void *hostptr;
387ec681f3Smrg   unsigned int len;
397ec681f3Smrg   uint64_t gpuaddr;
407ec681f3Smrg
417ec681f3Smrg   /* for 'once' mode, for buffers containing cmdstream keep track per offset
427ec681f3Smrg    * into buffer of which modes it has already been dumped;
437ec681f3Smrg    */
447ec681f3Smrg   struct {
457ec681f3Smrg      unsigned offset;
467ec681f3Smrg      unsigned dumped_mask;
477ec681f3Smrg   } offsets[64];
487ec681f3Smrg   unsigned noffsets;
497ec681f3Smrg};
507ec681f3Smrg
517ec681f3Smrgstatic struct rb_tree buffers;
527ec681f3Smrg
537ec681f3Smrgstatic int
547ec681f3Smrgbuffer_insert_cmp(const struct rb_node *n1, const struct rb_node *n2)
557ec681f3Smrg{
567ec681f3Smrg   const struct buffer *buf1 = (const struct buffer *)n1;
577ec681f3Smrg   const struct buffer *buf2 = (const struct buffer *)n2;
587ec681f3Smrg   /* Note that gpuaddr comparisions can overflow an int: */
597ec681f3Smrg   if (buf1->gpuaddr > buf2->gpuaddr)
607ec681f3Smrg      return 1;
617ec681f3Smrg   else if (buf1->gpuaddr < buf2->gpuaddr)
627ec681f3Smrg      return -1;
637ec681f3Smrg   return 0;
647ec681f3Smrg}
657ec681f3Smrg
667ec681f3Smrgstatic int
677ec681f3Smrgbuffer_search_cmp(const struct rb_node *node, const void *addrptr)
687ec681f3Smrg{
697ec681f3Smrg   const struct buffer *buf = (const struct buffer *)node;
707ec681f3Smrg   uint64_t gpuaddr = *(uint64_t *)addrptr;
717ec681f3Smrg   if (buf->gpuaddr + buf->len <= gpuaddr)
727ec681f3Smrg      return -1;
737ec681f3Smrg   else if (buf->gpuaddr > gpuaddr)
747ec681f3Smrg      return 1;
757ec681f3Smrg   return 0;
767ec681f3Smrg}
777ec681f3Smrg
787ec681f3Smrgstatic struct buffer *
797ec681f3Smrgget_buffer(uint64_t gpuaddr)
807ec681f3Smrg{
817ec681f3Smrg   if (gpuaddr == 0)
827ec681f3Smrg      return NULL;
837ec681f3Smrg   return (struct buffer *)rb_tree_search(&buffers, &gpuaddr,
847ec681f3Smrg                                          buffer_search_cmp);
857ec681f3Smrg}
867ec681f3Smrg
877ec681f3Smrgstatic int
887ec681f3Smrgbuffer_contains_hostptr(struct buffer *buf, void *hostptr)
897ec681f3Smrg{
907ec681f3Smrg   return (buf->hostptr <= hostptr) && (hostptr < (buf->hostptr + buf->len));
917ec681f3Smrg}
927ec681f3Smrg
937ec681f3Smrguint64_t
947ec681f3Smrggpuaddr(void *hostptr)
957ec681f3Smrg{
967ec681f3Smrg   rb_tree_foreach (struct buffer, buf, &buffers, node) {
977ec681f3Smrg      if (buffer_contains_hostptr(buf, hostptr))
987ec681f3Smrg         return buf->gpuaddr + (hostptr - buf->hostptr);
997ec681f3Smrg   }
1007ec681f3Smrg   return 0;
1017ec681f3Smrg}
1027ec681f3Smrg
1037ec681f3Smrguint64_t
1047ec681f3Smrggpubaseaddr(uint64_t gpuaddr)
1057ec681f3Smrg{
1067ec681f3Smrg   struct buffer *buf = get_buffer(gpuaddr);
1077ec681f3Smrg   if (buf)
1087ec681f3Smrg      return buf->gpuaddr;
1097ec681f3Smrg   else
1107ec681f3Smrg      return 0;
1117ec681f3Smrg}
1127ec681f3Smrg
1137ec681f3Smrgvoid *
1147ec681f3Smrghostptr(uint64_t gpuaddr)
1157ec681f3Smrg{
1167ec681f3Smrg   struct buffer *buf = get_buffer(gpuaddr);
1177ec681f3Smrg   if (buf)
1187ec681f3Smrg      return buf->hostptr + (gpuaddr - buf->gpuaddr);
1197ec681f3Smrg   else
1207ec681f3Smrg      return 0;
1217ec681f3Smrg}
1227ec681f3Smrg
1237ec681f3Smrgunsigned
1247ec681f3Smrghostlen(uint64_t gpuaddr)
1257ec681f3Smrg{
1267ec681f3Smrg   struct buffer *buf = get_buffer(gpuaddr);
1277ec681f3Smrg   if (buf)
1287ec681f3Smrg      return buf->len + buf->gpuaddr - gpuaddr;
1297ec681f3Smrg   else
1307ec681f3Smrg      return 0;
1317ec681f3Smrg}
1327ec681f3Smrg
1337ec681f3Smrgbool
1347ec681f3Smrghas_dumped(uint64_t gpuaddr, unsigned enable_mask)
1357ec681f3Smrg{
1367ec681f3Smrg   if (!gpuaddr)
1377ec681f3Smrg      return false;
1387ec681f3Smrg
1397ec681f3Smrg   struct buffer *b = get_buffer(gpuaddr);
1407ec681f3Smrg   if (!b)
1417ec681f3Smrg      return false;
1427ec681f3Smrg
1437ec681f3Smrg   assert(gpuaddr >= b->gpuaddr);
1447ec681f3Smrg   unsigned offset = gpuaddr - b->gpuaddr;
1457ec681f3Smrg
1467ec681f3Smrg   unsigned n = 0;
1477ec681f3Smrg   while (n < b->noffsets) {
1487ec681f3Smrg      if (offset == b->offsets[n].offset)
1497ec681f3Smrg         break;
1507ec681f3Smrg      n++;
1517ec681f3Smrg   }
1527ec681f3Smrg
1537ec681f3Smrg   /* if needed, allocate a new offset entry: */
1547ec681f3Smrg   if (n == b->noffsets) {
1557ec681f3Smrg      b->noffsets++;
1567ec681f3Smrg      assert(b->noffsets < ARRAY_SIZE(b->offsets));
1577ec681f3Smrg      b->offsets[n].dumped_mask = 0;
1587ec681f3Smrg      b->offsets[n].offset = offset;
1597ec681f3Smrg   }
1607ec681f3Smrg
1617ec681f3Smrg   if ((b->offsets[n].dumped_mask & enable_mask) == enable_mask)
1627ec681f3Smrg      return true;
1637ec681f3Smrg
1647ec681f3Smrg   b->offsets[n].dumped_mask |= enable_mask;
1657ec681f3Smrg
1667ec681f3Smrg   return false;
1677ec681f3Smrg}
1687ec681f3Smrg
1697ec681f3Smrgvoid
1707ec681f3Smrgreset_buffers(void)
1717ec681f3Smrg{
1727ec681f3Smrg   rb_tree_foreach_safe (struct buffer, buf, &buffers, node) {
1737ec681f3Smrg      rb_tree_remove(&buffers, &buf->node);
1747ec681f3Smrg      free(buf->hostptr);
1757ec681f3Smrg      free(buf);
1767ec681f3Smrg   }
1777ec681f3Smrg}
1787ec681f3Smrg
1797ec681f3Smrg/**
1807ec681f3Smrg * Record buffer contents, takes ownership of hostptr (freed in
1817ec681f3Smrg * reset_buffers())
1827ec681f3Smrg */
1837ec681f3Smrgvoid
1847ec681f3Smrgadd_buffer(uint64_t gpuaddr, unsigned int len, void *hostptr)
1857ec681f3Smrg{
1867ec681f3Smrg   struct buffer *buf = get_buffer(gpuaddr);
1877ec681f3Smrg
1887ec681f3Smrg   if (!buf) {
1897ec681f3Smrg      buf = calloc(sizeof(struct buffer), 1);
1907ec681f3Smrg      buf->gpuaddr = gpuaddr;
1917ec681f3Smrg      rb_tree_insert(&buffers, &buf->node, buffer_insert_cmp);
1927ec681f3Smrg   }
1937ec681f3Smrg
1947ec681f3Smrg   assert(buf->gpuaddr == gpuaddr);
1957ec681f3Smrg
1967ec681f3Smrg   buf->hostptr = hostptr;
1977ec681f3Smrg   buf->len = len;
1987ec681f3Smrg}
199