nouveau_vga.c 3.4 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 nvif_device *device = &nouveau_drm(priv)->device;
  13. if (device->info.family == NV_DEVICE_INFO_V0_CURIE &&
  14. device->info.chipset >= 0x4c)
  15. nvif_wr32(device, 0x088060, state);
  16. else
  17. if (device->info.chipset >= 0x40)
  18. nvif_wr32(device, 0x088054, state);
  19. else
  20. nvif_wr32(device, 0x001854, state);
  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. static void
  28. nouveau_switcheroo_set_state(struct pci_dev *pdev,
  29. enum vga_switcheroo_state state)
  30. {
  31. struct drm_device *dev = pci_get_drvdata(pdev);
  32. if ((nouveau_is_optimus() || nouveau_is_v1_dsm()) && state == VGA_SWITCHEROO_OFF)
  33. return;
  34. if (state == VGA_SWITCHEROO_ON) {
  35. printk(KERN_ERR "VGA switcheroo: switched nouveau on\n");
  36. dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
  37. nouveau_pmops_resume(&pdev->dev);
  38. drm_kms_helper_poll_enable(dev);
  39. dev->switch_power_state = DRM_SWITCH_POWER_ON;
  40. } else {
  41. printk(KERN_ERR "VGA switcheroo: switched nouveau off\n");
  42. dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
  43. drm_kms_helper_poll_disable(dev);
  44. nouveau_switcheroo_optimus_dsm();
  45. nouveau_pmops_suspend(&pdev->dev);
  46. dev->switch_power_state = DRM_SWITCH_POWER_OFF;
  47. }
  48. }
  49. static void
  50. nouveau_switcheroo_reprobe(struct pci_dev *pdev)
  51. {
  52. struct drm_device *dev = pci_get_drvdata(pdev);
  53. nouveau_fbcon_output_poll_changed(dev);
  54. }
  55. static bool
  56. nouveau_switcheroo_can_switch(struct pci_dev *pdev)
  57. {
  58. struct drm_device *dev = pci_get_drvdata(pdev);
  59. /*
  60. * FIXME: open_count is protected by drm_global_mutex but that would lead to
  61. * locking inversion with the driver load path. And the access here is
  62. * completely racy anyway. So don't bother with locking for now.
  63. */
  64. return dev->open_count == 0;
  65. }
  66. static const struct vga_switcheroo_client_ops
  67. nouveau_switcheroo_ops = {
  68. .set_gpu_state = nouveau_switcheroo_set_state,
  69. .reprobe = nouveau_switcheroo_reprobe,
  70. .can_switch = nouveau_switcheroo_can_switch,
  71. };
  72. void
  73. nouveau_vga_init(struct nouveau_drm *drm)
  74. {
  75. struct drm_device *dev = drm->dev;
  76. bool runtime = false;
  77. /* only relevant for PCI devices */
  78. if (!dev->pdev)
  79. return;
  80. vga_client_register(dev->pdev, dev, NULL, nouveau_vga_set_decode);
  81. if (nouveau_runtime_pm == 1)
  82. runtime = true;
  83. if ((nouveau_runtime_pm == -1) && (nouveau_is_optimus() || nouveau_is_v1_dsm()))
  84. runtime = true;
  85. vga_switcheroo_register_client(dev->pdev, &nouveau_switcheroo_ops, runtime);
  86. if (runtime && nouveau_is_v1_dsm() && !nouveau_is_optimus())
  87. vga_switcheroo_init_domain_pm_ops(drm->dev->dev, &drm->vga_pm_domain);
  88. }
  89. void
  90. nouveau_vga_fini(struct nouveau_drm *drm)
  91. {
  92. struct drm_device *dev = drm->dev;
  93. bool runtime = false;
  94. if (nouveau_runtime_pm == 1)
  95. runtime = true;
  96. if ((nouveau_runtime_pm == -1) && (nouveau_is_optimus() || nouveau_is_v1_dsm()))
  97. runtime = true;
  98. vga_switcheroo_unregister_client(dev->pdev);
  99. if (runtime && nouveau_is_v1_dsm() && !nouveau_is_optimus())
  100. vga_switcheroo_fini_domain_pm_ops(drm->dev->dev);
  101. vga_client_register(dev->pdev, NULL, NULL, NULL);
  102. }
  103. void
  104. nouveau_vga_lastclose(struct drm_device *dev)
  105. {
  106. vga_switcheroo_process_delayed_switch();
  107. }