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