blob: e990327d117a8daf869b21c51946d02555d5a6bf [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>
28
29#include <core/device.h>
30#include <core/client.h>
Ben Skeggsebb945a2012-07-20 08:17:34 +100031#include <core/gpuobj.h>
Ben Skeggs94580292012-07-06 12:14:00 +100032#include <core/class.h>
33
Ben Skeggsdded35d2013-04-25 17:23:43 +100034#include <engine/device.h>
Ben Skeggs1d7c71a2013-01-31 09:23:34 +100035#include <engine/disp.h>
Ben Skeggs9fe72f92013-05-02 16:20:44 +100036#include <engine/fifo.h>
Ben Skeggs1d7c71a2013-01-31 09:23:34 +100037
Ben Skeggsdded35d2013-04-25 17:23:43 +100038#include <subdev/vm.h>
39
Ben Skeggs94580292012-07-06 12:14:00 +100040#include "nouveau_drm.h"
Ben Skeggsebb945a2012-07-20 08:17:34 +100041#include "nouveau_dma.h"
Ben Skeggs77145f12012-07-31 16:16:21 +100042#include "nouveau_ttm.h"
43#include "nouveau_gem.h"
Ben Skeggscb75d972012-07-11 10:44:20 +100044#include "nouveau_agp.h"
Ben Skeggs77145f12012-07-31 16:16:21 +100045#include "nouveau_vga.h"
46#include "nouveau_pm.h"
47#include "nouveau_acpi.h"
48#include "nouveau_bios.h"
49#include "nouveau_ioctl.h"
Ben Skeggsebb945a2012-07-20 08:17:34 +100050#include "nouveau_abi16.h"
51#include "nouveau_fbcon.h"
52#include "nouveau_fence.h"
Marcin Slusarz33b903e2013-02-08 21:42:13 +010053#include "nouveau_debugfs.h"
Ben Skeggsebb945a2012-07-20 08:17:34 +100054
Ben Skeggs94580292012-07-06 12:14:00 +100055MODULE_PARM_DESC(config, "option string to pass to driver core");
56static char *nouveau_config;
57module_param_named(config, nouveau_config, charp, 0400);
58
59MODULE_PARM_DESC(debug, "debug string to pass to driver core");
60static char *nouveau_debug;
61module_param_named(debug, nouveau_debug, charp, 0400);
62
Ben Skeggsebb945a2012-07-20 08:17:34 +100063MODULE_PARM_DESC(noaccel, "disable kernel/abi16 acceleration");
64static int nouveau_noaccel = 0;
65module_param_named(noaccel, nouveau_noaccel, int, 0400);
66
Ben Skeggs94307382012-10-31 12:11:15 +100067MODULE_PARM_DESC(modeset, "enable driver (default: auto, "
68 "0 = disabled, 1 = enabled, 2 = headless)");
69int nouveau_modeset = -1;
Ben Skeggs77145f12012-07-31 16:16:21 +100070module_param_named(modeset, nouveau_modeset, int, 0400);
71
72static struct drm_driver driver;
73
Ben Skeggs1d7c71a2013-01-31 09:23:34 +100074static int
Maarten Lankhorste4604d82013-03-24 12:56:30 +010075nouveau_drm_vblank_handler(struct nouveau_eventh *event, int head)
76{
77 struct nouveau_drm *drm =
78 container_of(event, struct nouveau_drm, vblank[head]);
79 drm_handle_vblank(drm->dev, head);
80 return NVKM_EVENT_KEEP;
81}
82
83static int
Ben Skeggs1d7c71a2013-01-31 09:23:34 +100084nouveau_drm_vblank_enable(struct drm_device *dev, int head)
85{
86 struct nouveau_drm *drm = nouveau_drm(dev);
87 struct nouveau_disp *pdisp = nouveau_disp(drm->device);
Maarten Lankhorste4604d82013-03-24 12:56:30 +010088
89 if (WARN_ON_ONCE(head > ARRAY_SIZE(drm->vblank)))
90 return -EIO;
91 WARN_ON_ONCE(drm->vblank[head].func);
92 drm->vblank[head].func = nouveau_drm_vblank_handler;
93 nouveau_event_get(pdisp->vblank, head, &drm->vblank[head]);
Ben Skeggs1d7c71a2013-01-31 09:23:34 +100094 return 0;
95}
96
97static void
98nouveau_drm_vblank_disable(struct drm_device *dev, int head)
99{
100 struct nouveau_drm *drm = nouveau_drm(dev);
101 struct nouveau_disp *pdisp = nouveau_disp(drm->device);
Maarten Lankhorste4604d82013-03-24 12:56:30 +0100102 if (drm->vblank[head].func)
103 nouveau_event_put(pdisp->vblank, head, &drm->vblank[head]);
104 else
105 WARN_ON_ONCE(1);
106 drm->vblank[head].func = NULL;
Ben Skeggs1d7c71a2013-01-31 09:23:34 +1000107}
108
Ben Skeggs94580292012-07-06 12:14:00 +1000109static u64
110nouveau_name(struct pci_dev *pdev)
111{
112 u64 name = (u64)pci_domain_nr(pdev->bus) << 32;
113 name |= pdev->bus->number << 16;
114 name |= PCI_SLOT(pdev->devfn) << 8;
115 return name | PCI_FUNC(pdev->devfn);
116}
117
118static int
Ben Skeggsfa6df8c2012-09-12 13:09:23 +1000119nouveau_cli_create(struct pci_dev *pdev, const char *name,
120 int size, void **pcli)
Ben Skeggs94580292012-07-06 12:14:00 +1000121{
122 struct nouveau_cli *cli;
123 int ret;
124
Marcin Slusarzdd5700e2012-12-10 21:59:52 +0100125 *pcli = NULL;
Ben Skeggs94580292012-07-06 12:14:00 +1000126 ret = nouveau_client_create_(name, nouveau_name(pdev), nouveau_config,
127 nouveau_debug, size, pcli);
128 cli = *pcli;
Marcin Slusarzdd5700e2012-12-10 21:59:52 +0100129 if (ret) {
130 if (cli)
131 nouveau_client_destroy(&cli->base);
132 *pcli = NULL;
Ben Skeggs94580292012-07-06 12:14:00 +1000133 return ret;
Marcin Slusarzdd5700e2012-12-10 21:59:52 +0100134 }
Ben Skeggs94580292012-07-06 12:14:00 +1000135
136 mutex_init(&cli->mutex);
137 return 0;
138}
139
140static void
141nouveau_cli_destroy(struct nouveau_cli *cli)
142{
143 struct nouveau_object *client = nv_object(cli);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000144 nouveau_vm_ref(NULL, &cli->base.vm, NULL);
Ben Skeggs94580292012-07-06 12:14:00 +1000145 nouveau_client_fini(&cli->base, false);
146 atomic_set(&client->refcount, 1);
147 nouveau_object_ref(NULL, &client);
148}
149
Ben Skeggsebb945a2012-07-20 08:17:34 +1000150static void
151nouveau_accel_fini(struct nouveau_drm *drm)
152{
153 nouveau_gpuobj_ref(NULL, &drm->notify);
154 nouveau_channel_del(&drm->channel);
Ben Skeggs49981042012-08-06 19:38:25 +1000155 nouveau_channel_del(&drm->cechan);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000156 if (drm->fence)
157 nouveau_fence(drm)->dtor(drm);
158}
159
160static void
161nouveau_accel_init(struct nouveau_drm *drm)
162{
163 struct nouveau_device *device = nv_device(drm->device);
164 struct nouveau_object *object;
Ben Skeggs49981042012-08-06 19:38:25 +1000165 u32 arg0, arg1;
Ben Skeggsebb945a2012-07-20 08:17:34 +1000166 int ret;
167
Ben Skeggs9fe72f92013-05-02 16:20:44 +1000168 if (nouveau_noaccel || !nouveau_fifo(device) /*XXX*/)
Ben Skeggsebb945a2012-07-20 08:17:34 +1000169 return;
170
171 /* initialise synchronisation routines */
172 if (device->card_type < NV_10) ret = nv04_fence_create(drm);
Ben Skeggs60e5cb72013-02-14 12:59:36 +1000173 else if (device->chipset < 0x17) ret = nv10_fence_create(drm);
174 else if (device->card_type < NV_50) ret = nv17_fence_create(drm);
Maarten Lankhorstace5a9b2012-11-21 13:21:12 +0100175 else if (device->chipset < 0x84) ret = nv50_fence_create(drm);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000176 else if (device->card_type < NV_C0) ret = nv84_fence_create(drm);
177 else ret = nvc0_fence_create(drm);
178 if (ret) {
179 NV_ERROR(drm, "failed to initialise sync subsystem, %d\n", ret);
180 nouveau_accel_fini(drm);
181 return;
182 }
183
Ben Skeggs49981042012-08-06 19:38:25 +1000184 if (device->card_type >= NV_E0) {
185 ret = nouveau_channel_new(drm, &drm->client, NVDRM_DEVICE,
186 NVDRM_CHAN + 1,
187 NVE0_CHANNEL_IND_ENGINE_CE0 |
188 NVE0_CHANNEL_IND_ENGINE_CE1, 0,
189 &drm->cechan);
190 if (ret)
191 NV_ERROR(drm, "failed to create ce channel, %d\n", ret);
192
193 arg0 = NVE0_CHANNEL_IND_ENGINE_GR;
Ben Skeggs49469802012-11-22 13:43:55 +1000194 arg1 = 1;
Ben Skeggs49981042012-08-06 19:38:25 +1000195 } else {
196 arg0 = NvDmaFB;
197 arg1 = NvDmaTT;
198 }
199
Ben Skeggsebb945a2012-07-20 08:17:34 +1000200 ret = nouveau_channel_new(drm, &drm->client, NVDRM_DEVICE, NVDRM_CHAN,
Ben Skeggs49981042012-08-06 19:38:25 +1000201 arg0, arg1, &drm->channel);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000202 if (ret) {
203 NV_ERROR(drm, "failed to create kernel channel, %d\n", ret);
204 nouveau_accel_fini(drm);
205 return;
206 }
207
208 if (device->card_type < NV_C0) {
209 ret = nouveau_gpuobj_new(drm->device, NULL, 32, 0, 0,
210 &drm->notify);
211 if (ret) {
212 NV_ERROR(drm, "failed to allocate notifier, %d\n", ret);
213 nouveau_accel_fini(drm);
214 return;
215 }
216
217 ret = nouveau_object_new(nv_object(drm),
218 drm->channel->handle, NvNotify0,
219 0x003d, &(struct nv_dma_class) {
220 .flags = NV_DMA_TARGET_VRAM |
221 NV_DMA_ACCESS_RDWR,
222 .start = drm->notify->addr,
223 .limit = drm->notify->addr + 31
224 }, sizeof(struct nv_dma_class),
225 &object);
226 if (ret) {
227 nouveau_accel_fini(drm);
228 return;
229 }
230 }
231
232
Ben Skeggs49981042012-08-06 19:38:25 +1000233 nouveau_bo_move_init(drm);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000234}
235
Greg Kroah-Hartman56550d92012-12-21 15:09:25 -0800236static int nouveau_drm_probe(struct pci_dev *pdev,
237 const struct pci_device_id *pent)
Ben Skeggs94580292012-07-06 12:14:00 +1000238{
239 struct nouveau_device *device;
Ben Skeggsebb945a2012-07-20 08:17:34 +1000240 struct apertures_struct *aper;
241 bool boot = false;
Ben Skeggs94580292012-07-06 12:14:00 +1000242 int ret;
243
Ben Skeggsebb945a2012-07-20 08:17:34 +1000244 /* remove conflicting drivers (vesafb, efifb etc) */
245 aper = alloc_apertures(3);
246 if (!aper)
247 return -ENOMEM;
248
249 aper->ranges[0].base = pci_resource_start(pdev, 1);
250 aper->ranges[0].size = pci_resource_len(pdev, 1);
251 aper->count = 1;
252
253 if (pci_resource_len(pdev, 2)) {
254 aper->ranges[aper->count].base = pci_resource_start(pdev, 2);
255 aper->ranges[aper->count].size = pci_resource_len(pdev, 2);
256 aper->count++;
257 }
258
259 if (pci_resource_len(pdev, 3)) {
260 aper->ranges[aper->count].base = pci_resource_start(pdev, 3);
261 aper->ranges[aper->count].size = pci_resource_len(pdev, 3);
262 aper->count++;
263 }
264
265#ifdef CONFIG_X86
266 boot = pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW;
267#endif
268 remove_conflicting_framebuffers(aper, "nouveaufb", boot);
Tommi Rantala83ef7772012-11-09 09:19:40 +0000269 kfree(aper);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000270
Ben Skeggs94580292012-07-06 12:14:00 +1000271 ret = nouveau_device_create(pdev, nouveau_name(pdev), pci_name(pdev),
272 nouveau_config, nouveau_debug, &device);
273 if (ret)
274 return ret;
275
276 pci_set_master(pdev);
277
Ben Skeggs77145f12012-07-31 16:16:21 +1000278 ret = drm_get_pci_dev(pdev, pent, &driver);
Ben Skeggs94580292012-07-06 12:14:00 +1000279 if (ret) {
Ben Skeggsebb945a2012-07-20 08:17:34 +1000280 nouveau_object_ref(NULL, (struct nouveau_object **)&device);
Ben Skeggs94580292012-07-06 12:14:00 +1000281 return ret;
282 }
283
284 return 0;
285}
286
Marcin Slusarz5f97ab92013-02-04 21:52:54 +0100287static struct lock_class_key drm_client_lock_class_key;
288
Marcin Slusarz5b8a43a2012-08-19 23:00:00 +0200289static int
Ben Skeggs94580292012-07-06 12:14:00 +1000290nouveau_drm_load(struct drm_device *dev, unsigned long flags)
291{
292 struct pci_dev *pdev = dev->pdev;
Ben Skeggsebb945a2012-07-20 08:17:34 +1000293 struct nouveau_device *device;
Ben Skeggs94580292012-07-06 12:14:00 +1000294 struct nouveau_drm *drm;
295 int ret;
296
Ben Skeggsfa6df8c2012-09-12 13:09:23 +1000297 ret = nouveau_cli_create(pdev, "DRM", sizeof(*drm), (void**)&drm);
Ben Skeggs94580292012-07-06 12:14:00 +1000298 if (ret)
299 return ret;
Marcin Slusarz5f97ab92013-02-04 21:52:54 +0100300 lockdep_set_class(&drm->client.mutex, &drm_client_lock_class_key);
Ben Skeggs94580292012-07-06 12:14:00 +1000301
Ben Skeggs77145f12012-07-31 16:16:21 +1000302 dev->dev_private = drm;
303 drm->dev = dev;
304
Ben Skeggs94580292012-07-06 12:14:00 +1000305 INIT_LIST_HEAD(&drm->clients);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000306 spin_lock_init(&drm->tile.lock);
Ben Skeggs94580292012-07-06 12:14:00 +1000307
Ben Skeggscb75d972012-07-11 10:44:20 +1000308 /* make sure AGP controller is in a consistent state before we
309 * (possibly) execute vbios init tables (see nouveau_agp.h)
310 */
311 if (drm_pci_device_is_agp(dev) && dev->agp) {
312 /* dummy device object, doesn't init anything, but allows
313 * agp code access to registers
314 */
315 ret = nouveau_object_new(nv_object(drm), NVDRM_CLIENT,
316 NVDRM_DEVICE, 0x0080,
317 &(struct nv_device_class) {
318 .device = ~0,
319 .disable =
320 ~(NV_DEVICE_DISABLE_MMIO |
321 NV_DEVICE_DISABLE_IDENTIFY),
322 .debug0 = ~0,
323 }, sizeof(struct nv_device_class),
324 &drm->device);
325 if (ret)
Ben Skeggsebb945a2012-07-20 08:17:34 +1000326 goto fail_device;
Ben Skeggscb75d972012-07-11 10:44:20 +1000327
328 nouveau_agp_reset(drm);
329 nouveau_object_del(nv_object(drm), NVDRM_CLIENT, NVDRM_DEVICE);
330 }
331
Ben Skeggs94580292012-07-06 12:14:00 +1000332 ret = nouveau_object_new(nv_object(drm), NVDRM_CLIENT, NVDRM_DEVICE,
333 0x0080, &(struct nv_device_class) {
334 .device = ~0,
335 .disable = 0,
336 .debug0 = 0,
337 }, sizeof(struct nv_device_class),
338 &drm->device);
339 if (ret)
340 goto fail_device;
341
Ben Skeggs77145f12012-07-31 16:16:21 +1000342 /* workaround an odd issue on nvc1 by disabling the device's
343 * nosnoop capability. hopefully won't cause issues until a
344 * better fix is found - assuming there is one...
345 */
Ben Skeggsebb945a2012-07-20 08:17:34 +1000346 device = nv_device(drm->device);
Ben Skeggs77145f12012-07-31 16:16:21 +1000347 if (nv_device(drm->device)->chipset == 0xc1)
348 nv_mask(device, 0x00088080, 0x00000800, 0x00000000);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000349
Ben Skeggs77145f12012-07-31 16:16:21 +1000350 nouveau_vga_init(drm);
Ben Skeggscb75d972012-07-11 10:44:20 +1000351 nouveau_agp_init(drm);
352
Ben Skeggsebb945a2012-07-20 08:17:34 +1000353 if (device->card_type >= NV_50) {
354 ret = nouveau_vm_new(nv_device(drm->device), 0, (1ULL << 40),
355 0x1000, &drm->client.base.vm);
356 if (ret)
357 goto fail_device;
358 }
359
360 ret = nouveau_ttm_init(drm);
Ben Skeggs94580292012-07-06 12:14:00 +1000361 if (ret)
Ben Skeggs77145f12012-07-31 16:16:21 +1000362 goto fail_ttm;
Ben Skeggs94580292012-07-06 12:14:00 +1000363
Ben Skeggs77145f12012-07-31 16:16:21 +1000364 ret = nouveau_bios_init(dev);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000365 if (ret)
Ben Skeggs77145f12012-07-31 16:16:21 +1000366 goto fail_bios;
367
Ben Skeggs77145f12012-07-31 16:16:21 +1000368 ret = nouveau_display_create(dev);
369 if (ret)
370 goto fail_dispctor;
371
372 if (dev->mode_config.num_crtc) {
373 ret = nouveau_display_init(dev);
374 if (ret)
375 goto fail_dispinit;
376 }
377
378 nouveau_pm_init(dev);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000379
380 nouveau_accel_init(drm);
381 nouveau_fbcon_init(dev);
Ben Skeggs94580292012-07-06 12:14:00 +1000382 return 0;
383
Ben Skeggs77145f12012-07-31 16:16:21 +1000384fail_dispinit:
385 nouveau_display_destroy(dev);
386fail_dispctor:
Ben Skeggs77145f12012-07-31 16:16:21 +1000387 nouveau_bios_takedown(dev);
388fail_bios:
Ben Skeggsebb945a2012-07-20 08:17:34 +1000389 nouveau_ttm_fini(drm);
Ben Skeggs77145f12012-07-31 16:16:21 +1000390fail_ttm:
391 nouveau_agp_fini(drm);
392 nouveau_vga_fini(drm);
Ben Skeggs94580292012-07-06 12:14:00 +1000393fail_device:
394 nouveau_cli_destroy(&drm->client);
395 return ret;
396}
397
Marcin Slusarz5b8a43a2012-08-19 23:00:00 +0200398static int
Ben Skeggs94580292012-07-06 12:14:00 +1000399nouveau_drm_unload(struct drm_device *dev)
400{
Ben Skeggs77145f12012-07-31 16:16:21 +1000401 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs94580292012-07-06 12:14:00 +1000402
Ben Skeggsebb945a2012-07-20 08:17:34 +1000403 nouveau_fbcon_fini(dev);
404 nouveau_accel_fini(drm);
405
Ben Skeggs77145f12012-07-31 16:16:21 +1000406 nouveau_pm_fini(dev);
407
Ben Skeggs94307382012-10-31 12:11:15 +1000408 if (dev->mode_config.num_crtc)
409 nouveau_display_fini(dev);
Ben Skeggs77145f12012-07-31 16:16:21 +1000410 nouveau_display_destroy(dev);
411
Ben Skeggs77145f12012-07-31 16:16:21 +1000412 nouveau_bios_takedown(dev);
Ben Skeggs94580292012-07-06 12:14:00 +1000413
Ben Skeggsebb945a2012-07-20 08:17:34 +1000414 nouveau_ttm_fini(drm);
Ben Skeggscb75d972012-07-11 10:44:20 +1000415 nouveau_agp_fini(drm);
Ben Skeggs77145f12012-07-31 16:16:21 +1000416 nouveau_vga_fini(drm);
Ben Skeggscb75d972012-07-11 10:44:20 +1000417
Ben Skeggs94580292012-07-06 12:14:00 +1000418 nouveau_cli_destroy(&drm->client);
419 return 0;
420}
421
422static void
423nouveau_drm_remove(struct pci_dev *pdev)
424{
Ben Skeggs77145f12012-07-31 16:16:21 +1000425 struct drm_device *dev = pci_get_drvdata(pdev);
426 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000427 struct nouveau_object *device;
Ben Skeggs77145f12012-07-31 16:16:21 +1000428
429 device = drm->client.base.device;
430 drm_put_dev(dev);
431
Ben Skeggsebb945a2012-07-20 08:17:34 +1000432 nouveau_object_ref(NULL, &device);
433 nouveau_object_debug();
Ben Skeggs94580292012-07-06 12:14:00 +1000434}
435
Marcin Slusarzcd897832013-01-27 15:01:55 +0100436static int
Dave Airlie2d8b9cc2012-11-02 11:04:28 +1000437nouveau_do_suspend(struct drm_device *dev)
Ben Skeggs94580292012-07-06 12:14:00 +1000438{
Ben Skeggs77145f12012-07-31 16:16:21 +1000439 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs94580292012-07-06 12:14:00 +1000440 struct nouveau_cli *cli;
441 int ret;
442
Ben Skeggs94307382012-10-31 12:11:15 +1000443 if (dev->mode_config.num_crtc) {
444 NV_INFO(drm, "suspending fbcon...\n");
445 nouveau_fbcon_set_suspend(dev, 1);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000446
Ben Skeggs94307382012-10-31 12:11:15 +1000447 NV_INFO(drm, "suspending display...\n");
448 ret = nouveau_display_suspend(dev);
449 if (ret)
450 return ret;
451 }
Ben Skeggs94580292012-07-06 12:14:00 +1000452
Ben Skeggsebb945a2012-07-20 08:17:34 +1000453 NV_INFO(drm, "evicting buffers...\n");
454 ttm_bo_evict_mm(&drm->ttm.bdev, TTM_PL_VRAM);
455
Ben Skeggs81dff212013-05-07 08:33:10 +1000456 NV_INFO(drm, "waiting for kernel channels to go idle...\n");
457 if (drm->cechan) {
458 ret = nouveau_channel_idle(drm->cechan);
459 if (ret)
460 return ret;
461 }
462
463 if (drm->channel) {
464 ret = nouveau_channel_idle(drm->channel);
465 if (ret)
466 return ret;
467 }
468
469 NV_INFO(drm, "suspending client object trees...\n");
Ben Skeggsebb945a2012-07-20 08:17:34 +1000470 if (drm->fence && nouveau_fence(drm)->suspend) {
471 if (!nouveau_fence(drm)->suspend(drm))
472 return -ENOMEM;
473 }
474
Ben Skeggs94580292012-07-06 12:14:00 +1000475 list_for_each_entry(cli, &drm->clients, head) {
476 ret = nouveau_client_fini(&cli->base, true);
477 if (ret)
478 goto fail_client;
479 }
480
Ben Skeggs81dff212013-05-07 08:33:10 +1000481 NV_INFO(drm, "suspending kernel object tree...\n");
Ben Skeggs94580292012-07-06 12:14:00 +1000482 ret = nouveau_client_fini(&drm->client.base, true);
483 if (ret)
484 goto fail_client;
485
Ben Skeggscb75d972012-07-11 10:44:20 +1000486 nouveau_agp_fini(drm);
Ben Skeggs94580292012-07-06 12:14:00 +1000487 return 0;
488
489fail_client:
490 list_for_each_entry_continue_reverse(cli, &drm->clients, head) {
491 nouveau_client_init(&cli->base);
492 }
493
Ben Skeggs94307382012-10-31 12:11:15 +1000494 if (dev->mode_config.num_crtc) {
495 NV_INFO(drm, "resuming display...\n");
496 nouveau_display_resume(dev);
497 }
Ben Skeggs94580292012-07-06 12:14:00 +1000498 return ret;
499}
500
Dave Airlie2d8b9cc2012-11-02 11:04:28 +1000501int nouveau_pmops_suspend(struct device *dev)
Ben Skeggs94580292012-07-06 12:14:00 +1000502{
Dave Airlie2d8b9cc2012-11-02 11:04:28 +1000503 struct pci_dev *pdev = to_pci_dev(dev);
504 struct drm_device *drm_dev = pci_get_drvdata(pdev);
Ben Skeggs94580292012-07-06 12:14:00 +1000505 int ret;
506
Dave Airlie2d8b9cc2012-11-02 11:04:28 +1000507 if (drm_dev->switch_power_state == DRM_SWITCH_POWER_OFF)
Ben Skeggs94580292012-07-06 12:14:00 +1000508 return 0;
509
Dave Airlie2d8b9cc2012-11-02 11:04:28 +1000510 ret = nouveau_do_suspend(drm_dev);
Ben Skeggs94580292012-07-06 12:14:00 +1000511 if (ret)
512 return ret;
Dave Airlie2d8b9cc2012-11-02 11:04:28 +1000513
514 pci_save_state(pdev);
515 pci_disable_device(pdev);
516 pci_set_power_state(pdev, PCI_D3hot);
517
518 return 0;
519}
520
Marcin Slusarzcd897832013-01-27 15:01:55 +0100521static int
Dave Airlie2d8b9cc2012-11-02 11:04:28 +1000522nouveau_do_resume(struct drm_device *dev)
523{
524 struct nouveau_drm *drm = nouveau_drm(dev);
525 struct nouveau_cli *cli;
526
527 NV_INFO(drm, "re-enabling device...\n");
Ben Skeggs94580292012-07-06 12:14:00 +1000528
Ben Skeggscb75d972012-07-11 10:44:20 +1000529 nouveau_agp_reset(drm);
530
Ben Skeggs81dff212013-05-07 08:33:10 +1000531 NV_INFO(drm, "resuming kernel object tree...\n");
Ben Skeggs94580292012-07-06 12:14:00 +1000532 nouveau_client_init(&drm->client.base);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000533 nouveau_agp_init(drm);
Ben Skeggs94580292012-07-06 12:14:00 +1000534
Ben Skeggs81dff212013-05-07 08:33:10 +1000535 NV_INFO(drm, "resuming client object trees...\n");
536 if (drm->fence && nouveau_fence(drm)->resume)
537 nouveau_fence(drm)->resume(drm);
538
Ben Skeggs94580292012-07-06 12:14:00 +1000539 list_for_each_entry(cli, &drm->clients, head) {
540 nouveau_client_init(&cli->base);
541 }
542
Ben Skeggs77145f12012-07-31 16:16:21 +1000543 nouveau_run_vbios_init(dev);
Ben Skeggs77145f12012-07-31 16:16:21 +1000544 nouveau_pm_resume(dev);
545
Ben Skeggs94307382012-10-31 12:11:15 +1000546 if (dev->mode_config.num_crtc) {
547 NV_INFO(drm, "resuming display...\n");
548 nouveau_display_resume(dev);
549 }
Ben Skeggs77145f12012-07-31 16:16:21 +1000550 return 0;
Ben Skeggs94580292012-07-06 12:14:00 +1000551}
552
Dave Airlie2d8b9cc2012-11-02 11:04:28 +1000553int nouveau_pmops_resume(struct device *dev)
554{
555 struct pci_dev *pdev = to_pci_dev(dev);
556 struct drm_device *drm_dev = pci_get_drvdata(pdev);
557 int ret;
558
559 if (drm_dev->switch_power_state == DRM_SWITCH_POWER_OFF)
560 return 0;
561
562 pci_set_power_state(pdev, PCI_D0);
563 pci_restore_state(pdev);
564 ret = pci_enable_device(pdev);
565 if (ret)
566 return ret;
567 pci_set_master(pdev);
568
569 return nouveau_do_resume(drm_dev);
570}
571
572static int nouveau_pmops_freeze(struct device *dev)
573{
574 struct pci_dev *pdev = to_pci_dev(dev);
575 struct drm_device *drm_dev = pci_get_drvdata(pdev);
576
577 return nouveau_do_suspend(drm_dev);
578}
579
580static int nouveau_pmops_thaw(struct device *dev)
581{
582 struct pci_dev *pdev = to_pci_dev(dev);
583 struct drm_device *drm_dev = pci_get_drvdata(pdev);
584
585 return nouveau_do_resume(drm_dev);
586}
587
588
Marcin Slusarz5b8a43a2012-08-19 23:00:00 +0200589static int
Ben Skeggsebb945a2012-07-20 08:17:34 +1000590nouveau_drm_open(struct drm_device *dev, struct drm_file *fpriv)
591{
592 struct pci_dev *pdev = dev->pdev;
593 struct nouveau_drm *drm = nouveau_drm(dev);
594 struct nouveau_cli *cli;
Marcin Slusarza2896ce2012-12-09 15:45:21 +0100595 char name[32], tmpname[TASK_COMM_LEN];
Ben Skeggsebb945a2012-07-20 08:17:34 +1000596 int ret;
597
Marcin Slusarza2896ce2012-12-09 15:45:21 +0100598 get_task_comm(tmpname, current);
599 snprintf(name, sizeof(name), "%s[%d]", tmpname, pid_nr(fpriv->pid));
Ben Skeggsfa6df8c2012-09-12 13:09:23 +1000600
601 ret = nouveau_cli_create(pdev, name, sizeof(*cli), (void **)&cli);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000602 if (ret)
603 return ret;
604
605 if (nv_device(drm->device)->card_type >= NV_50) {
606 ret = nouveau_vm_new(nv_device(drm->device), 0, (1ULL << 40),
607 0x1000, &cli->base.vm);
608 if (ret) {
609 nouveau_cli_destroy(cli);
610 return ret;
611 }
612 }
613
614 fpriv->driver_priv = cli;
615
616 mutex_lock(&drm->client.mutex);
617 list_add(&cli->head, &drm->clients);
618 mutex_unlock(&drm->client.mutex);
619 return 0;
620}
621
Marcin Slusarz5b8a43a2012-08-19 23:00:00 +0200622static void
Ben Skeggsebb945a2012-07-20 08:17:34 +1000623nouveau_drm_preclose(struct drm_device *dev, struct drm_file *fpriv)
624{
625 struct nouveau_cli *cli = nouveau_cli(fpriv);
626 struct nouveau_drm *drm = nouveau_drm(dev);
627
628 if (cli->abi16)
629 nouveau_abi16_fini(cli->abi16);
630
631 mutex_lock(&drm->client.mutex);
632 list_del(&cli->head);
633 mutex_unlock(&drm->client.mutex);
634}
635
Marcin Slusarz5b8a43a2012-08-19 23:00:00 +0200636static void
Ben Skeggsebb945a2012-07-20 08:17:34 +1000637nouveau_drm_postclose(struct drm_device *dev, struct drm_file *fpriv)
638{
639 struct nouveau_cli *cli = nouveau_cli(fpriv);
640 nouveau_cli_destroy(cli);
641}
642
Ben Skeggs77145f12012-07-31 16:16:21 +1000643static struct drm_ioctl_desc
644nouveau_ioctls[] = {
645 DRM_IOCTL_DEF_DRV(NOUVEAU_GETPARAM, nouveau_abi16_ioctl_getparam, DRM_UNLOCKED|DRM_AUTH),
646 DRM_IOCTL_DEF_DRV(NOUVEAU_SETPARAM, nouveau_abi16_ioctl_setparam, DRM_UNLOCKED|DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
647 DRM_IOCTL_DEF_DRV(NOUVEAU_CHANNEL_ALLOC, nouveau_abi16_ioctl_channel_alloc, DRM_UNLOCKED|DRM_AUTH),
648 DRM_IOCTL_DEF_DRV(NOUVEAU_CHANNEL_FREE, nouveau_abi16_ioctl_channel_free, DRM_UNLOCKED|DRM_AUTH),
649 DRM_IOCTL_DEF_DRV(NOUVEAU_GROBJ_ALLOC, nouveau_abi16_ioctl_grobj_alloc, DRM_UNLOCKED|DRM_AUTH),
650 DRM_IOCTL_DEF_DRV(NOUVEAU_NOTIFIEROBJ_ALLOC, nouveau_abi16_ioctl_notifierobj_alloc, DRM_UNLOCKED|DRM_AUTH),
651 DRM_IOCTL_DEF_DRV(NOUVEAU_GPUOBJ_FREE, nouveau_abi16_ioctl_gpuobj_free, DRM_UNLOCKED|DRM_AUTH),
652 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_NEW, nouveau_gem_ioctl_new, DRM_UNLOCKED|DRM_AUTH),
653 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_PUSHBUF, nouveau_gem_ioctl_pushbuf, DRM_UNLOCKED|DRM_AUTH),
654 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_CPU_PREP, nouveau_gem_ioctl_cpu_prep, DRM_UNLOCKED|DRM_AUTH),
655 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_CPU_FINI, nouveau_gem_ioctl_cpu_fini, DRM_UNLOCKED|DRM_AUTH),
656 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_INFO, nouveau_gem_ioctl_info, DRM_UNLOCKED|DRM_AUTH),
657};
658
659static const struct file_operations
660nouveau_driver_fops = {
661 .owner = THIS_MODULE,
662 .open = drm_open,
663 .release = drm_release,
664 .unlocked_ioctl = drm_ioctl,
665 .mmap = nouveau_ttm_mmap,
666 .poll = drm_poll,
667 .fasync = drm_fasync,
668 .read = drm_read,
669#if defined(CONFIG_COMPAT)
670 .compat_ioctl = nouveau_compat_ioctl,
671#endif
672 .llseek = noop_llseek,
673};
674
675static struct drm_driver
676driver = {
677 .driver_features =
Daniel Vetter4cb4ea32013-07-10 14:11:54 +0200678 DRIVER_USE_AGP |
Ben Skeggs0fa90612013-03-22 10:05:03 +1000679 DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME,
Ben Skeggs77145f12012-07-31 16:16:21 +1000680
681 .load = nouveau_drm_load,
682 .unload = nouveau_drm_unload,
683 .open = nouveau_drm_open,
684 .preclose = nouveau_drm_preclose,
685 .postclose = nouveau_drm_postclose,
686 .lastclose = nouveau_vga_lastclose,
687
Marcin Slusarz33b903e2013-02-08 21:42:13 +0100688#if defined(CONFIG_DEBUG_FS)
689 .debugfs_init = nouveau_debugfs_init,
690 .debugfs_cleanup = nouveau_debugfs_takedown,
691#endif
692
Ben Skeggs77145f12012-07-31 16:16:21 +1000693 .get_vblank_counter = drm_vblank_count,
Ben Skeggs1d7c71a2013-01-31 09:23:34 +1000694 .enable_vblank = nouveau_drm_vblank_enable,
695 .disable_vblank = nouveau_drm_vblank_disable,
Ben Skeggs77145f12012-07-31 16:16:21 +1000696
697 .ioctls = nouveau_ioctls,
698 .fops = &nouveau_driver_fops,
699
700 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
701 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
Aaron Plattnerab9ccb92013-01-15 20:47:43 +0000702 .gem_prime_export = drm_gem_prime_export,
703 .gem_prime_import = drm_gem_prime_import,
704 .gem_prime_pin = nouveau_gem_prime_pin,
Maarten Lankhorst1af7c7d2013-06-27 13:38:19 +0200705 .gem_prime_unpin = nouveau_gem_prime_unpin,
Aaron Plattnerab9ccb92013-01-15 20:47:43 +0000706 .gem_prime_get_sg_table = nouveau_gem_prime_get_sg_table,
707 .gem_prime_import_sg_table = nouveau_gem_prime_import_sg_table,
708 .gem_prime_vmap = nouveau_gem_prime_vmap,
709 .gem_prime_vunmap = nouveau_gem_prime_vunmap,
Ben Skeggs77145f12012-07-31 16:16:21 +1000710
711 .gem_init_object = nouveau_gem_object_new,
712 .gem_free_object = nouveau_gem_object_del,
713 .gem_open_object = nouveau_gem_object_open,
714 .gem_close_object = nouveau_gem_object_close,
715
716 .dumb_create = nouveau_display_dumb_create,
717 .dumb_map_offset = nouveau_display_dumb_map_offset,
718 .dumb_destroy = nouveau_display_dumb_destroy,
719
720 .name = DRIVER_NAME,
721 .desc = DRIVER_DESC,
722#ifdef GIT_REVISION
723 .date = GIT_REVISION,
724#else
725 .date = DRIVER_DATE,
726#endif
727 .major = DRIVER_MAJOR,
728 .minor = DRIVER_MINOR,
729 .patchlevel = DRIVER_PATCHLEVEL,
730};
731
Ben Skeggs94580292012-07-06 12:14:00 +1000732static struct pci_device_id
733nouveau_drm_pci_table[] = {
734 {
735 PCI_DEVICE(PCI_VENDOR_ID_NVIDIA, PCI_ANY_ID),
736 .class = PCI_BASE_CLASS_DISPLAY << 16,
737 .class_mask = 0xff << 16,
738 },
739 {
740 PCI_DEVICE(PCI_VENDOR_ID_NVIDIA_SGS, PCI_ANY_ID),
741 .class = PCI_BASE_CLASS_DISPLAY << 16,
742 .class_mask = 0xff << 16,
743 },
744 {}
745};
746
Dave Airlie2d8b9cc2012-11-02 11:04:28 +1000747static const struct dev_pm_ops nouveau_pm_ops = {
748 .suspend = nouveau_pmops_suspend,
749 .resume = nouveau_pmops_resume,
750 .freeze = nouveau_pmops_freeze,
751 .thaw = nouveau_pmops_thaw,
752 .poweroff = nouveau_pmops_freeze,
753 .restore = nouveau_pmops_resume,
754};
755
Ben Skeggs94580292012-07-06 12:14:00 +1000756static struct pci_driver
757nouveau_drm_pci_driver = {
758 .name = "nouveau",
759 .id_table = nouveau_drm_pci_table,
760 .probe = nouveau_drm_probe,
761 .remove = nouveau_drm_remove,
Dave Airlie2d8b9cc2012-11-02 11:04:28 +1000762 .driver.pm = &nouveau_pm_ops,
Ben Skeggs94580292012-07-06 12:14:00 +1000763};
764
765static int __init
766nouveau_drm_init(void)
767{
Ben Skeggs77145f12012-07-31 16:16:21 +1000768 driver.num_ioctls = ARRAY_SIZE(nouveau_ioctls);
769
770 if (nouveau_modeset == -1) {
771#ifdef CONFIG_VGA_CONSOLE
772 if (vgacon_text_force())
773 nouveau_modeset = 0;
Ben Skeggs77145f12012-07-31 16:16:21 +1000774#endif
Ben Skeggs77145f12012-07-31 16:16:21 +1000775 }
776
777 if (!nouveau_modeset)
778 return 0;
779
780 nouveau_register_dsm_handler();
781 return drm_pci_init(&driver, &nouveau_drm_pci_driver);
Ben Skeggs94580292012-07-06 12:14:00 +1000782}
783
784static void __exit
785nouveau_drm_exit(void)
786{
Ben Skeggs77145f12012-07-31 16:16:21 +1000787 if (!nouveau_modeset)
788 return;
789
790 drm_pci_exit(&driver, &nouveau_drm_pci_driver);
791 nouveau_unregister_dsm_handler();
Ben Skeggs94580292012-07-06 12:14:00 +1000792}
793
794module_init(nouveau_drm_init);
795module_exit(nouveau_drm_exit);
796
797MODULE_DEVICE_TABLE(pci, nouveau_drm_pci_table);
Ben Skeggs77145f12012-07-31 16:16:21 +1000798MODULE_AUTHOR(DRIVER_AUTHOR);
799MODULE_DESCRIPTION(DRIVER_DESC);
Ben Skeggs94580292012-07-06 12:14:00 +1000800MODULE_LICENSE("GPL and additional rights");