i915_cmd_parser.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  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: 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. * implemented 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. /*
  136. * MI_BATCH_BUFFER_START requires some special handling. It's not
  137. * really a 'skip' action but it doesn't seem like it's worth adding
  138. * a new action. See i915_parse_cmds().
  139. */
  140. CMD( MI_BATCH_BUFFER_START, SMI, !F, 0xFF, S ),
  141. };
  142. static const struct drm_i915_cmd_descriptor render_cmds[] = {
  143. CMD( MI_FLUSH, SMI, F, 1, S ),
  144. CMD( MI_ARB_ON_OFF, SMI, F, 1, R ),
  145. CMD( MI_PREDICATE, SMI, F, 1, S ),
  146. CMD( MI_TOPOLOGY_FILTER, SMI, F, 1, S ),
  147. CMD( MI_DISPLAY_FLIP, SMI, !F, 0xFF, R ),
  148. CMD( MI_SET_CONTEXT, SMI, !F, 0xFF, R ),
  149. CMD( MI_URB_CLEAR, SMI, !F, 0xFF, S ),
  150. CMD( MI_STORE_DWORD_IMM, SMI, !F, 0x3F, B,
  151. .bits = {{
  152. .offset = 0,
  153. .mask = MI_GLOBAL_GTT,
  154. .expected = 0,
  155. }}, ),
  156. CMD( MI_UPDATE_GTT, SMI, !F, 0xFF, R ),
  157. CMD( MI_CLFLUSH, SMI, !F, 0x3FF, B,
  158. .bits = {{
  159. .offset = 0,
  160. .mask = MI_GLOBAL_GTT,
  161. .expected = 0,
  162. }}, ),
  163. CMD( MI_REPORT_PERF_COUNT, SMI, !F, 0x3F, B,
  164. .bits = {{
  165. .offset = 1,
  166. .mask = MI_REPORT_PERF_COUNT_GGTT,
  167. .expected = 0,
  168. }}, ),
  169. CMD( MI_CONDITIONAL_BATCH_BUFFER_END, SMI, !F, 0xFF, B,
  170. .bits = {{
  171. .offset = 0,
  172. .mask = MI_GLOBAL_GTT,
  173. .expected = 0,
  174. }}, ),
  175. CMD( GFX_OP_3DSTATE_VF_STATISTICS, S3D, F, 1, S ),
  176. CMD( PIPELINE_SELECT, S3D, F, 1, S ),
  177. CMD( MEDIA_VFE_STATE, S3D, !F, 0xFFFF, B,
  178. .bits = {{
  179. .offset = 2,
  180. .mask = MEDIA_VFE_STATE_MMIO_ACCESS_MASK,
  181. .expected = 0,
  182. }}, ),
  183. CMD( GPGPU_OBJECT, S3D, !F, 0xFF, S ),
  184. CMD( GPGPU_WALKER, S3D, !F, 0xFF, S ),
  185. CMD( GFX_OP_3DSTATE_SO_DECL_LIST, S3D, !F, 0x1FF, S ),
  186. CMD( GFX_OP_PIPE_CONTROL(5), S3D, !F, 0xFF, B,
  187. .bits = {{
  188. .offset = 1,
  189. .mask = (PIPE_CONTROL_MMIO_WRITE | PIPE_CONTROL_NOTIFY),
  190. .expected = 0,
  191. },
  192. {
  193. .offset = 1,
  194. .mask = (PIPE_CONTROL_GLOBAL_GTT_IVB |
  195. PIPE_CONTROL_STORE_DATA_INDEX),
  196. .expected = 0,
  197. .condition_offset = 1,
  198. .condition_mask = PIPE_CONTROL_POST_SYNC_OP_MASK,
  199. }}, ),
  200. };
  201. static const struct drm_i915_cmd_descriptor hsw_render_cmds[] = {
  202. CMD( MI_SET_PREDICATE, SMI, F, 1, S ),
  203. CMD( MI_RS_CONTROL, SMI, F, 1, S ),
  204. CMD( MI_URB_ATOMIC_ALLOC, SMI, F, 1, S ),
  205. CMD( MI_RS_CONTEXT, SMI, F, 1, S ),
  206. CMD( MI_LOAD_SCAN_LINES_INCL, SMI, !F, 0x3F, M ),
  207. CMD( MI_LOAD_SCAN_LINES_EXCL, SMI, !F, 0x3F, R ),
  208. CMD( MI_LOAD_REGISTER_REG, SMI, !F, 0xFF, R ),
  209. CMD( MI_RS_STORE_DATA_IMM, SMI, !F, 0xFF, S ),
  210. CMD( MI_LOAD_URB_MEM, SMI, !F, 0xFF, S ),
  211. CMD( MI_STORE_URB_MEM, SMI, !F, 0xFF, S ),
  212. CMD( GFX_OP_3DSTATE_DX9_CONSTANTF_VS, S3D, !F, 0x7FF, S ),
  213. CMD( GFX_OP_3DSTATE_DX9_CONSTANTF_PS, S3D, !F, 0x7FF, S ),
  214. CMD( GFX_OP_3DSTATE_BINDING_TABLE_EDIT_VS, S3D, !F, 0x1FF, S ),
  215. CMD( GFX_OP_3DSTATE_BINDING_TABLE_EDIT_GS, S3D, !F, 0x1FF, S ),
  216. CMD( GFX_OP_3DSTATE_BINDING_TABLE_EDIT_HS, S3D, !F, 0x1FF, S ),
  217. CMD( GFX_OP_3DSTATE_BINDING_TABLE_EDIT_DS, S3D, !F, 0x1FF, S ),
  218. CMD( GFX_OP_3DSTATE_BINDING_TABLE_EDIT_PS, S3D, !F, 0x1FF, S ),
  219. };
  220. static const struct drm_i915_cmd_descriptor video_cmds[] = {
  221. CMD( MI_ARB_ON_OFF, SMI, F, 1, R ),
  222. CMD( MI_STORE_DWORD_IMM, SMI, !F, 0xFF, B,
  223. .bits = {{
  224. .offset = 0,
  225. .mask = MI_GLOBAL_GTT,
  226. .expected = 0,
  227. }}, ),
  228. CMD( MI_UPDATE_GTT, SMI, !F, 0x3F, R ),
  229. CMD( MI_FLUSH_DW, SMI, !F, 0x3F, B,
  230. .bits = {{
  231. .offset = 0,
  232. .mask = MI_FLUSH_DW_NOTIFY,
  233. .expected = 0,
  234. },
  235. {
  236. .offset = 1,
  237. .mask = MI_FLUSH_DW_USE_GTT,
  238. .expected = 0,
  239. .condition_offset = 0,
  240. .condition_mask = MI_FLUSH_DW_OP_MASK,
  241. },
  242. {
  243. .offset = 0,
  244. .mask = MI_FLUSH_DW_STORE_INDEX,
  245. .expected = 0,
  246. .condition_offset = 0,
  247. .condition_mask = MI_FLUSH_DW_OP_MASK,
  248. }}, ),
  249. CMD( MI_CONDITIONAL_BATCH_BUFFER_END, SMI, !F, 0xFF, B,
  250. .bits = {{
  251. .offset = 0,
  252. .mask = MI_GLOBAL_GTT,
  253. .expected = 0,
  254. }}, ),
  255. /*
  256. * MFX_WAIT doesn't fit the way we handle length for most commands.
  257. * It has a length field but it uses a non-standard length bias.
  258. * It is always 1 dword though, so just treat it as fixed length.
  259. */
  260. CMD( MFX_WAIT, SMFX, F, 1, S ),
  261. };
  262. static const struct drm_i915_cmd_descriptor vecs_cmds[] = {
  263. CMD( MI_ARB_ON_OFF, SMI, F, 1, R ),
  264. CMD( MI_STORE_DWORD_IMM, SMI, !F, 0xFF, B,
  265. .bits = {{
  266. .offset = 0,
  267. .mask = MI_GLOBAL_GTT,
  268. .expected = 0,
  269. }}, ),
  270. CMD( MI_UPDATE_GTT, SMI, !F, 0x3F, R ),
  271. CMD( MI_FLUSH_DW, SMI, !F, 0x3F, B,
  272. .bits = {{
  273. .offset = 0,
  274. .mask = MI_FLUSH_DW_NOTIFY,
  275. .expected = 0,
  276. },
  277. {
  278. .offset = 1,
  279. .mask = MI_FLUSH_DW_USE_GTT,
  280. .expected = 0,
  281. .condition_offset = 0,
  282. .condition_mask = MI_FLUSH_DW_OP_MASK,
  283. },
  284. {
  285. .offset = 0,
  286. .mask = MI_FLUSH_DW_STORE_INDEX,
  287. .expected = 0,
  288. .condition_offset = 0,
  289. .condition_mask = MI_FLUSH_DW_OP_MASK,
  290. }}, ),
  291. CMD( MI_CONDITIONAL_BATCH_BUFFER_END, SMI, !F, 0xFF, B,
  292. .bits = {{
  293. .offset = 0,
  294. .mask = MI_GLOBAL_GTT,
  295. .expected = 0,
  296. }}, ),
  297. };
  298. static const struct drm_i915_cmd_descriptor blt_cmds[] = {
  299. CMD( MI_DISPLAY_FLIP, SMI, !F, 0xFF, R ),
  300. CMD( MI_STORE_DWORD_IMM, SMI, !F, 0x3FF, B,
  301. .bits = {{
  302. .offset = 0,
  303. .mask = MI_GLOBAL_GTT,
  304. .expected = 0,
  305. }}, ),
  306. CMD( MI_UPDATE_GTT, SMI, !F, 0x3F, R ),
  307. CMD( MI_FLUSH_DW, SMI, !F, 0x3F, B,
  308. .bits = {{
  309. .offset = 0,
  310. .mask = MI_FLUSH_DW_NOTIFY,
  311. .expected = 0,
  312. },
  313. {
  314. .offset = 1,
  315. .mask = MI_FLUSH_DW_USE_GTT,
  316. .expected = 0,
  317. .condition_offset = 0,
  318. .condition_mask = MI_FLUSH_DW_OP_MASK,
  319. },
  320. {
  321. .offset = 0,
  322. .mask = MI_FLUSH_DW_STORE_INDEX,
  323. .expected = 0,
  324. .condition_offset = 0,
  325. .condition_mask = MI_FLUSH_DW_OP_MASK,
  326. }}, ),
  327. CMD( COLOR_BLT, S2D, !F, 0x3F, S ),
  328. CMD( SRC_COPY_BLT, S2D, !F, 0x3F, S ),
  329. };
  330. static const struct drm_i915_cmd_descriptor hsw_blt_cmds[] = {
  331. CMD( MI_LOAD_SCAN_LINES_INCL, SMI, !F, 0x3F, M ),
  332. CMD( MI_LOAD_SCAN_LINES_EXCL, SMI, !F, 0x3F, R ),
  333. };
  334. #undef CMD
  335. #undef SMI
  336. #undef S3D
  337. #undef S2D
  338. #undef SMFX
  339. #undef F
  340. #undef S
  341. #undef R
  342. #undef W
  343. #undef B
  344. #undef M
  345. static const struct drm_i915_cmd_table gen7_render_cmds[] = {
  346. { common_cmds, ARRAY_SIZE(common_cmds) },
  347. { render_cmds, ARRAY_SIZE(render_cmds) },
  348. };
  349. static const struct drm_i915_cmd_table hsw_render_ring_cmds[] = {
  350. { common_cmds, ARRAY_SIZE(common_cmds) },
  351. { render_cmds, ARRAY_SIZE(render_cmds) },
  352. { hsw_render_cmds, ARRAY_SIZE(hsw_render_cmds) },
  353. };
  354. static const struct drm_i915_cmd_table gen7_video_cmds[] = {
  355. { common_cmds, ARRAY_SIZE(common_cmds) },
  356. { video_cmds, ARRAY_SIZE(video_cmds) },
  357. };
  358. static const struct drm_i915_cmd_table hsw_vebox_cmds[] = {
  359. { common_cmds, ARRAY_SIZE(common_cmds) },
  360. { vecs_cmds, ARRAY_SIZE(vecs_cmds) },
  361. };
  362. static const struct drm_i915_cmd_table gen7_blt_cmds[] = {
  363. { common_cmds, ARRAY_SIZE(common_cmds) },
  364. { blt_cmds, ARRAY_SIZE(blt_cmds) },
  365. };
  366. static const struct drm_i915_cmd_table hsw_blt_ring_cmds[] = {
  367. { common_cmds, ARRAY_SIZE(common_cmds) },
  368. { blt_cmds, ARRAY_SIZE(blt_cmds) },
  369. { hsw_blt_cmds, ARRAY_SIZE(hsw_blt_cmds) },
  370. };
  371. /*
  372. * Register whitelists, sorted by increasing register offset.
  373. *
  374. * Some registers that userspace accesses are 64 bits. The register
  375. * access commands only allow 32-bit accesses. Hence, we have to include
  376. * entries for both halves of the 64-bit registers.
  377. */
  378. /* Convenience macro for adding 64-bit registers */
  379. #define REG64(addr) (addr), (addr + sizeof(u32))
  380. static const u32 gen7_render_regs[] = {
  381. REG64(HS_INVOCATION_COUNT),
  382. REG64(DS_INVOCATION_COUNT),
  383. REG64(IA_VERTICES_COUNT),
  384. REG64(IA_PRIMITIVES_COUNT),
  385. REG64(VS_INVOCATION_COUNT),
  386. REG64(GS_INVOCATION_COUNT),
  387. REG64(GS_PRIMITIVES_COUNT),
  388. REG64(CL_INVOCATION_COUNT),
  389. REG64(CL_PRIMITIVES_COUNT),
  390. REG64(PS_INVOCATION_COUNT),
  391. REG64(PS_DEPTH_COUNT),
  392. OACONTROL, /* Only allowed for LRI and SRM. See below. */
  393. REG64(MI_PREDICATE_SRC0),
  394. REG64(MI_PREDICATE_SRC1),
  395. GEN7_3DPRIM_END_OFFSET,
  396. GEN7_3DPRIM_START_VERTEX,
  397. GEN7_3DPRIM_VERTEX_COUNT,
  398. GEN7_3DPRIM_INSTANCE_COUNT,
  399. GEN7_3DPRIM_START_INSTANCE,
  400. GEN7_3DPRIM_BASE_VERTEX,
  401. REG64(GEN7_SO_NUM_PRIMS_WRITTEN(0)),
  402. REG64(GEN7_SO_NUM_PRIMS_WRITTEN(1)),
  403. REG64(GEN7_SO_NUM_PRIMS_WRITTEN(2)),
  404. REG64(GEN7_SO_NUM_PRIMS_WRITTEN(3)),
  405. REG64(GEN7_SO_PRIM_STORAGE_NEEDED(0)),
  406. REG64(GEN7_SO_PRIM_STORAGE_NEEDED(1)),
  407. REG64(GEN7_SO_PRIM_STORAGE_NEEDED(2)),
  408. REG64(GEN7_SO_PRIM_STORAGE_NEEDED(3)),
  409. GEN7_SO_WRITE_OFFSET(0),
  410. GEN7_SO_WRITE_OFFSET(1),
  411. GEN7_SO_WRITE_OFFSET(2),
  412. GEN7_SO_WRITE_OFFSET(3),
  413. GEN7_L3SQCREG1,
  414. GEN7_L3CNTLREG2,
  415. GEN7_L3CNTLREG3,
  416. };
  417. static const u32 gen7_blt_regs[] = {
  418. BCS_SWCTRL,
  419. };
  420. static const u32 ivb_master_regs[] = {
  421. FORCEWAKE_MT,
  422. DERRMR,
  423. GEN7_PIPE_DE_LOAD_SL(PIPE_A),
  424. GEN7_PIPE_DE_LOAD_SL(PIPE_B),
  425. GEN7_PIPE_DE_LOAD_SL(PIPE_C),
  426. };
  427. static const u32 hsw_master_regs[] = {
  428. FORCEWAKE_MT,
  429. DERRMR,
  430. };
  431. #undef REG64
  432. static u32 gen7_render_get_cmd_length_mask(u32 cmd_header)
  433. {
  434. u32 client = (cmd_header & INSTR_CLIENT_MASK) >> INSTR_CLIENT_SHIFT;
  435. u32 subclient =
  436. (cmd_header & INSTR_SUBCLIENT_MASK) >> INSTR_SUBCLIENT_SHIFT;
  437. if (client == INSTR_MI_CLIENT)
  438. return 0x3F;
  439. else if (client == INSTR_RC_CLIENT) {
  440. if (subclient == INSTR_MEDIA_SUBCLIENT)
  441. return 0xFFFF;
  442. else
  443. return 0xFF;
  444. }
  445. DRM_DEBUG_DRIVER("CMD: Abnormal rcs cmd length! 0x%08X\n", cmd_header);
  446. return 0;
  447. }
  448. static u32 gen7_bsd_get_cmd_length_mask(u32 cmd_header)
  449. {
  450. u32 client = (cmd_header & INSTR_CLIENT_MASK) >> INSTR_CLIENT_SHIFT;
  451. u32 subclient =
  452. (cmd_header & INSTR_SUBCLIENT_MASK) >> INSTR_SUBCLIENT_SHIFT;
  453. if (client == INSTR_MI_CLIENT)
  454. return 0x3F;
  455. else if (client == INSTR_RC_CLIENT) {
  456. if (subclient == INSTR_MEDIA_SUBCLIENT)
  457. return 0xFFF;
  458. else
  459. return 0xFF;
  460. }
  461. DRM_DEBUG_DRIVER("CMD: Abnormal bsd cmd length! 0x%08X\n", cmd_header);
  462. return 0;
  463. }
  464. static u32 gen7_blt_get_cmd_length_mask(u32 cmd_header)
  465. {
  466. u32 client = (cmd_header & INSTR_CLIENT_MASK) >> INSTR_CLIENT_SHIFT;
  467. if (client == INSTR_MI_CLIENT)
  468. return 0x3F;
  469. else if (client == INSTR_BC_CLIENT)
  470. return 0xFF;
  471. DRM_DEBUG_DRIVER("CMD: Abnormal blt cmd length! 0x%08X\n", cmd_header);
  472. return 0;
  473. }
  474. static bool validate_cmds_sorted(struct intel_engine_cs *ring,
  475. const struct drm_i915_cmd_table *cmd_tables,
  476. int cmd_table_count)
  477. {
  478. int i;
  479. bool ret = true;
  480. if (!cmd_tables || cmd_table_count == 0)
  481. return true;
  482. for (i = 0; i < cmd_table_count; i++) {
  483. const struct drm_i915_cmd_table *table = &cmd_tables[i];
  484. u32 previous = 0;
  485. int j;
  486. for (j = 0; j < table->count; j++) {
  487. const struct drm_i915_cmd_descriptor *desc =
  488. &table->table[i];
  489. u32 curr = desc->cmd.value & desc->cmd.mask;
  490. if (curr < previous) {
  491. DRM_ERROR("CMD: table not sorted ring=%d table=%d entry=%d cmd=0x%08X prev=0x%08X\n",
  492. ring->id, i, j, curr, previous);
  493. ret = false;
  494. }
  495. previous = curr;
  496. }
  497. }
  498. return ret;
  499. }
  500. static bool check_sorted(int ring_id, const u32 *reg_table, int reg_count)
  501. {
  502. int i;
  503. u32 previous = 0;
  504. bool ret = true;
  505. for (i = 0; i < reg_count; i++) {
  506. u32 curr = reg_table[i];
  507. if (curr < previous) {
  508. DRM_ERROR("CMD: table not sorted ring=%d entry=%d reg=0x%08X prev=0x%08X\n",
  509. ring_id, i, curr, previous);
  510. ret = false;
  511. }
  512. previous = curr;
  513. }
  514. return ret;
  515. }
  516. static bool validate_regs_sorted(struct intel_engine_cs *ring)
  517. {
  518. return check_sorted(ring->id, ring->reg_table, ring->reg_count) &&
  519. check_sorted(ring->id, ring->master_reg_table,
  520. ring->master_reg_count);
  521. }
  522. struct cmd_node {
  523. const struct drm_i915_cmd_descriptor *desc;
  524. struct hlist_node node;
  525. };
  526. /*
  527. * Different command ranges have different numbers of bits for the opcode. For
  528. * example, MI commands use bits 31:23 while 3D commands use bits 31:16. The
  529. * problem is that, for example, MI commands use bits 22:16 for other fields
  530. * such as GGTT vs PPGTT bits. If we include those bits in the mask then when
  531. * we mask a command from a batch it could hash to the wrong bucket due to
  532. * non-opcode bits being set. But if we don't include those bits, some 3D
  533. * commands may hash to the same bucket due to not including opcode bits that
  534. * make the command unique. For now, we will risk hashing to the same bucket.
  535. *
  536. * If we attempt to generate a perfect hash, we should be able to look at bits
  537. * 31:29 of a command from a batch buffer and use the full mask for that
  538. * client. The existing INSTR_CLIENT_MASK/SHIFT defines can be used for this.
  539. */
  540. #define CMD_HASH_MASK STD_MI_OPCODE_MASK
  541. static int init_hash_table(struct intel_engine_cs *ring,
  542. const struct drm_i915_cmd_table *cmd_tables,
  543. int cmd_table_count)
  544. {
  545. int i, j;
  546. hash_init(ring->cmd_hash);
  547. for (i = 0; i < cmd_table_count; i++) {
  548. const struct drm_i915_cmd_table *table = &cmd_tables[i];
  549. for (j = 0; j < table->count; j++) {
  550. const struct drm_i915_cmd_descriptor *desc =
  551. &table->table[j];
  552. struct cmd_node *desc_node =
  553. kmalloc(sizeof(*desc_node), GFP_KERNEL);
  554. if (!desc_node)
  555. return -ENOMEM;
  556. desc_node->desc = desc;
  557. hash_add(ring->cmd_hash, &desc_node->node,
  558. desc->cmd.value & CMD_HASH_MASK);
  559. }
  560. }
  561. return 0;
  562. }
  563. static void fini_hash_table(struct intel_engine_cs *ring)
  564. {
  565. struct hlist_node *tmp;
  566. struct cmd_node *desc_node;
  567. int i;
  568. hash_for_each_safe(ring->cmd_hash, i, tmp, desc_node, node) {
  569. hash_del(&desc_node->node);
  570. kfree(desc_node);
  571. }
  572. }
  573. /**
  574. * i915_cmd_parser_init_ring() - set cmd parser related fields for a ringbuffer
  575. * @ring: the ringbuffer to initialize
  576. *
  577. * Optionally initializes fields related to batch buffer command parsing in the
  578. * struct intel_engine_cs based on whether the platform requires software
  579. * command parsing.
  580. *
  581. * Return: non-zero if initialization fails
  582. */
  583. int i915_cmd_parser_init_ring(struct intel_engine_cs *ring)
  584. {
  585. const struct drm_i915_cmd_table *cmd_tables;
  586. int cmd_table_count;
  587. int ret;
  588. if (!IS_GEN7(ring->dev))
  589. return 0;
  590. switch (ring->id) {
  591. case RCS:
  592. if (IS_HASWELL(ring->dev)) {
  593. cmd_tables = hsw_render_ring_cmds;
  594. cmd_table_count =
  595. ARRAY_SIZE(hsw_render_ring_cmds);
  596. } else {
  597. cmd_tables = gen7_render_cmds;
  598. cmd_table_count = ARRAY_SIZE(gen7_render_cmds);
  599. }
  600. ring->reg_table = gen7_render_regs;
  601. ring->reg_count = ARRAY_SIZE(gen7_render_regs);
  602. if (IS_HASWELL(ring->dev)) {
  603. ring->master_reg_table = hsw_master_regs;
  604. ring->master_reg_count = ARRAY_SIZE(hsw_master_regs);
  605. } else {
  606. ring->master_reg_table = ivb_master_regs;
  607. ring->master_reg_count = ARRAY_SIZE(ivb_master_regs);
  608. }
  609. ring->get_cmd_length_mask = gen7_render_get_cmd_length_mask;
  610. break;
  611. case VCS:
  612. cmd_tables = gen7_video_cmds;
  613. cmd_table_count = ARRAY_SIZE(gen7_video_cmds);
  614. ring->get_cmd_length_mask = gen7_bsd_get_cmd_length_mask;
  615. break;
  616. case BCS:
  617. if (IS_HASWELL(ring->dev)) {
  618. cmd_tables = hsw_blt_ring_cmds;
  619. cmd_table_count = ARRAY_SIZE(hsw_blt_ring_cmds);
  620. } else {
  621. cmd_tables = gen7_blt_cmds;
  622. cmd_table_count = ARRAY_SIZE(gen7_blt_cmds);
  623. }
  624. ring->reg_table = gen7_blt_regs;
  625. ring->reg_count = ARRAY_SIZE(gen7_blt_regs);
  626. if (IS_HASWELL(ring->dev)) {
  627. ring->master_reg_table = hsw_master_regs;
  628. ring->master_reg_count = ARRAY_SIZE(hsw_master_regs);
  629. } else {
  630. ring->master_reg_table = ivb_master_regs;
  631. ring->master_reg_count = ARRAY_SIZE(ivb_master_regs);
  632. }
  633. ring->get_cmd_length_mask = gen7_blt_get_cmd_length_mask;
  634. break;
  635. case VECS:
  636. cmd_tables = hsw_vebox_cmds;
  637. cmd_table_count = ARRAY_SIZE(hsw_vebox_cmds);
  638. /* VECS can use the same length_mask function as VCS */
  639. ring->get_cmd_length_mask = gen7_bsd_get_cmd_length_mask;
  640. break;
  641. default:
  642. DRM_ERROR("CMD: cmd_parser_init with unknown ring: %d\n",
  643. ring->id);
  644. BUG();
  645. }
  646. BUG_ON(!validate_cmds_sorted(ring, cmd_tables, cmd_table_count));
  647. BUG_ON(!validate_regs_sorted(ring));
  648. if (hash_empty(ring->cmd_hash)) {
  649. ret = init_hash_table(ring, cmd_tables, cmd_table_count);
  650. if (ret) {
  651. DRM_ERROR("CMD: cmd_parser_init failed!\n");
  652. fini_hash_table(ring);
  653. return ret;
  654. }
  655. }
  656. ring->needs_cmd_parser = true;
  657. return 0;
  658. }
  659. /**
  660. * i915_cmd_parser_fini_ring() - clean up cmd parser related fields
  661. * @ring: the ringbuffer to clean up
  662. *
  663. * Releases any resources related to command parsing that may have been
  664. * initialized for the specified ring.
  665. */
  666. void i915_cmd_parser_fini_ring(struct intel_engine_cs *ring)
  667. {
  668. if (!ring->needs_cmd_parser)
  669. return;
  670. fini_hash_table(ring);
  671. }
  672. static const struct drm_i915_cmd_descriptor*
  673. find_cmd_in_table(struct intel_engine_cs *ring,
  674. u32 cmd_header)
  675. {
  676. struct cmd_node *desc_node;
  677. hash_for_each_possible(ring->cmd_hash, desc_node, node,
  678. cmd_header & CMD_HASH_MASK) {
  679. const struct drm_i915_cmd_descriptor *desc = desc_node->desc;
  680. u32 masked_cmd = desc->cmd.mask & cmd_header;
  681. u32 masked_value = desc->cmd.value & desc->cmd.mask;
  682. if (masked_cmd == masked_value)
  683. return desc;
  684. }
  685. return NULL;
  686. }
  687. /*
  688. * Returns a pointer to a descriptor for the command specified by cmd_header.
  689. *
  690. * The caller must supply space for a default descriptor via the default_desc
  691. * parameter. If no descriptor for the specified command exists in the ring's
  692. * command parser tables, this function fills in default_desc based on the
  693. * ring's default length encoding and returns default_desc.
  694. */
  695. static const struct drm_i915_cmd_descriptor*
  696. find_cmd(struct intel_engine_cs *ring,
  697. u32 cmd_header,
  698. struct drm_i915_cmd_descriptor *default_desc)
  699. {
  700. const struct drm_i915_cmd_descriptor *desc;
  701. u32 mask;
  702. desc = find_cmd_in_table(ring, cmd_header);
  703. if (desc)
  704. return desc;
  705. mask = ring->get_cmd_length_mask(cmd_header);
  706. if (!mask)
  707. return NULL;
  708. BUG_ON(!default_desc);
  709. default_desc->flags = CMD_DESC_SKIP;
  710. default_desc->length.mask = mask;
  711. return default_desc;
  712. }
  713. static bool valid_reg(const u32 *table, int count, u32 addr)
  714. {
  715. if (table && count != 0) {
  716. int i;
  717. for (i = 0; i < count; i++) {
  718. if (table[i] == addr)
  719. return true;
  720. }
  721. }
  722. return false;
  723. }
  724. static u32 *vmap_batch(struct drm_i915_gem_object *obj)
  725. {
  726. int i;
  727. void *addr = NULL;
  728. struct sg_page_iter sg_iter;
  729. struct page **pages;
  730. pages = drm_malloc_ab(obj->base.size >> PAGE_SHIFT, sizeof(*pages));
  731. if (pages == NULL) {
  732. DRM_DEBUG_DRIVER("Failed to get space for pages\n");
  733. goto finish;
  734. }
  735. i = 0;
  736. for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents, 0) {
  737. pages[i] = sg_page_iter_page(&sg_iter);
  738. i++;
  739. }
  740. addr = vmap(pages, i, 0, PAGE_KERNEL);
  741. if (addr == NULL) {
  742. DRM_DEBUG_DRIVER("Failed to vmap pages\n");
  743. goto finish;
  744. }
  745. finish:
  746. if (pages)
  747. drm_free_large(pages);
  748. return (u32*)addr;
  749. }
  750. /**
  751. * i915_needs_cmd_parser() - should a given ring use software command parsing?
  752. * @ring: the ring in question
  753. *
  754. * Only certain platforms require software batch buffer command parsing, and
  755. * only when enabled via module parameter.
  756. *
  757. * Return: true if the ring requires software command parsing
  758. */
  759. bool i915_needs_cmd_parser(struct intel_engine_cs *ring)
  760. {
  761. if (!ring->needs_cmd_parser)
  762. return false;
  763. if (!USES_PPGTT(ring->dev))
  764. return false;
  765. return (i915.enable_cmd_parser == 1);
  766. }
  767. static bool check_cmd(const struct intel_engine_cs *ring,
  768. const struct drm_i915_cmd_descriptor *desc,
  769. const u32 *cmd,
  770. const bool is_master,
  771. bool *oacontrol_set)
  772. {
  773. if (desc->flags & CMD_DESC_REJECT) {
  774. DRM_DEBUG_DRIVER("CMD: Rejected command: 0x%08X\n", *cmd);
  775. return false;
  776. }
  777. if ((desc->flags & CMD_DESC_MASTER) && !is_master) {
  778. DRM_DEBUG_DRIVER("CMD: Rejected master-only command: 0x%08X\n",
  779. *cmd);
  780. return false;
  781. }
  782. if (desc->flags & CMD_DESC_REGISTER) {
  783. u32 reg_addr = cmd[desc->reg.offset] & desc->reg.mask;
  784. /*
  785. * OACONTROL requires some special handling for writes. We
  786. * want to make sure that any batch which enables OA also
  787. * disables it before the end of the batch. The goal is to
  788. * prevent one process from snooping on the perf data from
  789. * another process. To do that, we need to check the value
  790. * that will be written to the register. Hence, limit
  791. * OACONTROL writes to only MI_LOAD_REGISTER_IMM commands.
  792. */
  793. if (reg_addr == OACONTROL) {
  794. if (desc->cmd.value == MI_LOAD_REGISTER_MEM) {
  795. DRM_DEBUG_DRIVER("CMD: Rejected LRM to OACONTROL\n");
  796. return false;
  797. }
  798. if (desc->cmd.value == MI_LOAD_REGISTER_IMM(1))
  799. *oacontrol_set = (cmd[2] != 0);
  800. }
  801. if (!valid_reg(ring->reg_table,
  802. ring->reg_count, reg_addr)) {
  803. if (!is_master ||
  804. !valid_reg(ring->master_reg_table,
  805. ring->master_reg_count,
  806. reg_addr)) {
  807. DRM_DEBUG_DRIVER("CMD: Rejected register 0x%08X in command: 0x%08X (ring=%d)\n",
  808. reg_addr,
  809. *cmd,
  810. ring->id);
  811. return false;
  812. }
  813. }
  814. }
  815. if (desc->flags & CMD_DESC_BITMASK) {
  816. int i;
  817. for (i = 0; i < MAX_CMD_DESC_BITMASKS; i++) {
  818. u32 dword;
  819. if (desc->bits[i].mask == 0)
  820. break;
  821. if (desc->bits[i].condition_mask != 0) {
  822. u32 offset =
  823. desc->bits[i].condition_offset;
  824. u32 condition = cmd[offset] &
  825. desc->bits[i].condition_mask;
  826. if (condition == 0)
  827. continue;
  828. }
  829. dword = cmd[desc->bits[i].offset] &
  830. desc->bits[i].mask;
  831. if (dword != desc->bits[i].expected) {
  832. DRM_DEBUG_DRIVER("CMD: Rejected command 0x%08X for bitmask 0x%08X (exp=0x%08X act=0x%08X) (ring=%d)\n",
  833. *cmd,
  834. desc->bits[i].mask,
  835. desc->bits[i].expected,
  836. dword, ring->id);
  837. return false;
  838. }
  839. }
  840. }
  841. return true;
  842. }
  843. #define LENGTH_BIAS 2
  844. /**
  845. * i915_parse_cmds() - parse a submitted batch buffer for privilege violations
  846. * @ring: the ring on which the batch is to execute
  847. * @batch_obj: the batch buffer in question
  848. * @batch_start_offset: byte offset in the batch at which execution starts
  849. * @is_master: is the submitting process the drm master?
  850. *
  851. * Parses the specified batch buffer looking for privilege violations as
  852. * described in the overview.
  853. *
  854. * Return: non-zero if the parser finds violations or otherwise fails; -EACCES
  855. * if the batch appears legal but should use hardware parsing
  856. */
  857. int i915_parse_cmds(struct intel_engine_cs *ring,
  858. struct drm_i915_gem_object *batch_obj,
  859. u32 batch_start_offset,
  860. bool is_master)
  861. {
  862. int ret = 0;
  863. u32 *cmd, *batch_base, *batch_end;
  864. struct drm_i915_cmd_descriptor default_desc = { 0 };
  865. int needs_clflush = 0;
  866. bool oacontrol_set = false; /* OACONTROL tracking. See check_cmd() */
  867. ret = i915_gem_obj_prepare_shmem_read(batch_obj, &needs_clflush);
  868. if (ret) {
  869. DRM_DEBUG_DRIVER("CMD: failed to prep read\n");
  870. return ret;
  871. }
  872. batch_base = vmap_batch(batch_obj);
  873. if (!batch_base) {
  874. DRM_DEBUG_DRIVER("CMD: Failed to vmap batch\n");
  875. i915_gem_object_unpin_pages(batch_obj);
  876. return -ENOMEM;
  877. }
  878. if (needs_clflush)
  879. drm_clflush_virt_range((char *)batch_base, batch_obj->base.size);
  880. cmd = batch_base + (batch_start_offset / sizeof(*cmd));
  881. batch_end = cmd + (batch_obj->base.size / sizeof(*batch_end));
  882. while (cmd < batch_end) {
  883. const struct drm_i915_cmd_descriptor *desc;
  884. u32 length;
  885. if (*cmd == MI_BATCH_BUFFER_END)
  886. break;
  887. desc = find_cmd(ring, *cmd, &default_desc);
  888. if (!desc) {
  889. DRM_DEBUG_DRIVER("CMD: Unrecognized command: 0x%08X\n",
  890. *cmd);
  891. ret = -EINVAL;
  892. break;
  893. }
  894. /*
  895. * If the batch buffer contains a chained batch, return an
  896. * error that tells the caller to abort and dispatch the
  897. * workload as a non-secure batch.
  898. */
  899. if (desc->cmd.value == MI_BATCH_BUFFER_START) {
  900. ret = -EACCES;
  901. break;
  902. }
  903. if (desc->flags & CMD_DESC_FIXED)
  904. length = desc->length.fixed;
  905. else
  906. length = ((*cmd & desc->length.mask) + LENGTH_BIAS);
  907. if ((batch_end - cmd) < length) {
  908. DRM_DEBUG_DRIVER("CMD: Command length exceeds batch length: 0x%08X length=%u batchlen=%td\n",
  909. *cmd,
  910. length,
  911. batch_end - cmd);
  912. ret = -EINVAL;
  913. break;
  914. }
  915. if (!check_cmd(ring, desc, cmd, is_master, &oacontrol_set)) {
  916. ret = -EINVAL;
  917. break;
  918. }
  919. cmd += length;
  920. }
  921. if (oacontrol_set) {
  922. DRM_DEBUG_DRIVER("CMD: batch set OACONTROL but did not clear it\n");
  923. ret = -EINVAL;
  924. }
  925. if (cmd >= batch_end) {
  926. DRM_DEBUG_DRIVER("CMD: Got to the end of the buffer w/o a BBE cmd!\n");
  927. ret = -EINVAL;
  928. }
  929. vunmap(batch_base);
  930. i915_gem_object_unpin_pages(batch_obj);
  931. return ret;
  932. }
  933. /**
  934. * i915_cmd_parser_get_version() - get the cmd parser version number
  935. *
  936. * The cmd parser maintains a simple increasing integer version number suitable
  937. * for passing to userspace clients to determine what operations are permitted.
  938. *
  939. * Return: the current version number of the cmd parser
  940. */
  941. int i915_cmd_parser_get_version(void)
  942. {
  943. /*
  944. * Command parser version history
  945. *
  946. * 1. Initial version. Checks batches and reports violations, but leaves
  947. * hardware parsing enabled (so does not allow new use cases).
  948. * 2. Allow access to the MI_PREDICATE_SRC0 and
  949. * MI_PREDICATE_SRC1 registers.
  950. */
  951. return 2;
  952. }