blob: 22face34d0c5b3e2c292442752e28dc7bdbef975 [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 Wue24c3292014-08-21 14:05:23 +080040 struct intel_cmd_writer *writer;
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,
66 struct intel_cmd_writer *writer);
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,
72 struct intel_cmd_writer *writer,
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
81 reloc->writer = writer;
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{
95 struct intel_cmd_writer *writer = &cmd->batch;
96
97 if (writer->used + len > writer->size)
98 cmd_writer_grow(cmd, writer);
99 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{
118 struct intel_cmd_writer *writer = &cmd->batch;
119
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{
130 struct intel_cmd_writer *writer = &cmd->batch;
131
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{
146 struct intel_cmd_writer *writer = &cmd->batch;
147
Chia-I Wu32a22462014-08-26 14:13:46 +0800148 cmd_writer_add_reloc(cmd, writer, writer->used, val, bo, flags);
Chia-I Wu5e25c272014-08-21 20:19:12 +0800149
Chia-I Wue24c3292014-08-21 14:05:23 +0800150 writer->used++;
151}
152
153/**
Chia-I Wu48c283d2014-08-25 23:13:46 +0800154 * Begin the batch buffer.
Chia-I Wu79dfbb32014-08-25 12:19:02 +0800155 */
156static inline void cmd_batch_begin(struct intel_cmd *cmd)
157{
Chia-I Wu48c283d2014-08-25 23:13:46 +0800158 /* STATE_BASE_ADDRESS */
Chia-I Wu79dfbb32014-08-25 12:19:02 +0800159 const uint8_t cmd_len = 10;
Chia-I Wu426072d2014-08-26 14:31:55 +0800160 const uint32_t dw0 = GEN6_RENDER_CMD(COMMON, STATE_BASE_ADDRESS) |
Chia-I Wu79dfbb32014-08-25 12:19:02 +0800161 (cmd_len - 2);
162
163 CMD_ASSERT(cmd, 6, 7.5);
164
165 cmd_batch_reserve(cmd, cmd_len);
166
167 /* relocs are not added until cmd_batch_end() */
168 assert(cmd->batch.used == 0);
169
170 cmd_batch_write(cmd, dw0);
171
172 /* start offsets */
173 cmd_batch_write(cmd, 1);
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 /* end offsets */
179 cmd_batch_write(cmd, 1);
180 cmd_batch_write(cmd, 1 + 0xfffff000);
181 cmd_batch_write(cmd, 1 + 0xfffff000);
182 cmd_batch_write(cmd, 1);
183}
184
185/**
Chia-I Wue24c3292014-08-21 14:05:23 +0800186 * End the batch buffer.
187 */
188static inline void cmd_batch_end(struct intel_cmd *cmd)
189{
Chia-I Wu79dfbb32014-08-25 12:19:02 +0800190 struct intel_cmd_writer *writer = &cmd->batch;
191 const struct intel_cmd_writer *state = &cmd->state;
192 const struct intel_cmd_writer *kernel = &cmd->kernel;
193
194 cmd_reserve_reloc(cmd, 5);
Chia-I Wu32a22462014-08-26 14:13:46 +0800195 cmd_writer_add_reloc(cmd, writer, 2, 1, state->bo, 0);
196 cmd_writer_add_reloc(cmd, writer, 3, 1, state->bo, 0);
197 cmd_writer_add_reloc(cmd, writer, 5, 1, kernel->bo, 0);
198 cmd_writer_add_reloc(cmd, writer, 7, 1 +
199 (state->size << 2), state->bo, 0);
200 cmd_writer_add_reloc(cmd, writer, 9, 1 +
201 (kernel->size << 2), kernel->bo, 0);
Chia-I Wu79dfbb32014-08-25 12:19:02 +0800202
Chia-I Wue24c3292014-08-21 14:05:23 +0800203 if (cmd->batch.used & 1) {
204 cmd_batch_reserve(cmd, 1);
Chia-I Wu426072d2014-08-26 14:31:55 +0800205 cmd_batch_write(cmd, GEN6_MI_CMD(MI_BATCH_BUFFER_END));
Chia-I Wue24c3292014-08-21 14:05:23 +0800206 } else {
207 cmd_batch_reserve(cmd, 2);
Chia-I Wu426072d2014-08-26 14:31:55 +0800208 cmd_batch_write(cmd, GEN6_MI_CMD(MI_BATCH_BUFFER_END));
209 cmd_batch_write(cmd, GEN6_MI_CMD(MI_NOOP));
Chia-I Wue24c3292014-08-21 14:05:23 +0800210 }
Chia-I Wu343b1372014-08-20 16:39:20 +0800211}
212
Chia-I Wu525c6602014-08-27 10:22:34 +0800213void cmd_batch_flush(struct intel_cmd *cmd, uint32_t pipe_control_dw0);
214
Chia-I Wu759fa2e2014-08-30 18:44:47 +0800215void cmd_batch_depth_count(struct intel_cmd *cmd,
216 struct intel_bo *bo,
217 XGL_GPU_SIZE offset);
218
Chia-I Wue8dbd5d2014-08-31 13:15:58 +0800219void cmd_batch_timestamp(struct intel_cmd *cmd,
220 struct intel_bo *bo,
221 XGL_GPU_SIZE offset);
222
223void cmd_batch_immediate(struct intel_cmd *cmd,
224 struct intel_bo *bo,
225 XGL_GPU_SIZE offset,
226 uint64_t val);
Chia-I Wu24565ee2014-08-21 20:24:31 +0800227/**
Chia-I Wucdff0592014-08-22 09:27:36 +0800228 * Reserve \p len DWords in the state buffer for building a hardware state.
229 * The current writer position is aligned to \p alignment first. Both the
230 * pointer to the reserved region and the aligned position are returned.
231 *
232 * Note that the returned pointer is only valid until the next reserve call.
Chia-I Wu24565ee2014-08-21 20:24:31 +0800233 */
234static inline uint32_t *cmd_state_reserve(struct intel_cmd *cmd, XGL_UINT len,
235 XGL_UINT alignment, XGL_UINT *pos)
236{
237 struct intel_cmd_writer *writer = &cmd->state;
238 XGL_UINT aligned;
239
240 assert(alignment && u_is_pow2(alignment));
241 aligned = u_align(writer->used, alignment);
242
243 if (aligned + len > writer->size)
244 cmd_writer_grow(cmd, writer);
245 assert(aligned + len <= writer->size);
246
247 writer->used = aligned;
248 *pos = aligned;
249
250 return &((uint32_t *) writer->ptr_opaque)[writer->used];
251}
252
253/**
Chia-I Wucdff0592014-08-22 09:27:36 +0800254 * Similar to \p cmd_state_reserve, except that \p reloc_len relocs are also
255 * reserved.
Chia-I Wu24565ee2014-08-21 20:24:31 +0800256 */
Chia-I Wucdff0592014-08-22 09:27:36 +0800257static inline uint32_t *cmd_state_reserve_reloc(struct intel_cmd *cmd,
258 XGL_UINT len,
259 XGL_UINT reloc_len,
260 XGL_UINT alignment,
261 XGL_UINT *pos)
Chia-I Wu24565ee2014-08-21 20:24:31 +0800262{
Chia-I Wucdff0592014-08-22 09:27:36 +0800263 cmd_reserve_reloc(cmd, reloc_len);
264 return cmd_state_reserve(cmd, len, alignment, pos);
Chia-I Wu24565ee2014-08-21 20:24:31 +0800265}
266
267/**
Chia-I Wubda55fd2014-08-25 12:46:10 +0800268 * Add a reloc at \p offset, relative to the current writer position. No
269 * error checking.
270 */
271static inline void cmd_state_reloc(struct intel_cmd *cmd,
272 XGL_INT offset, uint32_t val,
273 struct intel_bo *bo,
Chia-I Wu32a22462014-08-26 14:13:46 +0800274 uint32_t flags)
Chia-I Wubda55fd2014-08-25 12:46:10 +0800275{
276 struct intel_cmd_writer *writer = &cmd->state;
277
Chia-I Wu32a22462014-08-26 14:13:46 +0800278 cmd_writer_add_reloc(cmd, writer, writer->used + offset, val, bo, flags);
Chia-I Wubda55fd2014-08-25 12:46:10 +0800279}
280
281/**
Chia-I Wucdff0592014-08-22 09:27:36 +0800282 * Advance the writer position of the state buffer. No error checking.
Chia-I Wu24565ee2014-08-21 20:24:31 +0800283 */
284static inline void cmd_state_advance(struct intel_cmd *cmd, XGL_UINT len)
285{
286 struct intel_cmd_writer *writer = &cmd->state;
287
288 assert(writer->used + len <= writer->size);
289 writer->used += len;
290}
291
292/**
Chia-I Wucdff0592014-08-22 09:27:36 +0800293 * A convenient function to copy a hardware state of \p len DWords into the
294 * state buffer. The position of the state is returned.
Chia-I Wu24565ee2014-08-21 20:24:31 +0800295 */
296static inline XGL_UINT cmd_state_copy(struct intel_cmd *cmd,
297 const uint32_t *vals, XGL_UINT len,
298 XGL_UINT alignment)
299{
300 uint32_t *dst;
301 XGL_UINT pos;
302
303 dst = cmd_state_reserve(cmd, len, alignment, &pos);
304 memcpy(dst, vals, sizeof(uint32_t) * len);
305 cmd_state_advance(cmd, len);
306
307 return pos;
308}
309
Chia-I Wu1cbc0052014-08-25 09:50:12 +0800310static inline XGL_UINT cmd_kernel_copy(struct intel_cmd *cmd,
311 const void *kernel, XGL_SIZE size)
312{
313 /*
314 * From the Sandy Bridge PRM, volume 4 part 2, page 112:
315 *
316 * "Due to prefetch of the instruction stream, the EUs may attempt to
317 * access up to 8 instructions (128 bytes) beyond the end of the
318 * kernel program - possibly into the next memory page. Although
319 * these instructions will not be executed, software must account for
320 * the prefetch in order to avoid invalid page access faults."
321 */
322 const XGL_UINT prefetch_len = 128 / sizeof(uint32_t);
323 /* kernels are aligned to 64-byte */
324 const XGL_UINT kernel_align = 64 / sizeof(uint32_t);
325 const XGL_UINT kernel_len = ((size + 3) & ~3) / sizeof(uint32_t);
326 struct intel_cmd_writer *writer = &cmd->kernel;
327 XGL_UINT kernel_pos;
328
329 kernel_pos = u_align(writer->used, kernel_align);
330 if (kernel_pos + kernel_len + prefetch_len > writer->size)
331 cmd_writer_grow(cmd, writer);
332 assert(kernel_pos + kernel_len + prefetch_len <= writer->size);
333
334 memcpy(&((uint32_t *) writer->ptr_opaque)[kernel_pos], kernel, size);
335 writer->used = kernel_pos + kernel_len;
336
337 return kernel_pos;
338}
339
Chia-I Wu00a23b22014-08-20 15:28:08 +0800340#endif /* CMD_PRIV_H */