Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2010 Red Hat Inc. |
| 3 | * Author : Dave Airlie <airlied@redhat.com> |
| 4 | * |
| 5 | * |
| 6 | * Licensed under GPLv2 |
| 7 | * |
| 8 | * vga_switcheroo.c - Support for laptop with dual GPU using one set of outputs |
| 9 | |
| 10 | Switcher interface - methods require for ATPX and DCM |
| 11 | - switchto - this throws the output MUX switch |
| 12 | - discrete_set_power - sets the power state for the discrete card |
| 13 | |
| 14 | GPU driver interface |
| 15 | - set_gpu_state - this should do the equiv of s/r for the card |
| 16 | - this should *not* set the discrete power state |
| 17 | - switch_check - check if the device is in a position to switch now |
| 18 | */ |
| 19 | |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/dmi.h> |
| 22 | #include <linux/seq_file.h> |
| 23 | #include <linux/uaccess.h> |
| 24 | #include <linux/fs.h> |
| 25 | #include <linux/debugfs.h> |
| 26 | #include <linux/fb.h> |
| 27 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 28 | #include <linux/pci.h> |
| 29 | #include <linux/vga_switcheroo.h> |
| 30 | |
Matthew Garrett | 2fbe8c7 | 2012-04-16 16:26:03 -0400 | [diff] [blame] | 31 | #include <linux/vgaarb.h> |
| 32 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 33 | struct vga_switcheroo_client { |
| 34 | struct pci_dev *pdev; |
| 35 | struct fb_info *fb_info; |
| 36 | int pwr_state; |
Takashi Iwai | 26ec685 | 2012-05-11 07:51:17 +0200 | [diff] [blame] | 37 | const struct vga_switcheroo_client_ops *ops; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 38 | int id; |
| 39 | bool active; |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 40 | struct list_head list; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | static DEFINE_MUTEX(vgasr_mutex); |
| 44 | |
| 45 | struct vgasr_priv { |
| 46 | |
| 47 | bool active; |
| 48 | bool delayed_switch_active; |
| 49 | enum vga_switcheroo_client_id delayed_client_id; |
| 50 | |
| 51 | struct dentry *debugfs_root; |
| 52 | struct dentry *switch_file; |
| 53 | |
| 54 | int registered_clients; |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 55 | struct list_head clients; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 56 | |
| 57 | struct vga_switcheroo_handler *handler; |
| 58 | }; |
| 59 | |
Takashi Iwai | 3e9e63d | 2012-04-26 14:29:48 +0200 | [diff] [blame] | 60 | #define ID_BIT_AUDIO 0x100 |
| 61 | #define client_is_audio(c) ((c)->id & ID_BIT_AUDIO) |
| 62 | #define client_is_vga(c) ((c)->id == -1 || !client_is_audio(c)) |
| 63 | #define client_id(c) ((c)->id & ~ID_BIT_AUDIO) |
| 64 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 65 | static int vga_switcheroo_debugfs_init(struct vgasr_priv *priv); |
| 66 | static void vga_switcheroo_debugfs_fini(struct vgasr_priv *priv); |
| 67 | |
| 68 | /* only one switcheroo per system */ |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 69 | static struct vgasr_priv vgasr_priv = { |
| 70 | .clients = LIST_HEAD_INIT(vgasr_priv.clients), |
| 71 | }; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 72 | |
| 73 | int vga_switcheroo_register_handler(struct vga_switcheroo_handler *handler) |
| 74 | { |
| 75 | mutex_lock(&vgasr_mutex); |
| 76 | if (vgasr_priv.handler) { |
| 77 | mutex_unlock(&vgasr_mutex); |
| 78 | return -EINVAL; |
| 79 | } |
| 80 | |
| 81 | vgasr_priv.handler = handler; |
| 82 | mutex_unlock(&vgasr_mutex); |
| 83 | return 0; |
| 84 | } |
| 85 | EXPORT_SYMBOL(vga_switcheroo_register_handler); |
| 86 | |
| 87 | void vga_switcheroo_unregister_handler(void) |
| 88 | { |
| 89 | mutex_lock(&vgasr_mutex); |
| 90 | vgasr_priv.handler = NULL; |
| 91 | mutex_unlock(&vgasr_mutex); |
| 92 | } |
| 93 | EXPORT_SYMBOL(vga_switcheroo_unregister_handler); |
| 94 | |
| 95 | static void vga_switcheroo_enable(void) |
| 96 | { |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 97 | int ret; |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 98 | struct vga_switcheroo_client *client; |
| 99 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 100 | /* call the handler to init */ |
| 101 | vgasr_priv.handler->init(); |
| 102 | |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 103 | list_for_each_entry(client, &vgasr_priv.clients, list) { |
Takashi Iwai | 3e9e63d | 2012-04-26 14:29:48 +0200 | [diff] [blame] | 104 | if (client->id != -1) |
| 105 | continue; |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 106 | ret = vgasr_priv.handler->get_client_id(client->pdev); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 107 | if (ret < 0) |
| 108 | return; |
| 109 | |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 110 | client->id = ret; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 111 | } |
| 112 | vga_switcheroo_debugfs_init(&vgasr_priv); |
| 113 | vgasr_priv.active = true; |
| 114 | } |
| 115 | |
Takashi Iwai | 3e9e63d | 2012-04-26 14:29:48 +0200 | [diff] [blame] | 116 | static int register_client(struct pci_dev *pdev, |
| 117 | const struct vga_switcheroo_client_ops *ops, |
| 118 | int id, bool active) |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 119 | { |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 120 | struct vga_switcheroo_client *client; |
| 121 | |
| 122 | client = kzalloc(sizeof(*client), GFP_KERNEL); |
| 123 | if (!client) |
| 124 | return -ENOMEM; |
| 125 | |
| 126 | client->pwr_state = VGA_SWITCHEROO_ON; |
| 127 | client->pdev = pdev; |
Takashi Iwai | 26ec685 | 2012-05-11 07:51:17 +0200 | [diff] [blame] | 128 | client->ops = ops; |
Takashi Iwai | 3e9e63d | 2012-04-26 14:29:48 +0200 | [diff] [blame] | 129 | client->id = id; |
| 130 | client->active = active; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 131 | |
| 132 | mutex_lock(&vgasr_mutex); |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 133 | list_add_tail(&client->list, &vgasr_priv.clients); |
Takashi Iwai | 3e9e63d | 2012-04-26 14:29:48 +0200 | [diff] [blame] | 134 | if (client_is_vga(client)) |
| 135 | vgasr_priv.registered_clients++; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 136 | |
| 137 | /* if we get two clients + handler */ |
Takashi Iwai | 3e9e63d | 2012-04-26 14:29:48 +0200 | [diff] [blame] | 138 | if (!vgasr_priv.active && |
| 139 | vgasr_priv.registered_clients == 2 && vgasr_priv.handler) { |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 140 | printk(KERN_INFO "vga_switcheroo: enabled\n"); |
| 141 | vga_switcheroo_enable(); |
| 142 | } |
| 143 | mutex_unlock(&vgasr_mutex); |
| 144 | return 0; |
| 145 | } |
Takashi Iwai | 3e9e63d | 2012-04-26 14:29:48 +0200 | [diff] [blame] | 146 | |
| 147 | int vga_switcheroo_register_client(struct pci_dev *pdev, |
| 148 | const struct vga_switcheroo_client_ops *ops) |
| 149 | { |
| 150 | return register_client(pdev, ops, -1, |
| 151 | pdev == vga_default_device()); |
| 152 | } |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 153 | EXPORT_SYMBOL(vga_switcheroo_register_client); |
| 154 | |
Takashi Iwai | 3e9e63d | 2012-04-26 14:29:48 +0200 | [diff] [blame] | 155 | int vga_switcheroo_register_audio_client(struct pci_dev *pdev, |
| 156 | const struct vga_switcheroo_client_ops *ops, |
| 157 | int id, bool active) |
| 158 | { |
| 159 | return register_client(pdev, ops, id | ID_BIT_AUDIO, active); |
| 160 | } |
| 161 | EXPORT_SYMBOL(vga_switcheroo_register_audio_client); |
| 162 | |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 163 | static struct vga_switcheroo_client * |
| 164 | find_client_from_pci(struct list_head *head, struct pci_dev *pdev) |
| 165 | { |
| 166 | struct vga_switcheroo_client *client; |
| 167 | list_for_each_entry(client, head, list) |
| 168 | if (client->pdev == pdev) |
| 169 | return client; |
| 170 | return NULL; |
| 171 | } |
| 172 | |
| 173 | static struct vga_switcheroo_client * |
| 174 | find_client_from_id(struct list_head *head, int client_id) |
| 175 | { |
| 176 | struct vga_switcheroo_client *client; |
| 177 | list_for_each_entry(client, head, list) |
| 178 | if (client->id == client_id) |
| 179 | return client; |
| 180 | return NULL; |
| 181 | } |
| 182 | |
| 183 | static struct vga_switcheroo_client * |
| 184 | find_active_client(struct list_head *head) |
| 185 | { |
| 186 | struct vga_switcheroo_client *client; |
| 187 | list_for_each_entry(client, head, list) |
Takashi Iwai | 3e9e63d | 2012-04-26 14:29:48 +0200 | [diff] [blame] | 188 | if (client->active && client_is_vga(client)) |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 189 | return client; |
| 190 | return NULL; |
| 191 | } |
| 192 | |
Takashi Iwai | c8e9cf7 | 2012-06-07 12:15:15 +0200 | [diff] [blame] | 193 | int vga_switcheroo_get_client_state(struct pci_dev *pdev) |
| 194 | { |
| 195 | struct vga_switcheroo_client *client; |
| 196 | |
| 197 | client = find_client_from_pci(&vgasr_priv.clients, pdev); |
| 198 | if (!client) |
| 199 | return VGA_SWITCHEROO_NOT_FOUND; |
| 200 | if (!vgasr_priv.active) |
| 201 | return VGA_SWITCHEROO_INIT; |
| 202 | return client->pwr_state; |
| 203 | } |
| 204 | EXPORT_SYMBOL(vga_switcheroo_get_client_state); |
| 205 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 206 | void vga_switcheroo_unregister_client(struct pci_dev *pdev) |
| 207 | { |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 208 | struct vga_switcheroo_client *client; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 209 | |
| 210 | mutex_lock(&vgasr_mutex); |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 211 | client = find_client_from_pci(&vgasr_priv.clients, pdev); |
| 212 | if (client) { |
Takashi Iwai | 3e9e63d | 2012-04-26 14:29:48 +0200 | [diff] [blame] | 213 | if (client_is_vga(client)) |
| 214 | vgasr_priv.registered_clients--; |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 215 | list_del(&client->list); |
| 216 | kfree(client); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 217 | } |
Takashi Iwai | 3e9e63d | 2012-04-26 14:29:48 +0200 | [diff] [blame] | 218 | if (vgasr_priv.active && vgasr_priv.registered_clients < 2) { |
| 219 | printk(KERN_INFO "vga_switcheroo: disabled\n"); |
| 220 | vga_switcheroo_debugfs_fini(&vgasr_priv); |
| 221 | vgasr_priv.active = false; |
| 222 | } |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 223 | mutex_unlock(&vgasr_mutex); |
| 224 | } |
| 225 | EXPORT_SYMBOL(vga_switcheroo_unregister_client); |
| 226 | |
| 227 | void vga_switcheroo_client_fb_set(struct pci_dev *pdev, |
| 228 | struct fb_info *info) |
| 229 | { |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 230 | struct vga_switcheroo_client *client; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 231 | |
| 232 | mutex_lock(&vgasr_mutex); |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 233 | client = find_client_from_pci(&vgasr_priv.clients, pdev); |
| 234 | if (client) |
| 235 | client->fb_info = info; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 236 | mutex_unlock(&vgasr_mutex); |
| 237 | } |
| 238 | EXPORT_SYMBOL(vga_switcheroo_client_fb_set); |
| 239 | |
| 240 | static int vga_switcheroo_show(struct seq_file *m, void *v) |
| 241 | { |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 242 | struct vga_switcheroo_client *client; |
| 243 | int i = 0; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 244 | mutex_lock(&vgasr_mutex); |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 245 | list_for_each_entry(client, &vgasr_priv.clients, list) { |
Takashi Iwai | 3e9e63d | 2012-04-26 14:29:48 +0200 | [diff] [blame] | 246 | seq_printf(m, "%d:%s%s:%c:%s:%s\n", i, |
| 247 | client_id(client) == VGA_SWITCHEROO_DIS ? "DIS" : "IGD", |
| 248 | client_is_vga(client) ? "" : "-Audio", |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 249 | client->active ? '+' : ' ', |
| 250 | client->pwr_state ? "Pwr" : "Off", |
| 251 | pci_name(client->pdev)); |
| 252 | i++; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 253 | } |
| 254 | mutex_unlock(&vgasr_mutex); |
| 255 | return 0; |
| 256 | } |
| 257 | |
| 258 | static int vga_switcheroo_debugfs_open(struct inode *inode, struct file *file) |
| 259 | { |
| 260 | return single_open(file, vga_switcheroo_show, NULL); |
| 261 | } |
| 262 | |
| 263 | static int vga_switchon(struct vga_switcheroo_client *client) |
| 264 | { |
Dave Airlie | 5cfb3c3 | 2010-12-06 12:31:50 +1000 | [diff] [blame] | 265 | if (vgasr_priv.handler->power_state) |
| 266 | vgasr_priv.handler->power_state(client->id, VGA_SWITCHEROO_ON); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 267 | /* call the driver callback to turn on device */ |
Takashi Iwai | 26ec685 | 2012-05-11 07:51:17 +0200 | [diff] [blame] | 268 | client->ops->set_gpu_state(client->pdev, VGA_SWITCHEROO_ON); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 269 | client->pwr_state = VGA_SWITCHEROO_ON; |
| 270 | return 0; |
| 271 | } |
| 272 | |
| 273 | static int vga_switchoff(struct vga_switcheroo_client *client) |
| 274 | { |
| 275 | /* call the driver callback to turn off device */ |
Takashi Iwai | 26ec685 | 2012-05-11 07:51:17 +0200 | [diff] [blame] | 276 | client->ops->set_gpu_state(client->pdev, VGA_SWITCHEROO_OFF); |
Dave Airlie | 5cfb3c3 | 2010-12-06 12:31:50 +1000 | [diff] [blame] | 277 | if (vgasr_priv.handler->power_state) |
| 278 | vgasr_priv.handler->power_state(client->id, VGA_SWITCHEROO_OFF); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 279 | client->pwr_state = VGA_SWITCHEROO_OFF; |
| 280 | return 0; |
| 281 | } |
| 282 | |
Takashi Iwai | 3e9e63d | 2012-04-26 14:29:48 +0200 | [diff] [blame] | 283 | static void set_audio_state(int id, int state) |
| 284 | { |
| 285 | struct vga_switcheroo_client *client; |
| 286 | |
| 287 | client = find_client_from_id(&vgasr_priv.clients, id | ID_BIT_AUDIO); |
| 288 | if (client && client->pwr_state != state) { |
| 289 | client->ops->set_gpu_state(client->pdev, state); |
| 290 | client->pwr_state = state; |
| 291 | } |
| 292 | } |
| 293 | |
Dave Airlie | 66b37c6 | 2010-12-07 14:24:25 +1000 | [diff] [blame] | 294 | /* stage one happens before delay */ |
| 295 | static int vga_switchto_stage1(struct vga_switcheroo_client *new_client) |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 296 | { |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 297 | struct vga_switcheroo_client *active; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 298 | |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 299 | active = find_active_client(&vgasr_priv.clients); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 300 | if (!active) |
| 301 | return 0; |
| 302 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 303 | if (new_client->pwr_state == VGA_SWITCHEROO_OFF) |
| 304 | vga_switchon(new_client); |
| 305 | |
Matthew Garrett | 2fbe8c7 | 2012-04-16 16:26:03 -0400 | [diff] [blame] | 306 | vga_set_default_device(new_client->pdev); |
Dave Airlie | 66b37c6 | 2010-12-07 14:24:25 +1000 | [diff] [blame] | 307 | return 0; |
| 308 | } |
| 309 | |
| 310 | /* post delay */ |
| 311 | static int vga_switchto_stage2(struct vga_switcheroo_client *new_client) |
| 312 | { |
| 313 | int ret; |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 314 | struct vga_switcheroo_client *active; |
Dave Airlie | 66b37c6 | 2010-12-07 14:24:25 +1000 | [diff] [blame] | 315 | |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 316 | active = find_active_client(&vgasr_priv.clients); |
Dave Airlie | 66b37c6 | 2010-12-07 14:24:25 +1000 | [diff] [blame] | 317 | if (!active) |
| 318 | return 0; |
| 319 | |
| 320 | active->active = false; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 321 | |
Takashi Iwai | c91c3fa | 2012-06-09 08:46:42 +0200 | [diff] [blame] | 322 | set_audio_state(active->id, VGA_SWITCHEROO_OFF); |
| 323 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 324 | if (new_client->fb_info) { |
| 325 | struct fb_event event; |
| 326 | event.info = new_client->fb_info; |
| 327 | fb_notifier_call_chain(FB_EVENT_REMAP_ALL_CONSOLE, &event); |
| 328 | } |
| 329 | |
| 330 | ret = vgasr_priv.handler->switchto(new_client->id); |
| 331 | if (ret) |
| 332 | return ret; |
| 333 | |
Takashi Iwai | 26ec685 | 2012-05-11 07:51:17 +0200 | [diff] [blame] | 334 | if (new_client->ops->reprobe) |
| 335 | new_client->ops->reprobe(new_client->pdev); |
Dave Airlie | 8d608aa | 2010-12-07 08:57:57 +1000 | [diff] [blame] | 336 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 337 | if (active->pwr_state == VGA_SWITCHEROO_ON) |
| 338 | vga_switchoff(active); |
| 339 | |
Takashi Iwai | c91c3fa | 2012-06-09 08:46:42 +0200 | [diff] [blame] | 340 | set_audio_state(new_client->id, VGA_SWITCHEROO_ON); |
| 341 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 342 | new_client->active = true; |
| 343 | return 0; |
| 344 | } |
| 345 | |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 346 | static bool check_can_switch(void) |
| 347 | { |
| 348 | struct vga_switcheroo_client *client; |
| 349 | |
| 350 | list_for_each_entry(client, &vgasr_priv.clients, list) { |
Takashi Iwai | 26ec685 | 2012-05-11 07:51:17 +0200 | [diff] [blame] | 351 | if (!client->ops->can_switch(client->pdev)) { |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 352 | printk(KERN_ERR "vga_switcheroo: client %x refused switch\n", client->id); |
| 353 | return false; |
| 354 | } |
| 355 | } |
| 356 | return true; |
| 357 | } |
| 358 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 359 | static ssize_t |
| 360 | vga_switcheroo_debugfs_write(struct file *filp, const char __user *ubuf, |
| 361 | size_t cnt, loff_t *ppos) |
| 362 | { |
| 363 | char usercmd[64]; |
| 364 | const char *pdev_name; |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 365 | int ret; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 366 | bool delay = false, can_switch; |
Dave Airlie | 851ab95 | 2010-12-06 12:35:52 +1000 | [diff] [blame] | 367 | bool just_mux = false; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 368 | int client_id = -1; |
| 369 | struct vga_switcheroo_client *client = NULL; |
| 370 | |
| 371 | if (cnt > 63) |
| 372 | cnt = 63; |
| 373 | |
| 374 | if (copy_from_user(usercmd, ubuf, cnt)) |
| 375 | return -EFAULT; |
| 376 | |
| 377 | mutex_lock(&vgasr_mutex); |
| 378 | |
Jiri Slaby | 8c88e50 | 2010-04-27 14:11:03 -0700 | [diff] [blame] | 379 | if (!vgasr_priv.active) { |
| 380 | cnt = -EINVAL; |
| 381 | goto out; |
| 382 | } |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 383 | |
| 384 | /* pwr off the device not in use */ |
| 385 | if (strncmp(usercmd, "OFF", 3) == 0) { |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 386 | list_for_each_entry(client, &vgasr_priv.clients, list) { |
Takashi Iwai | c91c3fa | 2012-06-09 08:46:42 +0200 | [diff] [blame] | 387 | if (client->active || client_is_audio(client)) |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 388 | continue; |
Takashi Iwai | c91c3fa | 2012-06-09 08:46:42 +0200 | [diff] [blame] | 389 | set_audio_state(client->id, VGA_SWITCHEROO_OFF); |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 390 | if (client->pwr_state == VGA_SWITCHEROO_ON) |
| 391 | vga_switchoff(client); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 392 | } |
| 393 | goto out; |
| 394 | } |
| 395 | /* pwr on the device not in use */ |
| 396 | if (strncmp(usercmd, "ON", 2) == 0) { |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 397 | list_for_each_entry(client, &vgasr_priv.clients, list) { |
Takashi Iwai | c91c3fa | 2012-06-09 08:46:42 +0200 | [diff] [blame] | 398 | if (client->active || client_is_audio(client)) |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 399 | continue; |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 400 | if (client->pwr_state == VGA_SWITCHEROO_OFF) |
| 401 | vga_switchon(client); |
Takashi Iwai | c91c3fa | 2012-06-09 08:46:42 +0200 | [diff] [blame] | 402 | set_audio_state(client->id, VGA_SWITCHEROO_ON); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 403 | } |
| 404 | goto out; |
| 405 | } |
| 406 | |
| 407 | /* request a delayed switch - test can we switch now */ |
| 408 | if (strncmp(usercmd, "DIGD", 4) == 0) { |
| 409 | client_id = VGA_SWITCHEROO_IGD; |
| 410 | delay = true; |
| 411 | } |
| 412 | |
| 413 | if (strncmp(usercmd, "DDIS", 4) == 0) { |
| 414 | client_id = VGA_SWITCHEROO_DIS; |
| 415 | delay = true; |
| 416 | } |
| 417 | |
| 418 | if (strncmp(usercmd, "IGD", 3) == 0) |
| 419 | client_id = VGA_SWITCHEROO_IGD; |
| 420 | |
| 421 | if (strncmp(usercmd, "DIS", 3) == 0) |
| 422 | client_id = VGA_SWITCHEROO_DIS; |
| 423 | |
Dan Carpenter | fea6f33 | 2011-01-07 08:12:27 +0300 | [diff] [blame] | 424 | if (strncmp(usercmd, "MIGD", 4) == 0) { |
Dave Airlie | 851ab95 | 2010-12-06 12:35:52 +1000 | [diff] [blame] | 425 | just_mux = true; |
| 426 | client_id = VGA_SWITCHEROO_IGD; |
| 427 | } |
Dan Carpenter | fea6f33 | 2011-01-07 08:12:27 +0300 | [diff] [blame] | 428 | if (strncmp(usercmd, "MDIS", 4) == 0) { |
Dave Airlie | 851ab95 | 2010-12-06 12:35:52 +1000 | [diff] [blame] | 429 | just_mux = true; |
| 430 | client_id = VGA_SWITCHEROO_DIS; |
| 431 | } |
| 432 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 433 | if (client_id == -1) |
| 434 | goto out; |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 435 | client = find_client_from_id(&vgasr_priv.clients, client_id); |
| 436 | if (!client) |
| 437 | goto out; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 438 | |
| 439 | vgasr_priv.delayed_switch_active = false; |
Dave Airlie | 851ab95 | 2010-12-06 12:35:52 +1000 | [diff] [blame] | 440 | |
| 441 | if (just_mux) { |
| 442 | ret = vgasr_priv.handler->switchto(client_id); |
| 443 | goto out; |
| 444 | } |
| 445 | |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 446 | if (client->active) |
Florian Mickler | a67b888 | 2011-05-15 16:32:50 +0200 | [diff] [blame] | 447 | goto out; |
| 448 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 449 | /* okay we want a switch - test if devices are willing to switch */ |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 450 | can_switch = check_can_switch(); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 451 | |
| 452 | if (can_switch == false && delay == false) |
| 453 | goto out; |
| 454 | |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 455 | if (can_switch) { |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 456 | pdev_name = pci_name(client->pdev); |
Dave Airlie | 66b37c6 | 2010-12-07 14:24:25 +1000 | [diff] [blame] | 457 | ret = vga_switchto_stage1(client); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 458 | if (ret) |
Dave Airlie | 66b37c6 | 2010-12-07 14:24:25 +1000 | [diff] [blame] | 459 | printk(KERN_ERR "vga_switcheroo: switching failed stage 1 %d\n", ret); |
| 460 | |
| 461 | ret = vga_switchto_stage2(client); |
| 462 | if (ret) |
| 463 | printk(KERN_ERR "vga_switcheroo: switching failed stage 2 %d\n", ret); |
| 464 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 465 | } else { |
| 466 | printk(KERN_INFO "vga_switcheroo: setting delayed switch to client %d\n", client->id); |
| 467 | vgasr_priv.delayed_switch_active = true; |
| 468 | vgasr_priv.delayed_client_id = client_id; |
| 469 | |
Dave Airlie | 66b37c6 | 2010-12-07 14:24:25 +1000 | [diff] [blame] | 470 | ret = vga_switchto_stage1(client); |
| 471 | if (ret) |
| 472 | printk(KERN_ERR "vga_switcheroo: delayed switching stage 1 failed %d\n", ret); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | out: |
| 476 | mutex_unlock(&vgasr_mutex); |
| 477 | return cnt; |
| 478 | } |
| 479 | |
| 480 | static const struct file_operations vga_switcheroo_debugfs_fops = { |
| 481 | .owner = THIS_MODULE, |
| 482 | .open = vga_switcheroo_debugfs_open, |
| 483 | .write = vga_switcheroo_debugfs_write, |
| 484 | .read = seq_read, |
| 485 | .llseek = seq_lseek, |
| 486 | .release = single_release, |
| 487 | }; |
| 488 | |
| 489 | static void vga_switcheroo_debugfs_fini(struct vgasr_priv *priv) |
| 490 | { |
| 491 | if (priv->switch_file) { |
| 492 | debugfs_remove(priv->switch_file); |
| 493 | priv->switch_file = NULL; |
| 494 | } |
| 495 | if (priv->debugfs_root) { |
| 496 | debugfs_remove(priv->debugfs_root); |
| 497 | priv->debugfs_root = NULL; |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | static int vga_switcheroo_debugfs_init(struct vgasr_priv *priv) |
| 502 | { |
| 503 | /* already initialised */ |
| 504 | if (priv->debugfs_root) |
| 505 | return 0; |
| 506 | priv->debugfs_root = debugfs_create_dir("vgaswitcheroo", NULL); |
| 507 | |
| 508 | if (!priv->debugfs_root) { |
| 509 | printk(KERN_ERR "vga_switcheroo: Cannot create /sys/kernel/debug/vgaswitcheroo\n"); |
| 510 | goto fail; |
| 511 | } |
| 512 | |
| 513 | priv->switch_file = debugfs_create_file("switch", 0644, |
| 514 | priv->debugfs_root, NULL, &vga_switcheroo_debugfs_fops); |
| 515 | if (!priv->switch_file) { |
| 516 | printk(KERN_ERR "vga_switcheroo: cannot create /sys/kernel/debug/vgaswitcheroo/switch\n"); |
| 517 | goto fail; |
| 518 | } |
| 519 | return 0; |
| 520 | fail: |
| 521 | vga_switcheroo_debugfs_fini(priv); |
| 522 | return -1; |
| 523 | } |
| 524 | |
| 525 | int vga_switcheroo_process_delayed_switch(void) |
| 526 | { |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 527 | struct vga_switcheroo_client *client; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 528 | const char *pdev_name; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 529 | int ret; |
| 530 | int err = -EINVAL; |
| 531 | |
| 532 | mutex_lock(&vgasr_mutex); |
| 533 | if (!vgasr_priv.delayed_switch_active) |
| 534 | goto err; |
| 535 | |
| 536 | printk(KERN_INFO "vga_switcheroo: processing delayed switch to %d\n", vgasr_priv.delayed_client_id); |
| 537 | |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 538 | client = find_client_from_id(&vgasr_priv.clients, |
| 539 | vgasr_priv.delayed_client_id); |
| 540 | if (!client || !check_can_switch()) |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 541 | goto err; |
| 542 | |
| 543 | pdev_name = pci_name(client->pdev); |
Dave Airlie | 66b37c6 | 2010-12-07 14:24:25 +1000 | [diff] [blame] | 544 | ret = vga_switchto_stage2(client); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 545 | if (ret) |
Dave Airlie | 66b37c6 | 2010-12-07 14:24:25 +1000 | [diff] [blame] | 546 | printk(KERN_ERR "vga_switcheroo: delayed switching failed stage 2 %d\n", ret); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 547 | |
| 548 | vgasr_priv.delayed_switch_active = false; |
| 549 | err = 0; |
| 550 | err: |
| 551 | mutex_unlock(&vgasr_mutex); |
| 552 | return err; |
| 553 | } |
| 554 | EXPORT_SYMBOL(vga_switcheroo_process_delayed_switch); |
| 555 | |