blob: 4a4e5ccd40d620870d78a106daedc940357666c5 [file] [log] [blame]
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001/**************************************************************************
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
28#include "vmwgfx_kms.h"
29
Thomas Hellstrom7a1c2f62010-10-01 10:21:49 +020030#define VMWGFX_LDU_NUM_DU 8
31
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +000032#define vmw_crtc_to_ldu(x) \
33 container_of(x, struct vmw_legacy_display_unit, base.crtc)
34#define vmw_encoder_to_ldu(x) \
35 container_of(x, struct vmw_legacy_display_unit, base.encoder)
36#define vmw_connector_to_ldu(x) \
37 container_of(x, struct vmw_legacy_display_unit, base.connector)
38
39struct vmw_legacy_display {
40 struct list_head active;
41
42 unsigned num_active;
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +020043 unsigned last_num_active;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +000044
45 struct vmw_framebuffer *fb;
46};
47
48/**
49 * Display unit using the legacy register interface.
50 */
51struct vmw_legacy_display_unit {
52 struct vmw_display_unit base;
53
54 struct list_head active;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +000055};
56
57static void vmw_ldu_destroy(struct vmw_legacy_display_unit *ldu)
58{
59 list_del_init(&ldu->active);
60 vmw_display_unit_cleanup(&ldu->base);
61 kfree(ldu);
62}
63
64
65/*
66 * Legacy Display Unit CRTC functions
67 */
68
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +000069static void vmw_ldu_crtc_destroy(struct drm_crtc *crtc)
70{
71 vmw_ldu_destroy(vmw_crtc_to_ldu(crtc));
72}
73
74static int vmw_ldu_commit_list(struct vmw_private *dev_priv)
75{
76 struct vmw_legacy_display *lds = dev_priv->ldu_priv;
77 struct vmw_legacy_display_unit *entry;
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +020078 struct drm_framebuffer *fb = NULL;
79 struct drm_crtc *crtc = NULL;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +000080 int i = 0;
81
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +020082 /* If there is no display topology the host just assumes
83 * that the guest will set the same layout as the host.
84 */
85 if (!(dev_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY)) {
86 int w = 0, h = 0;
87 list_for_each_entry(entry, &lds->active, active) {
88 crtc = &entry->base.crtc;
89 w = max(w, crtc->x + crtc->mode.hdisplay);
90 h = max(h, crtc->y + crtc->mode.vdisplay);
91 i++;
92 }
93
94 if (crtc == NULL)
95 return 0;
96 fb = entry->base.crtc.fb;
97
Michel Dänzer0bef23f2011-08-31 07:42:50 +000098 return vmw_kms_write_svga(dev_priv, w, h, fb->pitch,
99 fb->bits_per_pixel, fb->depth);
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +0200100 }
101
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +0200102 if (!list_empty(&lds->active)) {
103 entry = list_entry(lds->active.next, typeof(*entry), active);
104 fb = entry->base.crtc.fb;
105
106 vmw_kms_write_svga(dev_priv, fb->width, fb->height, fb->pitch,
107 fb->bits_per_pixel, fb->depth);
108 }
109
Jakob Bornecrantz259600d52010-05-28 11:22:03 +0200110 /* Make sure we always show something. */
111 vmw_write(dev_priv, SVGA_REG_NUM_GUEST_DISPLAYS,
112 lds->num_active ? lds->num_active : 1);
113
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000114 i = 0;
115 list_for_each_entry(entry, &lds->active, active) {
116 crtc = &entry->base.crtc;
117
118 vmw_write(dev_priv, SVGA_REG_DISPLAY_ID, i);
119 vmw_write(dev_priv, SVGA_REG_DISPLAY_IS_PRIMARY, !i);
120 vmw_write(dev_priv, SVGA_REG_DISPLAY_POSITION_X, crtc->x);
121 vmw_write(dev_priv, SVGA_REG_DISPLAY_POSITION_Y, crtc->y);
122 vmw_write(dev_priv, SVGA_REG_DISPLAY_WIDTH, crtc->mode.hdisplay);
123 vmw_write(dev_priv, SVGA_REG_DISPLAY_HEIGHT, crtc->mode.vdisplay);
124 vmw_write(dev_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
125
126 i++;
127 }
128
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +0200129 BUG_ON(i != lds->num_active);
130
131 lds->last_num_active = lds->num_active;
132
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000133 return 0;
134}
135
136static int vmw_ldu_del_active(struct vmw_private *vmw_priv,
137 struct vmw_legacy_display_unit *ldu)
138{
139 struct vmw_legacy_display *ld = vmw_priv->ldu_priv;
140 if (list_empty(&ldu->active))
141 return 0;
142
Jakob Bornecrantz6a591a92010-05-28 11:21:55 +0200143 /* Must init otherwise list_empty(&ldu->active) will not work. */
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000144 list_del_init(&ldu->active);
145 if (--(ld->num_active) == 0) {
146 BUG_ON(!ld->fb);
147 if (ld->fb->unpin)
148 ld->fb->unpin(ld->fb);
149 ld->fb = NULL;
150 }
151
152 return 0;
153}
154
155static int vmw_ldu_add_active(struct vmw_private *vmw_priv,
156 struct vmw_legacy_display_unit *ldu,
157 struct vmw_framebuffer *vfb)
158{
159 struct vmw_legacy_display *ld = vmw_priv->ldu_priv;
160 struct vmw_legacy_display_unit *entry;
161 struct list_head *at;
162
Jakob Bornecrantz04e9e942010-05-28 11:21:56 +0200163 BUG_ON(!ld->num_active && ld->fb);
164 if (vfb != ld->fb) {
165 if (ld->fb && ld->fb->unpin)
166 ld->fb->unpin(ld->fb);
167 if (vfb->pin)
168 vfb->pin(vfb);
169 ld->fb = vfb;
170 }
171
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000172 if (!list_empty(&ldu->active))
173 return 0;
174
175 at = &ld->active;
176 list_for_each_entry(entry, &ld->active, active) {
Jakob Bornecrantzbbfad332010-05-28 11:22:01 +0200177 if (entry->base.unit > ldu->base.unit)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000178 break;
179
180 at = &entry->active;
181 }
182
183 list_add(&ldu->active, at);
Jakob Bornecrantz04e9e942010-05-28 11:21:56 +0200184
185 ld->num_active++;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000186
187 return 0;
188}
189
190static int vmw_ldu_crtc_set_config(struct drm_mode_set *set)
191{
192 struct vmw_private *dev_priv;
193 struct vmw_legacy_display_unit *ldu;
194 struct drm_connector *connector;
195 struct drm_display_mode *mode;
196 struct drm_encoder *encoder;
197 struct vmw_framebuffer *vfb;
198 struct drm_framebuffer *fb;
199 struct drm_crtc *crtc;
200
201 if (!set)
202 return -EINVAL;
203
204 if (!set->crtc)
205 return -EINVAL;
206
207 /* get the ldu */
208 crtc = set->crtc;
209 ldu = vmw_crtc_to_ldu(crtc);
210 vfb = set->fb ? vmw_framebuffer_to_vfb(set->fb) : NULL;
211 dev_priv = vmw_priv(crtc->dev);
212
213 if (set->num_connectors > 1) {
214 DRM_ERROR("to many connectors\n");
215 return -EINVAL;
216 }
217
218 if (set->num_connectors == 1 &&
219 set->connectors[0] != &ldu->base.connector) {
220 DRM_ERROR("connector doesn't match %p %p\n",
221 set->connectors[0], &ldu->base.connector);
222 return -EINVAL;
223 }
224
225 /* ldu only supports one fb active at the time */
226 if (dev_priv->ldu_priv->fb && vfb &&
Jakob Bornecrantz6a591a92010-05-28 11:21:55 +0200227 !(dev_priv->ldu_priv->num_active == 1 &&
228 !list_empty(&ldu->active)) &&
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000229 dev_priv->ldu_priv->fb != vfb) {
230 DRM_ERROR("Multiple framebuffers not supported\n");
231 return -EINVAL;
232 }
233
234 /* since they always map one to one these are safe */
235 connector = &ldu->base.connector;
236 encoder = &ldu->base.encoder;
237
238 /* should we turn the crtc off? */
239 if (set->num_connectors == 0 || !set->mode || !set->fb) {
240
241 connector->encoder = NULL;
242 encoder->crtc = NULL;
243 crtc->fb = NULL;
244
245 vmw_ldu_del_active(dev_priv, ldu);
246
Michel Dänzer0bef23f2011-08-31 07:42:50 +0000247 return vmw_ldu_commit_list(dev_priv);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000248 }
249
250
251 /* we now know we want to set a mode */
252 mode = set->mode;
253 fb = set->fb;
254
255 if (set->x + mode->hdisplay > fb->width ||
256 set->y + mode->vdisplay > fb->height) {
257 DRM_ERROR("set outside of framebuffer\n");
258 return -EINVAL;
259 }
260
261 vmw_fb_off(dev_priv);
262
263 crtc->fb = fb;
264 encoder->crtc = crtc;
265 connector->encoder = encoder;
266 crtc->x = set->x;
267 crtc->y = set->y;
268 crtc->mode = *mode;
269
270 vmw_ldu_add_active(dev_priv, ldu, vfb);
271
Michel Dänzer0bef23f2011-08-31 07:42:50 +0000272 return vmw_ldu_commit_list(dev_priv);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000273}
274
275static struct drm_crtc_funcs vmw_legacy_crtc_funcs = {
Jakob Bornecrantz626ab772011-10-04 20:13:20 +0200276 .save = vmw_du_crtc_save,
277 .restore = vmw_du_crtc_restore,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000278 .cursor_set = vmw_du_crtc_cursor_set,
279 .cursor_move = vmw_du_crtc_cursor_move,
Jakob Bornecrantz626ab772011-10-04 20:13:20 +0200280 .gamma_set = vmw_du_crtc_gamma_set,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000281 .destroy = vmw_ldu_crtc_destroy,
282 .set_config = vmw_ldu_crtc_set_config,
283};
284
Jakob Bornecrantz626ab772011-10-04 20:13:20 +0200285
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000286/*
287 * Legacy Display Unit encoder functions
288 */
289
290static void vmw_ldu_encoder_destroy(struct drm_encoder *encoder)
291{
292 vmw_ldu_destroy(vmw_encoder_to_ldu(encoder));
293}
294
295static struct drm_encoder_funcs vmw_legacy_encoder_funcs = {
296 .destroy = vmw_ldu_encoder_destroy,
297};
298
299/*
300 * Legacy Display Unit connector functions
301 */
302
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000303static void vmw_ldu_connector_destroy(struct drm_connector *connector)
304{
305 vmw_ldu_destroy(vmw_connector_to_ldu(connector));
306}
307
308static struct drm_connector_funcs vmw_legacy_connector_funcs = {
Jakob Bornecrantz626ab772011-10-04 20:13:20 +0200309 .dpms = vmw_du_connector_dpms,
310 .save = vmw_du_connector_save,
311 .restore = vmw_du_connector_restore,
312 .detect = vmw_du_connector_detect,
313 .fill_modes = vmw_du_connector_fill_modes,
314 .set_property = vmw_du_connector_set_property,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000315 .destroy = vmw_ldu_connector_destroy,
316};
317
318static int vmw_ldu_init(struct vmw_private *dev_priv, unsigned unit)
319{
320 struct vmw_legacy_display_unit *ldu;
321 struct drm_device *dev = dev_priv->dev;
322 struct drm_connector *connector;
323 struct drm_encoder *encoder;
324 struct drm_crtc *crtc;
325
326 ldu = kzalloc(sizeof(*ldu), GFP_KERNEL);
327 if (!ldu)
328 return -ENOMEM;
329
Jakob Bornecrantzbbfad332010-05-28 11:22:01 +0200330 ldu->base.unit = unit;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000331 crtc = &ldu->base.crtc;
332 encoder = &ldu->base.encoder;
333 connector = &ldu->base.connector;
334
Jakob Bornecrantz1ae1ddd2010-05-28 11:21:58 +0200335 INIT_LIST_HEAD(&ldu->active);
336
Jakob Bornecrantz626ab772011-10-04 20:13:20 +0200337 ldu->base.pref_active = (unit == 0);
338 ldu->base.pref_width = 800;
339 ldu->base.pref_height = 600;
340 ldu->base.pref_mode = NULL;
Jakob Bornecrantzd8bd19d2010-06-01 11:54:20 +0200341
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000342 drm_connector_init(dev, connector, &vmw_legacy_connector_funcs,
343 DRM_MODE_CONNECTOR_LVDS);
Jakob Bornecrantz626ab772011-10-04 20:13:20 +0200344 connector->status = vmw_du_connector_detect(connector, true);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000345
346 drm_encoder_init(dev, encoder, &vmw_legacy_encoder_funcs,
347 DRM_MODE_ENCODER_LVDS);
348 drm_mode_connector_attach_encoder(connector, encoder);
349 encoder->possible_crtcs = (1 << unit);
350 encoder->possible_clones = 0;
351
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000352 drm_crtc_init(dev, crtc, &vmw_legacy_crtc_funcs);
353
Michel Dänzerf01b7ba2011-08-31 07:42:47 +0000354 drm_mode_crtc_set_gamma_size(crtc, 256);
355
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000356 drm_connector_attach_property(connector,
357 dev->mode_config.dirty_info_property,
358 1);
359
360 return 0;
361}
362
363int vmw_kms_init_legacy_display_system(struct vmw_private *dev_priv)
364{
Thomas Hellstrom7a1c2f62010-10-01 10:21:49 +0200365 struct drm_device *dev = dev_priv->dev;
366 int i;
367 int ret;
368
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000369 if (dev_priv->ldu_priv) {
370 DRM_INFO("ldu system already on\n");
371 return -EINVAL;
372 }
373
Joe Perches85b54e02010-10-31 22:33:53 +0000374 dev_priv->ldu_priv = kmalloc(sizeof(*dev_priv->ldu_priv), GFP_KERNEL);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000375
376 if (!dev_priv->ldu_priv)
377 return -ENOMEM;
378
379 INIT_LIST_HEAD(&dev_priv->ldu_priv->active);
380 dev_priv->ldu_priv->num_active = 0;
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +0200381 dev_priv->ldu_priv->last_num_active = 0;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000382 dev_priv->ldu_priv->fb = NULL;
383
384 drm_mode_create_dirty_info_property(dev_priv->dev);
385
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +0200386 if (dev_priv->capabilities & SVGA_CAP_MULTIMON) {
Thomas Hellstrom7a1c2f62010-10-01 10:21:49 +0200387 for (i = 0; i < VMWGFX_LDU_NUM_DU; ++i)
388 vmw_ldu_init(dev_priv, i);
389 ret = drm_vblank_init(dev, VMWGFX_LDU_NUM_DU);
390 } else {
391 /* for old hardware without multimon only enable one display */
392 vmw_ldu_init(dev_priv, 0);
393 ret = drm_vblank_init(dev, 1);
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +0200394 }
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000395
Thomas Hellstrom7a1c2f62010-10-01 10:21:49 +0200396 return ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000397}
398
399int vmw_kms_close_legacy_display_system(struct vmw_private *dev_priv)
400{
Thomas Hellstrom7a1c2f62010-10-01 10:21:49 +0200401 struct drm_device *dev = dev_priv->dev;
402
403 drm_vblank_cleanup(dev);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000404 if (!dev_priv->ldu_priv)
405 return -ENOSYS;
406
407 BUG_ON(!list_empty(&dev_priv->ldu_priv->active));
408
409 kfree(dev_priv->ldu_priv);
410
411 return 0;
412}