nouveau_vga.c 3.1 KB

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