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