vmwgfx_ldu.c 14 KB

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