vmwgfx_ldu.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /**************************************************************************
  2. *
  3. * Copyright © 2009 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. #define vmw_crtc_to_ldu(x) \
  29. container_of(x, struct vmw_legacy_display_unit, base.crtc)
  30. #define vmw_encoder_to_ldu(x) \
  31. container_of(x, struct vmw_legacy_display_unit, base.encoder)
  32. #define vmw_connector_to_ldu(x) \
  33. container_of(x, struct vmw_legacy_display_unit, base.connector)
  34. struct vmw_legacy_display {
  35. struct list_head active;
  36. unsigned num_active;
  37. unsigned last_num_active;
  38. struct vmw_framebuffer *fb;
  39. };
  40. /**
  41. * Display unit using the legacy register interface.
  42. */
  43. struct vmw_legacy_display_unit {
  44. struct vmw_display_unit base;
  45. struct list_head active;
  46. };
  47. static void vmw_ldu_destroy(struct vmw_legacy_display_unit *ldu)
  48. {
  49. list_del_init(&ldu->active);
  50. vmw_display_unit_cleanup(&ldu->base);
  51. kfree(ldu);
  52. }
  53. /*
  54. * Legacy Display Unit CRTC functions
  55. */
  56. static void vmw_ldu_crtc_destroy(struct drm_crtc *crtc)
  57. {
  58. vmw_ldu_destroy(vmw_crtc_to_ldu(crtc));
  59. }
  60. static int vmw_ldu_commit_list(struct vmw_private *dev_priv)
  61. {
  62. struct vmw_legacy_display *lds = dev_priv->ldu_priv;
  63. struct vmw_legacy_display_unit *entry;
  64. struct drm_framebuffer *fb = NULL;
  65. struct drm_crtc *crtc = NULL;
  66. int i = 0;
  67. /* If there is no display topology the host just assumes
  68. * that the guest will set the same layout as the host.
  69. */
  70. if (!(dev_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY)) {
  71. int w = 0, h = 0;
  72. list_for_each_entry(entry, &lds->active, active) {
  73. crtc = &entry->base.crtc;
  74. w = max(w, crtc->x + crtc->mode.hdisplay);
  75. h = max(h, crtc->y + crtc->mode.vdisplay);
  76. i++;
  77. }
  78. if (crtc == NULL)
  79. return 0;
  80. fb = entry->base.crtc.fb;
  81. return vmw_kms_write_svga(dev_priv, w, h, fb->pitch,
  82. fb->bits_per_pixel, fb->depth);
  83. }
  84. if (!list_empty(&lds->active)) {
  85. entry = list_entry(lds->active.next, typeof(*entry), active);
  86. fb = entry->base.crtc.fb;
  87. vmw_kms_write_svga(dev_priv, fb->width, fb->height, fb->pitch,
  88. fb->bits_per_pixel, fb->depth);
  89. }
  90. /* Make sure we always show something. */
  91. vmw_write(dev_priv, SVGA_REG_NUM_GUEST_DISPLAYS,
  92. lds->num_active ? lds->num_active : 1);
  93. i = 0;
  94. list_for_each_entry(entry, &lds->active, active) {
  95. crtc = &entry->base.crtc;
  96. vmw_write(dev_priv, SVGA_REG_DISPLAY_ID, i);
  97. vmw_write(dev_priv, SVGA_REG_DISPLAY_IS_PRIMARY, !i);
  98. vmw_write(dev_priv, SVGA_REG_DISPLAY_POSITION_X, crtc->x);
  99. vmw_write(dev_priv, SVGA_REG_DISPLAY_POSITION_Y, crtc->y);
  100. vmw_write(dev_priv, SVGA_REG_DISPLAY_WIDTH, crtc->mode.hdisplay);
  101. vmw_write(dev_priv, SVGA_REG_DISPLAY_HEIGHT, crtc->mode.vdisplay);
  102. vmw_write(dev_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
  103. i++;
  104. }
  105. BUG_ON(i != lds->num_active);
  106. lds->last_num_active = lds->num_active;
  107. return 0;
  108. }
  109. static int vmw_ldu_del_active(struct vmw_private *vmw_priv,
  110. struct vmw_legacy_display_unit *ldu)
  111. {
  112. struct vmw_legacy_display *ld = vmw_priv->ldu_priv;
  113. if (list_empty(&ldu->active))
  114. return 0;
  115. /* Must init otherwise list_empty(&ldu->active) will not work. */
  116. list_del_init(&ldu->active);
  117. if (--(ld->num_active) == 0) {
  118. BUG_ON(!ld->fb);
  119. if (ld->fb->unpin)
  120. ld->fb->unpin(ld->fb);
  121. ld->fb = NULL;
  122. }
  123. return 0;
  124. }
  125. static int vmw_ldu_add_active(struct vmw_private *vmw_priv,
  126. struct vmw_legacy_display_unit *ldu,
  127. struct vmw_framebuffer *vfb)
  128. {
  129. struct vmw_legacy_display *ld = vmw_priv->ldu_priv;
  130. struct vmw_legacy_display_unit *entry;
  131. struct list_head *at;
  132. BUG_ON(!ld->num_active && ld->fb);
  133. if (vfb != ld->fb) {
  134. if (ld->fb && ld->fb->unpin)
  135. ld->fb->unpin(ld->fb);
  136. if (vfb->pin)
  137. vfb->pin(vfb);
  138. ld->fb = vfb;
  139. }
  140. if (!list_empty(&ldu->active))
  141. return 0;
  142. at = &ld->active;
  143. list_for_each_entry(entry, &ld->active, active) {
  144. if (entry->base.unit > ldu->base.unit)
  145. break;
  146. at = &entry->active;
  147. }
  148. list_add(&ldu->active, at);
  149. ld->num_active++;
  150. return 0;
  151. }
  152. static int vmw_ldu_crtc_set_config(struct drm_mode_set *set)
  153. {
  154. struct vmw_private *dev_priv;
  155. struct vmw_legacy_display_unit *ldu;
  156. struct drm_connector *connector;
  157. struct drm_display_mode *mode;
  158. struct drm_encoder *encoder;
  159. struct vmw_framebuffer *vfb;
  160. struct drm_framebuffer *fb;
  161. struct drm_crtc *crtc;
  162. if (!set)
  163. return -EINVAL;
  164. if (!set->crtc)
  165. return -EINVAL;
  166. /* get the ldu */
  167. crtc = set->crtc;
  168. ldu = vmw_crtc_to_ldu(crtc);
  169. vfb = set->fb ? vmw_framebuffer_to_vfb(set->fb) : NULL;
  170. dev_priv = vmw_priv(crtc->dev);
  171. if (set->num_connectors > 1) {
  172. DRM_ERROR("to many connectors\n");
  173. return -EINVAL;
  174. }
  175. if (set->num_connectors == 1 &&
  176. set->connectors[0] != &ldu->base.connector) {
  177. DRM_ERROR("connector doesn't match %p %p\n",
  178. set->connectors[0], &ldu->base.connector);
  179. return -EINVAL;
  180. }
  181. /* ldu only supports one fb active at the time */
  182. if (dev_priv->ldu_priv->fb && vfb &&
  183. !(dev_priv->ldu_priv->num_active == 1 &&
  184. !list_empty(&ldu->active)) &&
  185. dev_priv->ldu_priv->fb != vfb) {
  186. DRM_ERROR("Multiple framebuffers not supported\n");
  187. return -EINVAL;
  188. }
  189. /* since they always map one to one these are safe */
  190. connector = &ldu->base.connector;
  191. encoder = &ldu->base.encoder;
  192. /* should we turn the crtc off? */
  193. if (set->num_connectors == 0 || !set->mode || !set->fb) {
  194. connector->encoder = NULL;
  195. encoder->crtc = NULL;
  196. crtc->fb = NULL;
  197. vmw_ldu_del_active(dev_priv, ldu);
  198. return vmw_ldu_commit_list(dev_priv);
  199. }
  200. /* we now know we want to set a mode */
  201. mode = set->mode;
  202. fb = set->fb;
  203. if (set->x + mode->hdisplay > fb->width ||
  204. set->y + mode->vdisplay > fb->height) {
  205. DRM_ERROR("set outside of framebuffer\n");
  206. return -EINVAL;
  207. }
  208. vmw_fb_off(dev_priv);
  209. crtc->fb = fb;
  210. encoder->crtc = crtc;
  211. connector->encoder = encoder;
  212. crtc->x = set->x;
  213. crtc->y = set->y;
  214. crtc->mode = *mode;
  215. vmw_ldu_add_active(dev_priv, ldu, vfb);
  216. return vmw_ldu_commit_list(dev_priv);
  217. }
  218. static struct drm_crtc_funcs vmw_legacy_crtc_funcs = {
  219. .save = vmw_du_crtc_save,
  220. .restore = vmw_du_crtc_restore,
  221. .cursor_set = vmw_du_crtc_cursor_set,
  222. .cursor_move = vmw_du_crtc_cursor_move,
  223. .gamma_set = vmw_du_crtc_gamma_set,
  224. .destroy = vmw_ldu_crtc_destroy,
  225. .set_config = vmw_ldu_crtc_set_config,
  226. };
  227. /*
  228. * Legacy Display Unit encoder functions
  229. */
  230. static void vmw_ldu_encoder_destroy(struct drm_encoder *encoder)
  231. {
  232. vmw_ldu_destroy(vmw_encoder_to_ldu(encoder));
  233. }
  234. static struct drm_encoder_funcs vmw_legacy_encoder_funcs = {
  235. .destroy = vmw_ldu_encoder_destroy,
  236. };
  237. /*
  238. * Legacy Display Unit connector functions
  239. */
  240. static void vmw_ldu_connector_destroy(struct drm_connector *connector)
  241. {
  242. vmw_ldu_destroy(vmw_connector_to_ldu(connector));
  243. }
  244. static struct drm_connector_funcs vmw_legacy_connector_funcs = {
  245. .dpms = vmw_du_connector_dpms,
  246. .save = vmw_du_connector_save,
  247. .restore = vmw_du_connector_restore,
  248. .detect = vmw_du_connector_detect,
  249. .fill_modes = vmw_du_connector_fill_modes,
  250. .set_property = vmw_du_connector_set_property,
  251. .destroy = vmw_ldu_connector_destroy,
  252. };
  253. static int vmw_ldu_init(struct vmw_private *dev_priv, unsigned unit)
  254. {
  255. struct vmw_legacy_display_unit *ldu;
  256. struct drm_device *dev = dev_priv->dev;
  257. struct drm_connector *connector;
  258. struct drm_encoder *encoder;
  259. struct drm_crtc *crtc;
  260. ldu = kzalloc(sizeof(*ldu), GFP_KERNEL);
  261. if (!ldu)
  262. return -ENOMEM;
  263. ldu->base.unit = unit;
  264. crtc = &ldu->base.crtc;
  265. encoder = &ldu->base.encoder;
  266. connector = &ldu->base.connector;
  267. INIT_LIST_HEAD(&ldu->active);
  268. ldu->base.pref_active = (unit == 0);
  269. ldu->base.pref_width = 800;
  270. ldu->base.pref_height = 600;
  271. ldu->base.pref_mode = NULL;
  272. ldu->base.is_implicit = true;
  273. drm_connector_init(dev, connector, &vmw_legacy_connector_funcs,
  274. DRM_MODE_CONNECTOR_VIRTUAL);
  275. connector->status = vmw_du_connector_detect(connector, true);
  276. drm_encoder_init(dev, encoder, &vmw_legacy_encoder_funcs,
  277. DRM_MODE_ENCODER_VIRTUAL);
  278. drm_mode_connector_attach_encoder(connector, encoder);
  279. encoder->possible_crtcs = (1 << unit);
  280. encoder->possible_clones = 0;
  281. drm_crtc_init(dev, crtc, &vmw_legacy_crtc_funcs);
  282. drm_mode_crtc_set_gamma_size(crtc, 256);
  283. drm_connector_attach_property(connector,
  284. dev->mode_config.dirty_info_property,
  285. 1);
  286. return 0;
  287. }
  288. int vmw_kms_init_legacy_display_system(struct vmw_private *dev_priv)
  289. {
  290. struct drm_device *dev = dev_priv->dev;
  291. int i, ret;
  292. if (dev_priv->ldu_priv) {
  293. DRM_INFO("ldu system already on\n");
  294. return -EINVAL;
  295. }
  296. dev_priv->ldu_priv = kmalloc(sizeof(*dev_priv->ldu_priv), GFP_KERNEL);
  297. if (!dev_priv->ldu_priv)
  298. return -ENOMEM;
  299. INIT_LIST_HEAD(&dev_priv->ldu_priv->active);
  300. dev_priv->ldu_priv->num_active = 0;
  301. dev_priv->ldu_priv->last_num_active = 0;
  302. dev_priv->ldu_priv->fb = NULL;
  303. /* for old hardware without multimon only enable one display */
  304. if (dev_priv->capabilities & SVGA_CAP_MULTIMON)
  305. ret = drm_vblank_init(dev, VMWGFX_NUM_DISPLAY_UNITS);
  306. else
  307. ret = drm_vblank_init(dev, 1);
  308. if (ret != 0)
  309. goto err_free;
  310. ret = drm_mode_create_dirty_info_property(dev);
  311. if (ret != 0)
  312. goto err_vblank_cleanup;
  313. if (dev_priv->capabilities & SVGA_CAP_MULTIMON)
  314. for (i = 0; i < VMWGFX_NUM_DISPLAY_UNITS; ++i)
  315. vmw_ldu_init(dev_priv, i);
  316. else
  317. vmw_ldu_init(dev_priv, 0);
  318. return 0;
  319. err_vblank_cleanup:
  320. drm_vblank_cleanup(dev);
  321. err_free:
  322. kfree(dev_priv->ldu_priv);
  323. dev_priv->ldu_priv = NULL;
  324. return ret;
  325. }
  326. int vmw_kms_close_legacy_display_system(struct vmw_private *dev_priv)
  327. {
  328. struct drm_device *dev = dev_priv->dev;
  329. if (!dev_priv->ldu_priv)
  330. return -ENOSYS;
  331. drm_vblank_cleanup(dev);
  332. BUG_ON(!list_empty(&dev_priv->ldu_priv->active));
  333. kfree(dev_priv->ldu_priv);
  334. return 0;
  335. }