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