blob: 309be93de597e1cc735d384817ab20ca28727a90 [file] [log] [blame]
Ben Skeggs94580292012-07-06 12:14:00 +10001/*
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 Skeggs77145f12012-07-31 16:16:21 +100025#include <linux/console.h>
Ben Skeggs94580292012-07-06 12:14:00 +100026#include <linux/module.h>
27#include <linux/pci.h>
Dave Airlie5addcf02012-09-10 14:20:51 +100028#include <linux/pm_runtime.h>
29#include <linux/vga_switcheroo.h>
30#include "drmP.h"
31#include "drm_crtc_helper.h"
Ben Skeggs94580292012-07-06 12:14:00 +100032#include <core/device.h>
Ben Skeggsebb945a2012-07-20 08:17:34 +100033#include <core/gpuobj.h>
Ben Skeggs94580292012-07-06 12:14:00 +100034#include <core/class.h>
Ilia Mirkinc33e05a2014-02-13 21:35:14 -050035#include <core/option.h>
Ben Skeggs94580292012-07-06 12:14:00 +100036
Ben Skeggs94580292012-07-06 12:14:00 +100037#include "nouveau_drm.h"
Ben Skeggsebb945a2012-07-20 08:17:34 +100038#include "nouveau_dma.h"
Ben Skeggs77145f12012-07-31 16:16:21 +100039#include "nouveau_ttm.h"
40#include "nouveau_gem.h"
Ben Skeggscb75d972012-07-11 10:44:20 +100041#include "nouveau_agp.h"
Ben Skeggs77145f12012-07-31 16:16:21 +100042#include "nouveau_vga.h"
Ben Skeggs26fdd782013-10-15 11:52:56 +100043#include "nouveau_sysfs.h"
Ben Skeggsb9ed9192013-10-15 09:44:02 +100044#include "nouveau_hwmon.h"
Ben Skeggs77145f12012-07-31 16:16:21 +100045#include "nouveau_acpi.h"
46#include "nouveau_bios.h"
47#include "nouveau_ioctl.h"
Ben Skeggsebb945a2012-07-20 08:17:34 +100048#include "nouveau_abi16.h"
49#include "nouveau_fbcon.h"
50#include "nouveau_fence.h"
Marcin Slusarz33b903e2013-02-08 21:42:13 +010051#include "nouveau_debugfs.h"
Ben Skeggsebb945a2012-07-20 08:17:34 +100052
Ben Skeggs94580292012-07-06 12:14:00 +100053MODULE_PARM_DESC(config, "option string to pass to driver core");
54static char *nouveau_config;
55module_param_named(config, nouveau_config, charp, 0400);
56
57MODULE_PARM_DESC(debug, "debug string to pass to driver core");
58static char *nouveau_debug;
59module_param_named(debug, nouveau_debug, charp, 0400);
60
Ben Skeggsebb945a2012-07-20 08:17:34 +100061MODULE_PARM_DESC(noaccel, "disable kernel/abi16 acceleration");
62static int nouveau_noaccel = 0;
63module_param_named(noaccel, nouveau_noaccel, int, 0400);
64
Ben Skeggs94307382012-10-31 12:11:15 +100065MODULE_PARM_DESC(modeset, "enable driver (default: auto, "
66 "0 = disabled, 1 = enabled, 2 = headless)");
67int nouveau_modeset = -1;
Ben Skeggs77145f12012-07-31 16:16:21 +100068module_param_named(modeset, nouveau_modeset, int, 0400);
69
Dave Airlie5addcf02012-09-10 14:20:51 +100070MODULE_PARM_DESC(runpm, "disable (0), force enable (1), optimus only default (-1)");
71int nouveau_runtime_pm = -1;
72module_param_named(runpm, nouveau_runtime_pm, int, 0400);
73
Ben Skeggs77145f12012-07-31 16:16:21 +100074static struct drm_driver driver;
75
Ben Skeggs94580292012-07-06 12:14:00 +100076static u64
Alexandre Courbot420b9462014-02-17 15:17:26 +090077nouveau_pci_name(struct pci_dev *pdev)
Ben Skeggs94580292012-07-06 12:14:00 +100078{
79 u64 name = (u64)pci_domain_nr(pdev->bus) << 32;
80 name |= pdev->bus->number << 16;
81 name |= PCI_SLOT(pdev->devfn) << 8;
82 return name | PCI_FUNC(pdev->devfn);
83}
84
Alexandre Courbot420b9462014-02-17 15:17:26 +090085static u64
86nouveau_platform_name(struct platform_device *platformdev)
87{
88 return platformdev->id;
89}
90
91static u64
92nouveau_name(struct drm_device *dev)
93{
94 if (dev->pdev)
95 return nouveau_pci_name(dev->pdev);
96 else
97 return nouveau_platform_name(dev->platformdev);
98}
99
Ben Skeggs94580292012-07-06 12:14:00 +1000100static int
Alexandre Courbot420b9462014-02-17 15:17:26 +0900101nouveau_cli_create(u64 name, const char *sname,
Ben Skeggsfa6df8c2012-09-12 13:09:23 +1000102 int size, void **pcli)
Ben Skeggs94580292012-07-06 12:14:00 +1000103{
Ben Skeggs0ad72862014-08-10 04:10:22 +1000104 struct nouveau_cli *cli = *pcli = kzalloc(size, GFP_KERNEL);
105 if (cli) {
106 int ret = nvif_client_init(NULL, NULL, sname, name,
107 nouveau_config, nouveau_debug,
108 &cli->base);
109 if (ret == 0)
110 mutex_init(&cli->mutex);
Ben Skeggs94580292012-07-06 12:14:00 +1000111 return ret;
Marcin Slusarzdd5700e2012-12-10 21:59:52 +0100112 }
Ben Skeggs0ad72862014-08-10 04:10:22 +1000113 return -ENOMEM;
Ben Skeggs94580292012-07-06 12:14:00 +1000114}
115
116static void
117nouveau_cli_destroy(struct nouveau_cli *cli)
118{
Ben Skeggs0ad72862014-08-10 04:10:22 +1000119 nouveau_vm_ref(NULL, &nvkm_client(&cli->base)->vm, NULL);
120 nvif_client_fini(&cli->base);
Ben Skeggs94580292012-07-06 12:14:00 +1000121}
122
Ben Skeggsebb945a2012-07-20 08:17:34 +1000123static void
124nouveau_accel_fini(struct nouveau_drm *drm)
125{
Ben Skeggsebb945a2012-07-20 08:17:34 +1000126 nouveau_channel_del(&drm->channel);
Ben Skeggs0ad72862014-08-10 04:10:22 +1000127 nvif_object_fini(&drm->ntfy);
128 nouveau_gpuobj_ref(NULL, &drm->notify);
129 nvif_object_fini(&drm->nvsw);
Ben Skeggs49981042012-08-06 19:38:25 +1000130 nouveau_channel_del(&drm->cechan);
Ben Skeggs0ad72862014-08-10 04:10:22 +1000131 nvif_object_fini(&drm->ttm.copy);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000132 if (drm->fence)
133 nouveau_fence(drm)->dtor(drm);
134}
135
136static void
137nouveau_accel_init(struct nouveau_drm *drm)
138{
Ben Skeggs967e7bd2014-08-10 04:10:22 +1000139 struct nvif_device *device = &drm->device;
Ben Skeggs49981042012-08-06 19:38:25 +1000140 u32 arg0, arg1;
Ben Skeggs967e7bd2014-08-10 04:10:22 +1000141 u32 sclass[16];
142 int ret, i;
Ben Skeggsebb945a2012-07-20 08:17:34 +1000143
Ben Skeggs967e7bd2014-08-10 04:10:22 +1000144 if (nouveau_noaccel)
Ben Skeggsebb945a2012-07-20 08:17:34 +1000145 return;
146
147 /* initialise synchronisation routines */
Ben Skeggs967e7bd2014-08-10 04:10:22 +1000148 /*XXX: this is crap, but the fence/channel stuff is a little
149 * backwards in some places. this will be fixed.
150 */
Ben Skeggs0ad72862014-08-10 04:10:22 +1000151 ret = nvif_object_sclass(&device->base, sclass, ARRAY_SIZE(sclass));
Ben Skeggs967e7bd2014-08-10 04:10:22 +1000152 if (ret < 0)
153 return;
154
155 for (ret = -ENOSYS, i = 0; ret && i < ARRAY_SIZE(sclass); i++) {
156 switch (sclass[i]) {
157 case NV03_CHANNEL_DMA_CLASS:
158 ret = nv04_fence_create(drm);
159 break;
160 case NV10_CHANNEL_DMA_CLASS:
161 ret = nv10_fence_create(drm);
162 break;
163 case NV17_CHANNEL_DMA_CLASS:
164 case NV40_CHANNEL_DMA_CLASS:
165 ret = nv17_fence_create(drm);
166 break;
167 case NV50_CHANNEL_IND_CLASS:
168 ret = nv50_fence_create(drm);
169 break;
170 case NV84_CHANNEL_IND_CLASS:
171 ret = nv84_fence_create(drm);
172 break;
173 case NVC0_CHANNEL_IND_CLASS:
174 case NVE0_CHANNEL_IND_CLASS:
175 ret = nvc0_fence_create(drm);
176 break;
177 default:
178 break;
179 }
180 }
181
Ben Skeggsebb945a2012-07-20 08:17:34 +1000182 if (ret) {
183 NV_ERROR(drm, "failed to initialise sync subsystem, %d\n", ret);
184 nouveau_accel_fini(drm);
185 return;
186 }
187
Ben Skeggs967e7bd2014-08-10 04:10:22 +1000188 if (device->info.family >= NV_DEVICE_INFO_V0_KEPLER) {
Ben Skeggs0ad72862014-08-10 04:10:22 +1000189 ret = nouveau_channel_new(drm, &drm->device, NVDRM_CHAN + 1,
Ben Skeggs49981042012-08-06 19:38:25 +1000190 NVE0_CHANNEL_IND_ENGINE_CE0 |
191 NVE0_CHANNEL_IND_ENGINE_CE1, 0,
192 &drm->cechan);
193 if (ret)
194 NV_ERROR(drm, "failed to create ce channel, %d\n", ret);
195
196 arg0 = NVE0_CHANNEL_IND_ENGINE_GR;
Ben Skeggs49469802012-11-22 13:43:55 +1000197 arg1 = 1;
Ben Skeggs00fc6f62013-07-09 14:20:15 +1000198 } else
Ben Skeggs967e7bd2014-08-10 04:10:22 +1000199 if (device->info.chipset >= 0xa3 &&
200 device->info.chipset != 0xaa &&
201 device->info.chipset != 0xac) {
Ben Skeggs0ad72862014-08-10 04:10:22 +1000202 ret = nouveau_channel_new(drm, &drm->device, NVDRM_CHAN + 1,
203 NvDmaFB, NvDmaTT, &drm->cechan);
Ben Skeggs00fc6f62013-07-09 14:20:15 +1000204 if (ret)
205 NV_ERROR(drm, "failed to create ce channel, %d\n", ret);
206
207 arg0 = NvDmaFB;
208 arg1 = NvDmaTT;
Ben Skeggs49981042012-08-06 19:38:25 +1000209 } else {
210 arg0 = NvDmaFB;
211 arg1 = NvDmaTT;
212 }
213
Ben Skeggs0ad72862014-08-10 04:10:22 +1000214 ret = nouveau_channel_new(drm, &drm->device, NVDRM_CHAN, arg0, arg1,
215 &drm->channel);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000216 if (ret) {
217 NV_ERROR(drm, "failed to create kernel channel, %d\n", ret);
218 nouveau_accel_fini(drm);
219 return;
220 }
221
Ben Skeggs0ad72862014-08-10 04:10:22 +1000222 ret = nvif_object_init(drm->channel->object, NULL, NVDRM_NVSW,
223 nouveau_abi16_swclass(drm), NULL, 0, &drm->nvsw);
Ben Skeggs69a61462013-11-13 10:58:51 +1000224 if (ret == 0) {
Ben Skeggs0ad72862014-08-10 04:10:22 +1000225 struct nouveau_software_chan *swch;
Ben Skeggs69a61462013-11-13 10:58:51 +1000226 ret = RING_SPACE(drm->channel, 2);
227 if (ret == 0) {
Ben Skeggs967e7bd2014-08-10 04:10:22 +1000228 if (device->info.family < NV_DEVICE_INFO_V0_FERMI) {
Ben Skeggs69a61462013-11-13 10:58:51 +1000229 BEGIN_NV04(drm->channel, NvSubSw, 0, 1);
230 OUT_RING (drm->channel, NVDRM_NVSW);
231 } else
Ben Skeggs967e7bd2014-08-10 04:10:22 +1000232 if (device->info.family < NV_DEVICE_INFO_V0_KEPLER) {
Ben Skeggs69a61462013-11-13 10:58:51 +1000233 BEGIN_NVC0(drm->channel, FermiSw, 0, 1);
234 OUT_RING (drm->channel, 0x001f0000);
235 }
236 }
Ben Skeggs0ad72862014-08-10 04:10:22 +1000237 swch = (void *)nvkm_object(&drm->nvsw)->parent;
Ben Skeggs69a61462013-11-13 10:58:51 +1000238 swch->flip = nouveau_flip_complete;
239 swch->flip_data = drm->channel;
240 }
241
242 if (ret) {
243 NV_ERROR(drm, "failed to allocate software object, %d\n", ret);
244 nouveau_accel_fini(drm);
245 return;
246 }
247
Ben Skeggs967e7bd2014-08-10 04:10:22 +1000248 if (device->info.family < NV_DEVICE_INFO_V0_FERMI) {
249 ret = nouveau_gpuobj_new(nvkm_object(&drm->device), NULL, 32,
250 0, 0, &drm->notify);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000251 if (ret) {
252 NV_ERROR(drm, "failed to allocate notifier, %d\n", ret);
253 nouveau_accel_fini(drm);
254 return;
255 }
256
Ben Skeggs0ad72862014-08-10 04:10:22 +1000257 ret = nvif_object_init(drm->channel->object, NULL, NvNotify0,
258 NV_DMA_IN_MEMORY_CLASS,
259 &(struct nv_dma_class) {
Ben Skeggsebb945a2012-07-20 08:17:34 +1000260 .flags = NV_DMA_TARGET_VRAM |
261 NV_DMA_ACCESS_RDWR,
262 .start = drm->notify->addr,
263 .limit = drm->notify->addr + 31
Ben Skeggs0ad72862014-08-10 04:10:22 +1000264 }, sizeof(struct nv_dma_class),
265 &drm->ntfy);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000266 if (ret) {
267 nouveau_accel_fini(drm);
268 return;
269 }
270 }
271
272
Ben Skeggs49981042012-08-06 19:38:25 +1000273 nouveau_bo_move_init(drm);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000274}
275
Greg Kroah-Hartman56550d92012-12-21 15:09:25 -0800276static int nouveau_drm_probe(struct pci_dev *pdev,
277 const struct pci_device_id *pent)
Ben Skeggs94580292012-07-06 12:14:00 +1000278{
279 struct nouveau_device *device;
Ben Skeggsebb945a2012-07-20 08:17:34 +1000280 struct apertures_struct *aper;
281 bool boot = false;
Ben Skeggs94580292012-07-06 12:14:00 +1000282 int ret;
283
Ben Skeggsebb945a2012-07-20 08:17:34 +1000284 /* remove conflicting drivers (vesafb, efifb etc) */
285 aper = alloc_apertures(3);
286 if (!aper)
287 return -ENOMEM;
288
289 aper->ranges[0].base = pci_resource_start(pdev, 1);
290 aper->ranges[0].size = pci_resource_len(pdev, 1);
291 aper->count = 1;
292
293 if (pci_resource_len(pdev, 2)) {
294 aper->ranges[aper->count].base = pci_resource_start(pdev, 2);
295 aper->ranges[aper->count].size = pci_resource_len(pdev, 2);
296 aper->count++;
297 }
298
299 if (pci_resource_len(pdev, 3)) {
300 aper->ranges[aper->count].base = pci_resource_start(pdev, 3);
301 aper->ranges[aper->count].size = pci_resource_len(pdev, 3);
302 aper->count++;
303 }
304
305#ifdef CONFIG_X86
306 boot = pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW;
307#endif
308 remove_conflicting_framebuffers(aper, "nouveaufb", boot);
Tommi Rantala83ef7772012-11-09 09:19:40 +0000309 kfree(aper);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000310
Alexandre Courbot420b9462014-02-17 15:17:26 +0900311 ret = nouveau_device_create(pdev, NOUVEAU_BUS_PCI,
312 nouveau_pci_name(pdev), pci_name(pdev),
Ben Skeggs94580292012-07-06 12:14:00 +1000313 nouveau_config, nouveau_debug, &device);
314 if (ret)
315 return ret;
316
317 pci_set_master(pdev);
318
Ben Skeggs77145f12012-07-31 16:16:21 +1000319 ret = drm_get_pci_dev(pdev, pent, &driver);
Ben Skeggs94580292012-07-06 12:14:00 +1000320 if (ret) {
Ben Skeggsebb945a2012-07-20 08:17:34 +1000321 nouveau_object_ref(NULL, (struct nouveau_object **)&device);
Ben Skeggs94580292012-07-06 12:14:00 +1000322 return ret;
323 }
324
325 return 0;
326}
327
Dave Airlie5addcf02012-09-10 14:20:51 +1000328#define PCI_CLASS_MULTIMEDIA_HD_AUDIO 0x0403
329
330static void
Ilia Mirkin46941b02014-02-13 03:39:50 -0500331nouveau_get_hdmi_dev(struct nouveau_drm *drm)
Dave Airlie5addcf02012-09-10 14:20:51 +1000332{
Ilia Mirkin46941b02014-02-13 03:39:50 -0500333 struct pci_dev *pdev = drm->dev->pdev;
Dave Airlie5addcf02012-09-10 14:20:51 +1000334
Alexandre Courbot420b9462014-02-17 15:17:26 +0900335 if (!pdev) {
Alexandre Courbot40189b02014-03-24 17:42:23 +0900336 DRM_INFO("not a PCI device; no HDMI\n");
Alexandre Courbot420b9462014-02-17 15:17:26 +0900337 drm->hdmi_device = NULL;
338 return;
339 }
340
Dave Airlie5addcf02012-09-10 14:20:51 +1000341 /* subfunction one is a hdmi audio device? */
342 drm->hdmi_device = pci_get_bus_and_slot((unsigned int)pdev->bus->number,
343 PCI_DEVFN(PCI_SLOT(pdev->devfn), 1));
344
345 if (!drm->hdmi_device) {
Ilia Mirkin46941b02014-02-13 03:39:50 -0500346 NV_DEBUG(drm, "hdmi device not found %d %d %d\n", pdev->bus->number, PCI_SLOT(pdev->devfn), 1);
Dave Airlie5addcf02012-09-10 14:20:51 +1000347 return;
348 }
349
350 if ((drm->hdmi_device->class >> 8) != PCI_CLASS_MULTIMEDIA_HD_AUDIO) {
Ilia Mirkin46941b02014-02-13 03:39:50 -0500351 NV_DEBUG(drm, "possible hdmi device not audio %d\n", drm->hdmi_device->class);
Dave Airlie5addcf02012-09-10 14:20:51 +1000352 pci_dev_put(drm->hdmi_device);
353 drm->hdmi_device = NULL;
354 return;
355 }
356}
357
Marcin Slusarz5b8a43a2012-08-19 23:00:00 +0200358static int
Ben Skeggs94580292012-07-06 12:14:00 +1000359nouveau_drm_load(struct drm_device *dev, unsigned long flags)
360{
361 struct pci_dev *pdev = dev->pdev;
362 struct nouveau_drm *drm;
363 int ret;
364
Alexandre Courbot420b9462014-02-17 15:17:26 +0900365 ret = nouveau_cli_create(nouveau_name(dev), "DRM", sizeof(*drm),
366 (void **)&drm);
Ben Skeggs94580292012-07-06 12:14:00 +1000367 if (ret)
368 return ret;
369
Ben Skeggs77145f12012-07-31 16:16:21 +1000370 dev->dev_private = drm;
371 drm->dev = dev;
Ben Skeggs0ad72862014-08-10 04:10:22 +1000372 nvkm_client(&drm->client.base)->debug =
373 nouveau_dbgopt(nouveau_debug, "DRM");
Ben Skeggs77145f12012-07-31 16:16:21 +1000374
Ben Skeggs94580292012-07-06 12:14:00 +1000375 INIT_LIST_HEAD(&drm->clients);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000376 spin_lock_init(&drm->tile.lock);
Ben Skeggs94580292012-07-06 12:14:00 +1000377
Ilia Mirkin46941b02014-02-13 03:39:50 -0500378 nouveau_get_hdmi_dev(drm);
Dave Airlie5addcf02012-09-10 14:20:51 +1000379
Ben Skeggscb75d972012-07-11 10:44:20 +1000380 /* make sure AGP controller is in a consistent state before we
381 * (possibly) execute vbios init tables (see nouveau_agp.h)
382 */
Alexandre Courbot420b9462014-02-17 15:17:26 +0900383 if (pdev && drm_pci_device_is_agp(dev) && dev->agp) {
Ben Skeggscb75d972012-07-11 10:44:20 +1000384 /* dummy device object, doesn't init anything, but allows
385 * agp code access to registers
386 */
Ben Skeggs0ad72862014-08-10 04:10:22 +1000387 ret = nvif_device_init(&drm->client.base.base, NULL,
388 NVDRM_DEVICE, NV_DEVICE_CLASS,
389 &(struct nv_device_class) {
Ben Skeggscb75d972012-07-11 10:44:20 +1000390 .device = ~0,
391 .disable =
392 ~(NV_DEVICE_DISABLE_MMIO |
393 NV_DEVICE_DISABLE_IDENTIFY),
394 .debug0 = ~0,
Ben Skeggs0ad72862014-08-10 04:10:22 +1000395 }, sizeof(struct nv_device_class),
396 &drm->device);
Ben Skeggscb75d972012-07-11 10:44:20 +1000397 if (ret)
Ben Skeggsebb945a2012-07-20 08:17:34 +1000398 goto fail_device;
Ben Skeggscb75d972012-07-11 10:44:20 +1000399
400 nouveau_agp_reset(drm);
Ben Skeggs0ad72862014-08-10 04:10:22 +1000401 nvif_device_fini(&drm->device);
Ben Skeggscb75d972012-07-11 10:44:20 +1000402 }
403
Ben Skeggs0ad72862014-08-10 04:10:22 +1000404 ret = nvif_device_init(&drm->client.base.base, NULL, NVDRM_DEVICE,
405 NV_DEVICE_CLASS,
406 &(struct nv_device_class) {
Ben Skeggs94580292012-07-06 12:14:00 +1000407 .device = ~0,
408 .disable = 0,
409 .debug0 = 0,
Ben Skeggs0ad72862014-08-10 04:10:22 +1000410 }, sizeof(struct nv_device_class),
411 &drm->device);
Ben Skeggs94580292012-07-06 12:14:00 +1000412 if (ret)
413 goto fail_device;
414
Ilia Mirkin7d3428c2014-01-29 19:53:00 -0500415 dev->irq_enabled = true;
416
Ben Skeggs77145f12012-07-31 16:16:21 +1000417 /* workaround an odd issue on nvc1 by disabling the device's
418 * nosnoop capability. hopefully won't cause issues until a
419 * better fix is found - assuming there is one...
420 */
Ben Skeggs967e7bd2014-08-10 04:10:22 +1000421 if (drm->device.info.chipset == 0xc1)
422 nvif_mask(&drm->device, 0x00088080, 0x00000800, 0x00000000);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000423
Ben Skeggs77145f12012-07-31 16:16:21 +1000424 nouveau_vga_init(drm);
Ben Skeggscb75d972012-07-11 10:44:20 +1000425 nouveau_agp_init(drm);
426
Ben Skeggs967e7bd2014-08-10 04:10:22 +1000427 if (drm->device.info.family >= NV_DEVICE_INFO_V0_TESLA) {
428 ret = nouveau_vm_new(nvkm_device(&drm->device), 0, (1ULL << 40),
Ben Skeggs3ee6f5b2014-08-10 04:10:20 +1000429 0x1000, &drm->client.vm);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000430 if (ret)
431 goto fail_device;
Ben Skeggs3ee6f5b2014-08-10 04:10:20 +1000432
Ben Skeggs0ad72862014-08-10 04:10:22 +1000433 nvkm_client(&drm->client.base)->vm = drm->client.vm;
Ben Skeggsebb945a2012-07-20 08:17:34 +1000434 }
435
436 ret = nouveau_ttm_init(drm);
Ben Skeggs94580292012-07-06 12:14:00 +1000437 if (ret)
Ben Skeggs77145f12012-07-31 16:16:21 +1000438 goto fail_ttm;
Ben Skeggs94580292012-07-06 12:14:00 +1000439
Ben Skeggs77145f12012-07-31 16:16:21 +1000440 ret = nouveau_bios_init(dev);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000441 if (ret)
Ben Skeggs77145f12012-07-31 16:16:21 +1000442 goto fail_bios;
443
Ben Skeggs77145f12012-07-31 16:16:21 +1000444 ret = nouveau_display_create(dev);
445 if (ret)
446 goto fail_dispctor;
447
448 if (dev->mode_config.num_crtc) {
449 ret = nouveau_display_init(dev);
450 if (ret)
451 goto fail_dispinit;
452 }
453
Ben Skeggs26fdd782013-10-15 11:52:56 +1000454 nouveau_sysfs_init(dev);
Ben Skeggsb9ed9192013-10-15 09:44:02 +1000455 nouveau_hwmon_init(dev);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000456 nouveau_accel_init(drm);
457 nouveau_fbcon_init(dev);
Dave Airlie5addcf02012-09-10 14:20:51 +1000458
459 if (nouveau_runtime_pm != 0) {
460 pm_runtime_use_autosuspend(dev->dev);
461 pm_runtime_set_autosuspend_delay(dev->dev, 5000);
462 pm_runtime_set_active(dev->dev);
463 pm_runtime_allow(dev->dev);
464 pm_runtime_mark_last_busy(dev->dev);
465 pm_runtime_put(dev->dev);
466 }
Ben Skeggs94580292012-07-06 12:14:00 +1000467 return 0;
468
Ben Skeggs77145f12012-07-31 16:16:21 +1000469fail_dispinit:
470 nouveau_display_destroy(dev);
471fail_dispctor:
Ben Skeggs77145f12012-07-31 16:16:21 +1000472 nouveau_bios_takedown(dev);
473fail_bios:
Ben Skeggsebb945a2012-07-20 08:17:34 +1000474 nouveau_ttm_fini(drm);
Ben Skeggs77145f12012-07-31 16:16:21 +1000475fail_ttm:
476 nouveau_agp_fini(drm);
477 nouveau_vga_fini(drm);
Ben Skeggs94580292012-07-06 12:14:00 +1000478fail_device:
Ben Skeggs0ad72862014-08-10 04:10:22 +1000479 nvif_device_fini(&drm->device);
Ben Skeggs94580292012-07-06 12:14:00 +1000480 nouveau_cli_destroy(&drm->client);
481 return ret;
482}
483
Marcin Slusarz5b8a43a2012-08-19 23:00:00 +0200484static int
Ben Skeggs94580292012-07-06 12:14:00 +1000485nouveau_drm_unload(struct drm_device *dev)
486{
Ben Skeggs77145f12012-07-31 16:16:21 +1000487 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs94580292012-07-06 12:14:00 +1000488
Dave Airlie5addcf02012-09-10 14:20:51 +1000489 pm_runtime_get_sync(dev->dev);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000490 nouveau_fbcon_fini(dev);
491 nouveau_accel_fini(drm);
Ben Skeggsb9ed9192013-10-15 09:44:02 +1000492 nouveau_hwmon_fini(dev);
Ben Skeggs26fdd782013-10-15 11:52:56 +1000493 nouveau_sysfs_fini(dev);
Ben Skeggs77145f12012-07-31 16:16:21 +1000494
Ben Skeggs94307382012-10-31 12:11:15 +1000495 if (dev->mode_config.num_crtc)
496 nouveau_display_fini(dev);
Ben Skeggs77145f12012-07-31 16:16:21 +1000497 nouveau_display_destroy(dev);
498
Ben Skeggs77145f12012-07-31 16:16:21 +1000499 nouveau_bios_takedown(dev);
Ben Skeggs94580292012-07-06 12:14:00 +1000500
Ben Skeggsebb945a2012-07-20 08:17:34 +1000501 nouveau_ttm_fini(drm);
Ben Skeggscb75d972012-07-11 10:44:20 +1000502 nouveau_agp_fini(drm);
Ben Skeggs77145f12012-07-31 16:16:21 +1000503 nouveau_vga_fini(drm);
Ben Skeggscb75d972012-07-11 10:44:20 +1000504
Ben Skeggs0ad72862014-08-10 04:10:22 +1000505 nvif_device_fini(&drm->device);
Dave Airlie5addcf02012-09-10 14:20:51 +1000506 if (drm->hdmi_device)
507 pci_dev_put(drm->hdmi_device);
Ben Skeggs94580292012-07-06 12:14:00 +1000508 nouveau_cli_destroy(&drm->client);
509 return 0;
510}
511
Alexandre Courbot8ba9ff12014-06-26 14:33:32 +0900512void
513nouveau_drm_device_remove(struct drm_device *dev)
Ben Skeggs94580292012-07-06 12:14:00 +1000514{
Ben Skeggs77145f12012-07-31 16:16:21 +1000515 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs0ad72862014-08-10 04:10:22 +1000516 struct nouveau_client *client;
Ben Skeggsebb945a2012-07-20 08:17:34 +1000517 struct nouveau_object *device;
Ben Skeggs77145f12012-07-31 16:16:21 +1000518
Ilia Mirkin7d3428c2014-01-29 19:53:00 -0500519 dev->irq_enabled = false;
Ben Skeggs0ad72862014-08-10 04:10:22 +1000520 client = nvkm_client(&drm->client.base);
521 device = client->device;
Ben Skeggs77145f12012-07-31 16:16:21 +1000522 drm_put_dev(dev);
523
Ben Skeggsebb945a2012-07-20 08:17:34 +1000524 nouveau_object_ref(NULL, &device);
525 nouveau_object_debug();
Ben Skeggs94580292012-07-06 12:14:00 +1000526}
Alexandre Courbot8ba9ff12014-06-26 14:33:32 +0900527EXPORT_SYMBOL(nouveau_drm_device_remove);
528
529static void
530nouveau_drm_remove(struct pci_dev *pdev)
531{
532 struct drm_device *dev = pci_get_drvdata(pdev);
533
534 nouveau_drm_device_remove(dev);
535}
Ben Skeggs94580292012-07-06 12:14:00 +1000536
Marcin Slusarzcd897832013-01-27 15:01:55 +0100537static int
Dave Airlie05c63c22014-03-26 14:10:06 +1000538nouveau_do_suspend(struct drm_device *dev, bool runtime)
Ben Skeggs94580292012-07-06 12:14:00 +1000539{
Ben Skeggs77145f12012-07-31 16:16:21 +1000540 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs94580292012-07-06 12:14:00 +1000541 struct nouveau_cli *cli;
542 int ret;
543
Dave Airlie05c63c22014-03-26 14:10:06 +1000544 if (dev->mode_config.num_crtc && !runtime) {
Ben Skeggsc52f4fa2013-11-08 14:38:40 +1000545 NV_INFO(drm, "suspending display...\n");
Ben Skeggs94307382012-10-31 12:11:15 +1000546 ret = nouveau_display_suspend(dev);
547 if (ret)
548 return ret;
549 }
Ben Skeggs94580292012-07-06 12:14:00 +1000550
Ben Skeggsc52f4fa2013-11-08 14:38:40 +1000551 NV_INFO(drm, "evicting buffers...\n");
Ben Skeggsebb945a2012-07-20 08:17:34 +1000552 ttm_bo_evict_mm(&drm->ttm.bdev, TTM_PL_VRAM);
553
Ben Skeggsc52f4fa2013-11-08 14:38:40 +1000554 NV_INFO(drm, "waiting for kernel channels to go idle...\n");
Ben Skeggs81dff212013-05-07 08:33:10 +1000555 if (drm->cechan) {
556 ret = nouveau_channel_idle(drm->cechan);
557 if (ret)
Ilia Mirkinf3980dc2014-01-23 02:45:02 -0500558 goto fail_display;
Ben Skeggs81dff212013-05-07 08:33:10 +1000559 }
560
561 if (drm->channel) {
562 ret = nouveau_channel_idle(drm->channel);
563 if (ret)
Ilia Mirkinf3980dc2014-01-23 02:45:02 -0500564 goto fail_display;
Ben Skeggs81dff212013-05-07 08:33:10 +1000565 }
566
Ben Skeggsc52f4fa2013-11-08 14:38:40 +1000567 NV_INFO(drm, "suspending client object trees...\n");
Ben Skeggsebb945a2012-07-20 08:17:34 +1000568 if (drm->fence && nouveau_fence(drm)->suspend) {
Ilia Mirkinf3980dc2014-01-23 02:45:02 -0500569 if (!nouveau_fence(drm)->suspend(drm)) {
570 ret = -ENOMEM;
571 goto fail_display;
572 }
Ben Skeggsebb945a2012-07-20 08:17:34 +1000573 }
574
Ben Skeggs94580292012-07-06 12:14:00 +1000575 list_for_each_entry(cli, &drm->clients, head) {
Ben Skeggs0ad72862014-08-10 04:10:22 +1000576 ret = nvif_client_suspend(&cli->base);
Ben Skeggs94580292012-07-06 12:14:00 +1000577 if (ret)
578 goto fail_client;
579 }
580
Ben Skeggsc52f4fa2013-11-08 14:38:40 +1000581 NV_INFO(drm, "suspending kernel object tree...\n");
Ben Skeggs0ad72862014-08-10 04:10:22 +1000582 ret = nvif_client_suspend(&drm->client.base);
Ben Skeggs94580292012-07-06 12:14:00 +1000583 if (ret)
584 goto fail_client;
585
Ben Skeggscb75d972012-07-11 10:44:20 +1000586 nouveau_agp_fini(drm);
Ben Skeggs94580292012-07-06 12:14:00 +1000587 return 0;
588
589fail_client:
590 list_for_each_entry_continue_reverse(cli, &drm->clients, head) {
Ben Skeggs0ad72862014-08-10 04:10:22 +1000591 nvif_client_resume(&cli->base);
Ben Skeggs94580292012-07-06 12:14:00 +1000592 }
593
Ilia Mirkinf3980dc2014-01-23 02:45:02 -0500594 if (drm->fence && nouveau_fence(drm)->resume)
595 nouveau_fence(drm)->resume(drm);
596
597fail_display:
Ben Skeggs94307382012-10-31 12:11:15 +1000598 if (dev->mode_config.num_crtc) {
Ben Skeggsc52f4fa2013-11-08 14:38:40 +1000599 NV_INFO(drm, "resuming display...\n");
Ben Skeggs94307382012-10-31 12:11:15 +1000600 nouveau_display_resume(dev);
601 }
Ben Skeggs94580292012-07-06 12:14:00 +1000602 return ret;
603}
604
Dave Airlie2d8b9cc2012-11-02 11:04:28 +1000605int nouveau_pmops_suspend(struct device *dev)
Ben Skeggs94580292012-07-06 12:14:00 +1000606{
Dave Airlie2d8b9cc2012-11-02 11:04:28 +1000607 struct pci_dev *pdev = to_pci_dev(dev);
608 struct drm_device *drm_dev = pci_get_drvdata(pdev);
Ben Skeggs94580292012-07-06 12:14:00 +1000609 int ret;
610
Dave Airlie5addcf02012-09-10 14:20:51 +1000611 if (drm_dev->switch_power_state == DRM_SWITCH_POWER_OFF ||
612 drm_dev->switch_power_state == DRM_SWITCH_POWER_DYNAMIC_OFF)
Ben Skeggs94580292012-07-06 12:14:00 +1000613 return 0;
614
Dave Airlie5addcf02012-09-10 14:20:51 +1000615 if (drm_dev->mode_config.num_crtc)
616 nouveau_fbcon_set_suspend(drm_dev, 1);
617
Dave Airlie05c63c22014-03-26 14:10:06 +1000618 ret = nouveau_do_suspend(drm_dev, false);
Ben Skeggs94580292012-07-06 12:14:00 +1000619 if (ret)
620 return ret;
Dave Airlie2d8b9cc2012-11-02 11:04:28 +1000621
622 pci_save_state(pdev);
623 pci_disable_device(pdev);
624 pci_set_power_state(pdev, PCI_D3hot);
Dave Airlie2d8b9cc2012-11-02 11:04:28 +1000625 return 0;
626}
627
Marcin Slusarzcd897832013-01-27 15:01:55 +0100628static int
Dave Airlie2d8b9cc2012-11-02 11:04:28 +1000629nouveau_do_resume(struct drm_device *dev)
630{
631 struct nouveau_drm *drm = nouveau_drm(dev);
632 struct nouveau_cli *cli;
633
Ben Skeggsc52f4fa2013-11-08 14:38:40 +1000634 NV_INFO(drm, "re-enabling device...\n");
Ben Skeggs94580292012-07-06 12:14:00 +1000635
Ben Skeggscb75d972012-07-11 10:44:20 +1000636 nouveau_agp_reset(drm);
637
Ben Skeggsc52f4fa2013-11-08 14:38:40 +1000638 NV_INFO(drm, "resuming kernel object tree...\n");
Ben Skeggs0ad72862014-08-10 04:10:22 +1000639 nvif_client_resume(&drm->client.base);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000640 nouveau_agp_init(drm);
Ben Skeggs94580292012-07-06 12:14:00 +1000641
Ben Skeggsc52f4fa2013-11-08 14:38:40 +1000642 NV_INFO(drm, "resuming client object trees...\n");
Ben Skeggs81dff212013-05-07 08:33:10 +1000643 if (drm->fence && nouveau_fence(drm)->resume)
644 nouveau_fence(drm)->resume(drm);
645
Ben Skeggs94580292012-07-06 12:14:00 +1000646 list_for_each_entry(cli, &drm->clients, head) {
Ben Skeggs0ad72862014-08-10 04:10:22 +1000647 nvif_client_resume(&cli->base);
Ben Skeggs94580292012-07-06 12:14:00 +1000648 }
649
Ben Skeggs77145f12012-07-31 16:16:21 +1000650 nouveau_run_vbios_init(dev);
Ben Skeggs77145f12012-07-31 16:16:21 +1000651
Ben Skeggs94307382012-10-31 12:11:15 +1000652 if (dev->mode_config.num_crtc) {
Ben Skeggsc52f4fa2013-11-08 14:38:40 +1000653 NV_INFO(drm, "resuming display...\n");
Dave Airlie5addcf02012-09-10 14:20:51 +1000654 nouveau_display_repin(dev);
Ben Skeggs94307382012-10-31 12:11:15 +1000655 }
Dave Airlie5addcf02012-09-10 14:20:51 +1000656
Ben Skeggs77145f12012-07-31 16:16:21 +1000657 return 0;
Ben Skeggs94580292012-07-06 12:14:00 +1000658}
659
Dave Airlie2d8b9cc2012-11-02 11:04:28 +1000660int nouveau_pmops_resume(struct device *dev)
661{
662 struct pci_dev *pdev = to_pci_dev(dev);
663 struct drm_device *drm_dev = pci_get_drvdata(pdev);
664 int ret;
665
Dave Airlie5addcf02012-09-10 14:20:51 +1000666 if (drm_dev->switch_power_state == DRM_SWITCH_POWER_OFF ||
667 drm_dev->switch_power_state == DRM_SWITCH_POWER_DYNAMIC_OFF)
Dave Airlie2d8b9cc2012-11-02 11:04:28 +1000668 return 0;
669
670 pci_set_power_state(pdev, PCI_D0);
671 pci_restore_state(pdev);
672 ret = pci_enable_device(pdev);
673 if (ret)
674 return ret;
675 pci_set_master(pdev);
676
Dave Airlie5addcf02012-09-10 14:20:51 +1000677 ret = nouveau_do_resume(drm_dev);
Ben Skeggsc52f4fa2013-11-08 14:38:40 +1000678 if (ret)
Dave Airlie5addcf02012-09-10 14:20:51 +1000679 return ret;
Dave Airlie5addcf02012-09-10 14:20:51 +1000680
Ben Skeggs028791b2014-06-30 13:04:14 +1000681 if (drm_dev->mode_config.num_crtc) {
Dave Airlie01172772013-09-09 07:30:56 +1000682 nouveau_display_resume(drm_dev);
Ben Skeggs028791b2014-06-30 13:04:14 +1000683 nouveau_fbcon_set_suspend(drm_dev, 0);
684 }
685
Dave Airlie5addcf02012-09-10 14:20:51 +1000686 return 0;
Dave Airlie2d8b9cc2012-11-02 11:04:28 +1000687}
688
689static int nouveau_pmops_freeze(struct device *dev)
690{
691 struct pci_dev *pdev = to_pci_dev(dev);
692 struct drm_device *drm_dev = pci_get_drvdata(pdev);
Dave Airlie5addcf02012-09-10 14:20:51 +1000693 int ret;
Dave Airlie2d8b9cc2012-11-02 11:04:28 +1000694
Dave Airlie5addcf02012-09-10 14:20:51 +1000695 if (drm_dev->mode_config.num_crtc)
696 nouveau_fbcon_set_suspend(drm_dev, 1);
697
Dave Airlie05c63c22014-03-26 14:10:06 +1000698 ret = nouveau_do_suspend(drm_dev, false);
Dave Airlie5addcf02012-09-10 14:20:51 +1000699 return ret;
Dave Airlie2d8b9cc2012-11-02 11:04:28 +1000700}
701
702static int nouveau_pmops_thaw(struct device *dev)
703{
704 struct pci_dev *pdev = to_pci_dev(dev);
705 struct drm_device *drm_dev = pci_get_drvdata(pdev);
Dave Airlie5addcf02012-09-10 14:20:51 +1000706 int ret;
Dave Airlie2d8b9cc2012-11-02 11:04:28 +1000707
Dave Airlie5addcf02012-09-10 14:20:51 +1000708 ret = nouveau_do_resume(drm_dev);
Ben Skeggsc52f4fa2013-11-08 14:38:40 +1000709 if (ret)
Dave Airlie5addcf02012-09-10 14:20:51 +1000710 return ret;
Ben Skeggs028791b2014-06-30 13:04:14 +1000711
712 if (drm_dev->mode_config.num_crtc) {
Dave Airlie01172772013-09-09 07:30:56 +1000713 nouveau_display_resume(drm_dev);
Ben Skeggs028791b2014-06-30 13:04:14 +1000714 nouveau_fbcon_set_suspend(drm_dev, 0);
715 }
716
Dave Airlie5addcf02012-09-10 14:20:51 +1000717 return 0;
Dave Airlie2d8b9cc2012-11-02 11:04:28 +1000718}
719
720
Marcin Slusarz5b8a43a2012-08-19 23:00:00 +0200721static int
Ben Skeggsebb945a2012-07-20 08:17:34 +1000722nouveau_drm_open(struct drm_device *dev, struct drm_file *fpriv)
723{
Ben Skeggsebb945a2012-07-20 08:17:34 +1000724 struct nouveau_drm *drm = nouveau_drm(dev);
725 struct nouveau_cli *cli;
Marcin Slusarza2896ce2012-12-09 15:45:21 +0100726 char name[32], tmpname[TASK_COMM_LEN];
Ben Skeggsebb945a2012-07-20 08:17:34 +1000727 int ret;
728
Dave Airlie5addcf02012-09-10 14:20:51 +1000729 /* need to bring up power immediately if opening device */
730 ret = pm_runtime_get_sync(dev->dev);
Alexandre Courbotb6c42852014-02-12 14:00:59 +0900731 if (ret < 0 && ret != -EACCES)
Dave Airlie5addcf02012-09-10 14:20:51 +1000732 return ret;
733
Marcin Slusarza2896ce2012-12-09 15:45:21 +0100734 get_task_comm(tmpname, current);
735 snprintf(name, sizeof(name), "%s[%d]", tmpname, pid_nr(fpriv->pid));
Ben Skeggsfa6df8c2012-09-12 13:09:23 +1000736
Alexandre Courbot420b9462014-02-17 15:17:26 +0900737 ret = nouveau_cli_create(nouveau_name(dev), name, sizeof(*cli),
738 (void **)&cli);
739
Ben Skeggsebb945a2012-07-20 08:17:34 +1000740 if (ret)
Dave Airlie5addcf02012-09-10 14:20:51 +1000741 goto out_suspend;
Ben Skeggsebb945a2012-07-20 08:17:34 +1000742
Ben Skeggs0ad72862014-08-10 04:10:22 +1000743 cli->base.super = false;
744
Ben Skeggs967e7bd2014-08-10 04:10:22 +1000745 if (drm->device.info.family >= NV_DEVICE_INFO_V0_TESLA) {
746 ret = nouveau_vm_new(nvkm_device(&drm->device), 0, (1ULL << 40),
Ben Skeggs3ee6f5b2014-08-10 04:10:20 +1000747 0x1000, &cli->vm);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000748 if (ret) {
749 nouveau_cli_destroy(cli);
Dave Airlie5addcf02012-09-10 14:20:51 +1000750 goto out_suspend;
Ben Skeggsebb945a2012-07-20 08:17:34 +1000751 }
Ben Skeggs3ee6f5b2014-08-10 04:10:20 +1000752
Ben Skeggs0ad72862014-08-10 04:10:22 +1000753 nvkm_client(&cli->base)->vm = cli->vm;
Ben Skeggsebb945a2012-07-20 08:17:34 +1000754 }
755
756 fpriv->driver_priv = cli;
757
758 mutex_lock(&drm->client.mutex);
759 list_add(&cli->head, &drm->clients);
760 mutex_unlock(&drm->client.mutex);
Dave Airlie5addcf02012-09-10 14:20:51 +1000761
762out_suspend:
763 pm_runtime_mark_last_busy(dev->dev);
764 pm_runtime_put_autosuspend(dev->dev);
765
766 return ret;
Ben Skeggsebb945a2012-07-20 08:17:34 +1000767}
768
Marcin Slusarz5b8a43a2012-08-19 23:00:00 +0200769static void
Ben Skeggsebb945a2012-07-20 08:17:34 +1000770nouveau_drm_preclose(struct drm_device *dev, struct drm_file *fpriv)
771{
772 struct nouveau_cli *cli = nouveau_cli(fpriv);
773 struct nouveau_drm *drm = nouveau_drm(dev);
774
Dave Airlie5addcf02012-09-10 14:20:51 +1000775 pm_runtime_get_sync(dev->dev);
776
Ben Skeggsebb945a2012-07-20 08:17:34 +1000777 if (cli->abi16)
778 nouveau_abi16_fini(cli->abi16);
779
780 mutex_lock(&drm->client.mutex);
781 list_del(&cli->head);
782 mutex_unlock(&drm->client.mutex);
Dave Airlie5addcf02012-09-10 14:20:51 +1000783
Ben Skeggsebb945a2012-07-20 08:17:34 +1000784}
785
Marcin Slusarz5b8a43a2012-08-19 23:00:00 +0200786static void
Ben Skeggsebb945a2012-07-20 08:17:34 +1000787nouveau_drm_postclose(struct drm_device *dev, struct drm_file *fpriv)
788{
789 struct nouveau_cli *cli = nouveau_cli(fpriv);
790 nouveau_cli_destroy(cli);
Dave Airlie5addcf02012-09-10 14:20:51 +1000791 pm_runtime_mark_last_busy(dev->dev);
792 pm_runtime_put_autosuspend(dev->dev);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000793}
794
Rob Clarkbaa70942013-08-02 13:27:49 -0400795static const struct drm_ioctl_desc
Ben Skeggs77145f12012-07-31 16:16:21 +1000796nouveau_ioctls[] = {
Martin Peres7d761252013-08-25 18:29:02 +0200797 DRM_IOCTL_DEF_DRV(NOUVEAU_GETPARAM, nouveau_abi16_ioctl_getparam, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
Ben Skeggs77145f12012-07-31 16:16:21 +1000798 DRM_IOCTL_DEF_DRV(NOUVEAU_SETPARAM, nouveau_abi16_ioctl_setparam, DRM_UNLOCKED|DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
Martin Peres7d761252013-08-25 18:29:02 +0200799 DRM_IOCTL_DEF_DRV(NOUVEAU_CHANNEL_ALLOC, nouveau_abi16_ioctl_channel_alloc, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
800 DRM_IOCTL_DEF_DRV(NOUVEAU_CHANNEL_FREE, nouveau_abi16_ioctl_channel_free, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
801 DRM_IOCTL_DEF_DRV(NOUVEAU_GROBJ_ALLOC, nouveau_abi16_ioctl_grobj_alloc, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
802 DRM_IOCTL_DEF_DRV(NOUVEAU_NOTIFIEROBJ_ALLOC, nouveau_abi16_ioctl_notifierobj_alloc, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
803 DRM_IOCTL_DEF_DRV(NOUVEAU_GPUOBJ_FREE, nouveau_abi16_ioctl_gpuobj_free, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
804 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_NEW, nouveau_gem_ioctl_new, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
805 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_PUSHBUF, nouveau_gem_ioctl_pushbuf, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
806 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_CPU_PREP, nouveau_gem_ioctl_cpu_prep, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
807 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_CPU_FINI, nouveau_gem_ioctl_cpu_fini, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
808 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_INFO, nouveau_gem_ioctl_info, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
Ben Skeggs77145f12012-07-31 16:16:21 +1000809};
810
Dave Airlie5addcf02012-09-10 14:20:51 +1000811long nouveau_drm_ioctl(struct file *filp,
812 unsigned int cmd, unsigned long arg)
813{
814 struct drm_file *file_priv = filp->private_data;
815 struct drm_device *dev;
816 long ret;
817 dev = file_priv->minor->dev;
818
819 ret = pm_runtime_get_sync(dev->dev);
Alexandre Courbotb6c42852014-02-12 14:00:59 +0900820 if (ret < 0 && ret != -EACCES)
Dave Airlie5addcf02012-09-10 14:20:51 +1000821 return ret;
822
823 ret = drm_ioctl(filp, cmd, arg);
824
825 pm_runtime_mark_last_busy(dev->dev);
826 pm_runtime_put_autosuspend(dev->dev);
827 return ret;
828}
Ben Skeggs77145f12012-07-31 16:16:21 +1000829static const struct file_operations
830nouveau_driver_fops = {
831 .owner = THIS_MODULE,
832 .open = drm_open,
833 .release = drm_release,
Dave Airlie5addcf02012-09-10 14:20:51 +1000834 .unlocked_ioctl = nouveau_drm_ioctl,
Ben Skeggs77145f12012-07-31 16:16:21 +1000835 .mmap = nouveau_ttm_mmap,
836 .poll = drm_poll,
Ben Skeggs77145f12012-07-31 16:16:21 +1000837 .read = drm_read,
838#if defined(CONFIG_COMPAT)
839 .compat_ioctl = nouveau_compat_ioctl,
840#endif
841 .llseek = noop_llseek,
842};
843
844static struct drm_driver
845driver = {
846 .driver_features =
Daniel Vetter4cb4ea32013-07-10 14:11:54 +0200847 DRIVER_USE_AGP |
Martin Peres7d761252013-08-25 18:29:02 +0200848 DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME | DRIVER_RENDER,
Ben Skeggs77145f12012-07-31 16:16:21 +1000849
850 .load = nouveau_drm_load,
851 .unload = nouveau_drm_unload,
852 .open = nouveau_drm_open,
853 .preclose = nouveau_drm_preclose,
854 .postclose = nouveau_drm_postclose,
855 .lastclose = nouveau_vga_lastclose,
856
Marcin Slusarz33b903e2013-02-08 21:42:13 +0100857#if defined(CONFIG_DEBUG_FS)
858 .debugfs_init = nouveau_debugfs_init,
859 .debugfs_cleanup = nouveau_debugfs_takedown,
860#endif
861
Ben Skeggs77145f12012-07-31 16:16:21 +1000862 .get_vblank_counter = drm_vblank_count,
Ben Skeggs51cb4b32013-10-03 07:02:29 +1000863 .enable_vblank = nouveau_display_vblank_enable,
864 .disable_vblank = nouveau_display_vblank_disable,
Ben Skeggsd83ef852013-11-14 13:37:49 +1000865 .get_scanout_position = nouveau_display_scanoutpos,
866 .get_vblank_timestamp = nouveau_display_vblstamp,
Ben Skeggs77145f12012-07-31 16:16:21 +1000867
868 .ioctls = nouveau_ioctls,
Rob Clarkbaa70942013-08-02 13:27:49 -0400869 .num_ioctls = ARRAY_SIZE(nouveau_ioctls),
Ben Skeggs77145f12012-07-31 16:16:21 +1000870 .fops = &nouveau_driver_fops,
871
872 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
873 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
Aaron Plattnerab9ccb92013-01-15 20:47:43 +0000874 .gem_prime_export = drm_gem_prime_export,
875 .gem_prime_import = drm_gem_prime_import,
876 .gem_prime_pin = nouveau_gem_prime_pin,
Maarten Lankhorst1af7c7d2013-06-27 13:38:19 +0200877 .gem_prime_unpin = nouveau_gem_prime_unpin,
Aaron Plattnerab9ccb92013-01-15 20:47:43 +0000878 .gem_prime_get_sg_table = nouveau_gem_prime_get_sg_table,
879 .gem_prime_import_sg_table = nouveau_gem_prime_import_sg_table,
880 .gem_prime_vmap = nouveau_gem_prime_vmap,
881 .gem_prime_vunmap = nouveau_gem_prime_vunmap,
Ben Skeggs77145f12012-07-31 16:16:21 +1000882
Ben Skeggs77145f12012-07-31 16:16:21 +1000883 .gem_free_object = nouveau_gem_object_del,
884 .gem_open_object = nouveau_gem_object_open,
885 .gem_close_object = nouveau_gem_object_close,
886
887 .dumb_create = nouveau_display_dumb_create,
888 .dumb_map_offset = nouveau_display_dumb_map_offset,
Daniel Vetter43387b32013-07-16 09:12:04 +0200889 .dumb_destroy = drm_gem_dumb_destroy,
Ben Skeggs77145f12012-07-31 16:16:21 +1000890
891 .name = DRIVER_NAME,
892 .desc = DRIVER_DESC,
893#ifdef GIT_REVISION
894 .date = GIT_REVISION,
895#else
896 .date = DRIVER_DATE,
897#endif
898 .major = DRIVER_MAJOR,
899 .minor = DRIVER_MINOR,
900 .patchlevel = DRIVER_PATCHLEVEL,
901};
902
Ben Skeggs94580292012-07-06 12:14:00 +1000903static struct pci_device_id
904nouveau_drm_pci_table[] = {
905 {
906 PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_ANY_ID),
907 .class = PCI_BASE_CLASS_DISPLAY << 16,
908 .class_mask = 0xff << 16,
909 },
910 {
911 PCI_DEVICE(PCI_VENDOR_ID_NVIDIA_SGS, PCI_ANY_ID),
912 .class = PCI_BASE_CLASS_DISPLAY << 16,
913 .class_mask = 0xff << 16,
914 },
915 {}
916};
917
Dave Airlie5addcf02012-09-10 14:20:51 +1000918static int nouveau_pmops_runtime_suspend(struct device *dev)
919{
920 struct pci_dev *pdev = to_pci_dev(dev);
921 struct drm_device *drm_dev = pci_get_drvdata(pdev);
922 int ret;
923
Dave Airlieadbbdba2014-03-26 14:09:37 +1000924 if (nouveau_runtime_pm == 0) {
925 pm_runtime_forbid(dev);
926 return -EBUSY;
927 }
Dave Airlie5addcf02012-09-10 14:20:51 +1000928
Ilia Mirkinb25b4422013-12-11 22:19:01 -0500929 /* are we optimus enabled? */
930 if (nouveau_runtime_pm == -1 && !nouveau_is_optimus() && !nouveau_is_v1_dsm()) {
931 DRM_DEBUG_DRIVER("failing to power off - not optimus\n");
Dave Airlieadbbdba2014-03-26 14:09:37 +1000932 pm_runtime_forbid(dev);
933 return -EBUSY;
Ilia Mirkinb25b4422013-12-11 22:19:01 -0500934 }
935
Ben Skeggsc52f4fa2013-11-08 14:38:40 +1000936 nv_debug_level(SILENT);
Dave Airlie5addcf02012-09-10 14:20:51 +1000937 drm_kms_helper_poll_disable(drm_dev);
938 vga_switcheroo_set_dynamic_switch(pdev, VGA_SWITCHEROO_OFF);
939 nouveau_switcheroo_optimus_dsm();
Dave Airlie05c63c22014-03-26 14:10:06 +1000940 ret = nouveau_do_suspend(drm_dev, true);
Dave Airlie5addcf02012-09-10 14:20:51 +1000941 pci_save_state(pdev);
942 pci_disable_device(pdev);
943 pci_set_power_state(pdev, PCI_D3cold);
944 drm_dev->switch_power_state = DRM_SWITCH_POWER_DYNAMIC_OFF;
945 return ret;
946}
947
948static int nouveau_pmops_runtime_resume(struct device *dev)
949{
950 struct pci_dev *pdev = to_pci_dev(dev);
951 struct drm_device *drm_dev = pci_get_drvdata(pdev);
Ben Skeggs967e7bd2014-08-10 04:10:22 +1000952 struct nvif_device *device = &nouveau_drm(drm_dev)->device;
Dave Airlie5addcf02012-09-10 14:20:51 +1000953 int ret;
954
955 if (nouveau_runtime_pm == 0)
956 return -EINVAL;
957
958 pci_set_power_state(pdev, PCI_D0);
959 pci_restore_state(pdev);
960 ret = pci_enable_device(pdev);
961 if (ret)
962 return ret;
963 pci_set_master(pdev);
964
965 ret = nouveau_do_resume(drm_dev);
Dave Airlie5addcf02012-09-10 14:20:51 +1000966 drm_kms_helper_poll_enable(drm_dev);
967 /* do magic */
Ben Skeggsdb2bec12014-08-10 04:10:22 +1000968 nvif_mask(device, 0x88488, (1 << 25), (1 << 25));
Dave Airlie5addcf02012-09-10 14:20:51 +1000969 vga_switcheroo_set_dynamic_switch(pdev, VGA_SWITCHEROO_ON);
970 drm_dev->switch_power_state = DRM_SWITCH_POWER_ON;
Ben Skeggsc52f4fa2013-11-08 14:38:40 +1000971 nv_debug_level(NORMAL);
Dave Airlie5addcf02012-09-10 14:20:51 +1000972 return ret;
973}
974
975static int nouveau_pmops_runtime_idle(struct device *dev)
976{
977 struct pci_dev *pdev = to_pci_dev(dev);
978 struct drm_device *drm_dev = pci_get_drvdata(pdev);
979 struct nouveau_drm *drm = nouveau_drm(drm_dev);
980 struct drm_crtc *crtc;
981
Dave Airlieadbbdba2014-03-26 14:09:37 +1000982 if (nouveau_runtime_pm == 0) {
983 pm_runtime_forbid(dev);
Dave Airlie5addcf02012-09-10 14:20:51 +1000984 return -EBUSY;
Dave Airlieadbbdba2014-03-26 14:09:37 +1000985 }
Dave Airlie5addcf02012-09-10 14:20:51 +1000986
987 /* are we optimus enabled? */
988 if (nouveau_runtime_pm == -1 && !nouveau_is_optimus() && !nouveau_is_v1_dsm()) {
989 DRM_DEBUG_DRIVER("failing to power off - not optimus\n");
Dave Airlieadbbdba2014-03-26 14:09:37 +1000990 pm_runtime_forbid(dev);
Dave Airlie5addcf02012-09-10 14:20:51 +1000991 return -EBUSY;
992 }
993
994 /* if we have a hdmi audio device - make sure it has a driver loaded */
995 if (drm->hdmi_device) {
996 if (!drm->hdmi_device->driver) {
997 DRM_DEBUG_DRIVER("failing to power off - no HDMI audio driver loaded\n");
998 pm_runtime_mark_last_busy(dev);
999 return -EBUSY;
1000 }
1001 }
1002
1003 list_for_each_entry(crtc, &drm->dev->mode_config.crtc_list, head) {
1004 if (crtc->enabled) {
1005 DRM_DEBUG_DRIVER("failing to power off - crtc active\n");
1006 return -EBUSY;
1007 }
1008 }
1009 pm_runtime_mark_last_busy(dev);
1010 pm_runtime_autosuspend(dev);
1011 /* we don't want the main rpm_idle to call suspend - we want to autosuspend */
1012 return 1;
1013}
1014
Dave Airlie2d8b9cc2012-11-02 11:04:28 +10001015static const struct dev_pm_ops nouveau_pm_ops = {
1016 .suspend = nouveau_pmops_suspend,
1017 .resume = nouveau_pmops_resume,
1018 .freeze = nouveau_pmops_freeze,
1019 .thaw = nouveau_pmops_thaw,
1020 .poweroff = nouveau_pmops_freeze,
1021 .restore = nouveau_pmops_resume,
Dave Airlie5addcf02012-09-10 14:20:51 +10001022 .runtime_suspend = nouveau_pmops_runtime_suspend,
1023 .runtime_resume = nouveau_pmops_runtime_resume,
1024 .runtime_idle = nouveau_pmops_runtime_idle,
Dave Airlie2d8b9cc2012-11-02 11:04:28 +10001025};
1026
Ben Skeggs94580292012-07-06 12:14:00 +10001027static struct pci_driver
1028nouveau_drm_pci_driver = {
1029 .name = "nouveau",
1030 .id_table = nouveau_drm_pci_table,
1031 .probe = nouveau_drm_probe,
1032 .remove = nouveau_drm_remove,
Dave Airlie2d8b9cc2012-11-02 11:04:28 +10001033 .driver.pm = &nouveau_pm_ops,
Ben Skeggs94580292012-07-06 12:14:00 +10001034};
1035
Alexandre Courbot8ba9ff12014-06-26 14:33:32 +09001036struct drm_device *
1037nouveau_platform_device_create_(struct platform_device *pdev, int size,
1038 void **pobject)
Alexandre Courbot420b9462014-02-17 15:17:26 +09001039{
Alexandre Courbot8ba9ff12014-06-26 14:33:32 +09001040 struct drm_device *drm;
1041 int err;
Alexandre Courbot420b9462014-02-17 15:17:26 +09001042
Alexandre Courbot8ba9ff12014-06-26 14:33:32 +09001043 err = nouveau_device_create_(pdev, NOUVEAU_BUS_PLATFORM,
Alexandre Courbot420b9462014-02-17 15:17:26 +09001044 nouveau_platform_name(pdev),
1045 dev_name(&pdev->dev), nouveau_config,
Alexandre Courbot8ba9ff12014-06-26 14:33:32 +09001046 nouveau_debug, size, pobject);
1047 if (err)
1048 return ERR_PTR(err);
Alexandre Courbot420b9462014-02-17 15:17:26 +09001049
Alexandre Courbot8ba9ff12014-06-26 14:33:32 +09001050 drm = drm_dev_alloc(&driver, &pdev->dev);
1051 if (!drm) {
1052 err = -ENOMEM;
1053 goto err_free;
Alexandre Courbot420b9462014-02-17 15:17:26 +09001054 }
1055
Alexandre Courbot8ba9ff12014-06-26 14:33:32 +09001056 err = drm_dev_set_unique(drm, "%s", dev_name(&pdev->dev));
1057 if (err < 0)
1058 goto err_free;
1059
1060 drm->platformdev = pdev;
1061 platform_set_drvdata(pdev, drm);
1062
1063 return drm;
1064
1065err_free:
1066 nouveau_object_ref(NULL, (struct nouveau_object **)pobject);
1067
1068 return ERR_PTR(err);
Alexandre Courbot420b9462014-02-17 15:17:26 +09001069}
Alexandre Courbot8ba9ff12014-06-26 14:33:32 +09001070EXPORT_SYMBOL(nouveau_platform_device_create_);
Alexandre Courbot420b9462014-02-17 15:17:26 +09001071
Ben Skeggs94580292012-07-06 12:14:00 +10001072static int __init
1073nouveau_drm_init(void)
1074{
Ben Skeggs77145f12012-07-31 16:16:21 +10001075 if (nouveau_modeset == -1) {
1076#ifdef CONFIG_VGA_CONSOLE
1077 if (vgacon_text_force())
1078 nouveau_modeset = 0;
Ben Skeggs77145f12012-07-31 16:16:21 +10001079#endif
Ben Skeggs77145f12012-07-31 16:16:21 +10001080 }
1081
1082 if (!nouveau_modeset)
1083 return 0;
1084
1085 nouveau_register_dsm_handler();
1086 return drm_pci_init(&driver, &nouveau_drm_pci_driver);
Ben Skeggs94580292012-07-06 12:14:00 +10001087}
1088
1089static void __exit
1090nouveau_drm_exit(void)
1091{
Ben Skeggs77145f12012-07-31 16:16:21 +10001092 if (!nouveau_modeset)
1093 return;
1094
1095 drm_pci_exit(&driver, &nouveau_drm_pci_driver);
1096 nouveau_unregister_dsm_handler();
Ben Skeggs94580292012-07-06 12:14:00 +10001097}
1098
1099module_init(nouveau_drm_init);
1100module_exit(nouveau_drm_exit);
1101
1102MODULE_DEVICE_TABLE(pci, nouveau_drm_pci_table);
Ben Skeggs77145f12012-07-31 16:16:21 +10001103MODULE_AUTHOR(DRIVER_AUTHOR);
1104MODULE_DESCRIPTION(DRIVER_DESC);
Ben Skeggs94580292012-07-06 12:14:00 +10001105MODULE_LICENSE("GPL and additional rights");