blob: f145521fb5995620c5194c5608006a5c3c98e84b [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 Wu9f039862014-08-20 15:39:56 +080078static inline int cmd_gen(const struct intel_cmd *cmd)
79{
80 return intel_gpu_gen(cmd->dev->gpu);
81}
82
Chia-I Wucdff0592014-08-22 09:27:36 +080083static inline void cmd_reserve_reloc(struct intel_cmd *cmd,
84 XGL_UINT reloc_len)
85{
86 /* fail silently */
87 if (cmd->reloc_used + reloc_len > cmd->reloc_count) {
88 cmd->reloc_used = 0;
89 cmd->result = XGL_ERROR_TOO_MANY_MEMORY_REFERENCES;
90 }
91 assert(cmd->reloc_used + reloc_len <= cmd->reloc_count);
92}
93
Chia-I Wue24c3292014-08-21 14:05:23 +080094void cmd_writer_grow(struct intel_cmd *cmd,
Chia-I Wu3c3edc02014-09-09 10:32:59 +080095 enum intel_cmd_writer_type which,
Chia-I Wu72292b72014-09-09 10:48:33 +080096 XGL_SIZE new_size);
97
Chia-I Wu00b51a82014-09-09 12:07:37 +080098void cmd_writer_record(struct intel_cmd *cmd,
99 enum intel_cmd_writer_type which,
100 enum intel_cmd_item_type type,
101 XGL_SIZE offset, XGL_SIZE size);
102
Chia-I Wu72292b72014-09-09 10:48:33 +0800103/**
104 * Return an offset to a region that is aligned to \p alignment and has at
105 * least \p size bytes.
106 */
107static inline XGL_SIZE cmd_writer_reserve(struct intel_cmd *cmd,
108 enum intel_cmd_writer_type which,
109 XGL_SIZE alignment, XGL_SIZE size)
110{
111 struct intel_cmd_writer *writer = &cmd->writers[which];
112 XGL_SIZE offset;
113
114 assert(alignment && u_is_pow2(alignment));
115 offset = u_align(writer->used, alignment);
116
117 if (offset + size > writer->size) {
118 cmd_writer_grow(cmd, which, offset + size);
119 /* align again in case of errors */
120 offset = u_align(writer->used, alignment);
121
122 assert(offset + size <= writer->size);
123 }
124
125 return offset;
126}
Chia-I Wu00a23b22014-08-20 15:28:08 +0800127
Chia-I Wu32710d72014-08-20 16:05:22 +0800128/**
Chia-I Wubda55fd2014-08-25 12:46:10 +0800129 * Add a reloc at \p pos. No error checking.
Chia-I Wucdff0592014-08-22 09:27:36 +0800130 */
Chia-I Wu72292b72014-09-09 10:48:33 +0800131static inline void cmd_writer_reloc(struct intel_cmd *cmd,
132 enum intel_cmd_writer_type which,
Chia-I Wud7d1e482014-10-18 13:25:10 +0800133 XGL_SIZE offset, intptr_t target,
134 uint32_t target_offset, uint32_t flags)
Chia-I Wucdff0592014-08-22 09:27:36 +0800135{
136 struct intel_cmd_reloc *reloc = &cmd->relocs[cmd->reloc_used];
137
138 assert(cmd->reloc_used < cmd->reloc_count);
139
Chia-I Wu68f319d2014-09-09 09:43:21 +0800140 reloc->which = which;
Chia-I Wu72292b72014-09-09 10:48:33 +0800141 reloc->offset = offset;
Chia-I Wud7d1e482014-10-18 13:25:10 +0800142 reloc->target = target;
143 reloc->target_offset = target_offset;
Chia-I Wu32a22462014-08-26 14:13:46 +0800144 reloc->flags = flags;
Chia-I Wucdff0592014-08-22 09:27:36 +0800145
146 cmd->reloc_used++;
147}
148
149/**
Chia-I Wu72292b72014-09-09 10:48:33 +0800150 * Reserve a region from the state buffer. Both the offset, in bytes, and the
151 * pointer to the reserved region are returned.
152 *
153 * Note that \p alignment is in bytes and \p len is in DWords.
Chia-I Wue24c3292014-08-21 14:05:23 +0800154 */
Chia-I Wu72292b72014-09-09 10:48:33 +0800155static inline uint32_t cmd_state_pointer(struct intel_cmd *cmd,
Chia-I Wu00b51a82014-09-09 12:07:37 +0800156 enum intel_cmd_item_type item,
Chia-I Wu72292b72014-09-09 10:48:33 +0800157 XGL_SIZE alignment, XGL_UINT len,
158 uint32_t **dw)
Chia-I Wue24c3292014-08-21 14:05:23 +0800159{
Chia-I Wu72292b72014-09-09 10:48:33 +0800160 const enum intel_cmd_writer_type which = INTEL_CMD_WRITER_STATE;
161 const XGL_SIZE size = len << 2;
162 const XGL_SIZE offset = cmd_writer_reserve(cmd, which, alignment, size);
163 struct intel_cmd_writer *writer = &cmd->writers[which];
Chia-I Wue24c3292014-08-21 14:05:23 +0800164
Chia-I Wu72292b72014-09-09 10:48:33 +0800165 /* all states are at least aligned to 32-bytes */
166 assert(alignment % 32 == 0);
167
168 *dw = (uint32_t *) ((char *) writer->ptr + offset);
169
170 writer->used = offset + size;
171
Chia-I Wu00b51a82014-09-09 12:07:37 +0800172 if (intel_debug & INTEL_DEBUG_BATCH)
173 cmd_writer_record(cmd, which, item, offset, size);
174
Chia-I Wu72292b72014-09-09 10:48:33 +0800175 return offset;
Chia-I Wue24c3292014-08-21 14:05:23 +0800176}
177
178/**
Chia-I Wu72292b72014-09-09 10:48:33 +0800179 * Write a dynamic state to the state buffer.
Chia-I Wucdff0592014-08-22 09:27:36 +0800180 */
Chia-I Wu72292b72014-09-09 10:48:33 +0800181static inline uint32_t cmd_state_write(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 const uint32_t *dw)
Chia-I Wucdff0592014-08-22 09:27:36 +0800185{
Chia-I Wu72292b72014-09-09 10:48:33 +0800186 uint32_t offset, *dst;
187
Chia-I Wu00b51a82014-09-09 12:07:37 +0800188 offset = cmd_state_pointer(cmd, item, alignment, len, &dst);
Chia-I Wu72292b72014-09-09 10:48:33 +0800189 memcpy(dst, dw, len << 2);
190
191 return offset;
Chia-I Wucdff0592014-08-22 09:27:36 +0800192}
193
194/**
Chia-I Wu72292b72014-09-09 10:48:33 +0800195 * Write a surface state to the surface buffer. The offset, in bytes, of the
196 * state is returned.
197 *
198 * Note that \p alignment is in bytes and \p len is in DWords.
Chia-I Wue24c3292014-08-21 14:05:23 +0800199 */
Chia-I Wu72292b72014-09-09 10:48:33 +0800200static inline uint32_t cmd_surface_write(struct intel_cmd *cmd,
Chia-I Wu00b51a82014-09-09 12:07:37 +0800201 enum intel_cmd_item_type item,
Chia-I Wu72292b72014-09-09 10:48:33 +0800202 XGL_SIZE alignment, XGL_UINT len,
203 const uint32_t *dw)
Chia-I Wue24c3292014-08-21 14:05:23 +0800204{
Chia-I Wu00b51a82014-09-09 12:07:37 +0800205 assert(item == INTEL_CMD_ITEM_SURFACE ||
206 item == INTEL_CMD_ITEM_BINDING_TABLE);
207
208 return cmd_state_write(cmd, item, alignment, len, dw);
Chia-I Wue24c3292014-08-21 14:05:23 +0800209}
210
211/**
Chia-I Wu72292b72014-09-09 10:48:33 +0800212 * Add a relocation entry for a DWord of a surface state.
Chia-I Wucdff0592014-08-22 09:27:36 +0800213 */
Chia-I Wu72292b72014-09-09 10:48:33 +0800214static inline void cmd_surface_reloc(struct intel_cmd *cmd,
215 uint32_t offset, XGL_UINT dw_index,
216 struct intel_bo *bo,
217 uint32_t bo_offset, uint32_t reloc_flags)
Chia-I Wucdff0592014-08-22 09:27:36 +0800218{
Chia-I Wu72292b72014-09-09 10:48:33 +0800219 const enum intel_cmd_writer_type which = INTEL_CMD_WRITER_STATE;
Chia-I Wucdff0592014-08-22 09:27:36 +0800220
Chia-I Wu72292b72014-09-09 10:48:33 +0800221 cmd_writer_reloc(cmd, which, offset + (dw_index << 2),
Chia-I Wud7d1e482014-10-18 13:25:10 +0800222 (intptr_t) bo, bo_offset, reloc_flags);
223}
224
225static inline void cmd_surface_reloc_writer(struct intel_cmd *cmd,
226 uint32_t offset, XGL_UINT dw_index,
227 enum intel_cmd_writer_type writer,
228 uint32_t writer_offset)
229{
230 const enum intel_cmd_writer_type which = INTEL_CMD_WRITER_STATE;
231
232 cmd_writer_reloc(cmd, which, offset + (dw_index << 2),
233 (intptr_t) writer, writer_offset,
234 INTEL_CMD_RELOC_TARGET_IS_WRITER);
Chia-I Wucdff0592014-08-22 09:27:36 +0800235}
236
237/**
Chia-I Wu72292b72014-09-09 10:48:33 +0800238 * Write a kernel to the instruction buffer. The offset, in bytes, of the
239 * kernel is returned.
Chia-I Wue24c3292014-08-21 14:05:23 +0800240 */
Chia-I Wu72292b72014-09-09 10:48:33 +0800241static inline uint32_t cmd_instruction_write(struct intel_cmd *cmd,
242 XGL_SIZE size,
243 const void *kernel)
Chia-I Wue24c3292014-08-21 14:05:23 +0800244{
Chia-I Wu72292b72014-09-09 10:48:33 +0800245 const enum intel_cmd_writer_type which = INTEL_CMD_WRITER_INSTRUCTION;
246 /*
247 * From the Sandy Bridge PRM, volume 4 part 2, page 112:
248 *
249 * "Due to prefetch of the instruction stream, the EUs may attempt to
250 * access up to 8 instructions (128 bytes) beyond the end of the
251 * kernel program - possibly into the next memory page. Although
252 * these instructions will not be executed, software must account for
253 * the prefetch in order to avoid invalid page access faults."
254 */
255 const XGL_SIZE reserved_size = size + 128;
256 /* kernels are aligned to 64 bytes */
257 const XGL_SIZE alignment = 64;
258 const XGL_SIZE offset = cmd_writer_reserve(cmd,
259 which, alignment, reserved_size);
260 struct intel_cmd_writer *writer = &cmd->writers[which];
Chia-I Wue24c3292014-08-21 14:05:23 +0800261
Chia-I Wu72292b72014-09-09 10:48:33 +0800262 memcpy((char *) writer->ptr + offset, kernel, size);
Chia-I Wu5e25c272014-08-21 20:19:12 +0800263
Chia-I Wu72292b72014-09-09 10:48:33 +0800264 writer->used = offset + size;
265
Chia-I Wu00b51a82014-09-09 12:07:37 +0800266 if (intel_debug & INTEL_DEBUG_BATCH)
267 cmd_writer_record(cmd, which, INTEL_CMD_ITEM_KERNEL, offset, size);
268
Chia-I Wu72292b72014-09-09 10:48:33 +0800269 return offset;
270}
271
272/**
273 * Reserve a region from the batch buffer. Both the offset, in DWords, and
274 * the pointer to the reserved region are returned.
275 *
276 * Note that \p len is in DWords.
277 */
278static inline XGL_UINT cmd_batch_pointer(struct intel_cmd *cmd,
279 XGL_UINT len, uint32_t **dw)
280{
281 const enum intel_cmd_writer_type which = INTEL_CMD_WRITER_BATCH;
282 /*
283 * We know the batch bo is always aligned. Using 1 here should allow the
284 * compiler to optimize away aligning.
285 */
286 const XGL_SIZE alignment = 1;
287 const XGL_SIZE size = len << 2;
288 const XGL_SIZE offset = cmd_writer_reserve(cmd, which, alignment, size);
289 struct intel_cmd_writer *writer = &cmd->writers[which];
290
291 assert(offset % 4 == 0);
292 *dw = (uint32_t *) ((char *) writer->ptr + offset);
293
294 writer->used = offset + size;
295
296 return offset >> 2;
297}
298
299/**
300 * Write a command to the batch buffer.
301 */
302static inline XGL_UINT cmd_batch_write(struct intel_cmd *cmd,
303 XGL_UINT len, const uint32_t *dw)
304{
305 XGL_UINT pos;
306 uint32_t *dst;
307
308 pos = cmd_batch_pointer(cmd, len, &dst);
309 memcpy(dst, dw, len << 2);
310
311 return pos;
312}
313
314/**
315 * Add a relocation entry for a DWord of a command.
316 */
317static inline void cmd_batch_reloc(struct intel_cmd *cmd, XGL_UINT pos,
318 struct intel_bo *bo,
319 uint32_t bo_offset, uint32_t reloc_flags)
320{
321 const enum intel_cmd_writer_type which = INTEL_CMD_WRITER_BATCH;
322
Chia-I Wud7d1e482014-10-18 13:25:10 +0800323 cmd_writer_reloc(cmd, which, pos << 2, (intptr_t) bo, bo_offset, reloc_flags);
324}
325
326static inline void cmd_batch_reloc_writer(struct intel_cmd *cmd, XGL_UINT pos,
327 enum intel_cmd_writer_type writer,
328 uint32_t writer_offset)
329{
330 const enum intel_cmd_writer_type which = INTEL_CMD_WRITER_BATCH;
331
332 cmd_writer_reloc(cmd, which, pos << 2, (intptr_t) writer, writer_offset,
333 INTEL_CMD_RELOC_TARGET_IS_WRITER);
Chia-I Wue24c3292014-08-21 14:05:23 +0800334}
335
336/**
Chia-I Wu48c283d2014-08-25 23:13:46 +0800337 * Begin the batch buffer.
Chia-I Wu79dfbb32014-08-25 12:19:02 +0800338 */
339static inline void cmd_batch_begin(struct intel_cmd *cmd)
340{
Chia-I Wu48c283d2014-08-25 23:13:46 +0800341 /* STATE_BASE_ADDRESS */
Chia-I Wu79dfbb32014-08-25 12:19:02 +0800342 const uint8_t cmd_len = 10;
Chia-I Wu426072d2014-08-26 14:31:55 +0800343 const uint32_t dw0 = GEN6_RENDER_CMD(COMMON, STATE_BASE_ADDRESS) |
Chia-I Wu79dfbb32014-08-25 12:19:02 +0800344 (cmd_len - 2);
Chia-I Wu72292b72014-09-09 10:48:33 +0800345 XGL_UINT pos;
346 uint32_t *dw;
Chia-I Wu79dfbb32014-08-25 12:19:02 +0800347
348 CMD_ASSERT(cmd, 6, 7.5);
349
Chia-I Wu72292b72014-09-09 10:48:33 +0800350 pos = cmd_batch_pointer(cmd, cmd_len, &dw);
Chia-I Wu79dfbb32014-08-25 12:19:02 +0800351
Chia-I Wu72292b72014-09-09 10:48:33 +0800352 dw[0] = dw0;
Chia-I Wu79dfbb32014-08-25 12:19:02 +0800353 /* start offsets */
Chia-I Wu72292b72014-09-09 10:48:33 +0800354 dw[1] = 1;
355 dw[2] = 1;
356 dw[3] = 1;
357 dw[4] = 1;
358 dw[5] = 1;
Chia-I Wu79dfbb32014-08-25 12:19:02 +0800359 /* end offsets */
Chia-I Wu72292b72014-09-09 10:48:33 +0800360 dw[6] = 1;
361 dw[7] = 1 + 0xfffff000;
362 dw[8] = 1 + 0xfffff000;
363 dw[9] = 1;
Chia-I Wud7d1e482014-10-18 13:25:10 +0800364
365 cmd_reserve_reloc(cmd, 3);
366 cmd_batch_reloc_writer(cmd, pos + 2, INTEL_CMD_WRITER_STATE, 1);
367 cmd_batch_reloc_writer(cmd, pos + 3, INTEL_CMD_WRITER_STATE, 1);
368 cmd_batch_reloc_writer(cmd, pos + 5, INTEL_CMD_WRITER_INSTRUCTION, 1);
Chia-I Wu79dfbb32014-08-25 12:19:02 +0800369}
370
371/**
Chia-I Wue24c3292014-08-21 14:05:23 +0800372 * End the batch buffer.
373 */
374static inline void cmd_batch_end(struct intel_cmd *cmd)
375{
Chia-I Wu68f319d2014-09-09 09:43:21 +0800376 struct intel_cmd_writer *writer = &cmd->writers[INTEL_CMD_WRITER_BATCH];
Chia-I Wu72292b72014-09-09 10:48:33 +0800377 uint32_t *dw;
Chia-I Wu79dfbb32014-08-25 12:19:02 +0800378
Chia-I Wu72292b72014-09-09 10:48:33 +0800379 if (writer->used & 0x7) {
380 cmd_batch_pointer(cmd, 1, &dw);
381 dw[0] = GEN6_MI_CMD(MI_BATCH_BUFFER_END);
Chia-I Wue24c3292014-08-21 14:05:23 +0800382 } else {
Chia-I Wu72292b72014-09-09 10:48:33 +0800383 cmd_batch_pointer(cmd, 2, &dw);
384 dw[0] = GEN6_MI_CMD(MI_BATCH_BUFFER_END);
385 dw[1] = GEN6_MI_CMD(MI_NOOP);
Chia-I Wue24c3292014-08-21 14:05:23 +0800386 }
Chia-I Wu343b1372014-08-20 16:39:20 +0800387}
388
Chia-I Wu525c6602014-08-27 10:22:34 +0800389void cmd_batch_flush(struct intel_cmd *cmd, uint32_t pipe_control_dw0);
390
Chia-I Wu759fa2e2014-08-30 18:44:47 +0800391void cmd_batch_depth_count(struct intel_cmd *cmd,
392 struct intel_bo *bo,
393 XGL_GPU_SIZE offset);
394
Chia-I Wue8dbd5d2014-08-31 13:15:58 +0800395void cmd_batch_timestamp(struct intel_cmd *cmd,
396 struct intel_bo *bo,
397 XGL_GPU_SIZE offset);
398
399void cmd_batch_immediate(struct intel_cmd *cmd,
400 struct intel_bo *bo,
401 XGL_GPU_SIZE offset,
402 uint64_t val);
Chia-I Wu1cbc0052014-08-25 09:50:12 +0800403
Chia-I Wu00a23b22014-08-20 15:28:08 +0800404#endif /* CMD_PRIV_H */