i915_cmd_parser.c 36 KB

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