i915_cmd_parser.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302
  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(RING_TIMESTAMP, BSD_RING_BASE),
  436. REG64_IDX(GEN7_SO_NUM_PRIMS_WRITTEN, 0),
  437. REG64_IDX(GEN7_SO_NUM_PRIMS_WRITTEN, 1),
  438. REG64_IDX(GEN7_SO_NUM_PRIMS_WRITTEN, 2),
  439. REG64_IDX(GEN7_SO_NUM_PRIMS_WRITTEN, 3),
  440. REG64_IDX(GEN7_SO_PRIM_STORAGE_NEEDED, 0),
  441. REG64_IDX(GEN7_SO_PRIM_STORAGE_NEEDED, 1),
  442. REG64_IDX(GEN7_SO_PRIM_STORAGE_NEEDED, 2),
  443. REG64_IDX(GEN7_SO_PRIM_STORAGE_NEEDED, 3),
  444. REG32(GEN7_SO_WRITE_OFFSET(0)),
  445. REG32(GEN7_SO_WRITE_OFFSET(1)),
  446. REG32(GEN7_SO_WRITE_OFFSET(2)),
  447. REG32(GEN7_SO_WRITE_OFFSET(3)),
  448. REG32(GEN7_L3SQCREG1),
  449. REG32(GEN7_L3CNTLREG2),
  450. REG32(GEN7_L3CNTLREG3),
  451. REG64_IDX(RING_TIMESTAMP, BLT_RING_BASE),
  452. };
  453. static const struct drm_i915_reg_descriptor hsw_render_regs[] = {
  454. REG64_IDX(HSW_CS_GPR, 0),
  455. REG64_IDX(HSW_CS_GPR, 1),
  456. REG64_IDX(HSW_CS_GPR, 2),
  457. REG64_IDX(HSW_CS_GPR, 3),
  458. REG64_IDX(HSW_CS_GPR, 4),
  459. REG64_IDX(HSW_CS_GPR, 5),
  460. REG64_IDX(HSW_CS_GPR, 6),
  461. REG64_IDX(HSW_CS_GPR, 7),
  462. REG64_IDX(HSW_CS_GPR, 8),
  463. REG64_IDX(HSW_CS_GPR, 9),
  464. REG64_IDX(HSW_CS_GPR, 10),
  465. REG64_IDX(HSW_CS_GPR, 11),
  466. REG64_IDX(HSW_CS_GPR, 12),
  467. REG64_IDX(HSW_CS_GPR, 13),
  468. REG64_IDX(HSW_CS_GPR, 14),
  469. REG64_IDX(HSW_CS_GPR, 15),
  470. REG32(HSW_SCRATCH1,
  471. .mask = ~HSW_SCRATCH1_L3_DATA_ATOMICS_DISABLE,
  472. .value = 0),
  473. REG32(HSW_ROW_CHICKEN3,
  474. .mask = ~(HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE << 16 |
  475. HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE),
  476. .value = 0),
  477. };
  478. static const struct drm_i915_reg_descriptor gen7_blt_regs[] = {
  479. REG64_IDX(RING_TIMESTAMP, RENDER_RING_BASE),
  480. REG64_IDX(RING_TIMESTAMP, BSD_RING_BASE),
  481. REG32(BCS_SWCTRL),
  482. REG64_IDX(RING_TIMESTAMP, BLT_RING_BASE),
  483. };
  484. static const struct drm_i915_reg_descriptor ivb_master_regs[] = {
  485. REG32(FORCEWAKE_MT),
  486. REG32(DERRMR),
  487. REG32(GEN7_PIPE_DE_LOAD_SL(PIPE_A)),
  488. REG32(GEN7_PIPE_DE_LOAD_SL(PIPE_B)),
  489. REG32(GEN7_PIPE_DE_LOAD_SL(PIPE_C)),
  490. };
  491. static const struct drm_i915_reg_descriptor hsw_master_regs[] = {
  492. REG32(FORCEWAKE_MT),
  493. REG32(DERRMR),
  494. };
  495. #undef REG64
  496. #undef REG32
  497. struct drm_i915_reg_table {
  498. const struct drm_i915_reg_descriptor *regs;
  499. int num_regs;
  500. bool master;
  501. };
  502. static const struct drm_i915_reg_table ivb_render_reg_tables[] = {
  503. { gen7_render_regs, ARRAY_SIZE(gen7_render_regs), false },
  504. { ivb_master_regs, ARRAY_SIZE(ivb_master_regs), true },
  505. };
  506. static const struct drm_i915_reg_table ivb_blt_reg_tables[] = {
  507. { gen7_blt_regs, ARRAY_SIZE(gen7_blt_regs), false },
  508. { ivb_master_regs, ARRAY_SIZE(ivb_master_regs), true },
  509. };
  510. static const struct drm_i915_reg_table hsw_render_reg_tables[] = {
  511. { gen7_render_regs, ARRAY_SIZE(gen7_render_regs), false },
  512. { hsw_render_regs, ARRAY_SIZE(hsw_render_regs), false },
  513. { hsw_master_regs, ARRAY_SIZE(hsw_master_regs), true },
  514. };
  515. static const struct drm_i915_reg_table hsw_blt_reg_tables[] = {
  516. { gen7_blt_regs, ARRAY_SIZE(gen7_blt_regs), false },
  517. { hsw_master_regs, ARRAY_SIZE(hsw_master_regs), true },
  518. };
  519. static u32 gen7_render_get_cmd_length_mask(u32 cmd_header)
  520. {
  521. u32 client = (cmd_header & INSTR_CLIENT_MASK) >> INSTR_CLIENT_SHIFT;
  522. u32 subclient =
  523. (cmd_header & INSTR_SUBCLIENT_MASK) >> INSTR_SUBCLIENT_SHIFT;
  524. if (client == INSTR_MI_CLIENT)
  525. return 0x3F;
  526. else if (client == INSTR_RC_CLIENT) {
  527. if (subclient == INSTR_MEDIA_SUBCLIENT)
  528. return 0xFFFF;
  529. else
  530. return 0xFF;
  531. }
  532. DRM_DEBUG_DRIVER("CMD: Abnormal rcs cmd length! 0x%08X\n", cmd_header);
  533. return 0;
  534. }
  535. static u32 gen7_bsd_get_cmd_length_mask(u32 cmd_header)
  536. {
  537. u32 client = (cmd_header & INSTR_CLIENT_MASK) >> INSTR_CLIENT_SHIFT;
  538. u32 subclient =
  539. (cmd_header & INSTR_SUBCLIENT_MASK) >> INSTR_SUBCLIENT_SHIFT;
  540. u32 op = (cmd_header & INSTR_26_TO_24_MASK) >> INSTR_26_TO_24_SHIFT;
  541. if (client == INSTR_MI_CLIENT)
  542. return 0x3F;
  543. else if (client == INSTR_RC_CLIENT) {
  544. if (subclient == INSTR_MEDIA_SUBCLIENT) {
  545. if (op == 6)
  546. return 0xFFFF;
  547. else
  548. return 0xFFF;
  549. } else
  550. return 0xFF;
  551. }
  552. DRM_DEBUG_DRIVER("CMD: Abnormal bsd cmd length! 0x%08X\n", cmd_header);
  553. return 0;
  554. }
  555. static u32 gen7_blt_get_cmd_length_mask(u32 cmd_header)
  556. {
  557. u32 client = (cmd_header & INSTR_CLIENT_MASK) >> INSTR_CLIENT_SHIFT;
  558. if (client == INSTR_MI_CLIENT)
  559. return 0x3F;
  560. else if (client == INSTR_BC_CLIENT)
  561. return 0xFF;
  562. DRM_DEBUG_DRIVER("CMD: Abnormal blt cmd length! 0x%08X\n", cmd_header);
  563. return 0;
  564. }
  565. static bool validate_cmds_sorted(const struct intel_engine_cs *engine,
  566. const struct drm_i915_cmd_table *cmd_tables,
  567. int cmd_table_count)
  568. {
  569. int i;
  570. bool ret = true;
  571. if (!cmd_tables || cmd_table_count == 0)
  572. return true;
  573. for (i = 0; i < cmd_table_count; i++) {
  574. const struct drm_i915_cmd_table *table = &cmd_tables[i];
  575. u32 previous = 0;
  576. int j;
  577. for (j = 0; j < table->count; j++) {
  578. const struct drm_i915_cmd_descriptor *desc =
  579. &table->table[j];
  580. u32 curr = desc->cmd.value & desc->cmd.mask;
  581. if (curr < previous) {
  582. DRM_ERROR("CMD: %s [%d] command table not sorted: "
  583. "table=%d entry=%d cmd=0x%08X prev=0x%08X\n",
  584. engine->name, engine->id,
  585. i, j, curr, previous);
  586. ret = false;
  587. }
  588. previous = curr;
  589. }
  590. }
  591. return ret;
  592. }
  593. static bool check_sorted(const struct intel_engine_cs *engine,
  594. const struct drm_i915_reg_descriptor *reg_table,
  595. int reg_count)
  596. {
  597. int i;
  598. u32 previous = 0;
  599. bool ret = true;
  600. for (i = 0; i < reg_count; i++) {
  601. u32 curr = i915_mmio_reg_offset(reg_table[i].addr);
  602. if (curr < previous) {
  603. DRM_ERROR("CMD: %s [%d] register table not sorted: "
  604. "entry=%d reg=0x%08X prev=0x%08X\n",
  605. engine->name, engine->id,
  606. i, curr, previous);
  607. ret = false;
  608. }
  609. previous = curr;
  610. }
  611. return ret;
  612. }
  613. static bool validate_regs_sorted(struct intel_engine_cs *engine)
  614. {
  615. int i;
  616. const struct drm_i915_reg_table *table;
  617. for (i = 0; i < engine->reg_table_count; i++) {
  618. table = &engine->reg_tables[i];
  619. if (!check_sorted(engine, table->regs, table->num_regs))
  620. return false;
  621. }
  622. return true;
  623. }
  624. struct cmd_node {
  625. const struct drm_i915_cmd_descriptor *desc;
  626. struct hlist_node node;
  627. };
  628. /*
  629. * Different command ranges have different numbers of bits for the opcode. For
  630. * example, MI commands use bits 31:23 while 3D commands use bits 31:16. The
  631. * problem is that, for example, MI commands use bits 22:16 for other fields
  632. * such as GGTT vs PPGTT bits. If we include those bits in the mask then when
  633. * we mask a command from a batch it could hash to the wrong bucket due to
  634. * non-opcode bits being set. But if we don't include those bits, some 3D
  635. * commands may hash to the same bucket due to not including opcode bits that
  636. * make the command unique. For now, we will risk hashing to the same bucket.
  637. *
  638. * If we attempt to generate a perfect hash, we should be able to look at bits
  639. * 31:29 of a command from a batch buffer and use the full mask for that
  640. * client. The existing INSTR_CLIENT_MASK/SHIFT defines can be used for this.
  641. */
  642. #define CMD_HASH_MASK STD_MI_OPCODE_MASK
  643. static int init_hash_table(struct intel_engine_cs *engine,
  644. const struct drm_i915_cmd_table *cmd_tables,
  645. int cmd_table_count)
  646. {
  647. int i, j;
  648. hash_init(engine->cmd_hash);
  649. for (i = 0; i < cmd_table_count; i++) {
  650. const struct drm_i915_cmd_table *table = &cmd_tables[i];
  651. for (j = 0; j < table->count; j++) {
  652. const struct drm_i915_cmd_descriptor *desc =
  653. &table->table[j];
  654. struct cmd_node *desc_node =
  655. kmalloc(sizeof(*desc_node), GFP_KERNEL);
  656. if (!desc_node)
  657. return -ENOMEM;
  658. desc_node->desc = desc;
  659. hash_add(engine->cmd_hash, &desc_node->node,
  660. desc->cmd.value & CMD_HASH_MASK);
  661. }
  662. }
  663. return 0;
  664. }
  665. static void fini_hash_table(struct intel_engine_cs *engine)
  666. {
  667. struct hlist_node *tmp;
  668. struct cmd_node *desc_node;
  669. int i;
  670. hash_for_each_safe(engine->cmd_hash, i, tmp, desc_node, node) {
  671. hash_del(&desc_node->node);
  672. kfree(desc_node);
  673. }
  674. }
  675. /**
  676. * intel_engine_init_cmd_parser() - set cmd parser related fields for an engine
  677. * @engine: the engine to initialize
  678. *
  679. * Optionally initializes fields related to batch buffer command parsing in the
  680. * struct intel_engine_cs based on whether the platform requires software
  681. * command parsing.
  682. */
  683. void intel_engine_init_cmd_parser(struct intel_engine_cs *engine)
  684. {
  685. const struct drm_i915_cmd_table *cmd_tables;
  686. int cmd_table_count;
  687. int ret;
  688. if (!IS_GEN7(engine->i915))
  689. return;
  690. switch (engine->id) {
  691. case RCS:
  692. if (IS_HASWELL(engine->i915)) {
  693. cmd_tables = hsw_render_ring_cmds;
  694. cmd_table_count =
  695. ARRAY_SIZE(hsw_render_ring_cmds);
  696. } else {
  697. cmd_tables = gen7_render_cmds;
  698. cmd_table_count = ARRAY_SIZE(gen7_render_cmds);
  699. }
  700. if (IS_HASWELL(engine->i915)) {
  701. engine->reg_tables = hsw_render_reg_tables;
  702. engine->reg_table_count = ARRAY_SIZE(hsw_render_reg_tables);
  703. } else {
  704. engine->reg_tables = ivb_render_reg_tables;
  705. engine->reg_table_count = ARRAY_SIZE(ivb_render_reg_tables);
  706. }
  707. engine->get_cmd_length_mask = gen7_render_get_cmd_length_mask;
  708. break;
  709. case VCS:
  710. cmd_tables = gen7_video_cmds;
  711. cmd_table_count = ARRAY_SIZE(gen7_video_cmds);
  712. engine->get_cmd_length_mask = gen7_bsd_get_cmd_length_mask;
  713. break;
  714. case BCS:
  715. if (IS_HASWELL(engine->i915)) {
  716. cmd_tables = hsw_blt_ring_cmds;
  717. cmd_table_count = ARRAY_SIZE(hsw_blt_ring_cmds);
  718. } else {
  719. cmd_tables = gen7_blt_cmds;
  720. cmd_table_count = ARRAY_SIZE(gen7_blt_cmds);
  721. }
  722. if (IS_HASWELL(engine->i915)) {
  723. engine->reg_tables = hsw_blt_reg_tables;
  724. engine->reg_table_count = ARRAY_SIZE(hsw_blt_reg_tables);
  725. } else {
  726. engine->reg_tables = ivb_blt_reg_tables;
  727. engine->reg_table_count = ARRAY_SIZE(ivb_blt_reg_tables);
  728. }
  729. engine->get_cmd_length_mask = gen7_blt_get_cmd_length_mask;
  730. break;
  731. case VECS:
  732. cmd_tables = hsw_vebox_cmds;
  733. cmd_table_count = ARRAY_SIZE(hsw_vebox_cmds);
  734. /* VECS can use the same length_mask function as VCS */
  735. engine->get_cmd_length_mask = gen7_bsd_get_cmd_length_mask;
  736. break;
  737. default:
  738. MISSING_CASE(engine->id);
  739. return;
  740. }
  741. if (!validate_cmds_sorted(engine, cmd_tables, cmd_table_count)) {
  742. DRM_ERROR("%s: command descriptions are not sorted\n",
  743. engine->name);
  744. return;
  745. }
  746. if (!validate_regs_sorted(engine)) {
  747. DRM_ERROR("%s: registers are not sorted\n", engine->name);
  748. return;
  749. }
  750. ret = init_hash_table(engine, cmd_tables, cmd_table_count);
  751. if (ret) {
  752. DRM_ERROR("%s: initialised failed!\n", engine->name);
  753. fini_hash_table(engine);
  754. return;
  755. }
  756. engine->needs_cmd_parser = true;
  757. }
  758. /**
  759. * intel_engine_cleanup_cmd_parser() - clean up cmd parser related fields
  760. * @engine: the engine to clean up
  761. *
  762. * Releases any resources related to command parsing that may have been
  763. * initialized for the specified engine.
  764. */
  765. void intel_engine_cleanup_cmd_parser(struct intel_engine_cs *engine)
  766. {
  767. if (!engine->needs_cmd_parser)
  768. return;
  769. fini_hash_table(engine);
  770. }
  771. static const struct drm_i915_cmd_descriptor*
  772. find_cmd_in_table(struct intel_engine_cs *engine,
  773. u32 cmd_header)
  774. {
  775. struct cmd_node *desc_node;
  776. hash_for_each_possible(engine->cmd_hash, desc_node, node,
  777. cmd_header & CMD_HASH_MASK) {
  778. const struct drm_i915_cmd_descriptor *desc = desc_node->desc;
  779. u32 masked_cmd = desc->cmd.mask & cmd_header;
  780. u32 masked_value = desc->cmd.value & desc->cmd.mask;
  781. if (masked_cmd == masked_value)
  782. return desc;
  783. }
  784. return NULL;
  785. }
  786. /*
  787. * Returns a pointer to a descriptor for the command specified by cmd_header.
  788. *
  789. * The caller must supply space for a default descriptor via the default_desc
  790. * parameter. If no descriptor for the specified command exists in the engine's
  791. * command parser tables, this function fills in default_desc based on the
  792. * engine's default length encoding and returns default_desc.
  793. */
  794. static const struct drm_i915_cmd_descriptor*
  795. find_cmd(struct intel_engine_cs *engine,
  796. u32 cmd_header,
  797. struct drm_i915_cmd_descriptor *default_desc)
  798. {
  799. const struct drm_i915_cmd_descriptor *desc;
  800. u32 mask;
  801. desc = find_cmd_in_table(engine, cmd_header);
  802. if (desc)
  803. return desc;
  804. mask = engine->get_cmd_length_mask(cmd_header);
  805. if (!mask)
  806. return NULL;
  807. BUG_ON(!default_desc);
  808. default_desc->flags = CMD_DESC_SKIP;
  809. default_desc->length.mask = mask;
  810. return default_desc;
  811. }
  812. static const struct drm_i915_reg_descriptor *
  813. find_reg(const struct drm_i915_reg_descriptor *table,
  814. int count, u32 addr)
  815. {
  816. int i;
  817. for (i = 0; i < count; i++) {
  818. if (i915_mmio_reg_offset(table[i].addr) == addr)
  819. return &table[i];
  820. }
  821. return NULL;
  822. }
  823. static const struct drm_i915_reg_descriptor *
  824. find_reg_in_tables(const struct drm_i915_reg_table *tables,
  825. int count, bool is_master, u32 addr)
  826. {
  827. int i;
  828. const struct drm_i915_reg_table *table;
  829. const struct drm_i915_reg_descriptor *reg;
  830. for (i = 0; i < count; i++) {
  831. table = &tables[i];
  832. if (!table->master || is_master) {
  833. reg = find_reg(table->regs, table->num_regs,
  834. addr);
  835. if (reg != NULL)
  836. return reg;
  837. }
  838. }
  839. return NULL;
  840. }
  841. /* Returns a vmap'd pointer to dst_obj, which the caller must unmap */
  842. static u32 *copy_batch(struct drm_i915_gem_object *dst_obj,
  843. struct drm_i915_gem_object *src_obj,
  844. u32 batch_start_offset,
  845. u32 batch_len,
  846. bool *needs_clflush_after)
  847. {
  848. unsigned int src_needs_clflush;
  849. unsigned int dst_needs_clflush;
  850. void *dst, *ptr;
  851. int offset, n;
  852. int ret;
  853. ret = i915_gem_obj_prepare_shmem_read(src_obj, &src_needs_clflush);
  854. if (ret)
  855. return ERR_PTR(ret);
  856. ret = i915_gem_obj_prepare_shmem_write(dst_obj, &dst_needs_clflush);
  857. if (ret) {
  858. dst = ERR_PTR(ret);
  859. goto unpin_src;
  860. }
  861. dst = i915_gem_object_pin_map(dst_obj, I915_MAP_WB);
  862. if (IS_ERR(dst))
  863. goto unpin_dst;
  864. ptr = dst;
  865. offset = offset_in_page(batch_start_offset);
  866. /* We can avoid clflushing partial cachelines before the write if we
  867. * only every write full cache-lines. Since we know that both the
  868. * source and destination are in multiples of PAGE_SIZE, we can simply
  869. * round up to the next cacheline. We don't care about copying too much
  870. * here as we only validate up to the end of the batch.
  871. */
  872. if (dst_needs_clflush & CLFLUSH_BEFORE)
  873. batch_len = roundup(batch_len, boot_cpu_data.x86_clflush_size);
  874. for (n = batch_start_offset >> PAGE_SHIFT; batch_len; n++) {
  875. int len = min_t(int, batch_len, PAGE_SIZE - offset);
  876. void *vaddr;
  877. vaddr = kmap_atomic(i915_gem_object_get_page(src_obj, n));
  878. if (src_needs_clflush)
  879. drm_clflush_virt_range(vaddr + offset, len);
  880. memcpy(ptr, vaddr + offset, len);
  881. kunmap_atomic(vaddr);
  882. ptr += len;
  883. batch_len -= len;
  884. offset = 0;
  885. }
  886. /* dst_obj is returned with vmap pinned */
  887. *needs_clflush_after = dst_needs_clflush & CLFLUSH_AFTER;
  888. unpin_dst:
  889. i915_gem_obj_finish_shmem_access(dst_obj);
  890. unpin_src:
  891. i915_gem_obj_finish_shmem_access(src_obj);
  892. return dst;
  893. }
  894. /**
  895. * intel_engine_needs_cmd_parser() - should a given engine use software
  896. * command parsing?
  897. * @engine: the engine in question
  898. *
  899. * Only certain platforms require software batch buffer command parsing, and
  900. * only when enabled via module parameter.
  901. *
  902. * Return: true if the engine requires software command parsing
  903. */
  904. bool intel_engine_needs_cmd_parser(struct intel_engine_cs *engine)
  905. {
  906. if (!engine->needs_cmd_parser)
  907. return false;
  908. if (!USES_PPGTT(engine->i915))
  909. return false;
  910. return (i915.enable_cmd_parser == 1);
  911. }
  912. static bool check_cmd(const struct intel_engine_cs *engine,
  913. const struct drm_i915_cmd_descriptor *desc,
  914. const u32 *cmd, u32 length,
  915. const bool is_master,
  916. bool *oacontrol_set)
  917. {
  918. if (desc->flags & CMD_DESC_REJECT) {
  919. DRM_DEBUG_DRIVER("CMD: Rejected command: 0x%08X\n", *cmd);
  920. return false;
  921. }
  922. if ((desc->flags & CMD_DESC_MASTER) && !is_master) {
  923. DRM_DEBUG_DRIVER("CMD: Rejected master-only command: 0x%08X\n",
  924. *cmd);
  925. return false;
  926. }
  927. if (desc->flags & CMD_DESC_REGISTER) {
  928. /*
  929. * Get the distance between individual register offset
  930. * fields if the command can perform more than one
  931. * access at a time.
  932. */
  933. const u32 step = desc->reg.step ? desc->reg.step : length;
  934. u32 offset;
  935. for (offset = desc->reg.offset; offset < length;
  936. offset += step) {
  937. const u32 reg_addr = cmd[offset] & desc->reg.mask;
  938. const struct drm_i915_reg_descriptor *reg =
  939. find_reg_in_tables(engine->reg_tables,
  940. engine->reg_table_count,
  941. is_master,
  942. reg_addr);
  943. if (!reg) {
  944. DRM_DEBUG_DRIVER("CMD: Rejected register 0x%08X in command: 0x%08X (exec_id=%d)\n",
  945. reg_addr, *cmd, engine->exec_id);
  946. return false;
  947. }
  948. /*
  949. * OACONTROL requires some special handling for
  950. * writes. We want to make sure that any batch which
  951. * enables OA also disables it before the end of the
  952. * batch. The goal is to prevent one process from
  953. * snooping on the perf data from another process. To do
  954. * that, we need to check the value that will be written
  955. * to the register. Hence, limit OACONTROL writes to
  956. * only MI_LOAD_REGISTER_IMM commands.
  957. */
  958. if (reg_addr == i915_mmio_reg_offset(OACONTROL)) {
  959. if (desc->cmd.value == MI_LOAD_REGISTER_MEM) {
  960. DRM_DEBUG_DRIVER("CMD: Rejected LRM to OACONTROL\n");
  961. return false;
  962. }
  963. if (desc->cmd.value == MI_LOAD_REGISTER_REG) {
  964. DRM_DEBUG_DRIVER("CMD: Rejected LRR to OACONTROL\n");
  965. return false;
  966. }
  967. if (desc->cmd.value == MI_LOAD_REGISTER_IMM(1))
  968. *oacontrol_set = (cmd[offset + 1] != 0);
  969. }
  970. /*
  971. * Check the value written to the register against the
  972. * allowed mask/value pair given in the whitelist entry.
  973. */
  974. if (reg->mask) {
  975. if (desc->cmd.value == MI_LOAD_REGISTER_MEM) {
  976. DRM_DEBUG_DRIVER("CMD: Rejected LRM to masked register 0x%08X\n",
  977. reg_addr);
  978. return false;
  979. }
  980. if (desc->cmd.value == MI_LOAD_REGISTER_REG) {
  981. DRM_DEBUG_DRIVER("CMD: Rejected LRR to masked register 0x%08X\n",
  982. reg_addr);
  983. return false;
  984. }
  985. if (desc->cmd.value == MI_LOAD_REGISTER_IMM(1) &&
  986. (offset + 2 > length ||
  987. (cmd[offset + 1] & reg->mask) != reg->value)) {
  988. DRM_DEBUG_DRIVER("CMD: Rejected LRI to masked register 0x%08X\n",
  989. reg_addr);
  990. return false;
  991. }
  992. }
  993. }
  994. }
  995. if (desc->flags & CMD_DESC_BITMASK) {
  996. int i;
  997. for (i = 0; i < MAX_CMD_DESC_BITMASKS; i++) {
  998. u32 dword;
  999. if (desc->bits[i].mask == 0)
  1000. break;
  1001. if (desc->bits[i].condition_mask != 0) {
  1002. u32 offset =
  1003. desc->bits[i].condition_offset;
  1004. u32 condition = cmd[offset] &
  1005. desc->bits[i].condition_mask;
  1006. if (condition == 0)
  1007. continue;
  1008. }
  1009. dword = cmd[desc->bits[i].offset] &
  1010. desc->bits[i].mask;
  1011. if (dword != desc->bits[i].expected) {
  1012. DRM_DEBUG_DRIVER("CMD: Rejected command 0x%08X for bitmask 0x%08X (exp=0x%08X act=0x%08X) (exec_id=%d)\n",
  1013. *cmd,
  1014. desc->bits[i].mask,
  1015. desc->bits[i].expected,
  1016. dword, engine->exec_id);
  1017. return false;
  1018. }
  1019. }
  1020. }
  1021. return true;
  1022. }
  1023. #define LENGTH_BIAS 2
  1024. /**
  1025. * i915_parse_cmds() - parse a submitted batch buffer for privilege violations
  1026. * @engine: the engine on which the batch is to execute
  1027. * @batch_obj: the batch buffer in question
  1028. * @shadow_batch_obj: copy of the batch buffer in question
  1029. * @batch_start_offset: byte offset in the batch at which execution starts
  1030. * @batch_len: length of the commands in batch_obj
  1031. * @is_master: is the submitting process the drm master?
  1032. *
  1033. * Parses the specified batch buffer looking for privilege violations as
  1034. * described in the overview.
  1035. *
  1036. * Return: non-zero if the parser finds violations or otherwise fails; -EACCES
  1037. * if the batch appears legal but should use hardware parsing
  1038. */
  1039. int intel_engine_cmd_parser(struct intel_engine_cs *engine,
  1040. struct drm_i915_gem_object *batch_obj,
  1041. struct drm_i915_gem_object *shadow_batch_obj,
  1042. u32 batch_start_offset,
  1043. u32 batch_len,
  1044. bool is_master)
  1045. {
  1046. u32 *cmd, *batch_end;
  1047. struct drm_i915_cmd_descriptor default_desc = { 0 };
  1048. bool oacontrol_set = false; /* OACONTROL tracking. See check_cmd() */
  1049. bool needs_clflush_after = false;
  1050. int ret = 0;
  1051. cmd = copy_batch(shadow_batch_obj, batch_obj,
  1052. batch_start_offset, batch_len,
  1053. &needs_clflush_after);
  1054. if (IS_ERR(cmd)) {
  1055. DRM_DEBUG_DRIVER("CMD: Failed to copy batch\n");
  1056. return PTR_ERR(cmd);
  1057. }
  1058. /*
  1059. * We use the batch length as size because the shadow object is as
  1060. * large or larger and copy_batch() will write MI_NOPs to the extra
  1061. * space. Parsing should be faster in some cases this way.
  1062. */
  1063. batch_end = cmd + (batch_len / sizeof(*batch_end));
  1064. while (cmd < batch_end) {
  1065. const struct drm_i915_cmd_descriptor *desc;
  1066. u32 length;
  1067. if (*cmd == MI_BATCH_BUFFER_END)
  1068. break;
  1069. desc = find_cmd(engine, *cmd, &default_desc);
  1070. if (!desc) {
  1071. DRM_DEBUG_DRIVER("CMD: Unrecognized command: 0x%08X\n",
  1072. *cmd);
  1073. ret = -EINVAL;
  1074. break;
  1075. }
  1076. /*
  1077. * If the batch buffer contains a chained batch, return an
  1078. * error that tells the caller to abort and dispatch the
  1079. * workload as a non-secure batch.
  1080. */
  1081. if (desc->cmd.value == MI_BATCH_BUFFER_START) {
  1082. ret = -EACCES;
  1083. break;
  1084. }
  1085. if (desc->flags & CMD_DESC_FIXED)
  1086. length = desc->length.fixed;
  1087. else
  1088. length = ((*cmd & desc->length.mask) + LENGTH_BIAS);
  1089. if ((batch_end - cmd) < length) {
  1090. DRM_DEBUG_DRIVER("CMD: Command length exceeds batch length: 0x%08X length=%u batchlen=%td\n",
  1091. *cmd,
  1092. length,
  1093. batch_end - cmd);
  1094. ret = -EINVAL;
  1095. break;
  1096. }
  1097. if (!check_cmd(engine, desc, cmd, length, is_master,
  1098. &oacontrol_set)) {
  1099. ret = -EINVAL;
  1100. break;
  1101. }
  1102. cmd += length;
  1103. }
  1104. if (oacontrol_set) {
  1105. DRM_DEBUG_DRIVER("CMD: batch set OACONTROL but did not clear it\n");
  1106. ret = -EINVAL;
  1107. }
  1108. if (cmd >= batch_end) {
  1109. DRM_DEBUG_DRIVER("CMD: Got to the end of the buffer w/o a BBE cmd!\n");
  1110. ret = -EINVAL;
  1111. }
  1112. if (ret == 0 && needs_clflush_after)
  1113. drm_clflush_virt_range(shadow_batch_obj->mapping, batch_len);
  1114. i915_gem_object_unpin_map(shadow_batch_obj);
  1115. return ret;
  1116. }
  1117. /**
  1118. * i915_cmd_parser_get_version() - get the cmd parser version number
  1119. * @dev_priv: i915 device private
  1120. *
  1121. * The cmd parser maintains a simple increasing integer version number suitable
  1122. * for passing to userspace clients to determine what operations are permitted.
  1123. *
  1124. * Return: the current version number of the cmd parser
  1125. */
  1126. int i915_cmd_parser_get_version(struct drm_i915_private *dev_priv)
  1127. {
  1128. struct intel_engine_cs *engine;
  1129. bool active = false;
  1130. /* If the command parser is not enabled, report 0 - unsupported */
  1131. for_each_engine(engine, dev_priv) {
  1132. if (intel_engine_needs_cmd_parser(engine)) {
  1133. active = true;
  1134. break;
  1135. }
  1136. }
  1137. if (!active)
  1138. return 0;
  1139. /*
  1140. * Command parser version history
  1141. *
  1142. * 1. Initial version. Checks batches and reports violations, but leaves
  1143. * hardware parsing enabled (so does not allow new use cases).
  1144. * 2. Allow access to the MI_PREDICATE_SRC0 and
  1145. * MI_PREDICATE_SRC1 registers.
  1146. * 3. Allow access to the GPGPU_THREADS_DISPATCHED register.
  1147. * 4. L3 atomic chicken bits of HSW_SCRATCH1 and HSW_ROW_CHICKEN3.
  1148. * 5. GPGPU dispatch compute indirect registers.
  1149. * 6. TIMESTAMP register and Haswell CS GPR registers
  1150. * 7. Allow MI_LOAD_REGISTER_REG between whitelisted registers.
  1151. */
  1152. return 7;
  1153. }