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 /*
43 * Helpers for pm4 pkt header building/parsing:
44 */
45
46 static inline unsigned
pm4_odd_parity_bit(unsigned val)47 pm4_odd_parity_bit(unsigned val)
48 {
49 /* See: http://graphics.stanford.edu/~seander/bithacks.html#ParityParallel
50 * note that we want odd parity so 0x6996 is inverted.
51 */
52 val ^= val >> 16;
53 val ^= val >> 8;
54 val ^= val >> 4;
55 val &= 0xf;
56 return (~0x6996 >> val) & 1;
57 }
58
59 static inline uint32_t
pm4_pkt0_hdr(uint16_t regindx,uint16_t cnt)60 pm4_pkt0_hdr(uint16_t regindx, uint16_t cnt)
61 {
62 return CP_TYPE0_PKT | ((cnt - 1) << 16) | (regindx & 0x7fff);
63 }
64
65 static inline uint32_t
pm4_pkt3_hdr(uint8_t opcode,uint16_t cnt)66 pm4_pkt3_hdr(uint8_t opcode, uint16_t cnt)
67 {
68 return CP_TYPE3_PKT | ((cnt - 1) << 16) | ((opcode & 0xff) << 8);
69 }
70
71 static inline uint32_t
pm4_pkt4_hdr(uint16_t regindx,uint16_t cnt)72 pm4_pkt4_hdr(uint16_t regindx, uint16_t cnt)
73 {
74 assert(cnt < 0x7f);
75 return CP_TYPE4_PKT | cnt | (pm4_odd_parity_bit(cnt) << 7) |
76 ((regindx & 0x3ffff) << 8) |
77 ((pm4_odd_parity_bit(regindx) << 27));
78 }
79
80 static inline uint32_t
pm4_pkt7_hdr(uint8_t opcode,uint16_t cnt)81 pm4_pkt7_hdr(uint8_t opcode, uint16_t cnt)
82 {
83 return CP_TYPE7_PKT | cnt | (pm4_odd_parity_bit(cnt) << 15) |
84 ((opcode & 0x7f) << 16) |
85 ((pm4_odd_parity_bit(opcode) << 23));
86 }
87
88 /*
89 * Helpers for packet parsing:
90 */
91
92 #define pkt_is_type0(pkt) (((pkt)&0XC0000000) == CP_TYPE0_PKT)
93 #define type0_pkt_size(pkt) ((((pkt) >> 16) & 0x3FFF) + 1)
94 #define type0_pkt_offset(pkt) ((pkt)&0x7FFF)
95
96 #define pkt_is_type2(pkt) ((pkt) == CP_TYPE2_PKT)
97
98 #define pkt_is_type3(pkt) \
99 ((((pkt)&0xC0000000) == CP_TYPE3_PKT) && (((pkt)&0x80FE) == 0))
100
101 #define cp_type3_opcode(pkt) (((pkt) >> 8) & 0xFF)
102 #define type3_pkt_size(pkt) ((((pkt) >> 16) & 0x3FFF) + 1)
103
104 static inline uint
pm4_calc_odd_parity_bit(uint val)105 pm4_calc_odd_parity_bit(uint val)
106 {
107 return (0x9669 >> (0xf & ((val) ^ ((val) >> 4) ^ ((val) >> 8) ^
108 ((val) >> 12) ^ ((val) >> 16) ^ ((val) >> 20) ^
109 ((val) >> 24) ^ ((val) >> 28)))) &
110 1;
111 }
112
113 #define pkt_is_type4(pkt) \
114 ((((pkt)&0xF0000000) == CP_TYPE4_PKT) && \
115 ((((pkt) >> 27) & 0x1) == \
116 pm4_calc_odd_parity_bit(type4_pkt_offset(pkt))) && \
117 ((((pkt) >> 7) & 0x1) == pm4_calc_odd_parity_bit(type4_pkt_size(pkt))))
118
119 #define type4_pkt_offset(pkt) (((pkt) >> 8) & 0x7FFFF)
120 #define type4_pkt_size(pkt) ((pkt)&0x7F)
121
122 #define pkt_is_type7(pkt) \
123 ((((pkt)&0xF0000000) == CP_TYPE7_PKT) && (((pkt)&0x0F000000) == 0) && \
124 ((((pkt) >> 23) & 0x1) == \
125 pm4_calc_odd_parity_bit(cp_type7_opcode(pkt))) && \
126 ((((pkt) >> 15) & 0x1) == pm4_calc_odd_parity_bit(type7_pkt_size(pkt))))
127
128 #define cp_type7_opcode(pkt) (((pkt) >> 16) & 0x7F)
129 #define type7_pkt_size(pkt) ((pkt)&0x3FFF)
130
131 #ifdef __cplusplus
132 } /* end of extern "C" */
133 #endif
134
135 #endif /* FREEDRENO_PM4_H_ */
136