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