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