13253c27bSkettenis /*
23253c27bSkettenis * Copyright © 2013 Intel Corporation
33253c27bSkettenis *
43253c27bSkettenis * Permission is hereby granted, free of charge, to any person obtaining a
53253c27bSkettenis * copy of this software and associated documentation files (the "Software"),
63253c27bSkettenis * to deal in the Software without restriction, including without limitation
73253c27bSkettenis * the rights to use, copy, modify, merge, publish, distribute, sublicense,
83253c27bSkettenis * and/or sell copies of the Software, and to permit persons to whom the
93253c27bSkettenis * Software is furnished to do so, subject to the following conditions:
103253c27bSkettenis *
113253c27bSkettenis * The above copyright notice and this permission notice (including the next
123253c27bSkettenis * paragraph) shall be included in all copies or substantial portions of the
133253c27bSkettenis * Software.
143253c27bSkettenis *
153253c27bSkettenis * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
163253c27bSkettenis * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
173253c27bSkettenis * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
183253c27bSkettenis * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
193253c27bSkettenis * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
203253c27bSkettenis * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
213253c27bSkettenis * IN THE SOFTWARE.
223253c27bSkettenis *
233253c27bSkettenis * Authors:
243253c27bSkettenis * Brad Volkin <bradley.d.volkin@intel.com>
253253c27bSkettenis *
263253c27bSkettenis */
273253c27bSkettenis
281bb76ff1Sjsg #include <linux/highmem.h>
29c349dbc7Sjsg
301bb76ff1Sjsg #include <drm/drm_cache.h>
311bb76ff1Sjsg
321bb76ff1Sjsg #include "gt/intel_engine.h"
331bb76ff1Sjsg #include "gt/intel_engine_regs.h"
341bb76ff1Sjsg #include "gt/intel_gpu_commands.h"
351bb76ff1Sjsg #include "gt/intel_gt_regs.h"
361bb76ff1Sjsg
371bb76ff1Sjsg #include "i915_cmd_parser.h"
383253c27bSkettenis #include "i915_drv.h"
39c349dbc7Sjsg #include "i915_memcpy.h"
401bb76ff1Sjsg #include "i915_reg.h"
413253c27bSkettenis
423253c27bSkettenis /**
433253c27bSkettenis * DOC: batch buffer command parser
443253c27bSkettenis *
453253c27bSkettenis * Motivation:
463253c27bSkettenis * Certain OpenGL features (e.g. transform feedback, performance monitoring)
473253c27bSkettenis * require userspace code to submit batches containing commands such as
483253c27bSkettenis * MI_LOAD_REGISTER_IMM to access various registers. Unfortunately, some
493253c27bSkettenis * generations of the hardware will noop these commands in "unsecure" batches
503253c27bSkettenis * (which includes all userspace batches submitted via i915) even though the
513253c27bSkettenis * commands may be safe and represent the intended programming model of the
523253c27bSkettenis * device.
533253c27bSkettenis *
543253c27bSkettenis * The software command parser is similar in operation to the command parsing
553253c27bSkettenis * done in hardware for unsecure batches. However, the software parser allows
563253c27bSkettenis * some operations that would be noop'd by hardware, if the parser determines
573253c27bSkettenis * the operation is safe, and submits the batch as "secure" to prevent hardware
583253c27bSkettenis * parsing.
593253c27bSkettenis *
603253c27bSkettenis * Threats:
613253c27bSkettenis * At a high level, the hardware (and software) checks attempt to prevent
623253c27bSkettenis * granting userspace undue privileges. There are three categories of privilege.
633253c27bSkettenis *
643253c27bSkettenis * First, commands which are explicitly defined as privileged or which should
65f59a1707Sjsg * only be used by the kernel driver. The parser rejects such commands
663253c27bSkettenis *
673253c27bSkettenis * Second, commands which access registers. To support correct/enhanced
683253c27bSkettenis * userspace functionality, particularly certain OpenGL extensions, the parser
69f59a1707Sjsg * provides a whitelist of registers which userspace may safely access
703253c27bSkettenis *
713253c27bSkettenis * Third, commands which access privileged memory (i.e. GGTT, HWS page, etc).
723253c27bSkettenis * The parser always rejects such commands.
733253c27bSkettenis *
743253c27bSkettenis * The majority of the problematic commands fall in the MI_* range, with only a
757f4dd379Sjsg * few specific commands on each engine (e.g. PIPE_CONTROL and MI_FLUSH_DW).
763253c27bSkettenis *
773253c27bSkettenis * Implementation:
787f4dd379Sjsg * Each engine maintains tables of commands and registers which the parser
797f4dd379Sjsg * uses in scanning batch buffers submitted to that engine.
803253c27bSkettenis *
813253c27bSkettenis * Since the set of commands that the parser must check for is significantly
823253c27bSkettenis * smaller than the number of commands supported, the parser tables contain only
833253c27bSkettenis * those commands required by the parser. This generally works because command
843253c27bSkettenis * opcode ranges have standard command length encodings. So for commands that
853253c27bSkettenis * the parser does not need to check, it can easily skip them. This is
867f4dd379Sjsg * implemented via a per-engine length decoding vfunc.
873253c27bSkettenis *
883253c27bSkettenis * Unfortunately, there are a number of commands that do not follow the standard
893253c27bSkettenis * length encoding for their opcode range, primarily amongst the MI_* commands.
903253c27bSkettenis * To handle this, the parser provides a way to define explicit "skip" entries
917f4dd379Sjsg * in the per-engine command tables.
923253c27bSkettenis *
933253c27bSkettenis * Other command table entries map fairly directly to high level categories
94f59a1707Sjsg * mentioned above: rejected, register whitelist. The parser implements a number
95f59a1707Sjsg * of checks, including the privileged memory checks, via a general bitmasking
96f59a1707Sjsg * mechanism.
973253c27bSkettenis */
983253c27bSkettenis
997f4dd379Sjsg /*
1007f4dd379Sjsg * A command that requires special handling by the command parser.
1017f4dd379Sjsg */
1027f4dd379Sjsg struct drm_i915_cmd_descriptor {
1037f4dd379Sjsg /*
1047f4dd379Sjsg * Flags describing how the command parser processes the command.
1057f4dd379Sjsg *
1067f4dd379Sjsg * CMD_DESC_FIXED: The command has a fixed length if this is set,
1077f4dd379Sjsg * a length mask if not set
1087f4dd379Sjsg * CMD_DESC_SKIP: The command is allowed but does not follow the
1097f4dd379Sjsg * standard length encoding for the opcode range in
1107f4dd379Sjsg * which it falls
1117f4dd379Sjsg * CMD_DESC_REJECT: The command is never allowed
1127f4dd379Sjsg * CMD_DESC_REGISTER: The command should be checked against the
1137f4dd379Sjsg * register whitelist for the appropriate ring
1147f4dd379Sjsg */
1157f4dd379Sjsg u32 flags;
1167f4dd379Sjsg #define CMD_DESC_FIXED (1<<0)
1177f4dd379Sjsg #define CMD_DESC_SKIP (1<<1)
1187f4dd379Sjsg #define CMD_DESC_REJECT (1<<2)
1197f4dd379Sjsg #define CMD_DESC_REGISTER (1<<3)
1207f4dd379Sjsg #define CMD_DESC_BITMASK (1<<4)
1217f4dd379Sjsg
1227f4dd379Sjsg /*
1237f4dd379Sjsg * The command's unique identification bits and the bitmask to get them.
1247f4dd379Sjsg * This isn't strictly the opcode field as defined in the spec and may
1257f4dd379Sjsg * also include type, subtype, and/or subop fields.
1267f4dd379Sjsg */
1277f4dd379Sjsg struct {
1287f4dd379Sjsg u32 value;
1297f4dd379Sjsg u32 mask;
1307f4dd379Sjsg } cmd;
1317f4dd379Sjsg
1327f4dd379Sjsg /*
1337f4dd379Sjsg * The command's length. The command is either fixed length (i.e. does
1347f4dd379Sjsg * not include a length field) or has a length field mask. The flag
1357f4dd379Sjsg * CMD_DESC_FIXED indicates a fixed length. Otherwise, the command has
1367f4dd379Sjsg * a length mask. All command entries in a command table must include
1377f4dd379Sjsg * length information.
1387f4dd379Sjsg */
1397f4dd379Sjsg union {
1407f4dd379Sjsg u32 fixed;
1417f4dd379Sjsg u32 mask;
1427f4dd379Sjsg } length;
1437f4dd379Sjsg
1447f4dd379Sjsg /*
1457f4dd379Sjsg * Describes where to find a register address in the command to check
1467f4dd379Sjsg * against the ring's register whitelist. Only valid if flags has the
1477f4dd379Sjsg * CMD_DESC_REGISTER bit set.
1487f4dd379Sjsg *
1497f4dd379Sjsg * A non-zero step value implies that the command may access multiple
1507f4dd379Sjsg * registers in sequence (e.g. LRI), in that case step gives the
1517f4dd379Sjsg * distance in dwords between individual offset fields.
1527f4dd379Sjsg */
1537f4dd379Sjsg struct {
1547f4dd379Sjsg u32 offset;
1557f4dd379Sjsg u32 mask;
1567f4dd379Sjsg u32 step;
1577f4dd379Sjsg } reg;
1587f4dd379Sjsg
1597f4dd379Sjsg #define MAX_CMD_DESC_BITMASKS 3
1607f4dd379Sjsg /*
1617f4dd379Sjsg * Describes command checks where a particular dword is masked and
1627f4dd379Sjsg * compared against an expected value. If the command does not match
1637f4dd379Sjsg * the expected value, the parser rejects it. Only valid if flags has
1647f4dd379Sjsg * the CMD_DESC_BITMASK bit set. Only entries where mask is non-zero
1657f4dd379Sjsg * are valid.
1667f4dd379Sjsg *
1677f4dd379Sjsg * If the check specifies a non-zero condition_mask then the parser
1687f4dd379Sjsg * only performs the check when the bits specified by condition_mask
1697f4dd379Sjsg * are non-zero.
1707f4dd379Sjsg */
1717f4dd379Sjsg struct {
1727f4dd379Sjsg u32 offset;
1737f4dd379Sjsg u32 mask;
1747f4dd379Sjsg u32 expected;
1757f4dd379Sjsg u32 condition_offset;
1767f4dd379Sjsg u32 condition_mask;
1777f4dd379Sjsg } bits[MAX_CMD_DESC_BITMASKS];
1787f4dd379Sjsg };
1797f4dd379Sjsg
1807f4dd379Sjsg /*
1817f4dd379Sjsg * A table of commands requiring special handling by the command parser.
1827f4dd379Sjsg *
1837f4dd379Sjsg * Each engine has an array of tables. Each table consists of an array of
1847f4dd379Sjsg * command descriptors, which must be sorted with command opcodes in
1857f4dd379Sjsg * ascending order.
1867f4dd379Sjsg */
1877f4dd379Sjsg struct drm_i915_cmd_table {
1887f4dd379Sjsg const struct drm_i915_cmd_descriptor *table;
1897f4dd379Sjsg int count;
1907f4dd379Sjsg };
1917f4dd379Sjsg
1927f4dd379Sjsg #define STD_MI_OPCODE_SHIFT (32 - 9)
1937f4dd379Sjsg #define STD_3D_OPCODE_SHIFT (32 - 16)
1947f4dd379Sjsg #define STD_2D_OPCODE_SHIFT (32 - 10)
1957f4dd379Sjsg #define STD_MFX_OPCODE_SHIFT (32 - 16)
1967f4dd379Sjsg #define MIN_OPCODE_SHIFT 16
1973253c27bSkettenis
1983253c27bSkettenis #define CMD(op, opm, f, lm, fl, ...) \
1993253c27bSkettenis { \
2003253c27bSkettenis .flags = (fl) | ((f) ? CMD_DESC_FIXED : 0), \
201427efabdSjsg .cmd = { (op & ~0u << (opm)), ~0u << (opm) }, \
2023253c27bSkettenis .length = { (lm) }, \
2033253c27bSkettenis __VA_ARGS__ \
2043253c27bSkettenis }
2053253c27bSkettenis
2063253c27bSkettenis /* Convenience macros to compress the tables */
2077f4dd379Sjsg #define SMI STD_MI_OPCODE_SHIFT
2087f4dd379Sjsg #define S3D STD_3D_OPCODE_SHIFT
2097f4dd379Sjsg #define S2D STD_2D_OPCODE_SHIFT
2107f4dd379Sjsg #define SMFX STD_MFX_OPCODE_SHIFT
2113253c27bSkettenis #define F true
2123253c27bSkettenis #define S CMD_DESC_SKIP
2133253c27bSkettenis #define R CMD_DESC_REJECT
2143253c27bSkettenis #define W CMD_DESC_REGISTER
2153253c27bSkettenis #define B CMD_DESC_BITMASK
2163253c27bSkettenis
2173253c27bSkettenis /* Command Mask Fixed Len Action
2183253c27bSkettenis ---------------------------------------------------------- */
2193f92f658Sjsg static const struct drm_i915_cmd_descriptor gen7_common_cmds[] = {
2203253c27bSkettenis CMD( MI_NOOP, SMI, F, 1, S ),
2213253c27bSkettenis CMD( MI_USER_INTERRUPT, SMI, F, 1, R ),
222f59a1707Sjsg CMD( MI_WAIT_FOR_EVENT, SMI, F, 1, R ),
2233253c27bSkettenis CMD( MI_ARB_CHECK, SMI, F, 1, S ),
2243253c27bSkettenis CMD( MI_REPORT_HEAD, SMI, F, 1, S ),
2253253c27bSkettenis CMD( MI_SUSPEND_FLUSH, SMI, F, 1, S ),
2263253c27bSkettenis CMD( MI_SEMAPHORE_MBOX, SMI, !F, 0xFF, R ),
2273253c27bSkettenis CMD( MI_STORE_DWORD_INDEX, SMI, !F, 0xFF, R ),
2283253c27bSkettenis CMD( MI_LOAD_REGISTER_IMM(1), SMI, !F, 0xFF, W,
2293253c27bSkettenis .reg = { .offset = 1, .mask = 0x007FFFFC, .step = 2 } ),
2303253c27bSkettenis CMD( MI_STORE_REGISTER_MEM, SMI, F, 3, W | B,
2313253c27bSkettenis .reg = { .offset = 1, .mask = 0x007FFFFC },
2323253c27bSkettenis .bits = {{
2333253c27bSkettenis .offset = 0,
2343253c27bSkettenis .mask = MI_GLOBAL_GTT,
2353253c27bSkettenis .expected = 0,
2363253c27bSkettenis }}, ),
2373253c27bSkettenis CMD( MI_LOAD_REGISTER_MEM, SMI, F, 3, W | B,
2383253c27bSkettenis .reg = { .offset = 1, .mask = 0x007FFFFC },
2393253c27bSkettenis .bits = {{
2403253c27bSkettenis .offset = 0,
2413253c27bSkettenis .mask = MI_GLOBAL_GTT,
2423253c27bSkettenis .expected = 0,
2433253c27bSkettenis }}, ),
2443253c27bSkettenis /*
2453253c27bSkettenis * MI_BATCH_BUFFER_START requires some special handling. It's not
2463253c27bSkettenis * really a 'skip' action but it doesn't seem like it's worth adding
247c349dbc7Sjsg * a new action. See intel_engine_cmd_parser().
2483253c27bSkettenis */
2493253c27bSkettenis CMD( MI_BATCH_BUFFER_START, SMI, !F, 0xFF, S ),
2503253c27bSkettenis };
2513253c27bSkettenis
2523f92f658Sjsg static const struct drm_i915_cmd_descriptor gen7_render_cmds[] = {
2533253c27bSkettenis CMD( MI_FLUSH, SMI, F, 1, S ),
2543253c27bSkettenis CMD( MI_ARB_ON_OFF, SMI, F, 1, R ),
2553253c27bSkettenis CMD( MI_PREDICATE, SMI, F, 1, S ),
2563253c27bSkettenis CMD( MI_TOPOLOGY_FILTER, SMI, F, 1, S ),
2573253c27bSkettenis CMD( MI_SET_APPID, SMI, F, 1, S ),
2583253c27bSkettenis CMD( MI_DISPLAY_FLIP, SMI, !F, 0xFF, R ),
2593253c27bSkettenis CMD( MI_SET_CONTEXT, SMI, !F, 0xFF, R ),
2603253c27bSkettenis CMD( MI_URB_CLEAR, SMI, !F, 0xFF, S ),
2613253c27bSkettenis CMD( MI_STORE_DWORD_IMM, SMI, !F, 0x3F, B,
2623253c27bSkettenis .bits = {{
2633253c27bSkettenis .offset = 0,
2643253c27bSkettenis .mask = MI_GLOBAL_GTT,
2653253c27bSkettenis .expected = 0,
2663253c27bSkettenis }}, ),
2673253c27bSkettenis CMD( MI_UPDATE_GTT, SMI, !F, 0xFF, R ),
2683253c27bSkettenis CMD( MI_CLFLUSH, SMI, !F, 0x3FF, B,
2693253c27bSkettenis .bits = {{
2703253c27bSkettenis .offset = 0,
2713253c27bSkettenis .mask = MI_GLOBAL_GTT,
2723253c27bSkettenis .expected = 0,
2733253c27bSkettenis }}, ),
2743253c27bSkettenis CMD( MI_REPORT_PERF_COUNT, SMI, !F, 0x3F, B,
2753253c27bSkettenis .bits = {{
2763253c27bSkettenis .offset = 1,
2773253c27bSkettenis .mask = MI_REPORT_PERF_COUNT_GGTT,
2783253c27bSkettenis .expected = 0,
2793253c27bSkettenis }}, ),
2803253c27bSkettenis CMD( MI_CONDITIONAL_BATCH_BUFFER_END, SMI, !F, 0xFF, B,
2813253c27bSkettenis .bits = {{
2823253c27bSkettenis .offset = 0,
2833253c27bSkettenis .mask = MI_GLOBAL_GTT,
2843253c27bSkettenis .expected = 0,
2853253c27bSkettenis }}, ),
2863253c27bSkettenis CMD( GFX_OP_3DSTATE_VF_STATISTICS, S3D, F, 1, S ),
2873253c27bSkettenis CMD( PIPELINE_SELECT, S3D, F, 1, S ),
2883253c27bSkettenis CMD( MEDIA_VFE_STATE, S3D, !F, 0xFFFF, B,
2893253c27bSkettenis .bits = {{
2903253c27bSkettenis .offset = 2,
2913253c27bSkettenis .mask = MEDIA_VFE_STATE_MMIO_ACCESS_MASK,
2923253c27bSkettenis .expected = 0,
2933253c27bSkettenis }}, ),
2943253c27bSkettenis CMD( GPGPU_OBJECT, S3D, !F, 0xFF, S ),
2953253c27bSkettenis CMD( GPGPU_WALKER, S3D, !F, 0xFF, S ),
2963253c27bSkettenis CMD( GFX_OP_3DSTATE_SO_DECL_LIST, S3D, !F, 0x1FF, S ),
2973253c27bSkettenis CMD( GFX_OP_PIPE_CONTROL(5), S3D, !F, 0xFF, B,
2983253c27bSkettenis .bits = {{
2993253c27bSkettenis .offset = 1,
3003253c27bSkettenis .mask = (PIPE_CONTROL_MMIO_WRITE | PIPE_CONTROL_NOTIFY),
3013253c27bSkettenis .expected = 0,
3023253c27bSkettenis },
3033253c27bSkettenis {
3043253c27bSkettenis .offset = 1,
3053253c27bSkettenis .mask = (PIPE_CONTROL_GLOBAL_GTT_IVB |
3063253c27bSkettenis PIPE_CONTROL_STORE_DATA_INDEX),
3073253c27bSkettenis .expected = 0,
3083253c27bSkettenis .condition_offset = 1,
3093253c27bSkettenis .condition_mask = PIPE_CONTROL_POST_SYNC_OP_MASK,
3103253c27bSkettenis }}, ),
3113253c27bSkettenis };
3123253c27bSkettenis
3133253c27bSkettenis static const struct drm_i915_cmd_descriptor hsw_render_cmds[] = {
3143253c27bSkettenis CMD( MI_SET_PREDICATE, SMI, F, 1, S ),
3153253c27bSkettenis CMD( MI_RS_CONTROL, SMI, F, 1, S ),
3163253c27bSkettenis CMD( MI_URB_ATOMIC_ALLOC, SMI, F, 1, S ),
3173253c27bSkettenis CMD( MI_SET_APPID, SMI, F, 1, S ),
3183253c27bSkettenis CMD( MI_RS_CONTEXT, SMI, F, 1, S ),
319f59a1707Sjsg CMD( MI_LOAD_SCAN_LINES_INCL, SMI, !F, 0x3F, R ),
3203253c27bSkettenis CMD( MI_LOAD_SCAN_LINES_EXCL, SMI, !F, 0x3F, R ),
3217f4dd379Sjsg CMD( MI_LOAD_REGISTER_REG, SMI, !F, 0xFF, W,
3227f4dd379Sjsg .reg = { .offset = 1, .mask = 0x007FFFFC, .step = 1 } ),
3233253c27bSkettenis CMD( MI_RS_STORE_DATA_IMM, SMI, !F, 0xFF, S ),
3243253c27bSkettenis CMD( MI_LOAD_URB_MEM, SMI, !F, 0xFF, S ),
3253253c27bSkettenis CMD( MI_STORE_URB_MEM, SMI, !F, 0xFF, S ),
3263253c27bSkettenis CMD( GFX_OP_3DSTATE_DX9_CONSTANTF_VS, S3D, !F, 0x7FF, S ),
3273253c27bSkettenis CMD( GFX_OP_3DSTATE_DX9_CONSTANTF_PS, S3D, !F, 0x7FF, S ),
3283253c27bSkettenis
3293253c27bSkettenis CMD( GFX_OP_3DSTATE_BINDING_TABLE_EDIT_VS, S3D, !F, 0x1FF, S ),
3303253c27bSkettenis CMD( GFX_OP_3DSTATE_BINDING_TABLE_EDIT_GS, S3D, !F, 0x1FF, S ),
3313253c27bSkettenis CMD( GFX_OP_3DSTATE_BINDING_TABLE_EDIT_HS, S3D, !F, 0x1FF, S ),
3323253c27bSkettenis CMD( GFX_OP_3DSTATE_BINDING_TABLE_EDIT_DS, S3D, !F, 0x1FF, S ),
3333253c27bSkettenis CMD( GFX_OP_3DSTATE_BINDING_TABLE_EDIT_PS, S3D, !F, 0x1FF, S ),
3343253c27bSkettenis };
3353253c27bSkettenis
3363f92f658Sjsg static const struct drm_i915_cmd_descriptor gen7_video_cmds[] = {
3373253c27bSkettenis CMD( MI_ARB_ON_OFF, SMI, F, 1, R ),
3383253c27bSkettenis CMD( MI_SET_APPID, SMI, F, 1, S ),
3393253c27bSkettenis CMD( MI_STORE_DWORD_IMM, SMI, !F, 0xFF, B,
3403253c27bSkettenis .bits = {{
3413253c27bSkettenis .offset = 0,
3423253c27bSkettenis .mask = MI_GLOBAL_GTT,
3433253c27bSkettenis .expected = 0,
3443253c27bSkettenis }}, ),
3453253c27bSkettenis CMD( MI_UPDATE_GTT, SMI, !F, 0x3F, R ),
3463253c27bSkettenis CMD( MI_FLUSH_DW, SMI, !F, 0x3F, B,
3473253c27bSkettenis .bits = {{
3483253c27bSkettenis .offset = 0,
3493253c27bSkettenis .mask = MI_FLUSH_DW_NOTIFY,
3503253c27bSkettenis .expected = 0,
3513253c27bSkettenis },
3523253c27bSkettenis {
3533253c27bSkettenis .offset = 1,
3543253c27bSkettenis .mask = MI_FLUSH_DW_USE_GTT,
3553253c27bSkettenis .expected = 0,
3563253c27bSkettenis .condition_offset = 0,
3573253c27bSkettenis .condition_mask = MI_FLUSH_DW_OP_MASK,
3583253c27bSkettenis },
3593253c27bSkettenis {
3603253c27bSkettenis .offset = 0,
3613253c27bSkettenis .mask = MI_FLUSH_DW_STORE_INDEX,
3623253c27bSkettenis .expected = 0,
3633253c27bSkettenis .condition_offset = 0,
3643253c27bSkettenis .condition_mask = MI_FLUSH_DW_OP_MASK,
3653253c27bSkettenis }}, ),
3663253c27bSkettenis CMD( MI_CONDITIONAL_BATCH_BUFFER_END, SMI, !F, 0xFF, B,
3673253c27bSkettenis .bits = {{
3683253c27bSkettenis .offset = 0,
3693253c27bSkettenis .mask = MI_GLOBAL_GTT,
3703253c27bSkettenis .expected = 0,
3713253c27bSkettenis }}, ),
3723253c27bSkettenis /*
3733253c27bSkettenis * MFX_WAIT doesn't fit the way we handle length for most commands.
3743253c27bSkettenis * It has a length field but it uses a non-standard length bias.
3753253c27bSkettenis * It is always 1 dword though, so just treat it as fixed length.
3763253c27bSkettenis */
3773253c27bSkettenis CMD( MFX_WAIT, SMFX, F, 1, S ),
3783253c27bSkettenis };
3793253c27bSkettenis
3803f92f658Sjsg static const struct drm_i915_cmd_descriptor gen7_vecs_cmds[] = {
3813253c27bSkettenis CMD( MI_ARB_ON_OFF, SMI, F, 1, R ),
3823253c27bSkettenis CMD( MI_SET_APPID, SMI, F, 1, S ),
3833253c27bSkettenis CMD( MI_STORE_DWORD_IMM, SMI, !F, 0xFF, B,
3843253c27bSkettenis .bits = {{
3853253c27bSkettenis .offset = 0,
3863253c27bSkettenis .mask = MI_GLOBAL_GTT,
3873253c27bSkettenis .expected = 0,
3883253c27bSkettenis }}, ),
3893253c27bSkettenis CMD( MI_UPDATE_GTT, SMI, !F, 0x3F, R ),
3903253c27bSkettenis CMD( MI_FLUSH_DW, SMI, !F, 0x3F, B,
3913253c27bSkettenis .bits = {{
3923253c27bSkettenis .offset = 0,
3933253c27bSkettenis .mask = MI_FLUSH_DW_NOTIFY,
3943253c27bSkettenis .expected = 0,
3953253c27bSkettenis },
3963253c27bSkettenis {
3973253c27bSkettenis .offset = 1,
3983253c27bSkettenis .mask = MI_FLUSH_DW_USE_GTT,
3993253c27bSkettenis .expected = 0,
4003253c27bSkettenis .condition_offset = 0,
4013253c27bSkettenis .condition_mask = MI_FLUSH_DW_OP_MASK,
4023253c27bSkettenis },
4033253c27bSkettenis {
4043253c27bSkettenis .offset = 0,
4053253c27bSkettenis .mask = MI_FLUSH_DW_STORE_INDEX,
4063253c27bSkettenis .expected = 0,
4073253c27bSkettenis .condition_offset = 0,
4083253c27bSkettenis .condition_mask = MI_FLUSH_DW_OP_MASK,
4093253c27bSkettenis }}, ),
4103253c27bSkettenis CMD( MI_CONDITIONAL_BATCH_BUFFER_END, SMI, !F, 0xFF, B,
4113253c27bSkettenis .bits = {{
4123253c27bSkettenis .offset = 0,
4133253c27bSkettenis .mask = MI_GLOBAL_GTT,
4143253c27bSkettenis .expected = 0,
4153253c27bSkettenis }}, ),
4163253c27bSkettenis };
4173253c27bSkettenis
4183f92f658Sjsg static const struct drm_i915_cmd_descriptor gen7_blt_cmds[] = {
4193253c27bSkettenis CMD( MI_DISPLAY_FLIP, SMI, !F, 0xFF, R ),
4203253c27bSkettenis CMD( MI_STORE_DWORD_IMM, SMI, !F, 0x3FF, B,
4213253c27bSkettenis .bits = {{
4223253c27bSkettenis .offset = 0,
4233253c27bSkettenis .mask = MI_GLOBAL_GTT,
4243253c27bSkettenis .expected = 0,
4253253c27bSkettenis }}, ),
4263253c27bSkettenis CMD( MI_UPDATE_GTT, SMI, !F, 0x3F, R ),
4273253c27bSkettenis CMD( MI_FLUSH_DW, SMI, !F, 0x3F, B,
4283253c27bSkettenis .bits = {{
4293253c27bSkettenis .offset = 0,
4303253c27bSkettenis .mask = MI_FLUSH_DW_NOTIFY,
4313253c27bSkettenis .expected = 0,
4323253c27bSkettenis },
4333253c27bSkettenis {
4343253c27bSkettenis .offset = 1,
4353253c27bSkettenis .mask = MI_FLUSH_DW_USE_GTT,
4363253c27bSkettenis .expected = 0,
4373253c27bSkettenis .condition_offset = 0,
4383253c27bSkettenis .condition_mask = MI_FLUSH_DW_OP_MASK,
4393253c27bSkettenis },
4403253c27bSkettenis {
4413253c27bSkettenis .offset = 0,
4423253c27bSkettenis .mask = MI_FLUSH_DW_STORE_INDEX,
4433253c27bSkettenis .expected = 0,
4443253c27bSkettenis .condition_offset = 0,
4453253c27bSkettenis .condition_mask = MI_FLUSH_DW_OP_MASK,
4463253c27bSkettenis }}, ),
4473253c27bSkettenis CMD( COLOR_BLT, S2D, !F, 0x3F, S ),
4483253c27bSkettenis CMD( SRC_COPY_BLT, S2D, !F, 0x3F, S ),
4493253c27bSkettenis };
4503253c27bSkettenis
4513253c27bSkettenis static const struct drm_i915_cmd_descriptor hsw_blt_cmds[] = {
452f59a1707Sjsg CMD( MI_LOAD_SCAN_LINES_INCL, SMI, !F, 0x3F, R ),
4533253c27bSkettenis CMD( MI_LOAD_SCAN_LINES_EXCL, SMI, !F, 0x3F, R ),
4543253c27bSkettenis };
4553253c27bSkettenis
4569a2084e7Sjsg /*
4579a2084e7Sjsg * For Gen9 we can still rely on the h/w to enforce cmd security, and only
4589a2084e7Sjsg * need to re-enforce the register access checks. We therefore only need to
4599a2084e7Sjsg * teach the cmdparser how to find the end of each command, and identify
4609a2084e7Sjsg * register accesses. The table doesn't need to reject any commands, and so
4619a2084e7Sjsg * the only commands listed here are:
4629a2084e7Sjsg * 1) Those that touch registers
4639a2084e7Sjsg * 2) Those that do not have the default 8-bit length
4649a2084e7Sjsg *
4659a2084e7Sjsg * Note that the default MI length mask chosen for this table is 0xFF, not
4669a2084e7Sjsg * the 0x3F used on older devices. This is because the vast majority of MI
4679a2084e7Sjsg * cmds on Gen9 use a standard 8-bit Length field.
4689a2084e7Sjsg * All the Gen9 blitter instructions are standard 0xFF length mask, and
4699a2084e7Sjsg * none allow access to non-general registers, so in fact no BLT cmds are
4709a2084e7Sjsg * included in the table at all.
4719a2084e7Sjsg *
4729a2084e7Sjsg */
4739a2084e7Sjsg static const struct drm_i915_cmd_descriptor gen9_blt_cmds[] = {
4749a2084e7Sjsg CMD( MI_NOOP, SMI, F, 1, S ),
4759a2084e7Sjsg CMD( MI_USER_INTERRUPT, SMI, F, 1, S ),
4769a2084e7Sjsg CMD( MI_WAIT_FOR_EVENT, SMI, F, 1, S ),
4779a2084e7Sjsg CMD( MI_FLUSH, SMI, F, 1, S ),
4789a2084e7Sjsg CMD( MI_ARB_CHECK, SMI, F, 1, S ),
4799a2084e7Sjsg CMD( MI_REPORT_HEAD, SMI, F, 1, S ),
4809a2084e7Sjsg CMD( MI_ARB_ON_OFF, SMI, F, 1, S ),
4819a2084e7Sjsg CMD( MI_SUSPEND_FLUSH, SMI, F, 1, S ),
4829a2084e7Sjsg CMD( MI_LOAD_SCAN_LINES_INCL, SMI, !F, 0x3F, S ),
4839a2084e7Sjsg CMD( MI_LOAD_SCAN_LINES_EXCL, SMI, !F, 0x3F, S ),
4849a2084e7Sjsg CMD( MI_STORE_DWORD_IMM, SMI, !F, 0x3FF, S ),
4859a2084e7Sjsg CMD( MI_LOAD_REGISTER_IMM(1), SMI, !F, 0xFF, W,
4869a2084e7Sjsg .reg = { .offset = 1, .mask = 0x007FFFFC, .step = 2 } ),
4879a2084e7Sjsg CMD( MI_UPDATE_GTT, SMI, !F, 0x3FF, S ),
4889a2084e7Sjsg CMD( MI_STORE_REGISTER_MEM_GEN8, SMI, F, 4, W,
4899a2084e7Sjsg .reg = { .offset = 1, .mask = 0x007FFFFC } ),
4909a2084e7Sjsg CMD( MI_FLUSH_DW, SMI, !F, 0x3F, S ),
4919a2084e7Sjsg CMD( MI_LOAD_REGISTER_MEM_GEN8, SMI, F, 4, W,
4929a2084e7Sjsg .reg = { .offset = 1, .mask = 0x007FFFFC } ),
4939a2084e7Sjsg CMD( MI_LOAD_REGISTER_REG, SMI, !F, 0xFF, W,
4949a2084e7Sjsg .reg = { .offset = 1, .mask = 0x007FFFFC, .step = 1 } ),
495466dcc0aSjsg
496466dcc0aSjsg /*
497466dcc0aSjsg * We allow BB_START but apply further checks. We just sanitize the
498466dcc0aSjsg * basic fields here.
499466dcc0aSjsg */
500466dcc0aSjsg #define MI_BB_START_OPERAND_MASK GENMASK(SMI-1, 0)
501466dcc0aSjsg #define MI_BB_START_OPERAND_EXPECT (MI_BATCH_PPGTT_HSW | 1)
502466dcc0aSjsg CMD( MI_BATCH_BUFFER_START_GEN8, SMI, !F, 0xFF, B,
503466dcc0aSjsg .bits = {{
504466dcc0aSjsg .offset = 0,
505466dcc0aSjsg .mask = MI_BB_START_OPERAND_MASK,
506466dcc0aSjsg .expected = MI_BB_START_OPERAND_EXPECT,
507466dcc0aSjsg }}, ),
5089a2084e7Sjsg };
5099a2084e7Sjsg
5107f4dd379Sjsg static const struct drm_i915_cmd_descriptor noop_desc =
5117f4dd379Sjsg CMD(MI_NOOP, SMI, F, 1, S);
5127f4dd379Sjsg
5133253c27bSkettenis #undef CMD
5143253c27bSkettenis #undef SMI
5153253c27bSkettenis #undef S3D
5163253c27bSkettenis #undef S2D
5173253c27bSkettenis #undef SMFX
5183253c27bSkettenis #undef F
5193253c27bSkettenis #undef S
5203253c27bSkettenis #undef R
5213253c27bSkettenis #undef W
5223253c27bSkettenis #undef B
5233253c27bSkettenis
5243f92f658Sjsg static const struct drm_i915_cmd_table gen7_render_cmd_table[] = {
5253f92f658Sjsg { gen7_common_cmds, ARRAY_SIZE(gen7_common_cmds) },
5263f92f658Sjsg { gen7_render_cmds, ARRAY_SIZE(gen7_render_cmds) },
5273253c27bSkettenis };
5283253c27bSkettenis
5293f92f658Sjsg static const struct drm_i915_cmd_table hsw_render_ring_cmd_table[] = {
5303f92f658Sjsg { gen7_common_cmds, ARRAY_SIZE(gen7_common_cmds) },
5313f92f658Sjsg { gen7_render_cmds, ARRAY_SIZE(gen7_render_cmds) },
5323253c27bSkettenis { hsw_render_cmds, ARRAY_SIZE(hsw_render_cmds) },
5333253c27bSkettenis };
5343253c27bSkettenis
5353f92f658Sjsg static const struct drm_i915_cmd_table gen7_video_cmd_table[] = {
5363f92f658Sjsg { gen7_common_cmds, ARRAY_SIZE(gen7_common_cmds) },
5373f92f658Sjsg { gen7_video_cmds, ARRAY_SIZE(gen7_video_cmds) },
5383253c27bSkettenis };
5393253c27bSkettenis
5403f92f658Sjsg static const struct drm_i915_cmd_table hsw_vebox_cmd_table[] = {
5413f92f658Sjsg { gen7_common_cmds, ARRAY_SIZE(gen7_common_cmds) },
5423f92f658Sjsg { gen7_vecs_cmds, ARRAY_SIZE(gen7_vecs_cmds) },
5433253c27bSkettenis };
5443253c27bSkettenis
5453f92f658Sjsg static const struct drm_i915_cmd_table gen7_blt_cmd_table[] = {
5463f92f658Sjsg { gen7_common_cmds, ARRAY_SIZE(gen7_common_cmds) },
5473f92f658Sjsg { gen7_blt_cmds, ARRAY_SIZE(gen7_blt_cmds) },
5483253c27bSkettenis };
5493253c27bSkettenis
5503f92f658Sjsg static const struct drm_i915_cmd_table hsw_blt_ring_cmd_table[] = {
5513f92f658Sjsg { gen7_common_cmds, ARRAY_SIZE(gen7_common_cmds) },
5523f92f658Sjsg { gen7_blt_cmds, ARRAY_SIZE(gen7_blt_cmds) },
5533253c27bSkettenis { hsw_blt_cmds, ARRAY_SIZE(hsw_blt_cmds) },
5543253c27bSkettenis };
5553253c27bSkettenis
5569a2084e7Sjsg static const struct drm_i915_cmd_table gen9_blt_cmd_table[] = {
5579a2084e7Sjsg { gen9_blt_cmds, ARRAY_SIZE(gen9_blt_cmds) },
5589a2084e7Sjsg };
5599a2084e7Sjsg
5609a2084e7Sjsg
5613253c27bSkettenis /*
5623253c27bSkettenis * Register whitelists, sorted by increasing register offset.
5633253c27bSkettenis */
5643253c27bSkettenis
5653253c27bSkettenis /*
5663253c27bSkettenis * An individual whitelist entry granting access to register addr. If
5673253c27bSkettenis * mask is non-zero the argument of immediate register writes will be
5683253c27bSkettenis * AND-ed with mask, and the command will be rejected if the result
5693253c27bSkettenis * doesn't match value.
5703253c27bSkettenis *
5713253c27bSkettenis * Registers with non-zero mask are only allowed to be written using
5723253c27bSkettenis * LRI.
5733253c27bSkettenis */
5743253c27bSkettenis struct drm_i915_reg_descriptor {
5757f4dd379Sjsg i915_reg_t addr;
5763253c27bSkettenis u32 mask;
5773253c27bSkettenis u32 value;
5783253c27bSkettenis };
5793253c27bSkettenis
5803253c27bSkettenis /* Convenience macro for adding 32-bit registers. */
5817f4dd379Sjsg #define REG32(_reg, ...) \
5827f4dd379Sjsg { .addr = (_reg), __VA_ARGS__ }
5833253c27bSkettenis
584e80f4449Sjsg #define REG32_IDX(_reg, idx) \
585e80f4449Sjsg { .addr = _reg(idx) }
586e80f4449Sjsg
5873253c27bSkettenis /*
5883253c27bSkettenis * Convenience macro for adding 64-bit registers.
5893253c27bSkettenis *
5903253c27bSkettenis * Some registers that userspace accesses are 64 bits. The register
5913253c27bSkettenis * access commands only allow 32-bit accesses. Hence, we have to include
5923253c27bSkettenis * entries for both halves of the 64-bit registers.
5933253c27bSkettenis */
5947f4dd379Sjsg #define REG64(_reg) \
5957f4dd379Sjsg { .addr = _reg }, \
5967f4dd379Sjsg { .addr = _reg ## _UDW }
5977f4dd379Sjsg
5987f4dd379Sjsg #define REG64_IDX(_reg, idx) \
5997f4dd379Sjsg { .addr = _reg(idx) }, \
6007f4dd379Sjsg { .addr = _reg ## _UDW(idx) }
6013253c27bSkettenis
6021bb76ff1Sjsg #define REG64_BASE_IDX(_reg, base, idx) \
6031bb76ff1Sjsg { .addr = _reg(base, idx) }, \
6041bb76ff1Sjsg { .addr = _reg ## _UDW(base, idx) }
6051bb76ff1Sjsg
6063253c27bSkettenis static const struct drm_i915_reg_descriptor gen7_render_regs[] = {
6073253c27bSkettenis REG64(GPGPU_THREADS_DISPATCHED),
6083253c27bSkettenis REG64(HS_INVOCATION_COUNT),
6093253c27bSkettenis REG64(DS_INVOCATION_COUNT),
6103253c27bSkettenis REG64(IA_VERTICES_COUNT),
6113253c27bSkettenis REG64(IA_PRIMITIVES_COUNT),
6123253c27bSkettenis REG64(VS_INVOCATION_COUNT),
6133253c27bSkettenis REG64(GS_INVOCATION_COUNT),
6143253c27bSkettenis REG64(GS_PRIMITIVES_COUNT),
6153253c27bSkettenis REG64(CL_INVOCATION_COUNT),
6163253c27bSkettenis REG64(CL_PRIMITIVES_COUNT),
6173253c27bSkettenis REG64(PS_INVOCATION_COUNT),
6183253c27bSkettenis REG64(PS_DEPTH_COUNT),
6197f4dd379Sjsg REG64_IDX(RING_TIMESTAMP, RENDER_RING_BASE),
6201bb76ff1Sjsg REG64_IDX(MI_PREDICATE_SRC0, RENDER_RING_BASE),
6211bb76ff1Sjsg REG64_IDX(MI_PREDICATE_SRC1, RENDER_RING_BASE),
6223253c27bSkettenis REG32(GEN7_3DPRIM_END_OFFSET),
6233253c27bSkettenis REG32(GEN7_3DPRIM_START_VERTEX),
6243253c27bSkettenis REG32(GEN7_3DPRIM_VERTEX_COUNT),
6253253c27bSkettenis REG32(GEN7_3DPRIM_INSTANCE_COUNT),
6263253c27bSkettenis REG32(GEN7_3DPRIM_START_INSTANCE),
6273253c27bSkettenis REG32(GEN7_3DPRIM_BASE_VERTEX),
6283253c27bSkettenis REG32(GEN7_GPGPU_DISPATCHDIMX),
6293253c27bSkettenis REG32(GEN7_GPGPU_DISPATCHDIMY),
6303253c27bSkettenis REG32(GEN7_GPGPU_DISPATCHDIMZ),
6317f4dd379Sjsg REG64_IDX(RING_TIMESTAMP, BSD_RING_BASE),
6327f4dd379Sjsg REG64_IDX(GEN7_SO_NUM_PRIMS_WRITTEN, 0),
6337f4dd379Sjsg REG64_IDX(GEN7_SO_NUM_PRIMS_WRITTEN, 1),
6347f4dd379Sjsg REG64_IDX(GEN7_SO_NUM_PRIMS_WRITTEN, 2),
6357f4dd379Sjsg REG64_IDX(GEN7_SO_NUM_PRIMS_WRITTEN, 3),
6367f4dd379Sjsg REG64_IDX(GEN7_SO_PRIM_STORAGE_NEEDED, 0),
6377f4dd379Sjsg REG64_IDX(GEN7_SO_PRIM_STORAGE_NEEDED, 1),
6387f4dd379Sjsg REG64_IDX(GEN7_SO_PRIM_STORAGE_NEEDED, 2),
6397f4dd379Sjsg REG64_IDX(GEN7_SO_PRIM_STORAGE_NEEDED, 3),
6403253c27bSkettenis REG32(GEN7_SO_WRITE_OFFSET(0)),
6413253c27bSkettenis REG32(GEN7_SO_WRITE_OFFSET(1)),
6423253c27bSkettenis REG32(GEN7_SO_WRITE_OFFSET(2)),
6433253c27bSkettenis REG32(GEN7_SO_WRITE_OFFSET(3)),
6443253c27bSkettenis REG32(GEN7_L3SQCREG1),
6453253c27bSkettenis REG32(GEN7_L3CNTLREG2),
6463253c27bSkettenis REG32(GEN7_L3CNTLREG3),
6477f4dd379Sjsg REG64_IDX(RING_TIMESTAMP, BLT_RING_BASE),
6487f4dd379Sjsg };
6497f4dd379Sjsg
6507f4dd379Sjsg static const struct drm_i915_reg_descriptor hsw_render_regs[] = {
6511bb76ff1Sjsg REG64_BASE_IDX(GEN8_RING_CS_GPR, RENDER_RING_BASE, 0),
6521bb76ff1Sjsg REG64_BASE_IDX(GEN8_RING_CS_GPR, RENDER_RING_BASE, 1),
6531bb76ff1Sjsg REG64_BASE_IDX(GEN8_RING_CS_GPR, RENDER_RING_BASE, 2),
6541bb76ff1Sjsg REG64_BASE_IDX(GEN8_RING_CS_GPR, RENDER_RING_BASE, 3),
6551bb76ff1Sjsg REG64_BASE_IDX(GEN8_RING_CS_GPR, RENDER_RING_BASE, 4),
6561bb76ff1Sjsg REG64_BASE_IDX(GEN8_RING_CS_GPR, RENDER_RING_BASE, 5),
6571bb76ff1Sjsg REG64_BASE_IDX(GEN8_RING_CS_GPR, RENDER_RING_BASE, 6),
6581bb76ff1Sjsg REG64_BASE_IDX(GEN8_RING_CS_GPR, RENDER_RING_BASE, 7),
6591bb76ff1Sjsg REG64_BASE_IDX(GEN8_RING_CS_GPR, RENDER_RING_BASE, 8),
6601bb76ff1Sjsg REG64_BASE_IDX(GEN8_RING_CS_GPR, RENDER_RING_BASE, 9),
6611bb76ff1Sjsg REG64_BASE_IDX(GEN8_RING_CS_GPR, RENDER_RING_BASE, 10),
6621bb76ff1Sjsg REG64_BASE_IDX(GEN8_RING_CS_GPR, RENDER_RING_BASE, 11),
6631bb76ff1Sjsg REG64_BASE_IDX(GEN8_RING_CS_GPR, RENDER_RING_BASE, 12),
6641bb76ff1Sjsg REG64_BASE_IDX(GEN8_RING_CS_GPR, RENDER_RING_BASE, 13),
6651bb76ff1Sjsg REG64_BASE_IDX(GEN8_RING_CS_GPR, RENDER_RING_BASE, 14),
6661bb76ff1Sjsg REG64_BASE_IDX(GEN8_RING_CS_GPR, RENDER_RING_BASE, 15),
6673253c27bSkettenis REG32(HSW_SCRATCH1,
6683253c27bSkettenis .mask = ~HSW_SCRATCH1_L3_DATA_ATOMICS_DISABLE,
6693253c27bSkettenis .value = 0),
6703253c27bSkettenis REG32(HSW_ROW_CHICKEN3,
6713253c27bSkettenis .mask = ~(HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE << 16 |
6723253c27bSkettenis HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE),
6733253c27bSkettenis .value = 0),
6743253c27bSkettenis };
6753253c27bSkettenis
6763253c27bSkettenis static const struct drm_i915_reg_descriptor gen7_blt_regs[] = {
6777f4dd379Sjsg REG64_IDX(RING_TIMESTAMP, RENDER_RING_BASE),
6787f4dd379Sjsg REG64_IDX(RING_TIMESTAMP, BSD_RING_BASE),
6793253c27bSkettenis REG32(BCS_SWCTRL),
6807f4dd379Sjsg REG64_IDX(RING_TIMESTAMP, BLT_RING_BASE),
6813253c27bSkettenis };
6823253c27bSkettenis
6839a2084e7Sjsg static const struct drm_i915_reg_descriptor gen9_blt_regs[] = {
6849a2084e7Sjsg REG64_IDX(RING_TIMESTAMP, RENDER_RING_BASE),
6859a2084e7Sjsg REG64_IDX(RING_TIMESTAMP, BSD_RING_BASE),
6869a2084e7Sjsg REG32(BCS_SWCTRL),
6879a2084e7Sjsg REG64_IDX(RING_TIMESTAMP, BLT_RING_BASE),
688e80f4449Sjsg REG32_IDX(RING_CTX_TIMESTAMP, BLT_RING_BASE),
6891bb76ff1Sjsg REG64_BASE_IDX(GEN8_RING_CS_GPR, BLT_RING_BASE, 0),
6901bb76ff1Sjsg REG64_BASE_IDX(GEN8_RING_CS_GPR, BLT_RING_BASE, 1),
6911bb76ff1Sjsg REG64_BASE_IDX(GEN8_RING_CS_GPR, BLT_RING_BASE, 2),
6921bb76ff1Sjsg REG64_BASE_IDX(GEN8_RING_CS_GPR, BLT_RING_BASE, 3),
6931bb76ff1Sjsg REG64_BASE_IDX(GEN8_RING_CS_GPR, BLT_RING_BASE, 4),
6941bb76ff1Sjsg REG64_BASE_IDX(GEN8_RING_CS_GPR, BLT_RING_BASE, 5),
6951bb76ff1Sjsg REG64_BASE_IDX(GEN8_RING_CS_GPR, BLT_RING_BASE, 6),
6961bb76ff1Sjsg REG64_BASE_IDX(GEN8_RING_CS_GPR, BLT_RING_BASE, 7),
6971bb76ff1Sjsg REG64_BASE_IDX(GEN8_RING_CS_GPR, BLT_RING_BASE, 8),
6981bb76ff1Sjsg REG64_BASE_IDX(GEN8_RING_CS_GPR, BLT_RING_BASE, 9),
6991bb76ff1Sjsg REG64_BASE_IDX(GEN8_RING_CS_GPR, BLT_RING_BASE, 10),
7001bb76ff1Sjsg REG64_BASE_IDX(GEN8_RING_CS_GPR, BLT_RING_BASE, 11),
7011bb76ff1Sjsg REG64_BASE_IDX(GEN8_RING_CS_GPR, BLT_RING_BASE, 12),
7021bb76ff1Sjsg REG64_BASE_IDX(GEN8_RING_CS_GPR, BLT_RING_BASE, 13),
7031bb76ff1Sjsg REG64_BASE_IDX(GEN8_RING_CS_GPR, BLT_RING_BASE, 14),
7041bb76ff1Sjsg REG64_BASE_IDX(GEN8_RING_CS_GPR, BLT_RING_BASE, 15),
7059a2084e7Sjsg };
7069a2084e7Sjsg
7073253c27bSkettenis #undef REG64
7083253c27bSkettenis #undef REG32
7093253c27bSkettenis
7107f4dd379Sjsg struct drm_i915_reg_table {
7117f4dd379Sjsg const struct drm_i915_reg_descriptor *regs;
7127f4dd379Sjsg int num_regs;
7137f4dd379Sjsg };
7147f4dd379Sjsg
7157f4dd379Sjsg static const struct drm_i915_reg_table ivb_render_reg_tables[] = {
716f59a1707Sjsg { gen7_render_regs, ARRAY_SIZE(gen7_render_regs) },
7177f4dd379Sjsg };
7187f4dd379Sjsg
7197f4dd379Sjsg static const struct drm_i915_reg_table ivb_blt_reg_tables[] = {
720f59a1707Sjsg { gen7_blt_regs, ARRAY_SIZE(gen7_blt_regs) },
7217f4dd379Sjsg };
7227f4dd379Sjsg
7237f4dd379Sjsg static const struct drm_i915_reg_table hsw_render_reg_tables[] = {
724f59a1707Sjsg { gen7_render_regs, ARRAY_SIZE(gen7_render_regs) },
725f59a1707Sjsg { hsw_render_regs, ARRAY_SIZE(hsw_render_regs) },
7267f4dd379Sjsg };
7277f4dd379Sjsg
7287f4dd379Sjsg static const struct drm_i915_reg_table hsw_blt_reg_tables[] = {
729f59a1707Sjsg { gen7_blt_regs, ARRAY_SIZE(gen7_blt_regs) },
7307f4dd379Sjsg };
7317f4dd379Sjsg
7329a2084e7Sjsg static const struct drm_i915_reg_table gen9_blt_reg_tables[] = {
7339a2084e7Sjsg { gen9_blt_regs, ARRAY_SIZE(gen9_blt_regs) },
7349a2084e7Sjsg };
7359a2084e7Sjsg
gen7_render_get_cmd_length_mask(u32 cmd_header)7363253c27bSkettenis static u32 gen7_render_get_cmd_length_mask(u32 cmd_header)
7373253c27bSkettenis {
7387f4dd379Sjsg u32 client = cmd_header >> INSTR_CLIENT_SHIFT;
7393253c27bSkettenis u32 subclient =
7403253c27bSkettenis (cmd_header & INSTR_SUBCLIENT_MASK) >> INSTR_SUBCLIENT_SHIFT;
7413253c27bSkettenis
7423253c27bSkettenis if (client == INSTR_MI_CLIENT)
7433253c27bSkettenis return 0x3F;
7443253c27bSkettenis else if (client == INSTR_RC_CLIENT) {
7453253c27bSkettenis if (subclient == INSTR_MEDIA_SUBCLIENT)
7463253c27bSkettenis return 0xFFFF;
7473253c27bSkettenis else
7483253c27bSkettenis return 0xFF;
7493253c27bSkettenis }
7503253c27bSkettenis
751c349dbc7Sjsg DRM_DEBUG("CMD: Abnormal rcs cmd length! 0x%08X\n", cmd_header);
7523253c27bSkettenis return 0;
7533253c27bSkettenis }
7543253c27bSkettenis
gen7_bsd_get_cmd_length_mask(u32 cmd_header)7553253c27bSkettenis static u32 gen7_bsd_get_cmd_length_mask(u32 cmd_header)
7563253c27bSkettenis {
7577f4dd379Sjsg u32 client = cmd_header >> INSTR_CLIENT_SHIFT;
7583253c27bSkettenis u32 subclient =
7593253c27bSkettenis (cmd_header & INSTR_SUBCLIENT_MASK) >> INSTR_SUBCLIENT_SHIFT;
7603253c27bSkettenis u32 op = (cmd_header & INSTR_26_TO_24_MASK) >> INSTR_26_TO_24_SHIFT;
7613253c27bSkettenis
7623253c27bSkettenis if (client == INSTR_MI_CLIENT)
7633253c27bSkettenis return 0x3F;
7643253c27bSkettenis else if (client == INSTR_RC_CLIENT) {
7653253c27bSkettenis if (subclient == INSTR_MEDIA_SUBCLIENT) {
7663253c27bSkettenis if (op == 6)
7673253c27bSkettenis return 0xFFFF;
7683253c27bSkettenis else
7693253c27bSkettenis return 0xFFF;
7703253c27bSkettenis } else
7713253c27bSkettenis return 0xFF;
7723253c27bSkettenis }
7733253c27bSkettenis
774c349dbc7Sjsg DRM_DEBUG("CMD: Abnormal bsd cmd length! 0x%08X\n", cmd_header);
7753253c27bSkettenis return 0;
7763253c27bSkettenis }
7773253c27bSkettenis
gen7_blt_get_cmd_length_mask(u32 cmd_header)7783253c27bSkettenis static u32 gen7_blt_get_cmd_length_mask(u32 cmd_header)
7793253c27bSkettenis {
7807f4dd379Sjsg u32 client = cmd_header >> INSTR_CLIENT_SHIFT;
7813253c27bSkettenis
7823253c27bSkettenis if (client == INSTR_MI_CLIENT)
7833253c27bSkettenis return 0x3F;
7843253c27bSkettenis else if (client == INSTR_BC_CLIENT)
7853253c27bSkettenis return 0xFF;
7863253c27bSkettenis
787c349dbc7Sjsg DRM_DEBUG("CMD: Abnormal blt cmd length! 0x%08X\n", cmd_header);
7883253c27bSkettenis return 0;
7893253c27bSkettenis }
7903253c27bSkettenis
gen9_blt_get_cmd_length_mask(u32 cmd_header)7919a2084e7Sjsg static u32 gen9_blt_get_cmd_length_mask(u32 cmd_header)
7929a2084e7Sjsg {
7939a2084e7Sjsg u32 client = cmd_header >> INSTR_CLIENT_SHIFT;
7949a2084e7Sjsg
7959a2084e7Sjsg if (client == INSTR_MI_CLIENT || client == INSTR_BC_CLIENT)
7969a2084e7Sjsg return 0xFF;
7979a2084e7Sjsg
798c349dbc7Sjsg DRM_DEBUG("CMD: Abnormal blt cmd length! 0x%08X\n", cmd_header);
7999a2084e7Sjsg return 0;
8009a2084e7Sjsg }
8019a2084e7Sjsg
validate_cmds_sorted(const struct intel_engine_cs * engine,const struct drm_i915_cmd_table * cmd_tables,int cmd_table_count)8027f4dd379Sjsg static bool validate_cmds_sorted(const struct intel_engine_cs *engine,
8033253c27bSkettenis const struct drm_i915_cmd_table *cmd_tables,
8043253c27bSkettenis int cmd_table_count)
8053253c27bSkettenis {
8063253c27bSkettenis int i;
8073253c27bSkettenis bool ret = true;
8083253c27bSkettenis
8093253c27bSkettenis if (!cmd_tables || cmd_table_count == 0)
8103253c27bSkettenis return true;
8113253c27bSkettenis
8123253c27bSkettenis for (i = 0; i < cmd_table_count; i++) {
8133253c27bSkettenis const struct drm_i915_cmd_table *table = &cmd_tables[i];
8143253c27bSkettenis u32 previous = 0;
8153253c27bSkettenis int j;
8163253c27bSkettenis
8173253c27bSkettenis for (j = 0; j < table->count; j++) {
8183253c27bSkettenis const struct drm_i915_cmd_descriptor *desc =
8193253c27bSkettenis &table->table[j];
8203253c27bSkettenis u32 curr = desc->cmd.value & desc->cmd.mask;
8213253c27bSkettenis
8223253c27bSkettenis if (curr < previous) {
823c349dbc7Sjsg drm_err(&engine->i915->drm,
824c349dbc7Sjsg "CMD: %s [%d] command table not sorted: "
8257f4dd379Sjsg "table=%d entry=%d cmd=0x%08X prev=0x%08X\n",
8267f4dd379Sjsg engine->name, engine->id,
8277f4dd379Sjsg i, j, curr, previous);
8283253c27bSkettenis ret = false;
8293253c27bSkettenis }
8303253c27bSkettenis
8313253c27bSkettenis previous = curr;
8323253c27bSkettenis }
8333253c27bSkettenis }
8343253c27bSkettenis
8353253c27bSkettenis return ret;
8363253c27bSkettenis }
8373253c27bSkettenis
check_sorted(const struct intel_engine_cs * engine,const struct drm_i915_reg_descriptor * reg_table,int reg_count)8387f4dd379Sjsg static bool check_sorted(const struct intel_engine_cs *engine,
8393253c27bSkettenis const struct drm_i915_reg_descriptor *reg_table,
8403253c27bSkettenis int reg_count)
8413253c27bSkettenis {
8423253c27bSkettenis int i;
8433253c27bSkettenis u32 previous = 0;
8443253c27bSkettenis bool ret = true;
8453253c27bSkettenis
8463253c27bSkettenis for (i = 0; i < reg_count; i++) {
8477f4dd379Sjsg u32 curr = i915_mmio_reg_offset(reg_table[i].addr);
8483253c27bSkettenis
8493253c27bSkettenis if (curr < previous) {
850c349dbc7Sjsg drm_err(&engine->i915->drm,
851c349dbc7Sjsg "CMD: %s [%d] register table not sorted: "
8527f4dd379Sjsg "entry=%d reg=0x%08X prev=0x%08X\n",
8537f4dd379Sjsg engine->name, engine->id,
8547f4dd379Sjsg i, curr, previous);
8553253c27bSkettenis ret = false;
8563253c27bSkettenis }
8573253c27bSkettenis
8583253c27bSkettenis previous = curr;
8593253c27bSkettenis }
8603253c27bSkettenis
8613253c27bSkettenis return ret;
8623253c27bSkettenis }
8633253c27bSkettenis
validate_regs_sorted(struct intel_engine_cs * engine)8647f4dd379Sjsg static bool validate_regs_sorted(struct intel_engine_cs *engine)
8653253c27bSkettenis {
8667f4dd379Sjsg int i;
8677f4dd379Sjsg const struct drm_i915_reg_table *table;
8687f4dd379Sjsg
8697f4dd379Sjsg for (i = 0; i < engine->reg_table_count; i++) {
8707f4dd379Sjsg table = &engine->reg_tables[i];
8717f4dd379Sjsg if (!check_sorted(engine, table->regs, table->num_regs))
8727f4dd379Sjsg return false;
8737f4dd379Sjsg }
8747f4dd379Sjsg
8757f4dd379Sjsg return true;
8763253c27bSkettenis }
8773253c27bSkettenis
8783253c27bSkettenis struct cmd_node {
8793253c27bSkettenis const struct drm_i915_cmd_descriptor *desc;
8803253c27bSkettenis struct hlist_node node;
8813253c27bSkettenis };
8823253c27bSkettenis
8833253c27bSkettenis /*
8843253c27bSkettenis * Different command ranges have different numbers of bits for the opcode. For
8853253c27bSkettenis * example, MI commands use bits 31:23 while 3D commands use bits 31:16. The
8863253c27bSkettenis * problem is that, for example, MI commands use bits 22:16 for other fields
8873253c27bSkettenis * such as GGTT vs PPGTT bits. If we include those bits in the mask then when
8883253c27bSkettenis * we mask a command from a batch it could hash to the wrong bucket due to
8893253c27bSkettenis * non-opcode bits being set. But if we don't include those bits, some 3D
8903253c27bSkettenis * commands may hash to the same bucket due to not including opcode bits that
8913253c27bSkettenis * make the command unique. For now, we will risk hashing to the same bucket.
8923253c27bSkettenis */
cmd_header_key(u32 x)8937f4dd379Sjsg static inline u32 cmd_header_key(u32 x)
8947f4dd379Sjsg {
8957f4dd379Sjsg switch (x >> INSTR_CLIENT_SHIFT) {
8967f4dd379Sjsg default:
8977f4dd379Sjsg case INSTR_MI_CLIENT:
8987f4dd379Sjsg return x >> STD_MI_OPCODE_SHIFT;
8997f4dd379Sjsg case INSTR_RC_CLIENT:
9007f4dd379Sjsg return x >> STD_3D_OPCODE_SHIFT;
9017f4dd379Sjsg case INSTR_BC_CLIENT:
9027f4dd379Sjsg return x >> STD_2D_OPCODE_SHIFT;
9037f4dd379Sjsg }
9047f4dd379Sjsg }
9053253c27bSkettenis
init_hash_table(struct intel_engine_cs * engine,const struct drm_i915_cmd_table * cmd_tables,int cmd_table_count)9067f4dd379Sjsg static int init_hash_table(struct intel_engine_cs *engine,
9073253c27bSkettenis const struct drm_i915_cmd_table *cmd_tables,
9083253c27bSkettenis int cmd_table_count)
9093253c27bSkettenis {
9103253c27bSkettenis int i, j;
9113253c27bSkettenis
9127f4dd379Sjsg hash_init(engine->cmd_hash);
9133253c27bSkettenis
9143253c27bSkettenis for (i = 0; i < cmd_table_count; i++) {
9153253c27bSkettenis const struct drm_i915_cmd_table *table = &cmd_tables[i];
9163253c27bSkettenis
9173253c27bSkettenis for (j = 0; j < table->count; j++) {
9183253c27bSkettenis const struct drm_i915_cmd_descriptor *desc =
9193253c27bSkettenis &table->table[j];
9203253c27bSkettenis struct cmd_node *desc_node =
9213253c27bSkettenis kmalloc(sizeof(*desc_node), GFP_KERNEL);
9223253c27bSkettenis
9233253c27bSkettenis if (!desc_node)
9243253c27bSkettenis return -ENOMEM;
9253253c27bSkettenis
9263253c27bSkettenis desc_node->desc = desc;
9277f4dd379Sjsg hash_add(engine->cmd_hash, &desc_node->node,
9287f4dd379Sjsg cmd_header_key(desc->cmd.value));
9293253c27bSkettenis }
9303253c27bSkettenis }
9313253c27bSkettenis
9323253c27bSkettenis return 0;
9333253c27bSkettenis }
9343253c27bSkettenis
fini_hash_table(struct intel_engine_cs * engine)9357f4dd379Sjsg static void fini_hash_table(struct intel_engine_cs *engine)
9363253c27bSkettenis {
9373253c27bSkettenis struct hlist_node *tmp;
9383253c27bSkettenis struct cmd_node *desc_node;
9393253c27bSkettenis int i;
9403253c27bSkettenis
9417f4dd379Sjsg hash_for_each_safe(engine->cmd_hash, i, tmp, desc_node, node) {
9423253c27bSkettenis hash_del(&desc_node->node);
9433253c27bSkettenis kfree(desc_node);
9443253c27bSkettenis }
9453253c27bSkettenis }
9463253c27bSkettenis
9473253c27bSkettenis /**
9487f4dd379Sjsg * intel_engine_init_cmd_parser() - set cmd parser related fields for an engine
9497f4dd379Sjsg * @engine: the engine to initialize
9503253c27bSkettenis *
9513253c27bSkettenis * Optionally initializes fields related to batch buffer command parsing in the
9523253c27bSkettenis * struct intel_engine_cs based on whether the platform requires software
9533253c27bSkettenis * command parsing.
9543253c27bSkettenis */
intel_engine_init_cmd_parser(struct intel_engine_cs * engine)955ad8b1aafSjsg int intel_engine_init_cmd_parser(struct intel_engine_cs *engine)
9563253c27bSkettenis {
9573253c27bSkettenis const struct drm_i915_cmd_table *cmd_tables;
9583253c27bSkettenis int cmd_table_count;
9593253c27bSkettenis int ret;
9603253c27bSkettenis
9615ca02815Sjsg if (GRAPHICS_VER(engine->i915) != 7 && !(GRAPHICS_VER(engine->i915) == 9 &&
962c349dbc7Sjsg engine->class == COPY_ENGINE_CLASS))
963ad8b1aafSjsg return 0;
9643253c27bSkettenis
965c349dbc7Sjsg switch (engine->class) {
966c349dbc7Sjsg case RENDER_CLASS:
9677f4dd379Sjsg if (IS_HASWELL(engine->i915)) {
9683f92f658Sjsg cmd_tables = hsw_render_ring_cmd_table;
9693253c27bSkettenis cmd_table_count =
9703f92f658Sjsg ARRAY_SIZE(hsw_render_ring_cmd_table);
9713253c27bSkettenis } else {
9723f92f658Sjsg cmd_tables = gen7_render_cmd_table;
9733f92f658Sjsg cmd_table_count = ARRAY_SIZE(gen7_render_cmd_table);
9743253c27bSkettenis }
9753253c27bSkettenis
9767f4dd379Sjsg if (IS_HASWELL(engine->i915)) {
9777f4dd379Sjsg engine->reg_tables = hsw_render_reg_tables;
9787f4dd379Sjsg engine->reg_table_count = ARRAY_SIZE(hsw_render_reg_tables);
9793253c27bSkettenis } else {
9807f4dd379Sjsg engine->reg_tables = ivb_render_reg_tables;
9817f4dd379Sjsg engine->reg_table_count = ARRAY_SIZE(ivb_render_reg_tables);
9823253c27bSkettenis }
9837f4dd379Sjsg engine->get_cmd_length_mask = gen7_render_get_cmd_length_mask;
9843253c27bSkettenis break;
985c349dbc7Sjsg case VIDEO_DECODE_CLASS:
9863f92f658Sjsg cmd_tables = gen7_video_cmd_table;
9873f92f658Sjsg cmd_table_count = ARRAY_SIZE(gen7_video_cmd_table);
9887f4dd379Sjsg engine->get_cmd_length_mask = gen7_bsd_get_cmd_length_mask;
9893253c27bSkettenis break;
990c349dbc7Sjsg case COPY_ENGINE_CLASS:
9919a2084e7Sjsg engine->get_cmd_length_mask = gen7_blt_get_cmd_length_mask;
9925ca02815Sjsg if (GRAPHICS_VER(engine->i915) == 9) {
9939a2084e7Sjsg cmd_tables = gen9_blt_cmd_table;
9949a2084e7Sjsg cmd_table_count = ARRAY_SIZE(gen9_blt_cmd_table);
9959a2084e7Sjsg engine->get_cmd_length_mask =
9969a2084e7Sjsg gen9_blt_get_cmd_length_mask;
9979a2084e7Sjsg
9989a2084e7Sjsg /* BCS Engine unsafe without parser */
9999a2084e7Sjsg engine->flags |= I915_ENGINE_REQUIRES_CMD_PARSER;
10009a2084e7Sjsg } else if (IS_HASWELL(engine->i915)) {
10013f92f658Sjsg cmd_tables = hsw_blt_ring_cmd_table;
10023f92f658Sjsg cmd_table_count = ARRAY_SIZE(hsw_blt_ring_cmd_table);
10033253c27bSkettenis } else {
10043f92f658Sjsg cmd_tables = gen7_blt_cmd_table;
10053f92f658Sjsg cmd_table_count = ARRAY_SIZE(gen7_blt_cmd_table);
10063253c27bSkettenis }
10073253c27bSkettenis
10085ca02815Sjsg if (GRAPHICS_VER(engine->i915) == 9) {
10099a2084e7Sjsg engine->reg_tables = gen9_blt_reg_tables;
10109a2084e7Sjsg engine->reg_table_count =
10119a2084e7Sjsg ARRAY_SIZE(gen9_blt_reg_tables);
10129a2084e7Sjsg } else if (IS_HASWELL(engine->i915)) {
10137f4dd379Sjsg engine->reg_tables = hsw_blt_reg_tables;
10147f4dd379Sjsg engine->reg_table_count = ARRAY_SIZE(hsw_blt_reg_tables);
10153253c27bSkettenis } else {
10167f4dd379Sjsg engine->reg_tables = ivb_blt_reg_tables;
10177f4dd379Sjsg engine->reg_table_count = ARRAY_SIZE(ivb_blt_reg_tables);
10183253c27bSkettenis }
10193253c27bSkettenis break;
1020c349dbc7Sjsg case VIDEO_ENHANCEMENT_CLASS:
10213f92f658Sjsg cmd_tables = hsw_vebox_cmd_table;
10223f92f658Sjsg cmd_table_count = ARRAY_SIZE(hsw_vebox_cmd_table);
10233253c27bSkettenis /* VECS can use the same length_mask function as VCS */
10247f4dd379Sjsg engine->get_cmd_length_mask = gen7_bsd_get_cmd_length_mask;
10253253c27bSkettenis break;
10263253c27bSkettenis default:
1027c349dbc7Sjsg MISSING_CASE(engine->class);
1028ad8b1aafSjsg goto out;
10293253c27bSkettenis }
10303253c27bSkettenis
10317f4dd379Sjsg if (!validate_cmds_sorted(engine, cmd_tables, cmd_table_count)) {
1032c349dbc7Sjsg drm_err(&engine->i915->drm,
1033c349dbc7Sjsg "%s: command descriptions are not sorted\n",
10347f4dd379Sjsg engine->name);
1035ad8b1aafSjsg goto out;
10367f4dd379Sjsg }
10377f4dd379Sjsg if (!validate_regs_sorted(engine)) {
1038c349dbc7Sjsg drm_err(&engine->i915->drm,
1039c349dbc7Sjsg "%s: registers are not sorted\n", engine->name);
1040ad8b1aafSjsg goto out;
10417f4dd379Sjsg }
10423253c27bSkettenis
10437f4dd379Sjsg ret = init_hash_table(engine, cmd_tables, cmd_table_count);
10443253c27bSkettenis if (ret) {
1045c349dbc7Sjsg drm_err(&engine->i915->drm,
1046c349dbc7Sjsg "%s: initialised failed!\n", engine->name);
10477f4dd379Sjsg fini_hash_table(engine);
1048ad8b1aafSjsg goto out;
10493253c27bSkettenis }
10503253c27bSkettenis
10512f1f75dbSjsg engine->flags |= I915_ENGINE_USING_CMD_PARSER;
1052ad8b1aafSjsg
1053ad8b1aafSjsg out:
1054ad8b1aafSjsg if (intel_engine_requires_cmd_parser(engine) &&
1055ad8b1aafSjsg !intel_engine_using_cmd_parser(engine))
1056ad8b1aafSjsg return -EINVAL;
1057ad8b1aafSjsg
1058ad8b1aafSjsg return 0;
10593253c27bSkettenis }
10603253c27bSkettenis
10613253c27bSkettenis /**
10627f4dd379Sjsg * intel_engine_cleanup_cmd_parser() - clean up cmd parser related fields
10637f4dd379Sjsg * @engine: the engine to clean up
10643253c27bSkettenis *
10653253c27bSkettenis * Releases any resources related to command parsing that may have been
10667f4dd379Sjsg * initialized for the specified engine.
10673253c27bSkettenis */
intel_engine_cleanup_cmd_parser(struct intel_engine_cs * engine)10687f4dd379Sjsg void intel_engine_cleanup_cmd_parser(struct intel_engine_cs *engine)
10693253c27bSkettenis {
10702f1f75dbSjsg if (!intel_engine_using_cmd_parser(engine))
10713253c27bSkettenis return;
10723253c27bSkettenis
10737f4dd379Sjsg fini_hash_table(engine);
10743253c27bSkettenis }
10753253c27bSkettenis
10763253c27bSkettenis static const struct drm_i915_cmd_descriptor*
find_cmd_in_table(struct intel_engine_cs * engine,u32 cmd_header)10777f4dd379Sjsg find_cmd_in_table(struct intel_engine_cs *engine,
10783253c27bSkettenis u32 cmd_header)
10793253c27bSkettenis {
10803253c27bSkettenis struct cmd_node *desc_node;
10813253c27bSkettenis
10827f4dd379Sjsg hash_for_each_possible(engine->cmd_hash, desc_node, node,
10837f4dd379Sjsg cmd_header_key(cmd_header)) {
10843253c27bSkettenis const struct drm_i915_cmd_descriptor *desc = desc_node->desc;
10857f4dd379Sjsg if (((cmd_header ^ desc->cmd.value) & desc->cmd.mask) == 0)
10863253c27bSkettenis return desc;
10873253c27bSkettenis }
10883253c27bSkettenis
10893253c27bSkettenis return NULL;
10903253c27bSkettenis }
10913253c27bSkettenis
10923253c27bSkettenis /*
10933253c27bSkettenis * Returns a pointer to a descriptor for the command specified by cmd_header.
10943253c27bSkettenis *
10953253c27bSkettenis * The caller must supply space for a default descriptor via the default_desc
10967f4dd379Sjsg * parameter. If no descriptor for the specified command exists in the engine's
10973253c27bSkettenis * command parser tables, this function fills in default_desc based on the
10987f4dd379Sjsg * engine's default length encoding and returns default_desc.
10993253c27bSkettenis */
11003253c27bSkettenis 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)11017f4dd379Sjsg find_cmd(struct intel_engine_cs *engine,
11023253c27bSkettenis u32 cmd_header,
11037f4dd379Sjsg const struct drm_i915_cmd_descriptor *desc,
11043253c27bSkettenis struct drm_i915_cmd_descriptor *default_desc)
11053253c27bSkettenis {
11063253c27bSkettenis u32 mask;
11073253c27bSkettenis
11087f4dd379Sjsg if (((cmd_header ^ desc->cmd.value) & desc->cmd.mask) == 0)
11097f4dd379Sjsg return desc;
11107f4dd379Sjsg
11117f4dd379Sjsg desc = find_cmd_in_table(engine, cmd_header);
11123253c27bSkettenis if (desc)
11133253c27bSkettenis return desc;
11143253c27bSkettenis
11157f4dd379Sjsg mask = engine->get_cmd_length_mask(cmd_header);
11163253c27bSkettenis if (!mask)
11173253c27bSkettenis return NULL;
11183253c27bSkettenis
11197f4dd379Sjsg default_desc->cmd.value = cmd_header;
11207f4dd379Sjsg default_desc->cmd.mask = ~0u << MIN_OPCODE_SHIFT;
11213253c27bSkettenis default_desc->length.mask = mask;
11227f4dd379Sjsg default_desc->flags = CMD_DESC_SKIP;
11233253c27bSkettenis return default_desc;
11243253c27bSkettenis }
11253253c27bSkettenis
11263253c27bSkettenis static const struct drm_i915_reg_descriptor *
__find_reg(const struct drm_i915_reg_descriptor * table,int count,u32 addr)11277f4dd379Sjsg __find_reg(const struct drm_i915_reg_descriptor *table, int count, u32 addr)
11283253c27bSkettenis {
11297f4dd379Sjsg int start = 0, end = count;
11307f4dd379Sjsg while (start < end) {
11317f4dd379Sjsg int mid = start + (end - start) / 2;
11327f4dd379Sjsg int ret = addr - i915_mmio_reg_offset(table[mid].addr);
11337f4dd379Sjsg if (ret < 0)
11347f4dd379Sjsg end = mid;
11357f4dd379Sjsg else if (ret > 0)
11367f4dd379Sjsg start = mid + 1;
11377f4dd379Sjsg else
11387f4dd379Sjsg return &table[mid];
11397f4dd379Sjsg }
11407f4dd379Sjsg return NULL;
11417f4dd379Sjsg }
11423253c27bSkettenis
11437f4dd379Sjsg static const struct drm_i915_reg_descriptor *
find_reg(const struct intel_engine_cs * engine,u32 addr)1144f59a1707Sjsg find_reg(const struct intel_engine_cs *engine, u32 addr)
11457f4dd379Sjsg {
11467f4dd379Sjsg const struct drm_i915_reg_table *table = engine->reg_tables;
1147f59a1707Sjsg const struct drm_i915_reg_descriptor *reg = NULL;
11487f4dd379Sjsg int count = engine->reg_table_count;
11497f4dd379Sjsg
1150f59a1707Sjsg for (; !reg && (count > 0); ++table, --count)
11517f4dd379Sjsg reg = __find_reg(table->regs, table->num_regs, addr);
11523253c27bSkettenis
1153f59a1707Sjsg return reg;
11543253c27bSkettenis }
11553253c27bSkettenis
11567f4dd379Sjsg /* 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,unsigned long offset,unsigned long length,bool * needs_clflush_after)11577f4dd379Sjsg static u32 *copy_batch(struct drm_i915_gem_object *dst_obj,
11583253c27bSkettenis struct drm_i915_gem_object *src_obj,
11595ca02815Sjsg unsigned long offset, unsigned long length,
11605ca02815Sjsg bool *needs_clflush_after)
11613253c27bSkettenis {
1162d9ace711Sjsg unsigned int src_needs_clflush;
1163d9ace711Sjsg unsigned int dst_needs_clflush;
11647f4dd379Sjsg void *dst, *src;
11653253c27bSkettenis int ret;
11663253c27bSkettenis
1167d9ace711Sjsg ret = i915_gem_object_prepare_write(dst_obj, &dst_needs_clflush);
1168d9ace711Sjsg if (ret)
1169d9ace711Sjsg return ERR_PTR(ret);
1170d9ace711Sjsg
11715ca02815Sjsg dst = i915_gem_object_pin_map(dst_obj, I915_MAP_WB);
1172d9ace711Sjsg i915_gem_object_finish_access(dst_obj);
11737f4dd379Sjsg if (IS_ERR(dst))
1174c349dbc7Sjsg return dst;
1175c349dbc7Sjsg
1176d9ace711Sjsg ret = i915_gem_object_prepare_read(src_obj, &src_needs_clflush);
1177c349dbc7Sjsg if (ret) {
1178c349dbc7Sjsg i915_gem_object_unpin_map(dst_obj);
1179c349dbc7Sjsg return ERR_PTR(ret);
1180c349dbc7Sjsg }
1181c349dbc7Sjsg
11827f4dd379Sjsg src = ERR_PTR(-ENODEV);
1183d9ace711Sjsg if (src_needs_clflush && i915_has_memcpy_from_wc()) {
11847f4dd379Sjsg src = i915_gem_object_pin_map(src_obj, I915_MAP_WC);
11857f4dd379Sjsg if (!IS_ERR(src)) {
1186c349dbc7Sjsg i915_unaligned_memcpy_from_wc(dst,
1187c349dbc7Sjsg src + offset,
1188c349dbc7Sjsg length);
11897f4dd379Sjsg i915_gem_object_unpin_map(src_obj);
11903253c27bSkettenis }
11913253c27bSkettenis }
11927f4dd379Sjsg if (IS_ERR(src)) {
1193ad8b1aafSjsg unsigned long x, n, remain;
11947f4dd379Sjsg void *ptr;
11953253c27bSkettenis
1196c349dbc7Sjsg /*
1197c349dbc7Sjsg * We can avoid clflushing partial cachelines before the write
11987f4dd379Sjsg * if we only every write full cache-lines. Since we know that
11997f4dd379Sjsg * both the source and destination are in multiples of
12007f4dd379Sjsg * PAGE_SIZE, we can simply round up to the next cacheline.
12017f4dd379Sjsg * We don't care about copying too much here as we only
12027f4dd379Sjsg * validate up to the end of the batch.
12033253c27bSkettenis */
1204ad8b1aafSjsg remain = length;
1205d9ace711Sjsg if (dst_needs_clflush & CLFLUSH_BEFORE)
1206c349dbc7Sjsg #ifdef __linux__
1207ad8b1aafSjsg remain = round_up(remain,
1208c349dbc7Sjsg boot_cpu_data.x86_clflush_size);
1209c349dbc7Sjsg #else
1210ad8b1aafSjsg remain = round_up(remain,
12117f4dd379Sjsg curcpu()->ci_cflushsz);
1212c349dbc7Sjsg #endif
12133253c27bSkettenis
12147f4dd379Sjsg ptr = dst;
1215c349dbc7Sjsg x = offset_in_page(offset);
1216ad8b1aafSjsg for (n = offset >> PAGE_SHIFT; remain; n++) {
1217ad8b1aafSjsg int len = min(remain, PAGE_SIZE - x);
12183253c27bSkettenis
12197f4dd379Sjsg src = kmap_atomic(i915_gem_object_get_page(src_obj, n));
1220d9ace711Sjsg if (src_needs_clflush)
1221c349dbc7Sjsg drm_clflush_virt_range(src + x, len);
1222c349dbc7Sjsg memcpy(ptr, src + x, len);
12237f4dd379Sjsg kunmap_atomic(src);
12247f4dd379Sjsg
12257f4dd379Sjsg ptr += len;
1226ad8b1aafSjsg remain -= len;
1227c349dbc7Sjsg x = 0;
12287f4dd379Sjsg }
12293253c27bSkettenis }
12303253c27bSkettenis
1231d9ace711Sjsg i915_gem_object_finish_access(src_obj);
1232c349dbc7Sjsg
1233ad8b1aafSjsg memset32(dst + length, 0, (dst_obj->base.size - length) / sizeof(u32));
1234ad8b1aafSjsg
12355ca02815Sjsg /* dst_obj is returned with vmap pinned */
12365ca02815Sjsg *needs_clflush_after = dst_needs_clflush & CLFLUSH_AFTER;
12375ca02815Sjsg
12387f4dd379Sjsg return dst;
12397f4dd379Sjsg }
12407f4dd379Sjsg
cmd_desc_is(const struct drm_i915_cmd_descriptor * const desc,const u32 cmd)1241ad8b1aafSjsg static inline bool cmd_desc_is(const struct drm_i915_cmd_descriptor * const desc,
1242ad8b1aafSjsg const u32 cmd)
1243ad8b1aafSjsg {
1244ad8b1aafSjsg return desc->cmd.value == (cmd & desc->cmd.mask);
1245ad8b1aafSjsg }
1246ad8b1aafSjsg
check_cmd(const struct intel_engine_cs * engine,const struct drm_i915_cmd_descriptor * desc,const u32 * cmd,u32 length)12477f4dd379Sjsg static bool check_cmd(const struct intel_engine_cs *engine,
12483253c27bSkettenis const struct drm_i915_cmd_descriptor *desc,
1249f59a1707Sjsg const u32 *cmd, u32 length)
12503253c27bSkettenis {
12517f4dd379Sjsg if (desc->flags & CMD_DESC_SKIP)
12527f4dd379Sjsg return true;
12537f4dd379Sjsg
12543253c27bSkettenis if (desc->flags & CMD_DESC_REJECT) {
1255c349dbc7Sjsg DRM_DEBUG("CMD: Rejected command: 0x%08X\n", *cmd);
12563253c27bSkettenis return false;
12573253c27bSkettenis }
12583253c27bSkettenis
12593253c27bSkettenis if (desc->flags & CMD_DESC_REGISTER) {
12603253c27bSkettenis /*
12613253c27bSkettenis * Get the distance between individual register offset
12623253c27bSkettenis * fields if the command can perform more than one
12633253c27bSkettenis * access at a time.
12643253c27bSkettenis */
12653253c27bSkettenis const u32 step = desc->reg.step ? desc->reg.step : length;
12663253c27bSkettenis u32 offset;
12673253c27bSkettenis
12683253c27bSkettenis for (offset = desc->reg.offset; offset < length;
12693253c27bSkettenis offset += step) {
12703253c27bSkettenis const u32 reg_addr = cmd[offset] & desc->reg.mask;
12713253c27bSkettenis const struct drm_i915_reg_descriptor *reg =
1272f59a1707Sjsg find_reg(engine, reg_addr);
12733253c27bSkettenis
12743253c27bSkettenis if (!reg) {
1275c349dbc7Sjsg DRM_DEBUG("CMD: Rejected register 0x%08X in command: 0x%08X (%s)\n",
12767f4dd379Sjsg reg_addr, *cmd, engine->name);
12773253c27bSkettenis return false;
12783253c27bSkettenis }
12793253c27bSkettenis
12803253c27bSkettenis /*
12813253c27bSkettenis * Check the value written to the register against the
12823253c27bSkettenis * allowed mask/value pair given in the whitelist entry.
12833253c27bSkettenis */
12843253c27bSkettenis if (reg->mask) {
1285ad8b1aafSjsg if (cmd_desc_is(desc, MI_LOAD_REGISTER_MEM)) {
1286c349dbc7Sjsg DRM_DEBUG("CMD: Rejected LRM to masked register 0x%08X\n",
12873253c27bSkettenis reg_addr);
12883253c27bSkettenis return false;
12893253c27bSkettenis }
12903253c27bSkettenis
1291ad8b1aafSjsg if (cmd_desc_is(desc, MI_LOAD_REGISTER_REG)) {
1292c349dbc7Sjsg DRM_DEBUG("CMD: Rejected LRR to masked register 0x%08X\n",
12937f4dd379Sjsg reg_addr);
12947f4dd379Sjsg return false;
12957f4dd379Sjsg }
12967f4dd379Sjsg
1297ad8b1aafSjsg if (cmd_desc_is(desc, MI_LOAD_REGISTER_IMM(1)) &&
12983253c27bSkettenis (offset + 2 > length ||
12993253c27bSkettenis (cmd[offset + 1] & reg->mask) != reg->value)) {
1300c349dbc7Sjsg DRM_DEBUG("CMD: Rejected LRI to masked register 0x%08X\n",
13013253c27bSkettenis reg_addr);
13023253c27bSkettenis return false;
13033253c27bSkettenis }
13043253c27bSkettenis }
13053253c27bSkettenis }
13063253c27bSkettenis }
13073253c27bSkettenis
13083253c27bSkettenis if (desc->flags & CMD_DESC_BITMASK) {
13093253c27bSkettenis int i;
13103253c27bSkettenis
13113253c27bSkettenis for (i = 0; i < MAX_CMD_DESC_BITMASKS; i++) {
13123253c27bSkettenis u32 dword;
13133253c27bSkettenis
13143253c27bSkettenis if (desc->bits[i].mask == 0)
13153253c27bSkettenis break;
13163253c27bSkettenis
13173253c27bSkettenis if (desc->bits[i].condition_mask != 0) {
13183253c27bSkettenis u32 offset =
13193253c27bSkettenis desc->bits[i].condition_offset;
13203253c27bSkettenis u32 condition = cmd[offset] &
13213253c27bSkettenis desc->bits[i].condition_mask;
13223253c27bSkettenis
13233253c27bSkettenis if (condition == 0)
13243253c27bSkettenis continue;
13253253c27bSkettenis }
13263253c27bSkettenis
13277f4dd379Sjsg if (desc->bits[i].offset >= length) {
1328c349dbc7Sjsg DRM_DEBUG("CMD: Rejected command 0x%08X, too short to check bitmask (%s)\n",
13297f4dd379Sjsg *cmd, engine->name);
13307f4dd379Sjsg return false;
13317f4dd379Sjsg }
13327f4dd379Sjsg
13333253c27bSkettenis dword = cmd[desc->bits[i].offset] &
13343253c27bSkettenis desc->bits[i].mask;
13353253c27bSkettenis
13363253c27bSkettenis if (dword != desc->bits[i].expected) {
1337c349dbc7Sjsg DRM_DEBUG("CMD: Rejected command 0x%08X for bitmask 0x%08X (exp=0x%08X act=0x%08X) (%s)\n",
13383253c27bSkettenis *cmd,
13393253c27bSkettenis desc->bits[i].mask,
13403253c27bSkettenis desc->bits[i].expected,
13417f4dd379Sjsg dword, engine->name);
13423253c27bSkettenis return false;
13433253c27bSkettenis }
13443253c27bSkettenis }
13453253c27bSkettenis }
13463253c27bSkettenis
13473253c27bSkettenis return true;
13483253c27bSkettenis }
13493253c27bSkettenis
check_bbstart(u32 * cmd,u32 offset,u32 length,u32 batch_length,u64 batch_addr,u64 shadow_addr,const unsigned long * jump_whitelist)1350c349dbc7Sjsg static int check_bbstart(u32 *cmd, u32 offset, u32 length,
1351c349dbc7Sjsg u32 batch_length,
1352c349dbc7Sjsg u64 batch_addr,
1353c349dbc7Sjsg u64 shadow_addr,
1354c349dbc7Sjsg const unsigned long *jump_whitelist)
1355466dcc0aSjsg {
1356466dcc0aSjsg u64 jump_offset, jump_target;
1357466dcc0aSjsg u32 target_cmd_offset, target_cmd_index;
1358466dcc0aSjsg
1359466dcc0aSjsg /* For igt compatibility on older platforms */
1360c349dbc7Sjsg if (!jump_whitelist) {
1361466dcc0aSjsg DRM_DEBUG("CMD: Rejecting BB_START for ggtt based submission\n");
1362466dcc0aSjsg return -EACCES;
1363466dcc0aSjsg }
1364466dcc0aSjsg
1365466dcc0aSjsg if (length != 3) {
1366466dcc0aSjsg DRM_DEBUG("CMD: Recursive BB_START with bad length(%u)\n",
1367466dcc0aSjsg length);
1368466dcc0aSjsg return -EINVAL;
1369466dcc0aSjsg }
1370466dcc0aSjsg
1371466dcc0aSjsg jump_target = *(u64 *)(cmd + 1);
1372c349dbc7Sjsg jump_offset = jump_target - batch_addr;
1373466dcc0aSjsg
1374466dcc0aSjsg /*
1375466dcc0aSjsg * Any underflow of jump_target is guaranteed to be outside the range
1376466dcc0aSjsg * of a u32, so >= test catches both too large and too small
1377466dcc0aSjsg */
1378c349dbc7Sjsg if (jump_offset >= batch_length) {
1379466dcc0aSjsg DRM_DEBUG("CMD: BB_START to 0x%llx jumps out of BB\n",
1380466dcc0aSjsg jump_target);
1381466dcc0aSjsg return -EINVAL;
1382466dcc0aSjsg }
1383466dcc0aSjsg
1384466dcc0aSjsg /*
1385466dcc0aSjsg * This cannot overflow a u32 because we already checked jump_offset
1386c349dbc7Sjsg * is within the BB, and the batch_length is a u32
1387466dcc0aSjsg */
1388466dcc0aSjsg target_cmd_offset = lower_32_bits(jump_offset);
1389466dcc0aSjsg target_cmd_index = target_cmd_offset / sizeof(u32);
1390466dcc0aSjsg
1391c349dbc7Sjsg *(u64 *)(cmd + 1) = shadow_addr + target_cmd_offset;
1392466dcc0aSjsg
1393466dcc0aSjsg if (target_cmd_index == offset)
1394466dcc0aSjsg return 0;
1395466dcc0aSjsg
1396c349dbc7Sjsg if (IS_ERR(jump_whitelist))
1397c349dbc7Sjsg return PTR_ERR(jump_whitelist);
1398c349dbc7Sjsg
1399c349dbc7Sjsg if (!test_bit(target_cmd_index, jump_whitelist)) {
1400466dcc0aSjsg DRM_DEBUG("CMD: BB_START to 0x%llx not a previously executed cmd\n",
1401466dcc0aSjsg jump_target);
1402466dcc0aSjsg return -EINVAL;
1403466dcc0aSjsg }
1404466dcc0aSjsg
1405466dcc0aSjsg return 0;
1406466dcc0aSjsg }
1407466dcc0aSjsg
alloc_whitelist(u32 batch_length)1408c349dbc7Sjsg static unsigned long *alloc_whitelist(u32 batch_length)
1409466dcc0aSjsg {
1410c349dbc7Sjsg unsigned long *jmp;
1411466dcc0aSjsg
1412c349dbc7Sjsg /*
1413c349dbc7Sjsg * We expect batch_length to be less than 256KiB for known users,
1414c349dbc7Sjsg * i.e. we need at most an 8KiB bitmap allocation which should be
1415c349dbc7Sjsg * reasonably cheap due to kmalloc caches.
1416c349dbc7Sjsg */
1417466dcc0aSjsg
1418c349dbc7Sjsg /* Prefer to report transient allocation failure rather than hit oom */
1419c349dbc7Sjsg jmp = bitmap_zalloc(DIV_ROUND_UP(batch_length, sizeof(u32)),
1420c349dbc7Sjsg GFP_KERNEL | __GFP_RETRY_MAYFAIL | __GFP_NOWARN);
1421c349dbc7Sjsg if (!jmp)
1422c349dbc7Sjsg return ERR_PTR(-ENOMEM);
1423466dcc0aSjsg
1424c349dbc7Sjsg return jmp;
1425466dcc0aSjsg }
1426466dcc0aSjsg
14273253c27bSkettenis #define LENGTH_BIAS 2
14283253c27bSkettenis
14293253c27bSkettenis /**
1430c349dbc7Sjsg * intel_engine_cmd_parser() - parse a batch buffer for privilege violations
14317f4dd379Sjsg * @engine: the engine on which the batch is to execute
1432c349dbc7Sjsg * @batch: the batch buffer in question
1433c349dbc7Sjsg * @batch_offset: byte offset in the batch at which execution starts
1434c349dbc7Sjsg * @batch_length: length of the commands in batch_obj
1435c349dbc7Sjsg * @shadow: validated copy of the batch buffer in question
14365ca02815Sjsg * @trampoline: true if we need to trampoline into privileged execution
14373253c27bSkettenis *
14383253c27bSkettenis * Parses the specified batch buffer looking for privilege violations as
14393253c27bSkettenis * described in the overview.
14403253c27bSkettenis *
14413253c27bSkettenis * Return: non-zero if the parser finds violations or otherwise fails; -EACCES
14423253c27bSkettenis * if the batch appears legal but should use hardware parsing
14433253c27bSkettenis */
1444d9ace711Sjsg
intel_engine_cmd_parser(struct intel_engine_cs * engine,struct i915_vma * batch,unsigned long batch_offset,unsigned long batch_length,struct i915_vma * shadow,bool trampoline)1445c349dbc7Sjsg int intel_engine_cmd_parser(struct intel_engine_cs *engine,
1446c349dbc7Sjsg struct i915_vma *batch,
1447ad8b1aafSjsg unsigned long batch_offset,
1448ad8b1aafSjsg unsigned long batch_length,
1449c349dbc7Sjsg struct i915_vma *shadow,
1450c349dbc7Sjsg bool trampoline)
14513253c27bSkettenis {
1452466dcc0aSjsg u32 *cmd, *batch_end, offset = 0;
14537f4dd379Sjsg struct drm_i915_cmd_descriptor default_desc = noop_desc;
14547f4dd379Sjsg const struct drm_i915_cmd_descriptor *desc = &default_desc;
14555ca02815Sjsg bool needs_clflush_after = false;
1456c349dbc7Sjsg unsigned long *jump_whitelist;
1457c349dbc7Sjsg u64 batch_addr, shadow_addr;
14583253c27bSkettenis int ret = 0;
14593253c27bSkettenis
1460c349dbc7Sjsg GEM_BUG_ON(!IS_ALIGNED(batch_offset, sizeof(*cmd)));
1461c349dbc7Sjsg GEM_BUG_ON(!IS_ALIGNED(batch_length, sizeof(*cmd)));
1462c349dbc7Sjsg GEM_BUG_ON(range_overflows_t(u64, batch_offset, batch_length,
1463c349dbc7Sjsg batch->size));
1464c349dbc7Sjsg GEM_BUG_ON(!batch_length);
1465c349dbc7Sjsg
1466d9ace711Sjsg cmd = copy_batch(shadow->obj, batch->obj,
14675ca02815Sjsg batch_offset, batch_length,
14685ca02815Sjsg &needs_clflush_after);
14697f4dd379Sjsg if (IS_ERR(cmd)) {
1470c349dbc7Sjsg DRM_DEBUG("CMD: Failed to copy batch\n");
14717f4dd379Sjsg return PTR_ERR(cmd);
14723253c27bSkettenis }
14733253c27bSkettenis
1474c349dbc7Sjsg jump_whitelist = NULL;
1475c349dbc7Sjsg if (!trampoline)
1476c349dbc7Sjsg /* Defer failure until attempted use */
1477c349dbc7Sjsg jump_whitelist = alloc_whitelist(batch_length);
1478c349dbc7Sjsg
1479*f005ef32Sjsg shadow_addr = gen8_canonical_addr(i915_vma_offset(shadow));
1480*f005ef32Sjsg batch_addr = gen8_canonical_addr(i915_vma_offset(batch) + batch_offset);
1481466dcc0aSjsg
14823253c27bSkettenis /*
14833253c27bSkettenis * We use the batch length as size because the shadow object is as
14843253c27bSkettenis * large or larger and copy_batch() will write MI_NOPs to the extra
14853253c27bSkettenis * space. Parsing should be faster in some cases this way.
14863253c27bSkettenis */
1487c349dbc7Sjsg batch_end = cmd + batch_length / sizeof(*batch_end);
14887f4dd379Sjsg do {
14893253c27bSkettenis u32 length;
14903253c27bSkettenis
1491c1dee687Sjsg if (*cmd == MI_BATCH_BUFFER_END)
14923253c27bSkettenis break;
14933253c27bSkettenis
14947f4dd379Sjsg desc = find_cmd(engine, *cmd, desc, &default_desc);
14953253c27bSkettenis if (!desc) {
1496c349dbc7Sjsg DRM_DEBUG("CMD: Unrecognized command: 0x%08X\n", *cmd);
14973253c27bSkettenis ret = -EINVAL;
1498c349dbc7Sjsg break;
14993253c27bSkettenis }
15003253c27bSkettenis
15013253c27bSkettenis if (desc->flags & CMD_DESC_FIXED)
15023253c27bSkettenis length = desc->length.fixed;
15033253c27bSkettenis else
1504c349dbc7Sjsg length = (*cmd & desc->length.mask) + LENGTH_BIAS;
15053253c27bSkettenis
15063253c27bSkettenis if ((batch_end - cmd) < length) {
1507c349dbc7Sjsg DRM_DEBUG("CMD: Command length exceeds batch length: 0x%08X length=%u batchlen=%td\n",
15083253c27bSkettenis *cmd,
15093253c27bSkettenis length,
15103253c27bSkettenis batch_end - cmd);
15113253c27bSkettenis ret = -EINVAL;
1512c349dbc7Sjsg break;
15133253c27bSkettenis }
15143253c27bSkettenis
1515f59a1707Sjsg if (!check_cmd(engine, desc, cmd, length)) {
15167f4dd379Sjsg ret = -EACCES;
1517466dcc0aSjsg break;
1518466dcc0aSjsg }
1519466dcc0aSjsg
1520ad8b1aafSjsg if (cmd_desc_is(desc, MI_BATCH_BUFFER_START)) {
1521c349dbc7Sjsg ret = check_bbstart(cmd, offset, length, batch_length,
1522c349dbc7Sjsg batch_addr, shadow_addr,
1523c349dbc7Sjsg jump_whitelist);
1524c349dbc7Sjsg break;
1525c349dbc7Sjsg }
1526c349dbc7Sjsg
1527c349dbc7Sjsg if (!IS_ERR_OR_NULL(jump_whitelist))
1528c349dbc7Sjsg __set_bit(offset, jump_whitelist);
1529466dcc0aSjsg
15303253c27bSkettenis cmd += length;
1531466dcc0aSjsg offset += length;
15323253c27bSkettenis if (cmd >= batch_end) {
1533c349dbc7Sjsg DRM_DEBUG("CMD: Got to the end of the buffer w/o a BBE cmd!\n");
15343253c27bSkettenis ret = -EINVAL;
1535c349dbc7Sjsg break;
15363253c27bSkettenis }
15377f4dd379Sjsg } while (1);
15383253c27bSkettenis
1539c349dbc7Sjsg if (trampoline) {
1540c349dbc7Sjsg /*
1541c349dbc7Sjsg * With the trampoline, the shadow is executed twice.
1542c349dbc7Sjsg *
1543c349dbc7Sjsg * 1 - starting at offset 0, in privileged mode
1544c349dbc7Sjsg * 2 - starting at offset batch_len, as non-privileged
1545c349dbc7Sjsg *
1546c349dbc7Sjsg * Only if the batch is valid and safe to execute, do we
1547c349dbc7Sjsg * allow the first privileged execution to proceed. If not,
1548c349dbc7Sjsg * we terminate the first batch and use the second batchbuffer
1549c349dbc7Sjsg * entry to chain to the original unsafe non-privileged batch,
1550c349dbc7Sjsg * leaving it to the HW to validate.
1551c349dbc7Sjsg */
1552c349dbc7Sjsg *batch_end = MI_BATCH_BUFFER_END;
1553c349dbc7Sjsg
1554c349dbc7Sjsg if (ret) {
1555c349dbc7Sjsg /* Batch unsafe to execute with privileges, cancel! */
1556c349dbc7Sjsg cmd = page_mask_bits(shadow->obj->mm.mapping);
1557c349dbc7Sjsg *cmd = MI_BATCH_BUFFER_END;
1558c349dbc7Sjsg
1559c349dbc7Sjsg /* If batch is unsafe but valid, jump to the original */
1560c349dbc7Sjsg if (ret == -EACCES) {
1561c349dbc7Sjsg unsigned int flags;
1562c349dbc7Sjsg
1563c349dbc7Sjsg flags = MI_BATCH_NON_SECURE_I965;
1564c349dbc7Sjsg if (IS_HASWELL(engine->i915))
1565c349dbc7Sjsg flags = MI_BATCH_NON_SECURE_HSW;
1566c349dbc7Sjsg
15675ca02815Sjsg GEM_BUG_ON(!IS_GRAPHICS_VER(engine->i915, 6, 7));
1568c349dbc7Sjsg __gen6_emit_bb_start(batch_end,
1569c349dbc7Sjsg batch_addr,
1570c349dbc7Sjsg flags);
1571c349dbc7Sjsg
1572c349dbc7Sjsg ret = 0; /* allow execution */
1573c349dbc7Sjsg }
1574c349dbc7Sjsg }
1575c349dbc7Sjsg }
1576c349dbc7Sjsg
1577ad8b1aafSjsg i915_gem_object_flush_map(shadow->obj);
1578c1dee687Sjsg
1579c349dbc7Sjsg if (!IS_ERR_OR_NULL(jump_whitelist))
1580c349dbc7Sjsg kfree(jump_whitelist);
1581c349dbc7Sjsg i915_gem_object_unpin_map(shadow->obj);
15823253c27bSkettenis return ret;
15833253c27bSkettenis }
15843253c27bSkettenis
15853253c27bSkettenis /**
15863253c27bSkettenis * i915_cmd_parser_get_version() - get the cmd parser version number
15877f4dd379Sjsg * @dev_priv: i915 device private
15883253c27bSkettenis *
15893253c27bSkettenis * The cmd parser maintains a simple increasing integer version number suitable
15903253c27bSkettenis * for passing to userspace clients to determine what operations are permitted.
15913253c27bSkettenis *
15923253c27bSkettenis * Return: the current version number of the cmd parser
15933253c27bSkettenis */
i915_cmd_parser_get_version(struct drm_i915_private * dev_priv)15947f4dd379Sjsg int i915_cmd_parser_get_version(struct drm_i915_private *dev_priv)
15953253c27bSkettenis {
15967f4dd379Sjsg struct intel_engine_cs *engine;
15977f4dd379Sjsg bool active = false;
15987f4dd379Sjsg
15997f4dd379Sjsg /* If the command parser is not enabled, report 0 - unsupported */
1600c349dbc7Sjsg for_each_uabi_engine(engine, dev_priv) {
16012f1f75dbSjsg if (intel_engine_using_cmd_parser(engine)) {
16027f4dd379Sjsg active = true;
16037f4dd379Sjsg break;
16047f4dd379Sjsg }
16057f4dd379Sjsg }
16067f4dd379Sjsg if (!active)
16077f4dd379Sjsg return 0;
16087f4dd379Sjsg
16093253c27bSkettenis /*
16103253c27bSkettenis * Command parser version history
16113253c27bSkettenis *
16123253c27bSkettenis * 1. Initial version. Checks batches and reports violations, but leaves
16133253c27bSkettenis * hardware parsing enabled (so does not allow new use cases).
16143253c27bSkettenis * 2. Allow access to the MI_PREDICATE_SRC0 and
16153253c27bSkettenis * MI_PREDICATE_SRC1 registers.
16163253c27bSkettenis * 3. Allow access to the GPGPU_THREADS_DISPATCHED register.
16173253c27bSkettenis * 4. L3 atomic chicken bits of HSW_SCRATCH1 and HSW_ROW_CHICKEN3.
16183253c27bSkettenis * 5. GPGPU dispatch compute indirect registers.
16197f4dd379Sjsg * 6. TIMESTAMP register and Haswell CS GPR registers
16207f4dd379Sjsg * 7. Allow MI_LOAD_REGISTER_REG between whitelisted registers.
16217f4dd379Sjsg * 8. Don't report cmd_check() failures as EINVAL errors to userspace;
16227f4dd379Sjsg * rely on the HW to NOOP disallowed commands as it would without
16237f4dd379Sjsg * the parser enabled.
16247f4dd379Sjsg * 9. Don't whitelist or handle oacontrol specially, as ownership
16257f4dd379Sjsg * for oacontrol state is moving to i915-perf.
16269a2084e7Sjsg * 10. Support for Gen9 BCS Parsing
16273253c27bSkettenis */
16289a2084e7Sjsg return 10;
16293253c27bSkettenis }
1630