drm/nouveau/nvif: give every disp object a human-readable identifier
[linux-2.6-microblaze.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 <acpi/video.h>
28
29 #include <drm/drm_atomic.h>
30 #include <drm/drm_atomic_helper.h>
31 #include <drm/drm_crtc_helper.h>
32 #include <drm/drm_fb_helper.h>
33 #include <drm/drm_fourcc.h>
34 #include <drm/drm_gem_framebuffer_helper.h>
35 #include <drm/drm_probe_helper.h>
36 #include <drm/drm_vblank.h>
37
38 #include "nouveau_fbcon.h"
39 #include "nouveau_crtc.h"
40 #include "nouveau_gem.h"
41 #include "nouveau_connector.h"
42 #include "nv50_display.h"
43
44 #include <nvif/class.h>
45 #include <nvif/cl0046.h>
46 #include <nvif/event.h>
47 #include <dispnv50/crc.h>
48
49 int
50 nouveau_display_vblank_enable(struct drm_crtc *crtc)
51 {
52         struct nouveau_crtc *nv_crtc;
53
54         nv_crtc = nouveau_crtc(crtc);
55         nvif_notify_get(&nv_crtc->vblank);
56
57         return 0;
58 }
59
60 void
61 nouveau_display_vblank_disable(struct drm_crtc *crtc)
62 {
63         struct nouveau_crtc *nv_crtc;
64
65         nv_crtc = nouveau_crtc(crtc);
66         nvif_notify_put(&nv_crtc->vblank);
67 }
68
69 static inline int
70 calc(int blanks, int blanke, int total, int line)
71 {
72         if (blanke >= blanks) {
73                 if (line >= blanks)
74                         line -= total;
75         } else {
76                 if (line >= blanks)
77                         line -= total;
78                 line -= blanke + 1;
79         }
80         return line;
81 }
82
83 static bool
84 nouveau_display_scanoutpos_head(struct drm_crtc *crtc, int *vpos, int *hpos,
85                                 ktime_t *stime, ktime_t *etime)
86 {
87         struct {
88                 struct nv04_disp_mthd_v0 base;
89                 struct nv04_disp_scanoutpos_v0 scan;
90         } args = {
91                 .base.method = NV04_DISP_SCANOUTPOS,
92                 .base.head = nouveau_crtc(crtc)->index,
93         };
94         struct nouveau_display *disp = nouveau_display(crtc->dev);
95         struct drm_vblank_crtc *vblank = &crtc->dev->vblank[drm_crtc_index(crtc)];
96         int retry = 20;
97         bool ret = false;
98
99         do {
100                 ret = nvif_mthd(&disp->disp.object, 0, &args, sizeof(args));
101                 if (ret != 0)
102                         return false;
103
104                 if (args.scan.vline) {
105                         ret = true;
106                         break;
107                 }
108
109                 if (retry) ndelay(vblank->linedur_ns);
110         } while (retry--);
111
112         *hpos = args.scan.hline;
113         *vpos = calc(args.scan.vblanks, args.scan.vblanke,
114                      args.scan.vtotal, args.scan.vline);
115         if (stime) *stime = ns_to_ktime(args.scan.time[0]);
116         if (etime) *etime = ns_to_ktime(args.scan.time[1]);
117
118         return ret;
119 }
120
121 bool
122 nouveau_display_scanoutpos(struct drm_crtc *crtc,
123                            bool in_vblank_irq, int *vpos, int *hpos,
124                            ktime_t *stime, ktime_t *etime,
125                            const struct drm_display_mode *mode)
126 {
127         return nouveau_display_scanoutpos_head(crtc, vpos, hpos,
128                                                stime, etime);
129 }
130
131 static const struct drm_framebuffer_funcs nouveau_framebuffer_funcs = {
132         .destroy = drm_gem_fb_destroy,
133         .create_handle = drm_gem_fb_create_handle,
134 };
135
136 static void
137 nouveau_decode_mod(struct nouveau_drm *drm,
138                    uint64_t modifier,
139                    uint32_t *tile_mode,
140                    uint8_t *kind)
141 {
142         BUG_ON(!tile_mode || !kind);
143
144         if (modifier == DRM_FORMAT_MOD_LINEAR) {
145                 /* tile_mode will not be used in this case */
146                 *tile_mode = 0;
147                 *kind = 0;
148         } else {
149                 /*
150                  * Extract the block height and kind from the corresponding
151                  * modifier fields.  See drm_fourcc.h for details.
152                  */
153                 *tile_mode = (uint32_t)(modifier & 0xF);
154                 *kind = (uint8_t)((modifier >> 12) & 0xFF);
155
156                 if (drm->client.device.info.chipset >= 0xc0)
157                         *tile_mode <<= 4;
158         }
159 }
160
161 void
162 nouveau_framebuffer_get_layout(struct drm_framebuffer *fb,
163                                uint32_t *tile_mode,
164                                uint8_t *kind)
165 {
166         if (fb->flags & DRM_MODE_FB_MODIFIERS) {
167                 struct nouveau_drm *drm = nouveau_drm(fb->dev);
168
169                 nouveau_decode_mod(drm, fb->modifier, tile_mode, kind);
170         } else {
171                 const struct nouveau_bo *nvbo = nouveau_gem_object(fb->obj[0]);
172
173                 *tile_mode = nvbo->mode;
174                 *kind = nvbo->kind;
175         }
176 }
177
178 static int
179 nouveau_validate_decode_mod(struct nouveau_drm *drm,
180                             uint64_t modifier,
181                             uint32_t *tile_mode,
182                             uint8_t *kind)
183 {
184         struct nouveau_display *disp = nouveau_display(drm->dev);
185         int mod;
186
187         if (drm->client.device.info.family < NV_DEVICE_INFO_V0_TESLA) {
188                 return -EINVAL;
189         }
190
191         BUG_ON(!disp->format_modifiers);
192
193         for (mod = 0;
194              (disp->format_modifiers[mod] != DRM_FORMAT_MOD_INVALID) &&
195              (disp->format_modifiers[mod] != modifier);
196              mod++);
197
198         if (disp->format_modifiers[mod] == DRM_FORMAT_MOD_INVALID)
199                 return -EINVAL;
200
201         nouveau_decode_mod(drm, modifier, tile_mode, kind);
202
203         return 0;
204 }
205
206 static inline uint32_t
207 nouveau_get_width_in_blocks(uint32_t stride)
208 {
209         /* GOBs per block in the x direction is always one, and GOBs are
210          * 64 bytes wide
211          */
212         static const uint32_t log_block_width = 6;
213
214         return (stride + (1 << log_block_width) - 1) >> log_block_width;
215 }
216
217 static inline uint32_t
218 nouveau_get_height_in_blocks(struct nouveau_drm *drm,
219                              uint32_t height,
220                              uint32_t log_block_height_in_gobs)
221 {
222         uint32_t log_gob_height;
223         uint32_t log_block_height;
224
225         BUG_ON(drm->client.device.info.family < NV_DEVICE_INFO_V0_TESLA);
226
227         if (drm->client.device.info.family < NV_DEVICE_INFO_V0_FERMI)
228                 log_gob_height = 2;
229         else
230                 log_gob_height = 3;
231
232         log_block_height = log_block_height_in_gobs + log_gob_height;
233
234         return (height + (1 << log_block_height) - 1) >> log_block_height;
235 }
236
237 static int
238 nouveau_check_bl_size(struct nouveau_drm *drm, struct nouveau_bo *nvbo,
239                       uint32_t offset, uint32_t stride, uint32_t h,
240                       uint32_t tile_mode)
241 {
242         uint32_t gob_size, bw, bh;
243         uint64_t bl_size;
244
245         BUG_ON(drm->client.device.info.family < NV_DEVICE_INFO_V0_TESLA);
246
247         if (drm->client.device.info.chipset >= 0xc0) {
248                 if (tile_mode & 0xF)
249                         return -EINVAL;
250                 tile_mode >>= 4;
251         }
252
253         if (tile_mode & 0xFFFFFFF0)
254                 return -EINVAL;
255
256         if (drm->client.device.info.family < NV_DEVICE_INFO_V0_FERMI)
257                 gob_size = 256;
258         else
259                 gob_size = 512;
260
261         bw = nouveau_get_width_in_blocks(stride);
262         bh = nouveau_get_height_in_blocks(drm, h, tile_mode);
263
264         bl_size = bw * bh * (1 << tile_mode) * gob_size;
265
266         DRM_DEBUG_KMS("offset=%u stride=%u h=%u tile_mode=0x%02x bw=%u bh=%u gob_size=%u bl_size=%llu size=%lu\n",
267                       offset, stride, h, tile_mode, bw, bh, gob_size, bl_size,
268                       nvbo->bo.mem.size);
269
270         if (bl_size + offset > nvbo->bo.mem.size)
271                 return -ERANGE;
272
273         return 0;
274 }
275
276 int
277 nouveau_framebuffer_new(struct drm_device *dev,
278                         const struct drm_mode_fb_cmd2 *mode_cmd,
279                         struct drm_gem_object *gem,
280                         struct drm_framebuffer **pfb)
281 {
282         struct nouveau_drm *drm = nouveau_drm(dev);
283         struct nouveau_bo *nvbo = nouveau_gem_object(gem);
284         struct drm_framebuffer *fb;
285         const struct drm_format_info *info;
286         unsigned int width, height, i;
287         uint32_t tile_mode;
288         uint8_t kind;
289         int ret;
290
291         /* YUV overlays have special requirements pre-NV50 */
292         if (drm->client.device.info.family < NV_DEVICE_INFO_V0_TESLA &&
293
294             (mode_cmd->pixel_format == DRM_FORMAT_YUYV ||
295              mode_cmd->pixel_format == DRM_FORMAT_UYVY ||
296              mode_cmd->pixel_format == DRM_FORMAT_NV12 ||
297              mode_cmd->pixel_format == DRM_FORMAT_NV21) &&
298             (mode_cmd->pitches[0] & 0x3f || /* align 64 */
299              mode_cmd->pitches[0] >= 0x10000 || /* at most 64k pitch */
300              (mode_cmd->pitches[1] && /* pitches for planes must match */
301               mode_cmd->pitches[0] != mode_cmd->pitches[1]))) {
302                 struct drm_format_name_buf format_name;
303                 DRM_DEBUG_KMS("Unsuitable framebuffer: format: %s; pitches: 0x%x\n 0x%x\n",
304                               drm_get_format_name(mode_cmd->pixel_format,
305                                                   &format_name),
306                               mode_cmd->pitches[0],
307                               mode_cmd->pitches[1]);
308                 return -EINVAL;
309         }
310
311         if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
312                 if (nouveau_validate_decode_mod(drm, mode_cmd->modifier[0],
313                                                 &tile_mode, &kind)) {
314                         DRM_DEBUG_KMS("Unsupported modifier: 0x%llx\n",
315                                       mode_cmd->modifier[0]);
316                         return -EINVAL;
317                 }
318         } else {
319                 tile_mode = nvbo->mode;
320                 kind = nvbo->kind;
321         }
322
323         info = drm_get_format_info(dev, mode_cmd);
324
325         for (i = 0; i < info->num_planes; i++) {
326                 width = drm_format_info_plane_width(info,
327                                                     mode_cmd->width,
328                                                     i);
329                 height = drm_format_info_plane_height(info,
330                                                       mode_cmd->height,
331                                                       i);
332
333                 if (kind) {
334                         ret = nouveau_check_bl_size(drm, nvbo,
335                                                     mode_cmd->offsets[i],
336                                                     mode_cmd->pitches[i],
337                                                     height, tile_mode);
338                         if (ret)
339                                 return ret;
340                 } else {
341                         uint32_t size = mode_cmd->pitches[i] * height;
342
343                         if (size + mode_cmd->offsets[i] > nvbo->bo.mem.size)
344                                 return -ERANGE;
345                 }
346         }
347
348         if (!(fb = *pfb = kzalloc(sizeof(*fb), GFP_KERNEL)))
349                 return -ENOMEM;
350
351         drm_helper_mode_fill_fb_struct(dev, fb, mode_cmd);
352         fb->obj[0] = gem;
353
354         ret = drm_framebuffer_init(dev, fb, &nouveau_framebuffer_funcs);
355         if (ret)
356                 kfree(fb);
357         return ret;
358 }
359
360 struct drm_framebuffer *
361 nouveau_user_framebuffer_create(struct drm_device *dev,
362                                 struct drm_file *file_priv,
363                                 const struct drm_mode_fb_cmd2 *mode_cmd)
364 {
365         struct drm_framebuffer *fb;
366         struct drm_gem_object *gem;
367         int ret;
368
369         gem = drm_gem_object_lookup(file_priv, mode_cmd->handles[0]);
370         if (!gem)
371                 return ERR_PTR(-ENOENT);
372
373         ret = nouveau_framebuffer_new(dev, mode_cmd, gem, &fb);
374         if (ret == 0)
375                 return fb;
376
377         drm_gem_object_put(gem);
378         return ERR_PTR(ret);
379 }
380
381 static const struct drm_mode_config_funcs nouveau_mode_config_funcs = {
382         .fb_create = nouveau_user_framebuffer_create,
383         .output_poll_changed = nouveau_fbcon_output_poll_changed,
384 };
385
386
387 struct nouveau_drm_prop_enum_list {
388         u8 gen_mask;
389         int type;
390         char *name;
391 };
392
393 static struct nouveau_drm_prop_enum_list underscan[] = {
394         { 6, UNDERSCAN_AUTO, "auto" },
395         { 6, UNDERSCAN_OFF, "off" },
396         { 6, UNDERSCAN_ON, "on" },
397         {}
398 };
399
400 static struct nouveau_drm_prop_enum_list dither_mode[] = {
401         { 7, DITHERING_MODE_AUTO, "auto" },
402         { 7, DITHERING_MODE_OFF, "off" },
403         { 1, DITHERING_MODE_ON, "on" },
404         { 6, DITHERING_MODE_STATIC2X2, "static 2x2" },
405         { 6, DITHERING_MODE_DYNAMIC2X2, "dynamic 2x2" },
406         { 4, DITHERING_MODE_TEMPORAL, "temporal" },
407         {}
408 };
409
410 static struct nouveau_drm_prop_enum_list dither_depth[] = {
411         { 6, DITHERING_DEPTH_AUTO, "auto" },
412         { 6, DITHERING_DEPTH_6BPC, "6 bpc" },
413         { 6, DITHERING_DEPTH_8BPC, "8 bpc" },
414         {}
415 };
416
417 #define PROP_ENUM(p,gen,n,list) do {                                           \
418         struct nouveau_drm_prop_enum_list *l = (list);                         \
419         int c = 0;                                                             \
420         while (l->gen_mask) {                                                  \
421                 if (l->gen_mask & (1 << (gen)))                                \
422                         c++;                                                   \
423                 l++;                                                           \
424         }                                                                      \
425         if (c) {                                                               \
426                 p = drm_property_create(dev, DRM_MODE_PROP_ENUM, n, c);        \
427                 l = (list);                                                    \
428                 while (p && l->gen_mask) {                                     \
429                         if (l->gen_mask & (1 << (gen))) {                      \
430                                 drm_property_add_enum(p, l->type, l->name);    \
431                         }                                                      \
432                         l++;                                                   \
433                 }                                                              \
434         }                                                                      \
435 } while(0)
436
437 static void
438 nouveau_display_hpd_work(struct work_struct *work)
439 {
440         struct nouveau_drm *drm = container_of(work, typeof(*drm), hpd_work);
441
442         pm_runtime_get_sync(drm->dev->dev);
443
444         drm_helper_hpd_irq_event(drm->dev);
445
446         pm_runtime_mark_last_busy(drm->dev->dev);
447         pm_runtime_put_sync(drm->dev->dev);
448 }
449
450 #ifdef CONFIG_ACPI
451
452 static int
453 nouveau_display_acpi_ntfy(struct notifier_block *nb, unsigned long val,
454                           void *data)
455 {
456         struct nouveau_drm *drm = container_of(nb, typeof(*drm), acpi_nb);
457         struct acpi_bus_event *info = data;
458         int ret;
459
460         if (!strcmp(info->device_class, ACPI_VIDEO_CLASS)) {
461                 if (info->type == ACPI_VIDEO_NOTIFY_PROBE) {
462                         ret = pm_runtime_get(drm->dev->dev);
463                         if (ret == 1 || ret == -EACCES) {
464                                 /* If the GPU is already awake, or in a state
465                                  * where we can't wake it up, it can handle
466                                  * it's own hotplug events.
467                                  */
468                                 pm_runtime_put_autosuspend(drm->dev->dev);
469                         } else if (ret == 0) {
470                                 /* This may be the only indication we receive
471                                  * of a connector hotplug on a runtime
472                                  * suspended GPU, schedule hpd_work to check.
473                                  */
474                                 NV_DEBUG(drm, "ACPI requested connector reprobe\n");
475                                 schedule_work(&drm->hpd_work);
476                                 pm_runtime_put_noidle(drm->dev->dev);
477                         } else {
478                                 NV_WARN(drm, "Dropped ACPI reprobe event due to RPM error: %d\n",
479                                         ret);
480                         }
481
482                         /* acpi-video should not generate keypresses for this */
483                         return NOTIFY_BAD;
484                 }
485         }
486
487         return NOTIFY_DONE;
488 }
489 #endif
490
491 int
492 nouveau_display_init(struct drm_device *dev, bool resume, bool runtime)
493 {
494         struct nouveau_display *disp = nouveau_display(dev);
495         struct drm_connector *connector;
496         struct drm_connector_list_iter conn_iter;
497         int ret;
498
499         /*
500          * Enable hotplug interrupts (done as early as possible, since we need
501          * them for MST)
502          */
503         drm_connector_list_iter_begin(dev, &conn_iter);
504         nouveau_for_each_non_mst_connector_iter(connector, &conn_iter) {
505                 struct nouveau_connector *conn = nouveau_connector(connector);
506                 nvif_notify_get(&conn->hpd);
507         }
508         drm_connector_list_iter_end(&conn_iter);
509
510         ret = disp->init(dev, resume, runtime);
511         if (ret)
512                 return ret;
513
514         /* enable connector detection and polling for connectors without HPD
515          * support
516          */
517         drm_kms_helper_poll_enable(dev);
518
519         return ret;
520 }
521
522 void
523 nouveau_display_fini(struct drm_device *dev, bool suspend, bool runtime)
524 {
525         struct nouveau_display *disp = nouveau_display(dev);
526         struct nouveau_drm *drm = nouveau_drm(dev);
527         struct drm_connector *connector;
528         struct drm_connector_list_iter conn_iter;
529
530         if (!suspend) {
531                 if (drm_drv_uses_atomic_modeset(dev))
532                         drm_atomic_helper_shutdown(dev);
533                 else
534                         drm_helper_force_disable_all(dev);
535         }
536
537         /* disable hotplug interrupts */
538         drm_connector_list_iter_begin(dev, &conn_iter);
539         nouveau_for_each_non_mst_connector_iter(connector, &conn_iter) {
540                 struct nouveau_connector *conn = nouveau_connector(connector);
541                 nvif_notify_put(&conn->hpd);
542         }
543         drm_connector_list_iter_end(&conn_iter);
544
545         if (!runtime)
546                 cancel_work_sync(&drm->hpd_work);
547
548         drm_kms_helper_poll_disable(dev);
549         disp->fini(dev, suspend);
550 }
551
552 static void
553 nouveau_display_create_properties(struct drm_device *dev)
554 {
555         struct nouveau_display *disp = nouveau_display(dev);
556         int gen;
557
558         if (disp->disp.object.oclass < NV50_DISP)
559                 gen = 0;
560         else
561         if (disp->disp.object.oclass < GF110_DISP)
562                 gen = 1;
563         else
564                 gen = 2;
565
566         PROP_ENUM(disp->dithering_mode, gen, "dithering mode", dither_mode);
567         PROP_ENUM(disp->dithering_depth, gen, "dithering depth", dither_depth);
568         PROP_ENUM(disp->underscan_property, gen, "underscan", underscan);
569
570         disp->underscan_hborder_property =
571                 drm_property_create_range(dev, 0, "underscan hborder", 0, 128);
572
573         disp->underscan_vborder_property =
574                 drm_property_create_range(dev, 0, "underscan vborder", 0, 128);
575
576         if (gen < 1)
577                 return;
578
579         /* -90..+90 */
580         disp->vibrant_hue_property =
581                 drm_property_create_range(dev, 0, "vibrant hue", 0, 180);
582
583         /* -100..+100 */
584         disp->color_vibrance_property =
585                 drm_property_create_range(dev, 0, "color vibrance", 0, 200);
586 }
587
588 int
589 nouveau_display_create(struct drm_device *dev)
590 {
591         struct nouveau_drm *drm = nouveau_drm(dev);
592         struct nvkm_device *device = nvxx_device(&drm->client.device);
593         struct nouveau_display *disp;
594         int ret;
595
596         disp = drm->display = kzalloc(sizeof(*disp), GFP_KERNEL);
597         if (!disp)
598                 return -ENOMEM;
599
600         drm_mode_config_init(dev);
601         drm_mode_create_scaling_mode_property(dev);
602         drm_mode_create_dvi_i_properties(dev);
603
604         dev->mode_config.funcs = &nouveau_mode_config_funcs;
605         dev->mode_config.fb_base = device->func->resource_addr(device, 1);
606
607         dev->mode_config.min_width = 0;
608         dev->mode_config.min_height = 0;
609         if (drm->client.device.info.family < NV_DEVICE_INFO_V0_CELSIUS) {
610                 dev->mode_config.max_width = 2048;
611                 dev->mode_config.max_height = 2048;
612         } else
613         if (drm->client.device.info.family < NV_DEVICE_INFO_V0_TESLA) {
614                 dev->mode_config.max_width = 4096;
615                 dev->mode_config.max_height = 4096;
616         } else
617         if (drm->client.device.info.family < NV_DEVICE_INFO_V0_FERMI) {
618                 dev->mode_config.max_width = 8192;
619                 dev->mode_config.max_height = 8192;
620         } else {
621                 dev->mode_config.max_width = 16384;
622                 dev->mode_config.max_height = 16384;
623         }
624
625         dev->mode_config.preferred_depth = 24;
626         dev->mode_config.prefer_shadow = 1;
627         dev->mode_config.allow_fb_modifiers = true;
628
629         if (drm->client.device.info.chipset < 0x11)
630                 dev->mode_config.async_page_flip = false;
631         else
632                 dev->mode_config.async_page_flip = true;
633
634         drm_kms_helper_poll_init(dev);
635         drm_kms_helper_poll_disable(dev);
636
637         if (nouveau_modeset != 2 && drm->vbios.dcb.entries) {
638                 ret = nvif_disp_ctor(&drm->client.device, "kmsDisp", 0,
639                                      &disp->disp);
640                 if (ret == 0) {
641                         nouveau_display_create_properties(dev);
642                         if (disp->disp.object.oclass < NV50_DISP)
643                                 ret = nv04_display_create(dev);
644                         else
645                                 ret = nv50_display_create(dev);
646                 }
647         } else {
648                 ret = 0;
649         }
650
651         if (ret)
652                 goto disp_create_err;
653
654         drm_mode_config_reset(dev);
655
656         if (dev->mode_config.num_crtc) {
657                 ret = drm_vblank_init(dev, dev->mode_config.num_crtc);
658                 if (ret)
659                         goto vblank_err;
660
661                 if (disp->disp.object.oclass >= NV50_DISP)
662                         nv50_crc_init(dev);
663         }
664
665         INIT_WORK(&drm->hpd_work, nouveau_display_hpd_work);
666 #ifdef CONFIG_ACPI
667         drm->acpi_nb.notifier_call = nouveau_display_acpi_ntfy;
668         register_acpi_notifier(&drm->acpi_nb);
669 #endif
670
671         return 0;
672
673 vblank_err:
674         disp->dtor(dev);
675 disp_create_err:
676         drm_kms_helper_poll_fini(dev);
677         drm_mode_config_cleanup(dev);
678         return ret;
679 }
680
681 void
682 nouveau_display_destroy(struct drm_device *dev)
683 {
684         struct nouveau_display *disp = nouveau_display(dev);
685
686 #ifdef CONFIG_ACPI
687         unregister_acpi_notifier(&nouveau_drm(dev)->acpi_nb);
688 #endif
689
690         drm_kms_helper_poll_fini(dev);
691         drm_mode_config_cleanup(dev);
692
693         if (disp->dtor)
694                 disp->dtor(dev);
695
696         nvif_disp_dtor(&disp->disp);
697
698         nouveau_drm(dev)->display = NULL;
699         kfree(disp);
700 }
701
702 int
703 nouveau_display_suspend(struct drm_device *dev, bool runtime)
704 {
705         struct nouveau_display *disp = nouveau_display(dev);
706
707         if (drm_drv_uses_atomic_modeset(dev)) {
708                 if (!runtime) {
709                         disp->suspend = drm_atomic_helper_suspend(dev);
710                         if (IS_ERR(disp->suspend)) {
711                                 int ret = PTR_ERR(disp->suspend);
712                                 disp->suspend = NULL;
713                                 return ret;
714                         }
715                 }
716         }
717
718         nouveau_display_fini(dev, true, runtime);
719         return 0;
720 }
721
722 void
723 nouveau_display_resume(struct drm_device *dev, bool runtime)
724 {
725         struct nouveau_display *disp = nouveau_display(dev);
726
727         nouveau_display_init(dev, true, runtime);
728
729         if (drm_drv_uses_atomic_modeset(dev)) {
730                 if (disp->suspend) {
731                         drm_atomic_helper_resume(dev, disp->suspend);
732                         disp->suspend = NULL;
733                 }
734                 return;
735         }
736 }
737
738 int
739 nouveau_display_dumb_create(struct drm_file *file_priv, struct drm_device *dev,
740                             struct drm_mode_create_dumb *args)
741 {
742         struct nouveau_cli *cli = nouveau_cli(file_priv);
743         struct nouveau_bo *bo;
744         uint32_t domain;
745         int ret;
746
747         args->pitch = roundup(args->width * (args->bpp / 8), 256);
748         args->size = args->pitch * args->height;
749         args->size = roundup(args->size, PAGE_SIZE);
750
751         /* Use VRAM if there is any ; otherwise fallback to system memory */
752         if (nouveau_drm(dev)->client.device.info.ram_size != 0)
753                 domain = NOUVEAU_GEM_DOMAIN_VRAM;
754         else
755                 domain = NOUVEAU_GEM_DOMAIN_GART;
756
757         ret = nouveau_gem_new(cli, args->size, 0, domain, 0, 0, &bo);
758         if (ret)
759                 return ret;
760
761         ret = drm_gem_handle_create(file_priv, &bo->bo.base, &args->handle);
762         drm_gem_object_put(&bo->bo.base);
763         return ret;
764 }
765
766 int
767 nouveau_display_dumb_map_offset(struct drm_file *file_priv,
768                                 struct drm_device *dev,
769                                 uint32_t handle, uint64_t *poffset)
770 {
771         struct drm_gem_object *gem;
772
773         gem = drm_gem_object_lookup(file_priv, handle);
774         if (gem) {
775                 struct nouveau_bo *bo = nouveau_gem_object(gem);
776                 *poffset = drm_vma_node_offset_addr(&bo->bo.base.vma_node);
777                 drm_gem_object_put(gem);
778                 return 0;
779         }
780
781         return -ENOENT;
782 }