vmwgfx_ldu.c 12 KB

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