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>
36 #include <core/option.h>
38 #include <engine/device.h>
39 #include <engine/disp.h>
40 #include <engine/fifo.h>
41 #include <engine/software.h>
43 #include <subdev/vm.h>
45 #include "nouveau_drm.h"
46 #include "nouveau_dma.h"
47 #include "nouveau_ttm.h"
48 #include "nouveau_gem.h"
49 #include "nouveau_agp.h"
50 #include "nouveau_vga.h"
51 #include "nouveau_sysfs.h"
52 #include "nouveau_hwmon.h"
53 #include "nouveau_acpi.h"
54 #include "nouveau_bios.h"
55 #include "nouveau_ioctl.h"
56 #include "nouveau_abi16.h"
57 #include "nouveau_fbcon.h"
58 #include "nouveau_fence.h"
59 #include "nouveau_debugfs.h"
61 MODULE_PARM_DESC(config, "option string to pass to driver core");
62 static char *nouveau_config;
63 module_param_named(config, nouveau_config, charp, 0400);
65 MODULE_PARM_DESC(debug, "debug string to pass to driver core");
66 static char *nouveau_debug;
67 module_param_named(debug, nouveau_debug, charp, 0400);
69 MODULE_PARM_DESC(noaccel, "disable kernel/abi16 acceleration");
70 static int nouveau_noaccel = 0;
71 module_param_named(noaccel, nouveau_noaccel, int, 0400);
73 MODULE_PARM_DESC(modeset, "enable driver (default: auto, "
74 "0 = disabled, 1 = enabled, 2 = headless)");
75 int nouveau_modeset = -1;
76 module_param_named(modeset, nouveau_modeset, int, 0400);
78 MODULE_PARM_DESC(runpm, "disable (0), force enable (1), optimus only default (-1)");
79 int nouveau_runtime_pm = -1;
80 module_param_named(runpm, nouveau_runtime_pm, int, 0400);
82 static struct drm_driver driver;
85 nouveau_pci_name(struct pci_dev *pdev)
87 u64 name = (u64)pci_domain_nr(pdev->bus) << 32;
88 name |= pdev->bus->number << 16;
89 name |= PCI_SLOT(pdev->devfn) << 8;
90 return name | PCI_FUNC(pdev->devfn);
94 nouveau_platform_name(struct platform_device *platformdev)
96 return platformdev->id;
100 nouveau_name(struct drm_device *dev)
103 return nouveau_pci_name(dev->pdev);
105 return nouveau_platform_name(dev->platformdev);
109 nouveau_cli_create(u64 name, const char *sname,
110 int size, void **pcli)
112 struct nouveau_cli *cli;
116 ret = nouveau_client_create_(sname, name, nouveau_config,
117 nouveau_debug, size, pcli);
121 nouveau_client_destroy(&cli->base);
126 mutex_init(&cli->mutex);
131 nouveau_cli_destroy(struct nouveau_cli *cli)
133 struct nouveau_object *client = nv_object(cli);
134 nouveau_vm_ref(NULL, &cli->base.vm, NULL);
135 nouveau_client_fini(&cli->base, false);
136 atomic_set(&client->refcount, 1);
137 nouveau_object_ref(NULL, &client);
141 nouveau_accel_fini(struct nouveau_drm *drm)
143 nouveau_gpuobj_ref(NULL, &drm->notify);
144 nouveau_channel_del(&drm->channel);
145 nouveau_channel_del(&drm->cechan);
147 nouveau_fence(drm)->dtor(drm);
151 nouveau_accel_init(struct nouveau_drm *drm)
153 struct nouveau_device *device = nv_device(drm->device);
154 struct nouveau_object *object;
158 if (nouveau_noaccel || !nouveau_fifo(device) /*XXX*/)
161 /* initialise synchronisation routines */
162 if (device->card_type < NV_10) ret = nv04_fence_create(drm);
163 else if (device->card_type < NV_11 ||
164 device->chipset < 0x17) ret = nv10_fence_create(drm);
165 else if (device->card_type < NV_50) ret = nv17_fence_create(drm);
166 else if (device->chipset < 0x84) ret = nv50_fence_create(drm);
167 else if (device->card_type < NV_C0) ret = nv84_fence_create(drm);
168 else ret = nvc0_fence_create(drm);
170 NV_ERROR(drm, "failed to initialise sync subsystem, %d\n", ret);
171 nouveau_accel_fini(drm);
175 if (device->card_type >= NV_E0) {
176 ret = nouveau_channel_new(drm, &drm->client, NVDRM_DEVICE,
178 NVE0_CHANNEL_IND_ENGINE_CE0 |
179 NVE0_CHANNEL_IND_ENGINE_CE1, 0,
182 NV_ERROR(drm, "failed to create ce channel, %d\n", ret);
184 arg0 = NVE0_CHANNEL_IND_ENGINE_GR;
187 if (device->chipset >= 0xa3 &&
188 device->chipset != 0xaa &&
189 device->chipset != 0xac) {
190 ret = nouveau_channel_new(drm, &drm->client, NVDRM_DEVICE,
191 NVDRM_CHAN + 1, NvDmaFB, NvDmaTT,
194 NV_ERROR(drm, "failed to create ce channel, %d\n", ret);
203 ret = nouveau_channel_new(drm, &drm->client, NVDRM_DEVICE, NVDRM_CHAN,
204 arg0, arg1, &drm->channel);
206 NV_ERROR(drm, "failed to create kernel channel, %d\n", ret);
207 nouveau_accel_fini(drm);
211 ret = nouveau_object_new(nv_object(drm), NVDRM_CHAN, NVDRM_NVSW,
212 nouveau_abi16_swclass(drm), NULL, 0, &object);
214 struct nouveau_software_chan *swch = (void *)object->parent;
215 ret = RING_SPACE(drm->channel, 2);
217 if (device->card_type < NV_C0) {
218 BEGIN_NV04(drm->channel, NvSubSw, 0, 1);
219 OUT_RING (drm->channel, NVDRM_NVSW);
221 if (device->card_type < NV_E0) {
222 BEGIN_NVC0(drm->channel, FermiSw, 0, 1);
223 OUT_RING (drm->channel, 0x001f0000);
226 swch = (void *)object->parent;
227 swch->flip = nouveau_flip_complete;
228 swch->flip_data = drm->channel;
232 NV_ERROR(drm, "failed to allocate software object, %d\n", ret);
233 nouveau_accel_fini(drm);
237 if (device->card_type < NV_C0) {
238 ret = nouveau_gpuobj_new(drm->device, NULL, 32, 0, 0,
241 NV_ERROR(drm, "failed to allocate notifier, %d\n", ret);
242 nouveau_accel_fini(drm);
246 ret = nouveau_object_new(nv_object(drm),
247 drm->channel->handle, NvNotify0,
248 0x003d, &(struct nv_dma_class) {
249 .flags = NV_DMA_TARGET_VRAM |
251 .start = drm->notify->addr,
252 .limit = drm->notify->addr + 31
253 }, sizeof(struct nv_dma_class),
256 nouveau_accel_fini(drm);
262 nouveau_bo_move_init(drm);
265 static int nouveau_drm_probe(struct pci_dev *pdev,
266 const struct pci_device_id *pent)
268 struct nouveau_device *device;
269 struct apertures_struct *aper;
273 /* remove conflicting drivers (vesafb, efifb etc) */
274 aper = alloc_apertures(3);
278 aper->ranges[0].base = pci_resource_start(pdev, 1);
279 aper->ranges[0].size = pci_resource_len(pdev, 1);
282 if (pci_resource_len(pdev, 2)) {
283 aper->ranges[aper->count].base = pci_resource_start(pdev, 2);
284 aper->ranges[aper->count].size = pci_resource_len(pdev, 2);
288 if (pci_resource_len(pdev, 3)) {
289 aper->ranges[aper->count].base = pci_resource_start(pdev, 3);
290 aper->ranges[aper->count].size = pci_resource_len(pdev, 3);
295 boot = pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW;
297 remove_conflicting_framebuffers(aper, "nouveaufb", boot);
300 ret = nouveau_device_create(pdev, NOUVEAU_BUS_PCI,
301 nouveau_pci_name(pdev), pci_name(pdev),
302 nouveau_config, nouveau_debug, &device);
306 pci_set_master(pdev);
308 ret = drm_get_pci_dev(pdev, pent, &driver);
310 nouveau_object_ref(NULL, (struct nouveau_object **)&device);
317 #define PCI_CLASS_MULTIMEDIA_HD_AUDIO 0x0403
320 nouveau_get_hdmi_dev(struct nouveau_drm *drm)
322 struct pci_dev *pdev = drm->dev->pdev;
325 DRM_INFO("not a PCI device; no HDMI\n");
326 drm->hdmi_device = NULL;
330 /* subfunction one is a hdmi audio device? */
331 drm->hdmi_device = pci_get_bus_and_slot((unsigned int)pdev->bus->number,
332 PCI_DEVFN(PCI_SLOT(pdev->devfn), 1));
334 if (!drm->hdmi_device) {
335 NV_DEBUG(drm, "hdmi device not found %d %d %d\n", pdev->bus->number, PCI_SLOT(pdev->devfn), 1);
339 if ((drm->hdmi_device->class >> 8) != PCI_CLASS_MULTIMEDIA_HD_AUDIO) {
340 NV_DEBUG(drm, "possible hdmi device not audio %d\n", drm->hdmi_device->class);
341 pci_dev_put(drm->hdmi_device);
342 drm->hdmi_device = NULL;
348 nouveau_drm_load(struct drm_device *dev, unsigned long flags)
350 struct pci_dev *pdev = dev->pdev;
351 struct nouveau_device *device;
352 struct nouveau_drm *drm;
355 ret = nouveau_cli_create(nouveau_name(dev), "DRM", sizeof(*drm),
360 dev->dev_private = drm;
362 nouveau_client(drm)->debug = nouveau_dbgopt(nouveau_debug, "DRM");
364 INIT_LIST_HEAD(&drm->clients);
365 spin_lock_init(&drm->tile.lock);
367 nouveau_get_hdmi_dev(drm);
369 /* make sure AGP controller is in a consistent state before we
370 * (possibly) execute vbios init tables (see nouveau_agp.h)
372 if (pdev && drm_pci_device_is_agp(dev) && dev->agp) {
373 /* dummy device object, doesn't init anything, but allows
374 * agp code access to registers
376 ret = nouveau_object_new(nv_object(drm), NVDRM_CLIENT,
377 NVDRM_DEVICE, 0x0080,
378 &(struct nv_device_class) {
381 ~(NV_DEVICE_DISABLE_MMIO |
382 NV_DEVICE_DISABLE_IDENTIFY),
384 }, sizeof(struct nv_device_class),
389 nouveau_agp_reset(drm);
390 nouveau_object_del(nv_object(drm), NVDRM_CLIENT, NVDRM_DEVICE);
393 ret = nouveau_object_new(nv_object(drm), NVDRM_CLIENT, NVDRM_DEVICE,
394 0x0080, &(struct nv_device_class) {
398 }, sizeof(struct nv_device_class),
403 dev->irq_enabled = true;
405 /* workaround an odd issue on nvc1 by disabling the device's
406 * nosnoop capability. hopefully won't cause issues until a
407 * better fix is found - assuming there is one...
409 device = nv_device(drm->device);
410 if (nv_device(drm->device)->chipset == 0xc1)
411 nv_mask(device, 0x00088080, 0x00000800, 0x00000000);
413 nouveau_vga_init(drm);
414 nouveau_agp_init(drm);
416 if (device->card_type >= NV_50) {
417 ret = nouveau_vm_new(nv_device(drm->device), 0, (1ULL << 40),
418 0x1000, &drm->client.base.vm);
423 ret = nouveau_ttm_init(drm);
427 ret = nouveau_bios_init(dev);
431 ret = nouveau_display_create(dev);
435 if (dev->mode_config.num_crtc) {
436 ret = nouveau_display_init(dev);
441 nouveau_sysfs_init(dev);
442 nouveau_hwmon_init(dev);
443 nouveau_accel_init(drm);
444 nouveau_fbcon_init(dev);
446 if (nouveau_runtime_pm != 0) {
447 pm_runtime_use_autosuspend(dev->dev);
448 pm_runtime_set_autosuspend_delay(dev->dev, 5000);
449 pm_runtime_set_active(dev->dev);
450 pm_runtime_allow(dev->dev);
451 pm_runtime_mark_last_busy(dev->dev);
452 pm_runtime_put(dev->dev);
457 nouveau_display_destroy(dev);
459 nouveau_bios_takedown(dev);
461 nouveau_ttm_fini(drm);
463 nouveau_agp_fini(drm);
464 nouveau_vga_fini(drm);
466 nouveau_cli_destroy(&drm->client);
471 nouveau_drm_unload(struct drm_device *dev)
473 struct nouveau_drm *drm = nouveau_drm(dev);
475 pm_runtime_get_sync(dev->dev);
476 nouveau_fbcon_fini(dev);
477 nouveau_accel_fini(drm);
478 nouveau_hwmon_fini(dev);
479 nouveau_sysfs_fini(dev);
481 if (dev->mode_config.num_crtc)
482 nouveau_display_fini(dev);
483 nouveau_display_destroy(dev);
485 nouveau_bios_takedown(dev);
487 nouveau_ttm_fini(drm);
488 nouveau_agp_fini(drm);
489 nouveau_vga_fini(drm);
491 if (drm->hdmi_device)
492 pci_dev_put(drm->hdmi_device);
493 nouveau_cli_destroy(&drm->client);
498 nouveau_drm_device_remove(struct drm_device *dev)
500 struct nouveau_drm *drm = nouveau_drm(dev);
501 struct nouveau_object *device;
503 dev->irq_enabled = false;
504 device = drm->client.base.device;
507 nouveau_object_ref(NULL, &device);
508 nouveau_object_debug();
510 EXPORT_SYMBOL(nouveau_drm_device_remove);
513 nouveau_drm_remove(struct pci_dev *pdev)
515 struct drm_device *dev = pci_get_drvdata(pdev);
517 nouveau_drm_device_remove(dev);
521 nouveau_do_suspend(struct drm_device *dev, bool runtime)
523 struct nouveau_drm *drm = nouveau_drm(dev);
524 struct nouveau_cli *cli;
527 if (dev->mode_config.num_crtc && !runtime) {
528 NV_INFO(drm, "suspending display...\n");
529 ret = nouveau_display_suspend(dev);
534 NV_INFO(drm, "evicting buffers...\n");
535 ttm_bo_evict_mm(&drm->ttm.bdev, TTM_PL_VRAM);
537 NV_INFO(drm, "waiting for kernel channels to go idle...\n");
539 ret = nouveau_channel_idle(drm->cechan);
545 ret = nouveau_channel_idle(drm->channel);
550 NV_INFO(drm, "suspending client object trees...\n");
551 if (drm->fence && nouveau_fence(drm)->suspend) {
552 if (!nouveau_fence(drm)->suspend(drm)) {
558 list_for_each_entry(cli, &drm->clients, head) {
559 ret = nouveau_client_fini(&cli->base, true);
564 NV_INFO(drm, "suspending kernel object tree...\n");
565 ret = nouveau_client_fini(&drm->client.base, true);
569 nouveau_agp_fini(drm);
573 list_for_each_entry_continue_reverse(cli, &drm->clients, head) {
574 nouveau_client_init(&cli->base);
577 if (drm->fence && nouveau_fence(drm)->resume)
578 nouveau_fence(drm)->resume(drm);
581 if (dev->mode_config.num_crtc) {
582 NV_INFO(drm, "resuming display...\n");
583 nouveau_display_resume(dev);
588 int nouveau_pmops_suspend(struct device *dev)
590 struct pci_dev *pdev = to_pci_dev(dev);
591 struct drm_device *drm_dev = pci_get_drvdata(pdev);
594 if (drm_dev->switch_power_state == DRM_SWITCH_POWER_OFF ||
595 drm_dev->switch_power_state == DRM_SWITCH_POWER_DYNAMIC_OFF)
598 if (drm_dev->mode_config.num_crtc)
599 nouveau_fbcon_set_suspend(drm_dev, 1);
601 ret = nouveau_do_suspend(drm_dev, false);
605 pci_save_state(pdev);
606 pci_disable_device(pdev);
607 pci_set_power_state(pdev, PCI_D3hot);
612 nouveau_do_resume(struct drm_device *dev)
614 struct nouveau_drm *drm = nouveau_drm(dev);
615 struct nouveau_cli *cli;
617 NV_INFO(drm, "re-enabling device...\n");
619 nouveau_agp_reset(drm);
621 NV_INFO(drm, "resuming kernel object tree...\n");
622 nouveau_client_init(&drm->client.base);
623 nouveau_agp_init(drm);
625 NV_INFO(drm, "resuming client object trees...\n");
626 if (drm->fence && nouveau_fence(drm)->resume)
627 nouveau_fence(drm)->resume(drm);
629 list_for_each_entry(cli, &drm->clients, head) {
630 nouveau_client_init(&cli->base);
633 nouveau_run_vbios_init(dev);
635 if (dev->mode_config.num_crtc) {
636 NV_INFO(drm, "resuming display...\n");
637 nouveau_display_repin(dev);
643 int nouveau_pmops_resume(struct device *dev)
645 struct pci_dev *pdev = to_pci_dev(dev);
646 struct drm_device *drm_dev = pci_get_drvdata(pdev);
649 if (drm_dev->switch_power_state == DRM_SWITCH_POWER_OFF ||
650 drm_dev->switch_power_state == DRM_SWITCH_POWER_DYNAMIC_OFF)
653 pci_set_power_state(pdev, PCI_D0);
654 pci_restore_state(pdev);
655 ret = pci_enable_device(pdev);
658 pci_set_master(pdev);
660 ret = nouveau_do_resume(drm_dev);
664 if (drm_dev->mode_config.num_crtc) {
665 nouveau_display_resume(drm_dev);
666 nouveau_fbcon_set_suspend(drm_dev, 0);
672 static int nouveau_pmops_freeze(struct device *dev)
674 struct pci_dev *pdev = to_pci_dev(dev);
675 struct drm_device *drm_dev = pci_get_drvdata(pdev);
678 if (drm_dev->mode_config.num_crtc)
679 nouveau_fbcon_set_suspend(drm_dev, 1);
681 ret = nouveau_do_suspend(drm_dev, false);
685 static int nouveau_pmops_thaw(struct device *dev)
687 struct pci_dev *pdev = to_pci_dev(dev);
688 struct drm_device *drm_dev = pci_get_drvdata(pdev);
691 ret = nouveau_do_resume(drm_dev);
695 if (drm_dev->mode_config.num_crtc) {
696 nouveau_display_resume(drm_dev);
697 nouveau_fbcon_set_suspend(drm_dev, 0);
705 nouveau_drm_open(struct drm_device *dev, struct drm_file *fpriv)
707 struct nouveau_drm *drm = nouveau_drm(dev);
708 struct nouveau_cli *cli;
709 char name[32], tmpname[TASK_COMM_LEN];
712 /* need to bring up power immediately if opening device */
713 ret = pm_runtime_get_sync(dev->dev);
714 if (ret < 0 && ret != -EACCES)
717 get_task_comm(tmpname, current);
718 snprintf(name, sizeof(name), "%s[%d]", tmpname, pid_nr(fpriv->pid));
720 ret = nouveau_cli_create(nouveau_name(dev), name, sizeof(*cli),
726 if (nv_device(drm->device)->card_type >= NV_50) {
727 ret = nouveau_vm_new(nv_device(drm->device), 0, (1ULL << 40),
728 0x1000, &cli->base.vm);
730 nouveau_cli_destroy(cli);
735 fpriv->driver_priv = cli;
737 mutex_lock(&drm->client.mutex);
738 list_add(&cli->head, &drm->clients);
739 mutex_unlock(&drm->client.mutex);
742 pm_runtime_mark_last_busy(dev->dev);
743 pm_runtime_put_autosuspend(dev->dev);
749 nouveau_drm_preclose(struct drm_device *dev, struct drm_file *fpriv)
751 struct nouveau_cli *cli = nouveau_cli(fpriv);
752 struct nouveau_drm *drm = nouveau_drm(dev);
754 pm_runtime_get_sync(dev->dev);
757 nouveau_abi16_fini(cli->abi16);
759 mutex_lock(&drm->client.mutex);
760 list_del(&cli->head);
761 mutex_unlock(&drm->client.mutex);
766 nouveau_drm_postclose(struct drm_device *dev, struct drm_file *fpriv)
768 struct nouveau_cli *cli = nouveau_cli(fpriv);
769 nouveau_cli_destroy(cli);
770 pm_runtime_mark_last_busy(dev->dev);
771 pm_runtime_put_autosuspend(dev->dev);
774 static const struct drm_ioctl_desc
776 DRM_IOCTL_DEF_DRV(NOUVEAU_GETPARAM, nouveau_abi16_ioctl_getparam, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
777 DRM_IOCTL_DEF_DRV(NOUVEAU_SETPARAM, nouveau_abi16_ioctl_setparam, DRM_UNLOCKED|DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
778 DRM_IOCTL_DEF_DRV(NOUVEAU_CHANNEL_ALLOC, nouveau_abi16_ioctl_channel_alloc, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
779 DRM_IOCTL_DEF_DRV(NOUVEAU_CHANNEL_FREE, nouveau_abi16_ioctl_channel_free, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
780 DRM_IOCTL_DEF_DRV(NOUVEAU_GROBJ_ALLOC, nouveau_abi16_ioctl_grobj_alloc, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
781 DRM_IOCTL_DEF_DRV(NOUVEAU_NOTIFIEROBJ_ALLOC, nouveau_abi16_ioctl_notifierobj_alloc, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
782 DRM_IOCTL_DEF_DRV(NOUVEAU_GPUOBJ_FREE, nouveau_abi16_ioctl_gpuobj_free, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
783 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_NEW, nouveau_gem_ioctl_new, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
784 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_PUSHBUF, nouveau_gem_ioctl_pushbuf, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
785 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_CPU_PREP, nouveau_gem_ioctl_cpu_prep, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
786 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_CPU_FINI, nouveau_gem_ioctl_cpu_fini, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
787 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_INFO, nouveau_gem_ioctl_info, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
790 long nouveau_drm_ioctl(struct file *filp,
791 unsigned int cmd, unsigned long arg)
793 struct drm_file *file_priv = filp->private_data;
794 struct drm_device *dev;
796 dev = file_priv->minor->dev;
798 ret = pm_runtime_get_sync(dev->dev);
799 if (ret < 0 && ret != -EACCES)
802 ret = drm_ioctl(filp, cmd, arg);
804 pm_runtime_mark_last_busy(dev->dev);
805 pm_runtime_put_autosuspend(dev->dev);
808 static const struct file_operations
809 nouveau_driver_fops = {
810 .owner = THIS_MODULE,
812 .release = drm_release,
813 .unlocked_ioctl = nouveau_drm_ioctl,
814 .mmap = nouveau_ttm_mmap,
817 #if defined(CONFIG_COMPAT)
818 .compat_ioctl = nouveau_compat_ioctl,
820 .llseek = noop_llseek,
823 static struct drm_driver
827 DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME | DRIVER_RENDER,
829 .load = nouveau_drm_load,
830 .unload = nouveau_drm_unload,
831 .open = nouveau_drm_open,
832 .preclose = nouveau_drm_preclose,
833 .postclose = nouveau_drm_postclose,
834 .lastclose = nouveau_vga_lastclose,
836 #if defined(CONFIG_DEBUG_FS)
837 .debugfs_init = nouveau_debugfs_init,
838 .debugfs_cleanup = nouveau_debugfs_takedown,
841 .get_vblank_counter = drm_vblank_count,
842 .enable_vblank = nouveau_display_vblank_enable,
843 .disable_vblank = nouveau_display_vblank_disable,
844 .get_scanout_position = nouveau_display_scanoutpos,
845 .get_vblank_timestamp = nouveau_display_vblstamp,
847 .ioctls = nouveau_ioctls,
848 .num_ioctls = ARRAY_SIZE(nouveau_ioctls),
849 .fops = &nouveau_driver_fops,
851 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
852 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
853 .gem_prime_export = drm_gem_prime_export,
854 .gem_prime_import = drm_gem_prime_import,
855 .gem_prime_pin = nouveau_gem_prime_pin,
856 .gem_prime_unpin = nouveau_gem_prime_unpin,
857 .gem_prime_get_sg_table = nouveau_gem_prime_get_sg_table,
858 .gem_prime_import_sg_table = nouveau_gem_prime_import_sg_table,
859 .gem_prime_vmap = nouveau_gem_prime_vmap,
860 .gem_prime_vunmap = nouveau_gem_prime_vunmap,
862 .gem_free_object = nouveau_gem_object_del,
863 .gem_open_object = nouveau_gem_object_open,
864 .gem_close_object = nouveau_gem_object_close,
866 .dumb_create = nouveau_display_dumb_create,
867 .dumb_map_offset = nouveau_display_dumb_map_offset,
868 .dumb_destroy = drm_gem_dumb_destroy,
873 .date = GIT_REVISION,
877 .major = DRIVER_MAJOR,
878 .minor = DRIVER_MINOR,
879 .patchlevel = DRIVER_PATCHLEVEL,
882 static struct pci_device_id
883 nouveau_drm_pci_table[] = {
885 PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_ANY_ID),
886 .class = PCI_BASE_CLASS_DISPLAY << 16,
887 .class_mask = 0xff << 16,
890 PCI_DEVICE(PCI_VENDOR_ID_NVIDIA_SGS, PCI_ANY_ID),
891 .class = PCI_BASE_CLASS_DISPLAY << 16,
892 .class_mask = 0xff << 16,
897 static int nouveau_pmops_runtime_suspend(struct device *dev)
899 struct pci_dev *pdev = to_pci_dev(dev);
900 struct drm_device *drm_dev = pci_get_drvdata(pdev);
903 if (nouveau_runtime_pm == 0) {
904 pm_runtime_forbid(dev);
908 /* are we optimus enabled? */
909 if (nouveau_runtime_pm == -1 && !nouveau_is_optimus() && !nouveau_is_v1_dsm()) {
910 DRM_DEBUG_DRIVER("failing to power off - not optimus\n");
911 pm_runtime_forbid(dev);
915 nv_debug_level(SILENT);
916 drm_kms_helper_poll_disable(drm_dev);
917 vga_switcheroo_set_dynamic_switch(pdev, VGA_SWITCHEROO_OFF);
918 nouveau_switcheroo_optimus_dsm();
919 ret = nouveau_do_suspend(drm_dev, true);
920 pci_save_state(pdev);
921 pci_disable_device(pdev);
922 pci_set_power_state(pdev, PCI_D3cold);
923 drm_dev->switch_power_state = DRM_SWITCH_POWER_DYNAMIC_OFF;
927 static int nouveau_pmops_runtime_resume(struct device *dev)
929 struct pci_dev *pdev = to_pci_dev(dev);
930 struct drm_device *drm_dev = pci_get_drvdata(pdev);
931 struct nouveau_device *device = nouveau_dev(drm_dev);
934 if (nouveau_runtime_pm == 0)
937 pci_set_power_state(pdev, PCI_D0);
938 pci_restore_state(pdev);
939 ret = pci_enable_device(pdev);
942 pci_set_master(pdev);
944 ret = nouveau_do_resume(drm_dev);
945 drm_kms_helper_poll_enable(drm_dev);
947 nv_mask(device, 0x88488, (1 << 25), (1 << 25));
948 vga_switcheroo_set_dynamic_switch(pdev, VGA_SWITCHEROO_ON);
949 drm_dev->switch_power_state = DRM_SWITCH_POWER_ON;
950 nv_debug_level(NORMAL);
954 static int nouveau_pmops_runtime_idle(struct device *dev)
956 struct pci_dev *pdev = to_pci_dev(dev);
957 struct drm_device *drm_dev = pci_get_drvdata(pdev);
958 struct nouveau_drm *drm = nouveau_drm(drm_dev);
959 struct drm_crtc *crtc;
961 if (nouveau_runtime_pm == 0) {
962 pm_runtime_forbid(dev);
966 /* are we optimus enabled? */
967 if (nouveau_runtime_pm == -1 && !nouveau_is_optimus() && !nouveau_is_v1_dsm()) {
968 DRM_DEBUG_DRIVER("failing to power off - not optimus\n");
969 pm_runtime_forbid(dev);
973 /* if we have a hdmi audio device - make sure it has a driver loaded */
974 if (drm->hdmi_device) {
975 if (!drm->hdmi_device->driver) {
976 DRM_DEBUG_DRIVER("failing to power off - no HDMI audio driver loaded\n");
977 pm_runtime_mark_last_busy(dev);
982 list_for_each_entry(crtc, &drm->dev->mode_config.crtc_list, head) {
984 DRM_DEBUG_DRIVER("failing to power off - crtc active\n");
988 pm_runtime_mark_last_busy(dev);
989 pm_runtime_autosuspend(dev);
990 /* we don't want the main rpm_idle to call suspend - we want to autosuspend */
994 static const struct dev_pm_ops nouveau_pm_ops = {
995 .suspend = nouveau_pmops_suspend,
996 .resume = nouveau_pmops_resume,
997 .freeze = nouveau_pmops_freeze,
998 .thaw = nouveau_pmops_thaw,
999 .poweroff = nouveau_pmops_freeze,
1000 .restore = nouveau_pmops_resume,
1001 .runtime_suspend = nouveau_pmops_runtime_suspend,
1002 .runtime_resume = nouveau_pmops_runtime_resume,
1003 .runtime_idle = nouveau_pmops_runtime_idle,
1006 static struct pci_driver
1007 nouveau_drm_pci_driver = {
1009 .id_table = nouveau_drm_pci_table,
1010 .probe = nouveau_drm_probe,
1011 .remove = nouveau_drm_remove,
1012 .driver.pm = &nouveau_pm_ops,
1016 nouveau_platform_device_create_(struct platform_device *pdev, int size,
1019 struct drm_device *drm;
1022 err = nouveau_device_create_(pdev, NOUVEAU_BUS_PLATFORM,
1023 nouveau_platform_name(pdev),
1024 dev_name(&pdev->dev), nouveau_config,
1025 nouveau_debug, size, pobject);
1027 return ERR_PTR(err);
1029 drm = drm_dev_alloc(&driver, &pdev->dev);
1035 err = drm_dev_set_unique(drm, "%s", dev_name(&pdev->dev));
1039 drm->platformdev = pdev;
1040 platform_set_drvdata(pdev, drm);
1045 nouveau_object_ref(NULL, (struct nouveau_object **)pobject);
1047 return ERR_PTR(err);
1049 EXPORT_SYMBOL(nouveau_platform_device_create_);
1052 nouveau_drm_init(void)
1054 if (nouveau_modeset == -1) {
1055 #ifdef CONFIG_VGA_CONSOLE
1056 if (vgacon_text_force())
1057 nouveau_modeset = 0;
1061 if (!nouveau_modeset)
1064 nouveau_register_dsm_handler();
1065 return drm_pci_init(&driver, &nouveau_drm_pci_driver);
1069 nouveau_drm_exit(void)
1071 if (!nouveau_modeset)
1074 drm_pci_exit(&driver, &nouveau_drm_pci_driver);
1075 nouveau_unregister_dsm_handler();
1078 module_init(nouveau_drm_init);
1079 module_exit(nouveau_drm_exit);
1081 MODULE_DEVICE_TABLE(pci, nouveau_drm_pci_table);
1082 MODULE_AUTHOR(DRIVER_AUTHOR);
1083 MODULE_DESCRIPTION(DRIVER_DESC);
1084 MODULE_LICENSE("GPL and additional rights");