i915_cmd_parser.c 41 KB

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