-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathurb.c
320 lines (273 loc) · 8.79 KB
/
urb.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
* Copyright © 2016 Kristian H. Kristensen
* Copyright © 2015 Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include "eu.h"
#include "kir.h"
struct free_urb {
uint32_t next;
};
void
set_urb_allocation(struct urb *urb, uint32_t address, uint32_t size, uint32_t total)
{
const uint32_t chunk_size_bytes = 8192;
urb->data = gt.urb + address * chunk_size_bytes;
urb->size = (size + 1) * 64;
urb->total = total;
urb->free_list = URB_EMPTY;
urb->count = 0;
}
void *
alloc_urb_entry(struct urb *urb)
{
struct free_urb *f;
void *p;
if (urb->free_list != URB_EMPTY) {
f = p = urb->data + urb->free_list;
urb->free_list = f->next;
} else {
ksim_assert(urb->count < urb->total);
p = urb->data + urb->size * urb->count++;
}
ksim_assert(p >= urb->data && p < urb->data + urb->total * urb->size);
ksim_assert(p >= (void *) gt.urb && p < (void *) gt.urb + sizeof(gt.urb));
return p;
}
void
free_urb_entry(struct urb* urb, void *entry)
{
struct free_urb *f = entry;
ksim_assert(entry >= urb->data &&
entry < urb->data + urb->total * urb->size);
f->next = urb->free_list;
urb->free_list = entry - urb->data;
}
void
validate_urb_state(void)
{
struct urb *all_urbs[] = {
>.vs.urb,
>.hs.urb,
>.ds.urb,
>.gs.urb,
}, *u, *v;
/* Validate that the URB allocations are properly sized and
* don't overlap
*/
for (uint32_t i = 0; i < ARRAY_LENGTH(all_urbs); i++) {
u = all_urbs[i];
char *ustart = u->data;
char *uend = ustart + u->total * u->size;
ksim_assert(gt.urb <= ustart && uend <= gt.urb + sizeof(gt.urb));
for (uint32_t j = i + 1; j < ARRAY_LENGTH(all_urbs); j++) {
v = all_urbs[j];
char *vstart = v->data;
char *vend = v->data + v->total * v->size;
ksim_assert(vend <= ustart || uend <= vstart);
}
}
/* If we're doing SIMD8 vs dispatch, we need at least 8 VUEs,
* but the BDW hw limit is even higher: 64. */
ksim_assert(64 <= gt.vs.urb.total && gt.vs.urb.total <= 2560);
}
static void
emit_sfid_urb_simd8_simple_write(struct kir_program *prog, struct inst *inst)
{
struct inst_send send = unpack_inst_send(inst);
uint32_t src = (unpack_inst_2src_src0(inst).num + 1) * 32;
uint32_t vue_offset = field(send.function_control, 4, 14);
uint32_t dst = prog->urb_offset + vue_offset * 4 * 32;
/* We should only get here if there's a urb offset set. */
ksim_assert(prog->urb_offset != 0);
kir_program_comment(prog, "urb write: length %d, offset %d",
send.mlen - 1, vue_offset);
for (uint32_t i = 0; i < send.mlen - 1; i++) {
kir_program_load_v8(prog, src + i * 32);
kir_program_store_v8(prog, dst + i * 32, prog->dst);
}
}
enum urb_opcode {
URB_WRITE_HWORD = 0,
URB_WRITE_OWORD = 1,
URB_READ_HWORD = 2,
URB_READ_OWORD = 3,
URB_ATOMIC_MOV = 4,
URB_ATOMIC_INC = 5,
URB_ATOMIC_ADD = 6,
URB_SIMD8_WRITE = 7,
URB_SIMD8_READ = 8,
};
enum urb_swizzle_control {
URB_NOSWIZZLE = 0,
URB_INTERLEAVED = 1,
};
struct urb_message_descriptor {
enum urb_opcode opcode;
uint32_t global_offset;
enum urb_swizzle_control swizzle;
bool channel_mask;
bool per_slot_offset;
bool header_present;
uint32_t response_length;
uint32_t message_length;
bool eot;
};
static inline struct urb_message_descriptor
unpack_urb_message_descriptor(uint32_t function_control)
{
return (struct urb_message_descriptor) {
.opcode = field(function_control, 0, 3),
.global_offset = field(function_control, 4, 14),
.swizzle = field(function_control, 15, 15),
.channel_mask = field(function_control, 15, 15),
.per_slot_offset = field(function_control, 17, 17),
.header_present = field(function_control, 19, 19),
.response_length = field(function_control, 20, 24),
.message_length = field(function_control, 25, 28),
.eot = field(function_control, 31, 31),
};
}
struct sfid_urb_args {
uint32_t global_offset;
bool per_slot_offset;
bool channel_mask;
uint32_t src, dst, len, rlen;
uint32_t scope;
};
static void
sfid_urb_simd8_read(struct thread *t, struct sfid_urb_args *args)
{
uint32_t grf = args->src;
struct reg vue_handles = t->grf[grf++];
struct reg offset, channel_mask;
offset.ireg = _mm256_set1_epi32(args->global_offset);
if (args->per_slot_offset)
offset.ireg = _mm256_add_epi32(offset.ireg, t->grf[grf++].ireg);
uint32_t valid_bits = (1 << args->rlen) - 1;
if (args->channel_mask) {
channel_mask.ireg = t->grf[grf++].ireg;
channel_mask.ireg = _mm256_srli_epi32(channel_mask.ireg, 16);
channel_mask.ireg = _mm256_and_si256(channel_mask.ireg,
_mm256_set1_epi32(valid_bits));
} else {
channel_mask.ireg = _mm256_set1_epi32(valid_bits);
}
struct reg mask;
mask.ireg = _mm256_and_si256(channel_mask.ireg, t->mask[args->scope].q[0]);
for (uint32_t c = 0; c < 8; c++) {
if (!mask.ud[c])
continue;
uint32_t *vue = urb_handle_to_entry(vue_handles.ud[c]) +
offset.ud[c] * 16;
uint32_t i;
for_each_bit(i, channel_mask.ud[c])
t->grf[args->dst + i].ud[c] = vue[i];
}
}
static void
sfid_urb_simd8_write(struct thread *t, struct sfid_urb_args *args)
{
uint32_t grf = args->src;
struct reg vue_handles = t->grf[grf++];
struct reg offset, channel_mask;
/* FIXME: For tessellation we often get a constant channel
* mask with just one bit set. We should find a way to emit
* just a single dword store for that. */
offset.ireg = _mm256_set1_epi32(args->global_offset);
if (args->per_slot_offset)
offset.ireg = _mm256_add_epi32(offset.ireg, t->grf[grf++].ireg);
if (args->channel_mask) {
channel_mask.ireg = t->grf[grf++].ireg;
uint32_t valid_bits = (1 << (args->src + args->len - grf)) - 1;
channel_mask.ireg = _mm256_srli_epi32(channel_mask.ireg, 16);
channel_mask.ireg = _mm256_and_si256(channel_mask.ireg,
_mm256_set1_epi32(valid_bits));
} else {
uint32_t valid_bits = (1 << (args->src + args->len - grf)) - 1;
channel_mask.ireg = _mm256_set1_epi32(valid_bits);
}
struct reg mask;
mask.ireg = _mm256_and_si256(channel_mask.ireg, t->mask[args->scope].q[0]);
for (uint32_t c = 0; c < 8; c++) {
if (!mask.ud[c])
continue;
uint32_t *vue = urb_handle_to_entry(vue_handles.ud[c]) +
offset.ud[c] * 16;
uint32_t i;
for_each_bit(i, channel_mask.ud[c])
vue[i] = t->grf[grf + i].ud[c];
}
}
static struct sfid_urb_args *
create_urb_args(struct kir_program *prog, struct inst *inst)
{
struct inst_send send = unpack_inst_send(inst);
struct urb_message_descriptor md =
unpack_urb_message_descriptor(send.function_control);
struct sfid_urb_args *args;
args = get_const_data(sizeof *args, 8);
args->per_slot_offset = md.per_slot_offset;
args->channel_mask = md.channel_mask;
args->global_offset = md.global_offset;
args->src = unpack_inst_2src_src0(inst).num;
args->dst = unpack_inst_2src_dst(inst).num;
args->len = send.mlen;
args->rlen = send.rlen;
args->scope = prog->scope;
return args;
}
void
builder_emit_sfid_urb(struct kir_program *prog, struct inst *inst)
{
struct inst_send send = unpack_inst_send(inst);
struct urb_message_descriptor md =
unpack_urb_message_descriptor(send.function_control);
struct sfid_urb_args *args;
ksim_assert(send.header_present);
switch (md.opcode) {
case URB_WRITE_HWORD:
case URB_WRITE_OWORD:
case URB_READ_HWORD:
case URB_READ_OWORD:
case URB_ATOMIC_MOV:
case URB_ATOMIC_INC:
case URB_ATOMIC_ADD:
stub("sfid urb opcode %d", md.opcode);
return;
case URB_SIMD8_READ:
args = create_urb_args(prog, inst);
kir_program_const_send(prog, inst, sfid_urb_simd8_read, args);
return;
case URB_SIMD8_WRITE:
ksim_assert(send.rlen == 0);
if (!md.per_slot_offset && !md.channel_mask && prog->urb_offset > 0) {
emit_sfid_urb_simd8_simple_write(prog, inst);
} else {
args = create_urb_args(prog, inst);
kir_program_send(prog, inst, sfid_urb_simd8_write, args);
}
break;
default:
ksim_unreachable("out of range urb opcode: %d", md.opcode);
break;
}
}