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"
53 #include "nouveau_usif.h"
55 MODULE_PARM_DESC(config, "option string to pass to driver core");
56 static char *nouveau_config;
57 module_param_named(config, nouveau_config, charp, 0400);
59 MODULE_PARM_DESC(debug, "debug string to pass to driver core");
60 static char *nouveau_debug;
61 module_param_named(debug, nouveau_debug, charp, 0400);
63 MODULE_PARM_DESC(noaccel, "disable kernel/abi16 acceleration");
64 static int nouveau_noaccel = 0;
65 module_param_named(noaccel, nouveau_noaccel, int, 0400);
67 MODULE_PARM_DESC(modeset, "enable driver (default: auto, "
68 "0 = disabled, 1 = enabled, 2 = headless)");
69 int nouveau_modeset = -1;
70 module_param_named(modeset, nouveau_modeset, int, 0400);
72 MODULE_PARM_DESC(runpm, "disable (0), force enable (1), optimus only default (-1)");
73 int nouveau_runtime_pm = -1;
74 module_param_named(runpm, nouveau_runtime_pm, int, 0400);
76 static struct drm_driver driver;
79 nouveau_pci_name(struct pci_dev *pdev)
81 u64 name = (u64)pci_domain_nr(pdev->bus) << 32;
82 name |= pdev->bus->number << 16;
83 name |= PCI_SLOT(pdev->devfn) << 8;
84 return name | PCI_FUNC(pdev->devfn);
88 nouveau_platform_name(struct platform_device *platformdev)
90 return platformdev->id;
94 nouveau_name(struct drm_device *dev)
97 return nouveau_pci_name(dev->pdev);
99 return nouveau_platform_name(dev->platformdev);
103 nouveau_cli_create(u64 name, const char *sname,
104 int size, void **pcli)
106 struct nouveau_cli *cli = *pcli = kzalloc(size, GFP_KERNEL);
108 int ret = nvif_client_init(NULL, NULL, sname, name,
109 nouveau_config, nouveau_debug,
112 mutex_init(&cli->mutex);
113 usif_client_init(cli);
121 nouveau_cli_destroy(struct nouveau_cli *cli)
123 nouveau_vm_ref(NULL, &nvkm_client(&cli->base)->vm, NULL);
124 nvif_client_fini(&cli->base);
125 usif_client_fini(cli);
129 nouveau_accel_fini(struct nouveau_drm *drm)
131 nouveau_channel_del(&drm->channel);
132 nvif_object_fini(&drm->ntfy);
133 nouveau_gpuobj_ref(NULL, &drm->notify);
134 nvif_object_fini(&drm->nvsw);
135 nouveau_channel_del(&drm->cechan);
136 nvif_object_fini(&drm->ttm.copy);
138 nouveau_fence(drm)->dtor(drm);
142 nouveau_accel_init(struct nouveau_drm *drm)
144 struct nvif_device *device = &drm->device;
152 /* initialise synchronisation routines */
153 /*XXX: this is crap, but the fence/channel stuff is a little
154 * backwards in some places. this will be fixed.
156 ret = nvif_object_sclass(&device->base, sclass, ARRAY_SIZE(sclass));
160 for (ret = -ENOSYS, i = 0; ret && i < ARRAY_SIZE(sclass); i++) {
162 case NV03_CHANNEL_DMA:
163 ret = nv04_fence_create(drm);
165 case NV10_CHANNEL_DMA:
166 ret = nv10_fence_create(drm);
168 case NV17_CHANNEL_DMA:
169 case NV40_CHANNEL_DMA:
170 ret = nv17_fence_create(drm);
172 case NV50_CHANNEL_GPFIFO:
173 ret = nv50_fence_create(drm);
175 case G82_CHANNEL_GPFIFO:
176 ret = nv84_fence_create(drm);
178 case FERMI_CHANNEL_GPFIFO:
179 case KEPLER_CHANNEL_GPFIFO_A:
180 ret = nvc0_fence_create(drm);
188 NV_ERROR(drm, "failed to initialise sync subsystem, %d\n", ret);
189 nouveau_accel_fini(drm);
193 if (device->info.family >= NV_DEVICE_INFO_V0_KEPLER) {
194 ret = nouveau_channel_new(drm, &drm->device, NVDRM_CHAN + 1,
195 KEPLER_CHANNEL_GPFIFO_A_V0_ENGINE_CE0|
196 KEPLER_CHANNEL_GPFIFO_A_V0_ENGINE_CE1,
199 NV_ERROR(drm, "failed to create ce channel, %d\n", ret);
201 arg0 = KEPLER_CHANNEL_GPFIFO_A_V0_ENGINE_GR;
204 if (device->info.chipset >= 0xa3 &&
205 device->info.chipset != 0xaa &&
206 device->info.chipset != 0xac) {
207 ret = nouveau_channel_new(drm, &drm->device, NVDRM_CHAN + 1,
208 NvDmaFB, NvDmaTT, &drm->cechan);
210 NV_ERROR(drm, "failed to create ce channel, %d\n", ret);
219 ret = nouveau_channel_new(drm, &drm->device, NVDRM_CHAN, arg0, arg1,
222 NV_ERROR(drm, "failed to create kernel channel, %d\n", ret);
223 nouveau_accel_fini(drm);
227 ret = nvif_object_init(drm->channel->object, NULL, NVDRM_NVSW,
228 nouveau_abi16_swclass(drm), NULL, 0, &drm->nvsw);
230 struct nouveau_software_chan *swch;
231 ret = RING_SPACE(drm->channel, 2);
233 if (device->info.family < NV_DEVICE_INFO_V0_FERMI) {
234 BEGIN_NV04(drm->channel, NvSubSw, 0, 1);
235 OUT_RING (drm->channel, NVDRM_NVSW);
237 if (device->info.family < NV_DEVICE_INFO_V0_KEPLER) {
238 BEGIN_NVC0(drm->channel, FermiSw, 0, 1);
239 OUT_RING (drm->channel, 0x001f0000);
242 swch = (void *)nvkm_object(&drm->nvsw)->parent;
243 swch->flip = nouveau_flip_complete;
244 swch->flip_data = drm->channel;
248 NV_ERROR(drm, "failed to allocate software object, %d\n", ret);
249 nouveau_accel_fini(drm);
253 if (device->info.family < NV_DEVICE_INFO_V0_FERMI) {
254 ret = nouveau_gpuobj_new(nvkm_object(&drm->device), NULL, 32,
257 NV_ERROR(drm, "failed to allocate notifier, %d\n", ret);
258 nouveau_accel_fini(drm);
262 ret = nvif_object_init(drm->channel->object, NULL, NvNotify0,
264 &(struct nv_dma_v0) {
265 .target = NV_DMA_V0_TARGET_VRAM,
266 .access = NV_DMA_V0_ACCESS_RDWR,
267 .start = drm->notify->addr,
268 .limit = drm->notify->addr + 31
269 }, sizeof(struct nv_dma_v0),
272 nouveau_accel_fini(drm);
278 nouveau_bo_move_init(drm);
281 static int nouveau_drm_probe(struct pci_dev *pdev,
282 const struct pci_device_id *pent)
284 struct nouveau_device *device;
285 struct apertures_struct *aper;
289 /* remove conflicting drivers (vesafb, efifb etc) */
290 aper = alloc_apertures(3);
294 aper->ranges[0].base = pci_resource_start(pdev, 1);
295 aper->ranges[0].size = pci_resource_len(pdev, 1);
298 if (pci_resource_len(pdev, 2)) {
299 aper->ranges[aper->count].base = pci_resource_start(pdev, 2);
300 aper->ranges[aper->count].size = pci_resource_len(pdev, 2);
304 if (pci_resource_len(pdev, 3)) {
305 aper->ranges[aper->count].base = pci_resource_start(pdev, 3);
306 aper->ranges[aper->count].size = pci_resource_len(pdev, 3);
311 boot = pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW;
313 if (nouveau_modeset != 2)
314 remove_conflicting_framebuffers(aper, "nouveaufb", boot);
317 ret = nouveau_device_create(pdev, NOUVEAU_BUS_PCI,
318 nouveau_pci_name(pdev), pci_name(pdev),
319 nouveau_config, nouveau_debug, &device);
323 pci_set_master(pdev);
325 ret = drm_get_pci_dev(pdev, pent, &driver);
327 nouveau_object_ref(NULL, (struct nouveau_object **)&device);
334 #define PCI_CLASS_MULTIMEDIA_HD_AUDIO 0x0403
337 nouveau_get_hdmi_dev(struct nouveau_drm *drm)
339 struct pci_dev *pdev = drm->dev->pdev;
342 DRM_INFO("not a PCI device; no HDMI\n");
343 drm->hdmi_device = NULL;
347 /* subfunction one is a hdmi audio device? */
348 drm->hdmi_device = pci_get_bus_and_slot((unsigned int)pdev->bus->number,
349 PCI_DEVFN(PCI_SLOT(pdev->devfn), 1));
351 if (!drm->hdmi_device) {
352 NV_DEBUG(drm, "hdmi device not found %d %d %d\n", pdev->bus->number, PCI_SLOT(pdev->devfn), 1);
356 if ((drm->hdmi_device->class >> 8) != PCI_CLASS_MULTIMEDIA_HD_AUDIO) {
357 NV_DEBUG(drm, "possible hdmi device not audio %d\n", drm->hdmi_device->class);
358 pci_dev_put(drm->hdmi_device);
359 drm->hdmi_device = NULL;
365 nouveau_drm_load(struct drm_device *dev, unsigned long flags)
367 struct pci_dev *pdev = dev->pdev;
368 struct nouveau_drm *drm;
371 ret = nouveau_cli_create(nouveau_name(dev), "DRM", sizeof(*drm),
376 dev->dev_private = drm;
378 nvkm_client(&drm->client.base)->debug =
379 nouveau_dbgopt(nouveau_debug, "DRM");
381 INIT_LIST_HEAD(&drm->clients);
382 spin_lock_init(&drm->tile.lock);
384 nouveau_get_hdmi_dev(drm);
386 /* make sure AGP controller is in a consistent state before we
387 * (possibly) execute vbios init tables (see nouveau_agp.h)
389 if (pdev && drm_pci_device_is_agp(dev) && dev->agp) {
390 const u64 enables = NV_DEVICE_V0_DISABLE_IDENTIFY |
391 NV_DEVICE_V0_DISABLE_MMIO;
392 /* dummy device object, doesn't init anything, but allows
393 * agp code access to registers
395 ret = nvif_device_init(&drm->client.base.base, NULL,
396 NVDRM_DEVICE, NV_DEVICE,
397 &(struct nv_device_v0) {
401 }, sizeof(struct nv_device_v0),
406 nouveau_agp_reset(drm);
407 nvif_device_fini(&drm->device);
410 ret = nvif_device_init(&drm->client.base.base, NULL, NVDRM_DEVICE,
412 &(struct nv_device_v0) {
416 }, sizeof(struct nv_device_v0),
421 dev->irq_enabled = true;
423 /* workaround an odd issue on nvc1 by disabling the device's
424 * nosnoop capability. hopefully won't cause issues until a
425 * better fix is found - assuming there is one...
427 if (drm->device.info.chipset == 0xc1)
428 nvif_mask(&drm->device, 0x00088080, 0x00000800, 0x00000000);
430 nouveau_vga_init(drm);
431 nouveau_agp_init(drm);
433 if (drm->device.info.family >= NV_DEVICE_INFO_V0_TESLA) {
434 ret = nouveau_vm_new(nvkm_device(&drm->device), 0, (1ULL << 40),
435 0x1000, &drm->client.vm);
439 nvkm_client(&drm->client.base)->vm = drm->client.vm;
442 ret = nouveau_ttm_init(drm);
446 ret = nouveau_bios_init(dev);
450 ret = nouveau_display_create(dev);
454 if (dev->mode_config.num_crtc) {
455 ret = nouveau_display_init(dev);
460 nouveau_sysfs_init(dev);
461 nouveau_hwmon_init(dev);
462 nouveau_accel_init(drm);
463 nouveau_fbcon_init(dev);
465 if (nouveau_runtime_pm != 0) {
466 pm_runtime_use_autosuspend(dev->dev);
467 pm_runtime_set_autosuspend_delay(dev->dev, 5000);
468 pm_runtime_set_active(dev->dev);
469 pm_runtime_allow(dev->dev);
470 pm_runtime_mark_last_busy(dev->dev);
471 pm_runtime_put(dev->dev);
476 nouveau_display_destroy(dev);
478 nouveau_bios_takedown(dev);
480 nouveau_ttm_fini(drm);
482 nouveau_agp_fini(drm);
483 nouveau_vga_fini(drm);
485 nvif_device_fini(&drm->device);
486 nouveau_cli_destroy(&drm->client);
491 nouveau_drm_unload(struct drm_device *dev)
493 struct nouveau_drm *drm = nouveau_drm(dev);
495 pm_runtime_get_sync(dev->dev);
496 nouveau_fbcon_fini(dev);
497 nouveau_accel_fini(drm);
498 nouveau_hwmon_fini(dev);
499 nouveau_sysfs_fini(dev);
501 if (dev->mode_config.num_crtc)
502 nouveau_display_fini(dev);
503 nouveau_display_destroy(dev);
505 nouveau_bios_takedown(dev);
507 nouveau_ttm_fini(drm);
508 nouveau_agp_fini(drm);
509 nouveau_vga_fini(drm);
511 nvif_device_fini(&drm->device);
512 if (drm->hdmi_device)
513 pci_dev_put(drm->hdmi_device);
514 nouveau_cli_destroy(&drm->client);
519 nouveau_drm_device_remove(struct drm_device *dev)
521 struct nouveau_drm *drm = nouveau_drm(dev);
522 struct nouveau_client *client;
523 struct nouveau_object *device;
525 dev->irq_enabled = false;
526 client = nvkm_client(&drm->client.base);
527 device = client->device;
530 nouveau_object_ref(NULL, &device);
531 nouveau_object_debug();
533 EXPORT_SYMBOL(nouveau_drm_device_remove);
536 nouveau_drm_remove(struct pci_dev *pdev)
538 struct drm_device *dev = pci_get_drvdata(pdev);
540 nouveau_drm_device_remove(dev);
544 nouveau_do_suspend(struct drm_device *dev, bool runtime)
546 struct nouveau_drm *drm = nouveau_drm(dev);
547 struct nouveau_cli *cli;
550 if (dev->mode_config.num_crtc && !runtime) {
551 NV_INFO(drm, "suspending display...\n");
552 ret = nouveau_display_suspend(dev);
557 NV_INFO(drm, "evicting buffers...\n");
558 ttm_bo_evict_mm(&drm->ttm.bdev, TTM_PL_VRAM);
560 NV_INFO(drm, "waiting for kernel channels to go idle...\n");
562 ret = nouveau_channel_idle(drm->cechan);
568 ret = nouveau_channel_idle(drm->channel);
573 NV_INFO(drm, "suspending client object trees...\n");
574 if (drm->fence && nouveau_fence(drm)->suspend) {
575 if (!nouveau_fence(drm)->suspend(drm)) {
581 list_for_each_entry(cli, &drm->clients, head) {
582 ret = nvif_client_suspend(&cli->base);
587 NV_INFO(drm, "suspending kernel object tree...\n");
588 ret = nvif_client_suspend(&drm->client.base);
592 nouveau_agp_fini(drm);
596 list_for_each_entry_continue_reverse(cli, &drm->clients, head) {
597 nvif_client_resume(&cli->base);
600 if (drm->fence && nouveau_fence(drm)->resume)
601 nouveau_fence(drm)->resume(drm);
604 if (dev->mode_config.num_crtc) {
605 NV_INFO(drm, "resuming display...\n");
606 nouveau_display_resume(dev);
611 int nouveau_pmops_suspend(struct device *dev)
613 struct pci_dev *pdev = to_pci_dev(dev);
614 struct drm_device *drm_dev = pci_get_drvdata(pdev);
617 if (drm_dev->switch_power_state == DRM_SWITCH_POWER_OFF ||
618 drm_dev->switch_power_state == DRM_SWITCH_POWER_DYNAMIC_OFF)
621 if (drm_dev->mode_config.num_crtc)
622 nouveau_fbcon_set_suspend(drm_dev, 1);
624 ret = nouveau_do_suspend(drm_dev, false);
628 pci_save_state(pdev);
629 pci_disable_device(pdev);
630 pci_ignore_hotplug(pdev);
631 pci_set_power_state(pdev, PCI_D3hot);
636 nouveau_do_resume(struct drm_device *dev)
638 struct nouveau_drm *drm = nouveau_drm(dev);
639 struct nouveau_cli *cli;
641 NV_INFO(drm, "re-enabling device...\n");
643 nouveau_agp_reset(drm);
645 NV_INFO(drm, "resuming kernel object tree...\n");
646 nvif_client_resume(&drm->client.base);
647 nouveau_agp_init(drm);
649 NV_INFO(drm, "resuming client object trees...\n");
650 if (drm->fence && nouveau_fence(drm)->resume)
651 nouveau_fence(drm)->resume(drm);
653 list_for_each_entry(cli, &drm->clients, head) {
654 nvif_client_resume(&cli->base);
657 nouveau_run_vbios_init(dev);
659 if (dev->mode_config.num_crtc) {
660 NV_INFO(drm, "resuming display...\n");
661 nouveau_display_repin(dev);
667 int nouveau_pmops_resume(struct device *dev)
669 struct pci_dev *pdev = to_pci_dev(dev);
670 struct drm_device *drm_dev = pci_get_drvdata(pdev);
673 if (drm_dev->switch_power_state == DRM_SWITCH_POWER_OFF ||
674 drm_dev->switch_power_state == DRM_SWITCH_POWER_DYNAMIC_OFF)
677 pci_set_power_state(pdev, PCI_D0);
678 pci_restore_state(pdev);
679 ret = pci_enable_device(pdev);
682 pci_set_master(pdev);
684 ret = nouveau_do_resume(drm_dev);
688 if (drm_dev->mode_config.num_crtc) {
689 nouveau_display_resume(drm_dev);
690 nouveau_fbcon_set_suspend(drm_dev, 0);
696 static int nouveau_pmops_freeze(struct device *dev)
698 struct pci_dev *pdev = to_pci_dev(dev);
699 struct drm_device *drm_dev = pci_get_drvdata(pdev);
702 if (drm_dev->mode_config.num_crtc)
703 nouveau_fbcon_set_suspend(drm_dev, 1);
705 ret = nouveau_do_suspend(drm_dev, false);
709 static int nouveau_pmops_thaw(struct device *dev)
711 struct pci_dev *pdev = to_pci_dev(dev);
712 struct drm_device *drm_dev = pci_get_drvdata(pdev);
715 ret = nouveau_do_resume(drm_dev);
719 if (drm_dev->mode_config.num_crtc) {
720 nouveau_display_resume(drm_dev);
721 nouveau_fbcon_set_suspend(drm_dev, 0);
729 nouveau_drm_open(struct drm_device *dev, struct drm_file *fpriv)
731 struct nouveau_drm *drm = nouveau_drm(dev);
732 struct nouveau_cli *cli;
733 char name[32], tmpname[TASK_COMM_LEN];
736 /* need to bring up power immediately if opening device */
737 ret = pm_runtime_get_sync(dev->dev);
738 if (ret < 0 && ret != -EACCES)
741 get_task_comm(tmpname, current);
742 snprintf(name, sizeof(name), "%s[%d]", tmpname, pid_nr(fpriv->pid));
744 ret = nouveau_cli_create(nouveau_name(dev), name, sizeof(*cli),
750 cli->base.super = false;
752 if (drm->device.info.family >= NV_DEVICE_INFO_V0_TESLA) {
753 ret = nouveau_vm_new(nvkm_device(&drm->device), 0, (1ULL << 40),
756 nouveau_cli_destroy(cli);
760 nvkm_client(&cli->base)->vm = cli->vm;
763 fpriv->driver_priv = cli;
765 mutex_lock(&drm->client.mutex);
766 list_add(&cli->head, &drm->clients);
767 mutex_unlock(&drm->client.mutex);
770 pm_runtime_mark_last_busy(dev->dev);
771 pm_runtime_put_autosuspend(dev->dev);
777 nouveau_drm_preclose(struct drm_device *dev, struct drm_file *fpriv)
779 struct nouveau_cli *cli = nouveau_cli(fpriv);
780 struct nouveau_drm *drm = nouveau_drm(dev);
782 pm_runtime_get_sync(dev->dev);
785 nouveau_abi16_fini(cli->abi16);
787 mutex_lock(&drm->client.mutex);
788 list_del(&cli->head);
789 mutex_unlock(&drm->client.mutex);
794 nouveau_drm_postclose(struct drm_device *dev, struct drm_file *fpriv)
796 struct nouveau_cli *cli = nouveau_cli(fpriv);
797 nouveau_cli_destroy(cli);
798 pm_runtime_mark_last_busy(dev->dev);
799 pm_runtime_put_autosuspend(dev->dev);
802 static const struct drm_ioctl_desc
804 DRM_IOCTL_DEF_DRV(NOUVEAU_GETPARAM, nouveau_abi16_ioctl_getparam, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
805 DRM_IOCTL_DEF_DRV(NOUVEAU_SETPARAM, nouveau_abi16_ioctl_setparam, DRM_UNLOCKED|DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
806 DRM_IOCTL_DEF_DRV(NOUVEAU_CHANNEL_ALLOC, nouveau_abi16_ioctl_channel_alloc, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
807 DRM_IOCTL_DEF_DRV(NOUVEAU_CHANNEL_FREE, nouveau_abi16_ioctl_channel_free, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
808 DRM_IOCTL_DEF_DRV(NOUVEAU_GROBJ_ALLOC, nouveau_abi16_ioctl_grobj_alloc, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
809 DRM_IOCTL_DEF_DRV(NOUVEAU_NOTIFIEROBJ_ALLOC, nouveau_abi16_ioctl_notifierobj_alloc, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
810 DRM_IOCTL_DEF_DRV(NOUVEAU_GPUOBJ_FREE, nouveau_abi16_ioctl_gpuobj_free, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
811 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_NEW, nouveau_gem_ioctl_new, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
812 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_PUSHBUF, nouveau_gem_ioctl_pushbuf, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
813 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_CPU_PREP, nouveau_gem_ioctl_cpu_prep, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
814 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_CPU_FINI, nouveau_gem_ioctl_cpu_fini, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
815 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_INFO, nouveau_gem_ioctl_info, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
819 nouveau_drm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
821 struct drm_file *filp = file->private_data;
822 struct drm_device *dev = filp->minor->dev;
825 ret = pm_runtime_get_sync(dev->dev);
826 if (ret < 0 && ret != -EACCES)
829 switch (_IOC_NR(cmd) - DRM_COMMAND_BASE) {
830 case DRM_NOUVEAU_NVIF:
831 ret = usif_ioctl(filp, (void __user *)arg, _IOC_SIZE(cmd));
834 ret = drm_ioctl(file, cmd, arg);
838 pm_runtime_mark_last_busy(dev->dev);
839 pm_runtime_put_autosuspend(dev->dev);
843 static const struct file_operations
844 nouveau_driver_fops = {
845 .owner = THIS_MODULE,
847 .release = drm_release,
848 .unlocked_ioctl = nouveau_drm_ioctl,
849 .mmap = nouveau_ttm_mmap,
852 #if defined(CONFIG_COMPAT)
853 .compat_ioctl = nouveau_compat_ioctl,
855 .llseek = noop_llseek,
858 static struct drm_driver
862 DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME | DRIVER_RENDER,
864 .load = nouveau_drm_load,
865 .unload = nouveau_drm_unload,
866 .open = nouveau_drm_open,
867 .preclose = nouveau_drm_preclose,
868 .postclose = nouveau_drm_postclose,
869 .lastclose = nouveau_vga_lastclose,
871 #if defined(CONFIG_DEBUG_FS)
872 .debugfs_init = nouveau_debugfs_init,
873 .debugfs_cleanup = nouveau_debugfs_takedown,
876 .get_vblank_counter = drm_vblank_count,
877 .enable_vblank = nouveau_display_vblank_enable,
878 .disable_vblank = nouveau_display_vblank_disable,
879 .get_scanout_position = nouveau_display_scanoutpos,
880 .get_vblank_timestamp = nouveau_display_vblstamp,
882 .ioctls = nouveau_ioctls,
883 .num_ioctls = ARRAY_SIZE(nouveau_ioctls),
884 .fops = &nouveau_driver_fops,
886 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
887 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
888 .gem_prime_export = drm_gem_prime_export,
889 .gem_prime_import = drm_gem_prime_import,
890 .gem_prime_pin = nouveau_gem_prime_pin,
891 .gem_prime_res_obj = nouveau_gem_prime_res_obj,
892 .gem_prime_unpin = nouveau_gem_prime_unpin,
893 .gem_prime_get_sg_table = nouveau_gem_prime_get_sg_table,
894 .gem_prime_import_sg_table = nouveau_gem_prime_import_sg_table,
895 .gem_prime_vmap = nouveau_gem_prime_vmap,
896 .gem_prime_vunmap = nouveau_gem_prime_vunmap,
898 .gem_free_object = nouveau_gem_object_del,
899 .gem_open_object = nouveau_gem_object_open,
900 .gem_close_object = nouveau_gem_object_close,
902 .dumb_create = nouveau_display_dumb_create,
903 .dumb_map_offset = nouveau_display_dumb_map_offset,
904 .dumb_destroy = drm_gem_dumb_destroy,
909 .date = GIT_REVISION,
913 .major = DRIVER_MAJOR,
914 .minor = DRIVER_MINOR,
915 .patchlevel = DRIVER_PATCHLEVEL,
918 static struct pci_device_id
919 nouveau_drm_pci_table[] = {
921 PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_ANY_ID),
922 .class = PCI_BASE_CLASS_DISPLAY << 16,
923 .class_mask = 0xff << 16,
926 PCI_DEVICE(PCI_VENDOR_ID_NVIDIA_SGS, PCI_ANY_ID),
927 .class = PCI_BASE_CLASS_DISPLAY << 16,
928 .class_mask = 0xff << 16,
933 static int nouveau_pmops_runtime_suspend(struct device *dev)
935 struct pci_dev *pdev = to_pci_dev(dev);
936 struct drm_device *drm_dev = pci_get_drvdata(pdev);
939 if (nouveau_runtime_pm == 0) {
940 pm_runtime_forbid(dev);
944 /* are we optimus enabled? */
945 if (nouveau_runtime_pm == -1 && !nouveau_is_optimus() && !nouveau_is_v1_dsm()) {
946 DRM_DEBUG_DRIVER("failing to power off - not optimus\n");
947 pm_runtime_forbid(dev);
951 nv_debug_level(SILENT);
952 drm_kms_helper_poll_disable(drm_dev);
953 vga_switcheroo_set_dynamic_switch(pdev, VGA_SWITCHEROO_OFF);
954 nouveau_switcheroo_optimus_dsm();
955 ret = nouveau_do_suspend(drm_dev, true);
956 pci_save_state(pdev);
957 pci_disable_device(pdev);
958 pci_set_power_state(pdev, PCI_D3cold);
959 drm_dev->switch_power_state = DRM_SWITCH_POWER_DYNAMIC_OFF;
963 static int nouveau_pmops_runtime_resume(struct device *dev)
965 struct pci_dev *pdev = to_pci_dev(dev);
966 struct drm_device *drm_dev = pci_get_drvdata(pdev);
967 struct nvif_device *device = &nouveau_drm(drm_dev)->device;
970 if (nouveau_runtime_pm == 0)
973 pci_set_power_state(pdev, PCI_D0);
974 pci_restore_state(pdev);
975 ret = pci_enable_device(pdev);
978 pci_set_master(pdev);
980 ret = nouveau_do_resume(drm_dev);
981 drm_kms_helper_poll_enable(drm_dev);
983 nvif_mask(device, 0x88488, (1 << 25), (1 << 25));
984 vga_switcheroo_set_dynamic_switch(pdev, VGA_SWITCHEROO_ON);
985 drm_dev->switch_power_state = DRM_SWITCH_POWER_ON;
986 nv_debug_level(NORMAL);
990 static int nouveau_pmops_runtime_idle(struct device *dev)
992 struct pci_dev *pdev = to_pci_dev(dev);
993 struct drm_device *drm_dev = pci_get_drvdata(pdev);
994 struct nouveau_drm *drm = nouveau_drm(drm_dev);
995 struct drm_crtc *crtc;
997 if (nouveau_runtime_pm == 0) {
998 pm_runtime_forbid(dev);
1002 /* are we optimus enabled? */
1003 if (nouveau_runtime_pm == -1 && !nouveau_is_optimus() && !nouveau_is_v1_dsm()) {
1004 DRM_DEBUG_DRIVER("failing to power off - not optimus\n");
1005 pm_runtime_forbid(dev);
1009 /* if we have a hdmi audio device - make sure it has a driver loaded */
1010 if (drm->hdmi_device) {
1011 if (!drm->hdmi_device->driver) {
1012 DRM_DEBUG_DRIVER("failing to power off - no HDMI audio driver loaded\n");
1013 pm_runtime_mark_last_busy(dev);
1018 list_for_each_entry(crtc, &drm->dev->mode_config.crtc_list, head) {
1019 if (crtc->enabled) {
1020 DRM_DEBUG_DRIVER("failing to power off - crtc active\n");
1024 pm_runtime_mark_last_busy(dev);
1025 pm_runtime_autosuspend(dev);
1026 /* we don't want the main rpm_idle to call suspend - we want to autosuspend */
1030 static const struct dev_pm_ops nouveau_pm_ops = {
1031 .suspend = nouveau_pmops_suspend,
1032 .resume = nouveau_pmops_resume,
1033 .freeze = nouveau_pmops_freeze,
1034 .thaw = nouveau_pmops_thaw,
1035 .poweroff = nouveau_pmops_freeze,
1036 .restore = nouveau_pmops_resume,
1037 .runtime_suspend = nouveau_pmops_runtime_suspend,
1038 .runtime_resume = nouveau_pmops_runtime_resume,
1039 .runtime_idle = nouveau_pmops_runtime_idle,
1042 static struct pci_driver
1043 nouveau_drm_pci_driver = {
1045 .id_table = nouveau_drm_pci_table,
1046 .probe = nouveau_drm_probe,
1047 .remove = nouveau_drm_remove,
1048 .driver.pm = &nouveau_pm_ops,
1052 nouveau_platform_device_create_(struct platform_device *pdev, int size,
1055 struct drm_device *drm;
1058 err = nouveau_device_create_(pdev, NOUVEAU_BUS_PLATFORM,
1059 nouveau_platform_name(pdev),
1060 dev_name(&pdev->dev), nouveau_config,
1061 nouveau_debug, size, pobject);
1063 return ERR_PTR(err);
1065 drm = drm_dev_alloc(&driver, &pdev->dev);
1071 err = drm_dev_set_unique(drm, "%s", dev_name(&pdev->dev));
1075 drm->platformdev = pdev;
1076 platform_set_drvdata(pdev, drm);
1081 nouveau_object_ref(NULL, (struct nouveau_object **)pobject);
1083 return ERR_PTR(err);
1085 EXPORT_SYMBOL(nouveau_platform_device_create_);
1088 nouveau_drm_init(void)
1090 if (nouveau_modeset == -1) {
1091 #ifdef CONFIG_VGA_CONSOLE
1092 if (vgacon_text_force())
1093 nouveau_modeset = 0;
1097 if (!nouveau_modeset)
1100 nouveau_register_dsm_handler();
1101 return drm_pci_init(&driver, &nouveau_drm_pci_driver);
1105 nouveau_drm_exit(void)
1107 if (!nouveau_modeset)
1110 drm_pci_exit(&driver, &nouveau_drm_pci_driver);
1111 nouveau_unregister_dsm_handler();
1114 module_init(nouveau_drm_init);
1115 module_exit(nouveau_drm_exit);
1117 MODULE_DEVICE_TABLE(pci, nouveau_drm_pci_table);
1118 MODULE_AUTHOR(DRIVER_AUTHOR);
1119 MODULE_DESCRIPTION(DRIVER_DESC);
1120 MODULE_LICENSE("GPL and additional rights");