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