2 * Copyright 2012 Red Hat Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
25 #include <linux/console.h>
26 #include <linux/module.h>
27 #include <linux/pci.h>
29 #include <core/device.h>
30 #include <core/client.h>
31 #include <core/gpuobj.h>
32 #include <core/class.h>
34 #include <subdev/device.h>
35 #include <subdev/vm.h>
37 #include "nouveau_drm.h"
38 #include "nouveau_irq.h"
39 #include "nouveau_dma.h"
40 #include "nouveau_ttm.h"
41 #include "nouveau_gem.h"
42 #include "nouveau_agp.h"
43 #include "nouveau_vga.h"
44 #include "nouveau_pm.h"
45 #include "nouveau_acpi.h"
46 #include "nouveau_bios.h"
47 #include "nouveau_ioctl.h"
48 #include "nouveau_abi16.h"
49 #include "nouveau_fbcon.h"
50 #include "nouveau_fence.h"
52 #include "nouveau_ttm.h"
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);
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);
62 MODULE_PARM_DESC(noaccel, "disable kernel/abi16 acceleration");
63 static int nouveau_noaccel = 0;
64 module_param_named(noaccel, nouveau_noaccel, int, 0400);
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);
71 static struct drm_driver driver;
74 nouveau_name(struct pci_dev *pdev)
76 u64 name = (u64)pci_domain_nr(pdev->bus) << 32;
77 name |= pdev->bus->number << 16;
78 name |= PCI_SLOT(pdev->devfn) << 8;
79 return name | PCI_FUNC(pdev->devfn);
83 nouveau_cli_create(struct pci_dev *pdev, const char *name,
84 int size, void **pcli)
86 struct nouveau_cli *cli;
89 ret = nouveau_client_create_(name, nouveau_name(pdev), nouveau_config,
90 nouveau_debug, size, pcli);
95 mutex_init(&cli->mutex);
100 nouveau_cli_destroy(struct nouveau_cli *cli)
102 struct nouveau_object *client = nv_object(cli);
103 nouveau_vm_ref(NULL, &cli->base.vm, NULL);
104 nouveau_client_fini(&cli->base, false);
105 atomic_set(&client->refcount, 1);
106 nouveau_object_ref(NULL, &client);
110 nouveau_accel_fini(struct nouveau_drm *drm)
112 nouveau_gpuobj_ref(NULL, &drm->notify);
113 nouveau_channel_del(&drm->channel);
114 nouveau_channel_del(&drm->cechan);
116 nouveau_fence(drm)->dtor(drm);
120 nouveau_accel_init(struct nouveau_drm *drm)
122 struct nouveau_device *device = nv_device(drm->device);
123 struct nouveau_object *object;
130 /* initialise synchronisation routines */
131 if (device->card_type < NV_10) ret = nv04_fence_create(drm);
132 else if (device->card_type < NV_50) ret = nv10_fence_create(drm);
133 else if (device->chipset < 0x84) ret = nv50_fence_create(drm);
134 else if (device->card_type < NV_C0) ret = nv84_fence_create(drm);
135 else ret = nvc0_fence_create(drm);
137 NV_ERROR(drm, "failed to initialise sync subsystem, %d\n", ret);
138 nouveau_accel_fini(drm);
142 if (device->card_type >= NV_E0) {
143 ret = nouveau_channel_new(drm, &drm->client, NVDRM_DEVICE,
145 NVE0_CHANNEL_IND_ENGINE_CE0 |
146 NVE0_CHANNEL_IND_ENGINE_CE1, 0,
149 NV_ERROR(drm, "failed to create ce channel, %d\n", ret);
151 arg0 = NVE0_CHANNEL_IND_ENGINE_GR;
158 ret = nouveau_channel_new(drm, &drm->client, NVDRM_DEVICE, NVDRM_CHAN,
159 arg0, arg1, &drm->channel);
161 NV_ERROR(drm, "failed to create kernel channel, %d\n", ret);
162 nouveau_accel_fini(drm);
166 if (device->card_type < NV_C0) {
167 ret = nouveau_gpuobj_new(drm->device, NULL, 32, 0, 0,
170 NV_ERROR(drm, "failed to allocate notifier, %d\n", ret);
171 nouveau_accel_fini(drm);
175 ret = nouveau_object_new(nv_object(drm),
176 drm->channel->handle, NvNotify0,
177 0x003d, &(struct nv_dma_class) {
178 .flags = NV_DMA_TARGET_VRAM |
180 .start = drm->notify->addr,
181 .limit = drm->notify->addr + 31
182 }, sizeof(struct nv_dma_class),
185 nouveau_accel_fini(drm);
191 nouveau_bo_move_init(drm);
195 nouveau_drm_probe(struct pci_dev *pdev, const struct pci_device_id *pent)
197 struct nouveau_device *device;
198 struct apertures_struct *aper;
202 /* remove conflicting drivers (vesafb, efifb etc) */
203 aper = alloc_apertures(3);
207 aper->ranges[0].base = pci_resource_start(pdev, 1);
208 aper->ranges[0].size = pci_resource_len(pdev, 1);
211 if (pci_resource_len(pdev, 2)) {
212 aper->ranges[aper->count].base = pci_resource_start(pdev, 2);
213 aper->ranges[aper->count].size = pci_resource_len(pdev, 2);
217 if (pci_resource_len(pdev, 3)) {
218 aper->ranges[aper->count].base = pci_resource_start(pdev, 3);
219 aper->ranges[aper->count].size = pci_resource_len(pdev, 3);
224 boot = pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW;
226 remove_conflicting_framebuffers(aper, "nouveaufb", boot);
228 ret = nouveau_device_create(pdev, nouveau_name(pdev), pci_name(pdev),
229 nouveau_config, nouveau_debug, &device);
233 pci_set_master(pdev);
235 ret = drm_get_pci_dev(pdev, pent, &driver);
237 nouveau_object_ref(NULL, (struct nouveau_object **)&device);
245 nouveau_drm_load(struct drm_device *dev, unsigned long flags)
247 struct pci_dev *pdev = dev->pdev;
248 struct nouveau_device *device;
249 struct nouveau_drm *drm;
252 ret = nouveau_cli_create(pdev, "DRM", sizeof(*drm), (void**)&drm);
256 dev->dev_private = drm;
259 INIT_LIST_HEAD(&drm->clients);
260 spin_lock_init(&drm->tile.lock);
262 /* make sure AGP controller is in a consistent state before we
263 * (possibly) execute vbios init tables (see nouveau_agp.h)
265 if (drm_pci_device_is_agp(dev) && dev->agp) {
266 /* dummy device object, doesn't init anything, but allows
267 * agp code access to registers
269 ret = nouveau_object_new(nv_object(drm), NVDRM_CLIENT,
270 NVDRM_DEVICE, 0x0080,
271 &(struct nv_device_class) {
274 ~(NV_DEVICE_DISABLE_MMIO |
275 NV_DEVICE_DISABLE_IDENTIFY),
277 }, sizeof(struct nv_device_class),
282 nouveau_agp_reset(drm);
283 nouveau_object_del(nv_object(drm), NVDRM_CLIENT, NVDRM_DEVICE);
286 ret = nouveau_object_new(nv_object(drm), NVDRM_CLIENT, NVDRM_DEVICE,
287 0x0080, &(struct nv_device_class) {
291 }, sizeof(struct nv_device_class),
296 /* workaround an odd issue on nvc1 by disabling the device's
297 * nosnoop capability. hopefully won't cause issues until a
298 * better fix is found - assuming there is one...
300 device = nv_device(drm->device);
301 if (nv_device(drm->device)->chipset == 0xc1)
302 nv_mask(device, 0x00088080, 0x00000800, 0x00000000);
304 nouveau_vga_init(drm);
305 nouveau_agp_init(drm);
307 if (device->card_type >= NV_50) {
308 ret = nouveau_vm_new(nv_device(drm->device), 0, (1ULL << 40),
309 0x1000, &drm->client.base.vm);
314 ret = nouveau_ttm_init(drm);
318 ret = nouveau_bios_init(dev);
322 ret = nouveau_irq_init(dev);
326 ret = nouveau_display_create(dev);
330 if (dev->mode_config.num_crtc) {
331 ret = nouveau_display_init(dev);
336 nouveau_pm_init(dev);
338 nouveau_accel_init(drm);
339 nouveau_fbcon_init(dev);
343 nouveau_display_destroy(dev);
345 nouveau_irq_fini(dev);
347 nouveau_bios_takedown(dev);
349 nouveau_ttm_fini(drm);
351 nouveau_agp_fini(drm);
352 nouveau_vga_fini(drm);
354 nouveau_cli_destroy(&drm->client);
359 nouveau_drm_unload(struct drm_device *dev)
361 struct nouveau_drm *drm = nouveau_drm(dev);
363 nouveau_fbcon_fini(dev);
364 nouveau_accel_fini(drm);
366 nouveau_pm_fini(dev);
368 if (dev->mode_config.num_crtc)
369 nouveau_display_fini(dev);
370 nouveau_display_destroy(dev);
372 nouveau_irq_fini(dev);
373 nouveau_bios_takedown(dev);
375 nouveau_ttm_fini(drm);
376 nouveau_agp_fini(drm);
377 nouveau_vga_fini(drm);
379 nouveau_cli_destroy(&drm->client);
384 nouveau_drm_remove(struct pci_dev *pdev)
386 struct drm_device *dev = pci_get_drvdata(pdev);
387 struct nouveau_drm *drm = nouveau_drm(dev);
388 struct nouveau_object *device;
390 device = drm->client.base.device;
393 nouveau_object_ref(NULL, &device);
394 nouveau_object_debug();
398 nouveau_drm_suspend(struct pci_dev *pdev, pm_message_t pm_state)
400 struct drm_device *dev = pci_get_drvdata(pdev);
401 struct nouveau_drm *drm = nouveau_drm(dev);
402 struct nouveau_cli *cli;
405 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF ||
406 pm_state.event == PM_EVENT_PRETHAW)
409 if (dev->mode_config.num_crtc) {
410 NV_INFO(drm, "suspending fbcon...\n");
411 nouveau_fbcon_set_suspend(dev, 1);
413 NV_INFO(drm, "suspending display...\n");
414 ret = nouveau_display_suspend(dev);
419 NV_INFO(drm, "evicting buffers...\n");
420 ttm_bo_evict_mm(&drm->ttm.bdev, TTM_PL_VRAM);
422 if (drm->fence && nouveau_fence(drm)->suspend) {
423 if (!nouveau_fence(drm)->suspend(drm))
427 NV_INFO(drm, "suspending client object trees...\n");
428 list_for_each_entry(cli, &drm->clients, head) {
429 ret = nouveau_client_fini(&cli->base, true);
434 ret = nouveau_client_fini(&drm->client.base, true);
438 nouveau_agp_fini(drm);
440 pci_save_state(pdev);
441 if (pm_state.event == PM_EVENT_SUSPEND) {
442 pci_disable_device(pdev);
443 pci_set_power_state(pdev, PCI_D3hot);
449 list_for_each_entry_continue_reverse(cli, &drm->clients, head) {
450 nouveau_client_init(&cli->base);
453 if (dev->mode_config.num_crtc) {
454 NV_INFO(drm, "resuming display...\n");
455 nouveau_display_resume(dev);
461 nouveau_drm_resume(struct pci_dev *pdev)
463 struct drm_device *dev = pci_get_drvdata(pdev);
464 struct nouveau_drm *drm = nouveau_drm(dev);
465 struct nouveau_cli *cli;
468 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
471 NV_INFO(drm, "re-enabling device...\n");
472 pci_set_power_state(pdev, PCI_D0);
473 pci_restore_state(pdev);
474 ret = pci_enable_device(pdev);
477 pci_set_master(pdev);
479 nouveau_agp_reset(drm);
481 NV_INFO(drm, "resuming client object trees...\n");
482 nouveau_client_init(&drm->client.base);
483 nouveau_agp_init(drm);
485 list_for_each_entry(cli, &drm->clients, head) {
486 nouveau_client_init(&cli->base);
489 if (drm->fence && nouveau_fence(drm)->resume)
490 nouveau_fence(drm)->resume(drm);
492 nouveau_run_vbios_init(dev);
493 nouveau_irq_postinstall(dev);
494 nouveau_pm_resume(dev);
496 if (dev->mode_config.num_crtc) {
497 NV_INFO(drm, "resuming display...\n");
498 nouveau_display_resume(dev);
504 nouveau_drm_open(struct drm_device *dev, struct drm_file *fpriv)
506 struct pci_dev *pdev = dev->pdev;
507 struct nouveau_drm *drm = nouveau_drm(dev);
508 struct nouveau_cli *cli;
512 snprintf(name, sizeof(name), "%d", pid_nr(fpriv->pid));
514 ret = nouveau_cli_create(pdev, name, sizeof(*cli), (void **)&cli);
518 if (nv_device(drm->device)->card_type >= NV_50) {
519 ret = nouveau_vm_new(nv_device(drm->device), 0, (1ULL << 40),
520 0x1000, &cli->base.vm);
522 nouveau_cli_destroy(cli);
527 fpriv->driver_priv = cli;
529 mutex_lock(&drm->client.mutex);
530 list_add(&cli->head, &drm->clients);
531 mutex_unlock(&drm->client.mutex);
536 nouveau_drm_preclose(struct drm_device *dev, struct drm_file *fpriv)
538 struct nouveau_cli *cli = nouveau_cli(fpriv);
539 struct nouveau_drm *drm = nouveau_drm(dev);
542 nouveau_abi16_fini(cli->abi16);
544 mutex_lock(&drm->client.mutex);
545 list_del(&cli->head);
546 mutex_unlock(&drm->client.mutex);
550 nouveau_drm_postclose(struct drm_device *dev, struct drm_file *fpriv)
552 struct nouveau_cli *cli = nouveau_cli(fpriv);
553 nouveau_cli_destroy(cli);
556 static struct drm_ioctl_desc
558 DRM_IOCTL_DEF_DRV(NOUVEAU_GETPARAM, nouveau_abi16_ioctl_getparam, DRM_UNLOCKED|DRM_AUTH),
559 DRM_IOCTL_DEF_DRV(NOUVEAU_SETPARAM, nouveau_abi16_ioctl_setparam, DRM_UNLOCKED|DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
560 DRM_IOCTL_DEF_DRV(NOUVEAU_CHANNEL_ALLOC, nouveau_abi16_ioctl_channel_alloc, DRM_UNLOCKED|DRM_AUTH),
561 DRM_IOCTL_DEF_DRV(NOUVEAU_CHANNEL_FREE, nouveau_abi16_ioctl_channel_free, DRM_UNLOCKED|DRM_AUTH),
562 DRM_IOCTL_DEF_DRV(NOUVEAU_GROBJ_ALLOC, nouveau_abi16_ioctl_grobj_alloc, DRM_UNLOCKED|DRM_AUTH),
563 DRM_IOCTL_DEF_DRV(NOUVEAU_NOTIFIEROBJ_ALLOC, nouveau_abi16_ioctl_notifierobj_alloc, DRM_UNLOCKED|DRM_AUTH),
564 DRM_IOCTL_DEF_DRV(NOUVEAU_GPUOBJ_FREE, nouveau_abi16_ioctl_gpuobj_free, DRM_UNLOCKED|DRM_AUTH),
565 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_NEW, nouveau_gem_ioctl_new, DRM_UNLOCKED|DRM_AUTH),
566 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_PUSHBUF, nouveau_gem_ioctl_pushbuf, DRM_UNLOCKED|DRM_AUTH),
567 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_CPU_PREP, nouveau_gem_ioctl_cpu_prep, DRM_UNLOCKED|DRM_AUTH),
568 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_CPU_FINI, nouveau_gem_ioctl_cpu_fini, DRM_UNLOCKED|DRM_AUTH),
569 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_INFO, nouveau_gem_ioctl_info, DRM_UNLOCKED|DRM_AUTH),
572 static const struct file_operations
573 nouveau_driver_fops = {
574 .owner = THIS_MODULE,
576 .release = drm_release,
577 .unlocked_ioctl = drm_ioctl,
578 .mmap = nouveau_ttm_mmap,
580 .fasync = drm_fasync,
582 #if defined(CONFIG_COMPAT)
583 .compat_ioctl = nouveau_compat_ioctl,
585 .llseek = noop_llseek,
588 static struct drm_driver
591 DRIVER_USE_AGP | DRIVER_PCI_DMA | DRIVER_SG |
592 DRIVER_HAVE_IRQ | DRIVER_IRQ_SHARED | DRIVER_GEM |
593 DRIVER_MODESET | DRIVER_PRIME,
595 .load = nouveau_drm_load,
596 .unload = nouveau_drm_unload,
597 .open = nouveau_drm_open,
598 .preclose = nouveau_drm_preclose,
599 .postclose = nouveau_drm_postclose,
600 .lastclose = nouveau_vga_lastclose,
602 .irq_preinstall = nouveau_irq_preinstall,
603 .irq_postinstall = nouveau_irq_postinstall,
604 .irq_uninstall = nouveau_irq_uninstall,
605 .irq_handler = nouveau_irq_handler,
607 .get_vblank_counter = drm_vblank_count,
608 .enable_vblank = nouveau_vblank_enable,
609 .disable_vblank = nouveau_vblank_disable,
611 .ioctls = nouveau_ioctls,
612 .fops = &nouveau_driver_fops,
614 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
615 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
616 .gem_prime_export = nouveau_gem_prime_export,
617 .gem_prime_import = nouveau_gem_prime_import,
619 .gem_init_object = nouveau_gem_object_new,
620 .gem_free_object = nouveau_gem_object_del,
621 .gem_open_object = nouveau_gem_object_open,
622 .gem_close_object = nouveau_gem_object_close,
624 .dumb_create = nouveau_display_dumb_create,
625 .dumb_map_offset = nouveau_display_dumb_map_offset,
626 .dumb_destroy = nouveau_display_dumb_destroy,
631 .date = GIT_REVISION,
635 .major = DRIVER_MAJOR,
636 .minor = DRIVER_MINOR,
637 .patchlevel = DRIVER_PATCHLEVEL,
640 static struct pci_device_id
641 nouveau_drm_pci_table[] = {
643 PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_ANY_ID),
644 .class = PCI_BASE_CLASS_DISPLAY << 16,
645 .class_mask = 0xff << 16,
648 PCI_DEVICE(PCI_VENDOR_ID_NVIDIA_SGS, PCI_ANY_ID),
649 .class = PCI_BASE_CLASS_DISPLAY << 16,
650 .class_mask = 0xff << 16,
655 static struct pci_driver
656 nouveau_drm_pci_driver = {
658 .id_table = nouveau_drm_pci_table,
659 .probe = nouveau_drm_probe,
660 .remove = nouveau_drm_remove,
661 .suspend = nouveau_drm_suspend,
662 .resume = nouveau_drm_resume,
666 nouveau_drm_init(void)
668 driver.num_ioctls = ARRAY_SIZE(nouveau_ioctls);
670 if (nouveau_modeset == -1) {
671 #ifdef CONFIG_VGA_CONSOLE
672 if (vgacon_text_force())
677 if (!nouveau_modeset)
680 nouveau_register_dsm_handler();
681 return drm_pci_init(&driver, &nouveau_drm_pci_driver);
685 nouveau_drm_exit(void)
687 if (!nouveau_modeset)
690 drm_pci_exit(&driver, &nouveau_drm_pci_driver);
691 nouveau_unregister_dsm_handler();
694 module_init(nouveau_drm_init);
695 module_exit(nouveau_drm_exit);
697 MODULE_DEVICE_TABLE(pci, nouveau_drm_pci_table);
698 MODULE_AUTHOR(DRIVER_AUTHOR);
699 MODULE_DESCRIPTION(DRIVER_DESC);
700 MODULE_LICENSE("GPL and additional rights");