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