vmwgfx_ldu.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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. crtc->enabled = false;
  213. vmw_ldu_del_active(dev_priv, ldu);
  214. return vmw_ldu_commit_list(dev_priv);
  215. }
  216. /* we now know we want to set a mode */
  217. mode = set->mode;
  218. fb = set->fb;
  219. if (set->x + mode->hdisplay > fb->width ||
  220. set->y + mode->vdisplay > fb->height) {
  221. DRM_ERROR("set outside of framebuffer\n");
  222. return -EINVAL;
  223. }
  224. vmw_fb_off(dev_priv);
  225. crtc->fb = fb;
  226. encoder->crtc = crtc;
  227. connector->encoder = encoder;
  228. crtc->x = set->x;
  229. crtc->y = set->y;
  230. crtc->mode = *mode;
  231. crtc->enabled = true;
  232. vmw_ldu_add_active(dev_priv, ldu, vfb);
  233. return vmw_ldu_commit_list(dev_priv);
  234. }
  235. static struct drm_crtc_funcs vmw_legacy_crtc_funcs = {
  236. .save = vmw_du_crtc_save,
  237. .restore = vmw_du_crtc_restore,
  238. .cursor_set = vmw_du_crtc_cursor_set,
  239. .cursor_move = vmw_du_crtc_cursor_move,
  240. .gamma_set = vmw_du_crtc_gamma_set,
  241. .destroy = vmw_ldu_crtc_destroy,
  242. .set_config = vmw_ldu_crtc_set_config,
  243. };
  244. /*
  245. * Legacy Display Unit encoder functions
  246. */
  247. static void vmw_ldu_encoder_destroy(struct drm_encoder *encoder)
  248. {
  249. vmw_ldu_destroy(vmw_encoder_to_ldu(encoder));
  250. }
  251. static struct drm_encoder_funcs vmw_legacy_encoder_funcs = {
  252. .destroy = vmw_ldu_encoder_destroy,
  253. };
  254. /*
  255. * Legacy Display Unit connector functions
  256. */
  257. static void vmw_ldu_connector_destroy(struct drm_connector *connector)
  258. {
  259. vmw_ldu_destroy(vmw_connector_to_ldu(connector));
  260. }
  261. static struct drm_connector_funcs vmw_legacy_connector_funcs = {
  262. .dpms = vmw_du_connector_dpms,
  263. .save = vmw_du_connector_save,
  264. .restore = vmw_du_connector_restore,
  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);
  295. drm_mode_connector_attach_encoder(connector, encoder);
  296. encoder->possible_crtcs = (1 << unit);
  297. encoder->possible_clones = 0;
  298. (void) drm_sysfs_connector_add(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->mode_config.dirty_info_property,
  303. 1);
  304. return 0;
  305. }
  306. int vmw_kms_init_legacy_display_system(struct vmw_private *dev_priv)
  307. {
  308. struct drm_device *dev = dev_priv->dev;
  309. int i, ret;
  310. if (dev_priv->ldu_priv) {
  311. DRM_INFO("ldu system already on\n");
  312. return -EINVAL;
  313. }
  314. dev_priv->ldu_priv = kmalloc(sizeof(*dev_priv->ldu_priv), GFP_KERNEL);
  315. if (!dev_priv->ldu_priv)
  316. return -ENOMEM;
  317. INIT_LIST_HEAD(&dev_priv->ldu_priv->active);
  318. dev_priv->ldu_priv->num_active = 0;
  319. dev_priv->ldu_priv->last_num_active = 0;
  320. dev_priv->ldu_priv->fb = NULL;
  321. /* for old hardware without multimon only enable one display */
  322. if (dev_priv->capabilities & SVGA_CAP_MULTIMON)
  323. ret = drm_vblank_init(dev, VMWGFX_NUM_DISPLAY_UNITS);
  324. else
  325. ret = drm_vblank_init(dev, 1);
  326. if (ret != 0)
  327. goto err_free;
  328. ret = drm_mode_create_dirty_info_property(dev);
  329. if (ret != 0)
  330. goto err_vblank_cleanup;
  331. if (dev_priv->capabilities & SVGA_CAP_MULTIMON)
  332. for (i = 0; i < VMWGFX_NUM_DISPLAY_UNITS; ++i)
  333. vmw_ldu_init(dev_priv, i);
  334. else
  335. vmw_ldu_init(dev_priv, 0);
  336. return 0;
  337. err_vblank_cleanup:
  338. drm_vblank_cleanup(dev);
  339. err_free:
  340. kfree(dev_priv->ldu_priv);
  341. dev_priv->ldu_priv = NULL;
  342. return ret;
  343. }
  344. int vmw_kms_close_legacy_display_system(struct vmw_private *dev_priv)
  345. {
  346. struct drm_device *dev = dev_priv->dev;
  347. if (!dev_priv->ldu_priv)
  348. return -ENOSYS;
  349. drm_vblank_cleanup(dev);
  350. BUG_ON(!list_empty(&dev_priv->ldu_priv->active));
  351. kfree(dev_priv->ldu_priv);
  352. return 0;
  353. }