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/delay.h>
27 #include <linux/module.h>
28 #include <linux/pci.h>
29 #include <linux/pm_runtime.h>
30 #include <linux/vga_switcheroo.h>
31 #include <linux/mmu_notifier.h>
32 
33 #include <drm/drm_crtc_helper.h>
34 #include <drm/drm_ioctl.h>
35 #include <drm/drm_vblank.h>
36 
37 #include <core/gpuobj.h>
38 #include <core/option.h>
39 #include <core/pci.h>
40 #include <core/tegra.h>
41 
42 #include <nvif/driver.h>
43 #include <nvif/fifo.h>
44 #include <nvif/push006c.h>
45 #include <nvif/user.h>
46 
47 #include <nvif/class.h>
48 #include <nvif/cl0002.h>
49 #include <nvif/cla06f.h>
50 
51 #include "nouveau_drv.h"
52 #include "nouveau_dma.h"
53 #include "nouveau_ttm.h"
54 #include "nouveau_gem.h"
55 #include "nouveau_vga.h"
56 #include "nouveau_led.h"
57 #include "nouveau_hwmon.h"
58 #include "nouveau_acpi.h"
59 #include "nouveau_bios.h"
60 #include "nouveau_ioctl.h"
61 #include "nouveau_abi16.h"
62 #include "nouveau_fbcon.h"
63 #include "nouveau_fence.h"
64 #include "nouveau_debugfs.h"
65 #include "nouveau_usif.h"
66 #include "nouveau_connector.h"
67 #include "nouveau_platform.h"
68 #include "nouveau_svm.h"
69 #include "nouveau_dmem.h"
70 
71 MODULE_PARM_DESC(config, "option string to pass to driver core");
72 static char *nouveau_config;
73 module_param_named(config, nouveau_config, charp, 0400);
74 
75 MODULE_PARM_DESC(debug, "debug string to pass to driver core");
76 static char *nouveau_debug;
77 module_param_named(debug, nouveau_debug, charp, 0400);
78 
79 MODULE_PARM_DESC(noaccel, "disable kernel/abi16 acceleration");
80 static int nouveau_noaccel = 0;
81 module_param_named(noaccel, nouveau_noaccel, int, 0400);
82 
83 MODULE_PARM_DESC(modeset, "enable driver (default: auto, "
84                           "0 = disabled, 1 = enabled, 2 = headless)");
85 int nouveau_modeset = -1;
86 module_param_named(modeset, nouveau_modeset, int, 0400);
87 
88 MODULE_PARM_DESC(atomic, "Expose atomic ioctl (default: disabled)");
89 static int nouveau_atomic = 0;
90 module_param_named(atomic, nouveau_atomic, int, 0400);
91 
92 MODULE_PARM_DESC(runpm, "disable (0), force enable (1), optimus only default (-1)");
93 static int nouveau_runtime_pm = -1;
94 module_param_named(runpm, nouveau_runtime_pm, int, 0400);
95 
96 static struct drm_driver driver_stub;
97 static struct drm_driver driver_pci;
98 static struct drm_driver driver_platform;
99 
nouveau_pci_name(struct pci_dev *pdev)100 static u64 nouveau_pci_name(struct pci_dev *pdev)
101 {
102     u64 name = (u64)pci_domain_nr(pdev->bus) << 0x20;
103     name |= pdev->bus->number << 0x10;
104     name |= PCI_SLOT(pdev->devfn) << 0x8;
105     return name | PCI_FUNC(pdev->devfn);
106 }
107 
nouveau_platform_name(struct platform_device *platformdev)108 static u64 nouveau_platform_name(struct platform_device *platformdev)
109 {
110     return platformdev->id;
111 }
112 
nouveau_name(struct drm_device *dev)113 static u64 nouveau_name(struct drm_device *dev)
114 {
115     if (dev->pdev) {
116         return nouveau_pci_name(dev->pdev);
117     } else {
118         return nouveau_platform_name(to_platform_device(dev->dev));
119     }
120 }
121 
nouveau_cli_work_ready(struct dma_fence *fence)122 static inline bool nouveau_cli_work_ready(struct dma_fence *fence)
123 {
124     if (!dma_fence_is_signaled(fence)) {
125         return false;
126     }
127     dma_fence_put(fence);
128     return true;
129 }
130 
nouveau_cli_work(struct work_struct *w)131 static void nouveau_cli_work(struct work_struct *w)
132 {
133     struct nouveau_cli *cli = container_of(w, typeof(*cli), work);
134     struct nouveau_cli_work *work, *wtmp;
135     mutex_lock(&cli->lock);
136     list_for_each_entry_safe(work, wtmp, &cli->worker, head)
137     {
138         if (!work->fence || nouveau_cli_work_ready(work->fence)) {
139             list_del(&work->head);
140             work->func(work);
141         }
142     }
143     mutex_unlock(&cli->lock);
144 }
145 
nouveau_cli_work_fence(struct dma_fence *fence, struct dma_fence_cb *cb)146 static void nouveau_cli_work_fence(struct dma_fence *fence, struct dma_fence_cb *cb)
147 {
148     struct nouveau_cli_work *work = container_of(cb, typeof(*work), cb);
149     schedule_work(&work->cli->work);
150 }
151 
nouveau_cli_work_queue(struct nouveau_cli *cli, struct dma_fence *fence, struct nouveau_cli_work *work)152 void nouveau_cli_work_queue(struct nouveau_cli *cli, struct dma_fence *fence, struct nouveau_cli_work *work)
153 {
154     work->fence = dma_fence_get(fence);
155     work->cli = cli;
156     mutex_lock(&cli->lock);
157     list_add_tail(&work->head, &cli->worker);
158     if (dma_fence_add_callback(fence, &work->cb, nouveau_cli_work_fence)) {
159         nouveau_cli_work_fence(fence, &work->cb);
160     }
161     mutex_unlock(&cli->lock);
162 }
163 
nouveau_cli_fini(struct nouveau_cli *cli)164 static void nouveau_cli_fini(struct nouveau_cli *cli)
165 {
166     /* All our channels are dead now, which means all the fences they
167      * own are signalled, and all callback functions have been called.
168      *
169      * So, after flushing the workqueue, there should be nothing left.
170      */
171     flush_work(&cli->work);
172     WARN_ON(!list_empty(&cli->worker));
173 
174     usif_client_fini(cli);
175     nouveau_vmm_fini(&cli->svm);
176     nouveau_vmm_fini(&cli->vmm);
177     nvif_mmu_dtor(&cli->mmu);
178     nvif_device_dtor(&cli->device);
179     mutex_lock(&cli->drm->master.lock);
180     nvif_client_dtor(&cli->base);
181     mutex_unlock(&cli->drm->master.lock);
182 }
183 
nouveau_cli_init(struct nouveau_drm *drm, const char *sname, struct nouveau_cli *cli)184 static int nouveau_cli_init(struct nouveau_drm *drm, const char *sname, struct nouveau_cli *cli)
185 {
186     static const struct nvif_mclass mems[] = {
187         {NVIF_CLASS_MEM_GF100, -1}, {NVIF_CLASS_MEM_NV50, -1}, {NVIF_CLASS_MEM_NV04, -1}, {}};
188     static const struct nvif_mclass mmus[] = {
189         {NVIF_CLASS_MMU_GF100, -1}, {NVIF_CLASS_MMU_NV50, -1}, {NVIF_CLASS_MMU_NV04, -1}, {}};
190     static const struct nvif_mclass vmms[] = {{NVIF_CLASS_VMM_GP100, -1}, {NVIF_CLASS_VMM_GM200, -1},
191                                               {NVIF_CLASS_VMM_GF100, -1}, {NVIF_CLASS_VMM_NV50, -1},
192                                               {NVIF_CLASS_VMM_NV04, -1},  {}};
193     u64 device = nouveau_name(drm->dev);
194     int ret;
195 
196     ret = snprintf(cli->name, sizeof(cli->name), "%s", sname);
197     cli->drm = drm;
198     mutex_init(&cli->mutex);
199     usif_client_init(cli);
200 
201     INIT_WORK(&cli->work, nouveau_cli_work);
202     INIT_LIST_HEAD(&cli->worker);
203     mutex_init(&cli->lock);
204 
205     if (cli == &drm->master) {
206         ret = nvif_driver_init(NULL, nouveau_config, nouveau_debug, cli->name, device, &cli->base);
207     } else {
208         mutex_lock(&drm->master.lock);
209         ret = nvif_client_ctor(&drm->master.base, cli->name, device, &cli->base);
210         mutex_unlock(&drm->master.lock);
211     }
212     if (ret) {
213         NV_PRINTK(err, cli, "Client allocation failed: %d\n", ret);
214         goto done;
215     }
216 
217     ret = nvif_device_ctor(&cli->base.object, "drmDevice", 0, NV_DEVICE,
218                            &(struct nv_device_v0) {
219                                .device = ~0,
220                            },
221                            sizeof(struct nv_device_v0), &cli->device);
222     if (ret) {
223         NV_PRINTK(err, cli, "Device allocation failed: %d\n", ret);
224         goto done;
225     }
226 
227     ret = nvif_mclass(&cli->device.object, mmus);
228     if (ret < 0) {
229         NV_PRINTK(err, cli, "No supported MMU class\n");
230         goto done;
231     }
232 
233     ret = nvif_mmu_ctor(&cli->device.object, "drmMmu", mmus[ret].oclass, &cli->mmu);
234     if (ret) {
235         NV_PRINTK(err, cli, "MMU allocation failed: %d\n", ret);
236         goto done;
237     }
238 
239     ret = nvif_mclass(&cli->mmu.object, vmms);
240     if (ret < 0) {
241         NV_PRINTK(err, cli, "No supported VMM class\n");
242         goto done;
243     }
244 
245     ret = nouveau_vmm_init(cli, vmms[ret].oclass, &cli->vmm);
246     if (ret) {
247         NV_PRINTK(err, cli, "VMM allocation failed: %d\n", ret);
248         goto done;
249     }
250 
251     ret = nvif_mclass(&cli->mmu.object, mems);
252     if (ret < 0) {
253         NV_PRINTK(err, cli, "No supported MEM class\n");
254         goto done;
255     }
256 
257     cli->mem = &mems[ret];
258     return 0;
259 done:
260     if (ret) {
261         nouveau_cli_fini(cli);
262     }
263     return ret;
264 }
265 
nouveau_accel_ce_fini(struct nouveau_drm *drm)266 static void nouveau_accel_ce_fini(struct nouveau_drm *drm)
267 {
268     nouveau_channel_idle(drm->cechan);
269     nvif_object_dtor(&drm->ttm.copy);
270     nouveau_channel_del(&drm->cechan);
271 }
272 
nouveau_accel_ce_init(struct nouveau_drm *drm)273 static void nouveau_accel_ce_init(struct nouveau_drm *drm)
274 {
275     struct nvif_device *device = &drm->client.device;
276     int ret = 0;
277 
278     /* Allocate channel that has access to a (preferably async) copy
279      * engine, to use for TTM buffer moves.
280      */
281     if (device->info.family >= NV_DEVICE_INFO_V0_KEPLER) {
282         ret = nouveau_channel_new(drm, device, nvif_fifo_runlist_ce(device), 0, true, &drm->cechan);
283     } else if (device->info.chipset >= 0xa3 && device->info.chipset != 0xaa && device->info.chipset != 0xac) {
284         /* Prior to Kepler, there's only a single runlist, so all
285          * engines can be accessed from any channel.
286          *
287          * We still want to use a separate channel though.
288          */
289         ret = nouveau_channel_new(drm, device, NvDmaFB, NvDmaTT, false, &drm->cechan);
290     }
291 
292     if (ret) {
293         NV_ERROR(drm, "failed to create ce channel, %d\n", ret);
294     }
295 }
296 
nouveau_accel_gr_fini(struct nouveau_drm *drm)297 static void nouveau_accel_gr_fini(struct nouveau_drm *drm)
298 {
299     nouveau_channel_idle(drm->channel);
300     nvif_object_dtor(&drm->ntfy);
301     nvkm_gpuobj_del(&drm->notify);
302     nouveau_channel_del(&drm->channel);
303 }
304 
nouveau_accel_gr_init(struct nouveau_drm *drm)305 static void nouveau_accel_gr_init(struct nouveau_drm *drm)
306 {
307     struct nvif_device *device = &drm->client.device;
308     u32 arg0, arg1;
309     int ret;
310 
311     /* Allocate channel that has access to the graphics engine. */
312     if (device->info.family >= NV_DEVICE_INFO_V0_KEPLER) {
313         arg0 = nvif_fifo_runlist(device, NV_DEVICE_INFO_ENGINE_GR);
314         arg1 = 1;
315     } else {
316         arg0 = NvDmaFB;
317         arg1 = NvDmaTT;
318     }
319 
320     ret = nouveau_channel_new(drm, device, arg0, arg1, false, &drm->channel);
321     if (ret) {
322         NV_ERROR(drm, "failed to create kernel channel, %d\n", ret);
323         nouveau_accel_gr_fini(drm);
324         return;
325     }
326 
327     /* A SW class is used on pre-NV50 HW to assist with handling the
328      * synchronisation of page flips, as well as to implement fences
329      * on TNT/TNT2 HW that lacks any kind of support in host.
330      */
331     if (!drm->channel->nvsw.client && device->info.family < NV_DEVICE_INFO_V0_TESLA) {
332         ret = nvif_object_ctor(&drm->channel->user, "drmNvsw", NVDRM_NVSW, nouveau_abi16_swclass(drm), NULL, 0,
333                                &drm->channel->nvsw);
334         if (ret == 0) {
335             struct nvif_push *push = drm->channel->chan.push;
336             ret = PUSH_WAIT(push, 0x2);
337             if (ret == 0) {
338                 PUSH_NVSQ(push, NV_SW, 0x0000, drm->channel->nvsw.handle);
339             }
340         }
341 
342         if (ret) {
343             NV_ERROR(drm, "failed to allocate sw class, %d\n", ret);
344             nouveau_accel_gr_fini(drm);
345             return;
346         }
347     }
348 
349     /* NvMemoryToMemoryFormat requires a notifier ctxdma for some reason,
350      * even if notification is never requested, so, allocate a ctxdma on
351      * any GPU where it's possible we'll end up using M2MF for BO moves.
352      */
353     if (device->info.family < NV_DEVICE_INFO_V0_FERMI) {
354         ret = nvkm_gpuobj_new(nvxx_device(device), 0x20, 0, false, NULL, &drm->notify);
355         if (ret) {
356             NV_ERROR(drm, "failed to allocate notifier, %d\n", ret);
357             nouveau_accel_gr_fini(drm);
358             return;
359         }
360 
361         ret = nvif_object_ctor(&drm->channel->user, "drmM2mfNtfy", NvNotify0, NV_DMA_IN_MEMORY,
362                                &(struct nv_dma_v0) {.target = NV_DMA_V0_TARGET_VRAM,
363                                                    .access = NV_DMA_V0_ACCESS_RDWR,
364                                                    .start = drm->notify->addr,
365                                                    .limit = drm->notify->addr + 0x1f},
366                                sizeof(struct nv_dma_v0), &drm->ntfy);
367         if (ret) {
368             nouveau_accel_gr_fini(drm);
369             return;
370         }
371     }
372 }
373 
nouveau_accel_fini(struct nouveau_drm *drm)374 static void nouveau_accel_fini(struct nouveau_drm *drm)
375 {
376     nouveau_accel_ce_fini(drm);
377     nouveau_accel_gr_fini(drm);
378     if (drm->fence) {
379         nouveau_fence(drm)->dtor(drm);
380     }
381 }
382 
nouveau_accel_init(struct nouveau_drm *drm)383 static void nouveau_accel_init(struct nouveau_drm *drm)
384 {
385     struct nvif_device *device = &drm->client.device;
386     struct nvif_sclass *sclass;
387     int ret, i, n;
388 
389     if (nouveau_noaccel) {
390         return;
391     }
392 
393     /* Initialise global support for channels, and synchronisation. */
394     ret = nouveau_channels_init(drm);
395     if (ret) {
396         return;
397     }
398 
399     /* XXX: this is crap, but the fence/channel stuff is a little
400      *     backwards in some places.  this will be fixed.
401      */
402     ret = n = nvif_object_sclass_get(&device->object, &sclass);
403     if (ret < 0) {
404         return;
405     }
406 
407     for (ret = -ENOSYS, i = 0; i < n; i++) {
408         switch (sclass[i].oclass) {
409             case NV03_CHANNEL_DMA:
410                 ret = nv04_fence_create(drm);
411                 break;
412             case NV10_CHANNEL_DMA:
413                 ret = nv10_fence_create(drm);
414                 break;
415             case NV17_CHANNEL_DMA:
416             case NV40_CHANNEL_DMA:
417                 ret = nv17_fence_create(drm);
418                 break;
419             case NV50_CHANNEL_GPFIFO:
420                 ret = nv50_fence_create(drm);
421                 break;
422             case G82_CHANNEL_GPFIFO:
423                 ret = nv84_fence_create(drm);
424                 break;
425             case FERMI_CHANNEL_GPFIFO:
426             case KEPLER_CHANNEL_GPFIFO_A:
427             case KEPLER_CHANNEL_GPFIFO_B:
428             case MAXWELL_CHANNEL_GPFIFO_A:
429             case PASCAL_CHANNEL_GPFIFO_A:
430             case VOLTA_CHANNEL_GPFIFO_A:
431             case TURING_CHANNEL_GPFIFO_A:
432                 ret = nvc0_fence_create(drm);
433                 break;
434             default:
435                 break;
436         }
437     }
438 
439     nvif_object_sclass_put(&sclass);
440     if (ret) {
441         NV_ERROR(drm, "failed to initialise sync subsystem, %d\n", ret);
442         nouveau_accel_fini(drm);
443         return;
444     }
445 
446     /* Volta requires access to a doorbell register for kickoff. */
447     if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_VOLTA) {
448         ret = nvif_user_ctor(device, "drmUsermode");
449         if (ret) {
450             return;
451         }
452     }
453 
454     /* Allocate channels we need to support various functions. */
455     nouveau_accel_gr_init(drm);
456     nouveau_accel_ce_init(drm);
457 
458     /* Initialise accelerated TTM buffer moves. */
459     nouveau_bo_move_init(drm);
460 }
461 
nouveau_drm_errorf(struct nvif_object *object, const char *fmt, ...)462 static void __printf(2, 3) nouveau_drm_errorf(struct nvif_object *object, const char *fmt, ...)
463 {
464     struct nouveau_drm *drm = container_of(object->parent, typeof(*drm), parent);
465     struct va_format vaf;
466     va_list va;
467 
468     va_start(va, fmt);
469     vaf.fmt = fmt;
470     vaf.va = &va;
471     NV_ERROR(drm, "%pV", &vaf);
472     va_end(va);
473 }
474 
nouveau_drm_debugf(struct nvif_object *object, const char *fmt, ...)475 static void __printf(2, 3) nouveau_drm_debugf(struct nvif_object *object, const char *fmt, ...)
476 {
477     struct nouveau_drm *drm = container_of(object->parent, typeof(*drm), parent);
478     struct va_format vaf;
479     va_list va;
480 
481     va_start(va, fmt);
482     vaf.fmt = fmt;
483     vaf.va = &va;
484     NV_DEBUG(drm, "%pV", &vaf);
485     va_end(va);
486 }
487 
488 static const struct nvif_parent_func nouveau_parent = {
489     .debugf = nouveau_drm_debugf,
490     .errorf = nouveau_drm_errorf,
491 };
492 
nouveau_drm_device_init(struct drm_device *dev)493 static int nouveau_drm_device_init(struct drm_device *dev)
494 {
495     struct nouveau_drm *drm;
496     int ret;
497 
498     if (!(drm = kzalloc(sizeof(*drm), GFP_KERNEL))) {
499         return -ENOMEM;
500     }
501     dev->dev_private = drm;
502     drm->dev = dev;
503 
504     nvif_parent_ctor(&nouveau_parent, &drm->parent);
505     drm->master.base.object.parent = &drm->parent;
506 
507     ret = nouveau_cli_init(drm, "DRM-master", &drm->master);
508     if (ret) {
509         goto fail_alloc;
510     }
511 
512     ret = nouveau_cli_init(drm, "DRM", &drm->client);
513     if (ret) {
514         goto fail_master;
515     }
516 
517     dev->irq_enabled = true;
518 
519     nvxx_client(&drm->client.base)->debug = nvkm_dbgopt(nouveau_debug, "DRM");
520 
521     INIT_LIST_HEAD(&drm->clients);
522     spin_lock_init(&drm->tile.lock);
523 
524     /* workaround an odd issue on nvc1 by disabling the device's
525      * nosnoop capability.  hopefully won't cause issues until a
526      * better fix is found - assuming there is one...
527      */
528     if (drm->client.device.info.chipset == 0xc1) {
529         nvif_mask(&drm->client.device.object, 0x00088080, 0x00000800, 0x00000000);
530     }
531 
532     nouveau_vga_init(drm);
533 
534     ret = nouveau_ttm_init(drm);
535     if (ret) {
536         goto fail_ttm;
537     }
538 
539     ret = nouveau_bios_init(dev);
540     if (ret) {
541         goto fail_bios;
542     }
543 
544     nouveau_accel_init(drm);
545 
546     ret = nouveau_display_create(dev);
547     if (ret) {
548         goto fail_dispctor;
549     }
550 
551     if (dev->mode_config.num_crtc) {
552         ret = nouveau_display_init(dev, false, false);
553         if (ret) {
554             goto fail_dispinit;
555         }
556     }
557 
558     nouveau_debugfs_init(drm);
559     nouveau_hwmon_init(dev);
560     nouveau_svm_init(drm);
561     nouveau_dmem_init(drm);
562     nouveau_fbcon_init(dev);
563     nouveau_led_init(dev);
564 
565     if (nouveau_pmops_runtime()) {
566         pm_runtime_use_autosuspend(dev->dev);
567         pm_runtime_set_autosuspend_delay(dev->dev, 0x1388);
568         pm_runtime_set_active(dev->dev);
569         pm_runtime_allow(dev->dev);
570         pm_runtime_mark_last_busy(dev->dev);
571         pm_runtime_put(dev->dev);
572     }
573 
574     return 0;
575 
576 fail_dispinit:
577     nouveau_display_destroy(dev);
578 fail_dispctor:
579     nouveau_accel_fini(drm);
580     nouveau_bios_takedown(dev);
581 fail_bios:
582     nouveau_ttm_fini(drm);
583 fail_ttm:
584     nouveau_vga_fini(drm);
585     nouveau_cli_fini(&drm->client);
586 fail_master:
587     nouveau_cli_fini(&drm->master);
588 fail_alloc:
589     nvif_parent_dtor(&drm->parent);
590     kfree(drm);
591     return ret;
592 }
593 
nouveau_drm_device_fini(struct drm_device *dev)594 static void nouveau_drm_device_fini(struct drm_device *dev)
595 {
596     struct nouveau_drm *drm = nouveau_drm(dev);
597 
598     if (nouveau_pmops_runtime()) {
599         pm_runtime_get_sync(dev->dev);
600         pm_runtime_forbid(dev->dev);
601     }
602 
603     nouveau_led_fini(dev);
604     nouveau_fbcon_fini(dev);
605     nouveau_dmem_fini(drm);
606     nouveau_svm_fini(drm);
607     nouveau_hwmon_fini(dev);
608     nouveau_debugfs_fini(drm);
609 
610     if (dev->mode_config.num_crtc) {
611         nouveau_display_fini(dev, false, false);
612     }
613     nouveau_display_destroy(dev);
614 
615     nouveau_accel_fini(drm);
616     nouveau_bios_takedown(dev);
617 
618     nouveau_ttm_fini(drm);
619     nouveau_vga_fini(drm);
620 
621     nouveau_cli_fini(&drm->client);
622     nouveau_cli_fini(&drm->master);
623     nvif_parent_dtor(&drm->parent);
624     kfree(drm);
625 }
626 
627 /*
628  * On some Intel PCIe bridge controllers doing a
629  * D0 -> D3hot -> D3cold -> D0 sequence causes Nvidia GPUs to not reappear.
630  * Skipping the intermediate D3hot step seems to make it work again. This is
631  * probably caused by not meeting the expectation the involved AML code has
632  * when the GPU is put into D3hot state before invoking it.
633  *
634  * This leads to various manifestations of this issue:
635  *  - AML code execution to power on the GPU hits an infinite loop (as the
636  *    code waits on device memory to change).
637  *  - kernel crashes, as all PCI reads return -1, which most code isn't able
638  *    to handle well enough.
639  *
640  * In all cases dmesg will contain at least one line like this:
641  * 'nouveau 0000:01:00.0: Refused to change power state, currently in D3'
642  * followed by a lot of nouveau timeouts.
643  *
644  * In the \_SB.PCI0.PEG0.PG00._OFF code deeper down writes bit 0x80 to the not
645  * documented PCI config space register 0x248 of the Intel PCIe bridge
646  * controller (0x1901) in order to change the state of the PCIe link between
647  * the PCIe port and the GPU. There are alternative code paths using other
648  * registers, which seem to work fine (executed pre Windows 8):
649  *  - 0xbc bit 0x20 (publicly available documentation claims 'reserved')
650  *  - 0xb0 bit 0x10 (link disable)
651  * Changing the conditions inside the firmware by poking into the relevant
652  * addresses does resolve the issue, but it seemed to be ACPI private memory
653  * and not any device accessible memory at all, so there is no portable way of
654  * changing the conditions.
655  * On a XPS 9560 that means bits [0,3] on \CPEX need to be cleared.
656  *
657  * The only systems where this behavior can be seen are hybrid graphics laptops
658  * with a secondary Nvidia Maxwell, Pascal or Turing GPU. It's unclear whether
659  * this issue only occurs in combination with listed Intel PCIe bridge
660  * controllers and the mentioned GPUs or other devices as well.
661  *
662  * documentation on the PCIe bridge controller can be found in the
663  * "7th Generation Intel® Processor Families for H Platforms Datasheet Volume 2"
664  * Section "12 PCI Express* Controller (x16) Registers"
665  */
666 
quirk_broken_nv_runpm(struct pci_dev *pdev)667 static void quirk_broken_nv_runpm(struct pci_dev *pdev)
668 {
669     struct drm_device *dev = pci_get_drvdata(pdev);
670     struct nouveau_drm *drm = nouveau_drm(dev);
671     struct pci_dev *bridge = pci_upstream_bridge(pdev);
672 
673     if (!bridge || bridge->vendor != PCI_VENDOR_ID_INTEL) {
674         return;
675     }
676 
677     if (bridge->device == 0x1901) {
678         drm->old_pm_cap = pdev->pm_cap;
679         pdev->pm_cap = 0;
680         NV_INFO(drm, "Disabling PCI power management to avoid bug\n");
681     }
682 }
683 
nouveau_drm_probe(struct pci_dev *pdev, const struct pci_device_id *pent)684 static int nouveau_drm_probe(struct pci_dev *pdev, const struct pci_device_id *pent)
685 {
686     struct nvkm_device *device;
687     struct drm_device *drm_dev;
688     int ret;
689 
690     if (vga_switcheroo_client_probe_defer(pdev)) {
691         return -EPROBE_DEFER;
692     }
693 
694     /* We need to check that the chipset is supported before booting
695      * fbdev off the hardware, as there's no way to put it back.
696      */
697     ret = nvkm_device_pci_new(pdev, nouveau_config, "error", true, false, 0, &device);
698     if (ret) {
699         return ret;
700     }
701 
702     nvkm_device_del(&device);
703 
704     /* Remove conflicting drivers (vesafb, efifb etc). */
705     ret = drm_fb_helper_remove_conflicting_pci_framebuffers(pdev, "nouveaufb");
706     if (ret) {
707         return ret;
708     }
709 
710     ret = nvkm_device_pci_new(pdev, nouveau_config, nouveau_debug, true, true, ~0ULL, &device);
711     if (ret) {
712         return ret;
713     }
714 
715     pci_set_master(pdev);
716 
717     if (nouveau_atomic) {
718         driver_pci.driver_features |= DRIVER_ATOMIC;
719     }
720 
721     drm_dev = drm_dev_alloc(&driver_pci, &pdev->dev);
722     if (IS_ERR(drm_dev)) {
723         ret = PTR_ERR(drm_dev);
724         goto fail_nvkm;
725     }
726 
727     ret = pci_enable_device(pdev);
728     if (ret) {
729         goto fail_drm;
730     }
731 
732     drm_dev->pdev = pdev;
733     pci_set_drvdata(pdev, drm_dev);
734 
735     ret = nouveau_drm_device_init(drm_dev);
736     if (ret) {
737         goto fail_pci;
738     }
739 
740     ret = drm_dev_register(drm_dev, pent->driver_data);
741     if (ret) {
742         goto fail_drm_dev_init;
743     }
744 
745     quirk_broken_nv_runpm(pdev);
746     return 0;
747 
748 fail_drm_dev_init:
749     nouveau_drm_device_fini(drm_dev);
750 fail_pci:
751     pci_disable_device(pdev);
752 fail_drm:
753     drm_dev_put(drm_dev);
754 fail_nvkm:
755     nvkm_device_del(&device);
756     return ret;
757 }
758 
nouveau_drm_device_remove(struct drm_device *dev)759 void nouveau_drm_device_remove(struct drm_device *dev)
760 {
761     struct nouveau_drm *drm = nouveau_drm(dev);
762     struct nvkm_client *client;
763     struct nvkm_device *device;
764 
765     drm_dev_unregister(dev);
766 
767     dev->irq_enabled = false;
768     client = nvxx_client(&drm->client.base);
769     device = nvkm_device_find(client->device);
770 
771     nouveau_drm_device_fini(dev);
772     drm_dev_put(dev);
773     nvkm_device_del(&device);
774 }
775 
nouveau_drm_remove(struct pci_dev *pdev)776 static void nouveau_drm_remove(struct pci_dev *pdev)
777 {
778     struct drm_device *dev = pci_get_drvdata(pdev);
779     struct nouveau_drm *drm = nouveau_drm(dev);
780 
781     /* revert our workaround */
782     if (drm->old_pm_cap) {
783         pdev->pm_cap = drm->old_pm_cap;
784     }
785     nouveau_drm_device_remove(dev);
786     pci_disable_device(pdev);
787 }
788 
nouveau_do_suspend(struct drm_device *dev, bool runtime)789 static int nouveau_do_suspend(struct drm_device *dev, bool runtime)
790 {
791     struct nouveau_drm *drm = nouveau_drm(dev);
792     int ret;
793 
794     nouveau_svm_suspend(drm);
795     nouveau_dmem_suspend(drm);
796     nouveau_led_suspend(dev);
797 
798     if (dev->mode_config.num_crtc) {
799         NV_DEBUG(drm, "suspending console...\n");
800         nouveau_fbcon_set_suspend(dev, 1);
801         NV_DEBUG(drm, "suspending display...\n");
802         ret = nouveau_display_suspend(dev, runtime);
803         if (ret) {
804             return ret;
805         }
806     }
807 
808     NV_DEBUG(drm, "evicting buffers...\n");
809     ttm_bo_evict_mm(&drm->ttm.bdev, TTM_PL_VRAM);
810 
811     NV_DEBUG(drm, "waiting for kernel channels to go idle...\n");
812     if (drm->cechan) {
813         ret = nouveau_channel_idle(drm->cechan);
814         if (ret) {
815             goto fail_display;
816         }
817     }
818 
819     if (drm->channel) {
820         ret = nouveau_channel_idle(drm->channel);
821         if (ret) {
822             goto fail_display;
823         }
824     }
825 
826     NV_DEBUG(drm, "suspending fence...\n");
827     if (drm->fence && nouveau_fence(drm)->suspend) {
828         if (!nouveau_fence(drm)->suspend(drm)) {
829             ret = -ENOMEM;
830             goto fail_display;
831         }
832     }
833 
834     NV_DEBUG(drm, "suspending object tree...\n");
835     ret = nvif_client_suspend(&drm->master.base);
836     if (ret) {
837         goto fail_client;
838     }
839 
840     return 0;
841 
842 fail_client:
843     if (drm->fence && nouveau_fence(drm)->resume) {
844         nouveau_fence(drm)->resume(drm);
845     }
846 
847 fail_display:
848     if (dev->mode_config.num_crtc) {
849         NV_DEBUG(drm, "resuming display...\n");
850         nouveau_display_resume(dev, runtime);
851     }
852     return ret;
853 }
854 
nouveau_do_resume(struct drm_device *dev, bool runtime)855 static int nouveau_do_resume(struct drm_device *dev, bool runtime)
856 {
857     int ret = 0;
858     struct nouveau_drm *drm = nouveau_drm(dev);
859 
860     NV_DEBUG(drm, "resuming object tree...\n");
861     ret = nvif_client_resume(&drm->master.base);
862     if (ret) {
863         NV_ERROR(drm, "Client resume failed with error: %d\n", ret);
864         return ret;
865     }
866 
867     NV_DEBUG(drm, "resuming fence...\n");
868     if (drm->fence && nouveau_fence(drm)->resume) {
869         nouveau_fence(drm)->resume(drm);
870     }
871 
872     nouveau_run_vbios_init(dev);
873 
874     if (dev->mode_config.num_crtc) {
875         NV_DEBUG(drm, "resuming display...\n");
876         nouveau_display_resume(dev, runtime);
877         NV_DEBUG(drm, "resuming console...\n");
878         nouveau_fbcon_set_suspend(dev, 0);
879     }
880 
881     nouveau_led_resume(dev);
882     nouveau_dmem_resume(drm);
883     nouveau_svm_resume(drm);
884     return 0;
885 }
886 
nouveau_pmops_suspend(struct device *dev)887 int nouveau_pmops_suspend(struct device *dev)
888 {
889     struct pci_dev *pdev = to_pci_dev(dev);
890     struct drm_device *drm_dev = pci_get_drvdata(pdev);
891     int ret;
892 
893     if (drm_dev->switch_power_state == DRM_SWITCH_POWER_OFF ||
894         drm_dev->switch_power_state == DRM_SWITCH_POWER_DYNAMIC_OFF) {
895         return 0;
896     }
897 
898     ret = nouveau_do_suspend(drm_dev, false);
899     if (ret) {
900         return ret;
901     }
902 
903     pci_save_state(pdev);
904     pci_disable_device(pdev);
905     pci_set_power_state(pdev, PCI_D3hot);
906     udelay(0xc8);
907     return 0;
908 }
909 
nouveau_pmops_resume(struct device *dev)910 int nouveau_pmops_resume(struct device *dev)
911 {
912     struct pci_dev *pdev = to_pci_dev(dev);
913     struct drm_device *drm_dev = pci_get_drvdata(pdev);
914     int ret;
915 
916     if (drm_dev->switch_power_state == DRM_SWITCH_POWER_OFF ||
917         drm_dev->switch_power_state == DRM_SWITCH_POWER_DYNAMIC_OFF) {
918         return 0;
919     }
920 
921     pci_set_power_state(pdev, PCI_D0);
922     pci_restore_state(pdev);
923     ret = pci_enable_device(pdev);
924     if (ret) {
925         return ret;
926     }
927     pci_set_master(pdev);
928 
929     ret = nouveau_do_resume(drm_dev, false);
930 
931     /* Monitors may have been connected / disconnected during suspend */
932     nouveau_display_hpd_resume(drm_dev);
933 
934     return ret;
935 }
936 
nouveau_pmops_freeze(struct device *dev)937 static int nouveau_pmops_freeze(struct device *dev)
938 {
939     struct pci_dev *pdev = to_pci_dev(dev);
940     struct drm_device *drm_dev = pci_get_drvdata(pdev);
941     return nouveau_do_suspend(drm_dev, false);
942 }
943 
nouveau_pmops_thaw(struct device *dev)944 static int nouveau_pmops_thaw(struct device *dev)
945 {
946     struct pci_dev *pdev = to_pci_dev(dev);
947     struct drm_device *drm_dev = pci_get_drvdata(pdev);
948     return nouveau_do_resume(drm_dev, false);
949 }
950 
nouveau_pmops_runtime(void)951 bool nouveau_pmops_runtime(void)
952 {
953     if (nouveau_runtime_pm == -1) {
954         return nouveau_is_optimus() || nouveau_is_v1_dsm();
955     }
956     return nouveau_runtime_pm == 1;
957 }
958 
nouveau_pmops_runtime_suspend(struct device *dev)959 static int nouveau_pmops_runtime_suspend(struct device *dev)
960 {
961     struct pci_dev *pdev = to_pci_dev(dev);
962     struct drm_device *drm_dev = pci_get_drvdata(pdev);
963     int ret;
964 
965     if (!nouveau_pmops_runtime()) {
966         pm_runtime_forbid(dev);
967         return -EBUSY;
968     }
969 
970     nouveau_switcheroo_optimus_dsm();
971     ret = nouveau_do_suspend(drm_dev, true);
972     pci_save_state(pdev);
973     pci_disable_device(pdev);
974     pci_ignore_hotplug(pdev);
975     pci_set_power_state(pdev, PCI_D3cold);
976     drm_dev->switch_power_state = DRM_SWITCH_POWER_DYNAMIC_OFF;
977     return ret;
978 }
979 
nouveau_pmops_runtime_resume(struct device *dev)980 static int nouveau_pmops_runtime_resume(struct device *dev)
981 {
982     struct pci_dev *pdev = to_pci_dev(dev);
983     struct drm_device *drm_dev = pci_get_drvdata(pdev);
984     struct nouveau_drm *drm = nouveau_drm(drm_dev);
985     struct nvif_device *device = &nouveau_drm(drm_dev)->client.device;
986     int ret;
987 
988     if (!nouveau_pmops_runtime()) {
989         pm_runtime_forbid(dev);
990         return -EBUSY;
991     }
992 
993     pci_set_power_state(pdev, PCI_D0);
994     pci_restore_state(pdev);
995     ret = pci_enable_device(pdev);
996     if (ret) {
997         return ret;
998     }
999     pci_set_master(pdev);
1000 
1001     ret = nouveau_do_resume(drm_dev, true);
1002     if (ret) {
1003         NV_ERROR(drm, "resume failed with: %d\n", ret);
1004         return ret;
1005     }
1006 
1007     /* do magic */
1008     nvif_mask(&device->object, 0x088488, (1 << 0x19), (1 << 0x19));
1009     drm_dev->switch_power_state = DRM_SWITCH_POWER_ON;
1010 
1011     /* Monitors may have been connected / disconnected during suspend */
1012     nouveau_display_hpd_resume(drm_dev);
1013 
1014     return ret;
1015 }
1016 
nouveau_pmops_runtime_idle(struct device *dev)1017 static int nouveau_pmops_runtime_idle(struct device *dev)
1018 {
1019     if (!nouveau_pmops_runtime()) {
1020         pm_runtime_forbid(dev);
1021         return -EBUSY;
1022     }
1023 
1024     pm_runtime_mark_last_busy(dev);
1025     pm_runtime_autosuspend(dev);
1026     /* we don't want the main rpm_idle to call suspend - we want to autosuspend */
1027     return 1;
1028 }
1029 
nouveau_drm_open(struct drm_device *dev, struct drm_file *fpriv)1030 static int nouveau_drm_open(struct drm_device *dev, struct drm_file *fpriv)
1031 {
1032     struct nouveau_drm *drm = nouveau_drm(dev);
1033     struct nouveau_cli *cli;
1034     char name[32], tmpname[TASK_COMM_LEN];
1035     int ret;
1036 
1037     /* need to bring up power immediately if opening device */
1038     ret = pm_runtime_get_sync(dev->dev);
1039     if (ret < 0 && ret != -EACCES) {
1040         pm_runtime_put_autosuspend(dev->dev);
1041         return ret;
1042     }
1043 
1044     get_task_comm(tmpname, current);
1045     ret = snprintf(name, sizeof(name), "%s[%d]", tmpname, pid_nr(fpriv->pid));
1046 
1047     if (!(cli = kzalloc(sizeof(*cli), GFP_KERNEL))) {
1048         ret = -ENOMEM;
1049         goto done;
1050     }
1051 
1052     ret = nouveau_cli_init(drm, name, cli);
1053     if (ret) {
1054         goto done;
1055     }
1056 
1057     cli->base.super = false;
1058 
1059     fpriv->driver_priv = cli;
1060 
1061     mutex_lock(&drm->client.mutex);
1062     list_add(&cli->head, &drm->clients);
1063     mutex_unlock(&drm->client.mutex);
1064 
1065 done:
1066     if (ret && cli) {
1067         nouveau_cli_fini(cli);
1068         kfree(cli);
1069     }
1070 
1071     pm_runtime_mark_last_busy(dev->dev);
1072     pm_runtime_put_autosuspend(dev->dev);
1073     return ret;
1074 }
1075 
nouveau_drm_postclose(struct drm_device *dev, struct drm_file *fpriv)1076 static void nouveau_drm_postclose(struct drm_device *dev, struct drm_file *fpriv)
1077 {
1078     struct nouveau_cli *cli = nouveau_cli(fpriv);
1079     struct nouveau_drm *drm = nouveau_drm(dev);
1080 
1081     pm_runtime_get_sync(dev->dev);
1082 
1083     mutex_lock(&cli->mutex);
1084     if (cli->abi16) {
1085         nouveau_abi16_fini(cli->abi16);
1086     }
1087     mutex_unlock(&cli->mutex);
1088 
1089     mutex_lock(&drm->client.mutex);
1090     list_del(&cli->head);
1091     mutex_unlock(&drm->client.mutex);
1092 
1093     nouveau_cli_fini(cli);
1094     kfree(cli);
1095     pm_runtime_mark_last_busy(dev->dev);
1096     pm_runtime_put_autosuspend(dev->dev);
1097 }
1098 
1099 static const struct drm_ioctl_desc nouveau_ioctls[] = {
1100     DRM_IOCTL_DEF_DRV(NOUVEAU_GETPARAM, nouveau_abi16_ioctl_getparam, DRM_RENDER_ALLOW),
1101     DRM_IOCTL_DEF_DRV(NOUVEAU_SETPARAM, drm_invalid_op, DRM_AUTH | DRM_MASTER | DRM_ROOT_ONLY),
1102     DRM_IOCTL_DEF_DRV(NOUVEAU_CHANNEL_ALLOC, nouveau_abi16_ioctl_channel_alloc, DRM_RENDER_ALLOW),
1103     DRM_IOCTL_DEF_DRV(NOUVEAU_CHANNEL_FREE, nouveau_abi16_ioctl_channel_free, DRM_RENDER_ALLOW),
1104     DRM_IOCTL_DEF_DRV(NOUVEAU_GROBJ_ALLOC, nouveau_abi16_ioctl_grobj_alloc, DRM_RENDER_ALLOW),
1105     DRM_IOCTL_DEF_DRV(NOUVEAU_NOTIFIEROBJ_ALLOC, nouveau_abi16_ioctl_notifierobj_alloc, DRM_RENDER_ALLOW),
1106     DRM_IOCTL_DEF_DRV(NOUVEAU_GPUOBJ_FREE, nouveau_abi16_ioctl_gpuobj_free, DRM_RENDER_ALLOW),
1107     DRM_IOCTL_DEF_DRV(NOUVEAU_SVM_INIT, nouveau_svmm_init, DRM_RENDER_ALLOW),
1108     DRM_IOCTL_DEF_DRV(NOUVEAU_SVM_BIND, nouveau_svmm_bind, DRM_RENDER_ALLOW),
1109     DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_NEW, nouveau_gem_ioctl_new, DRM_RENDER_ALLOW),
1110     DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_PUSHBUF, nouveau_gem_ioctl_pushbuf, DRM_RENDER_ALLOW),
1111     DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_CPU_PREP, nouveau_gem_ioctl_cpu_prep, DRM_RENDER_ALLOW),
1112     DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_CPU_FINI, nouveau_gem_ioctl_cpu_fini, DRM_RENDER_ALLOW),
1113     DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_INFO, nouveau_gem_ioctl_info, DRM_RENDER_ALLOW),
1114 };
1115 
nouveau_drm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)1116 long nouveau_drm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1117 {
1118     struct drm_file *filp = file->private_data;
1119     struct drm_device *dev = filp->minor->dev;
1120     long ret;
1121 
1122     ret = pm_runtime_get_sync(dev->dev);
1123     if (ret < 0 && ret != -EACCES) {
1124         pm_runtime_put_autosuspend(dev->dev);
1125         return ret;
1126     }
1127 
1128     switch (_IOC_NR(cmd) - DRM_COMMAND_BASE) {
1129         case DRM_NOUVEAU_NVIF:
1130             ret = usif_ioctl(filp, (void __user *)arg, _IOC_SIZE(cmd));
1131             break;
1132         default:
1133             ret = drm_ioctl(file, cmd, arg);
1134             break;
1135     }
1136 
1137     pm_runtime_mark_last_busy(dev->dev);
1138     pm_runtime_put_autosuspend(dev->dev);
1139     return ret;
1140 }
1141 
1142 static const struct file_operations nouveau_driver_fops = {
1143     .owner = THIS_MODULE,
1144     .open = drm_open,
1145     .release = drm_release,
1146     .unlocked_ioctl = nouveau_drm_ioctl,
1147     .mmap = nouveau_ttm_mmap,
1148     .poll = drm_poll,
1149     .read = drm_read,
1150 #if defined(CONFIG_COMPAT)
1151     .compat_ioctl = nouveau_compat_ioctl,
1152 #endif
1153     .llseek = noop_llseek,
1154 };
1155 
1156 static struct drm_driver driver_stub = {
1157 #if defined(CONFIG_NOUVEAU_LEGACY_CTX_SUPPORT)
1158     .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_RENDER | DRIVER_KMS_LEGACY_CONTEXT,
1159 #else
1160     .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_RENDER,
1161 #endif
1162     .open = nouveau_drm_open,
1163     .postclose = nouveau_drm_postclose,
1164     .lastclose = nouveau_vga_lastclose,
1165 
1166 #if defined(CONFIG_DEBUG_FS)
1167     .debugfs_init = nouveau_drm_debugfs_init,
1168 #endif
1169 
1170     .ioctls = nouveau_ioctls,
1171     .num_ioctls = ARRAY_SIZE(nouveau_ioctls),
1172     .fops = &nouveau_driver_fops,
1173 
1174     .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
1175     .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
1176     .gem_prime_pin = nouveau_gem_prime_pin,
1177     .gem_prime_unpin = nouveau_gem_prime_unpin,
1178     .gem_prime_get_sg_table = nouveau_gem_prime_get_sg_table,
1179     .gem_prime_import_sg_table = nouveau_gem_prime_import_sg_table,
1180     .gem_prime_vmap = nouveau_gem_prime_vmap,
1181     .gem_prime_vunmap = nouveau_gem_prime_vunmap,
1182 
1183     .gem_free_object_unlocked = nouveau_gem_object_del,
1184     .gem_open_object = nouveau_gem_object_open,
1185     .gem_close_object = nouveau_gem_object_close,
1186 
1187     .dumb_create = nouveau_display_dumb_create,
1188     .dumb_map_offset = nouveau_display_dumb_map_offset,
1189 
1190     .name = DRIVER_NAME,
1191     .desc = DRIVER_DESC,
1192 #ifdef GIT_REVISION
1193     .date = GIT_REVISION,
1194 #else
1195     .date = DRIVER_DATE,
1196 #endif
1197     .major = DRIVER_MAJOR,
1198     .minor = DRIVER_MINOR,
1199     .patchlevel = DRIVER_PATCHLEVEL,
1200 };
1201 
1202 static struct pci_device_id nouveau_drm_pci_table[] = {
1203     {
1204         PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_ANY_ID),
1205         .class = PCI_BASE_CLASS_DISPLAY << 16,
1206         .class_mask = 0xff << 16,
1207     },
1208     {
1209         PCI_DEVICE(PCI_VENDOR_ID_NVIDIA_SGS, PCI_ANY_ID),
1210         .class = PCI_BASE_CLASS_DISPLAY << 16,
1211         .class_mask = 0xff << 16,
1212     },
1213     {}
1214 };
1215 
nouveau_display_options(void)1216 static void nouveau_display_options(void)
1217 {
1218     DRM_DEBUG_DRIVER("Loading Nouveau with parameters:\n");
1219 
1220     DRM_DEBUG_DRIVER("... tv_disable   : %d\n", nouveau_tv_disable);
1221     DRM_DEBUG_DRIVER("... ignorelid    : %d\n", nouveau_ignorelid);
1222     DRM_DEBUG_DRIVER("... duallink     : %d\n", nouveau_duallink);
1223     DRM_DEBUG_DRIVER("... nofbaccel    : %d\n", nouveau_nofbaccel);
1224     DRM_DEBUG_DRIVER("... config       : %s\n", nouveau_config);
1225     DRM_DEBUG_DRIVER("... debug        : %s\n", nouveau_debug);
1226     DRM_DEBUG_DRIVER("... noaccel      : %d\n", nouveau_noaccel);
1227     DRM_DEBUG_DRIVER("... modeset      : %d\n", nouveau_modeset);
1228     DRM_DEBUG_DRIVER("... runpm        : %d\n", nouveau_runtime_pm);
1229     DRM_DEBUG_DRIVER("... vram_pushbuf : %d\n", nouveau_vram_pushbuf);
1230     DRM_DEBUG_DRIVER("... hdmimhz      : %d\n", nouveau_hdmimhz);
1231 }
1232 
1233 static const struct dev_pm_ops nouveau_pm_ops = {
1234     .suspend = nouveau_pmops_suspend,
1235     .resume = nouveau_pmops_resume,
1236     .freeze = nouveau_pmops_freeze,
1237     .thaw = nouveau_pmops_thaw,
1238     .poweroff = nouveau_pmops_freeze,
1239     .restore = nouveau_pmops_resume,
1240     .runtime_suspend = nouveau_pmops_runtime_suspend,
1241     .runtime_resume = nouveau_pmops_runtime_resume,
1242     .runtime_idle = nouveau_pmops_runtime_idle,
1243 };
1244 
1245 static struct pci_driver nouveau_drm_pci_driver = {
1246     .name = "nouveau",
1247     .id_table = nouveau_drm_pci_table,
1248     .probe = nouveau_drm_probe,
1249     .remove = nouveau_drm_remove,
1250     .driver.pm = &nouveau_pm_ops,
1251 };
1252 
nouveau_platform_device_create(const struct nvkm_device_tegra_func *func, struct platform_device *pdev, struct nvkm_device **pdevice)1253 struct drm_device *nouveau_platform_device_create(const struct nvkm_device_tegra_func *func,
1254                                                   struct platform_device *pdev, struct nvkm_device **pdevice)
1255 {
1256     struct drm_device *drm;
1257     int err;
1258 
1259     err = nvkm_device_tegra_new(func, pdev, nouveau_config, nouveau_debug, true, true, ~0ULL, pdevice);
1260     if (err) {
1261         goto err_free;
1262     }
1263 
1264     drm = drm_dev_alloc(&driver_platform, &pdev->dev);
1265     if (IS_ERR(drm)) {
1266         err = PTR_ERR(drm);
1267         goto err_free;
1268     }
1269 
1270     err = nouveau_drm_device_init(drm);
1271     if (err) {
1272         goto err_put;
1273     }
1274 
1275     platform_set_drvdata(pdev, drm);
1276 
1277     return drm;
1278 
1279 err_put:
1280     drm_dev_put(drm);
1281 err_free:
1282     nvkm_device_del(pdevice);
1283 
1284     return ERR_PTR(err);
1285 }
1286 
nouveau_drm_init(void)1287 static int __init nouveau_drm_init(void)
1288 {
1289     driver_pci = driver_stub;
1290     driver_platform = driver_stub;
1291 
1292     nouveau_display_options();
1293 
1294     if (nouveau_modeset == -1) {
1295         if (vgacon_text_force()) {
1296             nouveau_modeset = 0;
1297         }
1298     }
1299 
1300     if (!nouveau_modeset) {
1301         return 0;
1302     }
1303 
1304 #ifdef CONFIG_NOUVEAU_PLATFORM_DRIVER
1305     platform_driver_register(&nouveau_platform_driver);
1306 #endif
1307 
1308     nouveau_register_dsm_handler();
1309     nouveau_backlight_ctor();
1310 
1311 #ifdef CONFIG_PCI
1312     return pci_register_driver(&nouveau_drm_pci_driver);
1313 #else
1314     return 0;
1315 #endif
1316 }
1317 
nouveau_drm_exit(void)1318 static void __exit nouveau_drm_exit(void)
1319 {
1320     if (!nouveau_modeset) {
1321         return;
1322     }
1323 
1324 #ifdef CONFIG_PCI
1325     pci_unregister_driver(&nouveau_drm_pci_driver);
1326 #endif
1327     nouveau_backlight_dtor();
1328     nouveau_unregister_dsm_handler();
1329 
1330 #ifdef CONFIG_NOUVEAU_PLATFORM_DRIVER
1331     platform_driver_unregister(&nouveau_platform_driver);
1332 #endif
1333     if (IS_ENABLED(CONFIG_DRM_NOUVEAU_SVM)) {
1334         mmu_notifier_synchronize();
1335     }
1336 }
1337 
1338 module_init(nouveau_drm_init);
1339 module_exit(nouveau_drm_exit);
1340 
1341 MODULE_DEVICE_TABLE(pci, nouveau_drm_pci_table);
1342 MODULE_AUTHOR(DRIVER_AUTHOR);
1343 MODULE_DESCRIPTION(DRIVER_DESC);
1344 MODULE_LICENSE("GPL and additional rights");
1345