vmwgfx_stdu.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663
  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_atomic_enable(struct drm_crtc *crtc,
  334. struct drm_crtc_state *old_state)
  335. {
  336. struct vmw_private *dev_priv;
  337. struct vmw_screen_target_display_unit *stdu;
  338. struct vmw_framebuffer *vfb;
  339. struct drm_framebuffer *fb;
  340. stdu = vmw_crtc_to_stdu(crtc);
  341. dev_priv = vmw_priv(crtc->dev);
  342. fb = crtc->primary->fb;
  343. vfb = (fb) ? vmw_framebuffer_to_vfb(fb) : NULL;
  344. if (vfb)
  345. vmw_kms_add_active(dev_priv, &stdu->base, vfb);
  346. else
  347. vmw_kms_del_active(dev_priv, &stdu->base);
  348. }
  349. static void vmw_stdu_crtc_atomic_disable(struct drm_crtc *crtc,
  350. struct drm_crtc_state *old_state)
  351. {
  352. struct vmw_private *dev_priv;
  353. struct vmw_screen_target_display_unit *stdu;
  354. int ret;
  355. if (!crtc) {
  356. DRM_ERROR("CRTC is NULL\n");
  357. return;
  358. }
  359. stdu = vmw_crtc_to_stdu(crtc);
  360. dev_priv = vmw_priv(crtc->dev);
  361. if (stdu->defined) {
  362. ret = vmw_stdu_bind_st(dev_priv, stdu, NULL);
  363. if (ret)
  364. DRM_ERROR("Failed to blank CRTC\n");
  365. (void) vmw_stdu_update_st(dev_priv, stdu);
  366. ret = vmw_stdu_destroy_st(dev_priv, stdu);
  367. if (ret)
  368. DRM_ERROR("Failed to destroy Screen Target\n");
  369. stdu->content_fb_type = SAME_AS_DISPLAY;
  370. }
  371. }
  372. /**
  373. * vmw_stdu_crtc_page_flip - Binds a buffer to a screen target
  374. *
  375. * @crtc: CRTC to attach FB to
  376. * @fb: FB to attach
  377. * @event: Event to be posted. This event should've been alloced
  378. * using k[mz]alloc, and should've been completely initialized.
  379. * @page_flip_flags: Input flags.
  380. *
  381. * If the STDU uses the same display and content buffers, i.e. a true flip,
  382. * this function will replace the existing display buffer with the new content
  383. * buffer.
  384. *
  385. * If the STDU uses different display and content buffers, i.e. a blit, then
  386. * only the content buffer will be updated.
  387. *
  388. * RETURNS:
  389. * 0 on success, error code on failure
  390. */
  391. static int vmw_stdu_crtc_page_flip(struct drm_crtc *crtc,
  392. struct drm_framebuffer *new_fb,
  393. struct drm_pending_vblank_event *event,
  394. uint32_t flags,
  395. struct drm_modeset_acquire_ctx *ctx)
  396. {
  397. struct vmw_private *dev_priv = vmw_priv(crtc->dev);
  398. struct vmw_screen_target_display_unit *stdu = vmw_crtc_to_stdu(crtc);
  399. struct vmw_framebuffer *vfb = vmw_framebuffer_to_vfb(new_fb);
  400. struct drm_vmw_rect vclips;
  401. int ret;
  402. dev_priv = vmw_priv(crtc->dev);
  403. stdu = vmw_crtc_to_stdu(crtc);
  404. if (!stdu->defined || !vmw_kms_crtc_flippable(dev_priv, crtc))
  405. return -EINVAL;
  406. /*
  407. * We're always async, but the helper doesn't know how to set async
  408. * so lie to the helper. Also, the helper expects someone
  409. * to pick the event up from the crtc state, and if nobody does,
  410. * it will free it. Since we handle the event in this function,
  411. * don't hand it to the helper.
  412. */
  413. flags &= ~DRM_MODE_PAGE_FLIP_ASYNC;
  414. ret = drm_atomic_helper_page_flip(crtc, new_fb, NULL, flags, ctx);
  415. if (ret) {
  416. DRM_ERROR("Page flip error %d.\n", ret);
  417. return ret;
  418. }
  419. if (stdu->base.is_implicit)
  420. vmw_kms_update_implicit_fb(dev_priv, crtc);
  421. /*
  422. * Now that we've bound a new surface to the screen target,
  423. * update the contents.
  424. */
  425. vclips.x = crtc->x;
  426. vclips.y = crtc->y;
  427. vclips.w = crtc->mode.hdisplay;
  428. vclips.h = crtc->mode.vdisplay;
  429. if (vfb->dmabuf)
  430. ret = vmw_kms_stdu_dma(dev_priv, NULL, vfb, NULL, NULL, &vclips,
  431. 1, 1, true, false);
  432. else
  433. ret = vmw_kms_stdu_surface_dirty(dev_priv, vfb, NULL, &vclips,
  434. NULL, 0, 0, 1, 1, NULL);
  435. if (ret) {
  436. DRM_ERROR("Page flip update error %d.\n", ret);
  437. return ret;
  438. }
  439. if (event) {
  440. struct vmw_fence_obj *fence = NULL;
  441. struct drm_file *file_priv = event->base.file_priv;
  442. vmw_execbuf_fence_commands(NULL, dev_priv, &fence, NULL);
  443. if (!fence)
  444. return -ENOMEM;
  445. ret = vmw_event_fence_action_queue(file_priv, fence,
  446. &event->base,
  447. &event->event.tv_sec,
  448. &event->event.tv_usec,
  449. true);
  450. vmw_fence_obj_unreference(&fence);
  451. } else {
  452. (void) vmw_fifo_flush(dev_priv, false);
  453. }
  454. return 0;
  455. }
  456. /**
  457. * vmw_stdu_dmabuf_clip - Callback to encode a suface DMA command cliprect
  458. *
  459. * @dirty: The closure structure.
  460. *
  461. * Encodes a surface DMA command cliprect and updates the bounding box
  462. * for the DMA.
  463. */
  464. static void vmw_stdu_dmabuf_clip(struct vmw_kms_dirty *dirty)
  465. {
  466. struct vmw_stdu_dirty *ddirty =
  467. container_of(dirty, struct vmw_stdu_dirty, base);
  468. struct vmw_stdu_dma *cmd = dirty->cmd;
  469. struct SVGA3dCopyBox *blit = (struct SVGA3dCopyBox *) &cmd[1];
  470. blit += dirty->num_hits;
  471. blit->srcx = dirty->fb_x;
  472. blit->srcy = dirty->fb_y;
  473. blit->x = dirty->unit_x1;
  474. blit->y = dirty->unit_y1;
  475. blit->d = 1;
  476. blit->w = dirty->unit_x2 - dirty->unit_x1;
  477. blit->h = dirty->unit_y2 - dirty->unit_y1;
  478. dirty->num_hits++;
  479. if (ddirty->transfer != SVGA3D_WRITE_HOST_VRAM)
  480. return;
  481. /* Destination bounding box */
  482. ddirty->left = min_t(s32, ddirty->left, dirty->unit_x1);
  483. ddirty->top = min_t(s32, ddirty->top, dirty->unit_y1);
  484. ddirty->right = max_t(s32, ddirty->right, dirty->unit_x2);
  485. ddirty->bottom = max_t(s32, ddirty->bottom, dirty->unit_y2);
  486. }
  487. /**
  488. * vmw_stdu_dmabuf_fifo_commit - Callback to fill in and submit a DMA command.
  489. *
  490. * @dirty: The closure structure.
  491. *
  492. * Fills in the missing fields in a DMA command, and optionally encodes
  493. * a screen target update command, depending on transfer direction.
  494. */
  495. static void vmw_stdu_dmabuf_fifo_commit(struct vmw_kms_dirty *dirty)
  496. {
  497. struct vmw_stdu_dirty *ddirty =
  498. container_of(dirty, struct vmw_stdu_dirty, base);
  499. struct vmw_screen_target_display_unit *stdu =
  500. container_of(dirty->unit, typeof(*stdu), base);
  501. struct vmw_stdu_dma *cmd = dirty->cmd;
  502. struct SVGA3dCopyBox *blit = (struct SVGA3dCopyBox *) &cmd[1];
  503. SVGA3dCmdSurfaceDMASuffix *suffix =
  504. (SVGA3dCmdSurfaceDMASuffix *) &blit[dirty->num_hits];
  505. size_t blit_size = sizeof(*blit) * dirty->num_hits + sizeof(*suffix);
  506. if (!dirty->num_hits) {
  507. vmw_fifo_commit(dirty->dev_priv, 0);
  508. return;
  509. }
  510. cmd->header.id = SVGA_3D_CMD_SURFACE_DMA;
  511. cmd->header.size = sizeof(cmd->body) + blit_size;
  512. vmw_bo_get_guest_ptr(&ddirty->buf->base, &cmd->body.guest.ptr);
  513. cmd->body.guest.pitch = ddirty->pitch;
  514. cmd->body.host.sid = stdu->display_srf->res.id;
  515. cmd->body.host.face = 0;
  516. cmd->body.host.mipmap = 0;
  517. cmd->body.transfer = ddirty->transfer;
  518. suffix->suffixSize = sizeof(*suffix);
  519. suffix->maximumOffset = ddirty->buf->base.num_pages * PAGE_SIZE;
  520. if (ddirty->transfer == SVGA3D_WRITE_HOST_VRAM) {
  521. blit_size += sizeof(struct vmw_stdu_update);
  522. vmw_stdu_populate_update(&suffix[1], stdu->base.unit,
  523. ddirty->left, ddirty->right,
  524. ddirty->top, ddirty->bottom);
  525. }
  526. vmw_fifo_commit(dirty->dev_priv, sizeof(*cmd) + blit_size);
  527. ddirty->left = ddirty->top = S32_MAX;
  528. ddirty->right = ddirty->bottom = S32_MIN;
  529. }
  530. /**
  531. * vmw_stdu_dmabuf_cpu_clip - Callback to encode a CPU blit
  532. *
  533. * @dirty: The closure structure.
  534. *
  535. * This function calculates the bounding box for all the incoming clips.
  536. */
  537. static void vmw_stdu_dmabuf_cpu_clip(struct vmw_kms_dirty *dirty)
  538. {
  539. struct vmw_stdu_dirty *ddirty =
  540. container_of(dirty, struct vmw_stdu_dirty, base);
  541. dirty->num_hits = 1;
  542. /* Calculate destination bounding box */
  543. ddirty->left = min_t(s32, ddirty->left, dirty->unit_x1);
  544. ddirty->top = min_t(s32, ddirty->top, dirty->unit_y1);
  545. ddirty->right = max_t(s32, ddirty->right, dirty->unit_x2);
  546. ddirty->bottom = max_t(s32, ddirty->bottom, dirty->unit_y2);
  547. /*
  548. * Calculate content bounding box. We only need the top-left
  549. * coordinate because width and height will be the same as the
  550. * destination bounding box above
  551. */
  552. ddirty->fb_left = min_t(s32, ddirty->fb_left, dirty->fb_x);
  553. ddirty->fb_top = min_t(s32, ddirty->fb_top, dirty->fb_y);
  554. }
  555. /**
  556. * vmw_stdu_dmabuf_cpu_commit - Callback to do a CPU blit from DMAbuf
  557. *
  558. * @dirty: The closure structure.
  559. *
  560. * For the special case when we cannot create a proxy surface in a
  561. * 2D VM, we have to do a CPU blit ourselves.
  562. */
  563. static void vmw_stdu_dmabuf_cpu_commit(struct vmw_kms_dirty *dirty)
  564. {
  565. struct vmw_stdu_dirty *ddirty =
  566. container_of(dirty, struct vmw_stdu_dirty, base);
  567. struct vmw_screen_target_display_unit *stdu =
  568. container_of(dirty->unit, typeof(*stdu), base);
  569. s32 width, height;
  570. s32 src_pitch, dst_pitch;
  571. u8 *src, *dst;
  572. bool not_used;
  573. if (!dirty->num_hits)
  574. return;
  575. width = ddirty->right - ddirty->left;
  576. height = ddirty->bottom - ddirty->top;
  577. if (width == 0 || height == 0)
  578. return;
  579. /* Assume we are blitting from Host (display_srf) to Guest (dmabuf) */
  580. src_pitch = stdu->display_srf->base_size.width * stdu->cpp;
  581. src = ttm_kmap_obj_virtual(&stdu->host_map, &not_used);
  582. src += ddirty->top * src_pitch + ddirty->left * stdu->cpp;
  583. dst_pitch = ddirty->pitch;
  584. dst = ttm_kmap_obj_virtual(&stdu->guest_map, &not_used);
  585. dst += ddirty->fb_top * dst_pitch + ddirty->fb_left * stdu->cpp;
  586. /* Figure out the real direction */
  587. if (ddirty->transfer == SVGA3D_WRITE_HOST_VRAM) {
  588. u8 *tmp;
  589. s32 tmp_pitch;
  590. tmp = src;
  591. tmp_pitch = src_pitch;
  592. src = dst;
  593. src_pitch = dst_pitch;
  594. dst = tmp;
  595. dst_pitch = tmp_pitch;
  596. }
  597. /* CPU Blit */
  598. while (height-- > 0) {
  599. memcpy(dst, src, width * stdu->cpp);
  600. dst += dst_pitch;
  601. src += src_pitch;
  602. }
  603. if (ddirty->transfer == SVGA3D_WRITE_HOST_VRAM) {
  604. struct vmw_private *dev_priv;
  605. struct vmw_stdu_update *cmd;
  606. struct drm_clip_rect region;
  607. int ret;
  608. /* We are updating the actual surface, not a proxy */
  609. region.x1 = ddirty->left;
  610. region.x2 = ddirty->right;
  611. region.y1 = ddirty->top;
  612. region.y2 = ddirty->bottom;
  613. ret = vmw_kms_update_proxy(
  614. (struct vmw_resource *) &stdu->display_srf->res,
  615. (const struct drm_clip_rect *) &region, 1, 1);
  616. if (ret)
  617. goto out_cleanup;
  618. dev_priv = vmw_priv(stdu->base.crtc.dev);
  619. cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
  620. if (!cmd) {
  621. DRM_ERROR("Cannot reserve FIFO space to update STDU");
  622. goto out_cleanup;
  623. }
  624. vmw_stdu_populate_update(cmd, stdu->base.unit,
  625. ddirty->left, ddirty->right,
  626. ddirty->top, ddirty->bottom);
  627. vmw_fifo_commit(dev_priv, sizeof(*cmd));
  628. }
  629. out_cleanup:
  630. ddirty->left = ddirty->top = ddirty->fb_left = ddirty->fb_top = S32_MAX;
  631. ddirty->right = ddirty->bottom = S32_MIN;
  632. }
  633. /**
  634. * vmw_kms_stdu_dma - Perform a DMA transfer between a dma-buffer backed
  635. * framebuffer and the screen target system.
  636. *
  637. * @dev_priv: Pointer to the device private structure.
  638. * @file_priv: Pointer to a struct drm-file identifying the caller. May be
  639. * set to NULL, but then @user_fence_rep must also be set to NULL.
  640. * @vfb: Pointer to the dma-buffer backed framebuffer.
  641. * @clips: Array of clip rects. Either @clips or @vclips must be NULL.
  642. * @vclips: Alternate array of clip rects. Either @clips or @vclips must
  643. * be NULL.
  644. * @num_clips: Number of clip rects in @clips or @vclips.
  645. * @increment: Increment to use when looping over @clips or @vclips.
  646. * @to_surface: Whether to DMA to the screen target system as opposed to
  647. * from the screen target system.
  648. * @interruptible: Whether to perform waits interruptible if possible.
  649. *
  650. * If DMA-ing till the screen target system, the function will also notify
  651. * the screen target system that a bounding box of the cliprects has been
  652. * updated.
  653. * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
  654. * interrupted.
  655. */
  656. int vmw_kms_stdu_dma(struct vmw_private *dev_priv,
  657. struct drm_file *file_priv,
  658. struct vmw_framebuffer *vfb,
  659. struct drm_vmw_fence_rep __user *user_fence_rep,
  660. struct drm_clip_rect *clips,
  661. struct drm_vmw_rect *vclips,
  662. uint32_t num_clips,
  663. int increment,
  664. bool to_surface,
  665. bool interruptible)
  666. {
  667. struct vmw_dma_buffer *buf =
  668. container_of(vfb, struct vmw_framebuffer_dmabuf, base)->buffer;
  669. struct vmw_stdu_dirty ddirty;
  670. int ret;
  671. ret = vmw_kms_helper_buffer_prepare(dev_priv, buf, interruptible,
  672. false);
  673. if (ret)
  674. return ret;
  675. ddirty.transfer = (to_surface) ? SVGA3D_WRITE_HOST_VRAM :
  676. SVGA3D_READ_HOST_VRAM;
  677. ddirty.left = ddirty.top = S32_MAX;
  678. ddirty.right = ddirty.bottom = S32_MIN;
  679. ddirty.fb_left = ddirty.fb_top = S32_MAX;
  680. ddirty.pitch = vfb->base.pitches[0];
  681. ddirty.buf = buf;
  682. ddirty.base.fifo_commit = vmw_stdu_dmabuf_fifo_commit;
  683. ddirty.base.clip = vmw_stdu_dmabuf_clip;
  684. ddirty.base.fifo_reserve_size = sizeof(struct vmw_stdu_dma) +
  685. num_clips * sizeof(SVGA3dCopyBox) +
  686. sizeof(SVGA3dCmdSurfaceDMASuffix);
  687. if (to_surface)
  688. ddirty.base.fifo_reserve_size += sizeof(struct vmw_stdu_update);
  689. /* 2D VMs cannot use SVGA_3D_CMD_SURFACE_DMA so do CPU blit instead */
  690. if (!(dev_priv->capabilities & SVGA_CAP_3D)) {
  691. ddirty.base.fifo_commit = vmw_stdu_dmabuf_cpu_commit;
  692. ddirty.base.clip = vmw_stdu_dmabuf_cpu_clip;
  693. ddirty.base.fifo_reserve_size = 0;
  694. }
  695. ret = vmw_kms_helper_dirty(dev_priv, vfb, clips, vclips,
  696. 0, 0, num_clips, increment, &ddirty.base);
  697. vmw_kms_helper_buffer_finish(dev_priv, file_priv, buf, NULL,
  698. user_fence_rep);
  699. return ret;
  700. }
  701. /**
  702. * vmw_stdu_surface_clip - Callback to encode a surface copy command cliprect
  703. *
  704. * @dirty: The closure structure.
  705. *
  706. * Encodes a surface copy command cliprect and updates the bounding box
  707. * for the copy.
  708. */
  709. static void vmw_kms_stdu_surface_clip(struct vmw_kms_dirty *dirty)
  710. {
  711. struct vmw_stdu_dirty *sdirty =
  712. container_of(dirty, struct vmw_stdu_dirty, base);
  713. struct vmw_stdu_surface_copy *cmd = dirty->cmd;
  714. struct vmw_screen_target_display_unit *stdu =
  715. container_of(dirty->unit, typeof(*stdu), base);
  716. if (sdirty->sid != stdu->display_srf->res.id) {
  717. struct SVGA3dCopyBox *blit = (struct SVGA3dCopyBox *) &cmd[1];
  718. blit += dirty->num_hits;
  719. blit->srcx = dirty->fb_x;
  720. blit->srcy = dirty->fb_y;
  721. blit->x = dirty->unit_x1;
  722. blit->y = dirty->unit_y1;
  723. blit->d = 1;
  724. blit->w = dirty->unit_x2 - dirty->unit_x1;
  725. blit->h = dirty->unit_y2 - dirty->unit_y1;
  726. }
  727. dirty->num_hits++;
  728. /* Destination bounding box */
  729. sdirty->left = min_t(s32, sdirty->left, dirty->unit_x1);
  730. sdirty->top = min_t(s32, sdirty->top, dirty->unit_y1);
  731. sdirty->right = max_t(s32, sdirty->right, dirty->unit_x2);
  732. sdirty->bottom = max_t(s32, sdirty->bottom, dirty->unit_y2);
  733. }
  734. /**
  735. * vmw_stdu_surface_fifo_commit - Callback to fill in and submit a surface
  736. * copy command.
  737. *
  738. * @dirty: The closure structure.
  739. *
  740. * Fills in the missing fields in a surface copy command, and encodes a screen
  741. * target update command.
  742. */
  743. static void vmw_kms_stdu_surface_fifo_commit(struct vmw_kms_dirty *dirty)
  744. {
  745. struct vmw_stdu_dirty *sdirty =
  746. container_of(dirty, struct vmw_stdu_dirty, base);
  747. struct vmw_screen_target_display_unit *stdu =
  748. container_of(dirty->unit, typeof(*stdu), base);
  749. struct vmw_stdu_surface_copy *cmd = dirty->cmd;
  750. struct vmw_stdu_update *update;
  751. size_t blit_size = sizeof(SVGA3dCopyBox) * dirty->num_hits;
  752. size_t commit_size;
  753. if (!dirty->num_hits) {
  754. vmw_fifo_commit(dirty->dev_priv, 0);
  755. return;
  756. }
  757. if (sdirty->sid != stdu->display_srf->res.id) {
  758. struct SVGA3dCopyBox *blit = (struct SVGA3dCopyBox *) &cmd[1];
  759. cmd->header.id = SVGA_3D_CMD_SURFACE_COPY;
  760. cmd->header.size = sizeof(cmd->body) + blit_size;
  761. cmd->body.src.sid = sdirty->sid;
  762. cmd->body.dest.sid = stdu->display_srf->res.id;
  763. update = (struct vmw_stdu_update *) &blit[dirty->num_hits];
  764. commit_size = sizeof(*cmd) + blit_size + sizeof(*update);
  765. } else {
  766. update = dirty->cmd;
  767. commit_size = sizeof(*update);
  768. }
  769. vmw_stdu_populate_update(update, stdu->base.unit, sdirty->left,
  770. sdirty->right, sdirty->top, sdirty->bottom);
  771. vmw_fifo_commit(dirty->dev_priv, commit_size);
  772. sdirty->left = sdirty->top = S32_MAX;
  773. sdirty->right = sdirty->bottom = S32_MIN;
  774. }
  775. /**
  776. * vmw_kms_stdu_surface_dirty - Dirty part of a surface backed framebuffer
  777. *
  778. * @dev_priv: Pointer to the device private structure.
  779. * @framebuffer: Pointer to the surface-buffer backed framebuffer.
  780. * @clips: Array of clip rects. Either @clips or @vclips must be NULL.
  781. * @vclips: Alternate array of clip rects. Either @clips or @vclips must
  782. * be NULL.
  783. * @srf: Pointer to surface to blit from. If NULL, the surface attached
  784. * to @framebuffer will be used.
  785. * @dest_x: X coordinate offset to align @srf with framebuffer coordinates.
  786. * @dest_y: Y coordinate offset to align @srf with framebuffer coordinates.
  787. * @num_clips: Number of clip rects in @clips.
  788. * @inc: Increment to use when looping over @clips.
  789. * @out_fence: If non-NULL, will return a ref-counted pointer to a
  790. * struct vmw_fence_obj. The returned fence pointer may be NULL in which
  791. * case the device has already synchronized.
  792. *
  793. * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
  794. * interrupted.
  795. */
  796. int vmw_kms_stdu_surface_dirty(struct vmw_private *dev_priv,
  797. struct vmw_framebuffer *framebuffer,
  798. struct drm_clip_rect *clips,
  799. struct drm_vmw_rect *vclips,
  800. struct vmw_resource *srf,
  801. s32 dest_x,
  802. s32 dest_y,
  803. unsigned num_clips, int inc,
  804. struct vmw_fence_obj **out_fence)
  805. {
  806. struct vmw_framebuffer_surface *vfbs =
  807. container_of(framebuffer, typeof(*vfbs), base);
  808. struct vmw_stdu_dirty sdirty;
  809. int ret;
  810. if (!srf)
  811. srf = &vfbs->surface->res;
  812. ret = vmw_kms_helper_resource_prepare(srf, true);
  813. if (ret)
  814. return ret;
  815. if (vfbs->is_dmabuf_proxy) {
  816. ret = vmw_kms_update_proxy(srf, clips, num_clips, inc);
  817. if (ret)
  818. goto out_finish;
  819. }
  820. sdirty.base.fifo_commit = vmw_kms_stdu_surface_fifo_commit;
  821. sdirty.base.clip = vmw_kms_stdu_surface_clip;
  822. sdirty.base.fifo_reserve_size = sizeof(struct vmw_stdu_surface_copy) +
  823. sizeof(SVGA3dCopyBox) * num_clips +
  824. sizeof(struct vmw_stdu_update);
  825. sdirty.sid = srf->id;
  826. sdirty.left = sdirty.top = S32_MAX;
  827. sdirty.right = sdirty.bottom = S32_MIN;
  828. ret = vmw_kms_helper_dirty(dev_priv, framebuffer, clips, vclips,
  829. dest_x, dest_y, num_clips, inc,
  830. &sdirty.base);
  831. out_finish:
  832. vmw_kms_helper_resource_finish(srf, out_fence);
  833. return ret;
  834. }
  835. /*
  836. * Screen Target CRTC dispatch table
  837. */
  838. static const struct drm_crtc_funcs vmw_stdu_crtc_funcs = {
  839. .gamma_set = vmw_du_crtc_gamma_set,
  840. .destroy = vmw_stdu_crtc_destroy,
  841. .reset = vmw_du_crtc_reset,
  842. .atomic_duplicate_state = vmw_du_crtc_duplicate_state,
  843. .atomic_destroy_state = vmw_du_crtc_destroy_state,
  844. .set_config = vmw_kms_set_config,
  845. .page_flip = vmw_stdu_crtc_page_flip,
  846. };
  847. /******************************************************************************
  848. * Screen Target Display Unit Encoder Functions
  849. *****************************************************************************/
  850. /**
  851. * vmw_stdu_encoder_destroy - cleans up the STDU
  852. *
  853. * @encoder: used the get the containing STDU
  854. *
  855. * vmwgfx cleans up crtc/encoder/connector all at the same time so technically
  856. * this can be a no-op. Nevertheless, it doesn't hurt of have this in case
  857. * the common KMS code changes and somehow vmw_stdu_crtc_destroy() doesn't
  858. * get called.
  859. */
  860. static void vmw_stdu_encoder_destroy(struct drm_encoder *encoder)
  861. {
  862. vmw_stdu_destroy(vmw_encoder_to_stdu(encoder));
  863. }
  864. static const struct drm_encoder_funcs vmw_stdu_encoder_funcs = {
  865. .destroy = vmw_stdu_encoder_destroy,
  866. };
  867. /******************************************************************************
  868. * Screen Target Display Unit Connector Functions
  869. *****************************************************************************/
  870. /**
  871. * vmw_stdu_connector_destroy - cleans up the STDU
  872. *
  873. * @connector: used to get the containing STDU
  874. *
  875. * vmwgfx cleans up crtc/encoder/connector all at the same time so technically
  876. * this can be a no-op. Nevertheless, it doesn't hurt of have this in case
  877. * the common KMS code changes and somehow vmw_stdu_crtc_destroy() doesn't
  878. * get called.
  879. */
  880. static void vmw_stdu_connector_destroy(struct drm_connector *connector)
  881. {
  882. vmw_stdu_destroy(vmw_connector_to_stdu(connector));
  883. }
  884. static const struct drm_connector_funcs vmw_stdu_connector_funcs = {
  885. .dpms = vmw_du_connector_dpms,
  886. .detect = vmw_du_connector_detect,
  887. .fill_modes = vmw_du_connector_fill_modes,
  888. .set_property = vmw_du_connector_set_property,
  889. .destroy = vmw_stdu_connector_destroy,
  890. .reset = vmw_du_connector_reset,
  891. .atomic_duplicate_state = vmw_du_connector_duplicate_state,
  892. .atomic_destroy_state = vmw_du_connector_destroy_state,
  893. .atomic_set_property = vmw_du_connector_atomic_set_property,
  894. .atomic_get_property = vmw_du_connector_atomic_get_property,
  895. };
  896. static const struct
  897. drm_connector_helper_funcs vmw_stdu_connector_helper_funcs = {
  898. .best_encoder = drm_atomic_helper_best_encoder,
  899. };
  900. /******************************************************************************
  901. * Screen Target Display Plane Functions
  902. *****************************************************************************/
  903. /**
  904. * vmw_stdu_primary_plane_cleanup_fb - Unpins the display surface
  905. *
  906. * @plane: display plane
  907. * @old_state: Contains the FB to clean up
  908. *
  909. * Unpins the display surface
  910. *
  911. * Returns 0 on success
  912. */
  913. static void
  914. vmw_stdu_primary_plane_cleanup_fb(struct drm_plane *plane,
  915. struct drm_plane_state *old_state)
  916. {
  917. struct vmw_plane_state *vps = vmw_plane_state_to_vps(old_state);
  918. if (vps->guest_map.virtual)
  919. ttm_bo_kunmap(&vps->guest_map);
  920. if (vps->host_map.virtual)
  921. ttm_bo_kunmap(&vps->host_map);
  922. if (vps->surf)
  923. WARN_ON(!vps->pinned);
  924. vmw_du_plane_cleanup_fb(plane, old_state);
  925. vps->content_fb_type = SAME_AS_DISPLAY;
  926. vps->cpp = 0;
  927. }
  928. /**
  929. * vmw_stdu_primary_plane_prepare_fb - Readies the display surface
  930. *
  931. * @plane: display plane
  932. * @new_state: info on the new plane state, including the FB
  933. *
  934. * This function allocates a new display surface if the content is
  935. * backed by a DMA. The display surface is pinned here, and it'll
  936. * be unpinned in .cleanup_fb()
  937. *
  938. * Returns 0 on success
  939. */
  940. static int
  941. vmw_stdu_primary_plane_prepare_fb(struct drm_plane *plane,
  942. struct drm_plane_state *new_state)
  943. {
  944. struct vmw_private *dev_priv = vmw_priv(plane->dev);
  945. struct drm_framebuffer *new_fb = new_state->fb;
  946. struct vmw_framebuffer *vfb;
  947. struct vmw_plane_state *vps = vmw_plane_state_to_vps(new_state);
  948. enum stdu_content_type new_content_type;
  949. struct vmw_framebuffer_surface *new_vfbs;
  950. struct drm_crtc *crtc = new_state->crtc;
  951. uint32_t hdisplay = new_state->crtc_w, vdisplay = new_state->crtc_h;
  952. int ret;
  953. /* No FB to prepare */
  954. if (!new_fb) {
  955. if (vps->surf) {
  956. WARN_ON(vps->pinned != 0);
  957. vmw_surface_unreference(&vps->surf);
  958. }
  959. return 0;
  960. }
  961. vfb = vmw_framebuffer_to_vfb(new_fb);
  962. new_vfbs = (vfb->dmabuf) ? NULL : vmw_framebuffer_to_vfbs(new_fb);
  963. if (new_vfbs && new_vfbs->surface->base_size.width == hdisplay &&
  964. new_vfbs->surface->base_size.height == vdisplay)
  965. new_content_type = SAME_AS_DISPLAY;
  966. else if (vfb->dmabuf)
  967. new_content_type = SEPARATE_DMA;
  968. else
  969. new_content_type = SEPARATE_SURFACE;
  970. if (new_content_type != SAME_AS_DISPLAY) {
  971. struct vmw_surface content_srf;
  972. struct drm_vmw_size display_base_size = {0};
  973. display_base_size.width = hdisplay;
  974. display_base_size.height = vdisplay;
  975. display_base_size.depth = 1;
  976. /*
  977. * If content buffer is a DMA buf, then we have to construct
  978. * surface info
  979. */
  980. if (new_content_type == SEPARATE_DMA) {
  981. switch (new_fb->format->cpp[0]*8) {
  982. case 32:
  983. content_srf.format = SVGA3D_X8R8G8B8;
  984. break;
  985. case 16:
  986. content_srf.format = SVGA3D_R5G6B5;
  987. break;
  988. case 8:
  989. content_srf.format = SVGA3D_P8;
  990. break;
  991. default:
  992. DRM_ERROR("Invalid format\n");
  993. return -EINVAL;
  994. }
  995. content_srf.flags = 0;
  996. content_srf.mip_levels[0] = 1;
  997. content_srf.multisample_count = 0;
  998. } else {
  999. content_srf = *new_vfbs->surface;
  1000. }
  1001. if (vps->surf) {
  1002. struct drm_vmw_size cur_base_size = vps->surf->base_size;
  1003. if (cur_base_size.width != display_base_size.width ||
  1004. cur_base_size.height != display_base_size.height ||
  1005. vps->surf->format != content_srf.format) {
  1006. WARN_ON(vps->pinned != 0);
  1007. vmw_surface_unreference(&vps->surf);
  1008. }
  1009. }
  1010. if (!vps->surf) {
  1011. ret = vmw_surface_gb_priv_define
  1012. (crtc->dev,
  1013. /* Kernel visible only */
  1014. 0,
  1015. content_srf.flags,
  1016. content_srf.format,
  1017. true, /* a scanout buffer */
  1018. content_srf.mip_levels[0],
  1019. content_srf.multisample_count,
  1020. 0,
  1021. display_base_size,
  1022. &vps->surf);
  1023. if (ret != 0) {
  1024. DRM_ERROR("Couldn't allocate STDU surface.\n");
  1025. return ret;
  1026. }
  1027. }
  1028. } else {
  1029. /*
  1030. * prepare_fb and clean_fb should only take care of pinning
  1031. * and unpinning. References are tracked by state objects.
  1032. * The only time we add a reference in prepare_fb is if the
  1033. * state object doesn't have a reference to begin with
  1034. */
  1035. if (vps->surf) {
  1036. WARN_ON(vps->pinned != 0);
  1037. vmw_surface_unreference(&vps->surf);
  1038. }
  1039. vps->surf = vmw_surface_reference(new_vfbs->surface);
  1040. }
  1041. if (vps->surf) {
  1042. /* Pin new surface before flipping */
  1043. ret = vmw_resource_pin(&vps->surf->res, false);
  1044. if (ret)
  1045. goto out_srf_unref;
  1046. vps->pinned++;
  1047. }
  1048. vps->content_fb_type = new_content_type;
  1049. /*
  1050. * This should only happen if the DMA buf is too large to create a
  1051. * proxy surface for.
  1052. * If we are a 2D VM with a DMA buffer then we have to use CPU blit
  1053. * so cache these mappings
  1054. */
  1055. if (vps->content_fb_type == SEPARATE_DMA &&
  1056. !(dev_priv->capabilities & SVGA_CAP_3D)) {
  1057. struct vmw_framebuffer_dmabuf *new_vfbd;
  1058. new_vfbd = vmw_framebuffer_to_vfbd(new_fb);
  1059. ret = ttm_bo_reserve(&new_vfbd->buffer->base, false, false,
  1060. NULL);
  1061. if (ret)
  1062. goto out_srf_unpin;
  1063. ret = ttm_bo_kmap(&new_vfbd->buffer->base, 0,
  1064. new_vfbd->buffer->base.num_pages,
  1065. &vps->guest_map);
  1066. ttm_bo_unreserve(&new_vfbd->buffer->base);
  1067. if (ret) {
  1068. DRM_ERROR("Failed to map content buffer to CPU\n");
  1069. goto out_srf_unpin;
  1070. }
  1071. ret = ttm_bo_kmap(&vps->surf->res.backup->base, 0,
  1072. vps->surf->res.backup->base.num_pages,
  1073. &vps->host_map);
  1074. if (ret) {
  1075. DRM_ERROR("Failed to map display buffer to CPU\n");
  1076. ttm_bo_kunmap(&vps->guest_map);
  1077. goto out_srf_unpin;
  1078. }
  1079. vps->cpp = new_fb->pitches[0] / new_fb->width;
  1080. }
  1081. return 0;
  1082. out_srf_unpin:
  1083. vmw_resource_unpin(&vps->surf->res);
  1084. vps->pinned--;
  1085. out_srf_unref:
  1086. vmw_surface_unreference(&vps->surf);
  1087. return ret;
  1088. }
  1089. /**
  1090. * vmw_stdu_primary_plane_atomic_update - formally switches STDU to new plane
  1091. *
  1092. * @plane: display plane
  1093. * @old_state: Only used to get crtc info
  1094. *
  1095. * Formally update stdu->display_srf to the new plane, and bind the new
  1096. * plane STDU. This function is called during the commit phase when
  1097. * all the preparation have been done and all the configurations have
  1098. * been checked.
  1099. */
  1100. static void
  1101. vmw_stdu_primary_plane_atomic_update(struct drm_plane *plane,
  1102. struct drm_plane_state *old_state)
  1103. {
  1104. struct vmw_private *dev_priv;
  1105. struct vmw_screen_target_display_unit *stdu;
  1106. struct vmw_plane_state *vps = vmw_plane_state_to_vps(plane->state);
  1107. struct drm_crtc *crtc = plane->state->crtc ?: old_state->crtc;
  1108. int ret;
  1109. stdu = vmw_crtc_to_stdu(crtc);
  1110. dev_priv = vmw_priv(crtc->dev);
  1111. stdu->display_srf = vps->surf;
  1112. stdu->content_fb_type = vps->content_fb_type;
  1113. stdu->cpp = vps->cpp;
  1114. memcpy(&stdu->guest_map, &vps->guest_map, sizeof(vps->guest_map));
  1115. memcpy(&stdu->host_map, &vps->host_map, sizeof(vps->host_map));
  1116. if (!stdu->defined)
  1117. return;
  1118. if (plane->state->fb)
  1119. ret = vmw_stdu_bind_st(dev_priv, stdu, &stdu->display_srf->res);
  1120. else
  1121. ret = vmw_stdu_bind_st(dev_priv, stdu, NULL);
  1122. /*
  1123. * We cannot really fail this function, so if we do, then output an
  1124. * error and quit
  1125. */
  1126. if (ret)
  1127. DRM_ERROR("Failed to bind surface to STDU.\n");
  1128. else
  1129. crtc->primary->fb = plane->state->fb;
  1130. ret = vmw_stdu_update_st(dev_priv, stdu);
  1131. if (ret)
  1132. DRM_ERROR("Failed to update STDU.\n");
  1133. }
  1134. static const struct drm_plane_funcs vmw_stdu_plane_funcs = {
  1135. .update_plane = drm_atomic_helper_update_plane,
  1136. .disable_plane = drm_atomic_helper_disable_plane,
  1137. .destroy = vmw_du_primary_plane_destroy,
  1138. .reset = vmw_du_plane_reset,
  1139. .atomic_duplicate_state = vmw_du_plane_duplicate_state,
  1140. .atomic_destroy_state = vmw_du_plane_destroy_state,
  1141. };
  1142. static const struct drm_plane_funcs vmw_stdu_cursor_funcs = {
  1143. .update_plane = drm_atomic_helper_update_plane,
  1144. .disable_plane = drm_atomic_helper_disable_plane,
  1145. .destroy = vmw_du_cursor_plane_destroy,
  1146. .reset = vmw_du_plane_reset,
  1147. .atomic_duplicate_state = vmw_du_plane_duplicate_state,
  1148. .atomic_destroy_state = vmw_du_plane_destroy_state,
  1149. };
  1150. /*
  1151. * Atomic Helpers
  1152. */
  1153. static const struct
  1154. drm_plane_helper_funcs vmw_stdu_cursor_plane_helper_funcs = {
  1155. .atomic_check = vmw_du_cursor_plane_atomic_check,
  1156. .atomic_update = vmw_du_cursor_plane_atomic_update,
  1157. .prepare_fb = vmw_du_cursor_plane_prepare_fb,
  1158. .cleanup_fb = vmw_du_plane_cleanup_fb,
  1159. };
  1160. static const struct
  1161. drm_plane_helper_funcs vmw_stdu_primary_plane_helper_funcs = {
  1162. .atomic_check = vmw_du_primary_plane_atomic_check,
  1163. .atomic_update = vmw_stdu_primary_plane_atomic_update,
  1164. .prepare_fb = vmw_stdu_primary_plane_prepare_fb,
  1165. .cleanup_fb = vmw_stdu_primary_plane_cleanup_fb,
  1166. };
  1167. static const struct drm_crtc_helper_funcs vmw_stdu_crtc_helper_funcs = {
  1168. .prepare = vmw_stdu_crtc_helper_prepare,
  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. .atomic_enable = vmw_stdu_crtc_atomic_enable,
  1174. .atomic_disable = vmw_stdu_crtc_atomic_disable,
  1175. };
  1176. /**
  1177. * vmw_stdu_init - Sets up a Screen Target Display Unit
  1178. *
  1179. * @dev_priv: VMW DRM device
  1180. * @unit: unit number range from 0 to VMWGFX_NUM_DISPLAY_UNITS
  1181. *
  1182. * This function is called once per CRTC, and allocates one Screen Target
  1183. * display unit to represent that CRTC. Since the SVGA device does not separate
  1184. * out encoder and connector, they are represented as part of the STDU as well.
  1185. */
  1186. static int vmw_stdu_init(struct vmw_private *dev_priv, unsigned unit)
  1187. {
  1188. struct vmw_screen_target_display_unit *stdu;
  1189. struct drm_device *dev = dev_priv->dev;
  1190. struct drm_connector *connector;
  1191. struct drm_encoder *encoder;
  1192. struct drm_plane *primary, *cursor;
  1193. struct drm_crtc *crtc;
  1194. int ret;
  1195. stdu = kzalloc(sizeof(*stdu), GFP_KERNEL);
  1196. if (!stdu)
  1197. return -ENOMEM;
  1198. stdu->base.unit = unit;
  1199. crtc = &stdu->base.crtc;
  1200. encoder = &stdu->base.encoder;
  1201. connector = &stdu->base.connector;
  1202. primary = &stdu->base.primary;
  1203. cursor = &stdu->base.cursor;
  1204. stdu->base.pref_active = (unit == 0);
  1205. stdu->base.pref_width = dev_priv->initial_width;
  1206. stdu->base.pref_height = dev_priv->initial_height;
  1207. /*
  1208. * Remove this after enabling atomic because property values can
  1209. * only exist in a state object
  1210. */
  1211. stdu->base.is_implicit = false;
  1212. /* Initialize primary plane */
  1213. vmw_du_plane_reset(primary);
  1214. ret = drm_universal_plane_init(dev, primary,
  1215. 0, &vmw_stdu_plane_funcs,
  1216. vmw_primary_plane_formats,
  1217. ARRAY_SIZE(vmw_primary_plane_formats),
  1218. NULL, DRM_PLANE_TYPE_PRIMARY, NULL);
  1219. if (ret) {
  1220. DRM_ERROR("Failed to initialize primary plane");
  1221. goto err_free;
  1222. }
  1223. drm_plane_helper_add(primary, &vmw_stdu_primary_plane_helper_funcs);
  1224. /* Initialize cursor plane */
  1225. vmw_du_plane_reset(cursor);
  1226. ret = drm_universal_plane_init(dev, cursor,
  1227. 0, &vmw_stdu_cursor_funcs,
  1228. vmw_cursor_plane_formats,
  1229. ARRAY_SIZE(vmw_cursor_plane_formats),
  1230. NULL, DRM_PLANE_TYPE_CURSOR, NULL);
  1231. if (ret) {
  1232. DRM_ERROR("Failed to initialize cursor plane");
  1233. drm_plane_cleanup(&stdu->base.primary);
  1234. goto err_free;
  1235. }
  1236. drm_plane_helper_add(cursor, &vmw_stdu_cursor_plane_helper_funcs);
  1237. vmw_du_connector_reset(connector);
  1238. ret = drm_connector_init(dev, connector, &vmw_stdu_connector_funcs,
  1239. DRM_MODE_CONNECTOR_VIRTUAL);
  1240. if (ret) {
  1241. DRM_ERROR("Failed to initialize connector\n");
  1242. goto err_free;
  1243. }
  1244. drm_connector_helper_add(connector, &vmw_stdu_connector_helper_funcs);
  1245. connector->status = vmw_du_connector_detect(connector, false);
  1246. vmw_connector_state_to_vcs(connector->state)->is_implicit = false;
  1247. ret = drm_encoder_init(dev, encoder, &vmw_stdu_encoder_funcs,
  1248. DRM_MODE_ENCODER_VIRTUAL, NULL);
  1249. if (ret) {
  1250. DRM_ERROR("Failed to initialize encoder\n");
  1251. goto err_free_connector;
  1252. }
  1253. (void) drm_mode_connector_attach_encoder(connector, encoder);
  1254. encoder->possible_crtcs = (1 << unit);
  1255. encoder->possible_clones = 0;
  1256. ret = drm_connector_register(connector);
  1257. if (ret) {
  1258. DRM_ERROR("Failed to register connector\n");
  1259. goto err_free_encoder;
  1260. }
  1261. vmw_du_crtc_reset(crtc);
  1262. ret = drm_crtc_init_with_planes(dev, crtc, &stdu->base.primary,
  1263. &stdu->base.cursor,
  1264. &vmw_stdu_crtc_funcs, NULL);
  1265. if (ret) {
  1266. DRM_ERROR("Failed to initialize CRTC\n");
  1267. goto err_free_unregister;
  1268. }
  1269. drm_crtc_helper_add(crtc, &vmw_stdu_crtc_helper_funcs);
  1270. drm_mode_crtc_set_gamma_size(crtc, 256);
  1271. drm_object_attach_property(&connector->base,
  1272. dev_priv->hotplug_mode_update_property, 1);
  1273. drm_object_attach_property(&connector->base,
  1274. dev->mode_config.suggested_x_property, 0);
  1275. drm_object_attach_property(&connector->base,
  1276. dev->mode_config.suggested_y_property, 0);
  1277. if (dev_priv->implicit_placement_property)
  1278. drm_object_attach_property
  1279. (&connector->base,
  1280. dev_priv->implicit_placement_property,
  1281. stdu->base.is_implicit);
  1282. return 0;
  1283. err_free_unregister:
  1284. drm_connector_unregister(connector);
  1285. err_free_encoder:
  1286. drm_encoder_cleanup(encoder);
  1287. err_free_connector:
  1288. drm_connector_cleanup(connector);
  1289. err_free:
  1290. kfree(stdu);
  1291. return ret;
  1292. }
  1293. /**
  1294. * vmw_stdu_destroy - Cleans up a vmw_screen_target_display_unit
  1295. *
  1296. * @stdu: Screen Target Display Unit to be destroyed
  1297. *
  1298. * Clean up after vmw_stdu_init
  1299. */
  1300. static void vmw_stdu_destroy(struct vmw_screen_target_display_unit *stdu)
  1301. {
  1302. vmw_du_cleanup(&stdu->base);
  1303. kfree(stdu);
  1304. }
  1305. /******************************************************************************
  1306. * Screen Target Display KMS Functions
  1307. *
  1308. * These functions are called by the common KMS code in vmwgfx_kms.c
  1309. *****************************************************************************/
  1310. /**
  1311. * vmw_kms_stdu_init_display - Initializes a Screen Target based display
  1312. *
  1313. * @dev_priv: VMW DRM device
  1314. *
  1315. * This function initialize a Screen Target based display device. It checks
  1316. * the capability bits to make sure the underlying hardware can support
  1317. * screen targets, and then creates the maximum number of CRTCs, a.k.a Display
  1318. * Units, as supported by the display hardware.
  1319. *
  1320. * RETURNS:
  1321. * 0 on success, error code otherwise
  1322. */
  1323. int vmw_kms_stdu_init_display(struct vmw_private *dev_priv)
  1324. {
  1325. struct drm_device *dev = dev_priv->dev;
  1326. int i, ret;
  1327. /* Do nothing if Screen Target support is turned off */
  1328. if (!VMWGFX_ENABLE_SCREEN_TARGET_OTABLE)
  1329. return -ENOSYS;
  1330. if (!(dev_priv->capabilities & SVGA_CAP_GBOBJECTS))
  1331. return -ENOSYS;
  1332. ret = drm_vblank_init(dev, VMWGFX_NUM_DISPLAY_UNITS);
  1333. if (unlikely(ret != 0))
  1334. return ret;
  1335. dev_priv->active_display_unit = vmw_du_screen_target;
  1336. if (dev_priv->capabilities & SVGA_CAP_3D) {
  1337. /*
  1338. * For 3D VMs, display (scanout) buffer size is the smaller of
  1339. * max texture and max STDU
  1340. */
  1341. uint32_t max_width, max_height;
  1342. max_width = min(dev_priv->texture_max_width,
  1343. dev_priv->stdu_max_width);
  1344. max_height = min(dev_priv->texture_max_height,
  1345. dev_priv->stdu_max_height);
  1346. dev->mode_config.max_width = max_width;
  1347. dev->mode_config.max_height = max_height;
  1348. } else {
  1349. /*
  1350. * Given various display aspect ratios, there's no way to
  1351. * estimate these using prim_bb_mem. So just set these to
  1352. * something arbitrarily large and we will reject any layout
  1353. * that doesn't fit prim_bb_mem later
  1354. */
  1355. dev->mode_config.max_width = 8192;
  1356. dev->mode_config.max_height = 8192;
  1357. }
  1358. vmw_kms_create_implicit_placement_property(dev_priv, false);
  1359. for (i = 0; i < VMWGFX_NUM_DISPLAY_UNITS; ++i) {
  1360. ret = vmw_stdu_init(dev_priv, i);
  1361. if (unlikely(ret != 0)) {
  1362. DRM_ERROR("Failed to initialize STDU %d", i);
  1363. return ret;
  1364. }
  1365. }
  1366. DRM_INFO("Screen Target Display device initialized\n");
  1367. return 0;
  1368. }