Merge tag 'pci-v3.16-fixes-1' of git://git.kernel.org/pub/scm/linux/kernel/git/helgaa...
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / drm / nouveau / nouveau_vga.c
1 #include <linux/vgaarb.h>
2 #include <linux/vga_switcheroo.h>
3
4 #include <drm/drmP.h>
5 #include <drm/drm_crtc_helper.h>
6
7 #include "nouveau_drm.h"
8 #include "nouveau_acpi.h"
9 #include "nouveau_fbcon.h"
10 #include "nouveau_vga.h"
11
12 static unsigned int
13 nouveau_vga_set_decode(void *priv, bool state)
14 {
15         struct nouveau_device *device = nouveau_dev(priv);
16
17         if (device->card_type == NV_40 && device->chipset >= 0x4c)
18                 nv_wr32(device, 0x088060, state);
19         else if (device->chipset >= 0x40)
20                 nv_wr32(device, 0x088054, state);
21         else
22                 nv_wr32(device, 0x001854, state);
23
24         if (state)
25                 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
26                        VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
27         else
28                 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
29 }
30
31 static void
32 nouveau_switcheroo_set_state(struct pci_dev *pdev,
33                              enum vga_switcheroo_state state)
34 {
35         struct drm_device *dev = pci_get_drvdata(pdev);
36
37         if ((nouveau_is_optimus() || nouveau_is_v1_dsm()) && state == VGA_SWITCHEROO_OFF)
38                 return;
39
40         if (state == VGA_SWITCHEROO_ON) {
41                 printk(KERN_ERR "VGA switcheroo: switched nouveau on\n");
42                 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
43                 nouveau_pmops_resume(&pdev->dev);
44                 drm_kms_helper_poll_enable(dev);
45                 dev->switch_power_state = DRM_SWITCH_POWER_ON;
46         } else {
47                 printk(KERN_ERR "VGA switcheroo: switched nouveau off\n");
48                 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
49                 drm_kms_helper_poll_disable(dev);
50                 nouveau_switcheroo_optimus_dsm();
51                 nouveau_pmops_suspend(&pdev->dev);
52                 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
53         }
54 }
55
56 static void
57 nouveau_switcheroo_reprobe(struct pci_dev *pdev)
58 {
59         struct drm_device *dev = pci_get_drvdata(pdev);
60         nouveau_fbcon_output_poll_changed(dev);
61 }
62
63 static bool
64 nouveau_switcheroo_can_switch(struct pci_dev *pdev)
65 {
66         struct drm_device *dev = pci_get_drvdata(pdev);
67
68         /*
69          * FIXME: open_count is protected by drm_global_mutex but that would lead to
70          * locking inversion with the driver load path. And the access here is
71          * completely racy anyway. So don't bother with locking for now.
72          */
73         return dev->open_count == 0;
74 }
75
76 static const struct vga_switcheroo_client_ops
77 nouveau_switcheroo_ops = {
78         .set_gpu_state = nouveau_switcheroo_set_state,
79         .reprobe = nouveau_switcheroo_reprobe,
80         .can_switch = nouveau_switcheroo_can_switch,
81 };
82
83 void
84 nouveau_vga_init(struct nouveau_drm *drm)
85 {
86         struct drm_device *dev = drm->dev;
87         bool runtime = false;
88
89         /* only relevant for PCI devices */
90         if (!dev->pdev)
91                 return;
92
93         vga_client_register(dev->pdev, dev, NULL, nouveau_vga_set_decode);
94
95         if (nouveau_runtime_pm == 1)
96                 runtime = true;
97         if ((nouveau_runtime_pm == -1) && (nouveau_is_optimus() || nouveau_is_v1_dsm()))
98                 runtime = true;
99         vga_switcheroo_register_client(dev->pdev, &nouveau_switcheroo_ops, runtime);
100
101         if (runtime && nouveau_is_v1_dsm() && !nouveau_is_optimus())
102                 vga_switcheroo_init_domain_pm_ops(drm->dev->dev, &drm->vga_pm_domain);
103 }
104
105 void
106 nouveau_vga_fini(struct nouveau_drm *drm)
107 {
108         struct drm_device *dev = drm->dev;
109         vga_switcheroo_unregister_client(dev->pdev);
110         vga_client_register(dev->pdev, NULL, NULL, NULL);
111 }
112
113
114 void
115 nouveau_vga_lastclose(struct drm_device *dev)
116 {
117         vga_switcheroo_process_delayed_switch();
118 }