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>
41 #include <subdev/vm.h>
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"
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);
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);
67 MODULE_PARM_DESC(noaccel, "disable kernel/abi16 acceleration");
68 static int nouveau_noaccel = 0;
69 module_param_named(noaccel, nouveau_noaccel, int, 0400);
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);
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);
80 static struct drm_driver driver;
83 nouveau_name(struct pci_dev *pdev)
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);
92 nouveau_cli_create(struct pci_dev *pdev, const char *name,
93 int size, void **pcli)
95 struct nouveau_cli *cli;
99 ret = nouveau_client_create_(name, nouveau_name(pdev), nouveau_config,
100 nouveau_debug, size, pcli);
104 nouveau_client_destroy(&cli->base);
109 mutex_init(&cli->mutex);
114 nouveau_cli_destroy(struct nouveau_cli *cli)
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);
124 nouveau_accel_fini(struct nouveau_drm *drm)
126 nouveau_gpuobj_ref(NULL, &drm->notify);
127 nouveau_channel_del(&drm->channel);
128 nouveau_channel_del(&drm->cechan);
130 nouveau_fence(drm)->dtor(drm);
134 nouveau_accel_init(struct nouveau_drm *drm)
136 struct nouveau_device *device = nv_device(drm->device);
137 struct nouveau_object *object;
141 if (nouveau_noaccel || !nouveau_fifo(device) /*XXX*/)
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);
153 NV_ERROR(drm, "failed to initialise sync subsystem, %d\n", ret);
154 nouveau_accel_fini(drm);
158 if (device->card_type >= NV_E0) {
159 ret = nouveau_channel_new(drm, &drm->client, NVDRM_DEVICE,
161 NVE0_CHANNEL_IND_ENGINE_CE0 |
162 NVE0_CHANNEL_IND_ENGINE_CE1, 0,
165 NV_ERROR(drm, "failed to create ce channel, %d\n", ret);
167 arg0 = NVE0_CHANNEL_IND_ENGINE_GR;
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,
177 NV_ERROR(drm, "failed to create ce channel, %d\n", ret);
186 ret = nouveau_channel_new(drm, &drm->client, NVDRM_DEVICE, NVDRM_CHAN,
187 arg0, arg1, &drm->channel);
189 NV_ERROR(drm, "failed to create kernel channel, %d\n", ret);
190 nouveau_accel_fini(drm);
194 if (device->card_type < NV_C0) {
195 ret = nouveau_gpuobj_new(drm->device, NULL, 32, 0, 0,
198 NV_ERROR(drm, "failed to allocate notifier, %d\n", ret);
199 nouveau_accel_fini(drm);
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 |
208 .start = drm->notify->addr,
209 .limit = drm->notify->addr + 31
210 }, sizeof(struct nv_dma_class),
213 nouveau_accel_fini(drm);
219 nouveau_bo_move_init(drm);
222 static int nouveau_drm_probe(struct pci_dev *pdev,
223 const struct pci_device_id *pent)
225 struct nouveau_device *device;
226 struct apertures_struct *aper;
230 /* remove conflicting drivers (vesafb, efifb etc) */
231 aper = alloc_apertures(3);
235 aper->ranges[0].base = pci_resource_start(pdev, 1);
236 aper->ranges[0].size = pci_resource_len(pdev, 1);
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);
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);
252 boot = pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW;
254 remove_conflicting_framebuffers(aper, "nouveaufb", boot);
257 ret = nouveau_device_create(pdev, nouveau_name(pdev), pci_name(pdev),
258 nouveau_config, nouveau_debug, &device);
262 pci_set_master(pdev);
264 ret = drm_get_pci_dev(pdev, pent, &driver);
266 nouveau_object_ref(NULL, (struct nouveau_object **)&device);
273 #define PCI_CLASS_MULTIMEDIA_HD_AUDIO 0x0403
276 nouveau_get_hdmi_dev(struct drm_device *dev)
278 struct nouveau_drm *drm = dev->dev_private;
279 struct pci_dev *pdev = dev->pdev;
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));
285 if (!drm->hdmi_device) {
286 DRM_INFO("hdmi device not found %d %d %d\n", pdev->bus->number, PCI_SLOT(pdev->devfn), 1);
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;
299 nouveau_drm_load(struct drm_device *dev, unsigned long flags)
301 struct pci_dev *pdev = dev->pdev;
302 struct nouveau_device *device;
303 struct nouveau_drm *drm;
306 ret = nouveau_cli_create(pdev, "DRM", sizeof(*drm), (void**)&drm);
310 dev->dev_private = drm;
313 INIT_LIST_HEAD(&drm->clients);
314 spin_lock_init(&drm->tile.lock);
316 nouveau_get_hdmi_dev(dev);
318 /* make sure AGP controller is in a consistent state before we
319 * (possibly) execute vbios init tables (see nouveau_agp.h)
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
325 ret = nouveau_object_new(nv_object(drm), NVDRM_CLIENT,
326 NVDRM_DEVICE, 0x0080,
327 &(struct nv_device_class) {
330 ~(NV_DEVICE_DISABLE_MMIO |
331 NV_DEVICE_DISABLE_IDENTIFY),
333 }, sizeof(struct nv_device_class),
338 nouveau_agp_reset(drm);
339 nouveau_object_del(nv_object(drm), NVDRM_CLIENT, NVDRM_DEVICE);
342 ret = nouveau_object_new(nv_object(drm), NVDRM_CLIENT, NVDRM_DEVICE,
343 0x0080, &(struct nv_device_class) {
347 }, sizeof(struct nv_device_class),
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...
356 device = nv_device(drm->device);
357 if (nv_device(drm->device)->chipset == 0xc1)
358 nv_mask(device, 0x00088080, 0x00000800, 0x00000000);
360 nouveau_vga_init(drm);
361 nouveau_agp_init(drm);
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);
370 ret = nouveau_ttm_init(drm);
374 ret = nouveau_bios_init(dev);
378 ret = nouveau_display_create(dev);
382 if (dev->mode_config.num_crtc) {
383 ret = nouveau_display_init(dev);
388 nouveau_sysfs_init(dev);
389 nouveau_hwmon_init(dev);
390 nouveau_accel_init(drm);
391 nouveau_fbcon_init(dev);
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);
404 nouveau_display_destroy(dev);
406 nouveau_bios_takedown(dev);
408 nouveau_ttm_fini(drm);
410 nouveau_agp_fini(drm);
411 nouveau_vga_fini(drm);
413 nouveau_cli_destroy(&drm->client);
418 nouveau_drm_unload(struct drm_device *dev)
420 struct nouveau_drm *drm = nouveau_drm(dev);
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);
428 if (dev->mode_config.num_crtc)
429 nouveau_display_fini(dev);
430 nouveau_display_destroy(dev);
432 nouveau_bios_takedown(dev);
434 nouveau_ttm_fini(drm);
435 nouveau_agp_fini(drm);
436 nouveau_vga_fini(drm);
438 if (drm->hdmi_device)
439 pci_dev_put(drm->hdmi_device);
440 nouveau_cli_destroy(&drm->client);
445 nouveau_drm_remove(struct pci_dev *pdev)
447 struct drm_device *dev = pci_get_drvdata(pdev);
448 struct nouveau_drm *drm = nouveau_drm(dev);
449 struct nouveau_object *device;
451 device = drm->client.base.device;
454 nouveau_object_ref(NULL, &device);
455 nouveau_object_debug();
459 nouveau_do_suspend(struct drm_device *dev)
461 struct nouveau_drm *drm = nouveau_drm(dev);
462 struct nouveau_cli *cli;
465 if (dev->mode_config.num_crtc) {
466 NV_INFO(drm, "suspending display...\n");
467 ret = nouveau_display_suspend(dev);
472 NV_INFO(drm, "evicting buffers...\n");
473 ttm_bo_evict_mm(&drm->ttm.bdev, TTM_PL_VRAM);
475 NV_INFO(drm, "waiting for kernel channels to go idle...\n");
477 ret = nouveau_channel_idle(drm->cechan);
483 ret = nouveau_channel_idle(drm->channel);
488 NV_INFO(drm, "suspending client object trees...\n");
489 if (drm->fence && nouveau_fence(drm)->suspend) {
490 if (!nouveau_fence(drm)->suspend(drm))
494 list_for_each_entry(cli, &drm->clients, head) {
495 ret = nouveau_client_fini(&cli->base, true);
500 NV_INFO(drm, "suspending kernel object tree...\n");
501 ret = nouveau_client_fini(&drm->client.base, true);
505 nouveau_agp_fini(drm);
509 list_for_each_entry_continue_reverse(cli, &drm->clients, head) {
510 nouveau_client_init(&cli->base);
513 if (dev->mode_config.num_crtc) {
514 NV_INFO(drm, "resuming display...\n");
515 nouveau_display_resume(dev);
520 int nouveau_pmops_suspend(struct device *dev)
522 struct pci_dev *pdev = to_pci_dev(dev);
523 struct drm_device *drm_dev = pci_get_drvdata(pdev);
526 if (drm_dev->switch_power_state == DRM_SWITCH_POWER_OFF ||
527 drm_dev->switch_power_state == DRM_SWITCH_POWER_DYNAMIC_OFF)
530 if (drm_dev->mode_config.num_crtc)
531 nouveau_fbcon_set_suspend(drm_dev, 1);
533 ret = nouveau_do_suspend(drm_dev);
537 pci_save_state(pdev);
538 pci_disable_device(pdev);
539 pci_set_power_state(pdev, PCI_D3hot);
544 nouveau_do_resume(struct drm_device *dev)
546 struct nouveau_drm *drm = nouveau_drm(dev);
547 struct nouveau_cli *cli;
549 NV_INFO(drm, "re-enabling device...\n");
551 nouveau_agp_reset(drm);
553 NV_INFO(drm, "resuming kernel object tree...\n");
554 nouveau_client_init(&drm->client.base);
555 nouveau_agp_init(drm);
557 NV_INFO(drm, "resuming client object trees...\n");
558 if (drm->fence && nouveau_fence(drm)->resume)
559 nouveau_fence(drm)->resume(drm);
561 list_for_each_entry(cli, &drm->clients, head) {
562 nouveau_client_init(&cli->base);
565 nouveau_run_vbios_init(dev);
567 if (dev->mode_config.num_crtc) {
568 NV_INFO(drm, "resuming display...\n");
569 nouveau_display_repin(dev);
575 int nouveau_pmops_resume(struct device *dev)
577 struct pci_dev *pdev = to_pci_dev(dev);
578 struct drm_device *drm_dev = pci_get_drvdata(pdev);
581 if (drm_dev->switch_power_state == DRM_SWITCH_POWER_OFF ||
582 drm_dev->switch_power_state == DRM_SWITCH_POWER_DYNAMIC_OFF)
585 pci_set_power_state(pdev, PCI_D0);
586 pci_restore_state(pdev);
587 ret = pci_enable_device(pdev);
590 pci_set_master(pdev);
592 ret = nouveau_do_resume(drm_dev);
595 if (drm_dev->mode_config.num_crtc)
596 nouveau_fbcon_set_suspend(drm_dev, 0);
598 nouveau_fbcon_zfill_all(drm_dev);
599 if (drm_dev->mode_config.num_crtc)
600 nouveau_display_resume(drm_dev);
604 static int nouveau_pmops_freeze(struct device *dev)
606 struct pci_dev *pdev = to_pci_dev(dev);
607 struct drm_device *drm_dev = pci_get_drvdata(pdev);
610 if (drm_dev->mode_config.num_crtc)
611 nouveau_fbcon_set_suspend(drm_dev, 1);
613 ret = nouveau_do_suspend(drm_dev);
617 static int nouveau_pmops_thaw(struct device *dev)
619 struct pci_dev *pdev = to_pci_dev(dev);
620 struct drm_device *drm_dev = pci_get_drvdata(pdev);
623 ret = nouveau_do_resume(drm_dev);
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);
636 nouveau_drm_open(struct drm_device *dev, struct drm_file *fpriv)
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];
644 /* need to bring up power immediately if opening device */
645 ret = pm_runtime_get_sync(dev->dev);
649 get_task_comm(tmpname, current);
650 snprintf(name, sizeof(name), "%s[%d]", tmpname, pid_nr(fpriv->pid));
652 ret = nouveau_cli_create(pdev, name, sizeof(*cli), (void **)&cli);
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);
660 nouveau_cli_destroy(cli);
665 fpriv->driver_priv = cli;
667 mutex_lock(&drm->client.mutex);
668 list_add(&cli->head, &drm->clients);
669 mutex_unlock(&drm->client.mutex);
672 pm_runtime_mark_last_busy(dev->dev);
673 pm_runtime_put_autosuspend(dev->dev);
679 nouveau_drm_preclose(struct drm_device *dev, struct drm_file *fpriv)
681 struct nouveau_cli *cli = nouveau_cli(fpriv);
682 struct nouveau_drm *drm = nouveau_drm(dev);
684 pm_runtime_get_sync(dev->dev);
687 nouveau_abi16_fini(cli->abi16);
689 mutex_lock(&drm->client.mutex);
690 list_del(&cli->head);
691 mutex_unlock(&drm->client.mutex);
696 nouveau_drm_postclose(struct drm_device *dev, struct drm_file *fpriv)
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);
704 static const struct drm_ioctl_desc
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),
720 long nouveau_drm_ioctl(struct file *filp,
721 unsigned int cmd, unsigned long arg)
723 struct drm_file *file_priv = filp->private_data;
724 struct drm_device *dev;
726 dev = file_priv->minor->dev;
728 ret = pm_runtime_get_sync(dev->dev);
732 ret = drm_ioctl(filp, cmd, arg);
734 pm_runtime_mark_last_busy(dev->dev);
735 pm_runtime_put_autosuspend(dev->dev);
738 static const struct file_operations
739 nouveau_driver_fops = {
740 .owner = THIS_MODULE,
742 .release = drm_release,
743 .unlocked_ioctl = nouveau_drm_ioctl,
744 .mmap = nouveau_ttm_mmap,
747 #if defined(CONFIG_COMPAT)
748 .compat_ioctl = nouveau_compat_ioctl,
750 .llseek = noop_llseek,
753 static struct drm_driver
757 DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME | DRIVER_RENDER,
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,
766 #if defined(CONFIG_DEBUG_FS)
767 .debugfs_init = nouveau_debugfs_init,
768 .debugfs_cleanup = nouveau_debugfs_takedown,
771 .get_vblank_counter = drm_vblank_count,
772 .enable_vblank = nouveau_display_vblank_enable,
773 .disable_vblank = nouveau_display_vblank_disable,
775 .ioctls = nouveau_ioctls,
776 .num_ioctls = ARRAY_SIZE(nouveau_ioctls),
777 .fops = &nouveau_driver_fops,
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,
790 .gem_free_object = nouveau_gem_object_del,
791 .gem_open_object = nouveau_gem_object_open,
792 .gem_close_object = nouveau_gem_object_close,
794 .dumb_create = nouveau_display_dumb_create,
795 .dumb_map_offset = nouveau_display_dumb_map_offset,
796 .dumb_destroy = drm_gem_dumb_destroy,
801 .date = GIT_REVISION,
805 .major = DRIVER_MAJOR,
806 .minor = DRIVER_MINOR,
807 .patchlevel = DRIVER_PATCHLEVEL,
810 static struct pci_device_id
811 nouveau_drm_pci_table[] = {
813 PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_ANY_ID),
814 .class = PCI_BASE_CLASS_DISPLAY << 16,
815 .class_mask = 0xff << 16,
818 PCI_DEVICE(PCI_VENDOR_ID_NVIDIA_SGS, PCI_ANY_ID),
819 .class = PCI_BASE_CLASS_DISPLAY << 16,
820 .class_mask = 0xff << 16,
825 static int nouveau_pmops_runtime_suspend(struct device *dev)
827 struct pci_dev *pdev = to_pci_dev(dev);
828 struct drm_device *drm_dev = pci_get_drvdata(pdev);
831 if (nouveau_runtime_pm == 0)
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;
846 static int nouveau_pmops_runtime_resume(struct device *dev)
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);
853 if (nouveau_runtime_pm == 0)
856 pci_set_power_state(pdev, PCI_D0);
857 pci_restore_state(pdev);
858 ret = pci_enable_device(pdev);
861 pci_set_master(pdev);
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);
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);
875 static int nouveau_pmops_runtime_idle(struct device *dev)
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;
882 if (nouveau_runtime_pm == 0)
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");
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);
900 list_for_each_entry(crtc, &drm->dev->mode_config.crtc_list, head) {
902 DRM_DEBUG_DRIVER("failing to power off - crtc active\n");
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 */
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,
924 static struct pci_driver
925 nouveau_drm_pci_driver = {
927 .id_table = nouveau_drm_pci_table,
928 .probe = nouveau_drm_probe,
929 .remove = nouveau_drm_remove,
930 .driver.pm = &nouveau_pm_ops,
934 nouveau_drm_init(void)
936 if (nouveau_modeset == -1) {
937 #ifdef CONFIG_VGA_CONSOLE
938 if (vgacon_text_force())
943 if (!nouveau_modeset)
946 nouveau_register_dsm_handler();
947 return drm_pci_init(&driver, &nouveau_drm_pci_driver);
951 nouveau_drm_exit(void)
953 if (!nouveau_modeset)
956 drm_pci_exit(&driver, &nouveau_drm_pci_driver);
957 nouveau_unregister_dsm_handler();
960 module_init(nouveau_drm_init);
961 module_exit(nouveau_drm_exit);
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");