vmwgfx_stdu.c 49 KB

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