nouveau_vga.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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_drm *drm = nouveau_drm(priv);
  13. struct nvif_object *device = &drm->device.object;
  14. if (drm->device.info.family == NV_DEVICE_INFO_V0_CURIE &&
  15. drm->device.info.chipset >= 0x4c)
  16. nvif_wr32(device, 0x088060, state);
  17. else
  18. if (drm->device.info.chipset >= 0x40)
  19. nvif_wr32(device, 0x088054, state);
  20. else
  21. nvif_wr32(device, 0x001854, state);
  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. 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. if ((nouveau_is_optimus() || nouveau_is_v1_dsm()) && state == VGA_SWITCHEROO_OFF)
  34. return;
  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_pmops_resume(&pdev->dev);
  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_pmops_suspend(&pdev->dev);
  47. dev->switch_power_state = DRM_SWITCH_POWER_OFF;
  48. }
  49. }
  50. static void
  51. nouveau_switcheroo_reprobe(struct pci_dev *pdev)
  52. {
  53. struct drm_device *dev = pci_get_drvdata(pdev);
  54. nouveau_fbcon_output_poll_changed(dev);
  55. }
  56. static bool
  57. nouveau_switcheroo_can_switch(struct pci_dev *pdev)
  58. {
  59. struct drm_device *dev = pci_get_drvdata(pdev);
  60. /*
  61. * FIXME: open_count is protected by drm_global_mutex but that would lead to
  62. * locking inversion with the driver load path. And the access here is
  63. * completely racy anyway. So don't bother with locking for now.
  64. */
  65. return dev->open_count == 0;
  66. }
  67. static const struct vga_switcheroo_client_ops
  68. nouveau_switcheroo_ops = {
  69. .set_gpu_state = nouveau_switcheroo_set_state,
  70. .reprobe = nouveau_switcheroo_reprobe,
  71. .can_switch = nouveau_switcheroo_can_switch,
  72. };
  73. void
  74. nouveau_vga_init(struct nouveau_drm *drm)
  75. {
  76. struct drm_device *dev = drm->dev;
  77. bool runtime = false;
  78. /* only relevant for PCI devices */
  79. if (!dev->pdev)
  80. return;
  81. vga_client_register(dev->pdev, dev, NULL, nouveau_vga_set_decode);
  82. if (nouveau_runtime_pm == 1)
  83. runtime = true;
  84. if ((nouveau_runtime_pm == -1) && (nouveau_is_optimus() || nouveau_is_v1_dsm()))
  85. runtime = true;
  86. vga_switcheroo_register_client(dev->pdev, &nouveau_switcheroo_ops, runtime);
  87. if (runtime && nouveau_is_v1_dsm() && !nouveau_is_optimus())
  88. vga_switcheroo_init_domain_pm_ops(drm->dev->dev, &drm->vga_pm_domain);
  89. }
  90. void
  91. nouveau_vga_fini(struct nouveau_drm *drm)
  92. {
  93. struct drm_device *dev = drm->dev;
  94. bool runtime = false;
  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_unregister_client(dev->pdev);
  100. if (runtime && nouveau_is_v1_dsm() && !nouveau_is_optimus())
  101. vga_switcheroo_fini_domain_pm_ops(drm->dev->dev);
  102. vga_client_register(dev->pdev, NULL, NULL, NULL);
  103. }
  104. void
  105. nouveau_vga_lastclose(struct drm_device *dev)
  106. {
  107. vga_switcheroo_process_delayed_switch();
  108. }