17ec681f3Smrg/*
27ec681f3Smrg * Copyright (C) 2012-2018 Rob Clark <robclark@freedesktop.org>
37ec681f3Smrg *
47ec681f3Smrg * Permission is hereby granted, free of charge, to any person obtaining a
57ec681f3Smrg * copy of this software and associated documentation files (the "Software"),
67ec681f3Smrg * to deal in the Software without restriction, including without limitation
77ec681f3Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
87ec681f3Smrg * and/or sell copies of the Software, and to permit persons to whom the
97ec681f3Smrg * Software is furnished to do so, subject to the following conditions:
107ec681f3Smrg *
117ec681f3Smrg * The above copyright notice and this permission notice (including the next
127ec681f3Smrg * paragraph) shall be included in all copies or substantial portions of the
137ec681f3Smrg * Software.
147ec681f3Smrg *
157ec681f3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
167ec681f3Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
177ec681f3Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
187ec681f3Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
197ec681f3Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
207ec681f3Smrg * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
217ec681f3Smrg * SOFTWARE.
227ec681f3Smrg *
237ec681f3Smrg * Authors:
247ec681f3Smrg *    Rob Clark <robclark@freedesktop.org>
257ec681f3Smrg */
267ec681f3Smrg
277ec681f3Smrg#ifndef FREEDRENO_PM4_H_
287ec681f3Smrg#define FREEDRENO_PM4_H_
297ec681f3Smrg
307ec681f3Smrg#include <stdint.h>
317ec681f3Smrg
327ec681f3Smrg#ifdef __cplusplus
337ec681f3Smrgextern "C" {
347ec681f3Smrg#endif
357ec681f3Smrg
367ec681f3Smrg#define CP_TYPE0_PKT 0x00000000
377ec681f3Smrg#define CP_TYPE2_PKT 0x80000000
387ec681f3Smrg#define CP_TYPE3_PKT 0xc0000000
397ec681f3Smrg#define CP_TYPE4_PKT 0x40000000
407ec681f3Smrg#define CP_TYPE7_PKT 0x70000000
417ec681f3Smrg
427ec681f3Smrg/*
437ec681f3Smrg * Helpers for pm4 pkt header building/parsing:
447ec681f3Smrg */
457ec681f3Smrg
467ec681f3Smrgstatic inline unsigned
477ec681f3Smrgpm4_odd_parity_bit(unsigned val)
487ec681f3Smrg{
497ec681f3Smrg   /* See: http://graphics.stanford.edu/~seander/bithacks.html#ParityParallel
507ec681f3Smrg    * note that we want odd parity so 0x6996 is inverted.
517ec681f3Smrg    */
527ec681f3Smrg   val ^= val >> 16;
537ec681f3Smrg   val ^= val >> 8;
547ec681f3Smrg   val ^= val >> 4;
557ec681f3Smrg   val &= 0xf;
567ec681f3Smrg   return (~0x6996 >> val) & 1;
577ec681f3Smrg}
587ec681f3Smrg
597ec681f3Smrgstatic inline uint32_t
607ec681f3Smrgpm4_pkt0_hdr(uint16_t regindx, uint16_t cnt)
617ec681f3Smrg{
627ec681f3Smrg   return CP_TYPE0_PKT | ((cnt - 1) << 16) | (regindx & 0x7fff);
637ec681f3Smrg}
647ec681f3Smrg
657ec681f3Smrgstatic inline uint32_t
667ec681f3Smrgpm4_pkt3_hdr(uint8_t opcode, uint16_t cnt)
677ec681f3Smrg{
687ec681f3Smrg   return CP_TYPE3_PKT | ((cnt - 1) << 16) | ((opcode & 0xff) << 8);
697ec681f3Smrg}
707ec681f3Smrg
717ec681f3Smrgstatic inline uint32_t
727ec681f3Smrgpm4_pkt4_hdr(uint16_t regindx, uint16_t cnt)
737ec681f3Smrg{
747ec681f3Smrg   return CP_TYPE4_PKT | cnt | (pm4_odd_parity_bit(cnt) << 7) |
757ec681f3Smrg         ((regindx & 0x3ffff) << 8) |
767ec681f3Smrg         ((pm4_odd_parity_bit(regindx) << 27));
777ec681f3Smrg}
787ec681f3Smrg
797ec681f3Smrgstatic inline uint32_t
807ec681f3Smrgpm4_pkt7_hdr(uint8_t opcode, uint16_t cnt)
817ec681f3Smrg{
827ec681f3Smrg   return CP_TYPE7_PKT | cnt | (pm4_odd_parity_bit(cnt) << 15) |
837ec681f3Smrg         ((opcode & 0x7f) << 16) |
847ec681f3Smrg         ((pm4_odd_parity_bit(opcode) << 23));
857ec681f3Smrg}
867ec681f3Smrg
877ec681f3Smrg/*
887ec681f3Smrg * Helpers for packet parsing:
897ec681f3Smrg */
907ec681f3Smrg
917ec681f3Smrg#define pkt_is_type0(pkt)     (((pkt)&0XC0000000) == CP_TYPE0_PKT)
927ec681f3Smrg#define type0_pkt_size(pkt)   ((((pkt) >> 16) & 0x3FFF) + 1)
937ec681f3Smrg#define type0_pkt_offset(pkt) ((pkt)&0x7FFF)
947ec681f3Smrg
957ec681f3Smrg#define pkt_is_type2(pkt) ((pkt) == CP_TYPE2_PKT)
967ec681f3Smrg
977ec681f3Smrg#define pkt_is_type3(pkt)                                                      \
987ec681f3Smrg   ((((pkt)&0xC0000000) == CP_TYPE3_PKT) && (((pkt)&0x80FE) == 0))
997ec681f3Smrg
1007ec681f3Smrg#define cp_type3_opcode(pkt) (((pkt) >> 8) & 0xFF)
1017ec681f3Smrg#define type3_pkt_size(pkt)  ((((pkt) >> 16) & 0x3FFF) + 1)
1027ec681f3Smrg
1037ec681f3Smrgstatic inline uint
1047ec681f3Smrgpm4_calc_odd_parity_bit(uint val)
1057ec681f3Smrg{
1067ec681f3Smrg   return (0x9669 >> (0xf & ((val) ^ ((val) >> 4) ^ ((val) >> 8) ^
1077ec681f3Smrg                             ((val) >> 12) ^ ((val) >> 16) ^ ((val) >> 20) ^
1087ec681f3Smrg                             ((val) >> 24) ^ ((val) >> 28)))) &
1097ec681f3Smrg          1;
1107ec681f3Smrg}
1117ec681f3Smrg
1127ec681f3Smrg#define pkt_is_type4(pkt)                                                      \
1137ec681f3Smrg   ((((pkt)&0xF0000000) == CP_TYPE4_PKT) &&                                    \
1147ec681f3Smrg    ((((pkt) >> 27) & 0x1) ==                                                  \
1157ec681f3Smrg     pm4_calc_odd_parity_bit(type4_pkt_offset(pkt))) &&                        \
1167ec681f3Smrg    ((((pkt) >> 7) & 0x1) == pm4_calc_odd_parity_bit(type4_pkt_size(pkt))))
1177ec681f3Smrg
1187ec681f3Smrg#define type4_pkt_offset(pkt) (((pkt) >> 8) & 0x7FFFF)
1197ec681f3Smrg#define type4_pkt_size(pkt)   ((pkt)&0x7F)
1207ec681f3Smrg
1217ec681f3Smrg#define pkt_is_type7(pkt)                                                      \
1227ec681f3Smrg   ((((pkt)&0xF0000000) == CP_TYPE7_PKT) && (((pkt)&0x0F000000) == 0) &&       \
1237ec681f3Smrg    ((((pkt) >> 23) & 0x1) ==                                                  \
1247ec681f3Smrg     pm4_calc_odd_parity_bit(cp_type7_opcode(pkt))) &&                         \
1257ec681f3Smrg    ((((pkt) >> 15) & 0x1) == pm4_calc_odd_parity_bit(type7_pkt_size(pkt))))
1267ec681f3Smrg
1277ec681f3Smrg#define cp_type7_opcode(pkt) (((pkt) >> 16) & 0x7F)
1287ec681f3Smrg#define type7_pkt_size(pkt)  ((pkt)&0x3FFF)
1297ec681f3Smrg
1307ec681f3Smrg#ifdef __cplusplus
1317ec681f3Smrg} /* end of extern "C" */
1327ec681f3Smrg#endif
1337ec681f3Smrg
1347ec681f3Smrg#endif /* FREEDRENO_PM4_H_ */
135