nouveau_vga.c 2.9 KB

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