vmwgfx_stdu.c 45 KB

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