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"
54 #include "nouveau_connector.h"
56 MODULE_PARM_DESC(config, "option string to pass to driver core");
57 static char *nouveau_config;
58 module_param_named(config, nouveau_config, charp, 0400);
60 MODULE_PARM_DESC(debug, "debug string to pass to driver core");
61 static char *nouveau_debug;
62 module_param_named(debug, nouveau_debug, charp, 0400);
64 MODULE_PARM_DESC(noaccel, "disable kernel/abi16 acceleration");
65 static int nouveau_noaccel = 0;
66 module_param_named(noaccel, nouveau_noaccel, int, 0400);
68 MODULE_PARM_DESC(modeset, "enable driver (default: auto, "
69 "0 = disabled, 1 = enabled, 2 = headless)");
70 int nouveau_modeset = -1;
71 module_param_named(modeset, nouveau_modeset, int, 0400);
73 MODULE_PARM_DESC(runpm, "disable (0), force enable (1), optimus only default (-1)");
74 int nouveau_runtime_pm = -1;
75 module_param_named(runpm, nouveau_runtime_pm, int, 0400);
77 static struct drm_driver driver_stub;
78 static struct drm_driver driver_pci;
79 static struct drm_driver driver_platform;
82 nouveau_pci_name(struct pci_dev *pdev)
84 u64 name = (u64)pci_domain_nr(pdev->bus) << 32;
85 name |= pdev->bus->number << 16;
86 name |= PCI_SLOT(pdev->devfn) << 8;
87 return name | PCI_FUNC(pdev->devfn);
91 nouveau_platform_name(struct platform_device *platformdev)
93 return platformdev->id;
97 nouveau_name(struct drm_device *dev)
100 return nouveau_pci_name(dev->pdev);
102 return nouveau_platform_name(dev->platformdev);
106 nouveau_cli_create(u64 name, const char *sname,
107 int size, void **pcli)
109 struct nouveau_cli *cli = *pcli = kzalloc(size, GFP_KERNEL);
111 int ret = nvif_client_init(NULL, NULL, sname, name,
112 nouveau_config, nouveau_debug,
115 mutex_init(&cli->mutex);
116 usif_client_init(cli);
124 nouveau_cli_destroy(struct nouveau_cli *cli)
126 nouveau_vm_ref(NULL, &nvkm_client(&cli->base)->vm, NULL);
127 nvif_client_fini(&cli->base);
128 usif_client_fini(cli);
132 nouveau_accel_fini(struct nouveau_drm *drm)
134 nouveau_channel_del(&drm->channel);
135 nvif_object_fini(&drm->ntfy);
136 nouveau_gpuobj_ref(NULL, &drm->notify);
137 nvif_object_fini(&drm->nvsw);
138 nouveau_channel_del(&drm->cechan);
139 nvif_object_fini(&drm->ttm.copy);
141 nouveau_fence(drm)->dtor(drm);
145 nouveau_accel_init(struct nouveau_drm *drm)
147 struct nvif_device *device = &drm->device;
155 /* initialise synchronisation routines */
156 /*XXX: this is crap, but the fence/channel stuff is a little
157 * backwards in some places. this will be fixed.
159 ret = nvif_object_sclass(&device->base, sclass, ARRAY_SIZE(sclass));
163 for (ret = -ENOSYS, i = 0; ret && i < ARRAY_SIZE(sclass); i++) {
165 case NV03_CHANNEL_DMA:
166 ret = nv04_fence_create(drm);
168 case NV10_CHANNEL_DMA:
169 ret = nv10_fence_create(drm);
171 case NV17_CHANNEL_DMA:
172 case NV40_CHANNEL_DMA:
173 ret = nv17_fence_create(drm);
175 case NV50_CHANNEL_GPFIFO:
176 ret = nv50_fence_create(drm);
178 case G82_CHANNEL_GPFIFO:
179 ret = nv84_fence_create(drm);
181 case FERMI_CHANNEL_GPFIFO:
182 case KEPLER_CHANNEL_GPFIFO_A:
183 ret = nvc0_fence_create(drm);
191 NV_ERROR(drm, "failed to initialise sync subsystem, %d\n", ret);
192 nouveau_accel_fini(drm);
196 if (device->info.family >= NV_DEVICE_INFO_V0_KEPLER) {
197 ret = nouveau_channel_new(drm, &drm->device, NVDRM_CHAN + 1,
198 KEPLER_CHANNEL_GPFIFO_A_V0_ENGINE_CE0|
199 KEPLER_CHANNEL_GPFIFO_A_V0_ENGINE_CE1,
202 NV_ERROR(drm, "failed to create ce channel, %d\n", ret);
204 arg0 = KEPLER_CHANNEL_GPFIFO_A_V0_ENGINE_GR;
207 if (device->info.chipset >= 0xa3 &&
208 device->info.chipset != 0xaa &&
209 device->info.chipset != 0xac) {
210 ret = nouveau_channel_new(drm, &drm->device, NVDRM_CHAN + 1,
211 NvDmaFB, NvDmaTT, &drm->cechan);
213 NV_ERROR(drm, "failed to create ce channel, %d\n", ret);
222 ret = nouveau_channel_new(drm, &drm->device, NVDRM_CHAN, arg0, arg1,
225 NV_ERROR(drm, "failed to create kernel channel, %d\n", ret);
226 nouveau_accel_fini(drm);
230 ret = nvif_object_init(drm->channel->object, NULL, NVDRM_NVSW,
231 nouveau_abi16_swclass(drm), NULL, 0, &drm->nvsw);
233 struct nouveau_software_chan *swch;
234 ret = RING_SPACE(drm->channel, 2);
236 if (device->info.family < NV_DEVICE_INFO_V0_FERMI) {
237 BEGIN_NV04(drm->channel, NvSubSw, 0, 1);
238 OUT_RING (drm->channel, NVDRM_NVSW);
240 if (device->info.family < NV_DEVICE_INFO_V0_KEPLER) {
241 BEGIN_NVC0(drm->channel, FermiSw, 0, 1);
242 OUT_RING (drm->channel, 0x001f0000);
245 swch = (void *)nvkm_object(&drm->nvsw)->parent;
246 swch->flip = nouveau_flip_complete;
247 swch->flip_data = drm->channel;
251 NV_ERROR(drm, "failed to allocate software object, %d\n", ret);
252 nouveau_accel_fini(drm);
256 if (device->info.family < NV_DEVICE_INFO_V0_FERMI) {
257 ret = nouveau_gpuobj_new(nvkm_object(&drm->device), NULL, 32,
260 NV_ERROR(drm, "failed to allocate notifier, %d\n", ret);
261 nouveau_accel_fini(drm);
265 ret = nvif_object_init(drm->channel->object, NULL, NvNotify0,
267 &(struct nv_dma_v0) {
268 .target = NV_DMA_V0_TARGET_VRAM,
269 .access = NV_DMA_V0_ACCESS_RDWR,
270 .start = drm->notify->addr,
271 .limit = drm->notify->addr + 31
272 }, sizeof(struct nv_dma_v0),
275 nouveau_accel_fini(drm);
281 nouveau_bo_move_init(drm);
284 static int nouveau_drm_probe(struct pci_dev *pdev,
285 const struct pci_device_id *pent)
287 struct nouveau_device *device;
288 struct apertures_struct *aper;
292 /* remove conflicting drivers (vesafb, efifb etc) */
293 aper = alloc_apertures(3);
297 aper->ranges[0].base = pci_resource_start(pdev, 1);
298 aper->ranges[0].size = pci_resource_len(pdev, 1);
301 if (pci_resource_len(pdev, 2)) {
302 aper->ranges[aper->count].base = pci_resource_start(pdev, 2);
303 aper->ranges[aper->count].size = pci_resource_len(pdev, 2);
307 if (pci_resource_len(pdev, 3)) {
308 aper->ranges[aper->count].base = pci_resource_start(pdev, 3);
309 aper->ranges[aper->count].size = pci_resource_len(pdev, 3);
314 boot = pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW;
316 if (nouveau_modeset != 2)
317 remove_conflicting_framebuffers(aper, "nouveaufb", boot);
320 ret = nouveau_device_create(pdev, NOUVEAU_BUS_PCI,
321 nouveau_pci_name(pdev), pci_name(pdev),
322 nouveau_config, nouveau_debug, &device);
326 pci_set_master(pdev);
328 ret = drm_get_pci_dev(pdev, pent, &driver_pci);
330 nouveau_object_ref(NULL, (struct nouveau_object **)&device);
337 #define PCI_CLASS_MULTIMEDIA_HD_AUDIO 0x0403
340 nouveau_get_hdmi_dev(struct nouveau_drm *drm)
342 struct pci_dev *pdev = drm->dev->pdev;
345 DRM_INFO("not a PCI device; no HDMI\n");
346 drm->hdmi_device = NULL;
350 /* subfunction one is a hdmi audio device? */
351 drm->hdmi_device = pci_get_bus_and_slot((unsigned int)pdev->bus->number,
352 PCI_DEVFN(PCI_SLOT(pdev->devfn), 1));
354 if (!drm->hdmi_device) {
355 NV_DEBUG(drm, "hdmi device not found %d %d %d\n", pdev->bus->number, PCI_SLOT(pdev->devfn), 1);
359 if ((drm->hdmi_device->class >> 8) != PCI_CLASS_MULTIMEDIA_HD_AUDIO) {
360 NV_DEBUG(drm, "possible hdmi device not audio %d\n", drm->hdmi_device->class);
361 pci_dev_put(drm->hdmi_device);
362 drm->hdmi_device = NULL;
368 nouveau_drm_load(struct drm_device *dev, unsigned long flags)
370 struct pci_dev *pdev = dev->pdev;
371 struct nouveau_drm *drm;
374 ret = nouveau_cli_create(nouveau_name(dev), "DRM", sizeof(*drm),
379 dev->dev_private = drm;
381 nvkm_client(&drm->client.base)->debug =
382 nouveau_dbgopt(nouveau_debug, "DRM");
384 INIT_LIST_HEAD(&drm->clients);
385 spin_lock_init(&drm->tile.lock);
387 nouveau_get_hdmi_dev(drm);
389 /* make sure AGP controller is in a consistent state before we
390 * (possibly) execute vbios init tables (see nouveau_agp.h)
392 if (pdev && drm_pci_device_is_agp(dev) && dev->agp) {
393 const u64 enables = NV_DEVICE_V0_DISABLE_IDENTIFY |
394 NV_DEVICE_V0_DISABLE_MMIO;
395 /* dummy device object, doesn't init anything, but allows
396 * agp code access to registers
398 ret = nvif_device_init(&drm->client.base.base, NULL,
399 NVDRM_DEVICE, NV_DEVICE,
400 &(struct nv_device_v0) {
404 }, sizeof(struct nv_device_v0),
409 nouveau_agp_reset(drm);
410 nvif_device_fini(&drm->device);
413 ret = nvif_device_init(&drm->client.base.base, NULL, NVDRM_DEVICE,
415 &(struct nv_device_v0) {
419 }, sizeof(struct nv_device_v0),
424 dev->irq_enabled = true;
426 /* workaround an odd issue on nvc1 by disabling the device's
427 * nosnoop capability. hopefully won't cause issues until a
428 * better fix is found - assuming there is one...
430 if (drm->device.info.chipset == 0xc1)
431 nvif_mask(&drm->device, 0x00088080, 0x00000800, 0x00000000);
433 nouveau_vga_init(drm);
434 nouveau_agp_init(drm);
436 if (drm->device.info.family >= NV_DEVICE_INFO_V0_TESLA) {
437 ret = nouveau_vm_new(nvkm_device(&drm->device), 0, (1ULL << 40),
438 0x1000, &drm->client.vm);
442 nvkm_client(&drm->client.base)->vm = drm->client.vm;
445 ret = nouveau_ttm_init(drm);
449 ret = nouveau_bios_init(dev);
453 ret = nouveau_display_create(dev);
457 if (dev->mode_config.num_crtc) {
458 ret = nouveau_display_init(dev);
463 nouveau_sysfs_init(dev);
464 nouveau_hwmon_init(dev);
465 nouveau_accel_init(drm);
466 nouveau_fbcon_init(dev);
468 if (nouveau_runtime_pm != 0) {
469 pm_runtime_use_autosuspend(dev->dev);
470 pm_runtime_set_autosuspend_delay(dev->dev, 5000);
471 pm_runtime_set_active(dev->dev);
472 pm_runtime_allow(dev->dev);
473 pm_runtime_mark_last_busy(dev->dev);
474 pm_runtime_put(dev->dev);
479 nouveau_display_destroy(dev);
481 nouveau_bios_takedown(dev);
483 nouveau_ttm_fini(drm);
485 nouveau_agp_fini(drm);
486 nouveau_vga_fini(drm);
488 nvif_device_fini(&drm->device);
489 nouveau_cli_destroy(&drm->client);
494 nouveau_drm_unload(struct drm_device *dev)
496 struct nouveau_drm *drm = nouveau_drm(dev);
498 pm_runtime_get_sync(dev->dev);
499 nouveau_fbcon_fini(dev);
500 nouveau_accel_fini(drm);
501 nouveau_hwmon_fini(dev);
502 nouveau_sysfs_fini(dev);
504 if (dev->mode_config.num_crtc)
505 nouveau_display_fini(dev);
506 nouveau_display_destroy(dev);
508 nouveau_bios_takedown(dev);
510 nouveau_ttm_fini(drm);
511 nouveau_agp_fini(drm);
512 nouveau_vga_fini(drm);
514 nvif_device_fini(&drm->device);
515 if (drm->hdmi_device)
516 pci_dev_put(drm->hdmi_device);
517 nouveau_cli_destroy(&drm->client);
522 nouveau_drm_device_remove(struct drm_device *dev)
524 struct nouveau_drm *drm = nouveau_drm(dev);
525 struct nouveau_client *client;
526 struct nouveau_object *device;
528 dev->irq_enabled = false;
529 client = nvkm_client(&drm->client.base);
530 device = client->device;
533 nouveau_object_ref(NULL, &device);
534 nouveau_object_debug();
536 EXPORT_SYMBOL(nouveau_drm_device_remove);
539 nouveau_drm_remove(struct pci_dev *pdev)
541 struct drm_device *dev = pci_get_drvdata(pdev);
543 nouveau_drm_device_remove(dev);
547 nouveau_do_suspend(struct drm_device *dev, bool runtime)
549 struct nouveau_drm *drm = nouveau_drm(dev);
550 struct nouveau_cli *cli;
553 if (dev->mode_config.num_crtc) {
554 NV_INFO(drm, "suspending console...\n");
555 nouveau_fbcon_set_suspend(dev, 1);
556 NV_INFO(drm, "suspending display...\n");
557 ret = nouveau_display_suspend(dev, runtime);
562 NV_INFO(drm, "evicting buffers...\n");
563 ttm_bo_evict_mm(&drm->ttm.bdev, TTM_PL_VRAM);
565 NV_INFO(drm, "waiting for kernel channels to go idle...\n");
567 ret = nouveau_channel_idle(drm->cechan);
573 ret = nouveau_channel_idle(drm->channel);
578 NV_INFO(drm, "suspending client object trees...\n");
579 if (drm->fence && nouveau_fence(drm)->suspend) {
580 if (!nouveau_fence(drm)->suspend(drm)) {
586 list_for_each_entry(cli, &drm->clients, head) {
587 ret = nvif_client_suspend(&cli->base);
592 NV_INFO(drm, "suspending kernel object tree...\n");
593 ret = nvif_client_suspend(&drm->client.base);
597 nouveau_agp_fini(drm);
601 list_for_each_entry_continue_reverse(cli, &drm->clients, head) {
602 nvif_client_resume(&cli->base);
605 if (drm->fence && nouveau_fence(drm)->resume)
606 nouveau_fence(drm)->resume(drm);
609 if (dev->mode_config.num_crtc) {
610 NV_INFO(drm, "resuming display...\n");
611 nouveau_display_resume(dev, runtime);
617 nouveau_do_resume(struct drm_device *dev, bool runtime)
619 struct nouveau_drm *drm = nouveau_drm(dev);
620 struct nouveau_cli *cli;
622 NV_INFO(drm, "re-enabling device...\n");
624 nouveau_agp_reset(drm);
626 NV_INFO(drm, "resuming kernel object tree...\n");
627 nvif_client_resume(&drm->client.base);
628 nouveau_agp_init(drm);
630 NV_INFO(drm, "resuming client object trees...\n");
631 if (drm->fence && nouveau_fence(drm)->resume)
632 nouveau_fence(drm)->resume(drm);
634 list_for_each_entry(cli, &drm->clients, head) {
635 nvif_client_resume(&cli->base);
638 nouveau_run_vbios_init(dev);
640 if (dev->mode_config.num_crtc) {
641 NV_INFO(drm, "resuming display...\n");
642 nouveau_display_resume(dev, runtime);
643 NV_INFO(drm, "resuming console...\n");
644 nouveau_fbcon_set_suspend(dev, 0);
651 nouveau_pmops_suspend(struct device *dev)
653 struct pci_dev *pdev = to_pci_dev(dev);
654 struct drm_device *drm_dev = pci_get_drvdata(pdev);
657 if (drm_dev->switch_power_state == DRM_SWITCH_POWER_OFF ||
658 drm_dev->switch_power_state == DRM_SWITCH_POWER_DYNAMIC_OFF)
661 ret = nouveau_do_suspend(drm_dev, false);
665 pci_save_state(pdev);
666 pci_disable_device(pdev);
667 pci_ignore_hotplug(pdev);
668 pci_set_power_state(pdev, PCI_D3hot);
673 nouveau_pmops_resume(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 pci_set_power_state(pdev, PCI_D0);
684 pci_restore_state(pdev);
685 ret = pci_enable_device(pdev);
688 pci_set_master(pdev);
690 return nouveau_do_resume(drm_dev, false);
694 nouveau_pmops_freeze(struct device *dev)
696 struct pci_dev *pdev = to_pci_dev(dev);
697 struct drm_device *drm_dev = pci_get_drvdata(pdev);
698 return nouveau_do_suspend(drm_dev, false);
702 nouveau_pmops_thaw(struct device *dev)
704 struct pci_dev *pdev = to_pci_dev(dev);
705 struct drm_device *drm_dev = pci_get_drvdata(pdev);
706 return nouveau_do_resume(drm_dev, false);
710 nouveau_pmops_runtime_suspend(struct device *dev)
712 struct pci_dev *pdev = to_pci_dev(dev);
713 struct drm_device *drm_dev = pci_get_drvdata(pdev);
716 if (nouveau_runtime_pm == 0) {
717 pm_runtime_forbid(dev);
721 /* are we optimus enabled? */
722 if (nouveau_runtime_pm == -1 && !nouveau_is_optimus() && !nouveau_is_v1_dsm()) {
723 DRM_DEBUG_DRIVER("failing to power off - not optimus\n");
724 pm_runtime_forbid(dev);
728 nv_debug_level(SILENT);
729 drm_kms_helper_poll_disable(drm_dev);
730 vga_switcheroo_set_dynamic_switch(pdev, VGA_SWITCHEROO_OFF);
731 nouveau_switcheroo_optimus_dsm();
732 ret = nouveau_do_suspend(drm_dev, true);
733 pci_save_state(pdev);
734 pci_disable_device(pdev);
735 pci_set_power_state(pdev, PCI_D3cold);
736 drm_dev->switch_power_state = DRM_SWITCH_POWER_DYNAMIC_OFF;
741 nouveau_pmops_runtime_resume(struct device *dev)
743 struct pci_dev *pdev = to_pci_dev(dev);
744 struct drm_device *drm_dev = pci_get_drvdata(pdev);
745 struct nvif_device *device = &nouveau_drm(drm_dev)->device;
748 if (nouveau_runtime_pm == 0)
751 pci_set_power_state(pdev, PCI_D0);
752 pci_restore_state(pdev);
753 ret = pci_enable_device(pdev);
756 pci_set_master(pdev);
758 ret = nouveau_do_resume(drm_dev, true);
759 drm_kms_helper_poll_enable(drm_dev);
761 nvif_mask(device, 0x88488, (1 << 25), (1 << 25));
762 vga_switcheroo_set_dynamic_switch(pdev, VGA_SWITCHEROO_ON);
763 drm_dev->switch_power_state = DRM_SWITCH_POWER_ON;
764 nv_debug_level(NORMAL);
769 nouveau_pmops_runtime_idle(struct device *dev)
771 struct pci_dev *pdev = to_pci_dev(dev);
772 struct drm_device *drm_dev = pci_get_drvdata(pdev);
773 struct nouveau_drm *drm = nouveau_drm(drm_dev);
774 struct drm_crtc *crtc;
776 if (nouveau_runtime_pm == 0) {
777 pm_runtime_forbid(dev);
781 /* are we optimus enabled? */
782 if (nouveau_runtime_pm == -1 && !nouveau_is_optimus() && !nouveau_is_v1_dsm()) {
783 DRM_DEBUG_DRIVER("failing to power off - not optimus\n");
784 pm_runtime_forbid(dev);
788 /* if we have a hdmi audio device - make sure it has a driver loaded */
789 if (drm->hdmi_device) {
790 if (!drm->hdmi_device->driver) {
791 DRM_DEBUG_DRIVER("failing to power off - no HDMI audio driver loaded\n");
792 pm_runtime_mark_last_busy(dev);
797 list_for_each_entry(crtc, &drm->dev->mode_config.crtc_list, head) {
799 DRM_DEBUG_DRIVER("failing to power off - crtc active\n");
803 pm_runtime_mark_last_busy(dev);
804 pm_runtime_autosuspend(dev);
805 /* we don't want the main rpm_idle to call suspend - we want to autosuspend */
810 nouveau_drm_open(struct drm_device *dev, struct drm_file *fpriv)
812 struct nouveau_drm *drm = nouveau_drm(dev);
813 struct nouveau_cli *cli;
814 char name[32], tmpname[TASK_COMM_LEN];
817 /* need to bring up power immediately if opening device */
818 ret = pm_runtime_get_sync(dev->dev);
819 if (ret < 0 && ret != -EACCES)
822 get_task_comm(tmpname, current);
823 snprintf(name, sizeof(name), "%s[%d]", tmpname, pid_nr(fpriv->pid));
825 ret = nouveau_cli_create(nouveau_name(dev), name, sizeof(*cli),
831 cli->base.super = false;
833 if (drm->device.info.family >= NV_DEVICE_INFO_V0_TESLA) {
834 ret = nouveau_vm_new(nvkm_device(&drm->device), 0, (1ULL << 40),
837 nouveau_cli_destroy(cli);
841 nvkm_client(&cli->base)->vm = cli->vm;
844 fpriv->driver_priv = cli;
846 mutex_lock(&drm->client.mutex);
847 list_add(&cli->head, &drm->clients);
848 mutex_unlock(&drm->client.mutex);
851 pm_runtime_mark_last_busy(dev->dev);
852 pm_runtime_put_autosuspend(dev->dev);
858 nouveau_drm_preclose(struct drm_device *dev, struct drm_file *fpriv)
860 struct nouveau_cli *cli = nouveau_cli(fpriv);
861 struct nouveau_drm *drm = nouveau_drm(dev);
863 pm_runtime_get_sync(dev->dev);
866 nouveau_abi16_fini(cli->abi16);
868 mutex_lock(&drm->client.mutex);
869 list_del(&cli->head);
870 mutex_unlock(&drm->client.mutex);
875 nouveau_drm_postclose(struct drm_device *dev, struct drm_file *fpriv)
877 struct nouveau_cli *cli = nouveau_cli(fpriv);
878 nouveau_cli_destroy(cli);
879 pm_runtime_mark_last_busy(dev->dev);
880 pm_runtime_put_autosuspend(dev->dev);
883 static const struct drm_ioctl_desc
885 DRM_IOCTL_DEF_DRV(NOUVEAU_GETPARAM, nouveau_abi16_ioctl_getparam, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
886 DRM_IOCTL_DEF_DRV(NOUVEAU_SETPARAM, nouveau_abi16_ioctl_setparam, DRM_UNLOCKED|DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
887 DRM_IOCTL_DEF_DRV(NOUVEAU_CHANNEL_ALLOC, nouveau_abi16_ioctl_channel_alloc, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
888 DRM_IOCTL_DEF_DRV(NOUVEAU_CHANNEL_FREE, nouveau_abi16_ioctl_channel_free, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
889 DRM_IOCTL_DEF_DRV(NOUVEAU_GROBJ_ALLOC, nouveau_abi16_ioctl_grobj_alloc, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
890 DRM_IOCTL_DEF_DRV(NOUVEAU_NOTIFIEROBJ_ALLOC, nouveau_abi16_ioctl_notifierobj_alloc, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
891 DRM_IOCTL_DEF_DRV(NOUVEAU_GPUOBJ_FREE, nouveau_abi16_ioctl_gpuobj_free, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
892 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_NEW, nouveau_gem_ioctl_new, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
893 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_PUSHBUF, nouveau_gem_ioctl_pushbuf, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
894 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_CPU_PREP, nouveau_gem_ioctl_cpu_prep, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
895 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_CPU_FINI, nouveau_gem_ioctl_cpu_fini, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
896 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_INFO, nouveau_gem_ioctl_info, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
900 nouveau_drm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
902 struct drm_file *filp = file->private_data;
903 struct drm_device *dev = filp->minor->dev;
906 ret = pm_runtime_get_sync(dev->dev);
907 if (ret < 0 && ret != -EACCES)
910 switch (_IOC_NR(cmd) - DRM_COMMAND_BASE) {
911 case DRM_NOUVEAU_NVIF:
912 ret = usif_ioctl(filp, (void __user *)arg, _IOC_SIZE(cmd));
915 ret = drm_ioctl(file, cmd, arg);
919 pm_runtime_mark_last_busy(dev->dev);
920 pm_runtime_put_autosuspend(dev->dev);
924 static const struct file_operations
925 nouveau_driver_fops = {
926 .owner = THIS_MODULE,
928 .release = drm_release,
929 .unlocked_ioctl = nouveau_drm_ioctl,
930 .mmap = nouveau_ttm_mmap,
933 #if defined(CONFIG_COMPAT)
934 .compat_ioctl = nouveau_compat_ioctl,
936 .llseek = noop_llseek,
939 static struct drm_driver
943 DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME | DRIVER_RENDER,
945 .load = nouveau_drm_load,
946 .unload = nouveau_drm_unload,
947 .open = nouveau_drm_open,
948 .preclose = nouveau_drm_preclose,
949 .postclose = nouveau_drm_postclose,
950 .lastclose = nouveau_vga_lastclose,
952 #if defined(CONFIG_DEBUG_FS)
953 .debugfs_init = nouveau_debugfs_init,
954 .debugfs_cleanup = nouveau_debugfs_takedown,
957 .get_vblank_counter = drm_vblank_count,
958 .enable_vblank = nouveau_display_vblank_enable,
959 .disable_vblank = nouveau_display_vblank_disable,
960 .get_scanout_position = nouveau_display_scanoutpos,
961 .get_vblank_timestamp = nouveau_display_vblstamp,
963 .ioctls = nouveau_ioctls,
964 .num_ioctls = ARRAY_SIZE(nouveau_ioctls),
965 .fops = &nouveau_driver_fops,
967 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
968 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
969 .gem_prime_export = drm_gem_prime_export,
970 .gem_prime_import = drm_gem_prime_import,
971 .gem_prime_pin = nouveau_gem_prime_pin,
972 .gem_prime_res_obj = nouveau_gem_prime_res_obj,
973 .gem_prime_unpin = nouveau_gem_prime_unpin,
974 .gem_prime_get_sg_table = nouveau_gem_prime_get_sg_table,
975 .gem_prime_import_sg_table = nouveau_gem_prime_import_sg_table,
976 .gem_prime_vmap = nouveau_gem_prime_vmap,
977 .gem_prime_vunmap = nouveau_gem_prime_vunmap,
979 .gem_free_object = nouveau_gem_object_del,
980 .gem_open_object = nouveau_gem_object_open,
981 .gem_close_object = nouveau_gem_object_close,
983 .dumb_create = nouveau_display_dumb_create,
984 .dumb_map_offset = nouveau_display_dumb_map_offset,
985 .dumb_destroy = drm_gem_dumb_destroy,
990 .date = GIT_REVISION,
994 .major = DRIVER_MAJOR,
995 .minor = DRIVER_MINOR,
996 .patchlevel = DRIVER_PATCHLEVEL,
999 static struct pci_device_id
1000 nouveau_drm_pci_table[] = {
1002 PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_ANY_ID),
1003 .class = PCI_BASE_CLASS_DISPLAY << 16,
1004 .class_mask = 0xff << 16,
1007 PCI_DEVICE(PCI_VENDOR_ID_NVIDIA_SGS, PCI_ANY_ID),
1008 .class = PCI_BASE_CLASS_DISPLAY << 16,
1009 .class_mask = 0xff << 16,
1014 static void nouveau_display_options(void)
1016 DRM_DEBUG_DRIVER("Loading Nouveau with parameters:\n");
1018 DRM_DEBUG_DRIVER("... tv_disable : %d\n", nouveau_tv_disable);
1019 DRM_DEBUG_DRIVER("... ignorelid : %d\n", nouveau_ignorelid);
1020 DRM_DEBUG_DRIVER("... duallink : %d\n", nouveau_duallink);
1021 DRM_DEBUG_DRIVER("... nofbaccel : %d\n", nouveau_nofbaccel);
1022 DRM_DEBUG_DRIVER("... config : %s\n", nouveau_config);
1023 DRM_DEBUG_DRIVER("... debug : %s\n", nouveau_debug);
1024 DRM_DEBUG_DRIVER("... noaccel : %d\n", nouveau_noaccel);
1025 DRM_DEBUG_DRIVER("... modeset : %d\n", nouveau_modeset);
1026 DRM_DEBUG_DRIVER("... runpm : %d\n", nouveau_runtime_pm);
1027 DRM_DEBUG_DRIVER("... vram_pushbuf : %d\n", nouveau_vram_pushbuf);
1028 DRM_DEBUG_DRIVER("... pstate : %d\n", nouveau_pstate);
1031 static const struct dev_pm_ops nouveau_pm_ops = {
1032 .suspend = nouveau_pmops_suspend,
1033 .resume = nouveau_pmops_resume,
1034 .freeze = nouveau_pmops_freeze,
1035 .thaw = nouveau_pmops_thaw,
1036 .poweroff = nouveau_pmops_freeze,
1037 .restore = nouveau_pmops_resume,
1038 .runtime_suspend = nouveau_pmops_runtime_suspend,
1039 .runtime_resume = nouveau_pmops_runtime_resume,
1040 .runtime_idle = nouveau_pmops_runtime_idle,
1043 static struct pci_driver
1044 nouveau_drm_pci_driver = {
1046 .id_table = nouveau_drm_pci_table,
1047 .probe = nouveau_drm_probe,
1048 .remove = nouveau_drm_remove,
1049 .driver.pm = &nouveau_pm_ops,
1053 nouveau_platform_device_create_(struct platform_device *pdev, int size,
1056 struct drm_device *drm;
1059 err = nouveau_device_create_(pdev, NOUVEAU_BUS_PLATFORM,
1060 nouveau_platform_name(pdev),
1061 dev_name(&pdev->dev), nouveau_config,
1062 nouveau_debug, size, pobject);
1064 return ERR_PTR(err);
1066 drm = drm_dev_alloc(&driver_platform, &pdev->dev);
1072 err = drm_dev_set_unique(drm, "%s", dev_name(&pdev->dev));
1076 drm->platformdev = pdev;
1077 platform_set_drvdata(pdev, drm);
1082 nouveau_object_ref(NULL, (struct nouveau_object **)pobject);
1084 return ERR_PTR(err);
1086 EXPORT_SYMBOL(nouveau_platform_device_create_);
1089 nouveau_drm_init(void)
1091 driver_pci = driver_stub;
1092 driver_pci.set_busid = drm_pci_set_busid;
1093 driver_platform = driver_stub;
1094 driver_platform.set_busid = drm_platform_set_busid;
1096 nouveau_display_options();
1098 if (nouveau_modeset == -1) {
1099 #ifdef CONFIG_VGA_CONSOLE
1100 if (vgacon_text_force())
1101 nouveau_modeset = 0;
1105 if (!nouveau_modeset)
1108 nouveau_register_dsm_handler();
1109 return drm_pci_init(&driver_pci, &nouveau_drm_pci_driver);
1113 nouveau_drm_exit(void)
1115 if (!nouveau_modeset)
1118 drm_pci_exit(&driver_pci, &nouveau_drm_pci_driver);
1119 nouveau_unregister_dsm_handler();
1122 module_init(nouveau_drm_init);
1123 module_exit(nouveau_drm_exit);
1125 MODULE_DEVICE_TABLE(pci, nouveau_drm_pci_table);
1126 MODULE_AUTHOR(DRIVER_AUTHOR);
1127 MODULE_DESCRIPTION(DRIVER_DESC);
1128 MODULE_LICENSE("GPL and additional rights");