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