1 /*
2  * Copyright 2012 Red Hat Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * Authors: Ben Skeggs
23  */
24 
25 #include <linux/delay.h>
26 #include <linux/module.h>
27 #include <linux/pci.h>
28 #include <linux/pm_runtime.h>
29 #include <linux/vga_switcheroo.h>
30 #include <linux/mmu_notifier.h>
31 #include <linux/dynamic_debug.h>
32 
33 #include <drm/drm_aperture.h>
34 #include <drm/drm_crtc_helper.h>
35 #include <drm/drm_drv.h>
36 #include <drm/drm_fb_helper.h>
37 #include <drm/drm_fbdev_generic.h>
38 #include <drm/drm_gem_ttm_helper.h>
39 #include <drm/drm_ioctl.h>
40 #include <drm/drm_vblank.h>
41 
42 #include <core/gpuobj.h>
43 #include <core/option.h>
44 #include <core/pci.h>
45 #include <core/tegra.h>
46 
47 #include <nvif/driver.h>
48 #include <nvif/fifo.h>
49 #include <nvif/push006c.h>
50 #include <nvif/user.h>
51 
52 #include <nvif/class.h>
53 #include <nvif/cl0002.h>
54 
55 #include "nouveau_drv.h"
56 #include "nouveau_dma.h"
57 #include "nouveau_ttm.h"
58 #include "nouveau_gem.h"
59 #include "nouveau_vga.h"
60 #include "nouveau_led.h"
61 #include "nouveau_hwmon.h"
62 #include "nouveau_acpi.h"
63 #include "nouveau_bios.h"
64 #include "nouveau_ioctl.h"
65 #include "nouveau_abi16.h"
66 #include "nouveau_fence.h"
67 #include "nouveau_debugfs.h"
68 #include "nouveau_usif.h"
69 #include "nouveau_connector.h"
70 #include "nouveau_platform.h"
71 #include "nouveau_svm.h"
72 #include "nouveau_dmem.h"
73 
74 DECLARE_DYNDBG_CLASSMAP(drm_debug_classes, DD_CLASS_TYPE_DISJOINT_BITS, 0,
75 			"DRM_UT_CORE",
76 			"DRM_UT_DRIVER",
77 			"DRM_UT_KMS",
78 			"DRM_UT_PRIME",
79 			"DRM_UT_ATOMIC",
80 			"DRM_UT_VBL",
81 			"DRM_UT_STATE",
82 			"DRM_UT_LEASE",
83 			"DRM_UT_DP",
84 			"DRM_UT_DRMRES");
85 
86 MODULE_PARM_DESC(config, "option string to pass to driver core");
87 static char *nouveau_config;
88 module_param_named(config, nouveau_config, charp, 0400);
89 
90 MODULE_PARM_DESC(debug, "debug string to pass to driver core");
91 static char *nouveau_debug;
92 module_param_named(debug, nouveau_debug, charp, 0400);
93 
94 MODULE_PARM_DESC(noaccel, "disable kernel/abi16 acceleration");
95 static int nouveau_noaccel = 0;
96 module_param_named(noaccel, nouveau_noaccel, int, 0400);
97 
98 MODULE_PARM_DESC(modeset, "enable driver (default: auto, "
99 		          "0 = disabled, 1 = enabled, 2 = headless)");
100 int nouveau_modeset = -1;
101 module_param_named(modeset, nouveau_modeset, int, 0400);
102 
103 MODULE_PARM_DESC(atomic, "Expose atomic ioctl (default: disabled)");
104 static int nouveau_atomic = 0;
105 module_param_named(atomic, nouveau_atomic, int, 0400);
106 
107 MODULE_PARM_DESC(runpm, "disable (0), force enable (1), optimus only default (-1)");
108 static int nouveau_runtime_pm = -1;
109 module_param_named(runpm, nouveau_runtime_pm, int, 0400);
110 
111 static struct drm_driver driver_stub;
112 static struct drm_driver driver_pci;
113 static struct drm_driver driver_platform;
114 
115 static u64
116 nouveau_pci_name(struct pci_dev *pdev)
117 {
118 	u64 name = (u64)pci_domain_nr(pdev->bus) << 32;
119 	name |= pdev->bus->number << 16;
120 	name |= PCI_SLOT(pdev->devfn) << 8;
121 	return name | PCI_FUNC(pdev->devfn);
122 }
123 
124 static u64
125 nouveau_platform_name(struct platform_device *platformdev)
126 {
127 	return platformdev->id;
128 }
129 
130 static u64
131 nouveau_name(struct drm_device *dev)
132 {
133 	if (dev_is_pci(dev->dev))
134 		return nouveau_pci_name(to_pci_dev(dev->dev));
135 	else
136 		return nouveau_platform_name(to_platform_device(dev->dev));
137 }
138 
139 static inline bool
140 nouveau_cli_work_ready(struct dma_fence *fence)
141 {
142 	if (!dma_fence_is_signaled(fence))
143 		return false;
144 	dma_fence_put(fence);
145 	return true;
146 }
147 
148 static void
149 nouveau_cli_work(struct work_struct *w)
150 {
151 	struct nouveau_cli *cli = container_of(w, typeof(*cli), work);
152 	struct nouveau_cli_work *work, *wtmp;
153 	mutex_lock(&cli->lock);
154 	list_for_each_entry_safe(work, wtmp, &cli->worker, head) {
155 		if (!work->fence || nouveau_cli_work_ready(work->fence)) {
156 			list_del(&work->head);
157 			work->func(work);
158 		}
159 	}
160 	mutex_unlock(&cli->lock);
161 }
162 
163 static void
164 nouveau_cli_work_fence(struct dma_fence *fence, struct dma_fence_cb *cb)
165 {
166 	struct nouveau_cli_work *work = container_of(cb, typeof(*work), cb);
167 	schedule_work(&work->cli->work);
168 }
169 
170 void
171 nouveau_cli_work_queue(struct nouveau_cli *cli, struct dma_fence *fence,
172 		       struct nouveau_cli_work *work)
173 {
174 	work->fence = dma_fence_get(fence);
175 	work->cli = cli;
176 	mutex_lock(&cli->lock);
177 	list_add_tail(&work->head, &cli->worker);
178 	if (dma_fence_add_callback(fence, &work->cb, nouveau_cli_work_fence))
179 		nouveau_cli_work_fence(fence, &work->cb);
180 	mutex_unlock(&cli->lock);
181 }
182 
183 static void
184 nouveau_cli_fini(struct nouveau_cli *cli)
185 {
186 	/* All our channels are dead now, which means all the fences they
187 	 * own are signalled, and all callback functions have been called.
188 	 *
189 	 * So, after flushing the workqueue, there should be nothing left.
190 	 */
191 	flush_work(&cli->work);
192 	WARN_ON(!list_empty(&cli->worker));
193 
194 	usif_client_fini(cli);
195 	nouveau_vmm_fini(&cli->svm);
196 	nouveau_vmm_fini(&cli->vmm);
197 	nvif_mmu_dtor(&cli->mmu);
198 	nvif_device_dtor(&cli->device);
199 	mutex_lock(&cli->drm->master.lock);
200 	nvif_client_dtor(&cli->base);
201 	mutex_unlock(&cli->drm->master.lock);
202 }
203 
204 static int
205 nouveau_cli_init(struct nouveau_drm *drm, const char *sname,
206 		 struct nouveau_cli *cli)
207 {
208 	static const struct nvif_mclass
209 	mems[] = {
210 		{ NVIF_CLASS_MEM_GF100, -1 },
211 		{ NVIF_CLASS_MEM_NV50 , -1 },
212 		{ NVIF_CLASS_MEM_NV04 , -1 },
213 		{}
214 	};
215 	static const struct nvif_mclass
216 	mmus[] = {
217 		{ NVIF_CLASS_MMU_GF100, -1 },
218 		{ NVIF_CLASS_MMU_NV50 , -1 },
219 		{ NVIF_CLASS_MMU_NV04 , -1 },
220 		{}
221 	};
222 	static const struct nvif_mclass
223 	vmms[] = {
224 		{ NVIF_CLASS_VMM_GP100, -1 },
225 		{ NVIF_CLASS_VMM_GM200, -1 },
226 		{ NVIF_CLASS_VMM_GF100, -1 },
227 		{ NVIF_CLASS_VMM_NV50 , -1 },
228 		{ NVIF_CLASS_VMM_NV04 , -1 },
229 		{}
230 	};
231 	u64 device = nouveau_name(drm->dev);
232 	int ret;
233 
234 	snprintf(cli->name, sizeof(cli->name), "%s", sname);
235 	cli->drm = drm;
236 	mutex_init(&cli->mutex);
237 	usif_client_init(cli);
238 
239 	INIT_WORK(&cli->work, nouveau_cli_work);
240 	INIT_LIST_HEAD(&cli->worker);
241 	mutex_init(&cli->lock);
242 
243 	if (cli == &drm->master) {
244 		ret = nvif_driver_init(NULL, nouveau_config, nouveau_debug,
245 				       cli->name, device, &cli->base);
246 	} else {
247 		mutex_lock(&drm->master.lock);
248 		ret = nvif_client_ctor(&drm->master.base, cli->name, device,
249 				       &cli->base);
250 		mutex_unlock(&drm->master.lock);
251 	}
252 	if (ret) {
253 		NV_PRINTK(err, cli, "Client allocation failed: %d\n", ret);
254 		goto done;
255 	}
256 
257 	ret = nvif_device_ctor(&cli->base.object, "drmDevice", 0, NV_DEVICE,
258 			       &(struct nv_device_v0) {
259 					.device = ~0,
260 					.priv = true,
261 			       }, sizeof(struct nv_device_v0),
262 			       &cli->device);
263 	if (ret) {
264 		NV_PRINTK(err, cli, "Device allocation failed: %d\n", ret);
265 		goto done;
266 	}
267 
268 	ret = nvif_mclass(&cli->device.object, mmus);
269 	if (ret < 0) {
270 		NV_PRINTK(err, cli, "No supported MMU class\n");
271 		goto done;
272 	}
273 
274 	ret = nvif_mmu_ctor(&cli->device.object, "drmMmu", mmus[ret].oclass,
275 			    &cli->mmu);
276 	if (ret) {
277 		NV_PRINTK(err, cli, "MMU allocation failed: %d\n", ret);
278 		goto done;
279 	}
280 
281 	ret = nvif_mclass(&cli->mmu.object, vmms);
282 	if (ret < 0) {
283 		NV_PRINTK(err, cli, "No supported VMM class\n");
284 		goto done;
285 	}
286 
287 	ret = nouveau_vmm_init(cli, vmms[ret].oclass, &cli->vmm);
288 	if (ret) {
289 		NV_PRINTK(err, cli, "VMM allocation failed: %d\n", ret);
290 		goto done;
291 	}
292 
293 	ret = nvif_mclass(&cli->mmu.object, mems);
294 	if (ret < 0) {
295 		NV_PRINTK(err, cli, "No supported MEM class\n");
296 		goto done;
297 	}
298 
299 	cli->mem = &mems[ret];
300 	return 0;
301 done:
302 	if (ret)
303 		nouveau_cli_fini(cli);
304 	return ret;
305 }
306 
307 static void
308 nouveau_accel_ce_fini(struct nouveau_drm *drm)
309 {
310 	nouveau_channel_idle(drm->cechan);
311 	nvif_object_dtor(&drm->ttm.copy);
312 	nouveau_channel_del(&drm->cechan);
313 }
314 
315 static void
316 nouveau_accel_ce_init(struct nouveau_drm *drm)
317 {
318 	struct nvif_device *device = &drm->client.device;
319 	u64 runm;
320 	int ret = 0;
321 
322 	/* Allocate channel that has access to a (preferably async) copy
323 	 * engine, to use for TTM buffer moves.
324 	 */
325 	runm = nvif_fifo_runlist_ce(device);
326 	if (!runm) {
327 		NV_DEBUG(drm, "no ce runlist\n");
328 		return;
329 	}
330 
331 	ret = nouveau_channel_new(drm, device, false, runm, NvDmaFB, NvDmaTT, &drm->cechan);
332 	if (ret)
333 		NV_ERROR(drm, "failed to create ce channel, %d\n", ret);
334 }
335 
336 static void
337 nouveau_accel_gr_fini(struct nouveau_drm *drm)
338 {
339 	nouveau_channel_idle(drm->channel);
340 	nvif_object_dtor(&drm->ntfy);
341 	nvkm_gpuobj_del(&drm->notify);
342 	nouveau_channel_del(&drm->channel);
343 }
344 
345 static void
346 nouveau_accel_gr_init(struct nouveau_drm *drm)
347 {
348 	struct nvif_device *device = &drm->client.device;
349 	u64 runm;
350 	int ret;
351 
352 	/* Allocate channel that has access to the graphics engine. */
353 	runm = nvif_fifo_runlist(device, NV_DEVICE_HOST_RUNLIST_ENGINES_GR);
354 	if (!runm) {
355 		NV_DEBUG(drm, "no gr runlist\n");
356 		return;
357 	}
358 
359 	ret = nouveau_channel_new(drm, device, false, runm, NvDmaFB, NvDmaTT, &drm->channel);
360 	if (ret) {
361 		NV_ERROR(drm, "failed to create kernel channel, %d\n", ret);
362 		nouveau_accel_gr_fini(drm);
363 		return;
364 	}
365 
366 	/* A SW class is used on pre-NV50 HW to assist with handling the
367 	 * synchronisation of page flips, as well as to implement fences
368 	 * on TNT/TNT2 HW that lacks any kind of support in host.
369 	 */
370 	if (!drm->channel->nvsw.client && device->info.family < NV_DEVICE_INFO_V0_TESLA) {
371 		ret = nvif_object_ctor(&drm->channel->user, "drmNvsw",
372 				       NVDRM_NVSW, nouveau_abi16_swclass(drm),
373 				       NULL, 0, &drm->channel->nvsw);
374 		if (ret == 0) {
375 			struct nvif_push *push = drm->channel->chan.push;
376 			ret = PUSH_WAIT(push, 2);
377 			if (ret == 0)
378 				PUSH_NVSQ(push, NV_SW, 0x0000, drm->channel->nvsw.handle);
379 		}
380 
381 		if (ret) {
382 			NV_ERROR(drm, "failed to allocate sw class, %d\n", ret);
383 			nouveau_accel_gr_fini(drm);
384 			return;
385 		}
386 	}
387 
388 	/* NvMemoryToMemoryFormat requires a notifier ctxdma for some reason,
389 	 * even if notification is never requested, so, allocate a ctxdma on
390 	 * any GPU where it's possible we'll end up using M2MF for BO moves.
391 	 */
392 	if (device->info.family < NV_DEVICE_INFO_V0_FERMI) {
393 		ret = nvkm_gpuobj_new(nvxx_device(device), 32, 0, false, NULL,
394 				      &drm->notify);
395 		if (ret) {
396 			NV_ERROR(drm, "failed to allocate notifier, %d\n", ret);
397 			nouveau_accel_gr_fini(drm);
398 			return;
399 		}
400 
401 		ret = nvif_object_ctor(&drm->channel->user, "drmM2mfNtfy",
402 				       NvNotify0, NV_DMA_IN_MEMORY,
403 				       &(struct nv_dma_v0) {
404 						.target = NV_DMA_V0_TARGET_VRAM,
405 						.access = NV_DMA_V0_ACCESS_RDWR,
406 						.start = drm->notify->addr,
407 						.limit = drm->notify->addr + 31
408 				       }, sizeof(struct nv_dma_v0),
409 				       &drm->ntfy);
410 		if (ret) {
411 			nouveau_accel_gr_fini(drm);
412 			return;
413 		}
414 	}
415 }
416 
417 static void
418 nouveau_accel_fini(struct nouveau_drm *drm)
419 {
420 	nouveau_accel_ce_fini(drm);
421 	nouveau_accel_gr_fini(drm);
422 	if (drm->fence)
423 		nouveau_fence(drm)->dtor(drm);
424 	nouveau_channels_fini(drm);
425 }
426 
427 static void
428 nouveau_accel_init(struct nouveau_drm *drm)
429 {
430 	struct nvif_device *device = &drm->client.device;
431 	struct nvif_sclass *sclass;
432 	int ret, i, n;
433 
434 	if (nouveau_noaccel)
435 		return;
436 
437 	/* Initialise global support for channels, and synchronisation. */
438 	ret = nouveau_channels_init(drm);
439 	if (ret)
440 		return;
441 
442 	/*XXX: this is crap, but the fence/channel stuff is a little
443 	 *     backwards in some places.  this will be fixed.
444 	 */
445 	ret = n = nvif_object_sclass_get(&device->object, &sclass);
446 	if (ret < 0)
447 		return;
448 
449 	for (ret = -ENOSYS, i = 0; i < n; i++) {
450 		switch (sclass[i].oclass) {
451 		case NV03_CHANNEL_DMA:
452 			ret = nv04_fence_create(drm);
453 			break;
454 		case NV10_CHANNEL_DMA:
455 			ret = nv10_fence_create(drm);
456 			break;
457 		case NV17_CHANNEL_DMA:
458 		case NV40_CHANNEL_DMA:
459 			ret = nv17_fence_create(drm);
460 			break;
461 		case NV50_CHANNEL_GPFIFO:
462 			ret = nv50_fence_create(drm);
463 			break;
464 		case G82_CHANNEL_GPFIFO:
465 			ret = nv84_fence_create(drm);
466 			break;
467 		case FERMI_CHANNEL_GPFIFO:
468 		case KEPLER_CHANNEL_GPFIFO_A:
469 		case KEPLER_CHANNEL_GPFIFO_B:
470 		case MAXWELL_CHANNEL_GPFIFO_A:
471 		case PASCAL_CHANNEL_GPFIFO_A:
472 		case VOLTA_CHANNEL_GPFIFO_A:
473 		case TURING_CHANNEL_GPFIFO_A:
474 		case AMPERE_CHANNEL_GPFIFO_A:
475 		case AMPERE_CHANNEL_GPFIFO_B:
476 			ret = nvc0_fence_create(drm);
477 			break;
478 		default:
479 			break;
480 		}
481 	}
482 
483 	nvif_object_sclass_put(&sclass);
484 	if (ret) {
485 		NV_ERROR(drm, "failed to initialise sync subsystem, %d\n", ret);
486 		nouveau_accel_fini(drm);
487 		return;
488 	}
489 
490 	/* Volta requires access to a doorbell register for kickoff. */
491 	if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_VOLTA) {
492 		ret = nvif_user_ctor(device, "drmUsermode");
493 		if (ret)
494 			return;
495 	}
496 
497 	/* Allocate channels we need to support various functions. */
498 	nouveau_accel_gr_init(drm);
499 	nouveau_accel_ce_init(drm);
500 
501 	/* Initialise accelerated TTM buffer moves. */
502 	nouveau_bo_move_init(drm);
503 }
504 
505 static void __printf(2, 3)
506 nouveau_drm_errorf(struct nvif_object *object, const char *fmt, ...)
507 {
508 	struct nouveau_drm *drm = container_of(object->parent, typeof(*drm), parent);
509 	struct va_format vaf;
510 	va_list va;
511 
512 	va_start(va, fmt);
513 	vaf.fmt = fmt;
514 	vaf.va = &va;
515 	NV_ERROR(drm, "%pV", &vaf);
516 	va_end(va);
517 }
518 
519 static void __printf(2, 3)
520 nouveau_drm_debugf(struct nvif_object *object, const char *fmt, ...)
521 {
522 	struct nouveau_drm *drm = container_of(object->parent, typeof(*drm), parent);
523 	struct va_format vaf;
524 	va_list va;
525 
526 	va_start(va, fmt);
527 	vaf.fmt = fmt;
528 	vaf.va = &va;
529 	NV_DEBUG(drm, "%pV", &vaf);
530 	va_end(va);
531 }
532 
533 static const struct nvif_parent_func
534 nouveau_parent = {
535 	.debugf = nouveau_drm_debugf,
536 	.errorf = nouveau_drm_errorf,
537 };
538 
539 static int
540 nouveau_drm_device_init(struct drm_device *dev)
541 {
542 	struct nouveau_drm *drm;
543 	int ret;
544 
545 	if (!(drm = kzalloc(sizeof(*drm), GFP_KERNEL)))
546 		return -ENOMEM;
547 	dev->dev_private = drm;
548 	drm->dev = dev;
549 
550 	nvif_parent_ctor(&nouveau_parent, &drm->parent);
551 	drm->master.base.object.parent = &drm->parent;
552 
553 	ret = nouveau_cli_init(drm, "DRM-master", &drm->master);
554 	if (ret)
555 		goto fail_alloc;
556 
557 	ret = nouveau_cli_init(drm, "DRM", &drm->client);
558 	if (ret)
559 		goto fail_master;
560 
561 	nvxx_client(&drm->client.base)->debug =
562 		nvkm_dbgopt(nouveau_debug, "DRM");
563 
564 	INIT_LIST_HEAD(&drm->clients);
565 	mutex_init(&drm->clients_lock);
566 	spin_lock_init(&drm->tile.lock);
567 
568 	/* workaround an odd issue on nvc1 by disabling the device's
569 	 * nosnoop capability.  hopefully won't cause issues until a
570 	 * better fix is found - assuming there is one...
571 	 */
572 	if (drm->client.device.info.chipset == 0xc1)
573 		nvif_mask(&drm->client.device.object, 0x00088080, 0x00000800, 0x00000000);
574 
575 	nouveau_vga_init(drm);
576 
577 	ret = nouveau_ttm_init(drm);
578 	if (ret)
579 		goto fail_ttm;
580 
581 	ret = nouveau_bios_init(dev);
582 	if (ret)
583 		goto fail_bios;
584 
585 	nouveau_accel_init(drm);
586 
587 	ret = nouveau_display_create(dev);
588 	if (ret)
589 		goto fail_dispctor;
590 
591 	if (dev->mode_config.num_crtc) {
592 		ret = nouveau_display_init(dev, false, false);
593 		if (ret)
594 			goto fail_dispinit;
595 	}
596 
597 	nouveau_debugfs_init(drm);
598 	nouveau_hwmon_init(dev);
599 	nouveau_svm_init(drm);
600 	nouveau_dmem_init(drm);
601 	nouveau_led_init(dev);
602 
603 	if (nouveau_pmops_runtime()) {
604 		pm_runtime_use_autosuspend(dev->dev);
605 		pm_runtime_set_autosuspend_delay(dev->dev, 5000);
606 		pm_runtime_set_active(dev->dev);
607 		pm_runtime_allow(dev->dev);
608 		pm_runtime_mark_last_busy(dev->dev);
609 		pm_runtime_put(dev->dev);
610 	}
611 
612 	return 0;
613 
614 fail_dispinit:
615 	nouveau_display_destroy(dev);
616 fail_dispctor:
617 	nouveau_accel_fini(drm);
618 	nouveau_bios_takedown(dev);
619 fail_bios:
620 	nouveau_ttm_fini(drm);
621 fail_ttm:
622 	nouveau_vga_fini(drm);
623 	nouveau_cli_fini(&drm->client);
624 fail_master:
625 	nouveau_cli_fini(&drm->master);
626 fail_alloc:
627 	nvif_parent_dtor(&drm->parent);
628 	kfree(drm);
629 	return ret;
630 }
631 
632 static void
633 nouveau_drm_device_fini(struct drm_device *dev)
634 {
635 	struct nouveau_cli *cli, *temp_cli;
636 	struct nouveau_drm *drm = nouveau_drm(dev);
637 
638 	if (nouveau_pmops_runtime()) {
639 		pm_runtime_get_sync(dev->dev);
640 		pm_runtime_forbid(dev->dev);
641 	}
642 
643 	nouveau_led_fini(dev);
644 	nouveau_dmem_fini(drm);
645 	nouveau_svm_fini(drm);
646 	nouveau_hwmon_fini(dev);
647 	nouveau_debugfs_fini(drm);
648 
649 	if (dev->mode_config.num_crtc)
650 		nouveau_display_fini(dev, false, false);
651 	nouveau_display_destroy(dev);
652 
653 	nouveau_accel_fini(drm);
654 	nouveau_bios_takedown(dev);
655 
656 	nouveau_ttm_fini(drm);
657 	nouveau_vga_fini(drm);
658 
659 	/*
660 	 * There may be existing clients from as-yet unclosed files. For now,
661 	 * clean them up here rather than deferring until the file is closed,
662 	 * but this likely not correct if we want to support hot-unplugging
663 	 * properly.
664 	 */
665 	mutex_lock(&drm->clients_lock);
666 	list_for_each_entry_safe(cli, temp_cli, &drm->clients, head) {
667 		list_del(&cli->head);
668 		mutex_lock(&cli->mutex);
669 		if (cli->abi16)
670 			nouveau_abi16_fini(cli->abi16);
671 		mutex_unlock(&cli->mutex);
672 		nouveau_cli_fini(cli);
673 		kfree(cli);
674 	}
675 	mutex_unlock(&drm->clients_lock);
676 
677 	nouveau_cli_fini(&drm->client);
678 	nouveau_cli_fini(&drm->master);
679 	nvif_parent_dtor(&drm->parent);
680 	mutex_destroy(&drm->clients_lock);
681 	kfree(drm);
682 }
683 
684 /*
685  * On some Intel PCIe bridge controllers doing a
686  * D0 -> D3hot -> D3cold -> D0 sequence causes Nvidia GPUs to not reappear.
687  * Skipping the intermediate D3hot step seems to make it work again. This is
688  * probably caused by not meeting the expectation the involved AML code has
689  * when the GPU is put into D3hot state before invoking it.
690  *
691  * This leads to various manifestations of this issue:
692  *  - AML code execution to power on the GPU hits an infinite loop (as the
693  *    code waits on device memory to change).
694  *  - kernel crashes, as all PCI reads return -1, which most code isn't able
695  *    to handle well enough.
696  *
697  * In all cases dmesg will contain at least one line like this:
698  * 'nouveau 0000:01:00.0: Refused to change power state, currently in D3'
699  * followed by a lot of nouveau timeouts.
700  *
701  * In the \_SB.PCI0.PEG0.PG00._OFF code deeper down writes bit 0x80 to the not
702  * documented PCI config space register 0x248 of the Intel PCIe bridge
703  * controller (0x1901) in order to change the state of the PCIe link between
704  * the PCIe port and the GPU. There are alternative code paths using other
705  * registers, which seem to work fine (executed pre Windows 8):
706  *  - 0xbc bit 0x20 (publicly available documentation claims 'reserved')
707  *  - 0xb0 bit 0x10 (link disable)
708  * Changing the conditions inside the firmware by poking into the relevant
709  * addresses does resolve the issue, but it seemed to be ACPI private memory
710  * and not any device accessible memory at all, so there is no portable way of
711  * changing the conditions.
712  * On a XPS 9560 that means bits [0,3] on \CPEX need to be cleared.
713  *
714  * The only systems where this behavior can be seen are hybrid graphics laptops
715  * with a secondary Nvidia Maxwell, Pascal or Turing GPU. It's unclear whether
716  * this issue only occurs in combination with listed Intel PCIe bridge
717  * controllers and the mentioned GPUs or other devices as well.
718  *
719  * documentation on the PCIe bridge controller can be found in the
720  * "7th Generation Intel® Processor Families for H Platforms Datasheet Volume 2"
721  * Section "12 PCI Express* Controller (x16) Registers"
722  */
723 
724 static void quirk_broken_nv_runpm(struct pci_dev *pdev)
725 {
726 	struct drm_device *dev = pci_get_drvdata(pdev);
727 	struct nouveau_drm *drm = nouveau_drm(dev);
728 	struct pci_dev *bridge = pci_upstream_bridge(pdev);
729 
730 	if (!bridge || bridge->vendor != PCI_VENDOR_ID_INTEL)
731 		return;
732 
733 	switch (bridge->device) {
734 	case 0x1901:
735 		drm->old_pm_cap = pdev->pm_cap;
736 		pdev->pm_cap = 0;
737 		NV_INFO(drm, "Disabling PCI power management to avoid bug\n");
738 		break;
739 	}
740 }
741 
742 static int nouveau_drm_probe(struct pci_dev *pdev,
743 			     const struct pci_device_id *pent)
744 {
745 	struct nvkm_device *device;
746 	struct drm_device *drm_dev;
747 	int ret;
748 
749 	if (vga_switcheroo_client_probe_defer(pdev))
750 		return -EPROBE_DEFER;
751 
752 	/* We need to check that the chipset is supported before booting
753 	 * fbdev off the hardware, as there's no way to put it back.
754 	 */
755 	ret = nvkm_device_pci_new(pdev, nouveau_config, "error",
756 				  true, false, 0, &device);
757 	if (ret)
758 		return ret;
759 
760 	nvkm_device_del(&device);
761 
762 	/* Remove conflicting drivers (vesafb, efifb etc). */
763 	ret = drm_aperture_remove_conflicting_pci_framebuffers(pdev, &driver_pci);
764 	if (ret)
765 		return ret;
766 
767 	ret = nvkm_device_pci_new(pdev, nouveau_config, nouveau_debug,
768 				  true, true, ~0ULL, &device);
769 	if (ret)
770 		return ret;
771 
772 	pci_set_master(pdev);
773 
774 	if (nouveau_atomic)
775 		driver_pci.driver_features |= DRIVER_ATOMIC;
776 
777 	drm_dev = drm_dev_alloc(&driver_pci, &pdev->dev);
778 	if (IS_ERR(drm_dev)) {
779 		ret = PTR_ERR(drm_dev);
780 		goto fail_nvkm;
781 	}
782 
783 	ret = pci_enable_device(pdev);
784 	if (ret)
785 		goto fail_drm;
786 
787 	pci_set_drvdata(pdev, drm_dev);
788 
789 	ret = nouveau_drm_device_init(drm_dev);
790 	if (ret)
791 		goto fail_pci;
792 
793 	ret = drm_dev_register(drm_dev, pent->driver_data);
794 	if (ret)
795 		goto fail_drm_dev_init;
796 
797 	if (nouveau_drm(drm_dev)->client.device.info.ram_size <= 32 * 1024 * 1024)
798 		drm_fbdev_generic_setup(drm_dev, 8);
799 	else
800 		drm_fbdev_generic_setup(drm_dev, 32);
801 
802 	quirk_broken_nv_runpm(pdev);
803 	return 0;
804 
805 fail_drm_dev_init:
806 	nouveau_drm_device_fini(drm_dev);
807 fail_pci:
808 	pci_disable_device(pdev);
809 fail_drm:
810 	drm_dev_put(drm_dev);
811 fail_nvkm:
812 	nvkm_device_del(&device);
813 	return ret;
814 }
815 
816 void
817 nouveau_drm_device_remove(struct drm_device *dev)
818 {
819 	struct nouveau_drm *drm = nouveau_drm(dev);
820 	struct nvkm_client *client;
821 	struct nvkm_device *device;
822 
823 	drm_dev_unplug(dev);
824 
825 	client = nvxx_client(&drm->client.base);
826 	device = nvkm_device_find(client->device);
827 
828 	nouveau_drm_device_fini(dev);
829 	drm_dev_put(dev);
830 	nvkm_device_del(&device);
831 }
832 
833 static void
834 nouveau_drm_remove(struct pci_dev *pdev)
835 {
836 	struct drm_device *dev = pci_get_drvdata(pdev);
837 	struct nouveau_drm *drm = nouveau_drm(dev);
838 
839 	/* revert our workaround */
840 	if (drm->old_pm_cap)
841 		pdev->pm_cap = drm->old_pm_cap;
842 	nouveau_drm_device_remove(dev);
843 	pci_disable_device(pdev);
844 }
845 
846 static int
847 nouveau_do_suspend(struct drm_device *dev, bool runtime)
848 {
849 	struct nouveau_drm *drm = nouveau_drm(dev);
850 	struct ttm_resource_manager *man;
851 	int ret;
852 
853 	nouveau_svm_suspend(drm);
854 	nouveau_dmem_suspend(drm);
855 	nouveau_led_suspend(dev);
856 
857 	if (dev->mode_config.num_crtc) {
858 		NV_DEBUG(drm, "suspending display...\n");
859 		ret = nouveau_display_suspend(dev, runtime);
860 		if (ret)
861 			return ret;
862 	}
863 
864 	NV_DEBUG(drm, "evicting buffers...\n");
865 
866 	man = ttm_manager_type(&drm->ttm.bdev, TTM_PL_VRAM);
867 	ttm_resource_manager_evict_all(&drm->ttm.bdev, man);
868 
869 	NV_DEBUG(drm, "waiting for kernel channels to go idle...\n");
870 	if (drm->cechan) {
871 		ret = nouveau_channel_idle(drm->cechan);
872 		if (ret)
873 			goto fail_display;
874 	}
875 
876 	if (drm->channel) {
877 		ret = nouveau_channel_idle(drm->channel);
878 		if (ret)
879 			goto fail_display;
880 	}
881 
882 	NV_DEBUG(drm, "suspending fence...\n");
883 	if (drm->fence && nouveau_fence(drm)->suspend) {
884 		if (!nouveau_fence(drm)->suspend(drm)) {
885 			ret = -ENOMEM;
886 			goto fail_display;
887 		}
888 	}
889 
890 	NV_DEBUG(drm, "suspending object tree...\n");
891 	ret = nvif_client_suspend(&drm->master.base);
892 	if (ret)
893 		goto fail_client;
894 
895 	return 0;
896 
897 fail_client:
898 	if (drm->fence && nouveau_fence(drm)->resume)
899 		nouveau_fence(drm)->resume(drm);
900 
901 fail_display:
902 	if (dev->mode_config.num_crtc) {
903 		NV_DEBUG(drm, "resuming display...\n");
904 		nouveau_display_resume(dev, runtime);
905 	}
906 	return ret;
907 }
908 
909 static int
910 nouveau_do_resume(struct drm_device *dev, bool runtime)
911 {
912 	int ret = 0;
913 	struct nouveau_drm *drm = nouveau_drm(dev);
914 
915 	NV_DEBUG(drm, "resuming object tree...\n");
916 	ret = nvif_client_resume(&drm->master.base);
917 	if (ret) {
918 		NV_ERROR(drm, "Client resume failed with error: %d\n", ret);
919 		return ret;
920 	}
921 
922 	NV_DEBUG(drm, "resuming fence...\n");
923 	if (drm->fence && nouveau_fence(drm)->resume)
924 		nouveau_fence(drm)->resume(drm);
925 
926 	nouveau_run_vbios_init(dev);
927 
928 	if (dev->mode_config.num_crtc) {
929 		NV_DEBUG(drm, "resuming display...\n");
930 		nouveau_display_resume(dev, runtime);
931 	}
932 
933 	nouveau_led_resume(dev);
934 	nouveau_dmem_resume(drm);
935 	nouveau_svm_resume(drm);
936 	return 0;
937 }
938 
939 int
940 nouveau_pmops_suspend(struct device *dev)
941 {
942 	struct pci_dev *pdev = to_pci_dev(dev);
943 	struct drm_device *drm_dev = pci_get_drvdata(pdev);
944 	int ret;
945 
946 	if (drm_dev->switch_power_state == DRM_SWITCH_POWER_OFF ||
947 	    drm_dev->switch_power_state == DRM_SWITCH_POWER_DYNAMIC_OFF)
948 		return 0;
949 
950 	ret = nouveau_do_suspend(drm_dev, false);
951 	if (ret)
952 		return ret;
953 
954 	pci_save_state(pdev);
955 	pci_disable_device(pdev);
956 	pci_set_power_state(pdev, PCI_D3hot);
957 	udelay(200);
958 	return 0;
959 }
960 
961 int
962 nouveau_pmops_resume(struct device *dev)
963 {
964 	struct pci_dev *pdev = to_pci_dev(dev);
965 	struct drm_device *drm_dev = pci_get_drvdata(pdev);
966 	int ret;
967 
968 	if (drm_dev->switch_power_state == DRM_SWITCH_POWER_OFF ||
969 	    drm_dev->switch_power_state == DRM_SWITCH_POWER_DYNAMIC_OFF)
970 		return 0;
971 
972 	pci_set_power_state(pdev, PCI_D0);
973 	pci_restore_state(pdev);
974 	ret = pci_enable_device(pdev);
975 	if (ret)
976 		return ret;
977 	pci_set_master(pdev);
978 
979 	ret = nouveau_do_resume(drm_dev, false);
980 
981 	/* Monitors may have been connected / disconnected during suspend */
982 	nouveau_display_hpd_resume(drm_dev);
983 
984 	return ret;
985 }
986 
987 static int
988 nouveau_pmops_freeze(struct device *dev)
989 {
990 	struct pci_dev *pdev = to_pci_dev(dev);
991 	struct drm_device *drm_dev = pci_get_drvdata(pdev);
992 	return nouveau_do_suspend(drm_dev, false);
993 }
994 
995 static int
996 nouveau_pmops_thaw(struct device *dev)
997 {
998 	struct pci_dev *pdev = to_pci_dev(dev);
999 	struct drm_device *drm_dev = pci_get_drvdata(pdev);
1000 	return nouveau_do_resume(drm_dev, false);
1001 }
1002 
1003 bool
1004 nouveau_pmops_runtime(void)
1005 {
1006 	if (nouveau_runtime_pm == -1)
1007 		return nouveau_is_optimus() || nouveau_is_v1_dsm();
1008 	return nouveau_runtime_pm == 1;
1009 }
1010 
1011 static int
1012 nouveau_pmops_runtime_suspend(struct device *dev)
1013 {
1014 	struct pci_dev *pdev = to_pci_dev(dev);
1015 	struct drm_device *drm_dev = pci_get_drvdata(pdev);
1016 	int ret;
1017 
1018 	if (!nouveau_pmops_runtime()) {
1019 		pm_runtime_forbid(dev);
1020 		return -EBUSY;
1021 	}
1022 
1023 	nouveau_switcheroo_optimus_dsm();
1024 	ret = nouveau_do_suspend(drm_dev, true);
1025 	pci_save_state(pdev);
1026 	pci_disable_device(pdev);
1027 	pci_ignore_hotplug(pdev);
1028 	pci_set_power_state(pdev, PCI_D3cold);
1029 	drm_dev->switch_power_state = DRM_SWITCH_POWER_DYNAMIC_OFF;
1030 	return ret;
1031 }
1032 
1033 static int
1034 nouveau_pmops_runtime_resume(struct device *dev)
1035 {
1036 	struct pci_dev *pdev = to_pci_dev(dev);
1037 	struct drm_device *drm_dev = pci_get_drvdata(pdev);
1038 	struct nouveau_drm *drm = nouveau_drm(drm_dev);
1039 	struct nvif_device *device = &nouveau_drm(drm_dev)->client.device;
1040 	int ret;
1041 
1042 	if (!nouveau_pmops_runtime()) {
1043 		pm_runtime_forbid(dev);
1044 		return -EBUSY;
1045 	}
1046 
1047 	pci_set_power_state(pdev, PCI_D0);
1048 	pci_restore_state(pdev);
1049 	ret = pci_enable_device(pdev);
1050 	if (ret)
1051 		return ret;
1052 	pci_set_master(pdev);
1053 
1054 	ret = nouveau_do_resume(drm_dev, true);
1055 	if (ret) {
1056 		NV_ERROR(drm, "resume failed with: %d\n", ret);
1057 		return ret;
1058 	}
1059 
1060 	/* do magic */
1061 	nvif_mask(&device->object, 0x088488, (1 << 25), (1 << 25));
1062 	drm_dev->switch_power_state = DRM_SWITCH_POWER_ON;
1063 
1064 	/* Monitors may have been connected / disconnected during suspend */
1065 	nouveau_display_hpd_resume(drm_dev);
1066 
1067 	return ret;
1068 }
1069 
1070 static int
1071 nouveau_pmops_runtime_idle(struct device *dev)
1072 {
1073 	if (!nouveau_pmops_runtime()) {
1074 		pm_runtime_forbid(dev);
1075 		return -EBUSY;
1076 	}
1077 
1078 	pm_runtime_mark_last_busy(dev);
1079 	pm_runtime_autosuspend(dev);
1080 	/* we don't want the main rpm_idle to call suspend - we want to autosuspend */
1081 	return 1;
1082 }
1083 
1084 static int
1085 nouveau_drm_open(struct drm_device *dev, struct drm_file *fpriv)
1086 {
1087 	struct nouveau_drm *drm = nouveau_drm(dev);
1088 	struct nouveau_cli *cli;
1089 	char name[32], tmpname[TASK_COMM_LEN];
1090 	int ret;
1091 
1092 	/* need to bring up power immediately if opening device */
1093 	ret = pm_runtime_get_sync(dev->dev);
1094 	if (ret < 0 && ret != -EACCES) {
1095 		pm_runtime_put_autosuspend(dev->dev);
1096 		return ret;
1097 	}
1098 
1099 	get_task_comm(tmpname, current);
1100 	snprintf(name, sizeof(name), "%s[%d]", tmpname, pid_nr(fpriv->pid));
1101 
1102 	if (!(cli = kzalloc(sizeof(*cli), GFP_KERNEL))) {
1103 		ret = -ENOMEM;
1104 		goto done;
1105 	}
1106 
1107 	ret = nouveau_cli_init(drm, name, cli);
1108 	if (ret)
1109 		goto done;
1110 
1111 	fpriv->driver_priv = cli;
1112 
1113 	mutex_lock(&drm->clients_lock);
1114 	list_add(&cli->head, &drm->clients);
1115 	mutex_unlock(&drm->clients_lock);
1116 
1117 done:
1118 	if (ret && cli) {
1119 		nouveau_cli_fini(cli);
1120 		kfree(cli);
1121 	}
1122 
1123 	pm_runtime_mark_last_busy(dev->dev);
1124 	pm_runtime_put_autosuspend(dev->dev);
1125 	return ret;
1126 }
1127 
1128 static void
1129 nouveau_drm_postclose(struct drm_device *dev, struct drm_file *fpriv)
1130 {
1131 	struct nouveau_cli *cli = nouveau_cli(fpriv);
1132 	struct nouveau_drm *drm = nouveau_drm(dev);
1133 	int dev_index;
1134 
1135 	/*
1136 	 * The device is gone, and as it currently stands all clients are
1137 	 * cleaned up in the removal codepath. In the future this may change
1138 	 * so that we can support hot-unplugging, but for now we immediately
1139 	 * return to avoid a double-free situation.
1140 	 */
1141 	if (!drm_dev_enter(dev, &dev_index))
1142 		return;
1143 
1144 	pm_runtime_get_sync(dev->dev);
1145 
1146 	mutex_lock(&cli->mutex);
1147 	if (cli->abi16)
1148 		nouveau_abi16_fini(cli->abi16);
1149 	mutex_unlock(&cli->mutex);
1150 
1151 	mutex_lock(&drm->clients_lock);
1152 	list_del(&cli->head);
1153 	mutex_unlock(&drm->clients_lock);
1154 
1155 	nouveau_cli_fini(cli);
1156 	kfree(cli);
1157 	pm_runtime_mark_last_busy(dev->dev);
1158 	pm_runtime_put_autosuspend(dev->dev);
1159 	drm_dev_exit(dev_index);
1160 }
1161 
1162 static const struct drm_ioctl_desc
1163 nouveau_ioctls[] = {
1164 	DRM_IOCTL_DEF_DRV(NOUVEAU_GETPARAM, nouveau_abi16_ioctl_getparam, DRM_RENDER_ALLOW),
1165 	DRM_IOCTL_DEF_DRV(NOUVEAU_SETPARAM, drm_invalid_op, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1166 	DRM_IOCTL_DEF_DRV(NOUVEAU_CHANNEL_ALLOC, nouveau_abi16_ioctl_channel_alloc, DRM_RENDER_ALLOW),
1167 	DRM_IOCTL_DEF_DRV(NOUVEAU_CHANNEL_FREE, nouveau_abi16_ioctl_channel_free, DRM_RENDER_ALLOW),
1168 	DRM_IOCTL_DEF_DRV(NOUVEAU_GROBJ_ALLOC, nouveau_abi16_ioctl_grobj_alloc, DRM_RENDER_ALLOW),
1169 	DRM_IOCTL_DEF_DRV(NOUVEAU_NOTIFIEROBJ_ALLOC, nouveau_abi16_ioctl_notifierobj_alloc, DRM_RENDER_ALLOW),
1170 	DRM_IOCTL_DEF_DRV(NOUVEAU_GPUOBJ_FREE, nouveau_abi16_ioctl_gpuobj_free, DRM_RENDER_ALLOW),
1171 	DRM_IOCTL_DEF_DRV(NOUVEAU_SVM_INIT, nouveau_svmm_init, DRM_RENDER_ALLOW),
1172 	DRM_IOCTL_DEF_DRV(NOUVEAU_SVM_BIND, nouveau_svmm_bind, DRM_RENDER_ALLOW),
1173 	DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_NEW, nouveau_gem_ioctl_new, DRM_RENDER_ALLOW),
1174 	DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_PUSHBUF, nouveau_gem_ioctl_pushbuf, DRM_RENDER_ALLOW),
1175 	DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_CPU_PREP, nouveau_gem_ioctl_cpu_prep, DRM_RENDER_ALLOW),
1176 	DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_CPU_FINI, nouveau_gem_ioctl_cpu_fini, DRM_RENDER_ALLOW),
1177 	DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_INFO, nouveau_gem_ioctl_info, DRM_RENDER_ALLOW),
1178 };
1179 
1180 long
1181 nouveau_drm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1182 {
1183 	struct drm_file *filp = file->private_data;
1184 	struct drm_device *dev = filp->minor->dev;
1185 	long ret;
1186 
1187 	ret = pm_runtime_get_sync(dev->dev);
1188 	if (ret < 0 && ret != -EACCES) {
1189 		pm_runtime_put_autosuspend(dev->dev);
1190 		return ret;
1191 	}
1192 
1193 	switch (_IOC_NR(cmd) - DRM_COMMAND_BASE) {
1194 	case DRM_NOUVEAU_NVIF:
1195 		ret = usif_ioctl(filp, (void __user *)arg, _IOC_SIZE(cmd));
1196 		break;
1197 	default:
1198 		ret = drm_ioctl(file, cmd, arg);
1199 		break;
1200 	}
1201 
1202 	pm_runtime_mark_last_busy(dev->dev);
1203 	pm_runtime_put_autosuspend(dev->dev);
1204 	return ret;
1205 }
1206 
1207 static const struct file_operations
1208 nouveau_driver_fops = {
1209 	.owner = THIS_MODULE,
1210 	.open = drm_open,
1211 	.release = drm_release,
1212 	.unlocked_ioctl = nouveau_drm_ioctl,
1213 	.mmap = drm_gem_mmap,
1214 	.poll = drm_poll,
1215 	.read = drm_read,
1216 #if defined(CONFIG_COMPAT)
1217 	.compat_ioctl = nouveau_compat_ioctl,
1218 #endif
1219 	.llseek = noop_llseek,
1220 };
1221 
1222 static struct drm_driver
1223 driver_stub = {
1224 	.driver_features =
1225 		DRIVER_GEM | DRIVER_MODESET | DRIVER_RENDER
1226 #if defined(CONFIG_NOUVEAU_LEGACY_CTX_SUPPORT)
1227 		| DRIVER_KMS_LEGACY_CONTEXT
1228 #endif
1229 		,
1230 
1231 	.open = nouveau_drm_open,
1232 	.postclose = nouveau_drm_postclose,
1233 	.lastclose = nouveau_vga_lastclose,
1234 
1235 #if defined(CONFIG_DEBUG_FS)
1236 	.debugfs_init = nouveau_drm_debugfs_init,
1237 #endif
1238 
1239 	.ioctls = nouveau_ioctls,
1240 	.num_ioctls = ARRAY_SIZE(nouveau_ioctls),
1241 	.fops = &nouveau_driver_fops,
1242 
1243 	.prime_handle_to_fd = drm_gem_prime_handle_to_fd,
1244 	.prime_fd_to_handle = drm_gem_prime_fd_to_handle,
1245 	.gem_prime_import_sg_table = nouveau_gem_prime_import_sg_table,
1246 	.gem_prime_mmap = drm_gem_prime_mmap,
1247 
1248 	.dumb_create = nouveau_display_dumb_create,
1249 	.dumb_map_offset = drm_gem_ttm_dumb_map_offset,
1250 
1251 	.name = DRIVER_NAME,
1252 	.desc = DRIVER_DESC,
1253 #ifdef GIT_REVISION
1254 	.date = GIT_REVISION,
1255 #else
1256 	.date = DRIVER_DATE,
1257 #endif
1258 	.major = DRIVER_MAJOR,
1259 	.minor = DRIVER_MINOR,
1260 	.patchlevel = DRIVER_PATCHLEVEL,
1261 };
1262 
1263 static struct pci_device_id
1264 nouveau_drm_pci_table[] = {
1265 	{
1266 		PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_ANY_ID),
1267 		.class = PCI_BASE_CLASS_DISPLAY << 16,
1268 		.class_mask  = 0xff << 16,
1269 	},
1270 	{
1271 		PCI_DEVICE(PCI_VENDOR_ID_NVIDIA_SGS, PCI_ANY_ID),
1272 		.class = PCI_BASE_CLASS_DISPLAY << 16,
1273 		.class_mask  = 0xff << 16,
1274 	},
1275 	{}
1276 };
1277 
1278 static void nouveau_display_options(void)
1279 {
1280 	DRM_DEBUG_DRIVER("Loading Nouveau with parameters:\n");
1281 
1282 	DRM_DEBUG_DRIVER("... tv_disable   : %d\n", nouveau_tv_disable);
1283 	DRM_DEBUG_DRIVER("... ignorelid    : %d\n", nouveau_ignorelid);
1284 	DRM_DEBUG_DRIVER("... duallink     : %d\n", nouveau_duallink);
1285 	DRM_DEBUG_DRIVER("... config       : %s\n", nouveau_config);
1286 	DRM_DEBUG_DRIVER("... debug        : %s\n", nouveau_debug);
1287 	DRM_DEBUG_DRIVER("... noaccel      : %d\n", nouveau_noaccel);
1288 	DRM_DEBUG_DRIVER("... modeset      : %d\n", nouveau_modeset);
1289 	DRM_DEBUG_DRIVER("... runpm        : %d\n", nouveau_runtime_pm);
1290 	DRM_DEBUG_DRIVER("... vram_pushbuf : %d\n", nouveau_vram_pushbuf);
1291 	DRM_DEBUG_DRIVER("... hdmimhz      : %d\n", nouveau_hdmimhz);
1292 }
1293 
1294 static const struct dev_pm_ops nouveau_pm_ops = {
1295 	.suspend = nouveau_pmops_suspend,
1296 	.resume = nouveau_pmops_resume,
1297 	.freeze = nouveau_pmops_freeze,
1298 	.thaw = nouveau_pmops_thaw,
1299 	.poweroff = nouveau_pmops_freeze,
1300 	.restore = nouveau_pmops_resume,
1301 	.runtime_suspend = nouveau_pmops_runtime_suspend,
1302 	.runtime_resume = nouveau_pmops_runtime_resume,
1303 	.runtime_idle = nouveau_pmops_runtime_idle,
1304 };
1305 
1306 static struct pci_driver
1307 nouveau_drm_pci_driver = {
1308 	.name = "nouveau",
1309 	.id_table = nouveau_drm_pci_table,
1310 	.probe = nouveau_drm_probe,
1311 	.remove = nouveau_drm_remove,
1312 	.driver.pm = &nouveau_pm_ops,
1313 };
1314 
1315 struct drm_device *
1316 nouveau_platform_device_create(const struct nvkm_device_tegra_func *func,
1317 			       struct platform_device *pdev,
1318 			       struct nvkm_device **pdevice)
1319 {
1320 	struct drm_device *drm;
1321 	int err;
1322 
1323 	err = nvkm_device_tegra_new(func, pdev, nouveau_config, nouveau_debug,
1324 				    true, true, ~0ULL, pdevice);
1325 	if (err)
1326 		goto err_free;
1327 
1328 	drm = drm_dev_alloc(&driver_platform, &pdev->dev);
1329 	if (IS_ERR(drm)) {
1330 		err = PTR_ERR(drm);
1331 		goto err_free;
1332 	}
1333 
1334 	err = nouveau_drm_device_init(drm);
1335 	if (err)
1336 		goto err_put;
1337 
1338 	platform_set_drvdata(pdev, drm);
1339 
1340 	return drm;
1341 
1342 err_put:
1343 	drm_dev_put(drm);
1344 err_free:
1345 	nvkm_device_del(pdevice);
1346 
1347 	return ERR_PTR(err);
1348 }
1349 
1350 static int __init
1351 nouveau_drm_init(void)
1352 {
1353 	driver_pci = driver_stub;
1354 	driver_platform = driver_stub;
1355 
1356 	nouveau_display_options();
1357 
1358 	if (nouveau_modeset == -1) {
1359 		if (drm_firmware_drivers_only())
1360 			nouveau_modeset = 0;
1361 	}
1362 
1363 	if (!nouveau_modeset)
1364 		return 0;
1365 
1366 #ifdef CONFIG_NOUVEAU_PLATFORM_DRIVER
1367 	platform_driver_register(&nouveau_platform_driver);
1368 #endif
1369 
1370 	nouveau_register_dsm_handler();
1371 	nouveau_backlight_ctor();
1372 
1373 #ifdef CONFIG_PCI
1374 	return pci_register_driver(&nouveau_drm_pci_driver);
1375 #else
1376 	return 0;
1377 #endif
1378 }
1379 
1380 static void __exit
1381 nouveau_drm_exit(void)
1382 {
1383 	if (!nouveau_modeset)
1384 		return;
1385 
1386 #ifdef CONFIG_PCI
1387 	pci_unregister_driver(&nouveau_drm_pci_driver);
1388 #endif
1389 	nouveau_backlight_dtor();
1390 	nouveau_unregister_dsm_handler();
1391 
1392 #ifdef CONFIG_NOUVEAU_PLATFORM_DRIVER
1393 	platform_driver_unregister(&nouveau_platform_driver);
1394 #endif
1395 	if (IS_ENABLED(CONFIG_DRM_NOUVEAU_SVM))
1396 		mmu_notifier_synchronize();
1397 }
1398 
1399 module_init(nouveau_drm_init);
1400 module_exit(nouveau_drm_exit);
1401 
1402 MODULE_DEVICE_TABLE(pci, nouveau_drm_pci_table);
1403 MODULE_AUTHOR(DRIVER_AUTHOR);
1404 MODULE_DESCRIPTION(DRIVER_DESC);
1405 MODULE_LICENSE("GPL and additional rights");
1406