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