vmwgfx_ldu.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /**************************************************************************
  2. *
  3. * Copyright © 2009-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_ldu(x) \
  30. container_of(x, struct vmw_legacy_display_unit, base.crtc)
  31. #define vmw_encoder_to_ldu(x) \
  32. container_of(x, struct vmw_legacy_display_unit, base.encoder)
  33. #define vmw_connector_to_ldu(x) \
  34. container_of(x, struct vmw_legacy_display_unit, base.connector)
  35. struct vmw_legacy_display {
  36. struct list_head active;
  37. unsigned num_active;
  38. unsigned last_num_active;
  39. struct vmw_framebuffer *fb;
  40. };
  41. /**
  42. * Display unit using the legacy register interface.
  43. */
  44. struct vmw_legacy_display_unit {
  45. struct vmw_display_unit base;
  46. struct list_head active;
  47. };
  48. static void vmw_ldu_destroy(struct vmw_legacy_display_unit *ldu)
  49. {
  50. list_del_init(&ldu->active);
  51. vmw_du_cleanup(&ldu->base);
  52. kfree(ldu);
  53. }
  54. /*
  55. * Legacy Display Unit CRTC functions
  56. */
  57. static void vmw_ldu_crtc_destroy(struct drm_crtc *crtc)
  58. {
  59. vmw_ldu_destroy(vmw_crtc_to_ldu(crtc));
  60. }
  61. static int vmw_ldu_commit_list(struct vmw_private *dev_priv)
  62. {
  63. struct vmw_legacy_display *lds = dev_priv->ldu_priv;
  64. struct vmw_legacy_display_unit *entry;
  65. struct vmw_display_unit *du = NULL;
  66. struct drm_framebuffer *fb = NULL;
  67. struct drm_crtc *crtc = NULL;
  68. int i = 0, ret;
  69. /* If there is no display topology the host just assumes
  70. * that the guest will set the same layout as the host.
  71. */
  72. if (!(dev_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY)) {
  73. int w = 0, h = 0;
  74. list_for_each_entry(entry, &lds->active, active) {
  75. crtc = &entry->base.crtc;
  76. w = max(w, crtc->x + crtc->mode.hdisplay);
  77. h = max(h, crtc->y + crtc->mode.vdisplay);
  78. i++;
  79. }
  80. if (crtc == NULL)
  81. return 0;
  82. fb = entry->base.crtc.primary->fb;
  83. return vmw_kms_write_svga(dev_priv, w, h, fb->pitches[0],
  84. fb->format->cpp[0] * 8,
  85. fb->format->depth);
  86. }
  87. if (!list_empty(&lds->active)) {
  88. entry = list_entry(lds->active.next, typeof(*entry), active);
  89. fb = entry->base.crtc.primary->fb;
  90. vmw_kms_write_svga(dev_priv, fb->width, fb->height, fb->pitches[0],
  91. fb->format->cpp[0] * 8, fb->format->depth);
  92. }
  93. /* Make sure we always show something. */
  94. vmw_write(dev_priv, SVGA_REG_NUM_GUEST_DISPLAYS,
  95. lds->num_active ? lds->num_active : 1);
  96. i = 0;
  97. list_for_each_entry(entry, &lds->active, active) {
  98. crtc = &entry->base.crtc;
  99. vmw_write(dev_priv, SVGA_REG_DISPLAY_ID, i);
  100. vmw_write(dev_priv, SVGA_REG_DISPLAY_IS_PRIMARY, !i);
  101. vmw_write(dev_priv, SVGA_REG_DISPLAY_POSITION_X, crtc->x);
  102. vmw_write(dev_priv, SVGA_REG_DISPLAY_POSITION_Y, crtc->y);
  103. vmw_write(dev_priv, SVGA_REG_DISPLAY_WIDTH, crtc->mode.hdisplay);
  104. vmw_write(dev_priv, SVGA_REG_DISPLAY_HEIGHT, crtc->mode.vdisplay);
  105. vmw_write(dev_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
  106. i++;
  107. }
  108. BUG_ON(i != lds->num_active);
  109. lds->last_num_active = lds->num_active;
  110. /* Find the first du with a cursor. */
  111. list_for_each_entry(entry, &lds->active, active) {
  112. du = &entry->base;
  113. if (!du->cursor_dmabuf)
  114. continue;
  115. ret = vmw_cursor_update_dmabuf(dev_priv,
  116. du->cursor_dmabuf,
  117. 64, 64,
  118. du->hotspot_x,
  119. du->hotspot_y);
  120. if (ret == 0)
  121. break;
  122. DRM_ERROR("Could not update cursor image\n");
  123. }
  124. return 0;
  125. }
  126. static int vmw_ldu_del_active(struct vmw_private *vmw_priv,
  127. struct vmw_legacy_display_unit *ldu)
  128. {
  129. struct vmw_legacy_display *ld = vmw_priv->ldu_priv;
  130. if (list_empty(&ldu->active))
  131. return 0;
  132. /* Must init otherwise list_empty(&ldu->active) will not work. */
  133. list_del_init(&ldu->active);
  134. if (--(ld->num_active) == 0) {
  135. BUG_ON(!ld->fb);
  136. if (ld->fb->unpin)
  137. ld->fb->unpin(ld->fb);
  138. ld->fb = NULL;
  139. }
  140. return 0;
  141. }
  142. static int vmw_ldu_add_active(struct vmw_private *vmw_priv,
  143. struct vmw_legacy_display_unit *ldu,
  144. struct vmw_framebuffer *vfb)
  145. {
  146. struct vmw_legacy_display *ld = vmw_priv->ldu_priv;
  147. struct vmw_legacy_display_unit *entry;
  148. struct list_head *at;
  149. BUG_ON(!ld->num_active && ld->fb);
  150. if (vfb != ld->fb) {
  151. if (ld->fb && ld->fb->unpin)
  152. ld->fb->unpin(ld->fb);
  153. if (vfb->pin)
  154. vfb->pin(vfb);
  155. ld->fb = vfb;
  156. }
  157. if (!list_empty(&ldu->active))
  158. return 0;
  159. at = &ld->active;
  160. list_for_each_entry(entry, &ld->active, active) {
  161. if (entry->base.unit > ldu->base.unit)
  162. break;
  163. at = &entry->active;
  164. }
  165. list_add(&ldu->active, at);
  166. ld->num_active++;
  167. return 0;
  168. }
  169. static int vmw_ldu_crtc_set_config(struct drm_mode_set *set)
  170. {
  171. struct vmw_private *dev_priv;
  172. struct vmw_legacy_display_unit *ldu;
  173. struct drm_connector *connector;
  174. struct drm_display_mode *mode;
  175. struct drm_encoder *encoder;
  176. struct vmw_framebuffer *vfb;
  177. struct drm_framebuffer *fb;
  178. struct drm_crtc *crtc;
  179. if (!set)
  180. return -EINVAL;
  181. if (!set->crtc)
  182. return -EINVAL;
  183. /* get the ldu */
  184. crtc = set->crtc;
  185. ldu = vmw_crtc_to_ldu(crtc);
  186. vfb = set->fb ? vmw_framebuffer_to_vfb(set->fb) : NULL;
  187. dev_priv = vmw_priv(crtc->dev);
  188. if (set->num_connectors > 1) {
  189. DRM_ERROR("to many connectors\n");
  190. return -EINVAL;
  191. }
  192. if (set->num_connectors == 1 &&
  193. set->connectors[0] != &ldu->base.connector) {
  194. DRM_ERROR("connector doesn't match %p %p\n",
  195. set->connectors[0], &ldu->base.connector);
  196. return -EINVAL;
  197. }
  198. /* ldu only supports one fb active at the time */
  199. if (dev_priv->ldu_priv->fb && vfb &&
  200. !(dev_priv->ldu_priv->num_active == 1 &&
  201. !list_empty(&ldu->active)) &&
  202. dev_priv->ldu_priv->fb != vfb) {
  203. DRM_ERROR("Multiple framebuffers not supported\n");
  204. return -EINVAL;
  205. }
  206. /* since they always map one to one these are safe */
  207. connector = &ldu->base.connector;
  208. encoder = &ldu->base.encoder;
  209. /* should we turn the crtc off? */
  210. if (set->num_connectors == 0 || !set->mode || !set->fb) {
  211. connector->encoder = NULL;
  212. encoder->crtc = NULL;
  213. crtc->primary->fb = NULL;
  214. crtc->enabled = false;
  215. vmw_ldu_del_active(dev_priv, ldu);
  216. return vmw_ldu_commit_list(dev_priv);
  217. }
  218. /* we now know we want to set a mode */
  219. mode = set->mode;
  220. fb = set->fb;
  221. if (set->x + mode->hdisplay > fb->width ||
  222. set->y + mode->vdisplay > fb->height) {
  223. DRM_ERROR("set outside of framebuffer\n");
  224. return -EINVAL;
  225. }
  226. vmw_svga_enable(dev_priv);
  227. crtc->primary->fb = fb;
  228. encoder->crtc = crtc;
  229. connector->encoder = encoder;
  230. crtc->x = set->x;
  231. crtc->y = set->y;
  232. crtc->mode = *mode;
  233. crtc->enabled = true;
  234. ldu->base.set_gui_x = set->x;
  235. ldu->base.set_gui_y = set->y;
  236. vmw_ldu_add_active(dev_priv, ldu, vfb);
  237. return vmw_ldu_commit_list(dev_priv);
  238. }
  239. static const struct drm_crtc_funcs vmw_legacy_crtc_funcs = {
  240. .cursor_set2 = vmw_du_crtc_cursor_set2,
  241. .cursor_move = vmw_du_crtc_cursor_move,
  242. .gamma_set = vmw_du_crtc_gamma_set,
  243. .destroy = vmw_ldu_crtc_destroy,
  244. .set_config = vmw_ldu_crtc_set_config,
  245. };
  246. /*
  247. * Legacy Display Unit encoder functions
  248. */
  249. static void vmw_ldu_encoder_destroy(struct drm_encoder *encoder)
  250. {
  251. vmw_ldu_destroy(vmw_encoder_to_ldu(encoder));
  252. }
  253. static const struct drm_encoder_funcs vmw_legacy_encoder_funcs = {
  254. .destroy = vmw_ldu_encoder_destroy,
  255. };
  256. /*
  257. * Legacy Display Unit connector functions
  258. */
  259. static void vmw_ldu_connector_destroy(struct drm_connector *connector)
  260. {
  261. vmw_ldu_destroy(vmw_connector_to_ldu(connector));
  262. }
  263. static const struct drm_connector_funcs vmw_legacy_connector_funcs = {
  264. .dpms = vmw_du_connector_dpms,
  265. .detect = vmw_du_connector_detect,
  266. .fill_modes = vmw_du_connector_fill_modes,
  267. .set_property = vmw_du_connector_set_property,
  268. .destroy = vmw_ldu_connector_destroy,
  269. };
  270. static int vmw_ldu_init(struct vmw_private *dev_priv, unsigned unit)
  271. {
  272. struct vmw_legacy_display_unit *ldu;
  273. struct drm_device *dev = dev_priv->dev;
  274. struct drm_connector *connector;
  275. struct drm_encoder *encoder;
  276. struct drm_crtc *crtc;
  277. ldu = kzalloc(sizeof(*ldu), GFP_KERNEL);
  278. if (!ldu)
  279. return -ENOMEM;
  280. ldu->base.unit = unit;
  281. crtc = &ldu->base.crtc;
  282. encoder = &ldu->base.encoder;
  283. connector = &ldu->base.connector;
  284. INIT_LIST_HEAD(&ldu->active);
  285. ldu->base.pref_active = (unit == 0);
  286. ldu->base.pref_width = dev_priv->initial_width;
  287. ldu->base.pref_height = dev_priv->initial_height;
  288. ldu->base.pref_mode = NULL;
  289. ldu->base.is_implicit = true;
  290. drm_connector_init(dev, connector, &vmw_legacy_connector_funcs,
  291. DRM_MODE_CONNECTOR_VIRTUAL);
  292. connector->status = vmw_du_connector_detect(connector, true);
  293. drm_encoder_init(dev, encoder, &vmw_legacy_encoder_funcs,
  294. DRM_MODE_ENCODER_VIRTUAL, NULL);
  295. drm_mode_connector_attach_encoder(connector, encoder);
  296. encoder->possible_crtcs = (1 << unit);
  297. encoder->possible_clones = 0;
  298. (void) drm_connector_register(connector);
  299. drm_crtc_init(dev, crtc, &vmw_legacy_crtc_funcs);
  300. drm_mode_crtc_set_gamma_size(crtc, 256);
  301. drm_object_attach_property(&connector->base,
  302. dev_priv->hotplug_mode_update_property, 1);
  303. drm_object_attach_property(&connector->base,
  304. dev->mode_config.suggested_x_property, 0);
  305. drm_object_attach_property(&connector->base,
  306. dev->mode_config.suggested_y_property, 0);
  307. if (dev_priv->implicit_placement_property)
  308. drm_object_attach_property
  309. (&connector->base,
  310. dev_priv->implicit_placement_property,
  311. 1);
  312. return 0;
  313. }
  314. int vmw_kms_ldu_init_display(struct vmw_private *dev_priv)
  315. {
  316. struct drm_device *dev = dev_priv->dev;
  317. int i, ret;
  318. if (dev_priv->ldu_priv) {
  319. DRM_INFO("ldu system already on\n");
  320. return -EINVAL;
  321. }
  322. dev_priv->ldu_priv = kmalloc(sizeof(*dev_priv->ldu_priv), GFP_KERNEL);
  323. if (!dev_priv->ldu_priv)
  324. return -ENOMEM;
  325. INIT_LIST_HEAD(&dev_priv->ldu_priv->active);
  326. dev_priv->ldu_priv->num_active = 0;
  327. dev_priv->ldu_priv->last_num_active = 0;
  328. dev_priv->ldu_priv->fb = NULL;
  329. /* for old hardware without multimon only enable one display */
  330. if (dev_priv->capabilities & SVGA_CAP_MULTIMON)
  331. ret = drm_vblank_init(dev, VMWGFX_NUM_DISPLAY_UNITS);
  332. else
  333. ret = drm_vblank_init(dev, 1);
  334. if (ret != 0)
  335. goto err_free;
  336. vmw_kms_create_implicit_placement_property(dev_priv, true);
  337. if (dev_priv->capabilities & SVGA_CAP_MULTIMON)
  338. for (i = 0; i < VMWGFX_NUM_DISPLAY_UNITS; ++i)
  339. vmw_ldu_init(dev_priv, i);
  340. else
  341. vmw_ldu_init(dev_priv, 0);
  342. dev_priv->active_display_unit = vmw_du_legacy;
  343. DRM_INFO("Legacy Display Unit initialized\n");
  344. return 0;
  345. err_free:
  346. kfree(dev_priv->ldu_priv);
  347. dev_priv->ldu_priv = NULL;
  348. return ret;
  349. }
  350. int vmw_kms_ldu_close_display(struct vmw_private *dev_priv)
  351. {
  352. struct drm_device *dev = dev_priv->dev;
  353. if (!dev_priv->ldu_priv)
  354. return -ENOSYS;
  355. drm_vblank_cleanup(dev);
  356. BUG_ON(!list_empty(&dev_priv->ldu_priv->active));
  357. kfree(dev_priv->ldu_priv);
  358. return 0;
  359. }
  360. int vmw_kms_ldu_do_dmabuf_dirty(struct vmw_private *dev_priv,
  361. struct vmw_framebuffer *framebuffer,
  362. unsigned flags, unsigned color,
  363. struct drm_clip_rect *clips,
  364. unsigned num_clips, int increment)
  365. {
  366. size_t fifo_size;
  367. int i;
  368. struct {
  369. uint32_t header;
  370. SVGAFifoCmdUpdate body;
  371. } *cmd;
  372. fifo_size = sizeof(*cmd) * num_clips;
  373. cmd = vmw_fifo_reserve(dev_priv, fifo_size);
  374. if (unlikely(cmd == NULL)) {
  375. DRM_ERROR("Fifo reserve failed.\n");
  376. return -ENOMEM;
  377. }
  378. memset(cmd, 0, fifo_size);
  379. for (i = 0; i < num_clips; i++, clips += increment) {
  380. cmd[i].header = SVGA_CMD_UPDATE;
  381. cmd[i].body.x = clips->x1;
  382. cmd[i].body.y = clips->y1;
  383. cmd[i].body.width = clips->x2 - clips->x1;
  384. cmd[i].body.height = clips->y2 - clips->y1;
  385. }
  386. vmw_fifo_commit(dev_priv, fifo_size);
  387. return 0;
  388. }