vmwgfx_scrn.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054
  1. /**************************************************************************
  2. *
  3. * Copyright © 2011-2015 VMware, Inc., Palo Alto, CA., USA
  4. * All Rights Reserved.
  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. #define vmw_crtc_to_sou(x) \
  30. container_of(x, struct vmw_screen_object_unit, base.crtc)
  31. #define vmw_encoder_to_sou(x) \
  32. container_of(x, struct vmw_screen_object_unit, base.encoder)
  33. #define vmw_connector_to_sou(x) \
  34. container_of(x, struct vmw_screen_object_unit, base.connector)
  35. /**
  36. * struct vmw_kms_sou_surface_dirty - Closure structure for
  37. * blit surface to screen command.
  38. * @base: The base type we derive from. Used by vmw_kms_helper_dirty().
  39. * @left: Left side of bounding box.
  40. * @right: Right side of bounding box.
  41. * @top: Top side of bounding box.
  42. * @bottom: Bottom side of bounding box.
  43. * @dst_x: Difference between source clip rects and framebuffer coordinates.
  44. * @dst_y: Difference between source clip rects and framebuffer coordinates.
  45. * @sid: Surface id of surface to copy from.
  46. */
  47. struct vmw_kms_sou_surface_dirty {
  48. struct vmw_kms_dirty base;
  49. s32 left, right, top, bottom;
  50. s32 dst_x, dst_y;
  51. u32 sid;
  52. };
  53. /*
  54. * SVGA commands that are used by this code. Please see the device headers
  55. * for explanation.
  56. */
  57. struct vmw_kms_sou_readback_blit {
  58. uint32 header;
  59. SVGAFifoCmdBlitScreenToGMRFB body;
  60. };
  61. struct vmw_kms_sou_dmabuf_blit {
  62. uint32 header;
  63. SVGAFifoCmdBlitGMRFBToScreen body;
  64. };
  65. struct vmw_kms_sou_dirty_cmd {
  66. SVGA3dCmdHeader header;
  67. SVGA3dCmdBlitSurfaceToScreen body;
  68. };
  69. /*
  70. * Other structs.
  71. */
  72. struct vmw_screen_object_display {
  73. unsigned num_implicit;
  74. struct vmw_framebuffer *implicit_fb;
  75. SVGAFifoCmdDefineGMRFB cur;
  76. struct vmw_dma_buffer *pinned_gmrfb;
  77. };
  78. /**
  79. * Display unit using screen objects.
  80. */
  81. struct vmw_screen_object_unit {
  82. struct vmw_display_unit base;
  83. unsigned long buffer_size; /**< Size of allocated buffer */
  84. struct vmw_dma_buffer *buffer; /**< Backing store buffer */
  85. bool defined;
  86. bool active_implicit;
  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. static void vmw_sou_del_active(struct vmw_private *vmw_priv,
  101. struct vmw_screen_object_unit *sou)
  102. {
  103. struct vmw_screen_object_display *ld = vmw_priv->sou_priv;
  104. if (sou->active_implicit) {
  105. if (--(ld->num_implicit) == 0)
  106. ld->implicit_fb = NULL;
  107. sou->active_implicit = false;
  108. }
  109. }
  110. static void vmw_sou_add_active(struct vmw_private *vmw_priv,
  111. struct vmw_screen_object_unit *sou,
  112. struct vmw_framebuffer *vfb)
  113. {
  114. struct vmw_screen_object_display *ld = vmw_priv->sou_priv;
  115. BUG_ON(!ld->num_implicit && ld->implicit_fb);
  116. if (!sou->active_implicit && sou->base.is_implicit) {
  117. ld->implicit_fb = vfb;
  118. sou->active_implicit = true;
  119. ld->num_implicit++;
  120. }
  121. }
  122. /**
  123. * Send the fifo command to create a screen.
  124. */
  125. static int vmw_sou_fifo_create(struct vmw_private *dev_priv,
  126. struct vmw_screen_object_unit *sou,
  127. uint32_t x, uint32_t y,
  128. struct drm_display_mode *mode)
  129. {
  130. size_t fifo_size;
  131. struct {
  132. struct {
  133. uint32_t cmdType;
  134. } header;
  135. SVGAScreenObject obj;
  136. } *cmd;
  137. BUG_ON(!sou->buffer);
  138. fifo_size = sizeof(*cmd);
  139. cmd = vmw_fifo_reserve(dev_priv, fifo_size);
  140. /* The hardware has hung, nothing we can do about it here. */
  141. if (unlikely(cmd == NULL)) {
  142. DRM_ERROR("Fifo reserve failed.\n");
  143. return -ENOMEM;
  144. }
  145. memset(cmd, 0, fifo_size);
  146. cmd->header.cmdType = SVGA_CMD_DEFINE_SCREEN;
  147. cmd->obj.structSize = sizeof(SVGAScreenObject);
  148. cmd->obj.id = sou->base.unit;
  149. cmd->obj.flags = SVGA_SCREEN_HAS_ROOT |
  150. (sou->base.unit == 0 ? SVGA_SCREEN_IS_PRIMARY : 0);
  151. cmd->obj.size.width = mode->hdisplay;
  152. cmd->obj.size.height = mode->vdisplay;
  153. if (sou->base.is_implicit) {
  154. cmd->obj.root.x = x;
  155. cmd->obj.root.y = y;
  156. } else {
  157. cmd->obj.root.x = sou->base.gui_x;
  158. cmd->obj.root.y = sou->base.gui_y;
  159. }
  160. /* Ok to assume that buffer is pinned in vram */
  161. vmw_bo_get_guest_ptr(&sou->buffer->base, &cmd->obj.backingStore.ptr);
  162. cmd->obj.backingStore.pitch = mode->hdisplay * 4;
  163. vmw_fifo_commit(dev_priv, fifo_size);
  164. sou->defined = true;
  165. return 0;
  166. }
  167. /**
  168. * Send the fifo command to destroy a screen.
  169. */
  170. static int vmw_sou_fifo_destroy(struct vmw_private *dev_priv,
  171. struct vmw_screen_object_unit *sou)
  172. {
  173. size_t fifo_size;
  174. int ret;
  175. struct {
  176. struct {
  177. uint32_t cmdType;
  178. } header;
  179. SVGAFifoCmdDestroyScreen body;
  180. } *cmd;
  181. /* no need to do anything */
  182. if (unlikely(!sou->defined))
  183. return 0;
  184. fifo_size = sizeof(*cmd);
  185. cmd = vmw_fifo_reserve(dev_priv, fifo_size);
  186. /* the hardware has hung, nothing we can do about it here */
  187. if (unlikely(cmd == NULL)) {
  188. DRM_ERROR("Fifo reserve failed.\n");
  189. return -ENOMEM;
  190. }
  191. memset(cmd, 0, fifo_size);
  192. cmd->header.cmdType = SVGA_CMD_DESTROY_SCREEN;
  193. cmd->body.screenId = sou->base.unit;
  194. vmw_fifo_commit(dev_priv, fifo_size);
  195. /* Force sync */
  196. ret = vmw_fallback_wait(dev_priv, false, true, 0, false, 3*HZ);
  197. if (unlikely(ret != 0))
  198. DRM_ERROR("Failed to sync with HW");
  199. else
  200. sou->defined = false;
  201. return ret;
  202. }
  203. /**
  204. * Free the backing store.
  205. */
  206. static void vmw_sou_backing_free(struct vmw_private *dev_priv,
  207. struct vmw_screen_object_unit *sou)
  208. {
  209. vmw_dmabuf_unreference(&sou->buffer);
  210. sou->buffer_size = 0;
  211. }
  212. /**
  213. * Allocate the backing store for the buffer.
  214. */
  215. static int vmw_sou_backing_alloc(struct vmw_private *dev_priv,
  216. struct vmw_screen_object_unit *sou,
  217. unsigned long size)
  218. {
  219. int ret;
  220. if (sou->buffer_size == size)
  221. return 0;
  222. if (sou->buffer)
  223. vmw_sou_backing_free(dev_priv, sou);
  224. sou->buffer = kzalloc(sizeof(*sou->buffer), GFP_KERNEL);
  225. if (unlikely(sou->buffer == NULL))
  226. return -ENOMEM;
  227. /* After we have alloced the backing store might not be able to
  228. * resume the overlays, this is preferred to failing to alloc.
  229. */
  230. vmw_overlay_pause_all(dev_priv);
  231. ret = vmw_dmabuf_init(dev_priv, sou->buffer, size,
  232. &vmw_vram_ne_placement,
  233. false, &vmw_dmabuf_bo_free);
  234. vmw_overlay_resume_all(dev_priv);
  235. if (unlikely(ret != 0))
  236. sou->buffer = NULL; /* vmw_dmabuf_init frees on error */
  237. else
  238. sou->buffer_size = size;
  239. return ret;
  240. }
  241. static int vmw_sou_crtc_set_config(struct drm_mode_set *set)
  242. {
  243. struct vmw_private *dev_priv;
  244. struct vmw_screen_object_unit *sou;
  245. struct drm_connector *connector;
  246. struct drm_display_mode *mode;
  247. struct drm_encoder *encoder;
  248. struct vmw_framebuffer *vfb;
  249. struct drm_framebuffer *fb;
  250. struct drm_crtc *crtc;
  251. int ret = 0;
  252. if (!set)
  253. return -EINVAL;
  254. if (!set->crtc)
  255. return -EINVAL;
  256. /* get the sou */
  257. crtc = set->crtc;
  258. sou = vmw_crtc_to_sou(crtc);
  259. vfb = set->fb ? vmw_framebuffer_to_vfb(set->fb) : NULL;
  260. dev_priv = vmw_priv(crtc->dev);
  261. if (set->num_connectors > 1) {
  262. DRM_ERROR("Too many connectors\n");
  263. return -EINVAL;
  264. }
  265. if (set->num_connectors == 1 &&
  266. set->connectors[0] != &sou->base.connector) {
  267. DRM_ERROR("Connector doesn't match %p %p\n",
  268. set->connectors[0], &sou->base.connector);
  269. return -EINVAL;
  270. }
  271. /* sou only supports one fb active at the time */
  272. if (sou->base.is_implicit &&
  273. dev_priv->sou_priv->implicit_fb && vfb &&
  274. !(dev_priv->sou_priv->num_implicit == 1 &&
  275. sou->active_implicit) &&
  276. dev_priv->sou_priv->implicit_fb != vfb) {
  277. DRM_ERROR("Multiple framebuffers not supported\n");
  278. return -EINVAL;
  279. }
  280. /* since they always map one to one these are safe */
  281. connector = &sou->base.connector;
  282. encoder = &sou->base.encoder;
  283. /* should we turn the crtc off */
  284. if (set->num_connectors == 0 || !set->mode || !set->fb) {
  285. ret = vmw_sou_fifo_destroy(dev_priv, sou);
  286. /* the hardware has hung don't do anything more */
  287. if (unlikely(ret != 0))
  288. return ret;
  289. connector->encoder = NULL;
  290. encoder->crtc = NULL;
  291. crtc->primary->fb = NULL;
  292. crtc->x = 0;
  293. crtc->y = 0;
  294. crtc->enabled = false;
  295. vmw_sou_del_active(dev_priv, sou);
  296. vmw_sou_backing_free(dev_priv, sou);
  297. return 0;
  298. }
  299. /* we now know we want to set a mode */
  300. mode = set->mode;
  301. fb = set->fb;
  302. if (set->x + mode->hdisplay > fb->width ||
  303. set->y + mode->vdisplay > fb->height) {
  304. DRM_ERROR("set outside of framebuffer\n");
  305. return -EINVAL;
  306. }
  307. vmw_svga_enable(dev_priv);
  308. if (mode->hdisplay != crtc->mode.hdisplay ||
  309. mode->vdisplay != crtc->mode.vdisplay) {
  310. /* no need to check if depth is different, because backing
  311. * store depth is forced to 4 by the device.
  312. */
  313. ret = vmw_sou_fifo_destroy(dev_priv, sou);
  314. /* the hardware has hung don't do anything more */
  315. if (unlikely(ret != 0))
  316. return ret;
  317. vmw_sou_backing_free(dev_priv, sou);
  318. }
  319. if (!sou->buffer) {
  320. /* forced to depth 4 by the device */
  321. size_t size = mode->hdisplay * mode->vdisplay * 4;
  322. ret = vmw_sou_backing_alloc(dev_priv, sou, size);
  323. if (unlikely(ret != 0))
  324. return ret;
  325. }
  326. ret = vmw_sou_fifo_create(dev_priv, sou, set->x, set->y, mode);
  327. if (unlikely(ret != 0)) {
  328. /*
  329. * We are in a bit of a situation here, the hardware has
  330. * hung and we may or may not have a buffer hanging of
  331. * the screen object, best thing to do is not do anything
  332. * if we where defined, if not just turn the crtc of.
  333. * Not what userspace wants but it needs to htfu.
  334. */
  335. if (sou->defined)
  336. return ret;
  337. connector->encoder = NULL;
  338. encoder->crtc = NULL;
  339. crtc->primary->fb = NULL;
  340. crtc->x = 0;
  341. crtc->y = 0;
  342. crtc->enabled = false;
  343. return ret;
  344. }
  345. vmw_sou_add_active(dev_priv, sou, vfb);
  346. connector->encoder = encoder;
  347. encoder->crtc = crtc;
  348. crtc->mode = *mode;
  349. crtc->primary->fb = fb;
  350. crtc->x = set->x;
  351. crtc->y = set->y;
  352. crtc->enabled = true;
  353. return 0;
  354. }
  355. /**
  356. * Returns if this unit can be page flipped.
  357. * Must be called with the mode_config mutex held.
  358. */
  359. static bool vmw_sou_screen_object_flippable(struct vmw_private *dev_priv,
  360. struct drm_crtc *crtc)
  361. {
  362. struct vmw_screen_object_unit *sou = vmw_crtc_to_sou(crtc);
  363. if (!sou->base.is_implicit)
  364. return true;
  365. if (dev_priv->sou_priv->num_implicit != 1)
  366. return false;
  367. return true;
  368. }
  369. /**
  370. * Update the implicit fb to the current fb of this crtc.
  371. * Must be called with the mode_config mutex held.
  372. */
  373. static void vmw_sou_update_implicit_fb(struct vmw_private *dev_priv,
  374. struct drm_crtc *crtc)
  375. {
  376. struct vmw_screen_object_unit *sou = vmw_crtc_to_sou(crtc);
  377. BUG_ON(!sou->base.is_implicit);
  378. dev_priv->sou_priv->implicit_fb =
  379. vmw_framebuffer_to_vfb(sou->base.crtc.primary->fb);
  380. }
  381. static int vmw_sou_crtc_page_flip(struct drm_crtc *crtc,
  382. struct drm_framebuffer *fb,
  383. struct drm_pending_vblank_event *event,
  384. uint32_t flags)
  385. {
  386. struct vmw_private *dev_priv = vmw_priv(crtc->dev);
  387. struct drm_framebuffer *old_fb = crtc->primary->fb;
  388. struct vmw_framebuffer *vfb = vmw_framebuffer_to_vfb(fb);
  389. struct vmw_fence_obj *fence = NULL;
  390. struct drm_clip_rect clips;
  391. int ret;
  392. /* require ScreenObject support for page flipping */
  393. if (!dev_priv->sou_priv)
  394. return -ENOSYS;
  395. if (!vmw_sou_screen_object_flippable(dev_priv, crtc))
  396. return -EINVAL;
  397. crtc->primary->fb = fb;
  398. /* do a full screen dirty update */
  399. clips.x1 = clips.y1 = 0;
  400. clips.x2 = fb->width;
  401. clips.y2 = fb->height;
  402. if (vfb->dmabuf)
  403. ret = vmw_kms_sou_do_dmabuf_dirty(dev_priv, vfb,
  404. &clips, 1, 1,
  405. true, &fence);
  406. else
  407. ret = vmw_kms_sou_do_surface_dirty(dev_priv, vfb,
  408. &clips, NULL, NULL,
  409. 0, 0, 1, 1, &fence);
  410. if (ret != 0)
  411. goto out_no_fence;
  412. if (!fence) {
  413. ret = -EINVAL;
  414. goto out_no_fence;
  415. }
  416. if (event) {
  417. struct drm_file *file_priv = event->base.file_priv;
  418. ret = vmw_event_fence_action_queue(file_priv, fence,
  419. &event->base,
  420. &event->event.tv_sec,
  421. &event->event.tv_usec,
  422. true);
  423. }
  424. /*
  425. * No need to hold on to this now. The only cleanup
  426. * we need to do if we fail is unref the fence.
  427. */
  428. vmw_fence_obj_unreference(&fence);
  429. if (vmw_crtc_to_du(crtc)->is_implicit)
  430. vmw_sou_update_implicit_fb(dev_priv, crtc);
  431. return ret;
  432. out_no_fence:
  433. crtc->primary->fb = old_fb;
  434. return ret;
  435. }
  436. static struct drm_crtc_funcs vmw_screen_object_crtc_funcs = {
  437. .save = vmw_du_crtc_save,
  438. .restore = vmw_du_crtc_restore,
  439. .cursor_set2 = vmw_du_crtc_cursor_set2,
  440. .cursor_move = vmw_du_crtc_cursor_move,
  441. .gamma_set = vmw_du_crtc_gamma_set,
  442. .destroy = vmw_sou_crtc_destroy,
  443. .set_config = vmw_sou_crtc_set_config,
  444. .page_flip = vmw_sou_crtc_page_flip,
  445. };
  446. /*
  447. * Screen Object Display Unit encoder functions
  448. */
  449. static void vmw_sou_encoder_destroy(struct drm_encoder *encoder)
  450. {
  451. vmw_sou_destroy(vmw_encoder_to_sou(encoder));
  452. }
  453. static struct drm_encoder_funcs vmw_screen_object_encoder_funcs = {
  454. .destroy = vmw_sou_encoder_destroy,
  455. };
  456. /*
  457. * Screen Object Display Unit connector functions
  458. */
  459. static void vmw_sou_connector_destroy(struct drm_connector *connector)
  460. {
  461. vmw_sou_destroy(vmw_connector_to_sou(connector));
  462. }
  463. static struct drm_connector_funcs vmw_sou_connector_funcs = {
  464. .dpms = vmw_du_connector_dpms,
  465. .save = vmw_du_connector_save,
  466. .restore = vmw_du_connector_restore,
  467. .detect = vmw_du_connector_detect,
  468. .fill_modes = vmw_du_connector_fill_modes,
  469. .set_property = vmw_du_connector_set_property,
  470. .destroy = vmw_sou_connector_destroy,
  471. };
  472. static int vmw_sou_init(struct vmw_private *dev_priv, unsigned unit)
  473. {
  474. struct vmw_screen_object_unit *sou;
  475. struct drm_device *dev = dev_priv->dev;
  476. struct drm_connector *connector;
  477. struct drm_encoder *encoder;
  478. struct drm_crtc *crtc;
  479. sou = kzalloc(sizeof(*sou), GFP_KERNEL);
  480. if (!sou)
  481. return -ENOMEM;
  482. sou->base.unit = unit;
  483. crtc = &sou->base.crtc;
  484. encoder = &sou->base.encoder;
  485. connector = &sou->base.connector;
  486. sou->active_implicit = false;
  487. sou->base.pref_active = (unit == 0);
  488. sou->base.pref_width = dev_priv->initial_width;
  489. sou->base.pref_height = dev_priv->initial_height;
  490. sou->base.pref_mode = NULL;
  491. sou->base.is_implicit = true;
  492. drm_connector_init(dev, connector, &vmw_sou_connector_funcs,
  493. DRM_MODE_CONNECTOR_VIRTUAL);
  494. connector->status = vmw_du_connector_detect(connector, true);
  495. drm_encoder_init(dev, encoder, &vmw_screen_object_encoder_funcs,
  496. DRM_MODE_ENCODER_VIRTUAL);
  497. drm_mode_connector_attach_encoder(connector, encoder);
  498. encoder->possible_crtcs = (1 << unit);
  499. encoder->possible_clones = 0;
  500. (void) drm_connector_register(connector);
  501. drm_crtc_init(dev, crtc, &vmw_screen_object_crtc_funcs);
  502. drm_mode_crtc_set_gamma_size(crtc, 256);
  503. drm_object_attach_property(&connector->base,
  504. dev->mode_config.dirty_info_property,
  505. 1);
  506. return 0;
  507. }
  508. int vmw_kms_sou_init_display(struct vmw_private *dev_priv)
  509. {
  510. struct drm_device *dev = dev_priv->dev;
  511. int i, ret;
  512. if (dev_priv->sou_priv) {
  513. DRM_INFO("sou system already on\n");
  514. return -EINVAL;
  515. }
  516. if (!(dev_priv->capabilities & SVGA_CAP_SCREEN_OBJECT_2)) {
  517. DRM_INFO("Not using screen objects,"
  518. " missing cap SCREEN_OBJECT_2\n");
  519. return -ENOSYS;
  520. }
  521. ret = -ENOMEM;
  522. dev_priv->sou_priv = kmalloc(sizeof(*dev_priv->sou_priv), GFP_KERNEL);
  523. if (unlikely(!dev_priv->sou_priv))
  524. goto err_no_mem;
  525. dev_priv->sou_priv->num_implicit = 0;
  526. dev_priv->sou_priv->implicit_fb = NULL;
  527. ret = drm_vblank_init(dev, VMWGFX_NUM_DISPLAY_UNITS);
  528. if (unlikely(ret != 0))
  529. goto err_free;
  530. ret = drm_mode_create_dirty_info_property(dev);
  531. if (unlikely(ret != 0))
  532. goto err_vblank_cleanup;
  533. for (i = 0; i < VMWGFX_NUM_DISPLAY_UNITS; ++i)
  534. vmw_sou_init(dev_priv, i);
  535. dev_priv->active_display_unit = vmw_du_screen_object;
  536. DRM_INFO("Screen Objects Display Unit initialized\n");
  537. return 0;
  538. err_vblank_cleanup:
  539. drm_vblank_cleanup(dev);
  540. err_free:
  541. kfree(dev_priv->sou_priv);
  542. dev_priv->sou_priv = NULL;
  543. err_no_mem:
  544. return ret;
  545. }
  546. int vmw_kms_sou_close_display(struct vmw_private *dev_priv)
  547. {
  548. struct drm_device *dev = dev_priv->dev;
  549. if (!dev_priv->sou_priv)
  550. return -ENOSYS;
  551. drm_vblank_cleanup(dev);
  552. kfree(dev_priv->sou_priv);
  553. return 0;
  554. }
  555. static int do_dmabuf_define_gmrfb(struct vmw_private *dev_priv,
  556. struct vmw_framebuffer *framebuffer)
  557. {
  558. struct vmw_dma_buffer *buf =
  559. container_of(framebuffer, struct vmw_framebuffer_dmabuf,
  560. base)->buffer;
  561. int depth = framebuffer->base.depth;
  562. struct {
  563. uint32_t header;
  564. SVGAFifoCmdDefineGMRFB body;
  565. } *cmd;
  566. /* Emulate RGBA support, contrary to svga_reg.h this is not
  567. * supported by hosts. This is only a problem if we are reading
  568. * this value later and expecting what we uploaded back.
  569. */
  570. if (depth == 32)
  571. depth = 24;
  572. cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
  573. if (!cmd) {
  574. DRM_ERROR("Out of fifo space for dirty framebuffer command.\n");
  575. return -ENOMEM;
  576. }
  577. cmd->header = SVGA_CMD_DEFINE_GMRFB;
  578. cmd->body.format.bitsPerPixel = framebuffer->base.bits_per_pixel;
  579. cmd->body.format.colorDepth = depth;
  580. cmd->body.format.reserved = 0;
  581. cmd->body.bytesPerLine = framebuffer->base.pitches[0];
  582. /* Buffer is reserved in vram or GMR */
  583. vmw_bo_get_guest_ptr(&buf->base, &cmd->body.ptr);
  584. vmw_fifo_commit(dev_priv, sizeof(*cmd));
  585. return 0;
  586. }
  587. /**
  588. * vmw_sou_surface_fifo_commit - Callback to fill in and submit a
  589. * blit surface to screen command.
  590. *
  591. * @dirty: The closure structure.
  592. *
  593. * Fills in the missing fields in the command, and translates the cliprects
  594. * to match the destination bounding box encoded.
  595. */
  596. static void vmw_sou_surface_fifo_commit(struct vmw_kms_dirty *dirty)
  597. {
  598. struct vmw_kms_sou_surface_dirty *sdirty =
  599. container_of(dirty, typeof(*sdirty), base);
  600. struct vmw_kms_sou_dirty_cmd *cmd = dirty->cmd;
  601. s32 trans_x = dirty->unit->crtc.x - sdirty->dst_x;
  602. s32 trans_y = dirty->unit->crtc.y - sdirty->dst_y;
  603. size_t region_size = dirty->num_hits * sizeof(SVGASignedRect);
  604. SVGASignedRect *blit = (SVGASignedRect *) &cmd[1];
  605. int i;
  606. cmd->header.id = SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN;
  607. cmd->header.size = sizeof(cmd->body) + region_size;
  608. /*
  609. * Use the destination bounding box to specify destination - and
  610. * source bounding regions.
  611. */
  612. cmd->body.destRect.left = sdirty->left;
  613. cmd->body.destRect.right = sdirty->right;
  614. cmd->body.destRect.top = sdirty->top;
  615. cmd->body.destRect.bottom = sdirty->bottom;
  616. cmd->body.srcRect.left = sdirty->left + trans_x;
  617. cmd->body.srcRect.right = sdirty->right + trans_x;
  618. cmd->body.srcRect.top = sdirty->top + trans_y;
  619. cmd->body.srcRect.bottom = sdirty->bottom + trans_y;
  620. cmd->body.srcImage.sid = sdirty->sid;
  621. cmd->body.destScreenId = dirty->unit->unit;
  622. /* Blits are relative to the destination rect. Translate. */
  623. for (i = 0; i < dirty->num_hits; ++i, ++blit) {
  624. blit->left -= sdirty->left;
  625. blit->right -= sdirty->left;
  626. blit->top -= sdirty->top;
  627. blit->bottom -= sdirty->top;
  628. }
  629. vmw_fifo_commit(dirty->dev_priv, region_size + sizeof(*cmd));
  630. sdirty->left = sdirty->top = S32_MAX;
  631. sdirty->right = sdirty->bottom = S32_MIN;
  632. }
  633. /**
  634. * vmw_sou_surface_clip - Callback to encode a blit surface to screen cliprect.
  635. *
  636. * @dirty: The closure structure
  637. *
  638. * Encodes a SVGASignedRect cliprect and updates the bounding box of the
  639. * BLIT_SURFACE_TO_SCREEN command.
  640. */
  641. static void vmw_sou_surface_clip(struct vmw_kms_dirty *dirty)
  642. {
  643. struct vmw_kms_sou_surface_dirty *sdirty =
  644. container_of(dirty, typeof(*sdirty), base);
  645. struct vmw_kms_sou_dirty_cmd *cmd = dirty->cmd;
  646. SVGASignedRect *blit = (SVGASignedRect *) &cmd[1];
  647. /* Destination rect. */
  648. blit += dirty->num_hits;
  649. blit->left = dirty->unit_x1;
  650. blit->top = dirty->unit_y1;
  651. blit->right = dirty->unit_x2;
  652. blit->bottom = dirty->unit_y2;
  653. /* Destination bounding box */
  654. sdirty->left = min_t(s32, sdirty->left, dirty->unit_x1);
  655. sdirty->top = min_t(s32, sdirty->top, dirty->unit_y1);
  656. sdirty->right = max_t(s32, sdirty->right, dirty->unit_x2);
  657. sdirty->bottom = max_t(s32, sdirty->bottom, dirty->unit_y2);
  658. dirty->num_hits++;
  659. }
  660. /**
  661. * vmw_kms_sou_do_surface_dirty - Dirty part of a surface backed framebuffer
  662. *
  663. * @dev_priv: Pointer to the device private structure.
  664. * @framebuffer: Pointer to the surface-buffer backed framebuffer.
  665. * @clips: Array of clip rects. Either @clips or @vclips must be NULL.
  666. * @vclips: Alternate array of clip rects. Either @clips or @vclips must
  667. * be NULL.
  668. * @srf: Pointer to surface to blit from. If NULL, the surface attached
  669. * to @framebuffer will be used.
  670. * @dest_x: X coordinate offset to align @srf with framebuffer coordinates.
  671. * @dest_y: Y coordinate offset to align @srf with framebuffer coordinates.
  672. * @num_clips: Number of clip rects in @clips.
  673. * @inc: Increment to use when looping over @clips.
  674. * @out_fence: If non-NULL, will return a ref-counted pointer to a
  675. * struct vmw_fence_obj. The returned fence pointer may be NULL in which
  676. * case the device has already synchronized.
  677. *
  678. * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
  679. * interrupted.
  680. */
  681. int vmw_kms_sou_do_surface_dirty(struct vmw_private *dev_priv,
  682. struct vmw_framebuffer *framebuffer,
  683. struct drm_clip_rect *clips,
  684. struct drm_vmw_rect *vclips,
  685. struct vmw_resource *srf,
  686. s32 dest_x,
  687. s32 dest_y,
  688. unsigned num_clips, int inc,
  689. struct vmw_fence_obj **out_fence)
  690. {
  691. struct vmw_framebuffer_surface *vfbs =
  692. container_of(framebuffer, typeof(*vfbs), base);
  693. struct vmw_kms_sou_surface_dirty sdirty;
  694. struct vmw_validation_ctx ctx;
  695. int ret;
  696. if (!srf)
  697. srf = &vfbs->surface->res;
  698. ret = vmw_kms_helper_resource_prepare(srf, true, &ctx);
  699. if (ret)
  700. return ret;
  701. sdirty.base.fifo_commit = vmw_sou_surface_fifo_commit;
  702. sdirty.base.clip = vmw_sou_surface_clip;
  703. sdirty.base.dev_priv = dev_priv;
  704. sdirty.base.fifo_reserve_size = sizeof(struct vmw_kms_sou_dirty_cmd) +
  705. sizeof(SVGASignedRect) * num_clips;
  706. sdirty.sid = srf->id;
  707. sdirty.left = sdirty.top = S32_MAX;
  708. sdirty.right = sdirty.bottom = S32_MIN;
  709. sdirty.dst_x = dest_x;
  710. sdirty.dst_y = dest_y;
  711. ret = vmw_kms_helper_dirty(dev_priv, framebuffer, clips, vclips,
  712. dest_x, dest_y, num_clips, inc,
  713. &sdirty.base);
  714. vmw_kms_helper_resource_finish(&ctx, out_fence);
  715. return ret;
  716. }
  717. /**
  718. * vmw_sou_dmabuf_fifo_commit - Callback to submit a set of readback clips.
  719. *
  720. * @dirty: The closure structure.
  721. *
  722. * Commits a previously built command buffer of readback clips.
  723. */
  724. static void vmw_sou_dmabuf_fifo_commit(struct vmw_kms_dirty *dirty)
  725. {
  726. vmw_fifo_commit(dirty->dev_priv,
  727. sizeof(struct vmw_kms_sou_dmabuf_blit) *
  728. dirty->num_hits);
  729. }
  730. /**
  731. * vmw_sou_dmabuf_clip - Callback to encode a readback cliprect.
  732. *
  733. * @dirty: The closure structure
  734. *
  735. * Encodes a BLIT_GMRFB_TO_SCREEN cliprect.
  736. */
  737. static void vmw_sou_dmabuf_clip(struct vmw_kms_dirty *dirty)
  738. {
  739. struct vmw_kms_sou_dmabuf_blit *blit = dirty->cmd;
  740. blit += dirty->num_hits;
  741. blit->header = SVGA_CMD_BLIT_GMRFB_TO_SCREEN;
  742. blit->body.destScreenId = dirty->unit->unit;
  743. blit->body.srcOrigin.x = dirty->fb_x;
  744. blit->body.srcOrigin.y = dirty->fb_y;
  745. blit->body.destRect.left = dirty->unit_x1;
  746. blit->body.destRect.top = dirty->unit_y1;
  747. blit->body.destRect.right = dirty->unit_x2;
  748. blit->body.destRect.bottom = dirty->unit_y2;
  749. dirty->num_hits++;
  750. }
  751. /**
  752. * vmw_kms_do_dmabuf_dirty - Dirty part of a dma-buffer backed framebuffer
  753. *
  754. * @dev_priv: Pointer to the device private structure.
  755. * @framebuffer: Pointer to the dma-buffer backed framebuffer.
  756. * @clips: Array of clip rects.
  757. * @num_clips: Number of clip rects in @clips.
  758. * @increment: Increment to use when looping over @clips.
  759. * @interruptible: Whether to perform waits interruptible if possible.
  760. * @out_fence: If non-NULL, will return a ref-counted pointer to a
  761. * struct vmw_fence_obj. The returned fence pointer may be NULL in which
  762. * case the device has already synchronized.
  763. *
  764. * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
  765. * interrupted.
  766. */
  767. int vmw_kms_sou_do_dmabuf_dirty(struct vmw_private *dev_priv,
  768. struct vmw_framebuffer *framebuffer,
  769. struct drm_clip_rect *clips,
  770. unsigned num_clips, int increment,
  771. bool interruptible,
  772. struct vmw_fence_obj **out_fence)
  773. {
  774. struct vmw_dma_buffer *buf =
  775. container_of(framebuffer, struct vmw_framebuffer_dmabuf,
  776. base)->buffer;
  777. struct vmw_kms_dirty dirty;
  778. int ret;
  779. ret = vmw_kms_helper_buffer_prepare(dev_priv, buf, interruptible,
  780. false);
  781. if (ret)
  782. return ret;
  783. ret = do_dmabuf_define_gmrfb(dev_priv, framebuffer);
  784. if (unlikely(ret != 0))
  785. goto out_revert;
  786. dirty.fifo_commit = vmw_sou_dmabuf_fifo_commit;
  787. dirty.clip = vmw_sou_dmabuf_clip;
  788. dirty.fifo_reserve_size = sizeof(struct vmw_kms_sou_dmabuf_blit) *
  789. num_clips;
  790. ret = vmw_kms_helper_dirty(dev_priv, framebuffer, clips, NULL,
  791. 0, 0, num_clips, increment, &dirty);
  792. vmw_kms_helper_buffer_finish(dev_priv, NULL, buf, out_fence, NULL);
  793. return ret;
  794. out_revert:
  795. vmw_kms_helper_buffer_revert(buf);
  796. return ret;
  797. }
  798. /**
  799. * vmw_sou_readback_fifo_commit - Callback to submit a set of readback clips.
  800. *
  801. * @dirty: The closure structure.
  802. *
  803. * Commits a previously built command buffer of readback clips.
  804. */
  805. static void vmw_sou_readback_fifo_commit(struct vmw_kms_dirty *dirty)
  806. {
  807. vmw_fifo_commit(dirty->dev_priv,
  808. sizeof(struct vmw_kms_sou_readback_blit) *
  809. dirty->num_hits);
  810. }
  811. /**
  812. * vmw_sou_readback_clip - Callback to encode a readback cliprect.
  813. *
  814. * @dirty: The closure structure
  815. *
  816. * Encodes a BLIT_SCREEN_TO_GMRFB cliprect.
  817. */
  818. static void vmw_sou_readback_clip(struct vmw_kms_dirty *dirty)
  819. {
  820. struct vmw_kms_sou_readback_blit *blit = dirty->cmd;
  821. blit += dirty->num_hits;
  822. blit->header = SVGA_CMD_BLIT_SCREEN_TO_GMRFB;
  823. blit->body.srcScreenId = dirty->unit->unit;
  824. blit->body.destOrigin.x = dirty->fb_x;
  825. blit->body.destOrigin.y = dirty->fb_y;
  826. blit->body.srcRect.left = dirty->unit_x1;
  827. blit->body.srcRect.top = dirty->unit_y1;
  828. blit->body.srcRect.right = dirty->unit_x2;
  829. blit->body.srcRect.bottom = dirty->unit_y2;
  830. dirty->num_hits++;
  831. }
  832. /**
  833. * vmw_kms_sou_readback - Perform a readback from the screen object system to
  834. * a dma-buffer backed framebuffer.
  835. *
  836. * @dev_priv: Pointer to the device private structure.
  837. * @file_priv: Pointer to a struct drm_file identifying the caller.
  838. * Must be set to NULL if @user_fence_rep is NULL.
  839. * @vfb: Pointer to the dma-buffer backed framebuffer.
  840. * @user_fence_rep: User-space provided structure for fence information.
  841. * Must be set to non-NULL if @file_priv is non-NULL.
  842. * @vclips: Array of clip rects.
  843. * @num_clips: Number of clip rects in @vclips.
  844. *
  845. * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
  846. * interrupted.
  847. */
  848. int vmw_kms_sou_readback(struct vmw_private *dev_priv,
  849. struct drm_file *file_priv,
  850. struct vmw_framebuffer *vfb,
  851. struct drm_vmw_fence_rep __user *user_fence_rep,
  852. struct drm_vmw_rect *vclips,
  853. uint32_t num_clips)
  854. {
  855. struct vmw_dma_buffer *buf =
  856. container_of(vfb, struct vmw_framebuffer_dmabuf, base)->buffer;
  857. struct vmw_kms_dirty dirty;
  858. int ret;
  859. ret = vmw_kms_helper_buffer_prepare(dev_priv, buf, true, false);
  860. if (ret)
  861. return ret;
  862. ret = do_dmabuf_define_gmrfb(dev_priv, vfb);
  863. if (unlikely(ret != 0))
  864. goto out_revert;
  865. dirty.fifo_commit = vmw_sou_readback_fifo_commit;
  866. dirty.clip = vmw_sou_readback_clip;
  867. dirty.fifo_reserve_size = sizeof(struct vmw_kms_sou_readback_blit) *
  868. num_clips;
  869. ret = vmw_kms_helper_dirty(dev_priv, vfb, NULL, vclips,
  870. 0, 0, num_clips, 1, &dirty);
  871. vmw_kms_helper_buffer_finish(dev_priv, file_priv, buf, NULL,
  872. user_fence_rep);
  873. return ret;
  874. out_revert:
  875. vmw_kms_helper_buffer_revert(buf);
  876. return ret;
  877. }