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