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_enable(struct drm_device *dev, int head)
76 {
77 	struct nouveau_drm *drm = nouveau_drm(dev);
78 	struct nouveau_disp *pdisp = nouveau_disp(drm->device);
79 	nouveau_event_get(pdisp->vblank, head, &drm->vblank);
80 	return 0;
81 }
82 
83 static void
84 nouveau_drm_vblank_disable(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 	nouveau_event_put(pdisp->vblank, head, &drm->vblank);
89 }
90 
91 static int
92 nouveau_drm_vblank_handler(struct nouveau_eventh *event, int head)
93 {
94 	struct nouveau_drm *drm =
95 		container_of(event, struct nouveau_drm, vblank);
96 	drm_handle_vblank(drm->dev, head);
97 	return NVKM_EVENT_KEEP;
98 }
99 
100 static u64
101 nouveau_name(struct pci_dev *pdev)
102 {
103 	u64 name = (u64)pci_domain_nr(pdev->bus) << 32;
104 	name |= pdev->bus->number << 16;
105 	name |= PCI_SLOT(pdev->devfn) << 8;
106 	return name | PCI_FUNC(pdev->devfn);
107 }
108 
109 static int
110 nouveau_cli_create(struct pci_dev *pdev, const char *name,
111 		   int size, void **pcli)
112 {
113 	struct nouveau_cli *cli;
114 	int ret;
115 
116 	*pcli = NULL;
117 	ret = nouveau_client_create_(name, nouveau_name(pdev), nouveau_config,
118 				     nouveau_debug, size, pcli);
119 	cli = *pcli;
120 	if (ret) {
121 		if (cli)
122 			nouveau_client_destroy(&cli->base);
123 		*pcli = NULL;
124 		return ret;
125 	}
126 
127 	mutex_init(&cli->mutex);
128 	return 0;
129 }
130 
131 static void
132 nouveau_cli_destroy(struct nouveau_cli *cli)
133 {
134 	struct nouveau_object *client = nv_object(cli);
135 	nouveau_vm_ref(NULL, &cli->base.vm, NULL);
136 	nouveau_client_fini(&cli->base, false);
137 	atomic_set(&client->refcount, 1);
138 	nouveau_object_ref(NULL, &client);
139 }
140 
141 static void
142 nouveau_accel_fini(struct nouveau_drm *drm)
143 {
144 	nouveau_gpuobj_ref(NULL, &drm->notify);
145 	nouveau_channel_del(&drm->channel);
146 	nouveau_channel_del(&drm->cechan);
147 	if (drm->fence)
148 		nouveau_fence(drm)->dtor(drm);
149 }
150 
151 static void
152 nouveau_accel_init(struct nouveau_drm *drm)
153 {
154 	struct nouveau_device *device = nv_device(drm->device);
155 	struct nouveau_object *object;
156 	u32 arg0, arg1;
157 	int ret;
158 
159 	if (nouveau_noaccel)
160 		return;
161 
162 	/* initialise synchronisation routines */
163 	if      (device->card_type < NV_10) ret = nv04_fence_create(drm);
164 	else if (device->chipset   <  0x17) ret = nv10_fence_create(drm);
165 	else if (device->card_type < NV_50) ret = nv17_fence_create(drm);
166 	else if (device->chipset   <  0x84) ret = nv50_fence_create(drm);
167 	else if (device->card_type < NV_C0) ret = nv84_fence_create(drm);
168 	else                                ret = nvc0_fence_create(drm);
169 	if (ret) {
170 		NV_ERROR(drm, "failed to initialise sync subsystem, %d\n", ret);
171 		nouveau_accel_fini(drm);
172 		return;
173 	}
174 
175 	if (device->card_type >= NV_E0) {
176 		ret = nouveau_channel_new(drm, &drm->client, NVDRM_DEVICE,
177 					  NVDRM_CHAN + 1,
178 					  NVE0_CHANNEL_IND_ENGINE_CE0 |
179 					  NVE0_CHANNEL_IND_ENGINE_CE1, 0,
180 					  &drm->cechan);
181 		if (ret)
182 			NV_ERROR(drm, "failed to create ce channel, %d\n", ret);
183 
184 		arg0 = NVE0_CHANNEL_IND_ENGINE_GR;
185 		arg1 = 1;
186 	} else {
187 		arg0 = NvDmaFB;
188 		arg1 = NvDmaTT;
189 	}
190 
191 	ret = nouveau_channel_new(drm, &drm->client, NVDRM_DEVICE, NVDRM_CHAN,
192 				  arg0, arg1, &drm->channel);
193 	if (ret) {
194 		NV_ERROR(drm, "failed to create kernel channel, %d\n", ret);
195 		nouveau_accel_fini(drm);
196 		return;
197 	}
198 
199 	if (device->card_type < NV_C0) {
200 		ret = nouveau_gpuobj_new(drm->device, NULL, 32, 0, 0,
201 					&drm->notify);
202 		if (ret) {
203 			NV_ERROR(drm, "failed to allocate notifier, %d\n", ret);
204 			nouveau_accel_fini(drm);
205 			return;
206 		}
207 
208 		ret = nouveau_object_new(nv_object(drm),
209 					 drm->channel->handle, NvNotify0,
210 					 0x003d, &(struct nv_dma_class) {
211 						.flags = NV_DMA_TARGET_VRAM |
212 							 NV_DMA_ACCESS_RDWR,
213 						.start = drm->notify->addr,
214 						.limit = drm->notify->addr + 31
215 						}, sizeof(struct nv_dma_class),
216 					 &object);
217 		if (ret) {
218 			nouveau_accel_fini(drm);
219 			return;
220 		}
221 	}
222 
223 
224 	nouveau_bo_move_init(drm);
225 }
226 
227 static int nouveau_drm_probe(struct pci_dev *pdev,
228 			     const struct pci_device_id *pent)
229 {
230 	struct nouveau_device *device;
231 	struct apertures_struct *aper;
232 	bool boot = false;
233 	int ret;
234 
235 	/* remove conflicting drivers (vesafb, efifb etc) */
236 	aper = alloc_apertures(3);
237 	if (!aper)
238 		return -ENOMEM;
239 
240 	aper->ranges[0].base = pci_resource_start(pdev, 1);
241 	aper->ranges[0].size = pci_resource_len(pdev, 1);
242 	aper->count = 1;
243 
244 	if (pci_resource_len(pdev, 2)) {
245 		aper->ranges[aper->count].base = pci_resource_start(pdev, 2);
246 		aper->ranges[aper->count].size = pci_resource_len(pdev, 2);
247 		aper->count++;
248 	}
249 
250 	if (pci_resource_len(pdev, 3)) {
251 		aper->ranges[aper->count].base = pci_resource_start(pdev, 3);
252 		aper->ranges[aper->count].size = pci_resource_len(pdev, 3);
253 		aper->count++;
254 	}
255 
256 #ifdef CONFIG_X86
257 	boot = pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW;
258 #endif
259 	remove_conflicting_framebuffers(aper, "nouveaufb", boot);
260 	kfree(aper);
261 
262 	ret = nouveau_device_create(pdev, nouveau_name(pdev), pci_name(pdev),
263 				    nouveau_config, nouveau_debug, &device);
264 	if (ret)
265 		return ret;
266 
267 	pci_set_master(pdev);
268 
269 	ret = drm_get_pci_dev(pdev, pent, &driver);
270 	if (ret) {
271 		nouveau_object_ref(NULL, (struct nouveau_object **)&device);
272 		return ret;
273 	}
274 
275 	return 0;
276 }
277 
278 static struct lock_class_key drm_client_lock_class_key;
279 
280 static int
281 nouveau_drm_load(struct drm_device *dev, unsigned long flags)
282 {
283 	struct pci_dev *pdev = dev->pdev;
284 	struct nouveau_device *device;
285 	struct nouveau_drm *drm;
286 	int ret;
287 
288 	ret = nouveau_cli_create(pdev, "DRM", sizeof(*drm), (void**)&drm);
289 	if (ret)
290 		return ret;
291 	lockdep_set_class(&drm->client.mutex, &drm_client_lock_class_key);
292 
293 	dev->dev_private = drm;
294 	drm->dev = dev;
295 	drm->vblank.func = nouveau_drm_vblank_handler;
296 
297 	INIT_LIST_HEAD(&drm->clients);
298 	spin_lock_init(&drm->tile.lock);
299 
300 	/* make sure AGP controller is in a consistent state before we
301 	 * (possibly) execute vbios init tables (see nouveau_agp.h)
302 	 */
303 	if (drm_pci_device_is_agp(dev) && dev->agp) {
304 		/* dummy device object, doesn't init anything, but allows
305 		 * agp code access to registers
306 		 */
307 		ret = nouveau_object_new(nv_object(drm), NVDRM_CLIENT,
308 					 NVDRM_DEVICE, 0x0080,
309 					 &(struct nv_device_class) {
310 						.device = ~0,
311 						.disable =
312 						 ~(NV_DEVICE_DISABLE_MMIO |
313 						   NV_DEVICE_DISABLE_IDENTIFY),
314 						.debug0 = ~0,
315 					 }, sizeof(struct nv_device_class),
316 					 &drm->device);
317 		if (ret)
318 			goto fail_device;
319 
320 		nouveau_agp_reset(drm);
321 		nouveau_object_del(nv_object(drm), NVDRM_CLIENT, NVDRM_DEVICE);
322 	}
323 
324 	ret = nouveau_object_new(nv_object(drm), NVDRM_CLIENT, NVDRM_DEVICE,
325 				 0x0080, &(struct nv_device_class) {
326 					.device = ~0,
327 					.disable = 0,
328 					.debug0 = 0,
329 				 }, sizeof(struct nv_device_class),
330 				 &drm->device);
331 	if (ret)
332 		goto fail_device;
333 
334 	/* workaround an odd issue on nvc1 by disabling the device's
335 	 * nosnoop capability.  hopefully won't cause issues until a
336 	 * better fix is found - assuming there is one...
337 	 */
338 	device = nv_device(drm->device);
339 	if (nv_device(drm->device)->chipset == 0xc1)
340 		nv_mask(device, 0x00088080, 0x00000800, 0x00000000);
341 
342 	nouveau_vga_init(drm);
343 	nouveau_agp_init(drm);
344 
345 	if (device->card_type >= NV_50) {
346 		ret = nouveau_vm_new(nv_device(drm->device), 0, (1ULL << 40),
347 				     0x1000, &drm->client.base.vm);
348 		if (ret)
349 			goto fail_device;
350 	}
351 
352 	ret = nouveau_ttm_init(drm);
353 	if (ret)
354 		goto fail_ttm;
355 
356 	ret = nouveau_bios_init(dev);
357 	if (ret)
358 		goto fail_bios;
359 
360 	ret = nouveau_irq_init(dev);
361 	if (ret)
362 		goto fail_irq;
363 
364 	ret = nouveau_display_create(dev);
365 	if (ret)
366 		goto fail_dispctor;
367 
368 	if (dev->mode_config.num_crtc) {
369 		ret = nouveau_display_init(dev);
370 		if (ret)
371 			goto fail_dispinit;
372 	}
373 
374 	nouveau_pm_init(dev);
375 
376 	nouveau_accel_init(drm);
377 	nouveau_fbcon_init(dev);
378 	return 0;
379 
380 fail_dispinit:
381 	nouveau_display_destroy(dev);
382 fail_dispctor:
383 	nouveau_irq_fini(dev);
384 fail_irq:
385 	nouveau_bios_takedown(dev);
386 fail_bios:
387 	nouveau_ttm_fini(drm);
388 fail_ttm:
389 	nouveau_agp_fini(drm);
390 	nouveau_vga_fini(drm);
391 fail_device:
392 	nouveau_cli_destroy(&drm->client);
393 	return ret;
394 }
395 
396 static int
397 nouveau_drm_unload(struct drm_device *dev)
398 {
399 	struct nouveau_drm *drm = nouveau_drm(dev);
400 
401 	nouveau_fbcon_fini(dev);
402 	nouveau_accel_fini(drm);
403 
404 	nouveau_pm_fini(dev);
405 
406 	if (dev->mode_config.num_crtc)
407 		nouveau_display_fini(dev);
408 	nouveau_display_destroy(dev);
409 
410 	nouveau_irq_fini(dev);
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_irq_postinstall(dev);
529 	nouveau_pm_resume(dev);
530 
531 	if (dev->mode_config.num_crtc) {
532 		NV_INFO(drm, "resuming display...\n");
533 		nouveau_display_resume(dev);
534 	}
535 	return 0;
536 }
537 
538 int nouveau_pmops_resume(struct device *dev)
539 {
540 	struct pci_dev *pdev = to_pci_dev(dev);
541 	struct drm_device *drm_dev = pci_get_drvdata(pdev);
542 	int ret;
543 
544 	if (drm_dev->switch_power_state == DRM_SWITCH_POWER_OFF)
545 		return 0;
546 
547 	pci_set_power_state(pdev, PCI_D0);
548 	pci_restore_state(pdev);
549 	ret = pci_enable_device(pdev);
550 	if (ret)
551 		return ret;
552 	pci_set_master(pdev);
553 
554 	return nouveau_do_resume(drm_dev);
555 }
556 
557 static int nouveau_pmops_freeze(struct device *dev)
558 {
559 	struct pci_dev *pdev = to_pci_dev(dev);
560 	struct drm_device *drm_dev = pci_get_drvdata(pdev);
561 
562 	return nouveau_do_suspend(drm_dev);
563 }
564 
565 static int nouveau_pmops_thaw(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_resume(drm_dev);
571 }
572 
573 
574 static int
575 nouveau_drm_open(struct drm_device *dev, struct drm_file *fpriv)
576 {
577 	struct pci_dev *pdev = dev->pdev;
578 	struct nouveau_drm *drm = nouveau_drm(dev);
579 	struct nouveau_cli *cli;
580 	char name[32], tmpname[TASK_COMM_LEN];
581 	int ret;
582 
583 	get_task_comm(tmpname, current);
584 	snprintf(name, sizeof(name), "%s[%d]", tmpname, pid_nr(fpriv->pid));
585 
586 	ret = nouveau_cli_create(pdev, name, sizeof(*cli), (void **)&cli);
587 	if (ret)
588 		return ret;
589 
590 	if (nv_device(drm->device)->card_type >= NV_50) {
591 		ret = nouveau_vm_new(nv_device(drm->device), 0, (1ULL << 40),
592 				     0x1000, &cli->base.vm);
593 		if (ret) {
594 			nouveau_cli_destroy(cli);
595 			return ret;
596 		}
597 	}
598 
599 	fpriv->driver_priv = cli;
600 
601 	mutex_lock(&drm->client.mutex);
602 	list_add(&cli->head, &drm->clients);
603 	mutex_unlock(&drm->client.mutex);
604 	return 0;
605 }
606 
607 static void
608 nouveau_drm_preclose(struct drm_device *dev, struct drm_file *fpriv)
609 {
610 	struct nouveau_cli *cli = nouveau_cli(fpriv);
611 	struct nouveau_drm *drm = nouveau_drm(dev);
612 
613 	if (cli->abi16)
614 		nouveau_abi16_fini(cli->abi16);
615 
616 	mutex_lock(&drm->client.mutex);
617 	list_del(&cli->head);
618 	mutex_unlock(&drm->client.mutex);
619 }
620 
621 static void
622 nouveau_drm_postclose(struct drm_device *dev, struct drm_file *fpriv)
623 {
624 	struct nouveau_cli *cli = nouveau_cli(fpriv);
625 	nouveau_cli_destroy(cli);
626 }
627 
628 static struct drm_ioctl_desc
629 nouveau_ioctls[] = {
630 	DRM_IOCTL_DEF_DRV(NOUVEAU_GETPARAM, nouveau_abi16_ioctl_getparam, DRM_UNLOCKED|DRM_AUTH),
631 	DRM_IOCTL_DEF_DRV(NOUVEAU_SETPARAM, nouveau_abi16_ioctl_setparam, DRM_UNLOCKED|DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
632 	DRM_IOCTL_DEF_DRV(NOUVEAU_CHANNEL_ALLOC, nouveau_abi16_ioctl_channel_alloc, DRM_UNLOCKED|DRM_AUTH),
633 	DRM_IOCTL_DEF_DRV(NOUVEAU_CHANNEL_FREE, nouveau_abi16_ioctl_channel_free, DRM_UNLOCKED|DRM_AUTH),
634 	DRM_IOCTL_DEF_DRV(NOUVEAU_GROBJ_ALLOC, nouveau_abi16_ioctl_grobj_alloc, DRM_UNLOCKED|DRM_AUTH),
635 	DRM_IOCTL_DEF_DRV(NOUVEAU_NOTIFIEROBJ_ALLOC, nouveau_abi16_ioctl_notifierobj_alloc, DRM_UNLOCKED|DRM_AUTH),
636 	DRM_IOCTL_DEF_DRV(NOUVEAU_GPUOBJ_FREE, nouveau_abi16_ioctl_gpuobj_free, DRM_UNLOCKED|DRM_AUTH),
637 	DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_NEW, nouveau_gem_ioctl_new, DRM_UNLOCKED|DRM_AUTH),
638 	DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_PUSHBUF, nouveau_gem_ioctl_pushbuf, DRM_UNLOCKED|DRM_AUTH),
639 	DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_CPU_PREP, nouveau_gem_ioctl_cpu_prep, DRM_UNLOCKED|DRM_AUTH),
640 	DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_CPU_FINI, nouveau_gem_ioctl_cpu_fini, DRM_UNLOCKED|DRM_AUTH),
641 	DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_INFO, nouveau_gem_ioctl_info, DRM_UNLOCKED|DRM_AUTH),
642 };
643 
644 static const struct file_operations
645 nouveau_driver_fops = {
646 	.owner = THIS_MODULE,
647 	.open = drm_open,
648 	.release = drm_release,
649 	.unlocked_ioctl = drm_ioctl,
650 	.mmap = nouveau_ttm_mmap,
651 	.poll = drm_poll,
652 	.fasync = drm_fasync,
653 	.read = drm_read,
654 #if defined(CONFIG_COMPAT)
655 	.compat_ioctl = nouveau_compat_ioctl,
656 #endif
657 	.llseek = noop_llseek,
658 };
659 
660 static struct drm_driver
661 driver = {
662 	.driver_features =
663 		DRIVER_USE_AGP | DRIVER_PCI_DMA | DRIVER_SG |
664 		DRIVER_HAVE_IRQ | DRIVER_IRQ_SHARED | DRIVER_GEM |
665 		DRIVER_MODESET | DRIVER_PRIME,
666 
667 	.load = nouveau_drm_load,
668 	.unload = nouveau_drm_unload,
669 	.open = nouveau_drm_open,
670 	.preclose = nouveau_drm_preclose,
671 	.postclose = nouveau_drm_postclose,
672 	.lastclose = nouveau_vga_lastclose,
673 
674 #if defined(CONFIG_DEBUG_FS)
675 	.debugfs_init = nouveau_debugfs_init,
676 	.debugfs_cleanup = nouveau_debugfs_takedown,
677 #endif
678 
679 	.irq_preinstall = nouveau_irq_preinstall,
680 	.irq_postinstall = nouveau_irq_postinstall,
681 	.irq_uninstall = nouveau_irq_uninstall,
682 	.irq_handler = nouveau_irq_handler,
683 
684 	.get_vblank_counter = drm_vblank_count,
685 	.enable_vblank = nouveau_drm_vblank_enable,
686 	.disable_vblank = nouveau_drm_vblank_disable,
687 
688 	.ioctls = nouveau_ioctls,
689 	.fops = &nouveau_driver_fops,
690 
691 	.prime_handle_to_fd = drm_gem_prime_handle_to_fd,
692 	.prime_fd_to_handle = drm_gem_prime_fd_to_handle,
693 	.gem_prime_export = drm_gem_prime_export,
694 	.gem_prime_import = drm_gem_prime_import,
695 	.gem_prime_pin = nouveau_gem_prime_pin,
696 	.gem_prime_get_sg_table = nouveau_gem_prime_get_sg_table,
697 	.gem_prime_import_sg_table = nouveau_gem_prime_import_sg_table,
698 	.gem_prime_vmap = nouveau_gem_prime_vmap,
699 	.gem_prime_vunmap = nouveau_gem_prime_vunmap,
700 
701 	.gem_init_object = nouveau_gem_object_new,
702 	.gem_free_object = nouveau_gem_object_del,
703 	.gem_open_object = nouveau_gem_object_open,
704 	.gem_close_object = nouveau_gem_object_close,
705 
706 	.dumb_create = nouveau_display_dumb_create,
707 	.dumb_map_offset = nouveau_display_dumb_map_offset,
708 	.dumb_destroy = nouveau_display_dumb_destroy,
709 
710 	.name = DRIVER_NAME,
711 	.desc = DRIVER_DESC,
712 #ifdef GIT_REVISION
713 	.date = GIT_REVISION,
714 #else
715 	.date = DRIVER_DATE,
716 #endif
717 	.major = DRIVER_MAJOR,
718 	.minor = DRIVER_MINOR,
719 	.patchlevel = DRIVER_PATCHLEVEL,
720 };
721 
722 static struct pci_device_id
723 nouveau_drm_pci_table[] = {
724 	{
725 		PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_ANY_ID),
726 		.class = PCI_BASE_CLASS_DISPLAY << 16,
727 		.class_mask  = 0xff << 16,
728 	},
729 	{
730 		PCI_DEVICE(PCI_VENDOR_ID_NVIDIA_SGS, PCI_ANY_ID),
731 		.class = PCI_BASE_CLASS_DISPLAY << 16,
732 		.class_mask  = 0xff << 16,
733 	},
734 	{}
735 };
736 
737 static const struct dev_pm_ops nouveau_pm_ops = {
738 	.suspend = nouveau_pmops_suspend,
739 	.resume = nouveau_pmops_resume,
740 	.freeze = nouveau_pmops_freeze,
741 	.thaw = nouveau_pmops_thaw,
742 	.poweroff = nouveau_pmops_freeze,
743 	.restore = nouveau_pmops_resume,
744 };
745 
746 static struct pci_driver
747 nouveau_drm_pci_driver = {
748 	.name = "nouveau",
749 	.id_table = nouveau_drm_pci_table,
750 	.probe = nouveau_drm_probe,
751 	.remove = nouveau_drm_remove,
752 	.driver.pm = &nouveau_pm_ops,
753 };
754 
755 static int __init
756 nouveau_drm_init(void)
757 {
758 	driver.num_ioctls = ARRAY_SIZE(nouveau_ioctls);
759 
760 	if (nouveau_modeset == -1) {
761 #ifdef CONFIG_VGA_CONSOLE
762 		if (vgacon_text_force())
763 			nouveau_modeset = 0;
764 #endif
765 	}
766 
767 	if (!nouveau_modeset)
768 		return 0;
769 
770 	nouveau_register_dsm_handler();
771 	return drm_pci_init(&driver, &nouveau_drm_pci_driver);
772 }
773 
774 static void __exit
775 nouveau_drm_exit(void)
776 {
777 	if (!nouveau_modeset)
778 		return;
779 
780 	drm_pci_exit(&driver, &nouveau_drm_pci_driver);
781 	nouveau_unregister_dsm_handler();
782 }
783 
784 module_init(nouveau_drm_init);
785 module_exit(nouveau_drm_exit);
786 
787 MODULE_DEVICE_TABLE(pci, nouveau_drm_pci_table);
788 MODULE_AUTHOR(DRIVER_AUTHOR);
789 MODULE_DESCRIPTION(DRIVER_DESC);
790 MODULE_LICENSE("GPL and additional rights");
791