Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 1 | /* |
Lukas Wunner | a645654 | 2015-08-23 15:18:55 +0200 | [diff] [blame] | 2 | * vga_switcheroo.c - Support for laptop with dual GPU using one set of outputs |
| 3 | * |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 4 | * Copyright (c) 2010 Red Hat Inc. |
| 5 | * Author : Dave Airlie <airlied@redhat.com> |
| 6 | * |
Lukas Wunner | a645654 | 2015-08-23 15:18:55 +0200 | [diff] [blame] | 7 | * Copyright (c) 2015 Lukas Wunner <lukas@wunner.de> |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 8 | * |
Lukas Wunner | a645654 | 2015-08-23 15:18:55 +0200 | [diff] [blame] | 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: |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 15 | * |
Lukas Wunner | a645654 | 2015-08-23 15:18:55 +0200 | [diff] [blame] | 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. |
Thierry Reding | 7130927 | 2015-08-12 16:32:09 +0200 | [diff] [blame] | 19 | * |
Lukas Wunner | a645654 | 2015-08-23 15:18:55 +0200 | [diff] [blame] | 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. |
Thierry Reding | 7130927 | 2015-08-12 16:32:09 +0200 | [diff] [blame] | 28 | * |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 29 | */ |
| 30 | |
Thierry Reding | 9b0be1e | 2015-08-12 16:32:10 +0200 | [diff] [blame] | 31 | #define pr_fmt(fmt) "vga_switcheroo: " fmt |
| 32 | |
Lukas Wunner | b00e533 | 2016-05-31 11:13:27 +0200 | [diff] [blame] | 33 | #include <linux/apple-gmux.h> |
Lukas Wunner | 4127838 | 2015-09-05 13:40:23 +0200 | [diff] [blame] | 34 | #include <linux/console.h> |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 35 | #include <linux/debugfs.h> |
| 36 | #include <linux/fb.h> |
Lukas Wunner | 4127838 | 2015-09-05 13:40:23 +0200 | [diff] [blame] | 37 | #include <linux/fs.h> |
| 38 | #include <linux/module.h> |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 39 | #include <linux/pci.h> |
Tomeu Vizoso | 989561d | 2016-01-07 16:46:13 +0100 | [diff] [blame] | 40 | #include <linux/pm_domain.h> |
Dave Airlie | 0d69704 | 2012-09-10 12:28:36 +1000 | [diff] [blame] | 41 | #include <linux/pm_runtime.h> |
Lukas Wunner | 4127838 | 2015-09-05 13:40:23 +0200 | [diff] [blame] | 42 | #include <linux/seq_file.h> |
| 43 | #include <linux/uaccess.h> |
Matthew Garrett | 2fbe8c7 | 2012-04-16 16:26:03 -0400 | [diff] [blame] | 44 | #include <linux/vgaarb.h> |
Lukas Wunner | 4127838 | 2015-09-05 13:40:23 +0200 | [diff] [blame] | 45 | #include <linux/vga_switcheroo.h> |
Matthew Garrett | 2fbe8c7 | 2012-04-16 16:26:03 -0400 | [diff] [blame] | 46 | |
Lukas Wunner | a645654 | 2015-08-23 15:18:55 +0200 | [diff] [blame] | 47 | /** |
| 48 | * DOC: Overview |
| 49 | * |
| 50 | * vga_switcheroo is the Linux subsystem for laptop hybrid graphics. |
| 51 | * These come in two flavors: |
| 52 | * |
| 53 | * * muxed: Dual GPUs with a multiplexer chip to switch outputs between GPUs. |
| 54 | * * muxless: Dual GPUs but only one of them is connected to outputs. |
Lukas Wunner | fb001df | 2016-07-11 10:22:33 +0200 | [diff] [blame] | 55 | * The other one is merely used to offload rendering, its results |
| 56 | * are copied over PCIe into the framebuffer. On Linux this is |
| 57 | * supported with DRI PRIME. |
Lukas Wunner | a645654 | 2015-08-23 15:18:55 +0200 | [diff] [blame] | 58 | * |
| 59 | * Hybrid graphics started to appear in the late Naughties and were initially |
| 60 | * all muxed. Newer laptops moved to a muxless architecture for cost reasons. |
| 61 | * A notable exception is the MacBook Pro which continues to use a mux. |
| 62 | * Muxes come with varying capabilities: Some switch only the panel, others |
| 63 | * can also switch external displays. Some switch all display pins at once |
| 64 | * while others can switch just the DDC lines. (To allow EDID probing |
| 65 | * for the inactive GPU.) Also, muxes are often used to cut power to the |
| 66 | * discrete GPU while it is not used. |
| 67 | * |
Lukas Wunner | 3a84866 | 2016-01-02 18:47:17 +0100 | [diff] [blame] | 68 | * DRM drivers register GPUs with vga_switcheroo, these are henceforth called |
Lukas Wunner | a645654 | 2015-08-23 15:18:55 +0200 | [diff] [blame] | 69 | * clients. The mux is called the handler. Muxless machines also register a |
| 70 | * handler to control the power state of the discrete GPU, its ->switchto |
| 71 | * callback is a no-op for obvious reasons. The discrete GPU is often equipped |
| 72 | * with an HDA controller for the HDMI/DP audio signal, this will also |
| 73 | * register as a client so that vga_switcheroo can take care of the correct |
| 74 | * suspend/resume order when changing the discrete GPU's power state. In total |
| 75 | * there can thus be up to three clients: Two vga clients (GPUs) and one audio |
| 76 | * client (on the discrete GPU). The code is mostly prepared to support |
| 77 | * machines with more than two GPUs should they become available. |
Lukas Wunner | e4cb81d | 2016-01-11 20:09:20 +0100 | [diff] [blame] | 78 | * |
Lukas Wunner | a645654 | 2015-08-23 15:18:55 +0200 | [diff] [blame] | 79 | * The GPU to which the outputs are currently switched is called the |
| 80 | * active client in vga_switcheroo parlance. The GPU not in use is the |
Lukas Wunner | e4cb81d | 2016-01-11 20:09:20 +0100 | [diff] [blame] | 81 | * inactive client. When the inactive client's DRM driver is loaded, |
| 82 | * it will be unable to probe the panel's EDID and hence depends on |
| 83 | * VBIOS to provide its display modes. If the VBIOS modes are bogus or |
| 84 | * if there is no VBIOS at all (which is common on the MacBook Pro), |
| 85 | * a client may alternatively request that the DDC lines are temporarily |
| 86 | * switched to it, provided that the handler supports this. Switching |
| 87 | * only the DDC lines and not the entire output avoids unnecessary |
| 88 | * flickering. |
Lukas Wunner | a645654 | 2015-08-23 15:18:55 +0200 | [diff] [blame] | 89 | */ |
| 90 | |
| 91 | /** |
| 92 | * struct vga_switcheroo_client - registered client |
| 93 | * @pdev: client pci device |
| 94 | * @fb_info: framebuffer to which console is remapped on switching |
| 95 | * @pwr_state: current power state |
| 96 | * @ops: client callbacks |
Lukas Wunner | 21c5ba8 | 2015-08-28 13:30:32 +0200 | [diff] [blame] | 97 | * @id: client identifier. Determining the id requires the handler, |
Joan Jani | fa6d513 | 2017-02-28 18:59:52 +0000 | [diff] [blame] | 98 | * so gpus are initially assigned VGA_SWITCHEROO_UNKNOWN_ID |
| 99 | * and later given their true id in vga_switcheroo_enable() |
Lukas Wunner | a645654 | 2015-08-23 15:18:55 +0200 | [diff] [blame] | 100 | * @active: whether the outputs are currently switched to this client |
| 101 | * @driver_power_control: whether power state is controlled by the driver's |
Joan Jani | fa6d513 | 2017-02-28 18:59:52 +0000 | [diff] [blame] | 102 | * runtime pm. If true, writing ON and OFF to the vga_switcheroo debugfs |
| 103 | * interface is a no-op so as not to interfere with runtime pm |
Lukas Wunner | a645654 | 2015-08-23 15:18:55 +0200 | [diff] [blame] | 104 | * @list: client list |
| 105 | * |
| 106 | * Registered client. A client can be either a GPU or an audio device on a GPU. |
| 107 | * For audio clients, the @fb_info, @active and @driver_power_control members |
| 108 | * are bogus. |
| 109 | */ |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 110 | struct vga_switcheroo_client { |
| 111 | struct pci_dev *pdev; |
| 112 | struct fb_info *fb_info; |
Lukas Wunner | 203d027 | 2015-08-28 11:56:26 +0200 | [diff] [blame] | 113 | enum vga_switcheroo_state pwr_state; |
Takashi Iwai | 26ec685 | 2012-05-11 07:51:17 +0200 | [diff] [blame] | 114 | const struct vga_switcheroo_client_ops *ops; |
Lukas Wunner | fa3e967 | 2015-08-28 12:54:07 +0200 | [diff] [blame] | 115 | enum vga_switcheroo_client_id id; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 116 | bool active; |
Dave Airlie | 0d69704 | 2012-09-10 12:28:36 +1000 | [diff] [blame] | 117 | bool driver_power_control; |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 118 | struct list_head list; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 119 | }; |
| 120 | |
Lukas Wunner | a645654 | 2015-08-23 15:18:55 +0200 | [diff] [blame] | 121 | /* |
| 122 | * protects access to struct vgasr_priv |
| 123 | */ |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 124 | static DEFINE_MUTEX(vgasr_mutex); |
| 125 | |
Lukas Wunner | a645654 | 2015-08-23 15:18:55 +0200 | [diff] [blame] | 126 | /** |
| 127 | * struct vgasr_priv - vga_switcheroo private data |
| 128 | * @active: whether vga_switcheroo is enabled. |
Joan Jani | fa6d513 | 2017-02-28 18:59:52 +0000 | [diff] [blame] | 129 | * Prerequisite is the registration of two GPUs and a handler |
Lukas Wunner | a645654 | 2015-08-23 15:18:55 +0200 | [diff] [blame] | 130 | * @delayed_switch_active: whether a delayed switch is pending |
| 131 | * @delayed_client_id: client to which a delayed switch is pending |
| 132 | * @debugfs_root: directory for vga_switcheroo debugfs interface |
| 133 | * @switch_file: file for vga_switcheroo debugfs interface |
| 134 | * @registered_clients: number of registered GPUs |
Joan Jani | fa6d513 | 2017-02-28 18:59:52 +0000 | [diff] [blame] | 135 | * (counting only vga clients, not audio clients) |
Lukas Wunner | a645654 | 2015-08-23 15:18:55 +0200 | [diff] [blame] | 136 | * @clients: list of registered clients |
| 137 | * @handler: registered handler |
Lukas Wunner | 156d7d4 | 2016-01-11 20:09:20 +0100 | [diff] [blame] | 138 | * @handler_flags: flags of registered handler |
Lukas Wunner | e4cb81d | 2016-01-11 20:09:20 +0100 | [diff] [blame] | 139 | * @mux_hw_lock: protects mux state |
| 140 | * (in particular while DDC lines are temporarily switched) |
| 141 | * @old_ddc_owner: client to which DDC lines will be switched back on unlock |
Lukas Wunner | a645654 | 2015-08-23 15:18:55 +0200 | [diff] [blame] | 142 | * |
| 143 | * vga_switcheroo private data. Currently only one vga_switcheroo instance |
| 144 | * per system is supported. |
| 145 | */ |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 146 | struct vgasr_priv { |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 147 | bool active; |
| 148 | bool delayed_switch_active; |
| 149 | enum vga_switcheroo_client_id delayed_client_id; |
| 150 | |
| 151 | struct dentry *debugfs_root; |
| 152 | struct dentry *switch_file; |
| 153 | |
| 154 | int registered_clients; |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 155 | struct list_head clients; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 156 | |
Lukas Wunner | 5d17013 | 2015-10-18 13:05:40 +0200 | [diff] [blame] | 157 | const struct vga_switcheroo_handler *handler; |
Lukas Wunner | 156d7d4 | 2016-01-11 20:09:20 +0100 | [diff] [blame] | 158 | enum vga_switcheroo_handler_flags_t handler_flags; |
Lukas Wunner | e4cb81d | 2016-01-11 20:09:20 +0100 | [diff] [blame] | 159 | struct mutex mux_hw_lock; |
| 160 | int old_ddc_owner; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 161 | }; |
| 162 | |
Takashi Iwai | 3e9e63d | 2012-04-26 14:29:48 +0200 | [diff] [blame] | 163 | #define ID_BIT_AUDIO 0x100 |
| 164 | #define client_is_audio(c) ((c)->id & ID_BIT_AUDIO) |
Lukas Wunner | 21c5ba8 | 2015-08-28 13:30:32 +0200 | [diff] [blame] | 165 | #define client_is_vga(c) ((c)->id == VGA_SWITCHEROO_UNKNOWN_ID || \ |
| 166 | !client_is_audio(c)) |
Takashi Iwai | 3e9e63d | 2012-04-26 14:29:48 +0200 | [diff] [blame] | 167 | #define client_id(c) ((c)->id & ~ID_BIT_AUDIO) |
| 168 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 169 | static int vga_switcheroo_debugfs_init(struct vgasr_priv *priv); |
| 170 | static void vga_switcheroo_debugfs_fini(struct vgasr_priv *priv); |
| 171 | |
| 172 | /* only one switcheroo per system */ |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 173 | static struct vgasr_priv vgasr_priv = { |
| 174 | .clients = LIST_HEAD_INIT(vgasr_priv.clients), |
Lukas Wunner | e4cb81d | 2016-01-11 20:09:20 +0100 | [diff] [blame] | 175 | .mux_hw_lock = __MUTEX_INITIALIZER(vgasr_priv.mux_hw_lock), |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 176 | }; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 177 | |
Seth Forshee | 36704c0 | 2012-08-17 11:17:03 -0500 | [diff] [blame] | 178 | static bool vga_switcheroo_ready(void) |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 179 | { |
Seth Forshee | 36704c0 | 2012-08-17 11:17:03 -0500 | [diff] [blame] | 180 | /* we're ready if we get two clients + handler */ |
| 181 | return !vgasr_priv.active && |
| 182 | vgasr_priv.registered_clients == 2 && vgasr_priv.handler; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 183 | } |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 184 | |
| 185 | static void vga_switcheroo_enable(void) |
| 186 | { |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 187 | int ret; |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 188 | struct vga_switcheroo_client *client; |
| 189 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 190 | /* call the handler to init */ |
Seth Forshee | e99eac5 | 2012-08-17 11:17:02 -0500 | [diff] [blame] | 191 | if (vgasr_priv.handler->init) |
| 192 | vgasr_priv.handler->init(); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 193 | |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 194 | list_for_each_entry(client, &vgasr_priv.clients, list) { |
Lukas Wunner | 21c5ba8 | 2015-08-28 13:30:32 +0200 | [diff] [blame] | 195 | if (client->id != VGA_SWITCHEROO_UNKNOWN_ID) |
Takashi Iwai | 3e9e63d | 2012-04-26 14:29:48 +0200 | [diff] [blame] | 196 | continue; |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 197 | ret = vgasr_priv.handler->get_client_id(client->pdev); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 198 | if (ret < 0) |
| 199 | return; |
| 200 | |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 201 | client->id = ret; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 202 | } |
| 203 | vga_switcheroo_debugfs_init(&vgasr_priv); |
| 204 | vgasr_priv.active = true; |
| 205 | } |
| 206 | |
Lukas Wunner | a645654 | 2015-08-23 15:18:55 +0200 | [diff] [blame] | 207 | /** |
| 208 | * vga_switcheroo_register_handler() - register handler |
| 209 | * @handler: handler callbacks |
Lukas Wunner | 156d7d4 | 2016-01-11 20:09:20 +0100 | [diff] [blame] | 210 | * @handler_flags: handler flags |
Lukas Wunner | a645654 | 2015-08-23 15:18:55 +0200 | [diff] [blame] | 211 | * |
| 212 | * Register handler. Enable vga_switcheroo if two vga clients have already |
| 213 | * registered. |
| 214 | * |
| 215 | * Return: 0 on success, -EINVAL if a handler was already registered. |
| 216 | */ |
Joan Jani | fa6d513 | 2017-02-28 18:59:52 +0000 | [diff] [blame] | 217 | int vga_switcheroo_register_handler( |
| 218 | const struct vga_switcheroo_handler *handler, |
| 219 | enum vga_switcheroo_handler_flags_t handler_flags) |
Seth Forshee | 36704c0 | 2012-08-17 11:17:03 -0500 | [diff] [blame] | 220 | { |
| 221 | mutex_lock(&vgasr_mutex); |
| 222 | if (vgasr_priv.handler) { |
| 223 | mutex_unlock(&vgasr_mutex); |
| 224 | return -EINVAL; |
| 225 | } |
| 226 | |
| 227 | vgasr_priv.handler = handler; |
Lukas Wunner | 156d7d4 | 2016-01-11 20:09:20 +0100 | [diff] [blame] | 228 | vgasr_priv.handler_flags = handler_flags; |
Seth Forshee | 36704c0 | 2012-08-17 11:17:03 -0500 | [diff] [blame] | 229 | if (vga_switcheroo_ready()) { |
Thierry Reding | 9b0be1e | 2015-08-12 16:32:10 +0200 | [diff] [blame] | 230 | pr_info("enabled\n"); |
Seth Forshee | 36704c0 | 2012-08-17 11:17:03 -0500 | [diff] [blame] | 231 | vga_switcheroo_enable(); |
| 232 | } |
| 233 | mutex_unlock(&vgasr_mutex); |
| 234 | return 0; |
| 235 | } |
| 236 | EXPORT_SYMBOL(vga_switcheroo_register_handler); |
| 237 | |
Lukas Wunner | a645654 | 2015-08-23 15:18:55 +0200 | [diff] [blame] | 238 | /** |
| 239 | * vga_switcheroo_unregister_handler() - unregister handler |
| 240 | * |
| 241 | * Unregister handler. Disable vga_switcheroo. |
| 242 | */ |
Seth Forshee | 36704c0 | 2012-08-17 11:17:03 -0500 | [diff] [blame] | 243 | void vga_switcheroo_unregister_handler(void) |
| 244 | { |
| 245 | mutex_lock(&vgasr_mutex); |
Lukas Wunner | e4cb81d | 2016-01-11 20:09:20 +0100 | [diff] [blame] | 246 | mutex_lock(&vgasr_priv.mux_hw_lock); |
Lukas Wunner | 156d7d4 | 2016-01-11 20:09:20 +0100 | [diff] [blame] | 247 | vgasr_priv.handler_flags = 0; |
Seth Forshee | 36704c0 | 2012-08-17 11:17:03 -0500 | [diff] [blame] | 248 | vgasr_priv.handler = NULL; |
| 249 | if (vgasr_priv.active) { |
Thierry Reding | 9b0be1e | 2015-08-12 16:32:10 +0200 | [diff] [blame] | 250 | pr_info("disabled\n"); |
Seth Forshee | 36704c0 | 2012-08-17 11:17:03 -0500 | [diff] [blame] | 251 | vga_switcheroo_debugfs_fini(&vgasr_priv); |
| 252 | vgasr_priv.active = false; |
| 253 | } |
Lukas Wunner | e4cb81d | 2016-01-11 20:09:20 +0100 | [diff] [blame] | 254 | mutex_unlock(&vgasr_priv.mux_hw_lock); |
Seth Forshee | 36704c0 | 2012-08-17 11:17:03 -0500 | [diff] [blame] | 255 | mutex_unlock(&vgasr_mutex); |
| 256 | } |
| 257 | EXPORT_SYMBOL(vga_switcheroo_unregister_handler); |
| 258 | |
Lukas Wunner | 156d7d4 | 2016-01-11 20:09:20 +0100 | [diff] [blame] | 259 | /** |
| 260 | * vga_switcheroo_handler_flags() - obtain handler flags |
| 261 | * |
| 262 | * Helper for clients to obtain the handler flags bitmask. |
| 263 | * |
| 264 | * Return: Handler flags. A value of 0 means that no handler is registered |
| 265 | * or that the handler has no special capabilities. |
| 266 | */ |
| 267 | enum vga_switcheroo_handler_flags_t vga_switcheroo_handler_flags(void) |
| 268 | { |
| 269 | return vgasr_priv.handler_flags; |
| 270 | } |
| 271 | EXPORT_SYMBOL(vga_switcheroo_handler_flags); |
| 272 | |
Takashi Iwai | 3e9e63d | 2012-04-26 14:29:48 +0200 | [diff] [blame] | 273 | static int register_client(struct pci_dev *pdev, |
| 274 | const struct vga_switcheroo_client_ops *ops, |
Lukas Wunner | fa3e967 | 2015-08-28 12:54:07 +0200 | [diff] [blame] | 275 | enum vga_switcheroo_client_id id, bool active, |
| 276 | bool driver_power_control) |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 277 | { |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 278 | struct vga_switcheroo_client *client; |
| 279 | |
| 280 | client = kzalloc(sizeof(*client), GFP_KERNEL); |
| 281 | if (!client) |
| 282 | return -ENOMEM; |
| 283 | |
| 284 | client->pwr_state = VGA_SWITCHEROO_ON; |
| 285 | client->pdev = pdev; |
Takashi Iwai | 26ec685 | 2012-05-11 07:51:17 +0200 | [diff] [blame] | 286 | client->ops = ops; |
Takashi Iwai | 3e9e63d | 2012-04-26 14:29:48 +0200 | [diff] [blame] | 287 | client->id = id; |
| 288 | client->active = active; |
Dave Airlie | 0d69704 | 2012-09-10 12:28:36 +1000 | [diff] [blame] | 289 | client->driver_power_control = driver_power_control; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 290 | |
| 291 | mutex_lock(&vgasr_mutex); |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 292 | list_add_tail(&client->list, &vgasr_priv.clients); |
Takashi Iwai | 3e9e63d | 2012-04-26 14:29:48 +0200 | [diff] [blame] | 293 | if (client_is_vga(client)) |
| 294 | vgasr_priv.registered_clients++; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 295 | |
Seth Forshee | 36704c0 | 2012-08-17 11:17:03 -0500 | [diff] [blame] | 296 | if (vga_switcheroo_ready()) { |
Thierry Reding | 9b0be1e | 2015-08-12 16:32:10 +0200 | [diff] [blame] | 297 | pr_info("enabled\n"); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 298 | vga_switcheroo_enable(); |
| 299 | } |
| 300 | mutex_unlock(&vgasr_mutex); |
| 301 | return 0; |
| 302 | } |
Takashi Iwai | 3e9e63d | 2012-04-26 14:29:48 +0200 | [diff] [blame] | 303 | |
Lukas Wunner | a645654 | 2015-08-23 15:18:55 +0200 | [diff] [blame] | 304 | /** |
| 305 | * vga_switcheroo_register_client - register vga client |
| 306 | * @pdev: client pci device |
| 307 | * @ops: client callbacks |
| 308 | * @driver_power_control: whether power state is controlled by the driver's |
Joan Jani | fa6d513 | 2017-02-28 18:59:52 +0000 | [diff] [blame] | 309 | * runtime pm |
Lukas Wunner | a645654 | 2015-08-23 15:18:55 +0200 | [diff] [blame] | 310 | * |
| 311 | * Register vga client (GPU). Enable vga_switcheroo if another GPU and a |
| 312 | * handler have already registered. The power state of the client is assumed |
Lukas Wunner | b00e533 | 2016-05-31 11:13:27 +0200 | [diff] [blame] | 313 | * to be ON. Beforehand, vga_switcheroo_client_probe_defer() shall be called |
| 314 | * to ensure that all prerequisites are met. |
Lukas Wunner | a645654 | 2015-08-23 15:18:55 +0200 | [diff] [blame] | 315 | * |
| 316 | * Return: 0 on success, -ENOMEM on memory allocation error. |
| 317 | */ |
Takashi Iwai | 3e9e63d | 2012-04-26 14:29:48 +0200 | [diff] [blame] | 318 | int vga_switcheroo_register_client(struct pci_dev *pdev, |
Dave Airlie | 0d69704 | 2012-09-10 12:28:36 +1000 | [diff] [blame] | 319 | const struct vga_switcheroo_client_ops *ops, |
| 320 | bool driver_power_control) |
Takashi Iwai | 3e9e63d | 2012-04-26 14:29:48 +0200 | [diff] [blame] | 321 | { |
Lukas Wunner | 21c5ba8 | 2015-08-28 13:30:32 +0200 | [diff] [blame] | 322 | return register_client(pdev, ops, VGA_SWITCHEROO_UNKNOWN_ID, |
Thierry Reding | 7491bfb | 2015-08-12 16:32:11 +0200 | [diff] [blame] | 323 | pdev == vga_default_device(), |
| 324 | driver_power_control); |
Takashi Iwai | 3e9e63d | 2012-04-26 14:29:48 +0200 | [diff] [blame] | 325 | } |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 326 | EXPORT_SYMBOL(vga_switcheroo_register_client); |
| 327 | |
Lukas Wunner | a645654 | 2015-08-23 15:18:55 +0200 | [diff] [blame] | 328 | /** |
| 329 | * vga_switcheroo_register_audio_client - register audio client |
| 330 | * @pdev: client pci device |
| 331 | * @ops: client callbacks |
Lukas Wunner | fa3e967 | 2015-08-28 12:54:07 +0200 | [diff] [blame] | 332 | * @id: client identifier |
Lukas Wunner | a645654 | 2015-08-23 15:18:55 +0200 | [diff] [blame] | 333 | * |
| 334 | * Register audio client (audio device on a GPU). The power state of the |
Lukas Wunner | a345918 | 2016-05-31 11:13:27 +0200 | [diff] [blame] | 335 | * client is assumed to be ON. Beforehand, vga_switcheroo_client_probe_defer() |
| 336 | * shall be called to ensure that all prerequisites are met. |
Lukas Wunner | a645654 | 2015-08-23 15:18:55 +0200 | [diff] [blame] | 337 | * |
| 338 | * Return: 0 on success, -ENOMEM on memory allocation error. |
| 339 | */ |
Takashi Iwai | 3e9e63d | 2012-04-26 14:29:48 +0200 | [diff] [blame] | 340 | int vga_switcheroo_register_audio_client(struct pci_dev *pdev, |
Joan Jani | fa6d513 | 2017-02-28 18:59:52 +0000 | [diff] [blame] | 341 | const struct vga_switcheroo_client_ops *ops, |
| 342 | enum vga_switcheroo_client_id id) |
Takashi Iwai | 3e9e63d | 2012-04-26 14:29:48 +0200 | [diff] [blame] | 343 | { |
Lukas Wunner | 21b4567 | 2015-08-27 16:43:43 +0200 | [diff] [blame] | 344 | return register_client(pdev, ops, id | ID_BIT_AUDIO, false, false); |
Takashi Iwai | 3e9e63d | 2012-04-26 14:29:48 +0200 | [diff] [blame] | 345 | } |
| 346 | EXPORT_SYMBOL(vga_switcheroo_register_audio_client); |
| 347 | |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 348 | static struct vga_switcheroo_client * |
| 349 | find_client_from_pci(struct list_head *head, struct pci_dev *pdev) |
| 350 | { |
| 351 | struct vga_switcheroo_client *client; |
Thierry Reding | 7491bfb | 2015-08-12 16:32:11 +0200 | [diff] [blame] | 352 | |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 353 | list_for_each_entry(client, head, list) |
| 354 | if (client->pdev == pdev) |
| 355 | return client; |
| 356 | return NULL; |
| 357 | } |
| 358 | |
| 359 | static struct vga_switcheroo_client * |
Lukas Wunner | fa3e967 | 2015-08-28 12:54:07 +0200 | [diff] [blame] | 360 | find_client_from_id(struct list_head *head, |
| 361 | enum vga_switcheroo_client_id client_id) |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 362 | { |
| 363 | struct vga_switcheroo_client *client; |
Thierry Reding | 7491bfb | 2015-08-12 16:32:11 +0200 | [diff] [blame] | 364 | |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 365 | list_for_each_entry(client, head, list) |
| 366 | if (client->id == client_id) |
| 367 | return client; |
| 368 | return NULL; |
| 369 | } |
| 370 | |
| 371 | static struct vga_switcheroo_client * |
| 372 | find_active_client(struct list_head *head) |
| 373 | { |
| 374 | struct vga_switcheroo_client *client; |
Thierry Reding | 7491bfb | 2015-08-12 16:32:11 +0200 | [diff] [blame] | 375 | |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 376 | list_for_each_entry(client, head, list) |
Lukas Wunner | 21b4567 | 2015-08-27 16:43:43 +0200 | [diff] [blame] | 377 | if (client->active) |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 378 | return client; |
| 379 | return NULL; |
| 380 | } |
| 381 | |
Lukas Wunner | a645654 | 2015-08-23 15:18:55 +0200 | [diff] [blame] | 382 | /** |
Lukas Wunner | b00e533 | 2016-05-31 11:13:27 +0200 | [diff] [blame] | 383 | * vga_switcheroo_client_probe_defer() - whether to defer probing a given client |
| 384 | * @pdev: client pci device |
| 385 | * |
| 386 | * Determine whether any prerequisites are not fulfilled to probe a given |
| 387 | * client. Drivers shall invoke this early on in their ->probe callback |
| 388 | * and return %-EPROBE_DEFER if it evaluates to %true. Thou shalt not |
| 389 | * register the client ere thou hast called this. |
| 390 | * |
| 391 | * Return: %true if probing should be deferred, otherwise %false. |
| 392 | */ |
| 393 | bool vga_switcheroo_client_probe_defer(struct pci_dev *pdev) |
| 394 | { |
Lukas Wunner | a345918 | 2016-05-31 11:13:27 +0200 | [diff] [blame] | 395 | if ((pdev->class >> 16) == PCI_BASE_CLASS_DISPLAY) { |
| 396 | /* |
| 397 | * apple-gmux is needed on pre-retina MacBook Pro |
| 398 | * to probe the panel if pdev is the inactive GPU. |
| 399 | */ |
| 400 | if (apple_gmux_present() && pdev != vga_default_device() && |
| 401 | !vgasr_priv.handler_flags) |
| 402 | return true; |
| 403 | } |
Lukas Wunner | b00e533 | 2016-05-31 11:13:27 +0200 | [diff] [blame] | 404 | |
| 405 | return false; |
| 406 | } |
| 407 | EXPORT_SYMBOL(vga_switcheroo_client_probe_defer); |
| 408 | |
| 409 | /** |
Lukas Wunner | a645654 | 2015-08-23 15:18:55 +0200 | [diff] [blame] | 410 | * vga_switcheroo_get_client_state() - obtain power state of a given client |
| 411 | * @pdev: client pci device |
| 412 | * |
| 413 | * Obtain power state of a given client as seen from vga_switcheroo. |
| 414 | * The function is only called from hda_intel.c. |
| 415 | * |
| 416 | * Return: Power state. |
| 417 | */ |
Lukas Wunner | 203d027 | 2015-08-28 11:56:26 +0200 | [diff] [blame] | 418 | enum vga_switcheroo_state vga_switcheroo_get_client_state(struct pci_dev *pdev) |
Takashi Iwai | c8e9cf7 | 2012-06-07 12:15:15 +0200 | [diff] [blame] | 419 | { |
| 420 | struct vga_switcheroo_client *client; |
Lukas Wunner | 8f12a31 | 2015-08-23 23:23:02 +0200 | [diff] [blame] | 421 | enum vga_switcheroo_state ret; |
Takashi Iwai | c8e9cf7 | 2012-06-07 12:15:15 +0200 | [diff] [blame] | 422 | |
Lukas Wunner | 8f12a31 | 2015-08-23 23:23:02 +0200 | [diff] [blame] | 423 | mutex_lock(&vgasr_mutex); |
Takashi Iwai | c8e9cf7 | 2012-06-07 12:15:15 +0200 | [diff] [blame] | 424 | client = find_client_from_pci(&vgasr_priv.clients, pdev); |
| 425 | if (!client) |
Lukas Wunner | 8f12a31 | 2015-08-23 23:23:02 +0200 | [diff] [blame] | 426 | ret = VGA_SWITCHEROO_NOT_FOUND; |
Lukas Wunner | 8f12a31 | 2015-08-23 23:23:02 +0200 | [diff] [blame] | 427 | else |
| 428 | ret = client->pwr_state; |
| 429 | mutex_unlock(&vgasr_mutex); |
| 430 | return ret; |
Takashi Iwai | c8e9cf7 | 2012-06-07 12:15:15 +0200 | [diff] [blame] | 431 | } |
| 432 | EXPORT_SYMBOL(vga_switcheroo_get_client_state); |
| 433 | |
Lukas Wunner | a645654 | 2015-08-23 15:18:55 +0200 | [diff] [blame] | 434 | /** |
| 435 | * vga_switcheroo_unregister_client() - unregister client |
| 436 | * @pdev: client pci device |
| 437 | * |
| 438 | * Unregister client. Disable vga_switcheroo if this is a vga client (GPU). |
| 439 | */ |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 440 | void vga_switcheroo_unregister_client(struct pci_dev *pdev) |
| 441 | { |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 442 | struct vga_switcheroo_client *client; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 443 | |
| 444 | mutex_lock(&vgasr_mutex); |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 445 | client = find_client_from_pci(&vgasr_priv.clients, pdev); |
| 446 | if (client) { |
Takashi Iwai | 3e9e63d | 2012-04-26 14:29:48 +0200 | [diff] [blame] | 447 | if (client_is_vga(client)) |
| 448 | vgasr_priv.registered_clients--; |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 449 | list_del(&client->list); |
| 450 | kfree(client); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 451 | } |
Takashi Iwai | 3e9e63d | 2012-04-26 14:29:48 +0200 | [diff] [blame] | 452 | if (vgasr_priv.active && vgasr_priv.registered_clients < 2) { |
Thierry Reding | 9b0be1e | 2015-08-12 16:32:10 +0200 | [diff] [blame] | 453 | pr_info("disabled\n"); |
Takashi Iwai | 3e9e63d | 2012-04-26 14:29:48 +0200 | [diff] [blame] | 454 | vga_switcheroo_debugfs_fini(&vgasr_priv); |
| 455 | vgasr_priv.active = false; |
| 456 | } |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 457 | mutex_unlock(&vgasr_mutex); |
| 458 | } |
| 459 | EXPORT_SYMBOL(vga_switcheroo_unregister_client); |
| 460 | |
Lukas Wunner | a645654 | 2015-08-23 15:18:55 +0200 | [diff] [blame] | 461 | /** |
| 462 | * vga_switcheroo_client_fb_set() - set framebuffer of a given client |
| 463 | * @pdev: client pci device |
| 464 | * @info: framebuffer |
| 465 | * |
| 466 | * Set framebuffer of a given client. The console will be remapped to this |
| 467 | * on switching. |
| 468 | */ |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 469 | void vga_switcheroo_client_fb_set(struct pci_dev *pdev, |
| 470 | struct fb_info *info) |
| 471 | { |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 472 | struct vga_switcheroo_client *client; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 473 | |
| 474 | mutex_lock(&vgasr_mutex); |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 475 | client = find_client_from_pci(&vgasr_priv.clients, pdev); |
| 476 | if (client) |
| 477 | client->fb_info = info; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 478 | mutex_unlock(&vgasr_mutex); |
| 479 | } |
| 480 | EXPORT_SYMBOL(vga_switcheroo_client_fb_set); |
| 481 | |
Lukas Wunner | a645654 | 2015-08-23 15:18:55 +0200 | [diff] [blame] | 482 | /** |
Lukas Wunner | e4cb81d | 2016-01-11 20:09:20 +0100 | [diff] [blame] | 483 | * vga_switcheroo_lock_ddc() - temporarily switch DDC lines to a given client |
| 484 | * @pdev: client pci device |
| 485 | * |
| 486 | * Temporarily switch DDC lines to the client identified by @pdev |
| 487 | * (but leave the outputs otherwise switched to where they are). |
| 488 | * This allows the inactive client to probe EDID. The DDC lines must |
| 489 | * afterwards be switched back by calling vga_switcheroo_unlock_ddc(), |
| 490 | * even if this function returns an error. |
| 491 | * |
| 492 | * Return: Previous DDC owner on success or a negative int on error. |
| 493 | * Specifically, %-ENODEV if no handler has registered or if the handler |
| 494 | * does not support switching the DDC lines. Also, a negative value |
| 495 | * returned by the handler is propagated back to the caller. |
| 496 | * The return value has merely an informational purpose for any caller |
| 497 | * which might be interested in it. It is acceptable to ignore the return |
| 498 | * value and simply rely on the result of the subsequent EDID probe, |
| 499 | * which will be %NULL if DDC switching failed. |
| 500 | */ |
| 501 | int vga_switcheroo_lock_ddc(struct pci_dev *pdev) |
| 502 | { |
| 503 | enum vga_switcheroo_client_id id; |
| 504 | |
| 505 | mutex_lock(&vgasr_priv.mux_hw_lock); |
| 506 | if (!vgasr_priv.handler || !vgasr_priv.handler->switch_ddc) { |
| 507 | vgasr_priv.old_ddc_owner = -ENODEV; |
| 508 | return -ENODEV; |
| 509 | } |
| 510 | |
| 511 | id = vgasr_priv.handler->get_client_id(pdev); |
| 512 | vgasr_priv.old_ddc_owner = vgasr_priv.handler->switch_ddc(id); |
| 513 | return vgasr_priv.old_ddc_owner; |
| 514 | } |
| 515 | EXPORT_SYMBOL(vga_switcheroo_lock_ddc); |
| 516 | |
| 517 | /** |
| 518 | * vga_switcheroo_unlock_ddc() - switch DDC lines back to previous owner |
| 519 | * @pdev: client pci device |
| 520 | * |
| 521 | * Switch DDC lines back to the previous owner after calling |
| 522 | * vga_switcheroo_lock_ddc(). This must be called even if |
| 523 | * vga_switcheroo_lock_ddc() returned an error. |
| 524 | * |
| 525 | * Return: Previous DDC owner on success (i.e. the client identifier of @pdev) |
| 526 | * or a negative int on error. |
| 527 | * Specifically, %-ENODEV if no handler has registered or if the handler |
| 528 | * does not support switching the DDC lines. Also, a negative value |
| 529 | * returned by the handler is propagated back to the caller. |
| 530 | * Finally, invoking this function without calling vga_switcheroo_lock_ddc() |
| 531 | * first is not allowed and will result in %-EINVAL. |
| 532 | */ |
| 533 | int vga_switcheroo_unlock_ddc(struct pci_dev *pdev) |
| 534 | { |
| 535 | enum vga_switcheroo_client_id id; |
| 536 | int ret = vgasr_priv.old_ddc_owner; |
| 537 | |
| 538 | if (WARN_ON_ONCE(!mutex_is_locked(&vgasr_priv.mux_hw_lock))) |
| 539 | return -EINVAL; |
| 540 | |
| 541 | if (vgasr_priv.old_ddc_owner >= 0) { |
| 542 | id = vgasr_priv.handler->get_client_id(pdev); |
| 543 | if (vgasr_priv.old_ddc_owner != id) |
| 544 | ret = vgasr_priv.handler->switch_ddc( |
| 545 | vgasr_priv.old_ddc_owner); |
| 546 | } |
| 547 | mutex_unlock(&vgasr_priv.mux_hw_lock); |
| 548 | return ret; |
| 549 | } |
| 550 | EXPORT_SYMBOL(vga_switcheroo_unlock_ddc); |
| 551 | |
| 552 | /** |
Lukas Wunner | a645654 | 2015-08-23 15:18:55 +0200 | [diff] [blame] | 553 | * DOC: Manual switching and manual power control |
| 554 | * |
| 555 | * In this mode of use, the file /sys/kernel/debug/vgaswitcheroo/switch |
| 556 | * can be read to retrieve the current vga_switcheroo state and commands |
| 557 | * can be written to it to change the state. The file appears as soon as |
| 558 | * two GPU drivers and one handler have registered with vga_switcheroo. |
| 559 | * The following commands are understood: |
| 560 | * |
| 561 | * * OFF: Power off the device not in use. |
| 562 | * * ON: Power on the device not in use. |
| 563 | * * IGD: Switch to the integrated graphics device. |
Lukas Wunner | fb001df | 2016-07-11 10:22:33 +0200 | [diff] [blame] | 564 | * Power on the integrated GPU if necessary, power off the discrete GPU. |
| 565 | * Prerequisite is that no user space processes (e.g. Xorg, alsactl) |
| 566 | * have opened device files of the GPUs or the audio client. If the |
| 567 | * switch fails, the user may invoke lsof(8) or fuser(1) on /dev/dri/ |
| 568 | * and /dev/snd/controlC1 to identify processes blocking the switch. |
Lukas Wunner | a645654 | 2015-08-23 15:18:55 +0200 | [diff] [blame] | 569 | * * DIS: Switch to the discrete graphics device. |
| 570 | * * DIGD: Delayed switch to the integrated graphics device. |
Lukas Wunner | fb001df | 2016-07-11 10:22:33 +0200 | [diff] [blame] | 571 | * This will perform the switch once the last user space process has |
| 572 | * closed the device files of the GPUs and the audio client. |
Lukas Wunner | a645654 | 2015-08-23 15:18:55 +0200 | [diff] [blame] | 573 | * * DDIS: Delayed switch to the discrete graphics device. |
| 574 | * * MIGD: Mux-only switch to the integrated graphics device. |
Lukas Wunner | fb001df | 2016-07-11 10:22:33 +0200 | [diff] [blame] | 575 | * Does not remap console or change the power state of either gpu. |
| 576 | * If the integrated GPU is currently off, the screen will turn black. |
| 577 | * If it is on, the screen will show whatever happens to be in VRAM. |
| 578 | * Either way, the user has to blindly enter the command to switch back. |
Lukas Wunner | a645654 | 2015-08-23 15:18:55 +0200 | [diff] [blame] | 579 | * * MDIS: Mux-only switch to the discrete graphics device. |
| 580 | * |
| 581 | * For GPUs whose power state is controlled by the driver's runtime pm, |
| 582 | * the ON and OFF commands are a no-op (see next section). |
| 583 | * |
| 584 | * For muxless machines, the IGD/DIS, DIGD/DDIS and MIGD/MDIS commands |
| 585 | * should not be used. |
| 586 | */ |
| 587 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 588 | static int vga_switcheroo_show(struct seq_file *m, void *v) |
| 589 | { |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 590 | struct vga_switcheroo_client *client; |
| 591 | int i = 0; |
Thierry Reding | 7491bfb | 2015-08-12 16:32:11 +0200 | [diff] [blame] | 592 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 593 | mutex_lock(&vgasr_mutex); |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 594 | list_for_each_entry(client, &vgasr_priv.clients, list) { |
Dave Airlie | 0d69704 | 2012-09-10 12:28:36 +1000 | [diff] [blame] | 595 | seq_printf(m, "%d:%s%s:%c:%s%s:%s\n", i, |
Thierry Reding | 7491bfb | 2015-08-12 16:32:11 +0200 | [diff] [blame] | 596 | client_id(client) == VGA_SWITCHEROO_DIS ? "DIS" : |
| 597 | "IGD", |
Takashi Iwai | 3e9e63d | 2012-04-26 14:29:48 +0200 | [diff] [blame] | 598 | client_is_vga(client) ? "" : "-Audio", |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 599 | client->active ? '+' : ' ', |
Dave Airlie | 0d69704 | 2012-09-10 12:28:36 +1000 | [diff] [blame] | 600 | client->driver_power_control ? "Dyn" : "", |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 601 | client->pwr_state ? "Pwr" : "Off", |
| 602 | pci_name(client->pdev)); |
| 603 | i++; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 604 | } |
| 605 | mutex_unlock(&vgasr_mutex); |
| 606 | return 0; |
| 607 | } |
| 608 | |
| 609 | static int vga_switcheroo_debugfs_open(struct inode *inode, struct file *file) |
| 610 | { |
| 611 | return single_open(file, vga_switcheroo_show, NULL); |
| 612 | } |
| 613 | |
| 614 | static int vga_switchon(struct vga_switcheroo_client *client) |
| 615 | { |
Dave Airlie | 0d69704 | 2012-09-10 12:28:36 +1000 | [diff] [blame] | 616 | if (client->driver_power_control) |
| 617 | return 0; |
Dave Airlie | 5cfb3c3 | 2010-12-06 12:31:50 +1000 | [diff] [blame] | 618 | if (vgasr_priv.handler->power_state) |
| 619 | vgasr_priv.handler->power_state(client->id, VGA_SWITCHEROO_ON); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 620 | /* call the driver callback to turn on device */ |
Takashi Iwai | 26ec685 | 2012-05-11 07:51:17 +0200 | [diff] [blame] | 621 | client->ops->set_gpu_state(client->pdev, VGA_SWITCHEROO_ON); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 622 | client->pwr_state = VGA_SWITCHEROO_ON; |
| 623 | return 0; |
| 624 | } |
| 625 | |
| 626 | static int vga_switchoff(struct vga_switcheroo_client *client) |
| 627 | { |
Dave Airlie | 0d69704 | 2012-09-10 12:28:36 +1000 | [diff] [blame] | 628 | if (client->driver_power_control) |
| 629 | return 0; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 630 | /* call the driver callback to turn off device */ |
Takashi Iwai | 26ec685 | 2012-05-11 07:51:17 +0200 | [diff] [blame] | 631 | client->ops->set_gpu_state(client->pdev, VGA_SWITCHEROO_OFF); |
Dave Airlie | 5cfb3c3 | 2010-12-06 12:31:50 +1000 | [diff] [blame] | 632 | if (vgasr_priv.handler->power_state) |
| 633 | vgasr_priv.handler->power_state(client->id, VGA_SWITCHEROO_OFF); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 634 | client->pwr_state = VGA_SWITCHEROO_OFF; |
| 635 | return 0; |
| 636 | } |
| 637 | |
Lukas Wunner | fa3e967 | 2015-08-28 12:54:07 +0200 | [diff] [blame] | 638 | static void set_audio_state(enum vga_switcheroo_client_id id, |
| 639 | enum vga_switcheroo_state state) |
Takashi Iwai | 3e9e63d | 2012-04-26 14:29:48 +0200 | [diff] [blame] | 640 | { |
| 641 | struct vga_switcheroo_client *client; |
| 642 | |
| 643 | client = find_client_from_id(&vgasr_priv.clients, id | ID_BIT_AUDIO); |
| 644 | if (client && client->pwr_state != state) { |
| 645 | client->ops->set_gpu_state(client->pdev, state); |
| 646 | client->pwr_state = state; |
| 647 | } |
| 648 | } |
| 649 | |
Dave Airlie | 66b37c6 | 2010-12-07 14:24:25 +1000 | [diff] [blame] | 650 | /* stage one happens before delay */ |
| 651 | static int vga_switchto_stage1(struct vga_switcheroo_client *new_client) |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 652 | { |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 653 | struct vga_switcheroo_client *active; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 654 | |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 655 | active = find_active_client(&vgasr_priv.clients); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 656 | if (!active) |
| 657 | return 0; |
| 658 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 659 | if (new_client->pwr_state == VGA_SWITCHEROO_OFF) |
| 660 | vga_switchon(new_client); |
| 661 | |
Matthew Garrett | 2fbe8c7 | 2012-04-16 16:26:03 -0400 | [diff] [blame] | 662 | vga_set_default_device(new_client->pdev); |
Dave Airlie | 66b37c6 | 2010-12-07 14:24:25 +1000 | [diff] [blame] | 663 | return 0; |
| 664 | } |
| 665 | |
| 666 | /* post delay */ |
| 667 | static int vga_switchto_stage2(struct vga_switcheroo_client *new_client) |
| 668 | { |
| 669 | int ret; |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 670 | struct vga_switcheroo_client *active; |
Dave Airlie | 66b37c6 | 2010-12-07 14:24:25 +1000 | [diff] [blame] | 671 | |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 672 | active = find_active_client(&vgasr_priv.clients); |
Dave Airlie | 66b37c6 | 2010-12-07 14:24:25 +1000 | [diff] [blame] | 673 | if (!active) |
| 674 | return 0; |
| 675 | |
| 676 | active->active = false; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 677 | |
Takashi Iwai | c91c3fa | 2012-06-09 08:46:42 +0200 | [diff] [blame] | 678 | set_audio_state(active->id, VGA_SWITCHEROO_OFF); |
| 679 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 680 | if (new_client->fb_info) { |
| 681 | struct fb_event event; |
Thierry Reding | 7491bfb | 2015-08-12 16:32:11 +0200 | [diff] [blame] | 682 | |
Dave Airlie | 054430e | 2013-01-25 11:38:56 +1000 | [diff] [blame] | 683 | console_lock(); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 684 | event.info = new_client->fb_info; |
| 685 | fb_notifier_call_chain(FB_EVENT_REMAP_ALL_CONSOLE, &event); |
Dave Airlie | 054430e | 2013-01-25 11:38:56 +1000 | [diff] [blame] | 686 | console_unlock(); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 687 | } |
| 688 | |
Lukas Wunner | e4cb81d | 2016-01-11 20:09:20 +0100 | [diff] [blame] | 689 | mutex_lock(&vgasr_priv.mux_hw_lock); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 690 | ret = vgasr_priv.handler->switchto(new_client->id); |
Lukas Wunner | e4cb81d | 2016-01-11 20:09:20 +0100 | [diff] [blame] | 691 | mutex_unlock(&vgasr_priv.mux_hw_lock); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 692 | if (ret) |
| 693 | return ret; |
| 694 | |
Takashi Iwai | 26ec685 | 2012-05-11 07:51:17 +0200 | [diff] [blame] | 695 | if (new_client->ops->reprobe) |
| 696 | new_client->ops->reprobe(new_client->pdev); |
Dave Airlie | 8d608aa | 2010-12-07 08:57:57 +1000 | [diff] [blame] | 697 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 698 | if (active->pwr_state == VGA_SWITCHEROO_ON) |
| 699 | vga_switchoff(active); |
| 700 | |
Takashi Iwai | c91c3fa | 2012-06-09 08:46:42 +0200 | [diff] [blame] | 701 | set_audio_state(new_client->id, VGA_SWITCHEROO_ON); |
| 702 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 703 | new_client->active = true; |
| 704 | return 0; |
| 705 | } |
| 706 | |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 707 | static bool check_can_switch(void) |
| 708 | { |
| 709 | struct vga_switcheroo_client *client; |
| 710 | |
| 711 | list_for_each_entry(client, &vgasr_priv.clients, list) { |
Takashi Iwai | 26ec685 | 2012-05-11 07:51:17 +0200 | [diff] [blame] | 712 | if (!client->ops->can_switch(client->pdev)) { |
Thierry Reding | 9b0be1e | 2015-08-12 16:32:10 +0200 | [diff] [blame] | 713 | pr_err("client %x refused switch\n", client->id); |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 714 | return false; |
| 715 | } |
| 716 | } |
| 717 | return true; |
| 718 | } |
| 719 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 720 | static ssize_t |
| 721 | vga_switcheroo_debugfs_write(struct file *filp, const char __user *ubuf, |
| 722 | size_t cnt, loff_t *ppos) |
| 723 | { |
| 724 | char usercmd[64]; |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 725 | int ret; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 726 | bool delay = false, can_switch; |
Dave Airlie | 851ab95 | 2010-12-06 12:35:52 +1000 | [diff] [blame] | 727 | bool just_mux = false; |
Lukas Wunner | fa3e967 | 2015-08-28 12:54:07 +0200 | [diff] [blame] | 728 | enum vga_switcheroo_client_id client_id = VGA_SWITCHEROO_UNKNOWN_ID; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 729 | struct vga_switcheroo_client *client = NULL; |
| 730 | |
| 731 | if (cnt > 63) |
| 732 | cnt = 63; |
| 733 | |
| 734 | if (copy_from_user(usercmd, ubuf, cnt)) |
| 735 | return -EFAULT; |
| 736 | |
| 737 | mutex_lock(&vgasr_mutex); |
| 738 | |
Jiri Slaby | 8c88e50 | 2010-04-27 14:11:03 -0700 | [diff] [blame] | 739 | if (!vgasr_priv.active) { |
| 740 | cnt = -EINVAL; |
| 741 | goto out; |
| 742 | } |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 743 | |
| 744 | /* pwr off the device not in use */ |
| 745 | if (strncmp(usercmd, "OFF", 3) == 0) { |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 746 | list_for_each_entry(client, &vgasr_priv.clients, list) { |
Takashi Iwai | c91c3fa | 2012-06-09 08:46:42 +0200 | [diff] [blame] | 747 | if (client->active || client_is_audio(client)) |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 748 | continue; |
Dave Airlie | 0d69704 | 2012-09-10 12:28:36 +1000 | [diff] [blame] | 749 | if (client->driver_power_control) |
| 750 | continue; |
Takashi Iwai | c91c3fa | 2012-06-09 08:46:42 +0200 | [diff] [blame] | 751 | set_audio_state(client->id, VGA_SWITCHEROO_OFF); |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 752 | if (client->pwr_state == VGA_SWITCHEROO_ON) |
| 753 | vga_switchoff(client); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 754 | } |
| 755 | goto out; |
| 756 | } |
| 757 | /* pwr on the device not in use */ |
| 758 | if (strncmp(usercmd, "ON", 2) == 0) { |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 759 | list_for_each_entry(client, &vgasr_priv.clients, list) { |
Takashi Iwai | c91c3fa | 2012-06-09 08:46:42 +0200 | [diff] [blame] | 760 | if (client->active || client_is_audio(client)) |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 761 | continue; |
Dave Airlie | 0d69704 | 2012-09-10 12:28:36 +1000 | [diff] [blame] | 762 | if (client->driver_power_control) |
| 763 | continue; |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 764 | if (client->pwr_state == VGA_SWITCHEROO_OFF) |
| 765 | vga_switchon(client); |
Takashi Iwai | c91c3fa | 2012-06-09 08:46:42 +0200 | [diff] [blame] | 766 | set_audio_state(client->id, VGA_SWITCHEROO_ON); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 767 | } |
| 768 | goto out; |
| 769 | } |
| 770 | |
| 771 | /* request a delayed switch - test can we switch now */ |
| 772 | if (strncmp(usercmd, "DIGD", 4) == 0) { |
| 773 | client_id = VGA_SWITCHEROO_IGD; |
| 774 | delay = true; |
| 775 | } |
| 776 | |
| 777 | if (strncmp(usercmd, "DDIS", 4) == 0) { |
| 778 | client_id = VGA_SWITCHEROO_DIS; |
| 779 | delay = true; |
| 780 | } |
| 781 | |
| 782 | if (strncmp(usercmd, "IGD", 3) == 0) |
| 783 | client_id = VGA_SWITCHEROO_IGD; |
| 784 | |
| 785 | if (strncmp(usercmd, "DIS", 3) == 0) |
| 786 | client_id = VGA_SWITCHEROO_DIS; |
| 787 | |
Dan Carpenter | fea6f33 | 2011-01-07 08:12:27 +0300 | [diff] [blame] | 788 | if (strncmp(usercmd, "MIGD", 4) == 0) { |
Dave Airlie | 851ab95 | 2010-12-06 12:35:52 +1000 | [diff] [blame] | 789 | just_mux = true; |
| 790 | client_id = VGA_SWITCHEROO_IGD; |
| 791 | } |
Dan Carpenter | fea6f33 | 2011-01-07 08:12:27 +0300 | [diff] [blame] | 792 | if (strncmp(usercmd, "MDIS", 4) == 0) { |
Dave Airlie | 851ab95 | 2010-12-06 12:35:52 +1000 | [diff] [blame] | 793 | just_mux = true; |
| 794 | client_id = VGA_SWITCHEROO_DIS; |
| 795 | } |
| 796 | |
Lukas Wunner | 21c5ba8 | 2015-08-28 13:30:32 +0200 | [diff] [blame] | 797 | if (client_id == VGA_SWITCHEROO_UNKNOWN_ID) |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 798 | goto out; |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 799 | client = find_client_from_id(&vgasr_priv.clients, client_id); |
| 800 | if (!client) |
| 801 | goto out; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 802 | |
| 803 | vgasr_priv.delayed_switch_active = false; |
Dave Airlie | 851ab95 | 2010-12-06 12:35:52 +1000 | [diff] [blame] | 804 | |
| 805 | if (just_mux) { |
Lukas Wunner | e4cb81d | 2016-01-11 20:09:20 +0100 | [diff] [blame] | 806 | mutex_lock(&vgasr_priv.mux_hw_lock); |
Dave Airlie | 851ab95 | 2010-12-06 12:35:52 +1000 | [diff] [blame] | 807 | ret = vgasr_priv.handler->switchto(client_id); |
Lukas Wunner | e4cb81d | 2016-01-11 20:09:20 +0100 | [diff] [blame] | 808 | mutex_unlock(&vgasr_priv.mux_hw_lock); |
Dave Airlie | 851ab95 | 2010-12-06 12:35:52 +1000 | [diff] [blame] | 809 | goto out; |
| 810 | } |
| 811 | |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 812 | if (client->active) |
Florian Mickler | a67b888 | 2011-05-15 16:32:50 +0200 | [diff] [blame] | 813 | goto out; |
| 814 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 815 | /* okay we want a switch - test if devices are willing to switch */ |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 816 | can_switch = check_can_switch(); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 817 | |
| 818 | if (can_switch == false && delay == false) |
| 819 | goto out; |
| 820 | |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 821 | if (can_switch) { |
Dave Airlie | 66b37c6 | 2010-12-07 14:24:25 +1000 | [diff] [blame] | 822 | ret = vga_switchto_stage1(client); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 823 | if (ret) |
Thierry Reding | 9b0be1e | 2015-08-12 16:32:10 +0200 | [diff] [blame] | 824 | pr_err("switching failed stage 1 %d\n", ret); |
Dave Airlie | 66b37c6 | 2010-12-07 14:24:25 +1000 | [diff] [blame] | 825 | |
| 826 | ret = vga_switchto_stage2(client); |
| 827 | if (ret) |
Thierry Reding | 9b0be1e | 2015-08-12 16:32:10 +0200 | [diff] [blame] | 828 | pr_err("switching failed stage 2 %d\n", ret); |
Dave Airlie | 66b37c6 | 2010-12-07 14:24:25 +1000 | [diff] [blame] | 829 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 830 | } else { |
Thierry Reding | 9b0be1e | 2015-08-12 16:32:10 +0200 | [diff] [blame] | 831 | pr_info("setting delayed switch to client %d\n", client->id); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 832 | vgasr_priv.delayed_switch_active = true; |
| 833 | vgasr_priv.delayed_client_id = client_id; |
| 834 | |
Dave Airlie | 66b37c6 | 2010-12-07 14:24:25 +1000 | [diff] [blame] | 835 | ret = vga_switchto_stage1(client); |
| 836 | if (ret) |
Thierry Reding | 9b0be1e | 2015-08-12 16:32:10 +0200 | [diff] [blame] | 837 | pr_err("delayed switching stage 1 failed %d\n", ret); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 838 | } |
| 839 | |
| 840 | out: |
| 841 | mutex_unlock(&vgasr_mutex); |
| 842 | return cnt; |
| 843 | } |
| 844 | |
| 845 | static const struct file_operations vga_switcheroo_debugfs_fops = { |
| 846 | .owner = THIS_MODULE, |
| 847 | .open = vga_switcheroo_debugfs_open, |
| 848 | .write = vga_switcheroo_debugfs_write, |
| 849 | .read = seq_read, |
| 850 | .llseek = seq_lseek, |
| 851 | .release = single_release, |
| 852 | }; |
| 853 | |
| 854 | static void vga_switcheroo_debugfs_fini(struct vgasr_priv *priv) |
| 855 | { |
Thierry Reding | 798ae0f | 2015-08-12 16:32:12 +0200 | [diff] [blame] | 856 | debugfs_remove(priv->switch_file); |
| 857 | priv->switch_file = NULL; |
| 858 | |
| 859 | debugfs_remove(priv->debugfs_root); |
| 860 | priv->debugfs_root = NULL; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | static int vga_switcheroo_debugfs_init(struct vgasr_priv *priv) |
| 864 | { |
Thierry Reding | 9b0be1e | 2015-08-12 16:32:10 +0200 | [diff] [blame] | 865 | static const char mp[] = "/sys/kernel/debug"; |
| 866 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 867 | /* already initialised */ |
| 868 | if (priv->debugfs_root) |
| 869 | return 0; |
| 870 | priv->debugfs_root = debugfs_create_dir("vgaswitcheroo", NULL); |
| 871 | |
| 872 | if (!priv->debugfs_root) { |
Thierry Reding | 9b0be1e | 2015-08-12 16:32:10 +0200 | [diff] [blame] | 873 | pr_err("Cannot create %s/vgaswitcheroo\n", mp); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 874 | goto fail; |
| 875 | } |
| 876 | |
| 877 | priv->switch_file = debugfs_create_file("switch", 0644, |
Thierry Reding | 7491bfb | 2015-08-12 16:32:11 +0200 | [diff] [blame] | 878 | priv->debugfs_root, NULL, |
| 879 | &vga_switcheroo_debugfs_fops); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 880 | if (!priv->switch_file) { |
Thierry Reding | 9b0be1e | 2015-08-12 16:32:10 +0200 | [diff] [blame] | 881 | pr_err("cannot create %s/vgaswitcheroo/switch\n", mp); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 882 | goto fail; |
| 883 | } |
| 884 | return 0; |
| 885 | fail: |
| 886 | vga_switcheroo_debugfs_fini(priv); |
| 887 | return -1; |
| 888 | } |
| 889 | |
Lukas Wunner | a645654 | 2015-08-23 15:18:55 +0200 | [diff] [blame] | 890 | /** |
| 891 | * vga_switcheroo_process_delayed_switch() - helper for delayed switching |
| 892 | * |
| 893 | * Process a delayed switch if one is pending. DRM drivers should call this |
| 894 | * from their ->lastclose callback. |
| 895 | * |
| 896 | * Return: 0 on success. -EINVAL if no delayed switch is pending, if the client |
| 897 | * has unregistered in the meantime or if there are other clients blocking the |
| 898 | * switch. If the actual switch fails, an error is reported and 0 is returned. |
| 899 | */ |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 900 | int vga_switcheroo_process_delayed_switch(void) |
| 901 | { |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 902 | struct vga_switcheroo_client *client; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 903 | int ret; |
| 904 | int err = -EINVAL; |
| 905 | |
| 906 | mutex_lock(&vgasr_mutex); |
| 907 | if (!vgasr_priv.delayed_switch_active) |
| 908 | goto err; |
| 909 | |
Thierry Reding | 9b0be1e | 2015-08-12 16:32:10 +0200 | [diff] [blame] | 910 | pr_info("processing delayed switch to %d\n", |
| 911 | vgasr_priv.delayed_client_id); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 912 | |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 913 | client = find_client_from_id(&vgasr_priv.clients, |
| 914 | vgasr_priv.delayed_client_id); |
| 915 | if (!client || !check_can_switch()) |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 916 | goto err; |
| 917 | |
Dave Airlie | 66b37c6 | 2010-12-07 14:24:25 +1000 | [diff] [blame] | 918 | ret = vga_switchto_stage2(client); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 919 | if (ret) |
Thierry Reding | 9b0be1e | 2015-08-12 16:32:10 +0200 | [diff] [blame] | 920 | pr_err("delayed switching failed stage 2 %d\n", ret); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 921 | |
| 922 | vgasr_priv.delayed_switch_active = false; |
| 923 | err = 0; |
| 924 | err: |
| 925 | mutex_unlock(&vgasr_mutex); |
| 926 | return err; |
| 927 | } |
| 928 | EXPORT_SYMBOL(vga_switcheroo_process_delayed_switch); |
Dave Airlie | 0d69704 | 2012-09-10 12:28:36 +1000 | [diff] [blame] | 929 | |
Lukas Wunner | a645654 | 2015-08-23 15:18:55 +0200 | [diff] [blame] | 930 | /** |
| 931 | * DOC: Driver power control |
| 932 | * |
| 933 | * In this mode of use, the discrete GPU automatically powers up and down at |
| 934 | * the discretion of the driver's runtime pm. On muxed machines, the user may |
| 935 | * still influence the muxer state by way of the debugfs interface, however |
| 936 | * the ON and OFF commands become a no-op for the discrete GPU. |
| 937 | * |
| 938 | * This mode is the default on Nvidia HybridPower/Optimus and ATI PowerXpress. |
| 939 | * Specifying nouveau.runpm=0, radeon.runpm=0 or amdgpu.runpm=0 on the kernel |
| 940 | * command line disables it. |
| 941 | * |
| 942 | * When the driver decides to power up or down, it notifies vga_switcheroo |
| 943 | * thereof so that it can (a) power the audio device on the GPU up or down, |
| 944 | * and (b) update its internal power state representation for the device. |
| 945 | * This is achieved by vga_switcheroo_set_dynamic_switch(). |
| 946 | * |
| 947 | * After the GPU has been suspended, the handler needs to be called to cut |
| 948 | * power to the GPU. Likewise it needs to reinstate power before the GPU |
| 949 | * can resume. This is achieved by vga_switcheroo_init_domain_pm_ops(), |
| 950 | * which augments the GPU's suspend/resume functions by the requisite |
| 951 | * calls to the handler. |
| 952 | * |
| 953 | * When the audio device resumes, the GPU needs to be woken. This is achieved |
| 954 | * by vga_switcheroo_init_domain_pm_optimus_hdmi_audio(), which augments the |
| 955 | * audio device's resume function. |
| 956 | * |
| 957 | * On muxed machines, if the mux is initially switched to the discrete GPU, |
| 958 | * the user ends up with a black screen when the GPU powers down after boot. |
| 959 | * As a workaround, the mux is forced to the integrated GPU on runtime suspend, |
| 960 | * cf. https://bugs.freedesktop.org/show_bug.cgi?id=75917 |
| 961 | */ |
| 962 | |
Thierry Reding | 7491bfb | 2015-08-12 16:32:11 +0200 | [diff] [blame] | 963 | static void vga_switcheroo_power_switch(struct pci_dev *pdev, |
| 964 | enum vga_switcheroo_state state) |
Dave Airlie | 0d69704 | 2012-09-10 12:28:36 +1000 | [diff] [blame] | 965 | { |
| 966 | struct vga_switcheroo_client *client; |
| 967 | |
| 968 | if (!vgasr_priv.handler->power_state) |
| 969 | return; |
| 970 | |
| 971 | client = find_client_from_pci(&vgasr_priv.clients, pdev); |
| 972 | if (!client) |
| 973 | return; |
| 974 | |
| 975 | if (!client->driver_power_control) |
| 976 | return; |
| 977 | |
| 978 | vgasr_priv.handler->power_state(client->id, state); |
| 979 | } |
| 980 | |
Lukas Wunner | a645654 | 2015-08-23 15:18:55 +0200 | [diff] [blame] | 981 | /** |
| 982 | * vga_switcheroo_set_dynamic_switch() - helper for driver power control |
| 983 | * @pdev: client pci device |
| 984 | * @dynamic: new power state |
| 985 | * |
| 986 | * Helper for GPUs whose power state is controlled by the driver's runtime pm. |
| 987 | * When the driver decides to power up or down, it notifies vga_switcheroo |
| 988 | * thereof using this helper so that it can (a) power the audio device on |
| 989 | * the GPU up or down, and (b) update its internal power state representation |
| 990 | * for the device. |
| 991 | */ |
Thierry Reding | 7491bfb | 2015-08-12 16:32:11 +0200 | [diff] [blame] | 992 | void vga_switcheroo_set_dynamic_switch(struct pci_dev *pdev, |
| 993 | enum vga_switcheroo_state dynamic) |
Dave Airlie | 0d69704 | 2012-09-10 12:28:36 +1000 | [diff] [blame] | 994 | { |
| 995 | struct vga_switcheroo_client *client; |
| 996 | |
Lukas Wunner | 8f12a31 | 2015-08-23 23:23:02 +0200 | [diff] [blame] | 997 | mutex_lock(&vgasr_mutex); |
Dave Airlie | 0d69704 | 2012-09-10 12:28:36 +1000 | [diff] [blame] | 998 | client = find_client_from_pci(&vgasr_priv.clients, pdev); |
Lukas Wunner | 8f12a31 | 2015-08-23 23:23:02 +0200 | [diff] [blame] | 999 | if (!client || !client->driver_power_control) { |
| 1000 | mutex_unlock(&vgasr_mutex); |
Dave Airlie | 0d69704 | 2012-09-10 12:28:36 +1000 | [diff] [blame] | 1001 | return; |
Lukas Wunner | 8f12a31 | 2015-08-23 23:23:02 +0200 | [diff] [blame] | 1002 | } |
Dave Airlie | 0d69704 | 2012-09-10 12:28:36 +1000 | [diff] [blame] | 1003 | |
| 1004 | client->pwr_state = dynamic; |
| 1005 | set_audio_state(client->id, dynamic); |
Lukas Wunner | 8f12a31 | 2015-08-23 23:23:02 +0200 | [diff] [blame] | 1006 | mutex_unlock(&vgasr_mutex); |
Dave Airlie | 0d69704 | 2012-09-10 12:28:36 +1000 | [diff] [blame] | 1007 | } |
| 1008 | EXPORT_SYMBOL(vga_switcheroo_set_dynamic_switch); |
| 1009 | |
| 1010 | /* switcheroo power domain */ |
| 1011 | static int vga_switcheroo_runtime_suspend(struct device *dev) |
| 1012 | { |
| 1013 | struct pci_dev *pdev = to_pci_dev(dev); |
| 1014 | int ret; |
| 1015 | |
| 1016 | ret = dev->bus->pm->runtime_suspend(dev); |
| 1017 | if (ret) |
| 1018 | return ret; |
Lukas Wunner | 8f12a31 | 2015-08-23 23:23:02 +0200 | [diff] [blame] | 1019 | mutex_lock(&vgasr_mutex); |
Lukas Wunner | e4cb81d | 2016-01-11 20:09:20 +0100 | [diff] [blame] | 1020 | if (vgasr_priv.handler->switchto) { |
| 1021 | mutex_lock(&vgasr_priv.mux_hw_lock); |
Alex Deucher | f2bc56161 | 2014-05-19 14:04:43 -0400 | [diff] [blame] | 1022 | vgasr_priv.handler->switchto(VGA_SWITCHEROO_IGD); |
Lukas Wunner | e4cb81d | 2016-01-11 20:09:20 +0100 | [diff] [blame] | 1023 | mutex_unlock(&vgasr_priv.mux_hw_lock); |
| 1024 | } |
Dave Airlie | 0d69704 | 2012-09-10 12:28:36 +1000 | [diff] [blame] | 1025 | vga_switcheroo_power_switch(pdev, VGA_SWITCHEROO_OFF); |
Lukas Wunner | 8f12a31 | 2015-08-23 23:23:02 +0200 | [diff] [blame] | 1026 | mutex_unlock(&vgasr_mutex); |
Dave Airlie | 0d69704 | 2012-09-10 12:28:36 +1000 | [diff] [blame] | 1027 | return 0; |
| 1028 | } |
| 1029 | |
| 1030 | static int vga_switcheroo_runtime_resume(struct device *dev) |
| 1031 | { |
| 1032 | struct pci_dev *pdev = to_pci_dev(dev); |
| 1033 | int ret; |
| 1034 | |
Lukas Wunner | 8f12a31 | 2015-08-23 23:23:02 +0200 | [diff] [blame] | 1035 | mutex_lock(&vgasr_mutex); |
Dave Airlie | 0d69704 | 2012-09-10 12:28:36 +1000 | [diff] [blame] | 1036 | vga_switcheroo_power_switch(pdev, VGA_SWITCHEROO_ON); |
Lukas Wunner | 8f12a31 | 2015-08-23 23:23:02 +0200 | [diff] [blame] | 1037 | mutex_unlock(&vgasr_mutex); |
Dave Airlie | 0d69704 | 2012-09-10 12:28:36 +1000 | [diff] [blame] | 1038 | ret = dev->bus->pm->runtime_resume(dev); |
| 1039 | if (ret) |
| 1040 | return ret; |
| 1041 | |
| 1042 | return 0; |
| 1043 | } |
| 1044 | |
Lukas Wunner | a645654 | 2015-08-23 15:18:55 +0200 | [diff] [blame] | 1045 | /** |
| 1046 | * vga_switcheroo_init_domain_pm_ops() - helper for driver power control |
| 1047 | * @dev: vga client device |
| 1048 | * @domain: power domain |
| 1049 | * |
| 1050 | * Helper for GPUs whose power state is controlled by the driver's runtime pm. |
| 1051 | * After the GPU has been suspended, the handler needs to be called to cut |
| 1052 | * power to the GPU. Likewise it needs to reinstate power before the GPU |
| 1053 | * can resume. To this end, this helper augments the suspend/resume functions |
| 1054 | * by the requisite calls to the handler. It needs only be called on platforms |
| 1055 | * where the power switch is separate to the device being powered down. |
| 1056 | */ |
Thierry Reding | 7491bfb | 2015-08-12 16:32:11 +0200 | [diff] [blame] | 1057 | int vga_switcheroo_init_domain_pm_ops(struct device *dev, |
| 1058 | struct dev_pm_domain *domain) |
Dave Airlie | 0d69704 | 2012-09-10 12:28:36 +1000 | [diff] [blame] | 1059 | { |
| 1060 | /* copy over all the bus versions */ |
| 1061 | if (dev->bus && dev->bus->pm) { |
| 1062 | domain->ops = *dev->bus->pm; |
| 1063 | domain->ops.runtime_suspend = vga_switcheroo_runtime_suspend; |
| 1064 | domain->ops.runtime_resume = vga_switcheroo_runtime_resume; |
| 1065 | |
Tomeu Vizoso | 989561d | 2016-01-07 16:46:13 +0100 | [diff] [blame] | 1066 | dev_pm_domain_set(dev, domain); |
Dave Airlie | 0d69704 | 2012-09-10 12:28:36 +1000 | [diff] [blame] | 1067 | return 0; |
| 1068 | } |
Tomeu Vizoso | 989561d | 2016-01-07 16:46:13 +0100 | [diff] [blame] | 1069 | dev_pm_domain_set(dev, NULL); |
Dave Airlie | 0d69704 | 2012-09-10 12:28:36 +1000 | [diff] [blame] | 1070 | return -EINVAL; |
| 1071 | } |
| 1072 | EXPORT_SYMBOL(vga_switcheroo_init_domain_pm_ops); |
| 1073 | |
Alex Deucher | 766a53d | 2014-09-12 17:51:29 -0400 | [diff] [blame] | 1074 | void vga_switcheroo_fini_domain_pm_ops(struct device *dev) |
| 1075 | { |
Tomeu Vizoso | 989561d | 2016-01-07 16:46:13 +0100 | [diff] [blame] | 1076 | dev_pm_domain_set(dev, NULL); |
Alex Deucher | 766a53d | 2014-09-12 17:51:29 -0400 | [diff] [blame] | 1077 | } |
| 1078 | EXPORT_SYMBOL(vga_switcheroo_fini_domain_pm_ops); |
| 1079 | |
Dave Airlie | 0d69704 | 2012-09-10 12:28:36 +1000 | [diff] [blame] | 1080 | static int vga_switcheroo_runtime_resume_hdmi_audio(struct device *dev) |
| 1081 | { |
| 1082 | struct pci_dev *pdev = to_pci_dev(dev); |
Lukas Wunner | 8f12a31 | 2015-08-23 23:23:02 +0200 | [diff] [blame] | 1083 | struct vga_switcheroo_client *client; |
| 1084 | struct device *video_dev = NULL; |
Dave Airlie | 0d69704 | 2012-09-10 12:28:36 +1000 | [diff] [blame] | 1085 | int ret; |
Dave Airlie | 0d69704 | 2012-09-10 12:28:36 +1000 | [diff] [blame] | 1086 | |
| 1087 | /* we need to check if we have to switch back on the video |
Joan Jani | fa6d513 | 2017-02-28 18:59:52 +0000 | [diff] [blame] | 1088 | * device so the audio device can come back |
| 1089 | */ |
Lukas Wunner | 8f12a31 | 2015-08-23 23:23:02 +0200 | [diff] [blame] | 1090 | mutex_lock(&vgasr_mutex); |
Dave Airlie | 0d69704 | 2012-09-10 12:28:36 +1000 | [diff] [blame] | 1091 | list_for_each_entry(client, &vgasr_priv.clients, list) { |
Thierry Reding | 7491bfb | 2015-08-12 16:32:11 +0200 | [diff] [blame] | 1092 | if (PCI_SLOT(client->pdev->devfn) == PCI_SLOT(pdev->devfn) && |
| 1093 | client_is_vga(client)) { |
Lukas Wunner | 8f12a31 | 2015-08-23 23:23:02 +0200 | [diff] [blame] | 1094 | video_dev = &client->pdev->dev; |
Dave Airlie | 0d69704 | 2012-09-10 12:28:36 +1000 | [diff] [blame] | 1095 | break; |
| 1096 | } |
| 1097 | } |
Lukas Wunner | 8f12a31 | 2015-08-23 23:23:02 +0200 | [diff] [blame] | 1098 | mutex_unlock(&vgasr_mutex); |
| 1099 | |
| 1100 | if (video_dev) { |
| 1101 | ret = pm_runtime_get_sync(video_dev); |
| 1102 | if (ret && ret != 1) |
| 1103 | return ret; |
| 1104 | } |
Dave Airlie | 0d69704 | 2012-09-10 12:28:36 +1000 | [diff] [blame] | 1105 | ret = dev->bus->pm->runtime_resume(dev); |
| 1106 | |
| 1107 | /* put the reference for the gpu */ |
Lukas Wunner | 8f12a31 | 2015-08-23 23:23:02 +0200 | [diff] [blame] | 1108 | if (video_dev) { |
| 1109 | pm_runtime_mark_last_busy(video_dev); |
| 1110 | pm_runtime_put_autosuspend(video_dev); |
Dave Airlie | 0d69704 | 2012-09-10 12:28:36 +1000 | [diff] [blame] | 1111 | } |
| 1112 | return ret; |
| 1113 | } |
| 1114 | |
Lukas Wunner | a645654 | 2015-08-23 15:18:55 +0200 | [diff] [blame] | 1115 | /** |
| 1116 | * vga_switcheroo_init_domain_pm_optimus_hdmi_audio() - helper for driver |
Joan Jani | fa6d513 | 2017-02-28 18:59:52 +0000 | [diff] [blame] | 1117 | * power control |
Lukas Wunner | a645654 | 2015-08-23 15:18:55 +0200 | [diff] [blame] | 1118 | * @dev: audio client device |
| 1119 | * @domain: power domain |
| 1120 | * |
| 1121 | * Helper for GPUs whose power state is controlled by the driver's runtime pm. |
| 1122 | * When the audio device resumes, the GPU needs to be woken. This helper |
| 1123 | * augments the audio device's resume function to do that. |
| 1124 | * |
| 1125 | * Return: 0 on success, -EINVAL if no power management operations are |
| 1126 | * defined for this device. |
| 1127 | */ |
Thierry Reding | 7491bfb | 2015-08-12 16:32:11 +0200 | [diff] [blame] | 1128 | int |
| 1129 | vga_switcheroo_init_domain_pm_optimus_hdmi_audio(struct device *dev, |
| 1130 | struct dev_pm_domain *domain) |
Dave Airlie | 0d69704 | 2012-09-10 12:28:36 +1000 | [diff] [blame] | 1131 | { |
| 1132 | /* copy over all the bus versions */ |
| 1133 | if (dev->bus && dev->bus->pm) { |
| 1134 | domain->ops = *dev->bus->pm; |
Thierry Reding | 7491bfb | 2015-08-12 16:32:11 +0200 | [diff] [blame] | 1135 | domain->ops.runtime_resume = |
| 1136 | vga_switcheroo_runtime_resume_hdmi_audio; |
Dave Airlie | 0d69704 | 2012-09-10 12:28:36 +1000 | [diff] [blame] | 1137 | |
Tomeu Vizoso | 989561d | 2016-01-07 16:46:13 +0100 | [diff] [blame] | 1138 | dev_pm_domain_set(dev, domain); |
Dave Airlie | 0d69704 | 2012-09-10 12:28:36 +1000 | [diff] [blame] | 1139 | return 0; |
| 1140 | } |
Tomeu Vizoso | 989561d | 2016-01-07 16:46:13 +0100 | [diff] [blame] | 1141 | dev_pm_domain_set(dev, NULL); |
Dave Airlie | 0d69704 | 2012-09-10 12:28:36 +1000 | [diff] [blame] | 1142 | return -EINVAL; |
| 1143 | } |
| 1144 | EXPORT_SYMBOL(vga_switcheroo_init_domain_pm_optimus_hdmi_audio); |