i915_cmd_parser.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061
  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. };
  407. static const u32 gen7_blt_regs[] = {
  408. BCS_SWCTRL,
  409. };
  410. static const u32 ivb_master_regs[] = {
  411. FORCEWAKE_MT,
  412. DERRMR,
  413. GEN7_PIPE_DE_LOAD_SL(PIPE_A),
  414. GEN7_PIPE_DE_LOAD_SL(PIPE_B),
  415. GEN7_PIPE_DE_LOAD_SL(PIPE_C),
  416. };
  417. static const u32 hsw_master_regs[] = {
  418. FORCEWAKE_MT,
  419. DERRMR,
  420. };
  421. #undef REG64
  422. static u32 gen7_render_get_cmd_length_mask(u32 cmd_header)
  423. {
  424. u32 client = (cmd_header & INSTR_CLIENT_MASK) >> INSTR_CLIENT_SHIFT;
  425. u32 subclient =
  426. (cmd_header & INSTR_SUBCLIENT_MASK) >> INSTR_SUBCLIENT_SHIFT;
  427. if (client == INSTR_MI_CLIENT)
  428. return 0x3F;
  429. else if (client == INSTR_RC_CLIENT) {
  430. if (subclient == INSTR_MEDIA_SUBCLIENT)
  431. return 0xFFFF;
  432. else
  433. return 0xFF;
  434. }
  435. DRM_DEBUG_DRIVER("CMD: Abnormal rcs cmd length! 0x%08X\n", cmd_header);
  436. return 0;
  437. }
  438. static u32 gen7_bsd_get_cmd_length_mask(u32 cmd_header)
  439. {
  440. u32 client = (cmd_header & INSTR_CLIENT_MASK) >> INSTR_CLIENT_SHIFT;
  441. u32 subclient =
  442. (cmd_header & INSTR_SUBCLIENT_MASK) >> INSTR_SUBCLIENT_SHIFT;
  443. if (client == INSTR_MI_CLIENT)
  444. return 0x3F;
  445. else if (client == INSTR_RC_CLIENT) {
  446. if (subclient == INSTR_MEDIA_SUBCLIENT)
  447. return 0xFFF;
  448. else
  449. return 0xFF;
  450. }
  451. DRM_DEBUG_DRIVER("CMD: Abnormal bsd cmd length! 0x%08X\n", cmd_header);
  452. return 0;
  453. }
  454. static u32 gen7_blt_get_cmd_length_mask(u32 cmd_header)
  455. {
  456. u32 client = (cmd_header & INSTR_CLIENT_MASK) >> INSTR_CLIENT_SHIFT;
  457. if (client == INSTR_MI_CLIENT)
  458. return 0x3F;
  459. else if (client == INSTR_BC_CLIENT)
  460. return 0xFF;
  461. DRM_DEBUG_DRIVER("CMD: Abnormal blt cmd length! 0x%08X\n", cmd_header);
  462. return 0;
  463. }
  464. static bool validate_cmds_sorted(struct intel_engine_cs *ring,
  465. const struct drm_i915_cmd_table *cmd_tables,
  466. int cmd_table_count)
  467. {
  468. int i;
  469. bool ret = true;
  470. if (!cmd_tables || cmd_table_count == 0)
  471. return true;
  472. for (i = 0; i < cmd_table_count; i++) {
  473. const struct drm_i915_cmd_table *table = &cmd_tables[i];
  474. u32 previous = 0;
  475. int j;
  476. for (j = 0; j < table->count; j++) {
  477. const struct drm_i915_cmd_descriptor *desc =
  478. &table->table[i];
  479. u32 curr = desc->cmd.value & desc->cmd.mask;
  480. if (curr < previous) {
  481. DRM_ERROR("CMD: table not sorted ring=%d table=%d entry=%d cmd=0x%08X prev=0x%08X\n",
  482. ring->id, i, j, curr, previous);
  483. ret = false;
  484. }
  485. previous = curr;
  486. }
  487. }
  488. return ret;
  489. }
  490. static bool check_sorted(int ring_id, const u32 *reg_table, int reg_count)
  491. {
  492. int i;
  493. u32 previous = 0;
  494. bool ret = true;
  495. for (i = 0; i < reg_count; i++) {
  496. u32 curr = reg_table[i];
  497. if (curr < previous) {
  498. DRM_ERROR("CMD: table not sorted ring=%d entry=%d reg=0x%08X prev=0x%08X\n",
  499. ring_id, i, curr, previous);
  500. ret = false;
  501. }
  502. previous = curr;
  503. }
  504. return ret;
  505. }
  506. static bool validate_regs_sorted(struct intel_engine_cs *ring)
  507. {
  508. return check_sorted(ring->id, ring->reg_table, ring->reg_count) &&
  509. check_sorted(ring->id, ring->master_reg_table,
  510. ring->master_reg_count);
  511. }
  512. struct cmd_node {
  513. const struct drm_i915_cmd_descriptor *desc;
  514. struct hlist_node node;
  515. };
  516. /*
  517. * Different command ranges have different numbers of bits for the opcode. For
  518. * example, MI commands use bits 31:23 while 3D commands use bits 31:16. The
  519. * problem is that, for example, MI commands use bits 22:16 for other fields
  520. * such as GGTT vs PPGTT bits. If we include those bits in the mask then when
  521. * we mask a command from a batch it could hash to the wrong bucket due to
  522. * non-opcode bits being set. But if we don't include those bits, some 3D
  523. * commands may hash to the same bucket due to not including opcode bits that
  524. * make the command unique. For now, we will risk hashing to the same bucket.
  525. *
  526. * If we attempt to generate a perfect hash, we should be able to look at bits
  527. * 31:29 of a command from a batch buffer and use the full mask for that
  528. * client. The existing INSTR_CLIENT_MASK/SHIFT defines can be used for this.
  529. */
  530. #define CMD_HASH_MASK STD_MI_OPCODE_MASK
  531. static int init_hash_table(struct intel_engine_cs *ring,
  532. const struct drm_i915_cmd_table *cmd_tables,
  533. int cmd_table_count)
  534. {
  535. int i, j;
  536. hash_init(ring->cmd_hash);
  537. for (i = 0; i < cmd_table_count; i++) {
  538. const struct drm_i915_cmd_table *table = &cmd_tables[i];
  539. for (j = 0; j < table->count; j++) {
  540. const struct drm_i915_cmd_descriptor *desc =
  541. &table->table[j];
  542. struct cmd_node *desc_node =
  543. kmalloc(sizeof(*desc_node), GFP_KERNEL);
  544. if (!desc_node)
  545. return -ENOMEM;
  546. desc_node->desc = desc;
  547. hash_add(ring->cmd_hash, &desc_node->node,
  548. desc->cmd.value & CMD_HASH_MASK);
  549. }
  550. }
  551. return 0;
  552. }
  553. static void fini_hash_table(struct intel_engine_cs *ring)
  554. {
  555. struct hlist_node *tmp;
  556. struct cmd_node *desc_node;
  557. int i;
  558. hash_for_each_safe(ring->cmd_hash, i, tmp, desc_node, node) {
  559. hash_del(&desc_node->node);
  560. kfree(desc_node);
  561. }
  562. }
  563. /**
  564. * i915_cmd_parser_init_ring() - set cmd parser related fields for a ringbuffer
  565. * @ring: the ringbuffer to initialize
  566. *
  567. * Optionally initializes fields related to batch buffer command parsing in the
  568. * struct intel_engine_cs based on whether the platform requires software
  569. * command parsing.
  570. *
  571. * Return: non-zero if initialization fails
  572. */
  573. int i915_cmd_parser_init_ring(struct intel_engine_cs *ring)
  574. {
  575. const struct drm_i915_cmd_table *cmd_tables;
  576. int cmd_table_count;
  577. int ret;
  578. if (!IS_GEN7(ring->dev))
  579. return 0;
  580. switch (ring->id) {
  581. case RCS:
  582. if (IS_HASWELL(ring->dev)) {
  583. cmd_tables = hsw_render_ring_cmds;
  584. cmd_table_count =
  585. ARRAY_SIZE(hsw_render_ring_cmds);
  586. } else {
  587. cmd_tables = gen7_render_cmds;
  588. cmd_table_count = ARRAY_SIZE(gen7_render_cmds);
  589. }
  590. ring->reg_table = gen7_render_regs;
  591. ring->reg_count = ARRAY_SIZE(gen7_render_regs);
  592. if (IS_HASWELL(ring->dev)) {
  593. ring->master_reg_table = hsw_master_regs;
  594. ring->master_reg_count = ARRAY_SIZE(hsw_master_regs);
  595. } else {
  596. ring->master_reg_table = ivb_master_regs;
  597. ring->master_reg_count = ARRAY_SIZE(ivb_master_regs);
  598. }
  599. ring->get_cmd_length_mask = gen7_render_get_cmd_length_mask;
  600. break;
  601. case VCS:
  602. cmd_tables = gen7_video_cmds;
  603. cmd_table_count = ARRAY_SIZE(gen7_video_cmds);
  604. ring->get_cmd_length_mask = gen7_bsd_get_cmd_length_mask;
  605. break;
  606. case BCS:
  607. if (IS_HASWELL(ring->dev)) {
  608. cmd_tables = hsw_blt_ring_cmds;
  609. cmd_table_count = ARRAY_SIZE(hsw_blt_ring_cmds);
  610. } else {
  611. cmd_tables = gen7_blt_cmds;
  612. cmd_table_count = ARRAY_SIZE(gen7_blt_cmds);
  613. }
  614. ring->reg_table = gen7_blt_regs;
  615. ring->reg_count = ARRAY_SIZE(gen7_blt_regs);
  616. if (IS_HASWELL(ring->dev)) {
  617. ring->master_reg_table = hsw_master_regs;
  618. ring->master_reg_count = ARRAY_SIZE(hsw_master_regs);
  619. } else {
  620. ring->master_reg_table = ivb_master_regs;
  621. ring->master_reg_count = ARRAY_SIZE(ivb_master_regs);
  622. }
  623. ring->get_cmd_length_mask = gen7_blt_get_cmd_length_mask;
  624. break;
  625. case VECS:
  626. cmd_tables = hsw_vebox_cmds;
  627. cmd_table_count = ARRAY_SIZE(hsw_vebox_cmds);
  628. /* VECS can use the same length_mask function as VCS */
  629. ring->get_cmd_length_mask = gen7_bsd_get_cmd_length_mask;
  630. break;
  631. default:
  632. DRM_ERROR("CMD: cmd_parser_init with unknown ring: %d\n",
  633. ring->id);
  634. BUG();
  635. }
  636. BUG_ON(!validate_cmds_sorted(ring, cmd_tables, cmd_table_count));
  637. BUG_ON(!validate_regs_sorted(ring));
  638. ret = init_hash_table(ring, cmd_tables, cmd_table_count);
  639. if (ret) {
  640. DRM_ERROR("CMD: cmd_parser_init failed!\n");
  641. fini_hash_table(ring);
  642. return ret;
  643. }
  644. ring->needs_cmd_parser = true;
  645. return 0;
  646. }
  647. /**
  648. * i915_cmd_parser_fini_ring() - clean up cmd parser related fields
  649. * @ring: the ringbuffer to clean up
  650. *
  651. * Releases any resources related to command parsing that may have been
  652. * initialized for the specified ring.
  653. */
  654. void i915_cmd_parser_fini_ring(struct intel_engine_cs *ring)
  655. {
  656. if (!ring->needs_cmd_parser)
  657. return;
  658. fini_hash_table(ring);
  659. }
  660. static const struct drm_i915_cmd_descriptor*
  661. find_cmd_in_table(struct intel_engine_cs *ring,
  662. u32 cmd_header)
  663. {
  664. struct cmd_node *desc_node;
  665. hash_for_each_possible(ring->cmd_hash, desc_node, node,
  666. cmd_header & CMD_HASH_MASK) {
  667. const struct drm_i915_cmd_descriptor *desc = desc_node->desc;
  668. u32 masked_cmd = desc->cmd.mask & cmd_header;
  669. u32 masked_value = desc->cmd.value & desc->cmd.mask;
  670. if (masked_cmd == masked_value)
  671. return desc;
  672. }
  673. return NULL;
  674. }
  675. /*
  676. * Returns a pointer to a descriptor for the command specified by cmd_header.
  677. *
  678. * The caller must supply space for a default descriptor via the default_desc
  679. * parameter. If no descriptor for the specified command exists in the ring's
  680. * command parser tables, this function fills in default_desc based on the
  681. * ring's default length encoding and returns default_desc.
  682. */
  683. static const struct drm_i915_cmd_descriptor*
  684. find_cmd(struct intel_engine_cs *ring,
  685. u32 cmd_header,
  686. struct drm_i915_cmd_descriptor *default_desc)
  687. {
  688. const struct drm_i915_cmd_descriptor *desc;
  689. u32 mask;
  690. desc = find_cmd_in_table(ring, cmd_header);
  691. if (desc)
  692. return desc;
  693. mask = ring->get_cmd_length_mask(cmd_header);
  694. if (!mask)
  695. return NULL;
  696. BUG_ON(!default_desc);
  697. default_desc->flags = CMD_DESC_SKIP;
  698. default_desc->length.mask = mask;
  699. return default_desc;
  700. }
  701. static bool valid_reg(const u32 *table, int count, u32 addr)
  702. {
  703. if (table && count != 0) {
  704. int i;
  705. for (i = 0; i < count; i++) {
  706. if (table[i] == addr)
  707. return true;
  708. }
  709. }
  710. return false;
  711. }
  712. static u32 *vmap_batch(struct drm_i915_gem_object *obj)
  713. {
  714. int i;
  715. void *addr = NULL;
  716. struct sg_page_iter sg_iter;
  717. struct page **pages;
  718. pages = drm_malloc_ab(obj->base.size >> PAGE_SHIFT, sizeof(*pages));
  719. if (pages == NULL) {
  720. DRM_DEBUG_DRIVER("Failed to get space for pages\n");
  721. goto finish;
  722. }
  723. i = 0;
  724. for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents, 0) {
  725. pages[i] = sg_page_iter_page(&sg_iter);
  726. i++;
  727. }
  728. addr = vmap(pages, i, 0, PAGE_KERNEL);
  729. if (addr == NULL) {
  730. DRM_DEBUG_DRIVER("Failed to vmap pages\n");
  731. goto finish;
  732. }
  733. finish:
  734. if (pages)
  735. drm_free_large(pages);
  736. return (u32*)addr;
  737. }
  738. /**
  739. * i915_needs_cmd_parser() - should a given ring use software command parsing?
  740. * @ring: the ring in question
  741. *
  742. * Only certain platforms require software batch buffer command parsing, and
  743. * only when enabled via module paramter.
  744. *
  745. * Return: true if the ring requires software command parsing
  746. */
  747. bool i915_needs_cmd_parser(struct intel_engine_cs *ring)
  748. {
  749. struct drm_i915_private *dev_priv = ring->dev->dev_private;
  750. if (!ring->needs_cmd_parser)
  751. return false;
  752. /*
  753. * XXX: VLV is Gen7 and therefore has cmd_tables, but has PPGTT
  754. * disabled. That will cause all of the parser's PPGTT checks to
  755. * fail. For now, disable parsing when PPGTT is off.
  756. */
  757. if (!dev_priv->mm.aliasing_ppgtt)
  758. return false;
  759. return (i915.enable_cmd_parser == 1);
  760. }
  761. static bool check_cmd(const struct intel_engine_cs *ring,
  762. const struct drm_i915_cmd_descriptor *desc,
  763. const u32 *cmd,
  764. const bool is_master,
  765. bool *oacontrol_set)
  766. {
  767. if (desc->flags & CMD_DESC_REJECT) {
  768. DRM_DEBUG_DRIVER("CMD: Rejected command: 0x%08X\n", *cmd);
  769. return false;
  770. }
  771. if ((desc->flags & CMD_DESC_MASTER) && !is_master) {
  772. DRM_DEBUG_DRIVER("CMD: Rejected master-only command: 0x%08X\n",
  773. *cmd);
  774. return false;
  775. }
  776. if (desc->flags & CMD_DESC_REGISTER) {
  777. u32 reg_addr = cmd[desc->reg.offset] & desc->reg.mask;
  778. /*
  779. * OACONTROL requires some special handling for writes. We
  780. * want to make sure that any batch which enables OA also
  781. * disables it before the end of the batch. The goal is to
  782. * prevent one process from snooping on the perf data from
  783. * another process. To do that, we need to check the value
  784. * that will be written to the register. Hence, limit
  785. * OACONTROL writes to only MI_LOAD_REGISTER_IMM commands.
  786. */
  787. if (reg_addr == OACONTROL) {
  788. if (desc->cmd.value == MI_LOAD_REGISTER_MEM)
  789. return false;
  790. if (desc->cmd.value == MI_LOAD_REGISTER_IMM(1))
  791. *oacontrol_set = (cmd[2] != 0);
  792. }
  793. if (!valid_reg(ring->reg_table,
  794. ring->reg_count, reg_addr)) {
  795. if (!is_master ||
  796. !valid_reg(ring->master_reg_table,
  797. ring->master_reg_count,
  798. reg_addr)) {
  799. DRM_DEBUG_DRIVER("CMD: Rejected register 0x%08X in command: 0x%08X (ring=%d)\n",
  800. reg_addr,
  801. *cmd,
  802. ring->id);
  803. return false;
  804. }
  805. }
  806. }
  807. if (desc->flags & CMD_DESC_BITMASK) {
  808. int i;
  809. for (i = 0; i < MAX_CMD_DESC_BITMASKS; i++) {
  810. u32 dword;
  811. if (desc->bits[i].mask == 0)
  812. break;
  813. if (desc->bits[i].condition_mask != 0) {
  814. u32 offset =
  815. desc->bits[i].condition_offset;
  816. u32 condition = cmd[offset] &
  817. desc->bits[i].condition_mask;
  818. if (condition == 0)
  819. continue;
  820. }
  821. dword = cmd[desc->bits[i].offset] &
  822. desc->bits[i].mask;
  823. if (dword != desc->bits[i].expected) {
  824. DRM_DEBUG_DRIVER("CMD: Rejected command 0x%08X for bitmask 0x%08X (exp=0x%08X act=0x%08X) (ring=%d)\n",
  825. *cmd,
  826. desc->bits[i].mask,
  827. desc->bits[i].expected,
  828. dword, ring->id);
  829. return false;
  830. }
  831. }
  832. }
  833. return true;
  834. }
  835. #define LENGTH_BIAS 2
  836. /**
  837. * i915_parse_cmds() - parse a submitted batch buffer for privilege violations
  838. * @ring: the ring on which the batch is to execute
  839. * @batch_obj: the batch buffer in question
  840. * @batch_start_offset: byte offset in the batch at which execution starts
  841. * @is_master: is the submitting process the drm master?
  842. *
  843. * Parses the specified batch buffer looking for privilege violations as
  844. * described in the overview.
  845. *
  846. * Return: non-zero if the parser finds violations or otherwise fails
  847. */
  848. int i915_parse_cmds(struct intel_engine_cs *ring,
  849. struct drm_i915_gem_object *batch_obj,
  850. u32 batch_start_offset,
  851. bool is_master)
  852. {
  853. int ret = 0;
  854. u32 *cmd, *batch_base, *batch_end;
  855. struct drm_i915_cmd_descriptor default_desc = { 0 };
  856. int needs_clflush = 0;
  857. bool oacontrol_set = false; /* OACONTROL tracking. See check_cmd() */
  858. ret = i915_gem_obj_prepare_shmem_read(batch_obj, &needs_clflush);
  859. if (ret) {
  860. DRM_DEBUG_DRIVER("CMD: failed to prep read\n");
  861. return ret;
  862. }
  863. batch_base = vmap_batch(batch_obj);
  864. if (!batch_base) {
  865. DRM_DEBUG_DRIVER("CMD: Failed to vmap batch\n");
  866. i915_gem_object_unpin_pages(batch_obj);
  867. return -ENOMEM;
  868. }
  869. if (needs_clflush)
  870. drm_clflush_virt_range((char *)batch_base, batch_obj->base.size);
  871. cmd = batch_base + (batch_start_offset / sizeof(*cmd));
  872. batch_end = cmd + (batch_obj->base.size / sizeof(*batch_end));
  873. while (cmd < batch_end) {
  874. const struct drm_i915_cmd_descriptor *desc;
  875. u32 length;
  876. if (*cmd == MI_BATCH_BUFFER_END)
  877. break;
  878. desc = find_cmd(ring, *cmd, &default_desc);
  879. if (!desc) {
  880. DRM_DEBUG_DRIVER("CMD: Unrecognized command: 0x%08X\n",
  881. *cmd);
  882. ret = -EINVAL;
  883. break;
  884. }
  885. if (desc->flags & CMD_DESC_FIXED)
  886. length = desc->length.fixed;
  887. else
  888. length = ((*cmd & desc->length.mask) + LENGTH_BIAS);
  889. if ((batch_end - cmd) < length) {
  890. DRM_DEBUG_DRIVER("CMD: Command length exceeds batch length: 0x%08X length=%u batchlen=%td\n",
  891. *cmd,
  892. length,
  893. batch_end - cmd);
  894. ret = -EINVAL;
  895. break;
  896. }
  897. if (!check_cmd(ring, desc, cmd, is_master, &oacontrol_set)) {
  898. ret = -EINVAL;
  899. break;
  900. }
  901. cmd += length;
  902. }
  903. if (oacontrol_set) {
  904. DRM_DEBUG_DRIVER("CMD: batch set OACONTROL but did not clear it\n");
  905. ret = -EINVAL;
  906. }
  907. if (cmd >= batch_end) {
  908. DRM_DEBUG_DRIVER("CMD: Got to the end of the buffer w/o a BBE cmd!\n");
  909. ret = -EINVAL;
  910. }
  911. vunmap(batch_base);
  912. i915_gem_object_unpin_pages(batch_obj);
  913. return ret;
  914. }
  915. /**
  916. * i915_cmd_parser_get_version() - get the cmd parser version number
  917. *
  918. * The cmd parser maintains a simple increasing integer version number suitable
  919. * for passing to userspace clients to determine what operations are permitted.
  920. *
  921. * Return: the current version number of the cmd parser
  922. */
  923. int i915_cmd_parser_get_version(void)
  924. {
  925. /*
  926. * Command parser version history
  927. *
  928. * 1. Initial version. Checks batches and reports violations, but leaves
  929. * hardware parsing enabled (so does not allow new use cases).
  930. */
  931. return 1;
  932. }