1 /*	$NetBSD: nouveau_display.c,v 1.5 2021/12/18 23:45:32 riastradh Exp $	*/
2 
3 /*
4  * Copyright (C) 2008 Maarten Maathuis.
5  * All Rights Reserved.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining
8  * a copy of this software and associated documentation files (the
9  * "Software"), to deal in the Software without restriction, including
10  * without limitation the rights to use, copy, modify, merge, publish,
11  * distribute, sublicense, and/or sell copies of the Software, and to
12  * permit persons to whom the Software is furnished to do so, subject to
13  * the following conditions:
14  *
15  * The above copyright notice and this permission notice (including the
16  * next paragraph) shall be included in all copies or substantial
17  * portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22  * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
23  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26  *
27  */
28 
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: nouveau_display.c,v 1.5 2021/12/18 23:45:32 riastradh Exp $");
31 
32 #include <acpi/video.h>
33 
34 #include <drm/drm_atomic.h>
35 #include <drm/drm_atomic_helper.h>
36 #include <drm/drm_crtc_helper.h>
37 #include <drm/drm_fb_helper.h>
38 #include <drm/drm_fourcc.h>
39 #include <drm/drm_probe_helper.h>
40 #include <drm/drm_vblank.h>
41 
42 #include "nouveau_fbcon.h"
43 #include "nouveau_crtc.h"
44 #include "nouveau_gem.h"
45 #include "nouveau_connector.h"
46 #include "nv50_display.h"
47 
48 #include <nvif/class.h>
49 #include <nvif/cl0046.h>
50 #include <nvif/event.h>
51 
52 static int
nouveau_display_vblank_handler(struct nvif_notify * notify)53 nouveau_display_vblank_handler(struct nvif_notify *notify)
54 {
55 	struct nouveau_crtc *nv_crtc =
56 		container_of(notify, typeof(*nv_crtc), vblank);
57 	drm_crtc_handle_vblank(&nv_crtc->base);
58 	return NVIF_NOTIFY_KEEP;
59 }
60 
61 int
nouveau_display_vblank_enable(struct drm_device * dev,unsigned int pipe)62 nouveau_display_vblank_enable(struct drm_device *dev, unsigned int pipe)
63 {
64 	struct drm_crtc *crtc;
65 	struct nouveau_crtc *nv_crtc;
66 
67 	crtc = drm_crtc_from_index(dev, pipe);
68 	if (!crtc)
69 		return -EINVAL;
70 
71 	nv_crtc = nouveau_crtc(crtc);
72 	nvif_notify_get(&nv_crtc->vblank);
73 
74 	return 0;
75 }
76 
77 void
nouveau_display_vblank_disable(struct drm_device * dev,unsigned int pipe)78 nouveau_display_vblank_disable(struct drm_device *dev, unsigned int pipe)
79 {
80 	struct drm_crtc *crtc;
81 	struct nouveau_crtc *nv_crtc;
82 
83 	crtc = drm_crtc_from_index(dev, pipe);
84 	if (!crtc)
85 		return;
86 
87 	nv_crtc = nouveau_crtc(crtc);
88 	nvif_notify_put(&nv_crtc->vblank);
89 }
90 
91 static inline int
calc(int blanks,int blanke,int total,int line)92 calc(int blanks, int blanke, int total, int line)
93 {
94 	if (blanke >= blanks) {
95 		if (line >= blanks)
96 			line -= total;
97 	} else {
98 		if (line >= blanks)
99 			line -= total;
100 		line -= blanke + 1;
101 	}
102 	return line;
103 }
104 
105 static bool
nouveau_display_scanoutpos_head(struct drm_crtc * crtc,int * vpos,int * hpos,ktime_t * stime,ktime_t * etime)106 nouveau_display_scanoutpos_head(struct drm_crtc *crtc, int *vpos, int *hpos,
107 				ktime_t *stime, ktime_t *etime)
108 {
109 	struct {
110 		struct nv04_disp_mthd_v0 base;
111 		struct nv04_disp_scanoutpos_v0 scan;
112 	} args = {
113 		.base.method = NV04_DISP_SCANOUTPOS,
114 		.base.head = nouveau_crtc(crtc)->index,
115 	};
116 	struct nouveau_display *disp = nouveau_display(crtc->dev);
117 	struct drm_vblank_crtc *vblank = &crtc->dev->vblank[drm_crtc_index(crtc)];
118 	int retry = 20;
119 	bool ret = false;
120 
121 	do {
122 		ret = nvif_mthd(&disp->disp.object, 0, &args, sizeof(args));
123 		if (ret != 0)
124 			return false;
125 
126 		if (args.scan.vline) {
127 			ret = true;
128 			break;
129 		}
130 
131 		if (retry) ndelay(vblank->linedur_ns);
132 	} while (retry--);
133 
134 	*hpos = args.scan.hline;
135 	*vpos = calc(args.scan.vblanks, args.scan.vblanke,
136 		     args.scan.vtotal, args.scan.vline);
137 	if (stime) *stime = ns_to_ktime(args.scan.time[0]);
138 	if (etime) *etime = ns_to_ktime(args.scan.time[1]);
139 
140 	return ret;
141 }
142 
143 bool
nouveau_display_scanoutpos(struct drm_device * dev,unsigned int pipe,bool in_vblank_irq,int * vpos,int * hpos,ktime_t * stime,ktime_t * etime,const struct drm_display_mode * mode)144 nouveau_display_scanoutpos(struct drm_device *dev, unsigned int pipe,
145 			   bool in_vblank_irq, int *vpos, int *hpos,
146 			   ktime_t *stime, ktime_t *etime,
147 			   const struct drm_display_mode *mode)
148 {
149 	struct drm_crtc *crtc;
150 
151 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
152 		if (nouveau_crtc(crtc)->index == pipe) {
153 			return nouveau_display_scanoutpos_head(crtc, vpos, hpos,
154 							       stime, etime);
155 		}
156 	}
157 
158 	return false;
159 }
160 
161 static void
nouveau_display_vblank_fini(struct drm_device * dev)162 nouveau_display_vblank_fini(struct drm_device *dev)
163 {
164 	struct drm_crtc *crtc;
165 
166 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
167 		struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
168 		nvif_notify_fini(&nv_crtc->vblank);
169 	}
170 }
171 
172 static int
nouveau_display_vblank_init(struct drm_device * dev)173 nouveau_display_vblank_init(struct drm_device *dev)
174 {
175 	struct nouveau_display *disp = nouveau_display(dev);
176 	struct drm_crtc *crtc;
177 	int ret;
178 
179 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
180 		struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
181 		ret = nvif_notify_init(&disp->disp.object,
182 				       nouveau_display_vblank_handler, false,
183 				       NV04_DISP_NTFY_VBLANK,
184 				       &(struct nvif_notify_head_req_v0) {
185 					.head = nv_crtc->index,
186 				       },
187 				       sizeof(struct nvif_notify_head_req_v0),
188 				       sizeof(struct nvif_notify_head_rep_v0),
189 				       &nv_crtc->vblank);
190 		if (ret) {
191 			nouveau_display_vblank_fini(dev);
192 			return ret;
193 		}
194 	}
195 
196 	ret = drm_vblank_init(dev, dev->mode_config.num_crtc);
197 	if (ret) {
198 		nouveau_display_vblank_fini(dev);
199 		return ret;
200 	}
201 
202 	return 0;
203 }
204 
205 static void
nouveau_user_framebuffer_destroy(struct drm_framebuffer * drm_fb)206 nouveau_user_framebuffer_destroy(struct drm_framebuffer *drm_fb)
207 {
208 	struct nouveau_framebuffer *fb = nouveau_framebuffer(drm_fb);
209 
210 	if (fb->nvbo)
211 		drm_gem_object_put_unlocked(&fb->nvbo->bo.base);
212 
213 	drm_framebuffer_cleanup(drm_fb);
214 	kfree(fb);
215 }
216 
217 static int
nouveau_user_framebuffer_create_handle(struct drm_framebuffer * drm_fb,struct drm_file * file_priv,unsigned int * handle)218 nouveau_user_framebuffer_create_handle(struct drm_framebuffer *drm_fb,
219 				       struct drm_file *file_priv,
220 				       unsigned int *handle)
221 {
222 	struct nouveau_framebuffer *fb = nouveau_framebuffer(drm_fb);
223 
224 	return drm_gem_handle_create(file_priv, &fb->nvbo->bo.base, handle);
225 }
226 
227 static const struct drm_framebuffer_funcs nouveau_framebuffer_funcs = {
228 	.destroy = nouveau_user_framebuffer_destroy,
229 	.create_handle = nouveau_user_framebuffer_create_handle,
230 };
231 
232 int
nouveau_framebuffer_new(struct drm_device * dev,const struct drm_mode_fb_cmd2 * mode_cmd,struct nouveau_bo * nvbo,struct nouveau_framebuffer ** pfb)233 nouveau_framebuffer_new(struct drm_device *dev,
234 			const struct drm_mode_fb_cmd2 *mode_cmd,
235 			struct nouveau_bo *nvbo,
236 			struct nouveau_framebuffer **pfb)
237 {
238 	struct nouveau_drm *drm = nouveau_drm(dev);
239 	struct nouveau_framebuffer *fb;
240 	int ret;
241 
242         /* YUV overlays have special requirements pre-NV50 */
243 	if (drm->client.device.info.family < NV_DEVICE_INFO_V0_TESLA &&
244 
245 	    (mode_cmd->pixel_format == DRM_FORMAT_YUYV ||
246 	     mode_cmd->pixel_format == DRM_FORMAT_UYVY ||
247 	     mode_cmd->pixel_format == DRM_FORMAT_NV12 ||
248 	     mode_cmd->pixel_format == DRM_FORMAT_NV21) &&
249 	    (mode_cmd->pitches[0] & 0x3f || /* align 64 */
250 	     mode_cmd->pitches[0] >= 0x10000 || /* at most 64k pitch */
251 	     (mode_cmd->pitches[1] && /* pitches for planes must match */
252 	      mode_cmd->pitches[0] != mode_cmd->pitches[1]))) {
253 		struct drm_format_name_buf format_name;
254 		DRM_DEBUG_KMS("Unsuitable framebuffer: format: %s; pitches: 0x%x\n 0x%x\n",
255 			      drm_get_format_name(mode_cmd->pixel_format,
256 						  &format_name),
257 			      mode_cmd->pitches[0],
258 			      mode_cmd->pitches[1]);
259 		return -EINVAL;
260 	}
261 
262 	if (!(fb = *pfb = kzalloc(sizeof(*fb), GFP_KERNEL)))
263 		return -ENOMEM;
264 
265 	drm_helper_mode_fill_fb_struct(dev, &fb->base, mode_cmd);
266 	fb->nvbo = nvbo;
267 
268 	ret = drm_framebuffer_init(dev, &fb->base, &nouveau_framebuffer_funcs);
269 	if (ret)
270 		kfree(fb);
271 	return ret;
272 }
273 
274 struct drm_framebuffer *
nouveau_user_framebuffer_create(struct drm_device * dev,struct drm_file * file_priv,const struct drm_mode_fb_cmd2 * mode_cmd)275 nouveau_user_framebuffer_create(struct drm_device *dev,
276 				struct drm_file *file_priv,
277 				const struct drm_mode_fb_cmd2 *mode_cmd)
278 {
279 	struct nouveau_framebuffer *fb;
280 	struct nouveau_bo *nvbo;
281 	struct drm_gem_object *gem;
282 	int ret;
283 
284 	gem = drm_gem_object_lookup(file_priv, mode_cmd->handles[0]);
285 	if (!gem)
286 		return ERR_PTR(-ENOENT);
287 	nvbo = nouveau_gem_object(gem);
288 
289 	ret = nouveau_framebuffer_new(dev, mode_cmd, nvbo, &fb);
290 	if (ret == 0)
291 		return &fb->base;
292 
293 	drm_gem_object_put_unlocked(gem);
294 	return ERR_PTR(ret);
295 }
296 
297 static const struct drm_mode_config_funcs nouveau_mode_config_funcs = {
298 	.fb_create = nouveau_user_framebuffer_create,
299 	.output_poll_changed = nouveau_fbcon_output_poll_changed,
300 };
301 
302 
303 struct nouveau_drm_prop_enum_list {
304 	u8 gen_mask;
305 	int type;
306 	const char *name;
307 };
308 
309 static struct nouveau_drm_prop_enum_list underscan[] = {
310 	{ 6, UNDERSCAN_AUTO, "auto" },
311 	{ 6, UNDERSCAN_OFF, "off" },
312 	{ 6, UNDERSCAN_ON, "on" },
313 	{}
314 };
315 
316 static struct nouveau_drm_prop_enum_list dither_mode[] = {
317 	{ 7, DITHERING_MODE_AUTO, "auto" },
318 	{ 7, DITHERING_MODE_OFF, "off" },
319 	{ 1, DITHERING_MODE_ON, "on" },
320 	{ 6, DITHERING_MODE_STATIC2X2, "static 2x2" },
321 	{ 6, DITHERING_MODE_DYNAMIC2X2, "dynamic 2x2" },
322 	{ 4, DITHERING_MODE_TEMPORAL, "temporal" },
323 	{}
324 };
325 
326 static struct nouveau_drm_prop_enum_list dither_depth[] = {
327 	{ 6, DITHERING_DEPTH_AUTO, "auto" },
328 	{ 6, DITHERING_DEPTH_6BPC, "6 bpc" },
329 	{ 6, DITHERING_DEPTH_8BPC, "8 bpc" },
330 	{}
331 };
332 
333 #define PROP_ENUM(p,gen,n,list) do {                                           \
334 	struct nouveau_drm_prop_enum_list *l = (list);                         \
335 	int c = 0;                                                             \
336 	while (l->gen_mask) {                                                  \
337 		if (l->gen_mask & (1 << (gen)))                                \
338 			c++;                                                   \
339 		l++;                                                           \
340 	}                                                                      \
341 	if (c) {                                                               \
342 		p = drm_property_create(dev, DRM_MODE_PROP_ENUM, n, c);        \
343 		l = (list);                                                    \
344 		while (p && l->gen_mask) {                                     \
345 			if (l->gen_mask & (1 << (gen))) {                      \
346 				drm_property_add_enum(p, l->type, l->name);    \
347 			}                                                      \
348 			l++;                                                   \
349 		}                                                              \
350 	}                                                                      \
351 } while(0)
352 
353 static void
nouveau_display_hpd_work(struct work_struct * work)354 nouveau_display_hpd_work(struct work_struct *work)
355 {
356 	struct nouveau_drm *drm = container_of(work, typeof(*drm), hpd_work);
357 
358 	pm_runtime_get_sync(drm->dev->dev);
359 
360 	drm_helper_hpd_irq_event(drm->dev);
361 
362 	pm_runtime_mark_last_busy(drm->dev->dev);
363 	pm_runtime_put_sync(drm->dev->dev);
364 }
365 
366 #ifdef CONFIG_ACPI
367 
368 static int
nouveau_display_acpi_ntfy(struct notifier_block * nb,unsigned long val,void * data)369 nouveau_display_acpi_ntfy(struct notifier_block *nb, unsigned long val,
370 			  void *data)
371 {
372 	struct nouveau_drm *drm = container_of(nb, typeof(*drm), acpi_nb);
373 	struct acpi_bus_event *info = data;
374 	int ret;
375 
376 	if (!strcmp(info->device_class, ACPI_VIDEO_CLASS)) {
377 		if (info->type == ACPI_VIDEO_NOTIFY_PROBE) {
378 			ret = pm_runtime_get(drm->dev->dev);
379 			if (ret == 1 || ret == -EACCES) {
380 				/* If the GPU is already awake, or in a state
381 				 * where we can't wake it up, it can handle
382 				 * it's own hotplug events.
383 				 */
384 				pm_runtime_put_autosuspend(drm->dev->dev);
385 			} else if (ret == 0) {
386 				/* This may be the only indication we receive
387 				 * of a connector hotplug on a runtime
388 				 * suspended GPU, schedule hpd_work to check.
389 				 */
390 				NV_DEBUG(drm, "ACPI requested connector reprobe\n");
391 				schedule_work(&drm->hpd_work);
392 				pm_runtime_put_noidle(drm->dev->dev);
393 			} else {
394 				NV_WARN(drm, "Dropped ACPI reprobe event due to RPM error: %d\n",
395 					ret);
396 			}
397 
398 			/* acpi-video should not generate keypresses for this */
399 			return NOTIFY_BAD;
400 		}
401 	}
402 
403 	return NOTIFY_DONE;
404 }
405 #endif
406 
407 int
nouveau_display_init(struct drm_device * dev,bool resume,bool runtime)408 nouveau_display_init(struct drm_device *dev, bool resume, bool runtime)
409 {
410 	struct nouveau_display *disp = nouveau_display(dev);
411 	struct drm_connector *connector;
412 	struct drm_connector_list_iter conn_iter;
413 	int ret;
414 
415 	/*
416 	 * Enable hotplug interrupts (done as early as possible, since we need
417 	 * them for MST)
418 	 */
419 	drm_connector_list_iter_begin(dev, &conn_iter);
420 	nouveau_for_each_non_mst_connector_iter(connector, &conn_iter) {
421 		struct nouveau_connector *conn = nouveau_connector(connector);
422 		nvif_notify_get(&conn->hpd);
423 	}
424 	drm_connector_list_iter_end(&conn_iter);
425 
426 	ret = disp->init(dev, resume, runtime);
427 	if (ret)
428 		return ret;
429 
430 	/* enable connector detection and polling for connectors without HPD
431 	 * support
432 	 */
433 	drm_kms_helper_poll_enable(dev);
434 
435 	return ret;
436 }
437 
438 void
nouveau_display_fini(struct drm_device * dev,bool suspend,bool runtime)439 nouveau_display_fini(struct drm_device *dev, bool suspend, bool runtime)
440 {
441 	struct nouveau_display *disp = nouveau_display(dev);
442 	struct nouveau_drm *drm = nouveau_drm(dev);
443 	struct drm_connector *connector;
444 	struct drm_connector_list_iter conn_iter;
445 
446 	if (!suspend) {
447 		if (drm_drv_uses_atomic_modeset(dev))
448 			drm_atomic_helper_shutdown(dev);
449 		else
450 			drm_helper_force_disable_all(dev);
451 	}
452 
453 	/* disable hotplug interrupts */
454 	drm_connector_list_iter_begin(dev, &conn_iter);
455 	nouveau_for_each_non_mst_connector_iter(connector, &conn_iter) {
456 		struct nouveau_connector *conn = nouveau_connector(connector);
457 		nvif_notify_put(&conn->hpd);
458 	}
459 	drm_connector_list_iter_end(&conn_iter);
460 
461 	if (!runtime)
462 		cancel_work_sync(&drm->hpd_work);
463 
464 	drm_kms_helper_poll_disable(dev);
465 	disp->fini(dev, suspend);
466 }
467 
468 static void
nouveau_display_create_properties(struct drm_device * dev)469 nouveau_display_create_properties(struct drm_device *dev)
470 {
471 	struct nouveau_display *disp = nouveau_display(dev);
472 	int gen;
473 
474 	if (disp->disp.object.oclass < NV50_DISP)
475 		gen = 0;
476 	else
477 	if (disp->disp.object.oclass < GF110_DISP)
478 		gen = 1;
479 	else
480 		gen = 2;
481 
482 	PROP_ENUM(disp->dithering_mode, gen, "dithering mode", dither_mode);
483 	PROP_ENUM(disp->dithering_depth, gen, "dithering depth", dither_depth);
484 	PROP_ENUM(disp->underscan_property, gen, "underscan", underscan);
485 
486 	disp->underscan_hborder_property =
487 		drm_property_create_range(dev, 0, "underscan hborder", 0, 128);
488 
489 	disp->underscan_vborder_property =
490 		drm_property_create_range(dev, 0, "underscan vborder", 0, 128);
491 
492 	if (gen < 1)
493 		return;
494 
495 	/* -90..+90 */
496 	disp->vibrant_hue_property =
497 		drm_property_create_range(dev, 0, "vibrant hue", 0, 180);
498 
499 	/* -100..+100 */
500 	disp->color_vibrance_property =
501 		drm_property_create_range(dev, 0, "color vibrance", 0, 200);
502 }
503 
504 int
nouveau_display_create(struct drm_device * dev)505 nouveau_display_create(struct drm_device *dev)
506 {
507 	struct nouveau_drm *drm = nouveau_drm(dev);
508 	struct nvkm_device *device = nvxx_device(&drm->client.device);
509 	struct nouveau_display *disp;
510 	int ret;
511 
512 	disp = drm->display = kzalloc(sizeof(*disp), GFP_KERNEL);
513 	if (!disp)
514 		return -ENOMEM;
515 
516 	drm_mode_config_init(dev);
517 	drm_mode_create_scaling_mode_property(dev);
518 	drm_mode_create_dvi_i_properties(dev);
519 
520 	dev->mode_config.funcs = &nouveau_mode_config_funcs;
521 	dev->mode_config.fb_base = device->func->resource_addr(device, 1);
522 
523 	dev->mode_config.min_width = 0;
524 	dev->mode_config.min_height = 0;
525 	if (drm->client.device.info.family < NV_DEVICE_INFO_V0_CELSIUS) {
526 		dev->mode_config.max_width = 2048;
527 		dev->mode_config.max_height = 2048;
528 	} else
529 	if (drm->client.device.info.family < NV_DEVICE_INFO_V0_TESLA) {
530 		dev->mode_config.max_width = 4096;
531 		dev->mode_config.max_height = 4096;
532 	} else
533 	if (drm->client.device.info.family < NV_DEVICE_INFO_V0_FERMI) {
534 		dev->mode_config.max_width = 8192;
535 		dev->mode_config.max_height = 8192;
536 	} else {
537 		dev->mode_config.max_width = 16384;
538 		dev->mode_config.max_height = 16384;
539 	}
540 
541 	dev->mode_config.preferred_depth = 24;
542 	dev->mode_config.prefer_shadow = 1;
543 
544 	if (drm->client.device.info.chipset < 0x11)
545 		dev->mode_config.async_page_flip = false;
546 	else
547 		dev->mode_config.async_page_flip = true;
548 
549 	drm_kms_helper_poll_init(dev);
550 	drm_kms_helper_poll_disable(dev);
551 
552 	if (nouveau_modeset != 2 && drm->vbios.dcb.entries) {
553 		ret = nvif_disp_ctor(&drm->client.device, 0, &disp->disp);
554 		if (ret == 0) {
555 			nouveau_display_create_properties(dev);
556 			if (disp->disp.object.oclass < NV50_DISP)
557 				ret = nv04_display_create(dev);
558 			else
559 				ret = nv50_display_create(dev);
560 		}
561 	} else {
562 		ret = 0;
563 	}
564 
565 	if (ret)
566 		goto disp_create_err;
567 
568 	drm_mode_config_reset(dev);
569 
570 	if (dev->mode_config.num_crtc) {
571 		ret = nouveau_display_vblank_init(dev);
572 		if (ret)
573 			goto vblank_err;
574 	}
575 
576 	INIT_WORK(&drm->hpd_work, nouveau_display_hpd_work);
577 #ifdef CONFIG_ACPI
578 	drm->acpi_nb.notifier_call = nouveau_display_acpi_ntfy;
579 	register_acpi_notifier(&drm->acpi_nb);
580 #endif
581 
582 	return 0;
583 
584 vblank_err:
585 	disp->dtor(dev);
586 disp_create_err:
587 	drm_kms_helper_poll_fini(dev);
588 	drm_mode_config_cleanup(dev);
589 	return ret;
590 }
591 
592 void
nouveau_display_destroy(struct drm_device * dev)593 nouveau_display_destroy(struct drm_device *dev)
594 {
595 	struct nouveau_display *disp = nouveau_display(dev);
596 
597 #ifdef CONFIG_ACPI
598 	unregister_acpi_notifier(&nouveau_drm(dev)->acpi_nb);
599 #endif
600 	nouveau_display_vblank_fini(dev);
601 
602 	drm_kms_helper_poll_fini(dev);
603 	drm_mode_config_cleanup(dev);
604 
605 	if (disp->dtor)
606 		disp->dtor(dev);
607 
608 	nvif_disp_dtor(&disp->disp);
609 
610 	nouveau_drm(dev)->display = NULL;
611 	kfree(disp);
612 }
613 
614 int
nouveau_display_suspend(struct drm_device * dev,bool runtime)615 nouveau_display_suspend(struct drm_device *dev, bool runtime)
616 {
617 	struct nouveau_display *disp = nouveau_display(dev);
618 
619 	if (drm_drv_uses_atomic_modeset(dev)) {
620 		if (!runtime) {
621 			disp->suspend = drm_atomic_helper_suspend(dev);
622 			if (IS_ERR(disp->suspend)) {
623 				int ret = PTR_ERR(disp->suspend);
624 				disp->suspend = NULL;
625 				return ret;
626 			}
627 		}
628 	}
629 
630 	nouveau_display_fini(dev, true, runtime);
631 	return 0;
632 }
633 
634 void
nouveau_display_resume(struct drm_device * dev,bool runtime)635 nouveau_display_resume(struct drm_device *dev, bool runtime)
636 {
637 	struct nouveau_display *disp = nouveau_display(dev);
638 
639 	nouveau_display_init(dev, true, runtime);
640 
641 	if (drm_drv_uses_atomic_modeset(dev)) {
642 		if (disp->suspend) {
643 			drm_atomic_helper_resume(dev, disp->suspend);
644 			disp->suspend = NULL;
645 		}
646 		return;
647 	}
648 }
649 
650 int
nouveau_display_dumb_create(struct drm_file * file_priv,struct drm_device * dev,struct drm_mode_create_dumb * args)651 nouveau_display_dumb_create(struct drm_file *file_priv, struct drm_device *dev,
652 			    struct drm_mode_create_dumb *args)
653 {
654 	struct nouveau_cli *cli = nouveau_cli(file_priv);
655 	struct nouveau_bo *bo;
656 	uint32_t domain;
657 	int ret;
658 
659 	args->pitch = roundup(args->width * (args->bpp / 8), 256);
660 	args->size = args->pitch * args->height;
661 	args->size = roundup(args->size, PAGE_SIZE);
662 
663 	/* Use VRAM if there is any ; otherwise fallback to system memory */
664 	if (nouveau_drm(dev)->client.device.info.ram_size != 0)
665 		domain = NOUVEAU_GEM_DOMAIN_VRAM;
666 	else
667 		domain = NOUVEAU_GEM_DOMAIN_GART;
668 
669 	ret = nouveau_gem_new(cli, args->size, 0, domain, 0, 0, &bo);
670 	if (ret)
671 		return ret;
672 
673 	ret = drm_gem_handle_create(file_priv, &bo->bo.base, &args->handle);
674 	drm_gem_object_put_unlocked(&bo->bo.base);
675 	return ret;
676 }
677 
678 int
nouveau_display_dumb_map_offset(struct drm_file * file_priv,struct drm_device * dev,uint32_t handle,uint64_t * poffset)679 nouveau_display_dumb_map_offset(struct drm_file *file_priv,
680 				struct drm_device *dev,
681 				uint32_t handle, uint64_t *poffset)
682 {
683 	struct drm_gem_object *gem;
684 
685 	gem = drm_gem_object_lookup(file_priv, handle);
686 	if (gem) {
687 		struct nouveau_bo *bo = nouveau_gem_object(gem);
688 		*poffset = drm_vma_node_offset_addr(&bo->bo.base.vma_node);
689 		drm_gem_object_put_unlocked(gem);
690 		return 0;
691 	}
692 
693 	return -ENOENT;
694 }
695