blob: 6fbd524b6a8090ed5488aa1479565a96ef7855c6 [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 Wu958d1b72014-08-21 11:28:11 +080039struct intel_cmd_reloc {
Chia-I Wu68f319d2014-09-09 09:43:21 +080040 enum intel_cmd_writer_type which;
Chia-I Wu72292b72014-09-09 10:48:33 +080041 XGL_SIZE offset;
Chia-I Wu958d1b72014-08-21 11:28:11 +080042
Chia-I Wu9ee38722014-08-25 12:11:36 +080043 struct intel_bo *bo;
Chia-I Wu72292b72014-09-09 10:48:33 +080044 uint32_t bo_offset;
Chia-I Wu958d1b72014-08-21 11:28:11 +080045
Chia-I Wu32a22462014-08-26 14:13:46 +080046 uint32_t flags;
Chia-I Wu958d1b72014-08-21 11:28:11 +080047};
48
Chia-I Wu9f039862014-08-20 15:39:56 +080049static inline int cmd_gen(const struct intel_cmd *cmd)
50{
51 return intel_gpu_gen(cmd->dev->gpu);
52}
53
Chia-I Wucdff0592014-08-22 09:27:36 +080054static inline void cmd_reserve_reloc(struct intel_cmd *cmd,
55 XGL_UINT reloc_len)
56{
57 /* fail silently */
58 if (cmd->reloc_used + reloc_len > cmd->reloc_count) {
59 cmd->reloc_used = 0;
60 cmd->result = XGL_ERROR_TOO_MANY_MEMORY_REFERENCES;
61 }
62 assert(cmd->reloc_used + reloc_len <= cmd->reloc_count);
63}
64
Chia-I Wue24c3292014-08-21 14:05:23 +080065void cmd_writer_grow(struct intel_cmd *cmd,
Chia-I Wu3c3edc02014-09-09 10:32:59 +080066 enum intel_cmd_writer_type which,
Chia-I Wu72292b72014-09-09 10:48:33 +080067 XGL_SIZE new_size);
68
69/**
70 * Return an offset to a region that is aligned to \p alignment and has at
71 * least \p size bytes.
72 */
73static inline XGL_SIZE cmd_writer_reserve(struct intel_cmd *cmd,
74 enum intel_cmd_writer_type which,
75 XGL_SIZE alignment, XGL_SIZE size)
76{
77 struct intel_cmd_writer *writer = &cmd->writers[which];
78 XGL_SIZE offset;
79
80 assert(alignment && u_is_pow2(alignment));
81 offset = u_align(writer->used, alignment);
82
83 if (offset + size > writer->size) {
84 cmd_writer_grow(cmd, which, offset + size);
85 /* align again in case of errors */
86 offset = u_align(writer->used, alignment);
87
88 assert(offset + size <= writer->size);
89 }
90
91 return offset;
92}
Chia-I Wu00a23b22014-08-20 15:28:08 +080093
Chia-I Wu32710d72014-08-20 16:05:22 +080094/**
Chia-I Wubda55fd2014-08-25 12:46:10 +080095 * Add a reloc at \p pos. No error checking.
Chia-I Wucdff0592014-08-22 09:27:36 +080096 */
Chia-I Wu72292b72014-09-09 10:48:33 +080097static inline void cmd_writer_reloc(struct intel_cmd *cmd,
98 enum intel_cmd_writer_type which,
99 XGL_SIZE offset, struct intel_bo *bo,
100 uint32_t bo_offset, uint32_t flags)
Chia-I Wucdff0592014-08-22 09:27:36 +0800101{
102 struct intel_cmd_reloc *reloc = &cmd->relocs[cmd->reloc_used];
103
104 assert(cmd->reloc_used < cmd->reloc_count);
105
Chia-I Wu68f319d2014-09-09 09:43:21 +0800106 reloc->which = which;
Chia-I Wu72292b72014-09-09 10:48:33 +0800107 reloc->offset = offset;
Chia-I Wu9ee38722014-08-25 12:11:36 +0800108 reloc->bo = bo;
Chia-I Wu72292b72014-09-09 10:48:33 +0800109 reloc->bo_offset = bo_offset;
Chia-I Wu32a22462014-08-26 14:13:46 +0800110 reloc->flags = flags;
Chia-I Wucdff0592014-08-22 09:27:36 +0800111
112 cmd->reloc_used++;
113}
114
115/**
Chia-I Wu72292b72014-09-09 10:48:33 +0800116 * Reserve a region from the state buffer. Both the offset, in bytes, and the
117 * pointer to the reserved region are returned.
118 *
119 * Note that \p alignment is in bytes and \p len is in DWords.
Chia-I Wue24c3292014-08-21 14:05:23 +0800120 */
Chia-I Wu72292b72014-09-09 10:48:33 +0800121static inline uint32_t cmd_state_pointer(struct intel_cmd *cmd,
122 XGL_SIZE alignment, XGL_UINT len,
123 uint32_t **dw)
Chia-I Wue24c3292014-08-21 14:05:23 +0800124{
Chia-I Wu72292b72014-09-09 10:48:33 +0800125 const enum intel_cmd_writer_type which = INTEL_CMD_WRITER_STATE;
126 const XGL_SIZE size = len << 2;
127 const XGL_SIZE offset = cmd_writer_reserve(cmd, which, alignment, size);
128 struct intel_cmd_writer *writer = &cmd->writers[which];
Chia-I Wue24c3292014-08-21 14:05:23 +0800129
Chia-I Wu72292b72014-09-09 10:48:33 +0800130 /* all states are at least aligned to 32-bytes */
131 assert(alignment % 32 == 0);
132
133 *dw = (uint32_t *) ((char *) writer->ptr + offset);
134
135 writer->used = offset + size;
136
137 return offset;
Chia-I Wue24c3292014-08-21 14:05:23 +0800138}
139
140/**
Chia-I Wu72292b72014-09-09 10:48:33 +0800141 * Write a dynamic state to the state buffer.
Chia-I Wucdff0592014-08-22 09:27:36 +0800142 */
Chia-I Wu72292b72014-09-09 10:48:33 +0800143static inline uint32_t cmd_state_write(struct intel_cmd *cmd,
144 XGL_SIZE alignment, XGL_UINT len,
145 const uint32_t *dw)
Chia-I Wucdff0592014-08-22 09:27:36 +0800146{
Chia-I Wu72292b72014-09-09 10:48:33 +0800147 uint32_t offset, *dst;
148
149 offset = cmd_state_pointer(cmd, alignment, len, &dst);
150 memcpy(dst, dw, len << 2);
151
152 return offset;
Chia-I Wucdff0592014-08-22 09:27:36 +0800153}
154
155/**
Chia-I Wu72292b72014-09-09 10:48:33 +0800156 * Write a surface state to the surface buffer. The offset, in bytes, of the
157 * state is returned.
158 *
159 * Note that \p alignment is in bytes and \p len is in DWords.
Chia-I Wue24c3292014-08-21 14:05:23 +0800160 */
Chia-I Wu72292b72014-09-09 10:48:33 +0800161static inline uint32_t cmd_surface_write(struct intel_cmd *cmd,
162 XGL_SIZE alignment, XGL_UINT len,
163 const uint32_t *dw)
Chia-I Wue24c3292014-08-21 14:05:23 +0800164{
Chia-I Wu72292b72014-09-09 10:48:33 +0800165 return cmd_state_write(cmd, alignment, len, dw);
Chia-I Wue24c3292014-08-21 14:05:23 +0800166}
167
168/**
Chia-I Wu72292b72014-09-09 10:48:33 +0800169 * Add a relocation entry for a DWord of a surface state.
Chia-I Wucdff0592014-08-22 09:27:36 +0800170 */
Chia-I Wu72292b72014-09-09 10:48:33 +0800171static inline void cmd_surface_reloc(struct intel_cmd *cmd,
172 uint32_t offset, XGL_UINT dw_index,
173 struct intel_bo *bo,
174 uint32_t bo_offset, uint32_t reloc_flags)
Chia-I Wucdff0592014-08-22 09:27:36 +0800175{
Chia-I Wu72292b72014-09-09 10:48:33 +0800176 const enum intel_cmd_writer_type which = INTEL_CMD_WRITER_STATE;
Chia-I Wucdff0592014-08-22 09:27:36 +0800177
Chia-I Wu72292b72014-09-09 10:48:33 +0800178 cmd_writer_reloc(cmd, which, offset + (dw_index << 2),
179 bo, bo_offset, reloc_flags);
Chia-I Wucdff0592014-08-22 09:27:36 +0800180}
181
182/**
Chia-I Wu72292b72014-09-09 10:48:33 +0800183 * Write a kernel to the instruction buffer. The offset, in bytes, of the
184 * kernel is returned.
Chia-I Wue24c3292014-08-21 14:05:23 +0800185 */
Chia-I Wu72292b72014-09-09 10:48:33 +0800186static inline uint32_t cmd_instruction_write(struct intel_cmd *cmd,
187 XGL_SIZE size,
188 const void *kernel)
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_INSTRUCTION;
191 /*
192 * From the Sandy Bridge PRM, volume 4 part 2, page 112:
193 *
194 * "Due to prefetch of the instruction stream, the EUs may attempt to
195 * access up to 8 instructions (128 bytes) beyond the end of the
196 * kernel program - possibly into the next memory page. Although
197 * these instructions will not be executed, software must account for
198 * the prefetch in order to avoid invalid page access faults."
199 */
200 const XGL_SIZE reserved_size = size + 128;
201 /* kernels are aligned to 64 bytes */
202 const XGL_SIZE alignment = 64;
203 const XGL_SIZE offset = cmd_writer_reserve(cmd,
204 which, alignment, reserved_size);
205 struct intel_cmd_writer *writer = &cmd->writers[which];
Chia-I Wue24c3292014-08-21 14:05:23 +0800206
Chia-I Wu72292b72014-09-09 10:48:33 +0800207 memcpy((char *) writer->ptr + offset, kernel, size);
Chia-I Wu5e25c272014-08-21 20:19:12 +0800208
Chia-I Wu72292b72014-09-09 10:48:33 +0800209 writer->used = offset + size;
210
211 return offset;
212}
213
214/**
215 * Reserve a region from the batch buffer. Both the offset, in DWords, and
216 * the pointer to the reserved region are returned.
217 *
218 * Note that \p len is in DWords.
219 */
220static inline XGL_UINT cmd_batch_pointer(struct intel_cmd *cmd,
221 XGL_UINT len, uint32_t **dw)
222{
223 const enum intel_cmd_writer_type which = INTEL_CMD_WRITER_BATCH;
224 /*
225 * We know the batch bo is always aligned. Using 1 here should allow the
226 * compiler to optimize away aligning.
227 */
228 const XGL_SIZE alignment = 1;
229 const XGL_SIZE size = len << 2;
230 const XGL_SIZE offset = cmd_writer_reserve(cmd, which, alignment, size);
231 struct intel_cmd_writer *writer = &cmd->writers[which];
232
233 assert(offset % 4 == 0);
234 *dw = (uint32_t *) ((char *) writer->ptr + offset);
235
236 writer->used = offset + size;
237
238 return offset >> 2;
239}
240
241/**
242 * Write a command to the batch buffer.
243 */
244static inline XGL_UINT cmd_batch_write(struct intel_cmd *cmd,
245 XGL_UINT len, const uint32_t *dw)
246{
247 XGL_UINT pos;
248 uint32_t *dst;
249
250 pos = cmd_batch_pointer(cmd, len, &dst);
251 memcpy(dst, dw, len << 2);
252
253 return pos;
254}
255
256/**
257 * Add a relocation entry for a DWord of a command.
258 */
259static inline void cmd_batch_reloc(struct intel_cmd *cmd, XGL_UINT pos,
260 struct intel_bo *bo,
261 uint32_t bo_offset, uint32_t reloc_flags)
262{
263 const enum intel_cmd_writer_type which = INTEL_CMD_WRITER_BATCH;
264
265 cmd_writer_reloc(cmd, which, pos << 2, bo, bo_offset, reloc_flags);
Chia-I Wue24c3292014-08-21 14:05:23 +0800266}
267
268/**
Chia-I Wu48c283d2014-08-25 23:13:46 +0800269 * Begin the batch buffer.
Chia-I Wu79dfbb32014-08-25 12:19:02 +0800270 */
271static inline void cmd_batch_begin(struct intel_cmd *cmd)
272{
Chia-I Wu48c283d2014-08-25 23:13:46 +0800273 /* STATE_BASE_ADDRESS */
Chia-I Wu79dfbb32014-08-25 12:19:02 +0800274 const uint8_t cmd_len = 10;
Chia-I Wu426072d2014-08-26 14:31:55 +0800275 const uint32_t dw0 = GEN6_RENDER_CMD(COMMON, STATE_BASE_ADDRESS) |
Chia-I Wu79dfbb32014-08-25 12:19:02 +0800276 (cmd_len - 2);
Chia-I Wu72292b72014-09-09 10:48:33 +0800277 XGL_UINT pos;
278 uint32_t *dw;
Chia-I Wu79dfbb32014-08-25 12:19:02 +0800279
280 CMD_ASSERT(cmd, 6, 7.5);
281
Chia-I Wu72292b72014-09-09 10:48:33 +0800282 pos = cmd_batch_pointer(cmd, cmd_len, &dw);
Chia-I Wu79dfbb32014-08-25 12:19:02 +0800283
284 /* relocs are not added until cmd_batch_end() */
Chia-I Wu72292b72014-09-09 10:48:33 +0800285 assert(!pos);
Chia-I Wu79dfbb32014-08-25 12:19:02 +0800286
Chia-I Wu72292b72014-09-09 10:48:33 +0800287 dw[0] = dw0;
Chia-I Wu79dfbb32014-08-25 12:19:02 +0800288 /* start offsets */
Chia-I Wu72292b72014-09-09 10:48:33 +0800289 dw[1] = 1;
290 dw[2] = 1;
291 dw[3] = 1;
292 dw[4] = 1;
293 dw[5] = 1;
Chia-I Wu79dfbb32014-08-25 12:19:02 +0800294 /* end offsets */
Chia-I Wu72292b72014-09-09 10:48:33 +0800295 dw[6] = 1;
296 dw[7] = 1 + 0xfffff000;
297 dw[8] = 1 + 0xfffff000;
298 dw[9] = 1;
Chia-I Wu79dfbb32014-08-25 12:19:02 +0800299}
300
301/**
Chia-I Wue24c3292014-08-21 14:05:23 +0800302 * End the batch buffer.
303 */
304static inline void cmd_batch_end(struct intel_cmd *cmd)
305{
Chia-I Wu68f319d2014-09-09 09:43:21 +0800306 struct intel_cmd_writer *writer = &cmd->writers[INTEL_CMD_WRITER_BATCH];
307 const struct intel_cmd_writer *state =
308 &cmd->writers[INTEL_CMD_WRITER_STATE];
309 const struct intel_cmd_writer *inst =
310 &cmd->writers[INTEL_CMD_WRITER_INSTRUCTION];
Chia-I Wu72292b72014-09-09 10:48:33 +0800311 uint32_t *dw;
Chia-I Wu79dfbb32014-08-25 12:19:02 +0800312
313 cmd_reserve_reloc(cmd, 5);
Chia-I Wu72292b72014-09-09 10:48:33 +0800314 cmd_batch_reloc(cmd, 2, state->bo, 1, 0);
315 cmd_batch_reloc(cmd, 3, state->bo, 1, 0);
316 cmd_batch_reloc(cmd, 5, inst->bo, 1, 0);
317 cmd_batch_reloc(cmd, 7, state->bo, 1 + (state->size << 2), 0);
318 cmd_batch_reloc(cmd, 9, inst->bo, 1 + (inst->size << 2), 0);
Chia-I Wu79dfbb32014-08-25 12:19:02 +0800319
Chia-I Wu72292b72014-09-09 10:48:33 +0800320 if (writer->used & 0x7) {
321 cmd_batch_pointer(cmd, 1, &dw);
322 dw[0] = GEN6_MI_CMD(MI_BATCH_BUFFER_END);
Chia-I Wue24c3292014-08-21 14:05:23 +0800323 } else {
Chia-I Wu72292b72014-09-09 10:48:33 +0800324 cmd_batch_pointer(cmd, 2, &dw);
325 dw[0] = GEN6_MI_CMD(MI_BATCH_BUFFER_END);
326 dw[1] = GEN6_MI_CMD(MI_NOOP);
Chia-I Wue24c3292014-08-21 14:05:23 +0800327 }
Chia-I Wu343b1372014-08-20 16:39:20 +0800328}
329
Chia-I Wu525c6602014-08-27 10:22:34 +0800330void cmd_batch_flush(struct intel_cmd *cmd, uint32_t pipe_control_dw0);
331
Chia-I Wu759fa2e2014-08-30 18:44:47 +0800332void cmd_batch_depth_count(struct intel_cmd *cmd,
333 struct intel_bo *bo,
334 XGL_GPU_SIZE offset);
335
Chia-I Wue8dbd5d2014-08-31 13:15:58 +0800336void cmd_batch_timestamp(struct intel_cmd *cmd,
337 struct intel_bo *bo,
338 XGL_GPU_SIZE offset);
339
340void cmd_batch_immediate(struct intel_cmd *cmd,
341 struct intel_bo *bo,
342 XGL_GPU_SIZE offset,
343 uint64_t val);
Chia-I Wu1cbc0052014-08-25 09:50:12 +0800344
Chia-I Wu00a23b22014-08-20 15:28:08 +0800345#endif /* CMD_PRIV_H */