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