vmwgfx_scrn.c 32 KB

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