blob: b2d684e71de87366d01e2582eebcd5f591304f12 [file] [log] [blame]
Chia-I Wu00a23b22014-08-20 15:28:08 +08001/*
2 * XGL
3 *
4 * Copyright (C) 2014 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
Chia-I Wu44e42362014-09-02 08:32:09 +080023 *
24 * Authors:
25 * Chia-I Wu <olv@lunarg.com>
Chia-I Wu00a23b22014-08-20 15:28:08 +080026 */
27
28#ifndef CMD_PRIV_H
29#define CMD_PRIV_H
30
Chia-I Wue24c3292014-08-21 14:05:23 +080031#include "genhw/genhw.h"
Chia-I Wu32710d72014-08-20 16:05:22 +080032#include "dev.h"
33#include "gpu.h"
Chia-I Wu00a23b22014-08-20 15:28:08 +080034#include "cmd.h"
35
Chia-I Wu32710d72014-08-20 16:05:22 +080036#define CMD_ASSERT(cmd, min_gen, max_gen) \
37 INTEL_GPU_ASSERT((cmd)->dev->gpu, (min_gen), (max_gen))
38
Chia-I Wu00b51a82014-09-09 12:07:37 +080039enum intel_cmd_item_type {
40 /* for state buffer */
41 INTEL_CMD_ITEM_BLOB,
42 INTEL_CMD_ITEM_CLIP_VIEWPORT,
43 INTEL_CMD_ITEM_SF_VIEWPORT,
44 INTEL_CMD_ITEM_SCISSOR_RECT,
45 INTEL_CMD_ITEM_CC_VIEWPORT,
46 INTEL_CMD_ITEM_COLOR_CALC,
47 INTEL_CMD_ITEM_DEPTH_STENCIL,
48 INTEL_CMD_ITEM_BLEND,
49 INTEL_CMD_ITEM_SAMPLER,
50
51 /* for surface buffer */
52 INTEL_CMD_ITEM_SURFACE,
53 INTEL_CMD_ITEM_BINDING_TABLE,
54
55 /* for instruction buffer */
56 INTEL_CMD_ITEM_KERNEL,
57
58 INTEL_CMD_ITEM_COUNT,
59};
60
61struct intel_cmd_item {
62 enum intel_cmd_item_type type;
63 XGL_SIZE offset;
64 XGL_SIZE size;
65};
66
Chia-I Wud7d1e482014-10-18 13:25:10 +080067#define INTEL_CMD_RELOC_TARGET_IS_WRITER (1u << 31)
Chia-I Wu958d1b72014-08-21 11:28:11 +080068struct intel_cmd_reloc {
Chia-I Wu68f319d2014-09-09 09:43:21 +080069 enum intel_cmd_writer_type which;
Chia-I Wu72292b72014-09-09 10:48:33 +080070 XGL_SIZE offset;
Chia-I Wu958d1b72014-08-21 11:28:11 +080071
Chia-I Wud7d1e482014-10-18 13:25:10 +080072 intptr_t target;
73 uint32_t target_offset;
Chia-I Wu958d1b72014-08-21 11:28:11 +080074
Chia-I Wu32a22462014-08-26 14:13:46 +080075 uint32_t flags;
Chia-I Wu958d1b72014-08-21 11:28:11 +080076};
77
Chia-I Wuc14d1562014-10-17 09:49:22 +080078struct intel_ds_view;
79
80struct intel_cmd_meta {
81 enum intel_dev_meta_shader shader_id;
82
83 struct {
84 bool valid;
85
86 uint32_t surface[8];
87 XGL_UINT surface_len;
88
89 intptr_t reloc_target;
90 uint32_t reloc_offset;
91 uint32_t reloc_flags;
92
93 XGL_UINT lod, layer;
94 XGL_UINT x, y;
95 } src, dst;
96
97 struct intel_ds_view *ds;
98 uint32_t clear_val[4];
99
100 XGL_UINT width, height;
101 XGL_UINT samples;
102};
103
Chia-I Wu9f039862014-08-20 15:39:56 +0800104static inline int cmd_gen(const struct intel_cmd *cmd)
105{
106 return intel_gpu_gen(cmd->dev->gpu);
107}
108
Chia-I Wucdff0592014-08-22 09:27:36 +0800109static inline void cmd_reserve_reloc(struct intel_cmd *cmd,
110 XGL_UINT reloc_len)
111{
112 /* fail silently */
113 if (cmd->reloc_used + reloc_len > cmd->reloc_count) {
114 cmd->reloc_used = 0;
115 cmd->result = XGL_ERROR_TOO_MANY_MEMORY_REFERENCES;
116 }
117 assert(cmd->reloc_used + reloc_len <= cmd->reloc_count);
118}
119
Chia-I Wue24c3292014-08-21 14:05:23 +0800120void cmd_writer_grow(struct intel_cmd *cmd,
Chia-I Wu3c3edc02014-09-09 10:32:59 +0800121 enum intel_cmd_writer_type which,
Chia-I Wu72292b72014-09-09 10:48:33 +0800122 XGL_SIZE new_size);
123
Chia-I Wu00b51a82014-09-09 12:07:37 +0800124void cmd_writer_record(struct intel_cmd *cmd,
125 enum intel_cmd_writer_type which,
126 enum intel_cmd_item_type type,
127 XGL_SIZE offset, XGL_SIZE size);
128
Chia-I Wu72292b72014-09-09 10:48:33 +0800129/**
130 * Return an offset to a region that is aligned to \p alignment and has at
131 * least \p size bytes.
132 */
133static inline XGL_SIZE cmd_writer_reserve(struct intel_cmd *cmd,
134 enum intel_cmd_writer_type which,
135 XGL_SIZE alignment, XGL_SIZE size)
136{
137 struct intel_cmd_writer *writer = &cmd->writers[which];
138 XGL_SIZE offset;
139
140 assert(alignment && u_is_pow2(alignment));
141 offset = u_align(writer->used, alignment);
142
143 if (offset + size > writer->size) {
144 cmd_writer_grow(cmd, which, offset + size);
145 /* align again in case of errors */
146 offset = u_align(writer->used, alignment);
147
148 assert(offset + size <= writer->size);
149 }
150
151 return offset;
152}
Chia-I Wu00a23b22014-08-20 15:28:08 +0800153
Chia-I Wu32710d72014-08-20 16:05:22 +0800154/**
Chia-I Wubda55fd2014-08-25 12:46:10 +0800155 * Add a reloc at \p pos. No error checking.
Chia-I Wucdff0592014-08-22 09:27:36 +0800156 */
Chia-I Wu72292b72014-09-09 10:48:33 +0800157static inline void cmd_writer_reloc(struct intel_cmd *cmd,
158 enum intel_cmd_writer_type which,
Chia-I Wud7d1e482014-10-18 13:25:10 +0800159 XGL_SIZE offset, intptr_t target,
160 uint32_t target_offset, uint32_t flags)
Chia-I Wucdff0592014-08-22 09:27:36 +0800161{
162 struct intel_cmd_reloc *reloc = &cmd->relocs[cmd->reloc_used];
163
164 assert(cmd->reloc_used < cmd->reloc_count);
165
Chia-I Wu68f319d2014-09-09 09:43:21 +0800166 reloc->which = which;
Chia-I Wu72292b72014-09-09 10:48:33 +0800167 reloc->offset = offset;
Chia-I Wud7d1e482014-10-18 13:25:10 +0800168 reloc->target = target;
169 reloc->target_offset = target_offset;
Chia-I Wu32a22462014-08-26 14:13:46 +0800170 reloc->flags = flags;
Chia-I Wucdff0592014-08-22 09:27:36 +0800171
172 cmd->reloc_used++;
173}
174
175/**
Chia-I Wu72292b72014-09-09 10:48:33 +0800176 * Reserve a region from the state buffer. Both the offset, in bytes, and the
177 * pointer to the reserved region are returned.
178 *
179 * Note that \p alignment is in bytes and \p len is in DWords.
Chia-I Wue24c3292014-08-21 14:05:23 +0800180 */
Chia-I Wu72292b72014-09-09 10:48:33 +0800181static inline uint32_t cmd_state_pointer(struct intel_cmd *cmd,
Chia-I Wu00b51a82014-09-09 12:07:37 +0800182 enum intel_cmd_item_type item,
Chia-I Wu72292b72014-09-09 10:48:33 +0800183 XGL_SIZE alignment, XGL_UINT len,
184 uint32_t **dw)
Chia-I Wue24c3292014-08-21 14:05:23 +0800185{
Chia-I Wu72292b72014-09-09 10:48:33 +0800186 const enum intel_cmd_writer_type which = INTEL_CMD_WRITER_STATE;
187 const XGL_SIZE size = len << 2;
188 const XGL_SIZE offset = cmd_writer_reserve(cmd, which, alignment, size);
189 struct intel_cmd_writer *writer = &cmd->writers[which];
Chia-I Wue24c3292014-08-21 14:05:23 +0800190
Chia-I Wu72292b72014-09-09 10:48:33 +0800191 /* all states are at least aligned to 32-bytes */
192 assert(alignment % 32 == 0);
193
194 *dw = (uint32_t *) ((char *) writer->ptr + offset);
195
196 writer->used = offset + size;
197
Chia-I Wu00b51a82014-09-09 12:07:37 +0800198 if (intel_debug & INTEL_DEBUG_BATCH)
199 cmd_writer_record(cmd, which, item, offset, size);
200
Chia-I Wu72292b72014-09-09 10:48:33 +0800201 return offset;
Chia-I Wue24c3292014-08-21 14:05:23 +0800202}
203
204/**
Chia-I Wu72292b72014-09-09 10:48:33 +0800205 * Write a dynamic state to the state buffer.
Chia-I Wucdff0592014-08-22 09:27:36 +0800206 */
Chia-I Wu72292b72014-09-09 10:48:33 +0800207static inline uint32_t cmd_state_write(struct intel_cmd *cmd,
Chia-I Wu00b51a82014-09-09 12:07:37 +0800208 enum intel_cmd_item_type item,
Chia-I Wu72292b72014-09-09 10:48:33 +0800209 XGL_SIZE alignment, XGL_UINT len,
210 const uint32_t *dw)
Chia-I Wucdff0592014-08-22 09:27:36 +0800211{
Chia-I Wu72292b72014-09-09 10:48:33 +0800212 uint32_t offset, *dst;
213
Chia-I Wu00b51a82014-09-09 12:07:37 +0800214 offset = cmd_state_pointer(cmd, item, alignment, len, &dst);
Chia-I Wu72292b72014-09-09 10:48:33 +0800215 memcpy(dst, dw, len << 2);
216
217 return offset;
Chia-I Wucdff0592014-08-22 09:27:36 +0800218}
219
220/**
Chia-I Wu72292b72014-09-09 10:48:33 +0800221 * Write a surface state to the surface buffer. The offset, in bytes, of the
222 * state is returned.
223 *
224 * Note that \p alignment is in bytes and \p len is in DWords.
Chia-I Wue24c3292014-08-21 14:05:23 +0800225 */
Chia-I Wu72292b72014-09-09 10:48:33 +0800226static inline uint32_t cmd_surface_write(struct intel_cmd *cmd,
Chia-I Wu00b51a82014-09-09 12:07:37 +0800227 enum intel_cmd_item_type item,
Chia-I Wu72292b72014-09-09 10:48:33 +0800228 XGL_SIZE alignment, XGL_UINT len,
229 const uint32_t *dw)
Chia-I Wue24c3292014-08-21 14:05:23 +0800230{
Chia-I Wu00b51a82014-09-09 12:07:37 +0800231 assert(item == INTEL_CMD_ITEM_SURFACE ||
232 item == INTEL_CMD_ITEM_BINDING_TABLE);
233
234 return cmd_state_write(cmd, item, alignment, len, dw);
Chia-I Wue24c3292014-08-21 14:05:23 +0800235}
236
237/**
Chia-I Wu72292b72014-09-09 10:48:33 +0800238 * Add a relocation entry for a DWord of a surface state.
Chia-I Wucdff0592014-08-22 09:27:36 +0800239 */
Chia-I Wu72292b72014-09-09 10:48:33 +0800240static inline void cmd_surface_reloc(struct intel_cmd *cmd,
241 uint32_t offset, XGL_UINT dw_index,
242 struct intel_bo *bo,
243 uint32_t bo_offset, uint32_t reloc_flags)
Chia-I Wucdff0592014-08-22 09:27:36 +0800244{
Chia-I Wu72292b72014-09-09 10:48:33 +0800245 const enum intel_cmd_writer_type which = INTEL_CMD_WRITER_STATE;
Chia-I Wucdff0592014-08-22 09:27:36 +0800246
Chia-I Wu72292b72014-09-09 10:48:33 +0800247 cmd_writer_reloc(cmd, which, offset + (dw_index << 2),
Chia-I Wud7d1e482014-10-18 13:25:10 +0800248 (intptr_t) bo, bo_offset, reloc_flags);
249}
250
251static inline void cmd_surface_reloc_writer(struct intel_cmd *cmd,
252 uint32_t offset, XGL_UINT dw_index,
253 enum intel_cmd_writer_type writer,
254 uint32_t writer_offset)
255{
256 const enum intel_cmd_writer_type which = INTEL_CMD_WRITER_STATE;
257
258 cmd_writer_reloc(cmd, which, offset + (dw_index << 2),
259 (intptr_t) writer, writer_offset,
260 INTEL_CMD_RELOC_TARGET_IS_WRITER);
Chia-I Wucdff0592014-08-22 09:27:36 +0800261}
262
263/**
Chia-I Wu72292b72014-09-09 10:48:33 +0800264 * Write a kernel to the instruction buffer. The offset, in bytes, of the
265 * kernel is returned.
Chia-I Wue24c3292014-08-21 14:05:23 +0800266 */
Chia-I Wu72292b72014-09-09 10:48:33 +0800267static inline uint32_t cmd_instruction_write(struct intel_cmd *cmd,
268 XGL_SIZE size,
269 const void *kernel)
Chia-I Wue24c3292014-08-21 14:05:23 +0800270{
Chia-I Wu72292b72014-09-09 10:48:33 +0800271 const enum intel_cmd_writer_type which = INTEL_CMD_WRITER_INSTRUCTION;
272 /*
273 * From the Sandy Bridge PRM, volume 4 part 2, page 112:
274 *
275 * "Due to prefetch of the instruction stream, the EUs may attempt to
276 * access up to 8 instructions (128 bytes) beyond the end of the
277 * kernel program - possibly into the next memory page. Although
278 * these instructions will not be executed, software must account for
279 * the prefetch in order to avoid invalid page access faults."
280 */
281 const XGL_SIZE reserved_size = size + 128;
282 /* kernels are aligned to 64 bytes */
283 const XGL_SIZE alignment = 64;
284 const XGL_SIZE offset = cmd_writer_reserve(cmd,
285 which, alignment, reserved_size);
286 struct intel_cmd_writer *writer = &cmd->writers[which];
Chia-I Wue24c3292014-08-21 14:05:23 +0800287
Chia-I Wu72292b72014-09-09 10:48:33 +0800288 memcpy((char *) writer->ptr + offset, kernel, size);
Chia-I Wu5e25c272014-08-21 20:19:12 +0800289
Chia-I Wu72292b72014-09-09 10:48:33 +0800290 writer->used = offset + size;
291
Chia-I Wu00b51a82014-09-09 12:07:37 +0800292 if (intel_debug & INTEL_DEBUG_BATCH)
293 cmd_writer_record(cmd, which, INTEL_CMD_ITEM_KERNEL, offset, size);
294
Chia-I Wu72292b72014-09-09 10:48:33 +0800295 return offset;
296}
297
298/**
299 * Reserve a region from the batch buffer. Both the offset, in DWords, and
300 * the pointer to the reserved region are returned.
301 *
302 * Note that \p len is in DWords.
303 */
304static inline XGL_UINT cmd_batch_pointer(struct intel_cmd *cmd,
305 XGL_UINT len, uint32_t **dw)
306{
307 const enum intel_cmd_writer_type which = INTEL_CMD_WRITER_BATCH;
308 /*
309 * We know the batch bo is always aligned. Using 1 here should allow the
310 * compiler to optimize away aligning.
311 */
312 const XGL_SIZE alignment = 1;
313 const XGL_SIZE size = len << 2;
314 const XGL_SIZE offset = cmd_writer_reserve(cmd, which, alignment, size);
315 struct intel_cmd_writer *writer = &cmd->writers[which];
316
317 assert(offset % 4 == 0);
318 *dw = (uint32_t *) ((char *) writer->ptr + offset);
319
320 writer->used = offset + size;
321
322 return offset >> 2;
323}
324
325/**
326 * Write a command to the batch buffer.
327 */
328static inline XGL_UINT cmd_batch_write(struct intel_cmd *cmd,
329 XGL_UINT len, const uint32_t *dw)
330{
331 XGL_UINT pos;
332 uint32_t *dst;
333
334 pos = cmd_batch_pointer(cmd, len, &dst);
335 memcpy(dst, dw, len << 2);
336
337 return pos;
338}
339
340/**
341 * Add a relocation entry for a DWord of a command.
342 */
343static inline void cmd_batch_reloc(struct intel_cmd *cmd, XGL_UINT pos,
344 struct intel_bo *bo,
345 uint32_t bo_offset, uint32_t reloc_flags)
346{
347 const enum intel_cmd_writer_type which = INTEL_CMD_WRITER_BATCH;
348
Chia-I Wud7d1e482014-10-18 13:25:10 +0800349 cmd_writer_reloc(cmd, which, pos << 2, (intptr_t) bo, bo_offset, reloc_flags);
350}
351
352static inline void cmd_batch_reloc_writer(struct intel_cmd *cmd, XGL_UINT pos,
353 enum intel_cmd_writer_type writer,
354 uint32_t writer_offset)
355{
356 const enum intel_cmd_writer_type which = INTEL_CMD_WRITER_BATCH;
357
358 cmd_writer_reloc(cmd, which, pos << 2, (intptr_t) writer, writer_offset,
359 INTEL_CMD_RELOC_TARGET_IS_WRITER);
Chia-I Wue24c3292014-08-21 14:05:23 +0800360}
361
362/**
Chia-I Wu48c283d2014-08-25 23:13:46 +0800363 * Begin the batch buffer.
Chia-I Wu79dfbb32014-08-25 12:19:02 +0800364 */
365static inline void cmd_batch_begin(struct intel_cmd *cmd)
366{
Chia-I Wu48c283d2014-08-25 23:13:46 +0800367 /* STATE_BASE_ADDRESS */
Chia-I Wu79dfbb32014-08-25 12:19:02 +0800368 const uint8_t cmd_len = 10;
Chia-I Wu426072d2014-08-26 14:31:55 +0800369 const uint32_t dw0 = GEN6_RENDER_CMD(COMMON, STATE_BASE_ADDRESS) |
Chia-I Wu79dfbb32014-08-25 12:19:02 +0800370 (cmd_len - 2);
Chia-I Wu72292b72014-09-09 10:48:33 +0800371 XGL_UINT pos;
372 uint32_t *dw;
Chia-I Wu79dfbb32014-08-25 12:19:02 +0800373
374 CMD_ASSERT(cmd, 6, 7.5);
375
Chia-I Wu72292b72014-09-09 10:48:33 +0800376 pos = cmd_batch_pointer(cmd, cmd_len, &dw);
Chia-I Wu79dfbb32014-08-25 12:19:02 +0800377
Chia-I Wu72292b72014-09-09 10:48:33 +0800378 dw[0] = dw0;
Chia-I Wu79dfbb32014-08-25 12:19:02 +0800379 /* start offsets */
Chia-I Wu72292b72014-09-09 10:48:33 +0800380 dw[1] = 1;
381 dw[2] = 1;
382 dw[3] = 1;
383 dw[4] = 1;
384 dw[5] = 1;
Chia-I Wu79dfbb32014-08-25 12:19:02 +0800385 /* end offsets */
Chia-I Wu72292b72014-09-09 10:48:33 +0800386 dw[6] = 1;
387 dw[7] = 1 + 0xfffff000;
388 dw[8] = 1 + 0xfffff000;
389 dw[9] = 1;
Chia-I Wud7d1e482014-10-18 13:25:10 +0800390
391 cmd_reserve_reloc(cmd, 3);
392 cmd_batch_reloc_writer(cmd, pos + 2, INTEL_CMD_WRITER_STATE, 1);
393 cmd_batch_reloc_writer(cmd, pos + 3, INTEL_CMD_WRITER_STATE, 1);
394 cmd_batch_reloc_writer(cmd, pos + 5, INTEL_CMD_WRITER_INSTRUCTION, 1);
Chia-I Wu79dfbb32014-08-25 12:19:02 +0800395}
396
397/**
Chia-I Wue24c3292014-08-21 14:05:23 +0800398 * End the batch buffer.
399 */
400static inline void cmd_batch_end(struct intel_cmd *cmd)
401{
Chia-I Wu68f319d2014-09-09 09:43:21 +0800402 struct intel_cmd_writer *writer = &cmd->writers[INTEL_CMD_WRITER_BATCH];
Chia-I Wu72292b72014-09-09 10:48:33 +0800403 uint32_t *dw;
Chia-I Wu79dfbb32014-08-25 12:19:02 +0800404
Chia-I Wu72292b72014-09-09 10:48:33 +0800405 if (writer->used & 0x7) {
406 cmd_batch_pointer(cmd, 1, &dw);
407 dw[0] = GEN6_MI_CMD(MI_BATCH_BUFFER_END);
Chia-I Wue24c3292014-08-21 14:05:23 +0800408 } else {
Chia-I Wu72292b72014-09-09 10:48:33 +0800409 cmd_batch_pointer(cmd, 2, &dw);
410 dw[0] = GEN6_MI_CMD(MI_BATCH_BUFFER_END);
411 dw[1] = GEN6_MI_CMD(MI_NOOP);
Chia-I Wue24c3292014-08-21 14:05:23 +0800412 }
Chia-I Wu343b1372014-08-20 16:39:20 +0800413}
414
Chia-I Wu525c6602014-08-27 10:22:34 +0800415void cmd_batch_flush(struct intel_cmd *cmd, uint32_t pipe_control_dw0);
416
Chia-I Wu759fa2e2014-08-30 18:44:47 +0800417void cmd_batch_depth_count(struct intel_cmd *cmd,
418 struct intel_bo *bo,
419 XGL_GPU_SIZE offset);
420
Chia-I Wue8dbd5d2014-08-31 13:15:58 +0800421void cmd_batch_timestamp(struct intel_cmd *cmd,
422 struct intel_bo *bo,
423 XGL_GPU_SIZE offset);
424
425void cmd_batch_immediate(struct intel_cmd *cmd,
426 struct intel_bo *bo,
427 XGL_GPU_SIZE offset,
428 uint64_t val);
Chia-I Wu1cbc0052014-08-25 09:50:12 +0800429
Chia-I Wuc14d1562014-10-17 09:49:22 +0800430void cmd_draw_meta(struct intel_cmd *cmd, const struct intel_cmd_meta *meta);
431
Chia-I Wu00a23b22014-08-20 15:28:08 +0800432#endif /* CMD_PRIV_H */