i915_cmd_parser.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380
  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. switch (x >> INSTR_CLIENT_SHIFT) {
  733. default:
  734. case INSTR_MI_CLIENT:
  735. return x >> STD_MI_OPCODE_SHIFT;
  736. case INSTR_RC_CLIENT:
  737. return x >> STD_3D_OPCODE_SHIFT;
  738. case INSTR_BC_CLIENT:
  739. return x >> STD_2D_OPCODE_SHIFT;
  740. }
  741. }
  742. static int init_hash_table(struct intel_engine_cs *engine,
  743. const struct drm_i915_cmd_table *cmd_tables,
  744. int cmd_table_count)
  745. {
  746. int i, j;
  747. hash_init(engine->cmd_hash);
  748. for (i = 0; i < cmd_table_count; i++) {
  749. const struct drm_i915_cmd_table *table = &cmd_tables[i];
  750. for (j = 0; j < table->count; j++) {
  751. const struct drm_i915_cmd_descriptor *desc =
  752. &table->table[j];
  753. struct cmd_node *desc_node =
  754. kmalloc(sizeof(*desc_node), GFP_KERNEL);
  755. if (!desc_node)
  756. return -ENOMEM;
  757. desc_node->desc = desc;
  758. hash_add(engine->cmd_hash, &desc_node->node,
  759. cmd_header_key(desc->cmd.value));
  760. }
  761. }
  762. return 0;
  763. }
  764. static void fini_hash_table(struct intel_engine_cs *engine)
  765. {
  766. struct hlist_node *tmp;
  767. struct cmd_node *desc_node;
  768. int i;
  769. hash_for_each_safe(engine->cmd_hash, i, tmp, desc_node, node) {
  770. hash_del(&desc_node->node);
  771. kfree(desc_node);
  772. }
  773. }
  774. /**
  775. * intel_engine_init_cmd_parser() - set cmd parser related fields for an engine
  776. * @engine: the engine to initialize
  777. *
  778. * Optionally initializes fields related to batch buffer command parsing in the
  779. * struct intel_engine_cs based on whether the platform requires software
  780. * command parsing.
  781. */
  782. void intel_engine_init_cmd_parser(struct intel_engine_cs *engine)
  783. {
  784. const struct drm_i915_cmd_table *cmd_tables;
  785. int cmd_table_count;
  786. int ret;
  787. if (!IS_GEN7(engine->i915))
  788. return;
  789. switch (engine->id) {
  790. case RCS:
  791. if (IS_HASWELL(engine->i915)) {
  792. cmd_tables = hsw_render_ring_cmds;
  793. cmd_table_count =
  794. ARRAY_SIZE(hsw_render_ring_cmds);
  795. } else {
  796. cmd_tables = gen7_render_cmds;
  797. cmd_table_count = ARRAY_SIZE(gen7_render_cmds);
  798. }
  799. if (IS_HASWELL(engine->i915)) {
  800. engine->reg_tables = hsw_render_reg_tables;
  801. engine->reg_table_count = ARRAY_SIZE(hsw_render_reg_tables);
  802. } else {
  803. engine->reg_tables = ivb_render_reg_tables;
  804. engine->reg_table_count = ARRAY_SIZE(ivb_render_reg_tables);
  805. }
  806. engine->get_cmd_length_mask = gen7_render_get_cmd_length_mask;
  807. break;
  808. case VCS:
  809. cmd_tables = gen7_video_cmds;
  810. cmd_table_count = ARRAY_SIZE(gen7_video_cmds);
  811. engine->get_cmd_length_mask = gen7_bsd_get_cmd_length_mask;
  812. break;
  813. case BCS:
  814. if (IS_HASWELL(engine->i915)) {
  815. cmd_tables = hsw_blt_ring_cmds;
  816. cmd_table_count = ARRAY_SIZE(hsw_blt_ring_cmds);
  817. } else {
  818. cmd_tables = gen7_blt_cmds;
  819. cmd_table_count = ARRAY_SIZE(gen7_blt_cmds);
  820. }
  821. if (IS_HASWELL(engine->i915)) {
  822. engine->reg_tables = hsw_blt_reg_tables;
  823. engine->reg_table_count = ARRAY_SIZE(hsw_blt_reg_tables);
  824. } else {
  825. engine->reg_tables = ivb_blt_reg_tables;
  826. engine->reg_table_count = ARRAY_SIZE(ivb_blt_reg_tables);
  827. }
  828. engine->get_cmd_length_mask = gen7_blt_get_cmd_length_mask;
  829. break;
  830. case VECS:
  831. cmd_tables = hsw_vebox_cmds;
  832. cmd_table_count = ARRAY_SIZE(hsw_vebox_cmds);
  833. /* VECS can use the same length_mask function as VCS */
  834. engine->get_cmd_length_mask = gen7_bsd_get_cmd_length_mask;
  835. break;
  836. default:
  837. MISSING_CASE(engine->id);
  838. return;
  839. }
  840. if (!validate_cmds_sorted(engine, cmd_tables, cmd_table_count)) {
  841. DRM_ERROR("%s: command descriptions are not sorted\n",
  842. engine->name);
  843. return;
  844. }
  845. if (!validate_regs_sorted(engine)) {
  846. DRM_ERROR("%s: registers are not sorted\n", engine->name);
  847. return;
  848. }
  849. ret = init_hash_table(engine, cmd_tables, cmd_table_count);
  850. if (ret) {
  851. DRM_ERROR("%s: initialised failed!\n", engine->name);
  852. fini_hash_table(engine);
  853. return;
  854. }
  855. engine->needs_cmd_parser = true;
  856. }
  857. /**
  858. * intel_engine_cleanup_cmd_parser() - clean up cmd parser related fields
  859. * @engine: the engine to clean up
  860. *
  861. * Releases any resources related to command parsing that may have been
  862. * initialized for the specified engine.
  863. */
  864. void intel_engine_cleanup_cmd_parser(struct intel_engine_cs *engine)
  865. {
  866. if (!engine->needs_cmd_parser)
  867. return;
  868. fini_hash_table(engine);
  869. }
  870. static const struct drm_i915_cmd_descriptor*
  871. find_cmd_in_table(struct intel_engine_cs *engine,
  872. u32 cmd_header)
  873. {
  874. struct cmd_node *desc_node;
  875. hash_for_each_possible(engine->cmd_hash, desc_node, node,
  876. cmd_header_key(cmd_header)) {
  877. const struct drm_i915_cmd_descriptor *desc = desc_node->desc;
  878. if (((cmd_header ^ desc->cmd.value) & desc->cmd.mask) == 0)
  879. return desc;
  880. }
  881. return NULL;
  882. }
  883. /*
  884. * Returns a pointer to a descriptor for the command specified by cmd_header.
  885. *
  886. * The caller must supply space for a default descriptor via the default_desc
  887. * parameter. If no descriptor for the specified command exists in the engine's
  888. * command parser tables, this function fills in default_desc based on the
  889. * engine's default length encoding and returns default_desc.
  890. */
  891. static const struct drm_i915_cmd_descriptor*
  892. find_cmd(struct intel_engine_cs *engine,
  893. u32 cmd_header,
  894. const struct drm_i915_cmd_descriptor *desc,
  895. struct drm_i915_cmd_descriptor *default_desc)
  896. {
  897. u32 mask;
  898. if (((cmd_header ^ desc->cmd.value) & desc->cmd.mask) == 0)
  899. return desc;
  900. desc = find_cmd_in_table(engine, cmd_header);
  901. if (desc)
  902. return desc;
  903. mask = engine->get_cmd_length_mask(cmd_header);
  904. if (!mask)
  905. return NULL;
  906. default_desc->cmd.value = cmd_header;
  907. default_desc->cmd.mask = ~0u << MIN_OPCODE_SHIFT;
  908. default_desc->length.mask = mask;
  909. default_desc->flags = CMD_DESC_SKIP;
  910. return default_desc;
  911. }
  912. static const struct drm_i915_reg_descriptor *
  913. __find_reg(const struct drm_i915_reg_descriptor *table, int count, u32 addr)
  914. {
  915. int start = 0, end = count;
  916. while (start < end) {
  917. int mid = start + (end - start) / 2;
  918. int ret = addr - i915_mmio_reg_offset(table[mid].addr);
  919. if (ret < 0)
  920. end = mid;
  921. else if (ret > 0)
  922. start = mid + 1;
  923. else
  924. return &table[mid];
  925. }
  926. return NULL;
  927. }
  928. static const struct drm_i915_reg_descriptor *
  929. find_reg(const struct intel_engine_cs *engine, bool is_master, u32 addr)
  930. {
  931. const struct drm_i915_reg_table *table = engine->reg_tables;
  932. int count = engine->reg_table_count;
  933. do {
  934. if (!table->master || is_master) {
  935. const struct drm_i915_reg_descriptor *reg;
  936. reg = __find_reg(table->regs, table->num_regs, addr);
  937. if (reg != NULL)
  938. return reg;
  939. }
  940. } while (table++, --count);
  941. return NULL;
  942. }
  943. /* Returns a vmap'd pointer to dst_obj, which the caller must unmap */
  944. static u32 *copy_batch(struct drm_i915_gem_object *dst_obj,
  945. struct drm_i915_gem_object *src_obj,
  946. u32 batch_start_offset,
  947. u32 batch_len,
  948. bool *needs_clflush_after)
  949. {
  950. unsigned int src_needs_clflush;
  951. unsigned int dst_needs_clflush;
  952. void *dst, *src;
  953. int ret;
  954. ret = i915_gem_obj_prepare_shmem_read(src_obj, &src_needs_clflush);
  955. if (ret)
  956. return ERR_PTR(ret);
  957. ret = i915_gem_obj_prepare_shmem_write(dst_obj, &dst_needs_clflush);
  958. if (ret) {
  959. dst = ERR_PTR(ret);
  960. goto unpin_src;
  961. }
  962. dst = i915_gem_object_pin_map(dst_obj, I915_MAP_FORCE_WB);
  963. if (IS_ERR(dst))
  964. goto unpin_dst;
  965. src = ERR_PTR(-ENODEV);
  966. if (src_needs_clflush &&
  967. i915_can_memcpy_from_wc(NULL, batch_start_offset, 0)) {
  968. src = i915_gem_object_pin_map(src_obj, I915_MAP_WC);
  969. if (!IS_ERR(src)) {
  970. i915_memcpy_from_wc(dst,
  971. src + batch_start_offset,
  972. ALIGN(batch_len, 16));
  973. i915_gem_object_unpin_map(src_obj);
  974. }
  975. }
  976. if (IS_ERR(src)) {
  977. void *ptr;
  978. int offset, n;
  979. offset = offset_in_page(batch_start_offset);
  980. /* We can avoid clflushing partial cachelines before the write
  981. * if we only every write full cache-lines. Since we know that
  982. * both the source and destination are in multiples of
  983. * PAGE_SIZE, we can simply round up to the next cacheline.
  984. * We don't care about copying too much here as we only
  985. * validate up to the end of the batch.
  986. */
  987. if (dst_needs_clflush & CLFLUSH_BEFORE)
  988. batch_len = roundup(batch_len,
  989. boot_cpu_data.x86_clflush_size);
  990. ptr = dst;
  991. for (n = batch_start_offset >> PAGE_SHIFT; batch_len; n++) {
  992. int len = min_t(int, batch_len, PAGE_SIZE - offset);
  993. src = kmap_atomic(i915_gem_object_get_page(src_obj, n));
  994. if (src_needs_clflush)
  995. drm_clflush_virt_range(src + offset, len);
  996. memcpy(ptr, src + offset, len);
  997. kunmap_atomic(src);
  998. ptr += len;
  999. batch_len -= len;
  1000. offset = 0;
  1001. }
  1002. }
  1003. /* dst_obj is returned with vmap pinned */
  1004. *needs_clflush_after = dst_needs_clflush & CLFLUSH_AFTER;
  1005. unpin_dst:
  1006. i915_gem_obj_finish_shmem_access(dst_obj);
  1007. unpin_src:
  1008. i915_gem_obj_finish_shmem_access(src_obj);
  1009. return dst;
  1010. }
  1011. static bool check_cmd(const struct intel_engine_cs *engine,
  1012. const struct drm_i915_cmd_descriptor *desc,
  1013. const u32 *cmd, u32 length,
  1014. const bool is_master)
  1015. {
  1016. if (desc->flags & CMD_DESC_SKIP)
  1017. return true;
  1018. if (desc->flags & CMD_DESC_REJECT) {
  1019. DRM_DEBUG_DRIVER("CMD: Rejected command: 0x%08X\n", *cmd);
  1020. return false;
  1021. }
  1022. if ((desc->flags & CMD_DESC_MASTER) && !is_master) {
  1023. DRM_DEBUG_DRIVER("CMD: Rejected master-only command: 0x%08X\n",
  1024. *cmd);
  1025. return false;
  1026. }
  1027. if (desc->flags & CMD_DESC_REGISTER) {
  1028. /*
  1029. * Get the distance between individual register offset
  1030. * fields if the command can perform more than one
  1031. * access at a time.
  1032. */
  1033. const u32 step = desc->reg.step ? desc->reg.step : length;
  1034. u32 offset;
  1035. for (offset = desc->reg.offset; offset < length;
  1036. offset += step) {
  1037. const u32 reg_addr = cmd[offset] & desc->reg.mask;
  1038. const struct drm_i915_reg_descriptor *reg =
  1039. find_reg(engine, is_master, reg_addr);
  1040. if (!reg) {
  1041. DRM_DEBUG_DRIVER("CMD: Rejected register 0x%08X in command: 0x%08X (%s)\n",
  1042. reg_addr, *cmd, engine->name);
  1043. return false;
  1044. }
  1045. /*
  1046. * Check the value written to the register against the
  1047. * allowed mask/value pair given in the whitelist entry.
  1048. */
  1049. if (reg->mask) {
  1050. if (desc->cmd.value == MI_LOAD_REGISTER_MEM) {
  1051. DRM_DEBUG_DRIVER("CMD: Rejected LRM to masked register 0x%08X\n",
  1052. reg_addr);
  1053. return false;
  1054. }
  1055. if (desc->cmd.value == MI_LOAD_REGISTER_REG) {
  1056. DRM_DEBUG_DRIVER("CMD: Rejected LRR to masked register 0x%08X\n",
  1057. reg_addr);
  1058. return false;
  1059. }
  1060. if (desc->cmd.value == MI_LOAD_REGISTER_IMM(1) &&
  1061. (offset + 2 > length ||
  1062. (cmd[offset + 1] & reg->mask) != reg->value)) {
  1063. DRM_DEBUG_DRIVER("CMD: Rejected LRI to masked register 0x%08X\n",
  1064. reg_addr);
  1065. return false;
  1066. }
  1067. }
  1068. }
  1069. }
  1070. if (desc->flags & CMD_DESC_BITMASK) {
  1071. int i;
  1072. for (i = 0; i < MAX_CMD_DESC_BITMASKS; i++) {
  1073. u32 dword;
  1074. if (desc->bits[i].mask == 0)
  1075. break;
  1076. if (desc->bits[i].condition_mask != 0) {
  1077. u32 offset =
  1078. desc->bits[i].condition_offset;
  1079. u32 condition = cmd[offset] &
  1080. desc->bits[i].condition_mask;
  1081. if (condition == 0)
  1082. continue;
  1083. }
  1084. dword = cmd[desc->bits[i].offset] &
  1085. desc->bits[i].mask;
  1086. if (dword != desc->bits[i].expected) {
  1087. DRM_DEBUG_DRIVER("CMD: Rejected command 0x%08X for bitmask 0x%08X (exp=0x%08X act=0x%08X) (%s)\n",
  1088. *cmd,
  1089. desc->bits[i].mask,
  1090. desc->bits[i].expected,
  1091. dword, engine->name);
  1092. return false;
  1093. }
  1094. }
  1095. }
  1096. return true;
  1097. }
  1098. #define LENGTH_BIAS 2
  1099. /**
  1100. * i915_parse_cmds() - parse a submitted batch buffer for privilege violations
  1101. * @engine: the engine on which the batch is to execute
  1102. * @batch_obj: the batch buffer in question
  1103. * @shadow_batch_obj: copy of the batch buffer in question
  1104. * @batch_start_offset: byte offset in the batch at which execution starts
  1105. * @batch_len: length of the commands in batch_obj
  1106. * @is_master: is the submitting process the drm master?
  1107. *
  1108. * Parses the specified batch buffer looking for privilege violations as
  1109. * described in the overview.
  1110. *
  1111. * Return: non-zero if the parser finds violations or otherwise fails; -EACCES
  1112. * if the batch appears legal but should use hardware parsing
  1113. */
  1114. int intel_engine_cmd_parser(struct intel_engine_cs *engine,
  1115. struct drm_i915_gem_object *batch_obj,
  1116. struct drm_i915_gem_object *shadow_batch_obj,
  1117. u32 batch_start_offset,
  1118. u32 batch_len,
  1119. bool is_master)
  1120. {
  1121. u32 *cmd, *batch_end;
  1122. struct drm_i915_cmd_descriptor default_desc = noop_desc;
  1123. const struct drm_i915_cmd_descriptor *desc = &default_desc;
  1124. bool needs_clflush_after = false;
  1125. int ret = 0;
  1126. cmd = copy_batch(shadow_batch_obj, batch_obj,
  1127. batch_start_offset, batch_len,
  1128. &needs_clflush_after);
  1129. if (IS_ERR(cmd)) {
  1130. DRM_DEBUG_DRIVER("CMD: Failed to copy batch\n");
  1131. return PTR_ERR(cmd);
  1132. }
  1133. /*
  1134. * We use the batch length as size because the shadow object is as
  1135. * large or larger and copy_batch() will write MI_NOPs to the extra
  1136. * space. Parsing should be faster in some cases this way.
  1137. */
  1138. batch_end = cmd + (batch_len / sizeof(*batch_end));
  1139. do {
  1140. u32 length;
  1141. if (*cmd == MI_BATCH_BUFFER_END) {
  1142. if (needs_clflush_after) {
  1143. void *ptr = page_mask_bits(shadow_batch_obj->mm.mapping);
  1144. drm_clflush_virt_range(ptr,
  1145. (void *)(cmd + 1) - ptr);
  1146. }
  1147. break;
  1148. }
  1149. desc = find_cmd(engine, *cmd, desc, &default_desc);
  1150. if (!desc) {
  1151. DRM_DEBUG_DRIVER("CMD: Unrecognized command: 0x%08X\n",
  1152. *cmd);
  1153. ret = -EINVAL;
  1154. break;
  1155. }
  1156. /*
  1157. * If the batch buffer contains a chained batch, return an
  1158. * error that tells the caller to abort and dispatch the
  1159. * workload as a non-secure batch.
  1160. */
  1161. if (desc->cmd.value == MI_BATCH_BUFFER_START) {
  1162. ret = -EACCES;
  1163. break;
  1164. }
  1165. if (desc->flags & CMD_DESC_FIXED)
  1166. length = desc->length.fixed;
  1167. else
  1168. length = ((*cmd & desc->length.mask) + LENGTH_BIAS);
  1169. if ((batch_end - cmd) < length) {
  1170. DRM_DEBUG_DRIVER("CMD: Command length exceeds batch length: 0x%08X length=%u batchlen=%td\n",
  1171. *cmd,
  1172. length,
  1173. batch_end - cmd);
  1174. ret = -EINVAL;
  1175. break;
  1176. }
  1177. if (!check_cmd(engine, desc, cmd, length, is_master)) {
  1178. ret = -EACCES;
  1179. break;
  1180. }
  1181. cmd += length;
  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. break;
  1186. }
  1187. } while (1);
  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. }