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