nouveau_vga.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/vgaarb.h>
  3. #include <linux/vga_switcheroo.h>
  4. #include <drm/drmP.h>
  5. #include <drm/drm_crtc_helper.h>
  6. #include <drm/drm_fb_helper.h>
  7. #include "nouveau_drv.h"
  8. #include "nouveau_acpi.h"
  9. #include "nouveau_fbcon.h"
  10. #include "nouveau_vga.h"
  11. static unsigned int
  12. nouveau_vga_set_decode(void *priv, bool state)
  13. {
  14. struct nouveau_drm *drm = nouveau_drm(priv);
  15. struct nvif_object *device = &drm->client.device.object;
  16. if (drm->client.device.info.family == NV_DEVICE_INFO_V0_CURIE &&
  17. drm->client.device.info.chipset >= 0x4c)
  18. nvif_wr32(device, 0x088060, state);
  19. else
  20. if (drm->client.device.info.chipset >= 0x40)
  21. nvif_wr32(device, 0x088054, state);
  22. else
  23. nvif_wr32(device, 0x001854, state);
  24. if (state)
  25. return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
  26. VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
  27. else
  28. return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
  29. }
  30. static void
  31. nouveau_switcheroo_set_state(struct pci_dev *pdev,
  32. enum vga_switcheroo_state state)
  33. {
  34. struct drm_device *dev = pci_get_drvdata(pdev);
  35. if ((nouveau_is_optimus() || nouveau_is_v1_dsm()) && state == VGA_SWITCHEROO_OFF)
  36. return;
  37. if (state == VGA_SWITCHEROO_ON) {
  38. pr_err("VGA switcheroo: switched nouveau on\n");
  39. dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
  40. nouveau_pmops_resume(&pdev->dev);
  41. drm_kms_helper_poll_enable(dev);
  42. dev->switch_power_state = DRM_SWITCH_POWER_ON;
  43. } else {
  44. pr_err("VGA switcheroo: switched nouveau off\n");
  45. dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
  46. drm_kms_helper_poll_disable(dev);
  47. nouveau_switcheroo_optimus_dsm();
  48. nouveau_pmops_suspend(&pdev->dev);
  49. dev->switch_power_state = DRM_SWITCH_POWER_OFF;
  50. }
  51. }
  52. static void
  53. nouveau_switcheroo_reprobe(struct pci_dev *pdev)
  54. {
  55. struct drm_device *dev = pci_get_drvdata(pdev);
  56. drm_fb_helper_output_poll_changed(dev);
  57. }
  58. static bool
  59. nouveau_switcheroo_can_switch(struct pci_dev *pdev)
  60. {
  61. struct drm_device *dev = pci_get_drvdata(pdev);
  62. /*
  63. * FIXME: open_count is protected by drm_global_mutex but that would lead to
  64. * locking inversion with the driver load path. And the access here is
  65. * completely racy anyway. So don't bother with locking for now.
  66. */
  67. return dev->open_count == 0;
  68. }
  69. static const struct vga_switcheroo_client_ops
  70. nouveau_switcheroo_ops = {
  71. .set_gpu_state = nouveau_switcheroo_set_state,
  72. .reprobe = nouveau_switcheroo_reprobe,
  73. .can_switch = nouveau_switcheroo_can_switch,
  74. };
  75. void
  76. nouveau_vga_init(struct nouveau_drm *drm)
  77. {
  78. struct drm_device *dev = drm->dev;
  79. bool runtime = nouveau_pmops_runtime();
  80. /* only relevant for PCI devices */
  81. if (!dev->pdev)
  82. return;
  83. vga_client_register(dev->pdev, dev, NULL, nouveau_vga_set_decode);
  84. /* don't register Thunderbolt eGPU with vga_switcheroo */
  85. if (pci_is_thunderbolt_attached(dev->pdev))
  86. return;
  87. vga_switcheroo_register_client(dev->pdev, &nouveau_switcheroo_ops, runtime);
  88. if (runtime && nouveau_is_v1_dsm() && !nouveau_is_optimus())
  89. vga_switcheroo_init_domain_pm_ops(drm->dev->dev, &drm->vga_pm_domain);
  90. }
  91. void
  92. nouveau_vga_fini(struct nouveau_drm *drm)
  93. {
  94. struct drm_device *dev = drm->dev;
  95. bool runtime = nouveau_pmops_runtime();
  96. /* only relevant for PCI devices */
  97. if (!dev->pdev)
  98. return;
  99. vga_client_register(dev->pdev, NULL, NULL, NULL);
  100. if (pci_is_thunderbolt_attached(dev->pdev))
  101. return;
  102. vga_switcheroo_unregister_client(dev->pdev);
  103. if (runtime && nouveau_is_v1_dsm() && !nouveau_is_optimus())
  104. vga_switcheroo_fini_domain_pm_ops(drm->dev->dev);
  105. }
  106. void
  107. nouveau_vga_lastclose(struct drm_device *dev)
  108. {
  109. vga_switcheroo_process_delayed_switch();
  110. }