Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * vga_switcheroo.c - Support for laptop with dual GPU using one set of outputs
0003  *
0004  * Copyright (c) 2010 Red Hat Inc.
0005  * Author : Dave Airlie <airlied@redhat.com>
0006  *
0007  * Copyright (c) 2015 Lukas Wunner <lukas@wunner.de>
0008  *
0009  * Permission is hereby granted, free of charge, to any person obtaining a
0010  * copy of this software and associated documentation files (the "Software"),
0011  * to deal in the Software without restriction, including without limitation
0012  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
0013  * and/or sell copies of the Software, and to permit persons to whom the
0014  * Software is furnished to do so, subject to the following conditions:
0015  *
0016  * The above copyright notice and this permission notice (including the next
0017  * paragraph) shall be included in all copies or substantial portions of the
0018  * Software.
0019  *
0020  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0021  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0022  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
0023  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
0024  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
0025  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
0026  * DEALINGS
0027  * IN THE SOFTWARE.
0028  *
0029  */
0030 
0031 #define pr_fmt(fmt) "vga_switcheroo: " fmt
0032 
0033 #include <linux/apple-gmux.h>
0034 #include <linux/console.h>
0035 #include <linux/debugfs.h>
0036 #include <linux/fb.h>
0037 #include <linux/fs.h>
0038 #include <linux/fbcon.h>
0039 #include <linux/module.h>
0040 #include <linux/pci.h>
0041 #include <linux/pm_domain.h>
0042 #include <linux/pm_runtime.h>
0043 #include <linux/seq_file.h>
0044 #include <linux/uaccess.h>
0045 #include <linux/vgaarb.h>
0046 #include <linux/vga_switcheroo.h>
0047 
0048 /**
0049  * DOC: Overview
0050  *
0051  * vga_switcheroo is the Linux subsystem for laptop hybrid graphics.
0052  * These come in two flavors:
0053  *
0054  * * muxed: Dual GPUs with a multiplexer chip to switch outputs between GPUs.
0055  * * muxless: Dual GPUs but only one of them is connected to outputs.
0056  *   The other one is merely used to offload rendering, its results
0057  *   are copied over PCIe into the framebuffer. On Linux this is
0058  *   supported with DRI PRIME.
0059  *
0060  * Hybrid graphics started to appear in the late Naughties and were initially
0061  * all muxed. Newer laptops moved to a muxless architecture for cost reasons.
0062  * A notable exception is the MacBook Pro which continues to use a mux.
0063  * Muxes come with varying capabilities: Some switch only the panel, others
0064  * can also switch external displays. Some switch all display pins at once
0065  * while others can switch just the DDC lines. (To allow EDID probing
0066  * for the inactive GPU.) Also, muxes are often used to cut power to the
0067  * discrete GPU while it is not used.
0068  *
0069  * DRM drivers register GPUs with vga_switcheroo, these are henceforth called
0070  * clients. The mux is called the handler. Muxless machines also register a
0071  * handler to control the power state of the discrete GPU, its ->switchto
0072  * callback is a no-op for obvious reasons. The discrete GPU is often equipped
0073  * with an HDA controller for the HDMI/DP audio signal, this will also
0074  * register as a client so that vga_switcheroo can take care of the correct
0075  * suspend/resume order when changing the discrete GPU's power state. In total
0076  * there can thus be up to three clients: Two vga clients (GPUs) and one audio
0077  * client (on the discrete GPU). The code is mostly prepared to support
0078  * machines with more than two GPUs should they become available.
0079  *
0080  * The GPU to which the outputs are currently switched is called the
0081  * active client in vga_switcheroo parlance. The GPU not in use is the
0082  * inactive client. When the inactive client's DRM driver is loaded,
0083  * it will be unable to probe the panel's EDID and hence depends on
0084  * VBIOS to provide its display modes. If the VBIOS modes are bogus or
0085  * if there is no VBIOS at all (which is common on the MacBook Pro),
0086  * a client may alternatively request that the DDC lines are temporarily
0087  * switched to it, provided that the handler supports this. Switching
0088  * only the DDC lines and not the entire output avoids unnecessary
0089  * flickering.
0090  */
0091 
0092 /**
0093  * struct vga_switcheroo_client - registered client
0094  * @pdev: client pci device
0095  * @fb_info: framebuffer to which console is remapped on switching
0096  * @pwr_state: current power state if manual power control is used.
0097  *  For driver power control, call vga_switcheroo_pwr_state().
0098  * @ops: client callbacks
0099  * @id: client identifier. Determining the id requires the handler,
0100  *  so gpus are initially assigned VGA_SWITCHEROO_UNKNOWN_ID
0101  *  and later given their true id in vga_switcheroo_enable()
0102  * @active: whether the outputs are currently switched to this client
0103  * @driver_power_control: whether power state is controlled by the driver's
0104  *  runtime pm. If true, writing ON and OFF to the vga_switcheroo debugfs
0105  *  interface is a no-op so as not to interfere with runtime pm
0106  * @list: client list
0107  * @vga_dev: pci device, indicate which GPU is bound to current audio client
0108  *
0109  * Registered client. A client can be either a GPU or an audio device on a GPU.
0110  * For audio clients, the @fb_info and @active members are bogus. For GPU
0111  * clients, the @vga_dev is bogus.
0112  */
0113 struct vga_switcheroo_client {
0114     struct pci_dev *pdev;
0115     struct fb_info *fb_info;
0116     enum vga_switcheroo_state pwr_state;
0117     const struct vga_switcheroo_client_ops *ops;
0118     enum vga_switcheroo_client_id id;
0119     bool active;
0120     bool driver_power_control;
0121     struct list_head list;
0122     struct pci_dev *vga_dev;
0123 };
0124 
0125 /*
0126  * protects access to struct vgasr_priv
0127  */
0128 static DEFINE_MUTEX(vgasr_mutex);
0129 
0130 /**
0131  * struct vgasr_priv - vga_switcheroo private data
0132  * @active: whether vga_switcheroo is enabled.
0133  *  Prerequisite is the registration of two GPUs and a handler
0134  * @delayed_switch_active: whether a delayed switch is pending
0135  * @delayed_client_id: client to which a delayed switch is pending
0136  * @debugfs_root: directory for vga_switcheroo debugfs interface
0137  * @registered_clients: number of registered GPUs
0138  *  (counting only vga clients, not audio clients)
0139  * @clients: list of registered clients
0140  * @handler: registered handler
0141  * @handler_flags: flags of registered handler
0142  * @mux_hw_lock: protects mux state
0143  *  (in particular while DDC lines are temporarily switched)
0144  * @old_ddc_owner: client to which DDC lines will be switched back on unlock
0145  *
0146  * vga_switcheroo private data. Currently only one vga_switcheroo instance
0147  * per system is supported.
0148  */
0149 struct vgasr_priv {
0150     bool active;
0151     bool delayed_switch_active;
0152     enum vga_switcheroo_client_id delayed_client_id;
0153 
0154     struct dentry *debugfs_root;
0155 
0156     int registered_clients;
0157     struct list_head clients;
0158 
0159     const struct vga_switcheroo_handler *handler;
0160     enum vga_switcheroo_handler_flags_t handler_flags;
0161     struct mutex mux_hw_lock;
0162     int old_ddc_owner;
0163 };
0164 
0165 #define ID_BIT_AUDIO        0x100
0166 #define client_is_audio(c)      ((c)->id & ID_BIT_AUDIO)
0167 #define client_is_vga(c)        (!client_is_audio(c))
0168 #define client_id(c)        ((c)->id & ~ID_BIT_AUDIO)
0169 
0170 static void vga_switcheroo_debugfs_init(struct vgasr_priv *priv);
0171 static void vga_switcheroo_debugfs_fini(struct vgasr_priv *priv);
0172 
0173 /* only one switcheroo per system */
0174 static struct vgasr_priv vgasr_priv = {
0175     .clients = LIST_HEAD_INIT(vgasr_priv.clients),
0176     .mux_hw_lock = __MUTEX_INITIALIZER(vgasr_priv.mux_hw_lock),
0177 };
0178 
0179 static bool vga_switcheroo_ready(void)
0180 {
0181     /* we're ready if we get two clients + handler */
0182     return !vgasr_priv.active &&
0183            vgasr_priv.registered_clients == 2 && vgasr_priv.handler;
0184 }
0185 
0186 static void vga_switcheroo_enable(void)
0187 {
0188     int ret;
0189     struct vga_switcheroo_client *client;
0190 
0191     /* call the handler to init */
0192     if (vgasr_priv.handler->init)
0193         vgasr_priv.handler->init();
0194 
0195     list_for_each_entry(client, &vgasr_priv.clients, list) {
0196         if (!client_is_vga(client) ||
0197              client_id(client) != VGA_SWITCHEROO_UNKNOWN_ID)
0198             continue;
0199 
0200         ret = vgasr_priv.handler->get_client_id(client->pdev);
0201         if (ret < 0)
0202             return;
0203 
0204         client->id = ret;
0205     }
0206 
0207     list_for_each_entry(client, &vgasr_priv.clients, list) {
0208         if (!client_is_audio(client) ||
0209              client_id(client) != VGA_SWITCHEROO_UNKNOWN_ID)
0210             continue;
0211 
0212         ret = vgasr_priv.handler->get_client_id(client->vga_dev);
0213         if (ret < 0)
0214             return;
0215 
0216         client->id = ret | ID_BIT_AUDIO;
0217         if (client->ops->gpu_bound)
0218             client->ops->gpu_bound(client->pdev, ret);
0219     }
0220 
0221     vga_switcheroo_debugfs_init(&vgasr_priv);
0222     vgasr_priv.active = true;
0223 }
0224 
0225 /**
0226  * vga_switcheroo_register_handler() - register handler
0227  * @handler: handler callbacks
0228  * @handler_flags: handler flags
0229  *
0230  * Register handler. Enable vga_switcheroo if two vga clients have already
0231  * registered.
0232  *
0233  * Return: 0 on success, -EINVAL if a handler was already registered.
0234  */
0235 int vga_switcheroo_register_handler(
0236               const struct vga_switcheroo_handler *handler,
0237               enum vga_switcheroo_handler_flags_t handler_flags)
0238 {
0239     mutex_lock(&vgasr_mutex);
0240     if (vgasr_priv.handler) {
0241         mutex_unlock(&vgasr_mutex);
0242         return -EINVAL;
0243     }
0244 
0245     vgasr_priv.handler = handler;
0246     vgasr_priv.handler_flags = handler_flags;
0247     if (vga_switcheroo_ready()) {
0248         pr_info("enabled\n");
0249         vga_switcheroo_enable();
0250     }
0251     mutex_unlock(&vgasr_mutex);
0252     return 0;
0253 }
0254 EXPORT_SYMBOL(vga_switcheroo_register_handler);
0255 
0256 /**
0257  * vga_switcheroo_unregister_handler() - unregister handler
0258  *
0259  * Unregister handler. Disable vga_switcheroo.
0260  */
0261 void vga_switcheroo_unregister_handler(void)
0262 {
0263     mutex_lock(&vgasr_mutex);
0264     mutex_lock(&vgasr_priv.mux_hw_lock);
0265     vgasr_priv.handler_flags = 0;
0266     vgasr_priv.handler = NULL;
0267     if (vgasr_priv.active) {
0268         pr_info("disabled\n");
0269         vga_switcheroo_debugfs_fini(&vgasr_priv);
0270         vgasr_priv.active = false;
0271     }
0272     mutex_unlock(&vgasr_priv.mux_hw_lock);
0273     mutex_unlock(&vgasr_mutex);
0274 }
0275 EXPORT_SYMBOL(vga_switcheroo_unregister_handler);
0276 
0277 /**
0278  * vga_switcheroo_handler_flags() - obtain handler flags
0279  *
0280  * Helper for clients to obtain the handler flags bitmask.
0281  *
0282  * Return: Handler flags. A value of 0 means that no handler is registered
0283  * or that the handler has no special capabilities.
0284  */
0285 enum vga_switcheroo_handler_flags_t vga_switcheroo_handler_flags(void)
0286 {
0287     return vgasr_priv.handler_flags;
0288 }
0289 EXPORT_SYMBOL(vga_switcheroo_handler_flags);
0290 
0291 static int register_client(struct pci_dev *pdev,
0292                const struct vga_switcheroo_client_ops *ops,
0293                enum vga_switcheroo_client_id id,
0294                struct pci_dev *vga_dev,
0295                bool active,
0296                bool driver_power_control)
0297 {
0298     struct vga_switcheroo_client *client;
0299 
0300     client = kzalloc(sizeof(*client), GFP_KERNEL);
0301     if (!client)
0302         return -ENOMEM;
0303 
0304     client->pwr_state = VGA_SWITCHEROO_ON;
0305     client->pdev = pdev;
0306     client->ops = ops;
0307     client->id = id;
0308     client->active = active;
0309     client->driver_power_control = driver_power_control;
0310     client->vga_dev = vga_dev;
0311 
0312     mutex_lock(&vgasr_mutex);
0313     list_add_tail(&client->list, &vgasr_priv.clients);
0314     if (client_is_vga(client))
0315         vgasr_priv.registered_clients++;
0316 
0317     if (vga_switcheroo_ready()) {
0318         pr_info("enabled\n");
0319         vga_switcheroo_enable();
0320     }
0321     mutex_unlock(&vgasr_mutex);
0322     return 0;
0323 }
0324 
0325 /**
0326  * vga_switcheroo_register_client - register vga client
0327  * @pdev: client pci device
0328  * @ops: client callbacks
0329  * @driver_power_control: whether power state is controlled by the driver's
0330  *  runtime pm
0331  *
0332  * Register vga client (GPU). Enable vga_switcheroo if another GPU and a
0333  * handler have already registered. The power state of the client is assumed
0334  * to be ON. Beforehand, vga_switcheroo_client_probe_defer() shall be called
0335  * to ensure that all prerequisites are met.
0336  *
0337  * Return: 0 on success, -ENOMEM on memory allocation error.
0338  */
0339 int vga_switcheroo_register_client(struct pci_dev *pdev,
0340                    const struct vga_switcheroo_client_ops *ops,
0341                    bool driver_power_control)
0342 {
0343     return register_client(pdev, ops, VGA_SWITCHEROO_UNKNOWN_ID, NULL,
0344                    pdev == vga_default_device(),
0345                    driver_power_control);
0346 }
0347 EXPORT_SYMBOL(vga_switcheroo_register_client);
0348 
0349 /**
0350  * vga_switcheroo_register_audio_client - register audio client
0351  * @pdev: client pci device
0352  * @ops: client callbacks
0353  * @vga_dev:  pci device which is bound to current audio client
0354  *
0355  * Register audio client (audio device on a GPU). The client is assumed
0356  * to use runtime PM. Beforehand, vga_switcheroo_client_probe_defer()
0357  * shall be called to ensure that all prerequisites are met.
0358  *
0359  * Return: 0 on success, -ENOMEM on memory allocation error, -EINVAL on getting
0360  * client id error.
0361  */
0362 int vga_switcheroo_register_audio_client(struct pci_dev *pdev,
0363             const struct vga_switcheroo_client_ops *ops,
0364             struct pci_dev *vga_dev)
0365 {
0366     enum vga_switcheroo_client_id id = VGA_SWITCHEROO_UNKNOWN_ID;
0367 
0368     /*
0369      * if vga_switcheroo has enabled, that mean two GPU clients and also
0370      * handler are registered. Get audio client id from bound GPU client
0371      * id directly, otherwise, set it as VGA_SWITCHEROO_UNKNOWN_ID,
0372      * it will set to correct id in later when vga_switcheroo_enable()
0373      * is called.
0374      */
0375     mutex_lock(&vgasr_mutex);
0376     if (vgasr_priv.active) {
0377         id = vgasr_priv.handler->get_client_id(vga_dev);
0378         if (id < 0) {
0379             mutex_unlock(&vgasr_mutex);
0380             return -EINVAL;
0381         }
0382         /* notify if GPU has been already bound */
0383         if (ops->gpu_bound)
0384             ops->gpu_bound(pdev, id);
0385     }
0386     mutex_unlock(&vgasr_mutex);
0387 
0388     return register_client(pdev, ops, id | ID_BIT_AUDIO, vga_dev,
0389                    false, true);
0390 }
0391 EXPORT_SYMBOL(vga_switcheroo_register_audio_client);
0392 
0393 static struct vga_switcheroo_client *
0394 find_client_from_pci(struct list_head *head, struct pci_dev *pdev)
0395 {
0396     struct vga_switcheroo_client *client;
0397 
0398     list_for_each_entry(client, head, list)
0399         if (client->pdev == pdev)
0400             return client;
0401     return NULL;
0402 }
0403 
0404 static struct vga_switcheroo_client *
0405 find_client_from_id(struct list_head *head,
0406             enum vga_switcheroo_client_id client_id)
0407 {
0408     struct vga_switcheroo_client *client;
0409 
0410     list_for_each_entry(client, head, list)
0411         if (client->id == client_id)
0412             return client;
0413     return NULL;
0414 }
0415 
0416 static struct vga_switcheroo_client *
0417 find_active_client(struct list_head *head)
0418 {
0419     struct vga_switcheroo_client *client;
0420 
0421     list_for_each_entry(client, head, list)
0422         if (client->active)
0423             return client;
0424     return NULL;
0425 }
0426 
0427 /**
0428  * vga_switcheroo_client_probe_defer() - whether to defer probing a given client
0429  * @pdev: client pci device
0430  *
0431  * Determine whether any prerequisites are not fulfilled to probe a given
0432  * client. Drivers shall invoke this early on in their ->probe callback
0433  * and return %-EPROBE_DEFER if it evaluates to %true. Thou shalt not
0434  * register the client ere thou hast called this.
0435  *
0436  * Return: %true if probing should be deferred, otherwise %false.
0437  */
0438 bool vga_switcheroo_client_probe_defer(struct pci_dev *pdev)
0439 {
0440     if ((pdev->class >> 16) == PCI_BASE_CLASS_DISPLAY) {
0441         /*
0442          * apple-gmux is needed on pre-retina MacBook Pro
0443          * to probe the panel if pdev is the inactive GPU.
0444          */
0445         if (apple_gmux_present() && pdev != vga_default_device() &&
0446             !vgasr_priv.handler_flags)
0447             return true;
0448     }
0449 
0450     return false;
0451 }
0452 EXPORT_SYMBOL(vga_switcheroo_client_probe_defer);
0453 
0454 static enum vga_switcheroo_state
0455 vga_switcheroo_pwr_state(struct vga_switcheroo_client *client)
0456 {
0457     if (client->driver_power_control)
0458         if (pm_runtime_enabled(&client->pdev->dev) &&
0459             pm_runtime_active(&client->pdev->dev))
0460             return VGA_SWITCHEROO_ON;
0461         else
0462             return VGA_SWITCHEROO_OFF;
0463     else
0464         return client->pwr_state;
0465 }
0466 
0467 /**
0468  * vga_switcheroo_get_client_state() - obtain power state of a given client
0469  * @pdev: client pci device
0470  *
0471  * Obtain power state of a given client as seen from vga_switcheroo.
0472  * The function is only called from hda_intel.c.
0473  *
0474  * Return: Power state.
0475  */
0476 enum vga_switcheroo_state vga_switcheroo_get_client_state(struct pci_dev *pdev)
0477 {
0478     struct vga_switcheroo_client *client;
0479     enum vga_switcheroo_state ret;
0480 
0481     mutex_lock(&vgasr_mutex);
0482     client = find_client_from_pci(&vgasr_priv.clients, pdev);
0483     if (!client)
0484         ret = VGA_SWITCHEROO_NOT_FOUND;
0485     else
0486         ret = vga_switcheroo_pwr_state(client);
0487     mutex_unlock(&vgasr_mutex);
0488     return ret;
0489 }
0490 EXPORT_SYMBOL(vga_switcheroo_get_client_state);
0491 
0492 /**
0493  * vga_switcheroo_unregister_client() - unregister client
0494  * @pdev: client pci device
0495  *
0496  * Unregister client. Disable vga_switcheroo if this is a vga client (GPU).
0497  */
0498 void vga_switcheroo_unregister_client(struct pci_dev *pdev)
0499 {
0500     struct vga_switcheroo_client *client;
0501 
0502     mutex_lock(&vgasr_mutex);
0503     client = find_client_from_pci(&vgasr_priv.clients, pdev);
0504     if (client) {
0505         if (client_is_vga(client))
0506             vgasr_priv.registered_clients--;
0507         list_del(&client->list);
0508         kfree(client);
0509     }
0510     if (vgasr_priv.active && vgasr_priv.registered_clients < 2) {
0511         pr_info("disabled\n");
0512         vga_switcheroo_debugfs_fini(&vgasr_priv);
0513         vgasr_priv.active = false;
0514     }
0515     mutex_unlock(&vgasr_mutex);
0516 }
0517 EXPORT_SYMBOL(vga_switcheroo_unregister_client);
0518 
0519 /**
0520  * vga_switcheroo_client_fb_set() - set framebuffer of a given client
0521  * @pdev: client pci device
0522  * @info: framebuffer
0523  *
0524  * Set framebuffer of a given client. The console will be remapped to this
0525  * on switching.
0526  */
0527 void vga_switcheroo_client_fb_set(struct pci_dev *pdev,
0528                  struct fb_info *info)
0529 {
0530     struct vga_switcheroo_client *client;
0531 
0532     mutex_lock(&vgasr_mutex);
0533     client = find_client_from_pci(&vgasr_priv.clients, pdev);
0534     if (client)
0535         client->fb_info = info;
0536     mutex_unlock(&vgasr_mutex);
0537 }
0538 EXPORT_SYMBOL(vga_switcheroo_client_fb_set);
0539 
0540 /**
0541  * vga_switcheroo_lock_ddc() - temporarily switch DDC lines to a given client
0542  * @pdev: client pci device
0543  *
0544  * Temporarily switch DDC lines to the client identified by @pdev
0545  * (but leave the outputs otherwise switched to where they are).
0546  * This allows the inactive client to probe EDID. The DDC lines must
0547  * afterwards be switched back by calling vga_switcheroo_unlock_ddc(),
0548  * even if this function returns an error.
0549  *
0550  * Return: Previous DDC owner on success or a negative int on error.
0551  * Specifically, %-ENODEV if no handler has registered or if the handler
0552  * does not support switching the DDC lines. Also, a negative value
0553  * returned by the handler is propagated back to the caller.
0554  * The return value has merely an informational purpose for any caller
0555  * which might be interested in it. It is acceptable to ignore the return
0556  * value and simply rely on the result of the subsequent EDID probe,
0557  * which will be %NULL if DDC switching failed.
0558  */
0559 int vga_switcheroo_lock_ddc(struct pci_dev *pdev)
0560 {
0561     enum vga_switcheroo_client_id id;
0562 
0563     mutex_lock(&vgasr_priv.mux_hw_lock);
0564     if (!vgasr_priv.handler || !vgasr_priv.handler->switch_ddc) {
0565         vgasr_priv.old_ddc_owner = -ENODEV;
0566         return -ENODEV;
0567     }
0568 
0569     id = vgasr_priv.handler->get_client_id(pdev);
0570     vgasr_priv.old_ddc_owner = vgasr_priv.handler->switch_ddc(id);
0571     return vgasr_priv.old_ddc_owner;
0572 }
0573 EXPORT_SYMBOL(vga_switcheroo_lock_ddc);
0574 
0575 /**
0576  * vga_switcheroo_unlock_ddc() - switch DDC lines back to previous owner
0577  * @pdev: client pci device
0578  *
0579  * Switch DDC lines back to the previous owner after calling
0580  * vga_switcheroo_lock_ddc(). This must be called even if
0581  * vga_switcheroo_lock_ddc() returned an error.
0582  *
0583  * Return: Previous DDC owner on success (i.e. the client identifier of @pdev)
0584  * or a negative int on error.
0585  * Specifically, %-ENODEV if no handler has registered or if the handler
0586  * does not support switching the DDC lines. Also, a negative value
0587  * returned by the handler is propagated back to the caller.
0588  * Finally, invoking this function without calling vga_switcheroo_lock_ddc()
0589  * first is not allowed and will result in %-EINVAL.
0590  */
0591 int vga_switcheroo_unlock_ddc(struct pci_dev *pdev)
0592 {
0593     enum vga_switcheroo_client_id id;
0594     int ret = vgasr_priv.old_ddc_owner;
0595 
0596     if (WARN_ON_ONCE(!mutex_is_locked(&vgasr_priv.mux_hw_lock)))
0597         return -EINVAL;
0598 
0599     if (vgasr_priv.old_ddc_owner >= 0) {
0600         id = vgasr_priv.handler->get_client_id(pdev);
0601         if (vgasr_priv.old_ddc_owner != id)
0602             ret = vgasr_priv.handler->switch_ddc(
0603                              vgasr_priv.old_ddc_owner);
0604     }
0605     mutex_unlock(&vgasr_priv.mux_hw_lock);
0606     return ret;
0607 }
0608 EXPORT_SYMBOL(vga_switcheroo_unlock_ddc);
0609 
0610 /**
0611  * DOC: Manual switching and manual power control
0612  *
0613  * In this mode of use, the file /sys/kernel/debug/vgaswitcheroo/switch
0614  * can be read to retrieve the current vga_switcheroo state and commands
0615  * can be written to it to change the state. The file appears as soon as
0616  * two GPU drivers and one handler have registered with vga_switcheroo.
0617  * The following commands are understood:
0618  *
0619  * * OFF: Power off the device not in use.
0620  * * ON: Power on the device not in use.
0621  * * IGD: Switch to the integrated graphics device.
0622  *   Power on the integrated GPU if necessary, power off the discrete GPU.
0623  *   Prerequisite is that no user space processes (e.g. Xorg, alsactl)
0624  *   have opened device files of the GPUs or the audio client. If the
0625  *   switch fails, the user may invoke lsof(8) or fuser(1) on /dev/dri/
0626  *   and /dev/snd/controlC1 to identify processes blocking the switch.
0627  * * DIS: Switch to the discrete graphics device.
0628  * * DIGD: Delayed switch to the integrated graphics device.
0629  *   This will perform the switch once the last user space process has
0630  *   closed the device files of the GPUs and the audio client.
0631  * * DDIS: Delayed switch to the discrete graphics device.
0632  * * MIGD: Mux-only switch to the integrated graphics device.
0633  *   Does not remap console or change the power state of either gpu.
0634  *   If the integrated GPU is currently off, the screen will turn black.
0635  *   If it is on, the screen will show whatever happens to be in VRAM.
0636  *   Either way, the user has to blindly enter the command to switch back.
0637  * * MDIS: Mux-only switch to the discrete graphics device.
0638  *
0639  * For GPUs whose power state is controlled by the driver's runtime pm,
0640  * the ON and OFF commands are a no-op (see next section).
0641  *
0642  * For muxless machines, the IGD/DIS, DIGD/DDIS and MIGD/MDIS commands
0643  * should not be used.
0644  */
0645 
0646 static int vga_switcheroo_show(struct seq_file *m, void *v)
0647 {
0648     struct vga_switcheroo_client *client;
0649     int i = 0;
0650 
0651     mutex_lock(&vgasr_mutex);
0652     list_for_each_entry(client, &vgasr_priv.clients, list) {
0653         seq_printf(m, "%d:%s%s:%c:%s%s:%s\n", i,
0654                client_id(client) == VGA_SWITCHEROO_DIS ? "DIS" :
0655                                      "IGD",
0656                client_is_vga(client) ? "" : "-Audio",
0657                client->active ? '+' : ' ',
0658                client->driver_power_control ? "Dyn" : "",
0659                vga_switcheroo_pwr_state(client) ? "Pwr" : "Off",
0660                pci_name(client->pdev));
0661         i++;
0662     }
0663     mutex_unlock(&vgasr_mutex);
0664     return 0;
0665 }
0666 
0667 static int vga_switcheroo_debugfs_open(struct inode *inode, struct file *file)
0668 {
0669     return single_open(file, vga_switcheroo_show, NULL);
0670 }
0671 
0672 static int vga_switchon(struct vga_switcheroo_client *client)
0673 {
0674     if (client->driver_power_control)
0675         return 0;
0676     if (vgasr_priv.handler->power_state)
0677         vgasr_priv.handler->power_state(client->id, VGA_SWITCHEROO_ON);
0678     /* call the driver callback to turn on device */
0679     client->ops->set_gpu_state(client->pdev, VGA_SWITCHEROO_ON);
0680     client->pwr_state = VGA_SWITCHEROO_ON;
0681     return 0;
0682 }
0683 
0684 static int vga_switchoff(struct vga_switcheroo_client *client)
0685 {
0686     if (client->driver_power_control)
0687         return 0;
0688     /* call the driver callback to turn off device */
0689     client->ops->set_gpu_state(client->pdev, VGA_SWITCHEROO_OFF);
0690     if (vgasr_priv.handler->power_state)
0691         vgasr_priv.handler->power_state(client->id, VGA_SWITCHEROO_OFF);
0692     client->pwr_state = VGA_SWITCHEROO_OFF;
0693     return 0;
0694 }
0695 
0696 static void set_audio_state(enum vga_switcheroo_client_id id,
0697                 enum vga_switcheroo_state state)
0698 {
0699     struct vga_switcheroo_client *client;
0700 
0701     client = find_client_from_id(&vgasr_priv.clients, id | ID_BIT_AUDIO);
0702     if (client)
0703         client->ops->set_gpu_state(client->pdev, state);
0704 }
0705 
0706 /* stage one happens before delay */
0707 static int vga_switchto_stage1(struct vga_switcheroo_client *new_client)
0708 {
0709     struct vga_switcheroo_client *active;
0710 
0711     active = find_active_client(&vgasr_priv.clients);
0712     if (!active)
0713         return 0;
0714 
0715     if (vga_switcheroo_pwr_state(new_client) == VGA_SWITCHEROO_OFF)
0716         vga_switchon(new_client);
0717 
0718     vga_set_default_device(new_client->pdev);
0719     return 0;
0720 }
0721 
0722 /* post delay */
0723 static int vga_switchto_stage2(struct vga_switcheroo_client *new_client)
0724 {
0725     int ret;
0726     struct vga_switcheroo_client *active;
0727 
0728     active = find_active_client(&vgasr_priv.clients);
0729     if (!active)
0730         return 0;
0731 
0732     active->active = false;
0733 
0734     /* let HDA controller autosuspend if GPU uses driver power control */
0735     if (!active->driver_power_control)
0736         set_audio_state(active->id, VGA_SWITCHEROO_OFF);
0737 
0738     if (new_client->fb_info)
0739         fbcon_remap_all(new_client->fb_info);
0740 
0741     mutex_lock(&vgasr_priv.mux_hw_lock);
0742     ret = vgasr_priv.handler->switchto(new_client->id);
0743     mutex_unlock(&vgasr_priv.mux_hw_lock);
0744     if (ret)
0745         return ret;
0746 
0747     if (new_client->ops->reprobe)
0748         new_client->ops->reprobe(new_client->pdev);
0749 
0750     if (vga_switcheroo_pwr_state(active) == VGA_SWITCHEROO_ON)
0751         vga_switchoff(active);
0752 
0753     /* let HDA controller autoresume if GPU uses driver power control */
0754     if (!new_client->driver_power_control)
0755         set_audio_state(new_client->id, VGA_SWITCHEROO_ON);
0756 
0757     new_client->active = true;
0758     return 0;
0759 }
0760 
0761 static bool check_can_switch(void)
0762 {
0763     struct vga_switcheroo_client *client;
0764 
0765     list_for_each_entry(client, &vgasr_priv.clients, list) {
0766         if (!client->ops->can_switch(client->pdev)) {
0767             pr_err("client %x refused switch\n", client->id);
0768             return false;
0769         }
0770     }
0771     return true;
0772 }
0773 
0774 static ssize_t
0775 vga_switcheroo_debugfs_write(struct file *filp, const char __user *ubuf,
0776                  size_t cnt, loff_t *ppos)
0777 {
0778     char usercmd[64];
0779     int ret;
0780     bool delay = false, can_switch;
0781     bool just_mux = false;
0782     enum vga_switcheroo_client_id client_id = VGA_SWITCHEROO_UNKNOWN_ID;
0783     struct vga_switcheroo_client *client = NULL;
0784 
0785     if (cnt > 63)
0786         cnt = 63;
0787 
0788     if (copy_from_user(usercmd, ubuf, cnt))
0789         return -EFAULT;
0790 
0791     mutex_lock(&vgasr_mutex);
0792 
0793     if (!vgasr_priv.active) {
0794         cnt = -EINVAL;
0795         goto out;
0796     }
0797 
0798     /* pwr off the device not in use */
0799     if (strncmp(usercmd, "OFF", 3) == 0) {
0800         list_for_each_entry(client, &vgasr_priv.clients, list) {
0801             if (client->active || client_is_audio(client))
0802                 continue;
0803             if (client->driver_power_control)
0804                 continue;
0805             set_audio_state(client->id, VGA_SWITCHEROO_OFF);
0806             if (client->pwr_state == VGA_SWITCHEROO_ON)
0807                 vga_switchoff(client);
0808         }
0809         goto out;
0810     }
0811     /* pwr on the device not in use */
0812     if (strncmp(usercmd, "ON", 2) == 0) {
0813         list_for_each_entry(client, &vgasr_priv.clients, list) {
0814             if (client->active || client_is_audio(client))
0815                 continue;
0816             if (client->driver_power_control)
0817                 continue;
0818             if (client->pwr_state == VGA_SWITCHEROO_OFF)
0819                 vga_switchon(client);
0820             set_audio_state(client->id, VGA_SWITCHEROO_ON);
0821         }
0822         goto out;
0823     }
0824 
0825     /* request a delayed switch - test can we switch now */
0826     if (strncmp(usercmd, "DIGD", 4) == 0) {
0827         client_id = VGA_SWITCHEROO_IGD;
0828         delay = true;
0829     }
0830 
0831     if (strncmp(usercmd, "DDIS", 4) == 0) {
0832         client_id = VGA_SWITCHEROO_DIS;
0833         delay = true;
0834     }
0835 
0836     if (strncmp(usercmd, "IGD", 3) == 0)
0837         client_id = VGA_SWITCHEROO_IGD;
0838 
0839     if (strncmp(usercmd, "DIS", 3) == 0)
0840         client_id = VGA_SWITCHEROO_DIS;
0841 
0842     if (strncmp(usercmd, "MIGD", 4) == 0) {
0843         just_mux = true;
0844         client_id = VGA_SWITCHEROO_IGD;
0845     }
0846     if (strncmp(usercmd, "MDIS", 4) == 0) {
0847         just_mux = true;
0848         client_id = VGA_SWITCHEROO_DIS;
0849     }
0850 
0851     if (client_id == VGA_SWITCHEROO_UNKNOWN_ID)
0852         goto out;
0853     client = find_client_from_id(&vgasr_priv.clients, client_id);
0854     if (!client)
0855         goto out;
0856 
0857     vgasr_priv.delayed_switch_active = false;
0858 
0859     if (just_mux) {
0860         mutex_lock(&vgasr_priv.mux_hw_lock);
0861         ret = vgasr_priv.handler->switchto(client_id);
0862         mutex_unlock(&vgasr_priv.mux_hw_lock);
0863         goto out;
0864     }
0865 
0866     if (client->active)
0867         goto out;
0868 
0869     /* okay we want a switch - test if devices are willing to switch */
0870     can_switch = check_can_switch();
0871 
0872     if (can_switch == false && delay == false)
0873         goto out;
0874 
0875     if (can_switch) {
0876         ret = vga_switchto_stage1(client);
0877         if (ret)
0878             pr_err("switching failed stage 1 %d\n", ret);
0879 
0880         ret = vga_switchto_stage2(client);
0881         if (ret)
0882             pr_err("switching failed stage 2 %d\n", ret);
0883 
0884     } else {
0885         pr_info("setting delayed switch to client %d\n", client->id);
0886         vgasr_priv.delayed_switch_active = true;
0887         vgasr_priv.delayed_client_id = client_id;
0888 
0889         ret = vga_switchto_stage1(client);
0890         if (ret)
0891             pr_err("delayed switching stage 1 failed %d\n", ret);
0892     }
0893 
0894 out:
0895     mutex_unlock(&vgasr_mutex);
0896     return cnt;
0897 }
0898 
0899 static const struct file_operations vga_switcheroo_debugfs_fops = {
0900     .owner = THIS_MODULE,
0901     .open = vga_switcheroo_debugfs_open,
0902     .write = vga_switcheroo_debugfs_write,
0903     .read = seq_read,
0904     .llseek = seq_lseek,
0905     .release = single_release,
0906 };
0907 
0908 static void vga_switcheroo_debugfs_fini(struct vgasr_priv *priv)
0909 {
0910     debugfs_remove_recursive(priv->debugfs_root);
0911     priv->debugfs_root = NULL;
0912 }
0913 
0914 static void vga_switcheroo_debugfs_init(struct vgasr_priv *priv)
0915 {
0916     /* already initialised */
0917     if (priv->debugfs_root)
0918         return;
0919 
0920     priv->debugfs_root = debugfs_create_dir("vgaswitcheroo", NULL);
0921 
0922     debugfs_create_file("switch", 0644, priv->debugfs_root, NULL,
0923                 &vga_switcheroo_debugfs_fops);
0924 }
0925 
0926 /**
0927  * vga_switcheroo_process_delayed_switch() - helper for delayed switching
0928  *
0929  * Process a delayed switch if one is pending. DRM drivers should call this
0930  * from their ->lastclose callback.
0931  *
0932  * Return: 0 on success. -EINVAL if no delayed switch is pending, if the client
0933  * has unregistered in the meantime or if there are other clients blocking the
0934  * switch. If the actual switch fails, an error is reported and 0 is returned.
0935  */
0936 int vga_switcheroo_process_delayed_switch(void)
0937 {
0938     struct vga_switcheroo_client *client;
0939     int ret;
0940     int err = -EINVAL;
0941 
0942     mutex_lock(&vgasr_mutex);
0943     if (!vgasr_priv.delayed_switch_active)
0944         goto err;
0945 
0946     pr_info("processing delayed switch to %d\n",
0947         vgasr_priv.delayed_client_id);
0948 
0949     client = find_client_from_id(&vgasr_priv.clients,
0950                      vgasr_priv.delayed_client_id);
0951     if (!client || !check_can_switch())
0952         goto err;
0953 
0954     ret = vga_switchto_stage2(client);
0955     if (ret)
0956         pr_err("delayed switching failed stage 2 %d\n", ret);
0957 
0958     vgasr_priv.delayed_switch_active = false;
0959     err = 0;
0960 err:
0961     mutex_unlock(&vgasr_mutex);
0962     return err;
0963 }
0964 EXPORT_SYMBOL(vga_switcheroo_process_delayed_switch);
0965 
0966 /**
0967  * DOC: Driver power control
0968  *
0969  * In this mode of use, the discrete GPU automatically powers up and down at
0970  * the discretion of the driver's runtime pm. On muxed machines, the user may
0971  * still influence the muxer state by way of the debugfs interface, however
0972  * the ON and OFF commands become a no-op for the discrete GPU.
0973  *
0974  * This mode is the default on Nvidia HybridPower/Optimus and ATI PowerXpress.
0975  * Specifying nouveau.runpm=0, radeon.runpm=0 or amdgpu.runpm=0 on the kernel
0976  * command line disables it.
0977  *
0978  * After the GPU has been suspended, the handler needs to be called to cut
0979  * power to the GPU. Likewise it needs to reinstate power before the GPU
0980  * can resume. This is achieved by vga_switcheroo_init_domain_pm_ops(),
0981  * which augments the GPU's suspend/resume functions by the requisite
0982  * calls to the handler.
0983  *
0984  * When the audio device resumes, the GPU needs to be woken. This is achieved
0985  * by a PCI quirk which calls device_link_add() to declare a dependency on the
0986  * GPU. That way, the GPU is kept awake whenever and as long as the audio
0987  * device is in use.
0988  *
0989  * On muxed machines, if the mux is initially switched to the discrete GPU,
0990  * the user ends up with a black screen when the GPU powers down after boot.
0991  * As a workaround, the mux is forced to the integrated GPU on runtime suspend,
0992  * cf. https://bugs.freedesktop.org/show_bug.cgi?id=75917
0993  */
0994 
0995 static void vga_switcheroo_power_switch(struct pci_dev *pdev,
0996                     enum vga_switcheroo_state state)
0997 {
0998     struct vga_switcheroo_client *client;
0999 
1000     if (!vgasr_priv.handler->power_state)
1001         return;
1002 
1003     client = find_client_from_pci(&vgasr_priv.clients, pdev);
1004     if (!client)
1005         return;
1006 
1007     if (!client->driver_power_control)
1008         return;
1009 
1010     vgasr_priv.handler->power_state(client->id, state);
1011 }
1012 
1013 /* switcheroo power domain */
1014 static int vga_switcheroo_runtime_suspend(struct device *dev)
1015 {
1016     struct pci_dev *pdev = to_pci_dev(dev);
1017     int ret;
1018 
1019     ret = dev->bus->pm->runtime_suspend(dev);
1020     if (ret)
1021         return ret;
1022     mutex_lock(&vgasr_mutex);
1023     if (vgasr_priv.handler->switchto) {
1024         mutex_lock(&vgasr_priv.mux_hw_lock);
1025         vgasr_priv.handler->switchto(VGA_SWITCHEROO_IGD);
1026         mutex_unlock(&vgasr_priv.mux_hw_lock);
1027     }
1028     pci_bus_set_current_state(pdev->bus, PCI_D3cold);
1029     vga_switcheroo_power_switch(pdev, VGA_SWITCHEROO_OFF);
1030     mutex_unlock(&vgasr_mutex);
1031     return 0;
1032 }
1033 
1034 static int vga_switcheroo_runtime_resume(struct device *dev)
1035 {
1036     struct pci_dev *pdev = to_pci_dev(dev);
1037 
1038     mutex_lock(&vgasr_mutex);
1039     vga_switcheroo_power_switch(pdev, VGA_SWITCHEROO_ON);
1040     mutex_unlock(&vgasr_mutex);
1041     pci_resume_bus(pdev->bus);
1042     return dev->bus->pm->runtime_resume(dev);
1043 }
1044 
1045 /**
1046  * vga_switcheroo_init_domain_pm_ops() - helper for driver power control
1047  * @dev: vga client device
1048  * @domain: power domain
1049  *
1050  * Helper for GPUs whose power state is controlled by the driver's runtime pm.
1051  * After the GPU has been suspended, the handler needs to be called to cut
1052  * power to the GPU. Likewise it needs to reinstate power before the GPU
1053  * can resume. To this end, this helper augments the suspend/resume functions
1054  * by the requisite calls to the handler. It needs only be called on platforms
1055  * where the power switch is separate to the device being powered down.
1056  */
1057 int vga_switcheroo_init_domain_pm_ops(struct device *dev,
1058                       struct dev_pm_domain *domain)
1059 {
1060     /* copy over all the bus versions */
1061     if (dev->bus && dev->bus->pm) {
1062         domain->ops = *dev->bus->pm;
1063         domain->ops.runtime_suspend = vga_switcheroo_runtime_suspend;
1064         domain->ops.runtime_resume = vga_switcheroo_runtime_resume;
1065 
1066         dev_pm_domain_set(dev, domain);
1067         return 0;
1068     }
1069     dev_pm_domain_set(dev, NULL);
1070     return -EINVAL;
1071 }
1072 EXPORT_SYMBOL(vga_switcheroo_init_domain_pm_ops);
1073 
1074 void vga_switcheroo_fini_domain_pm_ops(struct device *dev)
1075 {
1076     dev_pm_domain_set(dev, NULL);
1077 }
1078 EXPORT_SYMBOL(vga_switcheroo_fini_domain_pm_ops);