vmwgfx_ldu.c 11 KB

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