i915_cmd_parser.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064
  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. * implementated 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 } ),
  121. CMD( MI_STORE_REGISTER_MEM(1), SMI, !F, 0xFF, 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, 0xFF, W | B,
  129. .reg = { .offset = 1, .mask = 0x007FFFFC },
  130. .bits = {{
  131. .offset = 0,
  132. .mask = MI_GLOBAL_GTT,
  133. .expected = 0,
  134. }}, ),
  135. CMD( MI_BATCH_BUFFER_START, SMI, !F, 0xFF, S ),
  136. };
  137. static const struct drm_i915_cmd_descriptor render_cmds[] = {
  138. CMD( MI_FLUSH, SMI, F, 1, S ),
  139. CMD( MI_ARB_ON_OFF, SMI, F, 1, R ),
  140. CMD( MI_PREDICATE, SMI, F, 1, S ),
  141. CMD( MI_TOPOLOGY_FILTER, SMI, F, 1, S ),
  142. CMD( MI_DISPLAY_FLIP, SMI, !F, 0xFF, R ),
  143. CMD( MI_SET_CONTEXT, SMI, !F, 0xFF, R ),
  144. CMD( MI_URB_CLEAR, SMI, !F, 0xFF, S ),
  145. CMD( MI_STORE_DWORD_IMM, SMI, !F, 0x3F, B,
  146. .bits = {{
  147. .offset = 0,
  148. .mask = MI_GLOBAL_GTT,
  149. .expected = 0,
  150. }}, ),
  151. CMD( MI_UPDATE_GTT, SMI, !F, 0xFF, R ),
  152. CMD( MI_CLFLUSH, SMI, !F, 0x3FF, B,
  153. .bits = {{
  154. .offset = 0,
  155. .mask = MI_GLOBAL_GTT,
  156. .expected = 0,
  157. }}, ),
  158. CMD( MI_REPORT_PERF_COUNT, SMI, !F, 0x3F, B,
  159. .bits = {{
  160. .offset = 1,
  161. .mask = MI_REPORT_PERF_COUNT_GGTT,
  162. .expected = 0,
  163. }}, ),
  164. CMD( MI_CONDITIONAL_BATCH_BUFFER_END, SMI, !F, 0xFF, B,
  165. .bits = {{
  166. .offset = 0,
  167. .mask = MI_GLOBAL_GTT,
  168. .expected = 0,
  169. }}, ),
  170. CMD( GFX_OP_3DSTATE_VF_STATISTICS, S3D, F, 1, S ),
  171. CMD( PIPELINE_SELECT, S3D, F, 1, S ),
  172. CMD( MEDIA_VFE_STATE, S3D, !F, 0xFFFF, B,
  173. .bits = {{
  174. .offset = 2,
  175. .mask = MEDIA_VFE_STATE_MMIO_ACCESS_MASK,
  176. .expected = 0,
  177. }}, ),
  178. CMD( GPGPU_OBJECT, S3D, !F, 0xFF, S ),
  179. CMD( GPGPU_WALKER, S3D, !F, 0xFF, S ),
  180. CMD( GFX_OP_3DSTATE_SO_DECL_LIST, S3D, !F, 0x1FF, S ),
  181. CMD( GFX_OP_PIPE_CONTROL(5), S3D, !F, 0xFF, B,
  182. .bits = {{
  183. .offset = 1,
  184. .mask = (PIPE_CONTROL_MMIO_WRITE | PIPE_CONTROL_NOTIFY),
  185. .expected = 0,
  186. },
  187. {
  188. .offset = 1,
  189. .mask = (PIPE_CONTROL_GLOBAL_GTT_IVB |
  190. PIPE_CONTROL_STORE_DATA_INDEX),
  191. .expected = 0,
  192. .condition_offset = 1,
  193. .condition_mask = PIPE_CONTROL_POST_SYNC_OP_MASK,
  194. }}, ),
  195. };
  196. static const struct drm_i915_cmd_descriptor hsw_render_cmds[] = {
  197. CMD( MI_SET_PREDICATE, SMI, F, 1, S ),
  198. CMD( MI_RS_CONTROL, SMI, F, 1, S ),
  199. CMD( MI_URB_ATOMIC_ALLOC, SMI, F, 1, S ),
  200. CMD( MI_RS_CONTEXT, SMI, F, 1, S ),
  201. CMD( MI_LOAD_SCAN_LINES_INCL, SMI, !F, 0x3F, M ),
  202. CMD( MI_LOAD_SCAN_LINES_EXCL, SMI, !F, 0x3F, R ),
  203. CMD( MI_LOAD_REGISTER_REG, SMI, !F, 0xFF, R ),
  204. CMD( MI_RS_STORE_DATA_IMM, SMI, !F, 0xFF, S ),
  205. CMD( MI_LOAD_URB_MEM, SMI, !F, 0xFF, S ),
  206. CMD( MI_STORE_URB_MEM, SMI, !F, 0xFF, S ),
  207. CMD( GFX_OP_3DSTATE_DX9_CONSTANTF_VS, S3D, !F, 0x7FF, S ),
  208. CMD( GFX_OP_3DSTATE_DX9_CONSTANTF_PS, S3D, !F, 0x7FF, S ),
  209. CMD( GFX_OP_3DSTATE_BINDING_TABLE_EDIT_VS, S3D, !F, 0x1FF, S ),
  210. CMD( GFX_OP_3DSTATE_BINDING_TABLE_EDIT_GS, S3D, !F, 0x1FF, S ),
  211. CMD( GFX_OP_3DSTATE_BINDING_TABLE_EDIT_HS, S3D, !F, 0x1FF, S ),
  212. CMD( GFX_OP_3DSTATE_BINDING_TABLE_EDIT_DS, S3D, !F, 0x1FF, S ),
  213. CMD( GFX_OP_3DSTATE_BINDING_TABLE_EDIT_PS, S3D, !F, 0x1FF, S ),
  214. };
  215. static const struct drm_i915_cmd_descriptor video_cmds[] = {
  216. CMD( MI_ARB_ON_OFF, SMI, F, 1, R ),
  217. CMD( MI_STORE_DWORD_IMM, SMI, !F, 0xFF, B,
  218. .bits = {{
  219. .offset = 0,
  220. .mask = MI_GLOBAL_GTT,
  221. .expected = 0,
  222. }}, ),
  223. CMD( MI_UPDATE_GTT, SMI, !F, 0x3F, R ),
  224. CMD( MI_FLUSH_DW, SMI, !F, 0x3F, B,
  225. .bits = {{
  226. .offset = 0,
  227. .mask = MI_FLUSH_DW_NOTIFY,
  228. .expected = 0,
  229. },
  230. {
  231. .offset = 1,
  232. .mask = MI_FLUSH_DW_USE_GTT,
  233. .expected = 0,
  234. .condition_offset = 0,
  235. .condition_mask = MI_FLUSH_DW_OP_MASK,
  236. },
  237. {
  238. .offset = 0,
  239. .mask = MI_FLUSH_DW_STORE_INDEX,
  240. .expected = 0,
  241. .condition_offset = 0,
  242. .condition_mask = MI_FLUSH_DW_OP_MASK,
  243. }}, ),
  244. CMD( MI_CONDITIONAL_BATCH_BUFFER_END, SMI, !F, 0xFF, B,
  245. .bits = {{
  246. .offset = 0,
  247. .mask = MI_GLOBAL_GTT,
  248. .expected = 0,
  249. }}, ),
  250. /*
  251. * MFX_WAIT doesn't fit the way we handle length for most commands.
  252. * It has a length field but it uses a non-standard length bias.
  253. * It is always 1 dword though, so just treat it as fixed length.
  254. */
  255. CMD( MFX_WAIT, SMFX, F, 1, S ),
  256. };
  257. static const struct drm_i915_cmd_descriptor vecs_cmds[] = {
  258. CMD( MI_ARB_ON_OFF, SMI, F, 1, R ),
  259. CMD( MI_STORE_DWORD_IMM, SMI, !F, 0xFF, B,
  260. .bits = {{
  261. .offset = 0,
  262. .mask = MI_GLOBAL_GTT,
  263. .expected = 0,
  264. }}, ),
  265. CMD( MI_UPDATE_GTT, SMI, !F, 0x3F, R ),
  266. CMD( MI_FLUSH_DW, SMI, !F, 0x3F, B,
  267. .bits = {{
  268. .offset = 0,
  269. .mask = MI_FLUSH_DW_NOTIFY,
  270. .expected = 0,
  271. },
  272. {
  273. .offset = 1,
  274. .mask = MI_FLUSH_DW_USE_GTT,
  275. .expected = 0,
  276. .condition_offset = 0,
  277. .condition_mask = MI_FLUSH_DW_OP_MASK,
  278. },
  279. {
  280. .offset = 0,
  281. .mask = MI_FLUSH_DW_STORE_INDEX,
  282. .expected = 0,
  283. .condition_offset = 0,
  284. .condition_mask = MI_FLUSH_DW_OP_MASK,
  285. }}, ),
  286. CMD( MI_CONDITIONAL_BATCH_BUFFER_END, SMI, !F, 0xFF, B,
  287. .bits = {{
  288. .offset = 0,
  289. .mask = MI_GLOBAL_GTT,
  290. .expected = 0,
  291. }}, ),
  292. };
  293. static const struct drm_i915_cmd_descriptor blt_cmds[] = {
  294. CMD( MI_DISPLAY_FLIP, SMI, !F, 0xFF, R ),
  295. CMD( MI_STORE_DWORD_IMM, SMI, !F, 0x3FF, B,
  296. .bits = {{
  297. .offset = 0,
  298. .mask = MI_GLOBAL_GTT,
  299. .expected = 0,
  300. }}, ),
  301. CMD( MI_UPDATE_GTT, SMI, !F, 0x3F, R ),
  302. CMD( MI_FLUSH_DW, SMI, !F, 0x3F, B,
  303. .bits = {{
  304. .offset = 0,
  305. .mask = MI_FLUSH_DW_NOTIFY,
  306. .expected = 0,
  307. },
  308. {
  309. .offset = 1,
  310. .mask = MI_FLUSH_DW_USE_GTT,
  311. .expected = 0,
  312. .condition_offset = 0,
  313. .condition_mask = MI_FLUSH_DW_OP_MASK,
  314. },
  315. {
  316. .offset = 0,
  317. .mask = MI_FLUSH_DW_STORE_INDEX,
  318. .expected = 0,
  319. .condition_offset = 0,
  320. .condition_mask = MI_FLUSH_DW_OP_MASK,
  321. }}, ),
  322. CMD( COLOR_BLT, S2D, !F, 0x3F, S ),
  323. CMD( SRC_COPY_BLT, S2D, !F, 0x3F, S ),
  324. };
  325. static const struct drm_i915_cmd_descriptor hsw_blt_cmds[] = {
  326. CMD( MI_LOAD_SCAN_LINES_INCL, SMI, !F, 0x3F, M ),
  327. CMD( MI_LOAD_SCAN_LINES_EXCL, SMI, !F, 0x3F, R ),
  328. };
  329. #undef CMD
  330. #undef SMI
  331. #undef S3D
  332. #undef S2D
  333. #undef SMFX
  334. #undef F
  335. #undef S
  336. #undef R
  337. #undef W
  338. #undef B
  339. #undef M
  340. static const struct drm_i915_cmd_table gen7_render_cmds[] = {
  341. { common_cmds, ARRAY_SIZE(common_cmds) },
  342. { render_cmds, ARRAY_SIZE(render_cmds) },
  343. };
  344. static const struct drm_i915_cmd_table hsw_render_ring_cmds[] = {
  345. { common_cmds, ARRAY_SIZE(common_cmds) },
  346. { render_cmds, ARRAY_SIZE(render_cmds) },
  347. { hsw_render_cmds, ARRAY_SIZE(hsw_render_cmds) },
  348. };
  349. static const struct drm_i915_cmd_table gen7_video_cmds[] = {
  350. { common_cmds, ARRAY_SIZE(common_cmds) },
  351. { video_cmds, ARRAY_SIZE(video_cmds) },
  352. };
  353. static const struct drm_i915_cmd_table hsw_vebox_cmds[] = {
  354. { common_cmds, ARRAY_SIZE(common_cmds) },
  355. { vecs_cmds, ARRAY_SIZE(vecs_cmds) },
  356. };
  357. static const struct drm_i915_cmd_table gen7_blt_cmds[] = {
  358. { common_cmds, ARRAY_SIZE(common_cmds) },
  359. { blt_cmds, ARRAY_SIZE(blt_cmds) },
  360. };
  361. static const struct drm_i915_cmd_table hsw_blt_ring_cmds[] = {
  362. { common_cmds, ARRAY_SIZE(common_cmds) },
  363. { blt_cmds, ARRAY_SIZE(blt_cmds) },
  364. { hsw_blt_cmds, ARRAY_SIZE(hsw_blt_cmds) },
  365. };
  366. /*
  367. * Register whitelists, sorted by increasing register offset.
  368. *
  369. * Some registers that userspace accesses are 64 bits. The register
  370. * access commands only allow 32-bit accesses. Hence, we have to include
  371. * entries for both halves of the 64-bit registers.
  372. */
  373. /* Convenience macro for adding 64-bit registers */
  374. #define REG64(addr) (addr), (addr + sizeof(u32))
  375. static const u32 gen7_render_regs[] = {
  376. REG64(HS_INVOCATION_COUNT),
  377. REG64(DS_INVOCATION_COUNT),
  378. REG64(IA_VERTICES_COUNT),
  379. REG64(IA_PRIMITIVES_COUNT),
  380. REG64(VS_INVOCATION_COUNT),
  381. REG64(GS_INVOCATION_COUNT),
  382. REG64(GS_PRIMITIVES_COUNT),
  383. REG64(CL_INVOCATION_COUNT),
  384. REG64(CL_PRIMITIVES_COUNT),
  385. REG64(PS_INVOCATION_COUNT),
  386. REG64(PS_DEPTH_COUNT),
  387. OACONTROL, /* Only allowed for LRI and SRM. See below. */
  388. GEN7_3DPRIM_END_OFFSET,
  389. GEN7_3DPRIM_START_VERTEX,
  390. GEN7_3DPRIM_VERTEX_COUNT,
  391. GEN7_3DPRIM_INSTANCE_COUNT,
  392. GEN7_3DPRIM_START_INSTANCE,
  393. GEN7_3DPRIM_BASE_VERTEX,
  394. REG64(GEN7_SO_NUM_PRIMS_WRITTEN(0)),
  395. REG64(GEN7_SO_NUM_PRIMS_WRITTEN(1)),
  396. REG64(GEN7_SO_NUM_PRIMS_WRITTEN(2)),
  397. REG64(GEN7_SO_NUM_PRIMS_WRITTEN(3)),
  398. REG64(GEN7_SO_PRIM_STORAGE_NEEDED(0)),
  399. REG64(GEN7_SO_PRIM_STORAGE_NEEDED(1)),
  400. REG64(GEN7_SO_PRIM_STORAGE_NEEDED(2)),
  401. REG64(GEN7_SO_PRIM_STORAGE_NEEDED(3)),
  402. GEN7_SO_WRITE_OFFSET(0),
  403. GEN7_SO_WRITE_OFFSET(1),
  404. GEN7_SO_WRITE_OFFSET(2),
  405. GEN7_SO_WRITE_OFFSET(3),
  406. GEN7_L3SQCREG1,
  407. GEN7_L3CNTLREG2,
  408. GEN7_L3CNTLREG3,
  409. };
  410. static const u32 gen7_blt_regs[] = {
  411. BCS_SWCTRL,
  412. };
  413. static const u32 ivb_master_regs[] = {
  414. FORCEWAKE_MT,
  415. DERRMR,
  416. GEN7_PIPE_DE_LOAD_SL(PIPE_A),
  417. GEN7_PIPE_DE_LOAD_SL(PIPE_B),
  418. GEN7_PIPE_DE_LOAD_SL(PIPE_C),
  419. };
  420. static const u32 hsw_master_regs[] = {
  421. FORCEWAKE_MT,
  422. DERRMR,
  423. };
  424. #undef REG64
  425. static u32 gen7_render_get_cmd_length_mask(u32 cmd_header)
  426. {
  427. u32 client = (cmd_header & INSTR_CLIENT_MASK) >> INSTR_CLIENT_SHIFT;
  428. u32 subclient =
  429. (cmd_header & INSTR_SUBCLIENT_MASK) >> INSTR_SUBCLIENT_SHIFT;
  430. if (client == INSTR_MI_CLIENT)
  431. return 0x3F;
  432. else if (client == INSTR_RC_CLIENT) {
  433. if (subclient == INSTR_MEDIA_SUBCLIENT)
  434. return 0xFFFF;
  435. else
  436. return 0xFF;
  437. }
  438. DRM_DEBUG_DRIVER("CMD: Abnormal rcs cmd length! 0x%08X\n", cmd_header);
  439. return 0;
  440. }
  441. static u32 gen7_bsd_get_cmd_length_mask(u32 cmd_header)
  442. {
  443. u32 client = (cmd_header & INSTR_CLIENT_MASK) >> INSTR_CLIENT_SHIFT;
  444. u32 subclient =
  445. (cmd_header & INSTR_SUBCLIENT_MASK) >> INSTR_SUBCLIENT_SHIFT;
  446. if (client == INSTR_MI_CLIENT)
  447. return 0x3F;
  448. else if (client == INSTR_RC_CLIENT) {
  449. if (subclient == INSTR_MEDIA_SUBCLIENT)
  450. return 0xFFF;
  451. else
  452. return 0xFF;
  453. }
  454. DRM_DEBUG_DRIVER("CMD: Abnormal bsd cmd length! 0x%08X\n", cmd_header);
  455. return 0;
  456. }
  457. static u32 gen7_blt_get_cmd_length_mask(u32 cmd_header)
  458. {
  459. u32 client = (cmd_header & INSTR_CLIENT_MASK) >> INSTR_CLIENT_SHIFT;
  460. if (client == INSTR_MI_CLIENT)
  461. return 0x3F;
  462. else if (client == INSTR_BC_CLIENT)
  463. return 0xFF;
  464. DRM_DEBUG_DRIVER("CMD: Abnormal blt cmd length! 0x%08X\n", cmd_header);
  465. return 0;
  466. }
  467. static bool validate_cmds_sorted(struct intel_engine_cs *ring,
  468. const struct drm_i915_cmd_table *cmd_tables,
  469. int cmd_table_count)
  470. {
  471. int i;
  472. bool ret = true;
  473. if (!cmd_tables || cmd_table_count == 0)
  474. return true;
  475. for (i = 0; i < cmd_table_count; i++) {
  476. const struct drm_i915_cmd_table *table = &cmd_tables[i];
  477. u32 previous = 0;
  478. int j;
  479. for (j = 0; j < table->count; j++) {
  480. const struct drm_i915_cmd_descriptor *desc =
  481. &table->table[i];
  482. u32 curr = desc->cmd.value & desc->cmd.mask;
  483. if (curr < previous) {
  484. DRM_ERROR("CMD: table not sorted ring=%d table=%d entry=%d cmd=0x%08X prev=0x%08X\n",
  485. ring->id, i, j, curr, previous);
  486. ret = false;
  487. }
  488. previous = curr;
  489. }
  490. }
  491. return ret;
  492. }
  493. static bool check_sorted(int ring_id, const u32 *reg_table, int reg_count)
  494. {
  495. int i;
  496. u32 previous = 0;
  497. bool ret = true;
  498. for (i = 0; i < reg_count; i++) {
  499. u32 curr = reg_table[i];
  500. if (curr < previous) {
  501. DRM_ERROR("CMD: table not sorted ring=%d entry=%d reg=0x%08X prev=0x%08X\n",
  502. ring_id, i, curr, previous);
  503. ret = false;
  504. }
  505. previous = curr;
  506. }
  507. return ret;
  508. }
  509. static bool validate_regs_sorted(struct intel_engine_cs *ring)
  510. {
  511. return check_sorted(ring->id, ring->reg_table, ring->reg_count) &&
  512. check_sorted(ring->id, ring->master_reg_table,
  513. ring->master_reg_count);
  514. }
  515. struct cmd_node {
  516. const struct drm_i915_cmd_descriptor *desc;
  517. struct hlist_node node;
  518. };
  519. /*
  520. * Different command ranges have different numbers of bits for the opcode. For
  521. * example, MI commands use bits 31:23 while 3D commands use bits 31:16. The
  522. * problem is that, for example, MI commands use bits 22:16 for other fields
  523. * such as GGTT vs PPGTT bits. If we include those bits in the mask then when
  524. * we mask a command from a batch it could hash to the wrong bucket due to
  525. * non-opcode bits being set. But if we don't include those bits, some 3D
  526. * commands may hash to the same bucket due to not including opcode bits that
  527. * make the command unique. For now, we will risk hashing to the same bucket.
  528. *
  529. * If we attempt to generate a perfect hash, we should be able to look at bits
  530. * 31:29 of a command from a batch buffer and use the full mask for that
  531. * client. The existing INSTR_CLIENT_MASK/SHIFT defines can be used for this.
  532. */
  533. #define CMD_HASH_MASK STD_MI_OPCODE_MASK
  534. static int init_hash_table(struct intel_engine_cs *ring,
  535. const struct drm_i915_cmd_table *cmd_tables,
  536. int cmd_table_count)
  537. {
  538. int i, j;
  539. hash_init(ring->cmd_hash);
  540. for (i = 0; i < cmd_table_count; i++) {
  541. const struct drm_i915_cmd_table *table = &cmd_tables[i];
  542. for (j = 0; j < table->count; j++) {
  543. const struct drm_i915_cmd_descriptor *desc =
  544. &table->table[j];
  545. struct cmd_node *desc_node =
  546. kmalloc(sizeof(*desc_node), GFP_KERNEL);
  547. if (!desc_node)
  548. return -ENOMEM;
  549. desc_node->desc = desc;
  550. hash_add(ring->cmd_hash, &desc_node->node,
  551. desc->cmd.value & CMD_HASH_MASK);
  552. }
  553. }
  554. return 0;
  555. }
  556. static void fini_hash_table(struct intel_engine_cs *ring)
  557. {
  558. struct hlist_node *tmp;
  559. struct cmd_node *desc_node;
  560. int i;
  561. hash_for_each_safe(ring->cmd_hash, i, tmp, desc_node, node) {
  562. hash_del(&desc_node->node);
  563. kfree(desc_node);
  564. }
  565. }
  566. /**
  567. * i915_cmd_parser_init_ring() - set cmd parser related fields for a ringbuffer
  568. * @ring: the ringbuffer to initialize
  569. *
  570. * Optionally initializes fields related to batch buffer command parsing in the
  571. * struct intel_engine_cs based on whether the platform requires software
  572. * command parsing.
  573. *
  574. * Return: non-zero if initialization fails
  575. */
  576. int i915_cmd_parser_init_ring(struct intel_engine_cs *ring)
  577. {
  578. const struct drm_i915_cmd_table *cmd_tables;
  579. int cmd_table_count;
  580. int ret;
  581. if (!IS_GEN7(ring->dev))
  582. return 0;
  583. switch (ring->id) {
  584. case RCS:
  585. if (IS_HASWELL(ring->dev)) {
  586. cmd_tables = hsw_render_ring_cmds;
  587. cmd_table_count =
  588. ARRAY_SIZE(hsw_render_ring_cmds);
  589. } else {
  590. cmd_tables = gen7_render_cmds;
  591. cmd_table_count = ARRAY_SIZE(gen7_render_cmds);
  592. }
  593. ring->reg_table = gen7_render_regs;
  594. ring->reg_count = ARRAY_SIZE(gen7_render_regs);
  595. if (IS_HASWELL(ring->dev)) {
  596. ring->master_reg_table = hsw_master_regs;
  597. ring->master_reg_count = ARRAY_SIZE(hsw_master_regs);
  598. } else {
  599. ring->master_reg_table = ivb_master_regs;
  600. ring->master_reg_count = ARRAY_SIZE(ivb_master_regs);
  601. }
  602. ring->get_cmd_length_mask = gen7_render_get_cmd_length_mask;
  603. break;
  604. case VCS:
  605. cmd_tables = gen7_video_cmds;
  606. cmd_table_count = ARRAY_SIZE(gen7_video_cmds);
  607. ring->get_cmd_length_mask = gen7_bsd_get_cmd_length_mask;
  608. break;
  609. case BCS:
  610. if (IS_HASWELL(ring->dev)) {
  611. cmd_tables = hsw_blt_ring_cmds;
  612. cmd_table_count = ARRAY_SIZE(hsw_blt_ring_cmds);
  613. } else {
  614. cmd_tables = gen7_blt_cmds;
  615. cmd_table_count = ARRAY_SIZE(gen7_blt_cmds);
  616. }
  617. ring->reg_table = gen7_blt_regs;
  618. ring->reg_count = ARRAY_SIZE(gen7_blt_regs);
  619. if (IS_HASWELL(ring->dev)) {
  620. ring->master_reg_table = hsw_master_regs;
  621. ring->master_reg_count = ARRAY_SIZE(hsw_master_regs);
  622. } else {
  623. ring->master_reg_table = ivb_master_regs;
  624. ring->master_reg_count = ARRAY_SIZE(ivb_master_regs);
  625. }
  626. ring->get_cmd_length_mask = gen7_blt_get_cmd_length_mask;
  627. break;
  628. case VECS:
  629. cmd_tables = hsw_vebox_cmds;
  630. cmd_table_count = ARRAY_SIZE(hsw_vebox_cmds);
  631. /* VECS can use the same length_mask function as VCS */
  632. ring->get_cmd_length_mask = gen7_bsd_get_cmd_length_mask;
  633. break;
  634. default:
  635. DRM_ERROR("CMD: cmd_parser_init with unknown ring: %d\n",
  636. ring->id);
  637. BUG();
  638. }
  639. BUG_ON(!validate_cmds_sorted(ring, cmd_tables, cmd_table_count));
  640. BUG_ON(!validate_regs_sorted(ring));
  641. ret = init_hash_table(ring, cmd_tables, cmd_table_count);
  642. if (ret) {
  643. DRM_ERROR("CMD: cmd_parser_init failed!\n");
  644. fini_hash_table(ring);
  645. return ret;
  646. }
  647. ring->needs_cmd_parser = true;
  648. return 0;
  649. }
  650. /**
  651. * i915_cmd_parser_fini_ring() - clean up cmd parser related fields
  652. * @ring: the ringbuffer to clean up
  653. *
  654. * Releases any resources related to command parsing that may have been
  655. * initialized for the specified ring.
  656. */
  657. void i915_cmd_parser_fini_ring(struct intel_engine_cs *ring)
  658. {
  659. if (!ring->needs_cmd_parser)
  660. return;
  661. fini_hash_table(ring);
  662. }
  663. static const struct drm_i915_cmd_descriptor*
  664. find_cmd_in_table(struct intel_engine_cs *ring,
  665. u32 cmd_header)
  666. {
  667. struct cmd_node *desc_node;
  668. hash_for_each_possible(ring->cmd_hash, desc_node, node,
  669. cmd_header & CMD_HASH_MASK) {
  670. const struct drm_i915_cmd_descriptor *desc = desc_node->desc;
  671. u32 masked_cmd = desc->cmd.mask & cmd_header;
  672. u32 masked_value = desc->cmd.value & desc->cmd.mask;
  673. if (masked_cmd == masked_value)
  674. return desc;
  675. }
  676. return NULL;
  677. }
  678. /*
  679. * Returns a pointer to a descriptor for the command specified by cmd_header.
  680. *
  681. * The caller must supply space for a default descriptor via the default_desc
  682. * parameter. If no descriptor for the specified command exists in the ring's
  683. * command parser tables, this function fills in default_desc based on the
  684. * ring's default length encoding and returns default_desc.
  685. */
  686. static const struct drm_i915_cmd_descriptor*
  687. find_cmd(struct intel_engine_cs *ring,
  688. u32 cmd_header,
  689. struct drm_i915_cmd_descriptor *default_desc)
  690. {
  691. const struct drm_i915_cmd_descriptor *desc;
  692. u32 mask;
  693. desc = find_cmd_in_table(ring, cmd_header);
  694. if (desc)
  695. return desc;
  696. mask = ring->get_cmd_length_mask(cmd_header);
  697. if (!mask)
  698. return NULL;
  699. BUG_ON(!default_desc);
  700. default_desc->flags = CMD_DESC_SKIP;
  701. default_desc->length.mask = mask;
  702. return default_desc;
  703. }
  704. static bool valid_reg(const u32 *table, int count, u32 addr)
  705. {
  706. if (table && count != 0) {
  707. int i;
  708. for (i = 0; i < count; i++) {
  709. if (table[i] == addr)
  710. return true;
  711. }
  712. }
  713. return false;
  714. }
  715. static u32 *vmap_batch(struct drm_i915_gem_object *obj)
  716. {
  717. int i;
  718. void *addr = NULL;
  719. struct sg_page_iter sg_iter;
  720. struct page **pages;
  721. pages = drm_malloc_ab(obj->base.size >> PAGE_SHIFT, sizeof(*pages));
  722. if (pages == NULL) {
  723. DRM_DEBUG_DRIVER("Failed to get space for pages\n");
  724. goto finish;
  725. }
  726. i = 0;
  727. for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents, 0) {
  728. pages[i] = sg_page_iter_page(&sg_iter);
  729. i++;
  730. }
  731. addr = vmap(pages, i, 0, PAGE_KERNEL);
  732. if (addr == NULL) {
  733. DRM_DEBUG_DRIVER("Failed to vmap pages\n");
  734. goto finish;
  735. }
  736. finish:
  737. if (pages)
  738. drm_free_large(pages);
  739. return (u32*)addr;
  740. }
  741. /**
  742. * i915_needs_cmd_parser() - should a given ring use software command parsing?
  743. * @ring: the ring in question
  744. *
  745. * Only certain platforms require software batch buffer command parsing, and
  746. * only when enabled via module paramter.
  747. *
  748. * Return: true if the ring requires software command parsing
  749. */
  750. bool i915_needs_cmd_parser(struct intel_engine_cs *ring)
  751. {
  752. struct drm_i915_private *dev_priv = ring->dev->dev_private;
  753. if (!ring->needs_cmd_parser)
  754. return false;
  755. /*
  756. * XXX: VLV is Gen7 and therefore has cmd_tables, but has PPGTT
  757. * disabled. That will cause all of the parser's PPGTT checks to
  758. * fail. For now, disable parsing when PPGTT is off.
  759. */
  760. if (!dev_priv->mm.aliasing_ppgtt)
  761. return false;
  762. return (i915.enable_cmd_parser == 1);
  763. }
  764. static bool check_cmd(const struct intel_engine_cs *ring,
  765. const struct drm_i915_cmd_descriptor *desc,
  766. const u32 *cmd,
  767. const bool is_master,
  768. bool *oacontrol_set)
  769. {
  770. if (desc->flags & CMD_DESC_REJECT) {
  771. DRM_DEBUG_DRIVER("CMD: Rejected command: 0x%08X\n", *cmd);
  772. return false;
  773. }
  774. if ((desc->flags & CMD_DESC_MASTER) && !is_master) {
  775. DRM_DEBUG_DRIVER("CMD: Rejected master-only command: 0x%08X\n",
  776. *cmd);
  777. return false;
  778. }
  779. if (desc->flags & CMD_DESC_REGISTER) {
  780. u32 reg_addr = cmd[desc->reg.offset] & desc->reg.mask;
  781. /*
  782. * OACONTROL requires some special handling for writes. We
  783. * want to make sure that any batch which enables OA also
  784. * disables it before the end of the batch. The goal is to
  785. * prevent one process from snooping on the perf data from
  786. * another process. To do that, we need to check the value
  787. * that will be written to the register. Hence, limit
  788. * OACONTROL writes to only MI_LOAD_REGISTER_IMM commands.
  789. */
  790. if (reg_addr == OACONTROL) {
  791. if (desc->cmd.value == MI_LOAD_REGISTER_MEM)
  792. return false;
  793. if (desc->cmd.value == MI_LOAD_REGISTER_IMM(1))
  794. *oacontrol_set = (cmd[2] != 0);
  795. }
  796. if (!valid_reg(ring->reg_table,
  797. ring->reg_count, reg_addr)) {
  798. if (!is_master ||
  799. !valid_reg(ring->master_reg_table,
  800. ring->master_reg_count,
  801. reg_addr)) {
  802. DRM_DEBUG_DRIVER("CMD: Rejected register 0x%08X in command: 0x%08X (ring=%d)\n",
  803. reg_addr,
  804. *cmd,
  805. ring->id);
  806. return false;
  807. }
  808. }
  809. }
  810. if (desc->flags & CMD_DESC_BITMASK) {
  811. int i;
  812. for (i = 0; i < MAX_CMD_DESC_BITMASKS; i++) {
  813. u32 dword;
  814. if (desc->bits[i].mask == 0)
  815. break;
  816. if (desc->bits[i].condition_mask != 0) {
  817. u32 offset =
  818. desc->bits[i].condition_offset;
  819. u32 condition = cmd[offset] &
  820. desc->bits[i].condition_mask;
  821. if (condition == 0)
  822. continue;
  823. }
  824. dword = cmd[desc->bits[i].offset] &
  825. desc->bits[i].mask;
  826. if (dword != desc->bits[i].expected) {
  827. DRM_DEBUG_DRIVER("CMD: Rejected command 0x%08X for bitmask 0x%08X (exp=0x%08X act=0x%08X) (ring=%d)\n",
  828. *cmd,
  829. desc->bits[i].mask,
  830. desc->bits[i].expected,
  831. dword, ring->id);
  832. return false;
  833. }
  834. }
  835. }
  836. return true;
  837. }
  838. #define LENGTH_BIAS 2
  839. /**
  840. * i915_parse_cmds() - parse a submitted batch buffer for privilege violations
  841. * @ring: the ring on which the batch is to execute
  842. * @batch_obj: the batch buffer in question
  843. * @batch_start_offset: byte offset in the batch at which execution starts
  844. * @is_master: is the submitting process the drm master?
  845. *
  846. * Parses the specified batch buffer looking for privilege violations as
  847. * described in the overview.
  848. *
  849. * Return: non-zero if the parser finds violations or otherwise fails
  850. */
  851. int i915_parse_cmds(struct intel_engine_cs *ring,
  852. struct drm_i915_gem_object *batch_obj,
  853. u32 batch_start_offset,
  854. bool is_master)
  855. {
  856. int ret = 0;
  857. u32 *cmd, *batch_base, *batch_end;
  858. struct drm_i915_cmd_descriptor default_desc = { 0 };
  859. int needs_clflush = 0;
  860. bool oacontrol_set = false; /* OACONTROL tracking. See check_cmd() */
  861. ret = i915_gem_obj_prepare_shmem_read(batch_obj, &needs_clflush);
  862. if (ret) {
  863. DRM_DEBUG_DRIVER("CMD: failed to prep read\n");
  864. return ret;
  865. }
  866. batch_base = vmap_batch(batch_obj);
  867. if (!batch_base) {
  868. DRM_DEBUG_DRIVER("CMD: Failed to vmap batch\n");
  869. i915_gem_object_unpin_pages(batch_obj);
  870. return -ENOMEM;
  871. }
  872. if (needs_clflush)
  873. drm_clflush_virt_range((char *)batch_base, batch_obj->base.size);
  874. cmd = batch_base + (batch_start_offset / sizeof(*cmd));
  875. batch_end = cmd + (batch_obj->base.size / sizeof(*batch_end));
  876. while (cmd < batch_end) {
  877. const struct drm_i915_cmd_descriptor *desc;
  878. u32 length;
  879. if (*cmd == MI_BATCH_BUFFER_END)
  880. break;
  881. desc = find_cmd(ring, *cmd, &default_desc);
  882. if (!desc) {
  883. DRM_DEBUG_DRIVER("CMD: Unrecognized command: 0x%08X\n",
  884. *cmd);
  885. ret = -EINVAL;
  886. break;
  887. }
  888. if (desc->flags & CMD_DESC_FIXED)
  889. length = desc->length.fixed;
  890. else
  891. length = ((*cmd & desc->length.mask) + LENGTH_BIAS);
  892. if ((batch_end - cmd) < length) {
  893. DRM_DEBUG_DRIVER("CMD: Command length exceeds batch length: 0x%08X length=%u batchlen=%td\n",
  894. *cmd,
  895. length,
  896. batch_end - cmd);
  897. ret = -EINVAL;
  898. break;
  899. }
  900. if (!check_cmd(ring, desc, cmd, is_master, &oacontrol_set)) {
  901. ret = -EINVAL;
  902. break;
  903. }
  904. cmd += length;
  905. }
  906. if (oacontrol_set) {
  907. DRM_DEBUG_DRIVER("CMD: batch set OACONTROL but did not clear it\n");
  908. ret = -EINVAL;
  909. }
  910. if (cmd >= batch_end) {
  911. DRM_DEBUG_DRIVER("CMD: Got to the end of the buffer w/o a BBE cmd!\n");
  912. ret = -EINVAL;
  913. }
  914. vunmap(batch_base);
  915. i915_gem_object_unpin_pages(batch_obj);
  916. return ret;
  917. }
  918. /**
  919. * i915_cmd_parser_get_version() - get the cmd parser version number
  920. *
  921. * The cmd parser maintains a simple increasing integer version number suitable
  922. * for passing to userspace clients to determine what operations are permitted.
  923. *
  924. * Return: the current version number of the cmd parser
  925. */
  926. int i915_cmd_parser_get_version(void)
  927. {
  928. /*
  929. * Command parser version history
  930. *
  931. * 1. Initial version. Checks batches and reports violations, but leaves
  932. * hardware parsing enabled (so does not allow new use cases).
  933. */
  934. return 1;
  935. }