vmwgfx_scrn.c 38 KB

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