nouveau_vga.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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(struct pci_dev *pdev, bool state)
  12. {
  13. struct nouveau_drm *drm = nouveau_drm(pci_get_drvdata(pdev));
  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. struct pci_dev *pdev;
  78. /* only relevant for PCI devices */
  79. if (!dev_is_pci(dev->dev))
  80. return;
  81. pdev = to_pci_dev(dev->dev);
  82. vga_client_register(pdev, nouveau_vga_set_decode);
  83. /* don't register Thunderbolt eGPU with vga_switcheroo */
  84. if (pci_is_thunderbolt_attached(pdev))
  85. return;
  86. vga_switcheroo_register_client(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 = nouveau_pmops_runtime();
  95. struct pci_dev *pdev;
  96. /* only relevant for PCI devices */
  97. if (!dev_is_pci(dev->dev))
  98. return;
  99. pdev = to_pci_dev(dev->dev);
  100. vga_client_unregister(pdev);
  101. if (pci_is_thunderbolt_attached(pdev))
  102. return;
  103. vga_switcheroo_unregister_client(pdev);
  104. if (runtime && nouveau_is_v1_dsm() && !nouveau_is_optimus())
  105. vga_switcheroo_fini_domain_pm_ops(drm->dev->dev);
  106. }
  107. void
  108. nouveau_vga_lastclose(struct drm_device *dev)
  109. {
  110. vga_switcheroo_process_delayed_switch();
  111. }