blob: 1ac85cb72b6f0dd30ea783027844fab4293fadfd [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
Chia-I Wu429a0aa2014-10-24 11:57:51 +080097 struct {
98 struct intel_ds_view *view;
99 struct intel_ds_state *state;
100 } ds;
101
Chia-I Wuc14d1562014-10-17 09:49:22 +0800102 uint32_t clear_val[4];
103
104 XGL_UINT width, height;
105 XGL_UINT samples;
106};
107
Chia-I Wu9f039862014-08-20 15:39:56 +0800108static inline int cmd_gen(const struct intel_cmd *cmd)
109{
110 return intel_gpu_gen(cmd->dev->gpu);
111}
112
Chia-I Wucdff0592014-08-22 09:27:36 +0800113static inline void cmd_reserve_reloc(struct intel_cmd *cmd,
114 XGL_UINT reloc_len)
115{
116 /* fail silently */
117 if (cmd->reloc_used + reloc_len > cmd->reloc_count) {
118 cmd->reloc_used = 0;
119 cmd->result = XGL_ERROR_TOO_MANY_MEMORY_REFERENCES;
120 }
121 assert(cmd->reloc_used + reloc_len <= cmd->reloc_count);
122}
123
Chia-I Wue24c3292014-08-21 14:05:23 +0800124void cmd_writer_grow(struct intel_cmd *cmd,
Chia-I Wu3c3edc02014-09-09 10:32:59 +0800125 enum intel_cmd_writer_type which,
Chia-I Wu72292b72014-09-09 10:48:33 +0800126 XGL_SIZE new_size);
127
Chia-I Wu00b51a82014-09-09 12:07:37 +0800128void cmd_writer_record(struct intel_cmd *cmd,
129 enum intel_cmd_writer_type which,
130 enum intel_cmd_item_type type,
131 XGL_SIZE offset, XGL_SIZE size);
132
Chia-I Wu72292b72014-09-09 10:48:33 +0800133/**
134 * Return an offset to a region that is aligned to \p alignment and has at
135 * least \p size bytes.
136 */
137static inline XGL_SIZE cmd_writer_reserve(struct intel_cmd *cmd,
138 enum intel_cmd_writer_type which,
139 XGL_SIZE alignment, XGL_SIZE size)
140{
141 struct intel_cmd_writer *writer = &cmd->writers[which];
142 XGL_SIZE offset;
143
144 assert(alignment && u_is_pow2(alignment));
145 offset = u_align(writer->used, alignment);
146
147 if (offset + size > writer->size) {
148 cmd_writer_grow(cmd, which, offset + size);
149 /* align again in case of errors */
150 offset = u_align(writer->used, alignment);
151
152 assert(offset + size <= writer->size);
153 }
154
155 return offset;
156}
Chia-I Wu00a23b22014-08-20 15:28:08 +0800157
Chia-I Wu32710d72014-08-20 16:05:22 +0800158/**
Chia-I Wubda55fd2014-08-25 12:46:10 +0800159 * Add a reloc at \p pos. No error checking.
Chia-I Wucdff0592014-08-22 09:27:36 +0800160 */
Chia-I Wu72292b72014-09-09 10:48:33 +0800161static inline void cmd_writer_reloc(struct intel_cmd *cmd,
162 enum intel_cmd_writer_type which,
Chia-I Wud7d1e482014-10-18 13:25:10 +0800163 XGL_SIZE offset, intptr_t target,
164 uint32_t target_offset, uint32_t flags)
Chia-I Wucdff0592014-08-22 09:27:36 +0800165{
166 struct intel_cmd_reloc *reloc = &cmd->relocs[cmd->reloc_used];
167
168 assert(cmd->reloc_used < cmd->reloc_count);
169
Chia-I Wu68f319d2014-09-09 09:43:21 +0800170 reloc->which = which;
Chia-I Wu72292b72014-09-09 10:48:33 +0800171 reloc->offset = offset;
Chia-I Wud7d1e482014-10-18 13:25:10 +0800172 reloc->target = target;
173 reloc->target_offset = target_offset;
Chia-I Wu32a22462014-08-26 14:13:46 +0800174 reloc->flags = flags;
Chia-I Wucdff0592014-08-22 09:27:36 +0800175
176 cmd->reloc_used++;
177}
178
179/**
Chia-I Wu72292b72014-09-09 10:48:33 +0800180 * Reserve a region from the state buffer. Both the offset, in bytes, and the
181 * pointer to the reserved region are returned.
182 *
183 * Note that \p alignment is in bytes and \p len is in DWords.
Chia-I Wue24c3292014-08-21 14:05:23 +0800184 */
Chia-I Wu72292b72014-09-09 10:48:33 +0800185static inline uint32_t cmd_state_pointer(struct intel_cmd *cmd,
Chia-I Wu00b51a82014-09-09 12:07:37 +0800186 enum intel_cmd_item_type item,
Chia-I Wu72292b72014-09-09 10:48:33 +0800187 XGL_SIZE alignment, XGL_UINT len,
188 uint32_t **dw)
Chia-I Wue24c3292014-08-21 14:05:23 +0800189{
Chia-I Wu72292b72014-09-09 10:48:33 +0800190 const enum intel_cmd_writer_type which = INTEL_CMD_WRITER_STATE;
191 const XGL_SIZE size = len << 2;
192 const XGL_SIZE offset = cmd_writer_reserve(cmd, which, alignment, size);
193 struct intel_cmd_writer *writer = &cmd->writers[which];
Chia-I Wue24c3292014-08-21 14:05:23 +0800194
Chia-I Wu72292b72014-09-09 10:48:33 +0800195 /* all states are at least aligned to 32-bytes */
196 assert(alignment % 32 == 0);
197
198 *dw = (uint32_t *) ((char *) writer->ptr + offset);
199
200 writer->used = offset + size;
201
Chia-I Wu00b51a82014-09-09 12:07:37 +0800202 if (intel_debug & INTEL_DEBUG_BATCH)
203 cmd_writer_record(cmd, which, item, offset, size);
204
Chia-I Wu72292b72014-09-09 10:48:33 +0800205 return offset;
Chia-I Wue24c3292014-08-21 14:05:23 +0800206}
207
208/**
Chia-I Wu72292b72014-09-09 10:48:33 +0800209 * Write a dynamic state to the state buffer.
Chia-I Wucdff0592014-08-22 09:27:36 +0800210 */
Chia-I Wu72292b72014-09-09 10:48:33 +0800211static inline uint32_t cmd_state_write(struct intel_cmd *cmd,
Chia-I Wu00b51a82014-09-09 12:07:37 +0800212 enum intel_cmd_item_type item,
Chia-I Wu72292b72014-09-09 10:48:33 +0800213 XGL_SIZE alignment, XGL_UINT len,
214 const uint32_t *dw)
Chia-I Wucdff0592014-08-22 09:27:36 +0800215{
Chia-I Wu72292b72014-09-09 10:48:33 +0800216 uint32_t offset, *dst;
217
Chia-I Wu00b51a82014-09-09 12:07:37 +0800218 offset = cmd_state_pointer(cmd, item, alignment, len, &dst);
Chia-I Wu72292b72014-09-09 10:48:33 +0800219 memcpy(dst, dw, len << 2);
220
221 return offset;
Chia-I Wucdff0592014-08-22 09:27:36 +0800222}
223
224/**
Chia-I Wu72292b72014-09-09 10:48:33 +0800225 * Write a surface state to the surface buffer. The offset, in bytes, of the
226 * state is returned.
227 *
228 * Note that \p alignment is in bytes and \p len is in DWords.
Chia-I Wue24c3292014-08-21 14:05:23 +0800229 */
Chia-I Wu72292b72014-09-09 10:48:33 +0800230static inline uint32_t cmd_surface_write(struct intel_cmd *cmd,
Chia-I Wu00b51a82014-09-09 12:07:37 +0800231 enum intel_cmd_item_type item,
Chia-I Wu72292b72014-09-09 10:48:33 +0800232 XGL_SIZE alignment, XGL_UINT len,
233 const uint32_t *dw)
Chia-I Wue24c3292014-08-21 14:05:23 +0800234{
Chia-I Wu00b51a82014-09-09 12:07:37 +0800235 assert(item == INTEL_CMD_ITEM_SURFACE ||
236 item == INTEL_CMD_ITEM_BINDING_TABLE);
237
238 return cmd_state_write(cmd, item, alignment, len, dw);
Chia-I Wue24c3292014-08-21 14:05:23 +0800239}
240
241/**
Chia-I Wu72292b72014-09-09 10:48:33 +0800242 * Add a relocation entry for a DWord of a surface state.
Chia-I Wucdff0592014-08-22 09:27:36 +0800243 */
Chia-I Wu72292b72014-09-09 10:48:33 +0800244static inline void cmd_surface_reloc(struct intel_cmd *cmd,
245 uint32_t offset, XGL_UINT dw_index,
246 struct intel_bo *bo,
247 uint32_t bo_offset, uint32_t reloc_flags)
Chia-I Wucdff0592014-08-22 09:27:36 +0800248{
Chia-I Wu72292b72014-09-09 10:48:33 +0800249 const enum intel_cmd_writer_type which = INTEL_CMD_WRITER_STATE;
Chia-I Wucdff0592014-08-22 09:27:36 +0800250
Chia-I Wu72292b72014-09-09 10:48:33 +0800251 cmd_writer_reloc(cmd, which, offset + (dw_index << 2),
Chia-I Wud7d1e482014-10-18 13:25:10 +0800252 (intptr_t) bo, bo_offset, reloc_flags);
253}
254
255static inline void cmd_surface_reloc_writer(struct intel_cmd *cmd,
256 uint32_t offset, XGL_UINT dw_index,
257 enum intel_cmd_writer_type writer,
258 uint32_t writer_offset)
259{
260 const enum intel_cmd_writer_type which = INTEL_CMD_WRITER_STATE;
261
262 cmd_writer_reloc(cmd, which, offset + (dw_index << 2),
263 (intptr_t) writer, writer_offset,
264 INTEL_CMD_RELOC_TARGET_IS_WRITER);
Chia-I Wucdff0592014-08-22 09:27:36 +0800265}
266
267/**
Chia-I Wu72292b72014-09-09 10:48:33 +0800268 * Write a kernel to the instruction buffer. The offset, in bytes, of the
269 * kernel is returned.
Chia-I Wue24c3292014-08-21 14:05:23 +0800270 */
Chia-I Wu72292b72014-09-09 10:48:33 +0800271static inline uint32_t cmd_instruction_write(struct intel_cmd *cmd,
272 XGL_SIZE size,
273 const void *kernel)
Chia-I Wue24c3292014-08-21 14:05:23 +0800274{
Chia-I Wu72292b72014-09-09 10:48:33 +0800275 const enum intel_cmd_writer_type which = INTEL_CMD_WRITER_INSTRUCTION;
276 /*
277 * From the Sandy Bridge PRM, volume 4 part 2, page 112:
278 *
279 * "Due to prefetch of the instruction stream, the EUs may attempt to
280 * access up to 8 instructions (128 bytes) beyond the end of the
281 * kernel program - possibly into the next memory page. Although
282 * these instructions will not be executed, software must account for
283 * the prefetch in order to avoid invalid page access faults."
284 */
285 const XGL_SIZE reserved_size = size + 128;
286 /* kernels are aligned to 64 bytes */
287 const XGL_SIZE alignment = 64;
288 const XGL_SIZE offset = cmd_writer_reserve(cmd,
289 which, alignment, reserved_size);
290 struct intel_cmd_writer *writer = &cmd->writers[which];
Chia-I Wue24c3292014-08-21 14:05:23 +0800291
Chia-I Wu72292b72014-09-09 10:48:33 +0800292 memcpy((char *) writer->ptr + offset, kernel, size);
Chia-I Wu5e25c272014-08-21 20:19:12 +0800293
Chia-I Wu72292b72014-09-09 10:48:33 +0800294 writer->used = offset + size;
295
Chia-I Wu00b51a82014-09-09 12:07:37 +0800296 if (intel_debug & INTEL_DEBUG_BATCH)
297 cmd_writer_record(cmd, which, INTEL_CMD_ITEM_KERNEL, offset, size);
298
Chia-I Wu72292b72014-09-09 10:48:33 +0800299 return offset;
300}
301
302/**
303 * Reserve a region from the batch buffer. Both the offset, in DWords, and
304 * the pointer to the reserved region are returned.
305 *
306 * Note that \p len is in DWords.
307 */
308static inline XGL_UINT cmd_batch_pointer(struct intel_cmd *cmd,
309 XGL_UINT len, uint32_t **dw)
310{
311 const enum intel_cmd_writer_type which = INTEL_CMD_WRITER_BATCH;
312 /*
313 * We know the batch bo is always aligned. Using 1 here should allow the
314 * compiler to optimize away aligning.
315 */
316 const XGL_SIZE alignment = 1;
317 const XGL_SIZE size = len << 2;
318 const XGL_SIZE offset = cmd_writer_reserve(cmd, which, alignment, size);
319 struct intel_cmd_writer *writer = &cmd->writers[which];
320
321 assert(offset % 4 == 0);
322 *dw = (uint32_t *) ((char *) writer->ptr + offset);
323
324 writer->used = offset + size;
325
326 return offset >> 2;
327}
328
329/**
330 * Write a command to the batch buffer.
331 */
332static inline XGL_UINT cmd_batch_write(struct intel_cmd *cmd,
333 XGL_UINT len, const uint32_t *dw)
334{
335 XGL_UINT pos;
336 uint32_t *dst;
337
338 pos = cmd_batch_pointer(cmd, len, &dst);
339 memcpy(dst, dw, len << 2);
340
341 return pos;
342}
343
344/**
345 * Add a relocation entry for a DWord of a command.
346 */
347static inline void cmd_batch_reloc(struct intel_cmd *cmd, XGL_UINT pos,
348 struct intel_bo *bo,
349 uint32_t bo_offset, uint32_t reloc_flags)
350{
351 const enum intel_cmd_writer_type which = INTEL_CMD_WRITER_BATCH;
352
Chia-I Wud7d1e482014-10-18 13:25:10 +0800353 cmd_writer_reloc(cmd, which, pos << 2, (intptr_t) bo, bo_offset, reloc_flags);
354}
355
356static inline void cmd_batch_reloc_writer(struct intel_cmd *cmd, XGL_UINT pos,
357 enum intel_cmd_writer_type writer,
358 uint32_t writer_offset)
359{
360 const enum intel_cmd_writer_type which = INTEL_CMD_WRITER_BATCH;
361
362 cmd_writer_reloc(cmd, which, pos << 2, (intptr_t) writer, writer_offset,
363 INTEL_CMD_RELOC_TARGET_IS_WRITER);
Chia-I Wue24c3292014-08-21 14:05:23 +0800364}
365
366/**
Chia-I Wu48c283d2014-08-25 23:13:46 +0800367 * Begin the batch buffer.
Chia-I Wu79dfbb32014-08-25 12:19:02 +0800368 */
369static inline void cmd_batch_begin(struct intel_cmd *cmd)
370{
Chia-I Wu48c283d2014-08-25 23:13:46 +0800371 /* STATE_BASE_ADDRESS */
Chia-I Wu79dfbb32014-08-25 12:19:02 +0800372 const uint8_t cmd_len = 10;
Chia-I Wu426072d2014-08-26 14:31:55 +0800373 const uint32_t dw0 = GEN6_RENDER_CMD(COMMON, STATE_BASE_ADDRESS) |
Chia-I Wu79dfbb32014-08-25 12:19:02 +0800374 (cmd_len - 2);
Chia-I Wu72292b72014-09-09 10:48:33 +0800375 XGL_UINT pos;
376 uint32_t *dw;
Chia-I Wu79dfbb32014-08-25 12:19:02 +0800377
378 CMD_ASSERT(cmd, 6, 7.5);
379
Chia-I Wu72292b72014-09-09 10:48:33 +0800380 pos = cmd_batch_pointer(cmd, cmd_len, &dw);
Chia-I Wu79dfbb32014-08-25 12:19:02 +0800381
Chia-I Wu72292b72014-09-09 10:48:33 +0800382 dw[0] = dw0;
Chia-I Wu79dfbb32014-08-25 12:19:02 +0800383 /* start offsets */
Chia-I Wu72292b72014-09-09 10:48:33 +0800384 dw[1] = 1;
385 dw[2] = 1;
386 dw[3] = 1;
387 dw[4] = 1;
388 dw[5] = 1;
Chia-I Wu79dfbb32014-08-25 12:19:02 +0800389 /* end offsets */
Chia-I Wu72292b72014-09-09 10:48:33 +0800390 dw[6] = 1;
391 dw[7] = 1 + 0xfffff000;
392 dw[8] = 1 + 0xfffff000;
393 dw[9] = 1;
Chia-I Wud7d1e482014-10-18 13:25:10 +0800394
395 cmd_reserve_reloc(cmd, 3);
396 cmd_batch_reloc_writer(cmd, pos + 2, INTEL_CMD_WRITER_STATE, 1);
397 cmd_batch_reloc_writer(cmd, pos + 3, INTEL_CMD_WRITER_STATE, 1);
398 cmd_batch_reloc_writer(cmd, pos + 5, INTEL_CMD_WRITER_INSTRUCTION, 1);
Chia-I Wu79dfbb32014-08-25 12:19:02 +0800399}
400
401/**
Chia-I Wue24c3292014-08-21 14:05:23 +0800402 * End the batch buffer.
403 */
404static inline void cmd_batch_end(struct intel_cmd *cmd)
405{
Chia-I Wu68f319d2014-09-09 09:43:21 +0800406 struct intel_cmd_writer *writer = &cmd->writers[INTEL_CMD_WRITER_BATCH];
Chia-I Wu72292b72014-09-09 10:48:33 +0800407 uint32_t *dw;
Chia-I Wu79dfbb32014-08-25 12:19:02 +0800408
Chia-I Wu72292b72014-09-09 10:48:33 +0800409 if (writer->used & 0x7) {
410 cmd_batch_pointer(cmd, 1, &dw);
411 dw[0] = GEN6_MI_CMD(MI_BATCH_BUFFER_END);
Chia-I Wue24c3292014-08-21 14:05:23 +0800412 } else {
Chia-I Wu72292b72014-09-09 10:48:33 +0800413 cmd_batch_pointer(cmd, 2, &dw);
414 dw[0] = GEN6_MI_CMD(MI_BATCH_BUFFER_END);
415 dw[1] = GEN6_MI_CMD(MI_NOOP);
Chia-I Wue24c3292014-08-21 14:05:23 +0800416 }
Chia-I Wu343b1372014-08-20 16:39:20 +0800417}
418
Chia-I Wu525c6602014-08-27 10:22:34 +0800419void cmd_batch_flush(struct intel_cmd *cmd, uint32_t pipe_control_dw0);
420
Chia-I Wu759fa2e2014-08-30 18:44:47 +0800421void cmd_batch_depth_count(struct intel_cmd *cmd,
422 struct intel_bo *bo,
423 XGL_GPU_SIZE offset);
424
Chia-I Wue8dbd5d2014-08-31 13:15:58 +0800425void cmd_batch_timestamp(struct intel_cmd *cmd,
426 struct intel_bo *bo,
427 XGL_GPU_SIZE offset);
428
429void cmd_batch_immediate(struct intel_cmd *cmd,
430 struct intel_bo *bo,
431 XGL_GPU_SIZE offset,
432 uint64_t val);
Chia-I Wu1cbc0052014-08-25 09:50:12 +0800433
Chia-I Wuc14d1562014-10-17 09:49:22 +0800434void cmd_draw_meta(struct intel_cmd *cmd, const struct intel_cmd_meta *meta);
435
Chia-I Wu00a23b22014-08-20 15:28:08 +0800436#endif /* CMD_PRIV_H */