2010-02-01 12:38:10 +07:00
|
|
|
/*
|
2015-08-23 20:18:55 +07:00
|
|
|
* vga_switcheroo.c - Support for laptop with dual GPU using one set of outputs
|
|
|
|
*
|
2010-02-01 12:38:10 +07:00
|
|
|
* Copyright (c) 2010 Red Hat Inc.
|
|
|
|
* Author : Dave Airlie <airlied@redhat.com>
|
|
|
|
*
|
2015-08-23 20:18:55 +07:00
|
|
|
* Copyright (c) 2015 Lukas Wunner <lukas@wunner.de>
|
2010-02-01 12:38:10 +07:00
|
|
|
*
|
2015-08-23 20:18:55 +07:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
2010-02-01 12:38:10 +07:00
|
|
|
*
|
2015-08-23 20:18:55 +07:00
|
|
|
* The above copyright notice and this permission notice (including the next
|
|
|
|
* paragraph) shall be included in all copies or substantial portions of the
|
|
|
|
* Software.
|
2015-08-12 21:32:09 +07:00
|
|
|
*
|
2015-08-23 20:18:55 +07:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
* DEALINGS
|
|
|
|
* IN THE SOFTWARE.
|
2015-08-12 21:32:09 +07:00
|
|
|
*
|
2010-02-01 12:38:10 +07:00
|
|
|
*/
|
|
|
|
|
2015-08-12 21:32:10 +07:00
|
|
|
#define pr_fmt(fmt) "vga_switcheroo: " fmt
|
|
|
|
|
2015-09-05 18:40:23 +07:00
|
|
|
#include <linux/console.h>
|
2010-02-01 12:38:10 +07:00
|
|
|
#include <linux/debugfs.h>
|
|
|
|
#include <linux/fb.h>
|
2015-09-05 18:40:23 +07:00
|
|
|
#include <linux/fs.h>
|
|
|
|
#include <linux/module.h>
|
2010-02-01 12:38:10 +07:00
|
|
|
#include <linux/pci.h>
|
2012-09-10 09:28:36 +07:00
|
|
|
#include <linux/pm_runtime.h>
|
2015-09-05 18:40:23 +07:00
|
|
|
#include <linux/seq_file.h>
|
|
|
|
#include <linux/uaccess.h>
|
2012-04-17 03:26:03 +07:00
|
|
|
#include <linux/vgaarb.h>
|
2015-09-05 18:40:23 +07:00
|
|
|
#include <linux/vga_switcheroo.h>
|
2012-04-17 03:26:03 +07:00
|
|
|
|
2015-08-23 20:18:55 +07:00
|
|
|
/**
|
|
|
|
* DOC: Overview
|
|
|
|
*
|
|
|
|
* vga_switcheroo is the Linux subsystem for laptop hybrid graphics.
|
|
|
|
* These come in two flavors:
|
|
|
|
*
|
|
|
|
* * muxed: Dual GPUs with a multiplexer chip to switch outputs between GPUs.
|
|
|
|
* * muxless: Dual GPUs but only one of them is connected to outputs.
|
|
|
|
* The other one is merely used to offload rendering, its results
|
|
|
|
* are copied over PCIe into the framebuffer. On Linux this is
|
|
|
|
* supported with DRI PRIME.
|
|
|
|
*
|
|
|
|
* Hybrid graphics started to appear in the late Naughties and were initially
|
|
|
|
* all muxed. Newer laptops moved to a muxless architecture for cost reasons.
|
|
|
|
* A notable exception is the MacBook Pro which continues to use a mux.
|
|
|
|
* Muxes come with varying capabilities: Some switch only the panel, others
|
|
|
|
* can also switch external displays. Some switch all display pins at once
|
|
|
|
* while others can switch just the DDC lines. (To allow EDID probing
|
|
|
|
* for the inactive GPU.) Also, muxes are often used to cut power to the
|
|
|
|
* discrete GPU while it is not used.
|
|
|
|
*
|
|
|
|
* DRM drivers register GPUs with vga_switcheroo, these are heretoforth called
|
|
|
|
* clients. The mux is called the handler. Muxless machines also register a
|
|
|
|
* handler to control the power state of the discrete GPU, its ->switchto
|
|
|
|
* callback is a no-op for obvious reasons. The discrete GPU is often equipped
|
|
|
|
* with an HDA controller for the HDMI/DP audio signal, this will also
|
|
|
|
* register as a client so that vga_switcheroo can take care of the correct
|
|
|
|
* suspend/resume order when changing the discrete GPU's power state. In total
|
|
|
|
* there can thus be up to three clients: Two vga clients (GPUs) and one audio
|
|
|
|
* client (on the discrete GPU). The code is mostly prepared to support
|
|
|
|
* machines with more than two GPUs should they become available.
|
|
|
|
* The GPU to which the outputs are currently switched is called the
|
|
|
|
* active client in vga_switcheroo parlance. The GPU not in use is the
|
|
|
|
* inactive client.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* struct vga_switcheroo_client - registered client
|
|
|
|
* @pdev: client pci device
|
|
|
|
* @fb_info: framebuffer to which console is remapped on switching
|
|
|
|
* @pwr_state: current power state
|
|
|
|
* @ops: client callbacks
|
|
|
|
* @id: client identifier, see enum vga_switcheroo_client_id.
|
|
|
|
* Determining the id requires the handler, so GPUs are initially
|
|
|
|
* assigned -1 and later given their true id in vga_switcheroo_enable()
|
|
|
|
* @active: whether the outputs are currently switched to this client
|
|
|
|
* @driver_power_control: whether power state is controlled by the driver's
|
|
|
|
* runtime pm. If true, writing ON and OFF to the vga_switcheroo debugfs
|
|
|
|
* interface is a no-op so as not to interfere with runtime pm
|
|
|
|
* @list: client list
|
|
|
|
*
|
|
|
|
* Registered client. A client can be either a GPU or an audio device on a GPU.
|
|
|
|
* For audio clients, the @fb_info, @active and @driver_power_control members
|
|
|
|
* are bogus.
|
|
|
|
*/
|
2010-02-01 12:38:10 +07:00
|
|
|
struct vga_switcheroo_client {
|
|
|
|
struct pci_dev *pdev;
|
|
|
|
struct fb_info *fb_info;
|
2015-08-28 16:56:26 +07:00
|
|
|
enum vga_switcheroo_state pwr_state;
|
2012-05-11 12:51:17 +07:00
|
|
|
const struct vga_switcheroo_client_ops *ops;
|
2010-02-01 12:38:10 +07:00
|
|
|
int id;
|
|
|
|
bool active;
|
2012-09-10 09:28:36 +07:00
|
|
|
bool driver_power_control;
|
2012-04-26 17:55:59 +07:00
|
|
|
struct list_head list;
|
2010-02-01 12:38:10 +07:00
|
|
|
};
|
|
|
|
|
2015-08-23 20:18:55 +07:00
|
|
|
/*
|
|
|
|
* protects access to struct vgasr_priv
|
|
|
|
*/
|
2010-02-01 12:38:10 +07:00
|
|
|
static DEFINE_MUTEX(vgasr_mutex);
|
|
|
|
|
2015-08-23 20:18:55 +07:00
|
|
|
/**
|
|
|
|
* struct vgasr_priv - vga_switcheroo private data
|
|
|
|
* @active: whether vga_switcheroo is enabled.
|
|
|
|
* Prerequisite is the registration of two GPUs and a handler
|
|
|
|
* @delayed_switch_active: whether a delayed switch is pending
|
|
|
|
* @delayed_client_id: client to which a delayed switch is pending
|
|
|
|
* @debugfs_root: directory for vga_switcheroo debugfs interface
|
|
|
|
* @switch_file: file for vga_switcheroo debugfs interface
|
|
|
|
* @registered_clients: number of registered GPUs
|
|
|
|
* (counting only vga clients, not audio clients)
|
|
|
|
* @clients: list of registered clients
|
|
|
|
* @handler: registered handler
|
|
|
|
*
|
|
|
|
* vga_switcheroo private data. Currently only one vga_switcheroo instance
|
|
|
|
* per system is supported.
|
|
|
|
*/
|
2010-02-01 12:38:10 +07:00
|
|
|
struct vgasr_priv {
|
|
|
|
bool active;
|
|
|
|
bool delayed_switch_active;
|
|
|
|
enum vga_switcheroo_client_id delayed_client_id;
|
|
|
|
|
|
|
|
struct dentry *debugfs_root;
|
|
|
|
struct dentry *switch_file;
|
|
|
|
|
|
|
|
int registered_clients;
|
2012-04-26 17:55:59 +07:00
|
|
|
struct list_head clients;
|
2010-02-01 12:38:10 +07:00
|
|
|
|
|
|
|
struct vga_switcheroo_handler *handler;
|
|
|
|
};
|
|
|
|
|
2012-04-26 19:29:48 +07:00
|
|
|
#define ID_BIT_AUDIO 0x100
|
|
|
|
#define client_is_audio(c) ((c)->id & ID_BIT_AUDIO)
|
|
|
|
#define client_is_vga(c) ((c)->id == -1 || !client_is_audio(c))
|
|
|
|
#define client_id(c) ((c)->id & ~ID_BIT_AUDIO)
|
|
|
|
|
2010-02-01 12:38:10 +07:00
|
|
|
static int vga_switcheroo_debugfs_init(struct vgasr_priv *priv);
|
|
|
|
static void vga_switcheroo_debugfs_fini(struct vgasr_priv *priv);
|
|
|
|
|
|
|
|
/* only one switcheroo per system */
|
2012-04-26 17:55:59 +07:00
|
|
|
static struct vgasr_priv vgasr_priv = {
|
|
|
|
.clients = LIST_HEAD_INIT(vgasr_priv.clients),
|
|
|
|
};
|
2010-02-01 12:38:10 +07:00
|
|
|
|
2012-08-17 23:17:03 +07:00
|
|
|
static bool vga_switcheroo_ready(void)
|
2010-02-01 12:38:10 +07:00
|
|
|
{
|
2012-08-17 23:17:03 +07:00
|
|
|
/* we're ready if we get two clients + handler */
|
|
|
|
return !vgasr_priv.active &&
|
|
|
|
vgasr_priv.registered_clients == 2 && vgasr_priv.handler;
|
2010-02-01 12:38:10 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void vga_switcheroo_enable(void)
|
|
|
|
{
|
|
|
|
int ret;
|
2012-04-26 17:55:59 +07:00
|
|
|
struct vga_switcheroo_client *client;
|
|
|
|
|
2010-02-01 12:38:10 +07:00
|
|
|
/* call the handler to init */
|
2012-08-17 23:17:02 +07:00
|
|
|
if (vgasr_priv.handler->init)
|
|
|
|
vgasr_priv.handler->init();
|
2010-02-01 12:38:10 +07:00
|
|
|
|
2012-04-26 17:55:59 +07:00
|
|
|
list_for_each_entry(client, &vgasr_priv.clients, list) {
|
2012-04-26 19:29:48 +07:00
|
|
|
if (client->id != -1)
|
|
|
|
continue;
|
2012-04-26 17:55:59 +07:00
|
|
|
ret = vgasr_priv.handler->get_client_id(client->pdev);
|
2010-02-01 12:38:10 +07:00
|
|
|
if (ret < 0)
|
|
|
|
return;
|
|
|
|
|
2012-04-26 17:55:59 +07:00
|
|
|
client->id = ret;
|
2010-02-01 12:38:10 +07:00
|
|
|
}
|
|
|
|
vga_switcheroo_debugfs_init(&vgasr_priv);
|
|
|
|
vgasr_priv.active = true;
|
|
|
|
}
|
|
|
|
|
2015-08-23 20:18:55 +07:00
|
|
|
/**
|
|
|
|
* vga_switcheroo_register_handler() - register handler
|
|
|
|
* @handler: handler callbacks
|
|
|
|
*
|
|
|
|
* Register handler. Enable vga_switcheroo if two vga clients have already
|
|
|
|
* registered.
|
|
|
|
*
|
|
|
|
* Return: 0 on success, -EINVAL if a handler was already registered.
|
|
|
|
*/
|
2012-08-17 23:17:03 +07:00
|
|
|
int vga_switcheroo_register_handler(struct vga_switcheroo_handler *handler)
|
|
|
|
{
|
|
|
|
mutex_lock(&vgasr_mutex);
|
|
|
|
if (vgasr_priv.handler) {
|
|
|
|
mutex_unlock(&vgasr_mutex);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
vgasr_priv.handler = handler;
|
|
|
|
if (vga_switcheroo_ready()) {
|
2015-08-12 21:32:10 +07:00
|
|
|
pr_info("enabled\n");
|
2012-08-17 23:17:03 +07:00
|
|
|
vga_switcheroo_enable();
|
|
|
|
}
|
|
|
|
mutex_unlock(&vgasr_mutex);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(vga_switcheroo_register_handler);
|
|
|
|
|
2015-08-23 20:18:55 +07:00
|
|
|
/**
|
|
|
|
* vga_switcheroo_unregister_handler() - unregister handler
|
|
|
|
*
|
|
|
|
* Unregister handler. Disable vga_switcheroo.
|
|
|
|
*/
|
2012-08-17 23:17:03 +07:00
|
|
|
void vga_switcheroo_unregister_handler(void)
|
|
|
|
{
|
|
|
|
mutex_lock(&vgasr_mutex);
|
|
|
|
vgasr_priv.handler = NULL;
|
|
|
|
if (vgasr_priv.active) {
|
2015-08-12 21:32:10 +07:00
|
|
|
pr_info("disabled\n");
|
2012-08-17 23:17:03 +07:00
|
|
|
vga_switcheroo_debugfs_fini(&vgasr_priv);
|
|
|
|
vgasr_priv.active = false;
|
|
|
|
}
|
|
|
|
mutex_unlock(&vgasr_mutex);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(vga_switcheroo_unregister_handler);
|
|
|
|
|
2012-04-26 19:29:48 +07:00
|
|
|
static int register_client(struct pci_dev *pdev,
|
|
|
|
const struct vga_switcheroo_client_ops *ops,
|
2012-09-10 09:28:36 +07:00
|
|
|
int id, bool active, bool driver_power_control)
|
2010-02-01 12:38:10 +07:00
|
|
|
{
|
2012-04-26 17:55:59 +07:00
|
|
|
struct vga_switcheroo_client *client;
|
2010-02-01 12:38:10 +07:00
|
|
|
|
2012-04-26 17:55:59 +07:00
|
|
|
client = kzalloc(sizeof(*client), GFP_KERNEL);
|
|
|
|
if (!client)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
client->pwr_state = VGA_SWITCHEROO_ON;
|
|
|
|
client->pdev = pdev;
|
2012-05-11 12:51:17 +07:00
|
|
|
client->ops = ops;
|
2012-04-26 19:29:48 +07:00
|
|
|
client->id = id;
|
|
|
|
client->active = active;
|
2012-09-10 09:28:36 +07:00
|
|
|
client->driver_power_control = driver_power_control;
|
2010-02-01 12:38:10 +07:00
|
|
|
|
2012-04-26 17:55:59 +07:00
|
|
|
mutex_lock(&vgasr_mutex);
|
|
|
|
list_add_tail(&client->list, &vgasr_priv.clients);
|
2012-04-26 19:29:48 +07:00
|
|
|
if (client_is_vga(client))
|
|
|
|
vgasr_priv.registered_clients++;
|
2010-02-01 12:38:10 +07:00
|
|
|
|
2012-08-17 23:17:03 +07:00
|
|
|
if (vga_switcheroo_ready()) {
|
2015-08-12 21:32:10 +07:00
|
|
|
pr_info("enabled\n");
|
2010-02-01 12:38:10 +07:00
|
|
|
vga_switcheroo_enable();
|
|
|
|
}
|
|
|
|
mutex_unlock(&vgasr_mutex);
|
|
|
|
return 0;
|
|
|
|
}
|
2012-04-26 19:29:48 +07:00
|
|
|
|
2015-08-23 20:18:55 +07:00
|
|
|
/**
|
|
|
|
* vga_switcheroo_register_client - register vga client
|
|
|
|
* @pdev: client pci device
|
|
|
|
* @ops: client callbacks
|
|
|
|
* @driver_power_control: whether power state is controlled by the driver's
|
|
|
|
* runtime pm
|
|
|
|
*
|
|
|
|
* Register vga client (GPU). Enable vga_switcheroo if another GPU and a
|
|
|
|
* handler have already registered. The power state of the client is assumed
|
|
|
|
* to be ON.
|
|
|
|
*
|
|
|
|
* Return: 0 on success, -ENOMEM on memory allocation error.
|
|
|
|
*/
|
2012-04-26 19:29:48 +07:00
|
|
|
int vga_switcheroo_register_client(struct pci_dev *pdev,
|
2012-09-10 09:28:36 +07:00
|
|
|
const struct vga_switcheroo_client_ops *ops,
|
|
|
|
bool driver_power_control)
|
2012-04-26 19:29:48 +07:00
|
|
|
{
|
|
|
|
return register_client(pdev, ops, -1,
|
2015-08-12 21:32:11 +07:00
|
|
|
pdev == vga_default_device(),
|
|
|
|
driver_power_control);
|
2012-04-26 19:29:48 +07:00
|
|
|
}
|
2010-02-01 12:38:10 +07:00
|
|
|
EXPORT_SYMBOL(vga_switcheroo_register_client);
|
|
|
|
|
2015-08-23 20:18:55 +07:00
|
|
|
/**
|
|
|
|
* vga_switcheroo_register_audio_client - register audio client
|
|
|
|
* @pdev: client pci device
|
|
|
|
* @ops: client callbacks
|
|
|
|
* @id: client identifier, see enum vga_switcheroo_client_id
|
|
|
|
*
|
|
|
|
* Register audio client (audio device on a GPU). The power state of the
|
|
|
|
* client is assumed to be ON.
|
|
|
|
*
|
|
|
|
* Return: 0 on success, -ENOMEM on memory allocation error.
|
|
|
|
*/
|
2012-04-26 19:29:48 +07:00
|
|
|
int vga_switcheroo_register_audio_client(struct pci_dev *pdev,
|
|
|
|
const struct vga_switcheroo_client_ops *ops,
|
2015-08-27 21:43:43 +07:00
|
|
|
int id)
|
2012-04-26 19:29:48 +07:00
|
|
|
{
|
2015-08-27 21:43:43 +07:00
|
|
|
return register_client(pdev, ops, id | ID_BIT_AUDIO, false, false);
|
2012-04-26 19:29:48 +07:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(vga_switcheroo_register_audio_client);
|
|
|
|
|
2012-04-26 17:55:59 +07:00
|
|
|
static struct vga_switcheroo_client *
|
|
|
|
find_client_from_pci(struct list_head *head, struct pci_dev *pdev)
|
|
|
|
{
|
|
|
|
struct vga_switcheroo_client *client;
|
2015-08-12 21:32:11 +07:00
|
|
|
|
2012-04-26 17:55:59 +07:00
|
|
|
list_for_each_entry(client, head, list)
|
|
|
|
if (client->pdev == pdev)
|
|
|
|
return client;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct vga_switcheroo_client *
|
|
|
|
find_client_from_id(struct list_head *head, int client_id)
|
|
|
|
{
|
|
|
|
struct vga_switcheroo_client *client;
|
2015-08-12 21:32:11 +07:00
|
|
|
|
2012-04-26 17:55:59 +07:00
|
|
|
list_for_each_entry(client, head, list)
|
|
|
|
if (client->id == client_id)
|
|
|
|
return client;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct vga_switcheroo_client *
|
|
|
|
find_active_client(struct list_head *head)
|
|
|
|
{
|
|
|
|
struct vga_switcheroo_client *client;
|
2015-08-12 21:32:11 +07:00
|
|
|
|
2012-04-26 17:55:59 +07:00
|
|
|
list_for_each_entry(client, head, list)
|
2015-08-27 21:43:43 +07:00
|
|
|
if (client->active)
|
2012-04-26 17:55:59 +07:00
|
|
|
return client;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-08-23 20:18:55 +07:00
|
|
|
/**
|
|
|
|
* vga_switcheroo_get_client_state() - obtain power state of a given client
|
|
|
|
* @pdev: client pci device
|
|
|
|
*
|
|
|
|
* Obtain power state of a given client as seen from vga_switcheroo.
|
|
|
|
* The function is only called from hda_intel.c.
|
|
|
|
*
|
|
|
|
* Return: Power state.
|
|
|
|
*/
|
2015-08-28 16:56:26 +07:00
|
|
|
enum vga_switcheroo_state vga_switcheroo_get_client_state(struct pci_dev *pdev)
|
2012-06-07 17:15:15 +07:00
|
|
|
{
|
|
|
|
struct vga_switcheroo_client *client;
|
2015-08-24 04:23:02 +07:00
|
|
|
enum vga_switcheroo_state ret;
|
2012-06-07 17:15:15 +07:00
|
|
|
|
2015-08-24 04:23:02 +07:00
|
|
|
mutex_lock(&vgasr_mutex);
|
2012-06-07 17:15:15 +07:00
|
|
|
client = find_client_from_pci(&vgasr_priv.clients, pdev);
|
|
|
|
if (!client)
|
2015-08-24 04:23:02 +07:00
|
|
|
ret = VGA_SWITCHEROO_NOT_FOUND;
|
|
|
|
else if (!vgasr_priv.active)
|
|
|
|
ret = VGA_SWITCHEROO_INIT;
|
|
|
|
else
|
|
|
|
ret = client->pwr_state;
|
|
|
|
mutex_unlock(&vgasr_mutex);
|
|
|
|
return ret;
|
2012-06-07 17:15:15 +07:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(vga_switcheroo_get_client_state);
|
|
|
|
|
2015-08-23 20:18:55 +07:00
|
|
|
/**
|
|
|
|
* vga_switcheroo_unregister_client() - unregister client
|
|
|
|
* @pdev: client pci device
|
|
|
|
*
|
|
|
|
* Unregister client. Disable vga_switcheroo if this is a vga client (GPU).
|
|
|
|
*/
|
2010-02-01 12:38:10 +07:00
|
|
|
void vga_switcheroo_unregister_client(struct pci_dev *pdev)
|
|
|
|
{
|
2012-04-26 17:55:59 +07:00
|
|
|
struct vga_switcheroo_client *client;
|
2010-02-01 12:38:10 +07:00
|
|
|
|
|
|
|
mutex_lock(&vgasr_mutex);
|
2012-04-26 17:55:59 +07:00
|
|
|
client = find_client_from_pci(&vgasr_priv.clients, pdev);
|
|
|
|
if (client) {
|
2012-04-26 19:29:48 +07:00
|
|
|
if (client_is_vga(client))
|
|
|
|
vgasr_priv.registered_clients--;
|
2012-04-26 17:55:59 +07:00
|
|
|
list_del(&client->list);
|
|
|
|
kfree(client);
|
2010-02-01 12:38:10 +07:00
|
|
|
}
|
2012-04-26 19:29:48 +07:00
|
|
|
if (vgasr_priv.active && vgasr_priv.registered_clients < 2) {
|
2015-08-12 21:32:10 +07:00
|
|
|
pr_info("disabled\n");
|
2012-04-26 19:29:48 +07:00
|
|
|
vga_switcheroo_debugfs_fini(&vgasr_priv);
|
|
|
|
vgasr_priv.active = false;
|
|
|
|
}
|
2010-02-01 12:38:10 +07:00
|
|
|
mutex_unlock(&vgasr_mutex);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(vga_switcheroo_unregister_client);
|
|
|
|
|
2015-08-23 20:18:55 +07:00
|
|
|
/**
|
|
|
|
* vga_switcheroo_client_fb_set() - set framebuffer of a given client
|
|
|
|
* @pdev: client pci device
|
|
|
|
* @info: framebuffer
|
|
|
|
*
|
|
|
|
* Set framebuffer of a given client. The console will be remapped to this
|
|
|
|
* on switching.
|
|
|
|
*/
|
2010-02-01 12:38:10 +07:00
|
|
|
void vga_switcheroo_client_fb_set(struct pci_dev *pdev,
|
|
|
|
struct fb_info *info)
|
|
|
|
{
|
2012-04-26 17:55:59 +07:00
|
|
|
struct vga_switcheroo_client *client;
|
2010-02-01 12:38:10 +07:00
|
|
|
|
|
|
|
mutex_lock(&vgasr_mutex);
|
2012-04-26 17:55:59 +07:00
|
|
|
client = find_client_from_pci(&vgasr_priv.clients, pdev);
|
|
|
|
if (client)
|
|
|
|
client->fb_info = info;
|
2010-02-01 12:38:10 +07:00
|
|
|
mutex_unlock(&vgasr_mutex);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(vga_switcheroo_client_fb_set);
|
|
|
|
|
2015-08-23 20:18:55 +07:00
|
|
|
/**
|
|
|
|
* DOC: Manual switching and manual power control
|
|
|
|
*
|
|
|
|
* In this mode of use, the file /sys/kernel/debug/vgaswitcheroo/switch
|
|
|
|
* can be read to retrieve the current vga_switcheroo state and commands
|
|
|
|
* can be written to it to change the state. The file appears as soon as
|
|
|
|
* two GPU drivers and one handler have registered with vga_switcheroo.
|
|
|
|
* The following commands are understood:
|
|
|
|
*
|
|
|
|
* * OFF: Power off the device not in use.
|
|
|
|
* * ON: Power on the device not in use.
|
|
|
|
* * IGD: Switch to the integrated graphics device.
|
|
|
|
* Power on the integrated GPU if necessary, power off the discrete GPU.
|
|
|
|
* Prerequisite is that no user space processes (e.g. Xorg, alsactl)
|
|
|
|
* have opened device files of the GPUs or the audio client. If the
|
|
|
|
* switch fails, the user may invoke lsof(8) or fuser(1) on /dev/dri/
|
|
|
|
* and /dev/snd/controlC1 to identify processes blocking the switch.
|
|
|
|
* * DIS: Switch to the discrete graphics device.
|
|
|
|
* * DIGD: Delayed switch to the integrated graphics device.
|
|
|
|
* This will perform the switch once the last user space process has
|
|
|
|
* closed the device files of the GPUs and the audio client.
|
|
|
|
* * DDIS: Delayed switch to the discrete graphics device.
|
|
|
|
* * MIGD: Mux-only switch to the integrated graphics device.
|
|
|
|
* Does not remap console or change the power state of either gpu.
|
|
|
|
* If the integrated GPU is currently off, the screen will turn black.
|
|
|
|
* If it is on, the screen will show whatever happens to be in VRAM.
|
|
|
|
* Either way, the user has to blindly enter the command to switch back.
|
|
|
|
* * MDIS: Mux-only switch to the discrete graphics device.
|
|
|
|
*
|
|
|
|
* For GPUs whose power state is controlled by the driver's runtime pm,
|
|
|
|
* the ON and OFF commands are a no-op (see next section).
|
|
|
|
*
|
|
|
|
* For muxless machines, the IGD/DIS, DIGD/DDIS and MIGD/MDIS commands
|
|
|
|
* should not be used.
|
|
|
|
*/
|
|
|
|
|
2010-02-01 12:38:10 +07:00
|
|
|
static int vga_switcheroo_show(struct seq_file *m, void *v)
|
|
|
|
{
|
2012-04-26 17:55:59 +07:00
|
|
|
struct vga_switcheroo_client *client;
|
|
|
|
int i = 0;
|
2015-08-12 21:32:11 +07:00
|
|
|
|
2010-02-01 12:38:10 +07:00
|
|
|
mutex_lock(&vgasr_mutex);
|
2012-04-26 17:55:59 +07:00
|
|
|
list_for_each_entry(client, &vgasr_priv.clients, list) {
|
2012-09-10 09:28:36 +07:00
|
|
|
seq_printf(m, "%d:%s%s:%c:%s%s:%s\n", i,
|
2015-08-12 21:32:11 +07:00
|
|
|
client_id(client) == VGA_SWITCHEROO_DIS ? "DIS" :
|
|
|
|
"IGD",
|
2012-04-26 19:29:48 +07:00
|
|
|
client_is_vga(client) ? "" : "-Audio",
|
2012-04-26 17:55:59 +07:00
|
|
|
client->active ? '+' : ' ',
|
2012-09-10 09:28:36 +07:00
|
|
|
client->driver_power_control ? "Dyn" : "",
|
2012-04-26 17:55:59 +07:00
|
|
|
client->pwr_state ? "Pwr" : "Off",
|
|
|
|
pci_name(client->pdev));
|
|
|
|
i++;
|
2010-02-01 12:38:10 +07:00
|
|
|
}
|
|
|
|
mutex_unlock(&vgasr_mutex);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int vga_switcheroo_debugfs_open(struct inode *inode, struct file *file)
|
|
|
|
{
|
|
|
|
return single_open(file, vga_switcheroo_show, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int vga_switchon(struct vga_switcheroo_client *client)
|
|
|
|
{
|
2012-09-10 09:28:36 +07:00
|
|
|
if (client->driver_power_control)
|
|
|
|
return 0;
|
2010-12-06 09:31:50 +07:00
|
|
|
if (vgasr_priv.handler->power_state)
|
|
|
|
vgasr_priv.handler->power_state(client->id, VGA_SWITCHEROO_ON);
|
2010-02-01 12:38:10 +07:00
|
|
|
/* call the driver callback to turn on device */
|
2012-05-11 12:51:17 +07:00
|
|
|
client->ops->set_gpu_state(client->pdev, VGA_SWITCHEROO_ON);
|
2010-02-01 12:38:10 +07:00
|
|
|
client->pwr_state = VGA_SWITCHEROO_ON;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int vga_switchoff(struct vga_switcheroo_client *client)
|
|
|
|
{
|
2012-09-10 09:28:36 +07:00
|
|
|
if (client->driver_power_control)
|
|
|
|
return 0;
|
2010-02-01 12:38:10 +07:00
|
|
|
/* call the driver callback to turn off device */
|
2012-05-11 12:51:17 +07:00
|
|
|
client->ops->set_gpu_state(client->pdev, VGA_SWITCHEROO_OFF);
|
2010-12-06 09:31:50 +07:00
|
|
|
if (vgasr_priv.handler->power_state)
|
|
|
|
vgasr_priv.handler->power_state(client->id, VGA_SWITCHEROO_OFF);
|
2010-02-01 12:38:10 +07:00
|
|
|
client->pwr_state = VGA_SWITCHEROO_OFF;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-28 16:56:26 +07:00
|
|
|
static void set_audio_state(int id, enum vga_switcheroo_state state)
|
2012-04-26 19:29:48 +07:00
|
|
|
{
|
|
|
|
struct vga_switcheroo_client *client;
|
|
|
|
|
|
|
|
client = find_client_from_id(&vgasr_priv.clients, id | ID_BIT_AUDIO);
|
|
|
|
if (client && client->pwr_state != state) {
|
|
|
|
client->ops->set_gpu_state(client->pdev, state);
|
|
|
|
client->pwr_state = state;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-07 11:24:25 +07:00
|
|
|
/* stage one happens before delay */
|
|
|
|
static int vga_switchto_stage1(struct vga_switcheroo_client *new_client)
|
2010-02-01 12:38:10 +07:00
|
|
|
{
|
2012-04-26 17:55:59 +07:00
|
|
|
struct vga_switcheroo_client *active;
|
2010-02-01 12:38:10 +07:00
|
|
|
|
2012-04-26 17:55:59 +07:00
|
|
|
active = find_active_client(&vgasr_priv.clients);
|
2010-02-01 12:38:10 +07:00
|
|
|
if (!active)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (new_client->pwr_state == VGA_SWITCHEROO_OFF)
|
|
|
|
vga_switchon(new_client);
|
|
|
|
|
2012-04-17 03:26:03 +07:00
|
|
|
vga_set_default_device(new_client->pdev);
|
2010-12-07 11:24:25 +07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* post delay */
|
|
|
|
static int vga_switchto_stage2(struct vga_switcheroo_client *new_client)
|
|
|
|
{
|
|
|
|
int ret;
|
2012-04-26 17:55:59 +07:00
|
|
|
struct vga_switcheroo_client *active;
|
2010-12-07 11:24:25 +07:00
|
|
|
|
2012-04-26 17:55:59 +07:00
|
|
|
active = find_active_client(&vgasr_priv.clients);
|
2010-12-07 11:24:25 +07:00
|
|
|
if (!active)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
active->active = false;
|
2010-02-01 12:38:10 +07:00
|
|
|
|
2012-06-09 13:46:42 +07:00
|
|
|
set_audio_state(active->id, VGA_SWITCHEROO_OFF);
|
|
|
|
|
2010-02-01 12:38:10 +07:00
|
|
|
if (new_client->fb_info) {
|
|
|
|
struct fb_event event;
|
2015-08-12 21:32:11 +07:00
|
|
|
|
2013-01-25 08:38:56 +07:00
|
|
|
console_lock();
|
2010-02-01 12:38:10 +07:00
|
|
|
event.info = new_client->fb_info;
|
|
|
|
fb_notifier_call_chain(FB_EVENT_REMAP_ALL_CONSOLE, &event);
|
2013-01-25 08:38:56 +07:00
|
|
|
console_unlock();
|
2010-02-01 12:38:10 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = vgasr_priv.handler->switchto(new_client->id);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2012-05-11 12:51:17 +07:00
|
|
|
if (new_client->ops->reprobe)
|
|
|
|
new_client->ops->reprobe(new_client->pdev);
|
2010-12-07 05:57:57 +07:00
|
|
|
|
2010-02-01 12:38:10 +07:00
|
|
|
if (active->pwr_state == VGA_SWITCHEROO_ON)
|
|
|
|
vga_switchoff(active);
|
|
|
|
|
2012-06-09 13:46:42 +07:00
|
|
|
set_audio_state(new_client->id, VGA_SWITCHEROO_ON);
|
|
|
|
|
2010-02-01 12:38:10 +07:00
|
|
|
new_client->active = true;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-04-26 17:55:59 +07:00
|
|
|
static bool check_can_switch(void)
|
|
|
|
{
|
|
|
|
struct vga_switcheroo_client *client;
|
|
|
|
|
|
|
|
list_for_each_entry(client, &vgasr_priv.clients, list) {
|
2012-05-11 12:51:17 +07:00
|
|
|
if (!client->ops->can_switch(client->pdev)) {
|
2015-08-12 21:32:10 +07:00
|
|
|
pr_err("client %x refused switch\n", client->id);
|
2012-04-26 17:55:59 +07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-02-01 12:38:10 +07:00
|
|
|
static ssize_t
|
|
|
|
vga_switcheroo_debugfs_write(struct file *filp, const char __user *ubuf,
|
|
|
|
size_t cnt, loff_t *ppos)
|
|
|
|
{
|
|
|
|
char usercmd[64];
|
2012-04-26 17:55:59 +07:00
|
|
|
int ret;
|
2010-02-01 12:38:10 +07:00
|
|
|
bool delay = false, can_switch;
|
2010-12-06 09:35:52 +07:00
|
|
|
bool just_mux = false;
|
2010-02-01 12:38:10 +07:00
|
|
|
int client_id = -1;
|
|
|
|
struct vga_switcheroo_client *client = NULL;
|
|
|
|
|
|
|
|
if (cnt > 63)
|
|
|
|
cnt = 63;
|
|
|
|
|
|
|
|
if (copy_from_user(usercmd, ubuf, cnt))
|
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
mutex_lock(&vgasr_mutex);
|
|
|
|
|
2010-04-28 04:11:03 +07:00
|
|
|
if (!vgasr_priv.active) {
|
|
|
|
cnt = -EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
2010-02-01 12:38:10 +07:00
|
|
|
|
|
|
|
/* pwr off the device not in use */
|
|
|
|
if (strncmp(usercmd, "OFF", 3) == 0) {
|
2012-04-26 17:55:59 +07:00
|
|
|
list_for_each_entry(client, &vgasr_priv.clients, list) {
|
2012-06-09 13:46:42 +07:00
|
|
|
if (client->active || client_is_audio(client))
|
2010-02-01 12:38:10 +07:00
|
|
|
continue;
|
2012-09-10 09:28:36 +07:00
|
|
|
if (client->driver_power_control)
|
|
|
|
continue;
|
2012-06-09 13:46:42 +07:00
|
|
|
set_audio_state(client->id, VGA_SWITCHEROO_OFF);
|
2012-04-26 17:55:59 +07:00
|
|
|
if (client->pwr_state == VGA_SWITCHEROO_ON)
|
|
|
|
vga_switchoff(client);
|
2010-02-01 12:38:10 +07:00
|
|
|
}
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
/* pwr on the device not in use */
|
|
|
|
if (strncmp(usercmd, "ON", 2) == 0) {
|
2012-04-26 17:55:59 +07:00
|
|
|
list_for_each_entry(client, &vgasr_priv.clients, list) {
|
2012-06-09 13:46:42 +07:00
|
|
|
if (client->active || client_is_audio(client))
|
2010-02-01 12:38:10 +07:00
|
|
|
continue;
|
2012-09-10 09:28:36 +07:00
|
|
|
if (client->driver_power_control)
|
|
|
|
continue;
|
2012-04-26 17:55:59 +07:00
|
|
|
if (client->pwr_state == VGA_SWITCHEROO_OFF)
|
|
|
|
vga_switchon(client);
|
2012-06-09 13:46:42 +07:00
|
|
|
set_audio_state(client->id, VGA_SWITCHEROO_ON);
|
2010-02-01 12:38:10 +07:00
|
|
|
}
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* request a delayed switch - test can we switch now */
|
|
|
|
if (strncmp(usercmd, "DIGD", 4) == 0) {
|
|
|
|
client_id = VGA_SWITCHEROO_IGD;
|
|
|
|
delay = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strncmp(usercmd, "DDIS", 4) == 0) {
|
|
|
|
client_id = VGA_SWITCHEROO_DIS;
|
|
|
|
delay = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strncmp(usercmd, "IGD", 3) == 0)
|
|
|
|
client_id = VGA_SWITCHEROO_IGD;
|
|
|
|
|
|
|
|
if (strncmp(usercmd, "DIS", 3) == 0)
|
|
|
|
client_id = VGA_SWITCHEROO_DIS;
|
|
|
|
|
2011-01-07 12:12:27 +07:00
|
|
|
if (strncmp(usercmd, "MIGD", 4) == 0) {
|
2010-12-06 09:35:52 +07:00
|
|
|
just_mux = true;
|
|
|
|
client_id = VGA_SWITCHEROO_IGD;
|
|
|
|
}
|
2011-01-07 12:12:27 +07:00
|
|
|
if (strncmp(usercmd, "MDIS", 4) == 0) {
|
2010-12-06 09:35:52 +07:00
|
|
|
just_mux = true;
|
|
|
|
client_id = VGA_SWITCHEROO_DIS;
|
|
|
|
}
|
|
|
|
|
2010-02-01 12:38:10 +07:00
|
|
|
if (client_id == -1)
|
|
|
|
goto out;
|
2012-04-26 17:55:59 +07:00
|
|
|
client = find_client_from_id(&vgasr_priv.clients, client_id);
|
|
|
|
if (!client)
|
|
|
|
goto out;
|
2010-02-01 12:38:10 +07:00
|
|
|
|
|
|
|
vgasr_priv.delayed_switch_active = false;
|
2010-12-06 09:35:52 +07:00
|
|
|
|
|
|
|
if (just_mux) {
|
|
|
|
ret = vgasr_priv.handler->switchto(client_id);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2012-04-26 17:55:59 +07:00
|
|
|
if (client->active)
|
2011-05-15 21:32:50 +07:00
|
|
|
goto out;
|
|
|
|
|
2010-02-01 12:38:10 +07:00
|
|
|
/* okay we want a switch - test if devices are willing to switch */
|
2012-04-26 17:55:59 +07:00
|
|
|
can_switch = check_can_switch();
|
2010-02-01 12:38:10 +07:00
|
|
|
|
|
|
|
if (can_switch == false && delay == false)
|
|
|
|
goto out;
|
|
|
|
|
2012-04-26 17:55:59 +07:00
|
|
|
if (can_switch) {
|
2010-12-07 11:24:25 +07:00
|
|
|
ret = vga_switchto_stage1(client);
|
|
|
|
if (ret)
|
2015-08-12 21:32:10 +07:00
|
|
|
pr_err("switching failed stage 1 %d\n", ret);
|
2010-12-07 11:24:25 +07:00
|
|
|
|
|
|
|
ret = vga_switchto_stage2(client);
|
2010-02-01 12:38:10 +07:00
|
|
|
if (ret)
|
2015-08-12 21:32:10 +07:00
|
|
|
pr_err("switching failed stage 2 %d\n", ret);
|
2010-12-07 11:24:25 +07:00
|
|
|
|
2010-02-01 12:38:10 +07:00
|
|
|
} else {
|
2015-08-12 21:32:10 +07:00
|
|
|
pr_info("setting delayed switch to client %d\n", client->id);
|
2010-02-01 12:38:10 +07:00
|
|
|
vgasr_priv.delayed_switch_active = true;
|
|
|
|
vgasr_priv.delayed_client_id = client_id;
|
|
|
|
|
2010-12-07 11:24:25 +07:00
|
|
|
ret = vga_switchto_stage1(client);
|
|
|
|
if (ret)
|
2015-08-12 21:32:10 +07:00
|
|
|
pr_err("delayed switching stage 1 failed %d\n", ret);
|
2010-02-01 12:38:10 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
mutex_unlock(&vgasr_mutex);
|
|
|
|
return cnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct file_operations vga_switcheroo_debugfs_fops = {
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.open = vga_switcheroo_debugfs_open,
|
|
|
|
.write = vga_switcheroo_debugfs_write,
|
|
|
|
.read = seq_read,
|
|
|
|
.llseek = seq_lseek,
|
|
|
|
.release = single_release,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void vga_switcheroo_debugfs_fini(struct vgasr_priv *priv)
|
|
|
|
{
|
2015-08-12 21:32:12 +07:00
|
|
|
debugfs_remove(priv->switch_file);
|
|
|
|
priv->switch_file = NULL;
|
|
|
|
|
|
|
|
debugfs_remove(priv->debugfs_root);
|
|
|
|
priv->debugfs_root = NULL;
|
2010-02-01 12:38:10 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
static int vga_switcheroo_debugfs_init(struct vgasr_priv *priv)
|
|
|
|
{
|
2015-08-12 21:32:10 +07:00
|
|
|
static const char mp[] = "/sys/kernel/debug";
|
|
|
|
|
2010-02-01 12:38:10 +07:00
|
|
|
/* already initialised */
|
|
|
|
if (priv->debugfs_root)
|
|
|
|
return 0;
|
|
|
|
priv->debugfs_root = debugfs_create_dir("vgaswitcheroo", NULL);
|
|
|
|
|
|
|
|
if (!priv->debugfs_root) {
|
2015-08-12 21:32:10 +07:00
|
|
|
pr_err("Cannot create %s/vgaswitcheroo\n", mp);
|
2010-02-01 12:38:10 +07:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
priv->switch_file = debugfs_create_file("switch", 0644,
|
2015-08-12 21:32:11 +07:00
|
|
|
priv->debugfs_root, NULL,
|
|
|
|
&vga_switcheroo_debugfs_fops);
|
2010-02-01 12:38:10 +07:00
|
|
|
if (!priv->switch_file) {
|
2015-08-12 21:32:10 +07:00
|
|
|
pr_err("cannot create %s/vgaswitcheroo/switch\n", mp);
|
2010-02-01 12:38:10 +07:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
fail:
|
|
|
|
vga_switcheroo_debugfs_fini(priv);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-08-23 20:18:55 +07:00
|
|
|
/**
|
|
|
|
* vga_switcheroo_process_delayed_switch() - helper for delayed switching
|
|
|
|
*
|
|
|
|
* Process a delayed switch if one is pending. DRM drivers should call this
|
|
|
|
* from their ->lastclose callback.
|
|
|
|
*
|
|
|
|
* Return: 0 on success. -EINVAL if no delayed switch is pending, if the client
|
|
|
|
* has unregistered in the meantime or if there are other clients blocking the
|
|
|
|
* switch. If the actual switch fails, an error is reported and 0 is returned.
|
|
|
|
*/
|
2010-02-01 12:38:10 +07:00
|
|
|
int vga_switcheroo_process_delayed_switch(void)
|
|
|
|
{
|
2012-04-26 17:55:59 +07:00
|
|
|
struct vga_switcheroo_client *client;
|
2010-02-01 12:38:10 +07:00
|
|
|
int ret;
|
|
|
|
int err = -EINVAL;
|
|
|
|
|
|
|
|
mutex_lock(&vgasr_mutex);
|
|
|
|
if (!vgasr_priv.delayed_switch_active)
|
|
|
|
goto err;
|
|
|
|
|
2015-08-12 21:32:10 +07:00
|
|
|
pr_info("processing delayed switch to %d\n",
|
|
|
|
vgasr_priv.delayed_client_id);
|
2010-02-01 12:38:10 +07:00
|
|
|
|
2012-04-26 17:55:59 +07:00
|
|
|
client = find_client_from_id(&vgasr_priv.clients,
|
|
|
|
vgasr_priv.delayed_client_id);
|
|
|
|
if (!client || !check_can_switch())
|
2010-02-01 12:38:10 +07:00
|
|
|
goto err;
|
|
|
|
|
2010-12-07 11:24:25 +07:00
|
|
|
ret = vga_switchto_stage2(client);
|
2010-02-01 12:38:10 +07:00
|
|
|
if (ret)
|
2015-08-12 21:32:10 +07:00
|
|
|
pr_err("delayed switching failed stage 2 %d\n", ret);
|
2010-02-01 12:38:10 +07:00
|
|
|
|
|
|
|
vgasr_priv.delayed_switch_active = false;
|
|
|
|
err = 0;
|
|
|
|
err:
|
|
|
|
mutex_unlock(&vgasr_mutex);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(vga_switcheroo_process_delayed_switch);
|
2012-09-10 09:28:36 +07:00
|
|
|
|
2015-08-23 20:18:55 +07:00
|
|
|
/**
|
|
|
|
* DOC: Driver power control
|
|
|
|
*
|
|
|
|
* In this mode of use, the discrete GPU automatically powers up and down at
|
|
|
|
* the discretion of the driver's runtime pm. On muxed machines, the user may
|
|
|
|
* still influence the muxer state by way of the debugfs interface, however
|
|
|
|
* the ON and OFF commands become a no-op for the discrete GPU.
|
|
|
|
*
|
|
|
|
* This mode is the default on Nvidia HybridPower/Optimus and ATI PowerXpress.
|
|
|
|
* Specifying nouveau.runpm=0, radeon.runpm=0 or amdgpu.runpm=0 on the kernel
|
|
|
|
* command line disables it.
|
|
|
|
*
|
|
|
|
* When the driver decides to power up or down, it notifies vga_switcheroo
|
|
|
|
* thereof so that it can (a) power the audio device on the GPU up or down,
|
|
|
|
* and (b) update its internal power state representation for the device.
|
|
|
|
* This is achieved by vga_switcheroo_set_dynamic_switch().
|
|
|
|
*
|
|
|
|
* After the GPU has been suspended, the handler needs to be called to cut
|
|
|
|
* power to the GPU. Likewise it needs to reinstate power before the GPU
|
|
|
|
* can resume. This is achieved by vga_switcheroo_init_domain_pm_ops(),
|
|
|
|
* which augments the GPU's suspend/resume functions by the requisite
|
|
|
|
* calls to the handler.
|
|
|
|
*
|
|
|
|
* When the audio device resumes, the GPU needs to be woken. This is achieved
|
|
|
|
* by vga_switcheroo_init_domain_pm_optimus_hdmi_audio(), which augments the
|
|
|
|
* audio device's resume function.
|
|
|
|
*
|
|
|
|
* On muxed machines, if the mux is initially switched to the discrete GPU,
|
|
|
|
* the user ends up with a black screen when the GPU powers down after boot.
|
|
|
|
* As a workaround, the mux is forced to the integrated GPU on runtime suspend,
|
|
|
|
* cf. https://bugs.freedesktop.org/show_bug.cgi?id=75917
|
|
|
|
*/
|
|
|
|
|
2015-08-12 21:32:11 +07:00
|
|
|
static void vga_switcheroo_power_switch(struct pci_dev *pdev,
|
|
|
|
enum vga_switcheroo_state state)
|
2012-09-10 09:28:36 +07:00
|
|
|
{
|
|
|
|
struct vga_switcheroo_client *client;
|
|
|
|
|
|
|
|
if (!vgasr_priv.handler->power_state)
|
|
|
|
return;
|
|
|
|
|
|
|
|
client = find_client_from_pci(&vgasr_priv.clients, pdev);
|
|
|
|
if (!client)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!client->driver_power_control)
|
|
|
|
return;
|
|
|
|
|
|
|
|
vgasr_priv.handler->power_state(client->id, state);
|
|
|
|
}
|
|
|
|
|
2015-08-23 20:18:55 +07:00
|
|
|
/**
|
|
|
|
* vga_switcheroo_set_dynamic_switch() - helper for driver power control
|
|
|
|
* @pdev: client pci device
|
|
|
|
* @dynamic: new power state
|
|
|
|
*
|
|
|
|
* Helper for GPUs whose power state is controlled by the driver's runtime pm.
|
|
|
|
* When the driver decides to power up or down, it notifies vga_switcheroo
|
|
|
|
* thereof using this helper so that it can (a) power the audio device on
|
|
|
|
* the GPU up or down, and (b) update its internal power state representation
|
|
|
|
* for the device.
|
|
|
|
*/
|
2015-08-12 21:32:11 +07:00
|
|
|
void vga_switcheroo_set_dynamic_switch(struct pci_dev *pdev,
|
|
|
|
enum vga_switcheroo_state dynamic)
|
2012-09-10 09:28:36 +07:00
|
|
|
{
|
|
|
|
struct vga_switcheroo_client *client;
|
|
|
|
|
2015-08-24 04:23:02 +07:00
|
|
|
mutex_lock(&vgasr_mutex);
|
2012-09-10 09:28:36 +07:00
|
|
|
client = find_client_from_pci(&vgasr_priv.clients, pdev);
|
2015-08-24 04:23:02 +07:00
|
|
|
if (!client || !client->driver_power_control) {
|
|
|
|
mutex_unlock(&vgasr_mutex);
|
2012-09-10 09:28:36 +07:00
|
|
|
return;
|
2015-08-24 04:23:02 +07:00
|
|
|
}
|
2012-09-10 09:28:36 +07:00
|
|
|
|
|
|
|
client->pwr_state = dynamic;
|
|
|
|
set_audio_state(client->id, dynamic);
|
2015-08-24 04:23:02 +07:00
|
|
|
mutex_unlock(&vgasr_mutex);
|
2012-09-10 09:28:36 +07:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(vga_switcheroo_set_dynamic_switch);
|
|
|
|
|
|
|
|
/* switcheroo power domain */
|
|
|
|
static int vga_switcheroo_runtime_suspend(struct device *dev)
|
|
|
|
{
|
|
|
|
struct pci_dev *pdev = to_pci_dev(dev);
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = dev->bus->pm->runtime_suspend(dev);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2015-08-24 04:23:02 +07:00
|
|
|
mutex_lock(&vgasr_mutex);
|
2014-05-20 01:04:43 +07:00
|
|
|
if (vgasr_priv.handler->switchto)
|
|
|
|
vgasr_priv.handler->switchto(VGA_SWITCHEROO_IGD);
|
2012-09-10 09:28:36 +07:00
|
|
|
vga_switcheroo_power_switch(pdev, VGA_SWITCHEROO_OFF);
|
2015-08-24 04:23:02 +07:00
|
|
|
mutex_unlock(&vgasr_mutex);
|
2012-09-10 09:28:36 +07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int vga_switcheroo_runtime_resume(struct device *dev)
|
|
|
|
{
|
|
|
|
struct pci_dev *pdev = to_pci_dev(dev);
|
|
|
|
int ret;
|
|
|
|
|
2015-08-24 04:23:02 +07:00
|
|
|
mutex_lock(&vgasr_mutex);
|
2012-09-10 09:28:36 +07:00
|
|
|
vga_switcheroo_power_switch(pdev, VGA_SWITCHEROO_ON);
|
2015-08-24 04:23:02 +07:00
|
|
|
mutex_unlock(&vgasr_mutex);
|
2012-09-10 09:28:36 +07:00
|
|
|
ret = dev->bus->pm->runtime_resume(dev);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-23 20:18:55 +07:00
|
|
|
/**
|
|
|
|
* vga_switcheroo_init_domain_pm_ops() - helper for driver power control
|
|
|
|
* @dev: vga client device
|
|
|
|
* @domain: power domain
|
|
|
|
*
|
|
|
|
* Helper for GPUs whose power state is controlled by the driver's runtime pm.
|
|
|
|
* After the GPU has been suspended, the handler needs to be called to cut
|
|
|
|
* power to the GPU. Likewise it needs to reinstate power before the GPU
|
|
|
|
* can resume. To this end, this helper augments the suspend/resume functions
|
|
|
|
* by the requisite calls to the handler. It needs only be called on platforms
|
|
|
|
* where the power switch is separate to the device being powered down.
|
|
|
|
*/
|
2015-08-12 21:32:11 +07:00
|
|
|
int vga_switcheroo_init_domain_pm_ops(struct device *dev,
|
|
|
|
struct dev_pm_domain *domain)
|
2012-09-10 09:28:36 +07:00
|
|
|
{
|
|
|
|
/* copy over all the bus versions */
|
|
|
|
if (dev->bus && dev->bus->pm) {
|
|
|
|
domain->ops = *dev->bus->pm;
|
|
|
|
domain->ops.runtime_suspend = vga_switcheroo_runtime_suspend;
|
|
|
|
domain->ops.runtime_resume = vga_switcheroo_runtime_resume;
|
|
|
|
|
|
|
|
dev->pm_domain = domain;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
dev->pm_domain = NULL;
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(vga_switcheroo_init_domain_pm_ops);
|
|
|
|
|
2014-09-13 04:51:29 +07:00
|
|
|
void vga_switcheroo_fini_domain_pm_ops(struct device *dev)
|
|
|
|
{
|
|
|
|
dev->pm_domain = NULL;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(vga_switcheroo_fini_domain_pm_ops);
|
|
|
|
|
2012-09-10 09:28:36 +07:00
|
|
|
static int vga_switcheroo_runtime_resume_hdmi_audio(struct device *dev)
|
|
|
|
{
|
|
|
|
struct pci_dev *pdev = to_pci_dev(dev);
|
2015-08-24 04:23:02 +07:00
|
|
|
struct vga_switcheroo_client *client;
|
|
|
|
struct device *video_dev = NULL;
|
2012-09-10 09:28:36 +07:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* we need to check if we have to switch back on the video
|
|
|
|
device so the audio device can come back */
|
2015-08-24 04:23:02 +07:00
|
|
|
mutex_lock(&vgasr_mutex);
|
2012-09-10 09:28:36 +07:00
|
|
|
list_for_each_entry(client, &vgasr_priv.clients, list) {
|
2015-08-12 21:32:11 +07:00
|
|
|
if (PCI_SLOT(client->pdev->devfn) == PCI_SLOT(pdev->devfn) &&
|
|
|
|
client_is_vga(client)) {
|
2015-08-24 04:23:02 +07:00
|
|
|
video_dev = &client->pdev->dev;
|
2012-09-10 09:28:36 +07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-08-24 04:23:02 +07:00
|
|
|
mutex_unlock(&vgasr_mutex);
|
|
|
|
|
|
|
|
if (video_dev) {
|
|
|
|
ret = pm_runtime_get_sync(video_dev);
|
|
|
|
if (ret && ret != 1)
|
|
|
|
return ret;
|
|
|
|
}
|
2012-09-10 09:28:36 +07:00
|
|
|
ret = dev->bus->pm->runtime_resume(dev);
|
|
|
|
|
|
|
|
/* put the reference for the gpu */
|
2015-08-24 04:23:02 +07:00
|
|
|
if (video_dev) {
|
|
|
|
pm_runtime_mark_last_busy(video_dev);
|
|
|
|
pm_runtime_put_autosuspend(video_dev);
|
2012-09-10 09:28:36 +07:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-08-23 20:18:55 +07:00
|
|
|
/**
|
|
|
|
* vga_switcheroo_init_domain_pm_optimus_hdmi_audio() - helper for driver
|
|
|
|
* power control
|
|
|
|
* @dev: audio client device
|
|
|
|
* @domain: power domain
|
|
|
|
*
|
|
|
|
* Helper for GPUs whose power state is controlled by the driver's runtime pm.
|
|
|
|
* When the audio device resumes, the GPU needs to be woken. This helper
|
|
|
|
* augments the audio device's resume function to do that.
|
|
|
|
*
|
|
|
|
* Return: 0 on success, -EINVAL if no power management operations are
|
|
|
|
* defined for this device.
|
|
|
|
*/
|
2015-08-12 21:32:11 +07:00
|
|
|
int
|
|
|
|
vga_switcheroo_init_domain_pm_optimus_hdmi_audio(struct device *dev,
|
|
|
|
struct dev_pm_domain *domain)
|
2012-09-10 09:28:36 +07:00
|
|
|
{
|
|
|
|
/* copy over all the bus versions */
|
|
|
|
if (dev->bus && dev->bus->pm) {
|
|
|
|
domain->ops = *dev->bus->pm;
|
2015-08-12 21:32:11 +07:00
|
|
|
domain->ops.runtime_resume =
|
|
|
|
vga_switcheroo_runtime_resume_hdmi_audio;
|
2012-09-10 09:28:36 +07:00
|
|
|
|
|
|
|
dev->pm_domain = domain;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
dev->pm_domain = NULL;
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(vga_switcheroo_init_domain_pm_optimus_hdmi_audio);
|