xref: /dflybsd-src/sys/dev/drm/include/linux/vga_switcheroo.h (revision a7d4637632ad5e829ba2f255d0edae845b680983)
12fc297a3SFrançois Tigeot /*
22fc297a3SFrançois Tigeot  * vga_switcheroo.h - Support for laptop with dual GPU using one set of outputs
32fc297a3SFrançois Tigeot  *
42fc297a3SFrançois Tigeot  * Copyright (c) 2010 Red Hat Inc.
52fc297a3SFrançois Tigeot  * Author : Dave Airlie <airlied@redhat.com>
62fc297a3SFrançois Tigeot  *
72fc297a3SFrançois Tigeot  * Copyright (c) 2015 Lukas Wunner <lukas@wunner.de>
82fc297a3SFrançois Tigeot  *
92fc297a3SFrançois Tigeot  * Permission is hereby granted, free of charge, to any person obtaining a
102fc297a3SFrançois Tigeot  * copy of this software and associated documentation files (the "Software"),
112fc297a3SFrançois Tigeot  * to deal in the Software without restriction, including without limitation
122fc297a3SFrançois Tigeot  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
132fc297a3SFrançois Tigeot  * and/or sell copies of the Software, and to permit persons to whom the
142fc297a3SFrançois Tigeot  * Software is furnished to do so, subject to the following conditions:
152fc297a3SFrançois Tigeot  *
162fc297a3SFrançois Tigeot  * The above copyright notice and this permission notice (including the next
172fc297a3SFrançois Tigeot  * paragraph) shall be included in all copies or substantial portions of the
182fc297a3SFrançois Tigeot  * Software.
192fc297a3SFrançois Tigeot  *
202fc297a3SFrançois Tigeot  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
212fc297a3SFrançois Tigeot  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
222fc297a3SFrançois Tigeot  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
232fc297a3SFrançois Tigeot  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
242fc297a3SFrançois Tigeot  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
252fc297a3SFrançois Tigeot  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
262fc297a3SFrançois Tigeot  * DEALINGS
272fc297a3SFrançois Tigeot  * IN THE SOFTWARE.
282fc297a3SFrançois Tigeot  *
292fc297a3SFrançois Tigeot  */
302fc297a3SFrançois Tigeot 
312fc297a3SFrançois Tigeot #ifndef _LINUX_VGA_SWITCHEROO_H_
322fc297a3SFrançois Tigeot #define _LINUX_VGA_SWITCHEROO_H_
332fc297a3SFrançois Tigeot 
342fc297a3SFrançois Tigeot #include <linux/fb.h>
352fc297a3SFrançois Tigeot 
362fc297a3SFrançois Tigeot struct pci_dev;
372fc297a3SFrançois Tigeot 
382fc297a3SFrançois Tigeot /**
392fc297a3SFrançois Tigeot  * enum vga_switcheroo_handler_flags_t - handler flags bitmask
402fc297a3SFrançois Tigeot  * @VGA_SWITCHEROO_CAN_SWITCH_DDC: whether the handler is able to switch the
412fc297a3SFrançois Tigeot  * 	DDC lines separately. This signals to clients that they should call
422fc297a3SFrançois Tigeot  * 	drm_get_edid_switcheroo() to probe the EDID
432fc297a3SFrançois Tigeot  * @VGA_SWITCHEROO_NEEDS_EDP_CONFIG: whether the handler is unable to switch
442fc297a3SFrançois Tigeot  * 	the AUX channel separately. This signals to clients that the active
452fc297a3SFrançois Tigeot  * 	GPU needs to train the link and communicate the link parameters to the
462fc297a3SFrançois Tigeot  * 	inactive GPU (mediated by vga_switcheroo). The inactive GPU may then
472fc297a3SFrançois Tigeot  * 	skip the AUX handshake and set up its output with these pre-calibrated
482fc297a3SFrançois Tigeot  * 	values (DisplayPort specification v1.1a, section 2.5.3.3)
492fc297a3SFrançois Tigeot  *
502fc297a3SFrançois Tigeot  * Handler flags bitmask. Used by handlers to declare their capabilities upon
512fc297a3SFrançois Tigeot  * registering with vga_switcheroo.
522fc297a3SFrançois Tigeot  */
532fc297a3SFrançois Tigeot enum vga_switcheroo_handler_flags_t {
542fc297a3SFrançois Tigeot 	VGA_SWITCHEROO_CAN_SWITCH_DDC	= (1 << 0),
552fc297a3SFrançois Tigeot 	VGA_SWITCHEROO_NEEDS_EDP_CONFIG	= (1 << 1),
562fc297a3SFrançois Tigeot };
572fc297a3SFrançois Tigeot 
582fc297a3SFrançois Tigeot /**
592fc297a3SFrançois Tigeot  * enum vga_switcheroo_state - client power state
602fc297a3SFrançois Tigeot  * @VGA_SWITCHEROO_OFF: off
612fc297a3SFrançois Tigeot  * @VGA_SWITCHEROO_ON: on
622fc297a3SFrançois Tigeot  * @VGA_SWITCHEROO_NOT_FOUND: client has not registered with vga_switcheroo.
632fc297a3SFrançois Tigeot  * 	Only used in vga_switcheroo_get_client_state() which in turn is only
642fc297a3SFrançois Tigeot  * 	called from hda_intel.c
652fc297a3SFrançois Tigeot  *
662fc297a3SFrançois Tigeot  * Client power state.
672fc297a3SFrançois Tigeot  */
682fc297a3SFrançois Tigeot enum vga_switcheroo_state {
692fc297a3SFrançois Tigeot 	VGA_SWITCHEROO_OFF,
702fc297a3SFrançois Tigeot 	VGA_SWITCHEROO_ON,
712fc297a3SFrançois Tigeot 	/* below are referred only from vga_switcheroo_get_client_state() */
722fc297a3SFrançois Tigeot 	VGA_SWITCHEROO_NOT_FOUND,
732fc297a3SFrançois Tigeot };
742fc297a3SFrançois Tigeot 
752fc297a3SFrançois Tigeot /**
762fc297a3SFrançois Tigeot  * enum vga_switcheroo_client_id - client identifier
772fc297a3SFrançois Tigeot  * @VGA_SWITCHEROO_UNKNOWN_ID: initial identifier assigned to vga clients.
782fc297a3SFrançois Tigeot  * 	Determining the id requires the handler, so GPUs are given their
792fc297a3SFrançois Tigeot  * 	true id in a delayed fashion in vga_switcheroo_enable()
802fc297a3SFrançois Tigeot  * @VGA_SWITCHEROO_IGD: integrated graphics device
812fc297a3SFrançois Tigeot  * @VGA_SWITCHEROO_DIS: discrete graphics device
822fc297a3SFrançois Tigeot  * @VGA_SWITCHEROO_MAX_CLIENTS: currently no more than two GPUs are supported
832fc297a3SFrançois Tigeot  *
842fc297a3SFrançois Tigeot  * Client identifier. Audio clients use the same identifier & 0x100.
852fc297a3SFrançois Tigeot  */
862fc297a3SFrançois Tigeot enum vga_switcheroo_client_id {
872fc297a3SFrançois Tigeot 	VGA_SWITCHEROO_UNKNOWN_ID = -1,
882fc297a3SFrançois Tigeot 	VGA_SWITCHEROO_IGD,
892fc297a3SFrançois Tigeot 	VGA_SWITCHEROO_DIS,
902fc297a3SFrançois Tigeot 	VGA_SWITCHEROO_MAX_CLIENTS,
912fc297a3SFrançois Tigeot };
922fc297a3SFrançois Tigeot 
932fc297a3SFrançois Tigeot /**
942fc297a3SFrançois Tigeot  * struct vga_switcheroo_handler - handler callbacks
952fc297a3SFrançois Tigeot  * @init: initialize handler.
962fc297a3SFrançois Tigeot  * 	Optional. This gets called when vga_switcheroo is enabled, i.e. when
972fc297a3SFrançois Tigeot  * 	two vga clients have registered. It allows the handler to perform
982fc297a3SFrançois Tigeot  * 	some delayed initialization that depends on the existence of the
992fc297a3SFrançois Tigeot  * 	vga clients. Currently only the radeon and amdgpu drivers use this.
1002fc297a3SFrançois Tigeot  * 	The return value is ignored
1012fc297a3SFrançois Tigeot  * @switchto: switch outputs to given client.
1022fc297a3SFrançois Tigeot  * 	Mandatory. For muxless machines this should be a no-op. Returning 0
1032fc297a3SFrançois Tigeot  * 	denotes success, anything else failure (in which case the switch is
1042fc297a3SFrançois Tigeot  * 	aborted)
1052fc297a3SFrançois Tigeot  * @switch_ddc: switch DDC lines to given client.
1062fc297a3SFrançois Tigeot  * 	Optional. Should return the previous DDC owner on success or a
1072fc297a3SFrançois Tigeot  * 	negative int on failure
1082fc297a3SFrançois Tigeot  * @power_state: cut or reinstate power of given client.
1092fc297a3SFrançois Tigeot  * 	Optional. The return value is ignored
1102fc297a3SFrançois Tigeot  * @get_client_id: determine if given pci device is integrated or discrete GPU.
1112fc297a3SFrançois Tigeot  * 	Mandatory
1122fc297a3SFrançois Tigeot  *
1132fc297a3SFrançois Tigeot  * Handler callbacks. The multiplexer itself. The @switchto and @get_client_id
1142fc297a3SFrançois Tigeot  * methods are mandatory, all others may be set to NULL.
1152fc297a3SFrançois Tigeot  */
1162fc297a3SFrançois Tigeot struct vga_switcheroo_handler {
1172fc297a3SFrançois Tigeot 	int (*init)(void);
1182fc297a3SFrançois Tigeot 	int (*switchto)(enum vga_switcheroo_client_id id);
1192fc297a3SFrançois Tigeot 	int (*switch_ddc)(enum vga_switcheroo_client_id id);
1202fc297a3SFrançois Tigeot 	int (*power_state)(enum vga_switcheroo_client_id id,
1212fc297a3SFrançois Tigeot 			   enum vga_switcheroo_state state);
1222fc297a3SFrançois Tigeot 	enum vga_switcheroo_client_id (*get_client_id)(struct pci_dev *pdev);
1232fc297a3SFrançois Tigeot };
1242fc297a3SFrançois Tigeot 
1252fc297a3SFrançois Tigeot /**
1262fc297a3SFrançois Tigeot  * struct vga_switcheroo_client_ops - client callbacks
1272fc297a3SFrançois Tigeot  * @set_gpu_state: do the equivalent of suspend/resume for the card.
1282fc297a3SFrançois Tigeot  * 	Mandatory. This should not cut power to the discrete GPU,
1292fc297a3SFrançois Tigeot  * 	which is the job of the handler
1302fc297a3SFrançois Tigeot  * @reprobe: poll outputs.
1312fc297a3SFrançois Tigeot  * 	Optional. This gets called after waking the GPU and switching
1322fc297a3SFrançois Tigeot  * 	the outputs to it
1332fc297a3SFrançois Tigeot  * @can_switch: check if the device is in a position to switch now.
1342fc297a3SFrançois Tigeot  * 	Mandatory. The client should return false if a user space process
1352fc297a3SFrançois Tigeot  * 	has one of its device files open
1362fc297a3SFrançois Tigeot  *
1372fc297a3SFrançois Tigeot  * Client callbacks. A client can be either a GPU or an audio device on a GPU.
1382fc297a3SFrançois Tigeot  * The @set_gpu_state and @can_switch methods are mandatory, @reprobe may be
1392fc297a3SFrançois Tigeot  * set to NULL. For audio clients, the @reprobe member is bogus.
1402fc297a3SFrançois Tigeot  */
1412fc297a3SFrançois Tigeot struct vga_switcheroo_client_ops {
1422fc297a3SFrançois Tigeot 	void (*set_gpu_state)(struct pci_dev *dev, enum vga_switcheroo_state);
1432fc297a3SFrançois Tigeot 	void (*reprobe)(struct pci_dev *dev);
1442fc297a3SFrançois Tigeot 	bool (*can_switch)(struct pci_dev *dev);
1452fc297a3SFrançois Tigeot };
1462fc297a3SFrançois Tigeot 
1472fc297a3SFrançois Tigeot #if defined(VGA_SWITCHEROO)
1482fc297a3SFrançois Tigeot void vga_switcheroo_unregister_client(struct pci_dev *dev);
1492fc297a3SFrançois Tigeot int vga_switcheroo_register_client(struct pci_dev *dev,
1502fc297a3SFrançois Tigeot 				   const struct vga_switcheroo_client_ops *ops,
1512fc297a3SFrançois Tigeot 				   bool driver_power_control);
1522fc297a3SFrançois Tigeot int vga_switcheroo_register_audio_client(struct pci_dev *pdev,
1532fc297a3SFrançois Tigeot 					 const struct vga_switcheroo_client_ops *ops,
1542fc297a3SFrançois Tigeot 					 enum vga_switcheroo_client_id id);
1552fc297a3SFrançois Tigeot 
1562fc297a3SFrançois Tigeot void vga_switcheroo_client_fb_set(struct pci_dev *dev,
1572fc297a3SFrançois Tigeot 				  struct fb_info *info);
1582fc297a3SFrançois Tigeot 
1592fc297a3SFrançois Tigeot int vga_switcheroo_register_handler(const struct vga_switcheroo_handler *handler,
1602fc297a3SFrançois Tigeot 				    enum vga_switcheroo_handler_flags_t handler_flags);
1612fc297a3SFrançois Tigeot void vga_switcheroo_unregister_handler(void);
1622fc297a3SFrançois Tigeot enum vga_switcheroo_handler_flags_t vga_switcheroo_handler_flags(void);
1632fc297a3SFrançois Tigeot int vga_switcheroo_lock_ddc(struct pci_dev *pdev);
1642fc297a3SFrançois Tigeot int vga_switcheroo_unlock_ddc(struct pci_dev *pdev);
1652fc297a3SFrançois Tigeot 
1662fc297a3SFrançois Tigeot int vga_switcheroo_process_delayed_switch(void);
1672fc297a3SFrançois Tigeot 
1682fc297a3SFrançois Tigeot enum vga_switcheroo_state vga_switcheroo_get_client_state(struct pci_dev *dev);
1692fc297a3SFrançois Tigeot 
1702fc297a3SFrançois Tigeot #if 0  /* dynamic_switch */
1712fc297a3SFrançois Tigeot void vga_switcheroo_set_dynamic_switch(struct pci_dev *pdev, enum vga_switcheroo_state dynamic);
1722fc297a3SFrançois Tigeot #endif  /* dynamic_switch */
1732fc297a3SFrançois Tigeot 
1742fc297a3SFrançois Tigeot #if 0  /* pm_ops */
1752fc297a3SFrançois Tigeot int vga_switcheroo_init_domain_pm_ops(struct device *dev, struct dev_pm_domain *domain);
1762fc297a3SFrançois Tigeot void vga_switcheroo_fini_domain_pm_ops(struct device *dev);
1772fc297a3SFrançois Tigeot int vga_switcheroo_init_domain_pm_optimus_hdmi_audio(struct device *dev, struct dev_pm_domain *domain);
1782fc297a3SFrançois Tigeot #endif  /* pm_ops */
179*a7d46376SPeeter Must #ifdef __DragonFly__
180*a7d46376SPeeter Must int vga_switcheroo_force_migd(void);
181*a7d46376SPeeter Must #endif
1822fc297a3SFrançois Tigeot #else  /* VGA_SWITCHEROO */
1832fc297a3SFrançois Tigeot 
vga_switcheroo_unregister_client(struct pci_dev * dev)1842fc297a3SFrançois Tigeot static inline void vga_switcheroo_unregister_client(struct pci_dev *dev) {}
vga_switcheroo_register_client(struct pci_dev * dev,const struct vga_switcheroo_client_ops * ops,bool driver_power_control)1852fc297a3SFrançois Tigeot static inline int vga_switcheroo_register_client(struct pci_dev *dev,
1862fc297a3SFrançois Tigeot 		const struct vga_switcheroo_client_ops *ops, bool driver_power_control) { return 0; }
vga_switcheroo_client_fb_set(struct pci_dev * dev,struct fb_info * info)1872fc297a3SFrançois Tigeot static inline void vga_switcheroo_client_fb_set(struct pci_dev *dev, struct fb_info *info) {}
vga_switcheroo_register_handler(const struct vga_switcheroo_handler * handler,enum vga_switcheroo_handler_flags_t handler_flags)1882fc297a3SFrançois Tigeot static inline int vga_switcheroo_register_handler(const struct vga_switcheroo_handler *handler,
1892fc297a3SFrançois Tigeot 		enum vga_switcheroo_handler_flags_t handler_flags) { return 0; }
vga_switcheroo_register_audio_client(struct pci_dev * pdev,const struct vga_switcheroo_client_ops * ops,enum vga_switcheroo_client_id id)1902fc297a3SFrançois Tigeot static inline int vga_switcheroo_register_audio_client(struct pci_dev *pdev,
1912fc297a3SFrançois Tigeot 	const struct vga_switcheroo_client_ops *ops,
1922fc297a3SFrançois Tigeot 	enum vga_switcheroo_client_id id) { return 0; }
vga_switcheroo_unregister_handler(void)1932fc297a3SFrançois Tigeot static inline void vga_switcheroo_unregister_handler(void) {}
vga_switcheroo_handler_flags(void)1942fc297a3SFrançois Tigeot static inline enum vga_switcheroo_handler_flags_t vga_switcheroo_handler_flags(void) { return 0; }
vga_switcheroo_lock_ddc(struct pci_dev * pdev)1952fc297a3SFrançois Tigeot static inline int vga_switcheroo_lock_ddc(struct pci_dev *pdev) { return -ENODEV; }
vga_switcheroo_unlock_ddc(struct pci_dev * pdev)1962fc297a3SFrançois Tigeot static inline int vga_switcheroo_unlock_ddc(struct pci_dev *pdev) { return -ENODEV; }
vga_switcheroo_process_delayed_switch(void)1972fc297a3SFrançois Tigeot static inline int vga_switcheroo_process_delayed_switch(void) { return 0; }
vga_switcheroo_client_probe_defer(struct pci_dev * pdev)1982fc297a3SFrançois Tigeot static inline bool vga_switcheroo_client_probe_defer(struct pci_dev *pdev) { return false; }
vga_switcheroo_get_client_state(struct pci_dev * dev)1992fc297a3SFrançois Tigeot static inline enum vga_switcheroo_state vga_switcheroo_get_client_state(struct pci_dev *dev) { return VGA_SWITCHEROO_ON; }
2002fc297a3SFrançois Tigeot 
2012fc297a3SFrançois Tigeot #if 0  /* dynamic_switch */
2022fc297a3SFrançois Tigeot static inline void vga_switcheroo_set_dynamic_switch(struct pci_dev *pdev, enum vga_switcheroo_state dynamic) {}
2032fc297a3SFrançois Tigeot #endif  /* dynamic_switch */
2042fc297a3SFrançois Tigeot 
2052fc297a3SFrançois Tigeot #if 0  /* pm_ops */
2062fc297a3SFrançois Tigeot static inline int vga_switcheroo_init_domain_pm_ops(struct device *dev, struct dev_pm_domain *domain) { return -EINVAL; }
2072fc297a3SFrançois Tigeot static inline void vga_switcheroo_fini_domain_pm_ops(struct device *dev) {}
2082fc297a3SFrançois Tigeot static inline int vga_switcheroo_init_domain_pm_optimus_hdmi_audio(struct device *dev, struct dev_pm_domain *domain) { return -EINVAL; }
2092fc297a3SFrançois Tigeot #endif /* pm_ops */
210*a7d46376SPeeter Must #ifdef __DragonFly__
vga_switcheroo_force_migd(void)211*a7d46376SPeeter Must static inline int vga_switcheroo_force_migd(void) { return 0; }
212*a7d46376SPeeter Must #endif
2132fc297a3SFrançois Tigeot 
2142fc297a3SFrançois Tigeot #endif /* VGA_SWITCHEROO */
2152fc297a3SFrançois Tigeot #endif /* _LINUX_VGA_SWITCHEROO_H_ */
216