4 * (C) Copyright 2005 Benjamin Herrenschmidt <benh@kernel.crashing.org>
5 * (C) Copyright 2007 Paulo R. Zanoni <przanoni@gmail.com>
6 * (C) Copyright 2007, 2009 Tiago Vignatti <vignatti@freedesktop.org>
8 * Implements the VGA arbitration. For details refer to
9 * Documentation/vgaarbiter.txt
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/pci.h>
15 #include <linux/errno.h>
16 #include <linux/init.h>
17 #include <linux/list.h>
18 #include <linux/sched.h>
19 #include <linux/wait.h>
20 #include <linux/spinlock.h>
21 #include <linux/poll.h>
22 #include <linux/miscdevice.h>
23 #include <linux/slab.h>
25 #include <linux/uaccess.h>
27 #include <linux/vgaarb.h>
29 static void vga_arbiter_notify_clients(void);
31 * We keep a list of all vga devices in the system to speed
32 * up the various operations of the arbiter
35 struct list_head list;
37 unsigned int decodes; /* what does it decodes */
38 unsigned int owns; /* what does it owns */
39 unsigned int locks; /* what does it locks */
40 unsigned int io_lock_cnt; /* legacy IO lock count */
41 unsigned int mem_lock_cnt; /* legacy MEM lock count */
42 unsigned int io_norm_cnt; /* normal IO count */
43 unsigned int mem_norm_cnt; /* normal MEM count */
45 /* allow IRQ enable/disable hook */
47 void (*irq_set_state)(void *cookie, bool enable);
48 unsigned int (*set_vga_decode)(void *cookie, bool decode);
51 static LIST_HEAD(vga_list);
52 static int vga_count, vga_decode_count;
53 static bool vga_arbiter_used;
54 static DEFINE_SPINLOCK(vga_lock);
55 static DECLARE_WAIT_QUEUE_HEAD(vga_wait_queue);
58 static const char *vga_iostate_to_str(unsigned int iostate)
60 /* Ignore VGA_RSRC_IO and VGA_RSRC_MEM */
61 iostate &= VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM;
63 case VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM:
65 case VGA_RSRC_LEGACY_IO:
67 case VGA_RSRC_LEGACY_MEM:
73 static int vga_str_to_iostate(char *buf, int str_size, int *io_state)
75 /* we could in theory hand out locks on IO and mem
76 * separately to userspace but it can cause deadlocks */
77 if (strncmp(buf, "none", 4) == 0) {
78 *io_state = VGA_RSRC_NONE;
82 /* XXX We're not chekcing the str_size! */
83 if (strncmp(buf, "io+mem", 6) == 0)
85 else if (strncmp(buf, "io", 2) == 0)
87 else if (strncmp(buf, "mem", 3) == 0)
91 *io_state = VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM;
95 #ifndef __ARCH_HAS_VGA_DEFAULT_DEVICE
96 /* this is only used a cookie - it should not be dereferenced */
97 static struct pci_dev *vga_default;
100 static void vga_arb_device_card_gone(struct pci_dev *pdev);
102 /* Find somebody in our list */
103 static struct vga_device *vgadev_find(struct pci_dev *pdev)
105 struct vga_device *vgadev;
107 list_for_each_entry(vgadev, &vga_list, list)
108 if (pdev == vgadev->pdev)
113 /* Returns the default VGA device (vgacon's babe) */
114 #ifndef __ARCH_HAS_VGA_DEFAULT_DEVICE
115 struct pci_dev *vga_default_device(void)
121 static inline void vga_irq_set_state(struct vga_device *vgadev, bool state)
123 if (vgadev->irq_set_state)
124 vgadev->irq_set_state(vgadev->cookie, state);
128 /* If we don't ever use VGA arb we should avoid
129 turning off anything anywhere due to old X servers getting
130 confused about the boot device not being VGA */
131 static void vga_check_first_use(void)
133 /* we should inform all GPUs in the system that
134 * VGA arb has occured and to try and disable resources
136 if (!vga_arbiter_used) {
137 vga_arbiter_used = true;
138 vga_arbiter_notify_clients();
142 static struct vga_device *__vga_tryget(struct vga_device *vgadev,
145 unsigned int wants, legacy_wants, match;
146 struct vga_device *conflict;
147 unsigned int pci_bits;
148 /* Account for "normal" resources to lock. If we decode the legacy,
149 * counterpart, we need to request it as well
151 if ((rsrc & VGA_RSRC_NORMAL_IO) &&
152 (vgadev->decodes & VGA_RSRC_LEGACY_IO))
153 rsrc |= VGA_RSRC_LEGACY_IO;
154 if ((rsrc & VGA_RSRC_NORMAL_MEM) &&
155 (vgadev->decodes & VGA_RSRC_LEGACY_MEM))
156 rsrc |= VGA_RSRC_LEGACY_MEM;
158 pr_debug("%s: %d\n", __func__, rsrc);
159 pr_debug("%s: owns: %d\n", __func__, vgadev->owns);
161 /* Check what resources we need to acquire */
162 wants = rsrc & ~vgadev->owns;
164 /* We already own everything, just mark locked & bye bye */
168 /* We don't need to request a legacy resource, we just enable
169 * appropriate decoding and go
171 legacy_wants = wants & VGA_RSRC_LEGACY_MASK;
172 if (legacy_wants == 0)
175 /* Ok, we don't, let's find out how we need to kick off */
176 list_for_each_entry(conflict, &vga_list, list) {
177 unsigned int lwants = legacy_wants;
178 unsigned int change_bridge = 0;
180 /* Don't conflict with myself */
181 if (vgadev == conflict)
184 /* Check if the architecture allows a conflict between those
185 * 2 devices or if they are on separate domains
187 if (!vga_conflicts(vgadev->pdev, conflict->pdev))
190 /* We have a possible conflict. before we go further, we must
191 * check if we sit on the same bus as the conflicting device.
192 * if we don't, then we must tie both IO and MEM resources
193 * together since there is only a single bit controlling
194 * VGA forwarding on P2P bridges
196 if (vgadev->pdev->bus != conflict->pdev->bus) {
198 lwants = VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM;
201 /* Check if the guy has a lock on the resource. If he does,
202 * return the conflicting entry
204 if (conflict->locks & lwants)
207 /* Ok, now check if he owns the resource we want. We don't need
208 * to check "decodes" since it should be impossible to own
209 * own legacy resources you don't decode unless I have a bug
212 WARN_ON(conflict->owns & ~conflict->decodes);
213 match = lwants & conflict->owns;
217 /* looks like he doesn't have a lock, we can steal
220 vga_irq_set_state(conflict, false);
223 if (lwants & (VGA_RSRC_LEGACY_MEM|VGA_RSRC_NORMAL_MEM))
224 pci_bits |= PCI_COMMAND_MEMORY;
225 if (lwants & (VGA_RSRC_LEGACY_IO|VGA_RSRC_NORMAL_IO))
226 pci_bits |= PCI_COMMAND_IO;
228 pci_set_vga_state(conflict->pdev, false, pci_bits,
230 conflict->owns &= ~lwants;
231 /* If he also owned non-legacy, that is no longer the case */
232 if (lwants & VGA_RSRC_LEGACY_MEM)
233 conflict->owns &= ~VGA_RSRC_NORMAL_MEM;
234 if (lwants & VGA_RSRC_LEGACY_IO)
235 conflict->owns &= ~VGA_RSRC_NORMAL_IO;
239 /* ok dude, we got it, everybody conflicting has been disabled, let's
240 * enable us. Make sure we don't mark a bit in "owns" that we don't
241 * also have in "decodes". We can lock resources we don't decode but
245 if (wants & (VGA_RSRC_LEGACY_MEM|VGA_RSRC_NORMAL_MEM))
246 pci_bits |= PCI_COMMAND_MEMORY;
247 if (wants & (VGA_RSRC_LEGACY_IO|VGA_RSRC_NORMAL_IO))
248 pci_bits |= PCI_COMMAND_IO;
249 pci_set_vga_state(vgadev->pdev, true, pci_bits, !!(wants & VGA_RSRC_LEGACY_MASK));
251 vga_irq_set_state(vgadev, true);
252 vgadev->owns |= (wants & vgadev->decodes);
254 vgadev->locks |= (rsrc & VGA_RSRC_LEGACY_MASK);
255 if (rsrc & VGA_RSRC_LEGACY_IO)
256 vgadev->io_lock_cnt++;
257 if (rsrc & VGA_RSRC_LEGACY_MEM)
258 vgadev->mem_lock_cnt++;
259 if (rsrc & VGA_RSRC_NORMAL_IO)
260 vgadev->io_norm_cnt++;
261 if (rsrc & VGA_RSRC_NORMAL_MEM)
262 vgadev->mem_norm_cnt++;
267 static void __vga_put(struct vga_device *vgadev, unsigned int rsrc)
269 unsigned int old_locks = vgadev->locks;
271 pr_debug("%s\n", __func__);
273 /* Update our counters, and account for equivalent legacy resources
276 if ((rsrc & VGA_RSRC_NORMAL_IO) && vgadev->io_norm_cnt > 0) {
277 vgadev->io_norm_cnt--;
278 if (vgadev->decodes & VGA_RSRC_LEGACY_IO)
279 rsrc |= VGA_RSRC_LEGACY_IO;
281 if ((rsrc & VGA_RSRC_NORMAL_MEM) && vgadev->mem_norm_cnt > 0) {
282 vgadev->mem_norm_cnt--;
283 if (vgadev->decodes & VGA_RSRC_LEGACY_MEM)
284 rsrc |= VGA_RSRC_LEGACY_MEM;
286 if ((rsrc & VGA_RSRC_LEGACY_IO) && vgadev->io_lock_cnt > 0)
287 vgadev->io_lock_cnt--;
288 if ((rsrc & VGA_RSRC_LEGACY_MEM) && vgadev->mem_lock_cnt > 0)
289 vgadev->mem_lock_cnt--;
291 /* Just clear lock bits, we do lazy operations so we don't really
292 * have to bother about anything else at this point
294 if (vgadev->io_lock_cnt == 0)
295 vgadev->locks &= ~VGA_RSRC_LEGACY_IO;
296 if (vgadev->mem_lock_cnt == 0)
297 vgadev->locks &= ~VGA_RSRC_LEGACY_MEM;
299 /* Kick the wait queue in case somebody was waiting if we actually
302 if (old_locks != vgadev->locks)
303 wake_up_all(&vga_wait_queue);
306 int vga_get(struct pci_dev *pdev, unsigned int rsrc, int interruptible)
308 struct vga_device *vgadev, *conflict;
313 vga_check_first_use();
314 /* The one who calls us should check for this, but lets be sure... */
316 pdev = vga_default_device();
321 spin_lock_irqsave(&vga_lock, flags);
322 vgadev = vgadev_find(pdev);
323 if (vgadev == NULL) {
324 spin_unlock_irqrestore(&vga_lock, flags);
328 conflict = __vga_tryget(vgadev, rsrc);
329 spin_unlock_irqrestore(&vga_lock, flags);
330 if (conflict == NULL)
334 /* We have a conflict, we wait until somebody kicks the
335 * work queue. Currently we have one work queue that we
336 * kick each time some resources are released, but it would
337 * be fairly easy to have a per device one so that we only
338 * need to attach to the conflicting device
340 init_waitqueue_entry(&wait, current);
341 add_wait_queue(&vga_wait_queue, &wait);
342 set_current_state(interruptible ?
344 TASK_UNINTERRUPTIBLE);
345 if (signal_pending(current)) {
350 remove_wait_queue(&vga_wait_queue, &wait);
351 set_current_state(TASK_RUNNING);
355 EXPORT_SYMBOL(vga_get);
357 int vga_tryget(struct pci_dev *pdev, unsigned int rsrc)
359 struct vga_device *vgadev;
363 vga_check_first_use();
365 /* The one who calls us should check for this, but lets be sure... */
367 pdev = vga_default_device();
370 spin_lock_irqsave(&vga_lock, flags);
371 vgadev = vgadev_find(pdev);
372 if (vgadev == NULL) {
376 if (__vga_tryget(vgadev, rsrc))
379 spin_unlock_irqrestore(&vga_lock, flags);
382 EXPORT_SYMBOL(vga_tryget);
384 void vga_put(struct pci_dev *pdev, unsigned int rsrc)
386 struct vga_device *vgadev;
389 /* The one who calls us should check for this, but lets be sure... */
391 pdev = vga_default_device();
394 spin_lock_irqsave(&vga_lock, flags);
395 vgadev = vgadev_find(pdev);
398 __vga_put(vgadev, rsrc);
400 spin_unlock_irqrestore(&vga_lock, flags);
402 EXPORT_SYMBOL(vga_put);
405 * Currently, we assume that the "initial" setup of the system is
406 * not sane, that is we come up with conflicting devices and let
407 * the arbiter's client decides if devices decodes or not legacy
410 static bool vga_arbiter_add_pci_device(struct pci_dev *pdev)
412 struct vga_device *vgadev;
415 struct pci_dev *bridge;
418 /* Only deal with VGA class devices */
419 if ((pdev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
422 /* Allocate structure */
423 vgadev = kmalloc(sizeof(struct vga_device), GFP_KERNEL);
424 if (vgadev == NULL) {
425 pr_err("vgaarb: failed to allocate pci device\n");
426 /* What to do on allocation failure ? For now, let's
427 * just do nothing, I'm not sure there is anything saner
433 memset(vgadev, 0, sizeof(*vgadev));
435 /* Take lock & check for duplicates */
436 spin_lock_irqsave(&vga_lock, flags);
437 if (vgadev_find(pdev) != NULL) {
443 /* By default, assume we decode everything */
444 vgadev->decodes = VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
445 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
447 /* by default mark it as decoding */
449 /* Mark that we "own" resources based on our enables, we will
450 * clear that below if the bridge isn't forwarding
452 pci_read_config_word(pdev, PCI_COMMAND, &cmd);
453 if (cmd & PCI_COMMAND_IO)
454 vgadev->owns |= VGA_RSRC_LEGACY_IO;
455 if (cmd & PCI_COMMAND_MEMORY)
456 vgadev->owns |= VGA_RSRC_LEGACY_MEM;
458 /* Check if VGA cycles can get down to us */
464 pci_read_config_word(bridge, PCI_BRIDGE_CONTROL,
466 if (!(l & PCI_BRIDGE_CTL_VGA)) {
474 /* Deal with VGA default device. Use first enabled one
475 * by default if arch doesn't have it's own hook
477 #ifndef __ARCH_HAS_VGA_DEFAULT_DEVICE
478 if (vga_default == NULL &&
479 ((vgadev->owns & VGA_RSRC_LEGACY_MASK) == VGA_RSRC_LEGACY_MASK))
480 vga_default = pci_dev_get(pdev);
483 /* Add to the list */
484 list_add(&vgadev->list, &vga_list);
486 pr_info("vgaarb: device added: PCI:%s,decodes=%s,owns=%s,locks=%s\n",
488 vga_iostate_to_str(vgadev->decodes),
489 vga_iostate_to_str(vgadev->owns),
490 vga_iostate_to_str(vgadev->locks));
492 spin_unlock_irqrestore(&vga_lock, flags);
495 spin_unlock_irqrestore(&vga_lock, flags);
500 static bool vga_arbiter_del_pci_device(struct pci_dev *pdev)
502 struct vga_device *vgadev;
506 spin_lock_irqsave(&vga_lock, flags);
507 vgadev = vgadev_find(pdev);
508 if (vgadev == NULL) {
513 if (vga_default == pdev) {
514 pci_dev_put(vga_default);
518 if (vgadev->decodes & (VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM))
521 /* Remove entry from list */
522 list_del(&vgadev->list);
524 /* Notify userland driver that the device is gone so it discards
525 * it's copies of the pci_dev pointer
527 vga_arb_device_card_gone(pdev);
529 /* Wake up all possible waiters */
530 wake_up_all(&vga_wait_queue);
532 spin_unlock_irqrestore(&vga_lock, flags);
537 /* this is called with the lock */
538 static inline void vga_update_device_decodes(struct vga_device *vgadev,
542 struct vga_device *new_vgadev, *conflict;
544 old_decodes = vgadev->decodes;
545 vgadev->decodes = new_decodes;
547 pr_info("vgaarb: device changed decodes: PCI:%s,olddecodes=%s,decodes=%s:owns=%s\n",
548 pci_name(vgadev->pdev),
549 vga_iostate_to_str(old_decodes),
550 vga_iostate_to_str(vgadev->decodes),
551 vga_iostate_to_str(vgadev->owns));
554 /* if we own the decodes we should move them along to
556 if ((vgadev->owns & old_decodes) && (vga_count > 1)) {
557 /* set us to own nothing */
558 vgadev->owns &= ~old_decodes;
559 list_for_each_entry(new_vgadev, &vga_list, list) {
560 if ((new_vgadev != vgadev) &&
561 (new_vgadev->decodes & VGA_RSRC_LEGACY_MASK)) {
562 pr_info("vgaarb: transferring owner from PCI:%s to PCI:%s\n", pci_name(vgadev->pdev), pci_name(new_vgadev->pdev));
563 conflict = __vga_tryget(new_vgadev, VGA_RSRC_LEGACY_MASK);
565 __vga_put(new_vgadev, VGA_RSRC_LEGACY_MASK);
571 /* change decodes counter */
572 if (old_decodes != new_decodes) {
573 if (new_decodes & (VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM))
578 pr_debug("vgaarb: decoding count now is: %d\n", vga_decode_count);
581 void __vga_set_legacy_decoding(struct pci_dev *pdev, unsigned int decodes, bool userspace)
583 struct vga_device *vgadev;
586 decodes &= VGA_RSRC_LEGACY_MASK;
588 spin_lock_irqsave(&vga_lock, flags);
589 vgadev = vgadev_find(pdev);
593 /* don't let userspace futz with kernel driver decodes */
594 if (userspace && vgadev->set_vga_decode)
597 /* update the device decodes + counter */
598 vga_update_device_decodes(vgadev, decodes);
600 /* XXX if somebody is going from "doesn't decode" to "decodes" state
601 * here, additional care must be taken as we may have pending owner
602 * ship of non-legacy region ...
605 spin_unlock_irqrestore(&vga_lock, flags);
608 void vga_set_legacy_decoding(struct pci_dev *pdev, unsigned int decodes)
610 __vga_set_legacy_decoding(pdev, decodes, false);
612 EXPORT_SYMBOL(vga_set_legacy_decoding);
614 /* call with NULL to unregister */
615 int vga_client_register(struct pci_dev *pdev, void *cookie,
616 void (*irq_set_state)(void *cookie, bool state),
617 unsigned int (*set_vga_decode)(void *cookie, bool decode))
620 struct vga_device *vgadev;
623 spin_lock_irqsave(&vga_lock, flags);
624 vgadev = vgadev_find(pdev);
628 vgadev->irq_set_state = irq_set_state;
629 vgadev->set_vga_decode = set_vga_decode;
630 vgadev->cookie = cookie;
634 spin_unlock_irqrestore(&vga_lock, flags);
638 EXPORT_SYMBOL(vga_client_register);
641 * Char driver implementation
645 * open : open user instance of the arbitrer. by default, it's
646 * attached to the default VGA device of the system.
648 * close : close user instance, release locks
650 * read : return a string indicating the status of the target.
651 * an IO state string is of the form {io,mem,io+mem,none},
652 * mc and ic are respectively mem and io lock counts (for
653 * debugging/diagnostic only). "decodes" indicate what the
654 * card currently decodes, "owns" indicates what is currently
655 * enabled on it, and "locks" indicates what is locked by this
656 * card. If the card is unplugged, we get "invalid" then for
657 * card_ID and an -ENODEV error is returned for any command
658 * until a new card is targeted
660 * "<card_ID>,decodes=<io_state>,owns=<io_state>,locks=<io_state> (ic,mc)"
662 * write : write a command to the arbiter. List of commands is:
664 * target <card_ID> : switch target to card <card_ID> (see below)
665 * lock <io_state> : acquires locks on target ("none" is invalid io_state)
666 * trylock <io_state> : non-blocking acquire locks on target
667 * unlock <io_state> : release locks on target
668 * unlock all : release all locks on target held by this user
669 * decodes <io_state> : set the legacy decoding attributes for the card
671 * poll : event if something change on any card (not just the target)
673 * card_ID is of the form "PCI:domain:bus:dev.fn". It can be set to "default"
674 * to go back to the system default card (TODO: not implemented yet).
675 * Currently, only PCI is supported as a prefix, but the userland API may
676 * support other bus types in the future, even if the current kernel
677 * implementation doesn't.
681 * The driver keeps track of which user has what locks on which card. It
682 * supports stacking, like the kernel one. This complexifies the implementation
683 * a bit, but makes the arbiter more tolerant to userspace problems and able
684 * to properly cleanup in all cases when a process dies.
685 * Currently, a max of 16 cards simultaneously can have locks issued from
686 * userspace for a given user (file descriptor instance) of the arbiter.
688 * If the device is hot-unplugged, there is a hook inside the module to notify
689 * they being added/removed in the system and automatically added/removed in
693 #define MAX_USER_CARDS CONFIG_VGA_ARB_MAX_GPUS
694 #define PCI_INVALID_CARD ((struct pci_dev *)-1UL)
697 * Each user has an array of these, tracking which cards have locks
699 struct vga_arb_user_card {
700 struct pci_dev *pdev;
701 unsigned int mem_cnt;
705 struct vga_arb_private {
706 struct list_head list;
707 struct pci_dev *target;
708 struct vga_arb_user_card cards[MAX_USER_CARDS];
712 static LIST_HEAD(vga_user_list);
713 static DEFINE_SPINLOCK(vga_user_lock);
717 * This function gets a string in the format: "PCI:domain:bus:dev.fn" and
718 * returns the respective values. If the string is not in this format,
721 static int vga_pci_str_to_vars(char *buf, int count, unsigned int *domain,
722 unsigned int *bus, unsigned int *devfn)
725 unsigned int slot, func;
728 n = sscanf(buf, "PCI:%x:%x:%x.%x", domain, bus, &slot, &func);
732 *devfn = PCI_DEVFN(slot, func);
737 static ssize_t vga_arb_read(struct file *file, char __user * buf,
738 size_t count, loff_t *ppos)
740 struct vga_arb_private *priv = file->private_data;
741 struct vga_device *vgadev;
742 struct pci_dev *pdev;
748 lbuf = kmalloc(1024, GFP_KERNEL);
752 /* Shields against vga_arb_device_card_gone (pci_dev going
753 * away), and allows access to vga list
755 spin_lock_irqsave(&vga_lock, flags);
757 /* If we are targetting the default, use it */
759 if (pdev == NULL || pdev == PCI_INVALID_CARD) {
760 spin_unlock_irqrestore(&vga_lock, flags);
761 len = sprintf(lbuf, "invalid");
765 /* Find card vgadev structure */
766 vgadev = vgadev_find(pdev);
767 if (vgadev == NULL) {
768 /* Wow, it's not in the list, that shouldn't happen,
769 * let's fix us up and return invalid card
771 if (pdev == priv->target)
772 vga_arb_device_card_gone(pdev);
773 spin_unlock_irqrestore(&vga_lock, flags);
774 len = sprintf(lbuf, "invalid");
778 /* Fill the buffer with infos */
779 len = snprintf(lbuf, 1024,
780 "count:%d,PCI:%s,decodes=%s,owns=%s,locks=%s(%d:%d)\n",
781 vga_decode_count, pci_name(pdev),
782 vga_iostate_to_str(vgadev->decodes),
783 vga_iostate_to_str(vgadev->owns),
784 vga_iostate_to_str(vgadev->locks),
785 vgadev->io_lock_cnt, vgadev->mem_lock_cnt);
787 spin_unlock_irqrestore(&vga_lock, flags);
790 /* Copy that to user */
793 rc = copy_to_user(buf, lbuf, len);
801 * TODO: To avoid parsing inside kernel and to improve the speed we may
802 * consider use ioctl here
804 static ssize_t vga_arb_write(struct file *file, const char __user * buf,
805 size_t count, loff_t *ppos)
807 struct vga_arb_private *priv = file->private_data;
808 struct vga_arb_user_card *uc = NULL;
809 struct pci_dev *pdev;
811 unsigned int io_state;
813 char *kbuf, *curr_pos;
814 size_t remaining = count;
820 kbuf = kmalloc(count + 1, GFP_KERNEL);
824 if (copy_from_user(kbuf, buf, count)) {
829 kbuf[count] = '\0'; /* Just to make sure... */
831 if (strncmp(curr_pos, "lock ", 5) == 0) {
835 pr_debug("client 0x%p called 'lock'\n", priv);
837 if (!vga_str_to_iostate(curr_pos, remaining, &io_state)) {
841 if (io_state == VGA_RSRC_NONE) {
847 if (priv->target == NULL) {
852 vga_get_uninterruptible(pdev, io_state);
854 /* Update the client's locks lists... */
855 for (i = 0; i < MAX_USER_CARDS; i++) {
856 if (priv->cards[i].pdev == pdev) {
857 if (io_state & VGA_RSRC_LEGACY_IO)
858 priv->cards[i].io_cnt++;
859 if (io_state & VGA_RSRC_LEGACY_MEM)
860 priv->cards[i].mem_cnt++;
867 } else if (strncmp(curr_pos, "unlock ", 7) == 0) {
871 pr_debug("client 0x%p called 'unlock'\n", priv);
873 if (strncmp(curr_pos, "all", 3) == 0)
874 io_state = VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM;
876 if (!vga_str_to_iostate
877 (curr_pos, remaining, &io_state)) {
882 if (io_state == VGA_RSRC_NONE) {
890 if (priv->target == NULL) {
894 for (i = 0; i < MAX_USER_CARDS; i++) {
895 if (priv->cards[i].pdev == pdev)
896 uc = &priv->cards[i];
902 if (io_state & VGA_RSRC_LEGACY_IO && uc->io_cnt == 0)
905 if (io_state & VGA_RSRC_LEGACY_MEM && uc->mem_cnt == 0)
908 vga_put(pdev, io_state);
910 if (io_state & VGA_RSRC_LEGACY_IO)
912 if (io_state & VGA_RSRC_LEGACY_MEM)
917 } else if (strncmp(curr_pos, "trylock ", 8) == 0) {
921 pr_debug("client 0x%p called 'trylock'\n", priv);
923 if (!vga_str_to_iostate(curr_pos, remaining, &io_state)) {
928 if (io_state == VGA_RSRC_NONE) {
935 if (priv->target == NULL) {
940 if (vga_tryget(pdev, io_state)) {
941 /* Update the client's locks lists... */
942 for (i = 0; i < MAX_USER_CARDS; i++) {
943 if (priv->cards[i].pdev == pdev) {
944 if (io_state & VGA_RSRC_LEGACY_IO)
945 priv->cards[i].io_cnt++;
946 if (io_state & VGA_RSRC_LEGACY_MEM)
947 priv->cards[i].mem_cnt++;
958 } else if (strncmp(curr_pos, "target ", 7) == 0) {
959 struct pci_bus *pbus;
960 unsigned int domain, bus, devfn;
961 struct vga_device *vgadev;
965 pr_debug("client 0x%p called 'target'\n", priv);
966 /* if target is default */
967 if (!strncmp(curr_pos, "default", 7))
968 pdev = pci_dev_get(vga_default_device());
970 if (!vga_pci_str_to_vars(curr_pos, remaining,
971 &domain, &bus, &devfn)) {
975 pr_debug("vgaarb: %s ==> %x:%x:%x.%x\n", curr_pos,
976 domain, bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
978 pbus = pci_find_bus(domain, bus);
979 pr_debug("vgaarb: pbus %p\n", pbus);
981 pr_err("vgaarb: invalid PCI domain and/or bus address %x:%x\n",
986 pdev = pci_get_slot(pbus, devfn);
987 pr_debug("vgaarb: pdev %p\n", pdev);
989 pr_err("vgaarb: invalid PCI address %x:%x\n",
996 vgadev = vgadev_find(pdev);
997 pr_debug("vgaarb: vgadev %p\n", vgadev);
998 if (vgadev == NULL) {
999 pr_err("vgaarb: this pci device is not a vga device\n");
1005 priv->target = pdev;
1006 for (i = 0; i < MAX_USER_CARDS; i++) {
1007 if (priv->cards[i].pdev == pdev)
1009 if (priv->cards[i].pdev == NULL) {
1010 priv->cards[i].pdev = pdev;
1011 priv->cards[i].io_cnt = 0;
1012 priv->cards[i].mem_cnt = 0;
1016 if (i == MAX_USER_CARDS) {
1017 pr_err("vgaarb: maximum user cards (%d) number reached!\n",
1020 /* XXX: which value to return? */
1030 } else if (strncmp(curr_pos, "decodes ", 8) == 0) {
1033 pr_debug("vgaarb: client 0x%p called 'decodes'\n", priv);
1035 if (!vga_str_to_iostate(curr_pos, remaining, &io_state)) {
1039 pdev = priv->target;
1040 if (priv->target == NULL) {
1045 __vga_set_legacy_decoding(pdev, io_state, true);
1049 /* If we got here, the message written is not part of the protocol! */
1058 static unsigned int vga_arb_fpoll(struct file *file, poll_table * wait)
1060 struct vga_arb_private *priv = file->private_data;
1062 pr_debug("%s\n", __func__);
1066 poll_wait(file, &vga_wait_queue, wait);
1070 static int vga_arb_open(struct inode *inode, struct file *file)
1072 struct vga_arb_private *priv;
1073 unsigned long flags;
1075 pr_debug("%s\n", __func__);
1077 priv = kmalloc(sizeof(struct vga_arb_private), GFP_KERNEL);
1080 memset(priv, 0, sizeof(*priv));
1081 spin_lock_init(&priv->lock);
1082 file->private_data = priv;
1084 spin_lock_irqsave(&vga_user_lock, flags);
1085 list_add(&priv->list, &vga_user_list);
1086 spin_unlock_irqrestore(&vga_user_lock, flags);
1088 /* Set the client' lists of locks */
1089 priv->target = vga_default_device(); /* Maybe this is still null! */
1090 priv->cards[0].pdev = priv->target;
1091 priv->cards[0].io_cnt = 0;
1092 priv->cards[0].mem_cnt = 0;
1098 static int vga_arb_release(struct inode *inode, struct file *file)
1100 struct vga_arb_private *priv = file->private_data;
1101 struct vga_arb_user_card *uc;
1102 unsigned long flags;
1105 pr_debug("%s\n", __func__);
1110 spin_lock_irqsave(&vga_user_lock, flags);
1111 list_del(&priv->list);
1112 for (i = 0; i < MAX_USER_CARDS; i++) {
1113 uc = &priv->cards[i];
1114 if (uc->pdev == NULL)
1116 pr_debug("uc->io_cnt == %d, uc->mem_cnt == %d\n",
1117 uc->io_cnt, uc->mem_cnt);
1118 while (uc->io_cnt--)
1119 vga_put(uc->pdev, VGA_RSRC_LEGACY_IO);
1120 while (uc->mem_cnt--)
1121 vga_put(uc->pdev, VGA_RSRC_LEGACY_MEM);
1123 spin_unlock_irqrestore(&vga_user_lock, flags);
1130 static void vga_arb_device_card_gone(struct pci_dev *pdev)
1135 * callback any registered clients to let them know we have a
1136 * change in VGA cards
1138 static void vga_arbiter_notify_clients(void)
1140 struct vga_device *vgadev;
1141 unsigned long flags;
1142 uint32_t new_decodes;
1145 if (!vga_arbiter_used)
1148 spin_lock_irqsave(&vga_lock, flags);
1149 list_for_each_entry(vgadev, &vga_list, list) {
1154 if (vgadev->set_vga_decode) {
1155 new_decodes = vgadev->set_vga_decode(vgadev->cookie, new_state);
1156 vga_update_device_decodes(vgadev, new_decodes);
1159 spin_unlock_irqrestore(&vga_lock, flags);
1162 static int pci_notify(struct notifier_block *nb, unsigned long action,
1165 struct device *dev = data;
1166 struct pci_dev *pdev = to_pci_dev(dev);
1167 bool notify = false;
1169 pr_debug("%s\n", __func__);
1171 /* For now we're only intereted in devices added and removed. I didn't
1172 * test this thing here, so someone needs to double check for the
1173 * cases of hotplugable vga cards. */
1174 if (action == BUS_NOTIFY_ADD_DEVICE)
1175 notify = vga_arbiter_add_pci_device(pdev);
1176 else if (action == BUS_NOTIFY_DEL_DEVICE)
1177 notify = vga_arbiter_del_pci_device(pdev);
1180 vga_arbiter_notify_clients();
1184 static struct notifier_block pci_notifier = {
1185 .notifier_call = pci_notify,
1188 static const struct file_operations vga_arb_device_fops = {
1189 .read = vga_arb_read,
1190 .write = vga_arb_write,
1191 .poll = vga_arb_fpoll,
1192 .open = vga_arb_open,
1193 .release = vga_arb_release,
1196 static struct miscdevice vga_arb_device = {
1197 MISC_DYNAMIC_MINOR, "vga_arbiter", &vga_arb_device_fops
1200 static int __init vga_arb_device_init(void)
1203 struct pci_dev *pdev;
1205 rc = misc_register(&vga_arb_device);
1207 pr_err("vgaarb: error %d registering device\n", rc);
1209 bus_register_notifier(&pci_bus_type, &pci_notifier);
1211 /* We add all pci devices satisfying vga class in the arbiter by
1215 pci_get_subsys(PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
1216 PCI_ANY_ID, pdev)) != NULL)
1217 vga_arbiter_add_pci_device(pdev);
1219 pr_info("vgaarb: loaded\n");
1222 subsys_initcall(vga_arb_device_init);