blob: 1118efa5681790afae4206fddfc5839bf35fadc5 [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>
Ben Skeggsfdb751e2014-08-10 04:10:23 +100030
Dave Airlie5addcf02012-09-10 14:20:51 +100031#include "drmP.h"
32#include "drm_crtc_helper.h"
Ben Skeggsfdb751e2014-08-10 04:10:23 +100033
Ben Skeggs94580292012-07-06 12:14:00 +100034#include <core/device.h>
Ben Skeggsebb945a2012-07-20 08:17:34 +100035#include <core/gpuobj.h>
Ilia Mirkinc33e05a2014-02-13 21:35:14 -050036#include <core/option.h>
Ben Skeggs94580292012-07-06 12:14:00 +100037
Ben Skeggs94580292012-07-06 12:14:00 +100038#include "nouveau_drm.h"
Ben Skeggsebb945a2012-07-20 08:17:34 +100039#include "nouveau_dma.h"
Ben Skeggs77145f12012-07-31 16:16:21 +100040#include "nouveau_ttm.h"
41#include "nouveau_gem.h"
Ben Skeggscb75d972012-07-11 10:44:20 +100042#include "nouveau_agp.h"
Ben Skeggs77145f12012-07-31 16:16:21 +100043#include "nouveau_vga.h"
Ben Skeggs26fdd782013-10-15 11:52:56 +100044#include "nouveau_sysfs.h"
Ben Skeggsb9ed9192013-10-15 09:44:02 +100045#include "nouveau_hwmon.h"
Ben Skeggs77145f12012-07-31 16:16:21 +100046#include "nouveau_acpi.h"
47#include "nouveau_bios.h"
48#include "nouveau_ioctl.h"
Ben Skeggsebb945a2012-07-20 08:17:34 +100049#include "nouveau_abi16.h"
50#include "nouveau_fbcon.h"
51#include "nouveau_fence.h"
Marcin Slusarz33b903e2013-02-08 21:42:13 +010052#include "nouveau_debugfs.h"
Ben Skeggs27111a22014-08-10 04:10:31 +100053#include "nouveau_usif.h"
Pierre Moreau703fa262014-08-18 22:43:24 +020054#include "nouveau_connector.h"
Alexandre Courbot055a65d2015-01-15 15:29:56 +090055#include "nouveau_platform.h"
Ben Skeggsebb945a2012-07-20 08:17:34 +100056
Ben Skeggs94580292012-07-06 12:14:00 +100057MODULE_PARM_DESC(config, "option string to pass to driver core");
58static char *nouveau_config;
59module_param_named(config, nouveau_config, charp, 0400);
60
61MODULE_PARM_DESC(debug, "debug string to pass to driver core");
62static char *nouveau_debug;
63module_param_named(debug, nouveau_debug, charp, 0400);
64
Ben Skeggsebb945a2012-07-20 08:17:34 +100065MODULE_PARM_DESC(noaccel, "disable kernel/abi16 acceleration");
66static int nouveau_noaccel = 0;
67module_param_named(noaccel, nouveau_noaccel, int, 0400);
68
Ben Skeggs94307382012-10-31 12:11:15 +100069MODULE_PARM_DESC(modeset, "enable driver (default: auto, "
70 "0 = disabled, 1 = enabled, 2 = headless)");
71int nouveau_modeset = -1;
Ben Skeggs77145f12012-07-31 16:16:21 +100072module_param_named(modeset, nouveau_modeset, int, 0400);
73
Dave Airlie5addcf02012-09-10 14:20:51 +100074MODULE_PARM_DESC(runpm, "disable (0), force enable (1), optimus only default (-1)");
75int nouveau_runtime_pm = -1;
76module_param_named(runpm, nouveau_runtime_pm, int, 0400);
77
David Herrmann915b4d12014-08-29 12:12:43 +020078static struct drm_driver driver_stub;
79static struct drm_driver driver_pci;
80static struct drm_driver driver_platform;
Ben Skeggs77145f12012-07-31 16:16:21 +100081
Ben Skeggs94580292012-07-06 12:14:00 +100082static u64
Alexandre Courbot420b9462014-02-17 15:17:26 +090083nouveau_pci_name(struct pci_dev *pdev)
Ben Skeggs94580292012-07-06 12:14:00 +100084{
85 u64 name = (u64)pci_domain_nr(pdev->bus) << 32;
86 name |= pdev->bus->number << 16;
87 name |= PCI_SLOT(pdev->devfn) << 8;
88 return name | PCI_FUNC(pdev->devfn);
89}
90
Alexandre Courbot420b9462014-02-17 15:17:26 +090091static u64
92nouveau_platform_name(struct platform_device *platformdev)
93{
94 return platformdev->id;
95}
96
97static u64
98nouveau_name(struct drm_device *dev)
99{
100 if (dev->pdev)
101 return nouveau_pci_name(dev->pdev);
102 else
103 return nouveau_platform_name(dev->platformdev);
104}
105
Ben Skeggs94580292012-07-06 12:14:00 +1000106static int
Alexandre Courbot420b9462014-02-17 15:17:26 +0900107nouveau_cli_create(u64 name, const char *sname,
Ben Skeggsfa6df8c2012-09-12 13:09:23 +1000108 int size, void **pcli)
Ben Skeggs94580292012-07-06 12:14:00 +1000109{
Ben Skeggs0ad72862014-08-10 04:10:22 +1000110 struct nouveau_cli *cli = *pcli = kzalloc(size, GFP_KERNEL);
111 if (cli) {
112 int ret = nvif_client_init(NULL, NULL, sname, name,
113 nouveau_config, nouveau_debug,
114 &cli->base);
Ben Skeggs27111a22014-08-10 04:10:31 +1000115 if (ret == 0) {
Ben Skeggs0ad72862014-08-10 04:10:22 +1000116 mutex_init(&cli->mutex);
Ben Skeggs27111a22014-08-10 04:10:31 +1000117 usif_client_init(cli);
118 }
Ben Skeggs94580292012-07-06 12:14:00 +1000119 return ret;
Marcin Slusarzdd5700e2012-12-10 21:59:52 +0100120 }
Ben Skeggs0ad72862014-08-10 04:10:22 +1000121 return -ENOMEM;
Ben Skeggs94580292012-07-06 12:14:00 +1000122}
123
124static void
125nouveau_cli_destroy(struct nouveau_cli *cli)
126{
Ben Skeggs0ad72862014-08-10 04:10:22 +1000127 nouveau_vm_ref(NULL, &nvkm_client(&cli->base)->vm, NULL);
128 nvif_client_fini(&cli->base);
Ben Skeggs27111a22014-08-10 04:10:31 +1000129 usif_client_fini(cli);
Ben Skeggs94580292012-07-06 12:14:00 +1000130}
131
Ben Skeggsebb945a2012-07-20 08:17:34 +1000132static void
133nouveau_accel_fini(struct nouveau_drm *drm)
134{
Ben Skeggsebb945a2012-07-20 08:17:34 +1000135 nouveau_channel_del(&drm->channel);
Ben Skeggs0ad72862014-08-10 04:10:22 +1000136 nvif_object_fini(&drm->ntfy);
137 nouveau_gpuobj_ref(NULL, &drm->notify);
138 nvif_object_fini(&drm->nvsw);
Ben Skeggs49981042012-08-06 19:38:25 +1000139 nouveau_channel_del(&drm->cechan);
Ben Skeggs0ad72862014-08-10 04:10:22 +1000140 nvif_object_fini(&drm->ttm.copy);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000141 if (drm->fence)
142 nouveau_fence(drm)->dtor(drm);
143}
144
145static void
146nouveau_accel_init(struct nouveau_drm *drm)
147{
Ben Skeggs967e7bd2014-08-10 04:10:22 +1000148 struct nvif_device *device = &drm->device;
Ben Skeggs49981042012-08-06 19:38:25 +1000149 u32 arg0, arg1;
Ben Skeggs967e7bd2014-08-10 04:10:22 +1000150 u32 sclass[16];
151 int ret, i;
Ben Skeggsebb945a2012-07-20 08:17:34 +1000152
Ben Skeggs967e7bd2014-08-10 04:10:22 +1000153 if (nouveau_noaccel)
Ben Skeggsebb945a2012-07-20 08:17:34 +1000154 return;
155
156 /* initialise synchronisation routines */
Ben Skeggs967e7bd2014-08-10 04:10:22 +1000157 /*XXX: this is crap, but the fence/channel stuff is a little
158 * backwards in some places. this will be fixed.
159 */
Ben Skeggs0ad72862014-08-10 04:10:22 +1000160 ret = nvif_object_sclass(&device->base, sclass, ARRAY_SIZE(sclass));
Ben Skeggs967e7bd2014-08-10 04:10:22 +1000161 if (ret < 0)
162 return;
163
164 for (ret = -ENOSYS, i = 0; ret && i < ARRAY_SIZE(sclass); i++) {
165 switch (sclass[i]) {
Ben Skeggsbbf89062014-08-10 04:10:25 +1000166 case NV03_CHANNEL_DMA:
Ben Skeggs967e7bd2014-08-10 04:10:22 +1000167 ret = nv04_fence_create(drm);
168 break;
Ben Skeggsbbf89062014-08-10 04:10:25 +1000169 case NV10_CHANNEL_DMA:
Ben Skeggs967e7bd2014-08-10 04:10:22 +1000170 ret = nv10_fence_create(drm);
171 break;
Ben Skeggsbbf89062014-08-10 04:10:25 +1000172 case NV17_CHANNEL_DMA:
173 case NV40_CHANNEL_DMA:
Ben Skeggs967e7bd2014-08-10 04:10:22 +1000174 ret = nv17_fence_create(drm);
175 break;
Ben Skeggsbbf89062014-08-10 04:10:25 +1000176 case NV50_CHANNEL_GPFIFO:
Ben Skeggs967e7bd2014-08-10 04:10:22 +1000177 ret = nv50_fence_create(drm);
178 break;
Ben Skeggsbbf89062014-08-10 04:10:25 +1000179 case G82_CHANNEL_GPFIFO:
Ben Skeggs967e7bd2014-08-10 04:10:22 +1000180 ret = nv84_fence_create(drm);
181 break;
Ben Skeggsbbf89062014-08-10 04:10:25 +1000182 case FERMI_CHANNEL_GPFIFO:
183 case KEPLER_CHANNEL_GPFIFO_A:
Ben Skeggs967e7bd2014-08-10 04:10:22 +1000184 ret = nvc0_fence_create(drm);
185 break;
186 default:
187 break;
188 }
189 }
190
Ben Skeggsebb945a2012-07-20 08:17:34 +1000191 if (ret) {
192 NV_ERROR(drm, "failed to initialise sync subsystem, %d\n", ret);
193 nouveau_accel_fini(drm);
194 return;
195 }
196
Ben Skeggs967e7bd2014-08-10 04:10:22 +1000197 if (device->info.family >= NV_DEVICE_INFO_V0_KEPLER) {
Ben Skeggs0ad72862014-08-10 04:10:22 +1000198 ret = nouveau_channel_new(drm, &drm->device, NVDRM_CHAN + 1,
Ben Skeggsbbf89062014-08-10 04:10:25 +1000199 KEPLER_CHANNEL_GPFIFO_A_V0_ENGINE_CE0|
200 KEPLER_CHANNEL_GPFIFO_A_V0_ENGINE_CE1,
201 0, &drm->cechan);
Ben Skeggs49981042012-08-06 19:38:25 +1000202 if (ret)
203 NV_ERROR(drm, "failed to create ce channel, %d\n", ret);
204
Ben Skeggsbbf89062014-08-10 04:10:25 +1000205 arg0 = KEPLER_CHANNEL_GPFIFO_A_V0_ENGINE_GR;
Ben Skeggs49469802012-11-22 13:43:55 +1000206 arg1 = 1;
Ben Skeggs00fc6f62013-07-09 14:20:15 +1000207 } else
Ben Skeggs967e7bd2014-08-10 04:10:22 +1000208 if (device->info.chipset >= 0xa3 &&
209 device->info.chipset != 0xaa &&
210 device->info.chipset != 0xac) {
Ben Skeggs0ad72862014-08-10 04:10:22 +1000211 ret = nouveau_channel_new(drm, &drm->device, NVDRM_CHAN + 1,
212 NvDmaFB, NvDmaTT, &drm->cechan);
Ben Skeggs00fc6f62013-07-09 14:20:15 +1000213 if (ret)
214 NV_ERROR(drm, "failed to create ce channel, %d\n", ret);
215
216 arg0 = NvDmaFB;
217 arg1 = NvDmaTT;
Ben Skeggs49981042012-08-06 19:38:25 +1000218 } else {
219 arg0 = NvDmaFB;
220 arg1 = NvDmaTT;
221 }
222
Ben Skeggs0ad72862014-08-10 04:10:22 +1000223 ret = nouveau_channel_new(drm, &drm->device, NVDRM_CHAN, arg0, arg1,
224 &drm->channel);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000225 if (ret) {
226 NV_ERROR(drm, "failed to create kernel channel, %d\n", ret);
227 nouveau_accel_fini(drm);
228 return;
229 }
230
Ben Skeggs0ad72862014-08-10 04:10:22 +1000231 ret = nvif_object_init(drm->channel->object, NULL, NVDRM_NVSW,
232 nouveau_abi16_swclass(drm), NULL, 0, &drm->nvsw);
Ben Skeggs69a61462013-11-13 10:58:51 +1000233 if (ret == 0) {
Ben Skeggs0ad72862014-08-10 04:10:22 +1000234 struct nouveau_software_chan *swch;
Ben Skeggs69a61462013-11-13 10:58:51 +1000235 ret = RING_SPACE(drm->channel, 2);
236 if (ret == 0) {
Ben Skeggs967e7bd2014-08-10 04:10:22 +1000237 if (device->info.family < NV_DEVICE_INFO_V0_FERMI) {
Ben Skeggs69a61462013-11-13 10:58:51 +1000238 BEGIN_NV04(drm->channel, NvSubSw, 0, 1);
239 OUT_RING (drm->channel, NVDRM_NVSW);
240 } else
Ben Skeggs967e7bd2014-08-10 04:10:22 +1000241 if (device->info.family < NV_DEVICE_INFO_V0_KEPLER) {
Ben Skeggs69a61462013-11-13 10:58:51 +1000242 BEGIN_NVC0(drm->channel, FermiSw, 0, 1);
243 OUT_RING (drm->channel, 0x001f0000);
244 }
245 }
Ben Skeggs0ad72862014-08-10 04:10:22 +1000246 swch = (void *)nvkm_object(&drm->nvsw)->parent;
Ben Skeggs69a61462013-11-13 10:58:51 +1000247 swch->flip = nouveau_flip_complete;
248 swch->flip_data = drm->channel;
249 }
250
251 if (ret) {
252 NV_ERROR(drm, "failed to allocate software object, %d\n", ret);
253 nouveau_accel_fini(drm);
254 return;
255 }
256
Ben Skeggs967e7bd2014-08-10 04:10:22 +1000257 if (device->info.family < NV_DEVICE_INFO_V0_FERMI) {
258 ret = nouveau_gpuobj_new(nvkm_object(&drm->device), NULL, 32,
259 0, 0, &drm->notify);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000260 if (ret) {
261 NV_ERROR(drm, "failed to allocate notifier, %d\n", ret);
262 nouveau_accel_fini(drm);
263 return;
264 }
265
Ben Skeggs0ad72862014-08-10 04:10:22 +1000266 ret = nvif_object_init(drm->channel->object, NULL, NvNotify0,
Ben Skeggs4acfd702014-08-10 04:10:24 +1000267 NV_DMA_IN_MEMORY,
268 &(struct nv_dma_v0) {
269 .target = NV_DMA_V0_TARGET_VRAM,
270 .access = NV_DMA_V0_ACCESS_RDWR,
Ben Skeggsebb945a2012-07-20 08:17:34 +1000271 .start = drm->notify->addr,
272 .limit = drm->notify->addr + 31
Ben Skeggs4acfd702014-08-10 04:10:24 +1000273 }, sizeof(struct nv_dma_v0),
Ben Skeggs0ad72862014-08-10 04:10:22 +1000274 &drm->ntfy);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000275 if (ret) {
276 nouveau_accel_fini(drm);
277 return;
278 }
279 }
280
281
Ben Skeggs49981042012-08-06 19:38:25 +1000282 nouveau_bo_move_init(drm);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000283}
284
Greg Kroah-Hartman56550d92012-12-21 15:09:25 -0800285static int nouveau_drm_probe(struct pci_dev *pdev,
286 const struct pci_device_id *pent)
Ben Skeggs94580292012-07-06 12:14:00 +1000287{
288 struct nouveau_device *device;
Ben Skeggsebb945a2012-07-20 08:17:34 +1000289 struct apertures_struct *aper;
290 bool boot = false;
Ben Skeggs94580292012-07-06 12:14:00 +1000291 int ret;
292
Ben Skeggsebb945a2012-07-20 08:17:34 +1000293 /* remove conflicting drivers (vesafb, efifb etc) */
294 aper = alloc_apertures(3);
295 if (!aper)
296 return -ENOMEM;
297
298 aper->ranges[0].base = pci_resource_start(pdev, 1);
299 aper->ranges[0].size = pci_resource_len(pdev, 1);
300 aper->count = 1;
301
302 if (pci_resource_len(pdev, 2)) {
303 aper->ranges[aper->count].base = pci_resource_start(pdev, 2);
304 aper->ranges[aper->count].size = pci_resource_len(pdev, 2);
305 aper->count++;
306 }
307
308 if (pci_resource_len(pdev, 3)) {
309 aper->ranges[aper->count].base = pci_resource_start(pdev, 3);
310 aper->ranges[aper->count].size = pci_resource_len(pdev, 3);
311 aper->count++;
312 }
313
314#ifdef CONFIG_X86
315 boot = pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW;
316#endif
Ben Skeggs771fa0e2014-08-10 04:10:31 +1000317 if (nouveau_modeset != 2)
318 remove_conflicting_framebuffers(aper, "nouveaufb", boot);
Tommi Rantala83ef7772012-11-09 09:19:40 +0000319 kfree(aper);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000320
Alexandre Courbot420b9462014-02-17 15:17:26 +0900321 ret = nouveau_device_create(pdev, NOUVEAU_BUS_PCI,
322 nouveau_pci_name(pdev), pci_name(pdev),
Ben Skeggs94580292012-07-06 12:14:00 +1000323 nouveau_config, nouveau_debug, &device);
324 if (ret)
325 return ret;
326
327 pci_set_master(pdev);
328
David Herrmann915b4d12014-08-29 12:12:43 +0200329 ret = drm_get_pci_dev(pdev, pent, &driver_pci);
Ben Skeggs94580292012-07-06 12:14:00 +1000330 if (ret) {
Ben Skeggsebb945a2012-07-20 08:17:34 +1000331 nouveau_object_ref(NULL, (struct nouveau_object **)&device);
Ben Skeggs94580292012-07-06 12:14:00 +1000332 return ret;
333 }
334
335 return 0;
336}
337
Dave Airlie5addcf02012-09-10 14:20:51 +1000338#define PCI_CLASS_MULTIMEDIA_HD_AUDIO 0x0403
339
340static void
Ilia Mirkin46941b02014-02-13 03:39:50 -0500341nouveau_get_hdmi_dev(struct nouveau_drm *drm)
Dave Airlie5addcf02012-09-10 14:20:51 +1000342{
Ilia Mirkin46941b02014-02-13 03:39:50 -0500343 struct pci_dev *pdev = drm->dev->pdev;
Dave Airlie5addcf02012-09-10 14:20:51 +1000344
Alexandre Courbot420b9462014-02-17 15:17:26 +0900345 if (!pdev) {
Alexandre Courbot40189b02014-03-24 17:42:23 +0900346 DRM_INFO("not a PCI device; no HDMI\n");
Alexandre Courbot420b9462014-02-17 15:17:26 +0900347 drm->hdmi_device = NULL;
348 return;
349 }
350
Dave Airlie5addcf02012-09-10 14:20:51 +1000351 /* subfunction one is a hdmi audio device? */
352 drm->hdmi_device = pci_get_bus_and_slot((unsigned int)pdev->bus->number,
353 PCI_DEVFN(PCI_SLOT(pdev->devfn), 1));
354
355 if (!drm->hdmi_device) {
Ilia Mirkin46941b02014-02-13 03:39:50 -0500356 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 +1000357 return;
358 }
359
360 if ((drm->hdmi_device->class >> 8) != PCI_CLASS_MULTIMEDIA_HD_AUDIO) {
Ilia Mirkin46941b02014-02-13 03:39:50 -0500361 NV_DEBUG(drm, "possible hdmi device not audio %d\n", drm->hdmi_device->class);
Dave Airlie5addcf02012-09-10 14:20:51 +1000362 pci_dev_put(drm->hdmi_device);
363 drm->hdmi_device = NULL;
364 return;
365 }
366}
367
Marcin Slusarz5b8a43a2012-08-19 23:00:00 +0200368static int
Ben Skeggs94580292012-07-06 12:14:00 +1000369nouveau_drm_load(struct drm_device *dev, unsigned long flags)
370{
371 struct pci_dev *pdev = dev->pdev;
372 struct nouveau_drm *drm;
373 int ret;
374
Alexandre Courbot420b9462014-02-17 15:17:26 +0900375 ret = nouveau_cli_create(nouveau_name(dev), "DRM", sizeof(*drm),
376 (void **)&drm);
Ben Skeggs94580292012-07-06 12:14:00 +1000377 if (ret)
378 return ret;
379
Ben Skeggs77145f12012-07-31 16:16:21 +1000380 dev->dev_private = drm;
381 drm->dev = dev;
Ben Skeggs0ad72862014-08-10 04:10:22 +1000382 nvkm_client(&drm->client.base)->debug =
383 nouveau_dbgopt(nouveau_debug, "DRM");
Ben Skeggs77145f12012-07-31 16:16:21 +1000384
Ben Skeggs94580292012-07-06 12:14:00 +1000385 INIT_LIST_HEAD(&drm->clients);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000386 spin_lock_init(&drm->tile.lock);
Ben Skeggs94580292012-07-06 12:14:00 +1000387
Ilia Mirkin46941b02014-02-13 03:39:50 -0500388 nouveau_get_hdmi_dev(drm);
Dave Airlie5addcf02012-09-10 14:20:51 +1000389
Ben Skeggscb75d972012-07-11 10:44:20 +1000390 /* make sure AGP controller is in a consistent state before we
391 * (possibly) execute vbios init tables (see nouveau_agp.h)
392 */
Alexandre Courbot420b9462014-02-17 15:17:26 +0900393 if (pdev && drm_pci_device_is_agp(dev) && dev->agp) {
Ben Skeggs586491e2014-08-10 04:10:24 +1000394 const u64 enables = NV_DEVICE_V0_DISABLE_IDENTIFY |
395 NV_DEVICE_V0_DISABLE_MMIO;
Ben Skeggscb75d972012-07-11 10:44:20 +1000396 /* dummy device object, doesn't init anything, but allows
397 * agp code access to registers
398 */
Ben Skeggs0ad72862014-08-10 04:10:22 +1000399 ret = nvif_device_init(&drm->client.base.base, NULL,
Ben Skeggs586491e2014-08-10 04:10:24 +1000400 NVDRM_DEVICE, NV_DEVICE,
401 &(struct nv_device_v0) {
Ben Skeggscb75d972012-07-11 10:44:20 +1000402 .device = ~0,
Ben Skeggs586491e2014-08-10 04:10:24 +1000403 .disable = ~enables,
Ben Skeggscb75d972012-07-11 10:44:20 +1000404 .debug0 = ~0,
Ben Skeggs586491e2014-08-10 04:10:24 +1000405 }, sizeof(struct nv_device_v0),
Ben Skeggs0ad72862014-08-10 04:10:22 +1000406 &drm->device);
Ben Skeggscb75d972012-07-11 10:44:20 +1000407 if (ret)
Ben Skeggsebb945a2012-07-20 08:17:34 +1000408 goto fail_device;
Ben Skeggscb75d972012-07-11 10:44:20 +1000409
410 nouveau_agp_reset(drm);
Ben Skeggs0ad72862014-08-10 04:10:22 +1000411 nvif_device_fini(&drm->device);
Ben Skeggscb75d972012-07-11 10:44:20 +1000412 }
413
Ben Skeggs0ad72862014-08-10 04:10:22 +1000414 ret = nvif_device_init(&drm->client.base.base, NULL, NVDRM_DEVICE,
Ben Skeggs586491e2014-08-10 04:10:24 +1000415 NV_DEVICE,
416 &(struct nv_device_v0) {
Ben Skeggs94580292012-07-06 12:14:00 +1000417 .device = ~0,
418 .disable = 0,
419 .debug0 = 0,
Ben Skeggs586491e2014-08-10 04:10:24 +1000420 }, sizeof(struct nv_device_v0),
Ben Skeggs0ad72862014-08-10 04:10:22 +1000421 &drm->device);
Ben Skeggs94580292012-07-06 12:14:00 +1000422 if (ret)
423 goto fail_device;
424
Ilia Mirkin7d3428c2014-01-29 19:53:00 -0500425 dev->irq_enabled = true;
426
Ben Skeggs77145f12012-07-31 16:16:21 +1000427 /* workaround an odd issue on nvc1 by disabling the device's
428 * nosnoop capability. hopefully won't cause issues until a
429 * better fix is found - assuming there is one...
430 */
Ben Skeggs967e7bd2014-08-10 04:10:22 +1000431 if (drm->device.info.chipset == 0xc1)
432 nvif_mask(&drm->device, 0x00088080, 0x00000800, 0x00000000);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000433
Ben Skeggs77145f12012-07-31 16:16:21 +1000434 nouveau_vga_init(drm);
Ben Skeggscb75d972012-07-11 10:44:20 +1000435 nouveau_agp_init(drm);
436
Ben Skeggs967e7bd2014-08-10 04:10:22 +1000437 if (drm->device.info.family >= NV_DEVICE_INFO_V0_TESLA) {
438 ret = nouveau_vm_new(nvkm_device(&drm->device), 0, (1ULL << 40),
Ben Skeggs3ee6f5b2014-08-10 04:10:20 +1000439 0x1000, &drm->client.vm);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000440 if (ret)
441 goto fail_device;
Ben Skeggs3ee6f5b2014-08-10 04:10:20 +1000442
Ben Skeggs0ad72862014-08-10 04:10:22 +1000443 nvkm_client(&drm->client.base)->vm = drm->client.vm;
Ben Skeggsebb945a2012-07-20 08:17:34 +1000444 }
445
446 ret = nouveau_ttm_init(drm);
Ben Skeggs94580292012-07-06 12:14:00 +1000447 if (ret)
Ben Skeggs77145f12012-07-31 16:16:21 +1000448 goto fail_ttm;
Ben Skeggs94580292012-07-06 12:14:00 +1000449
Ben Skeggs77145f12012-07-31 16:16:21 +1000450 ret = nouveau_bios_init(dev);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000451 if (ret)
Ben Skeggs77145f12012-07-31 16:16:21 +1000452 goto fail_bios;
453
Ben Skeggs77145f12012-07-31 16:16:21 +1000454 ret = nouveau_display_create(dev);
455 if (ret)
456 goto fail_dispctor;
457
458 if (dev->mode_config.num_crtc) {
459 ret = nouveau_display_init(dev);
460 if (ret)
461 goto fail_dispinit;
462 }
463
Ben Skeggs26fdd782013-10-15 11:52:56 +1000464 nouveau_sysfs_init(dev);
Ben Skeggsb9ed9192013-10-15 09:44:02 +1000465 nouveau_hwmon_init(dev);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000466 nouveau_accel_init(drm);
467 nouveau_fbcon_init(dev);
Dave Airlie5addcf02012-09-10 14:20:51 +1000468
469 if (nouveau_runtime_pm != 0) {
470 pm_runtime_use_autosuspend(dev->dev);
471 pm_runtime_set_autosuspend_delay(dev->dev, 5000);
472 pm_runtime_set_active(dev->dev);
473 pm_runtime_allow(dev->dev);
474 pm_runtime_mark_last_busy(dev->dev);
475 pm_runtime_put(dev->dev);
476 }
Ben Skeggs94580292012-07-06 12:14:00 +1000477 return 0;
478
Ben Skeggs77145f12012-07-31 16:16:21 +1000479fail_dispinit:
480 nouveau_display_destroy(dev);
481fail_dispctor:
Ben Skeggs77145f12012-07-31 16:16:21 +1000482 nouveau_bios_takedown(dev);
483fail_bios:
Ben Skeggsebb945a2012-07-20 08:17:34 +1000484 nouveau_ttm_fini(drm);
Ben Skeggs77145f12012-07-31 16:16:21 +1000485fail_ttm:
486 nouveau_agp_fini(drm);
487 nouveau_vga_fini(drm);
Ben Skeggs94580292012-07-06 12:14:00 +1000488fail_device:
Ben Skeggs0ad72862014-08-10 04:10:22 +1000489 nvif_device_fini(&drm->device);
Ben Skeggs94580292012-07-06 12:14:00 +1000490 nouveau_cli_destroy(&drm->client);
491 return ret;
492}
493
Marcin Slusarz5b8a43a2012-08-19 23:00:00 +0200494static int
Ben Skeggs94580292012-07-06 12:14:00 +1000495nouveau_drm_unload(struct drm_device *dev)
496{
Ben Skeggs77145f12012-07-31 16:16:21 +1000497 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs94580292012-07-06 12:14:00 +1000498
Dave Airlie5addcf02012-09-10 14:20:51 +1000499 pm_runtime_get_sync(dev->dev);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000500 nouveau_fbcon_fini(dev);
501 nouveau_accel_fini(drm);
Ben Skeggsb9ed9192013-10-15 09:44:02 +1000502 nouveau_hwmon_fini(dev);
Ben Skeggs26fdd782013-10-15 11:52:56 +1000503 nouveau_sysfs_fini(dev);
Ben Skeggs77145f12012-07-31 16:16:21 +1000504
Ben Skeggs94307382012-10-31 12:11:15 +1000505 if (dev->mode_config.num_crtc)
506 nouveau_display_fini(dev);
Ben Skeggs77145f12012-07-31 16:16:21 +1000507 nouveau_display_destroy(dev);
508
Ben Skeggs77145f12012-07-31 16:16:21 +1000509 nouveau_bios_takedown(dev);
Ben Skeggs94580292012-07-06 12:14:00 +1000510
Ben Skeggsebb945a2012-07-20 08:17:34 +1000511 nouveau_ttm_fini(drm);
Ben Skeggscb75d972012-07-11 10:44:20 +1000512 nouveau_agp_fini(drm);
Ben Skeggs77145f12012-07-31 16:16:21 +1000513 nouveau_vga_fini(drm);
Ben Skeggscb75d972012-07-11 10:44:20 +1000514
Ben Skeggs0ad72862014-08-10 04:10:22 +1000515 nvif_device_fini(&drm->device);
Dave Airlie5addcf02012-09-10 14:20:51 +1000516 if (drm->hdmi_device)
517 pci_dev_put(drm->hdmi_device);
Ben Skeggs94580292012-07-06 12:14:00 +1000518 nouveau_cli_destroy(&drm->client);
519 return 0;
520}
521
Alexandre Courbot8ba9ff12014-06-26 14:33:32 +0900522void
523nouveau_drm_device_remove(struct drm_device *dev)
Ben Skeggs94580292012-07-06 12:14:00 +1000524{
Ben Skeggs77145f12012-07-31 16:16:21 +1000525 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs0ad72862014-08-10 04:10:22 +1000526 struct nouveau_client *client;
Ben Skeggsebb945a2012-07-20 08:17:34 +1000527 struct nouveau_object *device;
Ben Skeggs77145f12012-07-31 16:16:21 +1000528
Ilia Mirkin7d3428c2014-01-29 19:53:00 -0500529 dev->irq_enabled = false;
Ben Skeggs0ad72862014-08-10 04:10:22 +1000530 client = nvkm_client(&drm->client.base);
531 device = client->device;
Ben Skeggs77145f12012-07-31 16:16:21 +1000532 drm_put_dev(dev);
533
Ben Skeggsebb945a2012-07-20 08:17:34 +1000534 nouveau_object_ref(NULL, &device);
535 nouveau_object_debug();
Ben Skeggs94580292012-07-06 12:14:00 +1000536}
Alexandre Courbot8ba9ff12014-06-26 14:33:32 +0900537
538static void
539nouveau_drm_remove(struct pci_dev *pdev)
540{
541 struct drm_device *dev = pci_get_drvdata(pdev);
542
543 nouveau_drm_device_remove(dev);
544}
Ben Skeggs94580292012-07-06 12:14:00 +1000545
Marcin Slusarzcd897832013-01-27 15:01:55 +0100546static int
Dave Airlie05c63c22014-03-26 14:10:06 +1000547nouveau_do_suspend(struct drm_device *dev, bool runtime)
Ben Skeggs94580292012-07-06 12:14:00 +1000548{
Ben Skeggs77145f12012-07-31 16:16:21 +1000549 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs94580292012-07-06 12:14:00 +1000550 struct nouveau_cli *cli;
551 int ret;
552
Ben Skeggs6fbb7022014-10-02 13:22:27 +1000553 if (dev->mode_config.num_crtc) {
554 NV_INFO(drm, "suspending console...\n");
555 nouveau_fbcon_set_suspend(dev, 1);
Ben Skeggsc52f4fa2013-11-08 14:38:40 +1000556 NV_INFO(drm, "suspending display...\n");
Ben Skeggs6fbb7022014-10-02 13:22:27 +1000557 ret = nouveau_display_suspend(dev, runtime);
Ben Skeggs94307382012-10-31 12:11:15 +1000558 if (ret)
559 return ret;
560 }
Ben Skeggs94580292012-07-06 12:14:00 +1000561
Ben Skeggsc52f4fa2013-11-08 14:38:40 +1000562 NV_INFO(drm, "evicting buffers...\n");
Ben Skeggsebb945a2012-07-20 08:17:34 +1000563 ttm_bo_evict_mm(&drm->ttm.bdev, TTM_PL_VRAM);
564
Ben Skeggsc52f4fa2013-11-08 14:38:40 +1000565 NV_INFO(drm, "waiting for kernel channels to go idle...\n");
Ben Skeggs81dff212013-05-07 08:33:10 +1000566 if (drm->cechan) {
567 ret = nouveau_channel_idle(drm->cechan);
568 if (ret)
Ilia Mirkinf3980dc2014-01-23 02:45:02 -0500569 goto fail_display;
Ben Skeggs81dff212013-05-07 08:33:10 +1000570 }
571
572 if (drm->channel) {
573 ret = nouveau_channel_idle(drm->channel);
574 if (ret)
Ilia Mirkinf3980dc2014-01-23 02:45:02 -0500575 goto fail_display;
Ben Skeggs81dff212013-05-07 08:33:10 +1000576 }
577
Ben Skeggsc52f4fa2013-11-08 14:38:40 +1000578 NV_INFO(drm, "suspending client object trees...\n");
Ben Skeggsebb945a2012-07-20 08:17:34 +1000579 if (drm->fence && nouveau_fence(drm)->suspend) {
Ilia Mirkinf3980dc2014-01-23 02:45:02 -0500580 if (!nouveau_fence(drm)->suspend(drm)) {
581 ret = -ENOMEM;
582 goto fail_display;
583 }
Ben Skeggsebb945a2012-07-20 08:17:34 +1000584 }
585
Ben Skeggs94580292012-07-06 12:14:00 +1000586 list_for_each_entry(cli, &drm->clients, head) {
Ben Skeggs0ad72862014-08-10 04:10:22 +1000587 ret = nvif_client_suspend(&cli->base);
Ben Skeggs94580292012-07-06 12:14:00 +1000588 if (ret)
589 goto fail_client;
590 }
591
Ben Skeggsc52f4fa2013-11-08 14:38:40 +1000592 NV_INFO(drm, "suspending kernel object tree...\n");
Ben Skeggs0ad72862014-08-10 04:10:22 +1000593 ret = nvif_client_suspend(&drm->client.base);
Ben Skeggs94580292012-07-06 12:14:00 +1000594 if (ret)
595 goto fail_client;
596
Ben Skeggscb75d972012-07-11 10:44:20 +1000597 nouveau_agp_fini(drm);
Ben Skeggs94580292012-07-06 12:14:00 +1000598 return 0;
599
600fail_client:
601 list_for_each_entry_continue_reverse(cli, &drm->clients, head) {
Ben Skeggs0ad72862014-08-10 04:10:22 +1000602 nvif_client_resume(&cli->base);
Ben Skeggs94580292012-07-06 12:14:00 +1000603 }
604
Ilia Mirkinf3980dc2014-01-23 02:45:02 -0500605 if (drm->fence && nouveau_fence(drm)->resume)
606 nouveau_fence(drm)->resume(drm);
607
608fail_display:
Ben Skeggs94307382012-10-31 12:11:15 +1000609 if (dev->mode_config.num_crtc) {
Ben Skeggsc52f4fa2013-11-08 14:38:40 +1000610 NV_INFO(drm, "resuming display...\n");
Ben Skeggs6fbb7022014-10-02 13:22:27 +1000611 nouveau_display_resume(dev, runtime);
Ben Skeggs94307382012-10-31 12:11:15 +1000612 }
Ben Skeggs94580292012-07-06 12:14:00 +1000613 return ret;
614}
615
Marcin Slusarzcd897832013-01-27 15:01:55 +0100616static int
Ben Skeggs6fbb7022014-10-02 13:22:27 +1000617nouveau_do_resume(struct drm_device *dev, bool runtime)
Dave Airlie2d8b9cc2012-11-02 11:04:28 +1000618{
619 struct nouveau_drm *drm = nouveau_drm(dev);
620 struct nouveau_cli *cli;
621
Ben Skeggsc52f4fa2013-11-08 14:38:40 +1000622 NV_INFO(drm, "re-enabling device...\n");
Ben Skeggs94580292012-07-06 12:14:00 +1000623
Ben Skeggscb75d972012-07-11 10:44:20 +1000624 nouveau_agp_reset(drm);
625
Ben Skeggsc52f4fa2013-11-08 14:38:40 +1000626 NV_INFO(drm, "resuming kernel object tree...\n");
Ben Skeggs0ad72862014-08-10 04:10:22 +1000627 nvif_client_resume(&drm->client.base);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000628 nouveau_agp_init(drm);
Ben Skeggs94580292012-07-06 12:14:00 +1000629
Ben Skeggsc52f4fa2013-11-08 14:38:40 +1000630 NV_INFO(drm, "resuming client object trees...\n");
Ben Skeggs81dff212013-05-07 08:33:10 +1000631 if (drm->fence && nouveau_fence(drm)->resume)
632 nouveau_fence(drm)->resume(drm);
633
Ben Skeggs94580292012-07-06 12:14:00 +1000634 list_for_each_entry(cli, &drm->clients, head) {
Ben Skeggs0ad72862014-08-10 04:10:22 +1000635 nvif_client_resume(&cli->base);
Ben Skeggs94580292012-07-06 12:14:00 +1000636 }
637
Ben Skeggs77145f12012-07-31 16:16:21 +1000638 nouveau_run_vbios_init(dev);
Ben Skeggs77145f12012-07-31 16:16:21 +1000639
Ben Skeggs94307382012-10-31 12:11:15 +1000640 if (dev->mode_config.num_crtc) {
Ben Skeggsc52f4fa2013-11-08 14:38:40 +1000641 NV_INFO(drm, "resuming display...\n");
Ben Skeggs6fbb7022014-10-02 13:22:27 +1000642 nouveau_display_resume(dev, runtime);
643 NV_INFO(drm, "resuming console...\n");
644 nouveau_fbcon_set_suspend(dev, 0);
Ben Skeggs94307382012-10-31 12:11:15 +1000645 }
Dave Airlie5addcf02012-09-10 14:20:51 +1000646
Ben Skeggs77145f12012-07-31 16:16:21 +1000647 return 0;
Ben Skeggs94580292012-07-06 12:14:00 +1000648}
649
Ben Skeggs7bb6d442014-10-02 13:31:00 +1000650int
651nouveau_pmops_suspend(struct device *dev)
652{
653 struct pci_dev *pdev = to_pci_dev(dev);
654 struct drm_device *drm_dev = pci_get_drvdata(pdev);
655 int ret;
656
657 if (drm_dev->switch_power_state == DRM_SWITCH_POWER_OFF ||
658 drm_dev->switch_power_state == DRM_SWITCH_POWER_DYNAMIC_OFF)
659 return 0;
660
661 ret = nouveau_do_suspend(drm_dev, false);
662 if (ret)
663 return ret;
664
665 pci_save_state(pdev);
666 pci_disable_device(pdev);
Ben Skeggs7bb6d442014-10-02 13:31:00 +1000667 pci_set_power_state(pdev, PCI_D3hot);
668 return 0;
669}
670
671int
672nouveau_pmops_resume(struct device *dev)
Dave Airlie2d8b9cc2012-11-02 11:04:28 +1000673{
674 struct pci_dev *pdev = to_pci_dev(dev);
675 struct drm_device *drm_dev = pci_get_drvdata(pdev);
676 int ret;
677
Dave Airlie5addcf02012-09-10 14:20:51 +1000678 if (drm_dev->switch_power_state == DRM_SWITCH_POWER_OFF ||
679 drm_dev->switch_power_state == DRM_SWITCH_POWER_DYNAMIC_OFF)
Dave Airlie2d8b9cc2012-11-02 11:04:28 +1000680 return 0;
681
682 pci_set_power_state(pdev, PCI_D0);
683 pci_restore_state(pdev);
684 ret = pci_enable_device(pdev);
685 if (ret)
686 return ret;
687 pci_set_master(pdev);
688
Ben Skeggs6fbb7022014-10-02 13:22:27 +1000689 return nouveau_do_resume(drm_dev, false);
Dave Airlie2d8b9cc2012-11-02 11:04:28 +1000690}
691
Ben Skeggs7bb6d442014-10-02 13:31:00 +1000692static int
693nouveau_pmops_freeze(struct device *dev)
Dave Airlie2d8b9cc2012-11-02 11:04:28 +1000694{
695 struct pci_dev *pdev = to_pci_dev(dev);
696 struct drm_device *drm_dev = pci_get_drvdata(pdev);
Ben Skeggs6fbb7022014-10-02 13:22:27 +1000697 return nouveau_do_suspend(drm_dev, false);
Dave Airlie2d8b9cc2012-11-02 11:04:28 +1000698}
699
Ben Skeggs7bb6d442014-10-02 13:31:00 +1000700static int
701nouveau_pmops_thaw(struct device *dev)
Dave Airlie2d8b9cc2012-11-02 11:04:28 +1000702{
703 struct pci_dev *pdev = to_pci_dev(dev);
704 struct drm_device *drm_dev = pci_get_drvdata(pdev);
Ben Skeggs6fbb7022014-10-02 13:22:27 +1000705 return nouveau_do_resume(drm_dev, false);
Dave Airlie2d8b9cc2012-11-02 11:04:28 +1000706}
707
Ben Skeggs7bb6d442014-10-02 13:31:00 +1000708static int
709nouveau_pmops_runtime_suspend(struct device *dev)
710{
711 struct pci_dev *pdev = to_pci_dev(dev);
712 struct drm_device *drm_dev = pci_get_drvdata(pdev);
713 int ret;
714
715 if (nouveau_runtime_pm == 0) {
716 pm_runtime_forbid(dev);
717 return -EBUSY;
718 }
719
720 /* are we optimus enabled? */
721 if (nouveau_runtime_pm == -1 && !nouveau_is_optimus() && !nouveau_is_v1_dsm()) {
722 DRM_DEBUG_DRIVER("failing to power off - not optimus\n");
723 pm_runtime_forbid(dev);
724 return -EBUSY;
725 }
726
727 nv_debug_level(SILENT);
728 drm_kms_helper_poll_disable(drm_dev);
729 vga_switcheroo_set_dynamic_switch(pdev, VGA_SWITCHEROO_OFF);
730 nouveau_switcheroo_optimus_dsm();
731 ret = nouveau_do_suspend(drm_dev, true);
732 pci_save_state(pdev);
733 pci_disable_device(pdev);
Dave Airlie8c863942014-12-08 10:33:52 +1000734 pci_ignore_hotplug(pdev);
Ben Skeggs7bb6d442014-10-02 13:31:00 +1000735 pci_set_power_state(pdev, PCI_D3cold);
736 drm_dev->switch_power_state = DRM_SWITCH_POWER_DYNAMIC_OFF;
737 return ret;
738}
739
740static int
741nouveau_pmops_runtime_resume(struct device *dev)
742{
743 struct pci_dev *pdev = to_pci_dev(dev);
744 struct drm_device *drm_dev = pci_get_drvdata(pdev);
745 struct nvif_device *device = &nouveau_drm(drm_dev)->device;
746 int ret;
747
748 if (nouveau_runtime_pm == 0)
749 return -EINVAL;
750
751 pci_set_power_state(pdev, PCI_D0);
752 pci_restore_state(pdev);
753 ret = pci_enable_device(pdev);
754 if (ret)
755 return ret;
756 pci_set_master(pdev);
757
758 ret = nouveau_do_resume(drm_dev, true);
759 drm_kms_helper_poll_enable(drm_dev);
760 /* do magic */
761 nvif_mask(device, 0x88488, (1 << 25), (1 << 25));
762 vga_switcheroo_set_dynamic_switch(pdev, VGA_SWITCHEROO_ON);
763 drm_dev->switch_power_state = DRM_SWITCH_POWER_ON;
764 nv_debug_level(NORMAL);
765 return ret;
766}
767
768static int
769nouveau_pmops_runtime_idle(struct device *dev)
770{
771 struct pci_dev *pdev = to_pci_dev(dev);
772 struct drm_device *drm_dev = pci_get_drvdata(pdev);
773 struct nouveau_drm *drm = nouveau_drm(drm_dev);
774 struct drm_crtc *crtc;
775
776 if (nouveau_runtime_pm == 0) {
777 pm_runtime_forbid(dev);
778 return -EBUSY;
779 }
780
781 /* are we optimus enabled? */
782 if (nouveau_runtime_pm == -1 && !nouveau_is_optimus() && !nouveau_is_v1_dsm()) {
783 DRM_DEBUG_DRIVER("failing to power off - not optimus\n");
784 pm_runtime_forbid(dev);
785 return -EBUSY;
786 }
787
788 /* if we have a hdmi audio device - make sure it has a driver loaded */
789 if (drm->hdmi_device) {
790 if (!drm->hdmi_device->driver) {
791 DRM_DEBUG_DRIVER("failing to power off - no HDMI audio driver loaded\n");
792 pm_runtime_mark_last_busy(dev);
793 return -EBUSY;
794 }
795 }
796
797 list_for_each_entry(crtc, &drm->dev->mode_config.crtc_list, head) {
798 if (crtc->enabled) {
799 DRM_DEBUG_DRIVER("failing to power off - crtc active\n");
800 return -EBUSY;
801 }
802 }
803 pm_runtime_mark_last_busy(dev);
804 pm_runtime_autosuspend(dev);
805 /* we don't want the main rpm_idle to call suspend - we want to autosuspend */
806 return 1;
807}
Dave Airlie2d8b9cc2012-11-02 11:04:28 +1000808
Marcin Slusarz5b8a43a2012-08-19 23:00:00 +0200809static int
Ben Skeggsebb945a2012-07-20 08:17:34 +1000810nouveau_drm_open(struct drm_device *dev, struct drm_file *fpriv)
811{
Ben Skeggsebb945a2012-07-20 08:17:34 +1000812 struct nouveau_drm *drm = nouveau_drm(dev);
813 struct nouveau_cli *cli;
Marcin Slusarza2896ce2012-12-09 15:45:21 +0100814 char name[32], tmpname[TASK_COMM_LEN];
Ben Skeggsebb945a2012-07-20 08:17:34 +1000815 int ret;
816
Dave Airlie5addcf02012-09-10 14:20:51 +1000817 /* need to bring up power immediately if opening device */
818 ret = pm_runtime_get_sync(dev->dev);
Alexandre Courbotb6c42852014-02-12 14:00:59 +0900819 if (ret < 0 && ret != -EACCES)
Dave Airlie5addcf02012-09-10 14:20:51 +1000820 return ret;
821
Marcin Slusarza2896ce2012-12-09 15:45:21 +0100822 get_task_comm(tmpname, current);
823 snprintf(name, sizeof(name), "%s[%d]", tmpname, pid_nr(fpriv->pid));
Ben Skeggsfa6df8c2012-09-12 13:09:23 +1000824
Alexandre Courbot420b9462014-02-17 15:17:26 +0900825 ret = nouveau_cli_create(nouveau_name(dev), name, sizeof(*cli),
826 (void **)&cli);
827
Ben Skeggsebb945a2012-07-20 08:17:34 +1000828 if (ret)
Dave Airlie5addcf02012-09-10 14:20:51 +1000829 goto out_suspend;
Ben Skeggsebb945a2012-07-20 08:17:34 +1000830
Ben Skeggs0ad72862014-08-10 04:10:22 +1000831 cli->base.super = false;
832
Ben Skeggs967e7bd2014-08-10 04:10:22 +1000833 if (drm->device.info.family >= NV_DEVICE_INFO_V0_TESLA) {
834 ret = nouveau_vm_new(nvkm_device(&drm->device), 0, (1ULL << 40),
Ben Skeggs3ee6f5b2014-08-10 04:10:20 +1000835 0x1000, &cli->vm);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000836 if (ret) {
837 nouveau_cli_destroy(cli);
Dave Airlie5addcf02012-09-10 14:20:51 +1000838 goto out_suspend;
Ben Skeggsebb945a2012-07-20 08:17:34 +1000839 }
Ben Skeggs3ee6f5b2014-08-10 04:10:20 +1000840
Ben Skeggs0ad72862014-08-10 04:10:22 +1000841 nvkm_client(&cli->base)->vm = cli->vm;
Ben Skeggsebb945a2012-07-20 08:17:34 +1000842 }
843
844 fpriv->driver_priv = cli;
845
846 mutex_lock(&drm->client.mutex);
847 list_add(&cli->head, &drm->clients);
848 mutex_unlock(&drm->client.mutex);
Dave Airlie5addcf02012-09-10 14:20:51 +1000849
850out_suspend:
851 pm_runtime_mark_last_busy(dev->dev);
852 pm_runtime_put_autosuspend(dev->dev);
853
854 return ret;
Ben Skeggsebb945a2012-07-20 08:17:34 +1000855}
856
Marcin Slusarz5b8a43a2012-08-19 23:00:00 +0200857static void
Ben Skeggsebb945a2012-07-20 08:17:34 +1000858nouveau_drm_preclose(struct drm_device *dev, struct drm_file *fpriv)
859{
860 struct nouveau_cli *cli = nouveau_cli(fpriv);
861 struct nouveau_drm *drm = nouveau_drm(dev);
862
Dave Airlie5addcf02012-09-10 14:20:51 +1000863 pm_runtime_get_sync(dev->dev);
864
Ben Skeggsebb945a2012-07-20 08:17:34 +1000865 if (cli->abi16)
866 nouveau_abi16_fini(cli->abi16);
867
868 mutex_lock(&drm->client.mutex);
869 list_del(&cli->head);
870 mutex_unlock(&drm->client.mutex);
Dave Airlie5addcf02012-09-10 14:20:51 +1000871
Ben Skeggsebb945a2012-07-20 08:17:34 +1000872}
873
Marcin Slusarz5b8a43a2012-08-19 23:00:00 +0200874static void
Ben Skeggsebb945a2012-07-20 08:17:34 +1000875nouveau_drm_postclose(struct drm_device *dev, struct drm_file *fpriv)
876{
877 struct nouveau_cli *cli = nouveau_cli(fpriv);
878 nouveau_cli_destroy(cli);
Dave Airlie5addcf02012-09-10 14:20:51 +1000879 pm_runtime_mark_last_busy(dev->dev);
880 pm_runtime_put_autosuspend(dev->dev);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000881}
882
Rob Clarkbaa70942013-08-02 13:27:49 -0400883static const struct drm_ioctl_desc
Ben Skeggs77145f12012-07-31 16:16:21 +1000884nouveau_ioctls[] = {
Martin Peres7d761252013-08-25 18:29:02 +0200885 DRM_IOCTL_DEF_DRV(NOUVEAU_GETPARAM, nouveau_abi16_ioctl_getparam, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
Ben Skeggs77145f12012-07-31 16:16:21 +1000886 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 +0200887 DRM_IOCTL_DEF_DRV(NOUVEAU_CHANNEL_ALLOC, nouveau_abi16_ioctl_channel_alloc, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
888 DRM_IOCTL_DEF_DRV(NOUVEAU_CHANNEL_FREE, nouveau_abi16_ioctl_channel_free, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
889 DRM_IOCTL_DEF_DRV(NOUVEAU_GROBJ_ALLOC, nouveau_abi16_ioctl_grobj_alloc, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
890 DRM_IOCTL_DEF_DRV(NOUVEAU_NOTIFIEROBJ_ALLOC, nouveau_abi16_ioctl_notifierobj_alloc, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
891 DRM_IOCTL_DEF_DRV(NOUVEAU_GPUOBJ_FREE, nouveau_abi16_ioctl_gpuobj_free, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
892 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_NEW, nouveau_gem_ioctl_new, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
893 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_PUSHBUF, nouveau_gem_ioctl_pushbuf, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
894 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_CPU_PREP, nouveau_gem_ioctl_cpu_prep, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
895 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_CPU_FINI, nouveau_gem_ioctl_cpu_fini, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
896 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 +1000897};
898
Ben Skeggs27111a22014-08-10 04:10:31 +1000899long
900nouveau_drm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Dave Airlie5addcf02012-09-10 14:20:51 +1000901{
Ben Skeggs27111a22014-08-10 04:10:31 +1000902 struct drm_file *filp = file->private_data;
903 struct drm_device *dev = filp->minor->dev;
Dave Airlie5addcf02012-09-10 14:20:51 +1000904 long ret;
Dave Airlie5addcf02012-09-10 14:20:51 +1000905
906 ret = pm_runtime_get_sync(dev->dev);
Alexandre Courbotb6c42852014-02-12 14:00:59 +0900907 if (ret < 0 && ret != -EACCES)
Dave Airlie5addcf02012-09-10 14:20:51 +1000908 return ret;
909
Ben Skeggs27111a22014-08-10 04:10:31 +1000910 switch (_IOC_NR(cmd) - DRM_COMMAND_BASE) {
911 case DRM_NOUVEAU_NVIF:
912 ret = usif_ioctl(filp, (void __user *)arg, _IOC_SIZE(cmd));
913 break;
914 default:
915 ret = drm_ioctl(file, cmd, arg);
916 break;
917 }
Dave Airlie5addcf02012-09-10 14:20:51 +1000918
919 pm_runtime_mark_last_busy(dev->dev);
920 pm_runtime_put_autosuspend(dev->dev);
921 return ret;
922}
Ben Skeggs27111a22014-08-10 04:10:31 +1000923
Ben Skeggs77145f12012-07-31 16:16:21 +1000924static const struct file_operations
925nouveau_driver_fops = {
926 .owner = THIS_MODULE,
927 .open = drm_open,
928 .release = drm_release,
Dave Airlie5addcf02012-09-10 14:20:51 +1000929 .unlocked_ioctl = nouveau_drm_ioctl,
Ben Skeggs77145f12012-07-31 16:16:21 +1000930 .mmap = nouveau_ttm_mmap,
931 .poll = drm_poll,
Ben Skeggs77145f12012-07-31 16:16:21 +1000932 .read = drm_read,
933#if defined(CONFIG_COMPAT)
934 .compat_ioctl = nouveau_compat_ioctl,
935#endif
936 .llseek = noop_llseek,
937};
938
939static struct drm_driver
David Herrmann915b4d12014-08-29 12:12:43 +0200940driver_stub = {
Ben Skeggs77145f12012-07-31 16:16:21 +1000941 .driver_features =
Daniel Vetter4cb4ea32013-07-10 14:11:54 +0200942 DRIVER_USE_AGP |
Martin Peres7d761252013-08-25 18:29:02 +0200943 DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME | DRIVER_RENDER,
Ben Skeggs77145f12012-07-31 16:16:21 +1000944
945 .load = nouveau_drm_load,
946 .unload = nouveau_drm_unload,
947 .open = nouveau_drm_open,
948 .preclose = nouveau_drm_preclose,
949 .postclose = nouveau_drm_postclose,
950 .lastclose = nouveau_vga_lastclose,
951
Marcin Slusarz33b903e2013-02-08 21:42:13 +0100952#if defined(CONFIG_DEBUG_FS)
953 .debugfs_init = nouveau_debugfs_init,
954 .debugfs_cleanup = nouveau_debugfs_takedown,
955#endif
956
Ben Skeggs77145f12012-07-31 16:16:21 +1000957 .get_vblank_counter = drm_vblank_count,
Ben Skeggs51cb4b32013-10-03 07:02:29 +1000958 .enable_vblank = nouveau_display_vblank_enable,
959 .disable_vblank = nouveau_display_vblank_disable,
Ben Skeggsd83ef852013-11-14 13:37:49 +1000960 .get_scanout_position = nouveau_display_scanoutpos,
961 .get_vblank_timestamp = nouveau_display_vblstamp,
Ben Skeggs77145f12012-07-31 16:16:21 +1000962
963 .ioctls = nouveau_ioctls,
Rob Clarkbaa70942013-08-02 13:27:49 -0400964 .num_ioctls = ARRAY_SIZE(nouveau_ioctls),
Ben Skeggs77145f12012-07-31 16:16:21 +1000965 .fops = &nouveau_driver_fops,
966
967 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
968 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
Aaron Plattnerab9ccb92013-01-15 20:47:43 +0000969 .gem_prime_export = drm_gem_prime_export,
970 .gem_prime_import = drm_gem_prime_import,
971 .gem_prime_pin = nouveau_gem_prime_pin,
Maarten Lankhorst3aac4502014-07-01 12:57:26 +0200972 .gem_prime_res_obj = nouveau_gem_prime_res_obj,
Maarten Lankhorst1af7c7d2013-06-27 13:38:19 +0200973 .gem_prime_unpin = nouveau_gem_prime_unpin,
Aaron Plattnerab9ccb92013-01-15 20:47:43 +0000974 .gem_prime_get_sg_table = nouveau_gem_prime_get_sg_table,
975 .gem_prime_import_sg_table = nouveau_gem_prime_import_sg_table,
976 .gem_prime_vmap = nouveau_gem_prime_vmap,
977 .gem_prime_vunmap = nouveau_gem_prime_vunmap,
Ben Skeggs77145f12012-07-31 16:16:21 +1000978
Ben Skeggs77145f12012-07-31 16:16:21 +1000979 .gem_free_object = nouveau_gem_object_del,
980 .gem_open_object = nouveau_gem_object_open,
981 .gem_close_object = nouveau_gem_object_close,
982
983 .dumb_create = nouveau_display_dumb_create,
984 .dumb_map_offset = nouveau_display_dumb_map_offset,
Daniel Vetter43387b32013-07-16 09:12:04 +0200985 .dumb_destroy = drm_gem_dumb_destroy,
Ben Skeggs77145f12012-07-31 16:16:21 +1000986
987 .name = DRIVER_NAME,
988 .desc = DRIVER_DESC,
989#ifdef GIT_REVISION
990 .date = GIT_REVISION,
991#else
992 .date = DRIVER_DATE,
993#endif
994 .major = DRIVER_MAJOR,
995 .minor = DRIVER_MINOR,
996 .patchlevel = DRIVER_PATCHLEVEL,
997};
998
Ben Skeggs94580292012-07-06 12:14:00 +1000999static struct pci_device_id
1000nouveau_drm_pci_table[] = {
1001 {
1002 PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_ANY_ID),
1003 .class = PCI_BASE_CLASS_DISPLAY << 16,
1004 .class_mask = 0xff << 16,
1005 },
1006 {
1007 PCI_DEVICE(PCI_VENDOR_ID_NVIDIA_SGS, PCI_ANY_ID),
1008 .class = PCI_BASE_CLASS_DISPLAY << 16,
1009 .class_mask = 0xff << 16,
1010 },
1011 {}
1012};
1013
Pierre Moreau703fa262014-08-18 22:43:24 +02001014static void nouveau_display_options(void)
1015{
1016 DRM_DEBUG_DRIVER("Loading Nouveau with parameters:\n");
1017
1018 DRM_DEBUG_DRIVER("... tv_disable : %d\n", nouveau_tv_disable);
1019 DRM_DEBUG_DRIVER("... ignorelid : %d\n", nouveau_ignorelid);
1020 DRM_DEBUG_DRIVER("... duallink : %d\n", nouveau_duallink);
1021 DRM_DEBUG_DRIVER("... nofbaccel : %d\n", nouveau_nofbaccel);
1022 DRM_DEBUG_DRIVER("... config : %s\n", nouveau_config);
1023 DRM_DEBUG_DRIVER("... debug : %s\n", nouveau_debug);
1024 DRM_DEBUG_DRIVER("... noaccel : %d\n", nouveau_noaccel);
1025 DRM_DEBUG_DRIVER("... modeset : %d\n", nouveau_modeset);
1026 DRM_DEBUG_DRIVER("... runpm : %d\n", nouveau_runtime_pm);
1027 DRM_DEBUG_DRIVER("... vram_pushbuf : %d\n", nouveau_vram_pushbuf);
1028 DRM_DEBUG_DRIVER("... pstate : %d\n", nouveau_pstate);
1029}
1030
Dave Airlie2d8b9cc2012-11-02 11:04:28 +10001031static const struct dev_pm_ops nouveau_pm_ops = {
1032 .suspend = nouveau_pmops_suspend,
1033 .resume = nouveau_pmops_resume,
1034 .freeze = nouveau_pmops_freeze,
1035 .thaw = nouveau_pmops_thaw,
1036 .poweroff = nouveau_pmops_freeze,
1037 .restore = nouveau_pmops_resume,
Dave Airlie5addcf02012-09-10 14:20:51 +10001038 .runtime_suspend = nouveau_pmops_runtime_suspend,
1039 .runtime_resume = nouveau_pmops_runtime_resume,
1040 .runtime_idle = nouveau_pmops_runtime_idle,
Dave Airlie2d8b9cc2012-11-02 11:04:28 +10001041};
1042
Ben Skeggs94580292012-07-06 12:14:00 +10001043static struct pci_driver
1044nouveau_drm_pci_driver = {
1045 .name = "nouveau",
1046 .id_table = nouveau_drm_pci_table,
1047 .probe = nouveau_drm_probe,
1048 .remove = nouveau_drm_remove,
Dave Airlie2d8b9cc2012-11-02 11:04:28 +10001049 .driver.pm = &nouveau_pm_ops,
Ben Skeggs94580292012-07-06 12:14:00 +10001050};
1051
Alexandre Courbot8ba9ff12014-06-26 14:33:32 +09001052struct drm_device *
1053nouveau_platform_device_create_(struct platform_device *pdev, int size,
1054 void **pobject)
Alexandre Courbot420b9462014-02-17 15:17:26 +09001055{
Alexandre Courbot8ba9ff12014-06-26 14:33:32 +09001056 struct drm_device *drm;
1057 int err;
Alexandre Courbot420b9462014-02-17 15:17:26 +09001058
Alexandre Courbot8ba9ff12014-06-26 14:33:32 +09001059 err = nouveau_device_create_(pdev, NOUVEAU_BUS_PLATFORM,
Alexandre Courbot420b9462014-02-17 15:17:26 +09001060 nouveau_platform_name(pdev),
1061 dev_name(&pdev->dev), nouveau_config,
Alexandre Courbot8ba9ff12014-06-26 14:33:32 +09001062 nouveau_debug, size, pobject);
1063 if (err)
1064 return ERR_PTR(err);
Alexandre Courbot420b9462014-02-17 15:17:26 +09001065
David Herrmann915b4d12014-08-29 12:12:43 +02001066 drm = drm_dev_alloc(&driver_platform, &pdev->dev);
Alexandre Courbot8ba9ff12014-06-26 14:33:32 +09001067 if (!drm) {
1068 err = -ENOMEM;
1069 goto err_free;
Alexandre Courbot420b9462014-02-17 15:17:26 +09001070 }
1071
Alexandre Courbot8ba9ff12014-06-26 14:33:32 +09001072 err = drm_dev_set_unique(drm, "%s", dev_name(&pdev->dev));
1073 if (err < 0)
1074 goto err_free;
1075
1076 drm->platformdev = pdev;
1077 platform_set_drvdata(pdev, drm);
1078
1079 return drm;
1080
1081err_free:
1082 nouveau_object_ref(NULL, (struct nouveau_object **)pobject);
1083
1084 return ERR_PTR(err);
Alexandre Courbot420b9462014-02-17 15:17:26 +09001085}
1086
Ben Skeggs94580292012-07-06 12:14:00 +10001087static int __init
1088nouveau_drm_init(void)
1089{
David Herrmann915b4d12014-08-29 12:12:43 +02001090 driver_pci = driver_stub;
1091 driver_pci.set_busid = drm_pci_set_busid;
1092 driver_platform = driver_stub;
1093 driver_platform.set_busid = drm_platform_set_busid;
1094
Pierre Moreau703fa262014-08-18 22:43:24 +02001095 nouveau_display_options();
1096
Ben Skeggs77145f12012-07-31 16:16:21 +10001097 if (nouveau_modeset == -1) {
1098#ifdef CONFIG_VGA_CONSOLE
1099 if (vgacon_text_force())
1100 nouveau_modeset = 0;
Ben Skeggs77145f12012-07-31 16:16:21 +10001101#endif
Ben Skeggs77145f12012-07-31 16:16:21 +10001102 }
1103
1104 if (!nouveau_modeset)
1105 return 0;
1106
Alexandre Courbot055a65d2015-01-15 15:29:56 +09001107#ifdef CONFIG_NOUVEAU_PLATFORM_DRIVER
1108 platform_driver_register(&nouveau_platform_driver);
1109#endif
1110
Ben Skeggs77145f12012-07-31 16:16:21 +10001111 nouveau_register_dsm_handler();
David Herrmann915b4d12014-08-29 12:12:43 +02001112 return drm_pci_init(&driver_pci, &nouveau_drm_pci_driver);
Ben Skeggs94580292012-07-06 12:14:00 +10001113}
1114
1115static void __exit
1116nouveau_drm_exit(void)
1117{
Ben Skeggs77145f12012-07-31 16:16:21 +10001118 if (!nouveau_modeset)
1119 return;
1120
David Herrmann915b4d12014-08-29 12:12:43 +02001121 drm_pci_exit(&driver_pci, &nouveau_drm_pci_driver);
Ben Skeggs77145f12012-07-31 16:16:21 +10001122 nouveau_unregister_dsm_handler();
Alexandre Courbot055a65d2015-01-15 15:29:56 +09001123
1124#ifdef CONFIG_NOUVEAU_PLATFORM_DRIVER
1125 platform_driver_unregister(&nouveau_platform_driver);
1126#endif
Ben Skeggs94580292012-07-06 12:14:00 +10001127}
1128
1129module_init(nouveau_drm_init);
1130module_exit(nouveau_drm_exit);
1131
1132MODULE_DEVICE_TABLE(pci, nouveau_drm_pci_table);
Ben Skeggs77145f12012-07-31 16:16:21 +10001133MODULE_AUTHOR(DRIVER_AUTHOR);
1134MODULE_DESCRIPTION(DRIVER_DESC);
Ben Skeggs94580292012-07-06 12:14:00 +10001135MODULE_LICENSE("GPL and additional rights");