vmwgfx_scrn.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  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 const struct drm_crtc_funcs vmw_screen_object_crtc_funcs = {
  437. .cursor_set2 = vmw_du_crtc_cursor_set2,
  438. .cursor_move = vmw_du_crtc_cursor_move,
  439. .gamma_set = vmw_du_crtc_gamma_set,
  440. .destroy = vmw_sou_crtc_destroy,
  441. .set_config = vmw_sou_crtc_set_config,
  442. .page_flip = vmw_sou_crtc_page_flip,
  443. };
  444. /*
  445. * Screen Object Display Unit encoder functions
  446. */
  447. static void vmw_sou_encoder_destroy(struct drm_encoder *encoder)
  448. {
  449. vmw_sou_destroy(vmw_encoder_to_sou(encoder));
  450. }
  451. static const struct drm_encoder_funcs vmw_screen_object_encoder_funcs = {
  452. .destroy = vmw_sou_encoder_destroy,
  453. };
  454. /*
  455. * Screen Object Display Unit connector functions
  456. */
  457. static void vmw_sou_connector_destroy(struct drm_connector *connector)
  458. {
  459. vmw_sou_destroy(vmw_connector_to_sou(connector));
  460. }
  461. static const struct drm_connector_funcs vmw_sou_connector_funcs = {
  462. .dpms = vmw_du_connector_dpms,
  463. .detect = vmw_du_connector_detect,
  464. .fill_modes = vmw_du_connector_fill_modes,
  465. .set_property = vmw_du_connector_set_property,
  466. .destroy = vmw_sou_connector_destroy,
  467. };
  468. static int vmw_sou_init(struct vmw_private *dev_priv, unsigned unit)
  469. {
  470. struct vmw_screen_object_unit *sou;
  471. struct drm_device *dev = dev_priv->dev;
  472. struct drm_connector *connector;
  473. struct drm_encoder *encoder;
  474. struct drm_crtc *crtc;
  475. sou = kzalloc(sizeof(*sou), GFP_KERNEL);
  476. if (!sou)
  477. return -ENOMEM;
  478. sou->base.unit = unit;
  479. crtc = &sou->base.crtc;
  480. encoder = &sou->base.encoder;
  481. connector = &sou->base.connector;
  482. sou->active_implicit = false;
  483. sou->base.pref_active = (unit == 0);
  484. sou->base.pref_width = dev_priv->initial_width;
  485. sou->base.pref_height = dev_priv->initial_height;
  486. sou->base.pref_mode = NULL;
  487. sou->base.is_implicit = true;
  488. drm_connector_init(dev, connector, &vmw_sou_connector_funcs,
  489. DRM_MODE_CONNECTOR_VIRTUAL);
  490. connector->status = vmw_du_connector_detect(connector, true);
  491. drm_encoder_init(dev, encoder, &vmw_screen_object_encoder_funcs,
  492. DRM_MODE_ENCODER_VIRTUAL, NULL);
  493. drm_mode_connector_attach_encoder(connector, encoder);
  494. encoder->possible_crtcs = (1 << unit);
  495. encoder->possible_clones = 0;
  496. (void) drm_connector_register(connector);
  497. drm_crtc_init(dev, crtc, &vmw_screen_object_crtc_funcs);
  498. drm_mode_crtc_set_gamma_size(crtc, 256);
  499. drm_object_attach_property(&connector->base,
  500. dev->mode_config.dirty_info_property,
  501. 1);
  502. return 0;
  503. }
  504. int vmw_kms_sou_init_display(struct vmw_private *dev_priv)
  505. {
  506. struct drm_device *dev = dev_priv->dev;
  507. int i, ret;
  508. if (dev_priv->sou_priv) {
  509. DRM_INFO("sou system already on\n");
  510. return -EINVAL;
  511. }
  512. if (!(dev_priv->capabilities & SVGA_CAP_SCREEN_OBJECT_2)) {
  513. DRM_INFO("Not using screen objects,"
  514. " missing cap SCREEN_OBJECT_2\n");
  515. return -ENOSYS;
  516. }
  517. ret = -ENOMEM;
  518. dev_priv->sou_priv = kmalloc(sizeof(*dev_priv->sou_priv), GFP_KERNEL);
  519. if (unlikely(!dev_priv->sou_priv))
  520. goto err_no_mem;
  521. dev_priv->sou_priv->num_implicit = 0;
  522. dev_priv->sou_priv->implicit_fb = NULL;
  523. ret = drm_vblank_init(dev, VMWGFX_NUM_DISPLAY_UNITS);
  524. if (unlikely(ret != 0))
  525. goto err_free;
  526. ret = drm_mode_create_dirty_info_property(dev);
  527. if (unlikely(ret != 0))
  528. goto err_vblank_cleanup;
  529. for (i = 0; i < VMWGFX_NUM_DISPLAY_UNITS; ++i)
  530. vmw_sou_init(dev_priv, i);
  531. dev_priv->active_display_unit = vmw_du_screen_object;
  532. DRM_INFO("Screen Objects Display Unit initialized\n");
  533. return 0;
  534. err_vblank_cleanup:
  535. drm_vblank_cleanup(dev);
  536. err_free:
  537. kfree(dev_priv->sou_priv);
  538. dev_priv->sou_priv = NULL;
  539. err_no_mem:
  540. return ret;
  541. }
  542. int vmw_kms_sou_close_display(struct vmw_private *dev_priv)
  543. {
  544. struct drm_device *dev = dev_priv->dev;
  545. if (!dev_priv->sou_priv)
  546. return -ENOSYS;
  547. drm_vblank_cleanup(dev);
  548. kfree(dev_priv->sou_priv);
  549. return 0;
  550. }
  551. static int do_dmabuf_define_gmrfb(struct vmw_private *dev_priv,
  552. struct vmw_framebuffer *framebuffer)
  553. {
  554. struct vmw_dma_buffer *buf =
  555. container_of(framebuffer, struct vmw_framebuffer_dmabuf,
  556. base)->buffer;
  557. int depth = framebuffer->base.depth;
  558. struct {
  559. uint32_t header;
  560. SVGAFifoCmdDefineGMRFB body;
  561. } *cmd;
  562. /* Emulate RGBA support, contrary to svga_reg.h this is not
  563. * supported by hosts. This is only a problem if we are reading
  564. * this value later and expecting what we uploaded back.
  565. */
  566. if (depth == 32)
  567. depth = 24;
  568. cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
  569. if (!cmd) {
  570. DRM_ERROR("Out of fifo space for dirty framebuffer command.\n");
  571. return -ENOMEM;
  572. }
  573. cmd->header = SVGA_CMD_DEFINE_GMRFB;
  574. cmd->body.format.bitsPerPixel = framebuffer->base.bits_per_pixel;
  575. cmd->body.format.colorDepth = depth;
  576. cmd->body.format.reserved = 0;
  577. cmd->body.bytesPerLine = framebuffer->base.pitches[0];
  578. /* Buffer is reserved in vram or GMR */
  579. vmw_bo_get_guest_ptr(&buf->base, &cmd->body.ptr);
  580. vmw_fifo_commit(dev_priv, sizeof(*cmd));
  581. return 0;
  582. }
  583. /**
  584. * vmw_sou_surface_fifo_commit - Callback to fill in and submit a
  585. * blit surface to screen command.
  586. *
  587. * @dirty: The closure structure.
  588. *
  589. * Fills in the missing fields in the command, and translates the cliprects
  590. * to match the destination bounding box encoded.
  591. */
  592. static void vmw_sou_surface_fifo_commit(struct vmw_kms_dirty *dirty)
  593. {
  594. struct vmw_kms_sou_surface_dirty *sdirty =
  595. container_of(dirty, typeof(*sdirty), base);
  596. struct vmw_kms_sou_dirty_cmd *cmd = dirty->cmd;
  597. s32 trans_x = dirty->unit->crtc.x - sdirty->dst_x;
  598. s32 trans_y = dirty->unit->crtc.y - sdirty->dst_y;
  599. size_t region_size = dirty->num_hits * sizeof(SVGASignedRect);
  600. SVGASignedRect *blit = (SVGASignedRect *) &cmd[1];
  601. int i;
  602. cmd->header.id = SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN;
  603. cmd->header.size = sizeof(cmd->body) + region_size;
  604. /*
  605. * Use the destination bounding box to specify destination - and
  606. * source bounding regions.
  607. */
  608. cmd->body.destRect.left = sdirty->left;
  609. cmd->body.destRect.right = sdirty->right;
  610. cmd->body.destRect.top = sdirty->top;
  611. cmd->body.destRect.bottom = sdirty->bottom;
  612. cmd->body.srcRect.left = sdirty->left + trans_x;
  613. cmd->body.srcRect.right = sdirty->right + trans_x;
  614. cmd->body.srcRect.top = sdirty->top + trans_y;
  615. cmd->body.srcRect.bottom = sdirty->bottom + trans_y;
  616. cmd->body.srcImage.sid = sdirty->sid;
  617. cmd->body.destScreenId = dirty->unit->unit;
  618. /* Blits are relative to the destination rect. Translate. */
  619. for (i = 0; i < dirty->num_hits; ++i, ++blit) {
  620. blit->left -= sdirty->left;
  621. blit->right -= sdirty->left;
  622. blit->top -= sdirty->top;
  623. blit->bottom -= sdirty->top;
  624. }
  625. vmw_fifo_commit(dirty->dev_priv, region_size + sizeof(*cmd));
  626. sdirty->left = sdirty->top = S32_MAX;
  627. sdirty->right = sdirty->bottom = S32_MIN;
  628. }
  629. /**
  630. * vmw_sou_surface_clip - Callback to encode a blit surface to screen cliprect.
  631. *
  632. * @dirty: The closure structure
  633. *
  634. * Encodes a SVGASignedRect cliprect and updates the bounding box of the
  635. * BLIT_SURFACE_TO_SCREEN command.
  636. */
  637. static void vmw_sou_surface_clip(struct vmw_kms_dirty *dirty)
  638. {
  639. struct vmw_kms_sou_surface_dirty *sdirty =
  640. container_of(dirty, typeof(*sdirty), base);
  641. struct vmw_kms_sou_dirty_cmd *cmd = dirty->cmd;
  642. SVGASignedRect *blit = (SVGASignedRect *) &cmd[1];
  643. /* Destination rect. */
  644. blit += dirty->num_hits;
  645. blit->left = dirty->unit_x1;
  646. blit->top = dirty->unit_y1;
  647. blit->right = dirty->unit_x2;
  648. blit->bottom = dirty->unit_y2;
  649. /* Destination bounding box */
  650. sdirty->left = min_t(s32, sdirty->left, dirty->unit_x1);
  651. sdirty->top = min_t(s32, sdirty->top, dirty->unit_y1);
  652. sdirty->right = max_t(s32, sdirty->right, dirty->unit_x2);
  653. sdirty->bottom = max_t(s32, sdirty->bottom, dirty->unit_y2);
  654. dirty->num_hits++;
  655. }
  656. /**
  657. * vmw_kms_sou_do_surface_dirty - Dirty part of a surface backed framebuffer
  658. *
  659. * @dev_priv: Pointer to the device private structure.
  660. * @framebuffer: Pointer to the surface-buffer backed framebuffer.
  661. * @clips: Array of clip rects. Either @clips or @vclips must be NULL.
  662. * @vclips: Alternate array of clip rects. Either @clips or @vclips must
  663. * be NULL.
  664. * @srf: Pointer to surface to blit from. If NULL, the surface attached
  665. * to @framebuffer will be used.
  666. * @dest_x: X coordinate offset to align @srf with framebuffer coordinates.
  667. * @dest_y: Y coordinate offset to align @srf with framebuffer coordinates.
  668. * @num_clips: Number of clip rects in @clips.
  669. * @inc: Increment to use when looping over @clips.
  670. * @out_fence: If non-NULL, will return a ref-counted pointer to a
  671. * struct vmw_fence_obj. The returned fence pointer may be NULL in which
  672. * case the device has already synchronized.
  673. *
  674. * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
  675. * interrupted.
  676. */
  677. int vmw_kms_sou_do_surface_dirty(struct vmw_private *dev_priv,
  678. struct vmw_framebuffer *framebuffer,
  679. struct drm_clip_rect *clips,
  680. struct drm_vmw_rect *vclips,
  681. struct vmw_resource *srf,
  682. s32 dest_x,
  683. s32 dest_y,
  684. unsigned num_clips, int inc,
  685. struct vmw_fence_obj **out_fence)
  686. {
  687. struct vmw_framebuffer_surface *vfbs =
  688. container_of(framebuffer, typeof(*vfbs), base);
  689. struct vmw_kms_sou_surface_dirty sdirty;
  690. int ret;
  691. if (!srf)
  692. srf = &vfbs->surface->res;
  693. ret = vmw_kms_helper_resource_prepare(srf, true);
  694. if (ret)
  695. return ret;
  696. sdirty.base.fifo_commit = vmw_sou_surface_fifo_commit;
  697. sdirty.base.clip = vmw_sou_surface_clip;
  698. sdirty.base.dev_priv = dev_priv;
  699. sdirty.base.fifo_reserve_size = sizeof(struct vmw_kms_sou_dirty_cmd) +
  700. sizeof(SVGASignedRect) * num_clips;
  701. sdirty.sid = srf->id;
  702. sdirty.left = sdirty.top = S32_MAX;
  703. sdirty.right = sdirty.bottom = S32_MIN;
  704. sdirty.dst_x = dest_x;
  705. sdirty.dst_y = dest_y;
  706. ret = vmw_kms_helper_dirty(dev_priv, framebuffer, clips, vclips,
  707. dest_x, dest_y, num_clips, inc,
  708. &sdirty.base);
  709. vmw_kms_helper_resource_finish(srf, out_fence);
  710. return ret;
  711. }
  712. /**
  713. * vmw_sou_dmabuf_fifo_commit - Callback to submit a set of readback clips.
  714. *
  715. * @dirty: The closure structure.
  716. *
  717. * Commits a previously built command buffer of readback clips.
  718. */
  719. static void vmw_sou_dmabuf_fifo_commit(struct vmw_kms_dirty *dirty)
  720. {
  721. vmw_fifo_commit(dirty->dev_priv,
  722. sizeof(struct vmw_kms_sou_dmabuf_blit) *
  723. dirty->num_hits);
  724. }
  725. /**
  726. * vmw_sou_dmabuf_clip - Callback to encode a readback cliprect.
  727. *
  728. * @dirty: The closure structure
  729. *
  730. * Encodes a BLIT_GMRFB_TO_SCREEN cliprect.
  731. */
  732. static void vmw_sou_dmabuf_clip(struct vmw_kms_dirty *dirty)
  733. {
  734. struct vmw_kms_sou_dmabuf_blit *blit = dirty->cmd;
  735. blit += dirty->num_hits;
  736. blit->header = SVGA_CMD_BLIT_GMRFB_TO_SCREEN;
  737. blit->body.destScreenId = dirty->unit->unit;
  738. blit->body.srcOrigin.x = dirty->fb_x;
  739. blit->body.srcOrigin.y = dirty->fb_y;
  740. blit->body.destRect.left = dirty->unit_x1;
  741. blit->body.destRect.top = dirty->unit_y1;
  742. blit->body.destRect.right = dirty->unit_x2;
  743. blit->body.destRect.bottom = dirty->unit_y2;
  744. dirty->num_hits++;
  745. }
  746. /**
  747. * vmw_kms_do_dmabuf_dirty - Dirty part of a dma-buffer backed framebuffer
  748. *
  749. * @dev_priv: Pointer to the device private structure.
  750. * @framebuffer: Pointer to the dma-buffer backed framebuffer.
  751. * @clips: Array of clip rects.
  752. * @num_clips: Number of clip rects in @clips.
  753. * @increment: Increment to use when looping over @clips.
  754. * @interruptible: Whether to perform waits interruptible if possible.
  755. * @out_fence: If non-NULL, will return a ref-counted pointer to a
  756. * struct vmw_fence_obj. The returned fence pointer may be NULL in which
  757. * case the device has already synchronized.
  758. *
  759. * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
  760. * interrupted.
  761. */
  762. int vmw_kms_sou_do_dmabuf_dirty(struct vmw_private *dev_priv,
  763. struct vmw_framebuffer *framebuffer,
  764. struct drm_clip_rect *clips,
  765. unsigned num_clips, int increment,
  766. bool interruptible,
  767. struct vmw_fence_obj **out_fence)
  768. {
  769. struct vmw_dma_buffer *buf =
  770. container_of(framebuffer, struct vmw_framebuffer_dmabuf,
  771. base)->buffer;
  772. struct vmw_kms_dirty dirty;
  773. int ret;
  774. ret = vmw_kms_helper_buffer_prepare(dev_priv, buf, interruptible,
  775. false);
  776. if (ret)
  777. return ret;
  778. ret = do_dmabuf_define_gmrfb(dev_priv, framebuffer);
  779. if (unlikely(ret != 0))
  780. goto out_revert;
  781. dirty.fifo_commit = vmw_sou_dmabuf_fifo_commit;
  782. dirty.clip = vmw_sou_dmabuf_clip;
  783. dirty.fifo_reserve_size = sizeof(struct vmw_kms_sou_dmabuf_blit) *
  784. num_clips;
  785. ret = vmw_kms_helper_dirty(dev_priv, framebuffer, clips, NULL,
  786. 0, 0, num_clips, increment, &dirty);
  787. vmw_kms_helper_buffer_finish(dev_priv, NULL, buf, out_fence, NULL);
  788. return ret;
  789. out_revert:
  790. vmw_kms_helper_buffer_revert(buf);
  791. return ret;
  792. }
  793. /**
  794. * vmw_sou_readback_fifo_commit - Callback to submit a set of readback clips.
  795. *
  796. * @dirty: The closure structure.
  797. *
  798. * Commits a previously built command buffer of readback clips.
  799. */
  800. static void vmw_sou_readback_fifo_commit(struct vmw_kms_dirty *dirty)
  801. {
  802. vmw_fifo_commit(dirty->dev_priv,
  803. sizeof(struct vmw_kms_sou_readback_blit) *
  804. dirty->num_hits);
  805. }
  806. /**
  807. * vmw_sou_readback_clip - Callback to encode a readback cliprect.
  808. *
  809. * @dirty: The closure structure
  810. *
  811. * Encodes a BLIT_SCREEN_TO_GMRFB cliprect.
  812. */
  813. static void vmw_sou_readback_clip(struct vmw_kms_dirty *dirty)
  814. {
  815. struct vmw_kms_sou_readback_blit *blit = dirty->cmd;
  816. blit += dirty->num_hits;
  817. blit->header = SVGA_CMD_BLIT_SCREEN_TO_GMRFB;
  818. blit->body.srcScreenId = dirty->unit->unit;
  819. blit->body.destOrigin.x = dirty->fb_x;
  820. blit->body.destOrigin.y = dirty->fb_y;
  821. blit->body.srcRect.left = dirty->unit_x1;
  822. blit->body.srcRect.top = dirty->unit_y1;
  823. blit->body.srcRect.right = dirty->unit_x2;
  824. blit->body.srcRect.bottom = dirty->unit_y2;
  825. dirty->num_hits++;
  826. }
  827. /**
  828. * vmw_kms_sou_readback - Perform a readback from the screen object system to
  829. * a dma-buffer backed framebuffer.
  830. *
  831. * @dev_priv: Pointer to the device private structure.
  832. * @file_priv: Pointer to a struct drm_file identifying the caller.
  833. * Must be set to NULL if @user_fence_rep is NULL.
  834. * @vfb: Pointer to the dma-buffer backed framebuffer.
  835. * @user_fence_rep: User-space provided structure for fence information.
  836. * Must be set to non-NULL if @file_priv is non-NULL.
  837. * @vclips: Array of clip rects.
  838. * @num_clips: Number of clip rects in @vclips.
  839. *
  840. * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
  841. * interrupted.
  842. */
  843. int vmw_kms_sou_readback(struct vmw_private *dev_priv,
  844. struct drm_file *file_priv,
  845. struct vmw_framebuffer *vfb,
  846. struct drm_vmw_fence_rep __user *user_fence_rep,
  847. struct drm_vmw_rect *vclips,
  848. uint32_t num_clips)
  849. {
  850. struct vmw_dma_buffer *buf =
  851. container_of(vfb, struct vmw_framebuffer_dmabuf, base)->buffer;
  852. struct vmw_kms_dirty dirty;
  853. int ret;
  854. ret = vmw_kms_helper_buffer_prepare(dev_priv, buf, true, false);
  855. if (ret)
  856. return ret;
  857. ret = do_dmabuf_define_gmrfb(dev_priv, vfb);
  858. if (unlikely(ret != 0))
  859. goto out_revert;
  860. dirty.fifo_commit = vmw_sou_readback_fifo_commit;
  861. dirty.clip = vmw_sou_readback_clip;
  862. dirty.fifo_reserve_size = sizeof(struct vmw_kms_sou_readback_blit) *
  863. num_clips;
  864. ret = vmw_kms_helper_dirty(dev_priv, vfb, NULL, vclips,
  865. 0, 0, num_clips, 1, &dirty);
  866. vmw_kms_helper_buffer_finish(dev_priv, file_priv, buf, NULL,
  867. user_fence_rep);
  868. return ret;
  869. out_revert:
  870. vmw_kms_helper_buffer_revert(buf);
  871. return ret;
  872. }