2 * Copyright 2012 Red Hat Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
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>
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>
37 #include <engine/device.h>
38 #include <engine/disp.h>
39 #include <engine/fifo.h>
40 #include <engine/software.h>
42 #include <subdev/vm.h>
44 #include "nouveau_drm.h"
45 #include "nouveau_dma.h"
46 #include "nouveau_ttm.h"
47 #include "nouveau_gem.h"
48 #include "nouveau_agp.h"
49 #include "nouveau_vga.h"
50 #include "nouveau_sysfs.h"
51 #include "nouveau_hwmon.h"
52 #include "nouveau_acpi.h"
53 #include "nouveau_bios.h"
54 #include "nouveau_ioctl.h"
55 #include "nouveau_abi16.h"
56 #include "nouveau_fbcon.h"
57 #include "nouveau_fence.h"
58 #include "nouveau_debugfs.h"
60 MODULE_PARM_DESC(config, "option string to pass to driver core");
61 static char *nouveau_config;
62 module_param_named(config, nouveau_config, charp, 0400);
64 MODULE_PARM_DESC(debug, "debug string to pass to driver core");
65 static char *nouveau_debug;
66 module_param_named(debug, nouveau_debug, charp, 0400);
68 MODULE_PARM_DESC(noaccel, "disable kernel/abi16 acceleration");
69 static int nouveau_noaccel = 0;
70 module_param_named(noaccel, nouveau_noaccel, int, 0400);
72 MODULE_PARM_DESC(modeset, "enable driver (default: auto, "
73 "0 = disabled, 1 = enabled, 2 = headless)");
74 int nouveau_modeset = -1;
75 module_param_named(modeset, nouveau_modeset, int, 0400);
77 MODULE_PARM_DESC(runpm, "disable (0), force enable (1), optimus only default (-1)");
78 int nouveau_runtime_pm = -1;
79 module_param_named(runpm, nouveau_runtime_pm, int, 0400);
81 static struct drm_driver driver;
84 nouveau_name(struct pci_dev *pdev)
86 u64 name = (u64)pci_domain_nr(pdev->bus) << 32;
87 name |= pdev->bus->number << 16;
88 name |= PCI_SLOT(pdev->devfn) << 8;
89 return name | PCI_FUNC(pdev->devfn);
93 nouveau_cli_create(struct pci_dev *pdev, const char *name,
94 int size, void **pcli)
96 struct nouveau_cli *cli;
100 ret = nouveau_client_create_(name, nouveau_name(pdev), nouveau_config,
101 nouveau_debug, size, pcli);
105 nouveau_client_destroy(&cli->base);
110 mutex_init(&cli->mutex);
115 nouveau_cli_destroy(struct nouveau_cli *cli)
117 struct nouveau_object *client = nv_object(cli);
118 nouveau_vm_ref(NULL, &cli->base.vm, NULL);
119 nouveau_client_fini(&cli->base, false);
120 atomic_set(&client->refcount, 1);
121 nouveau_object_ref(NULL, &client);
125 nouveau_accel_fini(struct nouveau_drm *drm)
127 nouveau_gpuobj_ref(NULL, &drm->notify);
128 nouveau_channel_del(&drm->channel);
129 nouveau_channel_del(&drm->cechan);
131 nouveau_fence(drm)->dtor(drm);
135 nouveau_accel_init(struct nouveau_drm *drm)
137 struct nouveau_device *device = nv_device(drm->device);
138 struct nouveau_object *object;
142 if (nouveau_noaccel || !nouveau_fifo(device) /*XXX*/)
145 /* initialise synchronisation routines */
146 if (device->card_type < NV_10) ret = nv04_fence_create(drm);
147 else if (device->card_type < NV_11 ||
148 device->chipset < 0x17) ret = nv10_fence_create(drm);
149 else if (device->card_type < NV_50) ret = nv17_fence_create(drm);
150 else if (device->chipset < 0x84) ret = nv50_fence_create(drm);
151 else if (device->card_type < NV_C0) ret = nv84_fence_create(drm);
152 else ret = nvc0_fence_create(drm);
154 NV_ERROR(drm, "failed to initialise sync subsystem, %d\n", ret);
155 nouveau_accel_fini(drm);
159 if (device->card_type >= NV_E0) {
160 ret = nouveau_channel_new(drm, &drm->client, NVDRM_DEVICE,
162 NVE0_CHANNEL_IND_ENGINE_CE0 |
163 NVE0_CHANNEL_IND_ENGINE_CE1, 0,
166 NV_ERROR(drm, "failed to create ce channel, %d\n", ret);
168 arg0 = NVE0_CHANNEL_IND_ENGINE_GR;
171 if (device->chipset >= 0xa3 &&
172 device->chipset != 0xaa &&
173 device->chipset != 0xac) {
174 ret = nouveau_channel_new(drm, &drm->client, NVDRM_DEVICE,
175 NVDRM_CHAN + 1, NvDmaFB, NvDmaTT,
178 NV_ERROR(drm, "failed to create ce channel, %d\n", ret);
187 ret = nouveau_channel_new(drm, &drm->client, NVDRM_DEVICE, NVDRM_CHAN,
188 arg0, arg1, &drm->channel);
190 NV_ERROR(drm, "failed to create kernel channel, %d\n", ret);
191 nouveau_accel_fini(drm);
195 ret = nouveau_object_new(nv_object(drm), NVDRM_CHAN, NVDRM_NVSW,
196 nouveau_abi16_swclass(drm), NULL, 0, &object);
198 struct nouveau_software_chan *swch = (void *)object->parent;
199 ret = RING_SPACE(drm->channel, 2);
201 if (device->card_type < NV_C0) {
202 BEGIN_NV04(drm->channel, NvSubSw, 0, 1);
203 OUT_RING (drm->channel, NVDRM_NVSW);
205 if (device->card_type < NV_E0) {
206 BEGIN_NVC0(drm->channel, FermiSw, 0, 1);
207 OUT_RING (drm->channel, 0x001f0000);
210 swch = (void *)object->parent;
211 swch->flip = nouveau_flip_complete;
212 swch->flip_data = drm->channel;
216 NV_ERROR(drm, "failed to allocate software object, %d\n", ret);
217 nouveau_accel_fini(drm);
221 if (device->card_type < NV_C0) {
222 ret = nouveau_gpuobj_new(drm->device, NULL, 32, 0, 0,
225 NV_ERROR(drm, "failed to allocate notifier, %d\n", ret);
226 nouveau_accel_fini(drm);
230 ret = nouveau_object_new(nv_object(drm),
231 drm->channel->handle, NvNotify0,
232 0x003d, &(struct nv_dma_class) {
233 .flags = NV_DMA_TARGET_VRAM |
235 .start = drm->notify->addr,
236 .limit = drm->notify->addr + 31
237 }, sizeof(struct nv_dma_class),
240 nouveau_accel_fini(drm);
246 nouveau_bo_move_init(drm);
249 static int nouveau_drm_probe(struct pci_dev *pdev,
250 const struct pci_device_id *pent)
252 struct nouveau_device *device;
253 struct apertures_struct *aper;
257 /* remove conflicting drivers (vesafb, efifb etc) */
258 aper = alloc_apertures(3);
262 aper->ranges[0].base = pci_resource_start(pdev, 1);
263 aper->ranges[0].size = pci_resource_len(pdev, 1);
266 if (pci_resource_len(pdev, 2)) {
267 aper->ranges[aper->count].base = pci_resource_start(pdev, 2);
268 aper->ranges[aper->count].size = pci_resource_len(pdev, 2);
272 if (pci_resource_len(pdev, 3)) {
273 aper->ranges[aper->count].base = pci_resource_start(pdev, 3);
274 aper->ranges[aper->count].size = pci_resource_len(pdev, 3);
279 boot = pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW;
281 remove_conflicting_framebuffers(aper, "nouveaufb", boot);
284 ret = nouveau_device_create(pdev, nouveau_name(pdev), pci_name(pdev),
285 nouveau_config, nouveau_debug, &device);
289 pci_set_master(pdev);
291 ret = drm_get_pci_dev(pdev, pent, &driver);
293 nouveau_object_ref(NULL, (struct nouveau_object **)&device);
300 #define PCI_CLASS_MULTIMEDIA_HD_AUDIO 0x0403
303 nouveau_get_hdmi_dev(struct drm_device *dev)
305 struct nouveau_drm *drm = dev->dev_private;
306 struct pci_dev *pdev = dev->pdev;
308 /* subfunction one is a hdmi audio device? */
309 drm->hdmi_device = pci_get_bus_and_slot((unsigned int)pdev->bus->number,
310 PCI_DEVFN(PCI_SLOT(pdev->devfn), 1));
312 if (!drm->hdmi_device) {
313 DRM_INFO("hdmi device not found %d %d %d\n", pdev->bus->number, PCI_SLOT(pdev->devfn), 1);
317 if ((drm->hdmi_device->class >> 8) != PCI_CLASS_MULTIMEDIA_HD_AUDIO) {
318 DRM_INFO("possible hdmi device not audio %d\n", drm->hdmi_device->class);
319 pci_dev_put(drm->hdmi_device);
320 drm->hdmi_device = NULL;
326 nouveau_drm_load(struct drm_device *dev, unsigned long flags)
328 struct pci_dev *pdev = dev->pdev;
329 struct nouveau_device *device;
330 struct nouveau_drm *drm;
333 ret = nouveau_cli_create(pdev, "DRM", sizeof(*drm), (void**)&drm);
337 dev->dev_private = drm;
340 INIT_LIST_HEAD(&drm->clients);
341 spin_lock_init(&drm->tile.lock);
343 nouveau_get_hdmi_dev(dev);
345 /* make sure AGP controller is in a consistent state before we
346 * (possibly) execute vbios init tables (see nouveau_agp.h)
348 if (drm_pci_device_is_agp(dev) && dev->agp) {
349 /* dummy device object, doesn't init anything, but allows
350 * agp code access to registers
352 ret = nouveau_object_new(nv_object(drm), NVDRM_CLIENT,
353 NVDRM_DEVICE, 0x0080,
354 &(struct nv_device_class) {
357 ~(NV_DEVICE_DISABLE_MMIO |
358 NV_DEVICE_DISABLE_IDENTIFY),
360 }, sizeof(struct nv_device_class),
365 nouveau_agp_reset(drm);
366 nouveau_object_del(nv_object(drm), NVDRM_CLIENT, NVDRM_DEVICE);
369 ret = nouveau_object_new(nv_object(drm), NVDRM_CLIENT, NVDRM_DEVICE,
370 0x0080, &(struct nv_device_class) {
374 }, sizeof(struct nv_device_class),
379 /* workaround an odd issue on nvc1 by disabling the device's
380 * nosnoop capability. hopefully won't cause issues until a
381 * better fix is found - assuming there is one...
383 device = nv_device(drm->device);
384 if (nv_device(drm->device)->chipset == 0xc1)
385 nv_mask(device, 0x00088080, 0x00000800, 0x00000000);
387 nouveau_vga_init(drm);
388 nouveau_agp_init(drm);
390 if (device->card_type >= NV_50) {
391 ret = nouveau_vm_new(nv_device(drm->device), 0, (1ULL << 40),
392 0x1000, &drm->client.base.vm);
397 ret = nouveau_ttm_init(drm);
401 ret = nouveau_bios_init(dev);
405 ret = nouveau_display_create(dev);
409 if (dev->mode_config.num_crtc) {
410 ret = nouveau_display_init(dev);
415 nouveau_sysfs_init(dev);
416 nouveau_hwmon_init(dev);
417 nouveau_accel_init(drm);
418 nouveau_fbcon_init(dev);
420 if (nouveau_runtime_pm != 0) {
421 pm_runtime_use_autosuspend(dev->dev);
422 pm_runtime_set_autosuspend_delay(dev->dev, 5000);
423 pm_runtime_set_active(dev->dev);
424 pm_runtime_allow(dev->dev);
425 pm_runtime_mark_last_busy(dev->dev);
426 pm_runtime_put(dev->dev);
431 nouveau_display_destroy(dev);
433 nouveau_bios_takedown(dev);
435 nouveau_ttm_fini(drm);
437 nouveau_agp_fini(drm);
438 nouveau_vga_fini(drm);
440 nouveau_cli_destroy(&drm->client);
445 nouveau_drm_unload(struct drm_device *dev)
447 struct nouveau_drm *drm = nouveau_drm(dev);
449 pm_runtime_get_sync(dev->dev);
450 nouveau_fbcon_fini(dev);
451 nouveau_accel_fini(drm);
452 nouveau_hwmon_fini(dev);
453 nouveau_sysfs_fini(dev);
455 if (dev->mode_config.num_crtc)
456 nouveau_display_fini(dev);
457 nouveau_display_destroy(dev);
459 nouveau_bios_takedown(dev);
461 nouveau_ttm_fini(drm);
462 nouveau_agp_fini(drm);
463 nouveau_vga_fini(drm);
465 if (drm->hdmi_device)
466 pci_dev_put(drm->hdmi_device);
467 nouveau_cli_destroy(&drm->client);
472 nouveau_drm_remove(struct pci_dev *pdev)
474 struct drm_device *dev = pci_get_drvdata(pdev);
475 struct nouveau_drm *drm = nouveau_drm(dev);
476 struct nouveau_object *device;
478 device = drm->client.base.device;
481 nouveau_object_ref(NULL, &device);
482 nouveau_object_debug();
486 nouveau_do_suspend(struct drm_device *dev)
488 struct nouveau_drm *drm = nouveau_drm(dev);
489 struct nouveau_cli *cli;
492 if (dev->mode_config.num_crtc) {
493 NV_INFO(drm, "suspending display...\n");
494 ret = nouveau_display_suspend(dev);
499 NV_INFO(drm, "evicting buffers...\n");
500 ttm_bo_evict_mm(&drm->ttm.bdev, TTM_PL_VRAM);
502 NV_INFO(drm, "waiting for kernel channels to go idle...\n");
504 ret = nouveau_channel_idle(drm->cechan);
510 ret = nouveau_channel_idle(drm->channel);
515 NV_INFO(drm, "suspending client object trees...\n");
516 if (drm->fence && nouveau_fence(drm)->suspend) {
517 if (!nouveau_fence(drm)->suspend(drm)) {
523 list_for_each_entry(cli, &drm->clients, head) {
524 ret = nouveau_client_fini(&cli->base, true);
529 NV_INFO(drm, "suspending kernel object tree...\n");
530 ret = nouveau_client_fini(&drm->client.base, true);
534 nouveau_agp_fini(drm);
538 list_for_each_entry_continue_reverse(cli, &drm->clients, head) {
539 nouveau_client_init(&cli->base);
542 if (drm->fence && nouveau_fence(drm)->resume)
543 nouveau_fence(drm)->resume(drm);
546 if (dev->mode_config.num_crtc) {
547 NV_INFO(drm, "resuming display...\n");
548 nouveau_display_resume(dev);
553 int nouveau_pmops_suspend(struct device *dev)
555 struct pci_dev *pdev = to_pci_dev(dev);
556 struct drm_device *drm_dev = pci_get_drvdata(pdev);
559 if (drm_dev->switch_power_state == DRM_SWITCH_POWER_OFF ||
560 drm_dev->switch_power_state == DRM_SWITCH_POWER_DYNAMIC_OFF)
563 if (drm_dev->mode_config.num_crtc)
564 nouveau_fbcon_set_suspend(drm_dev, 1);
566 ret = nouveau_do_suspend(drm_dev);
570 pci_save_state(pdev);
571 pci_disable_device(pdev);
572 pci_set_power_state(pdev, PCI_D3hot);
577 nouveau_do_resume(struct drm_device *dev)
579 struct nouveau_drm *drm = nouveau_drm(dev);
580 struct nouveau_cli *cli;
582 NV_INFO(drm, "re-enabling device...\n");
584 nouveau_agp_reset(drm);
586 NV_INFO(drm, "resuming kernel object tree...\n");
587 nouveau_client_init(&drm->client.base);
588 nouveau_agp_init(drm);
590 NV_INFO(drm, "resuming client object trees...\n");
591 if (drm->fence && nouveau_fence(drm)->resume)
592 nouveau_fence(drm)->resume(drm);
594 list_for_each_entry(cli, &drm->clients, head) {
595 nouveau_client_init(&cli->base);
598 nouveau_run_vbios_init(dev);
600 if (dev->mode_config.num_crtc) {
601 NV_INFO(drm, "resuming display...\n");
602 nouveau_display_repin(dev);
608 int nouveau_pmops_resume(struct device *dev)
610 struct pci_dev *pdev = to_pci_dev(dev);
611 struct drm_device *drm_dev = pci_get_drvdata(pdev);
614 if (drm_dev->switch_power_state == DRM_SWITCH_POWER_OFF ||
615 drm_dev->switch_power_state == DRM_SWITCH_POWER_DYNAMIC_OFF)
618 pci_set_power_state(pdev, PCI_D0);
619 pci_restore_state(pdev);
620 ret = pci_enable_device(pdev);
623 pci_set_master(pdev);
625 ret = nouveau_do_resume(drm_dev);
628 if (drm_dev->mode_config.num_crtc)
629 nouveau_fbcon_set_suspend(drm_dev, 0);
631 nouveau_fbcon_zfill_all(drm_dev);
632 if (drm_dev->mode_config.num_crtc)
633 nouveau_display_resume(drm_dev);
637 static int nouveau_pmops_freeze(struct device *dev)
639 struct pci_dev *pdev = to_pci_dev(dev);
640 struct drm_device *drm_dev = pci_get_drvdata(pdev);
643 if (drm_dev->mode_config.num_crtc)
644 nouveau_fbcon_set_suspend(drm_dev, 1);
646 ret = nouveau_do_suspend(drm_dev);
650 static int nouveau_pmops_thaw(struct device *dev)
652 struct pci_dev *pdev = to_pci_dev(dev);
653 struct drm_device *drm_dev = pci_get_drvdata(pdev);
656 ret = nouveau_do_resume(drm_dev);
659 if (drm_dev->mode_config.num_crtc)
660 nouveau_fbcon_set_suspend(drm_dev, 0);
661 nouveau_fbcon_zfill_all(drm_dev);
662 if (drm_dev->mode_config.num_crtc)
663 nouveau_display_resume(drm_dev);
669 nouveau_drm_open(struct drm_device *dev, struct drm_file *fpriv)
671 struct pci_dev *pdev = dev->pdev;
672 struct nouveau_drm *drm = nouveau_drm(dev);
673 struct nouveau_cli *cli;
674 char name[32], tmpname[TASK_COMM_LEN];
677 /* need to bring up power immediately if opening device */
678 ret = pm_runtime_get_sync(dev->dev);
682 get_task_comm(tmpname, current);
683 snprintf(name, sizeof(name), "%s[%d]", tmpname, pid_nr(fpriv->pid));
685 ret = nouveau_cli_create(pdev, name, sizeof(*cli), (void **)&cli);
689 if (nv_device(drm->device)->card_type >= NV_50) {
690 ret = nouveau_vm_new(nv_device(drm->device), 0, (1ULL << 40),
691 0x1000, &cli->base.vm);
693 nouveau_cli_destroy(cli);
698 fpriv->driver_priv = cli;
700 mutex_lock(&drm->client.mutex);
701 list_add(&cli->head, &drm->clients);
702 mutex_unlock(&drm->client.mutex);
705 pm_runtime_mark_last_busy(dev->dev);
706 pm_runtime_put_autosuspend(dev->dev);
712 nouveau_drm_preclose(struct drm_device *dev, struct drm_file *fpriv)
714 struct nouveau_cli *cli = nouveau_cli(fpriv);
715 struct nouveau_drm *drm = nouveau_drm(dev);
717 pm_runtime_get_sync(dev->dev);
720 nouveau_abi16_fini(cli->abi16);
722 mutex_lock(&drm->client.mutex);
723 list_del(&cli->head);
724 mutex_unlock(&drm->client.mutex);
729 nouveau_drm_postclose(struct drm_device *dev, struct drm_file *fpriv)
731 struct nouveau_cli *cli = nouveau_cli(fpriv);
732 nouveau_cli_destroy(cli);
733 pm_runtime_mark_last_busy(dev->dev);
734 pm_runtime_put_autosuspend(dev->dev);
737 static const struct drm_ioctl_desc
739 DRM_IOCTL_DEF_DRV(NOUVEAU_GETPARAM, nouveau_abi16_ioctl_getparam, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
740 DRM_IOCTL_DEF_DRV(NOUVEAU_SETPARAM, nouveau_abi16_ioctl_setparam, DRM_UNLOCKED|DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
741 DRM_IOCTL_DEF_DRV(NOUVEAU_CHANNEL_ALLOC, nouveau_abi16_ioctl_channel_alloc, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
742 DRM_IOCTL_DEF_DRV(NOUVEAU_CHANNEL_FREE, nouveau_abi16_ioctl_channel_free, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
743 DRM_IOCTL_DEF_DRV(NOUVEAU_GROBJ_ALLOC, nouveau_abi16_ioctl_grobj_alloc, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
744 DRM_IOCTL_DEF_DRV(NOUVEAU_NOTIFIEROBJ_ALLOC, nouveau_abi16_ioctl_notifierobj_alloc, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
745 DRM_IOCTL_DEF_DRV(NOUVEAU_GPUOBJ_FREE, nouveau_abi16_ioctl_gpuobj_free, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
746 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_NEW, nouveau_gem_ioctl_new, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
747 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_PUSHBUF, nouveau_gem_ioctl_pushbuf, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
748 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_CPU_PREP, nouveau_gem_ioctl_cpu_prep, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
749 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_CPU_FINI, nouveau_gem_ioctl_cpu_fini, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
750 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_INFO, nouveau_gem_ioctl_info, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
753 long nouveau_drm_ioctl(struct file *filp,
754 unsigned int cmd, unsigned long arg)
756 struct drm_file *file_priv = filp->private_data;
757 struct drm_device *dev;
759 dev = file_priv->minor->dev;
761 ret = pm_runtime_get_sync(dev->dev);
765 ret = drm_ioctl(filp, cmd, arg);
767 pm_runtime_mark_last_busy(dev->dev);
768 pm_runtime_put_autosuspend(dev->dev);
771 static const struct file_operations
772 nouveau_driver_fops = {
773 .owner = THIS_MODULE,
775 .release = drm_release,
776 .unlocked_ioctl = nouveau_drm_ioctl,
777 .mmap = nouveau_ttm_mmap,
780 #if defined(CONFIG_COMPAT)
781 .compat_ioctl = nouveau_compat_ioctl,
783 .llseek = noop_llseek,
786 static struct drm_driver
790 DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME | DRIVER_RENDER,
792 .load = nouveau_drm_load,
793 .unload = nouveau_drm_unload,
794 .open = nouveau_drm_open,
795 .preclose = nouveau_drm_preclose,
796 .postclose = nouveau_drm_postclose,
797 .lastclose = nouveau_vga_lastclose,
799 #if defined(CONFIG_DEBUG_FS)
800 .debugfs_init = nouveau_debugfs_init,
801 .debugfs_cleanup = nouveau_debugfs_takedown,
804 .get_vblank_counter = drm_vblank_count,
805 .enable_vblank = nouveau_display_vblank_enable,
806 .disable_vblank = nouveau_display_vblank_disable,
807 .get_scanout_position = nouveau_display_scanoutpos,
808 .get_vblank_timestamp = nouveau_display_vblstamp,
810 .ioctls = nouveau_ioctls,
811 .num_ioctls = ARRAY_SIZE(nouveau_ioctls),
812 .fops = &nouveau_driver_fops,
814 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
815 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
816 .gem_prime_export = drm_gem_prime_export,
817 .gem_prime_import = drm_gem_prime_import,
818 .gem_prime_pin = nouveau_gem_prime_pin,
819 .gem_prime_unpin = nouveau_gem_prime_unpin,
820 .gem_prime_get_sg_table = nouveau_gem_prime_get_sg_table,
821 .gem_prime_import_sg_table = nouveau_gem_prime_import_sg_table,
822 .gem_prime_vmap = nouveau_gem_prime_vmap,
823 .gem_prime_vunmap = nouveau_gem_prime_vunmap,
825 .gem_free_object = nouveau_gem_object_del,
826 .gem_open_object = nouveau_gem_object_open,
827 .gem_close_object = nouveau_gem_object_close,
829 .dumb_create = nouveau_display_dumb_create,
830 .dumb_map_offset = nouveau_display_dumb_map_offset,
831 .dumb_destroy = drm_gem_dumb_destroy,
836 .date = GIT_REVISION,
840 .major = DRIVER_MAJOR,
841 .minor = DRIVER_MINOR,
842 .patchlevel = DRIVER_PATCHLEVEL,
845 static struct pci_device_id
846 nouveau_drm_pci_table[] = {
848 PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_ANY_ID),
849 .class = PCI_BASE_CLASS_DISPLAY << 16,
850 .class_mask = 0xff << 16,
853 PCI_DEVICE(PCI_VENDOR_ID_NVIDIA_SGS, PCI_ANY_ID),
854 .class = PCI_BASE_CLASS_DISPLAY << 16,
855 .class_mask = 0xff << 16,
860 static int nouveau_pmops_runtime_suspend(struct device *dev)
862 struct pci_dev *pdev = to_pci_dev(dev);
863 struct drm_device *drm_dev = pci_get_drvdata(pdev);
866 if (nouveau_runtime_pm == 0)
869 /* are we optimus enabled? */
870 if (nouveau_runtime_pm == -1 && !nouveau_is_optimus() && !nouveau_is_v1_dsm()) {
871 DRM_DEBUG_DRIVER("failing to power off - not optimus\n");
875 nv_debug_level(SILENT);
876 drm_kms_helper_poll_disable(drm_dev);
877 vga_switcheroo_set_dynamic_switch(pdev, VGA_SWITCHEROO_OFF);
878 nouveau_switcheroo_optimus_dsm();
879 ret = nouveau_do_suspend(drm_dev);
880 pci_save_state(pdev);
881 pci_disable_device(pdev);
882 pci_set_power_state(pdev, PCI_D3cold);
883 drm_dev->switch_power_state = DRM_SWITCH_POWER_DYNAMIC_OFF;
887 static int nouveau_pmops_runtime_resume(struct device *dev)
889 struct pci_dev *pdev = to_pci_dev(dev);
890 struct drm_device *drm_dev = pci_get_drvdata(pdev);
891 struct nouveau_device *device = nouveau_dev(drm_dev);
894 if (nouveau_runtime_pm == 0)
897 pci_set_power_state(pdev, PCI_D0);
898 pci_restore_state(pdev);
899 ret = pci_enable_device(pdev);
902 pci_set_master(pdev);
904 ret = nouveau_do_resume(drm_dev);
905 if (drm_dev->mode_config.num_crtc)
906 nouveau_display_resume(drm_dev);
907 drm_kms_helper_poll_enable(drm_dev);
909 nv_mask(device, 0x88488, (1 << 25), (1 << 25));
910 vga_switcheroo_set_dynamic_switch(pdev, VGA_SWITCHEROO_ON);
911 drm_dev->switch_power_state = DRM_SWITCH_POWER_ON;
912 nv_debug_level(NORMAL);
916 static int nouveau_pmops_runtime_idle(struct device *dev)
918 struct pci_dev *pdev = to_pci_dev(dev);
919 struct drm_device *drm_dev = pci_get_drvdata(pdev);
920 struct nouveau_drm *drm = nouveau_drm(drm_dev);
921 struct drm_crtc *crtc;
923 if (nouveau_runtime_pm == 0)
926 /* are we optimus enabled? */
927 if (nouveau_runtime_pm == -1 && !nouveau_is_optimus() && !nouveau_is_v1_dsm()) {
928 DRM_DEBUG_DRIVER("failing to power off - not optimus\n");
932 /* if we have a hdmi audio device - make sure it has a driver loaded */
933 if (drm->hdmi_device) {
934 if (!drm->hdmi_device->driver) {
935 DRM_DEBUG_DRIVER("failing to power off - no HDMI audio driver loaded\n");
936 pm_runtime_mark_last_busy(dev);
941 list_for_each_entry(crtc, &drm->dev->mode_config.crtc_list, head) {
943 DRM_DEBUG_DRIVER("failing to power off - crtc active\n");
947 pm_runtime_mark_last_busy(dev);
948 pm_runtime_autosuspend(dev);
949 /* we don't want the main rpm_idle to call suspend - we want to autosuspend */
953 static const struct dev_pm_ops nouveau_pm_ops = {
954 .suspend = nouveau_pmops_suspend,
955 .resume = nouveau_pmops_resume,
956 .freeze = nouveau_pmops_freeze,
957 .thaw = nouveau_pmops_thaw,
958 .poweroff = nouveau_pmops_freeze,
959 .restore = nouveau_pmops_resume,
960 .runtime_suspend = nouveau_pmops_runtime_suspend,
961 .runtime_resume = nouveau_pmops_runtime_resume,
962 .runtime_idle = nouveau_pmops_runtime_idle,
965 static struct pci_driver
966 nouveau_drm_pci_driver = {
968 .id_table = nouveau_drm_pci_table,
969 .probe = nouveau_drm_probe,
970 .remove = nouveau_drm_remove,
971 .driver.pm = &nouveau_pm_ops,
975 nouveau_drm_init(void)
977 if (nouveau_modeset == -1) {
978 #ifdef CONFIG_VGA_CONSOLE
979 if (vgacon_text_force())
984 if (!nouveau_modeset)
987 nouveau_register_dsm_handler();
988 return drm_pci_init(&driver, &nouveau_drm_pci_driver);
992 nouveau_drm_exit(void)
994 if (!nouveau_modeset)
997 drm_pci_exit(&driver, &nouveau_drm_pci_driver);
998 nouveau_unregister_dsm_handler();
1001 module_init(nouveau_drm_init);
1002 module_exit(nouveau_drm_exit);
1004 MODULE_DEVICE_TABLE(pci, nouveau_drm_pci_table);
1005 MODULE_AUTHOR(DRIVER_AUTHOR);
1006 MODULE_DESCRIPTION(DRIVER_DESC);
1007 MODULE_LICENSE("GPL and additional rights");