vmwgfx_ldu.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  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. #include <drm/drm_atomic.h>
  30. #include <drm/drm_atomic_helper.h>
  31. #define vmw_crtc_to_ldu(x) \
  32. container_of(x, struct vmw_legacy_display_unit, base.crtc)
  33. #define vmw_encoder_to_ldu(x) \
  34. container_of(x, struct vmw_legacy_display_unit, base.encoder)
  35. #define vmw_connector_to_ldu(x) \
  36. container_of(x, struct vmw_legacy_display_unit, base.connector)
  37. struct vmw_legacy_display {
  38. struct list_head active;
  39. unsigned num_active;
  40. unsigned last_num_active;
  41. struct vmw_framebuffer *fb;
  42. };
  43. /**
  44. * Display unit using the legacy register interface.
  45. */
  46. struct vmw_legacy_display_unit {
  47. struct vmw_display_unit base;
  48. struct list_head active;
  49. };
  50. static void vmw_ldu_destroy(struct vmw_legacy_display_unit *ldu)
  51. {
  52. list_del_init(&ldu->active);
  53. vmw_du_cleanup(&ldu->base);
  54. kfree(ldu);
  55. }
  56. /*
  57. * Legacy Display Unit CRTC functions
  58. */
  59. static void vmw_ldu_crtc_destroy(struct drm_crtc *crtc)
  60. {
  61. vmw_ldu_destroy(vmw_crtc_to_ldu(crtc));
  62. }
  63. static int vmw_ldu_commit_list(struct vmw_private *dev_priv)
  64. {
  65. struct vmw_legacy_display *lds = dev_priv->ldu_priv;
  66. struct vmw_legacy_display_unit *entry;
  67. struct drm_framebuffer *fb = NULL;
  68. struct drm_crtc *crtc = NULL;
  69. int i = 0;
  70. /* If there is no display topology the host just assumes
  71. * that the guest will set the same layout as the host.
  72. */
  73. if (!(dev_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY)) {
  74. int w = 0, h = 0;
  75. list_for_each_entry(entry, &lds->active, active) {
  76. crtc = &entry->base.crtc;
  77. w = max(w, crtc->x + crtc->mode.hdisplay);
  78. h = max(h, crtc->y + crtc->mode.vdisplay);
  79. i++;
  80. }
  81. if (crtc == NULL)
  82. return 0;
  83. fb = entry->base.crtc.primary->state->fb;
  84. return vmw_kms_write_svga(dev_priv, w, h, fb->pitches[0],
  85. fb->format->cpp[0] * 8,
  86. fb->format->depth);
  87. }
  88. if (!list_empty(&lds->active)) {
  89. entry = list_entry(lds->active.next, typeof(*entry), active);
  90. fb = entry->base.crtc.primary->state->fb;
  91. vmw_kms_write_svga(dev_priv, fb->width, fb->height, fb->pitches[0],
  92. fb->format->cpp[0] * 8, fb->format->depth);
  93. }
  94. /* Make sure we always show something. */
  95. vmw_write(dev_priv, SVGA_REG_NUM_GUEST_DISPLAYS,
  96. lds->num_active ? lds->num_active : 1);
  97. i = 0;
  98. list_for_each_entry(entry, &lds->active, active) {
  99. crtc = &entry->base.crtc;
  100. vmw_write(dev_priv, SVGA_REG_DISPLAY_ID, i);
  101. vmw_write(dev_priv, SVGA_REG_DISPLAY_IS_PRIMARY, !i);
  102. vmw_write(dev_priv, SVGA_REG_DISPLAY_POSITION_X, crtc->x);
  103. vmw_write(dev_priv, SVGA_REG_DISPLAY_POSITION_Y, crtc->y);
  104. vmw_write(dev_priv, SVGA_REG_DISPLAY_WIDTH, crtc->mode.hdisplay);
  105. vmw_write(dev_priv, SVGA_REG_DISPLAY_HEIGHT, crtc->mode.vdisplay);
  106. vmw_write(dev_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
  107. i++;
  108. }
  109. BUG_ON(i != lds->num_active);
  110. lds->last_num_active = lds->num_active;
  111. return 0;
  112. }
  113. static int vmw_ldu_del_active(struct vmw_private *vmw_priv,
  114. struct vmw_legacy_display_unit *ldu)
  115. {
  116. struct vmw_legacy_display *ld = vmw_priv->ldu_priv;
  117. if (list_empty(&ldu->active))
  118. return 0;
  119. /* Must init otherwise list_empty(&ldu->active) will not work. */
  120. list_del_init(&ldu->active);
  121. if (--(ld->num_active) == 0) {
  122. BUG_ON(!ld->fb);
  123. if (ld->fb->unpin)
  124. ld->fb->unpin(ld->fb);
  125. ld->fb = NULL;
  126. }
  127. return 0;
  128. }
  129. static int vmw_ldu_add_active(struct vmw_private *vmw_priv,
  130. struct vmw_legacy_display_unit *ldu,
  131. struct vmw_framebuffer *vfb)
  132. {
  133. struct vmw_legacy_display *ld = vmw_priv->ldu_priv;
  134. struct vmw_legacy_display_unit *entry;
  135. struct list_head *at;
  136. BUG_ON(!ld->num_active && ld->fb);
  137. if (vfb != ld->fb) {
  138. if (ld->fb && ld->fb->unpin)
  139. ld->fb->unpin(ld->fb);
  140. vmw_svga_enable(vmw_priv);
  141. if (vfb->pin)
  142. vfb->pin(vfb);
  143. ld->fb = vfb;
  144. }
  145. if (!list_empty(&ldu->active))
  146. return 0;
  147. at = &ld->active;
  148. list_for_each_entry(entry, &ld->active, active) {
  149. if (entry->base.unit > ldu->base.unit)
  150. break;
  151. at = &entry->active;
  152. }
  153. list_add(&ldu->active, at);
  154. ld->num_active++;
  155. return 0;
  156. }
  157. /**
  158. * vmw_ldu_crtc_mode_set_nofb - Enable svga
  159. *
  160. * @crtc: CRTC associated with the new screen
  161. *
  162. * For LDU, just enable the svga
  163. */
  164. static void vmw_ldu_crtc_mode_set_nofb(struct drm_crtc *crtc)
  165. {
  166. }
  167. /**
  168. * vmw_ldu_crtc_atomic_enable - Noop
  169. *
  170. * @crtc: CRTC associated with the new screen
  171. *
  172. * This is called after a mode set has been completed. Here's
  173. * usually a good place to call vmw_ldu_add_active/vmw_ldu_del_active
  174. * but since for LDU the display plane is closely tied to the
  175. * CRTC, it makes more sense to do those at plane update time.
  176. */
  177. static void vmw_ldu_crtc_atomic_enable(struct drm_crtc *crtc,
  178. struct drm_crtc_state *old_state)
  179. {
  180. }
  181. /**
  182. * vmw_ldu_crtc_atomic_disable - Turns off CRTC
  183. *
  184. * @crtc: CRTC to be turned off
  185. */
  186. static void vmw_ldu_crtc_atomic_disable(struct drm_crtc *crtc,
  187. struct drm_crtc_state *old_state)
  188. {
  189. }
  190. static const struct drm_crtc_funcs vmw_legacy_crtc_funcs = {
  191. .gamma_set = vmw_du_crtc_gamma_set,
  192. .destroy = vmw_ldu_crtc_destroy,
  193. .reset = vmw_du_crtc_reset,
  194. .atomic_duplicate_state = vmw_du_crtc_duplicate_state,
  195. .atomic_destroy_state = vmw_du_crtc_destroy_state,
  196. .set_config = vmw_kms_set_config,
  197. };
  198. /*
  199. * Legacy Display Unit encoder functions
  200. */
  201. static void vmw_ldu_encoder_destroy(struct drm_encoder *encoder)
  202. {
  203. vmw_ldu_destroy(vmw_encoder_to_ldu(encoder));
  204. }
  205. static const struct drm_encoder_funcs vmw_legacy_encoder_funcs = {
  206. .destroy = vmw_ldu_encoder_destroy,
  207. };
  208. /*
  209. * Legacy Display Unit connector functions
  210. */
  211. static void vmw_ldu_connector_destroy(struct drm_connector *connector)
  212. {
  213. vmw_ldu_destroy(vmw_connector_to_ldu(connector));
  214. }
  215. static const struct drm_connector_funcs vmw_legacy_connector_funcs = {
  216. .dpms = vmw_du_connector_dpms,
  217. .detect = vmw_du_connector_detect,
  218. .fill_modes = vmw_du_connector_fill_modes,
  219. .set_property = vmw_du_connector_set_property,
  220. .destroy = vmw_ldu_connector_destroy,
  221. .reset = vmw_du_connector_reset,
  222. .atomic_duplicate_state = vmw_du_connector_duplicate_state,
  223. .atomic_destroy_state = vmw_du_connector_destroy_state,
  224. .atomic_set_property = vmw_du_connector_atomic_set_property,
  225. .atomic_get_property = vmw_du_connector_atomic_get_property,
  226. };
  227. static const struct
  228. drm_connector_helper_funcs vmw_ldu_connector_helper_funcs = {
  229. .best_encoder = drm_atomic_helper_best_encoder,
  230. };
  231. /*
  232. * Legacy Display Plane Functions
  233. */
  234. static void
  235. vmw_ldu_primary_plane_atomic_update(struct drm_plane *plane,
  236. struct drm_plane_state *old_state)
  237. {
  238. struct vmw_private *dev_priv;
  239. struct vmw_legacy_display_unit *ldu;
  240. struct vmw_framebuffer *vfb;
  241. struct drm_framebuffer *fb;
  242. struct drm_crtc *crtc = plane->state->crtc ?: old_state->crtc;
  243. ldu = vmw_crtc_to_ldu(crtc);
  244. dev_priv = vmw_priv(plane->dev);
  245. fb = plane->state->fb;
  246. vfb = (fb) ? vmw_framebuffer_to_vfb(fb) : NULL;
  247. if (vfb)
  248. vmw_ldu_add_active(dev_priv, ldu, vfb);
  249. else
  250. vmw_ldu_del_active(dev_priv, ldu);
  251. vmw_ldu_commit_list(dev_priv);
  252. }
  253. static const struct drm_plane_funcs vmw_ldu_plane_funcs = {
  254. .update_plane = drm_atomic_helper_update_plane,
  255. .disable_plane = drm_atomic_helper_disable_plane,
  256. .destroy = vmw_du_primary_plane_destroy,
  257. .reset = vmw_du_plane_reset,
  258. .atomic_duplicate_state = vmw_du_plane_duplicate_state,
  259. .atomic_destroy_state = vmw_du_plane_destroy_state,
  260. };
  261. static const struct drm_plane_funcs vmw_ldu_cursor_funcs = {
  262. .update_plane = drm_atomic_helper_update_plane,
  263. .disable_plane = drm_atomic_helper_disable_plane,
  264. .destroy = vmw_du_cursor_plane_destroy,
  265. .reset = vmw_du_plane_reset,
  266. .atomic_duplicate_state = vmw_du_plane_duplicate_state,
  267. .atomic_destroy_state = vmw_du_plane_destroy_state,
  268. };
  269. /*
  270. * Atomic Helpers
  271. */
  272. static const struct
  273. drm_plane_helper_funcs vmw_ldu_cursor_plane_helper_funcs = {
  274. .atomic_check = vmw_du_cursor_plane_atomic_check,
  275. .atomic_update = vmw_du_cursor_plane_atomic_update,
  276. .prepare_fb = vmw_du_cursor_plane_prepare_fb,
  277. .cleanup_fb = vmw_du_plane_cleanup_fb,
  278. };
  279. static const struct
  280. drm_plane_helper_funcs vmw_ldu_primary_plane_helper_funcs = {
  281. .atomic_check = vmw_du_primary_plane_atomic_check,
  282. .atomic_update = vmw_ldu_primary_plane_atomic_update,
  283. };
  284. static const struct drm_crtc_helper_funcs vmw_ldu_crtc_helper_funcs = {
  285. .mode_set_nofb = vmw_ldu_crtc_mode_set_nofb,
  286. .atomic_check = vmw_du_crtc_atomic_check,
  287. .atomic_begin = vmw_du_crtc_atomic_begin,
  288. .atomic_flush = vmw_du_crtc_atomic_flush,
  289. .atomic_enable = vmw_ldu_crtc_atomic_enable,
  290. .atomic_disable = vmw_ldu_crtc_atomic_disable,
  291. };
  292. static int vmw_ldu_init(struct vmw_private *dev_priv, unsigned unit)
  293. {
  294. struct vmw_legacy_display_unit *ldu;
  295. struct drm_device *dev = dev_priv->dev;
  296. struct drm_connector *connector;
  297. struct drm_encoder *encoder;
  298. struct drm_plane *primary, *cursor;
  299. struct drm_crtc *crtc;
  300. int ret;
  301. ldu = kzalloc(sizeof(*ldu), GFP_KERNEL);
  302. if (!ldu)
  303. return -ENOMEM;
  304. ldu->base.unit = unit;
  305. crtc = &ldu->base.crtc;
  306. encoder = &ldu->base.encoder;
  307. connector = &ldu->base.connector;
  308. primary = &ldu->base.primary;
  309. cursor = &ldu->base.cursor;
  310. INIT_LIST_HEAD(&ldu->active);
  311. ldu->base.pref_active = (unit == 0);
  312. ldu->base.pref_width = dev_priv->initial_width;
  313. ldu->base.pref_height = dev_priv->initial_height;
  314. ldu->base.pref_mode = NULL;
  315. /*
  316. * Remove this after enabling atomic because property values can
  317. * only exist in a state object
  318. */
  319. ldu->base.is_implicit = true;
  320. /* Initialize primary plane */
  321. vmw_du_plane_reset(primary);
  322. ret = drm_universal_plane_init(dev, &ldu->base.primary,
  323. 0, &vmw_ldu_plane_funcs,
  324. vmw_primary_plane_formats,
  325. ARRAY_SIZE(vmw_primary_plane_formats),
  326. NULL, DRM_PLANE_TYPE_PRIMARY, NULL);
  327. if (ret) {
  328. DRM_ERROR("Failed to initialize primary plane");
  329. goto err_free;
  330. }
  331. drm_plane_helper_add(primary, &vmw_ldu_primary_plane_helper_funcs);
  332. /* Initialize cursor plane */
  333. vmw_du_plane_reset(cursor);
  334. ret = drm_universal_plane_init(dev, &ldu->base.cursor,
  335. 0, &vmw_ldu_cursor_funcs,
  336. vmw_cursor_plane_formats,
  337. ARRAY_SIZE(vmw_cursor_plane_formats),
  338. NULL, DRM_PLANE_TYPE_CURSOR, NULL);
  339. if (ret) {
  340. DRM_ERROR("Failed to initialize cursor plane");
  341. drm_plane_cleanup(&ldu->base.primary);
  342. goto err_free;
  343. }
  344. drm_plane_helper_add(cursor, &vmw_ldu_cursor_plane_helper_funcs);
  345. vmw_du_connector_reset(connector);
  346. ret = drm_connector_init(dev, connector, &vmw_legacy_connector_funcs,
  347. DRM_MODE_CONNECTOR_VIRTUAL);
  348. if (ret) {
  349. DRM_ERROR("Failed to initialize connector\n");
  350. goto err_free;
  351. }
  352. drm_connector_helper_add(connector, &vmw_ldu_connector_helper_funcs);
  353. connector->status = vmw_du_connector_detect(connector, true);
  354. vmw_connector_state_to_vcs(connector->state)->is_implicit = true;
  355. ret = drm_encoder_init(dev, encoder, &vmw_legacy_encoder_funcs,
  356. DRM_MODE_ENCODER_VIRTUAL, NULL);
  357. if (ret) {
  358. DRM_ERROR("Failed to initialize encoder\n");
  359. goto err_free_connector;
  360. }
  361. (void) drm_mode_connector_attach_encoder(connector, encoder);
  362. encoder->possible_crtcs = (1 << unit);
  363. encoder->possible_clones = 0;
  364. ret = drm_connector_register(connector);
  365. if (ret) {
  366. DRM_ERROR("Failed to register connector\n");
  367. goto err_free_encoder;
  368. }
  369. vmw_du_crtc_reset(crtc);
  370. ret = drm_crtc_init_with_planes(dev, crtc, &ldu->base.primary,
  371. &ldu->base.cursor,
  372. &vmw_legacy_crtc_funcs, NULL);
  373. if (ret) {
  374. DRM_ERROR("Failed to initialize CRTC\n");
  375. goto err_free_unregister;
  376. }
  377. drm_crtc_helper_add(crtc, &vmw_ldu_crtc_helper_funcs);
  378. drm_mode_crtc_set_gamma_size(crtc, 256);
  379. drm_object_attach_property(&connector->base,
  380. dev_priv->hotplug_mode_update_property, 1);
  381. drm_object_attach_property(&connector->base,
  382. dev->mode_config.suggested_x_property, 0);
  383. drm_object_attach_property(&connector->base,
  384. dev->mode_config.suggested_y_property, 0);
  385. if (dev_priv->implicit_placement_property)
  386. drm_object_attach_property
  387. (&connector->base,
  388. dev_priv->implicit_placement_property,
  389. 1);
  390. return 0;
  391. err_free_unregister:
  392. drm_connector_unregister(connector);
  393. err_free_encoder:
  394. drm_encoder_cleanup(encoder);
  395. err_free_connector:
  396. drm_connector_cleanup(connector);
  397. err_free:
  398. kfree(ldu);
  399. return ret;
  400. }
  401. int vmw_kms_ldu_init_display(struct vmw_private *dev_priv)
  402. {
  403. struct drm_device *dev = dev_priv->dev;
  404. int i, ret;
  405. if (dev_priv->ldu_priv) {
  406. DRM_INFO("ldu system already on\n");
  407. return -EINVAL;
  408. }
  409. dev_priv->ldu_priv = kmalloc(sizeof(*dev_priv->ldu_priv), GFP_KERNEL);
  410. if (!dev_priv->ldu_priv)
  411. return -ENOMEM;
  412. INIT_LIST_HEAD(&dev_priv->ldu_priv->active);
  413. dev_priv->ldu_priv->num_active = 0;
  414. dev_priv->ldu_priv->last_num_active = 0;
  415. dev_priv->ldu_priv->fb = NULL;
  416. /* for old hardware without multimon only enable one display */
  417. if (dev_priv->capabilities & SVGA_CAP_MULTIMON)
  418. ret = drm_vblank_init(dev, VMWGFX_NUM_DISPLAY_UNITS);
  419. else
  420. ret = drm_vblank_init(dev, 1);
  421. if (ret != 0)
  422. goto err_free;
  423. vmw_kms_create_implicit_placement_property(dev_priv, true);
  424. if (dev_priv->capabilities & SVGA_CAP_MULTIMON)
  425. for (i = 0; i < VMWGFX_NUM_DISPLAY_UNITS; ++i)
  426. vmw_ldu_init(dev_priv, i);
  427. else
  428. vmw_ldu_init(dev_priv, 0);
  429. dev_priv->active_display_unit = vmw_du_legacy;
  430. DRM_INFO("Legacy Display Unit initialized\n");
  431. return 0;
  432. err_free:
  433. kfree(dev_priv->ldu_priv);
  434. dev_priv->ldu_priv = NULL;
  435. return ret;
  436. }
  437. int vmw_kms_ldu_close_display(struct vmw_private *dev_priv)
  438. {
  439. if (!dev_priv->ldu_priv)
  440. return -ENOSYS;
  441. BUG_ON(!list_empty(&dev_priv->ldu_priv->active));
  442. kfree(dev_priv->ldu_priv);
  443. return 0;
  444. }
  445. int vmw_kms_ldu_do_bo_dirty(struct vmw_private *dev_priv,
  446. struct vmw_framebuffer *framebuffer,
  447. unsigned int flags, unsigned int color,
  448. struct drm_clip_rect *clips,
  449. unsigned int num_clips, int increment)
  450. {
  451. size_t fifo_size;
  452. int i;
  453. struct {
  454. uint32_t header;
  455. SVGAFifoCmdUpdate body;
  456. } *cmd;
  457. fifo_size = sizeof(*cmd) * num_clips;
  458. cmd = vmw_fifo_reserve(dev_priv, fifo_size);
  459. if (unlikely(cmd == NULL)) {
  460. DRM_ERROR("Fifo reserve failed.\n");
  461. return -ENOMEM;
  462. }
  463. memset(cmd, 0, fifo_size);
  464. for (i = 0; i < num_clips; i++, clips += increment) {
  465. cmd[i].header = SVGA_CMD_UPDATE;
  466. cmd[i].body.x = clips->x1;
  467. cmd[i].body.y = clips->y1;
  468. cmd[i].body.width = clips->x2 - clips->x1;
  469. cmd[i].body.height = clips->y2 - clips->y1;
  470. }
  471. vmw_fifo_commit(dev_priv, fifo_size);
  472. return 0;
  473. }