vmwgfx_ldu.c 15 KB

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