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/module.h>
26 #include <linux/pci.h>
28 #include <core/device.h>
29 #include <core/client.h>
30 #include <core/gpuobj.h>
31 #include <core/class.h>
33 #include <subdev/device.h>
34 #include <subdev/vm.h>
36 #include "nouveau_drm.h"
37 #include "nouveau_dma.h"
38 #include "nouveau_agp.h"
39 #include "nouveau_abi16.h"
40 #include "nouveau_fbcon.h"
41 #include "nouveau_fence.h"
43 #include "nouveau_ttm.h"
45 int __devinit nouveau_pci_probe(struct pci_dev *, const struct pci_device_id *);
46 void nouveau_pci_remove(struct pci_dev *);
47 int nouveau_pci_suspend(struct pci_dev *, pm_message_t);
48 int nouveau_pci_resume(struct pci_dev *);
49 int __init nouveau_init(struct pci_driver *);
50 void __exit nouveau_exit(struct pci_driver *);
52 int nouveau_load(struct drm_device *, unsigned long);
53 int nouveau_unload(struct drm_device *);
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);
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);
63 MODULE_PARM_DESC(noaccel, "disable kernel/abi16 acceleration");
64 static int nouveau_noaccel = 0;
65 module_param_named(noaccel, nouveau_noaccel, int, 0400);
68 nouveau_name(struct pci_dev *pdev)
70 u64 name = (u64)pci_domain_nr(pdev->bus) << 32;
71 name |= pdev->bus->number << 16;
72 name |= PCI_SLOT(pdev->devfn) << 8;
73 return name | PCI_FUNC(pdev->devfn);
77 nouveau_cli_create(struct pci_dev *pdev, u32 name, int size, void **pcli)
79 struct nouveau_cli *cli;
82 ret = nouveau_client_create_(name, nouveau_name(pdev), nouveau_config,
83 nouveau_debug, size, pcli);
88 mutex_init(&cli->mutex);
93 nouveau_cli_destroy(struct nouveau_cli *cli)
95 struct nouveau_object *client = nv_object(cli);
96 nouveau_vm_ref(NULL, &cli->base.vm, NULL);
97 nouveau_client_fini(&cli->base, false);
98 atomic_set(&client->refcount, 1);
99 nouveau_object_ref(NULL, &client);
103 nouveau_accel_fini(struct nouveau_drm *drm)
105 nouveau_gpuobj_ref(NULL, &drm->notify);
106 nouveau_channel_del(&drm->channel);
108 nouveau_fence(drm)->dtor(drm);
112 nouveau_accel_init(struct nouveau_drm *drm)
114 struct nouveau_device *device = nv_device(drm->device);
115 struct nouveau_object *object;
121 /* initialise synchronisation routines */
122 if (device->card_type < NV_10) ret = nv04_fence_create(drm);
123 else if (device->chipset < 0x84) ret = nv10_fence_create(drm);
124 else if (device->card_type < NV_C0) ret = nv84_fence_create(drm);
125 else ret = nvc0_fence_create(drm);
127 NV_ERROR(drm, "failed to initialise sync subsystem, %d\n", ret);
128 nouveau_accel_fini(drm);
132 ret = nouveau_channel_new(drm, &drm->client, NVDRM_DEVICE, NVDRM_CHAN,
133 NvDmaFB, NvDmaTT, &drm->channel);
135 NV_ERROR(drm, "failed to create kernel channel, %d\n", ret);
136 nouveau_accel_fini(drm);
140 if (device->card_type < NV_C0) {
141 ret = nouveau_gpuobj_new(drm->device, NULL, 32, 0, 0,
144 NV_ERROR(drm, "failed to allocate notifier, %d\n", ret);
145 nouveau_accel_fini(drm);
149 ret = nouveau_object_new(nv_object(drm),
150 drm->channel->handle, NvNotify0,
151 0x003d, &(struct nv_dma_class) {
152 .flags = NV_DMA_TARGET_VRAM |
154 .start = drm->notify->addr,
155 .limit = drm->notify->addr + 31
156 }, sizeof(struct nv_dma_class),
159 nouveau_accel_fini(drm);
165 nouveau_bo_move_init(drm->channel);
169 nouveau_drm_probe(struct pci_dev *pdev, const struct pci_device_id *pent)
171 struct nouveau_device *device;
172 struct apertures_struct *aper;
176 /* remove conflicting drivers (vesafb, efifb etc) */
177 aper = alloc_apertures(3);
181 aper->ranges[0].base = pci_resource_start(pdev, 1);
182 aper->ranges[0].size = pci_resource_len(pdev, 1);
185 if (pci_resource_len(pdev, 2)) {
186 aper->ranges[aper->count].base = pci_resource_start(pdev, 2);
187 aper->ranges[aper->count].size = pci_resource_len(pdev, 2);
191 if (pci_resource_len(pdev, 3)) {
192 aper->ranges[aper->count].base = pci_resource_start(pdev, 3);
193 aper->ranges[aper->count].size = pci_resource_len(pdev, 3);
198 boot = pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW;
200 remove_conflicting_framebuffers(aper, "nouveaufb", boot);
202 ret = nouveau_device_create(pdev, nouveau_name(pdev), pci_name(pdev),
203 nouveau_config, nouveau_debug, &device);
207 pci_set_master(pdev);
209 ret = nouveau_pci_probe(pdev, pent);
211 nouveau_object_ref(NULL, (struct nouveau_object **)&device);
219 nouveau_drm_load(struct drm_device *dev, unsigned long flags)
221 struct pci_dev *pdev = dev->pdev;
222 struct nouveau_device *device;
223 struct nouveau_drm *drm;
226 ret = nouveau_cli_create(pdev, 0, sizeof(*drm), (void**)&drm);
227 dev->dev_private = drm;
231 INIT_LIST_HEAD(&drm->clients);
232 spin_lock_init(&drm->tile.lock);
235 /* make sure AGP controller is in a consistent state before we
236 * (possibly) execute vbios init tables (see nouveau_agp.h)
238 if (drm_pci_device_is_agp(dev) && dev->agp) {
239 /* dummy device object, doesn't init anything, but allows
240 * agp code access to registers
242 ret = nouveau_object_new(nv_object(drm), NVDRM_CLIENT,
243 NVDRM_DEVICE, 0x0080,
244 &(struct nv_device_class) {
247 ~(NV_DEVICE_DISABLE_MMIO |
248 NV_DEVICE_DISABLE_IDENTIFY),
250 }, sizeof(struct nv_device_class),
255 nouveau_agp_reset(drm);
256 nouveau_object_del(nv_object(drm), NVDRM_CLIENT, NVDRM_DEVICE);
259 ret = nouveau_object_new(nv_object(drm), NVDRM_CLIENT, NVDRM_DEVICE,
260 0x0080, &(struct nv_device_class) {
264 }, sizeof(struct nv_device_class),
269 device = nv_device(drm->device);
272 nouveau_agp_init(drm);
274 if (device->card_type >= NV_50) {
275 ret = nouveau_vm_new(nv_device(drm->device), 0, (1ULL << 40),
276 0x1000, &drm->client.base.vm);
281 ret = nouveau_ttm_init(drm);
285 ret = nouveau_load(dev, flags);
289 nouveau_accel_init(drm);
290 nouveau_fbcon_init(dev);
294 nouveau_ttm_fini(drm);
296 nouveau_cli_destroy(&drm->client);
301 nouveau_drm_unload(struct drm_device *dev)
303 struct nouveau_drm *drm = nouveau_newpriv(dev);
304 struct pci_dev *pdev = dev->pdev;
307 nouveau_fbcon_fini(dev);
308 nouveau_accel_fini(drm);
310 ret = nouveau_unload(dev);
314 nouveau_ttm_fini(drm);
315 nouveau_agp_fini(drm);
317 pci_set_drvdata(pdev, drm->client.base.device);
318 nouveau_cli_destroy(&drm->client);
323 nouveau_drm_remove(struct pci_dev *pdev)
325 struct nouveau_object *device;
326 nouveau_pci_remove(pdev);
327 device = pci_get_drvdata(pdev);
328 nouveau_object_ref(NULL, &device);
329 nouveau_object_debug();
333 nouveau_drm_suspend(struct pci_dev *pdev, pm_message_t pm_state)
335 struct drm_device *dev = pci_get_drvdata(pdev);
336 struct nouveau_drm *drm = nouveau_newpriv(dev);
337 struct nouveau_cli *cli;
340 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF ||
341 pm_state.event == PM_EVENT_PRETHAW)
344 NV_INFO(drm, "suspending fbcon...\n");
345 nouveau_fbcon_set_suspend(dev, 1);
347 NV_INFO(drm, "suspending drm...\n");
348 ret = nouveau_pci_suspend(pdev, pm_state);
352 NV_INFO(drm, "evicting buffers...\n");
353 ttm_bo_evict_mm(&drm->ttm.bdev, TTM_PL_VRAM);
355 if (drm->fence && nouveau_fence(drm)->suspend) {
356 if (!nouveau_fence(drm)->suspend(drm))
360 NV_INFO(drm, "suspending client object trees...\n");
361 list_for_each_entry(cli, &drm->clients, head) {
362 ret = nouveau_client_fini(&cli->base, true);
367 ret = nouveau_client_fini(&drm->client.base, true);
371 nouveau_agp_fini(drm);
373 pci_save_state(pdev);
374 if (pm_state.event == PM_EVENT_SUSPEND) {
375 pci_disable_device(pdev);
376 pci_set_power_state(pdev, PCI_D3hot);
382 list_for_each_entry_continue_reverse(cli, &drm->clients, head) {
383 nouveau_client_init(&cli->base);
386 nouveau_pci_resume(pdev);
391 nouveau_drm_resume(struct pci_dev *pdev)
393 struct drm_device *dev = pci_get_drvdata(pdev);
394 struct nouveau_drm *drm = nouveau_newpriv(dev);
395 struct nouveau_cli *cli;
398 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
401 NV_INFO(drm, "re-enabling device...\n");
402 pci_set_power_state(pdev, PCI_D0);
403 pci_restore_state(pdev);
404 ret = pci_enable_device(pdev);
407 pci_set_master(pdev);
409 nouveau_agp_reset(drm);
411 NV_INFO(drm, "resuming client object trees...\n");
412 nouveau_client_init(&drm->client.base);
413 nouveau_agp_init(drm);
415 list_for_each_entry(cli, &drm->clients, head) {
416 nouveau_client_init(&cli->base);
419 if (drm->fence && nouveau_fence(drm)->resume)
420 nouveau_fence(drm)->resume(drm);
422 return nouveau_pci_resume(pdev);
426 nouveau_drm_open(struct drm_device *dev, struct drm_file *fpriv)
428 struct pci_dev *pdev = dev->pdev;
429 struct nouveau_drm *drm = nouveau_drm(dev);
430 struct nouveau_cli *cli;
433 ret = nouveau_cli_create(pdev, fpriv->pid, sizeof(*cli), (void **)&cli);
437 if (nv_device(drm->device)->card_type >= NV_50) {
438 ret = nouveau_vm_new(nv_device(drm->device), 0, (1ULL << 40),
439 0x1000, &cli->base.vm);
441 nouveau_cli_destroy(cli);
446 fpriv->driver_priv = cli;
448 mutex_lock(&drm->client.mutex);
449 list_add(&cli->head, &drm->clients);
450 mutex_unlock(&drm->client.mutex);
455 nouveau_drm_preclose(struct drm_device *dev, struct drm_file *fpriv)
457 struct nouveau_cli *cli = nouveau_cli(fpriv);
458 struct nouveau_drm *drm = nouveau_drm(dev);
461 nouveau_abi16_fini(cli->abi16);
463 mutex_lock(&drm->client.mutex);
464 list_del(&cli->head);
465 mutex_unlock(&drm->client.mutex);
469 nouveau_drm_postclose(struct drm_device *dev, struct drm_file *fpriv)
471 struct nouveau_cli *cli = nouveau_cli(fpriv);
472 nouveau_cli_destroy(cli);
475 static struct pci_device_id
476 nouveau_drm_pci_table[] = {
478 PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_ANY_ID),
479 .class = PCI_BASE_CLASS_DISPLAY << 16,
480 .class_mask = 0xff << 16,
483 PCI_DEVICE(PCI_VENDOR_ID_NVIDIA_SGS, PCI_ANY_ID),
484 .class = PCI_BASE_CLASS_DISPLAY << 16,
485 .class_mask = 0xff << 16,
490 static struct pci_driver
491 nouveau_drm_pci_driver = {
493 .id_table = nouveau_drm_pci_table,
494 .probe = nouveau_drm_probe,
495 .remove = nouveau_drm_remove,
496 .suspend = nouveau_drm_suspend,
497 .resume = nouveau_drm_resume,
501 nouveau_drm_init(void)
503 return nouveau_init(&nouveau_drm_pci_driver);
507 nouveau_drm_exit(void)
509 nouveau_exit(&nouveau_drm_pci_driver);
512 module_init(nouveau_drm_init);
513 module_exit(nouveau_drm_exit);
515 MODULE_DEVICE_TABLE(pci, nouveau_drm_pci_table);
516 MODULE_AUTHOR("Nouveau Project");
517 MODULE_DESCRIPTION("nVidia Riva/TNT/GeForce/Quadro/Tesla");
518 MODULE_LICENSE("GPL and additional rights");