drm/nouveau/mc: handle irq-related setup ourselves
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / drm / nouveau / nouveau_drm.c
1 /*
2  * Copyright 2012 Red Hat Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * Authors: Ben Skeggs
23  */
24
25 #include <linux/console.h>
26 #include <linux/module.h>
27 #include <linux/pci.h>
28
29 #include <core/device.h>
30 #include <core/client.h>
31 #include <core/gpuobj.h>
32 #include <core/class.h>
33
34 #include <subdev/device.h>
35 #include <subdev/vm.h>
36
37 #include <engine/disp.h>
38
39 #include "nouveau_drm.h"
40 #include "nouveau_dma.h"
41 #include "nouveau_ttm.h"
42 #include "nouveau_gem.h"
43 #include "nouveau_agp.h"
44 #include "nouveau_vga.h"
45 #include "nouveau_pm.h"
46 #include "nouveau_acpi.h"
47 #include "nouveau_bios.h"
48 #include "nouveau_ioctl.h"
49 #include "nouveau_abi16.h"
50 #include "nouveau_fbcon.h"
51 #include "nouveau_fence.h"
52 #include "nouveau_debugfs.h"
53
54 MODULE_PARM_DESC(config, "option string to pass to driver core");
55 static char *nouveau_config;
56 module_param_named(config, nouveau_config, charp, 0400);
57
58 MODULE_PARM_DESC(debug, "debug string to pass to driver core");
59 static char *nouveau_debug;
60 module_param_named(debug, nouveau_debug, charp, 0400);
61
62 MODULE_PARM_DESC(noaccel, "disable kernel/abi16 acceleration");
63 static int nouveau_noaccel = 0;
64 module_param_named(noaccel, nouveau_noaccel, int, 0400);
65
66 MODULE_PARM_DESC(modeset, "enable driver (default: auto, "
67                           "0 = disabled, 1 = enabled, 2 = headless)");
68 int nouveau_modeset = -1;
69 module_param_named(modeset, nouveau_modeset, int, 0400);
70
71 static struct drm_driver driver;
72
73 static int
74 nouveau_drm_vblank_handler(struct nouveau_eventh *event, int head)
75 {
76         struct nouveau_drm *drm =
77                 container_of(event, struct nouveau_drm, vblank[head]);
78         drm_handle_vblank(drm->dev, head);
79         return NVKM_EVENT_KEEP;
80 }
81
82 static int
83 nouveau_drm_vblank_enable(struct drm_device *dev, int head)
84 {
85         struct nouveau_drm *drm = nouveau_drm(dev);
86         struct nouveau_disp *pdisp = nouveau_disp(drm->device);
87
88         if (WARN_ON_ONCE(head > ARRAY_SIZE(drm->vblank)))
89                 return -EIO;
90         WARN_ON_ONCE(drm->vblank[head].func);
91         drm->vblank[head].func = nouveau_drm_vblank_handler;
92         nouveau_event_get(pdisp->vblank, head, &drm->vblank[head]);
93         return 0;
94 }
95
96 static void
97 nouveau_drm_vblank_disable(struct drm_device *dev, int head)
98 {
99         struct nouveau_drm *drm = nouveau_drm(dev);
100         struct nouveau_disp *pdisp = nouveau_disp(drm->device);
101         if (drm->vblank[head].func)
102                 nouveau_event_put(pdisp->vblank, head, &drm->vblank[head]);
103         else
104                 WARN_ON_ONCE(1);
105         drm->vblank[head].func = NULL;
106 }
107
108 static u64
109 nouveau_name(struct pci_dev *pdev)
110 {
111         u64 name = (u64)pci_domain_nr(pdev->bus) << 32;
112         name |= pdev->bus->number << 16;
113         name |= PCI_SLOT(pdev->devfn) << 8;
114         return name | PCI_FUNC(pdev->devfn);
115 }
116
117 static int
118 nouveau_cli_create(struct pci_dev *pdev, const char *name,
119                    int size, void **pcli)
120 {
121         struct nouveau_cli *cli;
122         int ret;
123
124         *pcli = NULL;
125         ret = nouveau_client_create_(name, nouveau_name(pdev), nouveau_config,
126                                      nouveau_debug, size, pcli);
127         cli = *pcli;
128         if (ret) {
129                 if (cli)
130                         nouveau_client_destroy(&cli->base);
131                 *pcli = NULL;
132                 return ret;
133         }
134
135         mutex_init(&cli->mutex);
136         return 0;
137 }
138
139 static void
140 nouveau_cli_destroy(struct nouveau_cli *cli)
141 {
142         struct nouveau_object *client = nv_object(cli);
143         nouveau_vm_ref(NULL, &cli->base.vm, NULL);
144         nouveau_client_fini(&cli->base, false);
145         atomic_set(&client->refcount, 1);
146         nouveau_object_ref(NULL, &client);
147 }
148
149 static void
150 nouveau_accel_fini(struct nouveau_drm *drm)
151 {
152         nouveau_gpuobj_ref(NULL, &drm->notify);
153         nouveau_channel_del(&drm->channel);
154         nouveau_channel_del(&drm->cechan);
155         if (drm->fence)
156                 nouveau_fence(drm)->dtor(drm);
157 }
158
159 static void
160 nouveau_accel_init(struct nouveau_drm *drm)
161 {
162         struct nouveau_device *device = nv_device(drm->device);
163         struct nouveau_object *object;
164         u32 arg0, arg1;
165         int ret;
166
167         if (nouveau_noaccel)
168                 return;
169
170         /* initialise synchronisation routines */
171         if      (device->card_type < NV_10) ret = nv04_fence_create(drm);
172         else if (device->chipset   <  0x17) ret = nv10_fence_create(drm);
173         else if (device->card_type < NV_50) ret = nv17_fence_create(drm);
174         else if (device->chipset   <  0x84) ret = nv50_fence_create(drm);
175         else if (device->card_type < NV_C0) ret = nv84_fence_create(drm);
176         else                                ret = nvc0_fence_create(drm);
177         if (ret) {
178                 NV_ERROR(drm, "failed to initialise sync subsystem, %d\n", ret);
179                 nouveau_accel_fini(drm);
180                 return;
181         }
182
183         if (device->card_type >= NV_E0) {
184                 ret = nouveau_channel_new(drm, &drm->client, NVDRM_DEVICE,
185                                           NVDRM_CHAN + 1,
186                                           NVE0_CHANNEL_IND_ENGINE_CE0 |
187                                           NVE0_CHANNEL_IND_ENGINE_CE1, 0,
188                                           &drm->cechan);
189                 if (ret)
190                         NV_ERROR(drm, "failed to create ce channel, %d\n", ret);
191
192                 arg0 = NVE0_CHANNEL_IND_ENGINE_GR;
193                 arg1 = 1;
194         } else {
195                 arg0 = NvDmaFB;
196                 arg1 = NvDmaTT;
197         }
198
199         ret = nouveau_channel_new(drm, &drm->client, NVDRM_DEVICE, NVDRM_CHAN,
200                                   arg0, arg1, &drm->channel);
201         if (ret) {
202                 NV_ERROR(drm, "failed to create kernel channel, %d\n", ret);
203                 nouveau_accel_fini(drm);
204                 return;
205         }
206
207         if (device->card_type < NV_C0) {
208                 ret = nouveau_gpuobj_new(drm->device, NULL, 32, 0, 0,
209                                         &drm->notify);
210                 if (ret) {
211                         NV_ERROR(drm, "failed to allocate notifier, %d\n", ret);
212                         nouveau_accel_fini(drm);
213                         return;
214                 }
215
216                 ret = nouveau_object_new(nv_object(drm),
217                                          drm->channel->handle, NvNotify0,
218                                          0x003d, &(struct nv_dma_class) {
219                                                 .flags = NV_DMA_TARGET_VRAM |
220                                                          NV_DMA_ACCESS_RDWR,
221                                                 .start = drm->notify->addr,
222                                                 .limit = drm->notify->addr + 31
223                                                 }, sizeof(struct nv_dma_class),
224                                          &object);
225                 if (ret) {
226                         nouveau_accel_fini(drm);
227                         return;
228                 }
229         }
230
231
232         nouveau_bo_move_init(drm);
233 }
234
235 static int nouveau_drm_probe(struct pci_dev *pdev,
236                              const struct pci_device_id *pent)
237 {
238         struct nouveau_device *device;
239         struct apertures_struct *aper;
240         bool boot = false;
241         int ret;
242
243         /* remove conflicting drivers (vesafb, efifb etc) */
244         aper = alloc_apertures(3);
245         if (!aper)
246                 return -ENOMEM;
247
248         aper->ranges[0].base = pci_resource_start(pdev, 1);
249         aper->ranges[0].size = pci_resource_len(pdev, 1);
250         aper->count = 1;
251
252         if (pci_resource_len(pdev, 2)) {
253                 aper->ranges[aper->count].base = pci_resource_start(pdev, 2);
254                 aper->ranges[aper->count].size = pci_resource_len(pdev, 2);
255                 aper->count++;
256         }
257
258         if (pci_resource_len(pdev, 3)) {
259                 aper->ranges[aper->count].base = pci_resource_start(pdev, 3);
260                 aper->ranges[aper->count].size = pci_resource_len(pdev, 3);
261                 aper->count++;
262         }
263
264 #ifdef CONFIG_X86
265         boot = pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW;
266 #endif
267         remove_conflicting_framebuffers(aper, "nouveaufb", boot);
268         kfree(aper);
269
270         ret = nouveau_device_create(pdev, nouveau_name(pdev), pci_name(pdev),
271                                     nouveau_config, nouveau_debug, &device);
272         if (ret)
273                 return ret;
274
275         pci_set_master(pdev);
276
277         ret = drm_get_pci_dev(pdev, pent, &driver);
278         if (ret) {
279                 nouveau_object_ref(NULL, (struct nouveau_object **)&device);
280                 return ret;
281         }
282
283         return 0;
284 }
285
286 static struct lock_class_key drm_client_lock_class_key;
287
288 static int
289 nouveau_drm_load(struct drm_device *dev, unsigned long flags)
290 {
291         struct pci_dev *pdev = dev->pdev;
292         struct nouveau_device *device;
293         struct nouveau_drm *drm;
294         int ret;
295
296         ret = nouveau_cli_create(pdev, "DRM", sizeof(*drm), (void**)&drm);
297         if (ret)
298                 return ret;
299         lockdep_set_class(&drm->client.mutex, &drm_client_lock_class_key);
300
301         dev->dev_private = drm;
302         drm->dev = dev;
303
304         INIT_LIST_HEAD(&drm->clients);
305         spin_lock_init(&drm->tile.lock);
306
307         /* make sure AGP controller is in a consistent state before we
308          * (possibly) execute vbios init tables (see nouveau_agp.h)
309          */
310         if (drm_pci_device_is_agp(dev) && dev->agp) {
311                 /* dummy device object, doesn't init anything, but allows
312                  * agp code access to registers
313                  */
314                 ret = nouveau_object_new(nv_object(drm), NVDRM_CLIENT,
315                                          NVDRM_DEVICE, 0x0080,
316                                          &(struct nv_device_class) {
317                                                 .device = ~0,
318                                                 .disable =
319                                                  ~(NV_DEVICE_DISABLE_MMIO |
320                                                    NV_DEVICE_DISABLE_IDENTIFY),
321                                                 .debug0 = ~0,
322                                          }, sizeof(struct nv_device_class),
323                                          &drm->device);
324                 if (ret)
325                         goto fail_device;
326
327                 nouveau_agp_reset(drm);
328                 nouveau_object_del(nv_object(drm), NVDRM_CLIENT, NVDRM_DEVICE);
329         }
330
331         ret = nouveau_object_new(nv_object(drm), NVDRM_CLIENT, NVDRM_DEVICE,
332                                  0x0080, &(struct nv_device_class) {
333                                         .device = ~0,
334                                         .disable = 0,
335                                         .debug0 = 0,
336                                  }, sizeof(struct nv_device_class),
337                                  &drm->device);
338         if (ret)
339                 goto fail_device;
340
341         /* workaround an odd issue on nvc1 by disabling the device's
342          * nosnoop capability.  hopefully won't cause issues until a
343          * better fix is found - assuming there is one...
344          */
345         device = nv_device(drm->device);
346         if (nv_device(drm->device)->chipset == 0xc1)
347                 nv_mask(device, 0x00088080, 0x00000800, 0x00000000);
348
349         nouveau_vga_init(drm);
350         nouveau_agp_init(drm);
351
352         if (device->card_type >= NV_50) {
353                 ret = nouveau_vm_new(nv_device(drm->device), 0, (1ULL << 40),
354                                      0x1000, &drm->client.base.vm);
355                 if (ret)
356                         goto fail_device;
357         }
358
359         ret = nouveau_ttm_init(drm);
360         if (ret)
361                 goto fail_ttm;
362
363         ret = nouveau_bios_init(dev);
364         if (ret)
365                 goto fail_bios;
366
367         ret = nouveau_display_create(dev);
368         if (ret)
369                 goto fail_dispctor;
370
371         if (dev->mode_config.num_crtc) {
372                 ret = nouveau_display_init(dev);
373                 if (ret)
374                         goto fail_dispinit;
375         }
376
377         nouveau_pm_init(dev);
378
379         nouveau_accel_init(drm);
380         nouveau_fbcon_init(dev);
381         return 0;
382
383 fail_dispinit:
384         nouveau_display_destroy(dev);
385 fail_dispctor:
386         nouveau_bios_takedown(dev);
387 fail_bios:
388         nouveau_ttm_fini(drm);
389 fail_ttm:
390         nouveau_agp_fini(drm);
391         nouveau_vga_fini(drm);
392 fail_device:
393         nouveau_cli_destroy(&drm->client);
394         return ret;
395 }
396
397 static int
398 nouveau_drm_unload(struct drm_device *dev)
399 {
400         struct nouveau_drm *drm = nouveau_drm(dev);
401
402         nouveau_fbcon_fini(dev);
403         nouveau_accel_fini(drm);
404
405         nouveau_pm_fini(dev);
406
407         if (dev->mode_config.num_crtc)
408                 nouveau_display_fini(dev);
409         nouveau_display_destroy(dev);
410
411         nouveau_bios_takedown(dev);
412
413         nouveau_ttm_fini(drm);
414         nouveau_agp_fini(drm);
415         nouveau_vga_fini(drm);
416
417         nouveau_cli_destroy(&drm->client);
418         return 0;
419 }
420
421 static void
422 nouveau_drm_remove(struct pci_dev *pdev)
423 {
424         struct drm_device *dev = pci_get_drvdata(pdev);
425         struct nouveau_drm *drm = nouveau_drm(dev);
426         struct nouveau_object *device;
427
428         device = drm->client.base.device;
429         drm_put_dev(dev);
430
431         nouveau_object_ref(NULL, &device);
432         nouveau_object_debug();
433 }
434
435 static int
436 nouveau_do_suspend(struct drm_device *dev)
437 {
438         struct nouveau_drm *drm = nouveau_drm(dev);
439         struct nouveau_cli *cli;
440         int ret;
441
442         if (dev->mode_config.num_crtc) {
443                 NV_INFO(drm, "suspending fbcon...\n");
444                 nouveau_fbcon_set_suspend(dev, 1);
445
446                 NV_INFO(drm, "suspending display...\n");
447                 ret = nouveau_display_suspend(dev);
448                 if (ret)
449                         return ret;
450         }
451
452         NV_INFO(drm, "evicting buffers...\n");
453         ttm_bo_evict_mm(&drm->ttm.bdev, TTM_PL_VRAM);
454
455         if (drm->fence && nouveau_fence(drm)->suspend) {
456                 if (!nouveau_fence(drm)->suspend(drm))
457                         return -ENOMEM;
458         }
459
460         NV_INFO(drm, "suspending client object trees...\n");
461         list_for_each_entry(cli, &drm->clients, head) {
462                 ret = nouveau_client_fini(&cli->base, true);
463                 if (ret)
464                         goto fail_client;
465         }
466
467         ret = nouveau_client_fini(&drm->client.base, true);
468         if (ret)
469                 goto fail_client;
470
471         nouveau_agp_fini(drm);
472         return 0;
473
474 fail_client:
475         list_for_each_entry_continue_reverse(cli, &drm->clients, head) {
476                 nouveau_client_init(&cli->base);
477         }
478
479         if (dev->mode_config.num_crtc) {
480                 NV_INFO(drm, "resuming display...\n");
481                 nouveau_display_resume(dev);
482         }
483         return ret;
484 }
485
486 int nouveau_pmops_suspend(struct device *dev)
487 {
488         struct pci_dev *pdev = to_pci_dev(dev);
489         struct drm_device *drm_dev = pci_get_drvdata(pdev);
490         int ret;
491
492         if (drm_dev->switch_power_state == DRM_SWITCH_POWER_OFF)
493                 return 0;
494
495         ret = nouveau_do_suspend(drm_dev);
496         if (ret)
497                 return ret;
498
499         pci_save_state(pdev);
500         pci_disable_device(pdev);
501         pci_set_power_state(pdev, PCI_D3hot);
502
503         return 0;
504 }
505
506 static int
507 nouveau_do_resume(struct drm_device *dev)
508 {
509         struct nouveau_drm *drm = nouveau_drm(dev);
510         struct nouveau_cli *cli;
511
512         NV_INFO(drm, "re-enabling device...\n");
513
514         nouveau_agp_reset(drm);
515
516         NV_INFO(drm, "resuming client object trees...\n");
517         nouveau_client_init(&drm->client.base);
518         nouveau_agp_init(drm);
519
520         list_for_each_entry(cli, &drm->clients, head) {
521                 nouveau_client_init(&cli->base);
522         }
523
524         if (drm->fence && nouveau_fence(drm)->resume)
525                 nouveau_fence(drm)->resume(drm);
526
527         nouveau_run_vbios_init(dev);
528         nouveau_pm_resume(dev);
529
530         if (dev->mode_config.num_crtc) {
531                 NV_INFO(drm, "resuming display...\n");
532                 nouveau_display_resume(dev);
533         }
534         return 0;
535 }
536
537 int nouveau_pmops_resume(struct device *dev)
538 {
539         struct pci_dev *pdev = to_pci_dev(dev);
540         struct drm_device *drm_dev = pci_get_drvdata(pdev);
541         int ret;
542
543         if (drm_dev->switch_power_state == DRM_SWITCH_POWER_OFF)
544                 return 0;
545
546         pci_set_power_state(pdev, PCI_D0);
547         pci_restore_state(pdev);
548         ret = pci_enable_device(pdev);
549         if (ret)
550                 return ret;
551         pci_set_master(pdev);
552
553         return nouveau_do_resume(drm_dev);
554 }
555
556 static int nouveau_pmops_freeze(struct device *dev)
557 {
558         struct pci_dev *pdev = to_pci_dev(dev);
559         struct drm_device *drm_dev = pci_get_drvdata(pdev);
560
561         return nouveau_do_suspend(drm_dev);
562 }
563
564 static int nouveau_pmops_thaw(struct device *dev)
565 {
566         struct pci_dev *pdev = to_pci_dev(dev);
567         struct drm_device *drm_dev = pci_get_drvdata(pdev);
568
569         return nouveau_do_resume(drm_dev);
570 }
571
572
573 static int
574 nouveau_drm_open(struct drm_device *dev, struct drm_file *fpriv)
575 {
576         struct pci_dev *pdev = dev->pdev;
577         struct nouveau_drm *drm = nouveau_drm(dev);
578         struct nouveau_cli *cli;
579         char name[32], tmpname[TASK_COMM_LEN];
580         int ret;
581
582         get_task_comm(tmpname, current);
583         snprintf(name, sizeof(name), "%s[%d]", tmpname, pid_nr(fpriv->pid));
584
585         ret = nouveau_cli_create(pdev, name, sizeof(*cli), (void **)&cli);
586         if (ret)
587                 return ret;
588
589         if (nv_device(drm->device)->card_type >= NV_50) {
590                 ret = nouveau_vm_new(nv_device(drm->device), 0, (1ULL << 40),
591                                      0x1000, &cli->base.vm);
592                 if (ret) {
593                         nouveau_cli_destroy(cli);
594                         return ret;
595                 }
596         }
597
598         fpriv->driver_priv = cli;
599
600         mutex_lock(&drm->client.mutex);
601         list_add(&cli->head, &drm->clients);
602         mutex_unlock(&drm->client.mutex);
603         return 0;
604 }
605
606 static void
607 nouveau_drm_preclose(struct drm_device *dev, struct drm_file *fpriv)
608 {
609         struct nouveau_cli *cli = nouveau_cli(fpriv);
610         struct nouveau_drm *drm = nouveau_drm(dev);
611
612         if (cli->abi16)
613                 nouveau_abi16_fini(cli->abi16);
614
615         mutex_lock(&drm->client.mutex);
616         list_del(&cli->head);
617         mutex_unlock(&drm->client.mutex);
618 }
619
620 static void
621 nouveau_drm_postclose(struct drm_device *dev, struct drm_file *fpriv)
622 {
623         struct nouveau_cli *cli = nouveau_cli(fpriv);
624         nouveau_cli_destroy(cli);
625 }
626
627 static struct drm_ioctl_desc
628 nouveau_ioctls[] = {
629         DRM_IOCTL_DEF_DRV(NOUVEAU_GETPARAM, nouveau_abi16_ioctl_getparam, DRM_UNLOCKED|DRM_AUTH),
630         DRM_IOCTL_DEF_DRV(NOUVEAU_SETPARAM, nouveau_abi16_ioctl_setparam, DRM_UNLOCKED|DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
631         DRM_IOCTL_DEF_DRV(NOUVEAU_CHANNEL_ALLOC, nouveau_abi16_ioctl_channel_alloc, DRM_UNLOCKED|DRM_AUTH),
632         DRM_IOCTL_DEF_DRV(NOUVEAU_CHANNEL_FREE, nouveau_abi16_ioctl_channel_free, DRM_UNLOCKED|DRM_AUTH),
633         DRM_IOCTL_DEF_DRV(NOUVEAU_GROBJ_ALLOC, nouveau_abi16_ioctl_grobj_alloc, DRM_UNLOCKED|DRM_AUTH),
634         DRM_IOCTL_DEF_DRV(NOUVEAU_NOTIFIEROBJ_ALLOC, nouveau_abi16_ioctl_notifierobj_alloc, DRM_UNLOCKED|DRM_AUTH),
635         DRM_IOCTL_DEF_DRV(NOUVEAU_GPUOBJ_FREE, nouveau_abi16_ioctl_gpuobj_free, DRM_UNLOCKED|DRM_AUTH),
636         DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_NEW, nouveau_gem_ioctl_new, DRM_UNLOCKED|DRM_AUTH),
637         DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_PUSHBUF, nouveau_gem_ioctl_pushbuf, DRM_UNLOCKED|DRM_AUTH),
638         DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_CPU_PREP, nouveau_gem_ioctl_cpu_prep, DRM_UNLOCKED|DRM_AUTH),
639         DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_CPU_FINI, nouveau_gem_ioctl_cpu_fini, DRM_UNLOCKED|DRM_AUTH),
640         DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_INFO, nouveau_gem_ioctl_info, DRM_UNLOCKED|DRM_AUTH),
641 };
642
643 static const struct file_operations
644 nouveau_driver_fops = {
645         .owner = THIS_MODULE,
646         .open = drm_open,
647         .release = drm_release,
648         .unlocked_ioctl = drm_ioctl,
649         .mmap = nouveau_ttm_mmap,
650         .poll = drm_poll,
651         .fasync = drm_fasync,
652         .read = drm_read,
653 #if defined(CONFIG_COMPAT)
654         .compat_ioctl = nouveau_compat_ioctl,
655 #endif
656         .llseek = noop_llseek,
657 };
658
659 static struct drm_driver
660 driver = {
661         .driver_features =
662                 DRIVER_USE_AGP | DRIVER_PCI_DMA | DRIVER_SG |
663                 DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME,
664
665         .load = nouveau_drm_load,
666         .unload = nouveau_drm_unload,
667         .open = nouveau_drm_open,
668         .preclose = nouveau_drm_preclose,
669         .postclose = nouveau_drm_postclose,
670         .lastclose = nouveau_vga_lastclose,
671
672 #if defined(CONFIG_DEBUG_FS)
673         .debugfs_init = nouveau_debugfs_init,
674         .debugfs_cleanup = nouveau_debugfs_takedown,
675 #endif
676
677         .get_vblank_counter = drm_vblank_count,
678         .enable_vblank = nouveau_drm_vblank_enable,
679         .disable_vblank = nouveau_drm_vblank_disable,
680
681         .ioctls = nouveau_ioctls,
682         .fops = &nouveau_driver_fops,
683
684         .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
685         .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
686         .gem_prime_export = drm_gem_prime_export,
687         .gem_prime_import = drm_gem_prime_import,
688         .gem_prime_pin = nouveau_gem_prime_pin,
689         .gem_prime_get_sg_table = nouveau_gem_prime_get_sg_table,
690         .gem_prime_import_sg_table = nouveau_gem_prime_import_sg_table,
691         .gem_prime_vmap = nouveau_gem_prime_vmap,
692         .gem_prime_vunmap = nouveau_gem_prime_vunmap,
693
694         .gem_init_object = nouveau_gem_object_new,
695         .gem_free_object = nouveau_gem_object_del,
696         .gem_open_object = nouveau_gem_object_open,
697         .gem_close_object = nouveau_gem_object_close,
698
699         .dumb_create = nouveau_display_dumb_create,
700         .dumb_map_offset = nouveau_display_dumb_map_offset,
701         .dumb_destroy = nouveau_display_dumb_destroy,
702
703         .name = DRIVER_NAME,
704         .desc = DRIVER_DESC,
705 #ifdef GIT_REVISION
706         .date = GIT_REVISION,
707 #else
708         .date = DRIVER_DATE,
709 #endif
710         .major = DRIVER_MAJOR,
711         .minor = DRIVER_MINOR,
712         .patchlevel = DRIVER_PATCHLEVEL,
713 };
714
715 static struct pci_device_id
716 nouveau_drm_pci_table[] = {
717         {
718                 PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_ANY_ID),
719                 .class = PCI_BASE_CLASS_DISPLAY << 16,
720                 .class_mask  = 0xff << 16,
721         },
722         {
723                 PCI_DEVICE(PCI_VENDOR_ID_NVIDIA_SGS, PCI_ANY_ID),
724                 .class = PCI_BASE_CLASS_DISPLAY << 16,
725                 .class_mask  = 0xff << 16,
726         },
727         {}
728 };
729
730 static const struct dev_pm_ops nouveau_pm_ops = {
731         .suspend = nouveau_pmops_suspend,
732         .resume = nouveau_pmops_resume,
733         .freeze = nouveau_pmops_freeze,
734         .thaw = nouveau_pmops_thaw,
735         .poweroff = nouveau_pmops_freeze,
736         .restore = nouveau_pmops_resume,
737 };
738
739 static struct pci_driver
740 nouveau_drm_pci_driver = {
741         .name = "nouveau",
742         .id_table = nouveau_drm_pci_table,
743         .probe = nouveau_drm_probe,
744         .remove = nouveau_drm_remove,
745         .driver.pm = &nouveau_pm_ops,
746 };
747
748 static int __init
749 nouveau_drm_init(void)
750 {
751         driver.num_ioctls = ARRAY_SIZE(nouveau_ioctls);
752
753         if (nouveau_modeset == -1) {
754 #ifdef CONFIG_VGA_CONSOLE
755                 if (vgacon_text_force())
756                         nouveau_modeset = 0;
757 #endif
758         }
759
760         if (!nouveau_modeset)
761                 return 0;
762
763         nouveau_register_dsm_handler();
764         return drm_pci_init(&driver, &nouveau_drm_pci_driver);
765 }
766
767 static void __exit
768 nouveau_drm_exit(void)
769 {
770         if (!nouveau_modeset)
771                 return;
772
773         drm_pci_exit(&driver, &nouveau_drm_pci_driver);
774         nouveau_unregister_dsm_handler();
775 }
776
777 module_init(nouveau_drm_init);
778 module_exit(nouveau_drm_exit);
779
780 MODULE_DEVICE_TABLE(pci, nouveau_drm_pci_table);
781 MODULE_AUTHOR(DRIVER_AUTHOR);
782 MODULE_DESCRIPTION(DRIVER_DESC);
783 MODULE_LICENSE("GPL and additional rights");