drm/nouveau/mc: port to subdev interfaces
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / drm / nouveau / nouveau_drv.c
1 /*
2  * Copyright 2005 Stephane Marchesin.
3  * All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22  * OTHER DEALINGS IN THE SOFTWARE.
23  */
24
25 #include <linux/console.h>
26 #include <linux/module.h>
27
28 #include "drmP.h"
29 #include "drm.h"
30 #include "drm_crtc_helper.h"
31 #include "nouveau_drv.h"
32 #include "nouveau_abi16.h"
33 #include "nouveau_hw.h"
34 #include "nouveau_fb.h"
35 #include "nouveau_fbcon.h"
36 #include "nouveau_pm.h"
37 #include <engine/fifo.h>
38 #include "nv50_display.h"
39
40 #include "drm_pciids.h"
41
42 MODULE_PARM_DESC(modeset, "Enable kernel modesetting");
43 int nouveau_modeset = -1;
44 module_param_named(modeset, nouveau_modeset, int, 0400);
45
46 MODULE_PARM_DESC(vram_pushbuf, "Force DMA push buffers to be in VRAM");
47 int nouveau_vram_pushbuf;
48 module_param_named(vram_pushbuf, nouveau_vram_pushbuf, int, 0400);
49
50 MODULE_PARM_DESC(vram_notify, "Force DMA notifiers to be in VRAM");
51 int nouveau_vram_notify = 0;
52 module_param_named(vram_notify, nouveau_vram_notify, int, 0400);
53
54 MODULE_PARM_DESC(vram_type, "Override detected VRAM type");
55 char *nouveau_vram_type;
56 module_param_named(vram_type, nouveau_vram_type, charp, 0400);
57
58 MODULE_PARM_DESC(duallink, "Allow dual-link TMDS (>=GeForce 8)");
59 int nouveau_duallink = 1;
60 module_param_named(duallink, nouveau_duallink, int, 0400);
61
62 MODULE_PARM_DESC(uscript_lvds, "LVDS output script table ID (>=GeForce 8)");
63 int nouveau_uscript_lvds = -1;
64 module_param_named(uscript_lvds, nouveau_uscript_lvds, int, 0400);
65
66 MODULE_PARM_DESC(uscript_tmds, "TMDS output script table ID (>=GeForce 8)");
67 int nouveau_uscript_tmds = -1;
68 module_param_named(uscript_tmds, nouveau_uscript_tmds, int, 0400);
69
70 MODULE_PARM_DESC(ignorelid, "Ignore ACPI lid status");
71 int nouveau_ignorelid = 0;
72 module_param_named(ignorelid, nouveau_ignorelid, int, 0400);
73
74 MODULE_PARM_DESC(noaccel, "Disable all acceleration");
75 int nouveau_noaccel = -1;
76 module_param_named(noaccel, nouveau_noaccel, int, 0400);
77
78 MODULE_PARM_DESC(nofbaccel, "Disable fbcon acceleration");
79 int nouveau_nofbaccel = 0;
80 module_param_named(nofbaccel, nouveau_nofbaccel, int, 0400);
81
82 MODULE_PARM_DESC(force_post, "Force POST");
83 int nouveau_force_post = 0;
84 module_param_named(force_post, nouveau_force_post, int, 0400);
85
86 MODULE_PARM_DESC(override_conntype, "Ignore DCB connector type");
87 int nouveau_override_conntype = 0;
88 module_param_named(override_conntype, nouveau_override_conntype, int, 0400);
89
90 MODULE_PARM_DESC(tv_disable, "Disable TV-out detection");
91 int nouveau_tv_disable = 0;
92 module_param_named(tv_disable, nouveau_tv_disable, int, 0400);
93
94 MODULE_PARM_DESC(tv_norm, "Default TV norm.\n"
95                  "\t\tSupported: PAL, PAL-M, PAL-N, PAL-Nc, NTSC-M, NTSC-J,\n"
96                  "\t\t\thd480i, hd480p, hd576i, hd576p, hd720p, hd1080i.\n"
97                  "\t\tDefault: PAL\n"
98                  "\t\t*NOTE* Ignored for cards with external TV encoders.");
99 char *nouveau_tv_norm;
100 module_param_named(tv_norm, nouveau_tv_norm, charp, 0400);
101
102 MODULE_PARM_DESC(reg_debug, "Register access debug bitmask:\n"
103                 "\t\t0x1 mc, 0x2 video, 0x4 fb, 0x8 extdev,\n"
104                 "\t\t0x10 crtc, 0x20 ramdac, 0x40 vgacrtc, 0x80 rmvio,\n"
105                 "\t\t0x100 vgaattr, 0x200 EVO (G80+)");
106 int nouveau_reg_debug;
107 module_param_named(reg_debug, nouveau_reg_debug, int, 0600);
108
109 MODULE_PARM_DESC(perflvl, "Performance level (default: boot)");
110 char *nouveau_perflvl;
111 module_param_named(perflvl, nouveau_perflvl, charp, 0400);
112
113 MODULE_PARM_DESC(perflvl_wr, "Allow perflvl changes (warning: dangerous!)");
114 int nouveau_perflvl_wr;
115 module_param_named(perflvl_wr, nouveau_perflvl_wr, int, 0400);
116
117 MODULE_PARM_DESC(msi, "Enable MSI (default: off)");
118 int nouveau_msi;
119 module_param_named(msi, nouveau_msi, int, 0400);
120
121 MODULE_PARM_DESC(ctxfw, "Use external HUB/GPC ucode (fermi)");
122 int nouveau_ctxfw;
123 module_param_named(ctxfw, nouveau_ctxfw, int, 0400);
124
125 MODULE_PARM_DESC(mxmdcb, "Santise DCB table according to MXM-SIS");
126 int nouveau_mxmdcb = 1;
127 module_param_named(mxmdcb, nouveau_mxmdcb, int, 0400);
128
129 int nouveau_fbpercrtc;
130 #if 0
131 module_param_named(fbpercrtc, nouveau_fbpercrtc, int, 0400);
132 #endif
133
134 static struct drm_driver driver;
135
136 int __devinit
137 nouveau_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
138 {
139         return drm_get_pci_dev(pdev, ent, &driver);
140 }
141
142 void
143 nouveau_pci_remove(struct pci_dev *pdev)
144 {
145         struct drm_device *dev = pci_get_drvdata(pdev);
146
147         drm_put_dev(dev);
148 }
149
150 int
151 nouveau_pci_suspend(struct pci_dev *pdev, pm_message_t pm_state)
152 {
153         struct drm_device *dev = pci_get_drvdata(pdev);
154         struct drm_nouveau_private *dev_priv = dev->dev_private;
155         struct nouveau_instmem_engine *pinstmem = &dev_priv->engine.instmem;
156         struct nouveau_fifo_priv *pfifo = nv_engine(dev, NVOBJ_ENGINE_FIFO);
157         struct nouveau_channel *chan;
158         struct drm_crtc *crtc;
159         int ret, i, e;
160
161         NV_INFO(dev, "Disabling display...\n");
162         nouveau_display_fini(dev);
163
164         NV_INFO(dev, "Disabling fbcon...\n");
165         nouveau_fbcon_set_suspend(dev, 1);
166
167         NV_INFO(dev, "Unpinning framebuffer(s)...\n");
168         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
169                 struct nouveau_framebuffer *nouveau_fb;
170
171                 nouveau_fb = nouveau_framebuffer(crtc->fb);
172                 if (!nouveau_fb || !nouveau_fb->nvbo)
173                         continue;
174
175                 nouveau_bo_unpin(nouveau_fb->nvbo);
176         }
177
178         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
179                 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
180
181                 nouveau_bo_unmap(nv_crtc->cursor.nvbo);
182                 nouveau_bo_unpin(nv_crtc->cursor.nvbo);
183         }
184
185         NV_INFO(dev, "Evicting buffers...\n");
186         ttm_bo_evict_mm(&dev_priv->ttm.bdev, TTM_PL_VRAM);
187
188         NV_INFO(dev, "Idling channels...\n");
189         for (i = 0; i < (pfifo ? pfifo->channels : 0); i++) {
190                 chan = dev_priv->channels.ptr[i];
191
192                 if (chan && chan->pushbuf_bo)
193                         nouveau_channel_idle(chan);
194         }
195
196         for (e = NVOBJ_ENGINE_NR - 1; e >= 0; e--) {
197                 if (!dev_priv->eng[e])
198                         continue;
199
200                 ret = dev_priv->eng[e]->fini(dev, e, true);
201                 if (ret) {
202                         NV_ERROR(dev, "... engine %d failed: %d\n", e, ret);
203                         goto out_abort;
204                 }
205         }
206
207         ret = pinstmem->suspend(dev);
208         if (ret) {
209                 NV_ERROR(dev, "... failed: %d\n", ret);
210                 goto out_abort;
211         }
212
213         NV_INFO(dev, "Suspending GPU objects...\n");
214         ret = nouveau_gpuobj_suspend(dev);
215         if (ret) {
216                 NV_ERROR(dev, "... failed: %d\n", ret);
217                 pinstmem->resume(dev);
218                 goto out_abort;
219         }
220
221         return 0;
222
223 out_abort:
224         NV_INFO(dev, "Re-enabling acceleration..\n");
225         for (e = e + 1; e < NVOBJ_ENGINE_NR; e++) {
226                 if (dev_priv->eng[e])
227                         dev_priv->eng[e]->init(dev, e);
228         }
229         return ret;
230 }
231
232 int
233 nouveau_pci_resume(struct pci_dev *pdev)
234 {
235         struct drm_device *dev = pci_get_drvdata(pdev);
236         struct nouveau_fifo_priv *pfifo = nv_engine(dev, NVOBJ_ENGINE_FIFO);
237         struct drm_nouveau_private *dev_priv = dev->dev_private;
238         struct nouveau_engine *engine = &dev_priv->engine;
239         struct drm_crtc *crtc;
240         int ret, i;
241
242         /* Make the CRTCs accessible */
243         engine->display.early_init(dev);
244
245         NV_INFO(dev, "POSTing device...\n");
246         ret = nouveau_run_vbios_init(dev);
247         if (ret)
248                 return ret;
249
250         NV_INFO(dev, "Restoring GPU objects...\n");
251         nouveau_gpuobj_resume(dev);
252
253         NV_INFO(dev, "Reinitialising engines...\n");
254         engine->instmem.resume(dev);
255         engine->timer.init(dev);
256         engine->fb.init(dev);
257         for (i = 0; i < NVOBJ_ENGINE_NR; i++) {
258                 if (dev_priv->eng[i])
259                         dev_priv->eng[i]->init(dev, i);
260         }
261
262         nouveau_irq_postinstall(dev);
263
264         /* Re-write SKIPS, they'll have been lost over the suspend */
265         if (nouveau_vram_pushbuf) {
266                 struct nouveau_channel *chan;
267                 int j;
268
269                 for (i = 0; i < (pfifo ? pfifo->channels : 0); i++) {
270                         chan = dev_priv->channels.ptr[i];
271                         if (!chan || !chan->pushbuf_bo)
272                                 continue;
273
274                         for (j = 0; j < NOUVEAU_DMA_SKIPS; j++)
275                                 nouveau_bo_wr32(chan->pushbuf_bo, i, 0);
276                 }
277         }
278
279         nouveau_pm_resume(dev);
280
281         NV_INFO(dev, "Restoring mode...\n");
282         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
283                 struct nouveau_framebuffer *nouveau_fb;
284
285                 nouveau_fb = nouveau_framebuffer(crtc->fb);
286                 if (!nouveau_fb || !nouveau_fb->nvbo)
287                         continue;
288
289                 nouveau_bo_pin(nouveau_fb->nvbo, TTM_PL_FLAG_VRAM);
290         }
291
292         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
293                 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
294
295                 ret = nouveau_bo_pin(nv_crtc->cursor.nvbo, TTM_PL_FLAG_VRAM);
296                 if (!ret)
297                         ret = nouveau_bo_map(nv_crtc->cursor.nvbo);
298                 if (ret)
299                         NV_ERROR(dev, "Could not pin/map cursor.\n");
300         }
301
302         nouveau_fbcon_set_suspend(dev, 0);
303         nouveau_fbcon_zfill_all(dev);
304
305         nouveau_display_init(dev);
306
307         /* Force CLUT to get re-loaded during modeset */
308         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
309                 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
310
311                 nv_crtc->lut.depth = 0;
312         }
313
314         drm_helper_resume_force_mode(dev);
315
316         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
317                 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
318                 u32 offset = nv_crtc->cursor.nvbo->bo.offset;
319
320                 nv_crtc->cursor.set_offset(nv_crtc, offset);
321                 nv_crtc->cursor.set_pos(nv_crtc, nv_crtc->cursor_saved_x,
322                                                  nv_crtc->cursor_saved_y);
323         }
324
325         return 0;
326 }
327
328 static struct drm_ioctl_desc nouveau_ioctls[] = {
329         DRM_IOCTL_DEF_DRV(NOUVEAU_GETPARAM, nouveau_abi16_ioctl_getparam, DRM_UNLOCKED|DRM_AUTH),
330         DRM_IOCTL_DEF_DRV(NOUVEAU_SETPARAM, nouveau_abi16_ioctl_setparam, DRM_UNLOCKED|DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
331         DRM_IOCTL_DEF_DRV(NOUVEAU_CHANNEL_ALLOC, nouveau_abi16_ioctl_channel_alloc, DRM_UNLOCKED|DRM_AUTH),
332         DRM_IOCTL_DEF_DRV(NOUVEAU_CHANNEL_FREE, nouveau_abi16_ioctl_channel_free, DRM_UNLOCKED|DRM_AUTH),
333         DRM_IOCTL_DEF_DRV(NOUVEAU_GROBJ_ALLOC, nouveau_abi16_ioctl_grobj_alloc, DRM_UNLOCKED|DRM_AUTH),
334         DRM_IOCTL_DEF_DRV(NOUVEAU_NOTIFIEROBJ_ALLOC, nouveau_abi16_ioctl_notifierobj_alloc, DRM_UNLOCKED|DRM_AUTH),
335         DRM_IOCTL_DEF_DRV(NOUVEAU_GPUOBJ_FREE, nouveau_abi16_ioctl_gpuobj_free, DRM_UNLOCKED|DRM_AUTH),
336         DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_NEW, nouveau_gem_ioctl_new, DRM_UNLOCKED|DRM_AUTH),
337         DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_PUSHBUF, nouveau_gem_ioctl_pushbuf, DRM_UNLOCKED|DRM_AUTH),
338         DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_CPU_PREP, nouveau_gem_ioctl_cpu_prep, DRM_UNLOCKED|DRM_AUTH),
339         DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_CPU_FINI, nouveau_gem_ioctl_cpu_fini, DRM_UNLOCKED|DRM_AUTH),
340         DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_INFO, nouveau_gem_ioctl_info, DRM_UNLOCKED|DRM_AUTH),
341 };
342
343 static const struct file_operations nouveau_driver_fops = {
344         .owner = THIS_MODULE,
345         .open = drm_open,
346         .release = drm_release,
347         .unlocked_ioctl = drm_ioctl,
348         .mmap = nouveau_ttm_mmap,
349         .poll = drm_poll,
350         .fasync = drm_fasync,
351         .read = drm_read,
352 #if defined(CONFIG_COMPAT)
353         .compat_ioctl = nouveau_compat_ioctl,
354 #endif
355         .llseek = noop_llseek,
356 };
357
358 int nouveau_drm_load(struct drm_device *, unsigned long);
359 int nouveau_drm_unload(struct drm_device *);
360
361 static struct drm_driver driver = {
362         .driver_features =
363                 DRIVER_USE_AGP | DRIVER_PCI_DMA | DRIVER_SG |
364                 DRIVER_HAVE_IRQ | DRIVER_IRQ_SHARED | DRIVER_GEM |
365                 DRIVER_MODESET | DRIVER_PRIME,
366         .load = nouveau_drm_load,
367         .firstopen = nouveau_firstopen,
368         .lastclose = nouveau_lastclose,
369         .unload = nouveau_drm_unload,
370         .open = nouveau_open,
371         .preclose = nouveau_preclose,
372         .postclose = nouveau_postclose,
373 #if defined(CONFIG_DRM_NOUVEAU_DEBUG)
374         .debugfs_init = nouveau_debugfs_init,
375         .debugfs_cleanup = nouveau_debugfs_takedown,
376 #endif
377         .irq_preinstall = nouveau_irq_preinstall,
378         .irq_postinstall = nouveau_irq_postinstall,
379         .irq_uninstall = nouveau_irq_uninstall,
380         .irq_handler = nouveau_irq_handler,
381         .get_vblank_counter = drm_vblank_count,
382         .enable_vblank = nouveau_vblank_enable,
383         .disable_vblank = nouveau_vblank_disable,
384         .ioctls = nouveau_ioctls,
385         .fops = &nouveau_driver_fops,
386
387         .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
388         .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
389         .gem_prime_export = nouveau_gem_prime_export,
390         .gem_prime_import = nouveau_gem_prime_import,
391
392         .gem_init_object = nouveau_gem_object_new,
393         .gem_free_object = nouveau_gem_object_del,
394         .gem_open_object = nouveau_gem_object_open,
395         .gem_close_object = nouveau_gem_object_close,
396
397         .dumb_create = nouveau_display_dumb_create,
398         .dumb_map_offset = nouveau_display_dumb_map_offset,
399         .dumb_destroy = nouveau_display_dumb_destroy,
400
401         .name = DRIVER_NAME,
402         .desc = DRIVER_DESC,
403 #ifdef GIT_REVISION
404         .date = GIT_REVISION,
405 #else
406         .date = DRIVER_DATE,
407 #endif
408         .major = DRIVER_MAJOR,
409         .minor = DRIVER_MINOR,
410         .patchlevel = DRIVER_PATCHLEVEL,
411 };
412
413 int __init nouveau_init(struct pci_driver *pdrv)
414 {
415         driver.num_ioctls = ARRAY_SIZE(nouveau_ioctls);
416
417         if (nouveau_modeset == -1) {
418 #ifdef CONFIG_VGA_CONSOLE
419                 if (vgacon_text_force())
420                         nouveau_modeset = 0;
421                 else
422 #endif
423                         nouveau_modeset = 1;
424         }
425
426         if (!nouveau_modeset)
427                 return 0;
428
429         nouveau_register_dsm_handler();
430         return drm_pci_init(&driver, pdrv);
431 }
432
433 void __exit nouveau_exit(struct pci_driver *pdrv)
434 {
435         if (!nouveau_modeset)
436                 return;
437
438         drm_pci_exit(&driver, pdrv);
439         nouveau_unregister_dsm_handler();
440 }