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