]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/gpu/drm/nouveau/nouveau_display.c
drm/nouveau: call drm_vblank_cleanup() earlier
[karo-tx-linux.git] / drivers / gpu / drm / nouveau / nouveau_display.c
1 /*
2  * Copyright (C) 2008 Maarten Maathuis.
3  * All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the
14  * next paragraph) shall be included in all copies or substantial
15  * portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20  * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
21  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  *
25  */
26
27 #include <drm/drmP.h>
28 #include <drm/drm_crtc_helper.h>
29
30 #include "nouveau_fbcon.h"
31 #include "dispnv04/hw.h"
32 #include "nouveau_crtc.h"
33 #include "nouveau_dma.h"
34 #include "nouveau_gem.h"
35 #include "nouveau_connector.h"
36 #include "nv50_display.h"
37
38 #include "nouveau_fence.h"
39
40 #include <engine/disp.h>
41
42 #include <core/class.h>
43
44 static int
45 nouveau_display_vblank_handler(void *data, int head)
46 {
47         struct nouveau_drm *drm = data;
48         drm_handle_vblank(drm->dev, head);
49         return NVKM_EVENT_KEEP;
50 }
51
52 int
53 nouveau_display_vblank_enable(struct drm_device *dev, int head)
54 {
55         struct nouveau_display *disp = nouveau_display(dev);
56         if (disp) {
57                 nouveau_event_get(disp->vblank[head]);
58                 return 0;
59         }
60         return -EIO;
61 }
62
63 void
64 nouveau_display_vblank_disable(struct drm_device *dev, int head)
65 {
66         struct nouveau_display *disp = nouveau_display(dev);
67         if (disp)
68                 nouveau_event_put(disp->vblank[head]);
69 }
70
71 static void
72 nouveau_display_vblank_fini(struct drm_device *dev)
73 {
74         struct nouveau_display *disp = nouveau_display(dev);
75         int i;
76
77         drm_vblank_cleanup(dev);
78
79         if (disp->vblank) {
80                 for (i = 0; i < dev->mode_config.num_crtc; i++)
81                         nouveau_event_ref(NULL, &disp->vblank[i]);
82                 kfree(disp->vblank);
83                 disp->vblank = NULL;
84         }
85 }
86
87 static int
88 nouveau_display_vblank_init(struct drm_device *dev)
89 {
90         struct nouveau_display *disp = nouveau_display(dev);
91         struct nouveau_drm *drm = nouveau_drm(dev);
92         struct nouveau_disp *pdisp = nouveau_disp(drm->device);
93         int ret, i;
94
95         disp->vblank = kzalloc(dev->mode_config.num_crtc *
96                                sizeof(*disp->vblank), GFP_KERNEL);
97         if (!disp->vblank)
98                 return -ENOMEM;
99
100         for (i = 0; i < dev->mode_config.num_crtc; i++) {
101                 ret = nouveau_event_new(pdisp->vblank, i,
102                                         nouveau_display_vblank_handler,
103                                         drm, &disp->vblank[i]);
104                 if (ret) {
105                         nouveau_display_vblank_fini(dev);
106                         return ret;
107                 }
108         }
109
110         ret = drm_vblank_init(dev, dev->mode_config.num_crtc);
111         if (ret) {
112                 nouveau_display_vblank_fini(dev);
113                 return ret;
114         }
115
116         return 0;
117 }
118
119 static void
120 nouveau_user_framebuffer_destroy(struct drm_framebuffer *drm_fb)
121 {
122         struct nouveau_framebuffer *fb = nouveau_framebuffer(drm_fb);
123
124         if (fb->nvbo)
125                 drm_gem_object_unreference_unlocked(&fb->nvbo->gem);
126
127         drm_framebuffer_cleanup(drm_fb);
128         kfree(fb);
129 }
130
131 static int
132 nouveau_user_framebuffer_create_handle(struct drm_framebuffer *drm_fb,
133                                        struct drm_file *file_priv,
134                                        unsigned int *handle)
135 {
136         struct nouveau_framebuffer *fb = nouveau_framebuffer(drm_fb);
137
138         return drm_gem_handle_create(file_priv, &fb->nvbo->gem, handle);
139 }
140
141 static const struct drm_framebuffer_funcs nouveau_framebuffer_funcs = {
142         .destroy = nouveau_user_framebuffer_destroy,
143         .create_handle = nouveau_user_framebuffer_create_handle,
144 };
145
146 int
147 nouveau_framebuffer_init(struct drm_device *dev,
148                          struct nouveau_framebuffer *nv_fb,
149                          struct drm_mode_fb_cmd2 *mode_cmd,
150                          struct nouveau_bo *nvbo)
151 {
152         struct nouveau_drm *drm = nouveau_drm(dev);
153         struct drm_framebuffer *fb = &nv_fb->base;
154         int ret;
155
156         drm_helper_mode_fill_fb_struct(fb, mode_cmd);
157         nv_fb->nvbo = nvbo;
158
159         if (nv_device(drm->device)->card_type >= NV_50) {
160                 u32 tile_flags = nouveau_bo_tile_layout(nvbo);
161                 if (tile_flags == 0x7a00 ||
162                     tile_flags == 0xfe00)
163                         nv_fb->r_dma = NvEvoFB32;
164                 else
165                 if (tile_flags == 0x7000)
166                         nv_fb->r_dma = NvEvoFB16;
167                 else
168                         nv_fb->r_dma = NvEvoVRAM_LP;
169
170                 switch (fb->depth) {
171                 case  8: nv_fb->r_format = 0x1e00; break;
172                 case 15: nv_fb->r_format = 0xe900; break;
173                 case 16: nv_fb->r_format = 0xe800; break;
174                 case 24:
175                 case 32: nv_fb->r_format = 0xcf00; break;
176                 case 30: nv_fb->r_format = 0xd100; break;
177                 default:
178                          NV_ERROR(drm, "unknown depth %d\n", fb->depth);
179                          return -EINVAL;
180                 }
181
182                 if (nvbo->tile_flags & NOUVEAU_GEM_TILE_NONCONTIG) {
183                         NV_ERROR(drm, "framebuffer requires contiguous bo\n");
184                         return -EINVAL;
185                 }
186
187                 if (nv_device(drm->device)->chipset == 0x50)
188                         nv_fb->r_format |= (tile_flags << 8);
189
190                 if (!tile_flags) {
191                         if (nv_device(drm->device)->card_type < NV_D0)
192                                 nv_fb->r_pitch = 0x00100000 | fb->pitches[0];
193                         else
194                                 nv_fb->r_pitch = 0x01000000 | fb->pitches[0];
195                 } else {
196                         u32 mode = nvbo->tile_mode;
197                         if (nv_device(drm->device)->card_type >= NV_C0)
198                                 mode >>= 4;
199                         nv_fb->r_pitch = ((fb->pitches[0] / 4) << 4) | mode;
200                 }
201         }
202
203         ret = drm_framebuffer_init(dev, fb, &nouveau_framebuffer_funcs);
204         if (ret) {
205                 return ret;
206         }
207
208         return 0;
209 }
210
211 static struct drm_framebuffer *
212 nouveau_user_framebuffer_create(struct drm_device *dev,
213                                 struct drm_file *file_priv,
214                                 struct drm_mode_fb_cmd2 *mode_cmd)
215 {
216         struct nouveau_framebuffer *nouveau_fb;
217         struct drm_gem_object *gem;
218         int ret = -ENOMEM;
219
220         gem = drm_gem_object_lookup(dev, file_priv, mode_cmd->handles[0]);
221         if (!gem)
222                 return ERR_PTR(-ENOENT);
223
224         nouveau_fb = kzalloc(sizeof(struct nouveau_framebuffer), GFP_KERNEL);
225         if (!nouveau_fb)
226                 goto err_unref;
227
228         ret = nouveau_framebuffer_init(dev, nouveau_fb, mode_cmd, nouveau_gem_object(gem));
229         if (ret)
230                 goto err;
231
232         return &nouveau_fb->base;
233
234 err:
235         kfree(nouveau_fb);
236 err_unref:
237         drm_gem_object_unreference(gem);
238         return ERR_PTR(ret);
239 }
240
241 static const struct drm_mode_config_funcs nouveau_mode_config_funcs = {
242         .fb_create = nouveau_user_framebuffer_create,
243         .output_poll_changed = nouveau_fbcon_output_poll_changed,
244 };
245
246
247 struct nouveau_drm_prop_enum_list {
248         u8 gen_mask;
249         int type;
250         char *name;
251 };
252
253 static struct nouveau_drm_prop_enum_list underscan[] = {
254         { 6, UNDERSCAN_AUTO, "auto" },
255         { 6, UNDERSCAN_OFF, "off" },
256         { 6, UNDERSCAN_ON, "on" },
257         {}
258 };
259
260 static struct nouveau_drm_prop_enum_list dither_mode[] = {
261         { 7, DITHERING_MODE_AUTO, "auto" },
262         { 7, DITHERING_MODE_OFF, "off" },
263         { 1, DITHERING_MODE_ON, "on" },
264         { 6, DITHERING_MODE_STATIC2X2, "static 2x2" },
265         { 6, DITHERING_MODE_DYNAMIC2X2, "dynamic 2x2" },
266         { 4, DITHERING_MODE_TEMPORAL, "temporal" },
267         {}
268 };
269
270 static struct nouveau_drm_prop_enum_list dither_depth[] = {
271         { 6, DITHERING_DEPTH_AUTO, "auto" },
272         { 6, DITHERING_DEPTH_6BPC, "6 bpc" },
273         { 6, DITHERING_DEPTH_8BPC, "8 bpc" },
274         {}
275 };
276
277 #define PROP_ENUM(p,gen,n,list) do {                                           \
278         struct nouveau_drm_prop_enum_list *l = (list);                         \
279         int c = 0;                                                             \
280         while (l->gen_mask) {                                                  \
281                 if (l->gen_mask & (1 << (gen)))                                \
282                         c++;                                                   \
283                 l++;                                                           \
284         }                                                                      \
285         if (c) {                                                               \
286                 p = drm_property_create(dev, DRM_MODE_PROP_ENUM, n, c);        \
287                 l = (list);                                                    \
288                 c = 0;                                                         \
289                 while (p && l->gen_mask) {                                     \
290                         if (l->gen_mask & (1 << (gen))) {                      \
291                                 drm_property_add_enum(p, c, l->type, l->name); \
292                                 c++;                                           \
293                         }                                                      \
294                         l++;                                                   \
295                 }                                                              \
296         }                                                                      \
297 } while(0)
298
299 int
300 nouveau_display_init(struct drm_device *dev)
301 {
302         struct nouveau_display *disp = nouveau_display(dev);
303         struct drm_connector *connector;
304         int ret;
305
306         ret = disp->init(dev);
307         if (ret)
308                 return ret;
309
310         /* enable polling for external displays */
311         drm_kms_helper_poll_enable(dev);
312
313         /* enable hotplug interrupts */
314         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
315                 struct nouveau_connector *conn = nouveau_connector(connector);
316                 if (conn->hpd_func) nouveau_event_get(conn->hpd_func);
317         }
318
319         return ret;
320 }
321
322 void
323 nouveau_display_fini(struct drm_device *dev)
324 {
325         struct nouveau_display *disp = nouveau_display(dev);
326         struct drm_connector *connector;
327
328         /* disable hotplug interrupts */
329         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
330                 struct nouveau_connector *conn = nouveau_connector(connector);
331                 if (conn->hpd_func) nouveau_event_put(conn->hpd_func);
332         }
333
334         drm_kms_helper_poll_disable(dev);
335         disp->fini(dev);
336 }
337
338 int
339 nouveau_display_create(struct drm_device *dev)
340 {
341         struct nouveau_drm *drm = nouveau_drm(dev);
342         struct nouveau_display *disp;
343         int ret, gen;
344
345         disp = drm->display = kzalloc(sizeof(*disp), GFP_KERNEL);
346         if (!disp)
347                 return -ENOMEM;
348
349         drm_mode_config_init(dev);
350         drm_mode_create_scaling_mode_property(dev);
351         drm_mode_create_dvi_i_properties(dev);
352
353         if (nv_device(drm->device)->card_type < NV_50)
354                 gen = 0;
355         else
356         if (nv_device(drm->device)->card_type < NV_D0)
357                 gen = 1;
358         else
359                 gen = 2;
360
361         PROP_ENUM(disp->dithering_mode, gen, "dithering mode", dither_mode);
362         PROP_ENUM(disp->dithering_depth, gen, "dithering depth", dither_depth);
363         PROP_ENUM(disp->underscan_property, gen, "underscan", underscan);
364
365         disp->underscan_hborder_property =
366                 drm_property_create_range(dev, 0, "underscan hborder", 0, 128);
367
368         disp->underscan_vborder_property =
369                 drm_property_create_range(dev, 0, "underscan vborder", 0, 128);
370
371         if (gen >= 1) {
372                 /* -90..+90 */
373                 disp->vibrant_hue_property =
374                         drm_property_create_range(dev, 0, "vibrant hue", 0, 180);
375
376                 /* -100..+100 */
377                 disp->color_vibrance_property =
378                         drm_property_create_range(dev, 0, "color vibrance", 0, 200);
379         }
380
381         dev->mode_config.funcs = &nouveau_mode_config_funcs;
382         dev->mode_config.fb_base = pci_resource_start(dev->pdev, 1);
383
384         dev->mode_config.min_width = 0;
385         dev->mode_config.min_height = 0;
386         if (nv_device(drm->device)->card_type < NV_10) {
387                 dev->mode_config.max_width = 2048;
388                 dev->mode_config.max_height = 2048;
389         } else
390         if (nv_device(drm->device)->card_type < NV_50) {
391                 dev->mode_config.max_width = 4096;
392                 dev->mode_config.max_height = 4096;
393         } else {
394                 dev->mode_config.max_width = 8192;
395                 dev->mode_config.max_height = 8192;
396         }
397
398         dev->mode_config.preferred_depth = 24;
399         dev->mode_config.prefer_shadow = 1;
400
401         if (nv_device(drm->device)->chipset < 0x11)
402                 dev->mode_config.async_page_flip = false;
403         else
404                 dev->mode_config.async_page_flip = true;
405
406         drm_kms_helper_poll_init(dev);
407         drm_kms_helper_poll_disable(dev);
408
409         if (drm->vbios.dcb.entries) {
410                 static const u16 oclass[] = {
411                         NVF0_DISP_CLASS,
412                         NVE0_DISP_CLASS,
413                         NVD0_DISP_CLASS,
414                         NVA3_DISP_CLASS,
415                         NV94_DISP_CLASS,
416                         NVA0_DISP_CLASS,
417                         NV84_DISP_CLASS,
418                         NV50_DISP_CLASS,
419                         NV04_DISP_CLASS,
420                 };
421                 int i;
422
423                 for (i = 0, ret = -ENODEV; ret && i < ARRAY_SIZE(oclass); i++) {
424                         ret = nouveau_object_new(nv_object(drm), NVDRM_DEVICE,
425                                                  NVDRM_DISPLAY, oclass[i],
426                                                  NULL, 0, &disp->core);
427                 }
428
429                 if (ret == 0) {
430                         if (nv_mclass(disp->core) < NV50_DISP_CLASS)
431                                 ret = nv04_display_create(dev);
432                         else
433                                 ret = nv50_display_create(dev);
434                 }
435         } else {
436                 ret = 0;
437         }
438
439         if (ret)
440                 goto disp_create_err;
441
442         if (dev->mode_config.num_crtc) {
443                 ret = nouveau_display_vblank_init(dev);
444                 if (ret)
445                         goto vblank_err;
446         }
447
448         nouveau_backlight_init(dev);
449         return 0;
450
451 vblank_err:
452         disp->dtor(dev);
453 disp_create_err:
454         drm_kms_helper_poll_fini(dev);
455         drm_mode_config_cleanup(dev);
456         return ret;
457 }
458
459 void
460 nouveau_display_destroy(struct drm_device *dev)
461 {
462         struct nouveau_display *disp = nouveau_display(dev);
463         struct nouveau_drm *drm = nouveau_drm(dev);
464
465         nouveau_backlight_exit(dev);
466         nouveau_display_vblank_fini(dev);
467
468         drm_kms_helper_poll_fini(dev);
469         drm_mode_config_cleanup(dev);
470
471         if (disp->dtor)
472                 disp->dtor(dev);
473
474         nouveau_object_del(nv_object(drm), NVDRM_DEVICE, NVDRM_DISPLAY);
475
476         nouveau_drm(dev)->display = NULL;
477         kfree(disp);
478 }
479
480 int
481 nouveau_display_suspend(struct drm_device *dev)
482 {
483         struct nouveau_drm *drm = nouveau_drm(dev);
484         struct drm_crtc *crtc;
485
486         nouveau_display_fini(dev);
487
488         NV_INFO(drm, "unpinning framebuffer(s)...\n");
489         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
490                 struct nouveau_framebuffer *nouveau_fb;
491
492                 nouveau_fb = nouveau_framebuffer(crtc->fb);
493                 if (!nouveau_fb || !nouveau_fb->nvbo)
494                         continue;
495
496                 nouveau_bo_unpin(nouveau_fb->nvbo);
497         }
498
499         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
500                 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
501
502                 nouveau_bo_unmap(nv_crtc->cursor.nvbo);
503                 nouveau_bo_unpin(nv_crtc->cursor.nvbo);
504         }
505
506         return 0;
507 }
508
509 void
510 nouveau_display_repin(struct drm_device *dev)
511 {
512         struct nouveau_drm *drm = nouveau_drm(dev);
513         struct drm_crtc *crtc;
514         int ret;
515
516         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
517                 struct nouveau_framebuffer *nouveau_fb;
518
519                 nouveau_fb = nouveau_framebuffer(crtc->fb);
520                 if (!nouveau_fb || !nouveau_fb->nvbo)
521                         continue;
522
523                 nouveau_bo_pin(nouveau_fb->nvbo, TTM_PL_FLAG_VRAM);
524         }
525
526         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
527                 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
528
529                 ret = nouveau_bo_pin(nv_crtc->cursor.nvbo, TTM_PL_FLAG_VRAM);
530                 if (!ret)
531                         ret = nouveau_bo_map(nv_crtc->cursor.nvbo);
532                 if (ret)
533                         NV_ERROR(drm, "Could not pin/map cursor.\n");
534         }
535 }
536
537 void
538 nouveau_display_resume(struct drm_device *dev)
539 {
540         struct drm_crtc *crtc;
541         nouveau_display_init(dev);
542
543         /* Force CLUT to get re-loaded during modeset */
544         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
545                 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
546
547                 nv_crtc->lut.depth = 0;
548         }
549
550         drm_helper_resume_force_mode(dev);
551
552         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
553                 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
554                 u32 offset = nv_crtc->cursor.nvbo->bo.offset;
555
556                 nv_crtc->cursor.set_offset(nv_crtc, offset);
557                 nv_crtc->cursor.set_pos(nv_crtc, nv_crtc->cursor_saved_x,
558                                                  nv_crtc->cursor_saved_y);
559         }
560 }
561
562 static int
563 nouveau_page_flip_emit(struct nouveau_channel *chan,
564                        struct nouveau_bo *old_bo,
565                        struct nouveau_bo *new_bo,
566                        struct nouveau_page_flip_state *s,
567                        struct nouveau_fence **pfence)
568 {
569         struct nouveau_fence_chan *fctx = chan->fence;
570         struct nouveau_drm *drm = chan->drm;
571         struct drm_device *dev = drm->dev;
572         unsigned long flags;
573         int ret;
574
575         /* Queue it to the pending list */
576         spin_lock_irqsave(&dev->event_lock, flags);
577         list_add_tail(&s->head, &fctx->flip);
578         spin_unlock_irqrestore(&dev->event_lock, flags);
579
580         /* Synchronize with the old framebuffer */
581         ret = nouveau_fence_sync(old_bo->bo.sync_obj, chan);
582         if (ret)
583                 goto fail;
584
585         /* Emit the pageflip */
586         ret = RING_SPACE(chan, 2);
587         if (ret)
588                 goto fail;
589
590         if (nv_device(drm->device)->card_type < NV_C0)
591                 BEGIN_NV04(chan, NvSubSw, NV_SW_PAGE_FLIP, 1);
592         else
593                 BEGIN_NVC0(chan, FermiSw, NV_SW_PAGE_FLIP, 1);
594         OUT_RING  (chan, 0x00000000);
595         FIRE_RING (chan);
596
597         ret = nouveau_fence_new(chan, false, pfence);
598         if (ret)
599                 goto fail;
600
601         return 0;
602 fail:
603         spin_lock_irqsave(&dev->event_lock, flags);
604         list_del(&s->head);
605         spin_unlock_irqrestore(&dev->event_lock, flags);
606         return ret;
607 }
608
609 int
610 nouveau_crtc_page_flip(struct drm_crtc *crtc, struct drm_framebuffer *fb,
611                        struct drm_pending_vblank_event *event, u32 flags)
612 {
613         const int swap_interval = (flags & DRM_MODE_PAGE_FLIP_ASYNC) ? 0 : 1;
614         struct drm_device *dev = crtc->dev;
615         struct nouveau_drm *drm = nouveau_drm(dev);
616         struct nouveau_bo *old_bo = nouveau_framebuffer(crtc->fb)->nvbo;
617         struct nouveau_bo *new_bo = nouveau_framebuffer(fb)->nvbo;
618         struct nouveau_page_flip_state *s;
619         struct nouveau_channel *chan = drm->channel;
620         struct nouveau_fence *fence;
621         int ret;
622
623         if (!drm->channel)
624                 return -ENODEV;
625
626         s = kzalloc(sizeof(*s), GFP_KERNEL);
627         if (!s)
628                 return -ENOMEM;
629
630         if (new_bo != old_bo) {
631                 ret = nouveau_bo_pin(new_bo, TTM_PL_FLAG_VRAM);
632                 if (ret)
633                         goto fail_free;
634         }
635
636         mutex_lock(&chan->cli->mutex);
637
638         /* synchronise rendering channel with the kernel's channel */
639         spin_lock(&new_bo->bo.bdev->fence_lock);
640         fence = nouveau_fence_ref(new_bo->bo.sync_obj);
641         spin_unlock(&new_bo->bo.bdev->fence_lock);
642         ret = nouveau_fence_sync(fence, chan);
643         nouveau_fence_unref(&fence);
644         if (ret)
645                 goto fail_free;
646
647         ret = ttm_bo_reserve(&old_bo->bo, true, false, false, NULL);
648         if (ret)
649                 goto fail_unpin;
650
651         /* Initialize a page flip struct */
652         *s = (struct nouveau_page_flip_state)
653                 { { }, event, nouveau_crtc(crtc)->index,
654                   fb->bits_per_pixel, fb->pitches[0], crtc->x, crtc->y,
655                   new_bo->bo.offset };
656
657         /* Emit a page flip */
658         if (nv_device(drm->device)->card_type >= NV_50) {
659                 ret = nv50_display_flip_next(crtc, fb, chan, swap_interval);
660                 if (ret)
661                         goto fail_unreserve;
662         } else {
663                 struct nv04_display *dispnv04 = nv04_display(dev);
664                 int head = nouveau_crtc(crtc)->index;
665
666                 if (swap_interval) {
667                         ret = RING_SPACE(chan, 8);
668                         if (ret)
669                                 goto fail_unreserve;
670
671                         BEGIN_NV04(chan, NvSubImageBlit, 0x012c, 1);
672                         OUT_RING  (chan, 0);
673                         BEGIN_NV04(chan, NvSubImageBlit, 0x0134, 1);
674                         OUT_RING  (chan, head);
675                         BEGIN_NV04(chan, NvSubImageBlit, 0x0100, 1);
676                         OUT_RING  (chan, 0);
677                         BEGIN_NV04(chan, NvSubImageBlit, 0x0130, 1);
678                         OUT_RING  (chan, 0);
679                 }
680
681                 nouveau_bo_ref(new_bo, &dispnv04->image[head]);
682         }
683
684         ret = nouveau_page_flip_emit(chan, old_bo, new_bo, s, &fence);
685         mutex_unlock(&chan->cli->mutex);
686         if (ret)
687                 goto fail_unreserve;
688
689         /* Update the crtc struct and cleanup */
690         crtc->fb = fb;
691
692         nouveau_bo_fence(old_bo, fence);
693         ttm_bo_unreserve(&old_bo->bo);
694         if (old_bo != new_bo)
695                 nouveau_bo_unpin(old_bo);
696         nouveau_fence_unref(&fence);
697         return 0;
698
699 fail_unreserve:
700         ttm_bo_unreserve(&old_bo->bo);
701 fail_unpin:
702         mutex_unlock(&chan->cli->mutex);
703         if (old_bo != new_bo)
704                 nouveau_bo_unpin(new_bo);
705 fail_free:
706         kfree(s);
707         return ret;
708 }
709
710 int
711 nouveau_finish_page_flip(struct nouveau_channel *chan,
712                          struct nouveau_page_flip_state *ps)
713 {
714         struct nouveau_fence_chan *fctx = chan->fence;
715         struct nouveau_drm *drm = chan->drm;
716         struct drm_device *dev = drm->dev;
717         struct nouveau_page_flip_state *s;
718         unsigned long flags;
719
720         spin_lock_irqsave(&dev->event_lock, flags);
721
722         if (list_empty(&fctx->flip)) {
723                 NV_ERROR(drm, "unexpected pageflip\n");
724                 spin_unlock_irqrestore(&dev->event_lock, flags);
725                 return -EINVAL;
726         }
727
728         s = list_first_entry(&fctx->flip, struct nouveau_page_flip_state, head);
729         if (s->event)
730                 drm_send_vblank_event(dev, s->crtc, s->event);
731
732         list_del(&s->head);
733         if (ps)
734                 *ps = *s;
735         kfree(s);
736
737         spin_unlock_irqrestore(&dev->event_lock, flags);
738         return 0;
739 }
740
741 int
742 nouveau_flip_complete(void *data)
743 {
744         struct nouveau_channel *chan = data;
745         struct nouveau_drm *drm = chan->drm;
746         struct nouveau_page_flip_state state;
747
748         if (!nouveau_finish_page_flip(chan, &state)) {
749                 if (nv_device(drm->device)->card_type < NV_50) {
750                         nv_set_crtc_base(drm->dev, state.crtc, state.offset +
751                                          state.y * state.pitch +
752                                          state.x * state.bpp / 8);
753                 }
754         }
755
756         return 0;
757 }
758
759 int
760 nouveau_display_dumb_create(struct drm_file *file_priv, struct drm_device *dev,
761                             struct drm_mode_create_dumb *args)
762 {
763         struct nouveau_bo *bo;
764         int ret;
765
766         args->pitch = roundup(args->width * (args->bpp / 8), 256);
767         args->size = args->pitch * args->height;
768         args->size = roundup(args->size, PAGE_SIZE);
769
770         ret = nouveau_gem_new(dev, args->size, 0, NOUVEAU_GEM_DOMAIN_VRAM, 0, 0, &bo);
771         if (ret)
772                 return ret;
773
774         ret = drm_gem_handle_create(file_priv, &bo->gem, &args->handle);
775         drm_gem_object_unreference_unlocked(&bo->gem);
776         return ret;
777 }
778
779 int
780 nouveau_display_dumb_map_offset(struct drm_file *file_priv,
781                                 struct drm_device *dev,
782                                 uint32_t handle, uint64_t *poffset)
783 {
784         struct drm_gem_object *gem;
785
786         gem = drm_gem_object_lookup(dev, file_priv, handle);
787         if (gem) {
788                 struct nouveau_bo *bo = nouveau_gem_object(gem);
789                 *poffset = drm_vma_node_offset_addr(&bo->bo.vma_node);
790                 drm_gem_object_unreference_unlocked(gem);
791                 return 0;
792         }
793
794         return -ENOENT;
795 }