2 * vgaarb.c: Implements the VGA arbitration. For details refer to
3 * Documentation/vgaarbiter.txt
6 * (C) Copyright 2005 Benjamin Herrenschmidt <benh@kernel.crashing.org>
7 * (C) Copyright 2007 Paulo R. Zanoni <przanoni@gmail.com>
8 * (C) Copyright 2007, 2009 Tiago Vignatti <vignatti@freedesktop.org>
10 * Permission is hereby granted, free of charge, to any person obtaining a
11 * copy of this software and associated documentation files (the "Software"),
12 * to deal in the Software without restriction, including without limitation
13 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 * and/or sell copies of the Software, and to permit persons to whom the
15 * Software is furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice (including the next
18 * paragraph) shall be included in all copies or substantial portions of the
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32 #include <linux/module.h>
33 #include <linux/kernel.h>
34 #include <linux/pci.h>
35 #include <linux/errno.h>
36 #include <linux/init.h>
37 #include <linux/list.h>
38 #include <linux/sched.h>
39 #include <linux/wait.h>
40 #include <linux/spinlock.h>
41 #include <linux/poll.h>
42 #include <linux/miscdevice.h>
43 #include <linux/slab.h>
45 #include <linux/uaccess.h>
47 #include <linux/vgaarb.h>
49 static void vga_arbiter_notify_clients(void);
51 * We keep a list of all vga devices in the system to speed
52 * up the various operations of the arbiter
55 struct list_head list
;
57 unsigned int decodes
; /* what does it decodes */
58 unsigned int owns
; /* what does it owns */
59 unsigned int locks
; /* what does it locks */
60 unsigned int io_lock_cnt
; /* legacy IO lock count */
61 unsigned int mem_lock_cnt
; /* legacy MEM lock count */
62 unsigned int io_norm_cnt
; /* normal IO count */
63 unsigned int mem_norm_cnt
; /* normal MEM count */
65 /* allow IRQ enable/disable hook */
67 void (*irq_set_state
)(void *cookie
, bool enable
);
68 unsigned int (*set_vga_decode
)(void *cookie
, bool decode
);
71 static LIST_HEAD(vga_list
);
72 static int vga_count
, vga_decode_count
;
73 static bool vga_arbiter_used
;
74 static DEFINE_SPINLOCK(vga_lock
);
75 static DECLARE_WAIT_QUEUE_HEAD(vga_wait_queue
);
78 static const char *vga_iostate_to_str(unsigned int iostate
)
80 /* Ignore VGA_RSRC_IO and VGA_RSRC_MEM */
81 iostate
&= VGA_RSRC_LEGACY_IO
| VGA_RSRC_LEGACY_MEM
;
83 case VGA_RSRC_LEGACY_IO
| VGA_RSRC_LEGACY_MEM
:
85 case VGA_RSRC_LEGACY_IO
:
87 case VGA_RSRC_LEGACY_MEM
:
93 static int vga_str_to_iostate(char *buf
, int str_size
, int *io_state
)
95 /* we could in theory hand out locks on IO and mem
96 * separately to userspace but it can cause deadlocks */
97 if (strncmp(buf
, "none", 4) == 0) {
98 *io_state
= VGA_RSRC_NONE
;
102 /* XXX We're not chekcing the str_size! */
103 if (strncmp(buf
, "io+mem", 6) == 0)
105 else if (strncmp(buf
, "io", 2) == 0)
107 else if (strncmp(buf
, "mem", 3) == 0)
111 *io_state
= VGA_RSRC_LEGACY_IO
| VGA_RSRC_LEGACY_MEM
;
115 #ifndef __ARCH_HAS_VGA_DEFAULT_DEVICE
116 /* this is only used a cookie - it should not be dereferenced */
117 static struct pci_dev
*vga_default
;
120 static void vga_arb_device_card_gone(struct pci_dev
*pdev
);
122 /* Find somebody in our list */
123 static struct vga_device
*vgadev_find(struct pci_dev
*pdev
)
125 struct vga_device
*vgadev
;
127 list_for_each_entry(vgadev
, &vga_list
, list
)
128 if (pdev
== vgadev
->pdev
)
133 /* Returns the default VGA device (vgacon's babe) */
134 #ifndef __ARCH_HAS_VGA_DEFAULT_DEVICE
135 struct pci_dev
*vga_default_device(void)
141 static inline void vga_irq_set_state(struct vga_device
*vgadev
, bool state
)
143 if (vgadev
->irq_set_state
)
144 vgadev
->irq_set_state(vgadev
->cookie
, state
);
148 /* If we don't ever use VGA arb we should avoid
149 turning off anything anywhere due to old X servers getting
150 confused about the boot device not being VGA */
151 static void vga_check_first_use(void)
153 /* we should inform all GPUs in the system that
154 * VGA arb has occured and to try and disable resources
156 if (!vga_arbiter_used
) {
157 vga_arbiter_used
= true;
158 vga_arbiter_notify_clients();
162 static struct vga_device
*__vga_tryget(struct vga_device
*vgadev
,
165 unsigned int wants
, legacy_wants
, match
;
166 struct vga_device
*conflict
;
167 unsigned int pci_bits
;
168 /* Account for "normal" resources to lock. If we decode the legacy,
169 * counterpart, we need to request it as well
171 if ((rsrc
& VGA_RSRC_NORMAL_IO
) &&
172 (vgadev
->decodes
& VGA_RSRC_LEGACY_IO
))
173 rsrc
|= VGA_RSRC_LEGACY_IO
;
174 if ((rsrc
& VGA_RSRC_NORMAL_MEM
) &&
175 (vgadev
->decodes
& VGA_RSRC_LEGACY_MEM
))
176 rsrc
|= VGA_RSRC_LEGACY_MEM
;
178 pr_debug("%s: %d\n", __func__
, rsrc
);
179 pr_debug("%s: owns: %d\n", __func__
, vgadev
->owns
);
181 /* Check what resources we need to acquire */
182 wants
= rsrc
& ~vgadev
->owns
;
184 /* We already own everything, just mark locked & bye bye */
188 /* We don't need to request a legacy resource, we just enable
189 * appropriate decoding and go
191 legacy_wants
= wants
& VGA_RSRC_LEGACY_MASK
;
192 if (legacy_wants
== 0)
195 /* Ok, we don't, let's find out how we need to kick off */
196 list_for_each_entry(conflict
, &vga_list
, list
) {
197 unsigned int lwants
= legacy_wants
;
198 unsigned int change_bridge
= 0;
200 /* Don't conflict with myself */
201 if (vgadev
== conflict
)
204 /* Check if the architecture allows a conflict between those
205 * 2 devices or if they are on separate domains
207 if (!vga_conflicts(vgadev
->pdev
, conflict
->pdev
))
210 /* We have a possible conflict. before we go further, we must
211 * check if we sit on the same bus as the conflicting device.
212 * if we don't, then we must tie both IO and MEM resources
213 * together since there is only a single bit controlling
214 * VGA forwarding on P2P bridges
216 if (vgadev
->pdev
->bus
!= conflict
->pdev
->bus
) {
218 lwants
= VGA_RSRC_LEGACY_IO
| VGA_RSRC_LEGACY_MEM
;
221 /* Check if the guy has a lock on the resource. If he does,
222 * return the conflicting entry
224 if (conflict
->locks
& lwants
)
227 /* Ok, now check if he owns the resource we want. We don't need
228 * to check "decodes" since it should be impossible to own
229 * own legacy resources you don't decode unless I have a bug
232 WARN_ON(conflict
->owns
& ~conflict
->decodes
);
233 match
= lwants
& conflict
->owns
;
237 /* looks like he doesn't have a lock, we can steal
240 vga_irq_set_state(conflict
, false);
243 if (lwants
& (VGA_RSRC_LEGACY_MEM
|VGA_RSRC_NORMAL_MEM
))
244 pci_bits
|= PCI_COMMAND_MEMORY
;
245 if (lwants
& (VGA_RSRC_LEGACY_IO
|VGA_RSRC_NORMAL_IO
))
246 pci_bits
|= PCI_COMMAND_IO
;
248 pci_set_vga_state(conflict
->pdev
, false, pci_bits
,
250 conflict
->owns
&= ~lwants
;
251 /* If he also owned non-legacy, that is no longer the case */
252 if (lwants
& VGA_RSRC_LEGACY_MEM
)
253 conflict
->owns
&= ~VGA_RSRC_NORMAL_MEM
;
254 if (lwants
& VGA_RSRC_LEGACY_IO
)
255 conflict
->owns
&= ~VGA_RSRC_NORMAL_IO
;
259 /* ok dude, we got it, everybody conflicting has been disabled, let's
260 * enable us. Make sure we don't mark a bit in "owns" that we don't
261 * also have in "decodes". We can lock resources we don't decode but
265 if (wants
& (VGA_RSRC_LEGACY_MEM
|VGA_RSRC_NORMAL_MEM
))
266 pci_bits
|= PCI_COMMAND_MEMORY
;
267 if (wants
& (VGA_RSRC_LEGACY_IO
|VGA_RSRC_NORMAL_IO
))
268 pci_bits
|= PCI_COMMAND_IO
;
269 pci_set_vga_state(vgadev
->pdev
, true, pci_bits
, !!(wants
& VGA_RSRC_LEGACY_MASK
));
271 vga_irq_set_state(vgadev
, true);
272 vgadev
->owns
|= (wants
& vgadev
->decodes
);
274 vgadev
->locks
|= (rsrc
& VGA_RSRC_LEGACY_MASK
);
275 if (rsrc
& VGA_RSRC_LEGACY_IO
)
276 vgadev
->io_lock_cnt
++;
277 if (rsrc
& VGA_RSRC_LEGACY_MEM
)
278 vgadev
->mem_lock_cnt
++;
279 if (rsrc
& VGA_RSRC_NORMAL_IO
)
280 vgadev
->io_norm_cnt
++;
281 if (rsrc
& VGA_RSRC_NORMAL_MEM
)
282 vgadev
->mem_norm_cnt
++;
287 static void __vga_put(struct vga_device
*vgadev
, unsigned int rsrc
)
289 unsigned int old_locks
= vgadev
->locks
;
291 pr_debug("%s\n", __func__
);
293 /* Update our counters, and account for equivalent legacy resources
296 if ((rsrc
& VGA_RSRC_NORMAL_IO
) && vgadev
->io_norm_cnt
> 0) {
297 vgadev
->io_norm_cnt
--;
298 if (vgadev
->decodes
& VGA_RSRC_LEGACY_IO
)
299 rsrc
|= VGA_RSRC_LEGACY_IO
;
301 if ((rsrc
& VGA_RSRC_NORMAL_MEM
) && vgadev
->mem_norm_cnt
> 0) {
302 vgadev
->mem_norm_cnt
--;
303 if (vgadev
->decodes
& VGA_RSRC_LEGACY_MEM
)
304 rsrc
|= VGA_RSRC_LEGACY_MEM
;
306 if ((rsrc
& VGA_RSRC_LEGACY_IO
) && vgadev
->io_lock_cnt
> 0)
307 vgadev
->io_lock_cnt
--;
308 if ((rsrc
& VGA_RSRC_LEGACY_MEM
) && vgadev
->mem_lock_cnt
> 0)
309 vgadev
->mem_lock_cnt
--;
311 /* Just clear lock bits, we do lazy operations so we don't really
312 * have to bother about anything else at this point
314 if (vgadev
->io_lock_cnt
== 0)
315 vgadev
->locks
&= ~VGA_RSRC_LEGACY_IO
;
316 if (vgadev
->mem_lock_cnt
== 0)
317 vgadev
->locks
&= ~VGA_RSRC_LEGACY_MEM
;
319 /* Kick the wait queue in case somebody was waiting if we actually
322 if (old_locks
!= vgadev
->locks
)
323 wake_up_all(&vga_wait_queue
);
326 int vga_get(struct pci_dev
*pdev
, unsigned int rsrc
, int interruptible
)
328 struct vga_device
*vgadev
, *conflict
;
333 vga_check_first_use();
334 /* The one who calls us should check for this, but lets be sure... */
336 pdev
= vga_default_device();
341 spin_lock_irqsave(&vga_lock
, flags
);
342 vgadev
= vgadev_find(pdev
);
343 if (vgadev
== NULL
) {
344 spin_unlock_irqrestore(&vga_lock
, flags
);
348 conflict
= __vga_tryget(vgadev
, rsrc
);
349 spin_unlock_irqrestore(&vga_lock
, flags
);
350 if (conflict
== NULL
)
354 /* We have a conflict, we wait until somebody kicks the
355 * work queue. Currently we have one work queue that we
356 * kick each time some resources are released, but it would
357 * be fairly easy to have a per device one so that we only
358 * need to attach to the conflicting device
360 init_waitqueue_entry(&wait
, current
);
361 add_wait_queue(&vga_wait_queue
, &wait
);
362 set_current_state(interruptible
?
364 TASK_UNINTERRUPTIBLE
);
365 if (signal_pending(current
)) {
370 remove_wait_queue(&vga_wait_queue
, &wait
);
371 set_current_state(TASK_RUNNING
);
375 EXPORT_SYMBOL(vga_get
);
377 int vga_tryget(struct pci_dev
*pdev
, unsigned int rsrc
)
379 struct vga_device
*vgadev
;
383 vga_check_first_use();
385 /* The one who calls us should check for this, but lets be sure... */
387 pdev
= vga_default_device();
390 spin_lock_irqsave(&vga_lock
, flags
);
391 vgadev
= vgadev_find(pdev
);
392 if (vgadev
== NULL
) {
396 if (__vga_tryget(vgadev
, rsrc
))
399 spin_unlock_irqrestore(&vga_lock
, flags
);
402 EXPORT_SYMBOL(vga_tryget
);
404 void vga_put(struct pci_dev
*pdev
, unsigned int rsrc
)
406 struct vga_device
*vgadev
;
409 /* The one who calls us should check for this, but lets be sure... */
411 pdev
= vga_default_device();
414 spin_lock_irqsave(&vga_lock
, flags
);
415 vgadev
= vgadev_find(pdev
);
418 __vga_put(vgadev
, rsrc
);
420 spin_unlock_irqrestore(&vga_lock
, flags
);
422 EXPORT_SYMBOL(vga_put
);
425 * Currently, we assume that the "initial" setup of the system is
426 * not sane, that is we come up with conflicting devices and let
427 * the arbiter's client decides if devices decodes or not legacy
430 static bool vga_arbiter_add_pci_device(struct pci_dev
*pdev
)
432 struct vga_device
*vgadev
;
435 struct pci_dev
*bridge
;
438 /* Only deal with VGA class devices */
439 if ((pdev
->class >> 8) != PCI_CLASS_DISPLAY_VGA
)
442 /* Allocate structure */
443 vgadev
= kmalloc(sizeof(struct vga_device
), GFP_KERNEL
);
444 if (vgadev
== NULL
) {
445 pr_err("vgaarb: failed to allocate pci device\n");
446 /* What to do on allocation failure ? For now, let's
447 * just do nothing, I'm not sure there is anything saner
453 memset(vgadev
, 0, sizeof(*vgadev
));
455 /* Take lock & check for duplicates */
456 spin_lock_irqsave(&vga_lock
, flags
);
457 if (vgadev_find(pdev
) != NULL
) {
463 /* By default, assume we decode everything */
464 vgadev
->decodes
= VGA_RSRC_LEGACY_IO
| VGA_RSRC_LEGACY_MEM
|
465 VGA_RSRC_NORMAL_IO
| VGA_RSRC_NORMAL_MEM
;
467 /* by default mark it as decoding */
469 /* Mark that we "own" resources based on our enables, we will
470 * clear that below if the bridge isn't forwarding
472 pci_read_config_word(pdev
, PCI_COMMAND
, &cmd
);
473 if (cmd
& PCI_COMMAND_IO
)
474 vgadev
->owns
|= VGA_RSRC_LEGACY_IO
;
475 if (cmd
& PCI_COMMAND_MEMORY
)
476 vgadev
->owns
|= VGA_RSRC_LEGACY_MEM
;
478 /* Check if VGA cycles can get down to us */
484 pci_read_config_word(bridge
, PCI_BRIDGE_CONTROL
,
486 if (!(l
& PCI_BRIDGE_CTL_VGA
)) {
494 /* Deal with VGA default device. Use first enabled one
495 * by default if arch doesn't have it's own hook
497 #ifndef __ARCH_HAS_VGA_DEFAULT_DEVICE
498 if (vga_default
== NULL
&&
499 ((vgadev
->owns
& VGA_RSRC_LEGACY_MASK
) == VGA_RSRC_LEGACY_MASK
))
500 vga_default
= pci_dev_get(pdev
);
503 /* Add to the list */
504 list_add(&vgadev
->list
, &vga_list
);
506 pr_info("vgaarb: device added: PCI:%s,decodes=%s,owns=%s,locks=%s\n",
508 vga_iostate_to_str(vgadev
->decodes
),
509 vga_iostate_to_str(vgadev
->owns
),
510 vga_iostate_to_str(vgadev
->locks
));
512 spin_unlock_irqrestore(&vga_lock
, flags
);
515 spin_unlock_irqrestore(&vga_lock
, flags
);
520 static bool vga_arbiter_del_pci_device(struct pci_dev
*pdev
)
522 struct vga_device
*vgadev
;
526 spin_lock_irqsave(&vga_lock
, flags
);
527 vgadev
= vgadev_find(pdev
);
528 if (vgadev
== NULL
) {
533 if (vga_default
== pdev
) {
534 pci_dev_put(vga_default
);
538 if (vgadev
->decodes
& (VGA_RSRC_LEGACY_IO
| VGA_RSRC_LEGACY_MEM
))
541 /* Remove entry from list */
542 list_del(&vgadev
->list
);
544 /* Notify userland driver that the device is gone so it discards
545 * it's copies of the pci_dev pointer
547 vga_arb_device_card_gone(pdev
);
549 /* Wake up all possible waiters */
550 wake_up_all(&vga_wait_queue
);
552 spin_unlock_irqrestore(&vga_lock
, flags
);
557 /* this is called with the lock */
558 static inline void vga_update_device_decodes(struct vga_device
*vgadev
,
562 struct vga_device
*new_vgadev
, *conflict
;
564 old_decodes
= vgadev
->decodes
;
565 vgadev
->decodes
= new_decodes
;
567 pr_info("vgaarb: device changed decodes: PCI:%s,olddecodes=%s,decodes=%s:owns=%s\n",
568 pci_name(vgadev
->pdev
),
569 vga_iostate_to_str(old_decodes
),
570 vga_iostate_to_str(vgadev
->decodes
),
571 vga_iostate_to_str(vgadev
->owns
));
574 /* if we own the decodes we should move them along to
576 if ((vgadev
->owns
& old_decodes
) && (vga_count
> 1)) {
577 /* set us to own nothing */
578 vgadev
->owns
&= ~old_decodes
;
579 list_for_each_entry(new_vgadev
, &vga_list
, list
) {
580 if ((new_vgadev
!= vgadev
) &&
581 (new_vgadev
->decodes
& VGA_RSRC_LEGACY_MASK
)) {
582 pr_info("vgaarb: transferring owner from PCI:%s to PCI:%s\n", pci_name(vgadev
->pdev
), pci_name(new_vgadev
->pdev
));
583 conflict
= __vga_tryget(new_vgadev
, VGA_RSRC_LEGACY_MASK
);
585 __vga_put(new_vgadev
, VGA_RSRC_LEGACY_MASK
);
591 /* change decodes counter */
592 if (old_decodes
!= new_decodes
) {
593 if (new_decodes
& (VGA_RSRC_LEGACY_IO
| VGA_RSRC_LEGACY_MEM
))
598 pr_debug("vgaarb: decoding count now is: %d\n", vga_decode_count
);
601 static void __vga_set_legacy_decoding(struct pci_dev
*pdev
, unsigned int decodes
, bool userspace
)
603 struct vga_device
*vgadev
;
606 decodes
&= VGA_RSRC_LEGACY_MASK
;
608 spin_lock_irqsave(&vga_lock
, flags
);
609 vgadev
= vgadev_find(pdev
);
613 /* don't let userspace futz with kernel driver decodes */
614 if (userspace
&& vgadev
->set_vga_decode
)
617 /* update the device decodes + counter */
618 vga_update_device_decodes(vgadev
, decodes
);
620 /* XXX if somebody is going from "doesn't decode" to "decodes" state
621 * here, additional care must be taken as we may have pending owner
622 * ship of non-legacy region ...
625 spin_unlock_irqrestore(&vga_lock
, flags
);
628 void vga_set_legacy_decoding(struct pci_dev
*pdev
, unsigned int decodes
)
630 __vga_set_legacy_decoding(pdev
, decodes
, false);
632 EXPORT_SYMBOL(vga_set_legacy_decoding
);
634 /* call with NULL to unregister */
635 int vga_client_register(struct pci_dev
*pdev
, void *cookie
,
636 void (*irq_set_state
)(void *cookie
, bool state
),
637 unsigned int (*set_vga_decode
)(void *cookie
, bool decode
))
640 struct vga_device
*vgadev
;
643 spin_lock_irqsave(&vga_lock
, flags
);
644 vgadev
= vgadev_find(pdev
);
648 vgadev
->irq_set_state
= irq_set_state
;
649 vgadev
->set_vga_decode
= set_vga_decode
;
650 vgadev
->cookie
= cookie
;
654 spin_unlock_irqrestore(&vga_lock
, flags
);
658 EXPORT_SYMBOL(vga_client_register
);
661 * Char driver implementation
665 * open : open user instance of the arbitrer. by default, it's
666 * attached to the default VGA device of the system.
668 * close : close user instance, release locks
670 * read : return a string indicating the status of the target.
671 * an IO state string is of the form {io,mem,io+mem,none},
672 * mc and ic are respectively mem and io lock counts (for
673 * debugging/diagnostic only). "decodes" indicate what the
674 * card currently decodes, "owns" indicates what is currently
675 * enabled on it, and "locks" indicates what is locked by this
676 * card. If the card is unplugged, we get "invalid" then for
677 * card_ID and an -ENODEV error is returned for any command
678 * until a new card is targeted
680 * "<card_ID>,decodes=<io_state>,owns=<io_state>,locks=<io_state> (ic,mc)"
682 * write : write a command to the arbiter. List of commands is:
684 * target <card_ID> : switch target to card <card_ID> (see below)
685 * lock <io_state> : acquires locks on target ("none" is invalid io_state)
686 * trylock <io_state> : non-blocking acquire locks on target
687 * unlock <io_state> : release locks on target
688 * unlock all : release all locks on target held by this user
689 * decodes <io_state> : set the legacy decoding attributes for the card
691 * poll : event if something change on any card (not just the target)
693 * card_ID is of the form "PCI:domain:bus:dev.fn". It can be set to "default"
694 * to go back to the system default card (TODO: not implemented yet).
695 * Currently, only PCI is supported as a prefix, but the userland API may
696 * support other bus types in the future, even if the current kernel
697 * implementation doesn't.
701 * The driver keeps track of which user has what locks on which card. It
702 * supports stacking, like the kernel one. This complexifies the implementation
703 * a bit, but makes the arbiter more tolerant to userspace problems and able
704 * to properly cleanup in all cases when a process dies.
705 * Currently, a max of 16 cards simultaneously can have locks issued from
706 * userspace for a given user (file descriptor instance) of the arbiter.
708 * If the device is hot-unplugged, there is a hook inside the module to notify
709 * they being added/removed in the system and automatically added/removed in
713 #define MAX_USER_CARDS CONFIG_VGA_ARB_MAX_GPUS
714 #define PCI_INVALID_CARD ((struct pci_dev *)-1UL)
717 * Each user has an array of these, tracking which cards have locks
719 struct vga_arb_user_card
{
720 struct pci_dev
*pdev
;
721 unsigned int mem_cnt
;
725 struct vga_arb_private
{
726 struct list_head list
;
727 struct pci_dev
*target
;
728 struct vga_arb_user_card cards
[MAX_USER_CARDS
];
732 static LIST_HEAD(vga_user_list
);
733 static DEFINE_SPINLOCK(vga_user_lock
);
737 * This function gets a string in the format: "PCI:domain:bus:dev.fn" and
738 * returns the respective values. If the string is not in this format,
741 static int vga_pci_str_to_vars(char *buf
, int count
, unsigned int *domain
,
742 unsigned int *bus
, unsigned int *devfn
)
745 unsigned int slot
, func
;
748 n
= sscanf(buf
, "PCI:%x:%x:%x.%x", domain
, bus
, &slot
, &func
);
752 *devfn
= PCI_DEVFN(slot
, func
);
757 static ssize_t
vga_arb_read(struct file
*file
, char __user
* buf
,
758 size_t count
, loff_t
*ppos
)
760 struct vga_arb_private
*priv
= file
->private_data
;
761 struct vga_device
*vgadev
;
762 struct pci_dev
*pdev
;
768 lbuf
= kmalloc(1024, GFP_KERNEL
);
772 /* Shields against vga_arb_device_card_gone (pci_dev going
773 * away), and allows access to vga list
775 spin_lock_irqsave(&vga_lock
, flags
);
777 /* If we are targetting the default, use it */
779 if (pdev
== NULL
|| pdev
== PCI_INVALID_CARD
) {
780 spin_unlock_irqrestore(&vga_lock
, flags
);
781 len
= sprintf(lbuf
, "invalid");
785 /* Find card vgadev structure */
786 vgadev
= vgadev_find(pdev
);
787 if (vgadev
== NULL
) {
788 /* Wow, it's not in the list, that shouldn't happen,
789 * let's fix us up and return invalid card
791 if (pdev
== priv
->target
)
792 vga_arb_device_card_gone(pdev
);
793 spin_unlock_irqrestore(&vga_lock
, flags
);
794 len
= sprintf(lbuf
, "invalid");
798 /* Fill the buffer with infos */
799 len
= snprintf(lbuf
, 1024,
800 "count:%d,PCI:%s,decodes=%s,owns=%s,locks=%s(%d:%d)\n",
801 vga_decode_count
, pci_name(pdev
),
802 vga_iostate_to_str(vgadev
->decodes
),
803 vga_iostate_to_str(vgadev
->owns
),
804 vga_iostate_to_str(vgadev
->locks
),
805 vgadev
->io_lock_cnt
, vgadev
->mem_lock_cnt
);
807 spin_unlock_irqrestore(&vga_lock
, flags
);
810 /* Copy that to user */
813 rc
= copy_to_user(buf
, lbuf
, len
);
821 * TODO: To avoid parsing inside kernel and to improve the speed we may
822 * consider use ioctl here
824 static ssize_t
vga_arb_write(struct file
*file
, const char __user
* buf
,
825 size_t count
, loff_t
*ppos
)
827 struct vga_arb_private
*priv
= file
->private_data
;
828 struct vga_arb_user_card
*uc
= NULL
;
829 struct pci_dev
*pdev
;
831 unsigned int io_state
;
833 char *kbuf
, *curr_pos
;
834 size_t remaining
= count
;
840 kbuf
= kmalloc(count
+ 1, GFP_KERNEL
);
844 if (copy_from_user(kbuf
, buf
, count
)) {
849 kbuf
[count
] = '\0'; /* Just to make sure... */
851 if (strncmp(curr_pos
, "lock ", 5) == 0) {
855 pr_debug("client 0x%p called 'lock'\n", priv
);
857 if (!vga_str_to_iostate(curr_pos
, remaining
, &io_state
)) {
861 if (io_state
== VGA_RSRC_NONE
) {
867 if (priv
->target
== NULL
) {
872 vga_get_uninterruptible(pdev
, io_state
);
874 /* Update the client's locks lists... */
875 for (i
= 0; i
< MAX_USER_CARDS
; i
++) {
876 if (priv
->cards
[i
].pdev
== pdev
) {
877 if (io_state
& VGA_RSRC_LEGACY_IO
)
878 priv
->cards
[i
].io_cnt
++;
879 if (io_state
& VGA_RSRC_LEGACY_MEM
)
880 priv
->cards
[i
].mem_cnt
++;
887 } else if (strncmp(curr_pos
, "unlock ", 7) == 0) {
891 pr_debug("client 0x%p called 'unlock'\n", priv
);
893 if (strncmp(curr_pos
, "all", 3) == 0)
894 io_state
= VGA_RSRC_LEGACY_IO
| VGA_RSRC_LEGACY_MEM
;
896 if (!vga_str_to_iostate
897 (curr_pos
, remaining
, &io_state
)) {
902 if (io_state == VGA_RSRC_NONE) {
910 if (priv
->target
== NULL
) {
914 for (i
= 0; i
< MAX_USER_CARDS
; i
++) {
915 if (priv
->cards
[i
].pdev
== pdev
)
916 uc
= &priv
->cards
[i
];
922 if (io_state
& VGA_RSRC_LEGACY_IO
&& uc
->io_cnt
== 0)
925 if (io_state
& VGA_RSRC_LEGACY_MEM
&& uc
->mem_cnt
== 0)
928 vga_put(pdev
, io_state
);
930 if (io_state
& VGA_RSRC_LEGACY_IO
)
932 if (io_state
& VGA_RSRC_LEGACY_MEM
)
937 } else if (strncmp(curr_pos
, "trylock ", 8) == 0) {
941 pr_debug("client 0x%p called 'trylock'\n", priv
);
943 if (!vga_str_to_iostate(curr_pos
, remaining
, &io_state
)) {
948 if (io_state == VGA_RSRC_NONE) {
955 if (priv
->target
== NULL
) {
960 if (vga_tryget(pdev
, io_state
)) {
961 /* Update the client's locks lists... */
962 for (i
= 0; i
< MAX_USER_CARDS
; i
++) {
963 if (priv
->cards
[i
].pdev
== pdev
) {
964 if (io_state
& VGA_RSRC_LEGACY_IO
)
965 priv
->cards
[i
].io_cnt
++;
966 if (io_state
& VGA_RSRC_LEGACY_MEM
)
967 priv
->cards
[i
].mem_cnt
++;
978 } else if (strncmp(curr_pos
, "target ", 7) == 0) {
979 struct pci_bus
*pbus
;
980 unsigned int domain
, bus
, devfn
;
981 struct vga_device
*vgadev
;
985 pr_debug("client 0x%p called 'target'\n", priv
);
986 /* if target is default */
987 if (!strncmp(curr_pos
, "default", 7))
988 pdev
= pci_dev_get(vga_default_device());
990 if (!vga_pci_str_to_vars(curr_pos
, remaining
,
991 &domain
, &bus
, &devfn
)) {
995 pr_debug("vgaarb: %s ==> %x:%x:%x.%x\n", curr_pos
,
996 domain
, bus
, PCI_SLOT(devfn
), PCI_FUNC(devfn
));
998 pbus
= pci_find_bus(domain
, bus
);
999 pr_debug("vgaarb: pbus %p\n", pbus
);
1001 pr_err("vgaarb: invalid PCI domain and/or bus address %x:%x\n",
1006 pdev
= pci_get_slot(pbus
, devfn
);
1007 pr_debug("vgaarb: pdev %p\n", pdev
);
1009 pr_err("vgaarb: invalid PCI address %x:%x\n",
1016 vgadev
= vgadev_find(pdev
);
1017 pr_debug("vgaarb: vgadev %p\n", vgadev
);
1018 if (vgadev
== NULL
) {
1019 pr_err("vgaarb: this pci device is not a vga device\n");
1025 priv
->target
= pdev
;
1026 for (i
= 0; i
< MAX_USER_CARDS
; i
++) {
1027 if (priv
->cards
[i
].pdev
== pdev
)
1029 if (priv
->cards
[i
].pdev
== NULL
) {
1030 priv
->cards
[i
].pdev
= pdev
;
1031 priv
->cards
[i
].io_cnt
= 0;
1032 priv
->cards
[i
].mem_cnt
= 0;
1036 if (i
== MAX_USER_CARDS
) {
1037 pr_err("vgaarb: maximum user cards (%d) number reached!\n",
1040 /* XXX: which value to return? */
1050 } else if (strncmp(curr_pos
, "decodes ", 8) == 0) {
1053 pr_debug("vgaarb: client 0x%p called 'decodes'\n", priv
);
1055 if (!vga_str_to_iostate(curr_pos
, remaining
, &io_state
)) {
1059 pdev
= priv
->target
;
1060 if (priv
->target
== NULL
) {
1065 __vga_set_legacy_decoding(pdev
, io_state
, true);
1069 /* If we got here, the message written is not part of the protocol! */
1078 static unsigned int vga_arb_fpoll(struct file
*file
, poll_table
* wait
)
1080 struct vga_arb_private
*priv
= file
->private_data
;
1082 pr_debug("%s\n", __func__
);
1086 poll_wait(file
, &vga_wait_queue
, wait
);
1090 static int vga_arb_open(struct inode
*inode
, struct file
*file
)
1092 struct vga_arb_private
*priv
;
1093 unsigned long flags
;
1095 pr_debug("%s\n", __func__
);
1097 priv
= kmalloc(sizeof(struct vga_arb_private
), GFP_KERNEL
);
1100 memset(priv
, 0, sizeof(*priv
));
1101 spin_lock_init(&priv
->lock
);
1102 file
->private_data
= priv
;
1104 spin_lock_irqsave(&vga_user_lock
, flags
);
1105 list_add(&priv
->list
, &vga_user_list
);
1106 spin_unlock_irqrestore(&vga_user_lock
, flags
);
1108 /* Set the client' lists of locks */
1109 priv
->target
= vga_default_device(); /* Maybe this is still null! */
1110 priv
->cards
[0].pdev
= priv
->target
;
1111 priv
->cards
[0].io_cnt
= 0;
1112 priv
->cards
[0].mem_cnt
= 0;
1118 static int vga_arb_release(struct inode
*inode
, struct file
*file
)
1120 struct vga_arb_private
*priv
= file
->private_data
;
1121 struct vga_arb_user_card
*uc
;
1122 unsigned long flags
;
1125 pr_debug("%s\n", __func__
);
1130 spin_lock_irqsave(&vga_user_lock
, flags
);
1131 list_del(&priv
->list
);
1132 for (i
= 0; i
< MAX_USER_CARDS
; i
++) {
1133 uc
= &priv
->cards
[i
];
1134 if (uc
->pdev
== NULL
)
1136 pr_debug("uc->io_cnt == %d, uc->mem_cnt == %d\n",
1137 uc
->io_cnt
, uc
->mem_cnt
);
1138 while (uc
->io_cnt
--)
1139 vga_put(uc
->pdev
, VGA_RSRC_LEGACY_IO
);
1140 while (uc
->mem_cnt
--)
1141 vga_put(uc
->pdev
, VGA_RSRC_LEGACY_MEM
);
1143 spin_unlock_irqrestore(&vga_user_lock
, flags
);
1150 static void vga_arb_device_card_gone(struct pci_dev
*pdev
)
1155 * callback any registered clients to let them know we have a
1156 * change in VGA cards
1158 static void vga_arbiter_notify_clients(void)
1160 struct vga_device
*vgadev
;
1161 unsigned long flags
;
1162 uint32_t new_decodes
;
1165 if (!vga_arbiter_used
)
1168 spin_lock_irqsave(&vga_lock
, flags
);
1169 list_for_each_entry(vgadev
, &vga_list
, list
) {
1174 if (vgadev
->set_vga_decode
) {
1175 new_decodes
= vgadev
->set_vga_decode(vgadev
->cookie
, new_state
);
1176 vga_update_device_decodes(vgadev
, new_decodes
);
1179 spin_unlock_irqrestore(&vga_lock
, flags
);
1182 static int pci_notify(struct notifier_block
*nb
, unsigned long action
,
1185 struct device
*dev
= data
;
1186 struct pci_dev
*pdev
= to_pci_dev(dev
);
1187 bool notify
= false;
1189 pr_debug("%s\n", __func__
);
1191 /* For now we're only intereted in devices added and removed. I didn't
1192 * test this thing here, so someone needs to double check for the
1193 * cases of hotplugable vga cards. */
1194 if (action
== BUS_NOTIFY_ADD_DEVICE
)
1195 notify
= vga_arbiter_add_pci_device(pdev
);
1196 else if (action
== BUS_NOTIFY_DEL_DEVICE
)
1197 notify
= vga_arbiter_del_pci_device(pdev
);
1200 vga_arbiter_notify_clients();
1204 static struct notifier_block pci_notifier
= {
1205 .notifier_call
= pci_notify
,
1208 static const struct file_operations vga_arb_device_fops
= {
1209 .read
= vga_arb_read
,
1210 .write
= vga_arb_write
,
1211 .poll
= vga_arb_fpoll
,
1212 .open
= vga_arb_open
,
1213 .release
= vga_arb_release
,
1214 .llseek
= noop_llseek
,
1217 static struct miscdevice vga_arb_device
= {
1218 MISC_DYNAMIC_MINOR
, "vga_arbiter", &vga_arb_device_fops
1221 static int __init
vga_arb_device_init(void)
1224 struct pci_dev
*pdev
;
1226 rc
= misc_register(&vga_arb_device
);
1228 pr_err("vgaarb: error %d registering device\n", rc
);
1230 bus_register_notifier(&pci_bus_type
, &pci_notifier
);
1232 /* We add all pci devices satisfying vga class in the arbiter by
1236 pci_get_subsys(PCI_ANY_ID
, PCI_ANY_ID
, PCI_ANY_ID
,
1237 PCI_ANY_ID
, pdev
)) != NULL
)
1238 vga_arbiter_add_pci_device(pdev
);
1240 pr_info("vgaarb: loaded\n");
1243 subsys_initcall(vga_arb_device_init
);