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