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>
24 #include <linux/uaccess.h>
26 #include <linux/vgaarb.h>
28 static void vga_arbiter_notify_clients(void);
30 * We keep a list of all vga devices in the system to speed
31 * up the various operations of the arbiter
34 struct list_head list;
36 unsigned int decodes; /* what does it decodes */
37 unsigned int owns; /* what does it owns */
38 unsigned int locks; /* what does it locks */
39 unsigned int io_lock_cnt; /* legacy IO lock count */
40 unsigned int mem_lock_cnt; /* legacy MEM lock count */
41 unsigned int io_norm_cnt; /* normal IO count */
42 unsigned int mem_norm_cnt; /* normal MEM count */
44 /* allow IRQ enable/disable hook */
46 void (*irq_set_state)(void *cookie, bool enable);
47 unsigned int (*set_vga_decode)(void *cookie, bool decode);
50 static LIST_HEAD(vga_list);
51 static int vga_count, vga_decode_count;
52 static bool vga_arbiter_used;
53 static DEFINE_SPINLOCK(vga_lock);
54 static DECLARE_WAIT_QUEUE_HEAD(vga_wait_queue);
57 static const char *vga_iostate_to_str(unsigned int iostate)
59 /* Ignore VGA_RSRC_IO and VGA_RSRC_MEM */
60 iostate &= VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM;
62 case VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM:
64 case VGA_RSRC_LEGACY_IO:
66 case VGA_RSRC_LEGACY_MEM:
72 static int vga_str_to_iostate(char *buf, int str_size, int *io_state)
74 /* we could in theory hand out locks on IO and mem
75 * separately to userspace but it can cause deadlocks */
76 if (strncmp(buf, "none", 4) == 0) {
77 *io_state = VGA_RSRC_NONE;
81 /* XXX We're not chekcing the str_size! */
82 if (strncmp(buf, "io+mem", 6) == 0)
84 else if (strncmp(buf, "io", 2) == 0)
86 else if (strncmp(buf, "mem", 3) == 0)
90 *io_state = VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM;
94 #ifndef __ARCH_HAS_VGA_DEFAULT_DEVICE
95 /* this is only used a cookie - it should not be dereferenced */
96 static struct pci_dev *vga_default;
99 static void vga_arb_device_card_gone(struct pci_dev *pdev);
101 /* Find somebody in our list */
102 static struct vga_device *vgadev_find(struct pci_dev *pdev)
104 struct vga_device *vgadev;
106 list_for_each_entry(vgadev, &vga_list, list)
107 if (pdev == vgadev->pdev)
112 /* Returns the default VGA device (vgacon's babe) */
113 #ifndef __ARCH_HAS_VGA_DEFAULT_DEVICE
114 struct pci_dev *vga_default_device(void)
120 static inline void vga_irq_set_state(struct vga_device *vgadev, bool state)
122 if (vgadev->irq_set_state)
123 vgadev->irq_set_state(vgadev->cookie, state);
127 /* If we don't ever use VGA arb we should avoid
128 turning off anything anywhere due to old X servers getting
129 confused about the boot device not being VGA */
130 static void vga_check_first_use(void)
132 /* we should inform all GPUs in the system that
133 * VGA arb has occured and to try and disable resources
135 if (!vga_arbiter_used) {
136 vga_arbiter_used = true;
137 vga_arbiter_notify_clients();
141 static struct vga_device *__vga_tryget(struct vga_device *vgadev,
144 unsigned int wants, legacy_wants, match;
145 struct vga_device *conflict;
146 unsigned int pci_bits;
147 /* Account for "normal" resources to lock. If we decode the legacy,
148 * counterpart, we need to request it as well
150 if ((rsrc & VGA_RSRC_NORMAL_IO) &&
151 (vgadev->decodes & VGA_RSRC_LEGACY_IO))
152 rsrc |= VGA_RSRC_LEGACY_IO;
153 if ((rsrc & VGA_RSRC_NORMAL_MEM) &&
154 (vgadev->decodes & VGA_RSRC_LEGACY_MEM))
155 rsrc |= VGA_RSRC_LEGACY_MEM;
157 pr_devel("%s: %d\n", __func__, rsrc);
158 pr_devel("%s: owns: %d\n", __func__, vgadev->owns);
160 /* Check what resources we need to acquire */
161 wants = rsrc & ~vgadev->owns;
163 /* We already own everything, just mark locked & bye bye */
167 /* We don't need to request a legacy resource, we just enable
168 * appropriate decoding and go
170 legacy_wants = wants & VGA_RSRC_LEGACY_MASK;
171 if (legacy_wants == 0)
174 /* Ok, we don't, let's find out how we need to kick off */
175 list_for_each_entry(conflict, &vga_list, list) {
176 unsigned int lwants = legacy_wants;
177 unsigned int change_bridge = 0;
179 /* Don't conflict with myself */
180 if (vgadev == conflict)
183 /* Check if the architecture allows a conflict between those
184 * 2 devices or if they are on separate domains
186 if (!vga_conflicts(vgadev->pdev, conflict->pdev))
189 /* We have a possible conflict. before we go further, we must
190 * check if we sit on the same bus as the conflicting device.
191 * if we don't, then we must tie both IO and MEM resources
192 * together since there is only a single bit controlling
193 * VGA forwarding on P2P bridges
195 if (vgadev->pdev->bus != conflict->pdev->bus) {
197 lwants = VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM;
200 /* Check if the guy has a lock on the resource. If he does,
201 * return the conflicting entry
203 if (conflict->locks & lwants)
206 /* Ok, now check if he owns the resource we want. We don't need
207 * to check "decodes" since it should be impossible to own
208 * own legacy resources you don't decode unless I have a bug
211 WARN_ON(conflict->owns & ~conflict->decodes);
212 match = lwants & conflict->owns;
216 /* looks like he doesn't have a lock, we can steal
219 vga_irq_set_state(conflict, false);
222 if (lwants & (VGA_RSRC_LEGACY_MEM|VGA_RSRC_NORMAL_MEM))
223 pci_bits |= PCI_COMMAND_MEMORY;
224 if (lwants & (VGA_RSRC_LEGACY_IO|VGA_RSRC_NORMAL_IO))
225 pci_bits |= PCI_COMMAND_IO;
227 pci_set_vga_state(conflict->pdev, false, pci_bits,
229 conflict->owns &= ~lwants;
230 /* If he also owned non-legacy, that is no longer the case */
231 if (lwants & VGA_RSRC_LEGACY_MEM)
232 conflict->owns &= ~VGA_RSRC_NORMAL_MEM;
233 if (lwants & VGA_RSRC_LEGACY_IO)
234 conflict->owns &= ~VGA_RSRC_NORMAL_IO;
238 /* ok dude, we got it, everybody conflicting has been disabled, let's
239 * enable us. Make sure we don't mark a bit in "owns" that we don't
240 * also have in "decodes". We can lock resources we don't decode but
244 if (wants & (VGA_RSRC_LEGACY_MEM|VGA_RSRC_NORMAL_MEM))
245 pci_bits |= PCI_COMMAND_MEMORY;
246 if (wants & (VGA_RSRC_LEGACY_IO|VGA_RSRC_NORMAL_IO))
247 pci_bits |= PCI_COMMAND_IO;
248 pci_set_vga_state(vgadev->pdev, true, pci_bits, !!(wants & VGA_RSRC_LEGACY_MASK));
250 vga_irq_set_state(vgadev, true);
251 vgadev->owns |= (wants & vgadev->decodes);
253 vgadev->locks |= (rsrc & VGA_RSRC_LEGACY_MASK);
254 if (rsrc & VGA_RSRC_LEGACY_IO)
255 vgadev->io_lock_cnt++;
256 if (rsrc & VGA_RSRC_LEGACY_MEM)
257 vgadev->mem_lock_cnt++;
258 if (rsrc & VGA_RSRC_NORMAL_IO)
259 vgadev->io_norm_cnt++;
260 if (rsrc & VGA_RSRC_NORMAL_MEM)
261 vgadev->mem_norm_cnt++;
266 static void __vga_put(struct vga_device *vgadev, unsigned int rsrc)
268 unsigned int old_locks = vgadev->locks;
270 pr_devel("%s\n", __func__);
272 /* Update our counters, and account for equivalent legacy resources
275 if ((rsrc & VGA_RSRC_NORMAL_IO) && vgadev->io_norm_cnt > 0) {
276 vgadev->io_norm_cnt--;
277 if (vgadev->decodes & VGA_RSRC_LEGACY_IO)
278 rsrc |= VGA_RSRC_LEGACY_IO;
280 if ((rsrc & VGA_RSRC_NORMAL_MEM) && vgadev->mem_norm_cnt > 0) {
281 vgadev->mem_norm_cnt--;
282 if (vgadev->decodes & VGA_RSRC_LEGACY_MEM)
283 rsrc |= VGA_RSRC_LEGACY_MEM;
285 if ((rsrc & VGA_RSRC_LEGACY_IO) && vgadev->io_lock_cnt > 0)
286 vgadev->io_lock_cnt--;
287 if ((rsrc & VGA_RSRC_LEGACY_MEM) && vgadev->mem_lock_cnt > 0)
288 vgadev->mem_lock_cnt--;
290 /* Just clear lock bits, we do lazy operations so we don't really
291 * have to bother about anything else at this point
293 if (vgadev->io_lock_cnt == 0)
294 vgadev->locks &= ~VGA_RSRC_LEGACY_IO;
295 if (vgadev->mem_lock_cnt == 0)
296 vgadev->locks &= ~VGA_RSRC_LEGACY_MEM;
298 /* Kick the wait queue in case somebody was waiting if we actually
301 if (old_locks != vgadev->locks)
302 wake_up_all(&vga_wait_queue);
305 int vga_get(struct pci_dev *pdev, unsigned int rsrc, int interruptible)
307 struct vga_device *vgadev, *conflict;
312 vga_check_first_use();
313 /* The one who calls us should check for this, but lets be sure... */
315 pdev = vga_default_device();
320 spin_lock_irqsave(&vga_lock, flags);
321 vgadev = vgadev_find(pdev);
322 if (vgadev == NULL) {
323 spin_unlock_irqrestore(&vga_lock, flags);
327 conflict = __vga_tryget(vgadev, rsrc);
328 spin_unlock_irqrestore(&vga_lock, flags);
329 if (conflict == NULL)
333 /* We have a conflict, we wait until somebody kicks the
334 * work queue. Currently we have one work queue that we
335 * kick each time some resources are released, but it would
336 * be fairly easy to have a per device one so that we only
337 * need to attach to the conflicting device
339 init_waitqueue_entry(&wait, current);
340 add_wait_queue(&vga_wait_queue, &wait);
341 set_current_state(interruptible ?
343 TASK_UNINTERRUPTIBLE);
344 if (signal_pending(current)) {
349 remove_wait_queue(&vga_wait_queue, &wait);
350 set_current_state(TASK_RUNNING);
354 EXPORT_SYMBOL(vga_get);
356 int vga_tryget(struct pci_dev *pdev, unsigned int rsrc)
358 struct vga_device *vgadev;
362 vga_check_first_use();
364 /* The one who calls us should check for this, but lets be sure... */
366 pdev = vga_default_device();
369 spin_lock_irqsave(&vga_lock, flags);
370 vgadev = vgadev_find(pdev);
371 if (vgadev == NULL) {
375 if (__vga_tryget(vgadev, rsrc))
378 spin_unlock_irqrestore(&vga_lock, flags);
381 EXPORT_SYMBOL(vga_tryget);
383 void vga_put(struct pci_dev *pdev, unsigned int rsrc)
385 struct vga_device *vgadev;
388 /* The one who calls us should check for this, but lets be sure... */
390 pdev = vga_default_device();
393 spin_lock_irqsave(&vga_lock, flags);
394 vgadev = vgadev_find(pdev);
397 __vga_put(vgadev, rsrc);
399 spin_unlock_irqrestore(&vga_lock, flags);
401 EXPORT_SYMBOL(vga_put);
404 * Currently, we assume that the "initial" setup of the system is
405 * not sane, that is we come up with conflicting devices and let
406 * the arbiter's client decides if devices decodes or not legacy
409 static bool vga_arbiter_add_pci_device(struct pci_dev *pdev)
411 struct vga_device *vgadev;
414 struct pci_dev *bridge;
417 /* Only deal with VGA class devices */
418 if ((pdev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
421 /* Allocate structure */
422 vgadev = kmalloc(sizeof(struct vga_device), GFP_KERNEL);
423 if (vgadev == NULL) {
424 pr_err("vgaarb: failed to allocate pci device\n");
425 /* What to do on allocation failure ? For now, let's
426 * just do nothing, I'm not sure there is anything saner
432 memset(vgadev, 0, sizeof(*vgadev));
434 /* Take lock & check for duplicates */
435 spin_lock_irqsave(&vga_lock, flags);
436 if (vgadev_find(pdev) != NULL) {
442 /* By default, assume we decode everything */
443 vgadev->decodes = VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
444 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
446 /* by default mark it as decoding */
448 /* Mark that we "own" resources based on our enables, we will
449 * clear that below if the bridge isn't forwarding
451 pci_read_config_word(pdev, PCI_COMMAND, &cmd);
452 if (cmd & PCI_COMMAND_IO)
453 vgadev->owns |= VGA_RSRC_LEGACY_IO;
454 if (cmd & PCI_COMMAND_MEMORY)
455 vgadev->owns |= VGA_RSRC_LEGACY_MEM;
457 /* Check if VGA cycles can get down to us */
463 pci_read_config_word(bridge, PCI_BRIDGE_CONTROL,
465 if (!(l & PCI_BRIDGE_CTL_VGA)) {
473 /* Deal with VGA default device. Use first enabled one
474 * by default if arch doesn't have it's own hook
476 #ifndef __ARCH_HAS_VGA_DEFAULT_DEVICE
477 if (vga_default == NULL &&
478 ((vgadev->owns & VGA_RSRC_LEGACY_MASK) == VGA_RSRC_LEGACY_MASK))
479 vga_default = pci_dev_get(pdev);
482 /* Add to the list */
483 list_add(&vgadev->list, &vga_list);
485 pr_info("vgaarb: device added: PCI:%s,decodes=%s,owns=%s,locks=%s\n",
487 vga_iostate_to_str(vgadev->decodes),
488 vga_iostate_to_str(vgadev->owns),
489 vga_iostate_to_str(vgadev->locks));
491 spin_unlock_irqrestore(&vga_lock, flags);
494 spin_unlock_irqrestore(&vga_lock, flags);
499 static bool vga_arbiter_del_pci_device(struct pci_dev *pdev)
501 struct vga_device *vgadev;
505 spin_lock_irqsave(&vga_lock, flags);
506 vgadev = vgadev_find(pdev);
507 if (vgadev == NULL) {
512 if (vga_default == pdev) {
513 pci_dev_put(vga_default);
517 if (vgadev->decodes & (VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM))
520 /* Remove entry from list */
521 list_del(&vgadev->list);
523 /* Notify userland driver that the device is gone so it discards
524 * it's copies of the pci_dev pointer
526 vga_arb_device_card_gone(pdev);
528 /* Wake up all possible waiters */
529 wake_up_all(&vga_wait_queue);
531 spin_unlock_irqrestore(&vga_lock, flags);
536 /* this is called with the lock */
537 static inline void vga_update_device_decodes(struct vga_device *vgadev,
541 struct vga_device *new_vgadev, *conflict;
543 old_decodes = vgadev->decodes;
544 vgadev->decodes = new_decodes;
546 pr_info("vgaarb: device changed decodes: PCI:%s,olddecodes=%s,decodes=%s:owns=%s\n",
547 pci_name(vgadev->pdev),
548 vga_iostate_to_str(old_decodes),
549 vga_iostate_to_str(vgadev->decodes),
550 vga_iostate_to_str(vgadev->owns));
553 /* if we own the decodes we should move them along to
555 if ((vgadev->owns & old_decodes) && (vga_count > 1)) {
556 /* set us to own nothing */
557 vgadev->owns &= ~old_decodes;
558 list_for_each_entry(new_vgadev, &vga_list, list) {
559 if ((new_vgadev != vgadev) &&
560 (new_vgadev->decodes & VGA_RSRC_LEGACY_MASK)) {
561 pr_info("vgaarb: transferring owner from PCI:%s to PCI:%s\n", pci_name(vgadev->pdev), pci_name(new_vgadev->pdev));
562 conflict = __vga_tryget(new_vgadev, VGA_RSRC_LEGACY_MASK);
564 __vga_put(new_vgadev, VGA_RSRC_LEGACY_MASK);
570 /* change decodes counter */
571 if (old_decodes != new_decodes) {
572 if (new_decodes & (VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM))
579 void __vga_set_legacy_decoding(struct pci_dev *pdev, unsigned int decodes, bool userspace)
581 struct vga_device *vgadev;
584 decodes &= VGA_RSRC_LEGACY_MASK;
586 spin_lock_irqsave(&vga_lock, flags);
587 vgadev = vgadev_find(pdev);
591 /* don't let userspace futz with kernel driver decodes */
592 if (userspace && vgadev->set_vga_decode)
595 /* update the device decodes + counter */
596 vga_update_device_decodes(vgadev, decodes);
598 /* XXX if somebody is going from "doesn't decode" to "decodes" state
599 * here, additional care must be taken as we may have pending owner
600 * ship of non-legacy region ...
603 spin_unlock_irqrestore(&vga_lock, flags);
606 void vga_set_legacy_decoding(struct pci_dev *pdev, unsigned int decodes)
608 __vga_set_legacy_decoding(pdev, decodes, false);
610 EXPORT_SYMBOL(vga_set_legacy_decoding);
612 /* call with NULL to unregister */
613 int vga_client_register(struct pci_dev *pdev, void *cookie,
614 void (*irq_set_state)(void *cookie, bool state),
615 unsigned int (*set_vga_decode)(void *cookie, bool decode))
618 struct vga_device *vgadev;
621 spin_lock_irqsave(&vga_lock, flags);
622 vgadev = vgadev_find(pdev);
626 vgadev->irq_set_state = irq_set_state;
627 vgadev->set_vga_decode = set_vga_decode;
628 vgadev->cookie = cookie;
632 spin_unlock_irqrestore(&vga_lock, flags);
636 EXPORT_SYMBOL(vga_client_register);
639 * Char driver implementation
643 * open : open user instance of the arbitrer. by default, it's
644 * attached to the default VGA device of the system.
646 * close : close user instance, release locks
648 * read : return a string indicating the status of the target.
649 * an IO state string is of the form {io,mem,io+mem,none},
650 * mc and ic are respectively mem and io lock counts (for
651 * debugging/diagnostic only). "decodes" indicate what the
652 * card currently decodes, "owns" indicates what is currently
653 * enabled on it, and "locks" indicates what is locked by this
654 * card. If the card is unplugged, we get "invalid" then for
655 * card_ID and an -ENODEV error is returned for any command
656 * until a new card is targeted
658 * "<card_ID>,decodes=<io_state>,owns=<io_state>,locks=<io_state> (ic,mc)"
660 * write : write a command to the arbiter. List of commands is:
662 * target <card_ID> : switch target to card <card_ID> (see below)
663 * lock <io_state> : acquires locks on target ("none" is invalid io_state)
664 * trylock <io_state> : non-blocking acquire locks on target
665 * unlock <io_state> : release locks on target
666 * unlock all : release all locks on target held by this user
667 * decodes <io_state> : set the legacy decoding attributes for the card
669 * poll : event if something change on any card (not just the target)
671 * card_ID is of the form "PCI:domain:bus:dev.fn". It can be set to "default"
672 * to go back to the system default card (TODO: not implemented yet).
673 * Currently, only PCI is supported as a prefix, but the userland API may
674 * support other bus types in the future, even if the current kernel
675 * implementation doesn't.
679 * The driver keeps track of which user has what locks on which card. It
680 * supports stacking, like the kernel one. This complexifies the implementation
681 * a bit, but makes the arbiter more tolerant to userspace problems and able
682 * to properly cleanup in all cases when a process dies.
683 * Currently, a max of 16 cards simultaneously can have locks issued from
684 * userspace for a given user (file descriptor instance) of the arbiter.
686 * If the device is hot-unplugged, there is a hook inside the module to notify
687 * they being added/removed in the system and automatically added/removed in
691 #define MAX_USER_CARDS 16
692 #define PCI_INVALID_CARD ((struct pci_dev *)-1UL)
695 * Each user has an array of these, tracking which cards have locks
697 struct vga_arb_user_card {
698 struct pci_dev *pdev;
699 unsigned int mem_cnt;
703 struct vga_arb_private {
704 struct list_head list;
705 struct pci_dev *target;
706 struct vga_arb_user_card cards[MAX_USER_CARDS];
710 static LIST_HEAD(vga_user_list);
711 static DEFINE_SPINLOCK(vga_user_lock);
715 * This function gets a string in the format: "PCI:domain:bus:dev.fn" and
716 * returns the respective values. If the string is not in this format,
719 static int vga_pci_str_to_vars(char *buf, int count, unsigned int *domain,
720 unsigned int *bus, unsigned int *devfn)
723 unsigned int slot, func;
726 n = sscanf(buf, "PCI:%x:%x:%x.%x", domain, bus, &slot, &func);
730 *devfn = PCI_DEVFN(slot, func);
735 static ssize_t vga_arb_read(struct file *file, char __user * buf,
736 size_t count, loff_t *ppos)
738 struct vga_arb_private *priv = file->private_data;
739 struct vga_device *vgadev;
740 struct pci_dev *pdev;
746 lbuf = kmalloc(1024, GFP_KERNEL);
750 /* Shields against vga_arb_device_card_gone (pci_dev going
751 * away), and allows access to vga list
753 spin_lock_irqsave(&vga_lock, flags);
755 /* If we are targetting the default, use it */
757 if (pdev == NULL || pdev == PCI_INVALID_CARD) {
758 spin_unlock_irqrestore(&vga_lock, flags);
759 len = sprintf(lbuf, "invalid");
763 /* Find card vgadev structure */
764 vgadev = vgadev_find(pdev);
765 if (vgadev == NULL) {
766 /* Wow, it's not in the list, that shouldn't happen,
767 * let's fix us up and return invalid card
769 if (pdev == priv->target)
770 vga_arb_device_card_gone(pdev);
771 spin_unlock_irqrestore(&vga_lock, flags);
772 len = sprintf(lbuf, "invalid");
776 /* Fill the buffer with infos */
777 len = snprintf(lbuf, 1024,
778 "count:%d,PCI:%s,decodes=%s,owns=%s,locks=%s(%d:%d)\n",
779 vga_decode_count, pci_name(pdev),
780 vga_iostate_to_str(vgadev->decodes),
781 vga_iostate_to_str(vgadev->owns),
782 vga_iostate_to_str(vgadev->locks),
783 vgadev->io_lock_cnt, vgadev->mem_lock_cnt);
785 spin_unlock_irqrestore(&vga_lock, flags);
788 /* Copy that to user */
791 rc = copy_to_user(buf, lbuf, len);
799 * TODO: To avoid parsing inside kernel and to improve the speed we may
800 * consider use ioctl here
802 static ssize_t vga_arb_write(struct file *file, const char __user * buf,
803 size_t count, loff_t *ppos)
805 struct vga_arb_private *priv = file->private_data;
806 struct vga_arb_user_card *uc = NULL;
807 struct pci_dev *pdev;
809 unsigned int io_state;
811 char *kbuf, *curr_pos;
812 size_t remaining = count;
818 kbuf = kmalloc(count + 1, GFP_KERNEL);
822 if (copy_from_user(kbuf, buf, count)) {
827 kbuf[count] = '\0'; /* Just to make sure... */
829 if (strncmp(curr_pos, "lock ", 5) == 0) {
833 pr_devel("client 0x%p called 'lock'\n", priv);
835 if (!vga_str_to_iostate(curr_pos, remaining, &io_state)) {
839 if (io_state == VGA_RSRC_NONE) {
845 if (priv->target == NULL) {
850 vga_get_uninterruptible(pdev, io_state);
852 /* Update the client's locks lists... */
853 for (i = 0; i < MAX_USER_CARDS; i++) {
854 if (priv->cards[i].pdev == pdev) {
855 if (io_state & VGA_RSRC_LEGACY_IO)
856 priv->cards[i].io_cnt++;
857 if (io_state & VGA_RSRC_LEGACY_MEM)
858 priv->cards[i].mem_cnt++;
865 } else if (strncmp(curr_pos, "unlock ", 7) == 0) {
869 pr_devel("client 0x%p called 'unlock'\n", priv);
871 if (strncmp(curr_pos, "all", 3) == 0)
872 io_state = VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM;
874 if (!vga_str_to_iostate
875 (curr_pos, remaining, &io_state)) {
880 if (io_state == VGA_RSRC_NONE) {
888 if (priv->target == NULL) {
892 for (i = 0; i < MAX_USER_CARDS; i++) {
893 if (priv->cards[i].pdev == pdev)
894 uc = &priv->cards[i];
900 if (io_state & VGA_RSRC_LEGACY_IO && uc->io_cnt == 0)
903 if (io_state & VGA_RSRC_LEGACY_MEM && uc->mem_cnt == 0)
906 vga_put(pdev, io_state);
908 if (io_state & VGA_RSRC_LEGACY_IO)
910 if (io_state & VGA_RSRC_LEGACY_MEM)
915 } else if (strncmp(curr_pos, "trylock ", 8) == 0) {
919 pr_devel("client 0x%p called 'trylock'\n", priv);
921 if (!vga_str_to_iostate(curr_pos, remaining, &io_state)) {
926 if (io_state == VGA_RSRC_NONE) {
933 if (priv->target == NULL) {
938 if (vga_tryget(pdev, io_state)) {
939 /* Update the client's locks lists... */
940 for (i = 0; i < MAX_USER_CARDS; i++) {
941 if (priv->cards[i].pdev == pdev) {
942 if (io_state & VGA_RSRC_LEGACY_IO)
943 priv->cards[i].io_cnt++;
944 if (io_state & VGA_RSRC_LEGACY_MEM)
945 priv->cards[i].mem_cnt++;
956 } else if (strncmp(curr_pos, "target ", 7) == 0) {
957 unsigned int domain, bus, devfn;
958 struct vga_device *vgadev;
962 pr_devel("client 0x%p called 'target'\n", priv);
963 /* if target is default */
964 if (!strncmp(buf, "default", 7))
965 pdev = pci_dev_get(vga_default_device());
967 if (!vga_pci_str_to_vars(curr_pos, remaining,
968 &domain, &bus, &devfn)) {
973 pdev = pci_get_bus_and_slot(bus, devfn);
975 pr_info("vgaarb: invalid PCI address!\n");
981 vgadev = vgadev_find(pdev);
982 if (vgadev == NULL) {
983 pr_info("vgaarb: this pci device is not a vga device\n");
990 for (i = 0; i < MAX_USER_CARDS; i++) {
991 if (priv->cards[i].pdev == pdev)
993 if (priv->cards[i].pdev == NULL) {
994 priv->cards[i].pdev = pdev;
995 priv->cards[i].io_cnt = 0;
996 priv->cards[i].mem_cnt = 0;
1000 if (i == MAX_USER_CARDS) {
1001 pr_err("vgaarb: maximum user cards number reached!\n");
1003 /* XXX: which value to return? */
1013 } else if (strncmp(curr_pos, "decodes ", 8) == 0) {
1016 pr_devel("vgaarb: client 0x%p called 'decodes'\n", priv);
1018 if (!vga_str_to_iostate(curr_pos, remaining, &io_state)) {
1022 pdev = priv->target;
1023 if (priv->target == NULL) {
1028 __vga_set_legacy_decoding(pdev, io_state, true);
1032 /* If we got here, the message written is not part of the protocol! */
1041 static unsigned int vga_arb_fpoll(struct file *file, poll_table * wait)
1043 struct vga_arb_private *priv = file->private_data;
1045 pr_devel("%s\n", __func__);
1049 poll_wait(file, &vga_wait_queue, wait);
1053 static int vga_arb_open(struct inode *inode, struct file *file)
1055 struct vga_arb_private *priv;
1056 unsigned long flags;
1058 pr_devel("%s\n", __func__);
1060 priv = kmalloc(sizeof(struct vga_arb_private), GFP_KERNEL);
1063 memset(priv, 0, sizeof(*priv));
1064 spin_lock_init(&priv->lock);
1065 file->private_data = priv;
1067 spin_lock_irqsave(&vga_user_lock, flags);
1068 list_add(&priv->list, &vga_user_list);
1069 spin_unlock_irqrestore(&vga_user_lock, flags);
1071 /* Set the client' lists of locks */
1072 priv->target = vga_default_device(); /* Maybe this is still null! */
1073 priv->cards[0].pdev = priv->target;
1074 priv->cards[0].io_cnt = 0;
1075 priv->cards[0].mem_cnt = 0;
1081 static int vga_arb_release(struct inode *inode, struct file *file)
1083 struct vga_arb_private *priv = file->private_data;
1084 struct vga_arb_user_card *uc;
1085 unsigned long flags;
1088 pr_devel("%s\n", __func__);
1093 spin_lock_irqsave(&vga_user_lock, flags);
1094 list_del(&priv->list);
1095 for (i = 0; i < MAX_USER_CARDS; i++) {
1096 uc = &priv->cards[i];
1097 if (uc->pdev == NULL)
1099 pr_devel("uc->io_cnt == %d, uc->mem_cnt == %d\n",
1100 uc->io_cnt, uc->mem_cnt);
1101 while (uc->io_cnt--)
1102 vga_put(uc->pdev, VGA_RSRC_LEGACY_IO);
1103 while (uc->mem_cnt--)
1104 vga_put(uc->pdev, VGA_RSRC_LEGACY_MEM);
1106 spin_unlock_irqrestore(&vga_user_lock, flags);
1113 static void vga_arb_device_card_gone(struct pci_dev *pdev)
1118 * callback any registered clients to let them know we have a
1119 * change in VGA cards
1121 static void vga_arbiter_notify_clients(void)
1123 struct vga_device *vgadev;
1124 unsigned long flags;
1125 uint32_t new_decodes;
1128 if (!vga_arbiter_used)
1131 spin_lock_irqsave(&vga_lock, flags);
1132 list_for_each_entry(vgadev, &vga_list, list) {
1137 if (vgadev->set_vga_decode) {
1138 new_decodes = vgadev->set_vga_decode(vgadev->cookie, new_state);
1139 vga_update_device_decodes(vgadev, new_decodes);
1142 spin_unlock_irqrestore(&vga_lock, flags);
1145 static int pci_notify(struct notifier_block *nb, unsigned long action,
1148 struct device *dev = data;
1149 struct pci_dev *pdev = to_pci_dev(dev);
1150 bool notify = false;
1152 pr_devel("%s\n", __func__);
1154 /* For now we're only intereted in devices added and removed. I didn't
1155 * test this thing here, so someone needs to double check for the
1156 * cases of hotplugable vga cards. */
1157 if (action == BUS_NOTIFY_ADD_DEVICE)
1158 notify = vga_arbiter_add_pci_device(pdev);
1159 else if (action == BUS_NOTIFY_DEL_DEVICE)
1160 notify = vga_arbiter_del_pci_device(pdev);
1163 vga_arbiter_notify_clients();
1167 static struct notifier_block pci_notifier = {
1168 .notifier_call = pci_notify,
1171 static const struct file_operations vga_arb_device_fops = {
1172 .read = vga_arb_read,
1173 .write = vga_arb_write,
1174 .poll = vga_arb_fpoll,
1175 .open = vga_arb_open,
1176 .release = vga_arb_release,
1179 static struct miscdevice vga_arb_device = {
1180 MISC_DYNAMIC_MINOR, "vga_arbiter", &vga_arb_device_fops
1183 static int __init vga_arb_device_init(void)
1186 struct pci_dev *pdev;
1188 rc = misc_register(&vga_arb_device);
1190 pr_err("vgaarb: error %d registering device\n", rc);
1192 bus_register_notifier(&pci_bus_type, &pci_notifier);
1194 /* We add all pci devices satisfying vga class in the arbiter by
1198 pci_get_subsys(PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
1199 PCI_ANY_ID, pdev)) != NULL)
1200 vga_arbiter_add_pci_device(pdev);
1202 pr_info("vgaarb: loaded\n");
1205 subsys_initcall(vga_arb_device_init);