i915_cmd_parser.c 40 KB

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