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>
32 #include "drm_crtc_helper.h"
34 #include <core/device.h>
35 #include <core/gpuobj.h>
36 #include <core/option.h>
38 #include "nouveau_drm.h"
39 #include "nouveau_dma.h"
40 #include "nouveau_ttm.h"
41 #include "nouveau_gem.h"
42 #include "nouveau_agp.h"
43 #include "nouveau_vga.h"
44 #include "nouveau_sysfs.h"
45 #include "nouveau_hwmon.h"
46 #include "nouveau_acpi.h"
47 #include "nouveau_bios.h"
48 #include "nouveau_ioctl.h"
49 #include "nouveau_abi16.h"
50 #include "nouveau_fbcon.h"
51 #include "nouveau_fence.h"
52 #include "nouveau_debugfs.h"
54 MODULE_PARM_DESC(config, "option string to pass to driver core");
55 static char *nouveau_config;
56 module_param_named(config, nouveau_config, charp, 0400);
58 MODULE_PARM_DESC(debug, "debug string to pass to driver core");
59 static char *nouveau_debug;
60 module_param_named(debug, nouveau_debug, charp, 0400);
62 MODULE_PARM_DESC(noaccel, "disable kernel/abi16 acceleration");
63 static int nouveau_noaccel = 0;
64 module_param_named(noaccel, nouveau_noaccel, int, 0400);
66 MODULE_PARM_DESC(modeset, "enable driver (default: auto, "
67 "0 = disabled, 1 = enabled, 2 = headless)");
68 int nouveau_modeset = -1;
69 module_param_named(modeset, nouveau_modeset, int, 0400);
71 MODULE_PARM_DESC(runpm, "disable (0), force enable (1), optimus only default (-1)");
72 int nouveau_runtime_pm = -1;
73 module_param_named(runpm, nouveau_runtime_pm, int, 0400);
75 static struct drm_driver driver;
78 nouveau_pci_name(struct pci_dev *pdev)
80 u64 name = (u64)pci_domain_nr(pdev->bus) << 32;
81 name |= pdev->bus->number << 16;
82 name |= PCI_SLOT(pdev->devfn) << 8;
83 return name | PCI_FUNC(pdev->devfn);
87 nouveau_platform_name(struct platform_device *platformdev)
89 return platformdev->id;
93 nouveau_name(struct drm_device *dev)
96 return nouveau_pci_name(dev->pdev);
98 return nouveau_platform_name(dev->platformdev);
102 nouveau_cli_create(u64 name, const char *sname,
103 int size, void **pcli)
105 struct nouveau_cli *cli = *pcli = kzalloc(size, GFP_KERNEL);
107 int ret = nvif_client_init(NULL, NULL, sname, name,
108 nouveau_config, nouveau_debug,
111 mutex_init(&cli->mutex);
118 nouveau_cli_destroy(struct nouveau_cli *cli)
120 nouveau_vm_ref(NULL, &nvkm_client(&cli->base)->vm, NULL);
121 nvif_client_fini(&cli->base);
125 nouveau_accel_fini(struct nouveau_drm *drm)
127 nouveau_channel_del(&drm->channel);
128 nvif_object_fini(&drm->ntfy);
129 nouveau_gpuobj_ref(NULL, &drm->notify);
130 nvif_object_fini(&drm->nvsw);
131 nouveau_channel_del(&drm->cechan);
132 nvif_object_fini(&drm->ttm.copy);
134 nouveau_fence(drm)->dtor(drm);
138 nouveau_accel_init(struct nouveau_drm *drm)
140 struct nvif_device *device = &drm->device;
148 /* initialise synchronisation routines */
149 /*XXX: this is crap, but the fence/channel stuff is a little
150 * backwards in some places. this will be fixed.
152 ret = nvif_object_sclass(&device->base, sclass, ARRAY_SIZE(sclass));
156 for (ret = -ENOSYS, i = 0; ret && i < ARRAY_SIZE(sclass); i++) {
158 case NV03_CHANNEL_DMA:
159 ret = nv04_fence_create(drm);
161 case NV10_CHANNEL_DMA:
162 ret = nv10_fence_create(drm);
164 case NV17_CHANNEL_DMA:
165 case NV40_CHANNEL_DMA:
166 ret = nv17_fence_create(drm);
168 case NV50_CHANNEL_GPFIFO:
169 ret = nv50_fence_create(drm);
171 case G82_CHANNEL_GPFIFO:
172 ret = nv84_fence_create(drm);
174 case FERMI_CHANNEL_GPFIFO:
175 case KEPLER_CHANNEL_GPFIFO_A:
176 ret = nvc0_fence_create(drm);
184 NV_ERROR(drm, "failed to initialise sync subsystem, %d\n", ret);
185 nouveau_accel_fini(drm);
189 if (device->info.family >= NV_DEVICE_INFO_V0_KEPLER) {
190 ret = nouveau_channel_new(drm, &drm->device, NVDRM_CHAN + 1,
191 KEPLER_CHANNEL_GPFIFO_A_V0_ENGINE_CE0|
192 KEPLER_CHANNEL_GPFIFO_A_V0_ENGINE_CE1,
195 NV_ERROR(drm, "failed to create ce channel, %d\n", ret);
197 arg0 = KEPLER_CHANNEL_GPFIFO_A_V0_ENGINE_GR;
200 if (device->info.chipset >= 0xa3 &&
201 device->info.chipset != 0xaa &&
202 device->info.chipset != 0xac) {
203 ret = nouveau_channel_new(drm, &drm->device, NVDRM_CHAN + 1,
204 NvDmaFB, NvDmaTT, &drm->cechan);
206 NV_ERROR(drm, "failed to create ce channel, %d\n", ret);
215 ret = nouveau_channel_new(drm, &drm->device, NVDRM_CHAN, arg0, arg1,
218 NV_ERROR(drm, "failed to create kernel channel, %d\n", ret);
219 nouveau_accel_fini(drm);
223 ret = nvif_object_init(drm->channel->object, NULL, NVDRM_NVSW,
224 nouveau_abi16_swclass(drm), NULL, 0, &drm->nvsw);
226 struct nouveau_software_chan *swch;
227 ret = RING_SPACE(drm->channel, 2);
229 if (device->info.family < NV_DEVICE_INFO_V0_FERMI) {
230 BEGIN_NV04(drm->channel, NvSubSw, 0, 1);
231 OUT_RING (drm->channel, NVDRM_NVSW);
233 if (device->info.family < NV_DEVICE_INFO_V0_KEPLER) {
234 BEGIN_NVC0(drm->channel, FermiSw, 0, 1);
235 OUT_RING (drm->channel, 0x001f0000);
238 swch = (void *)nvkm_object(&drm->nvsw)->parent;
239 swch->flip = nouveau_flip_complete;
240 swch->flip_data = drm->channel;
244 NV_ERROR(drm, "failed to allocate software object, %d\n", ret);
245 nouveau_accel_fini(drm);
249 if (device->info.family < NV_DEVICE_INFO_V0_FERMI) {
250 ret = nouveau_gpuobj_new(nvkm_object(&drm->device), NULL, 32,
253 NV_ERROR(drm, "failed to allocate notifier, %d\n", ret);
254 nouveau_accel_fini(drm);
258 ret = nvif_object_init(drm->channel->object, NULL, NvNotify0,
260 &(struct nv_dma_v0) {
261 .target = NV_DMA_V0_TARGET_VRAM,
262 .access = NV_DMA_V0_ACCESS_RDWR,
263 .start = drm->notify->addr,
264 .limit = drm->notify->addr + 31
265 }, sizeof(struct nv_dma_v0),
268 nouveau_accel_fini(drm);
274 nouveau_bo_move_init(drm);
277 static int nouveau_drm_probe(struct pci_dev *pdev,
278 const struct pci_device_id *pent)
280 struct nouveau_device *device;
281 struct apertures_struct *aper;
285 /* remove conflicting drivers (vesafb, efifb etc) */
286 aper = alloc_apertures(3);
290 aper->ranges[0].base = pci_resource_start(pdev, 1);
291 aper->ranges[0].size = pci_resource_len(pdev, 1);
294 if (pci_resource_len(pdev, 2)) {
295 aper->ranges[aper->count].base = pci_resource_start(pdev, 2);
296 aper->ranges[aper->count].size = pci_resource_len(pdev, 2);
300 if (pci_resource_len(pdev, 3)) {
301 aper->ranges[aper->count].base = pci_resource_start(pdev, 3);
302 aper->ranges[aper->count].size = pci_resource_len(pdev, 3);
307 boot = pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW;
309 remove_conflicting_framebuffers(aper, "nouveaufb", boot);
312 ret = nouveau_device_create(pdev, NOUVEAU_BUS_PCI,
313 nouveau_pci_name(pdev), pci_name(pdev),
314 nouveau_config, nouveau_debug, &device);
318 pci_set_master(pdev);
320 ret = drm_get_pci_dev(pdev, pent, &driver);
322 nouveau_object_ref(NULL, (struct nouveau_object **)&device);
329 #define PCI_CLASS_MULTIMEDIA_HD_AUDIO 0x0403
332 nouveau_get_hdmi_dev(struct nouveau_drm *drm)
334 struct pci_dev *pdev = drm->dev->pdev;
337 DRM_INFO("not a PCI device; no HDMI\n");
338 drm->hdmi_device = NULL;
342 /* subfunction one is a hdmi audio device? */
343 drm->hdmi_device = pci_get_bus_and_slot((unsigned int)pdev->bus->number,
344 PCI_DEVFN(PCI_SLOT(pdev->devfn), 1));
346 if (!drm->hdmi_device) {
347 NV_DEBUG(drm, "hdmi device not found %d %d %d\n", pdev->bus->number, PCI_SLOT(pdev->devfn), 1);
351 if ((drm->hdmi_device->class >> 8) != PCI_CLASS_MULTIMEDIA_HD_AUDIO) {
352 NV_DEBUG(drm, "possible hdmi device not audio %d\n", drm->hdmi_device->class);
353 pci_dev_put(drm->hdmi_device);
354 drm->hdmi_device = NULL;
360 nouveau_drm_load(struct drm_device *dev, unsigned long flags)
362 struct pci_dev *pdev = dev->pdev;
363 struct nouveau_drm *drm;
366 ret = nouveau_cli_create(nouveau_name(dev), "DRM", sizeof(*drm),
371 dev->dev_private = drm;
373 nvkm_client(&drm->client.base)->debug =
374 nouveau_dbgopt(nouveau_debug, "DRM");
376 INIT_LIST_HEAD(&drm->clients);
377 spin_lock_init(&drm->tile.lock);
379 nouveau_get_hdmi_dev(drm);
381 /* make sure AGP controller is in a consistent state before we
382 * (possibly) execute vbios init tables (see nouveau_agp.h)
384 if (pdev && drm_pci_device_is_agp(dev) && dev->agp) {
385 const u64 enables = NV_DEVICE_V0_DISABLE_IDENTIFY |
386 NV_DEVICE_V0_DISABLE_MMIO;
387 /* dummy device object, doesn't init anything, but allows
388 * agp code access to registers
390 ret = nvif_device_init(&drm->client.base.base, NULL,
391 NVDRM_DEVICE, NV_DEVICE,
392 &(struct nv_device_v0) {
396 }, sizeof(struct nv_device_v0),
401 nouveau_agp_reset(drm);
402 nvif_device_fini(&drm->device);
405 ret = nvif_device_init(&drm->client.base.base, NULL, NVDRM_DEVICE,
407 &(struct nv_device_v0) {
411 }, sizeof(struct nv_device_v0),
416 dev->irq_enabled = true;
418 /* workaround an odd issue on nvc1 by disabling the device's
419 * nosnoop capability. hopefully won't cause issues until a
420 * better fix is found - assuming there is one...
422 if (drm->device.info.chipset == 0xc1)
423 nvif_mask(&drm->device, 0x00088080, 0x00000800, 0x00000000);
425 nouveau_vga_init(drm);
426 nouveau_agp_init(drm);
428 if (drm->device.info.family >= NV_DEVICE_INFO_V0_TESLA) {
429 ret = nouveau_vm_new(nvkm_device(&drm->device), 0, (1ULL << 40),
430 0x1000, &drm->client.vm);
434 nvkm_client(&drm->client.base)->vm = drm->client.vm;
437 ret = nouveau_ttm_init(drm);
441 ret = nouveau_bios_init(dev);
445 ret = nouveau_display_create(dev);
449 if (dev->mode_config.num_crtc) {
450 ret = nouveau_display_init(dev);
455 nouveau_sysfs_init(dev);
456 nouveau_hwmon_init(dev);
457 nouveau_accel_init(drm);
458 nouveau_fbcon_init(dev);
460 if (nouveau_runtime_pm != 0) {
461 pm_runtime_use_autosuspend(dev->dev);
462 pm_runtime_set_autosuspend_delay(dev->dev, 5000);
463 pm_runtime_set_active(dev->dev);
464 pm_runtime_allow(dev->dev);
465 pm_runtime_mark_last_busy(dev->dev);
466 pm_runtime_put(dev->dev);
471 nouveau_display_destroy(dev);
473 nouveau_bios_takedown(dev);
475 nouveau_ttm_fini(drm);
477 nouveau_agp_fini(drm);
478 nouveau_vga_fini(drm);
480 nvif_device_fini(&drm->device);
481 nouveau_cli_destroy(&drm->client);
486 nouveau_drm_unload(struct drm_device *dev)
488 struct nouveau_drm *drm = nouveau_drm(dev);
490 pm_runtime_get_sync(dev->dev);
491 nouveau_fbcon_fini(dev);
492 nouveau_accel_fini(drm);
493 nouveau_hwmon_fini(dev);
494 nouveau_sysfs_fini(dev);
496 if (dev->mode_config.num_crtc)
497 nouveau_display_fini(dev);
498 nouveau_display_destroy(dev);
500 nouveau_bios_takedown(dev);
502 nouveau_ttm_fini(drm);
503 nouveau_agp_fini(drm);
504 nouveau_vga_fini(drm);
506 nvif_device_fini(&drm->device);
507 if (drm->hdmi_device)
508 pci_dev_put(drm->hdmi_device);
509 nouveau_cli_destroy(&drm->client);
514 nouveau_drm_device_remove(struct drm_device *dev)
516 struct nouveau_drm *drm = nouveau_drm(dev);
517 struct nouveau_client *client;
518 struct nouveau_object *device;
520 dev->irq_enabled = false;
521 client = nvkm_client(&drm->client.base);
522 device = client->device;
525 nouveau_object_ref(NULL, &device);
526 nouveau_object_debug();
528 EXPORT_SYMBOL(nouveau_drm_device_remove);
531 nouveau_drm_remove(struct pci_dev *pdev)
533 struct drm_device *dev = pci_get_drvdata(pdev);
535 nouveau_drm_device_remove(dev);
539 nouveau_do_suspend(struct drm_device *dev, bool runtime)
541 struct nouveau_drm *drm = nouveau_drm(dev);
542 struct nouveau_cli *cli;
545 if (dev->mode_config.num_crtc && !runtime) {
546 NV_INFO(drm, "suspending display...\n");
547 ret = nouveau_display_suspend(dev);
552 NV_INFO(drm, "evicting buffers...\n");
553 ttm_bo_evict_mm(&drm->ttm.bdev, TTM_PL_VRAM);
555 NV_INFO(drm, "waiting for kernel channels to go idle...\n");
557 ret = nouveau_channel_idle(drm->cechan);
563 ret = nouveau_channel_idle(drm->channel);
568 NV_INFO(drm, "suspending client object trees...\n");
569 if (drm->fence && nouveau_fence(drm)->suspend) {
570 if (!nouveau_fence(drm)->suspend(drm)) {
576 list_for_each_entry(cli, &drm->clients, head) {
577 ret = nvif_client_suspend(&cli->base);
582 NV_INFO(drm, "suspending kernel object tree...\n");
583 ret = nvif_client_suspend(&drm->client.base);
587 nouveau_agp_fini(drm);
591 list_for_each_entry_continue_reverse(cli, &drm->clients, head) {
592 nvif_client_resume(&cli->base);
595 if (drm->fence && nouveau_fence(drm)->resume)
596 nouveau_fence(drm)->resume(drm);
599 if (dev->mode_config.num_crtc) {
600 NV_INFO(drm, "resuming display...\n");
601 nouveau_display_resume(dev);
606 int nouveau_pmops_suspend(struct device *dev)
608 struct pci_dev *pdev = to_pci_dev(dev);
609 struct drm_device *drm_dev = pci_get_drvdata(pdev);
612 if (drm_dev->switch_power_state == DRM_SWITCH_POWER_OFF ||
613 drm_dev->switch_power_state == DRM_SWITCH_POWER_DYNAMIC_OFF)
616 if (drm_dev->mode_config.num_crtc)
617 nouveau_fbcon_set_suspend(drm_dev, 1);
619 ret = nouveau_do_suspend(drm_dev, false);
623 pci_save_state(pdev);
624 pci_disable_device(pdev);
625 pci_set_power_state(pdev, PCI_D3hot);
630 nouveau_do_resume(struct drm_device *dev)
632 struct nouveau_drm *drm = nouveau_drm(dev);
633 struct nouveau_cli *cli;
635 NV_INFO(drm, "re-enabling device...\n");
637 nouveau_agp_reset(drm);
639 NV_INFO(drm, "resuming kernel object tree...\n");
640 nvif_client_resume(&drm->client.base);
641 nouveau_agp_init(drm);
643 NV_INFO(drm, "resuming client object trees...\n");
644 if (drm->fence && nouveau_fence(drm)->resume)
645 nouveau_fence(drm)->resume(drm);
647 list_for_each_entry(cli, &drm->clients, head) {
648 nvif_client_resume(&cli->base);
651 nouveau_run_vbios_init(dev);
653 if (dev->mode_config.num_crtc) {
654 NV_INFO(drm, "resuming display...\n");
655 nouveau_display_repin(dev);
661 int nouveau_pmops_resume(struct device *dev)
663 struct pci_dev *pdev = to_pci_dev(dev);
664 struct drm_device *drm_dev = pci_get_drvdata(pdev);
667 if (drm_dev->switch_power_state == DRM_SWITCH_POWER_OFF ||
668 drm_dev->switch_power_state == DRM_SWITCH_POWER_DYNAMIC_OFF)
671 pci_set_power_state(pdev, PCI_D0);
672 pci_restore_state(pdev);
673 ret = pci_enable_device(pdev);
676 pci_set_master(pdev);
678 ret = nouveau_do_resume(drm_dev);
682 if (drm_dev->mode_config.num_crtc) {
683 nouveau_display_resume(drm_dev);
684 nouveau_fbcon_set_suspend(drm_dev, 0);
690 static int nouveau_pmops_freeze(struct device *dev)
692 struct pci_dev *pdev = to_pci_dev(dev);
693 struct drm_device *drm_dev = pci_get_drvdata(pdev);
696 if (drm_dev->mode_config.num_crtc)
697 nouveau_fbcon_set_suspend(drm_dev, 1);
699 ret = nouveau_do_suspend(drm_dev, false);
703 static int nouveau_pmops_thaw(struct device *dev)
705 struct pci_dev *pdev = to_pci_dev(dev);
706 struct drm_device *drm_dev = pci_get_drvdata(pdev);
709 ret = nouveau_do_resume(drm_dev);
713 if (drm_dev->mode_config.num_crtc) {
714 nouveau_display_resume(drm_dev);
715 nouveau_fbcon_set_suspend(drm_dev, 0);
723 nouveau_drm_open(struct drm_device *dev, struct drm_file *fpriv)
725 struct nouveau_drm *drm = nouveau_drm(dev);
726 struct nouveau_cli *cli;
727 char name[32], tmpname[TASK_COMM_LEN];
730 /* need to bring up power immediately if opening device */
731 ret = pm_runtime_get_sync(dev->dev);
732 if (ret < 0 && ret != -EACCES)
735 get_task_comm(tmpname, current);
736 snprintf(name, sizeof(name), "%s[%d]", tmpname, pid_nr(fpriv->pid));
738 ret = nouveau_cli_create(nouveau_name(dev), name, sizeof(*cli),
744 cli->base.super = false;
746 if (drm->device.info.family >= NV_DEVICE_INFO_V0_TESLA) {
747 ret = nouveau_vm_new(nvkm_device(&drm->device), 0, (1ULL << 40),
750 nouveau_cli_destroy(cli);
754 nvkm_client(&cli->base)->vm = cli->vm;
757 fpriv->driver_priv = cli;
759 mutex_lock(&drm->client.mutex);
760 list_add(&cli->head, &drm->clients);
761 mutex_unlock(&drm->client.mutex);
764 pm_runtime_mark_last_busy(dev->dev);
765 pm_runtime_put_autosuspend(dev->dev);
771 nouveau_drm_preclose(struct drm_device *dev, struct drm_file *fpriv)
773 struct nouveau_cli *cli = nouveau_cli(fpriv);
774 struct nouveau_drm *drm = nouveau_drm(dev);
776 pm_runtime_get_sync(dev->dev);
779 nouveau_abi16_fini(cli->abi16);
781 mutex_lock(&drm->client.mutex);
782 list_del(&cli->head);
783 mutex_unlock(&drm->client.mutex);
788 nouveau_drm_postclose(struct drm_device *dev, struct drm_file *fpriv)
790 struct nouveau_cli *cli = nouveau_cli(fpriv);
791 nouveau_cli_destroy(cli);
792 pm_runtime_mark_last_busy(dev->dev);
793 pm_runtime_put_autosuspend(dev->dev);
796 static const struct drm_ioctl_desc
798 DRM_IOCTL_DEF_DRV(NOUVEAU_GETPARAM, nouveau_abi16_ioctl_getparam, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
799 DRM_IOCTL_DEF_DRV(NOUVEAU_SETPARAM, nouveau_abi16_ioctl_setparam, DRM_UNLOCKED|DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
800 DRM_IOCTL_DEF_DRV(NOUVEAU_CHANNEL_ALLOC, nouveau_abi16_ioctl_channel_alloc, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
801 DRM_IOCTL_DEF_DRV(NOUVEAU_CHANNEL_FREE, nouveau_abi16_ioctl_channel_free, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
802 DRM_IOCTL_DEF_DRV(NOUVEAU_GROBJ_ALLOC, nouveau_abi16_ioctl_grobj_alloc, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
803 DRM_IOCTL_DEF_DRV(NOUVEAU_NOTIFIEROBJ_ALLOC, nouveau_abi16_ioctl_notifierobj_alloc, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
804 DRM_IOCTL_DEF_DRV(NOUVEAU_GPUOBJ_FREE, nouveau_abi16_ioctl_gpuobj_free, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
805 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_NEW, nouveau_gem_ioctl_new, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
806 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_PUSHBUF, nouveau_gem_ioctl_pushbuf, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
807 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_CPU_PREP, nouveau_gem_ioctl_cpu_prep, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
808 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_CPU_FINI, nouveau_gem_ioctl_cpu_fini, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
809 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_INFO, nouveau_gem_ioctl_info, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
812 long nouveau_drm_ioctl(struct file *filp,
813 unsigned int cmd, unsigned long arg)
815 struct drm_file *file_priv = filp->private_data;
816 struct drm_device *dev;
818 dev = file_priv->minor->dev;
820 ret = pm_runtime_get_sync(dev->dev);
821 if (ret < 0 && ret != -EACCES)
824 ret = drm_ioctl(filp, cmd, arg);
826 pm_runtime_mark_last_busy(dev->dev);
827 pm_runtime_put_autosuspend(dev->dev);
830 static const struct file_operations
831 nouveau_driver_fops = {
832 .owner = THIS_MODULE,
834 .release = drm_release,
835 .unlocked_ioctl = nouveau_drm_ioctl,
836 .mmap = nouveau_ttm_mmap,
839 #if defined(CONFIG_COMPAT)
840 .compat_ioctl = nouveau_compat_ioctl,
842 .llseek = noop_llseek,
845 static struct drm_driver
849 DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME | DRIVER_RENDER,
851 .load = nouveau_drm_load,
852 .unload = nouveau_drm_unload,
853 .open = nouveau_drm_open,
854 .preclose = nouveau_drm_preclose,
855 .postclose = nouveau_drm_postclose,
856 .lastclose = nouveau_vga_lastclose,
858 #if defined(CONFIG_DEBUG_FS)
859 .debugfs_init = nouveau_debugfs_init,
860 .debugfs_cleanup = nouveau_debugfs_takedown,
863 .get_vblank_counter = drm_vblank_count,
864 .enable_vblank = nouveau_display_vblank_enable,
865 .disable_vblank = nouveau_display_vblank_disable,
866 .get_scanout_position = nouveau_display_scanoutpos,
867 .get_vblank_timestamp = nouveau_display_vblstamp,
869 .ioctls = nouveau_ioctls,
870 .num_ioctls = ARRAY_SIZE(nouveau_ioctls),
871 .fops = &nouveau_driver_fops,
873 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
874 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
875 .gem_prime_export = drm_gem_prime_export,
876 .gem_prime_import = drm_gem_prime_import,
877 .gem_prime_pin = nouveau_gem_prime_pin,
878 .gem_prime_unpin = nouveau_gem_prime_unpin,
879 .gem_prime_get_sg_table = nouveau_gem_prime_get_sg_table,
880 .gem_prime_import_sg_table = nouveau_gem_prime_import_sg_table,
881 .gem_prime_vmap = nouveau_gem_prime_vmap,
882 .gem_prime_vunmap = nouveau_gem_prime_vunmap,
884 .gem_free_object = nouveau_gem_object_del,
885 .gem_open_object = nouveau_gem_object_open,
886 .gem_close_object = nouveau_gem_object_close,
888 .dumb_create = nouveau_display_dumb_create,
889 .dumb_map_offset = nouveau_display_dumb_map_offset,
890 .dumb_destroy = drm_gem_dumb_destroy,
895 .date = GIT_REVISION,
899 .major = DRIVER_MAJOR,
900 .minor = DRIVER_MINOR,
901 .patchlevel = DRIVER_PATCHLEVEL,
904 static struct pci_device_id
905 nouveau_drm_pci_table[] = {
907 PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_ANY_ID),
908 .class = PCI_BASE_CLASS_DISPLAY << 16,
909 .class_mask = 0xff << 16,
912 PCI_DEVICE(PCI_VENDOR_ID_NVIDIA_SGS, PCI_ANY_ID),
913 .class = PCI_BASE_CLASS_DISPLAY << 16,
914 .class_mask = 0xff << 16,
919 static int nouveau_pmops_runtime_suspend(struct device *dev)
921 struct pci_dev *pdev = to_pci_dev(dev);
922 struct drm_device *drm_dev = pci_get_drvdata(pdev);
925 if (nouveau_runtime_pm == 0) {
926 pm_runtime_forbid(dev);
930 /* are we optimus enabled? */
931 if (nouveau_runtime_pm == -1 && !nouveau_is_optimus() && !nouveau_is_v1_dsm()) {
932 DRM_DEBUG_DRIVER("failing to power off - not optimus\n");
933 pm_runtime_forbid(dev);
937 nv_debug_level(SILENT);
938 drm_kms_helper_poll_disable(drm_dev);
939 vga_switcheroo_set_dynamic_switch(pdev, VGA_SWITCHEROO_OFF);
940 nouveau_switcheroo_optimus_dsm();
941 ret = nouveau_do_suspend(drm_dev, true);
942 pci_save_state(pdev);
943 pci_disable_device(pdev);
944 pci_set_power_state(pdev, PCI_D3cold);
945 drm_dev->switch_power_state = DRM_SWITCH_POWER_DYNAMIC_OFF;
949 static int nouveau_pmops_runtime_resume(struct device *dev)
951 struct pci_dev *pdev = to_pci_dev(dev);
952 struct drm_device *drm_dev = pci_get_drvdata(pdev);
953 struct nvif_device *device = &nouveau_drm(drm_dev)->device;
956 if (nouveau_runtime_pm == 0)
959 pci_set_power_state(pdev, PCI_D0);
960 pci_restore_state(pdev);
961 ret = pci_enable_device(pdev);
964 pci_set_master(pdev);
966 ret = nouveau_do_resume(drm_dev);
967 drm_kms_helper_poll_enable(drm_dev);
969 nvif_mask(device, 0x88488, (1 << 25), (1 << 25));
970 vga_switcheroo_set_dynamic_switch(pdev, VGA_SWITCHEROO_ON);
971 drm_dev->switch_power_state = DRM_SWITCH_POWER_ON;
972 nv_debug_level(NORMAL);
976 static int nouveau_pmops_runtime_idle(struct device *dev)
978 struct pci_dev *pdev = to_pci_dev(dev);
979 struct drm_device *drm_dev = pci_get_drvdata(pdev);
980 struct nouveau_drm *drm = nouveau_drm(drm_dev);
981 struct drm_crtc *crtc;
983 if (nouveau_runtime_pm == 0) {
984 pm_runtime_forbid(dev);
988 /* are we optimus enabled? */
989 if (nouveau_runtime_pm == -1 && !nouveau_is_optimus() && !nouveau_is_v1_dsm()) {
990 DRM_DEBUG_DRIVER("failing to power off - not optimus\n");
991 pm_runtime_forbid(dev);
995 /* if we have a hdmi audio device - make sure it has a driver loaded */
996 if (drm->hdmi_device) {
997 if (!drm->hdmi_device->driver) {
998 DRM_DEBUG_DRIVER("failing to power off - no HDMI audio driver loaded\n");
999 pm_runtime_mark_last_busy(dev);
1004 list_for_each_entry(crtc, &drm->dev->mode_config.crtc_list, head) {
1005 if (crtc->enabled) {
1006 DRM_DEBUG_DRIVER("failing to power off - crtc active\n");
1010 pm_runtime_mark_last_busy(dev);
1011 pm_runtime_autosuspend(dev);
1012 /* we don't want the main rpm_idle to call suspend - we want to autosuspend */
1016 static const struct dev_pm_ops nouveau_pm_ops = {
1017 .suspend = nouveau_pmops_suspend,
1018 .resume = nouveau_pmops_resume,
1019 .freeze = nouveau_pmops_freeze,
1020 .thaw = nouveau_pmops_thaw,
1021 .poweroff = nouveau_pmops_freeze,
1022 .restore = nouveau_pmops_resume,
1023 .runtime_suspend = nouveau_pmops_runtime_suspend,
1024 .runtime_resume = nouveau_pmops_runtime_resume,
1025 .runtime_idle = nouveau_pmops_runtime_idle,
1028 static struct pci_driver
1029 nouveau_drm_pci_driver = {
1031 .id_table = nouveau_drm_pci_table,
1032 .probe = nouveau_drm_probe,
1033 .remove = nouveau_drm_remove,
1034 .driver.pm = &nouveau_pm_ops,
1038 nouveau_platform_device_create_(struct platform_device *pdev, int size,
1041 struct drm_device *drm;
1044 err = nouveau_device_create_(pdev, NOUVEAU_BUS_PLATFORM,
1045 nouveau_platform_name(pdev),
1046 dev_name(&pdev->dev), nouveau_config,
1047 nouveau_debug, size, pobject);
1049 return ERR_PTR(err);
1051 drm = drm_dev_alloc(&driver, &pdev->dev);
1057 err = drm_dev_set_unique(drm, "%s", dev_name(&pdev->dev));
1061 drm->platformdev = pdev;
1062 platform_set_drvdata(pdev, drm);
1067 nouveau_object_ref(NULL, (struct nouveau_object **)pobject);
1069 return ERR_PTR(err);
1071 EXPORT_SYMBOL(nouveau_platform_device_create_);
1074 nouveau_drm_init(void)
1076 if (nouveau_modeset == -1) {
1077 #ifdef CONFIG_VGA_CONSOLE
1078 if (vgacon_text_force())
1079 nouveau_modeset = 0;
1083 if (!nouveau_modeset)
1086 nouveau_register_dsm_handler();
1087 return drm_pci_init(&driver, &nouveau_drm_pci_driver);
1091 nouveau_drm_exit(void)
1093 if (!nouveau_modeset)
1096 drm_pci_exit(&driver, &nouveau_drm_pci_driver);
1097 nouveau_unregister_dsm_handler();
1100 module_init(nouveau_drm_init);
1101 module_exit(nouveau_drm_exit);
1103 MODULE_DEVICE_TABLE(pci, nouveau_drm_pci_table);
1104 MODULE_AUTHOR(DRIVER_AUTHOR);
1105 MODULE_DESCRIPTION(DRIVER_DESC);
1106 MODULE_LICENSE("GPL and additional rights");