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