drm/vmwgfx: Introduce a pin count to allow for recursive pinning v2
[linux-2.6-block.git] / drivers / gpu / drm / vmwgfx / vmwgfx_fb.c
1 /**************************************************************************
2  *
3  * Copyright © 2007 David Airlie
4  * Copyright © 2009 VMware, Inc., Palo Alto, CA., USA
5  * All Rights Reserved.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a
8  * copy of this software and associated documentation files (the
9  * "Software"), to deal in the Software without restriction, including
10  * without limitation the rights to use, copy, modify, merge, publish,
11  * distribute, sub license, and/or sell copies of the Software, and to
12  * permit persons to whom the Software is furnished to do so, subject to
13  * the following conditions:
14  *
15  * The above copyright notice and this permission notice (including the
16  * next paragraph) shall be included in all copies or substantial portions
17  * of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
22  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
23  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
25  * USE OR OTHER DEALINGS IN THE SOFTWARE.
26  *
27  **************************************************************************/
28
29 #include <linux/export.h>
30
31 #include <drm/drmP.h>
32 #include "vmwgfx_drv.h"
33
34 #include <drm/ttm/ttm_placement.h>
35
36 #define VMW_DIRTY_DELAY (HZ / 30)
37
38 struct vmw_fb_par {
39         struct vmw_private *vmw_priv;
40
41         void *vmalloc;
42
43         struct vmw_dma_buffer *vmw_bo;
44         struct ttm_bo_kmap_obj map;
45
46         u32 pseudo_palette[17];
47
48         unsigned depth;
49         unsigned bpp;
50
51         unsigned max_width;
52         unsigned max_height;
53
54         void *bo_ptr;
55         unsigned bo_size;
56         bool bo_iowrite;
57
58         struct {
59                 spinlock_t lock;
60                 bool active;
61                 unsigned x1;
62                 unsigned y1;
63                 unsigned x2;
64                 unsigned y2;
65         } dirty;
66 };
67
68 static int vmw_fb_setcolreg(unsigned regno, unsigned red, unsigned green,
69                             unsigned blue, unsigned transp,
70                             struct fb_info *info)
71 {
72         struct vmw_fb_par *par = info->par;
73         u32 *pal = par->pseudo_palette;
74
75         if (regno > 15) {
76                 DRM_ERROR("Bad regno %u.\n", regno);
77                 return 1;
78         }
79
80         switch (par->depth) {
81         case 24:
82         case 32:
83                 pal[regno] = ((red & 0xff00) << 8) |
84                               (green & 0xff00) |
85                              ((blue  & 0xff00) >> 8);
86                 break;
87         default:
88                 DRM_ERROR("Bad depth %u, bpp %u.\n", par->depth, par->bpp);
89                 return 1;
90         }
91
92         return 0;
93 }
94
95 static int vmw_fb_check_var(struct fb_var_screeninfo *var,
96                             struct fb_info *info)
97 {
98         int depth = var->bits_per_pixel;
99         struct vmw_fb_par *par = info->par;
100         struct vmw_private *vmw_priv = par->vmw_priv;
101
102         switch (var->bits_per_pixel) {
103         case 32:
104                 depth = (var->transp.length > 0) ? 32 : 24;
105                 break;
106         default:
107                 DRM_ERROR("Bad bpp %u.\n", var->bits_per_pixel);
108                 return -EINVAL;
109         }
110
111         switch (depth) {
112         case 24:
113                 var->red.offset = 16;
114                 var->green.offset = 8;
115                 var->blue.offset = 0;
116                 var->red.length = 8;
117                 var->green.length = 8;
118                 var->blue.length = 8;
119                 var->transp.length = 0;
120                 var->transp.offset = 0;
121                 break;
122         case 32:
123                 var->red.offset = 16;
124                 var->green.offset = 8;
125                 var->blue.offset = 0;
126                 var->red.length = 8;
127                 var->green.length = 8;
128                 var->blue.length = 8;
129                 var->transp.length = 8;
130                 var->transp.offset = 24;
131                 break;
132         default:
133                 DRM_ERROR("Bad depth %u.\n", depth);
134                 return -EINVAL;
135         }
136
137         if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY) &&
138             (var->xoffset != 0 || var->yoffset != 0)) {
139                 DRM_ERROR("Can not handle panning without display topology\n");
140                 return -EINVAL;
141         }
142
143         if ((var->xoffset + var->xres) > par->max_width ||
144             (var->yoffset + var->yres) > par->max_height) {
145                 DRM_ERROR("Requested geom can not fit in framebuffer\n");
146                 return -EINVAL;
147         }
148
149         if (!vmw_kms_validate_mode_vram(vmw_priv,
150                                         var->xres * var->bits_per_pixel/8,
151                                         var->yoffset + var->yres)) {
152                 DRM_ERROR("Requested geom can not fit in framebuffer\n");
153                 return -EINVAL;
154         }
155
156         return 0;
157 }
158
159 static int vmw_fb_set_par(struct fb_info *info)
160 {
161         struct vmw_fb_par *par = info->par;
162         struct vmw_private *vmw_priv = par->vmw_priv;
163         int ret;
164
165         info->fix.line_length = info->var.xres * info->var.bits_per_pixel/8;
166
167         ret = vmw_kms_write_svga(vmw_priv, info->var.xres, info->var.yres,
168                                  info->fix.line_length,
169                                  par->bpp, par->depth);
170         if (ret)
171                 return ret;
172
173         if (vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY) {
174                 /* TODO check if pitch and offset changes */
175                 vmw_write(vmw_priv, SVGA_REG_NUM_GUEST_DISPLAYS, 1);
176                 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, 0);
177                 vmw_write(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY, true);
178                 vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_X, info->var.xoffset);
179                 vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y, info->var.yoffset);
180                 vmw_write(vmw_priv, SVGA_REG_DISPLAY_WIDTH, info->var.xres);
181                 vmw_write(vmw_priv, SVGA_REG_DISPLAY_HEIGHT, info->var.yres);
182                 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
183         }
184
185         /* This is really helpful since if this fails the user
186          * can probably not see anything on the screen.
187          */
188         WARN_ON(vmw_read(vmw_priv, SVGA_REG_FB_OFFSET) != 0);
189
190         return 0;
191 }
192
193 static int vmw_fb_pan_display(struct fb_var_screeninfo *var,
194                               struct fb_info *info)
195 {
196         return 0;
197 }
198
199 static int vmw_fb_blank(int blank, struct fb_info *info)
200 {
201         return 0;
202 }
203
204 /*
205  * Dirty code
206  */
207
208 static void vmw_fb_dirty_flush(struct vmw_fb_par *par)
209 {
210         struct vmw_private *vmw_priv = par->vmw_priv;
211         struct fb_info *info = vmw_priv->fb_info;
212         int stride = (info->fix.line_length / 4);
213         int *src = (int *)info->screen_base;
214         __le32 __iomem *vram_mem = par->bo_ptr;
215         unsigned long flags;
216         unsigned x, y, w, h;
217         int i, k;
218         struct {
219                 uint32_t header;
220                 SVGAFifoCmdUpdate body;
221         } *cmd;
222
223         if (vmw_priv->suspended)
224                 return;
225
226         spin_lock_irqsave(&par->dirty.lock, flags);
227         if (!par->dirty.active) {
228                 spin_unlock_irqrestore(&par->dirty.lock, flags);
229                 return;
230         }
231         x = par->dirty.x1;
232         y = par->dirty.y1;
233         w = min(par->dirty.x2, info->var.xres) - x;
234         h = min(par->dirty.y2, info->var.yres) - y;
235         par->dirty.x1 = par->dirty.x2 = 0;
236         par->dirty.y1 = par->dirty.y2 = 0;
237         spin_unlock_irqrestore(&par->dirty.lock, flags);
238
239         for (i = y * stride; i < info->fix.smem_len / 4; i += stride) {
240                 for (k = i+x; k < i+x+w && k < info->fix.smem_len / 4; k++)
241                         iowrite32(src[k], vram_mem + k);
242         }
243
244 #if 0
245         DRM_INFO("%s, (%u, %u) (%ux%u)\n", __func__, x, y, w, h);
246 #endif
247
248         cmd = vmw_fifo_reserve(vmw_priv, sizeof(*cmd));
249         if (unlikely(cmd == NULL)) {
250                 DRM_ERROR("Fifo reserve failed.\n");
251                 return;
252         }
253
254         cmd->header = cpu_to_le32(SVGA_CMD_UPDATE);
255         cmd->body.x = cpu_to_le32(x);
256         cmd->body.y = cpu_to_le32(y);
257         cmd->body.width = cpu_to_le32(w);
258         cmd->body.height = cpu_to_le32(h);
259         vmw_fifo_commit(vmw_priv, sizeof(*cmd));
260         vmw_fifo_flush(vmw_priv, false);
261 }
262
263 static void vmw_fb_dirty_mark(struct vmw_fb_par *par,
264                               unsigned x1, unsigned y1,
265                               unsigned width, unsigned height)
266 {
267         struct fb_info *info = par->vmw_priv->fb_info;
268         unsigned long flags;
269         unsigned x2 = x1 + width;
270         unsigned y2 = y1 + height;
271
272         spin_lock_irqsave(&par->dirty.lock, flags);
273         if (par->dirty.x1 == par->dirty.x2) {
274                 par->dirty.x1 = x1;
275                 par->dirty.y1 = y1;
276                 par->dirty.x2 = x2;
277                 par->dirty.y2 = y2;
278                 /* if we are active start the dirty work
279                  * we share the work with the defio system */
280                 if (par->dirty.active)
281                         schedule_delayed_work(&info->deferred_work, VMW_DIRTY_DELAY);
282         } else {
283                 if (x1 < par->dirty.x1)
284                         par->dirty.x1 = x1;
285                 if (y1 < par->dirty.y1)
286                         par->dirty.y1 = y1;
287                 if (x2 > par->dirty.x2)
288                         par->dirty.x2 = x2;
289                 if (y2 > par->dirty.y2)
290                         par->dirty.y2 = y2;
291         }
292         spin_unlock_irqrestore(&par->dirty.lock, flags);
293 }
294
295 static void vmw_deferred_io(struct fb_info *info,
296                             struct list_head *pagelist)
297 {
298         struct vmw_fb_par *par = info->par;
299         unsigned long start, end, min, max;
300         unsigned long flags;
301         struct page *page;
302         int y1, y2;
303
304         min = ULONG_MAX;
305         max = 0;
306         list_for_each_entry(page, pagelist, lru) {
307                 start = page->index << PAGE_SHIFT;
308                 end = start + PAGE_SIZE - 1;
309                 min = min(min, start);
310                 max = max(max, end);
311         }
312
313         if (min < max) {
314                 y1 = min / info->fix.line_length;
315                 y2 = (max / info->fix.line_length) + 1;
316
317                 spin_lock_irqsave(&par->dirty.lock, flags);
318                 par->dirty.x1 = 0;
319                 par->dirty.y1 = y1;
320                 par->dirty.x2 = info->var.xres;
321                 par->dirty.y2 = y2;
322                 spin_unlock_irqrestore(&par->dirty.lock, flags);
323         }
324
325         vmw_fb_dirty_flush(par);
326 };
327
328 struct fb_deferred_io vmw_defio = {
329         .delay          = VMW_DIRTY_DELAY,
330         .deferred_io    = vmw_deferred_io,
331 };
332
333 /*
334  * Draw code
335  */
336
337 static void vmw_fb_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
338 {
339         cfb_fillrect(info, rect);
340         vmw_fb_dirty_mark(info->par, rect->dx, rect->dy,
341                           rect->width, rect->height);
342 }
343
344 static void vmw_fb_copyarea(struct fb_info *info, const struct fb_copyarea *region)
345 {
346         cfb_copyarea(info, region);
347         vmw_fb_dirty_mark(info->par, region->dx, region->dy,
348                           region->width, region->height);
349 }
350
351 static void vmw_fb_imageblit(struct fb_info *info, const struct fb_image *image)
352 {
353         cfb_imageblit(info, image);
354         vmw_fb_dirty_mark(info->par, image->dx, image->dy,
355                           image->width, image->height);
356 }
357
358 /*
359  * Bring up code
360  */
361
362 static struct fb_ops vmw_fb_ops = {
363         .owner = THIS_MODULE,
364         .fb_check_var = vmw_fb_check_var,
365         .fb_set_par = vmw_fb_set_par,
366         .fb_setcolreg = vmw_fb_setcolreg,
367         .fb_fillrect = vmw_fb_fillrect,
368         .fb_copyarea = vmw_fb_copyarea,
369         .fb_imageblit = vmw_fb_imageblit,
370         .fb_pan_display = vmw_fb_pan_display,
371         .fb_blank = vmw_fb_blank,
372 };
373
374 static int vmw_fb_create_bo(struct vmw_private *vmw_priv,
375                             size_t size, struct vmw_dma_buffer **out)
376 {
377         struct vmw_dma_buffer *vmw_bo;
378         struct ttm_place ne_place = vmw_vram_ne_placement.placement[0];
379         struct ttm_placement ne_placement;
380         int ret;
381
382         ne_placement.num_placement = 1;
383         ne_placement.placement = &ne_place;
384         ne_placement.num_busy_placement = 1;
385         ne_placement.busy_placement = &ne_place;
386
387         ne_place.lpfn = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
388
389         (void) ttm_write_lock(&vmw_priv->reservation_sem, false);
390
391         vmw_bo = kmalloc(sizeof(*vmw_bo), GFP_KERNEL);
392         if (!vmw_bo) {
393                 ret = -ENOMEM;
394                 goto err_unlock;
395         }
396
397         ret = vmw_dmabuf_init(vmw_priv, vmw_bo, size,
398                               &ne_placement,
399                               false,
400                               &vmw_dmabuf_bo_free);
401         if (unlikely(ret != 0))
402                 goto err_unlock; /* init frees the buffer on failure */
403
404         *out = vmw_bo;
405
406         ttm_write_unlock(&vmw_priv->reservation_sem);
407
408         return 0;
409
410 err_unlock:
411         ttm_write_unlock(&vmw_priv->reservation_sem);
412         return ret;
413 }
414
415 int vmw_fb_init(struct vmw_private *vmw_priv)
416 {
417         struct device *device = &vmw_priv->dev->pdev->dev;
418         struct vmw_fb_par *par;
419         struct fb_info *info;
420         unsigned initial_width, initial_height;
421         unsigned fb_width, fb_height;
422         unsigned fb_bpp, fb_depth, fb_offset, fb_pitch, fb_size;
423         int ret;
424
425         fb_bpp = 32;
426         fb_depth = 24;
427
428         /* XXX As shouldn't these be as well. */
429         fb_width = min(vmw_priv->fb_max_width, (unsigned)2048);
430         fb_height = min(vmw_priv->fb_max_height, (unsigned)2048);
431
432         initial_width = min(vmw_priv->initial_width, fb_width);
433         initial_height = min(vmw_priv->initial_height, fb_height);
434
435         fb_pitch = fb_width * fb_bpp / 8;
436         fb_size = fb_pitch * fb_height;
437         fb_offset = vmw_read(vmw_priv, SVGA_REG_FB_OFFSET);
438
439         info = framebuffer_alloc(sizeof(*par), device);
440         if (!info)
441                 return -ENOMEM;
442
443         /*
444          * Par
445          */
446         vmw_priv->fb_info = info;
447         par = info->par;
448         par->vmw_priv = vmw_priv;
449         par->depth = fb_depth;
450         par->bpp = fb_bpp;
451         par->vmalloc = NULL;
452         par->max_width = fb_width;
453         par->max_height = fb_height;
454
455         /*
456          * Create buffers and alloc memory
457          */
458         par->vmalloc = vmalloc(fb_size);
459         if (unlikely(par->vmalloc == NULL)) {
460                 ret = -ENOMEM;
461                 goto err_free;
462         }
463
464         ret = vmw_fb_create_bo(vmw_priv, fb_size, &par->vmw_bo);
465         if (unlikely(ret != 0))
466                 goto err_free;
467
468         ret = ttm_bo_kmap(&par->vmw_bo->base,
469                           0,
470                           par->vmw_bo->base.num_pages,
471                           &par->map);
472         if (unlikely(ret != 0))
473                 goto err_unref;
474         par->bo_ptr = ttm_kmap_obj_virtual(&par->map, &par->bo_iowrite);
475         par->bo_size = fb_size;
476
477         /*
478          * Fixed and var
479          */
480         strcpy(info->fix.id, "svgadrmfb");
481         info->fix.type = FB_TYPE_PACKED_PIXELS;
482         info->fix.visual = FB_VISUAL_TRUECOLOR;
483         info->fix.type_aux = 0;
484         info->fix.xpanstep = 1; /* doing it in hw */
485         info->fix.ypanstep = 1; /* doing it in hw */
486         info->fix.ywrapstep = 0;
487         info->fix.accel = FB_ACCEL_NONE;
488         info->fix.line_length = fb_pitch;
489
490         info->fix.smem_start = 0;
491         info->fix.smem_len = fb_size;
492
493         info->pseudo_palette = par->pseudo_palette;
494         info->screen_base = par->vmalloc;
495         info->screen_size = fb_size;
496
497         info->flags = FBINFO_DEFAULT;
498         info->fbops = &vmw_fb_ops;
499
500         /* 24 depth per default */
501         info->var.red.offset = 16;
502         info->var.green.offset = 8;
503         info->var.blue.offset = 0;
504         info->var.red.length = 8;
505         info->var.green.length = 8;
506         info->var.blue.length = 8;
507         info->var.transp.offset = 0;
508         info->var.transp.length = 0;
509
510         info->var.xres_virtual = fb_width;
511         info->var.yres_virtual = fb_height;
512         info->var.bits_per_pixel = par->bpp;
513         info->var.xoffset = 0;
514         info->var.yoffset = 0;
515         info->var.activate = FB_ACTIVATE_NOW;
516         info->var.height = -1;
517         info->var.width = -1;
518
519         info->var.xres = initial_width;
520         info->var.yres = initial_height;
521
522         /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
523
524         info->apertures = alloc_apertures(1);
525         if (!info->apertures) {
526                 ret = -ENOMEM;
527                 goto err_aper;
528         }
529         info->apertures->ranges[0].base = vmw_priv->vram_start;
530         info->apertures->ranges[0].size = vmw_priv->vram_size;
531
532         /*
533          * Dirty & Deferred IO
534          */
535         par->dirty.x1 = par->dirty.x2 = 0;
536         par->dirty.y1 = par->dirty.y2 = 0;
537         par->dirty.active = true;
538         spin_lock_init(&par->dirty.lock);
539         info->fbdefio = &vmw_defio;
540         fb_deferred_io_init(info);
541
542         ret = register_framebuffer(info);
543         if (unlikely(ret != 0))
544                 goto err_defio;
545
546         return 0;
547
548 err_defio:
549         fb_deferred_io_cleanup(info);
550 err_aper:
551         ttm_bo_kunmap(&par->map);
552 err_unref:
553         ttm_bo_unref((struct ttm_buffer_object **)&par->vmw_bo);
554 err_free:
555         vfree(par->vmalloc);
556         framebuffer_release(info);
557         vmw_priv->fb_info = NULL;
558
559         return ret;
560 }
561
562 int vmw_fb_close(struct vmw_private *vmw_priv)
563 {
564         struct fb_info *info;
565         struct vmw_fb_par *par;
566         struct ttm_buffer_object *bo;
567
568         if (!vmw_priv->fb_info)
569                 return 0;
570
571         info = vmw_priv->fb_info;
572         par = info->par;
573         bo = &par->vmw_bo->base;
574         par->vmw_bo = NULL;
575
576         /* ??? order */
577         fb_deferred_io_cleanup(info);
578         unregister_framebuffer(info);
579
580         ttm_bo_kunmap(&par->map);
581         ttm_bo_unref(&bo);
582
583         vfree(par->vmalloc);
584         framebuffer_release(info);
585
586         return 0;
587 }
588
589 int vmw_fb_off(struct vmw_private *vmw_priv)
590 {
591         struct fb_info *info;
592         struct vmw_fb_par *par;
593         unsigned long flags;
594
595         if (!vmw_priv->fb_info)
596                 return -EINVAL;
597
598         info = vmw_priv->fb_info;
599         par = info->par;
600         if (!par->bo_ptr)
601                 return 0;
602
603         vmw_kms_save_vga(vmw_priv);
604         spin_lock_irqsave(&par->dirty.lock, flags);
605         par->dirty.active = false;
606         spin_unlock_irqrestore(&par->dirty.lock, flags);
607
608         flush_delayed_work(&info->deferred_work);
609
610         par->bo_ptr = NULL;
611         ttm_bo_kunmap(&par->map);
612
613         vmw_dmabuf_unpin(vmw_priv, par->vmw_bo, false);
614
615         return 0;
616 }
617
618 int vmw_fb_on(struct vmw_private *vmw_priv)
619 {
620         struct fb_info *info;
621         struct vmw_fb_par *par;
622         unsigned long flags;
623         bool dummy;
624         int ret;
625
626         if (!vmw_priv->fb_info)
627                 return -EINVAL;
628
629         info = vmw_priv->fb_info;
630         par = info->par;
631
632         /* we are already active */
633         if (par->bo_ptr != NULL)
634                 return 0;
635
636         /* Make sure that all overlays are stoped when we take over */
637         vmw_overlay_stop_all(vmw_priv);
638
639         ret = vmw_dmabuf_pin_in_start_of_vram(vmw_priv, par->vmw_bo, false);
640         if (unlikely(ret != 0)) {
641                 DRM_ERROR("could not move buffer to start of VRAM\n");
642                 goto err_no_buffer;
643         }
644
645         ret = ttm_bo_kmap(&par->vmw_bo->base,
646                           0,
647                           par->vmw_bo->base.num_pages,
648                           &par->map);
649         BUG_ON(ret != 0);
650         par->bo_ptr = ttm_kmap_obj_virtual(&par->map, &dummy);
651
652         spin_lock_irqsave(&par->dirty.lock, flags);
653         par->dirty.active = true;
654         spin_unlock_irqrestore(&par->dirty.lock, flags);
655         vmw_kms_restore_vga(vmw_priv);
656
657 err_no_buffer:
658         vmw_fb_set_par(info);
659
660         vmw_fb_dirty_mark(par, 0, 0, info->var.xres, info->var.yres);
661
662         /* If there already was stuff dirty we wont
663          * schedule a new work, so lets do it now */
664         schedule_delayed_work(&info->deferred_work, 0);
665
666         return 0;
667 }