xref: /linux/drivers/gpu/drm/nouveau/nouveau_drm.c (revision 3a8c3400f3e74638bedd0d2410416aa8b794c0fd)
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/console.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 
32 #include "drmP.h"
33 #include "drm_crtc_helper.h"
34 
35 #include <core/device.h>
36 #include <core/gpuobj.h>
37 #include <core/option.h>
38 
39 #include "nouveau_drm.h"
40 #include "nouveau_dma.h"
41 #include "nouveau_ttm.h"
42 #include "nouveau_gem.h"
43 #include "nouveau_agp.h"
44 #include "nouveau_vga.h"
45 #include "nouveau_sysfs.h"
46 #include "nouveau_hwmon.h"
47 #include "nouveau_acpi.h"
48 #include "nouveau_bios.h"
49 #include "nouveau_ioctl.h"
50 #include "nouveau_abi16.h"
51 #include "nouveau_fbcon.h"
52 #include "nouveau_fence.h"
53 #include "nouveau_debugfs.h"
54 #include "nouveau_usif.h"
55 #include "nouveau_connector.h"
56 #include "nouveau_platform.h"
57 
58 MODULE_PARM_DESC(config, "option string to pass to driver core");
59 static char *nouveau_config;
60 module_param_named(config, nouveau_config, charp, 0400);
61 
62 MODULE_PARM_DESC(debug, "debug string to pass to driver core");
63 static char *nouveau_debug;
64 module_param_named(debug, nouveau_debug, charp, 0400);
65 
66 MODULE_PARM_DESC(noaccel, "disable kernel/abi16 acceleration");
67 static int nouveau_noaccel = 0;
68 module_param_named(noaccel, nouveau_noaccel, int, 0400);
69 
70 MODULE_PARM_DESC(modeset, "enable driver (default: auto, "
71 		          "0 = disabled, 1 = enabled, 2 = headless)");
72 int nouveau_modeset = -1;
73 module_param_named(modeset, nouveau_modeset, int, 0400);
74 
75 MODULE_PARM_DESC(runpm, "disable (0), force enable (1), optimus only default (-1)");
76 int nouveau_runtime_pm = -1;
77 module_param_named(runpm, nouveau_runtime_pm, int, 0400);
78 
79 static struct drm_driver driver_stub;
80 static struct drm_driver driver_pci;
81 static struct drm_driver driver_platform;
82 
83 static u64
84 nouveau_pci_name(struct pci_dev *pdev)
85 {
86 	u64 name = (u64)pci_domain_nr(pdev->bus) << 32;
87 	name |= pdev->bus->number << 16;
88 	name |= PCI_SLOT(pdev->devfn) << 8;
89 	return name | PCI_FUNC(pdev->devfn);
90 }
91 
92 static u64
93 nouveau_platform_name(struct platform_device *platformdev)
94 {
95 	return platformdev->id;
96 }
97 
98 static u64
99 nouveau_name(struct drm_device *dev)
100 {
101 	if (dev->pdev)
102 		return nouveau_pci_name(dev->pdev);
103 	else
104 		return nouveau_platform_name(dev->platformdev);
105 }
106 
107 static int
108 nouveau_cli_create(struct drm_device *dev, const char *sname,
109 		   int size, void **pcli)
110 {
111 	struct nouveau_cli *cli = *pcli = kzalloc(size, GFP_KERNEL);
112 	int ret;
113 	if (cli) {
114 		snprintf(cli->name, sizeof(cli->name), "%s", sname);
115 		cli->dev = dev;
116 
117 		ret = nvif_client_init(NULL, cli->name, nouveau_name(dev),
118 				       nouveau_config, nouveau_debug,
119 				       &cli->base);
120 		if (ret == 0) {
121 			mutex_init(&cli->mutex);
122 			usif_client_init(cli);
123 		}
124 		return ret;
125 	}
126 	return -ENOMEM;
127 }
128 
129 static void
130 nouveau_cli_destroy(struct nouveau_cli *cli)
131 {
132 	nvkm_vm_ref(NULL, &nvxx_client(&cli->base)->vm, NULL);
133 	nvif_client_fini(&cli->base);
134 	usif_client_fini(cli);
135 	kfree(cli);
136 }
137 
138 static void
139 nouveau_accel_fini(struct nouveau_drm *drm)
140 {
141 	nouveau_channel_del(&drm->channel);
142 	nvif_object_fini(&drm->ntfy);
143 	nvkm_gpuobj_ref(NULL, &drm->notify);
144 	nvif_object_fini(&drm->nvsw);
145 	nouveau_channel_del(&drm->cechan);
146 	nvif_object_fini(&drm->ttm.copy);
147 	if (drm->fence)
148 		nouveau_fence(drm)->dtor(drm);
149 }
150 
151 static void
152 nouveau_accel_init(struct nouveau_drm *drm)
153 {
154 	struct nvif_device *device = &drm->device;
155 	struct nvif_sclass *sclass;
156 	u32 arg0, arg1;
157 	int ret, i, n;
158 
159 	if (nouveau_noaccel)
160 		return;
161 
162 	/* initialise synchronisation routines */
163 	/*XXX: this is crap, but the fence/channel stuff is a little
164 	 *     backwards in some places.  this will be fixed.
165 	 */
166 	ret = n = nvif_object_sclass_get(&device->object, &sclass);
167 	if (ret < 0)
168 		return;
169 
170 	for (ret = -ENOSYS, i = 0; i < n; i++) {
171 		switch (sclass[i].oclass) {
172 		case NV03_CHANNEL_DMA:
173 			ret = nv04_fence_create(drm);
174 			break;
175 		case NV10_CHANNEL_DMA:
176 			ret = nv10_fence_create(drm);
177 			break;
178 		case NV17_CHANNEL_DMA:
179 		case NV40_CHANNEL_DMA:
180 			ret = nv17_fence_create(drm);
181 			break;
182 		case NV50_CHANNEL_GPFIFO:
183 			ret = nv50_fence_create(drm);
184 			break;
185 		case G82_CHANNEL_GPFIFO:
186 			ret = nv84_fence_create(drm);
187 			break;
188 		case FERMI_CHANNEL_GPFIFO:
189 		case KEPLER_CHANNEL_GPFIFO_A:
190 		case MAXWELL_CHANNEL_GPFIFO_A:
191 			ret = nvc0_fence_create(drm);
192 			break;
193 		default:
194 			break;
195 		}
196 	}
197 
198 	nvif_object_sclass_put(&sclass);
199 	if (ret) {
200 		NV_ERROR(drm, "failed to initialise sync subsystem, %d\n", ret);
201 		nouveau_accel_fini(drm);
202 		return;
203 	}
204 
205 	if (device->info.family >= NV_DEVICE_INFO_V0_KEPLER) {
206 		ret = nouveau_channel_new(drm, &drm->device, NVDRM_CHAN + 1,
207 					  KEPLER_CHANNEL_GPFIFO_A_V0_ENGINE_CE0|
208 					  KEPLER_CHANNEL_GPFIFO_A_V0_ENGINE_CE1,
209 					  0, &drm->cechan);
210 		if (ret)
211 			NV_ERROR(drm, "failed to create ce channel, %d\n", ret);
212 
213 		arg0 = KEPLER_CHANNEL_GPFIFO_A_V0_ENGINE_GR;
214 		arg1 = 1;
215 	} else
216 	if (device->info.chipset >= 0xa3 &&
217 	    device->info.chipset != 0xaa &&
218 	    device->info.chipset != 0xac) {
219 		ret = nouveau_channel_new(drm, &drm->device, NVDRM_CHAN + 1,
220 					  NvDmaFB, NvDmaTT, &drm->cechan);
221 		if (ret)
222 			NV_ERROR(drm, "failed to create ce channel, %d\n", ret);
223 
224 		arg0 = NvDmaFB;
225 		arg1 = NvDmaTT;
226 	} else {
227 		arg0 = NvDmaFB;
228 		arg1 = NvDmaTT;
229 	}
230 
231 	ret = nouveau_channel_new(drm, &drm->device, NVDRM_CHAN, arg0, arg1,
232 				 &drm->channel);
233 	if (ret) {
234 		NV_ERROR(drm, "failed to create kernel channel, %d\n", ret);
235 		nouveau_accel_fini(drm);
236 		return;
237 	}
238 
239 	ret = nvif_object_init(&drm->channel->user, NVDRM_NVSW,
240 			       nouveau_abi16_swclass(drm), NULL, 0, &drm->nvsw);
241 	if (ret == 0) {
242 		struct nvkm_sw_chan *swch;
243 		ret = RING_SPACE(drm->channel, 2);
244 		if (ret == 0) {
245 			if (device->info.family < NV_DEVICE_INFO_V0_FERMI) {
246 				BEGIN_NV04(drm->channel, NvSubSw, 0, 1);
247 				OUT_RING  (drm->channel, NVDRM_NVSW);
248 			} else
249 			if (device->info.family < NV_DEVICE_INFO_V0_KEPLER) {
250 				BEGIN_NVC0(drm->channel, FermiSw, 0, 1);
251 				OUT_RING  (drm->channel, 0x001f0000);
252 			}
253 		}
254 		swch = (void *)nvxx_object(&drm->nvsw)->parent;
255 		swch->flip = nouveau_flip_complete;
256 		swch->flip_data = drm->channel;
257 	}
258 
259 	if (ret) {
260 		NV_ERROR(drm, "failed to allocate software object, %d\n", ret);
261 		nouveau_accel_fini(drm);
262 		return;
263 	}
264 
265 	if (device->info.family < NV_DEVICE_INFO_V0_FERMI) {
266 		ret = nvkm_gpuobj_new(nvxx_object(&drm->device.object), NULL, 32,
267 				      0, 0, &drm->notify);
268 		if (ret) {
269 			NV_ERROR(drm, "failed to allocate notifier, %d\n", ret);
270 			nouveau_accel_fini(drm);
271 			return;
272 		}
273 
274 		ret = nvif_object_init(&drm->channel->user, NvNotify0,
275 				       NV_DMA_IN_MEMORY,
276 				       &(struct nv_dma_v0) {
277 						.target = NV_DMA_V0_TARGET_VRAM,
278 						.access = NV_DMA_V0_ACCESS_RDWR,
279 						.start = drm->notify->addr,
280 						.limit = drm->notify->addr + 31
281 				       }, sizeof(struct nv_dma_v0),
282 				       &drm->ntfy);
283 		if (ret) {
284 			nouveau_accel_fini(drm);
285 			return;
286 		}
287 	}
288 
289 
290 	nouveau_bo_move_init(drm);
291 }
292 
293 static int nouveau_drm_probe(struct pci_dev *pdev,
294 			     const struct pci_device_id *pent)
295 {
296 	struct nvkm_device *device;
297 	struct apertures_struct *aper;
298 	bool boot = false;
299 	int ret;
300 
301 	/* remove conflicting drivers (vesafb, efifb etc) */
302 	aper = alloc_apertures(3);
303 	if (!aper)
304 		return -ENOMEM;
305 
306 	aper->ranges[0].base = pci_resource_start(pdev, 1);
307 	aper->ranges[0].size = pci_resource_len(pdev, 1);
308 	aper->count = 1;
309 
310 	if (pci_resource_len(pdev, 2)) {
311 		aper->ranges[aper->count].base = pci_resource_start(pdev, 2);
312 		aper->ranges[aper->count].size = pci_resource_len(pdev, 2);
313 		aper->count++;
314 	}
315 
316 	if (pci_resource_len(pdev, 3)) {
317 		aper->ranges[aper->count].base = pci_resource_start(pdev, 3);
318 		aper->ranges[aper->count].size = pci_resource_len(pdev, 3);
319 		aper->count++;
320 	}
321 
322 #ifdef CONFIG_X86
323 	boot = pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW;
324 #endif
325 	if (nouveau_modeset != 2)
326 		remove_conflicting_framebuffers(aper, "nouveaufb", boot);
327 	kfree(aper);
328 
329 	ret = nvkm_device_new(pdev, NVKM_BUS_PCI, nouveau_pci_name(pdev),
330 			      pci_name(pdev), nouveau_config, nouveau_debug,
331 			      true, true, ~0ULL, &device);
332 	if (ret)
333 		return ret;
334 
335 	pci_set_master(pdev);
336 
337 	ret = drm_get_pci_dev(pdev, pent, &driver_pci);
338 	if (ret) {
339 		nvkm_device_del(&device);
340 		return ret;
341 	}
342 
343 	return 0;
344 }
345 
346 #define PCI_CLASS_MULTIMEDIA_HD_AUDIO 0x0403
347 
348 static void
349 nouveau_get_hdmi_dev(struct nouveau_drm *drm)
350 {
351 	struct pci_dev *pdev = drm->dev->pdev;
352 
353 	if (!pdev) {
354 		DRM_INFO("not a PCI device; no HDMI\n");
355 		drm->hdmi_device = NULL;
356 		return;
357 	}
358 
359 	/* subfunction one is a hdmi audio device? */
360 	drm->hdmi_device = pci_get_bus_and_slot((unsigned int)pdev->bus->number,
361 						PCI_DEVFN(PCI_SLOT(pdev->devfn), 1));
362 
363 	if (!drm->hdmi_device) {
364 		NV_DEBUG(drm, "hdmi device not found %d %d %d\n", pdev->bus->number, PCI_SLOT(pdev->devfn), 1);
365 		return;
366 	}
367 
368 	if ((drm->hdmi_device->class >> 8) != PCI_CLASS_MULTIMEDIA_HD_AUDIO) {
369 		NV_DEBUG(drm, "possible hdmi device not audio %d\n", drm->hdmi_device->class);
370 		pci_dev_put(drm->hdmi_device);
371 		drm->hdmi_device = NULL;
372 		return;
373 	}
374 }
375 
376 static int
377 nouveau_drm_load(struct drm_device *dev, unsigned long flags)
378 {
379 	struct nouveau_drm *drm;
380 	int ret;
381 
382 	ret = nouveau_cli_create(dev, "DRM", sizeof(*drm), (void **)&drm);
383 	if (ret)
384 		return ret;
385 
386 	dev->dev_private = drm;
387 	drm->dev = dev;
388 	nvxx_client(&drm->client.base)->debug =
389 		nvkm_dbgopt(nouveau_debug, "DRM");
390 
391 	INIT_LIST_HEAD(&drm->clients);
392 	spin_lock_init(&drm->tile.lock);
393 
394 	nouveau_get_hdmi_dev(drm);
395 
396 	ret = nvif_device_init(&drm->client.base.object,
397 			       NVDRM_DEVICE, NV_DEVICE,
398 			       &(struct nv_device_v0) {
399 					.device = ~0,
400 			       }, sizeof(struct nv_device_v0),
401 			       &drm->device);
402 	if (ret)
403 		goto fail_device;
404 
405 	dev->irq_enabled = true;
406 
407 	/* workaround an odd issue on nvc1 by disabling the device's
408 	 * nosnoop capability.  hopefully won't cause issues until a
409 	 * better fix is found - assuming there is one...
410 	 */
411 	if (drm->device.info.chipset == 0xc1)
412 		nvif_mask(&drm->device.object, 0x00088080, 0x00000800, 0x00000000);
413 
414 	nouveau_vga_init(drm);
415 	nouveau_agp_init(drm);
416 
417 	if (drm->device.info.family >= NV_DEVICE_INFO_V0_TESLA) {
418 		ret = nvkm_vm_new(nvxx_device(&drm->device), 0, (1ULL << 40),
419 				  0x1000, &drm->client.vm);
420 		if (ret)
421 			goto fail_device;
422 
423 		nvxx_client(&drm->client.base)->vm = drm->client.vm;
424 	}
425 
426 	ret = nouveau_ttm_init(drm);
427 	if (ret)
428 		goto fail_ttm;
429 
430 	ret = nouveau_bios_init(dev);
431 	if (ret)
432 		goto fail_bios;
433 
434 	ret = nouveau_display_create(dev);
435 	if (ret)
436 		goto fail_dispctor;
437 
438 	if (dev->mode_config.num_crtc) {
439 		ret = nouveau_display_init(dev);
440 		if (ret)
441 			goto fail_dispinit;
442 	}
443 
444 	nouveau_sysfs_init(dev);
445 	nouveau_hwmon_init(dev);
446 	nouveau_accel_init(drm);
447 	nouveau_fbcon_init(dev);
448 
449 	if (nouveau_runtime_pm != 0) {
450 		pm_runtime_use_autosuspend(dev->dev);
451 		pm_runtime_set_autosuspend_delay(dev->dev, 5000);
452 		pm_runtime_set_active(dev->dev);
453 		pm_runtime_allow(dev->dev);
454 		pm_runtime_mark_last_busy(dev->dev);
455 		pm_runtime_put(dev->dev);
456 	}
457 	return 0;
458 
459 fail_dispinit:
460 	nouveau_display_destroy(dev);
461 fail_dispctor:
462 	nouveau_bios_takedown(dev);
463 fail_bios:
464 	nouveau_ttm_fini(drm);
465 fail_ttm:
466 	nouveau_agp_fini(drm);
467 	nouveau_vga_fini(drm);
468 fail_device:
469 	nvif_device_fini(&drm->device);
470 	nouveau_cli_destroy(&drm->client);
471 	return ret;
472 }
473 
474 static int
475 nouveau_drm_unload(struct drm_device *dev)
476 {
477 	struct nouveau_drm *drm = nouveau_drm(dev);
478 
479 	pm_runtime_get_sync(dev->dev);
480 	nouveau_fbcon_fini(dev);
481 	nouveau_accel_fini(drm);
482 	nouveau_hwmon_fini(dev);
483 	nouveau_sysfs_fini(dev);
484 
485 	if (dev->mode_config.num_crtc)
486 		nouveau_display_fini(dev);
487 	nouveau_display_destroy(dev);
488 
489 	nouveau_bios_takedown(dev);
490 
491 	nouveau_ttm_fini(drm);
492 	nouveau_agp_fini(drm);
493 	nouveau_vga_fini(drm);
494 
495 	nvif_device_fini(&drm->device);
496 	if (drm->hdmi_device)
497 		pci_dev_put(drm->hdmi_device);
498 	nouveau_cli_destroy(&drm->client);
499 	return 0;
500 }
501 
502 void
503 nouveau_drm_device_remove(struct drm_device *dev)
504 {
505 	struct nouveau_drm *drm = nouveau_drm(dev);
506 	struct nvkm_client *client;
507 	struct nvkm_device *device;
508 
509 	dev->irq_enabled = false;
510 	client = nvxx_client(&drm->client.base);
511 	device = nvkm_device_find(client->device);
512 	drm_put_dev(dev);
513 
514 	nvkm_device_del(&device);
515 }
516 
517 static void
518 nouveau_drm_remove(struct pci_dev *pdev)
519 {
520 	struct drm_device *dev = pci_get_drvdata(pdev);
521 
522 	nouveau_drm_device_remove(dev);
523 }
524 
525 static int
526 nouveau_do_suspend(struct drm_device *dev, bool runtime)
527 {
528 	struct nouveau_drm *drm = nouveau_drm(dev);
529 	struct nouveau_cli *cli;
530 	int ret;
531 
532 	if (dev->mode_config.num_crtc) {
533 		NV_INFO(drm, "suspending console...\n");
534 		nouveau_fbcon_set_suspend(dev, 1);
535 		NV_INFO(drm, "suspending display...\n");
536 		ret = nouveau_display_suspend(dev, runtime);
537 		if (ret)
538 			return ret;
539 	}
540 
541 	NV_INFO(drm, "evicting buffers...\n");
542 	ttm_bo_evict_mm(&drm->ttm.bdev, TTM_PL_VRAM);
543 
544 	NV_INFO(drm, "waiting for kernel channels to go idle...\n");
545 	if (drm->cechan) {
546 		ret = nouveau_channel_idle(drm->cechan);
547 		if (ret)
548 			goto fail_display;
549 	}
550 
551 	if (drm->channel) {
552 		ret = nouveau_channel_idle(drm->channel);
553 		if (ret)
554 			goto fail_display;
555 	}
556 
557 	NV_INFO(drm, "suspending client object trees...\n");
558 	if (drm->fence && nouveau_fence(drm)->suspend) {
559 		if (!nouveau_fence(drm)->suspend(drm)) {
560 			ret = -ENOMEM;
561 			goto fail_display;
562 		}
563 	}
564 
565 	list_for_each_entry(cli, &drm->clients, head) {
566 		ret = nvif_client_suspend(&cli->base);
567 		if (ret)
568 			goto fail_client;
569 	}
570 
571 	NV_INFO(drm, "suspending kernel object tree...\n");
572 	ret = nvif_client_suspend(&drm->client.base);
573 	if (ret)
574 		goto fail_client;
575 
576 	nouveau_agp_fini(drm);
577 	return 0;
578 
579 fail_client:
580 	list_for_each_entry_continue_reverse(cli, &drm->clients, head) {
581 		nvif_client_resume(&cli->base);
582 	}
583 
584 	if (drm->fence && nouveau_fence(drm)->resume)
585 		nouveau_fence(drm)->resume(drm);
586 
587 fail_display:
588 	if (dev->mode_config.num_crtc) {
589 		NV_INFO(drm, "resuming display...\n");
590 		nouveau_display_resume(dev, runtime);
591 	}
592 	return ret;
593 }
594 
595 static int
596 nouveau_do_resume(struct drm_device *dev, bool runtime)
597 {
598 	struct nouveau_drm *drm = nouveau_drm(dev);
599 	struct nouveau_cli *cli;
600 
601 	NV_INFO(drm, "re-enabling device...\n");
602 
603 	nouveau_agp_reset(drm);
604 
605 	NV_INFO(drm, "resuming kernel object tree...\n");
606 	nvif_client_resume(&drm->client.base);
607 	nouveau_agp_init(drm);
608 
609 	NV_INFO(drm, "resuming client object trees...\n");
610 	if (drm->fence && nouveau_fence(drm)->resume)
611 		nouveau_fence(drm)->resume(drm);
612 
613 	list_for_each_entry(cli, &drm->clients, head) {
614 		nvif_client_resume(&cli->base);
615 	}
616 
617 	nouveau_run_vbios_init(dev);
618 
619 	if (dev->mode_config.num_crtc) {
620 		NV_INFO(drm, "resuming display...\n");
621 		nouveau_display_resume(dev, runtime);
622 		NV_INFO(drm, "resuming console...\n");
623 		nouveau_fbcon_set_suspend(dev, 0);
624 	}
625 
626 	return 0;
627 }
628 
629 int
630 nouveau_pmops_suspend(struct device *dev)
631 {
632 	struct pci_dev *pdev = to_pci_dev(dev);
633 	struct drm_device *drm_dev = pci_get_drvdata(pdev);
634 	int ret;
635 
636 	if (drm_dev->switch_power_state == DRM_SWITCH_POWER_OFF ||
637 	    drm_dev->switch_power_state == DRM_SWITCH_POWER_DYNAMIC_OFF)
638 		return 0;
639 
640 	ret = nouveau_do_suspend(drm_dev, false);
641 	if (ret)
642 		return ret;
643 
644 	pci_save_state(pdev);
645 	pci_disable_device(pdev);
646 	pci_set_power_state(pdev, PCI_D3hot);
647 	udelay(200);
648 	return 0;
649 }
650 
651 int
652 nouveau_pmops_resume(struct device *dev)
653 {
654 	struct pci_dev *pdev = to_pci_dev(dev);
655 	struct drm_device *drm_dev = pci_get_drvdata(pdev);
656 	int ret;
657 
658 	if (drm_dev->switch_power_state == DRM_SWITCH_POWER_OFF ||
659 	    drm_dev->switch_power_state == DRM_SWITCH_POWER_DYNAMIC_OFF)
660 		return 0;
661 
662 	pci_set_power_state(pdev, PCI_D0);
663 	pci_restore_state(pdev);
664 	ret = pci_enable_device(pdev);
665 	if (ret)
666 		return ret;
667 	pci_set_master(pdev);
668 
669 	return nouveau_do_resume(drm_dev, false);
670 }
671 
672 static int
673 nouveau_pmops_freeze(struct device *dev)
674 {
675 	struct pci_dev *pdev = to_pci_dev(dev);
676 	struct drm_device *drm_dev = pci_get_drvdata(pdev);
677 	return nouveau_do_suspend(drm_dev, false);
678 }
679 
680 static int
681 nouveau_pmops_thaw(struct device *dev)
682 {
683 	struct pci_dev *pdev = to_pci_dev(dev);
684 	struct drm_device *drm_dev = pci_get_drvdata(pdev);
685 	return nouveau_do_resume(drm_dev, false);
686 }
687 
688 static int
689 nouveau_pmops_runtime_suspend(struct device *dev)
690 {
691 	struct pci_dev *pdev = to_pci_dev(dev);
692 	struct drm_device *drm_dev = pci_get_drvdata(pdev);
693 	int ret;
694 
695 	if (nouveau_runtime_pm == 0) {
696 		pm_runtime_forbid(dev);
697 		return -EBUSY;
698 	}
699 
700 	/* are we optimus enabled? */
701 	if (nouveau_runtime_pm == -1 && !nouveau_is_optimus() && !nouveau_is_v1_dsm()) {
702 		DRM_DEBUG_DRIVER("failing to power off - not optimus\n");
703 		pm_runtime_forbid(dev);
704 		return -EBUSY;
705 	}
706 
707 	drm_kms_helper_poll_disable(drm_dev);
708 	vga_switcheroo_set_dynamic_switch(pdev, VGA_SWITCHEROO_OFF);
709 	nouveau_switcheroo_optimus_dsm();
710 	ret = nouveau_do_suspend(drm_dev, true);
711 	pci_save_state(pdev);
712 	pci_disable_device(pdev);
713 	pci_ignore_hotplug(pdev);
714 	pci_set_power_state(pdev, PCI_D3cold);
715 	drm_dev->switch_power_state = DRM_SWITCH_POWER_DYNAMIC_OFF;
716 	return ret;
717 }
718 
719 static int
720 nouveau_pmops_runtime_resume(struct device *dev)
721 {
722 	struct pci_dev *pdev = to_pci_dev(dev);
723 	struct drm_device *drm_dev = pci_get_drvdata(pdev);
724 	struct nvif_device *device = &nouveau_drm(drm_dev)->device;
725 	int ret;
726 
727 	if (nouveau_runtime_pm == 0)
728 		return -EINVAL;
729 
730 	pci_set_power_state(pdev, PCI_D0);
731 	pci_restore_state(pdev);
732 	ret = pci_enable_device(pdev);
733 	if (ret)
734 		return ret;
735 	pci_set_master(pdev);
736 
737 	ret = nouveau_do_resume(drm_dev, true);
738 	drm_kms_helper_poll_enable(drm_dev);
739 	/* do magic */
740 	nvif_mask(&device->object, 0x088488, (1 << 25), (1 << 25));
741 	vga_switcheroo_set_dynamic_switch(pdev, VGA_SWITCHEROO_ON);
742 	drm_dev->switch_power_state = DRM_SWITCH_POWER_ON;
743 	return ret;
744 }
745 
746 static int
747 nouveau_pmops_runtime_idle(struct device *dev)
748 {
749 	struct pci_dev *pdev = to_pci_dev(dev);
750 	struct drm_device *drm_dev = pci_get_drvdata(pdev);
751 	struct nouveau_drm *drm = nouveau_drm(drm_dev);
752 	struct drm_crtc *crtc;
753 
754 	if (nouveau_runtime_pm == 0) {
755 		pm_runtime_forbid(dev);
756 		return -EBUSY;
757 	}
758 
759 	/* are we optimus enabled? */
760 	if (nouveau_runtime_pm == -1 && !nouveau_is_optimus() && !nouveau_is_v1_dsm()) {
761 		DRM_DEBUG_DRIVER("failing to power off - not optimus\n");
762 		pm_runtime_forbid(dev);
763 		return -EBUSY;
764 	}
765 
766 	/* if we have a hdmi audio device - make sure it has a driver loaded */
767 	if (drm->hdmi_device) {
768 		if (!drm->hdmi_device->driver) {
769 			DRM_DEBUG_DRIVER("failing to power off - no HDMI audio driver loaded\n");
770 			pm_runtime_mark_last_busy(dev);
771 			return -EBUSY;
772 		}
773 	}
774 
775 	list_for_each_entry(crtc, &drm->dev->mode_config.crtc_list, head) {
776 		if (crtc->enabled) {
777 			DRM_DEBUG_DRIVER("failing to power off - crtc active\n");
778 			return -EBUSY;
779 		}
780 	}
781 	pm_runtime_mark_last_busy(dev);
782 	pm_runtime_autosuspend(dev);
783 	/* we don't want the main rpm_idle to call suspend - we want to autosuspend */
784 	return 1;
785 }
786 
787 static int
788 nouveau_drm_open(struct drm_device *dev, struct drm_file *fpriv)
789 {
790 	struct nouveau_drm *drm = nouveau_drm(dev);
791 	struct nouveau_cli *cli;
792 	char name[32], tmpname[TASK_COMM_LEN];
793 	int ret;
794 
795 	/* need to bring up power immediately if opening device */
796 	ret = pm_runtime_get_sync(dev->dev);
797 	if (ret < 0 && ret != -EACCES)
798 		return ret;
799 
800 	get_task_comm(tmpname, current);
801 	snprintf(name, sizeof(name), "%s[%d]", tmpname, pid_nr(fpriv->pid));
802 
803 	ret = nouveau_cli_create(dev, name, sizeof(*cli), (void **)&cli);
804 
805 	if (ret)
806 		goto out_suspend;
807 
808 	cli->base.super = false;
809 
810 	if (drm->device.info.family >= NV_DEVICE_INFO_V0_TESLA) {
811 		ret = nvkm_vm_new(nvxx_device(&drm->device), 0, (1ULL << 40),
812 				  0x1000, &cli->vm);
813 		if (ret) {
814 			nouveau_cli_destroy(cli);
815 			goto out_suspend;
816 		}
817 
818 		nvxx_client(&cli->base)->vm = cli->vm;
819 	}
820 
821 	fpriv->driver_priv = cli;
822 
823 	mutex_lock(&drm->client.mutex);
824 	list_add(&cli->head, &drm->clients);
825 	mutex_unlock(&drm->client.mutex);
826 
827 out_suspend:
828 	pm_runtime_mark_last_busy(dev->dev);
829 	pm_runtime_put_autosuspend(dev->dev);
830 
831 	return ret;
832 }
833 
834 static void
835 nouveau_drm_preclose(struct drm_device *dev, struct drm_file *fpriv)
836 {
837 	struct nouveau_cli *cli = nouveau_cli(fpriv);
838 	struct nouveau_drm *drm = nouveau_drm(dev);
839 
840 	pm_runtime_get_sync(dev->dev);
841 
842 	mutex_lock(&cli->mutex);
843 	if (cli->abi16)
844 		nouveau_abi16_fini(cli->abi16);
845 	mutex_unlock(&cli->mutex);
846 
847 	mutex_lock(&drm->client.mutex);
848 	list_del(&cli->head);
849 	mutex_unlock(&drm->client.mutex);
850 
851 }
852 
853 static void
854 nouveau_drm_postclose(struct drm_device *dev, struct drm_file *fpriv)
855 {
856 	struct nouveau_cli *cli = nouveau_cli(fpriv);
857 	nouveau_cli_destroy(cli);
858 	pm_runtime_mark_last_busy(dev->dev);
859 	pm_runtime_put_autosuspend(dev->dev);
860 }
861 
862 static const struct drm_ioctl_desc
863 nouveau_ioctls[] = {
864 	DRM_IOCTL_DEF_DRV(NOUVEAU_GETPARAM, nouveau_abi16_ioctl_getparam, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
865 	DRM_IOCTL_DEF_DRV(NOUVEAU_SETPARAM, nouveau_abi16_ioctl_setparam, DRM_UNLOCKED|DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
866 	DRM_IOCTL_DEF_DRV(NOUVEAU_CHANNEL_ALLOC, nouveau_abi16_ioctl_channel_alloc, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
867 	DRM_IOCTL_DEF_DRV(NOUVEAU_CHANNEL_FREE, nouveau_abi16_ioctl_channel_free, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
868 	DRM_IOCTL_DEF_DRV(NOUVEAU_GROBJ_ALLOC, nouveau_abi16_ioctl_grobj_alloc, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
869 	DRM_IOCTL_DEF_DRV(NOUVEAU_NOTIFIEROBJ_ALLOC, nouveau_abi16_ioctl_notifierobj_alloc, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
870 	DRM_IOCTL_DEF_DRV(NOUVEAU_GPUOBJ_FREE, nouveau_abi16_ioctl_gpuobj_free, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
871 	DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_NEW, nouveau_gem_ioctl_new, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
872 	DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_PUSHBUF, nouveau_gem_ioctl_pushbuf, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
873 	DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_CPU_PREP, nouveau_gem_ioctl_cpu_prep, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
874 	DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_CPU_FINI, nouveau_gem_ioctl_cpu_fini, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
875 	DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_INFO, nouveau_gem_ioctl_info, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
876 };
877 
878 long
879 nouveau_drm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
880 {
881 	struct drm_file *filp = file->private_data;
882 	struct drm_device *dev = filp->minor->dev;
883 	long ret;
884 
885 	ret = pm_runtime_get_sync(dev->dev);
886 	if (ret < 0 && ret != -EACCES)
887 		return ret;
888 
889 	switch (_IOC_NR(cmd) - DRM_COMMAND_BASE) {
890 	case DRM_NOUVEAU_NVIF:
891 		ret = usif_ioctl(filp, (void __user *)arg, _IOC_SIZE(cmd));
892 		break;
893 	default:
894 		ret = drm_ioctl(file, cmd, arg);
895 		break;
896 	}
897 
898 	pm_runtime_mark_last_busy(dev->dev);
899 	pm_runtime_put_autosuspend(dev->dev);
900 	return ret;
901 }
902 
903 static const struct file_operations
904 nouveau_driver_fops = {
905 	.owner = THIS_MODULE,
906 	.open = drm_open,
907 	.release = drm_release,
908 	.unlocked_ioctl = nouveau_drm_ioctl,
909 	.mmap = nouveau_ttm_mmap,
910 	.poll = drm_poll,
911 	.read = drm_read,
912 #if defined(CONFIG_COMPAT)
913 	.compat_ioctl = nouveau_compat_ioctl,
914 #endif
915 	.llseek = noop_llseek,
916 };
917 
918 static struct drm_driver
919 driver_stub = {
920 	.driver_features =
921 		DRIVER_USE_AGP |
922 		DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME | DRIVER_RENDER |
923 		DRIVER_KMS_LEGACY_CONTEXT,
924 
925 	.load = nouveau_drm_load,
926 	.unload = nouveau_drm_unload,
927 	.open = nouveau_drm_open,
928 	.preclose = nouveau_drm_preclose,
929 	.postclose = nouveau_drm_postclose,
930 	.lastclose = nouveau_vga_lastclose,
931 
932 #if defined(CONFIG_DEBUG_FS)
933 	.debugfs_init = nouveau_debugfs_init,
934 	.debugfs_cleanup = nouveau_debugfs_takedown,
935 #endif
936 
937 	.get_vblank_counter = drm_vblank_count,
938 	.enable_vblank = nouveau_display_vblank_enable,
939 	.disable_vblank = nouveau_display_vblank_disable,
940 	.get_scanout_position = nouveau_display_scanoutpos,
941 	.get_vblank_timestamp = nouveau_display_vblstamp,
942 
943 	.ioctls = nouveau_ioctls,
944 	.num_ioctls = ARRAY_SIZE(nouveau_ioctls),
945 	.fops = &nouveau_driver_fops,
946 
947 	.prime_handle_to_fd = drm_gem_prime_handle_to_fd,
948 	.prime_fd_to_handle = drm_gem_prime_fd_to_handle,
949 	.gem_prime_export = drm_gem_prime_export,
950 	.gem_prime_import = drm_gem_prime_import,
951 	.gem_prime_pin = nouveau_gem_prime_pin,
952 	.gem_prime_res_obj = nouveau_gem_prime_res_obj,
953 	.gem_prime_unpin = nouveau_gem_prime_unpin,
954 	.gem_prime_get_sg_table = nouveau_gem_prime_get_sg_table,
955 	.gem_prime_import_sg_table = nouveau_gem_prime_import_sg_table,
956 	.gem_prime_vmap = nouveau_gem_prime_vmap,
957 	.gem_prime_vunmap = nouveau_gem_prime_vunmap,
958 
959 	.gem_free_object = nouveau_gem_object_del,
960 	.gem_open_object = nouveau_gem_object_open,
961 	.gem_close_object = nouveau_gem_object_close,
962 
963 	.dumb_create = nouveau_display_dumb_create,
964 	.dumb_map_offset = nouveau_display_dumb_map_offset,
965 	.dumb_destroy = drm_gem_dumb_destroy,
966 
967 	.name = DRIVER_NAME,
968 	.desc = DRIVER_DESC,
969 #ifdef GIT_REVISION
970 	.date = GIT_REVISION,
971 #else
972 	.date = DRIVER_DATE,
973 #endif
974 	.major = DRIVER_MAJOR,
975 	.minor = DRIVER_MINOR,
976 	.patchlevel = DRIVER_PATCHLEVEL,
977 };
978 
979 static struct pci_device_id
980 nouveau_drm_pci_table[] = {
981 	{
982 		PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_ANY_ID),
983 		.class = PCI_BASE_CLASS_DISPLAY << 16,
984 		.class_mask  = 0xff << 16,
985 	},
986 	{
987 		PCI_DEVICE(PCI_VENDOR_ID_NVIDIA_SGS, PCI_ANY_ID),
988 		.class = PCI_BASE_CLASS_DISPLAY << 16,
989 		.class_mask  = 0xff << 16,
990 	},
991 	{}
992 };
993 
994 static void nouveau_display_options(void)
995 {
996 	DRM_DEBUG_DRIVER("Loading Nouveau with parameters:\n");
997 
998 	DRM_DEBUG_DRIVER("... tv_disable   : %d\n", nouveau_tv_disable);
999 	DRM_DEBUG_DRIVER("... ignorelid    : %d\n", nouveau_ignorelid);
1000 	DRM_DEBUG_DRIVER("... duallink     : %d\n", nouveau_duallink);
1001 	DRM_DEBUG_DRIVER("... nofbaccel    : %d\n", nouveau_nofbaccel);
1002 	DRM_DEBUG_DRIVER("... config       : %s\n", nouveau_config);
1003 	DRM_DEBUG_DRIVER("... debug        : %s\n", nouveau_debug);
1004 	DRM_DEBUG_DRIVER("... noaccel      : %d\n", nouveau_noaccel);
1005 	DRM_DEBUG_DRIVER("... modeset      : %d\n", nouveau_modeset);
1006 	DRM_DEBUG_DRIVER("... runpm        : %d\n", nouveau_runtime_pm);
1007 	DRM_DEBUG_DRIVER("... vram_pushbuf : %d\n", nouveau_vram_pushbuf);
1008 	DRM_DEBUG_DRIVER("... pstate       : %d\n", nouveau_pstate);
1009 }
1010 
1011 static const struct dev_pm_ops nouveau_pm_ops = {
1012 	.suspend = nouveau_pmops_suspend,
1013 	.resume = nouveau_pmops_resume,
1014 	.freeze = nouveau_pmops_freeze,
1015 	.thaw = nouveau_pmops_thaw,
1016 	.poweroff = nouveau_pmops_freeze,
1017 	.restore = nouveau_pmops_resume,
1018 	.runtime_suspend = nouveau_pmops_runtime_suspend,
1019 	.runtime_resume = nouveau_pmops_runtime_resume,
1020 	.runtime_idle = nouveau_pmops_runtime_idle,
1021 };
1022 
1023 static struct pci_driver
1024 nouveau_drm_pci_driver = {
1025 	.name = "nouveau",
1026 	.id_table = nouveau_drm_pci_table,
1027 	.probe = nouveau_drm_probe,
1028 	.remove = nouveau_drm_remove,
1029 	.driver.pm = &nouveau_pm_ops,
1030 };
1031 
1032 struct drm_device *
1033 nouveau_platform_device_create(struct platform_device *pdev,
1034 			       struct nvkm_device **pdevice)
1035 {
1036 	struct drm_device *drm;
1037 	int err;
1038 
1039 	err = nvkm_device_new(pdev, NVKM_BUS_PLATFORM,
1040 			      nouveau_platform_name(pdev),
1041 			      dev_name(&pdev->dev), nouveau_config,
1042 			      nouveau_debug, true, true, ~0ULL,
1043 			      pdevice);
1044 	if (err)
1045 		goto err_free;
1046 
1047 	drm = drm_dev_alloc(&driver_platform, &pdev->dev);
1048 	if (!drm) {
1049 		err = -ENOMEM;
1050 		goto err_free;
1051 	}
1052 
1053 	err = drm_dev_set_unique(drm, "%s", dev_name(&pdev->dev));
1054 	if (err < 0)
1055 		goto err_free;
1056 
1057 	drm->platformdev = pdev;
1058 	platform_set_drvdata(pdev, drm);
1059 
1060 	return drm;
1061 
1062 err_free:
1063 	nvkm_device_del(pdevice);
1064 
1065 	return ERR_PTR(err);
1066 }
1067 
1068 static int __init
1069 nouveau_drm_init(void)
1070 {
1071 	driver_pci = driver_stub;
1072 	driver_pci.set_busid = drm_pci_set_busid;
1073 	driver_platform = driver_stub;
1074 	driver_platform.set_busid = drm_platform_set_busid;
1075 
1076 	nouveau_display_options();
1077 
1078 	if (nouveau_modeset == -1) {
1079 #ifdef CONFIG_VGA_CONSOLE
1080 		if (vgacon_text_force())
1081 			nouveau_modeset = 0;
1082 #endif
1083 	}
1084 
1085 	if (!nouveau_modeset)
1086 		return 0;
1087 
1088 #ifdef CONFIG_NOUVEAU_PLATFORM_DRIVER
1089 	platform_driver_register(&nouveau_platform_driver);
1090 #endif
1091 
1092 	nouveau_register_dsm_handler();
1093 	return drm_pci_init(&driver_pci, &nouveau_drm_pci_driver);
1094 }
1095 
1096 static void __exit
1097 nouveau_drm_exit(void)
1098 {
1099 	if (!nouveau_modeset)
1100 		return;
1101 
1102 	drm_pci_exit(&driver_pci, &nouveau_drm_pci_driver);
1103 	nouveau_unregister_dsm_handler();
1104 
1105 #ifdef CONFIG_NOUVEAU_PLATFORM_DRIVER
1106 	platform_driver_unregister(&nouveau_platform_driver);
1107 #endif
1108 }
1109 
1110 module_init(nouveau_drm_init);
1111 module_exit(nouveau_drm_exit);
1112 
1113 MODULE_DEVICE_TABLE(pci, nouveau_drm_pci_table);
1114 MODULE_AUTHOR(DRIVER_AUTHOR);
1115 MODULE_DESCRIPTION(DRIVER_DESC);
1116 MODULE_LICENSE("GPL and additional rights");
1117