i915_cmd_parser.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  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, S ),
  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, S ),
  118. CMD( MI_STORE_DWORD_INDEX, SMI, !F, 0xFF, S ),
  119. CMD( MI_LOAD_REGISTER_IMM(1), SMI, !F, 0xFF, S ),
  120. CMD( MI_STORE_REGISTER_MEM(1), SMI, !F, 0xFF, S ),
  121. CMD( MI_LOAD_REGISTER_MEM, SMI, !F, 0xFF, S ),
  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, S ),
  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, S ),
  130. CMD( MI_SET_CONTEXT, SMI, !F, 0xFF, S ),
  131. CMD( MI_URB_CLEAR, SMI, !F, 0xFF, S ),
  132. CMD( MI_UPDATE_GTT, SMI, !F, 0xFF, S ),
  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_REGISTER_REG, SMI, !F, 0xFF, S ),
  147. CMD( MI_RS_STORE_DATA_IMM, SMI, !F, 0xFF, S ),
  148. CMD( MI_LOAD_URB_MEM, SMI, !F, 0xFF, S ),
  149. CMD( MI_STORE_URB_MEM, SMI, !F, 0xFF, S ),
  150. CMD( GFX_OP_3DSTATE_DX9_CONSTANTF_VS, S3D, !F, 0x7FF, S ),
  151. CMD( GFX_OP_3DSTATE_DX9_CONSTANTF_PS, S3D, !F, 0x7FF, S ),
  152. CMD( GFX_OP_3DSTATE_BINDING_TABLE_EDIT_VS, S3D, !F, 0x1FF, S ),
  153. CMD( GFX_OP_3DSTATE_BINDING_TABLE_EDIT_GS, S3D, !F, 0x1FF, S ),
  154. CMD( GFX_OP_3DSTATE_BINDING_TABLE_EDIT_HS, S3D, !F, 0x1FF, S ),
  155. CMD( GFX_OP_3DSTATE_BINDING_TABLE_EDIT_DS, S3D, !F, 0x1FF, S ),
  156. CMD( GFX_OP_3DSTATE_BINDING_TABLE_EDIT_PS, S3D, !F, 0x1FF, S ),
  157. };
  158. static const struct drm_i915_cmd_descriptor video_cmds[] = {
  159. CMD( MI_ARB_ON_OFF, SMI, F, 1, S ),
  160. CMD( MI_STORE_DWORD_IMM, SMI, !F, 0xFF, S ),
  161. CMD( MI_CONDITIONAL_BATCH_BUFFER_END, SMI, !F, 0xFF, S ),
  162. /*
  163. * MFX_WAIT doesn't fit the way we handle length for most commands.
  164. * It has a length field but it uses a non-standard length bias.
  165. * It is always 1 dword though, so just treat it as fixed length.
  166. */
  167. CMD( MFX_WAIT, SMFX, F, 1, S ),
  168. };
  169. static const struct drm_i915_cmd_descriptor vecs_cmds[] = {
  170. CMD( MI_ARB_ON_OFF, SMI, F, 1, S ),
  171. CMD( MI_STORE_DWORD_IMM, SMI, !F, 0xFF, S ),
  172. CMD( MI_CONDITIONAL_BATCH_BUFFER_END, SMI, !F, 0xFF, S ),
  173. };
  174. static const struct drm_i915_cmd_descriptor blt_cmds[] = {
  175. CMD( MI_DISPLAY_FLIP, SMI, !F, 0xFF, S ),
  176. CMD( MI_STORE_DWORD_IMM, SMI, !F, 0x3FF, S ),
  177. CMD( COLOR_BLT, S2D, !F, 0x3F, S ),
  178. CMD( SRC_COPY_BLT, S2D, !F, 0x3F, S ),
  179. };
  180. #undef CMD
  181. #undef SMI
  182. #undef S3D
  183. #undef S2D
  184. #undef SMFX
  185. #undef F
  186. #undef S
  187. #undef R
  188. #undef W
  189. #undef B
  190. #undef M
  191. static const struct drm_i915_cmd_table gen7_render_cmds[] = {
  192. { common_cmds, ARRAY_SIZE(common_cmds) },
  193. { render_cmds, ARRAY_SIZE(render_cmds) },
  194. };
  195. static const struct drm_i915_cmd_table hsw_render_ring_cmds[] = {
  196. { common_cmds, ARRAY_SIZE(common_cmds) },
  197. { render_cmds, ARRAY_SIZE(render_cmds) },
  198. { hsw_render_cmds, ARRAY_SIZE(hsw_render_cmds) },
  199. };
  200. static const struct drm_i915_cmd_table gen7_video_cmds[] = {
  201. { common_cmds, ARRAY_SIZE(common_cmds) },
  202. { video_cmds, ARRAY_SIZE(video_cmds) },
  203. };
  204. static const struct drm_i915_cmd_table hsw_vebox_cmds[] = {
  205. { common_cmds, ARRAY_SIZE(common_cmds) },
  206. { vecs_cmds, ARRAY_SIZE(vecs_cmds) },
  207. };
  208. static const struct drm_i915_cmd_table gen7_blt_cmds[] = {
  209. { common_cmds, ARRAY_SIZE(common_cmds) },
  210. { blt_cmds, ARRAY_SIZE(blt_cmds) },
  211. };
  212. static u32 gen7_render_get_cmd_length_mask(u32 cmd_header)
  213. {
  214. u32 client = (cmd_header & INSTR_CLIENT_MASK) >> INSTR_CLIENT_SHIFT;
  215. u32 subclient =
  216. (cmd_header & INSTR_SUBCLIENT_MASK) >> INSTR_SUBCLIENT_SHIFT;
  217. if (client == INSTR_MI_CLIENT)
  218. return 0x3F;
  219. else if (client == INSTR_RC_CLIENT) {
  220. if (subclient == INSTR_MEDIA_SUBCLIENT)
  221. return 0xFFFF;
  222. else
  223. return 0xFF;
  224. }
  225. DRM_DEBUG_DRIVER("CMD: Abnormal rcs cmd length! 0x%08X\n", cmd_header);
  226. return 0;
  227. }
  228. static u32 gen7_bsd_get_cmd_length_mask(u32 cmd_header)
  229. {
  230. u32 client = (cmd_header & INSTR_CLIENT_MASK) >> INSTR_CLIENT_SHIFT;
  231. u32 subclient =
  232. (cmd_header & INSTR_SUBCLIENT_MASK) >> INSTR_SUBCLIENT_SHIFT;
  233. if (client == INSTR_MI_CLIENT)
  234. return 0x3F;
  235. else if (client == INSTR_RC_CLIENT) {
  236. if (subclient == INSTR_MEDIA_SUBCLIENT)
  237. return 0xFFF;
  238. else
  239. return 0xFF;
  240. }
  241. DRM_DEBUG_DRIVER("CMD: Abnormal bsd cmd length! 0x%08X\n", cmd_header);
  242. return 0;
  243. }
  244. static u32 gen7_blt_get_cmd_length_mask(u32 cmd_header)
  245. {
  246. u32 client = (cmd_header & INSTR_CLIENT_MASK) >> INSTR_CLIENT_SHIFT;
  247. if (client == INSTR_MI_CLIENT)
  248. return 0x3F;
  249. else if (client == INSTR_BC_CLIENT)
  250. return 0xFF;
  251. DRM_DEBUG_DRIVER("CMD: Abnormal blt cmd length! 0x%08X\n", cmd_header);
  252. return 0;
  253. }
  254. static void validate_cmds_sorted(struct intel_ring_buffer *ring)
  255. {
  256. int i;
  257. if (!ring->cmd_tables || ring->cmd_table_count == 0)
  258. return;
  259. for (i = 0; i < ring->cmd_table_count; i++) {
  260. const struct drm_i915_cmd_table *table = &ring->cmd_tables[i];
  261. u32 previous = 0;
  262. int j;
  263. for (j = 0; j < table->count; j++) {
  264. const struct drm_i915_cmd_descriptor *desc =
  265. &table->table[i];
  266. u32 curr = desc->cmd.value & desc->cmd.mask;
  267. if (curr < previous)
  268. DRM_ERROR("CMD: table not sorted ring=%d table=%d entry=%d cmd=0x%08X prev=0x%08X\n",
  269. ring->id, i, j, curr, previous);
  270. previous = curr;
  271. }
  272. }
  273. }
  274. static void check_sorted(int ring_id, const u32 *reg_table, int reg_count)
  275. {
  276. int i;
  277. u32 previous = 0;
  278. for (i = 0; i < reg_count; i++) {
  279. u32 curr = reg_table[i];
  280. if (curr < previous)
  281. DRM_ERROR("CMD: table not sorted ring=%d entry=%d reg=0x%08X prev=0x%08X\n",
  282. ring_id, i, curr, previous);
  283. previous = curr;
  284. }
  285. }
  286. static void validate_regs_sorted(struct intel_ring_buffer *ring)
  287. {
  288. check_sorted(ring->id, ring->reg_table, ring->reg_count);
  289. check_sorted(ring->id, ring->master_reg_table, ring->master_reg_count);
  290. }
  291. /**
  292. * i915_cmd_parser_init_ring() - set cmd parser related fields for a ringbuffer
  293. * @ring: the ringbuffer to initialize
  294. *
  295. * Optionally initializes fields related to batch buffer command parsing in the
  296. * struct intel_ring_buffer based on whether the platform requires software
  297. * command parsing.
  298. */
  299. void i915_cmd_parser_init_ring(struct intel_ring_buffer *ring)
  300. {
  301. if (!IS_GEN7(ring->dev))
  302. return;
  303. switch (ring->id) {
  304. case RCS:
  305. if (IS_HASWELL(ring->dev)) {
  306. ring->cmd_tables = hsw_render_ring_cmds;
  307. ring->cmd_table_count =
  308. ARRAY_SIZE(hsw_render_ring_cmds);
  309. } else {
  310. ring->cmd_tables = gen7_render_cmds;
  311. ring->cmd_table_count = ARRAY_SIZE(gen7_render_cmds);
  312. }
  313. ring->get_cmd_length_mask = gen7_render_get_cmd_length_mask;
  314. break;
  315. case VCS:
  316. ring->cmd_tables = gen7_video_cmds;
  317. ring->cmd_table_count = ARRAY_SIZE(gen7_video_cmds);
  318. ring->get_cmd_length_mask = gen7_bsd_get_cmd_length_mask;
  319. break;
  320. case BCS:
  321. ring->cmd_tables = gen7_blt_cmds;
  322. ring->cmd_table_count = ARRAY_SIZE(gen7_blt_cmds);
  323. ring->get_cmd_length_mask = gen7_blt_get_cmd_length_mask;
  324. break;
  325. case VECS:
  326. ring->cmd_tables = hsw_vebox_cmds;
  327. ring->cmd_table_count = ARRAY_SIZE(hsw_vebox_cmds);
  328. /* VECS can use the same length_mask function as VCS */
  329. ring->get_cmd_length_mask = gen7_bsd_get_cmd_length_mask;
  330. break;
  331. default:
  332. DRM_ERROR("CMD: cmd_parser_init with unknown ring: %d\n",
  333. ring->id);
  334. BUG();
  335. }
  336. validate_cmds_sorted(ring);
  337. validate_regs_sorted(ring);
  338. }
  339. static const struct drm_i915_cmd_descriptor*
  340. find_cmd_in_table(const struct drm_i915_cmd_table *table,
  341. u32 cmd_header)
  342. {
  343. int i;
  344. for (i = 0; i < table->count; i++) {
  345. const struct drm_i915_cmd_descriptor *desc = &table->table[i];
  346. u32 masked_cmd = desc->cmd.mask & cmd_header;
  347. u32 masked_value = desc->cmd.value & desc->cmd.mask;
  348. if (masked_cmd == masked_value)
  349. return desc;
  350. }
  351. return NULL;
  352. }
  353. /*
  354. * Returns a pointer to a descriptor for the command specified by cmd_header.
  355. *
  356. * The caller must supply space for a default descriptor via the default_desc
  357. * parameter. If no descriptor for the specified command exists in the ring's
  358. * command parser tables, this function fills in default_desc based on the
  359. * ring's default length encoding and returns default_desc.
  360. */
  361. static const struct drm_i915_cmd_descriptor*
  362. find_cmd(struct intel_ring_buffer *ring,
  363. u32 cmd_header,
  364. struct drm_i915_cmd_descriptor *default_desc)
  365. {
  366. u32 mask;
  367. int i;
  368. for (i = 0; i < ring->cmd_table_count; i++) {
  369. const struct drm_i915_cmd_descriptor *desc;
  370. desc = find_cmd_in_table(&ring->cmd_tables[i], cmd_header);
  371. if (desc)
  372. return desc;
  373. }
  374. mask = ring->get_cmd_length_mask(cmd_header);
  375. if (!mask)
  376. return NULL;
  377. BUG_ON(!default_desc);
  378. default_desc->flags = CMD_DESC_SKIP;
  379. default_desc->length.mask = mask;
  380. return default_desc;
  381. }
  382. static bool valid_reg(const u32 *table, int count, u32 addr)
  383. {
  384. if (table && count != 0) {
  385. int i;
  386. for (i = 0; i < count; i++) {
  387. if (table[i] == addr)
  388. return true;
  389. }
  390. }
  391. return false;
  392. }
  393. static u32 *vmap_batch(struct drm_i915_gem_object *obj)
  394. {
  395. int i;
  396. void *addr = NULL;
  397. struct sg_page_iter sg_iter;
  398. struct page **pages;
  399. pages = drm_malloc_ab(obj->base.size >> PAGE_SHIFT, sizeof(*pages));
  400. if (pages == NULL) {
  401. DRM_DEBUG_DRIVER("Failed to get space for pages\n");
  402. goto finish;
  403. }
  404. i = 0;
  405. for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents, 0) {
  406. pages[i] = sg_page_iter_page(&sg_iter);
  407. i++;
  408. }
  409. addr = vmap(pages, i, 0, PAGE_KERNEL);
  410. if (addr == NULL) {
  411. DRM_DEBUG_DRIVER("Failed to vmap pages\n");
  412. goto finish;
  413. }
  414. finish:
  415. if (pages)
  416. drm_free_large(pages);
  417. return (u32*)addr;
  418. }
  419. /**
  420. * i915_needs_cmd_parser() - should a given ring use software command parsing?
  421. * @ring: the ring in question
  422. *
  423. * Only certain platforms require software batch buffer command parsing, and
  424. * only when enabled via module paramter.
  425. *
  426. * Return: true if the ring requires software command parsing
  427. */
  428. bool i915_needs_cmd_parser(struct intel_ring_buffer *ring)
  429. {
  430. /* No command tables indicates a platform without parsing */
  431. if (!ring->cmd_tables)
  432. return false;
  433. return (i915.enable_cmd_parser == 1);
  434. }
  435. #define LENGTH_BIAS 2
  436. /**
  437. * i915_parse_cmds() - parse a submitted batch buffer for privilege violations
  438. * @ring: the ring on which the batch is to execute
  439. * @batch_obj: the batch buffer in question
  440. * @batch_start_offset: byte offset in the batch at which execution starts
  441. * @is_master: is the submitting process the drm master?
  442. *
  443. * Parses the specified batch buffer looking for privilege violations as
  444. * described in the overview.
  445. *
  446. * Return: non-zero if the parser finds violations or otherwise fails
  447. */
  448. int i915_parse_cmds(struct intel_ring_buffer *ring,
  449. struct drm_i915_gem_object *batch_obj,
  450. u32 batch_start_offset,
  451. bool is_master)
  452. {
  453. int ret = 0;
  454. u32 *cmd, *batch_base, *batch_end;
  455. struct drm_i915_cmd_descriptor default_desc = { 0 };
  456. int needs_clflush = 0;
  457. ret = i915_gem_obj_prepare_shmem_read(batch_obj, &needs_clflush);
  458. if (ret) {
  459. DRM_DEBUG_DRIVER("CMD: failed to prep read\n");
  460. return ret;
  461. }
  462. batch_base = vmap_batch(batch_obj);
  463. if (!batch_base) {
  464. DRM_DEBUG_DRIVER("CMD: Failed to vmap batch\n");
  465. i915_gem_object_unpin_pages(batch_obj);
  466. return -ENOMEM;
  467. }
  468. if (needs_clflush)
  469. drm_clflush_virt_range((char *)batch_base, batch_obj->base.size);
  470. cmd = batch_base + (batch_start_offset / sizeof(*cmd));
  471. batch_end = cmd + (batch_obj->base.size / sizeof(*batch_end));
  472. while (cmd < batch_end) {
  473. const struct drm_i915_cmd_descriptor *desc;
  474. u32 length;
  475. if (*cmd == MI_BATCH_BUFFER_END)
  476. break;
  477. desc = find_cmd(ring, *cmd, &default_desc);
  478. if (!desc) {
  479. DRM_DEBUG_DRIVER("CMD: Unrecognized command: 0x%08X\n",
  480. *cmd);
  481. ret = -EINVAL;
  482. break;
  483. }
  484. if (desc->flags & CMD_DESC_FIXED)
  485. length = desc->length.fixed;
  486. else
  487. length = ((*cmd & desc->length.mask) + LENGTH_BIAS);
  488. if ((batch_end - cmd) < length) {
  489. DRM_DEBUG_DRIVER("CMD: Command length exceeds batch length: 0x%08X length=%d batchlen=%td\n",
  490. *cmd,
  491. length,
  492. batch_end - cmd);
  493. ret = -EINVAL;
  494. break;
  495. }
  496. if (desc->flags & CMD_DESC_REJECT) {
  497. DRM_DEBUG_DRIVER("CMD: Rejected command: 0x%08X\n", *cmd);
  498. ret = -EINVAL;
  499. break;
  500. }
  501. if ((desc->flags & CMD_DESC_MASTER) && !is_master) {
  502. DRM_DEBUG_DRIVER("CMD: Rejected master-only command: 0x%08X\n",
  503. *cmd);
  504. ret = -EINVAL;
  505. break;
  506. }
  507. if (desc->flags & CMD_DESC_REGISTER) {
  508. u32 reg_addr = cmd[desc->reg.offset] & desc->reg.mask;
  509. if (!valid_reg(ring->reg_table,
  510. ring->reg_count, reg_addr)) {
  511. if (!is_master ||
  512. !valid_reg(ring->master_reg_table,
  513. ring->master_reg_count,
  514. reg_addr)) {
  515. DRM_DEBUG_DRIVER("CMD: Rejected register 0x%08X in command: 0x%08X (ring=%d)\n",
  516. reg_addr,
  517. *cmd,
  518. ring->id);
  519. ret = -EINVAL;
  520. break;
  521. }
  522. }
  523. }
  524. if (desc->flags & CMD_DESC_BITMASK) {
  525. int i;
  526. for (i = 0; i < MAX_CMD_DESC_BITMASKS; i++) {
  527. u32 dword;
  528. if (desc->bits[i].mask == 0)
  529. break;
  530. dword = cmd[desc->bits[i].offset] &
  531. desc->bits[i].mask;
  532. if (dword != desc->bits[i].expected) {
  533. DRM_DEBUG_DRIVER("CMD: Rejected command 0x%08X for bitmask 0x%08X (exp=0x%08X act=0x%08X) (ring=%d)\n",
  534. *cmd,
  535. desc->bits[i].mask,
  536. desc->bits[i].expected,
  537. dword, ring->id);
  538. ret = -EINVAL;
  539. break;
  540. }
  541. }
  542. if (ret)
  543. break;
  544. }
  545. cmd += length;
  546. }
  547. if (cmd >= batch_end) {
  548. DRM_DEBUG_DRIVER("CMD: Got to the end of the buffer w/o a BBE cmd!\n");
  549. ret = -EINVAL;
  550. }
  551. vunmap(batch_base);
  552. i915_gem_object_unpin_pages(batch_obj);
  553. return ret;
  554. }