vmwgfx_stdu.c 45 KB

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