1 /* 2 * Copyright 2012 Red Hat Inc. 3 * 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: 10 * 11 * The above copyright notice and this permission notice shall be included in 12 * all copies or substantial portions of the Software. 13 * 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. 21 * 22 * Authors: Ben Skeggs 23 */ 24 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> 31 32 #include "drmP.h" 33 #include "drm_crtc_helper.h" 34 35 #include <core/gpuobj.h> 36 #include <core/option.h> 37 #include <core/pci.h> 38 #include <core/tegra.h> 39 40 #include "nouveau_drm.h" 41 #include "nouveau_dma.h" 42 #include "nouveau_ttm.h" 43 #include "nouveau_gem.h" 44 #include "nouveau_agp.h" 45 #include "nouveau_vga.h" 46 #include "nouveau_sysfs.h" 47 #include "nouveau_hwmon.h" 48 #include "nouveau_acpi.h" 49 #include "nouveau_bios.h" 50 #include "nouveau_ioctl.h" 51 #include "nouveau_abi16.h" 52 #include "nouveau_fbcon.h" 53 #include "nouveau_fence.h" 54 #include "nouveau_debugfs.h" 55 #include "nouveau_usif.h" 56 #include "nouveau_connector.h" 57 #include "nouveau_platform.h" 58 59 MODULE_PARM_DESC(config, "option string to pass to driver core"); 60 static char *nouveau_config; 61 module_param_named(config, nouveau_config, charp, 0400); 62 63 MODULE_PARM_DESC(debug, "debug string to pass to driver core"); 64 static char *nouveau_debug; 65 module_param_named(debug, nouveau_debug, charp, 0400); 66 67 MODULE_PARM_DESC(noaccel, "disable kernel/abi16 acceleration"); 68 static int nouveau_noaccel = 0; 69 module_param_named(noaccel, nouveau_noaccel, int, 0400); 70 71 MODULE_PARM_DESC(modeset, "enable driver (default: auto, " 72 "0 = disabled, 1 = enabled, 2 = headless)"); 73 int nouveau_modeset = -1; 74 module_param_named(modeset, nouveau_modeset, int, 0400); 75 76 MODULE_PARM_DESC(runpm, "disable (0), force enable (1), optimus only default (-1)"); 77 int nouveau_runtime_pm = -1; 78 module_param_named(runpm, nouveau_runtime_pm, int, 0400); 79 80 static struct drm_driver driver_stub; 81 static struct drm_driver driver_pci; 82 static struct drm_driver driver_platform; 83 84 static u64 85 nouveau_pci_name(struct pci_dev *pdev) 86 { 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); 91 } 92 93 static u64 94 nouveau_platform_name(struct platform_device *platformdev) 95 { 96 return platformdev->id; 97 } 98 99 static u64 100 nouveau_name(struct drm_device *dev) 101 { 102 if (dev->pdev) 103 return nouveau_pci_name(dev->pdev); 104 else 105 return nouveau_platform_name(dev->platformdev); 106 } 107 108 static int 109 nouveau_cli_create(struct drm_device *dev, const char *sname, 110 int size, void **pcli) 111 { 112 struct nouveau_cli *cli = *pcli = kzalloc(size, GFP_KERNEL); 113 int ret; 114 if (cli) { 115 snprintf(cli->name, sizeof(cli->name), "%s", sname); 116 cli->dev = dev; 117 118 ret = nvif_client_init(NULL, cli->name, nouveau_name(dev), 119 nouveau_config, nouveau_debug, 120 &cli->base); 121 if (ret == 0) { 122 mutex_init(&cli->mutex); 123 usif_client_init(cli); 124 } 125 return ret; 126 } 127 return -ENOMEM; 128 } 129 130 static void 131 nouveau_cli_destroy(struct nouveau_cli *cli) 132 { 133 nvkm_vm_ref(NULL, &nvxx_client(&cli->base)->vm, NULL); 134 nvif_client_fini(&cli->base); 135 usif_client_fini(cli); 136 kfree(cli); 137 } 138 139 static void 140 nouveau_accel_fini(struct nouveau_drm *drm) 141 { 142 nouveau_channel_idle(drm->channel); 143 nvif_object_fini(&drm->ntfy); 144 nvkm_gpuobj_del(&drm->notify); 145 nvif_notify_fini(&drm->flip); 146 nvif_object_fini(&drm->nvsw); 147 nouveau_channel_del(&drm->channel); 148 149 nouveau_channel_idle(drm->cechan); 150 nvif_object_fini(&drm->ttm.copy); 151 nouveau_channel_del(&drm->cechan); 152 153 if (drm->fence) 154 nouveau_fence(drm)->dtor(drm); 155 } 156 157 static void 158 nouveau_accel_init(struct nouveau_drm *drm) 159 { 160 struct nvif_device *device = &drm->device; 161 struct nvif_sclass *sclass; 162 u32 arg0, arg1; 163 int ret, i, n; 164 165 if (nouveau_noaccel) 166 return; 167 168 /* initialise synchronisation routines */ 169 /*XXX: this is crap, but the fence/channel stuff is a little 170 * backwards in some places. this will be fixed. 171 */ 172 ret = n = nvif_object_sclass_get(&device->object, &sclass); 173 if (ret < 0) 174 return; 175 176 for (ret = -ENOSYS, i = 0; i < n; i++) { 177 switch (sclass[i].oclass) { 178 case NV03_CHANNEL_DMA: 179 ret = nv04_fence_create(drm); 180 break; 181 case NV10_CHANNEL_DMA: 182 ret = nv10_fence_create(drm); 183 break; 184 case NV17_CHANNEL_DMA: 185 case NV40_CHANNEL_DMA: 186 ret = nv17_fence_create(drm); 187 break; 188 case NV50_CHANNEL_GPFIFO: 189 ret = nv50_fence_create(drm); 190 break; 191 case G82_CHANNEL_GPFIFO: 192 ret = nv84_fence_create(drm); 193 break; 194 case FERMI_CHANNEL_GPFIFO: 195 case KEPLER_CHANNEL_GPFIFO_A: 196 case MAXWELL_CHANNEL_GPFIFO_A: 197 ret = nvc0_fence_create(drm); 198 break; 199 default: 200 break; 201 } 202 } 203 204 nvif_object_sclass_put(&sclass); 205 if (ret) { 206 NV_ERROR(drm, "failed to initialise sync subsystem, %d\n", ret); 207 nouveau_accel_fini(drm); 208 return; 209 } 210 211 if (device->info.family >= NV_DEVICE_INFO_V0_KEPLER) { 212 ret = nouveau_channel_new(drm, &drm->device, NVDRM_CHAN + 1, 213 KEPLER_CHANNEL_GPFIFO_A_V0_ENGINE_CE0| 214 KEPLER_CHANNEL_GPFIFO_A_V0_ENGINE_CE1, 215 0, &drm->cechan); 216 if (ret) 217 NV_ERROR(drm, "failed to create ce channel, %d\n", ret); 218 219 arg0 = KEPLER_CHANNEL_GPFIFO_A_V0_ENGINE_GR; 220 arg1 = 1; 221 } else 222 if (device->info.chipset >= 0xa3 && 223 device->info.chipset != 0xaa && 224 device->info.chipset != 0xac) { 225 ret = nouveau_channel_new(drm, &drm->device, NVDRM_CHAN + 1, 226 NvDmaFB, NvDmaTT, &drm->cechan); 227 if (ret) 228 NV_ERROR(drm, "failed to create ce channel, %d\n", ret); 229 230 arg0 = NvDmaFB; 231 arg1 = NvDmaTT; 232 } else { 233 arg0 = NvDmaFB; 234 arg1 = NvDmaTT; 235 } 236 237 ret = nouveau_channel_new(drm, &drm->device, NVDRM_CHAN, arg0, arg1, 238 &drm->channel); 239 if (ret) { 240 NV_ERROR(drm, "failed to create kernel channel, %d\n", ret); 241 nouveau_accel_fini(drm); 242 return; 243 } 244 245 ret = nvif_object_init(&drm->channel->user, NVDRM_NVSW, 246 nouveau_abi16_swclass(drm), NULL, 0, &drm->nvsw); 247 if (ret == 0) { 248 ret = RING_SPACE(drm->channel, 2); 249 if (ret == 0) { 250 if (device->info.family < NV_DEVICE_INFO_V0_FERMI) { 251 BEGIN_NV04(drm->channel, NvSubSw, 0, 1); 252 OUT_RING (drm->channel, NVDRM_NVSW); 253 } else 254 if (device->info.family < NV_DEVICE_INFO_V0_KEPLER) { 255 BEGIN_NVC0(drm->channel, FermiSw, 0, 1); 256 OUT_RING (drm->channel, 0x001f0000); 257 } 258 } 259 260 ret = nvif_notify_init(&drm->nvsw, nouveau_flip_complete, 261 false, NVSW_NTFY_UEVENT, NULL, 0, 0, 262 &drm->flip); 263 if (ret == 0) 264 ret = nvif_notify_get(&drm->flip); 265 if (ret) { 266 nouveau_accel_fini(drm); 267 return; 268 } 269 } 270 271 if (ret) { 272 NV_ERROR(drm, "failed to allocate software object, %d\n", ret); 273 nouveau_accel_fini(drm); 274 return; 275 } 276 277 if (device->info.family < NV_DEVICE_INFO_V0_FERMI) { 278 ret = nvkm_gpuobj_new(nvxx_device(&drm->device), 32, 0, false, 279 NULL, &drm->notify); 280 if (ret) { 281 NV_ERROR(drm, "failed to allocate notifier, %d\n", ret); 282 nouveau_accel_fini(drm); 283 return; 284 } 285 286 ret = nvif_object_init(&drm->channel->user, NvNotify0, 287 NV_DMA_IN_MEMORY, 288 &(struct nv_dma_v0) { 289 .target = NV_DMA_V0_TARGET_VRAM, 290 .access = NV_DMA_V0_ACCESS_RDWR, 291 .start = drm->notify->addr, 292 .limit = drm->notify->addr + 31 293 }, sizeof(struct nv_dma_v0), 294 &drm->ntfy); 295 if (ret) { 296 nouveau_accel_fini(drm); 297 return; 298 } 299 } 300 301 302 nouveau_bo_move_init(drm); 303 } 304 305 static int nouveau_drm_probe(struct pci_dev *pdev, 306 const struct pci_device_id *pent) 307 { 308 struct nvkm_device *device; 309 struct apertures_struct *aper; 310 bool boot = false; 311 int ret; 312 313 /* remove conflicting drivers (vesafb, efifb etc) */ 314 aper = alloc_apertures(3); 315 if (!aper) 316 return -ENOMEM; 317 318 aper->ranges[0].base = pci_resource_start(pdev, 1); 319 aper->ranges[0].size = pci_resource_len(pdev, 1); 320 aper->count = 1; 321 322 if (pci_resource_len(pdev, 2)) { 323 aper->ranges[aper->count].base = pci_resource_start(pdev, 2); 324 aper->ranges[aper->count].size = pci_resource_len(pdev, 2); 325 aper->count++; 326 } 327 328 if (pci_resource_len(pdev, 3)) { 329 aper->ranges[aper->count].base = pci_resource_start(pdev, 3); 330 aper->ranges[aper->count].size = pci_resource_len(pdev, 3); 331 aper->count++; 332 } 333 334 #ifdef CONFIG_X86 335 boot = pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW; 336 #endif 337 if (nouveau_modeset != 2) 338 remove_conflicting_framebuffers(aper, "nouveaufb", boot); 339 kfree(aper); 340 341 ret = nvkm_device_pci_new(pdev, nouveau_config, nouveau_debug, 342 true, true, ~0ULL, &device); 343 if (ret) 344 return ret; 345 346 pci_set_master(pdev); 347 348 ret = drm_get_pci_dev(pdev, pent, &driver_pci); 349 if (ret) { 350 nvkm_device_del(&device); 351 return ret; 352 } 353 354 return 0; 355 } 356 357 #define PCI_CLASS_MULTIMEDIA_HD_AUDIO 0x0403 358 359 static void 360 nouveau_get_hdmi_dev(struct nouveau_drm *drm) 361 { 362 struct pci_dev *pdev = drm->dev->pdev; 363 364 if (!pdev) { 365 DRM_INFO("not a PCI device; no HDMI\n"); 366 drm->hdmi_device = NULL; 367 return; 368 } 369 370 /* subfunction one is a hdmi audio device? */ 371 drm->hdmi_device = pci_get_bus_and_slot((unsigned int)pdev->bus->number, 372 PCI_DEVFN(PCI_SLOT(pdev->devfn), 1)); 373 374 if (!drm->hdmi_device) { 375 NV_DEBUG(drm, "hdmi device not found %d %d %d\n", pdev->bus->number, PCI_SLOT(pdev->devfn), 1); 376 return; 377 } 378 379 if ((drm->hdmi_device->class >> 8) != PCI_CLASS_MULTIMEDIA_HD_AUDIO) { 380 NV_DEBUG(drm, "possible hdmi device not audio %d\n", drm->hdmi_device->class); 381 pci_dev_put(drm->hdmi_device); 382 drm->hdmi_device = NULL; 383 return; 384 } 385 } 386 387 static int 388 nouveau_drm_load(struct drm_device *dev, unsigned long flags) 389 { 390 struct nouveau_drm *drm; 391 int ret; 392 393 ret = nouveau_cli_create(dev, "DRM", sizeof(*drm), (void **)&drm); 394 if (ret) 395 return ret; 396 397 dev->dev_private = drm; 398 drm->dev = dev; 399 nvxx_client(&drm->client.base)->debug = 400 nvkm_dbgopt(nouveau_debug, "DRM"); 401 402 INIT_LIST_HEAD(&drm->clients); 403 spin_lock_init(&drm->tile.lock); 404 405 nouveau_get_hdmi_dev(drm); 406 407 ret = nvif_device_init(&drm->client.base.object, 408 NVDRM_DEVICE, NV_DEVICE, 409 &(struct nv_device_v0) { 410 .device = ~0, 411 }, sizeof(struct nv_device_v0), 412 &drm->device); 413 if (ret) 414 goto fail_device; 415 416 dev->irq_enabled = true; 417 418 /* workaround an odd issue on nvc1 by disabling the device's 419 * nosnoop capability. hopefully won't cause issues until a 420 * better fix is found - assuming there is one... 421 */ 422 if (drm->device.info.chipset == 0xc1) 423 nvif_mask(&drm->device.object, 0x00088080, 0x00000800, 0x00000000); 424 425 nouveau_vga_init(drm); 426 nouveau_agp_init(drm); 427 428 if (drm->device.info.family >= NV_DEVICE_INFO_V0_TESLA) { 429 ret = nvkm_vm_new(nvxx_device(&drm->device), 0, (1ULL << 40), 430 0x1000, NULL, &drm->client.vm); 431 if (ret) 432 goto fail_device; 433 434 nvxx_client(&drm->client.base)->vm = drm->client.vm; 435 } 436 437 ret = nouveau_ttm_init(drm); 438 if (ret) 439 goto fail_ttm; 440 441 ret = nouveau_bios_init(dev); 442 if (ret) 443 goto fail_bios; 444 445 ret = nouveau_display_create(dev); 446 if (ret) 447 goto fail_dispctor; 448 449 if (dev->mode_config.num_crtc) { 450 ret = nouveau_display_init(dev); 451 if (ret) 452 goto fail_dispinit; 453 } 454 455 nouveau_sysfs_init(dev); 456 nouveau_hwmon_init(dev); 457 nouveau_accel_init(drm); 458 nouveau_fbcon_init(dev); 459 460 if (nouveau_runtime_pm != 0) { 461 pm_runtime_use_autosuspend(dev->dev); 462 pm_runtime_set_autosuspend_delay(dev->dev, 5000); 463 pm_runtime_set_active(dev->dev); 464 pm_runtime_allow(dev->dev); 465 pm_runtime_mark_last_busy(dev->dev); 466 pm_runtime_put(dev->dev); 467 } 468 return 0; 469 470 fail_dispinit: 471 nouveau_display_destroy(dev); 472 fail_dispctor: 473 nouveau_bios_takedown(dev); 474 fail_bios: 475 nouveau_ttm_fini(drm); 476 fail_ttm: 477 nouveau_agp_fini(drm); 478 nouveau_vga_fini(drm); 479 fail_device: 480 nvif_device_fini(&drm->device); 481 nouveau_cli_destroy(&drm->client); 482 return ret; 483 } 484 485 static int 486 nouveau_drm_unload(struct drm_device *dev) 487 { 488 struct nouveau_drm *drm = nouveau_drm(dev); 489 490 pm_runtime_get_sync(dev->dev); 491 nouveau_fbcon_fini(dev); 492 nouveau_accel_fini(drm); 493 nouveau_hwmon_fini(dev); 494 nouveau_sysfs_fini(dev); 495 496 if (dev->mode_config.num_crtc) 497 nouveau_display_fini(dev); 498 nouveau_display_destroy(dev); 499 500 nouveau_bios_takedown(dev); 501 502 nouveau_ttm_fini(drm); 503 nouveau_agp_fini(drm); 504 nouveau_vga_fini(drm); 505 506 nvif_device_fini(&drm->device); 507 if (drm->hdmi_device) 508 pci_dev_put(drm->hdmi_device); 509 nouveau_cli_destroy(&drm->client); 510 return 0; 511 } 512 513 void 514 nouveau_drm_device_remove(struct drm_device *dev) 515 { 516 struct nouveau_drm *drm = nouveau_drm(dev); 517 struct nvkm_client *client; 518 struct nvkm_device *device; 519 520 dev->irq_enabled = false; 521 client = nvxx_client(&drm->client.base); 522 device = nvkm_device_find(client->device); 523 drm_put_dev(dev); 524 525 nvkm_device_del(&device); 526 } 527 528 static void 529 nouveau_drm_remove(struct pci_dev *pdev) 530 { 531 struct drm_device *dev = pci_get_drvdata(pdev); 532 533 nouveau_drm_device_remove(dev); 534 } 535 536 static int 537 nouveau_do_suspend(struct drm_device *dev, bool runtime) 538 { 539 struct nouveau_drm *drm = nouveau_drm(dev); 540 struct nouveau_cli *cli; 541 int ret; 542 543 if (dev->mode_config.num_crtc) { 544 NV_INFO(drm, "suspending console...\n"); 545 nouveau_fbcon_set_suspend(dev, 1); 546 NV_INFO(drm, "suspending display...\n"); 547 ret = nouveau_display_suspend(dev, runtime); 548 if (ret) 549 return ret; 550 } 551 552 NV_INFO(drm, "evicting buffers...\n"); 553 ttm_bo_evict_mm(&drm->ttm.bdev, TTM_PL_VRAM); 554 555 NV_INFO(drm, "waiting for kernel channels to go idle...\n"); 556 if (drm->cechan) { 557 ret = nouveau_channel_idle(drm->cechan); 558 if (ret) 559 goto fail_display; 560 } 561 562 if (drm->channel) { 563 ret = nouveau_channel_idle(drm->channel); 564 if (ret) 565 goto fail_display; 566 } 567 568 NV_INFO(drm, "suspending client object trees...\n"); 569 if (drm->fence && nouveau_fence(drm)->suspend) { 570 if (!nouveau_fence(drm)->suspend(drm)) { 571 ret = -ENOMEM; 572 goto fail_display; 573 } 574 } 575 576 list_for_each_entry(cli, &drm->clients, head) { 577 ret = nvif_client_suspend(&cli->base); 578 if (ret) 579 goto fail_client; 580 } 581 582 NV_INFO(drm, "suspending kernel object tree...\n"); 583 ret = nvif_client_suspend(&drm->client.base); 584 if (ret) 585 goto fail_client; 586 587 nouveau_agp_fini(drm); 588 return 0; 589 590 fail_client: 591 list_for_each_entry_continue_reverse(cli, &drm->clients, head) { 592 nvif_client_resume(&cli->base); 593 } 594 595 if (drm->fence && nouveau_fence(drm)->resume) 596 nouveau_fence(drm)->resume(drm); 597 598 fail_display: 599 if (dev->mode_config.num_crtc) { 600 NV_INFO(drm, "resuming display...\n"); 601 nouveau_display_resume(dev, runtime); 602 } 603 return ret; 604 } 605 606 static int 607 nouveau_do_resume(struct drm_device *dev, bool runtime) 608 { 609 struct nouveau_drm *drm = nouveau_drm(dev); 610 struct nouveau_cli *cli; 611 612 NV_INFO(drm, "re-enabling device...\n"); 613 614 nouveau_agp_reset(drm); 615 616 NV_INFO(drm, "resuming kernel object tree...\n"); 617 nvif_client_resume(&drm->client.base); 618 nouveau_agp_init(drm); 619 620 NV_INFO(drm, "resuming client object trees...\n"); 621 if (drm->fence && nouveau_fence(drm)->resume) 622 nouveau_fence(drm)->resume(drm); 623 624 list_for_each_entry(cli, &drm->clients, head) { 625 nvif_client_resume(&cli->base); 626 } 627 628 nouveau_run_vbios_init(dev); 629 630 if (dev->mode_config.num_crtc) { 631 NV_INFO(drm, "resuming display...\n"); 632 nouveau_display_resume(dev, runtime); 633 NV_INFO(drm, "resuming console...\n"); 634 nouveau_fbcon_set_suspend(dev, 0); 635 } 636 637 return 0; 638 } 639 640 int 641 nouveau_pmops_suspend(struct device *dev) 642 { 643 struct pci_dev *pdev = to_pci_dev(dev); 644 struct drm_device *drm_dev = pci_get_drvdata(pdev); 645 int ret; 646 647 if (drm_dev->switch_power_state == DRM_SWITCH_POWER_OFF || 648 drm_dev->switch_power_state == DRM_SWITCH_POWER_DYNAMIC_OFF) 649 return 0; 650 651 ret = nouveau_do_suspend(drm_dev, false); 652 if (ret) 653 return ret; 654 655 pci_save_state(pdev); 656 pci_disable_device(pdev); 657 pci_set_power_state(pdev, PCI_D3hot); 658 udelay(200); 659 return 0; 660 } 661 662 int 663 nouveau_pmops_resume(struct device *dev) 664 { 665 struct pci_dev *pdev = to_pci_dev(dev); 666 struct drm_device *drm_dev = pci_get_drvdata(pdev); 667 int ret; 668 669 if (drm_dev->switch_power_state == DRM_SWITCH_POWER_OFF || 670 drm_dev->switch_power_state == DRM_SWITCH_POWER_DYNAMIC_OFF) 671 return 0; 672 673 pci_set_power_state(pdev, PCI_D0); 674 pci_restore_state(pdev); 675 ret = pci_enable_device(pdev); 676 if (ret) 677 return ret; 678 pci_set_master(pdev); 679 680 return nouveau_do_resume(drm_dev, false); 681 } 682 683 static int 684 nouveau_pmops_freeze(struct device *dev) 685 { 686 struct pci_dev *pdev = to_pci_dev(dev); 687 struct drm_device *drm_dev = pci_get_drvdata(pdev); 688 return nouveau_do_suspend(drm_dev, false); 689 } 690 691 static int 692 nouveau_pmops_thaw(struct device *dev) 693 { 694 struct pci_dev *pdev = to_pci_dev(dev); 695 struct drm_device *drm_dev = pci_get_drvdata(pdev); 696 return nouveau_do_resume(drm_dev, false); 697 } 698 699 static int 700 nouveau_pmops_runtime_suspend(struct device *dev) 701 { 702 struct pci_dev *pdev = to_pci_dev(dev); 703 struct drm_device *drm_dev = pci_get_drvdata(pdev); 704 int ret; 705 706 if (nouveau_runtime_pm == 0) { 707 pm_runtime_forbid(dev); 708 return -EBUSY; 709 } 710 711 /* are we optimus enabled? */ 712 if (nouveau_runtime_pm == -1 && !nouveau_is_optimus() && !nouveau_is_v1_dsm()) { 713 DRM_DEBUG_DRIVER("failing to power off - not optimus\n"); 714 pm_runtime_forbid(dev); 715 return -EBUSY; 716 } 717 718 drm_kms_helper_poll_disable(drm_dev); 719 vga_switcheroo_set_dynamic_switch(pdev, VGA_SWITCHEROO_OFF); 720 nouveau_switcheroo_optimus_dsm(); 721 ret = nouveau_do_suspend(drm_dev, true); 722 pci_save_state(pdev); 723 pci_disable_device(pdev); 724 pci_ignore_hotplug(pdev); 725 pci_set_power_state(pdev, PCI_D3cold); 726 drm_dev->switch_power_state = DRM_SWITCH_POWER_DYNAMIC_OFF; 727 return ret; 728 } 729 730 static int 731 nouveau_pmops_runtime_resume(struct device *dev) 732 { 733 struct pci_dev *pdev = to_pci_dev(dev); 734 struct drm_device *drm_dev = pci_get_drvdata(pdev); 735 struct nvif_device *device = &nouveau_drm(drm_dev)->device; 736 int ret; 737 738 if (nouveau_runtime_pm == 0) 739 return -EINVAL; 740 741 pci_set_power_state(pdev, PCI_D0); 742 pci_restore_state(pdev); 743 ret = pci_enable_device(pdev); 744 if (ret) 745 return ret; 746 pci_set_master(pdev); 747 748 ret = nouveau_do_resume(drm_dev, true); 749 drm_kms_helper_poll_enable(drm_dev); 750 /* do magic */ 751 nvif_mask(&device->object, 0x088488, (1 << 25), (1 << 25)); 752 vga_switcheroo_set_dynamic_switch(pdev, VGA_SWITCHEROO_ON); 753 drm_dev->switch_power_state = DRM_SWITCH_POWER_ON; 754 return ret; 755 } 756 757 static int 758 nouveau_pmops_runtime_idle(struct device *dev) 759 { 760 struct pci_dev *pdev = to_pci_dev(dev); 761 struct drm_device *drm_dev = pci_get_drvdata(pdev); 762 struct nouveau_drm *drm = nouveau_drm(drm_dev); 763 struct drm_crtc *crtc; 764 765 if (nouveau_runtime_pm == 0) { 766 pm_runtime_forbid(dev); 767 return -EBUSY; 768 } 769 770 /* are we optimus enabled? */ 771 if (nouveau_runtime_pm == -1 && !nouveau_is_optimus() && !nouveau_is_v1_dsm()) { 772 DRM_DEBUG_DRIVER("failing to power off - not optimus\n"); 773 pm_runtime_forbid(dev); 774 return -EBUSY; 775 } 776 777 /* if we have a hdmi audio device - make sure it has a driver loaded */ 778 if (drm->hdmi_device) { 779 if (!drm->hdmi_device->driver) { 780 DRM_DEBUG_DRIVER("failing to power off - no HDMI audio driver loaded\n"); 781 pm_runtime_mark_last_busy(dev); 782 return -EBUSY; 783 } 784 } 785 786 list_for_each_entry(crtc, &drm->dev->mode_config.crtc_list, head) { 787 if (crtc->enabled) { 788 DRM_DEBUG_DRIVER("failing to power off - crtc active\n"); 789 return -EBUSY; 790 } 791 } 792 pm_runtime_mark_last_busy(dev); 793 pm_runtime_autosuspend(dev); 794 /* we don't want the main rpm_idle to call suspend - we want to autosuspend */ 795 return 1; 796 } 797 798 static int 799 nouveau_drm_open(struct drm_device *dev, struct drm_file *fpriv) 800 { 801 struct nouveau_drm *drm = nouveau_drm(dev); 802 struct nouveau_cli *cli; 803 char name[32], tmpname[TASK_COMM_LEN]; 804 int ret; 805 806 /* need to bring up power immediately if opening device */ 807 ret = pm_runtime_get_sync(dev->dev); 808 if (ret < 0 && ret != -EACCES) 809 return ret; 810 811 get_task_comm(tmpname, current); 812 snprintf(name, sizeof(name), "%s[%d]", tmpname, pid_nr(fpriv->pid)); 813 814 ret = nouveau_cli_create(dev, name, sizeof(*cli), (void **)&cli); 815 816 if (ret) 817 goto out_suspend; 818 819 cli->base.super = false; 820 821 if (drm->device.info.family >= NV_DEVICE_INFO_V0_TESLA) { 822 ret = nvkm_vm_new(nvxx_device(&drm->device), 0, (1ULL << 40), 823 0x1000, NULL, &cli->vm); 824 if (ret) { 825 nouveau_cli_destroy(cli); 826 goto out_suspend; 827 } 828 829 nvxx_client(&cli->base)->vm = cli->vm; 830 } 831 832 fpriv->driver_priv = cli; 833 834 mutex_lock(&drm->client.mutex); 835 list_add(&cli->head, &drm->clients); 836 mutex_unlock(&drm->client.mutex); 837 838 out_suspend: 839 pm_runtime_mark_last_busy(dev->dev); 840 pm_runtime_put_autosuspend(dev->dev); 841 842 return ret; 843 } 844 845 static void 846 nouveau_drm_preclose(struct drm_device *dev, struct drm_file *fpriv) 847 { 848 struct nouveau_cli *cli = nouveau_cli(fpriv); 849 struct nouveau_drm *drm = nouveau_drm(dev); 850 851 pm_runtime_get_sync(dev->dev); 852 853 mutex_lock(&cli->mutex); 854 if (cli->abi16) 855 nouveau_abi16_fini(cli->abi16); 856 mutex_unlock(&cli->mutex); 857 858 mutex_lock(&drm->client.mutex); 859 list_del(&cli->head); 860 mutex_unlock(&drm->client.mutex); 861 862 } 863 864 static void 865 nouveau_drm_postclose(struct drm_device *dev, struct drm_file *fpriv) 866 { 867 struct nouveau_cli *cli = nouveau_cli(fpriv); 868 nouveau_cli_destroy(cli); 869 pm_runtime_mark_last_busy(dev->dev); 870 pm_runtime_put_autosuspend(dev->dev); 871 } 872 873 static const struct drm_ioctl_desc 874 nouveau_ioctls[] = { 875 DRM_IOCTL_DEF_DRV(NOUVEAU_GETPARAM, nouveau_abi16_ioctl_getparam, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW), 876 DRM_IOCTL_DEF_DRV(NOUVEAU_SETPARAM, nouveau_abi16_ioctl_setparam, DRM_UNLOCKED|DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY), 877 DRM_IOCTL_DEF_DRV(NOUVEAU_CHANNEL_ALLOC, nouveau_abi16_ioctl_channel_alloc, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW), 878 DRM_IOCTL_DEF_DRV(NOUVEAU_CHANNEL_FREE, nouveau_abi16_ioctl_channel_free, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW), 879 DRM_IOCTL_DEF_DRV(NOUVEAU_GROBJ_ALLOC, nouveau_abi16_ioctl_grobj_alloc, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW), 880 DRM_IOCTL_DEF_DRV(NOUVEAU_NOTIFIEROBJ_ALLOC, nouveau_abi16_ioctl_notifierobj_alloc, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW), 881 DRM_IOCTL_DEF_DRV(NOUVEAU_GPUOBJ_FREE, nouveau_abi16_ioctl_gpuobj_free, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW), 882 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_NEW, nouveau_gem_ioctl_new, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW), 883 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_PUSHBUF, nouveau_gem_ioctl_pushbuf, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW), 884 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_CPU_PREP, nouveau_gem_ioctl_cpu_prep, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW), 885 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_CPU_FINI, nouveau_gem_ioctl_cpu_fini, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW), 886 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_INFO, nouveau_gem_ioctl_info, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW), 887 }; 888 889 long 890 nouveau_drm_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 891 { 892 struct drm_file *filp = file->private_data; 893 struct drm_device *dev = filp->minor->dev; 894 long ret; 895 896 ret = pm_runtime_get_sync(dev->dev); 897 if (ret < 0 && ret != -EACCES) 898 return ret; 899 900 switch (_IOC_NR(cmd) - DRM_COMMAND_BASE) { 901 case DRM_NOUVEAU_NVIF: 902 ret = usif_ioctl(filp, (void __user *)arg, _IOC_SIZE(cmd)); 903 break; 904 default: 905 ret = drm_ioctl(file, cmd, arg); 906 break; 907 } 908 909 pm_runtime_mark_last_busy(dev->dev); 910 pm_runtime_put_autosuspend(dev->dev); 911 return ret; 912 } 913 914 static const struct file_operations 915 nouveau_driver_fops = { 916 .owner = THIS_MODULE, 917 .open = drm_open, 918 .release = drm_release, 919 .unlocked_ioctl = nouveau_drm_ioctl, 920 .mmap = nouveau_ttm_mmap, 921 .poll = drm_poll, 922 .read = drm_read, 923 #if defined(CONFIG_COMPAT) 924 .compat_ioctl = nouveau_compat_ioctl, 925 #endif 926 .llseek = noop_llseek, 927 }; 928 929 static struct drm_driver 930 driver_stub = { 931 .driver_features = 932 DRIVER_USE_AGP | 933 DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME | DRIVER_RENDER | 934 DRIVER_KMS_LEGACY_CONTEXT, 935 936 .load = nouveau_drm_load, 937 .unload = nouveau_drm_unload, 938 .open = nouveau_drm_open, 939 .preclose = nouveau_drm_preclose, 940 .postclose = nouveau_drm_postclose, 941 .lastclose = nouveau_vga_lastclose, 942 943 #if defined(CONFIG_DEBUG_FS) 944 .debugfs_init = nouveau_debugfs_init, 945 .debugfs_cleanup = nouveau_debugfs_takedown, 946 #endif 947 948 .get_vblank_counter = drm_vblank_count, 949 .enable_vblank = nouveau_display_vblank_enable, 950 .disable_vblank = nouveau_display_vblank_disable, 951 .get_scanout_position = nouveau_display_scanoutpos, 952 .get_vblank_timestamp = nouveau_display_vblstamp, 953 954 .ioctls = nouveau_ioctls, 955 .num_ioctls = ARRAY_SIZE(nouveau_ioctls), 956 .fops = &nouveau_driver_fops, 957 958 .prime_handle_to_fd = drm_gem_prime_handle_to_fd, 959 .prime_fd_to_handle = drm_gem_prime_fd_to_handle, 960 .gem_prime_export = drm_gem_prime_export, 961 .gem_prime_import = drm_gem_prime_import, 962 .gem_prime_pin = nouveau_gem_prime_pin, 963 .gem_prime_res_obj = nouveau_gem_prime_res_obj, 964 .gem_prime_unpin = nouveau_gem_prime_unpin, 965 .gem_prime_get_sg_table = nouveau_gem_prime_get_sg_table, 966 .gem_prime_import_sg_table = nouveau_gem_prime_import_sg_table, 967 .gem_prime_vmap = nouveau_gem_prime_vmap, 968 .gem_prime_vunmap = nouveau_gem_prime_vunmap, 969 970 .gem_free_object = nouveau_gem_object_del, 971 .gem_open_object = nouveau_gem_object_open, 972 .gem_close_object = nouveau_gem_object_close, 973 974 .dumb_create = nouveau_display_dumb_create, 975 .dumb_map_offset = nouveau_display_dumb_map_offset, 976 .dumb_destroy = drm_gem_dumb_destroy, 977 978 .name = DRIVER_NAME, 979 .desc = DRIVER_DESC, 980 #ifdef GIT_REVISION 981 .date = GIT_REVISION, 982 #else 983 .date = DRIVER_DATE, 984 #endif 985 .major = DRIVER_MAJOR, 986 .minor = DRIVER_MINOR, 987 .patchlevel = DRIVER_PATCHLEVEL, 988 }; 989 990 static struct pci_device_id 991 nouveau_drm_pci_table[] = { 992 { 993 PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_ANY_ID), 994 .class = PCI_BASE_CLASS_DISPLAY << 16, 995 .class_mask = 0xff << 16, 996 }, 997 { 998 PCI_DEVICE(PCI_VENDOR_ID_NVIDIA_SGS, PCI_ANY_ID), 999 .class = PCI_BASE_CLASS_DISPLAY << 16, 1000 .class_mask = 0xff << 16, 1001 }, 1002 {} 1003 }; 1004 1005 static void nouveau_display_options(void) 1006 { 1007 DRM_DEBUG_DRIVER("Loading Nouveau with parameters:\n"); 1008 1009 DRM_DEBUG_DRIVER("... tv_disable : %d\n", nouveau_tv_disable); 1010 DRM_DEBUG_DRIVER("... ignorelid : %d\n", nouveau_ignorelid); 1011 DRM_DEBUG_DRIVER("... duallink : %d\n", nouveau_duallink); 1012 DRM_DEBUG_DRIVER("... nofbaccel : %d\n", nouveau_nofbaccel); 1013 DRM_DEBUG_DRIVER("... config : %s\n", nouveau_config); 1014 DRM_DEBUG_DRIVER("... debug : %s\n", nouveau_debug); 1015 DRM_DEBUG_DRIVER("... noaccel : %d\n", nouveau_noaccel); 1016 DRM_DEBUG_DRIVER("... modeset : %d\n", nouveau_modeset); 1017 DRM_DEBUG_DRIVER("... runpm : %d\n", nouveau_runtime_pm); 1018 DRM_DEBUG_DRIVER("... vram_pushbuf : %d\n", nouveau_vram_pushbuf); 1019 DRM_DEBUG_DRIVER("... pstate : %d\n", nouveau_pstate); 1020 } 1021 1022 static const struct dev_pm_ops nouveau_pm_ops = { 1023 .suspend = nouveau_pmops_suspend, 1024 .resume = nouveau_pmops_resume, 1025 .freeze = nouveau_pmops_freeze, 1026 .thaw = nouveau_pmops_thaw, 1027 .poweroff = nouveau_pmops_freeze, 1028 .restore = nouveau_pmops_resume, 1029 .runtime_suspend = nouveau_pmops_runtime_suspend, 1030 .runtime_resume = nouveau_pmops_runtime_resume, 1031 .runtime_idle = nouveau_pmops_runtime_idle, 1032 }; 1033 1034 static struct pci_driver 1035 nouveau_drm_pci_driver = { 1036 .name = "nouveau", 1037 .id_table = nouveau_drm_pci_table, 1038 .probe = nouveau_drm_probe, 1039 .remove = nouveau_drm_remove, 1040 .driver.pm = &nouveau_pm_ops, 1041 }; 1042 1043 struct drm_device * 1044 nouveau_platform_device_create(struct platform_device *pdev, 1045 struct nvkm_device **pdevice) 1046 { 1047 struct drm_device *drm; 1048 int err; 1049 1050 err = nvkm_device_tegra_new(pdev, nouveau_config, nouveau_debug, 1051 true, true, ~0ULL, pdevice); 1052 if (err) 1053 goto err_free; 1054 1055 drm = drm_dev_alloc(&driver_platform, &pdev->dev); 1056 if (!drm) { 1057 err = -ENOMEM; 1058 goto err_free; 1059 } 1060 1061 err = drm_dev_set_unique(drm, "%s", dev_name(&pdev->dev)); 1062 if (err < 0) 1063 goto err_free; 1064 1065 drm->platformdev = pdev; 1066 platform_set_drvdata(pdev, drm); 1067 1068 return drm; 1069 1070 err_free: 1071 nvkm_device_del(pdevice); 1072 1073 return ERR_PTR(err); 1074 } 1075 1076 static int __init 1077 nouveau_drm_init(void) 1078 { 1079 driver_pci = driver_stub; 1080 driver_pci.set_busid = drm_pci_set_busid; 1081 driver_platform = driver_stub; 1082 driver_platform.set_busid = drm_platform_set_busid; 1083 1084 nouveau_display_options(); 1085 1086 if (nouveau_modeset == -1) { 1087 #ifdef CONFIG_VGA_CONSOLE 1088 if (vgacon_text_force()) 1089 nouveau_modeset = 0; 1090 #endif 1091 } 1092 1093 if (!nouveau_modeset) 1094 return 0; 1095 1096 #ifdef CONFIG_NOUVEAU_PLATFORM_DRIVER 1097 platform_driver_register(&nouveau_platform_driver); 1098 #endif 1099 1100 nouveau_register_dsm_handler(); 1101 return drm_pci_init(&driver_pci, &nouveau_drm_pci_driver); 1102 } 1103 1104 static void __exit 1105 nouveau_drm_exit(void) 1106 { 1107 if (!nouveau_modeset) 1108 return; 1109 1110 drm_pci_exit(&driver_pci, &nouveau_drm_pci_driver); 1111 nouveau_unregister_dsm_handler(); 1112 1113 #ifdef CONFIG_NOUVEAU_PLATFORM_DRIVER 1114 platform_driver_unregister(&nouveau_platform_driver); 1115 #endif 1116 } 1117 1118 module_init(nouveau_drm_init); 1119 module_exit(nouveau_drm_exit); 1120 1121 MODULE_DEVICE_TABLE(pci, nouveau_drm_pci_table); 1122 MODULE_AUTHOR(DRIVER_AUTHOR); 1123 MODULE_DESCRIPTION(DRIVER_DESC); 1124 MODULE_LICENSE("GPL and additional rights"); 1125