xref: /dflybsd-src/sys/dev/drm/i915/i915_cmd_parser.c (revision 3f2dd94a569761201b5b0a18b2f697f97fe1b9dc)
1ba55f2f5SFrançois Tigeot /*
2ba55f2f5SFrançois Tigeot  * Copyright © 2013 Intel Corporation
3ba55f2f5SFrançois Tigeot  *
4ba55f2f5SFrançois Tigeot  * Permission is hereby granted, free of charge, to any person obtaining a
5ba55f2f5SFrançois Tigeot  * copy of this software and associated documentation files (the "Software"),
6ba55f2f5SFrançois Tigeot  * to deal in the Software without restriction, including without limitation
7ba55f2f5SFrançois Tigeot  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8ba55f2f5SFrançois Tigeot  * and/or sell copies of the Software, and to permit persons to whom the
9ba55f2f5SFrançois Tigeot  * Software is furnished to do so, subject to the following conditions:
10ba55f2f5SFrançois Tigeot  *
11ba55f2f5SFrançois Tigeot  * The above copyright notice and this permission notice (including the next
12ba55f2f5SFrançois Tigeot  * paragraph) shall be included in all copies or substantial portions of the
13ba55f2f5SFrançois Tigeot  * Software.
14ba55f2f5SFrançois Tigeot  *
15ba55f2f5SFrançois Tigeot  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16ba55f2f5SFrançois Tigeot  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17ba55f2f5SFrançois Tigeot  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18ba55f2f5SFrançois Tigeot  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19ba55f2f5SFrançois Tigeot  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20ba55f2f5SFrançois Tigeot  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21ba55f2f5SFrançois Tigeot  * IN THE SOFTWARE.
22ba55f2f5SFrançois Tigeot  *
23ba55f2f5SFrançois Tigeot  * Authors:
24ba55f2f5SFrançois Tigeot  *    Brad Volkin <bradley.d.volkin@intel.com>
25ba55f2f5SFrançois Tigeot  *
26ba55f2f5SFrançois Tigeot  */
27ba55f2f5SFrançois Tigeot 
28ba55f2f5SFrançois Tigeot #include "i915_drv.h"
29ba55f2f5SFrançois Tigeot 
30ba55f2f5SFrançois Tigeot /**
31ba55f2f5SFrançois Tigeot  * DOC: batch buffer command parser
32ba55f2f5SFrançois Tigeot  *
33ba55f2f5SFrançois Tigeot  * Motivation:
34ba55f2f5SFrançois Tigeot  * Certain OpenGL features (e.g. transform feedback, performance monitoring)
35ba55f2f5SFrançois Tigeot  * require userspace code to submit batches containing commands such as
36ba55f2f5SFrançois Tigeot  * MI_LOAD_REGISTER_IMM to access various registers. Unfortunately, some
37ba55f2f5SFrançois Tigeot  * generations of the hardware will noop these commands in "unsecure" batches
38ba55f2f5SFrançois Tigeot  * (which includes all userspace batches submitted via i915) even though the
39ba55f2f5SFrançois Tigeot  * commands may be safe and represent the intended programming model of the
40ba55f2f5SFrançois Tigeot  * device.
41ba55f2f5SFrançois Tigeot  *
42ba55f2f5SFrançois Tigeot  * The software command parser is similar in operation to the command parsing
43ba55f2f5SFrançois Tigeot  * done in hardware for unsecure batches. However, the software parser allows
44ba55f2f5SFrançois Tigeot  * some operations that would be noop'd by hardware, if the parser determines
45ba55f2f5SFrançois Tigeot  * the operation is safe, and submits the batch as "secure" to prevent hardware
46ba55f2f5SFrançois Tigeot  * parsing.
47ba55f2f5SFrançois Tigeot  *
48ba55f2f5SFrançois Tigeot  * Threats:
49ba55f2f5SFrançois Tigeot  * At a high level, the hardware (and software) checks attempt to prevent
50ba55f2f5SFrançois Tigeot  * granting userspace undue privileges. There are three categories of privilege.
51ba55f2f5SFrançois Tigeot  *
52ba55f2f5SFrançois Tigeot  * First, commands which are explicitly defined as privileged or which should
53ba55f2f5SFrançois Tigeot  * only be used by the kernel driver. The parser generally rejects such
54ba55f2f5SFrançois Tigeot  * commands, though it may allow some from the drm master process.
55ba55f2f5SFrançois Tigeot  *
56ba55f2f5SFrançois Tigeot  * Second, commands which access registers. To support correct/enhanced
57ba55f2f5SFrançois Tigeot  * userspace functionality, particularly certain OpenGL extensions, the parser
58ba55f2f5SFrançois Tigeot  * provides a whitelist of registers which userspace may safely access (for both
59ba55f2f5SFrançois Tigeot  * normal and drm master processes).
60ba55f2f5SFrançois Tigeot  *
61ba55f2f5SFrançois Tigeot  * Third, commands which access privileged memory (i.e. GGTT, HWS page, etc).
62ba55f2f5SFrançois Tigeot  * The parser always rejects such commands.
63ba55f2f5SFrançois Tigeot  *
64ba55f2f5SFrançois Tigeot  * The majority of the problematic commands fall in the MI_* range, with only a
6571f41f3eSFrançois Tigeot  * few specific commands on each engine (e.g. PIPE_CONTROL and MI_FLUSH_DW).
66ba55f2f5SFrançois Tigeot  *
67ba55f2f5SFrançois Tigeot  * Implementation:
6871f41f3eSFrançois Tigeot  * Each engine maintains tables of commands and registers which the parser
6971f41f3eSFrançois Tigeot  * uses in scanning batch buffers submitted to that engine.
70ba55f2f5SFrançois Tigeot  *
71ba55f2f5SFrançois Tigeot  * Since the set of commands that the parser must check for is significantly
72ba55f2f5SFrançois Tigeot  * smaller than the number of commands supported, the parser tables contain only
73ba55f2f5SFrançois Tigeot  * those commands required by the parser. This generally works because command
74ba55f2f5SFrançois Tigeot  * opcode ranges have standard command length encodings. So for commands that
75ba55f2f5SFrançois Tigeot  * the parser does not need to check, it can easily skip them. This is
7671f41f3eSFrançois Tigeot  * implemented via a per-engine length decoding vfunc.
77ba55f2f5SFrançois Tigeot  *
78ba55f2f5SFrançois Tigeot  * Unfortunately, there are a number of commands that do not follow the standard
79ba55f2f5SFrançois Tigeot  * length encoding for their opcode range, primarily amongst the MI_* commands.
80ba55f2f5SFrançois Tigeot  * To handle this, the parser provides a way to define explicit "skip" entries
8171f41f3eSFrançois Tigeot  * in the per-engine command tables.
82ba55f2f5SFrançois Tigeot  *
83ba55f2f5SFrançois Tigeot  * Other command table entries map fairly directly to high level categories
84ba55f2f5SFrançois Tigeot  * mentioned above: rejected, master-only, register whitelist. The parser
85ba55f2f5SFrançois Tigeot  * implements a number of checks, including the privileged memory checks, via a
86ba55f2f5SFrançois Tigeot  * general bitmasking mechanism.
87ba55f2f5SFrançois Tigeot  */
88ba55f2f5SFrançois Tigeot 
89a85cb24fSFrançois Tigeot /*
90a85cb24fSFrançois Tigeot  * A command that requires special handling by the command parser.
91a85cb24fSFrançois Tigeot  */
92a85cb24fSFrançois Tigeot struct drm_i915_cmd_descriptor {
93a85cb24fSFrançois Tigeot 	/*
94a85cb24fSFrançois Tigeot 	 * Flags describing how the command parser processes the command.
95a85cb24fSFrançois Tigeot 	 *
96a85cb24fSFrançois Tigeot 	 * CMD_DESC_FIXED: The command has a fixed length if this is set,
97a85cb24fSFrançois Tigeot 	 *                 a length mask if not set
98a85cb24fSFrançois Tigeot 	 * CMD_DESC_SKIP: The command is allowed but does not follow the
99a85cb24fSFrançois Tigeot 	 *                standard length encoding for the opcode range in
100a85cb24fSFrançois Tigeot 	 *                which it falls
101a85cb24fSFrançois Tigeot 	 * CMD_DESC_REJECT: The command is never allowed
102a85cb24fSFrançois Tigeot 	 * CMD_DESC_REGISTER: The command should be checked against the
103a85cb24fSFrançois Tigeot 	 *                    register whitelist for the appropriate ring
104a85cb24fSFrançois Tigeot 	 * CMD_DESC_MASTER: The command is allowed if the submitting process
105a85cb24fSFrançois Tigeot 	 *                  is the DRM master
106a85cb24fSFrançois Tigeot 	 */
107a85cb24fSFrançois Tigeot 	u32 flags;
108a85cb24fSFrançois Tigeot #define CMD_DESC_FIXED    (1<<0)
109a85cb24fSFrançois Tigeot #define CMD_DESC_SKIP     (1<<1)
110a85cb24fSFrançois Tigeot #define CMD_DESC_REJECT   (1<<2)
111a85cb24fSFrançois Tigeot #define CMD_DESC_REGISTER (1<<3)
112a85cb24fSFrançois Tigeot #define CMD_DESC_BITMASK  (1<<4)
113a85cb24fSFrançois Tigeot #define CMD_DESC_MASTER   (1<<5)
114a85cb24fSFrançois Tigeot 
115a85cb24fSFrançois Tigeot 	/*
116a85cb24fSFrançois Tigeot 	 * The command's unique identification bits and the bitmask to get them.
117a85cb24fSFrançois Tigeot 	 * This isn't strictly the opcode field as defined in the spec and may
118a85cb24fSFrançois Tigeot 	 * also include type, subtype, and/or subop fields.
119a85cb24fSFrançois Tigeot 	 */
120a85cb24fSFrançois Tigeot 	struct {
121a85cb24fSFrançois Tigeot 		u32 value;
122a85cb24fSFrançois Tigeot 		u32 mask;
123a85cb24fSFrançois Tigeot 	} cmd;
124a85cb24fSFrançois Tigeot 
125a85cb24fSFrançois Tigeot 	/*
126a85cb24fSFrançois Tigeot 	 * The command's length. The command is either fixed length (i.e. does
127a85cb24fSFrançois Tigeot 	 * not include a length field) or has a length field mask. The flag
128a85cb24fSFrançois Tigeot 	 * CMD_DESC_FIXED indicates a fixed length. Otherwise, the command has
129a85cb24fSFrançois Tigeot 	 * a length mask. All command entries in a command table must include
130a85cb24fSFrançois Tigeot 	 * length information.
131a85cb24fSFrançois Tigeot 	 */
132a85cb24fSFrançois Tigeot 	union {
133a85cb24fSFrançois Tigeot 		u32 fixed;
134a85cb24fSFrançois Tigeot 		u32 mask;
135a85cb24fSFrançois Tigeot 	} length;
136a85cb24fSFrançois Tigeot 
137a85cb24fSFrançois Tigeot 	/*
138a85cb24fSFrançois Tigeot 	 * Describes where to find a register address in the command to check
139a85cb24fSFrançois Tigeot 	 * against the ring's register whitelist. Only valid if flags has the
140a85cb24fSFrançois Tigeot 	 * CMD_DESC_REGISTER bit set.
141a85cb24fSFrançois Tigeot 	 *
142a85cb24fSFrançois Tigeot 	 * A non-zero step value implies that the command may access multiple
143a85cb24fSFrançois Tigeot 	 * registers in sequence (e.g. LRI), in that case step gives the
144a85cb24fSFrançois Tigeot 	 * distance in dwords between individual offset fields.
145a85cb24fSFrançois Tigeot 	 */
146a85cb24fSFrançois Tigeot 	struct {
147a85cb24fSFrançois Tigeot 		u32 offset;
148a85cb24fSFrançois Tigeot 		u32 mask;
149a85cb24fSFrançois Tigeot 		u32 step;
150a85cb24fSFrançois Tigeot 	} reg;
151a85cb24fSFrançois Tigeot 
152a85cb24fSFrançois Tigeot #define MAX_CMD_DESC_BITMASKS 3
153a85cb24fSFrançois Tigeot 	/*
154a85cb24fSFrançois Tigeot 	 * Describes command checks where a particular dword is masked and
155a85cb24fSFrançois Tigeot 	 * compared against an expected value. If the command does not match
156a85cb24fSFrançois Tigeot 	 * the expected value, the parser rejects it. Only valid if flags has
157a85cb24fSFrançois Tigeot 	 * the CMD_DESC_BITMASK bit set. Only entries where mask is non-zero
158a85cb24fSFrançois Tigeot 	 * are valid.
159a85cb24fSFrançois Tigeot 	 *
160a85cb24fSFrançois Tigeot 	 * If the check specifies a non-zero condition_mask then the parser
161a85cb24fSFrançois Tigeot 	 * only performs the check when the bits specified by condition_mask
162a85cb24fSFrançois Tigeot 	 * are non-zero.
163a85cb24fSFrançois Tigeot 	 */
164a85cb24fSFrançois Tigeot 	struct {
165a85cb24fSFrançois Tigeot 		u32 offset;
166a85cb24fSFrançois Tigeot 		u32 mask;
167a85cb24fSFrançois Tigeot 		u32 expected;
168a85cb24fSFrançois Tigeot 		u32 condition_offset;
169a85cb24fSFrançois Tigeot 		u32 condition_mask;
170a85cb24fSFrançois Tigeot 	} bits[MAX_CMD_DESC_BITMASKS];
171a85cb24fSFrançois Tigeot };
172a85cb24fSFrançois Tigeot 
173a85cb24fSFrançois Tigeot /*
174a85cb24fSFrançois Tigeot  * A table of commands requiring special handling by the command parser.
175a85cb24fSFrançois Tigeot  *
176a85cb24fSFrançois Tigeot  * Each engine has an array of tables. Each table consists of an array of
177a85cb24fSFrançois Tigeot  * command descriptors, which must be sorted with command opcodes in
178a85cb24fSFrançois Tigeot  * ascending order.
179a85cb24fSFrançois Tigeot  */
180a85cb24fSFrançois Tigeot struct drm_i915_cmd_table {
181a85cb24fSFrançois Tigeot 	const struct drm_i915_cmd_descriptor *table;
182a85cb24fSFrançois Tigeot 	int count;
183a85cb24fSFrançois Tigeot };
184a85cb24fSFrançois Tigeot 
1851e12ee3bSFrançois Tigeot #define STD_MI_OPCODE_SHIFT  (32 - 9)
1861e12ee3bSFrançois Tigeot #define STD_3D_OPCODE_SHIFT  (32 - 16)
1871e12ee3bSFrançois Tigeot #define STD_2D_OPCODE_SHIFT  (32 - 10)
1881e12ee3bSFrançois Tigeot #define STD_MFX_OPCODE_SHIFT (32 - 16)
1891e12ee3bSFrançois Tigeot #define MIN_OPCODE_SHIFT 16
190ba55f2f5SFrançois Tigeot 
191ba55f2f5SFrançois Tigeot #define CMD(op, opm, f, lm, fl, ...)				\
192ba55f2f5SFrançois Tigeot 	{							\
193ba55f2f5SFrançois Tigeot 		.flags = (fl) | ((f) ? CMD_DESC_FIXED : 0),	\
1941e12ee3bSFrançois Tigeot 		.cmd = { (op), ~0u << (opm) },			\
195ba55f2f5SFrançois Tigeot 		.length = { (lm) },				\
196ba55f2f5SFrançois Tigeot 		__VA_ARGS__					\
197ba55f2f5SFrançois Tigeot 	}
198ba55f2f5SFrançois Tigeot 
199ba55f2f5SFrançois Tigeot /* Convenience macros to compress the tables */
2001e12ee3bSFrançois Tigeot #define SMI STD_MI_OPCODE_SHIFT
2011e12ee3bSFrançois Tigeot #define S3D STD_3D_OPCODE_SHIFT
2021e12ee3bSFrançois Tigeot #define S2D STD_2D_OPCODE_SHIFT
2031e12ee3bSFrançois Tigeot #define SMFX STD_MFX_OPCODE_SHIFT
204ba55f2f5SFrançois Tigeot #define F true
205ba55f2f5SFrançois Tigeot #define S CMD_DESC_SKIP
206ba55f2f5SFrançois Tigeot #define R CMD_DESC_REJECT
207ba55f2f5SFrançois Tigeot #define W CMD_DESC_REGISTER
208ba55f2f5SFrançois Tigeot #define B CMD_DESC_BITMASK
209ba55f2f5SFrançois Tigeot #define M CMD_DESC_MASTER
210ba55f2f5SFrançois Tigeot 
211ba55f2f5SFrançois Tigeot /*            Command                          Mask   Fixed Len   Action
212ba55f2f5SFrançois Tigeot 	      ---------------------------------------------------------- */
213ba55f2f5SFrançois Tigeot static const struct drm_i915_cmd_descriptor common_cmds[] = {
214ba55f2f5SFrançois Tigeot 	CMD(  MI_NOOP,                          SMI,    F,  1,      S  ),
215ba55f2f5SFrançois Tigeot 	CMD(  MI_USER_INTERRUPT,                SMI,    F,  1,      R  ),
216ba55f2f5SFrançois Tigeot 	CMD(  MI_WAIT_FOR_EVENT,                SMI,    F,  1,      M  ),
217ba55f2f5SFrançois Tigeot 	CMD(  MI_ARB_CHECK,                     SMI,    F,  1,      S  ),
218ba55f2f5SFrançois Tigeot 	CMD(  MI_REPORT_HEAD,                   SMI,    F,  1,      S  ),
219ba55f2f5SFrançois Tigeot 	CMD(  MI_SUSPEND_FLUSH,                 SMI,    F,  1,      S  ),
220ba55f2f5SFrançois Tigeot 	CMD(  MI_SEMAPHORE_MBOX,                SMI,   !F,  0xFF,   R  ),
221ba55f2f5SFrançois Tigeot 	CMD(  MI_STORE_DWORD_INDEX,             SMI,   !F,  0xFF,   R  ),
222ba55f2f5SFrançois Tigeot 	CMD(  MI_LOAD_REGISTER_IMM(1),          SMI,   !F,  0xFF,   W,
22319c468b4SFrançois Tigeot 	      .reg = { .offset = 1, .mask = 0x007FFFFC, .step = 2 }    ),
224352ff8bdSFrançois Tigeot 	CMD(  MI_STORE_REGISTER_MEM,            SMI,    F,  3,     W | B,
225ba55f2f5SFrançois Tigeot 	      .reg = { .offset = 1, .mask = 0x007FFFFC },
226ba55f2f5SFrançois Tigeot 	      .bits = {{
227ba55f2f5SFrançois Tigeot 			.offset = 0,
228ba55f2f5SFrançois Tigeot 			.mask = MI_GLOBAL_GTT,
229ba55f2f5SFrançois Tigeot 			.expected = 0,
230ba55f2f5SFrançois Tigeot 	      }},						       ),
231352ff8bdSFrançois Tigeot 	CMD(  MI_LOAD_REGISTER_MEM,             SMI,    F,  3,     W | B,
232ba55f2f5SFrançois Tigeot 	      .reg = { .offset = 1, .mask = 0x007FFFFC },
233ba55f2f5SFrançois Tigeot 	      .bits = {{
234ba55f2f5SFrançois Tigeot 			.offset = 0,
235ba55f2f5SFrançois Tigeot 			.mask = MI_GLOBAL_GTT,
236ba55f2f5SFrançois Tigeot 			.expected = 0,
237ba55f2f5SFrançois Tigeot 	      }},						       ),
2382c9916cdSFrançois Tigeot 	/*
2392c9916cdSFrançois Tigeot 	 * MI_BATCH_BUFFER_START requires some special handling. It's not
2402c9916cdSFrançois Tigeot 	 * really a 'skip' action but it doesn't seem like it's worth adding
2412c9916cdSFrançois Tigeot 	 * a new action. See i915_parse_cmds().
2422c9916cdSFrançois Tigeot 	 */
243ba55f2f5SFrançois Tigeot 	CMD(  MI_BATCH_BUFFER_START,            SMI,   !F,  0xFF,   S  ),
244ba55f2f5SFrançois Tigeot };
245ba55f2f5SFrançois Tigeot 
246ba55f2f5SFrançois Tigeot static const struct drm_i915_cmd_descriptor render_cmds[] = {
247ba55f2f5SFrançois Tigeot 	CMD(  MI_FLUSH,                         SMI,    F,  1,      S  ),
248ba55f2f5SFrançois Tigeot 	CMD(  MI_ARB_ON_OFF,                    SMI,    F,  1,      R  ),
249ba55f2f5SFrançois Tigeot 	CMD(  MI_PREDICATE,                     SMI,    F,  1,      S  ),
250ba55f2f5SFrançois Tigeot 	CMD(  MI_TOPOLOGY_FILTER,               SMI,    F,  1,      S  ),
2512c9916cdSFrançois Tigeot 	CMD(  MI_SET_APPID,                     SMI,    F,  1,      S  ),
252a05eeebfSFrançois Tigeot 	CMD(  MI_DISPLAY_FLIP,                  SMI,   !F,  0xFF,   R  ),
253ba55f2f5SFrançois Tigeot 	CMD(  MI_SET_CONTEXT,                   SMI,   !F,  0xFF,   R  ),
254ba55f2f5SFrançois Tigeot 	CMD(  MI_URB_CLEAR,                     SMI,   !F,  0xFF,   S  ),
255ba55f2f5SFrançois Tigeot 	CMD(  MI_STORE_DWORD_IMM,               SMI,   !F,  0x3F,   B,
256ba55f2f5SFrançois Tigeot 	      .bits = {{
257ba55f2f5SFrançois Tigeot 			.offset = 0,
258ba55f2f5SFrançois Tigeot 			.mask = MI_GLOBAL_GTT,
259ba55f2f5SFrançois Tigeot 			.expected = 0,
260ba55f2f5SFrançois Tigeot 	      }},						       ),
261ba55f2f5SFrançois Tigeot 	CMD(  MI_UPDATE_GTT,                    SMI,   !F,  0xFF,   R  ),
262ba55f2f5SFrançois Tigeot 	CMD(  MI_CLFLUSH,                       SMI,   !F,  0x3FF,  B,
263ba55f2f5SFrançois Tigeot 	      .bits = {{
264ba55f2f5SFrançois Tigeot 			.offset = 0,
265ba55f2f5SFrançois Tigeot 			.mask = MI_GLOBAL_GTT,
266ba55f2f5SFrançois Tigeot 			.expected = 0,
267ba55f2f5SFrançois Tigeot 	      }},						       ),
268ba55f2f5SFrançois Tigeot 	CMD(  MI_REPORT_PERF_COUNT,             SMI,   !F,  0x3F,   B,
269ba55f2f5SFrançois Tigeot 	      .bits = {{
270ba55f2f5SFrançois Tigeot 			.offset = 1,
271ba55f2f5SFrançois Tigeot 			.mask = MI_REPORT_PERF_COUNT_GGTT,
272ba55f2f5SFrançois Tigeot 			.expected = 0,
273ba55f2f5SFrançois Tigeot 	      }},						       ),
274ba55f2f5SFrançois Tigeot 	CMD(  MI_CONDITIONAL_BATCH_BUFFER_END,  SMI,   !F,  0xFF,   B,
275ba55f2f5SFrançois Tigeot 	      .bits = {{
276ba55f2f5SFrançois Tigeot 			.offset = 0,
277ba55f2f5SFrançois Tigeot 			.mask = MI_GLOBAL_GTT,
278ba55f2f5SFrançois Tigeot 			.expected = 0,
279ba55f2f5SFrançois Tigeot 	      }},						       ),
280ba55f2f5SFrançois Tigeot 	CMD(  GFX_OP_3DSTATE_VF_STATISTICS,     S3D,    F,  1,      S  ),
281ba55f2f5SFrançois Tigeot 	CMD(  PIPELINE_SELECT,                  S3D,    F,  1,      S  ),
282ba55f2f5SFrançois Tigeot 	CMD(  MEDIA_VFE_STATE,			S3D,   !F,  0xFFFF, B,
283ba55f2f5SFrançois Tigeot 	      .bits = {{
284ba55f2f5SFrançois Tigeot 			.offset = 2,
285ba55f2f5SFrançois Tigeot 			.mask = MEDIA_VFE_STATE_MMIO_ACCESS_MASK,
286ba55f2f5SFrançois Tigeot 			.expected = 0,
287ba55f2f5SFrançois Tigeot 	      }},						       ),
288ba55f2f5SFrançois Tigeot 	CMD(  GPGPU_OBJECT,                     S3D,   !F,  0xFF,   S  ),
289ba55f2f5SFrançois Tigeot 	CMD(  GPGPU_WALKER,                     S3D,   !F,  0xFF,   S  ),
290ba55f2f5SFrançois Tigeot 	CMD(  GFX_OP_3DSTATE_SO_DECL_LIST,      S3D,   !F,  0x1FF,  S  ),
291ba55f2f5SFrançois Tigeot 	CMD(  GFX_OP_PIPE_CONTROL(5),           S3D,   !F,  0xFF,   B,
292ba55f2f5SFrançois Tigeot 	      .bits = {{
293ba55f2f5SFrançois Tigeot 			.offset = 1,
294ba55f2f5SFrançois Tigeot 			.mask = (PIPE_CONTROL_MMIO_WRITE | PIPE_CONTROL_NOTIFY),
295ba55f2f5SFrançois Tigeot 			.expected = 0,
296ba55f2f5SFrançois Tigeot 	      },
297ba55f2f5SFrançois Tigeot 	      {
298ba55f2f5SFrançois Tigeot 			.offset = 1,
299ba55f2f5SFrançois Tigeot 		        .mask = (PIPE_CONTROL_GLOBAL_GTT_IVB |
300ba55f2f5SFrançois Tigeot 				 PIPE_CONTROL_STORE_DATA_INDEX),
301ba55f2f5SFrançois Tigeot 			.expected = 0,
302ba55f2f5SFrançois Tigeot 			.condition_offset = 1,
303ba55f2f5SFrançois Tigeot 			.condition_mask = PIPE_CONTROL_POST_SYNC_OP_MASK,
304ba55f2f5SFrançois Tigeot 	      }},						       ),
305ba55f2f5SFrançois Tigeot };
306ba55f2f5SFrançois Tigeot 
307ba55f2f5SFrançois Tigeot static const struct drm_i915_cmd_descriptor hsw_render_cmds[] = {
308ba55f2f5SFrançois Tigeot 	CMD(  MI_SET_PREDICATE,                 SMI,    F,  1,      S  ),
309ba55f2f5SFrançois Tigeot 	CMD(  MI_RS_CONTROL,                    SMI,    F,  1,      S  ),
310ba55f2f5SFrançois Tigeot 	CMD(  MI_URB_ATOMIC_ALLOC,              SMI,    F,  1,      S  ),
3112c9916cdSFrançois Tigeot 	CMD(  MI_SET_APPID,                     SMI,    F,  1,      S  ),
312ba55f2f5SFrançois Tigeot 	CMD(  MI_RS_CONTEXT,                    SMI,    F,  1,      S  ),
313ba55f2f5SFrançois Tigeot 	CMD(  MI_LOAD_SCAN_LINES_INCL,          SMI,   !F,  0x3F,   M  ),
314ba55f2f5SFrançois Tigeot 	CMD(  MI_LOAD_SCAN_LINES_EXCL,          SMI,   !F,  0x3F,   R  ),
3151487f786SFrançois Tigeot 	CMD(  MI_LOAD_REGISTER_REG,             SMI,   !F,  0xFF,   W,
3161487f786SFrançois Tigeot 	      .reg = { .offset = 1, .mask = 0x007FFFFC, .step = 1 }    ),
317ba55f2f5SFrançois Tigeot 	CMD(  MI_RS_STORE_DATA_IMM,             SMI,   !F,  0xFF,   S  ),
318ba55f2f5SFrançois Tigeot 	CMD(  MI_LOAD_URB_MEM,                  SMI,   !F,  0xFF,   S  ),
319ba55f2f5SFrançois Tigeot 	CMD(  MI_STORE_URB_MEM,                 SMI,   !F,  0xFF,   S  ),
320ba55f2f5SFrançois Tigeot 	CMD(  GFX_OP_3DSTATE_DX9_CONSTANTF_VS,  S3D,   !F,  0x7FF,  S  ),
321ba55f2f5SFrançois Tigeot 	CMD(  GFX_OP_3DSTATE_DX9_CONSTANTF_PS,  S3D,   !F,  0x7FF,  S  ),
322ba55f2f5SFrançois Tigeot 
323ba55f2f5SFrançois Tigeot 	CMD(  GFX_OP_3DSTATE_BINDING_TABLE_EDIT_VS,  S3D,   !F,  0x1FF,  S  ),
324ba55f2f5SFrançois Tigeot 	CMD(  GFX_OP_3DSTATE_BINDING_TABLE_EDIT_GS,  S3D,   !F,  0x1FF,  S  ),
325ba55f2f5SFrançois Tigeot 	CMD(  GFX_OP_3DSTATE_BINDING_TABLE_EDIT_HS,  S3D,   !F,  0x1FF,  S  ),
326ba55f2f5SFrançois Tigeot 	CMD(  GFX_OP_3DSTATE_BINDING_TABLE_EDIT_DS,  S3D,   !F,  0x1FF,  S  ),
327ba55f2f5SFrançois Tigeot 	CMD(  GFX_OP_3DSTATE_BINDING_TABLE_EDIT_PS,  S3D,   !F,  0x1FF,  S  ),
328ba55f2f5SFrançois Tigeot };
329ba55f2f5SFrançois Tigeot 
330ba55f2f5SFrançois Tigeot static const struct drm_i915_cmd_descriptor video_cmds[] = {
331ba55f2f5SFrançois Tigeot 	CMD(  MI_ARB_ON_OFF,                    SMI,    F,  1,      R  ),
3322c9916cdSFrançois Tigeot 	CMD(  MI_SET_APPID,                     SMI,    F,  1,      S  ),
333ba55f2f5SFrançois Tigeot 	CMD(  MI_STORE_DWORD_IMM,               SMI,   !F,  0xFF,   B,
334ba55f2f5SFrançois Tigeot 	      .bits = {{
335ba55f2f5SFrançois Tigeot 			.offset = 0,
336ba55f2f5SFrançois Tigeot 			.mask = MI_GLOBAL_GTT,
337ba55f2f5SFrançois Tigeot 			.expected = 0,
338ba55f2f5SFrançois Tigeot 	      }},						       ),
339ba55f2f5SFrançois Tigeot 	CMD(  MI_UPDATE_GTT,                    SMI,   !F,  0x3F,   R  ),
340ba55f2f5SFrançois Tigeot 	CMD(  MI_FLUSH_DW,                      SMI,   !F,  0x3F,   B,
341ba55f2f5SFrançois Tigeot 	      .bits = {{
342ba55f2f5SFrançois Tigeot 			.offset = 0,
343ba55f2f5SFrançois Tigeot 			.mask = MI_FLUSH_DW_NOTIFY,
344ba55f2f5SFrançois Tigeot 			.expected = 0,
345ba55f2f5SFrançois Tigeot 	      },
346ba55f2f5SFrançois Tigeot 	      {
347ba55f2f5SFrançois Tigeot 			.offset = 1,
348ba55f2f5SFrançois Tigeot 			.mask = MI_FLUSH_DW_USE_GTT,
349ba55f2f5SFrançois Tigeot 			.expected = 0,
350ba55f2f5SFrançois Tigeot 			.condition_offset = 0,
351ba55f2f5SFrançois Tigeot 			.condition_mask = MI_FLUSH_DW_OP_MASK,
352ba55f2f5SFrançois Tigeot 	      },
353ba55f2f5SFrançois Tigeot 	      {
354ba55f2f5SFrançois Tigeot 			.offset = 0,
355ba55f2f5SFrançois Tigeot 			.mask = MI_FLUSH_DW_STORE_INDEX,
356ba55f2f5SFrançois Tigeot 			.expected = 0,
357ba55f2f5SFrançois Tigeot 			.condition_offset = 0,
358ba55f2f5SFrançois Tigeot 			.condition_mask = MI_FLUSH_DW_OP_MASK,
359ba55f2f5SFrançois Tigeot 	      }},						       ),
360ba55f2f5SFrançois Tigeot 	CMD(  MI_CONDITIONAL_BATCH_BUFFER_END,  SMI,   !F,  0xFF,   B,
361ba55f2f5SFrançois Tigeot 	      .bits = {{
362ba55f2f5SFrançois Tigeot 			.offset = 0,
363ba55f2f5SFrançois Tigeot 			.mask = MI_GLOBAL_GTT,
364ba55f2f5SFrançois Tigeot 			.expected = 0,
365ba55f2f5SFrançois Tigeot 	      }},						       ),
366ba55f2f5SFrançois Tigeot 	/*
367ba55f2f5SFrançois Tigeot 	 * MFX_WAIT doesn't fit the way we handle length for most commands.
368ba55f2f5SFrançois Tigeot 	 * It has a length field but it uses a non-standard length bias.
369ba55f2f5SFrançois Tigeot 	 * It is always 1 dword though, so just treat it as fixed length.
370ba55f2f5SFrançois Tigeot 	 */
371ba55f2f5SFrançois Tigeot 	CMD(  MFX_WAIT,                         SMFX,   F,  1,      S  ),
372ba55f2f5SFrançois Tigeot };
373ba55f2f5SFrançois Tigeot 
374ba55f2f5SFrançois Tigeot static const struct drm_i915_cmd_descriptor vecs_cmds[] = {
375ba55f2f5SFrançois Tigeot 	CMD(  MI_ARB_ON_OFF,                    SMI,    F,  1,      R  ),
3762c9916cdSFrançois Tigeot 	CMD(  MI_SET_APPID,                     SMI,    F,  1,      S  ),
377ba55f2f5SFrançois Tigeot 	CMD(  MI_STORE_DWORD_IMM,               SMI,   !F,  0xFF,   B,
378ba55f2f5SFrançois Tigeot 	      .bits = {{
379ba55f2f5SFrançois Tigeot 			.offset = 0,
380ba55f2f5SFrançois Tigeot 			.mask = MI_GLOBAL_GTT,
381ba55f2f5SFrançois Tigeot 			.expected = 0,
382ba55f2f5SFrançois Tigeot 	      }},						       ),
383ba55f2f5SFrançois Tigeot 	CMD(  MI_UPDATE_GTT,                    SMI,   !F,  0x3F,   R  ),
384ba55f2f5SFrançois Tigeot 	CMD(  MI_FLUSH_DW,                      SMI,   !F,  0x3F,   B,
385ba55f2f5SFrançois Tigeot 	      .bits = {{
386ba55f2f5SFrançois Tigeot 			.offset = 0,
387ba55f2f5SFrançois Tigeot 			.mask = MI_FLUSH_DW_NOTIFY,
388ba55f2f5SFrançois Tigeot 			.expected = 0,
389ba55f2f5SFrançois Tigeot 	      },
390ba55f2f5SFrançois Tigeot 	      {
391ba55f2f5SFrançois Tigeot 			.offset = 1,
392ba55f2f5SFrançois Tigeot 			.mask = MI_FLUSH_DW_USE_GTT,
393ba55f2f5SFrançois Tigeot 			.expected = 0,
394ba55f2f5SFrançois Tigeot 			.condition_offset = 0,
395ba55f2f5SFrançois Tigeot 			.condition_mask = MI_FLUSH_DW_OP_MASK,
396ba55f2f5SFrançois Tigeot 	      },
397ba55f2f5SFrançois Tigeot 	      {
398ba55f2f5SFrançois Tigeot 			.offset = 0,
399ba55f2f5SFrançois Tigeot 			.mask = MI_FLUSH_DW_STORE_INDEX,
400ba55f2f5SFrançois Tigeot 			.expected = 0,
401ba55f2f5SFrançois Tigeot 			.condition_offset = 0,
402ba55f2f5SFrançois Tigeot 			.condition_mask = MI_FLUSH_DW_OP_MASK,
403ba55f2f5SFrançois Tigeot 	      }},						       ),
404ba55f2f5SFrançois Tigeot 	CMD(  MI_CONDITIONAL_BATCH_BUFFER_END,  SMI,   !F,  0xFF,   B,
405ba55f2f5SFrançois Tigeot 	      .bits = {{
406ba55f2f5SFrançois Tigeot 			.offset = 0,
407ba55f2f5SFrançois Tigeot 			.mask = MI_GLOBAL_GTT,
408ba55f2f5SFrançois Tigeot 			.expected = 0,
409ba55f2f5SFrançois Tigeot 	      }},						       ),
410ba55f2f5SFrançois Tigeot };
411ba55f2f5SFrançois Tigeot 
412ba55f2f5SFrançois Tigeot static const struct drm_i915_cmd_descriptor blt_cmds[] = {
413ba55f2f5SFrançois Tigeot 	CMD(  MI_DISPLAY_FLIP,                  SMI,   !F,  0xFF,   R  ),
414ba55f2f5SFrançois Tigeot 	CMD(  MI_STORE_DWORD_IMM,               SMI,   !F,  0x3FF,  B,
415ba55f2f5SFrançois Tigeot 	      .bits = {{
416ba55f2f5SFrançois Tigeot 			.offset = 0,
417ba55f2f5SFrançois Tigeot 			.mask = MI_GLOBAL_GTT,
418ba55f2f5SFrançois Tigeot 			.expected = 0,
419ba55f2f5SFrançois Tigeot 	      }},						       ),
420ba55f2f5SFrançois Tigeot 	CMD(  MI_UPDATE_GTT,                    SMI,   !F,  0x3F,   R  ),
421ba55f2f5SFrançois Tigeot 	CMD(  MI_FLUSH_DW,                      SMI,   !F,  0x3F,   B,
422ba55f2f5SFrançois Tigeot 	      .bits = {{
423ba55f2f5SFrançois Tigeot 			.offset = 0,
424ba55f2f5SFrançois Tigeot 			.mask = MI_FLUSH_DW_NOTIFY,
425ba55f2f5SFrançois Tigeot 			.expected = 0,
426ba55f2f5SFrançois Tigeot 	      },
427ba55f2f5SFrançois Tigeot 	      {
428ba55f2f5SFrançois Tigeot 			.offset = 1,
429ba55f2f5SFrançois Tigeot 			.mask = MI_FLUSH_DW_USE_GTT,
430ba55f2f5SFrançois Tigeot 			.expected = 0,
431ba55f2f5SFrançois Tigeot 			.condition_offset = 0,
432ba55f2f5SFrançois Tigeot 			.condition_mask = MI_FLUSH_DW_OP_MASK,
433ba55f2f5SFrançois Tigeot 	      },
434ba55f2f5SFrançois Tigeot 	      {
435ba55f2f5SFrançois Tigeot 			.offset = 0,
436ba55f2f5SFrançois Tigeot 			.mask = MI_FLUSH_DW_STORE_INDEX,
437ba55f2f5SFrançois Tigeot 			.expected = 0,
438ba55f2f5SFrançois Tigeot 			.condition_offset = 0,
439ba55f2f5SFrançois Tigeot 			.condition_mask = MI_FLUSH_DW_OP_MASK,
440ba55f2f5SFrançois Tigeot 	      }},						       ),
441ba55f2f5SFrançois Tigeot 	CMD(  COLOR_BLT,                        S2D,   !F,  0x3F,   S  ),
442ba55f2f5SFrançois Tigeot 	CMD(  SRC_COPY_BLT,                     S2D,   !F,  0x3F,   S  ),
443ba55f2f5SFrançois Tigeot };
444ba55f2f5SFrançois Tigeot 
445ba55f2f5SFrançois Tigeot static const struct drm_i915_cmd_descriptor hsw_blt_cmds[] = {
446ba55f2f5SFrançois Tigeot 	CMD(  MI_LOAD_SCAN_LINES_INCL,          SMI,   !F,  0x3F,   M  ),
447ba55f2f5SFrançois Tigeot 	CMD(  MI_LOAD_SCAN_LINES_EXCL,          SMI,   !F,  0x3F,   R  ),
448ba55f2f5SFrançois Tigeot };
449ba55f2f5SFrançois Tigeot 
4501e12ee3bSFrançois Tigeot static const struct drm_i915_cmd_descriptor noop_desc =
4511e12ee3bSFrançois Tigeot 	CMD(MI_NOOP, SMI, F, 1, S);
4521e12ee3bSFrançois Tigeot 
453ba55f2f5SFrançois Tigeot #undef CMD
454ba55f2f5SFrançois Tigeot #undef SMI
455ba55f2f5SFrançois Tigeot #undef S3D
456ba55f2f5SFrançois Tigeot #undef S2D
457ba55f2f5SFrançois Tigeot #undef SMFX
458ba55f2f5SFrançois Tigeot #undef F
459ba55f2f5SFrançois Tigeot #undef S
460ba55f2f5SFrançois Tigeot #undef R
461ba55f2f5SFrançois Tigeot #undef W
462ba55f2f5SFrançois Tigeot #undef B
463ba55f2f5SFrançois Tigeot #undef M
464ba55f2f5SFrançois Tigeot 
465ba55f2f5SFrançois Tigeot static const struct drm_i915_cmd_table gen7_render_cmds[] = {
466ba55f2f5SFrançois Tigeot 	{ common_cmds, ARRAY_SIZE(common_cmds) },
467ba55f2f5SFrançois Tigeot 	{ render_cmds, ARRAY_SIZE(render_cmds) },
468ba55f2f5SFrançois Tigeot };
469ba55f2f5SFrançois Tigeot 
470ba55f2f5SFrançois Tigeot static const struct drm_i915_cmd_table hsw_render_ring_cmds[] = {
471ba55f2f5SFrançois Tigeot 	{ common_cmds, ARRAY_SIZE(common_cmds) },
472ba55f2f5SFrançois Tigeot 	{ render_cmds, ARRAY_SIZE(render_cmds) },
473ba55f2f5SFrançois Tigeot 	{ hsw_render_cmds, ARRAY_SIZE(hsw_render_cmds) },
474ba55f2f5SFrançois Tigeot };
475ba55f2f5SFrançois Tigeot 
476ba55f2f5SFrançois Tigeot static const struct drm_i915_cmd_table gen7_video_cmds[] = {
477ba55f2f5SFrançois Tigeot 	{ common_cmds, ARRAY_SIZE(common_cmds) },
478ba55f2f5SFrançois Tigeot 	{ video_cmds, ARRAY_SIZE(video_cmds) },
479ba55f2f5SFrançois Tigeot };
480ba55f2f5SFrançois Tigeot 
481ba55f2f5SFrançois Tigeot static const struct drm_i915_cmd_table hsw_vebox_cmds[] = {
482ba55f2f5SFrançois Tigeot 	{ common_cmds, ARRAY_SIZE(common_cmds) },
483ba55f2f5SFrançois Tigeot 	{ vecs_cmds, ARRAY_SIZE(vecs_cmds) },
484ba55f2f5SFrançois Tigeot };
485ba55f2f5SFrançois Tigeot 
486ba55f2f5SFrançois Tigeot static const struct drm_i915_cmd_table gen7_blt_cmds[] = {
487ba55f2f5SFrançois Tigeot 	{ common_cmds, ARRAY_SIZE(common_cmds) },
488ba55f2f5SFrançois Tigeot 	{ blt_cmds, ARRAY_SIZE(blt_cmds) },
489ba55f2f5SFrançois Tigeot };
490ba55f2f5SFrançois Tigeot 
491ba55f2f5SFrançois Tigeot static const struct drm_i915_cmd_table hsw_blt_ring_cmds[] = {
492ba55f2f5SFrançois Tigeot 	{ common_cmds, ARRAY_SIZE(common_cmds) },
493ba55f2f5SFrançois Tigeot 	{ blt_cmds, ARRAY_SIZE(blt_cmds) },
494ba55f2f5SFrançois Tigeot 	{ hsw_blt_cmds, ARRAY_SIZE(hsw_blt_cmds) },
495ba55f2f5SFrançois Tigeot };
496ba55f2f5SFrançois Tigeot 
497ba55f2f5SFrançois Tigeot /*
498ba55f2f5SFrançois Tigeot  * Register whitelists, sorted by increasing register offset.
49919c468b4SFrançois Tigeot  */
50019c468b4SFrançois Tigeot 
50119c468b4SFrançois Tigeot /*
50219c468b4SFrançois Tigeot  * An individual whitelist entry granting access to register addr.  If
50319c468b4SFrançois Tigeot  * mask is non-zero the argument of immediate register writes will be
50419c468b4SFrançois Tigeot  * AND-ed with mask, and the command will be rejected if the result
50519c468b4SFrançois Tigeot  * doesn't match value.
50619c468b4SFrançois Tigeot  *
50719c468b4SFrançois Tigeot  * Registers with non-zero mask are only allowed to be written using
50819c468b4SFrançois Tigeot  * LRI.
50919c468b4SFrançois Tigeot  */
51019c468b4SFrançois Tigeot struct drm_i915_reg_descriptor {
511aee94f86SFrançois Tigeot 	i915_reg_t addr;
51219c468b4SFrançois Tigeot 	u32 mask;
51319c468b4SFrançois Tigeot 	u32 value;
51419c468b4SFrançois Tigeot };
51519c468b4SFrançois Tigeot 
51619c468b4SFrançois Tigeot /* Convenience macro for adding 32-bit registers. */
517aee94f86SFrançois Tigeot #define REG32(_reg, ...) \
518aee94f86SFrançois Tigeot 	{ .addr = (_reg), __VA_ARGS__ }
51919c468b4SFrançois Tigeot 
52019c468b4SFrançois Tigeot /*
52119c468b4SFrançois Tigeot  * Convenience macro for adding 64-bit registers.
522ba55f2f5SFrançois Tigeot  *
523ba55f2f5SFrançois Tigeot  * Some registers that userspace accesses are 64 bits. The register
524ba55f2f5SFrançois Tigeot  * access commands only allow 32-bit accesses. Hence, we have to include
525ba55f2f5SFrançois Tigeot  * entries for both halves of the 64-bit registers.
526ba55f2f5SFrançois Tigeot  */
527aee94f86SFrançois Tigeot #define REG64(_reg) \
528aee94f86SFrançois Tigeot 	{ .addr = _reg }, \
529aee94f86SFrançois Tigeot 	{ .addr = _reg ## _UDW }
530aee94f86SFrançois Tigeot 
531aee94f86SFrançois Tigeot #define REG64_IDX(_reg, idx) \
532aee94f86SFrançois Tigeot 	{ .addr = _reg(idx) }, \
533aee94f86SFrançois Tigeot 	{ .addr = _reg ## _UDW(idx) }
534ba55f2f5SFrançois Tigeot 
53519c468b4SFrançois Tigeot static const struct drm_i915_reg_descriptor gen7_render_regs[] = {
5362c9916cdSFrançois Tigeot 	REG64(GPGPU_THREADS_DISPATCHED),
537ba55f2f5SFrançois Tigeot 	REG64(HS_INVOCATION_COUNT),
538ba55f2f5SFrançois Tigeot 	REG64(DS_INVOCATION_COUNT),
539ba55f2f5SFrançois Tigeot 	REG64(IA_VERTICES_COUNT),
540ba55f2f5SFrançois Tigeot 	REG64(IA_PRIMITIVES_COUNT),
541ba55f2f5SFrançois Tigeot 	REG64(VS_INVOCATION_COUNT),
542ba55f2f5SFrançois Tigeot 	REG64(GS_INVOCATION_COUNT),
543ba55f2f5SFrançois Tigeot 	REG64(GS_PRIMITIVES_COUNT),
544ba55f2f5SFrançois Tigeot 	REG64(CL_INVOCATION_COUNT),
545ba55f2f5SFrançois Tigeot 	REG64(CL_PRIMITIVES_COUNT),
546ba55f2f5SFrançois Tigeot 	REG64(PS_INVOCATION_COUNT),
547ba55f2f5SFrançois Tigeot 	REG64(PS_DEPTH_COUNT),
5488621f407SFrançois Tigeot 	REG64_IDX(RING_TIMESTAMP, RENDER_RING_BASE),
5492c9916cdSFrançois Tigeot 	REG64(MI_PREDICATE_SRC0),
5502c9916cdSFrançois Tigeot 	REG64(MI_PREDICATE_SRC1),
55119c468b4SFrançois Tigeot 	REG32(GEN7_3DPRIM_END_OFFSET),
55219c468b4SFrançois Tigeot 	REG32(GEN7_3DPRIM_START_VERTEX),
55319c468b4SFrançois Tigeot 	REG32(GEN7_3DPRIM_VERTEX_COUNT),
55419c468b4SFrançois Tigeot 	REG32(GEN7_3DPRIM_INSTANCE_COUNT),
55519c468b4SFrançois Tigeot 	REG32(GEN7_3DPRIM_START_INSTANCE),
55619c468b4SFrançois Tigeot 	REG32(GEN7_3DPRIM_BASE_VERTEX),
557352ff8bdSFrançois Tigeot 	REG32(GEN7_GPGPU_DISPATCHDIMX),
558352ff8bdSFrançois Tigeot 	REG32(GEN7_GPGPU_DISPATCHDIMY),
559352ff8bdSFrançois Tigeot 	REG32(GEN7_GPGPU_DISPATCHDIMZ),
5601e12ee3bSFrançois Tigeot 	REG64_IDX(RING_TIMESTAMP, BSD_RING_BASE),
561aee94f86SFrançois Tigeot 	REG64_IDX(GEN7_SO_NUM_PRIMS_WRITTEN, 0),
562aee94f86SFrançois Tigeot 	REG64_IDX(GEN7_SO_NUM_PRIMS_WRITTEN, 1),
563aee94f86SFrançois Tigeot 	REG64_IDX(GEN7_SO_NUM_PRIMS_WRITTEN, 2),
564aee94f86SFrançois Tigeot 	REG64_IDX(GEN7_SO_NUM_PRIMS_WRITTEN, 3),
565aee94f86SFrançois Tigeot 	REG64_IDX(GEN7_SO_PRIM_STORAGE_NEEDED, 0),
566aee94f86SFrançois Tigeot 	REG64_IDX(GEN7_SO_PRIM_STORAGE_NEEDED, 1),
567aee94f86SFrançois Tigeot 	REG64_IDX(GEN7_SO_PRIM_STORAGE_NEEDED, 2),
568aee94f86SFrançois Tigeot 	REG64_IDX(GEN7_SO_PRIM_STORAGE_NEEDED, 3),
56919c468b4SFrançois Tigeot 	REG32(GEN7_SO_WRITE_OFFSET(0)),
57019c468b4SFrançois Tigeot 	REG32(GEN7_SO_WRITE_OFFSET(1)),
57119c468b4SFrançois Tigeot 	REG32(GEN7_SO_WRITE_OFFSET(2)),
57219c468b4SFrançois Tigeot 	REG32(GEN7_SO_WRITE_OFFSET(3)),
57319c468b4SFrançois Tigeot 	REG32(GEN7_L3SQCREG1),
57419c468b4SFrançois Tigeot 	REG32(GEN7_L3CNTLREG2),
57519c468b4SFrançois Tigeot 	REG32(GEN7_L3CNTLREG3),
5761e12ee3bSFrançois Tigeot 	REG64_IDX(RING_TIMESTAMP, BLT_RING_BASE),
5778621f407SFrançois Tigeot };
5788621f407SFrançois Tigeot 
5798621f407SFrançois Tigeot static const struct drm_i915_reg_descriptor hsw_render_regs[] = {
5808621f407SFrançois Tigeot 	REG64_IDX(HSW_CS_GPR, 0),
5818621f407SFrançois Tigeot 	REG64_IDX(HSW_CS_GPR, 1),
5828621f407SFrançois Tigeot 	REG64_IDX(HSW_CS_GPR, 2),
5838621f407SFrançois Tigeot 	REG64_IDX(HSW_CS_GPR, 3),
5848621f407SFrançois Tigeot 	REG64_IDX(HSW_CS_GPR, 4),
5858621f407SFrançois Tigeot 	REG64_IDX(HSW_CS_GPR, 5),
5868621f407SFrançois Tigeot 	REG64_IDX(HSW_CS_GPR, 6),
5878621f407SFrançois Tigeot 	REG64_IDX(HSW_CS_GPR, 7),
5888621f407SFrançois Tigeot 	REG64_IDX(HSW_CS_GPR, 8),
5898621f407SFrançois Tigeot 	REG64_IDX(HSW_CS_GPR, 9),
5908621f407SFrançois Tigeot 	REG64_IDX(HSW_CS_GPR, 10),
5918621f407SFrançois Tigeot 	REG64_IDX(HSW_CS_GPR, 11),
5928621f407SFrançois Tigeot 	REG64_IDX(HSW_CS_GPR, 12),
5938621f407SFrançois Tigeot 	REG64_IDX(HSW_CS_GPR, 13),
5948621f407SFrançois Tigeot 	REG64_IDX(HSW_CS_GPR, 14),
5958621f407SFrançois Tigeot 	REG64_IDX(HSW_CS_GPR, 15),
59619c468b4SFrançois Tigeot 	REG32(HSW_SCRATCH1,
59719c468b4SFrançois Tigeot 	      .mask = ~HSW_SCRATCH1_L3_DATA_ATOMICS_DISABLE,
59819c468b4SFrançois Tigeot 	      .value = 0),
59919c468b4SFrançois Tigeot 	REG32(HSW_ROW_CHICKEN3,
60019c468b4SFrançois Tigeot 	      .mask = ~(HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE << 16 |
60119c468b4SFrançois Tigeot                         HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE),
60219c468b4SFrançois Tigeot 	      .value = 0),
603ba55f2f5SFrançois Tigeot };
604ba55f2f5SFrançois Tigeot 
60519c468b4SFrançois Tigeot static const struct drm_i915_reg_descriptor gen7_blt_regs[] = {
6061e12ee3bSFrançois Tigeot 	REG64_IDX(RING_TIMESTAMP, RENDER_RING_BASE),
6071e12ee3bSFrançois Tigeot 	REG64_IDX(RING_TIMESTAMP, BSD_RING_BASE),
60819c468b4SFrançois Tigeot 	REG32(BCS_SWCTRL),
6091e12ee3bSFrançois Tigeot 	REG64_IDX(RING_TIMESTAMP, BLT_RING_BASE),
610ba55f2f5SFrançois Tigeot };
611ba55f2f5SFrançois Tigeot 
61219c468b4SFrançois Tigeot static const struct drm_i915_reg_descriptor ivb_master_regs[] = {
61319c468b4SFrançois Tigeot 	REG32(FORCEWAKE_MT),
61419c468b4SFrançois Tigeot 	REG32(DERRMR),
61519c468b4SFrançois Tigeot 	REG32(GEN7_PIPE_DE_LOAD_SL(PIPE_A)),
61619c468b4SFrançois Tigeot 	REG32(GEN7_PIPE_DE_LOAD_SL(PIPE_B)),
61719c468b4SFrançois Tigeot 	REG32(GEN7_PIPE_DE_LOAD_SL(PIPE_C)),
618ba55f2f5SFrançois Tigeot };
619ba55f2f5SFrançois Tigeot 
62019c468b4SFrançois Tigeot static const struct drm_i915_reg_descriptor hsw_master_regs[] = {
62119c468b4SFrançois Tigeot 	REG32(FORCEWAKE_MT),
62219c468b4SFrançois Tigeot 	REG32(DERRMR),
623ba55f2f5SFrançois Tigeot };
624ba55f2f5SFrançois Tigeot 
625ba55f2f5SFrançois Tigeot #undef REG64
62619c468b4SFrançois Tigeot #undef REG32
627ba55f2f5SFrançois Tigeot 
6288621f407SFrançois Tigeot struct drm_i915_reg_table {
6298621f407SFrançois Tigeot 	const struct drm_i915_reg_descriptor *regs;
6308621f407SFrançois Tigeot 	int num_regs;
6318621f407SFrançois Tigeot 	bool master;
6328621f407SFrançois Tigeot };
6338621f407SFrançois Tigeot 
6348621f407SFrançois Tigeot static const struct drm_i915_reg_table ivb_render_reg_tables[] = {
6358621f407SFrançois Tigeot 	{ gen7_render_regs, ARRAY_SIZE(gen7_render_regs), false },
6368621f407SFrançois Tigeot 	{ ivb_master_regs, ARRAY_SIZE(ivb_master_regs), true },
6378621f407SFrançois Tigeot };
6388621f407SFrançois Tigeot 
6398621f407SFrançois Tigeot static const struct drm_i915_reg_table ivb_blt_reg_tables[] = {
6408621f407SFrançois Tigeot 	{ gen7_blt_regs, ARRAY_SIZE(gen7_blt_regs), false },
6418621f407SFrançois Tigeot 	{ ivb_master_regs, ARRAY_SIZE(ivb_master_regs), true },
6428621f407SFrançois Tigeot };
6438621f407SFrançois Tigeot 
6448621f407SFrançois Tigeot static const struct drm_i915_reg_table hsw_render_reg_tables[] = {
6458621f407SFrançois Tigeot 	{ gen7_render_regs, ARRAY_SIZE(gen7_render_regs), false },
6468621f407SFrançois Tigeot 	{ hsw_render_regs, ARRAY_SIZE(hsw_render_regs), false },
6478621f407SFrançois Tigeot 	{ hsw_master_regs, ARRAY_SIZE(hsw_master_regs), true },
6488621f407SFrançois Tigeot };
6498621f407SFrançois Tigeot 
6508621f407SFrançois Tigeot static const struct drm_i915_reg_table hsw_blt_reg_tables[] = {
6518621f407SFrançois Tigeot 	{ gen7_blt_regs, ARRAY_SIZE(gen7_blt_regs), false },
6528621f407SFrançois Tigeot 	{ hsw_master_regs, ARRAY_SIZE(hsw_master_regs), true },
6538621f407SFrançois Tigeot };
6548621f407SFrançois Tigeot 
gen7_render_get_cmd_length_mask(u32 cmd_header)655ba55f2f5SFrançois Tigeot static u32 gen7_render_get_cmd_length_mask(u32 cmd_header)
656ba55f2f5SFrançois Tigeot {
657a85cb24fSFrançois Tigeot 	u32 client = cmd_header >> INSTR_CLIENT_SHIFT;
658ba55f2f5SFrançois Tigeot 	u32 subclient =
659ba55f2f5SFrançois Tigeot 		(cmd_header & INSTR_SUBCLIENT_MASK) >> INSTR_SUBCLIENT_SHIFT;
660ba55f2f5SFrançois Tigeot 
661ba55f2f5SFrançois Tigeot 	if (client == INSTR_MI_CLIENT)
662ba55f2f5SFrançois Tigeot 		return 0x3F;
663ba55f2f5SFrançois Tigeot 	else if (client == INSTR_RC_CLIENT) {
664ba55f2f5SFrançois Tigeot 		if (subclient == INSTR_MEDIA_SUBCLIENT)
665ba55f2f5SFrançois Tigeot 			return 0xFFFF;
666ba55f2f5SFrançois Tigeot 		else
667ba55f2f5SFrançois Tigeot 			return 0xFF;
668ba55f2f5SFrançois Tigeot 	}
669ba55f2f5SFrançois Tigeot 
670ba55f2f5SFrançois Tigeot 	DRM_DEBUG_DRIVER("CMD: Abnormal rcs cmd length! 0x%08X\n", cmd_header);
671ba55f2f5SFrançois Tigeot 	return 0;
672ba55f2f5SFrançois Tigeot }
673ba55f2f5SFrançois Tigeot 
gen7_bsd_get_cmd_length_mask(u32 cmd_header)674ba55f2f5SFrançois Tigeot static u32 gen7_bsd_get_cmd_length_mask(u32 cmd_header)
675ba55f2f5SFrançois Tigeot {
676a85cb24fSFrançois Tigeot 	u32 client = cmd_header >> INSTR_CLIENT_SHIFT;
677ba55f2f5SFrançois Tigeot 	u32 subclient =
678ba55f2f5SFrançois Tigeot 		(cmd_header & INSTR_SUBCLIENT_MASK) >> INSTR_SUBCLIENT_SHIFT;
6792c9916cdSFrançois Tigeot 	u32 op = (cmd_header & INSTR_26_TO_24_MASK) >> INSTR_26_TO_24_SHIFT;
680ba55f2f5SFrançois Tigeot 
681ba55f2f5SFrançois Tigeot 	if (client == INSTR_MI_CLIENT)
682ba55f2f5SFrançois Tigeot 		return 0x3F;
683ba55f2f5SFrançois Tigeot 	else if (client == INSTR_RC_CLIENT) {
6842c9916cdSFrançois Tigeot 		if (subclient == INSTR_MEDIA_SUBCLIENT) {
6852c9916cdSFrançois Tigeot 			if (op == 6)
6862c9916cdSFrançois Tigeot 				return 0xFFFF;
687ba55f2f5SFrançois Tigeot 			else
6882c9916cdSFrançois Tigeot 				return 0xFFF;
6892c9916cdSFrançois Tigeot 		} else
690ba55f2f5SFrançois Tigeot 			return 0xFF;
691ba55f2f5SFrançois Tigeot 	}
692ba55f2f5SFrançois Tigeot 
693ba55f2f5SFrançois Tigeot 	DRM_DEBUG_DRIVER("CMD: Abnormal bsd cmd length! 0x%08X\n", cmd_header);
694ba55f2f5SFrançois Tigeot 	return 0;
695ba55f2f5SFrançois Tigeot }
696ba55f2f5SFrançois Tigeot 
gen7_blt_get_cmd_length_mask(u32 cmd_header)697ba55f2f5SFrançois Tigeot static u32 gen7_blt_get_cmd_length_mask(u32 cmd_header)
698ba55f2f5SFrançois Tigeot {
699a85cb24fSFrançois Tigeot 	u32 client = cmd_header >> INSTR_CLIENT_SHIFT;
700ba55f2f5SFrançois Tigeot 
701ba55f2f5SFrançois Tigeot 	if (client == INSTR_MI_CLIENT)
702ba55f2f5SFrançois Tigeot 		return 0x3F;
703ba55f2f5SFrançois Tigeot 	else if (client == INSTR_BC_CLIENT)
704ba55f2f5SFrançois Tigeot 		return 0xFF;
705ba55f2f5SFrançois Tigeot 
706ba55f2f5SFrançois Tigeot 	DRM_DEBUG_DRIVER("CMD: Abnormal blt cmd length! 0x%08X\n", cmd_header);
707ba55f2f5SFrançois Tigeot 	return 0;
708ba55f2f5SFrançois Tigeot }
709ba55f2f5SFrançois Tigeot 
validate_cmds_sorted(const struct intel_engine_cs * engine,const struct drm_i915_cmd_table * cmd_tables,int cmd_table_count)71071f41f3eSFrançois Tigeot static bool validate_cmds_sorted(const struct intel_engine_cs *engine,
711ba55f2f5SFrançois Tigeot 				 const struct drm_i915_cmd_table *cmd_tables,
712ba55f2f5SFrançois Tigeot 				 int cmd_table_count)
713ba55f2f5SFrançois Tigeot {
714ba55f2f5SFrançois Tigeot 	int i;
715ba55f2f5SFrançois Tigeot 	bool ret = true;
716ba55f2f5SFrançois Tigeot 
717ba55f2f5SFrançois Tigeot 	if (!cmd_tables || cmd_table_count == 0)
718ba55f2f5SFrançois Tigeot 		return true;
719ba55f2f5SFrançois Tigeot 
720ba55f2f5SFrançois Tigeot 	for (i = 0; i < cmd_table_count; i++) {
721ba55f2f5SFrançois Tigeot 		const struct drm_i915_cmd_table *table = &cmd_tables[i];
722ba55f2f5SFrançois Tigeot 		u32 previous = 0;
723ba55f2f5SFrançois Tigeot 		int j;
724ba55f2f5SFrançois Tigeot 
725ba55f2f5SFrançois Tigeot 		for (j = 0; j < table->count; j++) {
726ba55f2f5SFrançois Tigeot 			const struct drm_i915_cmd_descriptor *desc =
727a05eeebfSFrançois Tigeot 				&table->table[j];
728ba55f2f5SFrançois Tigeot 			u32 curr = desc->cmd.value & desc->cmd.mask;
729ba55f2f5SFrançois Tigeot 
730ba55f2f5SFrançois Tigeot 			if (curr < previous) {
73171f41f3eSFrançois Tigeot 				DRM_ERROR("CMD: %s [%d] command table not sorted: "
73271f41f3eSFrançois Tigeot 					  "table=%d entry=%d cmd=0x%08X prev=0x%08X\n",
73371f41f3eSFrançois Tigeot 					  engine->name, engine->id,
73471f41f3eSFrançois Tigeot 					  i, j, curr, previous);
735ba55f2f5SFrançois Tigeot 				ret = false;
736ba55f2f5SFrançois Tigeot 			}
737ba55f2f5SFrançois Tigeot 
738ba55f2f5SFrançois Tigeot 			previous = curr;
739ba55f2f5SFrançois Tigeot 		}
740ba55f2f5SFrançois Tigeot 	}
741ba55f2f5SFrançois Tigeot 
742ba55f2f5SFrançois Tigeot 	return ret;
743ba55f2f5SFrançois Tigeot }
744ba55f2f5SFrançois Tigeot 
check_sorted(const struct intel_engine_cs * engine,const struct drm_i915_reg_descriptor * reg_table,int reg_count)74571f41f3eSFrançois Tigeot static bool check_sorted(const struct intel_engine_cs *engine,
74619c468b4SFrançois Tigeot 			 const struct drm_i915_reg_descriptor *reg_table,
74719c468b4SFrançois Tigeot 			 int reg_count)
748ba55f2f5SFrançois Tigeot {
749ba55f2f5SFrançois Tigeot 	int i;
750ba55f2f5SFrançois Tigeot 	u32 previous = 0;
751ba55f2f5SFrançois Tigeot 	bool ret = true;
752ba55f2f5SFrançois Tigeot 
753ba55f2f5SFrançois Tigeot 	for (i = 0; i < reg_count; i++) {
754aee94f86SFrançois Tigeot 		u32 curr = i915_mmio_reg_offset(reg_table[i].addr);
755ba55f2f5SFrançois Tigeot 
756ba55f2f5SFrançois Tigeot 		if (curr < previous) {
75771f41f3eSFrançois Tigeot 			DRM_ERROR("CMD: %s [%d] register table not sorted: "
75871f41f3eSFrançois Tigeot 				  "entry=%d reg=0x%08X prev=0x%08X\n",
75971f41f3eSFrançois Tigeot 				  engine->name, engine->id,
76071f41f3eSFrançois Tigeot 				  i, curr, previous);
761ba55f2f5SFrançois Tigeot 			ret = false;
762ba55f2f5SFrançois Tigeot 		}
763ba55f2f5SFrançois Tigeot 
764ba55f2f5SFrançois Tigeot 		previous = curr;
765ba55f2f5SFrançois Tigeot 	}
766ba55f2f5SFrançois Tigeot 
767ba55f2f5SFrançois Tigeot 	return ret;
768ba55f2f5SFrançois Tigeot }
769ba55f2f5SFrançois Tigeot 
validate_regs_sorted(struct intel_engine_cs * engine)7708621f407SFrançois Tigeot static bool validate_regs_sorted(struct intel_engine_cs *engine)
771ba55f2f5SFrançois Tigeot {
7728621f407SFrançois Tigeot 	int i;
7738621f407SFrançois Tigeot 	const struct drm_i915_reg_table *table;
7748621f407SFrançois Tigeot 
7758621f407SFrançois Tigeot 	for (i = 0; i < engine->reg_table_count; i++) {
7768621f407SFrançois Tigeot 		table = &engine->reg_tables[i];
77771f41f3eSFrançois Tigeot 		if (!check_sorted(engine, table->regs, table->num_regs))
7788621f407SFrançois Tigeot 			return false;
7798621f407SFrançois Tigeot 	}
7808621f407SFrançois Tigeot 
7818621f407SFrançois Tigeot 	return true;
782ba55f2f5SFrançois Tigeot }
783ba55f2f5SFrançois Tigeot 
784ba55f2f5SFrançois Tigeot struct cmd_node {
785ba55f2f5SFrançois Tigeot 	const struct drm_i915_cmd_descriptor *desc;
786ba55f2f5SFrançois Tigeot 	struct hlist_node node;
787ba55f2f5SFrançois Tigeot };
788ba55f2f5SFrançois Tigeot 
789ba55f2f5SFrançois Tigeot /*
790ba55f2f5SFrançois Tigeot  * Different command ranges have different numbers of bits for the opcode. For
791ba55f2f5SFrançois Tigeot  * example, MI commands use bits 31:23 while 3D commands use bits 31:16. The
792ba55f2f5SFrançois Tigeot  * problem is that, for example, MI commands use bits 22:16 for other fields
793ba55f2f5SFrançois Tigeot  * such as GGTT vs PPGTT bits. If we include those bits in the mask then when
794ba55f2f5SFrançois Tigeot  * we mask a command from a batch it could hash to the wrong bucket due to
795ba55f2f5SFrançois Tigeot  * non-opcode bits being set. But if we don't include those bits, some 3D
796ba55f2f5SFrançois Tigeot  * commands may hash to the same bucket due to not including opcode bits that
797ba55f2f5SFrançois Tigeot  * make the command unique. For now, we will risk hashing to the same bucket.
798ba55f2f5SFrançois Tigeot  */
cmd_header_key(u32 x)7991e12ee3bSFrançois Tigeot static inline u32 cmd_header_key(u32 x)
8001e12ee3bSFrançois Tigeot {
8011e12ee3bSFrançois Tigeot 	u32 shift;
8021e12ee3bSFrançois Tigeot 
8031e12ee3bSFrançois Tigeot 	switch (x >> INSTR_CLIENT_SHIFT) {
8041e12ee3bSFrançois Tigeot 	default:
8051e12ee3bSFrançois Tigeot 	case INSTR_MI_CLIENT:
8061e12ee3bSFrançois Tigeot 		shift = STD_MI_OPCODE_SHIFT;
8071e12ee3bSFrançois Tigeot 		break;
8081e12ee3bSFrançois Tigeot 	case INSTR_RC_CLIENT:
8091e12ee3bSFrançois Tigeot 		shift = STD_3D_OPCODE_SHIFT;
8101e12ee3bSFrançois Tigeot 		break;
8111e12ee3bSFrançois Tigeot 	case INSTR_BC_CLIENT:
8121e12ee3bSFrançois Tigeot 		shift = STD_2D_OPCODE_SHIFT;
8131e12ee3bSFrançois Tigeot 		break;
8141e12ee3bSFrançois Tigeot 	}
8151e12ee3bSFrançois Tigeot 
8161e12ee3bSFrançois Tigeot 	return x >> shift;
8171e12ee3bSFrançois Tigeot }
818ba55f2f5SFrançois Tigeot 
init_hash_table(struct intel_engine_cs * engine,const struct drm_i915_cmd_table * cmd_tables,int cmd_table_count)8198621f407SFrançois Tigeot static int init_hash_table(struct intel_engine_cs *engine,
820ba55f2f5SFrançois Tigeot 			   const struct drm_i915_cmd_table *cmd_tables,
821ba55f2f5SFrançois Tigeot 			   int cmd_table_count)
822ba55f2f5SFrançois Tigeot {
823ba55f2f5SFrançois Tigeot 	int i, j;
824ba55f2f5SFrançois Tigeot 
8258621f407SFrançois Tigeot 	hash_init(engine->cmd_hash);
826ba55f2f5SFrançois Tigeot 
827ba55f2f5SFrançois Tigeot 	for (i = 0; i < cmd_table_count; i++) {
828ba55f2f5SFrançois Tigeot 		const struct drm_i915_cmd_table *table = &cmd_tables[i];
829ba55f2f5SFrançois Tigeot 
830ba55f2f5SFrançois Tigeot 		for (j = 0; j < table->count; j++) {
831ba55f2f5SFrançois Tigeot 			const struct drm_i915_cmd_descriptor *desc =
832ba55f2f5SFrançois Tigeot 				&table->table[j];
833ba55f2f5SFrançois Tigeot 			struct cmd_node *desc_node =
8341487f786SFrançois Tigeot 				kmalloc(sizeof(*desc_node), M_DRM, GFP_KERNEL);
835ba55f2f5SFrançois Tigeot 
836ba55f2f5SFrançois Tigeot 			if (!desc_node)
837ba55f2f5SFrançois Tigeot 				return -ENOMEM;
838ba55f2f5SFrançois Tigeot 
839ba55f2f5SFrançois Tigeot 			desc_node->desc = desc;
8408621f407SFrançois Tigeot 			hash_add(engine->cmd_hash, &desc_node->node,
8411e12ee3bSFrançois Tigeot 				 cmd_header_key(desc->cmd.value));
842ba55f2f5SFrançois Tigeot 		}
843ba55f2f5SFrançois Tigeot 	}
844ba55f2f5SFrançois Tigeot 
845ba55f2f5SFrançois Tigeot 	return 0;
846ba55f2f5SFrançois Tigeot }
847ba55f2f5SFrançois Tigeot 
fini_hash_table(struct intel_engine_cs * engine)8488621f407SFrançois Tigeot static void fini_hash_table(struct intel_engine_cs *engine)
849ba55f2f5SFrançois Tigeot {
850ba55f2f5SFrançois Tigeot 	struct hlist_node *tmp;
851ba55f2f5SFrançois Tigeot 	struct cmd_node *desc_node;
852ba55f2f5SFrançois Tigeot 	int i;
853ba55f2f5SFrançois Tigeot 
8548621f407SFrançois Tigeot 	hash_for_each_safe(engine->cmd_hash, i, tmp, desc_node, node) {
855ba55f2f5SFrançois Tigeot 		hash_del(&desc_node->node);
856ba55f2f5SFrançois Tigeot 		kfree(desc_node);
857ba55f2f5SFrançois Tigeot 	}
858ba55f2f5SFrançois Tigeot }
859ba55f2f5SFrançois Tigeot 
860ba55f2f5SFrançois Tigeot /**
86171f41f3eSFrançois Tigeot  * intel_engine_init_cmd_parser() - set cmd parser related fields for an engine
8621487f786SFrançois Tigeot  * @engine: the engine to initialize
863ba55f2f5SFrançois Tigeot  *
864ba55f2f5SFrançois Tigeot  * Optionally initializes fields related to batch buffer command parsing in the
865ba55f2f5SFrançois Tigeot  * struct intel_engine_cs based on whether the platform requires software
866ba55f2f5SFrançois Tigeot  * command parsing.
867ba55f2f5SFrançois Tigeot  */
intel_engine_init_cmd_parser(struct intel_engine_cs * engine)8681e12ee3bSFrançois Tigeot void intel_engine_init_cmd_parser(struct intel_engine_cs *engine)
869ba55f2f5SFrançois Tigeot {
870ba55f2f5SFrançois Tigeot 	const struct drm_i915_cmd_table *cmd_tables;
871ba55f2f5SFrançois Tigeot 	int cmd_table_count;
872ba55f2f5SFrançois Tigeot 	int ret;
873ba55f2f5SFrançois Tigeot 
8741487f786SFrançois Tigeot 	if (!IS_GEN7(engine->i915))
8751e12ee3bSFrançois Tigeot 		return;
876ba55f2f5SFrançois Tigeot 
8778621f407SFrançois Tigeot 	switch (engine->id) {
878ba55f2f5SFrançois Tigeot 	case RCS:
8791487f786SFrançois Tigeot 		if (IS_HASWELL(engine->i915)) {
880ba55f2f5SFrançois Tigeot 			cmd_tables = hsw_render_ring_cmds;
881ba55f2f5SFrançois Tigeot 			cmd_table_count =
882ba55f2f5SFrançois Tigeot 				ARRAY_SIZE(hsw_render_ring_cmds);
883ba55f2f5SFrançois Tigeot 		} else {
884ba55f2f5SFrançois Tigeot 			cmd_tables = gen7_render_cmds;
885ba55f2f5SFrançois Tigeot 			cmd_table_count = ARRAY_SIZE(gen7_render_cmds);
886ba55f2f5SFrançois Tigeot 		}
887ba55f2f5SFrançois Tigeot 
8881487f786SFrançois Tigeot 		if (IS_HASWELL(engine->i915)) {
8898621f407SFrançois Tigeot 			engine->reg_tables = hsw_render_reg_tables;
8908621f407SFrançois Tigeot 			engine->reg_table_count = ARRAY_SIZE(hsw_render_reg_tables);
891ba55f2f5SFrançois Tigeot 		} else {
8928621f407SFrançois Tigeot 			engine->reg_tables = ivb_render_reg_tables;
8938621f407SFrançois Tigeot 			engine->reg_table_count = ARRAY_SIZE(ivb_render_reg_tables);
894ba55f2f5SFrançois Tigeot 		}
895ba55f2f5SFrançois Tigeot 
8968621f407SFrançois Tigeot 		engine->get_cmd_length_mask = gen7_render_get_cmd_length_mask;
897ba55f2f5SFrançois Tigeot 		break;
898ba55f2f5SFrançois Tigeot 	case VCS:
899ba55f2f5SFrançois Tigeot 		cmd_tables = gen7_video_cmds;
900ba55f2f5SFrançois Tigeot 		cmd_table_count = ARRAY_SIZE(gen7_video_cmds);
9018621f407SFrançois Tigeot 		engine->get_cmd_length_mask = gen7_bsd_get_cmd_length_mask;
902ba55f2f5SFrançois Tigeot 		break;
903ba55f2f5SFrançois Tigeot 	case BCS:
9041487f786SFrançois Tigeot 		if (IS_HASWELL(engine->i915)) {
905ba55f2f5SFrançois Tigeot 			cmd_tables = hsw_blt_ring_cmds;
906ba55f2f5SFrançois Tigeot 			cmd_table_count = ARRAY_SIZE(hsw_blt_ring_cmds);
907ba55f2f5SFrançois Tigeot 		} else {
908ba55f2f5SFrançois Tigeot 			cmd_tables = gen7_blt_cmds;
909ba55f2f5SFrançois Tigeot 			cmd_table_count = ARRAY_SIZE(gen7_blt_cmds);
910ba55f2f5SFrançois Tigeot 		}
911ba55f2f5SFrançois Tigeot 
9121487f786SFrançois Tigeot 		if (IS_HASWELL(engine->i915)) {
9138621f407SFrançois Tigeot 			engine->reg_tables = hsw_blt_reg_tables;
9148621f407SFrançois Tigeot 			engine->reg_table_count = ARRAY_SIZE(hsw_blt_reg_tables);
915ba55f2f5SFrançois Tigeot 		} else {
9168621f407SFrançois Tigeot 			engine->reg_tables = ivb_blt_reg_tables;
9178621f407SFrançois Tigeot 			engine->reg_table_count = ARRAY_SIZE(ivb_blt_reg_tables);
918ba55f2f5SFrançois Tigeot 		}
919ba55f2f5SFrançois Tigeot 
9208621f407SFrançois Tigeot 		engine->get_cmd_length_mask = gen7_blt_get_cmd_length_mask;
921ba55f2f5SFrançois Tigeot 		break;
922ba55f2f5SFrançois Tigeot 	case VECS:
923ba55f2f5SFrançois Tigeot 		cmd_tables = hsw_vebox_cmds;
924ba55f2f5SFrançois Tigeot 		cmd_table_count = ARRAY_SIZE(hsw_vebox_cmds);
925ba55f2f5SFrançois Tigeot 		/* VECS can use the same length_mask function as VCS */
9268621f407SFrançois Tigeot 		engine->get_cmd_length_mask = gen7_bsd_get_cmd_length_mask;
927ba55f2f5SFrançois Tigeot 		break;
928ba55f2f5SFrançois Tigeot 	default:
92971f41f3eSFrançois Tigeot 		MISSING_CASE(engine->id);
9301e12ee3bSFrançois Tigeot 		return;
931ba55f2f5SFrançois Tigeot 	}
932ba55f2f5SFrançois Tigeot 
9331e12ee3bSFrançois Tigeot 	if (!validate_cmds_sorted(engine, cmd_tables, cmd_table_count)) {
9341e12ee3bSFrançois Tigeot 		DRM_ERROR("%s: command descriptions are not sorted\n",
9351e12ee3bSFrançois Tigeot 			  engine->name);
9361e12ee3bSFrançois Tigeot 		return;
9371e12ee3bSFrançois Tigeot 	}
9381e12ee3bSFrançois Tigeot 	if (!validate_regs_sorted(engine)) {
9391e12ee3bSFrançois Tigeot 		DRM_ERROR("%s: registers are not sorted\n", engine->name);
9401e12ee3bSFrançois Tigeot 		return;
9411e12ee3bSFrançois Tigeot 	}
9422c9916cdSFrançois Tigeot 
9438621f407SFrançois Tigeot 	ret = init_hash_table(engine, cmd_tables, cmd_table_count);
944ba55f2f5SFrançois Tigeot 	if (ret) {
9451e12ee3bSFrançois Tigeot 		DRM_ERROR("%s: initialised failed!\n", engine->name);
9468621f407SFrançois Tigeot 		fini_hash_table(engine);
9471e12ee3bSFrançois Tigeot 		return;
948ba55f2f5SFrançois Tigeot 	}
949ba55f2f5SFrançois Tigeot 
9508621f407SFrançois Tigeot 	engine->needs_cmd_parser = true;
951ba55f2f5SFrançois Tigeot }
952ba55f2f5SFrançois Tigeot 
953ba55f2f5SFrançois Tigeot /**
95471f41f3eSFrançois Tigeot  * intel_engine_cleanup_cmd_parser() - clean up cmd parser related fields
9551487f786SFrançois Tigeot  * @engine: the engine to clean up
956ba55f2f5SFrançois Tigeot  *
957ba55f2f5SFrançois Tigeot  * Releases any resources related to command parsing that may have been
95871f41f3eSFrançois Tigeot  * initialized for the specified engine.
959ba55f2f5SFrançois Tigeot  */
intel_engine_cleanup_cmd_parser(struct intel_engine_cs * engine)96071f41f3eSFrançois Tigeot void intel_engine_cleanup_cmd_parser(struct intel_engine_cs *engine)
961ba55f2f5SFrançois Tigeot {
9628621f407SFrançois Tigeot 	if (!engine->needs_cmd_parser)
963ba55f2f5SFrançois Tigeot 		return;
964ba55f2f5SFrançois Tigeot 
9658621f407SFrançois Tigeot 	fini_hash_table(engine);
966ba55f2f5SFrançois Tigeot }
967ba55f2f5SFrançois Tigeot 
968ba55f2f5SFrançois Tigeot static const struct drm_i915_cmd_descriptor*
find_cmd_in_table(struct intel_engine_cs * engine,u32 cmd_header)9698621f407SFrançois Tigeot find_cmd_in_table(struct intel_engine_cs *engine,
970ba55f2f5SFrançois Tigeot 		  u32 cmd_header)
971ba55f2f5SFrançois Tigeot {
972ba55f2f5SFrançois Tigeot 	struct cmd_node *desc_node;
973ba55f2f5SFrançois Tigeot 
9748621f407SFrançois Tigeot 	hash_for_each_possible(engine->cmd_hash, desc_node, node,
9751e12ee3bSFrançois Tigeot 			       cmd_header_key(cmd_header)) {
976ba55f2f5SFrançois Tigeot 		const struct drm_i915_cmd_descriptor *desc = desc_node->desc;
9771e12ee3bSFrançois Tigeot 		if (((cmd_header ^ desc->cmd.value) & desc->cmd.mask) == 0)
978ba55f2f5SFrançois Tigeot 			return desc;
979ba55f2f5SFrançois Tigeot 	}
980ba55f2f5SFrançois Tigeot 
981ba55f2f5SFrançois Tigeot 	return NULL;
982ba55f2f5SFrançois Tigeot }
983ba55f2f5SFrançois Tigeot 
984ba55f2f5SFrançois Tigeot /*
985ba55f2f5SFrançois Tigeot  * Returns a pointer to a descriptor for the command specified by cmd_header.
986ba55f2f5SFrançois Tigeot  *
987ba55f2f5SFrançois Tigeot  * The caller must supply space for a default descriptor via the default_desc
98871f41f3eSFrançois Tigeot  * parameter. If no descriptor for the specified command exists in the engine's
989ba55f2f5SFrançois Tigeot  * command parser tables, this function fills in default_desc based on the
99071f41f3eSFrançois Tigeot  * engine's default length encoding and returns default_desc.
991ba55f2f5SFrançois Tigeot  */
992ba55f2f5SFrançois Tigeot static const struct drm_i915_cmd_descriptor*
find_cmd(struct intel_engine_cs * engine,u32 cmd_header,const struct drm_i915_cmd_descriptor * desc,struct drm_i915_cmd_descriptor * default_desc)9938621f407SFrançois Tigeot find_cmd(struct intel_engine_cs *engine,
994ba55f2f5SFrançois Tigeot 	 u32 cmd_header,
9951e12ee3bSFrançois Tigeot 	 const struct drm_i915_cmd_descriptor *desc,
996ba55f2f5SFrançois Tigeot 	 struct drm_i915_cmd_descriptor *default_desc)
997ba55f2f5SFrançois Tigeot {
998ba55f2f5SFrançois Tigeot 	u32 mask;
999ba55f2f5SFrançois Tigeot 
10001e12ee3bSFrançois Tigeot 	if (((cmd_header ^ desc->cmd.value) & desc->cmd.mask) == 0)
10011e12ee3bSFrançois Tigeot 		return desc;
10021e12ee3bSFrançois Tigeot 
10038621f407SFrançois Tigeot 	desc = find_cmd_in_table(engine, cmd_header);
1004ba55f2f5SFrançois Tigeot 	if (desc)
1005ba55f2f5SFrançois Tigeot 		return desc;
1006ba55f2f5SFrançois Tigeot 
10078621f407SFrançois Tigeot 	mask = engine->get_cmd_length_mask(cmd_header);
1008ba55f2f5SFrançois Tigeot 	if (!mask)
1009ba55f2f5SFrançois Tigeot 		return NULL;
1010ba55f2f5SFrançois Tigeot 
10111e12ee3bSFrançois Tigeot 	default_desc->cmd.value = cmd_header;
10121e12ee3bSFrançois Tigeot 	default_desc->cmd.mask = ~0u << MIN_OPCODE_SHIFT;
1013ba55f2f5SFrançois Tigeot 	default_desc->length.mask = mask;
10141e12ee3bSFrançois Tigeot 	default_desc->flags = CMD_DESC_SKIP;
1015ba55f2f5SFrançois Tigeot 	return default_desc;
1016ba55f2f5SFrançois Tigeot }
1017ba55f2f5SFrançois Tigeot 
101819c468b4SFrançois Tigeot static const struct drm_i915_reg_descriptor *
__find_reg(const struct drm_i915_reg_descriptor * table,int count,u32 addr)10191e12ee3bSFrançois Tigeot __find_reg(const struct drm_i915_reg_descriptor *table, int count, u32 addr)
1020ba55f2f5SFrançois Tigeot {
10211e12ee3bSFrançois Tigeot 	int start = 0, end = count;
10221e12ee3bSFrançois Tigeot 	while (start < end) {
10231e12ee3bSFrançois Tigeot 		int mid = start + (end - start) / 2;
10241e12ee3bSFrançois Tigeot 		int ret = addr - i915_mmio_reg_offset(table[mid].addr);
10251e12ee3bSFrançois Tigeot 		if (ret < 0)
10261e12ee3bSFrançois Tigeot 			end = mid;
10271e12ee3bSFrançois Tigeot 		else if (ret > 0)
10281e12ee3bSFrançois Tigeot 			start = mid + 1;
10291e12ee3bSFrançois Tigeot 		else
10301e12ee3bSFrançois Tigeot 			return &table[mid];
1031ba55f2f5SFrançois Tigeot 	}
10328621f407SFrançois Tigeot 	return NULL;
10338621f407SFrançois Tigeot }
10348621f407SFrançois Tigeot 
10358621f407SFrançois Tigeot static const struct drm_i915_reg_descriptor *
find_reg(const struct intel_engine_cs * engine,bool is_master,u32 addr)10361e12ee3bSFrançois Tigeot find_reg(const struct intel_engine_cs *engine, bool is_master, u32 addr)
10378621f407SFrançois Tigeot {
10381e12ee3bSFrançois Tigeot 	const struct drm_i915_reg_table *table = engine->reg_tables;
10391e12ee3bSFrançois Tigeot 	int count = engine->reg_table_count;
10401e12ee3bSFrançois Tigeot 
10411e12ee3bSFrançois Tigeot 	do {
10421e12ee3bSFrançois Tigeot 		if (!table->master || is_master) {
10438621f407SFrançois Tigeot 			const struct drm_i915_reg_descriptor *reg;
10448621f407SFrançois Tigeot 
10451e12ee3bSFrançois Tigeot 			reg = __find_reg(table->regs, table->num_regs, addr);
10468621f407SFrançois Tigeot 			if (reg != NULL)
10478621f407SFrançois Tigeot 				return reg;
10488621f407SFrançois Tigeot 		}
10491e12ee3bSFrançois Tigeot 	} while (table++, --count);
1050ba55f2f5SFrançois Tigeot 
105119c468b4SFrançois Tigeot 	return NULL;
1052ba55f2f5SFrançois Tigeot }
1053ba55f2f5SFrançois Tigeot 
10541e12ee3bSFrançois Tigeot /* Returns a vmap'd pointer to dst_obj, which the caller must unmap */
copy_batch(struct drm_i915_gem_object * dst_obj,struct drm_i915_gem_object * src_obj,u32 batch_start_offset,u32 batch_len,bool * needs_clflush_after)10551e12ee3bSFrançois Tigeot static u32 *copy_batch(struct drm_i915_gem_object *dst_obj,
10562c9916cdSFrançois Tigeot 		       struct drm_i915_gem_object *src_obj,
10572c9916cdSFrançois Tigeot 		       u32 batch_start_offset,
10581e12ee3bSFrançois Tigeot 		       u32 batch_len,
10591e12ee3bSFrançois Tigeot 		       bool *needs_clflush_after)
10602c9916cdSFrançois Tigeot {
10611e12ee3bSFrançois Tigeot 	unsigned int src_needs_clflush;
10621e12ee3bSFrançois Tigeot 	unsigned int dst_needs_clflush;
10631e12ee3bSFrançois Tigeot 	void *dst, *src;
1064477eb7f9SFrançois Tigeot 	int ret;
10652c9916cdSFrançois Tigeot 
10661e12ee3bSFrançois Tigeot 	ret = i915_gem_obj_prepare_shmem_read(src_obj, &src_needs_clflush);
10671e12ee3bSFrançois Tigeot 	if (ret)
10682c9916cdSFrançois Tigeot 		return ERR_PTR(ret);
10692c9916cdSFrançois Tigeot 
10701e12ee3bSFrançois Tigeot 	ret = i915_gem_obj_prepare_shmem_write(dst_obj, &dst_needs_clflush);
10711e12ee3bSFrançois Tigeot 	if (ret) {
10721e12ee3bSFrançois Tigeot 		dst = ERR_PTR(ret);
10732c9916cdSFrançois Tigeot 		goto unpin_src;
10742c9916cdSFrançois Tigeot 	}
10752c9916cdSFrançois Tigeot 
1076*3f2dd94aSFrançois Tigeot 	dst = i915_gem_object_pin_map(dst_obj, I915_MAP_FORCE_WB);
10771e12ee3bSFrançois Tigeot 	if (IS_ERR(dst))
10781e12ee3bSFrançois Tigeot 		goto unpin_dst;
10791e12ee3bSFrançois Tigeot 
10801e12ee3bSFrançois Tigeot 	src = ERR_PTR(-ENODEV);
10811e12ee3bSFrançois Tigeot 	if (src_needs_clflush &&
1082a85cb24fSFrançois Tigeot 	    i915_can_memcpy_from_wc(NULL, batch_start_offset, 0)) {
10831e12ee3bSFrançois Tigeot 		src = i915_gem_object_pin_map(src_obj, I915_MAP_WC);
10841e12ee3bSFrançois Tigeot 		if (!IS_ERR(src)) {
10851e12ee3bSFrançois Tigeot 			i915_memcpy_from_wc(dst,
10861e12ee3bSFrançois Tigeot 					    src + batch_start_offset,
10871e12ee3bSFrançois Tigeot 					    ALIGN(batch_len, 16));
10881e12ee3bSFrançois Tigeot 			i915_gem_object_unpin_map(src_obj);
10891e12ee3bSFrançois Tigeot 		}
10901e12ee3bSFrançois Tigeot 	}
10911e12ee3bSFrançois Tigeot 	if (IS_ERR(src)) {
10921e12ee3bSFrançois Tigeot 		void *ptr;
10931e12ee3bSFrançois Tigeot 		int offset, n;
10941e12ee3bSFrançois Tigeot 
10951e12ee3bSFrançois Tigeot 		offset = offset_in_page(batch_start_offset);
10961e12ee3bSFrançois Tigeot 
10971e12ee3bSFrançois Tigeot 		/* We can avoid clflushing partial cachelines before the write
10981e12ee3bSFrançois Tigeot 		 * if we only every write full cache-lines. Since we know that
10991e12ee3bSFrançois Tigeot 		 * both the source and destination are in multiples of
11001e12ee3bSFrançois Tigeot 		 * PAGE_SIZE, we can simply round up to the next cacheline.
11011e12ee3bSFrançois Tigeot 		 * We don't care about copying too much here as we only
11021e12ee3bSFrançois Tigeot 		 * validate up to the end of the batch.
11031e12ee3bSFrançois Tigeot 		 */
11041e12ee3bSFrançois Tigeot 		if (dst_needs_clflush & CLFLUSH_BEFORE)
11051e12ee3bSFrançois Tigeot 			batch_len = roundup(batch_len,
11061e12ee3bSFrançois Tigeot 					    boot_cpu_data.x86_clflush_size);
11071e12ee3bSFrançois Tigeot 
11081e12ee3bSFrançois Tigeot 		ptr = dst;
11091e12ee3bSFrançois Tigeot 		for (n = batch_start_offset >> PAGE_SHIFT; batch_len; n++) {
11101e12ee3bSFrançois Tigeot 			int len = min_t(int, batch_len, PAGE_SIZE - offset);
11111e12ee3bSFrançois Tigeot 
11121e12ee3bSFrançois Tigeot 			src = kmap_atomic(i915_gem_object_get_page(src_obj, n));
11131e12ee3bSFrançois Tigeot 			if (src_needs_clflush)
11141e12ee3bSFrançois Tigeot 				drm_clflush_virt_range(src + offset, len);
11151e12ee3bSFrançois Tigeot 			memcpy(ptr, src + offset, len);
11161e12ee3bSFrançois Tigeot 			kunmap_atomic(src);
11171e12ee3bSFrançois Tigeot 
11181e12ee3bSFrançois Tigeot 			ptr += len;
11191e12ee3bSFrançois Tigeot 			batch_len -= len;
11201e12ee3bSFrançois Tigeot 			offset = 0;
11211e12ee3bSFrançois Tigeot 		}
11222c9916cdSFrançois Tigeot 	}
11232c9916cdSFrançois Tigeot 
11241e12ee3bSFrançois Tigeot 	/* dst_obj is returned with vmap pinned */
11251e12ee3bSFrançois Tigeot 	*needs_clflush_after = dst_needs_clflush & CLFLUSH_AFTER;
11262c9916cdSFrançois Tigeot 
11271e12ee3bSFrançois Tigeot unpin_dst:
11281e12ee3bSFrançois Tigeot 	i915_gem_obj_finish_shmem_access(dst_obj);
11292c9916cdSFrançois Tigeot unpin_src:
11301e12ee3bSFrançois Tigeot 	i915_gem_obj_finish_shmem_access(src_obj);
11311e12ee3bSFrançois Tigeot 	return dst;
11322c9916cdSFrançois Tigeot }
11332c9916cdSFrançois Tigeot 
check_cmd(const struct intel_engine_cs * engine,const struct drm_i915_cmd_descriptor * desc,const u32 * cmd,u32 length,const bool is_master)11348621f407SFrançois Tigeot static bool check_cmd(const struct intel_engine_cs *engine,
1135ba55f2f5SFrançois Tigeot 		      const struct drm_i915_cmd_descriptor *desc,
113619c468b4SFrançois Tigeot 		      const u32 *cmd, u32 length,
1137a85cb24fSFrançois Tigeot 		      const bool is_master)
1138ba55f2f5SFrançois Tigeot {
11391e12ee3bSFrançois Tigeot 	if (desc->flags & CMD_DESC_SKIP)
11401e12ee3bSFrançois Tigeot 		return true;
11411e12ee3bSFrançois Tigeot 
1142ba55f2f5SFrançois Tigeot 	if (desc->flags & CMD_DESC_REJECT) {
1143ba55f2f5SFrançois Tigeot 		DRM_DEBUG_DRIVER("CMD: Rejected command: 0x%08X\n", *cmd);
1144ba55f2f5SFrançois Tigeot 		return false;
1145ba55f2f5SFrançois Tigeot 	}
1146ba55f2f5SFrançois Tigeot 
1147ba55f2f5SFrançois Tigeot 	if ((desc->flags & CMD_DESC_MASTER) && !is_master) {
1148ba55f2f5SFrançois Tigeot 		DRM_DEBUG_DRIVER("CMD: Rejected master-only command: 0x%08X\n",
1149ba55f2f5SFrançois Tigeot 				 *cmd);
1150ba55f2f5SFrançois Tigeot 		return false;
1151ba55f2f5SFrançois Tigeot 	}
1152ba55f2f5SFrançois Tigeot 
1153ba55f2f5SFrançois Tigeot 	if (desc->flags & CMD_DESC_REGISTER) {
115419c468b4SFrançois Tigeot 		/*
115519c468b4SFrançois Tigeot 		 * Get the distance between individual register offset
115619c468b4SFrançois Tigeot 		 * fields if the command can perform more than one
115719c468b4SFrançois Tigeot 		 * access at a time.
115819c468b4SFrançois Tigeot 		 */
115919c468b4SFrançois Tigeot 		const u32 step = desc->reg.step ? desc->reg.step : length;
116019c468b4SFrançois Tigeot 		u32 offset;
116119c468b4SFrançois Tigeot 
116219c468b4SFrançois Tigeot 		for (offset = desc->reg.offset; offset < length;
116319c468b4SFrançois Tigeot 		     offset += step) {
116419c468b4SFrançois Tigeot 			const u32 reg_addr = cmd[offset] & desc->reg.mask;
116519c468b4SFrançois Tigeot 			const struct drm_i915_reg_descriptor *reg =
11661e12ee3bSFrançois Tigeot 				find_reg(engine, is_master, reg_addr);
116719c468b4SFrançois Tigeot 
116819c468b4SFrançois Tigeot 			if (!reg) {
1169*3f2dd94aSFrançois Tigeot 				DRM_DEBUG_DRIVER("CMD: Rejected register 0x%08X in command: 0x%08X (%s)\n",
1170*3f2dd94aSFrançois Tigeot 						 reg_addr, *cmd, engine->name);
117119c468b4SFrançois Tigeot 				return false;
117219c468b4SFrançois Tigeot 			}
1173ba55f2f5SFrançois Tigeot 
1174ba55f2f5SFrançois Tigeot 			/*
117519c468b4SFrançois Tigeot 			 * Check the value written to the register against the
117619c468b4SFrançois Tigeot 			 * allowed mask/value pair given in the whitelist entry.
117719c468b4SFrançois Tigeot 			 */
117819c468b4SFrançois Tigeot 			if (reg->mask) {
1179352ff8bdSFrançois Tigeot 				if (desc->cmd.value == MI_LOAD_REGISTER_MEM) {
118019c468b4SFrançois Tigeot 					DRM_DEBUG_DRIVER("CMD: Rejected LRM to masked register 0x%08X\n",
118119c468b4SFrançois Tigeot 							 reg_addr);
1182ba55f2f5SFrançois Tigeot 					return false;
1183ba55f2f5SFrançois Tigeot 				}
118419c468b4SFrançois Tigeot 
11851487f786SFrançois Tigeot 				if (desc->cmd.value == MI_LOAD_REGISTER_REG) {
11861487f786SFrançois Tigeot 					DRM_DEBUG_DRIVER("CMD: Rejected LRR to masked register 0x%08X\n",
11871487f786SFrançois Tigeot 							 reg_addr);
11881487f786SFrançois Tigeot 					return false;
11891487f786SFrançois Tigeot 				}
11901487f786SFrançois Tigeot 
119119c468b4SFrançois Tigeot 				if (desc->cmd.value == MI_LOAD_REGISTER_IMM(1) &&
119219c468b4SFrançois Tigeot 				    (offset + 2 > length ||
119319c468b4SFrançois Tigeot 				     (cmd[offset + 1] & reg->mask) != reg->value)) {
119419c468b4SFrançois Tigeot 					DRM_DEBUG_DRIVER("CMD: Rejected LRI to masked register 0x%08X\n",
119519c468b4SFrançois Tigeot 							 reg_addr);
119619c468b4SFrançois Tigeot 					return false;
119719c468b4SFrançois Tigeot 				}
119819c468b4SFrançois Tigeot 			}
1199ba55f2f5SFrançois Tigeot 		}
1200ba55f2f5SFrançois Tigeot 	}
1201ba55f2f5SFrançois Tigeot 
1202ba55f2f5SFrançois Tigeot 	if (desc->flags & CMD_DESC_BITMASK) {
1203ba55f2f5SFrançois Tigeot 		int i;
1204ba55f2f5SFrançois Tigeot 
1205ba55f2f5SFrançois Tigeot 		for (i = 0; i < MAX_CMD_DESC_BITMASKS; i++) {
1206ba55f2f5SFrançois Tigeot 			u32 dword;
1207ba55f2f5SFrançois Tigeot 
1208ba55f2f5SFrançois Tigeot 			if (desc->bits[i].mask == 0)
1209ba55f2f5SFrançois Tigeot 				break;
1210ba55f2f5SFrançois Tigeot 
1211ba55f2f5SFrançois Tigeot 			if (desc->bits[i].condition_mask != 0) {
1212ba55f2f5SFrançois Tigeot 				u32 offset =
1213ba55f2f5SFrançois Tigeot 					desc->bits[i].condition_offset;
1214ba55f2f5SFrançois Tigeot 				u32 condition = cmd[offset] &
1215ba55f2f5SFrançois Tigeot 					desc->bits[i].condition_mask;
1216ba55f2f5SFrançois Tigeot 
1217ba55f2f5SFrançois Tigeot 				if (condition == 0)
1218ba55f2f5SFrançois Tigeot 					continue;
1219ba55f2f5SFrançois Tigeot 			}
1220ba55f2f5SFrançois Tigeot 
1221ba55f2f5SFrançois Tigeot 			dword = cmd[desc->bits[i].offset] &
1222ba55f2f5SFrançois Tigeot 				desc->bits[i].mask;
1223ba55f2f5SFrançois Tigeot 
1224ba55f2f5SFrançois Tigeot 			if (dword != desc->bits[i].expected) {
1225*3f2dd94aSFrançois Tigeot 				DRM_DEBUG_DRIVER("CMD: Rejected command 0x%08X for bitmask 0x%08X (exp=0x%08X act=0x%08X) (%s)\n",
1226ba55f2f5SFrançois Tigeot 						 *cmd,
1227ba55f2f5SFrançois Tigeot 						 desc->bits[i].mask,
1228ba55f2f5SFrançois Tigeot 						 desc->bits[i].expected,
1229*3f2dd94aSFrançois Tigeot 						 dword, engine->name);
1230ba55f2f5SFrançois Tigeot 				return false;
1231ba55f2f5SFrançois Tigeot 			}
1232ba55f2f5SFrançois Tigeot 		}
1233ba55f2f5SFrançois Tigeot 	}
1234ba55f2f5SFrançois Tigeot 
1235ba55f2f5SFrançois Tigeot 	return true;
1236ba55f2f5SFrançois Tigeot }
1237ba55f2f5SFrançois Tigeot 
1238ba55f2f5SFrançois Tigeot #define LENGTH_BIAS 2
1239ba55f2f5SFrançois Tigeot 
1240ba55f2f5SFrançois Tigeot /**
1241ba55f2f5SFrançois Tigeot  * i915_parse_cmds() - parse a submitted batch buffer for privilege violations
12421487f786SFrançois Tigeot  * @engine: the engine on which the batch is to execute
1243ba55f2f5SFrançois Tigeot  * @batch_obj: the batch buffer in question
12442c9916cdSFrançois Tigeot  * @shadow_batch_obj: copy of the batch buffer in question
1245ba55f2f5SFrançois Tigeot  * @batch_start_offset: byte offset in the batch at which execution starts
12462c9916cdSFrançois Tigeot  * @batch_len: length of the commands in batch_obj
1247ba55f2f5SFrançois Tigeot  * @is_master: is the submitting process the drm master?
1248ba55f2f5SFrançois Tigeot  *
1249ba55f2f5SFrançois Tigeot  * Parses the specified batch buffer looking for privilege violations as
1250ba55f2f5SFrançois Tigeot  * described in the overview.
1251ba55f2f5SFrançois Tigeot  *
12522c9916cdSFrançois Tigeot  * Return: non-zero if the parser finds violations or otherwise fails; -EACCES
12532c9916cdSFrançois Tigeot  * if the batch appears legal but should use hardware parsing
1254ba55f2f5SFrançois Tigeot  */
intel_engine_cmd_parser(struct intel_engine_cs * engine,struct drm_i915_gem_object * batch_obj,struct drm_i915_gem_object * shadow_batch_obj,u32 batch_start_offset,u32 batch_len,bool is_master)125571f41f3eSFrançois Tigeot int intel_engine_cmd_parser(struct intel_engine_cs *engine,
1256ba55f2f5SFrançois Tigeot 			    struct drm_i915_gem_object *batch_obj,
12572c9916cdSFrançois Tigeot 			    struct drm_i915_gem_object *shadow_batch_obj,
1258ba55f2f5SFrançois Tigeot 			    u32 batch_start_offset,
12592c9916cdSFrançois Tigeot 			    u32 batch_len,
1260ba55f2f5SFrançois Tigeot 			    bool is_master)
1261ba55f2f5SFrançois Tigeot {
12621e12ee3bSFrançois Tigeot 	u32 *cmd, *batch_end;
12631e12ee3bSFrançois Tigeot 	struct drm_i915_cmd_descriptor default_desc = noop_desc;
12641e12ee3bSFrançois Tigeot 	const struct drm_i915_cmd_descriptor *desc = &default_desc;
12651e12ee3bSFrançois Tigeot 	bool needs_clflush_after = false;
1266477eb7f9SFrançois Tigeot 	int ret = 0;
1267ba55f2f5SFrançois Tigeot 
12681e12ee3bSFrançois Tigeot 	cmd = copy_batch(shadow_batch_obj, batch_obj,
12691e12ee3bSFrançois Tigeot 			 batch_start_offset, batch_len,
12701e12ee3bSFrançois Tigeot 			 &needs_clflush_after);
12711e12ee3bSFrançois Tigeot 	if (IS_ERR(cmd)) {
12722c9916cdSFrançois Tigeot 		DRM_DEBUG_DRIVER("CMD: Failed to copy batch\n");
12731e12ee3bSFrançois Tigeot 		return PTR_ERR(cmd);
1274ba55f2f5SFrançois Tigeot 	}
1275ba55f2f5SFrançois Tigeot 
12762c9916cdSFrançois Tigeot 	/*
12772c9916cdSFrançois Tigeot 	 * We use the batch length as size because the shadow object is as
12782c9916cdSFrançois Tigeot 	 * large or larger and copy_batch() will write MI_NOPs to the extra
12792c9916cdSFrançois Tigeot 	 * space. Parsing should be faster in some cases this way.
12802c9916cdSFrançois Tigeot 	 */
12811e12ee3bSFrançois Tigeot 	batch_end = cmd + (batch_len / sizeof(*batch_end));
1282a85cb24fSFrançois Tigeot 	do {
1283ba55f2f5SFrançois Tigeot 		u32 length;
1284ba55f2f5SFrançois Tigeot 
1285a85cb24fSFrançois Tigeot 		if (*cmd == MI_BATCH_BUFFER_END) {
1286a85cb24fSFrançois Tigeot 			if (needs_clflush_after) {
1287*3f2dd94aSFrançois Tigeot 				void *ptr = page_mask_bits(shadow_batch_obj->mm.mapping);
1288a85cb24fSFrançois Tigeot 				drm_clflush_virt_range(ptr,
1289a85cb24fSFrançois Tigeot 						       (void *)(cmd + 1) - ptr);
1290a85cb24fSFrançois Tigeot 			}
1291ba55f2f5SFrançois Tigeot 			break;
1292a85cb24fSFrançois Tigeot 		}
1293ba55f2f5SFrançois Tigeot 
12941e12ee3bSFrançois Tigeot 		desc = find_cmd(engine, *cmd, desc, &default_desc);
1295ba55f2f5SFrançois Tigeot 		if (!desc) {
1296ba55f2f5SFrançois Tigeot 			DRM_DEBUG_DRIVER("CMD: Unrecognized command: 0x%08X\n",
1297ba55f2f5SFrançois Tigeot 					 *cmd);
1298ba55f2f5SFrançois Tigeot 			ret = -EINVAL;
1299ba55f2f5SFrançois Tigeot 			break;
1300ba55f2f5SFrançois Tigeot 		}
1301ba55f2f5SFrançois Tigeot 
13022c9916cdSFrançois Tigeot 		/*
13032c9916cdSFrançois Tigeot 		 * If the batch buffer contains a chained batch, return an
13042c9916cdSFrançois Tigeot 		 * error that tells the caller to abort and dispatch the
13052c9916cdSFrançois Tigeot 		 * workload as a non-secure batch.
13062c9916cdSFrançois Tigeot 		 */
13072c9916cdSFrançois Tigeot 		if (desc->cmd.value == MI_BATCH_BUFFER_START) {
13082c9916cdSFrançois Tigeot 			ret = -EACCES;
13092c9916cdSFrançois Tigeot 			break;
13102c9916cdSFrançois Tigeot 		}
13112c9916cdSFrançois Tigeot 
1312ba55f2f5SFrançois Tigeot 		if (desc->flags & CMD_DESC_FIXED)
1313ba55f2f5SFrançois Tigeot 			length = desc->length.fixed;
1314ba55f2f5SFrançois Tigeot 		else
1315ba55f2f5SFrançois Tigeot 			length = ((*cmd & desc->length.mask) + LENGTH_BIAS);
1316ba55f2f5SFrançois Tigeot 
1317ba55f2f5SFrançois Tigeot 		if ((batch_end - cmd) < length) {
1318ba55f2f5SFrançois Tigeot 			DRM_DEBUG_DRIVER("CMD: Command length exceeds batch length: 0x%08X length=%u batchlen=%td\n",
1319ba55f2f5SFrançois Tigeot 					 *cmd,
1320ba55f2f5SFrançois Tigeot 					 length,
1321ba55f2f5SFrançois Tigeot 					 batch_end - cmd);
1322ba55f2f5SFrançois Tigeot 			ret = -EINVAL;
1323ba55f2f5SFrançois Tigeot 			break;
1324ba55f2f5SFrançois Tigeot 		}
1325ba55f2f5SFrançois Tigeot 
1326a85cb24fSFrançois Tigeot 		if (!check_cmd(engine, desc, cmd, length, is_master)) {
1327a85cb24fSFrançois Tigeot 			ret = -EACCES;
1328ba55f2f5SFrançois Tigeot 			break;
1329ba55f2f5SFrançois Tigeot 		}
1330ba55f2f5SFrançois Tigeot 
1331ba55f2f5SFrançois Tigeot 		cmd += length;
1332ba55f2f5SFrançois Tigeot 		if  (cmd >= batch_end) {
1333ba55f2f5SFrançois Tigeot 			DRM_DEBUG_DRIVER("CMD: Got to the end of the buffer w/o a BBE cmd!\n");
1334ba55f2f5SFrançois Tigeot 			ret = -EINVAL;
1335a85cb24fSFrançois Tigeot 			break;
1336ba55f2f5SFrançois Tigeot 		}
1337a85cb24fSFrançois Tigeot 	} while (1);
1338ba55f2f5SFrançois Tigeot 
13391e12ee3bSFrançois Tigeot 	i915_gem_object_unpin_map(shadow_batch_obj);
1340ba55f2f5SFrançois Tigeot 	return ret;
1341ba55f2f5SFrançois Tigeot }
1342ba55f2f5SFrançois Tigeot 
1343ba55f2f5SFrançois Tigeot /**
1344ba55f2f5SFrançois Tigeot  * i915_cmd_parser_get_version() - get the cmd parser version number
13451487f786SFrançois Tigeot  * @dev_priv: i915 device private
1346ba55f2f5SFrançois Tigeot  *
1347ba55f2f5SFrançois Tigeot  * The cmd parser maintains a simple increasing integer version number suitable
1348ba55f2f5SFrançois Tigeot  * for passing to userspace clients to determine what operations are permitted.
1349ba55f2f5SFrançois Tigeot  *
1350ba55f2f5SFrançois Tigeot  * Return: the current version number of the cmd parser
1351ba55f2f5SFrançois Tigeot  */
i915_cmd_parser_get_version(struct drm_i915_private * dev_priv)13521487f786SFrançois Tigeot int i915_cmd_parser_get_version(struct drm_i915_private *dev_priv)
1353ba55f2f5SFrançois Tigeot {
13541487f786SFrançois Tigeot 	struct intel_engine_cs *engine;
13551e12ee3bSFrançois Tigeot 	enum intel_engine_id id;
13561487f786SFrançois Tigeot 	bool active = false;
13571487f786SFrançois Tigeot 
13581487f786SFrançois Tigeot 	/* If the command parser is not enabled, report 0 - unsupported */
13591e12ee3bSFrançois Tigeot 	for_each_engine(engine, dev_priv, id) {
1360a85cb24fSFrançois Tigeot 		if (engine->needs_cmd_parser) {
13611487f786SFrançois Tigeot 			active = true;
13621487f786SFrançois Tigeot 			break;
13631487f786SFrançois Tigeot 		}
13641487f786SFrançois Tigeot 	}
13651487f786SFrançois Tigeot 	if (!active)
13661487f786SFrançois Tigeot 		return 0;
13671487f786SFrançois Tigeot 
1368ba55f2f5SFrançois Tigeot 	/*
1369ba55f2f5SFrançois Tigeot 	 * Command parser version history
1370ba55f2f5SFrançois Tigeot 	 *
1371ba55f2f5SFrançois Tigeot 	 * 1. Initial version. Checks batches and reports violations, but leaves
1372ba55f2f5SFrançois Tigeot 	 *    hardware parsing enabled (so does not allow new use cases).
13732c9916cdSFrançois Tigeot 	 * 2. Allow access to the MI_PREDICATE_SRC0 and
13742c9916cdSFrançois Tigeot 	 *    MI_PREDICATE_SRC1 registers.
13752c9916cdSFrançois Tigeot 	 * 3. Allow access to the GPGPU_THREADS_DISPATCHED register.
1376352ff8bdSFrançois Tigeot 	 * 4. L3 atomic chicken bits of HSW_SCRATCH1 and HSW_ROW_CHICKEN3.
1377352ff8bdSFrançois Tigeot 	 * 5. GPGPU dispatch compute indirect registers.
13788621f407SFrançois Tigeot 	 * 6. TIMESTAMP register and Haswell CS GPR registers
13791487f786SFrançois Tigeot 	 * 7. Allow MI_LOAD_REGISTER_REG between whitelisted registers.
1380a85cb24fSFrançois Tigeot 	 * 8. Don't report cmd_check() failures as EINVAL errors to userspace;
1381a85cb24fSFrançois Tigeot 	 *    rely on the HW to NOOP disallowed commands as it would without
1382a85cb24fSFrançois Tigeot 	 *    the parser enabled.
1383a85cb24fSFrançois Tigeot 	 * 9. Don't whitelist or handle oacontrol specially, as ownership
1384a85cb24fSFrançois Tigeot 	 *    for oacontrol state is moving to i915-perf.
1385ba55f2f5SFrançois Tigeot 	 */
1386a85cb24fSFrançois Tigeot 	return 9;
1387ba55f2f5SFrançois Tigeot }
1388