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

source code of linux/drivers/gpu/drm/i915/i915_switcheroo.c