x86/xen: resume timer irqs early
[linux/fpc-iii.git] / drivers / gpu / drm / nouveau / nouveau_drm.c
blob95aae9110405fee180e15286a2afced03e34aef0
1 /*
2 * Copyright 2012 Red Hat Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
22 * Authors: Ben Skeggs
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>
37 #include <engine/device.h>
38 #include <engine/disp.h>
39 #include <engine/fifo.h>
41 #include <subdev/vm.h>
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"
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);
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);
66 MODULE_PARM_DESC(noaccel, "disable kernel/abi16 acceleration");
67 static int nouveau_noaccel = 0;
68 module_param_named(noaccel, nouveau_noaccel, int, 0400);
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);
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);
79 static struct drm_driver driver;
81 static int
82 nouveau_drm_vblank_handler(struct nouveau_eventh *event, int head)
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;
90 static int
91 nouveau_drm_vblank_enable(struct drm_device *dev, int head)
93 struct nouveau_drm *drm = nouveau_drm(dev);
94 struct nouveau_disp *pdisp = nouveau_disp(drm->device);
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;
104 static void
105 nouveau_drm_vblank_disable(struct drm_device *dev, int head)
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;
116 static u64
117 nouveau_name(struct pci_dev *pdev)
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);
125 static int
126 nouveau_cli_create(struct pci_dev *pdev, const char *name,
127 int size, void **pcli)
129 struct nouveau_cli *cli;
130 int ret;
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;
143 mutex_init(&cli->mutex);
144 return 0;
147 static void
148 nouveau_cli_destroy(struct nouveau_cli *cli)
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);
157 static void
158 nouveau_accel_fini(struct nouveau_drm *drm)
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);
167 static void
168 nouveau_accel_init(struct nouveau_drm *drm)
170 struct nouveau_device *device = nv_device(drm->device);
171 struct nouveau_object *object;
172 u32 arg0, arg1;
173 int ret;
175 if (nouveau_noaccel || !nouveau_fifo(device) /*XXX*/)
176 return;
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;
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);
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);
212 arg0 = NvDmaFB;
213 arg1 = NvDmaTT;
214 } else {
215 arg0 = NvDmaFB;
216 arg1 = NvDmaTT;
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;
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;
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;
252 nouveau_bo_move_init(drm);
255 static int nouveau_drm_probe(struct pci_dev *pdev,
256 const struct pci_device_id *pent)
258 struct nouveau_device *device;
259 struct apertures_struct *aper;
260 bool boot = false;
261 int ret;
263 /* remove conflicting drivers (vesafb, efifb etc) */
264 aper = alloc_apertures(3);
265 if (!aper)
266 return -ENOMEM;
268 aper->ranges[0].base = pci_resource_start(pdev, 1);
269 aper->ranges[0].size = pci_resource_len(pdev, 1);
270 aper->count = 1;
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++;
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++;
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);
290 ret = nouveau_device_create(pdev, nouveau_name(pdev), pci_name(pdev),
291 nouveau_config, nouveau_debug, &device);
292 if (ret)
293 return ret;
295 pci_set_master(pdev);
297 ret = drm_get_pci_dev(pdev, pent, &driver);
298 if (ret) {
299 nouveau_object_ref(NULL, (struct nouveau_object **)&device);
300 return ret;
303 return 0;
306 #define PCI_CLASS_MULTIMEDIA_HD_AUDIO 0x0403
308 static void
309 nouveau_get_hdmi_dev(struct drm_device *dev)
311 struct nouveau_drm *drm = dev->dev_private;
312 struct pci_dev *pdev = dev->pdev;
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));
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;
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;
331 static int
332 nouveau_drm_load(struct drm_device *dev, unsigned long flags)
334 struct pci_dev *pdev = dev->pdev;
335 struct nouveau_device *device;
336 struct nouveau_drm *drm;
337 int ret;
339 ret = nouveau_cli_create(pdev, "DRM", sizeof(*drm), (void**)&drm);
340 if (ret)
341 return ret;
343 dev->dev_private = drm;
344 drm->dev = dev;
346 INIT_LIST_HEAD(&drm->clients);
347 spin_lock_init(&drm->tile.lock);
349 nouveau_get_hdmi_dev(dev);
351 /* make sure AGP controller is in a consistent state before we
352 * (possibly) execute vbios init tables (see nouveau_agp.h)
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
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;
371 nouveau_agp_reset(drm);
372 nouveau_object_del(nv_object(drm), NVDRM_CLIENT, NVDRM_DEVICE);
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;
385 dev->irq_enabled = true;
387 /* workaround an odd issue on nvc1 by disabling the device's
388 * nosnoop capability. hopefully won't cause issues until a
389 * better fix is found - assuming there is one...
391 device = nv_device(drm->device);
392 if (nv_device(drm->device)->chipset == 0xc1)
393 nv_mask(device, 0x00088080, 0x00000800, 0x00000000);
395 nouveau_vga_init(drm);
396 nouveau_agp_init(drm);
398 if (device->card_type >= NV_50) {
399 ret = nouveau_vm_new(nv_device(drm->device), 0, (1ULL << 40),
400 0x1000, &drm->client.base.vm);
401 if (ret)
402 goto fail_device;
405 ret = nouveau_ttm_init(drm);
406 if (ret)
407 goto fail_ttm;
409 ret = nouveau_bios_init(dev);
410 if (ret)
411 goto fail_bios;
413 ret = nouveau_display_create(dev);
414 if (ret)
415 goto fail_dispctor;
417 if (dev->mode_config.num_crtc) {
418 ret = nouveau_display_init(dev);
419 if (ret)
420 goto fail_dispinit;
423 nouveau_pm_init(dev);
425 nouveau_accel_init(drm);
426 nouveau_fbcon_init(dev);
428 if (nouveau_runtime_pm != 0) {
429 pm_runtime_use_autosuspend(dev->dev);
430 pm_runtime_set_autosuspend_delay(dev->dev, 5000);
431 pm_runtime_set_active(dev->dev);
432 pm_runtime_allow(dev->dev);
433 pm_runtime_mark_last_busy(dev->dev);
434 pm_runtime_put(dev->dev);
436 return 0;
438 fail_dispinit:
439 nouveau_display_destroy(dev);
440 fail_dispctor:
441 nouveau_bios_takedown(dev);
442 fail_bios:
443 nouveau_ttm_fini(drm);
444 fail_ttm:
445 nouveau_agp_fini(drm);
446 nouveau_vga_fini(drm);
447 fail_device:
448 nouveau_cli_destroy(&drm->client);
449 return ret;
452 static int
453 nouveau_drm_unload(struct drm_device *dev)
455 struct nouveau_drm *drm = nouveau_drm(dev);
457 pm_runtime_get_sync(dev->dev);
458 nouveau_fbcon_fini(dev);
459 nouveau_accel_fini(drm);
461 nouveau_pm_fini(dev);
463 if (dev->mode_config.num_crtc)
464 nouveau_display_fini(dev);
465 nouveau_display_destroy(dev);
467 nouveau_bios_takedown(dev);
469 nouveau_ttm_fini(drm);
470 nouveau_agp_fini(drm);
471 nouveau_vga_fini(drm);
473 if (drm->hdmi_device)
474 pci_dev_put(drm->hdmi_device);
475 nouveau_cli_destroy(&drm->client);
476 return 0;
479 static void
480 nouveau_drm_remove(struct pci_dev *pdev)
482 struct drm_device *dev = pci_get_drvdata(pdev);
483 struct nouveau_drm *drm = nouveau_drm(dev);
484 struct nouveau_object *device;
486 dev->irq_enabled = false;
487 device = drm->client.base.device;
488 drm_put_dev(dev);
490 nouveau_object_ref(NULL, &device);
491 nouveau_object_debug();
494 static int
495 nouveau_do_suspend(struct drm_device *dev)
497 struct nouveau_drm *drm = nouveau_drm(dev);
498 struct nouveau_cli *cli;
499 int ret;
501 if (dev->mode_config.num_crtc) {
502 NV_SUSPEND(drm, "suspending display...\n");
503 ret = nouveau_display_suspend(dev);
504 if (ret)
505 return ret;
508 NV_SUSPEND(drm, "evicting buffers...\n");
509 ttm_bo_evict_mm(&drm->ttm.bdev, TTM_PL_VRAM);
511 NV_SUSPEND(drm, "waiting for kernel channels to go idle...\n");
512 if (drm->cechan) {
513 ret = nouveau_channel_idle(drm->cechan);
514 if (ret)
515 return ret;
518 if (drm->channel) {
519 ret = nouveau_channel_idle(drm->channel);
520 if (ret)
521 return ret;
524 NV_SUSPEND(drm, "suspending client object trees...\n");
525 if (drm->fence && nouveau_fence(drm)->suspend) {
526 if (!nouveau_fence(drm)->suspend(drm))
527 return -ENOMEM;
530 list_for_each_entry(cli, &drm->clients, head) {
531 ret = nouveau_client_fini(&cli->base, true);
532 if (ret)
533 goto fail_client;
536 NV_SUSPEND(drm, "suspending kernel object tree...\n");
537 ret = nouveau_client_fini(&drm->client.base, true);
538 if (ret)
539 goto fail_client;
541 nouveau_agp_fini(drm);
542 return 0;
544 fail_client:
545 list_for_each_entry_continue_reverse(cli, &drm->clients, head) {
546 nouveau_client_init(&cli->base);
549 if (dev->mode_config.num_crtc) {
550 NV_SUSPEND(drm, "resuming display...\n");
551 nouveau_display_resume(dev);
553 return ret;
556 int nouveau_pmops_suspend(struct device *dev)
558 struct pci_dev *pdev = to_pci_dev(dev);
559 struct drm_device *drm_dev = pci_get_drvdata(pdev);
560 int ret;
562 if (drm_dev->switch_power_state == DRM_SWITCH_POWER_OFF ||
563 drm_dev->switch_power_state == DRM_SWITCH_POWER_DYNAMIC_OFF)
564 return 0;
566 if (drm_dev->mode_config.num_crtc)
567 nouveau_fbcon_set_suspend(drm_dev, 1);
569 nv_suspend_set_printk_level(NV_DBG_INFO);
570 ret = nouveau_do_suspend(drm_dev);
571 if (ret)
572 return ret;
574 pci_save_state(pdev);
575 pci_disable_device(pdev);
576 pci_set_power_state(pdev, PCI_D3hot);
577 nv_suspend_set_printk_level(NV_DBG_DEBUG);
579 return 0;
582 static int
583 nouveau_do_resume(struct drm_device *dev)
585 struct nouveau_drm *drm = nouveau_drm(dev);
586 struct nouveau_cli *cli;
588 NV_SUSPEND(drm, "re-enabling device...\n");
590 nouveau_agp_reset(drm);
592 NV_SUSPEND(drm, "resuming kernel object tree...\n");
593 nouveau_client_init(&drm->client.base);
594 nouveau_agp_init(drm);
596 NV_SUSPEND(drm, "resuming client object trees...\n");
597 if (drm->fence && nouveau_fence(drm)->resume)
598 nouveau_fence(drm)->resume(drm);
600 list_for_each_entry(cli, &drm->clients, head) {
601 nouveau_client_init(&cli->base);
604 nouveau_run_vbios_init(dev);
605 nouveau_pm_resume(dev);
607 if (dev->mode_config.num_crtc) {
608 NV_SUSPEND(drm, "resuming display...\n");
609 nouveau_display_repin(dev);
612 return 0;
615 int nouveau_pmops_resume(struct device *dev)
617 struct pci_dev *pdev = to_pci_dev(dev);
618 struct drm_device *drm_dev = pci_get_drvdata(pdev);
619 int ret;
621 if (drm_dev->switch_power_state == DRM_SWITCH_POWER_OFF ||
622 drm_dev->switch_power_state == DRM_SWITCH_POWER_DYNAMIC_OFF)
623 return 0;
625 pci_set_power_state(pdev, PCI_D0);
626 pci_restore_state(pdev);
627 ret = pci_enable_device(pdev);
628 if (ret)
629 return ret;
630 pci_set_master(pdev);
632 nv_suspend_set_printk_level(NV_DBG_INFO);
633 ret = nouveau_do_resume(drm_dev);
634 if (ret) {
635 nv_suspend_set_printk_level(NV_DBG_DEBUG);
636 return ret;
638 if (drm_dev->mode_config.num_crtc)
639 nouveau_fbcon_set_suspend(drm_dev, 0);
641 nouveau_fbcon_zfill_all(drm_dev);
642 if (drm_dev->mode_config.num_crtc)
643 nouveau_display_resume(drm_dev);
644 nv_suspend_set_printk_level(NV_DBG_DEBUG);
645 return 0;
648 static int nouveau_pmops_freeze(struct device *dev)
650 struct pci_dev *pdev = to_pci_dev(dev);
651 struct drm_device *drm_dev = pci_get_drvdata(pdev);
652 int ret;
654 nv_suspend_set_printk_level(NV_DBG_INFO);
655 if (drm_dev->mode_config.num_crtc)
656 nouveau_fbcon_set_suspend(drm_dev, 1);
658 ret = nouveau_do_suspend(drm_dev);
659 nv_suspend_set_printk_level(NV_DBG_DEBUG);
660 return ret;
663 static int nouveau_pmops_thaw(struct device *dev)
665 struct pci_dev *pdev = to_pci_dev(dev);
666 struct drm_device *drm_dev = pci_get_drvdata(pdev);
667 int ret;
669 nv_suspend_set_printk_level(NV_DBG_INFO);
670 ret = nouveau_do_resume(drm_dev);
671 if (ret) {
672 nv_suspend_set_printk_level(NV_DBG_DEBUG);
673 return ret;
675 if (drm_dev->mode_config.num_crtc)
676 nouveau_fbcon_set_suspend(drm_dev, 0);
677 nouveau_fbcon_zfill_all(drm_dev);
678 if (drm_dev->mode_config.num_crtc)
679 nouveau_display_resume(drm_dev);
680 nv_suspend_set_printk_level(NV_DBG_DEBUG);
681 return 0;
685 static int
686 nouveau_drm_open(struct drm_device *dev, struct drm_file *fpriv)
688 struct pci_dev *pdev = dev->pdev;
689 struct nouveau_drm *drm = nouveau_drm(dev);
690 struct nouveau_cli *cli;
691 char name[32], tmpname[TASK_COMM_LEN];
692 int ret;
694 /* need to bring up power immediately if opening device */
695 ret = pm_runtime_get_sync(dev->dev);
696 if (ret < 0)
697 return ret;
699 get_task_comm(tmpname, current);
700 snprintf(name, sizeof(name), "%s[%d]", tmpname, pid_nr(fpriv->pid));
702 ret = nouveau_cli_create(pdev, name, sizeof(*cli), (void **)&cli);
703 if (ret)
704 goto out_suspend;
706 if (nv_device(drm->device)->card_type >= NV_50) {
707 ret = nouveau_vm_new(nv_device(drm->device), 0, (1ULL << 40),
708 0x1000, &cli->base.vm);
709 if (ret) {
710 nouveau_cli_destroy(cli);
711 goto out_suspend;
715 fpriv->driver_priv = cli;
717 mutex_lock(&drm->client.mutex);
718 list_add(&cli->head, &drm->clients);
719 mutex_unlock(&drm->client.mutex);
721 out_suspend:
722 pm_runtime_mark_last_busy(dev->dev);
723 pm_runtime_put_autosuspend(dev->dev);
725 return ret;
728 static void
729 nouveau_drm_preclose(struct drm_device *dev, struct drm_file *fpriv)
731 struct nouveau_cli *cli = nouveau_cli(fpriv);
732 struct nouveau_drm *drm = nouveau_drm(dev);
734 pm_runtime_get_sync(dev->dev);
736 if (cli->abi16)
737 nouveau_abi16_fini(cli->abi16);
739 mutex_lock(&drm->client.mutex);
740 list_del(&cli->head);
741 mutex_unlock(&drm->client.mutex);
745 static void
746 nouveau_drm_postclose(struct drm_device *dev, struct drm_file *fpriv)
748 struct nouveau_cli *cli = nouveau_cli(fpriv);
749 nouveau_cli_destroy(cli);
750 pm_runtime_mark_last_busy(dev->dev);
751 pm_runtime_put_autosuspend(dev->dev);
754 static const struct drm_ioctl_desc
755 nouveau_ioctls[] = {
756 DRM_IOCTL_DEF_DRV(NOUVEAU_GETPARAM, nouveau_abi16_ioctl_getparam, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
757 DRM_IOCTL_DEF_DRV(NOUVEAU_SETPARAM, nouveau_abi16_ioctl_setparam, DRM_UNLOCKED|DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
758 DRM_IOCTL_DEF_DRV(NOUVEAU_CHANNEL_ALLOC, nouveau_abi16_ioctl_channel_alloc, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
759 DRM_IOCTL_DEF_DRV(NOUVEAU_CHANNEL_FREE, nouveau_abi16_ioctl_channel_free, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
760 DRM_IOCTL_DEF_DRV(NOUVEAU_GROBJ_ALLOC, nouveau_abi16_ioctl_grobj_alloc, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
761 DRM_IOCTL_DEF_DRV(NOUVEAU_NOTIFIEROBJ_ALLOC, nouveau_abi16_ioctl_notifierobj_alloc, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
762 DRM_IOCTL_DEF_DRV(NOUVEAU_GPUOBJ_FREE, nouveau_abi16_ioctl_gpuobj_free, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
763 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_NEW, nouveau_gem_ioctl_new, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
764 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_PUSHBUF, nouveau_gem_ioctl_pushbuf, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
765 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_CPU_PREP, nouveau_gem_ioctl_cpu_prep, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
766 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_CPU_FINI, nouveau_gem_ioctl_cpu_fini, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
767 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_INFO, nouveau_gem_ioctl_info, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
770 long nouveau_drm_ioctl(struct file *filp,
771 unsigned int cmd, unsigned long arg)
773 struct drm_file *file_priv = filp->private_data;
774 struct drm_device *dev;
775 long ret;
776 dev = file_priv->minor->dev;
778 ret = pm_runtime_get_sync(dev->dev);
779 if (ret < 0)
780 return ret;
782 ret = drm_ioctl(filp, cmd, arg);
784 pm_runtime_mark_last_busy(dev->dev);
785 pm_runtime_put_autosuspend(dev->dev);
786 return ret;
788 static const struct file_operations
789 nouveau_driver_fops = {
790 .owner = THIS_MODULE,
791 .open = drm_open,
792 .release = drm_release,
793 .unlocked_ioctl = nouveau_drm_ioctl,
794 .mmap = nouveau_ttm_mmap,
795 .poll = drm_poll,
796 .read = drm_read,
797 #if defined(CONFIG_COMPAT)
798 .compat_ioctl = nouveau_compat_ioctl,
799 #endif
800 .llseek = noop_llseek,
803 static struct drm_driver
804 driver = {
805 .driver_features =
806 DRIVER_USE_AGP |
807 DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME | DRIVER_RENDER,
809 .load = nouveau_drm_load,
810 .unload = nouveau_drm_unload,
811 .open = nouveau_drm_open,
812 .preclose = nouveau_drm_preclose,
813 .postclose = nouveau_drm_postclose,
814 .lastclose = nouveau_vga_lastclose,
816 #if defined(CONFIG_DEBUG_FS)
817 .debugfs_init = nouveau_debugfs_init,
818 .debugfs_cleanup = nouveau_debugfs_takedown,
819 #endif
821 .get_vblank_counter = drm_vblank_count,
822 .enable_vblank = nouveau_drm_vblank_enable,
823 .disable_vblank = nouveau_drm_vblank_disable,
825 .ioctls = nouveau_ioctls,
826 .num_ioctls = ARRAY_SIZE(nouveau_ioctls),
827 .fops = &nouveau_driver_fops,
829 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
830 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
831 .gem_prime_export = drm_gem_prime_export,
832 .gem_prime_import = drm_gem_prime_import,
833 .gem_prime_pin = nouveau_gem_prime_pin,
834 .gem_prime_unpin = nouveau_gem_prime_unpin,
835 .gem_prime_get_sg_table = nouveau_gem_prime_get_sg_table,
836 .gem_prime_import_sg_table = nouveau_gem_prime_import_sg_table,
837 .gem_prime_vmap = nouveau_gem_prime_vmap,
838 .gem_prime_vunmap = nouveau_gem_prime_vunmap,
840 .gem_init_object = nouveau_gem_object_new,
841 .gem_free_object = nouveau_gem_object_del,
842 .gem_open_object = nouveau_gem_object_open,
843 .gem_close_object = nouveau_gem_object_close,
845 .dumb_create = nouveau_display_dumb_create,
846 .dumb_map_offset = nouveau_display_dumb_map_offset,
847 .dumb_destroy = drm_gem_dumb_destroy,
849 .name = DRIVER_NAME,
850 .desc = DRIVER_DESC,
851 #ifdef GIT_REVISION
852 .date = GIT_REVISION,
853 #else
854 .date = DRIVER_DATE,
855 #endif
856 .major = DRIVER_MAJOR,
857 .minor = DRIVER_MINOR,
858 .patchlevel = DRIVER_PATCHLEVEL,
861 static struct pci_device_id
862 nouveau_drm_pci_table[] = {
864 PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_ANY_ID),
865 .class = PCI_BASE_CLASS_DISPLAY << 16,
866 .class_mask = 0xff << 16,
869 PCI_DEVICE(PCI_VENDOR_ID_NVIDIA_SGS, PCI_ANY_ID),
870 .class = PCI_BASE_CLASS_DISPLAY << 16,
871 .class_mask = 0xff << 16,
876 static int nouveau_pmops_runtime_suspend(struct device *dev)
878 struct pci_dev *pdev = to_pci_dev(dev);
879 struct drm_device *drm_dev = pci_get_drvdata(pdev);
880 int ret;
882 if (nouveau_runtime_pm == 0)
883 return -EINVAL;
885 /* are we optimus enabled? */
886 if (nouveau_runtime_pm == -1 && !nouveau_is_optimus() && !nouveau_is_v1_dsm()) {
887 DRM_DEBUG_DRIVER("failing to power off - not optimus\n");
888 return -EINVAL;
891 drm_kms_helper_poll_disable(drm_dev);
892 vga_switcheroo_set_dynamic_switch(pdev, VGA_SWITCHEROO_OFF);
893 nouveau_switcheroo_optimus_dsm();
894 ret = nouveau_do_suspend(drm_dev);
895 pci_save_state(pdev);
896 pci_disable_device(pdev);
897 pci_set_power_state(pdev, PCI_D3cold);
898 drm_dev->switch_power_state = DRM_SWITCH_POWER_DYNAMIC_OFF;
899 return ret;
902 static int nouveau_pmops_runtime_resume(struct device *dev)
904 struct pci_dev *pdev = to_pci_dev(dev);
905 struct drm_device *drm_dev = pci_get_drvdata(pdev);
906 struct nouveau_device *device = nouveau_dev(drm_dev);
907 int ret;
909 if (nouveau_runtime_pm == 0)
910 return -EINVAL;
912 pci_set_power_state(pdev, PCI_D0);
913 pci_restore_state(pdev);
914 ret = pci_enable_device(pdev);
915 if (ret)
916 return ret;
917 pci_set_master(pdev);
919 ret = nouveau_do_resume(drm_dev);
920 if (drm_dev->mode_config.num_crtc)
921 nouveau_display_resume(drm_dev);
922 drm_kms_helper_poll_enable(drm_dev);
923 /* do magic */
924 nv_mask(device, 0x88488, (1 << 25), (1 << 25));
925 vga_switcheroo_set_dynamic_switch(pdev, VGA_SWITCHEROO_ON);
926 drm_dev->switch_power_state = DRM_SWITCH_POWER_ON;
927 return ret;
930 static int nouveau_pmops_runtime_idle(struct device *dev)
932 struct pci_dev *pdev = to_pci_dev(dev);
933 struct drm_device *drm_dev = pci_get_drvdata(pdev);
934 struct nouveau_drm *drm = nouveau_drm(drm_dev);
935 struct drm_crtc *crtc;
937 if (nouveau_runtime_pm == 0)
938 return -EBUSY;
940 /* are we optimus enabled? */
941 if (nouveau_runtime_pm == -1 && !nouveau_is_optimus() && !nouveau_is_v1_dsm()) {
942 DRM_DEBUG_DRIVER("failing to power off - not optimus\n");
943 return -EBUSY;
946 /* if we have a hdmi audio device - make sure it has a driver loaded */
947 if (drm->hdmi_device) {
948 if (!drm->hdmi_device->driver) {
949 DRM_DEBUG_DRIVER("failing to power off - no HDMI audio driver loaded\n");
950 pm_runtime_mark_last_busy(dev);
951 return -EBUSY;
955 list_for_each_entry(crtc, &drm->dev->mode_config.crtc_list, head) {
956 if (crtc->enabled) {
957 DRM_DEBUG_DRIVER("failing to power off - crtc active\n");
958 return -EBUSY;
961 pm_runtime_mark_last_busy(dev);
962 pm_runtime_autosuspend(dev);
963 /* we don't want the main rpm_idle to call suspend - we want to autosuspend */
964 return 1;
967 static const struct dev_pm_ops nouveau_pm_ops = {
968 .suspend = nouveau_pmops_suspend,
969 .resume = nouveau_pmops_resume,
970 .freeze = nouveau_pmops_freeze,
971 .thaw = nouveau_pmops_thaw,
972 .poweroff = nouveau_pmops_freeze,
973 .restore = nouveau_pmops_resume,
974 .runtime_suspend = nouveau_pmops_runtime_suspend,
975 .runtime_resume = nouveau_pmops_runtime_resume,
976 .runtime_idle = nouveau_pmops_runtime_idle,
979 static struct pci_driver
980 nouveau_drm_pci_driver = {
981 .name = "nouveau",
982 .id_table = nouveau_drm_pci_table,
983 .probe = nouveau_drm_probe,
984 .remove = nouveau_drm_remove,
985 .driver.pm = &nouveau_pm_ops,
988 static int __init
989 nouveau_drm_init(void)
991 if (nouveau_modeset == -1) {
992 #ifdef CONFIG_VGA_CONSOLE
993 if (vgacon_text_force())
994 nouveau_modeset = 0;
995 #endif
998 if (!nouveau_modeset)
999 return 0;
1001 nouveau_register_dsm_handler();
1002 return drm_pci_init(&driver, &nouveau_drm_pci_driver);
1005 static void __exit
1006 nouveau_drm_exit(void)
1008 if (!nouveau_modeset)
1009 return;
1011 drm_pci_exit(&driver, &nouveau_drm_pci_driver);
1012 nouveau_unregister_dsm_handler();
1015 module_init(nouveau_drm_init);
1016 module_exit(nouveau_drm_exit);
1018 MODULE_DEVICE_TABLE(pci, nouveau_drm_pci_table);
1019 MODULE_AUTHOR(DRIVER_AUTHOR);
1020 MODULE_DESCRIPTION(DRIVER_DESC);
1021 MODULE_LICENSE("GPL and additional rights");