i915_cmd_parser.c 39 KB

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