1 /*
2 * Copyright (C) 2012-2018 Rob Clark <robclark@freedesktop.org>
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 FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 *
23 * Authors:
24 * Rob Clark <robclark@freedesktop.org>
25 */
26
27 #ifndef FREEDRENO_PM4_H_
28 #define FREEDRENO_PM4_H_
29
30 #include <stdint.h>
31
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35
36 #define CP_TYPE0_PKT 0x00000000
37 #define CP_TYPE2_PKT 0x80000000
38 #define CP_TYPE3_PKT 0xc0000000
39 #define CP_TYPE4_PKT 0x40000000
40 #define CP_TYPE7_PKT 0x70000000
41
42 #define CP_NOP_MESG 0x4D455347
43 #define CP_NOP_BEGN 0x4245474E
44 #define CP_NOP_END 0x454E4400
45
46 /*
47 * Helpers for pm4 pkt header building/parsing:
48 */
49
50 static inline unsigned
pm4_odd_parity_bit(unsigned val)51 pm4_odd_parity_bit(unsigned val)
52 {
53 /* See: http://graphics.stanford.edu/~seander/bithacks.html#ParityParallel
54 * note that we want odd parity so 0x6996 is inverted.
55 */
56 val ^= val >> 16;
57 val ^= val >> 8;
58 val ^= val >> 4;
59 val &= 0xf;
60 return (~0x6996 >> val) & 1;
61 }
62
63 static inline uint32_t
pm4_pkt0_hdr(uint16_t regindx,uint16_t cnt)64 pm4_pkt0_hdr(uint16_t regindx, uint16_t cnt)
65 {
66 return CP_TYPE0_PKT | ((cnt - 1) << 16) | (regindx & 0x7fff);
67 }
68
69 static inline uint32_t
pm4_pkt3_hdr(uint8_t opcode,uint16_t cnt)70 pm4_pkt3_hdr(uint8_t opcode, uint16_t cnt)
71 {
72 return CP_TYPE3_PKT | ((cnt - 1) << 16) | ((opcode & 0xff) << 8);
73 }
74
75 static inline uint32_t
pm4_pkt4_hdr(uint16_t regindx,uint16_t cnt)76 pm4_pkt4_hdr(uint16_t regindx, uint16_t cnt)
77 {
78 assert(cnt < 0x7f);
79 return CP_TYPE4_PKT | cnt | (pm4_odd_parity_bit(cnt) << 7) |
80 ((regindx & 0x3ffff) << 8) |
81 ((pm4_odd_parity_bit(regindx) << 27));
82 }
83
84 static inline uint32_t
pm4_pkt7_hdr(uint8_t opcode,uint16_t cnt)85 pm4_pkt7_hdr(uint8_t opcode, uint16_t cnt)
86 {
87 return CP_TYPE7_PKT | cnt | (pm4_odd_parity_bit(cnt) << 15) |
88 ((opcode & 0x7f) << 16) |
89 ((pm4_odd_parity_bit(opcode) << 23));
90 }
91
92 /*
93 * Helpers for packet parsing:
94 */
95
96 #define pkt_is_type0(pkt) (((pkt)&0XC0000000) == CP_TYPE0_PKT)
97 #define type0_pkt_size(pkt) ((((pkt) >> 16) & 0x3FFF) + 1)
98 #define type0_pkt_offset(pkt) ((pkt)&0x7FFF)
99
100 #define pkt_is_type2(pkt) ((pkt) == CP_TYPE2_PKT)
101
102 #define pkt_is_type3(pkt) \
103 ((((pkt)&0xC0000000) == CP_TYPE3_PKT) && (((pkt)&0x80FE) == 0))
104
105 #define cp_type3_opcode(pkt) (((pkt) >> 8) & 0xFF)
106 #define type3_pkt_size(pkt) ((((pkt) >> 16) & 0x3FFF) + 1)
107
108 static inline unsigned
pm4_calc_odd_parity_bit(unsigned val)109 pm4_calc_odd_parity_bit(unsigned val)
110 {
111 return (0x9669 >> (0xf & ((val) ^ ((val) >> 4) ^ ((val) >> 8) ^
112 ((val) >> 12) ^ ((val) >> 16) ^ ((val) >> 20) ^
113 ((val) >> 24) ^ ((val) >> 28)))) &
114 1;
115 }
116
117 #define pkt_is_type4(pkt) \
118 ((((pkt)&0xF0000000) == CP_TYPE4_PKT) && \
119 ((((pkt) >> 27) & 0x1) == \
120 pm4_calc_odd_parity_bit(type4_pkt_offset(pkt))) && \
121 ((((pkt) >> 7) & 0x1) == pm4_calc_odd_parity_bit(type4_pkt_size(pkt))))
122
123 #define type4_pkt_offset(pkt) (((pkt) >> 8) & 0x7FFFF)
124 #define type4_pkt_size(pkt) ((pkt)&0x7F)
125
126 #define pkt_is_type7(pkt) \
127 ((((pkt)&0xF0000000) == CP_TYPE7_PKT) && (((pkt)&0x0F000000) == 0) && \
128 ((((pkt) >> 23) & 0x1) == \
129 pm4_calc_odd_parity_bit(cp_type7_opcode(pkt))) && \
130 ((((pkt) >> 15) & 0x1) == pm4_calc_odd_parity_bit(type7_pkt_size(pkt))))
131
132 #define cp_type7_opcode(pkt) (((pkt) >> 16) & 0x7F)
133 #define type7_pkt_size(pkt) ((pkt)&0x3FFF)
134
135 #ifdef __cplusplus
136 } /* end of extern "C" */
137 #endif
138
139 #endif /* FREEDRENO_PM4_H_ */
140