MIPS: Yosemite, Emma: Fix off-by-two in arcs_cmdline buffer size check
[linux-2.6/linux-mips.git] / drivers / gpu / drm / vmwgfx / vmwgfx_ldu.c
blob92f56bc594eb6bcd1f691fc7218316334f9c29ce
1 /**************************************************************************
3 * Copyright © 2009 VMware, Inc., Palo Alto, CA., USA
4 * All Rights Reserved.
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:
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.
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.
26 **************************************************************************/
28 #include "vmwgfx_kms.h"
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)
38 struct vmw_legacy_display {
39 struct list_head active;
41 unsigned num_active;
42 unsigned last_num_active;
44 struct vmw_framebuffer *fb;
47 /**
48 * Display unit using the legacy register interface.
50 struct vmw_legacy_display_unit {
51 struct vmw_display_unit base;
53 struct list_head active;
56 static void vmw_ldu_destroy(struct vmw_legacy_display_unit *ldu)
58 list_del_init(&ldu->active);
59 vmw_display_unit_cleanup(&ldu->base);
60 kfree(ldu);
65 * Legacy Display Unit CRTC functions
68 static void vmw_ldu_crtc_destroy(struct drm_crtc *crtc)
70 vmw_ldu_destroy(vmw_crtc_to_ldu(crtc));
73 static int vmw_ldu_commit_list(struct vmw_private *dev_priv)
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;
81 /* If there is no display topology the host just assumes
82 * that the guest will set the same layout as the host.
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++;
93 if (crtc == NULL)
94 return 0;
95 fb = entry->base.crtc.fb;
97 return vmw_kms_write_svga(dev_priv, w, h, fb->pitch,
98 fb->bits_per_pixel, fb->depth);
101 if (!list_empty(&lds->active)) {
102 entry = list_entry(lds->active.next, typeof(*entry), active);
103 fb = entry->base.crtc.fb;
105 vmw_kms_write_svga(dev_priv, fb->width, fb->height, fb->pitch,
106 fb->bits_per_pixel, fb->depth);
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);
113 i = 0;
114 list_for_each_entry(entry, &lds->active, active) {
115 crtc = &entry->base.crtc;
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);
125 i++;
128 BUG_ON(i != lds->num_active);
130 lds->last_num_active = lds->num_active;
132 return 0;
135 static int vmw_ldu_del_active(struct vmw_private *vmw_priv,
136 struct vmw_legacy_display_unit *ldu)
138 struct vmw_legacy_display *ld = vmw_priv->ldu_priv;
139 if (list_empty(&ldu->active))
140 return 0;
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;
151 return 0;
154 static int vmw_ldu_add_active(struct vmw_private *vmw_priv,
155 struct vmw_legacy_display_unit *ldu,
156 struct vmw_framebuffer *vfb)
158 struct vmw_legacy_display *ld = vmw_priv->ldu_priv;
159 struct vmw_legacy_display_unit *entry;
160 struct list_head *at;
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;
171 if (!list_empty(&ldu->active))
172 return 0;
174 at = &ld->active;
175 list_for_each_entry(entry, &ld->active, active) {
176 if (entry->base.unit > ldu->base.unit)
177 break;
179 at = &entry->active;
182 list_add(&ldu->active, at);
184 ld->num_active++;
186 return 0;
189 static int vmw_ldu_crtc_set_config(struct drm_mode_set *set)
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;
200 if (!set)
201 return -EINVAL;
203 if (!set->crtc)
204 return -EINVAL;
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);
212 if (set->num_connectors > 1) {
213 DRM_ERROR("to many connectors\n");
214 return -EINVAL;
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;
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;
233 /* since they always map one to one these are safe */
234 connector = &ldu->base.connector;
235 encoder = &ldu->base.encoder;
237 /* should we turn the crtc off? */
238 if (set->num_connectors == 0 || !set->mode || !set->fb) {
240 connector->encoder = NULL;
241 encoder->crtc = NULL;
242 crtc->fb = NULL;
244 vmw_ldu_del_active(dev_priv, ldu);
246 return vmw_ldu_commit_list(dev_priv);
250 /* we now know we want to set a mode */
251 mode = set->mode;
252 fb = set->fb;
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;
260 vmw_fb_off(dev_priv);
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;
269 vmw_ldu_add_active(dev_priv, ldu, vfb);
271 return vmw_ldu_commit_list(dev_priv);
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,
286 * Legacy Display Unit encoder functions
289 static void vmw_ldu_encoder_destroy(struct drm_encoder *encoder)
291 vmw_ldu_destroy(vmw_encoder_to_ldu(encoder));
294 static struct drm_encoder_funcs vmw_legacy_encoder_funcs = {
295 .destroy = vmw_ldu_encoder_destroy,
299 * Legacy Display Unit connector functions
302 static void vmw_ldu_connector_destroy(struct drm_connector *connector)
304 vmw_ldu_destroy(vmw_connector_to_ldu(connector));
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,
317 static int vmw_ldu_init(struct vmw_private *dev_priv, unsigned unit)
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;
325 ldu = kzalloc(sizeof(*ldu), GFP_KERNEL);
326 if (!ldu)
327 return -ENOMEM;
329 ldu->base.unit = unit;
330 crtc = &ldu->base.crtc;
331 encoder = &ldu->base.encoder;
332 connector = &ldu->base.connector;
334 INIT_LIST_HEAD(&ldu->active);
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;
341 drm_connector_init(dev, connector, &vmw_legacy_connector_funcs,
342 DRM_MODE_CONNECTOR_LVDS);
343 connector->status = vmw_du_connector_detect(connector, true);
345 drm_encoder_init(dev, encoder, &vmw_legacy_encoder_funcs,
346 DRM_MODE_ENCODER_LVDS);
347 drm_mode_connector_attach_encoder(connector, encoder);
348 encoder->possible_crtcs = (1 << unit);
349 encoder->possible_clones = 0;
351 drm_crtc_init(dev, crtc, &vmw_legacy_crtc_funcs);
353 drm_mode_crtc_set_gamma_size(crtc, 256);
355 drm_connector_attach_property(connector,
356 dev->mode_config.dirty_info_property,
359 return 0;
362 int vmw_kms_init_legacy_display_system(struct vmw_private *dev_priv)
364 struct drm_device *dev = dev_priv->dev;
365 int i, ret;
367 if (dev_priv->ldu_priv) {
368 DRM_INFO("ldu system already on\n");
369 return -EINVAL;
372 dev_priv->ldu_priv = kmalloc(sizeof(*dev_priv->ldu_priv), GFP_KERNEL);
373 if (!dev_priv->ldu_priv)
374 return -ENOMEM;
376 INIT_LIST_HEAD(&dev_priv->ldu_priv->active);
377 dev_priv->ldu_priv->num_active = 0;
378 dev_priv->ldu_priv->last_num_active = 0;
379 dev_priv->ldu_priv->fb = NULL;
381 /* for old hardware without multimon only enable one display */
382 if (dev_priv->capabilities & SVGA_CAP_MULTIMON)
383 ret = drm_vblank_init(dev, VMWGFX_NUM_DISPLAY_UNITS);
384 else
385 ret = drm_vblank_init(dev, 1);
386 if (ret != 0)
387 goto err_free;
389 ret = drm_mode_create_dirty_info_property(dev);
390 if (ret != 0)
391 goto err_vblank_cleanup;
393 if (dev_priv->capabilities & SVGA_CAP_MULTIMON)
394 for (i = 0; i < VMWGFX_NUM_DISPLAY_UNITS; ++i)
395 vmw_ldu_init(dev_priv, i);
396 else
397 vmw_ldu_init(dev_priv, 0);
399 return 0;
401 err_vblank_cleanup:
402 drm_vblank_cleanup(dev);
403 err_free:
404 kfree(dev_priv->ldu_priv);
405 dev_priv->ldu_priv = NULL;
406 return ret;
409 int vmw_kms_close_legacy_display_system(struct vmw_private *dev_priv)
411 struct drm_device *dev = dev_priv->dev;
413 if (!dev_priv->ldu_priv)
414 return -ENOSYS;
416 drm_vblank_cleanup(dev);
418 BUG_ON(!list_empty(&dev_priv->ldu_priv->active));
420 kfree(dev_priv->ldu_priv);
422 return 0;