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