blob: b3b73dd51bdb317aa64a037c6baa97b89c232991 [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.
23 */
24
25#ifndef CMD_PRIV_H
26#define CMD_PRIV_H
27
Chia-I Wue24c3292014-08-21 14:05:23 +080028#include "genhw/genhw.h"
Chia-I Wu32710d72014-08-20 16:05:22 +080029#include "dev.h"
30#include "gpu.h"
Chia-I Wu00a23b22014-08-20 15:28:08 +080031#include "cmd.h"
32
Chia-I Wu32710d72014-08-20 16:05:22 +080033#define CMD_ASSERT(cmd, min_gen, max_gen) \
34 INTEL_GPU_ASSERT((cmd)->dev->gpu, (min_gen), (max_gen))
35
Chia-I Wu958d1b72014-08-21 11:28:11 +080036struct intel_cmd_reloc {
Chia-I Wue24c3292014-08-21 14:05:23 +080037 struct intel_cmd_writer *writer;
Chia-I Wu958d1b72014-08-21 11:28:11 +080038 XGL_UINT pos;
39
40 uint32_t val;
Chia-I Wu9ee38722014-08-25 12:11:36 +080041 struct intel_bo *bo;
Chia-I Wu958d1b72014-08-21 11:28:11 +080042
Chia-I Wu32a22462014-08-26 14:13:46 +080043 uint32_t flags;
Chia-I Wu958d1b72014-08-21 11:28:11 +080044};
45
Chia-I Wu9f039862014-08-20 15:39:56 +080046static inline int cmd_gen(const struct intel_cmd *cmd)
47{
48 return intel_gpu_gen(cmd->dev->gpu);
49}
50
Chia-I Wucdff0592014-08-22 09:27:36 +080051static inline void cmd_reserve_reloc(struct intel_cmd *cmd,
52 XGL_UINT reloc_len)
53{
54 /* fail silently */
55 if (cmd->reloc_used + reloc_len > cmd->reloc_count) {
56 cmd->reloc_used = 0;
57 cmd->result = XGL_ERROR_TOO_MANY_MEMORY_REFERENCES;
58 }
59 assert(cmd->reloc_used + reloc_len <= cmd->reloc_count);
60}
61
Chia-I Wue24c3292014-08-21 14:05:23 +080062void cmd_writer_grow(struct intel_cmd *cmd,
63 struct intel_cmd_writer *writer);
Chia-I Wu00a23b22014-08-20 15:28:08 +080064
Chia-I Wu32710d72014-08-20 16:05:22 +080065/**
Chia-I Wubda55fd2014-08-25 12:46:10 +080066 * Add a reloc at \p pos. No error checking.
Chia-I Wucdff0592014-08-22 09:27:36 +080067 */
68static inline void cmd_writer_add_reloc(struct intel_cmd *cmd,
69 struct intel_cmd_writer *writer,
Chia-I Wubda55fd2014-08-25 12:46:10 +080070 XGL_UINT pos, uint32_t val,
Chia-I Wu9ee38722014-08-25 12:11:36 +080071 struct intel_bo *bo,
Chia-I Wu32a22462014-08-26 14:13:46 +080072 uint32_t flags)
Chia-I Wucdff0592014-08-22 09:27:36 +080073{
74 struct intel_cmd_reloc *reloc = &cmd->relocs[cmd->reloc_used];
75
76 assert(cmd->reloc_used < cmd->reloc_count);
77
78 reloc->writer = writer;
Chia-I Wubda55fd2014-08-25 12:46:10 +080079 reloc->pos = pos;
Chia-I Wucdff0592014-08-22 09:27:36 +080080 reloc->val = val;
Chia-I Wu9ee38722014-08-25 12:11:36 +080081 reloc->bo = bo;
Chia-I Wu32a22462014-08-26 14:13:46 +080082 reloc->flags = flags;
Chia-I Wucdff0592014-08-22 09:27:36 +080083
84 cmd->reloc_used++;
85}
86
87/**
88 * Reserve \p len DWords in the batch buffer for building a hardware command.
Chia-I Wue24c3292014-08-21 14:05:23 +080089 */
90static inline void cmd_batch_reserve(struct intel_cmd *cmd, XGL_UINT len)
91{
92 struct intel_cmd_writer *writer = &cmd->batch;
93
94 if (writer->used + len > writer->size)
95 cmd_writer_grow(cmd, writer);
96 assert(writer->used + len <= writer->size);
97}
98
99/**
Chia-I Wucdff0592014-08-22 09:27:36 +0800100 * Reserve \p len DWords in the batch buffer and \p reloc_len relocs for
101 * building a hardware command.
102 */
103static inline void cmd_batch_reserve_reloc(struct intel_cmd *cmd,
104 XGL_UINT len, XGL_UINT reloc_len)
105{
106 cmd_reserve_reloc(cmd, reloc_len);
107 cmd_batch_reserve(cmd, len);
108}
109
110/**
111 * Add a DWord to the hardware command being built. No error checking.
Chia-I Wue24c3292014-08-21 14:05:23 +0800112 */
113static inline void cmd_batch_write(struct intel_cmd *cmd, uint32_t val)
114{
115 struct intel_cmd_writer *writer = &cmd->batch;
116
117 assert(writer->used < writer->size);
118 ((uint32_t *) writer->ptr_opaque)[writer->used++] = val;
119}
120
121/**
Chia-I Wucdff0592014-08-22 09:27:36 +0800122 * Add \p len DWords to the hardware command being built. No error checking.
123 */
124static inline void cmd_batch_write_n(struct intel_cmd *cmd,
125 const uint32_t *vals, XGL_UINT len)
126{
127 struct intel_cmd_writer *writer = &cmd->batch;
128
129 assert(writer->used + len <= writer->size);
130
131 memcpy((uint32_t *) writer->ptr_opaque + writer->used,
132 vals, sizeof(uint32_t) * len);
133 writer->used += len;
134}
135
136/**
137 * Add a reloc to the hardware command being built. No error checking.
Chia-I Wue24c3292014-08-21 14:05:23 +0800138 */
139static inline void cmd_batch_reloc(struct intel_cmd *cmd,
Chia-I Wu9ee38722014-08-25 12:11:36 +0800140 uint32_t val, struct intel_bo *bo,
Chia-I Wu32a22462014-08-26 14:13:46 +0800141 uint32_t flags)
Chia-I Wue24c3292014-08-21 14:05:23 +0800142{
143 struct intel_cmd_writer *writer = &cmd->batch;
144
Chia-I Wu32a22462014-08-26 14:13:46 +0800145 cmd_writer_add_reloc(cmd, writer, writer->used, val, bo, flags);
Chia-I Wu5e25c272014-08-21 20:19:12 +0800146
Chia-I Wue24c3292014-08-21 14:05:23 +0800147 writer->used++;
148}
149
150/**
Chia-I Wu48c283d2014-08-25 23:13:46 +0800151 * Begin the batch buffer.
Chia-I Wu79dfbb32014-08-25 12:19:02 +0800152 */
153static inline void cmd_batch_begin(struct intel_cmd *cmd)
154{
Chia-I Wu48c283d2014-08-25 23:13:46 +0800155 /* STATE_BASE_ADDRESS */
Chia-I Wu79dfbb32014-08-25 12:19:02 +0800156 const uint8_t cmd_len = 10;
Chia-I Wu426072d2014-08-26 14:31:55 +0800157 const uint32_t dw0 = GEN6_RENDER_CMD(COMMON, STATE_BASE_ADDRESS) |
Chia-I Wu79dfbb32014-08-25 12:19:02 +0800158 (cmd_len - 2);
159
160 CMD_ASSERT(cmd, 6, 7.5);
161
162 cmd_batch_reserve(cmd, cmd_len);
163
164 /* relocs are not added until cmd_batch_end() */
165 assert(cmd->batch.used == 0);
166
167 cmd_batch_write(cmd, dw0);
168
169 /* start offsets */
170 cmd_batch_write(cmd, 1);
171 cmd_batch_write(cmd, 1);
172 cmd_batch_write(cmd, 1);
173 cmd_batch_write(cmd, 1);
174 cmd_batch_write(cmd, 1);
175 /* end offsets */
176 cmd_batch_write(cmd, 1);
177 cmd_batch_write(cmd, 1 + 0xfffff000);
178 cmd_batch_write(cmd, 1 + 0xfffff000);
179 cmd_batch_write(cmd, 1);
180}
181
182/**
Chia-I Wue24c3292014-08-21 14:05:23 +0800183 * End the batch buffer.
184 */
185static inline void cmd_batch_end(struct intel_cmd *cmd)
186{
Chia-I Wu79dfbb32014-08-25 12:19:02 +0800187 struct intel_cmd_writer *writer = &cmd->batch;
188 const struct intel_cmd_writer *state = &cmd->state;
189 const struct intel_cmd_writer *kernel = &cmd->kernel;
190
191 cmd_reserve_reloc(cmd, 5);
Chia-I Wu32a22462014-08-26 14:13:46 +0800192 cmd_writer_add_reloc(cmd, writer, 2, 1, state->bo, 0);
193 cmd_writer_add_reloc(cmd, writer, 3, 1, state->bo, 0);
194 cmd_writer_add_reloc(cmd, writer, 5, 1, kernel->bo, 0);
195 cmd_writer_add_reloc(cmd, writer, 7, 1 +
196 (state->size << 2), state->bo, 0);
197 cmd_writer_add_reloc(cmd, writer, 9, 1 +
198 (kernel->size << 2), kernel->bo, 0);
Chia-I Wu79dfbb32014-08-25 12:19:02 +0800199
Chia-I Wue24c3292014-08-21 14:05:23 +0800200 if (cmd->batch.used & 1) {
201 cmd_batch_reserve(cmd, 1);
Chia-I Wu426072d2014-08-26 14:31:55 +0800202 cmd_batch_write(cmd, GEN6_MI_CMD(MI_BATCH_BUFFER_END));
Chia-I Wue24c3292014-08-21 14:05:23 +0800203 } else {
204 cmd_batch_reserve(cmd, 2);
Chia-I Wu426072d2014-08-26 14:31:55 +0800205 cmd_batch_write(cmd, GEN6_MI_CMD(MI_BATCH_BUFFER_END));
206 cmd_batch_write(cmd, GEN6_MI_CMD(MI_NOOP));
Chia-I Wue24c3292014-08-21 14:05:23 +0800207 }
Chia-I Wu343b1372014-08-20 16:39:20 +0800208}
209
Chia-I Wu525c6602014-08-27 10:22:34 +0800210void cmd_batch_flush(struct intel_cmd *cmd, uint32_t pipe_control_dw0);
211
Chia-I Wu759fa2e2014-08-30 18:44:47 +0800212void cmd_batch_depth_count(struct intel_cmd *cmd,
213 struct intel_bo *bo,
214 XGL_GPU_SIZE offset);
215
Chia-I Wue8dbd5d2014-08-31 13:15:58 +0800216void cmd_batch_timestamp(struct intel_cmd *cmd,
217 struct intel_bo *bo,
218 XGL_GPU_SIZE offset);
219
220void cmd_batch_immediate(struct intel_cmd *cmd,
221 struct intel_bo *bo,
222 XGL_GPU_SIZE offset,
223 uint64_t val);
Chia-I Wu24565ee2014-08-21 20:24:31 +0800224/**
Chia-I Wucdff0592014-08-22 09:27:36 +0800225 * Reserve \p len DWords in the state buffer for building a hardware state.
226 * The current writer position is aligned to \p alignment first. Both the
227 * pointer to the reserved region and the aligned position are returned.
228 *
229 * Note that the returned pointer is only valid until the next reserve call.
Chia-I Wu24565ee2014-08-21 20:24:31 +0800230 */
231static inline uint32_t *cmd_state_reserve(struct intel_cmd *cmd, XGL_UINT len,
232 XGL_UINT alignment, XGL_UINT *pos)
233{
234 struct intel_cmd_writer *writer = &cmd->state;
235 XGL_UINT aligned;
236
237 assert(alignment && u_is_pow2(alignment));
238 aligned = u_align(writer->used, alignment);
239
240 if (aligned + len > writer->size)
241 cmd_writer_grow(cmd, writer);
242 assert(aligned + len <= writer->size);
243
244 writer->used = aligned;
245 *pos = aligned;
246
247 return &((uint32_t *) writer->ptr_opaque)[writer->used];
248}
249
250/**
Chia-I Wucdff0592014-08-22 09:27:36 +0800251 * Similar to \p cmd_state_reserve, except that \p reloc_len relocs are also
252 * reserved.
Chia-I Wu24565ee2014-08-21 20:24:31 +0800253 */
Chia-I Wucdff0592014-08-22 09:27:36 +0800254static inline uint32_t *cmd_state_reserve_reloc(struct intel_cmd *cmd,
255 XGL_UINT len,
256 XGL_UINT reloc_len,
257 XGL_UINT alignment,
258 XGL_UINT *pos)
Chia-I Wu24565ee2014-08-21 20:24:31 +0800259{
Chia-I Wucdff0592014-08-22 09:27:36 +0800260 cmd_reserve_reloc(cmd, reloc_len);
261 return cmd_state_reserve(cmd, len, alignment, pos);
Chia-I Wu24565ee2014-08-21 20:24:31 +0800262}
263
264/**
Chia-I Wubda55fd2014-08-25 12:46:10 +0800265 * Add a reloc at \p offset, relative to the current writer position. No
266 * error checking.
267 */
268static inline void cmd_state_reloc(struct intel_cmd *cmd,
269 XGL_INT offset, uint32_t val,
270 struct intel_bo *bo,
Chia-I Wu32a22462014-08-26 14:13:46 +0800271 uint32_t flags)
Chia-I Wubda55fd2014-08-25 12:46:10 +0800272{
273 struct intel_cmd_writer *writer = &cmd->state;
274
Chia-I Wu32a22462014-08-26 14:13:46 +0800275 cmd_writer_add_reloc(cmd, writer, writer->used + offset, val, bo, flags);
Chia-I Wubda55fd2014-08-25 12:46:10 +0800276}
277
278/**
Chia-I Wucdff0592014-08-22 09:27:36 +0800279 * Advance the writer position of the state buffer. No error checking.
Chia-I Wu24565ee2014-08-21 20:24:31 +0800280 */
281static inline void cmd_state_advance(struct intel_cmd *cmd, XGL_UINT len)
282{
283 struct intel_cmd_writer *writer = &cmd->state;
284
285 assert(writer->used + len <= writer->size);
286 writer->used += len;
287}
288
289/**
Chia-I Wucdff0592014-08-22 09:27:36 +0800290 * A convenient function to copy a hardware state of \p len DWords into the
291 * state buffer. The position of the state is returned.
Chia-I Wu24565ee2014-08-21 20:24:31 +0800292 */
293static inline XGL_UINT cmd_state_copy(struct intel_cmd *cmd,
294 const uint32_t *vals, XGL_UINT len,
295 XGL_UINT alignment)
296{
297 uint32_t *dst;
298 XGL_UINT pos;
299
300 dst = cmd_state_reserve(cmd, len, alignment, &pos);
301 memcpy(dst, vals, sizeof(uint32_t) * len);
302 cmd_state_advance(cmd, len);
303
304 return pos;
305}
306
Chia-I Wu1cbc0052014-08-25 09:50:12 +0800307static inline XGL_UINT cmd_kernel_copy(struct intel_cmd *cmd,
308 const void *kernel, XGL_SIZE size)
309{
310 /*
311 * From the Sandy Bridge PRM, volume 4 part 2, page 112:
312 *
313 * "Due to prefetch of the instruction stream, the EUs may attempt to
314 * access up to 8 instructions (128 bytes) beyond the end of the
315 * kernel program - possibly into the next memory page. Although
316 * these instructions will not be executed, software must account for
317 * the prefetch in order to avoid invalid page access faults."
318 */
319 const XGL_UINT prefetch_len = 128 / sizeof(uint32_t);
320 /* kernels are aligned to 64-byte */
321 const XGL_UINT kernel_align = 64 / sizeof(uint32_t);
322 const XGL_UINT kernel_len = ((size + 3) & ~3) / sizeof(uint32_t);
323 struct intel_cmd_writer *writer = &cmd->kernel;
324 XGL_UINT kernel_pos;
325
326 kernel_pos = u_align(writer->used, kernel_align);
327 if (kernel_pos + kernel_len + prefetch_len > writer->size)
328 cmd_writer_grow(cmd, writer);
329 assert(kernel_pos + kernel_len + prefetch_len <= writer->size);
330
331 memcpy(&((uint32_t *) writer->ptr_opaque)[kernel_pos], kernel, size);
332 writer->used = kernel_pos + kernel_len;
333
334 return kernel_pos;
335}
336
Chia-I Wu00a23b22014-08-20 15:28:08 +0800337#endif /* CMD_PRIV_H */