turnip: never fail tu_cs_begin/tu_cs_end
[mesa.git] / src / freedreno / vulkan / tu_cs.h
1 /*
2 * Copyright © 2019 Google LLC
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
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23 #ifndef TU_CS_H
24 #define TU_CS_H
25
26 #include "tu_private.h"
27
28 #include "registers/adreno_pm4.xml.h"
29
30 void
31 tu_cs_init(struct tu_cs *cs, uint32_t initial_size);
32
33 void
34 tu_cs_finish(struct tu_device *dev, struct tu_cs *cs);
35
36 void
37 tu_cs_begin(struct tu_cs *cs);
38
39 void
40 tu_cs_end(struct tu_cs *cs);
41
42 VkResult
43 tu_cs_reserve_space(struct tu_device *dev,
44 struct tu_cs *cs,
45 uint32_t reserved_size);
46
47 void
48 tu_cs_reset(struct tu_device *dev, struct tu_cs *cs);
49
50 /**
51 * Assert that we did not exceed the reserved space.
52 */
53 static inline void
54 tu_cs_reserve_space_assert(struct tu_cs *cs)
55 {
56 assert(cs->cur <= cs->reserved_end);
57 }
58
59 /**
60 * Emit a uint32_t value into a command stream, without boundary checking.
61 */
62 static inline void
63 tu_cs_emit(struct tu_cs *cs, uint32_t value)
64 {
65 assert(cs->cur < cs->end);
66 *cs->cur = value;
67 ++cs->cur;
68 }
69
70 static inline unsigned
71 tu_odd_parity_bit(unsigned val)
72 {
73 /* See: http://graphics.stanford.edu/~seander/bithacks.html#ParityParallel
74 * note that we want odd parity so 0x6996 is inverted.
75 */
76 val ^= val >> 16;
77 val ^= val >> 8;
78 val ^= val >> 4;
79 val &= 0xf;
80 return (~0x6996 >> val) & 1;
81 }
82
83 /**
84 * Emit a type-4 command packet header into a command stream.
85 */
86 static inline void
87 tu_cs_emit_pkt4(struct tu_cs *cs, uint16_t regindx, uint16_t cnt)
88 {
89 tu_cs_emit(cs, CP_TYPE4_PKT | cnt | (tu_odd_parity_bit(cnt) << 7) |
90 ((regindx & 0x3ffff) << 8) |
91 ((tu_odd_parity_bit(regindx) << 27)));
92 }
93
94 /**
95 * Emit a type-7 command packet header into a command stream.
96 */
97 static inline void
98 tu_cs_emit_pkt7(struct tu_cs *cs, uint8_t opcode, uint16_t cnt)
99 {
100 tu_cs_emit(cs, CP_TYPE7_PKT | cnt | (tu_odd_parity_bit(cnt) << 15) |
101 ((opcode & 0x7f) << 16) |
102 ((tu_odd_parity_bit(opcode) << 23)));
103 }
104
105 static inline void
106 tu_cs_emit_wfi(struct tu_cs *cs)
107 {
108 tu_cs_emit_pkt7(cs, CP_WAIT_FOR_IDLE, 0);
109 }
110
111 static inline void
112 tu_cs_emit_qw(struct tu_cs *cs, uint64_t value)
113 {
114 tu_cs_emit(cs, (uint32_t) value);
115 tu_cs_emit(cs, (uint32_t) (value >> 32));
116 }
117
118 static inline void
119 tu_cs_emit_write_reg(struct tu_cs *cs, uint16_t reg, uint32_t value)
120 {
121 tu_cs_emit_pkt4(cs, reg, 1);
122 tu_cs_emit(cs, value);
123 }
124
125 static inline void
126 tu_cs_emit_ib(struct tu_cs *cs, const struct tu_cs *target)
127 {
128 for (uint32_t i = 0; i < target->entry_count; i++) {
129 const struct tu_cs_entry *entry = target->entries + i;
130
131 tu_cs_emit_pkt7(cs, CP_INDIRECT_BUFFER, 3);
132 tu_cs_emit_qw(cs, entry->bo->iova + entry->offset);
133 tu_cs_emit(cs, entry->size / sizeof(uint32_t));
134 }
135 }
136
137 #endif /* TU_CS_H */