i915_cmd_parser.c 50 KB

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