blob: 58330f3ff0031aad7968877d961277002d5d9010 [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 Wu958d1b72014-08-21 11:28:11 +080041 XGL_UINT pos;
42
43 uint32_t val;
Chia-I Wu9ee38722014-08-25 12:11:36 +080044 struct intel_bo *bo;
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 Wu68f319d2014-09-09 09:43:21 +080066 enum intel_cmd_writer_type which);
Chia-I Wu00a23b22014-08-20 15:28:08 +080067
Chia-I Wu32710d72014-08-20 16:05:22 +080068/**
Chia-I Wubda55fd2014-08-25 12:46:10 +080069 * Add a reloc at \p pos. No error checking.
Chia-I Wucdff0592014-08-22 09:27:36 +080070 */
71static inline void cmd_writer_add_reloc(struct intel_cmd *cmd,
Chia-I Wu68f319d2014-09-09 09:43:21 +080072 enum intel_cmd_writer_type which,
Chia-I Wubda55fd2014-08-25 12:46:10 +080073 XGL_UINT pos, uint32_t val,
Chia-I Wu9ee38722014-08-25 12:11:36 +080074 struct intel_bo *bo,
Chia-I Wu32a22462014-08-26 14:13:46 +080075 uint32_t flags)
Chia-I Wucdff0592014-08-22 09:27:36 +080076{
77 struct intel_cmd_reloc *reloc = &cmd->relocs[cmd->reloc_used];
78
79 assert(cmd->reloc_used < cmd->reloc_count);
80
Chia-I Wu68f319d2014-09-09 09:43:21 +080081 reloc->which = which;
Chia-I Wubda55fd2014-08-25 12:46:10 +080082 reloc->pos = pos;
Chia-I Wucdff0592014-08-22 09:27:36 +080083 reloc->val = val;
Chia-I Wu9ee38722014-08-25 12:11:36 +080084 reloc->bo = bo;
Chia-I Wu32a22462014-08-26 14:13:46 +080085 reloc->flags = flags;
Chia-I Wucdff0592014-08-22 09:27:36 +080086
87 cmd->reloc_used++;
88}
89
90/**
91 * Reserve \p len DWords in the batch buffer for building a hardware command.
Chia-I Wue24c3292014-08-21 14:05:23 +080092 */
93static inline void cmd_batch_reserve(struct intel_cmd *cmd, XGL_UINT len)
94{
Chia-I Wu68f319d2014-09-09 09:43:21 +080095 struct intel_cmd_writer *writer = &cmd->writers[INTEL_CMD_WRITER_BATCH];
Chia-I Wue24c3292014-08-21 14:05:23 +080096
97 if (writer->used + len > writer->size)
Chia-I Wu68f319d2014-09-09 09:43:21 +080098 cmd_writer_grow(cmd, INTEL_CMD_WRITER_BATCH);
Chia-I Wue24c3292014-08-21 14:05:23 +080099 assert(writer->used + len <= writer->size);
100}
101
102/**
Chia-I Wucdff0592014-08-22 09:27:36 +0800103 * Reserve \p len DWords in the batch buffer and \p reloc_len relocs for
104 * building a hardware command.
105 */
106static inline void cmd_batch_reserve_reloc(struct intel_cmd *cmd,
107 XGL_UINT len, XGL_UINT reloc_len)
108{
109 cmd_reserve_reloc(cmd, reloc_len);
110 cmd_batch_reserve(cmd, len);
111}
112
113/**
114 * Add a DWord to the hardware command being built. No error checking.
Chia-I Wue24c3292014-08-21 14:05:23 +0800115 */
116static inline void cmd_batch_write(struct intel_cmd *cmd, uint32_t val)
117{
Chia-I Wu68f319d2014-09-09 09:43:21 +0800118 struct intel_cmd_writer *writer = &cmd->writers[INTEL_CMD_WRITER_BATCH];
Chia-I Wue24c3292014-08-21 14:05:23 +0800119
120 assert(writer->used < writer->size);
121 ((uint32_t *) writer->ptr_opaque)[writer->used++] = val;
122}
123
124/**
Chia-I Wucdff0592014-08-22 09:27:36 +0800125 * Add \p len DWords to the hardware command being built. No error checking.
126 */
127static inline void cmd_batch_write_n(struct intel_cmd *cmd,
128 const uint32_t *vals, XGL_UINT len)
129{
Chia-I Wu68f319d2014-09-09 09:43:21 +0800130 struct intel_cmd_writer *writer = &cmd->writers[INTEL_CMD_WRITER_BATCH];
Chia-I Wucdff0592014-08-22 09:27:36 +0800131
132 assert(writer->used + len <= writer->size);
133
134 memcpy((uint32_t *) writer->ptr_opaque + writer->used,
135 vals, sizeof(uint32_t) * len);
136 writer->used += len;
137}
138
139/**
140 * Add a reloc to the hardware command being built. No error checking.
Chia-I Wue24c3292014-08-21 14:05:23 +0800141 */
142static inline void cmd_batch_reloc(struct intel_cmd *cmd,
Chia-I Wu9ee38722014-08-25 12:11:36 +0800143 uint32_t val, struct intel_bo *bo,
Chia-I Wu32a22462014-08-26 14:13:46 +0800144 uint32_t flags)
Chia-I Wue24c3292014-08-21 14:05:23 +0800145{
Chia-I Wu68f319d2014-09-09 09:43:21 +0800146 struct intel_cmd_writer *writer = &cmd->writers[INTEL_CMD_WRITER_BATCH];
Chia-I Wue24c3292014-08-21 14:05:23 +0800147
Chia-I Wu68f319d2014-09-09 09:43:21 +0800148 cmd_writer_add_reloc(cmd, INTEL_CMD_WRITER_BATCH,
149 writer->used, val, bo, flags);
Chia-I Wu5e25c272014-08-21 20:19:12 +0800150
Chia-I Wue24c3292014-08-21 14:05:23 +0800151 writer->used++;
152}
153
154/**
Chia-I Wu48c283d2014-08-25 23:13:46 +0800155 * Begin the batch buffer.
Chia-I Wu79dfbb32014-08-25 12:19:02 +0800156 */
157static inline void cmd_batch_begin(struct intel_cmd *cmd)
158{
Chia-I Wu48c283d2014-08-25 23:13:46 +0800159 /* STATE_BASE_ADDRESS */
Chia-I Wu79dfbb32014-08-25 12:19:02 +0800160 const uint8_t cmd_len = 10;
Chia-I Wu426072d2014-08-26 14:31:55 +0800161 const uint32_t dw0 = GEN6_RENDER_CMD(COMMON, STATE_BASE_ADDRESS) |
Chia-I Wu79dfbb32014-08-25 12:19:02 +0800162 (cmd_len - 2);
163
164 CMD_ASSERT(cmd, 6, 7.5);
165
166 cmd_batch_reserve(cmd, cmd_len);
167
168 /* relocs are not added until cmd_batch_end() */
Chia-I Wu68f319d2014-09-09 09:43:21 +0800169 assert(cmd->writers[INTEL_CMD_WRITER_BATCH].used == 0);
Chia-I Wu79dfbb32014-08-25 12:19:02 +0800170
171 cmd_batch_write(cmd, dw0);
172
173 /* start offsets */
174 cmd_batch_write(cmd, 1);
175 cmd_batch_write(cmd, 1);
176 cmd_batch_write(cmd, 1);
177 cmd_batch_write(cmd, 1);
178 cmd_batch_write(cmd, 1);
179 /* end offsets */
180 cmd_batch_write(cmd, 1);
181 cmd_batch_write(cmd, 1 + 0xfffff000);
182 cmd_batch_write(cmd, 1 + 0xfffff000);
183 cmd_batch_write(cmd, 1);
184}
185
186/**
Chia-I Wue24c3292014-08-21 14:05:23 +0800187 * End the batch buffer.
188 */
189static inline void cmd_batch_end(struct intel_cmd *cmd)
190{
Chia-I Wu68f319d2014-09-09 09:43:21 +0800191 struct intel_cmd_writer *writer = &cmd->writers[INTEL_CMD_WRITER_BATCH];
192 const struct intel_cmd_writer *state =
193 &cmd->writers[INTEL_CMD_WRITER_STATE];
194 const struct intel_cmd_writer *inst =
195 &cmd->writers[INTEL_CMD_WRITER_INSTRUCTION];
Chia-I Wu79dfbb32014-08-25 12:19:02 +0800196
197 cmd_reserve_reloc(cmd, 5);
Chia-I Wu68f319d2014-09-09 09:43:21 +0800198 cmd_writer_add_reloc(cmd, INTEL_CMD_WRITER_BATCH,
199 2, 1, state->bo, 0);
200 cmd_writer_add_reloc(cmd, INTEL_CMD_WRITER_BATCH,
201 3, 1, state->bo, 0);
202 cmd_writer_add_reloc(cmd, INTEL_CMD_WRITER_BATCH,
203 5, 1, inst->bo, 0);
204 cmd_writer_add_reloc(cmd, INTEL_CMD_WRITER_BATCH,
205 7, 1 + (state->size << 2), state->bo, 0);
206 cmd_writer_add_reloc(cmd, INTEL_CMD_WRITER_BATCH,
207 9, 1 + (inst->size << 2), inst->bo, 0);
Chia-I Wu79dfbb32014-08-25 12:19:02 +0800208
Chia-I Wu68f319d2014-09-09 09:43:21 +0800209 if (writer->used & 1) {
Chia-I Wue24c3292014-08-21 14:05:23 +0800210 cmd_batch_reserve(cmd, 1);
Chia-I Wu426072d2014-08-26 14:31:55 +0800211 cmd_batch_write(cmd, GEN6_MI_CMD(MI_BATCH_BUFFER_END));
Chia-I Wue24c3292014-08-21 14:05:23 +0800212 } else {
213 cmd_batch_reserve(cmd, 2);
Chia-I Wu426072d2014-08-26 14:31:55 +0800214 cmd_batch_write(cmd, GEN6_MI_CMD(MI_BATCH_BUFFER_END));
215 cmd_batch_write(cmd, GEN6_MI_CMD(MI_NOOP));
Chia-I Wue24c3292014-08-21 14:05:23 +0800216 }
Chia-I Wu343b1372014-08-20 16:39:20 +0800217}
218
Chia-I Wu525c6602014-08-27 10:22:34 +0800219void cmd_batch_flush(struct intel_cmd *cmd, uint32_t pipe_control_dw0);
220
Chia-I Wu759fa2e2014-08-30 18:44:47 +0800221void cmd_batch_depth_count(struct intel_cmd *cmd,
222 struct intel_bo *bo,
223 XGL_GPU_SIZE offset);
224
Chia-I Wue8dbd5d2014-08-31 13:15:58 +0800225void cmd_batch_timestamp(struct intel_cmd *cmd,
226 struct intel_bo *bo,
227 XGL_GPU_SIZE offset);
228
229void cmd_batch_immediate(struct intel_cmd *cmd,
230 struct intel_bo *bo,
231 XGL_GPU_SIZE offset,
232 uint64_t val);
Chia-I Wu24565ee2014-08-21 20:24:31 +0800233/**
Chia-I Wucdff0592014-08-22 09:27:36 +0800234 * Reserve \p len DWords in the state buffer for building a hardware state.
235 * The current writer position is aligned to \p alignment first. Both the
236 * pointer to the reserved region and the aligned position are returned.
237 *
238 * Note that the returned pointer is only valid until the next reserve call.
Chia-I Wu24565ee2014-08-21 20:24:31 +0800239 */
240static inline uint32_t *cmd_state_reserve(struct intel_cmd *cmd, XGL_UINT len,
241 XGL_UINT alignment, XGL_UINT *pos)
242{
Chia-I Wu68f319d2014-09-09 09:43:21 +0800243 struct intel_cmd_writer *writer = &cmd->writers[INTEL_CMD_WRITER_STATE];
Chia-I Wu24565ee2014-08-21 20:24:31 +0800244 XGL_UINT aligned;
245
246 assert(alignment && u_is_pow2(alignment));
247 aligned = u_align(writer->used, alignment);
248
249 if (aligned + len > writer->size)
Chia-I Wu68f319d2014-09-09 09:43:21 +0800250 cmd_writer_grow(cmd, INTEL_CMD_WRITER_STATE);
Chia-I Wu24565ee2014-08-21 20:24:31 +0800251 assert(aligned + len <= writer->size);
252
253 writer->used = aligned;
254 *pos = aligned;
255
256 return &((uint32_t *) writer->ptr_opaque)[writer->used];
257}
258
259/**
Chia-I Wucdff0592014-08-22 09:27:36 +0800260 * Similar to \p cmd_state_reserve, except that \p reloc_len relocs are also
261 * reserved.
Chia-I Wu24565ee2014-08-21 20:24:31 +0800262 */
Chia-I Wucdff0592014-08-22 09:27:36 +0800263static inline uint32_t *cmd_state_reserve_reloc(struct intel_cmd *cmd,
264 XGL_UINT len,
265 XGL_UINT reloc_len,
266 XGL_UINT alignment,
267 XGL_UINT *pos)
Chia-I Wu24565ee2014-08-21 20:24:31 +0800268{
Chia-I Wucdff0592014-08-22 09:27:36 +0800269 cmd_reserve_reloc(cmd, reloc_len);
270 return cmd_state_reserve(cmd, len, alignment, pos);
Chia-I Wu24565ee2014-08-21 20:24:31 +0800271}
272
273/**
Chia-I Wubda55fd2014-08-25 12:46:10 +0800274 * Add a reloc at \p offset, relative to the current writer position. No
275 * error checking.
276 */
277static inline void cmd_state_reloc(struct intel_cmd *cmd,
278 XGL_INT offset, uint32_t val,
279 struct intel_bo *bo,
Chia-I Wu32a22462014-08-26 14:13:46 +0800280 uint32_t flags)
Chia-I Wubda55fd2014-08-25 12:46:10 +0800281{
Chia-I Wu68f319d2014-09-09 09:43:21 +0800282 struct intel_cmd_writer *writer = &cmd->writers[INTEL_CMD_WRITER_STATE];
Chia-I Wubda55fd2014-08-25 12:46:10 +0800283
Chia-I Wu68f319d2014-09-09 09:43:21 +0800284 cmd_writer_add_reloc(cmd, INTEL_CMD_WRITER_STATE,
285 writer->used + offset, val, bo, flags);
Chia-I Wubda55fd2014-08-25 12:46:10 +0800286}
287
288/**
Chia-I Wucdff0592014-08-22 09:27:36 +0800289 * Advance the writer position of the state buffer. No error checking.
Chia-I Wu24565ee2014-08-21 20:24:31 +0800290 */
291static inline void cmd_state_advance(struct intel_cmd *cmd, XGL_UINT len)
292{
Chia-I Wu68f319d2014-09-09 09:43:21 +0800293 struct intel_cmd_writer *writer = &cmd->writers[INTEL_CMD_WRITER_STATE];
Chia-I Wu24565ee2014-08-21 20:24:31 +0800294
295 assert(writer->used + len <= writer->size);
296 writer->used += len;
297}
298
299/**
Chia-I Wucdff0592014-08-22 09:27:36 +0800300 * A convenient function to copy a hardware state of \p len DWords into the
301 * state buffer. The position of the state is returned.
Chia-I Wu24565ee2014-08-21 20:24:31 +0800302 */
303static inline XGL_UINT cmd_state_copy(struct intel_cmd *cmd,
304 const uint32_t *vals, XGL_UINT len,
305 XGL_UINT alignment)
306{
307 uint32_t *dst;
308 XGL_UINT pos;
309
310 dst = cmd_state_reserve(cmd, len, alignment, &pos);
311 memcpy(dst, vals, sizeof(uint32_t) * len);
312 cmd_state_advance(cmd, len);
313
314 return pos;
315}
316
Chia-I Wu1cbc0052014-08-25 09:50:12 +0800317static inline XGL_UINT cmd_kernel_copy(struct intel_cmd *cmd,
318 const void *kernel, XGL_SIZE size)
319{
320 /*
321 * From the Sandy Bridge PRM, volume 4 part 2, page 112:
322 *
323 * "Due to prefetch of the instruction stream, the EUs may attempt to
324 * access up to 8 instructions (128 bytes) beyond the end of the
325 * kernel program - possibly into the next memory page. Although
326 * these instructions will not be executed, software must account for
327 * the prefetch in order to avoid invalid page access faults."
328 */
329 const XGL_UINT prefetch_len = 128 / sizeof(uint32_t);
330 /* kernels are aligned to 64-byte */
331 const XGL_UINT kernel_align = 64 / sizeof(uint32_t);
332 const XGL_UINT kernel_len = ((size + 3) & ~3) / sizeof(uint32_t);
Chia-I Wu68f319d2014-09-09 09:43:21 +0800333 struct intel_cmd_writer *writer =
334 &cmd->writers[INTEL_CMD_WRITER_INSTRUCTION];
Chia-I Wu1cbc0052014-08-25 09:50:12 +0800335 XGL_UINT kernel_pos;
336
337 kernel_pos = u_align(writer->used, kernel_align);
338 if (kernel_pos + kernel_len + prefetch_len > writer->size)
Chia-I Wu68f319d2014-09-09 09:43:21 +0800339 cmd_writer_grow(cmd, INTEL_CMD_WRITER_INSTRUCTION);
Chia-I Wu1cbc0052014-08-25 09:50:12 +0800340 assert(kernel_pos + kernel_len + prefetch_len <= writer->size);
341
342 memcpy(&((uint32_t *) writer->ptr_opaque)[kernel_pos], kernel, size);
343 writer->used = kernel_pos + kernel_len;
344
345 return kernel_pos;
346}
347
Chia-I Wu00a23b22014-08-20 15:28:08 +0800348#endif /* CMD_PRIV_H */