vmwgfx_scrn.c 32 KB

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