0001
0002 #include <linux/vgaarb.h>
0003 #include <linux/vga_switcheroo.h>
0004
0005 #include <drm/drm_crtc_helper.h>
0006 #include <drm/drm_fb_helper.h>
0007
0008 #include "nouveau_drv.h"
0009 #include "nouveau_acpi.h"
0010 #include "nouveau_fbcon.h"
0011 #include "nouveau_vga.h"
0012
0013 static unsigned int
0014 nouveau_vga_set_decode(struct pci_dev *pdev, bool state)
0015 {
0016 struct nouveau_drm *drm = nouveau_drm(pci_get_drvdata(pdev));
0017 struct nvif_object *device = &drm->client.device.object;
0018
0019 if (drm->client.device.info.family == NV_DEVICE_INFO_V0_CURIE &&
0020 drm->client.device.info.chipset >= 0x4c)
0021 nvif_wr32(device, 0x088060, state);
0022 else
0023 if (drm->client.device.info.chipset >= 0x40)
0024 nvif_wr32(device, 0x088054, state);
0025 else
0026 nvif_wr32(device, 0x001854, state);
0027
0028 if (state)
0029 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
0030 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
0031 else
0032 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
0033 }
0034
0035 static void
0036 nouveau_switcheroo_set_state(struct pci_dev *pdev,
0037 enum vga_switcheroo_state state)
0038 {
0039 struct drm_device *dev = pci_get_drvdata(pdev);
0040
0041 if ((nouveau_is_optimus() || nouveau_is_v1_dsm()) && state == VGA_SWITCHEROO_OFF)
0042 return;
0043
0044 if (state == VGA_SWITCHEROO_ON) {
0045 pr_err("VGA switcheroo: switched nouveau on\n");
0046 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
0047 nouveau_pmops_resume(&pdev->dev);
0048 dev->switch_power_state = DRM_SWITCH_POWER_ON;
0049 } else {
0050 pr_err("VGA switcheroo: switched nouveau off\n");
0051 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
0052 nouveau_switcheroo_optimus_dsm();
0053 nouveau_pmops_suspend(&pdev->dev);
0054 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
0055 }
0056 }
0057
0058 static void
0059 nouveau_switcheroo_reprobe(struct pci_dev *pdev)
0060 {
0061 struct drm_device *dev = pci_get_drvdata(pdev);
0062 drm_fb_helper_output_poll_changed(dev);
0063 }
0064
0065 static bool
0066 nouveau_switcheroo_can_switch(struct pci_dev *pdev)
0067 {
0068 struct drm_device *dev = pci_get_drvdata(pdev);
0069
0070
0071
0072
0073
0074
0075 return atomic_read(&dev->open_count) == 0;
0076 }
0077
0078 static const struct vga_switcheroo_client_ops
0079 nouveau_switcheroo_ops = {
0080 .set_gpu_state = nouveau_switcheroo_set_state,
0081 .reprobe = nouveau_switcheroo_reprobe,
0082 .can_switch = nouveau_switcheroo_can_switch,
0083 };
0084
0085 void
0086 nouveau_vga_init(struct nouveau_drm *drm)
0087 {
0088 struct drm_device *dev = drm->dev;
0089 bool runtime = nouveau_pmops_runtime();
0090 struct pci_dev *pdev;
0091
0092
0093 if (!dev_is_pci(dev->dev))
0094 return;
0095 pdev = to_pci_dev(dev->dev);
0096
0097 vga_client_register(pdev, nouveau_vga_set_decode);
0098
0099
0100 if (pci_is_thunderbolt_attached(pdev))
0101 return;
0102
0103 vga_switcheroo_register_client(pdev, &nouveau_switcheroo_ops, runtime);
0104
0105 if (runtime && nouveau_is_v1_dsm() && !nouveau_is_optimus())
0106 vga_switcheroo_init_domain_pm_ops(drm->dev->dev, &drm->vga_pm_domain);
0107 }
0108
0109 void
0110 nouveau_vga_fini(struct nouveau_drm *drm)
0111 {
0112 struct drm_device *dev = drm->dev;
0113 bool runtime = nouveau_pmops_runtime();
0114 struct pci_dev *pdev;
0115
0116
0117 if (!dev_is_pci(dev->dev))
0118 return;
0119 pdev = to_pci_dev(dev->dev);
0120
0121 vga_client_unregister(pdev);
0122
0123 if (pci_is_thunderbolt_attached(pdev))
0124 return;
0125
0126 vga_switcheroo_unregister_client(pdev);
0127 if (runtime && nouveau_is_v1_dsm() && !nouveau_is_optimus())
0128 vga_switcheroo_fini_domain_pm_ops(drm->dev->dev);
0129 }
0130
0131
0132 void
0133 nouveau_vga_lastclose(struct drm_device *dev)
0134 {
0135 vga_switcheroo_process_delayed_switch();
0136 }