Commit 79721e0a authored by Takashi Iwai's avatar Takashi Iwai

vga_switcheroo: Refactor using linked list

Refactor the code base a bit for the further work to adapt more clients.
Signed-off-by: default avatarTakashi Iwai <tiwai@suse.de>
parent 218c872b
...@@ -39,6 +39,7 @@ struct vga_switcheroo_client { ...@@ -39,6 +39,7 @@ struct vga_switcheroo_client {
bool (*can_switch)(struct pci_dev *pdev); bool (*can_switch)(struct pci_dev *pdev);
int id; int id;
bool active; bool active;
struct list_head list;
}; };
static DEFINE_MUTEX(vgasr_mutex); static DEFINE_MUTEX(vgasr_mutex);
...@@ -53,7 +54,7 @@ struct vgasr_priv { ...@@ -53,7 +54,7 @@ struct vgasr_priv {
struct dentry *switch_file; struct dentry *switch_file;
int registered_clients; int registered_clients;
struct vga_switcheroo_client clients[VGA_SWITCHEROO_MAX_CLIENTS]; struct list_head clients;
struct vga_switcheroo_handler *handler; struct vga_switcheroo_handler *handler;
}; };
...@@ -62,7 +63,9 @@ static int vga_switcheroo_debugfs_init(struct vgasr_priv *priv); ...@@ -62,7 +63,9 @@ static int vga_switcheroo_debugfs_init(struct vgasr_priv *priv);
static void vga_switcheroo_debugfs_fini(struct vgasr_priv *priv); static void vga_switcheroo_debugfs_fini(struct vgasr_priv *priv);
/* only one switcheroo per system */ /* only one switcheroo per system */
static struct vgasr_priv vgasr_priv; static struct vgasr_priv vgasr_priv = {
.clients = LIST_HEAD_INIT(vgasr_priv.clients),
};
int vga_switcheroo_register_handler(struct vga_switcheroo_handler *handler) int vga_switcheroo_register_handler(struct vga_switcheroo_handler *handler)
{ {
...@@ -88,17 +91,18 @@ EXPORT_SYMBOL(vga_switcheroo_unregister_handler); ...@@ -88,17 +91,18 @@ EXPORT_SYMBOL(vga_switcheroo_unregister_handler);
static void vga_switcheroo_enable(void) static void vga_switcheroo_enable(void)
{ {
int i;
int ret; int ret;
struct vga_switcheroo_client *client;
/* call the handler to init */ /* call the handler to init */
vgasr_priv.handler->init(); vgasr_priv.handler->init();
for (i = 0; i < VGA_SWITCHEROO_MAX_CLIENTS; i++) { list_for_each_entry(client, &vgasr_priv.clients, list) {
ret = vgasr_priv.handler->get_client_id(vgasr_priv.clients[i].pdev); ret = vgasr_priv.handler->get_client_id(client->pdev);
if (ret < 0) if (ret < 0)
return; return;
vgasr_priv.clients[i].id = ret; client->id = ret;
} }
vga_switcheroo_debugfs_init(&vgasr_priv); vga_switcheroo_debugfs_init(&vgasr_priv);
vgasr_priv.active = true; vgasr_priv.active = true;
...@@ -109,28 +113,27 @@ int vga_switcheroo_register_client(struct pci_dev *pdev, ...@@ -109,28 +113,27 @@ int vga_switcheroo_register_client(struct pci_dev *pdev,
void (*reprobe)(struct pci_dev *pdev), void (*reprobe)(struct pci_dev *pdev),
bool (*can_switch)(struct pci_dev *pdev)) bool (*can_switch)(struct pci_dev *pdev))
{ {
int index; struct vga_switcheroo_client *client;
mutex_lock(&vgasr_mutex); client = kzalloc(sizeof(*client), GFP_KERNEL);
/* don't do IGD vs DIS here */ if (!client)
if (vgasr_priv.registered_clients & 1) return -ENOMEM;
index = 1;
else client->pwr_state = VGA_SWITCHEROO_ON;
index = 0; client->pdev = pdev;
client->set_gpu_state = set_gpu_state;
vgasr_priv.clients[index].pwr_state = VGA_SWITCHEROO_ON; client->reprobe = reprobe;
vgasr_priv.clients[index].pdev = pdev; client->can_switch = can_switch;
vgasr_priv.clients[index].set_gpu_state = set_gpu_state; client->id = -1;
vgasr_priv.clients[index].reprobe = reprobe;
vgasr_priv.clients[index].can_switch = can_switch;
vgasr_priv.clients[index].id = -1;
if (pdev == vga_default_device()) if (pdev == vga_default_device())
vgasr_priv.clients[index].active = true; client->active = true;
vgasr_priv.registered_clients |= (1 << index); mutex_lock(&vgasr_mutex);
list_add_tail(&client->list, &vgasr_priv.clients);
vgasr_priv.registered_clients++;
/* if we get two clients + handler */ /* if we get two clients + handler */
if (vgasr_priv.registered_clients == 0x3 && vgasr_priv.handler) { if (vgasr_priv.registered_clients == 2 && vgasr_priv.handler) {
printk(KERN_INFO "vga_switcheroo: enabled\n"); printk(KERN_INFO "vga_switcheroo: enabled\n");
vga_switcheroo_enable(); vga_switcheroo_enable();
} }
...@@ -139,18 +142,47 @@ int vga_switcheroo_register_client(struct pci_dev *pdev, ...@@ -139,18 +142,47 @@ int vga_switcheroo_register_client(struct pci_dev *pdev,
} }
EXPORT_SYMBOL(vga_switcheroo_register_client); EXPORT_SYMBOL(vga_switcheroo_register_client);
static struct vga_switcheroo_client *
find_client_from_pci(struct list_head *head, struct pci_dev *pdev)
{
struct vga_switcheroo_client *client;
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;
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;
list_for_each_entry(client, head, list)
if (client->active == true)
return client;
return NULL;
}
void vga_switcheroo_unregister_client(struct pci_dev *pdev) void vga_switcheroo_unregister_client(struct pci_dev *pdev)
{ {
int i; struct vga_switcheroo_client *client;
mutex_lock(&vgasr_mutex); mutex_lock(&vgasr_mutex);
for (i = 0; i < VGA_SWITCHEROO_MAX_CLIENTS; i++) { client = find_client_from_pci(&vgasr_priv.clients, pdev);
if (vgasr_priv.clients[i].pdev == pdev) { if (client) {
vgasr_priv.registered_clients &= ~(1 << i); list_del(&client->list);
break; kfree(client);
} vgasr_priv.registered_clients--;
} }
printk(KERN_INFO "vga_switcheroo: disabled\n"); printk(KERN_INFO "vga_switcheroo: disabled\n");
vga_switcheroo_debugfs_fini(&vgasr_priv); vga_switcheroo_debugfs_fini(&vgasr_priv);
vgasr_priv.active = false; vgasr_priv.active = false;
...@@ -161,29 +193,28 @@ EXPORT_SYMBOL(vga_switcheroo_unregister_client); ...@@ -161,29 +193,28 @@ EXPORT_SYMBOL(vga_switcheroo_unregister_client);
void vga_switcheroo_client_fb_set(struct pci_dev *pdev, void vga_switcheroo_client_fb_set(struct pci_dev *pdev,
struct fb_info *info) struct fb_info *info)
{ {
int i; struct vga_switcheroo_client *client;
mutex_lock(&vgasr_mutex); mutex_lock(&vgasr_mutex);
for (i = 0; i < VGA_SWITCHEROO_MAX_CLIENTS; i++) { client = find_client_from_pci(&vgasr_priv.clients, pdev);
if (vgasr_priv.clients[i].pdev == pdev) { if (client)
vgasr_priv.clients[i].fb_info = info; client->fb_info = info;
break;
}
}
mutex_unlock(&vgasr_mutex); mutex_unlock(&vgasr_mutex);
} }
EXPORT_SYMBOL(vga_switcheroo_client_fb_set); EXPORT_SYMBOL(vga_switcheroo_client_fb_set);
static int vga_switcheroo_show(struct seq_file *m, void *v) static int vga_switcheroo_show(struct seq_file *m, void *v)
{ {
int i; struct vga_switcheroo_client *client;
int i = 0;
mutex_lock(&vgasr_mutex); mutex_lock(&vgasr_mutex);
for (i = 0; i < VGA_SWITCHEROO_MAX_CLIENTS; i++) { list_for_each_entry(client, &vgasr_priv.clients, list) {
seq_printf(m, "%d:%s:%c:%s:%s\n", i, seq_printf(m, "%d:%s:%c:%s:%s\n", i,
vgasr_priv.clients[i].id == VGA_SWITCHEROO_DIS ? "DIS" : "IGD", client->id == VGA_SWITCHEROO_DIS ? "DIS" : "IGD",
vgasr_priv.clients[i].active ? '+' : ' ', client->active ? '+' : ' ',
vgasr_priv.clients[i].pwr_state ? "Pwr" : "Off", client->pwr_state ? "Pwr" : "Off",
pci_name(vgasr_priv.clients[i].pdev)); pci_name(client->pdev));
i++;
} }
mutex_unlock(&vgasr_mutex); mutex_unlock(&vgasr_mutex);
return 0; return 0;
...@@ -217,15 +248,9 @@ static int vga_switchoff(struct vga_switcheroo_client *client) ...@@ -217,15 +248,9 @@ static int vga_switchoff(struct vga_switcheroo_client *client)
/* stage one happens before delay */ /* stage one happens before delay */
static int vga_switchto_stage1(struct vga_switcheroo_client *new_client) static int vga_switchto_stage1(struct vga_switcheroo_client *new_client)
{ {
int i; struct vga_switcheroo_client *active;
struct vga_switcheroo_client *active = NULL;
for (i = 0; i < VGA_SWITCHEROO_MAX_CLIENTS; i++) { active = find_active_client(&vgasr_priv.clients);
if (vgasr_priv.clients[i].active == true) {
active = &vgasr_priv.clients[i];
break;
}
}
if (!active) if (!active)
return 0; return 0;
...@@ -241,15 +266,9 @@ static int vga_switchto_stage1(struct vga_switcheroo_client *new_client) ...@@ -241,15 +266,9 @@ static int vga_switchto_stage1(struct vga_switcheroo_client *new_client)
static int vga_switchto_stage2(struct vga_switcheroo_client *new_client) static int vga_switchto_stage2(struct vga_switcheroo_client *new_client)
{ {
int ret; int ret;
int i; struct vga_switcheroo_client *active;
struct vga_switcheroo_client *active = NULL;
for (i = 0; i < VGA_SWITCHEROO_MAX_CLIENTS; i++) { active = find_active_client(&vgasr_priv.clients);
if (vgasr_priv.clients[i].active == true) {
active = &vgasr_priv.clients[i];
break;
}
}
if (!active) if (!active)
return 0; return 0;
...@@ -275,13 +294,26 @@ static int vga_switchto_stage2(struct vga_switcheroo_client *new_client) ...@@ -275,13 +294,26 @@ static int vga_switchto_stage2(struct vga_switcheroo_client *new_client)
return 0; return 0;
} }
static bool check_can_switch(void)
{
struct vga_switcheroo_client *client;
list_for_each_entry(client, &vgasr_priv.clients, list) {
if (!client->can_switch(client->pdev)) {
printk(KERN_ERR "vga_switcheroo: client %x refused switch\n", client->id);
return false;
}
}
return true;
}
static ssize_t static ssize_t
vga_switcheroo_debugfs_write(struct file *filp, const char __user *ubuf, vga_switcheroo_debugfs_write(struct file *filp, const char __user *ubuf,
size_t cnt, loff_t *ppos) size_t cnt, loff_t *ppos)
{ {
char usercmd[64]; char usercmd[64];
const char *pdev_name; const char *pdev_name;
int i, ret; int ret;
bool delay = false, can_switch; bool delay = false, can_switch;
bool just_mux = false; bool just_mux = false;
int client_id = -1; int client_id = -1;
...@@ -302,21 +334,21 @@ vga_switcheroo_debugfs_write(struct file *filp, const char __user *ubuf, ...@@ -302,21 +334,21 @@ vga_switcheroo_debugfs_write(struct file *filp, const char __user *ubuf,
/* pwr off the device not in use */ /* pwr off the device not in use */
if (strncmp(usercmd, "OFF", 3) == 0) { if (strncmp(usercmd, "OFF", 3) == 0) {
for (i = 0; i < VGA_SWITCHEROO_MAX_CLIENTS; i++) { list_for_each_entry(client, &vgasr_priv.clients, list) {
if (vgasr_priv.clients[i].active) if (client->active)
continue; continue;
if (vgasr_priv.clients[i].pwr_state == VGA_SWITCHEROO_ON) if (client->pwr_state == VGA_SWITCHEROO_ON)
vga_switchoff(&vgasr_priv.clients[i]); vga_switchoff(client);
} }
goto out; goto out;
} }
/* pwr on the device not in use */ /* pwr on the device not in use */
if (strncmp(usercmd, "ON", 2) == 0) { if (strncmp(usercmd, "ON", 2) == 0) {
for (i = 0; i < VGA_SWITCHEROO_MAX_CLIENTS; i++) { list_for_each_entry(client, &vgasr_priv.clients, list) {
if (vgasr_priv.clients[i].active) if (client->active)
continue; continue;
if (vgasr_priv.clients[i].pwr_state == VGA_SWITCHEROO_OFF) if (client->pwr_state == VGA_SWITCHEROO_OFF)
vga_switchon(&vgasr_priv.clients[i]); vga_switchon(client);
} }
goto out; goto out;
} }
...@@ -349,13 +381,9 @@ vga_switcheroo_debugfs_write(struct file *filp, const char __user *ubuf, ...@@ -349,13 +381,9 @@ vga_switcheroo_debugfs_write(struct file *filp, const char __user *ubuf,
if (client_id == -1) if (client_id == -1)
goto out; goto out;
client = find_client_from_id(&vgasr_priv.clients, client_id);
for (i = 0; i < VGA_SWITCHEROO_MAX_CLIENTS; i++) { if (!client)
if (vgasr_priv.clients[i].id == client_id) { goto out;
client = &vgasr_priv.clients[i];
break;
}
}
vgasr_priv.delayed_switch_active = false; vgasr_priv.delayed_switch_active = false;
...@@ -364,23 +392,16 @@ vga_switcheroo_debugfs_write(struct file *filp, const char __user *ubuf, ...@@ -364,23 +392,16 @@ vga_switcheroo_debugfs_write(struct file *filp, const char __user *ubuf,
goto out; goto out;
} }
if (client->active == true) if (client->active)
goto out; goto out;
/* okay we want a switch - test if devices are willing to switch */ /* okay we want a switch - test if devices are willing to switch */
can_switch = true; can_switch = check_can_switch();
for (i = 0; i < VGA_SWITCHEROO_MAX_CLIENTS; i++) {
can_switch = vgasr_priv.clients[i].can_switch(vgasr_priv.clients[i].pdev);
if (can_switch == false) {
printk(KERN_ERR "vga_switcheroo: client %d refused switch\n", i);
break;
}
}
if (can_switch == false && delay == false) if (can_switch == false && delay == false)
goto out; goto out;
if (can_switch == true) { if (can_switch) {
pdev_name = pci_name(client->pdev); pdev_name = pci_name(client->pdev);
ret = vga_switchto_stage1(client); ret = vga_switchto_stage1(client);
if (ret) if (ret)
...@@ -452,10 +473,8 @@ static int vga_switcheroo_debugfs_init(struct vgasr_priv *priv) ...@@ -452,10 +473,8 @@ static int vga_switcheroo_debugfs_init(struct vgasr_priv *priv)
int vga_switcheroo_process_delayed_switch(void) int vga_switcheroo_process_delayed_switch(void)
{ {
struct vga_switcheroo_client *client = NULL; struct vga_switcheroo_client *client;
const char *pdev_name; const char *pdev_name;
bool can_switch = true;
int i;
int ret; int ret;
int err = -EINVAL; int err = -EINVAL;
...@@ -465,17 +484,9 @@ int vga_switcheroo_process_delayed_switch(void) ...@@ -465,17 +484,9 @@ int vga_switcheroo_process_delayed_switch(void)
printk(KERN_INFO "vga_switcheroo: processing delayed switch to %d\n", vgasr_priv.delayed_client_id); printk(KERN_INFO "vga_switcheroo: processing delayed switch to %d\n", vgasr_priv.delayed_client_id);
for (i = 0; i < VGA_SWITCHEROO_MAX_CLIENTS; i++) { client = find_client_from_id(&vgasr_priv.clients,
if (vgasr_priv.clients[i].id == vgasr_priv.delayed_client_id) vgasr_priv.delayed_client_id);
client = &vgasr_priv.clients[i]; if (!client || !check_can_switch())
can_switch = vgasr_priv.clients[i].can_switch(vgasr_priv.clients[i].pdev);
if (can_switch == false) {
printk(KERN_ERR "vga_switcheroo: client %d refused switch\n", i);
break;
}
}
if (can_switch == false || client == NULL)
goto err; goto err;
pdev_name = pci_name(client->pdev); pdev_name = pci_name(client->pdev);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment