vmwgfx_stdu.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686
  1. /******************************************************************************
  2. *
  3. * COPYRIGHT © 2014-2015 VMware, Inc., Palo Alto, CA., USA
  4. * All Rights Reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sub license, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice (including the
  15. * next paragraph) shall be included in all copies or substantial portions
  16. * of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  21. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  22. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  23. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  24. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. *
  26. ******************************************************************************/
  27. #include "vmwgfx_kms.h"
  28. #include "device_include/svga3d_surfacedefs.h"
  29. #include <drm/drm_plane_helper.h>
  30. #include <drm/drm_atomic.h>
  31. #include <drm/drm_atomic_helper.h>
  32. #define vmw_crtc_to_stdu(x) \
  33. container_of(x, struct vmw_screen_target_display_unit, base.crtc)
  34. #define vmw_encoder_to_stdu(x) \
  35. container_of(x, struct vmw_screen_target_display_unit, base.encoder)
  36. #define vmw_connector_to_stdu(x) \
  37. container_of(x, struct vmw_screen_target_display_unit, base.connector)
  38. enum stdu_content_type {
  39. SAME_AS_DISPLAY = 0,
  40. SEPARATE_SURFACE,
  41. SEPARATE_DMA
  42. };
  43. /**
  44. * struct vmw_stdu_dirty - closure structure for the update functions
  45. *
  46. * @base: The base type we derive from. Used by vmw_kms_helper_dirty().
  47. * @transfer: Transfer direction for DMA command.
  48. * @left: Left side of bounding box.
  49. * @right: Right side of bounding box.
  50. * @top: Top side of bounding box.
  51. * @bottom: Bottom side of bounding box.
  52. * @fb_left: Left side of the framebuffer/content bounding box
  53. * @fb_top: Top of the framebuffer/content bounding box
  54. * @buf: DMA buffer when DMA-ing between buffer and screen targets.
  55. * @sid: Surface ID when copying between surface and screen targets.
  56. */
  57. struct vmw_stdu_dirty {
  58. struct vmw_kms_dirty base;
  59. SVGA3dTransferType transfer;
  60. s32 left, right, top, bottom;
  61. s32 fb_left, fb_top;
  62. u32 pitch;
  63. union {
  64. struct vmw_dma_buffer *buf;
  65. u32 sid;
  66. };
  67. };
  68. /*
  69. * SVGA commands that are used by this code. Please see the device headers
  70. * for explanation.
  71. */
  72. struct vmw_stdu_update {
  73. SVGA3dCmdHeader header;
  74. SVGA3dCmdUpdateGBScreenTarget body;
  75. };
  76. struct vmw_stdu_dma {
  77. SVGA3dCmdHeader header;
  78. SVGA3dCmdSurfaceDMA body;
  79. };
  80. struct vmw_stdu_surface_copy {
  81. SVGA3dCmdHeader header;
  82. SVGA3dCmdSurfaceCopy body;
  83. };
  84. /**
  85. * struct vmw_screen_target_display_unit
  86. *
  87. * @base: VMW specific DU structure
  88. * @display_srf: surface to be displayed. The dimension of this will always
  89. * match the display mode. If the display mode matches
  90. * content_vfbs dimensions, then this is a pointer into the
  91. * corresponding field in content_vfbs. If not, then this
  92. * is a separate buffer to which content_vfbs will blit to.
  93. * @content_type: content_fb type
  94. * @defined: true if the current display unit has been initialized
  95. */
  96. struct vmw_screen_target_display_unit {
  97. struct vmw_display_unit base;
  98. const struct vmw_surface *display_srf;
  99. enum stdu_content_type content_fb_type;
  100. s32 display_width, display_height;
  101. bool defined;
  102. /* For CPU Blit */
  103. struct ttm_bo_kmap_obj host_map, guest_map;
  104. unsigned int cpp;
  105. };
  106. static void vmw_stdu_destroy(struct vmw_screen_target_display_unit *stdu);
  107. /******************************************************************************
  108. * Screen Target Display Unit CRTC Functions
  109. *****************************************************************************/
  110. /**
  111. * vmw_stdu_crtc_destroy - cleans up the STDU
  112. *
  113. * @crtc: used to get a reference to the containing STDU
  114. */
  115. static void vmw_stdu_crtc_destroy(struct drm_crtc *crtc)
  116. {
  117. vmw_stdu_destroy(vmw_crtc_to_stdu(crtc));
  118. }
  119. /**
  120. * vmw_stdu_define_st - Defines a Screen Target
  121. *
  122. * @dev_priv: VMW DRM device
  123. * @stdu: display unit to create a Screen Target for
  124. * @mode: The mode to set.
  125. * @crtc_x: X coordinate of screen target relative to framebuffer origin.
  126. * @crtc_y: Y coordinate of screen target relative to framebuffer origin.
  127. *
  128. * Creates a STDU that we can used later. This function is called whenever the
  129. * framebuffer size changes.
  130. *
  131. * RETURNs:
  132. * 0 on success, error code on failure
  133. */
  134. static int vmw_stdu_define_st(struct vmw_private *dev_priv,
  135. struct vmw_screen_target_display_unit *stdu,
  136. struct drm_display_mode *mode,
  137. int crtc_x, int crtc_y)
  138. {
  139. struct {
  140. SVGA3dCmdHeader header;
  141. SVGA3dCmdDefineGBScreenTarget body;
  142. } *cmd;
  143. cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
  144. if (unlikely(cmd == NULL)) {
  145. DRM_ERROR("Out of FIFO space defining Screen Target\n");
  146. return -ENOMEM;
  147. }
  148. cmd->header.id = SVGA_3D_CMD_DEFINE_GB_SCREENTARGET;
  149. cmd->header.size = sizeof(cmd->body);
  150. cmd->body.stid = stdu->base.unit;
  151. cmd->body.width = mode->hdisplay;
  152. cmd->body.height = mode->vdisplay;
  153. cmd->body.flags = (0 == cmd->body.stid) ? SVGA_STFLAG_PRIMARY : 0;
  154. cmd->body.dpi = 0;
  155. if (stdu->base.is_implicit) {
  156. cmd->body.xRoot = crtc_x;
  157. cmd->body.yRoot = crtc_y;
  158. } else {
  159. cmd->body.xRoot = stdu->base.gui_x;
  160. cmd->body.yRoot = stdu->base.gui_y;
  161. }
  162. stdu->base.set_gui_x = cmd->body.xRoot;
  163. stdu->base.set_gui_y = cmd->body.yRoot;
  164. vmw_fifo_commit(dev_priv, sizeof(*cmd));
  165. stdu->defined = true;
  166. stdu->display_width = mode->hdisplay;
  167. stdu->display_height = mode->vdisplay;
  168. return 0;
  169. }
  170. /**
  171. * vmw_stdu_bind_st - Binds a surface to a Screen Target
  172. *
  173. * @dev_priv: VMW DRM device
  174. * @stdu: display unit affected
  175. * @res: Buffer to bind to the screen target. Set to NULL to blank screen.
  176. *
  177. * Binding a surface to a Screen Target the same as flipping
  178. */
  179. static int vmw_stdu_bind_st(struct vmw_private *dev_priv,
  180. struct vmw_screen_target_display_unit *stdu,
  181. const struct vmw_resource *res)
  182. {
  183. SVGA3dSurfaceImageId image;
  184. struct {
  185. SVGA3dCmdHeader header;
  186. SVGA3dCmdBindGBScreenTarget body;
  187. } *cmd;
  188. if (!stdu->defined) {
  189. DRM_ERROR("No screen target defined\n");
  190. return -EINVAL;
  191. }
  192. /* Set up image using information in vfb */
  193. memset(&image, 0, sizeof(image));
  194. image.sid = res ? res->id : SVGA3D_INVALID_ID;
  195. cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
  196. if (unlikely(cmd == NULL)) {
  197. DRM_ERROR("Out of FIFO space binding a screen target\n");
  198. return -ENOMEM;
  199. }
  200. cmd->header.id = SVGA_3D_CMD_BIND_GB_SCREENTARGET;
  201. cmd->header.size = sizeof(cmd->body);
  202. cmd->body.stid = stdu->base.unit;
  203. cmd->body.image = image;
  204. vmw_fifo_commit(dev_priv, sizeof(*cmd));
  205. return 0;
  206. }
  207. /**
  208. * vmw_stdu_populate_update - populate an UPDATE_GB_SCREENTARGET command with a
  209. * bounding box.
  210. *
  211. * @cmd: Pointer to command stream.
  212. * @unit: Screen target unit.
  213. * @left: Left side of bounding box.
  214. * @right: Right side of bounding box.
  215. * @top: Top side of bounding box.
  216. * @bottom: Bottom side of bounding box.
  217. */
  218. static void vmw_stdu_populate_update(void *cmd, int unit,
  219. s32 left, s32 right, s32 top, s32 bottom)
  220. {
  221. struct vmw_stdu_update *update = cmd;
  222. update->header.id = SVGA_3D_CMD_UPDATE_GB_SCREENTARGET;
  223. update->header.size = sizeof(update->body);
  224. update->body.stid = unit;
  225. update->body.rect.x = left;
  226. update->body.rect.y = top;
  227. update->body.rect.w = right - left;
  228. update->body.rect.h = bottom - top;
  229. }
  230. /**
  231. * vmw_stdu_update_st - Full update of a Screen Target
  232. *
  233. * @dev_priv: VMW DRM device
  234. * @stdu: display unit affected
  235. *
  236. * This function needs to be called whenever the content of a screen
  237. * target has changed completely. Typically as a result of a backing
  238. * surface change.
  239. *
  240. * RETURNS:
  241. * 0 on success, error code on failure
  242. */
  243. static int vmw_stdu_update_st(struct vmw_private *dev_priv,
  244. struct vmw_screen_target_display_unit *stdu)
  245. {
  246. struct vmw_stdu_update *cmd;
  247. if (!stdu->defined) {
  248. DRM_ERROR("No screen target defined");
  249. return -EINVAL;
  250. }
  251. cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
  252. if (unlikely(cmd == NULL)) {
  253. DRM_ERROR("Out of FIFO space updating a Screen Target\n");
  254. return -ENOMEM;
  255. }
  256. vmw_stdu_populate_update(cmd, stdu->base.unit,
  257. 0, stdu->display_width,
  258. 0, stdu->display_height);
  259. vmw_fifo_commit(dev_priv, sizeof(*cmd));
  260. return 0;
  261. }
  262. /**
  263. * vmw_stdu_destroy_st - Destroy a Screen Target
  264. *
  265. * @dev_priv: VMW DRM device
  266. * @stdu: display unit to destroy
  267. */
  268. static int vmw_stdu_destroy_st(struct vmw_private *dev_priv,
  269. struct vmw_screen_target_display_unit *stdu)
  270. {
  271. int ret;
  272. struct {
  273. SVGA3dCmdHeader header;
  274. SVGA3dCmdDestroyGBScreenTarget body;
  275. } *cmd;
  276. /* Nothing to do if not successfully defined */
  277. if (unlikely(!stdu->defined))
  278. return 0;
  279. cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
  280. if (unlikely(cmd == NULL)) {
  281. DRM_ERROR("Out of FIFO space, screen target not destroyed\n");
  282. return -ENOMEM;
  283. }
  284. cmd->header.id = SVGA_3D_CMD_DESTROY_GB_SCREENTARGET;
  285. cmd->header.size = sizeof(cmd->body);
  286. cmd->body.stid = stdu->base.unit;
  287. vmw_fifo_commit(dev_priv, sizeof(*cmd));
  288. /* Force sync */
  289. ret = vmw_fallback_wait(dev_priv, false, true, 0, false, 3*HZ);
  290. if (unlikely(ret != 0))
  291. DRM_ERROR("Failed to sync with HW");
  292. stdu->defined = false;
  293. stdu->display_width = 0;
  294. stdu->display_height = 0;
  295. return ret;
  296. }
  297. /**
  298. * vmw_stdu_crtc_mode_set_nofb - Updates screen target size
  299. *
  300. * @crtc: CRTC associated with the screen target
  301. *
  302. * This function defines/destroys a screen target
  303. *
  304. */
  305. static void vmw_stdu_crtc_mode_set_nofb(struct drm_crtc *crtc)
  306. {
  307. struct vmw_private *dev_priv;
  308. struct vmw_screen_target_display_unit *stdu;
  309. int ret;
  310. stdu = vmw_crtc_to_stdu(crtc);
  311. dev_priv = vmw_priv(crtc->dev);
  312. if (stdu->defined) {
  313. ret = vmw_stdu_bind_st(dev_priv, stdu, NULL);
  314. if (ret)
  315. DRM_ERROR("Failed to blank CRTC\n");
  316. (void) vmw_stdu_update_st(dev_priv, stdu);
  317. ret = vmw_stdu_destroy_st(dev_priv, stdu);
  318. if (ret)
  319. DRM_ERROR("Failed to destroy Screen Target\n");
  320. stdu->content_fb_type = SAME_AS_DISPLAY;
  321. }
  322. if (!crtc->state->enable)
  323. return;
  324. vmw_svga_enable(dev_priv);
  325. ret = vmw_stdu_define_st(dev_priv, stdu, &crtc->mode, crtc->x, crtc->y);
  326. if (ret)
  327. DRM_ERROR("Failed to define Screen Target of size %dx%d\n",
  328. crtc->x, crtc->y);
  329. }
  330. static void vmw_stdu_crtc_helper_prepare(struct drm_crtc *crtc)
  331. {
  332. }
  333. static void vmw_stdu_crtc_helper_commit(struct drm_crtc *crtc)
  334. {
  335. struct vmw_private *dev_priv;
  336. struct vmw_screen_target_display_unit *stdu;
  337. struct vmw_framebuffer *vfb;
  338. struct drm_framebuffer *fb;
  339. stdu = vmw_crtc_to_stdu(crtc);
  340. dev_priv = vmw_priv(crtc->dev);
  341. fb = crtc->primary->fb;
  342. vfb = (fb) ? vmw_framebuffer_to_vfb(fb) : NULL;
  343. if (vfb)
  344. vmw_kms_add_active(dev_priv, &stdu->base, vfb);
  345. else
  346. vmw_kms_del_active(dev_priv, &stdu->base);
  347. }
  348. static void vmw_stdu_crtc_helper_disable(struct drm_crtc *crtc)
  349. {
  350. struct vmw_private *dev_priv;
  351. struct vmw_screen_target_display_unit *stdu;
  352. int ret;
  353. if (!crtc) {
  354. DRM_ERROR("CRTC is NULL\n");
  355. return;
  356. }
  357. stdu = vmw_crtc_to_stdu(crtc);
  358. dev_priv = vmw_priv(crtc->dev);
  359. if (stdu->defined) {
  360. ret = vmw_stdu_bind_st(dev_priv, stdu, NULL);
  361. if (ret)
  362. DRM_ERROR("Failed to blank CRTC\n");
  363. (void) vmw_stdu_update_st(dev_priv, stdu);
  364. ret = vmw_stdu_destroy_st(dev_priv, stdu);
  365. if (ret)
  366. DRM_ERROR("Failed to destroy Screen Target\n");
  367. stdu->content_fb_type = SAME_AS_DISPLAY;
  368. }
  369. }
  370. /**
  371. * vmw_stdu_crtc_page_flip - Binds a buffer to a screen target
  372. *
  373. * @crtc: CRTC to attach FB to
  374. * @fb: FB to attach
  375. * @event: Event to be posted. This event should've been alloced
  376. * using k[mz]alloc, and should've been completely initialized.
  377. * @page_flip_flags: Input flags.
  378. *
  379. * If the STDU uses the same display and content buffers, i.e. a true flip,
  380. * this function will replace the existing display buffer with the new content
  381. * buffer.
  382. *
  383. * If the STDU uses different display and content buffers, i.e. a blit, then
  384. * only the content buffer will be updated.
  385. *
  386. * RETURNS:
  387. * 0 on success, error code on failure
  388. */
  389. static int vmw_stdu_crtc_page_flip(struct drm_crtc *crtc,
  390. struct drm_framebuffer *new_fb,
  391. struct drm_pending_vblank_event *event,
  392. uint32_t flags,
  393. struct drm_modeset_acquire_ctx *ctx)
  394. {
  395. struct vmw_private *dev_priv = vmw_priv(crtc->dev);
  396. struct vmw_screen_target_display_unit *stdu = vmw_crtc_to_stdu(crtc);
  397. struct vmw_framebuffer *vfb = vmw_framebuffer_to_vfb(new_fb);
  398. struct drm_vmw_rect vclips;
  399. int ret;
  400. dev_priv = vmw_priv(crtc->dev);
  401. stdu = vmw_crtc_to_stdu(crtc);
  402. if (!stdu->defined || !vmw_kms_crtc_flippable(dev_priv, crtc))
  403. return -EINVAL;
  404. /*
  405. * We're always async, but the helper doesn't know how to set async
  406. * so lie to the helper. Also, the helper expects someone
  407. * to pick the event up from the crtc state, and if nobody does,
  408. * it will free it. Since we handle the event in this function,
  409. * don't hand it to the helper.
  410. */
  411. flags &= ~DRM_MODE_PAGE_FLIP_ASYNC;
  412. ret = drm_atomic_helper_page_flip(crtc, new_fb, NULL, flags, ctx);
  413. if (ret) {
  414. DRM_ERROR("Page flip error %d.\n", ret);
  415. return ret;
  416. }
  417. if (stdu->base.is_implicit)
  418. vmw_kms_update_implicit_fb(dev_priv, crtc);
  419. /*
  420. * Now that we've bound a new surface to the screen target,
  421. * update the contents.
  422. */
  423. vclips.x = crtc->x;
  424. vclips.y = crtc->y;
  425. vclips.w = crtc->mode.hdisplay;
  426. vclips.h = crtc->mode.vdisplay;
  427. if (vfb->dmabuf)
  428. ret = vmw_kms_stdu_dma(dev_priv, NULL, vfb, NULL, NULL, &vclips,
  429. 1, 1, true, false);
  430. else
  431. ret = vmw_kms_stdu_surface_dirty(dev_priv, vfb, NULL, &vclips,
  432. NULL, 0, 0, 1, 1, NULL);
  433. if (ret) {
  434. DRM_ERROR("Page flip update error %d.\n", ret);
  435. return ret;
  436. }
  437. if (event) {
  438. struct vmw_fence_obj *fence = NULL;
  439. struct drm_file *file_priv = event->base.file_priv;
  440. vmw_execbuf_fence_commands(NULL, dev_priv, &fence, NULL);
  441. if (!fence)
  442. return -ENOMEM;
  443. ret = vmw_event_fence_action_queue(file_priv, fence,
  444. &event->base,
  445. &event->event.tv_sec,
  446. &event->event.tv_usec,
  447. true);
  448. vmw_fence_obj_unreference(&fence);
  449. } else {
  450. (void) vmw_fifo_flush(dev_priv, false);
  451. }
  452. return 0;
  453. }
  454. /**
  455. * vmw_stdu_dmabuf_clip - Callback to encode a suface DMA command cliprect
  456. *
  457. * @dirty: The closure structure.
  458. *
  459. * Encodes a surface DMA command cliprect and updates the bounding box
  460. * for the DMA.
  461. */
  462. static void vmw_stdu_dmabuf_clip(struct vmw_kms_dirty *dirty)
  463. {
  464. struct vmw_stdu_dirty *ddirty =
  465. container_of(dirty, struct vmw_stdu_dirty, base);
  466. struct vmw_stdu_dma *cmd = dirty->cmd;
  467. struct SVGA3dCopyBox *blit = (struct SVGA3dCopyBox *) &cmd[1];
  468. blit += dirty->num_hits;
  469. blit->srcx = dirty->fb_x;
  470. blit->srcy = dirty->fb_y;
  471. blit->x = dirty->unit_x1;
  472. blit->y = dirty->unit_y1;
  473. blit->d = 1;
  474. blit->w = dirty->unit_x2 - dirty->unit_x1;
  475. blit->h = dirty->unit_y2 - dirty->unit_y1;
  476. dirty->num_hits++;
  477. if (ddirty->transfer != SVGA3D_WRITE_HOST_VRAM)
  478. return;
  479. /* Destination bounding box */
  480. ddirty->left = min_t(s32, ddirty->left, dirty->unit_x1);
  481. ddirty->top = min_t(s32, ddirty->top, dirty->unit_y1);
  482. ddirty->right = max_t(s32, ddirty->right, dirty->unit_x2);
  483. ddirty->bottom = max_t(s32, ddirty->bottom, dirty->unit_y2);
  484. }
  485. /**
  486. * vmw_stdu_dmabuf_fifo_commit - Callback to fill in and submit a DMA command.
  487. *
  488. * @dirty: The closure structure.
  489. *
  490. * Fills in the missing fields in a DMA command, and optionally encodes
  491. * a screen target update command, depending on transfer direction.
  492. */
  493. static void vmw_stdu_dmabuf_fifo_commit(struct vmw_kms_dirty *dirty)
  494. {
  495. struct vmw_stdu_dirty *ddirty =
  496. container_of(dirty, struct vmw_stdu_dirty, base);
  497. struct vmw_screen_target_display_unit *stdu =
  498. container_of(dirty->unit, typeof(*stdu), base);
  499. struct vmw_stdu_dma *cmd = dirty->cmd;
  500. struct SVGA3dCopyBox *blit = (struct SVGA3dCopyBox *) &cmd[1];
  501. SVGA3dCmdSurfaceDMASuffix *suffix =
  502. (SVGA3dCmdSurfaceDMASuffix *) &blit[dirty->num_hits];
  503. size_t blit_size = sizeof(*blit) * dirty->num_hits + sizeof(*suffix);
  504. if (!dirty->num_hits) {
  505. vmw_fifo_commit(dirty->dev_priv, 0);
  506. return;
  507. }
  508. cmd->header.id = SVGA_3D_CMD_SURFACE_DMA;
  509. cmd->header.size = sizeof(cmd->body) + blit_size;
  510. vmw_bo_get_guest_ptr(&ddirty->buf->base, &cmd->body.guest.ptr);
  511. cmd->body.guest.pitch = ddirty->pitch;
  512. cmd->body.host.sid = stdu->display_srf->res.id;
  513. cmd->body.host.face = 0;
  514. cmd->body.host.mipmap = 0;
  515. cmd->body.transfer = ddirty->transfer;
  516. suffix->suffixSize = sizeof(*suffix);
  517. suffix->maximumOffset = ddirty->buf->base.num_pages * PAGE_SIZE;
  518. if (ddirty->transfer == SVGA3D_WRITE_HOST_VRAM) {
  519. blit_size += sizeof(struct vmw_stdu_update);
  520. vmw_stdu_populate_update(&suffix[1], stdu->base.unit,
  521. ddirty->left, ddirty->right,
  522. ddirty->top, ddirty->bottom);
  523. }
  524. vmw_fifo_commit(dirty->dev_priv, sizeof(*cmd) + blit_size);
  525. ddirty->left = ddirty->top = S32_MAX;
  526. ddirty->right = ddirty->bottom = S32_MIN;
  527. }
  528. /**
  529. * vmw_stdu_dmabuf_cpu_clip - Callback to encode a CPU blit
  530. *
  531. * @dirty: The closure structure.
  532. *
  533. * This function calculates the bounding box for all the incoming clips.
  534. */
  535. static void vmw_stdu_dmabuf_cpu_clip(struct vmw_kms_dirty *dirty)
  536. {
  537. struct vmw_stdu_dirty *ddirty =
  538. container_of(dirty, struct vmw_stdu_dirty, base);
  539. dirty->num_hits = 1;
  540. /* Calculate destination bounding box */
  541. ddirty->left = min_t(s32, ddirty->left, dirty->unit_x1);
  542. ddirty->top = min_t(s32, ddirty->top, dirty->unit_y1);
  543. ddirty->right = max_t(s32, ddirty->right, dirty->unit_x2);
  544. ddirty->bottom = max_t(s32, ddirty->bottom, dirty->unit_y2);
  545. /*
  546. * Calculate content bounding box. We only need the top-left
  547. * coordinate because width and height will be the same as the
  548. * destination bounding box above
  549. */
  550. ddirty->fb_left = min_t(s32, ddirty->fb_left, dirty->fb_x);
  551. ddirty->fb_top = min_t(s32, ddirty->fb_top, dirty->fb_y);
  552. }
  553. /**
  554. * vmw_stdu_dmabuf_cpu_commit - Callback to do a CPU blit from DMAbuf
  555. *
  556. * @dirty: The closure structure.
  557. *
  558. * For the special case when we cannot create a proxy surface in a
  559. * 2D VM, we have to do a CPU blit ourselves.
  560. */
  561. static void vmw_stdu_dmabuf_cpu_commit(struct vmw_kms_dirty *dirty)
  562. {
  563. struct vmw_stdu_dirty *ddirty =
  564. container_of(dirty, struct vmw_stdu_dirty, base);
  565. struct vmw_screen_target_display_unit *stdu =
  566. container_of(dirty->unit, typeof(*stdu), base);
  567. s32 width, height;
  568. s32 src_pitch, dst_pitch;
  569. u8 *src, *dst;
  570. bool not_used;
  571. if (!dirty->num_hits)
  572. return;
  573. width = ddirty->right - ddirty->left;
  574. height = ddirty->bottom - ddirty->top;
  575. if (width == 0 || height == 0)
  576. return;
  577. /* Assume we are blitting from Host (display_srf) to Guest (dmabuf) */
  578. src_pitch = stdu->display_srf->base_size.width * stdu->cpp;
  579. src = ttm_kmap_obj_virtual(&stdu->host_map, &not_used);
  580. src += ddirty->top * src_pitch + ddirty->left * stdu->cpp;
  581. dst_pitch = ddirty->pitch;
  582. dst = ttm_kmap_obj_virtual(&stdu->guest_map, &not_used);
  583. dst += ddirty->fb_top * dst_pitch + ddirty->fb_left * stdu->cpp;
  584. /* Figure out the real direction */
  585. if (ddirty->transfer == SVGA3D_WRITE_HOST_VRAM) {
  586. u8 *tmp;
  587. s32 tmp_pitch;
  588. tmp = src;
  589. tmp_pitch = src_pitch;
  590. src = dst;
  591. src_pitch = dst_pitch;
  592. dst = tmp;
  593. dst_pitch = tmp_pitch;
  594. }
  595. /* CPU Blit */
  596. while (height-- > 0) {
  597. memcpy(dst, src, width * stdu->cpp);
  598. dst += dst_pitch;
  599. src += src_pitch;
  600. }
  601. if (ddirty->transfer == SVGA3D_WRITE_HOST_VRAM) {
  602. struct vmw_private *dev_priv;
  603. struct vmw_stdu_update *cmd;
  604. struct drm_clip_rect region;
  605. int ret;
  606. /* We are updating the actual surface, not a proxy */
  607. region.x1 = ddirty->left;
  608. region.x2 = ddirty->right;
  609. region.y1 = ddirty->top;
  610. region.y2 = ddirty->bottom;
  611. ret = vmw_kms_update_proxy(
  612. (struct vmw_resource *) &stdu->display_srf->res,
  613. (const struct drm_clip_rect *) &region, 1, 1);
  614. if (ret)
  615. goto out_cleanup;
  616. dev_priv = vmw_priv(stdu->base.crtc.dev);
  617. cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
  618. if (!cmd) {
  619. DRM_ERROR("Cannot reserve FIFO space to update STDU");
  620. goto out_cleanup;
  621. }
  622. vmw_stdu_populate_update(cmd, stdu->base.unit,
  623. ddirty->left, ddirty->right,
  624. ddirty->top, ddirty->bottom);
  625. vmw_fifo_commit(dev_priv, sizeof(*cmd));
  626. }
  627. out_cleanup:
  628. ddirty->left = ddirty->top = ddirty->fb_left = ddirty->fb_top = S32_MAX;
  629. ddirty->right = ddirty->bottom = S32_MIN;
  630. }
  631. /**
  632. * vmw_kms_stdu_dma - Perform a DMA transfer between a dma-buffer backed
  633. * framebuffer and the screen target system.
  634. *
  635. * @dev_priv: Pointer to the device private structure.
  636. * @file_priv: Pointer to a struct drm-file identifying the caller. May be
  637. * set to NULL, but then @user_fence_rep must also be set to NULL.
  638. * @vfb: Pointer to the dma-buffer backed framebuffer.
  639. * @clips: Array of clip rects. Either @clips or @vclips must be NULL.
  640. * @vclips: Alternate array of clip rects. Either @clips or @vclips must
  641. * be NULL.
  642. * @num_clips: Number of clip rects in @clips or @vclips.
  643. * @increment: Increment to use when looping over @clips or @vclips.
  644. * @to_surface: Whether to DMA to the screen target system as opposed to
  645. * from the screen target system.
  646. * @interruptible: Whether to perform waits interruptible if possible.
  647. *
  648. * If DMA-ing till the screen target system, the function will also notify
  649. * the screen target system that a bounding box of the cliprects has been
  650. * updated.
  651. * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
  652. * interrupted.
  653. */
  654. int vmw_kms_stdu_dma(struct vmw_private *dev_priv,
  655. struct drm_file *file_priv,
  656. struct vmw_framebuffer *vfb,
  657. struct drm_vmw_fence_rep __user *user_fence_rep,
  658. struct drm_clip_rect *clips,
  659. struct drm_vmw_rect *vclips,
  660. uint32_t num_clips,
  661. int increment,
  662. bool to_surface,
  663. bool interruptible)
  664. {
  665. struct vmw_dma_buffer *buf =
  666. container_of(vfb, struct vmw_framebuffer_dmabuf, base)->buffer;
  667. struct vmw_stdu_dirty ddirty;
  668. int ret;
  669. ret = vmw_kms_helper_buffer_prepare(dev_priv, buf, interruptible,
  670. false);
  671. if (ret)
  672. return ret;
  673. ddirty.transfer = (to_surface) ? SVGA3D_WRITE_HOST_VRAM :
  674. SVGA3D_READ_HOST_VRAM;
  675. ddirty.left = ddirty.top = S32_MAX;
  676. ddirty.right = ddirty.bottom = S32_MIN;
  677. ddirty.fb_left = ddirty.fb_top = S32_MAX;
  678. ddirty.pitch = vfb->base.pitches[0];
  679. ddirty.buf = buf;
  680. ddirty.base.fifo_commit = vmw_stdu_dmabuf_fifo_commit;
  681. ddirty.base.clip = vmw_stdu_dmabuf_clip;
  682. ddirty.base.fifo_reserve_size = sizeof(struct vmw_stdu_dma) +
  683. num_clips * sizeof(SVGA3dCopyBox) +
  684. sizeof(SVGA3dCmdSurfaceDMASuffix);
  685. if (to_surface)
  686. ddirty.base.fifo_reserve_size += sizeof(struct vmw_stdu_update);
  687. /* 2D VMs cannot use SVGA_3D_CMD_SURFACE_DMA so do CPU blit instead */
  688. if (!(dev_priv->capabilities & SVGA_CAP_3D)) {
  689. ddirty.base.fifo_commit = vmw_stdu_dmabuf_cpu_commit;
  690. ddirty.base.clip = vmw_stdu_dmabuf_cpu_clip;
  691. ddirty.base.fifo_reserve_size = 0;
  692. }
  693. ret = vmw_kms_helper_dirty(dev_priv, vfb, clips, vclips,
  694. 0, 0, num_clips, increment, &ddirty.base);
  695. vmw_kms_helper_buffer_finish(dev_priv, file_priv, buf, NULL,
  696. user_fence_rep);
  697. return ret;
  698. }
  699. /**
  700. * vmw_stdu_surface_clip - Callback to encode a surface copy command cliprect
  701. *
  702. * @dirty: The closure structure.
  703. *
  704. * Encodes a surface copy command cliprect and updates the bounding box
  705. * for the copy.
  706. */
  707. static void vmw_kms_stdu_surface_clip(struct vmw_kms_dirty *dirty)
  708. {
  709. struct vmw_stdu_dirty *sdirty =
  710. container_of(dirty, struct vmw_stdu_dirty, base);
  711. struct vmw_stdu_surface_copy *cmd = dirty->cmd;
  712. struct vmw_screen_target_display_unit *stdu =
  713. container_of(dirty->unit, typeof(*stdu), base);
  714. if (sdirty->sid != stdu->display_srf->res.id) {
  715. struct SVGA3dCopyBox *blit = (struct SVGA3dCopyBox *) &cmd[1];
  716. blit += dirty->num_hits;
  717. blit->srcx = dirty->fb_x;
  718. blit->srcy = dirty->fb_y;
  719. blit->x = dirty->unit_x1;
  720. blit->y = dirty->unit_y1;
  721. blit->d = 1;
  722. blit->w = dirty->unit_x2 - dirty->unit_x1;
  723. blit->h = dirty->unit_y2 - dirty->unit_y1;
  724. }
  725. dirty->num_hits++;
  726. /* Destination bounding box */
  727. sdirty->left = min_t(s32, sdirty->left, dirty->unit_x1);
  728. sdirty->top = min_t(s32, sdirty->top, dirty->unit_y1);
  729. sdirty->right = max_t(s32, sdirty->right, dirty->unit_x2);
  730. sdirty->bottom = max_t(s32, sdirty->bottom, dirty->unit_y2);
  731. }
  732. /**
  733. * vmw_stdu_surface_fifo_commit - Callback to fill in and submit a surface
  734. * copy command.
  735. *
  736. * @dirty: The closure structure.
  737. *
  738. * Fills in the missing fields in a surface copy command, and encodes a screen
  739. * target update command.
  740. */
  741. static void vmw_kms_stdu_surface_fifo_commit(struct vmw_kms_dirty *dirty)
  742. {
  743. struct vmw_stdu_dirty *sdirty =
  744. container_of(dirty, struct vmw_stdu_dirty, base);
  745. struct vmw_screen_target_display_unit *stdu =
  746. container_of(dirty->unit, typeof(*stdu), base);
  747. struct vmw_stdu_surface_copy *cmd = dirty->cmd;
  748. struct vmw_stdu_update *update;
  749. size_t blit_size = sizeof(SVGA3dCopyBox) * dirty->num_hits;
  750. size_t commit_size;
  751. if (!dirty->num_hits) {
  752. vmw_fifo_commit(dirty->dev_priv, 0);
  753. return;
  754. }
  755. if (sdirty->sid != stdu->display_srf->res.id) {
  756. struct SVGA3dCopyBox *blit = (struct SVGA3dCopyBox *) &cmd[1];
  757. cmd->header.id = SVGA_3D_CMD_SURFACE_COPY;
  758. cmd->header.size = sizeof(cmd->body) + blit_size;
  759. cmd->body.src.sid = sdirty->sid;
  760. cmd->body.dest.sid = stdu->display_srf->res.id;
  761. update = (struct vmw_stdu_update *) &blit[dirty->num_hits];
  762. commit_size = sizeof(*cmd) + blit_size + sizeof(*update);
  763. } else {
  764. update = dirty->cmd;
  765. commit_size = sizeof(*update);
  766. }
  767. vmw_stdu_populate_update(update, stdu->base.unit, sdirty->left,
  768. sdirty->right, sdirty->top, sdirty->bottom);
  769. vmw_fifo_commit(dirty->dev_priv, commit_size);
  770. sdirty->left = sdirty->top = S32_MAX;
  771. sdirty->right = sdirty->bottom = S32_MIN;
  772. }
  773. /**
  774. * vmw_kms_stdu_surface_dirty - Dirty part of a surface backed framebuffer
  775. *
  776. * @dev_priv: Pointer to the device private structure.
  777. * @framebuffer: Pointer to the surface-buffer backed framebuffer.
  778. * @clips: Array of clip rects. Either @clips or @vclips must be NULL.
  779. * @vclips: Alternate array of clip rects. Either @clips or @vclips must
  780. * be NULL.
  781. * @srf: Pointer to surface to blit from. If NULL, the surface attached
  782. * to @framebuffer will be used.
  783. * @dest_x: X coordinate offset to align @srf with framebuffer coordinates.
  784. * @dest_y: Y coordinate offset to align @srf with framebuffer coordinates.
  785. * @num_clips: Number of clip rects in @clips.
  786. * @inc: Increment to use when looping over @clips.
  787. * @out_fence: If non-NULL, will return a ref-counted pointer to a
  788. * struct vmw_fence_obj. The returned fence pointer may be NULL in which
  789. * case the device has already synchronized.
  790. *
  791. * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
  792. * interrupted.
  793. */
  794. int vmw_kms_stdu_surface_dirty(struct vmw_private *dev_priv,
  795. struct vmw_framebuffer *framebuffer,
  796. struct drm_clip_rect *clips,
  797. struct drm_vmw_rect *vclips,
  798. struct vmw_resource *srf,
  799. s32 dest_x,
  800. s32 dest_y,
  801. unsigned num_clips, int inc,
  802. struct vmw_fence_obj **out_fence)
  803. {
  804. struct vmw_framebuffer_surface *vfbs =
  805. container_of(framebuffer, typeof(*vfbs), base);
  806. struct vmw_stdu_dirty sdirty;
  807. int ret;
  808. if (!srf)
  809. srf = &vfbs->surface->res;
  810. ret = vmw_kms_helper_resource_prepare(srf, true);
  811. if (ret)
  812. return ret;
  813. if (vfbs->is_dmabuf_proxy) {
  814. ret = vmw_kms_update_proxy(srf, clips, num_clips, inc);
  815. if (ret)
  816. goto out_finish;
  817. }
  818. sdirty.base.fifo_commit = vmw_kms_stdu_surface_fifo_commit;
  819. sdirty.base.clip = vmw_kms_stdu_surface_clip;
  820. sdirty.base.fifo_reserve_size = sizeof(struct vmw_stdu_surface_copy) +
  821. sizeof(SVGA3dCopyBox) * num_clips +
  822. sizeof(struct vmw_stdu_update);
  823. sdirty.sid = srf->id;
  824. sdirty.left = sdirty.top = S32_MAX;
  825. sdirty.right = sdirty.bottom = S32_MIN;
  826. ret = vmw_kms_helper_dirty(dev_priv, framebuffer, clips, vclips,
  827. dest_x, dest_y, num_clips, inc,
  828. &sdirty.base);
  829. out_finish:
  830. vmw_kms_helper_resource_finish(srf, out_fence);
  831. return ret;
  832. }
  833. /*
  834. * Screen Target CRTC dispatch table
  835. */
  836. static const struct drm_crtc_funcs vmw_stdu_crtc_funcs = {
  837. .gamma_set = vmw_du_crtc_gamma_set,
  838. .destroy = vmw_stdu_crtc_destroy,
  839. .reset = vmw_du_crtc_reset,
  840. .atomic_duplicate_state = vmw_du_crtc_duplicate_state,
  841. .atomic_destroy_state = vmw_du_crtc_destroy_state,
  842. .set_config = vmw_kms_set_config,
  843. .page_flip = vmw_stdu_crtc_page_flip,
  844. };
  845. /******************************************************************************
  846. * Screen Target Display Unit Encoder Functions
  847. *****************************************************************************/
  848. /**
  849. * vmw_stdu_encoder_destroy - cleans up the STDU
  850. *
  851. * @encoder: used the get the containing STDU
  852. *
  853. * vmwgfx cleans up crtc/encoder/connector all at the same time so technically
  854. * this can be a no-op. Nevertheless, it doesn't hurt of have this in case
  855. * the common KMS code changes and somehow vmw_stdu_crtc_destroy() doesn't
  856. * get called.
  857. */
  858. static void vmw_stdu_encoder_destroy(struct drm_encoder *encoder)
  859. {
  860. vmw_stdu_destroy(vmw_encoder_to_stdu(encoder));
  861. }
  862. static const struct drm_encoder_funcs vmw_stdu_encoder_funcs = {
  863. .destroy = vmw_stdu_encoder_destroy,
  864. };
  865. /******************************************************************************
  866. * Screen Target Display Unit Connector Functions
  867. *****************************************************************************/
  868. /**
  869. * vmw_stdu_connector_destroy - cleans up the STDU
  870. *
  871. * @connector: used to get the containing STDU
  872. *
  873. * vmwgfx cleans up crtc/encoder/connector all at the same time so technically
  874. * this can be a no-op. Nevertheless, it doesn't hurt of have this in case
  875. * the common KMS code changes and somehow vmw_stdu_crtc_destroy() doesn't
  876. * get called.
  877. */
  878. static void vmw_stdu_connector_destroy(struct drm_connector *connector)
  879. {
  880. vmw_stdu_destroy(vmw_connector_to_stdu(connector));
  881. }
  882. static const struct drm_connector_funcs vmw_stdu_connector_funcs = {
  883. .dpms = vmw_du_connector_dpms,
  884. .detect = vmw_du_connector_detect,
  885. .fill_modes = vmw_du_connector_fill_modes,
  886. .set_property = vmw_du_connector_set_property,
  887. .destroy = vmw_stdu_connector_destroy,
  888. .reset = vmw_du_connector_reset,
  889. .atomic_duplicate_state = vmw_du_connector_duplicate_state,
  890. .atomic_destroy_state = vmw_du_connector_destroy_state,
  891. .atomic_set_property = vmw_du_connector_atomic_set_property,
  892. .atomic_get_property = vmw_du_connector_atomic_get_property,
  893. };
  894. static const struct
  895. drm_connector_helper_funcs vmw_stdu_connector_helper_funcs = {
  896. .best_encoder = drm_atomic_helper_best_encoder,
  897. };
  898. /******************************************************************************
  899. * Screen Target Display Plane Functions
  900. *****************************************************************************/
  901. /**
  902. * vmw_stdu_primary_plane_cleanup_fb - Unpins the display surface
  903. *
  904. * @plane: display plane
  905. * @old_state: Contains the FB to clean up
  906. *
  907. * Unpins the display surface
  908. *
  909. * Returns 0 on success
  910. */
  911. static void
  912. vmw_stdu_primary_plane_cleanup_fb(struct drm_plane *plane,
  913. struct drm_plane_state *old_state)
  914. {
  915. struct vmw_plane_state *vps = vmw_plane_state_to_vps(old_state);
  916. if (vps->guest_map.virtual)
  917. ttm_bo_kunmap(&vps->guest_map);
  918. if (vps->host_map.virtual)
  919. ttm_bo_kunmap(&vps->host_map);
  920. if (vps->surf)
  921. WARN_ON(!vps->pinned);
  922. vmw_du_plane_cleanup_fb(plane, old_state);
  923. vps->content_fb_type = SAME_AS_DISPLAY;
  924. vps->cpp = 0;
  925. }
  926. /**
  927. * vmw_stdu_primary_plane_prepare_fb - Readies the display surface
  928. *
  929. * @plane: display plane
  930. * @new_state: info on the new plane state, including the FB
  931. *
  932. * This function allocates a new display surface if the content is
  933. * backed by a DMA. The display surface is pinned here, and it'll
  934. * be unpinned in .cleanup_fb()
  935. *
  936. * Returns 0 on success
  937. */
  938. static int
  939. vmw_stdu_primary_plane_prepare_fb(struct drm_plane *plane,
  940. struct drm_plane_state *new_state)
  941. {
  942. struct vmw_private *dev_priv = vmw_priv(plane->dev);
  943. struct drm_framebuffer *new_fb = new_state->fb;
  944. struct vmw_framebuffer *vfb;
  945. struct vmw_plane_state *vps = vmw_plane_state_to_vps(new_state);
  946. enum stdu_content_type new_content_type;
  947. struct vmw_framebuffer_surface *new_vfbs;
  948. struct drm_crtc *crtc = new_state->crtc;
  949. uint32_t hdisplay = new_state->crtc_w, vdisplay = new_state->crtc_h;
  950. int ret;
  951. /* No FB to prepare */
  952. if (!new_fb) {
  953. if (vps->surf) {
  954. WARN_ON(vps->pinned != 0);
  955. vmw_surface_unreference(&vps->surf);
  956. }
  957. return 0;
  958. }
  959. vfb = vmw_framebuffer_to_vfb(new_fb);
  960. new_vfbs = (vfb->dmabuf) ? NULL : vmw_framebuffer_to_vfbs(new_fb);
  961. if (new_vfbs && new_vfbs->surface->base_size.width == hdisplay &&
  962. new_vfbs->surface->base_size.height == vdisplay)
  963. new_content_type = SAME_AS_DISPLAY;
  964. else if (vfb->dmabuf)
  965. new_content_type = SEPARATE_DMA;
  966. else
  967. new_content_type = SEPARATE_SURFACE;
  968. if (new_content_type != SAME_AS_DISPLAY) {
  969. struct vmw_surface content_srf;
  970. struct drm_vmw_size display_base_size = {0};
  971. display_base_size.width = hdisplay;
  972. display_base_size.height = vdisplay;
  973. display_base_size.depth = 1;
  974. /*
  975. * If content buffer is a DMA buf, then we have to construct
  976. * surface info
  977. */
  978. if (new_content_type == SEPARATE_DMA) {
  979. switch (new_fb->format->cpp[0]*8) {
  980. case 32:
  981. content_srf.format = SVGA3D_X8R8G8B8;
  982. break;
  983. case 16:
  984. content_srf.format = SVGA3D_R5G6B5;
  985. break;
  986. case 8:
  987. content_srf.format = SVGA3D_P8;
  988. break;
  989. default:
  990. DRM_ERROR("Invalid format\n");
  991. return -EINVAL;
  992. }
  993. content_srf.flags = 0;
  994. content_srf.mip_levels[0] = 1;
  995. content_srf.multisample_count = 0;
  996. } else {
  997. content_srf = *new_vfbs->surface;
  998. }
  999. if (vps->surf) {
  1000. struct drm_vmw_size cur_base_size = vps->surf->base_size;
  1001. if (cur_base_size.width != display_base_size.width ||
  1002. cur_base_size.height != display_base_size.height ||
  1003. vps->surf->format != content_srf.format) {
  1004. WARN_ON(vps->pinned != 0);
  1005. vmw_surface_unreference(&vps->surf);
  1006. }
  1007. }
  1008. if (!vps->surf) {
  1009. ret = vmw_surface_gb_priv_define
  1010. (crtc->dev,
  1011. /* Kernel visible only */
  1012. 0,
  1013. content_srf.flags,
  1014. content_srf.format,
  1015. true, /* a scanout buffer */
  1016. content_srf.mip_levels[0],
  1017. content_srf.multisample_count,
  1018. 0,
  1019. display_base_size,
  1020. &vps->surf);
  1021. if (ret != 0) {
  1022. DRM_ERROR("Couldn't allocate STDU surface.\n");
  1023. return ret;
  1024. }
  1025. }
  1026. } else {
  1027. /*
  1028. * prepare_fb and clean_fb should only take care of pinning
  1029. * and unpinning. References are tracked by state objects.
  1030. * The only time we add a reference in prepare_fb is if the
  1031. * state object doesn't have a reference to begin with
  1032. */
  1033. if (vps->surf) {
  1034. WARN_ON(vps->pinned != 0);
  1035. vmw_surface_unreference(&vps->surf);
  1036. }
  1037. vps->surf = vmw_surface_reference(new_vfbs->surface);
  1038. }
  1039. if (vps->surf) {
  1040. /* Pin new surface before flipping */
  1041. ret = vmw_resource_pin(&vps->surf->res, false);
  1042. if (ret)
  1043. goto out_srf_unref;
  1044. vps->pinned++;
  1045. }
  1046. vps->content_fb_type = new_content_type;
  1047. /*
  1048. * This should only happen if the DMA buf is too large to create a
  1049. * proxy surface for.
  1050. * If we are a 2D VM with a DMA buffer then we have to use CPU blit
  1051. * so cache these mappings
  1052. */
  1053. if (vps->content_fb_type == SEPARATE_DMA &&
  1054. !(dev_priv->capabilities & SVGA_CAP_3D)) {
  1055. struct vmw_framebuffer_dmabuf *new_vfbd;
  1056. new_vfbd = vmw_framebuffer_to_vfbd(new_fb);
  1057. ret = ttm_bo_reserve(&new_vfbd->buffer->base, false, false,
  1058. NULL);
  1059. if (ret)
  1060. goto out_srf_unpin;
  1061. ret = ttm_bo_kmap(&new_vfbd->buffer->base, 0,
  1062. new_vfbd->buffer->base.num_pages,
  1063. &vps->guest_map);
  1064. ttm_bo_unreserve(&new_vfbd->buffer->base);
  1065. if (ret) {
  1066. DRM_ERROR("Failed to map content buffer to CPU\n");
  1067. goto out_srf_unpin;
  1068. }
  1069. ret = ttm_bo_kmap(&vps->surf->res.backup->base, 0,
  1070. vps->surf->res.backup->base.num_pages,
  1071. &vps->host_map);
  1072. if (ret) {
  1073. DRM_ERROR("Failed to map display buffer to CPU\n");
  1074. ttm_bo_kunmap(&vps->guest_map);
  1075. goto out_srf_unpin;
  1076. }
  1077. vps->cpp = new_fb->pitches[0] / new_fb->width;
  1078. }
  1079. return 0;
  1080. out_srf_unpin:
  1081. vmw_resource_unpin(&vps->surf->res);
  1082. vps->pinned--;
  1083. out_srf_unref:
  1084. vmw_surface_unreference(&vps->surf);
  1085. return ret;
  1086. }
  1087. /**
  1088. * vmw_stdu_primary_plane_atomic_update - formally switches STDU to new plane
  1089. *
  1090. * @plane: display plane
  1091. * @old_state: Only used to get crtc info
  1092. *
  1093. * Formally update stdu->display_srf to the new plane, and bind the new
  1094. * plane STDU. This function is called during the commit phase when
  1095. * all the preparation have been done and all the configurations have
  1096. * been checked.
  1097. */
  1098. static void
  1099. vmw_stdu_primary_plane_atomic_update(struct drm_plane *plane,
  1100. struct drm_plane_state *old_state)
  1101. {
  1102. struct vmw_private *dev_priv;
  1103. struct vmw_screen_target_display_unit *stdu;
  1104. struct vmw_plane_state *vps = vmw_plane_state_to_vps(plane->state);
  1105. struct drm_crtc *crtc = plane->state->crtc ?: old_state->crtc;
  1106. int ret;
  1107. stdu = vmw_crtc_to_stdu(crtc);
  1108. dev_priv = vmw_priv(crtc->dev);
  1109. stdu->display_srf = vps->surf;
  1110. stdu->content_fb_type = vps->content_fb_type;
  1111. stdu->cpp = vps->cpp;
  1112. memcpy(&stdu->guest_map, &vps->guest_map, sizeof(vps->guest_map));
  1113. memcpy(&stdu->host_map, &vps->host_map, sizeof(vps->host_map));
  1114. if (!stdu->defined)
  1115. return;
  1116. if (plane->state->fb)
  1117. ret = vmw_stdu_bind_st(dev_priv, stdu, &stdu->display_srf->res);
  1118. else
  1119. ret = vmw_stdu_bind_st(dev_priv, stdu, NULL);
  1120. /*
  1121. * We cannot really fail this function, so if we do, then output an
  1122. * error and quit
  1123. */
  1124. if (ret)
  1125. DRM_ERROR("Failed to bind surface to STDU.\n");
  1126. else
  1127. crtc->primary->fb = plane->state->fb;
  1128. ret = vmw_stdu_update_st(dev_priv, stdu);
  1129. if (ret)
  1130. DRM_ERROR("Failed to update STDU.\n");
  1131. }
  1132. static const struct drm_plane_funcs vmw_stdu_plane_funcs = {
  1133. .update_plane = drm_atomic_helper_update_plane,
  1134. .disable_plane = drm_atomic_helper_disable_plane,
  1135. .destroy = vmw_du_primary_plane_destroy,
  1136. .reset = vmw_du_plane_reset,
  1137. .atomic_duplicate_state = vmw_du_plane_duplicate_state,
  1138. .atomic_destroy_state = vmw_du_plane_destroy_state,
  1139. };
  1140. static const struct drm_plane_funcs vmw_stdu_cursor_funcs = {
  1141. .update_plane = drm_atomic_helper_update_plane,
  1142. .disable_plane = drm_atomic_helper_disable_plane,
  1143. .destroy = vmw_du_cursor_plane_destroy,
  1144. .reset = vmw_du_plane_reset,
  1145. .atomic_duplicate_state = vmw_du_plane_duplicate_state,
  1146. .atomic_destroy_state = vmw_du_plane_destroy_state,
  1147. };
  1148. /*
  1149. * Atomic Helpers
  1150. */
  1151. static const struct
  1152. drm_plane_helper_funcs vmw_stdu_cursor_plane_helper_funcs = {
  1153. .atomic_check = vmw_du_cursor_plane_atomic_check,
  1154. .atomic_update = vmw_du_cursor_plane_atomic_update,
  1155. .prepare_fb = vmw_du_cursor_plane_prepare_fb,
  1156. .cleanup_fb = vmw_du_plane_cleanup_fb,
  1157. };
  1158. static const struct
  1159. drm_plane_helper_funcs vmw_stdu_primary_plane_helper_funcs = {
  1160. .atomic_check = vmw_du_primary_plane_atomic_check,
  1161. .atomic_update = vmw_stdu_primary_plane_atomic_update,
  1162. .prepare_fb = vmw_stdu_primary_plane_prepare_fb,
  1163. .cleanup_fb = vmw_stdu_primary_plane_cleanup_fb,
  1164. };
  1165. static const struct drm_crtc_helper_funcs vmw_stdu_crtc_helper_funcs = {
  1166. .prepare = vmw_stdu_crtc_helper_prepare,
  1167. .commit = vmw_stdu_crtc_helper_commit,
  1168. .disable = vmw_stdu_crtc_helper_disable,
  1169. .mode_set_nofb = vmw_stdu_crtc_mode_set_nofb,
  1170. .atomic_check = vmw_du_crtc_atomic_check,
  1171. .atomic_begin = vmw_du_crtc_atomic_begin,
  1172. .atomic_flush = vmw_du_crtc_atomic_flush,
  1173. };
  1174. /**
  1175. * vmw_stdu_init - Sets up a Screen Target Display Unit
  1176. *
  1177. * @dev_priv: VMW DRM device
  1178. * @unit: unit number range from 0 to VMWGFX_NUM_DISPLAY_UNITS
  1179. *
  1180. * This function is called once per CRTC, and allocates one Screen Target
  1181. * display unit to represent that CRTC. Since the SVGA device does not separate
  1182. * out encoder and connector, they are represented as part of the STDU as well.
  1183. */
  1184. static int vmw_stdu_init(struct vmw_private *dev_priv, unsigned unit)
  1185. {
  1186. struct vmw_screen_target_display_unit *stdu;
  1187. struct drm_device *dev = dev_priv->dev;
  1188. struct drm_connector *connector;
  1189. struct drm_encoder *encoder;
  1190. struct drm_plane *primary, *cursor;
  1191. struct drm_crtc *crtc;
  1192. int ret;
  1193. stdu = kzalloc(sizeof(*stdu), GFP_KERNEL);
  1194. if (!stdu)
  1195. return -ENOMEM;
  1196. stdu->base.unit = unit;
  1197. crtc = &stdu->base.crtc;
  1198. encoder = &stdu->base.encoder;
  1199. connector = &stdu->base.connector;
  1200. primary = &stdu->base.primary;
  1201. cursor = &stdu->base.cursor;
  1202. stdu->base.pref_active = (unit == 0);
  1203. stdu->base.pref_width = dev_priv->initial_width;
  1204. stdu->base.pref_height = dev_priv->initial_height;
  1205. /*
  1206. * Remove this after enabling atomic because property values can
  1207. * only exist in a state object
  1208. */
  1209. stdu->base.is_implicit = false;
  1210. /* Initialize primary plane */
  1211. vmw_du_plane_reset(primary);
  1212. ret = drm_universal_plane_init(dev, primary,
  1213. 0, &vmw_stdu_plane_funcs,
  1214. vmw_primary_plane_formats,
  1215. ARRAY_SIZE(vmw_primary_plane_formats),
  1216. DRM_PLANE_TYPE_PRIMARY, NULL);
  1217. if (ret) {
  1218. DRM_ERROR("Failed to initialize primary plane");
  1219. goto err_free;
  1220. }
  1221. drm_plane_helper_add(primary, &vmw_stdu_primary_plane_helper_funcs);
  1222. /* Initialize cursor plane */
  1223. vmw_du_plane_reset(cursor);
  1224. ret = drm_universal_plane_init(dev, cursor,
  1225. 0, &vmw_stdu_cursor_funcs,
  1226. vmw_cursor_plane_formats,
  1227. ARRAY_SIZE(vmw_cursor_plane_formats),
  1228. DRM_PLANE_TYPE_CURSOR, NULL);
  1229. if (ret) {
  1230. DRM_ERROR("Failed to initialize cursor plane");
  1231. drm_plane_cleanup(&stdu->base.primary);
  1232. goto err_free;
  1233. }
  1234. drm_plane_helper_add(cursor, &vmw_stdu_cursor_plane_helper_funcs);
  1235. vmw_du_connector_reset(connector);
  1236. ret = drm_connector_init(dev, connector, &vmw_stdu_connector_funcs,
  1237. DRM_MODE_CONNECTOR_VIRTUAL);
  1238. if (ret) {
  1239. DRM_ERROR("Failed to initialize connector\n");
  1240. goto err_free;
  1241. }
  1242. drm_connector_helper_add(connector, &vmw_stdu_connector_helper_funcs);
  1243. connector->status = vmw_du_connector_detect(connector, false);
  1244. vmw_connector_state_to_vcs(connector->state)->is_implicit = false;
  1245. ret = drm_encoder_init(dev, encoder, &vmw_stdu_encoder_funcs,
  1246. DRM_MODE_ENCODER_VIRTUAL, NULL);
  1247. if (ret) {
  1248. DRM_ERROR("Failed to initialize encoder\n");
  1249. goto err_free_connector;
  1250. }
  1251. (void) drm_mode_connector_attach_encoder(connector, encoder);
  1252. encoder->possible_crtcs = (1 << unit);
  1253. encoder->possible_clones = 0;
  1254. ret = drm_connector_register(connector);
  1255. if (ret) {
  1256. DRM_ERROR("Failed to register connector\n");
  1257. goto err_free_encoder;
  1258. }
  1259. vmw_du_crtc_reset(crtc);
  1260. ret = drm_crtc_init_with_planes(dev, crtc, &stdu->base.primary,
  1261. &stdu->base.cursor,
  1262. &vmw_stdu_crtc_funcs, NULL);
  1263. if (ret) {
  1264. DRM_ERROR("Failed to initialize CRTC\n");
  1265. goto err_free_unregister;
  1266. }
  1267. drm_crtc_helper_add(crtc, &vmw_stdu_crtc_helper_funcs);
  1268. drm_mode_crtc_set_gamma_size(crtc, 256);
  1269. drm_object_attach_property(&connector->base,
  1270. dev_priv->hotplug_mode_update_property, 1);
  1271. drm_object_attach_property(&connector->base,
  1272. dev->mode_config.suggested_x_property, 0);
  1273. drm_object_attach_property(&connector->base,
  1274. dev->mode_config.suggested_y_property, 0);
  1275. if (dev_priv->implicit_placement_property)
  1276. drm_object_attach_property
  1277. (&connector->base,
  1278. dev_priv->implicit_placement_property,
  1279. stdu->base.is_implicit);
  1280. return 0;
  1281. err_free_unregister:
  1282. drm_connector_unregister(connector);
  1283. err_free_encoder:
  1284. drm_encoder_cleanup(encoder);
  1285. err_free_connector:
  1286. drm_connector_cleanup(connector);
  1287. err_free:
  1288. kfree(stdu);
  1289. return ret;
  1290. }
  1291. /**
  1292. * vmw_stdu_destroy - Cleans up a vmw_screen_target_display_unit
  1293. *
  1294. * @stdu: Screen Target Display Unit to be destroyed
  1295. *
  1296. * Clean up after vmw_stdu_init
  1297. */
  1298. static void vmw_stdu_destroy(struct vmw_screen_target_display_unit *stdu)
  1299. {
  1300. vmw_du_cleanup(&stdu->base);
  1301. kfree(stdu);
  1302. }
  1303. /******************************************************************************
  1304. * Screen Target Display KMS Functions
  1305. *
  1306. * These functions are called by the common KMS code in vmwgfx_kms.c
  1307. *****************************************************************************/
  1308. /**
  1309. * vmw_kms_stdu_init_display - Initializes a Screen Target based display
  1310. *
  1311. * @dev_priv: VMW DRM device
  1312. *
  1313. * This function initialize a Screen Target based display device. It checks
  1314. * the capability bits to make sure the underlying hardware can support
  1315. * screen targets, and then creates the maximum number of CRTCs, a.k.a Display
  1316. * Units, as supported by the display hardware.
  1317. *
  1318. * RETURNS:
  1319. * 0 on success, error code otherwise
  1320. */
  1321. int vmw_kms_stdu_init_display(struct vmw_private *dev_priv)
  1322. {
  1323. struct drm_device *dev = dev_priv->dev;
  1324. int i, ret;
  1325. /* Do nothing if Screen Target support is turned off */
  1326. if (!VMWGFX_ENABLE_SCREEN_TARGET_OTABLE)
  1327. return -ENOSYS;
  1328. if (!(dev_priv->capabilities & SVGA_CAP_GBOBJECTS))
  1329. return -ENOSYS;
  1330. ret = drm_vblank_init(dev, VMWGFX_NUM_DISPLAY_UNITS);
  1331. if (unlikely(ret != 0))
  1332. return ret;
  1333. dev_priv->active_display_unit = vmw_du_screen_target;
  1334. if (dev_priv->capabilities & SVGA_CAP_3D) {
  1335. /*
  1336. * For 3D VMs, display (scanout) buffer size is the smaller of
  1337. * max texture and max STDU
  1338. */
  1339. uint32_t max_width, max_height;
  1340. max_width = min(dev_priv->texture_max_width,
  1341. dev_priv->stdu_max_width);
  1342. max_height = min(dev_priv->texture_max_height,
  1343. dev_priv->stdu_max_height);
  1344. dev->mode_config.max_width = max_width;
  1345. dev->mode_config.max_height = max_height;
  1346. } else {
  1347. /*
  1348. * Given various display aspect ratios, there's no way to
  1349. * estimate these using prim_bb_mem. So just set these to
  1350. * something arbitrarily large and we will reject any layout
  1351. * that doesn't fit prim_bb_mem later
  1352. */
  1353. dev->mode_config.max_width = 16384;
  1354. dev->mode_config.max_height = 16384;
  1355. }
  1356. vmw_kms_create_implicit_placement_property(dev_priv, false);
  1357. for (i = 0; i < VMWGFX_NUM_DISPLAY_UNITS; ++i) {
  1358. ret = vmw_stdu_init(dev_priv, i);
  1359. if (unlikely(ret != 0)) {
  1360. DRM_ERROR("Failed to initialize STDU %d", i);
  1361. goto err_vblank_cleanup;
  1362. }
  1363. }
  1364. DRM_INFO("Screen Target Display device initialized\n");
  1365. return 0;
  1366. err_vblank_cleanup:
  1367. drm_vblank_cleanup(dev);
  1368. return ret;
  1369. }
  1370. /**
  1371. * vmw_kms_stdu_close_display - Cleans up after vmw_kms_stdu_init_display
  1372. *
  1373. * @dev_priv: VMW DRM device
  1374. *
  1375. * Frees up any resources allocated by vmw_kms_stdu_init_display
  1376. *
  1377. * RETURNS:
  1378. * 0 on success
  1379. */
  1380. int vmw_kms_stdu_close_display(struct vmw_private *dev_priv)
  1381. {
  1382. struct drm_device *dev = dev_priv->dev;
  1383. drm_vblank_cleanup(dev);
  1384. return 0;
  1385. }