vmwgfx_ldu.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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 VMWGFX_LDU_NUM_DU 8
  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_display_unit_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 drm_framebuffer *fb = NULL;
  66. struct drm_crtc *crtc = NULL;
  67. int i = 0;
  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->pitch,
  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->pitch,
  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. return 0;
  109. }
  110. static int vmw_ldu_del_active(struct vmw_private *vmw_priv,
  111. struct vmw_legacy_display_unit *ldu)
  112. {
  113. struct vmw_legacy_display *ld = vmw_priv->ldu_priv;
  114. if (list_empty(&ldu->active))
  115. return 0;
  116. /* Must init otherwise list_empty(&ldu->active) will not work. */
  117. list_del_init(&ldu->active);
  118. if (--(ld->num_active) == 0) {
  119. BUG_ON(!ld->fb);
  120. if (ld->fb->unpin)
  121. ld->fb->unpin(ld->fb);
  122. ld->fb = NULL;
  123. }
  124. return 0;
  125. }
  126. static int vmw_ldu_add_active(struct vmw_private *vmw_priv,
  127. struct vmw_legacy_display_unit *ldu,
  128. struct vmw_framebuffer *vfb)
  129. {
  130. struct vmw_legacy_display *ld = vmw_priv->ldu_priv;
  131. struct vmw_legacy_display_unit *entry;
  132. struct list_head *at;
  133. BUG_ON(!ld->num_active && ld->fb);
  134. if (vfb != ld->fb) {
  135. if (ld->fb && ld->fb->unpin)
  136. ld->fb->unpin(ld->fb);
  137. if (vfb->pin)
  138. vfb->pin(vfb);
  139. ld->fb = vfb;
  140. }
  141. if (!list_empty(&ldu->active))
  142. return 0;
  143. at = &ld->active;
  144. list_for_each_entry(entry, &ld->active, active) {
  145. if (entry->base.unit > ldu->base.unit)
  146. break;
  147. at = &entry->active;
  148. }
  149. list_add(&ldu->active, at);
  150. ld->num_active++;
  151. return 0;
  152. }
  153. static int vmw_ldu_crtc_set_config(struct drm_mode_set *set)
  154. {
  155. struct vmw_private *dev_priv;
  156. struct vmw_legacy_display_unit *ldu;
  157. struct drm_connector *connector;
  158. struct drm_display_mode *mode;
  159. struct drm_encoder *encoder;
  160. struct vmw_framebuffer *vfb;
  161. struct drm_framebuffer *fb;
  162. struct drm_crtc *crtc;
  163. if (!set)
  164. return -EINVAL;
  165. if (!set->crtc)
  166. return -EINVAL;
  167. /* get the ldu */
  168. crtc = set->crtc;
  169. ldu = vmw_crtc_to_ldu(crtc);
  170. vfb = set->fb ? vmw_framebuffer_to_vfb(set->fb) : NULL;
  171. dev_priv = vmw_priv(crtc->dev);
  172. if (set->num_connectors > 1) {
  173. DRM_ERROR("to many connectors\n");
  174. return -EINVAL;
  175. }
  176. if (set->num_connectors == 1 &&
  177. set->connectors[0] != &ldu->base.connector) {
  178. DRM_ERROR("connector doesn't match %p %p\n",
  179. set->connectors[0], &ldu->base.connector);
  180. return -EINVAL;
  181. }
  182. /* ldu only supports one fb active at the time */
  183. if (dev_priv->ldu_priv->fb && vfb &&
  184. !(dev_priv->ldu_priv->num_active == 1 &&
  185. !list_empty(&ldu->active)) &&
  186. dev_priv->ldu_priv->fb != vfb) {
  187. DRM_ERROR("Multiple framebuffers not supported\n");
  188. return -EINVAL;
  189. }
  190. /* since they always map one to one these are safe */
  191. connector = &ldu->base.connector;
  192. encoder = &ldu->base.encoder;
  193. /* should we turn the crtc off? */
  194. if (set->num_connectors == 0 || !set->mode || !set->fb) {
  195. connector->encoder = NULL;
  196. encoder->crtc = NULL;
  197. crtc->fb = NULL;
  198. vmw_ldu_del_active(dev_priv, ldu);
  199. return vmw_ldu_commit_list(dev_priv);
  200. }
  201. /* we now know we want to set a mode */
  202. mode = set->mode;
  203. fb = set->fb;
  204. if (set->x + mode->hdisplay > fb->width ||
  205. set->y + mode->vdisplay > fb->height) {
  206. DRM_ERROR("set outside of framebuffer\n");
  207. return -EINVAL;
  208. }
  209. vmw_fb_off(dev_priv);
  210. crtc->fb = fb;
  211. encoder->crtc = crtc;
  212. connector->encoder = encoder;
  213. crtc->x = set->x;
  214. crtc->y = set->y;
  215. crtc->mode = *mode;
  216. vmw_ldu_add_active(dev_priv, ldu, vfb);
  217. return vmw_ldu_commit_list(dev_priv);
  218. }
  219. static struct drm_crtc_funcs vmw_legacy_crtc_funcs = {
  220. .save = vmw_du_crtc_save,
  221. .restore = vmw_du_crtc_restore,
  222. .cursor_set = vmw_du_crtc_cursor_set,
  223. .cursor_move = vmw_du_crtc_cursor_move,
  224. .gamma_set = vmw_du_crtc_gamma_set,
  225. .destroy = vmw_ldu_crtc_destroy,
  226. .set_config = vmw_ldu_crtc_set_config,
  227. };
  228. /*
  229. * Legacy Display Unit encoder functions
  230. */
  231. static void vmw_ldu_encoder_destroy(struct drm_encoder *encoder)
  232. {
  233. vmw_ldu_destroy(vmw_encoder_to_ldu(encoder));
  234. }
  235. static struct drm_encoder_funcs vmw_legacy_encoder_funcs = {
  236. .destroy = vmw_ldu_encoder_destroy,
  237. };
  238. /*
  239. * Legacy Display Unit connector functions
  240. */
  241. static void vmw_ldu_connector_destroy(struct drm_connector *connector)
  242. {
  243. vmw_ldu_destroy(vmw_connector_to_ldu(connector));
  244. }
  245. static struct drm_connector_funcs vmw_legacy_connector_funcs = {
  246. .dpms = vmw_du_connector_dpms,
  247. .save = vmw_du_connector_save,
  248. .restore = vmw_du_connector_restore,
  249. .detect = vmw_du_connector_detect,
  250. .fill_modes = vmw_du_connector_fill_modes,
  251. .set_property = vmw_du_connector_set_property,
  252. .destroy = vmw_ldu_connector_destroy,
  253. };
  254. static int vmw_ldu_init(struct vmw_private *dev_priv, unsigned unit)
  255. {
  256. struct vmw_legacy_display_unit *ldu;
  257. struct drm_device *dev = dev_priv->dev;
  258. struct drm_connector *connector;
  259. struct drm_encoder *encoder;
  260. struct drm_crtc *crtc;
  261. ldu = kzalloc(sizeof(*ldu), GFP_KERNEL);
  262. if (!ldu)
  263. return -ENOMEM;
  264. ldu->base.unit = unit;
  265. crtc = &ldu->base.crtc;
  266. encoder = &ldu->base.encoder;
  267. connector = &ldu->base.connector;
  268. INIT_LIST_HEAD(&ldu->active);
  269. ldu->base.pref_active = (unit == 0);
  270. ldu->base.pref_width = 800;
  271. ldu->base.pref_height = 600;
  272. ldu->base.pref_mode = NULL;
  273. drm_connector_init(dev, connector, &vmw_legacy_connector_funcs,
  274. DRM_MODE_CONNECTOR_LVDS);
  275. connector->status = vmw_du_connector_detect(connector, true);
  276. drm_encoder_init(dev, encoder, &vmw_legacy_encoder_funcs,
  277. DRM_MODE_ENCODER_LVDS);
  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;
  292. int ret;
  293. if (dev_priv->ldu_priv) {
  294. DRM_INFO("ldu system already on\n");
  295. return -EINVAL;
  296. }
  297. dev_priv->ldu_priv = kmalloc(sizeof(*dev_priv->ldu_priv), GFP_KERNEL);
  298. if (!dev_priv->ldu_priv)
  299. return -ENOMEM;
  300. INIT_LIST_HEAD(&dev_priv->ldu_priv->active);
  301. dev_priv->ldu_priv->num_active = 0;
  302. dev_priv->ldu_priv->last_num_active = 0;
  303. dev_priv->ldu_priv->fb = NULL;
  304. drm_mode_create_dirty_info_property(dev_priv->dev);
  305. if (dev_priv->capabilities & SVGA_CAP_MULTIMON) {
  306. for (i = 0; i < VMWGFX_LDU_NUM_DU; ++i)
  307. vmw_ldu_init(dev_priv, i);
  308. ret = drm_vblank_init(dev, VMWGFX_LDU_NUM_DU);
  309. } else {
  310. /* for old hardware without multimon only enable one display */
  311. vmw_ldu_init(dev_priv, 0);
  312. ret = drm_vblank_init(dev, 1);
  313. }
  314. return ret;
  315. }
  316. int vmw_kms_close_legacy_display_system(struct vmw_private *dev_priv)
  317. {
  318. struct drm_device *dev = dev_priv->dev;
  319. drm_vblank_cleanup(dev);
  320. if (!dev_priv->ldu_priv)
  321. return -ENOSYS;
  322. BUG_ON(!list_empty(&dev_priv->ldu_priv->active));
  323. kfree(dev_priv->ldu_priv);
  324. return 0;
  325. }