i915_switcheroo.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // SPDX-License-Identifier: MIT
  2. /*
  3. * Copyright © 2019 Intel Corporation
  4. */
  5. #include <linux/vga_switcheroo.h>
  6. #include <drm/drm_print.h>
  7. #include "display/intel_display_device.h"
  8. #include "i915_driver.h"
  9. #include "i915_drv.h"
  10. #include "i915_switcheroo.h"
  11. static void i915_switcheroo_set_state(struct pci_dev *pdev,
  12. enum vga_switcheroo_state state)
  13. {
  14. struct drm_i915_private *i915 = pdev_to_i915(pdev);
  15. struct intel_display *display = i915 ? i915->display : NULL;
  16. pm_message_t pmm = { .event = PM_EVENT_SUSPEND };
  17. if (!i915) {
  18. dev_err(&pdev->dev, "DRM not initialized, aborting switch.\n");
  19. return;
  20. }
  21. if (!intel_display_device_present(display)) {
  22. dev_err(&pdev->dev, "Device state not initialized, aborting switch.\n");
  23. return;
  24. }
  25. if (state == VGA_SWITCHEROO_ON) {
  26. drm_info(&i915->drm, "switched on\n");
  27. i915->drm.switch_power_state = DRM_SWITCH_POWER_CHANGING;
  28. /* i915 resume handler doesn't set to D0 */
  29. pci_set_power_state(pdev, PCI_D0);
  30. i915_driver_resume_switcheroo(i915);
  31. i915->drm.switch_power_state = DRM_SWITCH_POWER_ON;
  32. } else {
  33. drm_info(&i915->drm, "switched off\n");
  34. i915->drm.switch_power_state = DRM_SWITCH_POWER_CHANGING;
  35. i915_driver_suspend_switcheroo(i915, pmm);
  36. i915->drm.switch_power_state = DRM_SWITCH_POWER_OFF;
  37. }
  38. }
  39. static bool i915_switcheroo_can_switch(struct pci_dev *pdev)
  40. {
  41. struct drm_i915_private *i915 = pdev_to_i915(pdev);
  42. struct intel_display *display = i915 ? i915->display : NULL;
  43. /*
  44. * FIXME: open_count is protected by drm_global_mutex but that would lead to
  45. * locking inversion with the driver load path. And the access here is
  46. * completely racy anyway. So don't bother with locking for now.
  47. */
  48. return i915 && intel_display_device_present(display) &&
  49. atomic_read(&i915->drm.open_count) == 0;
  50. }
  51. static const struct vga_switcheroo_client_ops i915_switcheroo_ops = {
  52. .set_gpu_state = i915_switcheroo_set_state,
  53. .reprobe = NULL,
  54. .can_switch = i915_switcheroo_can_switch,
  55. };
  56. int i915_switcheroo_register(struct drm_i915_private *i915)
  57. {
  58. struct pci_dev *pdev = to_pci_dev(i915->drm.dev);
  59. return vga_switcheroo_register_client(pdev, &i915_switcheroo_ops, false);
  60. }
  61. void i915_switcheroo_unregister(struct drm_i915_private *i915)
  62. {
  63. struct pci_dev *pdev = to_pci_dev(i915->drm.dev);
  64. vga_switcheroo_unregister_client(pdev);
  65. }