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