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