i915_cmd_parser.c 36 KB

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