1/*
2 * Copyright (C) 2017-2019 Lyude Paul
3 * Copyright (C) 2017-2019 Alyssa Rosenzweig
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 */
25
26#ifndef __PAN_DECODE_H__
27#define __PAN_DECODE_H__
28
29#include "genxml/gen_macros.h"
30
31#include "wrap.h"
32
33extern FILE *pandecode_dump_stream;
34
35void pandecode_dump_file_open(void);
36
37struct pandecode_mapped_memory {
38        size_t length;
39        void *addr;
40        uint64_t gpu_va;
41        bool ro;
42        char name[32];
43};
44
45char *pointer_as_memory_reference(uint64_t ptr);
46
47struct pandecode_mapped_memory *pandecode_find_mapped_gpu_mem_containing(uint64_t addr);
48
49void pandecode_map_read_write(void);
50
51static inline void *
52__pandecode_fetch_gpu_mem(const struct pandecode_mapped_memory *mem,
53                          uint64_t gpu_va, size_t size,
54                          int line, const char *filename)
55{
56        if (!mem)
57                mem = pandecode_find_mapped_gpu_mem_containing(gpu_va);
58
59        if (!mem) {
60                fprintf(stderr, "Access to unknown memory %" PRIx64 " in %s:%d\n",
61                        gpu_va, filename, line);
62                assert(0);
63        }
64
65        assert(mem);
66        assert(size + (gpu_va - mem->gpu_va) <= mem->length);
67
68        return mem->addr + gpu_va - mem->gpu_va;
69}
70
71#define pandecode_fetch_gpu_mem(mem, gpu_va, size) \
72	__pandecode_fetch_gpu_mem(mem, gpu_va, size, __LINE__, __FILE__)
73
74/* Returns a validated pointer to mapped GPU memory with the given pointer type,
75 * size automatically determined from the pointer type
76 */
77#define PANDECODE_PTR(mem, gpu_va, type) \
78	((type*)(__pandecode_fetch_gpu_mem(mem, gpu_va, sizeof(type), \
79					 __LINE__, __FILE__)))
80
81/* Usage: <variable type> PANDECODE_PTR_VAR(name, mem, gpu_va) */
82#define PANDECODE_PTR_VAR(name, mem, gpu_va) \
83	name = __pandecode_fetch_gpu_mem(mem, gpu_va, sizeof(*name), \
84				       __LINE__, __FILE__)
85
86#ifdef PAN_ARCH
87void GENX(pandecode_jc)(mali_ptr jc_gpu_va, unsigned gpu_id);
88void GENX(pandecode_abort_on_fault)(mali_ptr jc_gpu_va);
89#endif
90
91#endif /* __MMAP_TRACE_H__ */
92