Ben Skeggs | 9458029 | 2012-07-06 12:14:00 +1000 | [diff] [blame] | 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 | |
Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame] | 25 | #include <linux/console.h> |
Ben Skeggs | 9458029 | 2012-07-06 12:14:00 +1000 | [diff] [blame] | 26 | #include <linux/module.h> |
| 27 | #include <linux/pci.h> |
| 28 | |
| 29 | #include <core/device.h> |
| 30 | #include <core/client.h> |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 31 | #include <core/gpuobj.h> |
Ben Skeggs | 9458029 | 2012-07-06 12:14:00 +1000 | [diff] [blame] | 32 | #include <core/class.h> |
| 33 | |
| 34 | #include <subdev/device.h> |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 35 | #include <subdev/vm.h> |
Ben Skeggs | 9458029 | 2012-07-06 12:14:00 +1000 | [diff] [blame] | 36 | |
| 37 | #include "nouveau_drm.h" |
Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame] | 38 | #include "nouveau_irq.h" |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 39 | #include "nouveau_dma.h" |
Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame] | 40 | #include "nouveau_ttm.h" |
| 41 | #include "nouveau_gem.h" |
Ben Skeggs | cb75d97 | 2012-07-11 10:44:20 +1000 | [diff] [blame] | 42 | #include "nouveau_agp.h" |
Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame] | 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" |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 48 | #include "nouveau_abi16.h" |
| 49 | #include "nouveau_fbcon.h" |
| 50 | #include "nouveau_fence.h" |
| 51 | |
| 52 | #include "nouveau_ttm.h" |
Ben Skeggs | 9458029 | 2012-07-06 12:14:00 +1000 | [diff] [blame] | 53 | |
Ben Skeggs | 9458029 | 2012-07-06 12:14:00 +1000 | [diff] [blame] | 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 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 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 | |
Ben Skeggs | 9430738 | 2012-10-31 12:11:15 +1000 | [diff] [blame^] | 66 | MODULE_PARM_DESC(modeset, "enable driver (default: auto, " |
| 67 | "0 = disabled, 1 = enabled, 2 = headless)"); |
| 68 | int nouveau_modeset = -1; |
Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame] | 69 | module_param_named(modeset, nouveau_modeset, int, 0400); |
| 70 | |
| 71 | static struct drm_driver driver; |
| 72 | |
Ben Skeggs | 9458029 | 2012-07-06 12:14:00 +1000 | [diff] [blame] | 73 | static u64 |
| 74 | nouveau_name(struct pci_dev *pdev) |
| 75 | { |
| 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); |
| 80 | } |
| 81 | |
| 82 | static int |
Ben Skeggs | fa6df8c | 2012-09-12 13:09:23 +1000 | [diff] [blame] | 83 | nouveau_cli_create(struct pci_dev *pdev, const char *name, |
| 84 | int size, void **pcli) |
Ben Skeggs | 9458029 | 2012-07-06 12:14:00 +1000 | [diff] [blame] | 85 | { |
| 86 | struct nouveau_cli *cli; |
| 87 | int ret; |
| 88 | |
| 89 | ret = nouveau_client_create_(name, nouveau_name(pdev), nouveau_config, |
| 90 | nouveau_debug, size, pcli); |
| 91 | cli = *pcli; |
| 92 | if (ret) |
| 93 | return ret; |
| 94 | |
| 95 | mutex_init(&cli->mutex); |
| 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | static void |
| 100 | nouveau_cli_destroy(struct nouveau_cli *cli) |
| 101 | { |
| 102 | struct nouveau_object *client = nv_object(cli); |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 103 | nouveau_vm_ref(NULL, &cli->base.vm, NULL); |
Ben Skeggs | 9458029 | 2012-07-06 12:14:00 +1000 | [diff] [blame] | 104 | nouveau_client_fini(&cli->base, false); |
| 105 | atomic_set(&client->refcount, 1); |
| 106 | nouveau_object_ref(NULL, &client); |
| 107 | } |
| 108 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 109 | static void |
| 110 | nouveau_accel_fini(struct nouveau_drm *drm) |
| 111 | { |
| 112 | nouveau_gpuobj_ref(NULL, &drm->notify); |
| 113 | nouveau_channel_del(&drm->channel); |
Ben Skeggs | 4998104 | 2012-08-06 19:38:25 +1000 | [diff] [blame] | 114 | nouveau_channel_del(&drm->cechan); |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 115 | if (drm->fence) |
| 116 | nouveau_fence(drm)->dtor(drm); |
| 117 | } |
| 118 | |
| 119 | static void |
| 120 | nouveau_accel_init(struct nouveau_drm *drm) |
| 121 | { |
| 122 | struct nouveau_device *device = nv_device(drm->device); |
| 123 | struct nouveau_object *object; |
Ben Skeggs | 4998104 | 2012-08-06 19:38:25 +1000 | [diff] [blame] | 124 | u32 arg0, arg1; |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 125 | int ret; |
| 126 | |
| 127 | if (nouveau_noaccel) |
| 128 | return; |
| 129 | |
| 130 | /* initialise synchronisation routines */ |
| 131 | if (device->card_type < NV_10) ret = nv04_fence_create(drm); |
| 132 | else if (device->chipset < 0x84) ret = nv10_fence_create(drm); |
| 133 | else if (device->card_type < NV_C0) ret = nv84_fence_create(drm); |
| 134 | else ret = nvc0_fence_create(drm); |
| 135 | if (ret) { |
| 136 | NV_ERROR(drm, "failed to initialise sync subsystem, %d\n", ret); |
| 137 | nouveau_accel_fini(drm); |
| 138 | return; |
| 139 | } |
| 140 | |
Ben Skeggs | 4998104 | 2012-08-06 19:38:25 +1000 | [diff] [blame] | 141 | if (device->card_type >= NV_E0) { |
| 142 | ret = nouveau_channel_new(drm, &drm->client, NVDRM_DEVICE, |
| 143 | NVDRM_CHAN + 1, |
| 144 | NVE0_CHANNEL_IND_ENGINE_CE0 | |
| 145 | NVE0_CHANNEL_IND_ENGINE_CE1, 0, |
| 146 | &drm->cechan); |
| 147 | if (ret) |
| 148 | NV_ERROR(drm, "failed to create ce channel, %d\n", ret); |
| 149 | |
| 150 | arg0 = NVE0_CHANNEL_IND_ENGINE_GR; |
| 151 | arg1 = 0; |
| 152 | } else { |
| 153 | arg0 = NvDmaFB; |
| 154 | arg1 = NvDmaTT; |
| 155 | } |
| 156 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 157 | ret = nouveau_channel_new(drm, &drm->client, NVDRM_DEVICE, NVDRM_CHAN, |
Ben Skeggs | 4998104 | 2012-08-06 19:38:25 +1000 | [diff] [blame] | 158 | arg0, arg1, &drm->channel); |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 159 | if (ret) { |
| 160 | NV_ERROR(drm, "failed to create kernel channel, %d\n", ret); |
| 161 | nouveau_accel_fini(drm); |
| 162 | return; |
| 163 | } |
| 164 | |
| 165 | if (device->card_type < NV_C0) { |
| 166 | ret = nouveau_gpuobj_new(drm->device, NULL, 32, 0, 0, |
| 167 | &drm->notify); |
| 168 | if (ret) { |
| 169 | NV_ERROR(drm, "failed to allocate notifier, %d\n", ret); |
| 170 | nouveau_accel_fini(drm); |
| 171 | return; |
| 172 | } |
| 173 | |
| 174 | ret = nouveau_object_new(nv_object(drm), |
| 175 | drm->channel->handle, NvNotify0, |
| 176 | 0x003d, &(struct nv_dma_class) { |
| 177 | .flags = NV_DMA_TARGET_VRAM | |
| 178 | NV_DMA_ACCESS_RDWR, |
| 179 | .start = drm->notify->addr, |
| 180 | .limit = drm->notify->addr + 31 |
| 181 | }, sizeof(struct nv_dma_class), |
| 182 | &object); |
| 183 | if (ret) { |
| 184 | nouveau_accel_fini(drm); |
| 185 | return; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | |
Ben Skeggs | 4998104 | 2012-08-06 19:38:25 +1000 | [diff] [blame] | 190 | nouveau_bo_move_init(drm); |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 191 | } |
| 192 | |
Ben Skeggs | 9458029 | 2012-07-06 12:14:00 +1000 | [diff] [blame] | 193 | static int __devinit |
| 194 | nouveau_drm_probe(struct pci_dev *pdev, const struct pci_device_id *pent) |
| 195 | { |
| 196 | struct nouveau_device *device; |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 197 | struct apertures_struct *aper; |
| 198 | bool boot = false; |
Ben Skeggs | 9458029 | 2012-07-06 12:14:00 +1000 | [diff] [blame] | 199 | int ret; |
| 200 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 201 | /* remove conflicting drivers (vesafb, efifb etc) */ |
| 202 | aper = alloc_apertures(3); |
| 203 | if (!aper) |
| 204 | return -ENOMEM; |
| 205 | |
| 206 | aper->ranges[0].base = pci_resource_start(pdev, 1); |
| 207 | aper->ranges[0].size = pci_resource_len(pdev, 1); |
| 208 | aper->count = 1; |
| 209 | |
| 210 | if (pci_resource_len(pdev, 2)) { |
| 211 | aper->ranges[aper->count].base = pci_resource_start(pdev, 2); |
| 212 | aper->ranges[aper->count].size = pci_resource_len(pdev, 2); |
| 213 | aper->count++; |
| 214 | } |
| 215 | |
| 216 | if (pci_resource_len(pdev, 3)) { |
| 217 | aper->ranges[aper->count].base = pci_resource_start(pdev, 3); |
| 218 | aper->ranges[aper->count].size = pci_resource_len(pdev, 3); |
| 219 | aper->count++; |
| 220 | } |
| 221 | |
| 222 | #ifdef CONFIG_X86 |
| 223 | boot = pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW; |
| 224 | #endif |
| 225 | remove_conflicting_framebuffers(aper, "nouveaufb", boot); |
| 226 | |
Ben Skeggs | 9458029 | 2012-07-06 12:14:00 +1000 | [diff] [blame] | 227 | ret = nouveau_device_create(pdev, nouveau_name(pdev), pci_name(pdev), |
| 228 | nouveau_config, nouveau_debug, &device); |
| 229 | if (ret) |
| 230 | return ret; |
| 231 | |
| 232 | pci_set_master(pdev); |
| 233 | |
Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame] | 234 | ret = drm_get_pci_dev(pdev, pent, &driver); |
Ben Skeggs | 9458029 | 2012-07-06 12:14:00 +1000 | [diff] [blame] | 235 | if (ret) { |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 236 | nouveau_object_ref(NULL, (struct nouveau_object **)&device); |
Ben Skeggs | 9458029 | 2012-07-06 12:14:00 +1000 | [diff] [blame] | 237 | return ret; |
| 238 | } |
| 239 | |
| 240 | return 0; |
| 241 | } |
| 242 | |
Marcin Slusarz | 5b8a43a | 2012-08-19 23:00:00 +0200 | [diff] [blame] | 243 | static int |
Ben Skeggs | 9458029 | 2012-07-06 12:14:00 +1000 | [diff] [blame] | 244 | nouveau_drm_load(struct drm_device *dev, unsigned long flags) |
| 245 | { |
| 246 | struct pci_dev *pdev = dev->pdev; |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 247 | struct nouveau_device *device; |
Ben Skeggs | 9458029 | 2012-07-06 12:14:00 +1000 | [diff] [blame] | 248 | struct nouveau_drm *drm; |
| 249 | int ret; |
| 250 | |
Ben Skeggs | fa6df8c | 2012-09-12 13:09:23 +1000 | [diff] [blame] | 251 | ret = nouveau_cli_create(pdev, "DRM", sizeof(*drm), (void**)&drm); |
Ben Skeggs | 9458029 | 2012-07-06 12:14:00 +1000 | [diff] [blame] | 252 | if (ret) |
| 253 | return ret; |
| 254 | |
Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame] | 255 | dev->dev_private = drm; |
| 256 | drm->dev = dev; |
| 257 | |
Ben Skeggs | 9458029 | 2012-07-06 12:14:00 +1000 | [diff] [blame] | 258 | INIT_LIST_HEAD(&drm->clients); |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 259 | spin_lock_init(&drm->tile.lock); |
Ben Skeggs | 9458029 | 2012-07-06 12:14:00 +1000 | [diff] [blame] | 260 | |
Ben Skeggs | cb75d97 | 2012-07-11 10:44:20 +1000 | [diff] [blame] | 261 | /* make sure AGP controller is in a consistent state before we |
| 262 | * (possibly) execute vbios init tables (see nouveau_agp.h) |
| 263 | */ |
| 264 | if (drm_pci_device_is_agp(dev) && dev->agp) { |
| 265 | /* dummy device object, doesn't init anything, but allows |
| 266 | * agp code access to registers |
| 267 | */ |
| 268 | ret = nouveau_object_new(nv_object(drm), NVDRM_CLIENT, |
| 269 | NVDRM_DEVICE, 0x0080, |
| 270 | &(struct nv_device_class) { |
| 271 | .device = ~0, |
| 272 | .disable = |
| 273 | ~(NV_DEVICE_DISABLE_MMIO | |
| 274 | NV_DEVICE_DISABLE_IDENTIFY), |
| 275 | .debug0 = ~0, |
| 276 | }, sizeof(struct nv_device_class), |
| 277 | &drm->device); |
| 278 | if (ret) |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 279 | goto fail_device; |
Ben Skeggs | cb75d97 | 2012-07-11 10:44:20 +1000 | [diff] [blame] | 280 | |
| 281 | nouveau_agp_reset(drm); |
| 282 | nouveau_object_del(nv_object(drm), NVDRM_CLIENT, NVDRM_DEVICE); |
| 283 | } |
| 284 | |
Ben Skeggs | 9458029 | 2012-07-06 12:14:00 +1000 | [diff] [blame] | 285 | ret = nouveau_object_new(nv_object(drm), NVDRM_CLIENT, NVDRM_DEVICE, |
| 286 | 0x0080, &(struct nv_device_class) { |
| 287 | .device = ~0, |
| 288 | .disable = 0, |
| 289 | .debug0 = 0, |
| 290 | }, sizeof(struct nv_device_class), |
| 291 | &drm->device); |
| 292 | if (ret) |
| 293 | goto fail_device; |
| 294 | |
Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame] | 295 | /* workaround an odd issue on nvc1 by disabling the device's |
| 296 | * nosnoop capability. hopefully won't cause issues until a |
| 297 | * better fix is found - assuming there is one... |
| 298 | */ |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 299 | device = nv_device(drm->device); |
Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame] | 300 | if (nv_device(drm->device)->chipset == 0xc1) |
| 301 | nv_mask(device, 0x00088080, 0x00000800, 0x00000000); |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 302 | |
Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame] | 303 | nouveau_vga_init(drm); |
Ben Skeggs | cb75d97 | 2012-07-11 10:44:20 +1000 | [diff] [blame] | 304 | nouveau_agp_init(drm); |
| 305 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 306 | if (device->card_type >= NV_50) { |
| 307 | ret = nouveau_vm_new(nv_device(drm->device), 0, (1ULL << 40), |
| 308 | 0x1000, &drm->client.base.vm); |
| 309 | if (ret) |
| 310 | goto fail_device; |
| 311 | } |
| 312 | |
| 313 | ret = nouveau_ttm_init(drm); |
Ben Skeggs | 9458029 | 2012-07-06 12:14:00 +1000 | [diff] [blame] | 314 | if (ret) |
Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame] | 315 | goto fail_ttm; |
Ben Skeggs | 9458029 | 2012-07-06 12:14:00 +1000 | [diff] [blame] | 316 | |
Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame] | 317 | ret = nouveau_bios_init(dev); |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 318 | if (ret) |
Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame] | 319 | goto fail_bios; |
| 320 | |
| 321 | ret = nouveau_irq_init(dev); |
| 322 | if (ret) |
| 323 | goto fail_irq; |
| 324 | |
| 325 | ret = nouveau_display_create(dev); |
| 326 | if (ret) |
| 327 | goto fail_dispctor; |
| 328 | |
| 329 | if (dev->mode_config.num_crtc) { |
| 330 | ret = nouveau_display_init(dev); |
| 331 | if (ret) |
| 332 | goto fail_dispinit; |
| 333 | } |
| 334 | |
| 335 | nouveau_pm_init(dev); |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 336 | |
| 337 | nouveau_accel_init(drm); |
| 338 | nouveau_fbcon_init(dev); |
Ben Skeggs | 9458029 | 2012-07-06 12:14:00 +1000 | [diff] [blame] | 339 | return 0; |
| 340 | |
Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame] | 341 | fail_dispinit: |
| 342 | nouveau_display_destroy(dev); |
| 343 | fail_dispctor: |
| 344 | nouveau_irq_fini(dev); |
| 345 | fail_irq: |
| 346 | nouveau_bios_takedown(dev); |
| 347 | fail_bios: |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 348 | nouveau_ttm_fini(drm); |
Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame] | 349 | fail_ttm: |
| 350 | nouveau_agp_fini(drm); |
| 351 | nouveau_vga_fini(drm); |
Ben Skeggs | 9458029 | 2012-07-06 12:14:00 +1000 | [diff] [blame] | 352 | fail_device: |
| 353 | nouveau_cli_destroy(&drm->client); |
| 354 | return ret; |
| 355 | } |
| 356 | |
Marcin Slusarz | 5b8a43a | 2012-08-19 23:00:00 +0200 | [diff] [blame] | 357 | static int |
Ben Skeggs | 9458029 | 2012-07-06 12:14:00 +1000 | [diff] [blame] | 358 | nouveau_drm_unload(struct drm_device *dev) |
| 359 | { |
Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame] | 360 | struct nouveau_drm *drm = nouveau_drm(dev); |
Ben Skeggs | 9458029 | 2012-07-06 12:14:00 +1000 | [diff] [blame] | 361 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 362 | nouveau_fbcon_fini(dev); |
| 363 | nouveau_accel_fini(drm); |
| 364 | |
Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame] | 365 | nouveau_pm_fini(dev); |
| 366 | |
Ben Skeggs | 9430738 | 2012-10-31 12:11:15 +1000 | [diff] [blame^] | 367 | if (dev->mode_config.num_crtc) |
| 368 | nouveau_display_fini(dev); |
Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame] | 369 | nouveau_display_destroy(dev); |
| 370 | |
| 371 | nouveau_irq_fini(dev); |
| 372 | nouveau_bios_takedown(dev); |
Ben Skeggs | 9458029 | 2012-07-06 12:14:00 +1000 | [diff] [blame] | 373 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 374 | nouveau_ttm_fini(drm); |
Ben Skeggs | cb75d97 | 2012-07-11 10:44:20 +1000 | [diff] [blame] | 375 | nouveau_agp_fini(drm); |
Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame] | 376 | nouveau_vga_fini(drm); |
Ben Skeggs | cb75d97 | 2012-07-11 10:44:20 +1000 | [diff] [blame] | 377 | |
Ben Skeggs | 9458029 | 2012-07-06 12:14:00 +1000 | [diff] [blame] | 378 | nouveau_cli_destroy(&drm->client); |
| 379 | return 0; |
| 380 | } |
| 381 | |
| 382 | static void |
| 383 | nouveau_drm_remove(struct pci_dev *pdev) |
| 384 | { |
Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame] | 385 | struct drm_device *dev = pci_get_drvdata(pdev); |
| 386 | struct nouveau_drm *drm = nouveau_drm(dev); |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 387 | struct nouveau_object *device; |
Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame] | 388 | |
| 389 | device = drm->client.base.device; |
| 390 | drm_put_dev(dev); |
| 391 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 392 | nouveau_object_ref(NULL, &device); |
| 393 | nouveau_object_debug(); |
Ben Skeggs | 9458029 | 2012-07-06 12:14:00 +1000 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | int |
| 397 | nouveau_drm_suspend(struct pci_dev *pdev, pm_message_t pm_state) |
| 398 | { |
| 399 | struct drm_device *dev = pci_get_drvdata(pdev); |
Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame] | 400 | struct nouveau_drm *drm = nouveau_drm(dev); |
Ben Skeggs | 9458029 | 2012-07-06 12:14:00 +1000 | [diff] [blame] | 401 | struct nouveau_cli *cli; |
| 402 | int ret; |
| 403 | |
| 404 | if (dev->switch_power_state == DRM_SWITCH_POWER_OFF || |
| 405 | pm_state.event == PM_EVENT_PRETHAW) |
| 406 | return 0; |
| 407 | |
Ben Skeggs | 9430738 | 2012-10-31 12:11:15 +1000 | [diff] [blame^] | 408 | if (dev->mode_config.num_crtc) { |
| 409 | NV_INFO(drm, "suspending fbcon...\n"); |
| 410 | nouveau_fbcon_set_suspend(dev, 1); |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 411 | |
Ben Skeggs | 9430738 | 2012-10-31 12:11:15 +1000 | [diff] [blame^] | 412 | NV_INFO(drm, "suspending display...\n"); |
| 413 | ret = nouveau_display_suspend(dev); |
| 414 | if (ret) |
| 415 | return ret; |
| 416 | } |
Ben Skeggs | 9458029 | 2012-07-06 12:14:00 +1000 | [diff] [blame] | 417 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 418 | NV_INFO(drm, "evicting buffers...\n"); |
| 419 | ttm_bo_evict_mm(&drm->ttm.bdev, TTM_PL_VRAM); |
| 420 | |
| 421 | if (drm->fence && nouveau_fence(drm)->suspend) { |
| 422 | if (!nouveau_fence(drm)->suspend(drm)) |
| 423 | return -ENOMEM; |
| 424 | } |
| 425 | |
| 426 | NV_INFO(drm, "suspending client object trees...\n"); |
Ben Skeggs | 9458029 | 2012-07-06 12:14:00 +1000 | [diff] [blame] | 427 | list_for_each_entry(cli, &drm->clients, head) { |
| 428 | ret = nouveau_client_fini(&cli->base, true); |
| 429 | if (ret) |
| 430 | goto fail_client; |
| 431 | } |
| 432 | |
| 433 | ret = nouveau_client_fini(&drm->client.base, true); |
| 434 | if (ret) |
| 435 | goto fail_client; |
| 436 | |
Ben Skeggs | cb75d97 | 2012-07-11 10:44:20 +1000 | [diff] [blame] | 437 | nouveau_agp_fini(drm); |
| 438 | |
Ben Skeggs | 9458029 | 2012-07-06 12:14:00 +1000 | [diff] [blame] | 439 | pci_save_state(pdev); |
| 440 | if (pm_state.event == PM_EVENT_SUSPEND) { |
| 441 | pci_disable_device(pdev); |
| 442 | pci_set_power_state(pdev, PCI_D3hot); |
| 443 | } |
| 444 | |
| 445 | return 0; |
| 446 | |
| 447 | fail_client: |
| 448 | list_for_each_entry_continue_reverse(cli, &drm->clients, head) { |
| 449 | nouveau_client_init(&cli->base); |
| 450 | } |
| 451 | |
Ben Skeggs | 9430738 | 2012-10-31 12:11:15 +1000 | [diff] [blame^] | 452 | if (dev->mode_config.num_crtc) { |
| 453 | NV_INFO(drm, "resuming display...\n"); |
| 454 | nouveau_display_resume(dev); |
| 455 | } |
Ben Skeggs | 9458029 | 2012-07-06 12:14:00 +1000 | [diff] [blame] | 456 | return ret; |
| 457 | } |
| 458 | |
| 459 | int |
| 460 | nouveau_drm_resume(struct pci_dev *pdev) |
| 461 | { |
| 462 | struct drm_device *dev = pci_get_drvdata(pdev); |
Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame] | 463 | struct nouveau_drm *drm = nouveau_drm(dev); |
Ben Skeggs | 9458029 | 2012-07-06 12:14:00 +1000 | [diff] [blame] | 464 | struct nouveau_cli *cli; |
| 465 | int ret; |
| 466 | |
| 467 | if (dev->switch_power_state == DRM_SWITCH_POWER_OFF) |
| 468 | return 0; |
| 469 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 470 | NV_INFO(drm, "re-enabling device...\n"); |
Ben Skeggs | 9458029 | 2012-07-06 12:14:00 +1000 | [diff] [blame] | 471 | pci_set_power_state(pdev, PCI_D0); |
| 472 | pci_restore_state(pdev); |
| 473 | ret = pci_enable_device(pdev); |
| 474 | if (ret) |
| 475 | return ret; |
| 476 | pci_set_master(pdev); |
| 477 | |
Ben Skeggs | cb75d97 | 2012-07-11 10:44:20 +1000 | [diff] [blame] | 478 | nouveau_agp_reset(drm); |
| 479 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 480 | NV_INFO(drm, "resuming client object trees...\n"); |
Ben Skeggs | 9458029 | 2012-07-06 12:14:00 +1000 | [diff] [blame] | 481 | nouveau_client_init(&drm->client.base); |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 482 | nouveau_agp_init(drm); |
Ben Skeggs | 9458029 | 2012-07-06 12:14:00 +1000 | [diff] [blame] | 483 | |
| 484 | list_for_each_entry(cli, &drm->clients, head) { |
| 485 | nouveau_client_init(&cli->base); |
| 486 | } |
| 487 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 488 | if (drm->fence && nouveau_fence(drm)->resume) |
| 489 | nouveau_fence(drm)->resume(drm); |
Ben Skeggs | cb75d97 | 2012-07-11 10:44:20 +1000 | [diff] [blame] | 490 | |
Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame] | 491 | nouveau_run_vbios_init(dev); |
| 492 | nouveau_irq_postinstall(dev); |
| 493 | nouveau_pm_resume(dev); |
| 494 | |
Ben Skeggs | 9430738 | 2012-10-31 12:11:15 +1000 | [diff] [blame^] | 495 | if (dev->mode_config.num_crtc) { |
| 496 | NV_INFO(drm, "resuming display...\n"); |
| 497 | nouveau_display_resume(dev); |
| 498 | } |
Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame] | 499 | return 0; |
Ben Skeggs | 9458029 | 2012-07-06 12:14:00 +1000 | [diff] [blame] | 500 | } |
| 501 | |
Marcin Slusarz | 5b8a43a | 2012-08-19 23:00:00 +0200 | [diff] [blame] | 502 | static int |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 503 | nouveau_drm_open(struct drm_device *dev, struct drm_file *fpriv) |
| 504 | { |
| 505 | struct pci_dev *pdev = dev->pdev; |
| 506 | struct nouveau_drm *drm = nouveau_drm(dev); |
| 507 | struct nouveau_cli *cli; |
Ben Skeggs | fa6df8c | 2012-09-12 13:09:23 +1000 | [diff] [blame] | 508 | char name[16]; |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 509 | int ret; |
| 510 | |
Linus Torvalds | 612a9aa | 2012-10-03 23:29:23 -0700 | [diff] [blame] | 511 | snprintf(name, sizeof(name), "%d", pid_nr(fpriv->pid)); |
Ben Skeggs | fa6df8c | 2012-09-12 13:09:23 +1000 | [diff] [blame] | 512 | |
| 513 | ret = nouveau_cli_create(pdev, name, sizeof(*cli), (void **)&cli); |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 514 | if (ret) |
| 515 | return ret; |
| 516 | |
| 517 | if (nv_device(drm->device)->card_type >= NV_50) { |
| 518 | ret = nouveau_vm_new(nv_device(drm->device), 0, (1ULL << 40), |
| 519 | 0x1000, &cli->base.vm); |
| 520 | if (ret) { |
| 521 | nouveau_cli_destroy(cli); |
| 522 | return ret; |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | fpriv->driver_priv = cli; |
| 527 | |
| 528 | mutex_lock(&drm->client.mutex); |
| 529 | list_add(&cli->head, &drm->clients); |
| 530 | mutex_unlock(&drm->client.mutex); |
| 531 | return 0; |
| 532 | } |
| 533 | |
Marcin Slusarz | 5b8a43a | 2012-08-19 23:00:00 +0200 | [diff] [blame] | 534 | static void |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 535 | nouveau_drm_preclose(struct drm_device *dev, struct drm_file *fpriv) |
| 536 | { |
| 537 | struct nouveau_cli *cli = nouveau_cli(fpriv); |
| 538 | struct nouveau_drm *drm = nouveau_drm(dev); |
| 539 | |
| 540 | if (cli->abi16) |
| 541 | nouveau_abi16_fini(cli->abi16); |
| 542 | |
| 543 | mutex_lock(&drm->client.mutex); |
| 544 | list_del(&cli->head); |
| 545 | mutex_unlock(&drm->client.mutex); |
| 546 | } |
| 547 | |
Marcin Slusarz | 5b8a43a | 2012-08-19 23:00:00 +0200 | [diff] [blame] | 548 | static void |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 549 | nouveau_drm_postclose(struct drm_device *dev, struct drm_file *fpriv) |
| 550 | { |
| 551 | struct nouveau_cli *cli = nouveau_cli(fpriv); |
| 552 | nouveau_cli_destroy(cli); |
| 553 | } |
| 554 | |
Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame] | 555 | static struct drm_ioctl_desc |
| 556 | nouveau_ioctls[] = { |
| 557 | DRM_IOCTL_DEF_DRV(NOUVEAU_GETPARAM, nouveau_abi16_ioctl_getparam, DRM_UNLOCKED|DRM_AUTH), |
| 558 | DRM_IOCTL_DEF_DRV(NOUVEAU_SETPARAM, nouveau_abi16_ioctl_setparam, DRM_UNLOCKED|DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY), |
| 559 | DRM_IOCTL_DEF_DRV(NOUVEAU_CHANNEL_ALLOC, nouveau_abi16_ioctl_channel_alloc, DRM_UNLOCKED|DRM_AUTH), |
| 560 | DRM_IOCTL_DEF_DRV(NOUVEAU_CHANNEL_FREE, nouveau_abi16_ioctl_channel_free, DRM_UNLOCKED|DRM_AUTH), |
| 561 | DRM_IOCTL_DEF_DRV(NOUVEAU_GROBJ_ALLOC, nouveau_abi16_ioctl_grobj_alloc, DRM_UNLOCKED|DRM_AUTH), |
| 562 | DRM_IOCTL_DEF_DRV(NOUVEAU_NOTIFIEROBJ_ALLOC, nouveau_abi16_ioctl_notifierobj_alloc, DRM_UNLOCKED|DRM_AUTH), |
| 563 | DRM_IOCTL_DEF_DRV(NOUVEAU_GPUOBJ_FREE, nouveau_abi16_ioctl_gpuobj_free, DRM_UNLOCKED|DRM_AUTH), |
| 564 | DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_NEW, nouveau_gem_ioctl_new, DRM_UNLOCKED|DRM_AUTH), |
| 565 | DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_PUSHBUF, nouveau_gem_ioctl_pushbuf, DRM_UNLOCKED|DRM_AUTH), |
| 566 | DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_CPU_PREP, nouveau_gem_ioctl_cpu_prep, DRM_UNLOCKED|DRM_AUTH), |
| 567 | DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_CPU_FINI, nouveau_gem_ioctl_cpu_fini, DRM_UNLOCKED|DRM_AUTH), |
| 568 | DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_INFO, nouveau_gem_ioctl_info, DRM_UNLOCKED|DRM_AUTH), |
| 569 | }; |
| 570 | |
| 571 | static const struct file_operations |
| 572 | nouveau_driver_fops = { |
| 573 | .owner = THIS_MODULE, |
| 574 | .open = drm_open, |
| 575 | .release = drm_release, |
| 576 | .unlocked_ioctl = drm_ioctl, |
| 577 | .mmap = nouveau_ttm_mmap, |
| 578 | .poll = drm_poll, |
| 579 | .fasync = drm_fasync, |
| 580 | .read = drm_read, |
| 581 | #if defined(CONFIG_COMPAT) |
| 582 | .compat_ioctl = nouveau_compat_ioctl, |
| 583 | #endif |
| 584 | .llseek = noop_llseek, |
| 585 | }; |
| 586 | |
| 587 | static struct drm_driver |
| 588 | driver = { |
| 589 | .driver_features = |
| 590 | DRIVER_USE_AGP | DRIVER_PCI_DMA | DRIVER_SG | |
| 591 | DRIVER_HAVE_IRQ | DRIVER_IRQ_SHARED | DRIVER_GEM | |
| 592 | DRIVER_MODESET | DRIVER_PRIME, |
| 593 | |
| 594 | .load = nouveau_drm_load, |
| 595 | .unload = nouveau_drm_unload, |
| 596 | .open = nouveau_drm_open, |
| 597 | .preclose = nouveau_drm_preclose, |
| 598 | .postclose = nouveau_drm_postclose, |
| 599 | .lastclose = nouveau_vga_lastclose, |
| 600 | |
| 601 | .irq_preinstall = nouveau_irq_preinstall, |
| 602 | .irq_postinstall = nouveau_irq_postinstall, |
| 603 | .irq_uninstall = nouveau_irq_uninstall, |
| 604 | .irq_handler = nouveau_irq_handler, |
| 605 | |
| 606 | .get_vblank_counter = drm_vblank_count, |
| 607 | .enable_vblank = nouveau_vblank_enable, |
| 608 | .disable_vblank = nouveau_vblank_disable, |
| 609 | |
| 610 | .ioctls = nouveau_ioctls, |
| 611 | .fops = &nouveau_driver_fops, |
| 612 | |
| 613 | .prime_handle_to_fd = drm_gem_prime_handle_to_fd, |
| 614 | .prime_fd_to_handle = drm_gem_prime_fd_to_handle, |
| 615 | .gem_prime_export = nouveau_gem_prime_export, |
| 616 | .gem_prime_import = nouveau_gem_prime_import, |
| 617 | |
| 618 | .gem_init_object = nouveau_gem_object_new, |
| 619 | .gem_free_object = nouveau_gem_object_del, |
| 620 | .gem_open_object = nouveau_gem_object_open, |
| 621 | .gem_close_object = nouveau_gem_object_close, |
| 622 | |
| 623 | .dumb_create = nouveau_display_dumb_create, |
| 624 | .dumb_map_offset = nouveau_display_dumb_map_offset, |
| 625 | .dumb_destroy = nouveau_display_dumb_destroy, |
| 626 | |
| 627 | .name = DRIVER_NAME, |
| 628 | .desc = DRIVER_DESC, |
| 629 | #ifdef GIT_REVISION |
| 630 | .date = GIT_REVISION, |
| 631 | #else |
| 632 | .date = DRIVER_DATE, |
| 633 | #endif |
| 634 | .major = DRIVER_MAJOR, |
| 635 | .minor = DRIVER_MINOR, |
| 636 | .patchlevel = DRIVER_PATCHLEVEL, |
| 637 | }; |
| 638 | |
Ben Skeggs | 9458029 | 2012-07-06 12:14:00 +1000 | [diff] [blame] | 639 | static struct pci_device_id |
| 640 | nouveau_drm_pci_table[] = { |
| 641 | { |
| 642 | PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_ANY_ID), |
| 643 | .class = PCI_BASE_CLASS_DISPLAY << 16, |
| 644 | .class_mask = 0xff << 16, |
| 645 | }, |
| 646 | { |
| 647 | PCI_DEVICE(PCI_VENDOR_ID_NVIDIA_SGS, PCI_ANY_ID), |
| 648 | .class = PCI_BASE_CLASS_DISPLAY << 16, |
| 649 | .class_mask = 0xff << 16, |
| 650 | }, |
| 651 | {} |
| 652 | }; |
| 653 | |
| 654 | static struct pci_driver |
| 655 | nouveau_drm_pci_driver = { |
| 656 | .name = "nouveau", |
| 657 | .id_table = nouveau_drm_pci_table, |
| 658 | .probe = nouveau_drm_probe, |
| 659 | .remove = nouveau_drm_remove, |
| 660 | .suspend = nouveau_drm_suspend, |
| 661 | .resume = nouveau_drm_resume, |
| 662 | }; |
| 663 | |
| 664 | static int __init |
| 665 | nouveau_drm_init(void) |
| 666 | { |
Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame] | 667 | driver.num_ioctls = ARRAY_SIZE(nouveau_ioctls); |
| 668 | |
| 669 | if (nouveau_modeset == -1) { |
| 670 | #ifdef CONFIG_VGA_CONSOLE |
| 671 | if (vgacon_text_force()) |
| 672 | nouveau_modeset = 0; |
| 673 | else |
| 674 | #endif |
| 675 | nouveau_modeset = 1; |
| 676 | } |
| 677 | |
| 678 | if (!nouveau_modeset) |
| 679 | return 0; |
| 680 | |
| 681 | nouveau_register_dsm_handler(); |
| 682 | return drm_pci_init(&driver, &nouveau_drm_pci_driver); |
Ben Skeggs | 9458029 | 2012-07-06 12:14:00 +1000 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | static void __exit |
| 686 | nouveau_drm_exit(void) |
| 687 | { |
Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame] | 688 | if (!nouveau_modeset) |
| 689 | return; |
| 690 | |
| 691 | drm_pci_exit(&driver, &nouveau_drm_pci_driver); |
| 692 | nouveau_unregister_dsm_handler(); |
Ben Skeggs | 9458029 | 2012-07-06 12:14:00 +1000 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | module_init(nouveau_drm_init); |
| 696 | module_exit(nouveau_drm_exit); |
| 697 | |
| 698 | MODULE_DEVICE_TABLE(pci, nouveau_drm_pci_table); |
Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame] | 699 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 700 | MODULE_DESCRIPTION(DRIVER_DESC); |
Ben Skeggs | 9458029 | 2012-07-06 12:14:00 +1000 | [diff] [blame] | 701 | MODULE_LICENSE("GPL and additional rights"); |