vmwgfx_scrn.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365
  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_bo_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_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_cursor_plane_atomic_check,
  652. .atomic_update = vmw_cursor_plane_atomic_update,
  653. .prepare_fb = vmw_cursor_plane_prepare_fb,
  654. .cleanup_fb = vmw_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_obj(*sou);
  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. ret = drm_encoder_init(dev, encoder, &vmw_screen_object_encoder_funcs,
  731. DRM_MODE_ENCODER_VIRTUAL, NULL);
  732. if (ret) {
  733. DRM_ERROR("Failed to initialize encoder\n");
  734. goto err_free_connector;
  735. }
  736. (void) drm_connector_attach_encoder(connector, encoder);
  737. encoder->possible_crtcs = (1 << unit);
  738. encoder->possible_clones = 0;
  739. ret = drm_connector_register(connector);
  740. if (ret) {
  741. DRM_ERROR("Failed to register connector\n");
  742. goto err_free_encoder;
  743. }
  744. ret = drm_crtc_init_with_planes(dev, crtc, primary,
  745. &cursor->base,
  746. &vmw_screen_object_crtc_funcs, NULL);
  747. if (ret) {
  748. DRM_ERROR("Failed to initialize CRTC\n");
  749. goto err_free_unregister;
  750. }
  751. drm_crtc_helper_add(crtc, &vmw_sou_crtc_helper_funcs);
  752. drm_mode_crtc_set_gamma_size(crtc, 256);
  753. drm_object_attach_property(&connector->base,
  754. dev_priv->hotplug_mode_update_property, 1);
  755. drm_object_attach_property(&connector->base,
  756. dev->mode_config.suggested_x_property, 0);
  757. drm_object_attach_property(&connector->base,
  758. dev->mode_config.suggested_y_property, 0);
  759. vmw_du_init(&sou->base);
  760. return 0;
  761. err_free_unregister:
  762. drm_connector_unregister(connector);
  763. err_free_encoder:
  764. drm_encoder_cleanup(encoder);
  765. err_free_connector:
  766. drm_connector_cleanup(connector);
  767. err_free:
  768. kfree(sou);
  769. return ret;
  770. }
  771. int vmw_kms_sou_init_display(struct vmw_private *dev_priv)
  772. {
  773. struct drm_device *dev = &dev_priv->drm;
  774. int i;
  775. /* Screen objects won't work if GMR's aren't available */
  776. if (!dev_priv->has_gmr)
  777. return -ENOSYS;
  778. if (!(dev_priv->capabilities & SVGA_CAP_SCREEN_OBJECT_2)) {
  779. return -ENOSYS;
  780. }
  781. for (i = 0; i < VMWGFX_NUM_DISPLAY_UNITS; ++i)
  782. vmw_sou_init(dev_priv, i);
  783. dev_priv->active_display_unit = vmw_du_screen_object;
  784. drm_mode_config_reset(dev);
  785. return 0;
  786. }
  787. static int do_bo_define_gmrfb(struct vmw_private *dev_priv,
  788. struct vmw_framebuffer *framebuffer)
  789. {
  790. struct vmw_bo *buf =
  791. container_of(framebuffer, struct vmw_framebuffer_bo,
  792. base)->buffer;
  793. int depth = framebuffer->base.format->depth;
  794. struct {
  795. uint32_t header;
  796. SVGAFifoCmdDefineGMRFB body;
  797. } *cmd;
  798. /* Emulate RGBA support, contrary to svga_reg.h this is not
  799. * supported by hosts. This is only a problem if we are reading
  800. * this value later and expecting what we uploaded back.
  801. */
  802. if (depth == 32)
  803. depth = 24;
  804. cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd));
  805. if (!cmd)
  806. return -ENOMEM;
  807. cmd->header = SVGA_CMD_DEFINE_GMRFB;
  808. cmd->body.format.bitsPerPixel = framebuffer->base.format->cpp[0] * 8;
  809. cmd->body.format.colorDepth = depth;
  810. cmd->body.format.reserved = 0;
  811. cmd->body.bytesPerLine = framebuffer->base.pitches[0];
  812. /* Buffer is reserved in vram or GMR */
  813. vmw_bo_get_guest_ptr(&buf->tbo, &cmd->body.ptr);
  814. vmw_cmd_commit(dev_priv, sizeof(*cmd));
  815. return 0;
  816. }
  817. /**
  818. * vmw_sou_surface_fifo_commit - Callback to fill in and submit a
  819. * blit surface to screen command.
  820. *
  821. * @dirty: The closure structure.
  822. *
  823. * Fills in the missing fields in the command, and translates the cliprects
  824. * to match the destination bounding box encoded.
  825. */
  826. static void vmw_sou_surface_fifo_commit(struct vmw_kms_dirty *dirty)
  827. {
  828. struct vmw_kms_sou_surface_dirty *sdirty =
  829. container_of(dirty, typeof(*sdirty), base);
  830. struct vmw_kms_sou_dirty_cmd *cmd = dirty->cmd;
  831. s32 trans_x = dirty->unit->crtc.x - sdirty->dst_x;
  832. s32 trans_y = dirty->unit->crtc.y - sdirty->dst_y;
  833. size_t region_size = dirty->num_hits * sizeof(SVGASignedRect);
  834. SVGASignedRect *blit = (SVGASignedRect *) &cmd[1];
  835. int i;
  836. if (!dirty->num_hits) {
  837. vmw_cmd_commit(dirty->dev_priv, 0);
  838. return;
  839. }
  840. cmd->header.id = SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN;
  841. cmd->header.size = sizeof(cmd->body) + region_size;
  842. /*
  843. * Use the destination bounding box to specify destination - and
  844. * source bounding regions.
  845. */
  846. cmd->body.destRect.left = sdirty->left;
  847. cmd->body.destRect.right = sdirty->right;
  848. cmd->body.destRect.top = sdirty->top;
  849. cmd->body.destRect.bottom = sdirty->bottom;
  850. cmd->body.srcRect.left = sdirty->left + trans_x;
  851. cmd->body.srcRect.right = sdirty->right + trans_x;
  852. cmd->body.srcRect.top = sdirty->top + trans_y;
  853. cmd->body.srcRect.bottom = sdirty->bottom + trans_y;
  854. cmd->body.srcImage.sid = sdirty->sid;
  855. cmd->body.destScreenId = dirty->unit->unit;
  856. /* Blits are relative to the destination rect. Translate. */
  857. for (i = 0; i < dirty->num_hits; ++i, ++blit) {
  858. blit->left -= sdirty->left;
  859. blit->right -= sdirty->left;
  860. blit->top -= sdirty->top;
  861. blit->bottom -= sdirty->top;
  862. }
  863. vmw_cmd_commit(dirty->dev_priv, region_size + sizeof(*cmd));
  864. sdirty->left = sdirty->top = S32_MAX;
  865. sdirty->right = sdirty->bottom = S32_MIN;
  866. }
  867. /**
  868. * vmw_sou_surface_clip - Callback to encode a blit surface to screen cliprect.
  869. *
  870. * @dirty: The closure structure
  871. *
  872. * Encodes a SVGASignedRect cliprect and updates the bounding box of the
  873. * BLIT_SURFACE_TO_SCREEN command.
  874. */
  875. static void vmw_sou_surface_clip(struct vmw_kms_dirty *dirty)
  876. {
  877. struct vmw_kms_sou_surface_dirty *sdirty =
  878. container_of(dirty, typeof(*sdirty), base);
  879. struct vmw_kms_sou_dirty_cmd *cmd = dirty->cmd;
  880. SVGASignedRect *blit = (SVGASignedRect *) &cmd[1];
  881. /* Destination rect. */
  882. blit += dirty->num_hits;
  883. blit->left = dirty->unit_x1;
  884. blit->top = dirty->unit_y1;
  885. blit->right = dirty->unit_x2;
  886. blit->bottom = dirty->unit_y2;
  887. /* Destination bounding box */
  888. sdirty->left = min_t(s32, sdirty->left, dirty->unit_x1);
  889. sdirty->top = min_t(s32, sdirty->top, dirty->unit_y1);
  890. sdirty->right = max_t(s32, sdirty->right, dirty->unit_x2);
  891. sdirty->bottom = max_t(s32, sdirty->bottom, dirty->unit_y2);
  892. dirty->num_hits++;
  893. }
  894. /**
  895. * vmw_kms_sou_do_surface_dirty - Dirty part of a surface backed framebuffer
  896. *
  897. * @dev_priv: Pointer to the device private structure.
  898. * @framebuffer: Pointer to the surface-buffer backed framebuffer.
  899. * @clips: Array of clip rects. Either @clips or @vclips must be NULL.
  900. * @vclips: Alternate array of clip rects. Either @clips or @vclips must
  901. * be NULL.
  902. * @srf: Pointer to surface to blit from. If NULL, the surface attached
  903. * to @framebuffer will be used.
  904. * @dest_x: X coordinate offset to align @srf with framebuffer coordinates.
  905. * @dest_y: Y coordinate offset to align @srf with framebuffer coordinates.
  906. * @num_clips: Number of clip rects in @clips.
  907. * @inc: Increment to use when looping over @clips.
  908. * @out_fence: If non-NULL, will return a ref-counted pointer to a
  909. * struct vmw_fence_obj. The returned fence pointer may be NULL in which
  910. * case the device has already synchronized.
  911. * @crtc: If crtc is passed, perform surface dirty on that crtc only.
  912. *
  913. * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
  914. * interrupted.
  915. */
  916. int vmw_kms_sou_do_surface_dirty(struct vmw_private *dev_priv,
  917. struct vmw_framebuffer *framebuffer,
  918. struct drm_clip_rect *clips,
  919. struct drm_vmw_rect *vclips,
  920. struct vmw_resource *srf,
  921. s32 dest_x,
  922. s32 dest_y,
  923. unsigned num_clips, int inc,
  924. struct vmw_fence_obj **out_fence,
  925. struct drm_crtc *crtc)
  926. {
  927. struct vmw_framebuffer_surface *vfbs =
  928. container_of(framebuffer, typeof(*vfbs), base);
  929. struct vmw_kms_sou_surface_dirty sdirty;
  930. DECLARE_VAL_CONTEXT(val_ctx, NULL, 0);
  931. int ret;
  932. if (!srf)
  933. srf = &vmw_user_object_surface(&vfbs->uo)->res;
  934. ret = vmw_validation_add_resource(&val_ctx, srf, 0, VMW_RES_DIRTY_NONE,
  935. NULL, NULL);
  936. if (ret)
  937. return ret;
  938. ret = vmw_validation_prepare(&val_ctx, &dev_priv->cmdbuf_mutex, true);
  939. if (ret)
  940. goto out_unref;
  941. sdirty.base.fifo_commit = vmw_sou_surface_fifo_commit;
  942. sdirty.base.clip = vmw_sou_surface_clip;
  943. sdirty.base.dev_priv = dev_priv;
  944. sdirty.base.fifo_reserve_size = sizeof(struct vmw_kms_sou_dirty_cmd) +
  945. sizeof(SVGASignedRect) * num_clips;
  946. sdirty.base.crtc = crtc;
  947. sdirty.sid = srf->id;
  948. sdirty.left = sdirty.top = S32_MAX;
  949. sdirty.right = sdirty.bottom = S32_MIN;
  950. sdirty.dst_x = dest_x;
  951. sdirty.dst_y = dest_y;
  952. ret = vmw_kms_helper_dirty(dev_priv, framebuffer, clips, vclips,
  953. dest_x, dest_y, num_clips, inc,
  954. &sdirty.base);
  955. vmw_kms_helper_validation_finish(dev_priv, NULL, &val_ctx, out_fence,
  956. NULL);
  957. return ret;
  958. out_unref:
  959. vmw_validation_unref_lists(&val_ctx);
  960. return ret;
  961. }
  962. /**
  963. * vmw_sou_bo_fifo_commit - Callback to submit a set of readback clips.
  964. *
  965. * @dirty: The closure structure.
  966. *
  967. * Commits a previously built command buffer of readback clips.
  968. */
  969. static void vmw_sou_bo_fifo_commit(struct vmw_kms_dirty *dirty)
  970. {
  971. if (!dirty->num_hits) {
  972. vmw_cmd_commit(dirty->dev_priv, 0);
  973. return;
  974. }
  975. vmw_cmd_commit(dirty->dev_priv,
  976. sizeof(struct vmw_kms_sou_bo_blit) *
  977. dirty->num_hits);
  978. }
  979. /**
  980. * vmw_sou_bo_clip - Callback to encode a readback cliprect.
  981. *
  982. * @dirty: The closure structure
  983. *
  984. * Encodes a BLIT_GMRFB_TO_SCREEN cliprect.
  985. */
  986. static void vmw_sou_bo_clip(struct vmw_kms_dirty *dirty)
  987. {
  988. struct vmw_kms_sou_bo_blit *blit = dirty->cmd;
  989. blit += dirty->num_hits;
  990. blit->header = SVGA_CMD_BLIT_GMRFB_TO_SCREEN;
  991. blit->body.destScreenId = dirty->unit->unit;
  992. blit->body.srcOrigin.x = dirty->fb_x;
  993. blit->body.srcOrigin.y = dirty->fb_y;
  994. blit->body.destRect.left = dirty->unit_x1;
  995. blit->body.destRect.top = dirty->unit_y1;
  996. blit->body.destRect.right = dirty->unit_x2;
  997. blit->body.destRect.bottom = dirty->unit_y2;
  998. dirty->num_hits++;
  999. }
  1000. /**
  1001. * vmw_kms_sou_do_bo_dirty - Dirty part of a buffer-object backed framebuffer
  1002. *
  1003. * @dev_priv: Pointer to the device private structure.
  1004. * @framebuffer: Pointer to the buffer-object backed framebuffer.
  1005. * @clips: Array of clip rects.
  1006. * @vclips: Alternate array of clip rects. Either @clips or @vclips must
  1007. * be NULL.
  1008. * @num_clips: Number of clip rects in @clips.
  1009. * @increment: Increment to use when looping over @clips.
  1010. * @interruptible: Whether to perform waits interruptible if possible.
  1011. * @out_fence: If non-NULL, will return a ref-counted pointer to a
  1012. * struct vmw_fence_obj. The returned fence pointer may be NULL in which
  1013. * case the device has already synchronized.
  1014. * @crtc: If crtc is passed, perform bo dirty on that crtc only.
  1015. *
  1016. * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
  1017. * interrupted.
  1018. */
  1019. int vmw_kms_sou_do_bo_dirty(struct vmw_private *dev_priv,
  1020. struct vmw_framebuffer *framebuffer,
  1021. struct drm_clip_rect *clips,
  1022. struct drm_vmw_rect *vclips,
  1023. unsigned num_clips, int increment,
  1024. bool interruptible,
  1025. struct vmw_fence_obj **out_fence,
  1026. struct drm_crtc *crtc)
  1027. {
  1028. struct vmw_bo *buf =
  1029. container_of(framebuffer, struct vmw_framebuffer_bo,
  1030. base)->buffer;
  1031. struct vmw_kms_dirty dirty;
  1032. DECLARE_VAL_CONTEXT(val_ctx, NULL, 0);
  1033. int ret;
  1034. vmw_bo_placement_set(buf, VMW_BO_DOMAIN_GMR | VMW_BO_DOMAIN_VRAM,
  1035. VMW_BO_DOMAIN_GMR | VMW_BO_DOMAIN_VRAM);
  1036. ret = vmw_validation_add_bo(&val_ctx, buf);
  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_bo *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. vmw_bo_placement_set(buf, VMW_BO_DOMAIN_GMR | VMW_BO_DOMAIN_VRAM,
  1130. VMW_BO_DOMAIN_GMR | VMW_BO_DOMAIN_VRAM);
  1131. ret = vmw_validation_add_bo(&val_ctx, buf);
  1132. if (ret)
  1133. return ret;
  1134. ret = vmw_validation_prepare(&val_ctx, NULL, true);
  1135. if (ret)
  1136. goto out_unref;
  1137. ret = do_bo_define_gmrfb(dev_priv, vfb);
  1138. if (unlikely(ret != 0))
  1139. goto out_revert;
  1140. dirty.crtc = crtc;
  1141. dirty.fifo_commit = vmw_sou_readback_fifo_commit;
  1142. dirty.clip = vmw_sou_readback_clip;
  1143. dirty.fifo_reserve_size = sizeof(struct vmw_kms_sou_readback_blit) *
  1144. num_clips;
  1145. ret = vmw_kms_helper_dirty(dev_priv, vfb, NULL, vclips,
  1146. 0, 0, num_clips, 1, &dirty);
  1147. vmw_kms_helper_validation_finish(dev_priv, file_priv, &val_ctx, NULL,
  1148. user_fence_rep);
  1149. return ret;
  1150. out_revert:
  1151. vmw_validation_revert(&val_ctx);
  1152. out_unref:
  1153. vmw_validation_unref_lists(&val_ctx);
  1154. return ret;
  1155. }