i915_cmd_parser.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324
  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 engine (e.g. PIPE_CONTROL and MI_FLUSH_DW).
  64. *
  65. * Implementation:
  66. * Each engine maintains tables of commands and registers which the parser
  67. * uses in scanning batch buffers submitted to that engine.
  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-engine 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-engine 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, .step = 2 } ),
  121. CMD( MI_STORE_REGISTER_MEM, SMI, F, 3, 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, 3, 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_SET_APPID, SMI, F, 1, S ),
  148. CMD( MI_DISPLAY_FLIP, SMI, !F, 0xFF, R ),
  149. CMD( MI_SET_CONTEXT, SMI, !F, 0xFF, R ),
  150. CMD( MI_URB_CLEAR, SMI, !F, 0xFF, S ),
  151. CMD( MI_STORE_DWORD_IMM, SMI, !F, 0x3F, B,
  152. .bits = {{
  153. .offset = 0,
  154. .mask = MI_GLOBAL_GTT,
  155. .expected = 0,
  156. }}, ),
  157. CMD( MI_UPDATE_GTT, SMI, !F, 0xFF, R ),
  158. CMD( MI_CLFLUSH, SMI, !F, 0x3FF, B,
  159. .bits = {{
  160. .offset = 0,
  161. .mask = MI_GLOBAL_GTT,
  162. .expected = 0,
  163. }}, ),
  164. CMD( MI_REPORT_PERF_COUNT, SMI, !F, 0x3F, B,
  165. .bits = {{
  166. .offset = 1,
  167. .mask = MI_REPORT_PERF_COUNT_GGTT,
  168. .expected = 0,
  169. }}, ),
  170. CMD( MI_CONDITIONAL_BATCH_BUFFER_END, SMI, !F, 0xFF, B,
  171. .bits = {{
  172. .offset = 0,
  173. .mask = MI_GLOBAL_GTT,
  174. .expected = 0,
  175. }}, ),
  176. CMD( GFX_OP_3DSTATE_VF_STATISTICS, S3D, F, 1, S ),
  177. CMD( PIPELINE_SELECT, S3D, F, 1, S ),
  178. CMD( MEDIA_VFE_STATE, S3D, !F, 0xFFFF, B,
  179. .bits = {{
  180. .offset = 2,
  181. .mask = MEDIA_VFE_STATE_MMIO_ACCESS_MASK,
  182. .expected = 0,
  183. }}, ),
  184. CMD( GPGPU_OBJECT, S3D, !F, 0xFF, S ),
  185. CMD( GPGPU_WALKER, S3D, !F, 0xFF, S ),
  186. CMD( GFX_OP_3DSTATE_SO_DECL_LIST, S3D, !F, 0x1FF, S ),
  187. CMD( GFX_OP_PIPE_CONTROL(5), S3D, !F, 0xFF, B,
  188. .bits = {{
  189. .offset = 1,
  190. .mask = (PIPE_CONTROL_MMIO_WRITE | PIPE_CONTROL_NOTIFY),
  191. .expected = 0,
  192. },
  193. {
  194. .offset = 1,
  195. .mask = (PIPE_CONTROL_GLOBAL_GTT_IVB |
  196. PIPE_CONTROL_STORE_DATA_INDEX),
  197. .expected = 0,
  198. .condition_offset = 1,
  199. .condition_mask = PIPE_CONTROL_POST_SYNC_OP_MASK,
  200. }}, ),
  201. };
  202. static const struct drm_i915_cmd_descriptor hsw_render_cmds[] = {
  203. CMD( MI_SET_PREDICATE, SMI, F, 1, S ),
  204. CMD( MI_RS_CONTROL, SMI, F, 1, S ),
  205. CMD( MI_URB_ATOMIC_ALLOC, SMI, F, 1, S ),
  206. CMD( MI_SET_APPID, SMI, F, 1, S ),
  207. CMD( MI_RS_CONTEXT, SMI, F, 1, S ),
  208. CMD( MI_LOAD_SCAN_LINES_INCL, SMI, !F, 0x3F, M ),
  209. CMD( MI_LOAD_SCAN_LINES_EXCL, SMI, !F, 0x3F, R ),
  210. CMD( MI_LOAD_REGISTER_REG, SMI, !F, 0xFF, W,
  211. .reg = { .offset = 1, .mask = 0x007FFFFC, .step = 1 } ),
  212. CMD( MI_RS_STORE_DATA_IMM, SMI, !F, 0xFF, S ),
  213. CMD( MI_LOAD_URB_MEM, SMI, !F, 0xFF, S ),
  214. CMD( MI_STORE_URB_MEM, SMI, !F, 0xFF, S ),
  215. CMD( GFX_OP_3DSTATE_DX9_CONSTANTF_VS, S3D, !F, 0x7FF, S ),
  216. CMD( GFX_OP_3DSTATE_DX9_CONSTANTF_PS, S3D, !F, 0x7FF, S ),
  217. CMD( GFX_OP_3DSTATE_BINDING_TABLE_EDIT_VS, S3D, !F, 0x1FF, S ),
  218. CMD( GFX_OP_3DSTATE_BINDING_TABLE_EDIT_GS, S3D, !F, 0x1FF, S ),
  219. CMD( GFX_OP_3DSTATE_BINDING_TABLE_EDIT_HS, S3D, !F, 0x1FF, S ),
  220. CMD( GFX_OP_3DSTATE_BINDING_TABLE_EDIT_DS, S3D, !F, 0x1FF, S ),
  221. CMD( GFX_OP_3DSTATE_BINDING_TABLE_EDIT_PS, S3D, !F, 0x1FF, S ),
  222. };
  223. static const struct drm_i915_cmd_descriptor video_cmds[] = {
  224. CMD( MI_ARB_ON_OFF, SMI, F, 1, R ),
  225. CMD( MI_SET_APPID, SMI, F, 1, S ),
  226. CMD( MI_STORE_DWORD_IMM, SMI, !F, 0xFF, B,
  227. .bits = {{
  228. .offset = 0,
  229. .mask = MI_GLOBAL_GTT,
  230. .expected = 0,
  231. }}, ),
  232. CMD( MI_UPDATE_GTT, SMI, !F, 0x3F, R ),
  233. CMD( MI_FLUSH_DW, SMI, !F, 0x3F, B,
  234. .bits = {{
  235. .offset = 0,
  236. .mask = MI_FLUSH_DW_NOTIFY,
  237. .expected = 0,
  238. },
  239. {
  240. .offset = 1,
  241. .mask = MI_FLUSH_DW_USE_GTT,
  242. .expected = 0,
  243. .condition_offset = 0,
  244. .condition_mask = MI_FLUSH_DW_OP_MASK,
  245. },
  246. {
  247. .offset = 0,
  248. .mask = MI_FLUSH_DW_STORE_INDEX,
  249. .expected = 0,
  250. .condition_offset = 0,
  251. .condition_mask = MI_FLUSH_DW_OP_MASK,
  252. }}, ),
  253. CMD( MI_CONDITIONAL_BATCH_BUFFER_END, SMI, !F, 0xFF, B,
  254. .bits = {{
  255. .offset = 0,
  256. .mask = MI_GLOBAL_GTT,
  257. .expected = 0,
  258. }}, ),
  259. /*
  260. * MFX_WAIT doesn't fit the way we handle length for most commands.
  261. * It has a length field but it uses a non-standard length bias.
  262. * It is always 1 dword though, so just treat it as fixed length.
  263. */
  264. CMD( MFX_WAIT, SMFX, F, 1, S ),
  265. };
  266. static const struct drm_i915_cmd_descriptor vecs_cmds[] = {
  267. CMD( MI_ARB_ON_OFF, SMI, F, 1, R ),
  268. CMD( MI_SET_APPID, SMI, F, 1, S ),
  269. CMD( MI_STORE_DWORD_IMM, SMI, !F, 0xFF, B,
  270. .bits = {{
  271. .offset = 0,
  272. .mask = MI_GLOBAL_GTT,
  273. .expected = 0,
  274. }}, ),
  275. CMD( MI_UPDATE_GTT, SMI, !F, 0x3F, R ),
  276. CMD( MI_FLUSH_DW, SMI, !F, 0x3F, B,
  277. .bits = {{
  278. .offset = 0,
  279. .mask = MI_FLUSH_DW_NOTIFY,
  280. .expected = 0,
  281. },
  282. {
  283. .offset = 1,
  284. .mask = MI_FLUSH_DW_USE_GTT,
  285. .expected = 0,
  286. .condition_offset = 0,
  287. .condition_mask = MI_FLUSH_DW_OP_MASK,
  288. },
  289. {
  290. .offset = 0,
  291. .mask = MI_FLUSH_DW_STORE_INDEX,
  292. .expected = 0,
  293. .condition_offset = 0,
  294. .condition_mask = MI_FLUSH_DW_OP_MASK,
  295. }}, ),
  296. CMD( MI_CONDITIONAL_BATCH_BUFFER_END, SMI, !F, 0xFF, B,
  297. .bits = {{
  298. .offset = 0,
  299. .mask = MI_GLOBAL_GTT,
  300. .expected = 0,
  301. }}, ),
  302. };
  303. static const struct drm_i915_cmd_descriptor blt_cmds[] = {
  304. CMD( MI_DISPLAY_FLIP, SMI, !F, 0xFF, R ),
  305. CMD( MI_STORE_DWORD_IMM, SMI, !F, 0x3FF, B,
  306. .bits = {{
  307. .offset = 0,
  308. .mask = MI_GLOBAL_GTT,
  309. .expected = 0,
  310. }}, ),
  311. CMD( MI_UPDATE_GTT, SMI, !F, 0x3F, R ),
  312. CMD( MI_FLUSH_DW, SMI, !F, 0x3F, B,
  313. .bits = {{
  314. .offset = 0,
  315. .mask = MI_FLUSH_DW_NOTIFY,
  316. .expected = 0,
  317. },
  318. {
  319. .offset = 1,
  320. .mask = MI_FLUSH_DW_USE_GTT,
  321. .expected = 0,
  322. .condition_offset = 0,
  323. .condition_mask = MI_FLUSH_DW_OP_MASK,
  324. },
  325. {
  326. .offset = 0,
  327. .mask = MI_FLUSH_DW_STORE_INDEX,
  328. .expected = 0,
  329. .condition_offset = 0,
  330. .condition_mask = MI_FLUSH_DW_OP_MASK,
  331. }}, ),
  332. CMD( COLOR_BLT, S2D, !F, 0x3F, S ),
  333. CMD( SRC_COPY_BLT, S2D, !F, 0x3F, S ),
  334. };
  335. static const struct drm_i915_cmd_descriptor hsw_blt_cmds[] = {
  336. CMD( MI_LOAD_SCAN_LINES_INCL, SMI, !F, 0x3F, M ),
  337. CMD( MI_LOAD_SCAN_LINES_EXCL, SMI, !F, 0x3F, R ),
  338. };
  339. #undef CMD
  340. #undef SMI
  341. #undef S3D
  342. #undef S2D
  343. #undef SMFX
  344. #undef F
  345. #undef S
  346. #undef R
  347. #undef W
  348. #undef B
  349. #undef M
  350. static const struct drm_i915_cmd_table gen7_render_cmds[] = {
  351. { common_cmds, ARRAY_SIZE(common_cmds) },
  352. { render_cmds, ARRAY_SIZE(render_cmds) },
  353. };
  354. static const struct drm_i915_cmd_table hsw_render_ring_cmds[] = {
  355. { common_cmds, ARRAY_SIZE(common_cmds) },
  356. { render_cmds, ARRAY_SIZE(render_cmds) },
  357. { hsw_render_cmds, ARRAY_SIZE(hsw_render_cmds) },
  358. };
  359. static const struct drm_i915_cmd_table gen7_video_cmds[] = {
  360. { common_cmds, ARRAY_SIZE(common_cmds) },
  361. { video_cmds, ARRAY_SIZE(video_cmds) },
  362. };
  363. static const struct drm_i915_cmd_table hsw_vebox_cmds[] = {
  364. { common_cmds, ARRAY_SIZE(common_cmds) },
  365. { vecs_cmds, ARRAY_SIZE(vecs_cmds) },
  366. };
  367. static const struct drm_i915_cmd_table gen7_blt_cmds[] = {
  368. { common_cmds, ARRAY_SIZE(common_cmds) },
  369. { blt_cmds, ARRAY_SIZE(blt_cmds) },
  370. };
  371. static const struct drm_i915_cmd_table hsw_blt_ring_cmds[] = {
  372. { common_cmds, ARRAY_SIZE(common_cmds) },
  373. { blt_cmds, ARRAY_SIZE(blt_cmds) },
  374. { hsw_blt_cmds, ARRAY_SIZE(hsw_blt_cmds) },
  375. };
  376. /*
  377. * Register whitelists, sorted by increasing register offset.
  378. */
  379. /*
  380. * An individual whitelist entry granting access to register addr. If
  381. * mask is non-zero the argument of immediate register writes will be
  382. * AND-ed with mask, and the command will be rejected if the result
  383. * doesn't match value.
  384. *
  385. * Registers with non-zero mask are only allowed to be written using
  386. * LRI.
  387. */
  388. struct drm_i915_reg_descriptor {
  389. i915_reg_t addr;
  390. u32 mask;
  391. u32 value;
  392. };
  393. /* Convenience macro for adding 32-bit registers. */
  394. #define REG32(_reg, ...) \
  395. { .addr = (_reg), __VA_ARGS__ }
  396. /*
  397. * Convenience macro for adding 64-bit registers.
  398. *
  399. * Some registers that userspace accesses are 64 bits. The register
  400. * access commands only allow 32-bit accesses. Hence, we have to include
  401. * entries for both halves of the 64-bit registers.
  402. */
  403. #define REG64(_reg) \
  404. { .addr = _reg }, \
  405. { .addr = _reg ## _UDW }
  406. #define REG64_IDX(_reg, idx) \
  407. { .addr = _reg(idx) }, \
  408. { .addr = _reg ## _UDW(idx) }
  409. static const struct drm_i915_reg_descriptor gen7_render_regs[] = {
  410. REG64(GPGPU_THREADS_DISPATCHED),
  411. REG64(HS_INVOCATION_COUNT),
  412. REG64(DS_INVOCATION_COUNT),
  413. REG64(IA_VERTICES_COUNT),
  414. REG64(IA_PRIMITIVES_COUNT),
  415. REG64(VS_INVOCATION_COUNT),
  416. REG64(GS_INVOCATION_COUNT),
  417. REG64(GS_PRIMITIVES_COUNT),
  418. REG64(CL_INVOCATION_COUNT),
  419. REG64(CL_PRIMITIVES_COUNT),
  420. REG64(PS_INVOCATION_COUNT),
  421. REG64(PS_DEPTH_COUNT),
  422. REG64_IDX(RING_TIMESTAMP, RENDER_RING_BASE),
  423. REG32(OACONTROL), /* Only allowed for LRI and SRM. See below. */
  424. REG64(MI_PREDICATE_SRC0),
  425. REG64(MI_PREDICATE_SRC1),
  426. REG32(GEN7_3DPRIM_END_OFFSET),
  427. REG32(GEN7_3DPRIM_START_VERTEX),
  428. REG32(GEN7_3DPRIM_VERTEX_COUNT),
  429. REG32(GEN7_3DPRIM_INSTANCE_COUNT),
  430. REG32(GEN7_3DPRIM_START_INSTANCE),
  431. REG32(GEN7_3DPRIM_BASE_VERTEX),
  432. REG32(GEN7_GPGPU_DISPATCHDIMX),
  433. REG32(GEN7_GPGPU_DISPATCHDIMY),
  434. REG32(GEN7_GPGPU_DISPATCHDIMZ),
  435. REG64_IDX(GEN7_SO_NUM_PRIMS_WRITTEN, 0),
  436. REG64_IDX(GEN7_SO_NUM_PRIMS_WRITTEN, 1),
  437. REG64_IDX(GEN7_SO_NUM_PRIMS_WRITTEN, 2),
  438. REG64_IDX(GEN7_SO_NUM_PRIMS_WRITTEN, 3),
  439. REG64_IDX(GEN7_SO_PRIM_STORAGE_NEEDED, 0),
  440. REG64_IDX(GEN7_SO_PRIM_STORAGE_NEEDED, 1),
  441. REG64_IDX(GEN7_SO_PRIM_STORAGE_NEEDED, 2),
  442. REG64_IDX(GEN7_SO_PRIM_STORAGE_NEEDED, 3),
  443. REG32(GEN7_SO_WRITE_OFFSET(0)),
  444. REG32(GEN7_SO_WRITE_OFFSET(1)),
  445. REG32(GEN7_SO_WRITE_OFFSET(2)),
  446. REG32(GEN7_SO_WRITE_OFFSET(3)),
  447. REG32(GEN7_L3SQCREG1),
  448. REG32(GEN7_L3CNTLREG2),
  449. REG32(GEN7_L3CNTLREG3),
  450. };
  451. static const struct drm_i915_reg_descriptor hsw_render_regs[] = {
  452. REG64_IDX(HSW_CS_GPR, 0),
  453. REG64_IDX(HSW_CS_GPR, 1),
  454. REG64_IDX(HSW_CS_GPR, 2),
  455. REG64_IDX(HSW_CS_GPR, 3),
  456. REG64_IDX(HSW_CS_GPR, 4),
  457. REG64_IDX(HSW_CS_GPR, 5),
  458. REG64_IDX(HSW_CS_GPR, 6),
  459. REG64_IDX(HSW_CS_GPR, 7),
  460. REG64_IDX(HSW_CS_GPR, 8),
  461. REG64_IDX(HSW_CS_GPR, 9),
  462. REG64_IDX(HSW_CS_GPR, 10),
  463. REG64_IDX(HSW_CS_GPR, 11),
  464. REG64_IDX(HSW_CS_GPR, 12),
  465. REG64_IDX(HSW_CS_GPR, 13),
  466. REG64_IDX(HSW_CS_GPR, 14),
  467. REG64_IDX(HSW_CS_GPR, 15),
  468. REG32(HSW_SCRATCH1,
  469. .mask = ~HSW_SCRATCH1_L3_DATA_ATOMICS_DISABLE,
  470. .value = 0),
  471. REG32(HSW_ROW_CHICKEN3,
  472. .mask = ~(HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE << 16 |
  473. HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE),
  474. .value = 0),
  475. };
  476. static const struct drm_i915_reg_descriptor gen7_blt_regs[] = {
  477. REG32(BCS_SWCTRL),
  478. };
  479. static const struct drm_i915_reg_descriptor ivb_master_regs[] = {
  480. REG32(FORCEWAKE_MT),
  481. REG32(DERRMR),
  482. REG32(GEN7_PIPE_DE_LOAD_SL(PIPE_A)),
  483. REG32(GEN7_PIPE_DE_LOAD_SL(PIPE_B)),
  484. REG32(GEN7_PIPE_DE_LOAD_SL(PIPE_C)),
  485. };
  486. static const struct drm_i915_reg_descriptor hsw_master_regs[] = {
  487. REG32(FORCEWAKE_MT),
  488. REG32(DERRMR),
  489. };
  490. #undef REG64
  491. #undef REG32
  492. struct drm_i915_reg_table {
  493. const struct drm_i915_reg_descriptor *regs;
  494. int num_regs;
  495. bool master;
  496. };
  497. static const struct drm_i915_reg_table ivb_render_reg_tables[] = {
  498. { gen7_render_regs, ARRAY_SIZE(gen7_render_regs), false },
  499. { ivb_master_regs, ARRAY_SIZE(ivb_master_regs), true },
  500. };
  501. static const struct drm_i915_reg_table ivb_blt_reg_tables[] = {
  502. { gen7_blt_regs, ARRAY_SIZE(gen7_blt_regs), false },
  503. { ivb_master_regs, ARRAY_SIZE(ivb_master_regs), true },
  504. };
  505. static const struct drm_i915_reg_table hsw_render_reg_tables[] = {
  506. { gen7_render_regs, ARRAY_SIZE(gen7_render_regs), false },
  507. { hsw_render_regs, ARRAY_SIZE(hsw_render_regs), false },
  508. { hsw_master_regs, ARRAY_SIZE(hsw_master_regs), true },
  509. };
  510. static const struct drm_i915_reg_table hsw_blt_reg_tables[] = {
  511. { gen7_blt_regs, ARRAY_SIZE(gen7_blt_regs), false },
  512. { hsw_master_regs, ARRAY_SIZE(hsw_master_regs), true },
  513. };
  514. static u32 gen7_render_get_cmd_length_mask(u32 cmd_header)
  515. {
  516. u32 client = (cmd_header & INSTR_CLIENT_MASK) >> INSTR_CLIENT_SHIFT;
  517. u32 subclient =
  518. (cmd_header & INSTR_SUBCLIENT_MASK) >> INSTR_SUBCLIENT_SHIFT;
  519. if (client == INSTR_MI_CLIENT)
  520. return 0x3F;
  521. else if (client == INSTR_RC_CLIENT) {
  522. if (subclient == INSTR_MEDIA_SUBCLIENT)
  523. return 0xFFFF;
  524. else
  525. return 0xFF;
  526. }
  527. DRM_DEBUG_DRIVER("CMD: Abnormal rcs cmd length! 0x%08X\n", cmd_header);
  528. return 0;
  529. }
  530. static u32 gen7_bsd_get_cmd_length_mask(u32 cmd_header)
  531. {
  532. u32 client = (cmd_header & INSTR_CLIENT_MASK) >> INSTR_CLIENT_SHIFT;
  533. u32 subclient =
  534. (cmd_header & INSTR_SUBCLIENT_MASK) >> INSTR_SUBCLIENT_SHIFT;
  535. u32 op = (cmd_header & INSTR_26_TO_24_MASK) >> INSTR_26_TO_24_SHIFT;
  536. if (client == INSTR_MI_CLIENT)
  537. return 0x3F;
  538. else if (client == INSTR_RC_CLIENT) {
  539. if (subclient == INSTR_MEDIA_SUBCLIENT) {
  540. if (op == 6)
  541. return 0xFFFF;
  542. else
  543. return 0xFFF;
  544. } else
  545. return 0xFF;
  546. }
  547. DRM_DEBUG_DRIVER("CMD: Abnormal bsd cmd length! 0x%08X\n", cmd_header);
  548. return 0;
  549. }
  550. static u32 gen7_blt_get_cmd_length_mask(u32 cmd_header)
  551. {
  552. u32 client = (cmd_header & INSTR_CLIENT_MASK) >> INSTR_CLIENT_SHIFT;
  553. if (client == INSTR_MI_CLIENT)
  554. return 0x3F;
  555. else if (client == INSTR_BC_CLIENT)
  556. return 0xFF;
  557. DRM_DEBUG_DRIVER("CMD: Abnormal blt cmd length! 0x%08X\n", cmd_header);
  558. return 0;
  559. }
  560. static bool validate_cmds_sorted(const struct intel_engine_cs *engine,
  561. const struct drm_i915_cmd_table *cmd_tables,
  562. int cmd_table_count)
  563. {
  564. int i;
  565. bool ret = true;
  566. if (!cmd_tables || cmd_table_count == 0)
  567. return true;
  568. for (i = 0; i < cmd_table_count; i++) {
  569. const struct drm_i915_cmd_table *table = &cmd_tables[i];
  570. u32 previous = 0;
  571. int j;
  572. for (j = 0; j < table->count; j++) {
  573. const struct drm_i915_cmd_descriptor *desc =
  574. &table->table[j];
  575. u32 curr = desc->cmd.value & desc->cmd.mask;
  576. if (curr < previous) {
  577. DRM_ERROR("CMD: %s [%d] command table not sorted: "
  578. "table=%d entry=%d cmd=0x%08X prev=0x%08X\n",
  579. engine->name, engine->id,
  580. i, j, curr, previous);
  581. ret = false;
  582. }
  583. previous = curr;
  584. }
  585. }
  586. return ret;
  587. }
  588. static bool check_sorted(const struct intel_engine_cs *engine,
  589. const struct drm_i915_reg_descriptor *reg_table,
  590. int reg_count)
  591. {
  592. int i;
  593. u32 previous = 0;
  594. bool ret = true;
  595. for (i = 0; i < reg_count; i++) {
  596. u32 curr = i915_mmio_reg_offset(reg_table[i].addr);
  597. if (curr < previous) {
  598. DRM_ERROR("CMD: %s [%d] register table not sorted: "
  599. "entry=%d reg=0x%08X prev=0x%08X\n",
  600. engine->name, engine->id,
  601. i, curr, previous);
  602. ret = false;
  603. }
  604. previous = curr;
  605. }
  606. return ret;
  607. }
  608. static bool validate_regs_sorted(struct intel_engine_cs *engine)
  609. {
  610. int i;
  611. const struct drm_i915_reg_table *table;
  612. for (i = 0; i < engine->reg_table_count; i++) {
  613. table = &engine->reg_tables[i];
  614. if (!check_sorted(engine, table->regs, table->num_regs))
  615. return false;
  616. }
  617. return true;
  618. }
  619. struct cmd_node {
  620. const struct drm_i915_cmd_descriptor *desc;
  621. struct hlist_node node;
  622. };
  623. /*
  624. * Different command ranges have different numbers of bits for the opcode. For
  625. * example, MI commands use bits 31:23 while 3D commands use bits 31:16. The
  626. * problem is that, for example, MI commands use bits 22:16 for other fields
  627. * such as GGTT vs PPGTT bits. If we include those bits in the mask then when
  628. * we mask a command from a batch it could hash to the wrong bucket due to
  629. * non-opcode bits being set. But if we don't include those bits, some 3D
  630. * commands may hash to the same bucket due to not including opcode bits that
  631. * make the command unique. For now, we will risk hashing to the same bucket.
  632. *
  633. * If we attempt to generate a perfect hash, we should be able to look at bits
  634. * 31:29 of a command from a batch buffer and use the full mask for that
  635. * client. The existing INSTR_CLIENT_MASK/SHIFT defines can be used for this.
  636. */
  637. #define CMD_HASH_MASK STD_MI_OPCODE_MASK
  638. static int init_hash_table(struct intel_engine_cs *engine,
  639. const struct drm_i915_cmd_table *cmd_tables,
  640. int cmd_table_count)
  641. {
  642. int i, j;
  643. hash_init(engine->cmd_hash);
  644. for (i = 0; i < cmd_table_count; i++) {
  645. const struct drm_i915_cmd_table *table = &cmd_tables[i];
  646. for (j = 0; j < table->count; j++) {
  647. const struct drm_i915_cmd_descriptor *desc =
  648. &table->table[j];
  649. struct cmd_node *desc_node =
  650. kmalloc(sizeof(*desc_node), GFP_KERNEL);
  651. if (!desc_node)
  652. return -ENOMEM;
  653. desc_node->desc = desc;
  654. hash_add(engine->cmd_hash, &desc_node->node,
  655. desc->cmd.value & CMD_HASH_MASK);
  656. }
  657. }
  658. return 0;
  659. }
  660. static void fini_hash_table(struct intel_engine_cs *engine)
  661. {
  662. struct hlist_node *tmp;
  663. struct cmd_node *desc_node;
  664. int i;
  665. hash_for_each_safe(engine->cmd_hash, i, tmp, desc_node, node) {
  666. hash_del(&desc_node->node);
  667. kfree(desc_node);
  668. }
  669. }
  670. /**
  671. * intel_engine_init_cmd_parser() - set cmd parser related fields for an engine
  672. * @engine: the engine to initialize
  673. *
  674. * Optionally initializes fields related to batch buffer command parsing in the
  675. * struct intel_engine_cs based on whether the platform requires software
  676. * command parsing.
  677. *
  678. * Return: non-zero if initialization fails
  679. */
  680. int intel_engine_init_cmd_parser(struct intel_engine_cs *engine)
  681. {
  682. const struct drm_i915_cmd_table *cmd_tables;
  683. int cmd_table_count;
  684. int ret;
  685. if (!IS_GEN7(engine->i915))
  686. return 0;
  687. switch (engine->id) {
  688. case RCS:
  689. if (IS_HASWELL(engine->i915)) {
  690. cmd_tables = hsw_render_ring_cmds;
  691. cmd_table_count =
  692. ARRAY_SIZE(hsw_render_ring_cmds);
  693. } else {
  694. cmd_tables = gen7_render_cmds;
  695. cmd_table_count = ARRAY_SIZE(gen7_render_cmds);
  696. }
  697. if (IS_HASWELL(engine->i915)) {
  698. engine->reg_tables = hsw_render_reg_tables;
  699. engine->reg_table_count = ARRAY_SIZE(hsw_render_reg_tables);
  700. } else {
  701. engine->reg_tables = ivb_render_reg_tables;
  702. engine->reg_table_count = ARRAY_SIZE(ivb_render_reg_tables);
  703. }
  704. engine->get_cmd_length_mask = gen7_render_get_cmd_length_mask;
  705. break;
  706. case VCS:
  707. cmd_tables = gen7_video_cmds;
  708. cmd_table_count = ARRAY_SIZE(gen7_video_cmds);
  709. engine->get_cmd_length_mask = gen7_bsd_get_cmd_length_mask;
  710. break;
  711. case BCS:
  712. if (IS_HASWELL(engine->i915)) {
  713. cmd_tables = hsw_blt_ring_cmds;
  714. cmd_table_count = ARRAY_SIZE(hsw_blt_ring_cmds);
  715. } else {
  716. cmd_tables = gen7_blt_cmds;
  717. cmd_table_count = ARRAY_SIZE(gen7_blt_cmds);
  718. }
  719. if (IS_HASWELL(engine->i915)) {
  720. engine->reg_tables = hsw_blt_reg_tables;
  721. engine->reg_table_count = ARRAY_SIZE(hsw_blt_reg_tables);
  722. } else {
  723. engine->reg_tables = ivb_blt_reg_tables;
  724. engine->reg_table_count = ARRAY_SIZE(ivb_blt_reg_tables);
  725. }
  726. engine->get_cmd_length_mask = gen7_blt_get_cmd_length_mask;
  727. break;
  728. case VECS:
  729. cmd_tables = hsw_vebox_cmds;
  730. cmd_table_count = ARRAY_SIZE(hsw_vebox_cmds);
  731. /* VECS can use the same length_mask function as VCS */
  732. engine->get_cmd_length_mask = gen7_bsd_get_cmd_length_mask;
  733. break;
  734. default:
  735. MISSING_CASE(engine->id);
  736. BUG();
  737. }
  738. BUG_ON(!validate_cmds_sorted(engine, cmd_tables, cmd_table_count));
  739. BUG_ON(!validate_regs_sorted(engine));
  740. WARN_ON(!hash_empty(engine->cmd_hash));
  741. ret = init_hash_table(engine, cmd_tables, cmd_table_count);
  742. if (ret) {
  743. DRM_ERROR("CMD: cmd_parser_init failed!\n");
  744. fini_hash_table(engine);
  745. return ret;
  746. }
  747. engine->needs_cmd_parser = true;
  748. return 0;
  749. }
  750. /**
  751. * intel_engine_cleanup_cmd_parser() - clean up cmd parser related fields
  752. * @engine: the engine to clean up
  753. *
  754. * Releases any resources related to command parsing that may have been
  755. * initialized for the specified engine.
  756. */
  757. void intel_engine_cleanup_cmd_parser(struct intel_engine_cs *engine)
  758. {
  759. if (!engine->needs_cmd_parser)
  760. return;
  761. fini_hash_table(engine);
  762. }
  763. static const struct drm_i915_cmd_descriptor*
  764. find_cmd_in_table(struct intel_engine_cs *engine,
  765. u32 cmd_header)
  766. {
  767. struct cmd_node *desc_node;
  768. hash_for_each_possible(engine->cmd_hash, desc_node, node,
  769. cmd_header & CMD_HASH_MASK) {
  770. const struct drm_i915_cmd_descriptor *desc = desc_node->desc;
  771. u32 masked_cmd = desc->cmd.mask & cmd_header;
  772. u32 masked_value = desc->cmd.value & desc->cmd.mask;
  773. if (masked_cmd == masked_value)
  774. return desc;
  775. }
  776. return NULL;
  777. }
  778. /*
  779. * Returns a pointer to a descriptor for the command specified by cmd_header.
  780. *
  781. * The caller must supply space for a default descriptor via the default_desc
  782. * parameter. If no descriptor for the specified command exists in the engine's
  783. * command parser tables, this function fills in default_desc based on the
  784. * engine's default length encoding and returns default_desc.
  785. */
  786. static const struct drm_i915_cmd_descriptor*
  787. find_cmd(struct intel_engine_cs *engine,
  788. u32 cmd_header,
  789. struct drm_i915_cmd_descriptor *default_desc)
  790. {
  791. const struct drm_i915_cmd_descriptor *desc;
  792. u32 mask;
  793. desc = find_cmd_in_table(engine, cmd_header);
  794. if (desc)
  795. return desc;
  796. mask = engine->get_cmd_length_mask(cmd_header);
  797. if (!mask)
  798. return NULL;
  799. BUG_ON(!default_desc);
  800. default_desc->flags = CMD_DESC_SKIP;
  801. default_desc->length.mask = mask;
  802. return default_desc;
  803. }
  804. static const struct drm_i915_reg_descriptor *
  805. find_reg(const struct drm_i915_reg_descriptor *table,
  806. int count, u32 addr)
  807. {
  808. int i;
  809. for (i = 0; i < count; i++) {
  810. if (i915_mmio_reg_offset(table[i].addr) == addr)
  811. return &table[i];
  812. }
  813. return NULL;
  814. }
  815. static const struct drm_i915_reg_descriptor *
  816. find_reg_in_tables(const struct drm_i915_reg_table *tables,
  817. int count, bool is_master, u32 addr)
  818. {
  819. int i;
  820. const struct drm_i915_reg_table *table;
  821. const struct drm_i915_reg_descriptor *reg;
  822. for (i = 0; i < count; i++) {
  823. table = &tables[i];
  824. if (!table->master || is_master) {
  825. reg = find_reg(table->regs, table->num_regs,
  826. addr);
  827. if (reg != NULL)
  828. return reg;
  829. }
  830. }
  831. return NULL;
  832. }
  833. static u32 *vmap_batch(struct drm_i915_gem_object *obj,
  834. unsigned start, unsigned len)
  835. {
  836. int i;
  837. void *addr = NULL;
  838. struct sg_page_iter sg_iter;
  839. int first_page = start >> PAGE_SHIFT;
  840. int last_page = (len + start + 4095) >> PAGE_SHIFT;
  841. int npages = last_page - first_page;
  842. struct page **pages;
  843. pages = drm_malloc_ab(npages, sizeof(*pages));
  844. if (pages == NULL) {
  845. DRM_DEBUG_DRIVER("Failed to get space for pages\n");
  846. goto finish;
  847. }
  848. i = 0;
  849. for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents, first_page) {
  850. pages[i++] = sg_page_iter_page(&sg_iter);
  851. if (i == npages)
  852. break;
  853. }
  854. addr = vmap(pages, i, 0, PAGE_KERNEL);
  855. if (addr == NULL) {
  856. DRM_DEBUG_DRIVER("Failed to vmap pages\n");
  857. goto finish;
  858. }
  859. finish:
  860. if (pages)
  861. drm_free_large(pages);
  862. return (u32*)addr;
  863. }
  864. /* Returns a vmap'd pointer to dest_obj, which the caller must unmap */
  865. static u32 *copy_batch(struct drm_i915_gem_object *dest_obj,
  866. struct drm_i915_gem_object *src_obj,
  867. u32 batch_start_offset,
  868. u32 batch_len)
  869. {
  870. int needs_clflush = 0;
  871. void *src_base, *src;
  872. void *dst = NULL;
  873. int ret;
  874. if (batch_len > dest_obj->base.size ||
  875. batch_len + batch_start_offset > src_obj->base.size)
  876. return ERR_PTR(-E2BIG);
  877. if (WARN_ON(dest_obj->pages_pin_count == 0))
  878. return ERR_PTR(-ENODEV);
  879. ret = i915_gem_obj_prepare_shmem_read(src_obj, &needs_clflush);
  880. if (ret) {
  881. DRM_DEBUG_DRIVER("CMD: failed to prepare shadow batch\n");
  882. return ERR_PTR(ret);
  883. }
  884. src_base = vmap_batch(src_obj, batch_start_offset, batch_len);
  885. if (!src_base) {
  886. DRM_DEBUG_DRIVER("CMD: Failed to vmap batch\n");
  887. ret = -ENOMEM;
  888. goto unpin_src;
  889. }
  890. ret = i915_gem_object_set_to_cpu_domain(dest_obj, true);
  891. if (ret) {
  892. DRM_DEBUG_DRIVER("CMD: Failed to set shadow batch to CPU\n");
  893. goto unmap_src;
  894. }
  895. dst = vmap_batch(dest_obj, 0, batch_len);
  896. if (!dst) {
  897. DRM_DEBUG_DRIVER("CMD: Failed to vmap shadow batch\n");
  898. ret = -ENOMEM;
  899. goto unmap_src;
  900. }
  901. src = src_base + offset_in_page(batch_start_offset);
  902. if (needs_clflush)
  903. drm_clflush_virt_range(src, batch_len);
  904. memcpy(dst, src, batch_len);
  905. unmap_src:
  906. vunmap(src_base);
  907. unpin_src:
  908. i915_gem_object_unpin_pages(src_obj);
  909. return ret ? ERR_PTR(ret) : dst;
  910. }
  911. /**
  912. * intel_engine_needs_cmd_parser() - should a given engine use software
  913. * command parsing?
  914. * @engine: the engine in question
  915. *
  916. * Only certain platforms require software batch buffer command parsing, and
  917. * only when enabled via module parameter.
  918. *
  919. * Return: true if the engine requires software command parsing
  920. */
  921. bool intel_engine_needs_cmd_parser(struct intel_engine_cs *engine)
  922. {
  923. if (!engine->needs_cmd_parser)
  924. return false;
  925. if (!USES_PPGTT(engine->i915))
  926. return false;
  927. return (i915.enable_cmd_parser == 1);
  928. }
  929. static bool check_cmd(const struct intel_engine_cs *engine,
  930. const struct drm_i915_cmd_descriptor *desc,
  931. const u32 *cmd, u32 length,
  932. const bool is_master,
  933. bool *oacontrol_set)
  934. {
  935. if (desc->flags & CMD_DESC_REJECT) {
  936. DRM_DEBUG_DRIVER("CMD: Rejected command: 0x%08X\n", *cmd);
  937. return false;
  938. }
  939. if ((desc->flags & CMD_DESC_MASTER) && !is_master) {
  940. DRM_DEBUG_DRIVER("CMD: Rejected master-only command: 0x%08X\n",
  941. *cmd);
  942. return false;
  943. }
  944. if (desc->flags & CMD_DESC_REGISTER) {
  945. /*
  946. * Get the distance between individual register offset
  947. * fields if the command can perform more than one
  948. * access at a time.
  949. */
  950. const u32 step = desc->reg.step ? desc->reg.step : length;
  951. u32 offset;
  952. for (offset = desc->reg.offset; offset < length;
  953. offset += step) {
  954. const u32 reg_addr = cmd[offset] & desc->reg.mask;
  955. const struct drm_i915_reg_descriptor *reg =
  956. find_reg_in_tables(engine->reg_tables,
  957. engine->reg_table_count,
  958. is_master,
  959. reg_addr);
  960. if (!reg) {
  961. DRM_DEBUG_DRIVER("CMD: Rejected register 0x%08X in command: 0x%08X (exec_id=%d)\n",
  962. reg_addr, *cmd, engine->exec_id);
  963. return false;
  964. }
  965. /*
  966. * OACONTROL requires some special handling for
  967. * writes. We want to make sure that any batch which
  968. * enables OA also disables it before the end of the
  969. * batch. The goal is to prevent one process from
  970. * snooping on the perf data from another process. To do
  971. * that, we need to check the value that will be written
  972. * to the register. Hence, limit OACONTROL writes to
  973. * only MI_LOAD_REGISTER_IMM commands.
  974. */
  975. if (reg_addr == i915_mmio_reg_offset(OACONTROL)) {
  976. if (desc->cmd.value == MI_LOAD_REGISTER_MEM) {
  977. DRM_DEBUG_DRIVER("CMD: Rejected LRM to OACONTROL\n");
  978. return false;
  979. }
  980. if (desc->cmd.value == MI_LOAD_REGISTER_REG) {
  981. DRM_DEBUG_DRIVER("CMD: Rejected LRR to OACONTROL\n");
  982. return false;
  983. }
  984. if (desc->cmd.value == MI_LOAD_REGISTER_IMM(1))
  985. *oacontrol_set = (cmd[offset + 1] != 0);
  986. }
  987. /*
  988. * Check the value written to the register against the
  989. * allowed mask/value pair given in the whitelist entry.
  990. */
  991. if (reg->mask) {
  992. if (desc->cmd.value == MI_LOAD_REGISTER_MEM) {
  993. DRM_DEBUG_DRIVER("CMD: Rejected LRM to masked register 0x%08X\n",
  994. reg_addr);
  995. return false;
  996. }
  997. if (desc->cmd.value == MI_LOAD_REGISTER_REG) {
  998. DRM_DEBUG_DRIVER("CMD: Rejected LRR to masked register 0x%08X\n",
  999. reg_addr);
  1000. return false;
  1001. }
  1002. if (desc->cmd.value == MI_LOAD_REGISTER_IMM(1) &&
  1003. (offset + 2 > length ||
  1004. (cmd[offset + 1] & reg->mask) != reg->value)) {
  1005. DRM_DEBUG_DRIVER("CMD: Rejected LRI to masked register 0x%08X\n",
  1006. reg_addr);
  1007. return false;
  1008. }
  1009. }
  1010. }
  1011. }
  1012. if (desc->flags & CMD_DESC_BITMASK) {
  1013. int i;
  1014. for (i = 0; i < MAX_CMD_DESC_BITMASKS; i++) {
  1015. u32 dword;
  1016. if (desc->bits[i].mask == 0)
  1017. break;
  1018. if (desc->bits[i].condition_mask != 0) {
  1019. u32 offset =
  1020. desc->bits[i].condition_offset;
  1021. u32 condition = cmd[offset] &
  1022. desc->bits[i].condition_mask;
  1023. if (condition == 0)
  1024. continue;
  1025. }
  1026. dword = cmd[desc->bits[i].offset] &
  1027. desc->bits[i].mask;
  1028. if (dword != desc->bits[i].expected) {
  1029. DRM_DEBUG_DRIVER("CMD: Rejected command 0x%08X for bitmask 0x%08X (exp=0x%08X act=0x%08X) (exec_id=%d)\n",
  1030. *cmd,
  1031. desc->bits[i].mask,
  1032. desc->bits[i].expected,
  1033. dword, engine->exec_id);
  1034. return false;
  1035. }
  1036. }
  1037. }
  1038. return true;
  1039. }
  1040. #define LENGTH_BIAS 2
  1041. /**
  1042. * i915_parse_cmds() - parse a submitted batch buffer for privilege violations
  1043. * @engine: the engine on which the batch is to execute
  1044. * @batch_obj: the batch buffer in question
  1045. * @shadow_batch_obj: copy of the batch buffer in question
  1046. * @batch_start_offset: byte offset in the batch at which execution starts
  1047. * @batch_len: length of the commands in batch_obj
  1048. * @is_master: is the submitting process the drm master?
  1049. *
  1050. * Parses the specified batch buffer looking for privilege violations as
  1051. * described in the overview.
  1052. *
  1053. * Return: non-zero if the parser finds violations or otherwise fails; -EACCES
  1054. * if the batch appears legal but should use hardware parsing
  1055. */
  1056. int intel_engine_cmd_parser(struct intel_engine_cs *engine,
  1057. struct drm_i915_gem_object *batch_obj,
  1058. struct drm_i915_gem_object *shadow_batch_obj,
  1059. u32 batch_start_offset,
  1060. u32 batch_len,
  1061. bool is_master)
  1062. {
  1063. u32 *cmd, *batch_base, *batch_end;
  1064. struct drm_i915_cmd_descriptor default_desc = { 0 };
  1065. bool oacontrol_set = false; /* OACONTROL tracking. See check_cmd() */
  1066. int ret = 0;
  1067. batch_base = copy_batch(shadow_batch_obj, batch_obj,
  1068. batch_start_offset, batch_len);
  1069. if (IS_ERR(batch_base)) {
  1070. DRM_DEBUG_DRIVER("CMD: Failed to copy batch\n");
  1071. return PTR_ERR(batch_base);
  1072. }
  1073. /*
  1074. * We use the batch length as size because the shadow object is as
  1075. * large or larger and copy_batch() will write MI_NOPs to the extra
  1076. * space. Parsing should be faster in some cases this way.
  1077. */
  1078. batch_end = batch_base + (batch_len / sizeof(*batch_end));
  1079. cmd = batch_base;
  1080. while (cmd < batch_end) {
  1081. const struct drm_i915_cmd_descriptor *desc;
  1082. u32 length;
  1083. if (*cmd == MI_BATCH_BUFFER_END)
  1084. break;
  1085. desc = find_cmd(engine, *cmd, &default_desc);
  1086. if (!desc) {
  1087. DRM_DEBUG_DRIVER("CMD: Unrecognized command: 0x%08X\n",
  1088. *cmd);
  1089. ret = -EINVAL;
  1090. break;
  1091. }
  1092. /*
  1093. * If the batch buffer contains a chained batch, return an
  1094. * error that tells the caller to abort and dispatch the
  1095. * workload as a non-secure batch.
  1096. */
  1097. if (desc->cmd.value == MI_BATCH_BUFFER_START) {
  1098. ret = -EACCES;
  1099. break;
  1100. }
  1101. if (desc->flags & CMD_DESC_FIXED)
  1102. length = desc->length.fixed;
  1103. else
  1104. length = ((*cmd & desc->length.mask) + LENGTH_BIAS);
  1105. if ((batch_end - cmd) < length) {
  1106. DRM_DEBUG_DRIVER("CMD: Command length exceeds batch length: 0x%08X length=%u batchlen=%td\n",
  1107. *cmd,
  1108. length,
  1109. batch_end - cmd);
  1110. ret = -EINVAL;
  1111. break;
  1112. }
  1113. if (!check_cmd(engine, desc, cmd, length, is_master,
  1114. &oacontrol_set)) {
  1115. ret = -EINVAL;
  1116. break;
  1117. }
  1118. cmd += length;
  1119. }
  1120. if (oacontrol_set) {
  1121. DRM_DEBUG_DRIVER("CMD: batch set OACONTROL but did not clear it\n");
  1122. ret = -EINVAL;
  1123. }
  1124. if (cmd >= batch_end) {
  1125. DRM_DEBUG_DRIVER("CMD: Got to the end of the buffer w/o a BBE cmd!\n");
  1126. ret = -EINVAL;
  1127. }
  1128. vunmap(batch_base);
  1129. return ret;
  1130. }
  1131. /**
  1132. * i915_cmd_parser_get_version() - get the cmd parser version number
  1133. * @dev_priv: i915 device private
  1134. *
  1135. * The cmd parser maintains a simple increasing integer version number suitable
  1136. * for passing to userspace clients to determine what operations are permitted.
  1137. *
  1138. * Return: the current version number of the cmd parser
  1139. */
  1140. int i915_cmd_parser_get_version(struct drm_i915_private *dev_priv)
  1141. {
  1142. struct intel_engine_cs *engine;
  1143. bool active = false;
  1144. /* If the command parser is not enabled, report 0 - unsupported */
  1145. for_each_engine(engine, dev_priv) {
  1146. if (intel_engine_needs_cmd_parser(engine)) {
  1147. active = true;
  1148. break;
  1149. }
  1150. }
  1151. if (!active)
  1152. return 0;
  1153. /*
  1154. * Command parser version history
  1155. *
  1156. * 1. Initial version. Checks batches and reports violations, but leaves
  1157. * hardware parsing enabled (so does not allow new use cases).
  1158. * 2. Allow access to the MI_PREDICATE_SRC0 and
  1159. * MI_PREDICATE_SRC1 registers.
  1160. * 3. Allow access to the GPGPU_THREADS_DISPATCHED register.
  1161. * 4. L3 atomic chicken bits of HSW_SCRATCH1 and HSW_ROW_CHICKEN3.
  1162. * 5. GPGPU dispatch compute indirect registers.
  1163. * 6. TIMESTAMP register and Haswell CS GPR registers
  1164. * 7. Allow MI_LOAD_REGISTER_REG between whitelisted registers.
  1165. */
  1166. return 7;
  1167. }