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_pm.h"
50 #include "nouveau_acpi.h"
51 #include "nouveau_bios.h"
52 #include "nouveau_ioctl.h"
53 #include "nouveau_abi16.h"
54 #include "nouveau_fbcon.h"
55 #include "nouveau_fence.h"
56 #include "nouveau_debugfs.h"
58 MODULE_PARM_DESC(config, "option string to pass to driver core");
59 static char *nouveau_config;
60 module_param_named(config, nouveau_config, charp, 0400);
62 MODULE_PARM_DESC(debug, "debug string to pass to driver core");
63 static char *nouveau_debug;
64 module_param_named(debug, nouveau_debug, charp, 0400);
66 MODULE_PARM_DESC(noaccel, "disable kernel/abi16 acceleration");
67 static int nouveau_noaccel = 0;
68 module_param_named(noaccel, nouveau_noaccel, int, 0400);
70 MODULE_PARM_DESC(modeset, "enable driver (default: auto, "
71 "0 = disabled, 1 = enabled, 2 = headless)");
72 int nouveau_modeset = -1;
73 module_param_named(modeset, nouveau_modeset, int, 0400);
75 MODULE_PARM_DESC(runpm, "disable (0), force enable (1), optimus only default (-1)");
76 int nouveau_runtime_pm = -1;
77 module_param_named(runpm, nouveau_runtime_pm, int, 0400);
79 static struct drm_driver driver;
82 nouveau_drm_vblank_handler(struct nouveau_eventh *event, int head)
84 struct nouveau_drm *drm =
85 container_of(event, struct nouveau_drm, vblank[head]);
86 drm_handle_vblank(drm->dev, head);
87 return NVKM_EVENT_KEEP;
91 nouveau_drm_vblank_enable(struct drm_device *dev, int head)
93 struct nouveau_drm *drm = nouveau_drm(dev);
94 struct nouveau_disp *pdisp = nouveau_disp(drm->device);
96 if (WARN_ON_ONCE(head > ARRAY_SIZE(drm->vblank)))
98 WARN_ON_ONCE(drm->vblank[head].func);
99 drm->vblank[head].func = nouveau_drm_vblank_handler;
100 nouveau_event_get(pdisp->vblank, head, &drm->vblank[head]);
105 nouveau_drm_vblank_disable(struct drm_device *dev, int head)
107 struct nouveau_drm *drm = nouveau_drm(dev);
108 struct nouveau_disp *pdisp = nouveau_disp(drm->device);
109 if (drm->vblank[head].func)
110 nouveau_event_put(pdisp->vblank, head, &drm->vblank[head]);
113 drm->vblank[head].func = NULL;
117 nouveau_name(struct pci_dev *pdev)
119 u64 name = (u64)pci_domain_nr(pdev->bus) << 32;
120 name |= pdev->bus->number << 16;
121 name |= PCI_SLOT(pdev->devfn) << 8;
122 return name | PCI_FUNC(pdev->devfn);
126 nouveau_cli_create(struct pci_dev *pdev, const char *name,
127 int size, void **pcli)
129 struct nouveau_cli *cli;
133 ret = nouveau_client_create_(name, nouveau_name(pdev), nouveau_config,
134 nouveau_debug, size, pcli);
138 nouveau_client_destroy(&cli->base);
143 mutex_init(&cli->mutex);
148 nouveau_cli_destroy(struct nouveau_cli *cli)
150 struct nouveau_object *client = nv_object(cli);
151 nouveau_vm_ref(NULL, &cli->base.vm, NULL);
152 nouveau_client_fini(&cli->base, false);
153 atomic_set(&client->refcount, 1);
154 nouveau_object_ref(NULL, &client);
158 nouveau_accel_fini(struct nouveau_drm *drm)
160 nouveau_gpuobj_ref(NULL, &drm->notify);
161 nouveau_channel_del(&drm->channel);
162 nouveau_channel_del(&drm->cechan);
164 nouveau_fence(drm)->dtor(drm);
168 nouveau_accel_init(struct nouveau_drm *drm)
170 struct nouveau_device *device = nv_device(drm->device);
171 struct nouveau_object *object;
175 if (nouveau_noaccel || !nouveau_fifo(device) /*XXX*/)
178 /* initialise synchronisation routines */
179 if (device->card_type < NV_10) ret = nv04_fence_create(drm);
180 else if (device->chipset < 0x17) ret = nv10_fence_create(drm);
181 else if (device->card_type < NV_50) ret = nv17_fence_create(drm);
182 else if (device->chipset < 0x84) ret = nv50_fence_create(drm);
183 else if (device->card_type < NV_C0) ret = nv84_fence_create(drm);
184 else ret = nvc0_fence_create(drm);
186 NV_ERROR(drm, "failed to initialise sync subsystem, %d\n", ret);
187 nouveau_accel_fini(drm);
191 if (device->card_type >= NV_E0) {
192 ret = nouveau_channel_new(drm, &drm->client, NVDRM_DEVICE,
194 NVE0_CHANNEL_IND_ENGINE_CE0 |
195 NVE0_CHANNEL_IND_ENGINE_CE1, 0,
198 NV_ERROR(drm, "failed to create ce channel, %d\n", ret);
200 arg0 = NVE0_CHANNEL_IND_ENGINE_GR;
203 if (device->chipset >= 0xa3 &&
204 device->chipset != 0xaa &&
205 device->chipset != 0xac) {
206 ret = nouveau_channel_new(drm, &drm->client, NVDRM_DEVICE,
207 NVDRM_CHAN + 1, NvDmaFB, NvDmaTT,
210 NV_ERROR(drm, "failed to create ce channel, %d\n", ret);
219 ret = nouveau_channel_new(drm, &drm->client, NVDRM_DEVICE, NVDRM_CHAN,
220 arg0, arg1, &drm->channel);
222 NV_ERROR(drm, "failed to create kernel channel, %d\n", ret);
223 nouveau_accel_fini(drm);
227 if (device->card_type < NV_C0) {
228 ret = nouveau_gpuobj_new(drm->device, NULL, 32, 0, 0,
231 NV_ERROR(drm, "failed to allocate notifier, %d\n", ret);
232 nouveau_accel_fini(drm);
236 ret = nouveau_object_new(nv_object(drm),
237 drm->channel->handle, NvNotify0,
238 0x003d, &(struct nv_dma_class) {
239 .flags = NV_DMA_TARGET_VRAM |
241 .start = drm->notify->addr,
242 .limit = drm->notify->addr + 31
243 }, sizeof(struct nv_dma_class),
246 nouveau_accel_fini(drm);
252 nouveau_bo_move_init(drm);
255 static int nouveau_drm_probe(struct pci_dev *pdev,
256 const struct pci_device_id *pent)
258 struct nouveau_device *device;
259 struct apertures_struct *aper;
263 /* remove conflicting drivers (vesafb, efifb etc) */
264 aper = alloc_apertures(3);
268 aper->ranges[0].base = pci_resource_start(pdev, 1);
269 aper->ranges[0].size = pci_resource_len(pdev, 1);
272 if (pci_resource_len(pdev, 2)) {
273 aper->ranges[aper->count].base = pci_resource_start(pdev, 2);
274 aper->ranges[aper->count].size = pci_resource_len(pdev, 2);
278 if (pci_resource_len(pdev, 3)) {
279 aper->ranges[aper->count].base = pci_resource_start(pdev, 3);
280 aper->ranges[aper->count].size = pci_resource_len(pdev, 3);
285 boot = pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW;
287 remove_conflicting_framebuffers(aper, "nouveaufb", boot);
290 ret = nouveau_device_create(pdev, nouveau_name(pdev), pci_name(pdev),
291 nouveau_config, nouveau_debug, &device);
295 pci_set_master(pdev);
297 ret = drm_get_pci_dev(pdev, pent, &driver);
299 nouveau_object_ref(NULL, (struct nouveau_object **)&device);
306 #define PCI_CLASS_MULTIMEDIA_HD_AUDIO 0x0403
309 nouveau_get_hdmi_dev(struct drm_device *dev)
311 struct nouveau_drm *drm = dev->dev_private;
312 struct pci_dev *pdev = dev->pdev;
314 /* subfunction one is a hdmi audio device? */
315 drm->hdmi_device = pci_get_bus_and_slot((unsigned int)pdev->bus->number,
316 PCI_DEVFN(PCI_SLOT(pdev->devfn), 1));
318 if (!drm->hdmi_device) {
319 DRM_INFO("hdmi device not found %d %d %d\n", pdev->bus->number, PCI_SLOT(pdev->devfn), 1);
323 if ((drm->hdmi_device->class >> 8) != PCI_CLASS_MULTIMEDIA_HD_AUDIO) {
324 DRM_INFO("possible hdmi device not audio %d\n", drm->hdmi_device->class);
325 pci_dev_put(drm->hdmi_device);
326 drm->hdmi_device = NULL;
332 nouveau_drm_load(struct drm_device *dev, unsigned long flags)
334 struct pci_dev *pdev = dev->pdev;
335 struct nouveau_device *device;
336 struct nouveau_drm *drm;
339 ret = nouveau_cli_create(pdev, "DRM", sizeof(*drm), (void**)&drm);
343 dev->dev_private = drm;
346 INIT_LIST_HEAD(&drm->clients);
347 spin_lock_init(&drm->tile.lock);
349 nouveau_get_hdmi_dev(dev);
351 /* make sure AGP controller is in a consistent state before we
352 * (possibly) execute vbios init tables (see nouveau_agp.h)
354 if (drm_pci_device_is_agp(dev) && dev->agp) {
355 /* dummy device object, doesn't init anything, but allows
356 * agp code access to registers
358 ret = nouveau_object_new(nv_object(drm), NVDRM_CLIENT,
359 NVDRM_DEVICE, 0x0080,
360 &(struct nv_device_class) {
363 ~(NV_DEVICE_DISABLE_MMIO |
364 NV_DEVICE_DISABLE_IDENTIFY),
366 }, sizeof(struct nv_device_class),
371 nouveau_agp_reset(drm);
372 nouveau_object_del(nv_object(drm), NVDRM_CLIENT, NVDRM_DEVICE);
375 ret = nouveau_object_new(nv_object(drm), NVDRM_CLIENT, NVDRM_DEVICE,
376 0x0080, &(struct nv_device_class) {
380 }, sizeof(struct nv_device_class),
385 /* workaround an odd issue on nvc1 by disabling the device's
386 * nosnoop capability. hopefully won't cause issues until a
387 * better fix is found - assuming there is one...
389 device = nv_device(drm->device);
390 if (nv_device(drm->device)->chipset == 0xc1)
391 nv_mask(device, 0x00088080, 0x00000800, 0x00000000);
393 nouveau_vga_init(drm);
394 nouveau_agp_init(drm);
396 if (device->card_type >= NV_50) {
397 ret = nouveau_vm_new(nv_device(drm->device), 0, (1ULL << 40),
398 0x1000, &drm->client.base.vm);
403 ret = nouveau_ttm_init(drm);
407 ret = nouveau_bios_init(dev);
411 ret = nouveau_display_create(dev);
415 if (dev->mode_config.num_crtc) {
416 ret = nouveau_display_init(dev);
421 nouveau_pm_init(dev);
423 nouveau_accel_init(drm);
424 nouveau_fbcon_init(dev);
426 if (nouveau_runtime_pm != 0) {
427 pm_runtime_use_autosuspend(dev->dev);
428 pm_runtime_set_autosuspend_delay(dev->dev, 5000);
429 pm_runtime_set_active(dev->dev);
430 pm_runtime_allow(dev->dev);
431 pm_runtime_mark_last_busy(dev->dev);
432 pm_runtime_put(dev->dev);
437 nouveau_display_destroy(dev);
439 nouveau_bios_takedown(dev);
441 nouveau_ttm_fini(drm);
443 nouveau_agp_fini(drm);
444 nouveau_vga_fini(drm);
446 nouveau_cli_destroy(&drm->client);
451 nouveau_drm_unload(struct drm_device *dev)
453 struct nouveau_drm *drm = nouveau_drm(dev);
455 pm_runtime_get_sync(dev->dev);
456 nouveau_fbcon_fini(dev);
457 nouveau_accel_fini(drm);
459 nouveau_pm_fini(dev);
461 if (dev->mode_config.num_crtc)
462 nouveau_display_fini(dev);
463 nouveau_display_destroy(dev);
465 nouveau_bios_takedown(dev);
467 nouveau_ttm_fini(drm);
468 nouveau_agp_fini(drm);
469 nouveau_vga_fini(drm);
471 if (drm->hdmi_device)
472 pci_dev_put(drm->hdmi_device);
473 nouveau_cli_destroy(&drm->client);
478 nouveau_drm_remove(struct pci_dev *pdev)
480 struct drm_device *dev = pci_get_drvdata(pdev);
481 struct nouveau_drm *drm = nouveau_drm(dev);
482 struct nouveau_object *device;
484 device = drm->client.base.device;
487 nouveau_object_ref(NULL, &device);
488 nouveau_object_debug();
492 nouveau_do_suspend(struct drm_device *dev)
494 struct nouveau_drm *drm = nouveau_drm(dev);
495 struct nouveau_cli *cli;
498 if (dev->mode_config.num_crtc) {
499 NV_SUSPEND(drm, "suspending display...\n");
500 ret = nouveau_display_suspend(dev);
505 NV_SUSPEND(drm, "evicting buffers...\n");
506 ttm_bo_evict_mm(&drm->ttm.bdev, TTM_PL_VRAM);
508 NV_SUSPEND(drm, "waiting for kernel channels to go idle...\n");
510 ret = nouveau_channel_idle(drm->cechan);
516 ret = nouveau_channel_idle(drm->channel);
521 NV_SUSPEND(drm, "suspending client object trees...\n");
522 if (drm->fence && nouveau_fence(drm)->suspend) {
523 if (!nouveau_fence(drm)->suspend(drm))
527 list_for_each_entry(cli, &drm->clients, head) {
528 ret = nouveau_client_fini(&cli->base, true);
533 NV_SUSPEND(drm, "suspending kernel object tree...\n");
534 ret = nouveau_client_fini(&drm->client.base, true);
538 nouveau_agp_fini(drm);
542 list_for_each_entry_continue_reverse(cli, &drm->clients, head) {
543 nouveau_client_init(&cli->base);
546 if (dev->mode_config.num_crtc) {
547 NV_SUSPEND(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 nv_suspend_set_printk_level(NV_DBG_INFO);
567 ret = nouveau_do_suspend(drm_dev);
571 pci_save_state(pdev);
572 pci_disable_device(pdev);
573 pci_set_power_state(pdev, PCI_D3hot);
574 nv_suspend_set_printk_level(NV_DBG_DEBUG);
580 nouveau_do_resume(struct drm_device *dev)
582 struct nouveau_drm *drm = nouveau_drm(dev);
583 struct nouveau_cli *cli;
585 NV_SUSPEND(drm, "re-enabling device...\n");
587 nouveau_agp_reset(drm);
589 NV_SUSPEND(drm, "resuming kernel object tree...\n");
590 nouveau_client_init(&drm->client.base);
591 nouveau_agp_init(drm);
593 NV_SUSPEND(drm, "resuming client object trees...\n");
594 if (drm->fence && nouveau_fence(drm)->resume)
595 nouveau_fence(drm)->resume(drm);
597 list_for_each_entry(cli, &drm->clients, head) {
598 nouveau_client_init(&cli->base);
601 nouveau_run_vbios_init(dev);
602 nouveau_pm_resume(dev);
604 if (dev->mode_config.num_crtc) {
605 NV_SUSPEND(drm, "resuming display...\n");
606 nouveau_display_repin(dev);
612 int nouveau_pmops_resume(struct device *dev)
614 struct pci_dev *pdev = to_pci_dev(dev);
615 struct drm_device *drm_dev = pci_get_drvdata(pdev);
618 if (drm_dev->switch_power_state == DRM_SWITCH_POWER_OFF ||
619 drm_dev->switch_power_state == DRM_SWITCH_POWER_DYNAMIC_OFF)
622 pci_set_power_state(pdev, PCI_D0);
623 pci_restore_state(pdev);
624 ret = pci_enable_device(pdev);
627 pci_set_master(pdev);
629 nv_suspend_set_printk_level(NV_DBG_INFO);
630 ret = nouveau_do_resume(drm_dev);
632 nv_suspend_set_printk_level(NV_DBG_DEBUG);
635 if (drm_dev->mode_config.num_crtc)
636 nouveau_fbcon_set_suspend(drm_dev, 0);
638 nouveau_fbcon_zfill_all(drm_dev);
639 if (drm_dev->mode_config.num_crtc)
640 nouveau_display_resume(drm_dev);
641 nv_suspend_set_printk_level(NV_DBG_DEBUG);
645 static int nouveau_pmops_freeze(struct device *dev)
647 struct pci_dev *pdev = to_pci_dev(dev);
648 struct drm_device *drm_dev = pci_get_drvdata(pdev);
651 nv_suspend_set_printk_level(NV_DBG_INFO);
652 if (drm_dev->mode_config.num_crtc)
653 nouveau_fbcon_set_suspend(drm_dev, 1);
655 ret = nouveau_do_suspend(drm_dev);
656 nv_suspend_set_printk_level(NV_DBG_DEBUG);
660 static int nouveau_pmops_thaw(struct device *dev)
662 struct pci_dev *pdev = to_pci_dev(dev);
663 struct drm_device *drm_dev = pci_get_drvdata(pdev);
666 nv_suspend_set_printk_level(NV_DBG_INFO);
667 ret = nouveau_do_resume(drm_dev);
669 nv_suspend_set_printk_level(NV_DBG_DEBUG);
672 if (drm_dev->mode_config.num_crtc)
673 nouveau_fbcon_set_suspend(drm_dev, 0);
674 nouveau_fbcon_zfill_all(drm_dev);
675 if (drm_dev->mode_config.num_crtc)
676 nouveau_display_resume(drm_dev);
677 nv_suspend_set_printk_level(NV_DBG_DEBUG);
683 nouveau_drm_open(struct drm_device *dev, struct drm_file *fpriv)
685 struct pci_dev *pdev = dev->pdev;
686 struct nouveau_drm *drm = nouveau_drm(dev);
687 struct nouveau_cli *cli;
688 char name[32], tmpname[TASK_COMM_LEN];
691 /* need to bring up power immediately if opening device */
692 ret = pm_runtime_get_sync(dev->dev);
696 get_task_comm(tmpname, current);
697 snprintf(name, sizeof(name), "%s[%d]", tmpname, pid_nr(fpriv->pid));
699 ret = nouveau_cli_create(pdev, name, sizeof(*cli), (void **)&cli);
703 if (nv_device(drm->device)->card_type >= NV_50) {
704 ret = nouveau_vm_new(nv_device(drm->device), 0, (1ULL << 40),
705 0x1000, &cli->base.vm);
707 nouveau_cli_destroy(cli);
712 fpriv->driver_priv = cli;
714 mutex_lock(&drm->client.mutex);
715 list_add(&cli->head, &drm->clients);
716 mutex_unlock(&drm->client.mutex);
719 pm_runtime_mark_last_busy(dev->dev);
720 pm_runtime_put_autosuspend(dev->dev);
726 nouveau_drm_preclose(struct drm_device *dev, struct drm_file *fpriv)
728 struct nouveau_cli *cli = nouveau_cli(fpriv);
729 struct nouveau_drm *drm = nouveau_drm(dev);
731 pm_runtime_get_sync(dev->dev);
734 nouveau_abi16_fini(cli->abi16);
736 mutex_lock(&drm->client.mutex);
737 list_del(&cli->head);
738 mutex_unlock(&drm->client.mutex);
743 nouveau_drm_postclose(struct drm_device *dev, struct drm_file *fpriv)
745 struct nouveau_cli *cli = nouveau_cli(fpriv);
746 nouveau_cli_destroy(cli);
747 pm_runtime_mark_last_busy(dev->dev);
748 pm_runtime_put_autosuspend(dev->dev);
751 static const struct drm_ioctl_desc
753 DRM_IOCTL_DEF_DRV(NOUVEAU_GETPARAM, nouveau_abi16_ioctl_getparam, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
754 DRM_IOCTL_DEF_DRV(NOUVEAU_SETPARAM, nouveau_abi16_ioctl_setparam, DRM_UNLOCKED|DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
755 DRM_IOCTL_DEF_DRV(NOUVEAU_CHANNEL_ALLOC, nouveau_abi16_ioctl_channel_alloc, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
756 DRM_IOCTL_DEF_DRV(NOUVEAU_CHANNEL_FREE, nouveau_abi16_ioctl_channel_free, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
757 DRM_IOCTL_DEF_DRV(NOUVEAU_GROBJ_ALLOC, nouveau_abi16_ioctl_grobj_alloc, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
758 DRM_IOCTL_DEF_DRV(NOUVEAU_NOTIFIEROBJ_ALLOC, nouveau_abi16_ioctl_notifierobj_alloc, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
759 DRM_IOCTL_DEF_DRV(NOUVEAU_GPUOBJ_FREE, nouveau_abi16_ioctl_gpuobj_free, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
760 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_NEW, nouveau_gem_ioctl_new, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
761 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_PUSHBUF, nouveau_gem_ioctl_pushbuf, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
762 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_CPU_PREP, nouveau_gem_ioctl_cpu_prep, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
763 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_CPU_FINI, nouveau_gem_ioctl_cpu_fini, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
764 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_INFO, nouveau_gem_ioctl_info, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
767 long nouveau_drm_ioctl(struct file *filp,
768 unsigned int cmd, unsigned long arg)
770 struct drm_file *file_priv = filp->private_data;
771 struct drm_device *dev;
773 dev = file_priv->minor->dev;
775 ret = pm_runtime_get_sync(dev->dev);
779 ret = drm_ioctl(filp, cmd, arg);
781 pm_runtime_mark_last_busy(dev->dev);
782 pm_runtime_put_autosuspend(dev->dev);
785 static const struct file_operations
786 nouveau_driver_fops = {
787 .owner = THIS_MODULE,
789 .release = drm_release,
790 .unlocked_ioctl = nouveau_drm_ioctl,
791 .mmap = nouveau_ttm_mmap,
794 #if defined(CONFIG_COMPAT)
795 .compat_ioctl = nouveau_compat_ioctl,
797 .llseek = noop_llseek,
800 static struct drm_driver
804 DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME | DRIVER_RENDER,
806 .load = nouveau_drm_load,
807 .unload = nouveau_drm_unload,
808 .open = nouveau_drm_open,
809 .preclose = nouveau_drm_preclose,
810 .postclose = nouveau_drm_postclose,
811 .lastclose = nouveau_vga_lastclose,
813 #if defined(CONFIG_DEBUG_FS)
814 .debugfs_init = nouveau_debugfs_init,
815 .debugfs_cleanup = nouveau_debugfs_takedown,
818 .get_vblank_counter = drm_vblank_count,
819 .enable_vblank = nouveau_drm_vblank_enable,
820 .disable_vblank = nouveau_drm_vblank_disable,
822 .ioctls = nouveau_ioctls,
823 .num_ioctls = ARRAY_SIZE(nouveau_ioctls),
824 .fops = &nouveau_driver_fops,
826 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
827 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
828 .gem_prime_export = drm_gem_prime_export,
829 .gem_prime_import = drm_gem_prime_import,
830 .gem_prime_pin = nouveau_gem_prime_pin,
831 .gem_prime_unpin = nouveau_gem_prime_unpin,
832 .gem_prime_get_sg_table = nouveau_gem_prime_get_sg_table,
833 .gem_prime_import_sg_table = nouveau_gem_prime_import_sg_table,
834 .gem_prime_vmap = nouveau_gem_prime_vmap,
835 .gem_prime_vunmap = nouveau_gem_prime_vunmap,
837 .gem_free_object = nouveau_gem_object_del,
838 .gem_open_object = nouveau_gem_object_open,
839 .gem_close_object = nouveau_gem_object_close,
841 .dumb_create = nouveau_display_dumb_create,
842 .dumb_map_offset = nouveau_display_dumb_map_offset,
843 .dumb_destroy = drm_gem_dumb_destroy,
848 .date = GIT_REVISION,
852 .major = DRIVER_MAJOR,
853 .minor = DRIVER_MINOR,
854 .patchlevel = DRIVER_PATCHLEVEL,
857 static struct pci_device_id
858 nouveau_drm_pci_table[] = {
860 PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_ANY_ID),
861 .class = PCI_BASE_CLASS_DISPLAY << 16,
862 .class_mask = 0xff << 16,
865 PCI_DEVICE(PCI_VENDOR_ID_NVIDIA_SGS, PCI_ANY_ID),
866 .class = PCI_BASE_CLASS_DISPLAY << 16,
867 .class_mask = 0xff << 16,
872 static int nouveau_pmops_runtime_suspend(struct device *dev)
874 struct pci_dev *pdev = to_pci_dev(dev);
875 struct drm_device *drm_dev = pci_get_drvdata(pdev);
878 if (nouveau_runtime_pm == 0)
881 drm_kms_helper_poll_disable(drm_dev);
882 vga_switcheroo_set_dynamic_switch(pdev, VGA_SWITCHEROO_OFF);
883 nouveau_switcheroo_optimus_dsm();
884 ret = nouveau_do_suspend(drm_dev);
885 pci_save_state(pdev);
886 pci_disable_device(pdev);
887 pci_set_power_state(pdev, PCI_D3cold);
888 drm_dev->switch_power_state = DRM_SWITCH_POWER_DYNAMIC_OFF;
892 static int nouveau_pmops_runtime_resume(struct device *dev)
894 struct pci_dev *pdev = to_pci_dev(dev);
895 struct drm_device *drm_dev = pci_get_drvdata(pdev);
896 struct nouveau_device *device = nouveau_dev(drm_dev);
899 if (nouveau_runtime_pm == 0)
902 pci_set_power_state(pdev, PCI_D0);
903 pci_restore_state(pdev);
904 ret = pci_enable_device(pdev);
907 pci_set_master(pdev);
909 ret = nouveau_do_resume(drm_dev);
910 if (drm_dev->mode_config.num_crtc)
911 nouveau_display_resume(drm_dev);
912 drm_kms_helper_poll_enable(drm_dev);
914 nv_mask(device, 0x88488, (1 << 25), (1 << 25));
915 vga_switcheroo_set_dynamic_switch(pdev, VGA_SWITCHEROO_ON);
916 drm_dev->switch_power_state = DRM_SWITCH_POWER_ON;
920 static int nouveau_pmops_runtime_idle(struct device *dev)
922 struct pci_dev *pdev = to_pci_dev(dev);
923 struct drm_device *drm_dev = pci_get_drvdata(pdev);
924 struct nouveau_drm *drm = nouveau_drm(drm_dev);
925 struct drm_crtc *crtc;
927 if (nouveau_runtime_pm == 0)
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");
936 /* if we have a hdmi audio device - make sure it has a driver loaded */
937 if (drm->hdmi_device) {
938 if (!drm->hdmi_device->driver) {
939 DRM_DEBUG_DRIVER("failing to power off - no HDMI audio driver loaded\n");
940 pm_runtime_mark_last_busy(dev);
945 list_for_each_entry(crtc, &drm->dev->mode_config.crtc_list, head) {
947 DRM_DEBUG_DRIVER("failing to power off - crtc active\n");
951 pm_runtime_mark_last_busy(dev);
952 pm_runtime_autosuspend(dev);
953 /* we don't want the main rpm_idle to call suspend - we want to autosuspend */
957 static const struct dev_pm_ops nouveau_pm_ops = {
958 .suspend = nouveau_pmops_suspend,
959 .resume = nouveau_pmops_resume,
960 .freeze = nouveau_pmops_freeze,
961 .thaw = nouveau_pmops_thaw,
962 .poweroff = nouveau_pmops_freeze,
963 .restore = nouveau_pmops_resume,
964 .runtime_suspend = nouveau_pmops_runtime_suspend,
965 .runtime_resume = nouveau_pmops_runtime_resume,
966 .runtime_idle = nouveau_pmops_runtime_idle,
969 static struct pci_driver
970 nouveau_drm_pci_driver = {
972 .id_table = nouveau_drm_pci_table,
973 .probe = nouveau_drm_probe,
974 .remove = nouveau_drm_remove,
975 .driver.pm = &nouveau_pm_ops,
979 nouveau_drm_init(void)
981 if (nouveau_modeset == -1) {
982 #ifdef CONFIG_VGA_CONSOLE
983 if (vgacon_text_force())
988 if (!nouveau_modeset)
991 nouveau_register_dsm_handler();
992 return drm_pci_init(&driver, &nouveau_drm_pci_driver);
996 nouveau_drm_exit(void)
998 if (!nouveau_modeset)
1001 drm_pci_exit(&driver, &nouveau_drm_pci_driver);
1002 nouveau_unregister_dsm_handler();
1005 module_init(nouveau_drm_init);
1006 module_exit(nouveau_drm_exit);
1008 MODULE_DEVICE_TABLE(pci, nouveau_drm_pci_table);
1009 MODULE_AUTHOR(DRIVER_AUTHOR);
1010 MODULE_DESCRIPTION(DRIVER_DESC);
1011 MODULE_LICENSE("GPL and additional rights");