71097aa6442d03a02d620283a6f63879489d9e4d
[mesa.git] / src / panfrost / bifrost / bi_pack.c
1 /*
2 * Copyright (C) 2020 Collabora, Ltd.
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
24 #include "compiler.h"
25
26 /* This file contains the final passes of the compiler. Running after
27 * scheduling and RA, the IR is now finalized, so we need to emit it to actual
28 * bits on the wire (as well as fixup branches) */
29
30 static uint64_t
31 bi_pack_header(bi_clause *clause, bi_clause *next)
32 {
33 struct bifrost_header header = {
34 /* stub */
35 .no_end_of_shader = (next != NULL),
36 };
37
38 uint64_t u = 0;
39 memcpy(&u, &header, sizeof(header));
40 return u;
41 }
42
43 /* Represents the assignment of ports for a given bundle */
44
45 struct bi_registers {
46 /* Register to assign to each port */
47 unsigned port[4];
48
49 /* Read ports can be disabled */
50 bool enabled[2];
51
52 /* Should we write FMA? what about ADD? If only a single port is
53 * enabled it is in port 2, else ADD/FMA is 2/3 respectively */
54 bool write_fma, write_add;
55
56 /* Should we read with port 3? */
57 bool read_port3;
58
59 /* Packed uniform/constant */
60 unsigned uniform_constant;
61
62 /* Whether writes are actually for the last instruction */
63 bool first_instruction;
64 };
65
66 /* Determines the register control field, ignoring the first? flag */
67
68 static enum bifrost_reg_control
69 bi_pack_register_ctrl_lo(struct bi_registers r)
70 {
71 if (r.write_fma) {
72 if (r.write_add) {
73 assert(!r.read_port3);
74 return BIFROST_WRITE_ADD_P2_FMA_P3;
75 } else {
76 if (r.read_port3)
77 return BIFROST_WRITE_FMA_P2_READ_P3;
78 else
79 return BIFROST_WRITE_FMA_P2;
80 }
81 } else if (r.write_add) {
82 if (r.read_port3)
83 return BIFROST_WRITE_ADD_P2_READ_P3;
84 else
85 return BIFROST_WRITE_ADD_P2;
86 } else if (r.read_port3)
87 return BIFROST_READ_P3;
88 else
89 return BIFROST_REG_NONE;
90 }
91
92 /* Ditto but account for the first? flag this time */
93
94 static enum bifrost_reg_control
95 bi_pack_register_ctrl(struct bi_registers r)
96 {
97 enum bifrost_reg_control ctrl = bi_pack_register_ctrl_lo(r);
98
99 if (r.first_instruction) {
100 if (ctrl == BIFROST_REG_NONE)
101 ctrl = BIFROST_FIRST_NONE;
102 else
103 ctrl |= BIFROST_FIRST_NONE;
104 }
105
106 return ctrl;
107 }
108
109 static unsigned
110 bi_pack_registers(struct bi_registers regs)
111 {
112 /* TODO */
113 return 0;
114 }
115
116 static unsigned
117 bi_pack_fma(bi_clause *clause, bi_bundle bundle)
118 {
119 /* TODO */
120 return BIFROST_FMA_NOP;
121 }
122
123 static unsigned
124 bi_pack_add(bi_clause *clause, bi_bundle bundle)
125 {
126 /* TODO */
127 return BIFROST_ADD_NOP;
128 }
129
130 struct bi_packed_bundle {
131 uint64_t lo;
132 uint64_t hi;
133 };
134
135 static struct bi_packed_bundle
136 bi_pack_bundle(bi_clause *clause, bi_bundle bundle)
137 {
138 unsigned reg = /*bi_pack_registers(clause, bundle)*/0;
139 uint64_t fma = bi_pack_fma(clause, bundle);
140 uint64_t add = bi_pack_add(clause, bundle);
141
142 struct bi_packed_bundle packed = {
143 .lo = reg | (fma << 35) | ((add & 0b111111) << 58),
144 .hi = add >> 6
145 };
146
147 return packed;
148 }
149
150 static void
151 bi_pack_clause(bi_context *ctx, bi_clause *clause, bi_clause *next,
152 struct util_dynarray *emission)
153 {
154 struct bi_packed_bundle ins_1 = bi_pack_bundle(clause, clause->bundles[0]);
155 assert(clause->bundle_count == 1);
156
157 struct bifrost_fmt1 quad_1 = {
158 .tag = BIFROST_FMT1_FINAL,
159 .header = bi_pack_header(clause, next),
160 .ins_1 = ins_1.lo,
161 .ins_2 = ins_1.hi & ((1 << 11) - 1),
162 .ins_0 = (ins_1.hi >> 11) & 0b111,
163 };
164
165 util_dynarray_append(emission, struct bifrost_fmt1, quad_1);
166 }
167
168 static bi_clause *
169 bi_next_clause(bi_context *ctx, pan_block *block, bi_clause *clause)
170 {
171 /* Try the next clause in this block */
172 if (clause->link.next != &((bi_block *) block)->clauses)
173 return list_first_entry(&(clause->link), bi_clause, link);
174
175 /* Try the next block, or the one after that if it's empty, etc .*/
176 pan_block *next_block = pan_next_block(block);
177
178 bi_foreach_block_from(ctx, next_block, block) {
179 bi_block *blk = (bi_block *) block;
180
181 if (!list_is_empty(&blk->clauses))
182 return list_first_entry(&(blk->clauses), bi_clause, link);
183 }
184
185 return NULL;
186 }
187
188 void
189 bi_pack(bi_context *ctx, struct util_dynarray *emission)
190 {
191 util_dynarray_init(emission, NULL);
192
193 bi_foreach_block(ctx, _block) {
194 bi_block *block = (bi_block *) _block;
195
196 bi_foreach_clause_in_block(block, clause) {
197 bi_clause *next = bi_next_clause(ctx, _block, clause);
198 bi_pack_clause(ctx, clause, next, emission);
199 }
200 }
201 }