vmwgfx_ldu.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  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 <drm/drm_atomic.h>
  28. #include <drm/drm_atomic_helper.h>
  29. #include <drm/drm_fourcc.h>
  30. #include <drm/drm_plane_helper.h>
  31. #include <drm/drm_vblank.h>
  32. #include "vmwgfx_kms.h"
  33. #define vmw_crtc_to_ldu(x) \
  34. container_of(x, struct vmw_legacy_display_unit, base.crtc)
  35. #define vmw_encoder_to_ldu(x) \
  36. container_of(x, struct vmw_legacy_display_unit, base.encoder)
  37. #define vmw_connector_to_ldu(x) \
  38. container_of(x, struct vmw_legacy_display_unit, base.connector)
  39. struct vmw_legacy_display {
  40. struct list_head active;
  41. unsigned num_active;
  42. unsigned last_num_active;
  43. struct vmw_framebuffer *fb;
  44. };
  45. /**
  46. * Display unit using the legacy register interface.
  47. */
  48. struct vmw_legacy_display_unit {
  49. struct vmw_display_unit base;
  50. struct list_head active;
  51. };
  52. static void vmw_ldu_destroy(struct vmw_legacy_display_unit *ldu)
  53. {
  54. list_del_init(&ldu->active);
  55. vmw_du_cleanup(&ldu->base);
  56. kfree(ldu);
  57. }
  58. /*
  59. * Legacy Display Unit CRTC functions
  60. */
  61. static void vmw_ldu_crtc_destroy(struct drm_crtc *crtc)
  62. {
  63. vmw_ldu_destroy(vmw_crtc_to_ldu(crtc));
  64. }
  65. static int vmw_ldu_commit_list(struct vmw_private *dev_priv)
  66. {
  67. struct vmw_legacy_display *lds = dev_priv->ldu_priv;
  68. struct vmw_legacy_display_unit *entry;
  69. struct drm_framebuffer *fb = NULL;
  70. struct drm_crtc *crtc = NULL;
  71. int i;
  72. /* If there is no display topology the host just assumes
  73. * that the guest will set the same layout as the host.
  74. */
  75. if (!(dev_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY)) {
  76. int w = 0, h = 0;
  77. list_for_each_entry(entry, &lds->active, active) {
  78. crtc = &entry->base.crtc;
  79. w = max(w, crtc->x + crtc->mode.hdisplay);
  80. h = max(h, crtc->y + crtc->mode.vdisplay);
  81. }
  82. if (crtc == NULL)
  83. return 0;
  84. fb = crtc->primary->state->fb;
  85. return vmw_kms_write_svga(dev_priv, w, h, fb->pitches[0],
  86. fb->format->cpp[0] * 8,
  87. fb->format->depth);
  88. }
  89. if (!list_empty(&lds->active)) {
  90. entry = list_entry(lds->active.next, typeof(*entry), active);
  91. fb = entry->base.crtc.primary->state->fb;
  92. vmw_kms_write_svga(dev_priv, fb->width, fb->height, fb->pitches[0],
  93. fb->format->cpp[0] * 8, fb->format->depth);
  94. }
  95. /* Make sure we always show something. */
  96. vmw_write(dev_priv, SVGA_REG_NUM_GUEST_DISPLAYS,
  97. lds->num_active ? lds->num_active : 1);
  98. i = 0;
  99. list_for_each_entry(entry, &lds->active, active) {
  100. crtc = &entry->base.crtc;
  101. vmw_write(dev_priv, SVGA_REG_DISPLAY_ID, i);
  102. vmw_write(dev_priv, SVGA_REG_DISPLAY_IS_PRIMARY, !i);
  103. vmw_write(dev_priv, SVGA_REG_DISPLAY_POSITION_X, crtc->x);
  104. vmw_write(dev_priv, SVGA_REG_DISPLAY_POSITION_Y, crtc->y);
  105. vmw_write(dev_priv, SVGA_REG_DISPLAY_WIDTH, crtc->mode.hdisplay);
  106. vmw_write(dev_priv, SVGA_REG_DISPLAY_HEIGHT, crtc->mode.vdisplay);
  107. vmw_write(dev_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
  108. i++;
  109. }
  110. BUG_ON(i != lds->num_active);
  111. lds->last_num_active = lds->num_active;
  112. return 0;
  113. }
  114. static int vmw_ldu_del_active(struct vmw_private *vmw_priv,
  115. struct vmw_legacy_display_unit *ldu)
  116. {
  117. struct vmw_legacy_display *ld = vmw_priv->ldu_priv;
  118. if (list_empty(&ldu->active))
  119. return 0;
  120. /* Must init otherwise list_empty(&ldu->active) will not work. */
  121. list_del_init(&ldu->active);
  122. if (--(ld->num_active) == 0) {
  123. BUG_ON(!ld->fb);
  124. if (ld->fb->unpin)
  125. ld->fb->unpin(ld->fb);
  126. ld->fb = NULL;
  127. }
  128. return 0;
  129. }
  130. static int vmw_ldu_add_active(struct vmw_private *vmw_priv,
  131. struct vmw_legacy_display_unit *ldu,
  132. struct vmw_framebuffer *vfb)
  133. {
  134. struct vmw_legacy_display *ld = vmw_priv->ldu_priv;
  135. struct vmw_legacy_display_unit *entry;
  136. struct list_head *at;
  137. BUG_ON(!ld->num_active && ld->fb);
  138. if (vfb != ld->fb) {
  139. if (ld->fb && ld->fb->unpin)
  140. ld->fb->unpin(ld->fb);
  141. vmw_svga_enable(vmw_priv);
  142. if (vfb->pin)
  143. vfb->pin(vfb);
  144. ld->fb = vfb;
  145. }
  146. if (!list_empty(&ldu->active))
  147. return 0;
  148. at = &ld->active;
  149. list_for_each_entry(entry, &ld->active, active) {
  150. if (entry->base.unit > ldu->base.unit)
  151. break;
  152. at = &entry->active;
  153. }
  154. list_add(&ldu->active, at);
  155. ld->num_active++;
  156. return 0;
  157. }
  158. /**
  159. * vmw_ldu_crtc_mode_set_nofb - Enable svga
  160. *
  161. * @crtc: CRTC associated with the new screen
  162. *
  163. * For LDU, just enable the svga
  164. */
  165. static void vmw_ldu_crtc_mode_set_nofb(struct drm_crtc *crtc)
  166. {
  167. }
  168. /**
  169. * vmw_ldu_crtc_atomic_enable - Noop
  170. *
  171. * @crtc: CRTC associated with the new screen
  172. *
  173. * This is called after a mode set has been completed. Here's
  174. * usually a good place to call vmw_ldu_add_active/vmw_ldu_del_active
  175. * but since for LDU the display plane is closely tied to the
  176. * CRTC, it makes more sense to do those at plane update time.
  177. */
  178. static void vmw_ldu_crtc_atomic_enable(struct drm_crtc *crtc,
  179. struct drm_crtc_state *old_state)
  180. {
  181. }
  182. /**
  183. * vmw_ldu_crtc_atomic_disable - Turns off CRTC
  184. *
  185. * @crtc: CRTC to be turned off
  186. */
  187. static void vmw_ldu_crtc_atomic_disable(struct drm_crtc *crtc,
  188. struct drm_crtc_state *old_state)
  189. {
  190. }
  191. static const struct drm_crtc_funcs vmw_legacy_crtc_funcs = {
  192. .gamma_set = vmw_du_crtc_gamma_set,
  193. .destroy = vmw_ldu_crtc_destroy,
  194. .reset = vmw_du_crtc_reset,
  195. .atomic_duplicate_state = vmw_du_crtc_duplicate_state,
  196. .atomic_destroy_state = vmw_du_crtc_destroy_state,
  197. .set_config = drm_atomic_helper_set_config,
  198. .get_vblank_counter = vmw_get_vblank_counter,
  199. .enable_vblank = vmw_enable_vblank,
  200. .disable_vblank = vmw_disable_vblank,
  201. };
  202. /*
  203. * Legacy Display Unit encoder functions
  204. */
  205. static void vmw_ldu_encoder_destroy(struct drm_encoder *encoder)
  206. {
  207. vmw_ldu_destroy(vmw_encoder_to_ldu(encoder));
  208. }
  209. static const struct drm_encoder_funcs vmw_legacy_encoder_funcs = {
  210. .destroy = vmw_ldu_encoder_destroy,
  211. };
  212. /*
  213. * Legacy Display Unit connector functions
  214. */
  215. static void vmw_ldu_connector_destroy(struct drm_connector *connector)
  216. {
  217. vmw_ldu_destroy(vmw_connector_to_ldu(connector));
  218. }
  219. static const struct drm_connector_funcs vmw_legacy_connector_funcs = {
  220. .dpms = vmw_du_connector_dpms,
  221. .detect = vmw_du_connector_detect,
  222. .fill_modes = vmw_du_connector_fill_modes,
  223. .destroy = vmw_ldu_connector_destroy,
  224. .reset = vmw_du_connector_reset,
  225. .atomic_duplicate_state = vmw_du_connector_duplicate_state,
  226. .atomic_destroy_state = vmw_du_connector_destroy_state,
  227. };
  228. static const struct
  229. drm_connector_helper_funcs vmw_ldu_connector_helper_funcs = {
  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. 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. ret = drm_universal_plane_init(dev, &ldu->base.cursor,
  333. 0, &vmw_ldu_cursor_funcs,
  334. vmw_cursor_plane_formats,
  335. ARRAY_SIZE(vmw_cursor_plane_formats),
  336. NULL, DRM_PLANE_TYPE_CURSOR, NULL);
  337. if (ret) {
  338. DRM_ERROR("Failed to initialize cursor plane");
  339. drm_plane_cleanup(&ldu->base.primary);
  340. goto err_free;
  341. }
  342. drm_plane_helper_add(cursor, &vmw_ldu_cursor_plane_helper_funcs);
  343. ret = drm_connector_init(dev, connector, &vmw_legacy_connector_funcs,
  344. DRM_MODE_CONNECTOR_VIRTUAL);
  345. if (ret) {
  346. DRM_ERROR("Failed to initialize connector\n");
  347. goto err_free;
  348. }
  349. drm_connector_helper_add(connector, &vmw_ldu_connector_helper_funcs);
  350. connector->status = vmw_du_connector_detect(connector, true);
  351. ret = drm_encoder_init(dev, encoder, &vmw_legacy_encoder_funcs,
  352. DRM_MODE_ENCODER_VIRTUAL, NULL);
  353. if (ret) {
  354. DRM_ERROR("Failed to initialize encoder\n");
  355. goto err_free_connector;
  356. }
  357. (void) drm_connector_attach_encoder(connector, encoder);
  358. encoder->possible_crtcs = (1 << unit);
  359. encoder->possible_clones = 0;
  360. ret = drm_connector_register(connector);
  361. if (ret) {
  362. DRM_ERROR("Failed to register connector\n");
  363. goto err_free_encoder;
  364. }
  365. ret = drm_crtc_init_with_planes(dev, crtc, &ldu->base.primary,
  366. &ldu->base.cursor,
  367. &vmw_legacy_crtc_funcs, NULL);
  368. if (ret) {
  369. DRM_ERROR("Failed to initialize CRTC\n");
  370. goto err_free_unregister;
  371. }
  372. drm_crtc_helper_add(crtc, &vmw_ldu_crtc_helper_funcs);
  373. drm_mode_crtc_set_gamma_size(crtc, 256);
  374. drm_object_attach_property(&connector->base,
  375. dev_priv->hotplug_mode_update_property, 1);
  376. drm_object_attach_property(&connector->base,
  377. dev->mode_config.suggested_x_property, 0);
  378. drm_object_attach_property(&connector->base,
  379. dev->mode_config.suggested_y_property, 0);
  380. if (dev_priv->implicit_placement_property)
  381. drm_object_attach_property
  382. (&connector->base,
  383. dev_priv->implicit_placement_property,
  384. 1);
  385. return 0;
  386. err_free_unregister:
  387. drm_connector_unregister(connector);
  388. err_free_encoder:
  389. drm_encoder_cleanup(encoder);
  390. err_free_connector:
  391. drm_connector_cleanup(connector);
  392. err_free:
  393. kfree(ldu);
  394. return ret;
  395. }
  396. int vmw_kms_ldu_init_display(struct vmw_private *dev_priv)
  397. {
  398. struct drm_device *dev = dev_priv->dev;
  399. int i, ret;
  400. if (dev_priv->ldu_priv) {
  401. DRM_INFO("ldu system already on\n");
  402. return -EINVAL;
  403. }
  404. dev_priv->ldu_priv = kmalloc(sizeof(*dev_priv->ldu_priv), GFP_KERNEL);
  405. if (!dev_priv->ldu_priv)
  406. return -ENOMEM;
  407. INIT_LIST_HEAD(&dev_priv->ldu_priv->active);
  408. dev_priv->ldu_priv->num_active = 0;
  409. dev_priv->ldu_priv->last_num_active = 0;
  410. dev_priv->ldu_priv->fb = NULL;
  411. /* for old hardware without multimon only enable one display */
  412. if (dev_priv->capabilities & SVGA_CAP_MULTIMON)
  413. ret = drm_vblank_init(dev, VMWGFX_NUM_DISPLAY_UNITS);
  414. else
  415. ret = drm_vblank_init(dev, 1);
  416. if (ret != 0)
  417. goto err_free;
  418. vmw_kms_create_implicit_placement_property(dev_priv);
  419. if (dev_priv->capabilities & SVGA_CAP_MULTIMON)
  420. for (i = 0; i < VMWGFX_NUM_DISPLAY_UNITS; ++i)
  421. vmw_ldu_init(dev_priv, i);
  422. else
  423. vmw_ldu_init(dev_priv, 0);
  424. dev_priv->active_display_unit = vmw_du_legacy;
  425. drm_mode_config_reset(dev);
  426. DRM_INFO("Legacy Display Unit initialized\n");
  427. return 0;
  428. err_free:
  429. kfree(dev_priv->ldu_priv);
  430. dev_priv->ldu_priv = NULL;
  431. return ret;
  432. }
  433. int vmw_kms_ldu_close_display(struct vmw_private *dev_priv)
  434. {
  435. if (!dev_priv->ldu_priv)
  436. return -ENOSYS;
  437. BUG_ON(!list_empty(&dev_priv->ldu_priv->active));
  438. kfree(dev_priv->ldu_priv);
  439. return 0;
  440. }
  441. int vmw_kms_ldu_do_bo_dirty(struct vmw_private *dev_priv,
  442. struct vmw_framebuffer *framebuffer,
  443. unsigned int flags, unsigned int color,
  444. struct drm_clip_rect *clips,
  445. unsigned int num_clips, int increment)
  446. {
  447. size_t fifo_size;
  448. int i;
  449. struct {
  450. uint32_t header;
  451. SVGAFifoCmdUpdate body;
  452. } *cmd;
  453. fifo_size = sizeof(*cmd) * num_clips;
  454. cmd = VMW_FIFO_RESERVE(dev_priv, fifo_size);
  455. if (unlikely(cmd == NULL))
  456. return -ENOMEM;
  457. memset(cmd, 0, fifo_size);
  458. for (i = 0; i < num_clips; i++, clips += increment) {
  459. cmd[i].header = SVGA_CMD_UPDATE;
  460. cmd[i].body.x = clips->x1;
  461. cmd[i].body.y = clips->y1;
  462. cmd[i].body.width = clips->x2 - clips->x1;
  463. cmd[i].body.height = clips->y2 - clips->y1;
  464. }
  465. vmw_fifo_commit(dev_priv, fifo_size);
  466. return 0;
  467. }