turnip: document tu_cs
[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 "adreno_pm4.xml.h"
29
30 void
31 tu_cs_init(struct tu_cs *cs);
32 void
33 tu_cs_finish(struct tu_device *dev, struct tu_cs *cs);
34 VkResult
35 tu_cs_begin(struct tu_device *dev, struct tu_cs *cs, uint32_t reserve_size);
36 VkResult
37 tu_cs_end(struct tu_cs *cs);
38 void
39 tu_cs_reset(struct tu_device *dev, struct tu_cs *cs);
40 VkResult
41 tu_cs_check_space(struct tu_device *dev, struct tu_cs *cs, size_t size);
42
43 /**
44 * Emit a uint32_t value into a command stream, without boundary checking.
45 */
46 static inline void
47 tu_cs_emit(struct tu_cs *cs, uint32_t value)
48 {
49 assert(cs->cur < cs->end);
50 *cs->cur = value;
51 ++cs->cur;
52 }
53
54 static inline unsigned
55 tu_odd_parity_bit(unsigned val)
56 {
57 /* See: http://graphics.stanford.edu/~seander/bithacks.html#ParityParallel
58 * note that we want odd parity so 0x6996 is inverted.
59 */
60 val ^= val >> 16;
61 val ^= val >> 8;
62 val ^= val >> 4;
63 val &= 0xf;
64 return (~0x6996 >> val) & 1;
65 }
66
67 /**
68 * Emit a type-4 command packet header into a command stream.
69 */
70 static inline void
71 tu_cs_emit_pkt4(struct tu_cs *cs, uint16_t regindx, uint16_t cnt)
72 {
73 tu_cs_emit(cs, CP_TYPE4_PKT | cnt | (tu_odd_parity_bit(cnt) << 7) |
74 ((regindx & 0x3ffff) << 8) |
75 ((tu_odd_parity_bit(regindx) << 27)));
76 }
77
78 /**
79 * Emit a type-7 command packet header into a command stream.
80 */
81 static inline void
82 tu_cs_emit_pkt7(struct tu_cs *cs, uint8_t opcode, uint16_t cnt)
83 {
84 tu_cs_emit(cs, CP_TYPE7_PKT | cnt | (tu_odd_parity_bit(cnt) << 15) |
85 ((opcode & 0x7f) << 16) |
86 ((tu_odd_parity_bit(opcode) << 23)));
87 }
88
89 static inline void
90 tu_cs_emit_wfi5(struct tu_cs *cs)
91 {
92 tu_cs_emit_pkt7(cs, CP_WAIT_FOR_IDLE, 0);
93 }
94
95 #endif /* TU_CS_H */