vga_switcheroo.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * vga_switcheroo.h - Support for laptop with dual GPU using one set of outputs
  3. *
  4. * Copyright (c) 2010 Red Hat Inc.
  5. * Author : Dave Airlie <airlied@redhat.com>
  6. *
  7. * Copyright (c) 2015 Lukas Wunner <lukas@wunner.de>
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a
  10. * copy of this software and associated documentation files (the "Software"),
  11. * to deal in the Software without restriction, including without limitation
  12. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  13. * and/or sell copies of the Software, and to permit persons to whom the
  14. * Software is furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice (including the next
  17. * paragraph) shall be included in all copies or substantial portions of the
  18. * Software.
  19. *
  20. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  23. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  24. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  25. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  26. * DEALINGS
  27. * IN THE SOFTWARE.
  28. *
  29. */
  30. #ifndef _LINUX_VGA_SWITCHEROO_H_
  31. #define _LINUX_VGA_SWITCHEROO_H_
  32. #include <linux/fb.h>
  33. struct pci_dev;
  34. /**
  35. * enum vga_switcheroo_handler_flags_t - handler flags bitmask
  36. * @VGA_SWITCHEROO_CAN_SWITCH_DDC: whether the handler is able to switch the
  37. * DDC lines separately. This signals to clients that they should call
  38. * drm_get_edid_switcheroo() to probe the EDID
  39. * @VGA_SWITCHEROO_NEEDS_EDP_CONFIG: whether the handler is unable to switch
  40. * the AUX channel separately. This signals to clients that the active
  41. * GPU needs to train the link and communicate the link parameters to the
  42. * inactive GPU (mediated by vga_switcheroo). The inactive GPU may then
  43. * skip the AUX handshake and set up its output with these pre-calibrated
  44. * values (DisplayPort specification v1.1a, section 2.5.3.3)
  45. *
  46. * Handler flags bitmask. Used by handlers to declare their capabilities upon
  47. * registering with vga_switcheroo.
  48. */
  49. enum vga_switcheroo_handler_flags_t {
  50. VGA_SWITCHEROO_CAN_SWITCH_DDC = (1 << 0),
  51. VGA_SWITCHEROO_NEEDS_EDP_CONFIG = (1 << 1),
  52. };
  53. /**
  54. * enum vga_switcheroo_state - client power state
  55. * @VGA_SWITCHEROO_OFF: off
  56. * @VGA_SWITCHEROO_ON: on
  57. * @VGA_SWITCHEROO_NOT_FOUND: client has not registered with vga_switcheroo.
  58. * Only used in vga_switcheroo_get_client_state() which in turn is only
  59. * called from hda_intel.c
  60. *
  61. * Client power state.
  62. */
  63. enum vga_switcheroo_state {
  64. VGA_SWITCHEROO_OFF,
  65. VGA_SWITCHEROO_ON,
  66. /* below are referred only from vga_switcheroo_get_client_state() */
  67. VGA_SWITCHEROO_NOT_FOUND,
  68. };
  69. /**
  70. * enum vga_switcheroo_client_id - client identifier
  71. * @VGA_SWITCHEROO_UNKNOWN_ID: initial identifier assigned to vga clients.
  72. * Determining the id requires the handler, so GPUs are given their
  73. * true id in a delayed fashion in vga_switcheroo_enable()
  74. * @VGA_SWITCHEROO_IGD: integrated graphics device
  75. * @VGA_SWITCHEROO_DIS: discrete graphics device
  76. * @VGA_SWITCHEROO_MAX_CLIENTS: currently no more than two GPUs are supported
  77. *
  78. * Client identifier. Audio clients use the same identifier & 0x100.
  79. */
  80. enum vga_switcheroo_client_id {
  81. VGA_SWITCHEROO_UNKNOWN_ID = 0x1000,
  82. VGA_SWITCHEROO_IGD = 0,
  83. VGA_SWITCHEROO_DIS,
  84. VGA_SWITCHEROO_MAX_CLIENTS,
  85. };
  86. /**
  87. * struct vga_switcheroo_handler - handler callbacks
  88. * @init: initialize handler.
  89. * Optional. This gets called when vga_switcheroo is enabled, i.e. when
  90. * two vga clients have registered. It allows the handler to perform
  91. * some delayed initialization that depends on the existence of the
  92. * vga clients. Currently only the radeon and amdgpu drivers use this.
  93. * The return value is ignored
  94. * @switchto: switch outputs to given client.
  95. * Mandatory. For muxless machines this should be a no-op. Returning 0
  96. * denotes success, anything else failure (in which case the switch is
  97. * aborted)
  98. * @switch_ddc: switch DDC lines to given client.
  99. * Optional. Should return the previous DDC owner on success or a
  100. * negative int on failure
  101. * @power_state: cut or reinstate power of given client.
  102. * Optional. The return value is ignored
  103. * @get_client_id: determine if given pci device is integrated or discrete GPU.
  104. * Mandatory
  105. *
  106. * Handler callbacks. The multiplexer itself. The @switchto and @get_client_id
  107. * methods are mandatory, all others may be set to NULL.
  108. */
  109. struct vga_switcheroo_handler {
  110. int (*init)(void);
  111. int (*switchto)(enum vga_switcheroo_client_id id);
  112. int (*switch_ddc)(enum vga_switcheroo_client_id id);
  113. int (*power_state)(enum vga_switcheroo_client_id id,
  114. enum vga_switcheroo_state state);
  115. enum vga_switcheroo_client_id (*get_client_id)(struct pci_dev *pdev);
  116. };
  117. /**
  118. * struct vga_switcheroo_client_ops - client callbacks
  119. * @set_gpu_state: do the equivalent of suspend/resume for the card.
  120. * Mandatory. This should not cut power to the discrete GPU,
  121. * which is the job of the handler
  122. * @reprobe: poll outputs.
  123. * Optional. This gets called after waking the GPU and switching
  124. * the outputs to it
  125. * @can_switch: check if the device is in a position to switch now.
  126. * Mandatory. The client should return false if a user space process
  127. * has one of its device files open
  128. * @gpu_bound: notify the client id to audio client when the GPU is bound.
  129. *
  130. * Client callbacks. A client can be either a GPU or an audio device on a GPU.
  131. * The @set_gpu_state and @can_switch methods are mandatory, @reprobe may be
  132. * set to NULL. For audio clients, the @reprobe member is bogus.
  133. * OTOH, @gpu_bound is only for audio clients, and not used for GPU clients.
  134. */
  135. struct vga_switcheroo_client_ops {
  136. void (*set_gpu_state)(struct pci_dev *dev, enum vga_switcheroo_state);
  137. void (*reprobe)(struct pci_dev *dev);
  138. bool (*can_switch)(struct pci_dev *dev);
  139. void (*gpu_bound)(struct pci_dev *dev, enum vga_switcheroo_client_id);
  140. };
  141. #if defined(CONFIG_VGA_SWITCHEROO)
  142. void vga_switcheroo_unregister_client(struct pci_dev *dev);
  143. int vga_switcheroo_register_client(struct pci_dev *dev,
  144. const struct vga_switcheroo_client_ops *ops,
  145. bool driver_power_control);
  146. int vga_switcheroo_register_audio_client(struct pci_dev *pdev,
  147. const struct vga_switcheroo_client_ops *ops,
  148. struct pci_dev *vga_dev);
  149. void vga_switcheroo_client_fb_set(struct pci_dev *dev,
  150. struct fb_info *info);
  151. int vga_switcheroo_register_handler(const struct vga_switcheroo_handler *handler,
  152. enum vga_switcheroo_handler_flags_t handler_flags);
  153. void vga_switcheroo_unregister_handler(void);
  154. enum vga_switcheroo_handler_flags_t vga_switcheroo_handler_flags(void);
  155. int vga_switcheroo_lock_ddc(struct pci_dev *pdev);
  156. int vga_switcheroo_unlock_ddc(struct pci_dev *pdev);
  157. int vga_switcheroo_process_delayed_switch(void);
  158. bool vga_switcheroo_client_probe_defer(struct pci_dev *pdev);
  159. enum vga_switcheroo_state vga_switcheroo_get_client_state(struct pci_dev *dev);
  160. int vga_switcheroo_init_domain_pm_ops(struct device *dev, struct dev_pm_domain *domain);
  161. void vga_switcheroo_fini_domain_pm_ops(struct device *dev);
  162. #else
  163. static inline void vga_switcheroo_unregister_client(struct pci_dev *dev) {}
  164. static inline int vga_switcheroo_register_client(struct pci_dev *dev,
  165. const struct vga_switcheroo_client_ops *ops, bool driver_power_control) { return 0; }
  166. static inline void vga_switcheroo_client_fb_set(struct pci_dev *dev, struct fb_info *info) {}
  167. static inline int vga_switcheroo_register_handler(const struct vga_switcheroo_handler *handler,
  168. enum vga_switcheroo_handler_flags_t handler_flags) { return 0; }
  169. static inline int vga_switcheroo_register_audio_client(struct pci_dev *pdev,
  170. const struct vga_switcheroo_client_ops *ops,
  171. struct pci_dev *vga_dev) { return 0; }
  172. static inline void vga_switcheroo_unregister_handler(void) {}
  173. static inline enum vga_switcheroo_handler_flags_t vga_switcheroo_handler_flags(void) { return 0; }
  174. static inline int vga_switcheroo_lock_ddc(struct pci_dev *pdev) { return -ENODEV; }
  175. static inline int vga_switcheroo_unlock_ddc(struct pci_dev *pdev) { return -ENODEV; }
  176. static inline int vga_switcheroo_process_delayed_switch(void) { return 0; }
  177. static inline bool vga_switcheroo_client_probe_defer(struct pci_dev *pdev) { return false; }
  178. static inline enum vga_switcheroo_state vga_switcheroo_get_client_state(struct pci_dev *dev) { return VGA_SWITCHEROO_ON; }
  179. static inline int vga_switcheroo_init_domain_pm_ops(struct device *dev, struct dev_pm_domain *domain) { return -EINVAL; }
  180. static inline void vga_switcheroo_fini_domain_pm_ops(struct device *dev) {}
  181. #endif
  182. #endif /* _LINUX_VGA_SWITCHEROO_H_ */