nouveau_vga.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // SPDX-License-Identifier: MIT
  2. #include <linux/vgaarb.h>
  3. #include <linux/vga_switcheroo.h>
  4. #include <drm/drm_crtc_helper.h>
  5. #include <drm/drm_fb_helper.h>
  6. #include "nouveau_drv.h"
  7. #include "nouveau_acpi.h"
  8. #include "nouveau_fbcon.h"
  9. #include "nouveau_vga.h"
  10. static unsigned int
  11. nouveau_vga_set_decode(void *priv, bool state)
  12. {
  13. struct nouveau_drm *drm = nouveau_drm(priv);
  14. struct nvif_object *device = &drm->client.device.object;
  15. if (drm->client.device.info.family == NV_DEVICE_INFO_V0_CURIE &&
  16. drm->client.device.info.chipset >= 0x4c)
  17. nvif_wr32(device, 0x088060, state);
  18. else
  19. if (drm->client.device.info.chipset >= 0x40)
  20. nvif_wr32(device, 0x088054, state);
  21. else
  22. nvif_wr32(device, 0x001854, state);
  23. if (state)
  24. return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
  25. VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
  26. else
  27. return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
  28. }
  29. static void
  30. nouveau_switcheroo_set_state(struct pci_dev *pdev,
  31. enum vga_switcheroo_state state)
  32. {
  33. struct drm_device *dev = pci_get_drvdata(pdev);
  34. if ((nouveau_is_optimus() || nouveau_is_v1_dsm()) && state == VGA_SWITCHEROO_OFF)
  35. return;
  36. if (state == VGA_SWITCHEROO_ON) {
  37. pr_err("VGA switcheroo: switched nouveau on\n");
  38. dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
  39. nouveau_pmops_resume(&pdev->dev);
  40. dev->switch_power_state = DRM_SWITCH_POWER_ON;
  41. } else {
  42. pr_err("VGA switcheroo: switched nouveau off\n");
  43. dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
  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. drm_fb_helper_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 atomic_read(&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 = nouveau_pmops_runtime();
  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. /* don't register Thunderbolt eGPU with vga_switcheroo */
  82. if (pci_is_thunderbolt_attached(dev->pdev))
  83. return;
  84. vga_switcheroo_register_client(dev->pdev, &nouveau_switcheroo_ops, runtime);
  85. if (runtime && nouveau_is_v1_dsm() && !nouveau_is_optimus())
  86. vga_switcheroo_init_domain_pm_ops(drm->dev->dev, &drm->vga_pm_domain);
  87. }
  88. void
  89. nouveau_vga_fini(struct nouveau_drm *drm)
  90. {
  91. struct drm_device *dev = drm->dev;
  92. bool runtime = nouveau_pmops_runtime();
  93. /* only relevant for PCI devices */
  94. if (!dev->pdev)
  95. return;
  96. vga_client_register(dev->pdev, NULL, NULL, NULL);
  97. if (pci_is_thunderbolt_attached(dev->pdev))
  98. return;
  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. }
  103. void
  104. nouveau_vga_lastclose(struct drm_device *dev)
  105. {
  106. vga_switcheroo_process_delayed_switch();
  107. }