pan/midgard: Extend copy-propagation to swizzles
[mesa.git] / src / panfrost / pandecode / common.c
1 /*
2 * Copyright (C) 2019 Alyssa Rosenzweig
3 * Copyright (C) 2017-2018 Lyude Paul
4 * Copyright (C) 2019 Collabora, Ltd.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 */
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <assert.h>
29 #include <stdint.h>
30 #include <string.h>
31
32 #include "decode.h"
33 #include "util/macros.h"
34
35 /* Memory handling */
36
37 static struct pandecode_mapped_memory mmaps;
38
39 struct pandecode_mapped_memory *
40 pandecode_find_mapped_gpu_mem_containing(mali_ptr addr)
41 {
42 list_for_each_entry(struct pandecode_mapped_memory, pos, &mmaps.node, node) {
43 if (addr >= pos->gpu_va && addr < pos->gpu_va + pos->length)
44 return pos;
45 }
46
47 return NULL;
48 }
49
50 void
51 pandecode_inject_mmap(mali_ptr gpu_va, void *cpu, unsigned sz, const char *name)
52 {
53 struct pandecode_mapped_memory *mapped_mem = NULL;
54
55 mapped_mem = malloc(sizeof(*mapped_mem));
56 list_inithead(&mapped_mem->node);
57
58 mapped_mem->gpu_va = gpu_va;
59 mapped_mem->length = sz;
60 mapped_mem->addr = cpu;
61
62 if (!name) {
63 /* If we don't have a name, assign one */
64
65 snprintf(mapped_mem->name, ARRAY_SIZE(mapped_mem->name) - 1,
66 "memory_%" PRIx64, gpu_va);
67 } else {
68 assert(strlen(name) < ARRAY_SIZE(mapped_mem->name));
69 memcpy(mapped_mem->name, name, strlen(name));
70 }
71
72 list_add(&mapped_mem->node, &mmaps.node);
73 }
74
75 char *
76 pointer_as_memory_reference(mali_ptr ptr)
77 {
78 struct pandecode_mapped_memory *mapped;
79 char *out = malloc(128);
80
81 /* Try to find the corresponding mapped zone */
82
83 mapped = pandecode_find_mapped_gpu_mem_containing(ptr);
84
85 if (mapped) {
86 snprintf(out, 128, "%s + %d", mapped->name, (int) (ptr - mapped->gpu_va));
87 return out;
88 }
89
90 /* Just use the raw address if other options are exhausted */
91
92 snprintf(out, 128, MALI_PTR_FMT, ptr);
93 return out;
94
95 }
96
97 void
98 pandecode_initialize(void)
99 {
100 list_inithead(&mmaps.node);
101
102 }