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_devel("%s: %d\n", __func__
, rsrc
);
159 pr_devel("%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_devel("%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
))
580 void __vga_set_legacy_decoding(struct pci_dev
*pdev
, unsigned int decodes
, bool userspace
)
582 struct vga_device
*vgadev
;
585 decodes
&= VGA_RSRC_LEGACY_MASK
;
587 spin_lock_irqsave(&vga_lock
, flags
);
588 vgadev
= vgadev_find(pdev
);
592 /* don't let userspace futz with kernel driver decodes */
593 if (userspace
&& vgadev
->set_vga_decode
)
596 /* update the device decodes + counter */
597 vga_update_device_decodes(vgadev
, decodes
);
599 /* XXX if somebody is going from "doesn't decode" to "decodes" state
600 * here, additional care must be taken as we may have pending owner
601 * ship of non-legacy region ...
604 spin_unlock_irqrestore(&vga_lock
, flags
);
607 void vga_set_legacy_decoding(struct pci_dev
*pdev
, unsigned int decodes
)
609 __vga_set_legacy_decoding(pdev
, decodes
, false);
611 EXPORT_SYMBOL(vga_set_legacy_decoding
);
613 /* call with NULL to unregister */
614 int vga_client_register(struct pci_dev
*pdev
, void *cookie
,
615 void (*irq_set_state
)(void *cookie
, bool state
),
616 unsigned int (*set_vga_decode
)(void *cookie
, bool decode
))
619 struct vga_device
*vgadev
;
622 spin_lock_irqsave(&vga_lock
, flags
);
623 vgadev
= vgadev_find(pdev
);
627 vgadev
->irq_set_state
= irq_set_state
;
628 vgadev
->set_vga_decode
= set_vga_decode
;
629 vgadev
->cookie
= cookie
;
633 spin_unlock_irqrestore(&vga_lock
, flags
);
637 EXPORT_SYMBOL(vga_client_register
);
640 * Char driver implementation
644 * open : open user instance of the arbitrer. by default, it's
645 * attached to the default VGA device of the system.
647 * close : close user instance, release locks
649 * read : return a string indicating the status of the target.
650 * an IO state string is of the form {io,mem,io+mem,none},
651 * mc and ic are respectively mem and io lock counts (for
652 * debugging/diagnostic only). "decodes" indicate what the
653 * card currently decodes, "owns" indicates what is currently
654 * enabled on it, and "locks" indicates what is locked by this
655 * card. If the card is unplugged, we get "invalid" then for
656 * card_ID and an -ENODEV error is returned for any command
657 * until a new card is targeted
659 * "<card_ID>,decodes=<io_state>,owns=<io_state>,locks=<io_state> (ic,mc)"
661 * write : write a command to the arbiter. List of commands is:
663 * target <card_ID> : switch target to card <card_ID> (see below)
664 * lock <io_state> : acquires locks on target ("none" is invalid io_state)
665 * trylock <io_state> : non-blocking acquire locks on target
666 * unlock <io_state> : release locks on target
667 * unlock all : release all locks on target held by this user
668 * decodes <io_state> : set the legacy decoding attributes for the card
670 * poll : event if something change on any card (not just the target)
672 * card_ID is of the form "PCI:domain:bus:dev.fn". It can be set to "default"
673 * to go back to the system default card (TODO: not implemented yet).
674 * Currently, only PCI is supported as a prefix, but the userland API may
675 * support other bus types in the future, even if the current kernel
676 * implementation doesn't.
680 * The driver keeps track of which user has what locks on which card. It
681 * supports stacking, like the kernel one. This complexifies the implementation
682 * a bit, but makes the arbiter more tolerant to userspace problems and able
683 * to properly cleanup in all cases when a process dies.
684 * Currently, a max of 16 cards simultaneously can have locks issued from
685 * userspace for a given user (file descriptor instance) of the arbiter.
687 * If the device is hot-unplugged, there is a hook inside the module to notify
688 * they being added/removed in the system and automatically added/removed in
692 #define MAX_USER_CARDS CONFIG_VGA_ARB_MAX_GPUS
693 #define PCI_INVALID_CARD ((struct pci_dev *)-1UL)
696 * Each user has an array of these, tracking which cards have locks
698 struct vga_arb_user_card
{
699 struct pci_dev
*pdev
;
700 unsigned int mem_cnt
;
704 struct vga_arb_private
{
705 struct list_head list
;
706 struct pci_dev
*target
;
707 struct vga_arb_user_card cards
[MAX_USER_CARDS
];
711 static LIST_HEAD(vga_user_list
);
712 static DEFINE_SPINLOCK(vga_user_lock
);
716 * This function gets a string in the format: "PCI:domain:bus:dev.fn" and
717 * returns the respective values. If the string is not in this format,
720 static int vga_pci_str_to_vars(char *buf
, int count
, unsigned int *domain
,
721 unsigned int *bus
, unsigned int *devfn
)
724 unsigned int slot
, func
;
727 n
= sscanf(buf
, "PCI:%x:%x:%x.%x", domain
, bus
, &slot
, &func
);
731 *devfn
= PCI_DEVFN(slot
, func
);
736 static ssize_t
vga_arb_read(struct file
*file
, char __user
* buf
,
737 size_t count
, loff_t
*ppos
)
739 struct vga_arb_private
*priv
= file
->private_data
;
740 struct vga_device
*vgadev
;
741 struct pci_dev
*pdev
;
747 lbuf
= kmalloc(1024, GFP_KERNEL
);
751 /* Shields against vga_arb_device_card_gone (pci_dev going
752 * away), and allows access to vga list
754 spin_lock_irqsave(&vga_lock
, flags
);
756 /* If we are targetting the default, use it */
758 if (pdev
== NULL
|| pdev
== PCI_INVALID_CARD
) {
759 spin_unlock_irqrestore(&vga_lock
, flags
);
760 len
= sprintf(lbuf
, "invalid");
764 /* Find card vgadev structure */
765 vgadev
= vgadev_find(pdev
);
766 if (vgadev
== NULL
) {
767 /* Wow, it's not in the list, that shouldn't happen,
768 * let's fix us up and return invalid card
770 if (pdev
== priv
->target
)
771 vga_arb_device_card_gone(pdev
);
772 spin_unlock_irqrestore(&vga_lock
, flags
);
773 len
= sprintf(lbuf
, "invalid");
777 /* Fill the buffer with infos */
778 len
= snprintf(lbuf
, 1024,
779 "count:%d,PCI:%s,decodes=%s,owns=%s,locks=%s(%d:%d)\n",
780 vga_decode_count
, pci_name(pdev
),
781 vga_iostate_to_str(vgadev
->decodes
),
782 vga_iostate_to_str(vgadev
->owns
),
783 vga_iostate_to_str(vgadev
->locks
),
784 vgadev
->io_lock_cnt
, vgadev
->mem_lock_cnt
);
786 spin_unlock_irqrestore(&vga_lock
, flags
);
789 /* Copy that to user */
792 rc
= copy_to_user(buf
, lbuf
, len
);
800 * TODO: To avoid parsing inside kernel and to improve the speed we may
801 * consider use ioctl here
803 static ssize_t
vga_arb_write(struct file
*file
, const char __user
* buf
,
804 size_t count
, loff_t
*ppos
)
806 struct vga_arb_private
*priv
= file
->private_data
;
807 struct vga_arb_user_card
*uc
= NULL
;
808 struct pci_dev
*pdev
;
810 unsigned int io_state
;
812 char *kbuf
, *curr_pos
;
813 size_t remaining
= count
;
819 kbuf
= kmalloc(count
+ 1, GFP_KERNEL
);
823 if (copy_from_user(kbuf
, buf
, count
)) {
828 kbuf
[count
] = '\0'; /* Just to make sure... */
830 if (strncmp(curr_pos
, "lock ", 5) == 0) {
834 pr_devel("client 0x%p called 'lock'\n", priv
);
836 if (!vga_str_to_iostate(curr_pos
, remaining
, &io_state
)) {
840 if (io_state
== VGA_RSRC_NONE
) {
846 if (priv
->target
== NULL
) {
851 vga_get_uninterruptible(pdev
, io_state
);
853 /* Update the client's locks lists... */
854 for (i
= 0; i
< MAX_USER_CARDS
; i
++) {
855 if (priv
->cards
[i
].pdev
== pdev
) {
856 if (io_state
& VGA_RSRC_LEGACY_IO
)
857 priv
->cards
[i
].io_cnt
++;
858 if (io_state
& VGA_RSRC_LEGACY_MEM
)
859 priv
->cards
[i
].mem_cnt
++;
866 } else if (strncmp(curr_pos
, "unlock ", 7) == 0) {
870 pr_devel("client 0x%p called 'unlock'\n", priv
);
872 if (strncmp(curr_pos
, "all", 3) == 0)
873 io_state
= VGA_RSRC_LEGACY_IO
| VGA_RSRC_LEGACY_MEM
;
875 if (!vga_str_to_iostate
876 (curr_pos
, remaining
, &io_state
)) {
881 if (io_state == VGA_RSRC_NONE) {
889 if (priv
->target
== NULL
) {
893 for (i
= 0; i
< MAX_USER_CARDS
; i
++) {
894 if (priv
->cards
[i
].pdev
== pdev
)
895 uc
= &priv
->cards
[i
];
901 if (io_state
& VGA_RSRC_LEGACY_IO
&& uc
->io_cnt
== 0)
904 if (io_state
& VGA_RSRC_LEGACY_MEM
&& uc
->mem_cnt
== 0)
907 vga_put(pdev
, io_state
);
909 if (io_state
& VGA_RSRC_LEGACY_IO
)
911 if (io_state
& VGA_RSRC_LEGACY_MEM
)
916 } else if (strncmp(curr_pos
, "trylock ", 8) == 0) {
920 pr_devel("client 0x%p called 'trylock'\n", priv
);
922 if (!vga_str_to_iostate(curr_pos
, remaining
, &io_state
)) {
927 if (io_state == VGA_RSRC_NONE) {
934 if (priv
->target
== NULL
) {
939 if (vga_tryget(pdev
, io_state
)) {
940 /* Update the client's locks lists... */
941 for (i
= 0; i
< MAX_USER_CARDS
; i
++) {
942 if (priv
->cards
[i
].pdev
== pdev
) {
943 if (io_state
& VGA_RSRC_LEGACY_IO
)
944 priv
->cards
[i
].io_cnt
++;
945 if (io_state
& VGA_RSRC_LEGACY_MEM
)
946 priv
->cards
[i
].mem_cnt
++;
957 } else if (strncmp(curr_pos
, "target ", 7) == 0) {
958 struct pci_bus
*pbus
;
959 unsigned int domain
, bus
, devfn
;
960 struct vga_device
*vgadev
;
964 pr_devel("client 0x%p called 'target'\n", priv
);
965 /* if target is default */
966 if (!strncmp(curr_pos
, "default", 7))
967 pdev
= pci_dev_get(vga_default_device());
969 if (!vga_pci_str_to_vars(curr_pos
, remaining
,
970 &domain
, &bus
, &devfn
)) {
974 pr_devel("vgaarb: %s ==> %x:%x:%x.%x\n", curr_pos
,
975 domain
, bus
, PCI_SLOT(devfn
), PCI_FUNC(devfn
));
977 pbus
= pci_find_bus(domain
, bus
);
978 pr_devel("vgaarb: pbus %p\n", pbus
);
980 pr_err("vgaarb: invalid PCI domain and/or bus address %x:%x\n",
985 pdev
= pci_get_slot(pbus
, devfn
);
986 pr_devel("vgaarb: pdev %p\n", pdev
);
988 pr_err("vgaarb: invalid PCI address %x:%x\n",
995 vgadev
= vgadev_find(pdev
);
996 pr_devel("vgaarb: vgadev %p\n", vgadev
);
997 if (vgadev
== NULL
) {
998 pr_err("vgaarb: this pci device is not a vga device\n");
1004 priv
->target
= pdev
;
1005 for (i
= 0; i
< MAX_USER_CARDS
; i
++) {
1006 if (priv
->cards
[i
].pdev
== pdev
)
1008 if (priv
->cards
[i
].pdev
== NULL
) {
1009 priv
->cards
[i
].pdev
= pdev
;
1010 priv
->cards
[i
].io_cnt
= 0;
1011 priv
->cards
[i
].mem_cnt
= 0;
1015 if (i
== MAX_USER_CARDS
) {
1016 pr_err("vgaarb: maximum user cards (%d) number reached!\n",
1019 /* XXX: which value to return? */
1029 } else if (strncmp(curr_pos
, "decodes ", 8) == 0) {
1032 pr_devel("vgaarb: client 0x%p called 'decodes'\n", priv
);
1034 if (!vga_str_to_iostate(curr_pos
, remaining
, &io_state
)) {
1038 pdev
= priv
->target
;
1039 if (priv
->target
== NULL
) {
1044 __vga_set_legacy_decoding(pdev
, io_state
, true);
1048 /* If we got here, the message written is not part of the protocol! */
1057 static unsigned int vga_arb_fpoll(struct file
*file
, poll_table
* wait
)
1059 struct vga_arb_private
*priv
= file
->private_data
;
1061 pr_devel("%s\n", __func__
);
1065 poll_wait(file
, &vga_wait_queue
, wait
);
1069 static int vga_arb_open(struct inode
*inode
, struct file
*file
)
1071 struct vga_arb_private
*priv
;
1072 unsigned long flags
;
1074 pr_devel("%s\n", __func__
);
1076 priv
= kmalloc(sizeof(struct vga_arb_private
), GFP_KERNEL
);
1079 memset(priv
, 0, sizeof(*priv
));
1080 spin_lock_init(&priv
->lock
);
1081 file
->private_data
= priv
;
1083 spin_lock_irqsave(&vga_user_lock
, flags
);
1084 list_add(&priv
->list
, &vga_user_list
);
1085 spin_unlock_irqrestore(&vga_user_lock
, flags
);
1087 /* Set the client' lists of locks */
1088 priv
->target
= vga_default_device(); /* Maybe this is still null! */
1089 priv
->cards
[0].pdev
= priv
->target
;
1090 priv
->cards
[0].io_cnt
= 0;
1091 priv
->cards
[0].mem_cnt
= 0;
1097 static int vga_arb_release(struct inode
*inode
, struct file
*file
)
1099 struct vga_arb_private
*priv
= file
->private_data
;
1100 struct vga_arb_user_card
*uc
;
1101 unsigned long flags
;
1104 pr_devel("%s\n", __func__
);
1109 spin_lock_irqsave(&vga_user_lock
, flags
);
1110 list_del(&priv
->list
);
1111 for (i
= 0; i
< MAX_USER_CARDS
; i
++) {
1112 uc
= &priv
->cards
[i
];
1113 if (uc
->pdev
== NULL
)
1115 pr_devel("uc->io_cnt == %d, uc->mem_cnt == %d\n",
1116 uc
->io_cnt
, uc
->mem_cnt
);
1117 while (uc
->io_cnt
--)
1118 vga_put(uc
->pdev
, VGA_RSRC_LEGACY_IO
);
1119 while (uc
->mem_cnt
--)
1120 vga_put(uc
->pdev
, VGA_RSRC_LEGACY_MEM
);
1122 spin_unlock_irqrestore(&vga_user_lock
, flags
);
1129 static void vga_arb_device_card_gone(struct pci_dev
*pdev
)
1134 * callback any registered clients to let them know we have a
1135 * change in VGA cards
1137 static void vga_arbiter_notify_clients(void)
1139 struct vga_device
*vgadev
;
1140 unsigned long flags
;
1141 uint32_t new_decodes
;
1144 if (!vga_arbiter_used
)
1147 spin_lock_irqsave(&vga_lock
, flags
);
1148 list_for_each_entry(vgadev
, &vga_list
, list
) {
1153 if (vgadev
->set_vga_decode
) {
1154 new_decodes
= vgadev
->set_vga_decode(vgadev
->cookie
, new_state
);
1155 vga_update_device_decodes(vgadev
, new_decodes
);
1158 spin_unlock_irqrestore(&vga_lock
, flags
);
1161 static int pci_notify(struct notifier_block
*nb
, unsigned long action
,
1164 struct device
*dev
= data
;
1165 struct pci_dev
*pdev
= to_pci_dev(dev
);
1166 bool notify
= false;
1168 pr_devel("%s\n", __func__
);
1170 /* For now we're only intereted in devices added and removed. I didn't
1171 * test this thing here, so someone needs to double check for the
1172 * cases of hotplugable vga cards. */
1173 if (action
== BUS_NOTIFY_ADD_DEVICE
)
1174 notify
= vga_arbiter_add_pci_device(pdev
);
1175 else if (action
== BUS_NOTIFY_DEL_DEVICE
)
1176 notify
= vga_arbiter_del_pci_device(pdev
);
1179 vga_arbiter_notify_clients();
1183 static struct notifier_block pci_notifier
= {
1184 .notifier_call
= pci_notify
,
1187 static const struct file_operations vga_arb_device_fops
= {
1188 .read
= vga_arb_read
,
1189 .write
= vga_arb_write
,
1190 .poll
= vga_arb_fpoll
,
1191 .open
= vga_arb_open
,
1192 .release
= vga_arb_release
,
1195 static struct miscdevice vga_arb_device
= {
1196 MISC_DYNAMIC_MINOR
, "vga_arbiter", &vga_arb_device_fops
1199 static int __init
vga_arb_device_init(void)
1202 struct pci_dev
*pdev
;
1204 rc
= misc_register(&vga_arb_device
);
1206 pr_err("vgaarb: error %d registering device\n", rc
);
1208 bus_register_notifier(&pci_bus_type
, &pci_notifier
);
1210 /* We add all pci devices satisfying vga class in the arbiter by
1214 pci_get_subsys(PCI_ANY_ID
, PCI_ANY_ID
, PCI_ANY_ID
,
1215 PCI_ANY_ID
, pdev
)) != NULL
)
1216 vga_arbiter_add_pci_device(pdev
);
1218 pr_info("vgaarb: loaded\n");
1221 subsys_initcall(vga_arb_device_init
);