drm: Replace pitch with pitches[] in drm_framebuffer
[linux-2.6.git] / drivers / gpu / drm / vmwgfx / vmwgfx_ldu.c
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
28 #include "vmwgfx_kms.h"
29
30
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
38 struct vmw_legacy_display {
39         struct list_head active;
40
41         unsigned num_active;
42         unsigned last_num_active;
43
44         struct vmw_framebuffer *fb;
45 };
46
47 /**
48  * Display unit using the legacy register interface.
49  */
50 struct vmw_legacy_display_unit {
51         struct vmw_display_unit base;
52
53         struct list_head active;
54 };
55
56 static void vmw_ldu_destroy(struct vmw_legacy_display_unit *ldu)
57 {
58         list_del_init(&ldu->active);
59         vmw_display_unit_cleanup(&ldu->base);
60         kfree(ldu);
61 }
62
63
64 /*
65  * Legacy Display Unit CRTC functions
66  */
67
68 static void vmw_ldu_crtc_destroy(struct drm_crtc *crtc)
69 {
70         vmw_ldu_destroy(vmw_crtc_to_ldu(crtc));
71 }
72
73 static int vmw_ldu_commit_list(struct vmw_private *dev_priv)
74 {
75         struct vmw_legacy_display *lds = dev_priv->ldu_priv;
76         struct vmw_legacy_display_unit *entry;
77         struct drm_framebuffer *fb = NULL;
78         struct drm_crtc *crtc = NULL;
79         int i = 0;
80
81         /* If there is no display topology the host just assumes
82          * that the guest will set the same layout as the host.
83          */
84         if (!(dev_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY)) {
85                 int w = 0, h = 0;
86                 list_for_each_entry(entry, &lds->active, active) {
87                         crtc = &entry->base.crtc;
88                         w = max(w, crtc->x + crtc->mode.hdisplay);
89                         h = max(h, crtc->y + crtc->mode.vdisplay);
90                         i++;
91                 }
92
93                 if (crtc == NULL)
94                         return 0;
95                 fb = entry->base.crtc.fb;
96
97                 return vmw_kms_write_svga(dev_priv, w, h, fb->pitches[0],
98                                           fb->bits_per_pixel, fb->depth);
99         }
100
101         if (!list_empty(&lds->active)) {
102                 entry = list_entry(lds->active.next, typeof(*entry), active);
103                 fb = entry->base.crtc.fb;
104
105                 vmw_kms_write_svga(dev_priv, fb->width, fb->height, fb->pitches[0],
106                                    fb->bits_per_pixel, fb->depth);
107         }
108
109         /* Make sure we always show something. */
110         vmw_write(dev_priv, SVGA_REG_NUM_GUEST_DISPLAYS,
111                   lds->num_active ? lds->num_active : 1);
112
113         i = 0;
114         list_for_each_entry(entry, &lds->active, active) {
115                 crtc = &entry->base.crtc;
116
117                 vmw_write(dev_priv, SVGA_REG_DISPLAY_ID, i);
118                 vmw_write(dev_priv, SVGA_REG_DISPLAY_IS_PRIMARY, !i);
119                 vmw_write(dev_priv, SVGA_REG_DISPLAY_POSITION_X, crtc->x);
120                 vmw_write(dev_priv, SVGA_REG_DISPLAY_POSITION_Y, crtc->y);
121                 vmw_write(dev_priv, SVGA_REG_DISPLAY_WIDTH, crtc->mode.hdisplay);
122                 vmw_write(dev_priv, SVGA_REG_DISPLAY_HEIGHT, crtc->mode.vdisplay);
123                 vmw_write(dev_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
124
125                 i++;
126         }
127
128         BUG_ON(i != lds->num_active);
129
130         lds->last_num_active = lds->num_active;
131
132         return 0;
133 }
134
135 static int vmw_ldu_del_active(struct vmw_private *vmw_priv,
136                               struct vmw_legacy_display_unit *ldu)
137 {
138         struct vmw_legacy_display *ld = vmw_priv->ldu_priv;
139         if (list_empty(&ldu->active))
140                 return 0;
141
142         /* Must init otherwise list_empty(&ldu->active) will not work. */
143         list_del_init(&ldu->active);
144         if (--(ld->num_active) == 0) {
145                 BUG_ON(!ld->fb);
146                 if (ld->fb->unpin)
147                         ld->fb->unpin(ld->fb);
148                 ld->fb = NULL;
149         }
150
151         return 0;
152 }
153
154 static int vmw_ldu_add_active(struct vmw_private *vmw_priv,
155                               struct vmw_legacy_display_unit *ldu,
156                               struct vmw_framebuffer *vfb)
157 {
158         struct vmw_legacy_display *ld = vmw_priv->ldu_priv;
159         struct vmw_legacy_display_unit *entry;
160         struct list_head *at;
161
162         BUG_ON(!ld->num_active && ld->fb);
163         if (vfb != ld->fb) {
164                 if (ld->fb && ld->fb->unpin)
165                         ld->fb->unpin(ld->fb);
166                 if (vfb->pin)
167                         vfb->pin(vfb);
168                 ld->fb = vfb;
169         }
170
171         if (!list_empty(&ldu->active))
172                 return 0;
173
174         at = &ld->active;
175         list_for_each_entry(entry, &ld->active, active) {
176                 if (entry->base.unit > ldu->base.unit)
177                         break;
178
179                 at = &entry->active;
180         }
181
182         list_add(&ldu->active, at);
183
184         ld->num_active++;
185
186         return 0;
187 }
188
189 static int vmw_ldu_crtc_set_config(struct drm_mode_set *set)
190 {
191         struct vmw_private *dev_priv;
192         struct vmw_legacy_display_unit *ldu;
193         struct drm_connector *connector;
194         struct drm_display_mode *mode;
195         struct drm_encoder *encoder;
196         struct vmw_framebuffer *vfb;
197         struct drm_framebuffer *fb;
198         struct drm_crtc *crtc;
199
200         if (!set)
201                 return -EINVAL;
202
203         if (!set->crtc)
204                 return -EINVAL;
205
206         /* get the ldu */
207         crtc = set->crtc;
208         ldu = vmw_crtc_to_ldu(crtc);
209         vfb = set->fb ? vmw_framebuffer_to_vfb(set->fb) : NULL;
210         dev_priv = vmw_priv(crtc->dev);
211
212         if (set->num_connectors > 1) {
213                 DRM_ERROR("to many connectors\n");
214                 return -EINVAL;
215         }
216
217         if (set->num_connectors == 1 &&
218             set->connectors[0] != &ldu->base.connector) {
219                 DRM_ERROR("connector doesn't match %p %p\n",
220                         set->connectors[0], &ldu->base.connector);
221                 return -EINVAL;
222         }
223
224         /* ldu only supports one fb active at the time */
225         if (dev_priv->ldu_priv->fb && vfb &&
226             !(dev_priv->ldu_priv->num_active == 1 &&
227               !list_empty(&ldu->active)) &&
228             dev_priv->ldu_priv->fb != vfb) {
229                 DRM_ERROR("Multiple framebuffers not supported\n");
230                 return -EINVAL;
231         }
232
233         /* since they always map one to one these are safe */
234         connector = &ldu->base.connector;
235         encoder = &ldu->base.encoder;
236
237         /* should we turn the crtc off? */
238         if (set->num_connectors == 0 || !set->mode || !set->fb) {
239
240                 connector->encoder = NULL;
241                 encoder->crtc = NULL;
242                 crtc->fb = NULL;
243
244                 vmw_ldu_del_active(dev_priv, ldu);
245
246                 return vmw_ldu_commit_list(dev_priv);
247         }
248
249
250         /* we now know we want to set a mode */
251         mode = set->mode;
252         fb = set->fb;
253
254         if (set->x + mode->hdisplay > fb->width ||
255             set->y + mode->vdisplay > fb->height) {
256                 DRM_ERROR("set outside of framebuffer\n");
257                 return -EINVAL;
258         }
259
260         vmw_fb_off(dev_priv);
261
262         crtc->fb = fb;
263         encoder->crtc = crtc;
264         connector->encoder = encoder;
265         crtc->x = set->x;
266         crtc->y = set->y;
267         crtc->mode = *mode;
268
269         vmw_ldu_add_active(dev_priv, ldu, vfb);
270
271         return vmw_ldu_commit_list(dev_priv);
272 }
273
274 static struct drm_crtc_funcs vmw_legacy_crtc_funcs = {
275         .save = vmw_du_crtc_save,
276         .restore = vmw_du_crtc_restore,
277         .cursor_set = vmw_du_crtc_cursor_set,
278         .cursor_move = vmw_du_crtc_cursor_move,
279         .gamma_set = vmw_du_crtc_gamma_set,
280         .destroy = vmw_ldu_crtc_destroy,
281         .set_config = vmw_ldu_crtc_set_config,
282 };
283
284
285 /*
286  * Legacy Display Unit encoder functions
287  */
288
289 static void vmw_ldu_encoder_destroy(struct drm_encoder *encoder)
290 {
291         vmw_ldu_destroy(vmw_encoder_to_ldu(encoder));
292 }
293
294 static struct drm_encoder_funcs vmw_legacy_encoder_funcs = {
295         .destroy = vmw_ldu_encoder_destroy,
296 };
297
298 /*
299  * Legacy Display Unit connector functions
300  */
301
302 static void vmw_ldu_connector_destroy(struct drm_connector *connector)
303 {
304         vmw_ldu_destroy(vmw_connector_to_ldu(connector));
305 }
306
307 static struct drm_connector_funcs vmw_legacy_connector_funcs = {
308         .dpms = vmw_du_connector_dpms,
309         .save = vmw_du_connector_save,
310         .restore = vmw_du_connector_restore,
311         .detect = vmw_du_connector_detect,
312         .fill_modes = vmw_du_connector_fill_modes,
313         .set_property = vmw_du_connector_set_property,
314         .destroy = vmw_ldu_connector_destroy,
315 };
316
317 static int vmw_ldu_init(struct vmw_private *dev_priv, unsigned unit)
318 {
319         struct vmw_legacy_display_unit *ldu;
320         struct drm_device *dev = dev_priv->dev;
321         struct drm_connector *connector;
322         struct drm_encoder *encoder;
323         struct drm_crtc *crtc;
324
325         ldu = kzalloc(sizeof(*ldu), GFP_KERNEL);
326         if (!ldu)
327                 return -ENOMEM;
328
329         ldu->base.unit = unit;
330         crtc = &ldu->base.crtc;
331         encoder = &ldu->base.encoder;
332         connector = &ldu->base.connector;
333
334         INIT_LIST_HEAD(&ldu->active);
335
336         ldu->base.pref_active = (unit == 0);
337         ldu->base.pref_width = 800;
338         ldu->base.pref_height = 600;
339         ldu->base.pref_mode = NULL;
340         ldu->base.is_implicit = true;
341
342         drm_connector_init(dev, connector, &vmw_legacy_connector_funcs,
343                            DRM_MODE_CONNECTOR_VIRTUAL);
344         connector->status = vmw_du_connector_detect(connector, true);
345
346         drm_encoder_init(dev, encoder, &vmw_legacy_encoder_funcs,
347                          DRM_MODE_ENCODER_VIRTUAL);
348         drm_mode_connector_attach_encoder(connector, encoder);
349         encoder->possible_crtcs = (1 << unit);
350         encoder->possible_clones = 0;
351
352         drm_crtc_init(dev, crtc, &vmw_legacy_crtc_funcs);
353
354         drm_mode_crtc_set_gamma_size(crtc, 256);
355
356         drm_connector_attach_property(connector,
357                                       dev->mode_config.dirty_info_property,
358                                       1);
359
360         return 0;
361 }
362
363 int vmw_kms_init_legacy_display_system(struct vmw_private *dev_priv)
364 {
365         struct drm_device *dev = dev_priv->dev;
366         int i, ret;
367
368         if (dev_priv->ldu_priv) {
369                 DRM_INFO("ldu system already on\n");
370                 return -EINVAL;
371         }
372
373         dev_priv->ldu_priv = kmalloc(sizeof(*dev_priv->ldu_priv), GFP_KERNEL);
374         if (!dev_priv->ldu_priv)
375                 return -ENOMEM;
376
377         INIT_LIST_HEAD(&dev_priv->ldu_priv->active);
378         dev_priv->ldu_priv->num_active = 0;
379         dev_priv->ldu_priv->last_num_active = 0;
380         dev_priv->ldu_priv->fb = NULL;
381
382         /* for old hardware without multimon only enable one display */
383         if (dev_priv->capabilities & SVGA_CAP_MULTIMON)
384                 ret = drm_vblank_init(dev, VMWGFX_NUM_DISPLAY_UNITS);
385         else
386                 ret = drm_vblank_init(dev, 1);
387         if (ret != 0)
388                 goto err_free;
389
390         ret = drm_mode_create_dirty_info_property(dev);
391         if (ret != 0)
392                 goto err_vblank_cleanup;
393
394         if (dev_priv->capabilities & SVGA_CAP_MULTIMON)
395                 for (i = 0; i < VMWGFX_NUM_DISPLAY_UNITS; ++i)
396                         vmw_ldu_init(dev_priv, i);
397         else
398                 vmw_ldu_init(dev_priv, 0);
399
400         return 0;
401
402 err_vblank_cleanup:
403         drm_vblank_cleanup(dev);
404 err_free:
405         kfree(dev_priv->ldu_priv);
406         dev_priv->ldu_priv = NULL;
407         return ret;
408 }
409
410 int vmw_kms_close_legacy_display_system(struct vmw_private *dev_priv)
411 {
412         struct drm_device *dev = dev_priv->dev;
413
414         if (!dev_priv->ldu_priv)
415                 return -ENOSYS;
416
417         drm_vblank_cleanup(dev);
418
419         BUG_ON(!list_empty(&dev_priv->ldu_priv->active));
420
421         kfree(dev_priv->ldu_priv);
422
423         return 0;
424 }