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