i915_cmd_parser.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. /*
  2. * Copyright © 2013 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Brad Volkin <bradley.d.volkin@intel.com>
  25. *
  26. */
  27. #include "i915_drv.h"
  28. /**
  29. * DOC: i915 batch buffer command parser
  30. *
  31. * Motivation:
  32. * Certain OpenGL features (e.g. transform feedback, performance monitoring)
  33. * require userspace code to submit batches containing commands such as
  34. * MI_LOAD_REGISTER_IMM to access various registers. Unfortunately, some
  35. * generations of the hardware will noop these commands in "unsecure" batches
  36. * (which includes all userspace batches submitted via i915) even though the
  37. * commands may be safe and represent the intended programming model of the
  38. * device.
  39. *
  40. * The software command parser is similar in operation to the command parsing
  41. * done in hardware for unsecure batches. However, the software parser allows
  42. * some operations that would be noop'd by hardware, if the parser determines
  43. * the operation is safe, and submits the batch as "secure" to prevent hardware
  44. * parsing.
  45. *
  46. * Threats:
  47. * At a high level, the hardware (and software) checks attempt to prevent
  48. * granting userspace undue privileges. There are three categories of privilege.
  49. *
  50. * First, commands which are explicitly defined as privileged or which should
  51. * only be used by the kernel driver. The parser generally rejects such
  52. * commands, though it may allow some from the drm master process.
  53. *
  54. * Second, commands which access registers. To support correct/enhanced
  55. * userspace functionality, particularly certain OpenGL extensions, the parser
  56. * provides a whitelist of registers which userspace may safely access (for both
  57. * normal and drm master processes).
  58. *
  59. * Third, commands which access privileged memory (i.e. GGTT, HWS page, etc).
  60. * The parser always rejects such commands.
  61. *
  62. * The majority of the problematic commands fall in the MI_* range, with only a
  63. * few specific commands on each ring (e.g. PIPE_CONTROL and MI_FLUSH_DW).
  64. *
  65. * Implementation:
  66. * Each ring maintains tables of commands and registers which the parser uses in
  67. * scanning batch buffers submitted to that ring.
  68. *
  69. * Since the set of commands that the parser must check for is significantly
  70. * smaller than the number of commands supported, the parser tables contain only
  71. * those commands required by the parser. This generally works because command
  72. * opcode ranges have standard command length encodings. So for commands that
  73. * the parser does not need to check, it can easily skip them. This is
  74. * implementated via a per-ring length decoding vfunc.
  75. *
  76. * Unfortunately, there are a number of commands that do not follow the standard
  77. * length encoding for their opcode range, primarily amongst the MI_* commands.
  78. * To handle this, the parser provides a way to define explicit "skip" entries
  79. * in the per-ring command tables.
  80. *
  81. * Other command table entries map fairly directly to high level categories
  82. * mentioned above: rejected, master-only, register whitelist. The parser
  83. * implements a number of checks, including the privileged memory checks, via a
  84. * general bitmasking mechanism.
  85. */
  86. #define STD_MI_OPCODE_MASK 0xFF800000
  87. #define STD_3D_OPCODE_MASK 0xFFFF0000
  88. #define STD_2D_OPCODE_MASK 0xFFC00000
  89. #define STD_MFX_OPCODE_MASK 0xFFFF0000
  90. #define CMD(op, opm, f, lm, fl, ...) \
  91. { \
  92. .flags = (fl) | ((f) ? CMD_DESC_FIXED : 0), \
  93. .cmd = { (op), (opm) }, \
  94. .length = { (lm) }, \
  95. __VA_ARGS__ \
  96. }
  97. /* Convenience macros to compress the tables */
  98. #define SMI STD_MI_OPCODE_MASK
  99. #define S3D STD_3D_OPCODE_MASK
  100. #define S2D STD_2D_OPCODE_MASK
  101. #define SMFX STD_MFX_OPCODE_MASK
  102. #define F true
  103. #define S CMD_DESC_SKIP
  104. #define R CMD_DESC_REJECT
  105. #define W CMD_DESC_REGISTER
  106. #define B CMD_DESC_BITMASK
  107. #define M CMD_DESC_MASTER
  108. /* Command Mask Fixed Len Action
  109. ---------------------------------------------------------- */
  110. static const struct drm_i915_cmd_descriptor common_cmds[] = {
  111. CMD( MI_NOOP, SMI, F, 1, S ),
  112. CMD( MI_USER_INTERRUPT, SMI, F, 1, S ),
  113. CMD( MI_WAIT_FOR_EVENT, SMI, F, 1, M ),
  114. CMD( MI_ARB_CHECK, SMI, F, 1, S ),
  115. CMD( MI_REPORT_HEAD, SMI, F, 1, S ),
  116. CMD( MI_SUSPEND_FLUSH, SMI, F, 1, S ),
  117. CMD( MI_SEMAPHORE_MBOX, SMI, !F, 0xFF, R ),
  118. CMD( MI_STORE_DWORD_INDEX, SMI, !F, 0xFF, R ),
  119. CMD( MI_LOAD_REGISTER_IMM(1), SMI, !F, 0xFF, R ),
  120. CMD( MI_STORE_REGISTER_MEM(1), SMI, !F, 0xFF, R ),
  121. CMD( MI_LOAD_REGISTER_MEM, SMI, !F, 0xFF, R ),
  122. CMD( MI_BATCH_BUFFER_START, SMI, !F, 0xFF, S ),
  123. };
  124. static const struct drm_i915_cmd_descriptor render_cmds[] = {
  125. CMD( MI_FLUSH, SMI, F, 1, S ),
  126. CMD( MI_ARB_ON_OFF, SMI, F, 1, R ),
  127. CMD( MI_PREDICATE, SMI, F, 1, S ),
  128. CMD( MI_TOPOLOGY_FILTER, SMI, F, 1, S ),
  129. CMD( MI_DISPLAY_FLIP, SMI, !F, 0xFF, R ),
  130. CMD( MI_SET_CONTEXT, SMI, !F, 0xFF, R ),
  131. CMD( MI_URB_CLEAR, SMI, !F, 0xFF, S ),
  132. CMD( MI_UPDATE_GTT, SMI, !F, 0xFF, R ),
  133. CMD( MI_CLFLUSH, SMI, !F, 0x3FF, S ),
  134. CMD( MI_CONDITIONAL_BATCH_BUFFER_END, SMI, !F, 0xFF, S ),
  135. CMD( GFX_OP_3DSTATE_VF_STATISTICS, S3D, F, 1, S ),
  136. CMD( PIPELINE_SELECT, S3D, F, 1, S ),
  137. CMD( GPGPU_OBJECT, S3D, !F, 0xFF, S ),
  138. CMD( GPGPU_WALKER, S3D, !F, 0xFF, S ),
  139. CMD( GFX_OP_3DSTATE_SO_DECL_LIST, S3D, !F, 0x1FF, S ),
  140. };
  141. static const struct drm_i915_cmd_descriptor hsw_render_cmds[] = {
  142. CMD( MI_SET_PREDICATE, SMI, F, 1, S ),
  143. CMD( MI_RS_CONTROL, SMI, F, 1, S ),
  144. CMD( MI_URB_ATOMIC_ALLOC, SMI, F, 1, S ),
  145. CMD( MI_RS_CONTEXT, SMI, F, 1, S ),
  146. CMD( MI_LOAD_SCAN_LINES_INCL, SMI, !F, 0x3F, M ),
  147. CMD( MI_LOAD_SCAN_LINES_EXCL, SMI, !F, 0x3F, R ),
  148. CMD( MI_LOAD_REGISTER_REG, SMI, !F, 0xFF, R ),
  149. CMD( MI_RS_STORE_DATA_IMM, SMI, !F, 0xFF, S ),
  150. CMD( MI_LOAD_URB_MEM, SMI, !F, 0xFF, S ),
  151. CMD( MI_STORE_URB_MEM, SMI, !F, 0xFF, S ),
  152. CMD( GFX_OP_3DSTATE_DX9_CONSTANTF_VS, S3D, !F, 0x7FF, S ),
  153. CMD( GFX_OP_3DSTATE_DX9_CONSTANTF_PS, S3D, !F, 0x7FF, S ),
  154. CMD( GFX_OP_3DSTATE_BINDING_TABLE_EDIT_VS, S3D, !F, 0x1FF, S ),
  155. CMD( GFX_OP_3DSTATE_BINDING_TABLE_EDIT_GS, S3D, !F, 0x1FF, S ),
  156. CMD( GFX_OP_3DSTATE_BINDING_TABLE_EDIT_HS, S3D, !F, 0x1FF, S ),
  157. CMD( GFX_OP_3DSTATE_BINDING_TABLE_EDIT_DS, S3D, !F, 0x1FF, S ),
  158. CMD( GFX_OP_3DSTATE_BINDING_TABLE_EDIT_PS, S3D, !F, 0x1FF, S ),
  159. };
  160. static const struct drm_i915_cmd_descriptor video_cmds[] = {
  161. CMD( MI_ARB_ON_OFF, SMI, F, 1, R ),
  162. CMD( MI_STORE_DWORD_IMM, SMI, !F, 0xFF, S ),
  163. CMD( MI_UPDATE_GTT, SMI, !F, 0x3F, R ),
  164. CMD( MI_CONDITIONAL_BATCH_BUFFER_END, SMI, !F, 0xFF, S ),
  165. /*
  166. * MFX_WAIT doesn't fit the way we handle length for most commands.
  167. * It has a length field but it uses a non-standard length bias.
  168. * It is always 1 dword though, so just treat it as fixed length.
  169. */
  170. CMD( MFX_WAIT, SMFX, F, 1, S ),
  171. };
  172. static const struct drm_i915_cmd_descriptor vecs_cmds[] = {
  173. CMD( MI_ARB_ON_OFF, SMI, F, 1, R ),
  174. CMD( MI_STORE_DWORD_IMM, SMI, !F, 0xFF, S ),
  175. CMD( MI_UPDATE_GTT, SMI, !F, 0x3F, R ),
  176. CMD( MI_CONDITIONAL_BATCH_BUFFER_END, SMI, !F, 0xFF, S ),
  177. };
  178. static const struct drm_i915_cmd_descriptor blt_cmds[] = {
  179. CMD( MI_DISPLAY_FLIP, SMI, !F, 0xFF, R ),
  180. CMD( MI_STORE_DWORD_IMM, SMI, !F, 0x3FF, S ),
  181. CMD( MI_UPDATE_GTT, SMI, !F, 0x3F, R ),
  182. CMD( COLOR_BLT, S2D, !F, 0x3F, S ),
  183. CMD( SRC_COPY_BLT, S2D, !F, 0x3F, S ),
  184. };
  185. static const struct drm_i915_cmd_descriptor hsw_blt_cmds[] = {
  186. CMD( MI_LOAD_SCAN_LINES_INCL, SMI, !F, 0x3F, M ),
  187. CMD( MI_LOAD_SCAN_LINES_EXCL, SMI, !F, 0x3F, R ),
  188. };
  189. #undef CMD
  190. #undef SMI
  191. #undef S3D
  192. #undef S2D
  193. #undef SMFX
  194. #undef F
  195. #undef S
  196. #undef R
  197. #undef W
  198. #undef B
  199. #undef M
  200. static const struct drm_i915_cmd_table gen7_render_cmds[] = {
  201. { common_cmds, ARRAY_SIZE(common_cmds) },
  202. { render_cmds, ARRAY_SIZE(render_cmds) },
  203. };
  204. static const struct drm_i915_cmd_table hsw_render_ring_cmds[] = {
  205. { common_cmds, ARRAY_SIZE(common_cmds) },
  206. { render_cmds, ARRAY_SIZE(render_cmds) },
  207. { hsw_render_cmds, ARRAY_SIZE(hsw_render_cmds) },
  208. };
  209. static const struct drm_i915_cmd_table gen7_video_cmds[] = {
  210. { common_cmds, ARRAY_SIZE(common_cmds) },
  211. { video_cmds, ARRAY_SIZE(video_cmds) },
  212. };
  213. static const struct drm_i915_cmd_table hsw_vebox_cmds[] = {
  214. { common_cmds, ARRAY_SIZE(common_cmds) },
  215. { vecs_cmds, ARRAY_SIZE(vecs_cmds) },
  216. };
  217. static const struct drm_i915_cmd_table gen7_blt_cmds[] = {
  218. { common_cmds, ARRAY_SIZE(common_cmds) },
  219. { blt_cmds, ARRAY_SIZE(blt_cmds) },
  220. };
  221. static const struct drm_i915_cmd_table hsw_blt_ring_cmds[] = {
  222. { common_cmds, ARRAY_SIZE(common_cmds) },
  223. { blt_cmds, ARRAY_SIZE(blt_cmds) },
  224. { hsw_blt_cmds, ARRAY_SIZE(hsw_blt_cmds) },
  225. };
  226. /*
  227. * Register whitelists, sorted by increasing register offset.
  228. *
  229. * Some registers that userspace accesses are 64 bits. The register
  230. * access commands only allow 32-bit accesses. Hence, we have to include
  231. * entries for both halves of the 64-bit registers.
  232. */
  233. /* Convenience macro for adding 64-bit registers */
  234. #define REG64(addr) (addr), (addr + sizeof(u32))
  235. static const u32 gen7_render_regs[] = {
  236. REG64(HS_INVOCATION_COUNT),
  237. REG64(DS_INVOCATION_COUNT),
  238. REG64(IA_VERTICES_COUNT),
  239. REG64(IA_PRIMITIVES_COUNT),
  240. REG64(VS_INVOCATION_COUNT),
  241. REG64(GS_INVOCATION_COUNT),
  242. REG64(GS_PRIMITIVES_COUNT),
  243. REG64(CL_INVOCATION_COUNT),
  244. REG64(CL_PRIMITIVES_COUNT),
  245. REG64(PS_INVOCATION_COUNT),
  246. REG64(PS_DEPTH_COUNT),
  247. REG64(GEN7_SO_NUM_PRIMS_WRITTEN(0)),
  248. REG64(GEN7_SO_NUM_PRIMS_WRITTEN(1)),
  249. REG64(GEN7_SO_NUM_PRIMS_WRITTEN(2)),
  250. REG64(GEN7_SO_NUM_PRIMS_WRITTEN(3)),
  251. GEN7_SO_WRITE_OFFSET(0),
  252. GEN7_SO_WRITE_OFFSET(1),
  253. GEN7_SO_WRITE_OFFSET(2),
  254. GEN7_SO_WRITE_OFFSET(3),
  255. };
  256. static const u32 gen7_blt_regs[] = {
  257. BCS_SWCTRL,
  258. };
  259. #undef REG64
  260. static u32 gen7_render_get_cmd_length_mask(u32 cmd_header)
  261. {
  262. u32 client = (cmd_header & INSTR_CLIENT_MASK) >> INSTR_CLIENT_SHIFT;
  263. u32 subclient =
  264. (cmd_header & INSTR_SUBCLIENT_MASK) >> INSTR_SUBCLIENT_SHIFT;
  265. if (client == INSTR_MI_CLIENT)
  266. return 0x3F;
  267. else if (client == INSTR_RC_CLIENT) {
  268. if (subclient == INSTR_MEDIA_SUBCLIENT)
  269. return 0xFFFF;
  270. else
  271. return 0xFF;
  272. }
  273. DRM_DEBUG_DRIVER("CMD: Abnormal rcs cmd length! 0x%08X\n", cmd_header);
  274. return 0;
  275. }
  276. static u32 gen7_bsd_get_cmd_length_mask(u32 cmd_header)
  277. {
  278. u32 client = (cmd_header & INSTR_CLIENT_MASK) >> INSTR_CLIENT_SHIFT;
  279. u32 subclient =
  280. (cmd_header & INSTR_SUBCLIENT_MASK) >> INSTR_SUBCLIENT_SHIFT;
  281. if (client == INSTR_MI_CLIENT)
  282. return 0x3F;
  283. else if (client == INSTR_RC_CLIENT) {
  284. if (subclient == INSTR_MEDIA_SUBCLIENT)
  285. return 0xFFF;
  286. else
  287. return 0xFF;
  288. }
  289. DRM_DEBUG_DRIVER("CMD: Abnormal bsd cmd length! 0x%08X\n", cmd_header);
  290. return 0;
  291. }
  292. static u32 gen7_blt_get_cmd_length_mask(u32 cmd_header)
  293. {
  294. u32 client = (cmd_header & INSTR_CLIENT_MASK) >> INSTR_CLIENT_SHIFT;
  295. if (client == INSTR_MI_CLIENT)
  296. return 0x3F;
  297. else if (client == INSTR_BC_CLIENT)
  298. return 0xFF;
  299. DRM_DEBUG_DRIVER("CMD: Abnormal blt cmd length! 0x%08X\n", cmd_header);
  300. return 0;
  301. }
  302. static void validate_cmds_sorted(struct intel_ring_buffer *ring)
  303. {
  304. int i;
  305. if (!ring->cmd_tables || ring->cmd_table_count == 0)
  306. return;
  307. for (i = 0; i < ring->cmd_table_count; i++) {
  308. const struct drm_i915_cmd_table *table = &ring->cmd_tables[i];
  309. u32 previous = 0;
  310. int j;
  311. for (j = 0; j < table->count; j++) {
  312. const struct drm_i915_cmd_descriptor *desc =
  313. &table->table[i];
  314. u32 curr = desc->cmd.value & desc->cmd.mask;
  315. if (curr < previous)
  316. DRM_ERROR("CMD: table not sorted ring=%d table=%d entry=%d cmd=0x%08X prev=0x%08X\n",
  317. ring->id, i, j, curr, previous);
  318. previous = curr;
  319. }
  320. }
  321. }
  322. static void check_sorted(int ring_id, const u32 *reg_table, int reg_count)
  323. {
  324. int i;
  325. u32 previous = 0;
  326. for (i = 0; i < reg_count; i++) {
  327. u32 curr = reg_table[i];
  328. if (curr < previous)
  329. DRM_ERROR("CMD: table not sorted ring=%d entry=%d reg=0x%08X prev=0x%08X\n",
  330. ring_id, i, curr, previous);
  331. previous = curr;
  332. }
  333. }
  334. static void validate_regs_sorted(struct intel_ring_buffer *ring)
  335. {
  336. check_sorted(ring->id, ring->reg_table, ring->reg_count);
  337. check_sorted(ring->id, ring->master_reg_table, ring->master_reg_count);
  338. }
  339. /**
  340. * i915_cmd_parser_init_ring() - set cmd parser related fields for a ringbuffer
  341. * @ring: the ringbuffer to initialize
  342. *
  343. * Optionally initializes fields related to batch buffer command parsing in the
  344. * struct intel_ring_buffer based on whether the platform requires software
  345. * command parsing.
  346. */
  347. void i915_cmd_parser_init_ring(struct intel_ring_buffer *ring)
  348. {
  349. if (!IS_GEN7(ring->dev))
  350. return;
  351. switch (ring->id) {
  352. case RCS:
  353. if (IS_HASWELL(ring->dev)) {
  354. ring->cmd_tables = hsw_render_ring_cmds;
  355. ring->cmd_table_count =
  356. ARRAY_SIZE(hsw_render_ring_cmds);
  357. } else {
  358. ring->cmd_tables = gen7_render_cmds;
  359. ring->cmd_table_count = ARRAY_SIZE(gen7_render_cmds);
  360. }
  361. ring->reg_table = gen7_render_regs;
  362. ring->reg_count = ARRAY_SIZE(gen7_render_regs);
  363. ring->get_cmd_length_mask = gen7_render_get_cmd_length_mask;
  364. break;
  365. case VCS:
  366. ring->cmd_tables = gen7_video_cmds;
  367. ring->cmd_table_count = ARRAY_SIZE(gen7_video_cmds);
  368. ring->get_cmd_length_mask = gen7_bsd_get_cmd_length_mask;
  369. break;
  370. case BCS:
  371. if (IS_HASWELL(ring->dev)) {
  372. ring->cmd_tables = hsw_blt_ring_cmds;
  373. ring->cmd_table_count = ARRAY_SIZE(hsw_blt_ring_cmds);
  374. } else {
  375. ring->cmd_tables = gen7_blt_cmds;
  376. ring->cmd_table_count = ARRAY_SIZE(gen7_blt_cmds);
  377. }
  378. ring->reg_table = gen7_blt_regs;
  379. ring->reg_count = ARRAY_SIZE(gen7_blt_regs);
  380. ring->get_cmd_length_mask = gen7_blt_get_cmd_length_mask;
  381. break;
  382. case VECS:
  383. ring->cmd_tables = hsw_vebox_cmds;
  384. ring->cmd_table_count = ARRAY_SIZE(hsw_vebox_cmds);
  385. /* VECS can use the same length_mask function as VCS */
  386. ring->get_cmd_length_mask = gen7_bsd_get_cmd_length_mask;
  387. break;
  388. default:
  389. DRM_ERROR("CMD: cmd_parser_init with unknown ring: %d\n",
  390. ring->id);
  391. BUG();
  392. }
  393. validate_cmds_sorted(ring);
  394. validate_regs_sorted(ring);
  395. }
  396. static const struct drm_i915_cmd_descriptor*
  397. find_cmd_in_table(const struct drm_i915_cmd_table *table,
  398. u32 cmd_header)
  399. {
  400. int i;
  401. for (i = 0; i < table->count; i++) {
  402. const struct drm_i915_cmd_descriptor *desc = &table->table[i];
  403. u32 masked_cmd = desc->cmd.mask & cmd_header;
  404. u32 masked_value = desc->cmd.value & desc->cmd.mask;
  405. if (masked_cmd == masked_value)
  406. return desc;
  407. }
  408. return NULL;
  409. }
  410. /*
  411. * Returns a pointer to a descriptor for the command specified by cmd_header.
  412. *
  413. * The caller must supply space for a default descriptor via the default_desc
  414. * parameter. If no descriptor for the specified command exists in the ring's
  415. * command parser tables, this function fills in default_desc based on the
  416. * ring's default length encoding and returns default_desc.
  417. */
  418. static const struct drm_i915_cmd_descriptor*
  419. find_cmd(struct intel_ring_buffer *ring,
  420. u32 cmd_header,
  421. struct drm_i915_cmd_descriptor *default_desc)
  422. {
  423. u32 mask;
  424. int i;
  425. for (i = 0; i < ring->cmd_table_count; i++) {
  426. const struct drm_i915_cmd_descriptor *desc;
  427. desc = find_cmd_in_table(&ring->cmd_tables[i], cmd_header);
  428. if (desc)
  429. return desc;
  430. }
  431. mask = ring->get_cmd_length_mask(cmd_header);
  432. if (!mask)
  433. return NULL;
  434. BUG_ON(!default_desc);
  435. default_desc->flags = CMD_DESC_SKIP;
  436. default_desc->length.mask = mask;
  437. return default_desc;
  438. }
  439. static bool valid_reg(const u32 *table, int count, u32 addr)
  440. {
  441. if (table && count != 0) {
  442. int i;
  443. for (i = 0; i < count; i++) {
  444. if (table[i] == addr)
  445. return true;
  446. }
  447. }
  448. return false;
  449. }
  450. static u32 *vmap_batch(struct drm_i915_gem_object *obj)
  451. {
  452. int i;
  453. void *addr = NULL;
  454. struct sg_page_iter sg_iter;
  455. struct page **pages;
  456. pages = drm_malloc_ab(obj->base.size >> PAGE_SHIFT, sizeof(*pages));
  457. if (pages == NULL) {
  458. DRM_DEBUG_DRIVER("Failed to get space for pages\n");
  459. goto finish;
  460. }
  461. i = 0;
  462. for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents, 0) {
  463. pages[i] = sg_page_iter_page(&sg_iter);
  464. i++;
  465. }
  466. addr = vmap(pages, i, 0, PAGE_KERNEL);
  467. if (addr == NULL) {
  468. DRM_DEBUG_DRIVER("Failed to vmap pages\n");
  469. goto finish;
  470. }
  471. finish:
  472. if (pages)
  473. drm_free_large(pages);
  474. return (u32*)addr;
  475. }
  476. /**
  477. * i915_needs_cmd_parser() - should a given ring use software command parsing?
  478. * @ring: the ring in question
  479. *
  480. * Only certain platforms require software batch buffer command parsing, and
  481. * only when enabled via module paramter.
  482. *
  483. * Return: true if the ring requires software command parsing
  484. */
  485. bool i915_needs_cmd_parser(struct intel_ring_buffer *ring)
  486. {
  487. /* No command tables indicates a platform without parsing */
  488. if (!ring->cmd_tables)
  489. return false;
  490. return (i915.enable_cmd_parser == 1);
  491. }
  492. #define LENGTH_BIAS 2
  493. /**
  494. * i915_parse_cmds() - parse a submitted batch buffer for privilege violations
  495. * @ring: the ring on which the batch is to execute
  496. * @batch_obj: the batch buffer in question
  497. * @batch_start_offset: byte offset in the batch at which execution starts
  498. * @is_master: is the submitting process the drm master?
  499. *
  500. * Parses the specified batch buffer looking for privilege violations as
  501. * described in the overview.
  502. *
  503. * Return: non-zero if the parser finds violations or otherwise fails
  504. */
  505. int i915_parse_cmds(struct intel_ring_buffer *ring,
  506. struct drm_i915_gem_object *batch_obj,
  507. u32 batch_start_offset,
  508. bool is_master)
  509. {
  510. int ret = 0;
  511. u32 *cmd, *batch_base, *batch_end;
  512. struct drm_i915_cmd_descriptor default_desc = { 0 };
  513. int needs_clflush = 0;
  514. ret = i915_gem_obj_prepare_shmem_read(batch_obj, &needs_clflush);
  515. if (ret) {
  516. DRM_DEBUG_DRIVER("CMD: failed to prep read\n");
  517. return ret;
  518. }
  519. batch_base = vmap_batch(batch_obj);
  520. if (!batch_base) {
  521. DRM_DEBUG_DRIVER("CMD: Failed to vmap batch\n");
  522. i915_gem_object_unpin_pages(batch_obj);
  523. return -ENOMEM;
  524. }
  525. if (needs_clflush)
  526. drm_clflush_virt_range((char *)batch_base, batch_obj->base.size);
  527. cmd = batch_base + (batch_start_offset / sizeof(*cmd));
  528. batch_end = cmd + (batch_obj->base.size / sizeof(*batch_end));
  529. while (cmd < batch_end) {
  530. const struct drm_i915_cmd_descriptor *desc;
  531. u32 length;
  532. if (*cmd == MI_BATCH_BUFFER_END)
  533. break;
  534. desc = find_cmd(ring, *cmd, &default_desc);
  535. if (!desc) {
  536. DRM_DEBUG_DRIVER("CMD: Unrecognized command: 0x%08X\n",
  537. *cmd);
  538. ret = -EINVAL;
  539. break;
  540. }
  541. if (desc->flags & CMD_DESC_FIXED)
  542. length = desc->length.fixed;
  543. else
  544. length = ((*cmd & desc->length.mask) + LENGTH_BIAS);
  545. if ((batch_end - cmd) < length) {
  546. DRM_DEBUG_DRIVER("CMD: Command length exceeds batch length: 0x%08X length=%d batchlen=%td\n",
  547. *cmd,
  548. length,
  549. batch_end - cmd);
  550. ret = -EINVAL;
  551. break;
  552. }
  553. if (desc->flags & CMD_DESC_REJECT) {
  554. DRM_DEBUG_DRIVER("CMD: Rejected command: 0x%08X\n", *cmd);
  555. ret = -EINVAL;
  556. break;
  557. }
  558. if ((desc->flags & CMD_DESC_MASTER) && !is_master) {
  559. DRM_DEBUG_DRIVER("CMD: Rejected master-only command: 0x%08X\n",
  560. *cmd);
  561. ret = -EINVAL;
  562. break;
  563. }
  564. if (desc->flags & CMD_DESC_REGISTER) {
  565. u32 reg_addr = cmd[desc->reg.offset] & desc->reg.mask;
  566. if (!valid_reg(ring->reg_table,
  567. ring->reg_count, reg_addr)) {
  568. if (!is_master ||
  569. !valid_reg(ring->master_reg_table,
  570. ring->master_reg_count,
  571. reg_addr)) {
  572. DRM_DEBUG_DRIVER("CMD: Rejected register 0x%08X in command: 0x%08X (ring=%d)\n",
  573. reg_addr,
  574. *cmd,
  575. ring->id);
  576. ret = -EINVAL;
  577. break;
  578. }
  579. }
  580. }
  581. if (desc->flags & CMD_DESC_BITMASK) {
  582. int i;
  583. for (i = 0; i < MAX_CMD_DESC_BITMASKS; i++) {
  584. u32 dword;
  585. if (desc->bits[i].mask == 0)
  586. break;
  587. dword = cmd[desc->bits[i].offset] &
  588. desc->bits[i].mask;
  589. if (dword != desc->bits[i].expected) {
  590. DRM_DEBUG_DRIVER("CMD: Rejected command 0x%08X for bitmask 0x%08X (exp=0x%08X act=0x%08X) (ring=%d)\n",
  591. *cmd,
  592. desc->bits[i].mask,
  593. desc->bits[i].expected,
  594. dword, ring->id);
  595. ret = -EINVAL;
  596. break;
  597. }
  598. }
  599. if (ret)
  600. break;
  601. }
  602. cmd += length;
  603. }
  604. if (cmd >= batch_end) {
  605. DRM_DEBUG_DRIVER("CMD: Got to the end of the buffer w/o a BBE cmd!\n");
  606. ret = -EINVAL;
  607. }
  608. vunmap(batch_base);
  609. i915_gem_object_unpin_pages(batch_obj);
  610. return ret;
  611. }