Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame] | 1 | #include <linux/vgaarb.h> |
| 2 | #include <linux/vga_switcheroo.h> |
| 3 | |
Linus Torvalds | 612a9aa | 2012-10-03 23:29:23 -0700 | [diff] [blame] | 4 | #include <drm/drmP.h> |
| 5 | #include <drm/drm_crtc_helper.h> |
Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame] | 6 | |
| 7 | #include "nouveau_drm.h" |
| 8 | #include "nouveau_acpi.h" |
| 9 | #include "nouveau_fbcon.h" |
Marcin Slusarz | 5b8a43a | 2012-08-19 23:00:00 +0200 | [diff] [blame] | 10 | #include "nouveau_vga.h" |
Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame] | 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->chipset >= 0x40) |
| 18 | nv_wr32(device, 0x088054, state); |
| 19 | else |
| 20 | nv_wr32(device, 0x001854, state); |
| 21 | |
| 22 | if (state) |
| 23 | return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM | |
| 24 | VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM; |
| 25 | else |
| 26 | return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM; |
| 27 | } |
| 28 | |
| 29 | static void |
| 30 | nouveau_switcheroo_set_state(struct pci_dev *pdev, |
| 31 | enum vga_switcheroo_state state) |
| 32 | { |
| 33 | struct drm_device *dev = pci_get_drvdata(pdev); |
Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame] | 34 | |
Dave Airlie | 5addcf0 | 2012-09-10 14:20:51 +1000 | [diff] [blame] | 35 | if ((nouveau_is_optimus() || nouveau_is_v1_dsm()) && state == VGA_SWITCHEROO_OFF) |
| 36 | return; |
| 37 | |
Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame] | 38 | if (state == VGA_SWITCHEROO_ON) { |
| 39 | printk(KERN_ERR "VGA switcheroo: switched nouveau on\n"); |
| 40 | dev->switch_power_state = DRM_SWITCH_POWER_CHANGING; |
Dave Airlie | 2d8b9cc | 2012-11-02 11:04:28 +1000 | [diff] [blame] | 41 | nouveau_pmops_resume(&pdev->dev); |
Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame] | 42 | drm_kms_helper_poll_enable(dev); |
| 43 | dev->switch_power_state = DRM_SWITCH_POWER_ON; |
| 44 | } else { |
| 45 | printk(KERN_ERR "VGA switcheroo: switched nouveau off\n"); |
| 46 | dev->switch_power_state = DRM_SWITCH_POWER_CHANGING; |
| 47 | drm_kms_helper_poll_disable(dev); |
| 48 | nouveau_switcheroo_optimus_dsm(); |
Dave Airlie | 2d8b9cc | 2012-11-02 11:04:28 +1000 | [diff] [blame] | 49 | nouveau_pmops_suspend(&pdev->dev); |
Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame] | 50 | dev->switch_power_state = DRM_SWITCH_POWER_OFF; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | static void |
| 55 | nouveau_switcheroo_reprobe(struct pci_dev *pdev) |
| 56 | { |
| 57 | struct drm_device *dev = pci_get_drvdata(pdev); |
| 58 | nouveau_fbcon_output_poll_changed(dev); |
| 59 | } |
| 60 | |
| 61 | static bool |
| 62 | nouveau_switcheroo_can_switch(struct pci_dev *pdev) |
| 63 | { |
| 64 | struct drm_device *dev = pci_get_drvdata(pdev); |
| 65 | bool can_switch; |
| 66 | |
| 67 | spin_lock(&dev->count_lock); |
| 68 | can_switch = (dev->open_count == 0); |
| 69 | spin_unlock(&dev->count_lock); |
| 70 | return can_switch; |
| 71 | } |
| 72 | |
| 73 | static const struct vga_switcheroo_client_ops |
| 74 | nouveau_switcheroo_ops = { |
| 75 | .set_gpu_state = nouveau_switcheroo_set_state, |
| 76 | .reprobe = nouveau_switcheroo_reprobe, |
| 77 | .can_switch = nouveau_switcheroo_can_switch, |
| 78 | }; |
| 79 | |
| 80 | void |
| 81 | nouveau_vga_init(struct nouveau_drm *drm) |
| 82 | { |
| 83 | struct drm_device *dev = drm->dev; |
Dave Airlie | 5addcf0 | 2012-09-10 14:20:51 +1000 | [diff] [blame] | 84 | bool runtime = false; |
Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame] | 85 | vga_client_register(dev->pdev, dev, NULL, nouveau_vga_set_decode); |
Dave Airlie | 5addcf0 | 2012-09-10 14:20:51 +1000 | [diff] [blame] | 86 | |
| 87 | if (nouveau_runtime_pm == 1) |
| 88 | runtime = true; |
| 89 | if ((nouveau_runtime_pm == -1) && (nouveau_is_optimus() || nouveau_is_v1_dsm())) |
| 90 | runtime = true; |
| 91 | vga_switcheroo_register_client(dev->pdev, &nouveau_switcheroo_ops, runtime); |
| 92 | |
| 93 | if (runtime && nouveau_is_v1_dsm() && !nouveau_is_optimus()) |
| 94 | vga_switcheroo_init_domain_pm_ops(drm->dev->dev, &drm->vga_pm_domain); |
Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | void |
| 98 | nouveau_vga_fini(struct nouveau_drm *drm) |
| 99 | { |
| 100 | struct drm_device *dev = drm->dev; |
| 101 | vga_switcheroo_unregister_client(dev->pdev); |
| 102 | vga_client_register(dev->pdev, NULL, NULL, NULL); |
| 103 | } |
| 104 | |
| 105 | |
| 106 | void |
| 107 | nouveau_vga_lastclose(struct drm_device *dev) |
| 108 | { |
| 109 | vga_switcheroo_process_delayed_switch(); |
| 110 | } |