blob: e74b8e31d042c0cd499d3dad5c44191e7a466e74 [file] [log] [blame]
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +02001/**************************************************************************
2 *
3 * Copyright © 2011 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_sou(x) \
32 container_of(x, struct vmw_screen_object_unit, base.crtc)
33#define vmw_encoder_to_sou(x) \
34 container_of(x, struct vmw_screen_object_unit, base.encoder)
35#define vmw_connector_to_sou(x) \
36 container_of(x, struct vmw_screen_object_unit, base.connector)
37
38struct vmw_screen_object_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 screen objects.
49 */
50struct vmw_screen_object_unit {
51 struct vmw_display_unit base;
52
53 unsigned long buffer_size; /**< Size of allocated buffer */
54 struct vmw_dma_buffer *buffer; /**< Backing store buffer */
55
56 bool defined;
57
58 struct list_head active;
59};
60
61static void vmw_sou_destroy(struct vmw_screen_object_unit *sou)
62{
63 list_del_init(&sou->active);
64 vmw_display_unit_cleanup(&sou->base);
65 kfree(sou);
66}
67
68
69/*
70 * Screen Object Display Unit CRTC functions
71 */
72
73static void vmw_sou_crtc_destroy(struct drm_crtc *crtc)
74{
75 vmw_sou_destroy(vmw_crtc_to_sou(crtc));
76}
77
78static int vmw_sou_del_active(struct vmw_private *vmw_priv,
79 struct vmw_screen_object_unit *sou)
80{
81 struct vmw_screen_object_display *ld = vmw_priv->sou_priv;
82 if (list_empty(&sou->active))
83 return 0;
84
85 /* Must init otherwise list_empty(&sou->active) will not work. */
86 list_del_init(&sou->active);
87 if (--(ld->num_active) == 0) {
88 BUG_ON(!ld->fb);
89 if (ld->fb->unpin)
90 ld->fb->unpin(ld->fb);
91 ld->fb = NULL;
92 }
93
94 return 0;
95}
96
97static int vmw_sou_add_active(struct vmw_private *vmw_priv,
98 struct vmw_screen_object_unit *sou,
99 struct vmw_framebuffer *vfb)
100{
101 struct vmw_screen_object_display *ld = vmw_priv->sou_priv;
102 struct vmw_screen_object_unit *entry;
103 struct list_head *at;
104
105 BUG_ON(!ld->num_active && ld->fb);
106 if (vfb != ld->fb) {
107 if (ld->fb && ld->fb->unpin)
108 ld->fb->unpin(ld->fb);
109 if (vfb->pin)
110 vfb->pin(vfb);
111 ld->fb = vfb;
112 }
113
114 if (!list_empty(&sou->active))
115 return 0;
116
117 at = &ld->active;
118 list_for_each_entry(entry, &ld->active, active) {
119 if (entry->base.unit > sou->base.unit)
120 break;
121
122 at = &entry->active;
123 }
124
125 list_add(&sou->active, at);
126
127 ld->num_active++;
128
129 return 0;
130}
131
132/**
133 * Send the fifo command to create a screen.
134 */
135static int vmw_sou_fifo_create(struct vmw_private *dev_priv,
136 struct vmw_screen_object_unit *sou,
137 uint32_t x, uint32_t y,
138 struct drm_display_mode *mode)
139{
140 size_t fifo_size;
141
142 struct {
143 struct {
144 uint32_t cmdType;
145 } header;
146 SVGAScreenObject obj;
147 } *cmd;
148
149 BUG_ON(!sou->buffer);
150
151 fifo_size = sizeof(*cmd);
152 cmd = vmw_fifo_reserve(dev_priv, fifo_size);
153 /* The hardware has hung, nothing we can do about it here. */
154 if (unlikely(cmd == NULL)) {
155 DRM_ERROR("Fifo reserve failed.\n");
156 return -ENOMEM;
157 }
158
159 memset(cmd, 0, fifo_size);
160 cmd->header.cmdType = SVGA_CMD_DEFINE_SCREEN;
161 cmd->obj.structSize = sizeof(SVGAScreenObject);
162 cmd->obj.id = sou->base.unit;
163 cmd->obj.flags = SVGA_SCREEN_HAS_ROOT |
164 (sou->base.unit == 0 ? SVGA_SCREEN_IS_PRIMARY : 0);
165 cmd->obj.size.width = mode->hdisplay;
166 cmd->obj.size.height = mode->vdisplay;
167 cmd->obj.root.x = x;
168 cmd->obj.root.y = y;
169
170 /* Ok to assume that buffer is pinned in vram */
171 vmw_dmabuf_get_guest_ptr(sou->buffer, &cmd->obj.backingStore.ptr);
172 cmd->obj.backingStore.pitch = mode->hdisplay * 4;
173
174 vmw_fifo_commit(dev_priv, fifo_size);
175
176 sou->defined = true;
177
178 return 0;
179}
180
181/**
182 * Send the fifo command to destroy a screen.
183 */
184static int vmw_sou_fifo_destroy(struct vmw_private *dev_priv,
185 struct vmw_screen_object_unit *sou)
186{
187 size_t fifo_size;
188 int ret;
189
190 struct {
191 struct {
192 uint32_t cmdType;
193 } header;
194 SVGAFifoCmdDestroyScreen body;
195 } *cmd;
196
197 /* no need to do anything */
198 if (unlikely(!sou->defined))
199 return 0;
200
201 fifo_size = sizeof(*cmd);
202 cmd = vmw_fifo_reserve(dev_priv, fifo_size);
203 /* the hardware has hung, nothing we can do about it here */
204 if (unlikely(cmd == NULL)) {
205 DRM_ERROR("Fifo reserve failed.\n");
206 return -ENOMEM;
207 }
208
209 memset(cmd, 0, fifo_size);
210 cmd->header.cmdType = SVGA_CMD_DESTROY_SCREEN;
211 cmd->body.screenId = sou->base.unit;
212
213 vmw_fifo_commit(dev_priv, fifo_size);
214
215 /* Force sync */
216 ret = vmw_fallback_wait(dev_priv, false, true, 0, false, 3*HZ);
217 if (unlikely(ret != 0))
218 DRM_ERROR("Failed to sync with HW");
219 else
220 sou->defined = false;
221
222 return ret;
223}
224
225/**
226 * Free the backing store.
227 */
228static void vmw_sou_backing_free(struct vmw_private *dev_priv,
229 struct vmw_screen_object_unit *sou)
230{
231 struct ttm_buffer_object *bo;
232
233 if (unlikely(sou->buffer == NULL))
234 return;
235
236 bo = &sou->buffer->base;
237 ttm_bo_unref(&bo);
238 sou->buffer = NULL;
239 sou->buffer_size = 0;
240}
241
242/**
243 * Allocate the backing store for the buffer.
244 */
245static int vmw_sou_backing_alloc(struct vmw_private *dev_priv,
246 struct vmw_screen_object_unit *sou,
247 unsigned long size)
248{
249 int ret;
250
251 if (sou->buffer_size == size)
252 return 0;
253
254 if (sou->buffer)
255 vmw_sou_backing_free(dev_priv, sou);
256
257 sou->buffer = kzalloc(sizeof(*sou->buffer), GFP_KERNEL);
258 if (unlikely(sou->buffer == NULL))
259 return -ENOMEM;
260
261 /* After we have alloced the backing store might not be able to
262 * resume the overlays, this is preferred to failing to alloc.
263 */
264 vmw_overlay_pause_all(dev_priv);
265 ret = vmw_dmabuf_init(dev_priv, sou->buffer, size,
266 &vmw_vram_ne_placement,
267 false, &vmw_dmabuf_bo_free);
268 vmw_overlay_resume_all(dev_priv);
269
270 if (unlikely(ret != 0))
271 sou->buffer = NULL; /* vmw_dmabuf_init frees on error */
272 else
273 sou->buffer_size = size;
274
275 return ret;
276}
277
278static int vmw_sou_crtc_set_config(struct drm_mode_set *set)
279{
280 struct vmw_private *dev_priv;
281 struct vmw_screen_object_unit *sou;
282 struct drm_connector *connector;
283 struct drm_display_mode *mode;
284 struct drm_encoder *encoder;
285 struct vmw_framebuffer *vfb;
286 struct drm_framebuffer *fb;
287 struct drm_crtc *crtc;
288 int ret = 0;
289
290 if (!set)
291 return -EINVAL;
292
293 if (!set->crtc)
294 return -EINVAL;
295
296 /* get the sou */
297 crtc = set->crtc;
298 sou = vmw_crtc_to_sou(crtc);
299 vfb = set->fb ? vmw_framebuffer_to_vfb(set->fb) : NULL;
300 dev_priv = vmw_priv(crtc->dev);
301
302 if (set->num_connectors > 1) {
303 DRM_ERROR("to many connectors\n");
304 return -EINVAL;
305 }
306
307 if (set->num_connectors == 1 &&
308 set->connectors[0] != &sou->base.connector) {
309 DRM_ERROR("connector doesn't match %p %p\n",
310 set->connectors[0], &sou->base.connector);
311 return -EINVAL;
312 }
313
314 /* sou only supports one fb active at the time */
315 if (dev_priv->sou_priv->fb && vfb &&
316 !(dev_priv->sou_priv->num_active == 1 &&
317 !list_empty(&sou->active)) &&
318 dev_priv->sou_priv->fb != vfb) {
319 DRM_ERROR("Multiple framebuffers not supported\n");
320 return -EINVAL;
321 }
322
323 /* since they always map one to one these are safe */
324 connector = &sou->base.connector;
325 encoder = &sou->base.encoder;
326
327 /* should we turn the crtc off */
328 if (set->num_connectors == 0 || !set->mode || !set->fb) {
329 ret = vmw_sou_fifo_destroy(dev_priv, sou);
330 /* the hardware has hung don't do anything more */
331 if (unlikely(ret != 0))
332 return ret;
333
334 connector->encoder = NULL;
335 encoder->crtc = NULL;
336 crtc->fb = NULL;
337 crtc->x = 0;
338 crtc->y = 0;
339
340 vmw_sou_del_active(dev_priv, sou);
341
342 vmw_sou_backing_free(dev_priv, sou);
343
344 return 0;
345 }
346
347
348 /* we now know we want to set a mode */
349 mode = set->mode;
350 fb = set->fb;
351
352 if (set->x + mode->hdisplay > fb->width ||
353 set->y + mode->vdisplay > fb->height) {
354 DRM_ERROR("set outside of framebuffer\n");
355 return -EINVAL;
356 }
357
358 vmw_fb_off(dev_priv);
359
360 if (mode->hdisplay != crtc->mode.hdisplay ||
361 mode->vdisplay != crtc->mode.vdisplay) {
362 /* no need to check if depth is different, because backing
363 * store depth is forced to 4 by the device.
364 */
365
366 ret = vmw_sou_fifo_destroy(dev_priv, sou);
367 /* the hardware has hung don't do anything more */
368 if (unlikely(ret != 0))
369 return ret;
370
371 vmw_sou_backing_free(dev_priv, sou);
372 }
373
374 if (!sou->buffer) {
375 /* forced to depth 4 by the device */
376 size_t size = mode->hdisplay * mode->vdisplay * 4;
377 ret = vmw_sou_backing_alloc(dev_priv, sou, size);
378 if (unlikely(ret != 0))
379 return ret;
380 }
381
382 ret = vmw_sou_fifo_create(dev_priv, sou, set->x, set->y, mode);
383 if (unlikely(ret != 0)) {
384 /*
385 * We are in a bit of a situation here, the hardware has
386 * hung and we may or may not have a buffer hanging of
387 * the screen object, best thing to do is not do anything
388 * if we where defined, if not just turn the crtc of.
389 * Not what userspace wants but it needs to htfu.
390 */
391 if (sou->defined)
392 return ret;
393
394 connector->encoder = NULL;
395 encoder->crtc = NULL;
396 crtc->fb = NULL;
397 crtc->x = 0;
398 crtc->y = 0;
399
400 return ret;
401 }
402
403 vmw_sou_add_active(dev_priv, sou, vfb);
404
405 connector->encoder = encoder;
406 encoder->crtc = crtc;
407 crtc->mode = *mode;
408 crtc->fb = fb;
409 crtc->x = set->x;
410 crtc->y = set->y;
411
412 return 0;
413}
414
415static struct drm_crtc_funcs vmw_screen_object_crtc_funcs = {
416 .save = vmw_du_crtc_save,
417 .restore = vmw_du_crtc_restore,
418 .cursor_set = vmw_du_crtc_cursor_set,
419 .cursor_move = vmw_du_crtc_cursor_move,
420 .gamma_set = vmw_du_crtc_gamma_set,
421 .destroy = vmw_sou_crtc_destroy,
422 .set_config = vmw_sou_crtc_set_config,
423};
424
425/*
426 * Screen Object Display Unit encoder functions
427 */
428
429static void vmw_sou_encoder_destroy(struct drm_encoder *encoder)
430{
431 vmw_sou_destroy(vmw_encoder_to_sou(encoder));
432}
433
434static struct drm_encoder_funcs vmw_screen_object_encoder_funcs = {
435 .destroy = vmw_sou_encoder_destroy,
436};
437
438/*
439 * Screen Object Display Unit connector functions
440 */
441
442static void vmw_sou_connector_destroy(struct drm_connector *connector)
443{
444 vmw_sou_destroy(vmw_connector_to_sou(connector));
445}
446
447static struct drm_connector_funcs vmw_legacy_connector_funcs = {
448 .dpms = vmw_du_connector_dpms,
449 .save = vmw_du_connector_save,
450 .restore = vmw_du_connector_restore,
451 .detect = vmw_du_connector_detect,
452 .fill_modes = vmw_du_connector_fill_modes,
453 .set_property = vmw_du_connector_set_property,
454 .destroy = vmw_sou_connector_destroy,
455};
456
457static int vmw_sou_init(struct vmw_private *dev_priv, unsigned unit)
458{
459 struct vmw_screen_object_unit *sou;
460 struct drm_device *dev = dev_priv->dev;
461 struct drm_connector *connector;
462 struct drm_encoder *encoder;
463 struct drm_crtc *crtc;
464
465 sou = kzalloc(sizeof(*sou), GFP_KERNEL);
466 if (!sou)
467 return -ENOMEM;
468
469 sou->base.unit = unit;
470 crtc = &sou->base.crtc;
471 encoder = &sou->base.encoder;
472 connector = &sou->base.connector;
473
474 INIT_LIST_HEAD(&sou->active);
475
476 sou->base.pref_active = (unit == 0);
477 sou->base.pref_width = 800;
478 sou->base.pref_height = 600;
479 sou->base.pref_mode = NULL;
480
481 drm_connector_init(dev, connector, &vmw_legacy_connector_funcs,
482 DRM_MODE_CONNECTOR_LVDS);
483 connector->status = vmw_du_connector_detect(connector, true);
484
485 drm_encoder_init(dev, encoder, &vmw_screen_object_encoder_funcs,
486 DRM_MODE_ENCODER_LVDS);
487 drm_mode_connector_attach_encoder(connector, encoder);
488 encoder->possible_crtcs = (1 << unit);
489 encoder->possible_clones = 0;
490
491 drm_crtc_init(dev, crtc, &vmw_screen_object_crtc_funcs);
492
493 drm_mode_crtc_set_gamma_size(crtc, 256);
494
495 drm_connector_attach_property(connector,
496 dev->mode_config.dirty_info_property,
497 1);
498
499 return 0;
500}
501
502int vmw_kms_init_screen_object_display(struct vmw_private *dev_priv)
503{
504 struct drm_device *dev = dev_priv->dev;
505 int i;
506 int ret;
507
508 if (dev_priv->sou_priv) {
509 DRM_INFO("sou system already on\n");
510 return -EINVAL;
511 }
512
513 if (!(dev_priv->fifo.capabilities & SVGA_FIFO_CAP_SCREEN_OBJECT_2)) {
514 DRM_INFO("Not using screen objects,"
515 " missing cap SCREEN_OBJECT_2\n");
516 return -ENOSYS;
517 }
518
519 ret = -ENOMEM;
520 dev_priv->sou_priv = kmalloc(sizeof(*dev_priv->sou_priv), GFP_KERNEL);
521 if (unlikely(!dev_priv->sou_priv))
522 goto err_no_mem;
523
524 INIT_LIST_HEAD(&dev_priv->sou_priv->active);
525 dev_priv->sou_priv->num_active = 0;
526 dev_priv->sou_priv->last_num_active = 0;
527 dev_priv->sou_priv->fb = NULL;
528
529 ret = drm_vblank_init(dev, VMWGFX_NUM_DISPLAY_UNITS);
530 if (unlikely(ret != 0))
531 goto err_free;
532
533 ret = drm_mode_create_dirty_info_property(dev_priv->dev);
534 if (unlikely(ret != 0))
535 goto err_vblank_cleanup;
536
537 for (i = 0; i < VMWGFX_NUM_DISPLAY_UNITS; ++i)
538 vmw_sou_init(dev_priv, i);
539
540 DRM_INFO("Screen objects system initialized\n");
541
542 return 0;
543
544err_vblank_cleanup:
545 drm_vblank_cleanup(dev);
546err_free:
547 kfree(dev_priv->sou_priv);
548err_no_mem:
549 return ret;
550}
551
552int vmw_kms_close_screen_object_display(struct vmw_private *dev_priv)
553{
554 struct drm_device *dev = dev_priv->dev;
555
556 drm_vblank_cleanup(dev);
557 if (!dev_priv->sou_priv)
558 return -ENOSYS;
559
560 if (!list_empty(&dev_priv->sou_priv->active))
561 DRM_ERROR("Still have active outputs when unloading driver");
562
563 kfree(dev_priv->sou_priv);
564
565 return 0;
566}