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