blob: 3fd21819334472d0b1f6d52266ade3998ccd4d23 [file] [log] [blame]
Zhao Yakui31e94ad2014-02-11 10:48:43 +08001#include <intel_bufmgr.h>
2#include <i915_drm.h>
3
4#include "media_fill.h"
5#include "gen8_media.h"
6#include "intel_reg.h"
7
8#include <assert.h>
9
10#define ALIGN(x, y) (((x) + (y)-1) & ~((y)-1))
11
12static const uint32_t media_kernel[][4] = {
13 { 0x00400001, 0x20202288, 0x00000020, 0x00000000 },
14 { 0x00600001, 0x20800208, 0x008d0000, 0x00000000 },
15 { 0x00200001, 0x20800208, 0x00450040, 0x00000000 },
16 { 0x00000001, 0x20880608, 0x00000000, 0x000f000f },
17 { 0x00800001, 0x20a00208, 0x00000020, 0x00000000 },
18 { 0x00800001, 0x20e00208, 0x00000020, 0x00000000 },
19 { 0x00800001, 0x21200208, 0x00000020, 0x00000000 },
20 { 0x00800001, 0x21600208, 0x00000020, 0x00000000 },
21 { 0x0c800031, 0x24000a40, 0x0e000080, 0x120a8000 },
22 { 0x00600001, 0x2e000208, 0x008d0000, 0x00000000 },
23 { 0x07800031, 0x20000a40, 0x0e000e00, 0x82000010 },
24};
25
26static uint32_t
27batch_used(struct intel_batchbuffer *batch)
28{
29 return batch->ptr - batch->buffer;
30}
31
32static uint32_t
33batch_align(struct intel_batchbuffer *batch, uint32_t align)
34{
35 uint32_t offset = batch_used(batch);
36 offset = ALIGN(offset, align);
37 batch->ptr = batch->buffer + offset;
38 return offset;
39}
40
41static void *
42batch_alloc(struct intel_batchbuffer *batch, uint32_t size, uint32_t align)
43{
44 uint32_t offset = batch_align(batch, align);
45 batch->ptr += size;
46 return memset(batch->buffer + offset, 0, size);
47}
48
49static uint32_t
50batch_offset(struct intel_batchbuffer *batch, void *ptr)
51{
52 return (uint8_t *)ptr - batch->buffer;
53}
54
55static uint32_t
56batch_copy(struct intel_batchbuffer *batch, const void *ptr, uint32_t size, uint32_t align)
57{
58 return batch_offset(batch, memcpy(batch_alloc(batch, size, align), ptr, size));
59}
60
61static void
62gen8_render_flush(struct intel_batchbuffer *batch, uint32_t batch_end)
63{
64 int ret;
65
66 ret = drm_intel_bo_subdata(batch->bo, 0, 4096, batch->buffer);
67 if (ret == 0)
68 ret = drm_intel_bo_mrb_exec(batch->bo, batch_end,
69 NULL, 0, 0, 0);
70 assert(ret == 0);
71}
72
73static uint32_t
74gen8_fill_curbe_buffer_data(struct intel_batchbuffer *batch,
75 uint8_t color)
76{
77 uint8_t *curbe_buffer;
78 uint32_t offset;
79
80 curbe_buffer = batch_alloc(batch, sizeof(uint32_t) * 8, 64);
81 offset = batch_offset(batch, curbe_buffer);
82 *curbe_buffer = color;
83
84 return offset;
85}
86
87static uint32_t
88gen8_fill_surface_state(struct intel_batchbuffer *batch,
89 struct igt_buf *buf,
90 uint32_t format,
91 int is_dst)
92{
93 struct gen8_surface_state *ss;
94 uint32_t write_domain, read_domain, offset;
95 int ret;
96
97 if (is_dst) {
98 write_domain = read_domain = I915_GEM_DOMAIN_RENDER;
99 } else {
100 write_domain = 0;
101 read_domain = I915_GEM_DOMAIN_SAMPLER;
102 }
103
104 ss = batch_alloc(batch, sizeof(*ss), 64);
105 offset = batch_offset(batch, ss);
106
107 ss->ss0.surface_type = GEN8_SURFACE_2D;
108 ss->ss0.surface_format = format;
109 ss->ss0.render_cache_read_write = 1;
110 ss->ss0.vertical_alignment = 1; /* align 4 */
111 ss->ss0.horizontal_alignment = 1; /* align 4 */
112
113 if (buf->tiling == I915_TILING_X)
114 ss->ss0.tiled_mode = 2;
115 else if (buf->tiling == I915_TILING_Y)
116 ss->ss0.tiled_mode = 3;
117
118 ss->ss8.base_addr = buf->bo->offset;
119
120 ret = drm_intel_bo_emit_reloc(batch->bo,
121 batch_offset(batch, ss) + 8 * 4,
122 buf->bo, 0,
123 read_domain, write_domain);
124 assert(ret == 0);
125
126 ss->ss2.height = igt_buf_height(buf) - 1;
127 ss->ss2.width = igt_buf_width(buf) - 1;
128 ss->ss3.pitch = buf->stride - 1;
129
130 ss->ss7.shader_chanel_select_r = 4;
131 ss->ss7.shader_chanel_select_g = 5;
132 ss->ss7.shader_chanel_select_b = 6;
133 ss->ss7.shader_chanel_select_a = 7;
134
135 return offset;
136}
137
138static uint32_t
139gen8_fill_binding_table(struct intel_batchbuffer *batch,
140 struct igt_buf *dst)
141{
142 uint32_t *binding_table, offset;
143
144 binding_table = batch_alloc(batch, 32, 64);
145 offset = batch_offset(batch, binding_table);
146
147 binding_table[0] = gen8_fill_surface_state(batch, dst, GEN8_SURFACEFORMAT_R8_UNORM, 1);
148
149 return offset;
150}
151
152static uint32_t
153gen8_fill_media_kernel(struct intel_batchbuffer *batch,
154 const uint32_t kernel[][4],
155 size_t size)
156{
157 uint32_t offset;
158
159 offset = batch_copy(batch, kernel, size, 64);
160
161 return offset;
162}
163
164static uint32_t
165gen8_fill_interface_descriptor(struct intel_batchbuffer *batch, struct igt_buf *dst)
166{
167 struct gen8_interface_descriptor_data *idd;
168 uint32_t offset;
169 uint32_t binding_table_offset, kernel_offset;
170
171 binding_table_offset = gen8_fill_binding_table(batch, dst);
172 kernel_offset = gen8_fill_media_kernel(batch, media_kernel, sizeof(media_kernel));
173
174 idd = batch_alloc(batch, sizeof(*idd), 64);
175 offset = batch_offset(batch, idd);
176
177 idd->desc0.kernel_start_pointer = (kernel_offset >> 6);
178
179 idd->desc2.single_program_flow = 1;
180 idd->desc2.floating_point_mode = GEN8_FLOATING_POINT_IEEE_754;
181
182 idd->desc3.sampler_count = 0; /* 0 samplers used */
183 idd->desc3.sampler_state_pointer = 0;
184
185 idd->desc4.binding_table_entry_count = 0;
186 idd->desc4.binding_table_pointer = (binding_table_offset >> 5);
187
188 idd->desc5.constant_urb_entry_read_offset = 0;
189 idd->desc5.constant_urb_entry_read_length = 1; /* grf 1 */
190
191 return offset;
192}
193
194static void
Zhao Yakui17371952014-02-11 10:48:44 +0800195gen9_emit_state_base_address(struct intel_batchbuffer *batch)
Zhao Yakui31e94ad2014-02-11 10:48:43 +0800196{
Zhao Yakui17371952014-02-11 10:48:44 +0800197 OUT_BATCH(GEN8_STATE_BASE_ADDRESS | (19 - 2));
Zhao Yakui31e94ad2014-02-11 10:48:43 +0800198
199 /* general */
200 OUT_BATCH(0 | BASE_ADDRESS_MODIFY);
201 OUT_BATCH(0);
202
203 /* stateless data port */
204 OUT_BATCH(0 | BASE_ADDRESS_MODIFY);
205
206 /* surface */
207 OUT_RELOC(batch->bo, I915_GEM_DOMAIN_SAMPLER, 0, BASE_ADDRESS_MODIFY);
Zhao Yakui31e94ad2014-02-11 10:48:43 +0800208
209 /* dynamic */
210 OUT_RELOC(batch->bo, I915_GEM_DOMAIN_RENDER | I915_GEM_DOMAIN_INSTRUCTION,
211 0, BASE_ADDRESS_MODIFY);
Zhao Yakui31e94ad2014-02-11 10:48:43 +0800212
213 /* indirect */
214 OUT_BATCH(0);
215 OUT_BATCH(0);
216
217 /* instruction */
218 OUT_RELOC(batch->bo, I915_GEM_DOMAIN_INSTRUCTION, 0, BASE_ADDRESS_MODIFY);
Zhao Yakui31e94ad2014-02-11 10:48:43 +0800219
220 /* general state buffer size */
221 OUT_BATCH(0xfffff000 | 1);
222 /* dynamic state buffer size */
223 OUT_BATCH(1 << 12 | 1);
224 /* indirect object buffer size */
225 OUT_BATCH(0xfffff000 | 1);
226 /* intruction buffer size, must set modify enable bit, otherwise it may result in GPU hang */
227 OUT_BATCH(1 << 12 | 1);
Zhao Yakui17371952014-02-11 10:48:44 +0800228
229 /* Bindless surface state base address */
230 OUT_BATCH(0 | BASE_ADDRESS_MODIFY);
231 OUT_BATCH(0);
232 OUT_BATCH(0xfffff000);
Zhao Yakui31e94ad2014-02-11 10:48:43 +0800233}
234
235static void
236gen8_emit_vfe_state(struct intel_batchbuffer *batch)
237{
238 OUT_BATCH(GEN8_MEDIA_VFE_STATE | (9 - 2));
239
240 /* scratch buffer */
241 OUT_BATCH(0);
242 OUT_BATCH(0);
243
244 /* number of threads & urb entries */
245 OUT_BATCH(1 << 16 |
246 2 << 8);
247
248 OUT_BATCH(0);
249
250 /* urb entry size & curbe size */
251 OUT_BATCH(2 << 16 |
252 2);
253
254 /* scoreboard */
255 OUT_BATCH(0);
256 OUT_BATCH(0);
257 OUT_BATCH(0);
258}
259
260static void
261gen8_emit_curbe_load(struct intel_batchbuffer *batch, uint32_t curbe_buffer)
262{
263 OUT_BATCH(GEN8_MEDIA_CURBE_LOAD | (4 - 2));
264 OUT_BATCH(0);
265 /* curbe total data length */
266 OUT_BATCH(64);
267 /* curbe data start address, is relative to the dynamics base address */
268 OUT_BATCH(curbe_buffer);
269}
270
271static void
272gen8_emit_interface_descriptor_load(struct intel_batchbuffer *batch, uint32_t interface_descriptor)
273{
274 OUT_BATCH(GEN8_MEDIA_INTERFACE_DESCRIPTOR_LOAD | (4 - 2));
275 OUT_BATCH(0);
276 /* interface descriptor data length */
277 OUT_BATCH(sizeof(struct gen8_interface_descriptor_data));
278 /* interface descriptor address, is relative to the dynamics base address */
279 OUT_BATCH(interface_descriptor);
280}
281
282static void
283gen8_emit_media_state_flush(struct intel_batchbuffer *batch)
284{
285 OUT_BATCH(GEN8_MEDIA_STATE_FLUSH | (2 - 2));
286 OUT_BATCH(0);
287}
288
289static void
290gen8_emit_media_objects(struct intel_batchbuffer *batch,
291 unsigned x, unsigned y,
292 unsigned width, unsigned height)
293{
294 int i, j;
295
296 for (i = 0; i < width / 16; i++) {
297 for (j = 0; j < height / 16; j++) {
298 OUT_BATCH(GEN8_MEDIA_OBJECT | (8 - 2));
299
300 /* interface descriptor offset */
301 OUT_BATCH(0);
302
303 /* without indirect data */
304 OUT_BATCH(0);
305 OUT_BATCH(0);
306
307 /* scoreboard */
308 OUT_BATCH(0);
309 OUT_BATCH(0);
310
311 /* inline data (xoffset, yoffset) */
312 OUT_BATCH(x + i * 16);
313 OUT_BATCH(y + j * 16);
314 gen8_emit_media_state_flush(batch);
315 }
316 }
317}
318
319/*
320 * This sets up the media pipeline,
321 *
322 * +---------------+ <---- 4096
323 * | ^ |
324 * | | |
325 * | various |
326 * | state |
327 * | | |
328 * |_______|_______| <---- 2048 + ?
329 * | ^ |
330 * | | |
331 * | batch |
332 * | commands |
333 * | | |
334 * | | |
335 * +---------------+ <---- 0 + ?
336 *
337 */
338
339#define BATCH_STATE_SPLIT 2048
340
341void
342gen9_media_fillfunc(struct intel_batchbuffer *batch,
343 struct igt_buf *dst,
344 unsigned x, unsigned y,
345 unsigned width, unsigned height,
346 uint8_t color)
347{
348 uint32_t curbe_buffer, interface_descriptor;
349 uint32_t batch_end;
350
351 intel_batchbuffer_flush(batch);
352
353 /* setup states */
354 batch->ptr = &batch->buffer[BATCH_STATE_SPLIT];
355
356 curbe_buffer = gen8_fill_curbe_buffer_data(batch, color);
357 interface_descriptor = gen8_fill_interface_descriptor(batch, dst);
358 assert(batch->ptr < &batch->buffer[4095]);
359
360 /* media pipeline */
361 batch->ptr = batch->buffer;
Zhao Yakui54465b82014-02-11 10:48:45 +0800362 OUT_BATCH(GEN8_PIPELINE_SELECT | PIPELINE_SELECT_MEDIA |
Zhao Yakuid2e4eb32014-02-11 10:48:46 +0800363 GEN9_FORCE_MEDIA_AWAKE_ENABLE |
Zhao Yakui20e9cf32014-02-11 10:48:47 +0800364 GEN9_SAMPLER_DOP_GATE_DISABLE |
Zhao Yakuid2e4eb32014-02-11 10:48:46 +0800365 GEN9_PIPELINE_SELECTION_MASK |
Zhao Yakui20e9cf32014-02-11 10:48:47 +0800366 GEN9_SAMPLER_DOP_GATE_MASK |
Zhao Yakuid2e4eb32014-02-11 10:48:46 +0800367 GEN9_FORCE_MEDIA_AWAKE_MASK);
Zhao Yakui17371952014-02-11 10:48:44 +0800368 gen9_emit_state_base_address(batch);
Zhao Yakui31e94ad2014-02-11 10:48:43 +0800369
370 gen8_emit_vfe_state(batch);
371
372 gen8_emit_curbe_load(batch, curbe_buffer);
373
374 gen8_emit_interface_descriptor_load(batch, interface_descriptor);
375
376 gen8_emit_media_objects(batch, x, y, width, height);
377
Zhao Yakuid2e4eb32014-02-11 10:48:46 +0800378 OUT_BATCH(GEN8_PIPELINE_SELECT | PIPELINE_SELECT_MEDIA |
379 GEN9_FORCE_MEDIA_AWAKE_DISABLE |
Zhao Yakui20e9cf32014-02-11 10:48:47 +0800380 GEN9_SAMPLER_DOP_GATE_ENABLE |
Zhao Yakuid2e4eb32014-02-11 10:48:46 +0800381 GEN9_PIPELINE_SELECTION_MASK |
Zhao Yakui20e9cf32014-02-11 10:48:47 +0800382 GEN9_SAMPLER_DOP_GATE_MASK |
Zhao Yakuid2e4eb32014-02-11 10:48:46 +0800383 GEN9_FORCE_MEDIA_AWAKE_MASK);
384
Zhao Yakui31e94ad2014-02-11 10:48:43 +0800385 OUT_BATCH(MI_BATCH_BUFFER_END);
386
387 batch_end = batch_align(batch, 8);
388 assert(batch_end < BATCH_STATE_SPLIT);
389
390 gen8_render_flush(batch, batch_end);
391 intel_batchbuffer_reset(batch);
392}