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/delay.h>
27 #include <linux/module.h>
28 #include <linux/pci.h>
29 #include <linux/pm_runtime.h>
30 #include <linux/vga_switcheroo.h>
33 #include <drm/drm_crtc_helper.h>
35 #include <core/gpuobj.h>
36 #include <core/option.h>
38 #include <core/tegra.h>
40 #include <nvif/driver.h>
42 #include <nvif/class.h>
43 #include <nvif/cl0002.h>
44 #include <nvif/cla06f.h>
45 #include <nvif/if0004.h>
47 #include "nouveau_drv.h"
48 #include "nouveau_dma.h"
49 #include "nouveau_ttm.h"
50 #include "nouveau_gem.h"
51 #include "nouveau_vga.h"
52 #include "nouveau_led.h"
53 #include "nouveau_hwmon.h"
54 #include "nouveau_acpi.h"
55 #include "nouveau_bios.h"
56 #include "nouveau_ioctl.h"
57 #include "nouveau_abi16.h"
58 #include "nouveau_fbcon.h"
59 #include "nouveau_fence.h"
60 #include "nouveau_debugfs.h"
61 #include "nouveau_usif.h"
62 #include "nouveau_connector.h"
63 #include "nouveau_platform.h"
65 MODULE_PARM_DESC(config, "option string to pass to driver core");
66 static char *nouveau_config;
67 module_param_named(config, nouveau_config, charp, 0400);
69 MODULE_PARM_DESC(debug, "debug string to pass to driver core");
70 static char *nouveau_debug;
71 module_param_named(debug, nouveau_debug, charp, 0400);
73 MODULE_PARM_DESC(noaccel, "disable kernel/abi16 acceleration");
74 static int nouveau_noaccel = 0;
75 module_param_named(noaccel, nouveau_noaccel, int, 0400);
77 MODULE_PARM_DESC(modeset, "enable driver (default: auto, "
78 "0 = disabled, 1 = enabled, 2 = headless)");
79 int nouveau_modeset = -1;
80 module_param_named(modeset, nouveau_modeset, int, 0400);
82 MODULE_PARM_DESC(atomic, "Expose atomic ioctl (default: disabled)");
83 static int nouveau_atomic = 0;
84 module_param_named(atomic, nouveau_atomic, int, 0400);
86 MODULE_PARM_DESC(runpm, "disable (0), force enable (1), optimus only default (-1)");
87 static int nouveau_runtime_pm = -1;
88 module_param_named(runpm, nouveau_runtime_pm, int, 0400);
90 static struct drm_driver driver_stub;
91 static struct drm_driver driver_pci;
92 static struct drm_driver driver_platform;
95 nouveau_pci_name(struct pci_dev *pdev)
97 u64 name = (u64)pci_domain_nr(pdev->bus) << 32;
98 name |= pdev->bus->number << 16;
99 name |= PCI_SLOT(pdev->devfn) << 8;
100 return name | PCI_FUNC(pdev->devfn);
104 nouveau_platform_name(struct platform_device *platformdev)
106 return platformdev->id;
110 nouveau_name(struct drm_device *dev)
113 return nouveau_pci_name(dev->pdev);
115 return nouveau_platform_name(to_platform_device(dev->dev));
119 nouveau_cli_fini(struct nouveau_cli *cli)
121 nvkm_vm_ref(NULL, &nvxx_client(&cli->base)->vm, NULL);
122 usif_client_fini(cli);
123 nvif_device_fini(&cli->device);
124 nvif_client_fini(&cli->base);
128 nouveau_cli_init(struct nouveau_drm *drm, const char *sname,
129 struct nouveau_cli *cli)
131 u64 device = nouveau_name(drm->dev);
134 snprintf(cli->name, sizeof(cli->name), "%s", sname);
136 mutex_init(&cli->mutex);
137 usif_client_init(cli);
139 if (cli == &drm->client) {
140 ret = nvif_driver_init(NULL, nouveau_config, nouveau_debug,
141 cli->name, device, &cli->base);
143 ret = nvif_client_init(&drm->client.base, cli->name, device,
147 NV_ERROR(drm, "Client allocation failed: %d\n", ret);
151 ret = nvif_device_init(&cli->base.object, 0, NV_DEVICE,
152 &(struct nv_device_v0) {
154 }, sizeof(struct nv_device_v0),
157 NV_ERROR(drm, "Device allocation failed: %d\n", ret);
163 nouveau_cli_fini(cli);
168 nouveau_accel_fini(struct nouveau_drm *drm)
170 nouveau_channel_idle(drm->channel);
171 nvif_object_fini(&drm->ntfy);
172 nvkm_gpuobj_del(&drm->notify);
173 nvif_notify_fini(&drm->flip);
174 nvif_object_fini(&drm->nvsw);
175 nouveau_channel_del(&drm->channel);
177 nouveau_channel_idle(drm->cechan);
178 nvif_object_fini(&drm->ttm.copy);
179 nouveau_channel_del(&drm->cechan);
182 nouveau_fence(drm)->dtor(drm);
186 nouveau_accel_init(struct nouveau_drm *drm)
188 struct nvif_device *device = &drm->client.device;
189 struct nvif_sclass *sclass;
196 /* initialise synchronisation routines */
197 /*XXX: this is crap, but the fence/channel stuff is a little
198 * backwards in some places. this will be fixed.
200 ret = n = nvif_object_sclass_get(&device->object, &sclass);
204 for (ret = -ENOSYS, i = 0; i < n; i++) {
205 switch (sclass[i].oclass) {
206 case NV03_CHANNEL_DMA:
207 ret = nv04_fence_create(drm);
209 case NV10_CHANNEL_DMA:
210 ret = nv10_fence_create(drm);
212 case NV17_CHANNEL_DMA:
213 case NV40_CHANNEL_DMA:
214 ret = nv17_fence_create(drm);
216 case NV50_CHANNEL_GPFIFO:
217 ret = nv50_fence_create(drm);
219 case G82_CHANNEL_GPFIFO:
220 ret = nv84_fence_create(drm);
222 case FERMI_CHANNEL_GPFIFO:
223 case KEPLER_CHANNEL_GPFIFO_A:
224 case KEPLER_CHANNEL_GPFIFO_B:
225 case MAXWELL_CHANNEL_GPFIFO_A:
226 case PASCAL_CHANNEL_GPFIFO_A:
227 ret = nvc0_fence_create(drm);
234 nvif_object_sclass_put(&sclass);
236 NV_ERROR(drm, "failed to initialise sync subsystem, %d\n", ret);
237 nouveau_accel_fini(drm);
241 if (device->info.family >= NV_DEVICE_INFO_V0_KEPLER) {
242 ret = nouveau_channel_new(drm, &drm->client.device,
243 NVA06F_V0_ENGINE_CE0 |
244 NVA06F_V0_ENGINE_CE1,
247 NV_ERROR(drm, "failed to create ce channel, %d\n", ret);
249 arg0 = NVA06F_V0_ENGINE_GR;
252 if (device->info.chipset >= 0xa3 &&
253 device->info.chipset != 0xaa &&
254 device->info.chipset != 0xac) {
255 ret = nouveau_channel_new(drm, &drm->client.device,
256 NvDmaFB, NvDmaTT, &drm->cechan);
258 NV_ERROR(drm, "failed to create ce channel, %d\n", ret);
267 ret = nouveau_channel_new(drm, &drm->client.device,
268 arg0, arg1, &drm->channel);
270 NV_ERROR(drm, "failed to create kernel channel, %d\n", ret);
271 nouveau_accel_fini(drm);
275 ret = nvif_object_init(&drm->channel->user, NVDRM_NVSW,
276 nouveau_abi16_swclass(drm), NULL, 0, &drm->nvsw);
278 ret = RING_SPACE(drm->channel, 2);
280 if (device->info.family < NV_DEVICE_INFO_V0_FERMI) {
281 BEGIN_NV04(drm->channel, NvSubSw, 0, 1);
282 OUT_RING (drm->channel, NVDRM_NVSW);
284 if (device->info.family < NV_DEVICE_INFO_V0_KEPLER) {
285 BEGIN_NVC0(drm->channel, FermiSw, 0, 1);
286 OUT_RING (drm->channel, 0x001f0000);
290 ret = nvif_notify_init(&drm->nvsw, nouveau_flip_complete,
291 false, NV04_NVSW_NTFY_UEVENT,
292 NULL, 0, 0, &drm->flip);
294 ret = nvif_notify_get(&drm->flip);
296 nouveau_accel_fini(drm);
302 NV_ERROR(drm, "failed to allocate software object, %d\n", ret);
303 nouveau_accel_fini(drm);
307 if (device->info.family < NV_DEVICE_INFO_V0_FERMI) {
308 ret = nvkm_gpuobj_new(nvxx_device(&drm->client.device), 32, 0,
309 false, NULL, &drm->notify);
311 NV_ERROR(drm, "failed to allocate notifier, %d\n", ret);
312 nouveau_accel_fini(drm);
316 ret = nvif_object_init(&drm->channel->user, NvNotify0,
318 &(struct nv_dma_v0) {
319 .target = NV_DMA_V0_TARGET_VRAM,
320 .access = NV_DMA_V0_ACCESS_RDWR,
321 .start = drm->notify->addr,
322 .limit = drm->notify->addr + 31
323 }, sizeof(struct nv_dma_v0),
326 nouveau_accel_fini(drm);
332 nouveau_bo_move_init(drm);
335 static int nouveau_drm_probe(struct pci_dev *pdev,
336 const struct pci_device_id *pent)
338 struct nvkm_device *device;
339 struct apertures_struct *aper;
343 if (vga_switcheroo_client_probe_defer(pdev))
344 return -EPROBE_DEFER;
346 /* We need to check that the chipset is supported before booting
347 * fbdev off the hardware, as there's no way to put it back.
349 ret = nvkm_device_pci_new(pdev, NULL, "error", true, false, 0, &device);
353 nvkm_device_del(&device);
355 /* Remove conflicting drivers (vesafb, efifb etc). */
356 aper = alloc_apertures(3);
360 aper->ranges[0].base = pci_resource_start(pdev, 1);
361 aper->ranges[0].size = pci_resource_len(pdev, 1);
364 if (pci_resource_len(pdev, 2)) {
365 aper->ranges[aper->count].base = pci_resource_start(pdev, 2);
366 aper->ranges[aper->count].size = pci_resource_len(pdev, 2);
370 if (pci_resource_len(pdev, 3)) {
371 aper->ranges[aper->count].base = pci_resource_start(pdev, 3);
372 aper->ranges[aper->count].size = pci_resource_len(pdev, 3);
377 boot = pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW;
379 if (nouveau_modeset != 2)
380 drm_fb_helper_remove_conflicting_framebuffers(aper, "nouveaufb", boot);
383 ret = nvkm_device_pci_new(pdev, nouveau_config, nouveau_debug,
384 true, true, ~0ULL, &device);
388 pci_set_master(pdev);
391 driver_pci.driver_features |= DRIVER_ATOMIC;
393 ret = drm_get_pci_dev(pdev, pent, &driver_pci);
395 nvkm_device_del(&device);
402 #define PCI_CLASS_MULTIMEDIA_HD_AUDIO 0x0403
405 nouveau_get_hdmi_dev(struct nouveau_drm *drm)
407 struct pci_dev *pdev = drm->dev->pdev;
410 NV_DEBUG(drm, "not a PCI device; no HDMI\n");
411 drm->hdmi_device = NULL;
415 /* subfunction one is a hdmi audio device? */
416 drm->hdmi_device = pci_get_bus_and_slot((unsigned int)pdev->bus->number,
417 PCI_DEVFN(PCI_SLOT(pdev->devfn), 1));
419 if (!drm->hdmi_device) {
420 NV_DEBUG(drm, "hdmi device not found %d %d %d\n", pdev->bus->number, PCI_SLOT(pdev->devfn), 1);
424 if ((drm->hdmi_device->class >> 8) != PCI_CLASS_MULTIMEDIA_HD_AUDIO) {
425 NV_DEBUG(drm, "possible hdmi device not audio %d\n", drm->hdmi_device->class);
426 pci_dev_put(drm->hdmi_device);
427 drm->hdmi_device = NULL;
433 nouveau_drm_load(struct drm_device *dev, unsigned long flags)
435 struct nouveau_drm *drm;
438 if (!(drm = kzalloc(sizeof(*drm), GFP_KERNEL)))
440 dev->dev_private = drm;
443 ret = nouveau_cli_init(drm, "DRM", &drm->client);
447 dev->irq_enabled = true;
449 nvxx_client(&drm->client.base)->debug =
450 nvkm_dbgopt(nouveau_debug, "DRM");
452 INIT_LIST_HEAD(&drm->clients);
453 spin_lock_init(&drm->tile.lock);
455 nouveau_get_hdmi_dev(drm);
457 /* workaround an odd issue on nvc1 by disabling the device's
458 * nosnoop capability. hopefully won't cause issues until a
459 * better fix is found - assuming there is one...
461 if (drm->client.device.info.chipset == 0xc1)
462 nvif_mask(&drm->client.device.object, 0x00088080, 0x00000800, 0x00000000);
464 nouveau_vga_init(drm);
466 if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_TESLA) {
467 if (!nvxx_device(&drm->client.device)->mmu) {
472 ret = nvkm_vm_new(nvxx_device(&drm->client.device),
473 0, (1ULL << 40), 0x1000, NULL,
478 nvxx_client(&drm->client.base)->vm = drm->client.vm;
481 ret = nouveau_ttm_init(drm);
485 ret = nouveau_bios_init(dev);
489 ret = nouveau_display_create(dev);
493 if (dev->mode_config.num_crtc) {
494 ret = nouveau_display_init(dev);
499 nouveau_debugfs_init(drm);
500 nouveau_hwmon_init(dev);
501 nouveau_accel_init(drm);
502 nouveau_fbcon_init(dev);
503 nouveau_led_init(dev);
505 if (nouveau_pmops_runtime()) {
506 pm_runtime_use_autosuspend(dev->dev);
507 pm_runtime_set_autosuspend_delay(dev->dev, 5000);
508 pm_runtime_set_active(dev->dev);
509 pm_runtime_allow(dev->dev);
510 pm_runtime_mark_last_busy(dev->dev);
511 pm_runtime_put(dev->dev);
513 /* enable polling for external displays */
514 drm_kms_helper_poll_enable(dev);
519 nouveau_display_destroy(dev);
521 nouveau_bios_takedown(dev);
523 nouveau_ttm_fini(drm);
525 nouveau_vga_fini(drm);
527 nouveau_cli_fini(&drm->client);
533 nouveau_drm_unload(struct drm_device *dev)
535 struct nouveau_drm *drm = nouveau_drm(dev);
537 if (nouveau_pmops_runtime()) {
538 pm_runtime_get_sync(dev->dev);
539 pm_runtime_forbid(dev->dev);
542 nouveau_led_fini(dev);
543 nouveau_fbcon_fini(dev);
544 nouveau_accel_fini(drm);
545 nouveau_hwmon_fini(dev);
546 nouveau_debugfs_fini(drm);
548 if (dev->mode_config.num_crtc)
549 nouveau_display_fini(dev, false, false);
550 nouveau_display_destroy(dev);
552 nouveau_bios_takedown(dev);
554 nouveau_ttm_fini(drm);
555 nouveau_vga_fini(drm);
557 if (drm->hdmi_device)
558 pci_dev_put(drm->hdmi_device);
559 nouveau_cli_fini(&drm->client);
564 nouveau_drm_device_remove(struct drm_device *dev)
566 struct nouveau_drm *drm = nouveau_drm(dev);
567 struct nvkm_client *client;
568 struct nvkm_device *device;
570 dev->irq_enabled = false;
571 client = nvxx_client(&drm->client.base);
572 device = nvkm_device_find(client->device);
575 nvkm_device_del(&device);
579 nouveau_drm_remove(struct pci_dev *pdev)
581 struct drm_device *dev = pci_get_drvdata(pdev);
583 nouveau_drm_device_remove(dev);
587 nouveau_do_suspend(struct drm_device *dev, bool runtime)
589 struct nouveau_drm *drm = nouveau_drm(dev);
592 nouveau_led_suspend(dev);
594 if (dev->mode_config.num_crtc) {
595 NV_DEBUG(drm, "suspending console...\n");
596 nouveau_fbcon_set_suspend(dev, 1);
597 NV_DEBUG(drm, "suspending display...\n");
598 ret = nouveau_display_suspend(dev, runtime);
603 NV_DEBUG(drm, "evicting buffers...\n");
604 ttm_bo_evict_mm(&drm->ttm.bdev, TTM_PL_VRAM);
606 NV_DEBUG(drm, "waiting for kernel channels to go idle...\n");
608 ret = nouveau_channel_idle(drm->cechan);
614 ret = nouveau_channel_idle(drm->channel);
619 NV_DEBUG(drm, "suspending fence...\n");
620 if (drm->fence && nouveau_fence(drm)->suspend) {
621 if (!nouveau_fence(drm)->suspend(drm)) {
627 NV_DEBUG(drm, "suspending object tree...\n");
628 ret = nvif_client_suspend(&drm->client.base);
635 if (drm->fence && nouveau_fence(drm)->resume)
636 nouveau_fence(drm)->resume(drm);
639 if (dev->mode_config.num_crtc) {
640 NV_DEBUG(drm, "resuming display...\n");
641 nouveau_display_resume(dev, runtime);
647 nouveau_do_resume(struct drm_device *dev, bool runtime)
649 struct nouveau_drm *drm = nouveau_drm(dev);
651 NV_DEBUG(drm, "resuming object tree...\n");
652 nvif_client_resume(&drm->client.base);
654 NV_DEBUG(drm, "resuming fence...\n");
655 if (drm->fence && nouveau_fence(drm)->resume)
656 nouveau_fence(drm)->resume(drm);
658 nouveau_run_vbios_init(dev);
660 if (dev->mode_config.num_crtc) {
661 NV_DEBUG(drm, "resuming display...\n");
662 nouveau_display_resume(dev, runtime);
663 NV_DEBUG(drm, "resuming console...\n");
664 nouveau_fbcon_set_suspend(dev, 0);
667 nouveau_led_resume(dev);
673 nouveau_pmops_suspend(struct device *dev)
675 struct pci_dev *pdev = to_pci_dev(dev);
676 struct drm_device *drm_dev = pci_get_drvdata(pdev);
679 if (drm_dev->switch_power_state == DRM_SWITCH_POWER_OFF ||
680 drm_dev->switch_power_state == DRM_SWITCH_POWER_DYNAMIC_OFF)
683 ret = nouveau_do_suspend(drm_dev, false);
687 pci_save_state(pdev);
688 pci_disable_device(pdev);
689 pci_set_power_state(pdev, PCI_D3hot);
695 nouveau_pmops_resume(struct device *dev)
697 struct pci_dev *pdev = to_pci_dev(dev);
698 struct drm_device *drm_dev = pci_get_drvdata(pdev);
701 if (drm_dev->switch_power_state == DRM_SWITCH_POWER_OFF ||
702 drm_dev->switch_power_state == DRM_SWITCH_POWER_DYNAMIC_OFF)
705 pci_set_power_state(pdev, PCI_D0);
706 pci_restore_state(pdev);
707 ret = pci_enable_device(pdev);
710 pci_set_master(pdev);
712 ret = nouveau_do_resume(drm_dev, false);
714 /* Monitors may have been connected / disconnected during suspend */
715 schedule_work(&nouveau_drm(drm_dev)->hpd_work);
721 nouveau_pmops_freeze(struct device *dev)
723 struct pci_dev *pdev = to_pci_dev(dev);
724 struct drm_device *drm_dev = pci_get_drvdata(pdev);
725 return nouveau_do_suspend(drm_dev, false);
729 nouveau_pmops_thaw(struct device *dev)
731 struct pci_dev *pdev = to_pci_dev(dev);
732 struct drm_device *drm_dev = pci_get_drvdata(pdev);
733 return nouveau_do_resume(drm_dev, false);
737 nouveau_pmops_runtime(void)
739 if (nouveau_runtime_pm == -1)
740 return nouveau_is_optimus() || nouveau_is_v1_dsm();
741 return nouveau_runtime_pm == 1;
745 nouveau_pmops_runtime_suspend(struct device *dev)
747 struct pci_dev *pdev = to_pci_dev(dev);
748 struct drm_device *drm_dev = pci_get_drvdata(pdev);
751 if (!nouveau_pmops_runtime()) {
752 pm_runtime_forbid(dev);
756 drm_kms_helper_poll_disable(drm_dev);
757 vga_switcheroo_set_dynamic_switch(pdev, VGA_SWITCHEROO_OFF);
758 nouveau_switcheroo_optimus_dsm();
759 ret = nouveau_do_suspend(drm_dev, true);
760 pci_save_state(pdev);
761 pci_disable_device(pdev);
762 pci_ignore_hotplug(pdev);
763 pci_set_power_state(pdev, PCI_D3cold);
764 drm_dev->switch_power_state = DRM_SWITCH_POWER_DYNAMIC_OFF;
769 nouveau_pmops_runtime_resume(struct device *dev)
771 struct pci_dev *pdev = to_pci_dev(dev);
772 struct drm_device *drm_dev = pci_get_drvdata(pdev);
773 struct nvif_device *device = &nouveau_drm(drm_dev)->client.device;
776 if (!nouveau_pmops_runtime()) {
777 pm_runtime_forbid(dev);
781 pci_set_power_state(pdev, PCI_D0);
782 pci_restore_state(pdev);
783 ret = pci_enable_device(pdev);
786 pci_set_master(pdev);
788 ret = nouveau_do_resume(drm_dev, true);
791 nvif_mask(&device->object, 0x088488, (1 << 25), (1 << 25));
792 vga_switcheroo_set_dynamic_switch(pdev, VGA_SWITCHEROO_ON);
793 drm_dev->switch_power_state = DRM_SWITCH_POWER_ON;
795 /* Monitors may have been connected / disconnected during suspend */
796 schedule_work(&nouveau_drm(drm_dev)->hpd_work);
802 nouveau_pmops_runtime_idle(struct device *dev)
804 struct pci_dev *pdev = to_pci_dev(dev);
805 struct drm_device *drm_dev = pci_get_drvdata(pdev);
806 struct nouveau_drm *drm = nouveau_drm(drm_dev);
807 struct drm_crtc *crtc;
809 if (!nouveau_pmops_runtime()) {
810 pm_runtime_forbid(dev);
814 /* if we have a hdmi audio device - make sure it has a driver loaded */
815 if (drm->hdmi_device) {
816 if (!drm->hdmi_device->driver) {
817 DRM_DEBUG_DRIVER("failing to power off - no HDMI audio driver loaded\n");
818 pm_runtime_mark_last_busy(dev);
823 list_for_each_entry(crtc, &drm->dev->mode_config.crtc_list, head) {
825 DRM_DEBUG_DRIVER("failing to power off - crtc active\n");
829 pm_runtime_mark_last_busy(dev);
830 pm_runtime_autosuspend(dev);
831 /* we don't want the main rpm_idle to call suspend - we want to autosuspend */
836 nouveau_drm_open(struct drm_device *dev, struct drm_file *fpriv)
838 struct nouveau_drm *drm = nouveau_drm(dev);
839 struct nouveau_cli *cli;
840 char name[32], tmpname[TASK_COMM_LEN];
843 /* need to bring up power immediately if opening device */
844 ret = pm_runtime_get_sync(dev->dev);
845 if (ret < 0 && ret != -EACCES)
848 get_task_comm(tmpname, current);
849 snprintf(name, sizeof(name), "%s[%d]", tmpname, pid_nr(fpriv->pid));
851 if (!(cli = kzalloc(sizeof(*cli), GFP_KERNEL))) {
856 ret = nouveau_cli_init(drm, name, cli);
860 cli->base.super = false;
862 if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_TESLA) {
863 ret = nvkm_vm_new(nvxx_device(&drm->client.device), 0,
864 (1ULL << 40), 0x1000, NULL, &cli->vm);
868 nvxx_client(&cli->base)->vm = cli->vm;
871 fpriv->driver_priv = cli;
873 mutex_lock(&drm->client.mutex);
874 list_add(&cli->head, &drm->clients);
875 mutex_unlock(&drm->client.mutex);
879 nouveau_cli_fini(cli);
883 pm_runtime_mark_last_busy(dev->dev);
884 pm_runtime_put_autosuspend(dev->dev);
889 nouveau_drm_postclose(struct drm_device *dev, struct drm_file *fpriv)
891 struct nouveau_cli *cli = nouveau_cli(fpriv);
892 struct nouveau_drm *drm = nouveau_drm(dev);
894 pm_runtime_get_sync(dev->dev);
896 mutex_lock(&cli->mutex);
898 nouveau_abi16_fini(cli->abi16);
899 mutex_unlock(&cli->mutex);
901 mutex_lock(&drm->client.mutex);
902 list_del(&cli->head);
903 mutex_unlock(&drm->client.mutex);
905 nouveau_cli_fini(cli);
907 pm_runtime_mark_last_busy(dev->dev);
908 pm_runtime_put_autosuspend(dev->dev);
911 static const struct drm_ioctl_desc
913 DRM_IOCTL_DEF_DRV(NOUVEAU_GETPARAM, nouveau_abi16_ioctl_getparam, DRM_AUTH|DRM_RENDER_ALLOW),
914 DRM_IOCTL_DEF_DRV(NOUVEAU_SETPARAM, nouveau_abi16_ioctl_setparam, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
915 DRM_IOCTL_DEF_DRV(NOUVEAU_CHANNEL_ALLOC, nouveau_abi16_ioctl_channel_alloc, DRM_AUTH|DRM_RENDER_ALLOW),
916 DRM_IOCTL_DEF_DRV(NOUVEAU_CHANNEL_FREE, nouveau_abi16_ioctl_channel_free, DRM_AUTH|DRM_RENDER_ALLOW),
917 DRM_IOCTL_DEF_DRV(NOUVEAU_GROBJ_ALLOC, nouveau_abi16_ioctl_grobj_alloc, DRM_AUTH|DRM_RENDER_ALLOW),
918 DRM_IOCTL_DEF_DRV(NOUVEAU_NOTIFIEROBJ_ALLOC, nouveau_abi16_ioctl_notifierobj_alloc, DRM_AUTH|DRM_RENDER_ALLOW),
919 DRM_IOCTL_DEF_DRV(NOUVEAU_GPUOBJ_FREE, nouveau_abi16_ioctl_gpuobj_free, DRM_AUTH|DRM_RENDER_ALLOW),
920 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_NEW, nouveau_gem_ioctl_new, DRM_AUTH|DRM_RENDER_ALLOW),
921 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_PUSHBUF, nouveau_gem_ioctl_pushbuf, DRM_AUTH|DRM_RENDER_ALLOW),
922 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_CPU_PREP, nouveau_gem_ioctl_cpu_prep, DRM_AUTH|DRM_RENDER_ALLOW),
923 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_CPU_FINI, nouveau_gem_ioctl_cpu_fini, DRM_AUTH|DRM_RENDER_ALLOW),
924 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_INFO, nouveau_gem_ioctl_info, DRM_AUTH|DRM_RENDER_ALLOW),
928 nouveau_drm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
930 struct drm_file *filp = file->private_data;
931 struct drm_device *dev = filp->minor->dev;
934 ret = pm_runtime_get_sync(dev->dev);
935 if (ret < 0 && ret != -EACCES)
938 switch (_IOC_NR(cmd) - DRM_COMMAND_BASE) {
939 case DRM_NOUVEAU_NVIF:
940 ret = usif_ioctl(filp, (void __user *)arg, _IOC_SIZE(cmd));
943 ret = drm_ioctl(file, cmd, arg);
947 pm_runtime_mark_last_busy(dev->dev);
948 pm_runtime_put_autosuspend(dev->dev);
952 static const struct file_operations
953 nouveau_driver_fops = {
954 .owner = THIS_MODULE,
956 .release = drm_release,
957 .unlocked_ioctl = nouveau_drm_ioctl,
958 .mmap = nouveau_ttm_mmap,
961 #if defined(CONFIG_COMPAT)
962 .compat_ioctl = nouveau_compat_ioctl,
964 .llseek = noop_llseek,
967 static struct drm_driver
970 DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME | DRIVER_RENDER |
971 DRIVER_KMS_LEGACY_CONTEXT,
973 .load = nouveau_drm_load,
974 .unload = nouveau_drm_unload,
975 .open = nouveau_drm_open,
976 .postclose = nouveau_drm_postclose,
977 .lastclose = nouveau_vga_lastclose,
979 #if defined(CONFIG_DEBUG_FS)
980 .debugfs_init = nouveau_drm_debugfs_init,
983 .enable_vblank = nouveau_display_vblank_enable,
984 .disable_vblank = nouveau_display_vblank_disable,
985 .get_scanout_position = nouveau_display_scanoutpos,
986 .get_vblank_timestamp = drm_calc_vbltimestamp_from_scanoutpos,
988 .ioctls = nouveau_ioctls,
989 .num_ioctls = ARRAY_SIZE(nouveau_ioctls),
990 .fops = &nouveau_driver_fops,
992 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
993 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
994 .gem_prime_export = drm_gem_prime_export,
995 .gem_prime_import = drm_gem_prime_import,
996 .gem_prime_pin = nouveau_gem_prime_pin,
997 .gem_prime_res_obj = nouveau_gem_prime_res_obj,
998 .gem_prime_unpin = nouveau_gem_prime_unpin,
999 .gem_prime_get_sg_table = nouveau_gem_prime_get_sg_table,
1000 .gem_prime_import_sg_table = nouveau_gem_prime_import_sg_table,
1001 .gem_prime_vmap = nouveau_gem_prime_vmap,
1002 .gem_prime_vunmap = nouveau_gem_prime_vunmap,
1004 .gem_free_object_unlocked = nouveau_gem_object_del,
1005 .gem_open_object = nouveau_gem_object_open,
1006 .gem_close_object = nouveau_gem_object_close,
1008 .dumb_create = nouveau_display_dumb_create,
1009 .dumb_map_offset = nouveau_display_dumb_map_offset,
1011 .name = DRIVER_NAME,
1012 .desc = DRIVER_DESC,
1014 .date = GIT_REVISION,
1016 .date = DRIVER_DATE,
1018 .major = DRIVER_MAJOR,
1019 .minor = DRIVER_MINOR,
1020 .patchlevel = DRIVER_PATCHLEVEL,
1023 static struct pci_device_id
1024 nouveau_drm_pci_table[] = {
1026 PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_ANY_ID),
1027 .class = PCI_BASE_CLASS_DISPLAY << 16,
1028 .class_mask = 0xff << 16,
1031 PCI_DEVICE(PCI_VENDOR_ID_NVIDIA_SGS, PCI_ANY_ID),
1032 .class = PCI_BASE_CLASS_DISPLAY << 16,
1033 .class_mask = 0xff << 16,
1038 static void nouveau_display_options(void)
1040 DRM_DEBUG_DRIVER("Loading Nouveau with parameters:\n");
1042 DRM_DEBUG_DRIVER("... tv_disable : %d\n", nouveau_tv_disable);
1043 DRM_DEBUG_DRIVER("... ignorelid : %d\n", nouveau_ignorelid);
1044 DRM_DEBUG_DRIVER("... duallink : %d\n", nouveau_duallink);
1045 DRM_DEBUG_DRIVER("... nofbaccel : %d\n", nouveau_nofbaccel);
1046 DRM_DEBUG_DRIVER("... config : %s\n", nouveau_config);
1047 DRM_DEBUG_DRIVER("... debug : %s\n", nouveau_debug);
1048 DRM_DEBUG_DRIVER("... noaccel : %d\n", nouveau_noaccel);
1049 DRM_DEBUG_DRIVER("... modeset : %d\n", nouveau_modeset);
1050 DRM_DEBUG_DRIVER("... runpm : %d\n", nouveau_runtime_pm);
1051 DRM_DEBUG_DRIVER("... vram_pushbuf : %d\n", nouveau_vram_pushbuf);
1052 DRM_DEBUG_DRIVER("... hdmimhz : %d\n", nouveau_hdmimhz);
1055 static const struct dev_pm_ops nouveau_pm_ops = {
1056 .suspend = nouveau_pmops_suspend,
1057 .resume = nouveau_pmops_resume,
1058 .freeze = nouveau_pmops_freeze,
1059 .thaw = nouveau_pmops_thaw,
1060 .poweroff = nouveau_pmops_freeze,
1061 .restore = nouveau_pmops_resume,
1062 .runtime_suspend = nouveau_pmops_runtime_suspend,
1063 .runtime_resume = nouveau_pmops_runtime_resume,
1064 .runtime_idle = nouveau_pmops_runtime_idle,
1067 static struct pci_driver
1068 nouveau_drm_pci_driver = {
1070 .id_table = nouveau_drm_pci_table,
1071 .probe = nouveau_drm_probe,
1072 .remove = nouveau_drm_remove,
1073 .driver.pm = &nouveau_pm_ops,
1077 nouveau_platform_device_create(const struct nvkm_device_tegra_func *func,
1078 struct platform_device *pdev,
1079 struct nvkm_device **pdevice)
1081 struct drm_device *drm;
1084 err = nvkm_device_tegra_new(func, pdev, nouveau_config, nouveau_debug,
1085 true, true, ~0ULL, pdevice);
1089 drm = drm_dev_alloc(&driver_platform, &pdev->dev);
1095 platform_set_drvdata(pdev, drm);
1100 nvkm_device_del(pdevice);
1102 return ERR_PTR(err);
1106 nouveau_drm_init(void)
1108 driver_pci = driver_stub;
1109 driver_platform = driver_stub;
1111 nouveau_display_options();
1113 if (nouveau_modeset == -1) {
1114 if (vgacon_text_force())
1115 nouveau_modeset = 0;
1118 if (!nouveau_modeset)
1121 #ifdef CONFIG_NOUVEAU_PLATFORM_DRIVER
1122 platform_driver_register(&nouveau_platform_driver);
1125 nouveau_register_dsm_handler();
1126 nouveau_backlight_ctor();
1129 return pci_register_driver(&nouveau_drm_pci_driver);
1136 nouveau_drm_exit(void)
1138 if (!nouveau_modeset)
1142 pci_unregister_driver(&nouveau_drm_pci_driver);
1144 nouveau_backlight_dtor();
1145 nouveau_unregister_dsm_handler();
1147 #ifdef CONFIG_NOUVEAU_PLATFORM_DRIVER
1148 platform_driver_unregister(&nouveau_platform_driver);
1152 module_init(nouveau_drm_init);
1153 module_exit(nouveau_drm_exit);
1155 MODULE_DEVICE_TABLE(pci, nouveau_drm_pci_table);
1156 MODULE_AUTHOR(DRIVER_AUTHOR);
1157 MODULE_DESCRIPTION(DRIVER_DESC);
1158 MODULE_LICENSE("GPL and additional rights");