i915_cmd_parser.c 41 KB

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