32 #include <linux/module.h>
33 #include <linux/kernel.h>
34 #include <linux/pci.h>
35 #include <linux/errno.h>
37 #include <linux/list.h>
38 #include <linux/sched.h>
39 #include <linux/wait.h>
41 #include <linux/poll.h>
43 #include <linux/slab.h>
49 static void vga_arbiter_notify_clients(
void);
72 static int vga_count, vga_decode_count;
73 static bool vga_arbiter_used;
78 static const char *vga_iostate_to_str(
unsigned int iostate)
93 static int vga_str_to_iostate(
char *
buf,
int str_size,
int *io_state)
97 if (
strncmp(buf,
"none", 4) == 0) {
103 if (
strncmp(buf,
"io+mem", 6) == 0)
105 else if (
strncmp(buf,
"io", 2) == 0)
107 else if (
strncmp(buf,
"mem", 3) == 0)
115 #ifndef __ARCH_HAS_VGA_DEFAULT_DEVICE
117 static struct pci_dev *vga_default;
120 static void vga_arb_device_card_gone(
struct pci_dev *pdev);
128 if (pdev == vgadev->pdev)
134 #ifndef __ARCH_HAS_VGA_DEFAULT_DEVICE
144 if (vga_default == pdev)
152 static inline void vga_irq_set_state(
struct vga_device *vgadev,
bool state)
162 static void vga_check_first_use(
void)
167 if (!vga_arbiter_used) {
168 vga_arbiter_used =
true;
169 vga_arbiter_notify_clients();
176 unsigned int wants, legacy_wants,
match;
178 unsigned int pci_bits;
191 pr_debug(
"%s: %d\n", __func__, rsrc);
195 wants = rsrc & ~vgadev->
owns;
205 if (legacy_wants == 0)
210 unsigned int lwants = legacy_wants;
211 unsigned int change_bridge = 0;
214 if (vgadev == conflict)
220 if (!vga_conflicts(vgadev->
pdev, conflict->
pdev))
229 if (vgadev->
pdev->bus != conflict->
pdev->bus) {
237 if (conflict->
locks & lwants)
246 match = lwants & conflict->
owns;
258 vga_irq_set_state(conflict,
false);
259 flags |= PCI_VGA_STATE_CHANGE_DECODES;
267 flags |= PCI_VGA_STATE_CHANGE_BRIDGE;
270 conflict->
owns &= ~lwants;
273 conflict->
owns &= ~VGA_RSRC_NORMAL_MEM;
275 conflict->
owns &= ~VGA_RSRC_NORMAL_IO;
288 flags |= PCI_VGA_STATE_CHANGE_DECODES;
289 if (wants & (VGA_RSRC_LEGACY_MEM|VGA_RSRC_NORMAL_MEM))
294 if (!!(wants & VGA_RSRC_LEGACY_MASK))
295 flags |= PCI_VGA_STATE_CHANGE_BRIDGE;
300 vga_irq_set_state(vgadev,
true);
307 if (rsrc & VGA_RSRC_LEGACY_MEM)
309 if (rsrc & VGA_RSRC_NORMAL_IO)
311 if (rsrc & VGA_RSRC_NORMAL_MEM)
317 static void __vga_put(
struct vga_device *vgadev,
unsigned int rsrc)
319 unsigned int old_locks = vgadev->
locks;
326 if ((rsrc & VGA_RSRC_NORMAL_IO) && vgadev->
io_norm_cnt > 0) {
328 if (vgadev->
decodes & VGA_RSRC_LEGACY_IO)
331 if ((rsrc & VGA_RSRC_NORMAL_MEM) && vgadev->
mem_norm_cnt > 0) {
333 if (vgadev->
decodes & VGA_RSRC_LEGACY_MEM)
336 if ((rsrc & VGA_RSRC_LEGACY_IO) && vgadev->
io_lock_cnt > 0)
338 if ((rsrc & VGA_RSRC_LEGACY_MEM) && vgadev->
mem_lock_cnt > 0)
345 vgadev->
locks &= ~VGA_RSRC_LEGACY_IO;
347 vgadev->
locks &= ~VGA_RSRC_LEGACY_MEM;
352 if (old_locks != vgadev->
locks)
363 vga_check_first_use();
372 vgadev = vgadev_find(pdev);
373 if (vgadev == NULL) {
374 spin_unlock_irqrestore(&vga_lock, flags);
378 conflict = __vga_tryget(vgadev, rsrc);
379 spin_unlock_irqrestore(&vga_lock, flags);
380 if (conflict == NULL)
390 init_waitqueue_entry(&wait,
current);
413 vga_check_first_use();
421 vgadev = vgadev_find(pdev);
422 if (vgadev == NULL) {
426 if (__vga_tryget(vgadev, rsrc))
429 spin_unlock_irqrestore(&vga_lock, flags);
445 vgadev = vgadev_find(pdev);
448 __vga_put(vgadev, rsrc);
450 spin_unlock_irqrestore(&vga_lock, flags);
463 static void vga_arbiter_check_bridge_sharing(
struct vga_device *vgadev)
471 if (list_empty(&vga_list))
475 new_bus = vgadev->
pdev->bus;
477 new_bridge = new_bus->
self;
481 bus = same_bridge_vgadev->
pdev->bus;
485 if (new_bridge == bridge) {
498 if (bridge == vgadev->
pdev->bus->self)
504 new_bus = new_bus->
parent;
514 static bool vga_arbiter_add_pci_device(
struct pci_dev *pdev)
528 if (vgadev == NULL) {
529 pr_err(
"vgaarb: failed to allocate pci device\n");
537 memset(vgadev, 0,
sizeof(*vgadev));
541 if (vgadev_find(pdev) != NULL) {
548 vgadev->
decodes = VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
581 #ifndef __ARCH_HAS_VGA_DEFAULT_DEVICE
582 if (vga_default == NULL &&
583 ((vgadev->
owns & VGA_RSRC_LEGACY_MASK) == VGA_RSRC_LEGACY_MASK))
587 vga_arbiter_check_bridge_sharing(vgadev);
590 list_add(&vgadev->
list, &vga_list);
592 pr_info(
"vgaarb: device added: PCI:%s,decodes=%s,owns=%s,locks=%s\n",
594 vga_iostate_to_str(vgadev->
decodes),
595 vga_iostate_to_str(vgadev->
owns),
596 vga_iostate_to_str(vgadev->
locks));
598 spin_unlock_irqrestore(&vga_lock, flags);
601 spin_unlock_irqrestore(&vga_lock, flags);
606 static bool vga_arbiter_del_pci_device(
struct pci_dev *pdev)
613 vgadev = vgadev_find(pdev);
614 if (vgadev == NULL) {
619 #ifndef __ARCH_HAS_VGA_DEFAULT_DEVICE
620 if (vga_default == pdev)
624 if (vgadev->
decodes & (VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM))
633 vga_arb_device_card_gone(pdev);
638 spin_unlock_irqrestore(&vga_lock, flags);
644 static inline void vga_update_device_decodes(
struct vga_device *vgadev,
653 pr_info(
"vgaarb: device changed decodes: PCI:%s,olddecodes=%s,decodes=%s:owns=%s\n",
654 pci_name(vgadev->
pdev),
655 vga_iostate_to_str(old_decodes),
656 vga_iostate_to_str(vgadev->
decodes),
657 vga_iostate_to_str(vgadev->
owns));
662 if ((vgadev->
owns & old_decodes) && (vga_count > 1)) {
664 vgadev->
owns &= ~old_decodes;
666 if ((new_vgadev != vgadev) &&
667 (new_vgadev->
decodes & VGA_RSRC_LEGACY_MASK)) {
668 pr_info(
"vgaarb: transferring owner from PCI:%s to PCI:%s\n", pci_name(vgadev->
pdev), pci_name(new_vgadev->
pdev));
669 conflict = __vga_tryget(new_vgadev, VGA_RSRC_LEGACY_MASK);
671 __vga_put(new_vgadev, VGA_RSRC_LEGACY_MASK);
678 if (old_decodes != new_decodes) {
679 if (new_decodes & (VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM))
684 pr_debug(
"vgaarb: decoding count now is: %d\n", vga_decode_count);
695 vgadev = vgadev_find(pdev);
704 vga_update_device_decodes(vgadev, decodes);
711 spin_unlock_irqrestore(&vga_lock, flags);
716 __vga_set_legacy_decoding(pdev, decodes,
false);
730 vgadev = vgadev_find(pdev);
740 spin_unlock_irqrestore(&vga_lock, flags);
799 #define MAX_USER_CARDS CONFIG_VGA_ARB_MAX_GPUS
800 #define PCI_INVALID_CARD ((struct pci_dev *)-1UL)
827 static int vga_pci_str_to_vars(
char *buf,
int count,
unsigned int *domain,
828 unsigned int *bus,
unsigned int *
devfn)
834 n =
sscanf(buf,
"PCI:%x:%x:%x.%x", domain, bus, &slot, &func);
844 size_t count, loff_t *ppos)
866 spin_unlock_irqrestore(&vga_lock, flags);
867 len =
sprintf(lbuf,
"invalid");
872 vgadev = vgadev_find(pdev);
873 if (vgadev == NULL) {
878 vga_arb_device_card_gone(pdev);
879 spin_unlock_irqrestore(&vga_lock, flags);
880 len =
sprintf(lbuf,
"invalid");
886 "count:%d,PCI:%s,decodes=%s,owns=%s,locks=%s(%d:%d)\n",
887 vga_decode_count, pci_name(pdev),
888 vga_iostate_to_str(vgadev->
decodes),
889 vga_iostate_to_str(vgadev->
owns),
890 vga_iostate_to_str(vgadev->
locks),
893 spin_unlock_irqrestore(&vga_lock, flags);
910 static ssize_t vga_arb_write(
struct file *file,
const char __user * buf,
911 size_t count, loff_t *ppos)
917 unsigned int io_state;
919 char *kbuf, *curr_pos;
920 size_t remaining =
count;
937 if (
strncmp(curr_pos,
"lock ", 5) == 0) {
941 pr_debug(
"client 0x%p called 'lock'\n", priv);
943 if (!vga_str_to_iostate(curr_pos, remaining, &io_state)) {
953 if (priv->
target == NULL) {
958 vga_get_uninterruptible(pdev, io_state);
962 if (priv->
cards[i].pdev == pdev) {
963 if (io_state & VGA_RSRC_LEGACY_IO)
965 if (io_state & VGA_RSRC_LEGACY_MEM)
973 }
else if (
strncmp(curr_pos,
"unlock ", 7) == 0) {
977 pr_debug(
"client 0x%p called 'unlock'\n", priv);
979 if (
strncmp(curr_pos,
"all", 3) == 0)
982 if (!vga_str_to_iostate
983 (curr_pos, remaining, &io_state)) {
996 if (priv->
target == NULL) {
1001 if (priv->
cards[i].pdev == pdev)
1010 if (io_state & VGA_RSRC_LEGACY_IO && uc->
io_cnt == 0) {
1015 if (io_state & VGA_RSRC_LEGACY_MEM && uc->
mem_cnt == 0) {
1022 if (io_state & VGA_RSRC_LEGACY_IO)
1024 if (io_state & VGA_RSRC_LEGACY_MEM)
1029 }
else if (
strncmp(curr_pos,
"trylock ", 8) == 0) {
1033 pr_debug(
"client 0x%p called 'trylock'\n", priv);
1035 if (!vga_str_to_iostate(curr_pos, remaining, &io_state)) {
1047 if (priv->
target == NULL) {
1055 if (priv->
cards[i].pdev == pdev) {
1056 if (io_state & VGA_RSRC_LEGACY_IO)
1058 if (io_state & VGA_RSRC_LEGACY_MEM)
1059 priv->
cards[
i].mem_cnt++;
1070 }
else if (
strncmp(curr_pos,
"target ", 7) == 0) {
1076 pr_debug(
"client 0x%p called 'target'\n", priv);
1078 if (!
strncmp(curr_pos,
"default", 7))
1081 if (!vga_pci_str_to_vars(curr_pos, remaining,
1082 &domain, &bus, &devfn)) {
1086 pr_debug(
"vgaarb: %s ==> %x:%x:%x.%x\n", curr_pos,
1090 pr_debug(
"vgaarb: pdev %p\n", pdev);
1092 pr_err(
"vgaarb: invalid PCI address %x:%x:%x\n",
1093 domain, bus, devfn);
1099 vgadev = vgadev_find(pdev);
1100 pr_debug(
"vgaarb: vgadev %p\n", vgadev);
1101 if (vgadev == NULL) {
1102 pr_err(
"vgaarb: this pci device is not a vga device\n");
1110 if (priv->
cards[i].pdev == pdev)
1112 if (priv->
cards[i].pdev == NULL) {
1114 priv->
cards[
i].io_cnt = 0;
1115 priv->
cards[
i].mem_cnt = 0;
1119 if (i == MAX_USER_CARDS) {
1120 pr_err(
"vgaarb: maximum user cards (%d) number reached!\n",
1133 }
else if (
strncmp(curr_pos,
"decodes ", 8) == 0) {
1136 pr_debug(
"vgaarb: client 0x%p called 'decodes'\n", priv);
1138 if (!vga_str_to_iostate(curr_pos, remaining, &io_state)) {
1143 if (priv->
target == NULL) {
1148 __vga_set_legacy_decoding(pdev, io_state,
true);
1161 static unsigned int vga_arb_fpoll(
struct file *file,
poll_table *
wait)
1169 poll_wait(file, &vga_wait_queue, wait);
1173 static int vga_arb_open(
struct inode *
inode,
struct file *file)
1176 unsigned long flags;
1187 list_add(&priv->
list, &vga_user_list);
1188 spin_unlock_irqrestore(&vga_user_lock, flags);
1193 priv->
cards[0].io_cnt = 0;
1194 priv->
cards[0].mem_cnt = 0;
1200 static int vga_arb_release(
struct inode *inode,
struct file *file)
1204 unsigned long flags;
1216 if (uc->
pdev == NULL)
1218 pr_debug(
"uc->io_cnt == %d, uc->mem_cnt == %d\n",
1225 spin_unlock_irqrestore(&vga_user_lock, flags);
1232 static void vga_arb_device_card_gone(
struct pci_dev *pdev)
1240 static void vga_arbiter_notify_clients(
void)
1243 unsigned long flags;
1247 if (!vga_arbiter_used)
1258 vga_update_device_decodes(vgadev, new_decodes);
1261 spin_unlock_irqrestore(&vga_lock, flags);
1277 notify = vga_arbiter_add_pci_device(pdev);
1279 notify = vga_arbiter_del_pci_device(pdev);
1282 vga_arbiter_notify_clients();
1287 .notifier_call = pci_notify,
1291 .read = vga_arb_read,
1292 .write = vga_arb_write,
1293 .poll = vga_arb_fpoll,
1294 .open = vga_arb_open,
1295 .release = vga_arb_release,
1303 static int __init vga_arb_device_init(
void)
1311 pr_err(
"vgaarb: error %d registering device\n", rc);
1321 vga_arbiter_add_pci_device(pdev);
1327 pr_info(
"vgaarb: bridge control possible %s\n", pci_name(vgadev->
pdev));
1329 pr_info(
"vgaarb: no bridge control possible %s\n", pci_name(vgadev->
pdev));