1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright (C) 2012-2018 Rob Clark <robclark@freedesktop.org>
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20bf215546Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21bf215546Sopenharmony_ci * SOFTWARE.
22bf215546Sopenharmony_ci *
23bf215546Sopenharmony_ci * Authors:
24bf215546Sopenharmony_ci *    Rob Clark <robclark@freedesktop.org>
25bf215546Sopenharmony_ci */
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ci#ifndef FREEDRENO_PM4_H_
28bf215546Sopenharmony_ci#define FREEDRENO_PM4_H_
29bf215546Sopenharmony_ci
30bf215546Sopenharmony_ci#include <stdint.h>
31bf215546Sopenharmony_ci
32bf215546Sopenharmony_ci#ifdef __cplusplus
33bf215546Sopenharmony_ciextern "C" {
34bf215546Sopenharmony_ci#endif
35bf215546Sopenharmony_ci
36bf215546Sopenharmony_ci#define CP_TYPE0_PKT 0x00000000
37bf215546Sopenharmony_ci#define CP_TYPE2_PKT 0x80000000
38bf215546Sopenharmony_ci#define CP_TYPE3_PKT 0xc0000000
39bf215546Sopenharmony_ci#define CP_TYPE4_PKT 0x40000000
40bf215546Sopenharmony_ci#define CP_TYPE7_PKT 0x70000000
41bf215546Sopenharmony_ci
42bf215546Sopenharmony_ci/*
43bf215546Sopenharmony_ci * Helpers for pm4 pkt header building/parsing:
44bf215546Sopenharmony_ci */
45bf215546Sopenharmony_ci
46bf215546Sopenharmony_cistatic inline unsigned
47bf215546Sopenharmony_cipm4_odd_parity_bit(unsigned val)
48bf215546Sopenharmony_ci{
49bf215546Sopenharmony_ci   /* See: http://graphics.stanford.edu/~seander/bithacks.html#ParityParallel
50bf215546Sopenharmony_ci    * note that we want odd parity so 0x6996 is inverted.
51bf215546Sopenharmony_ci    */
52bf215546Sopenharmony_ci   val ^= val >> 16;
53bf215546Sopenharmony_ci   val ^= val >> 8;
54bf215546Sopenharmony_ci   val ^= val >> 4;
55bf215546Sopenharmony_ci   val &= 0xf;
56bf215546Sopenharmony_ci   return (~0x6996 >> val) & 1;
57bf215546Sopenharmony_ci}
58bf215546Sopenharmony_ci
59bf215546Sopenharmony_cistatic inline uint32_t
60bf215546Sopenharmony_cipm4_pkt0_hdr(uint16_t regindx, uint16_t cnt)
61bf215546Sopenharmony_ci{
62bf215546Sopenharmony_ci   return CP_TYPE0_PKT | ((cnt - 1) << 16) | (regindx & 0x7fff);
63bf215546Sopenharmony_ci}
64bf215546Sopenharmony_ci
65bf215546Sopenharmony_cistatic inline uint32_t
66bf215546Sopenharmony_cipm4_pkt3_hdr(uint8_t opcode, uint16_t cnt)
67bf215546Sopenharmony_ci{
68bf215546Sopenharmony_ci   return CP_TYPE3_PKT | ((cnt - 1) << 16) | ((opcode & 0xff) << 8);
69bf215546Sopenharmony_ci}
70bf215546Sopenharmony_ci
71bf215546Sopenharmony_cistatic inline uint32_t
72bf215546Sopenharmony_cipm4_pkt4_hdr(uint16_t regindx, uint16_t cnt)
73bf215546Sopenharmony_ci{
74bf215546Sopenharmony_ci   assert(cnt < 0x7f);
75bf215546Sopenharmony_ci   return CP_TYPE4_PKT | cnt | (pm4_odd_parity_bit(cnt) << 7) |
76bf215546Sopenharmony_ci         ((regindx & 0x3ffff) << 8) |
77bf215546Sopenharmony_ci         ((pm4_odd_parity_bit(regindx) << 27));
78bf215546Sopenharmony_ci}
79bf215546Sopenharmony_ci
80bf215546Sopenharmony_cistatic inline uint32_t
81bf215546Sopenharmony_cipm4_pkt7_hdr(uint8_t opcode, uint16_t cnt)
82bf215546Sopenharmony_ci{
83bf215546Sopenharmony_ci   return CP_TYPE7_PKT | cnt | (pm4_odd_parity_bit(cnt) << 15) |
84bf215546Sopenharmony_ci         ((opcode & 0x7f) << 16) |
85bf215546Sopenharmony_ci         ((pm4_odd_parity_bit(opcode) << 23));
86bf215546Sopenharmony_ci}
87bf215546Sopenharmony_ci
88bf215546Sopenharmony_ci/*
89bf215546Sopenharmony_ci * Helpers for packet parsing:
90bf215546Sopenharmony_ci */
91bf215546Sopenharmony_ci
92bf215546Sopenharmony_ci#define pkt_is_type0(pkt)     (((pkt)&0XC0000000) == CP_TYPE0_PKT)
93bf215546Sopenharmony_ci#define type0_pkt_size(pkt)   ((((pkt) >> 16) & 0x3FFF) + 1)
94bf215546Sopenharmony_ci#define type0_pkt_offset(pkt) ((pkt)&0x7FFF)
95bf215546Sopenharmony_ci
96bf215546Sopenharmony_ci#define pkt_is_type2(pkt) ((pkt) == CP_TYPE2_PKT)
97bf215546Sopenharmony_ci
98bf215546Sopenharmony_ci#define pkt_is_type3(pkt)                                                      \
99bf215546Sopenharmony_ci   ((((pkt)&0xC0000000) == CP_TYPE3_PKT) && (((pkt)&0x80FE) == 0))
100bf215546Sopenharmony_ci
101bf215546Sopenharmony_ci#define cp_type3_opcode(pkt) (((pkt) >> 8) & 0xFF)
102bf215546Sopenharmony_ci#define type3_pkt_size(pkt)  ((((pkt) >> 16) & 0x3FFF) + 1)
103bf215546Sopenharmony_ci
104bf215546Sopenharmony_cistatic inline uint
105bf215546Sopenharmony_cipm4_calc_odd_parity_bit(uint val)
106bf215546Sopenharmony_ci{
107bf215546Sopenharmony_ci   return (0x9669 >> (0xf & ((val) ^ ((val) >> 4) ^ ((val) >> 8) ^
108bf215546Sopenharmony_ci                             ((val) >> 12) ^ ((val) >> 16) ^ ((val) >> 20) ^
109bf215546Sopenharmony_ci                             ((val) >> 24) ^ ((val) >> 28)))) &
110bf215546Sopenharmony_ci          1;
111bf215546Sopenharmony_ci}
112bf215546Sopenharmony_ci
113bf215546Sopenharmony_ci#define pkt_is_type4(pkt)                                                      \
114bf215546Sopenharmony_ci   ((((pkt)&0xF0000000) == CP_TYPE4_PKT) &&                                    \
115bf215546Sopenharmony_ci    ((((pkt) >> 27) & 0x1) ==                                                  \
116bf215546Sopenharmony_ci     pm4_calc_odd_parity_bit(type4_pkt_offset(pkt))) &&                        \
117bf215546Sopenharmony_ci    ((((pkt) >> 7) & 0x1) == pm4_calc_odd_parity_bit(type4_pkt_size(pkt))))
118bf215546Sopenharmony_ci
119bf215546Sopenharmony_ci#define type4_pkt_offset(pkt) (((pkt) >> 8) & 0x7FFFF)
120bf215546Sopenharmony_ci#define type4_pkt_size(pkt)   ((pkt)&0x7F)
121bf215546Sopenharmony_ci
122bf215546Sopenharmony_ci#define pkt_is_type7(pkt)                                                      \
123bf215546Sopenharmony_ci   ((((pkt)&0xF0000000) == CP_TYPE7_PKT) && (((pkt)&0x0F000000) == 0) &&       \
124bf215546Sopenharmony_ci    ((((pkt) >> 23) & 0x1) ==                                                  \
125bf215546Sopenharmony_ci     pm4_calc_odd_parity_bit(cp_type7_opcode(pkt))) &&                         \
126bf215546Sopenharmony_ci    ((((pkt) >> 15) & 0x1) == pm4_calc_odd_parity_bit(type7_pkt_size(pkt))))
127bf215546Sopenharmony_ci
128bf215546Sopenharmony_ci#define cp_type7_opcode(pkt) (((pkt) >> 16) & 0x7F)
129bf215546Sopenharmony_ci#define type7_pkt_size(pkt)  ((pkt)&0x3FFF)
130bf215546Sopenharmony_ci
131bf215546Sopenharmony_ci#ifdef __cplusplus
132bf215546Sopenharmony_ci} /* end of extern "C" */
133bf215546Sopenharmony_ci#endif
134bf215546Sopenharmony_ci
135bf215546Sopenharmony_ci#endif /* FREEDRENO_PM4_H_ */
136