nouveau_vga.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. dev->switch_power_state = DRM_SWITCH_POWER_ON;
  42. } else {
  43. pr_err("VGA switcheroo: switched nouveau off\n");
  44. dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
  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. drm_fb_helper_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 = nouveau_pmops_runtime();
  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. /* don't register Thunderbolt eGPU with vga_switcheroo */
  83. if (pci_is_thunderbolt_attached(dev->pdev))
  84. return;
  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 = nouveau_pmops_runtime();
  94. /* only relevant for PCI devices */
  95. if (!dev->pdev)
  96. return;
  97. vga_client_register(dev->pdev, NULL, NULL, NULL);
  98. if (pci_is_thunderbolt_attached(dev->pdev))
  99. return;
  100. vga_switcheroo_unregister_client(dev->pdev);
  101. if (runtime && nouveau_is_v1_dsm() && !nouveau_is_optimus())
  102. vga_switcheroo_fini_domain_pm_ops(drm->dev->dev);
  103. }
  104. void
  105. nouveau_vga_lastclose(struct drm_device *dev)
  106. {
  107. vga_switcheroo_process_delayed_switch();
  108. }