i915_cmd_parser.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568
  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. #include "intel_ringbuffer.h"
  29. /**
  30. * DOC: batch buffer command parser
  31. *
  32. * Motivation:
  33. * Certain OpenGL features (e.g. transform feedback, performance monitoring)
  34. * require userspace code to submit batches containing commands such as
  35. * MI_LOAD_REGISTER_IMM to access various registers. Unfortunately, some
  36. * generations of the hardware will noop these commands in "unsecure" batches
  37. * (which includes all userspace batches submitted via i915) even though the
  38. * commands may be safe and represent the intended programming model of the
  39. * device.
  40. *
  41. * The software command parser is similar in operation to the command parsing
  42. * done in hardware for unsecure batches. However, the software parser allows
  43. * some operations that would be noop'd by hardware, if the parser determines
  44. * the operation is safe, and submits the batch as "secure" to prevent hardware
  45. * parsing.
  46. *
  47. * Threats:
  48. * At a high level, the hardware (and software) checks attempt to prevent
  49. * granting userspace undue privileges. There are three categories of privilege.
  50. *
  51. * First, commands which are explicitly defined as privileged or which should
  52. * only be used by the kernel driver. The parser rejects such commands
  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
  57. *
  58. * Third, commands which access privileged memory (i.e. GGTT, HWS page, etc).
  59. * The parser always rejects such commands.
  60. *
  61. * The majority of the problematic commands fall in the MI_* range, with only a
  62. * few specific commands on each engine (e.g. PIPE_CONTROL and MI_FLUSH_DW).
  63. *
  64. * Implementation:
  65. * Each engine maintains tables of commands and registers which the parser
  66. * uses in scanning batch buffers submitted to that engine.
  67. *
  68. * Since the set of commands that the parser must check for is significantly
  69. * smaller than the number of commands supported, the parser tables contain only
  70. * those commands required by the parser. This generally works because command
  71. * opcode ranges have standard command length encodings. So for commands that
  72. * the parser does not need to check, it can easily skip them. This is
  73. * implemented via a per-engine length decoding vfunc.
  74. *
  75. * Unfortunately, there are a number of commands that do not follow the standard
  76. * length encoding for their opcode range, primarily amongst the MI_* commands.
  77. * To handle this, the parser provides a way to define explicit "skip" entries
  78. * in the per-engine command tables.
  79. *
  80. * Other command table entries map fairly directly to high level categories
  81. * mentioned above: rejected, register whitelist. The parser implements a number
  82. * of checks, including the privileged memory checks, via a general bitmasking
  83. * mechanism.
  84. */
  85. /*
  86. * A command that requires special handling by the command parser.
  87. */
  88. struct drm_i915_cmd_descriptor {
  89. /*
  90. * Flags describing how the command parser processes the command.
  91. *
  92. * CMD_DESC_FIXED: The command has a fixed length if this is set,
  93. * a length mask if not set
  94. * CMD_DESC_SKIP: The command is allowed but does not follow the
  95. * standard length encoding for the opcode range in
  96. * which it falls
  97. * CMD_DESC_REJECT: The command is never allowed
  98. * CMD_DESC_REGISTER: The command should be checked against the
  99. * register whitelist for the appropriate ring
  100. */
  101. u32 flags;
  102. #define CMD_DESC_FIXED (1<<0)
  103. #define CMD_DESC_SKIP (1<<1)
  104. #define CMD_DESC_REJECT (1<<2)
  105. #define CMD_DESC_REGISTER (1<<3)
  106. #define CMD_DESC_BITMASK (1<<4)
  107. /*
  108. * The command's unique identification bits and the bitmask to get them.
  109. * This isn't strictly the opcode field as defined in the spec and may
  110. * also include type, subtype, and/or subop fields.
  111. */
  112. struct {
  113. u32 value;
  114. u32 mask;
  115. } cmd;
  116. /*
  117. * The command's length. The command is either fixed length (i.e. does
  118. * not include a length field) or has a length field mask. The flag
  119. * CMD_DESC_FIXED indicates a fixed length. Otherwise, the command has
  120. * a length mask. All command entries in a command table must include
  121. * length information.
  122. */
  123. union {
  124. u32 fixed;
  125. u32 mask;
  126. } length;
  127. /*
  128. * Describes where to find a register address in the command to check
  129. * against the ring's register whitelist. Only valid if flags has the
  130. * CMD_DESC_REGISTER bit set.
  131. *
  132. * A non-zero step value implies that the command may access multiple
  133. * registers in sequence (e.g. LRI), in that case step gives the
  134. * distance in dwords between individual offset fields.
  135. */
  136. struct {
  137. u32 offset;
  138. u32 mask;
  139. u32 step;
  140. } reg;
  141. #define MAX_CMD_DESC_BITMASKS 3
  142. /*
  143. * Describes command checks where a particular dword is masked and
  144. * compared against an expected value. If the command does not match
  145. * the expected value, the parser rejects it. Only valid if flags has
  146. * the CMD_DESC_BITMASK bit set. Only entries where mask is non-zero
  147. * are valid.
  148. *
  149. * If the check specifies a non-zero condition_mask then the parser
  150. * only performs the check when the bits specified by condition_mask
  151. * are non-zero.
  152. */
  153. struct {
  154. u32 offset;
  155. u32 mask;
  156. u32 expected;
  157. u32 condition_offset;
  158. u32 condition_mask;
  159. } bits[MAX_CMD_DESC_BITMASKS];
  160. };
  161. /*
  162. * A table of commands requiring special handling by the command parser.
  163. *
  164. * Each engine has an array of tables. Each table consists of an array of
  165. * command descriptors, which must be sorted with command opcodes in
  166. * ascending order.
  167. */
  168. struct drm_i915_cmd_table {
  169. const struct drm_i915_cmd_descriptor *table;
  170. int count;
  171. };
  172. #define STD_MI_OPCODE_SHIFT (32 - 9)
  173. #define STD_3D_OPCODE_SHIFT (32 - 16)
  174. #define STD_2D_OPCODE_SHIFT (32 - 10)
  175. #define STD_MFX_OPCODE_SHIFT (32 - 16)
  176. #define MIN_OPCODE_SHIFT 16
  177. #define CMD(op, opm, f, lm, fl, ...) \
  178. { \
  179. .flags = (fl) | ((f) ? CMD_DESC_FIXED : 0), \
  180. .cmd = { (op & ~0u << (opm)), ~0u << (opm) }, \
  181. .length = { (lm) }, \
  182. __VA_ARGS__ \
  183. }
  184. /* Convenience macros to compress the tables */
  185. #define SMI STD_MI_OPCODE_SHIFT
  186. #define S3D STD_3D_OPCODE_SHIFT
  187. #define S2D STD_2D_OPCODE_SHIFT
  188. #define SMFX STD_MFX_OPCODE_SHIFT
  189. #define F true
  190. #define S CMD_DESC_SKIP
  191. #define R CMD_DESC_REJECT
  192. #define W CMD_DESC_REGISTER
  193. #define B CMD_DESC_BITMASK
  194. /* Command Mask Fixed Len Action
  195. ---------------------------------------------------------- */
  196. static const struct drm_i915_cmd_descriptor gen7_common_cmds[] = {
  197. CMD( MI_NOOP, SMI, F, 1, S ),
  198. CMD( MI_USER_INTERRUPT, SMI, F, 1, R ),
  199. CMD( MI_WAIT_FOR_EVENT, SMI, F, 1, R ),
  200. CMD( MI_ARB_CHECK, SMI, F, 1, S ),
  201. CMD( MI_REPORT_HEAD, SMI, F, 1, S ),
  202. CMD( MI_SUSPEND_FLUSH, SMI, F, 1, S ),
  203. CMD( MI_SEMAPHORE_MBOX, SMI, !F, 0xFF, R ),
  204. CMD( MI_STORE_DWORD_INDEX, SMI, !F, 0xFF, R ),
  205. CMD( MI_LOAD_REGISTER_IMM(1), SMI, !F, 0xFF, W,
  206. .reg = { .offset = 1, .mask = 0x007FFFFC, .step = 2 } ),
  207. CMD( MI_STORE_REGISTER_MEM, SMI, F, 3, W | B,
  208. .reg = { .offset = 1, .mask = 0x007FFFFC },
  209. .bits = {{
  210. .offset = 0,
  211. .mask = MI_GLOBAL_GTT,
  212. .expected = 0,
  213. }}, ),
  214. CMD( MI_LOAD_REGISTER_MEM, SMI, F, 3, W | B,
  215. .reg = { .offset = 1, .mask = 0x007FFFFC },
  216. .bits = {{
  217. .offset = 0,
  218. .mask = MI_GLOBAL_GTT,
  219. .expected = 0,
  220. }}, ),
  221. /*
  222. * MI_BATCH_BUFFER_START requires some special handling. It's not
  223. * really a 'skip' action but it doesn't seem like it's worth adding
  224. * a new action. See i915_parse_cmds().
  225. */
  226. CMD( MI_BATCH_BUFFER_START, SMI, !F, 0xFF, S ),
  227. };
  228. static const struct drm_i915_cmd_descriptor gen7_render_cmds[] = {
  229. CMD( MI_FLUSH, SMI, F, 1, S ),
  230. CMD( MI_ARB_ON_OFF, SMI, F, 1, R ),
  231. CMD( MI_PREDICATE, SMI, F, 1, S ),
  232. CMD( MI_TOPOLOGY_FILTER, SMI, F, 1, S ),
  233. CMD( MI_SET_APPID, SMI, F, 1, S ),
  234. CMD( MI_DISPLAY_FLIP, SMI, !F, 0xFF, R ),
  235. CMD( MI_SET_CONTEXT, SMI, !F, 0xFF, R ),
  236. CMD( MI_URB_CLEAR, SMI, !F, 0xFF, S ),
  237. CMD( MI_STORE_DWORD_IMM, SMI, !F, 0x3F, B,
  238. .bits = {{
  239. .offset = 0,
  240. .mask = MI_GLOBAL_GTT,
  241. .expected = 0,
  242. }}, ),
  243. CMD( MI_UPDATE_GTT, SMI, !F, 0xFF, R ),
  244. CMD( MI_CLFLUSH, SMI, !F, 0x3FF, B,
  245. .bits = {{
  246. .offset = 0,
  247. .mask = MI_GLOBAL_GTT,
  248. .expected = 0,
  249. }}, ),
  250. CMD( MI_REPORT_PERF_COUNT, SMI, !F, 0x3F, B,
  251. .bits = {{
  252. .offset = 1,
  253. .mask = MI_REPORT_PERF_COUNT_GGTT,
  254. .expected = 0,
  255. }}, ),
  256. CMD( MI_CONDITIONAL_BATCH_BUFFER_END, SMI, !F, 0xFF, B,
  257. .bits = {{
  258. .offset = 0,
  259. .mask = MI_GLOBAL_GTT,
  260. .expected = 0,
  261. }}, ),
  262. CMD( GFX_OP_3DSTATE_VF_STATISTICS, S3D, F, 1, S ),
  263. CMD( PIPELINE_SELECT, S3D, F, 1, S ),
  264. CMD( MEDIA_VFE_STATE, S3D, !F, 0xFFFF, B,
  265. .bits = {{
  266. .offset = 2,
  267. .mask = MEDIA_VFE_STATE_MMIO_ACCESS_MASK,
  268. .expected = 0,
  269. }}, ),
  270. CMD( GPGPU_OBJECT, S3D, !F, 0xFF, S ),
  271. CMD( GPGPU_WALKER, S3D, !F, 0xFF, S ),
  272. CMD( GFX_OP_3DSTATE_SO_DECL_LIST, S3D, !F, 0x1FF, S ),
  273. CMD( GFX_OP_PIPE_CONTROL(5), S3D, !F, 0xFF, B,
  274. .bits = {{
  275. .offset = 1,
  276. .mask = (PIPE_CONTROL_MMIO_WRITE | PIPE_CONTROL_NOTIFY),
  277. .expected = 0,
  278. },
  279. {
  280. .offset = 1,
  281. .mask = (PIPE_CONTROL_GLOBAL_GTT_IVB |
  282. PIPE_CONTROL_STORE_DATA_INDEX),
  283. .expected = 0,
  284. .condition_offset = 1,
  285. .condition_mask = PIPE_CONTROL_POST_SYNC_OP_MASK,
  286. }}, ),
  287. };
  288. static const struct drm_i915_cmd_descriptor hsw_render_cmds[] = {
  289. CMD( MI_SET_PREDICATE, SMI, F, 1, S ),
  290. CMD( MI_RS_CONTROL, SMI, F, 1, S ),
  291. CMD( MI_URB_ATOMIC_ALLOC, SMI, F, 1, S ),
  292. CMD( MI_SET_APPID, SMI, F, 1, S ),
  293. CMD( MI_RS_CONTEXT, SMI, F, 1, S ),
  294. CMD( MI_LOAD_SCAN_LINES_INCL, SMI, !F, 0x3F, R ),
  295. CMD( MI_LOAD_SCAN_LINES_EXCL, SMI, !F, 0x3F, R ),
  296. CMD( MI_LOAD_REGISTER_REG, SMI, !F, 0xFF, W,
  297. .reg = { .offset = 1, .mask = 0x007FFFFC, .step = 1 } ),
  298. CMD( MI_RS_STORE_DATA_IMM, SMI, !F, 0xFF, S ),
  299. CMD( MI_LOAD_URB_MEM, SMI, !F, 0xFF, S ),
  300. CMD( MI_STORE_URB_MEM, SMI, !F, 0xFF, S ),
  301. CMD( GFX_OP_3DSTATE_DX9_CONSTANTF_VS, S3D, !F, 0x7FF, S ),
  302. CMD( GFX_OP_3DSTATE_DX9_CONSTANTF_PS, S3D, !F, 0x7FF, S ),
  303. CMD( GFX_OP_3DSTATE_BINDING_TABLE_EDIT_VS, S3D, !F, 0x1FF, S ),
  304. CMD( GFX_OP_3DSTATE_BINDING_TABLE_EDIT_GS, S3D, !F, 0x1FF, S ),
  305. CMD( GFX_OP_3DSTATE_BINDING_TABLE_EDIT_HS, S3D, !F, 0x1FF, S ),
  306. CMD( GFX_OP_3DSTATE_BINDING_TABLE_EDIT_DS, S3D, !F, 0x1FF, S ),
  307. CMD( GFX_OP_3DSTATE_BINDING_TABLE_EDIT_PS, S3D, !F, 0x1FF, S ),
  308. };
  309. static const struct drm_i915_cmd_descriptor gen7_video_cmds[] = {
  310. CMD( MI_ARB_ON_OFF, SMI, F, 1, R ),
  311. CMD( MI_SET_APPID, SMI, F, 1, S ),
  312. CMD( MI_STORE_DWORD_IMM, SMI, !F, 0xFF, B,
  313. .bits = {{
  314. .offset = 0,
  315. .mask = MI_GLOBAL_GTT,
  316. .expected = 0,
  317. }}, ),
  318. CMD( MI_UPDATE_GTT, SMI, !F, 0x3F, R ),
  319. CMD( MI_FLUSH_DW, SMI, !F, 0x3F, B,
  320. .bits = {{
  321. .offset = 0,
  322. .mask = MI_FLUSH_DW_NOTIFY,
  323. .expected = 0,
  324. },
  325. {
  326. .offset = 1,
  327. .mask = MI_FLUSH_DW_USE_GTT,
  328. .expected = 0,
  329. .condition_offset = 0,
  330. .condition_mask = MI_FLUSH_DW_OP_MASK,
  331. },
  332. {
  333. .offset = 0,
  334. .mask = MI_FLUSH_DW_STORE_INDEX,
  335. .expected = 0,
  336. .condition_offset = 0,
  337. .condition_mask = MI_FLUSH_DW_OP_MASK,
  338. }}, ),
  339. CMD( MI_CONDITIONAL_BATCH_BUFFER_END, SMI, !F, 0xFF, B,
  340. .bits = {{
  341. .offset = 0,
  342. .mask = MI_GLOBAL_GTT,
  343. .expected = 0,
  344. }}, ),
  345. /*
  346. * MFX_WAIT doesn't fit the way we handle length for most commands.
  347. * It has a length field but it uses a non-standard length bias.
  348. * It is always 1 dword though, so just treat it as fixed length.
  349. */
  350. CMD( MFX_WAIT, SMFX, F, 1, S ),
  351. };
  352. static const struct drm_i915_cmd_descriptor gen7_vecs_cmds[] = {
  353. CMD( MI_ARB_ON_OFF, SMI, F, 1, R ),
  354. CMD( MI_SET_APPID, SMI, F, 1, S ),
  355. CMD( MI_STORE_DWORD_IMM, SMI, !F, 0xFF, B,
  356. .bits = {{
  357. .offset = 0,
  358. .mask = MI_GLOBAL_GTT,
  359. .expected = 0,
  360. }}, ),
  361. CMD( MI_UPDATE_GTT, SMI, !F, 0x3F, R ),
  362. CMD( MI_FLUSH_DW, SMI, !F, 0x3F, B,
  363. .bits = {{
  364. .offset = 0,
  365. .mask = MI_FLUSH_DW_NOTIFY,
  366. .expected = 0,
  367. },
  368. {
  369. .offset = 1,
  370. .mask = MI_FLUSH_DW_USE_GTT,
  371. .expected = 0,
  372. .condition_offset = 0,
  373. .condition_mask = MI_FLUSH_DW_OP_MASK,
  374. },
  375. {
  376. .offset = 0,
  377. .mask = MI_FLUSH_DW_STORE_INDEX,
  378. .expected = 0,
  379. .condition_offset = 0,
  380. .condition_mask = MI_FLUSH_DW_OP_MASK,
  381. }}, ),
  382. CMD( MI_CONDITIONAL_BATCH_BUFFER_END, SMI, !F, 0xFF, B,
  383. .bits = {{
  384. .offset = 0,
  385. .mask = MI_GLOBAL_GTT,
  386. .expected = 0,
  387. }}, ),
  388. };
  389. static const struct drm_i915_cmd_descriptor gen7_blt_cmds[] = {
  390. CMD( MI_DISPLAY_FLIP, SMI, !F, 0xFF, R ),
  391. CMD( MI_STORE_DWORD_IMM, SMI, !F, 0x3FF, B,
  392. .bits = {{
  393. .offset = 0,
  394. .mask = MI_GLOBAL_GTT,
  395. .expected = 0,
  396. }}, ),
  397. CMD( MI_UPDATE_GTT, SMI, !F, 0x3F, R ),
  398. CMD( MI_FLUSH_DW, SMI, !F, 0x3F, B,
  399. .bits = {{
  400. .offset = 0,
  401. .mask = MI_FLUSH_DW_NOTIFY,
  402. .expected = 0,
  403. },
  404. {
  405. .offset = 1,
  406. .mask = MI_FLUSH_DW_USE_GTT,
  407. .expected = 0,
  408. .condition_offset = 0,
  409. .condition_mask = MI_FLUSH_DW_OP_MASK,
  410. },
  411. {
  412. .offset = 0,
  413. .mask = MI_FLUSH_DW_STORE_INDEX,
  414. .expected = 0,
  415. .condition_offset = 0,
  416. .condition_mask = MI_FLUSH_DW_OP_MASK,
  417. }}, ),
  418. CMD( COLOR_BLT, S2D, !F, 0x3F, S ),
  419. CMD( SRC_COPY_BLT, S2D, !F, 0x3F, S ),
  420. };
  421. static const struct drm_i915_cmd_descriptor hsw_blt_cmds[] = {
  422. CMD( MI_LOAD_SCAN_LINES_INCL, SMI, !F, 0x3F, R ),
  423. CMD( MI_LOAD_SCAN_LINES_EXCL, SMI, !F, 0x3F, R ),
  424. };
  425. /*
  426. * For Gen9 we can still rely on the h/w to enforce cmd security, and only
  427. * need to re-enforce the register access checks. We therefore only need to
  428. * teach the cmdparser how to find the end of each command, and identify
  429. * register accesses. The table doesn't need to reject any commands, and so
  430. * the only commands listed here are:
  431. * 1) Those that touch registers
  432. * 2) Those that do not have the default 8-bit length
  433. *
  434. * Note that the default MI length mask chosen for this table is 0xFF, not
  435. * the 0x3F used on older devices. This is because the vast majority of MI
  436. * cmds on Gen9 use a standard 8-bit Length field.
  437. * All the Gen9 blitter instructions are standard 0xFF length mask, and
  438. * none allow access to non-general registers, so in fact no BLT cmds are
  439. * included in the table at all.
  440. *
  441. */
  442. static const struct drm_i915_cmd_descriptor gen9_blt_cmds[] = {
  443. CMD( MI_NOOP, SMI, F, 1, S ),
  444. CMD( MI_USER_INTERRUPT, SMI, F, 1, S ),
  445. CMD( MI_WAIT_FOR_EVENT, SMI, F, 1, S ),
  446. CMD( MI_FLUSH, SMI, F, 1, S ),
  447. CMD( MI_ARB_CHECK, SMI, F, 1, S ),
  448. CMD( MI_REPORT_HEAD, SMI, F, 1, S ),
  449. CMD( MI_ARB_ON_OFF, SMI, F, 1, S ),
  450. CMD( MI_SUSPEND_FLUSH, SMI, F, 1, S ),
  451. CMD( MI_LOAD_SCAN_LINES_INCL, SMI, !F, 0x3F, S ),
  452. CMD( MI_LOAD_SCAN_LINES_EXCL, SMI, !F, 0x3F, S ),
  453. CMD( MI_STORE_DWORD_IMM, SMI, !F, 0x3FF, S ),
  454. CMD( MI_LOAD_REGISTER_IMM(1), SMI, !F, 0xFF, W,
  455. .reg = { .offset = 1, .mask = 0x007FFFFC, .step = 2 } ),
  456. CMD( MI_UPDATE_GTT, SMI, !F, 0x3FF, S ),
  457. CMD( MI_STORE_REGISTER_MEM_GEN8, SMI, F, 4, W,
  458. .reg = { .offset = 1, .mask = 0x007FFFFC } ),
  459. CMD( MI_FLUSH_DW, SMI, !F, 0x3F, S ),
  460. CMD( MI_LOAD_REGISTER_MEM_GEN8, SMI, F, 4, W,
  461. .reg = { .offset = 1, .mask = 0x007FFFFC } ),
  462. CMD( MI_LOAD_REGISTER_REG, SMI, !F, 0xFF, W,
  463. .reg = { .offset = 1, .mask = 0x007FFFFC, .step = 1 } ),
  464. /*
  465. * We allow BB_START but apply further checks. We just sanitize the
  466. * basic fields here.
  467. */
  468. #define MI_BB_START_OPERAND_MASK GENMASK(SMI-1, 0)
  469. #define MI_BB_START_OPERAND_EXPECT (MI_BATCH_PPGTT_HSW | 1)
  470. CMD( MI_BATCH_BUFFER_START_GEN8, SMI, !F, 0xFF, B,
  471. .bits = {{
  472. .offset = 0,
  473. .mask = MI_BB_START_OPERAND_MASK,
  474. .expected = MI_BB_START_OPERAND_EXPECT,
  475. }}, ),
  476. };
  477. static const struct drm_i915_cmd_descriptor noop_desc =
  478. CMD(MI_NOOP, SMI, F, 1, S);
  479. #undef CMD
  480. #undef SMI
  481. #undef S3D
  482. #undef S2D
  483. #undef SMFX
  484. #undef F
  485. #undef S
  486. #undef R
  487. #undef W
  488. #undef B
  489. static const struct drm_i915_cmd_table gen7_render_cmd_table[] = {
  490. { gen7_common_cmds, ARRAY_SIZE(gen7_common_cmds) },
  491. { gen7_render_cmds, ARRAY_SIZE(gen7_render_cmds) },
  492. };
  493. static const struct drm_i915_cmd_table hsw_render_ring_cmd_table[] = {
  494. { gen7_common_cmds, ARRAY_SIZE(gen7_common_cmds) },
  495. { gen7_render_cmds, ARRAY_SIZE(gen7_render_cmds) },
  496. { hsw_render_cmds, ARRAY_SIZE(hsw_render_cmds) },
  497. };
  498. static const struct drm_i915_cmd_table gen7_video_cmd_table[] = {
  499. { gen7_common_cmds, ARRAY_SIZE(gen7_common_cmds) },
  500. { gen7_video_cmds, ARRAY_SIZE(gen7_video_cmds) },
  501. };
  502. static const struct drm_i915_cmd_table hsw_vebox_cmd_table[] = {
  503. { gen7_common_cmds, ARRAY_SIZE(gen7_common_cmds) },
  504. { gen7_vecs_cmds, ARRAY_SIZE(gen7_vecs_cmds) },
  505. };
  506. static const struct drm_i915_cmd_table gen7_blt_cmd_table[] = {
  507. { gen7_common_cmds, ARRAY_SIZE(gen7_common_cmds) },
  508. { gen7_blt_cmds, ARRAY_SIZE(gen7_blt_cmds) },
  509. };
  510. static const struct drm_i915_cmd_table hsw_blt_ring_cmd_table[] = {
  511. { gen7_common_cmds, ARRAY_SIZE(gen7_common_cmds) },
  512. { gen7_blt_cmds, ARRAY_SIZE(gen7_blt_cmds) },
  513. { hsw_blt_cmds, ARRAY_SIZE(hsw_blt_cmds) },
  514. };
  515. static const struct drm_i915_cmd_table gen9_blt_cmd_table[] = {
  516. { gen9_blt_cmds, ARRAY_SIZE(gen9_blt_cmds) },
  517. };
  518. /*
  519. * Register whitelists, sorted by increasing register offset.
  520. */
  521. /*
  522. * An individual whitelist entry granting access to register addr. If
  523. * mask is non-zero the argument of immediate register writes will be
  524. * AND-ed with mask, and the command will be rejected if the result
  525. * doesn't match value.
  526. *
  527. * Registers with non-zero mask are only allowed to be written using
  528. * LRI.
  529. */
  530. struct drm_i915_reg_descriptor {
  531. i915_reg_t addr;
  532. u32 mask;
  533. u32 value;
  534. };
  535. /* Convenience macro for adding 32-bit registers. */
  536. #define REG32(_reg, ...) \
  537. { .addr = (_reg), __VA_ARGS__ }
  538. #define REG32_IDX(_reg, idx) \
  539. { .addr = _reg(idx) }
  540. /*
  541. * Convenience macro for adding 64-bit registers.
  542. *
  543. * Some registers that userspace accesses are 64 bits. The register
  544. * access commands only allow 32-bit accesses. Hence, we have to include
  545. * entries for both halves of the 64-bit registers.
  546. */
  547. #define REG64(_reg) \
  548. { .addr = _reg }, \
  549. { .addr = _reg ## _UDW }
  550. #define REG64_IDX(_reg, idx) \
  551. { .addr = _reg(idx) }, \
  552. { .addr = _reg ## _UDW(idx) }
  553. static const struct drm_i915_reg_descriptor gen7_render_regs[] = {
  554. REG64(GPGPU_THREADS_DISPATCHED),
  555. REG64(HS_INVOCATION_COUNT),
  556. REG64(DS_INVOCATION_COUNT),
  557. REG64(IA_VERTICES_COUNT),
  558. REG64(IA_PRIMITIVES_COUNT),
  559. REG64(VS_INVOCATION_COUNT),
  560. REG64(GS_INVOCATION_COUNT),
  561. REG64(GS_PRIMITIVES_COUNT),
  562. REG64(CL_INVOCATION_COUNT),
  563. REG64(CL_PRIMITIVES_COUNT),
  564. REG64(PS_INVOCATION_COUNT),
  565. REG64(PS_DEPTH_COUNT),
  566. REG64_IDX(RING_TIMESTAMP, RENDER_RING_BASE),
  567. REG64(MI_PREDICATE_SRC0),
  568. REG64(MI_PREDICATE_SRC1),
  569. REG32(GEN7_3DPRIM_END_OFFSET),
  570. REG32(GEN7_3DPRIM_START_VERTEX),
  571. REG32(GEN7_3DPRIM_VERTEX_COUNT),
  572. REG32(GEN7_3DPRIM_INSTANCE_COUNT),
  573. REG32(GEN7_3DPRIM_START_INSTANCE),
  574. REG32(GEN7_3DPRIM_BASE_VERTEX),
  575. REG32(GEN7_GPGPU_DISPATCHDIMX),
  576. REG32(GEN7_GPGPU_DISPATCHDIMY),
  577. REG32(GEN7_GPGPU_DISPATCHDIMZ),
  578. REG64_IDX(RING_TIMESTAMP, BSD_RING_BASE),
  579. REG64_IDX(GEN7_SO_NUM_PRIMS_WRITTEN, 0),
  580. REG64_IDX(GEN7_SO_NUM_PRIMS_WRITTEN, 1),
  581. REG64_IDX(GEN7_SO_NUM_PRIMS_WRITTEN, 2),
  582. REG64_IDX(GEN7_SO_NUM_PRIMS_WRITTEN, 3),
  583. REG64_IDX(GEN7_SO_PRIM_STORAGE_NEEDED, 0),
  584. REG64_IDX(GEN7_SO_PRIM_STORAGE_NEEDED, 1),
  585. REG64_IDX(GEN7_SO_PRIM_STORAGE_NEEDED, 2),
  586. REG64_IDX(GEN7_SO_PRIM_STORAGE_NEEDED, 3),
  587. REG32(GEN7_SO_WRITE_OFFSET(0)),
  588. REG32(GEN7_SO_WRITE_OFFSET(1)),
  589. REG32(GEN7_SO_WRITE_OFFSET(2)),
  590. REG32(GEN7_SO_WRITE_OFFSET(3)),
  591. REG32(GEN7_L3SQCREG1),
  592. REG32(GEN7_L3CNTLREG2),
  593. REG32(GEN7_L3CNTLREG3),
  594. REG64_IDX(RING_TIMESTAMP, BLT_RING_BASE),
  595. };
  596. static const struct drm_i915_reg_descriptor hsw_render_regs[] = {
  597. REG64_IDX(HSW_CS_GPR, 0),
  598. REG64_IDX(HSW_CS_GPR, 1),
  599. REG64_IDX(HSW_CS_GPR, 2),
  600. REG64_IDX(HSW_CS_GPR, 3),
  601. REG64_IDX(HSW_CS_GPR, 4),
  602. REG64_IDX(HSW_CS_GPR, 5),
  603. REG64_IDX(HSW_CS_GPR, 6),
  604. REG64_IDX(HSW_CS_GPR, 7),
  605. REG64_IDX(HSW_CS_GPR, 8),
  606. REG64_IDX(HSW_CS_GPR, 9),
  607. REG64_IDX(HSW_CS_GPR, 10),
  608. REG64_IDX(HSW_CS_GPR, 11),
  609. REG64_IDX(HSW_CS_GPR, 12),
  610. REG64_IDX(HSW_CS_GPR, 13),
  611. REG64_IDX(HSW_CS_GPR, 14),
  612. REG64_IDX(HSW_CS_GPR, 15),
  613. REG32(HSW_SCRATCH1,
  614. .mask = ~HSW_SCRATCH1_L3_DATA_ATOMICS_DISABLE,
  615. .value = 0),
  616. REG32(HSW_ROW_CHICKEN3,
  617. .mask = ~(HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE << 16 |
  618. HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE),
  619. .value = 0),
  620. };
  621. static const struct drm_i915_reg_descriptor gen7_blt_regs[] = {
  622. REG64_IDX(RING_TIMESTAMP, RENDER_RING_BASE),
  623. REG64_IDX(RING_TIMESTAMP, BSD_RING_BASE),
  624. REG32(BCS_SWCTRL),
  625. REG64_IDX(RING_TIMESTAMP, BLT_RING_BASE),
  626. };
  627. static const struct drm_i915_reg_descriptor gen9_blt_regs[] = {
  628. REG64_IDX(RING_TIMESTAMP, RENDER_RING_BASE),
  629. REG64_IDX(RING_TIMESTAMP, BSD_RING_BASE),
  630. REG32(BCS_SWCTRL),
  631. REG64_IDX(RING_TIMESTAMP, BLT_RING_BASE),
  632. REG32_IDX(RING_CTX_TIMESTAMP, BLT_RING_BASE),
  633. REG64_IDX(BCS_GPR, 0),
  634. REG64_IDX(BCS_GPR, 1),
  635. REG64_IDX(BCS_GPR, 2),
  636. REG64_IDX(BCS_GPR, 3),
  637. REG64_IDX(BCS_GPR, 4),
  638. REG64_IDX(BCS_GPR, 5),
  639. REG64_IDX(BCS_GPR, 6),
  640. REG64_IDX(BCS_GPR, 7),
  641. REG64_IDX(BCS_GPR, 8),
  642. REG64_IDX(BCS_GPR, 9),
  643. REG64_IDX(BCS_GPR, 10),
  644. REG64_IDX(BCS_GPR, 11),
  645. REG64_IDX(BCS_GPR, 12),
  646. REG64_IDX(BCS_GPR, 13),
  647. REG64_IDX(BCS_GPR, 14),
  648. REG64_IDX(BCS_GPR, 15),
  649. };
  650. #undef REG64
  651. #undef REG32
  652. struct drm_i915_reg_table {
  653. const struct drm_i915_reg_descriptor *regs;
  654. int num_regs;
  655. };
  656. static const struct drm_i915_reg_table ivb_render_reg_tables[] = {
  657. { gen7_render_regs, ARRAY_SIZE(gen7_render_regs) },
  658. };
  659. static const struct drm_i915_reg_table ivb_blt_reg_tables[] = {
  660. { gen7_blt_regs, ARRAY_SIZE(gen7_blt_regs) },
  661. };
  662. static const struct drm_i915_reg_table hsw_render_reg_tables[] = {
  663. { gen7_render_regs, ARRAY_SIZE(gen7_render_regs) },
  664. { hsw_render_regs, ARRAY_SIZE(hsw_render_regs) },
  665. };
  666. static const struct drm_i915_reg_table hsw_blt_reg_tables[] = {
  667. { gen7_blt_regs, ARRAY_SIZE(gen7_blt_regs) },
  668. };
  669. static const struct drm_i915_reg_table gen9_blt_reg_tables[] = {
  670. { gen9_blt_regs, ARRAY_SIZE(gen9_blt_regs) },
  671. };
  672. static u32 gen7_render_get_cmd_length_mask(u32 cmd_header)
  673. {
  674. u32 client = cmd_header >> INSTR_CLIENT_SHIFT;
  675. u32 subclient =
  676. (cmd_header & INSTR_SUBCLIENT_MASK) >> INSTR_SUBCLIENT_SHIFT;
  677. if (client == INSTR_MI_CLIENT)
  678. return 0x3F;
  679. else if (client == INSTR_RC_CLIENT) {
  680. if (subclient == INSTR_MEDIA_SUBCLIENT)
  681. return 0xFFFF;
  682. else
  683. return 0xFF;
  684. }
  685. DRM_DEBUG_DRIVER("CMD: Abnormal rcs cmd length! 0x%08X\n", cmd_header);
  686. return 0;
  687. }
  688. static u32 gen7_bsd_get_cmd_length_mask(u32 cmd_header)
  689. {
  690. u32 client = cmd_header >> INSTR_CLIENT_SHIFT;
  691. u32 subclient =
  692. (cmd_header & INSTR_SUBCLIENT_MASK) >> INSTR_SUBCLIENT_SHIFT;
  693. u32 op = (cmd_header & INSTR_26_TO_24_MASK) >> INSTR_26_TO_24_SHIFT;
  694. if (client == INSTR_MI_CLIENT)
  695. return 0x3F;
  696. else if (client == INSTR_RC_CLIENT) {
  697. if (subclient == INSTR_MEDIA_SUBCLIENT) {
  698. if (op == 6)
  699. return 0xFFFF;
  700. else
  701. return 0xFFF;
  702. } else
  703. return 0xFF;
  704. }
  705. DRM_DEBUG_DRIVER("CMD: Abnormal bsd cmd length! 0x%08X\n", cmd_header);
  706. return 0;
  707. }
  708. static u32 gen7_blt_get_cmd_length_mask(u32 cmd_header)
  709. {
  710. u32 client = cmd_header >> INSTR_CLIENT_SHIFT;
  711. if (client == INSTR_MI_CLIENT)
  712. return 0x3F;
  713. else if (client == INSTR_BC_CLIENT)
  714. return 0xFF;
  715. DRM_DEBUG_DRIVER("CMD: Abnormal blt cmd length! 0x%08X\n", cmd_header);
  716. return 0;
  717. }
  718. static u32 gen9_blt_get_cmd_length_mask(u32 cmd_header)
  719. {
  720. u32 client = cmd_header >> INSTR_CLIENT_SHIFT;
  721. if (client == INSTR_MI_CLIENT || client == INSTR_BC_CLIENT)
  722. return 0xFF;
  723. DRM_DEBUG_DRIVER("CMD: Abnormal blt cmd length! 0x%08X\n", cmd_header);
  724. return 0;
  725. }
  726. static bool validate_cmds_sorted(const struct intel_engine_cs *engine,
  727. const struct drm_i915_cmd_table *cmd_tables,
  728. int cmd_table_count)
  729. {
  730. int i;
  731. bool ret = true;
  732. if (!cmd_tables || cmd_table_count == 0)
  733. return true;
  734. for (i = 0; i < cmd_table_count; i++) {
  735. const struct drm_i915_cmd_table *table = &cmd_tables[i];
  736. u32 previous = 0;
  737. int j;
  738. for (j = 0; j < table->count; j++) {
  739. const struct drm_i915_cmd_descriptor *desc =
  740. &table->table[j];
  741. u32 curr = desc->cmd.value & desc->cmd.mask;
  742. if (curr < previous) {
  743. DRM_ERROR("CMD: %s [%d] command table not sorted: "
  744. "table=%d entry=%d cmd=0x%08X prev=0x%08X\n",
  745. engine->name, engine->id,
  746. i, j, curr, previous);
  747. ret = false;
  748. }
  749. previous = curr;
  750. }
  751. }
  752. return ret;
  753. }
  754. static bool check_sorted(const struct intel_engine_cs *engine,
  755. const struct drm_i915_reg_descriptor *reg_table,
  756. int reg_count)
  757. {
  758. int i;
  759. u32 previous = 0;
  760. bool ret = true;
  761. for (i = 0; i < reg_count; i++) {
  762. u32 curr = i915_mmio_reg_offset(reg_table[i].addr);
  763. if (curr < previous) {
  764. DRM_ERROR("CMD: %s [%d] register table not sorted: "
  765. "entry=%d reg=0x%08X prev=0x%08X\n",
  766. engine->name, engine->id,
  767. i, curr, previous);
  768. ret = false;
  769. }
  770. previous = curr;
  771. }
  772. return ret;
  773. }
  774. static bool validate_regs_sorted(struct intel_engine_cs *engine)
  775. {
  776. int i;
  777. const struct drm_i915_reg_table *table;
  778. for (i = 0; i < engine->reg_table_count; i++) {
  779. table = &engine->reg_tables[i];
  780. if (!check_sorted(engine, table->regs, table->num_regs))
  781. return false;
  782. }
  783. return true;
  784. }
  785. struct cmd_node {
  786. const struct drm_i915_cmd_descriptor *desc;
  787. struct hlist_node node;
  788. };
  789. /*
  790. * Different command ranges have different numbers of bits for the opcode. For
  791. * example, MI commands use bits 31:23 while 3D commands use bits 31:16. The
  792. * problem is that, for example, MI commands use bits 22:16 for other fields
  793. * such as GGTT vs PPGTT bits. If we include those bits in the mask then when
  794. * we mask a command from a batch it could hash to the wrong bucket due to
  795. * non-opcode bits being set. But if we don't include those bits, some 3D
  796. * commands may hash to the same bucket due to not including opcode bits that
  797. * make the command unique. For now, we will risk hashing to the same bucket.
  798. */
  799. static inline u32 cmd_header_key(u32 x)
  800. {
  801. switch (x >> INSTR_CLIENT_SHIFT) {
  802. default:
  803. case INSTR_MI_CLIENT:
  804. return x >> STD_MI_OPCODE_SHIFT;
  805. case INSTR_RC_CLIENT:
  806. return x >> STD_3D_OPCODE_SHIFT;
  807. case INSTR_BC_CLIENT:
  808. return x >> STD_2D_OPCODE_SHIFT;
  809. }
  810. }
  811. static int init_hash_table(struct intel_engine_cs *engine,
  812. const struct drm_i915_cmd_table *cmd_tables,
  813. int cmd_table_count)
  814. {
  815. int i, j;
  816. hash_init(engine->cmd_hash);
  817. for (i = 0; i < cmd_table_count; i++) {
  818. const struct drm_i915_cmd_table *table = &cmd_tables[i];
  819. for (j = 0; j < table->count; j++) {
  820. const struct drm_i915_cmd_descriptor *desc =
  821. &table->table[j];
  822. struct cmd_node *desc_node =
  823. kmalloc(sizeof(*desc_node), GFP_KERNEL);
  824. if (!desc_node)
  825. return -ENOMEM;
  826. desc_node->desc = desc;
  827. hash_add(engine->cmd_hash, &desc_node->node,
  828. cmd_header_key(desc->cmd.value));
  829. }
  830. }
  831. return 0;
  832. }
  833. static void fini_hash_table(struct intel_engine_cs *engine)
  834. {
  835. struct hlist_node *tmp;
  836. struct cmd_node *desc_node;
  837. int i;
  838. hash_for_each_safe(engine->cmd_hash, i, tmp, desc_node, node) {
  839. hash_del(&desc_node->node);
  840. kfree(desc_node);
  841. }
  842. }
  843. /**
  844. * intel_engine_init_cmd_parser() - set cmd parser related fields for an engine
  845. * @engine: the engine to initialize
  846. *
  847. * Optionally initializes fields related to batch buffer command parsing in the
  848. * struct intel_engine_cs based on whether the platform requires software
  849. * command parsing.
  850. */
  851. void intel_engine_init_cmd_parser(struct intel_engine_cs *engine)
  852. {
  853. const struct drm_i915_cmd_table *cmd_tables;
  854. int cmd_table_count;
  855. int ret;
  856. if (!IS_GEN7(engine->i915) && !(IS_GEN9(engine->i915) &&
  857. engine->id == BCS))
  858. return;
  859. switch (engine->id) {
  860. case RCS:
  861. if (IS_HASWELL(engine->i915)) {
  862. cmd_tables = hsw_render_ring_cmd_table;
  863. cmd_table_count =
  864. ARRAY_SIZE(hsw_render_ring_cmd_table);
  865. } else {
  866. cmd_tables = gen7_render_cmd_table;
  867. cmd_table_count = ARRAY_SIZE(gen7_render_cmd_table);
  868. }
  869. if (IS_HASWELL(engine->i915)) {
  870. engine->reg_tables = hsw_render_reg_tables;
  871. engine->reg_table_count = ARRAY_SIZE(hsw_render_reg_tables);
  872. } else {
  873. engine->reg_tables = ivb_render_reg_tables;
  874. engine->reg_table_count = ARRAY_SIZE(ivb_render_reg_tables);
  875. }
  876. engine->get_cmd_length_mask = gen7_render_get_cmd_length_mask;
  877. break;
  878. case VCS:
  879. cmd_tables = gen7_video_cmd_table;
  880. cmd_table_count = ARRAY_SIZE(gen7_video_cmd_table);
  881. engine->get_cmd_length_mask = gen7_bsd_get_cmd_length_mask;
  882. break;
  883. case BCS:
  884. engine->get_cmd_length_mask = gen7_blt_get_cmd_length_mask;
  885. if (IS_GEN9(engine->i915)) {
  886. cmd_tables = gen9_blt_cmd_table;
  887. cmd_table_count = ARRAY_SIZE(gen9_blt_cmd_table);
  888. engine->get_cmd_length_mask =
  889. gen9_blt_get_cmd_length_mask;
  890. /* BCS Engine unsafe without parser */
  891. engine->flags |= I915_ENGINE_REQUIRES_CMD_PARSER;
  892. } else if (IS_HASWELL(engine->i915)) {
  893. cmd_tables = hsw_blt_ring_cmd_table;
  894. cmd_table_count = ARRAY_SIZE(hsw_blt_ring_cmd_table);
  895. } else {
  896. cmd_tables = gen7_blt_cmd_table;
  897. cmd_table_count = ARRAY_SIZE(gen7_blt_cmd_table);
  898. }
  899. if (IS_GEN9(engine->i915)) {
  900. engine->reg_tables = gen9_blt_reg_tables;
  901. engine->reg_table_count =
  902. ARRAY_SIZE(gen9_blt_reg_tables);
  903. } else if (IS_HASWELL(engine->i915)) {
  904. engine->reg_tables = hsw_blt_reg_tables;
  905. engine->reg_table_count = ARRAY_SIZE(hsw_blt_reg_tables);
  906. } else {
  907. engine->reg_tables = ivb_blt_reg_tables;
  908. engine->reg_table_count = ARRAY_SIZE(ivb_blt_reg_tables);
  909. }
  910. break;
  911. case VECS:
  912. cmd_tables = hsw_vebox_cmd_table;
  913. cmd_table_count = ARRAY_SIZE(hsw_vebox_cmd_table);
  914. /* VECS can use the same length_mask function as VCS */
  915. engine->get_cmd_length_mask = gen7_bsd_get_cmd_length_mask;
  916. break;
  917. default:
  918. MISSING_CASE(engine->id);
  919. return;
  920. }
  921. if (!validate_cmds_sorted(engine, cmd_tables, cmd_table_count)) {
  922. DRM_ERROR("%s: command descriptions are not sorted\n",
  923. engine->name);
  924. return;
  925. }
  926. if (!validate_regs_sorted(engine)) {
  927. DRM_ERROR("%s: registers are not sorted\n", engine->name);
  928. return;
  929. }
  930. ret = init_hash_table(engine, cmd_tables, cmd_table_count);
  931. if (ret) {
  932. DRM_ERROR("%s: initialised failed!\n", engine->name);
  933. fini_hash_table(engine);
  934. return;
  935. }
  936. engine->flags |= I915_ENGINE_USING_CMD_PARSER;
  937. }
  938. /**
  939. * intel_engine_cleanup_cmd_parser() - clean up cmd parser related fields
  940. * @engine: the engine to clean up
  941. *
  942. * Releases any resources related to command parsing that may have been
  943. * initialized for the specified engine.
  944. */
  945. void intel_engine_cleanup_cmd_parser(struct intel_engine_cs *engine)
  946. {
  947. if (!intel_engine_using_cmd_parser(engine))
  948. return;
  949. fini_hash_table(engine);
  950. }
  951. static const struct drm_i915_cmd_descriptor*
  952. find_cmd_in_table(struct intel_engine_cs *engine,
  953. u32 cmd_header)
  954. {
  955. struct cmd_node *desc_node;
  956. hash_for_each_possible(engine->cmd_hash, desc_node, node,
  957. cmd_header_key(cmd_header)) {
  958. const struct drm_i915_cmd_descriptor *desc = desc_node->desc;
  959. if (((cmd_header ^ desc->cmd.value) & desc->cmd.mask) == 0)
  960. return desc;
  961. }
  962. return NULL;
  963. }
  964. /*
  965. * Returns a pointer to a descriptor for the command specified by cmd_header.
  966. *
  967. * The caller must supply space for a default descriptor via the default_desc
  968. * parameter. If no descriptor for the specified command exists in the engine's
  969. * command parser tables, this function fills in default_desc based on the
  970. * engine's default length encoding and returns default_desc.
  971. */
  972. static const struct drm_i915_cmd_descriptor*
  973. find_cmd(struct intel_engine_cs *engine,
  974. u32 cmd_header,
  975. const struct drm_i915_cmd_descriptor *desc,
  976. struct drm_i915_cmd_descriptor *default_desc)
  977. {
  978. u32 mask;
  979. if (((cmd_header ^ desc->cmd.value) & desc->cmd.mask) == 0)
  980. return desc;
  981. desc = find_cmd_in_table(engine, cmd_header);
  982. if (desc)
  983. return desc;
  984. mask = engine->get_cmd_length_mask(cmd_header);
  985. if (!mask)
  986. return NULL;
  987. default_desc->cmd.value = cmd_header;
  988. default_desc->cmd.mask = ~0u << MIN_OPCODE_SHIFT;
  989. default_desc->length.mask = mask;
  990. default_desc->flags = CMD_DESC_SKIP;
  991. return default_desc;
  992. }
  993. static const struct drm_i915_reg_descriptor *
  994. __find_reg(const struct drm_i915_reg_descriptor *table, int count, u32 addr)
  995. {
  996. int start = 0, end = count;
  997. while (start < end) {
  998. int mid = start + (end - start) / 2;
  999. int ret = addr - i915_mmio_reg_offset(table[mid].addr);
  1000. if (ret < 0)
  1001. end = mid;
  1002. else if (ret > 0)
  1003. start = mid + 1;
  1004. else
  1005. return &table[mid];
  1006. }
  1007. return NULL;
  1008. }
  1009. static const struct drm_i915_reg_descriptor *
  1010. find_reg(const struct intel_engine_cs *engine, u32 addr)
  1011. {
  1012. const struct drm_i915_reg_table *table = engine->reg_tables;
  1013. const struct drm_i915_reg_descriptor *reg = NULL;
  1014. int count = engine->reg_table_count;
  1015. for (; !reg && (count > 0); ++table, --count)
  1016. reg = __find_reg(table->regs, table->num_regs, addr);
  1017. return reg;
  1018. }
  1019. /* Returns a vmap'd pointer to dst_obj, which the caller must unmap */
  1020. static u32 *copy_batch(struct drm_i915_gem_object *dst_obj,
  1021. struct drm_i915_gem_object *src_obj,
  1022. u32 batch_start_offset,
  1023. u32 batch_len,
  1024. bool *needs_clflush_after)
  1025. {
  1026. unsigned int src_needs_clflush;
  1027. unsigned int dst_needs_clflush;
  1028. void *dst, *src;
  1029. int ret;
  1030. ret = i915_gem_obj_prepare_shmem_read(src_obj, &src_needs_clflush);
  1031. if (ret)
  1032. return ERR_PTR(ret);
  1033. ret = i915_gem_obj_prepare_shmem_write(dst_obj, &dst_needs_clflush);
  1034. if (ret) {
  1035. dst = ERR_PTR(ret);
  1036. goto unpin_src;
  1037. }
  1038. dst = i915_gem_object_pin_map(dst_obj, I915_MAP_FORCE_WB);
  1039. if (IS_ERR(dst))
  1040. goto unpin_dst;
  1041. src = ERR_PTR(-ENODEV);
  1042. if (src_needs_clflush &&
  1043. i915_can_memcpy_from_wc(NULL, batch_start_offset, 0)) {
  1044. src = i915_gem_object_pin_map(src_obj, I915_MAP_WC);
  1045. if (!IS_ERR(src)) {
  1046. i915_memcpy_from_wc(dst,
  1047. src + batch_start_offset,
  1048. ALIGN(batch_len, 16));
  1049. i915_gem_object_unpin_map(src_obj);
  1050. }
  1051. }
  1052. if (IS_ERR(src)) {
  1053. void *ptr;
  1054. int offset, n;
  1055. offset = offset_in_page(batch_start_offset);
  1056. /* We can avoid clflushing partial cachelines before the write
  1057. * if we only every write full cache-lines. Since we know that
  1058. * both the source and destination are in multiples of
  1059. * PAGE_SIZE, we can simply round up to the next cacheline.
  1060. * We don't care about copying too much here as we only
  1061. * validate up to the end of the batch.
  1062. */
  1063. if (dst_needs_clflush & CLFLUSH_BEFORE)
  1064. batch_len = roundup(batch_len,
  1065. boot_cpu_data.x86_clflush_size);
  1066. ptr = dst;
  1067. for (n = batch_start_offset >> PAGE_SHIFT; batch_len; n++) {
  1068. int len = min_t(int, batch_len, PAGE_SIZE - offset);
  1069. src = kmap_atomic(i915_gem_object_get_page(src_obj, n));
  1070. if (src_needs_clflush)
  1071. drm_clflush_virt_range(src + offset, len);
  1072. memcpy(ptr, src + offset, len);
  1073. kunmap_atomic(src);
  1074. ptr += len;
  1075. batch_len -= len;
  1076. offset = 0;
  1077. }
  1078. }
  1079. /* dst_obj is returned with vmap pinned */
  1080. *needs_clflush_after = dst_needs_clflush & CLFLUSH_AFTER;
  1081. unpin_dst:
  1082. i915_gem_obj_finish_shmem_access(dst_obj);
  1083. unpin_src:
  1084. i915_gem_obj_finish_shmem_access(src_obj);
  1085. return dst;
  1086. }
  1087. static bool check_cmd(const struct intel_engine_cs *engine,
  1088. const struct drm_i915_cmd_descriptor *desc,
  1089. const u32 *cmd, u32 length)
  1090. {
  1091. if (desc->flags & CMD_DESC_SKIP)
  1092. return true;
  1093. if (desc->flags & CMD_DESC_REJECT) {
  1094. DRM_DEBUG_DRIVER("CMD: Rejected command: 0x%08X\n", *cmd);
  1095. return false;
  1096. }
  1097. if (desc->flags & CMD_DESC_REGISTER) {
  1098. /*
  1099. * Get the distance between individual register offset
  1100. * fields if the command can perform more than one
  1101. * access at a time.
  1102. */
  1103. const u32 step = desc->reg.step ? desc->reg.step : length;
  1104. u32 offset;
  1105. for (offset = desc->reg.offset; offset < length;
  1106. offset += step) {
  1107. const u32 reg_addr = cmd[offset] & desc->reg.mask;
  1108. const struct drm_i915_reg_descriptor *reg =
  1109. find_reg(engine, reg_addr);
  1110. if (!reg) {
  1111. DRM_DEBUG_DRIVER("CMD: Rejected register 0x%08X in command: 0x%08X (%s)\n",
  1112. reg_addr, *cmd, engine->name);
  1113. return false;
  1114. }
  1115. /*
  1116. * Check the value written to the register against the
  1117. * allowed mask/value pair given in the whitelist entry.
  1118. */
  1119. if (reg->mask) {
  1120. if (desc->cmd.value == MI_LOAD_REGISTER_MEM) {
  1121. DRM_DEBUG_DRIVER("CMD: Rejected LRM to masked register 0x%08X\n",
  1122. reg_addr);
  1123. return false;
  1124. }
  1125. if (desc->cmd.value == MI_LOAD_REGISTER_REG) {
  1126. DRM_DEBUG_DRIVER("CMD: Rejected LRR to masked register 0x%08X\n",
  1127. reg_addr);
  1128. return false;
  1129. }
  1130. if (desc->cmd.value == MI_LOAD_REGISTER_IMM(1) &&
  1131. (offset + 2 > length ||
  1132. (cmd[offset + 1] & reg->mask) != reg->value)) {
  1133. DRM_DEBUG_DRIVER("CMD: Rejected LRI to masked register 0x%08X\n",
  1134. reg_addr);
  1135. return false;
  1136. }
  1137. }
  1138. }
  1139. }
  1140. if (desc->flags & CMD_DESC_BITMASK) {
  1141. int i;
  1142. for (i = 0; i < MAX_CMD_DESC_BITMASKS; i++) {
  1143. u32 dword;
  1144. if (desc->bits[i].mask == 0)
  1145. break;
  1146. if (desc->bits[i].condition_mask != 0) {
  1147. u32 offset =
  1148. desc->bits[i].condition_offset;
  1149. u32 condition = cmd[offset] &
  1150. desc->bits[i].condition_mask;
  1151. if (condition == 0)
  1152. continue;
  1153. }
  1154. if (desc->bits[i].offset >= length) {
  1155. DRM_DEBUG_DRIVER("CMD: Rejected command 0x%08X, too short to check bitmask (%s)\n",
  1156. *cmd, engine->name);
  1157. return false;
  1158. }
  1159. dword = cmd[desc->bits[i].offset] &
  1160. desc->bits[i].mask;
  1161. if (dword != desc->bits[i].expected) {
  1162. DRM_DEBUG_DRIVER("CMD: Rejected command 0x%08X for bitmask 0x%08X (exp=0x%08X act=0x%08X) (%s)\n",
  1163. *cmd,
  1164. desc->bits[i].mask,
  1165. desc->bits[i].expected,
  1166. dword, engine->name);
  1167. return false;
  1168. }
  1169. }
  1170. }
  1171. return true;
  1172. }
  1173. static int check_bbstart(const struct i915_gem_context *ctx,
  1174. u32 *cmd, u32 offset, u32 length,
  1175. u32 batch_len,
  1176. u64 batch_start,
  1177. u64 shadow_batch_start)
  1178. {
  1179. u64 jump_offset, jump_target;
  1180. u32 target_cmd_offset, target_cmd_index;
  1181. /* For igt compatibility on older platforms */
  1182. if (CMDPARSER_USES_GGTT(ctx->i915)) {
  1183. DRM_DEBUG("CMD: Rejecting BB_START for ggtt based submission\n");
  1184. return -EACCES;
  1185. }
  1186. if (length != 3) {
  1187. DRM_DEBUG("CMD: Recursive BB_START with bad length(%u)\n",
  1188. length);
  1189. return -EINVAL;
  1190. }
  1191. jump_target = *(u64*)(cmd+1);
  1192. jump_offset = jump_target - batch_start;
  1193. /*
  1194. * Any underflow of jump_target is guaranteed to be outside the range
  1195. * of a u32, so >= test catches both too large and too small
  1196. */
  1197. if (jump_offset >= batch_len) {
  1198. DRM_DEBUG("CMD: BB_START to 0x%llx jumps out of BB\n",
  1199. jump_target);
  1200. return -EINVAL;
  1201. }
  1202. /*
  1203. * This cannot overflow a u32 because we already checked jump_offset
  1204. * is within the BB, and the batch_len is a u32
  1205. */
  1206. target_cmd_offset = lower_32_bits(jump_offset);
  1207. target_cmd_index = target_cmd_offset / sizeof(u32);
  1208. *(u64*)(cmd + 1) = shadow_batch_start + target_cmd_offset;
  1209. if (target_cmd_index == offset)
  1210. return 0;
  1211. if (ctx->jump_whitelist_cmds <= target_cmd_index) {
  1212. DRM_DEBUG("CMD: Rejecting BB_START - truncated whitelist array\n");
  1213. return -EINVAL;
  1214. } else if (!test_bit(target_cmd_index, ctx->jump_whitelist)) {
  1215. DRM_DEBUG("CMD: BB_START to 0x%llx not a previously executed cmd\n",
  1216. jump_target);
  1217. return -EINVAL;
  1218. }
  1219. return 0;
  1220. }
  1221. static void init_whitelist(struct i915_gem_context *ctx, u32 batch_len)
  1222. {
  1223. const u32 batch_cmds = DIV_ROUND_UP(batch_len, sizeof(u32));
  1224. const u32 exact_size = BITS_TO_LONGS(batch_cmds);
  1225. u32 next_size = BITS_TO_LONGS(roundup_pow_of_two(batch_cmds));
  1226. unsigned long *next_whitelist;
  1227. if (CMDPARSER_USES_GGTT(ctx->i915))
  1228. return;
  1229. if (batch_cmds <= ctx->jump_whitelist_cmds) {
  1230. bitmap_zero(ctx->jump_whitelist, batch_cmds);
  1231. return;
  1232. }
  1233. again:
  1234. next_whitelist = kcalloc(next_size, sizeof(long), GFP_KERNEL);
  1235. if (next_whitelist) {
  1236. kfree(ctx->jump_whitelist);
  1237. ctx->jump_whitelist = next_whitelist;
  1238. ctx->jump_whitelist_cmds =
  1239. next_size * BITS_PER_BYTE * sizeof(long);
  1240. return;
  1241. }
  1242. if (next_size > exact_size) {
  1243. next_size = exact_size;
  1244. goto again;
  1245. }
  1246. DRM_DEBUG("CMD: Failed to extend whitelist. BB_START may be disallowed\n");
  1247. bitmap_zero(ctx->jump_whitelist, ctx->jump_whitelist_cmds);
  1248. return;
  1249. }
  1250. #define LENGTH_BIAS 2
  1251. /**
  1252. * i915_parse_cmds() - parse a submitted batch buffer for privilege violations
  1253. * @ctx: the context in which the batch is to execute
  1254. * @engine: the engine on which the batch is to execute
  1255. * @batch_obj: the batch buffer in question
  1256. * @batch_start: Canonical base address of batch
  1257. * @batch_start_offset: byte offset in the batch at which execution starts
  1258. * @batch_len: length of the commands in batch_obj
  1259. * @shadow_batch_obj: copy of the batch buffer in question
  1260. * @shadow_batch_start: Canonical base address of shadow_batch_obj
  1261. *
  1262. * Parses the specified batch buffer looking for privilege violations as
  1263. * described in the overview.
  1264. *
  1265. * Return: non-zero if the parser finds violations or otherwise fails; -EACCES
  1266. * if the batch appears legal but should use hardware parsing
  1267. */
  1268. int intel_engine_cmd_parser(struct i915_gem_context *ctx,
  1269. struct intel_engine_cs *engine,
  1270. struct drm_i915_gem_object *batch_obj,
  1271. u64 batch_start,
  1272. u32 batch_start_offset,
  1273. u32 batch_len,
  1274. struct drm_i915_gem_object *shadow_batch_obj,
  1275. u64 shadow_batch_start)
  1276. {
  1277. u32 *cmd, *batch_end, offset = 0;
  1278. struct drm_i915_cmd_descriptor default_desc = noop_desc;
  1279. const struct drm_i915_cmd_descriptor *desc = &default_desc;
  1280. bool needs_clflush_after = false;
  1281. int ret = 0;
  1282. cmd = copy_batch(shadow_batch_obj, batch_obj,
  1283. batch_start_offset, batch_len,
  1284. &needs_clflush_after);
  1285. if (IS_ERR(cmd)) {
  1286. DRM_DEBUG_DRIVER("CMD: Failed to copy batch\n");
  1287. return PTR_ERR(cmd);
  1288. }
  1289. init_whitelist(ctx, batch_len);
  1290. /*
  1291. * We use the batch length as size because the shadow object is as
  1292. * large or larger and copy_batch() will write MI_NOPs to the extra
  1293. * space. Parsing should be faster in some cases this way.
  1294. */
  1295. batch_end = cmd + (batch_len / sizeof(*batch_end));
  1296. do {
  1297. u32 length;
  1298. if (*cmd == MI_BATCH_BUFFER_END)
  1299. break;
  1300. desc = find_cmd(engine, *cmd, desc, &default_desc);
  1301. if (!desc) {
  1302. DRM_DEBUG_DRIVER("CMD: Unrecognized command: 0x%08X\n",
  1303. *cmd);
  1304. ret = -EINVAL;
  1305. goto err;
  1306. }
  1307. if (desc->flags & CMD_DESC_FIXED)
  1308. length = desc->length.fixed;
  1309. else
  1310. length = ((*cmd & desc->length.mask) + LENGTH_BIAS);
  1311. if ((batch_end - cmd) < length) {
  1312. DRM_DEBUG_DRIVER("CMD: Command length exceeds batch length: 0x%08X length=%u batchlen=%td\n",
  1313. *cmd,
  1314. length,
  1315. batch_end - cmd);
  1316. ret = -EINVAL;
  1317. goto err;
  1318. }
  1319. if (!check_cmd(engine, desc, cmd, length)) {
  1320. ret = -EACCES;
  1321. goto err;
  1322. }
  1323. if (desc->cmd.value == MI_BATCH_BUFFER_START) {
  1324. ret = check_bbstart(ctx, cmd, offset, length,
  1325. batch_len, batch_start,
  1326. shadow_batch_start);
  1327. if (ret)
  1328. goto err;
  1329. break;
  1330. }
  1331. if (ctx->jump_whitelist_cmds > offset)
  1332. set_bit(offset, ctx->jump_whitelist);
  1333. cmd += length;
  1334. offset += length;
  1335. if (cmd >= batch_end) {
  1336. DRM_DEBUG_DRIVER("CMD: Got to the end of the buffer w/o a BBE cmd!\n");
  1337. ret = -EINVAL;
  1338. goto err;
  1339. }
  1340. } while (1);
  1341. if (needs_clflush_after) {
  1342. void *ptr = page_mask_bits(shadow_batch_obj->mm.mapping);
  1343. drm_clflush_virt_range(ptr, (void *)(cmd + 1) - ptr);
  1344. }
  1345. err:
  1346. i915_gem_object_unpin_map(shadow_batch_obj);
  1347. return ret;
  1348. }
  1349. /**
  1350. * i915_cmd_parser_get_version() - get the cmd parser version number
  1351. * @dev_priv: i915 device private
  1352. *
  1353. * The cmd parser maintains a simple increasing integer version number suitable
  1354. * for passing to userspace clients to determine what operations are permitted.
  1355. *
  1356. * Return: the current version number of the cmd parser
  1357. */
  1358. int i915_cmd_parser_get_version(struct drm_i915_private *dev_priv)
  1359. {
  1360. struct intel_engine_cs *engine;
  1361. enum intel_engine_id id;
  1362. bool active = false;
  1363. /* If the command parser is not enabled, report 0 - unsupported */
  1364. for_each_engine(engine, dev_priv, id) {
  1365. if (intel_engine_using_cmd_parser(engine)) {
  1366. active = true;
  1367. break;
  1368. }
  1369. }
  1370. if (!active)
  1371. return 0;
  1372. /*
  1373. * Command parser version history
  1374. *
  1375. * 1. Initial version. Checks batches and reports violations, but leaves
  1376. * hardware parsing enabled (so does not allow new use cases).
  1377. * 2. Allow access to the MI_PREDICATE_SRC0 and
  1378. * MI_PREDICATE_SRC1 registers.
  1379. * 3. Allow access to the GPGPU_THREADS_DISPATCHED register.
  1380. * 4. L3 atomic chicken bits of HSW_SCRATCH1 and HSW_ROW_CHICKEN3.
  1381. * 5. GPGPU dispatch compute indirect registers.
  1382. * 6. TIMESTAMP register and Haswell CS GPR registers
  1383. * 7. Allow MI_LOAD_REGISTER_REG between whitelisted registers.
  1384. * 8. Don't report cmd_check() failures as EINVAL errors to userspace;
  1385. * rely on the HW to NOOP disallowed commands as it would without
  1386. * the parser enabled.
  1387. * 9. Don't whitelist or handle oacontrol specially, as ownership
  1388. * for oacontrol state is moving to i915-perf.
  1389. * 10. Support for Gen9 BCS Parsing
  1390. */
  1391. return 10;
  1392. }