vmwgfx_stdu.c 45 KB

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