vmwgfx_scrn.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362
  1. // SPDX-License-Identifier: GPL-2.0 OR MIT
  2. /**************************************************************************
  3. *
  4. * Copyright 2011-2022 VMware, Inc., Palo Alto, CA., USA
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sub license, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice (including the
  15. * next paragraph) shall be included in all copies or substantial portions
  16. * of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  21. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  22. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  23. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  24. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. *
  26. **************************************************************************/
  27. #include <drm/drm_atomic.h>
  28. #include <drm/drm_atomic_helper.h>
  29. #include <drm/drm_damage_helper.h>
  30. #include <drm/drm_fourcc.h>
  31. #include "vmwgfx_kms.h"
  32. #define vmw_crtc_to_sou(x) \
  33. container_of(x, struct vmw_screen_object_unit, base.crtc)
  34. #define vmw_encoder_to_sou(x) \
  35. container_of(x, struct vmw_screen_object_unit, base.encoder)
  36. #define vmw_connector_to_sou(x) \
  37. container_of(x, struct vmw_screen_object_unit, base.connector)
  38. /**
  39. * struct vmw_kms_sou_surface_dirty - Closure structure for
  40. * blit surface to screen command.
  41. * @base: The base type we derive from. Used by vmw_kms_helper_dirty().
  42. * @left: Left side of bounding box.
  43. * @right: Right side of bounding box.
  44. * @top: Top side of bounding box.
  45. * @bottom: Bottom side of bounding box.
  46. * @dst_x: Difference between source clip rects and framebuffer coordinates.
  47. * @dst_y: Difference between source clip rects and framebuffer coordinates.
  48. * @sid: Surface id of surface to copy from.
  49. */
  50. struct vmw_kms_sou_surface_dirty {
  51. struct vmw_kms_dirty base;
  52. s32 left, right, top, bottom;
  53. s32 dst_x, dst_y;
  54. u32 sid;
  55. };
  56. /*
  57. * SVGA commands that are used by this code. Please see the device headers
  58. * for explanation.
  59. */
  60. struct vmw_kms_sou_readback_blit {
  61. uint32 header;
  62. SVGAFifoCmdBlitScreenToGMRFB body;
  63. };
  64. struct vmw_kms_sou_bo_blit {
  65. uint32 header;
  66. SVGAFifoCmdBlitGMRFBToScreen body;
  67. };
  68. struct vmw_kms_sou_dirty_cmd {
  69. SVGA3dCmdHeader header;
  70. SVGA3dCmdBlitSurfaceToScreen body;
  71. };
  72. struct vmw_kms_sou_define_gmrfb {
  73. uint32_t header;
  74. SVGAFifoCmdDefineGMRFB body;
  75. };
  76. /*
  77. * Display unit using screen objects.
  78. */
  79. struct vmw_screen_object_unit {
  80. struct vmw_display_unit base;
  81. unsigned long buffer_size; /**< Size of allocated buffer */
  82. struct vmw_buffer_object *buffer; /**< Backing store buffer */
  83. bool defined;
  84. };
  85. static void vmw_sou_destroy(struct vmw_screen_object_unit *sou)
  86. {
  87. vmw_du_cleanup(&sou->base);
  88. kfree(sou);
  89. }
  90. /*
  91. * Screen Object Display Unit CRTC functions
  92. */
  93. static void vmw_sou_crtc_destroy(struct drm_crtc *crtc)
  94. {
  95. vmw_sou_destroy(vmw_crtc_to_sou(crtc));
  96. }
  97. /*
  98. * Send the fifo command to create a screen.
  99. */
  100. static int vmw_sou_fifo_create(struct vmw_private *dev_priv,
  101. struct vmw_screen_object_unit *sou,
  102. int x, int y,
  103. struct drm_display_mode *mode)
  104. {
  105. size_t fifo_size;
  106. struct {
  107. struct {
  108. uint32_t cmdType;
  109. } header;
  110. SVGAScreenObject obj;
  111. } *cmd;
  112. BUG_ON(!sou->buffer);
  113. fifo_size = sizeof(*cmd);
  114. cmd = VMW_CMD_RESERVE(dev_priv, fifo_size);
  115. if (unlikely(cmd == NULL))
  116. return -ENOMEM;
  117. memset(cmd, 0, fifo_size);
  118. cmd->header.cmdType = SVGA_CMD_DEFINE_SCREEN;
  119. cmd->obj.structSize = sizeof(SVGAScreenObject);
  120. cmd->obj.id = sou->base.unit;
  121. cmd->obj.flags = SVGA_SCREEN_HAS_ROOT |
  122. (sou->base.unit == 0 ? SVGA_SCREEN_IS_PRIMARY : 0);
  123. cmd->obj.size.width = mode->hdisplay;
  124. cmd->obj.size.height = mode->vdisplay;
  125. cmd->obj.root.x = x;
  126. cmd->obj.root.y = y;
  127. sou->base.set_gui_x = cmd->obj.root.x;
  128. sou->base.set_gui_y = cmd->obj.root.y;
  129. /* Ok to assume that buffer is pinned in vram */
  130. vmw_bo_get_guest_ptr(&sou->buffer->base, &cmd->obj.backingStore.ptr);
  131. cmd->obj.backingStore.pitch = mode->hdisplay * 4;
  132. vmw_cmd_commit(dev_priv, fifo_size);
  133. sou->defined = true;
  134. return 0;
  135. }
  136. /*
  137. * Send the fifo command to destroy a screen.
  138. */
  139. static int vmw_sou_fifo_destroy(struct vmw_private *dev_priv,
  140. struct vmw_screen_object_unit *sou)
  141. {
  142. size_t fifo_size;
  143. int ret;
  144. struct {
  145. struct {
  146. uint32_t cmdType;
  147. } header;
  148. SVGAFifoCmdDestroyScreen body;
  149. } *cmd;
  150. /* no need to do anything */
  151. if (unlikely(!sou->defined))
  152. return 0;
  153. fifo_size = sizeof(*cmd);
  154. cmd = VMW_CMD_RESERVE(dev_priv, fifo_size);
  155. if (unlikely(cmd == NULL))
  156. return -ENOMEM;
  157. memset(cmd, 0, fifo_size);
  158. cmd->header.cmdType = SVGA_CMD_DESTROY_SCREEN;
  159. cmd->body.screenId = sou->base.unit;
  160. vmw_cmd_commit(dev_priv, fifo_size);
  161. /* Force sync */
  162. ret = vmw_fallback_wait(dev_priv, false, true, 0, false, 3*HZ);
  163. if (unlikely(ret != 0))
  164. DRM_ERROR("Failed to sync with HW");
  165. else
  166. sou->defined = false;
  167. return ret;
  168. }
  169. /**
  170. * vmw_sou_crtc_mode_set_nofb - Create new screen
  171. *
  172. * @crtc: CRTC associated with the new screen
  173. *
  174. * This function creates/destroys a screen. This function cannot fail, so if
  175. * somehow we run into a failure, just do the best we can to get out.
  176. */
  177. static void vmw_sou_crtc_mode_set_nofb(struct drm_crtc *crtc)
  178. {
  179. struct vmw_private *dev_priv;
  180. struct vmw_screen_object_unit *sou;
  181. struct vmw_framebuffer *vfb;
  182. struct drm_framebuffer *fb;
  183. struct drm_plane_state *ps;
  184. struct vmw_plane_state *vps;
  185. int ret;
  186. sou = vmw_crtc_to_sou(crtc);
  187. dev_priv = vmw_priv(crtc->dev);
  188. ps = crtc->primary->state;
  189. fb = ps->fb;
  190. vps = vmw_plane_state_to_vps(ps);
  191. vfb = (fb) ? vmw_framebuffer_to_vfb(fb) : NULL;
  192. if (sou->defined) {
  193. ret = vmw_sou_fifo_destroy(dev_priv, sou);
  194. if (ret) {
  195. DRM_ERROR("Failed to destroy Screen Object\n");
  196. return;
  197. }
  198. }
  199. if (vfb) {
  200. struct drm_connector_state *conn_state;
  201. struct vmw_connector_state *vmw_conn_state;
  202. int x, y;
  203. sou->buffer = vps->bo;
  204. sou->buffer_size = vps->bo_size;
  205. conn_state = sou->base.connector.state;
  206. vmw_conn_state = vmw_connector_state_to_vcs(conn_state);
  207. x = vmw_conn_state->gui_x;
  208. y = vmw_conn_state->gui_y;
  209. ret = vmw_sou_fifo_create(dev_priv, sou, x, y, &crtc->mode);
  210. if (ret)
  211. DRM_ERROR("Failed to define Screen Object %dx%d\n",
  212. crtc->x, crtc->y);
  213. } else {
  214. sou->buffer = NULL;
  215. sou->buffer_size = 0;
  216. }
  217. }
  218. /**
  219. * vmw_sou_crtc_helper_prepare - Noop
  220. *
  221. * @crtc: CRTC associated with the new screen
  222. *
  223. * Prepares the CRTC for a mode set, but we don't need to do anything here.
  224. */
  225. static void vmw_sou_crtc_helper_prepare(struct drm_crtc *crtc)
  226. {
  227. }
  228. /**
  229. * vmw_sou_crtc_atomic_enable - Noop
  230. *
  231. * @crtc: CRTC associated with the new screen
  232. * @state: Unused
  233. *
  234. * This is called after a mode set has been completed.
  235. */
  236. static void vmw_sou_crtc_atomic_enable(struct drm_crtc *crtc,
  237. struct drm_atomic_state *state)
  238. {
  239. }
  240. /**
  241. * vmw_sou_crtc_atomic_disable - Turns off CRTC
  242. *
  243. * @crtc: CRTC to be turned off
  244. * @state: Unused
  245. */
  246. static void vmw_sou_crtc_atomic_disable(struct drm_crtc *crtc,
  247. struct drm_atomic_state *state)
  248. {
  249. struct vmw_private *dev_priv;
  250. struct vmw_screen_object_unit *sou;
  251. int ret;
  252. if (!crtc) {
  253. DRM_ERROR("CRTC is NULL\n");
  254. return;
  255. }
  256. sou = vmw_crtc_to_sou(crtc);
  257. dev_priv = vmw_priv(crtc->dev);
  258. if (sou->defined) {
  259. ret = vmw_sou_fifo_destroy(dev_priv, sou);
  260. if (ret)
  261. DRM_ERROR("Failed to destroy Screen Object\n");
  262. }
  263. }
  264. static const struct drm_crtc_funcs vmw_screen_object_crtc_funcs = {
  265. .gamma_set = vmw_du_crtc_gamma_set,
  266. .destroy = vmw_sou_crtc_destroy,
  267. .reset = vmw_du_crtc_reset,
  268. .atomic_duplicate_state = vmw_du_crtc_duplicate_state,
  269. .atomic_destroy_state = vmw_du_crtc_destroy_state,
  270. .set_config = drm_atomic_helper_set_config,
  271. .page_flip = drm_atomic_helper_page_flip,
  272. };
  273. /*
  274. * Screen Object Display Unit encoder functions
  275. */
  276. static void vmw_sou_encoder_destroy(struct drm_encoder *encoder)
  277. {
  278. vmw_sou_destroy(vmw_encoder_to_sou(encoder));
  279. }
  280. static const struct drm_encoder_funcs vmw_screen_object_encoder_funcs = {
  281. .destroy = vmw_sou_encoder_destroy,
  282. };
  283. /*
  284. * Screen Object Display Unit connector functions
  285. */
  286. static void vmw_sou_connector_destroy(struct drm_connector *connector)
  287. {
  288. vmw_sou_destroy(vmw_connector_to_sou(connector));
  289. }
  290. static const struct drm_connector_funcs vmw_sou_connector_funcs = {
  291. .dpms = vmw_du_connector_dpms,
  292. .detect = vmw_du_connector_detect,
  293. .fill_modes = vmw_du_connector_fill_modes,
  294. .destroy = vmw_sou_connector_destroy,
  295. .reset = vmw_du_connector_reset,
  296. .atomic_duplicate_state = vmw_du_connector_duplicate_state,
  297. .atomic_destroy_state = vmw_du_connector_destroy_state,
  298. };
  299. static const struct
  300. drm_connector_helper_funcs vmw_sou_connector_helper_funcs = {
  301. };
  302. /*
  303. * Screen Object Display Plane Functions
  304. */
  305. /**
  306. * vmw_sou_primary_plane_cleanup_fb - Frees sou backing buffer
  307. *
  308. * @plane: display plane
  309. * @old_state: Contains the FB to clean up
  310. *
  311. * Unpins the display surface
  312. *
  313. * Returns 0 on success
  314. */
  315. static void
  316. vmw_sou_primary_plane_cleanup_fb(struct drm_plane *plane,
  317. struct drm_plane_state *old_state)
  318. {
  319. struct vmw_plane_state *vps = vmw_plane_state_to_vps(old_state);
  320. struct drm_crtc *crtc = plane->state->crtc ?
  321. plane->state->crtc : old_state->crtc;
  322. if (vps->bo)
  323. vmw_bo_unpin(vmw_priv(crtc->dev), vps->bo, false);
  324. vmw_bo_unreference(&vps->bo);
  325. vps->bo_size = 0;
  326. vmw_du_plane_cleanup_fb(plane, old_state);
  327. }
  328. /**
  329. * vmw_sou_primary_plane_prepare_fb - allocate backing buffer
  330. *
  331. * @plane: display plane
  332. * @new_state: info on the new plane state, including the FB
  333. *
  334. * The SOU backing buffer is our equivalent of the display plane.
  335. *
  336. * Returns 0 on success
  337. */
  338. static int
  339. vmw_sou_primary_plane_prepare_fb(struct drm_plane *plane,
  340. struct drm_plane_state *new_state)
  341. {
  342. struct drm_framebuffer *new_fb = new_state->fb;
  343. struct drm_crtc *crtc = plane->state->crtc ?: new_state->crtc;
  344. struct vmw_plane_state *vps = vmw_plane_state_to_vps(new_state);
  345. struct vmw_private *dev_priv;
  346. size_t size;
  347. int ret;
  348. if (!new_fb) {
  349. vmw_bo_unreference(&vps->bo);
  350. vps->bo_size = 0;
  351. return 0;
  352. }
  353. size = new_state->crtc_w * new_state->crtc_h * 4;
  354. dev_priv = vmw_priv(crtc->dev);
  355. if (vps->bo) {
  356. if (vps->bo_size == size) {
  357. /*
  358. * Note that this might temporarily up the pin-count
  359. * to 2, until cleanup_fb() is called.
  360. */
  361. return vmw_bo_pin_in_vram(dev_priv, vps->bo,
  362. true);
  363. }
  364. vmw_bo_unreference(&vps->bo);
  365. vps->bo_size = 0;
  366. }
  367. vmw_svga_enable(dev_priv);
  368. /* After we have alloced the backing store might not be able to
  369. * resume the overlays, this is preferred to failing to alloc.
  370. */
  371. vmw_overlay_pause_all(dev_priv);
  372. ret = vmw_bo_create(dev_priv, size,
  373. &vmw_vram_placement,
  374. false, true, &vmw_bo_bo_free, &vps->bo);
  375. vmw_overlay_resume_all(dev_priv);
  376. if (ret) {
  377. vps->bo = NULL; /* vmw_bo_init frees on error */
  378. return ret;
  379. }
  380. vps->bo_size = size;
  381. /*
  382. * TTM already thinks the buffer is pinned, but make sure the
  383. * pin_count is upped.
  384. */
  385. return vmw_bo_pin_in_vram(dev_priv, vps->bo, true);
  386. }
  387. static uint32_t vmw_sou_bo_fifo_size(struct vmw_du_update_plane *update,
  388. uint32_t num_hits)
  389. {
  390. return sizeof(struct vmw_kms_sou_define_gmrfb) +
  391. sizeof(struct vmw_kms_sou_bo_blit) * num_hits;
  392. }
  393. static uint32_t vmw_sou_bo_define_gmrfb(struct vmw_du_update_plane *update,
  394. void *cmd)
  395. {
  396. struct vmw_framebuffer_bo *vfbbo =
  397. container_of(update->vfb, typeof(*vfbbo), base);
  398. struct vmw_kms_sou_define_gmrfb *gmr = cmd;
  399. int depth = update->vfb->base.format->depth;
  400. /* Emulate RGBA support, contrary to svga_reg.h this is not
  401. * supported by hosts. This is only a problem if we are reading
  402. * this value later and expecting what we uploaded back.
  403. */
  404. if (depth == 32)
  405. depth = 24;
  406. gmr->header = SVGA_CMD_DEFINE_GMRFB;
  407. gmr->body.format.bitsPerPixel = update->vfb->base.format->cpp[0] * 8;
  408. gmr->body.format.colorDepth = depth;
  409. gmr->body.format.reserved = 0;
  410. gmr->body.bytesPerLine = update->vfb->base.pitches[0];
  411. vmw_bo_get_guest_ptr(&vfbbo->buffer->base, &gmr->body.ptr);
  412. return sizeof(*gmr);
  413. }
  414. static uint32_t vmw_sou_bo_populate_clip(struct vmw_du_update_plane *update,
  415. void *cmd, struct drm_rect *clip,
  416. uint32_t fb_x, uint32_t fb_y)
  417. {
  418. struct vmw_kms_sou_bo_blit *blit = cmd;
  419. blit->header = SVGA_CMD_BLIT_GMRFB_TO_SCREEN;
  420. blit->body.destScreenId = update->du->unit;
  421. blit->body.srcOrigin.x = fb_x;
  422. blit->body.srcOrigin.y = fb_y;
  423. blit->body.destRect.left = clip->x1;
  424. blit->body.destRect.top = clip->y1;
  425. blit->body.destRect.right = clip->x2;
  426. blit->body.destRect.bottom = clip->y2;
  427. return sizeof(*blit);
  428. }
  429. static uint32_t vmw_stud_bo_post_clip(struct vmw_du_update_plane *update,
  430. void *cmd, struct drm_rect *bb)
  431. {
  432. return 0;
  433. }
  434. /**
  435. * vmw_sou_plane_update_bo - Update display unit for bo backed fb.
  436. * @dev_priv: Device private.
  437. * @plane: Plane state.
  438. * @old_state: Old plane state.
  439. * @vfb: Framebuffer which is blitted to display unit.
  440. * @out_fence: If non-NULL, will return a ref-counted pointer to vmw_fence_obj.
  441. * The returned fence pointer may be NULL in which case the device
  442. * has already synchronized.
  443. *
  444. * Return: 0 on success or a negative error code on failure.
  445. */
  446. static int vmw_sou_plane_update_bo(struct vmw_private *dev_priv,
  447. struct drm_plane *plane,
  448. struct drm_plane_state *old_state,
  449. struct vmw_framebuffer *vfb,
  450. struct vmw_fence_obj **out_fence)
  451. {
  452. struct vmw_du_update_plane_buffer bo_update;
  453. memset(&bo_update, 0, sizeof(struct vmw_du_update_plane_buffer));
  454. bo_update.base.plane = plane;
  455. bo_update.base.old_state = old_state;
  456. bo_update.base.dev_priv = dev_priv;
  457. bo_update.base.du = vmw_crtc_to_du(plane->state->crtc);
  458. bo_update.base.vfb = vfb;
  459. bo_update.base.out_fence = out_fence;
  460. bo_update.base.mutex = NULL;
  461. bo_update.base.cpu_blit = false;
  462. bo_update.base.intr = true;
  463. bo_update.base.calc_fifo_size = vmw_sou_bo_fifo_size;
  464. bo_update.base.post_prepare = vmw_sou_bo_define_gmrfb;
  465. bo_update.base.clip = vmw_sou_bo_populate_clip;
  466. bo_update.base.post_clip = vmw_stud_bo_post_clip;
  467. return vmw_du_helper_plane_update(&bo_update.base);
  468. }
  469. static uint32_t vmw_sou_surface_fifo_size(struct vmw_du_update_plane *update,
  470. uint32_t num_hits)
  471. {
  472. return sizeof(struct vmw_kms_sou_dirty_cmd) + sizeof(SVGASignedRect) *
  473. num_hits;
  474. }
  475. static uint32_t vmw_sou_surface_post_prepare(struct vmw_du_update_plane *update,
  476. void *cmd)
  477. {
  478. struct vmw_du_update_plane_surface *srf_update;
  479. srf_update = container_of(update, typeof(*srf_update), base);
  480. /*
  481. * SOU SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN is special in the sense that
  482. * its bounding box is filled before iterating over all the clips. So
  483. * store the FIFO start address and revisit to fill the details.
  484. */
  485. srf_update->cmd_start = cmd;
  486. return 0;
  487. }
  488. static uint32_t vmw_sou_surface_pre_clip(struct vmw_du_update_plane *update,
  489. void *cmd, uint32_t num_hits)
  490. {
  491. struct vmw_kms_sou_dirty_cmd *blit = cmd;
  492. struct vmw_framebuffer_surface *vfbs;
  493. vfbs = container_of(update->vfb, typeof(*vfbs), base);
  494. blit->header.id = SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN;
  495. blit->header.size = sizeof(blit->body) + sizeof(SVGASignedRect) *
  496. num_hits;
  497. blit->body.srcImage.sid = vfbs->surface->res.id;
  498. blit->body.destScreenId = update->du->unit;
  499. /* Update the source and destination bounding box later in post_clip */
  500. blit->body.srcRect.left = 0;
  501. blit->body.srcRect.top = 0;
  502. blit->body.srcRect.right = 0;
  503. blit->body.srcRect.bottom = 0;
  504. blit->body.destRect.left = 0;
  505. blit->body.destRect.top = 0;
  506. blit->body.destRect.right = 0;
  507. blit->body.destRect.bottom = 0;
  508. return sizeof(*blit);
  509. }
  510. static uint32_t vmw_sou_surface_clip_rect(struct vmw_du_update_plane *update,
  511. void *cmd, struct drm_rect *clip,
  512. uint32_t src_x, uint32_t src_y)
  513. {
  514. SVGASignedRect *rect = cmd;
  515. /*
  516. * rects are relative to dest bounding box rect on screen object, so
  517. * translate to it later in post_clip
  518. */
  519. rect->left = clip->x1;
  520. rect->top = clip->y1;
  521. rect->right = clip->x2;
  522. rect->bottom = clip->y2;
  523. return sizeof(*rect);
  524. }
  525. static uint32_t vmw_sou_surface_post_clip(struct vmw_du_update_plane *update,
  526. void *cmd, struct drm_rect *bb)
  527. {
  528. struct vmw_du_update_plane_surface *srf_update;
  529. struct drm_plane_state *state = update->plane->state;
  530. struct drm_rect src_bb;
  531. struct vmw_kms_sou_dirty_cmd *blit;
  532. SVGASignedRect *rect;
  533. uint32_t num_hits;
  534. int translate_src_x;
  535. int translate_src_y;
  536. int i;
  537. srf_update = container_of(update, typeof(*srf_update), base);
  538. blit = srf_update->cmd_start;
  539. rect = (SVGASignedRect *)&blit[1];
  540. num_hits = (blit->header.size - sizeof(blit->body))/
  541. sizeof(SVGASignedRect);
  542. src_bb = *bb;
  543. /* To translate bb back to fb src coord */
  544. translate_src_x = (state->src_x >> 16) - state->crtc_x;
  545. translate_src_y = (state->src_y >> 16) - state->crtc_y;
  546. drm_rect_translate(&src_bb, translate_src_x, translate_src_y);
  547. blit->body.srcRect.left = src_bb.x1;
  548. blit->body.srcRect.top = src_bb.y1;
  549. blit->body.srcRect.right = src_bb.x2;
  550. blit->body.srcRect.bottom = src_bb.y2;
  551. blit->body.destRect.left = bb->x1;
  552. blit->body.destRect.top = bb->y1;
  553. blit->body.destRect.right = bb->x2;
  554. blit->body.destRect.bottom = bb->y2;
  555. /* rects are relative to dest bb rect */
  556. for (i = 0; i < num_hits; i++) {
  557. rect->left -= bb->x1;
  558. rect->top -= bb->y1;
  559. rect->right -= bb->x1;
  560. rect->bottom -= bb->y1;
  561. rect++;
  562. }
  563. return 0;
  564. }
  565. /**
  566. * vmw_sou_plane_update_surface - Update display unit for surface backed fb.
  567. * @dev_priv: Device private.
  568. * @plane: Plane state.
  569. * @old_state: Old plane state.
  570. * @vfb: Framebuffer which is blitted to display unit
  571. * @out_fence: If non-NULL, will return a ref-counted pointer to vmw_fence_obj.
  572. * The returned fence pointer may be NULL in which case the device
  573. * has already synchronized.
  574. *
  575. * Return: 0 on success or a negative error code on failure.
  576. */
  577. static int vmw_sou_plane_update_surface(struct vmw_private *dev_priv,
  578. struct drm_plane *plane,
  579. struct drm_plane_state *old_state,
  580. struct vmw_framebuffer *vfb,
  581. struct vmw_fence_obj **out_fence)
  582. {
  583. struct vmw_du_update_plane_surface srf_update;
  584. memset(&srf_update, 0, sizeof(struct vmw_du_update_plane_surface));
  585. srf_update.base.plane = plane;
  586. srf_update.base.old_state = old_state;
  587. srf_update.base.dev_priv = dev_priv;
  588. srf_update.base.du = vmw_crtc_to_du(plane->state->crtc);
  589. srf_update.base.vfb = vfb;
  590. srf_update.base.out_fence = out_fence;
  591. srf_update.base.mutex = &dev_priv->cmdbuf_mutex;
  592. srf_update.base.cpu_blit = false;
  593. srf_update.base.intr = true;
  594. srf_update.base.calc_fifo_size = vmw_sou_surface_fifo_size;
  595. srf_update.base.post_prepare = vmw_sou_surface_post_prepare;
  596. srf_update.base.pre_clip = vmw_sou_surface_pre_clip;
  597. srf_update.base.clip = vmw_sou_surface_clip_rect;
  598. srf_update.base.post_clip = vmw_sou_surface_post_clip;
  599. return vmw_du_helper_plane_update(&srf_update.base);
  600. }
  601. static void
  602. vmw_sou_primary_plane_atomic_update(struct drm_plane *plane,
  603. struct drm_atomic_state *state)
  604. {
  605. struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state, plane);
  606. struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state, plane);
  607. struct drm_crtc *crtc = new_state->crtc;
  608. struct vmw_fence_obj *fence = NULL;
  609. int ret;
  610. /* In case of device error, maintain consistent atomic state */
  611. if (crtc && new_state->fb) {
  612. struct vmw_private *dev_priv = vmw_priv(crtc->dev);
  613. struct vmw_framebuffer *vfb =
  614. vmw_framebuffer_to_vfb(new_state->fb);
  615. if (vfb->bo)
  616. ret = vmw_sou_plane_update_bo(dev_priv, plane,
  617. old_state, vfb, &fence);
  618. else
  619. ret = vmw_sou_plane_update_surface(dev_priv, plane,
  620. old_state, vfb,
  621. &fence);
  622. if (ret != 0)
  623. DRM_ERROR("Failed to update screen.\n");
  624. } else {
  625. /* Do nothing when fb and crtc is NULL (blank crtc) */
  626. return;
  627. }
  628. if (fence)
  629. vmw_fence_obj_unreference(&fence);
  630. }
  631. static const struct drm_plane_funcs vmw_sou_plane_funcs = {
  632. .update_plane = drm_atomic_helper_update_plane,
  633. .disable_plane = drm_atomic_helper_disable_plane,
  634. .destroy = vmw_du_primary_plane_destroy,
  635. .reset = vmw_du_plane_reset,
  636. .atomic_duplicate_state = vmw_du_plane_duplicate_state,
  637. .atomic_destroy_state = vmw_du_plane_destroy_state,
  638. };
  639. static const struct drm_plane_funcs vmw_sou_cursor_funcs = {
  640. .update_plane = drm_atomic_helper_update_plane,
  641. .disable_plane = drm_atomic_helper_disable_plane,
  642. .destroy = vmw_du_cursor_plane_destroy,
  643. .reset = vmw_du_plane_reset,
  644. .atomic_duplicate_state = vmw_du_plane_duplicate_state,
  645. .atomic_destroy_state = vmw_du_plane_destroy_state,
  646. };
  647. /*
  648. * Atomic Helpers
  649. */
  650. static const struct
  651. drm_plane_helper_funcs vmw_sou_cursor_plane_helper_funcs = {
  652. .atomic_check = vmw_du_cursor_plane_atomic_check,
  653. .atomic_update = vmw_du_cursor_plane_atomic_update,
  654. .prepare_fb = vmw_du_cursor_plane_prepare_fb,
  655. .cleanup_fb = vmw_du_cursor_plane_cleanup_fb,
  656. };
  657. static const struct
  658. drm_plane_helper_funcs vmw_sou_primary_plane_helper_funcs = {
  659. .atomic_check = vmw_du_primary_plane_atomic_check,
  660. .atomic_update = vmw_sou_primary_plane_atomic_update,
  661. .prepare_fb = vmw_sou_primary_plane_prepare_fb,
  662. .cleanup_fb = vmw_sou_primary_plane_cleanup_fb,
  663. };
  664. static const struct drm_crtc_helper_funcs vmw_sou_crtc_helper_funcs = {
  665. .prepare = vmw_sou_crtc_helper_prepare,
  666. .mode_set_nofb = vmw_sou_crtc_mode_set_nofb,
  667. .atomic_check = vmw_du_crtc_atomic_check,
  668. .atomic_begin = vmw_du_crtc_atomic_begin,
  669. .atomic_flush = vmw_du_crtc_atomic_flush,
  670. .atomic_enable = vmw_sou_crtc_atomic_enable,
  671. .atomic_disable = vmw_sou_crtc_atomic_disable,
  672. };
  673. static int vmw_sou_init(struct vmw_private *dev_priv, unsigned unit)
  674. {
  675. struct vmw_screen_object_unit *sou;
  676. struct drm_device *dev = &dev_priv->drm;
  677. struct drm_connector *connector;
  678. struct drm_encoder *encoder;
  679. struct drm_plane *primary;
  680. struct vmw_cursor_plane *cursor;
  681. struct drm_crtc *crtc;
  682. int ret;
  683. sou = kzalloc(sizeof(*sou), GFP_KERNEL);
  684. if (!sou)
  685. return -ENOMEM;
  686. sou->base.unit = unit;
  687. crtc = &sou->base.crtc;
  688. encoder = &sou->base.encoder;
  689. connector = &sou->base.connector;
  690. primary = &sou->base.primary;
  691. cursor = &sou->base.cursor;
  692. sou->base.pref_active = (unit == 0);
  693. sou->base.pref_width = dev_priv->initial_width;
  694. sou->base.pref_height = dev_priv->initial_height;
  695. sou->base.pref_mode = NULL;
  696. /*
  697. * Remove this after enabling atomic because property values can
  698. * only exist in a state object
  699. */
  700. sou->base.is_implicit = false;
  701. /* Initialize primary plane */
  702. ret = drm_universal_plane_init(dev, primary,
  703. 0, &vmw_sou_plane_funcs,
  704. vmw_primary_plane_formats,
  705. ARRAY_SIZE(vmw_primary_plane_formats),
  706. NULL, DRM_PLANE_TYPE_PRIMARY, NULL);
  707. if (ret) {
  708. DRM_ERROR("Failed to initialize primary plane");
  709. goto err_free;
  710. }
  711. drm_plane_helper_add(primary, &vmw_sou_primary_plane_helper_funcs);
  712. drm_plane_enable_fb_damage_clips(primary);
  713. /* Initialize cursor plane */
  714. ret = drm_universal_plane_init(dev, &cursor->base,
  715. 0, &vmw_sou_cursor_funcs,
  716. vmw_cursor_plane_formats,
  717. ARRAY_SIZE(vmw_cursor_plane_formats),
  718. NULL, DRM_PLANE_TYPE_CURSOR, NULL);
  719. if (ret) {
  720. DRM_ERROR("Failed to initialize cursor plane");
  721. drm_plane_cleanup(&sou->base.primary);
  722. goto err_free;
  723. }
  724. drm_plane_helper_add(&cursor->base, &vmw_sou_cursor_plane_helper_funcs);
  725. ret = drm_connector_init(dev, connector, &vmw_sou_connector_funcs,
  726. DRM_MODE_CONNECTOR_VIRTUAL);
  727. if (ret) {
  728. DRM_ERROR("Failed to initialize connector\n");
  729. goto err_free;
  730. }
  731. drm_connector_helper_add(connector, &vmw_sou_connector_helper_funcs);
  732. connector->status = vmw_du_connector_detect(connector, true);
  733. ret = drm_encoder_init(dev, encoder, &vmw_screen_object_encoder_funcs,
  734. DRM_MODE_ENCODER_VIRTUAL, NULL);
  735. if (ret) {
  736. DRM_ERROR("Failed to initialize encoder\n");
  737. goto err_free_connector;
  738. }
  739. (void) drm_connector_attach_encoder(connector, encoder);
  740. encoder->possible_crtcs = (1 << unit);
  741. encoder->possible_clones = 0;
  742. ret = drm_connector_register(connector);
  743. if (ret) {
  744. DRM_ERROR("Failed to register connector\n");
  745. goto err_free_encoder;
  746. }
  747. ret = drm_crtc_init_with_planes(dev, crtc, primary,
  748. &cursor->base,
  749. &vmw_screen_object_crtc_funcs, NULL);
  750. if (ret) {
  751. DRM_ERROR("Failed to initialize CRTC\n");
  752. goto err_free_unregister;
  753. }
  754. drm_crtc_helper_add(crtc, &vmw_sou_crtc_helper_funcs);
  755. drm_mode_crtc_set_gamma_size(crtc, 256);
  756. drm_object_attach_property(&connector->base,
  757. dev_priv->hotplug_mode_update_property, 1);
  758. drm_object_attach_property(&connector->base,
  759. dev->mode_config.suggested_x_property, 0);
  760. drm_object_attach_property(&connector->base,
  761. dev->mode_config.suggested_y_property, 0);
  762. return 0;
  763. err_free_unregister:
  764. drm_connector_unregister(connector);
  765. err_free_encoder:
  766. drm_encoder_cleanup(encoder);
  767. err_free_connector:
  768. drm_connector_cleanup(connector);
  769. err_free:
  770. kfree(sou);
  771. return ret;
  772. }
  773. int vmw_kms_sou_init_display(struct vmw_private *dev_priv)
  774. {
  775. struct drm_device *dev = &dev_priv->drm;
  776. int i;
  777. /* Screen objects won't work if GMR's aren't available */
  778. if (!dev_priv->has_gmr)
  779. return -ENOSYS;
  780. if (!(dev_priv->capabilities & SVGA_CAP_SCREEN_OBJECT_2)) {
  781. return -ENOSYS;
  782. }
  783. for (i = 0; i < VMWGFX_NUM_DISPLAY_UNITS; ++i)
  784. vmw_sou_init(dev_priv, i);
  785. dev_priv->active_display_unit = vmw_du_screen_object;
  786. drm_mode_config_reset(dev);
  787. return 0;
  788. }
  789. static int do_bo_define_gmrfb(struct vmw_private *dev_priv,
  790. struct vmw_framebuffer *framebuffer)
  791. {
  792. struct vmw_buffer_object *buf =
  793. container_of(framebuffer, struct vmw_framebuffer_bo,
  794. base)->buffer;
  795. int depth = framebuffer->base.format->depth;
  796. struct {
  797. uint32_t header;
  798. SVGAFifoCmdDefineGMRFB body;
  799. } *cmd;
  800. /* Emulate RGBA support, contrary to svga_reg.h this is not
  801. * supported by hosts. This is only a problem if we are reading
  802. * this value later and expecting what we uploaded back.
  803. */
  804. if (depth == 32)
  805. depth = 24;
  806. cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd));
  807. if (!cmd)
  808. return -ENOMEM;
  809. cmd->header = SVGA_CMD_DEFINE_GMRFB;
  810. cmd->body.format.bitsPerPixel = framebuffer->base.format->cpp[0] * 8;
  811. cmd->body.format.colorDepth = depth;
  812. cmd->body.format.reserved = 0;
  813. cmd->body.bytesPerLine = framebuffer->base.pitches[0];
  814. /* Buffer is reserved in vram or GMR */
  815. vmw_bo_get_guest_ptr(&buf->base, &cmd->body.ptr);
  816. vmw_cmd_commit(dev_priv, sizeof(*cmd));
  817. return 0;
  818. }
  819. /**
  820. * vmw_sou_surface_fifo_commit - Callback to fill in and submit a
  821. * blit surface to screen command.
  822. *
  823. * @dirty: The closure structure.
  824. *
  825. * Fills in the missing fields in the command, and translates the cliprects
  826. * to match the destination bounding box encoded.
  827. */
  828. static void vmw_sou_surface_fifo_commit(struct vmw_kms_dirty *dirty)
  829. {
  830. struct vmw_kms_sou_surface_dirty *sdirty =
  831. container_of(dirty, typeof(*sdirty), base);
  832. struct vmw_kms_sou_dirty_cmd *cmd = dirty->cmd;
  833. s32 trans_x = dirty->unit->crtc.x - sdirty->dst_x;
  834. s32 trans_y = dirty->unit->crtc.y - sdirty->dst_y;
  835. size_t region_size = dirty->num_hits * sizeof(SVGASignedRect);
  836. SVGASignedRect *blit = (SVGASignedRect *) &cmd[1];
  837. int i;
  838. if (!dirty->num_hits) {
  839. vmw_cmd_commit(dirty->dev_priv, 0);
  840. return;
  841. }
  842. cmd->header.id = SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN;
  843. cmd->header.size = sizeof(cmd->body) + region_size;
  844. /*
  845. * Use the destination bounding box to specify destination - and
  846. * source bounding regions.
  847. */
  848. cmd->body.destRect.left = sdirty->left;
  849. cmd->body.destRect.right = sdirty->right;
  850. cmd->body.destRect.top = sdirty->top;
  851. cmd->body.destRect.bottom = sdirty->bottom;
  852. cmd->body.srcRect.left = sdirty->left + trans_x;
  853. cmd->body.srcRect.right = sdirty->right + trans_x;
  854. cmd->body.srcRect.top = sdirty->top + trans_y;
  855. cmd->body.srcRect.bottom = sdirty->bottom + trans_y;
  856. cmd->body.srcImage.sid = sdirty->sid;
  857. cmd->body.destScreenId = dirty->unit->unit;
  858. /* Blits are relative to the destination rect. Translate. */
  859. for (i = 0; i < dirty->num_hits; ++i, ++blit) {
  860. blit->left -= sdirty->left;
  861. blit->right -= sdirty->left;
  862. blit->top -= sdirty->top;
  863. blit->bottom -= sdirty->top;
  864. }
  865. vmw_cmd_commit(dirty->dev_priv, region_size + sizeof(*cmd));
  866. sdirty->left = sdirty->top = S32_MAX;
  867. sdirty->right = sdirty->bottom = S32_MIN;
  868. }
  869. /**
  870. * vmw_sou_surface_clip - Callback to encode a blit surface to screen cliprect.
  871. *
  872. * @dirty: The closure structure
  873. *
  874. * Encodes a SVGASignedRect cliprect and updates the bounding box of the
  875. * BLIT_SURFACE_TO_SCREEN command.
  876. */
  877. static void vmw_sou_surface_clip(struct vmw_kms_dirty *dirty)
  878. {
  879. struct vmw_kms_sou_surface_dirty *sdirty =
  880. container_of(dirty, typeof(*sdirty), base);
  881. struct vmw_kms_sou_dirty_cmd *cmd = dirty->cmd;
  882. SVGASignedRect *blit = (SVGASignedRect *) &cmd[1];
  883. /* Destination rect. */
  884. blit += dirty->num_hits;
  885. blit->left = dirty->unit_x1;
  886. blit->top = dirty->unit_y1;
  887. blit->right = dirty->unit_x2;
  888. blit->bottom = dirty->unit_y2;
  889. /* Destination bounding box */
  890. sdirty->left = min_t(s32, sdirty->left, dirty->unit_x1);
  891. sdirty->top = min_t(s32, sdirty->top, dirty->unit_y1);
  892. sdirty->right = max_t(s32, sdirty->right, dirty->unit_x2);
  893. sdirty->bottom = max_t(s32, sdirty->bottom, dirty->unit_y2);
  894. dirty->num_hits++;
  895. }
  896. /**
  897. * vmw_kms_sou_do_surface_dirty - Dirty part of a surface backed framebuffer
  898. *
  899. * @dev_priv: Pointer to the device private structure.
  900. * @framebuffer: Pointer to the surface-buffer backed framebuffer.
  901. * @clips: Array of clip rects. Either @clips or @vclips must be NULL.
  902. * @vclips: Alternate array of clip rects. Either @clips or @vclips must
  903. * be NULL.
  904. * @srf: Pointer to surface to blit from. If NULL, the surface attached
  905. * to @framebuffer will be used.
  906. * @dest_x: X coordinate offset to align @srf with framebuffer coordinates.
  907. * @dest_y: Y coordinate offset to align @srf with framebuffer coordinates.
  908. * @num_clips: Number of clip rects in @clips.
  909. * @inc: Increment to use when looping over @clips.
  910. * @out_fence: If non-NULL, will return a ref-counted pointer to a
  911. * struct vmw_fence_obj. The returned fence pointer may be NULL in which
  912. * case the device has already synchronized.
  913. * @crtc: If crtc is passed, perform surface dirty on that crtc only.
  914. *
  915. * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
  916. * interrupted.
  917. */
  918. int vmw_kms_sou_do_surface_dirty(struct vmw_private *dev_priv,
  919. struct vmw_framebuffer *framebuffer,
  920. struct drm_clip_rect *clips,
  921. struct drm_vmw_rect *vclips,
  922. struct vmw_resource *srf,
  923. s32 dest_x,
  924. s32 dest_y,
  925. unsigned num_clips, int inc,
  926. struct vmw_fence_obj **out_fence,
  927. struct drm_crtc *crtc)
  928. {
  929. struct vmw_framebuffer_surface *vfbs =
  930. container_of(framebuffer, typeof(*vfbs), base);
  931. struct vmw_kms_sou_surface_dirty sdirty;
  932. DECLARE_VAL_CONTEXT(val_ctx, NULL, 0);
  933. int ret;
  934. if (!srf)
  935. srf = &vfbs->surface->res;
  936. ret = vmw_validation_add_resource(&val_ctx, srf, 0, VMW_RES_DIRTY_NONE,
  937. NULL, NULL);
  938. if (ret)
  939. return ret;
  940. ret = vmw_validation_prepare(&val_ctx, &dev_priv->cmdbuf_mutex, true);
  941. if (ret)
  942. goto out_unref;
  943. sdirty.base.fifo_commit = vmw_sou_surface_fifo_commit;
  944. sdirty.base.clip = vmw_sou_surface_clip;
  945. sdirty.base.dev_priv = dev_priv;
  946. sdirty.base.fifo_reserve_size = sizeof(struct vmw_kms_sou_dirty_cmd) +
  947. sizeof(SVGASignedRect) * num_clips;
  948. sdirty.base.crtc = crtc;
  949. sdirty.sid = srf->id;
  950. sdirty.left = sdirty.top = S32_MAX;
  951. sdirty.right = sdirty.bottom = S32_MIN;
  952. sdirty.dst_x = dest_x;
  953. sdirty.dst_y = dest_y;
  954. ret = vmw_kms_helper_dirty(dev_priv, framebuffer, clips, vclips,
  955. dest_x, dest_y, num_clips, inc,
  956. &sdirty.base);
  957. vmw_kms_helper_validation_finish(dev_priv, NULL, &val_ctx, out_fence,
  958. NULL);
  959. return ret;
  960. out_unref:
  961. vmw_validation_unref_lists(&val_ctx);
  962. return ret;
  963. }
  964. /**
  965. * vmw_sou_bo_fifo_commit - Callback to submit a set of readback clips.
  966. *
  967. * @dirty: The closure structure.
  968. *
  969. * Commits a previously built command buffer of readback clips.
  970. */
  971. static void vmw_sou_bo_fifo_commit(struct vmw_kms_dirty *dirty)
  972. {
  973. if (!dirty->num_hits) {
  974. vmw_cmd_commit(dirty->dev_priv, 0);
  975. return;
  976. }
  977. vmw_cmd_commit(dirty->dev_priv,
  978. sizeof(struct vmw_kms_sou_bo_blit) *
  979. dirty->num_hits);
  980. }
  981. /**
  982. * vmw_sou_bo_clip - Callback to encode a readback cliprect.
  983. *
  984. * @dirty: The closure structure
  985. *
  986. * Encodes a BLIT_GMRFB_TO_SCREEN cliprect.
  987. */
  988. static void vmw_sou_bo_clip(struct vmw_kms_dirty *dirty)
  989. {
  990. struct vmw_kms_sou_bo_blit *blit = dirty->cmd;
  991. blit += dirty->num_hits;
  992. blit->header = SVGA_CMD_BLIT_GMRFB_TO_SCREEN;
  993. blit->body.destScreenId = dirty->unit->unit;
  994. blit->body.srcOrigin.x = dirty->fb_x;
  995. blit->body.srcOrigin.y = dirty->fb_y;
  996. blit->body.destRect.left = dirty->unit_x1;
  997. blit->body.destRect.top = dirty->unit_y1;
  998. blit->body.destRect.right = dirty->unit_x2;
  999. blit->body.destRect.bottom = dirty->unit_y2;
  1000. dirty->num_hits++;
  1001. }
  1002. /**
  1003. * vmw_kms_sou_do_bo_dirty - Dirty part of a buffer-object backed framebuffer
  1004. *
  1005. * @dev_priv: Pointer to the device private structure.
  1006. * @framebuffer: Pointer to the buffer-object backed framebuffer.
  1007. * @clips: Array of clip rects.
  1008. * @vclips: Alternate array of clip rects. Either @clips or @vclips must
  1009. * be NULL.
  1010. * @num_clips: Number of clip rects in @clips.
  1011. * @increment: Increment to use when looping over @clips.
  1012. * @interruptible: Whether to perform waits interruptible if possible.
  1013. * @out_fence: If non-NULL, will return a ref-counted pointer to a
  1014. * struct vmw_fence_obj. The returned fence pointer may be NULL in which
  1015. * case the device has already synchronized.
  1016. * @crtc: If crtc is passed, perform bo dirty on that crtc only.
  1017. *
  1018. * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
  1019. * interrupted.
  1020. */
  1021. int vmw_kms_sou_do_bo_dirty(struct vmw_private *dev_priv,
  1022. struct vmw_framebuffer *framebuffer,
  1023. struct drm_clip_rect *clips,
  1024. struct drm_vmw_rect *vclips,
  1025. unsigned num_clips, int increment,
  1026. bool interruptible,
  1027. struct vmw_fence_obj **out_fence,
  1028. struct drm_crtc *crtc)
  1029. {
  1030. struct vmw_buffer_object *buf =
  1031. container_of(framebuffer, struct vmw_framebuffer_bo,
  1032. base)->buffer;
  1033. struct vmw_kms_dirty dirty;
  1034. DECLARE_VAL_CONTEXT(val_ctx, NULL, 0);
  1035. int ret;
  1036. ret = vmw_validation_add_bo(&val_ctx, buf, false, false);
  1037. if (ret)
  1038. return ret;
  1039. ret = vmw_validation_prepare(&val_ctx, NULL, interruptible);
  1040. if (ret)
  1041. goto out_unref;
  1042. ret = do_bo_define_gmrfb(dev_priv, framebuffer);
  1043. if (unlikely(ret != 0))
  1044. goto out_revert;
  1045. dirty.crtc = crtc;
  1046. dirty.fifo_commit = vmw_sou_bo_fifo_commit;
  1047. dirty.clip = vmw_sou_bo_clip;
  1048. dirty.fifo_reserve_size = sizeof(struct vmw_kms_sou_bo_blit) *
  1049. num_clips;
  1050. ret = vmw_kms_helper_dirty(dev_priv, framebuffer, clips, vclips,
  1051. 0, 0, num_clips, increment, &dirty);
  1052. vmw_kms_helper_validation_finish(dev_priv, NULL, &val_ctx, out_fence,
  1053. NULL);
  1054. return ret;
  1055. out_revert:
  1056. vmw_validation_revert(&val_ctx);
  1057. out_unref:
  1058. vmw_validation_unref_lists(&val_ctx);
  1059. return ret;
  1060. }
  1061. /**
  1062. * vmw_sou_readback_fifo_commit - Callback to submit a set of readback clips.
  1063. *
  1064. * @dirty: The closure structure.
  1065. *
  1066. * Commits a previously built command buffer of readback clips.
  1067. */
  1068. static void vmw_sou_readback_fifo_commit(struct vmw_kms_dirty *dirty)
  1069. {
  1070. if (!dirty->num_hits) {
  1071. vmw_cmd_commit(dirty->dev_priv, 0);
  1072. return;
  1073. }
  1074. vmw_cmd_commit(dirty->dev_priv,
  1075. sizeof(struct vmw_kms_sou_readback_blit) *
  1076. dirty->num_hits);
  1077. }
  1078. /**
  1079. * vmw_sou_readback_clip - Callback to encode a readback cliprect.
  1080. *
  1081. * @dirty: The closure structure
  1082. *
  1083. * Encodes a BLIT_SCREEN_TO_GMRFB cliprect.
  1084. */
  1085. static void vmw_sou_readback_clip(struct vmw_kms_dirty *dirty)
  1086. {
  1087. struct vmw_kms_sou_readback_blit *blit = dirty->cmd;
  1088. blit += dirty->num_hits;
  1089. blit->header = SVGA_CMD_BLIT_SCREEN_TO_GMRFB;
  1090. blit->body.srcScreenId = dirty->unit->unit;
  1091. blit->body.destOrigin.x = dirty->fb_x;
  1092. blit->body.destOrigin.y = dirty->fb_y;
  1093. blit->body.srcRect.left = dirty->unit_x1;
  1094. blit->body.srcRect.top = dirty->unit_y1;
  1095. blit->body.srcRect.right = dirty->unit_x2;
  1096. blit->body.srcRect.bottom = dirty->unit_y2;
  1097. dirty->num_hits++;
  1098. }
  1099. /**
  1100. * vmw_kms_sou_readback - Perform a readback from the screen object system to
  1101. * a buffer-object backed framebuffer.
  1102. *
  1103. * @dev_priv: Pointer to the device private structure.
  1104. * @file_priv: Pointer to a struct drm_file identifying the caller.
  1105. * Must be set to NULL if @user_fence_rep is NULL.
  1106. * @vfb: Pointer to the buffer-object backed framebuffer.
  1107. * @user_fence_rep: User-space provided structure for fence information.
  1108. * Must be set to non-NULL if @file_priv is non-NULL.
  1109. * @vclips: Array of clip rects.
  1110. * @num_clips: Number of clip rects in @vclips.
  1111. * @crtc: If crtc is passed, readback on that crtc only.
  1112. *
  1113. * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
  1114. * interrupted.
  1115. */
  1116. int vmw_kms_sou_readback(struct vmw_private *dev_priv,
  1117. struct drm_file *file_priv,
  1118. struct vmw_framebuffer *vfb,
  1119. struct drm_vmw_fence_rep __user *user_fence_rep,
  1120. struct drm_vmw_rect *vclips,
  1121. uint32_t num_clips,
  1122. struct drm_crtc *crtc)
  1123. {
  1124. struct vmw_buffer_object *buf =
  1125. container_of(vfb, struct vmw_framebuffer_bo, base)->buffer;
  1126. struct vmw_kms_dirty dirty;
  1127. DECLARE_VAL_CONTEXT(val_ctx, NULL, 0);
  1128. int ret;
  1129. ret = vmw_validation_add_bo(&val_ctx, buf, false, false);
  1130. if (ret)
  1131. return ret;
  1132. ret = vmw_validation_prepare(&val_ctx, NULL, true);
  1133. if (ret)
  1134. goto out_unref;
  1135. ret = do_bo_define_gmrfb(dev_priv, vfb);
  1136. if (unlikely(ret != 0))
  1137. goto out_revert;
  1138. dirty.crtc = crtc;
  1139. dirty.fifo_commit = vmw_sou_readback_fifo_commit;
  1140. dirty.clip = vmw_sou_readback_clip;
  1141. dirty.fifo_reserve_size = sizeof(struct vmw_kms_sou_readback_blit) *
  1142. num_clips;
  1143. ret = vmw_kms_helper_dirty(dev_priv, vfb, NULL, vclips,
  1144. 0, 0, num_clips, 1, &dirty);
  1145. vmw_kms_helper_validation_finish(dev_priv, file_priv, &val_ctx, NULL,
  1146. user_fence_rep);
  1147. return ret;
  1148. out_revert:
  1149. vmw_validation_revert(&val_ctx);
  1150. out_unref:
  1151. vmw_validation_unref_lists(&val_ctx);
  1152. return ret;
  1153. }