i915_cmd_parser.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  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, R ),
  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, W,
  120. .reg = { .offset = 1, .mask = 0x007FFFFC } ),
  121. CMD( MI_STORE_REGISTER_MEM(1), SMI, !F, 0xFF, W | B,
  122. .reg = { .offset = 1, .mask = 0x007FFFFC },
  123. .bits = {{
  124. .offset = 0,
  125. .mask = MI_GLOBAL_GTT,
  126. .expected = 0,
  127. }}, ),
  128. CMD( MI_LOAD_REGISTER_MEM, SMI, !F, 0xFF, W | B,
  129. .reg = { .offset = 1, .mask = 0x007FFFFC },
  130. .bits = {{
  131. .offset = 0,
  132. .mask = MI_GLOBAL_GTT,
  133. .expected = 0,
  134. }}, ),
  135. CMD( MI_BATCH_BUFFER_START, SMI, !F, 0xFF, S ),
  136. };
  137. static const struct drm_i915_cmd_descriptor render_cmds[] = {
  138. CMD( MI_FLUSH, SMI, F, 1, S ),
  139. CMD( MI_ARB_ON_OFF, SMI, F, 1, R ),
  140. CMD( MI_PREDICATE, SMI, F, 1, S ),
  141. CMD( MI_TOPOLOGY_FILTER, SMI, F, 1, S ),
  142. CMD( MI_DISPLAY_FLIP, SMI, !F, 0xFF, R ),
  143. CMD( MI_SET_CONTEXT, SMI, !F, 0xFF, R ),
  144. CMD( MI_URB_CLEAR, SMI, !F, 0xFF, S ),
  145. CMD( MI_STORE_DWORD_IMM, SMI, !F, 0x3F, B,
  146. .bits = {{
  147. .offset = 0,
  148. .mask = MI_GLOBAL_GTT,
  149. .expected = 0,
  150. }}, ),
  151. CMD( MI_UPDATE_GTT, SMI, !F, 0xFF, R ),
  152. CMD( MI_CLFLUSH, SMI, !F, 0x3FF, B,
  153. .bits = {{
  154. .offset = 0,
  155. .mask = MI_GLOBAL_GTT,
  156. .expected = 0,
  157. }}, ),
  158. CMD( MI_REPORT_PERF_COUNT, SMI, !F, 0x3F, B,
  159. .bits = {{
  160. .offset = 1,
  161. .mask = MI_REPORT_PERF_COUNT_GGTT,
  162. .expected = 0,
  163. }}, ),
  164. CMD( MI_CONDITIONAL_BATCH_BUFFER_END, SMI, !F, 0xFF, B,
  165. .bits = {{
  166. .offset = 0,
  167. .mask = MI_GLOBAL_GTT,
  168. .expected = 0,
  169. }}, ),
  170. CMD( GFX_OP_3DSTATE_VF_STATISTICS, S3D, F, 1, S ),
  171. CMD( PIPELINE_SELECT, S3D, F, 1, S ),
  172. CMD( MEDIA_VFE_STATE, S3D, !F, 0xFFFF, B,
  173. .bits = {{
  174. .offset = 2,
  175. .mask = MEDIA_VFE_STATE_MMIO_ACCESS_MASK,
  176. .expected = 0,
  177. }}, ),
  178. CMD( GPGPU_OBJECT, S3D, !F, 0xFF, S ),
  179. CMD( GPGPU_WALKER, S3D, !F, 0xFF, S ),
  180. CMD( GFX_OP_3DSTATE_SO_DECL_LIST, S3D, !F, 0x1FF, S ),
  181. CMD( GFX_OP_PIPE_CONTROL(5), S3D, !F, 0xFF, B,
  182. .bits = {{
  183. .offset = 1,
  184. .mask = (PIPE_CONTROL_MMIO_WRITE | PIPE_CONTROL_NOTIFY),
  185. .expected = 0,
  186. },
  187. {
  188. .offset = 1,
  189. .mask = PIPE_CONTROL_GLOBAL_GTT_IVB,
  190. .expected = 0,
  191. .condition_offset = 1,
  192. .condition_mask = PIPE_CONTROL_POST_SYNC_OP_MASK,
  193. }}, ),
  194. };
  195. static const struct drm_i915_cmd_descriptor hsw_render_cmds[] = {
  196. CMD( MI_SET_PREDICATE, SMI, F, 1, S ),
  197. CMD( MI_RS_CONTROL, SMI, F, 1, S ),
  198. CMD( MI_URB_ATOMIC_ALLOC, SMI, F, 1, S ),
  199. CMD( MI_RS_CONTEXT, SMI, F, 1, S ),
  200. CMD( MI_LOAD_SCAN_LINES_INCL, SMI, !F, 0x3F, M ),
  201. CMD( MI_LOAD_SCAN_LINES_EXCL, SMI, !F, 0x3F, R ),
  202. CMD( MI_LOAD_REGISTER_REG, SMI, !F, 0xFF, R ),
  203. CMD( MI_RS_STORE_DATA_IMM, SMI, !F, 0xFF, S ),
  204. CMD( MI_LOAD_URB_MEM, SMI, !F, 0xFF, S ),
  205. CMD( MI_STORE_URB_MEM, SMI, !F, 0xFF, S ),
  206. CMD( GFX_OP_3DSTATE_DX9_CONSTANTF_VS, S3D, !F, 0x7FF, S ),
  207. CMD( GFX_OP_3DSTATE_DX9_CONSTANTF_PS, S3D, !F, 0x7FF, S ),
  208. CMD( GFX_OP_3DSTATE_BINDING_TABLE_EDIT_VS, S3D, !F, 0x1FF, S ),
  209. CMD( GFX_OP_3DSTATE_BINDING_TABLE_EDIT_GS, S3D, !F, 0x1FF, S ),
  210. CMD( GFX_OP_3DSTATE_BINDING_TABLE_EDIT_HS, S3D, !F, 0x1FF, S ),
  211. CMD( GFX_OP_3DSTATE_BINDING_TABLE_EDIT_DS, S3D, !F, 0x1FF, S ),
  212. CMD( GFX_OP_3DSTATE_BINDING_TABLE_EDIT_PS, S3D, !F, 0x1FF, S ),
  213. };
  214. static const struct drm_i915_cmd_descriptor video_cmds[] = {
  215. CMD( MI_ARB_ON_OFF, SMI, F, 1, R ),
  216. CMD( MI_STORE_DWORD_IMM, SMI, !F, 0xFF, B,
  217. .bits = {{
  218. .offset = 0,
  219. .mask = MI_GLOBAL_GTT,
  220. .expected = 0,
  221. }}, ),
  222. CMD( MI_UPDATE_GTT, SMI, !F, 0x3F, R ),
  223. CMD( MI_FLUSH_DW, SMI, !F, 0x3F, B,
  224. .bits = {{
  225. .offset = 0,
  226. .mask = MI_FLUSH_DW_NOTIFY,
  227. .expected = 0,
  228. },
  229. {
  230. .offset = 1,
  231. .mask = MI_FLUSH_DW_USE_GTT,
  232. .expected = 0,
  233. .condition_offset = 0,
  234. .condition_mask = MI_FLUSH_DW_OP_MASK,
  235. }}, ),
  236. CMD( MI_CONDITIONAL_BATCH_BUFFER_END, SMI, !F, 0xFF, B,
  237. .bits = {{
  238. .offset = 0,
  239. .mask = MI_GLOBAL_GTT,
  240. .expected = 0,
  241. }}, ),
  242. /*
  243. * MFX_WAIT doesn't fit the way we handle length for most commands.
  244. * It has a length field but it uses a non-standard length bias.
  245. * It is always 1 dword though, so just treat it as fixed length.
  246. */
  247. CMD( MFX_WAIT, SMFX, F, 1, S ),
  248. };
  249. static const struct drm_i915_cmd_descriptor vecs_cmds[] = {
  250. CMD( MI_ARB_ON_OFF, SMI, F, 1, R ),
  251. CMD( MI_STORE_DWORD_IMM, SMI, !F, 0xFF, B,
  252. .bits = {{
  253. .offset = 0,
  254. .mask = MI_GLOBAL_GTT,
  255. .expected = 0,
  256. }}, ),
  257. CMD( MI_UPDATE_GTT, SMI, !F, 0x3F, R ),
  258. CMD( MI_FLUSH_DW, SMI, !F, 0x3F, B,
  259. .bits = {{
  260. .offset = 0,
  261. .mask = MI_FLUSH_DW_NOTIFY,
  262. .expected = 0,
  263. },
  264. {
  265. .offset = 1,
  266. .mask = MI_FLUSH_DW_USE_GTT,
  267. .expected = 0,
  268. .condition_offset = 0,
  269. .condition_mask = MI_FLUSH_DW_OP_MASK,
  270. }}, ),
  271. CMD( MI_CONDITIONAL_BATCH_BUFFER_END, SMI, !F, 0xFF, B,
  272. .bits = {{
  273. .offset = 0,
  274. .mask = MI_GLOBAL_GTT,
  275. .expected = 0,
  276. }}, ),
  277. };
  278. static const struct drm_i915_cmd_descriptor blt_cmds[] = {
  279. CMD( MI_DISPLAY_FLIP, SMI, !F, 0xFF, R ),
  280. CMD( MI_STORE_DWORD_IMM, SMI, !F, 0x3FF, B,
  281. .bits = {{
  282. .offset = 0,
  283. .mask = MI_GLOBAL_GTT,
  284. .expected = 0,
  285. }}, ),
  286. CMD( MI_UPDATE_GTT, SMI, !F, 0x3F, R ),
  287. CMD( MI_FLUSH_DW, SMI, !F, 0x3F, B,
  288. .bits = {{
  289. .offset = 0,
  290. .mask = MI_FLUSH_DW_NOTIFY,
  291. .expected = 0,
  292. },
  293. {
  294. .offset = 1,
  295. .mask = MI_FLUSH_DW_USE_GTT,
  296. .expected = 0,
  297. .condition_offset = 0,
  298. .condition_mask = MI_FLUSH_DW_OP_MASK,
  299. }}, ),
  300. CMD( COLOR_BLT, S2D, !F, 0x3F, S ),
  301. CMD( SRC_COPY_BLT, S2D, !F, 0x3F, S ),
  302. };
  303. static const struct drm_i915_cmd_descriptor hsw_blt_cmds[] = {
  304. CMD( MI_LOAD_SCAN_LINES_INCL, SMI, !F, 0x3F, M ),
  305. CMD( MI_LOAD_SCAN_LINES_EXCL, SMI, !F, 0x3F, R ),
  306. };
  307. #undef CMD
  308. #undef SMI
  309. #undef S3D
  310. #undef S2D
  311. #undef SMFX
  312. #undef F
  313. #undef S
  314. #undef R
  315. #undef W
  316. #undef B
  317. #undef M
  318. static const struct drm_i915_cmd_table gen7_render_cmds[] = {
  319. { common_cmds, ARRAY_SIZE(common_cmds) },
  320. { render_cmds, ARRAY_SIZE(render_cmds) },
  321. };
  322. static const struct drm_i915_cmd_table hsw_render_ring_cmds[] = {
  323. { common_cmds, ARRAY_SIZE(common_cmds) },
  324. { render_cmds, ARRAY_SIZE(render_cmds) },
  325. { hsw_render_cmds, ARRAY_SIZE(hsw_render_cmds) },
  326. };
  327. static const struct drm_i915_cmd_table gen7_video_cmds[] = {
  328. { common_cmds, ARRAY_SIZE(common_cmds) },
  329. { video_cmds, ARRAY_SIZE(video_cmds) },
  330. };
  331. static const struct drm_i915_cmd_table hsw_vebox_cmds[] = {
  332. { common_cmds, ARRAY_SIZE(common_cmds) },
  333. { vecs_cmds, ARRAY_SIZE(vecs_cmds) },
  334. };
  335. static const struct drm_i915_cmd_table gen7_blt_cmds[] = {
  336. { common_cmds, ARRAY_SIZE(common_cmds) },
  337. { blt_cmds, ARRAY_SIZE(blt_cmds) },
  338. };
  339. static const struct drm_i915_cmd_table hsw_blt_ring_cmds[] = {
  340. { common_cmds, ARRAY_SIZE(common_cmds) },
  341. { blt_cmds, ARRAY_SIZE(blt_cmds) },
  342. { hsw_blt_cmds, ARRAY_SIZE(hsw_blt_cmds) },
  343. };
  344. /*
  345. * Register whitelists, sorted by increasing register offset.
  346. *
  347. * Some registers that userspace accesses are 64 bits. The register
  348. * access commands only allow 32-bit accesses. Hence, we have to include
  349. * entries for both halves of the 64-bit registers.
  350. */
  351. /* Convenience macro for adding 64-bit registers */
  352. #define REG64(addr) (addr), (addr + sizeof(u32))
  353. static const u32 gen7_render_regs[] = {
  354. REG64(HS_INVOCATION_COUNT),
  355. REG64(DS_INVOCATION_COUNT),
  356. REG64(IA_VERTICES_COUNT),
  357. REG64(IA_PRIMITIVES_COUNT),
  358. REG64(VS_INVOCATION_COUNT),
  359. REG64(GS_INVOCATION_COUNT),
  360. REG64(GS_PRIMITIVES_COUNT),
  361. REG64(CL_INVOCATION_COUNT),
  362. REG64(CL_PRIMITIVES_COUNT),
  363. REG64(PS_INVOCATION_COUNT),
  364. REG64(PS_DEPTH_COUNT),
  365. REG64(GEN7_SO_NUM_PRIMS_WRITTEN(0)),
  366. REG64(GEN7_SO_NUM_PRIMS_WRITTEN(1)),
  367. REG64(GEN7_SO_NUM_PRIMS_WRITTEN(2)),
  368. REG64(GEN7_SO_NUM_PRIMS_WRITTEN(3)),
  369. GEN7_SO_WRITE_OFFSET(0),
  370. GEN7_SO_WRITE_OFFSET(1),
  371. GEN7_SO_WRITE_OFFSET(2),
  372. GEN7_SO_WRITE_OFFSET(3),
  373. };
  374. static const u32 gen7_blt_regs[] = {
  375. BCS_SWCTRL,
  376. };
  377. static const u32 ivb_master_regs[] = {
  378. FORCEWAKE_MT,
  379. DERRMR,
  380. GEN7_PIPE_DE_LOAD_SL(PIPE_A),
  381. GEN7_PIPE_DE_LOAD_SL(PIPE_B),
  382. GEN7_PIPE_DE_LOAD_SL(PIPE_C),
  383. };
  384. static const u32 hsw_master_regs[] = {
  385. FORCEWAKE_MT,
  386. DERRMR,
  387. };
  388. #undef REG64
  389. static u32 gen7_render_get_cmd_length_mask(u32 cmd_header)
  390. {
  391. u32 client = (cmd_header & INSTR_CLIENT_MASK) >> INSTR_CLIENT_SHIFT;
  392. u32 subclient =
  393. (cmd_header & INSTR_SUBCLIENT_MASK) >> INSTR_SUBCLIENT_SHIFT;
  394. if (client == INSTR_MI_CLIENT)
  395. return 0x3F;
  396. else if (client == INSTR_RC_CLIENT) {
  397. if (subclient == INSTR_MEDIA_SUBCLIENT)
  398. return 0xFFFF;
  399. else
  400. return 0xFF;
  401. }
  402. DRM_DEBUG_DRIVER("CMD: Abnormal rcs cmd length! 0x%08X\n", cmd_header);
  403. return 0;
  404. }
  405. static u32 gen7_bsd_get_cmd_length_mask(u32 cmd_header)
  406. {
  407. u32 client = (cmd_header & INSTR_CLIENT_MASK) >> INSTR_CLIENT_SHIFT;
  408. u32 subclient =
  409. (cmd_header & INSTR_SUBCLIENT_MASK) >> INSTR_SUBCLIENT_SHIFT;
  410. if (client == INSTR_MI_CLIENT)
  411. return 0x3F;
  412. else if (client == INSTR_RC_CLIENT) {
  413. if (subclient == INSTR_MEDIA_SUBCLIENT)
  414. return 0xFFF;
  415. else
  416. return 0xFF;
  417. }
  418. DRM_DEBUG_DRIVER("CMD: Abnormal bsd cmd length! 0x%08X\n", cmd_header);
  419. return 0;
  420. }
  421. static u32 gen7_blt_get_cmd_length_mask(u32 cmd_header)
  422. {
  423. u32 client = (cmd_header & INSTR_CLIENT_MASK) >> INSTR_CLIENT_SHIFT;
  424. if (client == INSTR_MI_CLIENT)
  425. return 0x3F;
  426. else if (client == INSTR_BC_CLIENT)
  427. return 0xFF;
  428. DRM_DEBUG_DRIVER("CMD: Abnormal blt cmd length! 0x%08X\n", cmd_header);
  429. return 0;
  430. }
  431. static void validate_cmds_sorted(struct intel_ring_buffer *ring)
  432. {
  433. int i;
  434. if (!ring->cmd_tables || ring->cmd_table_count == 0)
  435. return;
  436. for (i = 0; i < ring->cmd_table_count; i++) {
  437. const struct drm_i915_cmd_table *table = &ring->cmd_tables[i];
  438. u32 previous = 0;
  439. int j;
  440. for (j = 0; j < table->count; j++) {
  441. const struct drm_i915_cmd_descriptor *desc =
  442. &table->table[i];
  443. u32 curr = desc->cmd.value & desc->cmd.mask;
  444. if (curr < previous)
  445. DRM_ERROR("CMD: table not sorted ring=%d table=%d entry=%d cmd=0x%08X prev=0x%08X\n",
  446. ring->id, i, j, curr, previous);
  447. previous = curr;
  448. }
  449. }
  450. }
  451. static void check_sorted(int ring_id, const u32 *reg_table, int reg_count)
  452. {
  453. int i;
  454. u32 previous = 0;
  455. for (i = 0; i < reg_count; i++) {
  456. u32 curr = reg_table[i];
  457. if (curr < previous)
  458. DRM_ERROR("CMD: table not sorted ring=%d entry=%d reg=0x%08X prev=0x%08X\n",
  459. ring_id, i, curr, previous);
  460. previous = curr;
  461. }
  462. }
  463. static void validate_regs_sorted(struct intel_ring_buffer *ring)
  464. {
  465. check_sorted(ring->id, ring->reg_table, ring->reg_count);
  466. check_sorted(ring->id, ring->master_reg_table, ring->master_reg_count);
  467. }
  468. /**
  469. * i915_cmd_parser_init_ring() - set cmd parser related fields for a ringbuffer
  470. * @ring: the ringbuffer to initialize
  471. *
  472. * Optionally initializes fields related to batch buffer command parsing in the
  473. * struct intel_ring_buffer based on whether the platform requires software
  474. * command parsing.
  475. */
  476. void i915_cmd_parser_init_ring(struct intel_ring_buffer *ring)
  477. {
  478. if (!IS_GEN7(ring->dev))
  479. return;
  480. switch (ring->id) {
  481. case RCS:
  482. if (IS_HASWELL(ring->dev)) {
  483. ring->cmd_tables = hsw_render_ring_cmds;
  484. ring->cmd_table_count =
  485. ARRAY_SIZE(hsw_render_ring_cmds);
  486. } else {
  487. ring->cmd_tables = gen7_render_cmds;
  488. ring->cmd_table_count = ARRAY_SIZE(gen7_render_cmds);
  489. }
  490. ring->reg_table = gen7_render_regs;
  491. ring->reg_count = ARRAY_SIZE(gen7_render_regs);
  492. if (IS_HASWELL(ring->dev)) {
  493. ring->master_reg_table = hsw_master_regs;
  494. ring->master_reg_count = ARRAY_SIZE(hsw_master_regs);
  495. } else {
  496. ring->master_reg_table = ivb_master_regs;
  497. ring->master_reg_count = ARRAY_SIZE(ivb_master_regs);
  498. }
  499. ring->get_cmd_length_mask = gen7_render_get_cmd_length_mask;
  500. break;
  501. case VCS:
  502. ring->cmd_tables = gen7_video_cmds;
  503. ring->cmd_table_count = ARRAY_SIZE(gen7_video_cmds);
  504. ring->get_cmd_length_mask = gen7_bsd_get_cmd_length_mask;
  505. break;
  506. case BCS:
  507. if (IS_HASWELL(ring->dev)) {
  508. ring->cmd_tables = hsw_blt_ring_cmds;
  509. ring->cmd_table_count = ARRAY_SIZE(hsw_blt_ring_cmds);
  510. } else {
  511. ring->cmd_tables = gen7_blt_cmds;
  512. ring->cmd_table_count = ARRAY_SIZE(gen7_blt_cmds);
  513. }
  514. ring->reg_table = gen7_blt_regs;
  515. ring->reg_count = ARRAY_SIZE(gen7_blt_regs);
  516. if (IS_HASWELL(ring->dev)) {
  517. ring->master_reg_table = hsw_master_regs;
  518. ring->master_reg_count = ARRAY_SIZE(hsw_master_regs);
  519. } else {
  520. ring->master_reg_table = ivb_master_regs;
  521. ring->master_reg_count = ARRAY_SIZE(ivb_master_regs);
  522. }
  523. ring->get_cmd_length_mask = gen7_blt_get_cmd_length_mask;
  524. break;
  525. case VECS:
  526. ring->cmd_tables = hsw_vebox_cmds;
  527. ring->cmd_table_count = ARRAY_SIZE(hsw_vebox_cmds);
  528. /* VECS can use the same length_mask function as VCS */
  529. ring->get_cmd_length_mask = gen7_bsd_get_cmd_length_mask;
  530. break;
  531. default:
  532. DRM_ERROR("CMD: cmd_parser_init with unknown ring: %d\n",
  533. ring->id);
  534. BUG();
  535. }
  536. validate_cmds_sorted(ring);
  537. validate_regs_sorted(ring);
  538. }
  539. static const struct drm_i915_cmd_descriptor*
  540. find_cmd_in_table(const struct drm_i915_cmd_table *table,
  541. u32 cmd_header)
  542. {
  543. int i;
  544. for (i = 0; i < table->count; i++) {
  545. const struct drm_i915_cmd_descriptor *desc = &table->table[i];
  546. u32 masked_cmd = desc->cmd.mask & cmd_header;
  547. u32 masked_value = desc->cmd.value & desc->cmd.mask;
  548. if (masked_cmd == masked_value)
  549. return desc;
  550. }
  551. return NULL;
  552. }
  553. /*
  554. * Returns a pointer to a descriptor for the command specified by cmd_header.
  555. *
  556. * The caller must supply space for a default descriptor via the default_desc
  557. * parameter. If no descriptor for the specified command exists in the ring's
  558. * command parser tables, this function fills in default_desc based on the
  559. * ring's default length encoding and returns default_desc.
  560. */
  561. static const struct drm_i915_cmd_descriptor*
  562. find_cmd(struct intel_ring_buffer *ring,
  563. u32 cmd_header,
  564. struct drm_i915_cmd_descriptor *default_desc)
  565. {
  566. u32 mask;
  567. int i;
  568. for (i = 0; i < ring->cmd_table_count; i++) {
  569. const struct drm_i915_cmd_descriptor *desc;
  570. desc = find_cmd_in_table(&ring->cmd_tables[i], cmd_header);
  571. if (desc)
  572. return desc;
  573. }
  574. mask = ring->get_cmd_length_mask(cmd_header);
  575. if (!mask)
  576. return NULL;
  577. BUG_ON(!default_desc);
  578. default_desc->flags = CMD_DESC_SKIP;
  579. default_desc->length.mask = mask;
  580. return default_desc;
  581. }
  582. static bool valid_reg(const u32 *table, int count, u32 addr)
  583. {
  584. if (table && count != 0) {
  585. int i;
  586. for (i = 0; i < count; i++) {
  587. if (table[i] == addr)
  588. return true;
  589. }
  590. }
  591. return false;
  592. }
  593. static u32 *vmap_batch(struct drm_i915_gem_object *obj)
  594. {
  595. int i;
  596. void *addr = NULL;
  597. struct sg_page_iter sg_iter;
  598. struct page **pages;
  599. pages = drm_malloc_ab(obj->base.size >> PAGE_SHIFT, sizeof(*pages));
  600. if (pages == NULL) {
  601. DRM_DEBUG_DRIVER("Failed to get space for pages\n");
  602. goto finish;
  603. }
  604. i = 0;
  605. for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents, 0) {
  606. pages[i] = sg_page_iter_page(&sg_iter);
  607. i++;
  608. }
  609. addr = vmap(pages, i, 0, PAGE_KERNEL);
  610. if (addr == NULL) {
  611. DRM_DEBUG_DRIVER("Failed to vmap pages\n");
  612. goto finish;
  613. }
  614. finish:
  615. if (pages)
  616. drm_free_large(pages);
  617. return (u32*)addr;
  618. }
  619. /**
  620. * i915_needs_cmd_parser() - should a given ring use software command parsing?
  621. * @ring: the ring in question
  622. *
  623. * Only certain platforms require software batch buffer command parsing, and
  624. * only when enabled via module paramter.
  625. *
  626. * Return: true if the ring requires software command parsing
  627. */
  628. bool i915_needs_cmd_parser(struct intel_ring_buffer *ring)
  629. {
  630. drm_i915_private_t *dev_priv = ring->dev->dev_private;
  631. /* No command tables indicates a platform without parsing */
  632. if (!ring->cmd_tables)
  633. return false;
  634. /*
  635. * XXX: VLV is Gen7 and therefore has cmd_tables, but has PPGTT
  636. * disabled. That will cause all of the parser's PPGTT checks to
  637. * fail. For now, disable parsing when PPGTT is off.
  638. */
  639. if (!dev_priv->mm.aliasing_ppgtt)
  640. return false;
  641. return (i915.enable_cmd_parser == 1);
  642. }
  643. #define LENGTH_BIAS 2
  644. /**
  645. * i915_parse_cmds() - parse a submitted batch buffer for privilege violations
  646. * @ring: the ring on which the batch is to execute
  647. * @batch_obj: the batch buffer in question
  648. * @batch_start_offset: byte offset in the batch at which execution starts
  649. * @is_master: is the submitting process the drm master?
  650. *
  651. * Parses the specified batch buffer looking for privilege violations as
  652. * described in the overview.
  653. *
  654. * Return: non-zero if the parser finds violations or otherwise fails
  655. */
  656. int i915_parse_cmds(struct intel_ring_buffer *ring,
  657. struct drm_i915_gem_object *batch_obj,
  658. u32 batch_start_offset,
  659. bool is_master)
  660. {
  661. int ret = 0;
  662. u32 *cmd, *batch_base, *batch_end;
  663. struct drm_i915_cmd_descriptor default_desc = { 0 };
  664. int needs_clflush = 0;
  665. ret = i915_gem_obj_prepare_shmem_read(batch_obj, &needs_clflush);
  666. if (ret) {
  667. DRM_DEBUG_DRIVER("CMD: failed to prep read\n");
  668. return ret;
  669. }
  670. batch_base = vmap_batch(batch_obj);
  671. if (!batch_base) {
  672. DRM_DEBUG_DRIVER("CMD: Failed to vmap batch\n");
  673. i915_gem_object_unpin_pages(batch_obj);
  674. return -ENOMEM;
  675. }
  676. if (needs_clflush)
  677. drm_clflush_virt_range((char *)batch_base, batch_obj->base.size);
  678. cmd = batch_base + (batch_start_offset / sizeof(*cmd));
  679. batch_end = cmd + (batch_obj->base.size / sizeof(*batch_end));
  680. while (cmd < batch_end) {
  681. const struct drm_i915_cmd_descriptor *desc;
  682. u32 length;
  683. if (*cmd == MI_BATCH_BUFFER_END)
  684. break;
  685. desc = find_cmd(ring, *cmd, &default_desc);
  686. if (!desc) {
  687. DRM_DEBUG_DRIVER("CMD: Unrecognized command: 0x%08X\n",
  688. *cmd);
  689. ret = -EINVAL;
  690. break;
  691. }
  692. if (desc->flags & CMD_DESC_FIXED)
  693. length = desc->length.fixed;
  694. else
  695. length = ((*cmd & desc->length.mask) + LENGTH_BIAS);
  696. if ((batch_end - cmd) < length) {
  697. DRM_DEBUG_DRIVER("CMD: Command length exceeds batch length: 0x%08X length=%d batchlen=%td\n",
  698. *cmd,
  699. length,
  700. batch_end - cmd);
  701. ret = -EINVAL;
  702. break;
  703. }
  704. if (desc->flags & CMD_DESC_REJECT) {
  705. DRM_DEBUG_DRIVER("CMD: Rejected command: 0x%08X\n", *cmd);
  706. ret = -EINVAL;
  707. break;
  708. }
  709. if ((desc->flags & CMD_DESC_MASTER) && !is_master) {
  710. DRM_DEBUG_DRIVER("CMD: Rejected master-only command: 0x%08X\n",
  711. *cmd);
  712. ret = -EINVAL;
  713. break;
  714. }
  715. if (desc->flags & CMD_DESC_REGISTER) {
  716. u32 reg_addr = cmd[desc->reg.offset] & desc->reg.mask;
  717. if (!valid_reg(ring->reg_table,
  718. ring->reg_count, reg_addr)) {
  719. if (!is_master ||
  720. !valid_reg(ring->master_reg_table,
  721. ring->master_reg_count,
  722. reg_addr)) {
  723. DRM_DEBUG_DRIVER("CMD: Rejected register 0x%08X in command: 0x%08X (ring=%d)\n",
  724. reg_addr,
  725. *cmd,
  726. ring->id);
  727. ret = -EINVAL;
  728. break;
  729. }
  730. }
  731. }
  732. if (desc->flags & CMD_DESC_BITMASK) {
  733. int i;
  734. for (i = 0; i < MAX_CMD_DESC_BITMASKS; i++) {
  735. u32 dword;
  736. if (desc->bits[i].mask == 0)
  737. break;
  738. if (desc->bits[i].condition_mask != 0) {
  739. u32 offset =
  740. desc->bits[i].condition_offset;
  741. u32 condition = cmd[offset] &
  742. desc->bits[i].condition_mask;
  743. if (condition == 0)
  744. continue;
  745. }
  746. dword = cmd[desc->bits[i].offset] &
  747. desc->bits[i].mask;
  748. if (dword != desc->bits[i].expected) {
  749. DRM_DEBUG_DRIVER("CMD: Rejected command 0x%08X for bitmask 0x%08X (exp=0x%08X act=0x%08X) (ring=%d)\n",
  750. *cmd,
  751. desc->bits[i].mask,
  752. desc->bits[i].expected,
  753. dword, ring->id);
  754. ret = -EINVAL;
  755. break;
  756. }
  757. }
  758. if (ret)
  759. break;
  760. }
  761. cmd += length;
  762. }
  763. if (cmd >= batch_end) {
  764. DRM_DEBUG_DRIVER("CMD: Got to the end of the buffer w/o a BBE cmd!\n");
  765. ret = -EINVAL;
  766. }
  767. vunmap(batch_base);
  768. i915_gem_object_unpin_pages(batch_obj);
  769. return ret;
  770. }