vmwgfx_ldu.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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 vmw_crtc_to_ldu(x) \
  29. container_of(x, struct vmw_legacy_display_unit, base.crtc)
  30. #define vmw_encoder_to_ldu(x) \
  31. container_of(x, struct vmw_legacy_display_unit, base.encoder)
  32. #define vmw_connector_to_ldu(x) \
  33. container_of(x, struct vmw_legacy_display_unit, base.connector)
  34. struct vmw_legacy_display {
  35. struct list_head active;
  36. unsigned num_active;
  37. struct vmw_framebuffer *fb;
  38. };
  39. /**
  40. * Display unit using the legacy register interface.
  41. */
  42. struct vmw_legacy_display_unit {
  43. struct vmw_display_unit base;
  44. struct list_head active;
  45. unsigned unit;
  46. };
  47. static void vmw_ldu_destroy(struct vmw_legacy_display_unit *ldu)
  48. {
  49. list_del_init(&ldu->active);
  50. vmw_display_unit_cleanup(&ldu->base);
  51. kfree(ldu);
  52. }
  53. /*
  54. * Legacy Display Unit CRTC functions
  55. */
  56. static void vmw_ldu_crtc_save(struct drm_crtc *crtc)
  57. {
  58. }
  59. static void vmw_ldu_crtc_restore(struct drm_crtc *crtc)
  60. {
  61. }
  62. static void vmw_ldu_crtc_gamma_set(struct drm_crtc *crtc,
  63. u16 *r, u16 *g, u16 *b,
  64. uint32_t size)
  65. {
  66. }
  67. static void vmw_ldu_crtc_destroy(struct drm_crtc *crtc)
  68. {
  69. vmw_ldu_destroy(vmw_crtc_to_ldu(crtc));
  70. }
  71. static int vmw_ldu_commit_list(struct vmw_private *dev_priv)
  72. {
  73. struct vmw_legacy_display *lds = dev_priv->ldu_priv;
  74. struct vmw_legacy_display_unit *entry;
  75. struct drm_crtc *crtc;
  76. int i = 0;
  77. /* to stop the screen from changing size on resize */
  78. vmw_write(dev_priv, SVGA_REG_NUM_GUEST_DISPLAYS, 0);
  79. for (i = 0; i < lds->num_active; i++) {
  80. vmw_write(dev_priv, SVGA_REG_DISPLAY_ID, i);
  81. vmw_write(dev_priv, SVGA_REG_DISPLAY_IS_PRIMARY, !i);
  82. vmw_write(dev_priv, SVGA_REG_DISPLAY_POSITION_X, 0);
  83. vmw_write(dev_priv, SVGA_REG_DISPLAY_POSITION_Y, 0);
  84. vmw_write(dev_priv, SVGA_REG_DISPLAY_WIDTH, 0);
  85. vmw_write(dev_priv, SVGA_REG_DISPLAY_HEIGHT, 0);
  86. vmw_write(dev_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
  87. }
  88. /* Now set the mode */
  89. vmw_write(dev_priv, SVGA_REG_NUM_GUEST_DISPLAYS, lds->num_active);
  90. i = 0;
  91. list_for_each_entry(entry, &lds->active, active) {
  92. crtc = &entry->base.crtc;
  93. vmw_write(dev_priv, SVGA_REG_DISPLAY_ID, i);
  94. vmw_write(dev_priv, SVGA_REG_DISPLAY_IS_PRIMARY, !i);
  95. vmw_write(dev_priv, SVGA_REG_DISPLAY_POSITION_X, crtc->x);
  96. vmw_write(dev_priv, SVGA_REG_DISPLAY_POSITION_Y, crtc->y);
  97. vmw_write(dev_priv, SVGA_REG_DISPLAY_WIDTH, crtc->mode.hdisplay);
  98. vmw_write(dev_priv, SVGA_REG_DISPLAY_HEIGHT, crtc->mode.vdisplay);
  99. vmw_write(dev_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
  100. i++;
  101. }
  102. return 0;
  103. }
  104. static int vmw_ldu_del_active(struct vmw_private *vmw_priv,
  105. struct vmw_legacy_display_unit *ldu)
  106. {
  107. struct vmw_legacy_display *ld = vmw_priv->ldu_priv;
  108. if (list_empty(&ldu->active))
  109. return 0;
  110. /* Must init otherwise list_empty(&ldu->active) will not work. */
  111. list_del_init(&ldu->active);
  112. if (--(ld->num_active) == 0) {
  113. BUG_ON(!ld->fb);
  114. if (ld->fb->unpin)
  115. ld->fb->unpin(ld->fb);
  116. ld->fb = NULL;
  117. }
  118. return 0;
  119. }
  120. static int vmw_ldu_add_active(struct vmw_private *vmw_priv,
  121. struct vmw_legacy_display_unit *ldu,
  122. struct vmw_framebuffer *vfb)
  123. {
  124. struct vmw_legacy_display *ld = vmw_priv->ldu_priv;
  125. struct vmw_legacy_display_unit *entry;
  126. struct list_head *at;
  127. BUG_ON(!ld->num_active && ld->fb);
  128. if (vfb != ld->fb) {
  129. if (ld->fb && ld->fb->unpin)
  130. ld->fb->unpin(ld->fb);
  131. if (vfb->pin)
  132. vfb->pin(vfb);
  133. ld->fb = vfb;
  134. }
  135. if (!list_empty(&ldu->active))
  136. return 0;
  137. at = &ld->active;
  138. list_for_each_entry(entry, &ld->active, active) {
  139. if (entry->unit > ldu->unit)
  140. break;
  141. at = &entry->active;
  142. }
  143. list_add(&ldu->active, at);
  144. ld->num_active++;
  145. return 0;
  146. }
  147. static int vmw_ldu_crtc_set_config(struct drm_mode_set *set)
  148. {
  149. struct vmw_private *dev_priv;
  150. struct vmw_legacy_display_unit *ldu;
  151. struct drm_connector *connector;
  152. struct drm_display_mode *mode;
  153. struct drm_encoder *encoder;
  154. struct vmw_framebuffer *vfb;
  155. struct drm_framebuffer *fb;
  156. struct drm_crtc *crtc;
  157. if (!set)
  158. return -EINVAL;
  159. if (!set->crtc)
  160. return -EINVAL;
  161. /* get the ldu */
  162. crtc = set->crtc;
  163. ldu = vmw_crtc_to_ldu(crtc);
  164. vfb = set->fb ? vmw_framebuffer_to_vfb(set->fb) : NULL;
  165. dev_priv = vmw_priv(crtc->dev);
  166. if (set->num_connectors > 1) {
  167. DRM_ERROR("to many connectors\n");
  168. return -EINVAL;
  169. }
  170. if (set->num_connectors == 1 &&
  171. set->connectors[0] != &ldu->base.connector) {
  172. DRM_ERROR("connector doesn't match %p %p\n",
  173. set->connectors[0], &ldu->base.connector);
  174. return -EINVAL;
  175. }
  176. /* ldu only supports one fb active at the time */
  177. if (dev_priv->ldu_priv->fb && vfb &&
  178. !(dev_priv->ldu_priv->num_active == 1 &&
  179. !list_empty(&ldu->active)) &&
  180. dev_priv->ldu_priv->fb != vfb) {
  181. DRM_ERROR("Multiple framebuffers not supported\n");
  182. return -EINVAL;
  183. }
  184. /* since they always map one to one these are safe */
  185. connector = &ldu->base.connector;
  186. encoder = &ldu->base.encoder;
  187. /* should we turn the crtc off? */
  188. if (set->num_connectors == 0 || !set->mode || !set->fb) {
  189. connector->encoder = NULL;
  190. encoder->crtc = NULL;
  191. crtc->fb = NULL;
  192. vmw_ldu_del_active(dev_priv, ldu);
  193. vmw_ldu_commit_list(dev_priv);
  194. return 0;
  195. }
  196. /* we now know we want to set a mode */
  197. mode = set->mode;
  198. fb = set->fb;
  199. if (set->x + mode->hdisplay > fb->width ||
  200. set->y + mode->vdisplay > fb->height) {
  201. DRM_ERROR("set outside of framebuffer\n");
  202. return -EINVAL;
  203. }
  204. vmw_fb_off(dev_priv);
  205. crtc->fb = fb;
  206. encoder->crtc = crtc;
  207. connector->encoder = encoder;
  208. crtc->x = set->x;
  209. crtc->y = set->y;
  210. crtc->mode = *mode;
  211. vmw_ldu_add_active(dev_priv, ldu, vfb);
  212. vmw_ldu_commit_list(dev_priv);
  213. return 0;
  214. }
  215. static struct drm_crtc_funcs vmw_legacy_crtc_funcs = {
  216. .save = vmw_ldu_crtc_save,
  217. .restore = vmw_ldu_crtc_restore,
  218. .cursor_set = vmw_du_crtc_cursor_set,
  219. .cursor_move = vmw_du_crtc_cursor_move,
  220. .gamma_set = vmw_ldu_crtc_gamma_set,
  221. .destroy = vmw_ldu_crtc_destroy,
  222. .set_config = vmw_ldu_crtc_set_config,
  223. };
  224. /*
  225. * Legacy Display Unit encoder functions
  226. */
  227. static void vmw_ldu_encoder_destroy(struct drm_encoder *encoder)
  228. {
  229. vmw_ldu_destroy(vmw_encoder_to_ldu(encoder));
  230. }
  231. static struct drm_encoder_funcs vmw_legacy_encoder_funcs = {
  232. .destroy = vmw_ldu_encoder_destroy,
  233. };
  234. /*
  235. * Legacy Display Unit connector functions
  236. */
  237. static void vmw_ldu_connector_dpms(struct drm_connector *connector, int mode)
  238. {
  239. }
  240. static void vmw_ldu_connector_save(struct drm_connector *connector)
  241. {
  242. }
  243. static void vmw_ldu_connector_restore(struct drm_connector *connector)
  244. {
  245. }
  246. static enum drm_connector_status
  247. vmw_ldu_connector_detect(struct drm_connector *connector)
  248. {
  249. /* XXX vmwctrl should control connection status */
  250. if (vmw_connector_to_ldu(connector)->base.unit == 0)
  251. return connector_status_connected;
  252. return connector_status_disconnected;
  253. }
  254. static struct drm_display_mode vmw_ldu_connector_builtin[] = {
  255. /* 640x480@60Hz */
  256. { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 25175, 640, 656,
  257. 752, 800, 0, 480, 489, 492, 525, 0,
  258. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
  259. /* 800x600@60Hz */
  260. { DRM_MODE("800x600",
  261. DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
  262. 40000, 800, 840, 968, 1056, 0, 600, 601, 605, 628,
  263. 0, DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  264. /* 1024x768@60Hz */
  265. { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 65000, 1024, 1048,
  266. 1184, 1344, 0, 768, 771, 777, 806, 0,
  267. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
  268. /* 1152x864@75Hz */
  269. { DRM_MODE("1152x864", DRM_MODE_TYPE_DRIVER, 108000, 1152, 1216,
  270. 1344, 1600, 0, 864, 865, 868, 900, 0,
  271. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  272. /* 1280x768@60Hz */
  273. { DRM_MODE("1280x768", DRM_MODE_TYPE_DRIVER, 79500, 1280, 1344,
  274. 1472, 1664, 0, 768, 771, 778, 798, 0,
  275. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  276. /* 1280x800@60Hz */
  277. { DRM_MODE("1280x800", DRM_MODE_TYPE_DRIVER, 83500, 1280, 1352,
  278. 1480, 1680, 0, 800, 803, 809, 831, 0,
  279. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
  280. /* 1280x960@60Hz */
  281. { DRM_MODE("1280x960", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1376,
  282. 1488, 1800, 0, 960, 961, 964, 1000, 0,
  283. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  284. /* 1280x1024@60Hz */
  285. { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1328,
  286. 1440, 1688, 0, 1024, 1025, 1028, 1066, 0,
  287. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  288. /* 1360x768@60Hz */
  289. { DRM_MODE("1360x768", DRM_MODE_TYPE_DRIVER, 85500, 1360, 1424,
  290. 1536, 1792, 0, 768, 771, 777, 795, 0,
  291. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  292. /* 1440x1050@60Hz */
  293. { DRM_MODE("1400x1050", DRM_MODE_TYPE_DRIVER, 121750, 1400, 1488,
  294. 1632, 1864, 0, 1050, 1053, 1057, 1089, 0,
  295. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  296. /* 1440x900@60Hz */
  297. { DRM_MODE("1440x900", DRM_MODE_TYPE_DRIVER, 106500, 1440, 1520,
  298. 1672, 1904, 0, 900, 903, 909, 934, 0,
  299. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  300. /* 1600x1200@60Hz */
  301. { DRM_MODE("1600x1200", DRM_MODE_TYPE_DRIVER, 162000, 1600, 1664,
  302. 1856, 2160, 0, 1200, 1201, 1204, 1250, 0,
  303. DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  304. /* 1680x1050@60Hz */
  305. { DRM_MODE("1680x1050", DRM_MODE_TYPE_DRIVER, 146250, 1680, 1784,
  306. 1960, 2240, 0, 1050, 1053, 1059, 1089, 0,
  307. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  308. /* 1792x1344@60Hz */
  309. { DRM_MODE("1792x1344", DRM_MODE_TYPE_DRIVER, 204750, 1792, 1920,
  310. 2120, 2448, 0, 1344, 1345, 1348, 1394, 0,
  311. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  312. /* 1853x1392@60Hz */
  313. { DRM_MODE("1856x1392", DRM_MODE_TYPE_DRIVER, 218250, 1856, 1952,
  314. 2176, 2528, 0, 1392, 1393, 1396, 1439, 0,
  315. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  316. /* 1920x1200@60Hz */
  317. { DRM_MODE("1920x1200", DRM_MODE_TYPE_DRIVER, 193250, 1920, 2056,
  318. 2256, 2592, 0, 1200, 1203, 1209, 1245, 0,
  319. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  320. /* 1920x1440@60Hz */
  321. { DRM_MODE("1920x1440", DRM_MODE_TYPE_DRIVER, 234000, 1920, 2048,
  322. 2256, 2600, 0, 1440, 1441, 1444, 1500, 0,
  323. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  324. /* 2560x1600@60Hz */
  325. { DRM_MODE("2560x1600", DRM_MODE_TYPE_DRIVER, 348500, 2560, 2752,
  326. 3032, 3504, 0, 1600, 1603, 1609, 1658, 0,
  327. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
  328. /* Terminate */
  329. { DRM_MODE("", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) },
  330. };
  331. static int vmw_ldu_connector_fill_modes(struct drm_connector *connector,
  332. uint32_t max_width, uint32_t max_height)
  333. {
  334. struct drm_device *dev = connector->dev;
  335. struct drm_display_mode *mode = NULL;
  336. int i;
  337. for (i = 0; vmw_ldu_connector_builtin[i].type != 0; i++) {
  338. if (vmw_ldu_connector_builtin[i].hdisplay > max_width ||
  339. vmw_ldu_connector_builtin[i].vdisplay > max_height)
  340. continue;
  341. mode = drm_mode_duplicate(dev, &vmw_ldu_connector_builtin[i]);
  342. if (!mode)
  343. return 0;
  344. mode->vrefresh = drm_mode_vrefresh(mode);
  345. drm_mode_probed_add(connector, mode);
  346. }
  347. drm_mode_connector_list_update(connector);
  348. return 1;
  349. }
  350. static int vmw_ldu_connector_set_property(struct drm_connector *connector,
  351. struct drm_property *property,
  352. uint64_t val)
  353. {
  354. return 0;
  355. }
  356. static void vmw_ldu_connector_destroy(struct drm_connector *connector)
  357. {
  358. vmw_ldu_destroy(vmw_connector_to_ldu(connector));
  359. }
  360. static struct drm_connector_funcs vmw_legacy_connector_funcs = {
  361. .dpms = vmw_ldu_connector_dpms,
  362. .save = vmw_ldu_connector_save,
  363. .restore = vmw_ldu_connector_restore,
  364. .detect = vmw_ldu_connector_detect,
  365. .fill_modes = vmw_ldu_connector_fill_modes,
  366. .set_property = vmw_ldu_connector_set_property,
  367. .destroy = vmw_ldu_connector_destroy,
  368. };
  369. static int vmw_ldu_init(struct vmw_private *dev_priv, unsigned unit)
  370. {
  371. struct vmw_legacy_display_unit *ldu;
  372. struct drm_device *dev = dev_priv->dev;
  373. struct drm_connector *connector;
  374. struct drm_encoder *encoder;
  375. struct drm_crtc *crtc;
  376. ldu = kzalloc(sizeof(*ldu), GFP_KERNEL);
  377. if (!ldu)
  378. return -ENOMEM;
  379. ldu->unit = unit;
  380. crtc = &ldu->base.crtc;
  381. encoder = &ldu->base.encoder;
  382. connector = &ldu->base.connector;
  383. drm_connector_init(dev, connector, &vmw_legacy_connector_funcs,
  384. DRM_MODE_CONNECTOR_LVDS);
  385. /* Initial status */
  386. if (unit == 0)
  387. connector->status = connector_status_connected;
  388. else
  389. connector->status = connector_status_disconnected;
  390. drm_encoder_init(dev, encoder, &vmw_legacy_encoder_funcs,
  391. DRM_MODE_ENCODER_LVDS);
  392. drm_mode_connector_attach_encoder(connector, encoder);
  393. encoder->possible_crtcs = (1 << unit);
  394. encoder->possible_clones = 0;
  395. INIT_LIST_HEAD(&ldu->active);
  396. drm_crtc_init(dev, crtc, &vmw_legacy_crtc_funcs);
  397. drm_connector_attach_property(connector,
  398. dev->mode_config.dirty_info_property,
  399. 1);
  400. return 0;
  401. }
  402. int vmw_kms_init_legacy_display_system(struct vmw_private *dev_priv)
  403. {
  404. if (dev_priv->ldu_priv) {
  405. DRM_INFO("ldu system already on\n");
  406. return -EINVAL;
  407. }
  408. dev_priv->ldu_priv = kmalloc(GFP_KERNEL, sizeof(*dev_priv->ldu_priv));
  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->fb = NULL;
  414. drm_mode_create_dirty_info_property(dev_priv->dev);
  415. vmw_ldu_init(dev_priv, 0);
  416. vmw_ldu_init(dev_priv, 1);
  417. vmw_ldu_init(dev_priv, 2);
  418. vmw_ldu_init(dev_priv, 3);
  419. vmw_ldu_init(dev_priv, 4);
  420. vmw_ldu_init(dev_priv, 5);
  421. vmw_ldu_init(dev_priv, 6);
  422. vmw_ldu_init(dev_priv, 7);
  423. return 0;
  424. }
  425. int vmw_kms_close_legacy_display_system(struct vmw_private *dev_priv)
  426. {
  427. if (!dev_priv->ldu_priv)
  428. return -ENOSYS;
  429. BUG_ON(!list_empty(&dev_priv->ldu_priv->active));
  430. kfree(dev_priv->ldu_priv);
  431. return 0;
  432. }