xref: /openbmc/linux/drivers/gpu/drm/nouveau/nouveau_display.c (revision a6ca5ac746d104019e76c29e69c2a1fc6dd2b29f)
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 #include <drm/drmP.h>
29 #include <drm/drm_atomic.h>
30 #include <drm/drm_atomic_helper.h>
31 #include <drm/drm_crtc_helper.h>
32 
33 #include <nvif/class.h>
34 
35 #include "nouveau_fbcon.h"
36 #include "dispnv04/hw.h"
37 #include "nouveau_crtc.h"
38 #include "nouveau_dma.h"
39 #include "nouveau_gem.h"
40 #include "nouveau_connector.h"
41 #include "nv50_display.h"
42 
43 #include "nouveau_fence.h"
44 
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_device *dev, unsigned int pipe)
59 {
60 	struct drm_crtc *crtc;
61 	struct nouveau_crtc *nv_crtc;
62 
63 	crtc = drm_crtc_from_index(dev, pipe);
64 	if (!crtc)
65 		return -EINVAL;
66 
67 	nv_crtc = nouveau_crtc(crtc);
68 	nvif_notify_get(&nv_crtc->vblank);
69 
70 	return 0;
71 }
72 
73 void
74 nouveau_display_vblank_disable(struct drm_device *dev, unsigned int pipe)
75 {
76 	struct drm_crtc *crtc;
77 	struct nouveau_crtc *nv_crtc;
78 
79 	crtc = drm_crtc_from_index(dev, pipe);
80 	if (!crtc)
81 		return;
82 
83 	nv_crtc = nouveau_crtc(crtc);
84 	nvif_notify_put(&nv_crtc->vblank);
85 }
86 
87 static inline int
88 calc(int blanks, int blanke, int total, int line)
89 {
90 	if (blanke >= blanks) {
91 		if (line >= blanks)
92 			line -= total;
93 	} else {
94 		if (line >= blanks)
95 			line -= total;
96 		line -= blanke + 1;
97 	}
98 	return line;
99 }
100 
101 static bool
102 nouveau_display_scanoutpos_head(struct drm_crtc *crtc, int *vpos, int *hpos,
103 				ktime_t *stime, ktime_t *etime)
104 {
105 	struct {
106 		struct nv04_disp_mthd_v0 base;
107 		struct nv04_disp_scanoutpos_v0 scan;
108 	} args = {
109 		.base.method = NV04_DISP_SCANOUTPOS,
110 		.base.head = nouveau_crtc(crtc)->index,
111 	};
112 	struct nouveau_display *disp = nouveau_display(crtc->dev);
113 	struct drm_vblank_crtc *vblank = &crtc->dev->vblank[drm_crtc_index(crtc)];
114 	int retry = 20;
115 	bool ret = false;
116 
117 	do {
118 		ret = nvif_mthd(&disp->disp, 0, &args, sizeof(args));
119 		if (ret != 0)
120 			return false;
121 
122 		if (args.scan.vline) {
123 			ret = true;
124 			break;
125 		}
126 
127 		if (retry) ndelay(vblank->linedur_ns);
128 	} while (retry--);
129 
130 	*hpos = args.scan.hline;
131 	*vpos = calc(args.scan.vblanks, args.scan.vblanke,
132 		     args.scan.vtotal, args.scan.vline);
133 	if (stime) *stime = ns_to_ktime(args.scan.time[0]);
134 	if (etime) *etime = ns_to_ktime(args.scan.time[1]);
135 
136 	return ret;
137 }
138 
139 bool
140 nouveau_display_scanoutpos(struct drm_device *dev, unsigned int pipe,
141 			   bool in_vblank_irq, int *vpos, int *hpos,
142 			   ktime_t *stime, ktime_t *etime,
143 			   const struct drm_display_mode *mode)
144 {
145 	struct drm_crtc *crtc;
146 
147 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
148 		if (nouveau_crtc(crtc)->index == pipe) {
149 			return nouveau_display_scanoutpos_head(crtc, vpos, hpos,
150 							       stime, etime);
151 		}
152 	}
153 
154 	return false;
155 }
156 
157 static void
158 nouveau_display_vblank_fini(struct drm_device *dev)
159 {
160 	struct drm_crtc *crtc;
161 
162 	drm_vblank_cleanup(dev);
163 
164 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
165 		struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
166 		nvif_notify_fini(&nv_crtc->vblank);
167 	}
168 }
169 
170 static int
171 nouveau_display_vblank_init(struct drm_device *dev)
172 {
173 	struct nouveau_display *disp = nouveau_display(dev);
174 	struct drm_crtc *crtc;
175 	int ret;
176 
177 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
178 		struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
179 		ret = nvif_notify_init(&disp->disp,
180 				       nouveau_display_vblank_handler, false,
181 				       NV04_DISP_NTFY_VBLANK,
182 				       &(struct nvif_notify_head_req_v0) {
183 					.head = nv_crtc->index,
184 				       },
185 				       sizeof(struct nvif_notify_head_req_v0),
186 				       sizeof(struct nvif_notify_head_rep_v0),
187 				       &nv_crtc->vblank);
188 		if (ret) {
189 			nouveau_display_vblank_fini(dev);
190 			return ret;
191 		}
192 	}
193 
194 	ret = drm_vblank_init(dev, dev->mode_config.num_crtc);
195 	if (ret) {
196 		nouveau_display_vblank_fini(dev);
197 		return ret;
198 	}
199 
200 	return 0;
201 }
202 
203 static void
204 nouveau_user_framebuffer_destroy(struct drm_framebuffer *drm_fb)
205 {
206 	struct nouveau_framebuffer *fb = nouveau_framebuffer(drm_fb);
207 
208 	if (fb->nvbo)
209 		drm_gem_object_unreference_unlocked(&fb->nvbo->gem);
210 
211 	drm_framebuffer_cleanup(drm_fb);
212 	kfree(fb);
213 }
214 
215 static int
216 nouveau_user_framebuffer_create_handle(struct drm_framebuffer *drm_fb,
217 				       struct drm_file *file_priv,
218 				       unsigned int *handle)
219 {
220 	struct nouveau_framebuffer *fb = nouveau_framebuffer(drm_fb);
221 
222 	return drm_gem_handle_create(file_priv, &fb->nvbo->gem, handle);
223 }
224 
225 static const struct drm_framebuffer_funcs nouveau_framebuffer_funcs = {
226 	.destroy = nouveau_user_framebuffer_destroy,
227 	.create_handle = nouveau_user_framebuffer_create_handle,
228 };
229 
230 int
231 nouveau_framebuffer_new(struct drm_device *dev,
232 			const struct drm_mode_fb_cmd2 *mode_cmd,
233 			struct nouveau_bo *nvbo,
234 			struct nouveau_framebuffer **pfb)
235 {
236 	struct nouveau_framebuffer *fb;
237 	int ret;
238 
239 	if (!(fb = *pfb = kzalloc(sizeof(*fb), GFP_KERNEL)))
240 		return -ENOMEM;
241 
242 	drm_helper_mode_fill_fb_struct(dev, &fb->base, mode_cmd);
243 	fb->nvbo = nvbo;
244 
245 	ret = drm_framebuffer_init(dev, &fb->base, &nouveau_framebuffer_funcs);
246 	if (ret)
247 		kfree(fb);
248 	return ret;
249 }
250 
251 struct drm_framebuffer *
252 nouveau_user_framebuffer_create(struct drm_device *dev,
253 				struct drm_file *file_priv,
254 				const struct drm_mode_fb_cmd2 *mode_cmd)
255 {
256 	struct nouveau_framebuffer *fb;
257 	struct nouveau_bo *nvbo;
258 	struct drm_gem_object *gem;
259 	int ret;
260 
261 	gem = drm_gem_object_lookup(file_priv, mode_cmd->handles[0]);
262 	if (!gem)
263 		return ERR_PTR(-ENOENT);
264 	nvbo = nouveau_gem_object(gem);
265 
266 	ret = nouveau_framebuffer_new(dev, mode_cmd, nvbo, &fb);
267 	if (ret == 0)
268 		return &fb->base;
269 
270 	drm_gem_object_unreference_unlocked(gem);
271 	return ERR_PTR(ret);
272 }
273 
274 static const struct drm_mode_config_funcs nouveau_mode_config_funcs = {
275 	.fb_create = nouveau_user_framebuffer_create,
276 	.output_poll_changed = nouveau_fbcon_output_poll_changed,
277 };
278 
279 
280 struct nouveau_drm_prop_enum_list {
281 	u8 gen_mask;
282 	int type;
283 	char *name;
284 };
285 
286 static struct nouveau_drm_prop_enum_list underscan[] = {
287 	{ 6, UNDERSCAN_AUTO, "auto" },
288 	{ 6, UNDERSCAN_OFF, "off" },
289 	{ 6, UNDERSCAN_ON, "on" },
290 	{}
291 };
292 
293 static struct nouveau_drm_prop_enum_list dither_mode[] = {
294 	{ 7, DITHERING_MODE_AUTO, "auto" },
295 	{ 7, DITHERING_MODE_OFF, "off" },
296 	{ 1, DITHERING_MODE_ON, "on" },
297 	{ 6, DITHERING_MODE_STATIC2X2, "static 2x2" },
298 	{ 6, DITHERING_MODE_DYNAMIC2X2, "dynamic 2x2" },
299 	{ 4, DITHERING_MODE_TEMPORAL, "temporal" },
300 	{}
301 };
302 
303 static struct nouveau_drm_prop_enum_list dither_depth[] = {
304 	{ 6, DITHERING_DEPTH_AUTO, "auto" },
305 	{ 6, DITHERING_DEPTH_6BPC, "6 bpc" },
306 	{ 6, DITHERING_DEPTH_8BPC, "8 bpc" },
307 	{}
308 };
309 
310 #define PROP_ENUM(p,gen,n,list) do {                                           \
311 	struct nouveau_drm_prop_enum_list *l = (list);                         \
312 	int c = 0;                                                             \
313 	while (l->gen_mask) {                                                  \
314 		if (l->gen_mask & (1 << (gen)))                                \
315 			c++;                                                   \
316 		l++;                                                           \
317 	}                                                                      \
318 	if (c) {                                                               \
319 		p = drm_property_create(dev, DRM_MODE_PROP_ENUM, n, c);        \
320 		l = (list);                                                    \
321 		c = 0;                                                         \
322 		while (p && l->gen_mask) {                                     \
323 			if (l->gen_mask & (1 << (gen))) {                      \
324 				drm_property_add_enum(p, c, l->type, l->name); \
325 				c++;                                           \
326 			}                                                      \
327 			l++;                                                   \
328 		}                                                              \
329 	}                                                                      \
330 } while(0)
331 
332 static void
333 nouveau_display_hpd_work(struct work_struct *work)
334 {
335 	struct nouveau_drm *drm = container_of(work, typeof(*drm), hpd_work);
336 
337 	pm_runtime_get_sync(drm->dev->dev);
338 
339 	drm_helper_hpd_irq_event(drm->dev);
340 
341 	pm_runtime_mark_last_busy(drm->dev->dev);
342 	pm_runtime_put_sync(drm->dev->dev);
343 }
344 
345 #ifdef CONFIG_ACPI
346 
347 /*
348  * Hans de Goede: This define belongs in acpi/video.h, I've submitted a patch
349  * to the acpi subsys to move it there from drivers/acpi/acpi_video.c .
350  * This should be dropped once that is merged.
351  */
352 #ifndef ACPI_VIDEO_NOTIFY_PROBE
353 #define ACPI_VIDEO_NOTIFY_PROBE			0x81
354 #endif
355 
356 static int
357 nouveau_display_acpi_ntfy(struct notifier_block *nb, unsigned long val,
358 			  void *data)
359 {
360 	struct nouveau_drm *drm = container_of(nb, typeof(*drm), acpi_nb);
361 	struct acpi_bus_event *info = data;
362 
363 	if (!strcmp(info->device_class, ACPI_VIDEO_CLASS)) {
364 		if (info->type == ACPI_VIDEO_NOTIFY_PROBE) {
365 			/*
366 			 * This may be the only indication we receive of a
367 			 * connector hotplug on a runtime suspended GPU,
368 			 * schedule hpd_work to check.
369 			 */
370 			schedule_work(&drm->hpd_work);
371 
372 			/* acpi-video should not generate keypresses for this */
373 			return NOTIFY_BAD;
374 		}
375 	}
376 
377 	return NOTIFY_DONE;
378 }
379 #endif
380 
381 int
382 nouveau_display_init(struct drm_device *dev)
383 {
384 	struct nouveau_display *disp = nouveau_display(dev);
385 	struct nouveau_drm *drm = nouveau_drm(dev);
386 	struct drm_connector *connector;
387 	int ret;
388 
389 	ret = disp->init(dev);
390 	if (ret)
391 		return ret;
392 
393 	/* enable polling for external displays */
394 	if (!dev->mode_config.poll_enabled)
395 		drm_kms_helper_poll_enable(dev);
396 
397 	/* enable hotplug interrupts */
398 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
399 		struct nouveau_connector *conn = nouveau_connector(connector);
400 		nvif_notify_get(&conn->hpd);
401 	}
402 
403 	/* enable flip completion events */
404 	nvif_notify_get(&drm->flip);
405 	return ret;
406 }
407 
408 void
409 nouveau_display_fini(struct drm_device *dev, bool suspend)
410 {
411 	struct nouveau_display *disp = nouveau_display(dev);
412 	struct nouveau_drm *drm = nouveau_drm(dev);
413 	struct drm_connector *connector;
414 	struct drm_crtc *crtc;
415 
416 	if (!suspend) {
417 		if (drm_drv_uses_atomic_modeset(dev))
418 			drm_atomic_helper_shutdown(dev);
419 		else
420 			drm_crtc_force_disable_all(dev);
421 	}
422 
423 	/* Make sure that drm and hw vblank irqs get properly disabled. */
424 	drm_for_each_crtc(crtc, dev)
425 		drm_crtc_vblank_off(crtc);
426 
427 	/* disable flip completion events */
428 	nvif_notify_put(&drm->flip);
429 
430 	/* disable hotplug interrupts */
431 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
432 		struct nouveau_connector *conn = nouveau_connector(connector);
433 		nvif_notify_put(&conn->hpd);
434 	}
435 
436 	drm_kms_helper_poll_disable(dev);
437 	disp->fini(dev);
438 }
439 
440 static void
441 nouveau_display_create_properties(struct drm_device *dev)
442 {
443 	struct nouveau_display *disp = nouveau_display(dev);
444 	int gen;
445 
446 	if (disp->disp.oclass < NV50_DISP)
447 		gen = 0;
448 	else
449 	if (disp->disp.oclass < GF110_DISP)
450 		gen = 1;
451 	else
452 		gen = 2;
453 
454 	PROP_ENUM(disp->dithering_mode, gen, "dithering mode", dither_mode);
455 	PROP_ENUM(disp->dithering_depth, gen, "dithering depth", dither_depth);
456 	PROP_ENUM(disp->underscan_property, gen, "underscan", underscan);
457 
458 	disp->underscan_hborder_property =
459 		drm_property_create_range(dev, 0, "underscan hborder", 0, 128);
460 
461 	disp->underscan_vborder_property =
462 		drm_property_create_range(dev, 0, "underscan vborder", 0, 128);
463 
464 	if (gen < 1)
465 		return;
466 
467 	/* -90..+90 */
468 	disp->vibrant_hue_property =
469 		drm_property_create_range(dev, 0, "vibrant hue", 0, 180);
470 
471 	/* -100..+100 */
472 	disp->color_vibrance_property =
473 		drm_property_create_range(dev, 0, "color vibrance", 0, 200);
474 }
475 
476 int
477 nouveau_display_create(struct drm_device *dev)
478 {
479 	struct nouveau_drm *drm = nouveau_drm(dev);
480 	struct nvkm_device *device = nvxx_device(&drm->client.device);
481 	struct nouveau_display *disp;
482 	int ret;
483 
484 	disp = drm->display = kzalloc(sizeof(*disp), GFP_KERNEL);
485 	if (!disp)
486 		return -ENOMEM;
487 
488 	drm_mode_config_init(dev);
489 	drm_mode_create_scaling_mode_property(dev);
490 	drm_mode_create_dvi_i_properties(dev);
491 
492 	dev->mode_config.funcs = &nouveau_mode_config_funcs;
493 	dev->mode_config.fb_base = device->func->resource_addr(device, 1);
494 
495 	dev->mode_config.min_width = 0;
496 	dev->mode_config.min_height = 0;
497 	if (drm->client.device.info.family < NV_DEVICE_INFO_V0_CELSIUS) {
498 		dev->mode_config.max_width = 2048;
499 		dev->mode_config.max_height = 2048;
500 	} else
501 	if (drm->client.device.info.family < NV_DEVICE_INFO_V0_TESLA) {
502 		dev->mode_config.max_width = 4096;
503 		dev->mode_config.max_height = 4096;
504 	} else
505 	if (drm->client.device.info.family < NV_DEVICE_INFO_V0_FERMI) {
506 		dev->mode_config.max_width = 8192;
507 		dev->mode_config.max_height = 8192;
508 	} else {
509 		dev->mode_config.max_width = 16384;
510 		dev->mode_config.max_height = 16384;
511 	}
512 
513 	dev->mode_config.preferred_depth = 24;
514 	dev->mode_config.prefer_shadow = 1;
515 
516 	if (drm->client.device.info.chipset < 0x11)
517 		dev->mode_config.async_page_flip = false;
518 	else
519 		dev->mode_config.async_page_flip = true;
520 
521 	drm_kms_helper_poll_init(dev);
522 	drm_kms_helper_poll_disable(dev);
523 
524 	if (nouveau_modeset != 2 && drm->vbios.dcb.entries) {
525 		static const u16 oclass[] = {
526 			GP102_DISP,
527 			GP100_DISP,
528 			GM200_DISP,
529 			GM107_DISP,
530 			GK110_DISP,
531 			GK104_DISP,
532 			GF110_DISP,
533 			GT214_DISP,
534 			GT206_DISP,
535 			GT200_DISP,
536 			G82_DISP,
537 			NV50_DISP,
538 			NV04_DISP,
539 		};
540 		int i;
541 
542 		for (i = 0, ret = -ENODEV; ret && i < ARRAY_SIZE(oclass); i++) {
543 			ret = nvif_object_init(&drm->client.device.object, 0,
544 					       oclass[i], NULL, 0, &disp->disp);
545 		}
546 
547 		if (ret == 0) {
548 			nouveau_display_create_properties(dev);
549 			if (disp->disp.oclass < NV50_DISP)
550 				ret = nv04_display_create(dev);
551 			else
552 				ret = nv50_display_create(dev);
553 		}
554 	} else {
555 		ret = 0;
556 	}
557 
558 	if (ret)
559 		goto disp_create_err;
560 
561 	drm_mode_config_reset(dev);
562 
563 	if (dev->mode_config.num_crtc) {
564 		ret = nouveau_display_vblank_init(dev);
565 		if (ret)
566 			goto vblank_err;
567 	}
568 
569 	nouveau_backlight_init(dev);
570 	INIT_WORK(&drm->hpd_work, nouveau_display_hpd_work);
571 #ifdef CONFIG_ACPI
572 	drm->acpi_nb.notifier_call = nouveau_display_acpi_ntfy;
573 	register_acpi_notifier(&drm->acpi_nb);
574 #endif
575 
576 	return 0;
577 
578 vblank_err:
579 	disp->dtor(dev);
580 disp_create_err:
581 	drm_kms_helper_poll_fini(dev);
582 	drm_mode_config_cleanup(dev);
583 	return ret;
584 }
585 
586 void
587 nouveau_display_destroy(struct drm_device *dev)
588 {
589 	struct nouveau_display *disp = nouveau_display(dev);
590 
591 #ifdef CONFIG_ACPI
592 	unregister_acpi_notifier(&nouveau_drm(dev)->acpi_nb);
593 #endif
594 	nouveau_backlight_exit(dev);
595 	nouveau_display_vblank_fini(dev);
596 
597 	drm_kms_helper_poll_fini(dev);
598 	drm_mode_config_cleanup(dev);
599 
600 	if (disp->dtor)
601 		disp->dtor(dev);
602 
603 	nvif_object_fini(&disp->disp);
604 
605 	nouveau_drm(dev)->display = NULL;
606 	kfree(disp);
607 }
608 
609 int
610 nouveau_display_suspend(struct drm_device *dev, bool runtime)
611 {
612 	struct nouveau_display *disp = nouveau_display(dev);
613 	struct drm_crtc *crtc;
614 
615 	if (drm_drv_uses_atomic_modeset(dev)) {
616 		if (!runtime) {
617 			disp->suspend = drm_atomic_helper_suspend(dev);
618 			if (IS_ERR(disp->suspend)) {
619 				int ret = PTR_ERR(disp->suspend);
620 				disp->suspend = NULL;
621 				return ret;
622 			}
623 		}
624 
625 		nouveau_display_fini(dev, true);
626 		return 0;
627 	}
628 
629 	nouveau_display_fini(dev, true);
630 
631 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
632 		struct nouveau_framebuffer *nouveau_fb;
633 
634 		nouveau_fb = nouveau_framebuffer(crtc->primary->fb);
635 		if (!nouveau_fb || !nouveau_fb->nvbo)
636 			continue;
637 
638 		nouveau_bo_unpin(nouveau_fb->nvbo);
639 	}
640 
641 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
642 		struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
643 		if (nv_crtc->cursor.nvbo) {
644 			if (nv_crtc->cursor.set_offset)
645 				nouveau_bo_unmap(nv_crtc->cursor.nvbo);
646 			nouveau_bo_unpin(nv_crtc->cursor.nvbo);
647 		}
648 	}
649 
650 	return 0;
651 }
652 
653 void
654 nouveau_display_resume(struct drm_device *dev, bool runtime)
655 {
656 	struct nouveau_display *disp = nouveau_display(dev);
657 	struct nouveau_drm *drm = nouveau_drm(dev);
658 	struct drm_crtc *crtc;
659 	int ret;
660 
661 	if (drm_drv_uses_atomic_modeset(dev)) {
662 		nouveau_display_init(dev);
663 		if (disp->suspend) {
664 			drm_atomic_helper_resume(dev, disp->suspend);
665 			disp->suspend = NULL;
666 		}
667 		return;
668 	}
669 
670 	/* re-pin fb/cursors */
671 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
672 		struct nouveau_framebuffer *nouveau_fb;
673 
674 		nouveau_fb = nouveau_framebuffer(crtc->primary->fb);
675 		if (!nouveau_fb || !nouveau_fb->nvbo)
676 			continue;
677 
678 		ret = nouveau_bo_pin(nouveau_fb->nvbo, TTM_PL_FLAG_VRAM, true);
679 		if (ret)
680 			NV_ERROR(drm, "Could not pin framebuffer\n");
681 	}
682 
683 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
684 		struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
685 		if (!nv_crtc->cursor.nvbo)
686 			continue;
687 
688 		ret = nouveau_bo_pin(nv_crtc->cursor.nvbo, TTM_PL_FLAG_VRAM, true);
689 		if (!ret && nv_crtc->cursor.set_offset)
690 			ret = nouveau_bo_map(nv_crtc->cursor.nvbo);
691 		if (ret)
692 			NV_ERROR(drm, "Could not pin/map cursor.\n");
693 	}
694 
695 	nouveau_display_init(dev);
696 
697 	/* Force CLUT to get re-loaded during modeset */
698 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
699 		struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
700 
701 		nv_crtc->lut.depth = 0;
702 	}
703 
704 	/* This should ensure we don't hit a locking problem when someone
705 	 * wakes us up via a connector.  We should never go into suspend
706 	 * while the display is on anyways.
707 	 */
708 	if (runtime)
709 		return;
710 
711 	drm_helper_resume_force_mode(dev);
712 
713 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
714 		struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
715 
716 		if (!nv_crtc->cursor.nvbo)
717 			continue;
718 
719 		if (nv_crtc->cursor.set_offset)
720 			nv_crtc->cursor.set_offset(nv_crtc, nv_crtc->cursor.nvbo->bo.offset);
721 		nv_crtc->cursor.set_pos(nv_crtc, nv_crtc->cursor_saved_x,
722 						 nv_crtc->cursor_saved_y);
723 	}
724 }
725 
726 static int
727 nouveau_page_flip_emit(struct nouveau_channel *chan,
728 		       struct nouveau_bo *old_bo,
729 		       struct nouveau_bo *new_bo,
730 		       struct nouveau_page_flip_state *s,
731 		       struct nouveau_fence **pfence)
732 {
733 	struct nouveau_fence_chan *fctx = chan->fence;
734 	struct nouveau_drm *drm = chan->drm;
735 	struct drm_device *dev = drm->dev;
736 	unsigned long flags;
737 	int ret;
738 
739 	/* Queue it to the pending list */
740 	spin_lock_irqsave(&dev->event_lock, flags);
741 	list_add_tail(&s->head, &fctx->flip);
742 	spin_unlock_irqrestore(&dev->event_lock, flags);
743 
744 	/* Synchronize with the old framebuffer */
745 	ret = nouveau_fence_sync(old_bo, chan, false, false);
746 	if (ret)
747 		goto fail;
748 
749 	/* Emit the pageflip */
750 	ret = RING_SPACE(chan, 2);
751 	if (ret)
752 		goto fail;
753 
754 	BEGIN_NV04(chan, NvSubSw, NV_SW_PAGE_FLIP, 1);
755 	OUT_RING  (chan, 0x00000000);
756 	FIRE_RING (chan);
757 
758 	ret = nouveau_fence_new(chan, false, pfence);
759 	if (ret)
760 		goto fail;
761 
762 	return 0;
763 fail:
764 	spin_lock_irqsave(&dev->event_lock, flags);
765 	list_del(&s->head);
766 	spin_unlock_irqrestore(&dev->event_lock, flags);
767 	return ret;
768 }
769 
770 int
771 nouveau_crtc_page_flip(struct drm_crtc *crtc, struct drm_framebuffer *fb,
772 		       struct drm_pending_vblank_event *event, u32 flags,
773 		       struct drm_modeset_acquire_ctx *ctx)
774 {
775 	const int swap_interval = (flags & DRM_MODE_PAGE_FLIP_ASYNC) ? 0 : 1;
776 	struct drm_device *dev = crtc->dev;
777 	struct nouveau_drm *drm = nouveau_drm(dev);
778 	struct nouveau_bo *old_bo = nouveau_framebuffer(crtc->primary->fb)->nvbo;
779 	struct nouveau_bo *new_bo = nouveau_framebuffer(fb)->nvbo;
780 	struct nouveau_page_flip_state *s;
781 	struct nouveau_channel *chan;
782 	struct nouveau_cli *cli;
783 	struct nouveau_fence *fence;
784 	struct nv04_display *dispnv04 = nv04_display(dev);
785 	int head = nouveau_crtc(crtc)->index;
786 	int ret;
787 
788 	chan = drm->channel;
789 	if (!chan)
790 		return -ENODEV;
791 	cli = (void *)chan->user.client;
792 
793 	s = kzalloc(sizeof(*s), GFP_KERNEL);
794 	if (!s)
795 		return -ENOMEM;
796 
797 	if (new_bo != old_bo) {
798 		ret = nouveau_bo_pin(new_bo, TTM_PL_FLAG_VRAM, true);
799 		if (ret)
800 			goto fail_free;
801 	}
802 
803 	mutex_lock(&cli->mutex);
804 	ret = ttm_bo_reserve(&new_bo->bo, true, false, NULL);
805 	if (ret)
806 		goto fail_unpin;
807 
808 	/* synchronise rendering channel with the kernel's channel */
809 	ret = nouveau_fence_sync(new_bo, chan, false, true);
810 	if (ret) {
811 		ttm_bo_unreserve(&new_bo->bo);
812 		goto fail_unpin;
813 	}
814 
815 	if (new_bo != old_bo) {
816 		ttm_bo_unreserve(&new_bo->bo);
817 
818 		ret = ttm_bo_reserve(&old_bo->bo, true, false, NULL);
819 		if (ret)
820 			goto fail_unpin;
821 	}
822 
823 	/* Initialize a page flip struct */
824 	*s = (struct nouveau_page_flip_state)
825 		{ { }, event, crtc, fb->format->cpp[0] * 8, fb->pitches[0],
826 		  new_bo->bo.offset };
827 
828 	/* Keep vblanks on during flip, for the target crtc of this flip */
829 	drm_crtc_vblank_get(crtc);
830 
831 	/* Emit a page flip */
832 	if (swap_interval) {
833 		ret = RING_SPACE(chan, 8);
834 		if (ret)
835 			goto fail_unreserve;
836 
837 		BEGIN_NV04(chan, NvSubImageBlit, 0x012c, 1);
838 		OUT_RING  (chan, 0);
839 		BEGIN_NV04(chan, NvSubImageBlit, 0x0134, 1);
840 		OUT_RING  (chan, head);
841 		BEGIN_NV04(chan, NvSubImageBlit, 0x0100, 1);
842 		OUT_RING  (chan, 0);
843 		BEGIN_NV04(chan, NvSubImageBlit, 0x0130, 1);
844 		OUT_RING  (chan, 0);
845 	}
846 
847 	nouveau_bo_ref(new_bo, &dispnv04->image[head]);
848 
849 	ret = nouveau_page_flip_emit(chan, old_bo, new_bo, s, &fence);
850 	if (ret)
851 		goto fail_unreserve;
852 	mutex_unlock(&cli->mutex);
853 
854 	/* Update the crtc struct and cleanup */
855 	crtc->primary->fb = fb;
856 
857 	nouveau_bo_fence(old_bo, fence, false);
858 	ttm_bo_unreserve(&old_bo->bo);
859 	if (old_bo != new_bo)
860 		nouveau_bo_unpin(old_bo);
861 	nouveau_fence_unref(&fence);
862 	return 0;
863 
864 fail_unreserve:
865 	drm_crtc_vblank_put(crtc);
866 	ttm_bo_unreserve(&old_bo->bo);
867 fail_unpin:
868 	mutex_unlock(&cli->mutex);
869 	if (old_bo != new_bo)
870 		nouveau_bo_unpin(new_bo);
871 fail_free:
872 	kfree(s);
873 	return ret;
874 }
875 
876 int
877 nouveau_finish_page_flip(struct nouveau_channel *chan,
878 			 struct nouveau_page_flip_state *ps)
879 {
880 	struct nouveau_fence_chan *fctx = chan->fence;
881 	struct nouveau_drm *drm = chan->drm;
882 	struct drm_device *dev = drm->dev;
883 	struct nouveau_page_flip_state *s;
884 	unsigned long flags;
885 
886 	spin_lock_irqsave(&dev->event_lock, flags);
887 
888 	if (list_empty(&fctx->flip)) {
889 		NV_ERROR(drm, "unexpected pageflip\n");
890 		spin_unlock_irqrestore(&dev->event_lock, flags);
891 		return -EINVAL;
892 	}
893 
894 	s = list_first_entry(&fctx->flip, struct nouveau_page_flip_state, head);
895 	if (s->event) {
896 		drm_crtc_arm_vblank_event(s->crtc, s->event);
897 	} else {
898 		/* Give up ownership of vblank for page-flipped crtc */
899 		drm_crtc_vblank_put(s->crtc);
900 	}
901 
902 	list_del(&s->head);
903 	if (ps)
904 		*ps = *s;
905 	kfree(s);
906 
907 	spin_unlock_irqrestore(&dev->event_lock, flags);
908 	return 0;
909 }
910 
911 int
912 nouveau_flip_complete(struct nvif_notify *notify)
913 {
914 	struct nouveau_drm *drm = container_of(notify, typeof(*drm), flip);
915 	struct nouveau_channel *chan = drm->channel;
916 	struct nouveau_page_flip_state state;
917 
918 	if (!nouveau_finish_page_flip(chan, &state)) {
919 		nv_set_crtc_base(drm->dev, drm_crtc_index(state.crtc),
920 				 state.offset + state.crtc->y *
921 				 state.pitch + state.crtc->x *
922 				 state.bpp / 8);
923 	}
924 
925 	return NVIF_NOTIFY_KEEP;
926 }
927 
928 int
929 nouveau_display_dumb_create(struct drm_file *file_priv, struct drm_device *dev,
930 			    struct drm_mode_create_dumb *args)
931 {
932 	struct nouveau_cli *cli = nouveau_cli(file_priv);
933 	struct nouveau_bo *bo;
934 	uint32_t domain;
935 	int ret;
936 
937 	args->pitch = roundup(args->width * (args->bpp / 8), 256);
938 	args->size = args->pitch * args->height;
939 	args->size = roundup(args->size, PAGE_SIZE);
940 
941 	/* Use VRAM if there is any ; otherwise fallback to system memory */
942 	if (nouveau_drm(dev)->client.device.info.ram_size != 0)
943 		domain = NOUVEAU_GEM_DOMAIN_VRAM;
944 	else
945 		domain = NOUVEAU_GEM_DOMAIN_GART;
946 
947 	ret = nouveau_gem_new(cli, args->size, 0, domain, 0, 0, &bo);
948 	if (ret)
949 		return ret;
950 
951 	ret = drm_gem_handle_create(file_priv, &bo->gem, &args->handle);
952 	drm_gem_object_unreference_unlocked(&bo->gem);
953 	return ret;
954 }
955 
956 int
957 nouveau_display_dumb_map_offset(struct drm_file *file_priv,
958 				struct drm_device *dev,
959 				uint32_t handle, uint64_t *poffset)
960 {
961 	struct drm_gem_object *gem;
962 
963 	gem = drm_gem_object_lookup(file_priv, handle);
964 	if (gem) {
965 		struct nouveau_bo *bo = nouveau_gem_object(gem);
966 		*poffset = drm_vma_node_offset_addr(&bo->bo.vma_node);
967 		drm_gem_object_unreference_unlocked(gem);
968 		return 0;
969 	}
970 
971 	return -ENOENT;
972 }
973