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 */
64 bool bridge_has_one_vga
;
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)
140 void vga_set_default_device(struct pci_dev
*pdev
)
146 static inline void vga_irq_set_state(struct vga_device
*vgadev
, bool state
)
148 if (vgadev
->irq_set_state
)
149 vgadev
->irq_set_state(vgadev
->cookie
, state
);
153 /* If we don't ever use VGA arb we should avoid
154 turning off anything anywhere due to old X servers getting
155 confused about the boot device not being VGA */
156 static void vga_check_first_use(void)
158 /* we should inform all GPUs in the system that
159 * VGA arb has occurred and to try and disable resources
161 if (!vga_arbiter_used
) {
162 vga_arbiter_used
= true;
163 vga_arbiter_notify_clients();
167 static struct vga_device
*__vga_tryget(struct vga_device
*vgadev
,
170 unsigned int wants
, legacy_wants
, match
;
171 struct vga_device
*conflict
;
172 unsigned int pci_bits
;
175 /* Account for "normal" resources to lock. If we decode the legacy,
176 * counterpart, we need to request it as well
178 if ((rsrc
& VGA_RSRC_NORMAL_IO
) &&
179 (vgadev
->decodes
& VGA_RSRC_LEGACY_IO
))
180 rsrc
|= VGA_RSRC_LEGACY_IO
;
181 if ((rsrc
& VGA_RSRC_NORMAL_MEM
) &&
182 (vgadev
->decodes
& VGA_RSRC_LEGACY_MEM
))
183 rsrc
|= VGA_RSRC_LEGACY_MEM
;
185 pr_debug("%s: %d\n", __func__
, rsrc
);
186 pr_debug("%s: owns: %d\n", __func__
, vgadev
->owns
);
188 /* Check what resources we need to acquire */
189 wants
= rsrc
& ~vgadev
->owns
;
191 /* We already own everything, just mark locked & bye bye */
195 /* We don't need to request a legacy resource, we just enable
196 * appropriate decoding and go
198 legacy_wants
= wants
& VGA_RSRC_LEGACY_MASK
;
199 if (legacy_wants
== 0)
202 /* Ok, we don't, let's find out how we need to kick off */
203 list_for_each_entry(conflict
, &vga_list
, list
) {
204 unsigned int lwants
= legacy_wants
;
205 unsigned int change_bridge
= 0;
207 /* Don't conflict with myself */
208 if (vgadev
== conflict
)
211 /* Check if the architecture allows a conflict between those
212 * 2 devices or if they are on separate domains
214 if (!vga_conflicts(vgadev
->pdev
, conflict
->pdev
))
217 /* We have a possible conflict. before we go further, we must
218 * check if we sit on the same bus as the conflicting device.
219 * if we don't, then we must tie both IO and MEM resources
220 * together since there is only a single bit controlling
221 * VGA forwarding on P2P bridges
223 if (vgadev
->pdev
->bus
!= conflict
->pdev
->bus
) {
225 lwants
= VGA_RSRC_LEGACY_IO
| VGA_RSRC_LEGACY_MEM
;
228 /* Check if the guy has a lock on the resource. If he does,
229 * return the conflicting entry
231 if (conflict
->locks
& lwants
)
234 /* Ok, now check if he owns the resource we want. We don't need
235 * to check "decodes" since it should be impossible to own
236 * own legacy resources you don't decode unless I have a bug
239 WARN_ON(conflict
->owns
& ~conflict
->decodes
);
240 match
= lwants
& conflict
->owns
;
244 /* looks like he doesn't have a lock, we can steal
251 if (!conflict
->bridge_has_one_vga
) {
252 vga_irq_set_state(conflict
, false);
253 flags
|= PCI_VGA_STATE_CHANGE_DECODES
;
254 if (lwants
& (VGA_RSRC_LEGACY_MEM
|VGA_RSRC_NORMAL_MEM
))
255 pci_bits
|= PCI_COMMAND_MEMORY
;
256 if (lwants
& (VGA_RSRC_LEGACY_IO
|VGA_RSRC_NORMAL_IO
))
257 pci_bits
|= PCI_COMMAND_IO
;
261 flags
|= PCI_VGA_STATE_CHANGE_BRIDGE
;
263 pci_set_vga_state(conflict
->pdev
, false, pci_bits
, flags
);
264 conflict
->owns
&= ~lwants
;
265 /* If he also owned non-legacy, that is no longer the case */
266 if (lwants
& VGA_RSRC_LEGACY_MEM
)
267 conflict
->owns
&= ~VGA_RSRC_NORMAL_MEM
;
268 if (lwants
& VGA_RSRC_LEGACY_IO
)
269 conflict
->owns
&= ~VGA_RSRC_NORMAL_IO
;
273 /* ok dude, we got it, everybody conflicting has been disabled, let's
274 * enable us. Make sure we don't mark a bit in "owns" that we don't
275 * also have in "decodes". We can lock resources we don't decode but
281 if (!vgadev
->bridge_has_one_vga
) {
282 flags
|= PCI_VGA_STATE_CHANGE_DECODES
;
283 if (wants
& (VGA_RSRC_LEGACY_MEM
|VGA_RSRC_NORMAL_MEM
))
284 pci_bits
|= PCI_COMMAND_MEMORY
;
285 if (wants
& (VGA_RSRC_LEGACY_IO
|VGA_RSRC_NORMAL_IO
))
286 pci_bits
|= PCI_COMMAND_IO
;
288 if (!!(wants
& VGA_RSRC_LEGACY_MASK
))
289 flags
|= PCI_VGA_STATE_CHANGE_BRIDGE
;
291 pci_set_vga_state(vgadev
->pdev
, true, pci_bits
, flags
);
293 if (!vgadev
->bridge_has_one_vga
) {
294 vga_irq_set_state(vgadev
, true);
296 vgadev
->owns
|= (wants
& vgadev
->decodes
);
298 vgadev
->locks
|= (rsrc
& VGA_RSRC_LEGACY_MASK
);
299 if (rsrc
& VGA_RSRC_LEGACY_IO
)
300 vgadev
->io_lock_cnt
++;
301 if (rsrc
& VGA_RSRC_LEGACY_MEM
)
302 vgadev
->mem_lock_cnt
++;
303 if (rsrc
& VGA_RSRC_NORMAL_IO
)
304 vgadev
->io_norm_cnt
++;
305 if (rsrc
& VGA_RSRC_NORMAL_MEM
)
306 vgadev
->mem_norm_cnt
++;
311 static void __vga_put(struct vga_device
*vgadev
, unsigned int rsrc
)
313 unsigned int old_locks
= vgadev
->locks
;
315 pr_debug("%s\n", __func__
);
317 /* Update our counters, and account for equivalent legacy resources
320 if ((rsrc
& VGA_RSRC_NORMAL_IO
) && vgadev
->io_norm_cnt
> 0) {
321 vgadev
->io_norm_cnt
--;
322 if (vgadev
->decodes
& VGA_RSRC_LEGACY_IO
)
323 rsrc
|= VGA_RSRC_LEGACY_IO
;
325 if ((rsrc
& VGA_RSRC_NORMAL_MEM
) && vgadev
->mem_norm_cnt
> 0) {
326 vgadev
->mem_norm_cnt
--;
327 if (vgadev
->decodes
& VGA_RSRC_LEGACY_MEM
)
328 rsrc
|= VGA_RSRC_LEGACY_MEM
;
330 if ((rsrc
& VGA_RSRC_LEGACY_IO
) && vgadev
->io_lock_cnt
> 0)
331 vgadev
->io_lock_cnt
--;
332 if ((rsrc
& VGA_RSRC_LEGACY_MEM
) && vgadev
->mem_lock_cnt
> 0)
333 vgadev
->mem_lock_cnt
--;
335 /* Just clear lock bits, we do lazy operations so we don't really
336 * have to bother about anything else at this point
338 if (vgadev
->io_lock_cnt
== 0)
339 vgadev
->locks
&= ~VGA_RSRC_LEGACY_IO
;
340 if (vgadev
->mem_lock_cnt
== 0)
341 vgadev
->locks
&= ~VGA_RSRC_LEGACY_MEM
;
343 /* Kick the wait queue in case somebody was waiting if we actually
346 if (old_locks
!= vgadev
->locks
)
347 wake_up_all(&vga_wait_queue
);
350 int vga_get(struct pci_dev
*pdev
, unsigned int rsrc
, int interruptible
)
352 struct vga_device
*vgadev
, *conflict
;
357 vga_check_first_use();
358 /* The one who calls us should check for this, but lets be sure... */
360 pdev
= vga_default_device();
365 spin_lock_irqsave(&vga_lock
, flags
);
366 vgadev
= vgadev_find(pdev
);
367 if (vgadev
== NULL
) {
368 spin_unlock_irqrestore(&vga_lock
, flags
);
372 conflict
= __vga_tryget(vgadev
, rsrc
);
373 spin_unlock_irqrestore(&vga_lock
, flags
);
374 if (conflict
== NULL
)
378 /* We have a conflict, we wait until somebody kicks the
379 * work queue. Currently we have one work queue that we
380 * kick each time some resources are released, but it would
381 * be fairly easy to have a per device one so that we only
382 * need to attach to the conflicting device
384 init_waitqueue_entry(&wait
, current
);
385 add_wait_queue(&vga_wait_queue
, &wait
);
386 set_current_state(interruptible
?
388 TASK_UNINTERRUPTIBLE
);
389 if (signal_pending(current
)) {
394 remove_wait_queue(&vga_wait_queue
, &wait
);
395 set_current_state(TASK_RUNNING
);
399 EXPORT_SYMBOL(vga_get
);
401 int vga_tryget(struct pci_dev
*pdev
, unsigned int rsrc
)
403 struct vga_device
*vgadev
;
407 vga_check_first_use();
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
);
416 if (vgadev
== NULL
) {
420 if (__vga_tryget(vgadev
, rsrc
))
423 spin_unlock_irqrestore(&vga_lock
, flags
);
426 EXPORT_SYMBOL(vga_tryget
);
428 void vga_put(struct pci_dev
*pdev
, unsigned int rsrc
)
430 struct vga_device
*vgadev
;
433 /* The one who calls us should check for this, but lets be sure... */
435 pdev
= vga_default_device();
438 spin_lock_irqsave(&vga_lock
, flags
);
439 vgadev
= vgadev_find(pdev
);
442 __vga_put(vgadev
, rsrc
);
444 spin_unlock_irqrestore(&vga_lock
, flags
);
446 EXPORT_SYMBOL(vga_put
);
448 /* Rules for using a bridge to control a VGA descendant decoding:
449 if a bridge has only one VGA descendant then it can be used
450 to control the VGA routing for that device.
451 It should always use the bridge closest to the device to control it.
452 If a bridge has a direct VGA descendant, but also have a sub-bridge
453 VGA descendant then we cannot use that bridge to control the direct VGA descendant.
454 So for every device we register, we need to iterate all its parent bridges
455 so we can invalidate any devices using them properly.
457 static void vga_arbiter_check_bridge_sharing(struct vga_device
*vgadev
)
459 struct vga_device
*same_bridge_vgadev
;
460 struct pci_bus
*new_bus
, *bus
;
461 struct pci_dev
*new_bridge
, *bridge
;
463 vgadev
->bridge_has_one_vga
= true;
465 if (list_empty(&vga_list
))
468 /* okay iterate the new devices bridge hierarachy */
469 new_bus
= vgadev
->pdev
->bus
;
471 new_bridge
= new_bus
->self
;
473 /* go through list of devices already registered */
474 list_for_each_entry(same_bridge_vgadev
, &vga_list
, list
) {
475 bus
= same_bridge_vgadev
->pdev
->bus
;
478 /* see if the share a bridge with this device */
479 if (new_bridge
== bridge
) {
480 /* if their direct parent bridge is the same
481 as any bridge of this device then it can't be used
483 same_bridge_vgadev
->bridge_has_one_vga
= false;
486 /* now iterate the previous devices bridge hierarchy */
487 /* if the new devices parent bridge is in the other devices
488 hierarchy then we can't use it to control this device */
492 if (bridge
== vgadev
->pdev
->bus
->self
)
493 vgadev
->bridge_has_one_vga
= false;
498 new_bus
= new_bus
->parent
;
503 * Currently, we assume that the "initial" setup of the system is
504 * not sane, that is we come up with conflicting devices and let
505 * the arbiter's client decides if devices decodes or not legacy
508 static bool vga_arbiter_add_pci_device(struct pci_dev
*pdev
)
510 struct vga_device
*vgadev
;
513 struct pci_dev
*bridge
;
516 /* Only deal with VGA class devices */
517 if ((pdev
->class >> 8) != PCI_CLASS_DISPLAY_VGA
)
520 /* Allocate structure */
521 vgadev
= kmalloc(sizeof(struct vga_device
), GFP_KERNEL
);
522 if (vgadev
== NULL
) {
523 pr_err("vgaarb: failed to allocate pci device\n");
524 /* What to do on allocation failure ? For now, let's
525 * just do nothing, I'm not sure there is anything saner
531 memset(vgadev
, 0, sizeof(*vgadev
));
533 /* Take lock & check for duplicates */
534 spin_lock_irqsave(&vga_lock
, flags
);
535 if (vgadev_find(pdev
) != NULL
) {
541 /* By default, assume we decode everything */
542 vgadev
->decodes
= VGA_RSRC_LEGACY_IO
| VGA_RSRC_LEGACY_MEM
|
543 VGA_RSRC_NORMAL_IO
| VGA_RSRC_NORMAL_MEM
;
545 /* by default mark it as decoding */
547 /* Mark that we "own" resources based on our enables, we will
548 * clear that below if the bridge isn't forwarding
550 pci_read_config_word(pdev
, PCI_COMMAND
, &cmd
);
551 if (cmd
& PCI_COMMAND_IO
)
552 vgadev
->owns
|= VGA_RSRC_LEGACY_IO
;
553 if (cmd
& PCI_COMMAND_MEMORY
)
554 vgadev
->owns
|= VGA_RSRC_LEGACY_MEM
;
556 /* Check if VGA cycles can get down to us */
562 pci_read_config_word(bridge
, PCI_BRIDGE_CONTROL
,
564 if (!(l
& PCI_BRIDGE_CTL_VGA
)) {
572 /* Deal with VGA default device. Use first enabled one
573 * by default if arch doesn't have it's own hook
575 #ifndef __ARCH_HAS_VGA_DEFAULT_DEVICE
576 if (vga_default
== NULL
&&
577 ((vgadev
->owns
& VGA_RSRC_LEGACY_MASK
) == VGA_RSRC_LEGACY_MASK
))
578 vga_default
= pci_dev_get(pdev
);
581 vga_arbiter_check_bridge_sharing(vgadev
);
583 /* Add to the list */
584 list_add(&vgadev
->list
, &vga_list
);
586 pr_info("vgaarb: device added: PCI:%s,decodes=%s,owns=%s,locks=%s\n",
588 vga_iostate_to_str(vgadev
->decodes
),
589 vga_iostate_to_str(vgadev
->owns
),
590 vga_iostate_to_str(vgadev
->locks
));
592 spin_unlock_irqrestore(&vga_lock
, flags
);
595 spin_unlock_irqrestore(&vga_lock
, flags
);
600 static bool vga_arbiter_del_pci_device(struct pci_dev
*pdev
)
602 struct vga_device
*vgadev
;
606 spin_lock_irqsave(&vga_lock
, flags
);
607 vgadev
= vgadev_find(pdev
);
608 if (vgadev
== NULL
) {
613 #ifndef __ARCH_HAS_VGA_DEFAULT_DEVICE
614 if (vga_default
== pdev
) {
615 pci_dev_put(vga_default
);
620 if (vgadev
->decodes
& (VGA_RSRC_LEGACY_IO
| VGA_RSRC_LEGACY_MEM
))
623 /* Remove entry from list */
624 list_del(&vgadev
->list
);
626 /* Notify userland driver that the device is gone so it discards
627 * it's copies of the pci_dev pointer
629 vga_arb_device_card_gone(pdev
);
631 /* Wake up all possible waiters */
632 wake_up_all(&vga_wait_queue
);
634 spin_unlock_irqrestore(&vga_lock
, flags
);
639 /* this is called with the lock */
640 static inline void vga_update_device_decodes(struct vga_device
*vgadev
,
644 struct vga_device
*new_vgadev
, *conflict
;
646 old_decodes
= vgadev
->decodes
;
647 vgadev
->decodes
= new_decodes
;
649 pr_info("vgaarb: device changed decodes: PCI:%s,olddecodes=%s,decodes=%s:owns=%s\n",
650 pci_name(vgadev
->pdev
),
651 vga_iostate_to_str(old_decodes
),
652 vga_iostate_to_str(vgadev
->decodes
),
653 vga_iostate_to_str(vgadev
->owns
));
656 /* if we own the decodes we should move them along to
658 if ((vgadev
->owns
& old_decodes
) && (vga_count
> 1)) {
659 /* set us to own nothing */
660 vgadev
->owns
&= ~old_decodes
;
661 list_for_each_entry(new_vgadev
, &vga_list
, list
) {
662 if ((new_vgadev
!= vgadev
) &&
663 (new_vgadev
->decodes
& VGA_RSRC_LEGACY_MASK
)) {
664 pr_info("vgaarb: transferring owner from PCI:%s to PCI:%s\n", pci_name(vgadev
->pdev
), pci_name(new_vgadev
->pdev
));
665 conflict
= __vga_tryget(new_vgadev
, VGA_RSRC_LEGACY_MASK
);
667 __vga_put(new_vgadev
, VGA_RSRC_LEGACY_MASK
);
673 /* change decodes counter */
674 if (old_decodes
!= new_decodes
) {
675 if (new_decodes
& (VGA_RSRC_LEGACY_IO
| VGA_RSRC_LEGACY_MEM
))
680 pr_debug("vgaarb: decoding count now is: %d\n", vga_decode_count
);
683 static void __vga_set_legacy_decoding(struct pci_dev
*pdev
, unsigned int decodes
, bool userspace
)
685 struct vga_device
*vgadev
;
688 decodes
&= VGA_RSRC_LEGACY_MASK
;
690 spin_lock_irqsave(&vga_lock
, flags
);
691 vgadev
= vgadev_find(pdev
);
695 /* don't let userspace futz with kernel driver decodes */
696 if (userspace
&& vgadev
->set_vga_decode
)
699 /* update the device decodes + counter */
700 vga_update_device_decodes(vgadev
, decodes
);
702 /* XXX if somebody is going from "doesn't decode" to "decodes" state
703 * here, additional care must be taken as we may have pending owner
704 * ship of non-legacy region ...
707 spin_unlock_irqrestore(&vga_lock
, flags
);
710 void vga_set_legacy_decoding(struct pci_dev
*pdev
, unsigned int decodes
)
712 __vga_set_legacy_decoding(pdev
, decodes
, false);
714 EXPORT_SYMBOL(vga_set_legacy_decoding
);
716 /* call with NULL to unregister */
717 int vga_client_register(struct pci_dev
*pdev
, void *cookie
,
718 void (*irq_set_state
)(void *cookie
, bool state
),
719 unsigned int (*set_vga_decode
)(void *cookie
, bool decode
))
722 struct vga_device
*vgadev
;
725 spin_lock_irqsave(&vga_lock
, flags
);
726 vgadev
= vgadev_find(pdev
);
730 vgadev
->irq_set_state
= irq_set_state
;
731 vgadev
->set_vga_decode
= set_vga_decode
;
732 vgadev
->cookie
= cookie
;
736 spin_unlock_irqrestore(&vga_lock
, flags
);
740 EXPORT_SYMBOL(vga_client_register
);
743 * Char driver implementation
747 * open : open user instance of the arbitrer. by default, it's
748 * attached to the default VGA device of the system.
750 * close : close user instance, release locks
752 * read : return a string indicating the status of the target.
753 * an IO state string is of the form {io,mem,io+mem,none},
754 * mc and ic are respectively mem and io lock counts (for
755 * debugging/diagnostic only). "decodes" indicate what the
756 * card currently decodes, "owns" indicates what is currently
757 * enabled on it, and "locks" indicates what is locked by this
758 * card. If the card is unplugged, we get "invalid" then for
759 * card_ID and an -ENODEV error is returned for any command
760 * until a new card is targeted
762 * "<card_ID>,decodes=<io_state>,owns=<io_state>,locks=<io_state> (ic,mc)"
764 * write : write a command to the arbiter. List of commands is:
766 * target <card_ID> : switch target to card <card_ID> (see below)
767 * lock <io_state> : acquires locks on target ("none" is invalid io_state)
768 * trylock <io_state> : non-blocking acquire locks on target
769 * unlock <io_state> : release locks on target
770 * unlock all : release all locks on target held by this user
771 * decodes <io_state> : set the legacy decoding attributes for the card
773 * poll : event if something change on any card (not just the target)
775 * card_ID is of the form "PCI:domain:bus:dev.fn". It can be set to "default"
776 * to go back to the system default card (TODO: not implemented yet).
777 * Currently, only PCI is supported as a prefix, but the userland API may
778 * support other bus types in the future, even if the current kernel
779 * implementation doesn't.
783 * The driver keeps track of which user has what locks on which card. It
784 * supports stacking, like the kernel one. This complexifies the implementation
785 * a bit, but makes the arbiter more tolerant to userspace problems and able
786 * to properly cleanup in all cases when a process dies.
787 * Currently, a max of 16 cards simultaneously can have locks issued from
788 * userspace for a given user (file descriptor instance) of the arbiter.
790 * If the device is hot-unplugged, there is a hook inside the module to notify
791 * they being added/removed in the system and automatically added/removed in
795 #define MAX_USER_CARDS CONFIG_VGA_ARB_MAX_GPUS
796 #define PCI_INVALID_CARD ((struct pci_dev *)-1UL)
799 * Each user has an array of these, tracking which cards have locks
801 struct vga_arb_user_card
{
802 struct pci_dev
*pdev
;
803 unsigned int mem_cnt
;
807 struct vga_arb_private
{
808 struct list_head list
;
809 struct pci_dev
*target
;
810 struct vga_arb_user_card cards
[MAX_USER_CARDS
];
814 static LIST_HEAD(vga_user_list
);
815 static DEFINE_SPINLOCK(vga_user_lock
);
819 * This function gets a string in the format: "PCI:domain:bus:dev.fn" and
820 * returns the respective values. If the string is not in this format,
823 static int vga_pci_str_to_vars(char *buf
, int count
, unsigned int *domain
,
824 unsigned int *bus
, unsigned int *devfn
)
827 unsigned int slot
, func
;
830 n
= sscanf(buf
, "PCI:%x:%x:%x.%x", domain
, bus
, &slot
, &func
);
834 *devfn
= PCI_DEVFN(slot
, func
);
839 static ssize_t
vga_arb_read(struct file
*file
, char __user
* buf
,
840 size_t count
, loff_t
*ppos
)
842 struct vga_arb_private
*priv
= file
->private_data
;
843 struct vga_device
*vgadev
;
844 struct pci_dev
*pdev
;
850 lbuf
= kmalloc(1024, GFP_KERNEL
);
854 /* Shields against vga_arb_device_card_gone (pci_dev going
855 * away), and allows access to vga list
857 spin_lock_irqsave(&vga_lock
, flags
);
859 /* If we are targeting the default, use it */
861 if (pdev
== NULL
|| pdev
== PCI_INVALID_CARD
) {
862 spin_unlock_irqrestore(&vga_lock
, flags
);
863 len
= sprintf(lbuf
, "invalid");
867 /* Find card vgadev structure */
868 vgadev
= vgadev_find(pdev
);
869 if (vgadev
== NULL
) {
870 /* Wow, it's not in the list, that shouldn't happen,
871 * let's fix us up and return invalid card
873 if (pdev
== priv
->target
)
874 vga_arb_device_card_gone(pdev
);
875 spin_unlock_irqrestore(&vga_lock
, flags
);
876 len
= sprintf(lbuf
, "invalid");
880 /* Fill the buffer with infos */
881 len
= snprintf(lbuf
, 1024,
882 "count:%d,PCI:%s,decodes=%s,owns=%s,locks=%s(%d:%d)\n",
883 vga_decode_count
, pci_name(pdev
),
884 vga_iostate_to_str(vgadev
->decodes
),
885 vga_iostate_to_str(vgadev
->owns
),
886 vga_iostate_to_str(vgadev
->locks
),
887 vgadev
->io_lock_cnt
, vgadev
->mem_lock_cnt
);
889 spin_unlock_irqrestore(&vga_lock
, flags
);
892 /* Copy that to user */
895 rc
= copy_to_user(buf
, lbuf
, len
);
903 * TODO: To avoid parsing inside kernel and to improve the speed we may
904 * consider use ioctl here
906 static ssize_t
vga_arb_write(struct file
*file
, const char __user
* buf
,
907 size_t count
, loff_t
*ppos
)
909 struct vga_arb_private
*priv
= file
->private_data
;
910 struct vga_arb_user_card
*uc
= NULL
;
911 struct pci_dev
*pdev
;
913 unsigned int io_state
;
915 char *kbuf
, *curr_pos
;
916 size_t remaining
= count
;
922 kbuf
= kmalloc(count
+ 1, GFP_KERNEL
);
926 if (copy_from_user(kbuf
, buf
, count
)) {
931 kbuf
[count
] = '\0'; /* Just to make sure... */
933 if (strncmp(curr_pos
, "lock ", 5) == 0) {
937 pr_debug("client 0x%p called 'lock'\n", priv
);
939 if (!vga_str_to_iostate(curr_pos
, remaining
, &io_state
)) {
943 if (io_state
== VGA_RSRC_NONE
) {
949 if (priv
->target
== NULL
) {
954 vga_get_uninterruptible(pdev
, io_state
);
956 /* Update the client's locks lists... */
957 for (i
= 0; i
< MAX_USER_CARDS
; i
++) {
958 if (priv
->cards
[i
].pdev
== pdev
) {
959 if (io_state
& VGA_RSRC_LEGACY_IO
)
960 priv
->cards
[i
].io_cnt
++;
961 if (io_state
& VGA_RSRC_LEGACY_MEM
)
962 priv
->cards
[i
].mem_cnt
++;
969 } else if (strncmp(curr_pos
, "unlock ", 7) == 0) {
973 pr_debug("client 0x%p called 'unlock'\n", priv
);
975 if (strncmp(curr_pos
, "all", 3) == 0)
976 io_state
= VGA_RSRC_LEGACY_IO
| VGA_RSRC_LEGACY_MEM
;
978 if (!vga_str_to_iostate
979 (curr_pos
, remaining
, &io_state
)) {
984 if (io_state == VGA_RSRC_NONE) {
992 if (priv
->target
== NULL
) {
996 for (i
= 0; i
< MAX_USER_CARDS
; i
++) {
997 if (priv
->cards
[i
].pdev
== pdev
)
998 uc
= &priv
->cards
[i
];
1006 if (io_state
& VGA_RSRC_LEGACY_IO
&& uc
->io_cnt
== 0) {
1011 if (io_state
& VGA_RSRC_LEGACY_MEM
&& uc
->mem_cnt
== 0) {
1016 vga_put(pdev
, io_state
);
1018 if (io_state
& VGA_RSRC_LEGACY_IO
)
1020 if (io_state
& VGA_RSRC_LEGACY_MEM
)
1025 } else if (strncmp(curr_pos
, "trylock ", 8) == 0) {
1029 pr_debug("client 0x%p called 'trylock'\n", priv
);
1031 if (!vga_str_to_iostate(curr_pos
, remaining
, &io_state
)) {
1036 if (io_state == VGA_RSRC_NONE) {
1042 pdev
= priv
->target
;
1043 if (priv
->target
== NULL
) {
1048 if (vga_tryget(pdev
, io_state
)) {
1049 /* Update the client's locks lists... */
1050 for (i
= 0; i
< MAX_USER_CARDS
; i
++) {
1051 if (priv
->cards
[i
].pdev
== pdev
) {
1052 if (io_state
& VGA_RSRC_LEGACY_IO
)
1053 priv
->cards
[i
].io_cnt
++;
1054 if (io_state
& VGA_RSRC_LEGACY_MEM
)
1055 priv
->cards
[i
].mem_cnt
++;
1066 } else if (strncmp(curr_pos
, "target ", 7) == 0) {
1067 struct pci_bus
*pbus
;
1068 unsigned int domain
, bus
, devfn
;
1069 struct vga_device
*vgadev
;
1073 pr_debug("client 0x%p called 'target'\n", priv
);
1074 /* if target is default */
1075 if (!strncmp(curr_pos
, "default", 7))
1076 pdev
= pci_dev_get(vga_default_device());
1078 if (!vga_pci_str_to_vars(curr_pos
, remaining
,
1079 &domain
, &bus
, &devfn
)) {
1083 pr_debug("vgaarb: %s ==> %x:%x:%x.%x\n", curr_pos
,
1084 domain
, bus
, PCI_SLOT(devfn
), PCI_FUNC(devfn
));
1086 pbus
= pci_find_bus(domain
, bus
);
1087 pr_debug("vgaarb: pbus %p\n", pbus
);
1089 pr_err("vgaarb: invalid PCI domain and/or bus address %x:%x\n",
1094 pdev
= pci_get_slot(pbus
, devfn
);
1095 pr_debug("vgaarb: pdev %p\n", pdev
);
1097 pr_err("vgaarb: invalid PCI address %x:%x\n",
1104 vgadev
= vgadev_find(pdev
);
1105 pr_debug("vgaarb: vgadev %p\n", vgadev
);
1106 if (vgadev
== NULL
) {
1107 pr_err("vgaarb: this pci device is not a vga device\n");
1113 priv
->target
= pdev
;
1114 for (i
= 0; i
< MAX_USER_CARDS
; i
++) {
1115 if (priv
->cards
[i
].pdev
== pdev
)
1117 if (priv
->cards
[i
].pdev
== NULL
) {
1118 priv
->cards
[i
].pdev
= pdev
;
1119 priv
->cards
[i
].io_cnt
= 0;
1120 priv
->cards
[i
].mem_cnt
= 0;
1124 if (i
== MAX_USER_CARDS
) {
1125 pr_err("vgaarb: maximum user cards (%d) number reached!\n",
1128 /* XXX: which value to return? */
1138 } else if (strncmp(curr_pos
, "decodes ", 8) == 0) {
1141 pr_debug("vgaarb: client 0x%p called 'decodes'\n", priv
);
1143 if (!vga_str_to_iostate(curr_pos
, remaining
, &io_state
)) {
1147 pdev
= priv
->target
;
1148 if (priv
->target
== NULL
) {
1153 __vga_set_legacy_decoding(pdev
, io_state
, true);
1157 /* If we got here, the message written is not part of the protocol! */
1166 static unsigned int vga_arb_fpoll(struct file
*file
, poll_table
* wait
)
1168 struct vga_arb_private
*priv
= file
->private_data
;
1170 pr_debug("%s\n", __func__
);
1174 poll_wait(file
, &vga_wait_queue
, wait
);
1178 static int vga_arb_open(struct inode
*inode
, struct file
*file
)
1180 struct vga_arb_private
*priv
;
1181 unsigned long flags
;
1183 pr_debug("%s\n", __func__
);
1185 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
1188 spin_lock_init(&priv
->lock
);
1189 file
->private_data
= priv
;
1191 spin_lock_irqsave(&vga_user_lock
, flags
);
1192 list_add(&priv
->list
, &vga_user_list
);
1193 spin_unlock_irqrestore(&vga_user_lock
, flags
);
1195 /* Set the client' lists of locks */
1196 priv
->target
= vga_default_device(); /* Maybe this is still null! */
1197 priv
->cards
[0].pdev
= priv
->target
;
1198 priv
->cards
[0].io_cnt
= 0;
1199 priv
->cards
[0].mem_cnt
= 0;
1205 static int vga_arb_release(struct inode
*inode
, struct file
*file
)
1207 struct vga_arb_private
*priv
= file
->private_data
;
1208 struct vga_arb_user_card
*uc
;
1209 unsigned long flags
;
1212 pr_debug("%s\n", __func__
);
1217 spin_lock_irqsave(&vga_user_lock
, flags
);
1218 list_del(&priv
->list
);
1219 for (i
= 0; i
< MAX_USER_CARDS
; i
++) {
1220 uc
= &priv
->cards
[i
];
1221 if (uc
->pdev
== NULL
)
1223 pr_debug("uc->io_cnt == %d, uc->mem_cnt == %d\n",
1224 uc
->io_cnt
, uc
->mem_cnt
);
1225 while (uc
->io_cnt
--)
1226 vga_put(uc
->pdev
, VGA_RSRC_LEGACY_IO
);
1227 while (uc
->mem_cnt
--)
1228 vga_put(uc
->pdev
, VGA_RSRC_LEGACY_MEM
);
1230 spin_unlock_irqrestore(&vga_user_lock
, flags
);
1237 static void vga_arb_device_card_gone(struct pci_dev
*pdev
)
1242 * callback any registered clients to let them know we have a
1243 * change in VGA cards
1245 static void vga_arbiter_notify_clients(void)
1247 struct vga_device
*vgadev
;
1248 unsigned long flags
;
1249 uint32_t new_decodes
;
1252 if (!vga_arbiter_used
)
1255 spin_lock_irqsave(&vga_lock
, flags
);
1256 list_for_each_entry(vgadev
, &vga_list
, list
) {
1261 if (vgadev
->set_vga_decode
) {
1262 new_decodes
= vgadev
->set_vga_decode(vgadev
->cookie
, new_state
);
1263 vga_update_device_decodes(vgadev
, new_decodes
);
1266 spin_unlock_irqrestore(&vga_lock
, flags
);
1269 static int pci_notify(struct notifier_block
*nb
, unsigned long action
,
1272 struct device
*dev
= data
;
1273 struct pci_dev
*pdev
= to_pci_dev(dev
);
1274 bool notify
= false;
1276 pr_debug("%s\n", __func__
);
1278 /* For now we're only intereted in devices added and removed. I didn't
1279 * test this thing here, so someone needs to double check for the
1280 * cases of hotplugable vga cards. */
1281 if (action
== BUS_NOTIFY_ADD_DEVICE
)
1282 notify
= vga_arbiter_add_pci_device(pdev
);
1283 else if (action
== BUS_NOTIFY_DEL_DEVICE
)
1284 notify
= vga_arbiter_del_pci_device(pdev
);
1287 vga_arbiter_notify_clients();
1291 static struct notifier_block pci_notifier
= {
1292 .notifier_call
= pci_notify
,
1295 static const struct file_operations vga_arb_device_fops
= {
1296 .read
= vga_arb_read
,
1297 .write
= vga_arb_write
,
1298 .poll
= vga_arb_fpoll
,
1299 .open
= vga_arb_open
,
1300 .release
= vga_arb_release
,
1301 .llseek
= noop_llseek
,
1304 static struct miscdevice vga_arb_device
= {
1305 MISC_DYNAMIC_MINOR
, "vga_arbiter", &vga_arb_device_fops
1308 static int __init
vga_arb_device_init(void)
1311 struct pci_dev
*pdev
;
1312 struct vga_device
*vgadev
;
1314 rc
= misc_register(&vga_arb_device
);
1316 pr_err("vgaarb: error %d registering device\n", rc
);
1318 bus_register_notifier(&pci_bus_type
, &pci_notifier
);
1320 /* We add all pci devices satisfying vga class in the arbiter by
1324 pci_get_subsys(PCI_ANY_ID
, PCI_ANY_ID
, PCI_ANY_ID
,
1325 PCI_ANY_ID
, pdev
)) != NULL
)
1326 vga_arbiter_add_pci_device(pdev
);
1328 pr_info("vgaarb: loaded\n");
1330 list_for_each_entry(vgadev
, &vga_list
, list
) {
1331 if (vgadev
->bridge_has_one_vga
)
1332 pr_info("vgaarb: bridge control possible %s\n", pci_name(vgadev
->pdev
));
1334 pr_info("vgaarb: no bridge control possible %s\n", pci_name(vgadev
->pdev
));
1338 subsys_initcall(vga_arb_device_init
);