Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame] | 1 | #include <linux/vgaarb.h> |
| 2 | #include <linux/vga_switcheroo.h> |
| 3 | |
| 4 | #include "drmP.h" |
| 5 | #include "drm_crtc_helper.h" |
| 6 | |
| 7 | #include "nouveau_drm.h" |
| 8 | #include "nouveau_acpi.h" |
| 9 | #include "nouveau_fbcon.h" |
| 10 | |
| 11 | static unsigned int |
| 12 | nouveau_vga_set_decode(void *priv, bool state) |
| 13 | { |
| 14 | struct nouveau_device *device = nouveau_dev(priv); |
| 15 | |
| 16 | if (device->chipset >= 0x40) |
| 17 | nv_wr32(device, 0x088054, state); |
| 18 | else |
| 19 | nv_wr32(device, 0x001854, state); |
| 20 | |
| 21 | if (state) |
| 22 | return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM | |
| 23 | VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM; |
| 24 | else |
| 25 | return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM; |
| 26 | } |
| 27 | |
| 28 | static void |
| 29 | nouveau_switcheroo_set_state(struct pci_dev *pdev, |
| 30 | enum vga_switcheroo_state state) |
| 31 | { |
| 32 | struct drm_device *dev = pci_get_drvdata(pdev); |
| 33 | pm_message_t pmm = { .event = PM_EVENT_SUSPEND }; |
| 34 | |
| 35 | if (state == VGA_SWITCHEROO_ON) { |
| 36 | printk(KERN_ERR "VGA switcheroo: switched nouveau on\n"); |
| 37 | dev->switch_power_state = DRM_SWITCH_POWER_CHANGING; |
| 38 | nouveau_drm_resume(pdev); |
| 39 | drm_kms_helper_poll_enable(dev); |
| 40 | dev->switch_power_state = DRM_SWITCH_POWER_ON; |
| 41 | } else { |
| 42 | printk(KERN_ERR "VGA switcheroo: switched nouveau off\n"); |
| 43 | dev->switch_power_state = DRM_SWITCH_POWER_CHANGING; |
| 44 | drm_kms_helper_poll_disable(dev); |
| 45 | nouveau_switcheroo_optimus_dsm(); |
| 46 | nouveau_drm_suspend(pdev, pmm); |
| 47 | dev->switch_power_state = DRM_SWITCH_POWER_OFF; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | static void |
| 52 | nouveau_switcheroo_reprobe(struct pci_dev *pdev) |
| 53 | { |
| 54 | struct drm_device *dev = pci_get_drvdata(pdev); |
| 55 | nouveau_fbcon_output_poll_changed(dev); |
| 56 | } |
| 57 | |
| 58 | static bool |
| 59 | nouveau_switcheroo_can_switch(struct pci_dev *pdev) |
| 60 | { |
| 61 | struct drm_device *dev = pci_get_drvdata(pdev); |
| 62 | bool can_switch; |
| 63 | |
| 64 | spin_lock(&dev->count_lock); |
| 65 | can_switch = (dev->open_count == 0); |
| 66 | spin_unlock(&dev->count_lock); |
| 67 | return can_switch; |
| 68 | } |
| 69 | |
| 70 | static const struct vga_switcheroo_client_ops |
| 71 | nouveau_switcheroo_ops = { |
| 72 | .set_gpu_state = nouveau_switcheroo_set_state, |
| 73 | .reprobe = nouveau_switcheroo_reprobe, |
| 74 | .can_switch = nouveau_switcheroo_can_switch, |
| 75 | }; |
| 76 | |
| 77 | void |
| 78 | nouveau_vga_init(struct nouveau_drm *drm) |
| 79 | { |
| 80 | struct drm_device *dev = drm->dev; |
| 81 | vga_client_register(dev->pdev, dev, NULL, nouveau_vga_set_decode); |
| 82 | vga_switcheroo_register_client(dev->pdev, &nouveau_switcheroo_ops); |
| 83 | } |
| 84 | |
| 85 | void |
| 86 | nouveau_vga_fini(struct nouveau_drm *drm) |
| 87 | { |
| 88 | struct drm_device *dev = drm->dev; |
| 89 | vga_switcheroo_unregister_client(dev->pdev); |
| 90 | vga_client_register(dev->pdev, NULL, NULL, NULL); |
| 91 | } |
| 92 | |
| 93 | |
| 94 | void |
| 95 | nouveau_vga_lastclose(struct drm_device *dev) |
| 96 | { |
| 97 | vga_switcheroo_process_delayed_switch(); |
| 98 | } |