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