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